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