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;
48 static int numa_distance_cnt;
49 static u8 *numa_distance;
52 * Given a shift value, try to populate memnodemap[]
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;
63 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
64 for (i = 0; i < mi->nr_blks; i++) {
65 addr = mi->blk[i].start;
69 if ((end >> shift) >= memnodemapsize)
72 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
74 memnodemap[addr >> shift] = mi->blk[i].nid;
75 addr += (1UL << shift);
82 static int __init allocate_cachealigned_memnodemap(void)
86 memnodemap = memnode.embedded_map;
87 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
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) {
96 "NUMA: Unable to allocate Memory to Node hash map\n");
97 nodemap_addr = nodemap_size = 0;
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);
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;
131 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
132 memnodemapsize = (memtop >> i)+1;
136 static int __init compute_hash_shift(const struct numa_meminfo *mi)
140 shift = extract_lsb_from_nodes(mi);
141 if (allocate_cachealigned_memnodemap())
143 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
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);
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,
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)
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)
186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
192 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
193 struct numa_meminfo *mi)
195 /* ignore zero length blks */
199 /* whine about and ignore invalid blks */
200 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
201 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
206 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
207 pr_err("NUMA: too many memblk ranges\n");
211 mi->blk[mi->nr_blks].start = start;
212 mi->blk[mi->nr_blks].end = end;
213 mi->blk[mi->nr_blks].nid = nid;
218 static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
221 memmove(&mi->blk[idx], &mi->blk[idx + 1],
222 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
225 int __init numa_add_memblk(int nid, u64 start, u64 end)
227 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
230 /* Initialize bootmem allocator for a node */
232 setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
234 unsigned long start_pfn, last_pfn, nodedata_phys;
235 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
242 * Don't confuse VM with a node that doesn't have the
243 * minimum amount of memory:
245 if (end && (end - start) < NODE_MIN_SIZE)
248 start = roundup(start, ZONE_ALIGN);
250 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
253 start_pfn = start >> PAGE_SHIFT;
254 last_pfn = end >> PAGE_SHIFT;
256 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
258 if (node_data[nodeid] == NULL)
260 nodedata_phys = __pa(node_data[nodeid]);
261 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
262 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
263 nodedata_phys + pgdat_size - 1);
264 nid = phys_to_nid(nodedata_phys);
266 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
268 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
269 NODE_DATA(nodeid)->node_id = nodeid;
270 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
271 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
273 node_set_online(nodeid);
276 static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
279 const u64 high = (u64)max_pfn << PAGE_SHIFT;
282 for (i = 0; i < mi->nr_blks; i++) {
283 struct numa_memblk *bi = &mi->blk[i];
285 /* make sure all blocks are inside the limits */
286 bi->start = max(bi->start, low);
287 bi->end = min(bi->end, high);
289 /* and there's no empty block */
290 if (bi->start == bi->end) {
291 numa_remove_memblk_from(i--, mi);
295 for (j = i + 1; j < mi->nr_blks; j++) {
296 struct numa_memblk *bj = &mi->blk[j];
297 unsigned long start, end;
300 * See whether there are overlapping blocks. Whine
301 * about but allow overlaps of the same nid. They
302 * will be merged below.
304 if (bi->end > bj->start && bi->start < bj->end) {
305 if (bi->nid != bj->nid) {
306 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
307 bi->nid, bi->start, bi->end,
308 bj->nid, bj->start, bj->end);
311 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
312 bi->nid, bi->start, bi->end,
317 * Join together blocks on the same node, holes
318 * between which don't overlap with memory on other
321 if (bi->nid != bj->nid)
323 start = max(min(bi->start, bj->start), low);
324 end = min(max(bi->end, bj->end), high);
325 for (k = 0; k < mi->nr_blks; k++) {
326 struct numa_memblk *bk = &mi->blk[k];
328 if (bi->nid == bk->nid)
330 if (start < bk->end && end > bk->start)
335 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
336 bi->nid, bi->start, bi->end, bj->start, bj->end,
340 numa_remove_memblk_from(j--, mi);
344 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
345 mi->blk[i].start = mi->blk[i].end = 0;
346 mi->blk[i].nid = NUMA_NO_NODE;
353 * Set nodes, which have memory in @mi, in *@nodemask.
355 static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
356 const struct numa_meminfo *mi)
360 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
361 if (mi->blk[i].start != mi->blk[i].end &&
362 mi->blk[i].nid != NUMA_NO_NODE)
363 node_set(mi->blk[i].nid, *nodemask);
367 * Reset distance table. The current table is freed. The next
368 * numa_set_distance() call will create a new one.
370 static void __init numa_reset_distance(void)
374 if (numa_distance_cnt) {
375 size = numa_distance_cnt * sizeof(numa_distance[0]);
376 memblock_x86_free_range(__pa(numa_distance),
377 __pa(numa_distance) + size);
378 numa_distance_cnt = 0;
380 numa_distance = NULL;
384 * Set the distance between node @from to @to to @distance. If distance
385 * table doesn't exist, one which is large enough to accomodate all the
386 * currently known nodes will be created.
388 void __init numa_set_distance(int from, int to, int distance)
390 if (!numa_distance) {
391 nodemask_t nodes_parsed;
396 /* size the new table and allocate it */
397 nodes_parsed = numa_nodes_parsed;
398 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
400 for_each_node_mask(i, nodes_parsed)
402 size = ++cnt * sizeof(numa_distance[0]);
404 phys = memblock_find_in_range(0,
405 (u64)max_pfn_mapped << PAGE_SHIFT,
407 if (phys == MEMBLOCK_ERROR) {
408 pr_warning("NUMA: Warning: can't allocate distance table!\n");
409 /* don't retry until explicitly reset */
410 numa_distance = (void *)1LU;
413 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
415 numa_distance = __va(phys);
416 numa_distance_cnt = cnt;
418 /* fill with the default distances */
419 for (i = 0; i < cnt; i++)
420 for (j = 0; j < cnt; j++)
421 numa_distance[i * cnt + j] = i == j ?
422 LOCAL_DISTANCE : REMOTE_DISTANCE;
423 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
426 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
427 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
432 if ((u8)distance != distance ||
433 (from == to && distance != LOCAL_DISTANCE)) {
434 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
439 numa_distance[from * numa_distance_cnt + to] = distance;
442 int __node_distance(int from, int to)
444 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
445 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
446 return numa_distance[from * numa_distance_cnt + to];
448 EXPORT_SYMBOL(__node_distance);
451 * Sanity check to catch more bad NUMA configurations (they are amazingly
452 * common). Make sure the nodes cover all memory.
454 static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
456 unsigned long numaram, e820ram;
460 for (i = 0; i < mi->nr_blks; i++) {
461 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
462 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
464 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
465 if ((long)numaram < 0)
469 e820ram = max_pfn - (memblock_x86_hole_size(0,
470 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
471 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
472 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
473 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
474 (numaram << PAGE_SHIFT) >> 20,
475 (e820ram << PAGE_SHIFT) >> 20);
481 static int __init numa_register_memblks(struct numa_meminfo *mi)
485 /* Account for nodes with cpus and no memory */
486 node_possible_map = numa_nodes_parsed;
487 numa_nodemask_from_meminfo(&node_possible_map, mi);
488 if (WARN_ON(nodes_empty(node_possible_map)))
491 memnode_shift = compute_hash_shift(mi);
492 if (memnode_shift < 0) {
493 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
497 for (i = 0; i < mi->nr_blks; i++)
498 memblock_x86_register_active_regions(mi->blk[i].nid,
499 mi->blk[i].start >> PAGE_SHIFT,
500 mi->blk[i].end >> PAGE_SHIFT);
502 /* for out of order entries */
504 if (!numa_meminfo_cover_memory(mi))
507 init_memory_mapping_high();
509 /* Finally register nodes. */
510 for_each_node_mask(nid, node_possible_map) {
511 u64 start = (u64)max_pfn << PAGE_SHIFT;
514 for (i = 0; i < mi->nr_blks; i++) {
515 if (nid != mi->blk[i].nid)
517 start = min(mi->blk[i].start, start);
518 end = max(mi->blk[i].end, end);
522 setup_node_bootmem(nid, start, end);
528 #ifdef CONFIG_NUMA_EMU
530 static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
531 static char *emu_cmdline __initdata;
533 void __init numa_emu_cmdline(char *str)
538 static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
542 for (i = 0; i < mi->nr_blks; i++)
543 if (mi->blk[i].nid == nid)
549 * Sets up nid to range from @start to @end. The return value is -errno if
550 * something went wrong, 0 otherwise.
552 static int __init emu_setup_memblk(struct numa_meminfo *ei,
553 struct numa_meminfo *pi,
554 int nid, int phys_blk, u64 size)
556 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
557 struct numa_memblk *pb = &pi->blk[phys_blk];
559 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
560 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
565 eb->start = pb->start;
566 eb->end = pb->start + size;
569 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
570 emu_nid_to_phys[nid] = pb->nid;
573 if (pb->start >= pb->end) {
574 WARN_ON_ONCE(pb->start > pb->end);
575 numa_remove_memblk_from(phys_blk, pi);
578 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
579 eb->start, eb->end, (eb->end - eb->start) >> 20);
584 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
585 * to max_addr. The return value is the number of nodes allocated.
587 static int __init split_nodes_interleave(struct numa_meminfo *ei,
588 struct numa_meminfo *pi,
589 u64 addr, u64 max_addr, int nr_nodes)
591 nodemask_t physnode_mask = NODE_MASK_NONE;
599 if (nr_nodes > MAX_NUMNODES) {
600 pr_info("numa=fake=%d too large, reducing to %d\n",
601 nr_nodes, MAX_NUMNODES);
602 nr_nodes = MAX_NUMNODES;
605 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
607 * Calculate the number of big nodes that can be allocated as a result
608 * of consolidating the remainder.
610 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
613 size &= FAKE_NODE_MIN_HASH_MASK;
615 pr_err("Not enough memory for each node. "
616 "NUMA emulation disabled.\n");
620 for (i = 0; i < pi->nr_blks; i++)
621 node_set(pi->blk[i].nid, physnode_mask);
624 * Continue to fill physical nodes with fake nodes until there is no
625 * memory left on any of them.
627 while (nodes_weight(physnode_mask)) {
628 for_each_node_mask(i, physnode_mask) {
629 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
630 u64 start, limit, end;
633 phys_blk = emu_find_memblk_by_nid(i, pi);
635 node_clear(i, physnode_mask);
638 start = pi->blk[phys_blk].start;
639 limit = pi->blk[phys_blk].end;
643 end += FAKE_NODE_MIN_SIZE;
646 * Continue to add memory to this fake node if its
647 * non-reserved memory is less than the per-node size.
650 memblock_x86_hole_size(start, end) < size) {
651 end += FAKE_NODE_MIN_SIZE;
659 * If there won't be at least FAKE_NODE_MIN_SIZE of
660 * non-reserved memory in ZONE_DMA32 for the next node,
661 * this one must extend to the boundary.
663 if (end < dma32_end && dma32_end - end -
664 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
668 * If there won't be enough non-reserved memory for the
669 * next node, this one must extend to the end of the
673 memblock_x86_hole_size(end, limit) < size)
676 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
678 min(end, limit) - start);
687 * Returns the end address of a node so that there is at least `size' amount of
688 * non-reserved memory or `max_addr' is reached.
690 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
692 u64 end = start + size;
694 while (end - start - memblock_x86_hole_size(start, end) < size) {
695 end += FAKE_NODE_MIN_SIZE;
696 if (end > max_addr) {
705 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
706 * `addr' to `max_addr'. The return value is the number of nodes allocated.
708 static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
709 struct numa_meminfo *pi,
710 u64 addr, u64 max_addr, u64 size)
712 nodemask_t physnode_mask = NODE_MASK_NONE;
720 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
721 * increased accordingly if the requested size is too small. This
722 * creates a uniform distribution of node sizes across the entire
723 * machine (but not necessarily over physical nodes).
725 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
727 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
728 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
729 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
730 FAKE_NODE_MIN_HASH_MASK;
731 if (size < min_size) {
732 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
733 size >> 20, min_size >> 20);
736 size &= FAKE_NODE_MIN_HASH_MASK;
738 for (i = 0; i < pi->nr_blks; i++)
739 node_set(pi->blk[i].nid, physnode_mask);
742 * Fill physical nodes with fake nodes of size until there is no memory
743 * left on any of them.
745 while (nodes_weight(physnode_mask)) {
746 for_each_node_mask(i, physnode_mask) {
747 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
748 u64 start, limit, end;
751 phys_blk = emu_find_memblk_by_nid(i, pi);
753 node_clear(i, physnode_mask);
756 start = pi->blk[phys_blk].start;
757 limit = pi->blk[phys_blk].end;
759 end = find_end_of_node(start, limit, size);
761 * If there won't be at least FAKE_NODE_MIN_SIZE of
762 * non-reserved memory in ZONE_DMA32 for the next node,
763 * this one must extend to the boundary.
765 if (end < dma32_end && dma32_end - end -
766 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
770 * If there won't be enough non-reserved memory for the
771 * next node, this one must extend to the end of the
775 memblock_x86_hole_size(end, limit) < size)
778 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
780 min(end, limit) - start);
789 * Sets up the system RAM area from start_pfn to last_pfn according to the
790 * numa=fake command-line option.
792 static void __init numa_emulation(struct numa_meminfo *numa_meminfo,
795 static struct numa_meminfo ei __initdata;
796 static struct numa_meminfo pi __initdata;
797 const u64 max_addr = max_pfn << PAGE_SHIFT;
798 u8 *phys_dist = NULL;
804 memset(&ei, 0, sizeof(ei));
807 for (i = 0; i < MAX_NUMNODES; i++)
808 emu_nid_to_phys[i] = NUMA_NO_NODE;
811 * If the numa=fake command-line contains a 'M' or 'G', it represents
812 * the fixed node size. Otherwise, if it is just a single number N,
813 * split the system RAM into N fake nodes.
815 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
818 size = memparse(emu_cmdline, &emu_cmdline);
819 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
823 n = simple_strtoul(emu_cmdline, NULL, 0);
824 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
830 if (numa_cleanup_meminfo(&ei) < 0) {
831 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
836 * Copy the original distance table. It's temporary so no need to
840 size_t size = numa_dist_cnt * sizeof(phys_dist[0]);
843 phys = memblock_find_in_range(0,
844 (u64)max_pfn_mapped << PAGE_SHIFT,
846 if (phys == MEMBLOCK_ERROR) {
847 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
850 phys_dist = __va(phys);
852 for (i = 0; i < numa_dist_cnt; i++)
853 for (j = 0; j < numa_dist_cnt; j++)
854 phys_dist[i * numa_dist_cnt + j] =
862 * Transform __apicid_to_node table to use emulated nids by
863 * reverse-mapping phys_nid. The maps should always exist but fall
864 * back to zero just in case.
866 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
867 if (__apicid_to_node[i] == NUMA_NO_NODE)
869 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
870 if (__apicid_to_node[i] == emu_nid_to_phys[j])
872 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
875 /* make sure all emulated nodes are mapped to a physical node */
876 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
877 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
878 emu_nid_to_phys[i] = 0;
880 /* transform distance table */
881 numa_reset_distance();
882 for (i = 0; i < MAX_NUMNODES; i++) {
883 for (j = 0; j < MAX_NUMNODES; j++) {
884 int physi = emu_nid_to_phys[i];
885 int physj = emu_nid_to_phys[j];
888 if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
889 dist = physi == physj ?
890 LOCAL_DISTANCE : REMOTE_DISTANCE;
892 dist = phys_dist[physi * numa_dist_cnt + physj];
894 numa_set_distance(i, j, dist);
900 /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
901 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
902 emu_nid_to_phys[i] = i;
904 #else /* CONFIG_NUMA_EMU */
905 static inline void numa_emulation(struct numa_meminfo *numa_meminfo,
908 #endif /* CONFIG_NUMA_EMU */
910 static int __init dummy_numa_init(void)
912 printk(KERN_INFO "%s\n",
913 numa_off ? "NUMA turned off" : "No NUMA configuration found");
914 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
915 0LU, max_pfn << PAGE_SHIFT);
917 node_set(0, numa_nodes_parsed);
918 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
923 void __init initmem_init(void)
925 int (*numa_init[])(void) = { [2] = dummy_numa_init };
929 #ifdef CONFIG_ACPI_NUMA
930 numa_init[0] = x86_acpi_numa_init;
932 #ifdef CONFIG_AMD_NUMA
933 numa_init[1] = amd_numa_init;
937 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
941 for (j = 0; j < MAX_LOCAL_APIC; j++)
942 set_apicid_to_node(j, NUMA_NO_NODE);
944 nodes_clear(numa_nodes_parsed);
945 nodes_clear(node_possible_map);
946 nodes_clear(node_online_map);
947 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
948 remove_all_active_ranges();
949 numa_reset_distance();
951 if (numa_init[i]() < 0)
954 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
957 numa_emulation(&numa_meminfo, numa_distance_cnt);
959 if (numa_register_memblks(&numa_meminfo) < 0)
962 for (j = 0; j < nr_cpu_ids; j++) {
963 int nid = early_cpu_to_node(j);
965 if (nid == NUMA_NO_NODE)
967 if (!node_online(nid))
976 unsigned long __init numa_free_all_bootmem(void)
978 unsigned long pages = 0;
981 for_each_online_node(i)
982 pages += free_all_bootmem_node(NODE_DATA(i));
984 pages += free_all_memory_core_early(MAX_NUMNODES);
989 int __cpuinit numa_cpu_node(int cpu)
991 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
993 if (apicid != BAD_APICID)
994 return __apicid_to_node[apicid];
999 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
1000 * of 64bit specific data structures. The distinction is artificial and
1001 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
1002 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
1005 * NUMA emulation is planned to be made generic and the following and other
1006 * related code should be moved to numa.c.
1008 #ifdef CONFIG_NUMA_EMU
1009 # ifndef CONFIG_DEBUG_PER_CPU_MAPS
1010 void __cpuinit numa_add_cpu(int cpu)
1014 nid = numa_cpu_node(cpu);
1015 if (nid == NUMA_NO_NODE)
1016 nid = early_cpu_to_node(cpu);
1017 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
1019 physnid = emu_nid_to_phys[nid];
1022 * Map the cpu to each emulated node that is allocated on the physical
1023 * node of the cpu's apic id.
1025 for_each_online_node(nid)
1026 if (emu_nid_to_phys[nid] == physnid)
1027 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
1030 void __cpuinit numa_remove_cpu(int cpu)
1034 for_each_online_node(i)
1035 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
1037 # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
1038 static void __cpuinit numa_set_cpumask(int cpu, int enable)
1040 struct cpumask *mask;
1041 int nid, physnid, i;
1043 nid = early_cpu_to_node(cpu);
1044 if (nid == NUMA_NO_NODE) {
1045 /* early_cpu_to_node() already emits a warning and trace */
1049 physnid = emu_nid_to_phys[nid];
1051 for_each_online_node(i) {
1052 if (emu_nid_to_phys[nid] != physnid)
1055 mask = debug_cpumask_set_cpu(cpu, enable);
1060 cpumask_set_cpu(cpu, mask);
1062 cpumask_clear_cpu(cpu, mask);
1066 void __cpuinit numa_add_cpu(int cpu)
1068 numa_set_cpumask(cpu, 1);
1071 void __cpuinit numa_remove_cpu(int cpu)
1073 numa_set_cpumask(cpu, 0);
1075 # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1076 #endif /* CONFIG_NUMA_EMU */