blob: 2d13f90476bdde27dd5821ac120fca4fe7108801 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pSeries NUMA support
3 *
4 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/threads.h>
12#include <linux/bootmem.h>
13#include <linux/init.h>
14#include <linux/mm.h>
15#include <linux/mmzone.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/nodemask.h>
18#include <linux/cpu.h>
19#include <linux/notifier.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100020#include <linux/memblock.h>
Michael Ellerman6df16462008-02-14 11:37:49 +110021#include <linux/of.h>
Dave Hansen06eccea2009-02-12 12:36:04 +000022#include <linux/pfn.h>
Jesse Larrew9eff1a32010-12-01 12:31:15 +000023#include <linux/cpuset.h>
24#include <linux/node.h>
Nathan Fontenot30c05352013-04-24 06:02:13 +000025#include <linux/stop_machine.h>
Nathan Fontenote04fa612013-04-24 06:07:39 +000026#include <linux/proc_fs.h>
27#include <linux/seq_file.h>
28#include <linux/uaccess.h>
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110029#include <asm/sparsemem.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080030#include <asm/prom.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110031#include <asm/smp.h>
Jesse Larrew9eff1a32010-12-01 12:31:15 +000032#include <asm/firmware.h>
33#include <asm/paca.h>
Jesse Larrew39bf9902010-12-17 22:07:47 +000034#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010035#include <asm/setup.h>
Jesse Larrew176bbf12013-04-24 06:03:48 +000036#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static int numa_enabled = 1;
39
Balbir Singh1daa6d02008-02-01 15:57:31 +110040static char *cmdline __initdata;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int numa_debug;
43#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
44
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110045int numa_cpu_lookup_table[NR_CPUS];
Anton Blanchard25863de2010-04-26 15:32:43 +000046cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct pglist_data *node_data[MAX_NUMNODES];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110048
49EXPORT_SYMBOL(numa_cpu_lookup_table);
Anton Blanchard25863de2010-04-26 15:32:43 +000050EXPORT_SYMBOL(node_to_cpumask_map);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110051EXPORT_SYMBOL(node_data);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int min_common_depth;
Mike Kravetz237a0982005-12-05 12:06:42 -080054static int n_mem_addr_cells, n_mem_size_cells;
Anton Blanchard41eab6f2010-05-16 20:22:31 +000055static int form1_affinity;
56
57#define MAX_DISTANCE_REF_POINTS 4
58static int distance_ref_points_depth;
59static const unsigned int *distance_ref_points;
60static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Anton Blanchard25863de2010-04-26 15:32:43 +000062/*
63 * Allocate node_to_cpumask_map based on number of available nodes
64 * Requires node_possible_map to be valid.
65 *
Wanlong Gao95129382012-01-12 17:20:09 -080066 * Note: cpumask_of_node() is not valid until after this is done.
Anton Blanchard25863de2010-04-26 15:32:43 +000067 */
68static void __init setup_node_to_cpumask_map(void)
69{
70 unsigned int node, num = 0;
71
72 /* setup nr_node_ids if not done yet */
73 if (nr_node_ids == MAX_NUMNODES) {
74 for_each_node_mask(node, node_possible_map)
75 num = node;
76 nr_node_ids = num + 1;
77 }
78
79 /* allocate the map */
80 for (node = 0; node < nr_node_ids; node++)
81 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
82
83 /* cpumask_of_node() will now work */
84 dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
85}
86
Stephen Rothwell55671f32013-03-25 18:44:44 +000087static int __init fake_numa_create_new_node(unsigned long end_pfn,
Balbir Singh1daa6d02008-02-01 15:57:31 +110088 unsigned int *nid)
89{
90 unsigned long long mem;
91 char *p = cmdline;
92 static unsigned int fake_nid;
93 static unsigned long long curr_boundary;
94
95 /*
96 * Modify node id, iff we started creating NUMA nodes
97 * We want to continue from where we left of the last time
98 */
99 if (fake_nid)
100 *nid = fake_nid;
101 /*
102 * In case there are no more arguments to parse, the
103 * node_id should be the same as the last fake node id
104 * (we've handled this above).
105 */
106 if (!p)
107 return 0;
108
109 mem = memparse(p, &p);
110 if (!mem)
111 return 0;
112
113 if (mem < curr_boundary)
114 return 0;
115
116 curr_boundary = mem;
117
118 if ((end_pfn << PAGE_SHIFT) > mem) {
119 /*
120 * Skip commas and spaces
121 */
122 while (*p == ',' || *p == ' ' || *p == '\t')
123 p++;
124
125 cmdline = p;
126 fake_nid++;
127 *nid = fake_nid;
128 dbg("created new fake_node with id %d\n", fake_nid);
129 return 1;
130 }
131 return 0;
132}
133
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000134/*
Tejun Heo5dfe8662011-07-14 09:46:10 +0200135 * get_node_active_region - Return active region containing pfn
Jon Tollefsone8170372008-10-16 18:59:43 +0000136 * Active range returned is empty if none found.
Tejun Heo5dfe8662011-07-14 09:46:10 +0200137 * @pfn: The page to return the region for
138 * @node_ar: Returned set to the active region containing @pfn
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000139 */
Tejun Heo5dfe8662011-07-14 09:46:10 +0200140static void __init get_node_active_region(unsigned long pfn,
141 struct node_active_region *node_ar)
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000142{
Tejun Heo5dfe8662011-07-14 09:46:10 +0200143 unsigned long start_pfn, end_pfn;
144 int i, nid;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000145
Tejun Heo5dfe8662011-07-14 09:46:10 +0200146 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
147 if (pfn >= start_pfn && pfn < end_pfn) {
148 node_ar->nid = nid;
149 node_ar->start_pfn = start_pfn;
150 node_ar->end_pfn = end_pfn;
151 break;
152 }
153 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000154}
155
Jesse Larrew39bf9902010-12-17 22:07:47 +0000156static void map_cpu_to_node(int cpu, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 numa_cpu_lookup_table[cpu] = node;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100159
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600160 dbg("adding cpu %d to node %d\n", cpu, node);
161
Anton Blanchard25863de2010-04-26 15:32:43 +0000162 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
163 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jesse Larrew39bf9902010-12-17 22:07:47 +0000166#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void unmap_cpu_from_node(unsigned long cpu)
168{
169 int node = numa_cpu_lookup_table[cpu];
170
171 dbg("removing cpu %lu from node %d\n", cpu, node);
172
Anton Blanchard25863de2010-04-26 15:32:43 +0000173 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
Anton Blanchard429f4d82011-01-29 12:37:16 +0000174 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } else {
176 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
177 cpu, node);
178 }
179}
Jesse Larrew39bf9902010-12-17 22:07:47 +0000180#endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* must hold reference to node during call */
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000183static const int *of_get_associativity(struct device_node *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000185 return of_get_property(dev, "ibm,associativity", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Chandrucf000852008-08-30 00:28:16 +1000188/*
189 * Returns the property linux,drconf-usable-memory if
190 * it exists (the property exists only in kexec/kdump kernels,
191 * added by kexec-tools)
192 */
193static const u32 *of_get_usable_memory(struct device_node *memory)
194{
195 const u32 *prop;
196 u32 len;
197 prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
198 if (!prop || len < sizeof(unsigned int))
199 return 0;
200 return prop;
201}
202
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000203int __node_distance(int a, int b)
204{
205 int i;
206 int distance = LOCAL_DISTANCE;
207
208 if (!form1_affinity)
Vaidyanathan Srinivasan7122bee2013-03-22 05:49:35 +0000209 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000210
211 for (i = 0; i < distance_ref_points_depth; i++) {
212 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
213 break;
214
215 /* Double the distance for each NUMA level */
216 distance *= 2;
217 }
218
219 return distance;
220}
221
222static void initialize_distance_lookup_table(int nid,
223 const unsigned int *associativity)
224{
225 int i;
226
227 if (!form1_affinity)
228 return;
229
230 for (i = 0; i < distance_ref_points_depth; i++) {
231 distance_lookup_table[nid][i] =
232 associativity[distance_ref_points[i]];
233 }
234}
235
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600236/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
237 * info is found.
238 */
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000239static int associativity_to_nid(const unsigned int *associativity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600241 int nid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (min_common_depth == -1)
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000246 if (associativity[0] >= min_common_depth)
247 nid = associativity[min_common_depth];
Nathan Lynchbc16a752006-03-20 18:36:15 -0600248
249 /* POWER4 LPAR uses 0xffff as invalid node */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600250 if (nid == 0xffff || nid >= MAX_NUMNODES)
251 nid = -1;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000252
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000253 if (nid > 0 && associativity[0] >= distance_ref_points_depth)
254 initialize_distance_lookup_table(nid, associativity);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000255
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600256out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600257 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000260/* Returns the nid associated with the given device tree node,
261 * or -1 if not found.
262 */
263static int of_node_to_nid_single(struct device_node *device)
264{
265 int nid = -1;
266 const unsigned int *tmp;
267
268 tmp = of_get_associativity(device);
269 if (tmp)
270 nid = associativity_to_nid(tmp);
271 return nid;
272}
273
Jeremy Kerr953039c2006-05-01 12:16:12 -0700274/* Walk the device tree upwards, looking for an associativity id */
275int of_node_to_nid(struct device_node *device)
276{
277 struct device_node *tmp;
278 int nid = -1;
279
280 of_node_get(device);
281 while (device) {
282 nid = of_node_to_nid_single(device);
283 if (nid != -1)
284 break;
285
286 tmp = device;
287 device = of_get_parent(tmp);
288 of_node_put(tmp);
289 }
290 of_node_put(device);
291
292 return nid;
293}
294EXPORT_SYMBOL_GPL(of_node_to_nid);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int __init find_min_common_depth(void)
297{
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000298 int depth;
Michael Ellermane70606e2011-04-10 20:42:05 +0000299 struct device_node *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000301 if (firmware_has_feature(FW_FEATURE_OPAL))
302 root = of_find_node_by_path("/ibm,opal");
303 else
304 root = of_find_node_by_path("/rtas");
Michael Ellermane70606e2011-04-10 20:42:05 +0000305 if (!root)
306 root = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000309 * This property is a set of 32-bit integers, each representing
310 * an index into the ibm,associativity nodes.
311 *
312 * With form 0 affinity the first integer is for an SMP configuration
313 * (should be all 0's) and the second is for a normal NUMA
314 * configuration. We have only one level of NUMA.
315 *
316 * With form 1 affinity the first integer is the most significant
317 * NUMA boundary and the following are progressively less significant
318 * boundaries. There can be more than one level of NUMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Michael Ellermane70606e2011-04-10 20:42:05 +0000320 distance_ref_points = of_get_property(root,
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000321 "ibm,associativity-reference-points",
322 &distance_ref_points_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000324 if (!distance_ref_points) {
325 dbg("NUMA: ibm,associativity-reference-points not found.\n");
326 goto err;
327 }
328
329 distance_ref_points_depth /= sizeof(int);
330
Nathan Fontenot8002b0c2013-04-24 05:58:23 +0000331 if (firmware_has_feature(FW_FEATURE_OPAL) ||
332 firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
333 dbg("Using form 1 affinity\n");
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000334 form1_affinity = 1;
Anton Blanchard4b83c332010-04-07 15:33:44 +0000335 }
336
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000337 if (form1_affinity) {
338 depth = distance_ref_points[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else {
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000340 if (distance_ref_points_depth < 2) {
341 printk(KERN_WARNING "NUMA: "
342 "short ibm,associativity-reference-points\n");
343 goto err;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000346 depth = distance_ref_points[1];
347 }
348
349 /*
350 * Warn and cap if the hardware supports more than
351 * MAX_DISTANCE_REF_POINTS domains.
352 */
353 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
354 printk(KERN_WARNING "NUMA: distance array capped at "
355 "%d entries\n", MAX_DISTANCE_REF_POINTS);
356 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
357 }
358
Michael Ellermane70606e2011-04-10 20:42:05 +0000359 of_node_put(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return depth;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000361
362err:
Michael Ellermane70606e2011-04-10 20:42:05 +0000363 of_node_put(root);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000364 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800367static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct device_node *memory = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 memory = of_find_node_by_type(memory, "memory");
Paul Mackerras54c23312005-12-05 15:50:39 +1100372 if (!memory)
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800373 panic("numa.c: No memory nodes found!");
Paul Mackerras54c23312005-12-05 15:50:39 +1100374
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000375 *n_addr_cells = of_n_addr_cells(memory);
Stephen Rothwell9213fee2007-04-03 10:57:48 +1000376 *n_size_cells = of_n_size_cells(memory);
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800377 of_node_put(memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
David Rientjes2011b1d2011-12-08 12:46:37 +0000380static unsigned long read_n_cells(int n, const unsigned int **buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 unsigned long result = 0;
383
384 while (n--) {
385 result = (result << 32) | **buf;
386 (*buf)++;
387 }
388 return result;
389}
390
Nathan Fontenot83426812008-07-03 13:35:54 +1000391/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000392 * Read the next memblock list entry from the ibm,dynamic-memory property
Nathan Fontenot83426812008-07-03 13:35:54 +1000393 * and return the information in the provided of_drconf_cell structure.
394 */
395static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
396{
397 const u32 *cp;
398
399 drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
400
401 cp = *cellp;
402 drmem->drc_index = cp[0];
403 drmem->reserved = cp[1];
404 drmem->aa_index = cp[2];
405 drmem->flags = cp[3];
406
407 *cellp = cp + 4;
408}
409
410/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300411 * Retrieve and validate the ibm,dynamic-memory property of the device tree.
Nathan Fontenot83426812008-07-03 13:35:54 +1000412 *
Yinghai Lu95f72d12010-07-12 14:36:09 +1000413 * The layout of the ibm,dynamic-memory property is a number N of memblock
414 * list entries followed by N memblock list entries. Each memblock list entry
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300415 * contains information as laid out in the of_drconf_cell struct above.
Nathan Fontenot83426812008-07-03 13:35:54 +1000416 */
417static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
418{
419 const u32 *prop;
420 u32 len, entries;
421
422 prop = of_get_property(memory, "ibm,dynamic-memory", &len);
423 if (!prop || len < sizeof(unsigned int))
424 return 0;
425
426 entries = *prop++;
427
428 /* Now that we know the number of entries, revalidate the size
429 * of the property read in to ensure we have everything
430 */
431 if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
432 return 0;
433
434 *dm = prop;
435 return entries;
436}
437
438/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300439 * Retrieve and validate the ibm,lmb-size property for drconf memory
Nathan Fontenot83426812008-07-03 13:35:54 +1000440 * from the device tree.
441 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000442static u64 of_get_lmb_size(struct device_node *memory)
Nathan Fontenot83426812008-07-03 13:35:54 +1000443{
444 const u32 *prop;
445 u32 len;
446
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000447 prop = of_get_property(memory, "ibm,lmb-size", &len);
Nathan Fontenot83426812008-07-03 13:35:54 +1000448 if (!prop || len < sizeof(unsigned int))
449 return 0;
450
451 return read_n_cells(n_mem_size_cells, &prop);
452}
453
454struct assoc_arrays {
455 u32 n_arrays;
456 u32 array_sz;
457 const u32 *arrays;
458};
459
460/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300461 * Retrieve and validate the list of associativity arrays for drconf
Nathan Fontenot83426812008-07-03 13:35:54 +1000462 * memory from the ibm,associativity-lookup-arrays property of the
463 * device tree..
464 *
465 * The layout of the ibm,associativity-lookup-arrays property is a number N
466 * indicating the number of associativity arrays, followed by a number M
467 * indicating the size of each associativity array, followed by a list
468 * of N associativity arrays.
469 */
470static int of_get_assoc_arrays(struct device_node *memory,
471 struct assoc_arrays *aa)
472{
473 const u32 *prop;
474 u32 len;
475
476 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
477 if (!prop || len < 2 * sizeof(unsigned int))
478 return -1;
479
480 aa->n_arrays = *prop++;
481 aa->array_sz = *prop++;
482
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800483 /* Now that we know the number of arrays and size of each array,
Nathan Fontenot83426812008-07-03 13:35:54 +1000484 * revalidate the size of the property read in.
485 */
486 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
487 return -1;
488
489 aa->arrays = prop;
490 return 0;
491}
492
493/*
494 * This is like of_node_to_nid_single() for memory represented in the
495 * ibm,dynamic-reconfiguration-memory node.
496 */
497static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
498 struct assoc_arrays *aa)
499{
500 int default_nid = 0;
501 int nid = default_nid;
502 int index;
503
504 if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
505 !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
506 drmem->aa_index < aa->n_arrays) {
507 index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
508 nid = aa->arrays[index];
509
510 if (nid == 0xffff || nid >= MAX_NUMNODES)
511 nid = default_nid;
512 }
513
514 return nid;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * Figure out to which domain a cpu belongs and stick it there.
519 * Return the id of the domain used.
520 */
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600521static int __cpuinit numa_setup_cpu(unsigned long lcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Nathan Lynchcf950b72006-03-20 18:35:45 -0600523 int nid = 0;
Milton Miller8b16cd22009-01-08 02:19:45 +0000524 struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (!cpu) {
527 WARN_ON(1);
528 goto out;
529 }
530
Jeremy Kerr953039c2006-05-01 12:16:12 -0700531 nid = of_node_to_nid_single(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600533 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -0800534 nid = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600536 map_cpu_to_node(lcpu, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 of_node_put(cpu);
539
Nathan Lynchcf950b72006-03-20 18:35:45 -0600540 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700543static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned long action,
545 void *hcpu)
546{
547 unsigned long lcpu = (unsigned long)hcpu;
548 int ret = NOTIFY_DONE;
549
550 switch (action) {
551 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700552 case CPU_UP_PREPARE_FROZEN:
Nathan Lynch2b261222006-03-20 18:37:15 -0600553 numa_setup_cpu(lcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ret = NOTIFY_OK;
555 break;
556#ifdef CONFIG_HOTPLUG_CPU
557 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700558 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700560 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unmap_cpu_from_node(lcpu);
562 break;
563 ret = NOTIFY_OK;
564#endif
565 }
566 return ret;
567}
568
569/*
570 * Check and possibly modify a memory region to enforce the memory limit.
571 *
572 * Returns the size the region should have to enforce the memory limit.
573 * This will either be the original value of size, a truncated value,
574 * or zero. If the returned value of size is 0 the region should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300575 * discarded as it lies wholly above the memory limit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100577static unsigned long __init numa_enforce_memory_limit(unsigned long start,
578 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000581 * We use memblock_end_of_DRAM() in here instead of memory_limit because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * we've already adjusted it for the limit and it takes care of
Milton Millerfe552492008-10-20 15:37:04 +0000583 * having memory holes below the limit. Also, in the case of
584 * iommu_is_off, memory_limit is not set but is implicitly enforced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Yinghai Lu95f72d12010-07-12 14:36:09 +1000587 if (start + size <= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return size;
589
Yinghai Lu95f72d12010-07-12 14:36:09 +1000590 if (start >= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592
Yinghai Lu95f72d12010-07-12 14:36:09 +1000593 return memblock_end_of_DRAM() - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Paul Mackerras02045682006-11-29 22:27:42 +1100596/*
Chandrucf000852008-08-30 00:28:16 +1000597 * Reads the counter for a given entry in
598 * linux,drconf-usable-memory property
599 */
600static inline int __init read_usm_ranges(const u32 **usm)
601{
602 /*
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000603 * For each lmb in ibm,dynamic-memory a corresponding
Chandrucf000852008-08-30 00:28:16 +1000604 * entry in linux,drconf-usable-memory property contains
605 * a counter followed by that many (base, size) duple.
606 * read the counter from linux,drconf-usable-memory
607 */
608 return read_n_cells(n_mem_size_cells, usm);
609}
610
611/*
Paul Mackerras02045682006-11-29 22:27:42 +1100612 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
613 * node. This assumes n_mem_{addr,size}_cells have been set.
614 */
615static void __init parse_drconf_memory(struct device_node *memory)
616{
Michael Neuling82b25212012-06-19 20:01:45 +0000617 const u32 *uninitialized_var(dm), *usm;
Chandrucf000852008-08-30 00:28:16 +1000618 unsigned int n, rc, ranges, is_kexec_kdump = 0;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000619 unsigned long lmb_size, base, size, sz;
Nathan Fontenot83426812008-07-03 13:35:54 +1000620 int nid;
Benjamin Herrenschmidtaa709f32012-07-05 16:30:33 +0000621 struct assoc_arrays aa = { .arrays = NULL };
Paul Mackerras02045682006-11-29 22:27:42 +1100622
Nathan Fontenot83426812008-07-03 13:35:54 +1000623 n = of_get_drconf_memory(memory, &dm);
624 if (!n)
Paul Mackerras02045682006-11-29 22:27:42 +1100625 return;
626
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000627 lmb_size = of_get_lmb_size(memory);
628 if (!lmb_size)
Nathan Fontenot83426812008-07-03 13:35:54 +1000629 return;
630
631 rc = of_get_assoc_arrays(memory, &aa);
632 if (rc)
Paul Mackerras02045682006-11-29 22:27:42 +1100633 return;
634
Chandrucf000852008-08-30 00:28:16 +1000635 /* check if this is a kexec/kdump kernel */
636 usm = of_get_usable_memory(memory);
637 if (usm != NULL)
638 is_kexec_kdump = 1;
639
Paul Mackerras02045682006-11-29 22:27:42 +1100640 for (; n != 0; --n) {
Nathan Fontenot83426812008-07-03 13:35:54 +1000641 struct of_drconf_cell drmem;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100642
Nathan Fontenot83426812008-07-03 13:35:54 +1000643 read_drconf_cell(&drmem, &dm);
644
645 /* skip this block if the reserved bit is set in flags (0x80)
646 or if the block is not assigned to this partition (0x8) */
647 if ((drmem.flags & DRCONF_MEM_RESERVED)
648 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
649 continue;
650
Chandrucf000852008-08-30 00:28:16 +1000651 base = drmem.base_addr;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000652 size = lmb_size;
Chandrucf000852008-08-30 00:28:16 +1000653 ranges = 1;
Nathan Fontenot83426812008-07-03 13:35:54 +1000654
Chandrucf000852008-08-30 00:28:16 +1000655 if (is_kexec_kdump) {
656 ranges = read_usm_ranges(&usm);
657 if (!ranges) /* there are no (base, size) duple */
658 continue;
659 }
660 do {
661 if (is_kexec_kdump) {
662 base = read_n_cells(n_mem_addr_cells, &usm);
663 size = read_n_cells(n_mem_size_cells, &usm);
664 }
665 nid = of_drconf_to_nid_single(&drmem, &aa);
666 fake_numa_create_new_node(
667 ((base + size) >> PAGE_SHIFT),
Nathan Fontenot83426812008-07-03 13:35:54 +1000668 &nid);
Chandrucf000852008-08-30 00:28:16 +1000669 node_set_online(nid);
670 sz = numa_enforce_memory_limit(base, size);
671 if (sz)
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800672 memblock_set_node(base, sz, nid);
Chandrucf000852008-08-30 00:28:16 +1000673 } while (--ranges);
Paul Mackerras02045682006-11-29 22:27:42 +1100674 }
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static int __init parse_numa_properties(void)
678{
Anton Blanchard94db7c52011-08-10 20:44:22 +0000679 struct device_node *memory;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600680 int default_nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned long i;
682
683 if (numa_enabled == 0) {
684 printk(KERN_WARNING "NUMA disabled by user\n");
685 return -1;
686 }
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 min_common_depth = find_min_common_depth();
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (min_common_depth < 0)
691 return min_common_depth;
692
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600693 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600696 * Even though we connect cpus to numa domains later in SMP
697 * init, we need to know the node ids now. This is because
698 * each node to be onlined must have NODE_DATA etc backing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600700 for_each_present_cpu(i) {
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000701 struct device_node *cpu;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600702 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Milton Miller8b16cd22009-01-08 02:19:45 +0000704 cpu = of_get_cpu_node(i, NULL);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600705 BUG_ON(!cpu);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700706 nid = of_node_to_nid_single(cpu);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600707 of_node_put(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600709 /*
710 * Don't fall back to default_nid yet -- we will plug
711 * cpus into nodes once the memory scan has discovered
712 * the topology.
713 */
714 if (nid < 0)
715 continue;
716 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Mike Kravetz237a0982005-12-05 12:06:42 -0800719 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
Anton Blanchard94db7c52011-08-10 20:44:22 +0000720
721 for_each_node_by_type(memory, "memory") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned long start;
723 unsigned long size;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600724 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000726 const unsigned int *memcell_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned int len;
728
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000729 memcell_buf = of_get_property(memory,
Michael Ellermanba759482005-12-04 18:39:55 +1100730 "linux,usable-memory", &len);
731 if (!memcell_buf || len <= 0)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000732 memcell_buf = of_get_property(memory, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!memcell_buf || len <= 0)
734 continue;
735
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100736 /* ranges in cell */
737 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738new_range:
739 /* these are order-sensitive, and modify the buffer pointer */
Mike Kravetz237a0982005-12-05 12:06:42 -0800740 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
741 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600743 /*
744 * Assumption: either all memory nodes or none will
745 * have associativity properties. If none, then
746 * everything goes to default_nid.
747 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700748 nid = of_node_to_nid_single(memory);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600749 if (nid < 0)
750 nid = default_nid;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100751
752 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600753 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100755 if (!(size = numa_enforce_memory_limit(start, size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (--ranges)
757 goto new_range;
758 else
759 continue;
760 }
761
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800762 memblock_set_node(start, size, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (--ranges)
765 goto new_range;
766 }
767
Paul Mackerras02045682006-11-29 22:27:42 +1100768 /*
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000769 * Now do the same thing for each MEMBLOCK listed in the
770 * ibm,dynamic-memory property in the
771 * ibm,dynamic-reconfiguration-memory node.
Paul Mackerras02045682006-11-29 22:27:42 +1100772 */
773 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
774 if (memory)
775 parse_drconf_memory(memory);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778}
779
780static void __init setup_nonnuma(void)
781{
Yinghai Lu95f72d12010-07-12 14:36:09 +1000782 unsigned long top_of_ram = memblock_end_of_DRAM();
783 unsigned long total_ram = memblock_phys_mem_size();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700784 unsigned long start_pfn, end_pfn;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000785 unsigned int nid = 0;
786 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Olof Johanssone110b282006-04-12 15:25:01 -0500788 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500790 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 (top_of_ram - total_ram) >> 20);
792
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000793 for_each_memblock(memory, reg) {
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700794 start_pfn = memblock_region_memory_base_pfn(reg);
795 end_pfn = memblock_region_memory_end_pfn(reg);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100796
797 fake_numa_create_new_node(end_pfn, &nid);
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800798 memblock_set_node(PFN_PHYS(start_pfn),
799 PFN_PHYS(end_pfn - start_pfn), nid);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100800 node_set_online(nid);
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Anton Blanchard4b703a22005-12-13 06:56:47 +1100804void __init dump_numa_cpu_topology(void)
805{
806 unsigned int node;
807 unsigned int cpu, count;
808
809 if (min_common_depth == -1 || !numa_enabled)
810 return;
811
812 for_each_online_node(node) {
Olof Johanssone110b282006-04-12 15:25:01 -0500813 printk(KERN_DEBUG "Node %d CPUs:", node);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100814
815 count = 0;
816 /*
817 * If we used a CPU iterator here we would miss printing
818 * the holes in the cpumap.
819 */
Anton Blanchard25863de2010-04-26 15:32:43 +0000820 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
821 if (cpumask_test_cpu(cpu,
822 node_to_cpumask_map[node])) {
Anton Blanchard4b703a22005-12-13 06:56:47 +1100823 if (count == 0)
824 printk(" %u", cpu);
825 ++count;
826 } else {
827 if (count > 1)
828 printk("-%u", cpu - 1);
829 count = 0;
830 }
831 }
832
833 if (count > 1)
Anton Blanchard25863de2010-04-26 15:32:43 +0000834 printk("-%u", nr_cpu_ids - 1);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100835 printk("\n");
836 }
837}
838
839static void __init dump_numa_memory_topology(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 unsigned int node;
842 unsigned int count;
843
844 if (min_common_depth == -1 || !numa_enabled)
845 return;
846
847 for_each_online_node(node) {
848 unsigned long i;
849
Olof Johanssone110b282006-04-12 15:25:01 -0500850 printk(KERN_DEBUG "Node %d Memory:", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 count = 0;
853
Yinghai Lu95f72d12010-07-12 14:36:09 +1000854 for (i = 0; i < memblock_end_of_DRAM();
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100855 i += (1 << SECTION_SIZE_BITS)) {
856 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (count == 0)
858 printk(" 0x%lx", i);
859 ++count;
860 } else {
861 if (count > 0)
862 printk("-0x%lx", i);
863 count = 0;
864 }
865 }
866
867 if (count > 0)
868 printk("-0x%lx", i);
869 printk("\n");
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000874 * Allocate some memory, satisfying the memblock or bootmem allocator where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * required. nid is the preferred node and end is the physical address of
876 * the highest address in the node.
877 *
Dave Hansen0be210f2008-12-09 08:21:35 +0000878 * Returns the virtual address of the memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Dave Hansen893473d2008-12-09 08:21:36 +0000880static void __init *careful_zallocation(int nid, unsigned long size,
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100881 unsigned long align,
882 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Dave Hansen0be210f2008-12-09 08:21:35 +0000884 void *ret;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100885 int new_nid;
Dave Hansen0be210f2008-12-09 08:21:35 +0000886 unsigned long ret_paddr;
887
Yinghai Lu95f72d12010-07-12 14:36:09 +1000888 ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* retry over all memory */
Dave Hansen0be210f2008-12-09 08:21:35 +0000891 if (!ret_paddr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000892 ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dave Hansen0be210f2008-12-09 08:21:35 +0000894 if (!ret_paddr)
Dave Hansen5d21ea22008-12-09 08:21:33 +0000895 panic("numa.c: cannot allocate %lu bytes for node %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 size, nid);
897
Dave Hansen0be210f2008-12-09 08:21:35 +0000898 ret = __va(ret_paddr);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
Dave Hansenc555e522008-12-09 08:21:32 +0000901 * We initialize the nodes in numeric order: 0, 1, 2...
Yinghai Lu95f72d12010-07-12 14:36:09 +1000902 * and hand over control from the MEMBLOCK allocator to the
Dave Hansenc555e522008-12-09 08:21:32 +0000903 * bootmem allocator. If this function is called for
904 * node 5, then we know that all nodes <5 are using the
Yinghai Lu95f72d12010-07-12 14:36:09 +1000905 * bootmem allocator instead of the MEMBLOCK allocator.
Dave Hansenc555e522008-12-09 08:21:32 +0000906 *
907 * So, check the nid from which this allocation came
908 * and double check to see if we need to use bootmem
Yinghai Lu95f72d12010-07-12 14:36:09 +1000909 * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
Dave Hansenc555e522008-12-09 08:21:32 +0000910 * since it would be useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Dave Hansen0be210f2008-12-09 08:21:35 +0000912 new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100913 if (new_nid < nid) {
Dave Hansen0be210f2008-12-09 08:21:35 +0000914 ret = __alloc_bootmem_node(NODE_DATA(new_nid),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 size, align, 0);
916
Dave Hansen0be210f2008-12-09 08:21:35 +0000917 dbg("alloc_bootmem %p %lx\n", ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Dave Hansen893473d2008-12-09 08:21:36 +0000920 memset(ret, 0, size);
Dave Hansen0be210f2008-12-09 08:21:35 +0000921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700924static struct notifier_block __cpuinitdata ppc64_numa_nb = {
925 .notifier_call = cpu_numa_callback,
926 .priority = 1 /* Must run before sched domains notifier. */
927};
928
David Rientjes28e86bd2011-12-08 12:33:29 +0000929static void __init mark_reserved_regions_for_nid(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Dave Hansen4a618662008-11-24 12:02:35 +0000931 struct pglist_data *node = NODE_DATA(nid);
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000932 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000934 for_each_memblock(reserved, reg) {
935 unsigned long physbase = reg->base;
936 unsigned long size = reg->size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000937 unsigned long start_pfn = physbase >> PAGE_SHIFT;
Dave Hansen06eccea2009-02-12 12:36:04 +0000938 unsigned long end_pfn = PFN_UP(physbase + size);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000939 struct node_active_region node_ar;
Dave Hansen4a618662008-11-24 12:02:35 +0000940 unsigned long node_end_pfn = node->node_start_pfn +
941 node->node_spanned_pages;
942
943 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000944 * Check to make sure that this memblock.reserved area is
Dave Hansen4a618662008-11-24 12:02:35 +0000945 * within the bounds of the node that we care about.
946 * Checking the nid of the start and end points is not
947 * sufficient because the reserved area could span the
948 * entire node.
949 */
950 if (end_pfn <= node->node_start_pfn ||
951 start_pfn >= node_end_pfn)
952 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000954 get_node_active_region(start_pfn, &node_ar);
Jon Tollefsone8170372008-10-16 18:59:43 +0000955 while (start_pfn < end_pfn &&
956 node_ar.start_pfn < node_ar.end_pfn) {
957 unsigned long reserve_size = size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000958 /*
959 * if reserved region extends past active region
960 * then trim size to active region
961 */
962 if (end_pfn > node_ar.end_pfn)
Jon Tollefsone8170372008-10-16 18:59:43 +0000963 reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
Dave Hansen06eccea2009-02-12 12:36:04 +0000964 - physbase;
Dave Hansena4c74dd2008-12-11 08:36:06 +0000965 /*
966 * Only worry about *this* node, others may not
967 * yet have valid NODE_DATA().
968 */
969 if (node_ar.nid == nid) {
970 dbg("reserve_bootmem %lx %lx nid=%d\n",
971 physbase, reserve_size, node_ar.nid);
972 reserve_bootmem_node(NODE_DATA(node_ar.nid),
973 physbase, reserve_size,
974 BOOTMEM_DEFAULT);
975 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000976 /*
977 * if reserved region is contained in the active region
978 * then done.
979 */
980 if (end_pfn <= node_ar.end_pfn)
981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000983 /*
984 * reserved region extends past the active region
985 * get next active region that contains this
986 * reserved region
987 */
988 start_pfn = node_ar.end_pfn;
989 physbase = start_pfn << PAGE_SHIFT;
Jon Tollefsone8170372008-10-16 18:59:43 +0000990 size = size - reserve_size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000991 get_node_active_region(start_pfn, &node_ar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Dave Hansen4a618662008-11-24 12:02:35 +0000994}
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000995
Dave Hansen4a618662008-11-24 12:02:35 +0000996
997void __init do_init_bootmem(void)
998{
999 int nid;
Dave Hansen4a618662008-11-24 12:02:35 +00001000
1001 min_low_pfn = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001002 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Dave Hansen4a618662008-11-24 12:02:35 +00001003 max_pfn = max_low_pfn;
1004
1005 if (parse_numa_properties())
1006 setup_nonnuma();
1007 else
1008 dump_numa_memory_topology();
1009
Dave Hansen4a618662008-11-24 12:02:35 +00001010 for_each_online_node(nid) {
1011 unsigned long start_pfn, end_pfn;
Dave Hansen0be210f2008-12-09 08:21:35 +00001012 void *bootmem_vaddr;
Dave Hansen4a618662008-11-24 12:02:35 +00001013 unsigned long bootmap_pages;
1014
1015 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1016
1017 /*
1018 * Allocate the node structure node local if possible
1019 *
1020 * Be careful moving this around, as it relies on all
1021 * previous nodes' bootmem to be initialized and have
1022 * all reserved areas marked.
1023 */
Dave Hansen893473d2008-12-09 08:21:36 +00001024 NODE_DATA(nid) = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001025 sizeof(struct pglist_data),
1026 SMP_CACHE_BYTES, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001027
1028 dbg("node %d\n", nid);
1029 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
1030
1031 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
1032 NODE_DATA(nid)->node_start_pfn = start_pfn;
1033 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
1034
1035 if (NODE_DATA(nid)->node_spanned_pages == 0)
1036 continue;
1037
1038 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
1039 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
1040
1041 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
Dave Hansen893473d2008-12-09 08:21:36 +00001042 bootmem_vaddr = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001043 bootmap_pages << PAGE_SHIFT,
1044 PAGE_SIZE, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001045
Dave Hansen0be210f2008-12-09 08:21:35 +00001046 dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
Dave Hansen4a618662008-11-24 12:02:35 +00001047
Dave Hansen0be210f2008-12-09 08:21:35 +00001048 init_bootmem_node(NODE_DATA(nid),
1049 __pa(bootmem_vaddr) >> PAGE_SHIFT,
Dave Hansen4a618662008-11-24 12:02:35 +00001050 start_pfn, end_pfn);
1051
1052 free_bootmem_with_active_regions(nid, end_pfn);
1053 /*
1054 * Be very careful about moving this around. Future
Dave Hansen893473d2008-12-09 08:21:36 +00001055 * calls to careful_zallocation() depend on this getting
Dave Hansen4a618662008-11-24 12:02:35 +00001056 * done correctly.
1057 */
1058 mark_reserved_regions_for_nid(nid);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +00001059 sparse_memory_present_with_active_regions(nid);
Dave Hansen4a618662008-11-24 12:02:35 +00001060 }
Benjamin Herrenschmidtd3f62042009-06-02 21:16:38 +00001061
1062 init_bootmem_done = 1;
Anton Blanchard25863de2010-04-26 15:32:43 +00001063
1064 /*
1065 * Now bootmem is initialised we can create the node to cpumask
1066 * lookup tables and setup the cpu callback to populate them.
1067 */
1068 setup_node_to_cpumask_map();
1069
1070 register_cpu_notifier(&ppc64_numa_nb);
1071 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
1072 (void *)(unsigned long)boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075void __init paging_init(void)
1076{
Mel Gorman6391af12006-10-11 01:20:39 -07001077 unsigned long max_zone_pfns[MAX_NR_ZONES];
1078 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Yinghai Lu95f72d12010-07-12 14:36:09 +10001079 max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
Mel Gormanc67c3cb2006-09-27 01:49:49 -07001080 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083static int __init early_numa(char *p)
1084{
1085 if (!p)
1086 return 0;
1087
1088 if (strstr(p, "off"))
1089 numa_enabled = 0;
1090
1091 if (strstr(p, "debug"))
1092 numa_debug = 1;
1093
Balbir Singh1daa6d02008-02-01 15:57:31 +11001094 p = strstr(p, "fake=");
1095 if (p)
1096 cmdline = p + strlen("fake=");
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100early_param("numa", early_numa);
Mike Kravetz237a0982005-12-05 12:06:42 -08001101
1102#ifdef CONFIG_MEMORY_HOTPLUG
1103/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001104 * Find the node associated with a hot added memory section for
1105 * memory represented in the device tree by the property
1106 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
Nathan Fontenot0db93602008-07-03 13:25:08 +10001107 */
1108static int hot_add_drconf_scn_to_nid(struct device_node *memory,
1109 unsigned long scn_addr)
1110{
1111 const u32 *dm;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001112 unsigned int drconf_cell_cnt, rc;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001113 unsigned long lmb_size;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001114 struct assoc_arrays aa;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001115 int nid = -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001116
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001117 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1118 if (!drconf_cell_cnt)
1119 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001120
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001121 lmb_size = of_get_lmb_size(memory);
1122 if (!lmb_size)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001123 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001124
1125 rc = of_get_assoc_arrays(memory, &aa);
1126 if (rc)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001127 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001128
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001129 for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
Nathan Fontenot0db93602008-07-03 13:25:08 +10001130 struct of_drconf_cell drmem;
1131
1132 read_drconf_cell(&drmem, &dm);
1133
1134 /* skip this block if it is reserved or not assigned to
1135 * this partition */
1136 if ((drmem.flags & DRCONF_MEM_RESERVED)
1137 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
1138 continue;
1139
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001140 if ((scn_addr < drmem.base_addr)
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001141 || (scn_addr >= (drmem.base_addr + lmb_size)))
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001142 continue;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001143
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001144 nid = of_drconf_to_nid_single(&drmem, &aa);
1145 break;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001146 }
1147
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001148 return nid;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001149}
1150
1151/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001152 * Find the node associated with a hot added memory section for memory
1153 * represented in the device tree as a node (i.e. memory@XXXX) for
Yinghai Lu95f72d12010-07-12 14:36:09 +10001154 * each memblock.
Mike Kravetz237a0982005-12-05 12:06:42 -08001155 */
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001156int hot_add_node_scn_to_nid(unsigned long scn_addr)
Mike Kravetz237a0982005-12-05 12:06:42 -08001157{
Anton Blanchard94db7c52011-08-10 20:44:22 +00001158 struct device_node *memory;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001159 int nid = -1;
Mike Kravetz237a0982005-12-05 12:06:42 -08001160
Anton Blanchard94db7c52011-08-10 20:44:22 +00001161 for_each_node_by_type(memory, "memory") {
Mike Kravetz237a0982005-12-05 12:06:42 -08001162 unsigned long start, size;
Mike Kravetzb226e462005-12-16 14:30:35 -08001163 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001164 const unsigned int *memcell_buf;
Mike Kravetz237a0982005-12-05 12:06:42 -08001165 unsigned int len;
1166
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001167 memcell_buf = of_get_property(memory, "reg", &len);
Mike Kravetz237a0982005-12-05 12:06:42 -08001168 if (!memcell_buf || len <= 0)
1169 continue;
1170
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001171 /* ranges in cell */
1172 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Mike Kravetz237a0982005-12-05 12:06:42 -08001173
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001174 while (ranges--) {
1175 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1176 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1177
1178 if ((scn_addr < start) || (scn_addr >= (start + size)))
1179 continue;
1180
1181 nid = of_node_to_nid_single(memory);
1182 break;
Mike Kravetz237a0982005-12-05 12:06:42 -08001183 }
1184
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001185 if (nid >= 0)
1186 break;
Mike Kravetz237a0982005-12-05 12:06:42 -08001187 }
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001188
Anton Blanchard60831842011-08-10 20:44:21 +00001189 of_node_put(memory);
1190
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001191 return nid;
Mike Kravetz237a0982005-12-05 12:06:42 -08001192}
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001193
1194/*
1195 * Find the node associated with a hot added memory section. Section
Yinghai Lu95f72d12010-07-12 14:36:09 +10001196 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1197 * sections are fully contained within a single MEMBLOCK.
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001198 */
1199int hot_add_scn_to_nid(unsigned long scn_addr)
1200{
1201 struct device_node *memory = NULL;
1202 int nid, found = 0;
1203
1204 if (!numa_enabled || (min_common_depth < 0))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001205 return first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001206
1207 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1208 if (memory) {
1209 nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
1210 of_node_put(memory);
1211 } else {
1212 nid = hot_add_node_scn_to_nid(scn_addr);
1213 }
1214
1215 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001216 nid = first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001217
1218 if (NODE_DATA(nid)->node_spanned_pages)
1219 return nid;
1220
1221 for_each_online_node(nid) {
1222 if (NODE_DATA(nid)->node_spanned_pages) {
1223 found = 1;
1224 break;
1225 }
1226 }
1227
1228 BUG_ON(!found);
1229 return nid;
1230}
1231
Nishanth Aravamudancd342062010-10-26 17:35:12 +00001232static u64 hot_add_drconf_memory_max(void)
1233{
1234 struct device_node *memory = NULL;
1235 unsigned int drconf_cell_cnt = 0;
1236 u64 lmb_size = 0;
1237 const u32 *dm = 0;
1238
1239 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1240 if (memory) {
1241 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1242 lmb_size = of_get_lmb_size(memory);
1243 of_node_put(memory);
1244 }
1245 return lmb_size * drconf_cell_cnt;
1246}
1247
1248/*
1249 * memory_hotplug_max - return max address of memory that may be added
1250 *
1251 * This is currently only used on systems that support drconfig memory
1252 * hotplug.
1253 */
1254u64 memory_hotplug_max(void)
1255{
1256 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1257}
Mike Kravetz237a0982005-12-05 12:06:42 -08001258#endif /* CONFIG_MEMORY_HOTPLUG */
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001259
Jesse Larrewbd034032011-01-20 19:00:51 +00001260/* Virtual Processor Home Node (VPHN) support */
Jesse Larrew39bf9902010-12-17 22:07:47 +00001261#ifdef CONFIG_PPC_SPLPAR
Nathan Fontenot30c05352013-04-24 06:02:13 +00001262struct topology_update_data {
1263 struct topology_update_data *next;
1264 unsigned int cpu;
1265 int old_nid;
1266 int new_nid;
1267};
1268
Anton Blanchard5de16692011-01-29 12:24:34 +00001269static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001270static cpumask_t cpu_associativity_changes_mask;
1271static int vphn_enabled;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001272static int prrn_enabled;
1273static void reset_topology_timer(void);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001274
1275/*
1276 * Store the current values of the associativity change counters in the
1277 * hypervisor.
1278 */
1279static void setup_cpu_associativity_change_counters(void)
1280{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001281 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001282
Anton Blanchard5de16692011-01-29 12:24:34 +00001283 /* The VPHN feature supports a maximum of 8 reference points */
1284 BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1285
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001286 for_each_possible_cpu(cpu) {
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001287 int i;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001288 u8 *counts = vphn_cpu_change_counts[cpu];
1289 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1290
Anton Blanchard5de16692011-01-29 12:24:34 +00001291 for (i = 0; i < distance_ref_points_depth; i++)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001292 counts[i] = hypervisor_counts[i];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001293 }
1294}
1295
1296/*
1297 * The hypervisor maintains a set of 8 associativity change counters in
1298 * the VPA of each cpu that correspond to the associativity levels in the
1299 * ibm,associativity-reference-points property. When an associativity
1300 * level changes, the corresponding counter is incremented.
1301 *
1302 * Set a bit in cpu_associativity_changes_mask for each cpu whose home
1303 * node associativity levels have changed.
1304 *
1305 * Returns the number of cpus with unhandled associativity changes.
1306 */
1307static int update_cpu_associativity_changes_mask(void)
1308{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001309 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001310 cpumask_t *changes = &cpu_associativity_changes_mask;
1311
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001312 for_each_possible_cpu(cpu) {
1313 int i, changed = 0;
1314 u8 *counts = vphn_cpu_change_counts[cpu];
1315 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1316
Anton Blanchard5de16692011-01-29 12:24:34 +00001317 for (i = 0; i < distance_ref_points_depth; i++) {
Anton Blanchardd69043e2011-01-29 12:26:19 +00001318 if (hypervisor_counts[i] != counts[i]) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001319 counts[i] = hypervisor_counts[i];
1320 changed = 1;
1321 }
1322 }
1323 if (changed) {
1324 cpumask_set_cpu(cpu, changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001325 }
1326 }
1327
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001328 return cpumask_weight(changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001329}
1330
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001331/*
1332 * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1333 * the complete property we have to add the length in the first cell.
1334 */
1335#define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001336
1337/*
1338 * Convert the associativity domain numbers returned from the hypervisor
1339 * to the sequence they would appear in the ibm,associativity property.
1340 */
1341static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
1342{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001343 int i, nr_assoc_doms = 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001344 const u16 *field = (const u16*) packed;
1345
1346#define VPHN_FIELD_UNUSED (0xffff)
1347#define VPHN_FIELD_MSB (0x8000)
1348#define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
1349
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001350 for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001351 if (*field == VPHN_FIELD_UNUSED) {
1352 /* All significant fields processed, and remaining
1353 * fields contain the reserved value of all 1's.
1354 * Just store them.
1355 */
1356 unpacked[i] = *((u32*)field);
1357 field += 2;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001358 } else if (*field & VPHN_FIELD_MSB) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001359 /* Data is in the lower 15 bits of this field */
1360 unpacked[i] = *field & VPHN_FIELD_MASK;
1361 field++;
1362 nr_assoc_doms++;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001363 } else {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001364 /* Data is in the lower 15 bits of this field
1365 * concatenated with the next 16 bit field
1366 */
1367 unpacked[i] = *((u32*)field);
1368 field += 2;
1369 nr_assoc_doms++;
1370 }
1371 }
1372
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001373 /* The first cell contains the length of the property */
1374 unpacked[0] = nr_assoc_doms;
1375
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001376 return nr_assoc_doms;
1377}
1378
1379/*
1380 * Retrieve the new associativity information for a virtual processor's
1381 * home node.
1382 */
1383static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
1384{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001385 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001386 long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1387 u64 flags = 1;
1388 int hwcpu = get_hard_smp_processor_id(cpu);
1389
1390 rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
1391 vphn_unpack_associativity(retbuf, associativity);
1392
1393 return rc;
1394}
1395
1396static long vphn_get_associativity(unsigned long cpu,
1397 unsigned int *associativity)
1398{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001399 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001400
1401 rc = hcall_vphn(cpu, associativity);
1402
1403 switch (rc) {
1404 case H_FUNCTION:
1405 printk(KERN_INFO
1406 "VPHN is not supported. Disabling polling...\n");
1407 stop_topology_update();
1408 break;
1409 case H_HARDWARE:
1410 printk(KERN_ERR
1411 "hcall_vphn() experienced a hardware fault "
1412 "preventing VPHN. Disabling polling...\n");
1413 stop_topology_update();
1414 }
1415
1416 return rc;
1417}
1418
1419/*
Nathan Fontenot30c05352013-04-24 06:02:13 +00001420 * Update the CPU maps and sysfs entries for a single CPU when its NUMA
1421 * characteristics change. This function doesn't perform any locking and is
1422 * only safe to call from stop_machine().
1423 */
1424static int update_cpu_topology(void *data)
1425{
1426 struct topology_update_data *update;
1427 unsigned long cpu;
1428
1429 if (!data)
1430 return -EINVAL;
1431
1432 cpu = get_cpu();
1433
1434 for (update = data; update; update = update->next) {
1435 if (cpu != update->cpu)
1436 continue;
1437
1438 unregister_cpu_under_node(update->cpu, update->old_nid);
1439 unmap_cpu_from_node(update->cpu);
1440 map_cpu_to_node(update->cpu, update->new_nid);
Jesse Larrew176bbf12013-04-24 06:03:48 +00001441 vdso_getcpu_init();
Nathan Fontenot30c05352013-04-24 06:02:13 +00001442 register_cpu_under_node(update->cpu, update->new_nid);
1443 }
1444
1445 return 0;
1446}
1447
1448/*
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001449 * Update the node maps and sysfs entries for each cpu whose home node
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001450 * has changed. Returns 1 when the topology has changed, and 0 otherwise.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001451 */
1452int arch_update_cpu_topology(void)
1453{
Nathan Fontenot30c05352013-04-24 06:02:13 +00001454 unsigned int cpu, changed = 0;
1455 struct topology_update_data *updates, *ud;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001456 unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
Jesse Larrew176bbf12013-04-24 06:03:48 +00001457 cpumask_t updated_cpus;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001458 struct device *dev;
Nathan Fontenot30c05352013-04-24 06:02:13 +00001459 int weight, i = 0;
1460
1461 weight = cpumask_weight(&cpu_associativity_changes_mask);
1462 if (!weight)
1463 return 0;
1464
1465 updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
1466 if (!updates)
1467 return 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001468
Jesse Larrew176bbf12013-04-24 06:03:48 +00001469 cpumask_clear(&updated_cpus);
1470
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001471 for_each_cpu(cpu, &cpu_associativity_changes_mask) {
Nathan Fontenot30c05352013-04-24 06:02:13 +00001472 ud = &updates[i++];
1473 ud->cpu = cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001474 vphn_get_associativity(cpu, associativity);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001475 ud->new_nid = associativity_to_nid(associativity);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001476
Nathan Fontenot30c05352013-04-24 06:02:13 +00001477 if (ud->new_nid < 0 || !node_online(ud->new_nid))
1478 ud->new_nid = first_online_node;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001479
Nathan Fontenot30c05352013-04-24 06:02:13 +00001480 ud->old_nid = numa_cpu_lookup_table[cpu];
Jesse Larrew176bbf12013-04-24 06:03:48 +00001481 cpumask_set_cpu(cpu, &updated_cpus);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001482
Nathan Fontenot30c05352013-04-24 06:02:13 +00001483 if (i < weight)
1484 ud->next = &updates[i];
1485 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001486
Jesse Larrew176bbf12013-04-24 06:03:48 +00001487 stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001488
1489 for (ud = &updates[0]; ud; ud = ud->next) {
1490 dev = get_cpu_device(ud->cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001491 if (dev)
1492 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001493 cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001494 changed = 1;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001495 }
1496
Nathan Fontenot30c05352013-04-24 06:02:13 +00001497 kfree(updates);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001498 return changed;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001499}
1500
1501static void topology_work_fn(struct work_struct *work)
1502{
1503 rebuild_sched_domains();
1504}
1505static DECLARE_WORK(topology_work, topology_work_fn);
1506
1507void topology_schedule_update(void)
1508{
1509 schedule_work(&topology_work);
1510}
1511
1512static void topology_timer_fn(unsigned long ignored)
1513{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001514 if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001515 topology_schedule_update();
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001516 else if (vphn_enabled) {
1517 if (update_cpu_associativity_changes_mask() > 0)
1518 topology_schedule_update();
1519 reset_topology_timer();
1520 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001521}
1522static struct timer_list topology_timer =
1523 TIMER_INITIALIZER(topology_timer_fn, 0, 0);
1524
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001525static void reset_topology_timer(void)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001526{
1527 topology_timer.data = 0;
1528 topology_timer.expires = jiffies + 60 * HZ;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001529 mod_timer(&topology_timer, topology_timer.expires);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001530}
1531
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001532static void stage_topology_update(int core_id)
1533{
1534 cpumask_or(&cpu_associativity_changes_mask,
1535 &cpu_associativity_changes_mask, cpu_sibling_mask(core_id));
1536 reset_topology_timer();
1537}
1538
1539static int dt_update_callback(struct notifier_block *nb,
1540 unsigned long action, void *data)
1541{
1542 struct of_prop_reconfig *update;
1543 int rc = NOTIFY_DONE;
1544
1545 switch (action) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001546 case OF_RECONFIG_UPDATE_PROPERTY:
1547 update = (struct of_prop_reconfig *)data;
Nathan Fontenot30c05352013-04-24 06:02:13 +00001548 if (!of_prop_cmp(update->dn->type, "cpu") &&
1549 !of_prop_cmp(update->prop->name, "ibm,associativity")) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001550 u32 core_id;
1551 of_property_read_u32(update->dn, "reg", &core_id);
1552 stage_topology_update(core_id);
1553 rc = NOTIFY_OK;
1554 }
1555 break;
1556 }
1557
1558 return rc;
1559}
1560
1561static struct notifier_block dt_update_nb = {
1562 .notifier_call = dt_update_callback,
1563};
1564
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001565/*
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001566 * Start polling for associativity changes.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001567 */
1568int start_topology_update(void)
1569{
1570 int rc = 0;
1571
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001572 if (firmware_has_feature(FW_FEATURE_PRRN)) {
1573 if (!prrn_enabled) {
1574 prrn_enabled = 1;
1575 vphn_enabled = 0;
1576 rc = of_reconfig_notifier_register(&dt_update_nb);
1577 }
Jesse Larrewb7abef02013-04-24 06:05:22 +00001578 } else if (firmware_has_feature(FW_FEATURE_VPHN) &&
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001579 get_lppaca()->shared_proc) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001580 if (!vphn_enabled) {
1581 prrn_enabled = 0;
1582 vphn_enabled = 1;
1583 setup_cpu_associativity_change_counters();
1584 init_timer_deferrable(&topology_timer);
1585 reset_topology_timer();
1586 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001587 }
1588
1589 return rc;
1590}
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001591
1592/*
1593 * Disable polling for VPHN associativity changes.
1594 */
1595int stop_topology_update(void)
1596{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001597 int rc = 0;
1598
1599 if (prrn_enabled) {
1600 prrn_enabled = 0;
1601 rc = of_reconfig_notifier_unregister(&dt_update_nb);
1602 } else if (vphn_enabled) {
1603 vphn_enabled = 0;
1604 rc = del_timer_sync(&topology_timer);
1605 }
1606
1607 return rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001608}
Nathan Fontenote04fa612013-04-24 06:07:39 +00001609
1610int prrn_is_enabled(void)
1611{
1612 return prrn_enabled;
1613}
1614
1615static int topology_read(struct seq_file *file, void *v)
1616{
1617 if (vphn_enabled || prrn_enabled)
1618 seq_puts(file, "on\n");
1619 else
1620 seq_puts(file, "off\n");
1621
1622 return 0;
1623}
1624
1625static int topology_open(struct inode *inode, struct file *file)
1626{
1627 return single_open(file, topology_read, NULL);
1628}
1629
1630static ssize_t topology_write(struct file *file, const char __user *buf,
1631 size_t count, loff_t *off)
1632{
1633 char kbuf[4]; /* "on" or "off" plus null. */
1634 int read_len;
1635
1636 read_len = count < 3 ? count : 3;
1637 if (copy_from_user(kbuf, buf, read_len))
1638 return -EINVAL;
1639
1640 kbuf[read_len] = '\0';
1641
1642 if (!strncmp(kbuf, "on", 2))
1643 start_topology_update();
1644 else if (!strncmp(kbuf, "off", 3))
1645 stop_topology_update();
1646 else
1647 return -EINVAL;
1648
1649 return count;
1650}
1651
1652static const struct file_operations topology_ops = {
1653 .read = seq_read,
1654 .write = topology_write,
1655 .open = topology_open,
1656 .release = single_release
1657};
1658
1659static int topology_update_init(void)
1660{
1661 start_topology_update();
1662 proc_create("powerpc/topology_updates", 644, NULL, &topology_ops);
1663
1664 return 0;
1665}
1666device_initcall(topology_update_init);
Jesse Larrew39bf9902010-12-17 22:07:47 +00001667#endif /* CONFIG_PPC_SPLPAR */