Commit | Line | Data |
---|---|---|
a95d0e16 MS |
1 | /* |
2 | * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu> | |
3 | * Copyright (C) 2006 Atmark Techno, Inc. | |
4 | * | |
5 | * This file is subject to the terms and conditions of the GNU General Public | |
6 | * License. See the file "COPYING" in the main directory of this archive | |
7 | * for more details. | |
8 | */ | |
9 | ||
10 | #include <linux/bootmem.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/lmb.h> | |
14 | #include <linux/mm.h> /* mem_init */ | |
15 | #include <linux/initrd.h> | |
16 | #include <linux/pagemap.h> | |
17 | #include <linux/pfn.h> | |
18 | #include <linux/swap.h> | |
19 | ||
20 | #include <asm/page.h> | |
21 | #include <asm/mmu_context.h> | |
22 | #include <asm/pgalloc.h> | |
23 | #include <asm/sections.h> | |
24 | #include <asm/tlb.h> | |
25 | ||
4dc60832 | 26 | #ifndef CONFIG_MMU |
a95d0e16 | 27 | unsigned int __page_offset; |
5af7fa68 | 28 | EXPORT_SYMBOL(__page_offset); |
a95d0e16 | 29 | |
4dc60832 MS |
30 | #else |
31 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); | |
32 | ||
33 | int mem_init_done; | |
34 | static int init_bootmem_done; | |
35 | #endif /* CONFIG_MMU */ | |
36 | ||
a95d0e16 MS |
37 | char *klimit = _end; |
38 | ||
39 | /* | |
40 | * Initialize the bootmem system and give it all the memory we | |
41 | * have available. | |
42 | */ | |
4dc60832 | 43 | unsigned long memory_start; |
fd6ed51f | 44 | EXPORT_SYMBOL(memory_start); |
4dc60832 MS |
45 | unsigned long memory_end; /* due to mm/nommu.c */ |
46 | unsigned long memory_size; | |
a95d0e16 MS |
47 | |
48 | /* | |
49 | * paging_init() sets up the page tables - in fact we've already done this. | |
50 | */ | |
51 | static void __init paging_init(void) | |
52 | { | |
a95d0e16 MS |
53 | unsigned long zones_size[MAX_NR_ZONES]; |
54 | ||
5af90438 SM |
55 | /* Clean every zones */ |
56 | memset(zones_size, 0, sizeof(zones_size)); | |
57 | ||
a95d0e16 MS |
58 | /* |
59 | * old: we can DMA to/from any address.put all page into ZONE_DMA | |
60 | * We use only ZONE_NORMAL | |
61 | */ | |
62 | zones_size[ZONE_NORMAL] = max_mapnr; | |
63 | ||
a95d0e16 MS |
64 | free_area_init(zones_size); |
65 | } | |
66 | ||
67 | void __init setup_memory(void) | |
68 | { | |
69 | int i; | |
70 | unsigned long map_size; | |
4dc60832 | 71 | #ifndef CONFIG_MMU |
a95d0e16 MS |
72 | u32 kernel_align_start, kernel_align_size; |
73 | ||
74 | /* Find main memory where is the kernel */ | |
75 | for (i = 0; i < lmb.memory.cnt; i++) { | |
76 | memory_start = (u32) lmb.memory.region[i].base; | |
77 | memory_end = (u32) lmb.memory.region[i].base | |
78 | + (u32) lmb.memory.region[i].size; | |
79 | if ((memory_start <= (u32)_text) && | |
80 | ((u32)_text <= memory_end)) { | |
81 | memory_size = memory_end - memory_start; | |
82 | PAGE_OFFSET = memory_start; | |
83 | printk(KERN_INFO "%s: Main mem: 0x%x-0x%x, " | |
db6e3f91 MS |
84 | "size 0x%08x\n", __func__, (u32) memory_start, |
85 | (u32) memory_end, (u32) memory_size); | |
a95d0e16 MS |
86 | break; |
87 | } | |
88 | } | |
89 | ||
90 | if (!memory_start || !memory_end) { | |
91 | panic("%s: Missing memory setting 0x%08x-0x%08x\n", | |
db6e3f91 | 92 | __func__, (u32) memory_start, (u32) memory_end); |
a95d0e16 MS |
93 | } |
94 | ||
95 | /* reservation of region where is the kernel */ | |
96 | kernel_align_start = PAGE_DOWN((u32)_text); | |
97 | /* ALIGN can be remove because _end in vmlinux.lds.S is align */ | |
98 | kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start; | |
99 | lmb_reserve(kernel_align_start, kernel_align_size); | |
100 | printk(KERN_INFO "%s: kernel addr=0x%08x-0x%08x size=0x%08x\n", | |
101 | __func__, kernel_align_start, kernel_align_start | |
102 | + kernel_align_size, kernel_align_size); | |
103 | ||
4dc60832 | 104 | #endif |
a95d0e16 MS |
105 | /* |
106 | * Kernel: | |
107 | * start: base phys address of kernel - page align | |
108 | * end: base phys address of kernel - page align | |
109 | * | |
110 | * min_low_pfn - the first page (mm/bootmem.c - node_boot_start) | |
111 | * max_low_pfn | |
112 | * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn) | |
113 | * num_physpages - number of all pages | |
114 | */ | |
115 | ||
116 | /* memory start is from the kernel end (aligned) to higher addr */ | |
117 | min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */ | |
118 | /* RAM is assumed contiguous */ | |
119 | num_physpages = max_mapnr = memory_size >> PAGE_SHIFT; | |
120 | max_pfn = max_low_pfn = memory_end >> PAGE_SHIFT; | |
121 | ||
122 | printk(KERN_INFO "%s: max_mapnr: %#lx\n", __func__, max_mapnr); | |
123 | printk(KERN_INFO "%s: min_low_pfn: %#lx\n", __func__, min_low_pfn); | |
124 | printk(KERN_INFO "%s: max_low_pfn: %#lx\n", __func__, max_low_pfn); | |
125 | ||
126 | /* | |
127 | * Find an area to use for the bootmem bitmap. | |
128 | * We look for the first area which is at least | |
129 | * 128kB in length (128kB is enough for a bitmap | |
130 | * for 4GB of memory, using 4kB pages), plus 1 page | |
131 | * (in case the address isn't page-aligned). | |
132 | */ | |
4dc60832 | 133 | #ifndef CONFIG_MMU |
8f37b6c9 | 134 | map_size = init_bootmem_node(NODE_DATA(0), PFN_UP(TOPHYS((u32)klimit)), |
a95d0e16 | 135 | min_low_pfn, max_low_pfn); |
4dc60832 MS |
136 | #else |
137 | map_size = init_bootmem_node(&contig_page_data, | |
8f37b6c9 | 138 | PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn); |
4dc60832 | 139 | #endif |
8f37b6c9 | 140 | lmb_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size); |
a95d0e16 MS |
141 | |
142 | /* free bootmem is whole main memory */ | |
143 | free_bootmem(memory_start, memory_size); | |
144 | ||
145 | /* reserve allocate blocks */ | |
146 | for (i = 0; i < lmb.reserved.cnt; i++) { | |
147 | pr_debug("reserved %d - 0x%08x-0x%08x\n", i, | |
148 | (u32) lmb.reserved.region[i].base, | |
149 | (u32) lmb_size_bytes(&lmb.reserved, i)); | |
150 | reserve_bootmem(lmb.reserved.region[i].base, | |
151 | lmb_size_bytes(&lmb.reserved, i) - 1, BOOTMEM_DEFAULT); | |
152 | } | |
4dc60832 MS |
153 | #ifdef CONFIG_MMU |
154 | init_bootmem_done = 1; | |
155 | #endif | |
a95d0e16 MS |
156 | paging_init(); |
157 | } | |
158 | ||
159 | void free_init_pages(char *what, unsigned long begin, unsigned long end) | |
160 | { | |
161 | unsigned long addr; | |
162 | ||
163 | for (addr = begin; addr < end; addr += PAGE_SIZE) { | |
164 | ClearPageReserved(virt_to_page(addr)); | |
165 | init_page_count(virt_to_page(addr)); | |
166 | memset((void *)addr, 0xcc, PAGE_SIZE); | |
167 | free_page(addr); | |
168 | totalram_pages++; | |
169 | } | |
170 | printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10); | |
171 | } | |
172 | ||
173 | #ifdef CONFIG_BLK_DEV_INITRD | |
174 | void free_initrd_mem(unsigned long start, unsigned long end) | |
175 | { | |
176 | int pages = 0; | |
177 | for (; start < end; start += PAGE_SIZE) { | |
178 | ClearPageReserved(virt_to_page(start)); | |
179 | init_page_count(virt_to_page(start)); | |
180 | free_page(start); | |
181 | totalram_pages++; | |
182 | pages++; | |
183 | } | |
d6f61770 LS |
184 | printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n", |
185 | (int)(pages * (PAGE_SIZE / 1024))); | |
a95d0e16 MS |
186 | } |
187 | #endif | |
188 | ||
189 | void free_initmem(void) | |
190 | { | |
191 | free_init_pages("unused kernel memory", | |
192 | (unsigned long)(&__init_begin), | |
193 | (unsigned long)(&__init_end)); | |
194 | } | |
195 | ||
a95d0e16 MS |
196 | void __init mem_init(void) |
197 | { | |
198 | high_memory = (void *)__va(memory_end); | |
199 | /* this will put all memory onto the freelists */ | |
200 | totalram_pages += free_all_bootmem(); | |
201 | ||
202 | printk(KERN_INFO "Memory: %luk/%luk available\n", | |
cc013a88 | 203 | nr_free_pages() << (PAGE_SHIFT-10), |
a95d0e16 | 204 | num_physpages << (PAGE_SHIFT-10)); |
4dc60832 MS |
205 | #ifdef CONFIG_MMU |
206 | mem_init_done = 1; | |
207 | #endif | |
a95d0e16 MS |
208 | } |
209 | ||
4dc60832 | 210 | #ifndef CONFIG_MMU |
a95d0e16 MS |
211 | /* Check against bounds of physical memory */ |
212 | int ___range_ok(unsigned long addr, unsigned long size) | |
213 | { | |
214 | return ((addr < memory_start) || | |
215 | ((addr + size) > memory_end)); | |
216 | } | |
5af7fa68 | 217 | EXPORT_SYMBOL(___range_ok); |
4dc60832 MS |
218 | |
219 | #else | |
220 | int page_is_ram(unsigned long pfn) | |
221 | { | |
222 | return pfn < max_low_pfn; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Check for command-line options that affect what MMU_init will do. | |
227 | */ | |
228 | static void mm_cmdline_setup(void) | |
229 | { | |
230 | unsigned long maxmem = 0; | |
231 | char *p = cmd_line; | |
232 | ||
233 | /* Look for mem= option on command line */ | |
234 | p = strstr(cmd_line, "mem="); | |
235 | if (p) { | |
236 | p += 4; | |
237 | maxmem = memparse(p, &p); | |
238 | if (maxmem && memory_size > maxmem) { | |
239 | memory_size = maxmem; | |
240 | memory_end = memory_start + memory_size; | |
241 | lmb.memory.region[0].size = memory_size; | |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | /* | |
247 | * MMU_init_hw does the chip-specific initialization of the MMU hardware. | |
248 | */ | |
249 | static void __init mmu_init_hw(void) | |
250 | { | |
251 | /* | |
252 | * The Zone Protection Register (ZPR) defines how protection will | |
253 | * be applied to every page which is a member of a given zone. At | |
254 | * present, we utilize only two of the zones. | |
255 | * The zone index bits (of ZSEL) in the PTE are used for software | |
256 | * indicators, except the LSB. For user access, zone 1 is used, | |
257 | * for kernel access, zone 0 is used. We set all but zone 1 | |
258 | * to zero, allowing only kernel access as indicated in the PTE. | |
259 | * For zone 1, we set a 01 binary (a value of 10 will not work) | |
260 | * to allow user access as indicated in the PTE. This also allows | |
261 | * kernel access as indicated in the PTE. | |
262 | */ | |
263 | __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \ | |
264 | "mts rzpr, r11;" | |
265 | : : : "r11"); | |
266 | } | |
267 | ||
268 | /* | |
269 | * MMU_init sets up the basic memory mappings for the kernel, | |
270 | * including both RAM and possibly some I/O regions, | |
271 | * and sets up the page tables and the MMU hardware ready to go. | |
272 | */ | |
273 | ||
274 | /* called from head.S */ | |
275 | asmlinkage void __init mmu_init(void) | |
276 | { | |
277 | unsigned int kstart, ksize; | |
278 | ||
279 | if (!lmb.reserved.cnt) { | |
280 | printk(KERN_EMERG "Error memory count\n"); | |
281 | machine_restart(NULL); | |
282 | } | |
283 | ||
284 | if ((u32) lmb.memory.region[0].size < 0x1000000) { | |
285 | printk(KERN_EMERG "Memory must be greater than 16MB\n"); | |
286 | machine_restart(NULL); | |
287 | } | |
288 | /* Find main memory where the kernel is */ | |
289 | memory_start = (u32) lmb.memory.region[0].base; | |
290 | memory_end = (u32) lmb.memory.region[0].base + | |
291 | (u32) lmb.memory.region[0].size; | |
292 | memory_size = memory_end - memory_start; | |
293 | ||
294 | mm_cmdline_setup(); /* FIXME parse args from command line - not used */ | |
295 | ||
296 | /* | |
297 | * Map out the kernel text/data/bss from the available physical | |
298 | * memory. | |
299 | */ | |
300 | kstart = __pa(CONFIG_KERNEL_START); /* kernel start */ | |
301 | /* kernel size */ | |
302 | ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START)); | |
303 | lmb_reserve(kstart, ksize); | |
304 | ||
305 | #if defined(CONFIG_BLK_DEV_INITRD) | |
306 | /* Remove the init RAM disk from the available memory. */ | |
307 | /* if (initrd_start) { | |
308 | mem_pieces_remove(&phys_avail, __pa(initrd_start), | |
309 | initrd_end - initrd_start, 1); | |
310 | }*/ | |
311 | #endif /* CONFIG_BLK_DEV_INITRD */ | |
312 | ||
313 | /* Initialize the MMU hardware */ | |
314 | mmu_init_hw(); | |
315 | ||
316 | /* Map in all of RAM starting at CONFIG_KERNEL_START */ | |
317 | mapin_ram(); | |
318 | ||
319 | #ifdef HIGHMEM_START_BOOL | |
320 | ioremap_base = HIGHMEM_START; | |
321 | #else | |
322 | ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */ | |
323 | #endif /* CONFIG_HIGHMEM */ | |
324 | ioremap_bot = ioremap_base; | |
325 | ||
326 | /* Initialize the context management stuff */ | |
327 | mmu_context_init(); | |
328 | } | |
329 | ||
330 | /* This is only called until mem_init is done. */ | |
331 | void __init *early_get_page(void) | |
332 | { | |
333 | void *p; | |
334 | if (init_bootmem_done) { | |
335 | p = alloc_bootmem_pages(PAGE_SIZE); | |
336 | } else { | |
337 | /* | |
338 | * Mem start + 32MB -> here is limit | |
339 | * because of mem mapping from head.S | |
340 | */ | |
341 | p = __va(lmb_alloc_base(PAGE_SIZE, PAGE_SIZE, | |
342 | memory_start + 0x2000000)); | |
343 | } | |
344 | return p; | |
345 | } | |
a84642a3 MS |
346 | |
347 | void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask) | |
348 | { | |
349 | if (mem_init_done) | |
350 | return kmalloc(size, mask); | |
351 | else | |
352 | return alloc_bootmem(size); | |
353 | } | |
354 | ||
355 | void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask) | |
356 | { | |
357 | void *p; | |
358 | ||
359 | if (mem_init_done) | |
360 | p = kzalloc(size, mask); | |
361 | else { | |
362 | p = alloc_bootmem(size); | |
363 | if (p) | |
364 | memset(p, 0, size); | |
365 | } | |
366 | return p; | |
367 | } | |
368 | ||
4dc60832 | 369 | #endif /* CONFIG_MMU */ |