vserver 1.9.5.x5
[linux-2.6.git] / arch / i386 / mm / ioremap.c
1 /*
2  * arch/i386/mm/ioremap.c
3  *
4  * Re-map IO memory to kernel address space so that we can access it.
5  * This is needed for high PCI addresses that aren't mapped in the
6  * 640k-1MB IO memory area on PC's
7  *
8  * (C) Copyright 1995 1996 Linus Torvalds
9  */
10
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/io.h>
15 #include <asm/fixmap.h>
16 #include <asm/cacheflush.h>
17 #include <asm/tlbflush.h>
18 #include <asm/pgtable.h>
19
20 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
21         unsigned long phys_addr, unsigned long flags)
22 {
23         unsigned long end;
24         unsigned long pfn;
25
26         address &= ~PMD_MASK;
27         end = address + size;
28         if (end > PMD_SIZE)
29                 end = PMD_SIZE;
30         if (address >= end)
31                 BUG();
32         pfn = phys_addr >> PAGE_SHIFT;
33         do {
34                 if (!pte_none(*pte)) {
35                         printk("remap_area_pte: page already exists\n");
36                         BUG();
37                 }
38                 set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW | 
39                                         _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
40                 address += PAGE_SIZE;
41                 pfn++;
42                 pte++;
43         } while (address && (address < end));
44 }
45
46 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
47         unsigned long phys_addr, unsigned long flags)
48 {
49         unsigned long end;
50
51         address &= ~PGDIR_MASK;
52         end = address + size;
53         if (end > PGDIR_SIZE)
54                 end = PGDIR_SIZE;
55         phys_addr -= address;
56         if (address >= end)
57                 BUG();
58         do {
59                 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
60                 if (!pte)
61                         return -ENOMEM;
62                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
63                 address = (address + PMD_SIZE) & PMD_MASK;
64                 pmd++;
65         } while (address && (address < end));
66         return 0;
67 }
68
69 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
70                                  unsigned long size, unsigned long flags)
71 {
72         int error;
73         pgd_t * dir;
74         unsigned long end = address + size;
75
76         phys_addr -= address;
77         dir = pgd_offset(&init_mm, address);
78         flush_cache_all();
79         if (address >= end)
80                 BUG();
81         spin_lock(&init_mm.page_table_lock);
82         do {
83                 pud_t *pud;
84                 pmd_t *pmd;
85                 
86                 error = -ENOMEM;
87                 pud = pud_alloc(&init_mm, dir, address);
88                 if (!pud)
89                         break;
90                 pmd = pmd_alloc(&init_mm, pud, address);
91                 if (!pmd)
92                         break;
93                 if (remap_area_pmd(pmd, address, end - address,
94                                          phys_addr + address, flags))
95                         break;
96                 error = 0;
97                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
98                 dir++;
99         } while (address && (address < end));
100         spin_unlock(&init_mm.page_table_lock);
101         flush_tlb_all();
102         return error;
103 }
104
105 /*
106  * Generic mapping function (not visible outside):
107  */
108
109 /*
110  * Remap an arbitrary physical address space into the kernel virtual
111  * address space. Needed when the kernel wants to access high addresses
112  * directly.
113  *
114  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
115  * have to convert them into an offset in a page-aligned mapping, but the
116  * caller shouldn't need to know that small detail.
117  */
118 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
119 {
120         void __iomem * addr;
121         struct vm_struct * area;
122         unsigned long offset, last_addr;
123
124         /* Don't allow wraparound or zero size */
125         last_addr = phys_addr + size - 1;
126         if (!size || last_addr < phys_addr)
127                 return NULL;
128
129         /*
130          * Don't remap the low PCI/ISA area, it's always mapped..
131          */
132         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
133                 return (void __iomem *) phys_to_virt(phys_addr);
134
135         /*
136          * Don't allow anybody to remap normal RAM that we're using..
137          */
138         if (phys_addr <= virt_to_phys(high_memory - 1)) {
139                 char *t_addr, *t_end;
140                 struct page *page;
141
142                 t_addr = __va(phys_addr);
143                 t_end = t_addr + (size - 1);
144            
145                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
146                         if(!PageReserved(page))
147                                 return NULL;
148         }
149
150         /*
151          * Mappings have to be page-aligned
152          */
153         offset = phys_addr & ~PAGE_MASK;
154         phys_addr &= PAGE_MASK;
155         size = PAGE_ALIGN(last_addr+1) - phys_addr;
156
157         /*
158          * Ok, go for it..
159          */
160         area = get_vm_area(size, VM_IOREMAP | (flags << 20));
161         if (!area)
162                 return NULL;
163         area->phys_addr = phys_addr;
164         addr = (void __iomem *) area->addr;
165         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
166                 vunmap((void __force *) addr);
167                 return NULL;
168         }
169         return (void __iomem *) (offset + (char __iomem *)addr);
170 }
171
172
173 /**
174  * ioremap_nocache     -   map bus memory into CPU space
175  * @offset:    bus address of the memory
176  * @size:      size of the resource to map
177  *
178  * ioremap_nocache performs a platform specific sequence of operations to
179  * make bus memory CPU accessible via the readb/readw/readl/writeb/
180  * writew/writel functions and the other mmio helpers. The returned
181  * address is not guaranteed to be usable directly as a virtual
182  * address. 
183  *
184  * This version of ioremap ensures that the memory is marked uncachable
185  * on the CPU as well as honouring existing caching rules from things like
186  * the PCI bus. Note that there are other caches and buffers on many 
187  * busses. In particular driver authors should read up on PCI writes
188  *
189  * It's useful if some control registers are in such an area and
190  * write combining or read caching is not desirable:
191  * 
192  * Must be freed with iounmap.
193  */
194
195 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
196 {
197         unsigned long last_addr;
198         void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
199         if (!p) 
200                 return p; 
201
202         /* Guaranteed to be > phys_addr, as per __ioremap() */
203         last_addr = phys_addr + size - 1;
204
205         if (last_addr < virt_to_phys(high_memory) - 1) {
206                 struct page *ppage = virt_to_page(__va(phys_addr));             
207                 unsigned long npages;
208
209                 phys_addr &= PAGE_MASK;
210
211                 /* This might overflow and become zero.. */
212                 last_addr = PAGE_ALIGN(last_addr);
213
214                 /* .. but that's ok, because modulo-2**n arithmetic will make
215                 * the page-aligned "last - first" come out right.
216                 */
217                 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
218
219                 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) { 
220                         iounmap(p); 
221                         p = NULL;
222                 }
223                 global_flush_tlb();
224         }
225
226         return p;                                       
227 }
228
229 void iounmap(volatile void __iomem *addr)
230 {
231         struct vm_struct *p;
232         if ((void __force *) addr <= high_memory) 
233                 return; 
234         p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
235         if (!p) { 
236                 printk("__iounmap: bad address %p\n", addr);
237                 return;
238         }
239
240         if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
241                 /* p->size includes the guard page, but cpa doesn't like that */
242                 change_page_attr(virt_to_page(__va(p->phys_addr)),
243                                  (p->size - PAGE_SIZE) >> PAGE_SHIFT,
244                                  PAGE_KERNEL);                           
245                 global_flush_tlb();
246         } 
247         kfree(p); 
248 }
249
250 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
251 {
252         unsigned long offset, last_addr;
253         unsigned int nrpages;
254         enum fixed_addresses idx;
255
256         /* Don't allow wraparound or zero size */
257         last_addr = phys_addr + size - 1;
258         if (!size || last_addr < phys_addr)
259                 return NULL;
260
261         /*
262          * Don't remap the low PCI/ISA area, it's always mapped..
263          */
264         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
265                 return phys_to_virt(phys_addr);
266
267         /*
268          * Mappings have to be page-aligned
269          */
270         offset = phys_addr & ~PAGE_MASK;
271         phys_addr &= PAGE_MASK;
272         size = PAGE_ALIGN(last_addr) - phys_addr;
273
274         /*
275          * Mappings have to fit in the FIX_BTMAP area.
276          */
277         nrpages = size >> PAGE_SHIFT;
278         if (nrpages > NR_FIX_BTMAPS)
279                 return NULL;
280
281         /*
282          * Ok, go for it..
283          */
284         idx = FIX_BTMAP_BEGIN;
285         while (nrpages > 0) {
286                 set_fixmap(idx, phys_addr);
287                 phys_addr += PAGE_SIZE;
288                 --idx;
289                 --nrpages;
290         }
291         return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
292 }
293
294 void __init bt_iounmap(void *addr, unsigned long size)
295 {
296         unsigned long virt_addr;
297         unsigned long offset;
298         unsigned int nrpages;
299         enum fixed_addresses idx;
300
301         virt_addr = (unsigned long)addr;
302         if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
303                 return;
304         offset = virt_addr & ~PAGE_MASK;
305         nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
306
307         idx = FIX_BTMAP_BEGIN;
308         while (nrpages > 0) {
309                 clear_fixmap(idx);
310                 --idx;
311                 --nrpages;
312         }
313 }