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