2 * linux/arch/arm/mm/consistent.c
4 * Copyright (C) 2000-2002 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/cacheflush.h>
23 #include <asm/tlbflush.h>
25 #define CONSISTENT_BASE (0xffc00000)
26 #define CONSISTENT_END (0xffe00000)
27 #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
30 * This is the page table (2MB) covering uncached, DMA consistent allocations
32 static pte_t *consistent_pte;
33 static spinlock_t consistent_lock = SPIN_LOCK_UNLOCKED;
36 * VM region handling support.
38 * This should become something generic, handling VM region allocations for
39 * vmalloc and similar (ioremap, module space, etc).
41 * I envisage vmalloc()'s supporting vm_struct becoming:
44 * struct vm_region region;
45 * unsigned long flags;
46 * struct page **pages;
47 * unsigned int nr_pages;
48 * unsigned long phys_addr;
51 * get_vm_area() would then call vm_region_alloc with an appropriate
52 * struct vm_region head (eg):
54 * struct vm_region vmalloc_head = {
55 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
56 * .vm_start = VMALLOC_START,
57 * .vm_end = VMALLOC_END,
60 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
61 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
62 * would have to initialise this each time prior to calling vm_region_alloc().
65 struct list_head vm_list;
66 unsigned long vm_start;
70 static struct vm_region consistent_head = {
71 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
72 .vm_start = CONSISTENT_BASE,
73 .vm_end = CONSISTENT_END,
76 static struct vm_region *
77 vm_region_alloc(struct vm_region *head, size_t size, int gfp)
79 unsigned long addr = head->vm_start, end = head->vm_end - size;
81 struct vm_region *c, *new;
83 new = kmalloc(sizeof(struct vm_region), gfp);
87 spin_lock_irqsave(&consistent_lock, flags);
89 list_for_each_entry(c, &head->vm_list, vm_list) {
90 if ((addr + size) < addr)
92 if ((addr + size) <= c->vm_start)
101 * Insert this entry _before_ the one we found.
103 list_add_tail(&new->vm_list, &c->vm_list);
104 new->vm_start = addr;
105 new->vm_end = addr + size;
107 spin_unlock_irqrestore(&consistent_lock, flags);
111 spin_unlock_irqrestore(&consistent_lock, flags);
117 static struct vm_region *vm_region_find(struct vm_region *head, unsigned long addr)
121 list_for_each_entry(c, &head->vm_list, vm_list) {
122 if (c->vm_start == addr)
130 #ifdef CONFIG_HUGETLB_PAGE
131 #error ARM Coherent DMA allocator does not (yet) support huge TLB
135 __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, int gfp,
141 u64 mask = 0x00ffffff, limit; /* ISA default */
143 if (!consistent_pte) {
144 printk(KERN_ERR "%s: not initialised\n", __func__);
150 mask = dev->coherent_dma_mask;
152 dev_warn(dev, "coherent DMA mask is unset\n");
157 size = PAGE_ALIGN(size);
158 limit = (mask + 1) & ~mask;
159 if ((limit && size >= limit) || size >= (CONSISTENT_END - CONSISTENT_BASE)) {
160 printk(KERN_WARNING "coherent allocation too big (requested %#x mask %#Lx)\n",
166 order = get_order(size);
168 if (mask != 0xffffffff)
171 page = alloc_pages(gfp, order);
176 * Invalidate any data that might be lurking in the
177 * kernel direct-mapped region for device DMA.
180 unsigned long kaddr = (unsigned long)page_address(page);
181 memset(page_address(page), 0, size);
182 dmac_flush_range(kaddr, kaddr + size);
186 * Allocate a virtual address in the consistent mapping region.
188 c = vm_region_alloc(&consistent_head, size,
189 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
191 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
192 struct page *end = page + (1 << order);
195 * Set the "dma handle"
197 *handle = page_to_bus(page);
200 BUG_ON(!pte_none(*pte));
202 set_page_count(page, 1);
203 SetPageReserved(page);
204 set_pte(pte, mk_pte(page, prot));
207 } while (size -= PAGE_SIZE);
210 * Free the otherwise unused pages.
213 set_page_count(page, 1);
218 return (void *)c->vm_start;
222 __free_pages(page, order);
228 * Allocate DMA-coherent memory space and return both the kernel remapped
229 * virtual and bus address for that space.
232 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
234 return __dma_alloc(dev, size, handle, gfp,
235 pgprot_noncached(pgprot_kernel));
237 EXPORT_SYMBOL(dma_alloc_coherent);
240 * Allocate a writecombining region, in much the same way as
241 * dma_alloc_coherent above.
244 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
246 return __dma_alloc(dev, size, handle, gfp,
247 pgprot_writecombine(pgprot_kernel));
249 EXPORT_SYMBOL(dma_alloc_writecombine);
252 * free a page as defined by the above mapping.
254 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
260 size = PAGE_ALIGN(size);
262 spin_lock_irqsave(&consistent_lock, flags);
264 c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
268 if ((c->vm_end - c->vm_start) != size) {
269 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
270 __func__, c->vm_end - c->vm_start, size);
272 size = c->vm_end - c->vm_start;
275 ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
277 pte_t pte = ptep_get_and_clear(ptep);
282 if (!pte_none(pte) && pte_present(pte)) {
285 if (pfn_valid(pfn)) {
286 struct page *page = pfn_to_page(pfn);
287 ClearPageReserved(page);
294 printk(KERN_CRIT "%s: bad page in kernel page table\n",
296 } while (size -= PAGE_SIZE);
298 flush_tlb_kernel_range(c->vm_start, c->vm_end);
300 list_del(&c->vm_list);
302 spin_unlock_irqrestore(&consistent_lock, flags);
308 spin_unlock_irqrestore(&consistent_lock, flags);
309 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
313 EXPORT_SYMBOL(dma_free_coherent);
316 * Initialise the consistent memory allocation.
318 static int __init consistent_init(void)
325 spin_lock(&init_mm.page_table_lock);
328 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
329 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
331 printk(KERN_ERR "%s: no pmd tables\n", __func__);
335 WARN_ON(!pmd_none(*pmd));
337 pte = pte_alloc_kernel(&init_mm, pmd, CONSISTENT_BASE);
339 printk(KERN_ERR "%s: no pte tables\n", __func__);
344 consistent_pte = pte;
347 spin_unlock(&init_mm.page_table_lock);
352 core_initcall(consistent_init);
355 * Make an area consistent for devices.
357 void consistent_sync(void *vaddr, size_t size, int direction)
359 unsigned long start = (unsigned long)vaddr;
360 unsigned long end = start + size;
363 case DMA_FROM_DEVICE: /* invalidate only */
364 dmac_inv_range(start, end);
366 case DMA_TO_DEVICE: /* writeback only */
367 dmac_clean_range(start, end);
369 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
370 dmac_flush_range(start, end);
376 EXPORT_SYMBOL(consistent_sync);