acpi_build: Refactor and cleanup AMD IVRS build
cover acpi_build: Refactor and cleanup AMD IVRS build
This series aims to improve redability and maintainibility of the the AMD IVRS building by introducing macros and packed structs which represents IVRS, IVHDs headers and IVHD device entries.
The goal is to land a maintainable baseline before adding AMD hardware-accelerated vIOMMU support in QEMU, where we will require multiple vIOMMUs attached to different pcie-pxb buses in order to serve set of passthrough devices.
Please refer to AMD IOMMU spec [1] for more details about IVRS.
[1] https://docs.amd.com/api/khub/documents/GD6kOXjzWsek8QUbn_qMvg/content
Sairaj Kodilkar (8):
tests/acpi: x86: Allow IVRS acpi table changes
amd_iommu: update PA, GVA and VA size macros
amd_iommu: Return empty efr for stub call
acpi_build: Use IOMMU pci device to build IOMMU device ID
acpi_build: Introduce necessary macros and structs for AMD IOMMU IVRS
acpi_build: Build IVRS feature report using extended feature register
acpi_build: Cleanup AMD IOMMU IVRS building
tests/acpi: x86: update golden masters for IVRS
hw/i386/acpi-build.c | 191 +++++++++++++++++++-------------------- hw/i386/acpi-build.h | 93 ++++++++++++++++++- hw/i386/amd_iommu-stub.c | 2 +- hw/i386/amd_iommu.c | 2 +- hw/i386/amd_iommu.h | 12 ++- 5 files changed, 195 insertions(+), 105 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/8 tests/acpi: x86: Allow IVRS acpi table changes
--- tests/qtest/bios-tables-test-allowed-diff.h | 1 + 1 file changed, 1 insertion(+)
tests/qtest/bios-tables-test-allowed-diff.h +1 -0
@@ -1 +1,2 @@/* List of comma-separated changed AML files to ignore */ +tests/data/acpi/x86/q35/IVRS .ivrs
Signature · 1 line
2.34.1
PATCH 2/8 amd_iommu: update PA, GVA and VA size macros
In reply to Sairaj KodilkarCurrent implemenation supports upto 64 bit IOVA and upto 52 bit PA. Modify related macros so that guest can see right size.
--- hw/i386/acpi-build.c | 2 +- hw/i386/amd_iommu.c | 2 +- hw/i386/amd_iommu.h | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)
hw/i386/acpi-build.c +1 -1
@@ -1763,7 +1763,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,*/ build_append_int_noprefix (table_data, (1UL <<0 ) |/* EFRSup */ - (40UL <<8 ),/* PASize */ +AMDVI_PA_SIZE_52 ,4 );/* reserved */ build_append_int_noprefix (table_data,0 ,8 );
hw/i386/amd_iommu.c +1 -1
@@ -2450,7 +2450,7 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp) 0xff000000);pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC ,0 );pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC , -AMDVI_MAX_PH_ADDR |AMDVI_MAX_GVA_ADDR |AMDVI_MAX_VA_ADDR ); +AMDVI_PA_SIZE_52 |AMDVI_GVA_SIZE_48 |AMDVI_VA_SIZE_64 ); }static void amdvi_sysbus_reset (DeviceState *dev)
hw/i386/amd_iommu.h +3 -3
@@ -245,9 +245,9 @@#define AMDVI_PAGE_SHIFT_4K 12#define AMDVI_PAGE_MASK_4K GENMASK64(63, 12) -#define AMDVI_MAX_GVA_ADDR (2UL << 5) -#define AMDVI_MAX_PH_ADDR (40UL << 8) -#define AMDVI_MAX_VA_ADDR (48UL << 15) +#define AMDVI_GVA_SIZE_48 (2UL << 5) +#define AMDVI_PA_SIZE_52 (52UL << 8) +#define AMDVI_VA_SIZE_64 (64UL << 15)/* Completion Wait data size */ #define AMDVI_COMPLETION_DATA_SIZE 8
Signature · 1 line
2.34.1
Re: [PATCH 2/8] amd_iommu: update PA, GVA and VA size macros
Quoted 4 lines · Sairaj Kodilkar
Current implemenation supports upto 64 bit IOVA and upto 52 bit PA. Modify related macros so that guest can see right size.
Signed-off-by Sairaj Kodilkar <[email protected]>
-Vasant
Re: [PATCH 2/8] amd_iommu: update PA, GVA and VA size macros
In reply to Sairaj KodilkarQuoted 1 line · Sairaj Kodilkar
Current implemenation
s/implemenation/implementation
supports upto 64 bit IOVA and upto 52 bit PA.
s/upto/up to
Quoted 40 lines
Modify related macros so that guest can see right size.
Signed-off-by Sairaj Kodilkar <[email protected]>--- hw/i386/acpi-build.c | 2 +- hw/i386/amd_iommu.c | 2 +- hw/i386/amd_iommu.h | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)hw/i386/acpi-build.c +1
-1@@ -1763,7 +1763,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,*/ build_append_int_noprefix (table_data, (1UL <<0 ) |/* EFRSup */ - (40UL <<8 ),/* PASize */ +AMDVI_PA_SIZE_52 ,4 );/* reserved */ build_append_int_noprefix (table_data,0 ,8 );hw/i386/amd_iommu.c +1
-1@@ -2450,7 +2450,7 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp) 0xff000000);pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC ,0 );pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC , -AMDVI_MAX_PH_ADDR |AMDVI_MAX_GVA_ADDR |AMDVI_MAX_VA_ADDR ); +AMDVI_PA_SIZE_52 |AMDVI_GVA_SIZE_48 |AMDVI_VA_SIZE_64 ); }static void amdvi_sysbus_reset (DeviceState *dev)hw/i386/amd_iommu.h +0
-0
hw/i386/amd_iommu.h line 250
Quoted 6 lines
hw/i386/amd_iommu.h +0
-3@@ -245,9 +245,9 @@#define AMDVI_PAGE_SHIFT_4K 12#define AMDVI_PAGE_MASK_4K GENMASK64(63, 12) -#define AMDVI_MAX_GVA_ADDR (2UL << 5) -#define AMDVI_MAX_PH_ADDR (40UL << 8) -#define AMDVI_MAX_VA_ADDR (48UL << 15)
Could you add a comment explaining that these constants encode fields from the AMD IOMMU “Miscellaneous Information Register 0” at IOMMU Capability Offset 10h. I prefer listing the field names and bit ranges exactly how they appear in the spec, which helps when searching through the doc, and also help explain the shifts. i.e. something like:
+/*
+ * IOMMU Capability Offset 10h: Miscellaneous Information Register 0.
+ * VAsize[21:15], PAsize[14:8], and GVAsize[7:5].
+ * These fields use the same layout in the IVRS IVinfo Field.
+ */
hw/i386/amd_iommu.h line 253
Quoted 6 lines
hw/i386/amd_iommu.h +3
-0+#define AMDVI_GVA_SIZE_48 (2UL << 5) +#define AMDVI_PA_SIZE_52 (52UL << 8) +#define AMDVI_VA_SIZE_64 (64UL << 15)/* Completion Wait data size */ #define AMDVI_COMPLETION_DATA_SIZE 8
With the above changes:
Re: [PATCH 2/8] amd_iommu: update PA, GVA and VA size macros
In reply to Sairaj KodilkarQuoted 15 lines · Sairaj Kodilkar
Current implemenation supports upto 64 bit IOVA and upto 52 bit PA. Modify related macros so that guest can see right size.
Signed-off-by Sairaj Kodilkar <[email protected]>--- hw/i386/acpi-build.c | 2 +- hw/i386/amd_iommu.c | 2 +- hw/i386/amd_iommu.h | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)hw/i386/acpi-build.c +0
-0
hw/i386/acpi-build.c build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, · line 1768
Quoted 7 lines
hw/i386/acpi-build.c +1
-1@@ -1763,7 +1763,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,*/ build_append_int_noprefix (table_data, (1UL <<0 ) |/* EFRSup */ - (40UL <<8 ),/* PASize */ +AMDVI_PA_SIZE_52 ,4 );/* reserved */
If a macro repeats I'm fine with a define but you dropped spec matching comment, not good.
#define AMDVI_PA_SIZE_52 (52UL << 8) /* PASize */
Will help reader find it in the spec.
In fact:
/* PASize: 011_0100b = 52 bits */
would be even better.
Similarly, elsewhere.
Quoted 33 lines
hw/i386/acpi-build.c +0
-0build_append_int_noprefix (table_data,0 ,8 );hw/i386/amd_iommu.c +1
-1@@ -2450,7 +2450,7 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp) 0xff000000);pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC ,0 );pci_set_long (pdev->config + s->capab_offset +AMDVI_CAPAB_MISC , -AMDVI_MAX_PH_ADDR |AMDVI_MAX_GVA_ADDR |AMDVI_MAX_VA_ADDR ); +AMDVI_PA_SIZE_52 |AMDVI_GVA_SIZE_48 |AMDVI_VA_SIZE_64 ); }static void amdvi_sysbus_reset (DeviceState *dev)hw/i386/amd_iommu.h +3
-3@@ -245,9 +245,9 @@#define AMDVI_PAGE_SHIFT_4K 12#define AMDVI_PAGE_MASK_4K GENMASK64(63, 12) -#define AMDVI_MAX_GVA_ADDR (2UL << 5) -#define AMDVI_MAX_PH_ADDR (40UL << 8) -#define AMDVI_MAX_VA_ADDR (48UL << 15) +#define AMDVI_GVA_SIZE_48 (2UL << 5) +#define AMDVI_PA_SIZE_52 (52UL << 8) +#define AMDVI_VA_SIZE_64 (64UL << 15)/* Completion Wait data size */ #define AMDVI_COMPLETION_DATA_SIZE 8Signature · 1 line
2.34.1
PATCH 3/8 amd_iommu: Return empty efr for stub call
In reply to Sairaj KodilkarThe stub call should not return normal EFR as it is used as place holder when actual AMD IOMMU calls are not available. Hence return 0.
--- hw/i386/amd_iommu-stub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/amd_iommu-stub.c +1 -1
@@ -22,5 +22,5 @@uint64_t amdvi_extended_feature_register (AMDVIState *s) { -return AMDVI_DEFAULT_EXT_FEATURES ; +return 0 ; }
Signature · 1 line
2.34.1
Re: [PATCH 3/8] amd_iommu: Return empty efr for stub call
Quoted 6 lines · Sairaj Kodilkar
The stub call should not return normal EFR as it is used as place holder when actual AMD IOMMU calls are not available. Hence return 0.
Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>
Quoted 15 lines
--- hw/i386/amd_iommu-stub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i386/amd_iommu-stub.c +1
-1@@ -22,5 +22,5 @@uint64_t amdvi_extended_feature_register (AMDVIState *s) { -return AMDVI_DEFAULT_EXT_FEATURES ; +return 0 ; }
PATCH 4/8 acpi_build: Use IOMMU pci device to build IOMMU device ID
In reply to Sairaj KodilkarCurrently, build_amd_iommu() uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
hw/i386/acpi-build.c +6 -6
@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all
@@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */
@@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */
Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 5 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.
Minor nit: I'd like to use "DeviceID" when possible, again with the goal of being able to quickly find matches to the relevant sections in the documentation. So making minor changes to the subject and commit message, I propose:
acpi_build: Use IOMMU pci device to build IOMMU DeviceID
Currently, build_amd_iommu() uses the "addr" property to build a DeviceID for an IOMMU device and advertise it through IVRS. But this property does not encode the IOMMU bus number, only the device and function numbers. This creates an incorrect DeviceID when the IOMMU is attached to a bus other than the default one with bus number 0.
Build the DeviceID using the complete BDF from the IOMMU PCI device.
Quoted 12 lines
Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +0
-0@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id)
hw/i386/acpi-build.c build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, · line 1762
Quoted 10 lines
hw/i386/acpi-build.c +3
-0{AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);
Instead of the three additions above, the last line can be only:
+ uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
taking the example from the virtio_iommu call to build_viot() in acpi_build().
Otherwise:
Quoted 26 lines
hw/i386/acpi-build.c +3
-6acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
In reply to Sairaj KodilkarQuoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>
But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
Quoted 48 lines
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +6
-6@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
Hi Michael
Yes, the ACPI function is called two times -- during qemu initialization and firmware writes. During first call, bus numbers are 0 and during second call, IVRS is created with bus number. This second IVRS overwrites the first one.
Thanks
Sairaj
Quoted 49 lines
Quoted 48 lines
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +6
-6@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise it
Quoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.
The spec doesn't forbid the above scenario, but "strongly recommends":
• An IOMMU should be a root-complex device (i.e., appear directly on the bus at the top of the PCI tree hierarchy).
• Some system software may prohibit an IOMMU from appearing under a PCI-to-PCI bridge.
(from Section 4.5 Software and Platform Firmware Implementation Issues)
While I don't know if that is the case in all HW implementations, my Genoa/Zen4 system does follow the recommended topology i.e. it exposes all of its 8 IOMMU functions directly on a PCI root bus, with no bridges.
So I think a reasonable choice is to enforce that an AMDVI-PCI device must always be directly attached to a PCI root bus. There is precedent for this in the virtio-iommu implementation already, the code change would be basically the same in amdvi_pci_realize(), see:
e72cfabf4ef2 ("hw/virtio/virtio-iommu-pci: Enforce the device is plugged on the root bus")
MST: does this address your concern?
Sairaj: Am I missing anything, perhaps from your unpublished patches, that makes this approach non-viable?
Thank you,
Alejandro
Quoted 61 lines
Hi Michael
Yes, the ACPI function is called two times -- during qemu initialization and firmware writes. During first call, bus numbers are 0 and during second call, IVRS is created with bus number. This second IVRS overwrites the first one.
Thanks
SairajQuoted 49 lines
Quoted 48 lines
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +6
-6@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 46 lines · Alejandro Jimenez
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise itQuoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.The spec doesn't forbid the above scenario, but "strongly recommends":
• An IOMMU should be a root-complex device (i.e., appear directly on the bus at the top of the PCI tree hierarchy).
• Some system software may prohibit an IOMMU from appearing under a PCI-to-PCI bridge.(from Section 4.5 Software and Platform Firmware Implementation Issues)
While I don't know if that is the case in all HW implementations, my Genoa/Zen4 system does follow the recommended topology i.e. it exposes all of its 8 IOMMU functions directly on a PCI root bus, with no bridges.
So I think a reasonable choice is to enforce that an AMDVI-PCI device must always be directly attached to a PCI root bus. There is precedent for this in the virtio-iommu implementation already, the code change would be basically the same in
amdvi_pci_realize(), see:
Quoted 4 lines
e72cfabf4ef2("hw/virtio/virtio-iommu-pci: Enforce the device is plugged on the root bus")MST: does this address your concern?
Indeed, it is.
Quoted 67 lines
Sairaj: Am I missing anything, perhaps from your unpublished patches, that makes this approach non-viable?
Thank you,
AlejandroQuoted 61 lines
Hi Michael
Yes, the ACPI function is called two times -- during qemu initialization and firmware writes. During first call, bus numbers are 0 and during second call, IVRS is created with bus number. This second IVRS overwrites the first one.
Thanks
SairajQuoted 49 lines
Quoted 48 lines
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +6
-6@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
In reply to Alejandro JimenezQuoted 46 lines · Alejandro Jimenez
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise itQuoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.The spec doesn't forbid the above scenario, but "strongly recommends":
• An IOMMU should be a root-complex device (i.e., appear directly on the bus at the top of the PCI tree hierarchy).
• Some system software may prohibit an IOMMU from appearing under a PCI-to-PCI bridge.(from Section 4.5 Software and Platform Firmware Implementation Issues)
While I don't know if that is the case in all HW implementations, my Genoa/Zen4 system does follow the recommended topology i.e. it exposes all of its 8 IOMMU functions directly on a PCI root bus, with no bridges.
So I think a reasonable choice is to enforce that an AMDVI-PCI device must always be directly attached to a PCI root bus. There is precedent for this in the virtio-iommu implementation already, the code change would be basically the same in
amdvi_pci_realize(), see:
Quoted 7 lines
e72cfabf4ef2("hw/virtio/virtio-iommu-pci: Enforce the device is plugged on the root bus")MST: does this address your concern?
Sairaj: Am I missing anything, perhaps from your unpublished patches, that makes this approach non-viable?
Hi Alejandro,
The Aim of this cleanup is to allow devices and IOMMU isolation under PCI-PXB and PCI-ROOT-PORT in future patches. Please see patches at [1] for more information.
Examples
--------
Example1, For below PCI configuration AMD IOMMU 1 will serve all the devices under pxb-pci1 and AMD IOMMU 2 will serve all the devices under pxb-pci2.
pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
| +- pcie-root-port1.2 --- Device1
| \- pcie-root-port1.3 --- Device2
\- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
+- pcie-root-port2.2 --- Device3
\- pcie-root-port2.3 --- Device4Example2, For below PCI configuration, AMD IOMMU 3 will only serve Device5 and Device6 and devices under pxb-pci buses will be served by IOMMUs of respective buses.
pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
| +- pcie-root-port1.2 --- Device1
| \- pcie-root-port1.3 --- Device2
+- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
| +- pcie-root-port2.2 --- Device3
| \- pcie-root-port2.3 --- Device4
+--- AMD IOMMU 3
+--- Device5
\--- Device6Example3, The below PCI configuration is invalid since both IOMMUs share same root bus i.e. pxb-pci1.
pcie.0 -+- pxb-pci1 -+- pcie-root-port1 --- AMD IOMMU 1
+- pcie-root-port2 --- AMD IOMMU 2
+- pcie-root-port3 --- Device2
\- pcie-root-port4 --- Device3----
Hardware vIOMMU requires this isolation since it does not support emulated devices (due to accelerated MMIO region). Enforcing IOMMU on pcie.0 will not work for vIOMMU.
Furthurmore, we need multiple hw-vIOMMUs when vfio devices do not share host IOMMUs (one hw-vIOMMU device for each host IOMMU)
Considering above facts, we cannot enforce pcie.0 for the IOMMU, but future patches ensure that IOMMU is attached to a PCI root port.
Also, Outline for vIOMMU developement will be as follow
1. IVRS cleanup
2. IOMMU PCI bus isolation support
3. Multiple vIOMMU support
4. New vIOMMU model (-device amd-viommu)
@Alejandro, does this answer your question ? or let me know if you need an overview of upcoming changes to evaluate this
[1] https://github.com/AMDESE/qemu-iommu/tree/wip/amd-viommu_20260728
Thanks
Sairaj
Quoted 66 lines
Thank you,
AlejandroQuoted 61 lines
Hi Michael
Yes, the ACPI function is called two times -- during qemu initialization and firmware writes. During first call, bus numbers are 0 and during second call, IVRS is created with bus number. This second IVRS overwrites the first one.
Thanks
SairajQuoted 49 lines
Quoted 48 lines
--- hw/i386/acpi-build.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +6
-6@@ -1752,10 +1752,13 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, const char *oem_table_id) {AMDVIState *s =AMD_IOMMU_DEVICE (x86_iommu_get_default ()); +PCIDevice *iommu_dev = &(s->pci->dev);GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id };uint64_t feature_report; +int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev)); +uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn);acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all @@ -1816,9 +1819,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 );/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ @@ -1850,10 +1851,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IVHD length */ build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); +/* DeviceID */ -build_append_int_noprefix (table_data, -object_property_get_int (OBJECT (s->pci),"addr" , - &error_abort),2 ); +build_append_int_noprefix (table_data, iommu_devid,2 );/* Capability offset */ build_append_int_noprefix (table_data, s->pci->capab_offset,2 );/* IOMMU base address */ Signature · 1 line
2.34.1
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
In reply to Alejandro JimenezQuoted 21 lines · Alejandro Jimenez
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise itQuoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
Quoted 7 lines
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.
Hi alejandro,
Could you please share the steps and commands you used to change the bus number inside guest ? I would like to run few tests around it but I am not able to change the PCI ID of the IOMMU with setpci command on root pci bridge.
Thanks
Sairaj
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 35 lines · Sairaj Kodilkar
Quoted 1 line · Michael S. Tsirkin
On Mon, May 11, 2026 at >> 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >>
Quoted 21 lines · Alejandro Jimenez
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise itQuoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
Quoted 7 lines
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.
Quoted 4 lines
Hi alejandro,
Could you please share the steps and commands you used to change the bus number inside guest ?
Sorry, my earlier statement was ambiguous/confusing because I used DeviceID referring to the name of the IVRS field, but can also be taken as a portion of the BDF. Let me try to be more clear:
I did not change the IOMMU device or function values (I don't think that is currently possible). What I was able to change is the secondary bus number of the pcie-root-port above the IOMMU. Then this changes the IOMMU BDF (more specifically the Bus part of it).
I launched the guest with this topology:
-device pcie-root-port,id=pcie-rp1,bus=pcie.0,addr=1c.0 -device AMDVI-PCI,id=iommu-pci,bus=pcie-rp1,addr=0 -device amd-iommu,dma-remap=on,xtsup=on,pci-id=iommu-pci
so firmware placed the IOMMU at 01:00.0, behind root port 00:1c.0. Inside the guest:
# setpci -s 00:1c.0 PRIMARY_BUS SECONDARY_BUS SUBORDINATE_BUS 00 01 01
# setpci -s 00:1c.0 SECONDARY_BUS=02 SUBORDINATE_BUS=02
so the write is targeting the root port (pcie-rp1), not the IOMMU endpoint itself. After that the IOMMU BDF in the guest is 02:00.0. IIUC the standard lspci output displays the old BDF that has been cached by the guest OS when enumerating the PCI devices, but setpci and lspci with option -H1 will do a raw access and show the new BDF.
# lspci -H1 -Dnn [...] 0000:02:00.0 IOMMU ... [1022:1419]
The IVRS table is not rebuilt following this guest write, which is I think what MST is warning about.
Now we need to decide whether that operation I described above (which I found researching ways to force the change to prove Michael's point) is something that is considered valid. Perhaps there is a use for it that I am not aware of, or perhaps it is just another way that a guest can break itself and we are just supposed to let it.
I do see your explanation about why my initial proposal is too restrictive for the future. I am just not sure yet what the right solution is.
Thank you,
Alejandro
I would like to run few tests around it but I am
Quoted 6 lines
not able to change the PCI ID of the IOMMU with setpci command on root pci bridge.
Thanks
Sairaj
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 77 lines · Alejandro Jimenez
Quoted 35 lines · Sairaj Kodilkar
Quoted 1 line · Michael S. Tsirkin
On Mon, May 11, 2026 at >> 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >>
Quoted 21 lines · Alejandro Jimenez
Quoted 6 lines · Sairaj Kodilkar
On 7/31/2026 3: 20 PM, Michael S. Tsirkin wrote: > On Mon, May 11, 2026 at 06: 09: 33PM +0530, Sairaj Kodilkar wrote: >> Currently,
build_amd_iommu()uses "addr" property to build the device ID for >> IOMMU device and advertise itQuoted 12 lines
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Sairaj Kodilkar
Currently,
build_amd_iommu()uses "addr" property to build the device ID for IOMMU device and advertise it throught IVRS. But this property does not encode IOMMU bus. This will be a problem if IOMMU is attached to different bus. Hence use iommu pci device which provides bus, to build the IOMMU device ID.Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>But is this called after firmware has enumerated the pci bus? And I guess OS better not change that bus number eh?
Quoted 7 lines
The above line sounds fairly threatening :) so I dug a bit more into the details.
Short story: I tested placing the IOMMU behind a PCI bridge, which the implementation currently allows, and the guest can (easily) change the bus number. That changes the IOMMU BDF, so the DeviceID encoded in IVRS becomes incorrect.Quoted 4 lines
Hi alejandro,
Could you please share the steps and commands you used to change the bus number inside guest ?
Sorry, my earlier statement was ambiguous/confusing because I used DeviceID referring to the name of the IVRS field, but can also be taken as a portion of the BDF. Let me try to be more clear:
I did not change the IOMMU device or function values (I don't think that is currently possible). What I was able to change is the secondary bus number of the pcie-root-port above the IOMMU. Then this changes the IOMMU BDF (more specifically the Bus part of it).
I launched the guest with this topology:-device pcie-root-port,id=pcie-rp1,bus=pcie.0,addr=1c.0 -device AMDVI-PCI,id=iommu-pci,bus=pcie-rp1,addr=0 -device amd-iommu,dma-remap=on,xtsup=on,pci-id=iommu-pci
so firmware placed the IOMMU at 01:00.0, behind root port 00:1c.0. Inside the guest:
# setpci -s 00:1c.0 PRIMARY_BUS SECONDARY_BUS SUBORDINATE_BUS 00 01 01# setpci -s 00:1c.0 SECONDARY_BUS=02 SUBORDINATE_BUS=02so the write is targeting the root port (pcie-rp1), not the IOMMU endpoint itself. After that the IOMMU BDF in the guest is 02:00.0. IIUC the standard lspci output displays the old BDF that has been cached by the guest OS when enumerating the PCI devices, but setpci and lspci with option -H1 will do a raw access and show the new BDF.
# lspci -H1 -Dnn [...] 0000:02:00.0 IOMMU ... [1022:1419]
Quoted 9 lines
The IVRS table is not rebuilt following this guest write, which is I think what MST is warning about.
Now we need to decide whether that operation I described above (which I found researching ways to force the change to prove Michael's point) is something that is considered valid. Perhaps there is a use for it that I am not aware of, or perhaps it is just another way that a guest can break itself and we are just supposed to let it.
Hi Alejandro,
I see that you are using lspci -H1 to read the actual hardware config instead of kernel topology, The plain lspci does not show new bus, It seems like you have to perform pci rescan.
When I perform PCI rescan I see following NULL pointer access in kernel
oot@ubuntu:~# echo 1 | sudo tee /sys/bus/pci/devices/0000:00:1c.0/rescan 1 [ 238.666837] pci 0000:02:00.0: EDR: Failed to install notify handler [ 238.675198] BUG: unable to handle page fault for address: 0000000000080060 [ 238.677682] #PF: supervisor read access in kernel mode [ 238.679330] #PF: error_code(0x0000) - not-present page [ 238.680976] PGD 0 P4D 0 [ 238.681846] Oops: Oops: 0000 [#1] SMP NOPTI [ 238.683206] CPU: 0 UID: 0 PID: 1436 Comm: tee Tainted: G W 7.1.0-rc1-qemu #2 PREEMPT(lazy) [ 238.686328] Tainted: [W]=WARN [ 238.687326] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 [ 238.690864] RIP: 0010:amd_iommu_probe_device+0x53/0x470 [ 238.692598] Code: b0 0f 84 6d 01 00 00 31 f6 e8 e9 d2 ff ff 85 c0 0f 88 45 01 00 00 4c 89 e7 0f b7 d8 e8 96 d6 ff ff 48 85 c0 0f 84 31 01 00 00 <48> 8b 40 60 0f b7 40 1a 39 c3 0f 8f 21 01 00 00 4c 89 e7 e8 6 [ 238.697728] RSP: 0018:ffffd317452ab948 EFLAGS: 00010206 [ 238.698706] RAX: 0000000000080000 RBX: 0000000000000200 RCX: 0000000000000000 [ 238.700044] RDX: ffff8e374195f000 RSI: 0000000000000200 RDI: ffff8e376ff580d0 [ 238.701369] RBP: ffffd317452ab970 R08: 0000000000080000 R09: ffff8e375156e380 [ 238.702693] R10: ffff8e37707264b8 R11: ffffffffb0f28a20 R12: ffff8e376ff580d0 [ 238.704033] R13: ffff8e376ff580d0 R14: 00000000ffffffff R15: ffff8e3740d0f5d8 [ 238.705358] FS: 00007f2d02060540(0000) GS:ffff8e37ff749000(0000) knlGS:0000000000000000 [ 238.706843] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 238.707918] CR2: 0000000000080060 CR3: 0000000101d00004 CR4: 0000000000770ef0 [ 238.709285] PKRU: 55555554 [ 238.709828] Call Trace: [ 238.710314] <TASK> [ 238.710742] iommu_init_device+0xbf/0x330 [ 238.711518] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.712450] iommu_probe_device+0x70/0xb0 [ 238.713214] iommu_bus_notifier+0x2c/0x80 [ 238.713984] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.714889] notifier_call_chain+0x66/0xf0 [ 238.715673] blocking_notifier_call_chain+0x47/0x70 [ 238.716632] bus_notify+0x3b/0x50 [ 238.717295] device_add+0x631/0x870 [ 238.717982] pci_device_add+0x303/0x720 [ 238.718735] pci_scan_single_device+0xad/0xd0 [ 238.719571] pci_scan_slot+0x97/0x230 [ 238.720279] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.721186] pci_scan_child_bus_extend+0x3b/0x2f0 [ 238.722082] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.722987] pci_scan_bridge_extend+0x447/0x4f0 [ 238.723847] pci_scan_child_bus_extend+0x106/0x2f0 [ 238.724795] pci_rescan_bus+0x1b/0x40 [ 238.725499] dev_rescan_store+0x70/0x90 [ 238.726252] dev_attr_store+0x1b/0x30 [ 238.726963] sysfs_kf_write+0x76/0x90 [ 238.727677] kernfs_fop_write_iter+0x145/0x1d0 [ 238.728530] vfs_write+0x307/0x420 [ 238.729401] ksys_write+0x6d/0xe0 [ 238.730223] __x64_sys_write+0x1d/0x30 [ 238.731116] x64_sys_call+0x1704/0x21c0 [ 238.732018] do_syscall_64+0xa3/0x7c0 [ 238.732907] ? irqentry_exit+0x42/0x5a0 [ 238.733815] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.734877] ? do_syscall_64+0x58/0x7c0 [ 238.735768] ? exc_page_fault+0x98/0x1c0 [ 238.736709] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 238.737825] RIP: 0033:0x7f2d01b10104 [ 238.738673] Code: 89 02 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 8d 05 e1 08 2e 00 8b 00 85 c0 75 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 f3 c3 66 90 41 54 55 49 89 d4 53 48 5 [ 238.742405] RSP: 002b:00007ffd09b75168 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 [ 238.743966] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007f2d01b10104 [ 238.745478] RDX: 0000000000000002 RSI: 00007ffd09b75250 RDI: 0000000000000003 [ 238.746962] RBP: 00007ffd09b75250 R08: 0000000000000002 R09: 00007f2d02060540 [ 238.748466] R10: 00000000000001b6 R11: 0000000000000246 R12: 0000555ec468b280 [ 238.749943] R13: 0000000000000002 R14: 00007f2d01de7760 R15: 0000000000000002 [ 238.751418] </TASK> [ 238.752010] Modules linked in: [ 238.752772] CR2: 0000000000080060 [ 238.753571] ---[ end trace 0000000000000000 ]--- [ 238.754628] RIP: 0010:amd_iommu_probe_device+0x53/0x470 [ 238.755772] Code: b0 0f 84 6d 01 00 00 31 f6 e8 e9 d2 ff ff 85 c0 0f 88 45 01 00 00 4c 89 e7 0f b7 d8 e8 96 d6 ff ff 48 85 c0 0f 84 31 01 00 00 <48> 8b 40 60 0f b7 40 1a 39 c3 0f 8f 21 01 00 00 4c 89 e7 e8 6 [ 238.759542] RSP: 0018:ffffd317452ab948 EFLAGS: 00010206 [ 238.760706] RAX: 0000000000080000 RBX: 0000000000000200 RCX: 0000000000000000 [ 238.762210] RDX: ffff8e374195f000 RSI: 0000000000000200 RDI: ffff8e376ff580d0 [ 238.763708] RBP: ffffd317452ab970 R08: 0000000000080000 R09: ffff8e375156e380 [ 238.765248] R10: ffff8e37707264b8 R11: ffffffffb0f28a20 R12: ffff8e376ff580d0 [ 238.766761] R13: ffff8e376ff580d0 R14: 00000000ffffffff R15: ffff8e3740d0f5d8 [ 238.768279] FS: 00007f2d02060540(0000) GS:ffff8e37ff749000(0000) knlGS:0000000000000000 [ 238.769959] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 238.771227] CR2: 0000000000080060 CR3: 0000000101d00004 CR4: 0000000000770ef0 [ 238.772767] PKRU: 55555554 [ 238.773488] note: tee[1436] exited with irqs disabled
It seems like this operations is not safe and we may need to patch kernel.
Can you try running pci bus rescan after changing the secondary bus ?
Thanks
Sairaj
Quoted 14 lines
I do see your explanation about why my initial proposal is too restrictive for the future. I am just not sure yet what the right solution is.
Thank you,
AlejandroI would like to run few tests around it but I am
Quoted 6 lines
not able to change the PCI ID of the IOMMU with setpci command on root pci bridge.
Thanks
Sairaj
Re: [PATCH 4/8] acpi_build: Use IOMMU pci device to build IOMMU device ID
Quoted 72 lines · Sairaj Kodilkar
[ 238.666837] pci 0000:02:00.0: EDR: Failed to install notify handler [ 238.675198] BUG: unable to handle page fault for address: 0000000000080060 [ 238.677682] #PF: supervisor read access in kernel mode [ 238.679330] #PF: error_code(0x0000) - not-present page [ 238.680976] PGD 0 P4D 0 [ 238.681846] Oops: Oops: 0000 [#1] SMP NOPTI [ 238.683206] CPU: 0 UID: 0 PID: 1436 Comm: tee Tainted: G W 7.1.0-rc1-qemu #2 PREEMPT(lazy) [ 238.686328] Tainted: [W]=WARN [ 238.687326] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 [ 238.690864] RIP: 0010:amd_iommu_probe_device+0x53/0x470 [ 238.692598] Code: b0 0f 84 6d 01 00 00 31 f6 e8 e9 d2 ff ff 85 c0 0f 88 45 01 00 00 4c 89 e7 0f b7 d8 e8 96 d6 ff ff 48 85 c0 0f 84 31 01 00 00 <48> 8b 40 60 0f b7 40 1a 39 c3 0f 8f 21 01 00 00 4c 89 e7 e8 6 [ 238.697728] RSP: 0018:ffffd317452ab948 EFLAGS: 00010206 [ 238.698706] RAX: 0000000000080000 RBX: 0000000000000200 RCX: 0000000000000000 [ 238.700044] RDX: ffff8e374195f000 RSI: 0000000000000200 RDI: ffff8e376ff580d0 [ 238.701369] RBP: ffffd317452ab970 R08: 0000000000080000 R09: ffff8e375156e380 [ 238.702693] R10: ffff8e37707264b8 R11: ffffffffb0f28a20 R12: ffff8e376ff580d0 [ 238.704033] R13: ffff8e376ff580d0 R14: 00000000ffffffff R15: ffff8e3740d0f5d8 [ 238.705358] FS: 00007f2d02060540(0000) GS:ffff8e37ff749000(0000) knlGS:0000000000000000 [ 238.706843] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 238.707918] CR2: 0000000000080060 CR3: 0000000101d00004 CR4: 0000000000770ef0 [ 238.709285] PKRU: 55555554 [ 238.709828] Call Trace: [ 238.710314] <TASK> [ 238.710742] iommu_init_device+0xbf/0x330 [ 238.711518] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.712450] iommu_probe_device+0x70/0xb0 [ 238.713214] iommu_bus_notifier+0x2c/0x80 [ 238.713984] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.714889] notifier_call_chain+0x66/0xf0 [ 238.715673] blocking_notifier_call_chain+0x47/0x70 [ 238.716632] bus_notify+0x3b/0x50 [ 238.717295] device_add+0x631/0x870 [ 238.717982] pci_device_add+0x303/0x720 [ 238.718735] pci_scan_single_device+0xad/0xd0 [ 238.719571] pci_scan_slot+0x97/0x230 [ 238.720279] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.721186] pci_scan_child_bus_extend+0x3b/0x2f0 [ 238.722082] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.722987] pci_scan_bridge_extend+0x447/0x4f0 [ 238.723847] pci_scan_child_bus_extend+0x106/0x2f0 [ 238.724795] pci_rescan_bus+0x1b/0x40 [ 238.725499] dev_rescan_store+0x70/0x90 [ 238.726252] dev_attr_store+0x1b/0x30 [ 238.726963] sysfs_kf_write+0x76/0x90 [ 238.727677] kernfs_fop_write_iter+0x145/0x1d0 [ 238.728530] vfs_write+0x307/0x420 [ 238.729401] ksys_write+0x6d/0xe0 [ 238.730223] __x64_sys_write+0x1d/0x30 [ 238.731116] x64_sys_call+0x1704/0x21c0 [ 238.732018] do_syscall_64+0xa3/0x7c0 [ 238.732907] ? irqentry_exit+0x42/0x5a0 [ 238.733815] ? srso_alias_return_thunk+0x5/0xfbef5 [ 238.734877] ? do_syscall_64+0x58/0x7c0 [ 238.735768] ? exc_page_fault+0x98/0x1c0 [ 238.736709] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 238.737825] RIP: 0033:0x7f2d01b10104 [ 238.738673] Code: 89 02 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 8d 05 e1 08 2e 00 8b 00 85 c0 75 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 f3 c3 66 90 41 54 55 49 89 d4 53 48 5 [ 238.742405] RSP: 002b:00007ffd09b75168 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 [ 238.743966] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007f2d01b10104 [ 238.745478] RDX: 0000000000000002 RSI: 00007ffd09b75250 RDI: 0000000000000003 [ 238.746962] RBP: 00007ffd09b75250 R08: 0000000000000002 R09: 00007f2d02060540 [ 238.748466] R10: 00000000000001b6 R11: 0000000000000246 R12: 0000555ec468b280 [ 238.749943] R13: 0000000000000002 R14: 00007f2d01de7760 R15: 0000000000000002 [ 238.751418] </TASK> [ 238.752010] Modules linked in: [ 238.752772] CR2: 0000000000080060 [ 238.753571] ---[ end trace 0000000000000000 ]--- [ 238.754628] RIP: 0010:amd_iommu_probe_device+0x53/0x470 [ 238.755772] Code: b0 0f 84 6d 01 00 00 31 f6 e8 e9 d2 ff ff 85 c0 0f 88 45 01 00 00 4c 89 e7 0f b7 d8 e8 96 d6 ff ff 48 85 c0 0f 84 31 01 00 00 <48> 8b 40 60 0f b7 40 1a 39 c3 0f 8f 21 01 00 00 4c 89 e7 e8 6 [ 238.759542] RSP: 0018:ffffd317452ab948 EFLAGS: 00010206 [ 238.760706] RAX: 0000000000080000 RBX: 0000000000000200 RCX: 0000000000000000 [ 238.762210] RDX: ffff8e374195f000 RSI: 0000000000000200 RDI: ffff8e376ff580d0
Quoted 8 lines
[ 238.763708] RBP: ffffd317452ab970 R08: 0000000000080000 R09: ffff8e375156e380 [ 238.765248] R10: ffff8e37707264b8 R11: ffffffffb0f28a20 R12: ffff8e376ff580d0 [ 238.766761] R13: ffff8e376ff580d0 R14: 00000000ffffffff R15: ffff8e3740d0f5d8 [ 238.768279] FS: 00007f2d02060540(0000) GS:ffff8e37ff749000(0000) knlGS:0000000000000000 [ 238.769959] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 238.771227] CR2: 0000000000080060 CR3: 0000000101d00004 CR4: 0000000000770ef0 [ 238.772767] PKRU: 55555554 [ 238.773488] note: tee[1436] exited with irqs disabled
Hi Alejandro,
Seems like upstream doesnt have this issue anymore This patch fixed it: 07d0f496fe7ec5abe3bee7e38be709521567bb33
Thanks
Sairaj
PATCH 5/8 acpi_build: Introduce necessary macros and structs for AMD IOMMU IVRS
In reply to Sairaj KodilkarIntroduce necessary macros, along with packed structs which represents the AMD IVRS data structure, to hold the necessary information. This will improve readability of the current code.
--- hw/i386/acpi-build.h | 92 ++++++++++++++++++++++++++++++++++++++++++++ hw/i386/amd_iommu.h | 6 +++ 2 files changed, 98 insertions(+)
hw/i386/acpi-build.h +92 -0
@@ -2,10 +2,102 @@#ifndef HW_I386_ACPI_BUILD_H #define HW_I386_ACPI_BUILD_H #include "hw/acpi/acpi-defs.h" +#include "qemu/bitops.h" extern const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio;void acpi_setup (void );Object *acpi_get_i386_pci_host (void ); +#define AMD_IVINFO_EFR_SUP BIT(0) + +#define AMD_IVHD_FLAG_HT_TUN_EN BIT(0) +#define AMD_IVHD_FLAG_IOTLB_SUP BIT(4) +#define AMD_IVHD_FLAG_PREF_SUP BIT(6) +#define AMD_IVHD_FLAG_PPR_SUP BIT(7) + +#define AMD_IVHD_FEATURE_REPORT_XT_SUP_SHIFT (0) +#define AMD_IVHD_FEATURE_REPORT_GT_SUP_SHIFT (2) +#define AMD_IVHD_FEATURE_REPORT_GLX_SUP_SHIFT (3) +#define AMD_IVHD_FEATURE_REPORT_GA_SUP_SHIFT (6) +#define AMD_IVHD_FEATURE_REPORT_GATS_SHIFT (28) +#define AMD_IVHD_FEATURE_REPORT_HATS_SHIFT (30) + +#define AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT (0) + +#define AMD_IVHD_DEVICE_ENTRY_TYPE_RESERVED (0) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALL (1) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT (2) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE (3) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE (4) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE (0x43) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE (0x48) + +#define IVHD_VARIETY_IOAPIC (1) +#define IVHD_VARIETY_HPET (2) + +/* +* Vendor(AMD) specific fields in the IVRS header +* Excludes fields in ACPI table header +*/ +typedef +struct AmdIvrsVendorHdr { +uint32_t ivinfo; +uint64_t reserved; +} __attribute__((packed))AmdIvrsVendorHdr ; + +/* IVHD type 10h */ +typedef +struct AmdIvhdHdr10 { +uint8_t type; +uint8_t flags; +uint16_t length; +uint16_t devid; +uint16_t capab_offset; +uint64_t base_addr; +uint16_t pci_seg; +uint16_t iommu_info; +uint32_t iommu_feature_report; +} __attribute__((packed))AmdIvhdHdr10 ; + +/* IVHD type 11h */ +typedef +struct AmdIvhdHdr11 { +uint8_t type; +uint8_t flags; +uint16_t length; +uint16_t devid; +uint16_t capab_offset; +uint64_t base_addr; +uint16_t pci_seg; +uint16_t iommu_info; +uint32_t iommu_attributes; +uint64_t efr; +uint64_t efr2; +} __attribute__((packed))AmdIvhdHdr11 ; + +typedef +struct AmdIvhdDeviceEntry { +uint8_t type; +uint16_t devid; +uint8_t dte_setting; +} __attribute__((packed))AmdIvhdDeviceEntry ; + +typedef +struct AmdIvhdDeviceEntryExt { +uint8_t type; +uint16_t devid_a; +uint8_t dte_setting; +union { +struct { +uint8_t handle; +uint16_t devid_b; +uint8_t variety; + } __attribute__((packed)); +struct { +uint32_t ext_dte_setting; + } __attribute__((packed)); + }; +} __attribute__((packed))AmdIvhdDeviceEntryExt ; +#endif
hw/i386/amd_iommu.h +6 -0
@@ -175,9 +175,15 @@#define AMDVI_DTE_QUAD3_RESERVED (GENMASK64(14, 0) | GENMASK64(53, 48))/* AMDVI paging mode */ +#define AMDVI_GATS_MODE_SHIFT (12) +#define AMDVI_GATS_MODE_MASK (3ULL << 12)#define AMDVI_GATS_MODE (2ULL << 12) +#define AMDVI_HATS_MODE_SHIFT (10) +#define AMDVI_HATS_MODE_MASK (3ULL << 10)#define AMDVI_HATS_MODE (2ULL << 10)#define AMDVI_HATS_MODE_RESERVED (3ULL << 10) +#define AMDVI_GLX_SUP_SHIFT (14) +#define AMDVI_GLX_SUP_MASK (3ULL << 14)/* Page Table format */
Signature · 1 line
2.34.1
Re: [PATCH 5/8] acpi_build: Introduce necessary macros and structs for AMD IOMMU IVRS
hw/i386/acpi-build.h
Quoted 15 lines · Sairaj Kodilkar
Introduce necessary macros, along with packed structs which represents the AMD IVRS data structure, to hold the necessary information. This will improve readability of the current code.
Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>--- hw/i386/acpi-build.h | 92 ++++++++++++++++++++++++++++++++++++++++++++ hw/i386/amd_iommu.h | 6 +++ 2 files changed, 98 insertions(+)hw/i386/acpi-build.h +0
-0
acpi-build.h currently doesn't have any device specific definitions, and it is included by several files that don't need AMD IOMMU information. I initially thought about creating a new header just for these new definitions, but I think the simplest approach is to put them in amd_iommu.h (which is already included in acpi-build.c).
Quoted 38 lines
hw/i386/acpi-build.h +28
-0@@ -2,10 +2,102 @@#ifndef HW_I386_ACPI_BUILD_H #define HW_I386_ACPI_BUILD_H #include "hw/acpi/acpi-defs.h" +#include "qemu/bitops.h" extern const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio;void acpi_setup (void );Object *acpi_get_i386_pci_host (void ); +#define AMD_IVINFO_EFR_SUP BIT(0) + +#define AMD_IVHD_FLAG_HT_TUN_EN BIT(0) +#define AMD_IVHD_FLAG_IOTLB_SUP BIT(4) +#define AMD_IVHD_FLAG_PREF_SUP BIT(6) +#define AMD_IVHD_FLAG_PPR_SUP BIT(7) + +#define AMD_IVHD_FEATURE_REPORT_XT_SUP_SHIFT (0) +#define AMD_IVHD_FEATURE_REPORT_GT_SUP_SHIFT (2) +#define AMD_IVHD_FEATURE_REPORT_GLX_SUP_SHIFT (3) +#define AMD_IVHD_FEATURE_REPORT_GA_SUP_SHIFT (6) +#define AMD_IVHD_FEATURE_REPORT_GATS_SHIFT (28) +#define AMD_IVHD_FEATURE_REPORT_HATS_SHIFT (30) + +#define AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT (0) + +#define AMD_IVHD_DEVICE_ENTRY_TYPE_RESERVED (0) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALL (1) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT (2) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE (3) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE (4) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE (0x43) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE (0x48) + +#define IVHD_VARIETY_IOAPIC (1) +#define IVHD_VARIETY_HPET (2) +
hw/i386/acpi-build.h line 48
Quoted 10 lines
hw/i386/acpi-build.h +10
-0+/* +* Vendor(AMD) specific fields in the IVRS header +* Excludes fields in ACPI table header +*/ +typedef +struct AmdIvrsVendorHdr { +uint32_t ivinfo; +uint64_t reserved; +} __attribute__((packed))AmdIvrsVendorHdr ; +
nit: I think the preferred way to specify the packed attribute is to use QEMU_PACKED like the FwCfgTPMConfig definition uses in acpi-build.c. I know it is ultimately the same, but I think we should use project-specific definitions when available/appropriate. Please change all the structs to use QEMU_PACKED.
Thank you,
Alejandro
Quoted 75 lines
hw/i386/acpi-build.h +54
-0+/* IVHD type 10h */ +typedef +struct AmdIvhdHdr10 { +uint8_t type; +uint8_t flags; +uint16_t length; +uint16_t devid; +uint16_t capab_offset; +uint64_t base_addr; +uint16_t pci_seg; +uint16_t iommu_info; +uint32_t iommu_feature_report; +} __attribute__((packed))AmdIvhdHdr10 ; + +/* IVHD type 11h */ +typedef +struct AmdIvhdHdr11 { +uint8_t type; +uint8_t flags; +uint16_t length; +uint16_t devid; +uint16_t capab_offset; +uint64_t base_addr; +uint16_t pci_seg; +uint16_t iommu_info; +uint32_t iommu_attributes; +uint64_t efr; +uint64_t efr2; +} __attribute__((packed))AmdIvhdHdr11 ; + +typedef +struct AmdIvhdDeviceEntry { +uint8_t type; +uint16_t devid; +uint8_t dte_setting; +} __attribute__((packed))AmdIvhdDeviceEntry ; + +typedef +struct AmdIvhdDeviceEntryExt { +uint8_t type; +uint16_t devid_a; +uint8_t dte_setting; +union { +struct { +uint8_t handle; +uint16_t devid_b; +uint8_t variety; + } __attribute__((packed)); +struct { +uint32_t ext_dte_setting; + } __attribute__((packed)); + }; +} __attribute__((packed))AmdIvhdDeviceEntryExt ; +#endif hw/i386/amd_iommu.h +6
-0@@ -175,9 +175,15 @@#define AMDVI_DTE_QUAD3_RESERVED (GENMASK64(14, 0) | GENMASK64(53, 48))/* AMDVI paging mode */ +#define AMDVI_GATS_MODE_SHIFT (12) +#define AMDVI_GATS_MODE_MASK (3ULL << 12)#define AMDVI_GATS_MODE (2ULL << 12) +#define AMDVI_HATS_MODE_SHIFT (10) +#define AMDVI_HATS_MODE_MASK (3ULL << 10)#define AMDVI_HATS_MODE (2ULL << 10)#define AMDVI_HATS_MODE_RESERVED (3ULL << 10) +#define AMDVI_GLX_SUP_SHIFT (14) +#define AMDVI_GLX_SUP_MASK (3ULL << 14)/* Page Table format */
Re: [PATCH 5/8] acpi_build: Introduce necessary macros and structs for AMD IOMMU IVRS
In reply to Sairaj KodilkarQuoted 108 lines · Sairaj Kodilkar
Introduce necessary macros, along with packed structs which represents the AMD IVRS data structure, to hold the necessary information. This will improve readability of the current code.
Signed-off-by Sairaj Kodilkar <[email protected]>Reviewed-by Vasant Hegde <[email protected]>--- hw/i386/acpi-build.h | 92 ++++++++++++++++++++++++++++++++++++++++++++ hw/i386/amd_iommu.h | 6 +++ 2 files changed, 98 insertions(+)hw/i386/acpi-build.h +83
-0@@ -2,10 +2,102 @@ #ifndef HW_I386_ACPI_BUILD_H #define HW_I386_ACPI_BUILD_H #include "hw/acpi/acpi-defs.h" +#include "qemu/bitops.h" extern const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio; void acpi_setup(void); Object *acpi_get_i386_pci_host(void); +#define AMD_IVINFO_EFR_SUP BIT(0) + +#define AMD_IVHD_FLAG_HT_TUN_EN BIT(0) +#define AMD_IVHD_FLAG_IOTLB_SUP BIT(4) +#define AMD_IVHD_FLAG_PREF_SUP BIT(6) +#define AMD_IVHD_FLAG_PPR_SUP BIT(7) + +#define AMD_IVHD_FEATURE_REPORT_XT_SUP_SHIFT (0) +#define AMD_IVHD_FEATURE_REPORT_GT_SUP_SHIFT (2) +#define AMD_IVHD_FEATURE_REPORT_GLX_SUP_SHIFT (3) +#define AMD_IVHD_FEATURE_REPORT_GA_SUP_SHIFT (6) +#define AMD_IVHD_FEATURE_REPORT_GATS_SHIFT (28) +#define AMD_IVHD_FEATURE_REPORT_HATS_SHIFT (30) + +#define AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT (0) + +#define AMD_IVHD_DEVICE_ENTRY_TYPE_RESERVED (0) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALL (1) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT (2) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE (3) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE (4) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE (0x43) +#define AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE (0x48) + +#define IVHD_VARIETY_IOAPIC (1) +#define IVHD_VARIETY_HPET (2) + +/* + * Vendor(AMD) specific fields in the IVRS header + * Excludes fields in ACPI table header + */ +typedef +struct AmdIvrsVendorHdr { + uint32_t ivinfo; + uint64_t reserved; +} __attribute__((packed)) AmdIvrsVendorHdr; + +/* IVHD type 10h */ +typedef +struct AmdIvhdHdr10 { + uint8_t type; + uint8_t flags; + uint16_t length; + uint16_t devid; + uint16_t capab_offset; + uint64_t base_addr; + uint16_t pci_seg; + uint16_t iommu_info; + uint32_t iommu_feature_report; +} __attribute__((packed)) AmdIvhdHdr10; + +/* IVHD type 11h */ +typedef +struct AmdIvhdHdr11 { + uint8_t type; + uint8_t flags; + uint16_t length; + uint16_t devid; + uint16_t capab_offset; + uint64_t base_addr; + uint16_t pci_seg; + uint16_t iommu_info; + uint32_t iommu_attributes; + uint64_t efr; + uint64_t efr2; +} __attribute__((packed)) AmdIvhdHdr11; + +typedef +struct AmdIvhdDeviceEntry { + uint8_t type; + uint16_t devid; + uint8_t dte_setting; +} __attribute__((packed)) AmdIvhdDeviceEntry; + +typedef +struct AmdIvhdDeviceEntryExt { + uint8_t type; + uint16_t devid_a; + uint8_t dte_setting; + union { + struct { + uint8_t handle;
hw/i386/acpi-build.h line 103
Quoted 10 lines
hw/i386/acpi-build.h +9
-0+uint16_t devid_b; +uint8_t variety; + } __attribute__((packed)); +struct { +uint32_t ext_dte_setting; + } __attribute__((packed)); + }; +} __attribute__((packed)) AmdIvhdDeviceEntryExt; +#endif
Pls do not do this is not how we build ACPI tables. It is completely impossible to match fields and structs to spec since it does not repeat it verbatim.
Instead of all this write functions matching spec exactly. Examples:
/* Table 104: IVHD Device Entry Type Codes (8-byte) */ build_append_int_noprefix(....) /* Handle */ build_append_int_noprefix(....) /* DevIDb */ build_append_int_noprefix(....) /* Variety */
Same with all these numbers they are used exactly once. Instead of a macro document in a comment:
build_append_int_noprefix(.... (0x0 << 30 /* 00b = 4 levels HATS */) |
....)
You must also find and document for each table the *earliest* spec that gives the features you are using and where it is in that spec.
Why earliest? it gives people better idea which guests it can be compatible with.
Quoted 22 lines
hw/i386/amd_iommu.h +6
-0@@ -175,9 +175,15 @@#define AMDVI_DTE_QUAD3_RESERVED (GENMASK64(14, 0) | GENMASK64(53, 48))/* AMDVI paging mode */ +#define AMDVI_GATS_MODE_SHIFT (12) +#define AMDVI_GATS_MODE_MASK (3ULL << 12)#define AMDVI_GATS_MODE (2ULL << 12) +#define AMDVI_HATS_MODE_SHIFT (10) +#define AMDVI_HATS_MODE_MASK (3ULL << 10)#define AMDVI_HATS_MODE (2ULL << 10)#define AMDVI_HATS_MODE_RESERVED (3ULL << 10) +#define AMDVI_GLX_SUP_SHIFT (14) +#define AMDVI_GLX_SUP_MASK (3ULL << 14)/* Page Table format */ Signature · 1 line
2.34.1
PATCH 6/8 acpi_build: Build IVRS feature report using extended feature register
In reply to Sairaj KodilkarCurrently IVRS feature report values are hardcoded, this is difficult to maintain as any updates to extended feature must be synced. Along with it current feature report does not have GATS and HATS set. Hence use the extended feature registers to build the IVRS feature report.
--- hw/i386/acpi-build.c | 38 +++++++++++++++++++++++++++++++------- hw/i386/acpi-build.h | 1 - 2 files changed, 31 insertions(+), 8 deletions(-)
hw/i386/acpi-build.c +31 -7
@@ -1747,6 +1747,36 @@ ivrs_host_bridges(Object *obj, void *opaque)return 0 ; } +/* +* IVHD type 0x10 reports features using Feature Reporting field, which has +* different format than extended feature register (EFR) in the IOMMU MMIO +* space. +* +* Convert the EFR format to feature reporting format. +*/ +static uint32_t +get_amd_ivhd_feature_report (AMDVIState *s) +{ +uint64_t feature =amdvi_extended_feature_register (s); +uint32_t is_gt = !!(feature &AMDVI_FEATURE_GT ); +uint32_t is_ga = !!(feature &AMDVI_FEATURE_GA ); +uint64_t glx_sup = (feature &AMDVI_GLX_SUP_MASK ) >>AMDVI_GLX_SUP_SHIFT ; +uint64_t hats_mode = (feature &AMDVI_HATS_MODE_MASK ) >> +AMDVI_HATS_MODE_SHIFT ; +uint64_t gats_mode = (feature &AMDVI_GATS_MODE_MASK ) >> +AMDVI_GATS_MODE_SHIFT ; +uint32_t feature_report; + + feature_report = s->xtsup <<AMD_IVHD_FEATURE_REPORT_XT_SUP_SHIFT | + is_gt <<AMD_IVHD_FEATURE_REPORT_GT_SUP_SHIFT | + glx_sup <<AMD_IVHD_FEATURE_REPORT_GLX_SUP_SHIFT | + is_ga <<AMD_IVHD_FEATURE_REPORT_GA_SUP_SHIFT | + hats_mode <<AMD_IVHD_FEATURE_REPORT_HATS_SHIFT | + gats_mode <<AMD_IVHD_FEATURE_REPORT_GATS_SHIFT ; + +return feature_report; +} +static void build_amd_iommu (GArray *table_data,BIOSLinker *linker,const char *oem_id,const char *oem_table_id)
@@ -1829,13 +1859,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IOMMU info */ build_append_int_noprefix (table_data,0 ,2 );/* IOMMU Feature Reporting */ - feature_report = (48UL <<30 ) |/* HATS */ - (48UL <<28 ) |/* GATS */ - (1UL <<2 ) |/* GTSup */ - (1UL <<6 );/* GASup */ -if (s->xtsup) { - feature_report |= (1UL <<0 );/* XTSup */ - } + feature_report =get_amd_ivhd_feature_report (s);build_append_int_noprefix (table_data, feature_report,4 );/* IVHD entries as found above */
hw/i386/acpi-build.h +0 -1
@@ -1,4 +1,3 @@ -#ifndef HW_I386_ACPI_BUILD_H #define HW_I386_ACPI_BUILD_H #include "hw/acpi/acpi-defs.h"
Signature · 1 line
2.34.1
Re: [PATCH 6/8] acpi_build: Build IVRS feature report using extended feature register
Quoted 31 lines · Sairaj Kodilkar
Currently IVRS feature report values are hardcoded, this is difficult to maintain as any updates to extended feature must be synced. Along with it current feature report does not have GATS and HATS set. Hence use the extended feature registers to build the IVRS feature report.
Quoted 1 line
Signed-off-by Sairaj Kodilkar <[email protected]>--- hw/i386/acpi-build.c | 38 +++++++++++++++++++++++++++++++------- hw/i386/acpi-build.h | 1 - 2 files changed, 31 insertions(+), 8 deletions(-)hw/i386/acpi-build.c +13
-0@@ -1747,6 +1747,36 @@ ivrs_host_bridges(Object *obj, void *opaque)return 0 ; } +/* +* IVHD type 0x10 reports features using Feature Reporting field, which has +* different format than extended feature register (EFR) in the IOMMU MMIO +* space. +* +* Convert the EFR format to feature reporting format. +*/ +static uint32_t +get_amd_ivhd_feature_report (AMDVIState *s) +{ +uint64_t feature =amdvi_extended_feature_register (s); +uint32_t is_gt = !!(feature &AMDVI_FEATURE_GT ); +uint32_t is_ga = !!(feature &AMDVI_FEATURE_GA );
hw/i386/acpi-build.c ivrs_host_bridges(Object *obj, void *opaque) · line 1772
Quoted 10 lines
hw/i386/acpi-build.c +10
-0+uint64_t glx_sup = (feature &AMDVI_GLX_SUP_MASK ) >>AMDVI_GLX_SUP_SHIFT ; +uint64_t hats_mode = (feature &AMDVI_HATS_MODE_MASK ) >> +AMDVI_HATS_MODE_SHIFT ; +uint64_t gats_mode = (feature &AMDVI_GATS_MODE_MASK ) >> +AMDVI_GATS_MODE_SHIFT ; +uint32_t feature_report; + + feature_report = s->xtsup <<AMD_IVHD_FEATURE_REPORT_XT_SUP_SHIFT | + is_gt <<AMD_IVHD_FEATURE_REPORT_GT_SUP_SHIFT | + glx_sup << AMD_IVHD_FEATURE_REPORT_GLX_SUP_SHIFT |
Looks like glx is new? Please drop the glx changes as we are not supporting guest page table yet.
Quoted 30 lines
hw/i386/acpi-build.c +8
-7+ is_ga <<AMD_IVHD_FEATURE_REPORT_GA_SUP_SHIFT | + hats_mode <<AMD_IVHD_FEATURE_REPORT_HATS_SHIFT | + gats_mode <<AMD_IVHD_FEATURE_REPORT_GATS_SHIFT ; + +return feature_report; +} +static void build_amd_iommu (GArray *table_data,BIOSLinker *linker,const char *oem_id,const char *oem_table_id)@@ -1829,13 +1859,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,/* IOMMU info */ build_append_int_noprefix (table_data,0 ,2 );/* IOMMU Feature Reporting */ - feature_report = (48UL <<30 ) |/* HATS */ - (48UL <<28 ) |/* GATS */ - (1UL <<2 ) |/* GTSup */ - (1UL <<6 );/* GASup */ -if (s->xtsup) { - feature_report |= (1UL <<0 );/* XTSup */ - } + feature_report =get_amd_ivhd_feature_report (s);build_append_int_noprefix (table_data, feature_report,4 );/* IVHD entries as found above */ hw/i386/acpi-build.h +0
-0
hw/i386/acpi-build.h line 1
Quoted 1 line
hw/i386/acpi-build.h +0
-1@@ -1,4 +1,3 @@ -
Unnecessary change.
-Vasant
PATCH 7/8 acpi_build: Cleanup AMD IOMMU IVRS building
In reply to Sairaj KodilkarUse structs and macros to improve the readability and maintainability of the the code.
--- hw/i386/acpi-build.c | 149 +++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 90 deletions(-)
hw/i386/acpi-build.c +59 -90
@@ -1663,11 +1663,13 @@ static void insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) {GArray *table_data = opaque; -uint32_t entry; +AmdIvhdDeviceEntry entry = {};/* "Select" IVHD entry, type 0x2 */ - entry =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn) <<8 |0x2 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT ; + entry.devid =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + +g_array_append_vals (table_data, &entry,sizeof (entry));if (object_dynamic_cast (OBJECT (dev),TYPE_PCI_BRIDGE )) {PCIBus *sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (dev));
@@ -1691,11 +1693,14 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)*/ if (sec == sub) {/* leaf bus */ /* "Start of Range" IVHD entry, type 0x3 */ - entry =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )) <<8 |0x3 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE ; + entry.devid =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); +g_array_append_vals (table_data, &entry,sizeof (entry)); +/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); }else {pci_for_each_device (sec_bus, sec, insert_ivhd, table_data); }
@@ -1708,24 +1713,26 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)* express bridges, just as in pci_device_iommu_address_space(). * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. */ -uint16_t dev_id_a, dev_id_b; +AmdIvhdDeviceEntryExt entry_ext = {}; - dev_id_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); + entry_ext.type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE ; + entry_ext.devid_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 ));if (pci_is_express (dev) &&pcie_cap_get_type (dev) ==PCI_EXP_TYPE_PCI_BRIDGE ) { - dev_id_b = dev_id_a; + entry_ext.devid_b = entry_ext.devid_a; }else { - dev_id_b =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + entry_ext.devid_b =PCI_BUILD_BDF (pci_bus_num (bus), + dev->devfn); }/* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ -build_append_int_noprefix (table_data, dev_id_a <<8 |0x43 ,4 ); -build_append_int_noprefix (table_data, dev_id_b <<8 |0x0 ,4 ); +g_array_append_vals (table_data, &entry_ext,sizeof (entry_ext));/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); } } }
@@ -1786,20 +1793,20 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id }; -uint64_t feature_report;int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev));uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn); +AmdIvrsVendorHdr ivrs_hdr = {}; +AmdIvhdHdr10 ivhd10 = {}; +AmdIvhdHdr11 ivhd11 = {};acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all * IOMMU units in a system */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* EFRSup */ -AMDVI_PA_SIZE_52 , -4 ); -/* reserved */ -build_append_int_noprefix (table_data,0 ,8 ); + ivrs_hdr.ivinfo =AMD_IVINFO_EFR_SUP |AMDVI_GVA_SIZE_48 | +AMDVI_PA_SIZE_52 |AMDVI_VA_SIZE_64 ; + +g_array_append_vals (table_data, &ivrs_hdr,sizeof (ivrs_hdr));/* * A PCI bus walk, for each PCI host bridge, is necessary to create a
@@ -1817,7 +1824,8 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* These are 4-byte device entries currently reporting the range of * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) */ -build_append_int_noprefix (ivhd_blob,0x0000001 ,4 ); +AmdIvhdDeviceEntry entry = { .type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALL }; +g_array_append_vals (ivhd_blob, &entry,sizeof (entry)); }/*
@@ -1829,76 +1837,37 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' */ if (x86_iommu_ir_supported (x86_iommu_get_default ())) { -build_append_int_noprefix (ivhd_blob, - (0x1ull <<56 ) |/* type IOAPIC */ - (IOAPIC_SB_DEVID <<40 ) |/* IOAPIC devid */ -0x48 ,/* special device */ -8 ); - } - -/* IVHD definition - type 10h */ -build_append_int_noprefix (table_data,0x10 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ) |/* iotblSup */ - (1UL <<6 ) |/* PrefSup */ - (1UL <<7 ),/* PPRSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 ); -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Feature Reporting */ - feature_report =get_amd_ivhd_feature_report (s); -build_append_int_noprefix (table_data, feature_report,4 ); - +AmdIvhdDeviceEntryExt entry_ext = { + .type =AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE , + .devid_b =IOAPIC_SB_DEVID , + .variety =IVHD_VARIETY_IOAPIC + }; + +g_array_append_vals (ivhd_blob, &entry_ext,sizeof (entry_ext)); + } + + ivhd10.type =0x10 ; + ivhd10.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP | +AMD_IVHD_FLAG_PREF_SUP |AMD_IVHD_FLAG_PPR_SUP ; + ivhd10.length = ivhd_blob->len +sizeof (ivhd10); + ivhd10.devid = iommu_devid; + ivhd10.capab_offset = s->pci->capab_offset; + ivhd10.base_addr = s->mr_mmio.addr; + ivhd10.iommu_feature_report =get_amd_ivhd_feature_report (s); +g_array_append_vals (table_data, &ivhd10,sizeof (ivhd10));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len); -/* IVHD definition - type 11h */ -build_append_int_noprefix (table_data,0x11 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ),/* iotblSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); - -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Attributes */ -if (!s->iommu.dma_translation) { -build_append_int_noprefix (table_data, (1UL <<0 )/* HATDis */ ,4 ); - }else { -build_append_int_noprefix (table_data,0 ,4 ); - } -/* EFR Register Image */ -build_append_int_noprefix (table_data, -amdvi_extended_feature_register (s), -8 ); -/* EFR Register Image 2 */ -build_append_int_noprefix (table_data,0 ,8 ); - + ivhd11.type =0x11 ; + ivhd11.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP ; + ivhd11.length = ivhd_blob->len +sizeof (ivhd11); + ivhd11.devid = iommu_devid; + ivhd11.capab_offset = s->pci->capab_offset; + ivhd11.base_addr = s->mr_mmio.addr; + ivhd11.iommu_attributes = !s->iommu.dma_translation << +AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT ; + ivhd11.efr =amdvi_extended_feature_register (s); +g_array_append_vals (table_data, &ivhd11,sizeof (ivhd11));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len);
Signature · 1 line
2.34.1
Re: [PATCH 7/8] acpi_build: Cleanup AMD IOMMU IVRS building
Quoted 98 lines · Sairaj Kodilkar
Use structs and macros to improve the readability and maintainability of the the code.
Signed-off-by Sairaj Kodilkar <[email protected]>--- hw/i386/acpi-build.c | 149 +++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 90 deletions(-)hw/i386/acpi-build.c +25
-16@@ -1663,11 +1663,13 @@ static void insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) {GArray *table_data = opaque; -uint32_t entry; +AmdIvhdDeviceEntry entry = {};/* "Select" IVHD entry, type 0x2 */ - entry =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn) <<8 |0x2 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT ; + entry.devid =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + +g_array_append_vals (table_data, &entry,sizeof (entry));if (object_dynamic_cast (OBJECT (dev),TYPE_PCI_BRIDGE )) {PCIBus *sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (dev));@@ -1691,11 +1693,14 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)*/ if (sec == sub) {/* leaf bus */ /* "Start of Range" IVHD entry, type 0x3 */ - entry =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )) <<8 |0x3 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE ; + entry.devid =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); +g_array_append_vals (table_data, &entry,sizeof (entry)); +/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); }else {pci_for_each_device (sec_bus, sec, insert_ivhd, table_data); }@@ -1708,24 +1713,26 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)* express bridges, just as in pci_device_iommu_address_space(). * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. */ -uint16_t dev_id_a, dev_id_b; +AmdIvhdDeviceEntryExt entry_ext = {}; - dev_id_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); + entry_ext.type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE ; + entry_ext.devid_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 ));if (pci_is_express (dev) &&pcie_cap_get_type (dev) ==PCI_EXP_TYPE_PCI_BRIDGE ) { - dev_id_b = dev_id_a; + entry_ext.devid_b = entry_ext.devid_a; }else { - dev_id_b =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + entry_ext.devid_b =PCI_BUILD_BDF (pci_bus_num (bus), + dev->devfn); }/* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ -build_append_int_noprefix (table_data, dev_id_a <<8 |0x43 ,4 ); -build_append_int_noprefix (table_data, dev_id_b <<8 |0x0 ,4 ); +g_array_append_vals (table_data, &entry_ext,sizeof (entry_ext));/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); } } }@@ -1786,20 +1793,20 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id }; -uint64_t feature_report;int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev));uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn); +AmdIvrsVendorHdr ivrs_hdr = {}; +AmdIvhdHdr10 ivhd10 = {}; +AmdIvhdHdr11 ivhd11 = {};acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all * IOMMU units in a system */
hw/i386/acpi-build.c build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id, · line 1809
Quoted 10 lines
hw/i386/acpi-build.c +4
-6-build_append_int_noprefix (table_data, - (1UL <<0 ) |/* EFRSup */ -AMDVI_PA_SIZE_52 , -4 ); -/* reserved */ -build_append_int_noprefix (table_data,0 ,8 ); + ivrs_hdr.ivinfo =AMD_IVINFO_EFR_SUP |AMDVI_GVA_SIZE_48 | +AMDVI_PA_SIZE_52 |AMDVI_VA_SIZE_64 ; + +g_array_append_vals (table_data, &ivrs_hdr,sizeof (ivrs_hdr));
This has broken endian-ness.
Please do not use packed structs.
ACPI has infrastructure to build up structs such as build_append_int_noprefix.
Just use that.
Quoted 120 lines
hw/i386/acpi-build.c +30
-68/* * A PCI bus walk, for each PCI host bridge, is necessary to create a @@ -1817,7 +1824,8 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* These are 4-byte device entries currently reporting the range of * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) */ -build_append_int_noprefix (ivhd_blob,0x0000001 ,4 ); +AmdIvhdDeviceEntry entry = { .type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALL }; +g_array_append_vals (ivhd_blob, &entry,sizeof (entry)); }/* @@ -1829,76 +1837,37 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' */ if (x86_iommu_ir_supported (x86_iommu_get_default ())) { -build_append_int_noprefix (ivhd_blob, - (0x1ull <<56 ) |/* type IOAPIC */ - (IOAPIC_SB_DEVID <<40 ) |/* IOAPIC devid */ -0x48 ,/* special device */ -8 ); - } - -/* IVHD definition - type 10h */ -build_append_int_noprefix (table_data,0x10 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ) |/* iotblSup */ - (1UL <<6 ) |/* PrefSup */ - (1UL <<7 ),/* PPRSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 ); -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Feature Reporting */ - feature_report =get_amd_ivhd_feature_report (s); -build_append_int_noprefix (table_data, feature_report,4 ); - +AmdIvhdDeviceEntryExt entry_ext = { + .type =AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE , + .devid_b =IOAPIC_SB_DEVID , + .variety =IVHD_VARIETY_IOAPIC + }; + +g_array_append_vals (ivhd_blob, &entry_ext,sizeof (entry_ext)); + } + + ivhd10.type =0x10 ; + ivhd10.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP | +AMD_IVHD_FLAG_PREF_SUP |AMD_IVHD_FLAG_PPR_SUP ; + ivhd10.length = ivhd_blob->len +sizeof (ivhd10); + ivhd10.devid = iommu_devid; + ivhd10.capab_offset = s->pci->capab_offset; + ivhd10.base_addr = s->mr_mmio.addr; + ivhd10.iommu_feature_report =get_amd_ivhd_feature_report (s); +g_array_append_vals (table_data, &ivhd10,sizeof (ivhd10));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len); -/* IVHD definition - type 11h */ -build_append_int_noprefix (table_data,0x11 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ),/* iotblSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); - -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Attributes */ -if (!s->iommu.dma_translation) { -build_append_int_noprefix (table_data, (1UL <<0 )/* HATDis */ ,4 ); - }else { -build_append_int_noprefix (table_data,0 ,4 ); - } -/* EFR Register Image */ -build_append_int_noprefix (table_data, -amdvi_extended_feature_register (s), -8 ); -/* EFR Register Image 2 */ -build_append_int_noprefix (table_data,0 ,8 ); - + ivhd11.type =0x11 ; + ivhd11.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP ; + ivhd11.length = ivhd_blob->len +sizeof (ivhd11); + ivhd11.devid = iommu_devid; + ivhd11.capab_offset = s->pci->capab_offset; + ivhd11.base_addr = s->mr_mmio.addr; + ivhd11.iommu_attributes = !s->iommu.dma_translation << +AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT ; + ivhd11.efr =amdvi_extended_feature_register (s); +g_array_append_vals (table_data, &ivhd11,sizeof (ivhd11));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len);Signature · 1 line
2.34.1
Re: [PATCH 7/8] acpi_build: Cleanup AMD IOMMU IVRS building
In reply to Sairaj KodilkarQuoted 4 lines · Sairaj Kodilkar
Use structs and macros to improve the readability and maintainability of the the code.
Signed-off-by Sairaj Kodilkar <[email protected]>
This does not "improve the readability and maintainability" it blows up code size by a factor of 4, makes it impossible to find things in the spec and breaks endian-ness.
Please don't.
Quoted 224 lines
--- hw/i386/acpi-build.c | 149 +++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 90 deletions(-)hw/i386/acpi-build.c +59
-90@@ -1663,11 +1663,13 @@ static void insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) {GArray *table_data = opaque; -uint32_t entry; +AmdIvhdDeviceEntry entry = {};/* "Select" IVHD entry, type 0x2 */ - entry =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn) <<8 |0x2 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT ; + entry.devid =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + +g_array_append_vals (table_data, &entry,sizeof (entry));if (object_dynamic_cast (OBJECT (dev),TYPE_PCI_BRIDGE )) {PCIBus *sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (dev));@@ -1691,11 +1693,14 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)*/ if (sec == sub) {/* leaf bus */ /* "Start of Range" IVHD entry, type 0x3 */ - entry =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )) <<8 |0x3 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE ; + entry.devid =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); +g_array_append_vals (table_data, &entry,sizeof (entry)); +/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); }else {pci_for_each_device (sec_bus, sec, insert_ivhd, table_data); }@@ -1708,24 +1713,26 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)* express bridges, just as in pci_device_iommu_address_space(). * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. */ -uint16_t dev_id_a, dev_id_b; +AmdIvhdDeviceEntryExt entry_ext = {}; - dev_id_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); + entry_ext.type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE ; + entry_ext.devid_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 ));if (pci_is_express (dev) &&pcie_cap_get_type (dev) ==PCI_EXP_TYPE_PCI_BRIDGE ) { - dev_id_b = dev_id_a; + entry_ext.devid_b = entry_ext.devid_a; }else { - dev_id_b =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + entry_ext.devid_b =PCI_BUILD_BDF (pci_bus_num (bus), + dev->devfn); }/* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ -build_append_int_noprefix (table_data, dev_id_a <<8 |0x43 ,4 ); -build_append_int_noprefix (table_data, dev_id_b <<8 |0x0 ,4 ); +g_array_append_vals (table_data, &entry_ext,sizeof (entry_ext));/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); } } }@@ -1786,20 +1793,20 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id }; -uint64_t feature_report;int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev));uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn); +AmdIvrsVendorHdr ivrs_hdr = {}; +AmdIvhdHdr10 ivhd10 = {}; +AmdIvhdHdr11 ivhd11 = {};acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all * IOMMU units in a system */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* EFRSup */ -AMDVI_PA_SIZE_52 , -4 ); -/* reserved */ -build_append_int_noprefix (table_data,0 ,8 ); + ivrs_hdr.ivinfo =AMD_IVINFO_EFR_SUP |AMDVI_GVA_SIZE_48 | +AMDVI_PA_SIZE_52 |AMDVI_VA_SIZE_64 ; + +g_array_append_vals (table_data, &ivrs_hdr,sizeof (ivrs_hdr));/* * A PCI bus walk, for each PCI host bridge, is necessary to create a @@ -1817,7 +1824,8 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* These are 4-byte device entries currently reporting the range of * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) */ -build_append_int_noprefix (ivhd_blob,0x0000001 ,4 ); +AmdIvhdDeviceEntry entry = { .type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALL }; +g_array_append_vals (ivhd_blob, &entry,sizeof (entry)); }/* @@ -1829,76 +1837,37 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' */ if (x86_iommu_ir_supported (x86_iommu_get_default ())) { -build_append_int_noprefix (ivhd_blob, - (0x1ull <<56 ) |/* type IOAPIC */ - (IOAPIC_SB_DEVID <<40 ) |/* IOAPIC devid */ -0x48 ,/* special device */ -8 ); - } - -/* IVHD definition - type 10h */ -build_append_int_noprefix (table_data,0x10 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ) |/* iotblSup */ - (1UL <<6 ) |/* PrefSup */ - (1UL <<7 ),/* PPRSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 ); -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Feature Reporting */ - feature_report =get_amd_ivhd_feature_report (s); -build_append_int_noprefix (table_data, feature_report,4 ); - +AmdIvhdDeviceEntryExt entry_ext = { + .type =AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE , + .devid_b =IOAPIC_SB_DEVID , + .variety =IVHD_VARIETY_IOAPIC + }; + +g_array_append_vals (ivhd_blob, &entry_ext,sizeof (entry_ext)); + } + + ivhd10.type =0x10 ; + ivhd10.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP | +AMD_IVHD_FLAG_PREF_SUP |AMD_IVHD_FLAG_PPR_SUP ; + ivhd10.length = ivhd_blob->len +sizeof (ivhd10); + ivhd10.devid = iommu_devid; + ivhd10.capab_offset = s->pci->capab_offset; + ivhd10.base_addr = s->mr_mmio.addr; + ivhd10.iommu_feature_report =get_amd_ivhd_feature_report (s); +g_array_append_vals (table_data, &ivhd10,sizeof (ivhd10));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len); -/* IVHD definition - type 11h */ -build_append_int_noprefix (table_data,0x11 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ),/* iotblSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); - -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Attributes */ -if (!s->iommu.dma_translation) { -build_append_int_noprefix (table_data, (1UL <<0 )/* HATDis */ ,4 ); - }else { -build_append_int_noprefix (table_data,0 ,4 ); - } -/* EFR Register Image */ -build_append_int_noprefix (table_data, -amdvi_extended_feature_register (s), -8 ); -/* EFR Register Image 2 */ -build_append_int_noprefix (table_data,0 ,8 ); - + ivhd11.type =0x11 ; + ivhd11.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP ; + ivhd11.length = ivhd_blob->len +sizeof (ivhd11); + ivhd11.devid = iommu_devid; + ivhd11.capab_offset = s->pci->capab_offset; + ivhd11.base_addr = s->mr_mmio.addr; + ivhd11.iommu_attributes = !s->iommu.dma_translation << +AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT ; + ivhd11.efr =amdvi_extended_feature_register (s); +g_array_append_vals (table_data, &ivhd11,sizeof (ivhd11));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len);Signature · 1 line
2.34.1
Re: [PATCH 7/8] acpi_build: Cleanup AMD IOMMU IVRS building
Quoted 11 lines · Michael S. Tsirkin
Quoted 4 lines · Sairaj Kodilkar
Use structs and macros to improve the readability and maintainability of the the code.
Signed-off-by Sairaj Kodilkar <[email protected]>This does not "improve the readability and maintainability" it blows up code size by a factor of 4, makes it impossible to find things in the spec and breaks endian-ness.
Please don't.
Understood, will change this in v2
Thanks
Sairaj.
Quoted 226 lines
Quoted 225 lines
--- hw/i386/acpi-build.c | 149 +++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 90 deletions(-)hw/i386/acpi-build.c +59
-90@@ -1663,11 +1663,13 @@ static void insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) {GArray *table_data = opaque; -uint32_t entry; +AmdIvhdDeviceEntry entry = {};/* "Select" IVHD entry, type 0x2 */ - entry =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn) <<8 |0x2 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_SELECT ; + entry.devid =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + +g_array_append_vals (table_data, &entry,sizeof (entry));if (object_dynamic_cast (OBJECT (dev),TYPE_PCI_BRIDGE )) {PCIBus *sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (dev));@@ -1691,11 +1693,14 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)*/ if (sec == sub) {/* leaf bus */ /* "Start of Range" IVHD entry, type 0x3 */ - entry =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )) <<8 |0x3 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_START_RANGE ; + entry.devid =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); +g_array_append_vals (table_data, &entry,sizeof (entry)); +/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); }else {pci_for_each_device (sec_bus, sec, insert_ivhd, table_data); }@@ -1708,24 +1713,26 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)* express bridges, just as in pci_device_iommu_address_space(). * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. */ -uint16_t dev_id_a, dev_id_b; +AmdIvhdDeviceEntryExt entry_ext = {}; - dev_id_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 )); + entry_ext.type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALIAS_START_RANGE ; + entry_ext.devid_a =PCI_BUILD_BDF (sec,PCI_DEVFN (0 ,0 ));if (pci_is_express (dev) &&pcie_cap_get_type (dev) ==PCI_EXP_TYPE_PCI_BRIDGE ) { - dev_id_b = dev_id_a; + entry_ext.devid_b = entry_ext.devid_a; }else { - dev_id_b =PCI_BUILD_BDF (pci_bus_num (bus), dev->devfn); + entry_ext.devid_b =PCI_BUILD_BDF (pci_bus_num (bus), + dev->devfn); }/* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ -build_append_int_noprefix (table_data, dev_id_a <<8 |0x43 ,4 ); -build_append_int_noprefix (table_data, dev_id_b <<8 |0x0 ,4 ); +g_array_append_vals (table_data, &entry_ext,sizeof (entry_ext));/* "End of Range" IVHD entry, type 0x4 */ - entry =PCI_BUILD_BDF (sub,PCI_DEVFN (31 ,7 )) <<8 |0x4 ; -build_append_int_noprefix (table_data, entry,4 ); + entry.type =AMD_IVHD_DEVICE_ENTRY_TYPE_END_RANGE ; + entry.devid =PCI_BUILD_BDF (sub,PCI_DEVFN_MAX -1 ); +g_array_append_vals (table_data, &entry,sizeof (entry)); } } }@@ -1786,20 +1793,20 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,GArray *ivhd_blob =g_array_new (false, true,1 );AcpiTable table = { .sig ="IVRS" , .rev =1 , .oem_id = oem_id, .oem_table_id = oem_table_id }; -uint64_t feature_report;int iommu_bus =pci_bus_num (pci_get_bus (iommu_dev));uint16_t iommu_devid =PCI_BUILD_BDF (iommu_bus, iommu_dev->devfn); +AmdIvrsVendorHdr ivrs_hdr = {}; +AmdIvhdHdr10 ivhd10 = {}; +AmdIvhdHdr11 ivhd11 = {};acpi_table_begin (&table, table_data);/* IVinfo - IO virtualization information common to all * IOMMU units in a system */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* EFRSup */ -AMDVI_PA_SIZE_52 , -4 ); -/* reserved */ -build_append_int_noprefix (table_data,0 ,8 ); + ivrs_hdr.ivinfo =AMD_IVINFO_EFR_SUP |AMDVI_GVA_SIZE_48 | +AMDVI_PA_SIZE_52 |AMDVI_VA_SIZE_64 ; + +g_array_append_vals (table_data, &ivrs_hdr,sizeof (ivrs_hdr));/* * A PCI bus walk, for each PCI host bridge, is necessary to create a @@ -1817,7 +1824,8 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* These are 4-byte device entries currently reporting the range of * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) */ -build_append_int_noprefix (ivhd_blob,0x0000001 ,4 ); +AmdIvhdDeviceEntry entry = { .type =AMD_IVHD_DEVICE_ENTRY_TYPE_ALL }; +g_array_append_vals (ivhd_blob, &entry,sizeof (entry)); }/* @@ -1829,76 +1837,37 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,* See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' */ if (x86_iommu_ir_supported (x86_iommu_get_default ())) { -build_append_int_noprefix (ivhd_blob, - (0x1ull <<56 ) |/* type IOAPIC */ - (IOAPIC_SB_DEVID <<40 ) |/* IOAPIC devid */ -0x48 ,/* special device */ -8 ); - } - -/* IVHD definition - type 10h */ -build_append_int_noprefix (table_data,0x10 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ) |/* iotblSup */ - (1UL <<6 ) |/* PrefSup */ - (1UL <<7 ),/* PPRSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +24 ,2 ); -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Feature Reporting */ - feature_report =get_amd_ivhd_feature_report (s); -build_append_int_noprefix (table_data, feature_report,4 ); - +AmdIvhdDeviceEntryExt entry_ext = { + .type =AMD_IVHD_DEVICE_ENTRY_TYPE_SPECIAL_DEVICE , + .devid_b =IOAPIC_SB_DEVID , + .variety =IVHD_VARIETY_IOAPIC + }; + +g_array_append_vals (ivhd_blob, &entry_ext,sizeof (entry_ext)); + } + + ivhd10.type =0x10 ; + ivhd10.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP | +AMD_IVHD_FLAG_PREF_SUP |AMD_IVHD_FLAG_PPR_SUP ; + ivhd10.length = ivhd_blob->len +sizeof (ivhd10); + ivhd10.devid = iommu_devid; + ivhd10.capab_offset = s->pci->capab_offset; + ivhd10.base_addr = s->mr_mmio.addr; + ivhd10.iommu_feature_report =get_amd_ivhd_feature_report (s); +g_array_append_vals (table_data, &ivhd10,sizeof (ivhd10));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len); -/* IVHD definition - type 11h */ -build_append_int_noprefix (table_data,0x11 ,1 ); -/* virtualization flags */ -build_append_int_noprefix (table_data, - (1UL <<0 ) |/* HtTunEn */ - (1UL <<4 ),/* iotblSup */ -1 ); - -/* IVHD length */ -build_append_int_noprefix (table_data, ivhd_blob->len +40 ,2 ); - -/* DeviceID */ -build_append_int_noprefix (table_data, iommu_devid,2 ); -/* Capability offset */ -build_append_int_noprefix (table_data, s->pci->capab_offset,2 ); -/* IOMMU base address */ -build_append_int_noprefix (table_data, s->mr_mmio.addr,8 ); -/* PCI Segment Group */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU info */ -build_append_int_noprefix (table_data,0 ,2 ); -/* IOMMU Attributes */ -if (!s->iommu.dma_translation) { -build_append_int_noprefix (table_data, (1UL <<0 )/* HATDis */ ,4 ); - }else { -build_append_int_noprefix (table_data,0 ,4 ); - } -/* EFR Register Image */ -build_append_int_noprefix (table_data, -amdvi_extended_feature_register (s), -8 ); -/* EFR Register Image 2 */ -build_append_int_noprefix (table_data,0 ,8 ); - + ivhd11.type =0x11 ; + ivhd11.flags =AMD_IVHD_FLAG_HT_TUN_EN |AMD_IVHD_FLAG_IOTLB_SUP ; + ivhd11.length = ivhd_blob->len +sizeof (ivhd11); + ivhd11.devid = iommu_devid; + ivhd11.capab_offset = s->pci->capab_offset; + ivhd11.base_addr = s->mr_mmio.addr; + ivhd11.iommu_attributes = !s->iommu.dma_translation << +AMD_IVHD_ATTRIBUTES_HATDIS_SHIFT ; + ivhd11.efr =amdvi_extended_feature_register (s); +g_array_append_vals (table_data, &ivhd11,sizeof (ivhd11));/* IVHD entries as found above */ g_array_append_vals (table_data, ivhd_blob->data, ivhd_blob->len);Signature · 1 line
2.34.1
PATCH 8/8 tests/acpi: x86: update golden masters for IVRS
In reply to Sairaj KodilkarDifferences between disassembled ASL files for IVRS:
/*
* Intel ACPI Component Architecture
* AML/ASL+ Disassembler version 20200925 (64-bit version)
* Copyright (c) 2000 - 2020 Intel Corporation
*
- * Disassembly of tests/data/acpi/x86/q35/IVRS.ivrs, Mon May 11 11:21:47 2026
+ * Disassembly of /tmp/aml-NGQ9O3, Mon May 11 11:21:47 2026
*
* ACPI Data Table [IVRS]
*
* Format: [HexOffset DecimalOffset ByteLength] FieldName : FieldValue */
[000h 0000 4] Signature : "IVRS" [I/O Virtualization Reporting Structure] [004h 0004 4] Table Length : 000000B0 [008h 0008 1] Revision : 01 -[009h 0009 1] Checksum : 74 +[009h 0009 1] Checksum : 68 [00Ah 0010 6] Oem ID : "BOCHS " [010h 0016 8] Oem Table ID : "BXPC " [018h 0024 4] Oem Revision : 00000001 [01Ch 0028 4] Asl Compiler ID : "BXPC" [020h 0032 4] Asl Compiler Revision : 00000001
-[024h 0036 4] Virtualization Info : 00002801 +[024h 0036 4] Virtualization Info : 00203441 [028h 0040 8] Reserved : 0000000000000000
[030h 0048 1] Subtable Type : 10 [Hardware Definition Block] [031h 0049 1] Flags : D1 [032h 0050 2] Length : 0038 [034h 0052 2] DeviceId : 0010
[036h 0054 2] Capability Offset : 0040 [038h 0056 8] Base Address : 00000000FED80000 [040h 0064 2] PCI Segment Group : 0000 [042h 0066 2] Virtualization Info : 0000 -[044h 0068 4] Feature Reporting : 00000044 +[044h 0068 4] Feature Reporting : A0000044
[048h 0072 1] Entry Type : 02 [049h 0073 2] Device ID : 0000 [04Bh 0075 1] Data Setting : 00
[04Ch 0076 1] Entry Type : 02 [04Dh 0077 2] Device ID : 0008 [04Fh 0079 1] Data Setting : 00
[050h 0080 1] Entry Type : 02 [051h 0081 2] Device ID : 0010 [053h 0083 1] Data Setting : 00
[054h 0084 1] Entry Type : 02 [055h 0085 2] Device ID : 00F8 [057h 0087 1] Data Setting : 00
@@ -98,27 +98,27 @@
[0A1h 0161 2] Device ID : 00FA
[0A3h 0163 1] Data Setting : 00
[0A4h 0164 1] Entry Type : 02 [0A5h 0165 2] Device ID : 00FB [0A7h 0167 1] Data Setting : 00
[0A8h 0168 1] Entry Type : 48 [0A9h 0169 2] Device ID : 0000 [0ABh 0171 1] Data Setting : 00 [0ACh 0172 1] Handle : 00 [0ADh 0173 2] Source Used Device ID : 00A0 [0AFh 0175 1] Variety : 01
--- tests/qtest/bios-tables-test-allowed-diff.h | 1 - 1 file changed, 1 deletion(-)
tests/qtest/bios-tables-test-allowed-diff.h +0 -1
@@ -1,2 +1 @@/* List of comma-separated changed AML files to ignore */ -tests/data/acpi/x86/q35/IVRS .ivrs
Signature · 1 line
2.34.1