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