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