1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/vmalloc.h>
33#include <linux/init.h>
34
35#include <asm/pgtable.h>
36#include <asm/pgalloc.h>
37#include <linux/io.h>
38#include <asm/mmu.h>
39#include <asm/sections.h>
40
41#define flush_HPTE(X, va, pg) _tlbie(va)
42
43unsigned long ioremap_base;
44unsigned long ioremap_bot;
45
46
47
48
49#define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
50
51#ifndef CONFIG_SMP
52struct pgtable_cache_struct quicklists;
53#endif
54
55static void __iomem *__ioremap(phys_addr_t addr, unsigned long size,
56 unsigned long flags)
57{
58 unsigned long v, i;
59 phys_addr_t p;
60 int err;
61
62
63
64
65
66
67
68 p = addr & PAGE_MASK;
69 size = PAGE_ALIGN(addr + size) - p;
70
71
72
73
74
75
76
77 if (mem_init_done &&
78 p >= memory_start && p < virt_to_phys(high_memory) &&
79 !(p >= virt_to_phys((unsigned long)&__bss_stop) &&
80 p < virt_to_phys((unsigned long)__bss_stop))) {
81 printk(KERN_WARNING "__ioremap(): phys addr "PTE_FMT
82 " is RAM lr %p\n", (unsigned long)p,
83 __builtin_return_address(0));
84 return NULL;
85 }
86
87 if (size == 0)
88 return NULL;
89
90
91
92
93
94
95
96
97
98
99
100
101 if (mem_init_done) {
102 struct vm_struct *area;
103 area = get_vm_area(size, VM_IOREMAP);
104 if (area == NULL)
105 return NULL;
106 v = VMALLOC_VMADDR(area->addr);
107 } else {
108 v = (ioremap_bot -= size);
109 }
110
111 if ((flags & _PAGE_PRESENT) == 0)
112 flags |= _PAGE_KERNEL;
113 if (flags & _PAGE_NO_CACHE)
114 flags |= _PAGE_GUARDED;
115
116 err = 0;
117 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
118 err = map_page(v + i, p + i, flags);
119 if (err) {
120 if (mem_init_done)
121 vfree((void *)v);
122 return NULL;
123 }
124
125 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
126}
127
128void __iomem *ioremap(phys_addr_t addr, unsigned long size)
129{
130 return __ioremap(addr, size, _PAGE_NO_CACHE);
131}
132EXPORT_SYMBOL(ioremap);
133
134void iounmap(void *addr)
135{
136 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
137 vfree((void *) (PAGE_MASK & (unsigned long) addr));
138}
139EXPORT_SYMBOL(iounmap);
140
141
142int map_page(unsigned long va, phys_addr_t pa, int flags)
143{
144 pmd_t *pd;
145 pte_t *pg;
146 int err = -ENOMEM;
147
148
149 pd = pmd_offset(pgd_offset_k(va), va);
150
151 pg = pte_alloc_kernel(pd, va);
152
153
154 if (pg != NULL) {
155 err = 0;
156 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
157 __pgprot(flags)));
158 if (mem_init_done)
159 flush_HPTE(0, va, pmd_val(*pd));
160
161
162 }
163
164 return err;
165}
166
167void __init adjust_total_lowmem(void)
168{
169
170#if 0
171 unsigned long max_low_mem = MAX_LOW_MEM;
172
173 if (total_lowmem > max_low_mem) {
174 total_lowmem = max_low_mem;
175#ifndef CONFIG_HIGHMEM
176 printk(KERN_INFO "Warning, memory limited to %ld Mb, use "
177 "CONFIG_HIGHMEM to reach %ld Mb\n",
178 max_low_mem >> 20, total_memory >> 20);
179 total_memory = total_lowmem;
180#endif
181 }
182#endif
183}
184
185static void show_tmem(unsigned long tmem)
186{
187 volatile unsigned long a;
188 a = a + tmem;
189}
190
191
192
193
194void __init mapin_ram(void)
195{
196 unsigned long v, p, s, f;
197
198 v = CONFIG_KERNEL_START;
199 p = memory_start;
200 show_tmem(memory_size);
201 for (s = 0; s < memory_size; s += PAGE_SIZE) {
202 f = _PAGE_PRESENT | _PAGE_ACCESSED |
203 _PAGE_SHARED | _PAGE_HWEXEC;
204 if ((char *) v < _stext || (char *) v >= _etext)
205 f |= _PAGE_WRENABLE;
206 else
207
208
209 f |= _PAGE_USER;
210 map_page(v, p, f);
211 v += PAGE_SIZE;
212 p += PAGE_SIZE;
213 }
214}
215
216
217#define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
218
219
220
221
222
223
224void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
225 unsigned int size, int flags)
226{
227 int i;
228
229 if (virt > CONFIG_KERNEL_START && virt < ioremap_bot)
230 ioremap_bot = ioremap_base = virt;
231
232
233 for (i = 0; i < size; i += PAGE_SIZE)
234 map_page(virt + i, phys + i, flags);
235}
236
237
238
239
240
241
242static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
243{
244 pgd_t *pgd;
245 pmd_t *pmd;
246 pte_t *pte;
247 int retval = 0;
248
249 pgd = pgd_offset(mm, addr & PAGE_MASK);
250 if (pgd) {
251 pmd = pmd_offset(pgd, addr & PAGE_MASK);
252 if (pmd_present(*pmd)) {
253 pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
254 if (pte) {
255 retval = 1;
256 *ptep = pte;
257 }
258 }
259 }
260 return retval;
261}
262
263
264
265
266unsigned long iopa(unsigned long addr)
267{
268 unsigned long pa;
269
270 pte_t *pte;
271 struct mm_struct *mm;
272
273
274
275
276 if (addr < TASK_SIZE)
277 mm = current->mm;
278 else
279 mm = &init_mm;
280
281 pa = 0;
282 if (get_pteptr(mm, addr, &pte))
283 pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
284
285 return pa;
286}
287