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