Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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 s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
30         [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
31 };
32
33 int numa_off __initdata;
34 static unsigned long __initdata nodemap_addr;
35 static unsigned long __initdata nodemap_size;
36
37 /*
38  * Map cpu index to node index
39  */
40 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
41 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
42
43 /*
44  * Given a shift value, try to populate memnodemap[]
45  * Returns :
46  * 1 if OK
47  * 0 if memnodmap[] too small (of shift too small)
48  * -1 if node overlap or lost ram (shift too big)
49  */
50 static int __init populate_memnodemap(const struct bootnode *nodes,
51                                       int numnodes, int shift, int *nodeids)
52 {
53         unsigned long addr, end;
54         int i, res = -1;
55
56         memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
57         for (i = 0; i < numnodes; i++) {
58                 addr = nodes[i].start;
59                 end = nodes[i].end;
60                 if (addr >= end)
61                         continue;
62                 if ((end >> shift) >= memnodemapsize)
63                         return 0;
64                 do {
65                         if (memnodemap[addr >> shift] != NUMA_NO_NODE)
66                                 return -1;
67
68                         if (!nodeids)
69                                 memnodemap[addr >> shift] = i;
70                         else
71                                 memnodemap[addr >> shift] = nodeids[i];
72
73                         addr += (1UL << shift);
74                 } while (addr < end);
75                 res = 1;
76         }
77         return res;
78 }
79
80 static int __init allocate_cachealigned_memnodemap(void)
81 {
82         unsigned long addr;
83
84         memnodemap = memnode.embedded_map;
85         if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
86                 return 0;
87
88         addr = 0x8000;
89         nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
90         nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
91                                       nodemap_size, L1_CACHE_BYTES);
92         if (nodemap_addr == MEMBLOCK_ERROR) {
93                 printk(KERN_ERR
94                        "NUMA: Unable to allocate Memory to Node hash map\n");
95                 nodemap_addr = nodemap_size = 0;
96                 return -1;
97         }
98         memnodemap = phys_to_virt(nodemap_addr);
99         memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
100
101         printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
102                nodemap_addr, nodemap_addr + nodemap_size);
103         return 0;
104 }
105
106 /*
107  * The LSB of all start and end addresses in the node map is the value of the
108  * maximum possible shift.
109  */
110 static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
111                                          int numnodes)
112 {
113         int i, nodes_used = 0;
114         unsigned long start, end;
115         unsigned long bitfield = 0, memtop = 0;
116
117         for (i = 0; i < numnodes; i++) {
118                 start = nodes[i].start;
119                 end = nodes[i].end;
120                 if (start >= end)
121                         continue;
122                 bitfield |= start;
123                 nodes_used++;
124                 if (end > memtop)
125                         memtop = end;
126         }
127         if (nodes_used <= 1)
128                 i = 63;
129         else
130                 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
131         memnodemapsize = (memtop >> i)+1;
132         return i;
133 }
134
135 int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
136                               int *nodeids)
137 {
138         int shift;
139
140         shift = extract_lsb_from_nodes(nodes, numnodes);
141         if (allocate_cachealigned_memnodemap())
142                 return -1;
143         printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
144                 shift);
145
146         if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
147                 printk(KERN_INFO "Your memory is not aligned you need to "
148                        "rebuild your kernel with a bigger NODEMAPSIZE "
149                        "shift=%d\n", shift);
150                 return -1;
151         }
152         return shift;
153 }
154
155 int __meminit  __early_pfn_to_nid(unsigned long pfn)
156 {
157         return phys_to_nid(pfn << PAGE_SHIFT);
158 }
159
160 static void * __init early_node_mem(int nodeid, unsigned long start,
161                                     unsigned long end, unsigned long size,
162                                     unsigned long align)
163 {
164         unsigned long mem;
165
166         /*
167          * put it on high as possible
168          * something will go with NODE_DATA
169          */
170         if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
171                 start = MAX_DMA_PFN<<PAGE_SHIFT;
172         if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
173             end > (MAX_DMA32_PFN<<PAGE_SHIFT))
174                 start = MAX_DMA32_PFN<<PAGE_SHIFT;
175         mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
176         if (mem != MEMBLOCK_ERROR)
177                 return __va(mem);
178
179         /* extend the search scope */
180         end = max_pfn_mapped << PAGE_SHIFT;
181         start = MAX_DMA_PFN << PAGE_SHIFT;
182         mem = memblock_find_in_range(start, end, size, align);
183         if (mem != MEMBLOCK_ERROR)
184                 return __va(mem);
185
186         printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
187                        size, nodeid);
188
189         return NULL;
190 }
191
192 /* Initialize bootmem allocator for a node */
193 void __init
194 setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
195 {
196         unsigned long start_pfn, last_pfn, nodedata_phys;
197         const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
198         int nid;
199
200         if (!end)
201                 return;
202
203         /*
204          * Don't confuse VM with a node that doesn't have the
205          * minimum amount of memory:
206          */
207         if (end && (end - start) < NODE_MIN_SIZE)
208                 return;
209
210         start = roundup(start, ZONE_ALIGN);
211
212         printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
213                start, end);
214
215         start_pfn = start >> PAGE_SHIFT;
216         last_pfn = end >> PAGE_SHIFT;
217
218         node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
219                                            SMP_CACHE_BYTES);
220         if (node_data[nodeid] == NULL)
221                 return;
222         nodedata_phys = __pa(node_data[nodeid]);
223         memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
224         printk(KERN_INFO "  NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
225                 nodedata_phys + pgdat_size - 1);
226         nid = phys_to_nid(nodedata_phys);
227         if (nid != nodeid)
228                 printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nodeid, nid);
229
230         memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
231         NODE_DATA(nodeid)->node_id = nodeid;
232         NODE_DATA(nodeid)->node_start_pfn = start_pfn;
233         NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
234
235         node_set_online(nodeid);
236 }
237
238 /*
239  * There are unfortunately some poorly designed mainboards around that
240  * only connect memory to a single CPU. This breaks the 1:1 cpu->node
241  * mapping. To avoid this fill in the mapping for all possible CPUs,
242  * as the number of CPUs is not known yet. We round robin the existing
243  * nodes.
244  */
245 void __init numa_init_array(void)
246 {
247         int rr, i;
248
249         rr = first_node(node_online_map);
250         for (i = 0; i < nr_cpu_ids; i++) {
251                 if (early_cpu_to_node(i) != NUMA_NO_NODE)
252                         continue;
253                 numa_set_node(i, rr);
254                 rr = next_node(rr, node_online_map);
255                 if (rr == MAX_NUMNODES)
256                         rr = first_node(node_online_map);
257         }
258 }
259
260 #ifdef CONFIG_NUMA_EMU
261 /* Numa emulation */
262 static struct bootnode nodes[MAX_NUMNODES] __initdata;
263 static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
264 static char *cmdline __initdata;
265
266 static int __init setup_physnodes(unsigned long start, unsigned long end,
267                                         int acpi, int amd)
268 {
269         int ret = 0;
270         int i;
271
272         memset(physnodes, 0, sizeof(physnodes));
273 #ifdef CONFIG_ACPI_NUMA
274         if (acpi)
275                 acpi_get_nodes(physnodes, start, end);
276 #endif
277 #ifdef CONFIG_AMD_NUMA
278         if (amd)
279                 amd_get_nodes(physnodes);
280 #endif
281         /*
282          * Basic sanity checking on the physical node map: there may be errors
283          * if the SRAT or AMD code incorrectly reported the topology or the mem=
284          * kernel parameter is used.
285          */
286         for (i = 0; i < MAX_NUMNODES; i++) {
287                 if (physnodes[i].start == physnodes[i].end)
288                         continue;
289                 if (physnodes[i].start > end) {
290                         physnodes[i].end = physnodes[i].start;
291                         continue;
292                 }
293                 if (physnodes[i].end < start) {
294                         physnodes[i].start = physnodes[i].end;
295                         continue;
296                 }
297                 if (physnodes[i].start < start)
298                         physnodes[i].start = start;
299                 if (physnodes[i].end > end)
300                         physnodes[i].end = end;
301                 ret++;
302         }
303
304         /*
305          * If no physical topology was detected, a single node is faked to cover
306          * the entire address space.
307          */
308         if (!ret) {
309                 physnodes[ret].start = start;
310                 physnodes[ret].end = end;
311                 ret = 1;
312         }
313         return ret;
314 }
315
316 static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
317 {
318         int i;
319
320         BUG_ON(acpi && amd);
321 #ifdef CONFIG_ACPI_NUMA
322         if (acpi)
323                 acpi_fake_nodes(nodes, nr_nodes);
324 #endif
325 #ifdef CONFIG_AMD_NUMA
326         if (amd)
327                 amd_fake_nodes(nodes, nr_nodes);
328 #endif
329         if (!acpi && !amd)
330                 for (i = 0; i < nr_cpu_ids; i++)
331                         numa_set_node(i, 0);
332 }
333
334 /*
335  * Setups up nid to range from addr to addr + size.  If the end
336  * boundary is greater than max_addr, then max_addr is used instead.
337  * The return value is 0 if there is additional memory left for
338  * allocation past addr and -1 otherwise.  addr is adjusted to be at
339  * the end of the node.
340  */
341 static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
342 {
343         int ret = 0;
344         nodes[nid].start = *addr;
345         *addr += size;
346         if (*addr >= max_addr) {
347                 *addr = max_addr;
348                 ret = -1;
349         }
350         nodes[nid].end = *addr;
351         node_set(nid, node_possible_map);
352         printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
353                nodes[nid].start, nodes[nid].end,
354                (nodes[nid].end - nodes[nid].start) >> 20);
355         return ret;
356 }
357
358 /*
359  * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
360  * to max_addr.  The return value is the number of nodes allocated.
361  */
362 static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
363 {
364         nodemask_t physnode_mask = NODE_MASK_NONE;
365         u64 size;
366         int big;
367         int ret = 0;
368         int i;
369
370         if (nr_nodes <= 0)
371                 return -1;
372         if (nr_nodes > MAX_NUMNODES) {
373                 pr_info("numa=fake=%d too large, reducing to %d\n",
374                         nr_nodes, MAX_NUMNODES);
375                 nr_nodes = MAX_NUMNODES;
376         }
377
378         size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
379         /*
380          * Calculate the number of big nodes that can be allocated as a result
381          * of consolidating the remainder.
382          */
383         big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
384                 FAKE_NODE_MIN_SIZE;
385
386         size &= FAKE_NODE_MIN_HASH_MASK;
387         if (!size) {
388                 pr_err("Not enough memory for each node.  "
389                         "NUMA emulation disabled.\n");
390                 return -1;
391         }
392
393         for (i = 0; i < MAX_NUMNODES; i++)
394                 if (physnodes[i].start != physnodes[i].end)
395                         node_set(i, physnode_mask);
396
397         /*
398          * Continue to fill physical nodes with fake nodes until there is no
399          * memory left on any of them.
400          */
401         while (nodes_weight(physnode_mask)) {
402                 for_each_node_mask(i, physnode_mask) {
403                         u64 end = physnodes[i].start + size;
404                         u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
405
406                         if (ret < big)
407                                 end += FAKE_NODE_MIN_SIZE;
408
409                         /*
410                          * Continue to add memory to this fake node if its
411                          * non-reserved memory is less than the per-node size.
412                          */
413                         while (end - physnodes[i].start -
414                                 memblock_x86_hole_size(physnodes[i].start, end) < size) {
415                                 end += FAKE_NODE_MIN_SIZE;
416                                 if (end > physnodes[i].end) {
417                                         end = physnodes[i].end;
418                                         break;
419                                 }
420                         }
421
422                         /*
423                          * If there won't be at least FAKE_NODE_MIN_SIZE of
424                          * non-reserved memory in ZONE_DMA32 for the next node,
425                          * this one must extend to the boundary.
426                          */
427                         if (end < dma32_end && dma32_end - end -
428                             memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
429                                 end = dma32_end;
430
431                         /*
432                          * If there won't be enough non-reserved memory for the
433                          * next node, this one must extend to the end of the
434                          * physical node.
435                          */
436                         if (physnodes[i].end - end -
437                             memblock_x86_hole_size(end, physnodes[i].end) < size)
438                                 end = physnodes[i].end;
439
440                         /*
441                          * Avoid allocating more nodes than requested, which can
442                          * happen as a result of rounding down each node's size
443                          * to FAKE_NODE_MIN_SIZE.
444                          */
445                         if (nodes_weight(physnode_mask) + ret >= nr_nodes)
446                                 end = physnodes[i].end;
447
448                         if (setup_node_range(ret++, &physnodes[i].start,
449                                                 end - physnodes[i].start,
450                                                 physnodes[i].end) < 0)
451                                 node_clear(i, physnode_mask);
452                 }
453         }
454         return ret;
455 }
456
457 /*
458  * Returns the end address of a node so that there is at least `size' amount of
459  * non-reserved memory or `max_addr' is reached.
460  */
461 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
462 {
463         u64 end = start + size;
464
465         while (end - start - memblock_x86_hole_size(start, end) < size) {
466                 end += FAKE_NODE_MIN_SIZE;
467                 if (end > max_addr) {
468                         end = max_addr;
469                         break;
470                 }
471         }
472         return end;
473 }
474
475 /*
476  * Sets up fake nodes of `size' interleaved over physical nodes ranging from
477  * `addr' to `max_addr'.  The return value is the number of nodes allocated.
478  */
479 static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
480 {
481         nodemask_t physnode_mask = NODE_MASK_NONE;
482         u64 min_size;
483         int ret = 0;
484         int i;
485
486         if (!size)
487                 return -1;
488         /*
489          * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
490          * increased accordingly if the requested size is too small.  This
491          * creates a uniform distribution of node sizes across the entire
492          * machine (but not necessarily over physical nodes).
493          */
494         min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
495                                                 MAX_NUMNODES;
496         min_size = max(min_size, FAKE_NODE_MIN_SIZE);
497         if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
498                 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
499                                                 FAKE_NODE_MIN_HASH_MASK;
500         if (size < min_size) {
501                 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
502                         size >> 20, min_size >> 20);
503                 size = min_size;
504         }
505         size &= FAKE_NODE_MIN_HASH_MASK;
506
507         for (i = 0; i < MAX_NUMNODES; i++)
508                 if (physnodes[i].start != physnodes[i].end)
509                         node_set(i, physnode_mask);
510         /*
511          * Fill physical nodes with fake nodes of size until there is no memory
512          * left on any of them.
513          */
514         while (nodes_weight(physnode_mask)) {
515                 for_each_node_mask(i, physnode_mask) {
516                         u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
517                         u64 end;
518
519                         end = find_end_of_node(physnodes[i].start,
520                                                 physnodes[i].end, size);
521                         /*
522                          * If there won't be at least FAKE_NODE_MIN_SIZE of
523                          * non-reserved memory in ZONE_DMA32 for the next node,
524                          * this one must extend to the boundary.
525                          */
526                         if (end < dma32_end && dma32_end - end -
527                             memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
528                                 end = dma32_end;
529
530                         /*
531                          * If there won't be enough non-reserved memory for the
532                          * next node, this one must extend to the end of the
533                          * physical node.
534                          */
535                         if (physnodes[i].end - end -
536                             memblock_x86_hole_size(end, physnodes[i].end) < size)
537                                 end = physnodes[i].end;
538
539                         /*
540                          * Setup the fake node that will be allocated as bootmem
541                          * later.  If setup_node_range() returns non-zero, there
542                          * is no more memory available on this physical node.
543                          */
544                         if (setup_node_range(ret++, &physnodes[i].start,
545                                                 end - physnodes[i].start,
546                                                 physnodes[i].end) < 0)
547                                 node_clear(i, physnode_mask);
548                 }
549         }
550         return ret;
551 }
552
553 /*
554  * Sets up the system RAM area from start_pfn to last_pfn according to the
555  * numa=fake command-line option.
556  */
557 static int __init numa_emulation(unsigned long start_pfn,
558                         unsigned long last_pfn, int acpi, int amd)
559 {
560         u64 addr = start_pfn << PAGE_SHIFT;
561         u64 max_addr = last_pfn << PAGE_SHIFT;
562         int num_nodes;
563         int i;
564
565         /*
566          * If the numa=fake command-line contains a 'M' or 'G', it represents
567          * the fixed node size.  Otherwise, if it is just a single number N,
568          * split the system RAM into N fake nodes.
569          */
570         if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
571                 u64 size;
572
573                 size = memparse(cmdline, &cmdline);
574                 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
575         } else {
576                 unsigned long n;
577
578                 n = simple_strtoul(cmdline, NULL, 0);
579                 num_nodes = split_nodes_interleave(addr, max_addr, n);
580         }
581
582         if (num_nodes < 0)
583                 return num_nodes;
584         memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
585         if (memnode_shift < 0) {
586                 memnode_shift = 0;
587                 printk(KERN_ERR "No NUMA hash function found.  NUMA emulation "
588                        "disabled.\n");
589                 return -1;
590         }
591
592         /*
593          * We need to vacate all active ranges that may have been registered for
594          * the e820 memory map.
595          */
596         remove_all_active_ranges();
597         for_each_node_mask(i, node_possible_map) {
598                 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
599                                                 nodes[i].end >> PAGE_SHIFT);
600                 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
601         }
602         setup_physnodes(addr, max_addr, acpi, amd);
603         fake_physnodes(acpi, amd, num_nodes);
604         numa_init_array();
605         return 0;
606 }
607 #endif /* CONFIG_NUMA_EMU */
608
609 void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
610                                 int acpi, int amd)
611 {
612         int i;
613
614         nodes_clear(node_possible_map);
615         nodes_clear(node_online_map);
616
617 #ifdef CONFIG_NUMA_EMU
618         setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
619                         acpi, amd);
620         if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
621                 return;
622         setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
623                         acpi, amd);
624         nodes_clear(node_possible_map);
625         nodes_clear(node_online_map);
626 #endif
627
628 #ifdef CONFIG_ACPI_NUMA
629         if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
630                                                   last_pfn << PAGE_SHIFT))
631                 return;
632         nodes_clear(node_possible_map);
633         nodes_clear(node_online_map);
634 #endif
635
636 #ifdef CONFIG_AMD_NUMA
637         if (!numa_off && amd && !amd_scan_nodes())
638                 return;
639         nodes_clear(node_possible_map);
640         nodes_clear(node_online_map);
641 #endif
642         printk(KERN_INFO "%s\n",
643                numa_off ? "NUMA turned off" : "No NUMA configuration found");
644
645         printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
646                start_pfn << PAGE_SHIFT,
647                last_pfn << PAGE_SHIFT);
648         /* setup dummy node covering all memory */
649         memnode_shift = 63;
650         memnodemap = memnode.embedded_map;
651         memnodemap[0] = 0;
652         node_set_online(0);
653         node_set(0, node_possible_map);
654         for (i = 0; i < nr_cpu_ids; i++)
655                 numa_set_node(i, 0);
656         memblock_x86_register_active_regions(0, start_pfn, last_pfn);
657         setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
658 }
659
660 unsigned long __init numa_free_all_bootmem(void)
661 {
662         unsigned long pages = 0;
663         int i;
664
665         for_each_online_node(i)
666                 pages += free_all_bootmem_node(NODE_DATA(i));
667
668         pages += free_all_memory_core_early(MAX_NUMNODES);
669
670         return pages;
671 }
672
673 static __init int numa_setup(char *opt)
674 {
675         if (!opt)
676                 return -EINVAL;
677         if (!strncmp(opt, "off", 3))
678                 numa_off = 1;
679 #ifdef CONFIG_NUMA_EMU
680         if (!strncmp(opt, "fake=", 5))
681                 cmdline = opt + 5;
682 #endif
683 #ifdef CONFIG_ACPI_NUMA
684         if (!strncmp(opt, "noacpi", 6))
685                 acpi_numa = -1;
686 #endif
687         return 0;
688 }
689 early_param("numa", numa_setup);
690
691 #ifdef CONFIG_NUMA
692
693 static __init int find_near_online_node(int node)
694 {
695         int n, val;
696         int min_val = INT_MAX;
697         int best_node = -1;
698
699         for_each_online_node(n) {
700                 val = node_distance(node, n);
701
702                 if (val < min_val) {
703                         min_val = val;
704                         best_node = n;
705                 }
706         }
707
708         return best_node;
709 }
710
711 /*
712  * Setup early cpu_to_node.
713  *
714  * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
715  * and apicid_to_node[] tables have valid entries for a CPU.
716  * This means we skip cpu_to_node[] initialisation for NUMA
717  * emulation and faking node case (when running a kernel compiled
718  * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
719  * is already initialized in a round robin manner at numa_init_array,
720  * prior to this call, and this initialization is good enough
721  * for the fake NUMA cases.
722  *
723  * Called before the per_cpu areas are setup.
724  */
725 void __init init_cpu_to_node(void)
726 {
727         int cpu;
728         u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
729
730         BUG_ON(cpu_to_apicid == NULL);
731
732         for_each_possible_cpu(cpu) {
733                 int node;
734                 u16 apicid = cpu_to_apicid[cpu];
735
736                 if (apicid == BAD_APICID)
737                         continue;
738                 node = apicid_to_node[apicid];
739                 if (node == NUMA_NO_NODE)
740                         continue;
741                 if (!node_online(node))
742                         node = find_near_online_node(node);
743                 numa_set_node(cpu, node);
744         }
745 }
746 #endif
747
748
749 void __cpuinit numa_set_node(int cpu, int node)
750 {
751         int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
752
753         /* early setting, no percpu area yet */
754         if (cpu_to_node_map) {
755                 cpu_to_node_map[cpu] = node;
756                 return;
757         }
758
759 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
760         if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
761                 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
762                 dump_stack();
763                 return;
764         }
765 #endif
766         per_cpu(x86_cpu_to_node_map, cpu) = node;
767
768         if (node != NUMA_NO_NODE)
769                 set_cpu_numa_node(cpu, node);
770 }
771
772 void __cpuinit numa_clear_node(int cpu)
773 {
774         numa_set_node(cpu, NUMA_NO_NODE);
775 }
776
777 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
778
779 #ifndef CONFIG_NUMA_EMU
780 void __cpuinit numa_add_cpu(int cpu)
781 {
782         cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
783 }
784
785 void __cpuinit numa_remove_cpu(int cpu)
786 {
787         cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
788 }
789 #else
790 void __cpuinit numa_add_cpu(int cpu)
791 {
792         unsigned long addr;
793         u16 apicid;
794         int physnid;
795         int nid = NUMA_NO_NODE;
796
797         apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
798         if (apicid != BAD_APICID)
799                 nid = apicid_to_node[apicid];
800         if (nid == NUMA_NO_NODE)
801                 nid = early_cpu_to_node(cpu);
802         BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
803
804         /*
805          * Use the starting address of the emulated node to find which physical
806          * node it is allocated on.
807          */
808         addr = node_start_pfn(nid) << PAGE_SHIFT;
809         for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
810                 if (addr >= physnodes[physnid].start &&
811                     addr < physnodes[physnid].end)
812                         break;
813
814         /*
815          * Map the cpu to each emulated node that is allocated on the physical
816          * node of the cpu's apic id.
817          */
818         for_each_online_node(nid) {
819                 addr = node_start_pfn(nid) << PAGE_SHIFT;
820                 if (addr >= physnodes[physnid].start &&
821                     addr < physnodes[physnid].end)
822                         cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
823         }
824 }
825
826 void __cpuinit numa_remove_cpu(int cpu)
827 {
828         int i;
829
830         for_each_online_node(i)
831                 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
832 }
833 #endif /* !CONFIG_NUMA_EMU */
834
835 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
836 static struct cpumask __cpuinit *debug_cpumask_set_cpu(int cpu, int enable)
837 {
838         int node = early_cpu_to_node(cpu);
839         struct cpumask *mask;
840         char buf[64];
841
842         mask = node_to_cpumask_map[node];
843         if (!mask) {
844                 pr_err("node_to_cpumask_map[%i] NULL\n", node);
845                 dump_stack();
846                 return NULL;
847         }
848
849         cpulist_scnprintf(buf, sizeof(buf), mask);
850         printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
851                 enable ? "numa_add_cpu" : "numa_remove_cpu",
852                 cpu, node, buf);
853         return mask;
854 }
855
856 /*
857  * --------- debug versions of the numa functions ---------
858  */
859 #ifndef CONFIG_NUMA_EMU
860 static void __cpuinit numa_set_cpumask(int cpu, int enable)
861 {
862         struct cpumask *mask;
863
864         mask = debug_cpumask_set_cpu(cpu, enable);
865         if (!mask)
866                 return;
867
868         if (enable)
869                 cpumask_set_cpu(cpu, mask);
870         else
871                 cpumask_clear_cpu(cpu, mask);
872 }
873 #else
874 static void __cpuinit numa_set_cpumask(int cpu, int enable)
875 {
876         int node = early_cpu_to_node(cpu);
877         struct cpumask *mask;
878         int i;
879
880         for_each_online_node(i) {
881                 unsigned long addr;
882
883                 addr = node_start_pfn(i) << PAGE_SHIFT;
884                 if (addr < physnodes[node].start ||
885                                         addr >= physnodes[node].end)
886                         continue;
887                 mask = debug_cpumask_set_cpu(cpu, enable);
888                 if (!mask)
889                         return;
890
891                 if (enable)
892                         cpumask_set_cpu(cpu, mask);
893                 else
894                         cpumask_clear_cpu(cpu, mask);
895         }
896 }
897 #endif /* CONFIG_NUMA_EMU */
898
899 void __cpuinit numa_add_cpu(int cpu)
900 {
901         numa_set_cpumask(cpu, 1);
902 }
903
904 void __cpuinit numa_remove_cpu(int cpu)
905 {
906         numa_set_cpumask(cpu, 0);
907 }
908
909 int __cpu_to_node(int cpu)
910 {
911         if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
912                 printk(KERN_WARNING
913                         "cpu_to_node(%d): usage too early!\n", cpu);
914                 dump_stack();
915                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
916         }
917         return per_cpu(x86_cpu_to_node_map, cpu);
918 }
919 EXPORT_SYMBOL(__cpu_to_node);
920
921 /*
922  * Same function as cpu_to_node() but used if called before the
923  * per_cpu areas are setup.
924  */
925 int early_cpu_to_node(int cpu)
926 {
927         if (early_per_cpu_ptr(x86_cpu_to_node_map))
928                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
929
930         if (!cpu_possible(cpu)) {
931                 printk(KERN_WARNING
932                         "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
933                 dump_stack();
934                 return NUMA_NO_NODE;
935         }
936         return per_cpu(x86_cpu_to_node_map, cpu);
937 }
938
939 /*
940  * --------- end of debug versions of the numa functions ---------
941  */
942
943 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */