blob: 908699fb61c9ccf6196ccc2f5eb748220c3b5990 [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>
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110026#include <asm/sparsemem.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080027#include <asm/prom.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110028#include <asm/smp.h>
Jesse Larrew9eff1a32010-12-01 12:31:15 +000029#include <asm/firmware.h>
30#include <asm/paca.h>
Jesse Larrew39bf9902010-12-17 22:07:47 +000031#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010032#include <asm/setup.h>
Jesse Larrew176bbf12013-04-24 06:03:48 +000033#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static int numa_enabled = 1;
36
Balbir Singh1daa6d02008-02-01 15:57:31 +110037static char *cmdline __initdata;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int numa_debug;
40#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
41
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110042int numa_cpu_lookup_table[NR_CPUS];
Anton Blanchard25863de2010-04-26 15:32:43 +000043cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct pglist_data *node_data[MAX_NUMNODES];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110045
46EXPORT_SYMBOL(numa_cpu_lookup_table);
Anton Blanchard25863de2010-04-26 15:32:43 +000047EXPORT_SYMBOL(node_to_cpumask_map);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110048EXPORT_SYMBOL(node_data);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int min_common_depth;
Mike Kravetz237a09892005-12-05 12:06:42 -080051static int n_mem_addr_cells, n_mem_size_cells;
Anton Blanchard41eab6f2010-05-16 20:22:31 +000052static int form1_affinity;
53
54#define MAX_DISTANCE_REF_POINTS 4
55static int distance_ref_points_depth;
56static const unsigned int *distance_ref_points;
57static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Anton Blanchard25863de2010-04-26 15:32:43 +000059/*
60 * Allocate node_to_cpumask_map based on number of available nodes
61 * Requires node_possible_map to be valid.
62 *
Wanlong Gao95129382012-01-12 17:20:09 -080063 * Note: cpumask_of_node() is not valid until after this is done.
Anton Blanchard25863de2010-04-26 15:32:43 +000064 */
65static void __init setup_node_to_cpumask_map(void)
66{
67 unsigned int node, num = 0;
68
69 /* setup nr_node_ids if not done yet */
70 if (nr_node_ids == MAX_NUMNODES) {
71 for_each_node_mask(node, node_possible_map)
72 num = node;
73 nr_node_ids = num + 1;
74 }
75
76 /* allocate the map */
77 for (node = 0; node < nr_node_ids; node++)
78 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
79
80 /* cpumask_of_node() will now work */
81 dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
82}
83
Stephen Rothwell55671f32013-03-25 18:44:44 +000084static int __init fake_numa_create_new_node(unsigned long end_pfn,
Balbir Singh1daa6d02008-02-01 15:57:31 +110085 unsigned int *nid)
86{
87 unsigned long long mem;
88 char *p = cmdline;
89 static unsigned int fake_nid;
90 static unsigned long long curr_boundary;
91
92 /*
93 * Modify node id, iff we started creating NUMA nodes
94 * We want to continue from where we left of the last time
95 */
96 if (fake_nid)
97 *nid = fake_nid;
98 /*
99 * In case there are no more arguments to parse, the
100 * node_id should be the same as the last fake node id
101 * (we've handled this above).
102 */
103 if (!p)
104 return 0;
105
106 mem = memparse(p, &p);
107 if (!mem)
108 return 0;
109
110 if (mem < curr_boundary)
111 return 0;
112
113 curr_boundary = mem;
114
115 if ((end_pfn << PAGE_SHIFT) > mem) {
116 /*
117 * Skip commas and spaces
118 */
119 while (*p == ',' || *p == ' ' || *p == '\t')
120 p++;
121
122 cmdline = p;
123 fake_nid++;
124 *nid = fake_nid;
125 dbg("created new fake_node with id %d\n", fake_nid);
126 return 1;
127 }
128 return 0;
129}
130
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000131/*
Tejun Heo5dfe8662011-07-14 09:46:10 +0200132 * get_node_active_region - Return active region containing pfn
Jon Tollefsone8170372008-10-16 18:59:43 +0000133 * Active range returned is empty if none found.
Tejun Heo5dfe8662011-07-14 09:46:10 +0200134 * @pfn: The page to return the region for
135 * @node_ar: Returned set to the active region containing @pfn
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000136 */
Tejun Heo5dfe8662011-07-14 09:46:10 +0200137static void __init get_node_active_region(unsigned long pfn,
138 struct node_active_region *node_ar)
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000139{
Tejun Heo5dfe8662011-07-14 09:46:10 +0200140 unsigned long start_pfn, end_pfn;
141 int i, nid;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000142
Tejun Heo5dfe8662011-07-14 09:46:10 +0200143 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
144 if (pfn >= start_pfn && pfn < end_pfn) {
145 node_ar->nid = nid;
146 node_ar->start_pfn = start_pfn;
147 node_ar->end_pfn = end_pfn;
148 break;
149 }
150 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000151}
152
Jesse Larrew39bf9902010-12-17 22:07:47 +0000153static void map_cpu_to_node(int cpu, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 numa_cpu_lookup_table[cpu] = node;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100156
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600157 dbg("adding cpu %d to node %d\n", cpu, node);
158
Anton Blanchard25863de2010-04-26 15:32:43 +0000159 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
160 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Jesse Larrew39bf9902010-12-17 22:07:47 +0000163#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void unmap_cpu_from_node(unsigned long cpu)
165{
166 int node = numa_cpu_lookup_table[cpu];
167
168 dbg("removing cpu %lu from node %d\n", cpu, node);
169
Anton Blanchard25863de2010-04-26 15:32:43 +0000170 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
Anton Blanchard429f4d82011-01-29 12:37:16 +0000171 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } else {
173 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
174 cpu, node);
175 }
176}
Jesse Larrew39bf9902010-12-17 22:07:47 +0000177#endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* must hold reference to node during call */
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000180static const int *of_get_associativity(struct device_node *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000182 return of_get_property(dev, "ibm,associativity", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Chandrucf000852008-08-30 00:28:16 +1000185/*
186 * Returns the property linux,drconf-usable-memory if
187 * it exists (the property exists only in kexec/kdump kernels,
188 * added by kexec-tools)
189 */
190static const u32 *of_get_usable_memory(struct device_node *memory)
191{
192 const u32 *prop;
193 u32 len;
194 prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
195 if (!prop || len < sizeof(unsigned int))
196 return 0;
197 return prop;
198}
199
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000200int __node_distance(int a, int b)
201{
202 int i;
203 int distance = LOCAL_DISTANCE;
204
205 if (!form1_affinity)
Vaidyanathan Srinivasan7122bee2013-03-22 05:49:35 +0000206 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000207
208 for (i = 0; i < distance_ref_points_depth; i++) {
209 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
210 break;
211
212 /* Double the distance for each NUMA level */
213 distance *= 2;
214 }
215
216 return distance;
217}
218
219static void initialize_distance_lookup_table(int nid,
220 const unsigned int *associativity)
221{
222 int i;
223
224 if (!form1_affinity)
225 return;
226
227 for (i = 0; i < distance_ref_points_depth; i++) {
228 distance_lookup_table[nid][i] =
229 associativity[distance_ref_points[i]];
230 }
231}
232
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600233/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
234 * info is found.
235 */
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000236static int associativity_to_nid(const unsigned int *associativity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600238 int nid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (min_common_depth == -1)
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000243 if (associativity[0] >= min_common_depth)
244 nid = associativity[min_common_depth];
Nathan Lynchbc16a752006-03-20 18:36:15 -0600245
246 /* POWER4 LPAR uses 0xffff as invalid node */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600247 if (nid == 0xffff || nid >= MAX_NUMNODES)
248 nid = -1;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000249
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000250 if (nid > 0 && associativity[0] >= distance_ref_points_depth)
251 initialize_distance_lookup_table(nid, associativity);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000252
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600253out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600254 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000257/* Returns the nid associated with the given device tree node,
258 * or -1 if not found.
259 */
260static int of_node_to_nid_single(struct device_node *device)
261{
262 int nid = -1;
263 const unsigned int *tmp;
264
265 tmp = of_get_associativity(device);
266 if (tmp)
267 nid = associativity_to_nid(tmp);
268 return nid;
269}
270
Jeremy Kerr953039c2006-05-01 12:16:12 -0700271/* Walk the device tree upwards, looking for an associativity id */
272int of_node_to_nid(struct device_node *device)
273{
274 struct device_node *tmp;
275 int nid = -1;
276
277 of_node_get(device);
278 while (device) {
279 nid = of_node_to_nid_single(device);
280 if (nid != -1)
281 break;
282
283 tmp = device;
284 device = of_get_parent(tmp);
285 of_node_put(tmp);
286 }
287 of_node_put(device);
288
289 return nid;
290}
291EXPORT_SYMBOL_GPL(of_node_to_nid);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int __init find_min_common_depth(void)
294{
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000295 int depth;
Michael Ellermane70606e2011-04-10 20:42:05 +0000296 struct device_node *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000298 if (firmware_has_feature(FW_FEATURE_OPAL))
299 root = of_find_node_by_path("/ibm,opal");
300 else
301 root = of_find_node_by_path("/rtas");
Michael Ellermane70606e2011-04-10 20:42:05 +0000302 if (!root)
303 root = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /*
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000306 * This property is a set of 32-bit integers, each representing
307 * an index into the ibm,associativity nodes.
308 *
309 * With form 0 affinity the first integer is for an SMP configuration
310 * (should be all 0's) and the second is for a normal NUMA
311 * configuration. We have only one level of NUMA.
312 *
313 * With form 1 affinity the first integer is the most significant
314 * NUMA boundary and the following are progressively less significant
315 * boundaries. There can be more than one level of NUMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Michael Ellermane70606e2011-04-10 20:42:05 +0000317 distance_ref_points = of_get_property(root,
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000318 "ibm,associativity-reference-points",
319 &distance_ref_points_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000321 if (!distance_ref_points) {
322 dbg("NUMA: ibm,associativity-reference-points not found.\n");
323 goto err;
324 }
325
326 distance_ref_points_depth /= sizeof(int);
327
Nathan Fontenot8002b0c2013-04-24 05:58:23 +0000328 if (firmware_has_feature(FW_FEATURE_OPAL) ||
329 firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
330 dbg("Using form 1 affinity\n");
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000331 form1_affinity = 1;
Anton Blanchard4b83c332010-04-07 15:33:44 +0000332 }
333
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000334 if (form1_affinity) {
335 depth = distance_ref_points[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else {
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000337 if (distance_ref_points_depth < 2) {
338 printk(KERN_WARNING "NUMA: "
339 "short ibm,associativity-reference-points\n");
340 goto err;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000343 depth = distance_ref_points[1];
344 }
345
346 /*
347 * Warn and cap if the hardware supports more than
348 * MAX_DISTANCE_REF_POINTS domains.
349 */
350 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
351 printk(KERN_WARNING "NUMA: distance array capped at "
352 "%d entries\n", MAX_DISTANCE_REF_POINTS);
353 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
354 }
355
Michael Ellermane70606e2011-04-10 20:42:05 +0000356 of_node_put(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return depth;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000358
359err:
Michael Ellermane70606e2011-04-10 20:42:05 +0000360 of_node_put(root);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000361 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800364static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct device_node *memory = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 memory = of_find_node_by_type(memory, "memory");
Paul Mackerras54c23312005-12-05 15:50:39 +1100369 if (!memory)
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800370 panic("numa.c: No memory nodes found!");
Paul Mackerras54c23312005-12-05 15:50:39 +1100371
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000372 *n_addr_cells = of_n_addr_cells(memory);
Stephen Rothwell9213fee2007-04-03 10:57:48 +1000373 *n_size_cells = of_n_size_cells(memory);
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800374 of_node_put(memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
David Rientjes2011b1d2011-12-08 12:46:37 +0000377static unsigned long read_n_cells(int n, const unsigned int **buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 unsigned long result = 0;
380
381 while (n--) {
382 result = (result << 32) | **buf;
383 (*buf)++;
384 }
385 return result;
386}
387
Nathan Fontenot83426812008-07-03 13:35:54 +1000388/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000389 * Read the next memblock list entry from the ibm,dynamic-memory property
Nathan Fontenot83426812008-07-03 13:35:54 +1000390 * and return the information in the provided of_drconf_cell structure.
391 */
392static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
393{
394 const u32 *cp;
395
396 drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
397
398 cp = *cellp;
399 drmem->drc_index = cp[0];
400 drmem->reserved = cp[1];
401 drmem->aa_index = cp[2];
402 drmem->flags = cp[3];
403
404 *cellp = cp + 4;
405}
406
407/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300408 * Retrieve and validate the ibm,dynamic-memory property of the device tree.
Nathan Fontenot83426812008-07-03 13:35:54 +1000409 *
Yinghai Lu95f72d12010-07-12 14:36:09 +1000410 * The layout of the ibm,dynamic-memory property is a number N of memblock
411 * list entries followed by N memblock list entries. Each memblock list entry
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300412 * contains information as laid out in the of_drconf_cell struct above.
Nathan Fontenot83426812008-07-03 13:35:54 +1000413 */
414static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
415{
416 const u32 *prop;
417 u32 len, entries;
418
419 prop = of_get_property(memory, "ibm,dynamic-memory", &len);
420 if (!prop || len < sizeof(unsigned int))
421 return 0;
422
423 entries = *prop++;
424
425 /* Now that we know the number of entries, revalidate the size
426 * of the property read in to ensure we have everything
427 */
428 if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
429 return 0;
430
431 *dm = prop;
432 return entries;
433}
434
435/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300436 * Retrieve and validate the ibm,lmb-size property for drconf memory
Nathan Fontenot83426812008-07-03 13:35:54 +1000437 * from the device tree.
438 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000439static u64 of_get_lmb_size(struct device_node *memory)
Nathan Fontenot83426812008-07-03 13:35:54 +1000440{
441 const u32 *prop;
442 u32 len;
443
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000444 prop = of_get_property(memory, "ibm,lmb-size", &len);
Nathan Fontenot83426812008-07-03 13:35:54 +1000445 if (!prop || len < sizeof(unsigned int))
446 return 0;
447
448 return read_n_cells(n_mem_size_cells, &prop);
449}
450
451struct assoc_arrays {
452 u32 n_arrays;
453 u32 array_sz;
454 const u32 *arrays;
455};
456
457/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300458 * Retrieve and validate the list of associativity arrays for drconf
Nathan Fontenot83426812008-07-03 13:35:54 +1000459 * memory from the ibm,associativity-lookup-arrays property of the
460 * device tree..
461 *
462 * The layout of the ibm,associativity-lookup-arrays property is a number N
463 * indicating the number of associativity arrays, followed by a number M
464 * indicating the size of each associativity array, followed by a list
465 * of N associativity arrays.
466 */
467static int of_get_assoc_arrays(struct device_node *memory,
468 struct assoc_arrays *aa)
469{
470 const u32 *prop;
471 u32 len;
472
473 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
474 if (!prop || len < 2 * sizeof(unsigned int))
475 return -1;
476
477 aa->n_arrays = *prop++;
478 aa->array_sz = *prop++;
479
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800480 /* Now that we know the number of arrays and size of each array,
Nathan Fontenot83426812008-07-03 13:35:54 +1000481 * revalidate the size of the property read in.
482 */
483 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
484 return -1;
485
486 aa->arrays = prop;
487 return 0;
488}
489
490/*
491 * This is like of_node_to_nid_single() for memory represented in the
492 * ibm,dynamic-reconfiguration-memory node.
493 */
494static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
495 struct assoc_arrays *aa)
496{
497 int default_nid = 0;
498 int nid = default_nid;
499 int index;
500
501 if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
502 !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
503 drmem->aa_index < aa->n_arrays) {
504 index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
505 nid = aa->arrays[index];
506
507 if (nid == 0xffff || nid >= MAX_NUMNODES)
508 nid = default_nid;
509 }
510
511 return nid;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * Figure out to which domain a cpu belongs and stick it there.
516 * Return the id of the domain used.
517 */
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600518static int __cpuinit numa_setup_cpu(unsigned long lcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Nathan Lynchcf950b72006-03-20 18:35:45 -0600520 int nid = 0;
Milton Miller8b16cd22009-01-08 02:19:45 +0000521 struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (!cpu) {
524 WARN_ON(1);
525 goto out;
526 }
527
Jeremy Kerr953039c2006-05-01 12:16:12 -0700528 nid = of_node_to_nid_single(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600530 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -0800531 nid = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600533 map_cpu_to_node(lcpu, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 of_node_put(cpu);
536
Nathan Lynchcf950b72006-03-20 18:35:45 -0600537 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700540static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned long action,
542 void *hcpu)
543{
544 unsigned long lcpu = (unsigned long)hcpu;
545 int ret = NOTIFY_DONE;
546
547 switch (action) {
548 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700549 case CPU_UP_PREPARE_FROZEN:
Nathan Lynch2b261222006-03-20 18:37:15 -0600550 numa_setup_cpu(lcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ret = NOTIFY_OK;
552 break;
553#ifdef CONFIG_HOTPLUG_CPU
554 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700555 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700557 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unmap_cpu_from_node(lcpu);
559 break;
560 ret = NOTIFY_OK;
561#endif
562 }
563 return ret;
564}
565
566/*
567 * Check and possibly modify a memory region to enforce the memory limit.
568 *
569 * Returns the size the region should have to enforce the memory limit.
570 * This will either be the original value of size, a truncated value,
571 * or zero. If the returned value of size is 0 the region should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300572 * discarded as it lies wholly above the memory limit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100574static unsigned long __init numa_enforce_memory_limit(unsigned long start,
575 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000578 * We use memblock_end_of_DRAM() in here instead of memory_limit because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * we've already adjusted it for the limit and it takes care of
Milton Millerfe552492008-10-20 15:37:04 +0000580 * having memory holes below the limit. Also, in the case of
581 * iommu_is_off, memory_limit is not set but is implicitly enforced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Yinghai Lu95f72d12010-07-12 14:36:09 +1000584 if (start + size <= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return size;
586
Yinghai Lu95f72d12010-07-12 14:36:09 +1000587 if (start >= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
589
Yinghai Lu95f72d12010-07-12 14:36:09 +1000590 return memblock_end_of_DRAM() - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Paul Mackerras02045682006-11-29 22:27:42 +1100593/*
Chandrucf000852008-08-30 00:28:16 +1000594 * Reads the counter for a given entry in
595 * linux,drconf-usable-memory property
596 */
597static inline int __init read_usm_ranges(const u32 **usm)
598{
599 /*
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000600 * For each lmb in ibm,dynamic-memory a corresponding
Chandrucf000852008-08-30 00:28:16 +1000601 * entry in linux,drconf-usable-memory property contains
602 * a counter followed by that many (base, size) duple.
603 * read the counter from linux,drconf-usable-memory
604 */
605 return read_n_cells(n_mem_size_cells, usm);
606}
607
608/*
Paul Mackerras02045682006-11-29 22:27:42 +1100609 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
610 * node. This assumes n_mem_{addr,size}_cells have been set.
611 */
612static void __init parse_drconf_memory(struct device_node *memory)
613{
Michael Neuling82b25212012-06-19 20:01:45 +0000614 const u32 *uninitialized_var(dm), *usm;
Chandrucf000852008-08-30 00:28:16 +1000615 unsigned int n, rc, ranges, is_kexec_kdump = 0;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000616 unsigned long lmb_size, base, size, sz;
Nathan Fontenot83426812008-07-03 13:35:54 +1000617 int nid;
Benjamin Herrenschmidtaa709f32012-07-05 16:30:33 +0000618 struct assoc_arrays aa = { .arrays = NULL };
Paul Mackerras02045682006-11-29 22:27:42 +1100619
Nathan Fontenot83426812008-07-03 13:35:54 +1000620 n = of_get_drconf_memory(memory, &dm);
621 if (!n)
Paul Mackerras02045682006-11-29 22:27:42 +1100622 return;
623
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000624 lmb_size = of_get_lmb_size(memory);
625 if (!lmb_size)
Nathan Fontenot83426812008-07-03 13:35:54 +1000626 return;
627
628 rc = of_get_assoc_arrays(memory, &aa);
629 if (rc)
Paul Mackerras02045682006-11-29 22:27:42 +1100630 return;
631
Chandrucf000852008-08-30 00:28:16 +1000632 /* check if this is a kexec/kdump kernel */
633 usm = of_get_usable_memory(memory);
634 if (usm != NULL)
635 is_kexec_kdump = 1;
636
Paul Mackerras02045682006-11-29 22:27:42 +1100637 for (; n != 0; --n) {
Nathan Fontenot83426812008-07-03 13:35:54 +1000638 struct of_drconf_cell drmem;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100639
Nathan Fontenot83426812008-07-03 13:35:54 +1000640 read_drconf_cell(&drmem, &dm);
641
642 /* skip this block if the reserved bit is set in flags (0x80)
643 or if the block is not assigned to this partition (0x8) */
644 if ((drmem.flags & DRCONF_MEM_RESERVED)
645 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
646 continue;
647
Chandrucf000852008-08-30 00:28:16 +1000648 base = drmem.base_addr;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000649 size = lmb_size;
Chandrucf000852008-08-30 00:28:16 +1000650 ranges = 1;
Nathan Fontenot83426812008-07-03 13:35:54 +1000651
Chandrucf000852008-08-30 00:28:16 +1000652 if (is_kexec_kdump) {
653 ranges = read_usm_ranges(&usm);
654 if (!ranges) /* there are no (base, size) duple */
655 continue;
656 }
657 do {
658 if (is_kexec_kdump) {
659 base = read_n_cells(n_mem_addr_cells, &usm);
660 size = read_n_cells(n_mem_size_cells, &usm);
661 }
662 nid = of_drconf_to_nid_single(&drmem, &aa);
663 fake_numa_create_new_node(
664 ((base + size) >> PAGE_SHIFT),
Nathan Fontenot83426812008-07-03 13:35:54 +1000665 &nid);
Chandrucf000852008-08-30 00:28:16 +1000666 node_set_online(nid);
667 sz = numa_enforce_memory_limit(base, size);
668 if (sz)
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800669 memblock_set_node(base, sz, nid);
Chandrucf000852008-08-30 00:28:16 +1000670 } while (--ranges);
Paul Mackerras02045682006-11-29 22:27:42 +1100671 }
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static int __init parse_numa_properties(void)
675{
Anton Blanchard94db7c52011-08-10 20:44:22 +0000676 struct device_node *memory;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600677 int default_nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned long i;
679
680 if (numa_enabled == 0) {
681 printk(KERN_WARNING "NUMA disabled by user\n");
682 return -1;
683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 min_common_depth = find_min_common_depth();
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (min_common_depth < 0)
688 return min_common_depth;
689
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600690 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600693 * Even though we connect cpus to numa domains later in SMP
694 * init, we need to know the node ids now. This is because
695 * each node to be onlined must have NODE_DATA etc backing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600697 for_each_present_cpu(i) {
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000698 struct device_node *cpu;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600699 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Milton Miller8b16cd22009-01-08 02:19:45 +0000701 cpu = of_get_cpu_node(i, NULL);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600702 BUG_ON(!cpu);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700703 nid = of_node_to_nid_single(cpu);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600704 of_node_put(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600706 /*
707 * Don't fall back to default_nid yet -- we will plug
708 * cpus into nodes once the memory scan has discovered
709 * the topology.
710 */
711 if (nid < 0)
712 continue;
713 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Mike Kravetz237a09892005-12-05 12:06:42 -0800716 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
Anton Blanchard94db7c52011-08-10 20:44:22 +0000717
718 for_each_node_by_type(memory, "memory") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long start;
720 unsigned long size;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600721 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000723 const unsigned int *memcell_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned int len;
725
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000726 memcell_buf = of_get_property(memory,
Michael Ellermanba759482005-12-04 18:39:55 +1100727 "linux,usable-memory", &len);
728 if (!memcell_buf || len <= 0)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000729 memcell_buf = of_get_property(memory, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!memcell_buf || len <= 0)
731 continue;
732
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100733 /* ranges in cell */
734 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735new_range:
736 /* these are order-sensitive, and modify the buffer pointer */
Mike Kravetz237a09892005-12-05 12:06:42 -0800737 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
738 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600740 /*
741 * Assumption: either all memory nodes or none will
742 * have associativity properties. If none, then
743 * everything goes to default_nid.
744 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700745 nid = of_node_to_nid_single(memory);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600746 if (nid < 0)
747 nid = default_nid;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100748
749 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600750 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100752 if (!(size = numa_enforce_memory_limit(start, size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (--ranges)
754 goto new_range;
755 else
756 continue;
757 }
758
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800759 memblock_set_node(start, size, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (--ranges)
762 goto new_range;
763 }
764
Paul Mackerras02045682006-11-29 22:27:42 +1100765 /*
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000766 * Now do the same thing for each MEMBLOCK listed in the
767 * ibm,dynamic-memory property in the
768 * ibm,dynamic-reconfiguration-memory node.
Paul Mackerras02045682006-11-29 22:27:42 +1100769 */
770 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
771 if (memory)
772 parse_drconf_memory(memory);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 0;
775}
776
777static void __init setup_nonnuma(void)
778{
Yinghai Lu95f72d12010-07-12 14:36:09 +1000779 unsigned long top_of_ram = memblock_end_of_DRAM();
780 unsigned long total_ram = memblock_phys_mem_size();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700781 unsigned long start_pfn, end_pfn;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000782 unsigned int nid = 0;
783 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Olof Johanssone110b282006-04-12 15:25:01 -0500785 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500787 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 (top_of_ram - total_ram) >> 20);
789
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000790 for_each_memblock(memory, reg) {
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700791 start_pfn = memblock_region_memory_base_pfn(reg);
792 end_pfn = memblock_region_memory_end_pfn(reg);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100793
794 fake_numa_create_new_node(end_pfn, &nid);
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800795 memblock_set_node(PFN_PHYS(start_pfn),
796 PFN_PHYS(end_pfn - start_pfn), nid);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100797 node_set_online(nid);
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Anton Blanchard4b703a22005-12-13 06:56:47 +1100801void __init dump_numa_cpu_topology(void)
802{
803 unsigned int node;
804 unsigned int cpu, count;
805
806 if (min_common_depth == -1 || !numa_enabled)
807 return;
808
809 for_each_online_node(node) {
Olof Johanssone110b282006-04-12 15:25:01 -0500810 printk(KERN_DEBUG "Node %d CPUs:", node);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100811
812 count = 0;
813 /*
814 * If we used a CPU iterator here we would miss printing
815 * the holes in the cpumap.
816 */
Anton Blanchard25863de2010-04-26 15:32:43 +0000817 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
818 if (cpumask_test_cpu(cpu,
819 node_to_cpumask_map[node])) {
Anton Blanchard4b703a22005-12-13 06:56:47 +1100820 if (count == 0)
821 printk(" %u", cpu);
822 ++count;
823 } else {
824 if (count > 1)
825 printk("-%u", cpu - 1);
826 count = 0;
827 }
828 }
829
830 if (count > 1)
Anton Blanchard25863de2010-04-26 15:32:43 +0000831 printk("-%u", nr_cpu_ids - 1);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100832 printk("\n");
833 }
834}
835
836static void __init dump_numa_memory_topology(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 unsigned int node;
839 unsigned int count;
840
841 if (min_common_depth == -1 || !numa_enabled)
842 return;
843
844 for_each_online_node(node) {
845 unsigned long i;
846
Olof Johanssone110b282006-04-12 15:25:01 -0500847 printk(KERN_DEBUG "Node %d Memory:", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 count = 0;
850
Yinghai Lu95f72d12010-07-12 14:36:09 +1000851 for (i = 0; i < memblock_end_of_DRAM();
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100852 i += (1 << SECTION_SIZE_BITS)) {
853 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (count == 0)
855 printk(" 0x%lx", i);
856 ++count;
857 } else {
858 if (count > 0)
859 printk("-0x%lx", i);
860 count = 0;
861 }
862 }
863
864 if (count > 0)
865 printk("-0x%lx", i);
866 printk("\n");
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000871 * Allocate some memory, satisfying the memblock or bootmem allocator where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * required. nid is the preferred node and end is the physical address of
873 * the highest address in the node.
874 *
Dave Hansen0be210f2008-12-09 08:21:35 +0000875 * Returns the virtual address of the memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Dave Hansen893473d2008-12-09 08:21:36 +0000877static void __init *careful_zallocation(int nid, unsigned long size,
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100878 unsigned long align,
879 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Dave Hansen0be210f2008-12-09 08:21:35 +0000881 void *ret;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100882 int new_nid;
Dave Hansen0be210f2008-12-09 08:21:35 +0000883 unsigned long ret_paddr;
884
Yinghai Lu95f72d12010-07-12 14:36:09 +1000885 ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* retry over all memory */
Dave Hansen0be210f2008-12-09 08:21:35 +0000888 if (!ret_paddr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000889 ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Dave Hansen0be210f2008-12-09 08:21:35 +0000891 if (!ret_paddr)
Dave Hansen5d21ea22008-12-09 08:21:33 +0000892 panic("numa.c: cannot allocate %lu bytes for node %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 size, nid);
894
Dave Hansen0be210f2008-12-09 08:21:35 +0000895 ret = __va(ret_paddr);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
Dave Hansenc555e5202008-12-09 08:21:32 +0000898 * We initialize the nodes in numeric order: 0, 1, 2...
Yinghai Lu95f72d12010-07-12 14:36:09 +1000899 * and hand over control from the MEMBLOCK allocator to the
Dave Hansenc555e5202008-12-09 08:21:32 +0000900 * bootmem allocator. If this function is called for
901 * node 5, then we know that all nodes <5 are using the
Yinghai Lu95f72d12010-07-12 14:36:09 +1000902 * bootmem allocator instead of the MEMBLOCK allocator.
Dave Hansenc555e5202008-12-09 08:21:32 +0000903 *
904 * So, check the nid from which this allocation came
905 * and double check to see if we need to use bootmem
Yinghai Lu95f72d12010-07-12 14:36:09 +1000906 * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
Dave Hansenc555e5202008-12-09 08:21:32 +0000907 * since it would be useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Dave Hansen0be210f2008-12-09 08:21:35 +0000909 new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100910 if (new_nid < nid) {
Dave Hansen0be210f2008-12-09 08:21:35 +0000911 ret = __alloc_bootmem_node(NODE_DATA(new_nid),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 size, align, 0);
913
Dave Hansen0be210f2008-12-09 08:21:35 +0000914 dbg("alloc_bootmem %p %lx\n", ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Dave Hansen893473d2008-12-09 08:21:36 +0000917 memset(ret, 0, size);
Dave Hansen0be210f2008-12-09 08:21:35 +0000918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700921static struct notifier_block __cpuinitdata ppc64_numa_nb = {
922 .notifier_call = cpu_numa_callback,
923 .priority = 1 /* Must run before sched domains notifier. */
924};
925
David Rientjes28e86bd2011-12-08 12:33:29 +0000926static void __init mark_reserved_regions_for_nid(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Dave Hansen4a618662008-11-24 12:02:35 +0000928 struct pglist_data *node = NODE_DATA(nid);
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000929 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000931 for_each_memblock(reserved, reg) {
932 unsigned long physbase = reg->base;
933 unsigned long size = reg->size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000934 unsigned long start_pfn = physbase >> PAGE_SHIFT;
Dave Hansen06eccea2009-02-12 12:36:04 +0000935 unsigned long end_pfn = PFN_UP(physbase + size);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000936 struct node_active_region node_ar;
Dave Hansen4a618662008-11-24 12:02:35 +0000937 unsigned long node_end_pfn = node->node_start_pfn +
938 node->node_spanned_pages;
939
940 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000941 * Check to make sure that this memblock.reserved area is
Dave Hansen4a618662008-11-24 12:02:35 +0000942 * within the bounds of the node that we care about.
943 * Checking the nid of the start and end points is not
944 * sufficient because the reserved area could span the
945 * entire node.
946 */
947 if (end_pfn <= node->node_start_pfn ||
948 start_pfn >= node_end_pfn)
949 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000951 get_node_active_region(start_pfn, &node_ar);
Jon Tollefsone8170372008-10-16 18:59:43 +0000952 while (start_pfn < end_pfn &&
953 node_ar.start_pfn < node_ar.end_pfn) {
954 unsigned long reserve_size = size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000955 /*
956 * if reserved region extends past active region
957 * then trim size to active region
958 */
959 if (end_pfn > node_ar.end_pfn)
Jon Tollefsone8170372008-10-16 18:59:43 +0000960 reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
Dave Hansen06eccea2009-02-12 12:36:04 +0000961 - physbase;
Dave Hansena4c74dd2008-12-11 08:36:06 +0000962 /*
963 * Only worry about *this* node, others may not
964 * yet have valid NODE_DATA().
965 */
966 if (node_ar.nid == nid) {
967 dbg("reserve_bootmem %lx %lx nid=%d\n",
968 physbase, reserve_size, node_ar.nid);
969 reserve_bootmem_node(NODE_DATA(node_ar.nid),
970 physbase, reserve_size,
971 BOOTMEM_DEFAULT);
972 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000973 /*
974 * if reserved region is contained in the active region
975 * then done.
976 */
977 if (end_pfn <= node_ar.end_pfn)
978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000980 /*
981 * reserved region extends past the active region
982 * get next active region that contains this
983 * reserved region
984 */
985 start_pfn = node_ar.end_pfn;
986 physbase = start_pfn << PAGE_SHIFT;
Jon Tollefsone8170372008-10-16 18:59:43 +0000987 size = size - reserve_size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000988 get_node_active_region(start_pfn, &node_ar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Dave Hansen4a618662008-11-24 12:02:35 +0000991}
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000992
Dave Hansen4a618662008-11-24 12:02:35 +0000993
994void __init do_init_bootmem(void)
995{
996 int nid;
Dave Hansen4a618662008-11-24 12:02:35 +0000997
998 min_low_pfn = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000999 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Dave Hansen4a618662008-11-24 12:02:35 +00001000 max_pfn = max_low_pfn;
1001
1002 if (parse_numa_properties())
1003 setup_nonnuma();
1004 else
1005 dump_numa_memory_topology();
1006
Dave Hansen4a618662008-11-24 12:02:35 +00001007 for_each_online_node(nid) {
1008 unsigned long start_pfn, end_pfn;
Dave Hansen0be210f2008-12-09 08:21:35 +00001009 void *bootmem_vaddr;
Dave Hansen4a618662008-11-24 12:02:35 +00001010 unsigned long bootmap_pages;
1011
1012 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1013
1014 /*
1015 * Allocate the node structure node local if possible
1016 *
1017 * Be careful moving this around, as it relies on all
1018 * previous nodes' bootmem to be initialized and have
1019 * all reserved areas marked.
1020 */
Dave Hansen893473d2008-12-09 08:21:36 +00001021 NODE_DATA(nid) = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001022 sizeof(struct pglist_data),
1023 SMP_CACHE_BYTES, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001024
1025 dbg("node %d\n", nid);
1026 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
1027
1028 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
1029 NODE_DATA(nid)->node_start_pfn = start_pfn;
1030 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
1031
1032 if (NODE_DATA(nid)->node_spanned_pages == 0)
1033 continue;
1034
1035 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
1036 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
1037
1038 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
Dave Hansen893473d2008-12-09 08:21:36 +00001039 bootmem_vaddr = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001040 bootmap_pages << PAGE_SHIFT,
1041 PAGE_SIZE, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001042
Dave Hansen0be210f2008-12-09 08:21:35 +00001043 dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
Dave Hansen4a618662008-11-24 12:02:35 +00001044
Dave Hansen0be210f2008-12-09 08:21:35 +00001045 init_bootmem_node(NODE_DATA(nid),
1046 __pa(bootmem_vaddr) >> PAGE_SHIFT,
Dave Hansen4a618662008-11-24 12:02:35 +00001047 start_pfn, end_pfn);
1048
1049 free_bootmem_with_active_regions(nid, end_pfn);
1050 /*
1051 * Be very careful about moving this around. Future
Dave Hansen893473d2008-12-09 08:21:36 +00001052 * calls to careful_zallocation() depend on this getting
Dave Hansen4a618662008-11-24 12:02:35 +00001053 * done correctly.
1054 */
1055 mark_reserved_regions_for_nid(nid);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +00001056 sparse_memory_present_with_active_regions(nid);
Dave Hansen4a618662008-11-24 12:02:35 +00001057 }
Benjamin Herrenschmidtd3f62042009-06-02 21:16:38 +00001058
1059 init_bootmem_done = 1;
Anton Blanchard25863de2010-04-26 15:32:43 +00001060
1061 /*
1062 * Now bootmem is initialised we can create the node to cpumask
1063 * lookup tables and setup the cpu callback to populate them.
1064 */
1065 setup_node_to_cpumask_map();
1066
1067 register_cpu_notifier(&ppc64_numa_nb);
1068 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
1069 (void *)(unsigned long)boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072void __init paging_init(void)
1073{
Mel Gorman6391af12006-10-11 01:20:39 -07001074 unsigned long max_zone_pfns[MAX_NR_ZONES];
1075 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Yinghai Lu95f72d12010-07-12 14:36:09 +10001076 max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
Mel Gormanc67c3cb2006-09-27 01:49:49 -07001077 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static int __init early_numa(char *p)
1081{
1082 if (!p)
1083 return 0;
1084
1085 if (strstr(p, "off"))
1086 numa_enabled = 0;
1087
1088 if (strstr(p, "debug"))
1089 numa_debug = 1;
1090
Balbir Singh1daa6d02008-02-01 15:57:31 +11001091 p = strstr(p, "fake=");
1092 if (p)
1093 cmdline = p + strlen("fake=");
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096}
1097early_param("numa", early_numa);
Mike Kravetz237a09892005-12-05 12:06:42 -08001098
1099#ifdef CONFIG_MEMORY_HOTPLUG
1100/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001101 * Find the node associated with a hot added memory section for
1102 * memory represented in the device tree by the property
1103 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
Nathan Fontenot0db93602008-07-03 13:25:08 +10001104 */
1105static int hot_add_drconf_scn_to_nid(struct device_node *memory,
1106 unsigned long scn_addr)
1107{
1108 const u32 *dm;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001109 unsigned int drconf_cell_cnt, rc;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001110 unsigned long lmb_size;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001111 struct assoc_arrays aa;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001112 int nid = -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001113
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001114 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1115 if (!drconf_cell_cnt)
1116 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001117
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001118 lmb_size = of_get_lmb_size(memory);
1119 if (!lmb_size)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001120 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001121
1122 rc = of_get_assoc_arrays(memory, &aa);
1123 if (rc)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001124 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001125
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001126 for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
Nathan Fontenot0db93602008-07-03 13:25:08 +10001127 struct of_drconf_cell drmem;
1128
1129 read_drconf_cell(&drmem, &dm);
1130
1131 /* skip this block if it is reserved or not assigned to
1132 * this partition */
1133 if ((drmem.flags & DRCONF_MEM_RESERVED)
1134 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
1135 continue;
1136
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001137 if ((scn_addr < drmem.base_addr)
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001138 || (scn_addr >= (drmem.base_addr + lmb_size)))
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001139 continue;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001140
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001141 nid = of_drconf_to_nid_single(&drmem, &aa);
1142 break;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001143 }
1144
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001145 return nid;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001146}
1147
1148/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001149 * Find the node associated with a hot added memory section for memory
1150 * represented in the device tree as a node (i.e. memory@XXXX) for
Yinghai Lu95f72d12010-07-12 14:36:09 +10001151 * each memblock.
Mike Kravetz237a09892005-12-05 12:06:42 -08001152 */
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001153int hot_add_node_scn_to_nid(unsigned long scn_addr)
Mike Kravetz237a09892005-12-05 12:06:42 -08001154{
Anton Blanchard94db7c52011-08-10 20:44:22 +00001155 struct device_node *memory;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001156 int nid = -1;
Mike Kravetz237a09892005-12-05 12:06:42 -08001157
Anton Blanchard94db7c52011-08-10 20:44:22 +00001158 for_each_node_by_type(memory, "memory") {
Mike Kravetz237a09892005-12-05 12:06:42 -08001159 unsigned long start, size;
Mike Kravetzb226e462005-12-16 14:30:35 -08001160 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001161 const unsigned int *memcell_buf;
Mike Kravetz237a09892005-12-05 12:06:42 -08001162 unsigned int len;
1163
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001164 memcell_buf = of_get_property(memory, "reg", &len);
Mike Kravetz237a09892005-12-05 12:06:42 -08001165 if (!memcell_buf || len <= 0)
1166 continue;
1167
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001168 /* ranges in cell */
1169 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Mike Kravetz237a09892005-12-05 12:06:42 -08001170
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001171 while (ranges--) {
1172 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1173 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1174
1175 if ((scn_addr < start) || (scn_addr >= (start + size)))
1176 continue;
1177
1178 nid = of_node_to_nid_single(memory);
1179 break;
Mike Kravetz237a09892005-12-05 12:06:42 -08001180 }
1181
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001182 if (nid >= 0)
1183 break;
Mike Kravetz237a09892005-12-05 12:06:42 -08001184 }
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001185
Anton Blanchard60831842011-08-10 20:44:21 +00001186 of_node_put(memory);
1187
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001188 return nid;
Mike Kravetz237a09892005-12-05 12:06:42 -08001189}
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001190
1191/*
1192 * Find the node associated with a hot added memory section. Section
Yinghai Lu95f72d12010-07-12 14:36:09 +10001193 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1194 * sections are fully contained within a single MEMBLOCK.
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001195 */
1196int hot_add_scn_to_nid(unsigned long scn_addr)
1197{
1198 struct device_node *memory = NULL;
1199 int nid, found = 0;
1200
1201 if (!numa_enabled || (min_common_depth < 0))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001202 return first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001203
1204 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1205 if (memory) {
1206 nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
1207 of_node_put(memory);
1208 } else {
1209 nid = hot_add_node_scn_to_nid(scn_addr);
1210 }
1211
1212 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001213 nid = first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001214
1215 if (NODE_DATA(nid)->node_spanned_pages)
1216 return nid;
1217
1218 for_each_online_node(nid) {
1219 if (NODE_DATA(nid)->node_spanned_pages) {
1220 found = 1;
1221 break;
1222 }
1223 }
1224
1225 BUG_ON(!found);
1226 return nid;
1227}
1228
Nishanth Aravamudancd342062010-10-26 17:35:12 +00001229static u64 hot_add_drconf_memory_max(void)
1230{
1231 struct device_node *memory = NULL;
1232 unsigned int drconf_cell_cnt = 0;
1233 u64 lmb_size = 0;
1234 const u32 *dm = 0;
1235
1236 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1237 if (memory) {
1238 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1239 lmb_size = of_get_lmb_size(memory);
1240 of_node_put(memory);
1241 }
1242 return lmb_size * drconf_cell_cnt;
1243}
1244
1245/*
1246 * memory_hotplug_max - return max address of memory that may be added
1247 *
1248 * This is currently only used on systems that support drconfig memory
1249 * hotplug.
1250 */
1251u64 memory_hotplug_max(void)
1252{
1253 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1254}
Mike Kravetz237a09892005-12-05 12:06:42 -08001255#endif /* CONFIG_MEMORY_HOTPLUG */
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001256
Jesse Larrewbd034032011-01-20 19:00:51 +00001257/* Virtual Processor Home Node (VPHN) support */
Jesse Larrew39bf9902010-12-17 22:07:47 +00001258#ifdef CONFIG_PPC_SPLPAR
Nathan Fontenot30c05352013-04-24 06:02:13 +00001259struct topology_update_data {
1260 struct topology_update_data *next;
1261 unsigned int cpu;
1262 int old_nid;
1263 int new_nid;
1264};
1265
Anton Blanchard5de16692011-01-29 12:24:34 +00001266static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001267static cpumask_t cpu_associativity_changes_mask;
1268static int vphn_enabled;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001269static int prrn_enabled;
1270static void reset_topology_timer(void);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001271
1272/*
1273 * Store the current values of the associativity change counters in the
1274 * hypervisor.
1275 */
1276static void setup_cpu_associativity_change_counters(void)
1277{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001278 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001279
Anton Blanchard5de16692011-01-29 12:24:34 +00001280 /* The VPHN feature supports a maximum of 8 reference points */
1281 BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1282
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001283 for_each_possible_cpu(cpu) {
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001284 int i;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001285 u8 *counts = vphn_cpu_change_counts[cpu];
1286 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1287
Anton Blanchard5de16692011-01-29 12:24:34 +00001288 for (i = 0; i < distance_ref_points_depth; i++)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001289 counts[i] = hypervisor_counts[i];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001290 }
1291}
1292
1293/*
1294 * The hypervisor maintains a set of 8 associativity change counters in
1295 * the VPA of each cpu that correspond to the associativity levels in the
1296 * ibm,associativity-reference-points property. When an associativity
1297 * level changes, the corresponding counter is incremented.
1298 *
1299 * Set a bit in cpu_associativity_changes_mask for each cpu whose home
1300 * node associativity levels have changed.
1301 *
1302 * Returns the number of cpus with unhandled associativity changes.
1303 */
1304static int update_cpu_associativity_changes_mask(void)
1305{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001306 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001307 cpumask_t *changes = &cpu_associativity_changes_mask;
1308
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001309 for_each_possible_cpu(cpu) {
1310 int i, changed = 0;
1311 u8 *counts = vphn_cpu_change_counts[cpu];
1312 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1313
Anton Blanchard5de16692011-01-29 12:24:34 +00001314 for (i = 0; i < distance_ref_points_depth; i++) {
Anton Blanchardd69043e2011-01-29 12:26:19 +00001315 if (hypervisor_counts[i] != counts[i]) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001316 counts[i] = hypervisor_counts[i];
1317 changed = 1;
1318 }
1319 }
1320 if (changed) {
1321 cpumask_set_cpu(cpu, changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001322 }
1323 }
1324
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001325 return cpumask_weight(changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001326}
1327
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001328/*
1329 * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1330 * the complete property we have to add the length in the first cell.
1331 */
1332#define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001333
1334/*
1335 * Convert the associativity domain numbers returned from the hypervisor
1336 * to the sequence they would appear in the ibm,associativity property.
1337 */
1338static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
1339{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001340 int i, nr_assoc_doms = 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001341 const u16 *field = (const u16*) packed;
1342
1343#define VPHN_FIELD_UNUSED (0xffff)
1344#define VPHN_FIELD_MSB (0x8000)
1345#define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
1346
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001347 for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001348 if (*field == VPHN_FIELD_UNUSED) {
1349 /* All significant fields processed, and remaining
1350 * fields contain the reserved value of all 1's.
1351 * Just store them.
1352 */
1353 unpacked[i] = *((u32*)field);
1354 field += 2;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001355 } else if (*field & VPHN_FIELD_MSB) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001356 /* Data is in the lower 15 bits of this field */
1357 unpacked[i] = *field & VPHN_FIELD_MASK;
1358 field++;
1359 nr_assoc_doms++;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001360 } else {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001361 /* Data is in the lower 15 bits of this field
1362 * concatenated with the next 16 bit field
1363 */
1364 unpacked[i] = *((u32*)field);
1365 field += 2;
1366 nr_assoc_doms++;
1367 }
1368 }
1369
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001370 /* The first cell contains the length of the property */
1371 unpacked[0] = nr_assoc_doms;
1372
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001373 return nr_assoc_doms;
1374}
1375
1376/*
1377 * Retrieve the new associativity information for a virtual processor's
1378 * home node.
1379 */
1380static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
1381{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001382 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001383 long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1384 u64 flags = 1;
1385 int hwcpu = get_hard_smp_processor_id(cpu);
1386
1387 rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
1388 vphn_unpack_associativity(retbuf, associativity);
1389
1390 return rc;
1391}
1392
1393static long vphn_get_associativity(unsigned long cpu,
1394 unsigned int *associativity)
1395{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001396 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001397
1398 rc = hcall_vphn(cpu, associativity);
1399
1400 switch (rc) {
1401 case H_FUNCTION:
1402 printk(KERN_INFO
1403 "VPHN is not supported. Disabling polling...\n");
1404 stop_topology_update();
1405 break;
1406 case H_HARDWARE:
1407 printk(KERN_ERR
1408 "hcall_vphn() experienced a hardware fault "
1409 "preventing VPHN. Disabling polling...\n");
1410 stop_topology_update();
1411 }
1412
1413 return rc;
1414}
1415
1416/*
Nathan Fontenot30c05352013-04-24 06:02:13 +00001417 * Update the CPU maps and sysfs entries for a single CPU when its NUMA
1418 * characteristics change. This function doesn't perform any locking and is
1419 * only safe to call from stop_machine().
1420 */
1421static int update_cpu_topology(void *data)
1422{
1423 struct topology_update_data *update;
1424 unsigned long cpu;
1425
1426 if (!data)
1427 return -EINVAL;
1428
1429 cpu = get_cpu();
1430
1431 for (update = data; update; update = update->next) {
1432 if (cpu != update->cpu)
1433 continue;
1434
1435 unregister_cpu_under_node(update->cpu, update->old_nid);
1436 unmap_cpu_from_node(update->cpu);
1437 map_cpu_to_node(update->cpu, update->new_nid);
Jesse Larrew176bbf12013-04-24 06:03:48 +00001438 vdso_getcpu_init();
Nathan Fontenot30c05352013-04-24 06:02:13 +00001439 register_cpu_under_node(update->cpu, update->new_nid);
1440 }
1441
1442 return 0;
1443}
1444
1445/*
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001446 * Update the node maps and sysfs entries for each cpu whose home node
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001447 * has changed. Returns 1 when the topology has changed, and 0 otherwise.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001448 */
1449int arch_update_cpu_topology(void)
1450{
Nathan Fontenot30c05352013-04-24 06:02:13 +00001451 unsigned int cpu, changed = 0;
1452 struct topology_update_data *updates, *ud;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001453 unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
Jesse Larrew176bbf12013-04-24 06:03:48 +00001454 cpumask_t updated_cpus;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001455 struct device *dev;
Nathan Fontenot30c05352013-04-24 06:02:13 +00001456 int weight, i = 0;
1457
1458 weight = cpumask_weight(&cpu_associativity_changes_mask);
1459 if (!weight)
1460 return 0;
1461
1462 updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
1463 if (!updates)
1464 return 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001465
Jesse Larrew176bbf12013-04-24 06:03:48 +00001466 cpumask_clear(&updated_cpus);
1467
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001468 for_each_cpu(cpu, &cpu_associativity_changes_mask) {
Nathan Fontenot30c05352013-04-24 06:02:13 +00001469 ud = &updates[i++];
1470 ud->cpu = cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001471 vphn_get_associativity(cpu, associativity);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001472 ud->new_nid = associativity_to_nid(associativity);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001473
Nathan Fontenot30c05352013-04-24 06:02:13 +00001474 if (ud->new_nid < 0 || !node_online(ud->new_nid))
1475 ud->new_nid = first_online_node;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001476
Nathan Fontenot30c05352013-04-24 06:02:13 +00001477 ud->old_nid = numa_cpu_lookup_table[cpu];
Jesse Larrew176bbf12013-04-24 06:03:48 +00001478 cpumask_set_cpu(cpu, &updated_cpus);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001479
Nathan Fontenot30c05352013-04-24 06:02:13 +00001480 if (i < weight)
1481 ud->next = &updates[i];
1482 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001483
Jesse Larrew176bbf12013-04-24 06:03:48 +00001484 stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001485
1486 for (ud = &updates[0]; ud; ud = ud->next) {
1487 dev = get_cpu_device(ud->cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001488 if (dev)
1489 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001490 cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001491 changed = 1;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001492 }
1493
Nathan Fontenot30c05352013-04-24 06:02:13 +00001494 kfree(updates);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001495 return changed;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001496}
1497
1498static void topology_work_fn(struct work_struct *work)
1499{
1500 rebuild_sched_domains();
1501}
1502static DECLARE_WORK(topology_work, topology_work_fn);
1503
1504void topology_schedule_update(void)
1505{
1506 schedule_work(&topology_work);
1507}
1508
1509static void topology_timer_fn(unsigned long ignored)
1510{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001511 if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001512 topology_schedule_update();
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001513 else if (vphn_enabled) {
1514 if (update_cpu_associativity_changes_mask() > 0)
1515 topology_schedule_update();
1516 reset_topology_timer();
1517 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001518}
1519static struct timer_list topology_timer =
1520 TIMER_INITIALIZER(topology_timer_fn, 0, 0);
1521
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001522static void reset_topology_timer(void)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001523{
1524 topology_timer.data = 0;
1525 topology_timer.expires = jiffies + 60 * HZ;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001526 mod_timer(&topology_timer, topology_timer.expires);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001527}
1528
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001529static void stage_topology_update(int core_id)
1530{
1531 cpumask_or(&cpu_associativity_changes_mask,
1532 &cpu_associativity_changes_mask, cpu_sibling_mask(core_id));
1533 reset_topology_timer();
1534}
1535
1536static int dt_update_callback(struct notifier_block *nb,
1537 unsigned long action, void *data)
1538{
1539 struct of_prop_reconfig *update;
1540 int rc = NOTIFY_DONE;
1541
1542 switch (action) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001543 case OF_RECONFIG_UPDATE_PROPERTY:
1544 update = (struct of_prop_reconfig *)data;
Nathan Fontenot30c05352013-04-24 06:02:13 +00001545 if (!of_prop_cmp(update->dn->type, "cpu") &&
1546 !of_prop_cmp(update->prop->name, "ibm,associativity")) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001547 u32 core_id;
1548 of_property_read_u32(update->dn, "reg", &core_id);
1549 stage_topology_update(core_id);
1550 rc = NOTIFY_OK;
1551 }
1552 break;
1553 }
1554
1555 return rc;
1556}
1557
1558static struct notifier_block dt_update_nb = {
1559 .notifier_call = dt_update_callback,
1560};
1561
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001562/*
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001563 * Start polling for associativity changes.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001564 */
1565int start_topology_update(void)
1566{
1567 int rc = 0;
1568
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001569 if (firmware_has_feature(FW_FEATURE_PRRN)) {
1570 if (!prrn_enabled) {
1571 prrn_enabled = 1;
1572 vphn_enabled = 0;
1573 rc = of_reconfig_notifier_register(&dt_update_nb);
1574 }
Jesse Larrewb7abef02013-04-24 06:05:22 +00001575 } else if (firmware_has_feature(FW_FEATURE_VPHN) &&
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001576 get_lppaca()->shared_proc) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001577 if (!vphn_enabled) {
1578 prrn_enabled = 0;
1579 vphn_enabled = 1;
1580 setup_cpu_associativity_change_counters();
1581 init_timer_deferrable(&topology_timer);
1582 reset_topology_timer();
1583 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001584 }
1585
1586 return rc;
1587}
1588__initcall(start_topology_update);
1589
1590/*
1591 * Disable polling for VPHN associativity changes.
1592 */
1593int stop_topology_update(void)
1594{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001595 int rc = 0;
1596
1597 if (prrn_enabled) {
1598 prrn_enabled = 0;
1599 rc = of_reconfig_notifier_unregister(&dt_update_nb);
1600 } else if (vphn_enabled) {
1601 vphn_enabled = 0;
1602 rc = del_timer_sync(&topology_timer);
1603 }
1604
1605 return rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001606}
Jesse Larrew39bf9902010-12-17 22:07:47 +00001607#endif /* CONFIG_PPC_SPLPAR */