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