diff options
author | Stefano Stabellini <stefano.stabellini@eu.citrix.com> | 2013-10-10 09:41:10 -0400 |
---|---|---|
committer | Stefano Stabellini <stefano.stabellini@eu.citrix.com> | 2013-10-10 09:41:10 -0400 |
commit | 1b65c4e5a9af1a1c61e792e2d0ed481e0c1f21a9 (patch) | |
tree | 15c7b03e6efede8905f3729de488341c9a9da81b | |
parent | d6fe76c58c358498b91d21f0ca8054f6aa6e672d (diff) |
swiotlb-xen: use xen_alloc/free_coherent_pages
Use xen_alloc_coherent_pages and xen_free_coherent_pages to allocate or
free coherent pages.
We need to be careful handling the pointer returned by
xen_alloc_coherent_pages, because on ARM the pointer is not equal to
phys_to_virt(*dma_handle). In fact virt_to_phys only works for kernel
direct mapped RAM memory.
In ARM case the pointer could be an ioremap address, therefore passing
it to virt_to_phys would give you another physical address that doesn't
correspond to it.
Make xen_create_contiguous_region take a phys_addr_t as start parameter to
avoid the virt_to_phys calls which would be incorrect.
Changes in v6:
- remove extra spaces.
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
-rw-r--r-- | arch/arm/xen/mm.c | 6 | ||||
-rw-r--r-- | arch/x86/xen/mmu.c | 7 | ||||
-rw-r--r-- | drivers/xen/swiotlb-xen.c | 31 | ||||
-rw-r--r-- | include/xen/xen-ops.h | 4 |
4 files changed, 31 insertions, 17 deletions
diff --git a/arch/arm/xen/mm.c b/arch/arm/xen/mm.c index 0d69b874d249..b0e77de99148 100644 --- a/arch/arm/xen/mm.c +++ b/arch/arm/xen/mm.c | |||
@@ -16,7 +16,7 @@ | |||
16 | #include <asm/xen/hypercall.h> | 16 | #include <asm/xen/hypercall.h> |
17 | #include <asm/xen/interface.h> | 17 | #include <asm/xen/interface.h> |
18 | 18 | ||
19 | int xen_create_contiguous_region(unsigned long vstart, unsigned int order, | 19 | int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, |
20 | unsigned int address_bits, | 20 | unsigned int address_bits, |
21 | dma_addr_t *dma_handle) | 21 | dma_addr_t *dma_handle) |
22 | { | 22 | { |
@@ -24,12 +24,12 @@ int xen_create_contiguous_region(unsigned long vstart, unsigned int order, | |||
24 | return -EINVAL; | 24 | return -EINVAL; |
25 | 25 | ||
26 | /* we assume that dom0 is mapped 1:1 for now */ | 26 | /* we assume that dom0 is mapped 1:1 for now */ |
27 | *dma_handle = virt_to_phys(pstart); | 27 | *dma_handle = pstart; |
28 | return 0; | 28 | return 0; |
29 | } | 29 | } |
30 | EXPORT_SYMBOL_GPL(xen_create_contiguous_region); | 30 | EXPORT_SYMBOL_GPL(xen_create_contiguous_region); |
31 | 31 | ||
32 | void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order) | 32 | void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order) |
33 | { | 33 | { |
34 | return; | 34 | return; |
35 | } | 35 | } |
diff --git a/arch/x86/xen/mmu.c b/arch/x86/xen/mmu.c index 6c34d7c03d5b..883088368ff0 100644 --- a/arch/x86/xen/mmu.c +++ b/arch/x86/xen/mmu.c | |||
@@ -2328,13 +2328,14 @@ static int xen_exchange_memory(unsigned long extents_in, unsigned int order_in, | |||
2328 | return success; | 2328 | return success; |
2329 | } | 2329 | } |
2330 | 2330 | ||
2331 | int xen_create_contiguous_region(unsigned long vstart, unsigned int order, | 2331 | int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, |
2332 | unsigned int address_bits, | 2332 | unsigned int address_bits, |
2333 | dma_addr_t *dma_handle) | 2333 | dma_addr_t *dma_handle) |
2334 | { | 2334 | { |
2335 | unsigned long *in_frames = discontig_frames, out_frame; | 2335 | unsigned long *in_frames = discontig_frames, out_frame; |
2336 | unsigned long flags; | 2336 | unsigned long flags; |
2337 | int success; | 2337 | int success; |
2338 | unsigned long vstart = (unsigned long)phys_to_virt(pstart); | ||
2338 | 2339 | ||
2339 | /* | 2340 | /* |
2340 | * Currently an auto-translated guest will not perform I/O, nor will | 2341 | * Currently an auto-translated guest will not perform I/O, nor will |
@@ -2374,11 +2375,12 @@ int xen_create_contiguous_region(unsigned long vstart, unsigned int order, | |||
2374 | } | 2375 | } |
2375 | EXPORT_SYMBOL_GPL(xen_create_contiguous_region); | 2376 | EXPORT_SYMBOL_GPL(xen_create_contiguous_region); |
2376 | 2377 | ||
2377 | void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order) | 2378 | void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order) |
2378 | { | 2379 | { |
2379 | unsigned long *out_frames = discontig_frames, in_frame; | 2380 | unsigned long *out_frames = discontig_frames, in_frame; |
2380 | unsigned long flags; | 2381 | unsigned long flags; |
2381 | int success; | 2382 | int success; |
2383 | unsigned long vstart; | ||
2382 | 2384 | ||
2383 | if (xen_feature(XENFEAT_auto_translated_physmap)) | 2385 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
2384 | return; | 2386 | return; |
@@ -2386,6 +2388,7 @@ void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order) | |||
2386 | if (unlikely(order > MAX_CONTIG_ORDER)) | 2388 | if (unlikely(order > MAX_CONTIG_ORDER)) |
2387 | return; | 2389 | return; |
2388 | 2390 | ||
2391 | vstart = (unsigned long)phys_to_virt(pstart); | ||
2389 | memset((void *) vstart, 0, PAGE_SIZE << order); | 2392 | memset((void *) vstart, 0, PAGE_SIZE << order); |
2390 | 2393 | ||
2391 | spin_lock_irqsave(&xen_reservation_lock, flags); | 2394 | spin_lock_irqsave(&xen_reservation_lock, flags); |
diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index d8ef0bf577d2..189b8db5c983 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c | |||
@@ -43,6 +43,7 @@ | |||
43 | #include <xen/xen-ops.h> | 43 | #include <xen/xen-ops.h> |
44 | #include <xen/hvc-console.h> | 44 | #include <xen/hvc-console.h> |
45 | #include <asm/dma-mapping.h> | 45 | #include <asm/dma-mapping.h> |
46 | #include <asm/xen/page-coherent.h> | ||
46 | /* | 47 | /* |
47 | * Used to do a quick range check in swiotlb_tbl_unmap_single and | 48 | * Used to do a quick range check in swiotlb_tbl_unmap_single and |
48 | * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this | 49 | * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this |
@@ -142,6 +143,7 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) | |||
142 | int i, rc; | 143 | int i, rc; |
143 | int dma_bits; | 144 | int dma_bits; |
144 | dma_addr_t dma_handle; | 145 | dma_addr_t dma_handle; |
146 | phys_addr_t p = virt_to_phys(buf); | ||
145 | 147 | ||
146 | dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT; | 148 | dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT; |
147 | 149 | ||
@@ -151,7 +153,7 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs) | |||
151 | 153 | ||
152 | do { | 154 | do { |
153 | rc = xen_create_contiguous_region( | 155 | rc = xen_create_contiguous_region( |
154 | (unsigned long)buf + (i << IO_TLB_SHIFT), | 156 | p + (i << IO_TLB_SHIFT), |
155 | get_order(slabs << IO_TLB_SHIFT), | 157 | get_order(slabs << IO_TLB_SHIFT), |
156 | dma_bits, &dma_handle); | 158 | dma_bits, &dma_handle); |
157 | } while (rc && dma_bits++ < max_dma_bits); | 159 | } while (rc && dma_bits++ < max_dma_bits); |
@@ -279,7 +281,6 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, | |||
279 | void *ret; | 281 | void *ret; |
280 | int order = get_order(size); | 282 | int order = get_order(size); |
281 | u64 dma_mask = DMA_BIT_MASK(32); | 283 | u64 dma_mask = DMA_BIT_MASK(32); |
282 | unsigned long vstart; | ||
283 | phys_addr_t phys; | 284 | phys_addr_t phys; |
284 | dma_addr_t dev_addr; | 285 | dma_addr_t dev_addr; |
285 | 286 | ||
@@ -294,8 +295,12 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, | |||
294 | if (dma_alloc_from_coherent(hwdev, size, dma_handle, &ret)) | 295 | if (dma_alloc_from_coherent(hwdev, size, dma_handle, &ret)) |
295 | return ret; | 296 | return ret; |
296 | 297 | ||
297 | vstart = __get_free_pages(flags, order); | 298 | /* On ARM this function returns an ioremap'ped virtual address for |
298 | ret = (void *)vstart; | 299 | * which virt_to_phys doesn't return the corresponding physical |
300 | * address. In fact on ARM virt_to_phys only works for kernel direct | ||
301 | * mapped RAM memory. Also see comment below. | ||
302 | */ | ||
303 | ret = xen_alloc_coherent_pages(hwdev, size, dma_handle, flags, attrs); | ||
299 | 304 | ||
300 | if (!ret) | 305 | if (!ret) |
301 | return ret; | 306 | return ret; |
@@ -303,15 +308,19 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, | |||
303 | if (hwdev && hwdev->coherent_dma_mask) | 308 | if (hwdev && hwdev->coherent_dma_mask) |
304 | dma_mask = dma_alloc_coherent_mask(hwdev, flags); | 309 | dma_mask = dma_alloc_coherent_mask(hwdev, flags); |
305 | 310 | ||
306 | phys = virt_to_phys(ret); | 311 | /* At this point dma_handle is the physical address, next we are |
312 | * going to set it to the machine address. | ||
313 | * Do not use virt_to_phys(ret) because on ARM it doesn't correspond | ||
314 | * to *dma_handle. */ | ||
315 | phys = *dma_handle; | ||
307 | dev_addr = xen_phys_to_bus(phys); | 316 | dev_addr = xen_phys_to_bus(phys); |
308 | if (((dev_addr + size - 1 <= dma_mask)) && | 317 | if (((dev_addr + size - 1 <= dma_mask)) && |
309 | !range_straddles_page_boundary(phys, size)) | 318 | !range_straddles_page_boundary(phys, size)) |
310 | *dma_handle = dev_addr; | 319 | *dma_handle = dev_addr; |
311 | else { | 320 | else { |
312 | if (xen_create_contiguous_region(vstart, order, | 321 | if (xen_create_contiguous_region(phys, order, |
313 | fls64(dma_mask), dma_handle) != 0) { | 322 | fls64(dma_mask), dma_handle) != 0) { |
314 | free_pages(vstart, order); | 323 | xen_free_coherent_pages(hwdev, size, ret, (dma_addr_t)phys, attrs); |
315 | return NULL; | 324 | return NULL; |
316 | } | 325 | } |
317 | } | 326 | } |
@@ -334,13 +343,15 @@ xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, | |||
334 | if (hwdev && hwdev->coherent_dma_mask) | 343 | if (hwdev && hwdev->coherent_dma_mask) |
335 | dma_mask = hwdev->coherent_dma_mask; | 344 | dma_mask = hwdev->coherent_dma_mask; |
336 | 345 | ||
337 | phys = virt_to_phys(vaddr); | 346 | /* do not use virt_to_phys because on ARM it doesn't return you the |
347 | * physical address */ | ||
348 | phys = xen_bus_to_phys(dev_addr); | ||
338 | 349 | ||
339 | if (((dev_addr + size - 1 > dma_mask)) || | 350 | if (((dev_addr + size - 1 > dma_mask)) || |
340 | range_straddles_page_boundary(phys, size)) | 351 | range_straddles_page_boundary(phys, size)) |
341 | xen_destroy_contiguous_region((unsigned long)vaddr, order); | 352 | xen_destroy_contiguous_region(phys, order); |
342 | 353 | ||
343 | free_pages((unsigned long)vaddr, order); | 354 | xen_free_coherent_pages(hwdev, size, vaddr, (dma_addr_t)phys, attrs); |
344 | } | 355 | } |
345 | EXPORT_SYMBOL_GPL(xen_swiotlb_free_coherent); | 356 | EXPORT_SYMBOL_GPL(xen_swiotlb_free_coherent); |
346 | 357 | ||
diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index 9ef704d3a9dd..fb2ea8f26552 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h | |||
@@ -19,11 +19,11 @@ void xen_arch_resume(void); | |||
19 | int xen_setup_shutdown_event(void); | 19 | int xen_setup_shutdown_event(void); |
20 | 20 | ||
21 | extern unsigned long *xen_contiguous_bitmap; | 21 | extern unsigned long *xen_contiguous_bitmap; |
22 | int xen_create_contiguous_region(unsigned long vstart, unsigned int order, | 22 | int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, |
23 | unsigned int address_bits, | 23 | unsigned int address_bits, |
24 | dma_addr_t *dma_handle); | 24 | dma_addr_t *dma_handle); |
25 | 25 | ||
26 | void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order); | 26 | void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order); |
27 | 27 | ||
28 | struct vm_area_struct; | 28 | struct vm_area_struct; |
29 | int xen_remap_domain_mfn_range(struct vm_area_struct *vma, | 29 | int xen_remap_domain_mfn_range(struct vm_area_struct *vma, |