]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/mm/numa.c
powerpc/pseries: Update NUMA VDSO information when updating CPU maps
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / mm / numa.c
CommitLineData
1da177e4
LT
1/*
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>
4b16f8e2 16#include <linux/export.h>
1da177e4
LT
17#include <linux/nodemask.h>
18#include <linux/cpu.h>
19#include <linux/notifier.h>
95f72d1e 20#include <linux/memblock.h>
6df1646e 21#include <linux/of.h>
06eccea6 22#include <linux/pfn.h>
9eff1a38
JL
23#include <linux/cpuset.h>
24#include <linux/node.h>
30c05350 25#include <linux/stop_machine.h>
45fb6cea 26#include <asm/sparsemem.h>
d9b2b2a2 27#include <asm/prom.h>
2249ca9d 28#include <asm/smp.h>
9eff1a38
JL
29#include <asm/firmware.h>
30#include <asm/paca.h>
39bf990e 31#include <asm/hvcall.h>
ae3a197e 32#include <asm/setup.h>
176bbf14 33#include <asm/vdso.h>
1da177e4
LT
34
35static int numa_enabled = 1;
36
1daa6d08
BS
37static char *cmdline __initdata;
38
1da177e4
LT
39static int numa_debug;
40#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
41
45fb6cea 42int numa_cpu_lookup_table[NR_CPUS];
25863de0 43cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
1da177e4 44struct pglist_data *node_data[MAX_NUMNODES];
45fb6cea
AB
45
46EXPORT_SYMBOL(numa_cpu_lookup_table);
25863de0 47EXPORT_SYMBOL(node_to_cpumask_map);
45fb6cea
AB
48EXPORT_SYMBOL(node_data);
49
1da177e4 50static int min_common_depth;
237a0989 51static int n_mem_addr_cells, n_mem_size_cells;
41eab6f8
AB
52static int form1_affinity;
53
54#define MAX_DISTANCE_REF_POINTS 4
55static int distance_ref_points_depth;
56static const unsigned int *distance_ref_points;
57static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
1da177e4 58
25863de0
AB
59/*
60 * Allocate node_to_cpumask_map based on number of available nodes
61 * Requires node_possible_map to be valid.
62 *
9512938b 63 * Note: cpumask_of_node() is not valid until after this is done.
25863de0
AB
64 */
65static void __init setup_node_to_cpumask_map(void)
66{
67 unsigned int node, num = 0;
68
69 /* setup nr_node_ids if not done yet */
70 if (nr_node_ids == MAX_NUMNODES) {
71 for_each_node_mask(node, node_possible_map)
72 num = node;
73 nr_node_ids = num + 1;
74 }
75
76 /* allocate the map */
77 for (node = 0; node < nr_node_ids; node++)
78 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
79
80 /* cpumask_of_node() will now work */
81 dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
82}
83
55671f3c 84static int __init fake_numa_create_new_node(unsigned long end_pfn,
1daa6d08
BS
85 unsigned int *nid)
86{
87 unsigned long long mem;
88 char *p = cmdline;
89 static unsigned int fake_nid;
90 static unsigned long long curr_boundary;
91
92 /*
93 * Modify node id, iff we started creating NUMA nodes
94 * We want to continue from where we left of the last time
95 */
96 if (fake_nid)
97 *nid = fake_nid;
98 /*
99 * In case there are no more arguments to parse, the
100 * node_id should be the same as the last fake node id
101 * (we've handled this above).
102 */
103 if (!p)
104 return 0;
105
106 mem = memparse(p, &p);
107 if (!mem)
108 return 0;
109
110 if (mem < curr_boundary)
111 return 0;
112
113 curr_boundary = mem;
114
115 if ((end_pfn << PAGE_SHIFT) > mem) {
116 /*
117 * Skip commas and spaces
118 */
119 while (*p == ',' || *p == ' ' || *p == '\t')
120 p++;
121
122 cmdline = p;
123 fake_nid++;
124 *nid = fake_nid;
125 dbg("created new fake_node with id %d\n", fake_nid);
126 return 1;
127 }
128 return 0;
129}
130
8f64e1f2 131/*
5dfe8660 132 * get_node_active_region - Return active region containing pfn
e8170372 133 * Active range returned is empty if none found.
5dfe8660
TH
134 * @pfn: The page to return the region for
135 * @node_ar: Returned set to the active region containing @pfn
8f64e1f2 136 */
5dfe8660
TH
137static void __init get_node_active_region(unsigned long pfn,
138 struct node_active_region *node_ar)
8f64e1f2 139{
5dfe8660
TH
140 unsigned long start_pfn, end_pfn;
141 int i, nid;
142
143 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
144 if (pfn >= start_pfn && pfn < end_pfn) {
145 node_ar->nid = nid;
146 node_ar->start_pfn = start_pfn;
147 node_ar->end_pfn = end_pfn;
148 break;
149 }
150 }
8f64e1f2
JT
151}
152
39bf990e 153static void map_cpu_to_node(int cpu, int node)
1da177e4
LT
154{
155 numa_cpu_lookup_table[cpu] = node;
45fb6cea 156
bf4b85b0
NL
157 dbg("adding cpu %d to node %d\n", cpu, node);
158
25863de0
AB
159 if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
160 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
1da177e4
LT
161}
162
39bf990e 163#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
1da177e4
LT
164static void unmap_cpu_from_node(unsigned long cpu)
165{
166 int node = numa_cpu_lookup_table[cpu];
167
168 dbg("removing cpu %lu from node %d\n", cpu, node);
169
25863de0 170 if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
429f4d8d 171 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
1da177e4
LT
172 } else {
173 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
174 cpu, node);
175 }
176}
39bf990e 177#endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
1da177e4 178
1da177e4 179/* must hold reference to node during call */
a7f67bdf 180static const int *of_get_associativity(struct device_node *dev)
1da177e4 181{
e2eb6392 182 return of_get_property(dev, "ibm,associativity", NULL);
1da177e4
LT
183}
184
cf00085d
C
185/*
186 * Returns the property linux,drconf-usable-memory if
187 * it exists (the property exists only in kexec/kdump kernels,
188 * added by kexec-tools)
189 */
190static const u32 *of_get_usable_memory(struct device_node *memory)
191{
192 const u32 *prop;
193 u32 len;
194 prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
195 if (!prop || len < sizeof(unsigned int))
196 return 0;
197 return prop;
198}
199
41eab6f8
AB
200int __node_distance(int a, int b)
201{
202 int i;
203 int distance = LOCAL_DISTANCE;
204
205 if (!form1_affinity)
7122beee 206 return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE);
41eab6f8
AB
207
208 for (i = 0; i < distance_ref_points_depth; i++) {
209 if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
210 break;
211
212 /* Double the distance for each NUMA level */
213 distance *= 2;
214 }
215
216 return distance;
217}
218
219static void initialize_distance_lookup_table(int nid,
220 const unsigned int *associativity)
221{
222 int i;
223
224 if (!form1_affinity)
225 return;
226
227 for (i = 0; i < distance_ref_points_depth; i++) {
228 distance_lookup_table[nid][i] =
229 associativity[distance_ref_points[i]];
230 }
231}
232
482ec7c4
NL
233/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
234 * info is found.
235 */
9eff1a38 236static int associativity_to_nid(const unsigned int *associativity)
1da177e4 237{
482ec7c4 238 int nid = -1;
1da177e4
LT
239
240 if (min_common_depth == -1)
482ec7c4 241 goto out;
1da177e4 242
9eff1a38
JL
243 if (associativity[0] >= min_common_depth)
244 nid = associativity[min_common_depth];
bc16a759
NL
245
246 /* POWER4 LPAR uses 0xffff as invalid node */
482ec7c4
NL
247 if (nid == 0xffff || nid >= MAX_NUMNODES)
248 nid = -1;
41eab6f8 249
9eff1a38
JL
250 if (nid > 0 && associativity[0] >= distance_ref_points_depth)
251 initialize_distance_lookup_table(nid, associativity);
41eab6f8 252
482ec7c4 253out:
cf950b7a 254 return nid;
1da177e4
LT
255}
256
9eff1a38
JL
257/* Returns the nid associated with the given device tree node,
258 * or -1 if not found.
259 */
260static int of_node_to_nid_single(struct device_node *device)
261{
262 int nid = -1;
263 const unsigned int *tmp;
264
265 tmp = of_get_associativity(device);
266 if (tmp)
267 nid = associativity_to_nid(tmp);
268 return nid;
269}
270
953039c8
JK
271/* Walk the device tree upwards, looking for an associativity id */
272int of_node_to_nid(struct device_node *device)
273{
274 struct device_node *tmp;
275 int nid = -1;
276
277 of_node_get(device);
278 while (device) {
279 nid = of_node_to_nid_single(device);
280 if (nid != -1)
281 break;
282
283 tmp = device;
284 device = of_get_parent(tmp);
285 of_node_put(tmp);
286 }
287 of_node_put(device);
288
289 return nid;
290}
291EXPORT_SYMBOL_GPL(of_node_to_nid);
292
1da177e4
LT
293static int __init find_min_common_depth(void)
294{
41eab6f8 295 int depth;
e70606eb 296 struct device_node *root;
1da177e4 297
1c8ee733
DS
298 if (firmware_has_feature(FW_FEATURE_OPAL))
299 root = of_find_node_by_path("/ibm,opal");
300 else
301 root = of_find_node_by_path("/rtas");
e70606eb
ME
302 if (!root)
303 root = of_find_node_by_path("/");
1da177e4
LT
304
305 /*
41eab6f8
AB
306 * This property is a set of 32-bit integers, each representing
307 * an index into the ibm,associativity nodes.
308 *
309 * With form 0 affinity the first integer is for an SMP configuration
310 * (should be all 0's) and the second is for a normal NUMA
311 * configuration. We have only one level of NUMA.
312 *
313 * With form 1 affinity the first integer is the most significant
314 * NUMA boundary and the following are progressively less significant
315 * boundaries. There can be more than one level of NUMA.
1da177e4 316 */
e70606eb 317 distance_ref_points = of_get_property(root,
41eab6f8
AB
318 "ibm,associativity-reference-points",
319 &distance_ref_points_depth);
320
321 if (!distance_ref_points) {
322 dbg("NUMA: ibm,associativity-reference-points not found.\n");
323 goto err;
324 }
325
326 distance_ref_points_depth /= sizeof(int);
1da177e4 327
8002b0c5
NF
328 if (firmware_has_feature(FW_FEATURE_OPAL) ||
329 firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) {
330 dbg("Using form 1 affinity\n");
1c8ee733 331 form1_affinity = 1;
4b83c330
AB
332 }
333
41eab6f8
AB
334 if (form1_affinity) {
335 depth = distance_ref_points[0];
1da177e4 336 } else {
41eab6f8
AB
337 if (distance_ref_points_depth < 2) {
338 printk(KERN_WARNING "NUMA: "
339 "short ibm,associativity-reference-points\n");
340 goto err;
341 }
342
343 depth = distance_ref_points[1];
1da177e4 344 }
1da177e4 345
41eab6f8
AB
346 /*
347 * Warn and cap if the hardware supports more than
348 * MAX_DISTANCE_REF_POINTS domains.
349 */
350 if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
351 printk(KERN_WARNING "NUMA: distance array capped at "
352 "%d entries\n", MAX_DISTANCE_REF_POINTS);
353 distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
354 }
355
e70606eb 356 of_node_put(root);
1da177e4 357 return depth;
41eab6f8
AB
358
359err:
e70606eb 360 of_node_put(root);
41eab6f8 361 return -1;
1da177e4
LT
362}
363
84c9fdd1 364static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
1da177e4
LT
365{
366 struct device_node *memory = NULL;
1da177e4
LT
367
368 memory = of_find_node_by_type(memory, "memory");
54c23310 369 if (!memory)
84c9fdd1 370 panic("numa.c: No memory nodes found!");
54c23310 371
a8bda5dd 372 *n_addr_cells = of_n_addr_cells(memory);
9213feea 373 *n_size_cells = of_n_size_cells(memory);
84c9fdd1 374 of_node_put(memory);
1da177e4
LT
375}
376
2011b1d0 377static unsigned long read_n_cells(int n, const unsigned int **buf)
1da177e4
LT
378{
379 unsigned long result = 0;
380
381 while (n--) {
382 result = (result << 32) | **buf;
383 (*buf)++;
384 }
385 return result;
386}
387
8342681d 388/*
95f72d1e 389 * Read the next memblock list entry from the ibm,dynamic-memory property
8342681d
NF
390 * and return the information in the provided of_drconf_cell structure.
391 */
392static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
393{
394 const u32 *cp;
395
396 drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
397
398 cp = *cellp;
399 drmem->drc_index = cp[0];
400 drmem->reserved = cp[1];
401 drmem->aa_index = cp[2];
402 drmem->flags = cp[3];
403
404 *cellp = cp + 4;
405}
406
407/*
25985edc 408 * Retrieve and validate the ibm,dynamic-memory property of the device tree.
8342681d 409 *
95f72d1e
YL
410 * The layout of the ibm,dynamic-memory property is a number N of memblock
411 * list entries followed by N memblock list entries. Each memblock list entry
25985edc 412 * contains information as laid out in the of_drconf_cell struct above.
8342681d
NF
413 */
414static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
415{
416 const u32 *prop;
417 u32 len, entries;
418
419 prop = of_get_property(memory, "ibm,dynamic-memory", &len);
420 if (!prop || len < sizeof(unsigned int))
421 return 0;
422
423 entries = *prop++;
424
425 /* Now that we know the number of entries, revalidate the size
426 * of the property read in to ensure we have everything
427 */
428 if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
429 return 0;
430
431 *dm = prop;
432 return entries;
433}
434
435/*
25985edc 436 * Retrieve and validate the ibm,lmb-size property for drconf memory
8342681d
NF
437 * from the device tree.
438 */
3fdfd990 439static u64 of_get_lmb_size(struct device_node *memory)
8342681d
NF
440{
441 const u32 *prop;
442 u32 len;
443
3fdfd990 444 prop = of_get_property(memory, "ibm,lmb-size", &len);
8342681d
NF
445 if (!prop || len < sizeof(unsigned int))
446 return 0;
447
448 return read_n_cells(n_mem_size_cells, &prop);
449}
450
451struct assoc_arrays {
452 u32 n_arrays;
453 u32 array_sz;
454 const u32 *arrays;
455};
456
457/*
25985edc 458 * Retrieve and validate the list of associativity arrays for drconf
8342681d
NF
459 * memory from the ibm,associativity-lookup-arrays property of the
460 * device tree..
461 *
462 * The layout of the ibm,associativity-lookup-arrays property is a number N
463 * indicating the number of associativity arrays, followed by a number M
464 * indicating the size of each associativity array, followed by a list
465 * of N associativity arrays.
466 */
467static int of_get_assoc_arrays(struct device_node *memory,
468 struct assoc_arrays *aa)
469{
470 const u32 *prop;
471 u32 len;
472
473 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
474 if (!prop || len < 2 * sizeof(unsigned int))
475 return -1;
476
477 aa->n_arrays = *prop++;
478 aa->array_sz = *prop++;
479
42b2aa86 480 /* Now that we know the number of arrays and size of each array,
8342681d
NF
481 * revalidate the size of the property read in.
482 */
483 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
484 return -1;
485
486 aa->arrays = prop;
487 return 0;
488}
489
490/*
491 * This is like of_node_to_nid_single() for memory represented in the
492 * ibm,dynamic-reconfiguration-memory node.
493 */
494static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
495 struct assoc_arrays *aa)
496{
497 int default_nid = 0;
498 int nid = default_nid;
499 int index;
500
501 if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
502 !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
503 drmem->aa_index < aa->n_arrays) {
504 index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
505 nid = aa->arrays[index];
506
507 if (nid == 0xffff || nid >= MAX_NUMNODES)
508 nid = default_nid;
509 }
510
511 return nid;
512}
513
1da177e4
LT
514/*
515 * Figure out to which domain a cpu belongs and stick it there.
516 * Return the id of the domain used.
517 */
2e5ce39d 518static int __cpuinit numa_setup_cpu(unsigned long lcpu)
1da177e4 519{
cf950b7a 520 int nid = 0;
8b16cd23 521 struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
1da177e4
LT
522
523 if (!cpu) {
524 WARN_ON(1);
525 goto out;
526 }
527
953039c8 528 nid = of_node_to_nid_single(cpu);
1da177e4 529
482ec7c4 530 if (nid < 0 || !node_online(nid))
72c33688 531 nid = first_online_node;
1da177e4 532out:
cf950b7a 533 map_cpu_to_node(lcpu, nid);
1da177e4
LT
534
535 of_node_put(cpu);
536
cf950b7a 537 return nid;
1da177e4
LT
538}
539
74b85f37 540static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
1da177e4
LT
541 unsigned long action,
542 void *hcpu)
543{
544 unsigned long lcpu = (unsigned long)hcpu;
545 int ret = NOTIFY_DONE;
546
547 switch (action) {
548 case CPU_UP_PREPARE:
8bb78442 549 case CPU_UP_PREPARE_FROZEN:
2b261227 550 numa_setup_cpu(lcpu);
1da177e4
LT
551 ret = NOTIFY_OK;
552 break;
553#ifdef CONFIG_HOTPLUG_CPU
554 case CPU_DEAD:
8bb78442 555 case CPU_DEAD_FROZEN:
1da177e4 556 case CPU_UP_CANCELED:
8bb78442 557 case CPU_UP_CANCELED_FROZEN:
1da177e4
LT
558 unmap_cpu_from_node(lcpu);
559 break;
560 ret = NOTIFY_OK;
561#endif
562 }
563 return ret;
564}
565
566/*
567 * Check and possibly modify a memory region to enforce the memory limit.
568 *
569 * Returns the size the region should have to enforce the memory limit.
570 * This will either be the original value of size, a truncated value,
571 * or zero. If the returned value of size is 0 the region should be
25985edc 572 * discarded as it lies wholly above the memory limit.
1da177e4 573 */
45fb6cea
AB
574static unsigned long __init numa_enforce_memory_limit(unsigned long start,
575 unsigned long size)
1da177e4
LT
576{
577 /*
95f72d1e 578 * We use memblock_end_of_DRAM() in here instead of memory_limit because
1da177e4 579 * we've already adjusted it for the limit and it takes care of
fe55249d
MM
580 * having memory holes below the limit. Also, in the case of
581 * iommu_is_off, memory_limit is not set but is implicitly enforced.
1da177e4 582 */
1da177e4 583
95f72d1e 584 if (start + size <= memblock_end_of_DRAM())
1da177e4
LT
585 return size;
586
95f72d1e 587 if (start >= memblock_end_of_DRAM())
1da177e4
LT
588 return 0;
589
95f72d1e 590 return memblock_end_of_DRAM() - start;
1da177e4
LT
591}
592
cf00085d
C
593/*
594 * Reads the counter for a given entry in
595 * linux,drconf-usable-memory property
596 */
597static inline int __init read_usm_ranges(const u32 **usm)
598{
599 /*
3fdfd990 600 * For each lmb in ibm,dynamic-memory a corresponding
cf00085d
C
601 * entry in linux,drconf-usable-memory property contains
602 * a counter followed by that many (base, size) duple.
603 * read the counter from linux,drconf-usable-memory
604 */
605 return read_n_cells(n_mem_size_cells, usm);
606}
607
0204568a
PM
608/*
609 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
610 * node. This assumes n_mem_{addr,size}_cells have been set.
611 */
612static void __init parse_drconf_memory(struct device_node *memory)
613{
82b2521d 614 const u32 *uninitialized_var(dm), *usm;
cf00085d 615 unsigned int n, rc, ranges, is_kexec_kdump = 0;
3fdfd990 616 unsigned long lmb_size, base, size, sz;
8342681d 617 int nid;
aa709f3b 618 struct assoc_arrays aa = { .arrays = NULL };
8342681d
NF
619
620 n = of_get_drconf_memory(memory, &dm);
621 if (!n)
0204568a
PM
622 return;
623
3fdfd990
BH
624 lmb_size = of_get_lmb_size(memory);
625 if (!lmb_size)
8342681d
NF
626 return;
627
628 rc = of_get_assoc_arrays(memory, &aa);
629 if (rc)
0204568a
PM
630 return;
631
cf00085d
C
632 /* check if this is a kexec/kdump kernel */
633 usm = of_get_usable_memory(memory);
634 if (usm != NULL)
635 is_kexec_kdump = 1;
636
0204568a 637 for (; n != 0; --n) {
8342681d
NF
638 struct of_drconf_cell drmem;
639
640 read_drconf_cell(&drmem, &dm);
641
642 /* skip this block if the reserved bit is set in flags (0x80)
643 or if the block is not assigned to this partition (0x8) */
644 if ((drmem.flags & DRCONF_MEM_RESERVED)
645 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
0204568a 646 continue;
1daa6d08 647
cf00085d 648 base = drmem.base_addr;
3fdfd990 649 size = lmb_size;
cf00085d 650 ranges = 1;
8342681d 651
cf00085d
C
652 if (is_kexec_kdump) {
653 ranges = read_usm_ranges(&usm);
654 if (!ranges) /* there are no (base, size) duple */
655 continue;
656 }
657 do {
658 if (is_kexec_kdump) {
659 base = read_n_cells(n_mem_addr_cells, &usm);
660 size = read_n_cells(n_mem_size_cells, &usm);
661 }
662 nid = of_drconf_to_nid_single(&drmem, &aa);
663 fake_numa_create_new_node(
664 ((base + size) >> PAGE_SHIFT),
8342681d 665 &nid);
cf00085d
C
666 node_set_online(nid);
667 sz = numa_enforce_memory_limit(base, size);
668 if (sz)
1d7cfe18 669 memblock_set_node(base, sz, nid);
cf00085d 670 } while (--ranges);
0204568a
PM
671 }
672}
673
1da177e4
LT
674static int __init parse_numa_properties(void)
675{
94db7c5e 676 struct device_node *memory;
482ec7c4 677 int default_nid = 0;
1da177e4
LT
678 unsigned long i;
679
680 if (numa_enabled == 0) {
681 printk(KERN_WARNING "NUMA disabled by user\n");
682 return -1;
683 }
684
1da177e4
LT
685 min_common_depth = find_min_common_depth();
686
1da177e4
LT
687 if (min_common_depth < 0)
688 return min_common_depth;
689
bf4b85b0
NL
690 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
691
1da177e4 692 /*
482ec7c4
NL
693 * Even though we connect cpus to numa domains later in SMP
694 * init, we need to know the node ids now. This is because
695 * each node to be onlined must have NODE_DATA etc backing it.
1da177e4 696 */
482ec7c4 697 for_each_present_cpu(i) {
dfbe93a2 698 struct device_node *cpu;
cf950b7a 699 int nid;
1da177e4 700
8b16cd23 701 cpu = of_get_cpu_node(i, NULL);
482ec7c4 702 BUG_ON(!cpu);
953039c8 703 nid = of_node_to_nid_single(cpu);
482ec7c4 704 of_node_put(cpu);
1da177e4 705
482ec7c4
NL
706 /*
707 * Don't fall back to default_nid yet -- we will plug
708 * cpus into nodes once the memory scan has discovered
709 * the topology.
710 */
711 if (nid < 0)
712 continue;
713 node_set_online(nid);
1da177e4
LT
714 }
715
237a0989 716 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
94db7c5e
AB
717
718 for_each_node_by_type(memory, "memory") {
1da177e4
LT
719 unsigned long start;
720 unsigned long size;
cf950b7a 721 int nid;
1da177e4 722 int ranges;
a7f67bdf 723 const unsigned int *memcell_buf;
1da177e4
LT
724 unsigned int len;
725
e2eb6392 726 memcell_buf = of_get_property(memory,
ba759485
ME
727 "linux,usable-memory", &len);
728 if (!memcell_buf || len <= 0)
e2eb6392 729 memcell_buf = of_get_property(memory, "reg", &len);
1da177e4
LT
730 if (!memcell_buf || len <= 0)
731 continue;
732
cc5d0189
BH
733 /* ranges in cell */
734 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1da177e4
LT
735new_range:
736 /* these are order-sensitive, and modify the buffer pointer */
237a0989
MK
737 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
738 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1da177e4 739
482ec7c4
NL
740 /*
741 * Assumption: either all memory nodes or none will
742 * have associativity properties. If none, then
743 * everything goes to default_nid.
744 */
953039c8 745 nid = of_node_to_nid_single(memory);
482ec7c4
NL
746 if (nid < 0)
747 nid = default_nid;
1daa6d08
BS
748
749 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
482ec7c4 750 node_set_online(nid);
1da177e4 751
45fb6cea 752 if (!(size = numa_enforce_memory_limit(start, size))) {
1da177e4
LT
753 if (--ranges)
754 goto new_range;
755 else
756 continue;
757 }
758
1d7cfe18 759 memblock_set_node(start, size, nid);
1da177e4
LT
760
761 if (--ranges)
762 goto new_range;
763 }
764
0204568a 765 /*
dfbe93a2
AB
766 * Now do the same thing for each MEMBLOCK listed in the
767 * ibm,dynamic-memory property in the
768 * ibm,dynamic-reconfiguration-memory node.
0204568a
PM
769 */
770 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
771 if (memory)
772 parse_drconf_memory(memory);
773
1da177e4
LT
774 return 0;
775}
776
777static void __init setup_nonnuma(void)
778{
95f72d1e
YL
779 unsigned long top_of_ram = memblock_end_of_DRAM();
780 unsigned long total_ram = memblock_phys_mem_size();
c67c3cb4 781 unsigned long start_pfn, end_pfn;
28be7072
BH
782 unsigned int nid = 0;
783 struct memblock_region *reg;
1da177e4 784
e110b281 785 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1da177e4 786 top_of_ram, total_ram);
e110b281 787 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
1da177e4
LT
788 (top_of_ram - total_ram) >> 20);
789
28be7072 790 for_each_memblock(memory, reg) {
c7fc2de0
YL
791 start_pfn = memblock_region_memory_base_pfn(reg);
792 end_pfn = memblock_region_memory_end_pfn(reg);
1daa6d08
BS
793
794 fake_numa_create_new_node(end_pfn, &nid);
1d7cfe18
TH
795 memblock_set_node(PFN_PHYS(start_pfn),
796 PFN_PHYS(end_pfn - start_pfn), nid);
1daa6d08 797 node_set_online(nid);
c67c3cb4 798 }
1da177e4
LT
799}
800
4b703a23
AB
801void __init dump_numa_cpu_topology(void)
802{
803 unsigned int node;
804 unsigned int cpu, count;
805
806 if (min_common_depth == -1 || !numa_enabled)
807 return;
808
809 for_each_online_node(node) {
e110b281 810 printk(KERN_DEBUG "Node %d CPUs:", node);
4b703a23
AB
811
812 count = 0;
813 /*
814 * If we used a CPU iterator here we would miss printing
815 * the holes in the cpumap.
816 */
25863de0
AB
817 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
818 if (cpumask_test_cpu(cpu,
819 node_to_cpumask_map[node])) {
4b703a23
AB
820 if (count == 0)
821 printk(" %u", cpu);
822 ++count;
823 } else {
824 if (count > 1)
825 printk("-%u", cpu - 1);
826 count = 0;
827 }
828 }
829
830 if (count > 1)
25863de0 831 printk("-%u", nr_cpu_ids - 1);
4b703a23
AB
832 printk("\n");
833 }
834}
835
836static void __init dump_numa_memory_topology(void)
1da177e4
LT
837{
838 unsigned int node;
839 unsigned int count;
840
841 if (min_common_depth == -1 || !numa_enabled)
842 return;
843
844 for_each_online_node(node) {
845 unsigned long i;
846
e110b281 847 printk(KERN_DEBUG "Node %d Memory:", node);
1da177e4
LT
848
849 count = 0;
850
95f72d1e 851 for (i = 0; i < memblock_end_of_DRAM();
45fb6cea
AB
852 i += (1 << SECTION_SIZE_BITS)) {
853 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
1da177e4
LT
854 if (count == 0)
855 printk(" 0x%lx", i);
856 ++count;
857 } else {
858 if (count > 0)
859 printk("-0x%lx", i);
860 count = 0;
861 }
862 }
863
864 if (count > 0)
865 printk("-0x%lx", i);
866 printk("\n");
867 }
1da177e4
LT
868}
869
870/*
95f72d1e 871 * Allocate some memory, satisfying the memblock or bootmem allocator where
1da177e4
LT
872 * required. nid is the preferred node and end is the physical address of
873 * the highest address in the node.
874 *
0be210fd 875 * Returns the virtual address of the memory.
1da177e4 876 */
893473df 877static void __init *careful_zallocation(int nid, unsigned long size,
45fb6cea
AB
878 unsigned long align,
879 unsigned long end_pfn)
1da177e4 880{
0be210fd 881 void *ret;
45fb6cea 882 int new_nid;
0be210fd
DH
883 unsigned long ret_paddr;
884
95f72d1e 885 ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
1da177e4
LT
886
887 /* retry over all memory */
0be210fd 888 if (!ret_paddr)
95f72d1e 889 ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
1da177e4 890
0be210fd 891 if (!ret_paddr)
5d21ea2b 892 panic("numa.c: cannot allocate %lu bytes for node %d",
1da177e4
LT
893 size, nid);
894
0be210fd
DH
895 ret = __va(ret_paddr);
896
1da177e4 897 /*
c555e520 898 * We initialize the nodes in numeric order: 0, 1, 2...
95f72d1e 899 * and hand over control from the MEMBLOCK allocator to the
c555e520
DH
900 * bootmem allocator. If this function is called for
901 * node 5, then we know that all nodes <5 are using the
95f72d1e 902 * bootmem allocator instead of the MEMBLOCK allocator.
c555e520
DH
903 *
904 * So, check the nid from which this allocation came
905 * and double check to see if we need to use bootmem
95f72d1e 906 * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
c555e520 907 * since it would be useless.
1da177e4 908 */
0be210fd 909 new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
45fb6cea 910 if (new_nid < nid) {
0be210fd 911 ret = __alloc_bootmem_node(NODE_DATA(new_nid),
1da177e4
LT
912 size, align, 0);
913
0be210fd 914 dbg("alloc_bootmem %p %lx\n", ret, size);
1da177e4
LT
915 }
916
893473df 917 memset(ret, 0, size);
0be210fd 918 return ret;
1da177e4
LT
919}
920
74b85f37
CS
921static struct notifier_block __cpuinitdata ppc64_numa_nb = {
922 .notifier_call = cpu_numa_callback,
923 .priority = 1 /* Must run before sched domains notifier. */
924};
925
28e86bdb 926static void __init mark_reserved_regions_for_nid(int nid)
4a618669
DH
927{
928 struct pglist_data *node = NODE_DATA(nid);
28be7072 929 struct memblock_region *reg;
4a618669 930
28be7072
BH
931 for_each_memblock(reserved, reg) {
932 unsigned long physbase = reg->base;
933 unsigned long size = reg->size;
4a618669 934 unsigned long start_pfn = physbase >> PAGE_SHIFT;
06eccea6 935 unsigned long end_pfn = PFN_UP(physbase + size);
4a618669
DH
936 struct node_active_region node_ar;
937 unsigned long node_end_pfn = node->node_start_pfn +
938 node->node_spanned_pages;
939
940 /*
95f72d1e 941 * Check to make sure that this memblock.reserved area is
4a618669
DH
942 * within the bounds of the node that we care about.
943 * Checking the nid of the start and end points is not
944 * sufficient because the reserved area could span the
945 * entire node.
946 */
947 if (end_pfn <= node->node_start_pfn ||
948 start_pfn >= node_end_pfn)
949 continue;
950
951 get_node_active_region(start_pfn, &node_ar);
952 while (start_pfn < end_pfn &&
953 node_ar.start_pfn < node_ar.end_pfn) {
954 unsigned long reserve_size = size;
955 /*
956 * if reserved region extends past active region
957 * then trim size to active region
958 */
959 if (end_pfn > node_ar.end_pfn)
960 reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
06eccea6 961 - physbase;
a4c74ddd
DH
962 /*
963 * Only worry about *this* node, others may not
964 * yet have valid NODE_DATA().
965 */
966 if (node_ar.nid == nid) {
967 dbg("reserve_bootmem %lx %lx nid=%d\n",
968 physbase, reserve_size, node_ar.nid);
969 reserve_bootmem_node(NODE_DATA(node_ar.nid),
970 physbase, reserve_size,
971 BOOTMEM_DEFAULT);
972 }
4a618669
DH
973 /*
974 * if reserved region is contained in the active region
975 * then done.
976 */
977 if (end_pfn <= node_ar.end_pfn)
978 break;
979
980 /*
981 * reserved region extends past the active region
982 * get next active region that contains this
983 * reserved region
984 */
985 start_pfn = node_ar.end_pfn;
986 physbase = start_pfn << PAGE_SHIFT;
987 size = size - reserve_size;
988 get_node_active_region(start_pfn, &node_ar);
989 }
990 }
991}
992
993
1da177e4
LT
994void __init do_init_bootmem(void)
995{
996 int nid;
1da177e4
LT
997
998 min_low_pfn = 0;
95f72d1e 999 max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1da177e4
LT
1000 max_pfn = max_low_pfn;
1001
1002 if (parse_numa_properties())
1003 setup_nonnuma();
1004 else
4b703a23 1005 dump_numa_memory_topology();
1da177e4 1006
1da177e4 1007 for_each_online_node(nid) {
c67c3cb4 1008 unsigned long start_pfn, end_pfn;
0be210fd 1009 void *bootmem_vaddr;
1da177e4
LT
1010 unsigned long bootmap_pages;
1011
c67c3cb4 1012 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1da177e4 1013
4a618669
DH
1014 /*
1015 * Allocate the node structure node local if possible
1016 *
1017 * Be careful moving this around, as it relies on all
1018 * previous nodes' bootmem to be initialized and have
1019 * all reserved areas marked.
1020 */
893473df 1021 NODE_DATA(nid) = careful_zallocation(nid,
1da177e4 1022 sizeof(struct pglist_data),
45fb6cea 1023 SMP_CACHE_BYTES, end_pfn);
1da177e4
LT
1024
1025 dbg("node %d\n", nid);
1026 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
1027
b61bfa3c 1028 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
45fb6cea
AB
1029 NODE_DATA(nid)->node_start_pfn = start_pfn;
1030 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
1da177e4
LT
1031
1032 if (NODE_DATA(nid)->node_spanned_pages == 0)
1033 continue;
1034
45fb6cea
AB
1035 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
1036 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
1da177e4 1037
45fb6cea 1038 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
893473df 1039 bootmem_vaddr = careful_zallocation(nid,
45fb6cea
AB
1040 bootmap_pages << PAGE_SHIFT,
1041 PAGE_SIZE, end_pfn);
1da177e4 1042
0be210fd 1043 dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
1da177e4 1044
0be210fd
DH
1045 init_bootmem_node(NODE_DATA(nid),
1046 __pa(bootmem_vaddr) >> PAGE_SHIFT,
45fb6cea 1047 start_pfn, end_pfn);
1da177e4 1048
c67c3cb4 1049 free_bootmem_with_active_regions(nid, end_pfn);
4a618669
DH
1050 /*
1051 * Be very careful about moving this around. Future
893473df 1052 * calls to careful_zallocation() depend on this getting
4a618669
DH
1053 * done correctly.
1054 */
1055 mark_reserved_regions_for_nid(nid);
8f64e1f2 1056 sparse_memory_present_with_active_regions(nid);
4a618669 1057 }
d3f6204a
BH
1058
1059 init_bootmem_done = 1;
25863de0
AB
1060
1061 /*
1062 * Now bootmem is initialised we can create the node to cpumask
1063 * lookup tables and setup the cpu callback to populate them.
1064 */
1065 setup_node_to_cpumask_map();
1066
1067 register_cpu_notifier(&ppc64_numa_nb);
1068 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
1069 (void *)(unsigned long)boot_cpuid);
1da177e4
LT
1070}
1071
1072void __init paging_init(void)
1073{
6391af17
MG
1074 unsigned long max_zone_pfns[MAX_NR_ZONES];
1075 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
95f72d1e 1076 max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
c67c3cb4 1077 free_area_init_nodes(max_zone_pfns);
1da177e4
LT
1078}
1079
1080static int __init early_numa(char *p)
1081{
1082 if (!p)
1083 return 0;
1084
1085 if (strstr(p, "off"))
1086 numa_enabled = 0;
1087
1088 if (strstr(p, "debug"))
1089 numa_debug = 1;
1090
1daa6d08
BS
1091 p = strstr(p, "fake=");
1092 if (p)
1093 cmdline = p + strlen("fake=");
1094
1da177e4
LT
1095 return 0;
1096}
1097early_param("numa", early_numa);
237a0989
MK
1098
1099#ifdef CONFIG_MEMORY_HOTPLUG
0db9360a 1100/*
0f16ef7f
NF
1101 * Find the node associated with a hot added memory section for
1102 * memory represented in the device tree by the property
1103 * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
0db9360a
NF
1104 */
1105static int hot_add_drconf_scn_to_nid(struct device_node *memory,
1106 unsigned long scn_addr)
1107{
1108 const u32 *dm;
0f16ef7f 1109 unsigned int drconf_cell_cnt, rc;
3fdfd990 1110 unsigned long lmb_size;
0db9360a 1111 struct assoc_arrays aa;
0f16ef7f 1112 int nid = -1;
0db9360a 1113
0f16ef7f
NF
1114 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1115 if (!drconf_cell_cnt)
1116 return -1;
0db9360a 1117
3fdfd990
BH
1118 lmb_size = of_get_lmb_size(memory);
1119 if (!lmb_size)
0f16ef7f 1120 return -1;
0db9360a
NF
1121
1122 rc = of_get_assoc_arrays(memory, &aa);
1123 if (rc)
0f16ef7f 1124 return -1;
0db9360a 1125
0f16ef7f 1126 for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
0db9360a
NF
1127 struct of_drconf_cell drmem;
1128
1129 read_drconf_cell(&drmem, &dm);
1130
1131 /* skip this block if it is reserved or not assigned to
1132 * this partition */
1133 if ((drmem.flags & DRCONF_MEM_RESERVED)
1134 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
1135 continue;
1136
0f16ef7f 1137 if ((scn_addr < drmem.base_addr)
3fdfd990 1138 || (scn_addr >= (drmem.base_addr + lmb_size)))
0f16ef7f
NF
1139 continue;
1140
0db9360a 1141 nid = of_drconf_to_nid_single(&drmem, &aa);
0f16ef7f
NF
1142 break;
1143 }
1144
1145 return nid;
1146}
1147
1148/*
1149 * Find the node associated with a hot added memory section for memory
1150 * represented in the device tree as a node (i.e. memory@XXXX) for
95f72d1e 1151 * each memblock.
0f16ef7f
NF
1152 */
1153int hot_add_node_scn_to_nid(unsigned long scn_addr)
1154{
94db7c5e 1155 struct device_node *memory;
0f16ef7f
NF
1156 int nid = -1;
1157
94db7c5e 1158 for_each_node_by_type(memory, "memory") {
0f16ef7f
NF
1159 unsigned long start, size;
1160 int ranges;
1161 const unsigned int *memcell_buf;
1162 unsigned int len;
1163
1164 memcell_buf = of_get_property(memory, "reg", &len);
1165 if (!memcell_buf || len <= 0)
1166 continue;
1167
1168 /* ranges in cell */
1169 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1170
1171 while (ranges--) {
1172 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1173 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1174
1175 if ((scn_addr < start) || (scn_addr >= (start + size)))
1176 continue;
1177
1178 nid = of_node_to_nid_single(memory);
1179 break;
1180 }
0db9360a 1181
0f16ef7f
NF
1182 if (nid >= 0)
1183 break;
0db9360a
NF
1184 }
1185
60831842
AB
1186 of_node_put(memory);
1187
0f16ef7f 1188 return nid;
0db9360a
NF
1189}
1190
237a0989
MK
1191/*
1192 * Find the node associated with a hot added memory section. Section
95f72d1e
YL
1193 * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
1194 * sections are fully contained within a single MEMBLOCK.
237a0989
MK
1195 */
1196int hot_add_scn_to_nid(unsigned long scn_addr)
1197{
1198 struct device_node *memory = NULL;
0f16ef7f 1199 int nid, found = 0;
237a0989
MK
1200
1201 if (!numa_enabled || (min_common_depth < 0))
72c33688 1202 return first_online_node;
0db9360a
NF
1203
1204 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1205 if (memory) {
1206 nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
1207 of_node_put(memory);
0f16ef7f
NF
1208 } else {
1209 nid = hot_add_node_scn_to_nid(scn_addr);
0db9360a 1210 }
237a0989 1211
0f16ef7f 1212 if (nid < 0 || !node_online(nid))
72c33688 1213 nid = first_online_node;
237a0989 1214
0f16ef7f
NF
1215 if (NODE_DATA(nid)->node_spanned_pages)
1216 return nid;
237a0989 1217
0f16ef7f
NF
1218 for_each_online_node(nid) {
1219 if (NODE_DATA(nid)->node_spanned_pages) {
1220 found = 1;
1221 break;
237a0989 1222 }
237a0989 1223 }
0f16ef7f
NF
1224
1225 BUG_ON(!found);
1226 return nid;
237a0989 1227}
0f16ef7f 1228
cd34206e
NA
1229static u64 hot_add_drconf_memory_max(void)
1230{
1231 struct device_node *memory = NULL;
1232 unsigned int drconf_cell_cnt = 0;
1233 u64 lmb_size = 0;
1234 const u32 *dm = 0;
1235
1236 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1237 if (memory) {
1238 drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
1239 lmb_size = of_get_lmb_size(memory);
1240 of_node_put(memory);
1241 }
1242 return lmb_size * drconf_cell_cnt;
1243}
1244
1245/*
1246 * memory_hotplug_max - return max address of memory that may be added
1247 *
1248 * This is currently only used on systems that support drconfig memory
1249 * hotplug.
1250 */
1251u64 memory_hotplug_max(void)
1252{
1253 return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
1254}
237a0989 1255#endif /* CONFIG_MEMORY_HOTPLUG */
9eff1a38 1256
bd03403a 1257/* Virtual Processor Home Node (VPHN) support */
39bf990e 1258#ifdef CONFIG_PPC_SPLPAR
30c05350
NF
1259struct topology_update_data {
1260 struct topology_update_data *next;
1261 unsigned int cpu;
1262 int old_nid;
1263 int new_nid;
1264};
1265
5de16699 1266static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
9eff1a38
JL
1267static cpumask_t cpu_associativity_changes_mask;
1268static int vphn_enabled;
5d88aa85
JL
1269static int prrn_enabled;
1270static void reset_topology_timer(void);
9eff1a38
JL
1271
1272/*
1273 * Store the current values of the associativity change counters in the
1274 * hypervisor.
1275 */
1276static void setup_cpu_associativity_change_counters(void)
1277{
cd9d6cc7 1278 int cpu;
9eff1a38 1279
5de16699
AB
1280 /* The VPHN feature supports a maximum of 8 reference points */
1281 BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
1282
9eff1a38 1283 for_each_possible_cpu(cpu) {
cd9d6cc7 1284 int i;
9eff1a38
JL
1285 u8 *counts = vphn_cpu_change_counts[cpu];
1286 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1287
5de16699 1288 for (i = 0; i < distance_ref_points_depth; i++)
9eff1a38 1289 counts[i] = hypervisor_counts[i];
9eff1a38
JL
1290 }
1291}
1292
1293/*
1294 * The hypervisor maintains a set of 8 associativity change counters in
1295 * the VPA of each cpu that correspond to the associativity levels in the
1296 * ibm,associativity-reference-points property. When an associativity
1297 * level changes, the corresponding counter is incremented.
1298 *
1299 * Set a bit in cpu_associativity_changes_mask for each cpu whose home
1300 * node associativity levels have changed.
1301 *
1302 * Returns the number of cpus with unhandled associativity changes.
1303 */
1304static int update_cpu_associativity_changes_mask(void)
1305{
5d88aa85 1306 int cpu;
9eff1a38
JL
1307 cpumask_t *changes = &cpu_associativity_changes_mask;
1308
9eff1a38
JL
1309 for_each_possible_cpu(cpu) {
1310 int i, changed = 0;
1311 u8 *counts = vphn_cpu_change_counts[cpu];
1312 volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
1313
5de16699 1314 for (i = 0; i < distance_ref_points_depth; i++) {
d69043e8 1315 if (hypervisor_counts[i] != counts[i]) {
9eff1a38
JL
1316 counts[i] = hypervisor_counts[i];
1317 changed = 1;
1318 }
1319 }
1320 if (changed) {
1321 cpumask_set_cpu(cpu, changes);
9eff1a38
JL
1322 }
1323 }
1324
5d88aa85 1325 return cpumask_weight(changes);
9eff1a38
JL
1326}
1327
c0e5e46f
AB
1328/*
1329 * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
1330 * the complete property we have to add the length in the first cell.
1331 */
1332#define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
9eff1a38
JL
1333
1334/*
1335 * Convert the associativity domain numbers returned from the hypervisor
1336 * to the sequence they would appear in the ibm,associativity property.
1337 */
1338static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
1339{
cd9d6cc7 1340 int i, nr_assoc_doms = 0;
9eff1a38
JL
1341 const u16 *field = (const u16*) packed;
1342
1343#define VPHN_FIELD_UNUSED (0xffff)
1344#define VPHN_FIELD_MSB (0x8000)
1345#define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
1346
c0e5e46f 1347 for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
9eff1a38
JL
1348 if (*field == VPHN_FIELD_UNUSED) {
1349 /* All significant fields processed, and remaining
1350 * fields contain the reserved value of all 1's.
1351 * Just store them.
1352 */
1353 unpacked[i] = *((u32*)field);
1354 field += 2;
7639adaa 1355 } else if (*field & VPHN_FIELD_MSB) {
9eff1a38
JL
1356 /* Data is in the lower 15 bits of this field */
1357 unpacked[i] = *field & VPHN_FIELD_MASK;
1358 field++;
1359 nr_assoc_doms++;
7639adaa 1360 } else {
9eff1a38
JL
1361 /* Data is in the lower 15 bits of this field
1362 * concatenated with the next 16 bit field
1363 */
1364 unpacked[i] = *((u32*)field);
1365 field += 2;
1366 nr_assoc_doms++;
1367 }
1368 }
1369
c0e5e46f
AB
1370 /* The first cell contains the length of the property */
1371 unpacked[0] = nr_assoc_doms;
1372
9eff1a38
JL
1373 return nr_assoc_doms;
1374}
1375
1376/*
1377 * Retrieve the new associativity information for a virtual processor's
1378 * home node.
1379 */
1380static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
1381{
cd9d6cc7 1382 long rc;
9eff1a38
JL
1383 long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1384 u64 flags = 1;
1385 int hwcpu = get_hard_smp_processor_id(cpu);
1386
1387 rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
1388 vphn_unpack_associativity(retbuf, associativity);
1389
1390 return rc;
1391}
1392
1393static long vphn_get_associativity(unsigned long cpu,
1394 unsigned int *associativity)
1395{
cd9d6cc7 1396 long rc;
9eff1a38
JL
1397
1398 rc = hcall_vphn(cpu, associativity);
1399
1400 switch (rc) {
1401 case H_FUNCTION:
1402 printk(KERN_INFO
1403 "VPHN is not supported. Disabling polling...\n");
1404 stop_topology_update();
1405 break;
1406 case H_HARDWARE:
1407 printk(KERN_ERR
1408 "hcall_vphn() experienced a hardware fault "
1409 "preventing VPHN. Disabling polling...\n");
1410 stop_topology_update();
1411 }
1412
1413 return rc;
1414}
1415
30c05350
NF
1416/*
1417 * Update the CPU maps and sysfs entries for a single CPU when its NUMA
1418 * characteristics change. This function doesn't perform any locking and is
1419 * only safe to call from stop_machine().
1420 */
1421static int update_cpu_topology(void *data)
1422{
1423 struct topology_update_data *update;
1424 unsigned long cpu;
1425
1426 if (!data)
1427 return -EINVAL;
1428
1429 cpu = get_cpu();
1430
1431 for (update = data; update; update = update->next) {
1432 if (cpu != update->cpu)
1433 continue;
1434
1435 unregister_cpu_under_node(update->cpu, update->old_nid);
1436 unmap_cpu_from_node(update->cpu);
1437 map_cpu_to_node(update->cpu, update->new_nid);
176bbf14 1438 vdso_getcpu_init();
30c05350
NF
1439 register_cpu_under_node(update->cpu, update->new_nid);
1440 }
1441
1442 return 0;
1443}
1444
9eff1a38
JL
1445/*
1446 * Update the node maps and sysfs entries for each cpu whose home node
79c5fceb 1447 * has changed. Returns 1 when the topology has changed, and 0 otherwise.
9eff1a38
JL
1448 */
1449int arch_update_cpu_topology(void)
1450{
30c05350
NF
1451 unsigned int cpu, changed = 0;
1452 struct topology_update_data *updates, *ud;
9eff1a38 1453 unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
176bbf14 1454 cpumask_t updated_cpus;
8a25a2fd 1455 struct device *dev;
30c05350
NF
1456 int weight, i = 0;
1457
1458 weight = cpumask_weight(&cpu_associativity_changes_mask);
1459 if (!weight)
1460 return 0;
1461
1462 updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
1463 if (!updates)
1464 return 0;
9eff1a38 1465
176bbf14
JL
1466 cpumask_clear(&updated_cpus);
1467
5d88aa85 1468 for_each_cpu(cpu, &cpu_associativity_changes_mask) {
30c05350
NF
1469 ud = &updates[i++];
1470 ud->cpu = cpu;
9eff1a38 1471 vphn_get_associativity(cpu, associativity);
30c05350 1472 ud->new_nid = associativity_to_nid(associativity);
9eff1a38 1473
30c05350
NF
1474 if (ud->new_nid < 0 || !node_online(ud->new_nid))
1475 ud->new_nid = first_online_node;
9eff1a38 1476
30c05350 1477 ud->old_nid = numa_cpu_lookup_table[cpu];
176bbf14 1478 cpumask_set_cpu(cpu, &updated_cpus);
9eff1a38 1479
30c05350
NF
1480 if (i < weight)
1481 ud->next = &updates[i];
1482 }
1483
176bbf14 1484 stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
30c05350
NF
1485
1486 for (ud = &updates[0]; ud; ud = ud->next) {
1487 dev = get_cpu_device(ud->cpu);
8a25a2fd
KS
1488 if (dev)
1489 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
30c05350 1490 cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
79c5fceb 1491 changed = 1;
9eff1a38
JL
1492 }
1493
30c05350 1494 kfree(updates);
79c5fceb 1495 return changed;
9eff1a38
JL
1496}
1497
1498static void topology_work_fn(struct work_struct *work)
1499{
1500 rebuild_sched_domains();
1501}
1502static DECLARE_WORK(topology_work, topology_work_fn);
1503
1504void topology_schedule_update(void)
1505{
1506 schedule_work(&topology_work);
1507}
1508
1509static void topology_timer_fn(unsigned long ignored)
1510{
5d88aa85 1511 if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
9eff1a38 1512 topology_schedule_update();
5d88aa85
JL
1513 else if (vphn_enabled) {
1514 if (update_cpu_associativity_changes_mask() > 0)
1515 topology_schedule_update();
1516 reset_topology_timer();
1517 }
9eff1a38
JL
1518}
1519static struct timer_list topology_timer =
1520 TIMER_INITIALIZER(topology_timer_fn, 0, 0);
1521
5d88aa85 1522static void reset_topology_timer(void)
9eff1a38
JL
1523{
1524 topology_timer.data = 0;
1525 topology_timer.expires = jiffies + 60 * HZ;
5d88aa85
JL
1526 mod_timer(&topology_timer, topology_timer.expires);
1527}
1528
1529static void stage_topology_update(int core_id)
1530{
1531 cpumask_or(&cpu_associativity_changes_mask,
1532 &cpu_associativity_changes_mask, cpu_sibling_mask(core_id));
1533 reset_topology_timer();
1534}
1535
1536static int dt_update_callback(struct notifier_block *nb,
1537 unsigned long action, void *data)
1538{
1539 struct of_prop_reconfig *update;
1540 int rc = NOTIFY_DONE;
1541
1542 switch (action) {
5d88aa85
JL
1543 case OF_RECONFIG_UPDATE_PROPERTY:
1544 update = (struct of_prop_reconfig *)data;
30c05350
NF
1545 if (!of_prop_cmp(update->dn->type, "cpu") &&
1546 !of_prop_cmp(update->prop->name, "ibm,associativity")) {
5d88aa85
JL
1547 u32 core_id;
1548 of_property_read_u32(update->dn, "reg", &core_id);
1549 stage_topology_update(core_id);
1550 rc = NOTIFY_OK;
1551 }
1552 break;
1553 }
1554
1555 return rc;
9eff1a38
JL
1556}
1557
5d88aa85
JL
1558static struct notifier_block dt_update_nb = {
1559 .notifier_call = dt_update_callback,
1560};
1561
9eff1a38 1562/*
5d88aa85 1563 * Start polling for associativity changes.
9eff1a38
JL
1564 */
1565int start_topology_update(void)
1566{
1567 int rc = 0;
1568
5d88aa85
JL
1569 if (firmware_has_feature(FW_FEATURE_PRRN)) {
1570 if (!prrn_enabled) {
1571 prrn_enabled = 1;
1572 vphn_enabled = 0;
1573 rc = of_reconfig_notifier_register(&dt_update_nb);
1574 }
1575 } else if (0 && firmware_has_feature(FW_FEATURE_VPHN) &&
1576 get_lppaca()->shared_proc) {
1577 /* Disabled until races with load balancing are fixed */
1578 if (!vphn_enabled) {
1579 prrn_enabled = 0;
1580 vphn_enabled = 1;
1581 setup_cpu_associativity_change_counters();
1582 init_timer_deferrable(&topology_timer);
1583 reset_topology_timer();
1584 }
9eff1a38
JL
1585 }
1586
1587 return rc;
1588}
1589__initcall(start_topology_update);
1590
1591/*
1592 * Disable polling for VPHN associativity changes.
1593 */
1594int stop_topology_update(void)
1595{
5d88aa85
JL
1596 int rc = 0;
1597
1598 if (prrn_enabled) {
1599 prrn_enabled = 0;
1600 rc = of_reconfig_notifier_unregister(&dt_update_nb);
1601 } else if (vphn_enabled) {
1602 vphn_enabled = 0;
1603 rc = del_timer_sync(&topology_timer);
1604 }
1605
1606 return rc;
9eff1a38 1607}
39bf990e 1608#endif /* CONFIG_PPC_SPLPAR */