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