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