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