@samitouri / QOSamiQemu / commits / 25e92816c8

amd_iommu: Use switch case to determine mmio register name

This makes it easier to add new MMIO registers for tracing and removes the unnecessary complexity introduced by amdvi_mmio_(low/high) array. Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com> Reviewed-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260302115130.5903-2-sarunkod@amd.com>

Sairaj Kodilkar committed Mar 2, 2026 at 17:21 UTC 25e92816c8d1e4c4f35a1c076d9d5f3a90e17a93
2 files changed +27 -53
hw/i386/amd_iommu.c
+27 -49
@@ -35,29 +35,6 @@
35 #include "kvm/kvm_i386.h"
36 #include "qemu/iova-tree.h"
37
38 -/* used AMD-Vi MMIO registers */
39 -const char *amdvi_mmio_low[] = {
40 - "AMDVI_MMIO_DEVTAB_BASE",
41 - "AMDVI_MMIO_CMDBUF_BASE",
42 - "AMDVI_MMIO_EVTLOG_BASE",
43 - "AMDVI_MMIO_CONTROL",
44 - "AMDVI_MMIO_EXCL_BASE",
45 - "AMDVI_MMIO_EXCL_LIMIT",
46 - "AMDVI_MMIO_EXT_FEATURES",
47 - "AMDVI_MMIO_PPR_BASE",
48 - "UNHANDLED"
49 -};
50 -const char *amdvi_mmio_high[] = {
51 - "AMDVI_MMIO_COMMAND_HEAD",
52 - "AMDVI_MMIO_COMMAND_TAIL",
53 - "AMDVI_MMIO_EVTLOG_HEAD",
54 - "AMDVI_MMIO_EVTLOG_TAIL",
55 - "AMDVI_MMIO_STATUS",
56 - "AMDVI_MMIO_PPR_HEAD",
57 - "AMDVI_MMIO_PPR_TAIL",
58 - "UNHANDLED"
59 -};
60 -
38 struct AMDVIAddressSpace {
39 PCIBus *bus; /* PCIBus (for bus number) */
40 uint8_t devfn; /* device function */
@@ -1540,31 +1517,31 @@ static void amdvi_cmdbuf_run(AMDVIState *s)
1517 }
1518 }
1519
1543 -static inline uint8_t amdvi_mmio_get_index(hwaddr addr)
1544 -{
1545 - uint8_t index = (addr & ~0x2000) / 8;
1546 -
1547 - if ((addr & 0x2000)) {
1548 - /* high table */
1549 - index = index >= AMDVI_MMIO_REGS_HIGH ? AMDVI_MMIO_REGS_HIGH : index;
1550 - } else {
1551 - index = index >= AMDVI_MMIO_REGS_LOW ? AMDVI_MMIO_REGS_LOW : index;
1520 +static inline
1521 +const char *amdvi_mmio_get_name(hwaddr addr)
1522 +{
1523 + /* Return MMIO names as string literals */
1524 + switch (addr) {
1525 +#define MMIO_REG_TO_STRING(mmio_reg) case mmio_reg: return #mmio_reg
1526 + MMIO_REG_TO_STRING(AMDVI_MMIO_DEVICE_TABLE);
1527 + MMIO_REG_TO_STRING(AMDVI_MMIO_COMMAND_BASE);
1528 + MMIO_REG_TO_STRING(AMDVI_MMIO_EVENT_BASE);
1529 + MMIO_REG_TO_STRING(AMDVI_MMIO_CONTROL);
1530 + MMIO_REG_TO_STRING(AMDVI_MMIO_EXCL_BASE);
1531 + MMIO_REG_TO_STRING(AMDVI_MMIO_EXCL_LIMIT);
1532 + MMIO_REG_TO_STRING(AMDVI_MMIO_EXT_FEATURES);
1533 + MMIO_REG_TO_STRING(AMDVI_MMIO_COMMAND_HEAD);
1534 + MMIO_REG_TO_STRING(AMDVI_MMIO_COMMAND_TAIL);
1535 + MMIO_REG_TO_STRING(AMDVI_MMIO_EVENT_HEAD);
1536 + MMIO_REG_TO_STRING(AMDVI_MMIO_EVENT_TAIL);
1537 + MMIO_REG_TO_STRING(AMDVI_MMIO_STATUS);
1538 + MMIO_REG_TO_STRING(AMDVI_MMIO_PPR_BASE);
1539 + MMIO_REG_TO_STRING(AMDVI_MMIO_PPR_HEAD);
1540 + MMIO_REG_TO_STRING(AMDVI_MMIO_PPR_TAIL);
1541 +#undef MMIO_REG_TO_STRING
1542 + default:
1543 + return "UNHANDLED";
1544 }
1553 -
1554 - return index;
1555 -}
1556 -
1557 -static void amdvi_mmio_trace_read(hwaddr addr, unsigned size)
1558 -{
1559 - uint8_t index = amdvi_mmio_get_index(addr);
1560 - trace_amdvi_mmio_read(amdvi_mmio_low[index], addr, size, addr & ~0x07);
1561 -}
1562 -
1563 -static void amdvi_mmio_trace_write(hwaddr addr, unsigned size, uint64_t val)
1564 -{
1565 - uint8_t index = amdvi_mmio_get_index(addr);
1566 - trace_amdvi_mmio_write(amdvi_mmio_low[index], addr, size, val,
1567 - addr & ~0x07);
1545 }
1546
1547 static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
@@ -1584,7 +1561,7 @@ static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
1561 } else if (size == 8) {
1562 val = amdvi_readq(s, addr);
1563 }
1587 - amdvi_mmio_trace_read(addr, size);
1564 + trace_amdvi_mmio_read(amdvi_mmio_get_name(addr), addr, size, addr & ~0x07);
1565
1566 return val;
1567 }
@@ -1742,7 +1719,8 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1719 return;
1720 }
1721
1745 - amdvi_mmio_trace_write(addr, size, val);
1722 + trace_amdvi_mmio_write(amdvi_mmio_get_name(addr), addr, size, val, offset);
1723 +
1724 switch (addr & ~0x07) {
1725 case AMDVI_MMIO_CONTROL:
1726 amdvi_mmio_reg_write(s, size, val, addr);
hw/i386/amd_iommu.h
-4
@@ -45,10 +45,6 @@
45 #define AMDVI_CAPAB_FLAG_IOTLBSUP (1 << 24)
46 #define AMDVI_CAPAB_INIT_TYPE (3 << 16)
47
48 -/* No. of used MMIO registers */
49 -#define AMDVI_MMIO_REGS_HIGH 7
50 -#define AMDVI_MMIO_REGS_LOW 8
51 -
48 /* MMIO registers */
49 #define AMDVI_MMIO_DEVICE_TABLE 0x0000
50 #define AMDVI_MMIO_COMMAND_BASE 0x0008