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/highmem.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
25 #include <asm/sizes.h>
27 /* Sanity check size */
28 #if (CONSISTENT_DMA_SIZE % SZ_2M)
29 #error "CONSISTENT_DMA_SIZE must be multiple of 2MiB"
32 #define CONSISTENT_END (0xffe00000)
33 #define CONSISTENT_BASE (CONSISTENT_END - CONSISTENT_DMA_SIZE)
35 #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
36 #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT)
37 #define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT)
41 * These are the page tables (2MB each) covering uncached, DMA consistent allocations
43 static pte_t
*consistent_pte
[NUM_CONSISTENT_PTES
];
44 static DEFINE_SPINLOCK(consistent_lock
);
47 * VM region handling support.
49 * This should become something generic, handling VM region allocations for
50 * vmalloc and similar (ioremap, module space, etc).
52 * I envisage vmalloc()'s supporting vm_struct becoming:
55 * struct vm_region region;
56 * unsigned long flags;
57 * struct page **pages;
58 * unsigned int nr_pages;
59 * unsigned long phys_addr;
62 * get_vm_area() would then call vm_region_alloc with an appropriate
63 * struct vm_region head (eg):
65 * struct vm_region vmalloc_head = {
66 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
67 * .vm_start = VMALLOC_START,
68 * .vm_end = VMALLOC_END,
71 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
72 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
73 * would have to initialise this each time prior to calling vm_region_alloc().
75 struct arm_vm_region
{
76 struct list_head vm_list
;
77 unsigned long vm_start
;
79 struct page
*vm_pages
;
83 static struct arm_vm_region consistent_head
= {
84 .vm_list
= LIST_HEAD_INIT(consistent_head
.vm_list
),
85 .vm_start
= CONSISTENT_BASE
,
86 .vm_end
= CONSISTENT_END
,
89 static struct arm_vm_region
*
90 arm_vm_region_alloc(struct arm_vm_region
*head
, size_t size
, gfp_t gfp
)
92 unsigned long addr
= head
->vm_start
, end
= head
->vm_end
- size
;
94 struct arm_vm_region
*c
, *new;
96 new = kmalloc(sizeof(struct arm_vm_region
), gfp
);
100 spin_lock_irqsave(&consistent_lock
, flags
);
102 list_for_each_entry(c
, &head
->vm_list
, vm_list
) {
103 if ((addr
+ size
) < addr
)
105 if ((addr
+ size
) <= c
->vm_start
)
114 * Insert this entry _before_ the one we found.
116 list_add_tail(&new->vm_list
, &c
->vm_list
);
117 new->vm_start
= addr
;
118 new->vm_end
= addr
+ size
;
121 spin_unlock_irqrestore(&consistent_lock
, flags
);
125 spin_unlock_irqrestore(&consistent_lock
, flags
);
131 static struct arm_vm_region
*arm_vm_region_find(struct arm_vm_region
*head
, unsigned long addr
)
133 struct arm_vm_region
*c
;
135 list_for_each_entry(c
, &head
->vm_list
, vm_list
) {
136 if (c
->vm_active
&& c
->vm_start
== addr
)
144 #ifdef CONFIG_HUGETLB_PAGE
145 #error ARM Coherent DMA allocator does not (yet) support huge TLB
149 __dma_alloc(struct device
*dev
, size_t size
, dma_addr_t
*handle
, gfp_t gfp
,
153 struct arm_vm_region
*c
;
155 u64 mask
= ISA_DMA_THRESHOLD
, limit
;
157 if (!consistent_pte
[0]) {
158 printk(KERN_ERR
"%s: not initialised\n", __func__
);
164 mask
= dev
->coherent_dma_mask
;
167 * Sanity check the DMA mask - it must be non-zero, and
168 * must be able to be satisfied by a DMA allocation.
171 dev_warn(dev
, "coherent DMA mask is unset\n");
175 if ((~mask
) & ISA_DMA_THRESHOLD
) {
176 dev_warn(dev
, "coherent DMA mask %#llx is smaller "
177 "than system GFP_DMA mask %#llx\n",
178 mask
, (unsigned long long)ISA_DMA_THRESHOLD
);
184 * Sanity check the allocation size.
186 size
= PAGE_ALIGN(size
);
187 limit
= (mask
+ 1) & ~mask
;
188 if ((limit
&& size
>= limit
) ||
189 size
>= (CONSISTENT_END
- CONSISTENT_BASE
)) {
190 printk(KERN_WARNING
"coherent allocation too big "
191 "(requested %#x mask %#llx)\n", size
, mask
);
195 order
= get_order(size
);
197 if (mask
!= 0xffffffff)
200 page
= alloc_pages(gfp
, order
);
205 * Invalidate any data that might be lurking in the
206 * kernel direct-mapped region for device DMA.
209 void *ptr
= page_address(page
);
210 memset(ptr
, 0, size
);
211 dmac_flush_range(ptr
, ptr
+ size
);
212 outer_flush_range(__pa(ptr
), __pa(ptr
) + size
);
216 * Allocate a virtual address in the consistent mapping region.
218 c
= arm_vm_region_alloc(&consistent_head
, size
,
219 gfp
& ~(__GFP_DMA
| __GFP_HIGHMEM
));
222 struct page
*end
= page
+ (1 << order
);
223 int idx
= CONSISTENT_PTE_INDEX(c
->vm_start
);
224 u32 off
= CONSISTENT_OFFSET(c
->vm_start
) & (PTRS_PER_PTE
-1);
226 pte
= consistent_pte
[idx
] + off
;
229 split_page(page
, order
);
232 * Set the "dma handle"
234 *handle
= page_to_dma(dev
, page
);
237 BUG_ON(!pte_none(*pte
));
240 * x86 does not mark the pages reserved...
242 SetPageReserved(page
);
243 set_pte_ext(pte
, mk_pte(page
, prot
), 0);
247 if (off
>= PTRS_PER_PTE
) {
249 pte
= consistent_pte
[++idx
];
251 } while (size
-= PAGE_SIZE
);
254 * Free the otherwise unused pages.
261 return (void *)c
->vm_start
;
265 __free_pages(page
, order
);
272 * Allocate DMA-coherent memory space and return both the kernel remapped
273 * virtual and bus address for that space.
276 dma_alloc_coherent(struct device
*dev
, size_t size
, dma_addr_t
*handle
, gfp_t gfp
)
280 if (dma_alloc_from_coherent(dev
, size
, handle
, &memory
))
283 if (arch_is_coherent()) {
286 virt
= kmalloc(size
, gfp
);
289 *handle
= virt_to_dma(dev
, virt
);
294 return __dma_alloc(dev
, size
, handle
, gfp
,
295 pgprot_noncached(pgprot_kernel
));
297 EXPORT_SYMBOL(dma_alloc_coherent
);
300 * Allocate a writecombining region, in much the same way as
301 * dma_alloc_coherent above.
304 dma_alloc_writecombine(struct device
*dev
, size_t size
, dma_addr_t
*handle
, gfp_t gfp
)
306 return __dma_alloc(dev
, size
, handle
, gfp
,
307 pgprot_writecombine(pgprot_kernel
));
309 EXPORT_SYMBOL(dma_alloc_writecombine
);
311 static int dma_mmap(struct device
*dev
, struct vm_area_struct
*vma
,
312 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
314 unsigned long flags
, user_size
, kern_size
;
315 struct arm_vm_region
*c
;
318 user_size
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
320 spin_lock_irqsave(&consistent_lock
, flags
);
321 c
= arm_vm_region_find(&consistent_head
, (unsigned long)cpu_addr
);
322 spin_unlock_irqrestore(&consistent_lock
, flags
);
325 unsigned long off
= vma
->vm_pgoff
;
327 kern_size
= (c
->vm_end
- c
->vm_start
) >> PAGE_SHIFT
;
329 if (off
< kern_size
&&
330 user_size
<= (kern_size
- off
)) {
331 ret
= remap_pfn_range(vma
, vma
->vm_start
,
332 page_to_pfn(c
->vm_pages
) + off
,
333 user_size
<< PAGE_SHIFT
,
341 int dma_mmap_coherent(struct device
*dev
, struct vm_area_struct
*vma
,
342 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
344 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
345 return dma_mmap(dev
, vma
, cpu_addr
, dma_addr
, size
);
347 EXPORT_SYMBOL(dma_mmap_coherent
);
349 int dma_mmap_writecombine(struct device
*dev
, struct vm_area_struct
*vma
,
350 void *cpu_addr
, dma_addr_t dma_addr
, size_t size
)
352 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
353 return dma_mmap(dev
, vma
, cpu_addr
, dma_addr
, size
);
355 EXPORT_SYMBOL(dma_mmap_writecombine
);
358 * free a page as defined by the above mapping.
359 * Must not be called with IRQs disabled.
361 void dma_free_coherent(struct device
*dev
, size_t size
, void *cpu_addr
, dma_addr_t handle
)
363 struct arm_vm_region
*c
;
364 unsigned long flags
, addr
;
369 WARN_ON(irqs_disabled());
371 if (dma_release_from_coherent(dev
, get_order(size
), cpu_addr
))
374 if (arch_is_coherent()) {
379 size
= PAGE_ALIGN(size
);
381 spin_lock_irqsave(&consistent_lock
, flags
);
382 c
= arm_vm_region_find(&consistent_head
, (unsigned long)cpu_addr
);
387 spin_unlock_irqrestore(&consistent_lock
, flags
);
389 if ((c
->vm_end
- c
->vm_start
) != size
) {
390 printk(KERN_ERR
"%s: freeing wrong coherent size (%ld != %d)\n",
391 __func__
, c
->vm_end
- c
->vm_start
, size
);
393 size
= c
->vm_end
- c
->vm_start
;
396 idx
= CONSISTENT_PTE_INDEX(c
->vm_start
);
397 off
= CONSISTENT_OFFSET(c
->vm_start
) & (PTRS_PER_PTE
-1);
398 ptep
= consistent_pte
[idx
] + off
;
401 pte_t pte
= ptep_get_and_clear(&init_mm
, addr
, ptep
);
407 if (off
>= PTRS_PER_PTE
) {
409 ptep
= consistent_pte
[++idx
];
412 if (!pte_none(pte
) && pte_present(pte
)) {
415 if (pfn_valid(pfn
)) {
416 struct page
*page
= pfn_to_page(pfn
);
419 * x86 does not mark the pages reserved...
421 ClearPageReserved(page
);
428 printk(KERN_CRIT
"%s: bad page in kernel page table\n",
430 } while (size
-= PAGE_SIZE
);
432 flush_tlb_kernel_range(c
->vm_start
, c
->vm_end
);
434 spin_lock_irqsave(&consistent_lock
, flags
);
435 list_del(&c
->vm_list
);
436 spin_unlock_irqrestore(&consistent_lock
, flags
);
442 spin_unlock_irqrestore(&consistent_lock
, flags
);
443 printk(KERN_ERR
"%s: trying to free invalid coherent area: %p\n",
447 EXPORT_SYMBOL(dma_free_coherent
);
450 * Initialise the consistent memory allocation.
452 static int __init
consistent_init(void)
458 u32 base
= CONSISTENT_BASE
;
461 pgd
= pgd_offset(&init_mm
, base
);
462 pmd
= pmd_alloc(&init_mm
, pgd
, base
);
464 printk(KERN_ERR
"%s: no pmd tables\n", __func__
);
468 WARN_ON(!pmd_none(*pmd
));
470 pte
= pte_alloc_kernel(pmd
, base
);
472 printk(KERN_ERR
"%s: no pte tables\n", __func__
);
477 consistent_pte
[i
++] = pte
;
478 base
+= (1 << PGDIR_SHIFT
);
479 } while (base
< CONSISTENT_END
);
484 core_initcall(consistent_init
);
487 * Make an area consistent for devices.
488 * Note: Drivers should NOT use this function directly, as it will break
489 * platforms with CONFIG_DMABOUNCE.
490 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
492 void dma_cache_maint(const void *start
, size_t size
, int direction
)
494 void (*inner_op
)(const void *, const void *);
495 void (*outer_op
)(unsigned long, unsigned long);
497 BUG_ON(!virt_addr_valid(start
) || !virt_addr_valid(start
+ size
- 1));
500 case DMA_FROM_DEVICE
: /* invalidate only */
501 inner_op
= dmac_inv_range
;
502 outer_op
= outer_inv_range
;
504 case DMA_TO_DEVICE
: /* writeback only */
505 inner_op
= dmac_clean_range
;
506 outer_op
= outer_clean_range
;
508 case DMA_BIDIRECTIONAL
: /* writeback and invalidate */
509 inner_op
= dmac_flush_range
;
510 outer_op
= outer_flush_range
;
516 inner_op(start
, start
+ size
);
517 outer_op(__pa(start
), __pa(start
) + size
);
519 EXPORT_SYMBOL(dma_cache_maint
);
521 static void dma_cache_maint_contiguous(struct page
*page
, unsigned long offset
,
522 size_t size
, int direction
)
526 void (*inner_op
)(const void *, const void *);
527 void (*outer_op
)(unsigned long, unsigned long);
530 case DMA_FROM_DEVICE
: /* invalidate only */
531 inner_op
= dmac_inv_range
;
532 outer_op
= outer_inv_range
;
534 case DMA_TO_DEVICE
: /* writeback only */
535 inner_op
= dmac_clean_range
;
536 outer_op
= outer_clean_range
;
538 case DMA_BIDIRECTIONAL
: /* writeback and invalidate */
539 inner_op
= dmac_flush_range
;
540 outer_op
= outer_flush_range
;
546 if (!PageHighMem(page
)) {
547 vaddr
= page_address(page
) + offset
;
548 inner_op(vaddr
, vaddr
+ size
);
550 vaddr
= kmap_high_get(page
);
553 inner_op(vaddr
, vaddr
+ size
);
558 paddr
= page_to_phys(page
) + offset
;
559 outer_op(paddr
, paddr
+ size
);
562 void dma_cache_maint_page(struct page
*page
, unsigned long offset
,
563 size_t size
, int dir
)
566 * A single sg entry may refer to multiple physically contiguous
567 * pages. But we still need to process highmem pages individually.
568 * If highmem is not configured then the bulk of this loop gets
574 if (PageHighMem(page
) && len
+ offset
> PAGE_SIZE
) {
575 if (offset
>= PAGE_SIZE
) {
576 page
+= offset
/ PAGE_SIZE
;
579 len
= PAGE_SIZE
- offset
;
581 dma_cache_maint_contiguous(page
, offset
, len
, dir
);
587 EXPORT_SYMBOL(dma_cache_maint_page
);
590 * dma_map_sg - map a set of SG buffers for streaming mode DMA
591 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
592 * @sg: list of buffers
593 * @nents: number of buffers to map
594 * @dir: DMA transfer direction
596 * Map a set of buffers described by scatterlist in streaming mode for DMA.
597 * This is the scatter-gather version of the dma_map_single interface.
598 * Here the scatter gather list elements are each tagged with the
599 * appropriate dma address and length. They are obtained via
600 * sg_dma_{address,length}.
602 * Device ownership issues as mentioned for dma_map_single are the same
605 int dma_map_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
606 enum dma_data_direction dir
)
608 struct scatterlist
*s
;
611 for_each_sg(sg
, s
, nents
, i
) {
612 s
->dma_address
= dma_map_page(dev
, sg_page(s
), s
->offset
,
614 if (dma_mapping_error(dev
, s
->dma_address
))
620 for_each_sg(sg
, s
, i
, j
)
621 dma_unmap_page(dev
, sg_dma_address(s
), sg_dma_len(s
), dir
);
624 EXPORT_SYMBOL(dma_map_sg
);
627 * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
628 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
629 * @sg: list of buffers
630 * @nents: number of buffers to unmap (returned from dma_map_sg)
631 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
633 * Unmap a set of streaming mode DMA translations. Again, CPU access
634 * rules concerning calls here are the same as for dma_unmap_single().
636 void dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
637 enum dma_data_direction dir
)
639 struct scatterlist
*s
;
642 for_each_sg(sg
, s
, nents
, i
)
643 dma_unmap_page(dev
, sg_dma_address(s
), sg_dma_len(s
), dir
);
645 EXPORT_SYMBOL(dma_unmap_sg
);
648 * dma_sync_sg_for_cpu
649 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
650 * @sg: list of buffers
651 * @nents: number of buffers to map (returned from dma_map_sg)
652 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
654 void dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
655 int nents
, enum dma_data_direction dir
)
657 struct scatterlist
*s
;
660 for_each_sg(sg
, s
, nents
, i
) {
661 dmabounce_sync_for_cpu(dev
, sg_dma_address(s
), 0,
665 EXPORT_SYMBOL(dma_sync_sg_for_cpu
);
668 * dma_sync_sg_for_device
669 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
670 * @sg: list of buffers
671 * @nents: number of buffers to map (returned from dma_map_sg)
672 * @dir: DMA transfer direction (same as was passed to dma_map_sg)
674 void dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
675 int nents
, enum dma_data_direction dir
)
677 struct scatterlist
*s
;
680 for_each_sg(sg
, s
, nents
, i
) {
681 if (!dmabounce_sync_for_device(dev
, sg_dma_address(s
), 0,
685 if (!arch_is_coherent())
686 dma_cache_maint_page(sg_page(s
), s
->offset
,
690 EXPORT_SYMBOL(dma_sync_sg_for_device
);