xref: /linux/arch/powerpc/mm/numa.c (revision e62520b8870837440262057f6573c231cd700116)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ab1f9dacSPaul Mackerras /*
3ab1f9dacSPaul Mackerras  * pSeries NUMA support
4ab1f9dacSPaul Mackerras  *
5ab1f9dacSPaul Mackerras  * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
6ab1f9dacSPaul Mackerras  */
72d73bae1SNishanth Aravamudan #define pr_fmt(fmt) "numa: " fmt
82d73bae1SNishanth Aravamudan 
9ab1f9dacSPaul Mackerras #include <linux/threads.h>
1057c8a661SMike Rapoport #include <linux/memblock.h>
11ab1f9dacSPaul Mackerras #include <linux/init.h>
12ab1f9dacSPaul Mackerras #include <linux/mm.h>
13ab1f9dacSPaul Mackerras #include <linux/mmzone.h>
144b16f8e2SPaul Gortmaker #include <linux/export.h>
15ab1f9dacSPaul Mackerras #include <linux/nodemask.h>
16ab1f9dacSPaul Mackerras #include <linux/cpu.h>
17ab1f9dacSPaul Mackerras #include <linux/notifier.h>
186df1646eSMichael Ellerman #include <linux/of.h>
1906eccea6SDave Hansen #include <linux/pfn.h>
209eff1a38SJesse Larrew #include <linux/cpuset.h>
219eff1a38SJesse Larrew #include <linux/node.h>
2230c05350SNathan Fontenot #include <linux/stop_machine.h>
23e04fa612SNathan Fontenot #include <linux/proc_fs.h>
24e04fa612SNathan Fontenot #include <linux/seq_file.h>
25e04fa612SNathan Fontenot #include <linux/uaccess.h>
26191a7120SLinus Torvalds #include <linux/slab.h>
273be7db6aSRobert Jennings #include <asm/cputhreads.h>
2845fb6ceaSAnton Blanchard #include <asm/sparsemem.h>
29d9b2b2a2SDavid S. Miller #include <asm/prom.h>
302249ca9dSPaul Mackerras #include <asm/smp.h>
31d4edc5b6SSrivatsa S. Bhat #include <asm/topology.h>
329eff1a38SJesse Larrew #include <asm/firmware.h>
339eff1a38SJesse Larrew #include <asm/paca.h>
3439bf990eSJesse Larrew #include <asm/hvcall.h>
35ae3a197eSDavid Howells #include <asm/setup.h>
36176bbf14SJesse Larrew #include <asm/vdso.h>
37514a9cb3SNathan Fontenot #include <asm/drmem.h>
38ab1f9dacSPaul Mackerras 
39ab1f9dacSPaul Mackerras static int numa_enabled = 1;
40ab1f9dacSPaul Mackerras 
411daa6d08SBalbir Singh static char *cmdline __initdata;
421daa6d08SBalbir Singh 
4345fb6ceaSAnton Blanchard int numa_cpu_lookup_table[NR_CPUS];
4425863de0SAnton Blanchard cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
45ab1f9dacSPaul Mackerras struct pglist_data *node_data[MAX_NUMNODES];
4645fb6ceaSAnton Blanchard 
4745fb6ceaSAnton Blanchard EXPORT_SYMBOL(numa_cpu_lookup_table);
4825863de0SAnton Blanchard EXPORT_SYMBOL(node_to_cpumask_map);
4945fb6ceaSAnton Blanchard EXPORT_SYMBOL(node_data);
5045fb6ceaSAnton Blanchard 
517e35ef66SAneesh Kumar K.V static int primary_domain_index;
52237a0989SMike Kravetz static int n_mem_addr_cells, n_mem_size_cells;
530eacd06bSAneesh Kumar K.V 
540eacd06bSAneesh Kumar K.V #define FORM0_AFFINITY 0
550eacd06bSAneesh Kumar K.V #define FORM1_AFFINITY 1
561c6b5a7eSAneesh Kumar K.V #define FORM2_AFFINITY 2
570eacd06bSAneesh Kumar K.V static int affinity_form;
5841eab6f8SAnton Blanchard 
5941eab6f8SAnton Blanchard #define MAX_DISTANCE_REF_POINTS 4
6041eab6f8SAnton Blanchard static int distance_ref_points_depth;
61b08a2a12SAlistair Popple static const __be32 *distance_ref_points;
6241eab6f8SAnton Blanchard static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
631c6b5a7eSAneesh Kumar K.V static int numa_distance_table[MAX_NUMNODES][MAX_NUMNODES] = {
641c6b5a7eSAneesh Kumar K.V 	[0 ... MAX_NUMNODES - 1] = { [0 ... MAX_NUMNODES - 1] = -1 }
651c6b5a7eSAneesh Kumar K.V };
661c6b5a7eSAneesh Kumar K.V static int numa_id_index_table[MAX_NUMNODES] = { [0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE };
67ab1f9dacSPaul Mackerras 
6825863de0SAnton Blanchard /*
6925863de0SAnton Blanchard  * Allocate node_to_cpumask_map based on number of available nodes
7025863de0SAnton Blanchard  * Requires node_possible_map to be valid.
7125863de0SAnton Blanchard  *
729512938bSWanlong Gao  * Note: cpumask_of_node() is not valid until after this is done.
7325863de0SAnton Blanchard  */
7425863de0SAnton Blanchard static void __init setup_node_to_cpumask_map(void)
7525863de0SAnton Blanchard {
76f9d531b8SCody P Schafer 	unsigned int node;
7725863de0SAnton Blanchard 
7825863de0SAnton Blanchard 	/* setup nr_node_ids if not done yet */
79f9d531b8SCody P Schafer 	if (nr_node_ids == MAX_NUMNODES)
80f9d531b8SCody P Schafer 		setup_nr_node_ids();
8125863de0SAnton Blanchard 
8225863de0SAnton Blanchard 	/* allocate the map */
83c118baf8SRaghavendra K T 	for_each_node(node)
8425863de0SAnton Blanchard 		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
8525863de0SAnton Blanchard 
8625863de0SAnton Blanchard 	/* cpumask_of_node() will now work */
87544af642SSrikar Dronamraju 	pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
8825863de0SAnton Blanchard }
8925863de0SAnton Blanchard 
9055671f3cSStephen Rothwell static int __init fake_numa_create_new_node(unsigned long end_pfn,
911daa6d08SBalbir Singh 						unsigned int *nid)
921daa6d08SBalbir Singh {
931daa6d08SBalbir Singh 	unsigned long long mem;
941daa6d08SBalbir Singh 	char *p = cmdline;
951daa6d08SBalbir Singh 	static unsigned int fake_nid;
961daa6d08SBalbir Singh 	static unsigned long long curr_boundary;
971daa6d08SBalbir Singh 
981daa6d08SBalbir Singh 	/*
991daa6d08SBalbir Singh 	 * Modify node id, iff we started creating NUMA nodes
1001daa6d08SBalbir Singh 	 * We want to continue from where we left of the last time
1011daa6d08SBalbir Singh 	 */
1021daa6d08SBalbir Singh 	if (fake_nid)
1031daa6d08SBalbir Singh 		*nid = fake_nid;
1041daa6d08SBalbir Singh 	/*
1051daa6d08SBalbir Singh 	 * In case there are no more arguments to parse, the
1061daa6d08SBalbir Singh 	 * node_id should be the same as the last fake node id
1071daa6d08SBalbir Singh 	 * (we've handled this above).
1081daa6d08SBalbir Singh 	 */
1091daa6d08SBalbir Singh 	if (!p)
1101daa6d08SBalbir Singh 		return 0;
1111daa6d08SBalbir Singh 
1121daa6d08SBalbir Singh 	mem = memparse(p, &p);
1131daa6d08SBalbir Singh 	if (!mem)
1141daa6d08SBalbir Singh 		return 0;
1151daa6d08SBalbir Singh 
1161daa6d08SBalbir Singh 	if (mem < curr_boundary)
1171daa6d08SBalbir Singh 		return 0;
1181daa6d08SBalbir Singh 
1191daa6d08SBalbir Singh 	curr_boundary = mem;
1201daa6d08SBalbir Singh 
1211daa6d08SBalbir Singh 	if ((end_pfn << PAGE_SHIFT) > mem) {
1221daa6d08SBalbir Singh 		/*
1231daa6d08SBalbir Singh 		 * Skip commas and spaces
1241daa6d08SBalbir Singh 		 */
1251daa6d08SBalbir Singh 		while (*p == ',' || *p == ' ' || *p == '\t')
1261daa6d08SBalbir Singh 			p++;
1271daa6d08SBalbir Singh 
1281daa6d08SBalbir Singh 		cmdline = p;
1291daa6d08SBalbir Singh 		fake_nid++;
1301daa6d08SBalbir Singh 		*nid = fake_nid;
131544af642SSrikar Dronamraju 		pr_debug("created new fake_node with id %d\n", fake_nid);
1321daa6d08SBalbir Singh 		return 1;
1331daa6d08SBalbir Singh 	}
1341daa6d08SBalbir Singh 	return 0;
1351daa6d08SBalbir Singh }
1361daa6d08SBalbir Singh 
137c13f2b2bSNick Child static void __init reset_numa_cpu_lookup_table(void)
138d4edc5b6SSrivatsa S. Bhat {
139d4edc5b6SSrivatsa S. Bhat 	unsigned int cpu;
140d4edc5b6SSrivatsa S. Bhat 
141d4edc5b6SSrivatsa S. Bhat 	for_each_possible_cpu(cpu)
142d4edc5b6SSrivatsa S. Bhat 		numa_cpu_lookup_table[cpu] = -1;
143d4edc5b6SSrivatsa S. Bhat }
144d4edc5b6SSrivatsa S. Bhat 
1459a245d0eSSrikar Dronamraju void map_cpu_to_node(int cpu, int node)
146d4edc5b6SSrivatsa S. Bhat {
147d4edc5b6SSrivatsa S. Bhat 	update_numa_cpu_lookup_table(cpu, node);
14845fb6ceaSAnton Blanchard 
149544a09eeSSrikar Dronamraju 	if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node]))) {
150544af642SSrikar Dronamraju 		pr_debug("adding cpu %d to node %d\n", cpu, node);
15125863de0SAnton Blanchard 		cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
152ab1f9dacSPaul Mackerras 	}
153544a09eeSSrikar Dronamraju }
154ab1f9dacSPaul Mackerras 
15539bf990eSJesse Larrew #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
1569a245d0eSSrikar Dronamraju void unmap_cpu_from_node(unsigned long cpu)
157ab1f9dacSPaul Mackerras {
158ab1f9dacSPaul Mackerras 	int node = numa_cpu_lookup_table[cpu];
159ab1f9dacSPaul Mackerras 
16025863de0SAnton Blanchard 	if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
161429f4d8dSAnton Blanchard 		cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
162544a09eeSSrikar Dronamraju 		pr_debug("removing cpu %lu from node %d\n", cpu, node);
163ab1f9dacSPaul Mackerras 	} else {
164506c2075SSrikar Dronamraju 		pr_warn("Warning: cpu %lu not found in node %d\n", cpu, node);
165ab1f9dacSPaul Mackerras 	}
166ab1f9dacSPaul Mackerras }
16739bf990eSJesse Larrew #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
168ab1f9dacSPaul Mackerras 
1698ddc6448SAneesh Kumar K.V static int __associativity_to_nid(const __be32 *associativity,
1708ddc6448SAneesh Kumar K.V 				  int max_array_sz)
17141eab6f8SAnton Blanchard {
1728ddc6448SAneesh Kumar K.V 	int nid;
1738ddc6448SAneesh Kumar K.V 	/*
1748ddc6448SAneesh Kumar K.V 	 * primary_domain_index is 1 based array index.
1758ddc6448SAneesh Kumar K.V 	 */
1768ddc6448SAneesh Kumar K.V 	int index = primary_domain_index  - 1;
17741eab6f8SAnton Blanchard 
1788ddc6448SAneesh Kumar K.V 	if (!numa_enabled || index >= max_array_sz)
1798ddc6448SAneesh Kumar K.V 		return NUMA_NO_NODE;
18041eab6f8SAnton Blanchard 
1818ddc6448SAneesh Kumar K.V 	nid = of_read_number(&associativity[index], 1);
182b08a2a12SAlistair Popple 
1838ddc6448SAneesh Kumar K.V 	/* POWER4 LPAR uses 0xffff as invalid node */
1848ddc6448SAneesh Kumar K.V 	if (nid == 0xffff || nid >= nr_node_ids)
1858ddc6448SAneesh Kumar K.V 		nid = NUMA_NO_NODE;
1868ddc6448SAneesh Kumar K.V 	return nid;
18741eab6f8SAnton Blanchard }
188dbce4562SSrikar Dronamraju /*
189dbce4562SSrikar Dronamraju  * Returns nid in the range [0..nr_node_ids], or -1 if no useful NUMA
190482ec7c4SNathan Lynch  * info is found.
191482ec7c4SNathan Lynch  */
192b08a2a12SAlistair Popple static int associativity_to_nid(const __be32 *associativity)
193ab1f9dacSPaul Mackerras {
1948ddc6448SAneesh Kumar K.V 	int array_sz = of_read_number(associativity, 1);
195ab1f9dacSPaul Mackerras 
1968ddc6448SAneesh Kumar K.V 	/* Skip the first element in the associativity array */
1978ddc6448SAneesh Kumar K.V 	return __associativity_to_nid((associativity + 1), array_sz);
198ab1f9dacSPaul Mackerras }
199ab1f9dacSPaul Mackerras 
2001c6b5a7eSAneesh Kumar K.V static int __cpu_form2_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
2011c6b5a7eSAneesh Kumar K.V {
2021c6b5a7eSAneesh Kumar K.V 	int dist;
2031c6b5a7eSAneesh Kumar K.V 	int node1, node2;
2041c6b5a7eSAneesh Kumar K.V 
2051c6b5a7eSAneesh Kumar K.V 	node1 = associativity_to_nid(cpu1_assoc);
2061c6b5a7eSAneesh Kumar K.V 	node2 = associativity_to_nid(cpu2_assoc);
2071c6b5a7eSAneesh Kumar K.V 
2081c6b5a7eSAneesh Kumar K.V 	dist = numa_distance_table[node1][node2];
2091c6b5a7eSAneesh Kumar K.V 	if (dist <= LOCAL_DISTANCE)
2101c6b5a7eSAneesh Kumar K.V 		return 0;
2111c6b5a7eSAneesh Kumar K.V 	else if (dist <= REMOTE_DISTANCE)
2121c6b5a7eSAneesh Kumar K.V 		return 1;
2131c6b5a7eSAneesh Kumar K.V 	else
2141c6b5a7eSAneesh Kumar K.V 		return 2;
2151c6b5a7eSAneesh Kumar K.V }
2161c6b5a7eSAneesh Kumar K.V 
2171c6b5a7eSAneesh Kumar K.V static int __cpu_form1_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
2181c6b5a7eSAneesh Kumar K.V {
2191c6b5a7eSAneesh Kumar K.V 	int dist = 0;
2201c6b5a7eSAneesh Kumar K.V 
2211c6b5a7eSAneesh Kumar K.V 	int i, index;
2221c6b5a7eSAneesh Kumar K.V 
2231c6b5a7eSAneesh Kumar K.V 	for (i = 0; i < distance_ref_points_depth; i++) {
2241c6b5a7eSAneesh Kumar K.V 		index = be32_to_cpu(distance_ref_points[i]);
2251c6b5a7eSAneesh Kumar K.V 		if (cpu1_assoc[index] == cpu2_assoc[index])
2261c6b5a7eSAneesh Kumar K.V 			break;
2271c6b5a7eSAneesh Kumar K.V 		dist++;
2281c6b5a7eSAneesh Kumar K.V 	}
2291c6b5a7eSAneesh Kumar K.V 
2301c6b5a7eSAneesh Kumar K.V 	return dist;
2311c6b5a7eSAneesh Kumar K.V }
2321c6b5a7eSAneesh Kumar K.V 
2331c6b5a7eSAneesh Kumar K.V int cpu_relative_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
2341c6b5a7eSAneesh Kumar K.V {
2351c6b5a7eSAneesh Kumar K.V 	/* We should not get called with FORM0 */
2361c6b5a7eSAneesh Kumar K.V 	VM_WARN_ON(affinity_form == FORM0_AFFINITY);
2371c6b5a7eSAneesh Kumar K.V 	if (affinity_form == FORM1_AFFINITY)
2381c6b5a7eSAneesh Kumar K.V 		return __cpu_form1_relative_distance(cpu1_assoc, cpu2_assoc);
2391c6b5a7eSAneesh Kumar K.V 	return __cpu_form2_relative_distance(cpu1_assoc, cpu2_assoc);
2401c6b5a7eSAneesh Kumar K.V }
2411c6b5a7eSAneesh Kumar K.V 
2421c6b5a7eSAneesh Kumar K.V /* must hold reference to node during call */
2431c6b5a7eSAneesh Kumar K.V static const __be32 *of_get_associativity(struct device_node *dev)
2441c6b5a7eSAneesh Kumar K.V {
2451c6b5a7eSAneesh Kumar K.V 	return of_get_property(dev, "ibm,associativity", NULL);
2461c6b5a7eSAneesh Kumar K.V }
2471c6b5a7eSAneesh Kumar K.V 
2481c6b5a7eSAneesh Kumar K.V int __node_distance(int a, int b)
2491c6b5a7eSAneesh Kumar K.V {
2501c6b5a7eSAneesh Kumar K.V 	int i;
2511c6b5a7eSAneesh Kumar K.V 	int distance = LOCAL_DISTANCE;
2521c6b5a7eSAneesh Kumar K.V 
2531c6b5a7eSAneesh Kumar K.V 	if (affinity_form == FORM2_AFFINITY)
2541c6b5a7eSAneesh Kumar K.V 		return numa_distance_table[a][b];
2551c6b5a7eSAneesh Kumar K.V 	else if (affinity_form == FORM0_AFFINITY)
2561c6b5a7eSAneesh Kumar K.V 		return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
2571c6b5a7eSAneesh Kumar K.V 
2581c6b5a7eSAneesh Kumar K.V 	for (i = 0; i < distance_ref_points_depth; i++) {
2591c6b5a7eSAneesh Kumar K.V 		if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
2601c6b5a7eSAneesh Kumar K.V 			break;
2611c6b5a7eSAneesh Kumar K.V 
2621c6b5a7eSAneesh Kumar K.V 		/* Double the distance for each NUMA level */
2631c6b5a7eSAneesh Kumar K.V 		distance *= 2;
2641c6b5a7eSAneesh Kumar K.V 	}
2651c6b5a7eSAneesh Kumar K.V 
2661c6b5a7eSAneesh Kumar K.V 	return distance;
2671c6b5a7eSAneesh Kumar K.V }
2681c6b5a7eSAneesh Kumar K.V EXPORT_SYMBOL(__node_distance);
2691c6b5a7eSAneesh Kumar K.V 
2709eff1a38SJesse Larrew /* Returns the nid associated with the given device tree node,
2719eff1a38SJesse Larrew  * or -1 if not found.
2729eff1a38SJesse Larrew  */
2739eff1a38SJesse Larrew static int of_node_to_nid_single(struct device_node *device)
2749eff1a38SJesse Larrew {
27598fa15f3SAnshuman Khandual 	int nid = NUMA_NO_NODE;
276b08a2a12SAlistair Popple 	const __be32 *tmp;
2779eff1a38SJesse Larrew 
2789eff1a38SJesse Larrew 	tmp = of_get_associativity(device);
2799eff1a38SJesse Larrew 	if (tmp)
2809eff1a38SJesse Larrew 		nid = associativity_to_nid(tmp);
2819eff1a38SJesse Larrew 	return nid;
2829eff1a38SJesse Larrew }
2839eff1a38SJesse Larrew 
284953039c8SJeremy Kerr /* Walk the device tree upwards, looking for an associativity id */
285953039c8SJeremy Kerr int of_node_to_nid(struct device_node *device)
286953039c8SJeremy Kerr {
28798fa15f3SAnshuman Khandual 	int nid = NUMA_NO_NODE;
288953039c8SJeremy Kerr 
289953039c8SJeremy Kerr 	of_node_get(device);
290953039c8SJeremy Kerr 	while (device) {
291953039c8SJeremy Kerr 		nid = of_node_to_nid_single(device);
292953039c8SJeremy Kerr 		if (nid != -1)
293953039c8SJeremy Kerr 			break;
294953039c8SJeremy Kerr 
2951def3758SChristophe Jaillet 		device = of_get_next_parent(device);
296953039c8SJeremy Kerr 	}
297953039c8SJeremy Kerr 	of_node_put(device);
298953039c8SJeremy Kerr 
299953039c8SJeremy Kerr 	return nid;
300953039c8SJeremy Kerr }
301be9ba9ffSShailendra Singh EXPORT_SYMBOL(of_node_to_nid);
302953039c8SJeremy Kerr 
3038ddc6448SAneesh Kumar K.V static void __initialize_form1_numa_distance(const __be32 *associativity,
3048ddc6448SAneesh Kumar K.V 					     int max_array_sz)
3058ddc6448SAneesh Kumar K.V {
3068ddc6448SAneesh Kumar K.V 	int i, nid;
3078ddc6448SAneesh Kumar K.V 
3088ddc6448SAneesh Kumar K.V 	if (affinity_form != FORM1_AFFINITY)
3098ddc6448SAneesh Kumar K.V 		return;
3108ddc6448SAneesh Kumar K.V 
3118ddc6448SAneesh Kumar K.V 	nid = __associativity_to_nid(associativity, max_array_sz);
3128ddc6448SAneesh Kumar K.V 	if (nid != NUMA_NO_NODE) {
3138ddc6448SAneesh Kumar K.V 		for (i = 0; i < distance_ref_points_depth; i++) {
3148ddc6448SAneesh Kumar K.V 			const __be32 *entry;
3158ddc6448SAneesh Kumar K.V 			int index = be32_to_cpu(distance_ref_points[i]) - 1;
3168ddc6448SAneesh Kumar K.V 
3178ddc6448SAneesh Kumar K.V 			/*
3188ddc6448SAneesh Kumar K.V 			 * broken hierarchy, return with broken distance table
3198ddc6448SAneesh Kumar K.V 			 */
3208ddc6448SAneesh Kumar K.V 			if (WARN(index >= max_array_sz, "Broken ibm,associativity property"))
3218ddc6448SAneesh Kumar K.V 				return;
3228ddc6448SAneesh Kumar K.V 
3238ddc6448SAneesh Kumar K.V 			entry = &associativity[index];
3248ddc6448SAneesh Kumar K.V 			distance_lookup_table[nid][i] = of_read_number(entry, 1);
3258ddc6448SAneesh Kumar K.V 		}
3268ddc6448SAneesh Kumar K.V 	}
3278ddc6448SAneesh Kumar K.V }
3288ddc6448SAneesh Kumar K.V 
3298ddc6448SAneesh Kumar K.V static void initialize_form1_numa_distance(const __be32 *associativity)
3308ddc6448SAneesh Kumar K.V {
3318ddc6448SAneesh Kumar K.V 	int array_sz;
3328ddc6448SAneesh Kumar K.V 
3338ddc6448SAneesh Kumar K.V 	array_sz = of_read_number(associativity, 1);
3348ddc6448SAneesh Kumar K.V 	/* Skip the first element in the associativity array */
3358ddc6448SAneesh Kumar K.V 	__initialize_form1_numa_distance(associativity + 1, array_sz);
3368ddc6448SAneesh Kumar K.V }
3378ddc6448SAneesh Kumar K.V 
3388ddc6448SAneesh Kumar K.V /*
3398ddc6448SAneesh Kumar K.V  * Used to update distance information w.r.t newly added node.
3408ddc6448SAneesh Kumar K.V  */
3418ddc6448SAneesh Kumar K.V void update_numa_distance(struct device_node *node)
3428ddc6448SAneesh Kumar K.V {
3431c6b5a7eSAneesh Kumar K.V 	int nid;
3441c6b5a7eSAneesh Kumar K.V 
3458ddc6448SAneesh Kumar K.V 	if (affinity_form == FORM0_AFFINITY)
3468ddc6448SAneesh Kumar K.V 		return;
3478ddc6448SAneesh Kumar K.V 	else if (affinity_form == FORM1_AFFINITY) {
3488ddc6448SAneesh Kumar K.V 		const __be32 *associativity;
3498ddc6448SAneesh Kumar K.V 
3508ddc6448SAneesh Kumar K.V 		associativity = of_get_associativity(node);
3518ddc6448SAneesh Kumar K.V 		if (!associativity)
3528ddc6448SAneesh Kumar K.V 			return;
3538ddc6448SAneesh Kumar K.V 
3548ddc6448SAneesh Kumar K.V 		initialize_form1_numa_distance(associativity);
3558ddc6448SAneesh Kumar K.V 		return;
3568ddc6448SAneesh Kumar K.V 	}
3571c6b5a7eSAneesh Kumar K.V 
3581c6b5a7eSAneesh Kumar K.V 	/* FORM2 affinity  */
3591c6b5a7eSAneesh Kumar K.V 	nid = of_node_to_nid_single(node);
3601c6b5a7eSAneesh Kumar K.V 	if (nid == NUMA_NO_NODE)
3611c6b5a7eSAneesh Kumar K.V 		return;
3621c6b5a7eSAneesh Kumar K.V 
3631c6b5a7eSAneesh Kumar K.V 	/*
3641c6b5a7eSAneesh Kumar K.V 	 * With FORM2 we expect NUMA distance of all possible NUMA
3651c6b5a7eSAneesh Kumar K.V 	 * nodes to be provided during boot.
3661c6b5a7eSAneesh Kumar K.V 	 */
3671c6b5a7eSAneesh Kumar K.V 	WARN(numa_distance_table[nid][nid] == -1,
3681c6b5a7eSAneesh Kumar K.V 	     "NUMA distance details for node %d not provided\n", nid);
3691c6b5a7eSAneesh Kumar K.V }
3701c6b5a7eSAneesh Kumar K.V 
3711c6b5a7eSAneesh Kumar K.V /*
3721c6b5a7eSAneesh Kumar K.V  * ibm,numa-lookup-index-table= {N, domainid1, domainid2, ..... domainidN}
3731c6b5a7eSAneesh Kumar K.V  * ibm,numa-distance-table = { N, 1, 2, 4, 5, 1, 6, .... N elements}
3741c6b5a7eSAneesh Kumar K.V  */
375c13f2b2bSNick Child static void __init initialize_form2_numa_distance_lookup_table(void)
3761c6b5a7eSAneesh Kumar K.V {
3771c6b5a7eSAneesh Kumar K.V 	int i, j;
3781c6b5a7eSAneesh Kumar K.V 	struct device_node *root;
3790bd81274SNicholas Piggin 	const __u8 *form2_distances;
3801c6b5a7eSAneesh Kumar K.V 	const __be32 *numa_lookup_index;
3810bd81274SNicholas Piggin 	int form2_distances_length;
3821c6b5a7eSAneesh Kumar K.V 	int max_numa_index, distance_index;
3831c6b5a7eSAneesh Kumar K.V 
3841c6b5a7eSAneesh Kumar K.V 	if (firmware_has_feature(FW_FEATURE_OPAL))
3851c6b5a7eSAneesh Kumar K.V 		root = of_find_node_by_path("/ibm,opal");
3861c6b5a7eSAneesh Kumar K.V 	else
3871c6b5a7eSAneesh Kumar K.V 		root = of_find_node_by_path("/rtas");
3881c6b5a7eSAneesh Kumar K.V 	if (!root)
3891c6b5a7eSAneesh Kumar K.V 		root = of_find_node_by_path("/");
3901c6b5a7eSAneesh Kumar K.V 
3911c6b5a7eSAneesh Kumar K.V 	numa_lookup_index = of_get_property(root, "ibm,numa-lookup-index-table", NULL);
3921c6b5a7eSAneesh Kumar K.V 	max_numa_index = of_read_number(&numa_lookup_index[0], 1);
3931c6b5a7eSAneesh Kumar K.V 
3941c6b5a7eSAneesh Kumar K.V 	/* first element of the array is the size and is encode-int */
3950bd81274SNicholas Piggin 	form2_distances = of_get_property(root, "ibm,numa-distance-table", NULL);
3960bd81274SNicholas Piggin 	form2_distances_length = of_read_number((const __be32 *)&form2_distances[0], 1);
3971c6b5a7eSAneesh Kumar K.V 	/* Skip the size which is encoded int */
3980bd81274SNicholas Piggin 	form2_distances += sizeof(__be32);
3991c6b5a7eSAneesh Kumar K.V 
4000bd81274SNicholas Piggin 	pr_debug("form2_distances_len = %d, numa_dist_indexes_len = %d\n",
4010bd81274SNicholas Piggin 		 form2_distances_length, max_numa_index);
4021c6b5a7eSAneesh Kumar K.V 
4031c6b5a7eSAneesh Kumar K.V 	for (i = 0; i < max_numa_index; i++)
4041c6b5a7eSAneesh Kumar K.V 		/* +1 skip the max_numa_index in the property */
4051c6b5a7eSAneesh Kumar K.V 		numa_id_index_table[i] = of_read_number(&numa_lookup_index[i + 1], 1);
4061c6b5a7eSAneesh Kumar K.V 
4071c6b5a7eSAneesh Kumar K.V 
4080bd81274SNicholas Piggin 	if (form2_distances_length != max_numa_index * max_numa_index) {
4091c6b5a7eSAneesh Kumar K.V 		WARN(1, "Wrong NUMA distance information\n");
41030203946SNicholas Piggin 		form2_distances = NULL; // don't use it
4111c6b5a7eSAneesh Kumar K.V 	}
4121c6b5a7eSAneesh Kumar K.V 	distance_index = 0;
4131c6b5a7eSAneesh Kumar K.V 	for (i = 0;  i < max_numa_index; i++) {
4141c6b5a7eSAneesh Kumar K.V 		for (j = 0; j < max_numa_index; j++) {
4151c6b5a7eSAneesh Kumar K.V 			int nodeA = numa_id_index_table[i];
4161c6b5a7eSAneesh Kumar K.V 			int nodeB = numa_id_index_table[j];
41730203946SNicholas Piggin 			int dist;
4181c6b5a7eSAneesh Kumar K.V 
41930203946SNicholas Piggin 			if (form2_distances)
42030203946SNicholas Piggin 				dist = form2_distances[distance_index++];
42130203946SNicholas Piggin 			else if (nodeA == nodeB)
42230203946SNicholas Piggin 				dist = LOCAL_DISTANCE;
42330203946SNicholas Piggin 			else
42430203946SNicholas Piggin 				dist = REMOTE_DISTANCE;
42530203946SNicholas Piggin 			numa_distance_table[nodeA][nodeB] = dist;
42630203946SNicholas Piggin 			pr_debug("dist[%d][%d]=%d ", nodeA, nodeB, dist);
4271c6b5a7eSAneesh Kumar K.V 		}
4281c6b5a7eSAneesh Kumar K.V 	}
42930203946SNicholas Piggin 
4301c6b5a7eSAneesh Kumar K.V 	of_node_put(root);
4318ddc6448SAneesh Kumar K.V }
4328ddc6448SAneesh Kumar K.V 
4337e35ef66SAneesh Kumar K.V static int __init find_primary_domain_index(void)
434ab1f9dacSPaul Mackerras {
4357e35ef66SAneesh Kumar K.V 	int index;
436e70606ebSMichael Ellerman 	struct device_node *root;
437ab1f9dacSPaul Mackerras 
4380eacd06bSAneesh Kumar K.V 	/*
4390eacd06bSAneesh Kumar K.V 	 * Check for which form of affinity.
4400eacd06bSAneesh Kumar K.V 	 */
4410eacd06bSAneesh Kumar K.V 	if (firmware_has_feature(FW_FEATURE_OPAL)) {
4420eacd06bSAneesh Kumar K.V 		affinity_form = FORM1_AFFINITY;
4431c6b5a7eSAneesh Kumar K.V 	} else if (firmware_has_feature(FW_FEATURE_FORM2_AFFINITY)) {
444544af642SSrikar Dronamraju 		pr_debug("Using form 2 affinity\n");
4451c6b5a7eSAneesh Kumar K.V 		affinity_form = FORM2_AFFINITY;
4460eacd06bSAneesh Kumar K.V 	} else if (firmware_has_feature(FW_FEATURE_FORM1_AFFINITY)) {
447544af642SSrikar Dronamraju 		pr_debug("Using form 1 affinity\n");
4480eacd06bSAneesh Kumar K.V 		affinity_form = FORM1_AFFINITY;
4490eacd06bSAneesh Kumar K.V 	} else
4500eacd06bSAneesh Kumar K.V 		affinity_form = FORM0_AFFINITY;
4510eacd06bSAneesh Kumar K.V 
4521c8ee733SDipankar Sarma 	if (firmware_has_feature(FW_FEATURE_OPAL))
4531c8ee733SDipankar Sarma 		root = of_find_node_by_path("/ibm,opal");
4541c8ee733SDipankar Sarma 	else
455e70606ebSMichael Ellerman 		root = of_find_node_by_path("/rtas");
456e70606ebSMichael Ellerman 	if (!root)
457e70606ebSMichael Ellerman 		root = of_find_node_by_path("/");
458ab1f9dacSPaul Mackerras 
459ab1f9dacSPaul Mackerras 	/*
46041eab6f8SAnton Blanchard 	 * This property is a set of 32-bit integers, each representing
46141eab6f8SAnton Blanchard 	 * an index into the ibm,associativity nodes.
46241eab6f8SAnton Blanchard 	 *
46341eab6f8SAnton Blanchard 	 * With form 0 affinity the first integer is for an SMP configuration
46441eab6f8SAnton Blanchard 	 * (should be all 0's) and the second is for a normal NUMA
46541eab6f8SAnton Blanchard 	 * configuration. We have only one level of NUMA.
46641eab6f8SAnton Blanchard 	 *
46741eab6f8SAnton Blanchard 	 * With form 1 affinity the first integer is the most significant
46841eab6f8SAnton Blanchard 	 * NUMA boundary and the following are progressively less significant
46941eab6f8SAnton Blanchard 	 * boundaries. There can be more than one level of NUMA.
470ab1f9dacSPaul Mackerras 	 */
471e70606ebSMichael Ellerman 	distance_ref_points = of_get_property(root,
47241eab6f8SAnton Blanchard 					"ibm,associativity-reference-points",
47341eab6f8SAnton Blanchard 					&distance_ref_points_depth);
474ab1f9dacSPaul Mackerras 
47541eab6f8SAnton Blanchard 	if (!distance_ref_points) {
476506c2075SSrikar Dronamraju 		pr_debug("ibm,associativity-reference-points not found.\n");
47741eab6f8SAnton Blanchard 		goto err;
47841eab6f8SAnton Blanchard 	}
47941eab6f8SAnton Blanchard 
48041eab6f8SAnton Blanchard 	distance_ref_points_depth /= sizeof(int);
4810eacd06bSAneesh Kumar K.V 	if (affinity_form == FORM0_AFFINITY) {
48241eab6f8SAnton Blanchard 		if (distance_ref_points_depth < 2) {
483506c2075SSrikar Dronamraju 			pr_warn("short ibm,associativity-reference-points\n");
48441eab6f8SAnton Blanchard 			goto err;
485ab1f9dacSPaul Mackerras 		}
486ab1f9dacSPaul Mackerras 
4877e35ef66SAneesh Kumar K.V 		index = of_read_number(&distance_ref_points[1], 1);
4880eacd06bSAneesh Kumar K.V 	} else {
4891c6b5a7eSAneesh Kumar K.V 		/*
4901c6b5a7eSAneesh Kumar K.V 		 * Both FORM1 and FORM2 affinity find the primary domain details
4911c6b5a7eSAneesh Kumar K.V 		 * at the same offset.
4921c6b5a7eSAneesh Kumar K.V 		 */
4930eacd06bSAneesh Kumar K.V 		index = of_read_number(distance_ref_points, 1);
49441eab6f8SAnton Blanchard 	}
49541eab6f8SAnton Blanchard 	/*
49641eab6f8SAnton Blanchard 	 * Warn and cap if the hardware supports more than
49741eab6f8SAnton Blanchard 	 * MAX_DISTANCE_REF_POINTS domains.
49841eab6f8SAnton Blanchard 	 */
49941eab6f8SAnton Blanchard 	if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
500506c2075SSrikar Dronamraju 		pr_warn("distance array capped at %d entries\n",
501506c2075SSrikar Dronamraju 			MAX_DISTANCE_REF_POINTS);
50241eab6f8SAnton Blanchard 		distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
50341eab6f8SAnton Blanchard 	}
50441eab6f8SAnton Blanchard 
505e70606ebSMichael Ellerman 	of_node_put(root);
5067e35ef66SAneesh Kumar K.V 	return index;
50741eab6f8SAnton Blanchard 
50841eab6f8SAnton Blanchard err:
509e70606ebSMichael Ellerman 	of_node_put(root);
51041eab6f8SAnton Blanchard 	return -1;
511ab1f9dacSPaul Mackerras }
512ab1f9dacSPaul Mackerras 
51384c9fdd1SMike Kravetz static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
514ab1f9dacSPaul Mackerras {
515ab1f9dacSPaul Mackerras 	struct device_node *memory = NULL;
516ab1f9dacSPaul Mackerras 
517ab1f9dacSPaul Mackerras 	memory = of_find_node_by_type(memory, "memory");
51854c23310SPaul Mackerras 	if (!memory)
51984c9fdd1SMike Kravetz 		panic("numa.c: No memory nodes found!");
52054c23310SPaul Mackerras 
521a8bda5ddSStephen Rothwell 	*n_addr_cells = of_n_addr_cells(memory);
5229213feeaSStephen Rothwell 	*n_size_cells = of_n_size_cells(memory);
52384c9fdd1SMike Kravetz 	of_node_put(memory);
524ab1f9dacSPaul Mackerras }
525ab1f9dacSPaul Mackerras 
526b08a2a12SAlistair Popple static unsigned long read_n_cells(int n, const __be32 **buf)
527ab1f9dacSPaul Mackerras {
528ab1f9dacSPaul Mackerras 	unsigned long result = 0;
529ab1f9dacSPaul Mackerras 
530ab1f9dacSPaul Mackerras 	while (n--) {
531b08a2a12SAlistair Popple 		result = (result << 32) | of_read_number(*buf, 1);
532ab1f9dacSPaul Mackerras 		(*buf)++;
533ab1f9dacSPaul Mackerras 	}
534ab1f9dacSPaul Mackerras 	return result;
535ab1f9dacSPaul Mackerras }
536ab1f9dacSPaul Mackerras 
5378342681dSNathan Fontenot struct assoc_arrays {
5388342681dSNathan Fontenot 	u32	n_arrays;
5398342681dSNathan Fontenot 	u32	array_sz;
540b08a2a12SAlistair Popple 	const __be32 *arrays;
5418342681dSNathan Fontenot };
5428342681dSNathan Fontenot 
5438342681dSNathan Fontenot /*
54425985edcSLucas De Marchi  * Retrieve and validate the list of associativity arrays for drconf
5458342681dSNathan Fontenot  * memory from the ibm,associativity-lookup-arrays property of the
5468342681dSNathan Fontenot  * device tree..
5478342681dSNathan Fontenot  *
5488342681dSNathan Fontenot  * The layout of the ibm,associativity-lookup-arrays property is a number N
5498342681dSNathan Fontenot  * indicating the number of associativity arrays, followed by a number M
5508342681dSNathan Fontenot  * indicating the size of each associativity array, followed by a list
5518342681dSNathan Fontenot  * of N associativity arrays.
5528342681dSNathan Fontenot  */
55335f80debSNathan Fontenot static int of_get_assoc_arrays(struct assoc_arrays *aa)
5548342681dSNathan Fontenot {
55535f80debSNathan Fontenot 	struct device_node *memory;
556b08a2a12SAlistair Popple 	const __be32 *prop;
5578342681dSNathan Fontenot 	u32 len;
5588342681dSNathan Fontenot 
55935f80debSNathan Fontenot 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
56035f80debSNathan Fontenot 	if (!memory)
5618342681dSNathan Fontenot 		return -1;
5628342681dSNathan Fontenot 
56335f80debSNathan Fontenot 	prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
56435f80debSNathan Fontenot 	if (!prop || len < 2 * sizeof(unsigned int)) {
56535f80debSNathan Fontenot 		of_node_put(memory);
56635f80debSNathan Fontenot 		return -1;
56735f80debSNathan Fontenot 	}
56835f80debSNathan Fontenot 
569b08a2a12SAlistair Popple 	aa->n_arrays = of_read_number(prop++, 1);
570b08a2a12SAlistair Popple 	aa->array_sz = of_read_number(prop++, 1);
5718342681dSNathan Fontenot 
57235f80debSNathan Fontenot 	of_node_put(memory);
57335f80debSNathan Fontenot 
57442b2aa86SJustin P. Mattock 	/* Now that we know the number of arrays and size of each array,
5758342681dSNathan Fontenot 	 * revalidate the size of the property read in.
5768342681dSNathan Fontenot 	 */
5778342681dSNathan Fontenot 	if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
5788342681dSNathan Fontenot 		return -1;
5798342681dSNathan Fontenot 
5808342681dSNathan Fontenot 	aa->arrays = prop;
5818342681dSNathan Fontenot 	return 0;
5828342681dSNathan Fontenot }
5838342681dSNathan Fontenot 
584c13f2b2bSNick Child static int __init get_nid_and_numa_distance(struct drmem_lmb *lmb)
5858ddc6448SAneesh Kumar K.V {
5868ddc6448SAneesh Kumar K.V 	struct assoc_arrays aa = { .arrays = NULL };
5878ddc6448SAneesh Kumar K.V 	int default_nid = NUMA_NO_NODE;
5888ddc6448SAneesh Kumar K.V 	int nid = default_nid;
5898ddc6448SAneesh Kumar K.V 	int rc, index;
5908ddc6448SAneesh Kumar K.V 
5918ddc6448SAneesh Kumar K.V 	if ((primary_domain_index < 0) || !numa_enabled)
5928ddc6448SAneesh Kumar K.V 		return default_nid;
5938ddc6448SAneesh Kumar K.V 
5948ddc6448SAneesh Kumar K.V 	rc = of_get_assoc_arrays(&aa);
5958ddc6448SAneesh Kumar K.V 	if (rc)
5968ddc6448SAneesh Kumar K.V 		return default_nid;
5978ddc6448SAneesh Kumar K.V 
5988ddc6448SAneesh Kumar K.V 	if (primary_domain_index <= aa.array_sz &&
5998ddc6448SAneesh Kumar K.V 	    !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
6008ddc6448SAneesh Kumar K.V 		const __be32 *associativity;
6018ddc6448SAneesh Kumar K.V 
6028ddc6448SAneesh Kumar K.V 		index = lmb->aa_index * aa.array_sz;
6038ddc6448SAneesh Kumar K.V 		associativity = &aa.arrays[index];
6048ddc6448SAneesh Kumar K.V 		nid = __associativity_to_nid(associativity, aa.array_sz);
6058ddc6448SAneesh Kumar K.V 		if (nid > 0 && affinity_form == FORM1_AFFINITY) {
6068ddc6448SAneesh Kumar K.V 			/*
6078ddc6448SAneesh Kumar K.V 			 * lookup array associativity entries have
6088ddc6448SAneesh Kumar K.V 			 * no length of the array as the first element.
6098ddc6448SAneesh Kumar K.V 			 */
6108ddc6448SAneesh Kumar K.V 			__initialize_form1_numa_distance(associativity, aa.array_sz);
6118ddc6448SAneesh Kumar K.V 		}
6128ddc6448SAneesh Kumar K.V 	}
6138ddc6448SAneesh Kumar K.V 	return nid;
6148ddc6448SAneesh Kumar K.V }
6158ddc6448SAneesh Kumar K.V 
6168342681dSNathan Fontenot /*
6178342681dSNathan Fontenot  * This is like of_node_to_nid_single() for memory represented in the
6188342681dSNathan Fontenot  * ibm,dynamic-reconfiguration-memory node.
6198342681dSNathan Fontenot  */
62072cdd117SScott Cheloha int of_drconf_to_nid_single(struct drmem_lmb *lmb)
6218342681dSNathan Fontenot {
622b88fc309SNathan Fontenot 	struct assoc_arrays aa = { .arrays = NULL };
623ea9f5b70SAneesh Kumar K.V 	int default_nid = NUMA_NO_NODE;
6248342681dSNathan Fontenot 	int nid = default_nid;
625b88fc309SNathan Fontenot 	int rc, index;
6268342681dSNathan Fontenot 
6277e35ef66SAneesh Kumar K.V 	if ((primary_domain_index < 0) || !numa_enabled)
628ea9f5b70SAneesh Kumar K.V 		return default_nid;
629ea9f5b70SAneesh Kumar K.V 
630b88fc309SNathan Fontenot 	rc = of_get_assoc_arrays(&aa);
631b88fc309SNathan Fontenot 	if (rc)
632b88fc309SNathan Fontenot 		return default_nid;
633b88fc309SNathan Fontenot 
6347e35ef66SAneesh Kumar K.V 	if (primary_domain_index <= aa.array_sz &&
635ea9f5b70SAneesh Kumar K.V 	    !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
6368ddc6448SAneesh Kumar K.V 		const __be32 *associativity;
6378342681dSNathan Fontenot 
638514a9cb3SNathan Fontenot 		index = lmb->aa_index * aa.array_sz;
6398ddc6448SAneesh Kumar K.V 		associativity = &aa.arrays[index];
6408ddc6448SAneesh Kumar K.V 		nid = __associativity_to_nid(associativity, aa.array_sz);
6411d805440SNikunj A Dadhania 	}
6428342681dSNathan Fontenot 	return nid;
6438342681dSNathan Fontenot }
6448342681dSNathan Fontenot 
645dc909d8bSSrikar Dronamraju #ifdef CONFIG_PPC_SPLPAR
6468ddc6448SAneesh Kumar K.V 
6478ddc6448SAneesh Kumar K.V static int __vphn_get_associativity(long lcpu, __be32 *associativity)
648dc909d8bSSrikar Dronamraju {
649dc909d8bSSrikar Dronamraju 	long rc, hwid;
650dc909d8bSSrikar Dronamraju 
651dc909d8bSSrikar Dronamraju 	/*
652dc909d8bSSrikar Dronamraju 	 * On a shared lpar, device tree will not have node associativity.
653dc909d8bSSrikar Dronamraju 	 * At this time lppaca, or its __old_status field may not be
654dc909d8bSSrikar Dronamraju 	 * updated. Hence kernel cannot detect if its on a shared lpar. So
655dc909d8bSSrikar Dronamraju 	 * request an explicit associativity irrespective of whether the
656dc909d8bSSrikar Dronamraju 	 * lpar is shared or dedicated. Use the device tree property as a
657dc909d8bSSrikar Dronamraju 	 * fallback. cpu_to_phys_id is only valid between
658dc909d8bSSrikar Dronamraju 	 * smp_setup_cpu_maps() and smp_setup_pacas().
659dc909d8bSSrikar Dronamraju 	 */
660dc909d8bSSrikar Dronamraju 	if (firmware_has_feature(FW_FEATURE_VPHN)) {
661dc909d8bSSrikar Dronamraju 		if (cpu_to_phys_id)
662dc909d8bSSrikar Dronamraju 			hwid = cpu_to_phys_id[lcpu];
663dc909d8bSSrikar Dronamraju 		else
664dc909d8bSSrikar Dronamraju 			hwid = get_hard_smp_processor_id(lcpu);
665dc909d8bSSrikar Dronamraju 
666dc909d8bSSrikar Dronamraju 		rc = hcall_vphn(hwid, VPHN_FLAG_VCPU, associativity);
667dc909d8bSSrikar Dronamraju 		if (rc == H_SUCCESS)
6688ddc6448SAneesh Kumar K.V 			return 0;
669dc909d8bSSrikar Dronamraju 	}
670dc909d8bSSrikar Dronamraju 
6718ddc6448SAneesh Kumar K.V 	return -1;
6728ddc6448SAneesh Kumar K.V }
6738ddc6448SAneesh Kumar K.V 
6748ddc6448SAneesh Kumar K.V static int vphn_get_nid(long lcpu)
6758ddc6448SAneesh Kumar K.V {
6768ddc6448SAneesh Kumar K.V 	__be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
6778ddc6448SAneesh Kumar K.V 
6788ddc6448SAneesh Kumar K.V 
6798ddc6448SAneesh Kumar K.V 	if (!__vphn_get_associativity(lcpu, associativity))
6808ddc6448SAneesh Kumar K.V 		return associativity_to_nid(associativity);
6818ddc6448SAneesh Kumar K.V 
682dc909d8bSSrikar Dronamraju 	return NUMA_NO_NODE;
6838ddc6448SAneesh Kumar K.V 
684dc909d8bSSrikar Dronamraju }
685dc909d8bSSrikar Dronamraju #else
6868ddc6448SAneesh Kumar K.V 
6878ddc6448SAneesh Kumar K.V static int __vphn_get_associativity(long lcpu, __be32 *associativity)
6888ddc6448SAneesh Kumar K.V {
6898ddc6448SAneesh Kumar K.V 	return -1;
6908ddc6448SAneesh Kumar K.V }
6918ddc6448SAneesh Kumar K.V 
692dc909d8bSSrikar Dronamraju static int vphn_get_nid(long unused)
693dc909d8bSSrikar Dronamraju {
694dc909d8bSSrikar Dronamraju 	return NUMA_NO_NODE;
695dc909d8bSSrikar Dronamraju }
696dc909d8bSSrikar Dronamraju #endif  /* CONFIG_PPC_SPLPAR */
697dc909d8bSSrikar Dronamraju 
698ab1f9dacSPaul Mackerras /*
699ab1f9dacSPaul Mackerras  * Figure out to which domain a cpu belongs and stick it there.
700ab1f9dacSPaul Mackerras  * Return the id of the domain used.
701ab1f9dacSPaul Mackerras  */
702061d19f2SPaul Gortmaker static int numa_setup_cpu(unsigned long lcpu)
703ab1f9dacSPaul Mackerras {
704d4edc5b6SSrivatsa S. Bhat 	struct device_node *cpu;
705413e4055SSrikar Dronamraju 	int fcpu = cpu_first_thread_sibling(lcpu);
706413e4055SSrikar Dronamraju 	int nid = NUMA_NO_NODE;
707d4edc5b6SSrivatsa S. Bhat 
708a874f100SSrikar Dronamraju 	if (!cpu_present(lcpu)) {
709a874f100SSrikar Dronamraju 		set_cpu_numa_node(lcpu, first_online_node);
710a874f100SSrikar Dronamraju 		return first_online_node;
711a874f100SSrikar Dronamraju 	}
712a874f100SSrikar Dronamraju 
713d4edc5b6SSrivatsa S. Bhat 	/*
714d4edc5b6SSrivatsa S. Bhat 	 * If a valid cpu-to-node mapping is already available, use it
715d4edc5b6SSrivatsa S. Bhat 	 * directly instead of querying the firmware, since it represents
716d4edc5b6SSrivatsa S. Bhat 	 * the most recent mapping notified to us by the platform (eg: VPHN).
717413e4055SSrikar Dronamraju 	 * Since cpu_to_node binding remains the same for all threads in the
718413e4055SSrikar Dronamraju 	 * core. If a valid cpu-to-node mapping is already available, for
719413e4055SSrikar Dronamraju 	 * the first thread in the core, use it.
720d4edc5b6SSrivatsa S. Bhat 	 */
721413e4055SSrikar Dronamraju 	nid = numa_cpu_lookup_table[fcpu];
722413e4055SSrikar Dronamraju 	if (nid >= 0) {
723d4edc5b6SSrivatsa S. Bhat 		map_cpu_to_node(lcpu, nid);
724d4edc5b6SSrivatsa S. Bhat 		return nid;
725d4edc5b6SSrivatsa S. Bhat 	}
726d4edc5b6SSrivatsa S. Bhat 
727dc909d8bSSrikar Dronamraju 	nid = vphn_get_nid(lcpu);
728dc909d8bSSrikar Dronamraju 	if (nid != NUMA_NO_NODE)
729dc909d8bSSrikar Dronamraju 		goto out_present;
730dc909d8bSSrikar Dronamraju 
731d4edc5b6SSrivatsa S. Bhat 	cpu = of_get_cpu_node(lcpu, NULL);
732ab1f9dacSPaul Mackerras 
733ab1f9dacSPaul Mackerras 	if (!cpu) {
734ab1f9dacSPaul Mackerras 		WARN_ON(1);
735297cf502SLi Zhong 		if (cpu_present(lcpu))
736297cf502SLi Zhong 			goto out_present;
737297cf502SLi Zhong 		else
738ab1f9dacSPaul Mackerras 			goto out;
739ab1f9dacSPaul Mackerras 	}
740ab1f9dacSPaul Mackerras 
741953039c8SJeremy Kerr 	nid = of_node_to_nid_single(cpu);
742dc909d8bSSrikar Dronamraju 	of_node_put(cpu);
743ab1f9dacSPaul Mackerras 
744297cf502SLi Zhong out_present:
745ea05ba7cSMichael Bringmann 	if (nid < 0 || !node_possible(nid))
74672c33688SH Hartley Sweeten 		nid = first_online_node;
747297cf502SLi Zhong 
748413e4055SSrikar Dronamraju 	/*
749413e4055SSrikar Dronamraju 	 * Update for the first thread of the core. All threads of a core
750413e4055SSrikar Dronamraju 	 * have to be part of the same node. This not only avoids querying
751413e4055SSrikar Dronamraju 	 * for every other thread in the core, but always avoids a case
752413e4055SSrikar Dronamraju 	 * where virtual node associativity change causes subsequent threads
753413e4055SSrikar Dronamraju 	 * of a core to be associated with different nid. However if first
754413e4055SSrikar Dronamraju 	 * thread is already online, expect it to have a valid mapping.
755413e4055SSrikar Dronamraju 	 */
756413e4055SSrikar Dronamraju 	if (fcpu != lcpu) {
757413e4055SSrikar Dronamraju 		WARN_ON(cpu_online(fcpu));
758413e4055SSrikar Dronamraju 		map_cpu_to_node(fcpu, nid);
759413e4055SSrikar Dronamraju 	}
760413e4055SSrikar Dronamraju 
761cf950b7aSNathan Lynch 	map_cpu_to_node(lcpu, nid);
762297cf502SLi Zhong out:
763cf950b7aSNathan Lynch 	return nid;
764ab1f9dacSPaul Mackerras }
765ab1f9dacSPaul Mackerras 
76668fb18aaSSrivatsa S. Bhat static void verify_cpu_node_mapping(int cpu, int node)
76768fb18aaSSrivatsa S. Bhat {
76868fb18aaSSrivatsa S. Bhat 	int base, sibling, i;
76968fb18aaSSrivatsa S. Bhat 
77068fb18aaSSrivatsa S. Bhat 	/* Verify that all the threads in the core belong to the same node */
77168fb18aaSSrivatsa S. Bhat 	base = cpu_first_thread_sibling(cpu);
77268fb18aaSSrivatsa S. Bhat 
77368fb18aaSSrivatsa S. Bhat 	for (i = 0; i < threads_per_core; i++) {
77468fb18aaSSrivatsa S. Bhat 		sibling = base + i;
77568fb18aaSSrivatsa S. Bhat 
77668fb18aaSSrivatsa S. Bhat 		if (sibling == cpu || cpu_is_offline(sibling))
77768fb18aaSSrivatsa S. Bhat 			continue;
77868fb18aaSSrivatsa S. Bhat 
77968fb18aaSSrivatsa S. Bhat 		if (cpu_to_node(sibling) != node) {
78068fb18aaSSrivatsa S. Bhat 			WARN(1, "CPU thread siblings %d and %d don't belong"
78168fb18aaSSrivatsa S. Bhat 				" to the same node!\n", cpu, sibling);
78268fb18aaSSrivatsa S. Bhat 			break;
78368fb18aaSSrivatsa S. Bhat 		}
78468fb18aaSSrivatsa S. Bhat 	}
78568fb18aaSSrivatsa S. Bhat }
78668fb18aaSSrivatsa S. Bhat 
787bdab88e0SSebastian Andrzej Siewior /* Must run before sched domains notifier. */
788bdab88e0SSebastian Andrzej Siewior static int ppc_numa_cpu_prepare(unsigned int cpu)
789ab1f9dacSPaul Mackerras {
790bdab88e0SSebastian Andrzej Siewior 	int nid;
791ab1f9dacSPaul Mackerras 
792bdab88e0SSebastian Andrzej Siewior 	nid = numa_setup_cpu(cpu);
793bdab88e0SSebastian Andrzej Siewior 	verify_cpu_node_mapping(cpu, nid);
794bdab88e0SSebastian Andrzej Siewior 	return 0;
795ab1f9dacSPaul Mackerras }
796bdab88e0SSebastian Andrzej Siewior 
797bdab88e0SSebastian Andrzej Siewior static int ppc_numa_cpu_dead(unsigned int cpu)
798bdab88e0SSebastian Andrzej Siewior {
799bdab88e0SSebastian Andrzej Siewior 	return 0;
800ab1f9dacSPaul Mackerras }
801ab1f9dacSPaul Mackerras 
802ab1f9dacSPaul Mackerras /*
803ab1f9dacSPaul Mackerras  * Check and possibly modify a memory region to enforce the memory limit.
804ab1f9dacSPaul Mackerras  *
805ab1f9dacSPaul Mackerras  * Returns the size the region should have to enforce the memory limit.
806ab1f9dacSPaul Mackerras  * This will either be the original value of size, a truncated value,
807ab1f9dacSPaul Mackerras  * or zero. If the returned value of size is 0 the region should be
80825985edcSLucas De Marchi  * discarded as it lies wholly above the memory limit.
809ab1f9dacSPaul Mackerras  */
81045fb6ceaSAnton Blanchard static unsigned long __init numa_enforce_memory_limit(unsigned long start,
81145fb6ceaSAnton Blanchard 						      unsigned long size)
812ab1f9dacSPaul Mackerras {
813ab1f9dacSPaul Mackerras 	/*
81495f72d1eSYinghai Lu 	 * We use memblock_end_of_DRAM() in here instead of memory_limit because
815ab1f9dacSPaul Mackerras 	 * we've already adjusted it for the limit and it takes care of
816fe55249dSMilton Miller 	 * having memory holes below the limit.  Also, in the case of
817fe55249dSMilton Miller 	 * iommu_is_off, memory_limit is not set but is implicitly enforced.
818ab1f9dacSPaul Mackerras 	 */
819ab1f9dacSPaul Mackerras 
82095f72d1eSYinghai Lu 	if (start + size <= memblock_end_of_DRAM())
821ab1f9dacSPaul Mackerras 		return size;
822ab1f9dacSPaul Mackerras 
82395f72d1eSYinghai Lu 	if (start >= memblock_end_of_DRAM())
824ab1f9dacSPaul Mackerras 		return 0;
825ab1f9dacSPaul Mackerras 
82695f72d1eSYinghai Lu 	return memblock_end_of_DRAM() - start;
827ab1f9dacSPaul Mackerras }
828ab1f9dacSPaul Mackerras 
8290204568aSPaul Mackerras /*
830cf00085dSChandru  * Reads the counter for a given entry in
831cf00085dSChandru  * linux,drconf-usable-memory property
832cf00085dSChandru  */
833b08a2a12SAlistair Popple static inline int __init read_usm_ranges(const __be32 **usm)
834cf00085dSChandru {
835cf00085dSChandru 	/*
8363fdfd990SBenjamin Herrenschmidt 	 * For each lmb in ibm,dynamic-memory a corresponding
837cf00085dSChandru 	 * entry in linux,drconf-usable-memory property contains
838cf00085dSChandru 	 * a counter followed by that many (base, size) duple.
839cf00085dSChandru 	 * read the counter from linux,drconf-usable-memory
840cf00085dSChandru 	 */
841cf00085dSChandru 	return read_n_cells(n_mem_size_cells, usm);
842cf00085dSChandru }
843cf00085dSChandru 
844cf00085dSChandru /*
8450204568aSPaul Mackerras  * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
8460204568aSPaul Mackerras  * node.  This assumes n_mem_{addr,size}_cells have been set.
8470204568aSPaul Mackerras  */
848adfefc60SHari Bathini static int __init numa_setup_drmem_lmb(struct drmem_lmb *lmb,
849adfefc60SHari Bathini 					const __be32 **usm,
850adfefc60SHari Bathini 					void *data)
8510204568aSPaul Mackerras {
852514a9cb3SNathan Fontenot 	unsigned int ranges, is_kexec_kdump = 0;
853514a9cb3SNathan Fontenot 	unsigned long base, size, sz;
8548342681dSNathan Fontenot 	int nid;
8550204568aSPaul Mackerras 
856514a9cb3SNathan Fontenot 	/*
857514a9cb3SNathan Fontenot 	 * Skip this block if the reserved bit is set in flags (0x80)
858514a9cb3SNathan Fontenot 	 * or if the block is not assigned to this partition (0x8)
859514a9cb3SNathan Fontenot 	 */
860514a9cb3SNathan Fontenot 	if ((lmb->flags & DRCONF_MEM_RESERVED)
861514a9cb3SNathan Fontenot 	    || !(lmb->flags & DRCONF_MEM_ASSIGNED))
862adfefc60SHari Bathini 		return 0;
8630204568aSPaul Mackerras 
864514a9cb3SNathan Fontenot 	if (*usm)
865cf00085dSChandru 		is_kexec_kdump = 1;
866cf00085dSChandru 
867514a9cb3SNathan Fontenot 	base = lmb->base_addr;
868514a9cb3SNathan Fontenot 	size = drmem_lmb_size();
869cf00085dSChandru 	ranges = 1;
8708342681dSNathan Fontenot 
871cf00085dSChandru 	if (is_kexec_kdump) {
872514a9cb3SNathan Fontenot 		ranges = read_usm_ranges(usm);
873cf00085dSChandru 		if (!ranges) /* there are no (base, size) duple */
874adfefc60SHari Bathini 			return 0;
875cf00085dSChandru 	}
876514a9cb3SNathan Fontenot 
877cf00085dSChandru 	do {
878cf00085dSChandru 		if (is_kexec_kdump) {
879514a9cb3SNathan Fontenot 			base = read_n_cells(n_mem_addr_cells, usm);
880514a9cb3SNathan Fontenot 			size = read_n_cells(n_mem_size_cells, usm);
881cf00085dSChandru 		}
882514a9cb3SNathan Fontenot 
8838ddc6448SAneesh Kumar K.V 		nid = get_nid_and_numa_distance(lmb);
884514a9cb3SNathan Fontenot 		fake_numa_create_new_node(((base + size) >> PAGE_SHIFT),
885cf00085dSChandru 					  &nid);
886cf00085dSChandru 		node_set_online(nid);
887cf00085dSChandru 		sz = numa_enforce_memory_limit(base, size);
888cf00085dSChandru 		if (sz)
889514a9cb3SNathan Fontenot 			memblock_set_node(base, sz, &memblock.memory, nid);
890cf00085dSChandru 	} while (--ranges);
891adfefc60SHari Bathini 
892adfefc60SHari Bathini 	return 0;
8930204568aSPaul Mackerras }
8940204568aSPaul Mackerras 
895ab1f9dacSPaul Mackerras static int __init parse_numa_properties(void)
896ab1f9dacSPaul Mackerras {
89794db7c5eSAnton Blanchard 	struct device_node *memory;
898482ec7c4SNathan Lynch 	int default_nid = 0;
899ab1f9dacSPaul Mackerras 	unsigned long i;
9008ddc6448SAneesh Kumar K.V 	const __be32 *associativity;
901ab1f9dacSPaul Mackerras 
902ab1f9dacSPaul Mackerras 	if (numa_enabled == 0) {
903506c2075SSrikar Dronamraju 		pr_warn("disabled by user\n");
904ab1f9dacSPaul Mackerras 		return -1;
905ab1f9dacSPaul Mackerras 	}
906ab1f9dacSPaul Mackerras 
9077e35ef66SAneesh Kumar K.V 	primary_domain_index = find_primary_domain_index();
908ab1f9dacSPaul Mackerras 
9097e35ef66SAneesh Kumar K.V 	if (primary_domain_index < 0) {
910495c2ff4SAneesh Kumar K.V 		/*
9117e35ef66SAneesh Kumar K.V 		 * if we fail to parse primary_domain_index from device tree
912495c2ff4SAneesh Kumar K.V 		 * mark the numa disabled, boot with numa disabled.
913495c2ff4SAneesh Kumar K.V 		 */
914495c2ff4SAneesh Kumar K.V 		numa_enabled = false;
9157e35ef66SAneesh Kumar K.V 		return primary_domain_index;
916495c2ff4SAneesh Kumar K.V 	}
917ab1f9dacSPaul Mackerras 
918506c2075SSrikar Dronamraju 	pr_debug("associativity depth for CPU/Memory: %d\n", primary_domain_index);
919bf4b85b0SNathan Lynch 
920ab1f9dacSPaul Mackerras 	/*
9211c6b5a7eSAneesh Kumar K.V 	 * If it is FORM2 initialize the distance table here.
9221c6b5a7eSAneesh Kumar K.V 	 */
9231c6b5a7eSAneesh Kumar K.V 	if (affinity_form == FORM2_AFFINITY)
9241c6b5a7eSAneesh Kumar K.V 		initialize_form2_numa_distance_lookup_table();
9251c6b5a7eSAneesh Kumar K.V 
9261c6b5a7eSAneesh Kumar K.V 	/*
927482ec7c4SNathan Lynch 	 * Even though we connect cpus to numa domains later in SMP
928482ec7c4SNathan Lynch 	 * init, we need to know the node ids now. This is because
929482ec7c4SNathan Lynch 	 * each node to be onlined must have NODE_DATA etc backing it.
930ab1f9dacSPaul Mackerras 	 */
931482ec7c4SNathan Lynch 	for_each_present_cpu(i) {
9328ddc6448SAneesh Kumar K.V 		__be32 vphn_assoc[VPHN_ASSOC_BUFSIZE];
933dfbe93a2SAnton Blanchard 		struct device_node *cpu;
9348ddc6448SAneesh Kumar K.V 		int nid = NUMA_NO_NODE;
9358ddc6448SAneesh Kumar K.V 
9368ddc6448SAneesh Kumar K.V 		memset(vphn_assoc, 0, VPHN_ASSOC_BUFSIZE * sizeof(__be32));
9378ddc6448SAneesh Kumar K.V 
9388ddc6448SAneesh Kumar K.V 		if (__vphn_get_associativity(i, vphn_assoc) == 0) {
9398ddc6448SAneesh Kumar K.V 			nid = associativity_to_nid(vphn_assoc);
9408ddc6448SAneesh Kumar K.V 			initialize_form1_numa_distance(vphn_assoc);
9418ddc6448SAneesh Kumar K.V 		} else {
942ab1f9dacSPaul Mackerras 
943482ec7c4SNathan Lynch 			/*
944482ec7c4SNathan Lynch 			 * Don't fall back to default_nid yet -- we will plug
945482ec7c4SNathan Lynch 			 * cpus into nodes once the memory scan has discovered
946482ec7c4SNathan Lynch 			 * the topology.
947482ec7c4SNathan Lynch 			 */
9486398eaa2SSrikar Dronamraju 			cpu = of_get_cpu_node(i, NULL);
9496398eaa2SSrikar Dronamraju 			BUG_ON(!cpu);
9508ddc6448SAneesh Kumar K.V 
9518ddc6448SAneesh Kumar K.V 			associativity = of_get_associativity(cpu);
9528ddc6448SAneesh Kumar K.V 			if (associativity) {
9538ddc6448SAneesh Kumar K.V 				nid = associativity_to_nid(associativity);
9548ddc6448SAneesh Kumar K.V 				initialize_form1_numa_distance(associativity);
9558ddc6448SAneesh Kumar K.V 			}
9566398eaa2SSrikar Dronamraju 			of_node_put(cpu);
9576398eaa2SSrikar Dronamraju 		}
9586398eaa2SSrikar Dronamraju 
959749ed4a2SDaniel Henrique Barboza 		/* node_set_online() is an UB if 'nid' is negative */
960749ed4a2SDaniel Henrique Barboza 		if (likely(nid >= 0))
961482ec7c4SNathan Lynch 			node_set_online(nid);
962ab1f9dacSPaul Mackerras 	}
963ab1f9dacSPaul Mackerras 
964237a0989SMike Kravetz 	get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
96594db7c5eSAnton Blanchard 
96694db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
967ab1f9dacSPaul Mackerras 		unsigned long start;
968ab1f9dacSPaul Mackerras 		unsigned long size;
969cf950b7aSNathan Lynch 		int nid;
970ab1f9dacSPaul Mackerras 		int ranges;
971b08a2a12SAlistair Popple 		const __be32 *memcell_buf;
972ab1f9dacSPaul Mackerras 		unsigned int len;
973ab1f9dacSPaul Mackerras 
974e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory,
975ba759485SMichael Ellerman 			"linux,usable-memory", &len);
976ba759485SMichael Ellerman 		if (!memcell_buf || len <= 0)
977e2eb6392SStephen Rothwell 			memcell_buf = of_get_property(memory, "reg", &len);
978ab1f9dacSPaul Mackerras 		if (!memcell_buf || len <= 0)
979ab1f9dacSPaul Mackerras 			continue;
980ab1f9dacSPaul Mackerras 
981cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
982cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
983ab1f9dacSPaul Mackerras new_range:
984ab1f9dacSPaul Mackerras 		/* these are order-sensitive, and modify the buffer pointer */
985237a0989SMike Kravetz 		start = read_n_cells(n_mem_addr_cells, &memcell_buf);
986237a0989SMike Kravetz 		size = read_n_cells(n_mem_size_cells, &memcell_buf);
987ab1f9dacSPaul Mackerras 
988482ec7c4SNathan Lynch 		/*
989482ec7c4SNathan Lynch 		 * Assumption: either all memory nodes or none will
990482ec7c4SNathan Lynch 		 * have associativity properties.  If none, then
991482ec7c4SNathan Lynch 		 * everything goes to default_nid.
992482ec7c4SNathan Lynch 		 */
9938ddc6448SAneesh Kumar K.V 		associativity = of_get_associativity(memory);
9948ddc6448SAneesh Kumar K.V 		if (associativity) {
9958ddc6448SAneesh Kumar K.V 			nid = associativity_to_nid(associativity);
9968ddc6448SAneesh Kumar K.V 			initialize_form1_numa_distance(associativity);
9978ddc6448SAneesh Kumar K.V 		} else
998482ec7c4SNathan Lynch 			nid = default_nid;
9991daa6d08SBalbir Singh 
10001daa6d08SBalbir Singh 		fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
1001482ec7c4SNathan Lynch 		node_set_online(nid);
1002ab1f9dacSPaul Mackerras 
10037656cd8eSReza Arbab 		size = numa_enforce_memory_limit(start, size);
10047656cd8eSReza Arbab 		if (size)
1005e7e8de59STang Chen 			memblock_set_node(start, size, &memblock.memory, nid);
1006ab1f9dacSPaul Mackerras 
1007ab1f9dacSPaul Mackerras 		if (--ranges)
1008ab1f9dacSPaul Mackerras 			goto new_range;
1009ab1f9dacSPaul Mackerras 	}
1010ab1f9dacSPaul Mackerras 
10110204568aSPaul Mackerras 	/*
1012dfbe93a2SAnton Blanchard 	 * Now do the same thing for each MEMBLOCK listed in the
1013dfbe93a2SAnton Blanchard 	 * ibm,dynamic-memory property in the
1014dfbe93a2SAnton Blanchard 	 * ibm,dynamic-reconfiguration-memory node.
10150204568aSPaul Mackerras 	 */
10160204568aSPaul Mackerras 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1017514a9cb3SNathan Fontenot 	if (memory) {
1018adfefc60SHari Bathini 		walk_drmem_lmbs(memory, NULL, numa_setup_drmem_lmb);
1019514a9cb3SNathan Fontenot 		of_node_put(memory);
1020514a9cb3SNathan Fontenot 	}
10210204568aSPaul Mackerras 
1022ab1f9dacSPaul Mackerras 	return 0;
1023ab1f9dacSPaul Mackerras }
1024ab1f9dacSPaul Mackerras 
1025ab1f9dacSPaul Mackerras static void __init setup_nonnuma(void)
1026ab1f9dacSPaul Mackerras {
102795f72d1eSYinghai Lu 	unsigned long top_of_ram = memblock_end_of_DRAM();
102895f72d1eSYinghai Lu 	unsigned long total_ram = memblock_phys_mem_size();
1029c67c3cb4SMel Gorman 	unsigned long start_pfn, end_pfn;
103028be7072SBenjamin Herrenschmidt 	unsigned int nid = 0;
1031c9118e6cSMike Rapoport 	int i;
1032ab1f9dacSPaul Mackerras 
1033506c2075SSrikar Dronamraju 	pr_debug("Top of RAM: 0x%lx, Total RAM: 0x%lx\n", top_of_ram, total_ram);
1034506c2075SSrikar Dronamraju 	pr_debug("Memory hole size: %ldMB\n", (top_of_ram - total_ram) >> 20);
1035ab1f9dacSPaul Mackerras 
1036c9118e6cSMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
10371daa6d08SBalbir Singh 		fake_numa_create_new_node(end_pfn, &nid);
10381d7cfe18STejun Heo 		memblock_set_node(PFN_PHYS(start_pfn),
1039e7e8de59STang Chen 				  PFN_PHYS(end_pfn - start_pfn),
1040e7e8de59STang Chen 				  &memblock.memory, nid);
10411daa6d08SBalbir Singh 		node_set_online(nid);
1042c67c3cb4SMel Gorman 	}
1043ab1f9dacSPaul Mackerras }
1044ab1f9dacSPaul Mackerras 
10454b703a23SAnton Blanchard void __init dump_numa_cpu_topology(void)
10464b703a23SAnton Blanchard {
10474b703a23SAnton Blanchard 	unsigned int node;
10484b703a23SAnton Blanchard 	unsigned int cpu, count;
10494b703a23SAnton Blanchard 
1050495c2ff4SAneesh Kumar K.V 	if (!numa_enabled)
10514b703a23SAnton Blanchard 		return;
10524b703a23SAnton Blanchard 
10534b703a23SAnton Blanchard 	for_each_online_node(node) {
10548467801cSAneesh Kumar K.V 		pr_info("Node %d CPUs:", node);
10554b703a23SAnton Blanchard 
10564b703a23SAnton Blanchard 		count = 0;
10574b703a23SAnton Blanchard 		/*
10584b703a23SAnton Blanchard 		 * If we used a CPU iterator here we would miss printing
10594b703a23SAnton Blanchard 		 * the holes in the cpumap.
10604b703a23SAnton Blanchard 		 */
106125863de0SAnton Blanchard 		for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
106225863de0SAnton Blanchard 			if (cpumask_test_cpu(cpu,
106325863de0SAnton Blanchard 					node_to_cpumask_map[node])) {
10644b703a23SAnton Blanchard 				if (count == 0)
10658467801cSAneesh Kumar K.V 					pr_cont(" %u", cpu);
10664b703a23SAnton Blanchard 				++count;
10674b703a23SAnton Blanchard 			} else {
10684b703a23SAnton Blanchard 				if (count > 1)
10698467801cSAneesh Kumar K.V 					pr_cont("-%u", cpu - 1);
10704b703a23SAnton Blanchard 				count = 0;
10714b703a23SAnton Blanchard 			}
10724b703a23SAnton Blanchard 		}
10734b703a23SAnton Blanchard 
10744b703a23SAnton Blanchard 		if (count > 1)
10758467801cSAneesh Kumar K.V 			pr_cont("-%u", nr_cpu_ids - 1);
10768467801cSAneesh Kumar K.V 		pr_cont("\n");
10774b703a23SAnton Blanchard 	}
10784b703a23SAnton Blanchard }
10794b703a23SAnton Blanchard 
108010239733SAnton Blanchard /* Initialize NODE_DATA for a node on the local memory */
108110239733SAnton Blanchard static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
1082ab1f9dacSPaul Mackerras {
108310239733SAnton Blanchard 	u64 spanned_pages = end_pfn - start_pfn;
108410239733SAnton Blanchard 	const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
108510239733SAnton Blanchard 	u64 nd_pa;
108610239733SAnton Blanchard 	void *nd;
108710239733SAnton Blanchard 	int tnid;
1088ab1f9dacSPaul Mackerras 
10899a8dd708SMike Rapoport 	nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
109033755574SMike Rapoport 	if (!nd_pa)
109133755574SMike Rapoport 		panic("Cannot allocate %zu bytes for node %d data\n",
109233755574SMike Rapoport 		      nd_size, nid);
109333755574SMike Rapoport 
109410239733SAnton Blanchard 	nd = __va(nd_pa);
1095ab1f9dacSPaul Mackerras 
109610239733SAnton Blanchard 	/* report and initialize */
109710239733SAnton Blanchard 	pr_info("  NODE_DATA [mem %#010Lx-%#010Lx]\n",
109810239733SAnton Blanchard 		nd_pa, nd_pa + nd_size - 1);
109910239733SAnton Blanchard 	tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
110010239733SAnton Blanchard 	if (tnid != nid)
110110239733SAnton Blanchard 		pr_info("    NODE_DATA(%d) on node %d\n", nid, tnid);
11028f64e1f2SJon Tollefson 
110310239733SAnton Blanchard 	node_data[nid] = nd;
110410239733SAnton Blanchard 	memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
110510239733SAnton Blanchard 	NODE_DATA(nid)->node_id = nid;
110610239733SAnton Blanchard 	NODE_DATA(nid)->node_start_pfn = start_pfn;
110710239733SAnton Blanchard 	NODE_DATA(nid)->node_spanned_pages = spanned_pages;
1108ab1f9dacSPaul Mackerras }
11098f64e1f2SJon Tollefson 
1110a346137eSMichael Bringmann static void __init find_possible_nodes(void)
1111a346137eSMichael Bringmann {
1112a346137eSMichael Bringmann 	struct device_node *rtas;
11139c7248bbSLaurent Dufour 	const __be32 *domains = NULL;
1114f9f130ffSSrikar Dronamraju 	int prop_length, max_nodes;
1115f9f130ffSSrikar Dronamraju 	u32 i;
1116a346137eSMichael Bringmann 
1117495c2ff4SAneesh Kumar K.V 	if (!numa_enabled)
1118a346137eSMichael Bringmann 		return;
1119a346137eSMichael Bringmann 
1120a346137eSMichael Bringmann 	rtas = of_find_node_by_path("/rtas");
1121a346137eSMichael Bringmann 	if (!rtas)
1122a346137eSMichael Bringmann 		return;
1123a346137eSMichael Bringmann 
112467df7784SSrikar Dronamraju 	/*
1125f9f130ffSSrikar Dronamraju 	 * ibm,current-associativity-domains is a fairly recent property. If
1126f9f130ffSSrikar Dronamraju 	 * it doesn't exist, then fallback on ibm,max-associativity-domains.
1127f9f130ffSSrikar Dronamraju 	 * Current denotes what the platform can support compared to max
1128f9f130ffSSrikar Dronamraju 	 * which denotes what the Hypervisor can support.
11299c7248bbSLaurent Dufour 	 *
11309c7248bbSLaurent Dufour 	 * If the LPAR is migratable, new nodes might be activated after a LPM,
11319c7248bbSLaurent Dufour 	 * so we should consider the max number in that case.
113267df7784SSrikar Dronamraju 	 */
11339c7248bbSLaurent Dufour 	if (!of_get_property(of_root, "ibm,migratable-partition", NULL))
11349c7248bbSLaurent Dufour 		domains = of_get_property(rtas,
11359c7248bbSLaurent Dufour 					  "ibm,current-associativity-domains",
1136f9f130ffSSrikar Dronamraju 					  &prop_length);
1137f9f130ffSSrikar Dronamraju 	if (!domains) {
1138f9f130ffSSrikar Dronamraju 		domains = of_get_property(rtas, "ibm,max-associativity-domains",
1139f9f130ffSSrikar Dronamraju 					&prop_length);
1140f9f130ffSSrikar Dronamraju 		if (!domains)
1141a346137eSMichael Bringmann 			goto out;
114267df7784SSrikar Dronamraju 	}
1143a346137eSMichael Bringmann 
11447e35ef66SAneesh Kumar K.V 	max_nodes = of_read_number(&domains[primary_domain_index], 1);
11459c7248bbSLaurent Dufour 	pr_info("Partition configured for %d NUMA nodes.\n", max_nodes);
11469c7248bbSLaurent Dufour 
1147f9f130ffSSrikar Dronamraju 	for (i = 0; i < max_nodes; i++) {
1148ea05ba7cSMichael Bringmann 		if (!node_possible(i))
1149a346137eSMichael Bringmann 			node_set(i, node_possible_map);
1150a346137eSMichael Bringmann 	}
1151a346137eSMichael Bringmann 
1152f9f130ffSSrikar Dronamraju 	prop_length /= sizeof(int);
11537e35ef66SAneesh Kumar K.V 	if (prop_length > primary_domain_index + 2)
1154f9f130ffSSrikar Dronamraju 		coregroup_enabled = 1;
1155f9f130ffSSrikar Dronamraju 
1156a346137eSMichael Bringmann out:
1157a346137eSMichael Bringmann 	of_node_put(rtas);
1158a346137eSMichael Bringmann }
1159a346137eSMichael Bringmann 
11609bd9be00SNicholas Piggin void __init mem_topology_setup(void)
11614a618669SDave Hansen {
11629bd9be00SNicholas Piggin 	int cpu;
11634a618669SDave Hansen 
1164e75130f2SSrikar Dronamraju 	/*
1165e75130f2SSrikar Dronamraju 	 * Linux/mm assumes node 0 to be online at boot. However this is not
1166e75130f2SSrikar Dronamraju 	 * true on PowerPC, where node 0 is similar to any other node, it
1167e75130f2SSrikar Dronamraju 	 * could be cpuless, memoryless node. So force node 0 to be offline
1168e75130f2SSrikar Dronamraju 	 * for now. This will prevent cpuless, memoryless node 0 showing up
1169e75130f2SSrikar Dronamraju 	 * unnecessarily as online. If a node has cpus or memory that need
1170e75130f2SSrikar Dronamraju 	 * to be online, then node will anyway be marked online.
1171e75130f2SSrikar Dronamraju 	 */
1172e75130f2SSrikar Dronamraju 	node_set_offline(0);
1173e75130f2SSrikar Dronamraju 
11744a618669SDave Hansen 	if (parse_numa_properties())
11754a618669SDave Hansen 		setup_nonnuma();
11764a618669SDave Hansen 
11773af229f2SNishanth Aravamudan 	/*
1178a346137eSMichael Bringmann 	 * Modify the set of possible NUMA nodes to reflect information
1179a346137eSMichael Bringmann 	 * available about the set of online nodes, and the set of nodes
1180a346137eSMichael Bringmann 	 * that we expect to make use of for this platform's affinity
1181a346137eSMichael Bringmann 	 * calculations.
11823af229f2SNishanth Aravamudan 	 */
11833af229f2SNishanth Aravamudan 	nodes_and(node_possible_map, node_possible_map, node_online_map);
11843af229f2SNishanth Aravamudan 
1185a346137eSMichael Bringmann 	find_possible_nodes();
1186a346137eSMichael Bringmann 
11879bd9be00SNicholas Piggin 	setup_node_to_cpumask_map();
11889bd9be00SNicholas Piggin 
11899bd9be00SNicholas Piggin 	reset_numa_cpu_lookup_table();
11909bd9be00SNicholas Piggin 
1191a874f100SSrikar Dronamraju 	for_each_possible_cpu(cpu) {
1192a874f100SSrikar Dronamraju 		/*
1193a874f100SSrikar Dronamraju 		 * Powerpc with CONFIG_NUMA always used to have a node 0,
1194a874f100SSrikar Dronamraju 		 * even if it was memoryless or cpuless. For all cpus that
1195a874f100SSrikar Dronamraju 		 * are possible but not present, cpu_to_node() would point
1196a874f100SSrikar Dronamraju 		 * to node 0. To remove a cpuless, memoryless dummy node,
1197a874f100SSrikar Dronamraju 		 * powerpc need to make sure all possible but not present
1198a874f100SSrikar Dronamraju 		 * cpu_to_node are set to a proper node.
1199a874f100SSrikar Dronamraju 		 */
12009bd9be00SNicholas Piggin 		numa_setup_cpu(cpu);
12019bd9be00SNicholas Piggin 	}
1202a874f100SSrikar Dronamraju }
12039bd9be00SNicholas Piggin 
12049bd9be00SNicholas Piggin void __init initmem_init(void)
12059bd9be00SNicholas Piggin {
12069bd9be00SNicholas Piggin 	int nid;
12079bd9be00SNicholas Piggin 
12089bd9be00SNicholas Piggin 	max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
12099bd9be00SNicholas Piggin 	max_pfn = max_low_pfn;
12109bd9be00SNicholas Piggin 
12119bd9be00SNicholas Piggin 	memblock_dump_all();
12129bd9be00SNicholas Piggin 
12134a618669SDave Hansen 	for_each_online_node(nid) {
12144a618669SDave Hansen 		unsigned long start_pfn, end_pfn;
12154a618669SDave Hansen 
12164a618669SDave Hansen 		get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
121710239733SAnton Blanchard 		setup_node_data(nid, start_pfn, end_pfn);
1218ab1f9dacSPaul Mackerras 	}
1219d3f6204aSBenjamin Herrenschmidt 
122021098b9eSAnton Blanchard 	sparse_init();
122125863de0SAnton Blanchard 
12222fabf084SNishanth Aravamudan 	/*
12232fabf084SNishanth Aravamudan 	 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
12242fabf084SNishanth Aravamudan 	 * even before we online them, so that we can use cpu_to_{node,mem}
12252fabf084SNishanth Aravamudan 	 * early in boot, cf. smp_prepare_cpus().
1226bdab88e0SSebastian Andrzej Siewior 	 * _nocalls() + manual invocation is used because cpuhp is not yet
1227bdab88e0SSebastian Andrzej Siewior 	 * initialized for the boot CPU.
12282fabf084SNishanth Aravamudan 	 */
122973c1b41eSThomas Gleixner 	cpuhp_setup_state_nocalls(CPUHP_POWER_NUMA_PREPARE, "powerpc/numa:prepare",
1230bdab88e0SSebastian Andrzej Siewior 				  ppc_numa_cpu_prepare, ppc_numa_cpu_dead);
12314a618669SDave Hansen }
1232ab1f9dacSPaul Mackerras 
1233ab1f9dacSPaul Mackerras static int __init early_numa(char *p)
1234ab1f9dacSPaul Mackerras {
1235ab1f9dacSPaul Mackerras 	if (!p)
1236ab1f9dacSPaul Mackerras 		return 0;
1237ab1f9dacSPaul Mackerras 
1238ab1f9dacSPaul Mackerras 	if (strstr(p, "off"))
1239ab1f9dacSPaul Mackerras 		numa_enabled = 0;
1240ab1f9dacSPaul Mackerras 
12411daa6d08SBalbir Singh 	p = strstr(p, "fake=");
12421daa6d08SBalbir Singh 	if (p)
12431daa6d08SBalbir Singh 		cmdline = p + strlen("fake=");
12441daa6d08SBalbir Singh 
1245ab1f9dacSPaul Mackerras 	return 0;
1246ab1f9dacSPaul Mackerras }
1247ab1f9dacSPaul Mackerras early_param("numa", early_numa);
1248237a0989SMike Kravetz 
1249237a0989SMike Kravetz #ifdef CONFIG_MEMORY_HOTPLUG
1250237a0989SMike Kravetz /*
12510f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for
12520f16ef7fSNathan Fontenot  * memory represented in the device tree by the property
12530f16ef7fSNathan Fontenot  * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
12540db9360aSNathan Fontenot  */
1255514a9cb3SNathan Fontenot static int hot_add_drconf_scn_to_nid(unsigned long scn_addr)
12560db9360aSNathan Fontenot {
1257514a9cb3SNathan Fontenot 	struct drmem_lmb *lmb;
12583fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size;
125998fa15f3SAnshuman Khandual 	int nid = NUMA_NO_NODE;
12600db9360aSNathan Fontenot 
1261514a9cb3SNathan Fontenot 	lmb_size = drmem_lmb_size();
12620db9360aSNathan Fontenot 
1263514a9cb3SNathan Fontenot 	for_each_drmem_lmb(lmb) {
12640db9360aSNathan Fontenot 		/* skip this block if it is reserved or not assigned to
12650db9360aSNathan Fontenot 		 * this partition */
1266514a9cb3SNathan Fontenot 		if ((lmb->flags & DRCONF_MEM_RESERVED)
1267514a9cb3SNathan Fontenot 		    || !(lmb->flags & DRCONF_MEM_ASSIGNED))
12680db9360aSNathan Fontenot 			continue;
12690db9360aSNathan Fontenot 
1270514a9cb3SNathan Fontenot 		if ((scn_addr < lmb->base_addr)
1271514a9cb3SNathan Fontenot 		    || (scn_addr >= (lmb->base_addr + lmb_size)))
12720f16ef7fSNathan Fontenot 			continue;
12730db9360aSNathan Fontenot 
1274514a9cb3SNathan Fontenot 		nid = of_drconf_to_nid_single(lmb);
12750f16ef7fSNathan Fontenot 		break;
12760db9360aSNathan Fontenot 	}
12770db9360aSNathan Fontenot 
12780f16ef7fSNathan Fontenot 	return nid;
12790db9360aSNathan Fontenot }
12800db9360aSNathan Fontenot 
12810db9360aSNathan Fontenot /*
12820f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for memory
12830f16ef7fSNathan Fontenot  * represented in the device tree as a node (i.e. memory@XXXX) for
128495f72d1eSYinghai Lu  * each memblock.
1285237a0989SMike Kravetz  */
1286ec32dd66SRobert Jennings static int hot_add_node_scn_to_nid(unsigned long scn_addr)
1287237a0989SMike Kravetz {
128894db7c5eSAnton Blanchard 	struct device_node *memory;
128998fa15f3SAnshuman Khandual 	int nid = NUMA_NO_NODE;
1290237a0989SMike Kravetz 
129194db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
1292237a0989SMike Kravetz 		unsigned long start, size;
1293b226e462SMike Kravetz 		int ranges;
1294b08a2a12SAlistair Popple 		const __be32 *memcell_buf;
1295237a0989SMike Kravetz 		unsigned int len;
1296237a0989SMike Kravetz 
1297e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory, "reg", &len);
1298237a0989SMike Kravetz 		if (!memcell_buf || len <= 0)
1299237a0989SMike Kravetz 			continue;
1300237a0989SMike Kravetz 
1301cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
1302cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
13030f16ef7fSNathan Fontenot 
13040f16ef7fSNathan Fontenot 		while (ranges--) {
1305237a0989SMike Kravetz 			start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1306237a0989SMike Kravetz 			size = read_n_cells(n_mem_size_cells, &memcell_buf);
1307237a0989SMike Kravetz 
13080f16ef7fSNathan Fontenot 			if ((scn_addr < start) || (scn_addr >= (start + size)))
13090f16ef7fSNathan Fontenot 				continue;
13100f16ef7fSNathan Fontenot 
13110f16ef7fSNathan Fontenot 			nid = of_node_to_nid_single(memory);
13120f16ef7fSNathan Fontenot 			break;
13130f16ef7fSNathan Fontenot 		}
13140f16ef7fSNathan Fontenot 
13150f16ef7fSNathan Fontenot 		if (nid >= 0)
13160f16ef7fSNathan Fontenot 			break;
13170f16ef7fSNathan Fontenot 	}
13180f16ef7fSNathan Fontenot 
131960831842SAnton Blanchard 	of_node_put(memory);
132060831842SAnton Blanchard 
13210db9360aSNathan Fontenot 	return nid;
1322237a0989SMike Kravetz }
1323237a0989SMike Kravetz 
13240f16ef7fSNathan Fontenot /*
13250f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section.  Section
132695f72d1eSYinghai Lu  * corresponds to a SPARSEMEM section, not an MEMBLOCK.  It is assumed that
132795f72d1eSYinghai Lu  * sections are fully contained within a single MEMBLOCK.
13280f16ef7fSNathan Fontenot  */
13290f16ef7fSNathan Fontenot int hot_add_scn_to_nid(unsigned long scn_addr)
13300f16ef7fSNathan Fontenot {
13310f16ef7fSNathan Fontenot 	struct device_node *memory = NULL;
13324a3bac4eSReza Arbab 	int nid;
13330f16ef7fSNathan Fontenot 
1334495c2ff4SAneesh Kumar K.V 	if (!numa_enabled)
133572c33688SH Hartley Sweeten 		return first_online_node;
13360f16ef7fSNathan Fontenot 
13370f16ef7fSNathan Fontenot 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
13380f16ef7fSNathan Fontenot 	if (memory) {
1339514a9cb3SNathan Fontenot 		nid = hot_add_drconf_scn_to_nid(scn_addr);
13400f16ef7fSNathan Fontenot 		of_node_put(memory);
13410f16ef7fSNathan Fontenot 	} else {
13420f16ef7fSNathan Fontenot 		nid = hot_add_node_scn_to_nid(scn_addr);
1343237a0989SMike Kravetz 	}
13440f16ef7fSNathan Fontenot 
13452a8628d4SReza Arbab 	if (nid < 0 || !node_possible(nid))
134672c33688SH Hartley Sweeten 		nid = first_online_node;
13470f16ef7fSNathan Fontenot 
13480f16ef7fSNathan Fontenot 	return nid;
13490f16ef7fSNathan Fontenot }
13500f16ef7fSNathan Fontenot 
1351cd34206eSNishanth Aravamudan static u64 hot_add_drconf_memory_max(void)
1352cd34206eSNishanth Aravamudan {
1353cd34206eSNishanth Aravamudan 	struct device_node *memory = NULL;
135445b64ee6SBharata B Rao 	struct device_node *dn = NULL;
135545b64ee6SBharata B Rao 	const __be64 *lrdr = NULL;
135645b64ee6SBharata B Rao 
135745b64ee6SBharata B Rao 	dn = of_find_node_by_path("/rtas");
135845b64ee6SBharata B Rao 	if (dn) {
135945b64ee6SBharata B Rao 		lrdr = of_get_property(dn, "ibm,lrdr-capacity", NULL);
136045b64ee6SBharata B Rao 		of_node_put(dn);
136145b64ee6SBharata B Rao 		if (lrdr)
136245b64ee6SBharata B Rao 			return be64_to_cpup(lrdr);
136345b64ee6SBharata B Rao 	}
1364cd34206eSNishanth Aravamudan 
1365cd34206eSNishanth Aravamudan 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1366cd34206eSNishanth Aravamudan 	if (memory) {
1367cd34206eSNishanth Aravamudan 		of_node_put(memory);
1368514a9cb3SNathan Fontenot 		return drmem_lmb_memory_max();
1369cd34206eSNishanth Aravamudan 	}
137045b64ee6SBharata B Rao 	return 0;
1371cd34206eSNishanth Aravamudan }
1372cd34206eSNishanth Aravamudan 
1373cd34206eSNishanth Aravamudan /*
1374cd34206eSNishanth Aravamudan  * memory_hotplug_max - return max address of memory that may be added
1375cd34206eSNishanth Aravamudan  *
1376cd34206eSNishanth Aravamudan  * This is currently only used on systems that support drconfig memory
1377cd34206eSNishanth Aravamudan  * hotplug.
1378cd34206eSNishanth Aravamudan  */
1379cd34206eSNishanth Aravamudan u64 memory_hotplug_max(void)
1380cd34206eSNishanth Aravamudan {
1381cd34206eSNishanth Aravamudan         return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1382cd34206eSNishanth Aravamudan }
1383237a0989SMike Kravetz #endif /* CONFIG_MEMORY_HOTPLUG */
13849eff1a38SJesse Larrew 
1385bd03403aSJesse Larrew /* Virtual Processor Home Node (VPHN) support */
138639bf990eSJesse Larrew #ifdef CONFIG_PPC_SPLPAR
138717f444c0SMichael Bringmann static int topology_inited;
13889eff1a38SJesse Larrew 
13899eff1a38SJesse Larrew /*
13909eff1a38SJesse Larrew  * Retrieve the new associativity information for a virtual processor's
13919eff1a38SJesse Larrew  * home node.
13929eff1a38SJesse Larrew  */
13939eff1a38SJesse Larrew static long vphn_get_associativity(unsigned long cpu,
1394b08a2a12SAlistair Popple 					__be32 *associativity)
13959eff1a38SJesse Larrew {
1396cd9d6cc7SJesse Larrew 	long rc;
13979eff1a38SJesse Larrew 
1398ef34e0efSNaveen N. Rao 	rc = hcall_vphn(get_hard_smp_processor_id(cpu),
1399ef34e0efSNaveen N. Rao 				VPHN_FLAG_VCPU, associativity);
14009eff1a38SJesse Larrew 
14019eff1a38SJesse Larrew 	switch (rc) {
140217f444c0SMichael Bringmann 	case H_SUCCESS:
1403544af642SSrikar Dronamraju 		pr_debug("VPHN hcall succeeded. Reset polling...\n");
140476b7bfb1SSrikar Dronamraju 		goto out;
140576b7bfb1SSrikar Dronamraju 
140676b7bfb1SSrikar Dronamraju 	case H_FUNCTION:
140776b7bfb1SSrikar Dronamraju 		pr_err_ratelimited("VPHN unsupported. Disabling polling...\n");
140876b7bfb1SSrikar Dronamraju 		break;
140976b7bfb1SSrikar Dronamraju 	case H_HARDWARE:
141076b7bfb1SSrikar Dronamraju 		pr_err_ratelimited("hcall_vphn() experienced a hardware fault "
141176b7bfb1SSrikar Dronamraju 			"preventing VPHN. Disabling polling...\n");
141276b7bfb1SSrikar Dronamraju 		break;
141376b7bfb1SSrikar Dronamraju 	case H_PARAMETER:
141476b7bfb1SSrikar Dronamraju 		pr_err_ratelimited("hcall_vphn() was passed an invalid parameter. "
141576b7bfb1SSrikar Dronamraju 			"Disabling polling...\n");
141676b7bfb1SSrikar Dronamraju 		break;
141776b7bfb1SSrikar Dronamraju 	default:
141876b7bfb1SSrikar Dronamraju 		pr_err_ratelimited("hcall_vphn() returned %ld. Disabling polling...\n"
141976b7bfb1SSrikar Dronamraju 			, rc);
142017f444c0SMichael Bringmann 		break;
14219eff1a38SJesse Larrew 	}
142276b7bfb1SSrikar Dronamraju out:
14239eff1a38SJesse Larrew 	return rc;
14249eff1a38SJesse Larrew }
14259eff1a38SJesse Larrew 
1426e67e02a5SMichael Bringmann int find_and_online_cpu_nid(int cpu)
1427ea05ba7cSMichael Bringmann {
1428ea05ba7cSMichael Bringmann 	__be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1429ea05ba7cSMichael Bringmann 	int new_nid;
1430ea05ba7cSMichael Bringmann 
1431ea05ba7cSMichael Bringmann 	/* Use associativity from first thread for all siblings */
14322483ef05SSrikar Dronamraju 	if (vphn_get_associativity(cpu, associativity))
14332483ef05SSrikar Dronamraju 		return cpu_to_node(cpu);
14342483ef05SSrikar Dronamraju 
1435ea05ba7cSMichael Bringmann 	new_nid = associativity_to_nid(associativity);
1436ea05ba7cSMichael Bringmann 	if (new_nid < 0 || !node_possible(new_nid))
1437ea05ba7cSMichael Bringmann 		new_nid = first_online_node;
1438ea05ba7cSMichael Bringmann 
1439e4ff7759SSrikar Dronamraju 	if (!node_online(new_nid)) {
1440ea05ba7cSMichael Bringmann #ifdef CONFIG_MEMORY_HOTPLUG
1441ea05ba7cSMichael Bringmann 		/*
1442ea05ba7cSMichael Bringmann 		 * Need to ensure that NODE_DATA is initialized for a node from
1443ea05ba7cSMichael Bringmann 		 * available memory (see memblock_alloc_try_nid). If unable to
1444ea05ba7cSMichael Bringmann 		 * init the node, then default to nearest node that has memory
1445ac1788ccSSrikar Dronamraju 		 * installed. Skip onlining a node if the subsystems are not
1446ac1788ccSSrikar Dronamraju 		 * yet initialized.
1447ea05ba7cSMichael Bringmann 		 */
1448ac1788ccSSrikar Dronamraju 		if (!topology_inited || try_online_node(new_nid))
1449ea05ba7cSMichael Bringmann 			new_nid = first_online_node;
1450ea05ba7cSMichael Bringmann #else
1451ea05ba7cSMichael Bringmann 		/*
1452ea05ba7cSMichael Bringmann 		 * Default to using the nearest node that has memory installed.
1453ea05ba7cSMichael Bringmann 		 * Otherwise, it would be necessary to patch the kernel MM code
1454ea05ba7cSMichael Bringmann 		 * to deal with more memoryless-node error conditions.
1455ea05ba7cSMichael Bringmann 		 */
1456ea05ba7cSMichael Bringmann 		new_nid = first_online_node;
1457ea05ba7cSMichael Bringmann #endif
1458ea05ba7cSMichael Bringmann 	}
1459ea05ba7cSMichael Bringmann 
1460*e62520b8SDwaipayan Ray 	pr_debug("%s:%d cpu %d nid %d\n", __func__, __LINE__, cpu, new_nid);
1461ea05ba7cSMichael Bringmann 	return new_nid;
1462ea05ba7cSMichael Bringmann }
1463ea05ba7cSMichael Bringmann 
146472730bfcSSrikar Dronamraju int cpu_to_coregroup_id(int cpu)
146572730bfcSSrikar Dronamraju {
1466fa35e868SSrikar Dronamraju 	__be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1467fa35e868SSrikar Dronamraju 	int index;
1468fa35e868SSrikar Dronamraju 
1469fa35e868SSrikar Dronamraju 	if (cpu < 0 || cpu > nr_cpu_ids)
1470fa35e868SSrikar Dronamraju 		return -1;
1471fa35e868SSrikar Dronamraju 
1472fa35e868SSrikar Dronamraju 	if (!coregroup_enabled)
1473fa35e868SSrikar Dronamraju 		goto out;
1474fa35e868SSrikar Dronamraju 
1475fa35e868SSrikar Dronamraju 	if (!firmware_has_feature(FW_FEATURE_VPHN))
1476fa35e868SSrikar Dronamraju 		goto out;
1477fa35e868SSrikar Dronamraju 
1478fa35e868SSrikar Dronamraju 	if (vphn_get_associativity(cpu, associativity))
1479fa35e868SSrikar Dronamraju 		goto out;
1480fa35e868SSrikar Dronamraju 
1481fa35e868SSrikar Dronamraju 	index = of_read_number(associativity, 1);
14827e35ef66SAneesh Kumar K.V 	if (index > primary_domain_index + 1)
1483fa35e868SSrikar Dronamraju 		return of_read_number(&associativity[index - 1], 1);
1484fa35e868SSrikar Dronamraju 
1485fa35e868SSrikar Dronamraju out:
148672730bfcSSrikar Dronamraju 	return cpu_to_core_id(cpu);
148772730bfcSSrikar Dronamraju }
148872730bfcSSrikar Dronamraju 
1489e04fa612SNathan Fontenot static int topology_update_init(void)
1490e04fa612SNathan Fontenot {
149117f444c0SMichael Bringmann 	topology_inited = 1;
1492e04fa612SNathan Fontenot 	return 0;
1493e04fa612SNathan Fontenot }
1494e04fa612SNathan Fontenot device_initcall(topology_update_init);
149539bf990eSJesse Larrew #endif /* CONFIG_PPC_SPLPAR */
1496