Stefano Stabellini
2013-Aug-05 16:29 UTC
[PATCH v3 0/10] enable swiotlb-xen on arm and arm64
Hi all, this patch series enables xen-swiotlb on arm and arm64. Considering that all guests, including dom0, run on xen on arm with second stage translation enabled, it follows that without an IOMMU no guests could actually drive the hardware. The solution for platforms without an IOMMU is to use swiotlb-xen, adapted to autotranslate guests. swiotlb-xen provides a set of dma_ops that can be used by Linux to setup a contiguous buffer in stage-2 addresses and use it for dma operations. Basically Linux asks Xen to make a buffer contiguous and gets the machine address for it. This buffer is going to be used by lib/swiotlb.c to allocate bounce buffers. The first 5 patches lay the groundwork on arm and arm64 to have alternative dma_ops and swiotlb. The sixth patch moves Xen initialization earlier so that we already know whether we are running on Xen at the time of initializing dma_ops on the platform. The following patches adapt swiotlb-xen to autotranslate guests (guest with second stage translation in hardware) and provide an arm implementation of xen_create_contiguous_region. Feedback is very welcome. Cheers, Stefano Changes in v3: - add a patch to compile SWIOTLB without CONFIG_NEED_SG_DMA_LENGTH; - add a patch to compile SWIOTLB_XEN without CONFIG_NEED_SG_DMA_LENGTH; - arm/dma_capable: do not treat dma_mask as a limit; - arm/dmabounce: keep using arm_dma_ops; - add missing __init in xen_early_init declaration; - many code style and name changes in swiotlb-xen.c; - improve error checks in xen_dma_add_entry; - warn on XENMEM_put_dma_buf failures. Changes in v2: - fixed a couple of errors in xen_bus_to_phys, xen_phys_to_bus and xen_swiotlb_fixup. EUNBONG SONG (1): swiotlb: replace dma_length with sg_dma_len() macro Stefano Stabellini (9): swiotlb-xen: replace dma_length with sg_dma_len() macro arm: make SWIOTLB available arm: introduce a global dma_ops pointer arm64: do not initialize arm64_swiotlb if dma_ops is already set xen/arm,arm64: move Xen initialization earlier xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf xen: make xen_create_contiguous_region return the dma address swiotlb-xen: support autotranslate guests xen/arm,arm64: enable SWIOTLB_XEN arch/arm/Kconfig | 7 ++ arch/arm/include/asm/dma-mapping.h | 33 ++++++- arch/arm/include/asm/xen/hypervisor.h | 8 ++ arch/arm/include/asm/xen/page.h | 2 + arch/arm/kernel/setup.c | 2 + arch/arm/mm/dma-mapping.c | 3 + arch/arm/xen/Makefile | 2 +- arch/arm/xen/enlighten.c | 24 +++-- arch/arm/xen/mm.c | 117 ++++++++++++++++++++ arch/arm64/Kconfig | 1 + arch/arm64/kernel/setup.c | 2 + arch/arm64/mm/dma-mapping.c | 2 + arch/arm64/xen/Makefile | 2 +- arch/x86/xen/mmu.c | 4 +- drivers/xen/Kconfig | 1 - drivers/xen/swiotlb-xen.c | 188 +++++++++++++++++++++++++++++---- include/xen/interface/memory.h | 62 +++++++++++ include/xen/xen-ops.h | 3 +- lib/swiotlb.c | 8 +- 19 files changed, 433 insertions(+), 38 deletions(-) create mode 100644 arch/arm/xen/mm.c git://git.kernel.org/pub/scm/linux/kernel/git/sstabellini/xen.git swiotlb-xen-3
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 01/10] swiotlb: replace dma_length with sg_dma_len() macro
From: EUNBONG SONG <eunb.song@samsung.com> This patch replace dma_length in "lib/swiotlb.c" to sg_dma_len() macro, because the build error can occur if CONFIG_NEED_SG_DMA_LENGTH is not set, and CONFIG_SWIOTLB is set. Singed-off-by: EunBong Song <eunb.song@samsung.com> Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> --- lib/swiotlb.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/swiotlb.c b/lib/swiotlb.c index d23762e..4e8686c 100644 --- a/lib/swiotlb.c +++ b/lib/swiotlb.c @@ -870,13 +870,13 @@ swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems, swiotlb_full(hwdev, sg->length, dir, 0); swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir, attrs); - sgl[0].dma_length = 0; + sg_dma_len(sgl) = 0; return 0; } sg->dma_address = phys_to_dma(hwdev, map); } else sg->dma_address = dev_addr; - sg->dma_length = sg->length; + sg_dma_len(sg) = sg->length; } return nelems; } @@ -904,7 +904,7 @@ swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, BUG_ON(dir == DMA_NONE); for_each_sg(sgl, sg, nelems, i) - unmap_single(hwdev, sg->dma_address, sg->dma_length, dir); + unmap_single(hwdev, sg->dma_address, sg_dma_len(sg), dir); } EXPORT_SYMBOL(swiotlb_unmap_sg_attrs); @@ -934,7 +934,7 @@ swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) swiotlb_sync_single(hwdev, sg->dma_address, - sg->dma_length, dir, target); + sg_dma_len(sg), dir, target); } void -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 02/10] swiotlb-xen: replace dma_length with sg_dma_len() macro
swiotlb-xen has an implicit dependency on CONFIG_NEED_SG_DMA_LENGTH. Remove it by replacing dma_length with sg_dma_len. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> --- drivers/xen/swiotlb-xen.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index aadffcf..1b2277c 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -506,13 +506,13 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, to do proper error handling. */ xen_swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir, attrs); - sgl[0].dma_length = 0; + sg_dma_len(sgl) = 0; return DMA_ERROR_CODE; } sg->dma_address = xen_phys_to_bus(map); } else sg->dma_address = dev_addr; - sg->dma_length = sg->length; + sg_dma_len(sg) = sg->length; } return nelems; } @@ -533,7 +533,7 @@ xen_swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, BUG_ON(dir == DMA_NONE); for_each_sg(sgl, sg, nelems, i) - xen_unmap_single(hwdev, sg->dma_address, sg->dma_length, dir); + xen_unmap_single(hwdev, sg->dma_address, sg_dma_len(sg), dir); } EXPORT_SYMBOL_GPL(xen_swiotlb_unmap_sg_attrs); @@ -555,7 +555,7 @@ xen_swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, for_each_sg(sgl, sg, nelems, i) xen_swiotlb_sync_single(hwdev, sg->dma_address, - sg->dma_length, dir, target); + sg_dma_len(sg), dir, target); } void -- 1.7.2.5
IOMMU_HELPER is needed because SWIOTLB calls iommu_is_span_boundary, provided by lib/iommu_helper.c. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> CC: will.deacon@arm.com CC: linux@arm.linux.org.uk Changes in v3: - dma_capable: do not treat dma_mask as a limit; - remove SWIOTLB dependency on NEED_SG_DMA_LENGTH. --- arch/arm/Kconfig | 6 ++++++ arch/arm/include/asm/dma-mapping.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 0 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index ba412e0..c0bfb33 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1832,6 +1832,12 @@ config CC_STACKPROTECTOR neutralized via a kernel panic. This feature requires gcc version 4.2 or above. +config SWIOTLB + def_bool y + +config IOMMU_HELPER + def_bool SWIOTLB + config XEN_DOM0 def_bool y depends on XEN diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h index 5b579b9..5b8eef9 100644 --- a/arch/arm/include/asm/dma-mapping.h +++ b/arch/arm/include/asm/dma-mapping.h @@ -86,6 +86,36 @@ static inline dma_addr_t virt_to_dma(struct device *dev, void *addr) } #endif +static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) +{ + unsigned int offset = paddr & ~PAGE_MASK; + return pfn_to_dma(dev, paddr >> PAGE_SHIFT) + offset; +} + +static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) +{ + unsigned int offset = dev_addr & ~PAGE_MASK; + return (dma_to_pfn(dev, dev_addr) << PAGE_SHIFT) + offset; +} + +static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) +{ + u64 limit, mask = *dev->dma_mask; + + limit = (mask + 1) & ~mask; + if (limit && size > limit) + return 0; + + if ((addr | (addr + size - 1)) & ~mask) + return 0; + + return 1; +} + +static inline void dma_mark_clean(void *addr, size_t size) +{ +} + /* * DMA errors are defined by all-bits-set in the DMA address. */ -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 04/10] arm: introduce a global dma_ops pointer
Initially set dma_ops to arm_dma_ops. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> CC: will.deacon@arm.com CC: linux@arm.linux.org.uk Changes in v3: - keep using arm_dma_ops in dmabounce. --- arch/arm/include/asm/dma-mapping.h | 3 ++- arch/arm/mm/dma-mapping.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletions(-) diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h index 5b8eef9..5c130bc 100644 --- a/arch/arm/include/asm/dma-mapping.h +++ b/arch/arm/include/asm/dma-mapping.h @@ -12,6 +12,7 @@ #include <asm/memory.h> #define DMA_ERROR_CODE (~0) +extern struct dma_map_ops *dma_ops; extern struct dma_map_ops arm_dma_ops; extern struct dma_map_ops arm_coherent_dma_ops; @@ -19,7 +20,7 @@ static inline struct dma_map_ops *get_dma_ops(struct device *dev) { if (dev && dev->archdata.dma_ops) return dev->archdata.dma_ops; - return &arm_dma_ops; + return dma_ops; } static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 7f9b179..870b12c 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -141,6 +141,9 @@ struct dma_map_ops arm_dma_ops = { }; EXPORT_SYMBOL(arm_dma_ops); +struct dma_map_ops *dma_ops = &arm_dma_ops; +EXPORT_SYMBOL(dma_ops); + static void *arm_coherent_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs); static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr, -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 05/10] arm64: do not initialize arm64_swiotlb if dma_ops is already set
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> CC: catalin.marinas@arm.com CC: will.deacon@arm.com --- arch/arm64/mm/dma-mapping.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index 4bd7579..a006e84 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -63,6 +63,8 @@ static struct dma_map_ops arm64_swiotlb_dma_ops = { void __init arm64_swiotlb_init(void) { + if (dma_ops != NULL) + return; dma_ops = &arm64_swiotlb_dma_ops; swiotlb_init(1); } -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 06/10] xen/arm,arm64: move Xen initialization earlier
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Changes in v3: - add missing __init in xen_early_init declaration. --- arch/arm/include/asm/xen/hypervisor.h | 8 ++++++++ arch/arm/kernel/setup.c | 2 ++ arch/arm/xen/enlighten.c | 21 ++++++++++++++------- arch/arm64/kernel/setup.c | 2 ++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/arch/arm/include/asm/xen/hypervisor.h b/arch/arm/include/asm/xen/hypervisor.h index d7ab99a..2782591 100644 --- a/arch/arm/include/asm/xen/hypervisor.h +++ b/arch/arm/include/asm/xen/hypervisor.h @@ -1,6 +1,8 @@ #ifndef _ASM_ARM_XEN_HYPERVISOR_H #define _ASM_ARM_XEN_HYPERVISOR_H +#include <linux/init.h> + extern struct shared_info *HYPERVISOR_shared_info; extern struct start_info *xen_start_info; @@ -16,4 +18,10 @@ static inline enum paravirt_lazy_mode paravirt_get_lazy_mode(void) return PARAVIRT_LAZY_NONE; } +#ifdef CONFIG_XEN +void __init xen_early_init(void); +#else +static inline void xen_early_init(void) { return; } +#endif + #endif /* _ASM_ARM_XEN_HYPERVISOR_H */ diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c index 63af9a7..cb7b8e2 100644 --- a/arch/arm/kernel/setup.c +++ b/arch/arm/kernel/setup.c @@ -45,6 +45,7 @@ #include <asm/cacheflush.h> #include <asm/cachetype.h> #include <asm/tlbflush.h> +#include <asm/xen/hypervisor.h> #include <asm/prom.h> #include <asm/mach/arch.h> @@ -889,6 +890,7 @@ void __init setup_arch(char **cmdline_p) arm_dt_init_cpu_maps(); psci_init(); + xen_early_init(); #ifdef CONFIG_SMP if (is_smp()) { if (!mdesc->smp_init || !mdesc->smp_init()) { diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c index c9770ba..14d17ab 100644 --- a/arch/arm/xen/enlighten.c +++ b/arch/arm/xen/enlighten.c @@ -195,21 +195,19 @@ static void xen_power_off(void) * documentation of the Xen Device Tree format. */ #define GRANT_TABLE_PHYSADDR 0 -static int __init xen_guest_init(void) +void __init xen_early_init(void) { - struct xen_add_to_physmap xatp; - static struct shared_info *shared_info_page = 0; + struct resource res; struct device_node *node; int len; const char *s = NULL; const char *version = NULL; const char *xen_prefix = "xen,xen-"; - struct resource res; node = of_find_compatible_node(NULL, NULL, "xen,xen"); if (!node) { pr_debug("No Xen support\n"); - return 0; + return; } s = of_get_property(node, "compatible", &len); if (strlen(xen_prefix) + 3 < len && @@ -217,10 +215,10 @@ static int __init xen_guest_init(void) version = s + strlen(xen_prefix); if (version == NULL) { pr_debug("Xen version not found\n"); - return 0; + return; } if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res)) - return 0; + return; xen_hvm_resume_frames = res.start >> PAGE_SHIFT; xen_events_irq = irq_of_parse_and_map(node, 0); pr_info("Xen %s support found, events_irq=%d gnttab_frame_pfn=%lx\n", @@ -232,6 +230,15 @@ static int __init xen_guest_init(void) xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED; else xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED); +} + +static int __init xen_guest_init(void) +{ + struct xen_add_to_physmap xatp; + static struct shared_info *shared_info_page = 0; + + if (!xen_domain()) + return 0; if (!shared_info_page) shared_info_page = (struct shared_info *) diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index add6ea6..e0d438a 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -53,6 +53,7 @@ #include <asm/traps.h> #include <asm/memblock.h> #include <asm/psci.h> +#include <asm/xen/hypervisor.h> unsigned int processor_id; EXPORT_SYMBOL(processor_id); @@ -267,6 +268,7 @@ void __init setup_arch(char **cmdline_p) unflatten_device_tree(); psci_init(); + xen_early_init(); cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK; #ifdef CONFIG_SMP -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 07/10] xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf
XENMEM_exchange can''t be used by autotranslate guests because of two severe limitations: - it does not copy back the mfns into the out field for autotranslate guests; - it does not guarantee that the hypervisor won''t change the p2m mappings for the exchanged pages while the guest is using them. Xen never promises to keep the p2m mapping stable for autotranslate guests in general. In practice it won''t happen unless one uses uncommon features like memory sharing or paging. To overcome these problems I am introducing two new hypercalls. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> --- include/xen/interface/memory.h | 62 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 0 deletions(-) diff --git a/include/xen/interface/memory.h b/include/xen/interface/memory.h index 2ecfe4f..ffd7f4e 100644 --- a/include/xen/interface/memory.h +++ b/include/xen/interface/memory.h @@ -263,4 +263,66 @@ struct xen_remove_from_physmap { }; DEFINE_GUEST_HANDLE_STRUCT(xen_remove_from_physmap); +#define XENMEM_get_dma_buf 26 +/* + * This hypercall is similar to XENMEM_exchange: it exchanges the pages + * passed in with a new set of pages, contiguous and under 4G if so + * requested. The new pages are going to be "pinned": it''s guaranteed + * that their p2m mapping won''t be changed until explicitly "unpinned". + * If return code is zero then @out.extent_list provides the MFNs of the + * newly-allocated memory. Returns zero on complete success, otherwise + * a negative error code. + * On complete success then always @nr_exchanged == @in.nr_extents. On + * partial success @nr_exchanged indicates how much work was done. + */ +struct xen_get_dma_buf { + /* + * [IN] Details of memory extents to be exchanged (GMFN bases). + * Note that @in.address_bits is ignored and unused. + */ + struct xen_memory_reservation in; + + /* + * [IN/OUT] Details of new memory extents. + * We require that: + * 1. @in.domid == @out.domid + * 2. @in.nr_extents << @in.extent_order == + * @out.nr_extents << @out.extent_order + * 3. @in.extent_start and @out.extent_start lists must not overlap + * 4. @out.extent_start lists GPFN bases to be populated + * 5. @out.extent_start is overwritten with allocated GMFN bases + */ + struct xen_memory_reservation out; + + /* + * [OUT] Number of input extents that were successfully exchanged: + * 1. The first @nr_exchanged input extents were successfully + * deallocated. + * 2. The corresponding first entries in the output extent list correctly + * indicate the GMFNs that were successfully exchanged. + * 3. All other input and output extents are untouched. + * 4. If not all input exents are exchanged then the return code of this + * command will be non-zero. + * 5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER! + */ + xen_ulong_t nr_exchanged; +}; +DEFINE_GUEST_HANDLE_STRUCT(xen_get_dma_buf); + +#define XENMEM_put_dma_buf 27 +/* + * XENMEM_put_dma_buf unpins a set of pages, previously pinned by + * XENMEM_get_dma_buf. After this call the p2m mapping of the pages can + * be transparently changed by the hypervisor, as usual. The pages are + * still accessible from the guest. + */ +struct xen_put_dma_buf { + /* + * [IN] Details of memory extents to be exchanged (GMFN bases). + * Note that @in.address_bits is ignored and unused. + */ + struct xen_memory_reservation in; +}; +DEFINE_GUEST_HANDLE_STRUCT(xen_put_dma_buf); + #endif /* __XEN_PUBLIC_MEMORY_H__ */ -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 08/10] xen: make xen_create_contiguous_region return the dma address
Modify xen_create_contiguous_region to return the dma address of the newly contiguous buffer. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Reviewed-by: David Vrabel <david.vrabel@citrix.com> --- arch/x86/xen/mmu.c | 4 +++- drivers/xen/swiotlb-xen.c | 6 +++--- include/xen/xen-ops.h | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/x86/xen/mmu.c b/arch/x86/xen/mmu.c index fdc3ba2..df9ab44 100644 --- a/arch/x86/xen/mmu.c +++ b/arch/x86/xen/mmu.c @@ -2329,7 +2329,8 @@ static int xen_exchange_memory(unsigned long extents_in, unsigned int order_in, } int xen_create_contiguous_region(unsigned long vstart, unsigned int order, - unsigned int address_bits) + unsigned int address_bits, + dma_addr_t *dma_handle) { unsigned long *in_frames = discontig_frames, out_frame; unsigned long flags; @@ -2368,6 +2369,7 @@ int xen_create_contiguous_region(unsigned long vstart, unsigned int order, spin_unlock_irqrestore(&xen_reservation_lock, flags); + *dma_handle = xen_virt_to_bus(vstart); return success ? 0 : -ENOMEM; } EXPORT_SYMBOL_GPL(xen_create_contiguous_region); diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 1b2277c..b72f31c 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -126,6 +126,7 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) { int i, rc; int dma_bits; + dma_addr_t dma_handle; dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT; @@ -137,7 +138,7 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) rc = xen_create_contiguous_region( (unsigned long)buf + (i << IO_TLB_SHIFT), get_order(slabs << IO_TLB_SHIFT), - dma_bits); + dma_bits, &dma_handle); } while (rc && dma_bits++ < max_dma_bits); if (rc) return rc; @@ -294,11 +295,10 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, *dma_handle = dev_addr; else { if (xen_create_contiguous_region(vstart, order, - fls64(dma_mask)) != 0) { + fls64(dma_mask), dma_handle) != 0) { free_pages(vstart, order); return NULL; } - *dma_handle = virt_to_machine(ret).maddr; } memset(ret, 0, size); return ret; diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index d6fe062..9ef704d 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h @@ -20,7 +20,8 @@ int xen_setup_shutdown_event(void); extern unsigned long *xen_contiguous_bitmap; int xen_create_contiguous_region(unsigned long vstart, unsigned int order, - unsigned int address_bits); + unsigned int address_bits, + dma_addr_t *dma_handle); void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order); -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 09/10] swiotlb-xen: support autotranslate guests
Support autotranslate guests in swiotlb-xen by keeping track of the phys-to-bus and bus-to-phys mappings of the swiotlb buffer (xen_io_tlb_start-xen_io_tlb_end). Use a simple direct access on a pre-allocated array for phys-to-bus queries. Use a red-black tree for bus-to-phys queries. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Reviewed-by: David Vrabel <david.vrabel@citrix.com> Changes in v3: - many code style and name changes; - improve error checks in xen_dma_add_entry. --- drivers/xen/swiotlb-xen.c | 160 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 144 insertions(+), 16 deletions(-) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index b72f31c..5e9ac83 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -38,32 +38,146 @@ #include <linux/bootmem.h> #include <linux/dma-mapping.h> #include <linux/export.h> +#include <linux/slab.h> +#include <linux/spinlock_types.h> +#include <linux/rbtree.h> #include <xen/swiotlb-xen.h> #include <xen/page.h> #include <xen/xen-ops.h> #include <xen/hvc-console.h> +#include <xen/features.h> /* * Used to do a quick range check in swiotlb_tbl_unmap_single and * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this * API. */ +#define NR_DMA_SEGS ((xen_io_tlb_nslabs + IO_TLB_SEGSIZE - 1) / IO_TLB_SEGSIZE) static char *xen_io_tlb_start, *xen_io_tlb_end; static unsigned long xen_io_tlb_nslabs; /* * Quick lookup value of the bus address of the IOTLB. */ -static u64 start_dma_addr; +struct xen_dma_info { + dma_addr_t dma_addr; + phys_addr_t phys_addr; + size_t size; + struct rb_node rbnode; +}; + +/* + * This array of struct xen_dma_info is indexed by physical addresses, + * starting from virt_to_phys(xen_io_tlb_start). Each entry maps + * (IO_TLB_SEGSIZE << IO_TLB_SHIFT) bytes, except the last one that is + * smaller. Getting the dma address corresponding to a given physical + * address can be done by direct access with the right index on the + * array. + */ +static struct xen_dma_info *xen_dma_seg; +/* + * This tree keeps track of bus address to physical address + * mappings. + */ +static struct rb_root bus_to_phys = RB_ROOT; +/* This lock protects operations on the bus_to_phys tree */ +static DEFINE_SPINLOCK(xen_bus_to_phys_lock); + +static int xen_dma_add_entry(struct xen_dma_info *new) +{ + struct rb_node **link = &bus_to_phys.rb_node; + struct rb_node *parent = NULL; + struct xen_dma_info *entry; + + spin_lock(&xen_bus_to_phys_lock); + + while (*link) { + parent = *link; + entry = rb_entry(parent, struct xen_dma_info, rbnode); + + if (new->dma_addr == entry->dma_addr) { + spin_unlock(&xen_bus_to_phys_lock); + pr_warn("%s: cannot add phys=0x%pa -> dma=0x%pa, the dma address is already present, mapping to 0x%pa\n", + __func__, &new->phys_addr, + &new->dma_addr, &entry->phys_addr); + return -EINVAL; + } + if (new->phys_addr == entry->phys_addr) { + spin_unlock(&xen_bus_to_phys_lock); + pr_warn("%s: cannot add phys=0x%pa -> dma=0x%pa, the phys address is already present, mapping to 0x%pa\n", + __func__, &new->phys_addr, + &new->dma_addr, &entry->dma_addr); + return -EINVAL; + } + + if (new->dma_addr < entry->dma_addr) + link = &(*link)->rb_left; + else + link = &(*link)->rb_right; + } + rb_link_node(&new->rbnode, parent, link); + rb_insert_color(&new->rbnode, &bus_to_phys); + + spin_unlock(&xen_bus_to_phys_lock); + return 0; +} + +static struct xen_dma_info *xen_get_dma_info(dma_addr_t dma_addr) +{ + struct rb_node *n = bus_to_phys.rb_node; + struct xen_dma_info *entry; + + spin_lock(&xen_bus_to_phys_lock); + + while (n) { + entry = rb_entry(n, struct xen_dma_info, rbnode); + if (entry->dma_addr <= dma_addr && + entry->dma_addr + entry->size > dma_addr) { + spin_unlock(&xen_bus_to_phys_lock); + return entry; + } + if (dma_addr < entry->dma_addr) + n = n->rb_left; + else + n = n->rb_right; + } + + spin_unlock(&xen_bus_to_phys_lock); + return NULL; +} + +#define INVALID_ADDRESS ~0 static dma_addr_t xen_phys_to_bus(phys_addr_t paddr) { - return phys_to_machine(XPADDR(paddr)).maddr; + int nr_seg; + unsigned long offset; + char *vaddr; + + if (!xen_feature(XENFEAT_auto_translated_physmap)) + return phys_to_machine(XPADDR(paddr)).maddr; + + vaddr = (char *) phys_to_virt(paddr); + if (vaddr >= xen_io_tlb_end || vaddr < xen_io_tlb_start) + return INVALID_ADDRESS; + + offset = vaddr - xen_io_tlb_start; + nr_seg = offset / (IO_TLB_SEGSIZE << IO_TLB_SHIFT); + + return xen_dma_seg[nr_seg].dma_addr + + (paddr - xen_dma_seg[nr_seg].phys_addr); } static phys_addr_t xen_bus_to_phys(dma_addr_t baddr) { - return machine_to_phys(XMADDR(baddr)).paddr; + if (xen_feature(XENFEAT_auto_translated_physmap)) { + struct xen_dma_info *dma = xen_get_dma_info(baddr); + if (dma == NULL) + return INVALID_ADDRESS; + else + return dma->phys_addr + (baddr - dma->dma_addr); + } else + return machine_to_phys(XMADDR(baddr)).paddr; } static dma_addr_t xen_virt_to_bus(void *address) @@ -107,6 +221,9 @@ static int is_xen_swiotlb_buffer(dma_addr_t dma_addr) unsigned long pfn = mfn_to_local_pfn(mfn); phys_addr_t paddr; + if (xen_feature(XENFEAT_auto_translated_physmap)) + return 1; + /* If the address is outside our domain, it CAN * have the same virtual address as another address * in our domain. Therefore _only_ check address within our domain. @@ -124,13 +241,12 @@ static int max_dma_bits = 32; static int xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) { - int i, rc; + int i, j, rc; int dma_bits; - dma_addr_t dma_handle; dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT; - i = 0; + i = j = 0; do { int slabs = min(nslabs - i, (unsigned long)IO_TLB_SEGSIZE); @@ -138,12 +254,18 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) rc = xen_create_contiguous_region( (unsigned long)buf + (i << IO_TLB_SHIFT), get_order(slabs << IO_TLB_SHIFT), - dma_bits, &dma_handle); + dma_bits, &xen_dma_seg[j].dma_addr); } while (rc && dma_bits++ < max_dma_bits); if (rc) return rc; + xen_dma_seg[j].phys_addr = virt_to_phys(buf + (i << IO_TLB_SHIFT)); + xen_dma_seg[j].size = slabs << IO_TLB_SHIFT; + rc = xen_dma_add_entry(&xen_dma_seg[j]); + if (rc != 0) + return rc; i += slabs; + j++; } while (i < nslabs); return 0; } @@ -193,9 +315,10 @@ retry: /* * Get IO TLB memory from any location. */ - if (early) + if (early) { xen_io_tlb_start = alloc_bootmem_pages(PAGE_ALIGN(bytes)); - else { + xen_dma_seg = alloc_bootmem(sizeof(struct xen_dma_info) * NR_DMA_SEGS); + } else { #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { @@ -210,6 +333,8 @@ retry: xen_io_tlb_nslabs = SLABS_PER_PAGE << order; bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT; } + xen_dma_seg = kzalloc(sizeof(struct xen_dma_info) * NR_DMA_SEGS, + GFP_KERNEL); } if (!xen_io_tlb_start) { m_ret = XEN_SWIOTLB_ENOMEM; @@ -232,7 +357,6 @@ retry: m_ret = XEN_SWIOTLB_EFIXUP; goto error; } - start_dma_addr = xen_virt_to_bus(xen_io_tlb_start); if (early) { if (swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs, verbose)) @@ -290,7 +414,8 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, phys = virt_to_phys(ret); dev_addr = xen_phys_to_bus(phys); - if (((dev_addr + size - 1 <= dma_mask)) && + if (!xen_feature(XENFEAT_auto_translated_physmap) && + ((dev_addr + size - 1 <= dma_mask)) && !range_straddles_page_boundary(phys, size)) *dma_handle = dev_addr; else { @@ -321,8 +446,9 @@ xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, phys = virt_to_phys(vaddr); - if (((dev_addr + size - 1 > dma_mask)) || - range_straddles_page_boundary(phys, size)) + if (xen_feature(XENFEAT_auto_translated_physmap) || + (((dev_addr + size - 1 > dma_mask)) || + range_straddles_page_boundary(phys, size))) xen_destroy_contiguous_region((unsigned long)vaddr, order); free_pages((unsigned long)vaddr, order); @@ -351,14 +477,15 @@ dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page, * we can safely return the device addr and not worry about bounce * buffering it. */ - if (dma_capable(dev, dev_addr, size) && + if (!xen_feature(XENFEAT_auto_translated_physmap) && + dma_capable(dev, dev_addr, size) && !range_straddles_page_boundary(phys, size) && !swiotlb_force) return dev_addr; /* * Oh well, have to allocate and map a bounce buffer. */ - map = swiotlb_tbl_map_single(dev, start_dma_addr, phys, size, dir); + map = swiotlb_tbl_map_single(dev, xen_dma_seg[0].dma_addr, phys, size, dir); if (map == SWIOTLB_MAP_ERROR) return DMA_ERROR_CODE; @@ -494,10 +621,11 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, dma_addr_t dev_addr = xen_phys_to_bus(paddr); if (swiotlb_force || + xen_feature(XENFEAT_auto_translated_physmap) || !dma_capable(hwdev, dev_addr, sg->length) || range_straddles_page_boundary(paddr, sg->length)) { phys_addr_t map = swiotlb_tbl_map_single(hwdev, - start_dma_addr, + xen_dma_seg[0].dma_addr, sg_phys(sg), sg->length, dir); -- 1.7.2.5
Stefano Stabellini
2013-Aug-05 16:30 UTC
[PATCH v3 10/10] xen/arm,arm64: enable SWIOTLB_XEN
Xen on arm and arm64 needs SWIOTLB_XEN: when running on Xen we need to program the hardware with mfns rather than pfns for dma addresses. Remove SWIOTLB_XEN dependency on X86 and PCI and make XEN select SWIOTLB_XEN on arm and arm64. Implement xen_create_contiguous_region on arm and arm64 by using XENMEM_get_dma_buf. Initialize the xen-swiotlb from xen_early_init (before the native dma_ops are initialized), set dma_ops to &xen_swiotlb_dma_ops if we are running on Xen. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Changes in v3: - code style changes; - warn on XENMEM_put_dma_buf failures. --- arch/arm/Kconfig | 1 + arch/arm/include/asm/xen/page.h | 2 + arch/arm/xen/Makefile | 2 +- arch/arm/xen/enlighten.c | 3 + arch/arm/xen/mm.c | 117 +++++++++++++++++++++++++++++++++++++++ arch/arm64/Kconfig | 1 + arch/arm64/xen/Makefile | 2 +- drivers/xen/Kconfig | 1 - drivers/xen/swiotlb-xen.c | 18 ++++++ 9 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 arch/arm/xen/mm.c diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index c0bfb33..2c9d112 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1848,6 +1848,7 @@ config XEN depends on CPU_V7 && !CPU_V6 depends on !GENERIC_ATOMIC64 select ARM_PSCI + select SWIOTLB_XEN help Say Y if you want to run Linux in a Virtual Machine on Xen on ARM. diff --git a/arch/arm/include/asm/xen/page.h b/arch/arm/include/asm/xen/page.h index 359a7b5..b0f7150 100644 --- a/arch/arm/include/asm/xen/page.h +++ b/arch/arm/include/asm/xen/page.h @@ -6,12 +6,14 @@ #include <linux/pfn.h> #include <linux/types.h> +#include <linux/dma-mapping.h> #include <xen/interface/grant_table.h> #define pfn_to_mfn(pfn) (pfn) #define phys_to_machine_mapping_valid(pfn) (1) #define mfn_to_pfn(mfn) (mfn) +#define mfn_to_local_pfn(m) (mfn_to_pfn(m)) #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT)) #define pte_mfn pte_pfn diff --git a/arch/arm/xen/Makefile b/arch/arm/xen/Makefile index 4384103..66fc35d 100644 --- a/arch/arm/xen/Makefile +++ b/arch/arm/xen/Makefile @@ -1 +1 @@ -obj-y := enlighten.o hypercall.o grant-table.o +obj-y := enlighten.o hypercall.o grant-table.o mm.o diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c index 14d17ab..06a6953 100644 --- a/arch/arm/xen/enlighten.c +++ b/arch/arm/xen/enlighten.c @@ -195,6 +195,7 @@ static void xen_power_off(void) * documentation of the Xen Device Tree format. */ #define GRANT_TABLE_PHYSADDR 0 +extern int xen_mm_init(void); void __init xen_early_init(void) { struct resource res; @@ -230,6 +231,8 @@ void __init xen_early_init(void) xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED; else xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED); + + xen_mm_init(); } static int __init xen_guest_init(void) diff --git a/arch/arm/xen/mm.c b/arch/arm/xen/mm.c new file mode 100644 index 0000000..de7711a --- /dev/null +++ b/arch/arm/xen/mm.c @@ -0,0 +1,117 @@ +#include <linux/bootmem.h> +#include <linux/gfp.h> +#include <linux/export.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/dma-mapping.h> +#include <linux/vmalloc.h> +#include <linux/swiotlb.h> + +#include <xen/xen.h> +#include <xen/interface/memory.h> +#include <xen/swiotlb-xen.h> + +#include <asm/cacheflush.h> +#include <asm/xen/page.h> +#include <asm/xen/hypercall.h> +#include <asm/xen/interface.h> + +static int xen_exchange_memory(xen_ulong_t extents_in, + unsigned int order_in, + xen_pfn_t *pfns_in, + xen_ulong_t extents_out, + unsigned int order_out, + xen_pfn_t *mfns_out, + unsigned int address_bits) +{ + long rc; + int success; + + struct xen_memory_exchange exchange = { + .in = { + .nr_extents = extents_in, + .extent_order = order_in, + .domid = DOMID_SELF + }, + .out = { + .nr_extents = extents_out, + .extent_order = order_out, + .address_bits = address_bits, + .domid = DOMID_SELF + } + }; + set_xen_guest_handle(exchange.in.extent_start, pfns_in); + set_xen_guest_handle(exchange.out.extent_start, mfns_out); + + BUG_ON(extents_in << order_in != extents_out << order_out); + + + rc = HYPERVISOR_memory_op(XENMEM_get_dma_buf, &exchange); + success = (exchange.nr_exchanged == extents_in); + + BUG_ON(!success && ((exchange.nr_exchanged != 0) || (rc == 0))); + BUG_ON(success && (rc != 0)); + + return success; +} + +int xen_create_contiguous_region(unsigned long vstart, unsigned int order, + unsigned int address_bits, + dma_addr_t *dma_handle) +{ + phys_addr_t pstart = __pa(vstart); + xen_pfn_t in_frame, out_frame; + int success; + + /* Get a new contiguous memory extent. */ + in_frame = out_frame = pstart >> PAGE_SHIFT; + success = xen_exchange_memory(1, order, &in_frame, + 1, order, &out_frame, + address_bits); + + if (!success) + return -ENOMEM; + + *dma_handle = out_frame << PAGE_SHIFT; + + return success ? 0 : -ENOMEM; +} +EXPORT_SYMBOL_GPL(xen_create_contiguous_region); + +void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order) +{ + xen_pfn_t in_frame = __pa(vstart) >> PAGE_SHIFT; + struct xen_put_dma_buf buf = { + .in = { + .nr_extents = 1, + .extent_order = order, + .domid = DOMID_SELF + }, + }; + set_xen_guest_handle(buf.in.extent_start, &in_frame); + + WARN_ON(HYPERVISOR_memory_op(XENMEM_put_dma_buf, &buf)); +} +EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region); + +static struct dma_map_ops xen_swiotlb_dma_ops = { + .mapping_error = xen_swiotlb_dma_mapping_error, + .alloc = xen_swiotlb_alloc_coherent, + .free = xen_swiotlb_free_coherent, + .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu, + .sync_single_for_device = xen_swiotlb_sync_single_for_device, + .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu, + .sync_sg_for_device = xen_swiotlb_sync_sg_for_device, + .map_sg = xen_swiotlb_map_sg_attrs, + .unmap_sg = xen_swiotlb_unmap_sg_attrs, + .map_page = xen_swiotlb_map_page, + .unmap_page = xen_swiotlb_unmap_page, + .dma_supported = xen_swiotlb_dma_supported, +}; + +int __init xen_mm_init(void) +{ + xen_swiotlb_init(1, true); + dma_ops = &xen_swiotlb_dma_ops; + return 0; +} diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 9737e97..aa1f6fb 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -209,6 +209,7 @@ config XEN_DOM0 config XEN bool "Xen guest support on ARM64 (EXPERIMENTAL)" depends on ARM64 && OF + select SWIOTLB_XEN help Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64. diff --git a/arch/arm64/xen/Makefile b/arch/arm64/xen/Makefile index be24040..0ef9637 100644 --- a/arch/arm64/xen/Makefile +++ b/arch/arm64/xen/Makefile @@ -1,2 +1,2 @@ -xen-arm-y += $(addprefix ../../arm/xen/, enlighten.o grant-table.o) +xen-arm-y += $(addprefix ../../arm/xen/, enlighten.o grant-table.o mm.o) obj-y := xen-arm.o hypercall.o diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig index 9e02d60..7e83688 100644 --- a/drivers/xen/Kconfig +++ b/drivers/xen/Kconfig @@ -140,7 +140,6 @@ config XEN_GRANT_DEV_ALLOC config SWIOTLB_XEN def_bool y - depends on PCI && X86 select SWIOTLB config XEN_TMEM diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 5e9ac83..f166a88 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -59,6 +59,24 @@ static unsigned long xen_io_tlb_nslabs; * Quick lookup value of the bus address of the IOTLB. */ +#ifndef DMA_ERROR_CODE +#define DMA_ERROR_CODE (~0) +#endif + +#ifndef CONFIG_X86 +static unsigned long dma_alloc_coherent_mask(struct device *dev, + gfp_t gfp) +{ + unsigned long dma_mask = 0; + + dma_mask = dev->coherent_dma_mask; + if (!dma_mask) + dma_mask = (gfp & GFP_DMA) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32); + + return dma_mask; +} +#endif + struct xen_dma_info { dma_addr_t dma_addr; phys_addr_t phys_addr; -- 1.7.2.5
Konrad Rzeszutek Wilk
2013-Aug-09 15:29 UTC
Re: [PATCH v3 03/10] arm: make SWIOTLB available
On Mon, Aug 05, 2013 at 05:30:49PM +0100, Stefano Stabellini wrote:> IOMMU_HELPER is needed because SWIOTLB calls iommu_is_span_boundary, > provided by lib/iommu_helper.c. > > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>And Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Thanks.> CC: will.deacon@arm.com > CC: linux@arm.linux.org.uk > > > Changes in v3: > - dma_capable: do not treat dma_mask as a limit; > - remove SWIOTLB dependency on NEED_SG_DMA_LENGTH. > --- > arch/arm/Kconfig | 6 ++++++ > arch/arm/include/asm/dma-mapping.h | 30 ++++++++++++++++++++++++++++++ > 2 files changed, 36 insertions(+), 0 deletions(-) > > diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig > index ba412e0..c0bfb33 100644 > --- a/arch/arm/Kconfig > +++ b/arch/arm/Kconfig > @@ -1832,6 +1832,12 @@ config CC_STACKPROTECTOR > neutralized via a kernel panic. > This feature requires gcc version 4.2 or above. > > +config SWIOTLB > + def_bool y > + > +config IOMMU_HELPER > + def_bool SWIOTLB > + > config XEN_DOM0 > def_bool y > depends on XEN > diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h > index 5b579b9..5b8eef9 100644 > --- a/arch/arm/include/asm/dma-mapping.h > +++ b/arch/arm/include/asm/dma-mapping.h > @@ -86,6 +86,36 @@ static inline dma_addr_t virt_to_dma(struct device *dev, void *addr) > } > #endif > > +static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) > +{ > + unsigned int offset = paddr & ~PAGE_MASK; > + return pfn_to_dma(dev, paddr >> PAGE_SHIFT) + offset; > +} > + > +static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) > +{ > + unsigned int offset = dev_addr & ~PAGE_MASK; > + return (dma_to_pfn(dev, dev_addr) << PAGE_SHIFT) + offset; > +} > + > +static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) > +{ > + u64 limit, mask = *dev->dma_mask; > + > + limit = (mask + 1) & ~mask; > + if (limit && size > limit) > + return 0; > + > + if ((addr | (addr + size - 1)) & ~mask) > + return 0; > + > + return 1; > +} > + > +static inline void dma_mark_clean(void *addr, size_t size) > +{ > +} > + > /* > * DMA errors are defined by all-bits-set in the DMA address. > */ > -- > 1.7.2.5 >
Konrad Rzeszutek Wilk
2013-Aug-09 15:36 UTC
Re: [PATCH v3 07/10] xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf
On Mon, Aug 05, 2013 at 05:30:53PM +0100, Stefano Stabellini wrote:> XENMEM_exchange can''t be used by autotranslate guests because of two > severe limitations: > > - it does not copy back the mfns into the out field for autotranslate > guests; > > - it does not guarantee that the hypervisor won''t change the p2m > mappings for the exchanged pages while the guest is using them. Xen > never promises to keep the p2m mapping stable for autotranslate guests > in general. In practice it won''t happen unless one uses uncommon > features like memory sharing or paging. > > To overcome these problems I am introducing two new hypercalls.You should also refer to the git or c/s of where it is implemented in the hypervisor (once it lands there of course).> > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> > --- > include/xen/interface/memory.h | 62 ++++++++++++++++++++++++++++++++++++++++ > 1 files changed, 62 insertions(+), 0 deletions(-) > > diff --git a/include/xen/interface/memory.h b/include/xen/interface/memory.h > index 2ecfe4f..ffd7f4e 100644 > --- a/include/xen/interface/memory.h > +++ b/include/xen/interface/memory.h > @@ -263,4 +263,66 @@ struct xen_remove_from_physmap { > }; > DEFINE_GUEST_HANDLE_STRUCT(xen_remove_from_physmap); > > +#define XENMEM_get_dma_buf 26 > +/* > + * This hypercall is similar to XENMEM_exchange: it exchanges the pages > + * passed in with a new set of pages, contiguous and under 4G if soThe "under 4G" is not true. It is based on the bit value. The user could request it be under "1G" if they wanted.> + * requested. The new pages are going to be "pinned": it''s guaranteed > + * that their p2m mapping won''t be changed until explicitly "unpinned".What if you try to balloon them out? What happens then? Does that unpin them automatically? What if I use said "pin" page for grants? Can they be shared with another guest?> + * If return code is zero then @out.extent_list provides the MFNs of the > + * newly-allocated memory. Returns zero on complete success, otherwise > + * a negative error code.Ahem. Which ones? I know you didn''t like the existing grant code b/c it returned some general error. Would it make sense to say which are ones are expected?> + * On complete success then always @nr_exchanged == @in.nr_extents. On > + * partial success @nr_exchanged indicates how much work was done.And no error?> + */ > +struct xen_get_dma_buf { > + /* > + * [IN] Details of memory extents to be exchanged (GMFN bases). > + * Note that @in.address_bits is ignored and unused.Ohhhh, why? What if the user wants to be it under 2G?> + */ > + struct xen_memory_reservation in; > + > + /* > + * [IN/OUT] Details of new memory extents. > + * We require that: > + * 1. @in.domid == @out.domid > + * 2. @in.nr_extents << @in.extent_order == > + * @out.nr_extents << @out.extent_order > + * 3. @in.extent_start and @out.extent_start lists must not overlap > + * 4. @out.extent_start lists GPFN bases to be populated > + * 5. @out.extent_start is overwritten with allocated GMFN bases > + */ > + struct xen_memory_reservation out; > + > + /* > + * [OUT] Number of input extents that were successfully exchanged: > + * 1. The first @nr_exchanged input extents were successfully > + * deallocated. > + * 2. The corresponding first entries in the output extent list correctly > + * indicate the GMFNs that were successfully exchanged. > + * 3. All other input and output extents are untouched. > + * 4. If not all input exents are exchanged then the return code of this > + * command will be non-zero. > + * 5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER! > + */ > + xen_ulong_t nr_exchanged; > +}; > +DEFINE_GUEST_HANDLE_STRUCT(xen_get_dma_buf); > + > +#define XENMEM_put_dma_buf 27 > +/* > + * XENMEM_put_dma_buf unpins a set of pages, previously pinned by > + * XENMEM_get_dma_buf. After this call the p2m mapping of the pages can > + * be transparently changed by the hypervisor, as usual. The pages are > + * still accessible from the guest.I don''t understand what ''pinned'' means. What is a ''pinned'' page versus a normal page? Is this like the pagetables which must be _RO?> + */ > +struct xen_put_dma_buf { > + /* > + * [IN] Details of memory extents to be exchanged (GMFN bases). > + * Note that @in.address_bits is ignored and unused. > + */ > + struct xen_memory_reservation in; > +}; > +DEFINE_GUEST_HANDLE_STRUCT(xen_put_dma_buf); > + > #endif /* __XEN_PUBLIC_MEMORY_H__ */ > -- > 1.7.2.5 >
Konrad Rzeszutek Wilk
2013-Aug-09 15:36 UTC
Re: [PATCH v3 04/10] arm: introduce a global dma_ops pointer
On Mon, Aug 05, 2013 at 05:30:50PM +0100, Stefano Stabellini wrote:> Initially set dma_ops to arm_dma_ops.Looks OK to me.> > > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> > CC: will.deacon@arm.com > CC: linux@arm.linux.org.uk > > > Changes in v3: > - keep using arm_dma_ops in dmabounce. > --- > arch/arm/include/asm/dma-mapping.h | 3 ++- > arch/arm/mm/dma-mapping.c | 3 +++ > 2 files changed, 5 insertions(+), 1 deletions(-) > > diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h > index 5b8eef9..5c130bc 100644 > --- a/arch/arm/include/asm/dma-mapping.h > +++ b/arch/arm/include/asm/dma-mapping.h > @@ -12,6 +12,7 @@ > #include <asm/memory.h> > > #define DMA_ERROR_CODE (~0) > +extern struct dma_map_ops *dma_ops; > extern struct dma_map_ops arm_dma_ops; > extern struct dma_map_ops arm_coherent_dma_ops; > > @@ -19,7 +20,7 @@ static inline struct dma_map_ops *get_dma_ops(struct device *dev) > { > if (dev && dev->archdata.dma_ops) > return dev->archdata.dma_ops; > - return &arm_dma_ops; > + return dma_ops; > } > > static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops) > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index 7f9b179..870b12c 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -141,6 +141,9 @@ struct dma_map_ops arm_dma_ops = { > }; > EXPORT_SYMBOL(arm_dma_ops); > > +struct dma_map_ops *dma_ops = &arm_dma_ops; > +EXPORT_SYMBOL(dma_ops); > + > static void *arm_coherent_dma_alloc(struct device *dev, size_t size, > dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs); > static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr, > -- > 1.7.2.5 >
Konrad Rzeszutek Wilk
2013-Aug-09 15:38 UTC
Re: [PATCH v3 10/10] xen/arm,arm64: enable SWIOTLB_XEN
On Mon, Aug 05, 2013 at 05:30:56PM +0100, Stefano Stabellini wrote:> Xen on arm and arm64 needs SWIOTLB_XEN: when running on Xen we need to > program the hardware with mfns rather than pfns for dma addresses. > Remove SWIOTLB_XEN dependency on X86 and PCI and make XEN select > SWIOTLB_XEN on arm and arm64.Should you say something about hardware that has IOMMUs?> > Implement xen_create_contiguous_region on arm and arm64 by using > XENMEM_get_dma_buf. > > Initialize the xen-swiotlb from xen_early_init (before the native > dma_ops are initialized), set dma_ops to &xen_swiotlb_dma_ops if we are > running on Xen. > > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> > > > Changes in v3: > - code style changes; > - warn on XENMEM_put_dma_buf failures. > --- > arch/arm/Kconfig | 1 + > arch/arm/include/asm/xen/page.h | 2 + > arch/arm/xen/Makefile | 2 +- > arch/arm/xen/enlighten.c | 3 + > arch/arm/xen/mm.c | 117 +++++++++++++++++++++++++++++++++++++++ > arch/arm64/Kconfig | 1 + > arch/arm64/xen/Makefile | 2 +- > drivers/xen/Kconfig | 1 - > drivers/xen/swiotlb-xen.c | 18 ++++++ > 9 files changed, 144 insertions(+), 3 deletions(-) > create mode 100644 arch/arm/xen/mm.c > > diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig > index c0bfb33..2c9d112 100644 > --- a/arch/arm/Kconfig > +++ b/arch/arm/Kconfig > @@ -1848,6 +1848,7 @@ config XEN > depends on CPU_V7 && !CPU_V6 > depends on !GENERIC_ATOMIC64 > select ARM_PSCI > + select SWIOTLB_XEN > help > Say Y if you want to run Linux in a Virtual Machine on Xen on ARM. > > diff --git a/arch/arm/include/asm/xen/page.h b/arch/arm/include/asm/xen/page.h > index 359a7b5..b0f7150 100644 > --- a/arch/arm/include/asm/xen/page.h > +++ b/arch/arm/include/asm/xen/page.h > @@ -6,12 +6,14 @@ > > #include <linux/pfn.h> > #include <linux/types.h> > +#include <linux/dma-mapping.h> > > #include <xen/interface/grant_table.h> > > #define pfn_to_mfn(pfn) (pfn) > #define phys_to_machine_mapping_valid(pfn) (1) > #define mfn_to_pfn(mfn) (mfn) > +#define mfn_to_local_pfn(m) (mfn_to_pfn(m)) > #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT)) > > #define pte_mfn pte_pfn > diff --git a/arch/arm/xen/Makefile b/arch/arm/xen/Makefile > index 4384103..66fc35d 100644 > --- a/arch/arm/xen/Makefile > +++ b/arch/arm/xen/Makefile > @@ -1 +1 @@ > -obj-y := enlighten.o hypercall.o grant-table.o > +obj-y := enlighten.o hypercall.o grant-table.o mm.o > diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c > index 14d17ab..06a6953 100644 > --- a/arch/arm/xen/enlighten.c > +++ b/arch/arm/xen/enlighten.c > @@ -195,6 +195,7 @@ static void xen_power_off(void) > * documentation of the Xen Device Tree format. > */ > #define GRANT_TABLE_PHYSADDR 0 > +extern int xen_mm_init(void); > void __init xen_early_init(void) > { > struct resource res; > @@ -230,6 +231,8 @@ void __init xen_early_init(void) > xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED; > else > xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED); > + > + xen_mm_init(); > } > > static int __init xen_guest_init(void) > diff --git a/arch/arm/xen/mm.c b/arch/arm/xen/mm.c > new file mode 100644 > index 0000000..de7711a > --- /dev/null > +++ b/arch/arm/xen/mm.c > @@ -0,0 +1,117 @@ > +#include <linux/bootmem.h> > +#include <linux/gfp.h> > +#include <linux/export.h> > +#include <linux/slab.h> > +#include <linux/types.h> > +#include <linux/dma-mapping.h> > +#include <linux/vmalloc.h> > +#include <linux/swiotlb.h> > + > +#include <xen/xen.h> > +#include <xen/interface/memory.h> > +#include <xen/swiotlb-xen.h> > + > +#include <asm/cacheflush.h> > +#include <asm/xen/page.h> > +#include <asm/xen/hypercall.h> > +#include <asm/xen/interface.h> > + > +static int xen_exchange_memory(xen_ulong_t extents_in, > + unsigned int order_in, > + xen_pfn_t *pfns_in, > + xen_ulong_t extents_out, > + unsigned int order_out, > + xen_pfn_t *mfns_out, > + unsigned int address_bits) > +{ > + long rc; > + int success; > + > + struct xen_memory_exchange exchange = { > + .in = { > + .nr_extents = extents_in, > + .extent_order = order_in, > + .domid = DOMID_SELF > + }, > + .out = { > + .nr_extents = extents_out, > + .extent_order = order_out, > + .address_bits = address_bits, > + .domid = DOMID_SELF > + } > + }; > + set_xen_guest_handle(exchange.in.extent_start, pfns_in); > + set_xen_guest_handle(exchange.out.extent_start, mfns_out); > + > + BUG_ON(extents_in << order_in != extents_out << order_out); > + > + > + rc = HYPERVISOR_memory_op(XENMEM_get_dma_buf, &exchange); > + success = (exchange.nr_exchanged == extents_in); > + > + BUG_ON(!success && ((exchange.nr_exchanged != 0) || (rc == 0))); > + BUG_ON(success && (rc != 0)); > + > + return success; > +} > + > +int xen_create_contiguous_region(unsigned long vstart, unsigned int order, > + unsigned int address_bits, > + dma_addr_t *dma_handle) > +{ > + phys_addr_t pstart = __pa(vstart); > + xen_pfn_t in_frame, out_frame; > + int success; > + > + /* Get a new contiguous memory extent. */ > + in_frame = out_frame = pstart >> PAGE_SHIFT; > + success = xen_exchange_memory(1, order, &in_frame, > + 1, order, &out_frame, > + address_bits); > + > + if (!success) > + return -ENOMEM; > + > + *dma_handle = out_frame << PAGE_SHIFT; > + > + return success ? 0 : -ENOMEM; > +} > +EXPORT_SYMBOL_GPL(xen_create_contiguous_region); > + > +void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order) > +{ > + xen_pfn_t in_frame = __pa(vstart) >> PAGE_SHIFT; > + struct xen_put_dma_buf buf = { > + .in = { > + .nr_extents = 1, > + .extent_order = order, > + .domid = DOMID_SELF > + }, > + }; > + set_xen_guest_handle(buf.in.extent_start, &in_frame); > + > + WARN_ON(HYPERVISOR_memory_op(XENMEM_put_dma_buf, &buf)); > +} > +EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region); > + > +static struct dma_map_ops xen_swiotlb_dma_ops = { > + .mapping_error = xen_swiotlb_dma_mapping_error, > + .alloc = xen_swiotlb_alloc_coherent, > + .free = xen_swiotlb_free_coherent, > + .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu, > + .sync_single_for_device = xen_swiotlb_sync_single_for_device, > + .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu, > + .sync_sg_for_device = xen_swiotlb_sync_sg_for_device, > + .map_sg = xen_swiotlb_map_sg_attrs, > + .unmap_sg = xen_swiotlb_unmap_sg_attrs, > + .map_page = xen_swiotlb_map_page, > + .unmap_page = xen_swiotlb_unmap_page, > + .dma_supported = xen_swiotlb_dma_supported, > +}; > + > +int __init xen_mm_init(void) > +{ > + xen_swiotlb_init(1, true); > + dma_ops = &xen_swiotlb_dma_ops; > + return 0; > +} > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index 9737e97..aa1f6fb 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -209,6 +209,7 @@ config XEN_DOM0 > config XEN > bool "Xen guest support on ARM64 (EXPERIMENTAL)" > depends on ARM64 && OF > + select SWIOTLB_XEN > help > Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64. > > diff --git a/arch/arm64/xen/Makefile b/arch/arm64/xen/Makefile > index be24040..0ef9637 100644 > --- a/arch/arm64/xen/Makefile > +++ b/arch/arm64/xen/Makefile > @@ -1,2 +1,2 @@ > -xen-arm-y += $(addprefix ../../arm/xen/, enlighten.o grant-table.o) > +xen-arm-y += $(addprefix ../../arm/xen/, enlighten.o grant-table.o mm.o) > obj-y := xen-arm.o hypercall.o > diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig > index 9e02d60..7e83688 100644 > --- a/drivers/xen/Kconfig > +++ b/drivers/xen/Kconfig > @@ -140,7 +140,6 @@ config XEN_GRANT_DEV_ALLOC > > config SWIOTLB_XEN > def_bool y > - depends on PCI && X86 > select SWIOTLB > > config XEN_TMEM > diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c > index 5e9ac83..f166a88 100644 > --- a/drivers/xen/swiotlb-xen.c > +++ b/drivers/xen/swiotlb-xen.c > @@ -59,6 +59,24 @@ static unsigned long xen_io_tlb_nslabs; > * Quick lookup value of the bus address of the IOTLB. > */ > > +#ifndef DMA_ERROR_CODE > +#define DMA_ERROR_CODE (~0)Not zero? And not (-1ULL)?> +#endif > + > +#ifndef CONFIG_X86 > +static unsigned long dma_alloc_coherent_mask(struct device *dev, > + gfp_t gfp) > +{ > + unsigned long dma_mask = 0; > + > + dma_mask = dev->coherent_dma_mask; > + if (!dma_mask) > + dma_mask = (gfp & GFP_DMA) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32); > + > + return dma_mask; > +} > +#endif > + > struct xen_dma_info { > dma_addr_t dma_addr; > phys_addr_t phys_addr; > -- > 1.7.2.5 >
Konrad Rzeszutek Wilk
2013-Aug-09 15:45 UTC
Re: [PATCH v3 09/10] swiotlb-xen: support autotranslate guests
n Mon, Aug 05, 2013 at 05:30:55PM +0100, Stefano Stabellini wrote:> Support autotranslate guests in swiotlb-xen by keeping track of the > phys-to-bus and bus-to-phys mappings of the swiotlb buffer > (xen_io_tlb_start-xen_io_tlb_end). > > Use a simple direct access on a pre-allocated array for phys-to-bus > queries. Use a red-black tree for bus-to-phys queries. > > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> > Reviewed-by: David Vrabel <david.vrabel@citrix.com> > > > Changes in v3: > - many code style and name changes; > - improve error checks in xen_dma_add_entry. > --- > drivers/xen/swiotlb-xen.c | 160 ++++++++++++++++++++++++++++++++++++++++----- > 1 files changed, 144 insertions(+), 16 deletions(-) > > diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c > index b72f31c..5e9ac83 100644 > --- a/drivers/xen/swiotlb-xen.c > +++ b/drivers/xen/swiotlb-xen.c > @@ -38,32 +38,146 @@ > #include <linux/bootmem.h> > #include <linux/dma-mapping.h> > #include <linux/export.h> > +#include <linux/slab.h> > +#include <linux/spinlock_types.h> > +#include <linux/rbtree.h> > #include <xen/swiotlb-xen.h> > #include <xen/page.h> > #include <xen/xen-ops.h> > #include <xen/hvc-console.h> > +#include <xen/features.h> > /* > * Used to do a quick range check in swiotlb_tbl_unmap_single and > * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this > * API. > */ > > +#define NR_DMA_SEGS ((xen_io_tlb_nslabs + IO_TLB_SEGSIZE - 1) / IO_TLB_SEGSIZE) > static char *xen_io_tlb_start, *xen_io_tlb_end; > static unsigned long xen_io_tlb_nslabs; > /* > * Quick lookup value of the bus address of the IOTLB. > */ > > -static u64 start_dma_addr; > +struct xen_dma_info { > + dma_addr_t dma_addr; > + phys_addr_t phys_addr; > + size_t size; > + struct rb_node rbnode; > +}; > + > +/* > + * This array of struct xen_dma_info is indexed by physical addresses, > + * starting from virt_to_phys(xen_io_tlb_start). Each entry maps > + * (IO_TLB_SEGSIZE << IO_TLB_SHIFT) bytes, except the last one that is > + * smaller. Getting the dma address corresponding to a given physical > + * address can be done by direct access with the right index on the > + * array. > + */ > +static struct xen_dma_info *xen_dma_seg; > +/* > + * This tree keeps track of bus address to physical address > + * mappings. > + */ > +static struct rb_root bus_to_phys = RB_ROOT; > +/* This lock protects operations on the bus_to_phys tree */ > +static DEFINE_SPINLOCK(xen_bus_to_phys_lock); > + > +static int xen_dma_add_entry(struct xen_dma_info *new) > +{ > + struct rb_node **link = &bus_to_phys.rb_node; > + struct rb_node *parent = NULL; > + struct xen_dma_info *entry; > + > + spin_lock(&xen_bus_to_phys_lock); > + > + while (*link) { > + parent = *link; > + entry = rb_entry(parent, struct xen_dma_info, rbnode); > + > + if (new->dma_addr == entry->dma_addr) { > + spin_unlock(&xen_bus_to_phys_lock); > + pr_warn("%s: cannot add phys=0x%pa -> dma=0x%pa, the dma address is already present, mapping to 0x%pa\n", > + __func__, &new->phys_addr, > + &new->dma_addr, &entry->phys_addr); > + return -EINVAL; > + } > + if (new->phys_addr == entry->phys_addr) { > + spin_unlock(&xen_bus_to_phys_lock); > + pr_warn("%s: cannot add phys=0x%pa -> dma=0x%pa, the phys address is already present, mapping to 0x%pa\n", > + __func__, &new->phys_addr, > + &new->dma_addr, &entry->dma_addr); > + return -EINVAL;How about adding a "err_out: spin_unlock(...) pr_warn(..) return -EINVAL: at the end of the function (past the return 0) and just do a goto err_out here.> + } > + > + if (new->dma_addr < entry->dma_addr) > + link = &(*link)->rb_left; > + else > + link = &(*link)->rb_right; > + } > + rb_link_node(&new->rbnode, parent, link); > + rb_insert_color(&new->rbnode, &bus_to_phys); > + > + spin_unlock(&xen_bus_to_phys_lock); > + return 0; > +} > + > +static struct xen_dma_info *xen_get_dma_info(dma_addr_t dma_addr) > +{ > + struct rb_node *n = bus_to_phys.rb_node; > + struct xen_dma_info *entry; > + > + spin_lock(&xen_bus_to_phys_lock); > + > + while (n) { > + entry = rb_entry(n, struct xen_dma_info, rbnode); > + if (entry->dma_addr <= dma_addr && > + entry->dma_addr + entry->size > dma_addr) { > + spin_unlock(&xen_bus_to_phys_lock); > + return entry; > + } > + if (dma_addr < entry->dma_addr) > + n = n->rb_left; > + else > + n = n->rb_right; > + } > + > + spin_unlock(&xen_bus_to_phys_lock); > + return NULL; > +} > + > +#define INVALID_ADDRESS ~0s/INVALID_ADDRESS/DMA_ERROR_CODE/> > static dma_addr_t xen_phys_to_bus(phys_addr_t paddr) > { > - return phys_to_machine(XPADDR(paddr)).maddr; > + int nr_seg; > + unsigned long offset; > + char *vaddr; > + > + if (!xen_feature(XENFEAT_auto_translated_physmap)) > + return phys_to_machine(XPADDR(paddr)).maddr; > + > + vaddr = (char *) phys_to_virt(paddr);The space there is not needed.> + if (vaddr >= xen_io_tlb_end || vaddr < xen_io_tlb_start) > + return INVALID_ADDRESS; > + > + offset = vaddr - xen_io_tlb_start; > + nr_seg = offset / (IO_TLB_SEGSIZE << IO_TLB_SHIFT); > + > + return xen_dma_seg[nr_seg].dma_addr + > + (paddr - xen_dma_seg[nr_seg].phys_addr); > } > > static phys_addr_t xen_bus_to_phys(dma_addr_t baddr) > { > - return machine_to_phys(XMADDR(baddr)).paddr; > + if (xen_feature(XENFEAT_auto_translated_physmap)) { > + struct xen_dma_info *dma = xen_get_dma_info(baddr); > + if (dma == NULL) > + return INVALID_ADDRESS; > + else > + return dma->phys_addr + (baddr - dma->dma_addr); > + } else > + return machine_to_phys(XMADDR(baddr)).paddr; > } > > static dma_addr_t xen_virt_to_bus(void *address) > @@ -107,6 +221,9 @@ static int is_xen_swiotlb_buffer(dma_addr_t dma_addr) > unsigned long pfn = mfn_to_local_pfn(mfn); > phys_addr_t paddr; > > + if (xen_feature(XENFEAT_auto_translated_physmap)) > + return 1; > + > /* If the address is outside our domain, it CAN > * have the same virtual address as another address > * in our domain. Therefore _only_ check address within our domain. > @@ -124,13 +241,12 @@ static int max_dma_bits = 32; > static int > xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) > { > - int i, rc; > + int i, j, rc; > int dma_bits; > - dma_addr_t dma_handle; > > dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT; > > - i = 0; > + i = j = 0; > do { > int slabs = min(nslabs - i, (unsigned long)IO_TLB_SEGSIZE); > > @@ -138,12 +254,18 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) > rc = xen_create_contiguous_region( > (unsigned long)buf + (i << IO_TLB_SHIFT), > get_order(slabs << IO_TLB_SHIFT), > - dma_bits, &dma_handle); > + dma_bits, &xen_dma_seg[j].dma_addr); > } while (rc && dma_bits++ < max_dma_bits); > if (rc) > return rc; > > + xen_dma_seg[j].phys_addr = virt_to_phys(buf + (i << IO_TLB_SHIFT)); > + xen_dma_seg[j].size = slabs << IO_TLB_SHIFT; > + rc = xen_dma_add_entry(&xen_dma_seg[j]); > + if (rc != 0) > + return rc; > i += slabs; > + j++; > } while (i < nslabs); > return 0; > } > @@ -193,9 +315,10 @@ retry: > /* > * Get IO TLB memory from any location. > */ > - if (early) > + if (early) { > xen_io_tlb_start = alloc_bootmem_pages(PAGE_ALIGN(bytes)); > - else { > + xen_dma_seg = alloc_bootmem(sizeof(struct xen_dma_info) * NR_DMA_SEGS);So what if the user over-wrote the size of the SWITOLB? Meaning the nslabs is different? Won''t that blow up in xen_swiotlb_fixup if we try to dereference past the array?> + } else { > #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) > #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) > while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { > @@ -210,6 +333,8 @@ retry: > xen_io_tlb_nslabs = SLABS_PER_PAGE << order; > bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT; > } > + xen_dma_seg = kzalloc(sizeof(struct xen_dma_info) * NR_DMA_SEGS, > + GFP_KERNEL);Ditto.> } > if (!xen_io_tlb_start) { > m_ret = XEN_SWIOTLB_ENOMEM; > @@ -232,7 +357,6 @@ retry: > m_ret = XEN_SWIOTLB_EFIXUP; > goto error; > } > - start_dma_addr = xen_virt_to_bus(xen_io_tlb_start); > if (early) { > if (swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs, > verbose)) > @@ -290,7 +414,8 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, > > phys = virt_to_phys(ret); > dev_addr = xen_phys_to_bus(phys); > - if (((dev_addr + size - 1 <= dma_mask)) && > + if (!xen_feature(XENFEAT_auto_translated_physmap) && > + ((dev_addr + size - 1 <= dma_mask)) && > !range_straddles_page_boundary(phys, size)) > *dma_handle = dev_addr; > else { > @@ -321,8 +446,9 @@ xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, > > phys = virt_to_phys(vaddr); > > - if (((dev_addr + size - 1 > dma_mask)) || > - range_straddles_page_boundary(phys, size)) > + if (xen_feature(XENFEAT_auto_translated_physmap) || > + (((dev_addr + size - 1 > dma_mask)) || > + range_straddles_page_boundary(phys, size))) > xen_destroy_contiguous_region((unsigned long)vaddr, order); > > free_pages((unsigned long)vaddr, order); > @@ -351,14 +477,15 @@ dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page, > * we can safely return the device addr and not worry about bounce > * buffering it. > */ > - if (dma_capable(dev, dev_addr, size) && > + if (!xen_feature(XENFEAT_auto_translated_physmap) && > + dma_capable(dev, dev_addr, size) && > !range_straddles_page_boundary(phys, size) && !swiotlb_force) > return dev_addr; > > /* > * Oh well, have to allocate and map a bounce buffer. > */ > - map = swiotlb_tbl_map_single(dev, start_dma_addr, phys, size, dir); > + map = swiotlb_tbl_map_single(dev, xen_dma_seg[0].dma_addr, phys, size, dir);That [0] really deserves a comment.> if (map == SWIOTLB_MAP_ERROR) > return DMA_ERROR_CODE; > > @@ -494,10 +621,11 @@ xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, > dma_addr_t dev_addr = xen_phys_to_bus(paddr); > > if (swiotlb_force || > + xen_feature(XENFEAT_auto_translated_physmap) || > !dma_capable(hwdev, dev_addr, sg->length) || > range_straddles_page_boundary(paddr, sg->length)) { > phys_addr_t map = swiotlb_tbl_map_single(hwdev, > - start_dma_addr, > + xen_dma_seg[0].dma_addr,Ditto> sg_phys(sg), > sg->length, > dir); > -- > 1.7.2.5 >
Konrad Rzeszutek Wilk
2013-Aug-09 15:50 UTC
Re: [PATCH v3 07/10] xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf
On Fri, Aug 09, 2013 at 11:36:06AM -0400, Konrad Rzeszutek Wilk wrote:> On Mon, Aug 05, 2013 at 05:30:53PM +0100, Stefano Stabellini wrote: > > XENMEM_exchange can''t be used by autotranslate guests because of two > > severe limitations: > > > > - it does not copy back the mfns into the out field for autotranslate > > guests; > > > > - it does not guarantee that the hypervisor won''t change the p2m > > mappings for the exchanged pages while the guest is using them. Xen > > never promises to keep the p2m mapping stable for autotranslate guests > > in general. In practice it won''t happen unless one uses uncommon > > features like memory sharing or paging. > > > > To overcome these problems I am introducing two new hypercalls. > > You should also refer to the git or c/s of where it is implemented > in the hypervisor (once it lands there of course). > > > > Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> > > --- > > include/xen/interface/memory.h | 62 ++++++++++++++++++++++++++++++++++++++++ > > 1 files changed, 62 insertions(+), 0 deletions(-) > > > > diff --git a/include/xen/interface/memory.h b/include/xen/interface/memory.h > > index 2ecfe4f..ffd7f4e 100644 > > --- a/include/xen/interface/memory.h > > +++ b/include/xen/interface/memory.h > > @@ -263,4 +263,66 @@ struct xen_remove_from_physmap { > > }; > > DEFINE_GUEST_HANDLE_STRUCT(xen_remove_from_physmap); > > > > +#define XENMEM_get_dma_buf 26 > > +/* > > + * This hypercall is similar to XENMEM_exchange: it exchanges the pages > > + * passed in with a new set of pages, contiguous and under 4G if so > > The "under 4G" is not true. It is based on the bit value. The user could > request it be under "1G" if they wanted.Or say below 16GB. Point here is that I was thinking you should just mention which of the parameters is needed to set this.> > > + * requested. The new pages are going to be "pinned": it''s guaranteed > > + * that their p2m mapping won''t be changed until explicitly "unpinned". > > What if you try to balloon them out? What happens then? Does that > unpin them automatically? > > What if I use said "pin" page for grants? Can they be shared with another > guest? > > > + * If return code is zero then @out.extent_list provides the MFNs of the > > + * newly-allocated memory. Returns zero on complete success, otherwise > > + * a negative error code. > > Ahem. Which ones? I know you didn''t like the existing grant code b/c it > returned some general error. Would it make sense to say which are ones > are expected? > > > + * On complete success then always @nr_exchanged == @in.nr_extents. On > > + * partial success @nr_exchanged indicates how much work was done. > > And no error? > > + */ > > +struct xen_get_dma_buf { > > + /* > > + * [IN] Details of memory extents to be exchanged (GMFN bases). > > + * Note that @in.address_bits is ignored and unused. > > Ohhhh, why? What if the user wants to be it under 2G?Looking a bit more at the code revealed that we stick that in the @out.address_bits> > > + */ > > + struct xen_memory_reservation in; > > + > > + /* > > + * [IN/OUT] Details of new memory extents. > > + * We require that: > > + * 1. @in.domid == @out.domid > > + * 2. @in.nr_extents << @in.extent_order == > > + * @out.nr_extents << @out.extent_order > > + * 3. @in.extent_start and @out.extent_start lists must not overlap > > + * 4. @out.extent_start lists GPFN bases to be populated > > + * 5. @out.extent_start is overwritten with allocated GMFN bases.. which you should document, otherwise the hypervisor might try to give you pages under 2^0..> > + */ > > + struct xen_memory_reservation out; > > + > > + /* > > + * [OUT] Number of input extents that were successfully exchanged: > > + * 1. The first @nr_exchanged input extents were successfully > > + * deallocated. > > + * 2. The corresponding first entries in the output extent list correctly > > + * indicate the GMFNs that were successfully exchanged. > > + * 3. All other input and output extents are untouched. > > + * 4. If not all input exents are exchanged then the return code of this > > + * command will be non-zero. > > + * 5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER!as this says the initial value is zero.
Stefano Stabellini
2013-Aug-14 13:01 UTC
Re: [PATCH v3 09/10] swiotlb-xen: support autotranslate guests
On Fri, 9 Aug 2013, Konrad Rzeszutek Wilk wrote:> > @@ -193,9 +315,10 @@ retry: > > /* > > * Get IO TLB memory from any location. > > */ > > - if (early) > > + if (early) { > > xen_io_tlb_start = alloc_bootmem_pages(PAGE_ALIGN(bytes)); > > - else { > > + xen_dma_seg = alloc_bootmem(sizeof(struct xen_dma_info) * NR_DMA_SEGS); > > So what if the user over-wrote the size of the SWITOLB? Meaning the nslabs is different? > Won''t that blow up in xen_swiotlb_fixup if we try to dereference past the array?The definition of NR_DMA_SEGS is: #define NR_DMA_SEGS ((xen_io_tlb_nslabs + IO_TLB_SEGSIZE - 1) / IO_TLB_SEGSIZE) xen_io_tlb_nslabs is calculated from io_tlb_nslabs. If the user increases io_tlb_nslabs, then xen_io_tlb_nslabs and NR_DMA_SEGS are also going to be bigger.> > @@ -351,14 +477,15 @@ dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page, > > * we can safely return the device addr and not worry about bounce > > * buffering it. > > */ > > - if (dma_capable(dev, dev_addr, size) && > > + if (!xen_feature(XENFEAT_auto_translated_physmap) && > > + dma_capable(dev, dev_addr, size) && > > !range_straddles_page_boundary(phys, size) && !swiotlb_force) > > return dev_addr; > > > > /* > > * Oh well, have to allocate and map a bounce buffer. > > */ > > - map = swiotlb_tbl_map_single(dev, start_dma_addr, phys, size, dir); > > + map = swiotlb_tbl_map_single(dev, xen_dma_seg[0].dma_addr, phys, size, dir); > > That [0] really deserves a comment.xen_dma_seg[0].dma_addr corresponds exactly to start_dma_addr, the first machine address of the swiotlb buffer. I think that reason why we pass the dma address of the first slab is that we want to allocate the bounce buffer from any of the slabs in io_tlb_start-io_tlb_end.
Stefano Stabellini
2013-Aug-14 16:41 UTC
Re: [PATCH v3 07/10] xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf
On Fri, 9 Aug 2013, Konrad Rzeszutek Wilk wrote:> > > +struct xen_get_dma_buf { > > > + /* > > > + * [IN] Details of memory extents to be exchanged (GMFN bases). > > > + * Note that @in.address_bits is ignored and unused. > > > > Ohhhh, why? What if the user wants to be it under 2G? > > Looking a bit more at the code revealed that we stick that in the > @out.address_bits > > > > > + */ > > > + struct xen_memory_reservation in; > > > + > > > + /* > > > + * [IN/OUT] Details of new memory extents. > > > + * We require that: > > > + * 1. @in.domid == @out.domid > > > + * 2. @in.nr_extents << @in.extent_order == > > > + * @out.nr_extents << @out.extent_order > > > + * 3. @in.extent_start and @out.extent_start lists must not overlap > > > + * 4. @out.extent_start lists GPFN bases to be populated > > > + * 5. @out.extent_start is overwritten with allocated GMFN bases > > .. which you should document, otherwise the hypervisor might try to give > you pages under 2^0..Yes, you are right. I''ll add a note about out.address_bits.> > > + */ > > > + struct xen_memory_reservation out; > > > + > > > + /* > > > + * [OUT] Number of input extents that were successfully exchanged: > > > + * 1. The first @nr_exchanged input extents were successfully > > > + * deallocated. > > > + * 2. The corresponding first entries in the output extent list correctly > > > + * indicate the GMFNs that were successfully exchanged. > > > + * 3. All other input and output extents are untouched. > > > + * 4. If not all input exents are exchanged then the return code of this > > > + * command will be non-zero. > > > + * 5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER! > > as this says the initial value is zero.This is referring to nr_exchanged.
Stefano Stabellini
2013-Aug-14 16:43 UTC
Re: [PATCH v3 07/10] xen: introduce XENMEM_get_dma_buf and xen_put_dma_buf
On Fri, 9 Aug 2013, Konrad Rzeszutek Wilk wrote:> > + * requested. The new pages are going to be "pinned": it''s guaranteed > > + * that their p2m mapping won''t be changed until explicitly "unpinned". > > What if you try to balloon them out? What happens then? Does that > unpin them automatically? > > What if I use said "pin" page for grants? Can they be shared with another > guest?I''ll mention that only normal guest r/w memory can be pinned.> > + * If return code is zero then @out.extent_list provides the MFNs of the > > + * newly-allocated memory. Returns zero on complete success, otherwise > > + * a negative error code. > > Ahem. Which ones? I know you didn''t like the existing grant code b/c it > returned some general error. Would it make sense to say which are ones > are expected?I''ll do that.> > + * On complete success then always @nr_exchanged == @in.nr_extents. On > > + * partial success @nr_exchanged indicates how much work was done. > > And no error? > > + */ > > +struct xen_get_dma_buf { > > + /* > > + * [IN] Details of memory extents to be exchanged (GMFN bases). > > + * Note that @in.address_bits is ignored and unused. > > Ohhhh, why? What if the user wants to be it under 2G?As per the other email, that goes in out.address_bits. I''ll add a note.> > + */ > > + struct xen_memory_reservation in; > > + > > + /* > > + * [IN/OUT] Details of new memory extents. > > + * We require that: > > + * 1. @in.domid == @out.domid > > + * 2. @in.nr_extents << @in.extent_order == > > + * @out.nr_extents << @out.extent_order > > + * 3. @in.extent_start and @out.extent_start lists must not overlap > > + * 4. @out.extent_start lists GPFN bases to be populated > > + * 5. @out.extent_start is overwritten with allocated GMFN bases > > + */ > > + struct xen_memory_reservation out; > > + > > + /* > > + * [OUT] Number of input extents that were successfully exchanged: > > + * 1. The first @nr_exchanged input extents were successfully > > + * deallocated. > > + * 2. The corresponding first entries in the output extent list correctly > > + * indicate the GMFNs that were successfully exchanged. > > + * 3. All other input and output extents are untouched. > > + * 4. If not all input exents are exchanged then the return code of this > > + * command will be non-zero. > > + * 5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER! > > + */ > > + xen_ulong_t nr_exchanged; > > +}; > > +DEFINE_GUEST_HANDLE_STRUCT(xen_get_dma_buf); > > + > > +#define XENMEM_put_dma_buf 27 > > +/* > > + * XENMEM_put_dma_buf unpins a set of pages, previously pinned by > > + * XENMEM_get_dma_buf. After this call the p2m mapping of the pages can > > + * be transparently changed by the hypervisor, as usual. The pages are > > + * still accessible from the guest. > > I don''t understand what ''pinned'' means. What is a ''pinned'' page versus > a normal page? Is this like the pagetables which must be _RO?A pinned page is just a normal page that the hypervisor promises is going to keep having the same machine address until unpinned.