1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
7 #define pr_fmt(fmt) "numa: " fmt
9 #include <linux/threads.h>
10 #include <linux/memblock.h>
11 #include <linux/init.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>
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>
31 #include <asm/topology.h>
32 #include <asm/firmware.h>
34 #include <asm/hvcall.h>
35 #include <asm/setup.h>
37 #include <asm/drmem.h>
39 static int numa_enabled = 1;
41 static char *cmdline __initdata;
43 static int numa_debug;
44 #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
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];
50 EXPORT_SYMBOL(numa_cpu_lookup_table);
51 EXPORT_SYMBOL(node_to_cpumask_map);
52 EXPORT_SYMBOL(node_data);
54 static int primary_domain_index;
55 static int n_mem_addr_cells, n_mem_size_cells;
57 #define FORM0_AFFINITY 0
58 #define FORM1_AFFINITY 1
59 #define FORM2_AFFINITY 2
60 static int affinity_form;
62 #define MAX_DISTANCE_REF_POINTS 4
63 static int distance_ref_points_depth;
64 static const __be32 *distance_ref_points;
65 static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
66 static int numa_distance_table[MAX_NUMNODES][MAX_NUMNODES] = {
67 [0 ... MAX_NUMNODES - 1] = { [0 ... MAX_NUMNODES - 1] = -1 }
69 static int numa_id_index_table[MAX_NUMNODES] = { [0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE };
72 * Allocate node_to_cpumask_map based on number of available nodes
73 * Requires node_possible_map to be valid.
75 * Note: cpumask_of_node() is not valid until after this is done.
77 static void __init setup_node_to_cpumask_map(void)
81 /* setup nr_node_ids if not done yet */
82 if (nr_node_ids == MAX_NUMNODES)
85 /* allocate the map */
87 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
89 /* cpumask_of_node() will now work */
90 dbg("Node to cpumask map for %u nodes\n", nr_node_ids);
93 static int __init fake_numa_create_new_node(unsigned long end_pfn,
96 unsigned long long mem;
98 static unsigned int fake_nid;
99 static unsigned long long curr_boundary;
102 * Modify node id, iff we started creating NUMA nodes
103 * We want to continue from where we left of the last time
108 * In case there are no more arguments to parse, the
109 * node_id should be the same as the last fake node id
110 * (we've handled this above).
115 mem = memparse(p, &p);
119 if (mem < curr_boundary)
124 if ((end_pfn << PAGE_SHIFT) > mem) {
126 * Skip commas and spaces
128 while (*p == ',' || *p == ' ' || *p == '\t')
134 dbg("created new fake_node with id %d\n", fake_nid);
140 static void reset_numa_cpu_lookup_table(void)
144 for_each_possible_cpu(cpu)
145 numa_cpu_lookup_table[cpu] = -1;
148 static void map_cpu_to_node(int cpu, int node)
150 update_numa_cpu_lookup_table(cpu, node);
152 dbg("adding cpu %d to node %d\n", cpu, node);
154 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
155 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
158 #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
159 static void unmap_cpu_from_node(unsigned long cpu)
161 int node = numa_cpu_lookup_table[cpu];
163 dbg("removing cpu %lu from node %d\n", cpu, node);
165 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
166 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
168 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
172 #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
174 static int __associativity_to_nid(const __be32 *associativity,
179 * primary_domain_index is 1 based array index.
181 int index = primary_domain_index - 1;
183 if (!numa_enabled || index >= max_array_sz)
186 nid = of_read_number(&associativity[index], 1);
188 /* POWER4 LPAR uses 0xffff as invalid node */
189 if (nid == 0xffff || nid >= nr_node_ids)
194 * Returns nid in the range [0..nr_node_ids], or -1 if no useful NUMA
197 static int associativity_to_nid(const __be32 *associativity)
199 int array_sz = of_read_number(associativity, 1);
201 /* Skip the first element in the associativity array */
202 return __associativity_to_nid((associativity + 1), array_sz);
205 static int __cpu_form2_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
210 node1 = associativity_to_nid(cpu1_assoc);
211 node2 = associativity_to_nid(cpu2_assoc);
213 dist = numa_distance_table[node1][node2];
214 if (dist <= LOCAL_DISTANCE)
216 else if (dist <= REMOTE_DISTANCE)
222 static int __cpu_form1_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
228 for (i = 0; i < distance_ref_points_depth; i++) {
229 index = be32_to_cpu(distance_ref_points[i]);
230 if (cpu1_assoc[index] == cpu2_assoc[index])
238 int cpu_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
240 /* We should not get called with FORM0 */
241 VM_WARN_ON(affinity_form == FORM0_AFFINITY);
242 if (affinity_form == FORM1_AFFINITY)
243 return __cpu_form1_relative_distance(cpu1_assoc, cpu2_assoc);
244 return __cpu_form2_relative_distance(cpu1_assoc, cpu2_assoc);
247 /* must hold reference to node during call */
248 static const __be32 *of_get_associativity(struct device_node *dev)
250 return of_get_property(dev, "ibm,associativity", NULL);
253 int __node_distance(int a, int b)
256 int distance = LOCAL_DISTANCE;
258 if (affinity_form == FORM2_AFFINITY)
259 return numa_distance_table[a][b];
260 else if (affinity_form == FORM0_AFFINITY)
261 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
263 for (i = 0; i < distance_ref_points_depth; i++) {
264 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
267 /* Double the distance for each NUMA level */
273 EXPORT_SYMBOL(__node_distance);
275 /* Returns the nid associated with the given device tree node,
276 * or -1 if not found.
278 static int of_node_to_nid_single(struct device_node *device)
280 int nid = NUMA_NO_NODE;
283 tmp = of_get_associativity(device);
285 nid = associativity_to_nid(tmp);
289 /* Walk the device tree upwards, looking for an associativity id */
290 int of_node_to_nid(struct device_node *device)
292 int nid = NUMA_NO_NODE;
296 nid = of_node_to_nid_single(device);
300 device = of_get_next_parent(device);
306 EXPORT_SYMBOL(of_node_to_nid);
308 static void __initialize_form1_numa_distance(const __be32 *associativity,
313 if (affinity_form != FORM1_AFFINITY)
316 nid = __associativity_to_nid(associativity, max_array_sz);
317 if (nid != NUMA_NO_NODE) {
318 for (i = 0; i < distance_ref_points_depth; i++) {
320 int index = be32_to_cpu(distance_ref_points[i]) - 1;
323 * broken hierarchy, return with broken distance table
325 if (WARN(index >= max_array_sz, "Broken ibm,associativity property"))
328 entry = &associativity[index];
329 distance_lookup_table[nid][i] = of_read_number(entry, 1);
334 static void initialize_form1_numa_distance(const __be32 *associativity)
338 array_sz = of_read_number(associativity, 1);
339 /* Skip the first element in the associativity array */
340 __initialize_form1_numa_distance(associativity + 1, array_sz);
344 * Used to update distance information w.r.t newly added node.
346 void update_numa_distance(struct device_node *node)
350 if (affinity_form == FORM0_AFFINITY)
352 else if (affinity_form == FORM1_AFFINITY) {
353 const __be32 *associativity;
355 associativity = of_get_associativity(node);
359 initialize_form1_numa_distance(associativity);
364 nid = of_node_to_nid_single(node);
365 if (nid == NUMA_NO_NODE)
369 * With FORM2 we expect NUMA distance of all possible NUMA
370 * nodes to be provided during boot.
372 WARN(numa_distance_table[nid][nid] == -1,
373 "NUMA distance details for node %d not provided\n", nid);
375 EXPORT_SYMBOL_GPL(update_numa_distance);
378 * ibm,numa-lookup-index-table= {N, domainid1, domainid2, ..... domainidN}
379 * ibm,numa-distance-table = { N, 1, 2, 4, 5, 1, 6, .... N elements}
381 static void initialize_form2_numa_distance_lookup_table(void)
384 struct device_node *root;
385 const __u8 *numa_dist_table;
386 const __be32 *numa_lookup_index;
387 int numa_dist_table_length;
388 int max_numa_index, distance_index;
390 if (firmware_has_feature(FW_FEATURE_OPAL))
391 root = of_find_node_by_path("/ibm,opal");
393 root = of_find_node_by_path("/rtas");
395 root = of_find_node_by_path("/");
397 numa_lookup_index = of_get_property(root, "ibm,numa-lookup-index-table", NULL);
398 max_numa_index = of_read_number(&numa_lookup_index[0], 1);
400 /* first element of the array is the size and is encode-int */
401 numa_dist_table = of_get_property(root, "ibm,numa-distance-table", NULL);
402 numa_dist_table_length = of_read_number((const __be32 *)&numa_dist_table[0], 1);
403 /* Skip the size which is encoded int */
404 numa_dist_table += sizeof(__be32);
406 pr_debug("numa_dist_table_len = %d, numa_dist_indexes_len = %d\n",
407 numa_dist_table_length, max_numa_index);
409 for (i = 0; i < max_numa_index; i++)
410 /* +1 skip the max_numa_index in the property */
411 numa_id_index_table[i] = of_read_number(&numa_lookup_index[i + 1], 1);
414 if (numa_dist_table_length != max_numa_index * max_numa_index) {
415 WARN(1, "Wrong NUMA distance information\n");
416 /* consider everybody else just remote. */
417 for (i = 0; i < max_numa_index; i++) {
418 for (j = 0; j < max_numa_index; j++) {
419 int nodeA = numa_id_index_table[i];
420 int nodeB = numa_id_index_table[j];
423 numa_distance_table[nodeA][nodeB] = LOCAL_DISTANCE;
425 numa_distance_table[nodeA][nodeB] = REMOTE_DISTANCE;
431 for (i = 0; i < max_numa_index; i++) {
432 for (j = 0; j < max_numa_index; j++) {
433 int nodeA = numa_id_index_table[i];
434 int nodeB = numa_id_index_table[j];
436 numa_distance_table[nodeA][nodeB] = numa_dist_table[distance_index++];
437 pr_debug("dist[%d][%d]=%d ", nodeA, nodeB, numa_distance_table[nodeA][nodeB]);
443 static int __init find_primary_domain_index(void)
446 struct device_node *root;
449 * Check for which form of affinity.
451 if (firmware_has_feature(FW_FEATURE_OPAL)) {
452 affinity_form = FORM1_AFFINITY;
453 } else if (firmware_has_feature(FW_FEATURE_FORM2_AFFINITY)) {
454 dbg("Using form 2 affinity\n");
455 affinity_form = FORM2_AFFINITY;
456 } else if (firmware_has_feature(FW_FEATURE_FORM1_AFFINITY)) {
457 dbg("Using form 1 affinity\n");
458 affinity_form = FORM1_AFFINITY;
460 affinity_form = FORM0_AFFINITY;
462 if (firmware_has_feature(FW_FEATURE_OPAL))
463 root = of_find_node_by_path("/ibm,opal");
465 root = of_find_node_by_path("/rtas");
467 root = of_find_node_by_path("/");
470 * This property is a set of 32-bit integers, each representing
471 * an index into the ibm,associativity nodes.
473 * With form 0 affinity the first integer is for an SMP configuration
474 * (should be all 0's) and the second is for a normal NUMA
475 * configuration. We have only one level of NUMA.
477 * With form 1 affinity the first integer is the most significant
478 * NUMA boundary and the following are progressively less significant
479 * boundaries. There can be more than one level of NUMA.
481 distance_ref_points = of_get_property(root,
482 "ibm,associativity-reference-points",
483 &distance_ref_points_depth);
485 if (!distance_ref_points) {
486 dbg("NUMA: ibm,associativity-reference-points not found.\n");
490 distance_ref_points_depth /= sizeof(int);
491 if (affinity_form == FORM0_AFFINITY) {
492 if (distance_ref_points_depth < 2) {
493 printk(KERN_WARNING "NUMA: "
494 "short ibm,associativity-reference-points\n");
498 index = of_read_number(&distance_ref_points[1], 1);
501 * Both FORM1 and FORM2 affinity find the primary domain details
502 * at the same offset.
504 index = of_read_number(distance_ref_points, 1);
507 * Warn and cap if the hardware supports more than
508 * MAX_DISTANCE_REF_POINTS domains.
510 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
511 printk(KERN_WARNING "NUMA: distance array capped at "
512 "%d entries\n", MAX_DISTANCE_REF_POINTS);
513 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
524 static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
526 struct device_node *memory = NULL;
528 memory = of_find_node_by_type(memory, "memory");
530 panic("numa.c: No memory nodes found!");
532 *n_addr_cells = of_n_addr_cells(memory);
533 *n_size_cells = of_n_size_cells(memory);
537 static unsigned long read_n_cells(int n, const __be32 **buf)
539 unsigned long result = 0;
542 result = (result << 32) | of_read_number(*buf, 1);
548 struct assoc_arrays {
551 const __be32 *arrays;
555 * Retrieve and validate the list of associativity arrays for drconf
556 * memory from the ibm,associativity-lookup-arrays property of the
559 * The layout of the ibm,associativity-lookup-arrays property is a number N
560 * indicating the number of associativity arrays, followed by a number M
561 * indicating the size of each associativity array, followed by a list
562 * of N associativity arrays.
564 static int of_get_assoc_arrays(struct assoc_arrays *aa)
566 struct device_node *memory;
570 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
574 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
575 if (!prop || len < 2 * sizeof(unsigned int)) {
580 aa->n_arrays = of_read_number(prop++, 1);
581 aa->array_sz = of_read_number(prop++, 1);
585 /* Now that we know the number of arrays and size of each array,
586 * revalidate the size of the property read in.
588 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
595 static int get_nid_and_numa_distance(struct drmem_lmb *lmb)
597 struct assoc_arrays aa = { .arrays = NULL };
598 int default_nid = NUMA_NO_NODE;
599 int nid = default_nid;
602 if ((primary_domain_index < 0) || !numa_enabled)
605 rc = of_get_assoc_arrays(&aa);
609 if (primary_domain_index <= aa.array_sz &&
610 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
611 const __be32 *associativity;
613 index = lmb->aa_index * aa.array_sz;
614 associativity = &aa.arrays[index];
615 nid = __associativity_to_nid(associativity, aa.array_sz);
616 if (nid > 0 && affinity_form == FORM1_AFFINITY) {
618 * lookup array associativity entries have
619 * no length of the array as the first element.
621 __initialize_form1_numa_distance(associativity, aa.array_sz);
628 * This is like of_node_to_nid_single() for memory represented in the
629 * ibm,dynamic-reconfiguration-memory node.
631 int of_drconf_to_nid_single(struct drmem_lmb *lmb)
633 struct assoc_arrays aa = { .arrays = NULL };
634 int default_nid = NUMA_NO_NODE;
635 int nid = default_nid;
638 if ((primary_domain_index < 0) || !numa_enabled)
641 rc = of_get_assoc_arrays(&aa);
645 if (primary_domain_index <= aa.array_sz &&
646 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
647 const __be32 *associativity;
649 index = lmb->aa_index * aa.array_sz;
650 associativity = &aa.arrays[index];
651 nid = __associativity_to_nid(associativity, aa.array_sz);
656 #ifdef CONFIG_PPC_SPLPAR
658 static int __vphn_get_associativity(long lcpu, __be32 *associativity)
663 * On a shared lpar, device tree will not have node associativity.
664 * At this time lppaca, or its __old_status field may not be
665 * updated. Hence kernel cannot detect if its on a shared lpar. So
666 * request an explicit associativity irrespective of whether the
667 * lpar is shared or dedicated. Use the device tree property as a
668 * fallback. cpu_to_phys_id is only valid between
669 * smp_setup_cpu_maps() and smp_setup_pacas().
671 if (firmware_has_feature(FW_FEATURE_VPHN)) {
673 hwid = cpu_to_phys_id[lcpu];
675 hwid = get_hard_smp_processor_id(lcpu);
677 rc = hcall_vphn(hwid, VPHN_FLAG_VCPU, associativity);
685 static int vphn_get_nid(long lcpu)
687 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
690 if (!__vphn_get_associativity(lcpu, associativity))
691 return associativity_to_nid(associativity);
698 static int __vphn_get_associativity(long lcpu, __be32 *associativity)
703 static int vphn_get_nid(long unused)
707 #endif /* CONFIG_PPC_SPLPAR */
710 * Figure out to which domain a cpu belongs and stick it there.
711 * Return the id of the domain used.
713 static int numa_setup_cpu(unsigned long lcpu)
715 struct device_node *cpu;
716 int fcpu = cpu_first_thread_sibling(lcpu);
717 int nid = NUMA_NO_NODE;
719 if (!cpu_present(lcpu)) {
720 set_cpu_numa_node(lcpu, first_online_node);
721 return first_online_node;
725 * If a valid cpu-to-node mapping is already available, use it
726 * directly instead of querying the firmware, since it represents
727 * the most recent mapping notified to us by the platform (eg: VPHN).
728 * Since cpu_to_node binding remains the same for all threads in the
729 * core. If a valid cpu-to-node mapping is already available, for
730 * the first thread in the core, use it.
732 nid = numa_cpu_lookup_table[fcpu];
734 map_cpu_to_node(lcpu, nid);
738 nid = vphn_get_nid(lcpu);
739 if (nid != NUMA_NO_NODE)
742 cpu = of_get_cpu_node(lcpu, NULL);
746 if (cpu_present(lcpu))
752 nid = of_node_to_nid_single(cpu);
756 if (nid < 0 || !node_possible(nid))
757 nid = first_online_node;
760 * Update for the first thread of the core. All threads of a core
761 * have to be part of the same node. This not only avoids querying
762 * for every other thread in the core, but always avoids a case
763 * where virtual node associativity change causes subsequent threads
764 * of a core to be associated with different nid. However if first
765 * thread is already online, expect it to have a valid mapping.
768 WARN_ON(cpu_online(fcpu));
769 map_cpu_to_node(fcpu, nid);
772 map_cpu_to_node(lcpu, nid);
777 static void verify_cpu_node_mapping(int cpu, int node)
779 int base, sibling, i;
781 /* Verify that all the threads in the core belong to the same node */
782 base = cpu_first_thread_sibling(cpu);
784 for (i = 0; i < threads_per_core; i++) {
787 if (sibling == cpu || cpu_is_offline(sibling))
790 if (cpu_to_node(sibling) != node) {
791 WARN(1, "CPU thread siblings %d and %d don't belong"
792 " to the same node!\n", cpu, sibling);
798 /* Must run before sched domains notifier. */
799 static int ppc_numa_cpu_prepare(unsigned int cpu)
803 nid = numa_setup_cpu(cpu);
804 verify_cpu_node_mapping(cpu, nid);
808 static int ppc_numa_cpu_dead(unsigned int cpu)
810 #ifdef CONFIG_HOTPLUG_CPU
811 unmap_cpu_from_node(cpu);
817 * Check and possibly modify a memory region to enforce the memory limit.
819 * Returns the size the region should have to enforce the memory limit.
820 * This will either be the original value of size, a truncated value,
821 * or zero. If the returned value of size is 0 the region should be
822 * discarded as it lies wholly above the memory limit.
824 static unsigned long __init numa_enforce_memory_limit(unsigned long start,
828 * We use memblock_end_of_DRAM() in here instead of memory_limit because
829 * we've already adjusted it for the limit and it takes care of
830 * having memory holes below the limit. Also, in the case of
831 * iommu_is_off, memory_limit is not set but is implicitly enforced.
834 if (start + size <= memblock_end_of_DRAM())
837 if (start >= memblock_end_of_DRAM())
840 return memblock_end_of_DRAM() - start;
844 * Reads the counter for a given entry in
845 * linux,drconf-usable-memory property
847 static inline int __init read_usm_ranges(const __be32 **usm)
850 * For each lmb in ibm,dynamic-memory a corresponding
851 * entry in linux,drconf-usable-memory property contains
852 * a counter followed by that many (base, size) duple.
853 * read the counter from linux,drconf-usable-memory
855 return read_n_cells(n_mem_size_cells, usm);
859 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
860 * node. This assumes n_mem_{addr,size}_cells have been set.
862 static int __init numa_setup_drmem_lmb(struct drmem_lmb *lmb,
866 unsigned int ranges, is_kexec_kdump = 0;
867 unsigned long base, size, sz;
871 * Skip this block if the reserved bit is set in flags (0x80)
872 * or if the block is not assigned to this partition (0x8)
874 if ((lmb->flags & DRCONF_MEM_RESERVED)
875 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
881 base = lmb->base_addr;
882 size = drmem_lmb_size();
885 if (is_kexec_kdump) {
886 ranges = read_usm_ranges(usm);
887 if (!ranges) /* there are no (base, size) duple */
892 if (is_kexec_kdump) {
893 base = read_n_cells(n_mem_addr_cells, usm);
894 size = read_n_cells(n_mem_size_cells, usm);
897 nid = get_nid_and_numa_distance(lmb);
898 fake_numa_create_new_node(((base + size) >> PAGE_SHIFT),
900 node_set_online(nid);
901 sz = numa_enforce_memory_limit(base, size);
903 memblock_set_node(base, sz, &memblock.memory, nid);
909 static int __init parse_numa_properties(void)
911 struct device_node *memory;
914 const __be32 *associativity;
916 if (numa_enabled == 0) {
917 printk(KERN_WARNING "NUMA disabled by user\n");
921 primary_domain_index = find_primary_domain_index();
923 if (primary_domain_index < 0) {
925 * if we fail to parse primary_domain_index from device tree
926 * mark the numa disabled, boot with numa disabled.
928 numa_enabled = false;
929 return primary_domain_index;
932 dbg("NUMA associativity depth for CPU/Memory: %d\n", primary_domain_index);
935 * If it is FORM2 initialize the distance table here.
937 if (affinity_form == FORM2_AFFINITY)
938 initialize_form2_numa_distance_lookup_table();
941 * Even though we connect cpus to numa domains later in SMP
942 * init, we need to know the node ids now. This is because
943 * each node to be onlined must have NODE_DATA etc backing it.
945 for_each_present_cpu(i) {
946 __be32 vphn_assoc[VPHN_ASSOC_BUFSIZE];
947 struct device_node *cpu;
948 int nid = NUMA_NO_NODE;
950 memset(vphn_assoc, 0, VPHN_ASSOC_BUFSIZE * sizeof(__be32));
952 if (__vphn_get_associativity(i, vphn_assoc) == 0) {
953 nid = associativity_to_nid(vphn_assoc);
954 initialize_form1_numa_distance(vphn_assoc);
958 * Don't fall back to default_nid yet -- we will plug
959 * cpus into nodes once the memory scan has discovered
962 cpu = of_get_cpu_node(i, NULL);
965 associativity = of_get_associativity(cpu);
967 nid = associativity_to_nid(associativity);
968 initialize_form1_numa_distance(associativity);
973 /* node_set_online() is an UB if 'nid' is negative */
974 if (likely(nid >= 0))
975 node_set_online(nid);
978 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
980 for_each_node_by_type(memory, "memory") {
985 const __be32 *memcell_buf;
988 memcell_buf = of_get_property(memory,
989 "linux,usable-memory", &len);
990 if (!memcell_buf || len <= 0)
991 memcell_buf = of_get_property(memory, "reg", &len);
992 if (!memcell_buf || len <= 0)
996 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
998 /* these are order-sensitive, and modify the buffer pointer */
999 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1000 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1003 * Assumption: either all memory nodes or none will
1004 * have associativity properties. If none, then
1005 * everything goes to default_nid.
1007 associativity = of_get_associativity(memory);
1008 if (associativity) {
1009 nid = associativity_to_nid(associativity);
1010 initialize_form1_numa_distance(associativity);
1014 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
1015 node_set_online(nid);
1017 size = numa_enforce_memory_limit(start, size);
1019 memblock_set_node(start, size, &memblock.memory, nid);
1026 * Now do the same thing for each MEMBLOCK listed in the
1027 * ibm,dynamic-memory property in the
1028 * ibm,dynamic-reconfiguration-memory node.
1030 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1032 walk_drmem_lmbs(memory, NULL, numa_setup_drmem_lmb);
1033 of_node_put(memory);
1039 static void __init setup_nonnuma(void)
1041 unsigned long top_of_ram = memblock_end_of_DRAM();
1042 unsigned long total_ram = memblock_phys_mem_size();
1043 unsigned long start_pfn, end_pfn;
1044 unsigned int nid = 0;
1047 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1048 top_of_ram, total_ram);
1049 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
1050 (top_of_ram - total_ram) >> 20);
1052 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
1053 fake_numa_create_new_node(end_pfn, &nid);
1054 memblock_set_node(PFN_PHYS(start_pfn),
1055 PFN_PHYS(end_pfn - start_pfn),
1056 &memblock.memory, nid);
1057 node_set_online(nid);
1061 void __init dump_numa_cpu_topology(void)
1064 unsigned int cpu, count;
1069 for_each_online_node(node) {
1070 pr_info("Node %d CPUs:", node);
1074 * If we used a CPU iterator here we would miss printing
1075 * the holes in the cpumap.
1077 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1078 if (cpumask_test_cpu(cpu,
1079 node_to_cpumask_map[node])) {
1081 pr_cont(" %u", cpu);
1085 pr_cont("-%u", cpu - 1);
1091 pr_cont("-%u", nr_cpu_ids - 1);
1096 /* Initialize NODE_DATA for a node on the local memory */
1097 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
1099 u64 spanned_pages = end_pfn - start_pfn;
1100 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
1105 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
1107 panic("Cannot allocate %zu bytes for node %d data\n",
1112 /* report and initialize */
1113 pr_info(" NODE_DATA [mem %#010Lx-%#010Lx]\n",
1114 nd_pa, nd_pa + nd_size - 1);
1115 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
1117 pr_info(" NODE_DATA(%d) on node %d\n", nid, tnid);
1119 node_data[nid] = nd;
1120 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
1121 NODE_DATA(nid)->node_id = nid;
1122 NODE_DATA(nid)->node_start_pfn = start_pfn;
1123 NODE_DATA(nid)->node_spanned_pages = spanned_pages;
1126 static void __init find_possible_nodes(void)
1128 struct device_node *rtas;
1129 const __be32 *domains = NULL;
1130 int prop_length, max_nodes;
1136 rtas = of_find_node_by_path("/rtas");
1141 * ibm,current-associativity-domains is a fairly recent property. If
1142 * it doesn't exist, then fallback on ibm,max-associativity-domains.
1143 * Current denotes what the platform can support compared to max
1144 * which denotes what the Hypervisor can support.
1146 * If the LPAR is migratable, new nodes might be activated after a LPM,
1147 * so we should consider the max number in that case.
1149 if (!of_get_property(of_root, "ibm,migratable-partition", NULL))
1150 domains = of_get_property(rtas,
1151 "ibm,current-associativity-domains",
1154 domains = of_get_property(rtas, "ibm,max-associativity-domains",
1160 max_nodes = of_read_number(&domains[primary_domain_index], 1);
1161 pr_info("Partition configured for %d NUMA nodes.\n", max_nodes);
1163 for (i = 0; i < max_nodes; i++) {
1164 if (!node_possible(i))
1165 node_set(i, node_possible_map);
1168 prop_length /= sizeof(int);
1169 if (prop_length > primary_domain_index + 2)
1170 coregroup_enabled = 1;
1176 void __init mem_topology_setup(void)
1181 * Linux/mm assumes node 0 to be online at boot. However this is not
1182 * true on PowerPC, where node 0 is similar to any other node, it
1183 * could be cpuless, memoryless node. So force node 0 to be offline
1184 * for now. This will prevent cpuless, memoryless node 0 showing up
1185 * unnecessarily as online. If a node has cpus or memory that need
1186 * to be online, then node will anyway be marked online.
1188 node_set_offline(0);
1190 if (parse_numa_properties())
1194 * Modify the set of possible NUMA nodes to reflect information
1195 * available about the set of online nodes, and the set of nodes
1196 * that we expect to make use of for this platform's affinity
1199 nodes_and(node_possible_map, node_possible_map, node_online_map);
1201 find_possible_nodes();
1203 setup_node_to_cpumask_map();
1205 reset_numa_cpu_lookup_table();
1207 for_each_possible_cpu(cpu) {
1209 * Powerpc with CONFIG_NUMA always used to have a node 0,
1210 * even if it was memoryless or cpuless. For all cpus that
1211 * are possible but not present, cpu_to_node() would point
1212 * to node 0. To remove a cpuless, memoryless dummy node,
1213 * powerpc need to make sure all possible but not present
1214 * cpu_to_node are set to a proper node.
1216 numa_setup_cpu(cpu);
1220 void __init initmem_init(void)
1224 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1225 max_pfn = max_low_pfn;
1227 memblock_dump_all();
1229 for_each_online_node(nid) {
1230 unsigned long start_pfn, end_pfn;
1232 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1233 setup_node_data(nid, start_pfn, end_pfn);
1239 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
1240 * even before we online them, so that we can use cpu_to_{node,mem}
1241 * early in boot, cf. smp_prepare_cpus().
1242 * _nocalls() + manual invocation is used because cpuhp is not yet
1243 * initialized for the boot CPU.
1245 cpuhp_setup_state_nocalls(CPUHP_POWER_NUMA_PREPARE, "powerpc/numa:prepare",
1246 ppc_numa_cpu_prepare, ppc_numa_cpu_dead);
1249 static int __init early_numa(char *p)
1254 if (strstr(p, "off"))
1257 if (strstr(p, "debug"))
1260 p = strstr(p, "fake=");
1262 cmdline = p + strlen("fake=");
1266 early_param("numa", early_numa);
1268 #ifdef CONFIG_MEMORY_HOTPLUG
1270 * Find the node associated with a hot added memory section for
1271 * memory represented in the device tree by the property
1272 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
1274 static int hot_add_drconf_scn_to_nid(unsigned long scn_addr)
1276 struct drmem_lmb *lmb;
1277 unsigned long lmb_size;
1278 int nid = NUMA_NO_NODE;
1280 lmb_size = drmem_lmb_size();
1282 for_each_drmem_lmb(lmb) {
1283 /* skip this block if it is reserved or not assigned to
1285 if ((lmb->flags & DRCONF_MEM_RESERVED)
1286 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
1289 if ((scn_addr < lmb->base_addr)
1290 || (scn_addr >= (lmb->base_addr + lmb_size)))
1293 nid = of_drconf_to_nid_single(lmb);
1301 * Find the node associated with a hot added memory section for memory
1302 * represented in the device tree as a node (i.e. memory@XXXX) for
1305 static int hot_add_node_scn_to_nid(unsigned long scn_addr)
1307 struct device_node *memory;
1308 int nid = NUMA_NO_NODE;
1310 for_each_node_by_type(memory, "memory") {
1311 unsigned long start, size;
1313 const __be32 *memcell_buf;
1316 memcell_buf = of_get_property(memory, "reg", &len);
1317 if (!memcell_buf || len <= 0)
1320 /* ranges in cell */
1321 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1324 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1325 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1327 if ((scn_addr < start) || (scn_addr >= (start + size)))
1330 nid = of_node_to_nid_single(memory);
1338 of_node_put(memory);
1344 * Find the node associated with a hot added memory section. Section
1345 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1346 * sections are fully contained within a single MEMBLOCK.
1348 int hot_add_scn_to_nid(unsigned long scn_addr)
1350 struct device_node *memory = NULL;
1354 return first_online_node;
1356 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1358 nid = hot_add_drconf_scn_to_nid(scn_addr);
1359 of_node_put(memory);
1361 nid = hot_add_node_scn_to_nid(scn_addr);
1364 if (nid < 0 || !node_possible(nid))
1365 nid = first_online_node;
1370 static u64 hot_add_drconf_memory_max(void)
1372 struct device_node *memory = NULL;
1373 struct device_node *dn = NULL;
1374 const __be64 *lrdr = NULL;
1376 dn = of_find_node_by_path("/rtas");
1378 lrdr = of_get_property(dn, "ibm,lrdr-capacity", NULL);
1381 return be64_to_cpup(lrdr);
1384 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1386 of_node_put(memory);
1387 return drmem_lmb_memory_max();
1393 * memory_hotplug_max - return max address of memory that may be added
1395 * This is currently only used on systems that support drconfig memory
1398 u64 memory_hotplug_max(void)
1400 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1402 #endif /* CONFIG_MEMORY_HOTPLUG */
1404 /* Virtual Processor Home Node (VPHN) support */
1405 #ifdef CONFIG_PPC_SPLPAR
1406 static int topology_inited;
1409 * Retrieve the new associativity information for a virtual processor's
1412 static long vphn_get_associativity(unsigned long cpu,
1413 __be32 *associativity)
1417 rc = hcall_vphn(get_hard_smp_processor_id(cpu),
1418 VPHN_FLAG_VCPU, associativity);
1422 dbg("VPHN hcall succeeded. Reset polling...\n");
1426 pr_err_ratelimited("VPHN unsupported. Disabling polling...\n");
1429 pr_err_ratelimited("hcall_vphn() experienced a hardware fault "
1430 "preventing VPHN. Disabling polling...\n");
1433 pr_err_ratelimited("hcall_vphn() was passed an invalid parameter. "
1434 "Disabling polling...\n");
1437 pr_err_ratelimited("hcall_vphn() returned %ld. Disabling polling...\n"
1445 int find_and_online_cpu_nid(int cpu)
1447 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1450 /* Use associativity from first thread for all siblings */
1451 if (vphn_get_associativity(cpu, associativity))
1452 return cpu_to_node(cpu);
1454 new_nid = associativity_to_nid(associativity);
1455 if (new_nid < 0 || !node_possible(new_nid))
1456 new_nid = first_online_node;
1458 if (NODE_DATA(new_nid) == NULL) {
1459 #ifdef CONFIG_MEMORY_HOTPLUG
1461 * Need to ensure that NODE_DATA is initialized for a node from
1462 * available memory (see memblock_alloc_try_nid). If unable to
1463 * init the node, then default to nearest node that has memory
1464 * installed. Skip onlining a node if the subsystems are not
1467 if (!topology_inited || try_online_node(new_nid))
1468 new_nid = first_online_node;
1471 * Default to using the nearest node that has memory installed.
1472 * Otherwise, it would be necessary to patch the kernel MM code
1473 * to deal with more memoryless-node error conditions.
1475 new_nid = first_online_node;
1479 pr_debug("%s:%d cpu %d nid %d\n", __FUNCTION__, __LINE__,
1484 int cpu_to_coregroup_id(int cpu)
1486 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1489 if (cpu < 0 || cpu > nr_cpu_ids)
1492 if (!coregroup_enabled)
1495 if (!firmware_has_feature(FW_FEATURE_VPHN))
1498 if (vphn_get_associativity(cpu, associativity))
1501 index = of_read_number(associativity, 1);
1502 if (index > primary_domain_index + 1)
1503 return of_read_number(&associativity[index - 1], 1);
1506 return cpu_to_core_id(cpu);
1509 static int topology_update_init(void)
1511 topology_inited = 1;
1514 device_initcall(topology_update_init);
1515 #endif /* CONFIG_PPC_SPLPAR */