tag IOMMU-protected devices in Dom0 fdt
Subsystems (4)
cover tag IOMMU-protected devices in Dom0 fdt
From: Roman Skakun <[email protected]>
At the moment, Dom0 can't distinguish which devices are protected by IOMMU and which are not. In some cases, this can cause swiotlb bounce buffer use for DMA addresses above 32 bits, which in turn can lead to poor performance. I started a conversation at [1], where we discussed addition of a new device tree property to mark IOMMU-protected devices for Dom0.
As a result of negotiation at [1], I would like to present two patches:
1. The first patch for the hypervisor. It adds a new device property 'xen,behind-iommu' to a relevant device node when the device is IOMMU-protected.
2. The second patch is a Linux kernel counterpart. It detects the said property and disables swiotlb for a device.
There is a possible issue: some devices may not be able to use DMA addresses above 32 bit boundaries, so we can have problems in the direct DMA mechanism when swiotlb-xen is disabled for a such device. More generally, this can affect any device which DMA address range is narrower than CPU one.
In this case,
the device DMA address should be no bigger than 32 bit boundaries for each device that is not using swiotlb-xen.
Several ideas on how to overcome it:
1. Do not use high memory for Dom0.
2. Set DMA 32bit mask for each device if swiotlb is not used for this device.
3. Force balloon driver to allocate buffers only below 4GB.
I will be glad to get any comments or suggestions.
Roman Skakun (1):
xen/arm: set iommu property for IOMMU-protected devices
xen/arch/arm/domain_build.c | 7 +++++++ 1 file changed, 7 insertions(+)
Signature · 1 line
2.27.0
RFC 1/1 xen/arm: set iommu property for IOMMU-protected devices
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.
To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
This patch adds mechanism to describe IOMMU-protected devices by adding xen,behind-iommu property to relevant device nodes in Dom0 device tree.
--- xen/arch/arm/domain_build.c | 7 +++++++ 1 file changed, 7 insertions(+)
xen/arch/arm/domain_build.c +7 -0
@@ -580,6 +580,13 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,return res; } +if ( iommu_node &&is_iommu_enabled (d) &&dt_device_is_protected (node) ) + { + res =fdt_property (kinfo->fdt,"xen,behind-iommu" ,NULL ,0 ); +if ( res ) +return res; + } +/* * Override the property "status" to disable the device when it's * marked for passthrough.
Signature · 1 line
2.27.0
RFC PATCH 1/1 dma-mapping: don't set swiotlb-xen fops for IOMMU-protected devices
In reply to Roman SkakunPrior to this patch swiotlb-xen fops are set for all devices when we are booting as as Dom0 or direct-mapped DomU.
Since Xen now marks devices which are IOMMU-protected by adding additional property to the relevant device, we can check for this property and don't set swiotlb-xen fops for any IOMMU-protected device.
--- arch/arm/mm/dma-mapping.c | 2 +- arch/arm/xen/enlighten.c | 4 ++++ arch/arm64/mm/dma-mapping.c | 2 +- include/xen/xen.h | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-)
arch/arm/mm/dma-mapping.c +1 -1
@@ -2280,7 +2280,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,set_dma_ops (dev, dma_ops);#ifdef CONFIG_XEN -if (xen_initial_domain ()) +if (xen_initial_domain () && !xen_is_device_protected (dev)) dev->dma_ops = &xen_swiotlb_dma_ops;#endif dev->archdata.dma_ops_setup = true;
arch/arm/xen/enlighten.c +4 -0
@@ -425,6 +425,10 @@ static int __init xen_pm_init(void) }late_initcall (xen_pm_init); +bool xen_is_device_protected (void *obj) { +struct device *dev = obj; +return of_property_read_bool (dev->of_node,"xen,behind-iommu" ); +}/* empty stubs */ void xen_arch_pre_suspend (void ) { }
arch/arm64/mm/dma-mapping.c +1 -1
@@ -53,7 +53,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,iommu_setup_dma_ops (dev, dma_base, size);#ifdef CONFIG_XEN -if (xen_initial_domain ()) +if (xen_initial_domain () && !xen_is_device_protected (dev)) dev->dma_ops = &xen_swiotlb_dma_ops;#endif }
include/xen/xen.h +2 -0
@@ -61,4 +61,6 @@ void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages);#include <xen/balloon.h> #endif +bool xen_is_device_protected (void *obj); +#endif /* _XEN_XEN_H */
Signature · 1 line
2.27.0
Re: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
In reply to Roman SkakunOn 04.10.21 12:54, Roman Skakun wrote:
Hi Roman
Quoted 5 lines
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.
Quoted 8 lines
To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
This patch adds mechanism to describe IOMMU-protected devices by adding
xen,behind-iommuproperty to relevant device nodes in Dom0 device tree.
I think that new property should be documented probably at
docs/misc/arm/device-tree/...
Quoted 24 lines
Signed-off-by Roman Skakun <[email protected]>--- xen/arch/arm/domain_build.c | 7 +++++++ 1 file changed, 7 insertions(+)xen/arch/arm/domain_build.c +7
-0@@ -580,6 +580,13 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,return res; } +if ( iommu_node &&is_iommu_enabled (d) &&dt_device_is_protected (node) ) + { + res =fdt_property (kinfo->fdt,"xen,behind-iommu" ,NULL ,0 ); +if ( res ) +return res; + } +/* * Override the property "status" to disable the device when it's * marked for passthrough.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
Hi Oleksandr,
Quoted 10 lines
Quoted 10 lines
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
Quoted 7 lines
Quoted 3 lines
This patch adds mechanism to describe IOMMU-protected devices by adding
xen,behind-iommuproperty to relevant device nodes in Dom0 device tree.I think that new property should be documented probably at
docs/misc/arm/device-tree/...
Yes, make sense.
I will add a description for a new property in the next patch series. Thanks!
Cheers,
Roman
ср, 6 окт. 2021 г. в 15:45, Oleksandr <[email protected]>:
Quoted 54 lines · Roman Skakun
Hi Roman
Quoted 5 lines
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.Quoted 8 lines
To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
This patch adds mechanism to describe IOMMU-protected devices by adding
xen,behind-iommuproperty to relevant device nodes in Dom0 device tree.I think that new property should be documented probably at
docs/misc/arm/device-tree/...
Quoted 14 lines
Signed-off-by Roman Skakun <[email protected]>--- xen/arch/arm/domain_build.c | 7 +++++++ 1 file changed, 7 insertions(+)xen/arch/arm/domain_build.c +0
-0@@ -580,6 +580,13 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,return res; }xen/arch/arm/domain_build.c static int __init write_properties(struct domain *d, struct kernel_info *kinfo, · line 592
Quoted 10 lines
xen/arch/arm/domain_build.c +7
-0+if ( iommu_node &&is_iommu_enabled (d) &&dt_device_is_protected (node) ) + { + res =fdt_property (kinfo->fdt,"xen,behind-iommu" ,NULL ,0 ); +if ( res ) +return res; + } +/* * Override the property "status" to disable the device when it's * marked for passthrough. --
Regards,Oleksandr Tyshchenko
Signature · 1 line
Best Regards, Roman.
Hi,
Would you be so kind to look at my patches, it would help me to know exactly whether I'm moving correctly and whether I've chosen the right path?
Thanks!
Best regards,
Roman
пн, 4 окт. 2021 г. в 12:54, Roman Skakun <[email protected]>:
Quoted 47 lines
From: Roman Skakun <[email protected]>
At the moment, Dom0 can't distinguish which devices are protected by IOMMU and which are not. In some cases, this can cause swiotlb bounce buffer use for DMA addresses above 32 bits, which in turn can lead to poor performance. I started a conversation at [1], where we discussed addition of a new device tree property to mark IOMMU-protected devices for Dom0.
As a result of negotiation at [1], I would like to present two patches:
1. The first patch for the hypervisor. It adds a new device property 'xen,behind-iommu' to a relevant device node when the device is IOMMU-protected.
2. The second patch is a Linux kernel counterpart. It detects the said property and disables swiotlb for a device.
There is a possible issue: some devices may not be able to use DMA addresses above 32 bit boundaries, so we can have problems in the direct DMA mechanism when swiotlb-xen is disabled for a such device. More generally, this can affect any device which DMA address range is narrower than CPU one.
In this case,
the device DMA address should be no bigger than 32 bit boundaries for each device that is not using swiotlb-xen.Several ideas on how to overcome it:
1. Do not use high memory for Dom0.
2. Set DMA 32bit mask for each device if swiotlb is not used for this device.
3. Force balloon driver to allocate buffers only below 4GB.I will be glad to get any comments or suggestions.
Roman Skakun (1):
xen/arm: set iommu property for IOMMU-protected devicesxen/arch/arm/domain_build.c | 7 +++++++ 1 file changed, 7 insertions(+)--
2.27.0
Signature · 1 line
Best Regards, Roman.
Quoted 1 line · Roman Skakun
Hi,
Hi Roman,
Quoted 3 lines
Would you be so kind to look at my patches, it would help me to know exactly whether I'm moving correctly and whether I've chosen the right path?
This is in my queue to review. At the moment, I am prioritizing work targeting 4.16. Unfortunately, this RFC was posted after the last posting date. So I will review it after the feature freeze.
Best regards,
Signature · 1 line
Julien Grall
Understood.
Thank you!
вт, 12 окт. 2021 г. в 17:20, Julien Grall <[email protected]>:
Quoted 20 lines · Roman Skakun
Quoted 1 line
Hi,
Hi Roman,
Quoted 3 lines
Would you be so kind to look at my patches, it would help me to know exactly whether I'm moving correctly and whether I've chosen the right path?
This is in my queue to review. At the moment, I am prioritizing work targeting 4.16. Unfortunately, this RFC was posted after the last posting date. So I will review it after the feature freeze.
Best regards,
--
Julien Grall
Signature · 1 line
Best Regards, Roman.
Re: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
In reply to Roman SkakunHi Roman,
Quoted 5 lines · Roman Skakun
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.
Quoted 8 lines
To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
This patch adds mechanism to describe IOMMU-protected devices by adding
xen,behind-iommuproperty to relevant device nodes in Dom0 device tree.
A few years ago, I attempted to disable the swiotlb when Xen configured the IOMMU for the device (see [1]). Did you have a chance to go through the thread? In particular, I think Ian Campbell suggestion about creating an IOMMU binding is quite interesting.
Stefano, what do you think?
Cheers,
[1] https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]/
Signature · 1 line
Julien Grall
Stefano Stabellini
maintainerRe: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
Quoted 17 lines · Julien Grall
Hi Roman,
Quoted 5 lines · Roman Skakun
From: Roman Skakun <[email protected]>
Xen is not exposing any IOMMU properties to Dom0.
So Dom0 assumes that all it's devices are not protected by IOMMU.Quoted 8 lines
To make Dom0 aware of IOMMU-protected devices, we need to mark them somehow. With this approach Dom0 Linux kernel will be able to selectively disable swiotlb-xen fops for them which will remove unnecessary buffer bounces.
This patch adds mechanism to describe IOMMU-protected devices by adding
xen,behind-iommuproperty to relevant device nodes in Dom0 device tree.
Quoted 6 lines
A few years ago, I attempted to disable the swiotlb when Xen configured the IOMMU for the device (see [1]). Did you have a chance to go through the thread? In particular, I think Ian Campbell suggestion about creating an IOMMU binding is quite interesting.
Stefano, what do you think?
Yes I think it is a good idea. In fact, thinking more about it, it is really the best option. Regardless of the implementation (swiotlb or whatever) the device tree description is likely to look similar to the description of an IOMMU because it is the common pattern shared by all controllers (reset, power, clocks, etc.) so it makes sense to re-use it.
- there is one controller node (the "IOMMU")
- there is one property under each device node that is protected, pointing to the controller with a phandle and optional parameters (in the case of IOMMUs it is called "iommus")
Re: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
hi Stefano, Julien,
Quoted 1 line · Stefano Stabellini
On Mon, 8 Nov 2021, Julien Grall wrote:
[..]
Quoted 13 lines
Quoted 6 lines
A few years ago, I attempted to disable the swiotlb when Xen configured the IOMMU for the device (see [1]). Did you have a chance to go through the thread? In particular, I think Ian Campbell suggestion about creating an IOMMU binding is quite interesting.
Stefano, what do you think?
Yes I think it is a good idea. In fact, thinking more about it, it is really the best option. Regardless of the implementation (swiotlb or whatever) the device tree description is likely to look similar to the description of an IOMMU because it is the common pattern shared by all controllers (reset, power, clocks, etc.) so it makes sense to re-use it.
Quoted 5 lines
- there is one controller node (the "IOMMU")
- there is one property under each device node that is protected, pointing to the controller with a phandle and optional parameters (in the case of IOMMUs it is called "iommus")
Code in arch_setup_dma_ops() always forces swiotlb for dom0 regardless of any prior IOMMU configuration for the device. So if we are to re-use IOMMU bindings and implement kind of dummy iommu (that merely does direct allocation and mapping) we'll have to check whether device is protected anyway, e.g.:
diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c
index 49f566ad9acb..6ddef3233095 100644
--- a/arch/arm/xen/enlighten.c
+++ b/arch/arm/xen/enlighten.c
@@ -425,6 +425,10 @@ static int __init xen_pm_init(void)
}
late_initcall(xen_pm_init);
+bool xen_is_device_protected(struct device *dev) {
+ return dev->dma_ops == &dummy_xen_iommu_ops;
+} /* empty stubs */
void xen_arch_pre_suspend(void) { }
Have I got it right?
- Sergiy
Stefano Stabellini
maintainerRe: [RFC 1/1] xen/arm: set iommu property for IOMMU-protected devices
Quoted 20 lines · Sergiy Kibrik
hi Stefano, Julien,
Quoted 1 line · Stefano Stabellini
On Mon, 8 Nov 2021, Julien Grall wrote:
[..]
Quoted 15 lines
Quoted 8 lines
A few years ago, I attempted to disable the swiotlb when Xen configured the
IOMMU for the device (see [1]). Did you have a chance to go through the thread? In particular, I think Ian Campbell suggestion about creating an IOMMU
binding is quite interesting.Stefano, what do you think?
Yes I think it is a good idea. In fact, thinking more about it, it is really the best option. Regardless of the implementation (swiotlb or whatever) the device tree description is likely to look similar to the description of an IOMMU because it is the common pattern shared by all controllers (reset, power, clocks, etc.) so it makes sense to re-use it.
Quoted 8 lines
Quoted 5 lines
- there is one controller node (the "IOMMU")
- there is one property under each device node that is protected, pointing to the controller with a phandle and optional parameters (in the case of IOMMUs it is called "iommus")Code in
arch_setup_dma_ops()always forces swiotlb for dom0 regardless of any prior IOMMU configuration for the device.
Yes. The only exception is cases where XENFEAT_not_direct_mapped is set.
Quoted 3 lines
So if we are to re-use IOMMU bindings and implement kind of dummy iommu (that merely does direct allocation and mapping) we'll have to check whether device is protected anyway
Yes, the Linux-side implementation wouldn't change very much compared to what you had in mind, it is just that the device tree part would be cleaner and more spec compliant.
We would still end up with a property for each to device to specify that is protected by the IOMMU, it is just that instead of "xen,behind-iommu" it would be called "iommus" and it would be pointing to a special "fake" Xen IOMMU node. E.g.:
xen-iommu {
compatible = "xen,iommu-el2-v1";
status = "okay";
#iommu-cells = <0x0>;
};device@ff00000 {
compatible = "vendor,device";
reg = <0xff00000 0x1000>;
iommus = <&xen-iommu>;
};I can imagine it could a be problem to try to comply to the internal iommu API in Linux. The Linux driver for xen-iommu could be tiny. It doesn't have to implement the Linux iommu API because struct iommu_ops is massive.
Linux would still have to check for each device if it is protected by the iommu, but the "iommus" property is parsed automatically by drivers/of/property.c:of_link_property. It should make the Linux side easier to implement. of_link_property creates a fwnode "link" between device@ff00000 and xen-iommu automatically for you, then I think you can just call fwnode_find_reference to check if device@ff00000 is behind an IOMMU.
Quoted 14 lines
e.g.:
diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c index 49f566ad9acb..6ddef3233095 100644 --- a/arch/arm/xen/enlighten.c +++ b/arch/arm/xen/enlighten.c @@ -425,6 +425,10 @@ static int __init xen_pm_init(void) } late_initcall(xen_pm_init); +bool xen_is_device_protected(struct device *dev) { + return dev->dma_ops == &dummy_xen_iommu_ops; +}
Quoted 5 lines
/* empty stubs */ void xen_arch_pre_suspend(void) { }Have I got it right?
I don't think I understand this example