aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mm/dma-mapping.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm/mm/dma-mapping.c')
-rw-r--r--arch/arm/mm/dma-mapping.c497
1 files changed, 181 insertions, 316 deletions
diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c
index b9590a7085ca..26325cb5d368 100644
--- a/arch/arm/mm/dma-mapping.c
+++ b/arch/arm/mm/dma-mapping.c
@@ -63,194 +63,152 @@ static u64 get_coherent_dma_mask(struct device *dev)
63 return mask; 63 return mask;
64} 64}
65 65
66#ifdef CONFIG_MMU
67/* 66/*
68 * These are the page tables (2MB each) covering uncached, DMA consistent allocations 67 * Allocate a DMA buffer for 'dev' of size 'size' using the
68 * specified gfp mask. Note that 'size' must be page aligned.
69 */ 69 */
70static pte_t *consistent_pte[NUM_CONSISTENT_PTES]; 70static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
71static DEFINE_SPINLOCK(consistent_lock); 71{
72 unsigned long order = get_order(size);
73 struct page *page, *p, *e;
74 void *ptr;
75 u64 mask = get_coherent_dma_mask(dev);
72 76
73/* 77#ifdef CONFIG_DMA_API_DEBUG
74 * VM region handling support. 78 u64 limit = (mask + 1) & ~mask;
75 * 79 if (limit && size >= limit) {
76 * This should become something generic, handling VM region allocations for 80 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
77 * vmalloc and similar (ioremap, module space, etc). 81 size, mask);
78 * 82 return NULL;
79 * I envisage vmalloc()'s supporting vm_struct becoming: 83 }
80 * 84#endif
81 * struct vm_struct {
82 * struct vm_region region;
83 * unsigned long flags;
84 * struct page **pages;
85 * unsigned int nr_pages;
86 * unsigned long phys_addr;
87 * };
88 *
89 * get_vm_area() would then call vm_region_alloc with an appropriate
90 * struct vm_region head (eg):
91 *
92 * struct vm_region vmalloc_head = {
93 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
94 * .vm_start = VMALLOC_START,
95 * .vm_end = VMALLOC_END,
96 * };
97 *
98 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
99 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
100 * would have to initialise this each time prior to calling vm_region_alloc().
101 */
102struct arm_vm_region {
103 struct list_head vm_list;
104 unsigned long vm_start;
105 unsigned long vm_end;
106 struct page *vm_pages;
107 int vm_active;
108};
109 85
110static struct arm_vm_region consistent_head = { 86 if (!mask)
111 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list), 87 return NULL;
112 .vm_start = CONSISTENT_BASE,
113 .vm_end = CONSISTENT_END,
114};
115 88
116static struct arm_vm_region * 89 if (mask < 0xffffffffULL)
117arm_vm_region_alloc(struct arm_vm_region *head, size_t size, gfp_t gfp) 90 gfp |= GFP_DMA;
118{ 91
119 unsigned long addr = head->vm_start, end = head->vm_end - size; 92 page = alloc_pages(gfp, order);
120 unsigned long flags; 93 if (!page)
121 struct arm_vm_region *c, *new; 94 return NULL;
122
123 new = kmalloc(sizeof(struct arm_vm_region), gfp);
124 if (!new)
125 goto out;
126
127 spin_lock_irqsave(&consistent_lock, flags);
128
129 list_for_each_entry(c, &head->vm_list, vm_list) {
130 if ((addr + size) < addr)
131 goto nospc;
132 if ((addr + size) <= c->vm_start)
133 goto found;
134 addr = c->vm_end;
135 if (addr > end)
136 goto nospc;
137 }
138 95
139 found:
140 /* 96 /*
141 * Insert this entry _before_ the one we found. 97 * Now split the huge page and free the excess pages
142 */ 98 */
143 list_add_tail(&new->vm_list, &c->vm_list); 99 split_page(page, order);
144 new->vm_start = addr; 100 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
145 new->vm_end = addr + size; 101 __free_page(p);
146 new->vm_active = 1; 102
147 103 /*
148 spin_unlock_irqrestore(&consistent_lock, flags); 104 * Ensure that the allocated pages are zeroed, and that any data
149 return new; 105 * lurking in the kernel direct-mapped region is invalidated.
150 106 */
151 nospc: 107 ptr = page_address(page);
152 spin_unlock_irqrestore(&consistent_lock, flags); 108 memset(ptr, 0, size);
153 kfree(new); 109 dmac_flush_range(ptr, ptr + size);
154 out: 110 outer_flush_range(__pa(ptr), __pa(ptr) + size);
155 return NULL; 111
112 return page;
156} 113}
157 114
158static struct arm_vm_region *arm_vm_region_find(struct arm_vm_region *head, unsigned long addr) 115/*
116 * Free a DMA buffer. 'size' must be page aligned.
117 */
118static void __dma_free_buffer(struct page *page, size_t size)
159{ 119{
160 struct arm_vm_region *c; 120 struct page *e = page + (size >> PAGE_SHIFT);
161 121
162 list_for_each_entry(c, &head->vm_list, vm_list) { 122 while (page < e) {
163 if (c->vm_active && c->vm_start == addr) 123 __free_page(page);
164 goto out; 124 page++;
165 } 125 }
166 c = NULL;
167 out:
168 return c;
169} 126}
170 127
128#ifdef CONFIG_MMU
129/*
130 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
131 */
132static pte_t *consistent_pte[NUM_CONSISTENT_PTES];
133
134#include "vmregion.h"
135
136static struct arm_vmregion_head consistent_head = {
137 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
138 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
139 .vm_start = CONSISTENT_BASE,
140 .vm_end = CONSISTENT_END,
141};
142
171#ifdef CONFIG_HUGETLB_PAGE 143#ifdef CONFIG_HUGETLB_PAGE
172#error ARM Coherent DMA allocator does not (yet) support huge TLB 144#error ARM Coherent DMA allocator does not (yet) support huge TLB
173#endif 145#endif
174 146
175static void * 147/*
176__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, 148 * Initialise the consistent memory allocation.
177 pgprot_t prot) 149 */
150static int __init consistent_init(void)
178{ 151{
179 struct page *page; 152 int ret = 0;
180 struct arm_vm_region *c; 153 pgd_t *pgd;
181 unsigned long order; 154 pmd_t *pmd;
182 u64 mask = get_coherent_dma_mask(dev); 155 pte_t *pte;
183 u64 limit; 156 int i = 0;
157 u32 base = CONSISTENT_BASE;
184 158
185 if (!consistent_pte[0]) { 159 do {
186 printk(KERN_ERR "%s: not initialised\n", __func__); 160 pgd = pgd_offset(&init_mm, base);
187 dump_stack(); 161 pmd = pmd_alloc(&init_mm, pgd, base);
188 return NULL; 162 if (!pmd) {
189 } 163 printk(KERN_ERR "%s: no pmd tables\n", __func__);
164 ret = -ENOMEM;
165 break;
166 }
167 WARN_ON(!pmd_none(*pmd));
190 168
191 if (!mask) 169 pte = pte_alloc_kernel(pmd, base);
192 goto no_page; 170 if (!pte) {
171 printk(KERN_ERR "%s: no pte tables\n", __func__);
172 ret = -ENOMEM;
173 break;
174 }
193 175
194 /* 176 consistent_pte[i++] = pte;
195 * Sanity check the allocation size. 177 base += (1 << PGDIR_SHIFT);
196 */ 178 } while (base < CONSISTENT_END);
197 size = PAGE_ALIGN(size);
198 limit = (mask + 1) & ~mask;
199 if ((limit && size >= limit) ||
200 size >= (CONSISTENT_END - CONSISTENT_BASE)) {
201 printk(KERN_WARNING "coherent allocation too big "
202 "(requested %#x mask %#llx)\n", size, mask);
203 goto no_page;
204 }
205 179
206 order = get_order(size); 180 return ret;
181}
207 182
208 if (mask < 0xffffffffULL) 183core_initcall(consistent_init);
209 gfp |= GFP_DMA;
210 184
211 page = alloc_pages(gfp, order); 185static void *
212 if (!page) 186__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot)
213 goto no_page; 187{
188 struct arm_vmregion *c;
214 189
215 /* 190 if (!consistent_pte[0]) {
216 * Invalidate any data that might be lurking in the 191 printk(KERN_ERR "%s: not initialised\n", __func__);
217 * kernel direct-mapped region for device DMA. 192 dump_stack();
218 */ 193 return NULL;
219 {
220 void *ptr = page_address(page);
221 memset(ptr, 0, size);
222 dmac_flush_range(ptr, ptr + size);
223 outer_flush_range(__pa(ptr), __pa(ptr) + size);
224 } 194 }
225 195
226 /* 196 /*
227 * Allocate a virtual address in the consistent mapping region. 197 * Allocate a virtual address in the consistent mapping region.
228 */ 198 */
229 c = arm_vm_region_alloc(&consistent_head, size, 199 c = arm_vmregion_alloc(&consistent_head, size,
230 gfp & ~(__GFP_DMA | __GFP_HIGHMEM)); 200 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
231 if (c) { 201 if (c) {
232 pte_t *pte; 202 pte_t *pte;
233 struct page *end = page + (1 << order);
234 int idx = CONSISTENT_PTE_INDEX(c->vm_start); 203 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
235 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); 204 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
236 205
237 pte = consistent_pte[idx] + off; 206 pte = consistent_pte[idx] + off;
238 c->vm_pages = page; 207 c->vm_pages = page;
239 208
240 split_page(page, order);
241
242 /*
243 * Set the "dma handle"
244 */
245 *handle = page_to_dma(dev, page);
246
247 do { 209 do {
248 BUG_ON(!pte_none(*pte)); 210 BUG_ON(!pte_none(*pte));
249 211
250 /*
251 * x86 does not mark the pages reserved...
252 */
253 SetPageReserved(page);
254 set_pte_ext(pte, mk_pte(page, prot), 0); 212 set_pte_ext(pte, mk_pte(page, prot), 0);
255 page++; 213 page++;
256 pte++; 214 pte++;
@@ -261,48 +219,90 @@ __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
261 } 219 }
262 } while (size -= PAGE_SIZE); 220 } while (size -= PAGE_SIZE);
263 221
264 /*
265 * Free the otherwise unused pages.
266 */
267 while (page < end) {
268 __free_page(page);
269 page++;
270 }
271
272 return (void *)c->vm_start; 222 return (void *)c->vm_start;
273 } 223 }
274
275 if (page)
276 __free_pages(page, order);
277 no_page:
278 *handle = ~0;
279 return NULL; 224 return NULL;
280} 225}
226
227static void __dma_free_remap(void *cpu_addr, size_t size)
228{
229 struct arm_vmregion *c;
230 unsigned long addr;
231 pte_t *ptep;
232 int idx;
233 u32 off;
234
235 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
236 if (!c) {
237 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
238 __func__, cpu_addr);
239 dump_stack();
240 return;
241 }
242
243 if ((c->vm_end - c->vm_start) != size) {
244 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
245 __func__, c->vm_end - c->vm_start, size);
246 dump_stack();
247 size = c->vm_end - c->vm_start;
248 }
249
250 idx = CONSISTENT_PTE_INDEX(c->vm_start);
251 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
252 ptep = consistent_pte[idx] + off;
253 addr = c->vm_start;
254 do {
255 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
256
257 ptep++;
258 addr += PAGE_SIZE;
259 off++;
260 if (off >= PTRS_PER_PTE) {
261 off = 0;
262 ptep = consistent_pte[++idx];
263 }
264
265 if (pte_none(pte) || !pte_present(pte))
266 printk(KERN_CRIT "%s: bad page in kernel page table\n",
267 __func__);
268 } while (size -= PAGE_SIZE);
269
270 flush_tlb_kernel_range(c->vm_start, c->vm_end);
271
272 arm_vmregion_free(&consistent_head, c);
273}
274
281#else /* !CONFIG_MMU */ 275#else /* !CONFIG_MMU */
276
277#define __dma_alloc_remap(page, size, gfp, prot) page_address(page)
278#define __dma_free_remap(addr, size) do { } while (0)
279
280#endif /* CONFIG_MMU */
281
282static void * 282static void *
283__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, 283__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
284 pgprot_t prot) 284 pgprot_t prot)
285{ 285{
286 void *virt; 286 struct page *page;
287 u64 mask = get_coherent_dma_mask(dev); 287 void *addr;
288 288
289 if (!mask) 289 *handle = ~0;
290 goto error; 290 size = PAGE_ALIGN(size);
291 291
292 if (mask < 0xffffffffULL) 292 page = __dma_alloc_buffer(dev, size, gfp);
293 gfp |= GFP_DMA; 293 if (!page)
294 virt = kmalloc(size, gfp); 294 return NULL;
295 if (!virt)
296 goto error;
297 295
298 *handle = virt_to_dma(dev, virt); 296 if (!arch_is_coherent())
299 return virt; 297 addr = __dma_alloc_remap(page, size, gfp, prot);
298 else
299 addr = page_address(page);
300 300
301error: 301 if (addr)
302 *handle = ~0; 302 *handle = page_to_dma(dev, page);
303 return NULL; 303
304 return addr;
304} 305}
305#endif /* CONFIG_MMU */
306 306
307/* 307/*
308 * Allocate DMA-coherent memory space and return both the kernel remapped 308 * Allocate DMA-coherent memory space and return both the kernel remapped
@@ -316,19 +316,8 @@ dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gf
316 if (dma_alloc_from_coherent(dev, size, handle, &memory)) 316 if (dma_alloc_from_coherent(dev, size, handle, &memory))
317 return memory; 317 return memory;
318 318
319 if (arch_is_coherent()) {
320 void *virt;
321
322 virt = kmalloc(size, gfp);
323 if (!virt)
324 return NULL;
325 *handle = virt_to_dma(dev, virt);
326
327 return virt;
328 }
329
330 return __dma_alloc(dev, size, handle, gfp, 319 return __dma_alloc(dev, size, handle, gfp,
331 pgprot_noncached(pgprot_kernel)); 320 pgprot_dmacoherent(pgprot_kernel));
332} 321}
333EXPORT_SYMBOL(dma_alloc_coherent); 322EXPORT_SYMBOL(dma_alloc_coherent);
334 323
@@ -349,15 +338,12 @@ static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
349{ 338{
350 int ret = -ENXIO; 339 int ret = -ENXIO;
351#ifdef CONFIG_MMU 340#ifdef CONFIG_MMU
352 unsigned long flags, user_size, kern_size; 341 unsigned long user_size, kern_size;
353 struct arm_vm_region *c; 342 struct arm_vmregion *c;
354 343
355 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 344 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
356 345
357 spin_lock_irqsave(&consistent_lock, flags); 346 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
358 c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
359 spin_unlock_irqrestore(&consistent_lock, flags);
360
361 if (c) { 347 if (c) {
362 unsigned long off = vma->vm_pgoff; 348 unsigned long off = vma->vm_pgoff;
363 349
@@ -379,7 +365,7 @@ static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
379int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma, 365int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
380 void *cpu_addr, dma_addr_t dma_addr, size_t size) 366 void *cpu_addr, dma_addr_t dma_addr, size_t size)
381{ 367{
382 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 368 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
383 return dma_mmap(dev, vma, cpu_addr, dma_addr, size); 369 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
384} 370}
385EXPORT_SYMBOL(dma_mmap_coherent); 371EXPORT_SYMBOL(dma_mmap_coherent);
@@ -396,144 +382,23 @@ EXPORT_SYMBOL(dma_mmap_writecombine);
396 * free a page as defined by the above mapping. 382 * free a page as defined by the above mapping.
397 * Must not be called with IRQs disabled. 383 * Must not be called with IRQs disabled.
398 */ 384 */
399#ifdef CONFIG_MMU
400void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle) 385void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
401{ 386{
402 struct arm_vm_region *c;
403 unsigned long flags, addr;
404 pte_t *ptep;
405 int idx;
406 u32 off;
407
408 WARN_ON(irqs_disabled()); 387 WARN_ON(irqs_disabled());
409 388
410 if (dma_release_from_coherent(dev, get_order(size), cpu_addr)) 389 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
411 return; 390 return;
412 391
413 if (arch_is_coherent()) {
414 kfree(cpu_addr);
415 return;
416 }
417
418 size = PAGE_ALIGN(size); 392 size = PAGE_ALIGN(size);
419 393
420 spin_lock_irqsave(&consistent_lock, flags); 394 if (!arch_is_coherent())
421 c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr); 395 __dma_free_remap(cpu_addr, size);
422 if (!c)
423 goto no_area;
424
425 c->vm_active = 0;
426 spin_unlock_irqrestore(&consistent_lock, flags);
427
428 if ((c->vm_end - c->vm_start) != size) {
429 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
430 __func__, c->vm_end - c->vm_start, size);
431 dump_stack();
432 size = c->vm_end - c->vm_start;
433 }
434
435 idx = CONSISTENT_PTE_INDEX(c->vm_start);
436 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
437 ptep = consistent_pte[idx] + off;
438 addr = c->vm_start;
439 do {
440 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
441 unsigned long pfn;
442
443 ptep++;
444 addr += PAGE_SIZE;
445 off++;
446 if (off >= PTRS_PER_PTE) {
447 off = 0;
448 ptep = consistent_pte[++idx];
449 }
450
451 if (!pte_none(pte) && pte_present(pte)) {
452 pfn = pte_pfn(pte);
453
454 if (pfn_valid(pfn)) {
455 struct page *page = pfn_to_page(pfn);
456
457 /*
458 * x86 does not mark the pages reserved...
459 */
460 ClearPageReserved(page);
461
462 __free_page(page);
463 continue;
464 }
465 }
466
467 printk(KERN_CRIT "%s: bad page in kernel page table\n",
468 __func__);
469 } while (size -= PAGE_SIZE);
470
471 flush_tlb_kernel_range(c->vm_start, c->vm_end);
472
473 spin_lock_irqsave(&consistent_lock, flags);
474 list_del(&c->vm_list);
475 spin_unlock_irqrestore(&consistent_lock, flags);
476
477 kfree(c);
478 return;
479 396
480 no_area: 397 __dma_free_buffer(dma_to_page(dev, handle), size);
481 spin_unlock_irqrestore(&consistent_lock, flags);
482 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
483 __func__, cpu_addr);
484 dump_stack();
485} 398}
486#else /* !CONFIG_MMU */
487void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
488{
489 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
490 return;
491 kfree(cpu_addr);
492}
493#endif /* CONFIG_MMU */
494EXPORT_SYMBOL(dma_free_coherent); 399EXPORT_SYMBOL(dma_free_coherent);
495 400
496/* 401/*
497 * Initialise the consistent memory allocation.
498 */
499static int __init consistent_init(void)
500{
501 int ret = 0;
502#ifdef CONFIG_MMU
503 pgd_t *pgd;
504 pmd_t *pmd;
505 pte_t *pte;
506 int i = 0;
507 u32 base = CONSISTENT_BASE;
508
509 do {
510 pgd = pgd_offset(&init_mm, base);
511 pmd = pmd_alloc(&init_mm, pgd, base);
512 if (!pmd) {
513 printk(KERN_ERR "%s: no pmd tables\n", __func__);
514 ret = -ENOMEM;
515 break;
516 }
517 WARN_ON(!pmd_none(*pmd));
518
519 pte = pte_alloc_kernel(pmd, base);
520 if (!pte) {
521 printk(KERN_ERR "%s: no pte tables\n", __func__);
522 ret = -ENOMEM;
523 break;
524 }
525
526 consistent_pte[i++] = pte;
527 base += (1 << PGDIR_SHIFT);
528 } while (base < CONSISTENT_END);
529#endif /* !CONFIG_MMU */
530
531 return ret;
532}
533
534core_initcall(consistent_init);
535
536/*
537 * Make an area consistent for devices. 402 * Make an area consistent for devices.
538 * Note: Drivers should NOT use this function directly, as it will break 403 * Note: Drivers should NOT use this function directly, as it will break
539 * platforms with CONFIG_DMABOUNCE. 404 * platforms with CONFIG_DMABOUNCE.