xref: /linux/arch/powerpc/mm/numa.c (revision 5de1669910a59025e6cf24baef242a6c264d5752)
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>
16ab1f9dacSPaul Mackerras #include <linux/module.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>
27cf00a8d1SPaul Mackerras #include <asm/system.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>
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  *
6125863de0SAnton Blanchard  * Note: node_to_cpumask() 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 /*
1308f64e1f2SJon Tollefson  * get_active_region_work_fn - A helper function for get_node_active_region
1318f64e1f2SJon Tollefson  *	Returns datax set to the start_pfn and end_pfn if they contain
1328f64e1f2SJon Tollefson  *	the initial value of datax->start_pfn between them
1338f64e1f2SJon Tollefson  * @start_pfn: start page(inclusive) of region to check
1348f64e1f2SJon Tollefson  * @end_pfn: end page(exclusive) of region to check
1358f64e1f2SJon Tollefson  * @datax: comes in with ->start_pfn set to value to search for and
1368f64e1f2SJon Tollefson  *	goes out with active range if it contains it
1378f64e1f2SJon Tollefson  * Returns 1 if search value is in range else 0
1388f64e1f2SJon Tollefson  */
1398f64e1f2SJon Tollefson static int __init get_active_region_work_fn(unsigned long start_pfn,
1408f64e1f2SJon Tollefson 					unsigned long end_pfn, void *datax)
1418f64e1f2SJon Tollefson {
1428f64e1f2SJon Tollefson 	struct node_active_region *data;
1438f64e1f2SJon Tollefson 	data = (struct node_active_region *)datax;
1448f64e1f2SJon Tollefson 
1458f64e1f2SJon Tollefson 	if (start_pfn <= data->start_pfn && end_pfn > data->start_pfn) {
1468f64e1f2SJon Tollefson 		data->start_pfn = start_pfn;
1478f64e1f2SJon Tollefson 		data->end_pfn = end_pfn;
1488f64e1f2SJon Tollefson 		return 1;
1498f64e1f2SJon Tollefson 	}
1508f64e1f2SJon Tollefson 	return 0;
1518f64e1f2SJon Tollefson 
1528f64e1f2SJon Tollefson }
1538f64e1f2SJon Tollefson 
1548f64e1f2SJon Tollefson /*
1558f64e1f2SJon Tollefson  * get_node_active_region - Return active region containing start_pfn
156e8170372SJon Tollefson  * Active range returned is empty if none found.
1578f64e1f2SJon Tollefson  * @start_pfn: The page to return the region for.
1588f64e1f2SJon Tollefson  * @node_ar: Returned set to the active region containing start_pfn
1598f64e1f2SJon Tollefson  */
1608f64e1f2SJon Tollefson static void __init get_node_active_region(unsigned long start_pfn,
1618f64e1f2SJon Tollefson 		       struct node_active_region *node_ar)
1628f64e1f2SJon Tollefson {
1638f64e1f2SJon Tollefson 	int nid = early_pfn_to_nid(start_pfn);
1648f64e1f2SJon Tollefson 
1658f64e1f2SJon Tollefson 	node_ar->nid = nid;
1668f64e1f2SJon Tollefson 	node_ar->start_pfn = start_pfn;
167e8170372SJon Tollefson 	node_ar->end_pfn = start_pfn;
1688f64e1f2SJon Tollefson 	work_with_active_regions(nid, get_active_region_work_fn, node_ar);
1698f64e1f2SJon Tollefson }
1708f64e1f2SJon Tollefson 
17139bf990eSJesse Larrew static void map_cpu_to_node(int cpu, int node)
172ab1f9dacSPaul Mackerras {
173ab1f9dacSPaul Mackerras 	numa_cpu_lookup_table[cpu] = node;
17445fb6ceaSAnton Blanchard 
175bf4b85b0SNathan Lynch 	dbg("adding cpu %d to node %d\n", cpu, node);
176bf4b85b0SNathan Lynch 
17725863de0SAnton Blanchard 	if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
17825863de0SAnton Blanchard 		cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
179ab1f9dacSPaul Mackerras }
180ab1f9dacSPaul Mackerras 
18139bf990eSJesse Larrew #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
182ab1f9dacSPaul Mackerras static void unmap_cpu_from_node(unsigned long cpu)
183ab1f9dacSPaul Mackerras {
184ab1f9dacSPaul Mackerras 	int node = numa_cpu_lookup_table[cpu];
185ab1f9dacSPaul Mackerras 
186ab1f9dacSPaul Mackerras 	dbg("removing cpu %lu from node %d\n", cpu, node);
187ab1f9dacSPaul Mackerras 
18825863de0SAnton Blanchard 	if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
18925863de0SAnton Blanchard 		cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
190ab1f9dacSPaul Mackerras 	} else {
191ab1f9dacSPaul Mackerras 		printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
192ab1f9dacSPaul Mackerras 		       cpu, node);
193ab1f9dacSPaul Mackerras 	}
194ab1f9dacSPaul Mackerras }
19539bf990eSJesse Larrew #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
196ab1f9dacSPaul Mackerras 
197ab1f9dacSPaul Mackerras /* must hold reference to node during call */
198a7f67bdfSJeremy Kerr static const int *of_get_associativity(struct device_node *dev)
199ab1f9dacSPaul Mackerras {
200e2eb6392SStephen Rothwell 	return of_get_property(dev, "ibm,associativity", NULL);
201ab1f9dacSPaul Mackerras }
202ab1f9dacSPaul Mackerras 
203cf00085dSChandru /*
204cf00085dSChandru  * Returns the property linux,drconf-usable-memory if
205cf00085dSChandru  * it exists (the property exists only in kexec/kdump kernels,
206cf00085dSChandru  * added by kexec-tools)
207cf00085dSChandru  */
208cf00085dSChandru static const u32 *of_get_usable_memory(struct device_node *memory)
209cf00085dSChandru {
210cf00085dSChandru 	const u32 *prop;
211cf00085dSChandru 	u32 len;
212cf00085dSChandru 	prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
213cf00085dSChandru 	if (!prop || len < sizeof(unsigned int))
214cf00085dSChandru 		return 0;
215cf00085dSChandru 	return prop;
216cf00085dSChandru }
217cf00085dSChandru 
21841eab6f8SAnton Blanchard int __node_distance(int a, int b)
21941eab6f8SAnton Blanchard {
22041eab6f8SAnton Blanchard 	int i;
22141eab6f8SAnton Blanchard 	int distance = LOCAL_DISTANCE;
22241eab6f8SAnton Blanchard 
22341eab6f8SAnton Blanchard 	if (!form1_affinity)
22441eab6f8SAnton Blanchard 		return distance;
22541eab6f8SAnton Blanchard 
22641eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
22741eab6f8SAnton Blanchard 		if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
22841eab6f8SAnton Blanchard 			break;
22941eab6f8SAnton Blanchard 
23041eab6f8SAnton Blanchard 		/* Double the distance for each NUMA level */
23141eab6f8SAnton Blanchard 		distance *= 2;
23241eab6f8SAnton Blanchard 	}
23341eab6f8SAnton Blanchard 
23441eab6f8SAnton Blanchard 	return distance;
23541eab6f8SAnton Blanchard }
23641eab6f8SAnton Blanchard 
23741eab6f8SAnton Blanchard static void initialize_distance_lookup_table(int nid,
23841eab6f8SAnton Blanchard 		const unsigned int *associativity)
23941eab6f8SAnton Blanchard {
24041eab6f8SAnton Blanchard 	int i;
24141eab6f8SAnton Blanchard 
24241eab6f8SAnton Blanchard 	if (!form1_affinity)
24341eab6f8SAnton Blanchard 		return;
24441eab6f8SAnton Blanchard 
24541eab6f8SAnton Blanchard 	for (i = 0; i < distance_ref_points_depth; i++) {
24641eab6f8SAnton Blanchard 		distance_lookup_table[nid][i] =
24741eab6f8SAnton Blanchard 			associativity[distance_ref_points[i]];
24841eab6f8SAnton Blanchard 	}
24941eab6f8SAnton Blanchard }
25041eab6f8SAnton Blanchard 
251482ec7c4SNathan Lynch /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
252482ec7c4SNathan Lynch  * info is found.
253482ec7c4SNathan Lynch  */
2549eff1a38SJesse Larrew static int associativity_to_nid(const unsigned int *associativity)
255ab1f9dacSPaul Mackerras {
256482ec7c4SNathan Lynch 	int nid = -1;
257ab1f9dacSPaul Mackerras 
258ab1f9dacSPaul Mackerras 	if (min_common_depth == -1)
259482ec7c4SNathan Lynch 		goto out;
260ab1f9dacSPaul Mackerras 
2619eff1a38SJesse Larrew 	if (associativity[0] >= min_common_depth)
2629eff1a38SJesse Larrew 		nid = associativity[min_common_depth];
263bc16a759SNathan Lynch 
264bc16a759SNathan Lynch 	/* POWER4 LPAR uses 0xffff as invalid node */
265482ec7c4SNathan Lynch 	if (nid == 0xffff || nid >= MAX_NUMNODES)
266482ec7c4SNathan Lynch 		nid = -1;
26741eab6f8SAnton Blanchard 
2689eff1a38SJesse Larrew 	if (nid > 0 && associativity[0] >= distance_ref_points_depth)
2699eff1a38SJesse Larrew 		initialize_distance_lookup_table(nid, associativity);
27041eab6f8SAnton Blanchard 
271482ec7c4SNathan Lynch out:
272cf950b7aSNathan Lynch 	return nid;
273ab1f9dacSPaul Mackerras }
274ab1f9dacSPaul Mackerras 
2759eff1a38SJesse Larrew /* Returns the nid associated with the given device tree node,
2769eff1a38SJesse Larrew  * or -1 if not found.
2779eff1a38SJesse Larrew  */
2789eff1a38SJesse Larrew static int of_node_to_nid_single(struct device_node *device)
2799eff1a38SJesse Larrew {
2809eff1a38SJesse Larrew 	int nid = -1;
2819eff1a38SJesse Larrew 	const unsigned int *tmp;
2829eff1a38SJesse Larrew 
2839eff1a38SJesse Larrew 	tmp = of_get_associativity(device);
2849eff1a38SJesse Larrew 	if (tmp)
2859eff1a38SJesse Larrew 		nid = associativity_to_nid(tmp);
2869eff1a38SJesse Larrew 	return nid;
2879eff1a38SJesse Larrew }
2889eff1a38SJesse Larrew 
289953039c8SJeremy Kerr /* Walk the device tree upwards, looking for an associativity id */
290953039c8SJeremy Kerr int of_node_to_nid(struct device_node *device)
291953039c8SJeremy Kerr {
292953039c8SJeremy Kerr 	struct device_node *tmp;
293953039c8SJeremy Kerr 	int nid = -1;
294953039c8SJeremy Kerr 
295953039c8SJeremy Kerr 	of_node_get(device);
296953039c8SJeremy Kerr 	while (device) {
297953039c8SJeremy Kerr 		nid = of_node_to_nid_single(device);
298953039c8SJeremy Kerr 		if (nid != -1)
299953039c8SJeremy Kerr 			break;
300953039c8SJeremy Kerr 
301953039c8SJeremy Kerr 	        tmp = device;
302953039c8SJeremy Kerr 		device = of_get_parent(tmp);
303953039c8SJeremy Kerr 		of_node_put(tmp);
304953039c8SJeremy Kerr 	}
305953039c8SJeremy Kerr 	of_node_put(device);
306953039c8SJeremy Kerr 
307953039c8SJeremy Kerr 	return nid;
308953039c8SJeremy Kerr }
309953039c8SJeremy Kerr EXPORT_SYMBOL_GPL(of_node_to_nid);
310953039c8SJeremy Kerr 
311ab1f9dacSPaul Mackerras static int __init find_min_common_depth(void)
312ab1f9dacSPaul Mackerras {
31341eab6f8SAnton Blanchard 	int depth;
314ab1f9dacSPaul Mackerras 	struct device_node *rtas_root;
315bc8449ccSAnton Blanchard 	struct device_node *chosen;
316bc8449ccSAnton Blanchard 	const char *vec5;
317ab1f9dacSPaul Mackerras 
318ab1f9dacSPaul Mackerras 	rtas_root = of_find_node_by_path("/rtas");
319ab1f9dacSPaul Mackerras 
320ab1f9dacSPaul Mackerras 	if (!rtas_root)
321ab1f9dacSPaul Mackerras 		return -1;
322ab1f9dacSPaul Mackerras 
323ab1f9dacSPaul Mackerras 	/*
32441eab6f8SAnton Blanchard 	 * This property is a set of 32-bit integers, each representing
32541eab6f8SAnton Blanchard 	 * an index into the ibm,associativity nodes.
32641eab6f8SAnton Blanchard 	 *
32741eab6f8SAnton Blanchard 	 * With form 0 affinity the first integer is for an SMP configuration
32841eab6f8SAnton Blanchard 	 * (should be all 0's) and the second is for a normal NUMA
32941eab6f8SAnton Blanchard 	 * configuration. We have only one level of NUMA.
33041eab6f8SAnton Blanchard 	 *
33141eab6f8SAnton Blanchard 	 * With form 1 affinity the first integer is the most significant
33241eab6f8SAnton Blanchard 	 * NUMA boundary and the following are progressively less significant
33341eab6f8SAnton Blanchard 	 * boundaries. There can be more than one level of NUMA.
334ab1f9dacSPaul Mackerras 	 */
33541eab6f8SAnton Blanchard 	distance_ref_points = of_get_property(rtas_root,
33641eab6f8SAnton Blanchard 					"ibm,associativity-reference-points",
33741eab6f8SAnton Blanchard 					&distance_ref_points_depth);
338ab1f9dacSPaul Mackerras 
33941eab6f8SAnton Blanchard 	if (!distance_ref_points) {
34041eab6f8SAnton Blanchard 		dbg("NUMA: ibm,associativity-reference-points not found.\n");
34141eab6f8SAnton Blanchard 		goto err;
34241eab6f8SAnton Blanchard 	}
34341eab6f8SAnton Blanchard 
34441eab6f8SAnton Blanchard 	distance_ref_points_depth /= sizeof(int);
34541eab6f8SAnton Blanchard 
346bc8449ccSAnton Blanchard #define VEC5_AFFINITY_BYTE	5
347bc8449ccSAnton Blanchard #define VEC5_AFFINITY		0x80
348bc8449ccSAnton Blanchard 	chosen = of_find_node_by_path("/chosen");
349bc8449ccSAnton Blanchard 	if (chosen) {
350bc8449ccSAnton Blanchard 		vec5 = of_get_property(chosen, "ibm,architecture-vec-5", NULL);
351bc8449ccSAnton Blanchard 		if (vec5 && (vec5[VEC5_AFFINITY_BYTE] & VEC5_AFFINITY)) {
352bc8449ccSAnton Blanchard 			dbg("Using form 1 affinity\n");
35341eab6f8SAnton Blanchard 			form1_affinity = 1;
3544b83c330SAnton Blanchard 		}
355bc8449ccSAnton Blanchard 	}
3564b83c330SAnton Blanchard 
35741eab6f8SAnton Blanchard 	if (form1_affinity) {
35841eab6f8SAnton Blanchard 		depth = distance_ref_points[0];
359ab1f9dacSPaul Mackerras 	} else {
36041eab6f8SAnton Blanchard 		if (distance_ref_points_depth < 2) {
36141eab6f8SAnton Blanchard 			printk(KERN_WARNING "NUMA: "
36241eab6f8SAnton Blanchard 				"short ibm,associativity-reference-points\n");
36341eab6f8SAnton Blanchard 			goto err;
364ab1f9dacSPaul Mackerras 		}
365ab1f9dacSPaul Mackerras 
36641eab6f8SAnton Blanchard 		depth = distance_ref_points[1];
36741eab6f8SAnton Blanchard 	}
36841eab6f8SAnton Blanchard 
36941eab6f8SAnton Blanchard 	/*
37041eab6f8SAnton Blanchard 	 * Warn and cap if the hardware supports more than
37141eab6f8SAnton Blanchard 	 * MAX_DISTANCE_REF_POINTS domains.
37241eab6f8SAnton Blanchard 	 */
37341eab6f8SAnton Blanchard 	if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
37441eab6f8SAnton Blanchard 		printk(KERN_WARNING "NUMA: distance array capped at "
37541eab6f8SAnton Blanchard 			"%d entries\n", MAX_DISTANCE_REF_POINTS);
37641eab6f8SAnton Blanchard 		distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
37741eab6f8SAnton Blanchard 	}
37841eab6f8SAnton Blanchard 
37941eab6f8SAnton Blanchard 	of_node_put(rtas_root);
380ab1f9dacSPaul Mackerras 	return depth;
38141eab6f8SAnton Blanchard 
38241eab6f8SAnton Blanchard err:
38341eab6f8SAnton Blanchard 	of_node_put(rtas_root);
38441eab6f8SAnton Blanchard 	return -1;
385ab1f9dacSPaul Mackerras }
386ab1f9dacSPaul Mackerras 
38784c9fdd1SMike Kravetz static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
388ab1f9dacSPaul Mackerras {
389ab1f9dacSPaul Mackerras 	struct device_node *memory = NULL;
390ab1f9dacSPaul Mackerras 
391ab1f9dacSPaul Mackerras 	memory = of_find_node_by_type(memory, "memory");
39254c23310SPaul Mackerras 	if (!memory)
39384c9fdd1SMike Kravetz 		panic("numa.c: No memory nodes found!");
39454c23310SPaul Mackerras 
395a8bda5ddSStephen Rothwell 	*n_addr_cells = of_n_addr_cells(memory);
3969213feeaSStephen Rothwell 	*n_size_cells = of_n_size_cells(memory);
39784c9fdd1SMike Kravetz 	of_node_put(memory);
398ab1f9dacSPaul Mackerras }
399ab1f9dacSPaul Mackerras 
400a7f67bdfSJeremy Kerr static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
401ab1f9dacSPaul Mackerras {
402ab1f9dacSPaul Mackerras 	unsigned long result = 0;
403ab1f9dacSPaul Mackerras 
404ab1f9dacSPaul Mackerras 	while (n--) {
405ab1f9dacSPaul Mackerras 		result = (result << 32) | **buf;
406ab1f9dacSPaul Mackerras 		(*buf)++;
407ab1f9dacSPaul Mackerras 	}
408ab1f9dacSPaul Mackerras 	return result;
409ab1f9dacSPaul Mackerras }
410ab1f9dacSPaul Mackerras 
4118342681dSNathan Fontenot struct of_drconf_cell {
4128342681dSNathan Fontenot 	u64	base_addr;
4138342681dSNathan Fontenot 	u32	drc_index;
4148342681dSNathan Fontenot 	u32	reserved;
4158342681dSNathan Fontenot 	u32	aa_index;
4168342681dSNathan Fontenot 	u32	flags;
4178342681dSNathan Fontenot };
4188342681dSNathan Fontenot 
4198342681dSNathan Fontenot #define DRCONF_MEM_ASSIGNED	0x00000008
4208342681dSNathan Fontenot #define DRCONF_MEM_AI_INVALID	0x00000040
4218342681dSNathan Fontenot #define DRCONF_MEM_RESERVED	0x00000080
4228342681dSNathan Fontenot 
4238342681dSNathan Fontenot /*
42495f72d1eSYinghai Lu  * Read the next memblock list entry from the ibm,dynamic-memory property
4258342681dSNathan Fontenot  * and return the information in the provided of_drconf_cell structure.
4268342681dSNathan Fontenot  */
4278342681dSNathan Fontenot static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
4288342681dSNathan Fontenot {
4298342681dSNathan Fontenot 	const u32 *cp;
4308342681dSNathan Fontenot 
4318342681dSNathan Fontenot 	drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
4328342681dSNathan Fontenot 
4338342681dSNathan Fontenot 	cp = *cellp;
4348342681dSNathan Fontenot 	drmem->drc_index = cp[0];
4358342681dSNathan Fontenot 	drmem->reserved = cp[1];
4368342681dSNathan Fontenot 	drmem->aa_index = cp[2];
4378342681dSNathan Fontenot 	drmem->flags = cp[3];
4388342681dSNathan Fontenot 
4398342681dSNathan Fontenot 	*cellp = cp + 4;
4408342681dSNathan Fontenot }
4418342681dSNathan Fontenot 
4428342681dSNathan Fontenot /*
4438342681dSNathan Fontenot  * Retreive and validate the ibm,dynamic-memory property of the device tree.
4448342681dSNathan Fontenot  *
44595f72d1eSYinghai Lu  * The layout of the ibm,dynamic-memory property is a number N of memblock
44695f72d1eSYinghai Lu  * list entries followed by N memblock list entries.  Each memblock list entry
4478342681dSNathan Fontenot  * contains information as layed out in the of_drconf_cell struct above.
4488342681dSNathan Fontenot  */
4498342681dSNathan Fontenot static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
4508342681dSNathan Fontenot {
4518342681dSNathan Fontenot 	const u32 *prop;
4528342681dSNathan Fontenot 	u32 len, entries;
4538342681dSNathan Fontenot 
4548342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,dynamic-memory", &len);
4558342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4568342681dSNathan Fontenot 		return 0;
4578342681dSNathan Fontenot 
4588342681dSNathan Fontenot 	entries = *prop++;
4598342681dSNathan Fontenot 
4608342681dSNathan Fontenot 	/* Now that we know the number of entries, revalidate the size
4618342681dSNathan Fontenot 	 * of the property read in to ensure we have everything
4628342681dSNathan Fontenot 	 */
4638342681dSNathan Fontenot 	if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
4648342681dSNathan Fontenot 		return 0;
4658342681dSNathan Fontenot 
4668342681dSNathan Fontenot 	*dm = prop;
4678342681dSNathan Fontenot 	return entries;
4688342681dSNathan Fontenot }
4698342681dSNathan Fontenot 
4708342681dSNathan Fontenot /*
4713fdfd990SBenjamin Herrenschmidt  * Retreive and validate the ibm,lmb-size property for drconf memory
4728342681dSNathan Fontenot  * from the device tree.
4738342681dSNathan Fontenot  */
4743fdfd990SBenjamin Herrenschmidt static u64 of_get_lmb_size(struct device_node *memory)
4758342681dSNathan Fontenot {
4768342681dSNathan Fontenot 	const u32 *prop;
4778342681dSNathan Fontenot 	u32 len;
4788342681dSNathan Fontenot 
4793fdfd990SBenjamin Herrenschmidt 	prop = of_get_property(memory, "ibm,lmb-size", &len);
4808342681dSNathan Fontenot 	if (!prop || len < sizeof(unsigned int))
4818342681dSNathan Fontenot 		return 0;
4828342681dSNathan Fontenot 
4838342681dSNathan Fontenot 	return read_n_cells(n_mem_size_cells, &prop);
4848342681dSNathan Fontenot }
4858342681dSNathan Fontenot 
4868342681dSNathan Fontenot struct assoc_arrays {
4878342681dSNathan Fontenot 	u32	n_arrays;
4888342681dSNathan Fontenot 	u32	array_sz;
4898342681dSNathan Fontenot 	const u32 *arrays;
4908342681dSNathan Fontenot };
4918342681dSNathan Fontenot 
4928342681dSNathan Fontenot /*
4938342681dSNathan Fontenot  * Retreive and validate the list of associativity arrays for drconf
4948342681dSNathan Fontenot  * memory from the ibm,associativity-lookup-arrays property of the
4958342681dSNathan Fontenot  * device tree..
4968342681dSNathan Fontenot  *
4978342681dSNathan Fontenot  * The layout of the ibm,associativity-lookup-arrays property is a number N
4988342681dSNathan Fontenot  * indicating the number of associativity arrays, followed by a number M
4998342681dSNathan Fontenot  * indicating the size of each associativity array, followed by a list
5008342681dSNathan Fontenot  * of N associativity arrays.
5018342681dSNathan Fontenot  */
5028342681dSNathan Fontenot static int of_get_assoc_arrays(struct device_node *memory,
5038342681dSNathan Fontenot 			       struct assoc_arrays *aa)
5048342681dSNathan Fontenot {
5058342681dSNathan Fontenot 	const u32 *prop;
5068342681dSNathan Fontenot 	u32 len;
5078342681dSNathan Fontenot 
5088342681dSNathan Fontenot 	prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
5098342681dSNathan Fontenot 	if (!prop || len < 2 * sizeof(unsigned int))
5108342681dSNathan Fontenot 		return -1;
5118342681dSNathan Fontenot 
5128342681dSNathan Fontenot 	aa->n_arrays = *prop++;
5138342681dSNathan Fontenot 	aa->array_sz = *prop++;
5148342681dSNathan Fontenot 
5158342681dSNathan Fontenot 	/* Now that we know the number of arrrays and size of each array,
5168342681dSNathan Fontenot 	 * revalidate the size of the property read in.
5178342681dSNathan Fontenot 	 */
5188342681dSNathan Fontenot 	if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
5198342681dSNathan Fontenot 		return -1;
5208342681dSNathan Fontenot 
5218342681dSNathan Fontenot 	aa->arrays = prop;
5228342681dSNathan Fontenot 	return 0;
5238342681dSNathan Fontenot }
5248342681dSNathan Fontenot 
5258342681dSNathan Fontenot /*
5268342681dSNathan Fontenot  * This is like of_node_to_nid_single() for memory represented in the
5278342681dSNathan Fontenot  * ibm,dynamic-reconfiguration-memory node.
5288342681dSNathan Fontenot  */
5298342681dSNathan Fontenot static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
5308342681dSNathan Fontenot 				   struct assoc_arrays *aa)
5318342681dSNathan Fontenot {
5328342681dSNathan Fontenot 	int default_nid = 0;
5338342681dSNathan Fontenot 	int nid = default_nid;
5348342681dSNathan Fontenot 	int index;
5358342681dSNathan Fontenot 
5368342681dSNathan Fontenot 	if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
5378342681dSNathan Fontenot 	    !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
5388342681dSNathan Fontenot 	    drmem->aa_index < aa->n_arrays) {
5398342681dSNathan Fontenot 		index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
5408342681dSNathan Fontenot 		nid = aa->arrays[index];
5418342681dSNathan Fontenot 
5428342681dSNathan Fontenot 		if (nid == 0xffff || nid >= MAX_NUMNODES)
5438342681dSNathan Fontenot 			nid = default_nid;
5448342681dSNathan Fontenot 	}
5458342681dSNathan Fontenot 
5468342681dSNathan Fontenot 	return nid;
5478342681dSNathan Fontenot }
5488342681dSNathan Fontenot 
549ab1f9dacSPaul Mackerras /*
550ab1f9dacSPaul Mackerras  * Figure out to which domain a cpu belongs and stick it there.
551ab1f9dacSPaul Mackerras  * Return the id of the domain used.
552ab1f9dacSPaul Mackerras  */
5532e5ce39dSNathan Lynch static int __cpuinit numa_setup_cpu(unsigned long lcpu)
554ab1f9dacSPaul Mackerras {
555cf950b7aSNathan Lynch 	int nid = 0;
5568b16cd23SMilton Miller 	struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
557ab1f9dacSPaul Mackerras 
558ab1f9dacSPaul Mackerras 	if (!cpu) {
559ab1f9dacSPaul Mackerras 		WARN_ON(1);
560ab1f9dacSPaul Mackerras 		goto out;
561ab1f9dacSPaul Mackerras 	}
562ab1f9dacSPaul Mackerras 
563953039c8SJeremy Kerr 	nid = of_node_to_nid_single(cpu);
564ab1f9dacSPaul Mackerras 
565482ec7c4SNathan Lynch 	if (nid < 0 || !node_online(nid))
56672c33688SH Hartley Sweeten 		nid = first_online_node;
567ab1f9dacSPaul Mackerras out:
568cf950b7aSNathan Lynch 	map_cpu_to_node(lcpu, nid);
569ab1f9dacSPaul Mackerras 
570ab1f9dacSPaul Mackerras 	of_node_put(cpu);
571ab1f9dacSPaul Mackerras 
572cf950b7aSNathan Lynch 	return nid;
573ab1f9dacSPaul Mackerras }
574ab1f9dacSPaul Mackerras 
57574b85f37SChandra Seetharaman static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
576ab1f9dacSPaul Mackerras 			     unsigned long action,
577ab1f9dacSPaul Mackerras 			     void *hcpu)
578ab1f9dacSPaul Mackerras {
579ab1f9dacSPaul Mackerras 	unsigned long lcpu = (unsigned long)hcpu;
580ab1f9dacSPaul Mackerras 	int ret = NOTIFY_DONE;
581ab1f9dacSPaul Mackerras 
582ab1f9dacSPaul Mackerras 	switch (action) {
583ab1f9dacSPaul Mackerras 	case CPU_UP_PREPARE:
5848bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
585ab1f9dacSPaul Mackerras 		numa_setup_cpu(lcpu);
586ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
587ab1f9dacSPaul Mackerras 		break;
588ab1f9dacSPaul Mackerras #ifdef CONFIG_HOTPLUG_CPU
589ab1f9dacSPaul Mackerras 	case CPU_DEAD:
5908bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
591ab1f9dacSPaul Mackerras 	case CPU_UP_CANCELED:
5928bb78442SRafael J. Wysocki 	case CPU_UP_CANCELED_FROZEN:
593ab1f9dacSPaul Mackerras 		unmap_cpu_from_node(lcpu);
594ab1f9dacSPaul Mackerras 		break;
595ab1f9dacSPaul Mackerras 		ret = NOTIFY_OK;
596ab1f9dacSPaul Mackerras #endif
597ab1f9dacSPaul Mackerras 	}
598ab1f9dacSPaul Mackerras 	return ret;
599ab1f9dacSPaul Mackerras }
600ab1f9dacSPaul Mackerras 
601ab1f9dacSPaul Mackerras /*
602ab1f9dacSPaul Mackerras  * Check and possibly modify a memory region to enforce the memory limit.
603ab1f9dacSPaul Mackerras  *
604ab1f9dacSPaul Mackerras  * Returns the size the region should have to enforce the memory limit.
605ab1f9dacSPaul Mackerras  * This will either be the original value of size, a truncated value,
606ab1f9dacSPaul Mackerras  * or zero. If the returned value of size is 0 the region should be
607ab1f9dacSPaul Mackerras  * discarded as it lies wholy above the memory limit.
608ab1f9dacSPaul Mackerras  */
60945fb6ceaSAnton Blanchard static unsigned long __init numa_enforce_memory_limit(unsigned long start,
61045fb6ceaSAnton Blanchard 						      unsigned long size)
611ab1f9dacSPaul Mackerras {
612ab1f9dacSPaul Mackerras 	/*
61395f72d1eSYinghai Lu 	 * We use memblock_end_of_DRAM() in here instead of memory_limit because
614ab1f9dacSPaul Mackerras 	 * we've already adjusted it for the limit and it takes care of
615fe55249dSMilton Miller 	 * having memory holes below the limit.  Also, in the case of
616fe55249dSMilton Miller 	 * iommu_is_off, memory_limit is not set but is implicitly enforced.
617ab1f9dacSPaul Mackerras 	 */
618ab1f9dacSPaul Mackerras 
61995f72d1eSYinghai Lu 	if (start + size <= memblock_end_of_DRAM())
620ab1f9dacSPaul Mackerras 		return size;
621ab1f9dacSPaul Mackerras 
62295f72d1eSYinghai Lu 	if (start >= memblock_end_of_DRAM())
623ab1f9dacSPaul Mackerras 		return 0;
624ab1f9dacSPaul Mackerras 
62595f72d1eSYinghai Lu 	return memblock_end_of_DRAM() - start;
626ab1f9dacSPaul Mackerras }
627ab1f9dacSPaul Mackerras 
6280204568aSPaul Mackerras /*
629cf00085dSChandru  * Reads the counter for a given entry in
630cf00085dSChandru  * linux,drconf-usable-memory property
631cf00085dSChandru  */
632cf00085dSChandru static inline int __init read_usm_ranges(const u32 **usm)
633cf00085dSChandru {
634cf00085dSChandru 	/*
6353fdfd990SBenjamin Herrenschmidt 	 * For each lmb in ibm,dynamic-memory a corresponding
636cf00085dSChandru 	 * entry in linux,drconf-usable-memory property contains
637cf00085dSChandru 	 * a counter followed by that many (base, size) duple.
638cf00085dSChandru 	 * read the counter from linux,drconf-usable-memory
639cf00085dSChandru 	 */
640cf00085dSChandru 	return read_n_cells(n_mem_size_cells, usm);
641cf00085dSChandru }
642cf00085dSChandru 
643cf00085dSChandru /*
6440204568aSPaul Mackerras  * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
6450204568aSPaul Mackerras  * node.  This assumes n_mem_{addr,size}_cells have been set.
6460204568aSPaul Mackerras  */
6470204568aSPaul Mackerras static void __init parse_drconf_memory(struct device_node *memory)
6480204568aSPaul Mackerras {
649cf00085dSChandru 	const u32 *dm, *usm;
650cf00085dSChandru 	unsigned int n, rc, ranges, is_kexec_kdump = 0;
6513fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size, base, size, sz;
6528342681dSNathan Fontenot 	int nid;
6538342681dSNathan Fontenot 	struct assoc_arrays aa;
6540204568aSPaul Mackerras 
6558342681dSNathan Fontenot 	n = of_get_drconf_memory(memory, &dm);
6568342681dSNathan Fontenot 	if (!n)
6570204568aSPaul Mackerras 		return;
6580204568aSPaul Mackerras 
6593fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
6603fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
6618342681dSNathan Fontenot 		return;
6628342681dSNathan Fontenot 
6638342681dSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
6648342681dSNathan Fontenot 	if (rc)
6650204568aSPaul Mackerras 		return;
6660204568aSPaul Mackerras 
667cf00085dSChandru 	/* check if this is a kexec/kdump kernel */
668cf00085dSChandru 	usm = of_get_usable_memory(memory);
669cf00085dSChandru 	if (usm != NULL)
670cf00085dSChandru 		is_kexec_kdump = 1;
671cf00085dSChandru 
6720204568aSPaul Mackerras 	for (; n != 0; --n) {
6738342681dSNathan Fontenot 		struct of_drconf_cell drmem;
6741daa6d08SBalbir Singh 
6758342681dSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
6768342681dSNathan Fontenot 
6778342681dSNathan Fontenot 		/* skip this block if the reserved bit is set in flags (0x80)
6788342681dSNathan Fontenot 		   or if the block is not assigned to this partition (0x8) */
6798342681dSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
6808342681dSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
6818342681dSNathan Fontenot 			continue;
6828342681dSNathan Fontenot 
683cf00085dSChandru 		base = drmem.base_addr;
6843fdfd990SBenjamin Herrenschmidt 		size = lmb_size;
685cf00085dSChandru 		ranges = 1;
6868342681dSNathan Fontenot 
687cf00085dSChandru 		if (is_kexec_kdump) {
688cf00085dSChandru 			ranges = read_usm_ranges(&usm);
689cf00085dSChandru 			if (!ranges) /* there are no (base, size) duple */
6900204568aSPaul Mackerras 				continue;
691cf00085dSChandru 		}
692cf00085dSChandru 		do {
693cf00085dSChandru 			if (is_kexec_kdump) {
694cf00085dSChandru 				base = read_n_cells(n_mem_addr_cells, &usm);
695cf00085dSChandru 				size = read_n_cells(n_mem_size_cells, &usm);
696cf00085dSChandru 			}
697cf00085dSChandru 			nid = of_drconf_to_nid_single(&drmem, &aa);
698cf00085dSChandru 			fake_numa_create_new_node(
699cf00085dSChandru 				((base + size) >> PAGE_SHIFT),
700cf00085dSChandru 					   &nid);
701cf00085dSChandru 			node_set_online(nid);
702cf00085dSChandru 			sz = numa_enforce_memory_limit(base, size);
703cf00085dSChandru 			if (sz)
704cf00085dSChandru 				add_active_range(nid, base >> PAGE_SHIFT,
705cf00085dSChandru 						 (base >> PAGE_SHIFT)
706cf00085dSChandru 						 + (sz >> PAGE_SHIFT));
707cf00085dSChandru 		} while (--ranges);
7080204568aSPaul Mackerras 	}
7090204568aSPaul Mackerras }
7100204568aSPaul Mackerras 
711ab1f9dacSPaul Mackerras static int __init parse_numa_properties(void)
712ab1f9dacSPaul Mackerras {
713ab1f9dacSPaul Mackerras 	struct device_node *cpu = NULL;
714ab1f9dacSPaul Mackerras 	struct device_node *memory = NULL;
715482ec7c4SNathan Lynch 	int default_nid = 0;
716ab1f9dacSPaul Mackerras 	unsigned long i;
717ab1f9dacSPaul Mackerras 
718ab1f9dacSPaul Mackerras 	if (numa_enabled == 0) {
719ab1f9dacSPaul Mackerras 		printk(KERN_WARNING "NUMA disabled by user\n");
720ab1f9dacSPaul Mackerras 		return -1;
721ab1f9dacSPaul Mackerras 	}
722ab1f9dacSPaul Mackerras 
723ab1f9dacSPaul Mackerras 	min_common_depth = find_min_common_depth();
724ab1f9dacSPaul Mackerras 
725ab1f9dacSPaul Mackerras 	if (min_common_depth < 0)
726ab1f9dacSPaul Mackerras 		return min_common_depth;
727ab1f9dacSPaul Mackerras 
728bf4b85b0SNathan Lynch 	dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
729bf4b85b0SNathan Lynch 
730ab1f9dacSPaul Mackerras 	/*
731482ec7c4SNathan Lynch 	 * Even though we connect cpus to numa domains later in SMP
732482ec7c4SNathan Lynch 	 * init, we need to know the node ids now. This is because
733482ec7c4SNathan Lynch 	 * each node to be onlined must have NODE_DATA etc backing it.
734ab1f9dacSPaul Mackerras 	 */
735482ec7c4SNathan Lynch 	for_each_present_cpu(i) {
736cf950b7aSNathan Lynch 		int nid;
737ab1f9dacSPaul Mackerras 
7388b16cd23SMilton Miller 		cpu = of_get_cpu_node(i, NULL);
739482ec7c4SNathan Lynch 		BUG_ON(!cpu);
740953039c8SJeremy Kerr 		nid = of_node_to_nid_single(cpu);
741ab1f9dacSPaul Mackerras 		of_node_put(cpu);
742ab1f9dacSPaul Mackerras 
743482ec7c4SNathan Lynch 		/*
744482ec7c4SNathan Lynch 		 * Don't fall back to default_nid yet -- we will plug
745482ec7c4SNathan Lynch 		 * cpus into nodes once the memory scan has discovered
746482ec7c4SNathan Lynch 		 * the topology.
747482ec7c4SNathan Lynch 		 */
748482ec7c4SNathan Lynch 		if (nid < 0)
749482ec7c4SNathan Lynch 			continue;
750482ec7c4SNathan Lynch 		node_set_online(nid);
751ab1f9dacSPaul Mackerras 	}
752ab1f9dacSPaul Mackerras 
753237a0989SMike Kravetz 	get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
754ab1f9dacSPaul Mackerras 	memory = NULL;
755ab1f9dacSPaul Mackerras 	while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
756ab1f9dacSPaul Mackerras 		unsigned long start;
757ab1f9dacSPaul Mackerras 		unsigned long size;
758cf950b7aSNathan Lynch 		int nid;
759ab1f9dacSPaul Mackerras 		int ranges;
760a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
761ab1f9dacSPaul Mackerras 		unsigned int len;
762ab1f9dacSPaul Mackerras 
763e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory,
764ba759485SMichael Ellerman 			"linux,usable-memory", &len);
765ba759485SMichael Ellerman 		if (!memcell_buf || len <= 0)
766e2eb6392SStephen Rothwell 			memcell_buf = of_get_property(memory, "reg", &len);
767ab1f9dacSPaul Mackerras 		if (!memcell_buf || len <= 0)
768ab1f9dacSPaul Mackerras 			continue;
769ab1f9dacSPaul Mackerras 
770cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
771cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
772ab1f9dacSPaul Mackerras new_range:
773ab1f9dacSPaul Mackerras 		/* these are order-sensitive, and modify the buffer pointer */
774237a0989SMike Kravetz 		start = read_n_cells(n_mem_addr_cells, &memcell_buf);
775237a0989SMike Kravetz 		size = read_n_cells(n_mem_size_cells, &memcell_buf);
776ab1f9dacSPaul Mackerras 
777482ec7c4SNathan Lynch 		/*
778482ec7c4SNathan Lynch 		 * Assumption: either all memory nodes or none will
779482ec7c4SNathan Lynch 		 * have associativity properties.  If none, then
780482ec7c4SNathan Lynch 		 * everything goes to default_nid.
781482ec7c4SNathan Lynch 		 */
782953039c8SJeremy Kerr 		nid = of_node_to_nid_single(memory);
783482ec7c4SNathan Lynch 		if (nid < 0)
784482ec7c4SNathan Lynch 			nid = default_nid;
7851daa6d08SBalbir Singh 
7861daa6d08SBalbir Singh 		fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
787482ec7c4SNathan Lynch 		node_set_online(nid);
788ab1f9dacSPaul Mackerras 
789ab1f9dacSPaul Mackerras 		if (!(size = numa_enforce_memory_limit(start, size))) {
790ab1f9dacSPaul Mackerras 			if (--ranges)
791ab1f9dacSPaul Mackerras 				goto new_range;
792ab1f9dacSPaul Mackerras 			else
793ab1f9dacSPaul Mackerras 				continue;
794ab1f9dacSPaul Mackerras 		}
795ab1f9dacSPaul Mackerras 
796c67c3cb4SMel Gorman 		add_active_range(nid, start >> PAGE_SHIFT,
797c67c3cb4SMel Gorman 				(start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
798ab1f9dacSPaul Mackerras 
799ab1f9dacSPaul Mackerras 		if (--ranges)
800ab1f9dacSPaul Mackerras 			goto new_range;
801ab1f9dacSPaul Mackerras 	}
802ab1f9dacSPaul Mackerras 
8030204568aSPaul Mackerras 	/*
80495f72d1eSYinghai Lu 	 * Now do the same thing for each MEMBLOCK listed in the ibm,dynamic-memory
8050204568aSPaul Mackerras 	 * property in the ibm,dynamic-reconfiguration-memory node.
8060204568aSPaul Mackerras 	 */
8070204568aSPaul Mackerras 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
8080204568aSPaul Mackerras 	if (memory)
8090204568aSPaul Mackerras 		parse_drconf_memory(memory);
8100204568aSPaul Mackerras 
811ab1f9dacSPaul Mackerras 	return 0;
812ab1f9dacSPaul Mackerras }
813ab1f9dacSPaul Mackerras 
814ab1f9dacSPaul Mackerras static void __init setup_nonnuma(void)
815ab1f9dacSPaul Mackerras {
81695f72d1eSYinghai Lu 	unsigned long top_of_ram = memblock_end_of_DRAM();
81795f72d1eSYinghai Lu 	unsigned long total_ram = memblock_phys_mem_size();
818c67c3cb4SMel Gorman 	unsigned long start_pfn, end_pfn;
81928be7072SBenjamin Herrenschmidt 	unsigned int nid = 0;
82028be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
821ab1f9dacSPaul Mackerras 
822e110b281SOlof Johansson 	printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
823ab1f9dacSPaul Mackerras 	       top_of_ram, total_ram);
824e110b281SOlof Johansson 	printk(KERN_DEBUG "Memory hole size: %ldMB\n",
825ab1f9dacSPaul Mackerras 	       (top_of_ram - total_ram) >> 20);
826ab1f9dacSPaul Mackerras 
82728be7072SBenjamin Herrenschmidt 	for_each_memblock(memory, reg) {
828c7fc2de0SYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(reg);
829c7fc2de0SYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(reg);
8301daa6d08SBalbir Singh 
8311daa6d08SBalbir Singh 		fake_numa_create_new_node(end_pfn, &nid);
8321daa6d08SBalbir Singh 		add_active_range(nid, start_pfn, end_pfn);
8331daa6d08SBalbir Singh 		node_set_online(nid);
834c67c3cb4SMel Gorman 	}
835ab1f9dacSPaul Mackerras }
836ab1f9dacSPaul Mackerras 
8374b703a23SAnton Blanchard void __init dump_numa_cpu_topology(void)
8384b703a23SAnton Blanchard {
8394b703a23SAnton Blanchard 	unsigned int node;
8404b703a23SAnton Blanchard 	unsigned int cpu, count;
8414b703a23SAnton Blanchard 
8424b703a23SAnton Blanchard 	if (min_common_depth == -1 || !numa_enabled)
8434b703a23SAnton Blanchard 		return;
8444b703a23SAnton Blanchard 
8454b703a23SAnton Blanchard 	for_each_online_node(node) {
846e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d CPUs:", node);
8474b703a23SAnton Blanchard 
8484b703a23SAnton Blanchard 		count = 0;
8494b703a23SAnton Blanchard 		/*
8504b703a23SAnton Blanchard 		 * If we used a CPU iterator here we would miss printing
8514b703a23SAnton Blanchard 		 * the holes in the cpumap.
8524b703a23SAnton Blanchard 		 */
85325863de0SAnton Blanchard 		for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
85425863de0SAnton Blanchard 			if (cpumask_test_cpu(cpu,
85525863de0SAnton Blanchard 					node_to_cpumask_map[node])) {
8564b703a23SAnton Blanchard 				if (count == 0)
8574b703a23SAnton Blanchard 					printk(" %u", cpu);
8584b703a23SAnton Blanchard 				++count;
8594b703a23SAnton Blanchard 			} else {
8604b703a23SAnton Blanchard 				if (count > 1)
8614b703a23SAnton Blanchard 					printk("-%u", cpu - 1);
8624b703a23SAnton Blanchard 				count = 0;
8634b703a23SAnton Blanchard 			}
8644b703a23SAnton Blanchard 		}
8654b703a23SAnton Blanchard 
8664b703a23SAnton Blanchard 		if (count > 1)
86725863de0SAnton Blanchard 			printk("-%u", nr_cpu_ids - 1);
8684b703a23SAnton Blanchard 		printk("\n");
8694b703a23SAnton Blanchard 	}
8704b703a23SAnton Blanchard }
8714b703a23SAnton Blanchard 
8724b703a23SAnton Blanchard static void __init dump_numa_memory_topology(void)
873ab1f9dacSPaul Mackerras {
874ab1f9dacSPaul Mackerras 	unsigned int node;
875ab1f9dacSPaul Mackerras 	unsigned int count;
876ab1f9dacSPaul Mackerras 
877ab1f9dacSPaul Mackerras 	if (min_common_depth == -1 || !numa_enabled)
878ab1f9dacSPaul Mackerras 		return;
879ab1f9dacSPaul Mackerras 
880ab1f9dacSPaul Mackerras 	for_each_online_node(node) {
881ab1f9dacSPaul Mackerras 		unsigned long i;
882ab1f9dacSPaul Mackerras 
883e110b281SOlof Johansson 		printk(KERN_DEBUG "Node %d Memory:", node);
884ab1f9dacSPaul Mackerras 
885ab1f9dacSPaul Mackerras 		count = 0;
886ab1f9dacSPaul Mackerras 
88795f72d1eSYinghai Lu 		for (i = 0; i < memblock_end_of_DRAM();
88845fb6ceaSAnton Blanchard 		     i += (1 << SECTION_SIZE_BITS)) {
88945fb6ceaSAnton Blanchard 			if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
890ab1f9dacSPaul Mackerras 				if (count == 0)
891ab1f9dacSPaul Mackerras 					printk(" 0x%lx", i);
892ab1f9dacSPaul Mackerras 				++count;
893ab1f9dacSPaul Mackerras 			} else {
894ab1f9dacSPaul Mackerras 				if (count > 0)
895ab1f9dacSPaul Mackerras 					printk("-0x%lx", i);
896ab1f9dacSPaul Mackerras 				count = 0;
897ab1f9dacSPaul Mackerras 			}
898ab1f9dacSPaul Mackerras 		}
899ab1f9dacSPaul Mackerras 
900ab1f9dacSPaul Mackerras 		if (count > 0)
901ab1f9dacSPaul Mackerras 			printk("-0x%lx", i);
902ab1f9dacSPaul Mackerras 		printk("\n");
903ab1f9dacSPaul Mackerras 	}
904ab1f9dacSPaul Mackerras }
905ab1f9dacSPaul Mackerras 
906ab1f9dacSPaul Mackerras /*
90795f72d1eSYinghai Lu  * Allocate some memory, satisfying the memblock or bootmem allocator where
908ab1f9dacSPaul Mackerras  * required. nid is the preferred node and end is the physical address of
909ab1f9dacSPaul Mackerras  * the highest address in the node.
910ab1f9dacSPaul Mackerras  *
9110be210fdSDave Hansen  * Returns the virtual address of the memory.
912ab1f9dacSPaul Mackerras  */
913893473dfSDave Hansen static void __init *careful_zallocation(int nid, unsigned long size,
91445fb6ceaSAnton Blanchard 				       unsigned long align,
91545fb6ceaSAnton Blanchard 				       unsigned long end_pfn)
916ab1f9dacSPaul Mackerras {
9170be210fdSDave Hansen 	void *ret;
91845fb6ceaSAnton Blanchard 	int new_nid;
9190be210fdSDave Hansen 	unsigned long ret_paddr;
9200be210fdSDave Hansen 
92195f72d1eSYinghai Lu 	ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
922ab1f9dacSPaul Mackerras 
923ab1f9dacSPaul Mackerras 	/* retry over all memory */
9240be210fdSDave Hansen 	if (!ret_paddr)
92595f72d1eSYinghai Lu 		ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
926ab1f9dacSPaul Mackerras 
9270be210fdSDave Hansen 	if (!ret_paddr)
9285d21ea2bSDave Hansen 		panic("numa.c: cannot allocate %lu bytes for node %d",
929ab1f9dacSPaul Mackerras 		      size, nid);
930ab1f9dacSPaul Mackerras 
9310be210fdSDave Hansen 	ret = __va(ret_paddr);
9320be210fdSDave Hansen 
933ab1f9dacSPaul Mackerras 	/*
934c555e520SDave Hansen 	 * We initialize the nodes in numeric order: 0, 1, 2...
93595f72d1eSYinghai Lu 	 * and hand over control from the MEMBLOCK allocator to the
936c555e520SDave Hansen 	 * bootmem allocator.  If this function is called for
937c555e520SDave Hansen 	 * node 5, then we know that all nodes <5 are using the
93895f72d1eSYinghai Lu 	 * bootmem allocator instead of the MEMBLOCK allocator.
939c555e520SDave Hansen 	 *
940c555e520SDave Hansen 	 * So, check the nid from which this allocation came
941c555e520SDave Hansen 	 * and double check to see if we need to use bootmem
94295f72d1eSYinghai Lu 	 * instead of the MEMBLOCK.  We don't free the MEMBLOCK memory
943c555e520SDave Hansen 	 * since it would be useless.
944ab1f9dacSPaul Mackerras 	 */
9450be210fdSDave Hansen 	new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
94645fb6ceaSAnton Blanchard 	if (new_nid < nid) {
9470be210fdSDave Hansen 		ret = __alloc_bootmem_node(NODE_DATA(new_nid),
948ab1f9dacSPaul Mackerras 				size, align, 0);
949ab1f9dacSPaul Mackerras 
9500be210fdSDave Hansen 		dbg("alloc_bootmem %p %lx\n", ret, size);
951ab1f9dacSPaul Mackerras 	}
952ab1f9dacSPaul Mackerras 
953893473dfSDave Hansen 	memset(ret, 0, size);
9540be210fdSDave Hansen 	return ret;
955ab1f9dacSPaul Mackerras }
956ab1f9dacSPaul Mackerras 
95774b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata ppc64_numa_nb = {
95874b85f37SChandra Seetharaman 	.notifier_call = cpu_numa_callback,
95974b85f37SChandra Seetharaman 	.priority = 1 /* Must run before sched domains notifier. */
96074b85f37SChandra Seetharaman };
96174b85f37SChandra Seetharaman 
9624a618669SDave Hansen static void mark_reserved_regions_for_nid(int nid)
963ab1f9dacSPaul Mackerras {
9644a618669SDave Hansen 	struct pglist_data *node = NODE_DATA(nid);
96528be7072SBenjamin Herrenschmidt 	struct memblock_region *reg;
966ab1f9dacSPaul Mackerras 
96728be7072SBenjamin Herrenschmidt 	for_each_memblock(reserved, reg) {
96828be7072SBenjamin Herrenschmidt 		unsigned long physbase = reg->base;
96928be7072SBenjamin Herrenschmidt 		unsigned long size = reg->size;
9708f64e1f2SJon Tollefson 		unsigned long start_pfn = physbase >> PAGE_SHIFT;
97106eccea6SDave Hansen 		unsigned long end_pfn = PFN_UP(physbase + size);
9728f64e1f2SJon Tollefson 		struct node_active_region node_ar;
9734a618669SDave Hansen 		unsigned long node_end_pfn = node->node_start_pfn +
9744a618669SDave Hansen 					     node->node_spanned_pages;
9754a618669SDave Hansen 
9764a618669SDave Hansen 		/*
97795f72d1eSYinghai Lu 		 * Check to make sure that this memblock.reserved area is
9784a618669SDave Hansen 		 * within the bounds of the node that we care about.
9794a618669SDave Hansen 		 * Checking the nid of the start and end points is not
9804a618669SDave Hansen 		 * sufficient because the reserved area could span the
9814a618669SDave Hansen 		 * entire node.
9824a618669SDave Hansen 		 */
9834a618669SDave Hansen 		if (end_pfn <= node->node_start_pfn ||
9844a618669SDave Hansen 		    start_pfn >= node_end_pfn)
9854a618669SDave Hansen 			continue;
986ab1f9dacSPaul Mackerras 
9878f64e1f2SJon Tollefson 		get_node_active_region(start_pfn, &node_ar);
988e8170372SJon Tollefson 		while (start_pfn < end_pfn &&
989e8170372SJon Tollefson 			node_ar.start_pfn < node_ar.end_pfn) {
990e8170372SJon Tollefson 			unsigned long reserve_size = size;
9918f64e1f2SJon Tollefson 			/*
9928f64e1f2SJon Tollefson 			 * if reserved region extends past active region
9938f64e1f2SJon Tollefson 			 * then trim size to active region
9948f64e1f2SJon Tollefson 			 */
9958f64e1f2SJon Tollefson 			if (end_pfn > node_ar.end_pfn)
996e8170372SJon Tollefson 				reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
99706eccea6SDave Hansen 					- physbase;
998a4c74dddSDave Hansen 			/*
999a4c74dddSDave Hansen 			 * Only worry about *this* node, others may not
1000a4c74dddSDave Hansen 			 * yet have valid NODE_DATA().
1001a4c74dddSDave Hansen 			 */
1002a4c74dddSDave Hansen 			if (node_ar.nid == nid) {
1003a4c74dddSDave Hansen 				dbg("reserve_bootmem %lx %lx nid=%d\n",
1004a4c74dddSDave Hansen 					physbase, reserve_size, node_ar.nid);
1005a4c74dddSDave Hansen 				reserve_bootmem_node(NODE_DATA(node_ar.nid),
1006a4c74dddSDave Hansen 						physbase, reserve_size,
1007a4c74dddSDave Hansen 						BOOTMEM_DEFAULT);
1008a4c74dddSDave Hansen 			}
10098f64e1f2SJon Tollefson 			/*
10108f64e1f2SJon Tollefson 			 * if reserved region is contained in the active region
10118f64e1f2SJon Tollefson 			 * then done.
10128f64e1f2SJon Tollefson 			 */
10138f64e1f2SJon Tollefson 			if (end_pfn <= node_ar.end_pfn)
10148f64e1f2SJon Tollefson 				break;
10158f64e1f2SJon Tollefson 
10168f64e1f2SJon Tollefson 			/*
10178f64e1f2SJon Tollefson 			 * reserved region extends past the active region
10188f64e1f2SJon Tollefson 			 *   get next active region that contains this
10198f64e1f2SJon Tollefson 			 *   reserved region
10208f64e1f2SJon Tollefson 			 */
10218f64e1f2SJon Tollefson 			start_pfn = node_ar.end_pfn;
10228f64e1f2SJon Tollefson 			physbase = start_pfn << PAGE_SHIFT;
1023e8170372SJon Tollefson 			size = size - reserve_size;
10248f64e1f2SJon Tollefson 			get_node_active_region(start_pfn, &node_ar);
1025ab1f9dacSPaul Mackerras 		}
10264a618669SDave Hansen 	}
1027ab1f9dacSPaul Mackerras }
10288f64e1f2SJon Tollefson 
10294a618669SDave Hansen 
10304a618669SDave Hansen void __init do_init_bootmem(void)
10314a618669SDave Hansen {
10324a618669SDave Hansen 	int nid;
10334a618669SDave Hansen 
10344a618669SDave Hansen 	min_low_pfn = 0;
103595f72d1eSYinghai Lu 	max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
10364a618669SDave Hansen 	max_pfn = max_low_pfn;
10374a618669SDave Hansen 
10384a618669SDave Hansen 	if (parse_numa_properties())
10394a618669SDave Hansen 		setup_nonnuma();
10404a618669SDave Hansen 	else
10414a618669SDave Hansen 		dump_numa_memory_topology();
10424a618669SDave Hansen 
10434a618669SDave Hansen 	for_each_online_node(nid) {
10444a618669SDave Hansen 		unsigned long start_pfn, end_pfn;
10450be210fdSDave Hansen 		void *bootmem_vaddr;
10464a618669SDave Hansen 		unsigned long bootmap_pages;
10474a618669SDave Hansen 
10484a618669SDave Hansen 		get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
10494a618669SDave Hansen 
10504a618669SDave Hansen 		/*
10514a618669SDave Hansen 		 * Allocate the node structure node local if possible
10524a618669SDave Hansen 		 *
10534a618669SDave Hansen 		 * Be careful moving this around, as it relies on all
10544a618669SDave Hansen 		 * previous nodes' bootmem to be initialized and have
10554a618669SDave Hansen 		 * all reserved areas marked.
10564a618669SDave Hansen 		 */
1057893473dfSDave Hansen 		NODE_DATA(nid) = careful_zallocation(nid,
10584a618669SDave Hansen 					sizeof(struct pglist_data),
10594a618669SDave Hansen 					SMP_CACHE_BYTES, end_pfn);
10604a618669SDave Hansen 
10614a618669SDave Hansen   		dbg("node %d\n", nid);
10624a618669SDave Hansen 		dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
10634a618669SDave Hansen 
10644a618669SDave Hansen 		NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
10654a618669SDave Hansen 		NODE_DATA(nid)->node_start_pfn = start_pfn;
10664a618669SDave Hansen 		NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
10674a618669SDave Hansen 
10684a618669SDave Hansen 		if (NODE_DATA(nid)->node_spanned_pages == 0)
10694a618669SDave Hansen   			continue;
10704a618669SDave Hansen 
10714a618669SDave Hansen   		dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
10724a618669SDave Hansen   		dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
10734a618669SDave Hansen 
10744a618669SDave Hansen 		bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
1075893473dfSDave Hansen 		bootmem_vaddr = careful_zallocation(nid,
10764a618669SDave Hansen 					bootmap_pages << PAGE_SHIFT,
10774a618669SDave Hansen 					PAGE_SIZE, end_pfn);
10784a618669SDave Hansen 
10790be210fdSDave Hansen 		dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
10804a618669SDave Hansen 
10810be210fdSDave Hansen 		init_bootmem_node(NODE_DATA(nid),
10820be210fdSDave Hansen 				  __pa(bootmem_vaddr) >> PAGE_SHIFT,
10834a618669SDave Hansen 				  start_pfn, end_pfn);
10844a618669SDave Hansen 
10854a618669SDave Hansen 		free_bootmem_with_active_regions(nid, end_pfn);
10864a618669SDave Hansen 		/*
10874a618669SDave Hansen 		 * Be very careful about moving this around.  Future
1088893473dfSDave Hansen 		 * calls to careful_zallocation() depend on this getting
10894a618669SDave Hansen 		 * done correctly.
10904a618669SDave Hansen 		 */
10914a618669SDave Hansen 		mark_reserved_regions_for_nid(nid);
10928f64e1f2SJon Tollefson 		sparse_memory_present_with_active_regions(nid);
1093ab1f9dacSPaul Mackerras 	}
1094d3f6204aSBenjamin Herrenschmidt 
1095d3f6204aSBenjamin Herrenschmidt 	init_bootmem_done = 1;
109625863de0SAnton Blanchard 
109725863de0SAnton Blanchard 	/*
109825863de0SAnton Blanchard 	 * Now bootmem is initialised we can create the node to cpumask
109925863de0SAnton Blanchard 	 * lookup tables and setup the cpu callback to populate them.
110025863de0SAnton Blanchard 	 */
110125863de0SAnton Blanchard 	setup_node_to_cpumask_map();
110225863de0SAnton Blanchard 
110325863de0SAnton Blanchard 	register_cpu_notifier(&ppc64_numa_nb);
110425863de0SAnton Blanchard 	cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
110525863de0SAnton Blanchard 			  (void *)(unsigned long)boot_cpuid);
11064a618669SDave Hansen }
1107ab1f9dacSPaul Mackerras 
1108ab1f9dacSPaul Mackerras void __init paging_init(void)
1109ab1f9dacSPaul Mackerras {
11106391af17SMel Gorman 	unsigned long max_zone_pfns[MAX_NR_ZONES];
11116391af17SMel Gorman 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
111295f72d1eSYinghai Lu 	max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
1113c67c3cb4SMel Gorman 	free_area_init_nodes(max_zone_pfns);
1114ab1f9dacSPaul Mackerras }
1115ab1f9dacSPaul Mackerras 
1116ab1f9dacSPaul Mackerras static int __init early_numa(char *p)
1117ab1f9dacSPaul Mackerras {
1118ab1f9dacSPaul Mackerras 	if (!p)
1119ab1f9dacSPaul Mackerras 		return 0;
1120ab1f9dacSPaul Mackerras 
1121ab1f9dacSPaul Mackerras 	if (strstr(p, "off"))
1122ab1f9dacSPaul Mackerras 		numa_enabled = 0;
1123ab1f9dacSPaul Mackerras 
1124ab1f9dacSPaul Mackerras 	if (strstr(p, "debug"))
1125ab1f9dacSPaul Mackerras 		numa_debug = 1;
1126ab1f9dacSPaul Mackerras 
11271daa6d08SBalbir Singh 	p = strstr(p, "fake=");
11281daa6d08SBalbir Singh 	if (p)
11291daa6d08SBalbir Singh 		cmdline = p + strlen("fake=");
11301daa6d08SBalbir Singh 
1131ab1f9dacSPaul Mackerras 	return 0;
1132ab1f9dacSPaul Mackerras }
1133ab1f9dacSPaul Mackerras early_param("numa", early_numa);
1134237a0989SMike Kravetz 
1135237a0989SMike Kravetz #ifdef CONFIG_MEMORY_HOTPLUG
1136237a0989SMike Kravetz /*
11370f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for
11380f16ef7fSNathan Fontenot  * memory represented in the device tree by the property
11390f16ef7fSNathan Fontenot  * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
11400db9360aSNathan Fontenot  */
11410db9360aSNathan Fontenot static int hot_add_drconf_scn_to_nid(struct device_node *memory,
11420db9360aSNathan Fontenot 				     unsigned long scn_addr)
11430db9360aSNathan Fontenot {
11440db9360aSNathan Fontenot 	const u32 *dm;
11450f16ef7fSNathan Fontenot 	unsigned int drconf_cell_cnt, rc;
11463fdfd990SBenjamin Herrenschmidt 	unsigned long lmb_size;
11470db9360aSNathan Fontenot 	struct assoc_arrays aa;
11480f16ef7fSNathan Fontenot 	int nid = -1;
11490db9360aSNathan Fontenot 
11500f16ef7fSNathan Fontenot 	drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
11510f16ef7fSNathan Fontenot 	if (!drconf_cell_cnt)
11520f16ef7fSNathan Fontenot 		return -1;
11530db9360aSNathan Fontenot 
11543fdfd990SBenjamin Herrenschmidt 	lmb_size = of_get_lmb_size(memory);
11553fdfd990SBenjamin Herrenschmidt 	if (!lmb_size)
11560f16ef7fSNathan Fontenot 		return -1;
11570db9360aSNathan Fontenot 
11580db9360aSNathan Fontenot 	rc = of_get_assoc_arrays(memory, &aa);
11590db9360aSNathan Fontenot 	if (rc)
11600f16ef7fSNathan Fontenot 		return -1;
11610db9360aSNathan Fontenot 
11620f16ef7fSNathan Fontenot 	for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
11630db9360aSNathan Fontenot 		struct of_drconf_cell drmem;
11640db9360aSNathan Fontenot 
11650db9360aSNathan Fontenot 		read_drconf_cell(&drmem, &dm);
11660db9360aSNathan Fontenot 
11670db9360aSNathan Fontenot 		/* skip this block if it is reserved or not assigned to
11680db9360aSNathan Fontenot 		 * this partition */
11690db9360aSNathan Fontenot 		if ((drmem.flags & DRCONF_MEM_RESERVED)
11700db9360aSNathan Fontenot 		    || !(drmem.flags & DRCONF_MEM_ASSIGNED))
11710db9360aSNathan Fontenot 			continue;
11720db9360aSNathan Fontenot 
11730f16ef7fSNathan Fontenot 		if ((scn_addr < drmem.base_addr)
11743fdfd990SBenjamin Herrenschmidt 		    || (scn_addr >= (drmem.base_addr + lmb_size)))
11750f16ef7fSNathan Fontenot 			continue;
11760db9360aSNathan Fontenot 
11770f16ef7fSNathan Fontenot 		nid = of_drconf_to_nid_single(&drmem, &aa);
11780f16ef7fSNathan Fontenot 		break;
11790db9360aSNathan Fontenot 	}
11800db9360aSNathan Fontenot 
11810f16ef7fSNathan Fontenot 	return nid;
11820db9360aSNathan Fontenot }
11830db9360aSNathan Fontenot 
11840db9360aSNathan Fontenot /*
11850f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section for memory
11860f16ef7fSNathan Fontenot  * represented in the device tree as a node (i.e. memory@XXXX) for
118795f72d1eSYinghai Lu  * each memblock.
1188237a0989SMike Kravetz  */
11890f16ef7fSNathan Fontenot int hot_add_node_scn_to_nid(unsigned long scn_addr)
1190237a0989SMike Kravetz {
1191237a0989SMike Kravetz 	struct device_node *memory = NULL;
11920f16ef7fSNathan Fontenot 	int nid = -1;
1193237a0989SMike Kravetz 
1194237a0989SMike Kravetz 	while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
1195237a0989SMike Kravetz 		unsigned long start, size;
1196b226e462SMike Kravetz 		int ranges;
1197a7f67bdfSJeremy Kerr 		const unsigned int *memcell_buf;
1198237a0989SMike Kravetz 		unsigned int len;
1199237a0989SMike Kravetz 
1200e2eb6392SStephen Rothwell 		memcell_buf = of_get_property(memory, "reg", &len);
1201237a0989SMike Kravetz 		if (!memcell_buf || len <= 0)
1202237a0989SMike Kravetz 			continue;
1203237a0989SMike Kravetz 
1204cc5d0189SBenjamin Herrenschmidt 		/* ranges in cell */
1205cc5d0189SBenjamin Herrenschmidt 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
12060f16ef7fSNathan Fontenot 
12070f16ef7fSNathan Fontenot 		while (ranges--) {
1208237a0989SMike Kravetz 			start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1209237a0989SMike Kravetz 			size = read_n_cells(n_mem_size_cells, &memcell_buf);
1210237a0989SMike Kravetz 
12110f16ef7fSNathan Fontenot 			if ((scn_addr < start) || (scn_addr >= (start + size)))
12120f16ef7fSNathan Fontenot 				continue;
12130f16ef7fSNathan Fontenot 
12140f16ef7fSNathan Fontenot 			nid = of_node_to_nid_single(memory);
12150f16ef7fSNathan Fontenot 			break;
12160f16ef7fSNathan Fontenot 		}
12170f16ef7fSNathan Fontenot 
1218237a0989SMike Kravetz 		of_node_put(memory);
12190f16ef7fSNathan Fontenot 		if (nid >= 0)
12200f16ef7fSNathan Fontenot 			break;
12210f16ef7fSNathan Fontenot 	}
12220f16ef7fSNathan Fontenot 
12230db9360aSNathan Fontenot 	return nid;
1224237a0989SMike Kravetz }
1225237a0989SMike Kravetz 
12260f16ef7fSNathan Fontenot /*
12270f16ef7fSNathan Fontenot  * Find the node associated with a hot added memory section.  Section
122895f72d1eSYinghai Lu  * corresponds to a SPARSEMEM section, not an MEMBLOCK.  It is assumed that
122995f72d1eSYinghai Lu  * sections are fully contained within a single MEMBLOCK.
12300f16ef7fSNathan Fontenot  */
12310f16ef7fSNathan Fontenot int hot_add_scn_to_nid(unsigned long scn_addr)
12320f16ef7fSNathan Fontenot {
12330f16ef7fSNathan Fontenot 	struct device_node *memory = NULL;
12340f16ef7fSNathan Fontenot 	int nid, found = 0;
12350f16ef7fSNathan Fontenot 
12360f16ef7fSNathan Fontenot 	if (!numa_enabled || (min_common_depth < 0))
123772c33688SH Hartley Sweeten 		return first_online_node;
12380f16ef7fSNathan Fontenot 
12390f16ef7fSNathan Fontenot 	memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
12400f16ef7fSNathan Fontenot 	if (memory) {
12410f16ef7fSNathan Fontenot 		nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
12420f16ef7fSNathan Fontenot 		of_node_put(memory);
12430f16ef7fSNathan Fontenot 	} else {
12440f16ef7fSNathan Fontenot 		nid = hot_add_node_scn_to_nid(scn_addr);
1245237a0989SMike Kravetz 	}
12460f16ef7fSNathan Fontenot 
12470f16ef7fSNathan Fontenot 	if (nid < 0 || !node_online(nid))
124872c33688SH Hartley Sweeten 		nid = first_online_node;
12490f16ef7fSNathan Fontenot 
12500f16ef7fSNathan Fontenot 	if (NODE_DATA(nid)->node_spanned_pages)
12510f16ef7fSNathan Fontenot 		return nid;
12520f16ef7fSNathan Fontenot 
12530f16ef7fSNathan Fontenot 	for_each_online_node(nid) {
12540f16ef7fSNathan Fontenot 		if (NODE_DATA(nid)->node_spanned_pages) {
12550f16ef7fSNathan Fontenot 			found = 1;
12560f16ef7fSNathan Fontenot 			break;
1257237a0989SMike Kravetz 		}
12580f16ef7fSNathan Fontenot 	}
12590f16ef7fSNathan Fontenot 
12600f16ef7fSNathan Fontenot 	BUG_ON(!found);
12610f16ef7fSNathan Fontenot 	return nid;
12620f16ef7fSNathan Fontenot }
12630f16ef7fSNathan Fontenot 
1264cd34206eSNishanth Aravamudan static u64 hot_add_drconf_memory_max(void)
1265cd34206eSNishanth Aravamudan {
1266cd34206eSNishanth Aravamudan         struct device_node *memory = NULL;
1267cd34206eSNishanth Aravamudan         unsigned int drconf_cell_cnt = 0;
1268cd34206eSNishanth Aravamudan         u64 lmb_size = 0;
1269cd34206eSNishanth Aravamudan         const u32 *dm = 0;
1270cd34206eSNishanth Aravamudan 
1271cd34206eSNishanth Aravamudan         memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1272cd34206eSNishanth Aravamudan         if (memory) {
1273cd34206eSNishanth Aravamudan                 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1274cd34206eSNishanth Aravamudan                 lmb_size = of_get_lmb_size(memory);
1275cd34206eSNishanth Aravamudan                 of_node_put(memory);
1276cd34206eSNishanth Aravamudan         }
1277cd34206eSNishanth Aravamudan         return lmb_size * drconf_cell_cnt;
1278cd34206eSNishanth Aravamudan }
1279cd34206eSNishanth Aravamudan 
1280cd34206eSNishanth Aravamudan /*
1281cd34206eSNishanth Aravamudan  * memory_hotplug_max - return max address of memory that may be added
1282cd34206eSNishanth Aravamudan  *
1283cd34206eSNishanth Aravamudan  * This is currently only used on systems that support drconfig memory
1284cd34206eSNishanth Aravamudan  * hotplug.
1285cd34206eSNishanth Aravamudan  */
1286cd34206eSNishanth Aravamudan u64 memory_hotplug_max(void)
1287cd34206eSNishanth Aravamudan {
1288cd34206eSNishanth Aravamudan         return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1289cd34206eSNishanth Aravamudan }
1290237a0989SMike Kravetz #endif /* CONFIG_MEMORY_HOTPLUG */
12919eff1a38SJesse Larrew 
1292bd03403aSJesse Larrew /* Virtual Processor Home Node (VPHN) support */
129339bf990eSJesse Larrew #ifdef CONFIG_PPC_SPLPAR
1294*5de16699SAnton Blanchard static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
12959eff1a38SJesse Larrew static cpumask_t cpu_associativity_changes_mask;
12969eff1a38SJesse Larrew static int vphn_enabled;
12979eff1a38SJesse Larrew static void set_topology_timer(void);
12989eff1a38SJesse Larrew 
12999eff1a38SJesse Larrew /*
13009eff1a38SJesse Larrew  * Store the current values of the associativity change counters in the
13019eff1a38SJesse Larrew  * hypervisor.
13029eff1a38SJesse Larrew  */
13039eff1a38SJesse Larrew static void setup_cpu_associativity_change_counters(void)
13049eff1a38SJesse Larrew {
1305cd9d6cc7SJesse Larrew 	int cpu;
13069eff1a38SJesse Larrew 
1307*5de16699SAnton Blanchard 	/* The VPHN feature supports a maximum of 8 reference points */
1308*5de16699SAnton Blanchard 	BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1309*5de16699SAnton Blanchard 
13109eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
1311cd9d6cc7SJesse Larrew 		int i;
13129eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
13139eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
13149eff1a38SJesse Larrew 
1315*5de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++)
13169eff1a38SJesse Larrew 			counts[i] = hypervisor_counts[i];
13179eff1a38SJesse Larrew 	}
13189eff1a38SJesse Larrew }
13199eff1a38SJesse Larrew 
13209eff1a38SJesse Larrew /*
13219eff1a38SJesse Larrew  * The hypervisor maintains a set of 8 associativity change counters in
13229eff1a38SJesse Larrew  * the VPA of each cpu that correspond to the associativity levels in the
13239eff1a38SJesse Larrew  * ibm,associativity-reference-points property. When an associativity
13249eff1a38SJesse Larrew  * level changes, the corresponding counter is incremented.
13259eff1a38SJesse Larrew  *
13269eff1a38SJesse Larrew  * Set a bit in cpu_associativity_changes_mask for each cpu whose home
13279eff1a38SJesse Larrew  * node associativity levels have changed.
13289eff1a38SJesse Larrew  *
13299eff1a38SJesse Larrew  * Returns the number of cpus with unhandled associativity changes.
13309eff1a38SJesse Larrew  */
13319eff1a38SJesse Larrew static int update_cpu_associativity_changes_mask(void)
13329eff1a38SJesse Larrew {
1333cd9d6cc7SJesse Larrew 	int cpu, nr_cpus = 0;
13349eff1a38SJesse Larrew 	cpumask_t *changes = &cpu_associativity_changes_mask;
13359eff1a38SJesse Larrew 
13369eff1a38SJesse Larrew 	cpumask_clear(changes);
13379eff1a38SJesse Larrew 
13389eff1a38SJesse Larrew 	for_each_possible_cpu(cpu) {
13399eff1a38SJesse Larrew 		int i, changed = 0;
13409eff1a38SJesse Larrew 		u8 *counts = vphn_cpu_change_counts[cpu];
13419eff1a38SJesse Larrew 		volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
13429eff1a38SJesse Larrew 
1343*5de16699SAnton Blanchard 		for (i = 0; i < distance_ref_points_depth; i++) {
13449eff1a38SJesse Larrew 			if (hypervisor_counts[i] > counts[i]) {
13459eff1a38SJesse Larrew 				counts[i] = hypervisor_counts[i];
13469eff1a38SJesse Larrew 				changed = 1;
13479eff1a38SJesse Larrew 			}
13489eff1a38SJesse Larrew 		}
13499eff1a38SJesse Larrew 		if (changed) {
13509eff1a38SJesse Larrew 			cpumask_set_cpu(cpu, changes);
13519eff1a38SJesse Larrew 			nr_cpus++;
13529eff1a38SJesse Larrew 		}
13539eff1a38SJesse Larrew 	}
13549eff1a38SJesse Larrew 
13559eff1a38SJesse Larrew 	return nr_cpus;
13569eff1a38SJesse Larrew }
13579eff1a38SJesse Larrew 
13589eff1a38SJesse Larrew /* 6 64-bit registers unpacked into 12 32-bit associativity values */
13599eff1a38SJesse Larrew #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32))
13609eff1a38SJesse Larrew 
13619eff1a38SJesse Larrew /*
13629eff1a38SJesse Larrew  * Convert the associativity domain numbers returned from the hypervisor
13639eff1a38SJesse Larrew  * to the sequence they would appear in the ibm,associativity property.
13649eff1a38SJesse Larrew  */
13659eff1a38SJesse Larrew static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
13669eff1a38SJesse Larrew {
1367cd9d6cc7SJesse Larrew 	int i, nr_assoc_doms = 0;
13689eff1a38SJesse Larrew 	const u16 *field = (const u16*) packed;
13699eff1a38SJesse Larrew 
13709eff1a38SJesse Larrew #define VPHN_FIELD_UNUSED	(0xffff)
13719eff1a38SJesse Larrew #define VPHN_FIELD_MSB		(0x8000)
13729eff1a38SJesse Larrew #define VPHN_FIELD_MASK		(~VPHN_FIELD_MSB)
13739eff1a38SJesse Larrew 
13749eff1a38SJesse Larrew 	for (i = 0; i < VPHN_ASSOC_BUFSIZE; i++) {
13759eff1a38SJesse Larrew 		if (*field == VPHN_FIELD_UNUSED) {
13769eff1a38SJesse Larrew 			/* All significant fields processed, and remaining
13779eff1a38SJesse Larrew 			 * fields contain the reserved value of all 1's.
13789eff1a38SJesse Larrew 			 * Just store them.
13799eff1a38SJesse Larrew 			 */
13809eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13819eff1a38SJesse Larrew 			field += 2;
13827639adaaSJesse Larrew 		} else if (*field & VPHN_FIELD_MSB) {
13839eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field */
13849eff1a38SJesse Larrew 			unpacked[i] = *field & VPHN_FIELD_MASK;
13859eff1a38SJesse Larrew 			field++;
13869eff1a38SJesse Larrew 			nr_assoc_doms++;
13877639adaaSJesse Larrew 		} else {
13889eff1a38SJesse Larrew 			/* Data is in the lower 15 bits of this field
13899eff1a38SJesse Larrew 			 * concatenated with the next 16 bit field
13909eff1a38SJesse Larrew 			 */
13919eff1a38SJesse Larrew 			unpacked[i] = *((u32*)field);
13929eff1a38SJesse Larrew 			field += 2;
13939eff1a38SJesse Larrew 			nr_assoc_doms++;
13949eff1a38SJesse Larrew 		}
13959eff1a38SJesse Larrew 	}
13969eff1a38SJesse Larrew 
13979eff1a38SJesse Larrew 	return nr_assoc_doms;
13989eff1a38SJesse Larrew }
13999eff1a38SJesse Larrew 
14009eff1a38SJesse Larrew /*
14019eff1a38SJesse Larrew  * Retrieve the new associativity information for a virtual processor's
14029eff1a38SJesse Larrew  * home node.
14039eff1a38SJesse Larrew  */
14049eff1a38SJesse Larrew static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
14059eff1a38SJesse Larrew {
1406cd9d6cc7SJesse Larrew 	long rc;
14079eff1a38SJesse Larrew 	long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
14089eff1a38SJesse Larrew 	u64 flags = 1;
14099eff1a38SJesse Larrew 	int hwcpu = get_hard_smp_processor_id(cpu);
14109eff1a38SJesse Larrew 
14119eff1a38SJesse Larrew 	rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
14129eff1a38SJesse Larrew 	vphn_unpack_associativity(retbuf, associativity);
14139eff1a38SJesse Larrew 
14149eff1a38SJesse Larrew 	return rc;
14159eff1a38SJesse Larrew }
14169eff1a38SJesse Larrew 
14179eff1a38SJesse Larrew static long vphn_get_associativity(unsigned long cpu,
14189eff1a38SJesse Larrew 					unsigned int *associativity)
14199eff1a38SJesse Larrew {
1420cd9d6cc7SJesse Larrew 	long rc;
14219eff1a38SJesse Larrew 
14229eff1a38SJesse Larrew 	rc = hcall_vphn(cpu, associativity);
14239eff1a38SJesse Larrew 
14249eff1a38SJesse Larrew 	switch (rc) {
14259eff1a38SJesse Larrew 	case H_FUNCTION:
14269eff1a38SJesse Larrew 		printk(KERN_INFO
14279eff1a38SJesse Larrew 			"VPHN is not supported. Disabling polling...\n");
14289eff1a38SJesse Larrew 		stop_topology_update();
14299eff1a38SJesse Larrew 		break;
14309eff1a38SJesse Larrew 	case H_HARDWARE:
14319eff1a38SJesse Larrew 		printk(KERN_ERR
14329eff1a38SJesse Larrew 			"hcall_vphn() experienced a hardware fault "
14339eff1a38SJesse Larrew 			"preventing VPHN. Disabling polling...\n");
14349eff1a38SJesse Larrew 		stop_topology_update();
14359eff1a38SJesse Larrew 	}
14369eff1a38SJesse Larrew 
14379eff1a38SJesse Larrew 	return rc;
14389eff1a38SJesse Larrew }
14399eff1a38SJesse Larrew 
14409eff1a38SJesse Larrew /*
14419eff1a38SJesse Larrew  * Update the node maps and sysfs entries for each cpu whose home node
14429eff1a38SJesse Larrew  * has changed.
14439eff1a38SJesse Larrew  */
14449eff1a38SJesse Larrew int arch_update_cpu_topology(void)
14459eff1a38SJesse Larrew {
1446cd9d6cc7SJesse Larrew 	int cpu, nid, old_nid;
14479eff1a38SJesse Larrew 	unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
1448cd9d6cc7SJesse Larrew 	struct sys_device *sysdev;
14499eff1a38SJesse Larrew 
14509eff1a38SJesse Larrew 	for_each_cpu_mask(cpu, cpu_associativity_changes_mask) {
14519eff1a38SJesse Larrew 		vphn_get_associativity(cpu, associativity);
14529eff1a38SJesse Larrew 		nid = associativity_to_nid(associativity);
14539eff1a38SJesse Larrew 
14549eff1a38SJesse Larrew 		if (nid < 0 || !node_online(nid))
14559eff1a38SJesse Larrew 			nid = first_online_node;
14569eff1a38SJesse Larrew 
14579eff1a38SJesse Larrew 		old_nid = numa_cpu_lookup_table[cpu];
14589eff1a38SJesse Larrew 
14599eff1a38SJesse Larrew 		/* Disable hotplug while we update the cpu
14609eff1a38SJesse Larrew 		 * masks and sysfs.
14619eff1a38SJesse Larrew 		 */
14629eff1a38SJesse Larrew 		get_online_cpus();
14639eff1a38SJesse Larrew 		unregister_cpu_under_node(cpu, old_nid);
14649eff1a38SJesse Larrew 		unmap_cpu_from_node(cpu);
14659eff1a38SJesse Larrew 		map_cpu_to_node(cpu, nid);
14669eff1a38SJesse Larrew 		register_cpu_under_node(cpu, nid);
14679eff1a38SJesse Larrew 		put_online_cpus();
14689eff1a38SJesse Larrew 
14699eff1a38SJesse Larrew 		sysdev = get_cpu_sysdev(cpu);
14709eff1a38SJesse Larrew 		if (sysdev)
14719eff1a38SJesse Larrew 			kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
14729eff1a38SJesse Larrew 	}
14739eff1a38SJesse Larrew 
14749eff1a38SJesse Larrew 	return 1;
14759eff1a38SJesse Larrew }
14769eff1a38SJesse Larrew 
14779eff1a38SJesse Larrew static void topology_work_fn(struct work_struct *work)
14789eff1a38SJesse Larrew {
14799eff1a38SJesse Larrew 	rebuild_sched_domains();
14809eff1a38SJesse Larrew }
14819eff1a38SJesse Larrew static DECLARE_WORK(topology_work, topology_work_fn);
14829eff1a38SJesse Larrew 
14839eff1a38SJesse Larrew void topology_schedule_update(void)
14849eff1a38SJesse Larrew {
14859eff1a38SJesse Larrew 	schedule_work(&topology_work);
14869eff1a38SJesse Larrew }
14879eff1a38SJesse Larrew 
14889eff1a38SJesse Larrew static void topology_timer_fn(unsigned long ignored)
14899eff1a38SJesse Larrew {
14909eff1a38SJesse Larrew 	if (!vphn_enabled)
14919eff1a38SJesse Larrew 		return;
14929eff1a38SJesse Larrew 	if (update_cpu_associativity_changes_mask() > 0)
14939eff1a38SJesse Larrew 		topology_schedule_update();
14949eff1a38SJesse Larrew 	set_topology_timer();
14959eff1a38SJesse Larrew }
14969eff1a38SJesse Larrew static struct timer_list topology_timer =
14979eff1a38SJesse Larrew 	TIMER_INITIALIZER(topology_timer_fn, 0, 0);
14989eff1a38SJesse Larrew 
14999eff1a38SJesse Larrew static void set_topology_timer(void)
15009eff1a38SJesse Larrew {
15019eff1a38SJesse Larrew 	topology_timer.data = 0;
15029eff1a38SJesse Larrew 	topology_timer.expires = jiffies + 60 * HZ;
15039eff1a38SJesse Larrew 	add_timer(&topology_timer);
15049eff1a38SJesse Larrew }
15059eff1a38SJesse Larrew 
15069eff1a38SJesse Larrew /*
15079eff1a38SJesse Larrew  * Start polling for VPHN associativity changes.
15089eff1a38SJesse Larrew  */
15099eff1a38SJesse Larrew int start_topology_update(void)
15109eff1a38SJesse Larrew {
15119eff1a38SJesse Larrew 	int rc = 0;
15129eff1a38SJesse Larrew 
15139eff1a38SJesse Larrew 	if (firmware_has_feature(FW_FEATURE_VPHN)) {
15149eff1a38SJesse Larrew 		vphn_enabled = 1;
15159eff1a38SJesse Larrew 		setup_cpu_associativity_change_counters();
15169eff1a38SJesse Larrew 		init_timer_deferrable(&topology_timer);
15179eff1a38SJesse Larrew 		set_topology_timer();
15189eff1a38SJesse Larrew 		rc = 1;
15199eff1a38SJesse Larrew 	}
15209eff1a38SJesse Larrew 
15219eff1a38SJesse Larrew 	return rc;
15229eff1a38SJesse Larrew }
15239eff1a38SJesse Larrew __initcall(start_topology_update);
15249eff1a38SJesse Larrew 
15259eff1a38SJesse Larrew /*
15269eff1a38SJesse Larrew  * Disable polling for VPHN associativity changes.
15279eff1a38SJesse Larrew  */
15289eff1a38SJesse Larrew int stop_topology_update(void)
15299eff1a38SJesse Larrew {
15309eff1a38SJesse Larrew 	vphn_enabled = 0;
15319eff1a38SJesse Larrew 	return del_timer_sync(&topology_timer);
15329eff1a38SJesse Larrew }
153339bf990eSJesse Larrew #endif /* CONFIG_PPC_SPLPAR */
1534