x86-32, mm: Remove reference to resume_map_numa_kva()
[deliverable/linux.git] / arch / x86 / mm / numa.c
CommitLineData
71ee73e7 1/* Common code for 32 and 64-bit NUMA */
a4106eae
TH
2#include <linux/kernel.h>
3#include <linux/mm.h>
4#include <linux/string.h>
5#include <linux/init.h>
71ee73e7 6#include <linux/bootmem.h>
a4106eae
TH
7#include <linux/memblock.h>
8#include <linux/mmzone.h>
9#include <linux/ctype.h>
10#include <linux/module.h>
11#include <linux/nodemask.h>
12#include <linux/sched.h>
13#include <linux/topology.h>
14
15#include <asm/e820.h>
16#include <asm/proto.h>
17#include <asm/dma.h>
90321602 18#include <asm/acpi.h>
a4106eae
TH
19#include <asm/amd_nb.h>
20
21#include "numa_internal.h"
90321602
JB
22
23int __initdata numa_off;
e6df595b 24nodemask_t numa_nodes_parsed __initdata;
90321602 25
a4106eae
TH
26struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
27EXPORT_SYMBOL(node_data);
28
29static struct numa_meminfo numa_meminfo
30#ifndef CONFIG_MEMORY_HOTPLUG
31__initdata
32#endif
33;
34
35static int numa_distance_cnt;
36static u8 *numa_distance;
a4106eae 37
90321602
JB
38static __init int numa_setup(char *opt)
39{
40 if (!opt)
41 return -EINVAL;
42 if (!strncmp(opt, "off", 3))
43 numa_off = 1;
44#ifdef CONFIG_NUMA_EMU
45 if (!strncmp(opt, "fake=", 5))
46 numa_emu_cmdline(opt + 5);
47#endif
48#ifdef CONFIG_ACPI_NUMA
49 if (!strncmp(opt, "noacpi", 6))
50 acpi_numa = -1;
51#endif
52 return 0;
53}
54early_param("numa", numa_setup);
71ee73e7 55
71ee73e7 56/*
bbc9e2f4 57 * apicid, cpu, node mappings
71ee73e7 58 */
bbc9e2f4
TH
59s16 __apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
60 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
61};
62
6bd26273
TH
63int __cpuinit numa_cpu_node(int cpu)
64{
65 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
66
67 if (apicid != BAD_APICID)
68 return __apicid_to_node[apicid];
69 return NUMA_NO_NODE;
70}
71
c032ef60 72cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
71ee73e7
RR
73EXPORT_SYMBOL(node_to_cpumask_map);
74
645a7919
TH
75/*
76 * Map cpu index to node index
77 */
645a7919 78DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
645a7919
TH
79EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
80
81void __cpuinit numa_set_node(int cpu, int node)
82{
83 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
84
85 /* early setting, no percpu area yet */
86 if (cpu_to_node_map) {
87 cpu_to_node_map[cpu] = node;
88 return;
89 }
90
91#ifdef CONFIG_DEBUG_PER_CPU_MAPS
92 if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
93 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
94 dump_stack();
95 return;
96 }
97#endif
98 per_cpu(x86_cpu_to_node_map, cpu) = node;
99
100 if (node != NUMA_NO_NODE)
101 set_cpu_numa_node(cpu, node);
102}
103
104void __cpuinit numa_clear_node(int cpu)
105{
106 numa_set_node(cpu, NUMA_NO_NODE);
107}
108
71ee73e7
RR
109/*
110 * Allocate node_to_cpumask_map based on number of available nodes
111 * Requires node_possible_map to be valid.
112 *
9512938b 113 * Note: cpumask_of_node() is not valid until after this is done.
71ee73e7
RR
114 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
115 */
116void __init setup_node_to_cpumask_map(void)
117{
118 unsigned int node, num = 0;
71ee73e7
RR
119
120 /* setup nr_node_ids if not done yet */
121 if (nr_node_ids == MAX_NUMNODES) {
122 for_each_node_mask(node, node_possible_map)
123 num = node;
124 nr_node_ids = num + 1;
125 }
126
127 /* allocate the map */
c032ef60
RR
128 for (node = 0; node < nr_node_ids; node++)
129 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
71ee73e7 130
c032ef60
RR
131 /* cpumask_of_node() will now work */
132 pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
71ee73e7
RR
133}
134
a4106eae
TH
135static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
136 struct numa_meminfo *mi)
137{
138 /* ignore zero length blks */
139 if (start == end)
140 return 0;
141
142 /* whine about and ignore invalid blks */
143 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
365811d6
BH
144 pr_warning("NUMA: Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
145 nid, start, end - 1);
a4106eae
TH
146 return 0;
147 }
148
149 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
150 pr_err("NUMA: too many memblk ranges\n");
151 return -EINVAL;
152 }
153
154 mi->blk[mi->nr_blks].start = start;
155 mi->blk[mi->nr_blks].end = end;
156 mi->blk[mi->nr_blks].nid = nid;
157 mi->nr_blks++;
158 return 0;
159}
160
161/**
162 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
163 * @idx: Index of memblk to remove
164 * @mi: numa_meminfo to remove memblk from
165 *
166 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
167 * decrementing @mi->nr_blks.
168 */
169void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
170{
171 mi->nr_blks--;
172 memmove(&mi->blk[idx], &mi->blk[idx + 1],
173 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
174}
175
176/**
177 * numa_add_memblk - Add one numa_memblk to numa_meminfo
178 * @nid: NUMA node ID of the new memblk
179 * @start: Start address of the new memblk
180 * @end: End address of the new memblk
181 *
182 * Add a new memblk to the default numa_meminfo.
183 *
184 * RETURNS:
185 * 0 on success, -errno on failure.
186 */
187int __init numa_add_memblk(int nid, u64 start, u64 end)
188{
189 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
190}
191
a56bca80
YL
192/* Initialize NODE_DATA for a node on the local memory */
193static void __init setup_node_data(int nid, u64 start, u64 end)
a4106eae 194{
a4106eae 195 const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
7888e96b 196 bool remapped = false;
38f3e1ca 197 u64 nd_pa;
7888e96b 198 void *nd;
a4106eae
TH
199 int tnid;
200
201 /*
202 * Don't confuse VM with a node that doesn't have the
203 * minimum amount of memory:
204 */
205 if (end && (end - start) < NODE_MIN_SIZE)
206 return;
207
208 start = roundup(start, ZONE_ALIGN);
209
365811d6
BH
210 printk(KERN_INFO "Initmem setup node %d [mem %#010Lx-%#010Lx]\n",
211 nid, start, end - 1);
a4106eae
TH
212
213 /*
7888e96b
TH
214 * Allocate node data. Try remap allocator first, node-local
215 * memory and then any node. Never allocate in DMA zone.
a4106eae 216 */
7888e96b
TH
217 nd = alloc_remap(nid, nd_size);
218 if (nd) {
1e9209ed 219 nd_pa = __pa_nodebug(nd);
7888e96b
TH
220 remapped = true;
221 } else {
eb40c4c2 222 nd_pa = memblock_alloc_nid(nd_size, SMP_CACHE_BYTES, nid);
1f5026a7 223 if (!nd_pa) {
7888e96b
TH
224 pr_err("Cannot find %zu bytes in node %d\n",
225 nd_size, nid);
226 return;
227 }
7888e96b 228 nd = __va(nd_pa);
a4106eae 229 }
a4106eae
TH
230
231 /* report and initialize */
365811d6 232 printk(KERN_INFO " NODE_DATA [mem %#010Lx-%#010Lx]%s\n",
7888e96b 233 nd_pa, nd_pa + nd_size - 1, remapped ? " (remapped)" : "");
a4106eae 234 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
7888e96b 235 if (!remapped && tnid != nid)
a4106eae
TH
236 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid);
237
7888e96b 238 node_data[nid] = nd;
a4106eae
TH
239 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
240 NODE_DATA(nid)->node_id = nid;
241 NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT;
242 NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT;
243
244 node_set_online(nid);
245}
246
247/**
248 * numa_cleanup_meminfo - Cleanup a numa_meminfo
249 * @mi: numa_meminfo to clean up
250 *
251 * Sanitize @mi by merging and removing unncessary memblks. Also check for
252 * conflicts and clear unused memblks.
253 *
254 * RETURNS:
255 * 0 on success, -errno on failure.
256 */
257int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
258{
259 const u64 low = 0;
38f3e1ca 260 const u64 high = PFN_PHYS(max_pfn);
a4106eae
TH
261 int i, j, k;
262
e5a10c1b 263 /* first, trim all entries */
a4106eae
TH
264 for (i = 0; i < mi->nr_blks; i++) {
265 struct numa_memblk *bi = &mi->blk[i];
266
267 /* make sure all blocks are inside the limits */
268 bi->start = max(bi->start, low);
269 bi->end = min(bi->end, high);
270
271 /* and there's no empty block */
e5a10c1b 272 if (bi->start >= bi->end)
a4106eae 273 numa_remove_memblk_from(i--, mi);
e5a10c1b
YL
274 }
275
276 /* merge neighboring / overlapping entries */
277 for (i = 0; i < mi->nr_blks; i++) {
278 struct numa_memblk *bi = &mi->blk[i];
a4106eae
TH
279
280 for (j = i + 1; j < mi->nr_blks; j++) {
281 struct numa_memblk *bj = &mi->blk[j];
38f3e1ca 282 u64 start, end;
a4106eae
TH
283
284 /*
285 * See whether there are overlapping blocks. Whine
286 * about but allow overlaps of the same nid. They
287 * will be merged below.
288 */
289 if (bi->end > bj->start && bi->start < bj->end) {
290 if (bi->nid != bj->nid) {
365811d6
BH
291 pr_err("NUMA: node %d [mem %#010Lx-%#010Lx] overlaps with node %d [mem %#010Lx-%#010Lx]\n",
292 bi->nid, bi->start, bi->end - 1,
293 bj->nid, bj->start, bj->end - 1);
a4106eae
TH
294 return -EINVAL;
295 }
365811d6
BH
296 pr_warning("NUMA: Warning: node %d [mem %#010Lx-%#010Lx] overlaps with itself [mem %#010Lx-%#010Lx]\n",
297 bi->nid, bi->start, bi->end - 1,
298 bj->start, bj->end - 1);
a4106eae
TH
299 }
300
301 /*
302 * Join together blocks on the same node, holes
303 * between which don't overlap with memory on other
304 * nodes.
305 */
306 if (bi->nid != bj->nid)
307 continue;
e5a10c1b
YL
308 start = min(bi->start, bj->start);
309 end = max(bi->end, bj->end);
a4106eae
TH
310 for (k = 0; k < mi->nr_blks; k++) {
311 struct numa_memblk *bk = &mi->blk[k];
312
313 if (bi->nid == bk->nid)
314 continue;
315 if (start < bk->end && end > bk->start)
316 break;
317 }
318 if (k < mi->nr_blks)
319 continue;
365811d6
BH
320 printk(KERN_INFO "NUMA: Node %d [mem %#010Lx-%#010Lx] + [mem %#010Lx-%#010Lx] -> [mem %#010Lx-%#010Lx]\n",
321 bi->nid, bi->start, bi->end - 1, bj->start,
322 bj->end - 1, start, end - 1);
a4106eae
TH
323 bi->start = start;
324 bi->end = end;
325 numa_remove_memblk_from(j--, mi);
326 }
327 }
328
e5a10c1b 329 /* clear unused ones */
a4106eae
TH
330 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
331 mi->blk[i].start = mi->blk[i].end = 0;
332 mi->blk[i].nid = NUMA_NO_NODE;
333 }
334
335 return 0;
336}
337
338/*
339 * Set nodes, which have memory in @mi, in *@nodemask.
340 */
341static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
342 const struct numa_meminfo *mi)
343{
344 int i;
345
346 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
347 if (mi->blk[i].start != mi->blk[i].end &&
348 mi->blk[i].nid != NUMA_NO_NODE)
349 node_set(mi->blk[i].nid, *nodemask);
350}
351
352/**
353 * numa_reset_distance - Reset NUMA distance table
354 *
355 * The current table is freed. The next numa_set_distance() call will
356 * create a new one.
357 */
358void __init numa_reset_distance(void)
359{
360 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
361
362 /* numa_distance could be 1LU marking allocation failure, test cnt */
363 if (numa_distance_cnt)
24aa0788 364 memblock_free(__pa(numa_distance), size);
a4106eae
TH
365 numa_distance_cnt = 0;
366 numa_distance = NULL; /* enable table creation */
367}
368
369static int __init numa_alloc_distance(void)
370{
371 nodemask_t nodes_parsed;
372 size_t size;
373 int i, j, cnt = 0;
374 u64 phys;
375
376 /* size the new table and allocate it */
377 nodes_parsed = numa_nodes_parsed;
378 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
379
380 for_each_node_mask(i, nodes_parsed)
381 cnt = i;
382 cnt++;
383 size = cnt * cnt * sizeof(numa_distance[0]);
384
38f3e1ca 385 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
a4106eae 386 size, PAGE_SIZE);
1f5026a7 387 if (!phys) {
a4106eae
TH
388 pr_warning("NUMA: Warning: can't allocate distance table!\n");
389 /* don't retry until explicitly reset */
390 numa_distance = (void *)1LU;
391 return -ENOMEM;
392 }
24aa0788 393 memblock_reserve(phys, size);
a4106eae
TH
394
395 numa_distance = __va(phys);
396 numa_distance_cnt = cnt;
397
398 /* fill with the default distances */
399 for (i = 0; i < cnt; i++)
400 for (j = 0; j < cnt; j++)
401 numa_distance[i * cnt + j] = i == j ?
402 LOCAL_DISTANCE : REMOTE_DISTANCE;
403 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
404
405 return 0;
406}
407
408/**
409 * numa_set_distance - Set NUMA distance from one NUMA to another
410 * @from: the 'from' node to set distance
411 * @to: the 'to' node to set distance
412 * @distance: NUMA distance
413 *
414 * Set the distance from node @from to @to to @distance. If distance table
415 * doesn't exist, one which is large enough to accommodate all the currently
416 * known nodes will be created.
417 *
418 * If such table cannot be allocated, a warning is printed and further
419 * calls are ignored until the distance table is reset with
420 * numa_reset_distance().
421 *
54eed6cb
PH
422 * If @from or @to is higher than the highest known node or lower than zero
423 * at the time of table creation or @distance doesn't make sense, the call
424 * is ignored.
a4106eae
TH
425 * This is to allow simplification of specific NUMA config implementations.
426 */
427void __init numa_set_distance(int from, int to, int distance)
428{
429 if (!numa_distance && numa_alloc_distance() < 0)
430 return;
431
54eed6cb
PH
432 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
433 from < 0 || to < 0) {
434 pr_warn_once("NUMA: Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
a4106eae
TH
435 from, to, distance);
436 return;
437 }
438
439 if ((u8)distance != distance ||
440 (from == to && distance != LOCAL_DISTANCE)) {
441 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
442 from, to, distance);
443 return;
444 }
445
446 numa_distance[from * numa_distance_cnt + to] = distance;
447}
448
449int __node_distance(int from, int to)
450{
451 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
452 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
453 return numa_distance[from * numa_distance_cnt + to];
454}
455EXPORT_SYMBOL(__node_distance);
456
457/*
458 * Sanity check to catch more bad NUMA configurations (they are amazingly
459 * common). Make sure the nodes cover all memory.
460 */
461static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
462{
38f3e1ca 463 u64 numaram, e820ram;
a4106eae
TH
464 int i;
465
466 numaram = 0;
467 for (i = 0; i < mi->nr_blks; i++) {
38f3e1ca
TH
468 u64 s = mi->blk[i].start >> PAGE_SHIFT;
469 u64 e = mi->blk[i].end >> PAGE_SHIFT;
a4106eae
TH
470 numaram += e - s;
471 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
38f3e1ca 472 if ((s64)numaram < 0)
a4106eae
TH
473 numaram = 0;
474 }
475
474b881b
TH
476 e820ram = max_pfn - absent_pages_in_range(0, max_pfn);
477
a4106eae 478 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
38f3e1ca
TH
479 if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
480 printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB e820 RAM. Not used.\n",
a4106eae
TH
481 (numaram << PAGE_SHIFT) >> 20,
482 (e820ram << PAGE_SHIFT) >> 20);
483 return false;
484 }
485 return true;
486}
487
488static int __init numa_register_memblks(struct numa_meminfo *mi)
489{
1e01979c 490 unsigned long uninitialized_var(pfn_align);
a4106eae
TH
491 int i, nid;
492
493 /* Account for nodes with cpus and no memory */
494 node_possible_map = numa_nodes_parsed;
495 numa_nodemask_from_meminfo(&node_possible_map, mi);
496 if (WARN_ON(nodes_empty(node_possible_map)))
497 return -EINVAL;
498
0608f70c
TH
499 for (i = 0; i < mi->nr_blks; i++) {
500 struct numa_memblk *mb = &mi->blk[i];
501 memblock_set_node(mb->start, mb->end - mb->start, mb->nid);
502 }
1e01979c
TH
503
504 /*
505 * If sections array is gonna be used for pfn -> nid mapping, check
506 * whether its granularity is fine enough.
507 */
508#ifdef NODE_NOT_IN_PAGE_FLAGS
509 pfn_align = node_map_pfn_alignment();
510 if (pfn_align && pfn_align < PAGES_PER_SECTION) {
511 printk(KERN_WARNING "Node alignment %LuMB < min %LuMB, rejecting NUMA config\n",
512 PFN_PHYS(pfn_align) >> 20,
513 PFN_PHYS(PAGES_PER_SECTION) >> 20);
514 return -EINVAL;
515 }
516#endif
a4106eae
TH
517 if (!numa_meminfo_cover_memory(mi))
518 return -EINVAL;
519
520 /* Finally register nodes. */
521 for_each_node_mask(nid, node_possible_map) {
38f3e1ca 522 u64 start = PFN_PHYS(max_pfn);
a4106eae
TH
523 u64 end = 0;
524
525 for (i = 0; i < mi->nr_blks; i++) {
526 if (nid != mi->blk[i].nid)
527 continue;
528 start = min(mi->blk[i].start, start);
529 end = max(mi->blk[i].end, end);
530 }
531
532 if (start < end)
a56bca80 533 setup_node_data(nid, start, end);
a4106eae
TH
534 }
535
0608f70c
TH
536 /* Dump memblock with node info and return. */
537 memblock_dump_all();
a4106eae
TH
538 return 0;
539}
a4106eae 540
8db78cc4
TH
541/*
542 * There are unfortunately some poorly designed mainboards around that
543 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
544 * mapping. To avoid this fill in the mapping for all possible CPUs,
545 * as the number of CPUs is not known yet. We round robin the existing
546 * nodes.
547 */
752d4f37 548static void __init numa_init_array(void)
8db78cc4
TH
549{
550 int rr, i;
551
552 rr = first_node(node_online_map);
553 for (i = 0; i < nr_cpu_ids; i++) {
554 if (early_cpu_to_node(i) != NUMA_NO_NODE)
555 continue;
556 numa_set_node(i, rr);
557 rr = next_node(rr, node_online_map);
558 if (rr == MAX_NUMNODES)
559 rr = first_node(node_online_map);
560 }
561}
562
a4106eae
TH
563static int __init numa_init(int (*init_func)(void))
564{
565 int i;
566 int ret;
567
568 for (i = 0; i < MAX_LOCAL_APIC; i++)
569 set_apicid_to_node(i, NUMA_NO_NODE);
570
571 nodes_clear(numa_nodes_parsed);
572 nodes_clear(node_possible_map);
573 nodes_clear(node_online_map);
574 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
0608f70c 575 WARN_ON(memblock_set_node(0, ULLONG_MAX, MAX_NUMNODES));
a4106eae
TH
576 numa_reset_distance();
577
578 ret = init_func();
579 if (ret < 0)
580 return ret;
581 ret = numa_cleanup_meminfo(&numa_meminfo);
582 if (ret < 0)
583 return ret;
584
585 numa_emulation(&numa_meminfo, numa_distance_cnt);
586
587 ret = numa_register_memblks(&numa_meminfo);
588 if (ret < 0)
589 return ret;
590
591 for (i = 0; i < nr_cpu_ids; i++) {
592 int nid = early_cpu_to_node(i);
593
594 if (nid == NUMA_NO_NODE)
595 continue;
596 if (!node_online(nid))
597 numa_clear_node(i);
598 }
599 numa_init_array();
600 return 0;
601}
602
603/**
604 * dummy_numa_init - Fallback dummy NUMA init
605 *
606 * Used if there's no underlying NUMA architecture, NUMA initialization
607 * fails, or NUMA is disabled on the command line.
608 *
609 * Must online at least one node and add memory blocks that cover all
610 * allowed memory. This function must not fail.
611 */
612static int __init dummy_numa_init(void)
613{
614 printk(KERN_INFO "%s\n",
615 numa_off ? "NUMA turned off" : "No NUMA configuration found");
365811d6
BH
616 printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
617 0LLU, PFN_PHYS(max_pfn) - 1);
a4106eae
TH
618
619 node_set(0, numa_nodes_parsed);
38f3e1ca 620 numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
a4106eae
TH
621
622 return 0;
623}
624
625/**
626 * x86_numa_init - Initialize NUMA
627 *
628 * Try each configured NUMA initialization method until one succeeds. The
629 * last fallback is dummy single node config encomapssing whole memory and
630 * never fails.
631 */
632void __init x86_numa_init(void)
633{
634 if (!numa_off) {
bd6709a9
TH
635#ifdef CONFIG_X86_NUMAQ
636 if (!numa_init(numaq_numa_init))
637 return;
638#endif
a4106eae
TH
639#ifdef CONFIG_ACPI_NUMA
640 if (!numa_init(x86_acpi_numa_init))
641 return;
642#endif
643#ifdef CONFIG_AMD_NUMA
644 if (!numa_init(amd_numa_init))
645 return;
646#endif
647 }
648
649 numa_init(dummy_numa_init);
650}
a4106eae 651
8db78cc4
TH
652static __init int find_near_online_node(int node)
653{
654 int n, val;
655 int min_val = INT_MAX;
656 int best_node = -1;
657
658 for_each_online_node(n) {
659 val = node_distance(node, n);
660
661 if (val < min_val) {
662 min_val = val;
663 best_node = n;
664 }
665 }
666
667 return best_node;
668}
669
670/*
671 * Setup early cpu_to_node.
672 *
673 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
674 * and apicid_to_node[] tables have valid entries for a CPU.
675 * This means we skip cpu_to_node[] initialisation for NUMA
676 * emulation and faking node case (when running a kernel compiled
677 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
678 * is already initialized in a round robin manner at numa_init_array,
679 * prior to this call, and this initialization is good enough
680 * for the fake NUMA cases.
681 *
682 * Called before the per_cpu areas are setup.
683 */
684void __init init_cpu_to_node(void)
685{
686 int cpu;
687 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
688
689 BUG_ON(cpu_to_apicid == NULL);
690
691 for_each_possible_cpu(cpu) {
692 int node = numa_cpu_node(cpu);
693
694 if (node == NUMA_NO_NODE)
695 continue;
696 if (!node_online(node))
697 node = find_near_online_node(node);
698 numa_set_node(cpu, node);
699 }
700}
701
de2d9445
TH
702#ifndef CONFIG_DEBUG_PER_CPU_MAPS
703
704# ifndef CONFIG_NUMA_EMU
705void __cpuinit numa_add_cpu(int cpu)
706{
707 cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
708}
709
710void __cpuinit numa_remove_cpu(int cpu)
711{
712 cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
713}
714# endif /* !CONFIG_NUMA_EMU */
715
716#else /* !CONFIG_DEBUG_PER_CPU_MAPS */
645a7919
TH
717
718int __cpu_to_node(int cpu)
719{
720 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
721 printk(KERN_WARNING
722 "cpu_to_node(%d): usage too early!\n", cpu);
723 dump_stack();
724 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
725 }
726 return per_cpu(x86_cpu_to_node_map, cpu);
727}
728EXPORT_SYMBOL(__cpu_to_node);
729
730/*
731 * Same function as cpu_to_node() but used if called before the
732 * per_cpu areas are setup.
733 */
734int early_cpu_to_node(int cpu)
735{
736 if (early_per_cpu_ptr(x86_cpu_to_node_map))
737 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
738
739 if (!cpu_possible(cpu)) {
740 printk(KERN_WARNING
741 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
742 dump_stack();
743 return NUMA_NO_NODE;
744 }
745 return per_cpu(x86_cpu_to_node_map, cpu);
746}
747
7a6c6547 748void debug_cpumask_set_cpu(int cpu, int node, bool enable)
de2d9445 749{
de2d9445
TH
750 struct cpumask *mask;
751 char buf[64];
752
14392fd3
DR
753 if (node == NUMA_NO_NODE) {
754 /* early_cpu_to_node() already emits a warning and trace */
7a6c6547 755 return;
14392fd3 756 }
de2d9445
TH
757 mask = node_to_cpumask_map[node];
758 if (!mask) {
759 pr_err("node_to_cpumask_map[%i] NULL\n", node);
760 dump_stack();
7a6c6547 761 return;
de2d9445
TH
762 }
763
7a6c6547
DR
764 if (enable)
765 cpumask_set_cpu(cpu, mask);
766 else
767 cpumask_clear_cpu(cpu, mask);
768
de2d9445
TH
769 cpulist_scnprintf(buf, sizeof(buf), mask);
770 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
771 enable ? "numa_add_cpu" : "numa_remove_cpu",
772 cpu, node, buf);
7a6c6547 773 return;
de2d9445
TH
774}
775
776# ifndef CONFIG_NUMA_EMU
7a6c6547 777static void __cpuinit numa_set_cpumask(int cpu, bool enable)
de2d9445 778{
7a6c6547 779 debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
de2d9445
TH
780}
781
782void __cpuinit numa_add_cpu(int cpu)
783{
7a6c6547 784 numa_set_cpumask(cpu, true);
de2d9445
TH
785}
786
787void __cpuinit numa_remove_cpu(int cpu)
788{
7a6c6547 789 numa_set_cpumask(cpu, false);
de2d9445
TH
790}
791# endif /* !CONFIG_NUMA_EMU */
792
71ee73e7
RR
793/*
794 * Returns a pointer to the bitmask of CPUs on Node 'node'.
795 */
73e907de 796const struct cpumask *cpumask_of_node(int node)
71ee73e7 797{
71ee73e7
RR
798 if (node >= nr_node_ids) {
799 printk(KERN_WARNING
800 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
801 node, nr_node_ids);
802 dump_stack();
803 return cpu_none_mask;
804 }
c032ef60
RR
805 if (node_to_cpumask_map[node] == NULL) {
806 printk(KERN_WARNING
807 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
808 node);
809 dump_stack();
810 return cpu_online_mask;
811 }
0b966252 812 return node_to_cpumask_map[node];
71ee73e7
RR
813}
814EXPORT_SYMBOL(cpumask_of_node);
645a7919 815
de2d9445 816#endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
a4106eae 817
bd6709a9 818#ifdef CONFIG_MEMORY_HOTPLUG
a4106eae
TH
819int memory_add_physaddr_to_nid(u64 start)
820{
821 struct numa_meminfo *mi = &numa_meminfo;
822 int nid = mi->blk[0].nid;
823 int i;
824
825 for (i = 0; i < mi->nr_blks; i++)
826 if (mi->blk[i].start <= start && mi->blk[i].end > start)
827 nid = mi->blk[i].nid;
828 return nid;
829}
830EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
831#endif
This page took 0.245566 seconds and 5 git commands to generate.