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