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"
23 #include <asm/machdep.h>
24 #include <linux/uaccess.h>
27 static struct workqueue_struct *pseries_hp_wq;
29 struct pseries_hp_work {
30 struct work_struct work;
31 struct pseries_hp_errorlog *errlog;
42 void dlpar_free_cc_property(struct property *prop)
49 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
51 struct property *prop;
55 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
59 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
60 prop->name = kstrdup(name, GFP_KERNEL);
62 dlpar_free_cc_property(prop);
66 prop->length = be32_to_cpu(ccwa->prop_length);
67 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
68 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
70 dlpar_free_cc_property(prop);
77 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
79 struct device_node *dn;
82 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
86 name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
87 dn->full_name = kstrdup(name, GFP_KERNEL);
93 of_node_set_flag(dn, OF_DYNAMIC);
99 static void dlpar_free_one_cc_node(struct device_node *dn)
101 struct property *prop;
103 while (dn->properties) {
104 prop = dn->properties;
105 dn->properties = prop->next;
106 dlpar_free_cc_property(prop);
109 kfree(dn->full_name);
113 void dlpar_free_cc_nodes(struct device_node *dn)
116 dlpar_free_cc_nodes(dn->child);
119 dlpar_free_cc_nodes(dn->sibling);
121 dlpar_free_one_cc_node(dn);
125 #define NEXT_SIBLING 1
127 #define NEXT_PROPERTY 3
128 #define PREV_PARENT 4
129 #define MORE_MEMORY 5
130 #define ERR_CFG_USE -9003
132 struct device_node *dlpar_configure_connector(__be32 drc_index,
133 struct device_node *parent)
135 struct device_node *dn;
136 struct device_node *first_dn = NULL;
137 struct device_node *last_dn = NULL;
138 struct property *property;
139 struct property *last_property = NULL;
140 struct cc_workarea *ccwa;
145 cc_token = rtas_token("ibm,configure-connector");
146 if (cc_token == RTAS_UNKNOWN_SERVICE)
149 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
153 ccwa = (struct cc_workarea *)&data_buf[0];
154 ccwa->drc_index = drc_index;
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.
162 spin_lock(&rtas_data_buf_lock);
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);
168 spin_unlock(&rtas_data_buf_lock);
170 if (rtas_busy_delay(rc))
178 dn = dlpar_parse_cc_node(ccwa);
182 dn->parent = last_dn->parent;
183 last_dn->sibling = dn;
188 dn = dlpar_parse_cc_node(ccwa);
196 dn->parent = last_dn;
205 property = dlpar_parse_cc_property(ccwa);
209 if (!last_dn->properties)
210 last_dn->properties = property;
212 last_property->next = property;
214 last_property = property;
218 last_dn = last_dn->parent;
224 printk(KERN_ERR "Unexpected Error (%d) "
225 "returned from configure-connector\n", rc);
235 dlpar_free_cc_nodes(first_dn);
243 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
249 rc = of_attach_node(dn);
251 printk(KERN_ERR "Failed to add device node %pOF\n", dn);
258 int dlpar_detach_node(struct device_node *dn)
260 struct device_node *child;
263 child = of_get_next_child(dn, NULL);
265 dlpar_detach_node(child);
266 child = of_get_next_child(dn, child);
269 rc = of_detach_node(dn);
278 #define DR_ENTITY_SENSE 9003
279 #define DR_ENTITY_PRESENT 1
280 #define DR_ENTITY_UNUSABLE 2
281 #define ALLOCATION_STATE 9003
282 #define ALLOC_UNUSABLE 0
283 #define ALLOC_USABLE 1
284 #define ISOLATION_STATE 9001
288 int dlpar_acquire_drc(u32 drc_index)
292 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
293 if (rc || dr_status != DR_ENTITY_UNUSABLE)
296 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
300 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
302 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
309 int dlpar_release_drc(u32 drc_index)
313 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
314 if (rc || dr_status != DR_ENTITY_PRESENT)
317 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
321 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
323 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
330 int dlpar_unisolate_drc(u32 drc_index)
334 rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
335 if (rc || dr_status != DR_ENTITY_PRESENT)
338 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
343 int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
347 /* pseries error logs are in BE format, convert to cpu type */
348 switch (hp_elog->id_type) {
349 case PSERIES_HP_ELOG_ID_DRC_COUNT:
350 hp_elog->_drc_u.drc_count =
351 be32_to_cpu(hp_elog->_drc_u.drc_count);
353 case PSERIES_HP_ELOG_ID_DRC_INDEX:
354 hp_elog->_drc_u.drc_index =
355 be32_to_cpu(hp_elog->_drc_u.drc_index);
357 case PSERIES_HP_ELOG_ID_DRC_IC:
358 hp_elog->_drc_u.ic.count =
359 be32_to_cpu(hp_elog->_drc_u.ic.count);
360 hp_elog->_drc_u.ic.index =
361 be32_to_cpu(hp_elog->_drc_u.ic.index);
364 switch (hp_elog->resource) {
365 case PSERIES_HP_ELOG_RESOURCE_MEM:
366 rc = dlpar_memory(hp_elog);
368 case PSERIES_HP_ELOG_RESOURCE_CPU:
369 rc = dlpar_cpu(hp_elog);
371 case PSERIES_HP_ELOG_RESOURCE_PMEM:
372 rc = dlpar_hp_pmem(hp_elog);
376 pr_warn_ratelimited("Invalid resource (%d) specified\n",
384 static void pseries_hp_work_fn(struct work_struct *work)
386 struct pseries_hp_work *hp_work =
387 container_of(work, struct pseries_hp_work, work);
389 handle_dlpar_errorlog(hp_work->errlog);
391 kfree(hp_work->errlog);
395 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
397 struct pseries_hp_work *work;
398 struct pseries_hp_errorlog *hp_errlog_copy;
400 hp_errlog_copy = kmemdup(hp_errlog, sizeof(*hp_errlog), GFP_ATOMIC);
404 work = kmalloc(sizeof(struct pseries_hp_work), GFP_ATOMIC);
406 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
407 work->errlog = hp_errlog_copy;
408 queue_work(pseries_hp_wq, (struct work_struct *)work);
410 kfree(hp_errlog_copy);
414 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
418 arg = strsep(cmd, " ");
422 if (sysfs_streq(arg, "memory")) {
423 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
424 } else if (sysfs_streq(arg, "cpu")) {
425 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
427 pr_err("Invalid resource specified.\n");
434 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
438 arg = strsep(cmd, " ");
442 if (sysfs_streq(arg, "add")) {
443 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
444 } else if (sysfs_streq(arg, "remove")) {
445 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
447 pr_err("Invalid action specified.\n");
454 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
459 arg = strsep(cmd, " ");
463 if (sysfs_streq(arg, "indexed-count")) {
464 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
465 arg = strsep(cmd, " ");
467 pr_err("No DRC count specified.\n");
471 if (kstrtou32(arg, 0, &count)) {
472 pr_err("Invalid DRC count specified.\n");
476 arg = strsep(cmd, " ");
478 pr_err("No DRC Index specified.\n");
482 if (kstrtou32(arg, 0, &index)) {
483 pr_err("Invalid DRC Index specified.\n");
487 hp_elog->_drc_u.ic.count = cpu_to_be32(count);
488 hp_elog->_drc_u.ic.index = cpu_to_be32(index);
489 } else if (sysfs_streq(arg, "index")) {
490 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
491 arg = strsep(cmd, " ");
493 pr_err("No DRC Index specified.\n");
497 if (kstrtou32(arg, 0, &index)) {
498 pr_err("Invalid DRC Index specified.\n");
502 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
503 } else if (sysfs_streq(arg, "count")) {
504 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
505 arg = strsep(cmd, " ");
507 pr_err("No DRC count specified.\n");
511 if (kstrtou32(arg, 0, &count)) {
512 pr_err("Invalid DRC count specified.\n");
516 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
518 pr_err("Invalid id_type specified.\n");
525 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
526 const char *buf, size_t count)
528 struct pseries_hp_errorlog hp_elog;
533 args = argbuf = kstrdup(buf, GFP_KERNEL);
538 * Parse out the request from the user, this will be in the form:
539 * <resource> <action> <id_type> <id>
541 rc = dlpar_parse_resource(&args, &hp_elog);
543 goto dlpar_store_out;
545 rc = dlpar_parse_action(&args, &hp_elog);
547 goto dlpar_store_out;
549 rc = dlpar_parse_id_type(&args, &hp_elog);
551 goto dlpar_store_out;
553 rc = handle_dlpar_errorlog(&hp_elog);
559 pr_err("Could not handle DLPAR request \"%s\"\n", buf);
561 return rc ? rc : count;
564 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
567 return sprintf(buf, "%s\n", "memory,cpu");
570 static CLASS_ATTR_RW(dlpar);
572 int __init dlpar_workqueue_init(void)
577 pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
580 return pseries_hp_wq ? 0 : -ENOMEM;
583 static int __init dlpar_sysfs_init(void)
587 rc = dlpar_workqueue_init();
591 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
593 machine_device_initcall(pseries, dlpar_sysfs_init);