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 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/dma-mapping.h> |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 20 | #include <linux/dma-contiguous.h> |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 21 | #include <linux/highmem.h> |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 22 | #include <linux/memblock.h> |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 24 | #include <linux/iommu.h> |
| 25 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Lennert Buytenhek | 23759dc | 2006-04-02 00:07:39 +0100 | [diff] [blame] | 27 | #include <asm/memory.h> |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 28 | #include <asm/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/tlbflush.h> |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 31 | #include <asm/sizes.h> |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 32 | #include <asm/mach/arch.h> |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 33 | #include <asm/mach/map.h> |
| 34 | #include <asm/system_info.h> |
| 35 | #include <asm/dma-contiguous.h> |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 36 | #include <asm/dma-iommu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 38 | #include "mm.h" |
| 39 | |
Marek Szyprowski | 75cd651 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 40 | /* |
| 41 | * The DMA API is built upon the notion of "buffer ownership". A buffer |
| 42 | * is either exclusively owned by the CPU (and therefore may be accessed |
| 43 | * by it) or exclusively owned by the DMA device. These helper functions |
| 44 | * represent the transitions between these two ownership states. |
| 45 | * |
| 46 | * Note, however, that on later ARMs, this notion does not work due to |
| 47 | * speculative prefetches. We model our approach on the assumption that |
| 48 | * the CPU does do speculative prefetches, which means we clean caches |
| 49 | * before transfers and delay cache invalidation until transfer completion. |
| 50 | * |
Marek Szyprowski | 75cd651 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 51 | */ |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 52 | static void __dma_page_cpu_to_dev(struct page *, unsigned long, |
Marek Szyprowski | 75cd651 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 53 | size_t, enum dma_data_direction); |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 54 | static void __dma_page_dev_to_cpu(struct page *, unsigned long, |
Marek Szyprowski | 75cd651 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 55 | size_t, enum dma_data_direction); |
| 56 | |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 57 | /** |
| 58 | * arm_dma_map_page - map a portion of a page for streaming DMA |
| 59 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 60 | * @page: page that buffer resides in |
| 61 | * @offset: offset into page for start of buffer |
| 62 | * @size: size of buffer to map |
| 63 | * @dir: DMA transfer direction |
| 64 | * |
| 65 | * Ensure that any data held in the cache is appropriately discarded |
| 66 | * or written back. |
| 67 | * |
| 68 | * The device owns this memory once this call has completed. The CPU |
| 69 | * can regain ownership by calling dma_unmap_page(). |
| 70 | */ |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 71 | static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page, |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 72 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 73 | struct dma_attrs *attrs) |
| 74 | { |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 75 | if (!arch_is_coherent()) |
| 76 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 77 | return pfn_to_dma(dev, page_to_pfn(page)) + offset; |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page() |
| 82 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 83 | * @handle: DMA address of buffer |
| 84 | * @size: size of buffer (same as passed to dma_map_page) |
| 85 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 86 | * |
| 87 | * Unmap a page streaming mode DMA translation. The handle and size |
| 88 | * must match what was provided in the previous dma_map_page() call. |
| 89 | * All other usages are undefined. |
| 90 | * |
| 91 | * After this call, reads by the CPU to the buffer are guaranteed to see |
| 92 | * whatever the device wrote there. |
| 93 | */ |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 94 | static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle, |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 95 | size_t size, enum dma_data_direction dir, |
| 96 | struct dma_attrs *attrs) |
| 97 | { |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 98 | if (!arch_is_coherent()) |
| 99 | __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)), |
| 100 | handle & ~PAGE_MASK, size, dir); |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 103 | static void arm_dma_sync_single_for_cpu(struct device *dev, |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 104 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 105 | { |
| 106 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 107 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 108 | if (!arch_is_coherent()) |
| 109 | __dma_page_dev_to_cpu(page, offset, size, dir); |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 112 | static void arm_dma_sync_single_for_device(struct device *dev, |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 113 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 114 | { |
| 115 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 116 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 117 | if (!arch_is_coherent()) |
| 118 | __dma_page_cpu_to_dev(page, offset, size, dir); |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask); |
| 122 | |
| 123 | struct dma_map_ops arm_dma_ops = { |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 124 | .alloc = arm_dma_alloc, |
| 125 | .free = arm_dma_free, |
| 126 | .mmap = arm_dma_mmap, |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 127 | .map_page = arm_dma_map_page, |
| 128 | .unmap_page = arm_dma_unmap_page, |
| 129 | .map_sg = arm_dma_map_sg, |
| 130 | .unmap_sg = arm_dma_unmap_sg, |
| 131 | .sync_single_for_cpu = arm_dma_sync_single_for_cpu, |
| 132 | .sync_single_for_device = arm_dma_sync_single_for_device, |
| 133 | .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu, |
| 134 | .sync_sg_for_device = arm_dma_sync_sg_for_device, |
| 135 | .set_dma_mask = arm_dma_set_mask, |
| 136 | }; |
| 137 | EXPORT_SYMBOL(arm_dma_ops); |
| 138 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 139 | static u64 get_coherent_dma_mask(struct device *dev) |
| 140 | { |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 141 | u64 mask = (u64)arm_dma_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 143 | if (dev) { |
| 144 | mask = dev->coherent_dma_mask; |
| 145 | |
| 146 | /* |
| 147 | * Sanity check the DMA mask - it must be non-zero, and |
| 148 | * must be able to be satisfied by a DMA allocation. |
| 149 | */ |
| 150 | if (mask == 0) { |
| 151 | dev_warn(dev, "coherent DMA mask is unset\n"); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 155 | if ((~mask) & (u64)arm_dma_limit) { |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 156 | dev_warn(dev, "coherent DMA mask %#llx is smaller " |
| 157 | "than system GFP_DMA mask %#llx\n", |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 158 | mask, (u64)arm_dma_limit); |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return mask; |
| 164 | } |
| 165 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 166 | static void __dma_clear_buffer(struct page *page, size_t size) |
| 167 | { |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 168 | /* |
| 169 | * Ensure that the allocated pages are zeroed, and that any data |
| 170 | * lurking in the kernel direct-mapped region is invalidated. |
| 171 | */ |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 172 | if (!PageHighMem(page)) { |
| 173 | void *ptr = page_address(page); |
| 174 | if (ptr) { |
| 175 | memset(ptr, 0, size); |
| 176 | dmac_flush_range(ptr, ptr + size); |
| 177 | outer_flush_range(__pa(ptr), __pa(ptr) + size); |
| 178 | } |
| 179 | } else { |
| 180 | phys_addr_t base = __pfn_to_phys(page_to_pfn(page)); |
| 181 | phys_addr_t end = base + size; |
| 182 | while (size > 0) { |
| 183 | void *ptr = kmap_atomic(page); |
| 184 | memset(ptr, 0, PAGE_SIZE); |
| 185 | dmac_flush_range(ptr, ptr + PAGE_SIZE); |
| 186 | kunmap_atomic(ptr); |
| 187 | page++; |
| 188 | size -= PAGE_SIZE; |
| 189 | } |
| 190 | outer_flush_range(base, end); |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 191 | } |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Allocate a DMA buffer for 'dev' of size 'size' using the |
| 196 | * specified gfp mask. Note that 'size' must be page aligned. |
| 197 | */ |
| 198 | static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) |
| 199 | { |
| 200 | unsigned long order = get_order(size); |
| 201 | struct page *page, *p, *e; |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 202 | |
| 203 | page = alloc_pages(gfp, order); |
| 204 | if (!page) |
| 205 | return NULL; |
| 206 | |
| 207 | /* |
| 208 | * Now split the huge page and free the excess pages |
| 209 | */ |
| 210 | split_page(page, order); |
| 211 | for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) |
| 212 | __free_page(p); |
| 213 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 214 | __dma_clear_buffer(page, size); |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 215 | |
| 216 | return page; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Free a DMA buffer. 'size' must be page aligned. |
| 221 | */ |
| 222 | static void __dma_free_buffer(struct page *page, size_t size) |
| 223 | { |
| 224 | struct page *e = page + (size >> PAGE_SHIFT); |
| 225 | |
| 226 | while (page < e) { |
| 227 | __free_page(page); |
| 228 | page++; |
| 229 | } |
| 230 | } |
| 231 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 232 | #ifdef CONFIG_MMU |
Catalin Marinas | a5e9d38 | 2010-06-21 15:09:06 +0100 | [diff] [blame] | 233 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 234 | #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT) |
Linus Torvalds | 1fdb24e | 2011-10-28 12:02:27 -0700 | [diff] [blame] | 235 | #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT) |
Catalin Marinas | a5e9d38 | 2010-06-21 15:09:06 +0100 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /* |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 238 | * These are the page tables (2MB each) covering uncached, DMA consistent allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | */ |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 240 | static pte_t **consistent_pte; |
| 241 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 242 | #define DEFAULT_CONSISTENT_DMA_SIZE (7*SZ_2M) |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 243 | |
Sachin Kamat | bf8effb | 2012-06-06 08:03:35 +0200 | [diff] [blame] | 244 | static unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE; |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 245 | |
| 246 | void __init init_consistent_dma_size(unsigned long size) |
| 247 | { |
| 248 | unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M); |
| 249 | |
| 250 | BUG_ON(consistent_pte); /* Check we're called before DMA region init */ |
| 251 | BUG_ON(base < VMALLOC_END); |
| 252 | |
| 253 | /* Grow region to accommodate specified size */ |
| 254 | if (base < consistent_base) |
| 255 | consistent_base = base; |
| 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 258 | #include "vmregion.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 260 | static struct arm_vmregion_head consistent_head = { |
| 261 | .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | .vm_list = LIST_HEAD_INIT(consistent_head.vm_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | .vm_end = CONSISTENT_END, |
| 264 | }; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_HUGETLB_PAGE |
| 267 | #error ARM Coherent DMA allocator does not (yet) support huge TLB |
| 268 | #endif |
| 269 | |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 270 | /* |
| 271 | * Initialise the consistent memory allocation. |
| 272 | */ |
| 273 | static int __init consistent_init(void) |
| 274 | { |
| 275 | int ret = 0; |
| 276 | pgd_t *pgd; |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 277 | pud_t *pud; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 278 | pmd_t *pmd; |
| 279 | pte_t *pte; |
| 280 | int i = 0; |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 281 | unsigned long base = consistent_base; |
Catalin Marinas | 53cbcbc | 2011-11-17 13:11:21 +0100 | [diff] [blame] | 282 | unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT; |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 283 | |
Marek Szyprowski | 5ee6b06 | 2012-05-30 10:48:29 +0200 | [diff] [blame] | 284 | if (IS_ENABLED(CONFIG_CMA) && !IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 285 | return 0; |
| 286 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 287 | consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL); |
| 288 | if (!consistent_pte) { |
| 289 | pr_err("%s: no memory\n", __func__); |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
| 293 | pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END); |
| 294 | consistent_head.vm_start = base; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 295 | |
| 296 | do { |
| 297 | pgd = pgd_offset(&init_mm, base); |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 298 | |
| 299 | pud = pud_alloc(&init_mm, pgd, base); |
| 300 | if (!pud) { |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 301 | pr_err("%s: no pud tables\n", __func__); |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 302 | ret = -ENOMEM; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | pmd = pmd_alloc(&init_mm, pud, base); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 307 | if (!pmd) { |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 308 | pr_err("%s: no pmd tables\n", __func__); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 309 | ret = -ENOMEM; |
| 310 | break; |
| 311 | } |
| 312 | WARN_ON(!pmd_none(*pmd)); |
| 313 | |
| 314 | pte = pte_alloc_kernel(pmd, base); |
| 315 | if (!pte) { |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 316 | pr_err("%s: no pte tables\n", __func__); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 317 | ret = -ENOMEM; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | consistent_pte[i++] = pte; |
Catalin Marinas | e73fc88 | 2011-08-23 14:07:23 +0100 | [diff] [blame] | 322 | base += PMD_SIZE; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 323 | } while (base < CONSISTENT_END); |
| 324 | |
| 325 | return ret; |
| 326 | } |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 327 | core_initcall(consistent_init); |
| 328 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 329 | static void *__alloc_from_contiguous(struct device *dev, size_t size, |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 330 | pgprot_t prot, struct page **ret_page, |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 331 | bool no_kernel_mapping, const void *caller); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 332 | |
| 333 | static struct arm_vmregion_head coherent_head = { |
| 334 | .vm_lock = __SPIN_LOCK_UNLOCKED(&coherent_head.vm_lock), |
| 335 | .vm_list = LIST_HEAD_INIT(coherent_head.vm_list), |
| 336 | }; |
| 337 | |
Sachin Kamat | bf8effb | 2012-06-06 08:03:35 +0200 | [diff] [blame] | 338 | static size_t coherent_pool_size = DEFAULT_CONSISTENT_DMA_SIZE / 8; |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 339 | |
| 340 | static int __init early_coherent_pool(char *p) |
| 341 | { |
| 342 | coherent_pool_size = memparse(p, &p); |
| 343 | return 0; |
| 344 | } |
| 345 | early_param("coherent_pool", early_coherent_pool); |
| 346 | |
| 347 | /* |
| 348 | * Initialise the coherent pool for atomic allocations. |
| 349 | */ |
| 350 | static int __init coherent_init(void) |
| 351 | { |
| 352 | pgprot_t prot = pgprot_dmacoherent(pgprot_kernel); |
| 353 | size_t size = coherent_pool_size; |
| 354 | struct page *page; |
| 355 | void *ptr; |
| 356 | |
Marek Szyprowski | 5ee6b06 | 2012-05-30 10:48:29 +0200 | [diff] [blame] | 357 | if (!IS_ENABLED(CONFIG_CMA)) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 360 | ptr = __alloc_from_contiguous(NULL, size, prot, &page, false, |
| 361 | coherent_init); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 362 | if (ptr) { |
| 363 | coherent_head.vm_start = (unsigned long) ptr; |
| 364 | coherent_head.vm_end = (unsigned long) ptr + size; |
| 365 | printk(KERN_INFO "DMA: preallocated %u KiB pool for atomic coherent allocations\n", |
| 366 | (unsigned)size / 1024); |
| 367 | return 0; |
| 368 | } |
| 369 | printk(KERN_ERR "DMA: failed to allocate %u KiB pool for atomic coherent allocation\n", |
| 370 | (unsigned)size / 1024); |
| 371 | return -ENOMEM; |
| 372 | } |
| 373 | /* |
| 374 | * CMA is activated by core_initcall, so we must be called after it. |
| 375 | */ |
| 376 | postcore_initcall(coherent_init); |
| 377 | |
| 378 | struct dma_contig_early_reserve { |
| 379 | phys_addr_t base; |
| 380 | unsigned long size; |
| 381 | }; |
| 382 | |
| 383 | static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata; |
| 384 | |
| 385 | static int dma_mmu_remap_num __initdata; |
| 386 | |
| 387 | void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) |
| 388 | { |
| 389 | dma_mmu_remap[dma_mmu_remap_num].base = base; |
| 390 | dma_mmu_remap[dma_mmu_remap_num].size = size; |
| 391 | dma_mmu_remap_num++; |
| 392 | } |
| 393 | |
| 394 | void __init dma_contiguous_remap(void) |
| 395 | { |
| 396 | int i; |
| 397 | for (i = 0; i < dma_mmu_remap_num; i++) { |
| 398 | phys_addr_t start = dma_mmu_remap[i].base; |
| 399 | phys_addr_t end = start + dma_mmu_remap[i].size; |
| 400 | struct map_desc map; |
| 401 | unsigned long addr; |
| 402 | |
| 403 | if (end > arm_lowmem_limit) |
| 404 | end = arm_lowmem_limit; |
| 405 | if (start >= end) |
Chris Brand | 8892fbb | 2012-08-07 14:01:14 +0200 | [diff] [blame] | 406 | continue; |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 407 | |
| 408 | map.pfn = __phys_to_pfn(start); |
| 409 | map.virtual = __phys_to_virt(start); |
| 410 | map.length = end - start; |
| 411 | map.type = MT_MEMORY_DMA_READY; |
| 412 | |
| 413 | /* |
| 414 | * Clear previous low-memory mapping |
| 415 | */ |
| 416 | for (addr = __phys_to_virt(start); addr < __phys_to_virt(end); |
Vitaly Andrianov | 808e303 | 2012-05-14 13:49:56 -0400 | [diff] [blame] | 417 | addr += PMD_SIZE) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 418 | pmd_clear(pmd_off_k(addr)); |
| 419 | |
| 420 | iotable_init(&map, 1); |
| 421 | } |
| 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | static void * |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 425 | __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot, |
| 426 | const void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 428 | struct arm_vmregion *c; |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 429 | size_t align; |
| 430 | int bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 432 | if (!consistent_pte) { |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 433 | pr_err("%s: not initialised\n", __func__); |
Russell King | ebd7a84 | 2009-11-19 20:58:31 +0000 | [diff] [blame] | 434 | dump_stack(); |
Russell King | ebd7a84 | 2009-11-19 20:58:31 +0000 | [diff] [blame] | 435 | return NULL; |
| 436 | } |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | /* |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 439 | * Align the virtual region allocation - maximum alignment is |
| 440 | * a section size, minimum is a page size. This helps reduce |
| 441 | * fragmentation of the DMA space, and also prevents allocations |
| 442 | * smaller than a section from crossing a section boundary. |
| 443 | */ |
Russell King | c947f69 | 2010-11-03 16:00:15 +0000 | [diff] [blame] | 444 | bit = fls(size - 1); |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 445 | if (bit > SECTION_SHIFT) |
| 446 | bit = SECTION_SHIFT; |
| 447 | align = 1 << bit; |
| 448 | |
| 449 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * Allocate a virtual address in the consistent mapping region. |
| 451 | */ |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 452 | c = arm_vmregion_alloc(&consistent_head, align, size, |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 453 | gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (c) { |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 455 | pte_t *pte; |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 456 | int idx = CONSISTENT_PTE_INDEX(c->vm_start); |
| 457 | u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 459 | pte = consistent_pte[idx] + off; |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 460 | c->priv = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | do { |
| 463 | BUG_ON(!pte_none(*pte)); |
| 464 | |
Russell King | ad1ae2f | 2006-12-13 14:34:43 +0000 | [diff] [blame] | 465 | set_pte_ext(pte, mk_pte(page, prot), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | page++; |
| 467 | pte++; |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 468 | off++; |
| 469 | if (off >= PTRS_PER_PTE) { |
| 470 | off = 0; |
| 471 | pte = consistent_pte[++idx]; |
| 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } while (size -= PAGE_SIZE); |
| 474 | |
Russell King | 2be23c4 | 2010-09-08 16:27:56 +0100 | [diff] [blame] | 475 | dsb(); |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | return (void *)c->vm_start; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 481 | |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 482 | static void __dma_free_remap(void *cpu_addr, size_t size, bool no_warn) |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 483 | { |
| 484 | struct arm_vmregion *c; |
| 485 | unsigned long addr; |
| 486 | pte_t *ptep; |
| 487 | int idx; |
| 488 | u32 off; |
| 489 | |
| 490 | c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr); |
| 491 | if (!c) { |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 492 | if (!no_warn) { |
| 493 | pr_err("%s: trying to free invalid coherent area: %p\n", |
| 494 | __func__, cpu_addr); |
| 495 | dump_stack(); |
| 496 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
| 500 | if ((c->vm_end - c->vm_start) != size) { |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 501 | pr_err("%s: freeing wrong coherent size (%ld != %d)\n", |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 502 | __func__, c->vm_end - c->vm_start, size); |
| 503 | dump_stack(); |
| 504 | size = c->vm_end - c->vm_start; |
| 505 | } |
| 506 | |
| 507 | idx = CONSISTENT_PTE_INDEX(c->vm_start); |
| 508 | off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); |
| 509 | ptep = consistent_pte[idx] + off; |
| 510 | addr = c->vm_start; |
| 511 | do { |
| 512 | pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep); |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 513 | |
| 514 | ptep++; |
| 515 | addr += PAGE_SIZE; |
| 516 | off++; |
| 517 | if (off >= PTRS_PER_PTE) { |
| 518 | off = 0; |
| 519 | ptep = consistent_pte[++idx]; |
| 520 | } |
| 521 | |
Russell King | acaac25 | 2009-11-20 18:19:52 +0000 | [diff] [blame] | 522 | if (pte_none(pte) || !pte_present(pte)) |
Marek Szyprowski | e2a8e41 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 523 | pr_crit("%s: bad page in kernel page table\n", |
| 524 | __func__); |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 525 | } while (size -= PAGE_SIZE); |
| 526 | |
| 527 | flush_tlb_kernel_range(c->vm_start, c->vm_end); |
| 528 | |
| 529 | arm_vmregion_free(&consistent_head, c); |
| 530 | } |
| 531 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 532 | static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr, |
| 533 | void *data) |
| 534 | { |
| 535 | struct page *page = virt_to_page(addr); |
| 536 | pgprot_t prot = *(pgprot_t *)data; |
| 537 | |
| 538 | set_pte_ext(pte, mk_pte(page, prot), 0); |
| 539 | return 0; |
| 540 | } |
| 541 | |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 542 | static int __dma_clear_pte(pte_t *pte, pgtable_t token, unsigned long addr, |
| 543 | void *data) |
| 544 | { |
| 545 | pte_clear(&init_mm, addr, pte); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static void __dma_remap(struct page *page, size_t size, pgprot_t prot, |
| 550 | bool no_kernel_map) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 551 | { |
| 552 | unsigned long start = (unsigned long) page_address(page); |
| 553 | unsigned end = start + size; |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 554 | int (*func)(pte_t *pte, pgtable_t token, unsigned long addr, |
| 555 | void *data); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 556 | |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 557 | if (no_kernel_map) |
| 558 | func = __dma_clear_pte; |
| 559 | else |
| 560 | func = __dma_update_pte; |
| 561 | |
| 562 | apply_to_page_range(&init_mm, start, size, func, &prot); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 563 | dsb(); |
| 564 | flush_tlb_kernel_range(start, end); |
| 565 | } |
| 566 | |
| 567 | static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp, |
| 568 | pgprot_t prot, struct page **ret_page, |
| 569 | const void *caller) |
| 570 | { |
| 571 | struct page *page; |
| 572 | void *ptr; |
| 573 | page = __dma_alloc_buffer(dev, size, gfp); |
| 574 | if (!page) |
| 575 | return NULL; |
| 576 | |
| 577 | ptr = __dma_alloc_remap(page, size, gfp, prot, caller); |
| 578 | if (!ptr) { |
| 579 | __dma_free_buffer(page, size); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | *ret_page = page; |
| 584 | return ptr; |
| 585 | } |
| 586 | |
| 587 | static void *__alloc_from_pool(struct device *dev, size_t size, |
| 588 | struct page **ret_page, const void *caller) |
| 589 | { |
| 590 | struct arm_vmregion *c; |
| 591 | size_t align; |
| 592 | |
| 593 | if (!coherent_head.vm_start) { |
| 594 | printk(KERN_ERR "%s: coherent pool not initialised!\n", |
| 595 | __func__); |
| 596 | dump_stack(); |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Align the region allocation - allocations from pool are rather |
| 602 | * small, so align them to their order in pages, minimum is a page |
| 603 | * size. This helps reduce fragmentation of the DMA space. |
| 604 | */ |
| 605 | align = PAGE_SIZE << get_order(size); |
| 606 | c = arm_vmregion_alloc(&coherent_head, align, size, 0, caller); |
| 607 | if (c) { |
| 608 | void *ptr = (void *)c->vm_start; |
| 609 | struct page *page = virt_to_page(ptr); |
| 610 | *ret_page = page; |
| 611 | return ptr; |
| 612 | } |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | static int __free_from_pool(void *cpu_addr, size_t size) |
| 617 | { |
| 618 | unsigned long start = (unsigned long)cpu_addr; |
| 619 | unsigned long end = start + size; |
| 620 | struct arm_vmregion *c; |
| 621 | |
| 622 | if (start < coherent_head.vm_start || end > coherent_head.vm_end) |
| 623 | return 0; |
| 624 | |
| 625 | c = arm_vmregion_find_remove(&coherent_head, (unsigned long)start); |
| 626 | |
| 627 | if ((c->vm_end - c->vm_start) != size) { |
| 628 | printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n", |
| 629 | __func__, c->vm_end - c->vm_start, size); |
| 630 | dump_stack(); |
| 631 | size = c->vm_end - c->vm_start; |
| 632 | } |
| 633 | |
| 634 | arm_vmregion_free(&coherent_head, c); |
| 635 | return 1; |
| 636 | } |
| 637 | |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 638 | #define NO_KERNEL_MAPPING_DUMMY 0x2222 |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 639 | static void *__alloc_from_contiguous(struct device *dev, size_t size, |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 640 | pgprot_t prot, struct page **ret_page, |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 641 | bool no_kernel_mapping, |
| 642 | const void *caller) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 643 | { |
| 644 | unsigned long order = get_order(size); |
| 645 | size_t count = size >> PAGE_SHIFT; |
| 646 | struct page *page; |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 647 | void *ptr; |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 648 | |
| 649 | page = dma_alloc_from_contiguous(dev, count, order); |
| 650 | if (!page) |
| 651 | return NULL; |
| 652 | |
| 653 | __dma_clear_buffer(page, size); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 654 | |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 655 | if (!PageHighMem(page)) { |
| 656 | __dma_remap(page, size, prot, no_kernel_mapping); |
| 657 | ptr = page_address(page); |
| 658 | } else { |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 659 | if (no_kernel_mapping) { |
| 660 | /* |
| 661 | * Something non-NULL needs to be returned here. Give |
| 662 | * back a dummy address that is unmapped to catch |
| 663 | * clients trying to use the address incorrectly |
| 664 | */ |
| 665 | ptr = (void *)NO_KERNEL_MAPPING_DUMMY; |
| 666 | } else { |
| 667 | ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, |
| 668 | caller); |
| 669 | if (!ptr) { |
| 670 | dma_release_from_contiguous(dev, page, count); |
| 671 | return NULL; |
| 672 | } |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 675 | *ret_page = page; |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 676 | return ptr; |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static void __free_from_contiguous(struct device *dev, struct page *page, |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 680 | void *cpu_addr, size_t size) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 681 | { |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 682 | if (!PageHighMem(page)) |
| 683 | __dma_remap(page, size, pgprot_kernel, false); |
| 684 | else |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 685 | __dma_free_remap(cpu_addr, size, true); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 686 | dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); |
| 687 | } |
| 688 | |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 689 | static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot) |
| 690 | { |
Laura Abbott | b203402 | 2012-10-29 11:54:38 -0700 | [diff] [blame] | 691 | if (dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs)) |
| 692 | prot = pgprot_writecombine(prot); |
| 693 | else if (dma_get_attr(DMA_ATTR_STRONGLY_ORDERED, attrs)) |
| 694 | prot = pgprot_stronglyordered(prot); |
| 695 | /* if non-consistent just pass back what was given */ |
| 696 | else if (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) |
| 697 | prot = pgprot_dmacoherent(prot); |
| 698 | |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 699 | return prot; |
| 700 | } |
| 701 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 702 | #define nommu() 0 |
| 703 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 704 | #else /* !CONFIG_MMU */ |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 705 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 706 | #define nommu() 1 |
| 707 | |
| 708 | #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c) NULL |
| 709 | #define __alloc_from_pool(dev, size, ret_page, c) NULL |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 710 | #define __alloc_from_contiguous(dev, size, prot, ret, w) NULL |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 711 | #define __free_from_pool(cpu_addr, size) 0 |
| 712 | #define __free_from_contiguous(dev, page, size) do { } while (0) |
| 713 | #define __dma_free_remap(cpu_addr, size) do { } while (0) |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 714 | #define __get_dma_pgprot(attrs, prot) __pgprot(0) |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 715 | |
| 716 | #endif /* CONFIG_MMU */ |
| 717 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 718 | static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp, |
| 719 | struct page **ret_page) |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 720 | { |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 721 | struct page *page; |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 722 | page = __dma_alloc_buffer(dev, size, gfp); |
| 723 | if (!page) |
| 724 | return NULL; |
| 725 | |
| 726 | *ret_page = page; |
| 727 | return page_address(page); |
| 728 | } |
| 729 | |
| 730 | |
| 731 | |
| 732 | static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 733 | gfp_t gfp, pgprot_t prot, const void *caller, |
| 734 | bool no_kernel_mapping) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 735 | { |
| 736 | u64 mask = get_coherent_dma_mask(dev); |
| 737 | struct page *page; |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 738 | void *addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 739 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 740 | #ifdef CONFIG_DMA_API_DEBUG |
| 741 | u64 limit = (mask + 1) & ~mask; |
| 742 | if (limit && size >= limit) { |
| 743 | dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n", |
| 744 | size, mask); |
| 745 | return NULL; |
| 746 | } |
| 747 | #endif |
| 748 | |
| 749 | if (!mask) |
| 750 | return NULL; |
| 751 | |
| 752 | if (mask < 0xffffffffULL) |
| 753 | gfp |= GFP_DMA; |
| 754 | |
Sumit Bhattacharya | ea2e705 | 2011-11-24 00:47:12 +0100 | [diff] [blame] | 755 | /* |
| 756 | * Following is a work-around (a.k.a. hack) to prevent pages |
| 757 | * with __GFP_COMP being passed to split_page() which cannot |
| 758 | * handle them. The real problem is that this flag probably |
| 759 | * should be 0 on ARM as it is not supported on this |
| 760 | * platform; see CONFIG_HUGETLBFS. |
| 761 | */ |
| 762 | gfp &= ~(__GFP_COMP); |
| 763 | |
Marek Szyprowski | 1dc8f00 | 2012-02-29 14:45:28 +0100 | [diff] [blame] | 764 | *handle = DMA_ERROR_CODE; |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 765 | size = PAGE_ALIGN(size); |
| 766 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 767 | if (arch_is_coherent() || nommu()) |
| 768 | addr = __alloc_simple_buffer(dev, size, gfp, &page); |
Marek Szyprowski | 5ee6b06 | 2012-05-30 10:48:29 +0200 | [diff] [blame] | 769 | else if (!IS_ENABLED(CONFIG_CMA)) |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 770 | addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller); |
| 771 | else if (gfp & GFP_ATOMIC) |
| 772 | addr = __alloc_from_pool(dev, size, &page, caller); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 773 | else |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 774 | addr = __alloc_from_contiguous(dev, size, prot, &page, |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 775 | no_kernel_mapping, caller); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 776 | |
| 777 | if (addr) |
Russell King | 9eedd96 | 2011-01-03 00:00:17 +0000 | [diff] [blame] | 778 | *handle = pfn_to_dma(dev, page_to_pfn(page)); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 779 | |
| 780 | return addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 781 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | /* |
| 784 | * Allocate DMA-coherent memory space and return both the kernel remapped |
| 785 | * virtual and bus address for that space. |
| 786 | */ |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 787 | void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
| 788 | gfp_t gfp, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | { |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 790 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 791 | void *memory; |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 792 | bool no_kernel_mapping = dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, |
| 793 | attrs); |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 794 | |
| 795 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) |
| 796 | return memory; |
| 797 | |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 798 | return __dma_alloc(dev, size, handle, gfp, prot, |
Laura Abbott | 4f5b9de | 2012-10-29 13:38:25 -0700 | [diff] [blame] | 799 | __builtin_return_address(0), no_kernel_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | /* |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 803 | * Create userspace mapping for the DMA-coherent memory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | */ |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 805 | int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma, |
| 806 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 807 | struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 809 | int ret = -ENXIO; |
| 810 | #ifdef CONFIG_MMU |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 811 | unsigned long pfn = dma_to_pfn(dev, dma_addr); |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 812 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); |
Marek Szyprowski | f504f8e | 2012-05-15 19:04:13 +0200 | [diff] [blame] | 813 | |
| 814 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) |
| 815 | return ret; |
| 816 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 817 | ret = remap_pfn_range(vma, vma->vm_start, |
| 818 | pfn + vma->vm_pgoff, |
| 819 | vma->vm_end - vma->vm_start, |
| 820 | vma->vm_page_prot); |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 821 | #endif /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | /* |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 827 | * Free a buffer as defined by the above mapping. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | */ |
Marek Szyprowski | a93786a | 2012-05-16 18:31:23 +0200 | [diff] [blame] | 829 | void arm_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 830 | dma_addr_t handle, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 832 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); |
Russell King | 5edf71a | 2005-11-25 15:52:51 +0000 | [diff] [blame] | 833 | |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 834 | if (dma_release_from_coherent(dev, get_order(size), cpu_addr)) |
| 835 | return; |
| 836 | |
Russell King | 3e82d01 | 2009-11-19 15:38:12 +0000 | [diff] [blame] | 837 | size = PAGE_ALIGN(size); |
| 838 | |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 839 | if (arch_is_coherent() || nommu()) { |
| 840 | __dma_free_buffer(page, size); |
Marek Szyprowski | 5ee6b06 | 2012-05-30 10:48:29 +0200 | [diff] [blame] | 841 | } else if (!IS_ENABLED(CONFIG_CMA)) { |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 842 | __dma_free_remap(cpu_addr, size, false); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 843 | __dma_free_buffer(page, size); |
| 844 | } else { |
| 845 | if (__free_from_pool(cpu_addr, size)) |
| 846 | return; |
| 847 | /* |
| 848 | * Non-atomic allocations cannot be freed with IRQs disabled |
| 849 | */ |
| 850 | WARN_ON(irqs_disabled()); |
Marek Szyprowski | 44d60d6 | 2013-01-16 16:31:22 +0100 | [diff] [blame] | 851 | __free_from_contiguous(dev, page, cpu_addr, size); |
Marek Szyprowski | d4398df | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 855 | static void dma_cache_maint_page(struct page *page, unsigned long offset, |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 856 | size_t size, enum dma_data_direction dir, |
| 857 | void (*op)(const void *, size_t, int)) |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 858 | { |
Russell King | e4e9dc8 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 859 | unsigned long pfn; |
| 860 | size_t left = size; |
| 861 | |
| 862 | pfn = page_to_pfn(page) + offset / PAGE_SIZE; |
| 863 | offset %= PAGE_SIZE; |
| 864 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 865 | /* |
| 866 | * A single sg entry may refer to multiple physically contiguous |
| 867 | * pages. But we still need to process highmem pages individually. |
| 868 | * If highmem is not configured then the bulk of this loop gets |
| 869 | * optimized out. |
| 870 | */ |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 871 | do { |
| 872 | size_t len = left; |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 873 | void *vaddr; |
| 874 | |
Russell King | e4e9dc8 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 875 | page = pfn_to_page(pfn); |
| 876 | |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 877 | if (PageHighMem(page)) { |
Russell King | e4e9dc8 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 878 | if (len + offset > PAGE_SIZE) |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 879 | len = PAGE_SIZE - offset; |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 880 | vaddr = kmap_high_get(page); |
| 881 | if (vaddr) { |
| 882 | vaddr += offset; |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 883 | op(vaddr, len, dir); |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 884 | kunmap_high(page); |
Nicolas Pitre | 7e5a69e | 2010-03-29 21:46:02 +0100 | [diff] [blame] | 885 | } else if (cache_is_vipt()) { |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 886 | /* unmapped pages might still be cached */ |
| 887 | vaddr = kmap_atomic(page); |
Nicolas Pitre | 7e5a69e | 2010-03-29 21:46:02 +0100 | [diff] [blame] | 888 | op(vaddr + offset, len, dir); |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 889 | kunmap_atomic(vaddr); |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 890 | } |
| 891 | } else { |
| 892 | vaddr = page_address(page) + offset; |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 893 | op(vaddr, len, dir); |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 894 | } |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 895 | offset = 0; |
Russell King | e4e9dc8 | 2013-01-19 11:05:57 +0000 | [diff] [blame] | 896 | pfn++; |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 897 | left -= len; |
| 898 | } while (left); |
| 899 | } |
| 900 | |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 901 | /* |
| 902 | * Make an area consistent for devices. |
| 903 | * Note: Drivers should NOT use this function directly, as it will break |
| 904 | * platforms with CONFIG_DMABOUNCE. |
| 905 | * Use the driver DMA support - see dma-mapping.h (dma_sync_*) |
| 906 | */ |
| 907 | 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] | 908 | size_t size, enum dma_data_direction dir) |
| 909 | { |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 910 | unsigned long paddr; |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 911 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 912 | dma_cache_maint_page(page, off, size, dir, dmac_map_area); |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 913 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 914 | paddr = page_to_phys(page) + off; |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 915 | if (dir == DMA_FROM_DEVICE) { |
| 916 | outer_inv_range(paddr, paddr + size); |
| 917 | } else { |
| 918 | outer_clean_range(paddr, paddr + size); |
| 919 | } |
| 920 | /* FIXME: non-speculating: flush on bidirectional mappings? */ |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 921 | } |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 922 | |
Marek Szyprowski | abe2a2f | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 923 | 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] | 924 | size_t size, enum dma_data_direction dir) |
| 925 | { |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 926 | unsigned long paddr = page_to_phys(page) + off; |
| 927 | |
| 928 | /* FIXME: non-speculating: not required */ |
| 929 | /* don't bother invalidating if DMA to device */ |
| 930 | if (dir != DMA_TO_DEVICE) |
| 931 | outer_inv_range(paddr, paddr + size); |
| 932 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 933 | dma_cache_maint_page(page, off, size, dir, dmac_unmap_area); |
Catalin Marinas | c017780 | 2010-09-13 15:57:36 +0100 | [diff] [blame] | 934 | |
| 935 | /* |
| 936 | * Mark the D-cache clean for this page to avoid extra flushing. |
| 937 | */ |
| 938 | if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE) |
| 939 | set_bit(PG_dcache_clean, &page->flags); |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 940 | } |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 941 | |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 942 | /** |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 943 | * 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] | 944 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 945 | * @sg: list of buffers |
| 946 | * @nents: number of buffers to map |
| 947 | * @dir: DMA transfer direction |
| 948 | * |
| 949 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 950 | * This is the scatter-gather version of the dma_map_single interface. |
| 951 | * Here the scatter gather list elements are each tagged with the |
| 952 | * appropriate dma address and length. They are obtained via |
| 953 | * sg_dma_{address,length}. |
| 954 | * |
| 955 | * Device ownership issues as mentioned for dma_map_single are the same |
| 956 | * here. |
| 957 | */ |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 958 | int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 959 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 960 | { |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 961 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 962 | struct scatterlist *s; |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 963 | int i, j; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 964 | |
| 965 | for_each_sg(sg, s, nents, i) { |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 966 | #ifdef CONFIG_NEED_SG_DMA_LENGTH |
| 967 | s->dma_length = s->length; |
| 968 | #endif |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 969 | s->dma_address = ops->map_page(dev, sg_page(s), s->offset, |
| 970 | s->length, dir, attrs); |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 971 | if (dma_mapping_error(dev, s->dma_address)) |
| 972 | goto bad_mapping; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 973 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 974 | return nents; |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 975 | |
| 976 | bad_mapping: |
| 977 | for_each_sg(sg, s, i, j) |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 978 | ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 979 | return 0; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 980 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 981 | |
| 982 | /** |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 983 | * 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] | 984 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 985 | * @sg: list of buffers |
Linus Walleij | 0adfca6 | 2011-01-12 18:50:37 +0100 | [diff] [blame] | 986 | * @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] | 987 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 988 | * |
| 989 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 990 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 991 | */ |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 992 | void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 993 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 994 | { |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 995 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 996 | struct scatterlist *s; |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 997 | |
Russell King | 01135d9 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 998 | int i; |
| 999 | |
| 1000 | for_each_sg(sg, s, nents, i) |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1001 | 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] | 1002 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1003 | |
| 1004 | /** |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1005 | * arm_dma_sync_sg_for_cpu |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1006 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 1007 | * @sg: list of buffers |
| 1008 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1009 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1010 | */ |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1011 | 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] | 1012 | int nents, enum dma_data_direction dir) |
| 1013 | { |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1014 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1015 | struct scatterlist *s; |
| 1016 | int i; |
| 1017 | |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1018 | for_each_sg(sg, s, nents, i) |
| 1019 | ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length, |
| 1020 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1021 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1022 | |
| 1023 | /** |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1024 | * arm_dma_sync_sg_for_device |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1025 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 1026 | * @sg: list of buffers |
| 1027 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1028 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1029 | */ |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1030 | 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] | 1031 | int nents, enum dma_data_direction dir) |
| 1032 | { |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1033 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1034 | struct scatterlist *s; |
| 1035 | int i; |
| 1036 | |
Marek Szyprowski | 253a140 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1037 | for_each_sg(sg, s, nents, i) |
| 1038 | ops->sync_single_for_device(dev, sg_dma_address(s), s->length, |
| 1039 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 1040 | } |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 1041 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1042 | /* |
| 1043 | * Return whether the given device DMA address mask can be supported |
| 1044 | * properly. For example, if your device can only drive the low 24-bits |
| 1045 | * during bus mastering, then you would pass 0x00ffffff as the mask |
| 1046 | * to this function. |
| 1047 | */ |
| 1048 | int dma_supported(struct device *dev, u64 mask) |
| 1049 | { |
| 1050 | if (mask < (u64)arm_dma_limit) |
| 1051 | return 0; |
| 1052 | return 1; |
| 1053 | } |
| 1054 | EXPORT_SYMBOL(dma_supported); |
| 1055 | |
Marek Szyprowski | f6fe282 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 1056 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask) |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1057 | { |
| 1058 | if (!dev->dma_mask || !dma_supported(dev, dma_mask)) |
| 1059 | return -EIO; |
| 1060 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1061 | *dev->dma_mask = dma_mask; |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1062 | |
| 1063 | return 0; |
| 1064 | } |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 1065 | |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 1066 | #define PREALLOC_DMA_DEBUG_ENTRIES 4096 |
| 1067 | |
| 1068 | static int __init dma_debug_do_init(void) |
| 1069 | { |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 1070 | #ifdef CONFIG_MMU |
| 1071 | arm_vmregion_create_proc("dma-mappings", &consistent_head); |
| 1072 | #endif |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 1073 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | fs_initcall(dma_debug_do_init); |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1077 | |
| 1078 | #ifdef CONFIG_ARM_DMA_USE_IOMMU |
| 1079 | |
| 1080 | /* IOMMU */ |
| 1081 | |
| 1082 | static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping, |
| 1083 | size_t size) |
| 1084 | { |
| 1085 | unsigned int order = get_order(size); |
| 1086 | unsigned int align = 0; |
| 1087 | unsigned int count, start; |
| 1088 | unsigned long flags; |
| 1089 | |
| 1090 | count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) + |
| 1091 | (1 << mapping->order) - 1) >> mapping->order; |
| 1092 | |
| 1093 | if (order > mapping->order) |
| 1094 | align = (1 << (order - mapping->order)) - 1; |
| 1095 | |
| 1096 | spin_lock_irqsave(&mapping->lock, flags); |
| 1097 | start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0, |
| 1098 | count, align); |
| 1099 | if (start > mapping->bits) { |
| 1100 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1101 | return DMA_ERROR_CODE; |
| 1102 | } |
| 1103 | |
| 1104 | bitmap_set(mapping->bitmap, start, count); |
| 1105 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1106 | |
| 1107 | return mapping->base + (start << (mapping->order + PAGE_SHIFT)); |
| 1108 | } |
| 1109 | |
| 1110 | static inline void __free_iova(struct dma_iommu_mapping *mapping, |
| 1111 | dma_addr_t addr, size_t size) |
| 1112 | { |
| 1113 | unsigned int start = (addr - mapping->base) >> |
| 1114 | (mapping->order + PAGE_SHIFT); |
| 1115 | unsigned int count = ((size >> PAGE_SHIFT) + |
| 1116 | (1 << mapping->order) - 1) >> mapping->order; |
| 1117 | unsigned long flags; |
| 1118 | |
| 1119 | spin_lock_irqsave(&mapping->lock, flags); |
| 1120 | bitmap_clear(mapping->bitmap, start, count); |
| 1121 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 1122 | } |
| 1123 | |
| 1124 | static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) |
| 1125 | { |
| 1126 | struct page **pages; |
| 1127 | int count = size >> PAGE_SHIFT; |
| 1128 | int array_size = count * sizeof(struct page *); |
| 1129 | int i = 0; |
| 1130 | |
| 1131 | if (array_size <= PAGE_SIZE) |
| 1132 | pages = kzalloc(array_size, gfp); |
| 1133 | else |
| 1134 | pages = vzalloc(array_size); |
| 1135 | if (!pages) |
| 1136 | return NULL; |
| 1137 | |
| 1138 | while (count) { |
Marek Szyprowski | b1fc902 | 2012-06-21 11:48:11 +0200 | [diff] [blame] | 1139 | int j, order = __fls(count); |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1140 | |
| 1141 | pages[i] = alloc_pages(gfp | __GFP_NOWARN, order); |
| 1142 | while (!pages[i] && order) |
| 1143 | pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order); |
| 1144 | if (!pages[i]) |
| 1145 | goto error; |
| 1146 | |
| 1147 | if (order) |
| 1148 | split_page(pages[i], order); |
| 1149 | j = 1 << order; |
| 1150 | while (--j) |
| 1151 | pages[i + j] = pages[i] + j; |
| 1152 | |
| 1153 | __dma_clear_buffer(pages[i], PAGE_SIZE << order); |
| 1154 | i += 1 << order; |
| 1155 | count -= 1 << order; |
| 1156 | } |
| 1157 | |
| 1158 | return pages; |
| 1159 | error: |
| 1160 | while (--i) |
| 1161 | if (pages[i]) |
| 1162 | __free_pages(pages[i], 0); |
| 1163 | if (array_size < PAGE_SIZE) |
| 1164 | kfree(pages); |
| 1165 | else |
| 1166 | vfree(pages); |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | |
| 1170 | static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size) |
| 1171 | { |
| 1172 | int count = size >> PAGE_SHIFT; |
| 1173 | int array_size = count * sizeof(struct page *); |
| 1174 | int i; |
| 1175 | for (i = 0; i < count; i++) |
| 1176 | if (pages[i]) |
| 1177 | __free_pages(pages[i], 0); |
| 1178 | if (array_size < PAGE_SIZE) |
| 1179 | kfree(pages); |
| 1180 | else |
| 1181 | vfree(pages); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Create a CPU mapping for a specified pages |
| 1187 | */ |
| 1188 | static void * |
| 1189 | __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot) |
| 1190 | { |
| 1191 | struct arm_vmregion *c; |
| 1192 | size_t align; |
| 1193 | size_t count = size >> PAGE_SHIFT; |
| 1194 | int bit; |
| 1195 | |
| 1196 | if (!consistent_pte[0]) { |
| 1197 | pr_err("%s: not initialised\n", __func__); |
| 1198 | dump_stack(); |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Align the virtual region allocation - maximum alignment is |
| 1204 | * a section size, minimum is a page size. This helps reduce |
| 1205 | * fragmentation of the DMA space, and also prevents allocations |
| 1206 | * smaller than a section from crossing a section boundary. |
| 1207 | */ |
| 1208 | bit = fls(size - 1); |
| 1209 | if (bit > SECTION_SHIFT) |
| 1210 | bit = SECTION_SHIFT; |
| 1211 | align = 1 << bit; |
| 1212 | |
| 1213 | /* |
| 1214 | * Allocate a virtual address in the consistent mapping region. |
| 1215 | */ |
| 1216 | c = arm_vmregion_alloc(&consistent_head, align, size, |
| 1217 | gfp & ~(__GFP_DMA | __GFP_HIGHMEM), NULL); |
| 1218 | if (c) { |
| 1219 | pte_t *pte; |
| 1220 | int idx = CONSISTENT_PTE_INDEX(c->vm_start); |
| 1221 | int i = 0; |
| 1222 | u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); |
| 1223 | |
| 1224 | pte = consistent_pte[idx] + off; |
| 1225 | c->priv = pages; |
| 1226 | |
| 1227 | do { |
| 1228 | BUG_ON(!pte_none(*pte)); |
| 1229 | |
| 1230 | set_pte_ext(pte, mk_pte(pages[i], prot), 0); |
| 1231 | pte++; |
| 1232 | off++; |
| 1233 | i++; |
| 1234 | if (off >= PTRS_PER_PTE) { |
| 1235 | off = 0; |
| 1236 | pte = consistent_pte[++idx]; |
| 1237 | } |
| 1238 | } while (i < count); |
| 1239 | |
| 1240 | dsb(); |
| 1241 | |
| 1242 | return (void *)c->vm_start; |
| 1243 | } |
| 1244 | return NULL; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Create a mapping in device IO address space for specified pages |
| 1249 | */ |
| 1250 | static dma_addr_t |
| 1251 | __iommu_create_mapping(struct device *dev, struct page **pages, size_t size) |
| 1252 | { |
| 1253 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1254 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 1255 | dma_addr_t dma_addr, iova; |
| 1256 | int i, ret = DMA_ERROR_CODE; |
| 1257 | |
| 1258 | dma_addr = __alloc_iova(mapping, size); |
| 1259 | if (dma_addr == DMA_ERROR_CODE) |
| 1260 | return dma_addr; |
| 1261 | |
| 1262 | iova = dma_addr; |
| 1263 | for (i = 0; i < count; ) { |
| 1264 | unsigned int next_pfn = page_to_pfn(pages[i]) + 1; |
| 1265 | phys_addr_t phys = page_to_phys(pages[i]); |
| 1266 | unsigned int len, j; |
| 1267 | |
| 1268 | for (j = i + 1; j < count; j++, next_pfn++) |
| 1269 | if (page_to_pfn(pages[j]) != next_pfn) |
| 1270 | break; |
| 1271 | |
| 1272 | len = (j - i) << PAGE_SHIFT; |
| 1273 | ret = iommu_map(mapping->domain, iova, phys, len, 0); |
| 1274 | if (ret < 0) |
| 1275 | goto fail; |
| 1276 | iova += len; |
| 1277 | i = j; |
| 1278 | } |
| 1279 | return dma_addr; |
| 1280 | fail: |
| 1281 | iommu_unmap(mapping->domain, dma_addr, iova-dma_addr); |
| 1282 | __free_iova(mapping, dma_addr, size); |
| 1283 | return DMA_ERROR_CODE; |
| 1284 | } |
| 1285 | |
| 1286 | static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size) |
| 1287 | { |
| 1288 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1289 | |
| 1290 | /* |
| 1291 | * add optional in-page offset from iova to size and align |
| 1292 | * result to page size |
| 1293 | */ |
| 1294 | size = PAGE_ALIGN((iova & ~PAGE_MASK) + size); |
| 1295 | iova &= PAGE_MASK; |
| 1296 | |
| 1297 | iommu_unmap(mapping->domain, iova, size); |
| 1298 | __free_iova(mapping, iova, size); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static void *arm_iommu_alloc_attrs(struct device *dev, size_t size, |
| 1303 | dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs) |
| 1304 | { |
| 1305 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); |
| 1306 | struct page **pages; |
| 1307 | void *addr = NULL; |
| 1308 | |
| 1309 | *handle = DMA_ERROR_CODE; |
| 1310 | size = PAGE_ALIGN(size); |
| 1311 | |
| 1312 | pages = __iommu_alloc_buffer(dev, size, gfp); |
| 1313 | if (!pages) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | *handle = __iommu_create_mapping(dev, pages, size); |
| 1317 | if (*handle == DMA_ERROR_CODE) |
| 1318 | goto err_buffer; |
| 1319 | |
| 1320 | addr = __iommu_alloc_remap(pages, size, gfp, prot); |
| 1321 | if (!addr) |
| 1322 | goto err_mapping; |
| 1323 | |
| 1324 | return addr; |
| 1325 | |
| 1326 | err_mapping: |
| 1327 | __iommu_remove_mapping(dev, *handle, size); |
| 1328 | err_buffer: |
| 1329 | __iommu_free_buffer(dev, pages, size); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
| 1333 | static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, |
| 1334 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 1335 | struct dma_attrs *attrs) |
| 1336 | { |
| 1337 | struct arm_vmregion *c; |
| 1338 | |
| 1339 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); |
| 1340 | c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); |
| 1341 | |
| 1342 | if (c) { |
| 1343 | struct page **pages = c->priv; |
| 1344 | |
| 1345 | unsigned long uaddr = vma->vm_start; |
| 1346 | unsigned long usize = vma->vm_end - vma->vm_start; |
| 1347 | int i = 0; |
| 1348 | |
| 1349 | do { |
| 1350 | int ret; |
| 1351 | |
| 1352 | ret = vm_insert_page(vma, uaddr, pages[i++]); |
| 1353 | if (ret) { |
| 1354 | pr_err("Remapping memory, error: %d\n", ret); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
| 1358 | uaddr += PAGE_SIZE; |
| 1359 | usize -= PAGE_SIZE; |
| 1360 | } while (usize > 0); |
| 1361 | } |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * free a page as defined by the above mapping. |
| 1367 | * Must not be called with IRQs disabled. |
| 1368 | */ |
| 1369 | void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, |
| 1370 | dma_addr_t handle, struct dma_attrs *attrs) |
| 1371 | { |
| 1372 | struct arm_vmregion *c; |
| 1373 | size = PAGE_ALIGN(size); |
| 1374 | |
| 1375 | c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); |
| 1376 | if (c) { |
| 1377 | struct page **pages = c->priv; |
Laura Abbott | 5f06ec8 | 2013-01-16 18:23:19 -0800 | [diff] [blame] | 1378 | __dma_free_remap(cpu_addr, size, false); |
Marek Szyprowski | bebe3ac | 2012-05-16 15:48:21 +0200 | [diff] [blame] | 1379 | __iommu_remove_mapping(dev, handle, size); |
| 1380 | __iommu_free_buffer(dev, pages, size); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Map a part of the scatter-gather list into contiguous io address space |
| 1386 | */ |
| 1387 | static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, |
| 1388 | size_t size, dma_addr_t *handle, |
| 1389 | enum dma_data_direction dir) |
| 1390 | { |
| 1391 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1392 | dma_addr_t iova, iova_base; |
| 1393 | int ret = 0; |
| 1394 | unsigned int count; |
| 1395 | struct scatterlist *s; |
| 1396 | |
| 1397 | size = PAGE_ALIGN(size); |
| 1398 | *handle = DMA_ERROR_CODE; |
| 1399 | |
| 1400 | iova_base = iova = __alloc_iova(mapping, size); |
| 1401 | if (iova == DMA_ERROR_CODE) |
| 1402 | return -ENOMEM; |
| 1403 | |
| 1404 | for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) { |
| 1405 | phys_addr_t phys = page_to_phys(sg_page(s)); |
| 1406 | unsigned int len = PAGE_ALIGN(s->offset + s->length); |
| 1407 | |
| 1408 | if (!arch_is_coherent()) |
| 1409 | __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); |
| 1410 | |
| 1411 | ret = iommu_map(mapping->domain, iova, phys, len, 0); |
| 1412 | if (ret < 0) |
| 1413 | goto fail; |
| 1414 | count += len >> PAGE_SHIFT; |
| 1415 | iova += len; |
| 1416 | } |
| 1417 | *handle = iova_base; |
| 1418 | |
| 1419 | return 0; |
| 1420 | fail: |
| 1421 | iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); |
| 1422 | __free_iova(mapping, iova_base, size); |
| 1423 | return ret; |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA |
| 1428 | * @dev: valid struct device pointer |
| 1429 | * @sg: list of buffers |
| 1430 | * @nents: number of buffers to map |
| 1431 | * @dir: DMA transfer direction |
| 1432 | * |
| 1433 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 1434 | * The scatter gather list elements are merged together (if possible) and |
| 1435 | * tagged with the appropriate dma address and length. They are obtained via |
| 1436 | * sg_dma_{address,length}. |
| 1437 | */ |
| 1438 | int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 1439 | enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1440 | { |
| 1441 | struct scatterlist *s = sg, *dma = sg, *start = sg; |
| 1442 | int i, count = 0; |
| 1443 | unsigned int offset = s->offset; |
| 1444 | unsigned int size = s->offset + s->length; |
| 1445 | unsigned int max = dma_get_max_seg_size(dev); |
| 1446 | |
| 1447 | for (i = 1; i < nents; i++) { |
| 1448 | s = sg_next(s); |
| 1449 | |
| 1450 | s->dma_address = DMA_ERROR_CODE; |
| 1451 | s->dma_length = 0; |
| 1452 | |
| 1453 | if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) { |
| 1454 | if (__map_sg_chunk(dev, start, size, &dma->dma_address, |
| 1455 | dir) < 0) |
| 1456 | goto bad_mapping; |
| 1457 | |
| 1458 | dma->dma_address += offset; |
| 1459 | dma->dma_length = size - offset; |
| 1460 | |
| 1461 | size = offset = s->offset; |
| 1462 | start = s; |
| 1463 | dma = sg_next(dma); |
| 1464 | count += 1; |
| 1465 | } |
| 1466 | size += s->length; |
| 1467 | } |
| 1468 | if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir) < 0) |
| 1469 | goto bad_mapping; |
| 1470 | |
| 1471 | dma->dma_address += offset; |
| 1472 | dma->dma_length = size - offset; |
| 1473 | |
| 1474 | return count+1; |
| 1475 | |
| 1476 | bad_mapping: |
| 1477 | for_each_sg(sg, s, count, i) |
| 1478 | __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s)); |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg |
| 1484 | * @dev: valid struct device pointer |
| 1485 | * @sg: list of buffers |
| 1486 | * @nents: number of buffers to unmap (same as was passed to dma_map_sg) |
| 1487 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1488 | * |
| 1489 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 1490 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 1491 | */ |
| 1492 | void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 1493 | enum dma_data_direction dir, struct dma_attrs *attrs) |
| 1494 | { |
| 1495 | struct scatterlist *s; |
| 1496 | int i; |
| 1497 | |
| 1498 | for_each_sg(sg, s, nents, i) { |
| 1499 | if (sg_dma_len(s)) |
| 1500 | __iommu_remove_mapping(dev, sg_dma_address(s), |
| 1501 | sg_dma_len(s)); |
| 1502 | if (!arch_is_coherent()) |
| 1503 | __dma_page_dev_to_cpu(sg_page(s), s->offset, |
| 1504 | s->length, dir); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * arm_iommu_sync_sg_for_cpu |
| 1510 | * @dev: valid struct device pointer |
| 1511 | * @sg: list of buffers |
| 1512 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1513 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1514 | */ |
| 1515 | void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 1516 | int nents, enum dma_data_direction dir) |
| 1517 | { |
| 1518 | struct scatterlist *s; |
| 1519 | int i; |
| 1520 | |
| 1521 | for_each_sg(sg, s, nents, i) |
| 1522 | if (!arch_is_coherent()) |
| 1523 | __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir); |
| 1524 | |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * arm_iommu_sync_sg_for_device |
| 1529 | * @dev: valid struct device pointer |
| 1530 | * @sg: list of buffers |
| 1531 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 1532 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 1533 | */ |
| 1534 | void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 1535 | int nents, enum dma_data_direction dir) |
| 1536 | { |
| 1537 | struct scatterlist *s; |
| 1538 | int i; |
| 1539 | |
| 1540 | for_each_sg(sg, s, nents, i) |
| 1541 | if (!arch_is_coherent()) |
| 1542 | __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | /** |
| 1547 | * arm_iommu_map_page |
| 1548 | * @dev: valid struct device pointer |
| 1549 | * @page: page that buffer resides in |
| 1550 | * @offset: offset into page for start of buffer |
| 1551 | * @size: size of buffer to map |
| 1552 | * @dir: DMA transfer direction |
| 1553 | * |
| 1554 | * IOMMU aware version of arm_dma_map_page() |
| 1555 | */ |
| 1556 | static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page, |
| 1557 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 1558 | struct dma_attrs *attrs) |
| 1559 | { |
| 1560 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1561 | dma_addr_t dma_addr; |
| 1562 | int ret, len = PAGE_ALIGN(size + offset); |
| 1563 | |
| 1564 | if (!arch_is_coherent()) |
| 1565 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 1566 | |
| 1567 | dma_addr = __alloc_iova(mapping, len); |
| 1568 | if (dma_addr == DMA_ERROR_CODE) |
| 1569 | return dma_addr; |
| 1570 | |
| 1571 | ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0); |
| 1572 | if (ret < 0) |
| 1573 | goto fail; |
| 1574 | |
| 1575 | return dma_addr + offset; |
| 1576 | fail: |
| 1577 | __free_iova(mapping, dma_addr, len); |
| 1578 | return DMA_ERROR_CODE; |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * arm_iommu_unmap_page |
| 1583 | * @dev: valid struct device pointer |
| 1584 | * @handle: DMA address of buffer |
| 1585 | * @size: size of buffer (same as passed to dma_map_page) |
| 1586 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 1587 | * |
| 1588 | * IOMMU aware version of arm_dma_unmap_page() |
| 1589 | */ |
| 1590 | static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle, |
| 1591 | size_t size, enum dma_data_direction dir, |
| 1592 | struct dma_attrs *attrs) |
| 1593 | { |
| 1594 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1595 | dma_addr_t iova = handle & PAGE_MASK; |
| 1596 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1597 | int offset = handle & ~PAGE_MASK; |
| 1598 | int len = PAGE_ALIGN(size + offset); |
| 1599 | |
| 1600 | if (!iova) |
| 1601 | return; |
| 1602 | |
| 1603 | if (!arch_is_coherent()) |
| 1604 | __dma_page_dev_to_cpu(page, offset, size, dir); |
| 1605 | |
| 1606 | iommu_unmap(mapping->domain, iova, len); |
| 1607 | __free_iova(mapping, iova, len); |
| 1608 | } |
| 1609 | |
| 1610 | static void arm_iommu_sync_single_for_cpu(struct device *dev, |
| 1611 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 1612 | { |
| 1613 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1614 | dma_addr_t iova = handle & PAGE_MASK; |
| 1615 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1616 | unsigned int offset = handle & ~PAGE_MASK; |
| 1617 | |
| 1618 | if (!iova) |
| 1619 | return; |
| 1620 | |
| 1621 | if (!arch_is_coherent()) |
| 1622 | __dma_page_dev_to_cpu(page, offset, size, dir); |
| 1623 | } |
| 1624 | |
| 1625 | static void arm_iommu_sync_single_for_device(struct device *dev, |
| 1626 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 1627 | { |
| 1628 | struct dma_iommu_mapping *mapping = dev->archdata.mapping; |
| 1629 | dma_addr_t iova = handle & PAGE_MASK; |
| 1630 | struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); |
| 1631 | unsigned int offset = handle & ~PAGE_MASK; |
| 1632 | |
| 1633 | if (!iova) |
| 1634 | return; |
| 1635 | |
| 1636 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 1637 | } |
| 1638 | |
| 1639 | struct dma_map_ops iommu_ops = { |
| 1640 | .alloc = arm_iommu_alloc_attrs, |
| 1641 | .free = arm_iommu_free_attrs, |
| 1642 | .mmap = arm_iommu_mmap_attrs, |
| 1643 | |
| 1644 | .map_page = arm_iommu_map_page, |
| 1645 | .unmap_page = arm_iommu_unmap_page, |
| 1646 | .sync_single_for_cpu = arm_iommu_sync_single_for_cpu, |
| 1647 | .sync_single_for_device = arm_iommu_sync_single_for_device, |
| 1648 | |
| 1649 | .map_sg = arm_iommu_map_sg, |
| 1650 | .unmap_sg = arm_iommu_unmap_sg, |
| 1651 | .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu, |
| 1652 | .sync_sg_for_device = arm_iommu_sync_sg_for_device, |
| 1653 | }; |
| 1654 | |
| 1655 | /** |
| 1656 | * arm_iommu_create_mapping |
| 1657 | * @bus: pointer to the bus holding the client device (for IOMMU calls) |
| 1658 | * @base: start address of the valid IO address space |
| 1659 | * @size: size of the valid IO address space |
| 1660 | * @order: accuracy of the IO addresses allocations |
| 1661 | * |
| 1662 | * Creates a mapping structure which holds information about used/unused |
| 1663 | * IO address ranges, which is required to perform memory allocation and |
| 1664 | * mapping with IOMMU aware functions. |
| 1665 | * |
| 1666 | * The client device need to be attached to the mapping with |
| 1667 | * arm_iommu_attach_device function. |
| 1668 | */ |
| 1669 | struct dma_iommu_mapping * |
| 1670 | arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, |
| 1671 | int order) |
| 1672 | { |
| 1673 | unsigned int count = size >> (PAGE_SHIFT + order); |
| 1674 | unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long); |
| 1675 | struct dma_iommu_mapping *mapping; |
| 1676 | int err = -ENOMEM; |
| 1677 | |
| 1678 | if (!count) |
| 1679 | return ERR_PTR(-EINVAL); |
| 1680 | |
| 1681 | mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL); |
| 1682 | if (!mapping) |
| 1683 | goto err; |
| 1684 | |
| 1685 | mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL); |
| 1686 | if (!mapping->bitmap) |
| 1687 | goto err2; |
| 1688 | |
| 1689 | mapping->base = base; |
| 1690 | mapping->bits = BITS_PER_BYTE * bitmap_size; |
| 1691 | mapping->order = order; |
| 1692 | spin_lock_init(&mapping->lock); |
| 1693 | |
| 1694 | mapping->domain = iommu_domain_alloc(bus); |
| 1695 | if (!mapping->domain) |
| 1696 | goto err3; |
| 1697 | |
| 1698 | kref_init(&mapping->kref); |
| 1699 | return mapping; |
| 1700 | err3: |
| 1701 | kfree(mapping->bitmap); |
| 1702 | err2: |
| 1703 | kfree(mapping); |
| 1704 | err: |
| 1705 | return ERR_PTR(err); |
| 1706 | } |
| 1707 | |
| 1708 | static void release_iommu_mapping(struct kref *kref) |
| 1709 | { |
| 1710 | struct dma_iommu_mapping *mapping = |
| 1711 | container_of(kref, struct dma_iommu_mapping, kref); |
| 1712 | |
| 1713 | iommu_domain_free(mapping->domain); |
| 1714 | kfree(mapping->bitmap); |
| 1715 | kfree(mapping); |
| 1716 | } |
| 1717 | |
| 1718 | void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping) |
| 1719 | { |
| 1720 | if (mapping) |
| 1721 | kref_put(&mapping->kref, release_iommu_mapping); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * arm_iommu_attach_device |
| 1726 | * @dev: valid struct device pointer |
| 1727 | * @mapping: io address space mapping structure (returned from |
| 1728 | * arm_iommu_create_mapping) |
| 1729 | * |
| 1730 | * Attaches specified io address space mapping to the provided device, |
| 1731 | * this replaces the dma operations (dma_map_ops pointer) with the |
| 1732 | * IOMMU aware version. More than one client might be attached to |
| 1733 | * the same io address space mapping. |
| 1734 | */ |
| 1735 | int arm_iommu_attach_device(struct device *dev, |
| 1736 | struct dma_iommu_mapping *mapping) |
| 1737 | { |
| 1738 | int err; |
| 1739 | |
| 1740 | err = iommu_attach_device(mapping->domain, dev); |
| 1741 | if (err) |
| 1742 | return err; |
| 1743 | |
| 1744 | kref_get(&mapping->kref); |
| 1745 | dev->archdata.mapping = mapping; |
| 1746 | set_dma_ops(dev, &iommu_ops); |
| 1747 | |
| 1748 | pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev)); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | #endif |