911262d8ca1991737cb884eb6427160e5008c2f1
[linux-2.6.git] / arch / x86_64 / mm / ioremap.c
1 /*
2  * arch/x86_64/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/pgalloc.h>
16 #include <asm/fixmap.h>
17 #include <asm/cacheflush.h>
18 #include <asm/tlbflush.h>
19 #include <asm/proto.h>
20
21 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
22         unsigned long phys_addr, unsigned long flags)
23 {
24         unsigned long end;
25         unsigned long pfn;
26
27         address &= ~PMD_MASK;
28         end = address + size;
29         if (end > PMD_SIZE)
30                 end = PMD_SIZE;
31         if (address >= end)
32                 BUG();
33         pfn = phys_addr >> PAGE_SHIFT;
34         do {
35                 if (!pte_none(*pte)) {
36                         printk("remap_area_pte: page already exists\n");
37                         BUG();
38                 }
39                 set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW | 
40                                         _PAGE_GLOBAL | _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
41                 address += PAGE_SIZE;
42                 pfn++;
43                 pte++;
44         } while (address && (address < end));
45 }
46
47 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
48         unsigned long phys_addr, unsigned long flags)
49 {
50         unsigned long end;
51
52         address &= ~PUD_MASK;
53         end = address + size;
54         if (end > PUD_SIZE)
55                 end = PUD_SIZE;
56         phys_addr -= address;
57         if (address >= end)
58                 BUG();
59         do {
60                 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
61                 if (!pte)
62                         return -ENOMEM;
63                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
64                 address = (address + PMD_SIZE) & PMD_MASK;
65                 pmd++;
66         } while (address && (address < end));
67         return 0;
68 }
69
70 static inline int remap_area_pud(pud_t * pud, unsigned long address, unsigned long size,
71         unsigned long phys_addr, unsigned long flags)
72 {
73         unsigned long end;
74
75         address &= ~PGDIR_MASK;
76         end = address + size;
77         if (end > PGDIR_SIZE)
78                 end = PGDIR_SIZE;
79         phys_addr -= address;
80         if (address >= end)
81                 BUG();
82         do {
83                 pmd_t * pmd = pmd_alloc(&init_mm, pud, address);
84                 if (!pmd)
85                         return -ENOMEM;
86                 remap_area_pmd(pmd, address, end - address, address + phys_addr, flags);
87                 address = (address + PUD_SIZE) & PUD_MASK;
88                 pud++;
89         } while (address && (address < end));
90         return 0;
91 }
92
93 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
94                                  unsigned long size, unsigned long flags)
95 {
96         int error;
97         pgd_t *pgd;
98         unsigned long end = address + size;
99
100         phys_addr -= address;
101         pgd = pgd_offset_k(address);
102         flush_cache_all();
103         if (address >= end)
104                 BUG();
105         spin_lock(&init_mm.page_table_lock);
106         do {
107                 pud_t *pud;
108                 pud = pud_alloc(&init_mm, pgd, address);
109                 error = -ENOMEM;
110                 if (!pud)
111                         break;
112                 if (remap_area_pud(pud, address, end - address,
113                                          phys_addr + address, flags))
114                         break;
115                 error = 0;
116                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
117                 pgd++;
118         } while (address && (address < end));
119         spin_unlock(&init_mm.page_table_lock);
120         flush_tlb_all();
121         return error;
122 }
123
124 /*
125  * Fix up the linear direct mapping of the kernel to avoid cache attribute
126  * conflicts.
127  */
128 static int
129 ioremap_change_attr(unsigned long phys_addr, unsigned long size,
130                                         unsigned long flags)
131 {
132         int err = 0;
133         if (flags && phys_addr + size - 1 < (end_pfn_map << PAGE_SHIFT)) {
134                 unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
135                 unsigned long vaddr = (unsigned long) __va(phys_addr);
136
137                 /*
138                  * Must use a address here and not struct page because the phys addr
139                  * can be a in hole between nodes and not have an memmap entry.
140                  */
141                 err = change_page_attr_addr(vaddr,npages,__pgprot(__PAGE_KERNEL|flags));
142                 if (!err)
143                         global_flush_tlb();
144         }
145         return err;
146 }
147
148 /*
149  * Generic mapping function
150  */
151
152 /*
153  * Remap an arbitrary physical address space into the kernel virtual
154  * address space. Needed when the kernel wants to access high addresses
155  * directly.
156  *
157  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
158  * have to convert them into an offset in a page-aligned mapping, but the
159  * caller shouldn't need to know that small detail.
160  */
161 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
162 {
163         void * addr;
164         struct vm_struct * area;
165         unsigned long offset, last_addr;
166
167         /* Don't allow wraparound or zero size */
168         last_addr = phys_addr + size - 1;
169         if (!size || last_addr < phys_addr)
170                 return NULL;
171
172         /*
173          * Don't remap the low PCI/ISA area, it's always mapped..
174          */
175         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
176                 return (__force void __iomem *)phys_to_virt(phys_addr);
177
178 #ifndef CONFIG_DISCONTIGMEM
179         /*
180          * Don't allow anybody to remap normal RAM that we're using..
181          */
182         if (last_addr < virt_to_phys(high_memory)) {
183                 char *t_addr, *t_end;
184                 struct page *page;
185
186                 t_addr = __va(phys_addr);
187                 t_end = t_addr + (size - 1);
188            
189                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
190                         if(!PageReserved(page))
191                                 return NULL;
192         }
193 #endif
194
195         /*
196          * Mappings have to be page-aligned
197          */
198         offset = phys_addr & ~PAGE_MASK;
199         phys_addr &= PAGE_MASK;
200         size = PAGE_ALIGN(last_addr+1) - phys_addr;
201
202         /*
203          * Ok, go for it..
204          */
205         area = get_vm_area(size, VM_IOREMAP | (flags << 20));
206         if (!area)
207                 return NULL;
208         area->phys_addr = phys_addr;
209         addr = area->addr;
210         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
211                 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
212                 return NULL;
213         }
214         if (ioremap_change_attr(phys_addr, size, flags) < 0) {
215                 area->flags &= 0xffffff;
216                 vunmap(addr);
217                 return NULL;
218         }
219         return (__force void __iomem *) (offset + (char *)addr);
220 }
221
222 /**
223  * ioremap_nocache     -   map bus memory into CPU space
224  * @offset:    bus address of the memory
225  * @size:      size of the resource to map
226  *
227  * ioremap_nocache performs a platform specific sequence of operations to
228  * make bus memory CPU accessible via the readb/readw/readl/writeb/
229  * writew/writel functions and the other mmio helpers. The returned
230  * address is not guaranteed to be usable directly as a virtual
231  * address. 
232  *
233  * This version of ioremap ensures that the memory is marked uncachable
234  * on the CPU as well as honouring existing caching rules from things like
235  * the PCI bus. Note that there are other caches and buffers on many 
236  * busses. In particular driver authors should read up on PCI writes
237  *
238  * It's useful if some control registers are in such an area and
239  * write combining or read caching is not desirable:
240  * 
241  * Must be freed with iounmap.
242  */
243
244 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
245 {
246         return __ioremap(phys_addr, size, _PAGE_PCD);
247 }
248
249 void iounmap(volatile void __iomem *addr)
250 {
251         struct vm_struct *p, **pprev;
252
253         if (addr <= high_memory) 
254                 return; 
255
256         write_lock(&vmlist_lock);
257         for (p = vmlist, pprev = &vmlist; p != NULL; pprev = &p->next, p = *pprev)
258                 if (p->addr == (void *)(PAGE_MASK & (unsigned long)addr))
259                         break;
260         if (!p) { 
261                 printk("__iounmap: bad address %p\n", addr);
262                 goto out_unlock;
263         }
264         *pprev = p->next;
265         unmap_vm_area(p);
266         if ((p->flags >> 20) &&
267                 p->phys_addr + p->size - 1 < virt_to_phys(high_memory)) {
268                 /* p->size includes the guard page, but cpa doesn't like that */
269                 change_page_attr(virt_to_page(__va(p->phys_addr)),
270                                  (p->size - PAGE_SIZE) >> PAGE_SHIFT,
271                                  PAGE_KERNEL);                           
272                 global_flush_tlb();
273         } 
274 out_unlock:
275         write_unlock(&vmlist_lock);
276         kfree(p); 
277 }