blob: 9afb93a0e5325264b37a76090045053e3e2f9338 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/init.c
3 *
Russell King90072052005-10-28 14:48:37 +01004 * Copyright (C) 1995-2005 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Grant Likely9eb8f672011-04-28 14:27:20 -060018#include <linux/of_fdt.h>
Nicolas Pitre3835f6c2008-09-17 15:21:55 -040019#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Russell King2778f622010-07-09 16:27:52 +010021#include <linux/memblock.h>
Russell King7dc50ec2010-10-27 18:14:56 +010022#include <linux/sort.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#ifdef CONFIG_MEMORY_HOTPLUG
24#include <linux/memory_hotplug.h>
25#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/mach-types.h>
Grant Likely93c02ab2011-04-28 14:27:21 -060028#include <asm/prom.h>
Russell King37efe642008-12-01 11:53:07 +000029#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/setup.h>
Russell King74d02fb2006-04-04 21:47:43 +010031#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/tlb.h>
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +010033#include <asm/fixmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/mach/arch.h>
36#include <asm/mach/map.h>
37
Russell King1b2e2b72006-08-21 17:06:38 +010038#include "mm.h"
39
Russell King012d1f42008-09-06 10:57:03 +010040static unsigned long phys_initrd_start __initdata = 0;
41static unsigned long phys_initrd_size __initdata = 0;
42
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010043static int __init early_initrd(char *p)
Russell King012d1f42008-09-06 10:57:03 +010044{
45 unsigned long start, size;
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010046 char *endp;
Russell King012d1f42008-09-06 10:57:03 +010047
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010048 start = memparse(p, &endp);
49 if (*endp == ',') {
50 size = memparse(endp + 1, NULL);
Russell King012d1f42008-09-06 10:57:03 +010051
52 phys_initrd_start = start;
53 phys_initrd_size = size;
54 }
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010055 return 0;
Russell King012d1f42008-09-06 10:57:03 +010056}
Jeremy Kerr2b0d8c22010-01-11 23:17:34 +010057early_param("initrd", early_initrd);
Russell King012d1f42008-09-06 10:57:03 +010058
59static int __init parse_tag_initrd(const struct tag *tag)
60{
61 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
62 "please update your bootloader.\n");
63 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
64 phys_initrd_size = tag->u.initrd.size;
65 return 0;
66}
67
68__tagtable(ATAG_INITRD, parse_tag_initrd);
69
70static int __init parse_tag_initrd2(const struct tag *tag)
71{
72 phys_initrd_start = tag->u.initrd.start;
73 phys_initrd_size = tag->u.initrd.size;
74 return 0;
75}
76
77__tagtable(ATAG_INITRD2, parse_tag_initrd2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Grant Likely9eb8f672011-04-28 14:27:20 -060079#ifdef CONFIG_OF_FLATTREE
80void __init early_init_dt_setup_initrd_arch(unsigned long start, unsigned long end)
81{
82 phys_initrd_start = start;
83 phys_initrd_size = end - start;
84}
85#endif /* CONFIG_OF_FLATTREE */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040088 * This keeps memory configuration data used by a couple memory
89 * initialization functions, as well as show_mem() for the skipping
90 * of holes in the memory map. It is populated by arm_add_memory().
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Nicolas Pitre4b5f32c2008-10-06 13:24:40 -040092struct meminfo meminfo;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010093
David Rientjesb2b755b2011-03-24 15:18:15 -070094void show_mem(unsigned int filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int free = 0, total = 0, reserved = 0;
Russell Kingbe370302010-05-07 17:40:33 +010097 int shared = 0, cached = 0, slab = 0, i;
Ray Lehtiniemi5e709822006-11-07 03:19:15 +010098 struct meminfo * mi = &meminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 printk("Mem-info:\n");
David Rientjes7bf02ea2011-05-24 17:11:16 -0700101 show_free_areas(filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Russell Kingbe370302010-05-07 17:40:33 +0100103 for_each_bank (i, mi) {
104 struct membank *bank = &mi->bank[i];
105 unsigned int pfn1, pfn2;
106 struct page *page, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Russell Kingbe370302010-05-07 17:40:33 +0100108 pfn1 = bank_pfn_start(bank);
109 pfn2 = bank_pfn_end(bank);
Ray Lehtiniemi5e709822006-11-07 03:19:15 +0100110
Russell Kingbe370302010-05-07 17:40:33 +0100111 page = pfn_to_page(pfn1);
112 end = pfn_to_page(pfn2 - 1) + 1;
113
114 do {
115 total++;
116 if (PageReserved(page))
117 reserved++;
118 else if (PageSwapCache(page))
119 cached++;
120 else if (PageSlab(page))
121 slab++;
122 else if (!page_count(page))
123 free++;
124 else
125 shared += page_count(page) - 1;
126 page++;
127 } while (page < end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 printk("%d pages of RAM\n", total);
131 printk("%d free pages\n", free);
132 printk("%d reserved pages\n", reserved);
133 printk("%d slab pages\n", slab);
134 printk("%d pages shared\n", shared);
135 printk("%d pages swap cached\n", cached);
136}
137
Russell Kingf25b4b42010-10-27 19:49:33 +0100138static void __init find_limits(unsigned long *min, unsigned long *max_low,
139 unsigned long *max_high)
Russell Kingdde58282009-08-15 12:36:00 +0100140{
Russell Kingf25b4b42010-10-27 19:49:33 +0100141 struct meminfo *mi = &meminfo;
Russell Kingdde58282009-08-15 12:36:00 +0100142 int i;
143
144 *min = -1UL;
145 *max_low = *max_high = 0;
146
Russell Kingbe370302010-05-07 17:40:33 +0100147 for_each_bank (i, mi) {
Russell Kingdde58282009-08-15 12:36:00 +0100148 struct membank *bank = &mi->bank[i];
149 unsigned long start, end;
150
151 start = bank_pfn_start(bank);
152 end = bank_pfn_end(bank);
153
154 if (*min > start)
155 *min = start;
156 if (*max_high < end)
157 *max_high = end;
158 if (bank->highmem)
159 continue;
160 if (*max_low < end)
161 *max_low = end;
162 }
163}
164
Russell Kinga801d272010-10-27 19:27:44 +0100165static void __init arm_bootmem_init(unsigned long start_pfn,
166 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Benjamin Herrenschmidt719c1512010-08-05 12:55:55 +1000168 struct memblock_region *reg;
Russell King90072052005-10-28 14:48:37 +0100169 unsigned int boot_pages;
Russell King2778f622010-07-09 16:27:52 +0100170 phys_addr_t bitmap;
Russell King90072052005-10-28 14:48:37 +0100171 pg_data_t *pgdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
Russell King2778f622010-07-09 16:27:52 +0100174 * Allocate the bootmem bitmap page. This must be in a region
175 * of memory which has already been mapped.
Russell King90072052005-10-28 14:48:37 +0100176 */
177 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
Russell King2778f622010-07-09 16:27:52 +0100178 bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
179 __pfn_to_phys(end_pfn));
Russell King90072052005-10-28 14:48:37 +0100180
181 /*
Russell Kingbe370302010-05-07 17:40:33 +0100182 * Initialise the bootmem allocator, handing the
Russell King90072052005-10-28 14:48:37 +0100183 * memory banks over to bootmem.
184 */
Russell Kingbe370302010-05-07 17:40:33 +0100185 node_set_online(0);
186 pgdat = NODE_DATA(0);
Russell King2778f622010-07-09 16:27:52 +0100187 init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
Russell King90072052005-10-28 14:48:37 +0100188
Russell Kinga801d272010-10-27 19:27:44 +0100189 /* Free the lowmem regions from memblock into bootmem. */
190 for_each_memblock(memory, reg) {
191 unsigned long start = memblock_region_memory_base_pfn(reg);
192 unsigned long end = memblock_region_memory_end_pfn(reg);
193
194 if (end >= end_pfn)
195 end = end_pfn;
196 if (start >= end)
197 break;
198
199 free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
Russell Kingd2a38ef2008-10-01 16:56:15 +0100200 }
Russell King90072052005-10-28 14:48:37 +0100201
Russell Kinga801d272010-10-27 19:27:44 +0100202 /* Reserve the lowmem memblock reserved regions in bootmem. */
Benjamin Herrenschmidt719c1512010-08-05 12:55:55 +1000203 for_each_memblock(reserved, reg) {
Russell Kinga801d272010-10-27 19:27:44 +0100204 unsigned long start = memblock_region_reserved_base_pfn(reg);
205 unsigned long end = memblock_region_reserved_end_pfn(reg);
206
207 if (end >= end_pfn)
208 end = end_pfn;
209 if (start >= end)
210 break;
211
212 reserve_bootmem(__pfn_to_phys(start),
213 (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Russell Kingb7a69ac2008-10-01 16:58:32 +0100215}
216
Russell Kingbe209022011-05-11 15:39:00 +0100217#ifdef CONFIG_ZONE_DMA
218static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
219 unsigned long dma_size)
220{
221 if (size[0] <= dma_size)
222 return;
223
224 size[ZONE_NORMAL] = size[0] - dma_size;
225 size[ZONE_DMA] = dma_size;
226 hole[ZONE_NORMAL] = hole[0];
227 hole[ZONE_DMA] = 0;
228}
229#endif
230
Russell Kinga2c54d22010-10-27 19:17:31 +0100231static void __init arm_bootmem_free(unsigned long min, unsigned long max_low,
232 unsigned long max_high)
Russell Kingb7a69ac2008-10-01 16:58:32 +0100233{
234 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Russell Kinga2c54d22010-10-27 19:17:31 +0100235 struct memblock_region *reg;
Russell Kingb7a69ac2008-10-01 16:58:32 +0100236
Russell King90072052005-10-28 14:48:37 +0100237 /*
Russell Kingbe370302010-05-07 17:40:33 +0100238 * initialise the zones.
Russell King90072052005-10-28 14:48:37 +0100239 */
240 memset(zone_size, 0, sizeof(zone_size));
Russell King90072052005-10-28 14:48:37 +0100241
242 /*
Russell Kingbe370302010-05-07 17:40:33 +0100243 * The memory size has already been determined. If we need
244 * to do anything fancy with the allocation of this memory
245 * to the zones, now is the time to do it.
Russell King90072052005-10-28 14:48:37 +0100246 */
Russell Kingdde58282009-08-15 12:36:00 +0100247 zone_size[0] = max_low - min;
248#ifdef CONFIG_HIGHMEM
249 zone_size[ZONE_HIGHMEM] = max_high - max_low;
250#endif
Russell King90072052005-10-28 14:48:37 +0100251
252 /*
Russell Kingbe370302010-05-07 17:40:33 +0100253 * Calculate the size of the holes.
254 * holes = node_size - sum(bank_sizes)
Russell King90072052005-10-28 14:48:37 +0100255 */
Russell Kingdde58282009-08-15 12:36:00 +0100256 memcpy(zhole_size, zone_size, sizeof(zhole_size));
Russell Kinga2c54d22010-10-27 19:17:31 +0100257 for_each_memblock(memory, reg) {
258 unsigned long start = memblock_region_memory_base_pfn(reg);
259 unsigned long end = memblock_region_memory_end_pfn(reg);
260
261 if (start < max_low) {
262 unsigned long low_end = min(end, max_low);
263 zhole_size[0] -= low_end - start;
264 }
Russell Kingdde58282009-08-15 12:36:00 +0100265#ifdef CONFIG_HIGHMEM
Russell Kinga2c54d22010-10-27 19:17:31 +0100266 if (end > max_low) {
267 unsigned long high_start = max(start, max_low);
268 zhole_size[ZONE_HIGHMEM] -= end - high_start;
269 }
Russell Kingdde58282009-08-15 12:36:00 +0100270#endif
Russell Kingdde58282009-08-15 12:36:00 +0100271 }
Russell King90072052005-10-28 14:48:37 +0100272
Russell Kingbe209022011-05-11 15:39:00 +0100273#ifdef ARM_DMA_ZONE_SIZE
274#ifndef CONFIG_ZONE_DMA
275#error ARM_DMA_ZONE_SIZE set but no DMA zone to limit allocations
276#endif
277
Russell King90072052005-10-28 14:48:37 +0100278 /*
279 * Adjust the sizes according to any special requirements for
280 * this machine type.
281 */
Russell Kingbe209022011-05-11 15:39:00 +0100282 arm_adjust_dma_zone(zone_size, zhole_size,
283 ARM_DMA_ZONE_SIZE >> PAGE_SHIFT);
284#endif
Russell King90072052005-10-28 14:48:37 +0100285
Russell Kingbe370302010-05-07 17:40:33 +0100286 free_area_init_node(0, zone_size, min, zhole_size);
Russell King90072052005-10-28 14:48:37 +0100287}
288
Will Deacon7b7bf492011-05-19 13:21:14 +0100289#ifdef CONFIG_HAVE_ARCH_PFN_VALID
Russell Kingb7cfda92009-09-07 15:06:42 +0100290int pfn_valid(unsigned long pfn)
291{
Benjamin Herrenschmidt5e6f6aa2010-08-04 13:23:02 +1000292 return memblock_is_memory(pfn << PAGE_SHIFT);
Russell Kingb7cfda92009-09-07 15:06:42 +0100293}
294EXPORT_SYMBOL(pfn_valid);
Will Deacon7b7bf492011-05-19 13:21:14 +0100295#endif
Russell King657e12f2009-10-29 17:06:17 +0000296
Will Deacon7b7bf492011-05-19 13:21:14 +0100297#ifndef CONFIG_SPARSEMEM
Russell Kingeda2e5d2010-07-01 12:00:57 +0100298static void arm_memory_present(void)
Russell King657e12f2009-10-29 17:06:17 +0000299{
300}
301#else
Russell Kingeda2e5d2010-07-01 12:00:57 +0100302static void arm_memory_present(void)
Russell King657e12f2009-10-29 17:06:17 +0000303{
Benjamin Herrenschmidt719c1512010-08-05 12:55:55 +1000304 struct memblock_region *reg;
Benjamin Herrenschmidt719c1512010-08-05 12:55:55 +1000305
Yinghai Lu7c996362010-09-16 00:20:36 -0700306 for_each_memblock(memory, reg)
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700307 memory_present(0, memblock_region_memory_base_pfn(reg),
308 memblock_region_memory_end_pfn(reg));
Russell King657e12f2009-10-29 17:06:17 +0000309}
Russell Kingb7cfda92009-09-07 15:06:42 +0100310#endif
311
Russell King7dc50ec2010-10-27 18:14:56 +0100312static int __init meminfo_cmp(const void *_a, const void *_b)
313{
314 const struct membank *a = _a, *b = _b;
315 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
316 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
317}
318
Russell King8d717a52010-05-22 19:47:18 +0100319void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc)
Russell King2778f622010-07-09 16:27:52 +0100320{
321 int i;
322
Russell King7dc50ec2010-10-27 18:14:56 +0100323 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]), meminfo_cmp, NULL);
324
Russell King2778f622010-07-09 16:27:52 +0100325 memblock_init();
326 for (i = 0; i < mi->nr_banks; i++)
327 memblock_add(mi->bank[i].start, mi->bank[i].size);
328
329 /* Register the kernel text, kernel data and initrd with memblock. */
330#ifdef CONFIG_XIP_KERNEL
Russell King842eab42010-10-01 14:12:22 +0100331 memblock_reserve(__pa(_sdata), _end - _sdata);
Russell King2778f622010-07-09 16:27:52 +0100332#else
333 memblock_reserve(__pa(_stext), _end - _stext);
334#endif
335#ifdef CONFIG_BLK_DEV_INITRD
Russell Kingb0a26792011-01-30 11:21:05 +0000336 if (phys_initrd_size &&
Russell King8f4b8c72011-06-11 00:43:21 +0100337 !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) {
338 pr_err("INITRD: 0x%08lx+0x%08lx is not a memory region - disabling initrd\n",
339 phys_initrd_start, phys_initrd_size);
340 phys_initrd_start = phys_initrd_size = 0;
341 }
342 if (phys_initrd_size &&
Russell Kingb0a26792011-01-30 11:21:05 +0000343 memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) {
344 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region - disabling initrd\n",
345 phys_initrd_start, phys_initrd_size);
346 phys_initrd_start = phys_initrd_size = 0;
347 }
Russell King2778f622010-07-09 16:27:52 +0100348 if (phys_initrd_size) {
349 memblock_reserve(phys_initrd_start, phys_initrd_size);
350
351 /* Now convert initrd to virtual addresses */
352 initrd_start = __phys_to_virt(phys_initrd_start);
353 initrd_end = initrd_start + phys_initrd_size;
354 }
355#endif
356
357 arm_mm_memblock_reserve();
Grant Likely93c02ab2011-04-28 14:27:21 -0600358 arm_dt_memblock_reserve();
Russell King2778f622010-07-09 16:27:52 +0100359
Russell King8d717a52010-05-22 19:47:18 +0100360 /* reserve any platform specific memblock areas */
361 if (mdesc->reserve)
362 mdesc->reserve();
363
Russell King2778f622010-07-09 16:27:52 +0100364 memblock_analyze();
365 memblock_dump_all();
366}
367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
369int _early_pfn_valid(unsigned long pfn)
370{
371 struct meminfo *mi = &meminfo;
372 unsigned int left = 0, right = mi->nr_banks;
373
374 do {
375 unsigned int mid = (right + left) / 2;
376 struct membank *bank = &mi->bank[mid];
377
378 if (pfn < bank_pfn_start(bank))
379 right = mid;
380 else if (pfn >= bank_pfn_end(bank))
381 left = mid + 1;
382 else
383 return 1;
384 } while (left < right);
385 return 0;
386}
387EXPORT_SYMBOL(_early_pfn_valid);
388#endif
389
390#ifdef CONFIG_MEMORY_HOTPLUG
391static void map_reserved_memory(void)
392{
393 struct map_desc map;
394
395 map.pfn = (movable_reserved_start >> PAGE_SHIFT);
396 map.virtual = __phys_to_virt(movable_reserved_start);
397 map.length = movable_reserved_size;
398#ifdef CONFIG_STRICT_MEMORY_RWX
399 map.type = MT_MEMORY_RW;
400#else
401 map.type = MT_MEMORY;
402#endif
403
404 create_mapping(&map);
405}
406#endif
407
Russell King8d717a52010-05-22 19:47:18 +0100408void __init bootmem_init(void)
Russell King90072052005-10-28 14:48:37 +0100409{
Russell Kingdde58282009-08-15 12:36:00 +0100410 unsigned long min, max_low, max_high;
Russell King90072052005-10-28 14:48:37 +0100411
Russell Kingdde58282009-08-15 12:36:00 +0100412 max_low = max_high = 0;
413
Russell Kingf25b4b42010-10-27 19:49:33 +0100414 find_limits(&min, &max_low, &max_high);
Russell Kingbe370302010-05-07 17:40:33 +0100415
Russell Kinga801d272010-10-27 19:27:44 +0100416 arm_bootmem_init(min, max_low);
Russell Kingbe370302010-05-07 17:40:33 +0100417
Russell Kingbe370302010-05-07 17:40:33 +0100418 /*
Russell Kingbe370302010-05-07 17:40:33 +0100419 * Sparsemem tries to allocate bootmem in memory_present(),
420 * so must be done after the fixed reservations
421 */
Russell Kingeda2e5d2010-07-01 12:00:57 +0100422 arm_memory_present();
Russell King90072052005-10-28 14:48:37 +0100423
Russell Kingb7a69ac2008-10-01 16:58:32 +0100424 /*
425 * sparse_init() needs the bootmem allocator up and running.
426 */
427 sparse_init();
428
429 /*
Russell Kingbe370302010-05-07 17:40:33 +0100430 * Now free the memory - free_area_init_node needs
Russell Kingb7a69ac2008-10-01 16:58:32 +0100431 * the sparse mem_map arrays initialized by sparse_init()
432 * for memmap_init_zone(), otherwise all PFNs are invalid.
433 */
Russell Kinga2c54d22010-10-27 19:17:31 +0100434 arm_bootmem_free(min, max_low, max_high);
Russell Kingb7a69ac2008-10-01 16:58:32 +0100435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436#ifdef CONFIG_MEMORY_HOTPLUG
437 if (movable_reserved_size) {
438 max_low = (movable_reserved_start + movable_reserved_size)
439 >> PAGE_SHIFT;
440 map_reserved_memory();
441 }
442#endif
Will Deaconcae62922011-02-15 12:42:57 +0100443 high_memory = __va(((phys_addr_t)max_low << PAGE_SHIFT) - 1) + 1;
Russell King90072052005-10-28 14:48:37 +0100444
445 /*
446 * This doesn't seem to be used by the Linux memory manager any
447 * more, but is used by ll_rw_block. If we can get rid of it, we
448 * also get rid of some of the stuff above as well.
449 *
450 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
451 * the system, not the maximum PFN.
452 */
Russell Kingdde58282009-08-15 12:36:00 +0100453 max_low_pfn = max_low - PHYS_PFN_OFFSET;
454 max_pfn = max_high - PHYS_PFN_OFFSET;
Russell King90072052005-10-28 14:48:37 +0100455}
456
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400457static inline int free_area(unsigned long pfn, unsigned long end, char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400459 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400461 for (; pfn < end; pfn++) {
462 struct page *page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800464 init_page_count(page);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400465 __free_page(page);
466 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 if (size && s)
470 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400471
472 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Russell Kinga0130532005-06-27 14:16:47 +0100475static inline void
Russell Kingbe370302010-05-07 17:40:33 +0100476free_memmap(unsigned long start_pfn, unsigned long end_pfn)
Russell Kinga0130532005-06-27 14:16:47 +0100477{
478 struct page *start_pg, *end_pg;
479 unsigned long pg, pgend;
480
481 /*
482 * Convert start_pfn/end_pfn to a struct page pointer.
483 */
Catalin Marinas3257f432009-10-06 17:57:22 +0100484 start_pg = pfn_to_page(start_pfn - 1) + 1;
Will Deacon9af386c2011-04-28 18:44:31 +0100485 end_pg = pfn_to_page(end_pfn - 1) + 1;
Russell Kinga0130532005-06-27 14:16:47 +0100486
487 /*
488 * Convert to physical addresses, and
489 * round start upwards and end downwards.
490 */
Will Deaconcae62922011-02-15 12:42:57 +0100491 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
492 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
Russell Kinga0130532005-06-27 14:16:47 +0100493
494 /*
495 * If there are free pages between these,
496 * free the section of the memmap array.
497 */
498 if (pg < pgend)
Russell Kingbe370302010-05-07 17:40:33 +0100499 free_bootmem(pg, pgend - pg);
Russell Kinga0130532005-06-27 14:16:47 +0100500}
501
502/*
503 * The mem_map array can get very big. Free the unused area of the memory map.
504 */
Russell Kingbe370302010-05-07 17:40:33 +0100505static void __init free_unused_memmap(struct meminfo *mi)
Russell Kinga0130532005-06-27 14:16:47 +0100506{
507 unsigned long bank_start, prev_bank_end = 0;
508 unsigned int i;
509
510 /*
Michael Bohan3260e522010-06-14 13:06:56 -0700511 * This relies on each bank being in address order.
512 * The banks are sorted previously in bootmem_init().
Russell Kinga0130532005-06-27 14:16:47 +0100513 */
Russell Kingbe370302010-05-07 17:40:33 +0100514 for_each_bank(i, mi) {
Russell Kingd2a38ef2008-10-01 16:56:15 +0100515 struct membank *bank = &mi->bank[i];
516
517 bank_start = bank_pfn_start(bank);
Russell Kinga0130532005-06-27 14:16:47 +0100518
Will Deacon9af386c2011-04-28 18:44:31 +0100519#ifdef CONFIG_SPARSEMEM
520 /*
521 * Take care not to free memmap entries that don't exist
522 * due to SPARSEMEM sections which aren't present.
523 */
524 bank_start = min(bank_start,
525 ALIGN(prev_bank_end, PAGES_PER_SECTION));
526#endif
Russell Kinga0130532005-06-27 14:16:47 +0100527 /*
528 * If we had a previous bank, and there is a space
529 * between the current bank and the previous, free it.
530 */
Michael Bohan3260e522010-06-14 13:06:56 -0700531 if (prev_bank_end && prev_bank_end < bank_start)
Russell Kingbe370302010-05-07 17:40:33 +0100532 free_memmap(prev_bank_end, bank_start);
Russell Kinga0130532005-06-27 14:16:47 +0100533
Michael Bohan3260e522010-06-14 13:06:56 -0700534 /*
535 * Align up here since the VM subsystem insists that the
536 * memmap entries are valid from the bank end aligned to
537 * MAX_ORDER_NR_PAGES.
538 */
539 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
Russell Kinga0130532005-06-27 14:16:47 +0100540 }
Will Deacon9af386c2011-04-28 18:44:31 +0100541
542#ifdef CONFIG_SPARSEMEM
543 if (!IS_ALIGNED(prev_bank_end, PAGES_PER_SECTION))
544 free_memmap(prev_bank_end,
545 ALIGN(prev_bank_end, PAGES_PER_SECTION));
546#endif
Russell Kinga0130532005-06-27 14:16:47 +0100547}
548
Russell Kingd0e775a2010-10-27 19:37:06 +0100549static void __init free_highpages(void)
550{
551#ifdef CONFIG_HIGHMEM
Russell Kingdf4f14c2010-10-27 19:45:49 +0100552 unsigned long max_low = max_low_pfn + PHYS_PFN_OFFSET;
553 struct memblock_region *mem, *res;
Russell Kingd0e775a2010-10-27 19:37:06 +0100554
555 /* set highmem page free */
Russell Kingdf4f14c2010-10-27 19:45:49 +0100556 for_each_memblock(memory, mem) {
557 unsigned long start = memblock_region_memory_base_pfn(mem);
558 unsigned long end = memblock_region_memory_end_pfn(mem);
559
560 /* Ignore complete lowmem entries */
561 if (end <= max_low)
562 continue;
563
564 /* Truncate partial highmem entries */
565 if (start < max_low)
566 start = max_low;
567
568 /* Find and exclude any reserved regions */
569 for_each_memblock(reserved, res) {
570 unsigned long res_start, res_end;
571
572 res_start = memblock_region_reserved_base_pfn(res);
573 res_end = memblock_region_reserved_end_pfn(res);
574
575 if (res_end < start)
576 continue;
577 if (res_start < start)
578 res_start = start;
579 if (res_start > end)
580 res_start = end;
581 if (res_end > end)
582 res_end = end;
583 if (res_start != start)
584 totalhigh_pages += free_area(start, res_start,
585 NULL);
586 start = res_end;
587 if (start == end)
588 break;
589 }
590
591 /* And now free anything which remains */
592 if (start < end)
Russell Kingd0e775a2010-10-27 19:37:06 +0100593 totalhigh_pages += free_area(start, end, NULL);
594 }
595 totalram_pages += totalhigh_pages;
596#endif
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * mem_init() marks the free areas in the mem_map and tells us how much
601 * memory is free. This is done after various parts of the system have
602 * claimed their memory after the kernel image.
603 */
604void __init mem_init(void)
605{
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100606 unsigned long reserved_pages, free_pages;
Russell King47ea3c12010-10-27 19:35:29 +0100607 struct memblock_region *reg;
Russell Kingbe370302010-05-07 17:40:33 +0100608 int i;
Linus Walleij1dbd30e2010-07-12 21:53:28 +0100609#ifdef CONFIG_HAVE_TCM
610 /* These pointers are filled in on TCM detection */
611 extern u32 dtcm_end;
612 extern u32 itcm_end;
613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400615 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* this will put all unused low memory onto the freelists */
Russell Kingbe370302010-05-07 17:40:33 +0100618 free_unused_memmap(&meminfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Russell Kingbe370302010-05-07 17:40:33 +0100620 totalram_pages += free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622#ifdef CONFIG_SA1111
623 /* now that our DMA memory is actually so designated, we can free it */
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400624 totalram_pages += free_area(PHYS_PFN_OFFSET,
625 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#endif
627
Russell Kingd0e775a2010-10-27 19:37:06 +0100628 free_highpages();
Nicolas Pitre3835f6c2008-09-17 15:21:55 -0400629
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100630 reserved_pages = free_pages = 0;
631
Russell Kingbe370302010-05-07 17:40:33 +0100632 for_each_bank(i, &meminfo) {
633 struct membank *bank = &meminfo.bank[i];
634 unsigned int pfn1, pfn2;
635 struct page *page, *end;
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100636
Russell Kingbe370302010-05-07 17:40:33 +0100637 pfn1 = bank_pfn_start(bank);
638 pfn2 = bank_pfn_end(bank);
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100639
Russell Kingbe370302010-05-07 17:40:33 +0100640 page = pfn_to_page(pfn1);
641 end = pfn_to_page(pfn2 - 1) + 1;
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100642
Russell Kingbe370302010-05-07 17:40:33 +0100643 do {
644 if (PageReserved(page))
645 reserved_pages++;
646 else if (!page_count(page))
647 free_pages++;
648 page++;
649 } while (page < end);
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * Since our memory may not be contiguous, calculate the
654 * real number of pages we have in this system
655 */
656 printk(KERN_INFO "Memory:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 num_physpages = 0;
Russell King47ea3c12010-10-27 19:35:29 +0100658 for_each_memblock(memory, reg) {
659 unsigned long pages = memblock_region_memory_end_pfn(reg) -
660 memblock_region_memory_base_pfn(reg);
661 num_physpages += pages;
662 printk(" %ldMB", pages >> (20 - PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400665
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100666 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
667 nr_free_pages() << (PAGE_SHIFT-10),
668 free_pages << (PAGE_SHIFT-10),
669 reserved_pages << (PAGE_SHIFT-10),
Andreas Fenkart4b529402010-01-08 14:42:31 -0800670 totalhigh_pages << (PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100672#define MLK(b, t) b, t, ((t) - (b)) >> 10
673#define MLM(b, t) b, t, ((t) - (b)) >> 20
674#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
675
676 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
677 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
Linus Walleij07d2a5c2010-07-12 21:52:34 +0100678#ifdef CONFIG_HAVE_TCM
Linus Walleij07d2a5c2010-07-12 21:52:34 +0100679 " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
Linus Walleij07d2a5c2010-07-12 21:52:34 +0100680 " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
681#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100682 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100683#ifdef CONFIG_MMU
684 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
685#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100686 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
687 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
688#ifdef CONFIG_HIGHMEM
689 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
690#endif
691 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
692 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
693 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
Rabin Vincent45f6d7e2011-06-02 15:01:36 +0100694 " .data : 0x%p" " - 0x%p" " (%4d kB)\n"
695 " .bss : 0x%p" " - 0x%p" " (%4d kB)\n",
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100696
697 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
698 (PAGE_SIZE)),
Linus Walleij07d2a5c2010-07-12 21:52:34 +0100699#ifdef CONFIG_HAVE_TCM
Linus Walleij1dbd30e2010-07-12 21:53:28 +0100700 MLK(DTCM_OFFSET, (unsigned long) dtcm_end),
701 MLK(ITCM_OFFSET, (unsigned long) itcm_end),
Linus Walleij07d2a5c2010-07-12 21:52:34 +0100702#endif
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100703 MLK(FIXADDR_START, FIXADDR_TOP),
Fenkart/Bostandzhyana7bd08c2010-02-07 21:46:33 +0100704#ifdef CONFIG_MMU
705 MLM(CONSISTENT_BASE, CONSISTENT_END),
706#endif
Fenkart/Bostandzhyanc931b4f2010-02-07 21:47:17 +0100707 MLM(VMALLOC_START, VMALLOC_END),
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100708 MLM(PAGE_OFFSET, (unsigned long)high_memory),
709#ifdef CONFIG_HIGHMEM
710 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
711 (PAGE_SIZE)),
712#endif
713 MLM(MODULES_VADDR, MODULES_END),
714
715 MLK_ROUNDUP(__init_begin, __init_end),
716 MLK_ROUNDUP(_text, _etext),
Rabin Vincent45f6d7e2011-06-02 15:01:36 +0100717 MLK_ROUNDUP(_sdata, _edata),
718 MLK_ROUNDUP(__bss_start, __bss_stop));
Fenkart/Bostandzhyandb9ef1a2010-02-07 21:45:47 +0100719
720#undef MLK
721#undef MLM
722#undef MLK_ROUNDUP
723
Fenkart/Bostandzhyana1839272010-02-07 21:47:58 +0100724 /*
725 * Check boundaries twice: Some fundamental inconsistencies can
726 * be detected at build time already.
727 */
728#ifdef CONFIG_MMU
729 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
730 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
731
732 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
733 BUG_ON(TASK_SIZE > MODULES_VADDR);
734#endif
735
736#ifdef CONFIG_HIGHMEM
737 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
738 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
739#endif
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
742 extern int sysctl_overcommit_memory;
743 /*
744 * On a machine this small we won't get
745 * anywhere without overcommit, so turn
746 * it on by default.
747 */
748 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
749 }
750}
751
752void free_initmem(void)
753{
Linus Walleijbc581772009-09-15 17:30:37 +0100754#ifdef CONFIG_HAVE_TCM
Linus Walleijea208f62010-05-26 07:37:57 +0100755 extern char __tcm_start, __tcm_end;
Linus Walleijbc581772009-09-15 17:30:37 +0100756
Linus Walleijea208f62010-05-26 07:37:57 +0100757 totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)),
758 __phys_to_pfn(__pa(&__tcm_end)),
Linus Walleijbc581772009-09-15 17:30:37 +0100759 "TCM link");
760#endif
761
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400762 if (!machine_is_integrator() && !machine_is_cintegrator())
Russell King37efe642008-12-01 11:53:07 +0000763 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
764 __phys_to_pfn(__pa(__init_end)),
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400765 "init");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768#ifdef CONFIG_MEMORY_HOTPLUG
769int arch_add_memory(int nid, u64 start, u64 size)
770{
771 struct pglist_data *pgdata = NODE_DATA(nid);
772 struct zone *zone = pgdata->node_zones + ZONE_MOVABLE;
773 unsigned long start_pfn = start >> PAGE_SHIFT;
774 unsigned long nr_pages = size >> PAGE_SHIFT;
775 int ret;
776
777 ret = __add_pages(nid, zone, start_pfn, nr_pages);
778 if (ret)
779 return ret;
780 return platform_physical_active_pages(start_pfn, nr_pages);
781}
782
783int arch_physical_active_memory(u64 start, u64 size)
784{
785 unsigned long start_pfn = start >> PAGE_SHIFT;
786 unsigned long nr_pages = size >> PAGE_SHIFT;
787
788 return platform_physical_active_pages(start_pfn, nr_pages);
789}
790
791int arch_physical_remove_memory(u64 start, u64 size)
792{
793 unsigned long start_pfn = start >> PAGE_SHIFT;
794 unsigned long nr_pages = size >> PAGE_SHIFT;
795
796 return platform_physical_remove_pages(start_pfn, nr_pages);
797}
798
799int arch_physical_low_power_memory(u64 start, u64 size)
800{
801 unsigned long start_pfn = start >> PAGE_SHIFT;
802 unsigned long nr_pages = size >> PAGE_SHIFT;
803
804 return platform_physical_low_power_pages(start_pfn, nr_pages);
805}
806#endif
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#ifdef CONFIG_BLK_DEV_INITRD
809
810static int keep_initrd;
811
812void free_initrd_mem(unsigned long start, unsigned long end)
813{
814 if (!keep_initrd)
Nicolas Pitre6db015e2008-09-17 14:50:42 -0400815 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
816 __phys_to_pfn(__pa(end)),
817 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820static int __init keepinitrd_setup(char *__unused)
821{
822 keep_initrd = 1;
823 return 1;
824}
825
826__setup("keepinitrd", keepinitrd_setup);
827#endif