[PARISC] Enable ioremap functionality unconditionally
[linux-2.6] / arch / parisc / mm / ioremap.c
1 /*
2  * arch/parisc/mm/ioremap.c
3  *
4  * (C) Copyright 1995 1996 Linus Torvalds
5  * (C) Copyright 2001 Helge Deller <deller@gmx.de>
6  * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org>
7  */
8
9 #include <linux/vmalloc.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <asm/io.h>
13 #include <asm/pgalloc.h>
14 #include <asm/tlbflush.h>
15 #include <asm/cacheflush.h>
16
17 static inline void 
18 remap_area_pte(pte_t *pte, unsigned long address, unsigned long size,
19                unsigned long phys_addr, unsigned long flags)
20 {
21         unsigned long end, pfn;
22         pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY |
23                                    _PAGE_ACCESSED | flags);
24
25         address &= ~PMD_MASK;
26
27         end = address + size;
28         if (end > PMD_SIZE)
29                 end = PMD_SIZE;
30
31         BUG_ON(address >= end);
32
33         pfn = phys_addr >> PAGE_SHIFT;
34         do {
35                 BUG_ON(!pte_none(*pte));
36
37                 set_pte(pte, pfn_pte(pfn, pgprot));
38
39                 address += PAGE_SIZE;
40                 pfn++;
41                 pte++;
42         } while (address && (address < end));
43 }
44
45 static inline int 
46 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
53         end = address + size;
54         if (end > PGDIR_SIZE)
55                 end = PGDIR_SIZE;
56
57         BUG_ON(address >= end);
58
59         phys_addr -= address;
60         do {
61                 pte_t *pte = pte_alloc_kernel(pmd, address);
62                 if (!pte)
63                         return -ENOMEM;
64
65                 remap_area_pte(pte, address, end - address, 
66                                address + phys_addr, flags);
67
68                 address = (address + PMD_SIZE) & PMD_MASK;
69                 pmd++;
70         } while (address && (address < end));
71
72         return 0;
73 }
74
75 static int 
76 remap_area_pages(unsigned long address, unsigned long phys_addr,
77                  unsigned long size, unsigned long flags)
78 {
79         pgd_t *dir;
80         int error = 0;
81         unsigned long end = address + size;
82
83         BUG_ON(address >= end);
84
85         phys_addr -= address;
86         dir = pgd_offset_k(address);
87
88         flush_cache_all();
89
90         do {
91                 pud_t *pud;
92                 pmd_t *pmd;
93
94                 error = -ENOMEM;
95                 pud = pud_alloc(&init_mm, dir, address);
96                 if (!pud)
97                         break;
98
99                 pmd = pmd_alloc(&init_mm, pud, address);
100                 if (!pmd)
101                         break;
102
103                 if (remap_area_pmd(pmd, address, end - address,
104                                    phys_addr + address, flags))
105                         break;
106
107                 error = 0;
108                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
109                 dir++;
110         } while (address && (address < end));
111
112         flush_tlb_all();
113
114         return error;
115 }
116
117 #ifdef CONFIG_DEBUG_IOREMAP
118 static unsigned long last = 0;
119
120 void gsc_bad_addr(unsigned long addr)
121 {
122         if (time_after(jiffies, last + HZ*10)) {
123                 printk("gsc_foo() called with bad address 0x%lx\n", addr);
124                 dump_stack();
125                 last = jiffies;
126         }
127 }
128 EXPORT_SYMBOL(gsc_bad_addr);
129
130 void __raw_bad_addr(const volatile void __iomem *addr)
131 {
132         if (time_after(jiffies, last + HZ*10)) {
133                 printk("__raw_foo() called with bad address 0x%p\n", addr);
134                 dump_stack();
135                 last = jiffies;
136         }
137 }
138 EXPORT_SYMBOL(__raw_bad_addr);
139 #endif
140
141 /*
142  * Generic mapping function (not visible outside):
143  */
144
145 /*
146  * Remap an arbitrary physical address space into the kernel virtual
147  * address space.
148  *
149  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
150  * have to convert them into an offset in a page-aligned mapping, but the
151  * caller shouldn't need to know that small detail.
152  */
153 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
154 {
155 #ifdef CONFIG_EISA
156         #error FIXME.
157         unsigned long end = phys_addr + size - 1;
158         /* Support EISA addresses */
159         if ((phys_addr >= 0x00080000 && end < 0x000fffff)
160                         || (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
161                 phys_addr |= 0xfc000000;
162         }
163 #endif
164
165         void *addr;
166         struct vm_struct *area;
167         unsigned long offset, last_addr;
168
169         /* Don't allow wraparound or zero size */
170         last_addr = phys_addr + size - 1;
171         if (!size || last_addr < phys_addr)
172                 return NULL;
173
174         /*
175          * Don't allow anybody to remap normal RAM that we're using..
176          */
177         if (phys_addr < virt_to_phys(high_memory)) {
178                 char *t_addr, *t_end;
179                 struct page *page;
180
181                 t_addr = __va(phys_addr);
182                 t_end = t_addr + (size - 1);
183            
184                 for (page = virt_to_page(t_addr); 
185                      page <= virt_to_page(t_end); page++) {
186                         if(!PageReserved(page))
187                                 return NULL;
188                 }
189         }
190
191         /*
192          * Mappings have to be page-aligned
193          */
194         offset = phys_addr & ~PAGE_MASK;
195         phys_addr &= PAGE_MASK;
196         size = PAGE_ALIGN(last_addr) - phys_addr;
197
198         /*
199          * Ok, go for it..
200          */
201         area = get_vm_area(size, VM_IOREMAP);
202         if (!area)
203                 return NULL;
204
205         addr = area->addr;
206         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
207                 vfree(addr);
208                 return NULL;
209         }
210
211         return (void __iomem *) (offset + (char *)addr);
212 }
213
214 void iounmap(void __iomem *addr)
215 {
216         if (addr > high_memory)
217                 return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
218 }