2 * Support for dynamic reconfiguration for PCI, Memory, and CPU
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms.
5 * Copyright (C) 2009 Nathan Fontenot
6 * Copyright (C) 2009 IBM Corporation
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.
13 #define pr_fmt(fmt) "dlpar: " fmt
15 #include <linux/kernel.h>
16 #include <linux/notifier.h>
17 #include <linux/spinlock.h>
18 #include <linux/cpu.h>
19 #include <linux/slab.h>
22 #include "of_helpers.h"
23 #include "offline_states.h"
27 #include <asm/machdep.h>
28 #include <asm/uaccess.h>
39 void dlpar_free_cc_property(struct property *prop)
46 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
48 struct property *prop;
52 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
56 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
57 prop->name = kstrdup(name, GFP_KERNEL);
59 dlpar_free_cc_property(prop);
63 prop->length = be32_to_cpu(ccwa->prop_length);
64 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
65 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
67 dlpar_free_cc_property(prop);
74 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
77 struct device_node *dn;
80 /* If parent node path is "/" advance path to NULL terminator to
81 * prevent double leading slashs in full_name.
86 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
90 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
91 dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
97 of_node_set_flag(dn, OF_DYNAMIC);
103 static void dlpar_free_one_cc_node(struct device_node *dn)
105 struct property *prop;
107 while (dn->properties) {
108 prop = dn->properties;
109 dn->properties = prop->next;
110 dlpar_free_cc_property(prop);
113 kfree(dn->full_name);
117 void dlpar_free_cc_nodes(struct device_node *dn)
120 dlpar_free_cc_nodes(dn->child);
123 dlpar_free_cc_nodes(dn->sibling);
125 dlpar_free_one_cc_node(dn);
129 #define NEXT_SIBLING 1
131 #define NEXT_PROPERTY 3
132 #define PREV_PARENT 4
133 #define MORE_MEMORY 5
134 #define ERR_CFG_USE -9003
136 struct device_node *dlpar_configure_connector(__be32 drc_index,
137 struct device_node *parent)
139 struct device_node *dn;
140 struct device_node *first_dn = NULL;
141 struct device_node *last_dn = NULL;
142 struct property *property;
143 struct property *last_property = NULL;
144 struct cc_workarea *ccwa;
146 const char *parent_path = parent->full_name;
150 cc_token = rtas_token("ibm,configure-connector");
151 if (cc_token == RTAS_UNKNOWN_SERVICE)
154 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
158 ccwa = (struct cc_workarea *)&data_buf[0];
159 ccwa->drc_index = drc_index;
163 /* Since we release the rtas_data_buf lock between configure
164 * connector calls we want to re-populate the rtas_data_buffer
165 * with the contents of the previous call.
167 spin_lock(&rtas_data_buf_lock);
169 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
170 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
171 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
173 spin_unlock(&rtas_data_buf_lock);
175 if (rtas_busy_delay(rc))
183 dn = dlpar_parse_cc_node(ccwa, parent_path);
187 dn->parent = last_dn->parent;
188 last_dn->sibling = dn;
194 parent_path = last_dn->full_name;
196 dn = dlpar_parse_cc_node(ccwa, parent_path);
204 dn->parent = last_dn;
213 property = dlpar_parse_cc_property(ccwa);
217 if (!last_dn->properties)
218 last_dn->properties = property;
220 last_property->next = property;
222 last_property = property;
226 last_dn = last_dn->parent;
227 parent_path = last_dn->parent->full_name;
233 printk(KERN_ERR "Unexpected Error (%d) "
234 "returned from configure-connector\n", rc);
244 dlpar_free_cc_nodes(first_dn);
252 int dlpar_attach_node(struct device_node *dn)
256 dn->parent = pseries_of_derive_parent(dn->full_name);
257 if (IS_ERR(dn->parent))
258 return PTR_ERR(dn->parent);
260 rc = of_attach_node(dn);
262 printk(KERN_ERR "Failed to add device node %s\n",
267 of_node_put(dn->parent);
271 int dlpar_detach_node(struct device_node *dn)
273 struct device_node *child;
276 child = of_get_next_child(dn, NULL);
278 dlpar_detach_node(child);
279 child = of_get_next_child(dn, child);
282 rc = of_detach_node(dn);
291 #define DR_ENTITY_SENSE 9003
292 #define DR_ENTITY_PRESENT 1
293 #define DR_ENTITY_UNUSABLE 2
294 #define ALLOCATION_STATE 9003
295 #define ALLOC_UNUSABLE 0
296 #define ALLOC_USABLE 1
297 #define ISOLATION_STATE 9001
301 int dlpar_acquire_drc(u32 drc_index)
305 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
306 DR_ENTITY_SENSE, drc_index);
307 if (rc || dr_status != DR_ENTITY_UNUSABLE)
310 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
314 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
316 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
323 int dlpar_release_drc(u32 drc_index)
327 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
328 DR_ENTITY_SENSE, drc_index);
329 if (rc || dr_status != DR_ENTITY_PRESENT)
332 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
336 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
338 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
345 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
347 static int dlpar_online_cpu(struct device_node *dn)
351 int len, nthreads, i;
352 const __be32 *intserv;
355 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
359 nthreads = len / sizeof(u32);
361 cpu_maps_update_begin();
362 for (i = 0; i < nthreads; i++) {
363 thread = be32_to_cpu(intserv[i]);
364 for_each_present_cpu(cpu) {
365 if (get_hard_smp_processor_id(cpu) != thread)
367 BUG_ON(get_cpu_current_state(cpu)
368 != CPU_STATE_OFFLINE);
369 cpu_maps_update_done();
370 rc = device_online(get_cpu_device(cpu));
373 cpu_maps_update_begin();
377 if (cpu == num_possible_cpus())
378 printk(KERN_WARNING "Could not find cpu to online "
379 "with physical id 0x%x\n", thread);
381 cpu_maps_update_done();
388 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
390 struct device_node *dn, *parent;
394 rc = kstrtou32(buf, 0, &drc_index);
398 rc = dlpar_acquire_drc(drc_index);
402 parent = of_find_node_by_path("/cpus");
406 dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
409 dlpar_release_drc(drc_index);
413 rc = dlpar_attach_node(dn);
415 dlpar_release_drc(drc_index);
416 dlpar_free_cc_nodes(dn);
420 rc = dlpar_online_cpu(dn);
427 static int dlpar_offline_cpu(struct device_node *dn)
431 int len, nthreads, i;
432 const __be32 *intserv;
435 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
439 nthreads = len / sizeof(u32);
441 cpu_maps_update_begin();
442 for (i = 0; i < nthreads; i++) {
443 thread = be32_to_cpu(intserv[i]);
444 for_each_present_cpu(cpu) {
445 if (get_hard_smp_processor_id(cpu) != thread)
448 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
451 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
452 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
453 cpu_maps_update_done();
454 rc = device_offline(get_cpu_device(cpu));
457 cpu_maps_update_begin();
463 * The cpu is in CPU_STATE_INACTIVE.
464 * Upgrade it's state to CPU_STATE_OFFLINE.
466 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
467 BUG_ON(plpar_hcall_norets(H_PROD, thread)
472 if (cpu == num_possible_cpus())
473 printk(KERN_WARNING "Could not find cpu to offline "
474 "with physical id 0x%x\n", thread);
476 cpu_maps_update_done();
483 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
485 struct device_node *dn;
489 dn = of_find_node_by_path(buf);
493 rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
499 rc = dlpar_offline_cpu(dn);
505 rc = dlpar_release_drc(drc_index);
511 rc = dlpar_detach_node(dn);
513 dlpar_acquire_drc(drc_index);
522 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
524 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
528 /* pseries error logs are in BE format, convert to cpu type */
529 switch (hp_elog->id_type) {
530 case PSERIES_HP_ELOG_ID_DRC_COUNT:
531 hp_elog->_drc_u.drc_count =
532 be32_to_cpu(hp_elog->_drc_u.drc_count);
534 case PSERIES_HP_ELOG_ID_DRC_INDEX:
535 hp_elog->_drc_u.drc_index =
536 be32_to_cpu(hp_elog->_drc_u.drc_index);
539 switch (hp_elog->resource) {
540 case PSERIES_HP_ELOG_RESOURCE_MEM:
541 rc = dlpar_memory(hp_elog);
544 pr_warn_ratelimited("Invalid resource (%d) specified\n",
552 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
553 const char *buf, size_t count)
555 struct pseries_hp_errorlog *hp_elog;
559 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
562 goto dlpar_store_out;
565 /* Parse out the request from the user, this will be in the form
566 * <resource> <action> <id_type> <id>
569 if (!strncmp(arg, "memory", 6)) {
570 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
571 arg += strlen("memory ");
573 pr_err("Invalid resource specified: \"%s\"\n", buf);
575 goto dlpar_store_out;
578 if (!strncmp(arg, "add", 3)) {
579 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
580 arg += strlen("add ");
581 } else if (!strncmp(arg, "remove", 6)) {
582 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
583 arg += strlen("remove ");
585 pr_err("Invalid action specified: \"%s\"\n", buf);
587 goto dlpar_store_out;
590 if (!strncmp(arg, "index", 5)) {
593 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
594 arg += strlen("index ");
595 if (kstrtou32(arg, 0, &index)) {
597 pr_err("Invalid drc_index specified: \"%s\"\n", buf);
598 goto dlpar_store_out;
601 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
602 } else if (!strncmp(arg, "count", 5)) {
605 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
606 arg += strlen("count ");
607 if (kstrtou32(arg, 0, &count)) {
609 pr_err("Invalid count specified: \"%s\"\n", buf);
610 goto dlpar_store_out;
613 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
615 pr_err("Invalid id_type specified: \"%s\"\n", buf);
617 goto dlpar_store_out;
620 rc = handle_dlpar_errorlog(hp_elog);
624 return rc ? rc : count;
627 static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
629 static int __init pseries_dlpar_init(void)
633 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
634 ppc_md.cpu_probe = dlpar_cpu_probe;
635 ppc_md.cpu_release = dlpar_cpu_release;
636 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
638 rc = sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
642 machine_device_initcall(pseries, pseries_dlpar_init);