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