]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/powerpc/mm/numa.c
powerpc/pseries: Consolidate different NUMA distance update code paths
[mirror_ubuntu-jammy-kernel.git] / arch / powerpc / mm / numa.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * pSeries NUMA support
4 *
5 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
1da177e4 6 */
2d73bae1
NA
7#define pr_fmt(fmt) "numa: " fmt
8
1da177e4 9#include <linux/threads.h>
57c8a661 10#include <linux/memblock.h>
1da177e4
LT
11#include <linux/init.h>
12#include <linux/mm.h>
13#include <linux/mmzone.h>
4b16f8e2 14#include <linux/export.h>
1da177e4
LT
15#include <linux/nodemask.h>
16#include <linux/cpu.h>
17#include <linux/notifier.h>
6df1646e 18#include <linux/of.h>
06eccea6 19#include <linux/pfn.h>
9eff1a38
JL
20#include <linux/cpuset.h>
21#include <linux/node.h>
30c05350 22#include <linux/stop_machine.h>
e04fa612
NF
23#include <linux/proc_fs.h>
24#include <linux/seq_file.h>
25#include <linux/uaccess.h>
191a7120 26#include <linux/slab.h>
3be7db6a 27#include <asm/cputhreads.h>
45fb6cea 28#include <asm/sparsemem.h>
d9b2b2a2 29#include <asm/prom.h>
2249ca9d 30#include <asm/smp.h>
d4edc5b6 31#include <asm/topology.h>
9eff1a38
JL
32#include <asm/firmware.h>
33#include <asm/paca.h>
39bf990e 34#include <asm/hvcall.h>
ae3a197e 35#include <asm/setup.h>
176bbf14 36#include <asm/vdso.h>
514a9cb3 37#include <asm/drmem.h>
1da177e4
LT
38
39static int numa_enabled = 1;
40
1daa6d08
BS
41static char *cmdline __initdata;
42
1da177e4
LT
43static int numa_debug;
44#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
45
45fb6cea 46int numa_cpu_lookup_table[NR_CPUS];
25863de0 47cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
1da177e4 48struct pglist_data *node_data[MAX_NUMNODES];
45fb6cea
AB
49
50EXPORT_SYMBOL(numa_cpu_lookup_table);
25863de0 51EXPORT_SYMBOL(node_to_cpumask_map);
45fb6cea
AB
52EXPORT_SYMBOL(node_data);
53
7e35ef66 54static int primary_domain_index;
237a0989 55static int n_mem_addr_cells, n_mem_size_cells;
0eacd06b
AK
56
57#define FORM0_AFFINITY 0
58#define FORM1_AFFINITY 1
59static int affinity_form;
41eab6f8
AB
60
61#define MAX_DISTANCE_REF_POINTS 4
62static int distance_ref_points_depth;
b08a2a12 63static const __be32 *distance_ref_points;
41eab6f8 64static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
1da177e4 65
25863de0
AB
66/*
67 * Allocate node_to_cpumask_map based on number of available nodes
68 * Requires node_possible_map to be valid.
69 *
9512938b 70 * Note: cpumask_of_node() is not valid until after this is done.
25863de0
AB
71 */
72static void __init setup_node_to_cpumask_map(void)
73{
f9d531b8 74 unsigned int node;
25863de0
AB
75
76 /* setup nr_node_ids if not done yet */
f9d531b8
CS
77 if (nr_node_ids == MAX_NUMNODES)
78 setup_nr_node_ids();
25863de0
AB
79
80 /* allocate the map */
c118baf8 81 for_each_node(node)
25863de0
AB
82 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
83
84 /* cpumask_of_node() will now work */
b9726c26 85 dbg("Node to cpumask map for %u nodes\n", nr_node_ids);
25863de0
AB
86}
87
55671f3c 88static int __init fake_numa_create_new_node(unsigned long end_pfn,
1daa6d08
BS
89 unsigned int *nid)
90{
91 unsigned long long mem;
92 char *p = cmdline;
93 static unsigned int fake_nid;
94 static unsigned long long curr_boundary;
95
96 /*
97 * Modify node id, iff we started creating NUMA nodes
98 * We want to continue from where we left of the last time
99 */
100 if (fake_nid)
101 *nid = fake_nid;
102 /*
103 * In case there are no more arguments to parse, the
104 * node_id should be the same as the last fake node id
105 * (we've handled this above).
106 */
107 if (!p)
108 return 0;
109
110 mem = memparse(p, &p);
111 if (!mem)
112 return 0;
113
114 if (mem < curr_boundary)
115 return 0;
116
117 curr_boundary = mem;
118
119 if ((end_pfn << PAGE_SHIFT) > mem) {
120 /*
121 * Skip commas and spaces
122 */
123 while (*p == ',' || *p == ' ' || *p == '\t')
124 p++;
125
126 cmdline = p;
127 fake_nid++;
128 *nid = fake_nid;
129 dbg("created new fake_node with id %d\n", fake_nid);
130 return 1;
131 }
132 return 0;
133}
134
d4edc5b6
SB
135static void reset_numa_cpu_lookup_table(void)
136{
137 unsigned int cpu;
138
139 for_each_possible_cpu(cpu)
140 numa_cpu_lookup_table[cpu] = -1;
141}
142
d4edc5b6
SB
143static void map_cpu_to_node(int cpu, int node)
144{
145 update_numa_cpu_lookup_table(cpu, node);
45fb6cea 146
bf4b85b0
NL
147 dbg("adding cpu %d to node %d\n", cpu, node);
148
25863de0
AB
149 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
150 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
1da177e4
LT
151}
152
39bf990e 153#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
1da177e4
LT
154static void unmap_cpu_from_node(unsigned long cpu)
155{
156 int node = numa_cpu_lookup_table[cpu];
157
158 dbg("removing cpu %lu from node %d\n", cpu, node);
159
25863de0 160 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
429f4d8d 161 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
1da177e4
LT
162 } else {
163 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
164 cpu, node);
165 }
166}
39bf990e 167#endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
1da177e4 168
d62c8dee
NR
169int cpu_distance(__be32 *cpu1_assoc, __be32 *cpu2_assoc)
170{
171 int dist = 0;
172
173 int i, index;
174
175 for (i = 0; i < distance_ref_points_depth; i++) {
176 index = be32_to_cpu(distance_ref_points[i]);
177 if (cpu1_assoc[index] == cpu2_assoc[index])
178 break;
179 dist++;
180 }
181
182 return dist;
183}
184
1da177e4 185/* must hold reference to node during call */
b08a2a12 186static const __be32 *of_get_associativity(struct device_node *dev)
1da177e4 187{
e2eb6392 188 return of_get_property(dev, "ibm,associativity", NULL);
1da177e4
LT
189}
190
41eab6f8
AB
191int __node_distance(int a, int b)
192{
193 int i;
194 int distance = LOCAL_DISTANCE;
195
0eacd06b 196 if (affinity_form == FORM0_AFFINITY)
7122beee 197 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
41eab6f8
AB
198
199 for (i = 0; i < distance_ref_points_depth; i++) {
200 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
201 break;
202
203 /* Double the distance for each NUMA level */
204 distance *= 2;
205 }
206
207 return distance;
208}
12c743eb 209EXPORT_SYMBOL(__node_distance);
41eab6f8 210
8ddc6448
AK
211static int __associativity_to_nid(const __be32 *associativity,
212 int max_array_sz)
41eab6f8 213{
8ddc6448
AK
214 int nid;
215 /*
216 * primary_domain_index is 1 based array index.
217 */
218 int index = primary_domain_index - 1;
41eab6f8 219
8ddc6448
AK
220 if (!numa_enabled || index >= max_array_sz)
221 return NUMA_NO_NODE;
41eab6f8 222
8ddc6448 223 nid = of_read_number(&associativity[index], 1);
b08a2a12 224
8ddc6448
AK
225 /* POWER4 LPAR uses 0xffff as invalid node */
226 if (nid == 0xffff || nid >= nr_node_ids)
227 nid = NUMA_NO_NODE;
228 return nid;
41eab6f8 229}
dbce4562
SD
230/*
231 * Returns nid in the range [0..nr_node_ids], or -1 if no useful NUMA
482ec7c4
NL
232 * info is found.
233 */
b08a2a12 234static int associativity_to_nid(const __be32 *associativity)
1da177e4 235{
8ddc6448 236 int array_sz = of_read_number(associativity, 1);
41eab6f8 237
8ddc6448
AK
238 /* Skip the first element in the associativity array */
239 return __associativity_to_nid((associativity + 1), array_sz);
1da177e4
LT
240}
241
9eff1a38
JL
242/* Returns the nid associated with the given device tree node,
243 * or -1 if not found.
244 */
245static int of_node_to_nid_single(struct device_node *device)
246{
98fa15f3 247 int nid = NUMA_NO_NODE;
b08a2a12 248 const __be32 *tmp;
9eff1a38
JL
249
250 tmp = of_get_associativity(device);
251 if (tmp)
252 nid = associativity_to_nid(tmp);
253 return nid;
254}
255
953039c8
JK
256/* Walk the device tree upwards, looking for an associativity id */
257int of_node_to_nid(struct device_node *device)
258{
98fa15f3 259 int nid = NUMA_NO_NODE;
953039c8
JK
260
261 of_node_get(device);
262 while (device) {
263 nid = of_node_to_nid_single(device);
264 if (nid != -1)
265 break;
266
1def3758 267 device = of_get_next_parent(device);
953039c8
JK
268 }
269 of_node_put(device);
270
271 return nid;
272}
be9ba9ff 273EXPORT_SYMBOL(of_node_to_nid);
953039c8 274
8ddc6448
AK
275static void __initialize_form1_numa_distance(const __be32 *associativity,
276 int max_array_sz)
277{
278 int i, nid;
279
280 if (affinity_form != FORM1_AFFINITY)
281 return;
282
283 nid = __associativity_to_nid(associativity, max_array_sz);
284 if (nid != NUMA_NO_NODE) {
285 for (i = 0; i < distance_ref_points_depth; i++) {
286 const __be32 *entry;
287 int index = be32_to_cpu(distance_ref_points[i]) - 1;
288
289 /*
290 * broken hierarchy, return with broken distance table
291 */
292 if (WARN(index >= max_array_sz, "Broken ibm,associativity property"))
293 return;
294
295 entry = &associativity[index];
296 distance_lookup_table[nid][i] = of_read_number(entry, 1);
297 }
298 }
299}
300
301static void initialize_form1_numa_distance(const __be32 *associativity)
302{
303 int array_sz;
304
305 array_sz = of_read_number(associativity, 1);
306 /* Skip the first element in the associativity array */
307 __initialize_form1_numa_distance(associativity + 1, array_sz);
308}
309
310/*
311 * Used to update distance information w.r.t newly added node.
312 */
313void update_numa_distance(struct device_node *node)
314{
315 if (affinity_form == FORM0_AFFINITY)
316 return;
317 else if (affinity_form == FORM1_AFFINITY) {
318 const __be32 *associativity;
319
320 associativity = of_get_associativity(node);
321 if (!associativity)
322 return;
323
324 initialize_form1_numa_distance(associativity);
325 return;
326 }
327}
328
7e35ef66 329static int __init find_primary_domain_index(void)
1da177e4 330{
7e35ef66 331 int index;
e70606eb 332 struct device_node *root;
1da177e4 333
0eacd06b
AK
334 /*
335 * Check for which form of affinity.
336 */
337 if (firmware_has_feature(FW_FEATURE_OPAL)) {
338 affinity_form = FORM1_AFFINITY;
339 } else if (firmware_has_feature(FW_FEATURE_FORM1_AFFINITY)) {
340 dbg("Using form 1 affinity\n");
341 affinity_form = FORM1_AFFINITY;
342 } else
343 affinity_form = FORM0_AFFINITY;
344
1c8ee733
DS
345 if (firmware_has_feature(FW_FEATURE_OPAL))
346 root = of_find_node_by_path("/ibm,opal");
347 else
348 root = of_find_node_by_path("/rtas");
e70606eb
ME
349 if (!root)
350 root = of_find_node_by_path("/");
1da177e4
LT
351
352 /*
41eab6f8
AB
353 * This property is a set of 32-bit integers, each representing
354 * an index into the ibm,associativity nodes.
355 *
356 * With form 0 affinity the first integer is for an SMP configuration
357 * (should be all 0's) and the second is for a normal NUMA
358 * configuration. We have only one level of NUMA.
359 *
360 * With form 1 affinity the first integer is the most significant
361 * NUMA boundary and the following are progressively less significant
362 * boundaries. There can be more than one level of NUMA.
1da177e4 363 */
e70606eb 364 distance_ref_points = of_get_property(root,
41eab6f8
AB
365 "ibm,associativity-reference-points",
366 &distance_ref_points_depth);
367
368 if (!distance_ref_points) {
369 dbg("NUMA: ibm,associativity-reference-points not found.\n");
370 goto err;
371 }
372
373 distance_ref_points_depth /= sizeof(int);
0eacd06b 374 if (affinity_form == FORM0_AFFINITY) {
41eab6f8
AB
375 if (distance_ref_points_depth < 2) {
376 printk(KERN_WARNING "NUMA: "
0eacd06b 377 "short ibm,associativity-reference-points\n");
41eab6f8
AB
378 goto err;
379 }
380
7e35ef66 381 index = of_read_number(&distance_ref_points[1], 1);
0eacd06b
AK
382 } else {
383 index = of_read_number(distance_ref_points, 1);
1da177e4 384 }
1da177e4 385
41eab6f8
AB
386 /*
387 * Warn and cap if the hardware supports more than
388 * MAX_DISTANCE_REF_POINTS domains.
389 */
390 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
391 printk(KERN_WARNING "NUMA: distance array capped at "
392 "%d entries\n", MAX_DISTANCE_REF_POINTS);
393 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
394 }
395
e70606eb 396 of_node_put(root);
7e35ef66 397 return index;
41eab6f8
AB
398
399err:
e70606eb 400 of_node_put(root);
41eab6f8 401 return -1;
1da177e4
LT
402}
403
84c9fdd1 404static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
1da177e4
LT
405{
406 struct device_node *memory = NULL;
1da177e4
LT
407
408 memory = of_find_node_by_type(memory, "memory");
54c23310 409 if (!memory)
84c9fdd1 410 panic("numa.c: No memory nodes found!");
54c23310 411
a8bda5dd 412 *n_addr_cells = of_n_addr_cells(memory);
9213feea 413 *n_size_cells = of_n_size_cells(memory);
84c9fdd1 414 of_node_put(memory);
1da177e4
LT
415}
416
b08a2a12 417static unsigned long read_n_cells(int n, const __be32 **buf)
1da177e4
LT
418{
419 unsigned long result = 0;
420
421 while (n--) {
b08a2a12 422 result = (result << 32) | of_read_number(*buf, 1);
1da177e4
LT
423 (*buf)++;
424 }
425 return result;
426}
427
8342681d
NF
428struct assoc_arrays {
429 u32 n_arrays;
430 u32 array_sz;
b08a2a12 431 const __be32 *arrays;
8342681d
NF
432};
433
434/*
25985edc 435 * Retrieve and validate the list of associativity arrays for drconf
8342681d
NF
436 * memory from the ibm,associativity-lookup-arrays property of the
437 * device tree..
438 *
439 * The layout of the ibm,associativity-lookup-arrays property is a number N
440 * indicating the number of associativity arrays, followed by a number M
441 * indicating the size of each associativity array, followed by a list
442 * of N associativity arrays.
443 */
35f80deb 444static int of_get_assoc_arrays(struct assoc_arrays *aa)
8342681d 445{
35f80deb 446 struct device_node *memory;
b08a2a12 447 const __be32 *prop;
8342681d
NF
448 u32 len;
449
35f80deb
NF
450 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
451 if (!memory)
452 return -1;
453
8342681d 454 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
35f80deb
NF
455 if (!prop || len < 2 * sizeof(unsigned int)) {
456 of_node_put(memory);
8342681d 457 return -1;
35f80deb 458 }
8342681d 459
b08a2a12
AP
460 aa->n_arrays = of_read_number(prop++, 1);
461 aa->array_sz = of_read_number(prop++, 1);
8342681d 462
35f80deb
NF
463 of_node_put(memory);
464
42b2aa86 465 /* Now that we know the number of arrays and size of each array,
8342681d
NF
466 * revalidate the size of the property read in.
467 */
468 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
469 return -1;
470
471 aa->arrays = prop;
472 return 0;
473}
474
8ddc6448
AK
475static int get_nid_and_numa_distance(struct drmem_lmb *lmb)
476{
477 struct assoc_arrays aa = { .arrays = NULL };
478 int default_nid = NUMA_NO_NODE;
479 int nid = default_nid;
480 int rc, index;
481
482 if ((primary_domain_index < 0) || !numa_enabled)
483 return default_nid;
484
485 rc = of_get_assoc_arrays(&aa);
486 if (rc)
487 return default_nid;
488
489 if (primary_domain_index <= aa.array_sz &&
490 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
491 const __be32 *associativity;
492
493 index = lmb->aa_index * aa.array_sz;
494 associativity = &aa.arrays[index];
495 nid = __associativity_to_nid(associativity, aa.array_sz);
496 if (nid > 0 && affinity_form == FORM1_AFFINITY) {
497 /*
498 * lookup array associativity entries have
499 * no length of the array as the first element.
500 */
501 __initialize_form1_numa_distance(associativity, aa.array_sz);
502 }
503 }
504 return nid;
505}
506
8342681d
NF
507/*
508 * This is like of_node_to_nid_single() for memory represented in the
509 * ibm,dynamic-reconfiguration-memory node.
510 */
72cdd117 511int of_drconf_to_nid_single(struct drmem_lmb *lmb)
8342681d 512{
b88fc309 513 struct assoc_arrays aa = { .arrays = NULL };
ea9f5b70 514 int default_nid = NUMA_NO_NODE;
8342681d 515 int nid = default_nid;
b88fc309
NF
516 int rc, index;
517
7e35ef66 518 if ((primary_domain_index < 0) || !numa_enabled)
ea9f5b70
AK
519 return default_nid;
520
b88fc309
NF
521 rc = of_get_assoc_arrays(&aa);
522 if (rc)
523 return default_nid;
8342681d 524
7e35ef66 525 if (primary_domain_index <= aa.array_sz &&
ea9f5b70 526 !(lmb->flags & DRCONF_MEM_AI_INVALID) && lmb->aa_index < aa.n_arrays) {
8ddc6448 527 const __be32 *associativity;
8342681d 528
8ddc6448
AK
529 index = lmb->aa_index * aa.array_sz;
530 associativity = &aa.arrays[index];
531 nid = __associativity_to_nid(associativity, aa.array_sz);
8342681d 532 }
8342681d
NF
533 return nid;
534}
535
dc909d8b 536#ifdef CONFIG_PPC_SPLPAR
8ddc6448
AK
537
538static int __vphn_get_associativity(long lcpu, __be32 *associativity)
dc909d8b 539{
dc909d8b
SD
540 long rc, hwid;
541
542 /*
543 * On a shared lpar, device tree will not have node associativity.
544 * At this time lppaca, or its __old_status field may not be
545 * updated. Hence kernel cannot detect if its on a shared lpar. So
546 * request an explicit associativity irrespective of whether the
547 * lpar is shared or dedicated. Use the device tree property as a
548 * fallback. cpu_to_phys_id is only valid between
549 * smp_setup_cpu_maps() and smp_setup_pacas().
550 */
551 if (firmware_has_feature(FW_FEATURE_VPHN)) {
552 if (cpu_to_phys_id)
553 hwid = cpu_to_phys_id[lcpu];
554 else
555 hwid = get_hard_smp_processor_id(lcpu);
556
557 rc = hcall_vphn(hwid, VPHN_FLAG_VCPU, associativity);
558 if (rc == H_SUCCESS)
8ddc6448 559 return 0;
dc909d8b
SD
560 }
561
8ddc6448
AK
562 return -1;
563}
564
565static int vphn_get_nid(long lcpu)
566{
567 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
568
569
570 if (!__vphn_get_associativity(lcpu, associativity))
571 return associativity_to_nid(associativity);
572
dc909d8b 573 return NUMA_NO_NODE;
8ddc6448 574
dc909d8b
SD
575}
576#else
8ddc6448
AK
577
578static int __vphn_get_associativity(long lcpu, __be32 *associativity)
579{
580 return -1;
581}
582
dc909d8b
SD
583static int vphn_get_nid(long unused)
584{
585 return NUMA_NO_NODE;
586}
587#endif /* CONFIG_PPC_SPLPAR */
588
1da177e4
LT
589/*
590 * Figure out to which domain a cpu belongs and stick it there.
591 * Return the id of the domain used.
592 */
061d19f2 593static int numa_setup_cpu(unsigned long lcpu)
1da177e4 594{
d4edc5b6 595 struct device_node *cpu;
413e4055
SD
596 int fcpu = cpu_first_thread_sibling(lcpu);
597 int nid = NUMA_NO_NODE;
d4edc5b6 598
a874f100
SD
599 if (!cpu_present(lcpu)) {
600 set_cpu_numa_node(lcpu, first_online_node);
601 return first_online_node;
602 }
603
d4edc5b6
SB
604 /*
605 * If a valid cpu-to-node mapping is already available, use it
606 * directly instead of querying the firmware, since it represents
607 * the most recent mapping notified to us by the platform (eg: VPHN).
413e4055
SD
608 * Since cpu_to_node binding remains the same for all threads in the
609 * core. If a valid cpu-to-node mapping is already available, for
610 * the first thread in the core, use it.
d4edc5b6 611 */
413e4055
SD
612 nid = numa_cpu_lookup_table[fcpu];
613 if (nid >= 0) {
d4edc5b6
SB
614 map_cpu_to_node(lcpu, nid);
615 return nid;
616 }
617
dc909d8b
SD
618 nid = vphn_get_nid(lcpu);
619 if (nid != NUMA_NO_NODE)
620 goto out_present;
621
d4edc5b6 622 cpu = of_get_cpu_node(lcpu, NULL);
1da177e4
LT
623
624 if (!cpu) {
625 WARN_ON(1);
297cf502
LZ
626 if (cpu_present(lcpu))
627 goto out_present;
628 else
629 goto out;
1da177e4
LT
630 }
631
953039c8 632 nid = of_node_to_nid_single(cpu);
dc909d8b 633 of_node_put(cpu);
1da177e4 634
297cf502 635out_present:
ea05ba7c 636 if (nid < 0 || !node_possible(nid))
72c33688 637 nid = first_online_node;
1da177e4 638
413e4055
SD
639 /*
640 * Update for the first thread of the core. All threads of a core
641 * have to be part of the same node. This not only avoids querying
642 * for every other thread in the core, but always avoids a case
643 * where virtual node associativity change causes subsequent threads
644 * of a core to be associated with different nid. However if first
645 * thread is already online, expect it to have a valid mapping.
646 */
647 if (fcpu != lcpu) {
648 WARN_ON(cpu_online(fcpu));
649 map_cpu_to_node(fcpu, nid);
650 }
651
297cf502 652 map_cpu_to_node(lcpu, nid);
297cf502 653out:
cf950b7a 654 return nid;
1da177e4
LT
655}
656
68fb18aa
SB
657static void verify_cpu_node_mapping(int cpu, int node)
658{
659 int base, sibling, i;
660
661 /* Verify that all the threads in the core belong to the same node */
662 base = cpu_first_thread_sibling(cpu);
663
664 for (i = 0; i < threads_per_core; i++) {
665 sibling = base + i;
666
667 if (sibling == cpu || cpu_is_offline(sibling))
668 continue;
669
670 if (cpu_to_node(sibling) != node) {
671 WARN(1, "CPU thread siblings %d and %d don't belong"
672 " to the same node!\n", cpu, sibling);
673 break;
674 }
675 }
676}
677
bdab88e0
SAS
678/* Must run before sched domains notifier. */
679static int ppc_numa_cpu_prepare(unsigned int cpu)
680{
681 int nid;
682
683 nid = numa_setup_cpu(cpu);
684 verify_cpu_node_mapping(cpu, nid);
685 return 0;
686}
687
688static int ppc_numa_cpu_dead(unsigned int cpu)
689{
1da177e4 690#ifdef CONFIG_HOTPLUG_CPU
bdab88e0 691 unmap_cpu_from_node(cpu);
1da177e4 692#endif
bdab88e0 693 return 0;
1da177e4
LT
694}
695
696/*
697 * Check and possibly modify a memory region to enforce the memory limit.
698 *
699 * Returns the size the region should have to enforce the memory limit.
700 * This will either be the original value of size, a truncated value,
701 * or zero. If the returned value of size is 0 the region should be
25985edc 702 * discarded as it lies wholly above the memory limit.
1da177e4 703 */
45fb6cea
AB
704static unsigned long __init numa_enforce_memory_limit(unsigned long start,
705 unsigned long size)
1da177e4
LT
706{
707 /*
95f72d1e 708 * We use memblock_end_of_DRAM() in here instead of memory_limit because
1da177e4 709 * we've already adjusted it for the limit and it takes care of
fe55249d
MM
710 * having memory holes below the limit. Also, in the case of
711 * iommu_is_off, memory_limit is not set but is implicitly enforced.
1da177e4 712 */
1da177e4 713
95f72d1e 714 if (start + size <= memblock_end_of_DRAM())
1da177e4
LT
715 return size;
716
95f72d1e 717 if (start >= memblock_end_of_DRAM())
1da177e4
LT
718 return 0;
719
95f72d1e 720 return memblock_end_of_DRAM() - start;
1da177e4
LT
721}
722
cf00085d
C
723/*
724 * Reads the counter for a given entry in
725 * linux,drconf-usable-memory property
726 */
b08a2a12 727static inline int __init read_usm_ranges(const __be32 **usm)
cf00085d
C
728{
729 /*
3fdfd990 730 * For each lmb in ibm,dynamic-memory a corresponding
cf00085d
C
731 * entry in linux,drconf-usable-memory property contains
732 * a counter followed by that many (base, size) duple.
733 * read the counter from linux,drconf-usable-memory
734 */
735 return read_n_cells(n_mem_size_cells, usm);
736}
737
0204568a
PM
738/*
739 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
740 * node. This assumes n_mem_{addr,size}_cells have been set.
741 */
adfefc60
HB
742static int __init numa_setup_drmem_lmb(struct drmem_lmb *lmb,
743 const __be32 **usm,
744 void *data)
0204568a 745{
514a9cb3
NF
746 unsigned int ranges, is_kexec_kdump = 0;
747 unsigned long base, size, sz;
8342681d 748 int nid;
8342681d 749
514a9cb3
NF
750 /*
751 * Skip this block if the reserved bit is set in flags (0x80)
752 * or if the block is not assigned to this partition (0x8)
753 */
754 if ((lmb->flags & DRCONF_MEM_RESERVED)
755 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
adfefc60 756 return 0;
8342681d 757
514a9cb3 758 if (*usm)
cf00085d
C
759 is_kexec_kdump = 1;
760
514a9cb3
NF
761 base = lmb->base_addr;
762 size = drmem_lmb_size();
763 ranges = 1;
8342681d 764
514a9cb3
NF
765 if (is_kexec_kdump) {
766 ranges = read_usm_ranges(usm);
767 if (!ranges) /* there are no (base, size) duple */
adfefc60 768 return 0;
514a9cb3 769 }
8342681d 770
514a9cb3 771 do {
cf00085d 772 if (is_kexec_kdump) {
514a9cb3
NF
773 base = read_n_cells(n_mem_addr_cells, usm);
774 size = read_n_cells(n_mem_size_cells, usm);
cf00085d 775 }
514a9cb3 776
8ddc6448 777 nid = get_nid_and_numa_distance(lmb);
514a9cb3
NF
778 fake_numa_create_new_node(((base + size) >> PAGE_SHIFT),
779 &nid);
780 node_set_online(nid);
781 sz = numa_enforce_memory_limit(base, size);
782 if (sz)
783 memblock_set_node(base, sz, &memblock.memory, nid);
784 } while (--ranges);
adfefc60
HB
785
786 return 0;
0204568a
PM
787}
788
1da177e4
LT
789static int __init parse_numa_properties(void)
790{
94db7c5e 791 struct device_node *memory;
482ec7c4 792 int default_nid = 0;
1da177e4 793 unsigned long i;
8ddc6448 794 const __be32 *associativity;
1da177e4
LT
795
796 if (numa_enabled == 0) {
797 printk(KERN_WARNING "NUMA disabled by user\n");
798 return -1;
799 }
800
7e35ef66 801 primary_domain_index = find_primary_domain_index();
1da177e4 802
7e35ef66 803 if (primary_domain_index < 0) {
495c2ff4 804 /*
7e35ef66 805 * if we fail to parse primary_domain_index from device tree
495c2ff4
AK
806 * mark the numa disabled, boot with numa disabled.
807 */
808 numa_enabled = false;
7e35ef66 809 return primary_domain_index;
495c2ff4 810 }
1da177e4 811
7e35ef66 812 dbg("NUMA associativity depth for CPU/Memory: %d\n", primary_domain_index);
bf4b85b0 813
1da177e4 814 /*
482ec7c4
NL
815 * Even though we connect cpus to numa domains later in SMP
816 * init, we need to know the node ids now. This is because
817 * each node to be onlined must have NODE_DATA etc backing it.
1da177e4 818 */
482ec7c4 819 for_each_present_cpu(i) {
8ddc6448 820 __be32 vphn_assoc[VPHN_ASSOC_BUFSIZE];
dfbe93a2 821 struct device_node *cpu;
8ddc6448 822 int nid = NUMA_NO_NODE;
1da177e4 823
8ddc6448
AK
824 memset(vphn_assoc, 0, VPHN_ASSOC_BUFSIZE * sizeof(__be32));
825
826 if (__vphn_get_associativity(i, vphn_assoc) == 0) {
827 nid = associativity_to_nid(vphn_assoc);
828 initialize_form1_numa_distance(vphn_assoc);
829 } else {
830
831 /*
832 * Don't fall back to default_nid yet -- we will plug
833 * cpus into nodes once the memory scan has discovered
834 * the topology.
835 */
6398eaa2
SD
836 cpu = of_get_cpu_node(i, NULL);
837 BUG_ON(!cpu);
8ddc6448
AK
838
839 associativity = of_get_associativity(cpu);
840 if (associativity) {
841 nid = associativity_to_nid(associativity);
842 initialize_form1_numa_distance(associativity);
843 }
6398eaa2
SD
844 of_node_put(cpu);
845 }
846
10f78fd0 847 node_set_online(nid);
1da177e4
LT
848 }
849
237a0989 850 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
94db7c5e
AB
851
852 for_each_node_by_type(memory, "memory") {
1da177e4
LT
853 unsigned long start;
854 unsigned long size;
cf950b7a 855 int nid;
1da177e4 856 int ranges;
b08a2a12 857 const __be32 *memcell_buf;
1da177e4
LT
858 unsigned int len;
859
e2eb6392 860 memcell_buf = of_get_property(memory,
ba759485
ME
861 "linux,usable-memory", &len);
862 if (!memcell_buf || len <= 0)
e2eb6392 863 memcell_buf = of_get_property(memory, "reg", &len);
1da177e4
LT
864 if (!memcell_buf || len <= 0)
865 continue;
866
cc5d0189
BH
867 /* ranges in cell */
868 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1da177e4
LT
869new_range:
870 /* these are order-sensitive, and modify the buffer pointer */
237a0989
MK
871 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
872 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1da177e4 873
482ec7c4
NL
874 /*
875 * Assumption: either all memory nodes or none will
876 * have associativity properties. If none, then
877 * everything goes to default_nid.
878 */
8ddc6448
AK
879 associativity = of_get_associativity(memory);
880 if (associativity) {
881 nid = associativity_to_nid(associativity);
882 initialize_form1_numa_distance(associativity);
883 } else
482ec7c4 884 nid = default_nid;
1daa6d08
BS
885
886 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
482ec7c4 887 node_set_online(nid);
1da177e4 888
7656cd8e
RA
889 size = numa_enforce_memory_limit(start, size);
890 if (size)
891 memblock_set_node(start, size, &memblock.memory, nid);
1da177e4
LT
892
893 if (--ranges)
894 goto new_range;
895 }
896
0204568a 897 /*
dfbe93a2
AB
898 * Now do the same thing for each MEMBLOCK listed in the
899 * ibm,dynamic-memory property in the
900 * ibm,dynamic-reconfiguration-memory node.
0204568a
PM
901 */
902 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
514a9cb3 903 if (memory) {
adfefc60 904 walk_drmem_lmbs(memory, NULL, numa_setup_drmem_lmb);
514a9cb3
NF
905 of_node_put(memory);
906 }
0204568a 907
1da177e4
LT
908 return 0;
909}
910
911static void __init setup_nonnuma(void)
912{
95f72d1e
YL
913 unsigned long top_of_ram = memblock_end_of_DRAM();
914 unsigned long total_ram = memblock_phys_mem_size();
c67c3cb4 915 unsigned long start_pfn, end_pfn;
28be7072 916 unsigned int nid = 0;
c9118e6c 917 int i;
1da177e4 918
e110b281 919 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1da177e4 920 top_of_ram, total_ram);
e110b281 921 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
1da177e4
LT
922 (top_of_ram - total_ram) >> 20);
923
c9118e6c 924 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
1daa6d08 925 fake_numa_create_new_node(end_pfn, &nid);
1d7cfe18 926 memblock_set_node(PFN_PHYS(start_pfn),
e7e8de59
TC
927 PFN_PHYS(end_pfn - start_pfn),
928 &memblock.memory, nid);
1daa6d08 929 node_set_online(nid);
c67c3cb4 930 }
1da177e4
LT
931}
932
4b703a23
AB
933void __init dump_numa_cpu_topology(void)
934{
935 unsigned int node;
936 unsigned int cpu, count;
937
495c2ff4 938 if (!numa_enabled)
4b703a23
AB
939 return;
940
941 for_each_online_node(node) {
8467801c 942 pr_info("Node %d CPUs:", node);
4b703a23
AB
943
944 count = 0;
945 /*
946 * If we used a CPU iterator here we would miss printing
947 * the holes in the cpumap.
948 */
25863de0
AB
949 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
950 if (cpumask_test_cpu(cpu,
951 node_to_cpumask_map[node])) {
4b703a23 952 if (count == 0)
8467801c 953 pr_cont(" %u", cpu);
4b703a23
AB
954 ++count;
955 } else {
956 if (count > 1)
8467801c 957 pr_cont("-%u", cpu - 1);
4b703a23
AB
958 count = 0;
959 }
960 }
961
962 if (count > 1)
8467801c
AK
963 pr_cont("-%u", nr_cpu_ids - 1);
964 pr_cont("\n");
4b703a23
AB
965 }
966}
967
10239733
AB
968/* Initialize NODE_DATA for a node on the local memory */
969static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
4a618669 970{
10239733
AB
971 u64 spanned_pages = end_pfn - start_pfn;
972 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
973 u64 nd_pa;
974 void *nd;
975 int tnid;
4a618669 976
9a8dd708 977 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
33755574
MR
978 if (!nd_pa)
979 panic("Cannot allocate %zu bytes for node %d data\n",
980 nd_size, nid);
981
10239733 982 nd = __va(nd_pa);
4a618669 983
10239733
AB
984 /* report and initialize */
985 pr_info(" NODE_DATA [mem %#010Lx-%#010Lx]\n",
986 nd_pa, nd_pa + nd_size - 1);
987 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
988 if (tnid != nid)
989 pr_info(" NODE_DATA(%d) on node %d\n", nid, tnid);
4a618669 990
10239733
AB
991 node_data[nid] = nd;
992 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
993 NODE_DATA(nid)->node_id = nid;
994 NODE_DATA(nid)->node_start_pfn = start_pfn;
995 NODE_DATA(nid)->node_spanned_pages = spanned_pages;
996}
4a618669 997
a346137e
MB
998static void __init find_possible_nodes(void)
999{
1000 struct device_node *rtas;
9c7248bb 1001 const __be32 *domains = NULL;
f9f130ff
SD
1002 int prop_length, max_nodes;
1003 u32 i;
a346137e 1004
495c2ff4 1005 if (!numa_enabled)
a346137e
MB
1006 return;
1007
1008 rtas = of_find_node_by_path("/rtas");
1009 if (!rtas)
1010 return;
1011
f9f130ff
SD
1012 /*
1013 * ibm,current-associativity-domains is a fairly recent property. If
1014 * it doesn't exist, then fallback on ibm,max-associativity-domains.
1015 * Current denotes what the platform can support compared to max
1016 * which denotes what the Hypervisor can support.
9c7248bb
LD
1017 *
1018 * If the LPAR is migratable, new nodes might be activated after a LPM,
1019 * so we should consider the max number in that case.
f9f130ff 1020 */
9c7248bb
LD
1021 if (!of_get_property(of_root, "ibm,migratable-partition", NULL))
1022 domains = of_get_property(rtas,
1023 "ibm,current-associativity-domains",
1024 &prop_length);
f9f130ff
SD
1025 if (!domains) {
1026 domains = of_get_property(rtas, "ibm,max-associativity-domains",
1027 &prop_length);
1028 if (!domains)
67df7784
SD
1029 goto out;
1030 }
a346137e 1031
7e35ef66 1032 max_nodes = of_read_number(&domains[primary_domain_index], 1);
9c7248bb
LD
1033 pr_info("Partition configured for %d NUMA nodes.\n", max_nodes);
1034
f9f130ff 1035 for (i = 0; i < max_nodes; i++) {
ea05ba7c 1036 if (!node_possible(i))
a346137e 1037 node_set(i, node_possible_map);
a346137e
MB
1038 }
1039
f9f130ff 1040 prop_length /= sizeof(int);
7e35ef66 1041 if (prop_length > primary_domain_index + 2)
f9f130ff
SD
1042 coregroup_enabled = 1;
1043
a346137e
MB
1044out:
1045 of_node_put(rtas);
1046}
1047
9bd9be00 1048void __init mem_topology_setup(void)
1da177e4 1049{
9bd9be00 1050 int cpu;
1da177e4 1051
e75130f2
SD
1052 /*
1053 * Linux/mm assumes node 0 to be online at boot. However this is not
1054 * true on PowerPC, where node 0 is similar to any other node, it
1055 * could be cpuless, memoryless node. So force node 0 to be offline
1056 * for now. This will prevent cpuless, memoryless node 0 showing up
1057 * unnecessarily as online. If a node has cpus or memory that need
1058 * to be online, then node will anyway be marked online.
1059 */
1060 node_set_offline(0);
1061
1da177e4
LT
1062 if (parse_numa_properties())
1063 setup_nonnuma();
1da177e4 1064
3af229f2 1065 /*
a346137e
MB
1066 * Modify the set of possible NUMA nodes to reflect information
1067 * available about the set of online nodes, and the set of nodes
1068 * that we expect to make use of for this platform's affinity
1069 * calculations.
3af229f2
NA
1070 */
1071 nodes_and(node_possible_map, node_possible_map, node_online_map);
1072
a346137e
MB
1073 find_possible_nodes();
1074
9bd9be00
NP
1075 setup_node_to_cpumask_map();
1076
1077 reset_numa_cpu_lookup_table();
1078
a874f100
SD
1079 for_each_possible_cpu(cpu) {
1080 /*
1081 * Powerpc with CONFIG_NUMA always used to have a node 0,
1082 * even if it was memoryless or cpuless. For all cpus that
1083 * are possible but not present, cpu_to_node() would point
1084 * to node 0. To remove a cpuless, memoryless dummy node,
1085 * powerpc need to make sure all possible but not present
1086 * cpu_to_node are set to a proper node.
1087 */
9bd9be00 1088 numa_setup_cpu(cpu);
a874f100 1089 }
9bd9be00
NP
1090}
1091
1092void __init initmem_init(void)
1093{
1094 int nid;
1095
1096 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1097 max_pfn = max_low_pfn;
1098
1099 memblock_dump_all();
1100
1da177e4 1101 for_each_online_node(nid) {
c67c3cb4 1102 unsigned long start_pfn, end_pfn;
1da177e4 1103
c67c3cb4 1104 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
10239733 1105 setup_node_data(nid, start_pfn, end_pfn);
4a618669 1106 }
d3f6204a 1107
21098b9e 1108 sparse_init();
25863de0 1109
2fabf084
NA
1110 /*
1111 * We need the numa_cpu_lookup_table to be accurate for all CPUs,
1112 * even before we online them, so that we can use cpu_to_{node,mem}
1113 * early in boot, cf. smp_prepare_cpus().
bdab88e0
SAS
1114 * _nocalls() + manual invocation is used because cpuhp is not yet
1115 * initialized for the boot CPU.
2fabf084 1116 */
73c1b41e 1117 cpuhp_setup_state_nocalls(CPUHP_POWER_NUMA_PREPARE, "powerpc/numa:prepare",
bdab88e0 1118 ppc_numa_cpu_prepare, ppc_numa_cpu_dead);
1da177e4
LT
1119}
1120
1da177e4
LT
1121static int __init early_numa(char *p)
1122{
1123 if (!p)
1124 return 0;
1125
1126 if (strstr(p, "off"))
1127 numa_enabled = 0;
1128
1129 if (strstr(p, "debug"))
1130 numa_debug = 1;
1131
1daa6d08
BS
1132 p = strstr(p, "fake=");
1133 if (p)
1134 cmdline = p + strlen("fake=");
1135
1da177e4
LT
1136 return 0;
1137}
1138early_param("numa", early_numa);
237a0989
MK
1139
1140#ifdef CONFIG_MEMORY_HOTPLUG
0db9360a 1141/*
0f16ef7f
NF
1142 * Find the node associated with a hot added memory section for
1143 * memory represented in the device tree by the property
1144 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
0db9360a 1145 */
514a9cb3 1146static int hot_add_drconf_scn_to_nid(unsigned long scn_addr)
0db9360a 1147{
514a9cb3 1148 struct drmem_lmb *lmb;
3fdfd990 1149 unsigned long lmb_size;
98fa15f3 1150 int nid = NUMA_NO_NODE;
0db9360a 1151
514a9cb3 1152 lmb_size = drmem_lmb_size();
0db9360a 1153
514a9cb3 1154 for_each_drmem_lmb(lmb) {
0db9360a
NF
1155 /* skip this block if it is reserved or not assigned to
1156 * this partition */
514a9cb3
NF
1157 if ((lmb->flags & DRCONF_MEM_RESERVED)
1158 || !(lmb->flags & DRCONF_MEM_ASSIGNED))
0db9360a
NF
1159 continue;
1160
514a9cb3
NF
1161 if ((scn_addr < lmb->base_addr)
1162 || (scn_addr >= (lmb->base_addr + lmb_size)))
0f16ef7f
NF
1163 continue;
1164
514a9cb3 1165 nid = of_drconf_to_nid_single(lmb);
0f16ef7f
NF
1166 break;
1167 }
1168
1169 return nid;
1170}
1171
1172/*
1173 * Find the node associated with a hot added memory section for memory
1174 * represented in the device tree as a node (i.e. memory@XXXX) for
95f72d1e 1175 * each memblock.
0f16ef7f 1176 */
ec32dd66 1177static int hot_add_node_scn_to_nid(unsigned long scn_addr)
0f16ef7f 1178{
94db7c5e 1179 struct device_node *memory;
98fa15f3 1180 int nid = NUMA_NO_NODE;
0f16ef7f 1181
94db7c5e 1182 for_each_node_by_type(memory, "memory") {
0f16ef7f
NF
1183 unsigned long start, size;
1184 int ranges;
b08a2a12 1185 const __be32 *memcell_buf;
0f16ef7f
NF
1186 unsigned int len;
1187
1188 memcell_buf = of_get_property(memory, "reg", &len);
1189 if (!memcell_buf || len <= 0)
1190 continue;
1191
1192 /* ranges in cell */
1193 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1194
1195 while (ranges--) {
1196 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1197 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1198
1199 if ((scn_addr < start) || (scn_addr >= (start + size)))
1200 continue;
1201
1202 nid = of_node_to_nid_single(memory);
1203 break;
1204 }
0db9360a 1205
0f16ef7f
NF
1206 if (nid >= 0)
1207 break;
0db9360a
NF
1208 }
1209
60831842
AB
1210 of_node_put(memory);
1211
0f16ef7f 1212 return nid;
0db9360a
NF
1213}
1214
237a0989
MK
1215/*
1216 * Find the node associated with a hot added memory section. Section
95f72d1e
YL
1217 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1218 * sections are fully contained within a single MEMBLOCK.
237a0989
MK
1219 */
1220int hot_add_scn_to_nid(unsigned long scn_addr)
1221{
1222 struct device_node *memory = NULL;
4a3bac4e 1223 int nid;
237a0989 1224
495c2ff4 1225 if (!numa_enabled)
72c33688 1226 return first_online_node;
0db9360a
NF
1227
1228 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1229 if (memory) {
514a9cb3 1230 nid = hot_add_drconf_scn_to_nid(scn_addr);
0db9360a 1231 of_node_put(memory);
0f16ef7f
NF
1232 } else {
1233 nid = hot_add_node_scn_to_nid(scn_addr);
0db9360a 1234 }
237a0989 1235
2a8628d4 1236 if (nid < 0 || !node_possible(nid))
72c33688 1237 nid = first_online_node;
237a0989 1238
0f16ef7f 1239 return nid;
237a0989 1240}
0f16ef7f 1241
cd34206e
NA
1242static u64 hot_add_drconf_memory_max(void)
1243{
e70bd3ae 1244 struct device_node *memory = NULL;
45b64ee6 1245 struct device_node *dn = NULL;
45b64ee6 1246 const __be64 *lrdr = NULL;
45b64ee6
BR
1247
1248 dn = of_find_node_by_path("/rtas");
1249 if (dn) {
1250 lrdr = of_get_property(dn, "ibm,lrdr-capacity", NULL);
1251 of_node_put(dn);
1252 if (lrdr)
1253 return be64_to_cpup(lrdr);
1254 }
cd34206e 1255
e70bd3ae
BR
1256 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1257 if (memory) {
e70bd3ae 1258 of_node_put(memory);
514a9cb3 1259 return drmem_lmb_memory_max();
e70bd3ae 1260 }
45b64ee6 1261 return 0;
cd34206e
NA
1262}
1263
1264/*
1265 * memory_hotplug_max - return max address of memory that may be added
1266 *
1267 * This is currently only used on systems that support drconfig memory
1268 * hotplug.
1269 */
1270u64 memory_hotplug_max(void)
1271{
1272 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1273}
237a0989 1274#endif /* CONFIG_MEMORY_HOTPLUG */
9eff1a38 1275
bd03403a 1276/* Virtual Processor Home Node (VPHN) support */
39bf990e 1277#ifdef CONFIG_PPC_SPLPAR
17f444c0 1278static int topology_inited;
9eff1a38 1279
9eff1a38
JL
1280/*
1281 * Retrieve the new associativity information for a virtual processor's
1282 * home node.
1283 */
9eff1a38 1284static long vphn_get_associativity(unsigned long cpu,
b08a2a12 1285 __be32 *associativity)
9eff1a38 1286{
cd9d6cc7 1287 long rc;
9eff1a38 1288
ef34e0ef
NR
1289 rc = hcall_vphn(get_hard_smp_processor_id(cpu),
1290 VPHN_FLAG_VCPU, associativity);
9eff1a38
JL
1291
1292 switch (rc) {
76b7bfb1
SD
1293 case H_SUCCESS:
1294 dbg("VPHN hcall succeeded. Reset polling...\n");
76b7bfb1
SD
1295 goto out;
1296
9eff1a38 1297 case H_FUNCTION:
76b7bfb1 1298 pr_err_ratelimited("VPHN unsupported. Disabling polling...\n");
9eff1a38
JL
1299 break;
1300 case H_HARDWARE:
76b7bfb1 1301 pr_err_ratelimited("hcall_vphn() experienced a hardware fault "
9eff1a38 1302 "preventing VPHN. Disabling polling...\n");
17f444c0 1303 break;
76b7bfb1
SD
1304 case H_PARAMETER:
1305 pr_err_ratelimited("hcall_vphn() was passed an invalid parameter. "
1306 "Disabling polling...\n");
1307 break;
1308 default:
1309 pr_err_ratelimited("hcall_vphn() returned %ld. Disabling polling...\n"
1310 , rc);
17f444c0 1311 break;
9eff1a38 1312 }
76b7bfb1 1313out:
9eff1a38
JL
1314 return rc;
1315}
1316
e67e02a5 1317int find_and_online_cpu_nid(int cpu)
ea05ba7c
MB
1318{
1319 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1320 int new_nid;
1321
1322 /* Use associativity from first thread for all siblings */
2483ef05
SD
1323 if (vphn_get_associativity(cpu, associativity))
1324 return cpu_to_node(cpu);
1325
ea05ba7c
MB
1326 new_nid = associativity_to_nid(associativity);
1327 if (new_nid < 0 || !node_possible(new_nid))
1328 new_nid = first_online_node;
1329
1330 if (NODE_DATA(new_nid) == NULL) {
1331#ifdef CONFIG_MEMORY_HOTPLUG
1332 /*
1333 * Need to ensure that NODE_DATA is initialized for a node from
1334 * available memory (see memblock_alloc_try_nid). If unable to
1335 * init the node, then default to nearest node that has memory
ac1788cc
SD
1336 * installed. Skip onlining a node if the subsystems are not
1337 * yet initialized.
ea05ba7c 1338 */
ac1788cc 1339 if (!topology_inited || try_online_node(new_nid))
ea05ba7c
MB
1340 new_nid = first_online_node;
1341#else
1342 /*
1343 * Default to using the nearest node that has memory installed.
1344 * Otherwise, it would be necessary to patch the kernel MM code
1345 * to deal with more memoryless-node error conditions.
1346 */
1347 new_nid = first_online_node;
1348#endif
1349 }
1350
e67e02a5
MB
1351 pr_debug("%s:%d cpu %d nid %d\n", __FUNCTION__, __LINE__,
1352 cpu, new_nid);
ea05ba7c
MB
1353 return new_nid;
1354}
1355
72730bfc
SD
1356int cpu_to_coregroup_id(int cpu)
1357{
fa35e868
SD
1358 __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
1359 int index;
1360
1361 if (cpu < 0 || cpu > nr_cpu_ids)
1362 return -1;
1363
1364 if (!coregroup_enabled)
1365 goto out;
1366
1367 if (!firmware_has_feature(FW_FEATURE_VPHN))
1368 goto out;
1369
1370 if (vphn_get_associativity(cpu, associativity))
1371 goto out;
1372
1373 index = of_read_number(associativity, 1);
7e35ef66 1374 if (index > primary_domain_index + 1)
fa35e868
SD
1375 return of_read_number(&associativity[index - 1], 1);
1376
1377out:
72730bfc
SD
1378 return cpu_to_core_id(cpu);
1379}
1380
e04fa612
NF
1381static int topology_update_init(void)
1382{
17f444c0 1383 topology_inited = 1;
e04fa612 1384 return 0;
9eff1a38 1385}
e04fa612 1386device_initcall(topology_update_init);
39bf990e 1387#endif /* CONFIG_PPC_SPLPAR */