1 // SPDX-License-Identifier: GPL-2.0-or-later
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 min_common_depth;
55 static int n_mem_addr_cells, n_mem_size_cells;
56 static int form1_affinity;
58 #define MAX_DISTANCE_REF_POINTS 4
59 static int distance_ref_points_depth;
60 static const __be32 *distance_ref_points;
61 static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
64 * Allocate node_to_cpumask_map based on number of available nodes
65 * Requires node_possible_map to be valid.
67 * Note: cpumask_of_node() is not valid until after this is done.
69 static void __init setup_node_to_cpumask_map(void)
73 /* setup nr_node_ids if not done yet */
74 if (nr_node_ids == MAX_NUMNODES)
77 /* allocate the map */
79 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
81 /* cpumask_of_node() will now work */
82 dbg("Node to cpumask map for %u nodes\n", nr_node_ids);
85 static int __init fake_numa_create_new_node(unsigned long end_pfn,
88 unsigned long long mem;
90 static unsigned int fake_nid;
91 static unsigned long long curr_boundary;
94 * Modify node id, iff we started creating NUMA nodes
95 * We want to continue from where we left of the last time
100 * In case there are no more arguments to parse, the
101 * node_id should be the same as the last fake node id
102 * (we've handled this above).
107 mem = memparse(p, &p);
111 if (mem < curr_boundary)
116 if ((end_pfn << PAGE_SHIFT) > mem) {
118 * Skip commas and spaces
120 while (*p == ',' || *p == ' ' || *p == '\t')
126 dbg("created new fake_node with id %d\n", fake_nid);
132 static void reset_numa_cpu_lookup_table(void)
136 for_each_possible_cpu(cpu)
137 numa_cpu_lookup_table[cpu] = -1;
140 static void map_cpu_to_node(int cpu, int node)
142 update_numa_cpu_lookup_table(cpu, node);
144 dbg("adding cpu %d to node %d\n", cpu, node);
146 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
147 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
150 #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
151 static void unmap_cpu_from_node(unsigned long cpu)
153 int node = numa_cpu_lookup_table[cpu];
155 dbg("removing cpu %lu from node %d\n", cpu, node);
157 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
158 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
160 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
164 #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
166 /* must hold reference to node during call */
167 static const __be32 *of_get_associativity(struct device_node *dev)
169 return of_get_property(dev, "ibm,associativity", NULL);
172 int __node_distance(int a, int b)
175 int distance = LOCAL_DISTANCE;
178 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
180 for (i = 0; i < distance_ref_points_depth; i++) {
181 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
184 /* Double the distance for each NUMA level */
190 EXPORT_SYMBOL(__node_distance);
192 static void initialize_distance_lookup_table(int nid,
193 const __be32 *associativity)
200 for (i = 0; i < distance_ref_points_depth; i++) {
203 entry = &associativity[be32_to_cpu(distance_ref_points[i]) - 1];
204 distance_lookup_table[nid][i] = of_read_number(entry, 1);
208 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
211 static int associativity_to_nid(const __be32 *associativity)
213 int nid = NUMA_NO_NODE;
215 if (min_common_depth == -1)
218 if (of_read_number(associativity, 1) >= min_common_depth)
219 nid = of_read_number(&associativity[min_common_depth], 1);
221 /* POWER4 LPAR uses 0xffff as invalid node */
222 if (nid == 0xffff || nid >= MAX_NUMNODES)
226 of_read_number(associativity, 1) >= distance_ref_points_depth) {
228 * Skip the length field and send start of associativity array
230 initialize_distance_lookup_table(nid, associativity + 1);
237 /* Returns the nid associated with the given device tree node,
238 * or -1 if not found.
240 static int of_node_to_nid_single(struct device_node *device)
242 int nid = NUMA_NO_NODE;
245 tmp = of_get_associativity(device);
247 nid = associativity_to_nid(tmp);
251 /* Walk the device tree upwards, looking for an associativity id */
252 int of_node_to_nid(struct device_node *device)
254 int nid = NUMA_NO_NODE;
258 nid = of_node_to_nid_single(device);
262 device = of_get_next_parent(device);
268 EXPORT_SYMBOL(of_node_to_nid);
270 static int __init find_min_common_depth(void)
273 struct device_node *root;
275 if (firmware_has_feature(FW_FEATURE_OPAL))
276 root = of_find_node_by_path("/ibm,opal");
278 root = of_find_node_by_path("/rtas");
280 root = of_find_node_by_path("/");
283 * This property is a set of 32-bit integers, each representing
284 * an index into the ibm,associativity nodes.
286 * With form 0 affinity the first integer is for an SMP configuration
287 * (should be all 0's) and the second is for a normal NUMA
288 * configuration. We have only one level of NUMA.
290 * With form 1 affinity the first integer is the most significant
291 * NUMA boundary and the following are progressively less significant
292 * boundaries. There can be more than one level of NUMA.
294 distance_ref_points = of_get_property(root,
295 "ibm,associativity-reference-points",
296 &distance_ref_points_depth);
298 if (!distance_ref_points) {
299 dbg("NUMA: ibm,associativity-reference-points not found.\n");
303 distance_ref_points_depth /= sizeof(int);
305 if (firmware_has_feature(FW_FEATURE_OPAL) ||
306 firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
307 dbg("Using form 1 affinity\n");
311 if (form1_affinity) {
312 depth = of_read_number(distance_ref_points, 1);
314 if (distance_ref_points_depth < 2) {
315 printk(KERN_WARNING "NUMA: "
316 "short ibm,associativity-reference-points\n");
320 depth = of_read_number(&distance_ref_points[1], 1);
324 * Warn and cap if the hardware supports more than
325 * MAX_DISTANCE_REF_POINTS domains.
327 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
328 printk(KERN_WARNING "NUMA: distance array capped at "
329 "%d entries\n", MAX_DISTANCE_REF_POINTS);
330 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
341 static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
343 struct device_node *memory = NULL;
345 memory = of_find_node_by_type(memory, "memory");
347 panic("numa.c: No memory nodes found!");
349 *n_addr_cells = of_n_addr_cells(memory);
350 *n_size_cells = of_n_size_cells(memory);
354 static unsigned long read_n_cells(int n, const __be32 **buf)
356 unsigned long result = 0;
359 result = (result << 32) | of_read_number(*buf, 1);
365 struct assoc_arrays {
368 const __be32 *arrays;
372 * Retrieve and validate the list of associativity arrays for drconf
373 * memory from the ibm,associativity-lookup-arrays property of the
376 * The layout of the ibm,associativity-lookup-arrays property is a number N
377 * indicating the number of associativity arrays, followed by a number M
378 * indicating the size of each associativity array, followed by a list
379 * of N associativity arrays.
381 static int of_get_assoc_arrays(struct assoc_arrays *aa)
383 struct device_node *memory;
387 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
391 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
392 if (!prop || len < 2 * sizeof(unsigned int)) {
397 aa->n_arrays = of_read_number(prop++, 1);
398 aa->array_sz = of_read_number(prop++, 1);
402 /* Now that we know the number of arrays and size of each array,
403 * revalidate the size of the property read in.
405 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
413 * This is like of_node_to_nid_single() for memory represented in the
414 * ibm,dynamic-reconfiguration-memory node.
416 static int of_drconf_to_nid_single(struct drmem_lmb *lmb)
418 struct assoc_arrays aa = { .arrays = NULL };
420 int nid = default_nid;
423 rc = of_get_assoc_arrays(&aa);
427 if (min_common_depth > 0 && min_common_depth <= aa.array_sz &&
428 !(lmb->flags & DRCONF_MEM_AI_INVALID) &&
429 lmb->aa_index < aa.n_arrays) {
430 index = lmb->aa_index * aa.array_sz + min_common_depth - 1;
431 nid = of_read_number(&aa.arrays[index], 1);
433 if (nid == 0xffff || nid >= MAX_NUMNODES)
437 index = lmb->aa_index * aa.array_sz;
438 initialize_distance_lookup_table(nid,
447 * Figure out to which domain a cpu belongs and stick it there.
448 * Return the id of the domain used.
450 static int numa_setup_cpu(unsigned long lcpu)
452 int nid = NUMA_NO_NODE;
453 struct device_node *cpu;
456 * If a valid cpu-to-node mapping is already available, use it
457 * directly instead of querying the firmware, since it represents
458 * the most recent mapping notified to us by the platform (eg: VPHN).
460 if ((nid = numa_cpu_lookup_table[lcpu]) >= 0) {
461 map_cpu_to_node(lcpu, nid);
465 cpu = of_get_cpu_node(lcpu, NULL);
469 if (cpu_present(lcpu))
475 nid = of_node_to_nid_single(cpu);
478 if (nid < 0 || !node_possible(nid))
479 nid = first_online_node;
481 map_cpu_to_node(lcpu, nid);
487 static void verify_cpu_node_mapping(int cpu, int node)
489 int base, sibling, i;
491 /* Verify that all the threads in the core belong to the same node */
492 base = cpu_first_thread_sibling(cpu);
494 for (i = 0; i < threads_per_core; i++) {
497 if (sibling == cpu || cpu_is_offline(sibling))
500 if (cpu_to_node(sibling) != node) {
501 WARN(1, "CPU thread siblings %d and %d don't belong"
502 " to the same node!\n", cpu, sibling);
508 /* Must run before sched domains notifier. */
509 static int ppc_numa_cpu_prepare(unsigned int cpu)
513 nid = numa_setup_cpu(cpu);
514 verify_cpu_node_mapping(cpu, nid);
518 static int ppc_numa_cpu_dead(unsigned int cpu)
520 #ifdef CONFIG_HOTPLUG_CPU
521 unmap_cpu_from_node(cpu);
527 * Check and possibly modify a memory region to enforce the memory limit.
529 * Returns the size the region should have to enforce the memory limit.
530 * This will either be the original value of size, a truncated value,
531 * or zero. If the returned value of size is 0 the region should be
532 * discarded as it lies wholly above the memory limit.
534 static unsigned long __init numa_enforce_memory_limit(unsigned long start,
538 * We use memblock_end_of_DRAM() in here instead of memory_limit because
539 * we've already adjusted it for the limit and it takes care of
540 * having memory holes below the limit. Also, in the case of
541 * iommu_is_off, memory_limit is not set but is implicitly enforced.
544 if (start + size <= memblock_end_of_DRAM())
547 if (start >= memblock_end_of_DRAM())
550 return memblock_end_of_DRAM() - start;
554 * Reads the counter for a given entry in
555 * linux,drconf-usable-memory property
557 static inline int __init read_usm_ranges(const __be32 **usm)
560 * For each lmb in ibm,dynamic-memory a corresponding
561 * entry in linux,drconf-usable-memory property contains
562 * a counter followed by that many (base, size) duple.
563 * read the counter from linux,drconf-usable-memory
565 return read_n_cells(n_mem_size_cells, usm);
569 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
570 * node. This assumes n_mem_{addr,size}_cells have been set.
572 static void __init numa_setup_drmem_lmb(struct drmem_lmb *lmb,
575 unsigned int ranges, is_kexec_kdump = 0;
576 unsigned long base, size, sz;
580 * Skip this block if the reserved bit is set in flags (0x80)
581 * or if the block is not assigned to this partition (0x8)
583 if ((lmb->flags & DRCONF_MEM_RESERVED)
584 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
590 base = lmb->base_addr;
591 size = drmem_lmb_size();
594 if (is_kexec_kdump) {
595 ranges = read_usm_ranges(usm);
596 if (!ranges) /* there are no (base, size) duple */
601 if (is_kexec_kdump) {
602 base = read_n_cells(n_mem_addr_cells, usm);
603 size = read_n_cells(n_mem_size_cells, usm);
606 nid = of_drconf_to_nid_single(lmb);
607 fake_numa_create_new_node(((base + size) >> PAGE_SHIFT),
609 node_set_online(nid);
610 sz = numa_enforce_memory_limit(base, size);
612 memblock_set_node(base, sz, &memblock.memory, nid);
616 static int __init parse_numa_properties(void)
618 struct device_node *memory;
622 if (numa_enabled == 0) {
623 printk(KERN_WARNING "NUMA disabled by user\n");
627 min_common_depth = find_min_common_depth();
629 if (min_common_depth < 0)
630 return min_common_depth;
632 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
635 * Even though we connect cpus to numa domains later in SMP
636 * init, we need to know the node ids now. This is because
637 * each node to be onlined must have NODE_DATA etc backing it.
639 for_each_present_cpu(i) {
640 struct device_node *cpu;
643 cpu = of_get_cpu_node(i, NULL);
645 nid = of_node_to_nid_single(cpu);
649 * Don't fall back to default_nid yet -- we will plug
650 * cpus into nodes once the memory scan has discovered
655 node_set_online(nid);
658 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
660 for_each_node_by_type(memory, "memory") {
665 const __be32 *memcell_buf;
668 memcell_buf = of_get_property(memory,
669 "linux,usable-memory", &len);
670 if (!memcell_buf || len <= 0)
671 memcell_buf = of_get_property(memory, "reg", &len);
672 if (!memcell_buf || len <= 0)
676 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
678 /* these are order-sensitive, and modify the buffer pointer */
679 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
680 size = read_n_cells(n_mem_size_cells, &memcell_buf);
683 * Assumption: either all memory nodes or none will
684 * have associativity properties. If none, then
685 * everything goes to default_nid.
687 nid = of_node_to_nid_single(memory);
691 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
692 node_set_online(nid);
694 size = numa_enforce_memory_limit(start, size);
696 memblock_set_node(start, size, &memblock.memory, nid);
703 * Now do the same thing for each MEMBLOCK listed in the
704 * ibm,dynamic-memory property in the
705 * ibm,dynamic-reconfiguration-memory node.
707 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
709 walk_drmem_lmbs(memory, numa_setup_drmem_lmb);
716 static void __init setup_nonnuma(void)
718 unsigned long top_of_ram = memblock_end_of_DRAM();
719 unsigned long total_ram = memblock_phys_mem_size();
720 unsigned long start_pfn, end_pfn;
721 unsigned int nid = 0;
722 struct memblock_region *reg;
724 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
725 top_of_ram, total_ram);
726 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
727 (top_of_ram - total_ram) >> 20);
729 for_each_memblock(memory, reg) {
730 start_pfn = memblock_region_memory_base_pfn(reg);
731 end_pfn = memblock_region_memory_end_pfn(reg);
733 fake_numa_create_new_node(end_pfn, &nid);
734 memblock_set_node(PFN_PHYS(start_pfn),
735 PFN_PHYS(end_pfn - start_pfn),
736 &memblock.memory, nid);
737 node_set_online(nid);
741 void __init dump_numa_cpu_topology(void)
744 unsigned int cpu, count;
746 if (min_common_depth == -1 || !numa_enabled)
749 for_each_online_node(node) {
750 pr_info("Node %d CPUs:", node);
754 * If we used a CPU iterator here we would miss printing
755 * the holes in the cpumap.
757 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
758 if (cpumask_test_cpu(cpu,
759 node_to_cpumask_map[node])) {
765 pr_cont("-%u", cpu - 1);
771 pr_cont("-%u", nr_cpu_ids - 1);
776 /* Initialize NODE_DATA for a node on the local memory */
777 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
779 u64 spanned_pages = end_pfn - start_pfn;
780 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
785 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
787 panic("Cannot allocate %zu bytes for node %d data\n",
792 /* report and initialize */
793 pr_info(" NODE_DATA [mem %#010Lx-%#010Lx]\n",
794 nd_pa, nd_pa + nd_size - 1);
795 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
797 pr_info(" NODE_DATA(%d) on node %d\n", nid, tnid);
800 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
801 NODE_DATA(nid)->node_id = nid;
802 NODE_DATA(nid)->node_start_pfn = start_pfn;
803 NODE_DATA(nid)->node_spanned_pages = spanned_pages;
806 static void __init find_possible_nodes(void)
808 struct device_node *rtas;
811 if (min_common_depth <= 0)
814 rtas = of_find_node_by_path("/rtas");
818 if (of_property_read_u32_index(rtas,
819 "ibm,max-associativity-domains",
820 min_common_depth, &numnodes))
823 for (i = 0; i < numnodes; i++) {
824 if (!node_possible(i))
825 node_set(i, node_possible_map);
832 void __init mem_topology_setup(void)
836 if (parse_numa_properties())
840 * Modify the set of possible NUMA nodes to reflect information
841 * available about the set of online nodes, and the set of nodes
842 * that we expect to make use of for this platform's affinity
845 nodes_and(node_possible_map, node_possible_map, node_online_map);
847 find_possible_nodes();
849 setup_node_to_cpumask_map();
851 reset_numa_cpu_lookup_table();
853 for_each_present_cpu(cpu)
857 void __init initmem_init(void)
861 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
862 max_pfn = max_low_pfn;
866 for_each_online_node(nid) {
867 unsigned long start_pfn, end_pfn;
869 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
870 setup_node_data(nid, start_pfn, end_pfn);
871 sparse_memory_present_with_active_regions(nid);
877 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
878 * even before we online them, so that we can use cpu_to_{node,mem}
879 * early in boot, cf. smp_prepare_cpus().
880 * _nocalls() + manual invocation is used because cpuhp is not yet
881 * initialized for the boot CPU.
883 cpuhp_setup_state_nocalls(CPUHP_POWER_NUMA_PREPARE, "powerpc/numa:prepare",
884 ppc_numa_cpu_prepare, ppc_numa_cpu_dead);
887 static int __init early_numa(char *p)
892 if (strstr(p, "off"))
895 if (strstr(p, "debug"))
898 p = strstr(p, "fake=");
900 cmdline = p + strlen("fake=");
904 early_param("numa", early_numa);
907 * The platform can inform us through one of several mechanisms
908 * (post-migration device tree updates, PRRN or VPHN) that the NUMA
909 * assignment of a resource has changed. This controls whether we act
910 * on that. Disabled by default.
912 static bool topology_updates_enabled;
914 static int __init early_topology_updates(char *p)
919 if (!strcmp(p, "on")) {
920 pr_warn("Caution: enabling topology updates\n");
921 topology_updates_enabled = true;
926 early_param("topology_updates", early_topology_updates);
928 #ifdef CONFIG_MEMORY_HOTPLUG
930 * Find the node associated with a hot added memory section for
931 * memory represented in the device tree by the property
932 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
934 static int hot_add_drconf_scn_to_nid(unsigned long scn_addr)
936 struct drmem_lmb *lmb;
937 unsigned long lmb_size;
938 int nid = NUMA_NO_NODE;
940 lmb_size = drmem_lmb_size();
942 for_each_drmem_lmb(lmb) {
943 /* skip this block if it is reserved or not assigned to
945 if ((lmb->flags & DRCONF_MEM_RESERVED)
946 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
949 if ((scn_addr < lmb->base_addr)
950 || (scn_addr >= (lmb->base_addr + lmb_size)))
953 nid = of_drconf_to_nid_single(lmb);
961 * Find the node associated with a hot added memory section for memory
962 * represented in the device tree as a node (i.e. memory@XXXX) for
965 static int hot_add_node_scn_to_nid(unsigned long scn_addr)
967 struct device_node *memory;
968 int nid = NUMA_NO_NODE;
970 for_each_node_by_type(memory, "memory") {
971 unsigned long start, size;
973 const __be32 *memcell_buf;
976 memcell_buf = of_get_property(memory, "reg", &len);
977 if (!memcell_buf || len <= 0)
981 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
984 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
985 size = read_n_cells(n_mem_size_cells, &memcell_buf);
987 if ((scn_addr < start) || (scn_addr >= (start + size)))
990 nid = of_node_to_nid_single(memory);
1004 * Find the node associated with a hot added memory section. Section
1005 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1006 * sections are fully contained within a single MEMBLOCK.
1008 int hot_add_scn_to_nid(unsigned long scn_addr)
1010 struct device_node *memory = NULL;
1013 if (!numa_enabled || (min_common_depth < 0))
1014 return first_online_node;
1016 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1018 nid = hot_add_drconf_scn_to_nid(scn_addr);
1019 of_node_put(memory);
1021 nid = hot_add_node_scn_to_nid(scn_addr);
1024 if (nid < 0 || !node_possible(nid))
1025 nid = first_online_node;
1030 static u64 hot_add_drconf_memory_max(void)
1032 struct device_node *memory = NULL;
1033 struct device_node *dn = NULL;
1034 const __be64 *lrdr = NULL;
1036 dn = of_find_node_by_path("/rtas");
1038 lrdr = of_get_property(dn, "ibm,lrdr-capacity", NULL);
1041 return be64_to_cpup(lrdr);
1044 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1046 of_node_put(memory);
1047 return drmem_lmb_memory_max();
1053 * memory_hotplug_max - return max address of memory that may be added
1055 * This is currently only used on systems that support drconfig memory
1058 u64 memory_hotplug_max(void)
1060 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1062 #endif /* CONFIG_MEMORY_HOTPLUG */
1064 /* Virtual Processor Home Node (VPHN) support */
1065 #ifdef CONFIG_PPC_SPLPAR
1067 #include "book3s64/vphn.h"
1069 struct topology_update_data {
1070 struct topology_update_data *next;
1076 #define TOPOLOGY_DEF_TIMER_SECS 60
1078 static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
1079 static cpumask_t cpu_associativity_changes_mask;
1080 static int vphn_enabled;
1081 static int prrn_enabled;
1082 static void reset_topology_timer(void);
1083 static int topology_timer_secs = 1;
1084 static int topology_inited;
1087 * Change polling interval for associativity changes.
1089 int timed_topology_update(int nsecs)
1093 topology_timer_secs = nsecs;
1095 topology_timer_secs = TOPOLOGY_DEF_TIMER_SECS;
1097 reset_topology_timer();
1104 * Store the current values of the associativity change counters in the
1107 static void setup_cpu_associativity_change_counters(void)
1111 /* The VPHN feature supports a maximum of 8 reference points */
1112 BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1114 for_each_possible_cpu(cpu) {
1116 u8 *counts = vphn_cpu_change_counts[cpu];
1117 volatile u8 *hypervisor_counts = lppaca_of(cpu).vphn_assoc_counts;
1119 for (i = 0; i < distance_ref_points_depth; i++)
1120 counts[i] = hypervisor_counts[i];
1125 * The hypervisor maintains a set of 8 associativity change counters in
1126 * the VPA of each cpu that correspond to the associativity levels in the
1127 * ibm,associativity-reference-points property. When an associativity
1128 * level changes, the corresponding counter is incremented.
1130 * Set a bit in cpu_associativity_changes_mask for each cpu whose home
1131 * node associativity levels have changed.
1133 * Returns the number of cpus with unhandled associativity changes.
1135 static int update_cpu_associativity_changes_mask(void)
1138 cpumask_t *changes = &cpu_associativity_changes_mask;
1140 for_each_possible_cpu(cpu) {
1142 u8 *counts = vphn_cpu_change_counts[cpu];
1143 volatile u8 *hypervisor_counts = lppaca_of(cpu).vphn_assoc_counts;
1145 for (i = 0; i < distance_ref_points_depth; i++) {
1146 if (hypervisor_counts[i] != counts[i]) {
1147 counts[i] = hypervisor_counts[i];
1152 cpumask_or(changes, changes, cpu_sibling_mask(cpu));
1153 cpu = cpu_last_thread_sibling(cpu);
1157 return cpumask_weight(changes);
1161 * Retrieve the new associativity information for a virtual processor's
1164 static long hcall_vphn(unsigned long cpu, __be32 *associativity)
1167 long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1169 int hwcpu = get_hard_smp_processor_id(cpu);
1171 rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
1172 vphn_unpack_associativity(retbuf, associativity);
1177 static long vphn_get_associativity(unsigned long cpu,
1178 __be32 *associativity)
1182 rc = hcall_vphn(cpu, associativity);
1186 printk_once(KERN_INFO
1187 "VPHN is not supported. Disabling polling...\n");
1188 stop_topology_update();
1192 "hcall_vphn() experienced a hardware fault "
1193 "preventing VPHN. Disabling polling...\n");
1194 stop_topology_update();
1197 dbg("VPHN hcall succeeded. Reset polling...\n");
1198 timed_topology_update(0);
1205 int find_and_online_cpu_nid(int cpu)
1207 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1210 /* Use associativity from first thread for all siblings */
1211 if (vphn_get_associativity(cpu, associativity))
1212 return cpu_to_node(cpu);
1214 new_nid = associativity_to_nid(associativity);
1215 if (new_nid < 0 || !node_possible(new_nid))
1216 new_nid = first_online_node;
1218 if (NODE_DATA(new_nid) == NULL) {
1219 #ifdef CONFIG_MEMORY_HOTPLUG
1221 * Need to ensure that NODE_DATA is initialized for a node from
1222 * available memory (see memblock_alloc_try_nid). If unable to
1223 * init the node, then default to nearest node that has memory
1224 * installed. Skip onlining a node if the subsystems are not
1227 if (!topology_inited || try_online_node(new_nid))
1228 new_nid = first_online_node;
1231 * Default to using the nearest node that has memory installed.
1232 * Otherwise, it would be necessary to patch the kernel MM code
1233 * to deal with more memoryless-node error conditions.
1235 new_nid = first_online_node;
1239 pr_debug("%s:%d cpu %d nid %d\n", __FUNCTION__, __LINE__,
1245 * Update the CPU maps and sysfs entries for a single CPU when its NUMA
1246 * characteristics change. This function doesn't perform any locking and is
1247 * only safe to call from stop_machine().
1249 static int update_cpu_topology(void *data)
1251 struct topology_update_data *update;
1257 cpu = smp_processor_id();
1259 for (update = data; update; update = update->next) {
1260 int new_nid = update->new_nid;
1261 if (cpu != update->cpu)
1264 unmap_cpu_from_node(cpu);
1265 map_cpu_to_node(cpu, new_nid);
1266 set_cpu_numa_node(cpu, new_nid);
1267 set_cpu_numa_mem(cpu, local_memory_node(new_nid));
1274 static int update_lookup_table(void *data)
1276 struct topology_update_data *update;
1282 * Upon topology update, the numa-cpu lookup table needs to be updated
1283 * for all threads in the core, including offline CPUs, to ensure that
1284 * future hotplug operations respect the cpu-to-node associativity
1287 for (update = data; update; update = update->next) {
1290 nid = update->new_nid;
1291 base = cpu_first_thread_sibling(update->cpu);
1293 for (j = 0; j < threads_per_core; j++) {
1294 update_numa_cpu_lookup_table(base + j, nid);
1302 * Update the node maps and sysfs entries for each cpu whose home node
1303 * has changed. Returns 1 when the topology has changed, and 0 otherwise.
1305 * cpus_locked says whether we already hold cpu_hotplug_lock.
1307 int numa_update_cpu_topology(bool cpus_locked)
1309 unsigned int cpu, sibling, changed = 0;
1310 struct topology_update_data *updates, *ud;
1311 cpumask_t updated_cpus;
1313 int weight, new_nid, i = 0;
1315 if (!prrn_enabled && !vphn_enabled && topology_inited)
1318 weight = cpumask_weight(&cpu_associativity_changes_mask);
1322 updates = kcalloc(weight, sizeof(*updates), GFP_KERNEL);
1326 cpumask_clear(&updated_cpus);
1328 for_each_cpu(cpu, &cpu_associativity_changes_mask) {
1330 * If siblings aren't flagged for changes, updates list
1331 * will be too short. Skip on this update and set for next
1334 if (!cpumask_subset(cpu_sibling_mask(cpu),
1335 &cpu_associativity_changes_mask)) {
1336 pr_info("Sibling bits not set for associativity "
1337 "change, cpu%d\n", cpu);
1338 cpumask_or(&cpu_associativity_changes_mask,
1339 &cpu_associativity_changes_mask,
1340 cpu_sibling_mask(cpu));
1341 cpu = cpu_last_thread_sibling(cpu);
1345 new_nid = find_and_online_cpu_nid(cpu);
1347 if (new_nid == numa_cpu_lookup_table[cpu]) {
1348 cpumask_andnot(&cpu_associativity_changes_mask,
1349 &cpu_associativity_changes_mask,
1350 cpu_sibling_mask(cpu));
1351 dbg("Assoc chg gives same node %d for cpu%d\n",
1353 cpu = cpu_last_thread_sibling(cpu);
1357 for_each_cpu(sibling, cpu_sibling_mask(cpu)) {
1359 ud->next = &updates[i];
1361 ud->new_nid = new_nid;
1362 ud->old_nid = numa_cpu_lookup_table[sibling];
1363 cpumask_set_cpu(sibling, &updated_cpus);
1365 cpu = cpu_last_thread_sibling(cpu);
1369 * Prevent processing of 'updates' from overflowing array
1370 * where last entry filled in a 'next' pointer.
1373 updates[i-1].next = NULL;
1375 pr_debug("Topology update for the following CPUs:\n");
1376 if (cpumask_weight(&updated_cpus)) {
1377 for (ud = &updates[0]; ud; ud = ud->next) {
1378 pr_debug("cpu %d moving from node %d "
1380 ud->old_nid, ud->new_nid);
1385 * In cases where we have nothing to update (because the updates list
1386 * is too short or because the new topology is same as the old one),
1387 * skip invoking update_cpu_topology() via stop-machine(). This is
1388 * necessary (and not just a fast-path optimization) since stop-machine
1389 * can end up electing a random CPU to run update_cpu_topology(), and
1390 * thus trick us into setting up incorrect cpu-node mappings (since
1391 * 'updates' is kzalloc()'ed).
1393 * And for the similar reason, we will skip all the following updating.
1395 if (!cpumask_weight(&updated_cpus))
1399 stop_machine_cpuslocked(update_cpu_topology, &updates[0],
1402 stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
1405 * Update the numa-cpu lookup table with the new mappings, even for
1406 * offline CPUs. It is best to perform this update from the stop-
1410 stop_machine_cpuslocked(update_lookup_table, &updates[0],
1411 cpumask_of(raw_smp_processor_id()));
1413 stop_machine(update_lookup_table, &updates[0],
1414 cpumask_of(raw_smp_processor_id()));
1416 for (ud = &updates[0]; ud; ud = ud->next) {
1417 unregister_cpu_under_node(ud->cpu, ud->old_nid);
1418 register_cpu_under_node(ud->cpu, ud->new_nid);
1420 dev = get_cpu_device(ud->cpu);
1422 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
1423 cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
1432 int arch_update_cpu_topology(void)
1434 return numa_update_cpu_topology(true);
1437 static void topology_work_fn(struct work_struct *work)
1439 rebuild_sched_domains();
1441 static DECLARE_WORK(topology_work, topology_work_fn);
1443 static void topology_schedule_update(void)
1445 schedule_work(&topology_work);
1448 static void topology_timer_fn(struct timer_list *unused)
1450 if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
1451 topology_schedule_update();
1452 else if (vphn_enabled) {
1453 if (update_cpu_associativity_changes_mask() > 0)
1454 topology_schedule_update();
1455 reset_topology_timer();
1458 static struct timer_list topology_timer;
1460 static void reset_topology_timer(void)
1463 mod_timer(&topology_timer, jiffies + topology_timer_secs * HZ);
1468 static int dt_update_callback(struct notifier_block *nb,
1469 unsigned long action, void *data)
1471 struct of_reconfig_data *update = data;
1472 int rc = NOTIFY_DONE;
1475 case OF_RECONFIG_UPDATE_PROPERTY:
1476 if (of_node_is_type(update->dn, "cpu") &&
1477 !of_prop_cmp(update->prop->name, "ibm,associativity")) {
1479 of_property_read_u32(update->dn, "reg", &core_id);
1480 rc = dlpar_cpu_readd(core_id);
1489 static struct notifier_block dt_update_nb = {
1490 .notifier_call = dt_update_callback,
1496 * Start polling for associativity changes.
1498 int start_topology_update(void)
1502 if (!topology_updates_enabled)
1505 if (firmware_has_feature(FW_FEATURE_PRRN)) {
1506 if (!prrn_enabled) {
1509 rc = of_reconfig_notifier_register(&dt_update_nb);
1513 if (firmware_has_feature(FW_FEATURE_VPHN) &&
1514 lppaca_shared_proc(get_lppaca())) {
1515 if (!vphn_enabled) {
1517 setup_cpu_associativity_change_counters();
1518 timer_setup(&topology_timer, topology_timer_fn,
1520 reset_topology_timer();
1524 pr_info("Starting topology update%s%s\n",
1525 (prrn_enabled ? " prrn_enabled" : ""),
1526 (vphn_enabled ? " vphn_enabled" : ""));
1532 * Disable polling for VPHN associativity changes.
1534 int stop_topology_update(void)
1538 if (!topology_updates_enabled)
1544 rc = of_reconfig_notifier_unregister(&dt_update_nb);
1549 rc = del_timer_sync(&topology_timer);
1552 pr_info("Stopping topology update\n");
1557 int prrn_is_enabled(void)
1559 return prrn_enabled;
1562 void __init shared_proc_topology_init(void)
1564 if (lppaca_shared_proc(get_lppaca())) {
1565 bitmap_fill(cpumask_bits(&cpu_associativity_changes_mask),
1567 numa_update_cpu_topology(false);
1571 static int topology_read(struct seq_file *file, void *v)
1573 if (vphn_enabled || prrn_enabled)
1574 seq_puts(file, "on\n");
1576 seq_puts(file, "off\n");
1581 static int topology_open(struct inode *inode, struct file *file)
1583 return single_open(file, topology_read, NULL);
1586 static ssize_t topology_write(struct file *file, const char __user *buf,
1587 size_t count, loff_t *off)
1589 char kbuf[4]; /* "on" or "off" plus null. */
1592 read_len = count < 3 ? count : 3;
1593 if (copy_from_user(kbuf, buf, read_len))
1596 kbuf[read_len] = '\0';
1598 if (!strncmp(kbuf, "on", 2)) {
1599 topology_updates_enabled = true;
1600 start_topology_update();
1601 } else if (!strncmp(kbuf, "off", 3)) {
1602 stop_topology_update();
1603 topology_updates_enabled = false;
1610 static const struct file_operations topology_ops = {
1612 .write = topology_write,
1613 .open = topology_open,
1614 .release = single_release
1617 static int topology_update_init(void)
1619 start_topology_update();
1622 topology_schedule_update();
1624 if (!proc_create("powerpc/topology_updates", 0644, NULL, &topology_ops))
1627 topology_inited = 1;
1630 device_initcall(topology_update_init);
1631 #endif /* CONFIG_PPC_SPLPAR */