xref: /linux/arch/powerpc/mm/numa.c (revision b7abef045fdacdb40eef7f22ebbb566d63f9f240)
1ab1f9dacSPaul Mackerras /*
2ab1f9dacSPaul Mackerras  * pSeries NUMA support
3ab1f9dacSPaul Mackerras  *
4ab1f9dacSPaul Mackerras  * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
5ab1f9dacSPaul Mackerras  *
6ab1f9dacSPaul Mackerras  * This program is free software; you can redistribute it and/or
7ab1f9dacSPaul Mackerras  * modify it under the terms of the GNU General Public License
8ab1f9dacSPaul Mackerras  * as published by the Free Software Foundation; either version
9ab1f9dacSPaul Mackerras  * 2 of the License, or (at your option) any later version.
10ab1f9dacSPaul Mackerras  */
11ab1f9dacSPaul Mackerras #include <linux/threads.h>
12ab1f9dacSPaul Mackerras #include <linux/bootmem.h>
13ab1f9dacSPaul Mackerras #include <linux/init.h>
14ab1f9dacSPaul Mackerras #include <linux/mm.h>
15ab1f9dacSPaul Mackerras #include <linux/mmzone.h>
164b16f8e2SPaul Gortmaker #include <linux/export.h>
17ab1f9dacSPaul Mackerras #include <linux/nodemask.h>
18ab1f9dacSPaul Mackerras #include <linux/cpu.h>
19ab1f9dacSPaul Mackerras #include <linux/notifier.h>
2095f72d1eSYinghai Lu #include <linux/memblock.h>
216df1646eSMichael Ellerman #include <linux/of.h>
2206eccea6SDave Hansen #include <linux/pfn.h>
239eff1a38SJesse Larrew #include <linux/cpuset.h>
249eff1a38SJesse Larrew #include <linux/node.h>
2530c05350SNathan Fontenot #include <linux/stop_machine.h>
2645fb6ceaSAnton Blanchard #include <asm/sparsemem.h>
27d9b2b2a2SDavid S. Miller #include <asm/prom.h>
282249ca9dSPaul Mackerras #include <asm/smp.h>
299eff1a38SJesse Larrew #include <asm/firmware.h>
309eff1a38SJesse Larrew #include <asm/paca.h>
3139bf990eSJesse Larrew #include <asm/hvcall.h>
32ae3a197eSDavid Howells #include <asm/setup.h>
33176bbf14SJesse Larrew #include <asm/vdso.h>
34ab1f9dacSPaul Mackerras 
35ab1f9dacSPaul Mackerras static int numa_enabled = 1;
36ab1f9dacSPaul Mackerras 
371daa6d08SBalbir Singh static char *cmdline __initdata;
381daa6d08SBalbir Singh 
39ab1f9dacSPaul Mackerras static int numa_debug;
40ab1f9dacSPaul Mackerras #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
41ab1f9dacSPaul Mackerras 
4245fb6ceaSAnton Blanchard int numa_cpu_lookup_table[NR_CPUS];
4325863de0SAnton Blanchard cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
44ab1f9dacSPaul Mackerras struct pglist_data *node_data[MAX_NUMNODES];
4545fb6ceaSAnton Blanchard 
4645fb6ceaSAnton Blanchard EXPORT_SYMBOL(numa_cpu_lookup_table);
4725863de0SAnton Blanchard EXPORT_SYMBOL(node_to_cpumask_map);
4845fb6ceaSAnton Blanchard EXPORT_SYMBOL(node_data);
4945fb6ceaSAnton Blanchard 
50ab1f9dacSPaul Mackerras static int min_common_depth;
51237a0989SMike Kravetz static int n_mem_addr_cells, n_mem_size_cells;
5241eab6f8SAnton Blanchard static int form1_affinity;
5341eab6f8SAnton Blanchard 
5441eab6f8SAnton Blanchard #define MAX_DISTANCE_REF_POINTS 4
5541eab6f8SAnton Blanchard static int distance_ref_points_depth;
5641eab6f8SAnton Blanchard static const unsigned int *distance_ref_points;
5741eab6f8SAnton Blanchard static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
58ab1f9dacSPaul Mackerras 
5925863de0SAnton Blanchard /*
6025863de0SAnton Blanchard  * Allocate node_to_cpumask_map based on number of available nodes
6125863de0SAnton Blanchard  * Requires node_possible_map to be valid.
6225863de0SAnton Blanchard  *
639512938bSWanlong Gao  * Note: cpumask_of_node() is not valid until after this is done.
6425863de0SAnton Blanchard  */
6525863de0SAnton Blanchard static void __init setup_node_to_cpumask_map(void)
6625863de0SAnton Blanchard {
6725863de0SAnton Blanchard 	unsigned int node, num = 0;
6825863de0SAnton Blanchard 
6925863de0SAnton Blanchard 	/* setup nr_node_ids if not done yet */
7025863de0SAnton Blanchard 	if (nr_node_ids == MAX_NUMNODES) {
7125863de0SAnton Blanchard 		for_each_node_mask(node, node_possible_map)
7225863de0SAnton Blanchard 			num = node;
7325863de0SAnton Blanchard 		nr_node_ids = num + 1;
7425863de0SAnton Blanchard 	}
7525863de0SAnton Blanchard 
7625863de0SAnton Blanchard 	/* allocate the map */
7725863de0SAnton Blanchard 	for (node = 0; node < nr_node_ids; node++)
7825863de0SAnton Blanchard 		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
7925863de0SAnton Blanchard 
8025863de0SAnton Blanchard 	/* cpumask_of_node() will now work */
8125863de0SAnton Blanchard 	dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
8225863de0SAnton Blanchard }
8325863de0SAnton Blanchard 
8455671f3cSStephen Rothwell static int __init fake_numa_create_new_node(unsigned long end_pfn,
851daa6d08SBalbir Singh 						unsigned int *nid)
861daa6d08SBalbir Singh {
871daa6d08SBalbir Singh 	unsigned long long mem;
881daa6d08SBalbir Singh 	char *p = cmdline;
891daa6d08SBalbir Singh 	static unsigned int fake_nid;
901daa6d08SBalbir Singh 	static unsigned long long curr_boundary;
911daa6d08SBalbir Singh 
921daa6d08SBalbir Singh 	/*
931daa6d08SBalbir Singh 	 * Modify node id, iff we started creating NUMA nodes
941daa6d08SBalbir Singh 	 * We want to continue from where we left of the last time
951daa6d08SBalbir Singh 	 */
961daa6d08SBalbir Singh 	if (fake_nid)
971daa6d08SBalbir Singh 		*nid = fake_nid;
981daa6d08SBalbir Singh 	/*
991daa6d08SBalbir Singh 	 * In case there are no more arguments to parse, the
1001daa6d08SBalbir Singh 	 * node_id should be the same as the last fake node id
1011daa6d08SBalbir Singh 	 * (we've handled this above).
1021daa6d08SBalbir Singh 	 */
1031daa6d08SBalbir Singh 	if (!p)
1041daa6d08SBalbir Singh 		return 0;
1051daa6d08SBalbir Singh 
1061daa6d08SBalbir Singh 	mem = memparse(p, &p);
1071daa6d08SBalbir Singh 	if (!mem)
1081daa6d08SBalbir Singh 		return 0;
1091daa6d08SBalbir Singh 
1101daa6d08SBalbir Singh 	if (mem < curr_boundary)
1111daa6d08SBalbir Singh 		return 0;
1121daa6d08SBalbir Singh 
1131daa6d08SBalbir Singh 	curr_boundary = mem;
1141daa6d08SBalbir Singh 
1151daa6d08SBalbir Singh 	if ((end_pfn << PAGE_SHIFT) > mem) {
1161daa6d08SBalbir Singh 		/*
1171daa6d08SBalbir Singh 		 * Skip commas and spaces
1181daa6d08SBalbir Singh 		 */
1191daa6d08SBalbir Singh 		while (*p == ',' || *p == ' ' || *p == '\t')
1201daa6d08SBalbir Singh 			p++;
1211daa6d08SBalbir Singh 
1221daa6d08SBalbir Singh 		cmdline = p;
1231daa6d08SBalbir Singh 		fake_nid++;
1241daa6d08SBalbir Singh 		*nid = fake_nid;
1251daa6d08SBalbir Singh 		dbg("created new fake_node with id %d\n", fake_nid);
1261daa6d08SBalbir Singh 		return 1;
1271daa6d08SBalbir Singh 	}
1281daa6d08SBalbir Singh 	return 0;
1291daa6d08SBalbir Singh }
1301daa6d08SBalbir Singh 
1318f64e1f2SJon Tollefson /*
1325dfe8660STejun Heo  * get_node_active_region - Return active region containing pfn
133e8170372SJon Tollefson  * Active range returned is empty if none found.
1345dfe8660STejun Heo  * @pfn: The page to return the region for
1355dfe8660STejun Heo  * @node_ar: Returned set to the active region containing @pfn
1368f64e1f2SJon Tollefson  */
1375dfe8660STejun Heo static void __init get_node_active_region(unsigned long pfn,
1388f64e1f2SJon Tollefson 					  struct node_active_region *node_ar)
1398f64e1f2SJon Tollefson {
1405dfe8660STejun Heo 	unsigned long start_pfn, end_pfn;
1415dfe8660STejun Heo 	int i, nid;
1428f64e1f2SJon Tollefson 
1435dfe8660STejun Heo 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
1445dfe8660STejun Heo 		if (pfn >= start_pfn && pfn < end_pfn) {
1458f64e1f2SJon Tollefson 			node_ar->nid = nid;
1468f64e1f2SJon Tollefson 			node_ar->start_pfn = start_pfn;
1475dfe8660STejun Heo 			node_ar->end_pfn = end_pfn;
1485dfe8660STejun Heo 			break;
1495dfe8660STejun Heo 		}
1505dfe8660STejun Heo 	}
1518f64e1f2SJon Tollefson }
1528f64e1f2SJon Tollefson 
15339bf990eSJesse Larrew static void map_cpu_to_node(int cpu, int node)
154ab1f9dacSPaul Mackerras {
155ab1f9dacSPaul Mackerras 	numa_cpu_lookup_table[cpu] = node;
15645fb6ceaSAnton Blanchard 
157bf4b85b0SNathan Lynch 	dbg("adding cpu %d to node %d\n", cpu, node);
158bf4b85b0SNathan Lynch 
15925863de0SAnton Blanchard 	if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
16025863de0SAnton Blanchard 		cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
161ab1f9dacSPaul Mackerras }
162ab1f9dacSPaul Mackerras 
16339bf990eSJesse Larrew #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
164ab1f9dacSPaul Mackerras static void unmap_cpu_from_node(unsigned long cpu)
165ab1f9dacSPaul Mackerras {
166ab1f9dacSPaul Mackerras 	int node = numa_cpu_lookup_table[cpu];
167ab1f9dacSPaul Mackerras 
168ab1f9dacSPaul Mackerras 	dbg("removing cpu %lu from node %d\n", cpu, node);
169ab1f9dacSPaul Mackerras 
17025863de0SAnton Blanchard 	if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
171429f4d8dSAnton Blanchard 		cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
172ab1f9dacSPaul Mackerras 	} else {
173ab1f9dacSPaul Mackerras 		printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
174ab1f9dacSPaul Mackerras 		       cpu, node);
175ab1f9dacSPaul Mackerras 	}
176ab1f9dacSPaul Mackerras }
17739bf990eSJesse Larrew #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
178ab1f9dacSPaul Mackerras 
179ab1f9dacSPaul Mackerras /* must hold reference to node during call */
180a7f67bdfSJeremy Kerr static const int *of_get_associativity(struct device_node *dev)
181ab1f9dacSPaul Mackerras {
182e2eb6392SStephen Rothwell 	return of_get_property(dev, "ibm,associativity", NULL);
183ab1f9dacSPaul Mackerras }
184ab1f9dacSPaul Mackerras 
185cf00085dSChandru /*
186cf00085dSChandru  * Returns the property linux,drconf-usable-memory if
187cf00085dSChandru  * it exists (the property exists only in kexec/kdump kernels,
188cf00085dSChandru  * added by kexec-tools)
189cf00085dSChandru  */
190cf00085dSChandru static const u32 *of_get_usable_memory(struct device_node *memory)
191cf00085dSChandru {
192cf00085dSChandru 	const u32 *prop;
193cf00085dSChandru 	u32 len;
194cf00085dSChandru 	prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
195cf00085dSChandru 	if (!prop || len < sizeof(unsigned int))
196cf00085dSChandru 		return 0;
197cf00085dSChandru 	return prop;
198cf00085dSChandru }
199cf00085dSChandru 
20041eab6f8SAnton Blanchard int __node_distance(int a, int b)
20141eab6f8SAnton Blanchard {
20241eab6f8SAnton Blanchard 	int i;
20341eab6f8SAnton Blanchard 	int distance = LOCAL_DISTANCE;
20441eab6f8SAnton Blanchard 
20541eab6f8SAnton Blanchard 	if (!form1_affinity)
2067122beeeSVaidyanathan Srinivasan 		return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
20741eab6f8SAnton Blanchard 
20841eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
20941eab6f8SAnton Blanchard 		if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
21041eab6f8SAnton Blanchard 			break;
21141eab6f8SAnton Blanchard 
21241eab6f8SAnton Blanchard 		/* Double the distance for each NUMA level */
21341eab6f8SAnton Blanchard 		distance *= 2;
21441eab6f8SAnton Blanchard 	}
21541eab6f8SAnton Blanchard 
21641eab6f8SAnton Blanchard 	return distance;
21741eab6f8SAnton Blanchard }
21841eab6f8SAnton Blanchard 
21941eab6f8SAnton Blanchard static void initialize_distance_lookup_table(int nid,
22041eab6f8SAnton Blanchard 		const unsigned int *associativity)
22141eab6f8SAnton Blanchard {
22241eab6f8SAnton Blanchard 	int i;
22341eab6f8SAnton Blanchard 
22441eab6f8SAnton Blanchard 	if (!form1_affinity)
22541eab6f8SAnton Blanchard 		return;
22641eab6f8SAnton Blanchard 
22741eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
22841eab6f8SAnton Blanchard 		distance_lookup_table[nid][i] =
22941eab6f8SAnton Blanchard 			associativity[distance_ref_points[i]];
23041eab6f8SAnton Blanchard 	}
23141eab6f8SAnton Blanchard }
23241eab6f8SAnton Blanchard 
233482ec7c4SNathan Lynch /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
234482ec7c4SNathan Lynch  * info is found.
235482ec7c4SNathan Lynch  */
2369eff1a38SJesse Larrew static int associativity_to_nid(const unsigned int *associativity)
237ab1f9dacSPaul Mackerras {
238482ec7c4SNathan Lynch 	int nid = -1;
239ab1f9dacSPaul Mackerras 
240ab1f9dacSPaul Mackerras 	if (min_common_depth == -1)
241482ec7c4SNathan Lynch 		goto out;
242ab1f9dacSPaul Mackerras 
2439eff1a38SJesse Larrew 	if (associativity[0] >= min_common_depth)
2449eff1a38SJesse Larrew 		nid = associativity[min_common_depth];
245bc16a759SNathan Lynch 
246bc16a759SNathan Lynch 	/* POWER4 LPAR uses 0xffff as invalid node */
247482ec7c4SNathan Lynch 	if (nid == 0xffff || nid >= MAX_NUMNODES)
248482ec7c4SNathan Lynch 		nid = -1;
24941eab6f8SAnton Blanchard 
2509eff1a38SJesse Larrew 	if (nid > 0 && associativity[0] >= distance_ref_points_depth)
2519eff1a38SJesse Larrew 		initialize_distance_lookup_table(nid, associativity);
25241eab6f8SAnton Blanchard 
253482ec7c4SNathan Lynch out:
254cf950b7aSNathan Lynch 	return nid;
255ab1f9dacSPaul Mackerras }
256ab1f9dacSPaul Mackerras 
2579eff1a38SJesse Larrew /* Returns the nid associated with the given device tree node,
2589eff1a38SJesse Larrew  * or -1 if not found.
2599eff1a38SJesse Larrew  */
2609eff1a38SJesse Larrew static int of_node_to_nid_single(struct device_node *device)
2619eff1a38SJesse Larrew {
2629eff1a38SJesse Larrew 	int nid = -1;
2639eff1a38SJesse Larrew 	const unsigned int *tmp;
2649eff1a38SJesse Larrew 
2659eff1a38SJesse Larrew 	tmp = of_get_associativity(device);
2669eff1a38SJesse Larrew 	if (tmp)
2679eff1a38SJesse Larrew 		nid = associativity_to_nid(tmp);
2689eff1a38SJesse Larrew 	return nid;
2699eff1a38SJesse Larrew }
2709eff1a38SJesse Larrew 
271953039c8SJeremy Kerr /* Walk the device tree upwards, looking for an associativity id */
272953039c8SJeremy Kerr int of_node_to_nid(struct device_node *device)
273953039c8SJeremy Kerr {
274953039c8SJeremy Kerr 	struct device_node *tmp;
275953039c8SJeremy Kerr 	int nid = -1;
276953039c8SJeremy Kerr 
277953039c8SJeremy Kerr 	of_node_get(device);
278953039c8SJeremy Kerr 	while (device) {
279953039c8SJeremy Kerr 		nid = of_node_to_nid_single(device);
280953039c8SJeremy Kerr 		if (nid != -1)
281953039c8SJeremy Kerr 			break;
282953039c8SJeremy Kerr 
283953039c8SJeremy Kerr 	        tmp = device;
284953039c8SJeremy Kerr 		device = of_get_parent(tmp);
285953039c8SJeremy Kerr 		of_node_put(tmp);
286953039c8SJeremy Kerr 	}
287953039c8SJeremy Kerr 	of_node_put(device);
288953039c8SJeremy Kerr 
289953039c8SJeremy Kerr 	return nid;
290953039c8SJeremy Kerr }
291953039c8SJeremy Kerr EXPORT_SYMBOL_GPL(of_node_to_nid);
292953039c8SJeremy Kerr 
293ab1f9dacSPaul Mackerras static int __init find_min_common_depth(void)
294ab1f9dacSPaul Mackerras {
29541eab6f8SAnton Blanchard 	int depth;
296e70606ebSMichael Ellerman 	struct device_node *root;
297ab1f9dacSPaul Mackerras 
2981c8ee733SDipankar Sarma 	if (firmware_has_feature(FW_FEATURE_OPAL))
2991c8ee733SDipankar Sarma 		root = of_find_node_by_path("/ibm,opal");
3001c8ee733SDipankar Sarma 	else
301e70606ebSMichael Ellerman 		root = of_find_node_by_path("/rtas");
302e70606ebSMichael Ellerman 	if (!root)
303e70606ebSMichael Ellerman 		root = of_find_node_by_path("/");
304ab1f9dacSPaul Mackerras 
305ab1f9dacSPaul Mackerras 	/*
30641eab6f8SAnton Blanchard 	 * This property is a set of 32-bit integers, each representing
30741eab6f8SAnton Blanchard 	 * an index into the ibm,associativity nodes.
30841eab6f8SAnton Blanchard 	 *
30941eab6f8SAnton Blanchard 	 * With form 0 affinity the first integer is for an SMP configuration
31041eab6f8SAnton Blanchard 	 * (should be all 0's) and the second is for a normal NUMA
31141eab6f8SAnton Blanchard 	 * configuration. We have only one level of NUMA.
31241eab6f8SAnton Blanchard 	 *
31341eab6f8SAnton Blanchard 	 * With form 1 affinity the first integer is the most significant
31441eab6f8SAnton Blanchard 	 * NUMA boundary and the following are progressively less significant
31541eab6f8SAnton Blanchard 	 * boundaries. There can be more than one level of NUMA.
316ab1f9dacSPaul Mackerras 	 */
317e70606ebSMichael Ellerman 	distance_ref_points = of_get_property(root,
31841eab6f8SAnton Blanchard 					"ibm,associativity-reference-points",
31941eab6f8SAnton Blanchard 					&distance_ref_points_depth);
320ab1f9dacSPaul Mackerras 
32141eab6f8SAnton Blanchard 	if (!distance_ref_points) {
32241eab6f8SAnton Blanchard 		dbg("NUMA: ibm,associativity-reference-points not found.\n");
32341eab6f8SAnton Blanchard 		goto err;
32441eab6f8SAnton Blanchard 	}
32541eab6f8SAnton Blanchard 
32641eab6f8SAnton Blanchard 	distance_ref_points_depth /= sizeof(int);
32741eab6f8SAnton Blanchard 
3288002b0c5SNathan Fontenot 	if (firmware_has_feature(FW_FEATURE_OPAL) ||
3298002b0c5SNathan Fontenot 	    firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
330bc8449ccSAnton Blanchard 		dbg("Using form 1 affinity\n");
33141eab6f8SAnton Blanchard 		form1_affinity = 1;
3324b83c330SAnton Blanchard 	}
3335b958a7eSGavin Shan 
33441eab6f8SAnton Blanchard 	if (form1_affinity) {
33541eab6f8SAnton Blanchard 		depth = distance_ref_points[0];
336ab1f9dacSPaul Mackerras 	} else {
33741eab6f8SAnton Blanchard 		if (distance_ref_points_depth < 2) {
33841eab6f8SAnton Blanchard 			printk(KERN_WARNING "NUMA: "
33941eab6f8SAnton Blanchard 				"short ibm,associativity-reference-points\n");
34041eab6f8SAnton Blanchard 			goto err;
341ab1f9dacSPaul Mackerras 		}
342ab1f9dacSPaul Mackerras 
34341eab6f8SAnton Blanchard 		depth = distance_ref_points[1];
34441eab6f8SAnton Blanchard 	}
34541eab6f8SAnton Blanchard 
34641eab6f8SAnton Blanchard 	/*
34741eab6f8SAnton Blanchard 	 * Warn and cap if the hardware supports more than
34841eab6f8SAnton Blanchard 	 * MAX_DISTANCE_REF_POINTS domains.
34941eab6f8SAnton Blanchard 	 */
35041eab6f8SAnton Blanchard 	if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
35141eab6f8SAnton Blanchard 		printk(KERN_WARNING "NUMA: distance array capped at "
35241eab6f8SAnton Blanchard 			"%d entries\n", MAX_DISTANCE_REF_POINTS);
35341eab6f8SAnton Blanchard 		distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
35441eab6f8SAnton Blanchard 	}
35541eab6f8SAnton Blanchard 
356e70606ebSMichael Ellerman 	of_node_put(root);
357ab1f9dacSPaul Mackerras 	return depth;
35841eab6f8SAnton Blanchard 
35941eab6f8SAnton Blanchard err:
360e70606ebSMichael Ellerman 	of_node_put(root);
36141eab6f8SAnton Blanchard 	return -1;
362ab1f9dacSPaul Mackerras }
363ab1f9dacSPaul Mackerras 
36484c9fdd1SMike Kravetz static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
365ab1f9dacSPaul Mackerras {
366ab1f9dacSPaul Mackerras 	struct device_node *memory = NULL;
367ab1f9dacSPaul Mackerras 
368ab1f9dacSPaul Mackerras 	memory = of_find_node_by_type(memory, "memory");
36954c23310SPaul Mackerras 	if (!memory)
37084c9fdd1SMike Kravetz 		panic("numa.c: No memory nodes found!");
37154c23310SPaul Mackerras 
372a8bda5ddSStephen Rothwell 	*n_addr_cells = of_n_addr_cells(memory);
3739213feeaSStephen Rothwell 	*n_size_cells = of_n_size_cells(memory);
37484c9fdd1SMike Kravetz 	of_node_put(memory);
375ab1f9dacSPaul Mackerras }
376ab1f9dacSPaul Mackerras 
3772011b1d0SDavid Rientjes static unsigned long read_n_cells(int n, const unsigned int **buf)
378ab1f9dacSPaul Mackerras {
379ab1f9dacSPaul Mackerras 	unsigned long result = 0;
380ab1f9dacSPaul Mackerras 
381ab1f9dacSPaul Mackerras 	while (n--) {
382ab1f9dacSPaul Mackerras 		result = (result << 32) | **buf;
383ab1f9dacSPaul Mackerras 		(*buf)++;
384ab1f9dacSPaul Mackerras 	}
385ab1f9dacSPaul Mackerras 	return result;
386ab1f9dacSPaul Mackerras }
387ab1f9dacSPaul Mackerras 
3888342681dSNathan Fontenot /*
38995f72d1eSYinghai Lu  * Read the next memblock list entry from the ibm,dynamic-memory property
3908342681dSNathan Fontenot  * and return the information in the provided of_drconf_cell structure.
3918342681dSNathan Fontenot  */
3928342681dSNathan Fontenot static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
3938342681dSNathan Fontenot {
3948342681dSNathan Fontenot 	const u32 *cp;
3958342681dSNathan Fontenot 
3968342681dSNathan Fontenot 	drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
3978342681dSNathan Fontenot 
3988342681dSNathan Fontenot 	cp = *cellp;
3998342681dSNathan Fontenot 	drmem->drc_index = cp[0];
4008342681dSNathan Fontenot 	drmem->reserved = cp[1];
4018342681dSNathan Fontenot 	drmem->aa_index = cp[2];
4028342681dSNathan Fontenot 	drmem->flags = cp[3];
4038342681dSNathan Fontenot 
4048342681dSNathan Fontenot 	*cellp = cp + 4;
4058342681dSNathan Fontenot }
4068342681dSNathan Fontenot 
4078342681dSNathan Fontenot /*
40825985edcSLucas De Marchi  * Retrieve and validate the ibm,dynamic-memory property of the device tree.
4098342681dSNathan Fontenot  *
41095f72d1eSYinghai Lu  * The layout of the ibm,dynamic-memory property is a number N of memblock
41195f72d1eSYinghai Lu  * list entries followed by N memblock list entries.  Each memblock list entry
41225985edcSLucas De Marchi  * contains information as laid out in the of_drconf_cell struct above.
4138342681dSNathan Fontenot  */
4148342681dSNathan Fontenot static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
4158342681dSNathan Fontenot {
4168342681dSNathan Fontenot 	const u32 *prop;
4178342681dSNathan Fontenot 	u32 len, entries;
4188342681dSNathan Fontenot 
4198342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,dynamic-memory", &len);
4208342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4218342681dSNathan Fontenot 		return 0;
4228342681dSNathan Fontenot 
4238342681dSNathan Fontenot 	entries = *prop++;
4248342681dSNathan Fontenot 
4258342681dSNathan Fontenot 	/* Now that we know the number of entries, revalidate the size
4268342681dSNathan Fontenot 	 * of the property read in to ensure we have everything
4278342681dSNathan Fontenot 	 */
4288342681dSNathan Fontenot 	if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
4298342681dSNathan Fontenot 		return 0;
4308342681dSNathan Fontenot 
4318342681dSNathan Fontenot 	*dm = prop;
4328342681dSNathan Fontenot 	return entries;
4338342681dSNathan Fontenot }
4348342681dSNathan Fontenot 
4358342681dSNathan Fontenot /*
43625985edcSLucas De Marchi  * Retrieve and validate the ibm,lmb-size property for drconf memory
4378342681dSNathan Fontenot  * from the device tree.
4388342681dSNathan Fontenot  */
4393fdfd990SBenjamin Herrenschmidt static u64 of_get_lmb_size(struct device_node *memory)
4408342681dSNathan Fontenot {
4418342681dSNathan Fontenot 	const u32 *prop;
4428342681dSNathan Fontenot 	u32 len;
4438342681dSNathan Fontenot 
4443fdfd990SBenjamin Herrenschmidt 	prop = of_get_property(memory, "ibm,lmb-size", &len);
4458342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4468342681dSNathan Fontenot 		return 0;
4478342681dSNathan Fontenot 
4488342681dSNathan Fontenot 	return read_n_cells(n_mem_size_cells, &prop);
4498342681dSNathan Fontenot }
4508342681dSNathan Fontenot 
4518342681dSNathan Fontenot struct assoc_arrays {
4528342681dSNathan Fontenot 	u32	n_arrays;
4538342681dSNathan Fontenot 	u32	array_sz;
4548342681dSNathan Fontenot 	const u32 *arrays;
4558342681dSNathan Fontenot };
4568342681dSNathan Fontenot 
4578342681dSNathan Fontenot /*
45825985edcSLucas De Marchi  * Retrieve and validate the list of associativity arrays for drconf
4598342681dSNathan Fontenot  * memory from the ibm,associativity-lookup-arrays property of the
4608342681dSNathan Fontenot  * device tree..
4618342681dSNathan Fontenot  *
4628342681dSNathan Fontenot  * The layout of the ibm,associativity-lookup-arrays property is a number N
4638342681dSNathan Fontenot  * indicating the number of associativity arrays, followed by a number M
4648342681dSNathan Fontenot  * indicating the size of each associativity array, followed by a list
4658342681dSNathan Fontenot  * of N associativity arrays.
4668342681dSNathan Fontenot  */
4678342681dSNathan Fontenot static int of_get_assoc_arrays(struct device_node *memory,
4688342681dSNathan Fontenot 			       struct assoc_arrays *aa)
4698342681dSNathan Fontenot {
4708342681dSNathan Fontenot 	const u32 *prop;
4718342681dSNathan Fontenot 	u32 len;
4728342681dSNathan Fontenot 
4738342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
4748342681dSNathan Fontenot 	if (!prop || len < 2 * sizeof(unsigned int))
4758342681dSNathan Fontenot 		return -1;
4768342681dSNathan Fontenot 
4778342681dSNathan Fontenot 	aa->n_arrays = *prop++;
4788342681dSNathan Fontenot 	aa->array_sz = *prop++;
4798342681dSNathan Fontenot 
48042b2aa86SJustin P. Mattock 	/* Now that we know the number of arrays and size of each array,
4818342681dSNathan Fontenot 	 * revalidate the size of the property read in.
4828342681dSNathan Fontenot 	 */
4838342681dSNathan Fontenot 	if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
4848342681dSNathan Fontenot 		return -1;
4858342681dSNathan Fontenot 
4868342681dSNathan Fontenot 	aa->arrays = prop;
4878342681dSNathan Fontenot 	return 0;
4888342681dSNathan Fontenot }
4898342681dSNathan Fontenot 
4908342681dSNathan Fontenot /*
4918342681dSNathan Fontenot  * This is like of_node_to_nid_single() for memory represented in the
4928342681dSNathan Fontenot  * ibm,dynamic-reconfiguration-memory node.
4938342681dSNathan Fontenot  */
4948342681dSNathan Fontenot static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
4958342681dSNathan Fontenot 				   struct assoc_arrays *aa)
4968342681dSNathan Fontenot {
4978342681dSNathan Fontenot 	int default_nid = 0;
4988342681dSNathan Fontenot 	int nid = default_nid;
4998342681dSNathan Fontenot 	int index;
5008342681dSNathan Fontenot 
5018342681dSNathan Fontenot 	if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
5028342681dSNathan Fontenot 	    !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
5038342681dSNathan Fontenot 	    drmem->aa_index < aa->n_arrays) {
5048342681dSNathan Fontenot 		index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
5058342681dSNathan Fontenot 		nid = aa->arrays[index];
5068342681dSNathan Fontenot 
5078342681dSNathan Fontenot 		if (nid == 0xffff || nid >= MAX_NUMNODES)
5088342681dSNathan Fontenot 			nid = default_nid;
5098342681dSNathan Fontenot 	}
5108342681dSNathan Fontenot 
5118342681dSNathan Fontenot 	return nid;
5128342681dSNathan Fontenot }
5138342681dSNathan Fontenot 
514ab1f9dacSPaul Mackerras /*
515ab1f9dacSPaul Mackerras  * Figure out to which domain a cpu belongs and stick it there.
516ab1f9dacSPaul Mackerras  * Return the id of the domain used.
517ab1f9dacSPaul Mackerras  */
5182e5ce39dSNathan Lynch static int __cpuinit numa_setup_cpu(unsigned long lcpu)
519ab1f9dacSPaul Mackerras {
520cf950b7aSNathan Lynch 	int nid = 0;
5218b16cd23SMilton Miller 	struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
522ab1f9dacSPaul Mackerras 
523ab1f9dacSPaul Mackerras 	if (!cpu) {
524ab1f9dacSPaul Mackerras 		WARN_ON(1);
525ab1f9dacSPaul Mackerras 		goto out;
526ab1f9dacSPaul Mackerras 	}
527ab1f9dacSPaul Mackerras 
528953039c8SJeremy Kerr 	nid = of_node_to_nid_single(cpu);
529ab1f9dacSPaul Mackerras 
530482ec7c4SNathan Lynch 	if (nid < 0 || !node_online(nid))
53172c33688SH Hartley Sweeten 		nid = first_online_node;
532ab1f9dacSPaul Mackerras out:
533cf950b7aSNathan Lynch 	map_cpu_to_node(lcpu, nid);
534ab1f9dacSPaul Mackerras 
535ab1f9dacSPaul Mackerras 	of_node_put(cpu);
536ab1f9dacSPaul Mackerras 
537cf950b7aSNathan Lynch 	return nid;
538ab1f9dacSPaul Mackerras }
539ab1f9dacSPaul Mackerras 
54074b85f37SChandra Seetharaman static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
541ab1f9dacSPaul Mackerras 			     unsigned long action,
542ab1f9dacSPaul Mackerras 			     void *hcpu)
543ab1f9dacSPaul Mackerras {
544ab1f9dacSPaul Mackerras 	unsigned long lcpu = (unsigned long)hcpu;
545ab1f9dacSPaul Mackerras 	int ret = NOTIFY_DONE;
546ab1f9dacSPaul Mackerras 
547ab1f9dacSPaul Mackerras 	switch (action) {
548ab1f9dacSPaul Mackerras 	case CPU_UP_PREPARE:
5498bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
550ab1f9dacSPaul Mackerras 		numa_setup_cpu(lcpu);
551ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
552ab1f9dacSPaul Mackerras 		break;
553ab1f9dacSPaul Mackerras #ifdef CONFIG_HOTPLUG_CPU
554ab1f9dacSPaul Mackerras 	case CPU_DEAD:
5558bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
556ab1f9dacSPaul Mackerras 	case CPU_UP_CANCELED:
5578bb78442SRafael J. Wysocki 	case CPU_UP_CANCELED_FROZEN:
558ab1f9dacSPaul Mackerras 		unmap_cpu_from_node(lcpu);
559ab1f9dacSPaul Mackerras 		break;
560ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
561ab1f9dacSPaul Mackerras #endif
562ab1f9dacSPaul Mackerras 	}
563ab1f9dacSPaul Mackerras 	return ret;
564ab1f9dacSPaul Mackerras }
565ab1f9dacSPaul Mackerras 
566ab1f9dacSPaul Mackerras /*
567ab1f9dacSPaul Mackerras  * Check and possibly modify a memory region to enforce the memory limit.
568ab1f9dacSPaul Mackerras  *
569ab1f9dacSPaul Mackerras  * Returns the size the region should have to enforce the memory limit.
570ab1f9dacSPaul Mackerras  * This will either be the original value of size, a truncated value,
571ab1f9dacSPaul Mackerras  * or zero. If the returned value of size is 0 the region should be
57225985edcSLucas De Marchi  * discarded as it lies wholly above the memory limit.
573ab1f9dacSPaul Mackerras  */
57445fb6ceaSAnton Blanchard static unsigned long __init numa_enforce_memory_limit(unsigned long start,
57545fb6ceaSAnton Blanchard 						      unsigned long size)
576ab1f9dacSPaul Mackerras {
577ab1f9dacSPaul Mackerras 	/*
57895f72d1eSYinghai Lu 	 * We use memblock_end_of_DRAM() in here instead of memory_limit because
579ab1f9dacSPaul Mackerras 	 * we've already adjusted it for the limit and it takes care of
580fe55249dSMilton Miller 	 * having memory holes below the limit.  Also, in the case of
581fe55249dSMilton Miller 	 * iommu_is_off, memory_limit is not set but is implicitly enforced.
582ab1f9dacSPaul Mackerras 	 */
583ab1f9dacSPaul Mackerras 
58495f72d1eSYinghai Lu 	if (start + size <= memblock_end_of_DRAM())
585ab1f9dacSPaul Mackerras 		return size;
586ab1f9dacSPaul Mackerras 
58795f72d1eSYinghai Lu 	if (start >= memblock_end_of_DRAM())
588ab1f9dacSPaul Mackerras 		return 0;
589ab1f9dacSPaul Mackerras 
59095f72d1eSYinghai Lu 	return memblock_end_of_DRAM() - start;
591ab1f9dacSPaul Mackerras }
592ab1f9dacSPaul Mackerras 
5930204568aSPaul Mackerras /*
594cf00085dSChandru  * Reads the counter for a given entry in
595cf00085dSChandru  * linux,drconf-usable-memory property
596cf00085dSChandru  */
597cf00085dSChandru static inline int __init read_usm_ranges(const u32 **usm)
598cf00085dSChandru {
599cf00085dSChandru 	/*
6003fdfd990SBenjamin Herrenschmidt 	 * For each lmb in ibm,dynamic-memory a corresponding
601cf00085dSChandru 	 * entry in linux,drconf-usable-memory property contains
602cf00085dSChandru 	 * a counter followed by that many (base, size) duple.
603cf00085dSChandru 	 * read the counter from linux,drconf-usable-memory
604cf00085dSChandru 	 */
605cf00085dSChandru 	return read_n_cells(n_mem_size_cells, usm);
606cf00085dSChandru }
607cf00085dSChandru 
608cf00085dSChandru /*
6090204568aSPaul Mackerras  * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
6100204568aSPaul Mackerras  * node.  This assumes n_mem_{addr,size}_cells have been set.
6110204568aSPaul Mackerras  */
6120204568aSPaul Mackerras static void __init parse_drconf_memory(struct device_node *memory)
6130204568aSPaul Mackerras {
61482b2521dSMichael Neuling 	const u32 *uninitialized_var(dm), *usm;
615cf00085dSChandru 	unsigned int n, rc, ranges, is_kexec_kdump = 0;
6163fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size, base, size, sz;
6178342681dSNathan Fontenot 	int nid;
618aa709f3bSBenjamin Herrenschmidt 	struct assoc_arrays aa = { .arrays = NULL };
6190204568aSPaul Mackerras 
6208342681dSNathan Fontenot 	n = of_get_drconf_memory(memory, &dm);
6218342681dSNathan Fontenot 	if (!n)
6220204568aSPaul Mackerras 		return;
6230204568aSPaul Mackerras 
6243fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
6253fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
6268342681dSNathan Fontenot 		return;
6278342681dSNathan Fontenot 
6288342681dSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
6298342681dSNathan Fontenot 	if (rc)
6300204568aSPaul Mackerras 		return;
6310204568aSPaul Mackerras 
632cf00085dSChandru 	/* check if this is a kexec/kdump kernel */
633cf00085dSChandru 	usm = of_get_usable_memory(memory);
634cf00085dSChandru 	if (usm != NULL)
635cf00085dSChandru 		is_kexec_kdump = 1;
636cf00085dSChandru 
6370204568aSPaul Mackerras 	for (; n != 0; --n) {
6388342681dSNathan Fontenot 		struct of_drconf_cell drmem;
6391daa6d08SBalbir Singh 
6408342681dSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
6418342681dSNathan Fontenot 
6428342681dSNathan Fontenot 		/* skip this block if the reserved bit is set in flags (0x80)
6438342681dSNathan Fontenot 		   or if the block is not assigned to this partition (0x8) */
6448342681dSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
6458342681dSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
6468342681dSNathan Fontenot 			continue;
6478342681dSNathan Fontenot 
648cf00085dSChandru 		base = drmem.base_addr;
6493fdfd990SBenjamin Herrenschmidt 		size = lmb_size;
650cf00085dSChandru 		ranges = 1;
6518342681dSNathan Fontenot 
652cf00085dSChandru 		if (is_kexec_kdump) {
653cf00085dSChandru 			ranges = read_usm_ranges(&usm);
654cf00085dSChandru 			if (!ranges) /* there are no (base, size) duple */
6550204568aSPaul Mackerras 				continue;
656cf00085dSChandru 		}
657cf00085dSChandru 		do {
658cf00085dSChandru 			if (is_kexec_kdump) {
659cf00085dSChandru 				base = read_n_cells(n_mem_addr_cells, &usm);
660cf00085dSChandru 				size = read_n_cells(n_mem_size_cells, &usm);
661cf00085dSChandru 			}
662cf00085dSChandru 			nid = of_drconf_to_nid_single(&drmem, &aa);
663cf00085dSChandru 			fake_numa_create_new_node(
664cf00085dSChandru 				((base + size) >> PAGE_SHIFT),
665cf00085dSChandru 					   &nid);
666cf00085dSChandru 			node_set_online(nid);
667cf00085dSChandru 			sz = numa_enforce_memory_limit(base, size);
668cf00085dSChandru 			if (sz)
6691d7cfe18STejun Heo 				memblock_set_node(base, sz, nid);
670cf00085dSChandru 		} while (--ranges);
6710204568aSPaul Mackerras 	}
6720204568aSPaul Mackerras }
6730204568aSPaul Mackerras 
674ab1f9dacSPaul Mackerras static int __init parse_numa_properties(void)
675ab1f9dacSPaul Mackerras {
67694db7c5eSAnton Blanchard 	struct device_node *memory;
677482ec7c4SNathan Lynch 	int default_nid = 0;
678ab1f9dacSPaul Mackerras 	unsigned long i;
679ab1f9dacSPaul Mackerras 
680ab1f9dacSPaul Mackerras 	if (numa_enabled == 0) {
681ab1f9dacSPaul Mackerras 		printk(KERN_WARNING "NUMA disabled by user\n");
682ab1f9dacSPaul Mackerras 		return -1;
683ab1f9dacSPaul Mackerras 	}
684ab1f9dacSPaul Mackerras 
685ab1f9dacSPaul Mackerras 	min_common_depth = find_min_common_depth();
686ab1f9dacSPaul Mackerras 
687ab1f9dacSPaul Mackerras 	if (min_common_depth < 0)
688ab1f9dacSPaul Mackerras 		return min_common_depth;
689ab1f9dacSPaul Mackerras 
690bf4b85b0SNathan Lynch 	dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
691bf4b85b0SNathan Lynch 
692ab1f9dacSPaul Mackerras 	/*
693482ec7c4SNathan Lynch 	 * Even though we connect cpus to numa domains later in SMP
694482ec7c4SNathan Lynch 	 * init, we need to know the node ids now. This is because
695482ec7c4SNathan Lynch 	 * each node to be onlined must have NODE_DATA etc backing it.
696ab1f9dacSPaul Mackerras 	 */
697482ec7c4SNathan Lynch 	for_each_present_cpu(i) {
698dfbe93a2SAnton Blanchard 		struct device_node *cpu;
699cf950b7aSNathan Lynch 		int nid;
700ab1f9dacSPaul Mackerras 
7018b16cd23SMilton Miller 		cpu = of_get_cpu_node(i, NULL);
702482ec7c4SNathan Lynch 		BUG_ON(!cpu);
703953039c8SJeremy Kerr 		nid = of_node_to_nid_single(cpu);
704ab1f9dacSPaul Mackerras 		of_node_put(cpu);
705ab1f9dacSPaul Mackerras 
706482ec7c4SNathan Lynch 		/*
707482ec7c4SNathan Lynch 		 * Don't fall back to default_nid yet -- we will plug
708482ec7c4SNathan Lynch 		 * cpus into nodes once the memory scan has discovered
709482ec7c4SNathan Lynch 		 * the topology.
710482ec7c4SNathan Lynch 		 */
711482ec7c4SNathan Lynch 		if (nid < 0)
712482ec7c4SNathan Lynch 			continue;
713482ec7c4SNathan Lynch 		node_set_online(nid);
714ab1f9dacSPaul Mackerras 	}
715ab1f9dacSPaul Mackerras 
716237a0989SMike Kravetz 	get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
71794db7c5eSAnton Blanchard 
71894db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
719ab1f9dacSPaul Mackerras 		unsigned long start;
720ab1f9dacSPaul Mackerras 		unsigned long size;
721cf950b7aSNathan Lynch 		int nid;
722ab1f9dacSPaul Mackerras 		int ranges;
723a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
724ab1f9dacSPaul Mackerras 		unsigned int len;
725ab1f9dacSPaul Mackerras 
726e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory,
727ba759485SMichael Ellerman 			"linux,usable-memory", &len);
728ba759485SMichael Ellerman 		if (!memcell_buf || len <= 0)
729e2eb6392SStephen Rothwell 			memcell_buf = of_get_property(memory, "reg", &len);
730ab1f9dacSPaul Mackerras 		if (!memcell_buf || len <= 0)
731ab1f9dacSPaul Mackerras 			continue;
732ab1f9dacSPaul Mackerras 
733cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
734cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
735ab1f9dacSPaul Mackerras new_range:
736ab1f9dacSPaul Mackerras 		/* these are order-sensitive, and modify the buffer pointer */
737237a0989SMike Kravetz 		start = read_n_cells(n_mem_addr_cells, &memcell_buf);
738237a0989SMike Kravetz 		size = read_n_cells(n_mem_size_cells, &memcell_buf);
739ab1f9dacSPaul Mackerras 
740482ec7c4SNathan Lynch 		/*
741482ec7c4SNathan Lynch 		 * Assumption: either all memory nodes or none will
742482ec7c4SNathan Lynch 		 * have associativity properties.  If none, then
743482ec7c4SNathan Lynch 		 * everything goes to default_nid.
744482ec7c4SNathan Lynch 		 */
745953039c8SJeremy Kerr 		nid = of_node_to_nid_single(memory);
746482ec7c4SNathan Lynch 		if (nid < 0)
747482ec7c4SNathan Lynch 			nid = default_nid;
7481daa6d08SBalbir Singh 
7491daa6d08SBalbir Singh 		fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
750482ec7c4SNathan Lynch 		node_set_online(nid);
751ab1f9dacSPaul Mackerras 
752ab1f9dacSPaul Mackerras 		if (!(size = numa_enforce_memory_limit(start, size))) {
753ab1f9dacSPaul Mackerras 			if (--ranges)
754ab1f9dacSPaul Mackerras 				goto new_range;
755ab1f9dacSPaul Mackerras 			else
756ab1f9dacSPaul Mackerras 				continue;
757ab1f9dacSPaul Mackerras 		}
758ab1f9dacSPaul Mackerras 
7591d7cfe18STejun Heo 		memblock_set_node(start, size, nid);
760ab1f9dacSPaul Mackerras 
761ab1f9dacSPaul Mackerras 		if (--ranges)
762ab1f9dacSPaul Mackerras 			goto new_range;
763ab1f9dacSPaul Mackerras 	}
764ab1f9dacSPaul Mackerras 
7650204568aSPaul Mackerras 	/*
766dfbe93a2SAnton Blanchard 	 * Now do the same thing for each MEMBLOCK listed in the
767dfbe93a2SAnton Blanchard 	 * ibm,dynamic-memory property in the
768dfbe93a2SAnton Blanchard 	 * ibm,dynamic-reconfiguration-memory node.
7690204568aSPaul Mackerras 	 */
7700204568aSPaul Mackerras 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
7710204568aSPaul Mackerras 	if (memory)
7720204568aSPaul Mackerras 		parse_drconf_memory(memory);
7730204568aSPaul Mackerras 
774ab1f9dacSPaul Mackerras 	return 0;
775ab1f9dacSPaul Mackerras }
776ab1f9dacSPaul Mackerras 
777ab1f9dacSPaul Mackerras static void __init setup_nonnuma(void)
778ab1f9dacSPaul Mackerras {
77995f72d1eSYinghai Lu 	unsigned long top_of_ram = memblock_end_of_DRAM();
78095f72d1eSYinghai Lu 	unsigned long total_ram = memblock_phys_mem_size();
781c67c3cb4SMel Gorman 	unsigned long start_pfn, end_pfn;
78228be7072SBenjamin Herrenschmidt 	unsigned int nid = 0;
78328be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
784ab1f9dacSPaul Mackerras 
785e110b281SOlof Johansson 	printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
786ab1f9dacSPaul Mackerras 	       top_of_ram, total_ram);
787e110b281SOlof Johansson 	printk(KERN_DEBUG "Memory hole size: %ldMB\n",
788ab1f9dacSPaul Mackerras 	       (top_of_ram - total_ram) >> 20);
789ab1f9dacSPaul Mackerras 
79028be7072SBenjamin Herrenschmidt 	for_each_memblock(memory, reg) {
791c7fc2de0SYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(reg);
792c7fc2de0SYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(reg);
7931daa6d08SBalbir Singh 
7941daa6d08SBalbir Singh 		fake_numa_create_new_node(end_pfn, &nid);
7951d7cfe18STejun Heo 		memblock_set_node(PFN_PHYS(start_pfn),
7961d7cfe18STejun Heo 				  PFN_PHYS(end_pfn - start_pfn), nid);
7971daa6d08SBalbir Singh 		node_set_online(nid);
798c67c3cb4SMel Gorman 	}
799ab1f9dacSPaul Mackerras }
800ab1f9dacSPaul Mackerras 
8014b703a23SAnton Blanchard void __init dump_numa_cpu_topology(void)
8024b703a23SAnton Blanchard {
8034b703a23SAnton Blanchard 	unsigned int node;
8044b703a23SAnton Blanchard 	unsigned int cpu, count;
8054b703a23SAnton Blanchard 
8064b703a23SAnton Blanchard 	if (min_common_depth == -1 || !numa_enabled)
8074b703a23SAnton Blanchard 		return;
8084b703a23SAnton Blanchard 
8094b703a23SAnton Blanchard 	for_each_online_node(node) {
810e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d CPUs:", node);
8114b703a23SAnton Blanchard 
8124b703a23SAnton Blanchard 		count = 0;
8134b703a23SAnton Blanchard 		/*
8144b703a23SAnton Blanchard 		 * If we used a CPU iterator here we would miss printing
8154b703a23SAnton Blanchard 		 * the holes in the cpumap.
8164b703a23SAnton Blanchard 		 */
81725863de0SAnton Blanchard 		for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
81825863de0SAnton Blanchard 			if (cpumask_test_cpu(cpu,
81925863de0SAnton Blanchard 					node_to_cpumask_map[node])) {
8204b703a23SAnton Blanchard 				if (count == 0)
8214b703a23SAnton Blanchard 					printk(" %u", cpu);
8224b703a23SAnton Blanchard 				++count;
8234b703a23SAnton Blanchard 			} else {
8244b703a23SAnton Blanchard 				if (count > 1)
8254b703a23SAnton Blanchard 					printk("-%u", cpu - 1);
8264b703a23SAnton Blanchard 				count = 0;
8274b703a23SAnton Blanchard 			}
8284b703a23SAnton Blanchard 		}
8294b703a23SAnton Blanchard 
8304b703a23SAnton Blanchard 		if (count > 1)
83125863de0SAnton Blanchard 			printk("-%u", nr_cpu_ids - 1);
8324b703a23SAnton Blanchard 		printk("\n");
8334b703a23SAnton Blanchard 	}
8344b703a23SAnton Blanchard }
8354b703a23SAnton Blanchard 
8364b703a23SAnton Blanchard static void __init dump_numa_memory_topology(void)
837ab1f9dacSPaul Mackerras {
838ab1f9dacSPaul Mackerras 	unsigned int node;
839ab1f9dacSPaul Mackerras 	unsigned int count;
840ab1f9dacSPaul Mackerras 
841ab1f9dacSPaul Mackerras 	if (min_common_depth == -1 || !numa_enabled)
842ab1f9dacSPaul Mackerras 		return;
843ab1f9dacSPaul Mackerras 
844ab1f9dacSPaul Mackerras 	for_each_online_node(node) {
845ab1f9dacSPaul Mackerras 		unsigned long i;
846ab1f9dacSPaul Mackerras 
847e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d Memory:", node);
848ab1f9dacSPaul Mackerras 
849ab1f9dacSPaul Mackerras 		count = 0;
850ab1f9dacSPaul Mackerras 
85195f72d1eSYinghai Lu 		for (i = 0; i < memblock_end_of_DRAM();
85245fb6ceaSAnton Blanchard 		     i += (1 << SECTION_SIZE_BITS)) {
85345fb6ceaSAnton Blanchard 			if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
854ab1f9dacSPaul Mackerras 				if (count == 0)
855ab1f9dacSPaul Mackerras 					printk(" 0x%lx", i);
856ab1f9dacSPaul Mackerras 				++count;
857ab1f9dacSPaul Mackerras 			} else {
858ab1f9dacSPaul Mackerras 				if (count > 0)
859ab1f9dacSPaul Mackerras 					printk("-0x%lx", i);
860ab1f9dacSPaul Mackerras 				count = 0;
861ab1f9dacSPaul Mackerras 			}
862ab1f9dacSPaul Mackerras 		}
863ab1f9dacSPaul Mackerras 
864ab1f9dacSPaul Mackerras 		if (count > 0)
865ab1f9dacSPaul Mackerras 			printk("-0x%lx", i);
866ab1f9dacSPaul Mackerras 		printk("\n");
867ab1f9dacSPaul Mackerras 	}
868ab1f9dacSPaul Mackerras }
869ab1f9dacSPaul Mackerras 
870ab1f9dacSPaul Mackerras /*
87195f72d1eSYinghai Lu  * Allocate some memory, satisfying the memblock or bootmem allocator where
872ab1f9dacSPaul Mackerras  * required. nid is the preferred node and end is the physical address of
873ab1f9dacSPaul Mackerras  * the highest address in the node.
874ab1f9dacSPaul Mackerras  *
8750be210fdSDave Hansen  * Returns the virtual address of the memory.
876ab1f9dacSPaul Mackerras  */
877893473dfSDave Hansen static void __init *careful_zallocation(int nid, unsigned long size,
87845fb6ceaSAnton Blanchard 				       unsigned long align,
87945fb6ceaSAnton Blanchard 				       unsigned long end_pfn)
880ab1f9dacSPaul Mackerras {
8810be210fdSDave Hansen 	void *ret;
88245fb6ceaSAnton Blanchard 	int new_nid;
8830be210fdSDave Hansen 	unsigned long ret_paddr;
8840be210fdSDave Hansen 
88595f72d1eSYinghai Lu 	ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
886ab1f9dacSPaul Mackerras 
887ab1f9dacSPaul Mackerras 	/* retry over all memory */
8880be210fdSDave Hansen 	if (!ret_paddr)
88995f72d1eSYinghai Lu 		ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
890ab1f9dacSPaul Mackerras 
8910be210fdSDave Hansen 	if (!ret_paddr)
8925d21ea2bSDave Hansen 		panic("numa.c: cannot allocate %lu bytes for node %d",
893ab1f9dacSPaul Mackerras 		      size, nid);
894ab1f9dacSPaul Mackerras 
8950be210fdSDave Hansen 	ret = __va(ret_paddr);
8960be210fdSDave Hansen 
897ab1f9dacSPaul Mackerras 	/*
898c555e520SDave Hansen 	 * We initialize the nodes in numeric order: 0, 1, 2...
89995f72d1eSYinghai Lu 	 * and hand over control from the MEMBLOCK allocator to the
900c555e520SDave Hansen 	 * bootmem allocator.  If this function is called for
901c555e520SDave Hansen 	 * node 5, then we know that all nodes <5 are using the
90295f72d1eSYinghai Lu 	 * bootmem allocator instead of the MEMBLOCK allocator.
903c555e520SDave Hansen 	 *
904c555e520SDave Hansen 	 * So, check the nid from which this allocation came
905c555e520SDave Hansen 	 * and double check to see if we need to use bootmem
90695f72d1eSYinghai Lu 	 * instead of the MEMBLOCK.  We don't free the MEMBLOCK memory
907c555e520SDave Hansen 	 * since it would be useless.
908ab1f9dacSPaul Mackerras 	 */
9090be210fdSDave Hansen 	new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
91045fb6ceaSAnton Blanchard 	if (new_nid < nid) {
9110be210fdSDave Hansen 		ret = __alloc_bootmem_node(NODE_DATA(new_nid),
912ab1f9dacSPaul Mackerras 				size, align, 0);
913ab1f9dacSPaul Mackerras 
9140be210fdSDave Hansen 		dbg("alloc_bootmem %p %lx\n", ret, size);
915ab1f9dacSPaul Mackerras 	}
916ab1f9dacSPaul Mackerras 
917893473dfSDave Hansen 	memset(ret, 0, size);
9180be210fdSDave Hansen 	return ret;
919ab1f9dacSPaul Mackerras }
920ab1f9dacSPaul Mackerras 
92174b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata ppc64_numa_nb = {
92274b85f37SChandra Seetharaman 	.notifier_call = cpu_numa_callback,
92374b85f37SChandra Seetharaman 	.priority = 1 /* Must run before sched domains notifier. */
92474b85f37SChandra Seetharaman };
92574b85f37SChandra Seetharaman 
92628e86bdbSDavid Rientjes static void __init mark_reserved_regions_for_nid(int nid)
927ab1f9dacSPaul Mackerras {
9284a618669SDave Hansen 	struct pglist_data *node = NODE_DATA(nid);
92928be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
930ab1f9dacSPaul Mackerras 
93128be7072SBenjamin Herrenschmidt 	for_each_memblock(reserved, reg) {
93228be7072SBenjamin Herrenschmidt 		unsigned long physbase = reg->base;
93328be7072SBenjamin Herrenschmidt 		unsigned long size = reg->size;
9348f64e1f2SJon Tollefson 		unsigned long start_pfn = physbase >> PAGE_SHIFT;
93506eccea6SDave Hansen 		unsigned long end_pfn = PFN_UP(physbase + size);
9368f64e1f2SJon Tollefson 		struct node_active_region node_ar;
9374a618669SDave Hansen 		unsigned long node_end_pfn = node->node_start_pfn +
9384a618669SDave Hansen 					     node->node_spanned_pages;
9394a618669SDave Hansen 
9404a618669SDave Hansen 		/*
94195f72d1eSYinghai Lu 		 * Check to make sure that this memblock.reserved area is
9424a618669SDave Hansen 		 * within the bounds of the node that we care about.
9434a618669SDave Hansen 		 * Checking the nid of the start and end points is not
9444a618669SDave Hansen 		 * sufficient because the reserved area could span the
9454a618669SDave Hansen 		 * entire node.
9464a618669SDave Hansen 		 */
9474a618669SDave Hansen 		if (end_pfn <= node->node_start_pfn ||
9484a618669SDave Hansen 		    start_pfn >= node_end_pfn)
9494a618669SDave Hansen 			continue;
950ab1f9dacSPaul Mackerras 
9518f64e1f2SJon Tollefson 		get_node_active_region(start_pfn, &node_ar);
952e8170372SJon Tollefson 		while (start_pfn < end_pfn &&
953e8170372SJon Tollefson 			node_ar.start_pfn < node_ar.end_pfn) {
954e8170372SJon Tollefson 			unsigned long reserve_size = size;
9558f64e1f2SJon Tollefson 			/*
9568f64e1f2SJon Tollefson 			 * if reserved region extends past active region
9578f64e1f2SJon Tollefson 			 * then trim size to active region
9588f64e1f2SJon Tollefson 			 */
9598f64e1f2SJon Tollefson 			if (end_pfn > node_ar.end_pfn)
960e8170372SJon Tollefson 				reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
96106eccea6SDave Hansen 					- physbase;
962a4c74dddSDave Hansen 			/*
963a4c74dddSDave Hansen 			 * Only worry about *this* node, others may not
964a4c74dddSDave Hansen 			 * yet have valid NODE_DATA().
965a4c74dddSDave Hansen 			 */
966a4c74dddSDave Hansen 			if (node_ar.nid == nid) {
967a4c74dddSDave Hansen 				dbg("reserve_bootmem %lx %lx nid=%d\n",
968a4c74dddSDave Hansen 					physbase, reserve_size, node_ar.nid);
969a4c74dddSDave Hansen 				reserve_bootmem_node(NODE_DATA(node_ar.nid),
970a4c74dddSDave Hansen 						physbase, reserve_size,
971a4c74dddSDave Hansen 						BOOTMEM_DEFAULT);
972a4c74dddSDave Hansen 			}
9738f64e1f2SJon Tollefson 			/*
9748f64e1f2SJon Tollefson 			 * if reserved region is contained in the active region
9758f64e1f2SJon Tollefson 			 * then done.
9768f64e1f2SJon Tollefson 			 */
9778f64e1f2SJon Tollefson 			if (end_pfn <= node_ar.end_pfn)
9788f64e1f2SJon Tollefson 				break;
9798f64e1f2SJon Tollefson 
9808f64e1f2SJon Tollefson 			/*
9818f64e1f2SJon Tollefson 			 * reserved region extends past the active region
9828f64e1f2SJon Tollefson 			 *   get next active region that contains this
9838f64e1f2SJon Tollefson 			 *   reserved region
9848f64e1f2SJon Tollefson 			 */
9858f64e1f2SJon Tollefson 			start_pfn = node_ar.end_pfn;
9868f64e1f2SJon Tollefson 			physbase = start_pfn << PAGE_SHIFT;
987e8170372SJon Tollefson 			size = size - reserve_size;
9888f64e1f2SJon Tollefson 			get_node_active_region(start_pfn, &node_ar);
989ab1f9dacSPaul Mackerras 		}
9904a618669SDave Hansen 	}
991ab1f9dacSPaul Mackerras }
9928f64e1f2SJon Tollefson 
9934a618669SDave Hansen 
9944a618669SDave Hansen void __init do_init_bootmem(void)
9954a618669SDave Hansen {
9964a618669SDave Hansen 	int nid;
9974a618669SDave Hansen 
9984a618669SDave Hansen 	min_low_pfn = 0;
99995f72d1eSYinghai Lu 	max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
10004a618669SDave Hansen 	max_pfn = max_low_pfn;
10014a618669SDave Hansen 
10024a618669SDave Hansen 	if (parse_numa_properties())
10034a618669SDave Hansen 		setup_nonnuma();
10044a618669SDave Hansen 	else
10054a618669SDave Hansen 		dump_numa_memory_topology();
10064a618669SDave Hansen 
10074a618669SDave Hansen 	for_each_online_node(nid) {
10084a618669SDave Hansen 		unsigned long start_pfn, end_pfn;
10090be210fdSDave Hansen 		void *bootmem_vaddr;
10104a618669SDave Hansen 		unsigned long bootmap_pages;
10114a618669SDave Hansen 
10124a618669SDave Hansen 		get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
10134a618669SDave Hansen 
10144a618669SDave Hansen 		/*
10154a618669SDave Hansen 		 * Allocate the node structure node local if possible
10164a618669SDave Hansen 		 *
10174a618669SDave Hansen 		 * Be careful moving this around, as it relies on all
10184a618669SDave Hansen 		 * previous nodes' bootmem to be initialized and have
10194a618669SDave Hansen 		 * all reserved areas marked.
10204a618669SDave Hansen 		 */
1021893473dfSDave Hansen 		NODE_DATA(nid) = careful_zallocation(nid,
10224a618669SDave Hansen 					sizeof(struct pglist_data),
10234a618669SDave Hansen 					SMP_CACHE_BYTES, end_pfn);
10244a618669SDave Hansen 
10254a618669SDave Hansen   		dbg("node %d\n", nid);
10264a618669SDave Hansen 		dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
10274a618669SDave Hansen 
10284a618669SDave Hansen 		NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
10294a618669SDave Hansen 		NODE_DATA(nid)->node_start_pfn = start_pfn;
10304a618669SDave Hansen 		NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
10314a618669SDave Hansen 
10324a618669SDave Hansen 		if (NODE_DATA(nid)->node_spanned_pages == 0)
10334a618669SDave Hansen   			continue;
10344a618669SDave Hansen 
10354a618669SDave Hansen   		dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
10364a618669SDave Hansen   		dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
10374a618669SDave Hansen 
10384a618669SDave Hansen 		bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
1039893473dfSDave Hansen 		bootmem_vaddr = careful_zallocation(nid,
10404a618669SDave Hansen 					bootmap_pages << PAGE_SHIFT,
10414a618669SDave Hansen 					PAGE_SIZE, end_pfn);
10424a618669SDave Hansen 
10430be210fdSDave Hansen 		dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
10444a618669SDave Hansen 
10450be210fdSDave Hansen 		init_bootmem_node(NODE_DATA(nid),
10460be210fdSDave Hansen 				  __pa(bootmem_vaddr) >> PAGE_SHIFT,
10474a618669SDave Hansen 				  start_pfn, end_pfn);
10484a618669SDave Hansen 
10494a618669SDave Hansen 		free_bootmem_with_active_regions(nid, end_pfn);
10504a618669SDave Hansen 		/*
10514a618669SDave Hansen 		 * Be very careful about moving this around.  Future
1052893473dfSDave Hansen 		 * calls to careful_zallocation() depend on this getting
10534a618669SDave Hansen 		 * done correctly.
10544a618669SDave Hansen 		 */
10554a618669SDave Hansen 		mark_reserved_regions_for_nid(nid);
10568f64e1f2SJon Tollefson 		sparse_memory_present_with_active_regions(nid);
1057ab1f9dacSPaul Mackerras 	}
1058d3f6204aSBenjamin Herrenschmidt 
1059d3f6204aSBenjamin Herrenschmidt 	init_bootmem_done = 1;
106025863de0SAnton Blanchard 
106125863de0SAnton Blanchard 	/*
106225863de0SAnton Blanchard 	 * Now bootmem is initialised we can create the node to cpumask
106325863de0SAnton Blanchard 	 * lookup tables and setup the cpu callback to populate them.
106425863de0SAnton Blanchard 	 */
106525863de0SAnton Blanchard 	setup_node_to_cpumask_map();
106625863de0SAnton Blanchard 
106725863de0SAnton Blanchard 	register_cpu_notifier(&ppc64_numa_nb);
106825863de0SAnton Blanchard 	cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
106925863de0SAnton Blanchard 			  (void *)(unsigned long)boot_cpuid);
10704a618669SDave Hansen }
1071ab1f9dacSPaul Mackerras 
1072ab1f9dacSPaul Mackerras void __init paging_init(void)
1073ab1f9dacSPaul Mackerras {
10746391af17SMel Gorman 	unsigned long max_zone_pfns[MAX_NR_ZONES];
10756391af17SMel Gorman 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
107695f72d1eSYinghai Lu 	max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
1077c67c3cb4SMel Gorman 	free_area_init_nodes(max_zone_pfns);
1078ab1f9dacSPaul Mackerras }
1079ab1f9dacSPaul Mackerras 
1080ab1f9dacSPaul Mackerras static int __init early_numa(char *p)
1081ab1f9dacSPaul Mackerras {
1082ab1f9dacSPaul Mackerras 	if (!p)
1083ab1f9dacSPaul Mackerras 		return 0;
1084ab1f9dacSPaul Mackerras 
1085ab1f9dacSPaul Mackerras 	if (strstr(p, "off"))
1086ab1f9dacSPaul Mackerras 		numa_enabled = 0;
1087ab1f9dacSPaul Mackerras 
1088ab1f9dacSPaul Mackerras 	if (strstr(p, "debug"))
1089ab1f9dacSPaul Mackerras 		numa_debug = 1;
1090ab1f9dacSPaul Mackerras 
10911daa6d08SBalbir Singh 	p = strstr(p, "fake=");
10921daa6d08SBalbir Singh 	if (p)
10931daa6d08SBalbir Singh 		cmdline = p + strlen("fake=");
10941daa6d08SBalbir Singh 
1095ab1f9dacSPaul Mackerras 	return 0;
1096ab1f9dacSPaul Mackerras }
1097ab1f9dacSPaul Mackerras early_param("numa", early_numa);
1098237a0989SMike Kravetz 
1099237a0989SMike Kravetz #ifdef CONFIG_MEMORY_HOTPLUG
1100237a0989SMike Kravetz /*
11010f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for
11020f16ef7fSNathan Fontenot  * memory represented in the device tree by the property
11030f16ef7fSNathan Fontenot  * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
11040db9360aSNathan Fontenot  */
11050db9360aSNathan Fontenot static int hot_add_drconf_scn_to_nid(struct device_node *memory,
11060db9360aSNathan Fontenot 				     unsigned long scn_addr)
11070db9360aSNathan Fontenot {
11080db9360aSNathan Fontenot 	const u32 *dm;
11090f16ef7fSNathan Fontenot 	unsigned int drconf_cell_cnt, rc;
11103fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size;
11110db9360aSNathan Fontenot 	struct assoc_arrays aa;
11120f16ef7fSNathan Fontenot 	int nid = -1;
11130db9360aSNathan Fontenot 
11140f16ef7fSNathan Fontenot 	drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
11150f16ef7fSNathan Fontenot 	if (!drconf_cell_cnt)
11160f16ef7fSNathan Fontenot 		return -1;
11170db9360aSNathan Fontenot 
11183fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
11193fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
11200f16ef7fSNathan Fontenot 		return -1;
11210db9360aSNathan Fontenot 
11220db9360aSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
11230db9360aSNathan Fontenot 	if (rc)
11240f16ef7fSNathan Fontenot 		return -1;
11250db9360aSNathan Fontenot 
11260f16ef7fSNathan Fontenot 	for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
11270db9360aSNathan Fontenot 		struct of_drconf_cell drmem;
11280db9360aSNathan Fontenot 
11290db9360aSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
11300db9360aSNathan Fontenot 
11310db9360aSNathan Fontenot 		/* skip this block if it is reserved or not assigned to
11320db9360aSNathan Fontenot 		 * this partition */
11330db9360aSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
11340db9360aSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
11350db9360aSNathan Fontenot 			continue;
11360db9360aSNathan Fontenot 
11370f16ef7fSNathan Fontenot 		if ((scn_addr < drmem.base_addr)
11383fdfd990SBenjamin Herrenschmidt 		    || (scn_addr >= (drmem.base_addr + lmb_size)))
11390f16ef7fSNathan Fontenot 			continue;
11400db9360aSNathan Fontenot 
11410f16ef7fSNathan Fontenot 		nid = of_drconf_to_nid_single(&drmem, &aa);
11420f16ef7fSNathan Fontenot 		break;
11430db9360aSNathan Fontenot 	}
11440db9360aSNathan Fontenot 
11450f16ef7fSNathan Fontenot 	return nid;
11460db9360aSNathan Fontenot }
11470db9360aSNathan Fontenot 
11480db9360aSNathan Fontenot /*
11490f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for memory
11500f16ef7fSNathan Fontenot  * represented in the device tree as a node (i.e. memory@XXXX) for
115195f72d1eSYinghai Lu  * each memblock.
1152237a0989SMike Kravetz  */
11530f16ef7fSNathan Fontenot int hot_add_node_scn_to_nid(unsigned long scn_addr)
1154237a0989SMike Kravetz {
115594db7c5eSAnton Blanchard 	struct device_node *memory;
11560f16ef7fSNathan Fontenot 	int nid = -1;
1157237a0989SMike Kravetz 
115894db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
1159237a0989SMike Kravetz 		unsigned long start, size;
1160b226e462SMike Kravetz 		int ranges;
1161a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
1162237a0989SMike Kravetz 		unsigned int len;
1163237a0989SMike Kravetz 
1164e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory, "reg", &len);
1165237a0989SMike Kravetz 		if (!memcell_buf || len <= 0)
1166237a0989SMike Kravetz 			continue;
1167237a0989SMike Kravetz 
1168cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
1169cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
11700f16ef7fSNathan Fontenot 
11710f16ef7fSNathan Fontenot 		while (ranges--) {
1172237a0989SMike Kravetz 			start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1173237a0989SMike Kravetz 			size = read_n_cells(n_mem_size_cells, &memcell_buf);
1174237a0989SMike Kravetz 
11750f16ef7fSNathan Fontenot 			if ((scn_addr < start) || (scn_addr >= (start + size)))
11760f16ef7fSNathan Fontenot 				continue;
11770f16ef7fSNathan Fontenot 
11780f16ef7fSNathan Fontenot 			nid = of_node_to_nid_single(memory);
11790f16ef7fSNathan Fontenot 			break;
11800f16ef7fSNathan Fontenot 		}
11810f16ef7fSNathan Fontenot 
11820f16ef7fSNathan Fontenot 		if (nid >= 0)
11830f16ef7fSNathan Fontenot 			break;
11840f16ef7fSNathan Fontenot 	}
11850f16ef7fSNathan Fontenot 
118660831842SAnton Blanchard 	of_node_put(memory);
118760831842SAnton Blanchard 
11880db9360aSNathan Fontenot 	return nid;
1189237a0989SMike Kravetz }
1190237a0989SMike Kravetz 
11910f16ef7fSNathan Fontenot /*
11920f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section.  Section
119395f72d1eSYinghai Lu  * corresponds to a SPARSEMEM section, not an MEMBLOCK.  It is assumed that
119495f72d1eSYinghai Lu  * sections are fully contained within a single MEMBLOCK.
11950f16ef7fSNathan Fontenot  */
11960f16ef7fSNathan Fontenot int hot_add_scn_to_nid(unsigned long scn_addr)
11970f16ef7fSNathan Fontenot {
11980f16ef7fSNathan Fontenot 	struct device_node *memory = NULL;
11990f16ef7fSNathan Fontenot 	int nid, found = 0;
12000f16ef7fSNathan Fontenot 
12010f16ef7fSNathan Fontenot 	if (!numa_enabled || (min_common_depth < 0))
120272c33688SH Hartley Sweeten 		return first_online_node;
12030f16ef7fSNathan Fontenot 
12040f16ef7fSNathan Fontenot 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
12050f16ef7fSNathan Fontenot 	if (memory) {
12060f16ef7fSNathan Fontenot 		nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
12070f16ef7fSNathan Fontenot 		of_node_put(memory);
12080f16ef7fSNathan Fontenot 	} else {
12090f16ef7fSNathan Fontenot 		nid = hot_add_node_scn_to_nid(scn_addr);
1210237a0989SMike Kravetz 	}
12110f16ef7fSNathan Fontenot 
12120f16ef7fSNathan Fontenot 	if (nid < 0 || !node_online(nid))
121372c33688SH Hartley Sweeten 		nid = first_online_node;
12140f16ef7fSNathan Fontenot 
12150f16ef7fSNathan Fontenot 	if (NODE_DATA(nid)->node_spanned_pages)
12160f16ef7fSNathan Fontenot 		return nid;
12170f16ef7fSNathan Fontenot 
12180f16ef7fSNathan Fontenot 	for_each_online_node(nid) {
12190f16ef7fSNathan Fontenot 		if (NODE_DATA(nid)->node_spanned_pages) {
12200f16ef7fSNathan Fontenot 			found = 1;
12210f16ef7fSNathan Fontenot 			break;
1222237a0989SMike Kravetz 		}
12230f16ef7fSNathan Fontenot 	}
12240f16ef7fSNathan Fontenot 
12250f16ef7fSNathan Fontenot 	BUG_ON(!found);
12260f16ef7fSNathan Fontenot 	return nid;
12270f16ef7fSNathan Fontenot }
12280f16ef7fSNathan Fontenot 
1229cd34206eSNishanth Aravamudan static u64 hot_add_drconf_memory_max(void)
1230cd34206eSNishanth Aravamudan {
1231cd34206eSNishanth Aravamudan         struct device_node *memory = NULL;
1232cd34206eSNishanth Aravamudan         unsigned int drconf_cell_cnt = 0;
1233cd34206eSNishanth Aravamudan         u64 lmb_size = 0;
1234cd34206eSNishanth Aravamudan         const u32 *dm = 0;
1235cd34206eSNishanth Aravamudan 
1236cd34206eSNishanth Aravamudan         memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1237cd34206eSNishanth Aravamudan         if (memory) {
1238cd34206eSNishanth Aravamudan                 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1239cd34206eSNishanth Aravamudan                 lmb_size = of_get_lmb_size(memory);
1240cd34206eSNishanth Aravamudan                 of_node_put(memory);
1241cd34206eSNishanth Aravamudan         }
1242cd34206eSNishanth Aravamudan         return lmb_size * drconf_cell_cnt;
1243cd34206eSNishanth Aravamudan }
1244cd34206eSNishanth Aravamudan 
1245cd34206eSNishanth Aravamudan /*
1246cd34206eSNishanth Aravamudan  * memory_hotplug_max - return max address of memory that may be added
1247cd34206eSNishanth Aravamudan  *
1248cd34206eSNishanth Aravamudan  * This is currently only used on systems that support drconfig memory
1249cd34206eSNishanth Aravamudan  * hotplug.
1250cd34206eSNishanth Aravamudan  */
1251cd34206eSNishanth Aravamudan u64 memory_hotplug_max(void)
1252cd34206eSNishanth Aravamudan {
1253cd34206eSNishanth Aravamudan         return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1254cd34206eSNishanth Aravamudan }
1255237a0989SMike Kravetz #endif /* CONFIG_MEMORY_HOTPLUG */
12569eff1a38SJesse Larrew 
1257bd03403aSJesse Larrew /* Virtual Processor Home Node (VPHN) support */
125839bf990eSJesse Larrew #ifdef CONFIG_PPC_SPLPAR
125930c05350SNathan Fontenot struct topology_update_data {
126030c05350SNathan Fontenot 	struct topology_update_data *next;
126130c05350SNathan Fontenot 	unsigned int cpu;
126230c05350SNathan Fontenot 	int old_nid;
126330c05350SNathan Fontenot 	int new_nid;
126430c05350SNathan Fontenot };
126530c05350SNathan Fontenot 
12665de16699SAnton Blanchard static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
12679eff1a38SJesse Larrew static cpumask_t cpu_associativity_changes_mask;
12689eff1a38SJesse Larrew static int vphn_enabled;
12695d88aa85SJesse Larrew static int prrn_enabled;
12705d88aa85SJesse Larrew static void reset_topology_timer(void);
12719eff1a38SJesse Larrew 
12729eff1a38SJesse Larrew /*
12739eff1a38SJesse Larrew  * Store the current values of the associativity change counters in the
12749eff1a38SJesse Larrew  * hypervisor.
12759eff1a38SJesse Larrew  */
12769eff1a38SJesse Larrew static void setup_cpu_associativity_change_counters(void)
12779eff1a38SJesse Larrew {
1278cd9d6cc7SJesse Larrew 	int cpu;
12799eff1a38SJesse Larrew 
12805de16699SAnton Blanchard 	/* The VPHN feature supports a maximum of 8 reference points */
12815de16699SAnton Blanchard 	BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
12825de16699SAnton Blanchard 
12839eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
1284cd9d6cc7SJesse Larrew 		int i;
12859eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
12869eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
12879eff1a38SJesse Larrew 
12885de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++)
12899eff1a38SJesse Larrew 			counts[i] = hypervisor_counts[i];
12909eff1a38SJesse Larrew 	}
12919eff1a38SJesse Larrew }
12929eff1a38SJesse Larrew 
12939eff1a38SJesse Larrew /*
12949eff1a38SJesse Larrew  * The hypervisor maintains a set of 8 associativity change counters in
12959eff1a38SJesse Larrew  * the VPA of each cpu that correspond to the associativity levels in the
12969eff1a38SJesse Larrew  * ibm,associativity-reference-points property. When an associativity
12979eff1a38SJesse Larrew  * level changes, the corresponding counter is incremented.
12989eff1a38SJesse Larrew  *
12999eff1a38SJesse Larrew  * Set a bit in cpu_associativity_changes_mask for each cpu whose home
13009eff1a38SJesse Larrew  * node associativity levels have changed.
13019eff1a38SJesse Larrew  *
13029eff1a38SJesse Larrew  * Returns the number of cpus with unhandled associativity changes.
13039eff1a38SJesse Larrew  */
13049eff1a38SJesse Larrew static int update_cpu_associativity_changes_mask(void)
13059eff1a38SJesse Larrew {
13065d88aa85SJesse Larrew 	int cpu;
13079eff1a38SJesse Larrew 	cpumask_t *changes = &cpu_associativity_changes_mask;
13089eff1a38SJesse Larrew 
13099eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
13109eff1a38SJesse Larrew 		int i, changed = 0;
13119eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
13129eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
13139eff1a38SJesse Larrew 
13145de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++) {
1315d69043e8SAnton Blanchard 			if (hypervisor_counts[i] != counts[i]) {
13169eff1a38SJesse Larrew 				counts[i] = hypervisor_counts[i];
13179eff1a38SJesse Larrew 				changed = 1;
13189eff1a38SJesse Larrew 			}
13199eff1a38SJesse Larrew 		}
13209eff1a38SJesse Larrew 		if (changed) {
13219eff1a38SJesse Larrew 			cpumask_set_cpu(cpu, changes);
13229eff1a38SJesse Larrew 		}
13239eff1a38SJesse Larrew 	}
13249eff1a38SJesse Larrew 
13255d88aa85SJesse Larrew 	return cpumask_weight(changes);
13269eff1a38SJesse Larrew }
13279eff1a38SJesse Larrew 
1328c0e5e46fSAnton Blanchard /*
1329c0e5e46fSAnton Blanchard  * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1330c0e5e46fSAnton Blanchard  * the complete property we have to add the length in the first cell.
1331c0e5e46fSAnton Blanchard  */
1332c0e5e46fSAnton Blanchard #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
13339eff1a38SJesse Larrew 
13349eff1a38SJesse Larrew /*
13359eff1a38SJesse Larrew  * Convert the associativity domain numbers returned from the hypervisor
13369eff1a38SJesse Larrew  * to the sequence they would appear in the ibm,associativity property.
13379eff1a38SJesse Larrew  */
13389eff1a38SJesse Larrew static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
13399eff1a38SJesse Larrew {
1340cd9d6cc7SJesse Larrew 	int i, nr_assoc_doms = 0;
13419eff1a38SJesse Larrew 	const u16 *field = (const u16*) packed;
13429eff1a38SJesse Larrew 
13439eff1a38SJesse Larrew #define VPHN_FIELD_UNUSED	(0xffff)
13449eff1a38SJesse Larrew #define VPHN_FIELD_MSB		(0x8000)
13459eff1a38SJesse Larrew #define VPHN_FIELD_MASK		(~VPHN_FIELD_MSB)
13469eff1a38SJesse Larrew 
1347c0e5e46fSAnton Blanchard 	for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
13489eff1a38SJesse Larrew 		if (*field == VPHN_FIELD_UNUSED) {
13499eff1a38SJesse Larrew 			/* All significant fields processed, and remaining
13509eff1a38SJesse Larrew 			 * fields contain the reserved value of all 1's.
13519eff1a38SJesse Larrew 			 * Just store them.
13529eff1a38SJesse Larrew 			 */
13539eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13549eff1a38SJesse Larrew 			field += 2;
13557639adaaSJesse Larrew 		} else if (*field & VPHN_FIELD_MSB) {
13569eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field */
13579eff1a38SJesse Larrew 			unpacked[i] = *field & VPHN_FIELD_MASK;
13589eff1a38SJesse Larrew 			field++;
13599eff1a38SJesse Larrew 			nr_assoc_doms++;
13607639adaaSJesse Larrew 		} else {
13619eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field
13629eff1a38SJesse Larrew 			 * concatenated with the next 16 bit field
13639eff1a38SJesse Larrew 			 */
13649eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13659eff1a38SJesse Larrew 			field += 2;
13669eff1a38SJesse Larrew 			nr_assoc_doms++;
13679eff1a38SJesse Larrew 		}
13689eff1a38SJesse Larrew 	}
13699eff1a38SJesse Larrew 
1370c0e5e46fSAnton Blanchard 	/* The first cell contains the length of the property */
1371c0e5e46fSAnton Blanchard 	unpacked[0] = nr_assoc_doms;
1372c0e5e46fSAnton Blanchard 
13739eff1a38SJesse Larrew 	return nr_assoc_doms;
13749eff1a38SJesse Larrew }
13759eff1a38SJesse Larrew 
13769eff1a38SJesse Larrew /*
13779eff1a38SJesse Larrew  * Retrieve the new associativity information for a virtual processor's
13789eff1a38SJesse Larrew  * home node.
13799eff1a38SJesse Larrew  */
13809eff1a38SJesse Larrew static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
13819eff1a38SJesse Larrew {
1382cd9d6cc7SJesse Larrew 	long rc;
13839eff1a38SJesse Larrew 	long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
13849eff1a38SJesse Larrew 	u64 flags = 1;
13859eff1a38SJesse Larrew 	int hwcpu = get_hard_smp_processor_id(cpu);
13869eff1a38SJesse Larrew 
13879eff1a38SJesse Larrew 	rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
13889eff1a38SJesse Larrew 	vphn_unpack_associativity(retbuf, associativity);
13899eff1a38SJesse Larrew 
13909eff1a38SJesse Larrew 	return rc;
13919eff1a38SJesse Larrew }
13929eff1a38SJesse Larrew 
13939eff1a38SJesse Larrew static long vphn_get_associativity(unsigned long cpu,
13949eff1a38SJesse Larrew 					unsigned int *associativity)
13959eff1a38SJesse Larrew {
1396cd9d6cc7SJesse Larrew 	long rc;
13979eff1a38SJesse Larrew 
13989eff1a38SJesse Larrew 	rc = hcall_vphn(cpu, associativity);
13999eff1a38SJesse Larrew 
14009eff1a38SJesse Larrew 	switch (rc) {
14019eff1a38SJesse Larrew 	case H_FUNCTION:
14029eff1a38SJesse Larrew 		printk(KERN_INFO
14039eff1a38SJesse Larrew 			"VPHN is not supported. Disabling polling...\n");
14049eff1a38SJesse Larrew 		stop_topology_update();
14059eff1a38SJesse Larrew 		break;
14069eff1a38SJesse Larrew 	case H_HARDWARE:
14079eff1a38SJesse Larrew 		printk(KERN_ERR
14089eff1a38SJesse Larrew 			"hcall_vphn() experienced a hardware fault "
14099eff1a38SJesse Larrew 			"preventing VPHN. Disabling polling...\n");
14109eff1a38SJesse Larrew 		stop_topology_update();
14119eff1a38SJesse Larrew 	}
14129eff1a38SJesse Larrew 
14139eff1a38SJesse Larrew 	return rc;
14149eff1a38SJesse Larrew }
14159eff1a38SJesse Larrew 
14169eff1a38SJesse Larrew /*
141730c05350SNathan Fontenot  * Update the CPU maps and sysfs entries for a single CPU when its NUMA
141830c05350SNathan Fontenot  * characteristics change. This function doesn't perform any locking and is
141930c05350SNathan Fontenot  * only safe to call from stop_machine().
142030c05350SNathan Fontenot  */
142130c05350SNathan Fontenot static int update_cpu_topology(void *data)
142230c05350SNathan Fontenot {
142330c05350SNathan Fontenot 	struct topology_update_data *update;
142430c05350SNathan Fontenot 	unsigned long cpu;
142530c05350SNathan Fontenot 
142630c05350SNathan Fontenot 	if (!data)
142730c05350SNathan Fontenot 		return -EINVAL;
142830c05350SNathan Fontenot 
142930c05350SNathan Fontenot 	cpu = get_cpu();
143030c05350SNathan Fontenot 
143130c05350SNathan Fontenot 	for (update = data; update; update = update->next) {
143230c05350SNathan Fontenot 		if (cpu != update->cpu)
143330c05350SNathan Fontenot 			continue;
143430c05350SNathan Fontenot 
143530c05350SNathan Fontenot 		unregister_cpu_under_node(update->cpu, update->old_nid);
143630c05350SNathan Fontenot 		unmap_cpu_from_node(update->cpu);
143730c05350SNathan Fontenot 		map_cpu_to_node(update->cpu, update->new_nid);
1438176bbf14SJesse Larrew 		vdso_getcpu_init();
143930c05350SNathan Fontenot 		register_cpu_under_node(update->cpu, update->new_nid);
144030c05350SNathan Fontenot 	}
144130c05350SNathan Fontenot 
144230c05350SNathan Fontenot 	return 0;
144330c05350SNathan Fontenot }
144430c05350SNathan Fontenot 
144530c05350SNathan Fontenot /*
14469eff1a38SJesse Larrew  * Update the node maps and sysfs entries for each cpu whose home node
144779c5fcebSJesse Larrew  * has changed. Returns 1 when the topology has changed, and 0 otherwise.
14489eff1a38SJesse Larrew  */
14499eff1a38SJesse Larrew int arch_update_cpu_topology(void)
14509eff1a38SJesse Larrew {
145130c05350SNathan Fontenot 	unsigned int cpu, changed = 0;
145230c05350SNathan Fontenot 	struct topology_update_data *updates, *ud;
14539eff1a38SJesse Larrew 	unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
1454176bbf14SJesse Larrew 	cpumask_t updated_cpus;
14558a25a2fdSKay Sievers 	struct device *dev;
145630c05350SNathan Fontenot 	int weight, i = 0;
145730c05350SNathan Fontenot 
145830c05350SNathan Fontenot 	weight = cpumask_weight(&cpu_associativity_changes_mask);
145930c05350SNathan Fontenot 	if (!weight)
146030c05350SNathan Fontenot 		return 0;
146130c05350SNathan Fontenot 
146230c05350SNathan Fontenot 	updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
146330c05350SNathan Fontenot 	if (!updates)
146430c05350SNathan Fontenot 		return 0;
14659eff1a38SJesse Larrew 
1466176bbf14SJesse Larrew 	cpumask_clear(&updated_cpus);
1467176bbf14SJesse Larrew 
1468104699c0SKOSAKI Motohiro 	for_each_cpu(cpu, &cpu_associativity_changes_mask) {
146930c05350SNathan Fontenot 		ud = &updates[i++];
147030c05350SNathan Fontenot 		ud->cpu = cpu;
14719eff1a38SJesse Larrew 		vphn_get_associativity(cpu, associativity);
147230c05350SNathan Fontenot 		ud->new_nid = associativity_to_nid(associativity);
14739eff1a38SJesse Larrew 
147430c05350SNathan Fontenot 		if (ud->new_nid < 0 || !node_online(ud->new_nid))
147530c05350SNathan Fontenot 			ud->new_nid = first_online_node;
14769eff1a38SJesse Larrew 
147730c05350SNathan Fontenot 		ud->old_nid = numa_cpu_lookup_table[cpu];
1478176bbf14SJesse Larrew 		cpumask_set_cpu(cpu, &updated_cpus);
14799eff1a38SJesse Larrew 
148030c05350SNathan Fontenot 		if (i < weight)
148130c05350SNathan Fontenot 			ud->next = &updates[i];
148230c05350SNathan Fontenot 	}
14839eff1a38SJesse Larrew 
1484176bbf14SJesse Larrew 	stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
148530c05350SNathan Fontenot 
148630c05350SNathan Fontenot 	for (ud = &updates[0]; ud; ud = ud->next) {
148730c05350SNathan Fontenot 		dev = get_cpu_device(ud->cpu);
14888a25a2fdSKay Sievers 		if (dev)
14898a25a2fdSKay Sievers 			kobject_uevent(&dev->kobj, KOBJ_CHANGE);
149030c05350SNathan Fontenot 		cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
149179c5fcebSJesse Larrew 		changed = 1;
14929eff1a38SJesse Larrew 	}
14939eff1a38SJesse Larrew 
149430c05350SNathan Fontenot 	kfree(updates);
149579c5fcebSJesse Larrew 	return changed;
14969eff1a38SJesse Larrew }
14979eff1a38SJesse Larrew 
14989eff1a38SJesse Larrew static void topology_work_fn(struct work_struct *work)
14999eff1a38SJesse Larrew {
15009eff1a38SJesse Larrew 	rebuild_sched_domains();
15019eff1a38SJesse Larrew }
15029eff1a38SJesse Larrew static DECLARE_WORK(topology_work, topology_work_fn);
15039eff1a38SJesse Larrew 
15049eff1a38SJesse Larrew void topology_schedule_update(void)
15059eff1a38SJesse Larrew {
15069eff1a38SJesse Larrew 	schedule_work(&topology_work);
15079eff1a38SJesse Larrew }
15089eff1a38SJesse Larrew 
15099eff1a38SJesse Larrew static void topology_timer_fn(unsigned long ignored)
15109eff1a38SJesse Larrew {
15115d88aa85SJesse Larrew 	if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
15125d88aa85SJesse Larrew 		topology_schedule_update();
15135d88aa85SJesse Larrew 	else if (vphn_enabled) {
15149eff1a38SJesse Larrew 		if (update_cpu_associativity_changes_mask() > 0)
15159eff1a38SJesse Larrew 			topology_schedule_update();
15165d88aa85SJesse Larrew 		reset_topology_timer();
15175d88aa85SJesse Larrew 	}
15189eff1a38SJesse Larrew }
15199eff1a38SJesse Larrew static struct timer_list topology_timer =
15209eff1a38SJesse Larrew 	TIMER_INITIALIZER(topology_timer_fn, 0, 0);
15219eff1a38SJesse Larrew 
15225d88aa85SJesse Larrew static void reset_topology_timer(void)
15239eff1a38SJesse Larrew {
15249eff1a38SJesse Larrew 	topology_timer.data = 0;
15259eff1a38SJesse Larrew 	topology_timer.expires = jiffies + 60 * HZ;
15265d88aa85SJesse Larrew 	mod_timer(&topology_timer, topology_timer.expires);
15279eff1a38SJesse Larrew }
15289eff1a38SJesse Larrew 
15295d88aa85SJesse Larrew static void stage_topology_update(int core_id)
15305d88aa85SJesse Larrew {
15315d88aa85SJesse Larrew 	cpumask_or(&cpu_associativity_changes_mask,
15325d88aa85SJesse Larrew 		&cpu_associativity_changes_mask, cpu_sibling_mask(core_id));
15335d88aa85SJesse Larrew 	reset_topology_timer();
15345d88aa85SJesse Larrew }
15355d88aa85SJesse Larrew 
15365d88aa85SJesse Larrew static int dt_update_callback(struct notifier_block *nb,
15375d88aa85SJesse Larrew 				unsigned long action, void *data)
15385d88aa85SJesse Larrew {
15395d88aa85SJesse Larrew 	struct of_prop_reconfig *update;
15405d88aa85SJesse Larrew 	int rc = NOTIFY_DONE;
15415d88aa85SJesse Larrew 
15425d88aa85SJesse Larrew 	switch (action) {
15435d88aa85SJesse Larrew 	case OF_RECONFIG_UPDATE_PROPERTY:
15445d88aa85SJesse Larrew 		update = (struct of_prop_reconfig *)data;
154530c05350SNathan Fontenot 		if (!of_prop_cmp(update->dn->type, "cpu") &&
154630c05350SNathan Fontenot 		    !of_prop_cmp(update->prop->name, "ibm,associativity")) {
15475d88aa85SJesse Larrew 			u32 core_id;
15485d88aa85SJesse Larrew 			of_property_read_u32(update->dn, "reg", &core_id);
15495d88aa85SJesse Larrew 			stage_topology_update(core_id);
15505d88aa85SJesse Larrew 			rc = NOTIFY_OK;
15515d88aa85SJesse Larrew 		}
15525d88aa85SJesse Larrew 		break;
15535d88aa85SJesse Larrew 	}
15545d88aa85SJesse Larrew 
15555d88aa85SJesse Larrew 	return rc;
15565d88aa85SJesse Larrew }
15575d88aa85SJesse Larrew 
15585d88aa85SJesse Larrew static struct notifier_block dt_update_nb = {
15595d88aa85SJesse Larrew 	.notifier_call = dt_update_callback,
15605d88aa85SJesse Larrew };
15615d88aa85SJesse Larrew 
15629eff1a38SJesse Larrew /*
15635d88aa85SJesse Larrew  * Start polling for associativity changes.
15649eff1a38SJesse Larrew  */
15659eff1a38SJesse Larrew int start_topology_update(void)
15669eff1a38SJesse Larrew {
15679eff1a38SJesse Larrew 	int rc = 0;
15689eff1a38SJesse Larrew 
15695d88aa85SJesse Larrew 	if (firmware_has_feature(FW_FEATURE_PRRN)) {
15705d88aa85SJesse Larrew 		if (!prrn_enabled) {
15715d88aa85SJesse Larrew 			prrn_enabled = 1;
15725d88aa85SJesse Larrew 			vphn_enabled = 0;
15735d88aa85SJesse Larrew 			rc = of_reconfig_notifier_register(&dt_update_nb);
15745d88aa85SJesse Larrew 		}
1575*b7abef04SJesse Larrew 	} else if (firmware_has_feature(FW_FEATURE_VPHN) &&
1576fe5cfd63SAnton Blanchard 		   get_lppaca()->shared_proc) {
15775d88aa85SJesse Larrew 		if (!vphn_enabled) {
15785d88aa85SJesse Larrew 			prrn_enabled = 0;
15799eff1a38SJesse Larrew 			vphn_enabled = 1;
15809eff1a38SJesse Larrew 			setup_cpu_associativity_change_counters();
15819eff1a38SJesse Larrew 			init_timer_deferrable(&topology_timer);
15825d88aa85SJesse Larrew 			reset_topology_timer();
15835d88aa85SJesse Larrew 		}
15849eff1a38SJesse Larrew 	}
15859eff1a38SJesse Larrew 
15869eff1a38SJesse Larrew 	return rc;
15879eff1a38SJesse Larrew }
15889eff1a38SJesse Larrew __initcall(start_topology_update);
15899eff1a38SJesse Larrew 
15909eff1a38SJesse Larrew /*
15919eff1a38SJesse Larrew  * Disable polling for VPHN associativity changes.
15929eff1a38SJesse Larrew  */
15939eff1a38SJesse Larrew int stop_topology_update(void)
15949eff1a38SJesse Larrew {
15955d88aa85SJesse Larrew 	int rc = 0;
15965d88aa85SJesse Larrew 
15975d88aa85SJesse Larrew 	if (prrn_enabled) {
15985d88aa85SJesse Larrew 		prrn_enabled = 0;
15995d88aa85SJesse Larrew 		rc = of_reconfig_notifier_unregister(&dt_update_nb);
16005d88aa85SJesse Larrew 	} else if (vphn_enabled) {
16019eff1a38SJesse Larrew 		vphn_enabled = 0;
16025d88aa85SJesse Larrew 		rc = del_timer_sync(&topology_timer);
16035d88aa85SJesse Larrew 	}
16045d88aa85SJesse Larrew 
16055d88aa85SJesse Larrew 	return rc;
16069eff1a38SJesse Larrew }
160739bf990eSJesse Larrew #endif /* CONFIG_PPC_SPLPAR */
1608