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