]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/platforms/pseries/dlpar.c
selftests/powerpc: Fix the pmu install rule
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / platforms / pseries / dlpar.c
CommitLineData
ab519a01
NF
1/*
2 * Support for dynamic reconfiguration for PCI, Memory, and CPU
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms.
4 *
5 * Copyright (C) 2009 Nathan Fontenot
6 * Copyright (C) 2009 IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 */
12
999e2dad
NF
13#define pr_fmt(fmt) "dlpar: " fmt
14
ab519a01 15#include <linux/kernel.h>
ab519a01 16#include <linux/notifier.h>
ab519a01
NF
17#include <linux/spinlock.h>
18#include <linux/cpu.h>
5a0e3ad6 19#include <linux/slab.h>
1cf3d8b3 20#include <linux/of.h>
b6db63d1 21#include "offline_states.h"
1217d34b 22#include "pseries.h"
ab519a01
NF
23
24#include <asm/prom.h>
25#include <asm/machdep.h>
26#include <asm/uaccess.h>
27#include <asm/rtas.h>
ab519a01
NF
28
29struct cc_workarea {
d6f1e7ab
BR
30 __be32 drc_index;
31 __be32 zero;
32 __be32 name_offset;
33 __be32 prop_length;
34 __be32 prop_offset;
ab519a01
NF
35};
36
20648974 37void dlpar_free_cc_property(struct property *prop)
ab519a01
NF
38{
39 kfree(prop->name);
40 kfree(prop->value);
41 kfree(prop);
42}
43
44static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
45{
46 struct property *prop;
47 char *name;
48 char *value;
49
50 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
51 if (!prop)
52 return NULL;
53
d6f1e7ab 54 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
ab519a01
NF
55 prop->name = kstrdup(name, GFP_KERNEL);
56
d6f1e7ab
BR
57 prop->length = be32_to_cpu(ccwa->prop_length);
58 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
e72ed6b5 59 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
ab519a01
NF
60 if (!prop->value) {
61 dlpar_free_cc_property(prop);
62 return NULL;
63 }
64
ab519a01
NF
65 return prop;
66}
67
8d5ff320
TD
68static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
69 const char *path)
ab519a01
NF
70{
71 struct device_node *dn;
72 char *name;
73
8d5ff320
TD
74 /* If parent node path is "/" advance path to NULL terminator to
75 * prevent double leading slashs in full_name.
76 */
77 if (!path[1])
78 path++;
79
ab519a01
NF
80 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
81 if (!dn)
82 return NULL;
83
d6f1e7ab 84 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
8d5ff320 85 dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
ab519a01
NF
86 if (!dn->full_name) {
87 kfree(dn);
88 return NULL;
89 }
90
1578cb76 91 of_node_set_flag(dn, OF_DYNAMIC);
97a9a717 92 of_node_init(dn);
1578cb76 93
ab519a01
NF
94 return dn;
95}
96
97static void dlpar_free_one_cc_node(struct device_node *dn)
98{
99 struct property *prop;
100
101 while (dn->properties) {
102 prop = dn->properties;
103 dn->properties = prop->next;
104 dlpar_free_cc_property(prop);
105 }
106
107 kfree(dn->full_name);
108 kfree(dn);
109}
110
20648974 111void dlpar_free_cc_nodes(struct device_node *dn)
ab519a01
NF
112{
113 if (dn->child)
114 dlpar_free_cc_nodes(dn->child);
115
116 if (dn->sibling)
117 dlpar_free_cc_nodes(dn->sibling);
118
119 dlpar_free_one_cc_node(dn);
120}
121
9c740025 122#define COMPLETE 0
ab519a01
NF
123#define NEXT_SIBLING 1
124#define NEXT_CHILD 2
125#define NEXT_PROPERTY 3
126#define PREV_PARENT 4
127#define MORE_MEMORY 5
128#define CALL_AGAIN -2
129#define ERR_CFG_USE -9003
130
d6f1e7ab 131struct device_node *dlpar_configure_connector(__be32 drc_index,
8d5ff320 132 struct device_node *parent)
ab519a01
NF
133{
134 struct device_node *dn;
135 struct device_node *first_dn = NULL;
136 struct device_node *last_dn = NULL;
137 struct property *property;
138 struct property *last_property = NULL;
139 struct cc_workarea *ccwa;
93f68f1e 140 char *data_buf;
8d5ff320 141 const char *parent_path = parent->full_name;
ab519a01 142 int cc_token;
93f68f1e 143 int rc = -1;
ab519a01
NF
144
145 cc_token = rtas_token("ibm,configure-connector");
146 if (cc_token == RTAS_UNKNOWN_SERVICE)
147 return NULL;
148
93f68f1e
NF
149 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
150 if (!data_buf)
151 return NULL;
152
153 ccwa = (struct cc_workarea *)&data_buf[0];
ab519a01
NF
154 ccwa->drc_index = drc_index;
155 ccwa->zero = 0;
156
93f68f1e
NF
157 do {
158 /* Since we release the rtas_data_buf lock between configure
159 * connector calls we want to re-populate the rtas_data_buffer
160 * with the contents of the previous call.
161 */
162 spin_lock(&rtas_data_buf_lock);
163
164 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
165 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
166 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
167
168 spin_unlock(&rtas_data_buf_lock);
169
ab519a01 170 switch (rc) {
9c740025
AB
171 case COMPLETE:
172 break;
173
ab519a01 174 case NEXT_SIBLING:
8d5ff320 175 dn = dlpar_parse_cc_node(ccwa, parent_path);
ab519a01
NF
176 if (!dn)
177 goto cc_error;
178
179 dn->parent = last_dn->parent;
180 last_dn->sibling = dn;
181 last_dn = dn;
182 break;
183
184 case NEXT_CHILD:
8d5ff320
TD
185 if (first_dn)
186 parent_path = last_dn->full_name;
187
188 dn = dlpar_parse_cc_node(ccwa, parent_path);
ab519a01
NF
189 if (!dn)
190 goto cc_error;
191
8d5ff320
TD
192 if (!first_dn) {
193 dn->parent = parent;
ab519a01 194 first_dn = dn;
8d5ff320 195 } else {
ab519a01
NF
196 dn->parent = last_dn;
197 if (last_dn)
198 last_dn->child = dn;
199 }
200
201 last_dn = dn;
202 break;
203
204 case NEXT_PROPERTY:
205 property = dlpar_parse_cc_property(ccwa);
206 if (!property)
207 goto cc_error;
208
209 if (!last_dn->properties)
210 last_dn->properties = property;
211 else
212 last_property->next = property;
213
214 last_property = property;
215 break;
216
217 case PREV_PARENT:
218 last_dn = last_dn->parent;
8d5ff320 219 parent_path = last_dn->parent->full_name;
ab519a01
NF
220 break;
221
222 case CALL_AGAIN:
223 break;
224
225 case MORE_MEMORY:
226 case ERR_CFG_USE:
227 default:
228 printk(KERN_ERR "Unexpected Error (%d) "
229 "returned from configure-connector\n", rc);
230 goto cc_error;
231 }
93f68f1e 232 } while (rc);
ab519a01 233
93f68f1e
NF
234cc_error:
235 kfree(data_buf);
236
237 if (rc) {
238 if (first_dn)
239 dlpar_free_cc_nodes(first_dn);
240
241 return NULL;
ab519a01
NF
242 }
243
ab519a01 244 return first_dn;
ab519a01
NF
245}
246
247static struct device_node *derive_parent(const char *path)
248{
249 struct device_node *parent;
250 char *last_slash;
251
252 last_slash = strrchr(path, '/');
253 if (last_slash == path) {
254 parent = of_find_node_by_path("/");
255 } else {
256 char *parent_path;
257 int parent_path_len = last_slash - path + 1;
258 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
259 if (!parent_path)
260 return NULL;
261
262 strlcpy(parent_path, path, parent_path_len);
263 parent = of_find_node_by_path(parent_path);
264 kfree(parent_path);
265 }
266
267 return parent;
268}
269
270int dlpar_attach_node(struct device_node *dn)
271{
ab519a01
NF
272 int rc;
273
ab519a01
NF
274 dn->parent = derive_parent(dn->full_name);
275 if (!dn->parent)
276 return -ENOMEM;
277
1cf3d8b3 278 rc = of_attach_node(dn);
3aef19f0 279 if (rc) {
ab519a01
NF
280 printk(KERN_ERR "Failed to add device node %s\n",
281 dn->full_name);
3aef19f0 282 return rc;
ab519a01
NF
283 }
284
ab519a01
NF
285 of_node_put(dn->parent);
286 return 0;
287}
288
289int dlpar_detach_node(struct device_node *dn)
290{
5935ff43 291 struct device_node *child;
1cf3d8b3 292 int rc;
ab519a01 293
5935ff43
TD
294 child = of_get_next_child(dn, NULL);
295 while (child) {
296 dlpar_detach_node(child);
297 child = of_get_next_child(dn, child);
298 }
299
1cf3d8b3
NF
300 rc = of_detach_node(dn);
301 if (rc)
302 return rc;
303
304 of_node_put(dn); /* Must decrement the refcount */
ab519a01
NF
305 return 0;
306}
307
308#define DR_ENTITY_SENSE 9003
309#define DR_ENTITY_PRESENT 1
310#define DR_ENTITY_UNUSABLE 2
311#define ALLOCATION_STATE 9003
312#define ALLOC_UNUSABLE 0
313#define ALLOC_USABLE 1
314#define ISOLATION_STATE 9001
315#define ISOLATE 0
316#define UNISOLATE 1
317
318int dlpar_acquire_drc(u32 drc_index)
319{
320 int dr_status, rc;
321
322 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
323 DR_ENTITY_SENSE, drc_index);
324 if (rc || dr_status != DR_ENTITY_UNUSABLE)
325 return -1;
326
327 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
328 if (rc)
329 return rc;
330
331 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
332 if (rc) {
333 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
334 return rc;
335 }
336
337 return 0;
338}
339
340int dlpar_release_drc(u32 drc_index)
341{
342 int dr_status, rc;
343
344 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
345 DR_ENTITY_SENSE, drc_index);
346 if (rc || dr_status != DR_ENTITY_PRESENT)
347 return -1;
348
349 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
350 if (rc)
351 return rc;
352
353 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
354 if (rc) {
355 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
356 return rc;
357 }
358
359 return 0;
360}
361
1a8061c4 362#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
ab519a01 363
275a64f6
NF
364static int dlpar_online_cpu(struct device_node *dn)
365{
366 int rc = 0;
367 unsigned int cpu;
368 int len, nthreads, i;
822e7122
TF
369 const __be32 *intserv;
370 u32 thread;
275a64f6
NF
371
372 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
373 if (!intserv)
374 return -EINVAL;
375
376 nthreads = len / sizeof(u32);
377
378 cpu_maps_update_begin();
379 for (i = 0; i < nthreads; i++) {
822e7122 380 thread = be32_to_cpu(intserv[i]);
275a64f6 381 for_each_present_cpu(cpu) {
822e7122 382 if (get_hard_smp_processor_id(cpu) != thread)
275a64f6
NF
383 continue;
384 BUG_ON(get_cpu_current_state(cpu)
385 != CPU_STATE_OFFLINE);
386 cpu_maps_update_done();
10ccaf17 387 rc = device_online(get_cpu_device(cpu));
275a64f6
NF
388 if (rc)
389 goto out;
390 cpu_maps_update_begin();
391
392 break;
393 }
394 if (cpu == num_possible_cpus())
395 printk(KERN_WARNING "Could not find cpu to online "
822e7122 396 "with physical id 0x%x\n", thread);
275a64f6
NF
397 }
398 cpu_maps_update_done();
399
400out:
401 return rc;
402
403}
404
1a8061c4
NF
405static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
406{
8d5ff320 407 struct device_node *dn, *parent;
1618bd53 408 u32 drc_index;
1a8061c4
NF
409 int rc;
410
1618bd53 411 rc = kstrtou32(buf, 0, &drc_index);
6dedcca6
TK
412 if (rc)
413 return -EINVAL;
1a8061c4 414
8d5ff320 415 parent = of_find_node_by_path("/cpus");
6dedcca6
TK
416 if (!parent)
417 return -ENODEV;
1a8061c4 418
d6f1e7ab 419 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
6dedcca6
TK
420 if (!dn)
421 return -EINVAL;
1a8061c4 422
8d5ff320 423 of_node_put(parent);
1a8061c4
NF
424
425 rc = dlpar_acquire_drc(drc_index);
426 if (rc) {
427 dlpar_free_cc_nodes(dn);
6dedcca6 428 return -EINVAL;
1a8061c4
NF
429 }
430
431 rc = dlpar_attach_node(dn);
432 if (rc) {
433 dlpar_release_drc(drc_index);
434 dlpar_free_cc_nodes(dn);
6dedcca6 435 return rc;
1a8061c4
NF
436 }
437
275a64f6 438 rc = dlpar_online_cpu(dn);
6dedcca6
TK
439 if (rc)
440 return rc;
b6db63d1 441
6dedcca6 442 return count;
1a8061c4
NF
443}
444
275a64f6
NF
445static int dlpar_offline_cpu(struct device_node *dn)
446{
447 int rc = 0;
448 unsigned int cpu;
449 int len, nthreads, i;
e36d1227
TF
450 const __be32 *intserv;
451 u32 thread;
275a64f6
NF
452
453 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
454 if (!intserv)
455 return -EINVAL;
456
457 nthreads = len / sizeof(u32);
458
459 cpu_maps_update_begin();
460 for (i = 0; i < nthreads; i++) {
e36d1227 461 thread = be32_to_cpu(intserv[i]);
275a64f6 462 for_each_present_cpu(cpu) {
e36d1227 463 if (get_hard_smp_processor_id(cpu) != thread)
275a64f6
NF
464 continue;
465
466 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
467 break;
468
469 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
ceddee23 470 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
275a64f6 471 cpu_maps_update_done();
10ccaf17 472 rc = device_offline(get_cpu_device(cpu));
275a64f6
NF
473 if (rc)
474 goto out;
475 cpu_maps_update_begin();
476 break;
477
478 }
479
480 /*
481 * The cpu is in CPU_STATE_INACTIVE.
482 * Upgrade it's state to CPU_STATE_OFFLINE.
483 */
484 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
e36d1227 485 BUG_ON(plpar_hcall_norets(H_PROD, thread)
275a64f6
NF
486 != H_SUCCESS);
487 __cpu_die(cpu);
488 break;
489 }
490 if (cpu == num_possible_cpus())
491 printk(KERN_WARNING "Could not find cpu to offline "
e36d1227 492 "with physical id 0x%x\n", thread);
275a64f6
NF
493 }
494 cpu_maps_update_done();
495
496out:
497 return rc;
498
499}
500
1a8061c4
NF
501static ssize_t dlpar_cpu_release(const char *buf, size_t count)
502{
503 struct device_node *dn;
e36d1227 504 u32 drc_index;
1a8061c4
NF
505 int rc;
506
507 dn = of_find_node_by_path(buf);
508 if (!dn)
509 return -EINVAL;
510
e36d1227
TF
511 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
512 if (rc) {
1a8061c4
NF
513 of_node_put(dn);
514 return -EINVAL;
515 }
516
275a64f6 517 rc = dlpar_offline_cpu(dn);
b6db63d1
GS
518 if (rc) {
519 of_node_put(dn);
6dedcca6 520 return -EINVAL;
b6db63d1
GS
521 }
522
e36d1227 523 rc = dlpar_release_drc(drc_index);
1a8061c4
NF
524 if (rc) {
525 of_node_put(dn);
6dedcca6 526 return rc;
1a8061c4
NF
527 }
528
529 rc = dlpar_detach_node(dn);
530 if (rc) {
e36d1227 531 dlpar_acquire_drc(drc_index);
6dedcca6 532 return rc;
1a8061c4
NF
533 }
534
535 of_node_put(dn);
6dedcca6
TK
536
537 return count;
1a8061c4
NF
538}
539
999e2dad
NF
540#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
541
542static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
543{
544 int rc;
545
546 /* pseries error logs are in BE format, convert to cpu type */
547 switch (hp_elog->id_type) {
548 case PSERIES_HP_ELOG_ID_DRC_COUNT:
549 hp_elog->_drc_u.drc_count =
550 be32_to_cpu(hp_elog->_drc_u.drc_count);
551 break;
552 case PSERIES_HP_ELOG_ID_DRC_INDEX:
553 hp_elog->_drc_u.drc_index =
554 be32_to_cpu(hp_elog->_drc_u.drc_index);
555 }
556
557 switch (hp_elog->resource) {
558 case PSERIES_HP_ELOG_RESOURCE_MEM:
559 rc = dlpar_memory(hp_elog);
560 break;
561 default:
562 pr_warn_ratelimited("Invalid resource (%d) specified\n",
563 hp_elog->resource);
564 rc = -EINVAL;
565 }
566
567 return rc;
568}
569
570static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
571 const char *buf, size_t count)
572{
573 struct pseries_hp_errorlog *hp_elog;
574 const char *arg;
575 int rc;
576
577 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
578 if (!hp_elog) {
579 rc = -ENOMEM;
580 goto dlpar_store_out;
581 }
582
583 /* Parse out the request from the user, this will be in the form
584 * <resource> <action> <id_type> <id>
585 */
586 arg = buf;
587 if (!strncmp(arg, "memory", 6)) {
588 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
589 arg += strlen("memory ");
590 } else {
591 pr_err("Invalid resource specified: \"%s\"\n", buf);
592 rc = -EINVAL;
593 goto dlpar_store_out;
594 }
595
596 if (!strncmp(arg, "add", 3)) {
597 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
598 arg += strlen("add ");
599 } else if (!strncmp(arg, "remove", 6)) {
600 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
601 arg += strlen("remove ");
602 } else {
603 pr_err("Invalid action specified: \"%s\"\n", buf);
604 rc = -EINVAL;
605 goto dlpar_store_out;
606 }
607
608 if (!strncmp(arg, "index", 5)) {
609 u32 index;
610
611 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
612 arg += strlen("index ");
613 if (kstrtou32(arg, 0, &index)) {
614 rc = -EINVAL;
615 pr_err("Invalid drc_index specified: \"%s\"\n", buf);
616 goto dlpar_store_out;
617 }
618
619 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
620 } else if (!strncmp(arg, "count", 5)) {
621 u32 count;
622
623 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
624 arg += strlen("count ");
625 if (kstrtou32(arg, 0, &count)) {
626 rc = -EINVAL;
627 pr_err("Invalid count specified: \"%s\"\n", buf);
628 goto dlpar_store_out;
629 }
630
631 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
632 } else {
633 pr_err("Invalid id_type specified: \"%s\"\n", buf);
634 rc = -EINVAL;
635 goto dlpar_store_out;
636 }
637
638 rc = handle_dlpar_errorlog(hp_elog);
639
640dlpar_store_out:
641 kfree(hp_elog);
642 return rc ? rc : count;
643}
644
645static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
646
1a8061c4
NF
647static int __init pseries_dlpar_init(void)
648{
999e2dad
NF
649 int rc;
650
651#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
1a8061c4
NF
652 ppc_md.cpu_probe = dlpar_cpu_probe;
653 ppc_md.cpu_release = dlpar_cpu_release;
999e2dad 654#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
1a8061c4 655
999e2dad
NF
656 rc = sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
657
658 return rc;
1a8061c4
NF
659}
660machine_device_initcall(pseries, pseries_dlpar_init);
661