1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for dynamic reconfiguration for PCI, Memory, and CPU
4 * Hotplug and Dynamic Logical Partitioning on RPA platforms.
6 * Copyright (C) 2009 Nathan Fontenot
7 * Copyright (C) 2009 IBM Corporation
10 #define pr_fmt(fmt) "dlpar: " fmt
12 #include <linux/kernel.h>
13 #include <linux/notifier.h>
14 #include <linux/spinlock.h>
15 #include <linux/cpu.h>
16 #include <linux/slab.h>
19 #include "of_helpers.h"
22 #include <asm/machdep.h>
23 #include <linux/uaccess.h>
25 #include <asm/rtas-work-area.h>
28 static struct workqueue_struct *pseries_hp_wq;
30 struct pseries_hp_work {
31 struct work_struct work;
32 struct pseries_hp_errorlog *errlog;
43 void dlpar_free_cc_property(struct property *prop)
50 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
52 struct property *prop;
56 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
60 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
61 prop->name = kstrdup(name, GFP_KERNEL);
63 dlpar_free_cc_property(prop);
67 prop->length = be32_to_cpu(ccwa->prop_length);
68 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
69 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
71 dlpar_free_cc_property(prop);
78 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
80 struct device_node *dn;
83 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
87 name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
88 dn->full_name = kstrdup(name, GFP_KERNEL);
94 of_node_set_flag(dn, OF_DYNAMIC);
100 static void dlpar_free_one_cc_node(struct device_node *dn)
102 struct property *prop;
104 while (dn->properties) {
105 prop = dn->properties;
106 dn->properties = prop->next;
107 dlpar_free_cc_property(prop);
110 kfree(dn->full_name);
114 void dlpar_free_cc_nodes(struct device_node *dn)
117 dlpar_free_cc_nodes(dn->child);
120 dlpar_free_cc_nodes(dn->sibling);
122 dlpar_free_one_cc_node(dn);
126 #define NEXT_SIBLING 1
128 #define NEXT_PROPERTY 3
129 #define PREV_PARENT 4
130 #define MORE_MEMORY 5
131 #define ERR_CFG_USE -9003
133 struct device_node *dlpar_configure_connector(__be32 drc_index,
134 struct device_node *parent)
136 struct device_node *dn;
137 struct device_node *first_dn = NULL;
138 struct device_node *last_dn = NULL;
139 struct property *property;
140 struct property *last_property = NULL;
141 struct cc_workarea *ccwa;
142 struct rtas_work_area *work_area;
147 cc_token = rtas_function_token(RTAS_FN_IBM_CONFIGURE_CONNECTOR);
148 if (cc_token == RTAS_UNKNOWN_SERVICE)
151 work_area = rtas_work_area_alloc(SZ_4K);
152 data_buf = rtas_work_area_raw_buf(work_area);
154 ccwa = (struct cc_workarea *)&data_buf[0];
155 ccwa->drc_index = drc_index;
160 rc = rtas_call(cc_token, 2, 1, NULL,
161 rtas_work_area_phys(work_area), NULL);
162 } while (rtas_busy_delay(rc));
169 dn = dlpar_parse_cc_node(ccwa);
173 dn->parent = last_dn->parent;
174 last_dn->sibling = dn;
179 dn = dlpar_parse_cc_node(ccwa);
187 dn->parent = last_dn;
196 property = dlpar_parse_cc_property(ccwa);
200 if (!last_dn->properties)
201 last_dn->properties = property;
203 last_property->next = property;
205 last_property = property;
209 last_dn = last_dn->parent;
215 printk(KERN_ERR "Unexpected Error (%d) "
216 "returned from configure-connector\n", rc);
222 rtas_work_area_free(work_area);
226 dlpar_free_cc_nodes(first_dn);
234 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
240 rc = of_attach_node(dn);
242 printk(KERN_ERR "Failed to add device node %pOF\n", dn);
249 int dlpar_detach_node(struct device_node *dn)
251 struct device_node *child;
254 for_each_child_of_node(dn, child)
255 dlpar_detach_node(child);
257 rc = of_detach_node(dn);
265 static int dlpar_changeset_attach_cc_nodes(struct of_changeset *ocs,
266 struct device_node *dn)
270 rc = of_changeset_attach_node(ocs, dn);
272 if (!rc && dn->child)
273 rc = dlpar_changeset_attach_cc_nodes(ocs, dn->child);
274 if (!rc && dn->sibling)
275 rc = dlpar_changeset_attach_cc_nodes(ocs, dn->sibling);
280 #define DR_ENTITY_SENSE 9003
281 #define DR_ENTITY_PRESENT 1
282 #define DR_ENTITY_UNUSABLE 2
283 #define ALLOCATION_STATE 9003
284 #define ALLOC_UNUSABLE 0
285 #define ALLOC_USABLE 1
286 #define ISOLATION_STATE 9001
290 int dlpar_acquire_drc(u32 drc_index)
294 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
295 if (rc || dr_status != DR_ENTITY_UNUSABLE)
298 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
302 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
304 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
311 int dlpar_release_drc(u32 drc_index)
315 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
316 if (rc || dr_status != DR_ENTITY_PRESENT)
319 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
323 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
325 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
332 int dlpar_unisolate_drc(u32 drc_index)
336 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
337 if (rc || dr_status != DR_ENTITY_PRESENT)
340 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
345 static struct device_node *
346 get_device_node_with_drc_index(u32 index)
348 struct device_node *np = NULL;
352 for_each_node_with_property(np, "ibm,my-drc-index") {
353 rc = of_property_read_u32(np, "ibm,my-drc-index",
356 pr_err("%s: %pOF: of_property_read_u32 %s: %d\n",
357 __func__, np, "ibm,my-drc-index", rc);
362 if (index == node_index)
369 static struct device_node *
370 get_device_node_with_drc_info(u32 index)
372 struct device_node *np = NULL;
373 struct of_drc_info drc;
374 struct property *info;
379 for_each_node_with_property(np, "ibm,drc-info") {
380 info = of_find_property(np, "ibm,drc-info", NULL);
382 /* XXX can this happen? */
386 value = of_prop_next_u32(info, NULL, &count);
390 for (i = 0; i < count; i++) {
391 if (of_read_drc_info_cell(&info, &value, &drc))
393 if (index > drc.last_drc_index)
395 node_index = drc.drc_index_start;
396 for (j = 0; j < drc.num_sequential_elems; j++) {
397 if (index == node_index)
399 node_index += drc.sequential_inc;
407 static int dlpar_hp_dt_add(u32 index)
409 struct device_node *np, *nodes;
410 struct of_changeset ocs;
414 * Do not add device node(s) if already exists in the
417 np = get_device_node_with_drc_index(index);
419 pr_err("%s: Adding device node for index (%d), but "
420 "already exists in the device tree\n",
426 np = get_device_node_with_drc_info(index);
431 /* Next, configure the connector. */
432 nodes = dlpar_configure_connector(cpu_to_be32(index), np);
439 * Add the new nodes from dlpar_configure_connector() onto
442 of_changeset_init(&ocs);
443 rc = dlpar_changeset_attach_cc_nodes(&ocs, nodes);
446 rc = of_changeset_apply(&ocs);
448 dlpar_free_cc_nodes(nodes);
450 of_changeset_destroy(&ocs);
457 static int changeset_detach_node_recursive(struct of_changeset *ocs,
458 struct device_node *node)
460 struct device_node *child;
463 for_each_child_of_node(node, child) {
464 rc = changeset_detach_node_recursive(ocs, child);
471 return of_changeset_detach_node(ocs, node);
474 static int dlpar_hp_dt_remove(u32 drc_index)
476 struct device_node *np;
477 struct of_changeset ocs;
482 * Prune all nodes with a matching index.
484 of_changeset_init(&ocs);
486 for_each_node_with_property(np, "ibm,my-drc-index") {
487 rc = of_property_read_u32(np, "ibm,my-drc-index", &index);
489 pr_err("%s: %pOF: of_property_read_u32 %s: %d\n",
490 __func__, np, "ibm,my-drc-index", rc);
495 if (index == drc_index) {
496 rc = changeset_detach_node_recursive(&ocs, np);
504 rc = of_changeset_apply(&ocs);
507 of_changeset_destroy(&ocs);
511 static int dlpar_hp_dt(struct pseries_hp_errorlog *phpe)
516 if (phpe->id_type != PSERIES_HP_ELOG_ID_DRC_INDEX)
519 drc_index = be32_to_cpu(phpe->_drc_u.drc_index);
521 lock_device_hotplug();
523 switch (phpe->action) {
524 case PSERIES_HP_ELOG_ACTION_ADD:
525 rc = dlpar_hp_dt_add(drc_index);
527 case PSERIES_HP_ELOG_ACTION_REMOVE:
528 rc = dlpar_hp_dt_remove(drc_index);
531 pr_err("Invalid action (%d) specified\n", phpe->action);
536 unlock_device_hotplug();
541 int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
545 switch (hp_elog->resource) {
546 case PSERIES_HP_ELOG_RESOURCE_MEM:
547 rc = dlpar_memory(hp_elog);
549 case PSERIES_HP_ELOG_RESOURCE_CPU:
550 rc = dlpar_cpu(hp_elog);
552 case PSERIES_HP_ELOG_RESOURCE_PMEM:
553 rc = dlpar_hp_pmem(hp_elog);
555 case PSERIES_HP_ELOG_RESOURCE_DT:
556 rc = dlpar_hp_dt(hp_elog);
560 pr_warn_ratelimited("Invalid resource (%d) specified\n",
568 static void pseries_hp_work_fn(struct work_struct *work)
570 struct pseries_hp_work *hp_work =
571 container_of(work, struct pseries_hp_work, work);
573 handle_dlpar_errorlog(hp_work->errlog);
575 kfree(hp_work->errlog);
579 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
581 struct pseries_hp_work *work;
582 struct pseries_hp_errorlog *hp_errlog_copy;
584 hp_errlog_copy = kmemdup(hp_errlog, sizeof(*hp_errlog), GFP_ATOMIC);
588 work = kmalloc(sizeof(struct pseries_hp_work), GFP_ATOMIC);
590 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
591 work->errlog = hp_errlog_copy;
592 queue_work(pseries_hp_wq, (struct work_struct *)work);
594 kfree(hp_errlog_copy);
598 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
602 arg = strsep(cmd, " ");
606 if (sysfs_streq(arg, "memory")) {
607 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
608 } else if (sysfs_streq(arg, "cpu")) {
609 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
610 } else if (sysfs_streq(arg, "dt")) {
611 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_DT;
613 pr_err("Invalid resource specified.\n");
620 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
624 arg = strsep(cmd, " ");
628 if (sysfs_streq(arg, "add")) {
629 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
630 } else if (sysfs_streq(arg, "remove")) {
631 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
633 pr_err("Invalid action specified.\n");
640 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
645 arg = strsep(cmd, " ");
649 if (sysfs_streq(arg, "indexed-count")) {
650 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
651 arg = strsep(cmd, " ");
653 pr_err("No DRC count specified.\n");
657 if (kstrtou32(arg, 0, &count)) {
658 pr_err("Invalid DRC count specified.\n");
662 arg = strsep(cmd, " ");
664 pr_err("No DRC Index specified.\n");
668 if (kstrtou32(arg, 0, &index)) {
669 pr_err("Invalid DRC Index specified.\n");
673 hp_elog->_drc_u.ic.count = cpu_to_be32(count);
674 hp_elog->_drc_u.ic.index = cpu_to_be32(index);
675 } else if (sysfs_streq(arg, "index")) {
676 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
677 arg = strsep(cmd, " ");
679 pr_err("No DRC Index specified.\n");
683 if (kstrtou32(arg, 0, &index)) {
684 pr_err("Invalid DRC Index specified.\n");
688 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
689 } else if (sysfs_streq(arg, "count")) {
690 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
691 arg = strsep(cmd, " ");
693 pr_err("No DRC count specified.\n");
697 if (kstrtou32(arg, 0, &count)) {
698 pr_err("Invalid DRC count specified.\n");
702 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
704 pr_err("Invalid id_type specified.\n");
711 static ssize_t dlpar_store(const struct class *class, const struct class_attribute *attr,
712 const char *buf, size_t count)
714 struct pseries_hp_errorlog hp_elog;
719 args = argbuf = kstrdup(buf, GFP_KERNEL);
724 * Parse out the request from the user, this will be in the form:
725 * <resource> <action> <id_type> <id>
727 rc = dlpar_parse_resource(&args, &hp_elog);
729 goto dlpar_store_out;
731 rc = dlpar_parse_action(&args, &hp_elog);
733 goto dlpar_store_out;
735 rc = dlpar_parse_id_type(&args, &hp_elog);
737 goto dlpar_store_out;
739 rc = handle_dlpar_errorlog(&hp_elog);
745 pr_err("Could not handle DLPAR request \"%s\"\n", buf);
747 return rc ? rc : count;
750 static ssize_t dlpar_show(const struct class *class, const struct class_attribute *attr,
753 return sprintf(buf, "%s\n", "memory,cpu,dt");
756 static CLASS_ATTR_RW(dlpar);
758 int __init dlpar_workqueue_init(void)
763 pseries_hp_wq = alloc_ordered_workqueue("pseries hotplug workqueue", 0);
765 return pseries_hp_wq ? 0 : -ENOMEM;
768 static int __init dlpar_sysfs_init(void)
772 rc = dlpar_workqueue_init();
776 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
778 machine_device_initcall(pseries, dlpar_sysfs_init);