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 #include <linux/kernel.h>
14 #include <linux/notifier.h>
15 #include <linux/spinlock.h>
16 #include <linux/cpu.h>
17 #include <linux/slab.h>
19 #include "offline_states.h"
23 #include <asm/machdep.h>
24 #include <asm/uaccess.h>
35 void dlpar_free_cc_property(struct property *prop)
42 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
44 struct property *prop;
48 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
52 name = (char *)ccwa + ccwa->name_offset;
53 prop->name = kstrdup(name, GFP_KERNEL);
55 prop->length = ccwa->prop_length;
56 value = (char *)ccwa + ccwa->prop_offset;
57 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
59 dlpar_free_cc_property(prop);
66 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
69 struct device_node *dn;
72 /* If parent node path is "/" advance path to NULL terminator to
73 * prevent double leading slashs in full_name.
78 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
82 name = (char *)ccwa + ccwa->name_offset;
83 dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
89 of_node_set_flag(dn, OF_DYNAMIC);
95 static void dlpar_free_one_cc_node(struct device_node *dn)
97 struct property *prop;
99 while (dn->properties) {
100 prop = dn->properties;
101 dn->properties = prop->next;
102 dlpar_free_cc_property(prop);
105 kfree(dn->full_name);
109 void dlpar_free_cc_nodes(struct device_node *dn)
112 dlpar_free_cc_nodes(dn->child);
115 dlpar_free_cc_nodes(dn->sibling);
117 dlpar_free_one_cc_node(dn);
121 #define NEXT_SIBLING 1
123 #define NEXT_PROPERTY 3
124 #define PREV_PARENT 4
125 #define MORE_MEMORY 5
126 #define CALL_AGAIN -2
127 #define ERR_CFG_USE -9003
129 struct device_node *dlpar_configure_connector(u32 drc_index,
130 struct device_node *parent)
132 struct device_node *dn;
133 struct device_node *first_dn = NULL;
134 struct device_node *last_dn = NULL;
135 struct property *property;
136 struct property *last_property = NULL;
137 struct cc_workarea *ccwa;
139 const char *parent_path = parent->full_name;
143 cc_token = rtas_token("ibm,configure-connector");
144 if (cc_token == RTAS_UNKNOWN_SERVICE)
147 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
151 ccwa = (struct cc_workarea *)&data_buf[0];
152 ccwa->drc_index = drc_index;
156 /* Since we release the rtas_data_buf lock between configure
157 * connector calls we want to re-populate the rtas_data_buffer
158 * with the contents of the previous call.
160 spin_lock(&rtas_data_buf_lock);
162 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
163 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
164 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
166 spin_unlock(&rtas_data_buf_lock);
173 dn = dlpar_parse_cc_node(ccwa, parent_path);
177 dn->parent = last_dn->parent;
178 last_dn->sibling = dn;
184 parent_path = last_dn->full_name;
186 dn = dlpar_parse_cc_node(ccwa, parent_path);
194 dn->parent = last_dn;
203 property = dlpar_parse_cc_property(ccwa);
207 if (!last_dn->properties)
208 last_dn->properties = property;
210 last_property->next = property;
212 last_property = property;
216 last_dn = last_dn->parent;
217 parent_path = last_dn->parent->full_name;
226 printk(KERN_ERR "Unexpected Error (%d) "
227 "returned from configure-connector\n", rc);
237 dlpar_free_cc_nodes(first_dn);
245 static struct device_node *derive_parent(const char *path)
247 struct device_node *parent;
250 last_slash = strrchr(path, '/');
251 if (last_slash == path) {
252 parent = of_find_node_by_path("/");
255 int parent_path_len = last_slash - path + 1;
256 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
260 strlcpy(parent_path, path, parent_path_len);
261 parent = of_find_node_by_path(parent_path);
268 int dlpar_attach_node(struct device_node *dn)
272 dn->parent = derive_parent(dn->full_name);
276 rc = of_attach_node(dn);
278 printk(KERN_ERR "Failed to add device node %s\n",
283 of_node_put(dn->parent);
287 int dlpar_detach_node(struct device_node *dn)
289 struct device_node *child;
292 child = of_get_next_child(dn, NULL);
294 dlpar_detach_node(child);
295 child = of_get_next_child(dn, child);
298 rc = of_detach_node(dn);
302 of_node_put(dn); /* Must decrement the refcount */
306 #define DR_ENTITY_SENSE 9003
307 #define DR_ENTITY_PRESENT 1
308 #define DR_ENTITY_UNUSABLE 2
309 #define ALLOCATION_STATE 9003
310 #define ALLOC_UNUSABLE 0
311 #define ALLOC_USABLE 1
312 #define ISOLATION_STATE 9001
316 int dlpar_acquire_drc(u32 drc_index)
320 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
321 DR_ENTITY_SENSE, drc_index);
322 if (rc || dr_status != DR_ENTITY_UNUSABLE)
325 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
329 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
331 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
338 int dlpar_release_drc(u32 drc_index)
342 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
343 DR_ENTITY_SENSE, drc_index);
344 if (rc || dr_status != DR_ENTITY_PRESENT)
347 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
351 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
353 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
360 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
362 static int dlpar_online_cpu(struct device_node *dn)
366 int len, nthreads, i;
367 const __be32 *intserv;
370 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
374 nthreads = len / sizeof(u32);
376 cpu_maps_update_begin();
377 for (i = 0; i < nthreads; i++) {
378 thread = be32_to_cpu(intserv[i]);
379 for_each_present_cpu(cpu) {
380 if (get_hard_smp_processor_id(cpu) != thread)
382 BUG_ON(get_cpu_current_state(cpu)
383 != CPU_STATE_OFFLINE);
384 cpu_maps_update_done();
388 cpu_maps_update_begin();
392 if (cpu == num_possible_cpus())
393 printk(KERN_WARNING "Could not find cpu to online "
394 "with physical id 0x%x\n", thread);
396 cpu_maps_update_done();
403 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
405 struct device_node *dn, *parent;
409 rc = kstrtou32(buf, 0, &drc_index);
413 parent = of_find_node_by_path("/cpus");
417 dn = dlpar_configure_connector(drc_index, parent);
423 rc = dlpar_acquire_drc(drc_index);
425 dlpar_free_cc_nodes(dn);
429 rc = dlpar_attach_node(dn);
431 dlpar_release_drc(drc_index);
432 dlpar_free_cc_nodes(dn);
436 rc = dlpar_online_cpu(dn);
443 static int dlpar_offline_cpu(struct device_node *dn)
447 int len, nthreads, i;
450 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
454 nthreads = len / sizeof(u32);
456 cpu_maps_update_begin();
457 for (i = 0; i < nthreads; i++) {
458 for_each_present_cpu(cpu) {
459 if (get_hard_smp_processor_id(cpu) != intserv[i])
462 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
465 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
466 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
467 cpu_maps_update_done();
471 cpu_maps_update_begin();
477 * The cpu is in CPU_STATE_INACTIVE.
478 * Upgrade it's state to CPU_STATE_OFFLINE.
480 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
481 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
486 if (cpu == num_possible_cpus())
487 printk(KERN_WARNING "Could not find cpu to offline "
488 "with physical id 0x%x\n", intserv[i]);
490 cpu_maps_update_done();
497 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
499 struct device_node *dn;
500 const u32 *drc_index;
503 dn = of_find_node_by_path(buf);
507 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
513 rc = dlpar_offline_cpu(dn);
519 rc = dlpar_release_drc(*drc_index);
525 rc = dlpar_detach_node(dn);
527 dlpar_acquire_drc(*drc_index);
536 static int __init pseries_dlpar_init(void)
538 ppc_md.cpu_probe = dlpar_cpu_probe;
539 ppc_md.cpu_release = dlpar_cpu_release;
543 machine_device_initcall(pseries, pseries_dlpar_init);
545 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */