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>
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().
38 struct meminfo meminfo
;
40 static void __init
find_limits(unsigned long *min
, unsigned long *max_low
,
41 unsigned long *max_high
)
43 struct meminfo
*mi
= &meminfo
;
47 *max_low
= *max_high
= 0;
49 for_each_bank(i
, mi
) {
50 struct membank
*bank
= &mi
->bank
[i
];
51 unsigned long start
, end
;
53 start
= bank_pfn_start(bank
);
54 end
= bank_pfn_end(bank
);
67 static void __init
uc32_bootmem_free(unsigned long min
, unsigned long max_low
,
68 unsigned long max_high
)
70 unsigned long zone_size
[MAX_NR_ZONES
], zhole_size
[MAX_NR_ZONES
];
71 struct memblock_region
*reg
;
74 * initialise the zones.
76 memset(zone_size
, 0, sizeof(zone_size
));
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.
83 zone_size
[0] = max_low
- min
;
86 * Calculate the size of the holes.
87 * holes = node_size - sum(bank_sizes)
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
);
94 if (start
< max_low
) {
95 unsigned long low_end
= min(end
, max_low
);
96 zhole_size
[0] -= low_end
- start
;
101 * Adjust the sizes according to any special requirements for
104 arch_adjust_zones(zone_size
, zhole_size
);
106 free_area_init_node(0, zone_size
, min
, zhole_size
);
109 int pfn_valid(unsigned long pfn
)
111 return memblock_is_memory(pfn
<< PAGE_SHIFT
);
113 EXPORT_SYMBOL(pfn_valid
);
115 static void uc32_memory_present(void)
119 static int __init
meminfo_cmp(const void *_a
, const void *_b
)
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;
126 void __init
uc32_memblock_init(struct meminfo
*mi
)
130 sort(&meminfo
.bank
, meminfo
.nr_banks
, sizeof(meminfo
.bank
[0]),
133 for (i
= 0; i
< mi
->nr_banks
; i
++)
134 memblock_add(mi
->bank
[i
].start
, mi
->bank
[i
].size
);
136 /* Register the kernel text, kernel data and initrd with memblock. */
137 memblock_reserve(__pa(_text
), _end
- _text
);
139 #ifdef CONFIG_BLK_DEV_INITRD
140 if (!phys_initrd_size
) {
141 phys_initrd_start
= 0x01000000;
142 phys_initrd_size
= SZ_8M
;
145 if (phys_initrd_size
) {
146 memblock_reserve(phys_initrd_start
, phys_initrd_size
);
148 /* Now convert initrd to virtual addresses */
149 initrd_start
= __phys_to_virt(phys_initrd_start
);
150 initrd_end
= initrd_start
+ phys_initrd_size
;
154 uc32_mm_memblock_reserve();
156 memblock_allow_resize();
160 void __init
bootmem_init(void)
162 unsigned long min
, max_low
, max_high
;
164 max_low
= max_high
= 0;
166 find_limits(&min
, &max_low
, &max_high
);
171 * Sparsemem tries to allocate bootmem in memory_present(),
172 * so must be done after the fixed reservations
174 uc32_memory_present();
177 * sparse_init() needs the bootmem allocator up and running.
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.
186 uc32_bootmem_free(min
, max_low
, max_high
);
188 high_memory
= __va((max_low
<< PAGE_SHIFT
) - 1) + 1;
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.
195 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
196 * the system, not the maximum PFN.
198 max_low_pfn
= max_low
- PHYS_PFN_OFFSET
;
199 max_pfn
= max_high
- PHYS_PFN_OFFSET
;
203 free_memmap(unsigned long start_pfn
, unsigned long end_pfn
)
205 struct page
*start_pg
, *end_pg
;
206 unsigned long pg
, pgend
;
209 * Convert start_pfn/end_pfn to a struct page pointer.
211 start_pg
= pfn_to_page(start_pfn
- 1) + 1;
212 end_pg
= pfn_to_page(end_pfn
);
215 * Convert to physical addresses, and
216 * round start upwards and end downwards.
218 pg
= PAGE_ALIGN(__pa(start_pg
));
219 pgend
= __pa(end_pg
) & PAGE_MASK
;
222 * If there are free pages between these,
223 * free the section of the memmap array.
226 memblock_free(pg
, pgend
- pg
);
230 * The mem_map array can get very big. Free the unused area of the memory map.
232 static void __init
free_unused_memmap(struct meminfo
*mi
)
234 unsigned long bank_start
, prev_bank_end
= 0;
238 * This relies on each bank being in address order.
239 * The banks are sorted previously in bootmem_init().
241 for_each_bank(i
, mi
) {
242 struct membank
*bank
= &mi
->bank
[i
];
244 bank_start
= bank_pfn_start(bank
);
247 * If we had a previous bank, and there is a space
248 * between the current bank and the previous, free it.
250 if (prev_bank_end
&& prev_bank_end
< bank_start
)
251 free_memmap(prev_bank_end
, bank_start
);
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.
258 prev_bank_end
= ALIGN(bank_pfn_end(bank
), MAX_ORDER_NR_PAGES
);
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.
267 void __init
mem_init(void)
269 max_mapnr
= pfn_to_page(max_pfn
+ PHYS_PFN_OFFSET
) - mem_map
;
271 free_unused_memmap(&meminfo
);
273 /* this will put all unused low memory onto the freelists */
276 mem_init_print_info(NULL
);
278 BUILD_BUG_ON(TASK_SIZE
> MODULES_VADDR
);
279 BUG_ON(TASK_SIZE
> MODULES_VADDR
);
281 if (PAGE_SIZE
>= 16384 && get_num_physpages() <= 128) {
283 * On a machine this small we won't get
284 * anywhere without overcommit, so turn
287 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
291 void free_initmem(void)
293 free_initmem_default(-1);
296 #ifdef CONFIG_BLK_DEV_INITRD
298 static int keep_initrd
;
300 void free_initrd_mem(unsigned long start
, unsigned long end
)
303 free_reserved_area((void *)start
, (void *)end
, -1, "initrd");
306 static int __init
keepinitrd_setup(char *__unused
)
312 __setup("keepinitrd", keepinitrd_setup
);