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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Lennert Buytenhek | 23759dc | 2006-04-02 00:07:39 +0100 | [diff] [blame] | 23 | #include <asm/memory.h> |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 24 | #include <asm/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/tlbflush.h> |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 27 | #include <asm/sizes.h> |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 28 | #include <asm/mach/arch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 30 | #include "mm.h" |
| 31 | |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 32 | /* |
| 33 | * The DMA API is built upon the notion of "buffer ownership". A buffer |
| 34 | * is either exclusively owned by the CPU (and therefore may be accessed |
| 35 | * by it) or exclusively owned by the DMA device. These helper functions |
| 36 | * represent the transitions between these two ownership states. |
| 37 | * |
| 38 | * Note, however, that on later ARMs, this notion does not work due to |
| 39 | * speculative prefetches. We model our approach on the assumption that |
| 40 | * the CPU does do speculative prefetches, which means we clean caches |
| 41 | * before transfers and delay cache invalidation until transfer completion. |
| 42 | * |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 43 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 44 | static void __dma_page_cpu_to_dev(struct page *, unsigned long, |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 45 | size_t, enum dma_data_direction); |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 46 | static void __dma_page_dev_to_cpu(struct page *, unsigned long, |
Marek Szyprowski | 15237e1 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 47 | size_t, enum dma_data_direction); |
| 48 | |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 49 | /** |
| 50 | * arm_dma_map_page - map a portion of a page for streaming DMA |
| 51 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 52 | * @page: page that buffer resides in |
| 53 | * @offset: offset into page for start of buffer |
| 54 | * @size: size of buffer to map |
| 55 | * @dir: DMA transfer direction |
| 56 | * |
| 57 | * Ensure that any data held in the cache is appropriately discarded |
| 58 | * or written back. |
| 59 | * |
| 60 | * The device owns this memory once this call has completed. The CPU |
| 61 | * can regain ownership by calling dma_unmap_page(). |
| 62 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 63 | 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] | 64 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 65 | struct dma_attrs *attrs) |
| 66 | { |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 67 | if (!arch_is_coherent()) |
| 68 | __dma_page_cpu_to_dev(page, offset, size, dir); |
| 69 | return pfn_to_dma(dev, page_to_pfn(page)) + offset; |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page() |
| 74 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 75 | * @handle: DMA address of buffer |
| 76 | * @size: size of buffer (same as passed to dma_map_page) |
| 77 | * @dir: DMA transfer direction (same as passed to dma_map_page) |
| 78 | * |
| 79 | * Unmap a page streaming mode DMA translation. The handle and size |
| 80 | * must match what was provided in the previous dma_map_page() call. |
| 81 | * All other usages are undefined. |
| 82 | * |
| 83 | * After this call, reads by the CPU to the buffer are guaranteed to see |
| 84 | * whatever the device wrote there. |
| 85 | */ |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 86 | 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] | 87 | size_t size, enum dma_data_direction dir, |
| 88 | struct dma_attrs *attrs) |
| 89 | { |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 90 | if (!arch_is_coherent()) |
| 91 | __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)), |
| 92 | handle & ~PAGE_MASK, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 95 | static void arm_dma_sync_single_for_cpu(struct device *dev, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 96 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 97 | { |
| 98 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 99 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 100 | if (!arch_is_coherent()) |
| 101 | __dma_page_dev_to_cpu(page, offset, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 104 | static void arm_dma_sync_single_for_device(struct device *dev, |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 105 | dma_addr_t handle, size_t size, enum dma_data_direction dir) |
| 106 | { |
| 107 | unsigned int offset = handle & (PAGE_SIZE - 1); |
| 108 | struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset)); |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 109 | if (!arch_is_coherent()) |
| 110 | __dma_page_cpu_to_dev(page, offset, size, dir); |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask); |
| 114 | |
| 115 | struct dma_map_ops arm_dma_ops = { |
| 116 | .map_page = arm_dma_map_page, |
| 117 | .unmap_page = arm_dma_unmap_page, |
| 118 | .map_sg = arm_dma_map_sg, |
| 119 | .unmap_sg = arm_dma_unmap_sg, |
| 120 | .sync_single_for_cpu = arm_dma_sync_single_for_cpu, |
| 121 | .sync_single_for_device = arm_dma_sync_single_for_device, |
| 122 | .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu, |
| 123 | .sync_sg_for_device = arm_dma_sync_sg_for_device, |
| 124 | .set_dma_mask = arm_dma_set_mask, |
| 125 | }; |
| 126 | EXPORT_SYMBOL(arm_dma_ops); |
| 127 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 128 | static u64 get_coherent_dma_mask(struct device *dev) |
| 129 | { |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 130 | u64 mask = (u64)arm_dma_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 132 | if (dev) { |
| 133 | mask = dev->coherent_dma_mask; |
| 134 | |
| 135 | /* |
| 136 | * Sanity check the DMA mask - it must be non-zero, and |
| 137 | * must be able to be satisfied by a DMA allocation. |
| 138 | */ |
| 139 | if (mask == 0) { |
| 140 | dev_warn(dev, "coherent DMA mask is unset\n"); |
| 141 | return 0; |
| 142 | } |
| 143 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 144 | if ((~mask) & (u64)arm_dma_limit) { |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 145 | dev_warn(dev, "coherent DMA mask %#llx is smaller " |
| 146 | "than system GFP_DMA mask %#llx\n", |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 147 | mask, (u64)arm_dma_limit); |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return mask; |
| 153 | } |
| 154 | |
Russell King | 7a9a32a | 2009-11-19 15:31:07 +0000 | [diff] [blame] | 155 | /* |
| 156 | * Allocate a DMA buffer for 'dev' of size 'size' using the |
| 157 | * specified gfp mask. Note that 'size' must be page aligned. |
| 158 | */ |
| 159 | static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) |
| 160 | { |
| 161 | unsigned long order = get_order(size); |
| 162 | struct page *page, *p, *e; |
| 163 | void *ptr; |
| 164 | u64 mask = get_coherent_dma_mask(dev); |
| 165 | |
| 166 | #ifdef CONFIG_DMA_API_DEBUG |
| 167 | u64 limit = (mask + 1) & ~mask; |
| 168 | if (limit && size >= limit) { |
| 169 | dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n", |
| 170 | size, mask); |
| 171 | return NULL; |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | if (!mask) |
| 176 | return NULL; |
| 177 | |
| 178 | if (mask < 0xffffffffULL) |
| 179 | gfp |= GFP_DMA; |
| 180 | |
| 181 | page = alloc_pages(gfp, order); |
| 182 | if (!page) |
| 183 | return NULL; |
| 184 | |
| 185 | /* |
| 186 | * Now split the huge page and free the excess pages |
| 187 | */ |
| 188 | split_page(page, order); |
| 189 | for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) |
| 190 | __free_page(p); |
| 191 | |
| 192 | /* |
| 193 | * Ensure that the allocated pages are zeroed, and that any data |
| 194 | * lurking in the kernel direct-mapped region is invalidated. |
| 195 | */ |
| 196 | ptr = page_address(page); |
| 197 | memset(ptr, 0, size); |
| 198 | dmac_flush_range(ptr, ptr + size); |
| 199 | outer_flush_range(__pa(ptr), __pa(ptr) + size); |
| 200 | |
| 201 | return page; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Free a DMA buffer. 'size' must be page aligned. |
| 206 | */ |
| 207 | static void __dma_free_buffer(struct page *page, size_t size) |
| 208 | { |
| 209 | struct page *e = page + (size >> PAGE_SHIFT); |
| 210 | |
| 211 | while (page < e) { |
| 212 | __free_page(page); |
| 213 | page++; |
| 214 | } |
| 215 | } |
| 216 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 217 | #ifdef CONFIG_MMU |
Catalin Marinas | a5e9d38 | 2010-06-21 15:09:06 +0100 | [diff] [blame] | 218 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 219 | #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - consistent_base) >> PAGE_SHIFT) |
Linus Torvalds | 1fdb24e | 2011-10-28 12:02:27 -0700 | [diff] [blame] | 220 | #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - consistent_base) >> PMD_SHIFT) |
Catalin Marinas | a5e9d38 | 2010-06-21 15:09:06 +0100 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 223 | * These are the page tables (2MB each) covering uncached, DMA consistent allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | */ |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 225 | static pte_t **consistent_pte; |
| 226 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 227 | #define DEFAULT_CONSISTENT_DMA_SIZE SZ_2M |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 228 | |
| 229 | unsigned long consistent_base = CONSISTENT_END - DEFAULT_CONSISTENT_DMA_SIZE; |
| 230 | |
| 231 | void __init init_consistent_dma_size(unsigned long size) |
| 232 | { |
| 233 | unsigned long base = CONSISTENT_END - ALIGN(size, SZ_2M); |
| 234 | |
| 235 | BUG_ON(consistent_pte); /* Check we're called before DMA region init */ |
| 236 | BUG_ON(base < VMALLOC_END); |
| 237 | |
| 238 | /* Grow region to accommodate specified size */ |
| 239 | if (base < consistent_base) |
| 240 | consistent_base = base; |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 243 | #include "vmregion.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 245 | static struct arm_vmregion_head consistent_head = { |
| 246 | .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | .vm_list = LIST_HEAD_INIT(consistent_head.vm_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | .vm_end = CONSISTENT_END, |
| 249 | }; |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | #ifdef CONFIG_HUGETLB_PAGE |
| 252 | #error ARM Coherent DMA allocator does not (yet) support huge TLB |
| 253 | #endif |
| 254 | |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Initialise the consistent memory allocation. |
| 257 | */ |
| 258 | static int __init consistent_init(void) |
| 259 | { |
| 260 | int ret = 0; |
| 261 | pgd_t *pgd; |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 262 | pud_t *pud; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 263 | pmd_t *pmd; |
| 264 | pte_t *pte; |
| 265 | int i = 0; |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 266 | unsigned long base = consistent_base; |
Catalin Marinas | 53cbcbc | 2011-11-17 13:11:21 +0100 | [diff] [blame] | 267 | unsigned long num_ptes = (CONSISTENT_END - base) >> PMD_SHIFT; |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 268 | |
| 269 | consistent_pte = kmalloc(num_ptes * sizeof(pte_t), GFP_KERNEL); |
| 270 | if (!consistent_pte) { |
| 271 | pr_err("%s: no memory\n", __func__); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | |
| 275 | pr_debug("DMA memory: 0x%08lx - 0x%08lx:\n", base, CONSISTENT_END); |
| 276 | consistent_head.vm_start = base; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 277 | |
| 278 | do { |
| 279 | pgd = pgd_offset(&init_mm, base); |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 280 | |
| 281 | pud = pud_alloc(&init_mm, pgd, base); |
| 282 | if (!pud) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 283 | pr_err("%s: no pud tables\n", __func__); |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 284 | ret = -ENOMEM; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | pmd = pmd_alloc(&init_mm, pud, base); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 289 | if (!pmd) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 290 | pr_err("%s: no pmd tables\n", __func__); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 291 | ret = -ENOMEM; |
| 292 | break; |
| 293 | } |
| 294 | WARN_ON(!pmd_none(*pmd)); |
| 295 | |
| 296 | pte = pte_alloc_kernel(pmd, base); |
| 297 | if (!pte) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 298 | pr_err("%s: no pte tables\n", __func__); |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 299 | ret = -ENOMEM; |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | consistent_pte[i++] = pte; |
Catalin Marinas | e73fc88 | 2011-08-23 14:07:23 +0100 | [diff] [blame] | 304 | base += PMD_SIZE; |
Russell King | 88c58f3 | 2009-11-19 16:46:02 +0000 | [diff] [blame] | 305 | } while (base < CONSISTENT_END); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | core_initcall(consistent_init); |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | static void * |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 313 | __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot, |
| 314 | const void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 316 | struct arm_vmregion *c; |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 317 | size_t align; |
| 318 | int bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Jon Medhurst | 99d1717 | 2011-08-02 17:28:27 +0100 | [diff] [blame] | 320 | if (!consistent_pte) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 321 | pr_err("%s: not initialised\n", __func__); |
Russell King | ebd7a84 | 2009-11-19 20:58:31 +0000 | [diff] [blame] | 322 | dump_stack(); |
Russell King | ebd7a84 | 2009-11-19 20:58:31 +0000 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | /* |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 327 | * Align the virtual region allocation - maximum alignment is |
| 328 | * a section size, minimum is a page size. This helps reduce |
| 329 | * fragmentation of the DMA space, and also prevents allocations |
| 330 | * smaller than a section from crossing a section boundary. |
| 331 | */ |
Russell King | c947f69 | 2010-11-03 16:00:15 +0000 | [diff] [blame] | 332 | bit = fls(size - 1); |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 333 | if (bit > SECTION_SHIFT) |
| 334 | bit = SECTION_SHIFT; |
| 335 | align = 1 << bit; |
| 336 | |
| 337 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * Allocate a virtual address in the consistent mapping region. |
| 339 | */ |
Russell King | 5bc23d3 | 2010-07-25 08:57:02 +0100 | [diff] [blame] | 340 | c = arm_vmregion_alloc(&consistent_head, align, size, |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 341 | gfp & ~(__GFP_DMA | __GFP_HIGHMEM), caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (c) { |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 343 | pte_t *pte; |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 344 | int idx = CONSISTENT_PTE_INDEX(c->vm_start); |
| 345 | u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 347 | pte = consistent_pte[idx] + off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | c->vm_pages = page; |
| 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | do { |
| 351 | BUG_ON(!pte_none(*pte)); |
| 352 | |
Russell King | ad1ae2f | 2006-12-13 14:34:43 +0000 | [diff] [blame] | 353 | set_pte_ext(pte, mk_pte(page, prot), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | page++; |
| 355 | pte++; |
Kevin Hilman | 37134cd | 2006-01-12 16:12:21 +0000 | [diff] [blame] | 356 | off++; |
| 357 | if (off >= PTRS_PER_PTE) { |
| 358 | off = 0; |
| 359 | pte = consistent_pte[++idx]; |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } while (size -= PAGE_SIZE); |
| 362 | |
Russell King | 2be23c4 | 2010-09-08 16:27:56 +0100 | [diff] [blame] | 363 | dsb(); |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return (void *)c->vm_start; |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 369 | |
| 370 | static void __dma_free_remap(void *cpu_addr, size_t size) |
| 371 | { |
| 372 | struct arm_vmregion *c; |
| 373 | unsigned long addr; |
| 374 | pte_t *ptep; |
| 375 | int idx; |
| 376 | u32 off; |
| 377 | |
| 378 | c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr); |
| 379 | if (!c) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 380 | pr_err("%s: trying to free invalid coherent area: %p\n", |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 381 | __func__, cpu_addr); |
| 382 | dump_stack(); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | if ((c->vm_end - c->vm_start) != size) { |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 387 | pr_err("%s: freeing wrong coherent size (%ld != %d)\n", |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 388 | __func__, c->vm_end - c->vm_start, size); |
| 389 | dump_stack(); |
| 390 | size = c->vm_end - c->vm_start; |
| 391 | } |
| 392 | |
| 393 | idx = CONSISTENT_PTE_INDEX(c->vm_start); |
| 394 | off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); |
| 395 | ptep = consistent_pte[idx] + off; |
| 396 | addr = c->vm_start; |
| 397 | do { |
| 398 | pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep); |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 399 | |
| 400 | ptep++; |
| 401 | addr += PAGE_SIZE; |
| 402 | off++; |
| 403 | if (off >= PTRS_PER_PTE) { |
| 404 | off = 0; |
| 405 | ptep = consistent_pte[++idx]; |
| 406 | } |
| 407 | |
Russell King | acaac25 | 2009-11-20 18:19:52 +0000 | [diff] [blame] | 408 | if (pte_none(pte) || !pte_present(pte)) |
Marek Szyprowski | 6b6f770 | 2012-02-28 10:19:14 +0100 | [diff] [blame] | 409 | pr_crit("%s: bad page in kernel page table\n", |
| 410 | __func__); |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 411 | } while (size -= PAGE_SIZE); |
| 412 | |
| 413 | flush_tlb_kernel_range(c->vm_start, c->vm_end); |
| 414 | |
| 415 | arm_vmregion_free(&consistent_head, c); |
| 416 | } |
| 417 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 418 | #else /* !CONFIG_MMU */ |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 419 | |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 420 | #define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page) |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 421 | #define __dma_free_remap(addr, size) do { } while (0) |
| 422 | |
| 423 | #endif /* CONFIG_MMU */ |
| 424 | |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 425 | static void * |
| 426 | __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] | 427 | pgprot_t prot, const void *caller) |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 428 | { |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 429 | struct page *page; |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 430 | void *addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 431 | |
Sumit Bhattacharya | ea2e705 | 2011-11-24 00:47:12 +0100 | [diff] [blame] | 432 | /* |
| 433 | * Following is a work-around (a.k.a. hack) to prevent pages |
| 434 | * with __GFP_COMP being passed to split_page() which cannot |
| 435 | * handle them. The real problem is that this flag probably |
| 436 | * should be 0 on ARM as it is not supported on this |
| 437 | * platform; see CONFIG_HUGETLBFS. |
| 438 | */ |
| 439 | gfp &= ~(__GFP_COMP); |
| 440 | |
Marek Szyprowski | 553ac78 | 2012-02-29 14:45:28 +0100 | [diff] [blame] | 441 | *handle = DMA_ERROR_CODE; |
Russell King | 04da569 | 2009-11-19 15:54:45 +0000 | [diff] [blame] | 442 | size = PAGE_ALIGN(size); |
| 443 | |
| 444 | page = __dma_alloc_buffer(dev, size, gfp); |
| 445 | if (!page) |
| 446 | return NULL; |
| 447 | |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 448 | if (!arch_is_coherent()) |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 449 | addr = __dma_alloc_remap(page, size, gfp, prot, caller); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 450 | else |
| 451 | addr = page_address(page); |
| 452 | |
| 453 | if (addr) |
Russell King | 9eedd96 | 2011-01-03 00:00:17 +0000 | [diff] [blame] | 454 | *handle = pfn_to_dma(dev, page_to_pfn(page)); |
Russell King | d8e89b4 | 2011-09-22 10:32:25 +0100 | [diff] [blame] | 455 | else |
| 456 | __dma_free_buffer(page, size); |
Russell King | 31ebf94 | 2009-11-19 21:12:17 +0000 | [diff] [blame] | 457 | |
| 458 | return addr; |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 459 | } |
Russell King | 695ae0a | 2009-11-19 16:31:39 +0000 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | /* |
| 462 | * Allocate DMA-coherent memory space and return both the kernel remapped |
| 463 | * virtual and bus address for that space. |
| 464 | */ |
| 465 | void * |
Al Viro | f9e3214 | 2005-10-21 03:20:58 -0400 | [diff] [blame] | 466 | dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Dmitry Baryshkov | 1fe5326 | 2008-07-18 13:30:14 +0400 | [diff] [blame] | 468 | void *memory; |
| 469 | |
| 470 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) |
| 471 | return memory; |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return __dma_alloc(dev, size, handle, gfp, |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 474 | pgprot_dmacoherent(pgprot_kernel), |
| 475 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 478 | |
| 479 | /* |
| 480 | * Allocate a writecombining region, in much the same way as |
| 481 | * dma_alloc_coherent above. |
| 482 | */ |
| 483 | void * |
Al Viro | f9e3214 | 2005-10-21 03:20:58 -0400 | [diff] [blame] | 484 | dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | return __dma_alloc(dev, size, handle, gfp, |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 487 | pgprot_writecombine(pgprot_kernel), |
| 488 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | EXPORT_SYMBOL(dma_alloc_writecombine); |
| 491 | |
| 492 | static int dma_mmap(struct device *dev, struct vm_area_struct *vma, |
| 493 | void *cpu_addr, dma_addr_t dma_addr, size_t size) |
| 494 | { |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 495 | int ret = -ENXIO; |
| 496 | #ifdef CONFIG_MMU |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 497 | unsigned long user_size, kern_size; |
| 498 | struct arm_vmregion *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Marek Szyprowski | 47142f0 | 2012-05-15 19:04:13 +0200 | [diff] [blame] | 500 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) |
| 501 | return ret; |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 504 | |
Russell King | 13ccf3a | 2009-11-19 15:07:04 +0000 | [diff] [blame] | 505 | c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (c) { |
| 507 | unsigned long off = vma->vm_pgoff; |
| 508 | |
| 509 | kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT; |
| 510 | |
| 511 | if (off < kern_size && |
| 512 | user_size <= (kern_size - off)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | ret = remap_pfn_range(vma, vma->vm_start, |
| 514 | page_to_pfn(c->vm_pages) + off, |
| 515 | user_size << PAGE_SHIFT, |
| 516 | vma->vm_page_prot); |
| 517 | } |
| 518 | } |
Catalin Marinas | ab6494f | 2009-07-24 12:35:02 +0100 | [diff] [blame] | 519 | #endif /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma, |
| 525 | void *cpu_addr, dma_addr_t dma_addr, size_t size) |
| 526 | { |
Russell King | 26a26d3 | 2009-11-20 21:06:43 +0000 | [diff] [blame] | 527 | vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return dma_mmap(dev, vma, cpu_addr, dma_addr, size); |
| 529 | } |
| 530 | EXPORT_SYMBOL(dma_mmap_coherent); |
| 531 | |
| 532 | int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma, |
| 533 | void *cpu_addr, dma_addr_t dma_addr, size_t size) |
| 534 | { |
| 535 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 536 | return dma_mmap(dev, vma, cpu_addr, dma_addr, size); |
| 537 | } |
| 538 | EXPORT_SYMBOL(dma_mmap_writecombine); |
| 539 | |
| 540 | /* |
| 541 | * free a page as defined by the above mapping. |
Russell King | 5edf71a | 2005-11-25 15:52:51 +0000 | [diff] [blame] | 542 | * Must not be called with IRQs disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | */ |
| 544 | void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle) |
| 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 | } |
| 558 | EXPORT_SYMBOL(dma_free_coherent); |
| 559 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 560 | static void dma_cache_maint_page(struct page *page, unsigned long offset, |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 561 | size_t size, enum dma_data_direction dir, |
| 562 | void (*op)(const void *, size_t, int)) |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 563 | { |
| 564 | /* |
| 565 | * A single sg entry may refer to multiple physically contiguous |
| 566 | * pages. But we still need to process highmem pages individually. |
| 567 | * If highmem is not configured then the bulk of this loop gets |
| 568 | * optimized out. |
| 569 | */ |
| 570 | size_t left = size; |
| 571 | do { |
| 572 | size_t len = left; |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 573 | void *vaddr; |
| 574 | |
| 575 | if (PageHighMem(page)) { |
| 576 | if (len + offset > PAGE_SIZE) { |
| 577 | if (offset >= PAGE_SIZE) { |
| 578 | page += offset / PAGE_SIZE; |
| 579 | offset %= PAGE_SIZE; |
| 580 | } |
| 581 | len = PAGE_SIZE - offset; |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 582 | } |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 583 | vaddr = kmap_high_get(page); |
| 584 | if (vaddr) { |
| 585 | vaddr += offset; |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 586 | op(vaddr, len, dir); |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 587 | kunmap_high(page); |
Nicolas Pitre | 7e5a69e | 2010-03-29 21:46:02 +0100 | [diff] [blame] | 588 | } else if (cache_is_vipt()) { |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 589 | /* unmapped pages might still be cached */ |
| 590 | vaddr = kmap_atomic(page); |
Nicolas Pitre | 7e5a69e | 2010-03-29 21:46:02 +0100 | [diff] [blame] | 591 | op(vaddr + offset, len, dir); |
Nicolas Pitre | 39af22a | 2010-12-15 15:14:45 -0500 | [diff] [blame] | 592 | kunmap_atomic(vaddr); |
Russell King | 93f1d62 | 2009-11-24 14:41:01 +0000 | [diff] [blame] | 593 | } |
| 594 | } else { |
| 595 | vaddr = page_address(page) + offset; |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 596 | op(vaddr, len, dir); |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 597 | } |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 598 | offset = 0; |
| 599 | page++; |
| 600 | left -= len; |
| 601 | } while (left); |
| 602 | } |
| 603 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 604 | /* |
| 605 | * Make an area consistent for devices. |
| 606 | * Note: Drivers should NOT use this function directly, as it will break |
| 607 | * platforms with CONFIG_DMABOUNCE. |
| 608 | * Use the driver DMA support - see dma-mapping.h (dma_sync_*) |
| 609 | */ |
| 610 | 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] | 611 | size_t size, enum dma_data_direction dir) |
| 612 | { |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 613 | unsigned long paddr; |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 614 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 615 | dma_cache_maint_page(page, off, size, dir, dmac_map_area); |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 616 | |
Russell King | 65af191 | 2009-11-24 17:53:33 +0000 | [diff] [blame] | 617 | paddr = page_to_phys(page) + off; |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 618 | if (dir == DMA_FROM_DEVICE) { |
| 619 | outer_inv_range(paddr, paddr + size); |
| 620 | } else { |
| 621 | outer_clean_range(paddr, paddr + size); |
| 622 | } |
| 623 | /* FIXME: non-speculating: flush on bidirectional mappings? */ |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 624 | } |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 625 | |
Marek Szyprowski | 51fde349 | 2012-02-10 19:55:20 +0100 | [diff] [blame^] | 626 | 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] | 627 | size_t size, enum dma_data_direction dir) |
| 628 | { |
Russell King | 2ffe2da | 2009-10-31 16:52:16 +0000 | [diff] [blame] | 629 | unsigned long paddr = page_to_phys(page) + off; |
| 630 | |
| 631 | /* FIXME: non-speculating: not required */ |
| 632 | /* don't bother invalidating if DMA to device */ |
| 633 | if (dir != DMA_TO_DEVICE) |
| 634 | outer_inv_range(paddr, paddr + size); |
| 635 | |
Russell King | a9c9147 | 2009-11-26 16:19:58 +0000 | [diff] [blame] | 636 | dma_cache_maint_page(page, off, size, dir, dmac_unmap_area); |
Catalin Marinas | c017780 | 2010-09-13 15:57:36 +0100 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Mark the D-cache clean for this page to avoid extra flushing. |
| 640 | */ |
| 641 | if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE) |
| 642 | set_bit(PG_dcache_clean, &page->flags); |
Russell King | 4ea0d73 | 2009-11-24 16:27:17 +0000 | [diff] [blame] | 643 | } |
Nicolas Pitre | 4337745 | 2009-03-12 22:52:09 -0400 | [diff] [blame] | 644 | |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 645 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 646 | * 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] | 647 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 648 | * @sg: list of buffers |
| 649 | * @nents: number of buffers to map |
| 650 | * @dir: DMA transfer direction |
| 651 | * |
| 652 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 653 | * This is the scatter-gather version of the dma_map_single interface. |
| 654 | * Here the scatter gather list elements are each tagged with the |
| 655 | * appropriate dma address and length. They are obtained via |
| 656 | * sg_dma_{address,length}. |
| 657 | * |
| 658 | * Device ownership issues as mentioned for dma_map_single are the same |
| 659 | * here. |
| 660 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 661 | int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 662 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 663 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 664 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 665 | struct scatterlist *s; |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 666 | int i, j; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 667 | |
| 668 | for_each_sg(sg, s, nents, i) { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 669 | s->dma_address = ops->map_page(dev, sg_page(s), s->offset, |
| 670 | s->length, dir, attrs); |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 671 | if (dma_mapping_error(dev, s->dma_address)) |
| 672 | goto bad_mapping; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 673 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 674 | return nents; |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 675 | |
| 676 | bad_mapping: |
| 677 | for_each_sg(sg, s, i, j) |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 678 | 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] | 679 | return 0; |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 680 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 681 | |
| 682 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 683 | * 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] | 684 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 685 | * @sg: list of buffers |
Linus Walleij | 0adfca6 | 2011-01-12 18:50:37 +0100 | [diff] [blame] | 686 | * @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] | 687 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 688 | * |
| 689 | * Unmap a set of streaming mode DMA translations. Again, CPU access |
| 690 | * rules concerning calls here are the same as for dma_unmap_single(). |
| 691 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 692 | void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 693 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 694 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 695 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 696 | struct scatterlist *s; |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 697 | |
Russell King | 01135d92 | 2008-09-25 21:05:02 +0100 | [diff] [blame] | 698 | int i; |
| 699 | |
| 700 | for_each_sg(sg, s, nents, i) |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 701 | 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] | 702 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 703 | |
| 704 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 705 | * arm_dma_sync_sg_for_cpu |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 706 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 707 | * @sg: list of buffers |
| 708 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 709 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 710 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 711 | 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] | 712 | int nents, enum dma_data_direction dir) |
| 713 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 714 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 715 | struct scatterlist *s; |
| 716 | int i; |
| 717 | |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 718 | for_each_sg(sg, s, nents, i) |
| 719 | ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length, |
| 720 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 721 | } |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 722 | |
| 723 | /** |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 724 | * arm_dma_sync_sg_for_device |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 725 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 726 | * @sg: list of buffers |
| 727 | * @nents: number of buffers to map (returned from dma_map_sg) |
| 728 | * @dir: DMA transfer direction (same as was passed to dma_map_sg) |
| 729 | */ |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 730 | 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] | 731 | int nents, enum dma_data_direction dir) |
| 732 | { |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 733 | struct dma_map_ops *ops = get_dma_ops(dev); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 734 | struct scatterlist *s; |
| 735 | int i; |
| 736 | |
Marek Szyprowski | 2a550e7 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 737 | for_each_sg(sg, s, nents, i) |
| 738 | ops->sync_single_for_device(dev, sg_dma_address(s), s->length, |
| 739 | dir); |
Russell King | afd1a32 | 2008-09-25 16:30:57 +0100 | [diff] [blame] | 740 | } |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 741 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 742 | /* |
| 743 | * Return whether the given device DMA address mask can be supported |
| 744 | * properly. For example, if your device can only drive the low 24-bits |
| 745 | * during bus mastering, then you would pass 0x00ffffff as the mask |
| 746 | * to this function. |
| 747 | */ |
| 748 | int dma_supported(struct device *dev, u64 mask) |
| 749 | { |
| 750 | if (mask < (u64)arm_dma_limit) |
| 751 | return 0; |
| 752 | return 1; |
| 753 | } |
| 754 | EXPORT_SYMBOL(dma_supported); |
| 755 | |
Marek Szyprowski | 2dc6a01 | 2012-02-10 19:55:20 +0100 | [diff] [blame] | 756 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask) |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 757 | { |
| 758 | if (!dev->dma_mask || !dma_supported(dev, dma_mask)) |
| 759 | return -EIO; |
| 760 | |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 761 | *dev->dma_mask = dma_mask; |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 762 | |
| 763 | return 0; |
| 764 | } |
Russell King | 022ae53 | 2011-07-08 21:26:59 +0100 | [diff] [blame] | 765 | |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 766 | #define PREALLOC_DMA_DEBUG_ENTRIES 4096 |
| 767 | |
| 768 | static int __init dma_debug_do_init(void) |
| 769 | { |
Russell King | 45cd529 | 2012-01-12 23:08:07 +0000 | [diff] [blame] | 770 | #ifdef CONFIG_MMU |
| 771 | arm_vmregion_create_proc("dma-mappings", &consistent_head); |
| 772 | #endif |
Russell King | 24056f5 | 2011-01-03 11:29:28 +0000 | [diff] [blame] | 773 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 774 | return 0; |
| 775 | } |
| 776 | fs_initcall(dma_debug_do_init); |