]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/powerpc/mm/numa.c
powerpc/pseries: Add a helper for form1 cpu distance
[mirror_ubuntu-jammy-kernel.git] / arch / powerpc / mm / numa.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * pSeries NUMA support
4 *
5 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
6 */
7 #define pr_fmt(fmt) "numa: " fmt
8
9 #include <linux/threads.h>
10 #include <linux/memblock.h>
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <linux/mmzone.h>
14 #include <linux/export.h>
15 #include <linux/nodemask.h>
16 #include <linux/cpu.h>
17 #include <linux/notifier.h>
18 #include <linux/of.h>
19 #include <linux/pfn.h>
20 #include <linux/cpuset.h>
21 #include <linux/node.h>
22 #include <linux/stop_machine.h>
23 #include <linux/proc_fs.h>
24 #include <linux/seq_file.h>
25 #include <linux/uaccess.h>
26 #include <linux/slab.h>
27 #include <asm/cputhreads.h>
28 #include <asm/sparsemem.h>
29 #include <asm/prom.h>
30 #include <asm/smp.h>
31 #include <asm/topology.h>
32 #include <asm/firmware.h>
33 #include <asm/paca.h>
34 #include <asm/hvcall.h>
35 #include <asm/setup.h>
36 #include <asm/vdso.h>
37 #include <asm/drmem.h>
38
39 static int numa_enabled = 1;
40
41 static char *cmdline __initdata;
42
43 static int numa_debug;
44 #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
45
46 int numa_cpu_lookup_table[NR_CPUS];
47 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
48 struct pglist_data *node_data[MAX_NUMNODES];
49
50 EXPORT_SYMBOL(numa_cpu_lookup_table);
51 EXPORT_SYMBOL(node_to_cpumask_map);
52 EXPORT_SYMBOL(node_data);
53
54 static int primary_domain_index;
55 static int n_mem_addr_cells, n_mem_size_cells;
56
57 #define FORM0_AFFINITY 0
58 #define FORM1_AFFINITY 1
59 static int affinity_form;
60
61 #define MAX_DISTANCE_REF_POINTS 4
62 static int distance_ref_points_depth;
63 static const __be32 *distance_ref_points;
64 static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
65
66 /*
67 * Allocate node_to_cpumask_map based on number of available nodes
68 * Requires node_possible_map to be valid.
69 *
70 * Note: cpumask_of_node() is not valid until after this is done.
71 */
72 static void __init setup_node_to_cpumask_map(void)
73 {
74 unsigned int node;
75
76 /* setup nr_node_ids if not done yet */
77 if (nr_node_ids == MAX_NUMNODES)
78 setup_nr_node_ids();
79
80 /* allocate the map */
81 for_each_node(node)
82 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
83
84 /* cpumask_of_node() will now work */
85 dbg("Node to cpumask map for %u nodes\n", nr_node_ids);
86 }
87
88 static int __init fake_numa_create_new_node(unsigned long end_pfn,
89 unsigned int *nid)
90 {
91 unsigned long long mem;
92 char *p = cmdline;
93 static unsigned int fake_nid;
94 static unsigned long long curr_boundary;
95
96 /*
97 * Modify node id, iff we started creating NUMA nodes
98 * We want to continue from where we left of the last time
99 */
100 if (fake_nid)
101 *nid = fake_nid;
102 /*
103 * In case there are no more arguments to parse, the
104 * node_id should be the same as the last fake node id
105 * (we've handled this above).
106 */
107 if (!p)
108 return 0;
109
110 mem = memparse(p, &p);
111 if (!mem)
112 return 0;
113
114 if (mem < curr_boundary)
115 return 0;
116
117 curr_boundary = mem;
118
119 if ((end_pfn << PAGE_SHIFT) > mem) {
120 /*
121 * Skip commas and spaces
122 */
123 while (*p == ',' || *p == ' ' || *p == '\t')
124 p++;
125
126 cmdline = p;
127 fake_nid++;
128 *nid = fake_nid;
129 dbg("created new fake_node with id %d\n", fake_nid);
130 return 1;
131 }
132 return 0;
133 }
134
135 static void reset_numa_cpu_lookup_table(void)
136 {
137 unsigned int cpu;
138
139 for_each_possible_cpu(cpu)
140 numa_cpu_lookup_table[cpu] = -1;
141 }
142
143 static void map_cpu_to_node(int cpu, int node)
144 {
145 update_numa_cpu_lookup_table(cpu, node);
146
147 dbg("adding cpu %d to node %d\n", cpu, node);
148
149 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
150 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
151 }
152
153 #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
154 static void unmap_cpu_from_node(unsigned long cpu)
155 {
156 int node = numa_cpu_lookup_table[cpu];
157
158 dbg("removing cpu %lu from node %d\n", cpu, node);
159
160 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
161 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
162 } else {
163 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
164 cpu, node);
165 }
166 }
167 #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
168
169 static int __cpu_form1_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
170 {
171 int dist = 0;
172
173 int i, index;
174
175 for (i = 0; i < distance_ref_points_depth; i++) {
176 index = be32_to_cpu(distance_ref_points[i]);
177 if (cpu1_assoc[index] == cpu2_assoc[index])
178 break;
179 dist++;
180 }
181
182 return dist;
183 }
184
185 int cpu_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
186 {
187 /* We should not get called with FORM0 */
188 VM_WARN_ON(affinity_form == FORM0_AFFINITY);
189
190 return __cpu_form1_relative_distance(cpu1_assoc, cpu2_assoc);
191 }
192
193 /* must hold reference to node during call */
194 static const __be32 *of_get_associativity(struct device_node *dev)
195 {
196 return of_get_property(dev, "ibm,associativity", NULL);
197 }
198
199 int __node_distance(int a, int b)
200 {
201 int i;
202 int distance = LOCAL_DISTANCE;
203
204 if (affinity_form == FORM0_AFFINITY)
205 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
206
207 for (i = 0; i < distance_ref_points_depth; i++) {
208 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
209 break;
210
211 /* Double the distance for each NUMA level */
212 distance *= 2;
213 }
214
215 return distance;
216 }
217 EXPORT_SYMBOL(__node_distance);
218
219 static int __associativity_to_nid(const __be32 *associativity,
220 int max_array_sz)
221 {
222 int nid;
223 /*
224 * primary_domain_index is 1 based array index.
225 */
226 int index = primary_domain_index - 1;
227
228 if (!numa_enabled || index >= max_array_sz)
229 return NUMA_NO_NODE;
230
231 nid = of_read_number(&associativity[index], 1);
232
233 /* POWER4 LPAR uses 0xffff as invalid node */
234 if (nid == 0xffff || nid >= nr_node_ids)
235 nid = NUMA_NO_NODE;
236 return nid;
237 }
238 /*
239 * Returns nid in the range [0..nr_node_ids], or -1 if no useful NUMA
240 * info is found.
241 */
242 static int associativity_to_nid(const __be32 *associativity)
243 {
244 int array_sz = of_read_number(associativity, 1);
245
246 /* Skip the first element in the associativity array */
247 return __associativity_to_nid((associativity + 1), array_sz);
248 }
249
250 /* Returns the nid associated with the given device tree node,
251 * or -1 if not found.
252 */
253 static int of_node_to_nid_single(struct device_node *device)
254 {
255 int nid = NUMA_NO_NODE;
256 const __be32 *tmp;
257
258 tmp = of_get_associativity(device);
259 if (tmp)
260 nid = associativity_to_nid(tmp);
261 return nid;
262 }
263
264 /* Walk the device tree upwards, looking for an associativity id */
265 int of_node_to_nid(struct device_node *device)
266 {
267 int nid = NUMA_NO_NODE;
268
269 of_node_get(device);
270 while (device) {
271 nid = of_node_to_nid_single(device);
272 if (nid != -1)
273 break;
274
275 device = of_get_next_parent(device);
276 }
277 of_node_put(device);
278
279 return nid;
280 }
281 EXPORT_SYMBOL(of_node_to_nid);
282
283 static void __initialize_form1_numa_distance(const __be32 *associativity,
284 int max_array_sz)
285 {
286 int i, nid;
287
288 if (affinity_form != FORM1_AFFINITY)
289 return;
290
291 nid = __associativity_to_nid(associativity, max_array_sz);
292 if (nid != NUMA_NO_NODE) {
293 for (i = 0; i < distance_ref_points_depth; i++) {
294 const __be32 *entry;
295 int index = be32_to_cpu(distance_ref_points[i]) - 1;
296
297 /*
298 * broken hierarchy, return with broken distance table
299 */
300 if (WARN(index >= max_array_sz, "Broken ibm,associativity property"))
301 return;
302
303 entry = &associativity[index];
304 distance_lookup_table[nid][i] = of_read_number(entry, 1);
305 }
306 }
307 }
308
309 static void initialize_form1_numa_distance(const __be32 *associativity)
310 {
311 int array_sz;
312
313 array_sz = of_read_number(associativity, 1);
314 /* Skip the first element in the associativity array */
315 __initialize_form1_numa_distance(associativity + 1, array_sz);
316 }
317
318 /*
319 * Used to update distance information w.r.t newly added node.
320 */
321 void update_numa_distance(struct device_node *node)
322 {
323 if (affinity_form == FORM0_AFFINITY)
324 return;
325 else if (affinity_form == FORM1_AFFINITY) {
326 const __be32 *associativity;
327
328 associativity = of_get_associativity(node);
329 if (!associativity)
330 return;
331
332 initialize_form1_numa_distance(associativity);
333 return;
334 }
335 }
336
337 static int __init find_primary_domain_index(void)
338 {
339 int index;
340 struct device_node *root;
341
342 /*
343 * Check for which form of affinity.
344 */
345 if (firmware_has_feature(FW_FEATURE_OPAL)) {
346 affinity_form = FORM1_AFFINITY;
347 } else if (firmware_has_feature(FW_FEATURE_FORM1_AFFINITY)) {
348 dbg("Using form 1 affinity\n");
349 affinity_form = FORM1_AFFINITY;
350 } else
351 affinity_form = FORM0_AFFINITY;
352
353 if (firmware_has_feature(FW_FEATURE_OPAL))
354 root = of_find_node_by_path("/ibm,opal");
355 else
356 root = of_find_node_by_path("/rtas");
357 if (!root)
358 root = of_find_node_by_path("/");
359
360 /*
361 * This property is a set of 32-bit integers, each representing
362 * an index into the ibm,associativity nodes.
363 *
364 * With form 0 affinity the first integer is for an SMP configuration
365 * (should be all 0's) and the second is for a normal NUMA
366 * configuration. We have only one level of NUMA.
367 *
368 * With form 1 affinity the first integer is the most significant
369 * NUMA boundary and the following are progressively less significant
370 * boundaries. There can be more than one level of NUMA.
371 */
372 distance_ref_points = of_get_property(root,
373 "ibm,associativity-reference-points",
374 &distance_ref_points_depth);
375
376 if (!distance_ref_points) {
377 dbg("NUMA: ibm,associativity-reference-points not found.\n");
378 goto err;
379 }
380
381 distance_ref_points_depth /= sizeof(int);
382 if (affinity_form == FORM0_AFFINITY) {
383 if (distance_ref_points_depth < 2) {
384 printk(KERN_WARNING "NUMA: "
385 "short ibm,associativity-reference-points\n");
386 goto err;
387 }
388
389 index = of_read_number(&distance_ref_points[1], 1);
390 } else {
391 index = of_read_number(distance_ref_points, 1);
392 }
393
394 /*
395 * Warn and cap if the hardware supports more than
396 * MAX_DISTANCE_REF_POINTS domains.
397 */
398 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
399 printk(KERN_WARNING "NUMA: distance array capped at "
400 "%d entries\n", MAX_DISTANCE_REF_POINTS);
401 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
402 }
403
404 of_node_put(root);
405 return index;
406
407 err:
408 of_node_put(root);
409 return -1;
410 }
411
412 static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
413 {
414 struct device_node *memory = NULL;
415
416 memory = of_find_node_by_type(memory, "memory");
417 if (!memory)
418 panic("numa.c: No memory nodes found!");
419
420 *n_addr_cells = of_n_addr_cells(memory);
421 *n_size_cells = of_n_size_cells(memory);
422 of_node_put(memory);
423 }
424
425 static unsigned long read_n_cells(int n, const __be32 **buf)
426 {
427 unsigned long result = 0;
428
429 while (n--) {
430 result = (result << 32) | of_read_number(*buf, 1);
431 (*buf)++;
432 }
433 return result;
434 }
435
436 struct assoc_arrays {
437 u32 n_arrays;
438 u32 array_sz;
439 const __be32 *arrays;
440 };
441
442 /*
443 * Retrieve and validate the list of associativity arrays for drconf
444 * memory from the ibm,associativity-lookup-arrays property of the
445 * device tree..
446 *
447 * The layout of the ibm,associativity-lookup-arrays property is a number N
448 * indicating the number of associativity arrays, followed by a number M
449 * indicating the size of each associativity array, followed by a list
450 * of N associativity arrays.
451 */
452 static int of_get_assoc_arrays(struct assoc_arrays *aa)
453 {
454 struct device_node *memory;
455 const __be32 *prop;
456 u32 len;
457
458 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
459 if (!memory)
460 return -1;
461
462 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
463 if (!prop || len < 2 * sizeof(unsigned int)) {
464 of_node_put(memory);
465 return -1;
466 }
467
468 aa->n_arrays = of_read_number(prop++, 1);
469 aa->array_sz = of_read_number(prop++, 1);
470
471 of_node_put(memory);
472
473 /* Now that we know the number of arrays and size of each array,
474 * revalidate the size of the property read in.
475 */
476 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
477 return -1;
478
479 aa->arrays = prop;
480 return 0;
481 }
482
483 static int get_nid_and_numa_distance(struct drmem_lmb *lmb)
484 {
485 struct assoc_arrays aa = { .arrays = NULL };
486 int default_nid = NUMA_NO_NODE;
487 int nid = default_nid;
488 int rc, index;
489
490 if ((primary_domain_index < 0) || !numa_enabled)
491 return default_nid;
492
493 rc = of_get_assoc_arrays(&aa);
494 if (rc)
495 return default_nid;
496
497 if (primary_domain_index <= aa.array_sz &&
498 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
499 const __be32 *associativity;
500
501 index = lmb->aa_index * aa.array_sz;
502 associativity = &aa.arrays[index];
503 nid = __associativity_to_nid(associativity, aa.array_sz);
504 if (nid > 0 && affinity_form == FORM1_AFFINITY) {
505 /*
506 * lookup array associativity entries have
507 * no length of the array as the first element.
508 */
509 __initialize_form1_numa_distance(associativity, aa.array_sz);
510 }
511 }
512 return nid;
513 }
514
515 /*
516 * This is like of_node_to_nid_single() for memory represented in the
517 * ibm,dynamic-reconfiguration-memory node.
518 */
519 int of_drconf_to_nid_single(struct drmem_lmb *lmb)
520 {
521 struct assoc_arrays aa = { .arrays = NULL };
522 int default_nid = NUMA_NO_NODE;
523 int nid = default_nid;
524 int rc, index;
525
526 if ((primary_domain_index < 0) || !numa_enabled)
527 return default_nid;
528
529 rc = of_get_assoc_arrays(&aa);
530 if (rc)
531 return default_nid;
532
533 if (primary_domain_index <= aa.array_sz &&
534 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
535 const __be32 *associativity;
536
537 index = lmb->aa_index * aa.array_sz;
538 associativity = &aa.arrays[index];
539 nid = __associativity_to_nid(associativity, aa.array_sz);
540 }
541 return nid;
542 }
543
544 #ifdef CONFIG_PPC_SPLPAR
545
546 static int __vphn_get_associativity(long lcpu, __be32 *associativity)
547 {
548 long rc, hwid;
549
550 /*
551 * On a shared lpar, device tree will not have node associativity.
552 * At this time lppaca, or its __old_status field may not be
553 * updated. Hence kernel cannot detect if its on a shared lpar. So
554 * request an explicit associativity irrespective of whether the
555 * lpar is shared or dedicated. Use the device tree property as a
556 * fallback. cpu_to_phys_id is only valid between
557 * smp_setup_cpu_maps() and smp_setup_pacas().
558 */
559 if (firmware_has_feature(FW_FEATURE_VPHN)) {
560 if (cpu_to_phys_id)
561 hwid = cpu_to_phys_id[lcpu];
562 else
563 hwid = get_hard_smp_processor_id(lcpu);
564
565 rc = hcall_vphn(hwid, VPHN_FLAG_VCPU, associativity);
566 if (rc == H_SUCCESS)
567 return 0;
568 }
569
570 return -1;
571 }
572
573 static int vphn_get_nid(long lcpu)
574 {
575 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
576
577
578 if (!__vphn_get_associativity(lcpu, associativity))
579 return associativity_to_nid(associativity);
580
581 return NUMA_NO_NODE;
582
583 }
584 #else
585
586 static int __vphn_get_associativity(long lcpu, __be32 *associativity)
587 {
588 return -1;
589 }
590
591 static int vphn_get_nid(long unused)
592 {
593 return NUMA_NO_NODE;
594 }
595 #endif /* CONFIG_PPC_SPLPAR */
596
597 /*
598 * Figure out to which domain a cpu belongs and stick it there.
599 * Return the id of the domain used.
600 */
601 static int numa_setup_cpu(unsigned long lcpu)
602 {
603 struct device_node *cpu;
604 int fcpu = cpu_first_thread_sibling(lcpu);
605 int nid = NUMA_NO_NODE;
606
607 if (!cpu_present(lcpu)) {
608 set_cpu_numa_node(lcpu, first_online_node);
609 return first_online_node;
610 }
611
612 /*
613 * If a valid cpu-to-node mapping is already available, use it
614 * directly instead of querying the firmware, since it represents
615 * the most recent mapping notified to us by the platform (eg: VPHN).
616 * Since cpu_to_node binding remains the same for all threads in the
617 * core. If a valid cpu-to-node mapping is already available, for
618 * the first thread in the core, use it.
619 */
620 nid = numa_cpu_lookup_table[fcpu];
621 if (nid >= 0) {
622 map_cpu_to_node(lcpu, nid);
623 return nid;
624 }
625
626 nid = vphn_get_nid(lcpu);
627 if (nid != NUMA_NO_NODE)
628 goto out_present;
629
630 cpu = of_get_cpu_node(lcpu, NULL);
631
632 if (!cpu) {
633 WARN_ON(1);
634 if (cpu_present(lcpu))
635 goto out_present;
636 else
637 goto out;
638 }
639
640 nid = of_node_to_nid_single(cpu);
641 of_node_put(cpu);
642
643 out_present:
644 if (nid < 0 || !node_possible(nid))
645 nid = first_online_node;
646
647 /*
648 * Update for the first thread of the core. All threads of a core
649 * have to be part of the same node. This not only avoids querying
650 * for every other thread in the core, but always avoids a case
651 * where virtual node associativity change causes subsequent threads
652 * of a core to be associated with different nid. However if first
653 * thread is already online, expect it to have a valid mapping.
654 */
655 if (fcpu != lcpu) {
656 WARN_ON(cpu_online(fcpu));
657 map_cpu_to_node(fcpu, nid);
658 }
659
660 map_cpu_to_node(lcpu, nid);
661 out:
662 return nid;
663 }
664
665 static void verify_cpu_node_mapping(int cpu, int node)
666 {
667 int base, sibling, i;
668
669 /* Verify that all the threads in the core belong to the same node */
670 base = cpu_first_thread_sibling(cpu);
671
672 for (i = 0; i < threads_per_core; i++) {
673 sibling = base + i;
674
675 if (sibling == cpu || cpu_is_offline(sibling))
676 continue;
677
678 if (cpu_to_node(sibling) != node) {
679 WARN(1, "CPU thread siblings %d and %d don't belong"
680 " to the same node!\n", cpu, sibling);
681 break;
682 }
683 }
684 }
685
686 /* Must run before sched domains notifier. */
687 static int ppc_numa_cpu_prepare(unsigned int cpu)
688 {
689 int nid;
690
691 nid = numa_setup_cpu(cpu);
692 verify_cpu_node_mapping(cpu, nid);
693 return 0;
694 }
695
696 static int ppc_numa_cpu_dead(unsigned int cpu)
697 {
698 #ifdef CONFIG_HOTPLUG_CPU
699 unmap_cpu_from_node(cpu);
700 #endif
701 return 0;
702 }
703
704 /*
705 * Check and possibly modify a memory region to enforce the memory limit.
706 *
707 * Returns the size the region should have to enforce the memory limit.
708 * This will either be the original value of size, a truncated value,
709 * or zero. If the returned value of size is 0 the region should be
710 * discarded as it lies wholly above the memory limit.
711 */
712 static unsigned long __init numa_enforce_memory_limit(unsigned long start,
713 unsigned long size)
714 {
715 /*
716 * We use memblock_end_of_DRAM() in here instead of memory_limit because
717 * we've already adjusted it for the limit and it takes care of
718 * having memory holes below the limit. Also, in the case of
719 * iommu_is_off, memory_limit is not set but is implicitly enforced.
720 */
721
722 if (start + size <= memblock_end_of_DRAM())
723 return size;
724
725 if (start >= memblock_end_of_DRAM())
726 return 0;
727
728 return memblock_end_of_DRAM() - start;
729 }
730
731 /*
732 * Reads the counter for a given entry in
733 * linux,drconf-usable-memory property
734 */
735 static inline int __init read_usm_ranges(const __be32 **usm)
736 {
737 /*
738 * For each lmb in ibm,dynamic-memory a corresponding
739 * entry in linux,drconf-usable-memory property contains
740 * a counter followed by that many (base, size) duple.
741 * read the counter from linux,drconf-usable-memory
742 */
743 return read_n_cells(n_mem_size_cells, usm);
744 }
745
746 /*
747 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
748 * node. This assumes n_mem_{addr,size}_cells have been set.
749 */
750 static int __init numa_setup_drmem_lmb(struct drmem_lmb *lmb,
751 const __be32 **usm,
752 void *data)
753 {
754 unsigned int ranges, is_kexec_kdump = 0;
755 unsigned long base, size, sz;
756 int nid;
757
758 /*
759 * Skip this block if the reserved bit is set in flags (0x80)
760 * or if the block is not assigned to this partition (0x8)
761 */
762 if ((lmb->flags & DRCONF_MEM_RESERVED)
763 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
764 return 0;
765
766 if (*usm)
767 is_kexec_kdump = 1;
768
769 base = lmb->base_addr;
770 size = drmem_lmb_size();
771 ranges = 1;
772
773 if (is_kexec_kdump) {
774 ranges = read_usm_ranges(usm);
775 if (!ranges) /* there are no (base, size) duple */
776 return 0;
777 }
778
779 do {
780 if (is_kexec_kdump) {
781 base = read_n_cells(n_mem_addr_cells, usm);
782 size = read_n_cells(n_mem_size_cells, usm);
783 }
784
785 nid = get_nid_and_numa_distance(lmb);
786 fake_numa_create_new_node(((base + size) >> PAGE_SHIFT),
787 &nid);
788 node_set_online(nid);
789 sz = numa_enforce_memory_limit(base, size);
790 if (sz)
791 memblock_set_node(base, sz, &memblock.memory, nid);
792 } while (--ranges);
793
794 return 0;
795 }
796
797 static int __init parse_numa_properties(void)
798 {
799 struct device_node *memory;
800 int default_nid = 0;
801 unsigned long i;
802 const __be32 *associativity;
803
804 if (numa_enabled == 0) {
805 printk(KERN_WARNING "NUMA disabled by user\n");
806 return -1;
807 }
808
809 primary_domain_index = find_primary_domain_index();
810
811 if (primary_domain_index < 0) {
812 /*
813 * if we fail to parse primary_domain_index from device tree
814 * mark the numa disabled, boot with numa disabled.
815 */
816 numa_enabled = false;
817 return primary_domain_index;
818 }
819
820 dbg("NUMA associativity depth for CPU/Memory: %d\n", primary_domain_index);
821
822 /*
823 * Even though we connect cpus to numa domains later in SMP
824 * init, we need to know the node ids now. This is because
825 * each node to be onlined must have NODE_DATA etc backing it.
826 */
827 for_each_present_cpu(i) {
828 __be32 vphn_assoc[VPHN_ASSOC_BUFSIZE];
829 struct device_node *cpu;
830 int nid = NUMA_NO_NODE;
831
832 memset(vphn_assoc, 0, VPHN_ASSOC_BUFSIZE * sizeof(__be32));
833
834 if (__vphn_get_associativity(i, vphn_assoc) == 0) {
835 nid = associativity_to_nid(vphn_assoc);
836 initialize_form1_numa_distance(vphn_assoc);
837 } else {
838
839 /*
840 * Don't fall back to default_nid yet -- we will plug
841 * cpus into nodes once the memory scan has discovered
842 * the topology.
843 */
844 cpu = of_get_cpu_node(i, NULL);
845 BUG_ON(!cpu);
846
847 associativity = of_get_associativity(cpu);
848 if (associativity) {
849 nid = associativity_to_nid(associativity);
850 initialize_form1_numa_distance(associativity);
851 }
852 of_node_put(cpu);
853 }
854
855 node_set_online(nid);
856 }
857
858 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
859
860 for_each_node_by_type(memory, "memory") {
861 unsigned long start;
862 unsigned long size;
863 int nid;
864 int ranges;
865 const __be32 *memcell_buf;
866 unsigned int len;
867
868 memcell_buf = of_get_property(memory,
869 "linux,usable-memory", &len);
870 if (!memcell_buf || len <= 0)
871 memcell_buf = of_get_property(memory, "reg", &len);
872 if (!memcell_buf || len <= 0)
873 continue;
874
875 /* ranges in cell */
876 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
877 new_range:
878 /* these are order-sensitive, and modify the buffer pointer */
879 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
880 size = read_n_cells(n_mem_size_cells, &memcell_buf);
881
882 /*
883 * Assumption: either all memory nodes or none will
884 * have associativity properties. If none, then
885 * everything goes to default_nid.
886 */
887 associativity = of_get_associativity(memory);
888 if (associativity) {
889 nid = associativity_to_nid(associativity);
890 initialize_form1_numa_distance(associativity);
891 } else
892 nid = default_nid;
893
894 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
895 node_set_online(nid);
896
897 size = numa_enforce_memory_limit(start, size);
898 if (size)
899 memblock_set_node(start, size, &memblock.memory, nid);
900
901 if (--ranges)
902 goto new_range;
903 }
904
905 /*
906 * Now do the same thing for each MEMBLOCK listed in the
907 * ibm,dynamic-memory property in the
908 * ibm,dynamic-reconfiguration-memory node.
909 */
910 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
911 if (memory) {
912 walk_drmem_lmbs(memory, NULL, numa_setup_drmem_lmb);
913 of_node_put(memory);
914 }
915
916 return 0;
917 }
918
919 static void __init setup_nonnuma(void)
920 {
921 unsigned long top_of_ram = memblock_end_of_DRAM();
922 unsigned long total_ram = memblock_phys_mem_size();
923 unsigned long start_pfn, end_pfn;
924 unsigned int nid = 0;
925 int i;
926
927 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
928 top_of_ram, total_ram);
929 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
930 (top_of_ram - total_ram) >> 20);
931
932 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
933 fake_numa_create_new_node(end_pfn, &nid);
934 memblock_set_node(PFN_PHYS(start_pfn),
935 PFN_PHYS(end_pfn - start_pfn),
936 &memblock.memory, nid);
937 node_set_online(nid);
938 }
939 }
940
941 void __init dump_numa_cpu_topology(void)
942 {
943 unsigned int node;
944 unsigned int cpu, count;
945
946 if (!numa_enabled)
947 return;
948
949 for_each_online_node(node) {
950 pr_info("Node %d CPUs:", node);
951
952 count = 0;
953 /*
954 * If we used a CPU iterator here we would miss printing
955 * the holes in the cpumap.
956 */
957 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
958 if (cpumask_test_cpu(cpu,
959 node_to_cpumask_map[node])) {
960 if (count == 0)
961 pr_cont(" %u", cpu);
962 ++count;
963 } else {
964 if (count > 1)
965 pr_cont("-%u", cpu - 1);
966 count = 0;
967 }
968 }
969
970 if (count > 1)
971 pr_cont("-%u", nr_cpu_ids - 1);
972 pr_cont("\n");
973 }
974 }
975
976 /* Initialize NODE_DATA for a node on the local memory */
977 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
978 {
979 u64 spanned_pages = end_pfn - start_pfn;
980 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
981 u64 nd_pa;
982 void *nd;
983 int tnid;
984
985 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
986 if (!nd_pa)
987 panic("Cannot allocate %zu bytes for node %d data\n",
988 nd_size, nid);
989
990 nd = __va(nd_pa);
991
992 /* report and initialize */
993 pr_info(" NODE_DATA [mem %#010Lx-%#010Lx]\n",
994 nd_pa, nd_pa + nd_size - 1);
995 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
996 if (tnid != nid)
997 pr_info(" NODE_DATA(%d) on node %d\n", nid, tnid);
998
999 node_data[nid] = nd;
1000 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
1001 NODE_DATA(nid)->node_id = nid;
1002 NODE_DATA(nid)->node_start_pfn = start_pfn;
1003 NODE_DATA(nid)->node_spanned_pages = spanned_pages;
1004 }
1005
1006 static void __init find_possible_nodes(void)
1007 {
1008 struct device_node *rtas;
1009 const __be32 *domains = NULL;
1010 int prop_length, max_nodes;
1011 u32 i;
1012
1013 if (!numa_enabled)
1014 return;
1015
1016 rtas = of_find_node_by_path("/rtas");
1017 if (!rtas)
1018 return;
1019
1020 /*
1021 * ibm,current-associativity-domains is a fairly recent property. If
1022 * it doesn't exist, then fallback on ibm,max-associativity-domains.
1023 * Current denotes what the platform can support compared to max
1024 * which denotes what the Hypervisor can support.
1025 *
1026 * If the LPAR is migratable, new nodes might be activated after a LPM,
1027 * so we should consider the max number in that case.
1028 */
1029 if (!of_get_property(of_root, "ibm,migratable-partition", NULL))
1030 domains = of_get_property(rtas,
1031 "ibm,current-associativity-domains",
1032 &prop_length);
1033 if (!domains) {
1034 domains = of_get_property(rtas, "ibm,max-associativity-domains",
1035 &prop_length);
1036 if (!domains)
1037 goto out;
1038 }
1039
1040 max_nodes = of_read_number(&domains[primary_domain_index], 1);
1041 pr_info("Partition configured for %d NUMA nodes.\n", max_nodes);
1042
1043 for (i = 0; i < max_nodes; i++) {
1044 if (!node_possible(i))
1045 node_set(i, node_possible_map);
1046 }
1047
1048 prop_length /= sizeof(int);
1049 if (prop_length > primary_domain_index + 2)
1050 coregroup_enabled = 1;
1051
1052 out:
1053 of_node_put(rtas);
1054 }
1055
1056 void __init mem_topology_setup(void)
1057 {
1058 int cpu;
1059
1060 /*
1061 * Linux/mm assumes node 0 to be online at boot. However this is not
1062 * true on PowerPC, where node 0 is similar to any other node, it
1063 * could be cpuless, memoryless node. So force node 0 to be offline
1064 * for now. This will prevent cpuless, memoryless node 0 showing up
1065 * unnecessarily as online. If a node has cpus or memory that need
1066 * to be online, then node will anyway be marked online.
1067 */
1068 node_set_offline(0);
1069
1070 if (parse_numa_properties())
1071 setup_nonnuma();
1072
1073 /*
1074 * Modify the set of possible NUMA nodes to reflect information
1075 * available about the set of online nodes, and the set of nodes
1076 * that we expect to make use of for this platform's affinity
1077 * calculations.
1078 */
1079 nodes_and(node_possible_map, node_possible_map, node_online_map);
1080
1081 find_possible_nodes();
1082
1083 setup_node_to_cpumask_map();
1084
1085 reset_numa_cpu_lookup_table();
1086
1087 for_each_possible_cpu(cpu) {
1088 /*
1089 * Powerpc with CONFIG_NUMA always used to have a node 0,
1090 * even if it was memoryless or cpuless. For all cpus that
1091 * are possible but not present, cpu_to_node() would point
1092 * to node 0. To remove a cpuless, memoryless dummy node,
1093 * powerpc need to make sure all possible but not present
1094 * cpu_to_node are set to a proper node.
1095 */
1096 numa_setup_cpu(cpu);
1097 }
1098 }
1099
1100 void __init initmem_init(void)
1101 {
1102 int nid;
1103
1104 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1105 max_pfn = max_low_pfn;
1106
1107 memblock_dump_all();
1108
1109 for_each_online_node(nid) {
1110 unsigned long start_pfn, end_pfn;
1111
1112 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1113 setup_node_data(nid, start_pfn, end_pfn);
1114 }
1115
1116 sparse_init();
1117
1118 /*
1119 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
1120 * even before we online them, so that we can use cpu_to_{node,mem}
1121 * early in boot, cf. smp_prepare_cpus().
1122 * _nocalls() + manual invocation is used because cpuhp is not yet
1123 * initialized for the boot CPU.
1124 */
1125 cpuhp_setup_state_nocalls(CPUHP_POWER_NUMA_PREPARE, "powerpc/numa:prepare",
1126 ppc_numa_cpu_prepare, ppc_numa_cpu_dead);
1127 }
1128
1129 static int __init early_numa(char *p)
1130 {
1131 if (!p)
1132 return 0;
1133
1134 if (strstr(p, "off"))
1135 numa_enabled = 0;
1136
1137 if (strstr(p, "debug"))
1138 numa_debug = 1;
1139
1140 p = strstr(p, "fake=");
1141 if (p)
1142 cmdline = p + strlen("fake=");
1143
1144 return 0;
1145 }
1146 early_param("numa", early_numa);
1147
1148 #ifdef CONFIG_MEMORY_HOTPLUG
1149 /*
1150 * Find the node associated with a hot added memory section for
1151 * memory represented in the device tree by the property
1152 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
1153 */
1154 static int hot_add_drconf_scn_to_nid(unsigned long scn_addr)
1155 {
1156 struct drmem_lmb *lmb;
1157 unsigned long lmb_size;
1158 int nid = NUMA_NO_NODE;
1159
1160 lmb_size = drmem_lmb_size();
1161
1162 for_each_drmem_lmb(lmb) {
1163 /* skip this block if it is reserved or not assigned to
1164 * this partition */
1165 if ((lmb->flags & DRCONF_MEM_RESERVED)
1166 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
1167 continue;
1168
1169 if ((scn_addr < lmb->base_addr)
1170 || (scn_addr >= (lmb->base_addr + lmb_size)))
1171 continue;
1172
1173 nid = of_drconf_to_nid_single(lmb);
1174 break;
1175 }
1176
1177 return nid;
1178 }
1179
1180 /*
1181 * Find the node associated with a hot added memory section for memory
1182 * represented in the device tree as a node (i.e. memory@XXXX) for
1183 * each memblock.
1184 */
1185 static int hot_add_node_scn_to_nid(unsigned long scn_addr)
1186 {
1187 struct device_node *memory;
1188 int nid = NUMA_NO_NODE;
1189
1190 for_each_node_by_type(memory, "memory") {
1191 unsigned long start, size;
1192 int ranges;
1193 const __be32 *memcell_buf;
1194 unsigned int len;
1195
1196 memcell_buf = of_get_property(memory, "reg", &len);
1197 if (!memcell_buf || len <= 0)
1198 continue;
1199
1200 /* ranges in cell */
1201 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1202
1203 while (ranges--) {
1204 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1205 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1206
1207 if ((scn_addr < start) || (scn_addr >= (start + size)))
1208 continue;
1209
1210 nid = of_node_to_nid_single(memory);
1211 break;
1212 }
1213
1214 if (nid >= 0)
1215 break;
1216 }
1217
1218 of_node_put(memory);
1219
1220 return nid;
1221 }
1222
1223 /*
1224 * Find the node associated with a hot added memory section. Section
1225 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1226 * sections are fully contained within a single MEMBLOCK.
1227 */
1228 int hot_add_scn_to_nid(unsigned long scn_addr)
1229 {
1230 struct device_node *memory = NULL;
1231 int nid;
1232
1233 if (!numa_enabled)
1234 return first_online_node;
1235
1236 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1237 if (memory) {
1238 nid = hot_add_drconf_scn_to_nid(scn_addr);
1239 of_node_put(memory);
1240 } else {
1241 nid = hot_add_node_scn_to_nid(scn_addr);
1242 }
1243
1244 if (nid < 0 || !node_possible(nid))
1245 nid = first_online_node;
1246
1247 return nid;
1248 }
1249
1250 static u64 hot_add_drconf_memory_max(void)
1251 {
1252 struct device_node *memory = NULL;
1253 struct device_node *dn = NULL;
1254 const __be64 *lrdr = NULL;
1255
1256 dn = of_find_node_by_path("/rtas");
1257 if (dn) {
1258 lrdr = of_get_property(dn, "ibm,lrdr-capacity", NULL);
1259 of_node_put(dn);
1260 if (lrdr)
1261 return be64_to_cpup(lrdr);
1262 }
1263
1264 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1265 if (memory) {
1266 of_node_put(memory);
1267 return drmem_lmb_memory_max();
1268 }
1269 return 0;
1270 }
1271
1272 /*
1273 * memory_hotplug_max - return max address of memory that may be added
1274 *
1275 * This is currently only used on systems that support drconfig memory
1276 * hotplug.
1277 */
1278 u64 memory_hotplug_max(void)
1279 {
1280 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1281 }
1282 #endif /* CONFIG_MEMORY_HOTPLUG */
1283
1284 /* Virtual Processor Home Node (VPHN) support */
1285 #ifdef CONFIG_PPC_SPLPAR
1286 static int topology_inited;
1287
1288 /*
1289 * Retrieve the new associativity information for a virtual processor's
1290 * home node.
1291 */
1292 static long vphn_get_associativity(unsigned long cpu,
1293 __be32 *associativity)
1294 {
1295 long rc;
1296
1297 rc = hcall_vphn(get_hard_smp_processor_id(cpu),
1298 VPHN_FLAG_VCPU, associativity);
1299
1300 switch (rc) {
1301 case H_SUCCESS:
1302 dbg("VPHN hcall succeeded. Reset polling...\n");
1303 goto out;
1304
1305 case H_FUNCTION:
1306 pr_err_ratelimited("VPHN unsupported. Disabling polling...\n");
1307 break;
1308 case H_HARDWARE:
1309 pr_err_ratelimited("hcall_vphn() experienced a hardware fault "
1310 "preventing VPHN. Disabling polling...\n");
1311 break;
1312 case H_PARAMETER:
1313 pr_err_ratelimited("hcall_vphn() was passed an invalid parameter. "
1314 "Disabling polling...\n");
1315 break;
1316 default:
1317 pr_err_ratelimited("hcall_vphn() returned %ld. Disabling polling...\n"
1318 , rc);
1319 break;
1320 }
1321 out:
1322 return rc;
1323 }
1324
1325 int find_and_online_cpu_nid(int cpu)
1326 {
1327 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1328 int new_nid;
1329
1330 /* Use associativity from first thread for all siblings */
1331 if (vphn_get_associativity(cpu, associativity))
1332 return cpu_to_node(cpu);
1333
1334 new_nid = associativity_to_nid(associativity);
1335 if (new_nid < 0 || !node_possible(new_nid))
1336 new_nid = first_online_node;
1337
1338 if (NODE_DATA(new_nid) == NULL) {
1339 #ifdef CONFIG_MEMORY_HOTPLUG
1340 /*
1341 * Need to ensure that NODE_DATA is initialized for a node from
1342 * available memory (see memblock_alloc_try_nid). If unable to
1343 * init the node, then default to nearest node that has memory
1344 * installed. Skip onlining a node if the subsystems are not
1345 * yet initialized.
1346 */
1347 if (!topology_inited || try_online_node(new_nid))
1348 new_nid = first_online_node;
1349 #else
1350 /*
1351 * Default to using the nearest node that has memory installed.
1352 * Otherwise, it would be necessary to patch the kernel MM code
1353 * to deal with more memoryless-node error conditions.
1354 */
1355 new_nid = first_online_node;
1356 #endif
1357 }
1358
1359 pr_debug("%s:%d cpu %d nid %d\n", __FUNCTION__, __LINE__,
1360 cpu, new_nid);
1361 return new_nid;
1362 }
1363
1364 int cpu_to_coregroup_id(int cpu)
1365 {
1366 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1367 int index;
1368
1369 if (cpu < 0 || cpu > nr_cpu_ids)
1370 return -1;
1371
1372 if (!coregroup_enabled)
1373 goto out;
1374
1375 if (!firmware_has_feature(FW_FEATURE_VPHN))
1376 goto out;
1377
1378 if (vphn_get_associativity(cpu, associativity))
1379 goto out;
1380
1381 index = of_read_number(associativity, 1);
1382 if (index > primary_domain_index + 1)
1383 return of_read_number(&associativity[index - 1], 1);
1384
1385 out:
1386 return cpu_to_core_id(cpu);
1387 }
1388
1389 static int topology_update_init(void)
1390 {
1391 topology_inited = 1;
1392 return 0;
1393 }
1394 device_initcall(topology_update_init);
1395 #endif /* CONFIG_PPC_SPLPAR */