AMD/IOMMU: Improve register_iommu_exclusion_range()
PATCH 1/1 AMD/IOMMU: Improve register_iommu_exclusion_range()
* Use 64bit accesses instead of 32bit accesses
* Simplify the constant names
* Pull base into a local variable to avoid it being reloaded because of the memory clobber in writeq().
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
RFC. This is my proposed way of cleaning up the whole IOMMU file. The diffstat speaks for itself.
I've finally found the bit in the AMD IOMMU spec which says 64bit accesses are permitted:
3.4 IOMMU MMIO Registers:
Software access to IOMMU registers may not be larger than 64 bits. Accesses must be aligned to the size of the access and the size in bytes must be a power of two. Software may use accesses as small as one byte.
If we want to further simplify the logic, we could reject non-page-aligned base/limits when parsing IVRS.
Also, these registers don't exist in newer AMD systems:
When the system is SNP-enabled, the contents of the Exclusion range base address field are locked and re- purposed as the Completion store base address field. This contains bits [51:12] of the 4Kbyte-aligned base address that defines the starting address range that host COMPLETION_WAIT stores may target
I take this to mean the writes are discarded.
--- xen/drivers/passthrough/amd/iommu-defs.h | 20 +++--------- xen/drivers/passthrough/amd/iommu_init.c | 41 ++++++------------------ 2 files changed, 14 insertions(+), 47 deletions(-)
xen/drivers/passthrough/amd/iommu-defs.h +4 -16
@@ -338,22 +338,10 @@ union amd_iommu_control { };/* Exclusion Register */ -#define IOMMU_EXCLUSION_BASE_LOW_OFFSET 0x20 -#define IOMMU_EXCLUSION_BASE_HIGH_OFFSET 0x24 -#define IOMMU_EXCLUSION_LIMIT_LOW_OFFSET 0x28 -#define IOMMU_EXCLUSION_LIMIT_HIGH_OFFSET 0x2C -#define IOMMU_EXCLUSION_BASE_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_BASE_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_BASE_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_BASE_HIGH_SHIFT 0 -#define IOMMU_EXCLUSION_RANGE_ENABLE_MASK 0x00000001U -#define IOMMU_EXCLUSION_RANGE_ENABLE_SHIFT 0 -#define IOMMU_EXCLUSION_ALLOW_ALL_MASK 0x00000002U -#define IOMMU_EXCLUSION_ALLOW_ALL_SHIFT 1 -#define IOMMU_EXCLUSION_LIMIT_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_LIMIT_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_LIMIT_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_LIMIT_HIGH_SHIFT 0 +#define IOMMU_MMIO_EXCLUSION_BASE 0x20 +#define EXCLUSION_RANGE_ENABLE (1 << 0) +#define EXCLUSION_ALLOW_ALL (1 << 1) +#define IOMMU_MMIO_EXCLUSION_LIMIT 0x28/* Extended Feature Register */ #define IOMMU_EXT_FEATURE_MMIO_OFFSET 0x30
xen/drivers/passthrough/amd/iommu_init.c +10 -31
50 lines not on this page · raw message
Signature · 1 line
2.39.2
Quoted 7 lines · Andrew Cooper
* Use 64bit accesses instead of 32bit accesses
* Simplify the constant names
* Pull base into a local variable to avoid it being reloaded because of the memory clobber inwriteq().No functional change.
Quoted 7 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>RFC. This is my proposed way of cleaning up the whole IOMMU file. The diffstat speaks for itself.
Absolutely.
Quoted 8 lines
I've finally found the bit in the AMD IOMMU spec which says 64bit accesses are permitted:
3.4 IOMMU MMIO Registers:
Software access to IOMMU registers may not be larger than 64 bits. Accesses must be aligned to the size of the access and the size in bytes must be a power of two. Software may use accesses as small as one byte.
I take it that the use of 32-bit writes was because of the past need also work in a 32-bit hypervisor, not because of perceived restrictions by the spec.
Quoted 16 lines
xen/drivers/passthrough/amd/iommu-defs.h +0
-10@@ -338,22 +338,10 @@ union amd_iommu_control { };/* Exclusion Register */ -#define IOMMU_EXCLUSION_BASE_LOW_OFFSET 0x20 -#define IOMMU_EXCLUSION_BASE_HIGH_OFFSET 0x24 -#define IOMMU_EXCLUSION_LIMIT_LOW_OFFSET 0x28 -#define IOMMU_EXCLUSION_LIMIT_HIGH_OFFSET 0x2C -#define IOMMU_EXCLUSION_BASE_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_BASE_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_BASE_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_BASE_HIGH_SHIFT 0 -#define IOMMU_EXCLUSION_RANGE_ENABLE_MASK 0x00000001U -#define IOMMU_EXCLUSION_RANGE_ENABLE_SHIFT 0
xen/drivers/passthrough/amd/iommu-defs.h union amd_iommu_control { · line 344
Quoted 10 lines
xen/drivers/passthrough/amd/iommu-defs.h +4
-6-#define IOMMU_EXCLUSION_ALLOW_ALL_MASK 0x00000002U -#define IOMMU_EXCLUSION_ALLOW_ALL_SHIFT 1 -#define IOMMU_EXCLUSION_LIMIT_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_LIMIT_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_LIMIT_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_LIMIT_HIGH_SHIFT 0 +#define IOMMU_MMIO_EXCLUSION_BASE 0x20 +#define EXCLUSION_RANGE_ENABLE (1 << 0) +#define EXCLUSION_ALLOW_ALL (1 << 1) +#define IOMMU_MMIO_EXCLUSION_LIMIT 0x28
Just one question here: Previously you suggested we switch to bitfields for anything like this, and we've already done so with e.g. union amd_iommu_control and union amd_iommu_ext_features. IOW I wonder if we wouldn't better strive to be consistent in this regard. Or if not, what the (written or unwritten) guidelines are when to use which approach.
Jan
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
I've finally found the bit in the AMD IOMMU spec which says 64bit accesses are permitted:
3.4 IOMMU MMIO Registers:
Quoted 7 lines
Quoted 3 lines
Software access to IOMMU registers may not be larger than 64 bits. Accesses must be aligned to the size of the access and the size in bytes must be a power of two. Software may use accesses as small as one byte.
I take it that the use of 32-bit writes was because of the past need also work in a 32-bit hypervisor, not because of perceived restrictions by the spec.
In fact it looks like we're already halfway through converting to writeq().
Jan
Quoted 20 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
* Use 64bit accesses instead of 32bit accesses
* Simplify the constant names
* Pull base into a local variable to avoid it being reloaded because of the memory clobber inwriteq().No functional change.
Quoted 7 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>RFC. This is my proposed way of cleaning up the whole IOMMU file. The diffstat speaks for itself.
Absolutely.
Quoted 3 lines
I've finally found the bit in the AMD IOMMU spec which says 64bit accesses are permitted:
Quoted 8 lines
Quoted 5 lines
3.4 IOMMU MMIO Registers:
Software access to IOMMU registers may not be larger than 64 bits. Accesses must be aligned to the size of the access and the size in bytes must be a power of two. Software may use accesses as small as one byte.
I take it that the use of 32-bit writes was because of the past need also work in a 32-bit hypervisor, not because of perceived restrictions by the spec.
I recall having problems getting writeq() acked in the past, even after we'd dropped 32bit.
But this is the first time that I've positively found anything in the spec saying that 64bit accesses are ok.
Quoted 25 lines
Quoted 25 lines
xen/drivers/passthrough/amd/iommu-defs.h +2
-16@@ -338,22 +338,10 @@ union amd_iommu_control { };/* Exclusion Register */ -#define IOMMU_EXCLUSION_BASE_LOW_OFFSET 0x20 -#define IOMMU_EXCLUSION_BASE_HIGH_OFFSET 0x24 -#define IOMMU_EXCLUSION_LIMIT_LOW_OFFSET 0x28 -#define IOMMU_EXCLUSION_LIMIT_HIGH_OFFSET 0x2C -#define IOMMU_EXCLUSION_BASE_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_BASE_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_BASE_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_BASE_HIGH_SHIFT 0 -#define IOMMU_EXCLUSION_RANGE_ENABLE_MASK 0x00000001U -#define IOMMU_EXCLUSION_RANGE_ENABLE_SHIFT 0 -#define IOMMU_EXCLUSION_ALLOW_ALL_MASK 0x00000002U -#define IOMMU_EXCLUSION_ALLOW_ALL_SHIFT 1 -#define IOMMU_EXCLUSION_LIMIT_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_LIMIT_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_LIMIT_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_LIMIT_HIGH_SHIFT 0 +#define IOMMU_MMIO_EXCLUSION_BASE 0x20 +#define EXCLUSION_RANGE_ENABLE (1 << 0)
Quoted 8 lines
xen/drivers/passthrough/amd/iommu-defs.h union amd_iommu_control { · line 344
Quoted 2 lines
xen/drivers/passthrough/amd/iommu-defs.h +2
-0+#define EXCLUSION_ALLOW_ALL (1 << 1) +#define IOMMU_MMIO_EXCLUSION_LIMIT 0x28Just one question here: Previously you suggested we switch to bitfields for anything like this, and we've already done so with e.g. union
amd_iommu_controland unionamd_iommu_ext_features. IOW I wonder if we wouldn't better strive to be consistent in this regard. Or if not, what the (written or unwritten) guidelines are when to use which approach.
We've got two very different kinds of things here.
The device table/etc are in-memory WB datastructure which we're interpreting and editing routinely. It's completely full of bits and small fields, and letting the compiler do the hard work there is preferable; certainly in terms of legibility.
This example is an MMIO register (in a bar on the IOMMU PCI device, even though we find the address in the IVRS). We set it up once at boot and don't touch it afterwards.
So while we could make a struct for it, we'd still need to get it into a form that we can writeq(), and that's more code than the single case were we need to put two metadata bits into an address.
~Andrew
Quoted 26 lines · Andrew Cooper
Quoted 25 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
* Use 64bit accesses instead of 32bit accesses
* Simplify the constant names
* Pull base into a local variable to avoid it being reloaded because of the memory clobber inwriteq().No functional change.
Quoted 7 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>RFC. This is my proposed way of cleaning up the whole IOMMU file. The diffstat speaks for itself.
Absolutely.
Quoted 8 lines
I've finally found the bit in the AMD IOMMU spec which says 64bit accesses are permitted:
3.4 IOMMU MMIO Registers:
Software access to IOMMU registers may not be larger than 64 bits. Accesses must be aligned to the size of the access and the size in bytes must be a power of two. Software may use accesses as small as one byte.
Quoted 6 lines
Quoted 3 lines
I take it that the use of 32-bit writes was because of the past need also work in a 32-bit hypervisor, not because of perceived restrictions by the spec.
I recall having problems getting
writeq()acked in the past, even after we'd dropped 32bit.
That's odd, as per my subsequent reply.
Quoted 36 lines
But this is the first time that I've positively found anything in the spec saying that 64bit accesses are ok.
Quoted 24 lines
Quoted 24 lines
xen/drivers/passthrough/amd/iommu-defs.h +2
-16@@ -338,22 +338,10 @@ union amd_iommu_control { };/* Exclusion Register */ -#define IOMMU_EXCLUSION_BASE_LOW_OFFSET 0x20 -#define IOMMU_EXCLUSION_BASE_HIGH_OFFSET 0x24 -#define IOMMU_EXCLUSION_LIMIT_LOW_OFFSET 0x28 -#define IOMMU_EXCLUSION_LIMIT_HIGH_OFFSET 0x2C -#define IOMMU_EXCLUSION_BASE_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_BASE_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_BASE_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_BASE_HIGH_SHIFT 0 -#define IOMMU_EXCLUSION_RANGE_ENABLE_MASK 0x00000001U -#define IOMMU_EXCLUSION_RANGE_ENABLE_SHIFT 0 -#define IOMMU_EXCLUSION_ALLOW_ALL_MASK 0x00000002U -#define IOMMU_EXCLUSION_ALLOW_ALL_SHIFT 1 -#define IOMMU_EXCLUSION_LIMIT_LOW_MASK 0xFFFFF000U -#define IOMMU_EXCLUSION_LIMIT_LOW_SHIFT 12 -#define IOMMU_EXCLUSION_LIMIT_HIGH_MASK 0xFFFFFFFFU -#define IOMMU_EXCLUSION_LIMIT_HIGH_SHIFT 0 +#define IOMMU_MMIO_EXCLUSION_BASE 0x20 +#define EXCLUSION_RANGE_ENABLE (1 << 0)Quoted 8 lines
xen/drivers/passthrough/amd/iommu-defs.h union amd_iommu_control { · line 344
Quoted 2 lines
xen/drivers/passthrough/amd/iommu-defs.h +2
-0+#define EXCLUSION_ALLOW_ALL (1 << 1) +#define IOMMU_MMIO_EXCLUSION_LIMIT 0x28Just one question here: Previously you suggested we switch to bitfields for anything like this, and we've already done so with e.g. union
amd_iommu_controland unionamd_iommu_ext_features. IOW I wonder if we wouldn't better strive to be consistent in this regard. Or if not, what the (written or unwritten) guidelines are when to use which approach.
Quoted 6 lines
We've got two very different kinds of things here.
The device table/etc are in-memory WB datastructure which we're interpreting and editing routinely. It's completely full of bits and small fields, and letting the compiler do the hard work there is preferable; certainly in terms of legibility.
And it was specifically not the DTE I used as example in my reply, ...
Quoted 3 lines
This example is an MMIO register (in a bar on the IOMMU PCI device, even though we find the address in the IVRS). We set it up once at boot and don't touch it afterwards.
... but other MMIO registers.
Quoted 3 lines
So while we could make a struct for it, we'd still need to get it into a form that we can
writeq(), and that's more code than the single case were we need to put two metadata bits into an address.
See those other examples, which are usable with writeq() by way of their "raw" fields.
Jan