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