vserver 1.9.3
[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 = ISA_DMA_THRESHOLD, limit;
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
152                 /*
153                  * Sanity check the DMA mask - it must be non-zero, and
154                  * must be able to be satisfied by a DMA allocation.
155                  */
156                 if (mask == 0) {
157                         dev_warn(dev, "coherent DMA mask is unset\n");
158                         goto no_page;
159                 }
160
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);
165                         goto no_page;
166                 }
167         }
168
169         /*
170          * Sanity check the allocation size.
171          */
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);
178                 goto no_page;
179         }
180
181         order = get_order(size);
182
183         if (mask != 0xffffffff)
184                 gfp |= GFP_DMA;
185
186         page = alloc_pages(gfp, order);
187         if (!page)
188                 goto no_page;
189
190         /*
191          * Invalidate any data that might be lurking in the
192          * kernel direct-mapped region for device DMA.
193          */
194         {
195                 unsigned long kaddr = (unsigned long)page_address(page);
196                 memset(page_address(page), 0, size);
197                 dmac_flush_range(kaddr, kaddr + size);
198         }
199
200         /*
201          * Allocate a virtual address in the consistent mapping region.
202          */
203         c = vm_region_alloc(&consistent_head, size,
204                             gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
205         if (c) {
206                 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
207                 struct page *end = page + (1 << order);
208
209                 /*
210                  * Set the "dma handle"
211                  */
212                 *handle = page_to_dma(dev, page);
213
214                 do {
215                         BUG_ON(!pte_none(*pte));
216
217                         set_page_count(page, 1);
218                         SetPageReserved(page);
219                         set_pte(pte, mk_pte(page, prot));
220                         page++;
221                         pte++;
222                 } while (size -= PAGE_SIZE);
223
224                 /*
225                  * Free the otherwise unused pages.
226                  */
227                 while (page < end) {
228                         set_page_count(page, 1);
229                         __free_page(page);
230                         page++;
231                 }
232
233                 return (void *)c->vm_start;
234         }
235
236         if (page)
237                 __free_pages(page, order);
238  no_page:
239         *handle = ~0;
240         return NULL;
241 }
242
243 /*
244  * Allocate DMA-coherent memory space and return both the kernel remapped
245  * virtual and bus address for that space.
246  */
247 void *
248 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
249 {
250         return __dma_alloc(dev, size, handle, gfp,
251                            pgprot_noncached(pgprot_kernel));
252 }
253 EXPORT_SYMBOL(dma_alloc_coherent);
254
255 /*
256  * Allocate a writecombining region, in much the same way as
257  * dma_alloc_coherent above.
258  */
259 void *
260 dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, int gfp)
261 {
262         return __dma_alloc(dev, size, handle, gfp,
263                            pgprot_writecombine(pgprot_kernel));
264 }
265 EXPORT_SYMBOL(dma_alloc_writecombine);
266
267 /*
268  * free a page as defined by the above mapping.
269  */
270 void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
271 {
272         struct vm_region *c;
273         unsigned long flags;
274         pte_t *ptep;
275
276         size = PAGE_ALIGN(size);
277
278         spin_lock_irqsave(&consistent_lock, flags);
279
280         c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
281         if (!c)
282                 goto no_area;
283
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);
287                 dump_stack();
288                 size = c->vm_end - c->vm_start;
289         }
290
291         ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
292         do {
293                 pte_t pte = ptep_get_and_clear(ptep);
294                 unsigned long pfn;
295
296                 ptep++;
297
298                 if (!pte_none(pte) && pte_present(pte)) {
299                         pfn = pte_pfn(pte);
300
301                         if (pfn_valid(pfn)) {
302                                 struct page *page = pfn_to_page(pfn);
303                                 ClearPageReserved(page);
304
305                                 __free_page(page);
306                                 continue;
307                         }
308                 }
309
310                 printk(KERN_CRIT "%s: bad page in kernel page table\n",
311                        __func__);
312         } while (size -= PAGE_SIZE);
313
314         flush_tlb_kernel_range(c->vm_start, c->vm_end);
315
316         list_del(&c->vm_list);
317
318         spin_unlock_irqrestore(&consistent_lock, flags);
319
320         kfree(c);
321         return;
322
323  no_area:
324         spin_unlock_irqrestore(&consistent_lock, flags);
325         printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
326                __func__, cpu_addr);
327         dump_stack();
328 }
329 EXPORT_SYMBOL(dma_free_coherent);
330
331 /*
332  * Initialise the consistent memory allocation.
333  */
334 static int __init consistent_init(void)
335 {
336         pgd_t *pgd;
337         pmd_t *pmd;
338         pte_t *pte;
339         int ret = 0;
340
341         spin_lock(&init_mm.page_table_lock);
342
343         do {
344                 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
345                 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
346                 if (!pmd) {
347                         printk(KERN_ERR "%s: no pmd tables\n", __func__);
348                         ret = -ENOMEM;
349                         break;
350                 }
351                 WARN_ON(!pmd_none(*pmd));
352
353                 pte = pte_alloc_kernel(&init_mm, pmd, CONSISTENT_BASE);
354                 if (!pte) {
355                         printk(KERN_ERR "%s: no pte tables\n", __func__);
356                         ret = -ENOMEM;
357                         break;
358                 }
359
360                 consistent_pte = pte;
361         } while (0);
362
363         spin_unlock(&init_mm.page_table_lock);
364
365         return ret;
366 }
367
368 core_initcall(consistent_init);
369
370 /*
371  * Make an area consistent for devices.
372  */
373 void consistent_sync(void *vaddr, size_t size, int direction)
374 {
375         unsigned long start = (unsigned long)vaddr;
376         unsigned long end   = start + size;
377
378         switch (direction) {
379         case DMA_FROM_DEVICE:           /* invalidate only */
380                 dmac_inv_range(start, end);
381                 break;
382         case DMA_TO_DEVICE:             /* writeback only */
383                 dmac_clean_range(start, end);
384                 break;
385         case DMA_BIDIRECTIONAL:         /* writeback and invalidate */
386                 dmac_flush_range(start, end);
387                 break;
388         default:
389                 BUG();
390         }
391 }
392 EXPORT_SYMBOL(consistent_sync);