blob: c994cdf1411957f55eb78cae1ea2e29399106423 [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>
Mike Rapoport57c8a662018-10-30 15:09:49 -070014#include <linux/memblock.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080015#include <linux/mman.h>
16#include <linux/nodemask.h>
17#include <linux/initrd.h>
18#include <linux/highmem.h>
19#include <linux/gfp.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080020#include <linux/sort.h>
21#include <linux/dma-mapping.h>
Axel Lin5270f312011-10-18 16:51:46 +080022#include <linux/export.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080023
24#include <asm/sections.h>
25#include <asm/setup.h>
Masahiro Yamada87dfb312019-05-14 15:46:51 -070026#include <linux/sizes.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080027#include <asm/tlb.h>
Tejun Heo1c16d242011-12-08 10:22:06 -080028#include <asm/memblock.h>
GuanXuetaob50f1702011-01-15 18:16:59 +080029#include <mach/map.h>
30
31#include "mm.h"
32
GuanXuetaob50f1702011-01-15 18:16:59 +080033/*
34 * This keeps memory configuration data used by a couple memory
35 * initialization functions, as well as show_mem() for the skipping
36 * of holes in the memory map. It is populated by uc32_add_memory().
37 */
38struct meminfo meminfo;
39
GuanXuetaob50f1702011-01-15 18:16:59 +080040static void __init find_limits(unsigned long *min, unsigned long *max_low,
41 unsigned long *max_high)
42{
43 struct meminfo *mi = &meminfo;
44 int i;
45
46 *min = -1UL;
47 *max_low = *max_high = 0;
48
49 for_each_bank(i, mi) {
50 struct membank *bank = &mi->bank[i];
51 unsigned long start, end;
52
53 start = bank_pfn_start(bank);
54 end = bank_pfn_end(bank);
55
56 if (*min > start)
57 *min = start;
58 if (*max_high < end)
59 *max_high = end;
60 if (bank->highmem)
61 continue;
62 if (*max_low < end)
63 *max_low = end;
64 }
65}
66
GuanXuetaob50f1702011-01-15 18:16:59 +080067static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
68 unsigned long max_high)
69{
70 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
71 struct memblock_region *reg;
72
73 /*
74 * initialise the zones.
75 */
76 memset(zone_size, 0, sizeof(zone_size));
77
78 /*
79 * The memory size has already been determined. If we need
80 * to do anything fancy with the allocation of this memory
81 * to the zones, now is the time to do it.
82 */
83 zone_size[0] = max_low - min;
84
85 /*
86 * Calculate the size of the holes.
87 * holes = node_size - sum(bank_sizes)
88 */
89 memcpy(zhole_size, zone_size, sizeof(zhole_size));
90 for_each_memblock(memory, reg) {
91 unsigned long start = memblock_region_memory_base_pfn(reg);
92 unsigned long end = memblock_region_memory_end_pfn(reg);
93
94 if (start < max_low) {
95 unsigned long low_end = min(end, max_low);
96 zhole_size[0] -= low_end - start;
97 }
98 }
99
100 /*
101 * Adjust the sizes according to any special requirements for
102 * this machine type.
103 */
104 arch_adjust_zones(zone_size, zhole_size);
105
106 free_area_init_node(0, zone_size, min, zhole_size);
107}
108
109int pfn_valid(unsigned long pfn)
110{
111 return memblock_is_memory(pfn << PAGE_SHIFT);
112}
113EXPORT_SYMBOL(pfn_valid);
114
115static void uc32_memory_present(void)
116{
117}
118
119static int __init meminfo_cmp(const void *_a, const void *_b)
120{
121 const struct membank *a = _a, *b = _b;
122 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
123 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
124}
125
126void __init uc32_memblock_init(struct meminfo *mi)
127{
128 int i;
129
130 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
131 meminfo_cmp, NULL);
132
GuanXuetaob50f1702011-01-15 18:16:59 +0800133 for (i = 0; i < mi->nr_banks; i++)
134 memblock_add(mi->bank[i].start, mi->bank[i].size);
135
136 /* Register the kernel text, kernel data and initrd with memblock. */
137 memblock_reserve(__pa(_text), _end - _text);
138
139#ifdef CONFIG_BLK_DEV_INITRD
Florian Fainellib1ab95c2018-11-05 14:54:27 -0800140 if (!phys_initrd_size) {
141 phys_initrd_start = 0x01000000;
142 phys_initrd_size = SZ_8M;
143 }
144
GuanXuetaob50f1702011-01-15 18:16:59 +0800145 if (phys_initrd_size) {
146 memblock_reserve(phys_initrd_start, phys_initrd_size);
147
148 /* Now convert initrd to virtual addresses */
149 initrd_start = __phys_to_virt(phys_initrd_start);
150 initrd_end = initrd_start + phys_initrd_size;
151 }
152#endif
153
154 uc32_mm_memblock_reserve();
155
Tejun Heo1aadc052011-12-08 10:22:08 -0800156 memblock_allow_resize();
GuanXuetaob50f1702011-01-15 18:16:59 +0800157 memblock_dump_all();
158}
159
160void __init bootmem_init(void)
161{
162 unsigned long min, max_low, max_high;
163
164 max_low = max_high = 0;
165
166 find_limits(&min, &max_low, &max_high);
167
Mike Rapoporte92d39c2018-10-26 15:05:05 -0700168 node_set_online(0);
GuanXuetaob50f1702011-01-15 18:16:59 +0800169
GuanXuetaob50f1702011-01-15 18:16:59 +0800170 /*
171 * Sparsemem tries to allocate bootmem in memory_present(),
172 * so must be done after the fixed reservations
173 */
174 uc32_memory_present();
175
176 /*
177 * sparse_init() needs the bootmem allocator up and running.
178 */
179 sparse_init();
180
181 /*
182 * Now free the memory - free_area_init_node needs
183 * the sparse mem_map arrays initialized by sparse_init()
184 * for memmap_init_zone(), otherwise all PFNs are invalid.
185 */
186 uc32_bootmem_free(min, max_low, max_high);
187
188 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
189
190 /*
191 * This doesn't seem to be used by the Linux memory manager any
192 * more, but is used by ll_rw_block. If we can get rid of it, we
193 * also get rid of some of the stuff above as well.
194 *
195 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
196 * the system, not the maximum PFN.
197 */
198 max_low_pfn = max_low - PHYS_PFN_OFFSET;
199 max_pfn = max_high - PHYS_PFN_OFFSET;
200}
201
GuanXuetaob50f1702011-01-15 18:16:59 +0800202static inline void
203free_memmap(unsigned long start_pfn, unsigned long end_pfn)
204{
205 struct page *start_pg, *end_pg;
206 unsigned long pg, pgend;
207
208 /*
209 * Convert start_pfn/end_pfn to a struct page pointer.
210 */
211 start_pg = pfn_to_page(start_pfn - 1) + 1;
212 end_pg = pfn_to_page(end_pfn);
213
214 /*
215 * Convert to physical addresses, and
216 * round start upwards and end downwards.
217 */
218 pg = PAGE_ALIGN(__pa(start_pg));
219 pgend = __pa(end_pg) & PAGE_MASK;
220
221 /*
222 * If there are free pages between these,
223 * free the section of the memmap array.
224 */
225 if (pg < pgend)
Mike Rapoport20132882018-10-30 15:09:21 -0700226 memblock_free(pg, pgend - pg);
GuanXuetaob50f1702011-01-15 18:16:59 +0800227}
228
229/*
230 * The mem_map array can get very big. Free the unused area of the memory map.
231 */
232static void __init free_unused_memmap(struct meminfo *mi)
233{
234 unsigned long bank_start, prev_bank_end = 0;
235 unsigned int i;
236
237 /*
238 * This relies on each bank being in address order.
239 * The banks are sorted previously in bootmem_init().
240 */
241 for_each_bank(i, mi) {
242 struct membank *bank = &mi->bank[i];
243
244 bank_start = bank_pfn_start(bank);
245
246 /*
247 * If we had a previous bank, and there is a space
248 * between the current bank and the previous, free it.
249 */
250 if (prev_bank_end && prev_bank_end < bank_start)
251 free_memmap(prev_bank_end, bank_start);
252
253 /*
254 * Align up here since the VM subsystem insists that the
255 * memmap entries are valid from the bank end aligned to
256 * MAX_ORDER_NR_PAGES.
257 */
258 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
259 }
260}
261
262/*
263 * mem_init() marks the free areas in the mem_map and tells us how much
264 * memory is free. This is done after various parts of the system have
265 * claimed their memory after the kernel image.
266 */
267void __init mem_init(void)
268{
GuanXuetaob50f1702011-01-15 18:16:59 +0800269 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
270
GuanXuetaob50f1702011-01-15 18:16:59 +0800271 free_unused_memmap(&meminfo);
272
Jiang Liuccecb512013-04-29 15:06:52 -0700273 /* this will put all unused low memory onto the freelists */
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -0700274 memblock_free_all();
GuanXuetaob50f1702011-01-15 18:16:59 +0800275
Jiang Liu0d0b6d22013-07-03 15:04:17 -0700276 mem_init_print_info(NULL);
GuanXuetaob50f1702011-01-15 18:16:59 +0800277
278 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
279 BUG_ON(TASK_SIZE > MODULES_VADDR);
280
Jiang Liu0d0b6d22013-07-03 15:04:17 -0700281 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
GuanXuetaob50f1702011-01-15 18:16:59 +0800282 /*
283 * On a machine this small we won't get
284 * anywhere without overcommit, so turn
285 * it on by default.
286 */
287 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
288 }
289}