Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 0ddbccd | 2008-09-25 15:59:19 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mm/dma-mapping.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2000-2004 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * DMA uncached mapping support. |
| 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/dma-mapping.h> |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 20 | #include <linux/dma-contiguous.h> |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 21 | #include <linux/highmem.h> |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 22 | #include <linux/memblock.h> |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 24 | #include <linux/iommu.h> |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 25 | #include <linux/io.h> |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
Alessandro Rubini | 158e8bf | 2012-06-24 12:46:26 +0100 | [diff] [blame] | 27 | #include <linux/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Lennert Buytenhek | 23759dc | 2006-04-02 00:07:39 +0100 | [diff] [blame] | 29 | #include <asm/memory.h> |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 30 | #include <asm/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/tlbflush.h> |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 33 | #include <asm/mach/arch.h> |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 34 | #include <asm/dma-iommu.h> |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 35 | #include <asm/mach/map.h> |
| 36 | #include <asm/system_info.h> |
| 37 | #include <asm/dma-contiguous.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 39 | #include "mm.h" |
| 40 | |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 41 | /* |
| 42 | * The DMA API is built upon the notion of "buffer ownership". A buffer |
| 43 | * is either exclusively owned by the CPU (and therefore may be accessed |
| 44 | * by it) or exclusively owned by the DMA device. These helper functions |
| 45 | * represent the transitions between these two ownership states. |
| 46 | * |
| 47 | * Note, however, that on later ARMs, this notion does not work due to |
| 48 | * speculative prefetches. We model our approach on the assumption that |
| 49 | * the CPU does do speculative prefetches, which means we clean caches |
| 50 | * before transfers and delay cache invalidation until transfer completion. |
| 51 | * |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 52 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 53 | static void __dma_page_cpu_to_dev(struct page *, unsigned long, |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 54 | size_t, enum dma_data_direction); |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 55 | static void __dma_page_dev_to_cpu(struct page *, unsigned long, |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 56 | size_t, enum dma_data_direction); |
| 57 | |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 58 | /** |
| 59 | * arm_dma_map_page - map a portion of a page for streaming DMA |
| 60 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 61 | * @page: page that buffer resides in |
| 62 | * @offset: offset into page for start of buffer |
| 63 | * @size: size of buffer to map |
| 64 | * @dir: DMA transfer direction |
| 65 | * |
| 66 | * Ensure that any data held in the cache is appropriately discarded |
| 67 | * or written back. |
| 68 | * |
| 69 | * The device owns this memory once this call has completed. The CPU |
| 70 | * can regain ownership by calling dma_unmap_page(). |
| 71 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 72 | static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 73 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 74 | struct dma_attrs *attrs) |
| 75 | { |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 76 | if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 77 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 78 | return pfn_to_dma(dev, page_to_pfn(page)) + offset; |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 81 | static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page, |
| 82 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 83 | struct dma_attrs *attrs) |
| 84 | { |
| 85 | return pfn_to_dma(dev, page_to_pfn(page)) + offset; |
| 86 | } |
| 87 | |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 88 | /** |
| 89 | * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page() |
| 90 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 91 | * @handle: DMA address of buffer |
| 92 | * @size: size of buffer (same as passed to dma_map_page) |
| 93 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 94 | * |
| 95 | * Unmap a page streaming mode DMA translation. The handle and size |
| 96 | * must match what was provided in the previous dma_map_page() call. |
| 97 | * All other usages are undefined. |
| 98 | * |
| 99 | * After this call, reads by the CPU to the buffer are guaranteed to see |
| 100 | * whatever the device wrote there. |
| 101 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 102 | static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 103 | size_t size, enum dma_data_direction dir, |
| 104 | struct dma_attrs *attrs) |
| 105 | { |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 106 | if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 107 | __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)), |
| 108 | handle & ~PAGE_MASK, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 111 | static void arm_dma_sync_single_for_cpu(struct device *dev, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 112 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 113 | { |
| 114 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 115 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 116 | __dma_page_dev_to_cpu(page, offset, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 119 | static void arm_dma_sync_single_for_device(struct device *dev, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 120 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 121 | { |
| 122 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 123 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 124 | __dma_page_cpu_to_dev(page, offset, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 127 | struct dma_map_ops arm_dma_ops = { |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 128 | .alloc = arm_dma_alloc, |
| 129 | .free = arm_dma_free, |
| 130 | .mmap = arm_dma_mmap, |
Marek Szyprowski | dc2832e | 2012-06-13 10:01:15 +0200 | [diff] [blame] | 131 | .get_sgtable = arm_dma_get_sgtable, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 132 | .map_page = arm_dma_map_page, |
| 133 | .unmap_page = arm_dma_unmap_page, |
| 134 | .map_sg = arm_dma_map_sg, |
| 135 | .unmap_sg = arm_dma_unmap_sg, |
| 136 | .sync_single_for_cpu = arm_dma_sync_single_for_cpu, |
| 137 | .sync_single_for_device = arm_dma_sync_single_for_device, |
| 138 | .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu, |
| 139 | .sync_sg_for_device = arm_dma_sync_sg_for_device, |
| 140 | .set_dma_mask = arm_dma_set_mask, |
| 141 | }; |
| 142 | EXPORT_SYMBOL(arm_dma_ops); |
| 143 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 144 | static void *arm_coherent_dma_alloc(struct device *dev, size_t size, |
| 145 | dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs); |
| 146 | static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 147 | dma_addr_t handle, struct dma_attrs *attrs); |
| 148 | |
| 149 | struct dma_map_ops arm_coherent_dma_ops = { |
| 150 | .alloc = arm_coherent_dma_alloc, |
| 151 | .free = arm_coherent_dma_free, |
| 152 | .mmap = arm_dma_mmap, |
| 153 | .get_sgtable = arm_dma_get_sgtable, |
| 154 | .map_page = arm_coherent_dma_map_page, |
| 155 | .map_sg = arm_dma_map_sg, |
| 156 | .set_dma_mask = arm_dma_set_mask, |
| 157 | }; |
| 158 | EXPORT_SYMBOL(arm_coherent_dma_ops); |
| 159 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 160 | static u64 get_coherent_dma_mask(struct device *dev) |
| 161 | { |
Russell King | 4dcfa60 | 2013-07-09 12:14:49 +0100 | [diff] [blame^] | 162 | u64 mask = (u64)DMA_BIT_MASK(32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 164 | if (dev) { |
| 165 | mask = dev->coherent_dma_mask; |
| 166 | |
| 167 | /* |
| 168 | * Sanity check the DMA mask - it must be non-zero, and |
| 169 | * must be able to be satisfied by a DMA allocation. |
| 170 | */ |
| 171 | if (mask == 0) { |
| 172 | dev_warn(dev, "coherent DMA mask is unset\n"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
Russell King | 4dcfa60 | 2013-07-09 12:14:49 +0100 | [diff] [blame^] | 176 | /* |
| 177 | * If the mask allows for more memory than we can address, |
| 178 | * and we actually have that much memory, then fail the |
| 179 | * allocation. |
| 180 | */ |
| 181 | if (sizeof(mask) != sizeof(dma_addr_t) && |
| 182 | mask > (dma_addr_t)~0 && |
| 183 | dma_to_pfn(dev, ~0) > arm_dma_pfn_limit) { |
| 184 | dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n", |
| 185 | mask); |
| 186 | dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n"); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Now check that the mask, when translated to a PFN, |
| 192 | * fits within the allowable addresses which we can |
| 193 | * allocate. |
| 194 | */ |
| 195 | if (dma_to_pfn(dev, mask) < arm_dma_pfn_limit) { |
| 196 | dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n", |
| 197 | mask, |
| 198 | dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1, |
| 199 | arm_dma_pfn_limit + 1); |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return mask; |
| 205 | } |
| 206 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 207 | static void __dma_clear_buffer(struct page *page, size_t size) |
| 208 | { |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Ensure that the allocated pages are zeroed, and that any data |
| 211 | * lurking in the kernel direct-mapped region is invalidated. |
| 212 | */ |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 213 | if (PageHighMem(page)) { |
| 214 | phys_addr_t base = __pfn_to_phys(page_to_pfn(page)); |
| 215 | phys_addr_t end = base + size; |
| 216 | while (size > 0) { |
| 217 | void *ptr = kmap_atomic(page); |
| 218 | memset(ptr, 0, PAGE_SIZE); |
| 219 | dmac_flush_range(ptr, ptr + PAGE_SIZE); |
| 220 | kunmap_atomic(ptr); |
| 221 | page++; |
| 222 | size -= PAGE_SIZE; |
| 223 | } |
| 224 | outer_flush_range(base, end); |
| 225 | } else { |
| 226 | void *ptr = page_address(page); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 227 | memset(ptr, 0, size); |
| 228 | dmac_flush_range(ptr, ptr + size); |
| 229 | outer_flush_range(__pa(ptr), __pa(ptr) + size); |
| 230 | } |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 233 | /* |
| 234 | * Allocate a DMA buffer for 'dev' of size 'size' using the |
| 235 | * specified gfp mask. Note that 'size' must be page aligned. |
| 236 | */ |
| 237 | static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) |
| 238 | { |
| 239 | unsigned long order = get_order(size); |
| 240 | struct page *page, *p, *e; |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 241 | |
| 242 | page = alloc_pages(gfp, order); |
| 243 | if (!page) |
| 244 | return NULL; |
| 245 | |
| 246 | /* |
| 247 | * Now split the huge page and free the excess pages |
| 248 | */ |
| 249 | split_page(page, order); |
| 250 | for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) |
| 251 | __free_page(p); |
| 252 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 253 | __dma_clear_buffer(page, size); |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 254 | |
| 255 | return page; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Free a DMA buffer. 'size' must be page aligned. |
| 260 | */ |
| 261 | static void __dma_free_buffer(struct page *page, size_t size) |
| 262 | { |
| 263 | struct page *e = page + (size >> PAGE_SHIFT); |
| 264 | |
| 265 | while (page < e) { |
| 266 | __free_page(page); |
| 267 | page++; |
| 268 | } |
| 269 | } |
| 270 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 271 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | #ifdef CONFIG_HUGETLB_PAGE |
Catalin Marinas | 1355e2a | 2012-07-25 14:32:38 +0100 | [diff] [blame] | 273 | #warning ARM Coherent DMA allocator does not (yet) support huge TLB |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | #endif |
| 275 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 276 | static void *__alloc_from_contiguous(struct device *dev, size_t size, |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 277 | pgprot_t prot, struct page **ret_page, |
| 278 | const void *caller); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 279 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 280 | static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp, |
| 281 | pgprot_t prot, struct page **ret_page, |
| 282 | const void *caller); |
| 283 | |
| 284 | static void * |
| 285 | __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot, |
| 286 | const void *caller) |
| 287 | { |
| 288 | struct vm_struct *area; |
| 289 | unsigned long addr; |
| 290 | |
| 291 | /* |
| 292 | * DMA allocation can be mapped to user space, so lets |
| 293 | * set VM_USERMAP flags too. |
| 294 | */ |
| 295 | area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP, |
| 296 | caller); |
| 297 | if (!area) |
| 298 | return NULL; |
| 299 | addr = (unsigned long)area->addr; |
| 300 | area->phys_addr = __pfn_to_phys(page_to_pfn(page)); |
| 301 | |
| 302 | if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) { |
| 303 | vunmap((void *)addr); |
| 304 | return NULL; |
| 305 | } |
| 306 | return (void *)addr; |
| 307 | } |
| 308 | |
| 309 | static void __dma_free_remap(void *cpu_addr, size_t size) |
| 310 | { |
| 311 | unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP; |
| 312 | struct vm_struct *area = find_vm_area(cpu_addr); |
| 313 | if (!area || (area->flags & flags) != flags) { |
| 314 | WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr); |
| 315 | return; |
| 316 | } |
| 317 | unmap_kernel_range((unsigned long)cpu_addr, size); |
| 318 | vunmap(cpu_addr); |
| 319 | } |
| 320 | |
Marek Szyprowski | 6e5267a | 2012-08-20 11:19:25 +0200 | [diff] [blame] | 321 | #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K |
| 322 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 323 | struct dma_pool { |
| 324 | size_t size; |
| 325 | spinlock_t lock; |
| 326 | unsigned long *bitmap; |
| 327 | unsigned long nr_pages; |
| 328 | void *vaddr; |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 329 | struct page **pages; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 330 | }; |
| 331 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 332 | static struct dma_pool atomic_pool = { |
Marek Szyprowski | 6e5267a | 2012-08-20 11:19:25 +0200 | [diff] [blame] | 333 | .size = DEFAULT_DMA_COHERENT_POOL_SIZE, |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 334 | }; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 335 | |
| 336 | static int __init early_coherent_pool(char *p) |
| 337 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 338 | atomic_pool.size = memparse(p, &p); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | early_param("coherent_pool", early_coherent_pool); |
| 342 | |
Marek Szyprowski | 6e5267a | 2012-08-20 11:19:25 +0200 | [diff] [blame] | 343 | void __init init_dma_coherent_pool_size(unsigned long size) |
| 344 | { |
| 345 | /* |
| 346 | * Catch any attempt to set the pool size too late. |
| 347 | */ |
| 348 | BUG_ON(atomic_pool.vaddr); |
| 349 | |
| 350 | /* |
| 351 | * Set architecture specific coherent pool size only if |
| 352 | * it has not been changed by kernel command line parameter. |
| 353 | */ |
| 354 | if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE) |
| 355 | atomic_pool.size = size; |
| 356 | } |
| 357 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 358 | /* |
| 359 | * Initialise the coherent pool for atomic allocations. |
| 360 | */ |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 361 | static int __init atomic_pool_init(void) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 362 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 363 | struct dma_pool *pool = &atomic_pool; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 364 | pgprot_t prot = pgprot_dmacoherent(pgprot_kernel); |
Marek Szyprowski | 9d1400c | 2013-02-26 07:46:24 +0100 | [diff] [blame] | 365 | gfp_t gfp = GFP_KERNEL | GFP_DMA; |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 366 | unsigned long nr_pages = pool->size >> PAGE_SHIFT; |
| 367 | unsigned long *bitmap; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 368 | struct page *page; |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 369 | struct page **pages; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 370 | void *ptr; |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 371 | int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 372 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 373 | bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 374 | if (!bitmap) |
| 375 | goto no_bitmap; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 376 | |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 377 | pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL); |
| 378 | if (!pages) |
| 379 | goto no_pages; |
| 380 | |
Aneesh Kumar K.V | f825c73 | 2013-07-02 11:15:15 +0530 | [diff] [blame] | 381 | if (IS_ENABLED(CONFIG_DMA_CMA)) |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 382 | ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page, |
| 383 | atomic_pool_init); |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 384 | else |
Marek Szyprowski | 9d1400c | 2013-02-26 07:46:24 +0100 | [diff] [blame] | 385 | ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page, |
| 386 | atomic_pool_init); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 387 | if (ptr) { |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 388 | int i; |
| 389 | |
| 390 | for (i = 0; i < nr_pages; i++) |
| 391 | pages[i] = page + i; |
| 392 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 393 | spin_lock_init(&pool->lock); |
| 394 | pool->vaddr = ptr; |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 395 | pool->pages = pages; |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 396 | pool->bitmap = bitmap; |
| 397 | pool->nr_pages = nr_pages; |
| 398 | pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n", |
| 399 | (unsigned)pool->size / 1024); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 400 | return 0; |
| 401 | } |
Sachin Kamat | ec10665 | 2012-09-24 08:35:03 +0200 | [diff] [blame] | 402 | |
| 403 | kfree(pages); |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 404 | no_pages: |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 405 | kfree(bitmap); |
| 406 | no_bitmap: |
| 407 | pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n", |
| 408 | (unsigned)pool->size / 1024); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 409 | return -ENOMEM; |
| 410 | } |
| 411 | /* |
| 412 | * CMA is activated by core_initcall, so we must be called after it. |
| 413 | */ |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 414 | postcore_initcall(atomic_pool_init); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 415 | |
| 416 | struct dma_contig_early_reserve { |
| 417 | phys_addr_t base; |
| 418 | unsigned long size; |
| 419 | }; |
| 420 | |
| 421 | static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata; |
| 422 | |
| 423 | static int dma_mmu_remap_num __initdata; |
| 424 | |
| 425 | void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) |
| 426 | { |
| 427 | dma_mmu_remap[dma_mmu_remap_num].base = base; |
| 428 | dma_mmu_remap[dma_mmu_remap_num].size = size; |
| 429 | dma_mmu_remap_num++; |
| 430 | } |
| 431 | |
| 432 | void __init dma_contiguous_remap(void) |
| 433 | { |
| 434 | int i; |
| 435 | for (i = 0; i < dma_mmu_remap_num; i++) { |
| 436 | phys_addr_t start = dma_mmu_remap[i].base; |
| 437 | phys_addr_t end = start + dma_mmu_remap[i].size; |
| 438 | struct map_desc map; |
| 439 | unsigned long addr; |
| 440 | |
| 441 | if (end > arm_lowmem_limit) |
| 442 | end = arm_lowmem_limit; |
| 443 | if (start >= end) |
Chris Brand | 39f78e7 | 2012-08-07 14:01:14 +0200 | [diff] [blame] | 444 | continue; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 445 | |
| 446 | map.pfn = __phys_to_pfn(start); |
| 447 | map.virtual = __phys_to_virt(start); |
| 448 | map.length = end - start; |
| 449 | map.type = MT_MEMORY_DMA_READY; |
| 450 | |
| 451 | /* |
| 452 | * Clear previous low-memory mapping |
| 453 | */ |
| 454 | for (addr = __phys_to_virt(start); addr < __phys_to_virt(end); |
Vitaly Andrianov | 61f6c7a | 2012-05-14 13:49:56 -0400 | [diff] [blame] | 455 | addr += PMD_SIZE) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 456 | pmd_clear(pmd_off_k(addr)); |
| 457 | |
| 458 | iotable_init(&map, 1); |
| 459 | } |
| 460 | } |
| 461 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 462 | static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr, |
| 463 | void *data) |
| 464 | { |
| 465 | struct page *page = virt_to_page(addr); |
| 466 | pgprot_t prot = *(pgprot_t *)data; |
| 467 | |
| 468 | set_pte_ext(pte, mk_pte(page, prot), 0); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static void __dma_remap(struct page *page, size_t size, pgprot_t prot) |
| 473 | { |
| 474 | unsigned long start = (unsigned long) page_address(page); |
| 475 | unsigned end = start + size; |
| 476 | |
| 477 | apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 478 | flush_tlb_kernel_range(start, end); |
| 479 | } |
| 480 | |
| 481 | static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp, |
| 482 | pgprot_t prot, struct page **ret_page, |
| 483 | const void *caller) |
| 484 | { |
| 485 | struct page *page; |
| 486 | void *ptr; |
| 487 | page = __dma_alloc_buffer(dev, size, gfp); |
| 488 | if (!page) |
| 489 | return NULL; |
| 490 | |
| 491 | ptr = __dma_alloc_remap(page, size, gfp, prot, caller); |
| 492 | if (!ptr) { |
| 493 | __dma_free_buffer(page, size); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | *ret_page = page; |
| 498 | return ptr; |
| 499 | } |
| 500 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 501 | static void *__alloc_from_pool(size_t size, struct page **ret_page) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 502 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 503 | struct dma_pool *pool = &atomic_pool; |
| 504 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 505 | unsigned int pageno; |
| 506 | unsigned long flags; |
| 507 | void *ptr = NULL; |
Aaro Koskinen | e4ea691 | 2012-08-07 14:39:25 +0200 | [diff] [blame] | 508 | unsigned long align_mask; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 509 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 510 | if (!pool->vaddr) { |
| 511 | WARN(1, "coherent pool not initialised!\n"); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Align the region allocation - allocations from pool are rather |
| 517 | * small, so align them to their order in pages, minimum is a page |
| 518 | * size. This helps reduce fragmentation of the DMA space. |
| 519 | */ |
Aaro Koskinen | e4ea691 | 2012-08-07 14:39:25 +0200 | [diff] [blame] | 520 | align_mask = (1 << get_order(size)) - 1; |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 521 | |
| 522 | spin_lock_irqsave(&pool->lock, flags); |
| 523 | pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages, |
Aaro Koskinen | e4ea691 | 2012-08-07 14:39:25 +0200 | [diff] [blame] | 524 | 0, count, align_mask); |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 525 | if (pageno < pool->nr_pages) { |
| 526 | bitmap_set(pool->bitmap, pageno, count); |
| 527 | ptr = pool->vaddr + PAGE_SIZE * pageno; |
Hiroshi Doyu | 6b3fe47 | 2012-08-28 08:13:01 +0300 | [diff] [blame] | 528 | *ret_page = pool->pages[pageno]; |
Marek Szyprowski | fb71285 | 2012-08-22 14:50:42 +0200 | [diff] [blame] | 529 | } else { |
| 530 | pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n" |
| 531 | "Please increase it with coherent_pool= kernel parameter!\n", |
| 532 | (unsigned)pool->size / 1024); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 533 | } |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 534 | spin_unlock_irqrestore(&pool->lock, flags); |
| 535 | |
| 536 | return ptr; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Hiroshi Doyu | 21d0a75 | 2012-08-28 08:13:02 +0300 | [diff] [blame] | 539 | static bool __in_atomic_pool(void *start, size_t size) |
| 540 | { |
| 541 | struct dma_pool *pool = &atomic_pool; |
| 542 | void *end = start + size; |
| 543 | void *pool_start = pool->vaddr; |
| 544 | void *pool_end = pool->vaddr + pool->size; |
| 545 | |
Thomas Petazzoni | f3d8752 | 2012-09-10 16:14:16 +0200 | [diff] [blame] | 546 | if (start < pool_start || start >= pool_end) |
Hiroshi Doyu | 21d0a75 | 2012-08-28 08:13:02 +0300 | [diff] [blame] | 547 | return false; |
| 548 | |
| 549 | if (end <= pool_end) |
| 550 | return true; |
| 551 | |
| 552 | WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n", |
| 553 | start, end - 1, pool_start, pool_end - 1); |
| 554 | |
| 555 | return false; |
| 556 | } |
| 557 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 558 | static int __free_from_pool(void *start, size_t size) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 559 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 560 | struct dma_pool *pool = &atomic_pool; |
| 561 | unsigned long pageno, count; |
| 562 | unsigned long flags; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 563 | |
Hiroshi Doyu | 21d0a75 | 2012-08-28 08:13:02 +0300 | [diff] [blame] | 564 | if (!__in_atomic_pool(start, size)) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 565 | return 0; |
| 566 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 567 | pageno = (start - pool->vaddr) >> PAGE_SHIFT; |
| 568 | count = size >> PAGE_SHIFT; |
| 569 | |
| 570 | spin_lock_irqsave(&pool->lock, flags); |
| 571 | bitmap_clear(pool->bitmap, pageno, count); |
| 572 | spin_unlock_irqrestore(&pool->lock, flags); |
| 573 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | static void *__alloc_from_contiguous(struct device *dev, size_t size, |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 578 | pgprot_t prot, struct page **ret_page, |
| 579 | const void *caller) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 580 | { |
| 581 | unsigned long order = get_order(size); |
| 582 | size_t count = size >> PAGE_SHIFT; |
| 583 | struct page *page; |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 584 | void *ptr; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 585 | |
| 586 | page = dma_alloc_from_contiguous(dev, count, order); |
| 587 | if (!page) |
| 588 | return NULL; |
| 589 | |
| 590 | __dma_clear_buffer(page, size); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 591 | |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 592 | if (PageHighMem(page)) { |
| 593 | ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller); |
| 594 | if (!ptr) { |
| 595 | dma_release_from_contiguous(dev, page, count); |
| 596 | return NULL; |
| 597 | } |
| 598 | } else { |
| 599 | __dma_remap(page, size, prot); |
| 600 | ptr = page_address(page); |
| 601 | } |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 602 | *ret_page = page; |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 603 | return ptr; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static void __free_from_contiguous(struct device *dev, struct page *page, |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 607 | void *cpu_addr, size_t size) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 608 | { |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 609 | if (PageHighMem(page)) |
| 610 | __dma_free_remap(cpu_addr, size); |
| 611 | else |
| 612 | __dma_remap(page, size, pgprot_kernel); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 613 | dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); |
| 614 | } |
| 615 | |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 616 | static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot) |
| 617 | { |
| 618 | prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ? |
| 619 | pgprot_writecombine(prot) : |
| 620 | pgprot_dmacoherent(prot); |
| 621 | return prot; |
| 622 | } |
| 623 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 624 | #define nommu() 0 |
| 625 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 626 | #else /* !CONFIG_MMU */ |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 627 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 628 | #define nommu() 1 |
| 629 | |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 630 | #define __get_dma_pgprot(attrs, prot) __pgprot(0) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 631 | #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 632 | #define __alloc_from_pool(size, ret_page) NULL |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 633 | #define __alloc_from_contiguous(dev, size, prot, ret, c) NULL |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 634 | #define __free_from_pool(cpu_addr, size) 0 |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 635 | #define __free_from_contiguous(dev, page, cpu_addr, size) do { } while (0) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 636 | #define __dma_free_remap(cpu_addr, size) do { } while (0) |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 637 | |
| 638 | #endif /* CONFIG_MMU */ |
| 639 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 640 | static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp, |
| 641 | struct page **ret_page) |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 642 | { |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 643 | struct page *page; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 644 | page = __dma_alloc_buffer(dev, size, gfp); |
| 645 | if (!page) |
| 646 | return NULL; |
| 647 | |
| 648 | *ret_page = page; |
| 649 | return page_address(page); |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |
| 654 | static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 655 | gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 656 | { |
| 657 | u64 mask = get_coherent_dma_mask(dev); |
Jingoo Han | 3dd7ea9 | 2012-10-24 14:09:14 +0900 | [diff] [blame] | 658 | struct page *page = NULL; |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 659 | void *addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 660 | |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 661 | #ifdef CONFIG_DMA_API_DEBUG |
| 662 | u64 limit = (mask + 1) & ~mask; |
| 663 | if (limit && size >= limit) { |
| 664 | dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n", |
| 665 | size, mask); |
| 666 | return NULL; |
| 667 | } |
| 668 | #endif |
| 669 | |
| 670 | if (!mask) |
| 671 | return NULL; |
| 672 | |
| 673 | if (mask < 0xffffffffULL) |
| 674 | gfp |= GFP_DMA; |
| 675 | |
Sumit Bhattacharya | ea2e705 | 2011-11-24 00:47:12 +0100 | [diff] [blame] | 676 | /* |
| 677 | * Following is a work-around (a.k.a. hack) to prevent pages |
| 678 | * with __GFP_COMP being passed to split_page() which cannot |
| 679 | * handle them. The real problem is that this flag probably |
| 680 | * should be 0 on ARM as it is not supported on this |
| 681 | * platform; see CONFIG_HUGETLBFS. |
| 682 | */ |
| 683 | gfp &= ~(__GFP_COMP); |
| 684 | |
Marek Szyprowski | 553ac78 | 2012-02-29 14:45:28 +0100 | [diff] [blame] | 685 | *handle = DMA_ERROR_CODE; |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 686 | size = PAGE_ALIGN(size); |
| 687 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 688 | if (is_coherent || nommu()) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 689 | addr = __alloc_simple_buffer(dev, size, gfp, &page); |
Russell King | 633dc92 | 2013-01-30 23:55:35 +0000 | [diff] [blame] | 690 | else if (!(gfp & __GFP_WAIT)) |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 691 | addr = __alloc_from_pool(size, &page); |
Aneesh Kumar K.V | f825c73 | 2013-07-02 11:15:15 +0530 | [diff] [blame] | 692 | else if (!IS_ENABLED(CONFIG_DMA_CMA)) |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 693 | addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 694 | else |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 695 | addr = __alloc_from_contiguous(dev, size, prot, &page, caller); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 696 | |
| 697 | if (addr) |
Russell King | 9eedd96 | 2011-01-03 00:00:17 +0000 | [diff] [blame] | 698 | *handle = pfn_to_dma(dev, page_to_pfn(page)); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 699 | |
| 700 | return addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 701 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | /* |
| 704 | * Allocate DMA-coherent memory space and return both the kernel remapped |
| 705 | * virtual and bus address for that space. |
| 706 | */ |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 707 | void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
| 708 | gfp_t gfp, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 710 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 711 | void *memory; |
| 712 | |
| 713 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) |
| 714 | return memory; |
| 715 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 716 | return __dma_alloc(dev, size, handle, gfp, prot, false, |
| 717 | __builtin_return_address(0)); |
| 718 | } |
| 719 | |
| 720 | static void *arm_coherent_dma_alloc(struct device *dev, size_t size, |
| 721 | dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs) |
| 722 | { |
| 723 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); |
| 724 | void *memory; |
| 725 | |
| 726 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) |
| 727 | return memory; |
| 728 | |
| 729 | return __dma_alloc(dev, size, handle, gfp, prot, true, |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 730 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | /* |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 734 | * Create userspace mapping for the DMA-coherent memory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | */ |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 736 | int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma, |
| 737 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 738 | struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 740 | int ret = -ENXIO; |
| 741 | #ifdef CONFIG_MMU |
Marek Szyprowski | 50262a4 | 2012-07-30 09:35:26 +0200 | [diff] [blame] | 742 | unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 743 | unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 744 | unsigned long pfn = dma_to_pfn(dev, dma_addr); |
Marek Szyprowski | 50262a4 | 2012-07-30 09:35:26 +0200 | [diff] [blame] | 745 | unsigned long off = vma->vm_pgoff; |
| 746 | |
Marek Szyprowski | f99d603 | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 747 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); |
| 748 | |
Marek Szyprowski | 47142f0 | 2012-05-15 19:04:13 +0200 | [diff] [blame] | 749 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) |
| 750 | return ret; |
| 751 | |
Marek Szyprowski | 50262a4 | 2012-07-30 09:35:26 +0200 | [diff] [blame] | 752 | if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) { |
| 753 | ret = remap_pfn_range(vma, vma->vm_start, |
| 754 | pfn + off, |
| 755 | vma->vm_end - vma->vm_start, |
| 756 | vma->vm_page_prot); |
| 757 | } |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 758 | #endif /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | return ret; |
| 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 764 | * Free a buffer as defined by the above mapping. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | */ |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 766 | static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 767 | dma_addr_t handle, struct dma_attrs *attrs, |
| 768 | bool is_coherent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 770 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); |
Russell King | 5edf71a | 2005-11-25 15:52:51 +0000 | [diff] [blame] | 771 | |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 772 | if (dma_release_from_coherent(dev, get_order(size), cpu_addr)) |
| 773 | return; |
| 774 | |
Russell King | 3e82d01 | 2009-11-19 15:38:12 +0000 | [diff] [blame] | 775 | size = PAGE_ALIGN(size); |
| 776 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 777 | if (is_coherent || nommu()) { |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 778 | __dma_free_buffer(page, size); |
Aaro Koskinen | d9e0d14 | 2012-08-07 14:44:05 +0200 | [diff] [blame] | 779 | } else if (__free_from_pool(cpu_addr, size)) { |
| 780 | return; |
Aneesh Kumar K.V | f825c73 | 2013-07-02 11:15:15 +0530 | [diff] [blame] | 781 | } else if (!IS_ENABLED(CONFIG_DMA_CMA)) { |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 782 | __dma_free_remap(cpu_addr, size); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 783 | __dma_free_buffer(page, size); |
| 784 | } else { |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 785 | /* |
| 786 | * Non-atomic allocations cannot be freed with IRQs disabled |
| 787 | */ |
| 788 | WARN_ON(irqs_disabled()); |
Marek Szyprowski | 9848e48 | 2013-01-16 15:38:44 +0100 | [diff] [blame] | 789 | __free_from_contiguous(dev, page, cpu_addr, size); |
Marek Szyprowski | c790950 | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 792 | |
Rob Herring | dd37e94 | 2012-08-21 12:20:17 +0200 | [diff] [blame] | 793 | void arm_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 794 | dma_addr_t handle, struct dma_attrs *attrs) |
| 795 | { |
| 796 | __arm_dma_free(dev, size, cpu_addr, handle, attrs, false); |
| 797 | } |
| 798 | |
| 799 | static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 800 | dma_addr_t handle, struct dma_attrs *attrs) |
| 801 | { |
| 802 | __arm_dma_free(dev, size, cpu_addr, handle, attrs, true); |
| 803 | } |
| 804 | |
Marek Szyprowski | dc2832e | 2012-06-13 10:01:15 +0200 | [diff] [blame] | 805 | int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 806 | void *cpu_addr, dma_addr_t handle, size_t size, |
| 807 | struct dma_attrs *attrs) |
| 808 | { |
| 809 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); |
| 810 | int ret; |
| 811 | |
| 812 | ret = sg_alloc_table(sgt, 1, GFP_KERNEL); |
| 813 | if (unlikely(ret)) |
| 814 | return ret; |
| 815 | |
| 816 | sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); |
| 817 | return 0; |
| 818 | } |
| 819 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 820 | static void dma_cache_maint_page(struct page *page, unsigned long offset, |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 821 | size_t size, enum dma_data_direction dir, |
| 822 | void (*op)(const void *, size_t, int)) |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 823 | { |
Russell King | 1565337 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 824 | unsigned long pfn; |
| 825 | size_t left = size; |
| 826 | |
| 827 | pfn = page_to_pfn(page) + offset / PAGE_SIZE; |
| 828 | offset %= PAGE_SIZE; |
| 829 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 830 | /* |
| 831 | * A single sg entry may refer to multiple physically contiguous |
| 832 | * pages. But we still need to process highmem pages individually. |
| 833 | * If highmem is not configured then the bulk of this loop gets |
| 834 | * optimized out. |
| 835 | */ |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 836 | do { |
| 837 | size_t len = left; |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 838 | void *vaddr; |
| 839 | |
Russell King | 1565337 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 840 | page = pfn_to_page(pfn); |
| 841 | |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 842 | if (PageHighMem(page)) { |
Russell King | 1565337 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 843 | if (len + offset > PAGE_SIZE) |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 844 | len = PAGE_SIZE - offset; |
Joonsoo Kim | dd0f67f | 2013-04-05 03:16:14 +0100 | [diff] [blame] | 845 | |
| 846 | if (cache_is_vipt_nonaliasing()) { |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 847 | vaddr = kmap_atomic(page); |
Nicolas Pitre | 7e5a69e | 2010-03-29 21:46:02 +0100 | [diff] [blame] | 848 | op(vaddr + offset, len, dir); |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 849 | kunmap_atomic(vaddr); |
Joonsoo Kim | dd0f67f | 2013-04-05 03:16:14 +0100 | [diff] [blame] | 850 | } else { |
| 851 | vaddr = kmap_high_get(page); |
| 852 | if (vaddr) { |
| 853 | op(vaddr + offset, len, dir); |
| 854 | kunmap_high(page); |
| 855 | } |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 856 | } |
| 857 | } else { |
| 858 | vaddr = page_address(page) + offset; |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 859 | op(vaddr, len, dir); |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 860 | } |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 861 | offset = 0; |
Russell King | 1565337 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 862 | pfn++; |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 863 | left -= len; |
| 864 | } while (left); |
| 865 | } |
| 866 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 867 | /* |
| 868 | * Make an area consistent for devices. |
| 869 | * Note: Drivers should NOT use this function directly, as it will break |
| 870 | * platforms with CONFIG_DMABOUNCE. |
| 871 | * Use the driver DMA support - see dma-mapping.h (dma_sync_*) |
| 872 | */ |
| 873 | static void __dma_page_cpu_to_dev(struct page *page, unsigned long off, |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 874 | size_t size, enum dma_data_direction dir) |
| 875 | { |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 876 | unsigned long paddr; |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 877 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 878 | dma_cache_maint_page(page, off, size, dir, dmac_map_area); |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 879 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 880 | paddr = page_to_phys(page) + off; |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 881 | if (dir == DMA_FROM_DEVICE) { |
| 882 | outer_inv_range(paddr, paddr + size); |
| 883 | } else { |
| 884 | outer_clean_range(paddr, paddr + size); |
| 885 | } |
| 886 | /* FIXME: non-speculating: flush on bidirectional mappings? */ |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 887 | } |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 888 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 889 | static void __dma_page_dev_to_cpu(struct page *page, unsigned long off, |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 890 | size_t size, enum dma_data_direction dir) |
| 891 | { |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 892 | unsigned long paddr = page_to_phys(page) + off; |
| 893 | |
| 894 | /* FIXME: non-speculating: not required */ |
| 895 | /* don't bother invalidating if DMA to device */ |
| 896 | if (dir != DMA_TO_DEVICE) |
| 897 | outer_inv_range(paddr, paddr + size); |
| 898 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 899 | dma_cache_maint_page(page, off, size, dir, dmac_unmap_area); |
Catalin Marinas | c017780 | 2010-09-13 15:57:36 +0100 | [diff] [blame] | 900 | |
| 901 | /* |
Ming Lei | b2a234e | 2013-05-18 11:21:36 +0100 | [diff] [blame] | 902 | * Mark the D-cache clean for these pages to avoid extra flushing. |
Catalin Marinas | c017780 | 2010-09-13 15:57:36 +0100 | [diff] [blame] | 903 | */ |
Ming Lei | b2a234e | 2013-05-18 11:21:36 +0100 | [diff] [blame] | 904 | if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) { |
| 905 | unsigned long pfn; |
| 906 | size_t left = size; |
| 907 | |
| 908 | pfn = page_to_pfn(page) + off / PAGE_SIZE; |
| 909 | off %= PAGE_SIZE; |
| 910 | if (off) { |
| 911 | pfn++; |
| 912 | left -= PAGE_SIZE - off; |
| 913 | } |
| 914 | while (left >= PAGE_SIZE) { |
| 915 | page = pfn_to_page(pfn++); |
| 916 | set_bit(PG_dcache_clean, &page->flags); |
| 917 | left -= PAGE_SIZE; |
| 918 | } |
| 919 | } |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 920 | } |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 921 | |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 922 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 923 | * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 924 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 925 | * @sg: list of buffers |
| 926 | * @nents: number of buffers to map |
| 927 | * @dir: DMA transfer direction |
| 928 | * |
| 929 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 930 | * This is the scatter-gather version of the dma_map_single interface. |
| 931 | * Here the scatter gather list elements are each tagged with the |
| 932 | * appropriate dma address and length. They are obtained via |
| 933 | * sg_dma_{address,length}. |
| 934 | * |
| 935 | * Device ownership issues as mentioned for dma_map_single are the same |
| 936 | * here. |
| 937 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 938 | int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 939 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 940 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 941 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 942 | struct scatterlist *s; |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 943 | int i, j; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 944 | |
| 945 | for_each_sg(sg, s, nents, i) { |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 946 | #ifdef CONFIG_NEED_SG_DMA_LENGTH |
| 947 | s->dma_length = s->length; |
| 948 | #endif |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 949 | s->dma_address = ops->map_page(dev, sg_page(s), s->offset, |
| 950 | s->length, dir, attrs); |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 951 | if (dma_mapping_error(dev, s->dma_address)) |
| 952 | goto bad_mapping; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 953 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 954 | return nents; |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 955 | |
| 956 | bad_mapping: |
| 957 | for_each_sg(sg, s, i, j) |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 958 | ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 959 | return 0; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 960 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 961 | |
| 962 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 963 | * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 964 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 965 | * @sg: list of buffers |
Linus Walleij | 0adfca6 | 2011-01-12 18:50:37 +0100 | [diff] [blame] | 966 | * @nents: number of buffers to unmap (same as was passed to dma_map_sg) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 967 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 968 | * |
| 969 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 970 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 971 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 972 | void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 973 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 974 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 975 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 976 | struct scatterlist *s; |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 977 | |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 978 | int i; |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 979 | |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 980 | for_each_sg(sg, s, nents, i) |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 981 | ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 982 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 983 | |
| 984 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 985 | * arm_dma_sync_sg_for_cpu |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 986 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 987 | * @sg: list of buffers |
| 988 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 989 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 990 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 991 | void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 992 | int nents, enum dma_data_direction dir) |
| 993 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 994 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 995 | struct scatterlist *s; |
| 996 | int i; |
| 997 | |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 998 | for_each_sg(sg, s, nents, i) |
| 999 | ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length, |
| 1000 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1001 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1002 | |
| 1003 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1004 | * arm_dma_sync_sg_for_device |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1005 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 1006 | * @sg: list of buffers |
| 1007 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1008 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1009 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1010 | void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1011 | int nents, enum dma_data_direction dir) |
| 1012 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1013 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1014 | struct scatterlist *s; |
| 1015 | int i; |
| 1016 | |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1017 | for_each_sg(sg, s, nents, i) |
| 1018 | ops->sync_single_for_device(dev, sg_dma_address(s), s->length, |
| 1019 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1020 | } |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 1021 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1022 | /* |
| 1023 | * Return whether the given device DMA address mask can be supported |
| 1024 | * properly. For example, if your device can only drive the low 24-bits |
| 1025 | * during bus mastering, then you would pass 0x00ffffff as the mask |
| 1026 | * to this function. |
| 1027 | */ |
| 1028 | int dma_supported(struct device *dev, u64 mask) |
| 1029 | { |
Russell King | 4dcfa60 | 2013-07-09 12:14:49 +0100 | [diff] [blame^] | 1030 | unsigned long limit; |
| 1031 | |
| 1032 | /* |
| 1033 | * If the mask allows for more memory than we can address, |
| 1034 | * and we actually have that much memory, then we must |
| 1035 | * indicate that DMA to this device is not supported. |
| 1036 | */ |
| 1037 | if (sizeof(mask) != sizeof(dma_addr_t) && |
| 1038 | mask > (dma_addr_t)~0 && |
| 1039 | dma_to_pfn(dev, ~0) > arm_dma_pfn_limit) |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1040 | return 0; |
Russell King | 4dcfa60 | 2013-07-09 12:14:49 +0100 | [diff] [blame^] | 1041 | |
| 1042 | /* |
| 1043 | * Translate the device's DMA mask to a PFN limit. This |
| 1044 | * PFN number includes the page which we can DMA to. |
| 1045 | */ |
| 1046 | limit = dma_to_pfn(dev, mask); |
| 1047 | |
| 1048 | if (limit < arm_dma_pfn_limit) |
| 1049 | return 0; |
| 1050 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1051 | return 1; |
| 1052 | } |
| 1053 | EXPORT_SYMBOL(dma_supported); |
| 1054 | |
Gregory CLEMENT | 87b54e7 | 2012-11-21 09:39:19 +0100 | [diff] [blame] | 1055 | int arm_dma_set_mask(struct device *dev, u64 dma_mask) |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1056 | { |
| 1057 | if (!dev->dma_mask || !dma_supported(dev, dma_mask)) |
| 1058 | return -EIO; |
| 1059 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1060 | *dev->dma_mask = dma_mask; |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1061 | |
| 1062 | return 0; |
| 1063 | } |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1064 | |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 1065 | #define PREALLOC_DMA_DEBUG_ENTRIES 4096 |
| 1066 | |
| 1067 | static int __init dma_debug_do_init(void) |
| 1068 | { |
| 1069 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 1070 | return 0; |
| 1071 | } |
| 1072 | fs_initcall(dma_debug_do_init); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1073 | |
| 1074 | #ifdef CONFIG_ARM_DMA_USE_IOMMU |
| 1075 | |
| 1076 | /* IOMMU */ |
| 1077 | |
| 1078 | static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping, |
| 1079 | size_t size) |
| 1080 | { |
| 1081 | unsigned int order = get_order(size); |
| 1082 | unsigned int align = 0; |
| 1083 | unsigned int count, start; |
| 1084 | unsigned long flags; |
| 1085 | |
Seung-Woo Kim | 60460ab | 2013-02-06 13:21:14 +0900 | [diff] [blame] | 1086 | if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT) |
| 1087 | order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT; |
| 1088 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1089 | count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) + |
| 1090 | (1 << mapping->order) - 1) >> mapping->order; |
| 1091 | |
| 1092 | if (order > mapping->order) |
| 1093 | align = (1 << (order - mapping->order)) - 1; |
| 1094 | |
| 1095 | spin_lock_irqsave(&mapping->lock, flags); |
| 1096 | start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0, |
| 1097 | count, align); |
| 1098 | if (start > mapping->bits) { |
| 1099 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1100 | return DMA_ERROR_CODE; |
| 1101 | } |
| 1102 | |
| 1103 | bitmap_set(mapping->bitmap, start, count); |
| 1104 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1105 | |
| 1106 | return mapping->base + (start << (mapping->order + PAGE_SHIFT)); |
| 1107 | } |
| 1108 | |
| 1109 | static inline void __free_iova(struct dma_iommu_mapping *mapping, |
| 1110 | dma_addr_t addr, size_t size) |
| 1111 | { |
| 1112 | unsigned int start = (addr - mapping->base) >> |
| 1113 | (mapping->order + PAGE_SHIFT); |
| 1114 | unsigned int count = ((size >> PAGE_SHIFT) + |
| 1115 | (1 << mapping->order) - 1) >> mapping->order; |
| 1116 | unsigned long flags; |
| 1117 | |
| 1118 | spin_lock_irqsave(&mapping->lock, flags); |
| 1119 | bitmap_clear(mapping->bitmap, start, count); |
| 1120 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1121 | } |
| 1122 | |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1123 | static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, |
| 1124 | gfp_t gfp, struct dma_attrs *attrs) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1125 | { |
| 1126 | struct page **pages; |
| 1127 | int count = size >> PAGE_SHIFT; |
| 1128 | int array_size = count * sizeof(struct page *); |
| 1129 | int i = 0; |
| 1130 | |
| 1131 | if (array_size <= PAGE_SIZE) |
| 1132 | pages = kzalloc(array_size, gfp); |
| 1133 | else |
| 1134 | pages = vzalloc(array_size); |
| 1135 | if (!pages) |
| 1136 | return NULL; |
| 1137 | |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1138 | if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) |
| 1139 | { |
| 1140 | unsigned long order = get_order(size); |
| 1141 | struct page *page; |
| 1142 | |
| 1143 | page = dma_alloc_from_contiguous(dev, count, order); |
| 1144 | if (!page) |
| 1145 | goto error; |
| 1146 | |
| 1147 | __dma_clear_buffer(page, size); |
| 1148 | |
| 1149 | for (i = 0; i < count; i++) |
| 1150 | pages[i] = page + i; |
| 1151 | |
| 1152 | return pages; |
| 1153 | } |
| 1154 | |
Marek Szyprowski | f8669be | 2013-01-16 15:41:02 +0100 | [diff] [blame] | 1155 | /* |
| 1156 | * IOMMU can map any pages, so himem can also be used here |
| 1157 | */ |
| 1158 | gfp |= __GFP_NOWARN | __GFP_HIGHMEM; |
| 1159 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1160 | while (count) { |
Marek Szyprowski | 593f473 | 2012-06-21 11:48:11 +0200 | [diff] [blame] | 1161 | int j, order = __fls(count); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1162 | |
Marek Szyprowski | f8669be | 2013-01-16 15:41:02 +0100 | [diff] [blame] | 1163 | pages[i] = alloc_pages(gfp, order); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1164 | while (!pages[i] && order) |
Marek Szyprowski | f8669be | 2013-01-16 15:41:02 +0100 | [diff] [blame] | 1165 | pages[i] = alloc_pages(gfp, --order); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1166 | if (!pages[i]) |
| 1167 | goto error; |
| 1168 | |
Hiroshi Doyu | 5a796ee | 2012-09-11 07:39:39 +0200 | [diff] [blame] | 1169 | if (order) { |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1170 | split_page(pages[i], order); |
Hiroshi Doyu | 5a796ee | 2012-09-11 07:39:39 +0200 | [diff] [blame] | 1171 | j = 1 << order; |
| 1172 | while (--j) |
| 1173 | pages[i + j] = pages[i] + j; |
| 1174 | } |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1175 | |
| 1176 | __dma_clear_buffer(pages[i], PAGE_SIZE << order); |
| 1177 | i += 1 << order; |
| 1178 | count -= 1 << order; |
| 1179 | } |
| 1180 | |
| 1181 | return pages; |
| 1182 | error: |
Marek Szyprowski | 9fa8af9 | 2012-07-27 17:12:50 +0200 | [diff] [blame] | 1183 | while (i--) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1184 | if (pages[i]) |
| 1185 | __free_pages(pages[i], 0); |
Prathyush K | 46c8785 | 2012-07-16 08:59:55 +0200 | [diff] [blame] | 1186 | if (array_size <= PAGE_SIZE) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1187 | kfree(pages); |
| 1188 | else |
| 1189 | vfree(pages); |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1193 | static int __iommu_free_buffer(struct device *dev, struct page **pages, |
| 1194 | size_t size, struct dma_attrs *attrs) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1195 | { |
| 1196 | int count = size >> PAGE_SHIFT; |
| 1197 | int array_size = count * sizeof(struct page *); |
| 1198 | int i; |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1199 | |
| 1200 | if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) { |
| 1201 | dma_release_from_contiguous(dev, pages[0], count); |
| 1202 | } else { |
| 1203 | for (i = 0; i < count; i++) |
| 1204 | if (pages[i]) |
| 1205 | __free_pages(pages[i], 0); |
| 1206 | } |
| 1207 | |
Prathyush K | 46c8785 | 2012-07-16 08:59:55 +0200 | [diff] [blame] | 1208 | if (array_size <= PAGE_SIZE) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1209 | kfree(pages); |
| 1210 | else |
| 1211 | vfree(pages); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Create a CPU mapping for a specified pages |
| 1217 | */ |
| 1218 | static void * |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1219 | __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot, |
| 1220 | const void *caller) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1221 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1222 | unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 1223 | struct vm_struct *area; |
| 1224 | unsigned long p; |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1225 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1226 | area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP, |
| 1227 | caller); |
| 1228 | if (!area) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1229 | return NULL; |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1230 | |
| 1231 | area->pages = pages; |
| 1232 | area->nr_pages = nr_pages; |
| 1233 | p = (unsigned long)area->addr; |
| 1234 | |
| 1235 | for (i = 0; i < nr_pages; i++) { |
| 1236 | phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i])); |
| 1237 | if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot)) |
| 1238 | goto err; |
| 1239 | p += PAGE_SIZE; |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1240 | } |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1241 | return area->addr; |
| 1242 | err: |
| 1243 | unmap_kernel_range((unsigned long)area->addr, size); |
| 1244 | vunmap(area->addr); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1245 | return NULL; |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Create a mapping in device IO address space for specified pages |
| 1250 | */ |
| 1251 | static dma_addr_t |
| 1252 | __iommu_create_mapping(struct device *dev, struct page **pages, size_t size) |
| 1253 | { |
| 1254 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1255 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 1256 | dma_addr_t dma_addr, iova; |
| 1257 | int i, ret = DMA_ERROR_CODE; |
| 1258 | |
| 1259 | dma_addr = __alloc_iova(mapping, size); |
| 1260 | if (dma_addr == DMA_ERROR_CODE) |
| 1261 | return dma_addr; |
| 1262 | |
| 1263 | iova = dma_addr; |
| 1264 | for (i = 0; i < count; ) { |
| 1265 | unsigned int next_pfn = page_to_pfn(pages[i]) + 1; |
| 1266 | phys_addr_t phys = page_to_phys(pages[i]); |
| 1267 | unsigned int len, j; |
| 1268 | |
| 1269 | for (j = i + 1; j < count; j++, next_pfn++) |
| 1270 | if (page_to_pfn(pages[j]) != next_pfn) |
| 1271 | break; |
| 1272 | |
| 1273 | len = (j - i) << PAGE_SHIFT; |
| 1274 | ret = iommu_map(mapping->domain, iova, phys, len, 0); |
| 1275 | if (ret < 0) |
| 1276 | goto fail; |
| 1277 | iova += len; |
| 1278 | i = j; |
| 1279 | } |
| 1280 | return dma_addr; |
| 1281 | fail: |
| 1282 | iommu_unmap(mapping->domain, dma_addr, iova-dma_addr); |
| 1283 | __free_iova(mapping, dma_addr, size); |
| 1284 | return DMA_ERROR_CODE; |
| 1285 | } |
| 1286 | |
| 1287 | static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size) |
| 1288 | { |
| 1289 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1290 | |
| 1291 | /* |
| 1292 | * add optional in-page offset from iova to size and align |
| 1293 | * result to page size |
| 1294 | */ |
| 1295 | size = PAGE_ALIGN((iova & ~PAGE_MASK) + size); |
| 1296 | iova &= PAGE_MASK; |
| 1297 | |
| 1298 | iommu_unmap(mapping->domain, iova, size); |
| 1299 | __free_iova(mapping, iova, size); |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Hiroshi Doyu | 665bad7 | 2012-08-28 08:13:03 +0300 | [diff] [blame] | 1303 | static struct page **__atomic_get_pages(void *addr) |
| 1304 | { |
| 1305 | struct dma_pool *pool = &atomic_pool; |
| 1306 | struct page **pages = pool->pages; |
| 1307 | int offs = (addr - pool->vaddr) >> PAGE_SHIFT; |
| 1308 | |
| 1309 | return pages + offs; |
| 1310 | } |
| 1311 | |
Marek Szyprowski | 955c757 | 2012-05-16 19:38:58 +0100 | [diff] [blame] | 1312 | static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs) |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1313 | { |
| 1314 | struct vm_struct *area; |
| 1315 | |
Hiroshi Doyu | 665bad7 | 2012-08-28 08:13:03 +0300 | [diff] [blame] | 1316 | if (__in_atomic_pool(cpu_addr, PAGE_SIZE)) |
| 1317 | return __atomic_get_pages(cpu_addr); |
| 1318 | |
Marek Szyprowski | 955c757 | 2012-05-16 19:38:58 +0100 | [diff] [blame] | 1319 | if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) |
| 1320 | return cpu_addr; |
| 1321 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1322 | area = find_vm_area(cpu_addr); |
| 1323 | if (area && (area->flags & VM_ARM_DMA_CONSISTENT)) |
| 1324 | return area->pages; |
| 1325 | return NULL; |
| 1326 | } |
| 1327 | |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1328 | static void *__iommu_alloc_atomic(struct device *dev, size_t size, |
| 1329 | dma_addr_t *handle) |
| 1330 | { |
| 1331 | struct page *page; |
| 1332 | void *addr; |
| 1333 | |
| 1334 | addr = __alloc_from_pool(size, &page); |
| 1335 | if (!addr) |
| 1336 | return NULL; |
| 1337 | |
| 1338 | *handle = __iommu_create_mapping(dev, &page, size); |
| 1339 | if (*handle == DMA_ERROR_CODE) |
| 1340 | goto err_mapping; |
| 1341 | |
| 1342 | return addr; |
| 1343 | |
| 1344 | err_mapping: |
| 1345 | __free_from_pool(addr, size); |
| 1346 | return NULL; |
| 1347 | } |
| 1348 | |
Marek Szyprowski | d589829 | 2013-02-08 10:54:48 +0100 | [diff] [blame] | 1349 | static void __iommu_free_atomic(struct device *dev, void *cpu_addr, |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1350 | dma_addr_t handle, size_t size) |
| 1351 | { |
| 1352 | __iommu_remove_mapping(dev, handle, size); |
Marek Szyprowski | d589829 | 2013-02-08 10:54:48 +0100 | [diff] [blame] | 1353 | __free_from_pool(cpu_addr, size); |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1354 | } |
| 1355 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1356 | static void *arm_iommu_alloc_attrs(struct device *dev, size_t size, |
| 1357 | dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs) |
| 1358 | { |
| 1359 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); |
| 1360 | struct page **pages; |
| 1361 | void *addr = NULL; |
| 1362 | |
| 1363 | *handle = DMA_ERROR_CODE; |
| 1364 | size = PAGE_ALIGN(size); |
| 1365 | |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1366 | if (gfp & GFP_ATOMIC) |
| 1367 | return __iommu_alloc_atomic(dev, size, handle); |
| 1368 | |
Richard Zhao | 5b91a98 | 2013-06-20 20:31:00 +0800 | [diff] [blame] | 1369 | /* |
| 1370 | * Following is a work-around (a.k.a. hack) to prevent pages |
| 1371 | * with __GFP_COMP being passed to split_page() which cannot |
| 1372 | * handle them. The real problem is that this flag probably |
| 1373 | * should be 0 on ARM as it is not supported on this |
| 1374 | * platform; see CONFIG_HUGETLBFS. |
| 1375 | */ |
| 1376 | gfp &= ~(__GFP_COMP); |
| 1377 | |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1378 | pages = __iommu_alloc_buffer(dev, size, gfp, attrs); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1379 | if (!pages) |
| 1380 | return NULL; |
| 1381 | |
| 1382 | *handle = __iommu_create_mapping(dev, pages, size); |
| 1383 | if (*handle == DMA_ERROR_CODE) |
| 1384 | goto err_buffer; |
| 1385 | |
Marek Szyprowski | 955c757 | 2012-05-16 19:38:58 +0100 | [diff] [blame] | 1386 | if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) |
| 1387 | return pages; |
| 1388 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1389 | addr = __iommu_alloc_remap(pages, size, gfp, prot, |
| 1390 | __builtin_return_address(0)); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1391 | if (!addr) |
| 1392 | goto err_mapping; |
| 1393 | |
| 1394 | return addr; |
| 1395 | |
| 1396 | err_mapping: |
| 1397 | __iommu_remove_mapping(dev, *handle, size); |
| 1398 | err_buffer: |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1399 | __iommu_free_buffer(dev, pages, size, attrs); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1400 | return NULL; |
| 1401 | } |
| 1402 | |
| 1403 | static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, |
| 1404 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 1405 | struct dma_attrs *attrs) |
| 1406 | { |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1407 | unsigned long uaddr = vma->vm_start; |
| 1408 | unsigned long usize = vma->vm_end - vma->vm_start; |
Marek Szyprowski | 955c757 | 2012-05-16 19:38:58 +0100 | [diff] [blame] | 1409 | struct page **pages = __iommu_get_pages(cpu_addr, attrs); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1410 | |
| 1411 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1412 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1413 | if (!pages) |
| 1414 | return -ENXIO; |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1415 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1416 | do { |
| 1417 | int ret = vm_insert_page(vma, uaddr, *pages++); |
| 1418 | if (ret) { |
| 1419 | pr_err("Remapping memory failed: %d\n", ret); |
| 1420 | return ret; |
| 1421 | } |
| 1422 | uaddr += PAGE_SIZE; |
| 1423 | usize -= PAGE_SIZE; |
| 1424 | } while (usize > 0); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1425 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * free a page as defined by the above mapping. |
| 1431 | * Must not be called with IRQs disabled. |
| 1432 | */ |
| 1433 | void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, |
| 1434 | dma_addr_t handle, struct dma_attrs *attrs) |
| 1435 | { |
YoungJun Cho | 836bfa0 | 2013-06-17 13:18:52 +0900 | [diff] [blame] | 1436 | struct page **pages; |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1437 | size = PAGE_ALIGN(size); |
| 1438 | |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1439 | if (__in_atomic_pool(cpu_addr, size)) { |
Marek Szyprowski | d589829 | 2013-02-08 10:54:48 +0100 | [diff] [blame] | 1440 | __iommu_free_atomic(dev, cpu_addr, handle, size); |
Hiroshi Doyu | 479ed93 | 2012-08-28 08:13:04 +0300 | [diff] [blame] | 1441 | return; |
| 1442 | } |
| 1443 | |
YoungJun Cho | 836bfa0 | 2013-06-17 13:18:52 +0900 | [diff] [blame] | 1444 | pages = __iommu_get_pages(cpu_addr, attrs); |
| 1445 | if (!pages) { |
| 1446 | WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr); |
| 1447 | return; |
| 1448 | } |
| 1449 | |
Marek Szyprowski | 955c757 | 2012-05-16 19:38:58 +0100 | [diff] [blame] | 1450 | if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) { |
| 1451 | unmap_kernel_range((unsigned long)cpu_addr, size); |
| 1452 | vunmap(cpu_addr); |
| 1453 | } |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1454 | |
| 1455 | __iommu_remove_mapping(dev, handle, size); |
Marek Szyprowski | 549a17e | 2012-10-15 16:03:52 +0200 | [diff] [blame] | 1456 | __iommu_free_buffer(dev, pages, size, attrs); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Marek Szyprowski | dc2832e | 2012-06-13 10:01:15 +0200 | [diff] [blame] | 1459 | static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 1460 | void *cpu_addr, dma_addr_t dma_addr, |
| 1461 | size_t size, struct dma_attrs *attrs) |
| 1462 | { |
| 1463 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 1464 | struct page **pages = __iommu_get_pages(cpu_addr, attrs); |
| 1465 | |
| 1466 | if (!pages) |
| 1467 | return -ENXIO; |
| 1468 | |
| 1469 | return sg_alloc_table_from_pages(sgt, pages, count, 0, size, |
| 1470 | GFP_KERNEL); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * Map a part of the scatter-gather list into contiguous io address space |
| 1475 | */ |
| 1476 | static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, |
| 1477 | size_t size, dma_addr_t *handle, |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1478 | enum dma_data_direction dir, struct dma_attrs *attrs, |
| 1479 | bool is_coherent) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1480 | { |
| 1481 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1482 | dma_addr_t iova, iova_base; |
| 1483 | int ret = 0; |
| 1484 | unsigned int count; |
| 1485 | struct scatterlist *s; |
| 1486 | |
| 1487 | size = PAGE_ALIGN(size); |
| 1488 | *handle = DMA_ERROR_CODE; |
| 1489 | |
| 1490 | iova_base = iova = __alloc_iova(mapping, size); |
| 1491 | if (iova == DMA_ERROR_CODE) |
| 1492 | return -ENOMEM; |
| 1493 | |
| 1494 | for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) { |
| 1495 | phys_addr_t phys = page_to_phys(sg_page(s)); |
| 1496 | unsigned int len = PAGE_ALIGN(s->offset + s->length); |
| 1497 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1498 | if (!is_coherent && |
| 1499 | !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1500 | __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); |
| 1501 | |
| 1502 | ret = iommu_map(mapping->domain, iova, phys, len, 0); |
| 1503 | if (ret < 0) |
| 1504 | goto fail; |
| 1505 | count += len >> PAGE_SHIFT; |
| 1506 | iova += len; |
| 1507 | } |
| 1508 | *handle = iova_base; |
| 1509 | |
| 1510 | return 0; |
| 1511 | fail: |
| 1512 | iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); |
| 1513 | __free_iova(mapping, iova_base, size); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1517 | static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 1518 | enum dma_data_direction dir, struct dma_attrs *attrs, |
| 1519 | bool is_coherent) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1520 | { |
| 1521 | struct scatterlist *s = sg, *dma = sg, *start = sg; |
| 1522 | int i, count = 0; |
| 1523 | unsigned int offset = s->offset; |
| 1524 | unsigned int size = s->offset + s->length; |
| 1525 | unsigned int max = dma_get_max_seg_size(dev); |
| 1526 | |
| 1527 | for (i = 1; i < nents; i++) { |
| 1528 | s = sg_next(s); |
| 1529 | |
| 1530 | s->dma_address = DMA_ERROR_CODE; |
| 1531 | s->dma_length = 0; |
| 1532 | |
| 1533 | if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) { |
| 1534 | if (__map_sg_chunk(dev, start, size, &dma->dma_address, |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1535 | dir, attrs, is_coherent) < 0) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1536 | goto bad_mapping; |
| 1537 | |
| 1538 | dma->dma_address += offset; |
| 1539 | dma->dma_length = size - offset; |
| 1540 | |
| 1541 | size = offset = s->offset; |
| 1542 | start = s; |
| 1543 | dma = sg_next(dma); |
| 1544 | count += 1; |
| 1545 | } |
| 1546 | size += s->length; |
| 1547 | } |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1548 | if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs, |
| 1549 | is_coherent) < 0) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1550 | goto bad_mapping; |
| 1551 | |
| 1552 | dma->dma_address += offset; |
| 1553 | dma->dma_length = size - offset; |
| 1554 | |
| 1555 | return count+1; |
| 1556 | |
| 1557 | bad_mapping: |
| 1558 | for_each_sg(sg, s, count, i) |
| 1559 | __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s)); |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1564 | * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA |
| 1565 | * @dev: valid struct device pointer |
| 1566 | * @sg: list of buffers |
| 1567 | * @nents: number of buffers to map |
| 1568 | * @dir: DMA transfer direction |
| 1569 | * |
| 1570 | * Map a set of i/o coherent buffers described by scatterlist in streaming |
| 1571 | * mode for DMA. The scatter gather list elements are merged together (if |
| 1572 | * possible) and tagged with the appropriate dma address and length. They are |
| 1573 | * obtained via sg_dma_{address,length}. |
| 1574 | */ |
| 1575 | int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg, |
| 1576 | int nents, enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1577 | { |
| 1578 | return __iommu_map_sg(dev, sg, nents, dir, attrs, true); |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA |
| 1583 | * @dev: valid struct device pointer |
| 1584 | * @sg: list of buffers |
| 1585 | * @nents: number of buffers to map |
| 1586 | * @dir: DMA transfer direction |
| 1587 | * |
| 1588 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 1589 | * The scatter gather list elements are merged together (if possible) and |
| 1590 | * tagged with the appropriate dma address and length. They are obtained via |
| 1591 | * sg_dma_{address,length}. |
| 1592 | */ |
| 1593 | int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, |
| 1594 | int nents, enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1595 | { |
| 1596 | return __iommu_map_sg(dev, sg, nents, dir, attrs, false); |
| 1597 | } |
| 1598 | |
| 1599 | static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 1600 | int nents, enum dma_data_direction dir, struct dma_attrs *attrs, |
| 1601 | bool is_coherent) |
| 1602 | { |
| 1603 | struct scatterlist *s; |
| 1604 | int i; |
| 1605 | |
| 1606 | for_each_sg(sg, s, nents, i) { |
| 1607 | if (sg_dma_len(s)) |
| 1608 | __iommu_remove_mapping(dev, sg_dma_address(s), |
| 1609 | sg_dma_len(s)); |
| 1610 | if (!is_coherent && |
| 1611 | !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
| 1612 | __dma_page_dev_to_cpu(sg_page(s), s->offset, |
| 1613 | s->length, dir); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg |
| 1619 | * @dev: valid struct device pointer |
| 1620 | * @sg: list of buffers |
| 1621 | * @nents: number of buffers to unmap (same as was passed to dma_map_sg) |
| 1622 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1623 | * |
| 1624 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 1625 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 1626 | */ |
| 1627 | void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 1628 | int nents, enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1629 | { |
| 1630 | __iommu_unmap_sg(dev, sg, nents, dir, attrs, true); |
| 1631 | } |
| 1632 | |
| 1633 | /** |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1634 | * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg |
| 1635 | * @dev: valid struct device pointer |
| 1636 | * @sg: list of buffers |
| 1637 | * @nents: number of buffers to unmap (same as was passed to dma_map_sg) |
| 1638 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1639 | * |
| 1640 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 1641 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 1642 | */ |
| 1643 | void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 1644 | enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1645 | { |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1646 | __iommu_unmap_sg(dev, sg, nents, dir, attrs, false); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * arm_iommu_sync_sg_for_cpu |
| 1651 | * @dev: valid struct device pointer |
| 1652 | * @sg: list of buffers |
| 1653 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1654 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1655 | */ |
| 1656 | void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 1657 | int nents, enum dma_data_direction dir) |
| 1658 | { |
| 1659 | struct scatterlist *s; |
| 1660 | int i; |
| 1661 | |
| 1662 | for_each_sg(sg, s, nents, i) |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1663 | __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1664 | |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * arm_iommu_sync_sg_for_device |
| 1669 | * @dev: valid struct device pointer |
| 1670 | * @sg: list of buffers |
| 1671 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1672 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1673 | */ |
| 1674 | void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 1675 | int nents, enum dma_data_direction dir) |
| 1676 | { |
| 1677 | struct scatterlist *s; |
| 1678 | int i; |
| 1679 | |
| 1680 | for_each_sg(sg, s, nents, i) |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1681 | __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | /** |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1686 | * arm_coherent_iommu_map_page |
| 1687 | * @dev: valid struct device pointer |
| 1688 | * @page: page that buffer resides in |
| 1689 | * @offset: offset into page for start of buffer |
| 1690 | * @size: size of buffer to map |
| 1691 | * @dir: DMA transfer direction |
| 1692 | * |
| 1693 | * Coherent IOMMU aware version of arm_dma_map_page() |
| 1694 | */ |
| 1695 | static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page, |
| 1696 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 1697 | struct dma_attrs *attrs) |
| 1698 | { |
| 1699 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1700 | dma_addr_t dma_addr; |
Will Deacon | 13987d6 | 2013-06-10 19:34:39 +0100 | [diff] [blame] | 1701 | int ret, prot, len = PAGE_ALIGN(size + offset); |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1702 | |
| 1703 | dma_addr = __alloc_iova(mapping, len); |
| 1704 | if (dma_addr == DMA_ERROR_CODE) |
| 1705 | return dma_addr; |
| 1706 | |
Will Deacon | 13987d6 | 2013-06-10 19:34:39 +0100 | [diff] [blame] | 1707 | switch (dir) { |
| 1708 | case DMA_BIDIRECTIONAL: |
| 1709 | prot = IOMMU_READ | IOMMU_WRITE; |
| 1710 | break; |
| 1711 | case DMA_TO_DEVICE: |
| 1712 | prot = IOMMU_READ; |
| 1713 | break; |
| 1714 | case DMA_FROM_DEVICE: |
| 1715 | prot = IOMMU_WRITE; |
| 1716 | break; |
| 1717 | default: |
| 1718 | prot = 0; |
| 1719 | } |
| 1720 | |
| 1721 | ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot); |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1722 | if (ret < 0) |
| 1723 | goto fail; |
| 1724 | |
| 1725 | return dma_addr + offset; |
| 1726 | fail: |
| 1727 | __free_iova(mapping, dma_addr, len); |
| 1728 | return DMA_ERROR_CODE; |
| 1729 | } |
| 1730 | |
| 1731 | /** |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1732 | * arm_iommu_map_page |
| 1733 | * @dev: valid struct device pointer |
| 1734 | * @page: page that buffer resides in |
| 1735 | * @offset: offset into page for start of buffer |
| 1736 | * @size: size of buffer to map |
| 1737 | * @dir: DMA transfer direction |
| 1738 | * |
| 1739 | * IOMMU aware version of arm_dma_map_page() |
| 1740 | */ |
| 1741 | static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page, |
| 1742 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 1743 | struct dma_attrs *attrs) |
| 1744 | { |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1745 | if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1746 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 1747 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1748 | return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs); |
| 1749 | } |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1750 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1751 | /** |
| 1752 | * arm_coherent_iommu_unmap_page |
| 1753 | * @dev: valid struct device pointer |
| 1754 | * @handle: DMA address of buffer |
| 1755 | * @size: size of buffer (same as passed to dma_map_page) |
| 1756 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 1757 | * |
| 1758 | * Coherent IOMMU aware version of arm_dma_unmap_page() |
| 1759 | */ |
| 1760 | static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle, |
| 1761 | size_t size, enum dma_data_direction dir, |
| 1762 | struct dma_attrs *attrs) |
| 1763 | { |
| 1764 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1765 | dma_addr_t iova = handle & PAGE_MASK; |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1766 | int offset = handle & ~PAGE_MASK; |
| 1767 | int len = PAGE_ALIGN(size + offset); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1768 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1769 | if (!iova) |
| 1770 | return; |
| 1771 | |
| 1772 | iommu_unmap(mapping->domain, iova, len); |
| 1773 | __free_iova(mapping, iova, len); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /** |
| 1777 | * arm_iommu_unmap_page |
| 1778 | * @dev: valid struct device pointer |
| 1779 | * @handle: DMA address of buffer |
| 1780 | * @size: size of buffer (same as passed to dma_map_page) |
| 1781 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 1782 | * |
| 1783 | * IOMMU aware version of arm_dma_unmap_page() |
| 1784 | */ |
| 1785 | static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle, |
| 1786 | size_t size, enum dma_data_direction dir, |
| 1787 | struct dma_attrs *attrs) |
| 1788 | { |
| 1789 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1790 | dma_addr_t iova = handle & PAGE_MASK; |
| 1791 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1792 | int offset = handle & ~PAGE_MASK; |
| 1793 | int len = PAGE_ALIGN(size + offset); |
| 1794 | |
| 1795 | if (!iova) |
| 1796 | return; |
| 1797 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1798 | if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs)) |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1799 | __dma_page_dev_to_cpu(page, offset, size, dir); |
| 1800 | |
| 1801 | iommu_unmap(mapping->domain, iova, len); |
| 1802 | __free_iova(mapping, iova, len); |
| 1803 | } |
| 1804 | |
| 1805 | static void arm_iommu_sync_single_for_cpu(struct device *dev, |
| 1806 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 1807 | { |
| 1808 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1809 | dma_addr_t iova = handle & PAGE_MASK; |
| 1810 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1811 | unsigned int offset = handle & ~PAGE_MASK; |
| 1812 | |
| 1813 | if (!iova) |
| 1814 | return; |
| 1815 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1816 | __dma_page_dev_to_cpu(page, offset, size, dir); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static void arm_iommu_sync_single_for_device(struct device *dev, |
| 1820 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 1821 | { |
| 1822 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1823 | dma_addr_t iova = handle & PAGE_MASK; |
| 1824 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1825 | unsigned int offset = handle & ~PAGE_MASK; |
| 1826 | |
| 1827 | if (!iova) |
| 1828 | return; |
| 1829 | |
| 1830 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 1831 | } |
| 1832 | |
| 1833 | struct dma_map_ops iommu_ops = { |
| 1834 | .alloc = arm_iommu_alloc_attrs, |
| 1835 | .free = arm_iommu_free_attrs, |
| 1836 | .mmap = arm_iommu_mmap_attrs, |
Marek Szyprowski | dc2832e | 2012-06-13 10:01:15 +0200 | [diff] [blame] | 1837 | .get_sgtable = arm_iommu_get_sgtable, |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1838 | |
| 1839 | .map_page = arm_iommu_map_page, |
| 1840 | .unmap_page = arm_iommu_unmap_page, |
| 1841 | .sync_single_for_cpu = arm_iommu_sync_single_for_cpu, |
| 1842 | .sync_single_for_device = arm_iommu_sync_single_for_device, |
| 1843 | |
| 1844 | .map_sg = arm_iommu_map_sg, |
| 1845 | .unmap_sg = arm_iommu_unmap_sg, |
| 1846 | .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu, |
| 1847 | .sync_sg_for_device = arm_iommu_sync_sg_for_device, |
Hiroshi Doyu | d09e133 | 2013-01-29 14:57:07 +0200 | [diff] [blame] | 1848 | |
| 1849 | .set_dma_mask = arm_dma_set_mask, |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1850 | }; |
| 1851 | |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1852 | struct dma_map_ops iommu_coherent_ops = { |
| 1853 | .alloc = arm_iommu_alloc_attrs, |
| 1854 | .free = arm_iommu_free_attrs, |
| 1855 | .mmap = arm_iommu_mmap_attrs, |
| 1856 | .get_sgtable = arm_iommu_get_sgtable, |
| 1857 | |
| 1858 | .map_page = arm_coherent_iommu_map_page, |
| 1859 | .unmap_page = arm_coherent_iommu_unmap_page, |
| 1860 | |
| 1861 | .map_sg = arm_coherent_iommu_map_sg, |
| 1862 | .unmap_sg = arm_coherent_iommu_unmap_sg, |
Hiroshi Doyu | d09e133 | 2013-01-29 14:57:07 +0200 | [diff] [blame] | 1863 | |
| 1864 | .set_dma_mask = arm_dma_set_mask, |
Rob Herring | 0fa478d | 2012-08-21 12:23:23 +0200 | [diff] [blame] | 1865 | }; |
| 1866 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1867 | /** |
| 1868 | * arm_iommu_create_mapping |
| 1869 | * @bus: pointer to the bus holding the client device (for IOMMU calls) |
| 1870 | * @base: start address of the valid IO address space |
| 1871 | * @size: size of the valid IO address space |
| 1872 | * @order: accuracy of the IO addresses allocations |
| 1873 | * |
| 1874 | * Creates a mapping structure which holds information about used/unused |
| 1875 | * IO address ranges, which is required to perform memory allocation and |
| 1876 | * mapping with IOMMU aware functions. |
| 1877 | * |
| 1878 | * The client device need to be attached to the mapping with |
| 1879 | * arm_iommu_attach_device function. |
| 1880 | */ |
| 1881 | struct dma_iommu_mapping * |
| 1882 | arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, |
| 1883 | int order) |
| 1884 | { |
| 1885 | unsigned int count = size >> (PAGE_SHIFT + order); |
| 1886 | unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long); |
| 1887 | struct dma_iommu_mapping *mapping; |
| 1888 | int err = -ENOMEM; |
| 1889 | |
| 1890 | if (!count) |
| 1891 | return ERR_PTR(-EINVAL); |
| 1892 | |
| 1893 | mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL); |
| 1894 | if (!mapping) |
| 1895 | goto err; |
| 1896 | |
| 1897 | mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 1898 | if (!mapping->bitmap) |
| 1899 | goto err2; |
| 1900 | |
| 1901 | mapping->base = base; |
| 1902 | mapping->bits = BITS_PER_BYTE * bitmap_size; |
| 1903 | mapping->order = order; |
| 1904 | spin_lock_init(&mapping->lock); |
| 1905 | |
| 1906 | mapping->domain = iommu_domain_alloc(bus); |
| 1907 | if (!mapping->domain) |
| 1908 | goto err3; |
| 1909 | |
| 1910 | kref_init(&mapping->kref); |
| 1911 | return mapping; |
| 1912 | err3: |
| 1913 | kfree(mapping->bitmap); |
| 1914 | err2: |
| 1915 | kfree(mapping); |
| 1916 | err: |
| 1917 | return ERR_PTR(err); |
| 1918 | } |
Prathyush K | 18177d1 | 2013-01-04 06:22:42 -0500 | [diff] [blame] | 1919 | EXPORT_SYMBOL_GPL(arm_iommu_create_mapping); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1920 | |
| 1921 | static void release_iommu_mapping(struct kref *kref) |
| 1922 | { |
| 1923 | struct dma_iommu_mapping *mapping = |
| 1924 | container_of(kref, struct dma_iommu_mapping, kref); |
| 1925 | |
| 1926 | iommu_domain_free(mapping->domain); |
| 1927 | kfree(mapping->bitmap); |
| 1928 | kfree(mapping); |
| 1929 | } |
| 1930 | |
| 1931 | void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping) |
| 1932 | { |
| 1933 | if (mapping) |
| 1934 | kref_put(&mapping->kref, release_iommu_mapping); |
| 1935 | } |
Prathyush K | 18177d1 | 2013-01-04 06:22:42 -0500 | [diff] [blame] | 1936 | EXPORT_SYMBOL_GPL(arm_iommu_release_mapping); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1937 | |
| 1938 | /** |
| 1939 | * arm_iommu_attach_device |
| 1940 | * @dev: valid struct device pointer |
| 1941 | * @mapping: io address space mapping structure (returned from |
| 1942 | * arm_iommu_create_mapping) |
| 1943 | * |
| 1944 | * Attaches specified io address space mapping to the provided device, |
| 1945 | * this replaces the dma operations (dma_map_ops pointer) with the |
| 1946 | * IOMMU aware version. More than one client might be attached to |
| 1947 | * the same io address space mapping. |
| 1948 | */ |
| 1949 | int arm_iommu_attach_device(struct device *dev, |
| 1950 | struct dma_iommu_mapping *mapping) |
| 1951 | { |
| 1952 | int err; |
| 1953 | |
| 1954 | err = iommu_attach_device(mapping->domain, dev); |
| 1955 | if (err) |
| 1956 | return err; |
| 1957 | |
| 1958 | kref_get(&mapping->kref); |
| 1959 | dev->archdata.mapping = mapping; |
| 1960 | set_dma_ops(dev, &iommu_ops); |
| 1961 | |
Hiroshi Doyu | 75c5971 | 2012-09-11 07:39:48 +0200 | [diff] [blame] | 1962 | pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev)); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1963 | return 0; |
| 1964 | } |
Prathyush K | 18177d1 | 2013-01-04 06:22:42 -0500 | [diff] [blame] | 1965 | EXPORT_SYMBOL_GPL(arm_iommu_attach_device); |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1966 | |
Hiroshi Doyu | 6fe3675 | 2013-01-24 15:16:57 +0200 | [diff] [blame] | 1967 | /** |
| 1968 | * arm_iommu_detach_device |
| 1969 | * @dev: valid struct device pointer |
| 1970 | * |
| 1971 | * Detaches the provided device from a previously attached map. |
| 1972 | * This voids the dma operations (dma_map_ops pointer) |
| 1973 | */ |
| 1974 | void arm_iommu_detach_device(struct device *dev) |
| 1975 | { |
| 1976 | struct dma_iommu_mapping *mapping; |
| 1977 | |
| 1978 | mapping = to_dma_iommu_mapping(dev); |
| 1979 | if (!mapping) { |
| 1980 | dev_warn(dev, "Not attached\n"); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
| 1984 | iommu_detach_device(mapping->domain, dev); |
| 1985 | kref_put(&mapping->kref, release_iommu_mapping); |
Will Deacon | 9e4b259 | 2013-06-10 19:34:40 +0100 | [diff] [blame] | 1986 | dev->archdata.mapping = NULL; |
Hiroshi Doyu | 6fe3675 | 2013-01-24 15:16:57 +0200 | [diff] [blame] | 1987 | set_dma_ops(dev, NULL); |
| 1988 | |
| 1989 | pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev)); |
| 1990 | } |
Prathyush K | 18177d1 | 2013-01-04 06:22:42 -0500 | [diff] [blame] | 1991 | EXPORT_SYMBOL_GPL(arm_iommu_detach_device); |
Hiroshi Doyu | 6fe3675 | 2013-01-24 15:16:57 +0200 | [diff] [blame] | 1992 | |
Marek Szyprowski | 4ce63fc | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1993 | #endif |