2 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
5 #include <linux/kernel.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>
19 #include <asm/proto.h>
23 #include <asm/amd_nb.h>
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 numa_nodes_parsed __initdata;
41 struct memnode memnode;
43 static unsigned long __initdata nodemap_addr;
44 static unsigned long __initdata nodemap_size;
46 static struct numa_meminfo numa_meminfo __initdata;
49 * Given a shift value, try to populate memnodemap[]
52 * 0 if memnodmap[] too small (of shift too small)
53 * -1 if node overlap or lost ram (shift too big)
55 static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
57 unsigned long addr, end;
60 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
61 for (i = 0; i < mi->nr_blks; i++) {
62 addr = mi->blk[i].start;
66 if ((end >> shift) >= memnodemapsize)
69 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
71 memnodemap[addr >> shift] = mi->blk[i].nid;
72 addr += (1UL << shift);
79 static int __init allocate_cachealigned_memnodemap(void)
83 memnodemap = memnode.embedded_map;
84 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
88 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
89 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
90 nodemap_size, L1_CACHE_BYTES);
91 if (nodemap_addr == MEMBLOCK_ERROR) {
93 "NUMA: Unable to allocate Memory to Node hash map\n");
94 nodemap_addr = nodemap_size = 0;
97 memnodemap = phys_to_virt(nodemap_addr);
98 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
100 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
101 nodemap_addr, nodemap_addr + nodemap_size);
106 * The LSB of all start and end addresses in the node map is the value of the
107 * maximum possible shift.
109 static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
111 int i, nodes_used = 0;
112 unsigned long start, end;
113 unsigned long bitfield = 0, memtop = 0;
115 for (i = 0; i < mi->nr_blks; i++) {
116 start = mi->blk[i].start;
117 end = mi->blk[i].end;
128 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
129 memnodemapsize = (memtop >> i)+1;
133 static int __init compute_hash_shift(const struct numa_meminfo *mi)
137 shift = extract_lsb_from_nodes(mi);
138 if (allocate_cachealigned_memnodemap())
140 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
143 if (populate_memnodemap(mi, shift) != 1) {
144 printk(KERN_INFO "Your memory is not aligned you need to "
145 "rebuild your kernel with a bigger NODEMAPSIZE "
146 "shift=%d\n", shift);
152 int __meminit __early_pfn_to_nid(unsigned long pfn)
154 return phys_to_nid(pfn << PAGE_SHIFT);
157 static void * __init early_node_mem(int nodeid, unsigned long start,
158 unsigned long end, unsigned long size,
164 * put it on high as possible
165 * something will go with NODE_DATA
167 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
168 start = MAX_DMA_PFN<<PAGE_SHIFT;
169 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
170 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
171 start = MAX_DMA32_PFN<<PAGE_SHIFT;
172 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
173 if (mem != MEMBLOCK_ERROR)
176 /* extend the search scope */
177 end = max_pfn_mapped << PAGE_SHIFT;
178 start = MAX_DMA_PFN << PAGE_SHIFT;
179 mem = memblock_find_in_range(start, end, size, align);
180 if (mem != MEMBLOCK_ERROR)
183 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
189 int __init numa_add_memblk(int nid, u64 start, u64 end)
191 struct numa_meminfo *mi = &numa_meminfo;
193 /* ignore zero length blks */
197 /* whine about and ignore invalid blks */
198 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
199 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
204 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
205 pr_err("NUMA: too many memblk ranges\n");
209 mi->blk[mi->nr_blks].start = start;
210 mi->blk[mi->nr_blks].end = end;
211 mi->blk[mi->nr_blks].nid = nid;
216 static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
219 memmove(&mi->blk[idx], &mi->blk[idx + 1],
220 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
223 /* Initialize bootmem allocator for a node */
225 setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
227 unsigned long start_pfn, last_pfn, nodedata_phys;
228 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
235 * Don't confuse VM with a node that doesn't have the
236 * minimum amount of memory:
238 if (end && (end - start) < NODE_MIN_SIZE)
241 start = roundup(start, ZONE_ALIGN);
243 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
246 start_pfn = start >> PAGE_SHIFT;
247 last_pfn = end >> PAGE_SHIFT;
249 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
251 if (node_data[nodeid] == NULL)
253 nodedata_phys = __pa(node_data[nodeid]);
254 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
255 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
256 nodedata_phys + pgdat_size - 1);
257 nid = phys_to_nid(nodedata_phys);
259 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
261 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
262 NODE_DATA(nodeid)->node_id = nodeid;
263 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
264 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
266 node_set_online(nodeid);
269 static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
272 const u64 high = (u64)max_pfn << PAGE_SHIFT;
275 for (i = 0; i < mi->nr_blks; i++) {
276 struct numa_memblk *bi = &mi->blk[i];
278 /* make sure all blocks are inside the limits */
279 bi->start = max(bi->start, low);
280 bi->end = min(bi->end, high);
282 /* and there's no empty block */
283 if (bi->start == bi->end) {
284 numa_remove_memblk_from(i--, mi);
288 for (j = i + 1; j < mi->nr_blks; j++) {
289 struct numa_memblk *bj = &mi->blk[j];
290 unsigned long start, end;
293 * See whether there are overlapping blocks. Whine
294 * about but allow overlaps of the same nid. They
295 * will be merged below.
297 if (bi->end > bj->start && bi->start < bj->end) {
298 if (bi->nid != bj->nid) {
299 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
300 bi->nid, bi->start, bi->end,
301 bj->nid, bj->start, bj->end);
304 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
305 bi->nid, bi->start, bi->end,
310 * Join together blocks on the same node, holes
311 * between which don't overlap with memory on other
314 if (bi->nid != bj->nid)
316 start = max(min(bi->start, bj->start), low);
317 end = min(max(bi->end, bj->end), high);
318 for (k = 0; k < mi->nr_blks; k++) {
319 struct numa_memblk *bk = &mi->blk[k];
321 if (bi->nid == bk->nid)
323 if (start < bk->end && end > bk->start)
328 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
329 bi->nid, bi->start, bi->end, bj->start, bj->end,
333 numa_remove_memblk_from(j--, mi);
337 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
338 mi->blk[i].start = mi->blk[i].end = 0;
339 mi->blk[i].nid = NUMA_NO_NODE;
346 * Set nodes, which have memory in @mi, in *@nodemask.
348 static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
349 const struct numa_meminfo *mi)
353 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
354 if (mi->blk[i].start != mi->blk[i].end &&
355 mi->blk[i].nid != NUMA_NO_NODE)
356 node_set(mi->blk[i].nid, *nodemask);
360 * Sanity check to catch more bad NUMA configurations (they are amazingly
361 * common). Make sure the nodes cover all memory.
363 static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
365 unsigned long numaram, e820ram;
369 for (i = 0; i < mi->nr_blks; i++) {
370 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
371 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
373 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
374 if ((long)numaram < 0)
378 e820ram = max_pfn - (memblock_x86_hole_size(0,
379 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
380 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
381 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
382 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
383 (numaram << PAGE_SHIFT) >> 20,
384 (e820ram << PAGE_SHIFT) >> 20);
390 static int __init numa_register_memblks(struct numa_meminfo *mi)
394 /* Account for nodes with cpus and no memory */
395 node_possible_map = numa_nodes_parsed;
396 numa_nodemask_from_meminfo(&node_possible_map, mi);
397 if (WARN_ON(nodes_empty(node_possible_map)))
400 memnode_shift = compute_hash_shift(mi);
401 if (memnode_shift < 0) {
402 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
406 for (i = 0; i < mi->nr_blks; i++)
407 memblock_x86_register_active_regions(mi->blk[i].nid,
408 mi->blk[i].start >> PAGE_SHIFT,
409 mi->blk[i].end >> PAGE_SHIFT);
411 /* for out of order entries */
413 if (!numa_meminfo_cover_memory(mi))
416 init_memory_mapping_high();
419 * Finally register nodes. Do it twice in case setup_node_bootmem
420 * missed one due to missing bootmem.
422 for (i = 0; i < 2; i++) {
423 for_each_node_mask(nid, node_possible_map) {
424 u64 start = (u64)max_pfn << PAGE_SHIFT;
427 if (node_online(nid))
430 for (j = 0; j < mi->nr_blks; j++) {
431 if (nid != mi->blk[j].nid)
433 start = min(mi->blk[j].start, start);
434 end = max(mi->blk[j].end, end);
438 setup_node_bootmem(nid, start, end);
445 #ifdef CONFIG_NUMA_EMU
447 static struct bootnode nodes[MAX_NUMNODES] __initdata;
448 static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
449 static char *cmdline __initdata;
451 void __init numa_emu_cmdline(char *str)
456 int __init find_node_by_addr(unsigned long addr)
458 const struct numa_meminfo *mi = &numa_meminfo;
461 for (i = 0; i < mi->nr_blks; i++) {
463 * Find the real node that this emulated node appears on. For
464 * the sake of simplicity, we only use a real node's starting
465 * address to determine which emulated node it appears on.
467 if (addr >= mi->blk[i].start && addr < mi->blk[i].end)
468 return mi->blk[i].nid;
473 static int __init setup_physnodes(unsigned long start, unsigned long end)
475 const struct numa_meminfo *mi = &numa_meminfo;
479 memset(physnodes, 0, sizeof(physnodes));
481 for (i = 0; i < mi->nr_blks; i++) {
482 int nid = mi->blk[i].nid;
484 if (physnodes[nid].start == physnodes[nid].end) {
485 physnodes[nid].start = mi->blk[i].start;
486 physnodes[nid].end = mi->blk[i].end;
488 physnodes[nid].start = min(physnodes[nid].start,
490 physnodes[nid].end = max(physnodes[nid].end,
496 * Basic sanity checking on the physical node map: there may be errors
497 * if the SRAT or AMD code incorrectly reported the topology or the mem=
498 * kernel parameter is used.
500 for (i = 0; i < MAX_NUMNODES; i++) {
501 if (physnodes[i].start == physnodes[i].end)
503 if (physnodes[i].start > end) {
504 physnodes[i].end = physnodes[i].start;
507 if (physnodes[i].end < start) {
508 physnodes[i].start = physnodes[i].end;
511 if (physnodes[i].start < start)
512 physnodes[i].start = start;
513 if (physnodes[i].end > end)
514 physnodes[i].end = end;
519 * If no physical topology was detected, a single node is faked to cover
520 * the entire address space.
523 physnodes[ret].start = start;
524 physnodes[ret].end = end;
530 static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
535 #ifdef CONFIG_ACPI_NUMA
537 acpi_fake_nodes(nodes, nr_nodes);
539 #ifdef CONFIG_AMD_NUMA
541 amd_fake_nodes(nodes, nr_nodes);
544 for (i = 0; i < nr_cpu_ids; i++)
549 * Setups up nid to range from addr to addr + size. If the end
550 * boundary is greater than max_addr, then max_addr is used instead.
551 * The return value is 0 if there is additional memory left for
552 * allocation past addr and -1 otherwise. addr is adjusted to be at
553 * the end of the node.
555 static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
558 nodes[nid].start = *addr;
560 if (*addr >= max_addr) {
564 nodes[nid].end = *addr;
565 node_set(nid, node_possible_map);
566 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
567 nodes[nid].start, nodes[nid].end,
568 (nodes[nid].end - nodes[nid].start) >> 20);
573 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
574 * to max_addr. The return value is the number of nodes allocated.
576 static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
578 nodemask_t physnode_mask = NODE_MASK_NONE;
586 if (nr_nodes > MAX_NUMNODES) {
587 pr_info("numa=fake=%d too large, reducing to %d\n",
588 nr_nodes, MAX_NUMNODES);
589 nr_nodes = MAX_NUMNODES;
592 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
594 * Calculate the number of big nodes that can be allocated as a result
595 * of consolidating the remainder.
597 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
600 size &= FAKE_NODE_MIN_HASH_MASK;
602 pr_err("Not enough memory for each node. "
603 "NUMA emulation disabled.\n");
607 for (i = 0; i < MAX_NUMNODES; i++)
608 if (physnodes[i].start != physnodes[i].end)
609 node_set(i, physnode_mask);
612 * Continue to fill physical nodes with fake nodes until there is no
613 * memory left on any of them.
615 while (nodes_weight(physnode_mask)) {
616 for_each_node_mask(i, physnode_mask) {
617 u64 end = physnodes[i].start + size;
618 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
621 end += FAKE_NODE_MIN_SIZE;
624 * Continue to add memory to this fake node if its
625 * non-reserved memory is less than the per-node size.
627 while (end - physnodes[i].start -
628 memblock_x86_hole_size(physnodes[i].start, end) < size) {
629 end += FAKE_NODE_MIN_SIZE;
630 if (end > physnodes[i].end) {
631 end = physnodes[i].end;
637 * If there won't be at least FAKE_NODE_MIN_SIZE of
638 * non-reserved memory in ZONE_DMA32 for the next node,
639 * this one must extend to the boundary.
641 if (end < dma32_end && dma32_end - end -
642 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
646 * If there won't be enough non-reserved memory for the
647 * next node, this one must extend to the end of the
650 if (physnodes[i].end - end -
651 memblock_x86_hole_size(end, physnodes[i].end) < size)
652 end = physnodes[i].end;
655 * Avoid allocating more nodes than requested, which can
656 * happen as a result of rounding down each node's size
657 * to FAKE_NODE_MIN_SIZE.
659 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
660 end = physnodes[i].end;
662 if (setup_node_range(ret++, &physnodes[i].start,
663 end - physnodes[i].start,
664 physnodes[i].end) < 0)
665 node_clear(i, physnode_mask);
672 * Returns the end address of a node so that there is at least `size' amount of
673 * non-reserved memory or `max_addr' is reached.
675 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
677 u64 end = start + size;
679 while (end - start - memblock_x86_hole_size(start, end) < size) {
680 end += FAKE_NODE_MIN_SIZE;
681 if (end > max_addr) {
690 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
691 * `addr' to `max_addr'. The return value is the number of nodes allocated.
693 static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
695 nodemask_t physnode_mask = NODE_MASK_NONE;
703 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
704 * increased accordingly if the requested size is too small. This
705 * creates a uniform distribution of node sizes across the entire
706 * machine (but not necessarily over physical nodes).
708 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
710 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
711 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
712 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
713 FAKE_NODE_MIN_HASH_MASK;
714 if (size < min_size) {
715 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
716 size >> 20, min_size >> 20);
719 size &= FAKE_NODE_MIN_HASH_MASK;
721 for (i = 0; i < MAX_NUMNODES; i++)
722 if (physnodes[i].start != physnodes[i].end)
723 node_set(i, physnode_mask);
725 * Fill physical nodes with fake nodes of size until there is no memory
726 * left on any of them.
728 while (nodes_weight(physnode_mask)) {
729 for_each_node_mask(i, physnode_mask) {
730 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
733 end = find_end_of_node(physnodes[i].start,
734 physnodes[i].end, size);
736 * If there won't be at least FAKE_NODE_MIN_SIZE of
737 * non-reserved memory in ZONE_DMA32 for the next node,
738 * this one must extend to the boundary.
740 if (end < dma32_end && dma32_end - end -
741 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
745 * If there won't be enough non-reserved memory for the
746 * next node, this one must extend to the end of the
749 if (physnodes[i].end - end -
750 memblock_x86_hole_size(end, physnodes[i].end) < size)
751 end = physnodes[i].end;
754 * Setup the fake node that will be allocated as bootmem
755 * later. If setup_node_range() returns non-zero, there
756 * is no more memory available on this physical node.
758 if (setup_node_range(ret++, &physnodes[i].start,
759 end - physnodes[i].start,
760 physnodes[i].end) < 0)
761 node_clear(i, physnode_mask);
768 * Sets up the system RAM area from start_pfn to last_pfn according to the
769 * numa=fake command-line option.
771 static int __init numa_emulation(unsigned long start_pfn,
772 unsigned long last_pfn, int acpi, int amd)
774 static struct numa_meminfo ei __initdata;
775 u64 addr = start_pfn << PAGE_SHIFT;
776 u64 max_addr = last_pfn << PAGE_SHIFT;
781 * If the numa=fake command-line contains a 'M' or 'G', it represents
782 * the fixed node size. Otherwise, if it is just a single number N,
783 * split the system RAM into N fake nodes.
785 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
788 size = memparse(cmdline, &cmdline);
789 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
793 n = simple_strtoul(cmdline, NULL, 0);
794 num_nodes = split_nodes_interleave(addr, max_addr, n);
800 ei.nr_blks = num_nodes;
801 for (i = 0; i < ei.nr_blks; i++) {
802 ei.blk[i].start = nodes[i].start;
803 ei.blk[i].end = nodes[i].end;
807 memnode_shift = compute_hash_shift(&ei);
808 if (memnode_shift < 0) {
810 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
816 * We need to vacate all active ranges that may have been registered for
817 * the e820 memory map.
819 remove_all_active_ranges();
820 for_each_node_mask(i, node_possible_map)
821 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
822 nodes[i].end >> PAGE_SHIFT);
823 init_memory_mapping_high();
824 for_each_node_mask(i, node_possible_map)
825 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
826 setup_physnodes(addr, max_addr);
827 fake_physnodes(acpi, amd, num_nodes);
831 #endif /* CONFIG_NUMA_EMU */
833 static int dummy_numa_init(void)
835 printk(KERN_INFO "%s\n",
836 numa_off ? "NUMA turned off" : "No NUMA configuration found");
837 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
838 0LU, max_pfn << PAGE_SHIFT);
840 node_set(0, numa_nodes_parsed);
841 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
846 void __init initmem_init(void)
848 int (*numa_init[])(void) = { [2] = dummy_numa_init };
852 #ifdef CONFIG_ACPI_NUMA
853 numa_init[0] = x86_acpi_numa_init;
855 #ifdef CONFIG_AMD_NUMA
856 numa_init[1] = amd_numa_init;
860 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
864 for (j = 0; j < MAX_LOCAL_APIC; j++)
865 set_apicid_to_node(j, NUMA_NO_NODE);
867 nodes_clear(numa_nodes_parsed);
868 nodes_clear(node_possible_map);
869 nodes_clear(node_online_map);
870 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
871 remove_all_active_ranges();
873 if (numa_init[i]() < 0)
876 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
878 #ifdef CONFIG_NUMA_EMU
879 setup_physnodes(0, max_pfn << PAGE_SHIFT);
880 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
882 setup_physnodes(0, max_pfn << PAGE_SHIFT);
883 nodes_clear(node_possible_map);
884 nodes_clear(node_online_map);
886 if (numa_register_memblks(&numa_meminfo) < 0)
889 for (j = 0; j < nr_cpu_ids; j++) {
890 int nid = early_cpu_to_node(j);
892 if (nid == NUMA_NO_NODE)
894 if (!node_online(nid))
903 unsigned long __init numa_free_all_bootmem(void)
905 unsigned long pages = 0;
908 for_each_online_node(i)
909 pages += free_all_bootmem_node(NODE_DATA(i));
911 pages += free_all_memory_core_early(MAX_NUMNODES);
916 int __cpuinit numa_cpu_node(int cpu)
918 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
920 if (apicid != BAD_APICID)
921 return __apicid_to_node[apicid];
926 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
927 * of 64bit specific data structures. The distinction is artificial and
928 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
929 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
932 * NUMA emulation is planned to be made generic and the following and other
933 * related code should be moved to numa.c.
935 #ifdef CONFIG_NUMA_EMU
936 # ifndef CONFIG_DEBUG_PER_CPU_MAPS
937 void __cpuinit numa_add_cpu(int cpu)
942 nid = numa_cpu_node(cpu);
943 if (nid == NUMA_NO_NODE)
944 nid = early_cpu_to_node(cpu);
945 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
948 * Use the starting address of the emulated node to find which physical
949 * node it is allocated on.
951 addr = node_start_pfn(nid) << PAGE_SHIFT;
952 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
953 if (addr >= physnodes[physnid].start &&
954 addr < physnodes[physnid].end)
958 * Map the cpu to each emulated node that is allocated on the physical
959 * node of the cpu's apic id.
961 for_each_online_node(nid) {
962 addr = node_start_pfn(nid) << PAGE_SHIFT;
963 if (addr >= physnodes[physnid].start &&
964 addr < physnodes[physnid].end)
965 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
969 void __cpuinit numa_remove_cpu(int cpu)
973 for_each_online_node(i)
974 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
976 # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
977 static void __cpuinit numa_set_cpumask(int cpu, int enable)
979 int node = early_cpu_to_node(cpu);
980 struct cpumask *mask;
983 if (node == NUMA_NO_NODE) {
984 /* early_cpu_to_node() already emits a warning and trace */
987 for_each_online_node(i) {
990 addr = node_start_pfn(i) << PAGE_SHIFT;
991 if (addr < physnodes[node].start ||
992 addr >= physnodes[node].end)
994 mask = debug_cpumask_set_cpu(cpu, enable);
999 cpumask_set_cpu(cpu, mask);
1001 cpumask_clear_cpu(cpu, mask);
1005 void __cpuinit numa_add_cpu(int cpu)
1007 numa_set_cpumask(cpu, 1);
1010 void __cpuinit numa_remove_cpu(int cpu)
1012 numa_set_cpumask(cpu, 0);
1014 # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1015 #endif /* CONFIG_NUMA_EMU */