Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mm/init.c |
| 3 | * |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 4 | * Copyright (C) 1995-2005 Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 19 | #include <asm/mach-types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/setup.h> |
Russell King | 74d02fb | 2006-04-04 21:47:43 +0100 | [diff] [blame] | 21 | #include <asm/sizes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/tlb.h> |
| 23 | |
| 24 | #include <asm/mach/arch.h> |
| 25 | #include <asm/mach/map.h> |
| 26 | |
Russell King | 1b2e2b7 | 2006-08-21 17:06:38 +0100 | [diff] [blame] | 27 | #include "mm.h" |
| 28 | |
Russell King | 012d1f4 | 2008-09-06 10:57:03 +0100 | [diff] [blame] | 29 | static unsigned long phys_initrd_start __initdata = 0; |
| 30 | static unsigned long phys_initrd_size __initdata = 0; |
| 31 | |
| 32 | static void __init early_initrd(char **p) |
| 33 | { |
| 34 | unsigned long start, size; |
| 35 | |
| 36 | start = memparse(*p, p); |
| 37 | if (**p == ',') { |
| 38 | size = memparse((*p) + 1, p); |
| 39 | |
| 40 | phys_initrd_start = start; |
| 41 | phys_initrd_size = size; |
| 42 | } |
| 43 | } |
| 44 | __early_param("initrd=", early_initrd); |
| 45 | |
| 46 | static int __init parse_tag_initrd(const struct tag *tag) |
| 47 | { |
| 48 | printk(KERN_WARNING "ATAG_INITRD is deprecated; " |
| 49 | "please update your bootloader.\n"); |
| 50 | phys_initrd_start = __virt_to_phys(tag->u.initrd.start); |
| 51 | phys_initrd_size = tag->u.initrd.size; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | __tagtable(ATAG_INITRD, parse_tag_initrd); |
| 56 | |
| 57 | static int __init parse_tag_initrd2(const struct tag *tag) |
| 58 | { |
| 59 | phys_initrd_start = tag->u.initrd.start; |
| 60 | phys_initrd_size = tag->u.initrd.size; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | __tagtable(ATAG_INITRD2, parse_tag_initrd2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
Nicolas Pitre | 4b5f32c | 2008-10-06 13:24:40 -0400 | [diff] [blame^] | 67 | * This keeps memory configuration data used by a couple memory |
| 68 | * initialization functions, as well as show_mem() for the skipping |
| 69 | * of holes in the memory map. It is populated by arm_add_memory(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
Nicolas Pitre | 4b5f32c | 2008-10-06 13:24:40 -0400 | [diff] [blame^] | 71 | struct meminfo meminfo; |
Ray Lehtiniemi | 5e70982 | 2006-11-07 03:19:15 +0100 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | void show_mem(void) |
| 74 | { |
| 75 | int free = 0, total = 0, reserved = 0; |
Ray Lehtiniemi | 5e70982 | 2006-11-07 03:19:15 +0100 | [diff] [blame] | 76 | int shared = 0, cached = 0, slab = 0, node, i; |
| 77 | struct meminfo * mi = &meminfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | printk("Mem-info:\n"); |
| 80 | show_free_areas(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | for_each_online_node(node) { |
Russell King | 204ecae | 2007-01-16 14:01:47 +0000 | [diff] [blame] | 82 | pg_data_t *n = NODE_DATA(node); |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 83 | struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn; |
Russell King | 204ecae | 2007-01-16 14:01:47 +0000 | [diff] [blame] | 84 | |
Ray Lehtiniemi | 5e70982 | 2006-11-07 03:19:15 +0100 | [diff] [blame] | 85 | for_each_nodebank (i,mi,node) { |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 86 | struct membank *bank = &mi->bank[i]; |
Ray Lehtiniemi | 5e70982 | 2006-11-07 03:19:15 +0100 | [diff] [blame] | 87 | unsigned int pfn1, pfn2; |
| 88 | struct page *page, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 90 | pfn1 = bank_pfn_start(bank); |
| 91 | pfn2 = bank_pfn_end(bank); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Russell King | 204ecae | 2007-01-16 14:01:47 +0000 | [diff] [blame] | 93 | page = map + pfn1; |
| 94 | end = map + pfn2; |
Ray Lehtiniemi | 5e70982 | 2006-11-07 03:19:15 +0100 | [diff] [blame] | 95 | |
| 96 | do { |
| 97 | total++; |
| 98 | if (PageReserved(page)) |
| 99 | reserved++; |
| 100 | else if (PageSwapCache(page)) |
| 101 | cached++; |
| 102 | else if (PageSlab(page)) |
| 103 | slab++; |
| 104 | else if (!page_count(page)) |
| 105 | free++; |
| 106 | else |
| 107 | shared += page_count(page) - 1; |
| 108 | page++; |
| 109 | } while (page < end); |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | printk("%d pages of RAM\n", total); |
| 114 | printk("%d free pages\n", free); |
| 115 | printk("%d reserved pages\n", reserved); |
| 116 | printk("%d slab pages\n", slab); |
| 117 | printk("%d pages shared\n", shared); |
| 118 | printk("%d pages swap cached\n", cached); |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* |
| 122 | * FIXME: We really want to avoid allocating the bootmap bitmap |
| 123 | * over the top of the initrd. Hopefully, this is located towards |
| 124 | * the start of a bank, so if we allocate the bootmap bitmap at |
| 125 | * the end, we won't clash. |
| 126 | */ |
| 127 | static unsigned int __init |
| 128 | find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages) |
| 129 | { |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 130 | unsigned int start_pfn, i, bootmap_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 132 | start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | bootmap_pfn = 0; |
| 134 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 135 | for_each_nodebank(i, mi, node) { |
| 136 | struct membank *bank = &mi->bank[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | unsigned int start, end; |
| 138 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 139 | start = bank_pfn_start(bank); |
| 140 | end = bank_pfn_end(bank); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (end < start_pfn) |
| 143 | continue; |
| 144 | |
| 145 | if (start < start_pfn) |
| 146 | start = start_pfn; |
| 147 | |
| 148 | if (end <= start) |
| 149 | continue; |
| 150 | |
| 151 | if (end - start >= bootmap_pages) { |
| 152 | bootmap_pfn = start; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (bootmap_pfn == 0) |
| 158 | BUG(); |
| 159 | |
| 160 | return bootmap_pfn; |
| 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | static int __init check_initrd(struct meminfo *mi) |
| 164 | { |
| 165 | int initrd_node = -2; |
| 166 | #ifdef CONFIG_BLK_DEV_INITRD |
| 167 | unsigned long end = phys_initrd_start + phys_initrd_size; |
| 168 | |
| 169 | /* |
| 170 | * Make sure that the initrd is within a valid area of |
| 171 | * memory. |
| 172 | */ |
| 173 | if (phys_initrd_size) { |
| 174 | unsigned int i; |
| 175 | |
| 176 | initrd_node = -1; |
| 177 | |
| 178 | for (i = 0; i < mi->nr_banks; i++) { |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 179 | struct membank *bank = &mi->bank[i]; |
| 180 | if (bank_phys_start(bank) <= phys_initrd_start && |
| 181 | end <= bank_phys_end(bank)) |
| 182 | initrd_node = bank->node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | if (initrd_node == -1) { |
Russell King | b962a28 | 2008-07-30 21:24:56 +0100 | [diff] [blame] | 187 | printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | "physical memory - disabling initrd\n", |
Russell King | b962a28 | 2008-07-30 21:24:56 +0100 | [diff] [blame] | 189 | phys_initrd_start, phys_initrd_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | phys_initrd_start = phys_initrd_size = 0; |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | return initrd_node; |
| 195 | } |
| 196 | |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 197 | static inline void map_memory_bank(struct membank *bank) |
| 198 | { |
Russell King | d111e8f | 2006-09-27 15:27:33 +0100 | [diff] [blame] | 199 | #ifdef CONFIG_MMU |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 200 | struct map_desc map; |
| 201 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 202 | map.pfn = bank_pfn_start(bank); |
| 203 | map.virtual = __phys_to_virt(bank_phys_start(bank)); |
| 204 | map.length = bank_phys_size(bank); |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 205 | map.type = MT_MEMORY; |
| 206 | |
| 207 | create_mapping(&map); |
Russell King | d111e8f | 2006-09-27 15:27:33 +0100 | [diff] [blame] | 208 | #endif |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 211 | static unsigned long __init bootmem_init_node(int node, struct meminfo *mi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 213 | unsigned long start_pfn, end_pfn, boot_pfn; |
| 214 | unsigned int boot_pages; |
| 215 | pg_data_t *pgdat; |
| 216 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 218 | start_pfn = -1UL; |
| 219 | end_pfn = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 222 | * Calculate the pfn range, and map the memory banks for this node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | */ |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 224 | for_each_nodebank(i, mi, node) { |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 225 | struct membank *bank = &mi->bank[i]; |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 226 | unsigned long start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 228 | start = bank_pfn_start(bank); |
| 229 | end = bank_pfn_end(bank); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 231 | if (start_pfn > start) |
| 232 | start_pfn = start; |
| 233 | if (end_pfn < end) |
| 234 | end_pfn = end; |
| 235 | |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 236 | map_memory_bank(bank); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 239 | /* |
| 240 | * If there is no memory in this node, ignore it. |
| 241 | */ |
| 242 | if (end_pfn == 0) |
| 243 | return end_pfn; |
| 244 | |
| 245 | /* |
| 246 | * Allocate the bootmem bitmap page. |
| 247 | */ |
| 248 | boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn); |
| 249 | boot_pfn = find_bootmap_pfn(node, mi, boot_pages); |
| 250 | |
| 251 | /* |
| 252 | * Initialise the bootmem allocator for this node, handing the |
| 253 | * memory banks over to bootmem. |
| 254 | */ |
| 255 | node_set_online(node); |
| 256 | pgdat = NODE_DATA(node); |
| 257 | init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn); |
| 258 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 259 | for_each_nodebank(i, mi, node) { |
| 260 | struct membank *bank = &mi->bank[i]; |
| 261 | free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank)); |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 262 | memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank)); |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 263 | } |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Reserve the bootmem bitmap for this node. |
| 267 | */ |
| 268 | reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT, |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 269 | boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 271 | return end_pfn; |
| 272 | } |
Russell King | b962a28 | 2008-07-30 21:24:56 +0100 | [diff] [blame] | 273 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 274 | static void __init bootmem_reserve_initrd(int node) |
| 275 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | #ifdef CONFIG_BLK_DEV_INITRD |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 277 | pg_data_t *pgdat = NODE_DATA(node); |
| 278 | int res; |
Russell King | b962a28 | 2008-07-30 21:24:56 +0100 | [diff] [blame] | 279 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 280 | res = reserve_bootmem_node(pgdat, phys_initrd_start, |
| 281 | phys_initrd_size, BOOTMEM_EXCLUSIVE); |
| 282 | |
| 283 | if (res == 0) { |
| 284 | initrd_start = __phys_to_virt(phys_initrd_start); |
| 285 | initrd_end = initrd_start + phys_initrd_size; |
| 286 | } else { |
| 287 | printk(KERN_ERR |
| 288 | "INITRD: 0x%08lx+0x%08lx overlaps in-use " |
| 289 | "memory region - disabling initrd\n", |
| 290 | phys_initrd_start, phys_initrd_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | #endif |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void __init bootmem_free_node(int node, struct meminfo *mi) |
| 296 | { |
| 297 | unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; |
| 298 | unsigned long start_pfn, end_pfn; |
| 299 | pg_data_t *pgdat = NODE_DATA(node); |
| 300 | int i; |
| 301 | |
| 302 | start_pfn = pgdat->bdata->node_min_pfn; |
| 303 | end_pfn = pgdat->bdata->node_low_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 305 | /* |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 306 | * initialise the zones within this node. |
| 307 | */ |
| 308 | memset(zone_size, 0, sizeof(zone_size)); |
| 309 | memset(zhole_size, 0, sizeof(zhole_size)); |
| 310 | |
| 311 | /* |
| 312 | * The size of this node has already been determined. If we need |
| 313 | * to do anything fancy with the allocation of this memory to the |
| 314 | * zones, now is the time to do it. |
| 315 | */ |
| 316 | zone_size[0] = end_pfn - start_pfn; |
| 317 | |
| 318 | /* |
| 319 | * For each bank in this node, calculate the size of the holes. |
| 320 | * holes = node_size - sum(bank_sizes_in_node) |
| 321 | */ |
| 322 | zhole_size[0] = zone_size[0]; |
| 323 | for_each_nodebank(i, mi, node) |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 324 | zhole_size[0] -= bank_pfn_size(&mi->bank[i]); |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Adjust the sizes according to any special requirements for |
| 328 | * this machine type. |
| 329 | */ |
| 330 | arch_adjust_zones(node, zone_size, zhole_size); |
| 331 | |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 332 | free_area_init_node(node, zone_size, start_pfn, zhole_size); |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Nicolas Pitre | 4b5f32c | 2008-10-06 13:24:40 -0400 | [diff] [blame^] | 335 | void __init bootmem_init(void) |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 336 | { |
Nicolas Pitre | 4b5f32c | 2008-10-06 13:24:40 -0400 | [diff] [blame^] | 337 | struct meminfo *mi = &meminfo; |
Russell King | 456335e | 2006-09-27 10:00:54 +0100 | [diff] [blame] | 338 | unsigned long memend_pfn = 0; |
Russell King | eca7321 | 2008-09-30 19:29:25 +0100 | [diff] [blame] | 339 | int node, initrd_node; |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 340 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 341 | /* |
| 342 | * Locate which node contains the ramdisk image, if any. |
| 343 | */ |
| 344 | initrd_node = check_initrd(mi); |
| 345 | |
| 346 | /* |
| 347 | * Run through each node initialising the bootmem allocator. |
| 348 | */ |
| 349 | for_each_node(node) { |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 350 | unsigned long end_pfn = bootmem_init_node(node, mi); |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 351 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 352 | /* |
| 353 | * Reserve any special node zero regions. |
| 354 | */ |
| 355 | if (node == 0) |
| 356 | reserve_node_zero(NODE_DATA(node)); |
| 357 | |
| 358 | /* |
| 359 | * If the initrd is in this node, reserve its memory. |
| 360 | */ |
| 361 | if (node == initrd_node) |
| 362 | bootmem_reserve_initrd(node); |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * Remember the highest memory PFN. |
| 366 | */ |
| 367 | if (end_pfn > memend_pfn) |
| 368 | memend_pfn = end_pfn; |
| 369 | } |
| 370 | |
Russell King | b7a69ac | 2008-10-01 16:58:32 +0100 | [diff] [blame] | 371 | /* |
| 372 | * sparse_init() needs the bootmem allocator up and running. |
| 373 | */ |
| 374 | sparse_init(); |
| 375 | |
| 376 | /* |
| 377 | * Now free memory in each node - free_area_init_node needs |
| 378 | * the sparse mem_map arrays initialized by sparse_init() |
| 379 | * for memmap_init_zone(), otherwise all PFNs are invalid. |
| 380 | */ |
| 381 | for_each_node(node) |
| 382 | bootmem_free_node(node, mi); |
| 383 | |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 384 | high_memory = __va(memend_pfn << PAGE_SHIFT); |
| 385 | |
| 386 | /* |
| 387 | * This doesn't seem to be used by the Linux memory manager any |
| 388 | * more, but is used by ll_rw_block. If we can get rid of it, we |
| 389 | * also get rid of some of the stuff above as well. |
| 390 | * |
| 391 | * Note: max_low_pfn and max_pfn reflect the number of _pages_ in |
| 392 | * the system, not the maximum PFN. |
| 393 | */ |
| 394 | max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET; |
| 395 | } |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | static inline void free_area(unsigned long addr, unsigned long end, char *s) |
| 398 | { |
| 399 | unsigned int size = (end - addr) >> 10; |
| 400 | |
| 401 | for (; addr < end; addr += PAGE_SIZE) { |
| 402 | struct page *page = virt_to_page(addr); |
| 403 | ClearPageReserved(page); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 404 | init_page_count(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | free_page(addr); |
| 406 | totalram_pages++; |
| 407 | } |
| 408 | |
| 409 | if (size && s) |
| 410 | printk(KERN_INFO "Freeing %s memory: %dK\n", s, size); |
| 411 | } |
| 412 | |
Russell King | a013053 | 2005-06-27 14:16:47 +0100 | [diff] [blame] | 413 | static inline void |
| 414 | free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn) |
| 415 | { |
| 416 | struct page *start_pg, *end_pg; |
| 417 | unsigned long pg, pgend; |
| 418 | |
| 419 | /* |
| 420 | * Convert start_pfn/end_pfn to a struct page pointer. |
| 421 | */ |
| 422 | start_pg = pfn_to_page(start_pfn); |
| 423 | end_pg = pfn_to_page(end_pfn); |
| 424 | |
| 425 | /* |
| 426 | * Convert to physical addresses, and |
| 427 | * round start upwards and end downwards. |
| 428 | */ |
| 429 | pg = PAGE_ALIGN(__pa(start_pg)); |
| 430 | pgend = __pa(end_pg) & PAGE_MASK; |
| 431 | |
| 432 | /* |
| 433 | * If there are free pages between these, |
| 434 | * free the section of the memmap array. |
| 435 | */ |
| 436 | if (pg < pgend) |
| 437 | free_bootmem_node(NODE_DATA(node), pg, pgend - pg); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * The mem_map array can get very big. Free the unused area of the memory map. |
| 442 | */ |
| 443 | static void __init free_unused_memmap_node(int node, struct meminfo *mi) |
| 444 | { |
| 445 | unsigned long bank_start, prev_bank_end = 0; |
| 446 | unsigned int i; |
| 447 | |
| 448 | /* |
| 449 | * [FIXME] This relies on each bank being in address order. This |
| 450 | * may not be the case, especially if the user has provided the |
| 451 | * information on the command line. |
| 452 | */ |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 453 | for_each_nodebank(i, mi, node) { |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 454 | struct membank *bank = &mi->bank[i]; |
| 455 | |
| 456 | bank_start = bank_pfn_start(bank); |
Russell King | a013053 | 2005-06-27 14:16:47 +0100 | [diff] [blame] | 457 | if (bank_start < prev_bank_end) { |
| 458 | printk(KERN_ERR "MEM: unordered memory banks. " |
| 459 | "Not freeing memmap.\n"); |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * If we had a previous bank, and there is a space |
| 465 | * between the current bank and the previous, free it. |
| 466 | */ |
| 467 | if (prev_bank_end && prev_bank_end != bank_start) |
| 468 | free_memmap(node, prev_bank_end, bank_start); |
| 469 | |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 470 | prev_bank_end = bank_pfn_end(bank); |
Russell King | a013053 | 2005-06-27 14:16:47 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
| 475 | * mem_init() marks the free areas in the mem_map and tells us how much |
| 476 | * memory is free. This is done after various parts of the system have |
| 477 | * claimed their memory after the kernel image. |
| 478 | */ |
| 479 | void __init mem_init(void) |
| 480 | { |
| 481 | unsigned int codepages, datapages, initpages; |
| 482 | int i, node; |
| 483 | |
| 484 | codepages = &_etext - &_text; |
| 485 | datapages = &_end - &__data_start; |
| 486 | initpages = &__init_end - &__init_begin; |
| 487 | |
| 488 | #ifndef CONFIG_DISCONTIGMEM |
| 489 | max_mapnr = virt_to_page(high_memory) - mem_map; |
| 490 | #endif |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* this will put all unused low memory onto the freelists */ |
| 493 | for_each_online_node(node) { |
| 494 | pg_data_t *pgdat = NODE_DATA(node); |
| 495 | |
Russell King | a013053 | 2005-06-27 14:16:47 +0100 | [diff] [blame] | 496 | free_unused_memmap_node(node, &meminfo); |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (pgdat->node_spanned_pages != 0) |
| 499 | totalram_pages += free_all_bootmem_node(pgdat); |
| 500 | } |
| 501 | |
| 502 | #ifdef CONFIG_SA1111 |
| 503 | /* now that our DMA memory is actually so designated, we can free it */ |
| 504 | free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL); |
| 505 | #endif |
| 506 | |
| 507 | /* |
| 508 | * Since our memory may not be contiguous, calculate the |
| 509 | * real number of pages we have in this system |
| 510 | */ |
| 511 | printk(KERN_INFO "Memory:"); |
| 512 | |
| 513 | num_physpages = 0; |
| 514 | for (i = 0; i < meminfo.nr_banks; i++) { |
Russell King | d2a38ef | 2008-10-01 16:56:15 +0100 | [diff] [blame] | 515 | num_physpages += bank_pfn_size(&meminfo.bank[i]); |
| 516 | printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT)); |
| 520 | printk(KERN_NOTICE "Memory: %luKB available (%dK code, " |
| 521 | "%dK data, %dK init)\n", |
| 522 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
| 523 | codepages >> 10, datapages >> 10, initpages >> 10); |
| 524 | |
| 525 | if (PAGE_SIZE >= 16384 && num_physpages <= 128) { |
| 526 | extern int sysctl_overcommit_memory; |
| 527 | /* |
| 528 | * On a machine this small we won't get |
| 529 | * anywhere without overcommit, so turn |
| 530 | * it on by default. |
| 531 | */ |
| 532 | sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void free_initmem(void) |
| 537 | { |
| 538 | if (!machine_is_integrator() && !machine_is_cintegrator()) { |
| 539 | free_area((unsigned long)(&__init_begin), |
| 540 | (unsigned long)(&__init_end), |
| 541 | "init"); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | #ifdef CONFIG_BLK_DEV_INITRD |
| 546 | |
| 547 | static int keep_initrd; |
| 548 | |
| 549 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 550 | { |
| 551 | if (!keep_initrd) |
| 552 | free_area(start, end, "initrd"); |
| 553 | } |
| 554 | |
| 555 | static int __init keepinitrd_setup(char *__unused) |
| 556 | { |
| 557 | keep_initrd = 1; |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | __setup("keepinitrd", keepinitrd_setup); |
| 562 | #endif |