blob: 4ba51991c7de1472933e01260b017baa97e0c11a [file] [log] [blame]
GuanXuetaob50f1702011-01-15 18:16:59 +08001/*
2 * linux/arch/unicore32/mm/init.c
3 *
4 * Copyright (C) 2010 GUAN Xue-tao
5 *
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.
9 */
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/swap.h>
13#include <linux/init.h>
14#include <linux/bootmem.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/memblock.h>
21#include <linux/sort.h>
22#include <linux/dma-mapping.h>
Axel Lin5270f312011-10-18 16:51:46 +080023#include <linux/export.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080024
25#include <asm/sections.h>
26#include <asm/setup.h>
27#include <asm/sizes.h>
28#include <asm/tlb.h>
Tejun Heo1c16d242011-12-08 10:22:06 -080029#include <asm/memblock.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080030#include <mach/map.h>
31
32#include "mm.h"
33
34static unsigned long phys_initrd_start __initdata = 0x01000000;
35static unsigned long phys_initrd_size __initdata = SZ_8M;
36
37static int __init early_initrd(char *p)
38{
39 unsigned long start, size;
40 char *endp;
41
42 start = memparse(p, &endp);
43 if (*endp == ',') {
44 size = memparse(endp + 1, NULL);
45
46 phys_initrd_start = start;
47 phys_initrd_size = size;
48 }
49 return 0;
50}
51early_param("initrd", early_initrd);
52
53/*
54 * This keeps memory configuration data used by a couple memory
55 * initialization functions, as well as show_mem() for the skipping
56 * of holes in the memory map. It is populated by uc32_add_memory().
57 */
58struct meminfo meminfo;
59
GuanXuetaob50f1702011-01-15 18:16:59 +080060static void __init find_limits(unsigned long *min, unsigned long *max_low,
61 unsigned long *max_high)
62{
63 struct meminfo *mi = &meminfo;
64 int i;
65
66 *min = -1UL;
67 *max_low = *max_high = 0;
68
69 for_each_bank(i, mi) {
70 struct membank *bank = &mi->bank[i];
71 unsigned long start, end;
72
73 start = bank_pfn_start(bank);
74 end = bank_pfn_end(bank);
75
76 if (*min > start)
77 *min = start;
78 if (*max_high < end)
79 *max_high = end;
80 if (bank->highmem)
81 continue;
82 if (*max_low < end)
83 *max_low = end;
84 }
85}
86
GuanXuetaob50f1702011-01-15 18:16:59 +080087static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
88 unsigned long max_high)
89{
90 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
91 struct memblock_region *reg;
92
93 /*
94 * initialise the zones.
95 */
96 memset(zone_size, 0, sizeof(zone_size));
97
98 /*
99 * The memory size has already been determined. If we need
100 * to do anything fancy with the allocation of this memory
101 * to the zones, now is the time to do it.
102 */
103 zone_size[0] = max_low - min;
104
105 /*
106 * Calculate the size of the holes.
107 * holes = node_size - sum(bank_sizes)
108 */
109 memcpy(zhole_size, zone_size, sizeof(zhole_size));
110 for_each_memblock(memory, reg) {
111 unsigned long start = memblock_region_memory_base_pfn(reg);
112 unsigned long end = memblock_region_memory_end_pfn(reg);
113
114 if (start < max_low) {
115 unsigned long low_end = min(end, max_low);
116 zhole_size[0] -= low_end - start;
117 }
118 }
119
120 /*
121 * Adjust the sizes according to any special requirements for
122 * this machine type.
123 */
124 arch_adjust_zones(zone_size, zhole_size);
125
126 free_area_init_node(0, zone_size, min, zhole_size);
127}
128
129int pfn_valid(unsigned long pfn)
130{
131 return memblock_is_memory(pfn << PAGE_SHIFT);
132}
133EXPORT_SYMBOL(pfn_valid);
134
135static void uc32_memory_present(void)
136{
137}
138
139static int __init meminfo_cmp(const void *_a, const void *_b)
140{
141 const struct membank *a = _a, *b = _b;
142 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
143 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
144}
145
146void __init uc32_memblock_init(struct meminfo *mi)
147{
148 int i;
149
150 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
151 meminfo_cmp, NULL);
152
GuanXuetaob50f1702011-01-15 18:16:59 +0800153 for (i = 0; i < mi->nr_banks; i++)
154 memblock_add(mi->bank[i].start, mi->bank[i].size);
155
156 /* Register the kernel text, kernel data and initrd with memblock. */
157 memblock_reserve(__pa(_text), _end - _text);
158
159#ifdef CONFIG_BLK_DEV_INITRD
160 if (phys_initrd_size) {
161 memblock_reserve(phys_initrd_start, phys_initrd_size);
162
163 /* Now convert initrd to virtual addresses */
164 initrd_start = __phys_to_virt(phys_initrd_start);
165 initrd_end = initrd_start + phys_initrd_size;
166 }
167#endif
168
169 uc32_mm_memblock_reserve();
170
Tejun Heo1aadc052011-12-08 10:22:08 -0800171 memblock_allow_resize();
GuanXuetaob50f1702011-01-15 18:16:59 +0800172 memblock_dump_all();
173}
174
175void __init bootmem_init(void)
176{
177 unsigned long min, max_low, max_high;
178
179 max_low = max_high = 0;
180
181 find_limits(&min, &max_low, &max_high);
182
Mike Rapoporte92d39c2018-10-26 15:05:05 -0700183 node_set_online(0);
GuanXuetaob50f1702011-01-15 18:16:59 +0800184
GuanXuetaob50f1702011-01-15 18:16:59 +0800185 /*
186 * Sparsemem tries to allocate bootmem in memory_present(),
187 * so must be done after the fixed reservations
188 */
189 uc32_memory_present();
190
191 /*
192 * sparse_init() needs the bootmem allocator up and running.
193 */
194 sparse_init();
195
196 /*
197 * Now free the memory - free_area_init_node needs
198 * the sparse mem_map arrays initialized by sparse_init()
199 * for memmap_init_zone(), otherwise all PFNs are invalid.
200 */
201 uc32_bootmem_free(min, max_low, max_high);
202
203 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
204
205 /*
206 * This doesn't seem to be used by the Linux memory manager any
207 * more, but is used by ll_rw_block. If we can get rid of it, we
208 * also get rid of some of the stuff above as well.
209 *
210 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
211 * the system, not the maximum PFN.
212 */
213 max_low_pfn = max_low - PHYS_PFN_OFFSET;
214 max_pfn = max_high - PHYS_PFN_OFFSET;
215}
216
GuanXuetaob50f1702011-01-15 18:16:59 +0800217static inline void
218free_memmap(unsigned long start_pfn, unsigned long end_pfn)
219{
220 struct page *start_pg, *end_pg;
221 unsigned long pg, pgend;
222
223 /*
224 * Convert start_pfn/end_pfn to a struct page pointer.
225 */
226 start_pg = pfn_to_page(start_pfn - 1) + 1;
227 end_pg = pfn_to_page(end_pfn);
228
229 /*
230 * Convert to physical addresses, and
231 * round start upwards and end downwards.
232 */
233 pg = PAGE_ALIGN(__pa(start_pg));
234 pgend = __pa(end_pg) & PAGE_MASK;
235
236 /*
237 * If there are free pages between these,
238 * free the section of the memmap array.
239 */
240 if (pg < pgend)
Mike Rapoport20132882018-10-30 15:09:21 -0700241 memblock_free(pg, pgend - pg);
GuanXuetaob50f1702011-01-15 18:16:59 +0800242}
243
244/*
245 * The mem_map array can get very big. Free the unused area of the memory map.
246 */
247static void __init free_unused_memmap(struct meminfo *mi)
248{
249 unsigned long bank_start, prev_bank_end = 0;
250 unsigned int i;
251
252 /*
253 * This relies on each bank being in address order.
254 * The banks are sorted previously in bootmem_init().
255 */
256 for_each_bank(i, mi) {
257 struct membank *bank = &mi->bank[i];
258
259 bank_start = bank_pfn_start(bank);
260
261 /*
262 * If we had a previous bank, and there is a space
263 * between the current bank and the previous, free it.
264 */
265 if (prev_bank_end && prev_bank_end < bank_start)
266 free_memmap(prev_bank_end, bank_start);
267
268 /*
269 * Align up here since the VM subsystem insists that the
270 * memmap entries are valid from the bank end aligned to
271 * MAX_ORDER_NR_PAGES.
272 */
273 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
274 }
275}
276
277/*
278 * mem_init() marks the free areas in the mem_map and tells us how much
279 * memory is free. This is done after various parts of the system have
280 * claimed their memory after the kernel image.
281 */
282void __init mem_init(void)
283{
GuanXuetaob50f1702011-01-15 18:16:59 +0800284 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
285
GuanXuetaob50f1702011-01-15 18:16:59 +0800286 free_unused_memmap(&meminfo);
287
Jiang Liuccecb512013-04-29 15:06:52 -0700288 /* this will put all unused low memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -0700289 free_all_bootmem();
GuanXuetaob50f1702011-01-15 18:16:59 +0800290
Jiang Liu0d0b6d22013-07-03 15:04:17 -0700291 mem_init_print_info(NULL);
GuanXuetaob50f1702011-01-15 18:16:59 +0800292 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
293 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
294 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
295 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
296 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
297 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
298 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
299 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
300
301 VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
302 DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
303 VMALLOC_START, VMALLOC_END,
304 DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
305 PAGE_OFFSET, (unsigned long)high_memory,
306 DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
307 MODULES_VADDR, MODULES_END,
308 DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
309
310 __init_begin, __init_end,
311 DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
312 _stext, _etext,
313 DIV_ROUND_UP((_etext - _stext), SZ_1K),
314 _sdata, _edata,
315 DIV_ROUND_UP((_edata - _sdata), SZ_1K));
316
317 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
318 BUG_ON(TASK_SIZE > MODULES_VADDR);
319
Jiang Liu0d0b6d22013-07-03 15:04:17 -0700320 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
GuanXuetaob50f1702011-01-15 18:16:59 +0800321 /*
322 * On a machine this small we won't get
323 * anywhere without overcommit, so turn
324 * it on by default.
325 */
326 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
327 }
328}
329
330void free_initmem(void)
331{
Jiang Liudbe67df2013-07-03 15:02:51 -0700332 free_initmem_default(-1);
GuanXuetaob50f1702011-01-15 18:16:59 +0800333}
334
335#ifdef CONFIG_BLK_DEV_INITRD
336
337static int keep_initrd;
338
339void free_initrd_mem(unsigned long start, unsigned long end)
340{
341 if (!keep_initrd)
Jiang Liudbe67df2013-07-03 15:02:51 -0700342 free_reserved_area((void *)start, (void *)end, -1, "initrd");
GuanXuetaob50f1702011-01-15 18:16:59 +0800343}
344
345static int __init keepinitrd_setup(char *__unused)
346{
347 keep_initrd = 1;
348 return 1;
349}
350
351__setup("keepinitrd", keepinitrd_setup);
352#endif