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