blob: 88c0425dc0a88f41f61c38b4d01888ec468a937c [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>
Linus Torvalds191a7122013-04-29 19:14:20 -070029#include <linux/slab.h>
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110030#include <asm/sparsemem.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080031#include <asm/prom.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110032#include <asm/smp.h>
Jesse Larrew9eff1a32010-12-01 12:31:15 +000033#include <asm/firmware.h>
34#include <asm/paca.h>
Jesse Larrew39bf9902010-12-17 22:07:47 +000035#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010036#include <asm/setup.h>
Jesse Larrew176bbf12013-04-24 06:03:48 +000037#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static int numa_enabled = 1;
40
Balbir Singh1daa6d02008-02-01 15:57:31 +110041static char *cmdline __initdata;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int numa_debug;
44#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
45
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110046int numa_cpu_lookup_table[NR_CPUS];
Anton Blanchard25863de2010-04-26 15:32:43 +000047cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct pglist_data *node_data[MAX_NUMNODES];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110049
50EXPORT_SYMBOL(numa_cpu_lookup_table);
Anton Blanchard25863de2010-04-26 15:32:43 +000051EXPORT_SYMBOL(node_to_cpumask_map);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110052EXPORT_SYMBOL(node_data);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int min_common_depth;
Mike Kravetz237a0982005-12-05 12:06:42 -080055static int n_mem_addr_cells, n_mem_size_cells;
Anton Blanchard41eab6f2010-05-16 20:22:31 +000056static int form1_affinity;
57
58#define MAX_DISTANCE_REF_POINTS 4
59static int distance_ref_points_depth;
60static const unsigned int *distance_ref_points;
61static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Anton Blanchard25863de2010-04-26 15:32:43 +000063/*
64 * Allocate node_to_cpumask_map based on number of available nodes
65 * Requires node_possible_map to be valid.
66 *
Wanlong Gao95129382012-01-12 17:20:09 -080067 * Note: cpumask_of_node() is not valid until after this is done.
Anton Blanchard25863de2010-04-26 15:32:43 +000068 */
69static void __init setup_node_to_cpumask_map(void)
70{
Cody P Schaferf9d531b2013-04-29 15:08:03 -070071 unsigned int node;
Anton Blanchard25863de2010-04-26 15:32:43 +000072
73 /* setup nr_node_ids if not done yet */
Cody P Schaferf9d531b2013-04-29 15:08:03 -070074 if (nr_node_ids == MAX_NUMNODES)
75 setup_nr_node_ids();
Anton Blanchard25863de2010-04-26 15:32:43 +000076
77 /* allocate the map */
78 for (node = 0; node < nr_node_ids; node++)
79 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
80
81 /* cpumask_of_node() will now work */
82 dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
83}
84
Stephen Rothwell55671f32013-03-25 18:44:44 +000085static int __init fake_numa_create_new_node(unsigned long end_pfn,
Balbir Singh1daa6d02008-02-01 15:57:31 +110086 unsigned int *nid)
87{
88 unsigned long long mem;
89 char *p = cmdline;
90 static unsigned int fake_nid;
91 static unsigned long long curr_boundary;
92
93 /*
94 * Modify node id, iff we started creating NUMA nodes
95 * We want to continue from where we left of the last time
96 */
97 if (fake_nid)
98 *nid = fake_nid;
99 /*
100 * In case there are no more arguments to parse, the
101 * node_id should be the same as the last fake node id
102 * (we've handled this above).
103 */
104 if (!p)
105 return 0;
106
107 mem = memparse(p, &p);
108 if (!mem)
109 return 0;
110
111 if (mem < curr_boundary)
112 return 0;
113
114 curr_boundary = mem;
115
116 if ((end_pfn << PAGE_SHIFT) > mem) {
117 /*
118 * Skip commas and spaces
119 */
120 while (*p == ',' || *p == ' ' || *p == '\t')
121 p++;
122
123 cmdline = p;
124 fake_nid++;
125 *nid = fake_nid;
126 dbg("created new fake_node with id %d\n", fake_nid);
127 return 1;
128 }
129 return 0;
130}
131
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000132/*
Tejun Heo5dfe8662011-07-14 09:46:10 +0200133 * get_node_active_region - Return active region containing pfn
Jon Tollefsone8170372008-10-16 18:59:43 +0000134 * Active range returned is empty if none found.
Tejun Heo5dfe8662011-07-14 09:46:10 +0200135 * @pfn: The page to return the region for
136 * @node_ar: Returned set to the active region containing @pfn
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000137 */
Tejun Heo5dfe8662011-07-14 09:46:10 +0200138static void __init get_node_active_region(unsigned long pfn,
139 struct node_active_region *node_ar)
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000140{
Tejun Heo5dfe8662011-07-14 09:46:10 +0200141 unsigned long start_pfn, end_pfn;
142 int i, nid;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000143
Tejun Heo5dfe8662011-07-14 09:46:10 +0200144 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
145 if (pfn >= start_pfn && pfn < end_pfn) {
146 node_ar->nid = nid;
147 node_ar->start_pfn = start_pfn;
148 node_ar->end_pfn = end_pfn;
149 break;
150 }
151 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000152}
153
Jesse Larrew39bf9902010-12-17 22:07:47 +0000154static void map_cpu_to_node(int cpu, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 numa_cpu_lookup_table[cpu] = node;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100157
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600158 dbg("adding cpu %d to node %d\n", cpu, node);
159
Anton Blanchard25863de2010-04-26 15:32:43 +0000160 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
161 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jesse Larrew39bf9902010-12-17 22:07:47 +0000164#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void unmap_cpu_from_node(unsigned long cpu)
166{
167 int node = numa_cpu_lookup_table[cpu];
168
169 dbg("removing cpu %lu from node %d\n", cpu, node);
170
Anton Blanchard25863de2010-04-26 15:32:43 +0000171 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
Anton Blanchard429f4d82011-01-29 12:37:16 +0000172 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 } else {
174 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
175 cpu, node);
176 }
177}
Jesse Larrew39bf9902010-12-17 22:07:47 +0000178#endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* must hold reference to node during call */
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000181static const int *of_get_associativity(struct device_node *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000183 return of_get_property(dev, "ibm,associativity", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Chandrucf000852008-08-30 00:28:16 +1000186/*
187 * Returns the property linux,drconf-usable-memory if
188 * it exists (the property exists only in kexec/kdump kernels,
189 * added by kexec-tools)
190 */
191static const u32 *of_get_usable_memory(struct device_node *memory)
192{
193 const u32 *prop;
194 u32 len;
195 prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
196 if (!prop || len < sizeof(unsigned int))
197 return 0;
198 return prop;
199}
200
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000201int __node_distance(int a, int b)
202{
203 int i;
204 int distance = LOCAL_DISTANCE;
205
206 if (!form1_affinity)
Vaidyanathan Srinivasan7122bee2013-03-22 05:49:35 +0000207 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000208
209 for (i = 0; i < distance_ref_points_depth; i++) {
210 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
211 break;
212
213 /* Double the distance for each NUMA level */
214 distance *= 2;
215 }
216
217 return distance;
218}
219
220static void initialize_distance_lookup_table(int nid,
221 const unsigned int *associativity)
222{
223 int i;
224
225 if (!form1_affinity)
226 return;
227
228 for (i = 0; i < distance_ref_points_depth; i++) {
229 distance_lookup_table[nid][i] =
230 associativity[distance_ref_points[i]];
231 }
232}
233
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600234/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
235 * info is found.
236 */
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000237static int associativity_to_nid(const unsigned int *associativity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600239 int nid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (min_common_depth == -1)
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000244 if (associativity[0] >= min_common_depth)
245 nid = associativity[min_common_depth];
Nathan Lynchbc16a752006-03-20 18:36:15 -0600246
247 /* POWER4 LPAR uses 0xffff as invalid node */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600248 if (nid == 0xffff || nid >= MAX_NUMNODES)
249 nid = -1;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000250
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000251 if (nid > 0 && associativity[0] >= distance_ref_points_depth)
252 initialize_distance_lookup_table(nid, associativity);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000253
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600254out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600255 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Jesse Larrew9eff1a32010-12-01 12:31:15 +0000258/* Returns the nid associated with the given device tree node,
259 * or -1 if not found.
260 */
261static int of_node_to_nid_single(struct device_node *device)
262{
263 int nid = -1;
264 const unsigned int *tmp;
265
266 tmp = of_get_associativity(device);
267 if (tmp)
268 nid = associativity_to_nid(tmp);
269 return nid;
270}
271
Jeremy Kerr953039c2006-05-01 12:16:12 -0700272/* Walk the device tree upwards, looking for an associativity id */
273int of_node_to_nid(struct device_node *device)
274{
275 struct device_node *tmp;
276 int nid = -1;
277
278 of_node_get(device);
279 while (device) {
280 nid = of_node_to_nid_single(device);
281 if (nid != -1)
282 break;
283
284 tmp = device;
285 device = of_get_parent(tmp);
286 of_node_put(tmp);
287 }
288 of_node_put(device);
289
290 return nid;
291}
292EXPORT_SYMBOL_GPL(of_node_to_nid);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int __init find_min_common_depth(void)
295{
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000296 int depth;
Michael Ellermane70606e2011-04-10 20:42:05 +0000297 struct device_node *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000299 if (firmware_has_feature(FW_FEATURE_OPAL))
300 root = of_find_node_by_path("/ibm,opal");
301 else
302 root = of_find_node_by_path("/rtas");
Michael Ellermane70606e2011-04-10 20:42:05 +0000303 if (!root)
304 root = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000307 * This property is a set of 32-bit integers, each representing
308 * an index into the ibm,associativity nodes.
309 *
310 * With form 0 affinity the first integer is for an SMP configuration
311 * (should be all 0's) and the second is for a normal NUMA
312 * configuration. We have only one level of NUMA.
313 *
314 * With form 1 affinity the first integer is the most significant
315 * NUMA boundary and the following are progressively less significant
316 * boundaries. There can be more than one level of NUMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Michael Ellermane70606e2011-04-10 20:42:05 +0000318 distance_ref_points = of_get_property(root,
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000319 "ibm,associativity-reference-points",
320 &distance_ref_points_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000322 if (!distance_ref_points) {
323 dbg("NUMA: ibm,associativity-reference-points not found.\n");
324 goto err;
325 }
326
327 distance_ref_points_depth /= sizeof(int);
328
Nathan Fontenot8002b0c2013-04-24 05:58:23 +0000329 if (firmware_has_feature(FW_FEATURE_OPAL) ||
330 firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
331 dbg("Using form 1 affinity\n");
Dipankar Sarma1c8ee732011-10-28 04:25:32 +0000332 form1_affinity = 1;
Anton Blanchard4b83c332010-04-07 15:33:44 +0000333 }
334
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000335 if (form1_affinity) {
336 depth = distance_ref_points[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000338 if (distance_ref_points_depth < 2) {
339 printk(KERN_WARNING "NUMA: "
340 "short ibm,associativity-reference-points\n");
341 goto err;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000344 depth = distance_ref_points[1];
345 }
346
347 /*
348 * Warn and cap if the hardware supports more than
349 * MAX_DISTANCE_REF_POINTS domains.
350 */
351 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
352 printk(KERN_WARNING "NUMA: distance array capped at "
353 "%d entries\n", MAX_DISTANCE_REF_POINTS);
354 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
355 }
356
Michael Ellermane70606e2011-04-10 20:42:05 +0000357 of_node_put(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return depth;
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000359
360err:
Michael Ellermane70606e2011-04-10 20:42:05 +0000361 of_node_put(root);
Anton Blanchard41eab6f2010-05-16 20:22:31 +0000362 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800365static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct device_node *memory = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 memory = of_find_node_by_type(memory, "memory");
Paul Mackerras54c23312005-12-05 15:50:39 +1100370 if (!memory)
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800371 panic("numa.c: No memory nodes found!");
Paul Mackerras54c23312005-12-05 15:50:39 +1100372
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000373 *n_addr_cells = of_n_addr_cells(memory);
Stephen Rothwell9213fee2007-04-03 10:57:48 +1000374 *n_size_cells = of_n_size_cells(memory);
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800375 of_node_put(memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
David Rientjes2011b1d2011-12-08 12:46:37 +0000378static unsigned long read_n_cells(int n, const unsigned int **buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long result = 0;
381
382 while (n--) {
383 result = (result << 32) | **buf;
384 (*buf)++;
385 }
386 return result;
387}
388
Nathan Fontenot83426812008-07-03 13:35:54 +1000389/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000390 * Read the next memblock list entry from the ibm,dynamic-memory property
Nathan Fontenot83426812008-07-03 13:35:54 +1000391 * and return the information in the provided of_drconf_cell structure.
392 */
393static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
394{
395 const u32 *cp;
396
397 drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
398
399 cp = *cellp;
400 drmem->drc_index = cp[0];
401 drmem->reserved = cp[1];
402 drmem->aa_index = cp[2];
403 drmem->flags = cp[3];
404
405 *cellp = cp + 4;
406}
407
408/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300409 * Retrieve and validate the ibm,dynamic-memory property of the device tree.
Nathan Fontenot83426812008-07-03 13:35:54 +1000410 *
Yinghai Lu95f72d12010-07-12 14:36:09 +1000411 * The layout of the ibm,dynamic-memory property is a number N of memblock
412 * list entries followed by N memblock list entries. Each memblock list entry
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300413 * contains information as laid out in the of_drconf_cell struct above.
Nathan Fontenot83426812008-07-03 13:35:54 +1000414 */
415static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
416{
417 const u32 *prop;
418 u32 len, entries;
419
420 prop = of_get_property(memory, "ibm,dynamic-memory", &len);
421 if (!prop || len < sizeof(unsigned int))
422 return 0;
423
424 entries = *prop++;
425
426 /* Now that we know the number of entries, revalidate the size
427 * of the property read in to ensure we have everything
428 */
429 if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
430 return 0;
431
432 *dm = prop;
433 return entries;
434}
435
436/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300437 * Retrieve and validate the ibm,lmb-size property for drconf memory
Nathan Fontenot83426812008-07-03 13:35:54 +1000438 * from the device tree.
439 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000440static u64 of_get_lmb_size(struct device_node *memory)
Nathan Fontenot83426812008-07-03 13:35:54 +1000441{
442 const u32 *prop;
443 u32 len;
444
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000445 prop = of_get_property(memory, "ibm,lmb-size", &len);
Nathan Fontenot83426812008-07-03 13:35:54 +1000446 if (!prop || len < sizeof(unsigned int))
447 return 0;
448
449 return read_n_cells(n_mem_size_cells, &prop);
450}
451
452struct assoc_arrays {
453 u32 n_arrays;
454 u32 array_sz;
455 const u32 *arrays;
456};
457
458/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300459 * Retrieve and validate the list of associativity arrays for drconf
Nathan Fontenot83426812008-07-03 13:35:54 +1000460 * memory from the ibm,associativity-lookup-arrays property of the
461 * device tree..
462 *
463 * The layout of the ibm,associativity-lookup-arrays property is a number N
464 * indicating the number of associativity arrays, followed by a number M
465 * indicating the size of each associativity array, followed by a list
466 * of N associativity arrays.
467 */
468static int of_get_assoc_arrays(struct device_node *memory,
469 struct assoc_arrays *aa)
470{
471 const u32 *prop;
472 u32 len;
473
474 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
475 if (!prop || len < 2 * sizeof(unsigned int))
476 return -1;
477
478 aa->n_arrays = *prop++;
479 aa->array_sz = *prop++;
480
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800481 /* Now that we know the number of arrays and size of each array,
Nathan Fontenot83426812008-07-03 13:35:54 +1000482 * revalidate the size of the property read in.
483 */
484 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
485 return -1;
486
487 aa->arrays = prop;
488 return 0;
489}
490
491/*
492 * This is like of_node_to_nid_single() for memory represented in the
493 * ibm,dynamic-reconfiguration-memory node.
494 */
495static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
496 struct assoc_arrays *aa)
497{
498 int default_nid = 0;
499 int nid = default_nid;
500 int index;
501
502 if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
503 !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
504 drmem->aa_index < aa->n_arrays) {
505 index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
506 nid = aa->arrays[index];
507
508 if (nid == 0xffff || nid >= MAX_NUMNODES)
509 nid = default_nid;
510 }
511
512 return nid;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
516 * Figure out to which domain a cpu belongs and stick it there.
517 * Return the id of the domain used.
518 */
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600519static int __cpuinit numa_setup_cpu(unsigned long lcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Nathan Lynchcf950b72006-03-20 18:35:45 -0600521 int nid = 0;
Milton Miller8b16cd22009-01-08 02:19:45 +0000522 struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (!cpu) {
525 WARN_ON(1);
526 goto out;
527 }
528
Jeremy Kerr953039c2006-05-01 12:16:12 -0700529 nid = of_node_to_nid_single(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600531 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -0800532 nid = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600534 map_cpu_to_node(lcpu, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 of_node_put(cpu);
537
Nathan Lynchcf950b72006-03-20 18:35:45 -0600538 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700541static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long action,
543 void *hcpu)
544{
545 unsigned long lcpu = (unsigned long)hcpu;
546 int ret = NOTIFY_DONE;
547
548 switch (action) {
549 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700550 case CPU_UP_PREPARE_FROZEN:
Nathan Lynch2b261222006-03-20 18:37:15 -0600551 numa_setup_cpu(lcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ret = NOTIFY_OK;
553 break;
554#ifdef CONFIG_HOTPLUG_CPU
555 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700556 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700558 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unmap_cpu_from_node(lcpu);
560 break;
561 ret = NOTIFY_OK;
562#endif
563 }
564 return ret;
565}
566
567/*
568 * Check and possibly modify a memory region to enforce the memory limit.
569 *
570 * Returns the size the region should have to enforce the memory limit.
571 * This will either be the original value of size, a truncated value,
572 * or zero. If the returned value of size is 0 the region should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300573 * discarded as it lies wholly above the memory limit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100575static unsigned long __init numa_enforce_memory_limit(unsigned long start,
576 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000579 * We use memblock_end_of_DRAM() in here instead of memory_limit because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * we've already adjusted it for the limit and it takes care of
Milton Millerfe552492008-10-20 15:37:04 +0000581 * having memory holes below the limit. Also, in the case of
582 * iommu_is_off, memory_limit is not set but is implicitly enforced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Yinghai Lu95f72d12010-07-12 14:36:09 +1000585 if (start + size <= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return size;
587
Yinghai Lu95f72d12010-07-12 14:36:09 +1000588 if (start >= memblock_end_of_DRAM())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590
Yinghai Lu95f72d12010-07-12 14:36:09 +1000591 return memblock_end_of_DRAM() - start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Paul Mackerras02045682006-11-29 22:27:42 +1100594/*
Chandrucf000852008-08-30 00:28:16 +1000595 * Reads the counter for a given entry in
596 * linux,drconf-usable-memory property
597 */
598static inline int __init read_usm_ranges(const u32 **usm)
599{
600 /*
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000601 * For each lmb in ibm,dynamic-memory a corresponding
Chandrucf000852008-08-30 00:28:16 +1000602 * entry in linux,drconf-usable-memory property contains
603 * a counter followed by that many (base, size) duple.
604 * read the counter from linux,drconf-usable-memory
605 */
606 return read_n_cells(n_mem_size_cells, usm);
607}
608
609/*
Paul Mackerras02045682006-11-29 22:27:42 +1100610 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
611 * node. This assumes n_mem_{addr,size}_cells have been set.
612 */
613static void __init parse_drconf_memory(struct device_node *memory)
614{
Michael Neuling82b25212012-06-19 20:01:45 +0000615 const u32 *uninitialized_var(dm), *usm;
Chandrucf000852008-08-30 00:28:16 +1000616 unsigned int n, rc, ranges, is_kexec_kdump = 0;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000617 unsigned long lmb_size, base, size, sz;
Nathan Fontenot83426812008-07-03 13:35:54 +1000618 int nid;
Benjamin Herrenschmidtaa709f32012-07-05 16:30:33 +0000619 struct assoc_arrays aa = { .arrays = NULL };
Paul Mackerras02045682006-11-29 22:27:42 +1100620
Nathan Fontenot83426812008-07-03 13:35:54 +1000621 n = of_get_drconf_memory(memory, &dm);
622 if (!n)
Paul Mackerras02045682006-11-29 22:27:42 +1100623 return;
624
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000625 lmb_size = of_get_lmb_size(memory);
626 if (!lmb_size)
Nathan Fontenot83426812008-07-03 13:35:54 +1000627 return;
628
629 rc = of_get_assoc_arrays(memory, &aa);
630 if (rc)
Paul Mackerras02045682006-11-29 22:27:42 +1100631 return;
632
Chandrucf000852008-08-30 00:28:16 +1000633 /* check if this is a kexec/kdump kernel */
634 usm = of_get_usable_memory(memory);
635 if (usm != NULL)
636 is_kexec_kdump = 1;
637
Paul Mackerras02045682006-11-29 22:27:42 +1100638 for (; n != 0; --n) {
Nathan Fontenot83426812008-07-03 13:35:54 +1000639 struct of_drconf_cell drmem;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100640
Nathan Fontenot83426812008-07-03 13:35:54 +1000641 read_drconf_cell(&drmem, &dm);
642
643 /* skip this block if the reserved bit is set in flags (0x80)
644 or if the block is not assigned to this partition (0x8) */
645 if ((drmem.flags & DRCONF_MEM_RESERVED)
646 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
647 continue;
648
Chandrucf000852008-08-30 00:28:16 +1000649 base = drmem.base_addr;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000650 size = lmb_size;
Chandrucf000852008-08-30 00:28:16 +1000651 ranges = 1;
Nathan Fontenot83426812008-07-03 13:35:54 +1000652
Chandrucf000852008-08-30 00:28:16 +1000653 if (is_kexec_kdump) {
654 ranges = read_usm_ranges(&usm);
655 if (!ranges) /* there are no (base, size) duple */
656 continue;
657 }
658 do {
659 if (is_kexec_kdump) {
660 base = read_n_cells(n_mem_addr_cells, &usm);
661 size = read_n_cells(n_mem_size_cells, &usm);
662 }
663 nid = of_drconf_to_nid_single(&drmem, &aa);
664 fake_numa_create_new_node(
665 ((base + size) >> PAGE_SHIFT),
Nathan Fontenot83426812008-07-03 13:35:54 +1000666 &nid);
Chandrucf000852008-08-30 00:28:16 +1000667 node_set_online(nid);
668 sz = numa_enforce_memory_limit(base, size);
669 if (sz)
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800670 memblock_set_node(base, sz, nid);
Chandrucf000852008-08-30 00:28:16 +1000671 } while (--ranges);
Paul Mackerras02045682006-11-29 22:27:42 +1100672 }
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675static int __init parse_numa_properties(void)
676{
Anton Blanchard94db7c52011-08-10 20:44:22 +0000677 struct device_node *memory;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600678 int default_nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long i;
680
681 if (numa_enabled == 0) {
682 printk(KERN_WARNING "NUMA disabled by user\n");
683 return -1;
684 }
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 min_common_depth = find_min_common_depth();
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (min_common_depth < 0)
689 return min_common_depth;
690
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600691 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600694 * Even though we connect cpus to numa domains later in SMP
695 * init, we need to know the node ids now. This is because
696 * each node to be onlined must have NODE_DATA etc backing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600698 for_each_present_cpu(i) {
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000699 struct device_node *cpu;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600700 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Milton Miller8b16cd22009-01-08 02:19:45 +0000702 cpu = of_get_cpu_node(i, NULL);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600703 BUG_ON(!cpu);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700704 nid = of_node_to_nid_single(cpu);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600705 of_node_put(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600707 /*
708 * Don't fall back to default_nid yet -- we will plug
709 * cpus into nodes once the memory scan has discovered
710 * the topology.
711 */
712 if (nid < 0)
713 continue;
714 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Mike Kravetz237a0982005-12-05 12:06:42 -0800717 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
Anton Blanchard94db7c52011-08-10 20:44:22 +0000718
719 for_each_node_by_type(memory, "memory") {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned long start;
721 unsigned long size;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600722 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000724 const unsigned int *memcell_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned int len;
726
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000727 memcell_buf = of_get_property(memory,
Michael Ellermanba759482005-12-04 18:39:55 +1100728 "linux,usable-memory", &len);
729 if (!memcell_buf || len <= 0)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000730 memcell_buf = of_get_property(memory, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!memcell_buf || len <= 0)
732 continue;
733
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100734 /* ranges in cell */
735 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736new_range:
737 /* these are order-sensitive, and modify the buffer pointer */
Mike Kravetz237a0982005-12-05 12:06:42 -0800738 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
739 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600741 /*
742 * Assumption: either all memory nodes or none will
743 * have associativity properties. If none, then
744 * everything goes to default_nid.
745 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700746 nid = of_node_to_nid_single(memory);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600747 if (nid < 0)
748 nid = default_nid;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100749
750 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600751 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100753 if (!(size = numa_enforce_memory_limit(start, size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (--ranges)
755 goto new_range;
756 else
757 continue;
758 }
759
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800760 memblock_set_node(start, size, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (--ranges)
763 goto new_range;
764 }
765
Paul Mackerras02045682006-11-29 22:27:42 +1100766 /*
Anton Blancharddfbe93a2011-08-10 20:44:23 +0000767 * Now do the same thing for each MEMBLOCK listed in the
768 * ibm,dynamic-memory property in the
769 * ibm,dynamic-reconfiguration-memory node.
Paul Mackerras02045682006-11-29 22:27:42 +1100770 */
771 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
772 if (memory)
773 parse_drconf_memory(memory);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 0;
776}
777
778static void __init setup_nonnuma(void)
779{
Yinghai Lu95f72d12010-07-12 14:36:09 +1000780 unsigned long top_of_ram = memblock_end_of_DRAM();
781 unsigned long total_ram = memblock_phys_mem_size();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700782 unsigned long start_pfn, end_pfn;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000783 unsigned int nid = 0;
784 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Olof Johanssone110b282006-04-12 15:25:01 -0500786 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500788 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 (top_of_ram - total_ram) >> 20);
790
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000791 for_each_memblock(memory, reg) {
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700792 start_pfn = memblock_region_memory_base_pfn(reg);
793 end_pfn = memblock_region_memory_end_pfn(reg);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100794
795 fake_numa_create_new_node(end_pfn, &nid);
Tejun Heo1d7cfe12011-12-08 10:22:08 -0800796 memblock_set_node(PFN_PHYS(start_pfn),
797 PFN_PHYS(end_pfn - start_pfn), nid);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100798 node_set_online(nid);
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Anton Blanchard4b703a22005-12-13 06:56:47 +1100802void __init dump_numa_cpu_topology(void)
803{
804 unsigned int node;
805 unsigned int cpu, count;
806
807 if (min_common_depth == -1 || !numa_enabled)
808 return;
809
810 for_each_online_node(node) {
Olof Johanssone110b282006-04-12 15:25:01 -0500811 printk(KERN_DEBUG "Node %d CPUs:", node);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100812
813 count = 0;
814 /*
815 * If we used a CPU iterator here we would miss printing
816 * the holes in the cpumap.
817 */
Anton Blanchard25863de2010-04-26 15:32:43 +0000818 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
819 if (cpumask_test_cpu(cpu,
820 node_to_cpumask_map[node])) {
Anton Blanchard4b703a22005-12-13 06:56:47 +1100821 if (count == 0)
822 printk(" %u", cpu);
823 ++count;
824 } else {
825 if (count > 1)
826 printk("-%u", cpu - 1);
827 count = 0;
828 }
829 }
830
831 if (count > 1)
Anton Blanchard25863de2010-04-26 15:32:43 +0000832 printk("-%u", nr_cpu_ids - 1);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100833 printk("\n");
834 }
835}
836
837static void __init dump_numa_memory_topology(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 unsigned int node;
840 unsigned int count;
841
842 if (min_common_depth == -1 || !numa_enabled)
843 return;
844
845 for_each_online_node(node) {
846 unsigned long i;
847
Olof Johanssone110b282006-04-12 15:25:01 -0500848 printk(KERN_DEBUG "Node %d Memory:", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 count = 0;
851
Yinghai Lu95f72d12010-07-12 14:36:09 +1000852 for (i = 0; i < memblock_end_of_DRAM();
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100853 i += (1 << SECTION_SIZE_BITS)) {
854 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (count == 0)
856 printk(" 0x%lx", i);
857 ++count;
858 } else {
859 if (count > 0)
860 printk("-0x%lx", i);
861 count = 0;
862 }
863 }
864
865 if (count > 0)
866 printk("-0x%lx", i);
867 printk("\n");
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000872 * Allocate some memory, satisfying the memblock or bootmem allocator where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * required. nid is the preferred node and end is the physical address of
874 * the highest address in the node.
875 *
Dave Hansen0be210f2008-12-09 08:21:35 +0000876 * Returns the virtual address of the memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Dave Hansen893473d2008-12-09 08:21:36 +0000878static void __init *careful_zallocation(int nid, unsigned long size,
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100879 unsigned long align,
880 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Dave Hansen0be210f2008-12-09 08:21:35 +0000882 void *ret;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100883 int new_nid;
Dave Hansen0be210f2008-12-09 08:21:35 +0000884 unsigned long ret_paddr;
885
Yinghai Lu95f72d12010-07-12 14:36:09 +1000886 ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* retry over all memory */
Dave Hansen0be210f2008-12-09 08:21:35 +0000889 if (!ret_paddr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000890 ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dave Hansen0be210f2008-12-09 08:21:35 +0000892 if (!ret_paddr)
Dave Hansen5d21ea22008-12-09 08:21:33 +0000893 panic("numa.c: cannot allocate %lu bytes for node %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 size, nid);
895
Dave Hansen0be210f2008-12-09 08:21:35 +0000896 ret = __va(ret_paddr);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
Dave Hansenc555e522008-12-09 08:21:32 +0000899 * We initialize the nodes in numeric order: 0, 1, 2...
Yinghai Lu95f72d12010-07-12 14:36:09 +1000900 * and hand over control from the MEMBLOCK allocator to the
Dave Hansenc555e522008-12-09 08:21:32 +0000901 * bootmem allocator. If this function is called for
902 * node 5, then we know that all nodes <5 are using the
Yinghai Lu95f72d12010-07-12 14:36:09 +1000903 * bootmem allocator instead of the MEMBLOCK allocator.
Dave Hansenc555e522008-12-09 08:21:32 +0000904 *
905 * So, check the nid from which this allocation came
906 * and double check to see if we need to use bootmem
Yinghai Lu95f72d12010-07-12 14:36:09 +1000907 * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
Dave Hansenc555e522008-12-09 08:21:32 +0000908 * since it would be useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Dave Hansen0be210f2008-12-09 08:21:35 +0000910 new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100911 if (new_nid < nid) {
Dave Hansen0be210f2008-12-09 08:21:35 +0000912 ret = __alloc_bootmem_node(NODE_DATA(new_nid),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 size, align, 0);
914
Dave Hansen0be210f2008-12-09 08:21:35 +0000915 dbg("alloc_bootmem %p %lx\n", ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Dave Hansen893473d2008-12-09 08:21:36 +0000918 memset(ret, 0, size);
Dave Hansen0be210f2008-12-09 08:21:35 +0000919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700922static struct notifier_block __cpuinitdata ppc64_numa_nb = {
923 .notifier_call = cpu_numa_callback,
924 .priority = 1 /* Must run before sched domains notifier. */
925};
926
David Rientjes28e86bd2011-12-08 12:33:29 +0000927static void __init mark_reserved_regions_for_nid(int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Dave Hansen4a618662008-11-24 12:02:35 +0000929 struct pglist_data *node = NODE_DATA(nid);
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000930 struct memblock_region *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000932 for_each_memblock(reserved, reg) {
933 unsigned long physbase = reg->base;
934 unsigned long size = reg->size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000935 unsigned long start_pfn = physbase >> PAGE_SHIFT;
Dave Hansen06eccea2009-02-12 12:36:04 +0000936 unsigned long end_pfn = PFN_UP(physbase + size);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000937 struct node_active_region node_ar;
Dave Hansen4a618662008-11-24 12:02:35 +0000938 unsigned long node_end_pfn = node->node_start_pfn +
939 node->node_spanned_pages;
940
941 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000942 * Check to make sure that this memblock.reserved area is
Dave Hansen4a618662008-11-24 12:02:35 +0000943 * within the bounds of the node that we care about.
944 * Checking the nid of the start and end points is not
945 * sufficient because the reserved area could span the
946 * entire node.
947 */
948 if (end_pfn <= node->node_start_pfn ||
949 start_pfn >= node_end_pfn)
950 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000952 get_node_active_region(start_pfn, &node_ar);
Jon Tollefsone8170372008-10-16 18:59:43 +0000953 while (start_pfn < end_pfn &&
954 node_ar.start_pfn < node_ar.end_pfn) {
955 unsigned long reserve_size = size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000956 /*
957 * if reserved region extends past active region
958 * then trim size to active region
959 */
960 if (end_pfn > node_ar.end_pfn)
Jon Tollefsone8170372008-10-16 18:59:43 +0000961 reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
Dave Hansen06eccea2009-02-12 12:36:04 +0000962 - physbase;
Dave Hansena4c74dd2008-12-11 08:36:06 +0000963 /*
964 * Only worry about *this* node, others may not
965 * yet have valid NODE_DATA().
966 */
967 if (node_ar.nid == nid) {
968 dbg("reserve_bootmem %lx %lx nid=%d\n",
969 physbase, reserve_size, node_ar.nid);
970 reserve_bootmem_node(NODE_DATA(node_ar.nid),
971 physbase, reserve_size,
972 BOOTMEM_DEFAULT);
973 }
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000974 /*
975 * if reserved region is contained in the active region
976 * then done.
977 */
978 if (end_pfn <= node_ar.end_pfn)
979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000981 /*
982 * reserved region extends past the active region
983 * get next active region that contains this
984 * reserved region
985 */
986 start_pfn = node_ar.end_pfn;
987 physbase = start_pfn << PAGE_SHIFT;
Jon Tollefsone8170372008-10-16 18:59:43 +0000988 size = size - reserve_size;
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000989 get_node_active_region(start_pfn, &node_ar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Dave Hansen4a618662008-11-24 12:02:35 +0000992}
Jon Tollefson8f64e1f2008-10-09 10:18:40 +0000993
Dave Hansen4a618662008-11-24 12:02:35 +0000994
995void __init do_init_bootmem(void)
996{
997 int nid;
Dave Hansen4a618662008-11-24 12:02:35 +0000998
999 min_low_pfn = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001000 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Dave Hansen4a618662008-11-24 12:02:35 +00001001 max_pfn = max_low_pfn;
1002
1003 if (parse_numa_properties())
1004 setup_nonnuma();
1005 else
1006 dump_numa_memory_topology();
1007
Dave Hansen4a618662008-11-24 12:02:35 +00001008 for_each_online_node(nid) {
1009 unsigned long start_pfn, end_pfn;
Dave Hansen0be210f2008-12-09 08:21:35 +00001010 void *bootmem_vaddr;
Dave Hansen4a618662008-11-24 12:02:35 +00001011 unsigned long bootmap_pages;
1012
1013 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1014
1015 /*
1016 * Allocate the node structure node local if possible
1017 *
1018 * Be careful moving this around, as it relies on all
1019 * previous nodes' bootmem to be initialized and have
1020 * all reserved areas marked.
1021 */
Dave Hansen893473d2008-12-09 08:21:36 +00001022 NODE_DATA(nid) = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001023 sizeof(struct pglist_data),
1024 SMP_CACHE_BYTES, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001025
1026 dbg("node %d\n", nid);
1027 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
1028
1029 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
1030 NODE_DATA(nid)->node_start_pfn = start_pfn;
1031 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
1032
1033 if (NODE_DATA(nid)->node_spanned_pages == 0)
1034 continue;
1035
1036 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
1037 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
1038
1039 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
Dave Hansen893473d2008-12-09 08:21:36 +00001040 bootmem_vaddr = careful_zallocation(nid,
Dave Hansen4a618662008-11-24 12:02:35 +00001041 bootmap_pages << PAGE_SHIFT,
1042 PAGE_SIZE, end_pfn);
Dave Hansen4a618662008-11-24 12:02:35 +00001043
Dave Hansen0be210f2008-12-09 08:21:35 +00001044 dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
Dave Hansen4a618662008-11-24 12:02:35 +00001045
Dave Hansen0be210f2008-12-09 08:21:35 +00001046 init_bootmem_node(NODE_DATA(nid),
1047 __pa(bootmem_vaddr) >> PAGE_SHIFT,
Dave Hansen4a618662008-11-24 12:02:35 +00001048 start_pfn, end_pfn);
1049
1050 free_bootmem_with_active_regions(nid, end_pfn);
1051 /*
1052 * Be very careful about moving this around. Future
Dave Hansen893473d2008-12-09 08:21:36 +00001053 * calls to careful_zallocation() depend on this getting
Dave Hansen4a618662008-11-24 12:02:35 +00001054 * done correctly.
1055 */
1056 mark_reserved_regions_for_nid(nid);
Jon Tollefson8f64e1f2008-10-09 10:18:40 +00001057 sparse_memory_present_with_active_regions(nid);
Dave Hansen4a618662008-11-24 12:02:35 +00001058 }
Benjamin Herrenschmidtd3f62042009-06-02 21:16:38 +00001059
1060 init_bootmem_done = 1;
Anton Blanchard25863de2010-04-26 15:32:43 +00001061
1062 /*
1063 * Now bootmem is initialised we can create the node to cpumask
1064 * lookup tables and setup the cpu callback to populate them.
1065 */
1066 setup_node_to_cpumask_map();
1067
1068 register_cpu_notifier(&ppc64_numa_nb);
1069 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
1070 (void *)(unsigned long)boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073void __init paging_init(void)
1074{
Mel Gorman6391af12006-10-11 01:20:39 -07001075 unsigned long max_zone_pfns[MAX_NR_ZONES];
1076 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Yinghai Lu95f72d12010-07-12 14:36:09 +10001077 max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
Mel Gormanc67c3cb2006-09-27 01:49:49 -07001078 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static int __init early_numa(char *p)
1082{
1083 if (!p)
1084 return 0;
1085
1086 if (strstr(p, "off"))
1087 numa_enabled = 0;
1088
1089 if (strstr(p, "debug"))
1090 numa_debug = 1;
1091
Balbir Singh1daa6d02008-02-01 15:57:31 +11001092 p = strstr(p, "fake=");
1093 if (p)
1094 cmdline = p + strlen("fake=");
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098early_param("numa", early_numa);
Mike Kravetz237a0982005-12-05 12:06:42 -08001099
1100#ifdef CONFIG_MEMORY_HOTPLUG
1101/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001102 * Find the node associated with a hot added memory section for
1103 * memory represented in the device tree by the property
1104 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
Nathan Fontenot0db93602008-07-03 13:25:08 +10001105 */
1106static int hot_add_drconf_scn_to_nid(struct device_node *memory,
1107 unsigned long scn_addr)
1108{
1109 const u32 *dm;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001110 unsigned int drconf_cell_cnt, rc;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001111 unsigned long lmb_size;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001112 struct assoc_arrays aa;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001113 int nid = -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001114
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001115 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1116 if (!drconf_cell_cnt)
1117 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001118
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001119 lmb_size = of_get_lmb_size(memory);
1120 if (!lmb_size)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001121 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001122
1123 rc = of_get_assoc_arrays(memory, &aa);
1124 if (rc)
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001125 return -1;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001126
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001127 for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
Nathan Fontenot0db93602008-07-03 13:25:08 +10001128 struct of_drconf_cell drmem;
1129
1130 read_drconf_cell(&drmem, &dm);
1131
1132 /* skip this block if it is reserved or not assigned to
1133 * this partition */
1134 if ((drmem.flags & DRCONF_MEM_RESERVED)
1135 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
1136 continue;
1137
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001138 if ((scn_addr < drmem.base_addr)
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +10001139 || (scn_addr >= (drmem.base_addr + lmb_size)))
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001140 continue;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001141
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001142 nid = of_drconf_to_nid_single(&drmem, &aa);
1143 break;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001144 }
1145
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001146 return nid;
Nathan Fontenot0db93602008-07-03 13:25:08 +10001147}
1148
1149/*
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001150 * Find the node associated with a hot added memory section for memory
1151 * represented in the device tree as a node (i.e. memory@XXXX) for
Yinghai Lu95f72d12010-07-12 14:36:09 +10001152 * each memblock.
Mike Kravetz237a0982005-12-05 12:06:42 -08001153 */
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001154int hot_add_node_scn_to_nid(unsigned long scn_addr)
Mike Kravetz237a0982005-12-05 12:06:42 -08001155{
Anton Blanchard94db7c52011-08-10 20:44:22 +00001156 struct device_node *memory;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001157 int nid = -1;
Mike Kravetz237a0982005-12-05 12:06:42 -08001158
Anton Blanchard94db7c52011-08-10 20:44:22 +00001159 for_each_node_by_type(memory, "memory") {
Mike Kravetz237a0982005-12-05 12:06:42 -08001160 unsigned long start, size;
Mike Kravetzb226e462005-12-16 14:30:35 -08001161 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001162 const unsigned int *memcell_buf;
Mike Kravetz237a0982005-12-05 12:06:42 -08001163 unsigned int len;
1164
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001165 memcell_buf = of_get_property(memory, "reg", &len);
Mike Kravetz237a0982005-12-05 12:06:42 -08001166 if (!memcell_buf || len <= 0)
1167 continue;
1168
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001169 /* ranges in cell */
1170 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Mike Kravetz237a0982005-12-05 12:06:42 -08001171
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001172 while (ranges--) {
1173 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1174 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1175
1176 if ((scn_addr < start) || (scn_addr >= (start + size)))
1177 continue;
1178
1179 nid = of_node_to_nid_single(memory);
1180 break;
Mike Kravetz237a0982005-12-05 12:06:42 -08001181 }
1182
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001183 if (nid >= 0)
1184 break;
Mike Kravetz237a0982005-12-05 12:06:42 -08001185 }
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001186
Anton Blanchard60831842011-08-10 20:44:21 +00001187 of_node_put(memory);
1188
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001189 return nid;
Mike Kravetz237a0982005-12-05 12:06:42 -08001190}
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001191
1192/*
1193 * Find the node associated with a hot added memory section. Section
Yinghai Lu95f72d12010-07-12 14:36:09 +10001194 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1195 * sections are fully contained within a single MEMBLOCK.
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001196 */
1197int hot_add_scn_to_nid(unsigned long scn_addr)
1198{
1199 struct device_node *memory = NULL;
1200 int nid, found = 0;
1201
1202 if (!numa_enabled || (min_common_depth < 0))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001203 return first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001204
1205 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1206 if (memory) {
1207 nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
1208 of_node_put(memory);
1209 } else {
1210 nid = hot_add_node_scn_to_nid(scn_addr);
1211 }
1212
1213 if (nid < 0 || !node_online(nid))
H Hartley Sweeten72c33682010-03-05 13:42:43 -08001214 nid = first_online_node;
Nathan Fontenot0f16ef72009-02-17 08:08:30 +00001215
1216 if (NODE_DATA(nid)->node_spanned_pages)
1217 return nid;
1218
1219 for_each_online_node(nid) {
1220 if (NODE_DATA(nid)->node_spanned_pages) {
1221 found = 1;
1222 break;
1223 }
1224 }
1225
1226 BUG_ON(!found);
1227 return nid;
1228}
1229
Nishanth Aravamudancd342062010-10-26 17:35:12 +00001230static u64 hot_add_drconf_memory_max(void)
1231{
1232 struct device_node *memory = NULL;
1233 unsigned int drconf_cell_cnt = 0;
1234 u64 lmb_size = 0;
1235 const u32 *dm = 0;
1236
1237 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1238 if (memory) {
1239 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1240 lmb_size = of_get_lmb_size(memory);
1241 of_node_put(memory);
1242 }
1243 return lmb_size * drconf_cell_cnt;
1244}
1245
1246/*
1247 * memory_hotplug_max - return max address of memory that may be added
1248 *
1249 * This is currently only used on systems that support drconfig memory
1250 * hotplug.
1251 */
1252u64 memory_hotplug_max(void)
1253{
1254 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1255}
Mike Kravetz237a0982005-12-05 12:06:42 -08001256#endif /* CONFIG_MEMORY_HOTPLUG */
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001257
Jesse Larrewbd034032011-01-20 19:00:51 +00001258/* Virtual Processor Home Node (VPHN) support */
Jesse Larrew39bf9902010-12-17 22:07:47 +00001259#ifdef CONFIG_PPC_SPLPAR
Nathan Fontenot30c05352013-04-24 06:02:13 +00001260struct topology_update_data {
1261 struct topology_update_data *next;
1262 unsigned int cpu;
1263 int old_nid;
1264 int new_nid;
1265};
1266
Anton Blanchard5de16692011-01-29 12:24:34 +00001267static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001268static cpumask_t cpu_associativity_changes_mask;
1269static int vphn_enabled;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001270static int prrn_enabled;
1271static void reset_topology_timer(void);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001272
1273/*
1274 * Store the current values of the associativity change counters in the
1275 * hypervisor.
1276 */
1277static void setup_cpu_associativity_change_counters(void)
1278{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001279 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001280
Anton Blanchard5de16692011-01-29 12:24:34 +00001281 /* The VPHN feature supports a maximum of 8 reference points */
1282 BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1283
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001284 for_each_possible_cpu(cpu) {
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001285 int i;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001286 u8 *counts = vphn_cpu_change_counts[cpu];
1287 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1288
Anton Blanchard5de16692011-01-29 12:24:34 +00001289 for (i = 0; i < distance_ref_points_depth; i++)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001290 counts[i] = hypervisor_counts[i];
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001291 }
1292}
1293
1294/*
1295 * The hypervisor maintains a set of 8 associativity change counters in
1296 * the VPA of each cpu that correspond to the associativity levels in the
1297 * ibm,associativity-reference-points property. When an associativity
1298 * level changes, the corresponding counter is incremented.
1299 *
1300 * Set a bit in cpu_associativity_changes_mask for each cpu whose home
1301 * node associativity levels have changed.
1302 *
1303 * Returns the number of cpus with unhandled associativity changes.
1304 */
1305static int update_cpu_associativity_changes_mask(void)
1306{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001307 int cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001308 cpumask_t *changes = &cpu_associativity_changes_mask;
1309
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001310 for_each_possible_cpu(cpu) {
1311 int i, changed = 0;
1312 u8 *counts = vphn_cpu_change_counts[cpu];
1313 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1314
Anton Blanchard5de16692011-01-29 12:24:34 +00001315 for (i = 0; i < distance_ref_points_depth; i++) {
Anton Blanchardd69043e2011-01-29 12:26:19 +00001316 if (hypervisor_counts[i] != counts[i]) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001317 counts[i] = hypervisor_counts[i];
1318 changed = 1;
1319 }
1320 }
1321 if (changed) {
1322 cpumask_set_cpu(cpu, changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001323 }
1324 }
1325
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001326 return cpumask_weight(changes);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001327}
1328
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001329/*
1330 * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1331 * the complete property we have to add the length in the first cell.
1332 */
1333#define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001334
1335/*
1336 * Convert the associativity domain numbers returned from the hypervisor
1337 * to the sequence they would appear in the ibm,associativity property.
1338 */
1339static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
1340{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001341 int i, nr_assoc_doms = 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001342 const u16 *field = (const u16*) packed;
1343
1344#define VPHN_FIELD_UNUSED (0xffff)
1345#define VPHN_FIELD_MSB (0x8000)
1346#define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
1347
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001348 for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001349 if (*field == VPHN_FIELD_UNUSED) {
1350 /* All significant fields processed, and remaining
1351 * fields contain the reserved value of all 1's.
1352 * Just store them.
1353 */
1354 unpacked[i] = *((u32*)field);
1355 field += 2;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001356 } else if (*field & VPHN_FIELD_MSB) {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001357 /* Data is in the lower 15 bits of this field */
1358 unpacked[i] = *field & VPHN_FIELD_MASK;
1359 field++;
1360 nr_assoc_doms++;
Jesse Larrew7639ada2011-01-20 19:01:13 +00001361 } else {
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001362 /* Data is in the lower 15 bits of this field
1363 * concatenated with the next 16 bit field
1364 */
1365 unpacked[i] = *((u32*)field);
1366 field += 2;
1367 nr_assoc_doms++;
1368 }
1369 }
1370
Anton Blanchardc0e5e462011-01-29 12:28:04 +00001371 /* The first cell contains the length of the property */
1372 unpacked[0] = nr_assoc_doms;
1373
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001374 return nr_assoc_doms;
1375}
1376
1377/*
1378 * Retrieve the new associativity information for a virtual processor's
1379 * home node.
1380 */
1381static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
1382{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001383 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001384 long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1385 u64 flags = 1;
1386 int hwcpu = get_hard_smp_processor_id(cpu);
1387
1388 rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
1389 vphn_unpack_associativity(retbuf, associativity);
1390
1391 return rc;
1392}
1393
1394static long vphn_get_associativity(unsigned long cpu,
1395 unsigned int *associativity)
1396{
Jesse Larrewcd9d6cc2011-01-20 19:01:35 +00001397 long rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001398
1399 rc = hcall_vphn(cpu, associativity);
1400
1401 switch (rc) {
1402 case H_FUNCTION:
1403 printk(KERN_INFO
1404 "VPHN is not supported. Disabling polling...\n");
1405 stop_topology_update();
1406 break;
1407 case H_HARDWARE:
1408 printk(KERN_ERR
1409 "hcall_vphn() experienced a hardware fault "
1410 "preventing VPHN. Disabling polling...\n");
1411 stop_topology_update();
1412 }
1413
1414 return rc;
1415}
1416
1417/*
Nathan Fontenot30c05352013-04-24 06:02:13 +00001418 * Update the CPU maps and sysfs entries for a single CPU when its NUMA
1419 * characteristics change. This function doesn't perform any locking and is
1420 * only safe to call from stop_machine().
1421 */
1422static int update_cpu_topology(void *data)
1423{
1424 struct topology_update_data *update;
1425 unsigned long cpu;
1426
1427 if (!data)
1428 return -EINVAL;
1429
1430 cpu = get_cpu();
1431
1432 for (update = data; update; update = update->next) {
1433 if (cpu != update->cpu)
1434 continue;
1435
1436 unregister_cpu_under_node(update->cpu, update->old_nid);
1437 unmap_cpu_from_node(update->cpu);
1438 map_cpu_to_node(update->cpu, update->new_nid);
Jesse Larrew176bbf12013-04-24 06:03:48 +00001439 vdso_getcpu_init();
Nathan Fontenot30c05352013-04-24 06:02:13 +00001440 register_cpu_under_node(update->cpu, update->new_nid);
1441 }
1442
1443 return 0;
1444}
1445
1446/*
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001447 * Update the node maps and sysfs entries for each cpu whose home node
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001448 * has changed. Returns 1 when the topology has changed, and 0 otherwise.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001449 */
1450int arch_update_cpu_topology(void)
1451{
Nathan Fontenot30c05352013-04-24 06:02:13 +00001452 unsigned int cpu, changed = 0;
1453 struct topology_update_data *updates, *ud;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001454 unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
Jesse Larrew176bbf12013-04-24 06:03:48 +00001455 cpumask_t updated_cpus;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001456 struct device *dev;
Nathan Fontenot30c05352013-04-24 06:02:13 +00001457 int weight, i = 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001458
Nathan Fontenot30c05352013-04-24 06:02:13 +00001459 weight = cpumask_weight(&cpu_associativity_changes_mask);
1460 if (!weight)
1461 return 0;
1462
1463 updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
1464 if (!updates)
1465 return 0;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001466
Jesse Larrew176bbf12013-04-24 06:03:48 +00001467 cpumask_clear(&updated_cpus);
1468
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001469 for_each_cpu(cpu, &cpu_associativity_changes_mask) {
Nathan Fontenot30c05352013-04-24 06:02:13 +00001470 ud = &updates[i++];
1471 ud->cpu = cpu;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001472 vphn_get_associativity(cpu, associativity);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001473 ud->new_nid = associativity_to_nid(associativity);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001474
Nathan Fontenot30c05352013-04-24 06:02:13 +00001475 if (ud->new_nid < 0 || !node_online(ud->new_nid))
1476 ud->new_nid = first_online_node;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001477
Nathan Fontenot30c05352013-04-24 06:02:13 +00001478 ud->old_nid = numa_cpu_lookup_table[cpu];
Jesse Larrew176bbf12013-04-24 06:03:48 +00001479 cpumask_set_cpu(cpu, &updated_cpus);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001480
Nathan Fontenot30c05352013-04-24 06:02:13 +00001481 if (i < weight)
1482 ud->next = &updates[i];
1483 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001484
Jesse Larrew176bbf12013-04-24 06:03:48 +00001485 stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001486
1487 for (ud = &updates[0]; ud; ud = ud->next) {
1488 dev = get_cpu_device(ud->cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001489 if (dev)
1490 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
Nathan Fontenot30c05352013-04-24 06:02:13 +00001491 cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001492 changed = 1;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001493 }
1494
Nathan Fontenot30c05352013-04-24 06:02:13 +00001495 kfree(updates);
Jesse Larrew79c5fce2012-06-07 16:04:34 -05001496 return changed;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001497}
1498
1499static void topology_work_fn(struct work_struct *work)
1500{
1501 rebuild_sched_domains();
1502}
1503static DECLARE_WORK(topology_work, topology_work_fn);
1504
1505void topology_schedule_update(void)
1506{
1507 schedule_work(&topology_work);
1508}
1509
1510static void topology_timer_fn(unsigned long ignored)
1511{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001512 if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001513 topology_schedule_update();
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001514 else if (vphn_enabled) {
1515 if (update_cpu_associativity_changes_mask() > 0)
1516 topology_schedule_update();
1517 reset_topology_timer();
1518 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001519}
1520static struct timer_list topology_timer =
1521 TIMER_INITIALIZER(topology_timer_fn, 0, 0);
1522
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001523static void reset_topology_timer(void)
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001524{
1525 topology_timer.data = 0;
1526 topology_timer.expires = jiffies + 60 * HZ;
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001527 mod_timer(&topology_timer, topology_timer.expires);
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001528}
1529
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001530#ifdef CONFIG_SMP
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
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001565#endif
1566
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001567/*
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001568 * Start polling for associativity changes.
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001569 */
1570int start_topology_update(void)
1571{
1572 int rc = 0;
1573
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001574 if (firmware_has_feature(FW_FEATURE_PRRN)) {
1575 if (!prrn_enabled) {
1576 prrn_enabled = 1;
1577 vphn_enabled = 0;
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001578#ifdef CONFIG_SMP
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001579 rc = of_reconfig_notifier_register(&dt_update_nb);
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001580#endif
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001581 }
Jesse Larrewb7abef02013-04-24 06:05:22 +00001582 } else if (firmware_has_feature(FW_FEATURE_VPHN) &&
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001583 get_lppaca()->shared_proc) {
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001584 if (!vphn_enabled) {
1585 prrn_enabled = 0;
1586 vphn_enabled = 1;
1587 setup_cpu_associativity_change_counters();
1588 init_timer_deferrable(&topology_timer);
1589 reset_topology_timer();
1590 }
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001591 }
1592
1593 return rc;
1594}
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001595
1596/*
1597 * Disable polling for VPHN associativity changes.
1598 */
1599int stop_topology_update(void)
1600{
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001601 int rc = 0;
1602
1603 if (prrn_enabled) {
1604 prrn_enabled = 0;
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001605#ifdef CONFIG_SMP
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001606 rc = of_reconfig_notifier_unregister(&dt_update_nb);
Nathan Fontenot601abdc32013-04-29 03:45:36 +00001607#endif
Jesse Larrew5d88aa82013-04-24 06:00:35 +00001608 } else if (vphn_enabled) {
1609 vphn_enabled = 0;
1610 rc = del_timer_sync(&topology_timer);
1611 }
1612
1613 return rc;
Jesse Larrew9eff1a32010-12-01 12:31:15 +00001614}
Nathan Fontenote04fa612013-04-24 06:07:39 +00001615
1616int prrn_is_enabled(void)
1617{
1618 return prrn_enabled;
1619}
1620
1621static int topology_read(struct seq_file *file, void *v)
1622{
1623 if (vphn_enabled || prrn_enabled)
1624 seq_puts(file, "on\n");
1625 else
1626 seq_puts(file, "off\n");
1627
1628 return 0;
1629}
1630
1631static int topology_open(struct inode *inode, struct file *file)
1632{
1633 return single_open(file, topology_read, NULL);
1634}
1635
1636static ssize_t topology_write(struct file *file, const char __user *buf,
1637 size_t count, loff_t *off)
1638{
1639 char kbuf[4]; /* "on" or "off" plus null. */
1640 int read_len;
1641
1642 read_len = count < 3 ? count : 3;
1643 if (copy_from_user(kbuf, buf, read_len))
1644 return -EINVAL;
1645
1646 kbuf[read_len] = '\0';
1647
1648 if (!strncmp(kbuf, "on", 2))
1649 start_topology_update();
1650 else if (!strncmp(kbuf, "off", 3))
1651 stop_topology_update();
1652 else
1653 return -EINVAL;
1654
1655 return count;
1656}
1657
1658static const struct file_operations topology_ops = {
1659 .read = seq_read,
1660 .write = topology_write,
1661 .open = topology_open,
1662 .release = single_release
1663};
1664
1665static int topology_update_init(void)
1666{
1667 start_topology_update();
1668 proc_create("powerpc/topology_updates", 644, NULL, &topology_ops);
1669
1670 return 0;
1671}
1672device_initcall(topology_update_init);
Jesse Larrew39bf9902010-12-17 22:07:47 +00001673#endif /* CONFIG_PPC_SPLPAR */