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>
26 static struct workqueue_struct *pseries_hp_wq;
28 struct pseries_hp_work {
29 struct work_struct work;
30 struct pseries_hp_errorlog *errlog;
41 void dlpar_free_cc_property(struct property *prop)
48 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
50 struct property *prop;
54 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
58 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
59 prop->name = kstrdup(name, GFP_KERNEL);
61 dlpar_free_cc_property(prop);
65 prop->length = be32_to_cpu(ccwa->prop_length);
66 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
67 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
69 dlpar_free_cc_property(prop);
76 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
78 struct device_node *dn;
81 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
85 name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
86 dn->full_name = kstrdup(name, GFP_KERNEL);
92 of_node_set_flag(dn, OF_DYNAMIC);
98 static void dlpar_free_one_cc_node(struct device_node *dn)
100 struct property *prop;
102 while (dn->properties) {
103 prop = dn->properties;
104 dn->properties = prop->next;
105 dlpar_free_cc_property(prop);
108 kfree(dn->full_name);
112 void dlpar_free_cc_nodes(struct device_node *dn)
115 dlpar_free_cc_nodes(dn->child);
118 dlpar_free_cc_nodes(dn->sibling);
120 dlpar_free_one_cc_node(dn);
124 #define NEXT_SIBLING 1
126 #define NEXT_PROPERTY 3
127 #define PREV_PARENT 4
128 #define MORE_MEMORY 5
129 #define ERR_CFG_USE -9003
131 struct device_node *dlpar_configure_connector(__be32 drc_index,
132 struct device_node *parent)
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;
144 cc_token = rtas_token("ibm,configure-connector");
145 if (cc_token == RTAS_UNKNOWN_SERVICE)
148 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
152 ccwa = (struct cc_workarea *)&data_buf[0];
153 ccwa->drc_index = drc_index;
157 /* Since we release the rtas_data_buf lock between configure
158 * connector calls we want to re-populate the rtas_data_buffer
159 * with the contents of the previous call.
161 spin_lock(&rtas_data_buf_lock);
163 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
164 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
165 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
167 spin_unlock(&rtas_data_buf_lock);
169 if (rtas_busy_delay(rc))
177 dn = dlpar_parse_cc_node(ccwa);
181 dn->parent = last_dn->parent;
182 last_dn->sibling = dn;
187 dn = dlpar_parse_cc_node(ccwa);
195 dn->parent = last_dn;
204 property = dlpar_parse_cc_property(ccwa);
208 if (!last_dn->properties)
209 last_dn->properties = property;
211 last_property->next = property;
213 last_property = property;
217 last_dn = last_dn->parent;
223 printk(KERN_ERR "Unexpected Error (%d) "
224 "returned from configure-connector\n", rc);
234 dlpar_free_cc_nodes(first_dn);
242 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
248 rc = of_attach_node(dn);
250 printk(KERN_ERR "Failed to add device node %pOF\n", dn);
257 int dlpar_detach_node(struct device_node *dn)
259 struct device_node *child;
262 child = of_get_next_child(dn, NULL);
264 dlpar_detach_node(child);
265 child = of_get_next_child(dn, child);
268 rc = of_detach_node(dn);
277 #define DR_ENTITY_SENSE 9003
278 #define DR_ENTITY_PRESENT 1
279 #define DR_ENTITY_UNUSABLE 2
280 #define ALLOCATION_STATE 9003
281 #define ALLOC_UNUSABLE 0
282 #define ALLOC_USABLE 1
283 #define ISOLATION_STATE 9001
287 int dlpar_acquire_drc(u32 drc_index)
291 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
292 if (rc || dr_status != DR_ENTITY_UNUSABLE)
295 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
299 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
301 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
308 int dlpar_release_drc(u32 drc_index)
312 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
313 if (rc || dr_status != DR_ENTITY_PRESENT)
316 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
320 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
322 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
329 int dlpar_unisolate_drc(u32 drc_index)
333 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
334 if (rc || dr_status != DR_ENTITY_PRESENT)
337 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
342 int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
346 /* pseries error logs are in BE format, convert to cpu type */
347 switch (hp_elog->id_type) {
348 case PSERIES_HP_ELOG_ID_DRC_COUNT:
349 hp_elog->_drc_u.drc_count =
350 be32_to_cpu(hp_elog->_drc_u.drc_count);
352 case PSERIES_HP_ELOG_ID_DRC_INDEX:
353 hp_elog->_drc_u.drc_index =
354 be32_to_cpu(hp_elog->_drc_u.drc_index);
356 case PSERIES_HP_ELOG_ID_DRC_IC:
357 hp_elog->_drc_u.ic.count =
358 be32_to_cpu(hp_elog->_drc_u.ic.count);
359 hp_elog->_drc_u.ic.index =
360 be32_to_cpu(hp_elog->_drc_u.ic.index);
363 switch (hp_elog->resource) {
364 case PSERIES_HP_ELOG_RESOURCE_MEM:
365 rc = dlpar_memory(hp_elog);
367 case PSERIES_HP_ELOG_RESOURCE_CPU:
368 rc = dlpar_cpu(hp_elog);
370 case PSERIES_HP_ELOG_RESOURCE_PMEM:
371 rc = dlpar_hp_pmem(hp_elog);
375 pr_warn_ratelimited("Invalid resource (%d) specified\n",
383 static void pseries_hp_work_fn(struct work_struct *work)
385 struct pseries_hp_work *hp_work =
386 container_of(work, struct pseries_hp_work, work);
388 handle_dlpar_errorlog(hp_work->errlog);
390 kfree(hp_work->errlog);
394 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
396 struct pseries_hp_work *work;
397 struct pseries_hp_errorlog *hp_errlog_copy;
399 hp_errlog_copy = kmemdup(hp_errlog, sizeof(*hp_errlog), GFP_ATOMIC);
403 work = kmalloc(sizeof(struct pseries_hp_work), GFP_ATOMIC);
405 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
406 work->errlog = hp_errlog_copy;
407 queue_work(pseries_hp_wq, (struct work_struct *)work);
409 kfree(hp_errlog_copy);
413 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
417 arg = strsep(cmd, " ");
421 if (sysfs_streq(arg, "memory")) {
422 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
423 } else if (sysfs_streq(arg, "cpu")) {
424 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
426 pr_err("Invalid resource specified.\n");
433 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
437 arg = strsep(cmd, " ");
441 if (sysfs_streq(arg, "add")) {
442 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
443 } else if (sysfs_streq(arg, "remove")) {
444 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
446 pr_err("Invalid action specified.\n");
453 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
458 arg = strsep(cmd, " ");
462 if (sysfs_streq(arg, "indexed-count")) {
463 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
464 arg = strsep(cmd, " ");
466 pr_err("No DRC count specified.\n");
470 if (kstrtou32(arg, 0, &count)) {
471 pr_err("Invalid DRC count specified.\n");
475 arg = strsep(cmd, " ");
477 pr_err("No DRC Index specified.\n");
481 if (kstrtou32(arg, 0, &index)) {
482 pr_err("Invalid DRC Index specified.\n");
486 hp_elog->_drc_u.ic.count = cpu_to_be32(count);
487 hp_elog->_drc_u.ic.index = cpu_to_be32(index);
488 } else if (sysfs_streq(arg, "index")) {
489 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
490 arg = strsep(cmd, " ");
492 pr_err("No DRC Index specified.\n");
496 if (kstrtou32(arg, 0, &index)) {
497 pr_err("Invalid DRC Index specified.\n");
501 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
502 } else if (sysfs_streq(arg, "count")) {
503 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
504 arg = strsep(cmd, " ");
506 pr_err("No DRC count specified.\n");
510 if (kstrtou32(arg, 0, &count)) {
511 pr_err("Invalid DRC count specified.\n");
515 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
517 pr_err("Invalid id_type specified.\n");
524 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
525 const char *buf, size_t count)
527 struct pseries_hp_errorlog hp_elog;
532 args = argbuf = kstrdup(buf, GFP_KERNEL);
537 * Parse out the request from the user, this will be in the form:
538 * <resource> <action> <id_type> <id>
540 rc = dlpar_parse_resource(&args, &hp_elog);
542 goto dlpar_store_out;
544 rc = dlpar_parse_action(&args, &hp_elog);
546 goto dlpar_store_out;
548 rc = dlpar_parse_id_type(&args, &hp_elog);
550 goto dlpar_store_out;
552 rc = handle_dlpar_errorlog(&hp_elog);
558 pr_err("Could not handle DLPAR request \"%s\"\n", buf);
560 return rc ? rc : count;
563 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
566 return sprintf(buf, "%s\n", "memory,cpu");
569 static CLASS_ATTR_RW(dlpar);
571 int __init dlpar_workqueue_init(void)
576 pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
579 return pseries_hp_wq ? 0 : -ENOMEM;
582 static int __init dlpar_sysfs_init(void)
586 rc = dlpar_workqueue_init();
590 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
592 machine_device_initcall(pseries, dlpar_sysfs_init);