GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / unicore32 / mm / init.c
1 /*
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>
23 #include <linux/export.h>
24
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/sizes.h>
28 #include <asm/tlb.h>
29 #include <asm/memblock.h>
30 #include <mach/map.h>
31
32 #include "mm.h"
33
34 static unsigned long phys_initrd_start __initdata = 0x01000000;
35 static unsigned long phys_initrd_size __initdata = SZ_8M;
36
37 static 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 }
51 early_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  */
58 struct meminfo meminfo;
59
60 static 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
87 static void __init uc32_bootmem_init(unsigned long start_pfn,
88         unsigned long end_pfn)
89 {
90         struct memblock_region *reg;
91         unsigned int boot_pages;
92         phys_addr_t bitmap;
93         pg_data_t *pgdat;
94
95         /*
96          * Allocate the bootmem bitmap page.  This must be in a region
97          * of memory which has already been mapped.
98          */
99         boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
100         bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
101                                 __pfn_to_phys(end_pfn));
102
103         /*
104          * Initialise the bootmem allocator, handing the
105          * memory banks over to bootmem.
106          */
107         node_set_online(0);
108         pgdat = NODE_DATA(0);
109         init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
110
111         /* Free the lowmem regions from memblock into bootmem. */
112         for_each_memblock(memory, reg) {
113                 unsigned long start = memblock_region_memory_base_pfn(reg);
114                 unsigned long end = memblock_region_memory_end_pfn(reg);
115
116                 if (end >= end_pfn)
117                         end = end_pfn;
118                 if (start >= end)
119                         break;
120
121                 free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
122         }
123
124         /* Reserve the lowmem memblock reserved regions in bootmem. */
125         for_each_memblock(reserved, reg) {
126                 unsigned long start = memblock_region_reserved_base_pfn(reg);
127                 unsigned long end = memblock_region_reserved_end_pfn(reg);
128
129                 if (end >= end_pfn)
130                         end = end_pfn;
131                 if (start >= end)
132                         break;
133
134                 reserve_bootmem(__pfn_to_phys(start),
135                         (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
136         }
137 }
138
139 static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
140         unsigned long max_high)
141 {
142         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
143         struct memblock_region *reg;
144
145         /*
146          * initialise the zones.
147          */
148         memset(zone_size, 0, sizeof(zone_size));
149
150         /*
151          * The memory size has already been determined.  If we need
152          * to do anything fancy with the allocation of this memory
153          * to the zones, now is the time to do it.
154          */
155         zone_size[0] = max_low - min;
156
157         /*
158          * Calculate the size of the holes.
159          *  holes = node_size - sum(bank_sizes)
160          */
161         memcpy(zhole_size, zone_size, sizeof(zhole_size));
162         for_each_memblock(memory, reg) {
163                 unsigned long start = memblock_region_memory_base_pfn(reg);
164                 unsigned long end = memblock_region_memory_end_pfn(reg);
165
166                 if (start < max_low) {
167                         unsigned long low_end = min(end, max_low);
168                         zhole_size[0] -= low_end - start;
169                 }
170         }
171
172         /*
173          * Adjust the sizes according to any special requirements for
174          * this machine type.
175          */
176         arch_adjust_zones(zone_size, zhole_size);
177
178         free_area_init_node(0, zone_size, min, zhole_size);
179 }
180
181 int pfn_valid(unsigned long pfn)
182 {
183         return memblock_is_memory(pfn << PAGE_SHIFT);
184 }
185 EXPORT_SYMBOL(pfn_valid);
186
187 static void uc32_memory_present(void)
188 {
189 }
190
191 static int __init meminfo_cmp(const void *_a, const void *_b)
192 {
193         const struct membank *a = _a, *b = _b;
194         long cmp = bank_pfn_start(a) - bank_pfn_start(b);
195         return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
196 }
197
198 void __init uc32_memblock_init(struct meminfo *mi)
199 {
200         int i;
201
202         sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
203                 meminfo_cmp, NULL);
204
205         for (i = 0; i < mi->nr_banks; i++)
206                 memblock_add(mi->bank[i].start, mi->bank[i].size);
207
208         /* Register the kernel text, kernel data and initrd with memblock. */
209         memblock_reserve(__pa(_text), _end - _text);
210
211 #ifdef CONFIG_BLK_DEV_INITRD
212         if (phys_initrd_size) {
213                 memblock_reserve(phys_initrd_start, phys_initrd_size);
214
215                 /* Now convert initrd to virtual addresses */
216                 initrd_start = __phys_to_virt(phys_initrd_start);
217                 initrd_end = initrd_start + phys_initrd_size;
218         }
219 #endif
220
221         uc32_mm_memblock_reserve();
222
223         memblock_allow_resize();
224         memblock_dump_all();
225 }
226
227 void __init bootmem_init(void)
228 {
229         unsigned long min, max_low, max_high;
230
231         max_low = max_high = 0;
232
233         find_limits(&min, &max_low, &max_high);
234
235         uc32_bootmem_init(min, max_low);
236
237 #ifdef CONFIG_SWIOTLB
238         swiotlb_init(1);
239 #endif
240         /*
241          * Sparsemem tries to allocate bootmem in memory_present(),
242          * so must be done after the fixed reservations
243          */
244         uc32_memory_present();
245
246         /*
247          * sparse_init() needs the bootmem allocator up and running.
248          */
249         sparse_init();
250
251         /*
252          * Now free the memory - free_area_init_node needs
253          * the sparse mem_map arrays initialized by sparse_init()
254          * for memmap_init_zone(), otherwise all PFNs are invalid.
255          */
256         uc32_bootmem_free(min, max_low, max_high);
257
258         high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
259
260         /*
261          * This doesn't seem to be used by the Linux memory manager any
262          * more, but is used by ll_rw_block.  If we can get rid of it, we
263          * also get rid of some of the stuff above as well.
264          *
265          * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
266          * the system, not the maximum PFN.
267          */
268         max_low_pfn = max_low - PHYS_PFN_OFFSET;
269         max_pfn = max_high - PHYS_PFN_OFFSET;
270 }
271
272 static inline void
273 free_memmap(unsigned long start_pfn, unsigned long end_pfn)
274 {
275         struct page *start_pg, *end_pg;
276         unsigned long pg, pgend;
277
278         /*
279          * Convert start_pfn/end_pfn to a struct page pointer.
280          */
281         start_pg = pfn_to_page(start_pfn - 1) + 1;
282         end_pg = pfn_to_page(end_pfn);
283
284         /*
285          * Convert to physical addresses, and
286          * round start upwards and end downwards.
287          */
288         pg = PAGE_ALIGN(__pa(start_pg));
289         pgend = __pa(end_pg) & PAGE_MASK;
290
291         /*
292          * If there are free pages between these,
293          * free the section of the memmap array.
294          */
295         if (pg < pgend)
296                 free_bootmem(pg, pgend - pg);
297 }
298
299 /*
300  * The mem_map array can get very big.  Free the unused area of the memory map.
301  */
302 static void __init free_unused_memmap(struct meminfo *mi)
303 {
304         unsigned long bank_start, prev_bank_end = 0;
305         unsigned int i;
306
307         /*
308          * This relies on each bank being in address order.
309          * The banks are sorted previously in bootmem_init().
310          */
311         for_each_bank(i, mi) {
312                 struct membank *bank = &mi->bank[i];
313
314                 bank_start = bank_pfn_start(bank);
315
316                 /*
317                  * If we had a previous bank, and there is a space
318                  * between the current bank and the previous, free it.
319                  */
320                 if (prev_bank_end && prev_bank_end < bank_start)
321                         free_memmap(prev_bank_end, bank_start);
322
323                 /*
324                  * Align up here since the VM subsystem insists that the
325                  * memmap entries are valid from the bank end aligned to
326                  * MAX_ORDER_NR_PAGES.
327                  */
328                 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
329         }
330 }
331
332 /*
333  * mem_init() marks the free areas in the mem_map and tells us how much
334  * memory is free.  This is done after various parts of the system have
335  * claimed their memory after the kernel image.
336  */
337 void __init mem_init(void)
338 {
339         max_mapnr   = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
340
341         free_unused_memmap(&meminfo);
342
343         /* this will put all unused low memory onto the freelists */
344         free_all_bootmem();
345
346         mem_init_print_info(NULL);
347         printk(KERN_NOTICE "Virtual kernel memory layout:\n"
348                 "    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
349                 "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
350                 "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
351                 "    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
352                 "      .init : 0x%p" " - 0x%p" "   (%4d kB)\n"
353                 "      .text : 0x%p" " - 0x%p" "   (%4d kB)\n"
354                 "      .data : 0x%p" " - 0x%p" "   (%4d kB)\n",
355
356                 VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
357                 DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
358                 VMALLOC_START, VMALLOC_END,
359                 DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
360                 PAGE_OFFSET, (unsigned long)high_memory,
361                 DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
362                 MODULES_VADDR, MODULES_END,
363                 DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
364
365                 __init_begin, __init_end,
366                 DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
367                 _stext, _etext,
368                 DIV_ROUND_UP((_etext - _stext), SZ_1K),
369                 _sdata, _edata,
370                 DIV_ROUND_UP((_edata - _sdata), SZ_1K));
371
372         BUILD_BUG_ON(TASK_SIZE                          > MODULES_VADDR);
373         BUG_ON(TASK_SIZE                                > MODULES_VADDR);
374
375         if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
376                 /*
377                  * On a machine this small we won't get
378                  * anywhere without overcommit, so turn
379                  * it on by default.
380                  */
381                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
382         }
383 }
384
385 void free_initmem(void)
386 {
387         free_initmem_default(-1);
388 }
389
390 #ifdef CONFIG_BLK_DEV_INITRD
391
392 static int keep_initrd;
393
394 void free_initrd_mem(unsigned long start, unsigned long end)
395 {
396         if (!keep_initrd)
397                 free_reserved_area((void *)start, (void *)end, -1, "initrd");
398 }
399
400 static int __init keepinitrd_setup(char *__unused)
401 {
402         keep_initrd = 1;
403         return 1;
404 }
405
406 __setup("keepinitrd", keepinitrd_setup);
407 #endif