This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc / mm / cachemap.c
1 /*
2  *  PowerPC version derived from arch/arm/mm/consistent.c
3  *    Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
4  *
5  *  arch/ppc/mm/cachemap.c
6  *
7  *  Copyright (C) 2000 Russell King
8  *
9  * Consistent memory allocators.  Used for DMA devices that want to
10  * share uncached memory with the processor core.  The function return
11  * is the virtual address and 'dma_handle' is the physical address.
12  * Mostly stolen from the ARM port, with some changes for PowerPC.
13  *                                              -- Dan
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License version 2 as
17  * published by the Free Software Foundation.
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/ptrace.h>
29 #include <linux/mman.h>
30 #include <linux/mm.h>
31 #include <linux/swap.h>
32 #include <linux/stddef.h>
33 #include <linux/vmalloc.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/bootmem.h>
37 #include <linux/highmem.h>
38 #include <linux/dma-mapping.h>
39
40 #include <asm/pgalloc.h>
41 #include <asm/prom.h>
42 #include <asm/io.h>
43 #include <asm/hardirq.h>
44 #include <asm/mmu_context.h>
45 #include <asm/pgtable.h>
46 #include <asm/mmu.h>
47 #include <asm/uaccess.h>
48 #include <asm/smp.h>
49 #include <asm/machdep.h>
50
51 int map_page(unsigned long va, phys_addr_t pa, int flags);
52
53 /* This function will allocate the requested contiguous pages and
54  * map them into the kernel's vmalloc() space.  This is done so we
55  * get unique mapping for these pages, outside of the kernel's 1:1
56  * virtual:physical mapping.  This is necessary so we can cover large
57  * portions of the kernel with single large page TLB entries, and
58  * still get unique uncached pages for consistent DMA.
59  */
60 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
61 {
62         int order, err;
63         struct page *page, *free, *end;
64         phys_addr_t pa;
65         unsigned long flags, offset;
66         struct vm_struct *area = NULL;
67         unsigned long va = 0;
68
69         BUG_ON(in_interrupt());
70
71         /* Only allocate page size areas */
72         size = PAGE_ALIGN(size);
73         order = get_order(size);
74
75         free = page = alloc_pages(gfp, order);
76         if (! page)
77                 return NULL;
78
79         pa = page_to_phys(page);
80         *dma_handle = page_to_bus(page);
81         end = page + (1 << order);
82
83         /*
84          * we need to ensure that there are no cachelines in use,
85          * or worse dirty in this area.
86          */
87         invalidate_dcache_range((unsigned long)page_address(page),
88                                 (unsigned long)page_address(page) + size);
89
90         /*
91          * alloc_pages() expects the block to be handled as a unit, so
92          * it only sets the page count on the first page.  We set the
93          * counts on each page so they can be freed individually
94          */
95         for (; page < end; page++)
96                 set_page_count(page, 1);
97
98
99         /* Allocate some common virtual space to map the new pages*/
100         area = get_vm_area(size, VM_ALLOC);
101         if (! area)
102                 goto out;
103
104         va = (unsigned long) area->addr;
105
106         flags = _PAGE_KERNEL | _PAGE_NO_CACHE;
107         
108         for (offset = 0; offset < size; offset += PAGE_SIZE) {
109                 err = map_page(va+offset, pa+offset, flags);
110                 if (err) {
111                         vfree((void *)va);
112                         va = 0;
113                         goto out;
114                 }
115
116                 free++;
117         }
118
119  out:
120         /* Free pages which weren't mapped */
121         for (; free < end; free++) {
122                 __free_page(free);
123         }
124
125         return (void *)va;
126 }
127
128 /*
129  * free page(s) as defined by the above mapping.
130  */
131 void consistent_free(void *vaddr)
132 {
133         BUG_ON(in_interrupt());
134         vfree(vaddr);
135 }
136
137 /*
138  * make an area consistent.
139  */
140 void consistent_sync(void *vaddr, size_t size, int direction)
141 {
142         unsigned long start = (unsigned long)vaddr;
143         unsigned long end   = start + size;
144
145         switch (direction) {
146         case DMA_NONE:
147                 BUG();
148         case DMA_FROM_DEVICE:   /* invalidate only */
149                 invalidate_dcache_range(start, end);
150                 break;
151         case DMA_TO_DEVICE:             /* writeback only */
152                 clean_dcache_range(start, end);
153                 break;
154         case DMA_BIDIRECTIONAL: /* writeback and invalidate */
155                 flush_dcache_range(start, end);
156                 break;
157         }
158 }
159
160 /*
161  * consistent_sync_page make a page are consistent. identical
162  * to consistent_sync, but takes a struct page instead of a virtual address
163  */
164
165 void consistent_sync_page(struct page *page, unsigned long offset,
166         size_t size, int direction)
167 {
168         unsigned long start;
169
170         start = (unsigned long)page_address(page) + offset;
171         consistent_sync((void *)start, size, direction);
172 }
173
174 EXPORT_SYMBOL(consistent_sync_page);