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