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/kref.h>
15 #include <linux/notifier.h>
16 #include <linux/spinlock.h>
17 #include <linux/cpu.h>
18 #include <linux/slab.h>
20 #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)
68 struct device_node *dn;
71 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
75 /* The configure connector reported name does not contain a
76 * preceding '/', so we allocate a buffer large enough to
77 * prepend this to the full_name.
79 name = (char *)ccwa + ccwa->name_offset;
80 dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
89 static void dlpar_free_one_cc_node(struct device_node *dn)
91 struct property *prop;
93 while (dn->properties) {
94 prop = dn->properties;
95 dn->properties = prop->next;
96 dlpar_free_cc_property(prop);
103 void dlpar_free_cc_nodes(struct device_node *dn)
106 dlpar_free_cc_nodes(dn->child);
109 dlpar_free_cc_nodes(dn->sibling);
111 dlpar_free_one_cc_node(dn);
115 #define NEXT_SIBLING 1
117 #define NEXT_PROPERTY 3
118 #define PREV_PARENT 4
119 #define MORE_MEMORY 5
120 #define CALL_AGAIN -2
121 #define ERR_CFG_USE -9003
123 struct device_node *dlpar_configure_connector(u32 drc_index)
125 struct device_node *dn;
126 struct device_node *first_dn = NULL;
127 struct device_node *last_dn = NULL;
128 struct property *property;
129 struct property *last_property = NULL;
130 struct cc_workarea *ccwa;
135 cc_token = rtas_token("ibm,configure-connector");
136 if (cc_token == RTAS_UNKNOWN_SERVICE)
139 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
143 ccwa = (struct cc_workarea *)&data_buf[0];
144 ccwa->drc_index = drc_index;
148 /* Since we release the rtas_data_buf lock between configure
149 * connector calls we want to re-populate the rtas_data_buffer
150 * with the contents of the previous call.
152 spin_lock(&rtas_data_buf_lock);
154 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
155 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
156 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
158 spin_unlock(&rtas_data_buf_lock);
165 dn = dlpar_parse_cc_node(ccwa);
169 dn->parent = last_dn->parent;
170 last_dn->sibling = dn;
175 dn = dlpar_parse_cc_node(ccwa);
182 dn->parent = last_dn;
191 property = dlpar_parse_cc_property(ccwa);
195 if (!last_dn->properties)
196 last_dn->properties = property;
198 last_property->next = property;
200 last_property = property;
204 last_dn = last_dn->parent;
213 printk(KERN_ERR "Unexpected Error (%d) "
214 "returned from configure-connector\n", rc);
224 dlpar_free_cc_nodes(first_dn);
232 static struct device_node *derive_parent(const char *path)
234 struct device_node *parent;
237 last_slash = strrchr(path, '/');
238 if (last_slash == path) {
239 parent = of_find_node_by_path("/");
242 int parent_path_len = last_slash - path + 1;
243 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
247 strlcpy(parent_path, path, parent_path_len);
248 parent = of_find_node_by_path(parent_path);
255 int dlpar_attach_node(struct device_node *dn)
259 of_node_set_flag(dn, OF_DYNAMIC);
260 kref_init(&dn->kref);
261 dn->parent = derive_parent(dn->full_name);
265 rc = of_attach_node(dn);
267 printk(KERN_ERR "Failed to add device node %s\n",
272 of_node_put(dn->parent);
276 int dlpar_detach_node(struct device_node *dn)
280 rc = of_detach_node(dn);
284 of_node_put(dn); /* Must decrement the refcount */
288 #define DR_ENTITY_SENSE 9003
289 #define DR_ENTITY_PRESENT 1
290 #define DR_ENTITY_UNUSABLE 2
291 #define ALLOCATION_STATE 9003
292 #define ALLOC_UNUSABLE 0
293 #define ALLOC_USABLE 1
294 #define ISOLATION_STATE 9001
298 int dlpar_acquire_drc(u32 drc_index)
302 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
303 DR_ENTITY_SENSE, drc_index);
304 if (rc || dr_status != DR_ENTITY_UNUSABLE)
307 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
311 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
313 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
320 int dlpar_release_drc(u32 drc_index)
324 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
325 DR_ENTITY_SENSE, drc_index);
326 if (rc || dr_status != DR_ENTITY_PRESENT)
329 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
333 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
335 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
342 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
344 static int dlpar_online_cpu(struct device_node *dn)
348 int len, nthreads, i;
351 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
355 nthreads = len / sizeof(u32);
357 cpu_maps_update_begin();
358 for (i = 0; i < nthreads; i++) {
359 for_each_present_cpu(cpu) {
360 if (get_hard_smp_processor_id(cpu) != intserv[i])
362 BUG_ON(get_cpu_current_state(cpu)
363 != CPU_STATE_OFFLINE);
364 cpu_maps_update_done();
368 cpu_maps_update_begin();
372 if (cpu == num_possible_cpus())
373 printk(KERN_WARNING "Could not find cpu to online "
374 "with physical id 0x%x\n", intserv[i]);
376 cpu_maps_update_done();
383 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
385 struct device_node *dn;
386 unsigned long drc_index;
390 cpu_hotplug_driver_lock();
391 rc = strict_strtoul(buf, 0, &drc_index);
397 dn = dlpar_configure_connector(drc_index);
403 /* configure-connector reports cpus as living in the base
404 * directory of the device tree. CPUs actually live in the
405 * cpus directory so we need to fixup the full_name.
407 cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
409 dlpar_free_cc_nodes(dn);
414 kfree(dn->full_name);
415 dn->full_name = cpu_name;
417 rc = dlpar_acquire_drc(drc_index);
419 dlpar_free_cc_nodes(dn);
424 rc = dlpar_attach_node(dn);
426 dlpar_release_drc(drc_index);
427 dlpar_free_cc_nodes(dn);
431 rc = dlpar_online_cpu(dn);
433 cpu_hotplug_driver_unlock();
435 return rc ? rc : count;
438 static int dlpar_offline_cpu(struct device_node *dn)
442 int len, nthreads, i;
445 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
449 nthreads = len / sizeof(u32);
451 cpu_maps_update_begin();
452 for (i = 0; i < nthreads; i++) {
453 for_each_present_cpu(cpu) {
454 if (get_hard_smp_processor_id(cpu) != intserv[i])
457 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
460 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
461 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
462 cpu_maps_update_done();
466 cpu_maps_update_begin();
472 * The cpu is in CPU_STATE_INACTIVE.
473 * Upgrade it's state to CPU_STATE_OFFLINE.
475 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
476 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
481 if (cpu == num_possible_cpus())
482 printk(KERN_WARNING "Could not find cpu to offline "
483 "with physical id 0x%x\n", intserv[i]);
485 cpu_maps_update_done();
492 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
494 struct device_node *dn;
495 const u32 *drc_index;
498 dn = of_find_node_by_path(buf);
502 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
508 cpu_hotplug_driver_lock();
509 rc = dlpar_offline_cpu(dn);
516 rc = dlpar_release_drc(*drc_index);
522 rc = dlpar_detach_node(dn);
524 dlpar_acquire_drc(*drc_index);
530 cpu_hotplug_driver_unlock();
531 return rc ? rc : count;
534 static int __init pseries_dlpar_init(void)
536 ppc_md.cpu_probe = dlpar_cpu_probe;
537 ppc_md.cpu_release = dlpar_cpu_release;
541 machine_device_initcall(pseries, pseries_dlpar_init);
543 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */