blob: 2253e122aa85e54c4c1ab593f5f622efe450f8e4 [file] [log] [blame]
Michal Simeka95d0e12009-03-27 14:25:29 +01001/*
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>
Yinghai Lu95f72d12010-07-12 14:36:09 +100013#include <linux/memblock.h>
Michal Simeka95d0e12009-03-27 14:25:29 +010014#include <linux/mm.h> /* mem_init */
15#include <linux/initrd.h>
16#include <linux/pagemap.h>
17#include <linux/pfn.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Michal Simeka95d0e12009-03-27 14:25:29 +010019#include <linux/swap.h>
Paul Gortmaker66421a62011-09-22 11:22:55 -040020#include <linux/export.h>
Michal Simeka95d0e12009-03-27 14:25:29 +010021
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
Michal Simek79bf3a12010-01-20 15:17:08 +010028/* Use for MMU and noMMU because of PCI generic code */
29int mem_init_done;
30
Michal Simek4dc60832009-05-26 16:30:12 +020031#ifndef CONFIG_MMU
Michal Simeka95d0e12009-03-27 14:25:29 +010032unsigned int __page_offset;
Arnd Bergmann5af7fa62009-05-01 21:48:15 +000033EXPORT_SYMBOL(__page_offset);
Michal Simeka95d0e12009-03-27 14:25:29 +010034
Michal Simek4dc60832009-05-26 16:30:12 +020035#else
Michal Simek4dc60832009-05-26 16:30:12 +020036static int init_bootmem_done;
37#endif /* CONFIG_MMU */
38
Michal Simeka95d0e12009-03-27 14:25:29 +010039char *klimit = _end;
40
41/*
42 * Initialize the bootmem system and give it all the memory we
43 * have available.
44 */
Michal Simek4dc60832009-05-26 16:30:12 +020045unsigned long memory_start;
Michal Simekfd6ed512009-07-23 08:23:53 +020046EXPORT_SYMBOL(memory_start);
Michal Simek4dc60832009-05-26 16:30:12 +020047unsigned long memory_size;
Michal Simekee4bcdf2010-05-13 12:11:42 +020048EXPORT_SYMBOL(memory_size);
Michal Simek83a92522011-12-19 13:46:35 +010049unsigned long lowmem_size;
Michal Simeka95d0e12009-03-27 14:25:29 +010050
51/*
52 * paging_init() sets up the page tables - in fact we've already done this.
53 */
54static void __init paging_init(void)
55{
Michal Simeka95d0e12009-03-27 14:25:29 +010056 unsigned long zones_size[MAX_NR_ZONES];
57
Steve Magnani5af90432009-05-18 03:22:40 +020058 /* Clean every zones */
59 memset(zones_size, 0, sizeof(zones_size));
60
Michal Simek83a92522011-12-19 13:46:35 +010061 zones_size[ZONE_DMA] = max_pfn;
Michal Simeka95d0e12009-03-27 14:25:29 +010062
Michal Simeka95d0e12009-03-27 14:25:29 +010063 free_area_init(zones_size);
64}
65
66void __init setup_memory(void)
67{
Michal Simeka95d0e12009-03-27 14:25:29 +010068 unsigned long map_size;
Benjamin Herrenschmidt76bfcc82010-08-04 14:13:06 +100069 struct memblock_region *reg;
70
Michal Simek4dc60832009-05-26 16:30:12 +020071#ifndef CONFIG_MMU
Michal Simeka95d0e12009-03-27 14:25:29 +010072 u32 kernel_align_start, kernel_align_size;
73
74 /* Find main memory where is the kernel */
Benjamin Herrenschmidt76bfcc82010-08-04 14:13:06 +100075 for_each_memblock(memory, reg) {
76 memory_start = (u32)reg->base;
Michal Simek83a92522011-12-19 13:46:35 +010077 lowmem_size = reg->size;
Michal Simeka95d0e12009-03-27 14:25:29 +010078 if ((memory_start <= (u32)_text) &&
Michal Simek83a92522011-12-19 13:46:35 +010079 ((u32)_text <= (memory_start + lowmem_size - 1))) {
80 memory_size = lowmem_size;
Michal Simeka95d0e12009-03-27 14:25:29 +010081 PAGE_OFFSET = memory_start;
Michal Simek83a92522011-12-19 13:46:35 +010082 printk(KERN_INFO "%s: Main mem: 0x%x, "
Michal Simekdb6e3f92009-07-06 08:21:09 +020083 "size 0x%08x\n", __func__, (u32) memory_start,
Michal Simek83a92522011-12-19 13:46:35 +010084 (u32) memory_size);
Michal Simeka95d0e12009-03-27 14:25:29 +010085 break;
86 }
87 }
88
Michal Simek83a92522011-12-19 13:46:35 +010089 if (!memory_start || !memory_size) {
90 panic("%s: Missing memory setting 0x%08x, size=0x%08x\n",
91 __func__, (u32) memory_start, (u32) memory_size);
Michal Simeka95d0e12009-03-27 14:25:29 +010092 }
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;
Michal Simek83a92522011-12-19 13:46:35 +010098 printk(KERN_INFO "%s: kernel addr:0x%08x-0x%08x size=0x%08x\n",
Michal Simeka95d0e12009-03-27 14:25:29 +010099 __func__, kernel_align_start, kernel_align_start
100 + kernel_align_size, kernel_align_size);
Michal Simek83a92522011-12-19 13:46:35 +0100101 memblock_reserve(kernel_align_start, kernel_align_size);
Michal Simek4dc60832009-05-26 16:30:12 +0200102#endif
Michal Simeka95d0e12009-03-27 14:25:29 +0100103 /*
104 * Kernel:
105 * start: base phys address of kernel - page align
106 * end: base phys address of kernel - page align
107 *
108 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
109 * max_low_pfn
110 * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
111 * num_physpages - number of all pages
112 */
113
114 /* memory start is from the kernel end (aligned) to higher addr */
115 min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
116 /* RAM is assumed contiguous */
117 num_physpages = max_mapnr = memory_size >> PAGE_SHIFT;
Michal Simek83a92522011-12-19 13:46:35 +0100118 max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
119 max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
Michal Simeka95d0e12009-03-27 14:25:29 +0100120
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);
Michal Simek83a92522011-12-19 13:46:35 +0100124 printk(KERN_INFO "%s: max_pfn: %#lx\n", __func__, max_pfn);
Michal Simeka95d0e12009-03-27 14:25:29 +0100125
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 */
Michal Simeke0581662010-05-27 11:17:35 +0200133 map_size = init_bootmem_node(NODE_DATA(0),
Michal Simek8f37b6c2009-08-11 12:36:12 +0200134 PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000135 memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size);
Michal Simeka95d0e12009-03-27 14:25:29 +0100136
137 /* free bootmem is whole main memory */
Michal Simek83a92522011-12-19 13:46:35 +0100138 free_bootmem(memory_start, lowmem_size);
Michal Simeka95d0e12009-03-27 14:25:29 +0100139
140 /* reserve allocate blocks */
Benjamin Herrenschmidt76bfcc82010-08-04 14:13:06 +1000141 for_each_memblock(reserved, reg) {
Michal Simek83a92522011-12-19 13:46:35 +0100142 unsigned long top = reg->base + reg->size - 1;
143
144 pr_debug("reserved - 0x%08x-0x%08x, %lx, %lx\n",
145 (u32) reg->base, (u32) reg->size, top,
146 memory_start + lowmem_size - 1);
147
148 if (top <= (memory_start + lowmem_size - 1)) {
149 reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
150 } else if (reg->base < (memory_start + lowmem_size - 1)) {
151 unsigned long trunc_size = memory_start + lowmem_size -
152 reg->base;
153 reserve_bootmem(reg->base, trunc_size, BOOTMEM_DEFAULT);
154 }
Michal Simeka95d0e12009-03-27 14:25:29 +0100155 }
Michal Simek83a92522011-12-19 13:46:35 +0100156
Michal Simek4dc60832009-05-26 16:30:12 +0200157#ifdef CONFIG_MMU
158 init_bootmem_done = 1;
159#endif
Michal Simeka95d0e12009-03-27 14:25:29 +0100160 paging_init();
161}
162
163void free_init_pages(char *what, unsigned long begin, unsigned long end)
164{
165 unsigned long addr;
166
167 for (addr = begin; addr < end; addr += PAGE_SIZE) {
168 ClearPageReserved(virt_to_page(addr));
169 init_page_count(virt_to_page(addr));
Michal Simeka95d0e12009-03-27 14:25:29 +0100170 free_page(addr);
171 totalram_pages++;
172 }
173 printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
174}
175
176#ifdef CONFIG_BLK_DEV_INITRD
177void free_initrd_mem(unsigned long start, unsigned long end)
178{
179 int pages = 0;
180 for (; start < end; start += PAGE_SIZE) {
181 ClearPageReserved(virt_to_page(start));
182 init_page_count(virt_to_page(start));
183 free_page(start);
184 totalram_pages++;
185 pages++;
186 }
Lennart Sorensend6f61772009-09-17 11:47:06 -0400187 printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n",
188 (int)(pages * (PAGE_SIZE / 1024)));
Michal Simeka95d0e12009-03-27 14:25:29 +0100189}
190#endif
191
192void free_initmem(void)
193{
194 free_init_pages("unused kernel memory",
195 (unsigned long)(&__init_begin),
196 (unsigned long)(&__init_end));
197}
198
Michal Simeka95d0e12009-03-27 14:25:29 +0100199void __init mem_init(void)
200{
Michal Simek83a92522011-12-19 13:46:35 +0100201 high_memory = (void *)__va(memory_start + lowmem_size - 1);
202
Michal Simeka95d0e12009-03-27 14:25:29 +0100203 /* this will put all memory onto the freelists */
204 totalram_pages += free_all_bootmem();
205
206 printk(KERN_INFO "Memory: %luk/%luk available\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700207 nr_free_pages() << (PAGE_SHIFT-10),
Michal Simeka95d0e12009-03-27 14:25:29 +0100208 num_physpages << (PAGE_SHIFT-10));
Michal Simek4dc60832009-05-26 16:30:12 +0200209 mem_init_done = 1;
Michal Simeka95d0e12009-03-27 14:25:29 +0100210}
211
Michal Simek4dc60832009-05-26 16:30:12 +0200212#ifndef CONFIG_MMU
Michal Simek79bf3a12010-01-20 15:17:08 +0100213int page_is_ram(unsigned long pfn)
214{
215 return __range_ok(pfn, 0);
216}
Michal Simek4dc60832009-05-26 16:30:12 +0200217#else
218int page_is_ram(unsigned long pfn)
219{
220 return pfn < max_low_pfn;
221}
222
223/*
224 * Check for command-line options that affect what MMU_init will do.
225 */
226static void mm_cmdline_setup(void)
227{
228 unsigned long maxmem = 0;
229 char *p = cmd_line;
230
231 /* Look for mem= option on command line */
232 p = strstr(cmd_line, "mem=");
233 if (p) {
234 p += 4;
235 maxmem = memparse(p, &p);
236 if (maxmem && memory_size > maxmem) {
237 memory_size = maxmem;
Michal Simekda5ab112010-09-11 00:07:06 -0700238 memblock.memory.regions[0].size = memory_size;
Michal Simek4dc60832009-05-26 16:30:12 +0200239 }
240 }
241}
242
243/*
244 * MMU_init_hw does the chip-specific initialization of the MMU hardware.
245 */
246static void __init mmu_init_hw(void)
247{
248 /*
249 * The Zone Protection Register (ZPR) defines how protection will
250 * be applied to every page which is a member of a given zone. At
251 * present, we utilize only two of the zones.
252 * The zone index bits (of ZSEL) in the PTE are used for software
253 * indicators, except the LSB. For user access, zone 1 is used,
254 * for kernel access, zone 0 is used. We set all but zone 1
255 * to zero, allowing only kernel access as indicated in the PTE.
256 * For zone 1, we set a 01 binary (a value of 10 will not work)
257 * to allow user access as indicated in the PTE. This also allows
258 * kernel access as indicated in the PTE.
259 */
260 __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
261 "mts rzpr, r11;"
262 : : : "r11");
263}
264
265/*
266 * MMU_init sets up the basic memory mappings for the kernel,
267 * including both RAM and possibly some I/O regions,
268 * and sets up the page tables and the MMU hardware ready to go.
269 */
270
271/* called from head.S */
272asmlinkage void __init mmu_init(void)
273{
274 unsigned int kstart, ksize;
275
Yinghai Lu95f72d12010-07-12 14:36:09 +1000276 if (!memblock.reserved.cnt) {
Michal Simek4dc60832009-05-26 16:30:12 +0200277 printk(KERN_EMERG "Error memory count\n");
278 machine_restart(NULL);
279 }
280
Michal Simekda5ab112010-09-11 00:07:06 -0700281 if ((u32) memblock.memory.regions[0].size < 0x1000000) {
Michal Simek4dc60832009-05-26 16:30:12 +0200282 printk(KERN_EMERG "Memory must be greater than 16MB\n");
283 machine_restart(NULL);
284 }
285 /* Find main memory where the kernel is */
Michal Simekda5ab112010-09-11 00:07:06 -0700286 memory_start = (u32) memblock.memory.regions[0].base;
Michal Simek83a92522011-12-19 13:46:35 +0100287 lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
288
289 if (lowmem_size > CONFIG_LOWMEM_SIZE) {
290 lowmem_size = CONFIG_LOWMEM_SIZE;
291 memory_size = lowmem_size;
292 }
Michal Simek4dc60832009-05-26 16:30:12 +0200293
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));
Yinghai Lu95f72d12010-07-12 14:36:09 +1000303 memblock_reserve(kstart, ksize);
Michal Simek4dc60832009-05-26 16:30:12 +0200304
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
Paul Bolle6be9dd12011-10-12 21:28:36 +0200319#ifdef CONFIG_HIGHMEM_START_BOOL
320 ioremap_base = CONFIG_HIGHMEM_START;
Michal Simek4dc60832009-05-26 16:30:12 +0200321#else
322 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
Paul Bolle6be9dd12011-10-12 21:28:36 +0200323#endif /* CONFIG_HIGHMEM_START_BOOL */
Michal Simek4dc60832009-05-26 16:30:12 +0200324 ioremap_bot = ioremap_base;
Michal Simek4dc60832009-05-26 16:30:12 +0200325 /* Initialize the context management stuff */
326 mmu_context_init();
Michal Simek83a92522011-12-19 13:46:35 +0100327
328 /* Shortly after that, the entire linear mapping will be available */
329 /* This will also cause that unflatten device tree will be allocated
330 * inside 768MB limit */
331 memblock_set_current_limit(memory_start + lowmem_size - 1);
Michal Simek4dc60832009-05-26 16:30:12 +0200332}
333
334/* This is only called until mem_init is done. */
335void __init *early_get_page(void)
336{
337 void *p;
338 if (init_bootmem_done) {
339 p = alloc_bootmem_pages(PAGE_SIZE);
340 } else {
341 /*
342 * Mem start + 32MB -> here is limit
343 * because of mem mapping from head.S
344 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000345 p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
Michal Simek4dc60832009-05-26 16:30:12 +0200346 memory_start + 0x2000000));
347 }
348 return p;
349}
Michal Simeka84642a2010-01-14 17:03:49 +0100350
Michal Simek79bf3a12010-01-20 15:17:08 +0100351#endif /* CONFIG_MMU */
352
Michal Simeka84642a2010-01-14 17:03:49 +0100353void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask)
354{
355 if (mem_init_done)
356 return kmalloc(size, mask);
357 else
358 return alloc_bootmem(size);
359}
360
361void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask)
362{
363 void *p;
364
365 if (mem_init_done)
366 p = kzalloc(size, mask);
367 else {
368 p = alloc_bootmem(size);
369 if (p)
370 memset(p, 0, size);
371 }
372 return p;
373}