287ae798935fb8ea16450ee639bd17aff1e69c9b
[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/acpi.h>
22 #include <asm/amd_nb.h>
23
24 #include "numa_internal.h"
25
26 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
27 EXPORT_SYMBOL(node_data);
28
29 nodemask_t numa_nodes_parsed __initdata;
30
31 static struct numa_meminfo numa_meminfo
32 #ifndef CONFIG_MEMORY_HOTPLUG
33 __initdata
34 #endif
35 ;
36
37 static int numa_distance_cnt;
38 static u8 *numa_distance;
39
40 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
41                                      struct numa_meminfo *mi)
42 {
43         /* ignore zero length blks */
44         if (start == end)
45                 return 0;
46
47         /* whine about and ignore invalid blks */
48         if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
49                 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
50                            nid, start, end);
51                 return 0;
52         }
53
54         if (mi->nr_blks >= NR_NODE_MEMBLKS) {
55                 pr_err("NUMA: too many memblk ranges\n");
56                 return -EINVAL;
57         }
58
59         mi->blk[mi->nr_blks].start = start;
60         mi->blk[mi->nr_blks].end = end;
61         mi->blk[mi->nr_blks].nid = nid;
62         mi->nr_blks++;
63         return 0;
64 }
65
66 /**
67  * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
68  * @idx: Index of memblk to remove
69  * @mi: numa_meminfo to remove memblk from
70  *
71  * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
72  * decrementing @mi->nr_blks.
73  */
74 void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
75 {
76         mi->nr_blks--;
77         memmove(&mi->blk[idx], &mi->blk[idx + 1],
78                 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
79 }
80
81 /**
82  * numa_add_memblk - Add one numa_memblk to numa_meminfo
83  * @nid: NUMA node ID of the new memblk
84  * @start: Start address of the new memblk
85  * @end: End address of the new memblk
86  *
87  * Add a new memblk to the default numa_meminfo.
88  *
89  * RETURNS:
90  * 0 on success, -errno on failure.
91  */
92 int __init numa_add_memblk(int nid, u64 start, u64 end)
93 {
94         return numa_add_memblk_to(nid, start, end, &numa_meminfo);
95 }
96
97 /* Initialize bootmem allocator for a node */
98 static void __init
99 setup_node_bootmem(int nid, unsigned long start, unsigned long end)
100 {
101         const u64 nd_low = (u64)MAX_DMA_PFN << PAGE_SHIFT;
102         const u64 nd_high = (u64)max_pfn_mapped << PAGE_SHIFT;
103         const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
104         unsigned long nd_pa;
105         int tnid;
106
107         /*
108          * Don't confuse VM with a node that doesn't have the
109          * minimum amount of memory:
110          */
111         if (end && (end - start) < NODE_MIN_SIZE)
112                 return;
113
114         start = roundup(start, ZONE_ALIGN);
115
116         printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n",
117                nid, start, end);
118
119         /*
120          * Try to allocate node data on local node and then fall back to
121          * all nodes.  Never allocate in DMA zone.
122          */
123         nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high,
124                                                 nd_size, SMP_CACHE_BYTES);
125         if (nd_pa == MEMBLOCK_ERROR)
126                 nd_pa = memblock_find_in_range(nd_low, nd_high,
127                                                nd_size, SMP_CACHE_BYTES);
128         if (nd_pa == MEMBLOCK_ERROR) {
129                 pr_err("Cannot find %lu bytes in node %d\n", nd_size, nid);
130                 return;
131         }
132         memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA");
133
134         /* report and initialize */
135         printk(KERN_INFO "  NODE_DATA [%016lx - %016lx]\n",
136                nd_pa, nd_pa + nd_size - 1);
137         tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
138         if (tnid != nid)
139                 printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nid, tnid);
140
141         node_data[nid] = __va(nd_pa);
142         memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
143         NODE_DATA(nid)->node_id = nid;
144         NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT;
145         NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT;
146
147         node_set_online(nid);
148 }
149
150 /**
151  * numa_cleanup_meminfo - Cleanup a numa_meminfo
152  * @mi: numa_meminfo to clean up
153  *
154  * Sanitize @mi by merging and removing unncessary memblks.  Also check for
155  * conflicts and clear unused memblks.
156  *
157  * RETURNS:
158  * 0 on success, -errno on failure.
159  */
160 int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
161 {
162         const u64 low = 0;
163         const u64 high = (u64)max_pfn << PAGE_SHIFT;
164         int i, j, k;
165
166         for (i = 0; i < mi->nr_blks; i++) {
167                 struct numa_memblk *bi = &mi->blk[i];
168
169                 /* make sure all blocks are inside the limits */
170                 bi->start = max(bi->start, low);
171                 bi->end = min(bi->end, high);
172
173                 /* and there's no empty block */
174                 if (bi->start >= bi->end) {
175                         numa_remove_memblk_from(i--, mi);
176                         continue;
177                 }
178
179                 for (j = i + 1; j < mi->nr_blks; j++) {
180                         struct numa_memblk *bj = &mi->blk[j];
181                         unsigned long start, end;
182
183                         /*
184                          * See whether there are overlapping blocks.  Whine
185                          * about but allow overlaps of the same nid.  They
186                          * will be merged below.
187                          */
188                         if (bi->end > bj->start && bi->start < bj->end) {
189                                 if (bi->nid != bj->nid) {
190                                         pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
191                                                bi->nid, bi->start, bi->end,
192                                                bj->nid, bj->start, bj->end);
193                                         return -EINVAL;
194                                 }
195                                 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
196                                            bi->nid, bi->start, bi->end,
197                                            bj->start, bj->end);
198                         }
199
200                         /*
201                          * Join together blocks on the same node, holes
202                          * between which don't overlap with memory on other
203                          * nodes.
204                          */
205                         if (bi->nid != bj->nid)
206                                 continue;
207                         start = max(min(bi->start, bj->start), low);
208                         end = min(max(bi->end, bj->end), high);
209                         for (k = 0; k < mi->nr_blks; k++) {
210                                 struct numa_memblk *bk = &mi->blk[k];
211
212                                 if (bi->nid == bk->nid)
213                                         continue;
214                                 if (start < bk->end && end > bk->start)
215                                         break;
216                         }
217                         if (k < mi->nr_blks)
218                                 continue;
219                         printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
220                                bi->nid, bi->start, bi->end, bj->start, bj->end,
221                                start, end);
222                         bi->start = start;
223                         bi->end = end;
224                         numa_remove_memblk_from(j--, mi);
225                 }
226         }
227
228         for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
229                 mi->blk[i].start = mi->blk[i].end = 0;
230                 mi->blk[i].nid = NUMA_NO_NODE;
231         }
232
233         return 0;
234 }
235
236 /*
237  * Set nodes, which have memory in @mi, in *@nodemask.
238  */
239 static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
240                                               const struct numa_meminfo *mi)
241 {
242         int i;
243
244         for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
245                 if (mi->blk[i].start != mi->blk[i].end &&
246                     mi->blk[i].nid != NUMA_NO_NODE)
247                         node_set(mi->blk[i].nid, *nodemask);
248 }
249
250 /**
251  * numa_reset_distance - Reset NUMA distance table
252  *
253  * The current table is freed.  The next numa_set_distance() call will
254  * create a new one.
255  */
256 void __init numa_reset_distance(void)
257 {
258         size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
259
260         /* numa_distance could be 1LU marking allocation failure, test cnt */
261         if (numa_distance_cnt)
262                 memblock_x86_free_range(__pa(numa_distance),
263                                         __pa(numa_distance) + size);
264         numa_distance_cnt = 0;
265         numa_distance = NULL;   /* enable table creation */
266 }
267
268 static int __init numa_alloc_distance(void)
269 {
270         nodemask_t nodes_parsed;
271         size_t size;
272         int i, j, cnt = 0;
273         u64 phys;
274
275         /* size the new table and allocate it */
276         nodes_parsed = numa_nodes_parsed;
277         numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
278
279         for_each_node_mask(i, nodes_parsed)
280                 cnt = i;
281         cnt++;
282         size = cnt * cnt * sizeof(numa_distance[0]);
283
284         phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
285                                       size, PAGE_SIZE);
286         if (phys == MEMBLOCK_ERROR) {
287                 pr_warning("NUMA: Warning: can't allocate distance table!\n");
288                 /* don't retry until explicitly reset */
289                 numa_distance = (void *)1LU;
290                 return -ENOMEM;
291         }
292         memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
293
294         numa_distance = __va(phys);
295         numa_distance_cnt = cnt;
296
297         /* fill with the default distances */
298         for (i = 0; i < cnt; i++)
299                 for (j = 0; j < cnt; j++)
300                         numa_distance[i * cnt + j] = i == j ?
301                                 LOCAL_DISTANCE : REMOTE_DISTANCE;
302         printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
303
304         return 0;
305 }
306
307 /**
308  * numa_set_distance - Set NUMA distance from one NUMA to another
309  * @from: the 'from' node to set distance
310  * @to: the 'to'  node to set distance
311  * @distance: NUMA distance
312  *
313  * Set the distance from node @from to @to to @distance.  If distance table
314  * doesn't exist, one which is large enough to accommodate all the currently
315  * known nodes will be created.
316  *
317  * If such table cannot be allocated, a warning is printed and further
318  * calls are ignored until the distance table is reset with
319  * numa_reset_distance().
320  *
321  * If @from or @to is higher than the highest known node at the time of
322  * table creation or @distance doesn't make sense, the call is ignored.
323  * This is to allow simplification of specific NUMA config implementations.
324  */
325 void __init numa_set_distance(int from, int to, int distance)
326 {
327         if (!numa_distance && numa_alloc_distance() < 0)
328                 return;
329
330         if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
331                 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
332                             from, to, distance);
333                 return;
334         }
335
336         if ((u8)distance != distance ||
337             (from == to && distance != LOCAL_DISTANCE)) {
338                 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
339                              from, to, distance);
340                 return;
341         }
342
343         numa_distance[from * numa_distance_cnt + to] = distance;
344 }
345
346 int __node_distance(int from, int to)
347 {
348         if (from >= numa_distance_cnt || to >= numa_distance_cnt)
349                 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
350         return numa_distance[from * numa_distance_cnt + to];
351 }
352 EXPORT_SYMBOL(__node_distance);
353
354 /*
355  * Sanity check to catch more bad NUMA configurations (they are amazingly
356  * common).  Make sure the nodes cover all memory.
357  */
358 static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
359 {
360         unsigned long numaram, e820ram;
361         int i;
362
363         numaram = 0;
364         for (i = 0; i < mi->nr_blks; i++) {
365                 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
366                 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
367                 numaram += e - s;
368                 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
369                 if ((long)numaram < 0)
370                         numaram = 0;
371         }
372
373         e820ram = max_pfn - (memblock_x86_hole_size(0,
374                                         max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
375         /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
376         if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
377                 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
378                        (numaram << PAGE_SHIFT) >> 20,
379                        (e820ram << PAGE_SHIFT) >> 20);
380                 return false;
381         }
382         return true;
383 }
384
385 static int __init numa_register_memblks(struct numa_meminfo *mi)
386 {
387         int i, nid;
388
389         /* Account for nodes with cpus and no memory */
390         node_possible_map = numa_nodes_parsed;
391         numa_nodemask_from_meminfo(&node_possible_map, mi);
392         if (WARN_ON(nodes_empty(node_possible_map)))
393                 return -EINVAL;
394
395         for (i = 0; i < mi->nr_blks; i++)
396                 memblock_x86_register_active_regions(mi->blk[i].nid,
397                                         mi->blk[i].start >> PAGE_SHIFT,
398                                         mi->blk[i].end >> PAGE_SHIFT);
399
400         /* for out of order entries */
401         sort_node_map();
402         if (!numa_meminfo_cover_memory(mi))
403                 return -EINVAL;
404
405         /* Finally register nodes. */
406         for_each_node_mask(nid, node_possible_map) {
407                 u64 start = (u64)max_pfn << PAGE_SHIFT;
408                 u64 end = 0;
409
410                 for (i = 0; i < mi->nr_blks; i++) {
411                         if (nid != mi->blk[i].nid)
412                                 continue;
413                         start = min(mi->blk[i].start, start);
414                         end = max(mi->blk[i].end, end);
415                 }
416
417                 if (start < end)
418                         setup_node_bootmem(nid, start, end);
419         }
420
421         return 0;
422 }
423
424 /**
425  * dummy_numma_init - Fallback dummy NUMA init
426  *
427  * Used if there's no underlying NUMA architecture, NUMA initialization
428  * fails, or NUMA is disabled on the command line.
429  *
430  * Must online at least one node and add memory blocks that cover all
431  * allowed memory.  This function must not fail.
432  */
433 static int __init dummy_numa_init(void)
434 {
435         printk(KERN_INFO "%s\n",
436                numa_off ? "NUMA turned off" : "No NUMA configuration found");
437         printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
438                0LU, max_pfn << PAGE_SHIFT);
439
440         node_set(0, numa_nodes_parsed);
441         numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
442
443         return 0;
444 }
445
446 static int __init numa_init(int (*init_func)(void))
447 {
448         int i;
449         int ret;
450
451         for (i = 0; i < MAX_LOCAL_APIC; i++)
452                 set_apicid_to_node(i, NUMA_NO_NODE);
453
454         nodes_clear(numa_nodes_parsed);
455         nodes_clear(node_possible_map);
456         nodes_clear(node_online_map);
457         memset(&numa_meminfo, 0, sizeof(numa_meminfo));
458         remove_all_active_ranges();
459         numa_reset_distance();
460
461         ret = init_func();
462         if (ret < 0)
463                 return ret;
464         ret = numa_cleanup_meminfo(&numa_meminfo);
465         if (ret < 0)
466                 return ret;
467
468         numa_emulation(&numa_meminfo, numa_distance_cnt);
469
470         ret = numa_register_memblks(&numa_meminfo);
471         if (ret < 0)
472                 return ret;
473
474         for (i = 0; i < nr_cpu_ids; i++) {
475                 int nid = early_cpu_to_node(i);
476
477                 if (nid == NUMA_NO_NODE)
478                         continue;
479                 if (!node_online(nid))
480                         numa_clear_node(i);
481         }
482         numa_init_array();
483         return 0;
484 }
485
486 void __init initmem_init(void)
487 {
488         if (!numa_off) {
489 #ifdef CONFIG_ACPI_NUMA
490                 if (!numa_init(x86_acpi_numa_init))
491                         return;
492 #endif
493 #ifdef CONFIG_AMD_NUMA
494                 if (!numa_init(amd_numa_init))
495                         return;
496 #endif
497         }
498
499         numa_init(dummy_numa_init);
500 }
501
502 unsigned long __init numa_free_all_bootmem(void)
503 {
504         unsigned long pages = 0;
505         int i;
506
507         for_each_online_node(i)
508                 pages += free_all_bootmem_node(NODE_DATA(i));
509
510         pages += free_all_memory_core_early(MAX_NUMNODES);
511
512         return pages;
513 }
514
515 #ifdef CONFIG_MEMORY_HOTPLUG
516 int memory_add_physaddr_to_nid(u64 start)
517 {
518         struct numa_meminfo *mi = &numa_meminfo;
519         int nid = mi->blk[0].nid;
520         int i;
521
522         for (i = 0; i < mi->nr_blks; i++)
523                 if (mi->blk[i].start <= start && mi->blk[i].end > start)
524                         nid = mi->blk[i].nid;
525         return nid;
526 }
527 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
528 #endif