VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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                 pmd_t *pmd;
84                 pmd = pmd_alloc(&init_mm, dir, address);
85                 error = -ENOMEM;
86                 if (!pmd)
87                         break;
88                 if (remap_area_pmd(pmd, address, end - address,
89                                          phys_addr + address, flags))
90                         break;
91                 error = 0;
92                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
93                 dir++;
94         } while (address && (address < end));
95         spin_unlock(&init_mm.page_table_lock);
96         flush_tlb_all();
97         return error;
98 }
99
100 /*
101  * Generic mapping function (not visible outside):
102  */
103
104 /*
105  * Remap an arbitrary physical address space into the kernel virtual
106  * address space. Needed when the kernel wants to access high addresses
107  * directly.
108  *
109  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
110  * have to convert them into an offset in a page-aligned mapping, but the
111  * caller shouldn't need to know that small detail.
112  */
113 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
114 {
115         void * addr;
116         struct vm_struct * area;
117         unsigned long offset, last_addr;
118
119         /* Don't allow wraparound or zero size */
120         last_addr = phys_addr + size - 1;
121         if (!size || last_addr < phys_addr)
122                 return NULL;
123
124         /*
125          * Don't remap the low PCI/ISA area, it's always mapped..
126          */
127         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
128                 return phys_to_virt(phys_addr);
129
130         /*
131          * Don't allow anybody to remap normal RAM that we're using..
132          */
133         if (phys_addr < virt_to_phys(high_memory)) {
134                 char *t_addr, *t_end;
135                 struct page *page;
136
137                 t_addr = __va(phys_addr);
138                 t_end = t_addr + (size - 1);
139            
140                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
141                         if(!PageReserved(page))
142                                 return NULL;
143         }
144
145         /*
146          * Mappings have to be page-aligned
147          */
148         offset = phys_addr & ~PAGE_MASK;
149         phys_addr &= PAGE_MASK;
150         size = PAGE_ALIGN(last_addr+1) - phys_addr;
151
152         /*
153          * Ok, go for it..
154          */
155         area = get_vm_area(size, VM_IOREMAP);
156         if (!area)
157                 return NULL;
158         area->phys_addr = phys_addr;
159         addr = area->addr;
160         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
161                 vunmap(addr);
162                 return NULL;
163         }
164         return (void *) (offset + (char *)addr);
165 }
166
167
168 /**
169  * ioremap_nocache     -   map bus memory into CPU space
170  * @offset:    bus address of the memory
171  * @size:      size of the resource to map
172  *
173  * ioremap_nocache performs a platform specific sequence of operations to
174  * make bus memory CPU accessible via the readb/readw/readl/writeb/
175  * writew/writel functions and the other mmio helpers. The returned
176  * address is not guaranteed to be usable directly as a virtual
177  * address. 
178  *
179  * This version of ioremap ensures that the memory is marked uncachable
180  * on the CPU as well as honouring existing caching rules from things like
181  * the PCI bus. Note that there are other caches and buffers on many 
182  * busses. In particular driver authors should read up on PCI writes
183  *
184  * It's useful if some control registers are in such an area and
185  * write combining or read caching is not desirable:
186  * 
187  * Must be freed with iounmap.
188  */
189
190 void *ioremap_nocache (unsigned long phys_addr, unsigned long size)
191 {
192         unsigned long last_addr;
193         void *p = __ioremap(phys_addr, size, _PAGE_PCD);
194         if (!p) 
195                 return p; 
196
197         /* Guaranteed to be > phys_addr, as per __ioremap() */
198         last_addr = phys_addr + size - 1;
199
200         if (last_addr < virt_to_phys(high_memory)) { 
201                 struct page *ppage = virt_to_page(__va(phys_addr));             
202                 unsigned long npages;
203
204                 phys_addr &= PAGE_MASK;
205
206                 /* This might overflow and become zero.. */
207                 last_addr = PAGE_ALIGN(last_addr);
208
209                 /* .. but that's ok, because modulo-2**n arithmetic will make
210                 * the page-aligned "last - first" come out right.
211                 */
212                 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
213
214                 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) { 
215                         iounmap(p); 
216                         p = NULL;
217                 }
218                 global_flush_tlb();
219         }
220
221         return p;                                       
222 }
223
224 void iounmap(void *addr)
225 {
226         struct vm_struct *p;
227         if (addr <= high_memory) 
228                 return; 
229         p = remove_vm_area((void *) (PAGE_MASK & (unsigned long) addr));
230         if (!p) { 
231                 printk("__iounmap: bad address %p\n", addr);
232                 return;
233         } 
234
235         if (p->flags && p->phys_addr < virt_to_phys(high_memory)) { 
236                 change_page_attr(virt_to_page(__va(p->phys_addr)),
237                                  p->size >> PAGE_SHIFT,
238                                  PAGE_KERNEL);                           
239                 global_flush_tlb();
240         } 
241         kfree(p); 
242 }
243
244 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
245 {
246         unsigned long offset, last_addr;
247         unsigned int nrpages;
248         enum fixed_addresses idx;
249
250         /* Don't allow wraparound or zero size */
251         last_addr = phys_addr + size - 1;
252         if (!size || last_addr < phys_addr)
253                 return NULL;
254
255         /*
256          * Don't remap the low PCI/ISA area, it's always mapped..
257          */
258         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
259                 return phys_to_virt(phys_addr);
260
261         /*
262          * Mappings have to be page-aligned
263          */
264         offset = phys_addr & ~PAGE_MASK;
265         phys_addr &= PAGE_MASK;
266         size = PAGE_ALIGN(last_addr) - phys_addr;
267
268         /*
269          * Mappings have to fit in the FIX_BTMAP area.
270          */
271         nrpages = size >> PAGE_SHIFT;
272         if (nrpages > NR_FIX_BTMAPS)
273                 return NULL;
274
275         /*
276          * Ok, go for it..
277          */
278         idx = FIX_BTMAP_BEGIN;
279         while (nrpages > 0) {
280                 set_fixmap(idx, phys_addr);
281                 phys_addr += PAGE_SIZE;
282                 --idx;
283                 --nrpages;
284         }
285         return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
286 }
287
288 void __init bt_iounmap(void *addr, unsigned long size)
289 {
290         unsigned long virt_addr;
291         unsigned long offset;
292         unsigned int nrpages;
293         enum fixed_addresses idx;
294
295         virt_addr = (unsigned long)addr;
296         if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
297                 return;
298         offset = virt_addr & ~PAGE_MASK;
299         nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
300
301         idx = FIX_BTMAP_BEGIN;
302         while (nrpages > 0) {
303                 clear_fixmap(idx);
304                 --idx;
305                 --nrpages;
306         }
307 }