XEN SWIOTLB for ARM dma operations extension
Subsystems (2)
v2 cover XEN SWIOTLB for ARM dma operations extension
From: Andrii Anisov <[email protected]>
Some drivers need additional dma ops to be provided by xen swiotlb. Because common operations implementation came from x86 world and does not suite ARM needs we have to provide needed XEN SWIOTLB for ARM callbacks.
dma_mmap patch is a port of an antique and lost patch discussed here:
http://marc.info/?l=xen-devel&m=139695901430667&w=4
Changes in V2:
- patches are rebased and checked for compilation for x86, arm, arm64 with the current LK master HEAD 83346fbc07d267de777e2597552f785174ad0373
Andrii Anisov (1):
swiotlb-xen: implement xen_swiotlb_get_sgtable callback
Stefano Stabellini (1):
swiotlb-xen: implement xen_swiotlb_dma_mmap callback
arch/arm/xen/mm.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v2 1/2 swiotlb-xen: implement xen_swiotlb_dma_mmap callback
From: Stefano Stabellini <[email protected]>
This function creates userspace mapping for the DMA-coherent memory.
--- arch/arm/xen/mm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/arm/xen/mm.c +14 -0
@@ -163,6 +163,19 @@ bool xen_arch_need_swiotlb(struct device *dev, !is_device_dma_coherent(dev)); } +/* +* Create userspace mapping for the DMA-coherent memory. +*/ +static int xen_swiotlb_dma_mmap (struct device *dev,struct vm_area_struct *vma, +void *cpu_addr,dma_addr_t dma_addr,size_t size, +unsigned long attrs) +{ +if (__generic_dma_ops (dev)->mmap) +return __generic_dma_ops (dev)->mmap (dev, vma, cpu_addr, dma_addr, size, attrs); + +return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); +} +int xen_create_contiguous_region (phys_addr_t pstart,unsigned int order,unsigned int address_bits,dma_addr_t *dma_handle)
@@ -198,6 +211,7 @@ static struct dma_map_ops xen_swiotlb_dma_ops = { .unmap_page = xen_swiotlb_unmap_page, .dma_supported = xen_swiotlb_dma_supported, .set_dma_mask = xen_swiotlb_set_dma_mask, + .mmap = xen_swiotlb_dma_mmap, };int __init xen_mm_init (void )
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v2 2/2 swiotlb-xen: implement xen_swiotlb_get_sgtable callback
In reply to Andrii AnisovFrom: Andrii Anisov <[email protected]>
--- arch/arm/xen/mm.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/arm/xen/mm.c +11 -0
@@ -176,6 +176,16 @@ static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma,return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); } +static int xen_swiotlb_get_sgtable (struct device *dev,struct sg_table *sgt, +void *cpu_addr,dma_addr_t handle,size_t size, +unsigned long attrs) +{ +if (__generic_dma_ops (dev)->get_sgtable) +return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, + size, attrs); +return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); +} +int xen_create_contiguous_region (phys_addr_t pstart,unsigned int order,unsigned int address_bits,dma_addr_t *dma_handle)
@@ -212,6 +222,7 @@ static struct dma_map_ops xen_swiotlb_dma_ops = { .dma_supported = xen_swiotlb_dma_supported, .set_dma_mask = xen_swiotlb_set_dma_mask, .mmap = xen_swiotlb_dma_mmap, + .get_sgtable = xen_swiotlb_get_sgtable, };int __init xen_mm_init (void )
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Stefano Stabellini
maintainerRe: [PATCH v2 2/2] swiotlb-xen: implement xen_swiotlb_get_sgtable callback
Quoted 3 lines · Andrii Anisov
From: Andrii Anisov <[email protected]>
Signed-off-by Andrii Anisov <[email protected]>
Thanks for the patch!
arch/arm/xen/mm.c static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 177
Quoted 10 lines
arch/arm/xen/mm.c | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/arm/xen/mm.c +0
-0@@ -176,6 +176,16 @@ static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma,return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); }
As for the other patch, I would move xen_swiotlb_get_sgtable to drivers/xen/swiotlb-xen.c, if Konrad agrees.
arch/arm/xen/mm.c static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 187
Quoted 10 lines
arch/arm/xen/mm.c +10
-0+static int xen_swiotlb_get_sgtable (struct device *dev,struct sg_table *sgt, +void *cpu_addr,dma_addr_t handle,size_t size, +unsigned long attrs) +{ +if (__generic_dma_ops (dev)->get_sgtable) +return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, + size, attrs); +return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); +} +
__generic_dma_ops(dev)->get_sgtable on ARM is implemented by arm_dma_get_sgtable, which doesn't work on foreign pages (pages for which bfn != pfn).
If get_sgtable is guaranteed to be always called passing references to pages previously allocated with dma_alloc_coherent, then we don't have any issues, because those can't be foreign pages. I suggest we add an in-code comment to explain why this is safe, as for the previous patch. I think this is the case, but I am not 100% sure.
On the other hand, if this function can be called passing as parameters cpu_addr and handle that could potentially refer to a foreign page, then we have a problem. On ARM, virt_to_phys doesn't work on some pages, in fact that is the reason why ARM has its own separate get_sgtable implementation (arm_dma_get_sgtable). But with Xen foreign pages, dma_to_pfn doesn't work either, because we have no way of finding out the pfn address corresponding to the mfn of the foreign page. Both arm_dma_get_sgtable and dma_common_get_sgtable wouldn't work. I have no solution to this problem, but maybe we could add a check like the following (also to the previous patch?). I haven't tested it, but I think it should work as long as page_is_ram is returns the correct value for the handle parameter.
arch/arm/xen/mm.c +11 -1
@@ -18,6 +18,7 @@#include <xen/page.h> #include <xen/swiotlb-xen.h> +#include <asm/dma-mapping.h> #include <asm/cacheflush.h> #include <asm/xen/hypercall.h> #include <asm/xen/interface.h>
@@ -180,9 +181,18 @@ static int xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t handle, size_t size, unsigned long attrs) { - if (__generic_dma_ops(dev)->get_sgtable) + + if (__generic_dma_ops(dev)->get_sgtable) { + /* We can't handle foreign pages here. */ +#ifdef CONFIG_ARM + unsigned long bfn = dma_to_pfn(dev, handle); +#else +unsigned long bfn = handle >>PAGE_SHIFT ; +#endif +BUG_ON (!page_is_ram (bfn));return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, size, attrs); + }return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); }
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/2] swiotlb-xen: implement xen_swiotlb_get_sgtable callback
Quoted 11 lines · Stefano Stabellini
Quoted 3 lines · Andrii Anisov
From: Andrii Anisov <[email protected]>
Signed-off-by Andrii Anisov <[email protected]>Thanks for the patch!
Quoted 3 lines
arch/arm/xen/mm.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
Quoted 10 lines
arch/arm/xen/mm.c static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 177
Quoted 7 lines
arch/arm/xen/mm.c +0
-0@@ -176,6 +176,16 @@ static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma,return dma_common_mmap (dev, vma, cpu_addr, dma_addr, size); }As for the other patch, I would move
xen_swiotlb_get_sgtabletodrivers/xen/swiotlb-xen.c, if Konrad agrees.
That is fine.
Quoted 71 lines
arch/arm/xen/mm.c static int xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 187
Quoted 12 lines
arch/arm/xen/mm.c +10
-0+static int xen_swiotlb_get_sgtable (struct device *dev,struct sg_table *sgt, +void *cpu_addr,dma_addr_t handle,size_t size, +unsigned long attrs) +{ +if (__generic_dma_ops (dev)->get_sgtable) +return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, + size, attrs); +return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); +} +__generic_dma_ops(dev)->get_sgtable on ARM is implemented by
arm_dma_get_sgtable, which doesn't work on foreign pages (pages for which bfn != pfn).If
get_sgtableis guaranteed to be always called passing references to pages previously allocated withdma_alloc_coherent, then we don't have any issues, because those can't be foreign pages. I suggest we add an in-code comment to explain why this is safe, as for the previous patch. I think this is the case, but I am not 100% sure.On the other hand, if this function can be called passing as parameters
cpu_addrand handle that could potentially refer to a foreign page, then we have a problem. On ARM,virt_to_physdoesn't work on some pages, in fact that is the reason why ARM has its own separateget_sgtableimplementation (arm_dma_get_sgtable). But with Xen foreign pages,dma_to_pfndoesn't work either, because we have no way of finding out the pfn address corresponding to the mfn of the foreign page. Botharm_dma_get_sgtableanddma_common_get_sgtablewouldn't work. I have no solution to this problem, but maybe we could add a check like the following (also to the previous patch?). I haven't tested it, but I think it should work as long aspage_is_ramis returns the correct value for the handle parameter.Signed-off-by Stefano Stabellini <[email protected]>arch/arm/xen/mm.c +11
-1@@ -18,6 +18,7 @@#include <xen/page.h> #include <xen/swiotlb-xen.h> +#include <asm/dma-mapping.h> #include <asm/cacheflush.h> #include <asm/xen/hypercall.h> #include <asm/xen/interface.h> @@ -180,9 +181,18 @@ static int xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t handle, size_t size, unsigned long attrs) { - if (__generic_dma_ops(dev)->get_sgtable) + + if (__generic_dma_ops(dev)->get_sgtable) { + /* We can't handle foreign pages here. */ +#ifdef CONFIG_ARM + unsigned long bfn = dma_to_pfn(dev, handle); +#else +unsigned long bfn = handle >>PAGE_SHIFT ; +#endif +BUG_ON (!page_is_ram (bfn));return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, size, attrs); + }return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); }
Quoted 4 lines
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/2] swiotlb-xen: implement xen_swiotlb_get_sgtable callback
In reply to Stefano StabelliniStefano,
About this piece:
Quoted 6 lines
arch/arm/xen/mm.c +4
-1-if (__generic_dma_ops (dev)->get_sgtable) + +if (__generic_dma_ops (dev)->get_sgtable) { +/* We can't handle foreign pages here. */ +#ifdef CONFIG_ARM
arch/arm/xen/mm.c static int xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, · line 197
Quoted 10 lines
arch/arm/xen/mm.c +6
-0+unsigned long bfn =dma_to_pfn (dev, handle); +#else +unsigned long bfn = handle >>PAGE_SHIFT ; +#endif +BUG_ON (!page_is_ram (bfn));return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, size, attrs); + }return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); }
Would it be in drivers/xen/swiotlb-xen.c as you suggested, the whole "if (__generic_dma_ops(dev)->get_sgtable) {}" should be under ifdef.
IMO it would be better to avoid ifdefs in drivers/xen/swiotlb-xen.c, but I haven't find out how to do that.
Sincerely,
Andrii Anisov.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Stefano Stabellini
maintainerRe: [PATCH v2 2/2] swiotlb-xen: implement xen_swiotlb_get_sgtable callback
Quoted 25 lines · Andrii Anisov
Stefano,
About this piece:
Quoted 6 lines
arch/arm/xen/mm.c +4
-1-if (__generic_dma_ops (dev)->get_sgtable) + +if (__generic_dma_ops (dev)->get_sgtable) { +/* We can't handle foreign pages here. */ +#ifdef CONFIG_ARM arch/arm/xen/mm.c static int xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt, · line 197
Quoted 10 lines
arch/arm/xen/mm.c +6
-0+unsigned long bfn =dma_to_pfn (dev, handle); +#else +unsigned long bfn = handle >>PAGE_SHIFT ; +#endif +BUG_ON (!page_is_ram (bfn));return __generic_dma_ops (dev)->get_sgtable (dev, sgt, cpu_addr, handle, size, attrs); + }return dma_common_get_sgtable (dev, sgt, cpu_addr, handle, size); }Would it be in
drivers/xen/swiotlb-xen.cas you suggested, the whole "if (__generic_dma_ops(dev)->get_sgtable) {}" should be under ifdef.
Quoted 2 lines
IMO it would be better to avoid ifdefs in
drivers/xen/swiotlb-xen.c, but I haven't find out how to do that.
Yes, I dislike ifdef like everybody else, but sometimes they are unavoidable.
But please test this code because I only compile tested it :-)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel