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