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 = ISA_DMA_THRESHOLD, limit;
143 if (!consistent_pte) {
144 printk(KERN_ERR "%s: not initialised\n", __func__);
150 mask = dev->coherent_dma_mask;
153 * Sanity check the DMA mask - it must be non-zero, and
154 * must be able to be satisfied by a DMA allocation.
157 dev_warn(dev, "coherent DMA mask is unset\n");
161 if ((~mask) & ISA_DMA_THRESHOLD) {
162 dev_warn(dev, "coherent DMA mask %#llx is smaller "
163 "than system GFP_DMA mask %#llx\n",
164 mask, (unsigned long long)ISA_DMA_THRESHOLD);
170 * Sanity check the allocation size.
172 size = PAGE_ALIGN(size);
173 limit = (mask + 1) & ~mask;
174 if ((limit && size >= limit) ||
175 size >= (CONSISTENT_END - CONSISTENT_BASE)) {
176 printk(KERN_WARNING "coherent allocation too big "
177 "(requested %#x mask %#llx)\n", size, mask);
181 order = get_order(size);
183 if (mask != 0xffffffff)
186 page = alloc_pages(gfp, order);
191 * Invalidate any data that might be lurking in the
192 * kernel direct-mapped region for device DMA.
195 unsigned long kaddr = (unsigned long)page_address(page);
196 memset(page_address(page), 0, size);
197 dmac_flush_range(kaddr, kaddr + size);
201 * Allocate a virtual address in the consistent mapping region.
203 c = vm_region_alloc(&consistent_head, size,
204 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
206 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
207 struct page *end = page + (1 << order);
210 * Set the "dma handle"
212 *handle = page_to_dma(dev, page);
215 BUG_ON(!pte_none(*pte));
217 set_page_count(page, 1);
218 SetPageReserved(page);
219 set_pte(pte, mk_pte(page, prot));
222 } while (size -= PAGE_SIZE);
225 * Free the otherwise unused pages.
228 set_page_count(page, 1);
233 return (void *)c->vm_start;
237 __free_pages(page, order);
244 * Allocate DMA-coherent memory space and return both the kernel remapped
245 * virtual and bus address for that space.
248 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
250 return __dma_alloc(dev, size, handle, gfp,
251 pgprot_noncached(pgprot_kernel));
253 EXPORT_SYMBOL(dma_alloc_coherent);
256 * Allocate a writecombining region, in much the same way as
257 * dma_alloc_coherent above.
260 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
262 return __dma_alloc(dev, size, handle, gfp,
263 pgprot_writecombine(pgprot_kernel));
265 EXPORT_SYMBOL(dma_alloc_writecombine);
268 * free a page as defined by the above mapping.
270 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
276 size = PAGE_ALIGN(size);
278 spin_lock_irqsave(&consistent_lock, flags);
280 c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
284 if ((c->vm_end - c->vm_start) != size) {
285 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
286 __func__, c->vm_end - c->vm_start, size);
288 size = c->vm_end - c->vm_start;
291 ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
293 pte_t pte = ptep_get_and_clear(ptep);
298 if (!pte_none(pte) && pte_present(pte)) {
301 if (pfn_valid(pfn)) {
302 struct page *page = pfn_to_page(pfn);
303 ClearPageReserved(page);
310 printk(KERN_CRIT "%s: bad page in kernel page table\n",
312 } while (size -= PAGE_SIZE);
314 flush_tlb_kernel_range(c->vm_start, c->vm_end);
316 list_del(&c->vm_list);
318 spin_unlock_irqrestore(&consistent_lock, flags);
324 spin_unlock_irqrestore(&consistent_lock, flags);
325 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
329 EXPORT_SYMBOL(dma_free_coherent);
332 * Initialise the consistent memory allocation.
334 static int __init consistent_init(void)
341 spin_lock(&init_mm.page_table_lock);
344 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
345 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
347 printk(KERN_ERR "%s: no pmd tables\n", __func__);
351 WARN_ON(!pmd_none(*pmd));
353 pte = pte_alloc_kernel(&init_mm, pmd, CONSISTENT_BASE);
355 printk(KERN_ERR "%s: no pte tables\n", __func__);
360 consistent_pte = pte;
363 spin_unlock(&init_mm.page_table_lock);
368 core_initcall(consistent_init);
371 * Make an area consistent for devices.
373 void consistent_sync(void *vaddr, size_t size, int direction)
375 unsigned long start = (unsigned long)vaddr;
376 unsigned long end = start + size;
379 case DMA_FROM_DEVICE: /* invalidate only */
380 dmac_inv_range(start, end);
382 case DMA_TO_DEVICE: /* writeback only */
383 dmac_clean_range(start, end);
385 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
386 dmac_flush_range(start, end);
392 EXPORT_SYMBOL(consistent_sync);