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