]> Pileus Git - ~andy/linux/blob - arch/x86/mm/numa_64.c
x86-64, NUMA: Restructure initmem_init()
[~andy/linux] / arch / x86 / mm / numa_64.c
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>
17
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>
24
25 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
26 EXPORT_SYMBOL(node_data);
27
28 struct memnode memnode;
29
30 static unsigned long __initdata nodemap_addr;
31 static unsigned long __initdata nodemap_size;
32
33 /*
34  * Given a shift value, try to populate memnodemap[]
35  * Returns :
36  * 1 if OK
37  * 0 if memnodmap[] too small (of shift too small)
38  * -1 if node overlap or lost ram (shift too big)
39  */
40 static int __init populate_memnodemap(const struct bootnode *nodes,
41                                       int numnodes, int shift, int *nodeids)
42 {
43         unsigned long addr, end;
44         int i, res = -1;
45
46         memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
47         for (i = 0; i < numnodes; i++) {
48                 addr = nodes[i].start;
49                 end = nodes[i].end;
50                 if (addr >= end)
51                         continue;
52                 if ((end >> shift) >= memnodemapsize)
53                         return 0;
54                 do {
55                         if (memnodemap[addr >> shift] != NUMA_NO_NODE)
56                                 return -1;
57
58                         if (!nodeids)
59                                 memnodemap[addr >> shift] = i;
60                         else
61                                 memnodemap[addr >> shift] = nodeids[i];
62
63                         addr += (1UL << shift);
64                 } while (addr < end);
65                 res = 1;
66         }
67         return res;
68 }
69
70 static int __init allocate_cachealigned_memnodemap(void)
71 {
72         unsigned long addr;
73
74         memnodemap = memnode.embedded_map;
75         if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
76                 return 0;
77
78         addr = 0x8000;
79         nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
80         nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
81                                       nodemap_size, L1_CACHE_BYTES);
82         if (nodemap_addr == MEMBLOCK_ERROR) {
83                 printk(KERN_ERR
84                        "NUMA: Unable to allocate Memory to Node hash map\n");
85                 nodemap_addr = nodemap_size = 0;
86                 return -1;
87         }
88         memnodemap = phys_to_virt(nodemap_addr);
89         memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
90
91         printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
92                nodemap_addr, nodemap_addr + nodemap_size);
93         return 0;
94 }
95
96 /*
97  * The LSB of all start and end addresses in the node map is the value of the
98  * maximum possible shift.
99  */
100 static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
101                                          int numnodes)
102 {
103         int i, nodes_used = 0;
104         unsigned long start, end;
105         unsigned long bitfield = 0, memtop = 0;
106
107         for (i = 0; i < numnodes; i++) {
108                 start = nodes[i].start;
109                 end = nodes[i].end;
110                 if (start >= end)
111                         continue;
112                 bitfield |= start;
113                 nodes_used++;
114                 if (end > memtop)
115                         memtop = end;
116         }
117         if (nodes_used <= 1)
118                 i = 63;
119         else
120                 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
121         memnodemapsize = (memtop >> i)+1;
122         return i;
123 }
124
125 int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
126                               int *nodeids)
127 {
128         int shift;
129
130         shift = extract_lsb_from_nodes(nodes, numnodes);
131         if (allocate_cachealigned_memnodemap())
132                 return -1;
133         printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
134                 shift);
135
136         if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
137                 printk(KERN_INFO "Your memory is not aligned you need to "
138                        "rebuild your kernel with a bigger NODEMAPSIZE "
139                        "shift=%d\n", shift);
140                 return -1;
141         }
142         return shift;
143 }
144
145 int __meminit  __early_pfn_to_nid(unsigned long pfn)
146 {
147         return phys_to_nid(pfn << PAGE_SHIFT);
148 }
149
150 static void * __init early_node_mem(int nodeid, unsigned long start,
151                                     unsigned long end, unsigned long size,
152                                     unsigned long align)
153 {
154         unsigned long mem;
155
156         /*
157          * put it on high as possible
158          * something will go with NODE_DATA
159          */
160         if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
161                 start = MAX_DMA_PFN<<PAGE_SHIFT;
162         if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
163             end > (MAX_DMA32_PFN<<PAGE_SHIFT))
164                 start = MAX_DMA32_PFN<<PAGE_SHIFT;
165         mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
166         if (mem != MEMBLOCK_ERROR)
167                 return __va(mem);
168
169         /* extend the search scope */
170         end = max_pfn_mapped << PAGE_SHIFT;
171         start = MAX_DMA_PFN << PAGE_SHIFT;
172         mem = memblock_find_in_range(start, end, size, align);
173         if (mem != MEMBLOCK_ERROR)
174                 return __va(mem);
175
176         printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
177                        size, nodeid);
178
179         return NULL;
180 }
181
182 /* Initialize bootmem allocator for a node */
183 void __init
184 setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
185 {
186         unsigned long start_pfn, last_pfn, nodedata_phys;
187         const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
188         int nid;
189
190         if (!end)
191                 return;
192
193         /*
194          * Don't confuse VM with a node that doesn't have the
195          * minimum amount of memory:
196          */
197         if (end && (end - start) < NODE_MIN_SIZE)
198                 return;
199
200         start = roundup(start, ZONE_ALIGN);
201
202         printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
203                start, end);
204
205         start_pfn = start >> PAGE_SHIFT;
206         last_pfn = end >> PAGE_SHIFT;
207
208         node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
209                                            SMP_CACHE_BYTES);
210         if (node_data[nodeid] == NULL)
211                 return;
212         nodedata_phys = __pa(node_data[nodeid]);
213         memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
214         printk(KERN_INFO "  NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
215                 nodedata_phys + pgdat_size - 1);
216         nid = phys_to_nid(nodedata_phys);
217         if (nid != nodeid)
218                 printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nodeid, nid);
219
220         memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
221         NODE_DATA(nodeid)->node_id = nodeid;
222         NODE_DATA(nodeid)->node_start_pfn = start_pfn;
223         NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
224
225         node_set_online(nodeid);
226 }
227
228 #ifdef CONFIG_NUMA_EMU
229 /* Numa emulation */
230 static struct bootnode nodes[MAX_NUMNODES] __initdata;
231 static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
232 static char *cmdline __initdata;
233
234 void __init numa_emu_cmdline(char *str)
235 {
236         cmdline = str;
237 }
238
239 static int __init setup_physnodes(unsigned long start, unsigned long end,
240                                         int acpi, int amd)
241 {
242         int ret = 0;
243         int i;
244
245         memset(physnodes, 0, sizeof(physnodes));
246 #ifdef CONFIG_ACPI_NUMA
247         if (acpi)
248                 acpi_get_nodes(physnodes, start, end);
249 #endif
250 #ifdef CONFIG_AMD_NUMA
251         if (amd)
252                 amd_get_nodes(physnodes);
253 #endif
254         /*
255          * Basic sanity checking on the physical node map: there may be errors
256          * if the SRAT or AMD code incorrectly reported the topology or the mem=
257          * kernel parameter is used.
258          */
259         for (i = 0; i < MAX_NUMNODES; i++) {
260                 if (physnodes[i].start == physnodes[i].end)
261                         continue;
262                 if (physnodes[i].start > end) {
263                         physnodes[i].end = physnodes[i].start;
264                         continue;
265                 }
266                 if (physnodes[i].end < start) {
267                         physnodes[i].start = physnodes[i].end;
268                         continue;
269                 }
270                 if (physnodes[i].start < start)
271                         physnodes[i].start = start;
272                 if (physnodes[i].end > end)
273                         physnodes[i].end = end;
274                 ret++;
275         }
276
277         /*
278          * If no physical topology was detected, a single node is faked to cover
279          * the entire address space.
280          */
281         if (!ret) {
282                 physnodes[ret].start = start;
283                 physnodes[ret].end = end;
284                 ret = 1;
285         }
286         return ret;
287 }
288
289 static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
290 {
291         int i;
292
293         BUG_ON(acpi && amd);
294 #ifdef CONFIG_ACPI_NUMA
295         if (acpi)
296                 acpi_fake_nodes(nodes, nr_nodes);
297 #endif
298 #ifdef CONFIG_AMD_NUMA
299         if (amd)
300                 amd_fake_nodes(nodes, nr_nodes);
301 #endif
302         if (!acpi && !amd)
303                 for (i = 0; i < nr_cpu_ids; i++)
304                         numa_set_node(i, 0);
305 }
306
307 /*
308  * Setups up nid to range from addr to addr + size.  If the end
309  * boundary is greater than max_addr, then max_addr is used instead.
310  * The return value is 0 if there is additional memory left for
311  * allocation past addr and -1 otherwise.  addr is adjusted to be at
312  * the end of the node.
313  */
314 static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
315 {
316         int ret = 0;
317         nodes[nid].start = *addr;
318         *addr += size;
319         if (*addr >= max_addr) {
320                 *addr = max_addr;
321                 ret = -1;
322         }
323         nodes[nid].end = *addr;
324         node_set(nid, node_possible_map);
325         printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
326                nodes[nid].start, nodes[nid].end,
327                (nodes[nid].end - nodes[nid].start) >> 20);
328         return ret;
329 }
330
331 /*
332  * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
333  * to max_addr.  The return value is the number of nodes allocated.
334  */
335 static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
336 {
337         nodemask_t physnode_mask = NODE_MASK_NONE;
338         u64 size;
339         int big;
340         int ret = 0;
341         int i;
342
343         if (nr_nodes <= 0)
344                 return -1;
345         if (nr_nodes > MAX_NUMNODES) {
346                 pr_info("numa=fake=%d too large, reducing to %d\n",
347                         nr_nodes, MAX_NUMNODES);
348                 nr_nodes = MAX_NUMNODES;
349         }
350
351         size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
352         /*
353          * Calculate the number of big nodes that can be allocated as a result
354          * of consolidating the remainder.
355          */
356         big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
357                 FAKE_NODE_MIN_SIZE;
358
359         size &= FAKE_NODE_MIN_HASH_MASK;
360         if (!size) {
361                 pr_err("Not enough memory for each node.  "
362                         "NUMA emulation disabled.\n");
363                 return -1;
364         }
365
366         for (i = 0; i < MAX_NUMNODES; i++)
367                 if (physnodes[i].start != physnodes[i].end)
368                         node_set(i, physnode_mask);
369
370         /*
371          * Continue to fill physical nodes with fake nodes until there is no
372          * memory left on any of them.
373          */
374         while (nodes_weight(physnode_mask)) {
375                 for_each_node_mask(i, physnode_mask) {
376                         u64 end = physnodes[i].start + size;
377                         u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
378
379                         if (ret < big)
380                                 end += FAKE_NODE_MIN_SIZE;
381
382                         /*
383                          * Continue to add memory to this fake node if its
384                          * non-reserved memory is less than the per-node size.
385                          */
386                         while (end - physnodes[i].start -
387                                 memblock_x86_hole_size(physnodes[i].start, end) < size) {
388                                 end += FAKE_NODE_MIN_SIZE;
389                                 if (end > physnodes[i].end) {
390                                         end = physnodes[i].end;
391                                         break;
392                                 }
393                         }
394
395                         /*
396                          * If there won't be at least FAKE_NODE_MIN_SIZE of
397                          * non-reserved memory in ZONE_DMA32 for the next node,
398                          * this one must extend to the boundary.
399                          */
400                         if (end < dma32_end && dma32_end - end -
401                             memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
402                                 end = dma32_end;
403
404                         /*
405                          * If there won't be enough non-reserved memory for the
406                          * next node, this one must extend to the end of the
407                          * physical node.
408                          */
409                         if (physnodes[i].end - end -
410                             memblock_x86_hole_size(end, physnodes[i].end) < size)
411                                 end = physnodes[i].end;
412
413                         /*
414                          * Avoid allocating more nodes than requested, which can
415                          * happen as a result of rounding down each node's size
416                          * to FAKE_NODE_MIN_SIZE.
417                          */
418                         if (nodes_weight(physnode_mask) + ret >= nr_nodes)
419                                 end = physnodes[i].end;
420
421                         if (setup_node_range(ret++, &physnodes[i].start,
422                                                 end - physnodes[i].start,
423                                                 physnodes[i].end) < 0)
424                                 node_clear(i, physnode_mask);
425                 }
426         }
427         return ret;
428 }
429
430 /*
431  * Returns the end address of a node so that there is at least `size' amount of
432  * non-reserved memory or `max_addr' is reached.
433  */
434 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
435 {
436         u64 end = start + size;
437
438         while (end - start - memblock_x86_hole_size(start, end) < size) {
439                 end += FAKE_NODE_MIN_SIZE;
440                 if (end > max_addr) {
441                         end = max_addr;
442                         break;
443                 }
444         }
445         return end;
446 }
447
448 /*
449  * Sets up fake nodes of `size' interleaved over physical nodes ranging from
450  * `addr' to `max_addr'.  The return value is the number of nodes allocated.
451  */
452 static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
453 {
454         nodemask_t physnode_mask = NODE_MASK_NONE;
455         u64 min_size;
456         int ret = 0;
457         int i;
458
459         if (!size)
460                 return -1;
461         /*
462          * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
463          * increased accordingly if the requested size is too small.  This
464          * creates a uniform distribution of node sizes across the entire
465          * machine (but not necessarily over physical nodes).
466          */
467         min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
468                                                 MAX_NUMNODES;
469         min_size = max(min_size, FAKE_NODE_MIN_SIZE);
470         if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
471                 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
472                                                 FAKE_NODE_MIN_HASH_MASK;
473         if (size < min_size) {
474                 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
475                         size >> 20, min_size >> 20);
476                 size = min_size;
477         }
478         size &= FAKE_NODE_MIN_HASH_MASK;
479
480         for (i = 0; i < MAX_NUMNODES; i++)
481                 if (physnodes[i].start != physnodes[i].end)
482                         node_set(i, physnode_mask);
483         /*
484          * Fill physical nodes with fake nodes of size until there is no memory
485          * left on any of them.
486          */
487         while (nodes_weight(physnode_mask)) {
488                 for_each_node_mask(i, physnode_mask) {
489                         u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
490                         u64 end;
491
492                         end = find_end_of_node(physnodes[i].start,
493                                                 physnodes[i].end, size);
494                         /*
495                          * If there won't be at least FAKE_NODE_MIN_SIZE of
496                          * non-reserved memory in ZONE_DMA32 for the next node,
497                          * this one must extend to the boundary.
498                          */
499                         if (end < dma32_end && dma32_end - end -
500                             memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
501                                 end = dma32_end;
502
503                         /*
504                          * If there won't be enough non-reserved memory for the
505                          * next node, this one must extend to the end of the
506                          * physical node.
507                          */
508                         if (physnodes[i].end - end -
509                             memblock_x86_hole_size(end, physnodes[i].end) < size)
510                                 end = physnodes[i].end;
511
512                         /*
513                          * Setup the fake node that will be allocated as bootmem
514                          * later.  If setup_node_range() returns non-zero, there
515                          * is no more memory available on this physical node.
516                          */
517                         if (setup_node_range(ret++, &physnodes[i].start,
518                                                 end - physnodes[i].start,
519                                                 physnodes[i].end) < 0)
520                                 node_clear(i, physnode_mask);
521                 }
522         }
523         return ret;
524 }
525
526 /*
527  * Sets up the system RAM area from start_pfn to last_pfn according to the
528  * numa=fake command-line option.
529  */
530 static int __init numa_emulation(unsigned long start_pfn,
531                         unsigned long last_pfn, int acpi, int amd)
532 {
533         u64 addr = start_pfn << PAGE_SHIFT;
534         u64 max_addr = last_pfn << PAGE_SHIFT;
535         int num_nodes;
536         int i;
537
538         /*
539          * If the numa=fake command-line contains a 'M' or 'G', it represents
540          * the fixed node size.  Otherwise, if it is just a single number N,
541          * split the system RAM into N fake nodes.
542          */
543         if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
544                 u64 size;
545
546                 size = memparse(cmdline, &cmdline);
547                 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
548         } else {
549                 unsigned long n;
550
551                 n = simple_strtoul(cmdline, NULL, 0);
552                 num_nodes = split_nodes_interleave(addr, max_addr, n);
553         }
554
555         if (num_nodes < 0)
556                 return num_nodes;
557         memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
558         if (memnode_shift < 0) {
559                 memnode_shift = 0;
560                 printk(KERN_ERR "No NUMA hash function found.  NUMA emulation "
561                        "disabled.\n");
562                 return -1;
563         }
564
565         /*
566          * We need to vacate all active ranges that may have been registered for
567          * the e820 memory map.
568          */
569         remove_all_active_ranges();
570         for_each_node_mask(i, node_possible_map)
571                 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
572                                                 nodes[i].end >> PAGE_SHIFT);
573         init_memory_mapping_high();
574         for_each_node_mask(i, node_possible_map)
575                 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
576         setup_physnodes(addr, max_addr, acpi, amd);
577         fake_physnodes(acpi, amd, num_nodes);
578         numa_init_array();
579         return 0;
580 }
581 #endif /* CONFIG_NUMA_EMU */
582
583 static int dummy_numa_init(void)
584 {
585         return 0;
586 }
587
588 static int dummy_scan_nodes(void)
589 {
590         printk(KERN_INFO "%s\n",
591                numa_off ? "NUMA turned off" : "No NUMA configuration found");
592         printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
593                0LU, max_pfn << PAGE_SHIFT);
594
595         /* setup dummy node covering all memory */
596         memnode_shift = 63;
597         memnodemap = memnode.embedded_map;
598         memnodemap[0] = 0;
599         node_set_online(0);
600         node_set(0, node_possible_map);
601         memblock_x86_register_active_regions(0, 0, max_pfn);
602         init_memory_mapping_high();
603         setup_node_bootmem(0, 0, max_pfn << PAGE_SHIFT);
604         numa_init_array();
605
606         return 0;
607 }
608
609 void __init initmem_init(void)
610 {
611         int (*numa_init[])(void) = { [2] = dummy_numa_init };
612         int (*scan_nodes[])(void) = { [2] = dummy_scan_nodes };
613         int i, j;
614
615         if (!numa_off) {
616 #ifdef CONFIG_ACPI_NUMA
617                 numa_init[0] = x86_acpi_numa_init;
618                 scan_nodes[0] = acpi_scan_nodes;
619 #endif
620 #ifdef CONFIG_AMD_NUMA
621                 numa_init[1] = amd_numa_init;
622                 scan_nodes[1] = amd_scan_nodes;
623 #endif
624         }
625
626         for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
627                 if (!numa_init[i])
628                         continue;
629
630                 for (j = 0; j < MAX_LOCAL_APIC; j++)
631                         set_apicid_to_node(j, NUMA_NO_NODE);
632
633                 nodes_clear(node_possible_map);
634                 nodes_clear(node_online_map);
635
636                 if (numa_init[i]() < 0)
637                         continue;
638 #ifdef CONFIG_NUMA_EMU
639                 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
640                 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
641                         return;
642                 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
643                 nodes_clear(node_possible_map);
644                 nodes_clear(node_online_map);
645 #endif
646                 if (!scan_nodes[i]())
647                         return;
648         }
649         BUG();
650 }
651
652 unsigned long __init numa_free_all_bootmem(void)
653 {
654         unsigned long pages = 0;
655         int i;
656
657         for_each_online_node(i)
658                 pages += free_all_bootmem_node(NODE_DATA(i));
659
660         pages += free_all_memory_core_early(MAX_NUMNODES);
661
662         return pages;
663 }
664
665 int __cpuinit numa_cpu_node(int cpu)
666 {
667         int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
668
669         if (apicid != BAD_APICID)
670                 return __apicid_to_node[apicid];
671         return NUMA_NO_NODE;
672 }
673
674 /*
675  * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
676  * of 64bit specific data structures.  The distinction is artificial and
677  * should be removed.  numa_{add|remove}_cpu() are implemented in numa.c
678  * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
679  * enabled.
680  *
681  * NUMA emulation is planned to be made generic and the following and other
682  * related code should be moved to numa.c.
683  */
684 #ifdef CONFIG_NUMA_EMU
685 # ifndef CONFIG_DEBUG_PER_CPU_MAPS
686 void __cpuinit numa_add_cpu(int cpu)
687 {
688         unsigned long addr;
689         int physnid, nid;
690
691         nid = numa_cpu_node(cpu);
692         if (nid == NUMA_NO_NODE)
693                 nid = early_cpu_to_node(cpu);
694         BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
695
696         /*
697          * Use the starting address of the emulated node to find which physical
698          * node it is allocated on.
699          */
700         addr = node_start_pfn(nid) << PAGE_SHIFT;
701         for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
702                 if (addr >= physnodes[physnid].start &&
703                     addr < physnodes[physnid].end)
704                         break;
705
706         /*
707          * Map the cpu to each emulated node that is allocated on the physical
708          * node of the cpu's apic id.
709          */
710         for_each_online_node(nid) {
711                 addr = node_start_pfn(nid) << PAGE_SHIFT;
712                 if (addr >= physnodes[physnid].start &&
713                     addr < physnodes[physnid].end)
714                         cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
715         }
716 }
717
718 void __cpuinit numa_remove_cpu(int cpu)
719 {
720         int i;
721
722         for_each_online_node(i)
723                 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
724 }
725 # else  /* !CONFIG_DEBUG_PER_CPU_MAPS */
726 static void __cpuinit numa_set_cpumask(int cpu, int enable)
727 {
728         int node = early_cpu_to_node(cpu);
729         struct cpumask *mask;
730         int i;
731
732         if (node == NUMA_NO_NODE) {
733                 /* early_cpu_to_node() already emits a warning and trace */
734                 return;
735         }
736         for_each_online_node(i) {
737                 unsigned long addr;
738
739                 addr = node_start_pfn(i) << PAGE_SHIFT;
740                 if (addr < physnodes[node].start ||
741                                         addr >= physnodes[node].end)
742                         continue;
743                 mask = debug_cpumask_set_cpu(cpu, enable);
744                 if (!mask)
745                         return;
746
747                 if (enable)
748                         cpumask_set_cpu(cpu, mask);
749                 else
750                         cpumask_clear_cpu(cpu, mask);
751         }
752 }
753
754 void __cpuinit numa_add_cpu(int cpu)
755 {
756         numa_set_cpumask(cpu, 1);
757 }
758
759 void __cpuinit numa_remove_cpu(int cpu)
760 {
761         numa_set_cpumask(cpu, 0);
762 }
763 # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
764 #endif  /* CONFIG_NUMA_EMU */