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