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