xref: /linux/arch/powerpc/mm/numa.c (revision 82b2521d257b5c0efd51821cf5fa306e53bbb6ba)
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>
2545fb6ceaSAnton Blanchard #include <asm/sparsemem.h>
26d9b2b2a2SDavid S. Miller #include <asm/prom.h>
272249ca9dSPaul Mackerras #include <asm/smp.h>
289eff1a38SJesse Larrew #include <asm/firmware.h>
299eff1a38SJesse Larrew #include <asm/paca.h>
3039bf990eSJesse Larrew #include <asm/hvcall.h>
31ae3a197eSDavid Howells #include <asm/setup.h>
32ab1f9dacSPaul Mackerras 
33ab1f9dacSPaul Mackerras static int numa_enabled = 1;
34ab1f9dacSPaul Mackerras 
351daa6d08SBalbir Singh static char *cmdline __initdata;
361daa6d08SBalbir Singh 
37ab1f9dacSPaul Mackerras static int numa_debug;
38ab1f9dacSPaul Mackerras #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
39ab1f9dacSPaul Mackerras 
4045fb6ceaSAnton Blanchard int numa_cpu_lookup_table[NR_CPUS];
4125863de0SAnton Blanchard cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
42ab1f9dacSPaul Mackerras struct pglist_data *node_data[MAX_NUMNODES];
4345fb6ceaSAnton Blanchard 
4445fb6ceaSAnton Blanchard EXPORT_SYMBOL(numa_cpu_lookup_table);
4525863de0SAnton Blanchard EXPORT_SYMBOL(node_to_cpumask_map);
4645fb6ceaSAnton Blanchard EXPORT_SYMBOL(node_data);
4745fb6ceaSAnton Blanchard 
48ab1f9dacSPaul Mackerras static int min_common_depth;
49237a0989SMike Kravetz static int n_mem_addr_cells, n_mem_size_cells;
5041eab6f8SAnton Blanchard static int form1_affinity;
5141eab6f8SAnton Blanchard 
5241eab6f8SAnton Blanchard #define MAX_DISTANCE_REF_POINTS 4
5341eab6f8SAnton Blanchard static int distance_ref_points_depth;
5441eab6f8SAnton Blanchard static const unsigned int *distance_ref_points;
5541eab6f8SAnton Blanchard static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
56ab1f9dacSPaul Mackerras 
5725863de0SAnton Blanchard /*
5825863de0SAnton Blanchard  * Allocate node_to_cpumask_map based on number of available nodes
5925863de0SAnton Blanchard  * Requires node_possible_map to be valid.
6025863de0SAnton Blanchard  *
619512938bSWanlong Gao  * Note: cpumask_of_node() is not valid until after this is done.
6225863de0SAnton Blanchard  */
6325863de0SAnton Blanchard static void __init setup_node_to_cpumask_map(void)
6425863de0SAnton Blanchard {
6525863de0SAnton Blanchard 	unsigned int node, num = 0;
6625863de0SAnton Blanchard 
6725863de0SAnton Blanchard 	/* setup nr_node_ids if not done yet */
6825863de0SAnton Blanchard 	if (nr_node_ids == MAX_NUMNODES) {
6925863de0SAnton Blanchard 		for_each_node_mask(node, node_possible_map)
7025863de0SAnton Blanchard 			num = node;
7125863de0SAnton Blanchard 		nr_node_ids = num + 1;
7225863de0SAnton Blanchard 	}
7325863de0SAnton Blanchard 
7425863de0SAnton Blanchard 	/* allocate the map */
7525863de0SAnton Blanchard 	for (node = 0; node < nr_node_ids; node++)
7625863de0SAnton Blanchard 		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
7725863de0SAnton Blanchard 
7825863de0SAnton Blanchard 	/* cpumask_of_node() will now work */
7925863de0SAnton Blanchard 	dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
8025863de0SAnton Blanchard }
8125863de0SAnton Blanchard 
821daa6d08SBalbir Singh static int __cpuinit fake_numa_create_new_node(unsigned long end_pfn,
831daa6d08SBalbir Singh 						unsigned int *nid)
841daa6d08SBalbir Singh {
851daa6d08SBalbir Singh 	unsigned long long mem;
861daa6d08SBalbir Singh 	char *p = cmdline;
871daa6d08SBalbir Singh 	static unsigned int fake_nid;
881daa6d08SBalbir Singh 	static unsigned long long curr_boundary;
891daa6d08SBalbir Singh 
901daa6d08SBalbir Singh 	/*
911daa6d08SBalbir Singh 	 * Modify node id, iff we started creating NUMA nodes
921daa6d08SBalbir Singh 	 * We want to continue from where we left of the last time
931daa6d08SBalbir Singh 	 */
941daa6d08SBalbir Singh 	if (fake_nid)
951daa6d08SBalbir Singh 		*nid = fake_nid;
961daa6d08SBalbir Singh 	/*
971daa6d08SBalbir Singh 	 * In case there are no more arguments to parse, the
981daa6d08SBalbir Singh 	 * node_id should be the same as the last fake node id
991daa6d08SBalbir Singh 	 * (we've handled this above).
1001daa6d08SBalbir Singh 	 */
1011daa6d08SBalbir Singh 	if (!p)
1021daa6d08SBalbir Singh 		return 0;
1031daa6d08SBalbir Singh 
1041daa6d08SBalbir Singh 	mem = memparse(p, &p);
1051daa6d08SBalbir Singh 	if (!mem)
1061daa6d08SBalbir Singh 		return 0;
1071daa6d08SBalbir Singh 
1081daa6d08SBalbir Singh 	if (mem < curr_boundary)
1091daa6d08SBalbir Singh 		return 0;
1101daa6d08SBalbir Singh 
1111daa6d08SBalbir Singh 	curr_boundary = mem;
1121daa6d08SBalbir Singh 
1131daa6d08SBalbir Singh 	if ((end_pfn << PAGE_SHIFT) > mem) {
1141daa6d08SBalbir Singh 		/*
1151daa6d08SBalbir Singh 		 * Skip commas and spaces
1161daa6d08SBalbir Singh 		 */
1171daa6d08SBalbir Singh 		while (*p == ',' || *p == ' ' || *p == '\t')
1181daa6d08SBalbir Singh 			p++;
1191daa6d08SBalbir Singh 
1201daa6d08SBalbir Singh 		cmdline = p;
1211daa6d08SBalbir Singh 		fake_nid++;
1221daa6d08SBalbir Singh 		*nid = fake_nid;
1231daa6d08SBalbir Singh 		dbg("created new fake_node with id %d\n", fake_nid);
1241daa6d08SBalbir Singh 		return 1;
1251daa6d08SBalbir Singh 	}
1261daa6d08SBalbir Singh 	return 0;
1271daa6d08SBalbir Singh }
1281daa6d08SBalbir Singh 
1298f64e1f2SJon Tollefson /*
1305dfe8660STejun Heo  * get_node_active_region - Return active region containing pfn
131e8170372SJon Tollefson  * Active range returned is empty if none found.
1325dfe8660STejun Heo  * @pfn: The page to return the region for
1335dfe8660STejun Heo  * @node_ar: Returned set to the active region containing @pfn
1348f64e1f2SJon Tollefson  */
1355dfe8660STejun Heo static void __init get_node_active_region(unsigned long pfn,
1368f64e1f2SJon Tollefson 					  struct node_active_region *node_ar)
1378f64e1f2SJon Tollefson {
1385dfe8660STejun Heo 	unsigned long start_pfn, end_pfn;
1395dfe8660STejun Heo 	int i, nid;
1408f64e1f2SJon Tollefson 
1415dfe8660STejun Heo 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
1425dfe8660STejun Heo 		if (pfn >= start_pfn && pfn < end_pfn) {
1438f64e1f2SJon Tollefson 			node_ar->nid = nid;
1448f64e1f2SJon Tollefson 			node_ar->start_pfn = start_pfn;
1455dfe8660STejun Heo 			node_ar->end_pfn = end_pfn;
1465dfe8660STejun Heo 			break;
1475dfe8660STejun Heo 		}
1485dfe8660STejun Heo 	}
1498f64e1f2SJon Tollefson }
1508f64e1f2SJon Tollefson 
15139bf990eSJesse Larrew static void map_cpu_to_node(int cpu, int node)
152ab1f9dacSPaul Mackerras {
153ab1f9dacSPaul Mackerras 	numa_cpu_lookup_table[cpu] = node;
15445fb6ceaSAnton Blanchard 
155bf4b85b0SNathan Lynch 	dbg("adding cpu %d to node %d\n", cpu, node);
156bf4b85b0SNathan Lynch 
15725863de0SAnton Blanchard 	if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
15825863de0SAnton Blanchard 		cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
159ab1f9dacSPaul Mackerras }
160ab1f9dacSPaul Mackerras 
16139bf990eSJesse Larrew #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
162ab1f9dacSPaul Mackerras static void unmap_cpu_from_node(unsigned long cpu)
163ab1f9dacSPaul Mackerras {
164ab1f9dacSPaul Mackerras 	int node = numa_cpu_lookup_table[cpu];
165ab1f9dacSPaul Mackerras 
166ab1f9dacSPaul Mackerras 	dbg("removing cpu %lu from node %d\n", cpu, node);
167ab1f9dacSPaul Mackerras 
16825863de0SAnton Blanchard 	if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
169429f4d8dSAnton Blanchard 		cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
170ab1f9dacSPaul Mackerras 	} else {
171ab1f9dacSPaul Mackerras 		printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
172ab1f9dacSPaul Mackerras 		       cpu, node);
173ab1f9dacSPaul Mackerras 	}
174ab1f9dacSPaul Mackerras }
17539bf990eSJesse Larrew #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
176ab1f9dacSPaul Mackerras 
177ab1f9dacSPaul Mackerras /* must hold reference to node during call */
178a7f67bdfSJeremy Kerr static const int *of_get_associativity(struct device_node *dev)
179ab1f9dacSPaul Mackerras {
180e2eb6392SStephen Rothwell 	return of_get_property(dev, "ibm,associativity", NULL);
181ab1f9dacSPaul Mackerras }
182ab1f9dacSPaul Mackerras 
183cf00085dSChandru /*
184cf00085dSChandru  * Returns the property linux,drconf-usable-memory if
185cf00085dSChandru  * it exists (the property exists only in kexec/kdump kernels,
186cf00085dSChandru  * added by kexec-tools)
187cf00085dSChandru  */
188cf00085dSChandru static const u32 *of_get_usable_memory(struct device_node *memory)
189cf00085dSChandru {
190cf00085dSChandru 	const u32 *prop;
191cf00085dSChandru 	u32 len;
192cf00085dSChandru 	prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
193cf00085dSChandru 	if (!prop || len < sizeof(unsigned int))
194cf00085dSChandru 		return 0;
195cf00085dSChandru 	return prop;
196cf00085dSChandru }
197cf00085dSChandru 
19841eab6f8SAnton Blanchard int __node_distance(int a, int b)
19941eab6f8SAnton Blanchard {
20041eab6f8SAnton Blanchard 	int i;
20141eab6f8SAnton Blanchard 	int distance = LOCAL_DISTANCE;
20241eab6f8SAnton Blanchard 
20341eab6f8SAnton Blanchard 	if (!form1_affinity)
20441eab6f8SAnton Blanchard 		return distance;
20541eab6f8SAnton Blanchard 
20641eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
20741eab6f8SAnton Blanchard 		if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
20841eab6f8SAnton Blanchard 			break;
20941eab6f8SAnton Blanchard 
21041eab6f8SAnton Blanchard 		/* Double the distance for each NUMA level */
21141eab6f8SAnton Blanchard 		distance *= 2;
21241eab6f8SAnton Blanchard 	}
21341eab6f8SAnton Blanchard 
21441eab6f8SAnton Blanchard 	return distance;
21541eab6f8SAnton Blanchard }
21641eab6f8SAnton Blanchard 
21741eab6f8SAnton Blanchard static void initialize_distance_lookup_table(int nid,
21841eab6f8SAnton Blanchard 		const unsigned int *associativity)
21941eab6f8SAnton Blanchard {
22041eab6f8SAnton Blanchard 	int i;
22141eab6f8SAnton Blanchard 
22241eab6f8SAnton Blanchard 	if (!form1_affinity)
22341eab6f8SAnton Blanchard 		return;
22441eab6f8SAnton Blanchard 
22541eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
22641eab6f8SAnton Blanchard 		distance_lookup_table[nid][i] =
22741eab6f8SAnton Blanchard 			associativity[distance_ref_points[i]];
22841eab6f8SAnton Blanchard 	}
22941eab6f8SAnton Blanchard }
23041eab6f8SAnton Blanchard 
231482ec7c4SNathan Lynch /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
232482ec7c4SNathan Lynch  * info is found.
233482ec7c4SNathan Lynch  */
2349eff1a38SJesse Larrew static int associativity_to_nid(const unsigned int *associativity)
235ab1f9dacSPaul Mackerras {
236482ec7c4SNathan Lynch 	int nid = -1;
237ab1f9dacSPaul Mackerras 
238ab1f9dacSPaul Mackerras 	if (min_common_depth == -1)
239482ec7c4SNathan Lynch 		goto out;
240ab1f9dacSPaul Mackerras 
2419eff1a38SJesse Larrew 	if (associativity[0] >= min_common_depth)
2429eff1a38SJesse Larrew 		nid = associativity[min_common_depth];
243bc16a759SNathan Lynch 
244bc16a759SNathan Lynch 	/* POWER4 LPAR uses 0xffff as invalid node */
245482ec7c4SNathan Lynch 	if (nid == 0xffff || nid >= MAX_NUMNODES)
246482ec7c4SNathan Lynch 		nid = -1;
24741eab6f8SAnton Blanchard 
2489eff1a38SJesse Larrew 	if (nid > 0 && associativity[0] >= distance_ref_points_depth)
2499eff1a38SJesse Larrew 		initialize_distance_lookup_table(nid, associativity);
25041eab6f8SAnton Blanchard 
251482ec7c4SNathan Lynch out:
252cf950b7aSNathan Lynch 	return nid;
253ab1f9dacSPaul Mackerras }
254ab1f9dacSPaul Mackerras 
2559eff1a38SJesse Larrew /* Returns the nid associated with the given device tree node,
2569eff1a38SJesse Larrew  * or -1 if not found.
2579eff1a38SJesse Larrew  */
2589eff1a38SJesse Larrew static int of_node_to_nid_single(struct device_node *device)
2599eff1a38SJesse Larrew {
2609eff1a38SJesse Larrew 	int nid = -1;
2619eff1a38SJesse Larrew 	const unsigned int *tmp;
2629eff1a38SJesse Larrew 
2639eff1a38SJesse Larrew 	tmp = of_get_associativity(device);
2649eff1a38SJesse Larrew 	if (tmp)
2659eff1a38SJesse Larrew 		nid = associativity_to_nid(tmp);
2669eff1a38SJesse Larrew 	return nid;
2679eff1a38SJesse Larrew }
2689eff1a38SJesse Larrew 
269953039c8SJeremy Kerr /* Walk the device tree upwards, looking for an associativity id */
270953039c8SJeremy Kerr int of_node_to_nid(struct device_node *device)
271953039c8SJeremy Kerr {
272953039c8SJeremy Kerr 	struct device_node *tmp;
273953039c8SJeremy Kerr 	int nid = -1;
274953039c8SJeremy Kerr 
275953039c8SJeremy Kerr 	of_node_get(device);
276953039c8SJeremy Kerr 	while (device) {
277953039c8SJeremy Kerr 		nid = of_node_to_nid_single(device);
278953039c8SJeremy Kerr 		if (nid != -1)
279953039c8SJeremy Kerr 			break;
280953039c8SJeremy Kerr 
281953039c8SJeremy Kerr 	        tmp = device;
282953039c8SJeremy Kerr 		device = of_get_parent(tmp);
283953039c8SJeremy Kerr 		of_node_put(tmp);
284953039c8SJeremy Kerr 	}
285953039c8SJeremy Kerr 	of_node_put(device);
286953039c8SJeremy Kerr 
287953039c8SJeremy Kerr 	return nid;
288953039c8SJeremy Kerr }
289953039c8SJeremy Kerr EXPORT_SYMBOL_GPL(of_node_to_nid);
290953039c8SJeremy Kerr 
291ab1f9dacSPaul Mackerras static int __init find_min_common_depth(void)
292ab1f9dacSPaul Mackerras {
29341eab6f8SAnton Blanchard 	int depth;
294bc8449ccSAnton Blanchard 	struct device_node *chosen;
295e70606ebSMichael Ellerman 	struct device_node *root;
296bc8449ccSAnton Blanchard 	const char *vec5;
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 
328bc8449ccSAnton Blanchard #define VEC5_AFFINITY_BYTE	5
329bc8449ccSAnton Blanchard #define VEC5_AFFINITY		0x80
3301c8ee733SDipankar Sarma 
3311c8ee733SDipankar Sarma 	if (firmware_has_feature(FW_FEATURE_OPAL))
3321c8ee733SDipankar Sarma 		form1_affinity = 1;
3331c8ee733SDipankar Sarma 	else {
334bc8449ccSAnton Blanchard 		chosen = of_find_node_by_path("/chosen");
335bc8449ccSAnton Blanchard 		if (chosen) {
3361c8ee733SDipankar Sarma 			vec5 = of_get_property(chosen,
3371c8ee733SDipankar Sarma 					       "ibm,architecture-vec-5", NULL);
3381c8ee733SDipankar Sarma 			if (vec5 && (vec5[VEC5_AFFINITY_BYTE] &
3391c8ee733SDipankar Sarma 							VEC5_AFFINITY)) {
340bc8449ccSAnton Blanchard 				dbg("Using form 1 affinity\n");
34141eab6f8SAnton Blanchard 				form1_affinity = 1;
3424b83c330SAnton Blanchard 			}
343bc8449ccSAnton Blanchard 		}
3441c8ee733SDipankar Sarma 	}
3454b83c330SAnton Blanchard 
34641eab6f8SAnton Blanchard 	if (form1_affinity) {
34741eab6f8SAnton Blanchard 		depth = distance_ref_points[0];
348ab1f9dacSPaul Mackerras 	} else {
34941eab6f8SAnton Blanchard 		if (distance_ref_points_depth < 2) {
35041eab6f8SAnton Blanchard 			printk(KERN_WARNING "NUMA: "
35141eab6f8SAnton Blanchard 				"short ibm,associativity-reference-points\n");
35241eab6f8SAnton Blanchard 			goto err;
353ab1f9dacSPaul Mackerras 		}
354ab1f9dacSPaul Mackerras 
35541eab6f8SAnton Blanchard 		depth = distance_ref_points[1];
35641eab6f8SAnton Blanchard 	}
35741eab6f8SAnton Blanchard 
35841eab6f8SAnton Blanchard 	/*
35941eab6f8SAnton Blanchard 	 * Warn and cap if the hardware supports more than
36041eab6f8SAnton Blanchard 	 * MAX_DISTANCE_REF_POINTS domains.
36141eab6f8SAnton Blanchard 	 */
36241eab6f8SAnton Blanchard 	if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
36341eab6f8SAnton Blanchard 		printk(KERN_WARNING "NUMA: distance array capped at "
36441eab6f8SAnton Blanchard 			"%d entries\n", MAX_DISTANCE_REF_POINTS);
36541eab6f8SAnton Blanchard 		distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
36641eab6f8SAnton Blanchard 	}
36741eab6f8SAnton Blanchard 
368e70606ebSMichael Ellerman 	of_node_put(root);
369ab1f9dacSPaul Mackerras 	return depth;
37041eab6f8SAnton Blanchard 
37141eab6f8SAnton Blanchard err:
372e70606ebSMichael Ellerman 	of_node_put(root);
37341eab6f8SAnton Blanchard 	return -1;
374ab1f9dacSPaul Mackerras }
375ab1f9dacSPaul Mackerras 
37684c9fdd1SMike Kravetz static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
377ab1f9dacSPaul Mackerras {
378ab1f9dacSPaul Mackerras 	struct device_node *memory = NULL;
379ab1f9dacSPaul Mackerras 
380ab1f9dacSPaul Mackerras 	memory = of_find_node_by_type(memory, "memory");
38154c23310SPaul Mackerras 	if (!memory)
38284c9fdd1SMike Kravetz 		panic("numa.c: No memory nodes found!");
38354c23310SPaul Mackerras 
384a8bda5ddSStephen Rothwell 	*n_addr_cells = of_n_addr_cells(memory);
3859213feeaSStephen Rothwell 	*n_size_cells = of_n_size_cells(memory);
38684c9fdd1SMike Kravetz 	of_node_put(memory);
387ab1f9dacSPaul Mackerras }
388ab1f9dacSPaul Mackerras 
3892011b1d0SDavid Rientjes static unsigned long read_n_cells(int n, const unsigned int **buf)
390ab1f9dacSPaul Mackerras {
391ab1f9dacSPaul Mackerras 	unsigned long result = 0;
392ab1f9dacSPaul Mackerras 
393ab1f9dacSPaul Mackerras 	while (n--) {
394ab1f9dacSPaul Mackerras 		result = (result << 32) | **buf;
395ab1f9dacSPaul Mackerras 		(*buf)++;
396ab1f9dacSPaul Mackerras 	}
397ab1f9dacSPaul Mackerras 	return result;
398ab1f9dacSPaul Mackerras }
399ab1f9dacSPaul Mackerras 
4008342681dSNathan Fontenot struct of_drconf_cell {
4018342681dSNathan Fontenot 	u64	base_addr;
4028342681dSNathan Fontenot 	u32	drc_index;
4038342681dSNathan Fontenot 	u32	reserved;
4048342681dSNathan Fontenot 	u32	aa_index;
4058342681dSNathan Fontenot 	u32	flags;
4068342681dSNathan Fontenot };
4078342681dSNathan Fontenot 
4088342681dSNathan Fontenot #define DRCONF_MEM_ASSIGNED	0x00000008
4098342681dSNathan Fontenot #define DRCONF_MEM_AI_INVALID	0x00000040
4108342681dSNathan Fontenot #define DRCONF_MEM_RESERVED	0x00000080
4118342681dSNathan Fontenot 
4128342681dSNathan Fontenot /*
41395f72d1eSYinghai Lu  * Read the next memblock list entry from the ibm,dynamic-memory property
4148342681dSNathan Fontenot  * and return the information in the provided of_drconf_cell structure.
4158342681dSNathan Fontenot  */
4168342681dSNathan Fontenot static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
4178342681dSNathan Fontenot {
4188342681dSNathan Fontenot 	const u32 *cp;
4198342681dSNathan Fontenot 
4208342681dSNathan Fontenot 	drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
4218342681dSNathan Fontenot 
4228342681dSNathan Fontenot 	cp = *cellp;
4238342681dSNathan Fontenot 	drmem->drc_index = cp[0];
4248342681dSNathan Fontenot 	drmem->reserved = cp[1];
4258342681dSNathan Fontenot 	drmem->aa_index = cp[2];
4268342681dSNathan Fontenot 	drmem->flags = cp[3];
4278342681dSNathan Fontenot 
4288342681dSNathan Fontenot 	*cellp = cp + 4;
4298342681dSNathan Fontenot }
4308342681dSNathan Fontenot 
4318342681dSNathan Fontenot /*
43225985edcSLucas De Marchi  * Retrieve and validate the ibm,dynamic-memory property of the device tree.
4338342681dSNathan Fontenot  *
43495f72d1eSYinghai Lu  * The layout of the ibm,dynamic-memory property is a number N of memblock
43595f72d1eSYinghai Lu  * list entries followed by N memblock list entries.  Each memblock list entry
43625985edcSLucas De Marchi  * contains information as laid out in the of_drconf_cell struct above.
4378342681dSNathan Fontenot  */
4388342681dSNathan Fontenot static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
4398342681dSNathan Fontenot {
4408342681dSNathan Fontenot 	const u32 *prop;
4418342681dSNathan Fontenot 	u32 len, entries;
4428342681dSNathan Fontenot 
4438342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,dynamic-memory", &len);
4448342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4458342681dSNathan Fontenot 		return 0;
4468342681dSNathan Fontenot 
4478342681dSNathan Fontenot 	entries = *prop++;
4488342681dSNathan Fontenot 
4498342681dSNathan Fontenot 	/* Now that we know the number of entries, revalidate the size
4508342681dSNathan Fontenot 	 * of the property read in to ensure we have everything
4518342681dSNathan Fontenot 	 */
4528342681dSNathan Fontenot 	if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
4538342681dSNathan Fontenot 		return 0;
4548342681dSNathan Fontenot 
4558342681dSNathan Fontenot 	*dm = prop;
4568342681dSNathan Fontenot 	return entries;
4578342681dSNathan Fontenot }
4588342681dSNathan Fontenot 
4598342681dSNathan Fontenot /*
46025985edcSLucas De Marchi  * Retrieve and validate the ibm,lmb-size property for drconf memory
4618342681dSNathan Fontenot  * from the device tree.
4628342681dSNathan Fontenot  */
4633fdfd990SBenjamin Herrenschmidt static u64 of_get_lmb_size(struct device_node *memory)
4648342681dSNathan Fontenot {
4658342681dSNathan Fontenot 	const u32 *prop;
4668342681dSNathan Fontenot 	u32 len;
4678342681dSNathan Fontenot 
4683fdfd990SBenjamin Herrenschmidt 	prop = of_get_property(memory, "ibm,lmb-size", &len);
4698342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4708342681dSNathan Fontenot 		return 0;
4718342681dSNathan Fontenot 
4728342681dSNathan Fontenot 	return read_n_cells(n_mem_size_cells, &prop);
4738342681dSNathan Fontenot }
4748342681dSNathan Fontenot 
4758342681dSNathan Fontenot struct assoc_arrays {
4768342681dSNathan Fontenot 	u32	n_arrays;
4778342681dSNathan Fontenot 	u32	array_sz;
4788342681dSNathan Fontenot 	const u32 *arrays;
4798342681dSNathan Fontenot };
4808342681dSNathan Fontenot 
4818342681dSNathan Fontenot /*
48225985edcSLucas De Marchi  * Retrieve and validate the list of associativity arrays for drconf
4838342681dSNathan Fontenot  * memory from the ibm,associativity-lookup-arrays property of the
4848342681dSNathan Fontenot  * device tree..
4858342681dSNathan Fontenot  *
4868342681dSNathan Fontenot  * The layout of the ibm,associativity-lookup-arrays property is a number N
4878342681dSNathan Fontenot  * indicating the number of associativity arrays, followed by a number M
4888342681dSNathan Fontenot  * indicating the size of each associativity array, followed by a list
4898342681dSNathan Fontenot  * of N associativity arrays.
4908342681dSNathan Fontenot  */
4918342681dSNathan Fontenot static int of_get_assoc_arrays(struct device_node *memory,
4928342681dSNathan Fontenot 			       struct assoc_arrays *aa)
4938342681dSNathan Fontenot {
4948342681dSNathan Fontenot 	const u32 *prop;
4958342681dSNathan Fontenot 	u32 len;
4968342681dSNathan Fontenot 
4978342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
4988342681dSNathan Fontenot 	if (!prop || len < 2 * sizeof(unsigned int))
4998342681dSNathan Fontenot 		return -1;
5008342681dSNathan Fontenot 
5018342681dSNathan Fontenot 	aa->n_arrays = *prop++;
5028342681dSNathan Fontenot 	aa->array_sz = *prop++;
5038342681dSNathan Fontenot 
50442b2aa86SJustin P. Mattock 	/* Now that we know the number of arrays and size of each array,
5058342681dSNathan Fontenot 	 * revalidate the size of the property read in.
5068342681dSNathan Fontenot 	 */
5078342681dSNathan Fontenot 	if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
5088342681dSNathan Fontenot 		return -1;
5098342681dSNathan Fontenot 
5108342681dSNathan Fontenot 	aa->arrays = prop;
5118342681dSNathan Fontenot 	return 0;
5128342681dSNathan Fontenot }
5138342681dSNathan Fontenot 
5148342681dSNathan Fontenot /*
5158342681dSNathan Fontenot  * This is like of_node_to_nid_single() for memory represented in the
5168342681dSNathan Fontenot  * ibm,dynamic-reconfiguration-memory node.
5178342681dSNathan Fontenot  */
5188342681dSNathan Fontenot static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
5198342681dSNathan Fontenot 				   struct assoc_arrays *aa)
5208342681dSNathan Fontenot {
5218342681dSNathan Fontenot 	int default_nid = 0;
5228342681dSNathan Fontenot 	int nid = default_nid;
5238342681dSNathan Fontenot 	int index;
5248342681dSNathan Fontenot 
5258342681dSNathan Fontenot 	if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
5268342681dSNathan Fontenot 	    !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
5278342681dSNathan Fontenot 	    drmem->aa_index < aa->n_arrays) {
5288342681dSNathan Fontenot 		index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
5298342681dSNathan Fontenot 		nid = aa->arrays[index];
5308342681dSNathan Fontenot 
5318342681dSNathan Fontenot 		if (nid == 0xffff || nid >= MAX_NUMNODES)
5328342681dSNathan Fontenot 			nid = default_nid;
5338342681dSNathan Fontenot 	}
5348342681dSNathan Fontenot 
5358342681dSNathan Fontenot 	return nid;
5368342681dSNathan Fontenot }
5378342681dSNathan Fontenot 
538ab1f9dacSPaul Mackerras /*
539ab1f9dacSPaul Mackerras  * Figure out to which domain a cpu belongs and stick it there.
540ab1f9dacSPaul Mackerras  * Return the id of the domain used.
541ab1f9dacSPaul Mackerras  */
5422e5ce39dSNathan Lynch static int __cpuinit numa_setup_cpu(unsigned long lcpu)
543ab1f9dacSPaul Mackerras {
544cf950b7aSNathan Lynch 	int nid = 0;
5458b16cd23SMilton Miller 	struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
546ab1f9dacSPaul Mackerras 
547ab1f9dacSPaul Mackerras 	if (!cpu) {
548ab1f9dacSPaul Mackerras 		WARN_ON(1);
549ab1f9dacSPaul Mackerras 		goto out;
550ab1f9dacSPaul Mackerras 	}
551ab1f9dacSPaul Mackerras 
552953039c8SJeremy Kerr 	nid = of_node_to_nid_single(cpu);
553ab1f9dacSPaul Mackerras 
554482ec7c4SNathan Lynch 	if (nid < 0 || !node_online(nid))
55572c33688SH Hartley Sweeten 		nid = first_online_node;
556ab1f9dacSPaul Mackerras out:
557cf950b7aSNathan Lynch 	map_cpu_to_node(lcpu, nid);
558ab1f9dacSPaul Mackerras 
559ab1f9dacSPaul Mackerras 	of_node_put(cpu);
560ab1f9dacSPaul Mackerras 
561cf950b7aSNathan Lynch 	return nid;
562ab1f9dacSPaul Mackerras }
563ab1f9dacSPaul Mackerras 
56474b85f37SChandra Seetharaman static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
565ab1f9dacSPaul Mackerras 			     unsigned long action,
566ab1f9dacSPaul Mackerras 			     void *hcpu)
567ab1f9dacSPaul Mackerras {
568ab1f9dacSPaul Mackerras 	unsigned long lcpu = (unsigned long)hcpu;
569ab1f9dacSPaul Mackerras 	int ret = NOTIFY_DONE;
570ab1f9dacSPaul Mackerras 
571ab1f9dacSPaul Mackerras 	switch (action) {
572ab1f9dacSPaul Mackerras 	case CPU_UP_PREPARE:
5738bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
574ab1f9dacSPaul Mackerras 		numa_setup_cpu(lcpu);
575ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
576ab1f9dacSPaul Mackerras 		break;
577ab1f9dacSPaul Mackerras #ifdef CONFIG_HOTPLUG_CPU
578ab1f9dacSPaul Mackerras 	case CPU_DEAD:
5798bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
580ab1f9dacSPaul Mackerras 	case CPU_UP_CANCELED:
5818bb78442SRafael J. Wysocki 	case CPU_UP_CANCELED_FROZEN:
582ab1f9dacSPaul Mackerras 		unmap_cpu_from_node(lcpu);
583ab1f9dacSPaul Mackerras 		break;
584ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
585ab1f9dacSPaul Mackerras #endif
586ab1f9dacSPaul Mackerras 	}
587ab1f9dacSPaul Mackerras 	return ret;
588ab1f9dacSPaul Mackerras }
589ab1f9dacSPaul Mackerras 
590ab1f9dacSPaul Mackerras /*
591ab1f9dacSPaul Mackerras  * Check and possibly modify a memory region to enforce the memory limit.
592ab1f9dacSPaul Mackerras  *
593ab1f9dacSPaul Mackerras  * Returns the size the region should have to enforce the memory limit.
594ab1f9dacSPaul Mackerras  * This will either be the original value of size, a truncated value,
595ab1f9dacSPaul Mackerras  * or zero. If the returned value of size is 0 the region should be
59625985edcSLucas De Marchi  * discarded as it lies wholly above the memory limit.
597ab1f9dacSPaul Mackerras  */
59845fb6ceaSAnton Blanchard static unsigned long __init numa_enforce_memory_limit(unsigned long start,
59945fb6ceaSAnton Blanchard 						      unsigned long size)
600ab1f9dacSPaul Mackerras {
601ab1f9dacSPaul Mackerras 	/*
60295f72d1eSYinghai Lu 	 * We use memblock_end_of_DRAM() in here instead of memory_limit because
603ab1f9dacSPaul Mackerras 	 * we've already adjusted it for the limit and it takes care of
604fe55249dSMilton Miller 	 * having memory holes below the limit.  Also, in the case of
605fe55249dSMilton Miller 	 * iommu_is_off, memory_limit is not set but is implicitly enforced.
606ab1f9dacSPaul Mackerras 	 */
607ab1f9dacSPaul Mackerras 
60895f72d1eSYinghai Lu 	if (start + size <= memblock_end_of_DRAM())
609ab1f9dacSPaul Mackerras 		return size;
610ab1f9dacSPaul Mackerras 
61195f72d1eSYinghai Lu 	if (start >= memblock_end_of_DRAM())
612ab1f9dacSPaul Mackerras 		return 0;
613ab1f9dacSPaul Mackerras 
61495f72d1eSYinghai Lu 	return memblock_end_of_DRAM() - start;
615ab1f9dacSPaul Mackerras }
616ab1f9dacSPaul Mackerras 
6170204568aSPaul Mackerras /*
618cf00085dSChandru  * Reads the counter for a given entry in
619cf00085dSChandru  * linux,drconf-usable-memory property
620cf00085dSChandru  */
621cf00085dSChandru static inline int __init read_usm_ranges(const u32 **usm)
622cf00085dSChandru {
623cf00085dSChandru 	/*
6243fdfd990SBenjamin Herrenschmidt 	 * For each lmb in ibm,dynamic-memory a corresponding
625cf00085dSChandru 	 * entry in linux,drconf-usable-memory property contains
626cf00085dSChandru 	 * a counter followed by that many (base, size) duple.
627cf00085dSChandru 	 * read the counter from linux,drconf-usable-memory
628cf00085dSChandru 	 */
629cf00085dSChandru 	return read_n_cells(n_mem_size_cells, usm);
630cf00085dSChandru }
631cf00085dSChandru 
632cf00085dSChandru /*
6330204568aSPaul Mackerras  * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
6340204568aSPaul Mackerras  * node.  This assumes n_mem_{addr,size}_cells have been set.
6350204568aSPaul Mackerras  */
6360204568aSPaul Mackerras static void __init parse_drconf_memory(struct device_node *memory)
6370204568aSPaul Mackerras {
638*82b2521dSMichael Neuling 	const u32 *uninitialized_var(dm), *usm;
639cf00085dSChandru 	unsigned int n, rc, ranges, is_kexec_kdump = 0;
6403fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size, base, size, sz;
6418342681dSNathan Fontenot 	int nid;
6428342681dSNathan Fontenot 	struct assoc_arrays aa;
6430204568aSPaul Mackerras 
6448342681dSNathan Fontenot 	n = of_get_drconf_memory(memory, &dm);
6458342681dSNathan Fontenot 	if (!n)
6460204568aSPaul Mackerras 		return;
6470204568aSPaul Mackerras 
6483fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
6493fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
6508342681dSNathan Fontenot 		return;
6518342681dSNathan Fontenot 
6528342681dSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
6538342681dSNathan Fontenot 	if (rc)
6540204568aSPaul Mackerras 		return;
6550204568aSPaul Mackerras 
656cf00085dSChandru 	/* check if this is a kexec/kdump kernel */
657cf00085dSChandru 	usm = of_get_usable_memory(memory);
658cf00085dSChandru 	if (usm != NULL)
659cf00085dSChandru 		is_kexec_kdump = 1;
660cf00085dSChandru 
6610204568aSPaul Mackerras 	for (; n != 0; --n) {
6628342681dSNathan Fontenot 		struct of_drconf_cell drmem;
6631daa6d08SBalbir Singh 
6648342681dSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
6658342681dSNathan Fontenot 
6668342681dSNathan Fontenot 		/* skip this block if the reserved bit is set in flags (0x80)
6678342681dSNathan Fontenot 		   or if the block is not assigned to this partition (0x8) */
6688342681dSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
6698342681dSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
6708342681dSNathan Fontenot 			continue;
6718342681dSNathan Fontenot 
672cf00085dSChandru 		base = drmem.base_addr;
6733fdfd990SBenjamin Herrenschmidt 		size = lmb_size;
674cf00085dSChandru 		ranges = 1;
6758342681dSNathan Fontenot 
676cf00085dSChandru 		if (is_kexec_kdump) {
677cf00085dSChandru 			ranges = read_usm_ranges(&usm);
678cf00085dSChandru 			if (!ranges) /* there are no (base, size) duple */
6790204568aSPaul Mackerras 				continue;
680cf00085dSChandru 		}
681cf00085dSChandru 		do {
682cf00085dSChandru 			if (is_kexec_kdump) {
683cf00085dSChandru 				base = read_n_cells(n_mem_addr_cells, &usm);
684cf00085dSChandru 				size = read_n_cells(n_mem_size_cells, &usm);
685cf00085dSChandru 			}
686cf00085dSChandru 			nid = of_drconf_to_nid_single(&drmem, &aa);
687cf00085dSChandru 			fake_numa_create_new_node(
688cf00085dSChandru 				((base + size) >> PAGE_SHIFT),
689cf00085dSChandru 					   &nid);
690cf00085dSChandru 			node_set_online(nid);
691cf00085dSChandru 			sz = numa_enforce_memory_limit(base, size);
692cf00085dSChandru 			if (sz)
6931d7cfe18STejun Heo 				memblock_set_node(base, sz, nid);
694cf00085dSChandru 		} while (--ranges);
6950204568aSPaul Mackerras 	}
6960204568aSPaul Mackerras }
6970204568aSPaul Mackerras 
698ab1f9dacSPaul Mackerras static int __init parse_numa_properties(void)
699ab1f9dacSPaul Mackerras {
70094db7c5eSAnton Blanchard 	struct device_node *memory;
701482ec7c4SNathan Lynch 	int default_nid = 0;
702ab1f9dacSPaul Mackerras 	unsigned long i;
703ab1f9dacSPaul Mackerras 
704ab1f9dacSPaul Mackerras 	if (numa_enabled == 0) {
705ab1f9dacSPaul Mackerras 		printk(KERN_WARNING "NUMA disabled by user\n");
706ab1f9dacSPaul Mackerras 		return -1;
707ab1f9dacSPaul Mackerras 	}
708ab1f9dacSPaul Mackerras 
709ab1f9dacSPaul Mackerras 	min_common_depth = find_min_common_depth();
710ab1f9dacSPaul Mackerras 
711ab1f9dacSPaul Mackerras 	if (min_common_depth < 0)
712ab1f9dacSPaul Mackerras 		return min_common_depth;
713ab1f9dacSPaul Mackerras 
714bf4b85b0SNathan Lynch 	dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
715bf4b85b0SNathan Lynch 
716ab1f9dacSPaul Mackerras 	/*
717482ec7c4SNathan Lynch 	 * Even though we connect cpus to numa domains later in SMP
718482ec7c4SNathan Lynch 	 * init, we need to know the node ids now. This is because
719482ec7c4SNathan Lynch 	 * each node to be onlined must have NODE_DATA etc backing it.
720ab1f9dacSPaul Mackerras 	 */
721482ec7c4SNathan Lynch 	for_each_present_cpu(i) {
722dfbe93a2SAnton Blanchard 		struct device_node *cpu;
723cf950b7aSNathan Lynch 		int nid;
724ab1f9dacSPaul Mackerras 
7258b16cd23SMilton Miller 		cpu = of_get_cpu_node(i, NULL);
726482ec7c4SNathan Lynch 		BUG_ON(!cpu);
727953039c8SJeremy Kerr 		nid = of_node_to_nid_single(cpu);
728ab1f9dacSPaul Mackerras 		of_node_put(cpu);
729ab1f9dacSPaul Mackerras 
730482ec7c4SNathan Lynch 		/*
731482ec7c4SNathan Lynch 		 * Don't fall back to default_nid yet -- we will plug
732482ec7c4SNathan Lynch 		 * cpus into nodes once the memory scan has discovered
733482ec7c4SNathan Lynch 		 * the topology.
734482ec7c4SNathan Lynch 		 */
735482ec7c4SNathan Lynch 		if (nid < 0)
736482ec7c4SNathan Lynch 			continue;
737482ec7c4SNathan Lynch 		node_set_online(nid);
738ab1f9dacSPaul Mackerras 	}
739ab1f9dacSPaul Mackerras 
740237a0989SMike Kravetz 	get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
74194db7c5eSAnton Blanchard 
74294db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
743ab1f9dacSPaul Mackerras 		unsigned long start;
744ab1f9dacSPaul Mackerras 		unsigned long size;
745cf950b7aSNathan Lynch 		int nid;
746ab1f9dacSPaul Mackerras 		int ranges;
747a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
748ab1f9dacSPaul Mackerras 		unsigned int len;
749ab1f9dacSPaul Mackerras 
750e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory,
751ba759485SMichael Ellerman 			"linux,usable-memory", &len);
752ba759485SMichael Ellerman 		if (!memcell_buf || len <= 0)
753e2eb6392SStephen Rothwell 			memcell_buf = of_get_property(memory, "reg", &len);
754ab1f9dacSPaul Mackerras 		if (!memcell_buf || len <= 0)
755ab1f9dacSPaul Mackerras 			continue;
756ab1f9dacSPaul Mackerras 
757cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
758cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
759ab1f9dacSPaul Mackerras new_range:
760ab1f9dacSPaul Mackerras 		/* these are order-sensitive, and modify the buffer pointer */
761237a0989SMike Kravetz 		start = read_n_cells(n_mem_addr_cells, &memcell_buf);
762237a0989SMike Kravetz 		size = read_n_cells(n_mem_size_cells, &memcell_buf);
763ab1f9dacSPaul Mackerras 
764482ec7c4SNathan Lynch 		/*
765482ec7c4SNathan Lynch 		 * Assumption: either all memory nodes or none will
766482ec7c4SNathan Lynch 		 * have associativity properties.  If none, then
767482ec7c4SNathan Lynch 		 * everything goes to default_nid.
768482ec7c4SNathan Lynch 		 */
769953039c8SJeremy Kerr 		nid = of_node_to_nid_single(memory);
770482ec7c4SNathan Lynch 		if (nid < 0)
771482ec7c4SNathan Lynch 			nid = default_nid;
7721daa6d08SBalbir Singh 
7731daa6d08SBalbir Singh 		fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
774482ec7c4SNathan Lynch 		node_set_online(nid);
775ab1f9dacSPaul Mackerras 
776ab1f9dacSPaul Mackerras 		if (!(size = numa_enforce_memory_limit(start, size))) {
777ab1f9dacSPaul Mackerras 			if (--ranges)
778ab1f9dacSPaul Mackerras 				goto new_range;
779ab1f9dacSPaul Mackerras 			else
780ab1f9dacSPaul Mackerras 				continue;
781ab1f9dacSPaul Mackerras 		}
782ab1f9dacSPaul Mackerras 
7831d7cfe18STejun Heo 		memblock_set_node(start, size, nid);
784ab1f9dacSPaul Mackerras 
785ab1f9dacSPaul Mackerras 		if (--ranges)
786ab1f9dacSPaul Mackerras 			goto new_range;
787ab1f9dacSPaul Mackerras 	}
788ab1f9dacSPaul Mackerras 
7890204568aSPaul Mackerras 	/*
790dfbe93a2SAnton Blanchard 	 * Now do the same thing for each MEMBLOCK listed in the
791dfbe93a2SAnton Blanchard 	 * ibm,dynamic-memory property in the
792dfbe93a2SAnton Blanchard 	 * ibm,dynamic-reconfiguration-memory node.
7930204568aSPaul Mackerras 	 */
7940204568aSPaul Mackerras 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
7950204568aSPaul Mackerras 	if (memory)
7960204568aSPaul Mackerras 		parse_drconf_memory(memory);
7970204568aSPaul Mackerras 
798ab1f9dacSPaul Mackerras 	return 0;
799ab1f9dacSPaul Mackerras }
800ab1f9dacSPaul Mackerras 
801ab1f9dacSPaul Mackerras static void __init setup_nonnuma(void)
802ab1f9dacSPaul Mackerras {
80395f72d1eSYinghai Lu 	unsigned long top_of_ram = memblock_end_of_DRAM();
80495f72d1eSYinghai Lu 	unsigned long total_ram = memblock_phys_mem_size();
805c67c3cb4SMel Gorman 	unsigned long start_pfn, end_pfn;
80628be7072SBenjamin Herrenschmidt 	unsigned int nid = 0;
80728be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
808ab1f9dacSPaul Mackerras 
809e110b281SOlof Johansson 	printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
810ab1f9dacSPaul Mackerras 	       top_of_ram, total_ram);
811e110b281SOlof Johansson 	printk(KERN_DEBUG "Memory hole size: %ldMB\n",
812ab1f9dacSPaul Mackerras 	       (top_of_ram - total_ram) >> 20);
813ab1f9dacSPaul Mackerras 
81428be7072SBenjamin Herrenschmidt 	for_each_memblock(memory, reg) {
815c7fc2de0SYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(reg);
816c7fc2de0SYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(reg);
8171daa6d08SBalbir Singh 
8181daa6d08SBalbir Singh 		fake_numa_create_new_node(end_pfn, &nid);
8191d7cfe18STejun Heo 		memblock_set_node(PFN_PHYS(start_pfn),
8201d7cfe18STejun Heo 				  PFN_PHYS(end_pfn - start_pfn), nid);
8211daa6d08SBalbir Singh 		node_set_online(nid);
822c67c3cb4SMel Gorman 	}
823ab1f9dacSPaul Mackerras }
824ab1f9dacSPaul Mackerras 
8254b703a23SAnton Blanchard void __init dump_numa_cpu_topology(void)
8264b703a23SAnton Blanchard {
8274b703a23SAnton Blanchard 	unsigned int node;
8284b703a23SAnton Blanchard 	unsigned int cpu, count;
8294b703a23SAnton Blanchard 
8304b703a23SAnton Blanchard 	if (min_common_depth == -1 || !numa_enabled)
8314b703a23SAnton Blanchard 		return;
8324b703a23SAnton Blanchard 
8334b703a23SAnton Blanchard 	for_each_online_node(node) {
834e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d CPUs:", node);
8354b703a23SAnton Blanchard 
8364b703a23SAnton Blanchard 		count = 0;
8374b703a23SAnton Blanchard 		/*
8384b703a23SAnton Blanchard 		 * If we used a CPU iterator here we would miss printing
8394b703a23SAnton Blanchard 		 * the holes in the cpumap.
8404b703a23SAnton Blanchard 		 */
84125863de0SAnton Blanchard 		for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
84225863de0SAnton Blanchard 			if (cpumask_test_cpu(cpu,
84325863de0SAnton Blanchard 					node_to_cpumask_map[node])) {
8444b703a23SAnton Blanchard 				if (count == 0)
8454b703a23SAnton Blanchard 					printk(" %u", cpu);
8464b703a23SAnton Blanchard 				++count;
8474b703a23SAnton Blanchard 			} else {
8484b703a23SAnton Blanchard 				if (count > 1)
8494b703a23SAnton Blanchard 					printk("-%u", cpu - 1);
8504b703a23SAnton Blanchard 				count = 0;
8514b703a23SAnton Blanchard 			}
8524b703a23SAnton Blanchard 		}
8534b703a23SAnton Blanchard 
8544b703a23SAnton Blanchard 		if (count > 1)
85525863de0SAnton Blanchard 			printk("-%u", nr_cpu_ids - 1);
8564b703a23SAnton Blanchard 		printk("\n");
8574b703a23SAnton Blanchard 	}
8584b703a23SAnton Blanchard }
8594b703a23SAnton Blanchard 
8604b703a23SAnton Blanchard static void __init dump_numa_memory_topology(void)
861ab1f9dacSPaul Mackerras {
862ab1f9dacSPaul Mackerras 	unsigned int node;
863ab1f9dacSPaul Mackerras 	unsigned int count;
864ab1f9dacSPaul Mackerras 
865ab1f9dacSPaul Mackerras 	if (min_common_depth == -1 || !numa_enabled)
866ab1f9dacSPaul Mackerras 		return;
867ab1f9dacSPaul Mackerras 
868ab1f9dacSPaul Mackerras 	for_each_online_node(node) {
869ab1f9dacSPaul Mackerras 		unsigned long i;
870ab1f9dacSPaul Mackerras 
871e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d Memory:", node);
872ab1f9dacSPaul Mackerras 
873ab1f9dacSPaul Mackerras 		count = 0;
874ab1f9dacSPaul Mackerras 
87595f72d1eSYinghai Lu 		for (i = 0; i < memblock_end_of_DRAM();
87645fb6ceaSAnton Blanchard 		     i += (1 << SECTION_SIZE_BITS)) {
87745fb6ceaSAnton Blanchard 			if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
878ab1f9dacSPaul Mackerras 				if (count == 0)
879ab1f9dacSPaul Mackerras 					printk(" 0x%lx", i);
880ab1f9dacSPaul Mackerras 				++count;
881ab1f9dacSPaul Mackerras 			} else {
882ab1f9dacSPaul Mackerras 				if (count > 0)
883ab1f9dacSPaul Mackerras 					printk("-0x%lx", i);
884ab1f9dacSPaul Mackerras 				count = 0;
885ab1f9dacSPaul Mackerras 			}
886ab1f9dacSPaul Mackerras 		}
887ab1f9dacSPaul Mackerras 
888ab1f9dacSPaul Mackerras 		if (count > 0)
889ab1f9dacSPaul Mackerras 			printk("-0x%lx", i);
890ab1f9dacSPaul Mackerras 		printk("\n");
891ab1f9dacSPaul Mackerras 	}
892ab1f9dacSPaul Mackerras }
893ab1f9dacSPaul Mackerras 
894ab1f9dacSPaul Mackerras /*
89595f72d1eSYinghai Lu  * Allocate some memory, satisfying the memblock or bootmem allocator where
896ab1f9dacSPaul Mackerras  * required. nid is the preferred node and end is the physical address of
897ab1f9dacSPaul Mackerras  * the highest address in the node.
898ab1f9dacSPaul Mackerras  *
8990be210fdSDave Hansen  * Returns the virtual address of the memory.
900ab1f9dacSPaul Mackerras  */
901893473dfSDave Hansen static void __init *careful_zallocation(int nid, unsigned long size,
90245fb6ceaSAnton Blanchard 				       unsigned long align,
90345fb6ceaSAnton Blanchard 				       unsigned long end_pfn)
904ab1f9dacSPaul Mackerras {
9050be210fdSDave Hansen 	void *ret;
90645fb6ceaSAnton Blanchard 	int new_nid;
9070be210fdSDave Hansen 	unsigned long ret_paddr;
9080be210fdSDave Hansen 
90995f72d1eSYinghai Lu 	ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
910ab1f9dacSPaul Mackerras 
911ab1f9dacSPaul Mackerras 	/* retry over all memory */
9120be210fdSDave Hansen 	if (!ret_paddr)
91395f72d1eSYinghai Lu 		ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
914ab1f9dacSPaul Mackerras 
9150be210fdSDave Hansen 	if (!ret_paddr)
9165d21ea2bSDave Hansen 		panic("numa.c: cannot allocate %lu bytes for node %d",
917ab1f9dacSPaul Mackerras 		      size, nid);
918ab1f9dacSPaul Mackerras 
9190be210fdSDave Hansen 	ret = __va(ret_paddr);
9200be210fdSDave Hansen 
921ab1f9dacSPaul Mackerras 	/*
922c555e520SDave Hansen 	 * We initialize the nodes in numeric order: 0, 1, 2...
92395f72d1eSYinghai Lu 	 * and hand over control from the MEMBLOCK allocator to the
924c555e520SDave Hansen 	 * bootmem allocator.  If this function is called for
925c555e520SDave Hansen 	 * node 5, then we know that all nodes <5 are using the
92695f72d1eSYinghai Lu 	 * bootmem allocator instead of the MEMBLOCK allocator.
927c555e520SDave Hansen 	 *
928c555e520SDave Hansen 	 * So, check the nid from which this allocation came
929c555e520SDave Hansen 	 * and double check to see if we need to use bootmem
93095f72d1eSYinghai Lu 	 * instead of the MEMBLOCK.  We don't free the MEMBLOCK memory
931c555e520SDave Hansen 	 * since it would be useless.
932ab1f9dacSPaul Mackerras 	 */
9330be210fdSDave Hansen 	new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
93445fb6ceaSAnton Blanchard 	if (new_nid < nid) {
9350be210fdSDave Hansen 		ret = __alloc_bootmem_node(NODE_DATA(new_nid),
936ab1f9dacSPaul Mackerras 				size, align, 0);
937ab1f9dacSPaul Mackerras 
9380be210fdSDave Hansen 		dbg("alloc_bootmem %p %lx\n", ret, size);
939ab1f9dacSPaul Mackerras 	}
940ab1f9dacSPaul Mackerras 
941893473dfSDave Hansen 	memset(ret, 0, size);
9420be210fdSDave Hansen 	return ret;
943ab1f9dacSPaul Mackerras }
944ab1f9dacSPaul Mackerras 
94574b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata ppc64_numa_nb = {
94674b85f37SChandra Seetharaman 	.notifier_call = cpu_numa_callback,
94774b85f37SChandra Seetharaman 	.priority = 1 /* Must run before sched domains notifier. */
94874b85f37SChandra Seetharaman };
94974b85f37SChandra Seetharaman 
95028e86bdbSDavid Rientjes static void __init mark_reserved_regions_for_nid(int nid)
951ab1f9dacSPaul Mackerras {
9524a618669SDave Hansen 	struct pglist_data *node = NODE_DATA(nid);
95328be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
954ab1f9dacSPaul Mackerras 
95528be7072SBenjamin Herrenschmidt 	for_each_memblock(reserved, reg) {
95628be7072SBenjamin Herrenschmidt 		unsigned long physbase = reg->base;
95728be7072SBenjamin Herrenschmidt 		unsigned long size = reg->size;
9588f64e1f2SJon Tollefson 		unsigned long start_pfn = physbase >> PAGE_SHIFT;
95906eccea6SDave Hansen 		unsigned long end_pfn = PFN_UP(physbase + size);
9608f64e1f2SJon Tollefson 		struct node_active_region node_ar;
9614a618669SDave Hansen 		unsigned long node_end_pfn = node->node_start_pfn +
9624a618669SDave Hansen 					     node->node_spanned_pages;
9634a618669SDave Hansen 
9644a618669SDave Hansen 		/*
96595f72d1eSYinghai Lu 		 * Check to make sure that this memblock.reserved area is
9664a618669SDave Hansen 		 * within the bounds of the node that we care about.
9674a618669SDave Hansen 		 * Checking the nid of the start and end points is not
9684a618669SDave Hansen 		 * sufficient because the reserved area could span the
9694a618669SDave Hansen 		 * entire node.
9704a618669SDave Hansen 		 */
9714a618669SDave Hansen 		if (end_pfn <= node->node_start_pfn ||
9724a618669SDave Hansen 		    start_pfn >= node_end_pfn)
9734a618669SDave Hansen 			continue;
974ab1f9dacSPaul Mackerras 
9758f64e1f2SJon Tollefson 		get_node_active_region(start_pfn, &node_ar);
976e8170372SJon Tollefson 		while (start_pfn < end_pfn &&
977e8170372SJon Tollefson 			node_ar.start_pfn < node_ar.end_pfn) {
978e8170372SJon Tollefson 			unsigned long reserve_size = size;
9798f64e1f2SJon Tollefson 			/*
9808f64e1f2SJon Tollefson 			 * if reserved region extends past active region
9818f64e1f2SJon Tollefson 			 * then trim size to active region
9828f64e1f2SJon Tollefson 			 */
9838f64e1f2SJon Tollefson 			if (end_pfn > node_ar.end_pfn)
984e8170372SJon Tollefson 				reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
98506eccea6SDave Hansen 					- physbase;
986a4c74dddSDave Hansen 			/*
987a4c74dddSDave Hansen 			 * Only worry about *this* node, others may not
988a4c74dddSDave Hansen 			 * yet have valid NODE_DATA().
989a4c74dddSDave Hansen 			 */
990a4c74dddSDave Hansen 			if (node_ar.nid == nid) {
991a4c74dddSDave Hansen 				dbg("reserve_bootmem %lx %lx nid=%d\n",
992a4c74dddSDave Hansen 					physbase, reserve_size, node_ar.nid);
993a4c74dddSDave Hansen 				reserve_bootmem_node(NODE_DATA(node_ar.nid),
994a4c74dddSDave Hansen 						physbase, reserve_size,
995a4c74dddSDave Hansen 						BOOTMEM_DEFAULT);
996a4c74dddSDave Hansen 			}
9978f64e1f2SJon Tollefson 			/*
9988f64e1f2SJon Tollefson 			 * if reserved region is contained in the active region
9998f64e1f2SJon Tollefson 			 * then done.
10008f64e1f2SJon Tollefson 			 */
10018f64e1f2SJon Tollefson 			if (end_pfn <= node_ar.end_pfn)
10028f64e1f2SJon Tollefson 				break;
10038f64e1f2SJon Tollefson 
10048f64e1f2SJon Tollefson 			/*
10058f64e1f2SJon Tollefson 			 * reserved region extends past the active region
10068f64e1f2SJon Tollefson 			 *   get next active region that contains this
10078f64e1f2SJon Tollefson 			 *   reserved region
10088f64e1f2SJon Tollefson 			 */
10098f64e1f2SJon Tollefson 			start_pfn = node_ar.end_pfn;
10108f64e1f2SJon Tollefson 			physbase = start_pfn << PAGE_SHIFT;
1011e8170372SJon Tollefson 			size = size - reserve_size;
10128f64e1f2SJon Tollefson 			get_node_active_region(start_pfn, &node_ar);
1013ab1f9dacSPaul Mackerras 		}
10144a618669SDave Hansen 	}
1015ab1f9dacSPaul Mackerras }
10168f64e1f2SJon Tollefson 
10174a618669SDave Hansen 
10184a618669SDave Hansen void __init do_init_bootmem(void)
10194a618669SDave Hansen {
10204a618669SDave Hansen 	int nid;
10214a618669SDave Hansen 
10224a618669SDave Hansen 	min_low_pfn = 0;
102395f72d1eSYinghai Lu 	max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
10244a618669SDave Hansen 	max_pfn = max_low_pfn;
10254a618669SDave Hansen 
10264a618669SDave Hansen 	if (parse_numa_properties())
10274a618669SDave Hansen 		setup_nonnuma();
10284a618669SDave Hansen 	else
10294a618669SDave Hansen 		dump_numa_memory_topology();
10304a618669SDave Hansen 
10314a618669SDave Hansen 	for_each_online_node(nid) {
10324a618669SDave Hansen 		unsigned long start_pfn, end_pfn;
10330be210fdSDave Hansen 		void *bootmem_vaddr;
10344a618669SDave Hansen 		unsigned long bootmap_pages;
10354a618669SDave Hansen 
10364a618669SDave Hansen 		get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
10374a618669SDave Hansen 
10384a618669SDave Hansen 		/*
10394a618669SDave Hansen 		 * Allocate the node structure node local if possible
10404a618669SDave Hansen 		 *
10414a618669SDave Hansen 		 * Be careful moving this around, as it relies on all
10424a618669SDave Hansen 		 * previous nodes' bootmem to be initialized and have
10434a618669SDave Hansen 		 * all reserved areas marked.
10444a618669SDave Hansen 		 */
1045893473dfSDave Hansen 		NODE_DATA(nid) = careful_zallocation(nid,
10464a618669SDave Hansen 					sizeof(struct pglist_data),
10474a618669SDave Hansen 					SMP_CACHE_BYTES, end_pfn);
10484a618669SDave Hansen 
10494a618669SDave Hansen   		dbg("node %d\n", nid);
10504a618669SDave Hansen 		dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
10514a618669SDave Hansen 
10524a618669SDave Hansen 		NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
10534a618669SDave Hansen 		NODE_DATA(nid)->node_start_pfn = start_pfn;
10544a618669SDave Hansen 		NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
10554a618669SDave Hansen 
10564a618669SDave Hansen 		if (NODE_DATA(nid)->node_spanned_pages == 0)
10574a618669SDave Hansen   			continue;
10584a618669SDave Hansen 
10594a618669SDave Hansen   		dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
10604a618669SDave Hansen   		dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
10614a618669SDave Hansen 
10624a618669SDave Hansen 		bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
1063893473dfSDave Hansen 		bootmem_vaddr = careful_zallocation(nid,
10644a618669SDave Hansen 					bootmap_pages << PAGE_SHIFT,
10654a618669SDave Hansen 					PAGE_SIZE, end_pfn);
10664a618669SDave Hansen 
10670be210fdSDave Hansen 		dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
10684a618669SDave Hansen 
10690be210fdSDave Hansen 		init_bootmem_node(NODE_DATA(nid),
10700be210fdSDave Hansen 				  __pa(bootmem_vaddr) >> PAGE_SHIFT,
10714a618669SDave Hansen 				  start_pfn, end_pfn);
10724a618669SDave Hansen 
10734a618669SDave Hansen 		free_bootmem_with_active_regions(nid, end_pfn);
10744a618669SDave Hansen 		/*
10754a618669SDave Hansen 		 * Be very careful about moving this around.  Future
1076893473dfSDave Hansen 		 * calls to careful_zallocation() depend on this getting
10774a618669SDave Hansen 		 * done correctly.
10784a618669SDave Hansen 		 */
10794a618669SDave Hansen 		mark_reserved_regions_for_nid(nid);
10808f64e1f2SJon Tollefson 		sparse_memory_present_with_active_regions(nid);
1081ab1f9dacSPaul Mackerras 	}
1082d3f6204aSBenjamin Herrenschmidt 
1083d3f6204aSBenjamin Herrenschmidt 	init_bootmem_done = 1;
108425863de0SAnton Blanchard 
108525863de0SAnton Blanchard 	/*
108625863de0SAnton Blanchard 	 * Now bootmem is initialised we can create the node to cpumask
108725863de0SAnton Blanchard 	 * lookup tables and setup the cpu callback to populate them.
108825863de0SAnton Blanchard 	 */
108925863de0SAnton Blanchard 	setup_node_to_cpumask_map();
109025863de0SAnton Blanchard 
109125863de0SAnton Blanchard 	register_cpu_notifier(&ppc64_numa_nb);
109225863de0SAnton Blanchard 	cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
109325863de0SAnton Blanchard 			  (void *)(unsigned long)boot_cpuid);
10944a618669SDave Hansen }
1095ab1f9dacSPaul Mackerras 
1096ab1f9dacSPaul Mackerras void __init paging_init(void)
1097ab1f9dacSPaul Mackerras {
10986391af17SMel Gorman 	unsigned long max_zone_pfns[MAX_NR_ZONES];
10996391af17SMel Gorman 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
110095f72d1eSYinghai Lu 	max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
1101c67c3cb4SMel Gorman 	free_area_init_nodes(max_zone_pfns);
1102ab1f9dacSPaul Mackerras }
1103ab1f9dacSPaul Mackerras 
1104ab1f9dacSPaul Mackerras static int __init early_numa(char *p)
1105ab1f9dacSPaul Mackerras {
1106ab1f9dacSPaul Mackerras 	if (!p)
1107ab1f9dacSPaul Mackerras 		return 0;
1108ab1f9dacSPaul Mackerras 
1109ab1f9dacSPaul Mackerras 	if (strstr(p, "off"))
1110ab1f9dacSPaul Mackerras 		numa_enabled = 0;
1111ab1f9dacSPaul Mackerras 
1112ab1f9dacSPaul Mackerras 	if (strstr(p, "debug"))
1113ab1f9dacSPaul Mackerras 		numa_debug = 1;
1114ab1f9dacSPaul Mackerras 
11151daa6d08SBalbir Singh 	p = strstr(p, "fake=");
11161daa6d08SBalbir Singh 	if (p)
11171daa6d08SBalbir Singh 		cmdline = p + strlen("fake=");
11181daa6d08SBalbir Singh 
1119ab1f9dacSPaul Mackerras 	return 0;
1120ab1f9dacSPaul Mackerras }
1121ab1f9dacSPaul Mackerras early_param("numa", early_numa);
1122237a0989SMike Kravetz 
1123237a0989SMike Kravetz #ifdef CONFIG_MEMORY_HOTPLUG
1124237a0989SMike Kravetz /*
11250f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for
11260f16ef7fSNathan Fontenot  * memory represented in the device tree by the property
11270f16ef7fSNathan Fontenot  * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
11280db9360aSNathan Fontenot  */
11290db9360aSNathan Fontenot static int hot_add_drconf_scn_to_nid(struct device_node *memory,
11300db9360aSNathan Fontenot 				     unsigned long scn_addr)
11310db9360aSNathan Fontenot {
11320db9360aSNathan Fontenot 	const u32 *dm;
11330f16ef7fSNathan Fontenot 	unsigned int drconf_cell_cnt, rc;
11343fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size;
11350db9360aSNathan Fontenot 	struct assoc_arrays aa;
11360f16ef7fSNathan Fontenot 	int nid = -1;
11370db9360aSNathan Fontenot 
11380f16ef7fSNathan Fontenot 	drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
11390f16ef7fSNathan Fontenot 	if (!drconf_cell_cnt)
11400f16ef7fSNathan Fontenot 		return -1;
11410db9360aSNathan Fontenot 
11423fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
11433fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
11440f16ef7fSNathan Fontenot 		return -1;
11450db9360aSNathan Fontenot 
11460db9360aSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
11470db9360aSNathan Fontenot 	if (rc)
11480f16ef7fSNathan Fontenot 		return -1;
11490db9360aSNathan Fontenot 
11500f16ef7fSNathan Fontenot 	for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
11510db9360aSNathan Fontenot 		struct of_drconf_cell drmem;
11520db9360aSNathan Fontenot 
11530db9360aSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
11540db9360aSNathan Fontenot 
11550db9360aSNathan Fontenot 		/* skip this block if it is reserved or not assigned to
11560db9360aSNathan Fontenot 		 * this partition */
11570db9360aSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
11580db9360aSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
11590db9360aSNathan Fontenot 			continue;
11600db9360aSNathan Fontenot 
11610f16ef7fSNathan Fontenot 		if ((scn_addr < drmem.base_addr)
11623fdfd990SBenjamin Herrenschmidt 		    || (scn_addr >= (drmem.base_addr + lmb_size)))
11630f16ef7fSNathan Fontenot 			continue;
11640db9360aSNathan Fontenot 
11650f16ef7fSNathan Fontenot 		nid = of_drconf_to_nid_single(&drmem, &aa);
11660f16ef7fSNathan Fontenot 		break;
11670db9360aSNathan Fontenot 	}
11680db9360aSNathan Fontenot 
11690f16ef7fSNathan Fontenot 	return nid;
11700db9360aSNathan Fontenot }
11710db9360aSNathan Fontenot 
11720db9360aSNathan Fontenot /*
11730f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for memory
11740f16ef7fSNathan Fontenot  * represented in the device tree as a node (i.e. memory@XXXX) for
117595f72d1eSYinghai Lu  * each memblock.
1176237a0989SMike Kravetz  */
11770f16ef7fSNathan Fontenot int hot_add_node_scn_to_nid(unsigned long scn_addr)
1178237a0989SMike Kravetz {
117994db7c5eSAnton Blanchard 	struct device_node *memory;
11800f16ef7fSNathan Fontenot 	int nid = -1;
1181237a0989SMike Kravetz 
118294db7c5eSAnton Blanchard 	for_each_node_by_type(memory, "memory") {
1183237a0989SMike Kravetz 		unsigned long start, size;
1184b226e462SMike Kravetz 		int ranges;
1185a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
1186237a0989SMike Kravetz 		unsigned int len;
1187237a0989SMike Kravetz 
1188e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory, "reg", &len);
1189237a0989SMike Kravetz 		if (!memcell_buf || len <= 0)
1190237a0989SMike Kravetz 			continue;
1191237a0989SMike Kravetz 
1192cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
1193cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
11940f16ef7fSNathan Fontenot 
11950f16ef7fSNathan Fontenot 		while (ranges--) {
1196237a0989SMike Kravetz 			start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1197237a0989SMike Kravetz 			size = read_n_cells(n_mem_size_cells, &memcell_buf);
1198237a0989SMike Kravetz 
11990f16ef7fSNathan Fontenot 			if ((scn_addr < start) || (scn_addr >= (start + size)))
12000f16ef7fSNathan Fontenot 				continue;
12010f16ef7fSNathan Fontenot 
12020f16ef7fSNathan Fontenot 			nid = of_node_to_nid_single(memory);
12030f16ef7fSNathan Fontenot 			break;
12040f16ef7fSNathan Fontenot 		}
12050f16ef7fSNathan Fontenot 
12060f16ef7fSNathan Fontenot 		if (nid >= 0)
12070f16ef7fSNathan Fontenot 			break;
12080f16ef7fSNathan Fontenot 	}
12090f16ef7fSNathan Fontenot 
121060831842SAnton Blanchard 	of_node_put(memory);
121160831842SAnton Blanchard 
12120db9360aSNathan Fontenot 	return nid;
1213237a0989SMike Kravetz }
1214237a0989SMike Kravetz 
12150f16ef7fSNathan Fontenot /*
12160f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section.  Section
121795f72d1eSYinghai Lu  * corresponds to a SPARSEMEM section, not an MEMBLOCK.  It is assumed that
121895f72d1eSYinghai Lu  * sections are fully contained within a single MEMBLOCK.
12190f16ef7fSNathan Fontenot  */
12200f16ef7fSNathan Fontenot int hot_add_scn_to_nid(unsigned long scn_addr)
12210f16ef7fSNathan Fontenot {
12220f16ef7fSNathan Fontenot 	struct device_node *memory = NULL;
12230f16ef7fSNathan Fontenot 	int nid, found = 0;
12240f16ef7fSNathan Fontenot 
12250f16ef7fSNathan Fontenot 	if (!numa_enabled || (min_common_depth < 0))
122672c33688SH Hartley Sweeten 		return first_online_node;
12270f16ef7fSNathan Fontenot 
12280f16ef7fSNathan Fontenot 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
12290f16ef7fSNathan Fontenot 	if (memory) {
12300f16ef7fSNathan Fontenot 		nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
12310f16ef7fSNathan Fontenot 		of_node_put(memory);
12320f16ef7fSNathan Fontenot 	} else {
12330f16ef7fSNathan Fontenot 		nid = hot_add_node_scn_to_nid(scn_addr);
1234237a0989SMike Kravetz 	}
12350f16ef7fSNathan Fontenot 
12360f16ef7fSNathan Fontenot 	if (nid < 0 || !node_online(nid))
123772c33688SH Hartley Sweeten 		nid = first_online_node;
12380f16ef7fSNathan Fontenot 
12390f16ef7fSNathan Fontenot 	if (NODE_DATA(nid)->node_spanned_pages)
12400f16ef7fSNathan Fontenot 		return nid;
12410f16ef7fSNathan Fontenot 
12420f16ef7fSNathan Fontenot 	for_each_online_node(nid) {
12430f16ef7fSNathan Fontenot 		if (NODE_DATA(nid)->node_spanned_pages) {
12440f16ef7fSNathan Fontenot 			found = 1;
12450f16ef7fSNathan Fontenot 			break;
1246237a0989SMike Kravetz 		}
12470f16ef7fSNathan Fontenot 	}
12480f16ef7fSNathan Fontenot 
12490f16ef7fSNathan Fontenot 	BUG_ON(!found);
12500f16ef7fSNathan Fontenot 	return nid;
12510f16ef7fSNathan Fontenot }
12520f16ef7fSNathan Fontenot 
1253cd34206eSNishanth Aravamudan static u64 hot_add_drconf_memory_max(void)
1254cd34206eSNishanth Aravamudan {
1255cd34206eSNishanth Aravamudan         struct device_node *memory = NULL;
1256cd34206eSNishanth Aravamudan         unsigned int drconf_cell_cnt = 0;
1257cd34206eSNishanth Aravamudan         u64 lmb_size = 0;
1258cd34206eSNishanth Aravamudan         const u32 *dm = 0;
1259cd34206eSNishanth Aravamudan 
1260cd34206eSNishanth Aravamudan         memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1261cd34206eSNishanth Aravamudan         if (memory) {
1262cd34206eSNishanth Aravamudan                 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1263cd34206eSNishanth Aravamudan                 lmb_size = of_get_lmb_size(memory);
1264cd34206eSNishanth Aravamudan                 of_node_put(memory);
1265cd34206eSNishanth Aravamudan         }
1266cd34206eSNishanth Aravamudan         return lmb_size * drconf_cell_cnt;
1267cd34206eSNishanth Aravamudan }
1268cd34206eSNishanth Aravamudan 
1269cd34206eSNishanth Aravamudan /*
1270cd34206eSNishanth Aravamudan  * memory_hotplug_max - return max address of memory that may be added
1271cd34206eSNishanth Aravamudan  *
1272cd34206eSNishanth Aravamudan  * This is currently only used on systems that support drconfig memory
1273cd34206eSNishanth Aravamudan  * hotplug.
1274cd34206eSNishanth Aravamudan  */
1275cd34206eSNishanth Aravamudan u64 memory_hotplug_max(void)
1276cd34206eSNishanth Aravamudan {
1277cd34206eSNishanth Aravamudan         return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1278cd34206eSNishanth Aravamudan }
1279237a0989SMike Kravetz #endif /* CONFIG_MEMORY_HOTPLUG */
12809eff1a38SJesse Larrew 
1281bd03403aSJesse Larrew /* Virtual Processor Home Node (VPHN) support */
128239bf990eSJesse Larrew #ifdef CONFIG_PPC_SPLPAR
12835de16699SAnton Blanchard static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
12849eff1a38SJesse Larrew static cpumask_t cpu_associativity_changes_mask;
12859eff1a38SJesse Larrew static int vphn_enabled;
12869eff1a38SJesse Larrew static void set_topology_timer(void);
12879eff1a38SJesse Larrew 
12889eff1a38SJesse Larrew /*
12899eff1a38SJesse Larrew  * Store the current values of the associativity change counters in the
12909eff1a38SJesse Larrew  * hypervisor.
12919eff1a38SJesse Larrew  */
12929eff1a38SJesse Larrew static void setup_cpu_associativity_change_counters(void)
12939eff1a38SJesse Larrew {
1294cd9d6cc7SJesse Larrew 	int cpu;
12959eff1a38SJesse Larrew 
12965de16699SAnton Blanchard 	/* The VPHN feature supports a maximum of 8 reference points */
12975de16699SAnton Blanchard 	BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
12985de16699SAnton Blanchard 
12999eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
1300cd9d6cc7SJesse Larrew 		int i;
13019eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
13029eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
13039eff1a38SJesse Larrew 
13045de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++)
13059eff1a38SJesse Larrew 			counts[i] = hypervisor_counts[i];
13069eff1a38SJesse Larrew 	}
13079eff1a38SJesse Larrew }
13089eff1a38SJesse Larrew 
13099eff1a38SJesse Larrew /*
13109eff1a38SJesse Larrew  * The hypervisor maintains a set of 8 associativity change counters in
13119eff1a38SJesse Larrew  * the VPA of each cpu that correspond to the associativity levels in the
13129eff1a38SJesse Larrew  * ibm,associativity-reference-points property. When an associativity
13139eff1a38SJesse Larrew  * level changes, the corresponding counter is incremented.
13149eff1a38SJesse Larrew  *
13159eff1a38SJesse Larrew  * Set a bit in cpu_associativity_changes_mask for each cpu whose home
13169eff1a38SJesse Larrew  * node associativity levels have changed.
13179eff1a38SJesse Larrew  *
13189eff1a38SJesse Larrew  * Returns the number of cpus with unhandled associativity changes.
13199eff1a38SJesse Larrew  */
13209eff1a38SJesse Larrew static int update_cpu_associativity_changes_mask(void)
13219eff1a38SJesse Larrew {
1322cd9d6cc7SJesse Larrew 	int cpu, nr_cpus = 0;
13239eff1a38SJesse Larrew 	cpumask_t *changes = &cpu_associativity_changes_mask;
13249eff1a38SJesse Larrew 
13259eff1a38SJesse Larrew 	cpumask_clear(changes);
13269eff1a38SJesse Larrew 
13279eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
13289eff1a38SJesse Larrew 		int i, changed = 0;
13299eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
13309eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
13319eff1a38SJesse Larrew 
13325de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++) {
1333d69043e8SAnton Blanchard 			if (hypervisor_counts[i] != counts[i]) {
13349eff1a38SJesse Larrew 				counts[i] = hypervisor_counts[i];
13359eff1a38SJesse Larrew 				changed = 1;
13369eff1a38SJesse Larrew 			}
13379eff1a38SJesse Larrew 		}
13389eff1a38SJesse Larrew 		if (changed) {
13399eff1a38SJesse Larrew 			cpumask_set_cpu(cpu, changes);
13409eff1a38SJesse Larrew 			nr_cpus++;
13419eff1a38SJesse Larrew 		}
13429eff1a38SJesse Larrew 	}
13439eff1a38SJesse Larrew 
13449eff1a38SJesse Larrew 	return nr_cpus;
13459eff1a38SJesse Larrew }
13469eff1a38SJesse Larrew 
1347c0e5e46fSAnton Blanchard /*
1348c0e5e46fSAnton Blanchard  * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1349c0e5e46fSAnton Blanchard  * the complete property we have to add the length in the first cell.
1350c0e5e46fSAnton Blanchard  */
1351c0e5e46fSAnton Blanchard #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
13529eff1a38SJesse Larrew 
13539eff1a38SJesse Larrew /*
13549eff1a38SJesse Larrew  * Convert the associativity domain numbers returned from the hypervisor
13559eff1a38SJesse Larrew  * to the sequence they would appear in the ibm,associativity property.
13569eff1a38SJesse Larrew  */
13579eff1a38SJesse Larrew static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
13589eff1a38SJesse Larrew {
1359cd9d6cc7SJesse Larrew 	int i, nr_assoc_doms = 0;
13609eff1a38SJesse Larrew 	const u16 *field = (const u16*) packed;
13619eff1a38SJesse Larrew 
13629eff1a38SJesse Larrew #define VPHN_FIELD_UNUSED	(0xffff)
13639eff1a38SJesse Larrew #define VPHN_FIELD_MSB		(0x8000)
13649eff1a38SJesse Larrew #define VPHN_FIELD_MASK		(~VPHN_FIELD_MSB)
13659eff1a38SJesse Larrew 
1366c0e5e46fSAnton Blanchard 	for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
13679eff1a38SJesse Larrew 		if (*field == VPHN_FIELD_UNUSED) {
13689eff1a38SJesse Larrew 			/* All significant fields processed, and remaining
13699eff1a38SJesse Larrew 			 * fields contain the reserved value of all 1's.
13709eff1a38SJesse Larrew 			 * Just store them.
13719eff1a38SJesse Larrew 			 */
13729eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13739eff1a38SJesse Larrew 			field += 2;
13747639adaaSJesse Larrew 		} else if (*field & VPHN_FIELD_MSB) {
13759eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field */
13769eff1a38SJesse Larrew 			unpacked[i] = *field & VPHN_FIELD_MASK;
13779eff1a38SJesse Larrew 			field++;
13789eff1a38SJesse Larrew 			nr_assoc_doms++;
13797639adaaSJesse Larrew 		} else {
13809eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field
13819eff1a38SJesse Larrew 			 * concatenated with the next 16 bit field
13829eff1a38SJesse Larrew 			 */
13839eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13849eff1a38SJesse Larrew 			field += 2;
13859eff1a38SJesse Larrew 			nr_assoc_doms++;
13869eff1a38SJesse Larrew 		}
13879eff1a38SJesse Larrew 	}
13889eff1a38SJesse Larrew 
1389c0e5e46fSAnton Blanchard 	/* The first cell contains the length of the property */
1390c0e5e46fSAnton Blanchard 	unpacked[0] = nr_assoc_doms;
1391c0e5e46fSAnton Blanchard 
13929eff1a38SJesse Larrew 	return nr_assoc_doms;
13939eff1a38SJesse Larrew }
13949eff1a38SJesse Larrew 
13959eff1a38SJesse Larrew /*
13969eff1a38SJesse Larrew  * Retrieve the new associativity information for a virtual processor's
13979eff1a38SJesse Larrew  * home node.
13989eff1a38SJesse Larrew  */
13999eff1a38SJesse Larrew static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
14009eff1a38SJesse Larrew {
1401cd9d6cc7SJesse Larrew 	long rc;
14029eff1a38SJesse Larrew 	long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
14039eff1a38SJesse Larrew 	u64 flags = 1;
14049eff1a38SJesse Larrew 	int hwcpu = get_hard_smp_processor_id(cpu);
14059eff1a38SJesse Larrew 
14069eff1a38SJesse Larrew 	rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
14079eff1a38SJesse Larrew 	vphn_unpack_associativity(retbuf, associativity);
14089eff1a38SJesse Larrew 
14099eff1a38SJesse Larrew 	return rc;
14109eff1a38SJesse Larrew }
14119eff1a38SJesse Larrew 
14129eff1a38SJesse Larrew static long vphn_get_associativity(unsigned long cpu,
14139eff1a38SJesse Larrew 					unsigned int *associativity)
14149eff1a38SJesse Larrew {
1415cd9d6cc7SJesse Larrew 	long rc;
14169eff1a38SJesse Larrew 
14179eff1a38SJesse Larrew 	rc = hcall_vphn(cpu, associativity);
14189eff1a38SJesse Larrew 
14199eff1a38SJesse Larrew 	switch (rc) {
14209eff1a38SJesse Larrew 	case H_FUNCTION:
14219eff1a38SJesse Larrew 		printk(KERN_INFO
14229eff1a38SJesse Larrew 			"VPHN is not supported. Disabling polling...\n");
14239eff1a38SJesse Larrew 		stop_topology_update();
14249eff1a38SJesse Larrew 		break;
14259eff1a38SJesse Larrew 	case H_HARDWARE:
14269eff1a38SJesse Larrew 		printk(KERN_ERR
14279eff1a38SJesse Larrew 			"hcall_vphn() experienced a hardware fault "
14289eff1a38SJesse Larrew 			"preventing VPHN. Disabling polling...\n");
14299eff1a38SJesse Larrew 		stop_topology_update();
14309eff1a38SJesse Larrew 	}
14319eff1a38SJesse Larrew 
14329eff1a38SJesse Larrew 	return rc;
14339eff1a38SJesse Larrew }
14349eff1a38SJesse Larrew 
14359eff1a38SJesse Larrew /*
14369eff1a38SJesse Larrew  * Update the node maps and sysfs entries for each cpu whose home node
14379eff1a38SJesse Larrew  * has changed.
14389eff1a38SJesse Larrew  */
14399eff1a38SJesse Larrew int arch_update_cpu_topology(void)
14409eff1a38SJesse Larrew {
1441cd9d6cc7SJesse Larrew 	int cpu, nid, old_nid;
14429eff1a38SJesse Larrew 	unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
14438a25a2fdSKay Sievers 	struct device *dev;
14449eff1a38SJesse Larrew 
1445104699c0SKOSAKI Motohiro 	for_each_cpu(cpu,&cpu_associativity_changes_mask) {
14469eff1a38SJesse Larrew 		vphn_get_associativity(cpu, associativity);
14479eff1a38SJesse Larrew 		nid = associativity_to_nid(associativity);
14489eff1a38SJesse Larrew 
14499eff1a38SJesse Larrew 		if (nid < 0 || !node_online(nid))
14509eff1a38SJesse Larrew 			nid = first_online_node;
14519eff1a38SJesse Larrew 
14529eff1a38SJesse Larrew 		old_nid = numa_cpu_lookup_table[cpu];
14539eff1a38SJesse Larrew 
14549eff1a38SJesse Larrew 		/* Disable hotplug while we update the cpu
14559eff1a38SJesse Larrew 		 * masks and sysfs.
14569eff1a38SJesse Larrew 		 */
14579eff1a38SJesse Larrew 		get_online_cpus();
14589eff1a38SJesse Larrew 		unregister_cpu_under_node(cpu, old_nid);
14599eff1a38SJesse Larrew 		unmap_cpu_from_node(cpu);
14609eff1a38SJesse Larrew 		map_cpu_to_node(cpu, nid);
14619eff1a38SJesse Larrew 		register_cpu_under_node(cpu, nid);
14629eff1a38SJesse Larrew 		put_online_cpus();
14639eff1a38SJesse Larrew 
14648a25a2fdSKay Sievers 		dev = get_cpu_device(cpu);
14658a25a2fdSKay Sievers 		if (dev)
14668a25a2fdSKay Sievers 			kobject_uevent(&dev->kobj, KOBJ_CHANGE);
14679eff1a38SJesse Larrew 	}
14689eff1a38SJesse Larrew 
14699eff1a38SJesse Larrew 	return 1;
14709eff1a38SJesse Larrew }
14719eff1a38SJesse Larrew 
14729eff1a38SJesse Larrew static void topology_work_fn(struct work_struct *work)
14739eff1a38SJesse Larrew {
14749eff1a38SJesse Larrew 	rebuild_sched_domains();
14759eff1a38SJesse Larrew }
14769eff1a38SJesse Larrew static DECLARE_WORK(topology_work, topology_work_fn);
14779eff1a38SJesse Larrew 
14789eff1a38SJesse Larrew void topology_schedule_update(void)
14799eff1a38SJesse Larrew {
14809eff1a38SJesse Larrew 	schedule_work(&topology_work);
14819eff1a38SJesse Larrew }
14829eff1a38SJesse Larrew 
14839eff1a38SJesse Larrew static void topology_timer_fn(unsigned long ignored)
14849eff1a38SJesse Larrew {
14859eff1a38SJesse Larrew 	if (!vphn_enabled)
14869eff1a38SJesse Larrew 		return;
14879eff1a38SJesse Larrew 	if (update_cpu_associativity_changes_mask() > 0)
14889eff1a38SJesse Larrew 		topology_schedule_update();
14899eff1a38SJesse Larrew 	set_topology_timer();
14909eff1a38SJesse Larrew }
14919eff1a38SJesse Larrew static struct timer_list topology_timer =
14929eff1a38SJesse Larrew 	TIMER_INITIALIZER(topology_timer_fn, 0, 0);
14939eff1a38SJesse Larrew 
14949eff1a38SJesse Larrew static void set_topology_timer(void)
14959eff1a38SJesse Larrew {
14969eff1a38SJesse Larrew 	topology_timer.data = 0;
14979eff1a38SJesse Larrew 	topology_timer.expires = jiffies + 60 * HZ;
14989eff1a38SJesse Larrew 	add_timer(&topology_timer);
14999eff1a38SJesse Larrew }
15009eff1a38SJesse Larrew 
15019eff1a38SJesse Larrew /*
15029eff1a38SJesse Larrew  * Start polling for VPHN associativity changes.
15039eff1a38SJesse Larrew  */
15049eff1a38SJesse Larrew int start_topology_update(void)
15059eff1a38SJesse Larrew {
15069eff1a38SJesse Larrew 	int rc = 0;
15079eff1a38SJesse Larrew 
150836e8695cSBenjamin Herrenschmidt 	/* Disabled until races with load balancing are fixed */
150936e8695cSBenjamin Herrenschmidt 	if (0 && firmware_has_feature(FW_FEATURE_VPHN) &&
1510fe5cfd63SAnton Blanchard 	    get_lppaca()->shared_proc) {
15119eff1a38SJesse Larrew 		vphn_enabled = 1;
15129eff1a38SJesse Larrew 		setup_cpu_associativity_change_counters();
15139eff1a38SJesse Larrew 		init_timer_deferrable(&topology_timer);
15149eff1a38SJesse Larrew 		set_topology_timer();
15159eff1a38SJesse Larrew 		rc = 1;
15169eff1a38SJesse Larrew 	}
15179eff1a38SJesse Larrew 
15189eff1a38SJesse Larrew 	return rc;
15199eff1a38SJesse Larrew }
15209eff1a38SJesse Larrew __initcall(start_topology_update);
15219eff1a38SJesse Larrew 
15229eff1a38SJesse Larrew /*
15239eff1a38SJesse Larrew  * Disable polling for VPHN associativity changes.
15249eff1a38SJesse Larrew  */
15259eff1a38SJesse Larrew int stop_topology_update(void)
15269eff1a38SJesse Larrew {
15279eff1a38SJesse Larrew 	vphn_enabled = 0;
15289eff1a38SJesse Larrew 	return del_timer_sync(&topology_timer);
15299eff1a38SJesse Larrew }
153039bf990eSJesse Larrew #endif /* CONFIG_PPC_SPLPAR */
1531