aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ppc/kernel/dma-mapping.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/ppc/kernel/dma-mapping.c')
-rw-r--r--arch/ppc/kernel/dma-mapping.c442
1 files changed, 0 insertions, 442 deletions
diff --git a/arch/ppc/kernel/dma-mapping.c b/arch/ppc/kernel/dma-mapping.c
deleted file mode 100644
index 10fec7363962..000000000000
--- a/arch/ppc/kernel/dma-mapping.c
+++ /dev/null
@@ -1,442 +0,0 @@
1/*
2 * PowerPC version derived from arch/arm/mm/consistent.c
3 * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
4 *
5 * Copyright (C) 2000 Russell King
6 *
7 * Consistent memory allocators. Used for DMA devices that want to
8 * share uncached memory with the processor core. The function return
9 * is the virtual address and 'dma_handle' is the physical address.
10 * Mostly stolen from the ARM port, with some changes for PowerPC.
11 * -- Dan
12 *
13 * Reorganized to get rid of the arch-specific consistent_* functions
14 * and provide non-coherent implementations for the DMA API. -Matt
15 *
16 * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()
17 * implementation. This is pulled straight from ARM and barely
18 * modified. -Matt
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
23 */
24
25#include <linux/module.h>
26#include <linux/signal.h>
27#include <linux/sched.h>
28#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/string.h>
31#include <linux/types.h>
32#include <linux/ptrace.h>
33#include <linux/mman.h>
34#include <linux/mm.h>
35#include <linux/swap.h>
36#include <linux/stddef.h>
37#include <linux/vmalloc.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/bootmem.h>
41#include <linux/highmem.h>
42#include <linux/dma-mapping.h>
43#include <linux/hardirq.h>
44
45#include <asm/pgalloc.h>
46#include <asm/prom.h>
47#include <asm/io.h>
48#include <asm/mmu_context.h>
49#include <asm/pgtable.h>
50#include <asm/mmu.h>
51#include <asm/uaccess.h>
52#include <asm/smp.h>
53#include <asm/machdep.h>
54
55int map_page(unsigned long va, phys_addr_t pa, int flags);
56
57#include <asm/tlbflush.h>
58
59/*
60 * This address range defaults to a value that is safe for all
61 * platforms which currently set CONFIG_NOT_COHERENT_CACHE. It
62 * can be further configured for specific applications under
63 * the "Advanced Setup" menu. -Matt
64 */
65#define CONSISTENT_BASE (CONFIG_CONSISTENT_START)
66#define CONSISTENT_END (CONFIG_CONSISTENT_START + CONFIG_CONSISTENT_SIZE)
67#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
68
69/*
70 * This is the page table (2MB) covering uncached, DMA consistent allocations
71 */
72static pte_t *consistent_pte;
73static DEFINE_SPINLOCK(consistent_lock);
74
75/*
76 * VM region handling support.
77 *
78 * This should become something generic, handling VM region allocations for
79 * vmalloc and similar (ioremap, module space, etc).
80 *
81 * I envisage vmalloc()'s supporting vm_struct becoming:
82 *
83 * struct vm_struct {
84 * struct vm_region region;
85 * unsigned long flags;
86 * struct page **pages;
87 * unsigned int nr_pages;
88 * unsigned long phys_addr;
89 * };
90 *
91 * get_vm_area() would then call vm_region_alloc with an appropriate
92 * struct vm_region head (eg):
93 *
94 * struct vm_region vmalloc_head = {
95 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
96 * .vm_start = VMALLOC_START,
97 * .vm_end = VMALLOC_END,
98 * };
99 *
100 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
101 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
102 * would have to initialise this each time prior to calling vm_region_alloc().
103 */
104struct vm_region {
105 struct list_head vm_list;
106 unsigned long vm_start;
107 unsigned long vm_end;
108};
109
110static struct vm_region consistent_head = {
111 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
112 .vm_start = CONSISTENT_BASE,
113 .vm_end = CONSISTENT_END,
114};
115
116static struct vm_region *
117vm_region_alloc(struct vm_region *head, size_t size, gfp_t gfp)
118{
119 unsigned long addr = head->vm_start, end = head->vm_end - size;
120 unsigned long flags;
121 struct vm_region *c, *new;
122
123 new = kmalloc(sizeof(struct vm_region), gfp);
124 if (!new)
125 goto out;
126
127 spin_lock_irqsave(&consistent_lock, flags);
128
129 list_for_each_entry(c, &head->vm_list, vm_list) {
130 if ((addr + size) < addr)
131 goto nospc;
132 if ((addr + size) <= c->vm_start)
133 goto found;
134 addr = c->vm_end;
135 if (addr > end)
136 goto nospc;
137 }
138
139 found:
140 /*
141 * Insert this entry _before_ the one we found.
142 */
143 list_add_tail(&new->vm_list, &c->vm_list);
144 new->vm_start = addr;
145 new->vm_end = addr + size;
146
147 spin_unlock_irqrestore(&consistent_lock, flags);
148 return new;
149
150 nospc:
151 spin_unlock_irqrestore(&consistent_lock, flags);
152 kfree(new);
153 out:
154 return NULL;
155}
156
157static struct vm_region *vm_region_find(struct vm_region *head, unsigned long addr)
158{
159 struct vm_region *c;
160
161 list_for_each_entry(c, &head->vm_list, vm_list) {
162 if (c->vm_start == addr)
163 goto out;
164 }
165 c = NULL;
166 out:
167 return c;
168}
169
170/*
171 * Allocate DMA-coherent memory space and return both the kernel remapped
172 * virtual and bus address for that space.
173 */
174void *
175__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp)
176{
177 struct page *page;
178 struct vm_region *c;
179 unsigned long order;
180 u64 mask = 0x00ffffff, limit; /* ISA default */
181
182 if (!consistent_pte) {
183 printk(KERN_ERR "%s: not initialised\n", __func__);
184 dump_stack();
185 return NULL;
186 }
187
188 size = PAGE_ALIGN(size);
189 limit = (mask + 1) & ~mask;
190 if ((limit && size >= limit) || size >= (CONSISTENT_END - CONSISTENT_BASE)) {
191 printk(KERN_WARNING "coherent allocation too big (requested %#x mask %#Lx)\n",
192 size, mask);
193 return NULL;
194 }
195
196 order = get_order(size);
197
198 if (mask != 0xffffffff)
199 gfp |= GFP_DMA;
200
201 page = alloc_pages(gfp, order);
202 if (!page)
203 goto no_page;
204
205 /*
206 * Invalidate any data that might be lurking in the
207 * kernel direct-mapped region for device DMA.
208 */
209 {
210 unsigned long kaddr = (unsigned long)page_address(page);
211 memset(page_address(page), 0, size);
212 flush_dcache_range(kaddr, kaddr + size);
213 }
214
215 /*
216 * Allocate a virtual address in the consistent mapping region.
217 */
218 c = vm_region_alloc(&consistent_head, size,
219 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
220 if (c) {
221 unsigned long vaddr = c->vm_start;
222 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(vaddr);
223 struct page *end = page + (1 << order);
224
225 split_page(page, order);
226
227 /*
228 * Set the "dma handle"
229 */
230 *handle = page_to_bus(page);
231
232 do {
233 BUG_ON(!pte_none(*pte));
234
235 SetPageReserved(page);
236 set_pte_at(&init_mm, vaddr,
237 pte, mk_pte(page, pgprot_noncached(PAGE_KERNEL)));
238 page++;
239 pte++;
240 vaddr += PAGE_SIZE;
241 } while (size -= PAGE_SIZE);
242
243 /*
244 * Free the otherwise unused pages.
245 */
246 while (page < end) {
247 __free_page(page);
248 page++;
249 }
250
251 return (void *)c->vm_start;
252 }
253
254 if (page)
255 __free_pages(page, order);
256 no_page:
257 return NULL;
258}
259EXPORT_SYMBOL(__dma_alloc_coherent);
260
261/*
262 * free a page as defined by the above mapping.
263 */
264void __dma_free_coherent(size_t size, void *vaddr)
265{
266 struct vm_region *c;
267 unsigned long flags, addr;
268 pte_t *ptep;
269
270 size = PAGE_ALIGN(size);
271
272 spin_lock_irqsave(&consistent_lock, flags);
273
274 c = vm_region_find(&consistent_head, (unsigned long)vaddr);
275 if (!c)
276 goto no_area;
277
278 if ((c->vm_end - c->vm_start) != size) {
279 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
280 __func__, c->vm_end - c->vm_start, size);
281 dump_stack();
282 size = c->vm_end - c->vm_start;
283 }
284
285 ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
286 addr = c->vm_start;
287 do {
288 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
289 unsigned long pfn;
290
291 ptep++;
292 addr += PAGE_SIZE;
293
294 if (!pte_none(pte) && pte_present(pte)) {
295 pfn = pte_pfn(pte);
296
297 if (pfn_valid(pfn)) {
298 struct page *page = pfn_to_page(pfn);
299 ClearPageReserved(page);
300
301 __free_page(page);
302 continue;
303 }
304 }
305
306 printk(KERN_CRIT "%s: bad page in kernel page table\n",
307 __func__);
308 } while (size -= PAGE_SIZE);
309
310 flush_tlb_kernel_range(c->vm_start, c->vm_end);
311
312 list_del(&c->vm_list);
313
314 spin_unlock_irqrestore(&consistent_lock, flags);
315
316 kfree(c);
317 return;
318
319 no_area:
320 spin_unlock_irqrestore(&consistent_lock, flags);
321 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
322 __func__, vaddr);
323 dump_stack();
324}
325EXPORT_SYMBOL(__dma_free_coherent);
326
327/*
328 * Initialise the consistent memory allocation.
329 */
330static int __init dma_alloc_init(void)
331{
332 pgd_t *pgd;
333 pmd_t *pmd;
334 pte_t *pte;
335 int ret = 0;
336
337 do {
338 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
339 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
340 if (!pmd) {
341 printk(KERN_ERR "%s: no pmd tables\n", __func__);
342 ret = -ENOMEM;
343 break;
344 }
345 WARN_ON(!pmd_none(*pmd));
346
347 pte = pte_alloc_kernel(pmd, CONSISTENT_BASE);
348 if (!pte) {
349 printk(KERN_ERR "%s: no pte tables\n", __func__);
350 ret = -ENOMEM;
351 break;
352 }
353
354 consistent_pte = pte;
355 } while (0);
356
357 return ret;
358}
359
360core_initcall(dma_alloc_init);
361
362/*
363 * make an area consistent.
364 */
365void __dma_sync(void *vaddr, size_t size, int direction)
366{
367 unsigned long start = (unsigned long)vaddr;
368 unsigned long end = start + size;
369
370 switch (direction) {
371 case DMA_NONE:
372 BUG();
373 case DMA_FROM_DEVICE: /* invalidate only */
374 invalidate_dcache_range(start, end);
375 break;
376 case DMA_TO_DEVICE: /* writeback only */
377 clean_dcache_range(start, end);
378 break;
379 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
380 flush_dcache_range(start, end);
381 break;
382 }
383}
384EXPORT_SYMBOL(__dma_sync);
385
386#ifdef CONFIG_HIGHMEM
387/*
388 * __dma_sync_page() implementation for systems using highmem.
389 * In this case, each page of a buffer must be kmapped/kunmapped
390 * in order to have a virtual address for __dma_sync(). This must
391 * not sleep so kmap_atomic()/kunmap_atomic() are used.
392 *
393 * Note: yes, it is possible and correct to have a buffer extend
394 * beyond the first page.
395 */
396static inline void __dma_sync_page_highmem(struct page *page,
397 unsigned long offset, size_t size, int direction)
398{
399 size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
400 size_t cur_size = seg_size;
401 unsigned long flags, start, seg_offset = offset;
402 int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
403 int seg_nr = 0;
404
405 local_irq_save(flags);
406
407 do {
408 start = (unsigned long)kmap_atomic(page + seg_nr,
409 KM_PPC_SYNC_PAGE) + seg_offset;
410
411 /* Sync this buffer segment */
412 __dma_sync((void *)start, seg_size, direction);
413 kunmap_atomic((void *)start, KM_PPC_SYNC_PAGE);
414 seg_nr++;
415
416 /* Calculate next buffer segment size */
417 seg_size = min((size_t)PAGE_SIZE, size - cur_size);
418
419 /* Add the segment size to our running total */
420 cur_size += seg_size;
421 seg_offset = 0;
422 } while (seg_nr < nr_segs);
423
424 local_irq_restore(flags);
425}
426#endif /* CONFIG_HIGHMEM */
427
428/*
429 * __dma_sync_page makes memory consistent. identical to __dma_sync, but
430 * takes a struct page instead of a virtual address
431 */
432void __dma_sync_page(struct page *page, unsigned long offset,
433 size_t size, int direction)
434{
435#ifdef CONFIG_HIGHMEM
436 __dma_sync_page_highmem(page, offset, size, direction);
437#else
438 unsigned long start = (unsigned long)page_address(page) + offset;
439 __dma_sync((void *)start, size, direction);
440#endif
441}
442EXPORT_SYMBOL(__dma_sync_page);