]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * PowerPC version | |
3 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) | |
4 | * | |
5 | * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au) | |
6 | * and Cort Dougan (PReP) (cort@cs.nmt.edu) | |
7 | * Copyright (C) 1996 Paul Mackerras | |
8 | * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk). | |
9 | * PPC44x/36-bit changes by Matt Porter (mporter@mvista.com) | |
10 | * | |
11 | * Derived from "arch/i386/mm/init.c" | |
12 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License | |
16 | * as published by the Free Software Foundation; either version | |
17 | * 2 of the License, or (at your option) any later version. | |
18 | * | |
19 | */ | |
20 | ||
1da177e4 LT |
21 | #include <linux/module.h> |
22 | #include <linux/sched.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/string.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/stddef.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/bootmem.h> | |
31 | #include <linux/highmem.h> | |
32 | #include <linux/initrd.h> | |
33 | #include <linux/pagemap.h> | |
34 | ||
35 | #include <asm/pgalloc.h> | |
36 | #include <asm/prom.h> | |
37 | #include <asm/io.h> | |
38 | #include <asm/mmu_context.h> | |
39 | #include <asm/pgtable.h> | |
40 | #include <asm/mmu.h> | |
41 | #include <asm/smp.h> | |
42 | #include <asm/machdep.h> | |
43 | #include <asm/btext.h> | |
44 | #include <asm/tlb.h> | |
45 | #include <asm/bootinfo.h> | |
46 | ||
47 | #include "mem_pieces.h" | |
48 | #include "mmu_decl.h" | |
49 | ||
50 | #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL) | |
a8de5ce9 | 51 | /* The amount of lowmem must be within 0xF0000000 - KERNELBASE. */ |
1da177e4 LT |
52 | #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE)) |
53 | #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL" | |
54 | #endif | |
55 | #endif | |
56 | #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE | |
57 | ||
58 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); | |
59 | ||
60 | unsigned long total_memory; | |
61 | unsigned long total_lowmem; | |
62 | ||
63 | unsigned long ppc_memstart; | |
64 | unsigned long ppc_memoffset = PAGE_OFFSET; | |
65 | ||
66 | int mem_init_done; | |
67 | int init_bootmem_done; | |
68 | int boot_mapsize; | |
1da177e4 LT |
69 | |
70 | extern char _end[]; | |
71 | extern char etext[], _stext[]; | |
72 | extern char __init_begin, __init_end; | |
1da177e4 LT |
73 | |
74 | #ifdef CONFIG_HIGHMEM | |
75 | pte_t *kmap_pte; | |
76 | pgprot_t kmap_prot; | |
77 | ||
78 | EXPORT_SYMBOL(kmap_prot); | |
79 | EXPORT_SYMBOL(kmap_pte); | |
80 | #endif | |
81 | ||
82 | void MMU_init(void); | |
83 | void set_phys_avail(unsigned long total_ram); | |
84 | ||
85 | /* XXX should be in current.h -- paulus */ | |
86 | extern struct task_struct *current_set[NR_CPUS]; | |
87 | ||
88 | char *klimit = _end; | |
89 | struct mem_pieces phys_avail; | |
90 | ||
1da177e4 LT |
91 | /* |
92 | * this tells the system to map all of ram with the segregs | |
93 | * (i.e. page tables) instead of the bats. | |
94 | * -- Cort | |
95 | */ | |
96 | int __map_without_bats; | |
97 | int __map_without_ltlbs; | |
98 | ||
99 | /* max amount of RAM to use */ | |
100 | unsigned long __max_memory; | |
101 | /* max amount of low RAM to map in */ | |
102 | unsigned long __max_low_memory = MAX_LOW_MEM; | |
103 | ||
104 | void show_mem(void) | |
105 | { | |
106 | int i,free = 0,total = 0,reserved = 0; | |
107 | int shared = 0, cached = 0; | |
108 | int highmem = 0; | |
109 | ||
110 | printk("Mem-info:\n"); | |
111 | show_free_areas(); | |
1da177e4 LT |
112 | i = max_mapnr; |
113 | while (i-- > 0) { | |
114 | total++; | |
115 | if (PageHighMem(mem_map+i)) | |
116 | highmem++; | |
117 | if (PageReserved(mem_map+i)) | |
118 | reserved++; | |
119 | else if (PageSwapCache(mem_map+i)) | |
120 | cached++; | |
121 | else if (!page_count(mem_map+i)) | |
122 | free++; | |
123 | else | |
124 | shared += page_count(mem_map+i) - 1; | |
125 | } | |
126 | printk("%d pages of RAM\n",total); | |
127 | printk("%d pages of HIGHMEM\n", highmem); | |
128 | printk("%d free pages\n",free); | |
129 | printk("%d reserved pages\n",reserved); | |
130 | printk("%d pages shared\n",shared); | |
131 | printk("%d pages swap cached\n",cached); | |
132 | } | |
133 | ||
134 | /* Free up now-unused memory */ | |
135 | static void free_sec(unsigned long start, unsigned long end, const char *name) | |
136 | { | |
137 | unsigned long cnt = 0; | |
138 | ||
139 | while (start < end) { | |
140 | ClearPageReserved(virt_to_page(start)); | |
7835e98b | 141 | init_page_count(virt_to_page(start)); |
1da177e4 LT |
142 | free_page(start); |
143 | cnt++; | |
144 | start += PAGE_SIZE; | |
145 | } | |
146 | if (cnt) { | |
147 | printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name); | |
148 | totalram_pages += cnt; | |
149 | } | |
150 | } | |
151 | ||
152 | void free_initmem(void) | |
153 | { | |
154 | #define FREESEC(TYPE) \ | |
155 | free_sec((unsigned long)(&__ ## TYPE ## _begin), \ | |
156 | (unsigned long)(&__ ## TYPE ## _end), \ | |
157 | #TYPE); | |
158 | ||
159 | printk ("Freeing unused kernel memory:"); | |
160 | FREESEC(init); | |
1da177e4 | 161 | printk("\n"); |
6c37a88c | 162 | ppc_md.progress = NULL; |
1da177e4 LT |
163 | #undef FREESEC |
164 | } | |
165 | ||
166 | #ifdef CONFIG_BLK_DEV_INITRD | |
167 | void free_initrd_mem(unsigned long start, unsigned long end) | |
168 | { | |
169 | printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10); | |
170 | ||
171 | for (; start < end; start += PAGE_SIZE) { | |
172 | ClearPageReserved(virt_to_page(start)); | |
7835e98b | 173 | init_page_count(virt_to_page(start)); |
1da177e4 LT |
174 | free_page(start); |
175 | totalram_pages++; | |
176 | } | |
177 | } | |
178 | #endif | |
179 | ||
180 | /* | |
181 | * Check for command-line options that affect what MMU_init will do. | |
182 | */ | |
183 | void MMU_setup(void) | |
184 | { | |
185 | /* Check for nobats option (used in mapin_ram). */ | |
186 | if (strstr(cmd_line, "nobats")) { | |
187 | __map_without_bats = 1; | |
188 | } | |
189 | ||
190 | if (strstr(cmd_line, "noltlbs")) { | |
191 | __map_without_ltlbs = 1; | |
192 | } | |
193 | ||
194 | /* Look for mem= option on command line */ | |
195 | if (strstr(cmd_line, "mem=")) { | |
196 | char *p, *q; | |
197 | unsigned long maxmem = 0; | |
198 | ||
199 | for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) { | |
200 | q = p + 4; | |
201 | if (p > cmd_line && p[-1] != ' ') | |
202 | continue; | |
203 | maxmem = simple_strtoul(q, &q, 0); | |
204 | if (*q == 'k' || *q == 'K') { | |
205 | maxmem <<= 10; | |
206 | ++q; | |
207 | } else if (*q == 'm' || *q == 'M') { | |
208 | maxmem <<= 20; | |
209 | ++q; | |
210 | } | |
211 | } | |
212 | __max_memory = maxmem; | |
213 | } | |
214 | } | |
215 | ||
216 | /* | |
217 | * MMU_init sets up the basic memory mappings for the kernel, | |
218 | * including both RAM and possibly some I/O regions, | |
219 | * and sets up the page tables and the MMU hardware ready to go. | |
220 | */ | |
221 | void __init MMU_init(void) | |
222 | { | |
223 | if (ppc_md.progress) | |
224 | ppc_md.progress("MMU:enter", 0x111); | |
225 | ||
226 | /* parse args from command line */ | |
227 | MMU_setup(); | |
228 | ||
229 | /* | |
230 | * Figure out how much memory we have, how much | |
231 | * is lowmem, and how much is highmem. If we were | |
232 | * passed the total memory size from the bootloader, | |
233 | * just use it. | |
234 | */ | |
235 | if (boot_mem_size) | |
236 | total_memory = boot_mem_size; | |
237 | else | |
238 | total_memory = ppc_md.find_end_of_memory(); | |
239 | ||
240 | if (__max_memory && total_memory > __max_memory) | |
241 | total_memory = __max_memory; | |
242 | total_lowmem = total_memory; | |
1da177e4 LT |
243 | if (total_lowmem > __max_low_memory) { |
244 | total_lowmem = __max_low_memory; | |
245 | #ifndef CONFIG_HIGHMEM | |
246 | total_memory = total_lowmem; | |
247 | #endif /* CONFIG_HIGHMEM */ | |
248 | } | |
249 | set_phys_avail(total_lowmem); | |
250 | ||
251 | /* Initialize the MMU hardware */ | |
252 | if (ppc_md.progress) | |
253 | ppc_md.progress("MMU:hw init", 0x300); | |
254 | MMU_init_hw(); | |
255 | ||
256 | /* Map in all of RAM starting at KERNELBASE */ | |
257 | if (ppc_md.progress) | |
258 | ppc_md.progress("MMU:mapin", 0x301); | |
259 | mapin_ram(); | |
260 | ||
261 | #ifdef CONFIG_HIGHMEM | |
262 | ioremap_base = PKMAP_BASE; | |
263 | #else | |
264 | ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */ | |
265 | #endif /* CONFIG_HIGHMEM */ | |
266 | ioremap_bot = ioremap_base; | |
267 | ||
268 | /* Map in I/O resources */ | |
269 | if (ppc_md.progress) | |
270 | ppc_md.progress("MMU:setio", 0x302); | |
271 | if (ppc_md.setup_io_mappings) | |
272 | ppc_md.setup_io_mappings(); | |
273 | ||
274 | /* Initialize the context management stuff */ | |
275 | mmu_context_init(); | |
276 | ||
277 | if (ppc_md.progress) | |
278 | ppc_md.progress("MMU:exit", 0x211); | |
279 | ||
280 | #ifdef CONFIG_BOOTX_TEXT | |
281 | /* By default, we are no longer mapped */ | |
282 | boot_text_mapped = 0; | |
283 | /* Must be done last, or ppc_md.progress will die. */ | |
284 | map_boot_text(); | |
285 | #endif | |
286 | } | |
287 | ||
288 | /* This is only called until mem_init is done. */ | |
289 | void __init *early_get_page(void) | |
290 | { | |
291 | void *p; | |
292 | ||
293 | if (init_bootmem_done) { | |
294 | p = alloc_bootmem_pages(PAGE_SIZE); | |
295 | } else { | |
296 | p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE); | |
297 | } | |
298 | return p; | |
299 | } | |
300 | ||
301 | /* | |
302 | * Initialize the bootmem system and give it all the memory we | |
303 | * have available. | |
304 | */ | |
305 | void __init do_init_bootmem(void) | |
306 | { | |
307 | unsigned long start, size; | |
308 | int i; | |
309 | ||
310 | /* | |
311 | * Find an area to use for the bootmem bitmap. | |
312 | * We look for the first area which is at least | |
313 | * 128kB in length (128kB is enough for a bitmap | |
314 | * for 4GB of memory, using 4kB pages), plus 1 page | |
315 | * (in case the address isn't page-aligned). | |
316 | */ | |
317 | start = 0; | |
318 | size = 0; | |
319 | for (i = 0; i < phys_avail.n_regions; ++i) { | |
320 | unsigned long a = phys_avail.regions[i].address; | |
321 | unsigned long s = phys_avail.regions[i].size; | |
322 | if (s <= size) | |
323 | continue; | |
324 | start = a; | |
325 | size = s; | |
326 | if (s >= 33 * PAGE_SIZE) | |
327 | break; | |
328 | } | |
329 | start = PAGE_ALIGN(start); | |
330 | ||
331 | min_low_pfn = start >> PAGE_SHIFT; | |
332 | max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT; | |
333 | max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT; | |
334 | boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn, | |
335 | PPC_MEMSTART >> PAGE_SHIFT, | |
336 | max_low_pfn); | |
337 | ||
338 | /* remove the bootmem bitmap from the available memory */ | |
339 | mem_pieces_remove(&phys_avail, start, boot_mapsize, 1); | |
340 | ||
341 | /* add everything in phys_avail into the bootmem map */ | |
342 | for (i = 0; i < phys_avail.n_regions; ++i) | |
343 | free_bootmem(phys_avail.regions[i].address, | |
344 | phys_avail.regions[i].size); | |
345 | ||
346 | init_bootmem_done = 1; | |
347 | } | |
348 | ||
349 | /* | |
350 | * paging_init() sets up the page tables - in fact we've already done this. | |
351 | */ | |
352 | void __init paging_init(void) | |
353 | { | |
c67c3cb4 MG |
354 | unsigned long start_pfn, end_pfn; |
355 | unsigned long max_zone_pfns[MAX_NR_ZONES]; | |
1da177e4 LT |
356 | #ifdef CONFIG_HIGHMEM |
357 | map_page(PKMAP_BASE, 0, 0); /* XXX gross */ | |
358 | pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k | |
359 | (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE); | |
360 | map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */ | |
361 | kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k | |
362 | (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN); | |
363 | kmap_prot = PAGE_KERNEL; | |
364 | #endif /* CONFIG_HIGHMEM */ | |
c67c3cb4 MG |
365 | /* All pages are DMA-able so we put them all in the DMA zone. */ |
366 | start_pfn = __pa(PAGE_OFFSET) >> PAGE_SHIFT; | |
367 | end_pfn = start_pfn + (total_memory >> PAGE_SHIFT); | |
368 | add_active_range(0, start_pfn, end_pfn); | |
1da177e4 | 369 | |
6391af17 | 370 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
1da177e4 | 371 | #ifdef CONFIG_HIGHMEM |
6391af17 MG |
372 | max_zone_pfns[ZONE_DMA] = total_lowmem >> PAGE_SHIFT; |
373 | max_zone_pfns[ZONE_HIGHMEM] = total_memory >> PAGE_SHIFT; | |
c67c3cb4 | 374 | #else |
6391af17 | 375 | max_zone_pfns[ZONE_DMA] = total_memory >> PAGE_SHIFT; |
1da177e4 | 376 | #endif /* CONFIG_HIGHMEM */ |
c67c3cb4 | 377 | free_area_init_nodes(max_zone_pfns); |
1da177e4 LT |
378 | } |
379 | ||
380 | void __init mem_init(void) | |
381 | { | |
382 | unsigned long addr; | |
383 | int codepages = 0; | |
384 | int datapages = 0; | |
385 | int initpages = 0; | |
386 | #ifdef CONFIG_HIGHMEM | |
387 | unsigned long highmem_mapnr; | |
388 | ||
389 | highmem_mapnr = total_lowmem >> PAGE_SHIFT; | |
390 | #endif /* CONFIG_HIGHMEM */ | |
391 | max_mapnr = total_memory >> PAGE_SHIFT; | |
392 | ||
393 | high_memory = (void *) __va(PPC_MEMSTART + total_lowmem); | |
394 | num_physpages = max_mapnr; /* RAM is assumed contiguous */ | |
395 | ||
396 | totalram_pages += free_all_bootmem(); | |
397 | ||
398 | #ifdef CONFIG_BLK_DEV_INITRD | |
399 | /* if we are booted from BootX with an initial ramdisk, | |
400 | make sure the ramdisk pages aren't reserved. */ | |
401 | if (initrd_start) { | |
402 | for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE) | |
403 | ClearPageReserved(virt_to_page(addr)); | |
404 | } | |
405 | #endif /* CONFIG_BLK_DEV_INITRD */ | |
406 | ||
1da177e4 LT |
407 | for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory; |
408 | addr += PAGE_SIZE) { | |
409 | if (!PageReserved(virt_to_page(addr))) | |
410 | continue; | |
411 | if (addr < (ulong) etext) | |
412 | codepages++; | |
413 | else if (addr >= (unsigned long)&__init_begin | |
414 | && addr < (unsigned long)&__init_end) | |
415 | initpages++; | |
416 | else if (addr < (ulong) klimit) | |
417 | datapages++; | |
418 | } | |
419 | ||
420 | #ifdef CONFIG_HIGHMEM | |
421 | { | |
422 | unsigned long pfn; | |
423 | ||
424 | for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) { | |
425 | struct page *page = mem_map + pfn; | |
426 | ||
427 | ClearPageReserved(page); | |
7835e98b | 428 | init_page_count(page); |
1da177e4 LT |
429 | __free_page(page); |
430 | totalhigh_pages++; | |
431 | } | |
432 | totalram_pages += totalhigh_pages; | |
433 | } | |
434 | #endif /* CONFIG_HIGHMEM */ | |
435 | ||
436 | printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n", | |
437 | (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10), | |
438 | codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10), | |
439 | initpages<< (PAGE_SHIFT-10), | |
440 | (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))); | |
6879dc13 | 441 | |
1da177e4 LT |
442 | mem_init_done = 1; |
443 | } | |
444 | ||
445 | /* | |
446 | * Set phys_avail to the amount of physical memory, | |
447 | * less the kernel text/data/bss. | |
448 | */ | |
449 | void __init | |
450 | set_phys_avail(unsigned long total_memory) | |
451 | { | |
452 | unsigned long kstart, ksize; | |
453 | ||
454 | /* | |
455 | * Initially, available physical memory is equivalent to all | |
456 | * physical memory. | |
457 | */ | |
458 | ||
459 | phys_avail.regions[0].address = PPC_MEMSTART; | |
460 | phys_avail.regions[0].size = total_memory; | |
461 | phys_avail.n_regions = 1; | |
462 | ||
463 | /* | |
464 | * Map out the kernel text/data/bss from the available physical | |
465 | * memory. | |
466 | */ | |
467 | ||
468 | kstart = __pa(_stext); /* should be 0 */ | |
469 | ksize = PAGE_ALIGN(klimit - _stext); | |
470 | ||
471 | mem_pieces_remove(&phys_avail, kstart, ksize, 0); | |
472 | mem_pieces_remove(&phys_avail, 0, 0x4000, 0); | |
473 | ||
474 | #if defined(CONFIG_BLK_DEV_INITRD) | |
475 | /* Remove the init RAM disk from the available memory. */ | |
476 | if (initrd_start) { | |
477 | mem_pieces_remove(&phys_avail, __pa(initrd_start), | |
478 | initrd_end - initrd_start, 1); | |
479 | } | |
480 | #endif /* CONFIG_BLK_DEV_INITRD */ | |
1da177e4 LT |
481 | } |
482 | ||
483 | /* Mark some memory as reserved by removing it from phys_avail. */ | |
484 | void __init reserve_phys_mem(unsigned long start, unsigned long size) | |
485 | { | |
486 | mem_pieces_remove(&phys_avail, start, size, 1); | |
487 | } | |
488 | ||
489 | /* | |
490 | * This is called when a page has been modified by the kernel. | |
491 | * It just marks the page as not i-cache clean. We do the i-cache | |
492 | * flush later when the page is given to a user process, if necessary. | |
493 | */ | |
494 | void flush_dcache_page(struct page *page) | |
495 | { | |
496 | clear_bit(PG_arch_1, &page->flags); | |
497 | } | |
498 | ||
499 | void flush_dcache_icache_page(struct page *page) | |
500 | { | |
501 | #ifdef CONFIG_BOOKE | |
5a6a4d43 RD |
502 | void *start = kmap_atomic(page, KM_PPC_SYNC_ICACHE); |
503 | __flush_dcache_icache(start); | |
504 | kunmap_atomic(start, KM_PPC_SYNC_ICACHE); | |
cc9c540b | 505 | #elif defined(CONFIG_8xx) |