more changes on original files
[linux-2.4.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  *  linux/arch/arm/mm/consistent.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  * Modified for 36-bit support.  -Matt
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/ptrace.h>
30 #include <linux/mman.h>
31 #include <linux/mm.h>
32 #include <linux/swap.h>
33 #include <linux/stddef.h>
34 #include <linux/vmalloc.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/bootmem.h>
38 #include <linux/highmem.h>
39 #include <linux/pci.h>
40
41 #include <asm/pgalloc.h>
42 #include <asm/prom.h>
43 #include <asm/io.h>
44 #include <asm/hardirq.h>
45 #include <asm/mmu_context.h>
46 #include <asm/pgtable.h>
47 #include <asm/mmu.h>
48 #include <asm/uaccess.h>
49 #include <asm/smp.h>
50 #include <asm/machdep.h>
51
52 #include "mmu_decl.h"
53
54 extern int map_page(unsigned long va, phys_addr_t pa, int flags);
55
56 /* This function will allocate the requested contiguous pages and
57  * map them into the kernel's vmalloc() space.  This is done so we
58  * get unique mapping for these pages, outside of the kernel's 1:1
59  * virtual:physical mapping.  This is necessary so we can cover large
60  * portions of the kernel with single large page TLB entries, and
61  * still get unique uncached pages for consistent DMA.
62  */
63 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
64 {
65         int order, err, i;
66         unsigned long page, va, flags;
67         phys_addr_t pa;
68         struct vm_struct *area;
69         void     *ret;
70
71         if (in_interrupt())
72                 BUG();
73
74         /* Only allocate page size areas.
75         */
76         size = PAGE_ALIGN(size);
77         order = get_order(size);
78
79         page = __get_free_pages(gfp, order);
80         if (!page) {
81                 BUG();
82                 return NULL;
83         }
84
85         /*
86          * we need to ensure that there are no cachelines in use,
87          * or worse dirty in this area.
88          */
89         invalidate_dcache_range(page, page + size);
90
91         /* Allocate some common virtual space to map the new pages.
92         */
93         area = get_vm_area(size, VM_ALLOC);
94         if (area == 0) {
95                 free_pages(page, order);
96                 return NULL;
97         }
98         va = VMALLOC_VMADDR(area->addr);
99         ret = (void *)va;
100
101         /* This gives us the real physical address of the first page.
102         */
103         *dma_handle = pa = virt_to_bus((void *)page);
104
105         flags = _PAGE_KERNEL | _PAGE_NO_CACHE;
106
107         /*
108          * Set refcount=1 on all pages in an order>0
109          * allocation so that vfree() will actually
110          * free all pages that were allocated. 
111          */
112         if (order > 0)
113         {
114                 struct page *rpage = virt_to_page(page);
115                 for (i = 1; i < (1 << order); i++)
116                         set_page_count(rpage+i, 1);
117         }
118
119         err = 0;
120         for (i = 0; i < size && err == 0; i += PAGE_SIZE)
121                 err = map_page(va+i, pa+i, flags);
122         
123         if (err) {
124                 vfree((void *)va);
125                 return NULL;
126         }
127
128         return ret;
129 }
130
131 /*
132  * free page(s) as defined by the above mapping.
133  */
134 void consistent_free(void *vaddr)
135 {
136         if (in_interrupt())
137                 BUG();
138         vfree(vaddr);
139 }
140
141 /*
142  * make an area consistent.
143  */
144 void consistent_sync(void *vaddr, size_t size, int direction)
145 {
146         unsigned long start = (unsigned long)vaddr;
147         unsigned long end   = start + size;
148
149         switch (direction) {
150         case PCI_DMA_NONE:
151                 BUG();
152         case PCI_DMA_FROMDEVICE:        /* invalidate only */
153                 invalidate_dcache_range(start, end);
154                 break;
155         case PCI_DMA_TODEVICE:          /* writeback only */
156                 clean_dcache_range(start, end);
157                 break;
158         case PCI_DMA_BIDIRECTIONAL:     /* writeback and invalidate */
159                 flush_dcache_range(start, end);
160                 break;
161         }
162 }
163
164 /*
165  * consistent_sync_page make a page are consistent. identical
166  * to consistent_sync, but takes a struct page instead of a virtual address
167  */
168
169 void consistent_sync_page(struct page *page, unsigned long offset,
170 size_t size, int direction)
171 {
172         void *start;
173
174         start = page_address(page) + offset; 
175         consistent_sync(start, size, direction);
176 }