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