2 * linux/arch/arm/mm/dma-mapping.c
4 * Copyright (C) 2000-2004 Russell King
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.
10 * DMA uncached mapping support.
12 #include <linux/module.h>
14 #include <linux/slab.h>
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>
21 #include <asm/memory.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
24 #include <asm/sizes.h>
26 /* Sanity check size */
27 #if (CONSISTENT_DMA_SIZE % SZ_2M)
28 #error "CONSISTENT_DMA_SIZE must be multiple of 2MiB"
31 #define CONSISTENT_END (0xffe00000)
32 #define CONSISTENT_BASE (CONSISTENT_END - CONSISTENT_DMA_SIZE)
34 #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
35 #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT)
36 #define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT)
40 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
42 static pte_t *consistent_pte[NUM_CONSISTENT_PTES];
43 static DEFINE_SPINLOCK(consistent_lock);
46 * VM region handling support.
48 * This should become something generic, handling VM region allocations for
49 * vmalloc and similar (ioremap, module space, etc).
51 * I envisage vmalloc()'s supporting vm_struct becoming:
54 * struct vm_region region;
55 * unsigned long flags;
56 * struct page **pages;
57 * unsigned int nr_pages;
58 * unsigned long phys_addr;
61 * get_vm_area() would then call vm_region_alloc with an appropriate
62 * struct vm_region head (eg):
64 * struct vm_region vmalloc_head = {
65 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
66 * .vm_start = VMALLOC_START,
67 * .vm_end = VMALLOC_END,
70 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
71 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
72 * would have to initialise this each time prior to calling vm_region_alloc().
74 struct arm_vm_region {
75 struct list_head vm_list;
76 unsigned long vm_start;
78 struct page *vm_pages;
82 static struct arm_vm_region consistent_head = {
83 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
84 .vm_start = CONSISTENT_BASE,
85 .vm_end = CONSISTENT_END,
88 static struct arm_vm_region *
89 arm_vm_region_alloc(struct arm_vm_region *head, size_t size, gfp_t gfp)
91 unsigned long addr = head->vm_start, end = head->vm_end - size;
93 struct arm_vm_region *c, *new;
95 new = kmalloc(sizeof(struct arm_vm_region), gfp);
99 spin_lock_irqsave(&consistent_lock, flags);
101 list_for_each_entry(c, &head->vm_list, vm_list) {
102 if ((addr + size) < addr)
104 if ((addr + size) <= c->vm_start)
113 * Insert this entry _before_ the one we found.
115 list_add_tail(&new->vm_list, &c->vm_list);
116 new->vm_start = addr;
117 new->vm_end = addr + size;
120 spin_unlock_irqrestore(&consistent_lock, flags);
124 spin_unlock_irqrestore(&consistent_lock, flags);
130 static struct arm_vm_region *arm_vm_region_find(struct arm_vm_region *head, unsigned long addr)
132 struct arm_vm_region *c;
134 list_for_each_entry(c, &head->vm_list, vm_list) {
135 if (c->vm_active && c->vm_start == addr)
143 #ifdef CONFIG_HUGETLB_PAGE
144 #error ARM Coherent DMA allocator does not (yet) support huge TLB
148 __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
152 struct arm_vm_region *c;
154 u64 mask = ISA_DMA_THRESHOLD, limit;
156 if (!consistent_pte[0]) {
157 printk(KERN_ERR "%s: not initialised\n", __func__);
163 mask = dev->coherent_dma_mask;
166 * Sanity check the DMA mask - it must be non-zero, and
167 * must be able to be satisfied by a DMA allocation.
170 dev_warn(dev, "coherent DMA mask is unset\n");
174 if ((~mask) & ISA_DMA_THRESHOLD) {
175 dev_warn(dev, "coherent DMA mask %#llx is smaller "
176 "than system GFP_DMA mask %#llx\n",
177 mask, (unsigned long long)ISA_DMA_THRESHOLD);
183 * Sanity check the allocation size.
185 size = PAGE_ALIGN(size);
186 limit = (mask + 1) & ~mask;
187 if ((limit && size >= limit) ||
188 size >= (CONSISTENT_END - CONSISTENT_BASE)) {
189 printk(KERN_WARNING "coherent allocation too big "
190 "(requested %#x mask %#llx)\n", size, mask);
194 order = get_order(size);
196 if (mask != 0xffffffff)
199 page = alloc_pages(gfp, order);
204 * Invalidate any data that might be lurking in the
205 * kernel direct-mapped region for device DMA.
208 void *ptr = page_address(page);
209 memset(ptr, 0, size);
210 dmac_flush_range(ptr, ptr + size);
211 outer_flush_range(__pa(ptr), __pa(ptr) + size);
215 * Allocate a virtual address in the consistent mapping region.
217 c = arm_vm_region_alloc(&consistent_head, size,
218 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
221 struct page *end = page + (1 << order);
222 int idx = CONSISTENT_PTE_INDEX(c->vm_start);
223 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
225 pte = consistent_pte[idx] + off;
228 split_page(page, order);
231 * Set the "dma handle"
233 *handle = page_to_dma(dev, page);
236 BUG_ON(!pte_none(*pte));
239 * x86 does not mark the pages reserved...
241 SetPageReserved(page);
242 set_pte_ext(pte, mk_pte(page, prot), 0);
246 if (off >= PTRS_PER_PTE) {
248 pte = consistent_pte[++idx];
250 } while (size -= PAGE_SIZE);
253 * Free the otherwise unused pages.
260 return (void *)c->vm_start;
264 __free_pages(page, order);
271 * Allocate DMA-coherent memory space and return both the kernel remapped
272 * virtual and bus address for that space.
275 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
279 if (dma_alloc_from_coherent(dev, size, handle, &memory))
282 if (arch_is_coherent()) {
285 virt = kmalloc(size, gfp);
288 *handle = virt_to_dma(dev, virt);
293 return __dma_alloc(dev, size, handle, gfp,
294 pgprot_noncached(pgprot_kernel));
296 EXPORT_SYMBOL(dma_alloc_coherent);
299 * Allocate a writecombining region, in much the same way as
300 * dma_alloc_coherent above.
303 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
305 return __dma_alloc(dev, size, handle, gfp,
306 pgprot_writecombine(pgprot_kernel));
308 EXPORT_SYMBOL(dma_alloc_writecombine);
310 static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
311 void *cpu_addr, dma_addr_t dma_addr, size_t size)
313 unsigned long flags, user_size, kern_size;
314 struct arm_vm_region *c;
317 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
319 spin_lock_irqsave(&consistent_lock, flags);
320 c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
321 spin_unlock_irqrestore(&consistent_lock, flags);
324 unsigned long off = vma->vm_pgoff;
326 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
328 if (off < kern_size &&
329 user_size <= (kern_size - off)) {
330 ret = remap_pfn_range(vma, vma->vm_start,
331 page_to_pfn(c->vm_pages) + off,
332 user_size << PAGE_SHIFT,
340 int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
341 void *cpu_addr, dma_addr_t dma_addr, size_t size)
343 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
344 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
346 EXPORT_SYMBOL(dma_mmap_coherent);
348 int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
349 void *cpu_addr, dma_addr_t dma_addr, size_t size)
351 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
352 return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
354 EXPORT_SYMBOL(dma_mmap_writecombine);
357 * free a page as defined by the above mapping.
358 * Must not be called with IRQs disabled.
360 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
362 struct arm_vm_region *c;
363 unsigned long flags, addr;
368 WARN_ON(irqs_disabled());
370 if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
373 if (arch_is_coherent()) {
378 size = PAGE_ALIGN(size);
380 spin_lock_irqsave(&consistent_lock, flags);
381 c = arm_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
386 spin_unlock_irqrestore(&consistent_lock, flags);
388 if ((c->vm_end - c->vm_start) != size) {
389 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
390 __func__, c->vm_end - c->vm_start, size);
392 size = c->vm_end - c->vm_start;
395 idx = CONSISTENT_PTE_INDEX(c->vm_start);
396 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
397 ptep = consistent_pte[idx] + off;
400 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
406 if (off >= PTRS_PER_PTE) {
408 ptep = consistent_pte[++idx];
411 if (!pte_none(pte) && pte_present(pte)) {
414 if (pfn_valid(pfn)) {
415 struct page *page = pfn_to_page(pfn);
418 * x86 does not mark the pages reserved...
420 ClearPageReserved(page);
427 printk(KERN_CRIT "%s: bad page in kernel page table\n",
429 } while (size -= PAGE_SIZE);
431 flush_tlb_kernel_range(c->vm_start, c->vm_end);
433 spin_lock_irqsave(&consistent_lock, flags);
434 list_del(&c->vm_list);
435 spin_unlock_irqrestore(&consistent_lock, flags);
441 spin_unlock_irqrestore(&consistent_lock, flags);
442 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
446 EXPORT_SYMBOL(dma_free_coherent);
449 * Initialise the consistent memory allocation.
451 static int __init consistent_init(void)
457 u32 base = CONSISTENT_BASE;
460 pgd = pgd_offset(&init_mm, base);
461 pmd = pmd_alloc(&init_mm, pgd, base);
463 printk(KERN_ERR "%s: no pmd tables\n", __func__);
467 WARN_ON(!pmd_none(*pmd));
469 pte = pte_alloc_kernel(pmd, base);
471 printk(KERN_ERR "%s: no pte tables\n", __func__);
476 consistent_pte[i++] = pte;
477 base += (1 << PGDIR_SHIFT);
478 } while (base < CONSISTENT_END);
483 core_initcall(consistent_init);
486 * Make an area consistent for devices.
487 * Note: Drivers should NOT use this function directly, as it will break
488 * platforms with CONFIG_DMABOUNCE.
489 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
491 void dma_cache_maint(const void *start, size_t size, int direction)
493 const void *end = start + size;
495 BUG_ON(!virt_addr_valid(start) || !virt_addr_valid(end - 1));
498 case DMA_FROM_DEVICE: /* invalidate only */
499 dmac_inv_range(start, end);
500 outer_inv_range(__pa(start), __pa(end));
502 case DMA_TO_DEVICE: /* writeback only */
503 dmac_clean_range(start, end);
504 outer_clean_range(__pa(start), __pa(end));
506 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
507 dmac_flush_range(start, end);
508 outer_flush_range(__pa(start), __pa(end));
514 EXPORT_SYMBOL(dma_cache_maint);
517 * dma_map_sg - map a set of SG buffers for streaming mode DMA
518 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
519 * @sg: list of buffers
520 * @nents: number of buffers to map
521 * @dir: DMA transfer direction
523 * Map a set of buffers described by scatterlist in streaming mode for DMA.
524 * This is the scatter-gather version of the dma_map_single interface.
525 * Here the scatter gather list elements are each tagged with the
526 * appropriate dma address and length. They are obtained via
527 * sg_dma_{address,length}.
529 * Device ownership issues as mentioned for dma_map_single are the same
532 int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
533 enum dma_data_direction dir)
535 struct scatterlist *s;
538 for_each_sg(sg, s, nents, i) {
539 s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
541 if (dma_mapping_error(dev, s->dma_address))
547 for_each_sg(sg, s, i, j)
548 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
551 EXPORT_SYMBOL(dma_map_sg);
554 * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
555 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
556 * @sg: list of buffers
557 * @nents: number of buffers to unmap (returned from dma_map_sg)
558 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
560 * Unmap a set of streaming mode DMA translations. Again, CPU access
561 * rules concerning calls here are the same as for dma_unmap_single().
563 void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
564 enum dma_data_direction dir)
566 struct scatterlist *s;
569 for_each_sg(sg, s, nents, i)
570 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
572 EXPORT_SYMBOL(dma_unmap_sg);
575 * dma_sync_sg_for_cpu
576 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
577 * @sg: list of buffers
578 * @nents: number of buffers to map (returned from dma_map_sg)
579 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
581 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
582 int nents, enum dma_data_direction dir)
584 struct scatterlist *s;
587 for_each_sg(sg, s, nents, i) {
588 dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
592 EXPORT_SYMBOL(dma_sync_sg_for_cpu);
595 * dma_sync_sg_for_device
596 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
597 * @sg: list of buffers
598 * @nents: number of buffers to map (returned from dma_map_sg)
599 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
601 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
602 int nents, enum dma_data_direction dir)
604 struct scatterlist *s;
607 for_each_sg(sg, s, nents, i) {
608 if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
612 if (!arch_is_coherent())
613 dma_cache_maint(sg_virt(s), s->length, dir);
616 EXPORT_SYMBOL(dma_sync_sg_for_device);