ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / mm / consistent.c
1 /*
2  *  linux/arch/arm/mm/consistent.c
3  *
4  *  Copyright (C) 2000-2002 Russell King
5  *
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.
9  *
10  *  DMA uncached mapping support.
11  */
12 #include <linux/module.h>
13 #include <linux/mm.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>
20
21 #include <asm/cacheflush.h>
22 #include <asm/io.h>
23 #include <asm/tlbflush.h>
24
25 #define CONSISTENT_BASE (0xffc00000)
26 #define CONSISTENT_END  (0xffe00000)
27 #define CONSISTENT_OFFSET(x)    (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
28
29 /*
30  * This is the page table (2MB) covering uncached, DMA consistent allocations
31  */
32 static pte_t *consistent_pte;
33 static spinlock_t consistent_lock = SPIN_LOCK_UNLOCKED;
34
35 /*
36  * VM region handling support.
37  *
38  * This should become something generic, handling VM region allocations for
39  * vmalloc and similar (ioremap, module space, etc).
40  *
41  * I envisage vmalloc()'s supporting vm_struct becoming:
42  *
43  *  struct vm_struct {
44  *    struct vm_region  region;
45  *    unsigned long     flags;
46  *    struct page       **pages;
47  *    unsigned int      nr_pages;
48  *    unsigned long     phys_addr;
49  *  };
50  *
51  * get_vm_area() would then call vm_region_alloc with an appropriate
52  * struct vm_region head (eg):
53  *
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,
58  *  };
59  *
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().
63  */
64 struct vm_region {
65         struct list_head        vm_list;
66         unsigned long           vm_start;
67         unsigned long           vm_end;
68 };
69
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,
74 };
75
76 static struct vm_region *
77 vm_region_alloc(struct vm_region *head, size_t size, int gfp)
78 {
79         unsigned long addr = head->vm_start, end = head->vm_end - size;
80         unsigned long flags;
81         struct vm_region *c, *new;
82
83         new = kmalloc(sizeof(struct vm_region), gfp);
84         if (!new)
85                 goto out;
86
87         spin_lock_irqsave(&consistent_lock, flags);
88
89         list_for_each_entry(c, &head->vm_list, vm_list) {
90                 if ((addr + size) < addr)
91                         goto nospc;
92                 if ((addr + size) <= c->vm_start)
93                         goto found;
94                 addr = c->vm_end;
95                 if (addr > end)
96                         goto nospc;
97         }
98
99  found:
100         /*
101          * Insert this entry _before_ the one we found.
102          */
103         list_add_tail(&new->vm_list, &c->vm_list);
104         new->vm_start = addr;
105         new->vm_end = addr + size;
106
107         spin_unlock_irqrestore(&consistent_lock, flags);
108         return new;
109
110  nospc:
111         spin_unlock_irqrestore(&consistent_lock, flags);
112         kfree(new);
113  out:
114         return NULL;
115 }
116
117 static struct vm_region *vm_region_find(struct vm_region *head, unsigned long addr)
118 {
119         struct vm_region *c;
120         
121         list_for_each_entry(c, &head->vm_list, vm_list) {
122                 if (c->vm_start == addr)
123                         goto out;
124         }
125         c = NULL;
126  out:
127         return c;
128 }
129
130 #ifdef CONFIG_HUGETLB_PAGE
131 #error ARM Coherent DMA allocator does not (yet) support huge TLB
132 #endif
133
134 static void *
135 __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, int gfp,
136             pgprot_t prot)
137 {
138         struct page *page;
139         struct vm_region *c;
140         unsigned long order;
141         u64 mask = 0x00ffffff, limit; /* ISA default */
142
143         if (!consistent_pte) {
144                 printk(KERN_ERR "%s: not initialised\n", __func__);
145                 dump_stack();
146                 return NULL;
147         }
148
149         if (dev) {
150                 mask = dev->coherent_dma_mask;
151                 if (mask == 0) {
152                         dev_warn(dev, "coherent DMA mask is unset\n");
153                         return NULL;
154                 }
155         }
156
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",
161                        size, mask);
162                 *handle = ~0;
163                 return NULL;
164         }
165
166         order = get_order(size);
167
168         if (mask != 0xffffffff)
169                 gfp |= GFP_DMA;
170
171         page = alloc_pages(gfp, order);
172         if (!page)
173                 goto no_page;
174
175         /*
176          * Invalidate any data that might be lurking in the
177          * kernel direct-mapped region for device DMA.
178          */
179         {
180                 unsigned long kaddr = (unsigned long)page_address(page);
181                 memset(page_address(page), 0, size);
182                 dmac_flush_range(kaddr, kaddr + size);
183         }
184
185         /*
186          * Allocate a virtual address in the consistent mapping region.
187          */
188         c = vm_region_alloc(&consistent_head, size,
189                             gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
190         if (c) {
191                 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
192                 struct page *end = page + (1 << order);
193
194                 /*
195                  * Set the "dma handle"
196                  */
197                 *handle = page_to_bus(page);
198
199                 do {
200                         BUG_ON(!pte_none(*pte));
201
202                         set_page_count(page, 1);
203                         SetPageReserved(page);
204                         set_pte(pte, mk_pte(page, prot));
205                         page++;
206                         pte++;
207                 } while (size -= PAGE_SIZE);
208
209                 /*
210                  * Free the otherwise unused pages.
211                  */
212                 while (page < end) {
213                         set_page_count(page, 1);
214                         __free_page(page);
215                         page++;
216                 }
217
218                 return (void *)c->vm_start;
219         }
220
221         if (page)
222                 __free_pages(page, order);
223  no_page:
224         return NULL;
225 }
226
227 /*
228  * Allocate DMA-coherent memory space and return both the kernel remapped
229  * virtual and bus address for that space.
230  */
231 void *
232 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
233 {
234         return __dma_alloc(dev, size, handle, gfp,
235                            pgprot_noncached(pgprot_kernel));
236 }
237 EXPORT_SYMBOL(dma_alloc_coherent);
238
239 /*
240  * Allocate a writecombining region, in much the same way as
241  * dma_alloc_coherent above.
242  */
243 void *
244 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
245 {
246         return __dma_alloc(dev, size, handle, gfp,
247                            pgprot_writecombine(pgprot_kernel));
248 }
249 EXPORT_SYMBOL(dma_alloc_writecombine);
250
251 /*
252  * free a page as defined by the above mapping.
253  */
254 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
255 {
256         struct vm_region *c;
257         unsigned long flags;
258         pte_t *ptep;
259
260         size = PAGE_ALIGN(size);
261
262         spin_lock_irqsave(&consistent_lock, flags);
263
264         c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
265         if (!c)
266                 goto no_area;
267
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);
271                 dump_stack();
272                 size = c->vm_end - c->vm_start;
273         }
274
275         ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
276         do {
277                 pte_t pte = ptep_get_and_clear(ptep);
278                 unsigned long pfn;
279
280                 ptep++;
281
282                 if (!pte_none(pte) && pte_present(pte)) {
283                         pfn = pte_pfn(pte);
284
285                         if (pfn_valid(pfn)) {
286                                 struct page *page = pfn_to_page(pfn);
287                                 ClearPageReserved(page);
288
289                                 __free_page(page);
290                                 continue;
291                         }
292                 }
293
294                 printk(KERN_CRIT "%s: bad page in kernel page table\n",
295                        __func__);
296         } while (size -= PAGE_SIZE);
297
298         flush_tlb_kernel_range(c->vm_start, c->vm_end);
299
300         list_del(&c->vm_list);
301
302         spin_unlock_irqrestore(&consistent_lock, flags);
303
304         kfree(c);
305         return;
306
307  no_area:
308         spin_unlock_irqrestore(&consistent_lock, flags);
309         printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
310                __func__, cpu_addr);
311         dump_stack();
312 }
313 EXPORT_SYMBOL(dma_free_coherent);
314
315 /*
316  * Initialise the consistent memory allocation.
317  */
318 static int __init consistent_init(void)
319 {
320         pgd_t *pgd;
321         pmd_t *pmd;
322         pte_t *pte;
323         int ret = 0;
324
325         spin_lock(&init_mm.page_table_lock);
326
327         do {
328                 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
329                 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
330                 if (!pmd) {
331                         printk(KERN_ERR "%s: no pmd tables\n", __func__);
332                         ret = -ENOMEM;
333                         break;
334                 }
335                 WARN_ON(!pmd_none(*pmd));
336
337                 pte = pte_alloc_kernel(&init_mm, pmd, CONSISTENT_BASE);
338                 if (!pte) {
339                         printk(KERN_ERR "%s: no pte tables\n", __func__);
340                         ret = -ENOMEM;
341                         break;
342                 }
343
344                 consistent_pte = pte;
345         } while (0);
346
347         spin_unlock(&init_mm.page_table_lock);
348
349         return ret;
350 }
351
352 core_initcall(consistent_init);
353
354 /*
355  * Make an area consistent for devices.
356  */
357 void consistent_sync(void *vaddr, size_t size, int direction)
358 {
359         unsigned long start = (unsigned long)vaddr;
360         unsigned long end   = start + size;
361
362         switch (direction) {
363         case DMA_FROM_DEVICE:           /* invalidate only */
364                 dmac_inv_range(start, end);
365                 break;
366         case DMA_TO_DEVICE:             /* writeback only */
367                 dmac_clean_range(start, end);
368                 break;
369         case DMA_BIDIRECTIONAL:         /* writeback and invalidate */
370                 dmac_flush_range(start, end);
371                 break;
372         default:
373                 BUG();
374         }
375 }
376 EXPORT_SYMBOL(consistent_sync);