Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation |
| 3 | * August 2002: added remote node KVA remap - Martin J. Bligh |
| 4 | * |
| 5 | * Copyright (C) 2002, IBM Corp. |
| 6 | * |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 17 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 18 | * details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/config.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/bootmem.h> |
| 28 | #include <linux/mmzone.h> |
| 29 | #include <linux/highmem.h> |
| 30 | #include <linux/initrd.h> |
| 31 | #include <linux/nodemask.h> |
| 32 | #include <asm/e820.h> |
| 33 | #include <asm/setup.h> |
| 34 | #include <asm/mmzone.h> |
| 35 | #include <bios_ebda.h> |
| 36 | |
| 37 | struct pglist_data *node_data[MAX_NUMNODES]; |
| 38 | bootmem_data_t node0_bdata; |
| 39 | |
| 40 | /* |
| 41 | * numa interface - we expect the numa architecture specfic code to have |
| 42 | * populated the following initialisation. |
| 43 | * |
| 44 | * 1) node_online_map - the map of all nodes configured (online) in the system |
| 45 | * 2) physnode_map - the mapping between a pfn and owning node |
| 46 | * 3) node_start_pfn - the starting page frame number for a node |
| 47 | * 3) node_end_pfn - the ending page fram number for a node |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * physnode_map keeps track of the physical memory layout of a generic |
| 52 | * numa node on a 256Mb break (each element of the array will |
| 53 | * represent 256Mb of memory and will be marked by the node id. so, |
| 54 | * if the first gig is on node 0, and the second gig is on node 1 |
| 55 | * physnode_map will contain: |
| 56 | * |
| 57 | * physnode_map[0-3] = 0; |
| 58 | * physnode_map[4-7] = 1; |
| 59 | * physnode_map[8- ] = -1; |
| 60 | */ |
| 61 | s8 physnode_map[MAX_ELEMENTS] = { [0 ... (MAX_ELEMENTS - 1)] = -1}; |
| 62 | |
| 63 | void memory_present(int nid, unsigned long start, unsigned long end) |
| 64 | { |
| 65 | unsigned long pfn; |
| 66 | |
| 67 | printk(KERN_INFO "Node: %d, start_pfn: %ld, end_pfn: %ld\n", |
| 68 | nid, start, end); |
| 69 | printk(KERN_DEBUG " Setting physnode_map array to node %d for pfns:\n", nid); |
| 70 | printk(KERN_DEBUG " "); |
| 71 | for (pfn = start; pfn < end; pfn += PAGES_PER_ELEMENT) { |
| 72 | physnode_map[pfn / PAGES_PER_ELEMENT] = nid; |
| 73 | printk("%ld ", pfn); |
| 74 | } |
| 75 | printk("\n"); |
| 76 | } |
| 77 | |
| 78 | unsigned long node_memmap_size_bytes(int nid, unsigned long start_pfn, |
| 79 | unsigned long end_pfn) |
| 80 | { |
| 81 | unsigned long nr_pages = end_pfn - start_pfn; |
| 82 | |
| 83 | if (!nr_pages) |
| 84 | return 0; |
| 85 | |
| 86 | return (nr_pages + 1) * sizeof(struct page); |
| 87 | } |
| 88 | |
| 89 | unsigned long node_start_pfn[MAX_NUMNODES]; |
| 90 | unsigned long node_end_pfn[MAX_NUMNODES]; |
| 91 | |
| 92 | extern unsigned long find_max_low_pfn(void); |
| 93 | extern void find_max_pfn(void); |
| 94 | extern void one_highpage_init(struct page *, int, int); |
| 95 | |
| 96 | extern struct e820map e820; |
| 97 | extern unsigned long init_pg_tables_end; |
| 98 | extern unsigned long highend_pfn, highstart_pfn; |
| 99 | extern unsigned long max_low_pfn; |
| 100 | extern unsigned long totalram_pages; |
| 101 | extern unsigned long totalhigh_pages; |
| 102 | |
| 103 | #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE) |
| 104 | |
| 105 | unsigned long node_remap_start_pfn[MAX_NUMNODES]; |
| 106 | unsigned long node_remap_size[MAX_NUMNODES]; |
| 107 | unsigned long node_remap_offset[MAX_NUMNODES]; |
| 108 | void *node_remap_start_vaddr[MAX_NUMNODES]; |
| 109 | void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags); |
| 110 | |
| 111 | /* |
| 112 | * FLAT - support for basic PC memory model with discontig enabled, essentially |
| 113 | * a single node with all available processors in it with a flat |
| 114 | * memory map. |
| 115 | */ |
| 116 | int __init get_memcfg_numa_flat(void) |
| 117 | { |
| 118 | printk("NUMA - single node, flat memory mode\n"); |
| 119 | |
| 120 | /* Run the memory configuration and find the top of memory. */ |
| 121 | find_max_pfn(); |
| 122 | node_start_pfn[0] = 0; |
| 123 | node_end_pfn[0] = max_pfn; |
| 124 | memory_present(0, 0, max_pfn); |
| 125 | |
| 126 | /* Indicate there is one node available. */ |
| 127 | nodes_clear(node_online_map); |
| 128 | node_set_online(0); |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Find the highest page frame number we have available for the node |
| 134 | */ |
| 135 | static void __init find_max_pfn_node(int nid) |
| 136 | { |
| 137 | if (node_end_pfn[nid] > max_pfn) |
| 138 | node_end_pfn[nid] = max_pfn; |
| 139 | /* |
| 140 | * if a user has given mem=XXXX, then we need to make sure |
| 141 | * that the node _starts_ before that, too, not just ends |
| 142 | */ |
| 143 | if (node_start_pfn[nid] > max_pfn) |
| 144 | node_start_pfn[nid] = max_pfn; |
| 145 | if (node_start_pfn[nid] > node_end_pfn[nid]) |
| 146 | BUG(); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Allocate memory for the pg_data_t for this node via a crude pre-bootmem |
| 151 | * method. For node zero take this from the bottom of memory, for |
| 152 | * subsequent nodes place them at node_remap_start_vaddr which contains |
| 153 | * node local data in physically node local memory. See setup_memory() |
| 154 | * for details. |
| 155 | */ |
| 156 | static void __init allocate_pgdat(int nid) |
| 157 | { |
| 158 | if (nid && node_has_online_mem(nid)) |
| 159 | NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid]; |
| 160 | else { |
| 161 | NODE_DATA(nid) = (pg_data_t *)(__va(min_low_pfn << PAGE_SHIFT)); |
| 162 | min_low_pfn += PFN_UP(sizeof(pg_data_t)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void __init remap_numa_kva(void) |
| 167 | { |
| 168 | void *vaddr; |
| 169 | unsigned long pfn; |
| 170 | int node; |
| 171 | |
| 172 | for_each_online_node(node) { |
| 173 | if (node == 0) |
| 174 | continue; |
| 175 | for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) { |
| 176 | vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT); |
| 177 | set_pmd_pfn((ulong) vaddr, |
| 178 | node_remap_start_pfn[node] + pfn, |
| 179 | PAGE_KERNEL_LARGE); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static unsigned long calculate_numa_remap_pages(void) |
| 185 | { |
| 186 | int nid; |
| 187 | unsigned long size, reserve_pages = 0; |
| 188 | |
| 189 | for_each_online_node(nid) { |
| 190 | if (nid == 0) |
| 191 | continue; |
| 192 | if (!node_remap_size[nid]) |
| 193 | continue; |
| 194 | |
| 195 | /* |
| 196 | * The acpi/srat node info can show hot-add memroy zones |
| 197 | * where memory could be added but not currently present. |
| 198 | */ |
| 199 | if (node_start_pfn[nid] > max_pfn) |
| 200 | continue; |
| 201 | if (node_end_pfn[nid] > max_pfn) |
| 202 | node_end_pfn[nid] = max_pfn; |
| 203 | |
| 204 | /* ensure the remap includes space for the pgdat. */ |
| 205 | size = node_remap_size[nid] + sizeof(pg_data_t); |
| 206 | |
| 207 | /* convert size to large (pmd size) pages, rounding up */ |
| 208 | size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES; |
| 209 | /* now the roundup is correct, convert to PAGE_SIZE pages */ |
| 210 | size = size * PTRS_PER_PTE; |
| 211 | printk("Reserving %ld pages of KVA for lmem_map of node %d\n", |
| 212 | size, nid); |
| 213 | node_remap_size[nid] = size; |
| 214 | reserve_pages += size; |
| 215 | node_remap_offset[nid] = reserve_pages; |
| 216 | printk("Shrinking node %d from %ld pages to %ld pages\n", |
| 217 | nid, node_end_pfn[nid], node_end_pfn[nid] - size); |
| 218 | node_end_pfn[nid] -= size; |
| 219 | node_remap_start_pfn[nid] = node_end_pfn[nid]; |
| 220 | } |
| 221 | printk("Reserving total of %ld pages for numa KVA remap\n", |
| 222 | reserve_pages); |
| 223 | return reserve_pages; |
| 224 | } |
| 225 | |
| 226 | extern void setup_bootmem_allocator(void); |
| 227 | unsigned long __init setup_memory(void) |
| 228 | { |
| 229 | int nid; |
| 230 | unsigned long system_start_pfn, system_max_low_pfn; |
| 231 | unsigned long reserve_pages; |
| 232 | |
| 233 | /* |
| 234 | * When mapping a NUMA machine we allocate the node_mem_map arrays |
| 235 | * from node local memory. They are then mapped directly into KVA |
| 236 | * between zone normal and vmalloc space. Calculate the size of |
| 237 | * this space and use it to adjust the boundry between ZONE_NORMAL |
| 238 | * and ZONE_HIGHMEM. |
| 239 | */ |
| 240 | find_max_pfn(); |
| 241 | get_memcfg_numa(); |
| 242 | |
| 243 | reserve_pages = calculate_numa_remap_pages(); |
| 244 | |
| 245 | /* partially used pages are not usable - thus round upwards */ |
| 246 | system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end); |
| 247 | |
| 248 | system_max_low_pfn = max_low_pfn = find_max_low_pfn() - reserve_pages; |
| 249 | printk("reserve_pages = %ld find_max_low_pfn() ~ %ld\n", |
| 250 | reserve_pages, max_low_pfn + reserve_pages); |
| 251 | printk("max_pfn = %ld\n", max_pfn); |
| 252 | #ifdef CONFIG_HIGHMEM |
| 253 | highstart_pfn = highend_pfn = max_pfn; |
| 254 | if (max_pfn > system_max_low_pfn) |
| 255 | highstart_pfn = system_max_low_pfn; |
| 256 | printk(KERN_NOTICE "%ldMB HIGHMEM available.\n", |
| 257 | pages_to_mb(highend_pfn - highstart_pfn)); |
| 258 | #endif |
| 259 | printk(KERN_NOTICE "%ldMB LOWMEM available.\n", |
| 260 | pages_to_mb(system_max_low_pfn)); |
| 261 | printk("min_low_pfn = %ld, max_low_pfn = %ld, highstart_pfn = %ld\n", |
| 262 | min_low_pfn, max_low_pfn, highstart_pfn); |
| 263 | |
| 264 | printk("Low memory ends at vaddr %08lx\n", |
| 265 | (ulong) pfn_to_kaddr(max_low_pfn)); |
| 266 | for_each_online_node(nid) { |
| 267 | node_remap_start_vaddr[nid] = pfn_to_kaddr( |
| 268 | (highstart_pfn + reserve_pages) - node_remap_offset[nid]); |
| 269 | allocate_pgdat(nid); |
| 270 | printk ("node %d will remap to vaddr %08lx - %08lx\n", nid, |
| 271 | (ulong) node_remap_start_vaddr[nid], |
| 272 | (ulong) pfn_to_kaddr(highstart_pfn + reserve_pages |
| 273 | - node_remap_offset[nid] + node_remap_size[nid])); |
| 274 | } |
| 275 | printk("High memory starts at vaddr %08lx\n", |
| 276 | (ulong) pfn_to_kaddr(highstart_pfn)); |
| 277 | vmalloc_earlyreserve = reserve_pages * PAGE_SIZE; |
| 278 | for_each_online_node(nid) |
| 279 | find_max_pfn_node(nid); |
| 280 | |
| 281 | memset(NODE_DATA(0), 0, sizeof(struct pglist_data)); |
| 282 | NODE_DATA(0)->bdata = &node0_bdata; |
| 283 | setup_bootmem_allocator(); |
| 284 | return max_low_pfn; |
| 285 | } |
| 286 | |
| 287 | void __init zone_sizes_init(void) |
| 288 | { |
| 289 | int nid; |
| 290 | |
| 291 | /* |
| 292 | * Insert nodes into pgdat_list backward so they appear in order. |
| 293 | * Clobber node 0's links and NULL out pgdat_list before starting. |
| 294 | */ |
| 295 | pgdat_list = NULL; |
| 296 | for (nid = MAX_NUMNODES - 1; nid >= 0; nid--) { |
| 297 | if (!node_online(nid)) |
| 298 | continue; |
| 299 | NODE_DATA(nid)->pgdat_next = pgdat_list; |
| 300 | pgdat_list = NODE_DATA(nid); |
| 301 | } |
| 302 | |
| 303 | for_each_online_node(nid) { |
| 304 | unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0}; |
| 305 | unsigned long *zholes_size; |
| 306 | unsigned int max_dma; |
| 307 | |
| 308 | unsigned long low = max_low_pfn; |
| 309 | unsigned long start = node_start_pfn[nid]; |
| 310 | unsigned long high = node_end_pfn[nid]; |
| 311 | |
| 312 | max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT; |
| 313 | |
| 314 | if (node_has_online_mem(nid)){ |
| 315 | if (start > low) { |
| 316 | #ifdef CONFIG_HIGHMEM |
| 317 | BUG_ON(start > high); |
| 318 | zones_size[ZONE_HIGHMEM] = high - start; |
| 319 | #endif |
| 320 | } else { |
| 321 | if (low < max_dma) |
| 322 | zones_size[ZONE_DMA] = low; |
| 323 | else { |
| 324 | BUG_ON(max_dma > low); |
| 325 | BUG_ON(low > high); |
| 326 | zones_size[ZONE_DMA] = max_dma; |
| 327 | zones_size[ZONE_NORMAL] = low - max_dma; |
| 328 | #ifdef CONFIG_HIGHMEM |
| 329 | zones_size[ZONE_HIGHMEM] = high - low; |
| 330 | #endif |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | zholes_size = get_zholes_size(nid); |
| 336 | /* |
| 337 | * We let the lmem_map for node 0 be allocated from the |
| 338 | * normal bootmem allocator, but other nodes come from the |
| 339 | * remapped KVA area - mbligh |
| 340 | */ |
| 341 | if (!nid) |
| 342 | free_area_init_node(nid, NODE_DATA(nid), |
| 343 | zones_size, start, zholes_size); |
| 344 | else { |
| 345 | unsigned long lmem_map; |
| 346 | lmem_map = (unsigned long)node_remap_start_vaddr[nid]; |
| 347 | lmem_map += sizeof(pg_data_t) + PAGE_SIZE - 1; |
| 348 | lmem_map &= PAGE_MASK; |
| 349 | NODE_DATA(nid)->node_mem_map = (struct page *)lmem_map; |
| 350 | free_area_init_node(nid, NODE_DATA(nid), zones_size, |
| 351 | start, zholes_size); |
| 352 | } |
| 353 | } |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | void __init set_highmem_pages_init(int bad_ppro) |
| 358 | { |
| 359 | #ifdef CONFIG_HIGHMEM |
| 360 | struct zone *zone; |
| 361 | |
| 362 | for_each_zone(zone) { |
| 363 | unsigned long node_pfn, node_high_size, zone_start_pfn; |
| 364 | struct page * zone_mem_map; |
| 365 | |
| 366 | if (!is_highmem(zone)) |
| 367 | continue; |
| 368 | |
| 369 | printk("Initializing %s for node %d\n", zone->name, |
| 370 | zone->zone_pgdat->node_id); |
| 371 | |
| 372 | node_high_size = zone->spanned_pages; |
| 373 | zone_mem_map = zone->zone_mem_map; |
| 374 | zone_start_pfn = zone->zone_start_pfn; |
| 375 | |
| 376 | for (node_pfn = 0; node_pfn < node_high_size; node_pfn++) { |
| 377 | one_highpage_init((struct page *)(zone_mem_map + node_pfn), |
| 378 | zone_start_pfn + node_pfn, bad_ppro); |
| 379 | } |
| 380 | } |
| 381 | totalram_pages += totalhigh_pages; |
| 382 | #endif |
| 383 | } |