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