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