x86-64, NUMA: Separate out numa_cleanup_meminfo()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / mm / numa_64.c
blobf2721de30a43cc59c7fb232c34417c2c5c71b69d
1 /*
2 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
4 */
5 #include <linux/kernel.h>
6 #include <linux/mm.h>
7 #include <linux/string.h>
8 #include <linux/init.h>
9 #include <linux/bootmem.h>
10 #include <linux/memblock.h>
11 #include <linux/mmzone.h>
12 #include <linux/ctype.h>
13 #include <linux/module.h>
14 #include <linux/nodemask.h>
15 #include <linux/sched.h>
16 #include <linux/acpi.h>
18 #include <asm/e820.h>
19 #include <asm/proto.h>
20 #include <asm/dma.h>
21 #include <asm/numa.h>
22 #include <asm/acpi.h>
23 #include <asm/amd_nb.h>
25 struct numa_memblk {
26 u64 start;
27 u64 end;
28 int nid;
31 struct numa_meminfo {
32 int nr_blks;
33 struct numa_memblk blk[NR_NODE_MEMBLKS];
36 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
37 EXPORT_SYMBOL(node_data);
39 nodemask_t cpu_nodes_parsed __initdata;
40 nodemask_t mem_nodes_parsed __initdata;
42 struct memnode memnode;
44 static unsigned long __initdata nodemap_addr;
45 static unsigned long __initdata nodemap_size;
47 static struct numa_meminfo numa_meminfo __initdata;
49 struct bootnode numa_nodes[MAX_NUMNODES] __initdata;
52 * Given a shift value, try to populate memnodemap[]
53 * Returns :
54 * 1 if OK
55 * 0 if memnodmap[] too small (of shift too small)
56 * -1 if node overlap or lost ram (shift too big)
58 static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
60 unsigned long addr, end;
61 int i, res = -1;
63 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
64 for (i = 0; i < mi->nr_blks; i++) {
65 addr = mi->blk[i].start;
66 end = mi->blk[i].end;
67 if (addr >= end)
68 continue;
69 if ((end >> shift) >= memnodemapsize)
70 return 0;
71 do {
72 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
73 return -1;
74 memnodemap[addr >> shift] = mi->blk[i].nid;
75 addr += (1UL << shift);
76 } while (addr < end);
77 res = 1;
79 return res;
82 static int __init allocate_cachealigned_memnodemap(void)
84 unsigned long addr;
86 memnodemap = memnode.embedded_map;
87 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
88 return 0;
90 addr = 0x8000;
91 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
92 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
93 nodemap_size, L1_CACHE_BYTES);
94 if (nodemap_addr == MEMBLOCK_ERROR) {
95 printk(KERN_ERR
96 "NUMA: Unable to allocate Memory to Node hash map\n");
97 nodemap_addr = nodemap_size = 0;
98 return -1;
100 memnodemap = phys_to_virt(nodemap_addr);
101 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
103 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
104 nodemap_addr, nodemap_addr + nodemap_size);
105 return 0;
109 * The LSB of all start and end addresses in the node map is the value of the
110 * maximum possible shift.
112 static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
114 int i, nodes_used = 0;
115 unsigned long start, end;
116 unsigned long bitfield = 0, memtop = 0;
118 for (i = 0; i < mi->nr_blks; i++) {
119 start = mi->blk[i].start;
120 end = mi->blk[i].end;
121 if (start >= end)
122 continue;
123 bitfield |= start;
124 nodes_used++;
125 if (end > memtop)
126 memtop = end;
128 if (nodes_used <= 1)
129 i = 63;
130 else
131 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
132 memnodemapsize = (memtop >> i)+1;
133 return i;
136 static int __init compute_hash_shift(const struct numa_meminfo *mi)
138 int shift;
140 shift = extract_lsb_from_nodes(mi);
141 if (allocate_cachealigned_memnodemap())
142 return -1;
143 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
144 shift);
146 if (populate_memnodemap(mi, shift) != 1) {
147 printk(KERN_INFO "Your memory is not aligned you need to "
148 "rebuild your kernel with a bigger NODEMAPSIZE "
149 "shift=%d\n", shift);
150 return -1;
152 return shift;
155 int __meminit __early_pfn_to_nid(unsigned long pfn)
157 return phys_to_nid(pfn << PAGE_SHIFT);
160 static void * __init early_node_mem(int nodeid, unsigned long start,
161 unsigned long end, unsigned long size,
162 unsigned long align)
164 unsigned long mem;
167 * put it on high as possible
168 * something will go with NODE_DATA
170 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
171 start = MAX_DMA_PFN<<PAGE_SHIFT;
172 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
173 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
174 start = MAX_DMA32_PFN<<PAGE_SHIFT;
175 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
176 if (mem != MEMBLOCK_ERROR)
177 return __va(mem);
179 /* extend the search scope */
180 end = max_pfn_mapped << PAGE_SHIFT;
181 start = MAX_DMA_PFN << PAGE_SHIFT;
182 mem = memblock_find_in_range(start, end, size, align);
183 if (mem != MEMBLOCK_ERROR)
184 return __va(mem);
186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
187 size, nodeid);
189 return NULL;
192 static __init int conflicting_memblks(unsigned long start, unsigned long end)
194 struct numa_meminfo *mi = &numa_meminfo;
195 int i;
197 for (i = 0; i < mi->nr_blks; i++) {
198 struct numa_memblk *blk = &mi->blk[i];
200 if (blk->start == blk->end)
201 continue;
202 if (blk->end > start && blk->start < end)
203 return blk->nid;
204 if (blk->end == end && blk->start == start)
205 return blk->nid;
207 return -1;
210 int __init numa_add_memblk(int nid, u64 start, u64 end)
212 struct numa_meminfo *mi = &numa_meminfo;
213 int i;
215 i = conflicting_memblks(start, end);
216 if (i == nid) {
217 printk(KERN_WARNING "NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
218 nid, start, end, numa_nodes[i].start, numa_nodes[i].end);
219 } else if (i >= 0) {
220 printk(KERN_ERR "NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
221 nid, start, end, i,
222 numa_nodes[i].start, numa_nodes[i].end);
223 return -EINVAL;
226 mi->blk[mi->nr_blks].start = start;
227 mi->blk[mi->nr_blks].end = end;
228 mi->blk[mi->nr_blks].nid = nid;
229 mi->nr_blks++;
230 return 0;
233 static __init void cutoff_node(int i, unsigned long start, unsigned long end)
235 struct bootnode *nd = &numa_nodes[i];
237 if (nd->start < start) {
238 nd->start = start;
239 if (nd->end < nd->start)
240 nd->start = nd->end;
242 if (nd->end > end) {
243 nd->end = end;
244 if (nd->start > nd->end)
245 nd->start = nd->end;
249 /* Initialize bootmem allocator for a node */
250 void __init
251 setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
253 unsigned long start_pfn, last_pfn, nodedata_phys;
254 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
255 int nid;
257 if (!end)
258 return;
261 * Don't confuse VM with a node that doesn't have the
262 * minimum amount of memory:
264 if (end && (end - start) < NODE_MIN_SIZE)
265 return;
267 start = roundup(start, ZONE_ALIGN);
269 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
270 start, end);
272 start_pfn = start >> PAGE_SHIFT;
273 last_pfn = end >> PAGE_SHIFT;
275 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
276 SMP_CACHE_BYTES);
277 if (node_data[nodeid] == NULL)
278 return;
279 nodedata_phys = __pa(node_data[nodeid]);
280 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
281 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
282 nodedata_phys + pgdat_size - 1);
283 nid = phys_to_nid(nodedata_phys);
284 if (nid != nodeid)
285 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
287 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
288 NODE_DATA(nodeid)->node_id = nodeid;
289 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
290 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
292 node_set_online(nodeid);
295 static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
297 int i;
300 * Join together blocks on the same node, holes between
301 * which don't overlap with memory on other nodes.
303 for (i = 0; i < mi->nr_blks; ++i) {
304 struct numa_memblk *bi = &mi->blk[i];
305 int j, k;
307 for (j = i + 1; j < mi->nr_blks; ++j) {
308 struct numa_memblk *bj = &mi->blk[j];
309 unsigned long start, end;
311 if (bi->nid != bj->nid)
312 continue;
313 start = min(bi->end, bj->end);
314 end = max(bi->start, bj->start);
315 for (k = 0; k < mi->nr_blks; ++k) {
316 struct numa_memblk *bk = &mi->blk[k];
318 if (bi->nid == bk->nid)
319 continue;
320 if (start < bk->end && end > bk->start)
321 break;
323 if (k < mi->nr_blks)
324 continue;
325 start = min(bi->start, bj->start);
326 end = max(bi->end, bj->end);
327 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
328 bi->nid, bi->start, bi->end, bj->start, bj->end,
329 start, end);
330 bi->start = start;
331 bi->end = end;
332 k = --mi->nr_blks - j;
333 memmove(mi->blk + j, mi->blk + j + 1,
334 k * sizeof(mi->blk[0]));
335 --j;
339 return 0;
343 * Sanity check to catch more bad NUMA configurations (they are amazingly
344 * common). Make sure the nodes cover all memory.
346 static int __init nodes_cover_memory(const struct bootnode *nodes)
348 unsigned long numaram, e820ram;
349 int i;
351 numaram = 0;
352 for_each_node_mask(i, mem_nodes_parsed) {
353 unsigned long s = nodes[i].start >> PAGE_SHIFT;
354 unsigned long e = nodes[i].end >> PAGE_SHIFT;
355 numaram += e - s;
356 numaram -= __absent_pages_in_range(i, s, e);
357 if ((long)numaram < 0)
358 numaram = 0;
361 e820ram = max_pfn - (memblock_x86_hole_size(0,
362 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
363 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
364 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
365 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
366 (numaram << PAGE_SHIFT) >> 20,
367 (e820ram << PAGE_SHIFT) >> 20);
368 return 0;
370 return 1;
373 static int __init numa_register_memblks(struct numa_meminfo *mi)
375 int i;
377 /* Account for nodes with cpus and no memory */
378 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
379 if (WARN_ON(nodes_empty(node_possible_map)))
380 return -EINVAL;
382 memnode_shift = compute_hash_shift(mi);
383 if (memnode_shift < 0) {
384 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
385 return -EINVAL;
388 for (i = 0; i < mi->nr_blks; i++)
389 memblock_x86_register_active_regions(mi->blk[i].nid,
390 mi->blk[i].start >> PAGE_SHIFT,
391 mi->blk[i].end >> PAGE_SHIFT);
393 /* for out of order entries */
394 sort_node_map();
395 if (!nodes_cover_memory(numa_nodes))
396 return -EINVAL;
398 init_memory_mapping_high();
400 /* Finally register nodes. */
401 for_each_node_mask(i, node_possible_map)
402 setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
405 * Try again in case setup_node_bootmem missed one due to missing
406 * bootmem.
408 for_each_node_mask(i, node_possible_map)
409 if (!node_online(i))
410 setup_node_bootmem(i, numa_nodes[i].start,
411 numa_nodes[i].end);
413 return 0;
416 #ifdef CONFIG_NUMA_EMU
417 /* Numa emulation */
418 static struct bootnode nodes[MAX_NUMNODES] __initdata;
419 static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
420 static char *cmdline __initdata;
422 void __init numa_emu_cmdline(char *str)
424 cmdline = str;
427 static int __init setup_physnodes(unsigned long start, unsigned long end)
429 int ret = 0;
430 int i;
432 memset(physnodes, 0, sizeof(physnodes));
434 for_each_node_mask(i, mem_nodes_parsed) {
435 physnodes[i].start = numa_nodes[i].start;
436 physnodes[i].end = numa_nodes[i].end;
440 * Basic sanity checking on the physical node map: there may be errors
441 * if the SRAT or AMD code incorrectly reported the topology or the mem=
442 * kernel parameter is used.
444 for (i = 0; i < MAX_NUMNODES; i++) {
445 if (physnodes[i].start == physnodes[i].end)
446 continue;
447 if (physnodes[i].start > end) {
448 physnodes[i].end = physnodes[i].start;
449 continue;
451 if (physnodes[i].end < start) {
452 physnodes[i].start = physnodes[i].end;
453 continue;
455 if (physnodes[i].start < start)
456 physnodes[i].start = start;
457 if (physnodes[i].end > end)
458 physnodes[i].end = end;
459 ret++;
463 * If no physical topology was detected, a single node is faked to cover
464 * the entire address space.
466 if (!ret) {
467 physnodes[ret].start = start;
468 physnodes[ret].end = end;
469 ret = 1;
471 return ret;
474 static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
476 int i;
478 BUG_ON(acpi && amd);
479 #ifdef CONFIG_ACPI_NUMA
480 if (acpi)
481 acpi_fake_nodes(nodes, nr_nodes);
482 #endif
483 #ifdef CONFIG_AMD_NUMA
484 if (amd)
485 amd_fake_nodes(nodes, nr_nodes);
486 #endif
487 if (!acpi && !amd)
488 for (i = 0; i < nr_cpu_ids; i++)
489 numa_set_node(i, 0);
493 * Setups up nid to range from addr to addr + size. If the end
494 * boundary is greater than max_addr, then max_addr is used instead.
495 * The return value is 0 if there is additional memory left for
496 * allocation past addr and -1 otherwise. addr is adjusted to be at
497 * the end of the node.
499 static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
501 int ret = 0;
502 nodes[nid].start = *addr;
503 *addr += size;
504 if (*addr >= max_addr) {
505 *addr = max_addr;
506 ret = -1;
508 nodes[nid].end = *addr;
509 node_set(nid, node_possible_map);
510 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
511 nodes[nid].start, nodes[nid].end,
512 (nodes[nid].end - nodes[nid].start) >> 20);
513 return ret;
517 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
518 * to max_addr. The return value is the number of nodes allocated.
520 static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
522 nodemask_t physnode_mask = NODE_MASK_NONE;
523 u64 size;
524 int big;
525 int ret = 0;
526 int i;
528 if (nr_nodes <= 0)
529 return -1;
530 if (nr_nodes > MAX_NUMNODES) {
531 pr_info("numa=fake=%d too large, reducing to %d\n",
532 nr_nodes, MAX_NUMNODES);
533 nr_nodes = MAX_NUMNODES;
536 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
538 * Calculate the number of big nodes that can be allocated as a result
539 * of consolidating the remainder.
541 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
542 FAKE_NODE_MIN_SIZE;
544 size &= FAKE_NODE_MIN_HASH_MASK;
545 if (!size) {
546 pr_err("Not enough memory for each node. "
547 "NUMA emulation disabled.\n");
548 return -1;
551 for (i = 0; i < MAX_NUMNODES; i++)
552 if (physnodes[i].start != physnodes[i].end)
553 node_set(i, physnode_mask);
556 * Continue to fill physical nodes with fake nodes until there is no
557 * memory left on any of them.
559 while (nodes_weight(physnode_mask)) {
560 for_each_node_mask(i, physnode_mask) {
561 u64 end = physnodes[i].start + size;
562 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
564 if (ret < big)
565 end += FAKE_NODE_MIN_SIZE;
568 * Continue to add memory to this fake node if its
569 * non-reserved memory is less than the per-node size.
571 while (end - physnodes[i].start -
572 memblock_x86_hole_size(physnodes[i].start, end) < size) {
573 end += FAKE_NODE_MIN_SIZE;
574 if (end > physnodes[i].end) {
575 end = physnodes[i].end;
576 break;
581 * If there won't be at least FAKE_NODE_MIN_SIZE of
582 * non-reserved memory in ZONE_DMA32 for the next node,
583 * this one must extend to the boundary.
585 if (end < dma32_end && dma32_end - end -
586 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
587 end = dma32_end;
590 * If there won't be enough non-reserved memory for the
591 * next node, this one must extend to the end of the
592 * physical node.
594 if (physnodes[i].end - end -
595 memblock_x86_hole_size(end, physnodes[i].end) < size)
596 end = physnodes[i].end;
599 * Avoid allocating more nodes than requested, which can
600 * happen as a result of rounding down each node's size
601 * to FAKE_NODE_MIN_SIZE.
603 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
604 end = physnodes[i].end;
606 if (setup_node_range(ret++, &physnodes[i].start,
607 end - physnodes[i].start,
608 physnodes[i].end) < 0)
609 node_clear(i, physnode_mask);
612 return ret;
616 * Returns the end address of a node so that there is at least `size' amount of
617 * non-reserved memory or `max_addr' is reached.
619 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
621 u64 end = start + size;
623 while (end - start - memblock_x86_hole_size(start, end) < size) {
624 end += FAKE_NODE_MIN_SIZE;
625 if (end > max_addr) {
626 end = max_addr;
627 break;
630 return end;
634 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
635 * `addr' to `max_addr'. The return value is the number of nodes allocated.
637 static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
639 nodemask_t physnode_mask = NODE_MASK_NONE;
640 u64 min_size;
641 int ret = 0;
642 int i;
644 if (!size)
645 return -1;
647 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
648 * increased accordingly if the requested size is too small. This
649 * creates a uniform distribution of node sizes across the entire
650 * machine (but not necessarily over physical nodes).
652 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
653 MAX_NUMNODES;
654 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
655 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
656 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
657 FAKE_NODE_MIN_HASH_MASK;
658 if (size < min_size) {
659 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
660 size >> 20, min_size >> 20);
661 size = min_size;
663 size &= FAKE_NODE_MIN_HASH_MASK;
665 for (i = 0; i < MAX_NUMNODES; i++)
666 if (physnodes[i].start != physnodes[i].end)
667 node_set(i, physnode_mask);
669 * Fill physical nodes with fake nodes of size until there is no memory
670 * left on any of them.
672 while (nodes_weight(physnode_mask)) {
673 for_each_node_mask(i, physnode_mask) {
674 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
675 u64 end;
677 end = find_end_of_node(physnodes[i].start,
678 physnodes[i].end, size);
680 * If there won't be at least FAKE_NODE_MIN_SIZE of
681 * non-reserved memory in ZONE_DMA32 for the next node,
682 * this one must extend to the boundary.
684 if (end < dma32_end && dma32_end - end -
685 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
686 end = dma32_end;
689 * If there won't be enough non-reserved memory for the
690 * next node, this one must extend to the end of the
691 * physical node.
693 if (physnodes[i].end - end -
694 memblock_x86_hole_size(end, physnodes[i].end) < size)
695 end = physnodes[i].end;
698 * Setup the fake node that will be allocated as bootmem
699 * later. If setup_node_range() returns non-zero, there
700 * is no more memory available on this physical node.
702 if (setup_node_range(ret++, &physnodes[i].start,
703 end - physnodes[i].start,
704 physnodes[i].end) < 0)
705 node_clear(i, physnode_mask);
708 return ret;
712 * Sets up the system RAM area from start_pfn to last_pfn according to the
713 * numa=fake command-line option.
715 static int __init numa_emulation(unsigned long start_pfn,
716 unsigned long last_pfn, int acpi, int amd)
718 static struct numa_meminfo ei __initdata;
719 u64 addr = start_pfn << PAGE_SHIFT;
720 u64 max_addr = last_pfn << PAGE_SHIFT;
721 int num_nodes;
722 int i;
725 * If the numa=fake command-line contains a 'M' or 'G', it represents
726 * the fixed node size. Otherwise, if it is just a single number N,
727 * split the system RAM into N fake nodes.
729 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
730 u64 size;
732 size = memparse(cmdline, &cmdline);
733 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
734 } else {
735 unsigned long n;
737 n = simple_strtoul(cmdline, NULL, 0);
738 num_nodes = split_nodes_interleave(addr, max_addr, n);
741 if (num_nodes < 0)
742 return num_nodes;
744 ei.nr_blks = num_nodes;
745 for (i = 0; i < ei.nr_blks; i++) {
746 ei.blk[i].start = nodes[i].start;
747 ei.blk[i].end = nodes[i].end;
748 ei.blk[i].nid = i;
751 memnode_shift = compute_hash_shift(&ei);
752 if (memnode_shift < 0) {
753 memnode_shift = 0;
754 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
755 "disabled.\n");
756 return -1;
760 * We need to vacate all active ranges that may have been registered for
761 * the e820 memory map.
763 remove_all_active_ranges();
764 for_each_node_mask(i, node_possible_map)
765 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
766 nodes[i].end >> PAGE_SHIFT);
767 init_memory_mapping_high();
768 for_each_node_mask(i, node_possible_map)
769 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
770 setup_physnodes(addr, max_addr);
771 fake_physnodes(acpi, amd, num_nodes);
772 numa_init_array();
773 return 0;
775 #endif /* CONFIG_NUMA_EMU */
777 static int dummy_numa_init(void)
779 printk(KERN_INFO "%s\n",
780 numa_off ? "NUMA turned off" : "No NUMA configuration found");
781 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
782 0LU, max_pfn << PAGE_SHIFT);
784 node_set(0, cpu_nodes_parsed);
785 node_set(0, mem_nodes_parsed);
786 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
787 numa_nodes[0].start = 0;
788 numa_nodes[0].end = (u64)max_pfn << PAGE_SHIFT;
790 return 0;
793 void __init initmem_init(void)
795 int (*numa_init[])(void) = { [2] = dummy_numa_init };
796 int i, j;
798 if (!numa_off) {
799 #ifdef CONFIG_ACPI_NUMA
800 numa_init[0] = x86_acpi_numa_init;
801 #endif
802 #ifdef CONFIG_AMD_NUMA
803 numa_init[1] = amd_numa_init;
804 #endif
807 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
808 if (!numa_init[i])
809 continue;
811 for (j = 0; j < MAX_LOCAL_APIC; j++)
812 set_apicid_to_node(j, NUMA_NO_NODE);
814 nodes_clear(cpu_nodes_parsed);
815 nodes_clear(mem_nodes_parsed);
816 nodes_clear(node_possible_map);
817 nodes_clear(node_online_map);
818 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
819 memset(numa_nodes, 0, sizeof(numa_nodes));
820 remove_all_active_ranges();
822 if (numa_init[i]() < 0)
823 continue;
825 /* clean up the node list */
826 for (j = 0; j < MAX_NUMNODES; j++)
827 cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
829 #ifdef CONFIG_NUMA_EMU
830 setup_physnodes(0, max_pfn << PAGE_SHIFT);
831 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
832 return;
833 setup_physnodes(0, max_pfn << PAGE_SHIFT);
834 nodes_clear(node_possible_map);
835 nodes_clear(node_online_map);
836 #endif
837 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
838 continue;
840 if (numa_register_memblks(&numa_meminfo) < 0)
841 continue;
843 for (j = 0; j < nr_cpu_ids; j++) {
844 int nid = early_cpu_to_node(j);
846 if (nid == NUMA_NO_NODE)
847 continue;
848 if (!node_online(nid))
849 numa_clear_node(j);
851 numa_init_array();
852 return;
854 BUG();
857 unsigned long __init numa_free_all_bootmem(void)
859 unsigned long pages = 0;
860 int i;
862 for_each_online_node(i)
863 pages += free_all_bootmem_node(NODE_DATA(i));
865 pages += free_all_memory_core_early(MAX_NUMNODES);
867 return pages;
870 int __cpuinit numa_cpu_node(int cpu)
872 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
874 if (apicid != BAD_APICID)
875 return __apicid_to_node[apicid];
876 return NUMA_NO_NODE;
880 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
881 * of 64bit specific data structures. The distinction is artificial and
882 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
883 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
884 * enabled.
886 * NUMA emulation is planned to be made generic and the following and other
887 * related code should be moved to numa.c.
889 #ifdef CONFIG_NUMA_EMU
890 # ifndef CONFIG_DEBUG_PER_CPU_MAPS
891 void __cpuinit numa_add_cpu(int cpu)
893 unsigned long addr;
894 int physnid, nid;
896 nid = numa_cpu_node(cpu);
897 if (nid == NUMA_NO_NODE)
898 nid = early_cpu_to_node(cpu);
899 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
902 * Use the starting address of the emulated node to find which physical
903 * node it is allocated on.
905 addr = node_start_pfn(nid) << PAGE_SHIFT;
906 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
907 if (addr >= physnodes[physnid].start &&
908 addr < physnodes[physnid].end)
909 break;
912 * Map the cpu to each emulated node that is allocated on the physical
913 * node of the cpu's apic id.
915 for_each_online_node(nid) {
916 addr = node_start_pfn(nid) << PAGE_SHIFT;
917 if (addr >= physnodes[physnid].start &&
918 addr < physnodes[physnid].end)
919 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
923 void __cpuinit numa_remove_cpu(int cpu)
925 int i;
927 for_each_online_node(i)
928 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
930 # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
931 static void __cpuinit numa_set_cpumask(int cpu, int enable)
933 int node = early_cpu_to_node(cpu);
934 struct cpumask *mask;
935 int i;
937 if (node == NUMA_NO_NODE) {
938 /* early_cpu_to_node() already emits a warning and trace */
939 return;
941 for_each_online_node(i) {
942 unsigned long addr;
944 addr = node_start_pfn(i) << PAGE_SHIFT;
945 if (addr < physnodes[node].start ||
946 addr >= physnodes[node].end)
947 continue;
948 mask = debug_cpumask_set_cpu(cpu, enable);
949 if (!mask)
950 return;
952 if (enable)
953 cpumask_set_cpu(cpu, mask);
954 else
955 cpumask_clear_cpu(cpu, mask);
959 void __cpuinit numa_add_cpu(int cpu)
961 numa_set_cpumask(cpu, 1);
964 void __cpuinit numa_remove_cpu(int cpu)
966 numa_set_cpumask(cpu, 0);
968 # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
969 #endif /* CONFIG_NUMA_EMU */