Commit | Line | Data |
---|---|---|
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 | |
23 | int __initdata numa_off; | |
e6df595b | 24 | nodemask_t numa_nodes_parsed __initdata; |
90321602 | 25 | |
a4106eae TH |
26 | struct pglist_data *node_data[MAX_NUMNODES] __read_mostly; |
27 | EXPORT_SYMBOL(node_data); | |
28 | ||
29 | static struct numa_meminfo numa_meminfo | |
30 | #ifndef CONFIG_MEMORY_HOTPLUG | |
31 | __initdata | |
32 | #endif | |
33 | ; | |
34 | ||
35 | static int numa_distance_cnt; | |
36 | static u8 *numa_distance; | |
a4106eae | 37 | |
90321602 JB |
38 | static __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 | } | |
54 | early_param("numa", numa_setup); | |
71ee73e7 | 55 | |
71ee73e7 | 56 | /* |
bbc9e2f4 | 57 | * apicid, cpu, node mappings |
71ee73e7 | 58 | */ |
bbc9e2f4 TH |
59 | s16 __apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = { |
60 | [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE | |
61 | }; | |
62 | ||
6bd26273 TH |
63 | int __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 | 72 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; |
71ee73e7 RR |
73 | EXPORT_SYMBOL(node_to_cpumask_map); |
74 | ||
645a7919 TH |
75 | /* |
76 | * Map cpu index to node index | |
77 | */ | |
645a7919 | 78 | DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE); |
645a7919 TH |
79 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map); |
80 | ||
81 | void __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 | ||
104 | void __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 | * | |
113 | * Note: node_to_cpumask() is not valid until after this is done. | |
114 | * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.) | |
115 | */ | |
116 | void __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 |
135 | static 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) { | |
144 | pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n", | |
145 | nid, start, end); | |
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 | */ | |
169 | void __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 | ||
744baba0 | 176 | #ifdef CONFIG_X86_64 |
a4106eae TH |
177 | /** |
178 | * numa_add_memblk - Add one numa_memblk to numa_meminfo | |
179 | * @nid: NUMA node ID of the new memblk | |
180 | * @start: Start address of the new memblk | |
181 | * @end: End address of the new memblk | |
182 | * | |
183 | * Add a new memblk to the default numa_meminfo. | |
184 | * | |
185 | * RETURNS: | |
186 | * 0 on success, -errno on failure. | |
187 | */ | |
188 | int __init numa_add_memblk(int nid, u64 start, u64 end) | |
189 | { | |
190 | return numa_add_memblk_to(nid, start, end, &numa_meminfo); | |
191 | } | |
744baba0 | 192 | #endif |
a4106eae TH |
193 | |
194 | /* Initialize bootmem allocator for a node */ | |
195 | static void __init | |
196 | setup_node_bootmem(int nid, unsigned long start, unsigned long end) | |
197 | { | |
198 | const u64 nd_low = (u64)MAX_DMA_PFN << PAGE_SHIFT; | |
199 | const u64 nd_high = (u64)max_pfn_mapped << PAGE_SHIFT; | |
200 | const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE); | |
201 | unsigned long nd_pa; | |
202 | int tnid; | |
203 | ||
204 | /* | |
205 | * Don't confuse VM with a node that doesn't have the | |
206 | * minimum amount of memory: | |
207 | */ | |
208 | if (end && (end - start) < NODE_MIN_SIZE) | |
209 | return; | |
210 | ||
211 | start = roundup(start, ZONE_ALIGN); | |
212 | ||
213 | printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", | |
214 | nid, start, end); | |
215 | ||
216 | /* | |
217 | * Try to allocate node data on local node and then fall back to | |
218 | * all nodes. Never allocate in DMA zone. | |
219 | */ | |
220 | nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high, | |
221 | nd_size, SMP_CACHE_BYTES); | |
222 | if (nd_pa == MEMBLOCK_ERROR) | |
223 | nd_pa = memblock_find_in_range(nd_low, nd_high, | |
224 | nd_size, SMP_CACHE_BYTES); | |
225 | if (nd_pa == MEMBLOCK_ERROR) { | |
226 | pr_err("Cannot find %lu bytes in node %d\n", nd_size, nid); | |
227 | return; | |
228 | } | |
229 | memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA"); | |
230 | ||
231 | /* report and initialize */ | |
232 | printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", | |
233 | nd_pa, nd_pa + nd_size - 1); | |
234 | tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT); | |
235 | if (tnid != nid) | |
236 | printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid); | |
237 | ||
238 | node_data[nid] = __va(nd_pa); | |
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 | */ | |
257 | int __init numa_cleanup_meminfo(struct numa_meminfo *mi) | |
258 | { | |
259 | const u64 low = 0; | |
260 | const u64 high = (u64)max_pfn << PAGE_SHIFT; | |
261 | int i, j, k; | |
262 | ||
263 | for (i = 0; i < mi->nr_blks; i++) { | |
264 | struct numa_memblk *bi = &mi->blk[i]; | |
265 | ||
266 | /* make sure all blocks are inside the limits */ | |
267 | bi->start = max(bi->start, low); | |
268 | bi->end = min(bi->end, high); | |
269 | ||
270 | /* and there's no empty block */ | |
271 | if (bi->start >= bi->end) { | |
272 | numa_remove_memblk_from(i--, mi); | |
273 | continue; | |
274 | } | |
275 | ||
276 | for (j = i + 1; j < mi->nr_blks; j++) { | |
277 | struct numa_memblk *bj = &mi->blk[j]; | |
278 | unsigned long start, end; | |
279 | ||
280 | /* | |
281 | * See whether there are overlapping blocks. Whine | |
282 | * about but allow overlaps of the same nid. They | |
283 | * will be merged below. | |
284 | */ | |
285 | if (bi->end > bj->start && bi->start < bj->end) { | |
286 | if (bi->nid != bj->nid) { | |
287 | pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n", | |
288 | bi->nid, bi->start, bi->end, | |
289 | bj->nid, bj->start, bj->end); | |
290 | return -EINVAL; | |
291 | } | |
292 | pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n", | |
293 | bi->nid, bi->start, bi->end, | |
294 | bj->start, bj->end); | |
295 | } | |
296 | ||
297 | /* | |
298 | * Join together blocks on the same node, holes | |
299 | * between which don't overlap with memory on other | |
300 | * nodes. | |
301 | */ | |
302 | if (bi->nid != bj->nid) | |
303 | continue; | |
304 | start = max(min(bi->start, bj->start), low); | |
305 | end = min(max(bi->end, bj->end), high); | |
306 | for (k = 0; k < mi->nr_blks; k++) { | |
307 | struct numa_memblk *bk = &mi->blk[k]; | |
308 | ||
309 | if (bi->nid == bk->nid) | |
310 | continue; | |
311 | if (start < bk->end && end > bk->start) | |
312 | break; | |
313 | } | |
314 | if (k < mi->nr_blks) | |
315 | continue; | |
316 | printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n", | |
317 | bi->nid, bi->start, bi->end, bj->start, bj->end, | |
318 | start, end); | |
319 | bi->start = start; | |
320 | bi->end = end; | |
321 | numa_remove_memblk_from(j--, mi); | |
322 | } | |
323 | } | |
324 | ||
325 | for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) { | |
326 | mi->blk[i].start = mi->blk[i].end = 0; | |
327 | mi->blk[i].nid = NUMA_NO_NODE; | |
328 | } | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | /* | |
334 | * Set nodes, which have memory in @mi, in *@nodemask. | |
335 | */ | |
336 | static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask, | |
337 | const struct numa_meminfo *mi) | |
338 | { | |
339 | int i; | |
340 | ||
341 | for (i = 0; i < ARRAY_SIZE(mi->blk); i++) | |
342 | if (mi->blk[i].start != mi->blk[i].end && | |
343 | mi->blk[i].nid != NUMA_NO_NODE) | |
344 | node_set(mi->blk[i].nid, *nodemask); | |
345 | } | |
346 | ||
347 | /** | |
348 | * numa_reset_distance - Reset NUMA distance table | |
349 | * | |
350 | * The current table is freed. The next numa_set_distance() call will | |
351 | * create a new one. | |
352 | */ | |
353 | void __init numa_reset_distance(void) | |
354 | { | |
355 | size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]); | |
356 | ||
357 | /* numa_distance could be 1LU marking allocation failure, test cnt */ | |
358 | if (numa_distance_cnt) | |
359 | memblock_x86_free_range(__pa(numa_distance), | |
360 | __pa(numa_distance) + size); | |
361 | numa_distance_cnt = 0; | |
362 | numa_distance = NULL; /* enable table creation */ | |
363 | } | |
364 | ||
365 | static int __init numa_alloc_distance(void) | |
366 | { | |
367 | nodemask_t nodes_parsed; | |
368 | size_t size; | |
369 | int i, j, cnt = 0; | |
370 | u64 phys; | |
371 | ||
372 | /* size the new table and allocate it */ | |
373 | nodes_parsed = numa_nodes_parsed; | |
374 | numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo); | |
375 | ||
376 | for_each_node_mask(i, nodes_parsed) | |
377 | cnt = i; | |
378 | cnt++; | |
379 | size = cnt * cnt * sizeof(numa_distance[0]); | |
380 | ||
381 | phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT, | |
382 | size, PAGE_SIZE); | |
383 | if (phys == MEMBLOCK_ERROR) { | |
384 | pr_warning("NUMA: Warning: can't allocate distance table!\n"); | |
385 | /* don't retry until explicitly reset */ | |
386 | numa_distance = (void *)1LU; | |
387 | return -ENOMEM; | |
388 | } | |
389 | memblock_x86_reserve_range(phys, phys + size, "NUMA DIST"); | |
390 | ||
391 | numa_distance = __va(phys); | |
392 | numa_distance_cnt = cnt; | |
393 | ||
394 | /* fill with the default distances */ | |
395 | for (i = 0; i < cnt; i++) | |
396 | for (j = 0; j < cnt; j++) | |
397 | numa_distance[i * cnt + j] = i == j ? | |
398 | LOCAL_DISTANCE : REMOTE_DISTANCE; | |
399 | printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt); | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
744baba0 | 404 | #ifdef CONFIG_X86_64 |
a4106eae TH |
405 | /** |
406 | * numa_set_distance - Set NUMA distance from one NUMA to another | |
407 | * @from: the 'from' node to set distance | |
408 | * @to: the 'to' node to set distance | |
409 | * @distance: NUMA distance | |
410 | * | |
411 | * Set the distance from node @from to @to to @distance. If distance table | |
412 | * doesn't exist, one which is large enough to accommodate all the currently | |
413 | * known nodes will be created. | |
414 | * | |
415 | * If such table cannot be allocated, a warning is printed and further | |
416 | * calls are ignored until the distance table is reset with | |
417 | * numa_reset_distance(). | |
418 | * | |
419 | * If @from or @to is higher than the highest known node at the time of | |
420 | * table creation or @distance doesn't make sense, the call is ignored. | |
421 | * This is to allow simplification of specific NUMA config implementations. | |
422 | */ | |
423 | void __init numa_set_distance(int from, int to, int distance) | |
424 | { | |
425 | if (!numa_distance && numa_alloc_distance() < 0) | |
426 | return; | |
427 | ||
428 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) { | |
429 | printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n", | |
430 | from, to, distance); | |
431 | return; | |
432 | } | |
433 | ||
434 | if ((u8)distance != distance || | |
435 | (from == to && distance != LOCAL_DISTANCE)) { | |
436 | pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n", | |
437 | from, to, distance); | |
438 | return; | |
439 | } | |
440 | ||
441 | numa_distance[from * numa_distance_cnt + to] = distance; | |
442 | } | |
744baba0 | 443 | #endif |
a4106eae TH |
444 | |
445 | int __node_distance(int from, int to) | |
446 | { | |
447 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) | |
448 | return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE; | |
449 | return numa_distance[from * numa_distance_cnt + to]; | |
450 | } | |
451 | EXPORT_SYMBOL(__node_distance); | |
452 | ||
453 | /* | |
454 | * Sanity check to catch more bad NUMA configurations (they are amazingly | |
455 | * common). Make sure the nodes cover all memory. | |
456 | */ | |
457 | static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi) | |
458 | { | |
459 | unsigned long numaram, e820ram; | |
460 | int i; | |
461 | ||
462 | numaram = 0; | |
463 | for (i = 0; i < mi->nr_blks; i++) { | |
464 | unsigned long s = mi->blk[i].start >> PAGE_SHIFT; | |
465 | unsigned long e = mi->blk[i].end >> PAGE_SHIFT; | |
466 | numaram += e - s; | |
467 | numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e); | |
468 | if ((long)numaram < 0) | |
469 | numaram = 0; | |
470 | } | |
471 | ||
472 | e820ram = max_pfn - (memblock_x86_hole_size(0, | |
473 | max_pfn << PAGE_SHIFT) >> PAGE_SHIFT); | |
474 | /* We seem to lose 3 pages somewhere. Allow 1M of slack. */ | |
475 | if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) { | |
476 | printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n", | |
477 | (numaram << PAGE_SHIFT) >> 20, | |
478 | (e820ram << PAGE_SHIFT) >> 20); | |
479 | return false; | |
480 | } | |
481 | return true; | |
482 | } | |
483 | ||
484 | static int __init numa_register_memblks(struct numa_meminfo *mi) | |
485 | { | |
486 | int i, nid; | |
487 | ||
488 | /* Account for nodes with cpus and no memory */ | |
489 | node_possible_map = numa_nodes_parsed; | |
490 | numa_nodemask_from_meminfo(&node_possible_map, mi); | |
491 | if (WARN_ON(nodes_empty(node_possible_map))) | |
492 | return -EINVAL; | |
493 | ||
494 | for (i = 0; i < mi->nr_blks; i++) | |
495 | memblock_x86_register_active_regions(mi->blk[i].nid, | |
496 | mi->blk[i].start >> PAGE_SHIFT, | |
497 | mi->blk[i].end >> PAGE_SHIFT); | |
498 | ||
499 | /* for out of order entries */ | |
500 | sort_node_map(); | |
501 | if (!numa_meminfo_cover_memory(mi)) | |
502 | return -EINVAL; | |
503 | ||
504 | /* Finally register nodes. */ | |
505 | for_each_node_mask(nid, node_possible_map) { | |
506 | u64 start = (u64)max_pfn << PAGE_SHIFT; | |
507 | u64 end = 0; | |
508 | ||
509 | for (i = 0; i < mi->nr_blks; i++) { | |
510 | if (nid != mi->blk[i].nid) | |
511 | continue; | |
512 | start = min(mi->blk[i].start, start); | |
513 | end = max(mi->blk[i].end, end); | |
514 | } | |
515 | ||
516 | if (start < end) | |
517 | setup_node_bootmem(nid, start, end); | |
518 | } | |
519 | ||
520 | return 0; | |
521 | } | |
a4106eae | 522 | |
8db78cc4 TH |
523 | /* |
524 | * There are unfortunately some poorly designed mainboards around that | |
525 | * only connect memory to a single CPU. This breaks the 1:1 cpu->node | |
526 | * mapping. To avoid this fill in the mapping for all possible CPUs, | |
527 | * as the number of CPUs is not known yet. We round robin the existing | |
528 | * nodes. | |
529 | */ | |
530 | void __init numa_init_array(void) | |
531 | { | |
532 | int rr, i; | |
533 | ||
534 | rr = first_node(node_online_map); | |
535 | for (i = 0; i < nr_cpu_ids; i++) { | |
536 | if (early_cpu_to_node(i) != NUMA_NO_NODE) | |
537 | continue; | |
538 | numa_set_node(i, rr); | |
539 | rr = next_node(rr, node_online_map); | |
540 | if (rr == MAX_NUMNODES) | |
541 | rr = first_node(node_online_map); | |
542 | } | |
543 | } | |
544 | ||
a4106eae TH |
545 | static int __init numa_init(int (*init_func)(void)) |
546 | { | |
547 | int i; | |
548 | int ret; | |
549 | ||
550 | for (i = 0; i < MAX_LOCAL_APIC; i++) | |
551 | set_apicid_to_node(i, NUMA_NO_NODE); | |
552 | ||
553 | nodes_clear(numa_nodes_parsed); | |
554 | nodes_clear(node_possible_map); | |
555 | nodes_clear(node_online_map); | |
556 | memset(&numa_meminfo, 0, sizeof(numa_meminfo)); | |
557 | remove_all_active_ranges(); | |
558 | numa_reset_distance(); | |
559 | ||
560 | ret = init_func(); | |
561 | if (ret < 0) | |
562 | return ret; | |
563 | ret = numa_cleanup_meminfo(&numa_meminfo); | |
564 | if (ret < 0) | |
565 | return ret; | |
566 | ||
567 | numa_emulation(&numa_meminfo, numa_distance_cnt); | |
568 | ||
569 | ret = numa_register_memblks(&numa_meminfo); | |
570 | if (ret < 0) | |
571 | return ret; | |
572 | ||
573 | for (i = 0; i < nr_cpu_ids; i++) { | |
574 | int nid = early_cpu_to_node(i); | |
575 | ||
576 | if (nid == NUMA_NO_NODE) | |
577 | continue; | |
578 | if (!node_online(nid)) | |
579 | numa_clear_node(i); | |
580 | } | |
581 | numa_init_array(); | |
582 | return 0; | |
583 | } | |
584 | ||
585 | /** | |
586 | * dummy_numa_init - Fallback dummy NUMA init | |
587 | * | |
588 | * Used if there's no underlying NUMA architecture, NUMA initialization | |
589 | * fails, or NUMA is disabled on the command line. | |
590 | * | |
591 | * Must online at least one node and add memory blocks that cover all | |
592 | * allowed memory. This function must not fail. | |
593 | */ | |
594 | static int __init dummy_numa_init(void) | |
595 | { | |
596 | printk(KERN_INFO "%s\n", | |
597 | numa_off ? "NUMA turned off" : "No NUMA configuration found"); | |
598 | printk(KERN_INFO "Faking a node at %016lx-%016lx\n", | |
599 | 0LU, max_pfn << PAGE_SHIFT); | |
600 | ||
601 | node_set(0, numa_nodes_parsed); | |
602 | numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT); | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | /** | |
608 | * x86_numa_init - Initialize NUMA | |
609 | * | |
610 | * Try each configured NUMA initialization method until one succeeds. The | |
611 | * last fallback is dummy single node config encomapssing whole memory and | |
612 | * never fails. | |
613 | */ | |
614 | void __init x86_numa_init(void) | |
615 | { | |
616 | if (!numa_off) { | |
617 | #ifdef CONFIG_ACPI_NUMA | |
618 | if (!numa_init(x86_acpi_numa_init)) | |
619 | return; | |
620 | #endif | |
621 | #ifdef CONFIG_AMD_NUMA | |
622 | if (!numa_init(amd_numa_init)) | |
623 | return; | |
624 | #endif | |
625 | } | |
626 | ||
627 | numa_init(dummy_numa_init); | |
628 | } | |
a4106eae | 629 | |
8db78cc4 TH |
630 | static __init int find_near_online_node(int node) |
631 | { | |
632 | int n, val; | |
633 | int min_val = INT_MAX; | |
634 | int best_node = -1; | |
635 | ||
636 | for_each_online_node(n) { | |
637 | val = node_distance(node, n); | |
638 | ||
639 | if (val < min_val) { | |
640 | min_val = val; | |
641 | best_node = n; | |
642 | } | |
643 | } | |
644 | ||
645 | return best_node; | |
646 | } | |
647 | ||
648 | /* | |
649 | * Setup early cpu_to_node. | |
650 | * | |
651 | * Populate cpu_to_node[] only if x86_cpu_to_apicid[], | |
652 | * and apicid_to_node[] tables have valid entries for a CPU. | |
653 | * This means we skip cpu_to_node[] initialisation for NUMA | |
654 | * emulation and faking node case (when running a kernel compiled | |
655 | * for NUMA on a non NUMA box), which is OK as cpu_to_node[] | |
656 | * is already initialized in a round robin manner at numa_init_array, | |
657 | * prior to this call, and this initialization is good enough | |
658 | * for the fake NUMA cases. | |
659 | * | |
660 | * Called before the per_cpu areas are setup. | |
661 | */ | |
662 | void __init init_cpu_to_node(void) | |
663 | { | |
664 | int cpu; | |
665 | u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid); | |
666 | ||
667 | BUG_ON(cpu_to_apicid == NULL); | |
668 | ||
669 | for_each_possible_cpu(cpu) { | |
670 | int node = numa_cpu_node(cpu); | |
671 | ||
672 | if (node == NUMA_NO_NODE) | |
673 | continue; | |
674 | if (!node_online(node)) | |
675 | node = find_near_online_node(node); | |
676 | numa_set_node(cpu, node); | |
677 | } | |
678 | } | |
679 | ||
de2d9445 TH |
680 | #ifndef CONFIG_DEBUG_PER_CPU_MAPS |
681 | ||
682 | # ifndef CONFIG_NUMA_EMU | |
683 | void __cpuinit numa_add_cpu(int cpu) | |
684 | { | |
685 | cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); | |
686 | } | |
687 | ||
688 | void __cpuinit numa_remove_cpu(int cpu) | |
689 | { | |
690 | cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); | |
691 | } | |
692 | # endif /* !CONFIG_NUMA_EMU */ | |
693 | ||
694 | #else /* !CONFIG_DEBUG_PER_CPU_MAPS */ | |
645a7919 TH |
695 | |
696 | int __cpu_to_node(int cpu) | |
697 | { | |
698 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) { | |
699 | printk(KERN_WARNING | |
700 | "cpu_to_node(%d): usage too early!\n", cpu); | |
701 | dump_stack(); | |
702 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
703 | } | |
704 | return per_cpu(x86_cpu_to_node_map, cpu); | |
705 | } | |
706 | EXPORT_SYMBOL(__cpu_to_node); | |
707 | ||
708 | /* | |
709 | * Same function as cpu_to_node() but used if called before the | |
710 | * per_cpu areas are setup. | |
711 | */ | |
712 | int early_cpu_to_node(int cpu) | |
713 | { | |
714 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) | |
715 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
716 | ||
717 | if (!cpu_possible(cpu)) { | |
718 | printk(KERN_WARNING | |
719 | "early_cpu_to_node(%d): no per_cpu area!\n", cpu); | |
720 | dump_stack(); | |
721 | return NUMA_NO_NODE; | |
722 | } | |
723 | return per_cpu(x86_cpu_to_node_map, cpu); | |
724 | } | |
725 | ||
7a6c6547 | 726 | void debug_cpumask_set_cpu(int cpu, int node, bool enable) |
de2d9445 | 727 | { |
de2d9445 TH |
728 | struct cpumask *mask; |
729 | char buf[64]; | |
730 | ||
14392fd3 DR |
731 | if (node == NUMA_NO_NODE) { |
732 | /* early_cpu_to_node() already emits a warning and trace */ | |
7a6c6547 | 733 | return; |
14392fd3 | 734 | } |
de2d9445 TH |
735 | mask = node_to_cpumask_map[node]; |
736 | if (!mask) { | |
737 | pr_err("node_to_cpumask_map[%i] NULL\n", node); | |
738 | dump_stack(); | |
7a6c6547 | 739 | return; |
de2d9445 TH |
740 | } |
741 | ||
7a6c6547 DR |
742 | if (enable) |
743 | cpumask_set_cpu(cpu, mask); | |
744 | else | |
745 | cpumask_clear_cpu(cpu, mask); | |
746 | ||
de2d9445 TH |
747 | cpulist_scnprintf(buf, sizeof(buf), mask); |
748 | printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n", | |
749 | enable ? "numa_add_cpu" : "numa_remove_cpu", | |
750 | cpu, node, buf); | |
7a6c6547 | 751 | return; |
de2d9445 TH |
752 | } |
753 | ||
754 | # ifndef CONFIG_NUMA_EMU | |
7a6c6547 | 755 | static void __cpuinit numa_set_cpumask(int cpu, bool enable) |
de2d9445 | 756 | { |
7a6c6547 | 757 | debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable); |
de2d9445 TH |
758 | } |
759 | ||
760 | void __cpuinit numa_add_cpu(int cpu) | |
761 | { | |
7a6c6547 | 762 | numa_set_cpumask(cpu, true); |
de2d9445 TH |
763 | } |
764 | ||
765 | void __cpuinit numa_remove_cpu(int cpu) | |
766 | { | |
7a6c6547 | 767 | numa_set_cpumask(cpu, false); |
de2d9445 TH |
768 | } |
769 | # endif /* !CONFIG_NUMA_EMU */ | |
770 | ||
71ee73e7 RR |
771 | /* |
772 | * Returns a pointer to the bitmask of CPUs on Node 'node'. | |
773 | */ | |
73e907de | 774 | const struct cpumask *cpumask_of_node(int node) |
71ee73e7 | 775 | { |
71ee73e7 RR |
776 | if (node >= nr_node_ids) { |
777 | printk(KERN_WARNING | |
778 | "cpumask_of_node(%d): node > nr_node_ids(%d)\n", | |
779 | node, nr_node_ids); | |
780 | dump_stack(); | |
781 | return cpu_none_mask; | |
782 | } | |
c032ef60 RR |
783 | if (node_to_cpumask_map[node] == NULL) { |
784 | printk(KERN_WARNING | |
785 | "cpumask_of_node(%d): no node_to_cpumask_map!\n", | |
786 | node); | |
787 | dump_stack(); | |
788 | return cpu_online_mask; | |
789 | } | |
0b966252 | 790 | return node_to_cpumask_map[node]; |
71ee73e7 RR |
791 | } |
792 | EXPORT_SYMBOL(cpumask_of_node); | |
645a7919 | 793 | |
de2d9445 | 794 | #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */ |
a4106eae TH |
795 | |
796 | #if defined(CONFIG_X86_64) && defined(CONFIG_MEMORY_HOTPLUG) | |
797 | int memory_add_physaddr_to_nid(u64 start) | |
798 | { | |
799 | struct numa_meminfo *mi = &numa_meminfo; | |
800 | int nid = mi->blk[0].nid; | |
801 | int i; | |
802 | ||
803 | for (i = 0; i < mi->nr_blks; i++) | |
804 | if (mi->blk[i].start <= start && mi->blk[i].end > start) | |
805 | nid = mi->blk[i].nid; | |
806 | return nid; | |
807 | } | |
808 | EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); | |
809 | #endif |