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"
22 #include <asm/machdep.h>
23 #include <asm/uaccess.h>
34 void dlpar_free_cc_property(struct property *prop)
41 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
43 struct property *prop;
47 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
51 name = (char *)ccwa + ccwa->name_offset;
52 prop->name = kstrdup(name, GFP_KERNEL);
54 prop->length = ccwa->prop_length;
55 value = (char *)ccwa + ccwa->prop_offset;
56 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
58 dlpar_free_cc_property(prop);
65 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
67 struct device_node *dn;
70 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
74 /* The configure connector reported name does not contain a
75 * preceding '/', so we allocate a buffer large enough to
76 * prepend this to the full_name.
78 name = (char *)ccwa + ccwa->name_offset;
79 dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
85 of_node_set_flag(dn, OF_DYNAMIC);
90 static void dlpar_free_one_cc_node(struct device_node *dn)
92 struct property *prop;
94 while (dn->properties) {
95 prop = dn->properties;
96 dn->properties = prop->next;
97 dlpar_free_cc_property(prop);
100 kfree(dn->full_name);
104 void dlpar_free_cc_nodes(struct device_node *dn)
107 dlpar_free_cc_nodes(dn->child);
110 dlpar_free_cc_nodes(dn->sibling);
112 dlpar_free_one_cc_node(dn);
116 #define NEXT_SIBLING 1
118 #define NEXT_PROPERTY 3
119 #define PREV_PARENT 4
120 #define MORE_MEMORY 5
121 #define CALL_AGAIN -2
122 #define ERR_CFG_USE -9003
124 struct device_node *dlpar_configure_connector(u32 drc_index)
126 struct device_node *dn;
127 struct device_node *first_dn = NULL;
128 struct device_node *last_dn = NULL;
129 struct property *property;
130 struct property *last_property = NULL;
131 struct cc_workarea *ccwa;
136 cc_token = rtas_token("ibm,configure-connector");
137 if (cc_token == RTAS_UNKNOWN_SERVICE)
140 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
144 ccwa = (struct cc_workarea *)&data_buf[0];
145 ccwa->drc_index = drc_index;
149 /* Since we release the rtas_data_buf lock between configure
150 * connector calls we want to re-populate the rtas_data_buffer
151 * with the contents of the previous call.
153 spin_lock(&rtas_data_buf_lock);
155 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
156 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
157 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
159 spin_unlock(&rtas_data_buf_lock);
166 dn = dlpar_parse_cc_node(ccwa);
170 dn->parent = last_dn->parent;
171 last_dn->sibling = dn;
176 dn = dlpar_parse_cc_node(ccwa);
183 dn->parent = last_dn;
192 property = dlpar_parse_cc_property(ccwa);
196 if (!last_dn->properties)
197 last_dn->properties = property;
199 last_property->next = property;
201 last_property = property;
205 last_dn = last_dn->parent;
214 printk(KERN_ERR "Unexpected Error (%d) "
215 "returned from configure-connector\n", rc);
225 dlpar_free_cc_nodes(first_dn);
233 static struct device_node *derive_parent(const char *path)
235 struct device_node *parent;
238 last_slash = strrchr(path, '/');
239 if (last_slash == path) {
240 parent = of_find_node_by_path("/");
243 int parent_path_len = last_slash - path + 1;
244 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
248 strlcpy(parent_path, path, parent_path_len);
249 parent = of_find_node_by_path(parent_path);
256 int dlpar_attach_node(struct device_node *dn)
260 of_node_set_flag(dn, OF_DYNAMIC);
261 kref_init(&dn->kref);
262 dn->parent = derive_parent(dn->full_name);
266 rc = of_attach_node(dn);
268 printk(KERN_ERR "Failed to add device node %s\n",
273 of_node_put(dn->parent);
277 int dlpar_detach_node(struct device_node *dn)
281 rc = of_detach_node(dn);
285 of_node_put(dn); /* Must decrement the refcount */
289 #define DR_ENTITY_SENSE 9003
290 #define DR_ENTITY_PRESENT 1
291 #define DR_ENTITY_UNUSABLE 2
292 #define ALLOCATION_STATE 9003
293 #define ALLOC_UNUSABLE 0
294 #define ALLOC_USABLE 1
295 #define ISOLATION_STATE 9001
299 int dlpar_acquire_drc(u32 drc_index)
303 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
304 DR_ENTITY_SENSE, drc_index);
305 if (rc || dr_status != DR_ENTITY_UNUSABLE)
308 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
312 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
314 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
321 int dlpar_release_drc(u32 drc_index)
325 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
326 DR_ENTITY_SENSE, drc_index);
327 if (rc || dr_status != DR_ENTITY_PRESENT)
330 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
334 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
336 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
343 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
345 static int dlpar_online_cpu(struct device_node *dn)
349 int len, nthreads, i;
352 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
356 nthreads = len / sizeof(u32);
358 cpu_maps_update_begin();
359 for (i = 0; i < nthreads; i++) {
360 for_each_present_cpu(cpu) {
361 if (get_hard_smp_processor_id(cpu) != intserv[i])
363 BUG_ON(get_cpu_current_state(cpu)
364 != CPU_STATE_OFFLINE);
365 cpu_maps_update_done();
369 cpu_maps_update_begin();
373 if (cpu == num_possible_cpus())
374 printk(KERN_WARNING "Could not find cpu to online "
375 "with physical id 0x%x\n", intserv[i]);
377 cpu_maps_update_done();
384 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
386 struct device_node *dn;
387 unsigned long drc_index;
391 cpu_hotplug_driver_lock();
392 rc = strict_strtoul(buf, 0, &drc_index);
398 dn = dlpar_configure_connector(drc_index);
404 /* configure-connector reports cpus as living in the base
405 * directory of the device tree. CPUs actually live in the
406 * cpus directory so we need to fixup the full_name.
408 cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
410 dlpar_free_cc_nodes(dn);
415 kfree(dn->full_name);
416 dn->full_name = cpu_name;
418 rc = dlpar_acquire_drc(drc_index);
420 dlpar_free_cc_nodes(dn);
425 rc = dlpar_attach_node(dn);
427 dlpar_release_drc(drc_index);
428 dlpar_free_cc_nodes(dn);
432 rc = dlpar_online_cpu(dn);
434 cpu_hotplug_driver_unlock();
436 return rc ? rc : count;
439 static int dlpar_offline_cpu(struct device_node *dn)
443 int len, nthreads, i;
446 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
450 nthreads = len / sizeof(u32);
452 cpu_maps_update_begin();
453 for (i = 0; i < nthreads; i++) {
454 for_each_present_cpu(cpu) {
455 if (get_hard_smp_processor_id(cpu) != intserv[i])
458 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
461 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
462 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
463 cpu_maps_update_done();
467 cpu_maps_update_begin();
473 * The cpu is in CPU_STATE_INACTIVE.
474 * Upgrade it's state to CPU_STATE_OFFLINE.
476 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
477 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
482 if (cpu == num_possible_cpus())
483 printk(KERN_WARNING "Could not find cpu to offline "
484 "with physical id 0x%x\n", intserv[i]);
486 cpu_maps_update_done();
493 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
495 struct device_node *dn;
496 const u32 *drc_index;
499 dn = of_find_node_by_path(buf);
503 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
509 cpu_hotplug_driver_lock();
510 rc = dlpar_offline_cpu(dn);
517 rc = dlpar_release_drc(*drc_index);
523 rc = dlpar_detach_node(dn);
525 dlpar_acquire_drc(*drc_index);
531 cpu_hotplug_driver_unlock();
532 return rc ? rc : count;
535 static int __init pseries_dlpar_init(void)
537 ppc_md.cpu_probe = dlpar_cpu_probe;
538 ppc_md.cpu_release = dlpar_cpu_release;
542 machine_device_initcall(pseries, pseries_dlpar_init);
544 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */