2 * linux/arch/unicore32/mm/init.c
4 * Copyright (C) 2010 GUAN Xue-tao
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/memblock.h>
15 #include <linux/mman.h>
16 #include <linux/nodemask.h>
17 #include <linux/initrd.h>
18 #include <linux/highmem.h>
19 #include <linux/gfp.h>
20 #include <linux/sort.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/export.h>
24 #include <asm/sections.h>
25 #include <asm/setup.h>
26 #include <asm/sizes.h>
28 #include <asm/memblock.h>
33 static unsigned long phys_initrd_start __initdata = 0x01000000;
34 static unsigned long phys_initrd_size __initdata = SZ_8M;
36 static int __init early_initrd(char *p)
38 unsigned long start, size;
41 start = memparse(p, &endp);
43 size = memparse(endp + 1, NULL);
45 phys_initrd_start = start;
46 phys_initrd_size = size;
50 early_param("initrd", early_initrd);
53 * This keeps memory configuration data used by a couple memory
54 * initialization functions, as well as show_mem() for the skipping
55 * of holes in the memory map. It is populated by uc32_add_memory().
57 struct meminfo meminfo;
59 static void __init find_limits(unsigned long *min, unsigned long *max_low,
60 unsigned long *max_high)
62 struct meminfo *mi = &meminfo;
66 *max_low = *max_high = 0;
68 for_each_bank(i, mi) {
69 struct membank *bank = &mi->bank[i];
70 unsigned long start, end;
72 start = bank_pfn_start(bank);
73 end = bank_pfn_end(bank);
86 static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
87 unsigned long max_high)
89 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
90 struct memblock_region *reg;
93 * initialise the zones.
95 memset(zone_size, 0, sizeof(zone_size));
98 * The memory size has already been determined. If we need
99 * to do anything fancy with the allocation of this memory
100 * to the zones, now is the time to do it.
102 zone_size[0] = max_low - min;
105 * Calculate the size of the holes.
106 * holes = node_size - sum(bank_sizes)
108 memcpy(zhole_size, zone_size, sizeof(zhole_size));
109 for_each_memblock(memory, reg) {
110 unsigned long start = memblock_region_memory_base_pfn(reg);
111 unsigned long end = memblock_region_memory_end_pfn(reg);
113 if (start < max_low) {
114 unsigned long low_end = min(end, max_low);
115 zhole_size[0] -= low_end - start;
120 * Adjust the sizes according to any special requirements for
123 arch_adjust_zones(zone_size, zhole_size);
125 free_area_init_node(0, zone_size, min, zhole_size);
128 int pfn_valid(unsigned long pfn)
130 return memblock_is_memory(pfn << PAGE_SHIFT);
132 EXPORT_SYMBOL(pfn_valid);
134 static void uc32_memory_present(void)
138 static int __init meminfo_cmp(const void *_a, const void *_b)
140 const struct membank *a = _a, *b = _b;
141 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
142 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
145 void __init uc32_memblock_init(struct meminfo *mi)
149 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
152 for (i = 0; i < mi->nr_banks; i++)
153 memblock_add(mi->bank[i].start, mi->bank[i].size);
155 /* Register the kernel text, kernel data and initrd with memblock. */
156 memblock_reserve(__pa(_text), _end - _text);
158 #ifdef CONFIG_BLK_DEV_INITRD
159 if (phys_initrd_size) {
160 memblock_reserve(phys_initrd_start, phys_initrd_size);
162 /* Now convert initrd to virtual addresses */
163 initrd_start = __phys_to_virt(phys_initrd_start);
164 initrd_end = initrd_start + phys_initrd_size;
168 uc32_mm_memblock_reserve();
170 memblock_allow_resize();
174 void __init bootmem_init(void)
176 unsigned long min, max_low, max_high;
178 max_low = max_high = 0;
180 find_limits(&min, &max_low, &max_high);
185 * Sparsemem tries to allocate bootmem in memory_present(),
186 * so must be done after the fixed reservations
188 uc32_memory_present();
191 * sparse_init() needs the bootmem allocator up and running.
196 * Now free the memory - free_area_init_node needs
197 * the sparse mem_map arrays initialized by sparse_init()
198 * for memmap_init_zone(), otherwise all PFNs are invalid.
200 uc32_bootmem_free(min, max_low, max_high);
202 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
205 * This doesn't seem to be used by the Linux memory manager any
206 * more, but is used by ll_rw_block. If we can get rid of it, we
207 * also get rid of some of the stuff above as well.
209 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
210 * the system, not the maximum PFN.
212 max_low_pfn = max_low - PHYS_PFN_OFFSET;
213 max_pfn = max_high - PHYS_PFN_OFFSET;
217 free_memmap(unsigned long start_pfn, unsigned long end_pfn)
219 struct page *start_pg, *end_pg;
220 unsigned long pg, pgend;
223 * Convert start_pfn/end_pfn to a struct page pointer.
225 start_pg = pfn_to_page(start_pfn - 1) + 1;
226 end_pg = pfn_to_page(end_pfn);
229 * Convert to physical addresses, and
230 * round start upwards and end downwards.
232 pg = PAGE_ALIGN(__pa(start_pg));
233 pgend = __pa(end_pg) & PAGE_MASK;
236 * If there are free pages between these,
237 * free the section of the memmap array.
240 memblock_free(pg, pgend - pg);
244 * The mem_map array can get very big. Free the unused area of the memory map.
246 static void __init free_unused_memmap(struct meminfo *mi)
248 unsigned long bank_start, prev_bank_end = 0;
252 * This relies on each bank being in address order.
253 * The banks are sorted previously in bootmem_init().
255 for_each_bank(i, mi) {
256 struct membank *bank = &mi->bank[i];
258 bank_start = bank_pfn_start(bank);
261 * If we had a previous bank, and there is a space
262 * between the current bank and the previous, free it.
264 if (prev_bank_end && prev_bank_end < bank_start)
265 free_memmap(prev_bank_end, bank_start);
268 * Align up here since the VM subsystem insists that the
269 * memmap entries are valid from the bank end aligned to
270 * MAX_ORDER_NR_PAGES.
272 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
277 * mem_init() marks the free areas in the mem_map and tells us how much
278 * memory is free. This is done after various parts of the system have
279 * claimed their memory after the kernel image.
281 void __init mem_init(void)
283 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
285 free_unused_memmap(&meminfo);
287 /* this will put all unused low memory onto the freelists */
290 mem_init_print_info(NULL);
291 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
292 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
293 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
294 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
295 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
296 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
297 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
298 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
300 VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
301 DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
302 VMALLOC_START, VMALLOC_END,
303 DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
304 PAGE_OFFSET, (unsigned long)high_memory,
305 DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
306 MODULES_VADDR, MODULES_END,
307 DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
309 __init_begin, __init_end,
310 DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
312 DIV_ROUND_UP((_etext - _stext), SZ_1K),
314 DIV_ROUND_UP((_edata - _sdata), SZ_1K));
316 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
317 BUG_ON(TASK_SIZE > MODULES_VADDR);
319 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
321 * On a machine this small we won't get
322 * anywhere without overcommit, so turn
325 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
329 void free_initmem(void)
331 free_initmem_default(-1);
334 #ifdef CONFIG_BLK_DEV_INITRD
336 static int keep_initrd;
338 void free_initrd_mem(unsigned long start, unsigned long end)
341 free_reserved_area((void *)start, (void *)end, -1, "initrd");
344 static int __init keepinitrd_setup(char *__unused)
350 __setup("keepinitrd", keepinitrd_setup);