x86: convert Dprintk to pr_debug
[safe/jmp/linux-2.6] / 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/mmzone.h>
11 #include <linux/ctype.h>
12 #include <linux/module.h>
13 #include <linux/nodemask.h>
14 #include <linux/sched.h>
15
16 #include <asm/e820.h>
17 #include <asm/proto.h>
18 #include <asm/dma.h>
19 #include <asm/numa.h>
20 #include <asm/acpi.h>
21 #include <asm/k8.h>
22
23 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
24 EXPORT_SYMBOL(node_data);
25
26 static bootmem_data_t plat_node_bdata[MAX_NUMNODES];
27
28 struct memnode memnode;
29
30 s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
31         [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
32 };
33
34 int numa_off __initdata;
35 static unsigned long __initdata nodemap_addr;
36 static unsigned long __initdata nodemap_size;
37
38 /*
39  * Given a shift value, try to populate memnodemap[]
40  * Returns :
41  * 1 if OK
42  * 0 if memnodmap[] too small (of shift too small)
43  * -1 if node overlap or lost ram (shift too big)
44  */
45 static int __init populate_memnodemap(const struct bootnode *nodes,
46                                       int numnodes, int shift, int *nodeids)
47 {
48         unsigned long addr, end;
49         int i, res = -1;
50
51         memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
52         for (i = 0; i < numnodes; i++) {
53                 addr = nodes[i].start;
54                 end = nodes[i].end;
55                 if (addr >= end)
56                         continue;
57                 if ((end >> shift) >= memnodemapsize)
58                         return 0;
59                 do {
60                         if (memnodemap[addr >> shift] != NUMA_NO_NODE)
61                                 return -1;
62
63                         if (!nodeids)
64                                 memnodemap[addr >> shift] = i;
65                         else
66                                 memnodemap[addr >> shift] = nodeids[i];
67
68                         addr += (1UL << shift);
69                 } while (addr < end);
70                 res = 1;
71         }
72         return res;
73 }
74
75 static int __init allocate_cachealigned_memnodemap(void)
76 {
77         unsigned long addr;
78
79         memnodemap = memnode.embedded_map;
80         if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
81                 return 0;
82
83         addr = 0x8000;
84         nodemap_size = round_up(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
85         nodemap_addr = find_e820_area(addr, max_pfn<<PAGE_SHIFT,
86                                       nodemap_size, L1_CACHE_BYTES);
87         if (nodemap_addr == -1UL) {
88                 printk(KERN_ERR
89                        "NUMA: Unable to allocate Memory to Node hash map\n");
90                 nodemap_addr = nodemap_size = 0;
91                 return -1;
92         }
93         memnodemap = phys_to_virt(nodemap_addr);
94         reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
95
96         printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
97                nodemap_addr, nodemap_addr + nodemap_size);
98         return 0;
99 }
100
101 /*
102  * The LSB of all start and end addresses in the node map is the value of the
103  * maximum possible shift.
104  */
105 static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
106                                          int numnodes)
107 {
108         int i, nodes_used = 0;
109         unsigned long start, end;
110         unsigned long bitfield = 0, memtop = 0;
111
112         for (i = 0; i < numnodes; i++) {
113                 start = nodes[i].start;
114                 end = nodes[i].end;
115                 if (start >= end)
116                         continue;
117                 bitfield |= start;
118                 nodes_used++;
119                 if (end > memtop)
120                         memtop = end;
121         }
122         if (nodes_used <= 1)
123                 i = 63;
124         else
125                 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
126         memnodemapsize = (memtop >> i)+1;
127         return i;
128 }
129
130 int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
131                               int *nodeids)
132 {
133         int shift;
134
135         shift = extract_lsb_from_nodes(nodes, numnodes);
136         if (allocate_cachealigned_memnodemap())
137                 return -1;
138         printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
139                 shift);
140
141         if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
142                 printk(KERN_INFO "Your memory is not aligned you need to "
143                        "rebuild your kernel with a bigger NODEMAPSIZE "
144                        "shift=%d\n", shift);
145                 return -1;
146         }
147         return shift;
148 }
149
150 int early_pfn_to_nid(unsigned long pfn)
151 {
152         return phys_to_nid(pfn << PAGE_SHIFT);
153 }
154
155 static void * __init early_node_mem(int nodeid, unsigned long start,
156                                     unsigned long end, unsigned long size,
157                                     unsigned long align)
158 {
159         unsigned long mem = find_e820_area(start, end, size, align);
160         void *ptr;
161
162         if (mem != -1L)
163                 return __va(mem);
164
165         ptr = __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
166         if (ptr == NULL) {
167                 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
168                        size, nodeid);
169                 return NULL;
170         }
171         return ptr;
172 }
173
174 /* Initialize bootmem allocator for a node */
175 void __init setup_node_bootmem(int nodeid, unsigned long start,
176                                unsigned long end)
177 {
178         unsigned long start_pfn, last_pfn, bootmap_pages, bootmap_size;
179         unsigned long bootmap_start, nodedata_phys;
180         void *bootmap;
181         const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
182         int nid;
183
184         start = round_up(start, ZONE_ALIGN);
185
186         printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
187                start, end);
188
189         start_pfn = start >> PAGE_SHIFT;
190         last_pfn = end >> PAGE_SHIFT;
191
192         node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
193                                            SMP_CACHE_BYTES);
194         if (node_data[nodeid] == NULL)
195                 return;
196         nodedata_phys = __pa(node_data[nodeid]);
197         printk(KERN_INFO "  NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
198                 nodedata_phys + pgdat_size - 1);
199
200         memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
201         NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
202         NODE_DATA(nodeid)->node_start_pfn = start_pfn;
203         NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
204
205         /*
206          * Find a place for the bootmem map
207          * nodedata_phys could be on other nodes by alloc_bootmem,
208          * so need to sure bootmap_start not to be small, otherwise
209          * early_node_mem will get that with find_e820_area instead
210          * of alloc_bootmem, that could clash with reserved range
211          */
212         bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn);
213         nid = phys_to_nid(nodedata_phys);
214         if (nid == nodeid)
215                 bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
216         else
217                 bootmap_start = round_up(start, PAGE_SIZE);
218         /*
219          * SMP_CACHE_BYTES could be enough, but init_bootmem_node like
220          * to use that to align to PAGE_SIZE
221          */
222         bootmap = early_node_mem(nodeid, bootmap_start, end,
223                                  bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
224         if (bootmap == NULL)  {
225                 if (nodedata_phys < start || nodedata_phys >= end)
226                         free_bootmem(nodedata_phys, pgdat_size);
227                 node_data[nodeid] = NULL;
228                 return;
229         }
230         bootmap_start = __pa(bootmap);
231
232         bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
233                                          bootmap_start >> PAGE_SHIFT,
234                                          start_pfn, last_pfn);
235
236         printk(KERN_INFO "  bootmap [%016lx -  %016lx] pages %lx\n",
237                  bootmap_start, bootmap_start + bootmap_size - 1,
238                  bootmap_pages);
239
240         free_bootmem_with_active_regions(nodeid, end);
241
242         /*
243          * convert early reserve to bootmem reserve earlier
244          * otherwise early_node_mem could use early reserved mem
245          * on previous node
246          */
247         early_res_to_bootmem(start, end);
248
249         /*
250          * in some case early_node_mem could use alloc_bootmem
251          * to get range on other node, don't reserve that again
252          */
253         if (nid != nodeid)
254                 printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nodeid, nid);
255         else
256                 reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys,
257                                         pgdat_size, BOOTMEM_DEFAULT);
258         nid = phys_to_nid(bootmap_start);
259         if (nid != nodeid)
260                 printk(KERN_INFO "    bootmap(%d) on node %d\n", nodeid, nid);
261         else
262                 reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
263                                  bootmap_pages<<PAGE_SHIFT, BOOTMEM_DEFAULT);
264
265 #ifdef CONFIG_ACPI_NUMA
266         srat_reserve_add_area(nodeid);
267 #endif
268         node_set_online(nodeid);
269 }
270
271 /*
272  * There are unfortunately some poorly designed mainboards around that
273  * only connect memory to a single CPU. This breaks the 1:1 cpu->node
274  * mapping. To avoid this fill in the mapping for all possible CPUs,
275  * as the number of CPUs is not known yet. We round robin the existing
276  * nodes.
277  */
278 void __init numa_init_array(void)
279 {
280         int rr, i;
281
282         rr = first_node(node_online_map);
283         for (i = 0; i < NR_CPUS; i++) {
284                 if (early_cpu_to_node(i) != NUMA_NO_NODE)
285                         continue;
286                 numa_set_node(i, rr);
287                 rr = next_node(rr, node_online_map);
288                 if (rr == MAX_NUMNODES)
289                         rr = first_node(node_online_map);
290         }
291 }
292
293 #ifdef CONFIG_NUMA_EMU
294 /* Numa emulation */
295 static char *cmdline __initdata;
296
297 /*
298  * Setups up nid to range from addr to addr + size.  If the end
299  * boundary is greater than max_addr, then max_addr is used instead.
300  * The return value is 0 if there is additional memory left for
301  * allocation past addr and -1 otherwise.  addr is adjusted to be at
302  * the end of the node.
303  */
304 static int __init setup_node_range(int nid, struct bootnode *nodes, u64 *addr,
305                                    u64 size, u64 max_addr)
306 {
307         int ret = 0;
308
309         nodes[nid].start = *addr;
310         *addr += size;
311         if (*addr >= max_addr) {
312                 *addr = max_addr;
313                 ret = -1;
314         }
315         nodes[nid].end = *addr;
316         node_set(nid, node_possible_map);
317         printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
318                nodes[nid].start, nodes[nid].end,
319                (nodes[nid].end - nodes[nid].start) >> 20);
320         return ret;
321 }
322
323 /*
324  * Splits num_nodes nodes up equally starting at node_start.  The return value
325  * is the number of nodes split up and addr is adjusted to be at the end of the
326  * last node allocated.
327  */
328 static int __init split_nodes_equally(struct bootnode *nodes, u64 *addr,
329                                       u64 max_addr, int node_start,
330                                       int num_nodes)
331 {
332         unsigned int big;
333         u64 size;
334         int i;
335
336         if (num_nodes <= 0)
337                 return -1;
338         if (num_nodes > MAX_NUMNODES)
339                 num_nodes = MAX_NUMNODES;
340         size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
341                num_nodes;
342         /*
343          * Calculate the number of big nodes that can be allocated as a result
344          * of consolidating the leftovers.
345          */
346         big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
347               FAKE_NODE_MIN_SIZE;
348
349         /* Round down to nearest FAKE_NODE_MIN_SIZE. */
350         size &= FAKE_NODE_MIN_HASH_MASK;
351         if (!size) {
352                 printk(KERN_ERR "Not enough memory for each node.  "
353                        "NUMA emulation disabled.\n");
354                 return -1;
355         }
356
357         for (i = node_start; i < num_nodes + node_start; i++) {
358                 u64 end = *addr + size;
359
360                 if (i < big)
361                         end += FAKE_NODE_MIN_SIZE;
362                 /*
363                  * The final node can have the remaining system RAM.  Other
364                  * nodes receive roughly the same amount of available pages.
365                  */
366                 if (i == num_nodes + node_start - 1)
367                         end = max_addr;
368                 else
369                         while (end - *addr - e820_hole_size(*addr, end) <
370                                size) {
371                                 end += FAKE_NODE_MIN_SIZE;
372                                 if (end > max_addr) {
373                                         end = max_addr;
374                                         break;
375                                 }
376                         }
377                 if (setup_node_range(i, nodes, addr, end - *addr, max_addr) < 0)
378                         break;
379         }
380         return i - node_start + 1;
381 }
382
383 /*
384  * Splits the remaining system RAM into chunks of size.  The remaining memory is
385  * always assigned to a final node and can be asymmetric.  Returns the number of
386  * nodes split.
387  */
388 static int __init split_nodes_by_size(struct bootnode *nodes, u64 *addr,
389                                       u64 max_addr, int node_start, u64 size)
390 {
391         int i = node_start;
392         size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
393         while (!setup_node_range(i++, nodes, addr, size, max_addr))
394                 ;
395         return i - node_start;
396 }
397
398 /*
399  * Sets up the system RAM area from start_pfn to last_pfn according to the
400  * numa=fake command-line option.
401  */
402 static struct bootnode nodes[MAX_NUMNODES] __initdata;
403
404 static int __init numa_emulation(unsigned long start_pfn, unsigned long last_pfn)
405 {
406         u64 size, addr = start_pfn << PAGE_SHIFT;
407         u64 max_addr = last_pfn << PAGE_SHIFT;
408         int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
409
410         memset(&nodes, 0, sizeof(nodes));
411         /*
412          * If the numa=fake command-line is just a single number N, split the
413          * system RAM into N fake nodes.
414          */
415         if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
416                 long n = simple_strtol(cmdline, NULL, 0);
417
418                 num_nodes = split_nodes_equally(nodes, &addr, max_addr, 0, n);
419                 if (num_nodes < 0)
420                         return num_nodes;
421                 goto out;
422         }
423
424         /* Parse the command line. */
425         for (coeff_flag = 0; ; cmdline++) {
426                 if (*cmdline && isdigit(*cmdline)) {
427                         num = num * 10 + *cmdline - '0';
428                         continue;
429                 }
430                 if (*cmdline == '*') {
431                         if (num > 0)
432                                 coeff = num;
433                         coeff_flag = 1;
434                 }
435                 if (!*cmdline || *cmdline == ',') {
436                         if (!coeff_flag)
437                                 coeff = 1;
438                         /*
439                          * Round down to the nearest FAKE_NODE_MIN_SIZE.
440                          * Command-line coefficients are in megabytes.
441                          */
442                         size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
443                         if (size)
444                                 for (i = 0; i < coeff; i++, num_nodes++)
445                                         if (setup_node_range(num_nodes, nodes,
446                                                 &addr, size, max_addr) < 0)
447                                                 goto done;
448                         if (!*cmdline)
449                                 break;
450                         coeff_flag = 0;
451                         coeff = -1;
452                 }
453                 num = 0;
454         }
455 done:
456         if (!num_nodes)
457                 return -1;
458         /* Fill remainder of system RAM, if appropriate. */
459         if (addr < max_addr) {
460                 if (coeff_flag && coeff < 0) {
461                         /* Split remaining nodes into num-sized chunks */
462                         num_nodes += split_nodes_by_size(nodes, &addr, max_addr,
463                                                          num_nodes, num);
464                         goto out;
465                 }
466                 switch (*(cmdline - 1)) {
467                 case '*':
468                         /* Split remaining nodes into coeff chunks */
469                         if (coeff <= 0)
470                                 break;
471                         num_nodes += split_nodes_equally(nodes, &addr, max_addr,
472                                                          num_nodes, coeff);
473                         break;
474                 case ',':
475                         /* Do not allocate remaining system RAM */
476                         break;
477                 default:
478                         /* Give one final node */
479                         setup_node_range(num_nodes, nodes, &addr,
480                                          max_addr - addr, max_addr);
481                         num_nodes++;
482                 }
483         }
484 out:
485         memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
486         if (memnode_shift < 0) {
487                 memnode_shift = 0;
488                 printk(KERN_ERR "No NUMA hash function found.  NUMA emulation "
489                        "disabled.\n");
490                 return -1;
491         }
492
493         /*
494          * We need to vacate all active ranges that may have been registered by
495          * SRAT and set acpi_numa to -1 so that srat_disabled() always returns
496          * true.  NUMA emulation has succeeded so we will not scan ACPI nodes.
497          */
498         remove_all_active_ranges();
499 #ifdef CONFIG_ACPI_NUMA
500         acpi_numa = -1;
501 #endif
502         for_each_node_mask(i, node_possible_map) {
503                 e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
504                                                 nodes[i].end >> PAGE_SHIFT);
505                 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
506         }
507         acpi_fake_nodes(nodes, num_nodes);
508         numa_init_array();
509         return 0;
510 }
511 #endif /* CONFIG_NUMA_EMU */
512
513 void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn)
514 {
515         int i;
516
517         nodes_clear(node_possible_map);
518         nodes_clear(node_online_map);
519
520 #ifdef CONFIG_NUMA_EMU
521         if (cmdline && !numa_emulation(start_pfn, last_pfn))
522                 return;
523         nodes_clear(node_possible_map);
524         nodes_clear(node_online_map);
525 #endif
526
527 #ifdef CONFIG_ACPI_NUMA
528         if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
529                                           last_pfn << PAGE_SHIFT))
530                 return;
531         nodes_clear(node_possible_map);
532         nodes_clear(node_online_map);
533 #endif
534
535 #ifdef CONFIG_K8_NUMA
536         if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT,
537                                         last_pfn<<PAGE_SHIFT))
538                 return;
539         nodes_clear(node_possible_map);
540         nodes_clear(node_online_map);
541 #endif
542         printk(KERN_INFO "%s\n",
543                numa_off ? "NUMA turned off" : "No NUMA configuration found");
544
545         printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
546                start_pfn << PAGE_SHIFT,
547                last_pfn << PAGE_SHIFT);
548         /* setup dummy node covering all memory */
549         memnode_shift = 63;
550         memnodemap = memnode.embedded_map;
551         memnodemap[0] = 0;
552         node_set_online(0);
553         node_set(0, node_possible_map);
554         for (i = 0; i < NR_CPUS; i++)
555                 numa_set_node(i, 0);
556         e820_register_active_regions(0, start_pfn, last_pfn);
557         setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
558 }
559
560 unsigned long __init numa_free_all_bootmem(void)
561 {
562         unsigned long pages = 0;
563         int i;
564
565         for_each_online_node(i)
566                 pages += free_all_bootmem_node(NODE_DATA(i));
567
568         return pages;
569 }
570
571 void __init paging_init(void)
572 {
573         unsigned long max_zone_pfns[MAX_NR_ZONES];
574
575         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
576         max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
577         max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
578         max_zone_pfns[ZONE_NORMAL] = max_pfn;
579
580         sparse_memory_present_with_active_regions(MAX_NUMNODES);
581         sparse_init();
582
583         free_area_init_nodes(max_zone_pfns);
584 }
585
586 static __init int numa_setup(char *opt)
587 {
588         if (!opt)
589                 return -EINVAL;
590         if (!strncmp(opt, "off", 3))
591                 numa_off = 1;
592 #ifdef CONFIG_NUMA_EMU
593         if (!strncmp(opt, "fake=", 5))
594                 cmdline = opt + 5;
595 #endif
596 #ifdef CONFIG_ACPI_NUMA
597         if (!strncmp(opt, "noacpi", 6))
598                 acpi_numa = -1;
599         if (!strncmp(opt, "hotadd=", 7))
600                 hotadd_percent = simple_strtoul(opt+7, NULL, 10);
601 #endif
602         return 0;
603 }
604 early_param("numa", numa_setup);
605
606 #ifdef CONFIG_NUMA
607 /*
608  * Setup early cpu_to_node.
609  *
610  * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
611  * and apicid_to_node[] tables have valid entries for a CPU.
612  * This means we skip cpu_to_node[] initialisation for NUMA
613  * emulation and faking node case (when running a kernel compiled
614  * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
615  * is already initialized in a round robin manner at numa_init_array,
616  * prior to this call, and this initialization is good enough
617  * for the fake NUMA cases.
618  *
619  * Called before the per_cpu areas are setup.
620  */
621 void __init init_cpu_to_node(void)
622 {
623         int cpu;
624         u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
625
626         BUG_ON(cpu_to_apicid == NULL);
627
628         for_each_possible_cpu(cpu) {
629                 int node;
630                 u16 apicid = cpu_to_apicid[cpu];
631
632                 if (apicid == BAD_APICID)
633                         continue;
634                 node = apicid_to_node[apicid];
635                 if (node == NUMA_NO_NODE)
636                         continue;
637                 if (!node_online(node))
638                         continue;
639                 numa_set_node(cpu, node);
640         }
641 }
642 #endif
643
644