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
Stefano Stabellini
maintainerRe: [PATCH v2 1/2] swiotlb-xen: implement xen_swiotlb_dma_mmap callback
Quoted 36 lines · Andrii Anisov
From: Stefano Stabellini <[email protected]>
This function creates userspace mapping for the DMA-coherent memory.
Signed-off-by Stefano Stabellini <[email protected]>Signed-off-by Oleksandr Dmytryshyn <[email protected]>Signed-off-by Andrii Anisov <[email protected]>--- arch/arm/xen/mm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)arch/arm/xen/mm.c +13
-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)
arch/arm/xen/mm.c static struct dma_map_ops xen_swiotlb_dma_ops = { · line 217
Quoted 7 lines
arch/arm/xen/mm.c +1
-0@@ -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 )
The patch should work fine and looks OK. It is better written like this, compared to the previous versions that reimplemented dma_common_mmap. I like the fact that we are reusing the arm specific generic mmap functions via __generic_dma_ops.
For consistency, I would prefer to have xen_swiotlb_dma_mmap in drivers/xen/swiotlb-xen.c, even if it needs to be #ifdef'ed CONFIG_ARM (at least the __generic_dma_ops calls need to be #ifdef'ed).
Konrad, what do you think?
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Stefano Stabellini
maintainerRe: [PATCH v2 1/2] swiotlb-xen: implement xen_swiotlb_dma_mmap callback
Quoted 20 lines · Stefano Stabellini
Quoted 19 lines · Andrii Anisov
From: Stefano Stabellini <[email protected]>
This function creates userspace mapping for the DMA-coherent memory.
Signed-off-by Stefano Stabellini <[email protected]>Signed-off-by Oleksandr Dmytryshyn <[email protected]>Signed-off-by Andrii Anisov <[email protected]>--- arch/arm/xen/mm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)arch/arm/xen/mm.c +0
-0@@ -163,6 +163,19 @@ bool xen_arch_need_swiotlb(struct device *dev, !is_device_dma_coherent(dev)); }
Quoted 7 lines
arch/arm/xen/mm.c bool xen_arch_need_swiotlb(struct device *dev, · line 172
Quoted 7 lines
arch/arm/xen/mm.c +7
-0+/* +* 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) +{
Only one suggestion more. For this to work correctly, we are assuming that no foreging pages are involved here, which is a very reasonable assumption given that mmap should be called on memory returned by dma_alloc_coherent. Please add an in-code comment here so that we'll remember.
Quoted 23 lines
Quoted 10 lines
arch/arm/xen/mm.c +6
-0+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)arch/arm/xen/mm.c static struct dma_map_ops xen_swiotlb_dma_ops = { · line 217
Quoted 7 lines
arch/arm/xen/mm.c +1
-0@@ -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 )The patch should work fine and looks OK. It is better written like this, compared to the previous versions that reimplemented
dma_common_mmap. I like the fact that we are reusing the arm specific generic mmap functions via__generic_dma_ops.
Quoted 6 lines
For consistency, I would prefer to have
xen_swiotlb_dma_mmapindrivers/xen/swiotlb-xen.c, even if it needs to be #ifdef'edCONFIG_ARM(at least the__generic_dma_opscalls need to be #ifdef'ed).Konrad, what do you think?
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] swiotlb-xen: implement xen_swiotlb_dma_mmap callback
Dear Stefano,
Quoted 4 lines
Only one suggestion more. For this to work correctly, we are assuming that no foreging pages are involved here, which is a very reasonable assumption given that mmap should be called on memory returned by
dma_alloc_coherent.
I also kept in mind this problem, that's why the first version was RFC.
Quoted 1 line
Please add an in-code comment here so that we'll remember.
Do you think comment would be enough so far?
Maybe fallback to common ops would be better in order to keep current (even broken) functionality for now? Or BUG_ON as you suggested for get_sgtable callback?
ANDRII ANISOV
Lead Systems Engineer
Office: +380 44 390 5457<tel:+380%2044%20390%205457> x 66766<tel:66766> Cell: +380 50 573 8852<tel:+380%2050%20573%208852> Email: [email protected]<mailto:[email protected]>
Kyiv, Ukraine (GMT+3) epam.com<http://www.epam.com>
CONFIDENTIALITY CAUTION AND DISCLAIMER
This message is intended only for the use of the individual(s) or entity(ies) to which it is addressed and contains information that is legally privileged and confidential. If you are not the intended recipient, or the person responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. All unintended recipients are obliged to delete this message and destroy any printed copies.
________________________________
From: Stefano Stabellini <[email protected]>
Sent: Tuesday, January 17, 2017 12:56:12 AM
To: Stefano Stabellini
Cc: Andrii Anisov; [email protected]; Andrii Anisov; [email protected]; [email protected]; Stefano Stabellini; Oleksandr Dmytryshyn
Subject: Re: [PATCH v2 1/2] swiotlb-xen: implement xen_swiotlb_dma_mmap callback
Quoted 20 lines · Stefano Stabellini
Quoted 19 lines · Andrii Anisov
From: Stefano Stabellini <[email protected]>
This function creates userspace mapping for the DMA-coherent memory.
Signed-off-by Stefano Stabellini <[email protected]>Signed-off-by Oleksandr Dmytryshyn <[email protected]>Signed-off-by Andrii Anisov <[email protected]>--- arch/arm/xen/mm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)arch/arm/xen/mm.c +0
-0@@ -163,6 +163,19 @@ bool xen_arch_need_swiotlb(struct device *dev, !is_device_dma_coherent(dev)); }
Quoted 7 lines
arch/arm/xen/mm.c bool xen_arch_need_swiotlb(struct device *dev, · line 172
Quoted 7 lines
arch/arm/xen/mm.c +7
-0+/* +* 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) +{
Only one suggestion more. For this to work correctly, we are assuming that no foreging pages are involved here, which is a very reasonable assumption given that mmap should be called on memory returned by dma_alloc_coherent. Please add an in-code comment here so that we'll remember.
Quoted 23 lines
Quoted 10 lines
arch/arm/xen/mm.c +6
-0+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)arch/arm/xen/mm.c static struct dma_map_ops xen_swiotlb_dma_ops = { · line 217
Quoted 7 lines
arch/arm/xen/mm.c +1
-0@@ -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 )The patch should work fine and looks OK. It is better written like this, compared to the previous versions that reimplemented
dma_common_mmap. I like the fact that we are reusing the arm specific generic mmap functions via__generic_dma_ops.
Quoted 6 lines
For consistency, I would prefer to have
xen_swiotlb_dma_mmapindrivers/xen/swiotlb-xen.c, even if it needs to be #ifdef'edCONFIG_ARM(at least the__generic_dma_opscalls need to be #ifdef'ed).Konrad, what do you think?
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Stefano Stabellini
maintainerRe: [PATCH v2 1/2] swiotlb-xen: implement xen_swiotlb_dma_mmap callback
Quoted 8 lines · Andrii Anisov
Dear Stefano,
Quoted 4 lines
Only one suggestion more. For this to work correctly, we are assuming that no foreging pages are involved here, which is a very reasonable assumption given that mmap should be called on memory returned by
dma_alloc_coherent.
Quoted 5 lines
I also kept in mind this problem, that's why the first version was RFC.
Quoted 1 line
Please add an in-code comment here so that we'll remember.
Do you think comment would be enough so far?
A comment is enough in the case of xen_swiotlb_dma_mmap, because we are sure that the function can only be called with local pages. See the comment above dma_mmap_attrs:
* Map a coherent DMA buffer previously allocated by dma_alloc_attrs
* into user space. The coherent DMA buffer must not be freed by the
* driver until the user space mapping has been released.
If the page must comes from dma_alloc_coherent, then we are safe.
I wasn't sure about dma_get_sgtable_attrs, because there is no in-tree description, but looking at git log:
commit d2b7428eb0caa7c66e34b6ac869a43915b294123
Author: Marek Szyprowski <[email protected]>
Date: Wed Jun 13 10:05:52 2012 +0200
common: dma-mapping: introduce dma_get_sgtable() function
This patch adds dma_get_sgtable() function which is required to let drivers to share the buffers allocated by DMA-mapping subsystem. Right
It looks like dma_get_sgtable is also supposed to be called on buffers returned by dma_alloc_coherent. We should be safe in both cases.
Quoted 2 lines
Maybe fallback to common ops would be better in order to keep current (even broken) functionality for now? Or
BUG_ONas you suggested forget_sgtablecallback?
BUG_ON is good because it is an obvious failure for a case we don't know how to handle. If it actually works as expected, we could add it to both functions anyway, surrounded by #ifdef DEBUG_DRIVER not to slow down the common case.
_______________________________________________
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