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/proc_fs.h>
17 #include <linux/spinlock.h>
18 #include <linux/cpu.h>
19 #include <linux/slab.h>
20 #include "offline_states.h"
23 #include <asm/machdep.h>
24 #include <asm/uaccess.h>
26 #include <asm/pSeries_reconfig.h>
36 void dlpar_free_cc_property(struct property *prop)
43 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
45 struct property *prop;
49 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
53 name = (char *)ccwa + ccwa->name_offset;
54 prop->name = kstrdup(name, GFP_KERNEL);
56 prop->length = ccwa->prop_length;
57 value = (char *)ccwa + ccwa->prop_offset;
58 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
60 dlpar_free_cc_property(prop);
67 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
69 struct device_node *dn;
72 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
76 /* The configure connector reported name does not contain a
77 * preceeding '/', so we allocate a buffer large enough to
78 * prepend this to the full_name.
80 name = (char *)ccwa + ccwa->name_offset;
81 dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
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);
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);
162 dn = dlpar_parse_cc_node(ccwa);
166 dn->parent = last_dn->parent;
167 last_dn->sibling = dn;
172 dn = dlpar_parse_cc_node(ccwa);
179 dn->parent = last_dn;
188 property = dlpar_parse_cc_property(ccwa);
192 if (!last_dn->properties)
193 last_dn->properties = property;
195 last_property->next = property;
197 last_property = property;
201 last_dn = last_dn->parent;
210 printk(KERN_ERR "Unexpected Error (%d) "
211 "returned from configure-connector\n", rc);
221 dlpar_free_cc_nodes(first_dn);
229 static struct device_node *derive_parent(const char *path)
231 struct device_node *parent;
234 last_slash = strrchr(path, '/');
235 if (last_slash == path) {
236 parent = of_find_node_by_path("/");
239 int parent_path_len = last_slash - path + 1;
240 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
244 strlcpy(parent_path, path, parent_path_len);
245 parent = of_find_node_by_path(parent_path);
252 int dlpar_attach_node(struct device_node *dn)
254 #ifdef CONFIG_PROC_DEVICETREE
255 struct proc_dir_entry *ent;
259 of_node_set_flag(dn, OF_DYNAMIC);
260 kref_init(&dn->kref);
261 dn->parent = derive_parent(dn->full_name);
265 rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
266 PSERIES_RECONFIG_ADD, dn);
267 if (rc == NOTIFY_BAD) {
268 printk(KERN_ERR "Failed to add device node %s\n",
270 return -ENOMEM; /* For now, safe to assume kmalloc failure */
275 #ifdef CONFIG_PROC_DEVICETREE
276 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
278 proc_device_tree_add_node(dn, ent);
281 of_node_put(dn->parent);
285 int dlpar_detach_node(struct device_node *dn)
287 #ifdef CONFIG_PROC_DEVICETREE
288 struct device_node *parent = dn->parent;
289 struct property *prop = dn->properties;
292 remove_proc_entry(prop->name, dn->pde);
297 remove_proc_entry(dn->pde->name, parent->pde);
300 blocking_notifier_call_chain(&pSeries_reconfig_chain,
301 PSERIES_RECONFIG_REMOVE, dn);
303 of_node_put(dn); /* Must decrement the refcount */
308 #define DR_ENTITY_SENSE 9003
309 #define DR_ENTITY_PRESENT 1
310 #define DR_ENTITY_UNUSABLE 2
311 #define ALLOCATION_STATE 9003
312 #define ALLOC_UNUSABLE 0
313 #define ALLOC_USABLE 1
314 #define ISOLATION_STATE 9001
318 int dlpar_acquire_drc(u32 drc_index)
322 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
323 DR_ENTITY_SENSE, drc_index);
324 if (rc || dr_status != DR_ENTITY_UNUSABLE)
327 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
331 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
333 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
340 int dlpar_release_drc(u32 drc_index)
344 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
345 DR_ENTITY_SENSE, drc_index);
346 if (rc || dr_status != DR_ENTITY_PRESENT)
349 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
353 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
355 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
362 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
364 static int dlpar_online_cpu(struct device_node *dn)
368 int len, nthreads, i;
371 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
375 nthreads = len / sizeof(u32);
377 cpu_maps_update_begin();
378 for (i = 0; i < nthreads; i++) {
379 for_each_present_cpu(cpu) {
380 if (get_hard_smp_processor_id(cpu) != intserv[i])
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", intserv[i]);
396 cpu_maps_update_done();
403 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
405 struct device_node *dn;
406 unsigned long drc_index;
410 cpu_hotplug_driver_lock();
411 rc = strict_strtoul(buf, 0, &drc_index);
417 dn = dlpar_configure_connector(drc_index);
423 /* configure-connector reports cpus as living in the base
424 * directory of the device tree. CPUs actually live in the
425 * cpus directory so we need to fixup the full_name.
427 cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
429 dlpar_free_cc_nodes(dn);
434 kfree(dn->full_name);
435 dn->full_name = cpu_name;
437 rc = dlpar_acquire_drc(drc_index);
439 dlpar_free_cc_nodes(dn);
444 rc = dlpar_attach_node(dn);
446 dlpar_release_drc(drc_index);
447 dlpar_free_cc_nodes(dn);
451 rc = dlpar_online_cpu(dn);
453 cpu_hotplug_driver_unlock();
455 return rc ? rc : count;
458 static int dlpar_offline_cpu(struct device_node *dn)
462 int len, nthreads, i;
465 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
469 nthreads = len / sizeof(u32);
471 cpu_maps_update_begin();
472 for (i = 0; i < nthreads; i++) {
473 for_each_present_cpu(cpu) {
474 if (get_hard_smp_processor_id(cpu) != intserv[i])
477 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
480 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
481 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
482 cpu_maps_update_done();
486 cpu_maps_update_begin();
492 * The cpu is in CPU_STATE_INACTIVE.
493 * Upgrade it's state to CPU_STATE_OFFLINE.
495 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
496 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
501 if (cpu == num_possible_cpus())
502 printk(KERN_WARNING "Could not find cpu to offline "
503 "with physical id 0x%x\n", intserv[i]);
505 cpu_maps_update_done();
512 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
514 struct device_node *dn;
515 const u32 *drc_index;
518 dn = of_find_node_by_path(buf);
522 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
528 cpu_hotplug_driver_lock();
529 rc = dlpar_offline_cpu(dn);
536 rc = dlpar_release_drc(*drc_index);
542 rc = dlpar_detach_node(dn);
544 dlpar_acquire_drc(*drc_index);
550 cpu_hotplug_driver_unlock();
551 return rc ? rc : count;
554 static int __init pseries_dlpar_init(void)
556 ppc_md.cpu_probe = dlpar_cpu_probe;
557 ppc_md.cpu_release = dlpar_cpu_release;
561 machine_device_initcall(pseries, pseries_dlpar_init);
563 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */