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 "offline_states.h"
22 #include <asm/machdep.h>
23 #include <asm/uaccess.h>
25 #include <asm/pSeries_reconfig.h>
35 static 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
= kzalloc(prop
->length
, GFP_KERNEL
);
59 dlpar_free_cc_property(prop
);
63 memcpy(prop
->value
, value
, prop
->length
);
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
= kmalloc(strlen(name
) + 2, GFP_KERNEL
);
87 sprintf(dn
->full_name
, "/%s", name
);
91 static void dlpar_free_one_cc_node(struct device_node
*dn
)
93 struct property
*prop
;
95 while (dn
->properties
) {
96 prop
= dn
->properties
;
97 dn
->properties
= prop
->next
;
98 dlpar_free_cc_property(prop
);
101 kfree(dn
->full_name
);
105 static void dlpar_free_cc_nodes(struct device_node
*dn
)
108 dlpar_free_cc_nodes(dn
->child
);
111 dlpar_free_cc_nodes(dn
->sibling
);
113 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
;
135 cc_token
= rtas_token("ibm,configure-connector");
136 if (cc_token
== RTAS_UNKNOWN_SERVICE
)
139 spin_lock(&rtas_data_buf_lock
);
140 ccwa
= (struct cc_workarea
*)&rtas_data_buf
[0];
141 ccwa
->drc_index
= drc_index
;
144 rc
= rtas_call(cc_token
, 2, 1, NULL
, rtas_data_buf
, NULL
);
148 dn
= dlpar_parse_cc_node(ccwa
);
152 dn
->parent
= last_dn
->parent
;
153 last_dn
->sibling
= dn
;
158 dn
= dlpar_parse_cc_node(ccwa
);
165 dn
->parent
= last_dn
;
174 property
= dlpar_parse_cc_property(ccwa
);
178 if (!last_dn
->properties
)
179 last_dn
->properties
= property
;
181 last_property
->next
= property
;
183 last_property
= property
;
187 last_dn
= last_dn
->parent
;
196 printk(KERN_ERR
"Unexpected Error (%d) "
197 "returned from configure-connector\n", rc
);
201 rc
= rtas_call(cc_token
, 2, 1, NULL
, rtas_data_buf
, NULL
);
204 spin_unlock(&rtas_data_buf_lock
);
209 dlpar_free_cc_nodes(first_dn
);
210 spin_unlock(&rtas_data_buf_lock
);
214 static struct device_node
*derive_parent(const char *path
)
216 struct device_node
*parent
;
219 last_slash
= strrchr(path
, '/');
220 if (last_slash
== path
) {
221 parent
= of_find_node_by_path("/");
224 int parent_path_len
= last_slash
- path
+ 1;
225 parent_path
= kmalloc(parent_path_len
, GFP_KERNEL
);
229 strlcpy(parent_path
, path
, parent_path_len
);
230 parent
= of_find_node_by_path(parent_path
);
237 int dlpar_attach_node(struct device_node
*dn
)
239 struct proc_dir_entry
*ent
;
242 of_node_set_flag(dn
, OF_DYNAMIC
);
243 kref_init(&dn
->kref
);
244 dn
->parent
= derive_parent(dn
->full_name
);
248 rc
= blocking_notifier_call_chain(&pSeries_reconfig_chain
,
249 PSERIES_RECONFIG_ADD
, dn
);
250 if (rc
== NOTIFY_BAD
) {
251 printk(KERN_ERR
"Failed to add device node %s\n",
253 return -ENOMEM
; /* For now, safe to assume kmalloc failure */
258 #ifdef CONFIG_PROC_DEVICETREE
259 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
261 proc_device_tree_add_node(dn
, ent
);
264 of_node_put(dn
->parent
);
268 int dlpar_detach_node(struct device_node
*dn
)
270 struct device_node
*parent
= dn
->parent
;
271 struct property
*prop
= dn
->properties
;
273 #ifdef CONFIG_PROC_DEVICETREE
275 remove_proc_entry(prop
->name
, dn
->pde
);
280 remove_proc_entry(dn
->pde
->name
, parent
->pde
);
283 blocking_notifier_call_chain(&pSeries_reconfig_chain
,
284 PSERIES_RECONFIG_REMOVE
, dn
);
286 of_node_put(dn
); /* Must decrement the refcount */
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 DEFINE_MUTEX(pseries_cpu_hotplug_mutex
);
349 void cpu_hotplug_driver_lock(void)
350 __acquires(pseries_cpu_hotplug_mutex
)
352 mutex_lock(&pseries_cpu_hotplug_mutex
);
355 void cpu_hotplug_driver_unlock(void)
356 __releases(pseries_cpu_hotplug_mutex
)
358 mutex_unlock(&pseries_cpu_hotplug_mutex
);
361 static int dlpar_online_cpu(struct device_node
*dn
)
365 int len
, nthreads
, i
;
368 intserv
= of_get_property(dn
, "ibm,ppc-interrupt-server#s", &len
);
372 nthreads
= len
/ sizeof(u32
);
374 cpu_maps_update_begin();
375 for (i
= 0; i
< nthreads
; i
++) {
376 for_each_present_cpu(cpu
) {
377 if (get_hard_smp_processor_id(cpu
) != intserv
[i
])
379 BUG_ON(get_cpu_current_state(cpu
)
380 != CPU_STATE_OFFLINE
);
381 cpu_maps_update_done();
385 cpu_maps_update_begin();
389 if (cpu
== num_possible_cpus())
390 printk(KERN_WARNING
"Could not find cpu to online "
391 "with physical id 0x%x\n", intserv
[i
]);
393 cpu_maps_update_done();
400 static ssize_t
dlpar_cpu_probe(const char *buf
, size_t count
)
402 struct device_node
*dn
;
403 unsigned long drc_index
;
407 cpu_hotplug_driver_lock();
408 rc
= strict_strtoul(buf
, 0, &drc_index
);
414 dn
= dlpar_configure_connector(drc_index
);
420 /* configure-connector reports cpus as living in the base
421 * directory of the device tree. CPUs actually live in the
422 * cpus directory so we need to fixup the full_name.
424 cpu_name
= kzalloc(strlen(dn
->full_name
) + strlen("/cpus") + 1,
427 dlpar_free_cc_nodes(dn
);
432 sprintf(cpu_name
, "/cpus%s", dn
->full_name
);
433 kfree(dn
->full_name
);
434 dn
->full_name
= cpu_name
;
436 rc
= dlpar_acquire_drc(drc_index
);
438 dlpar_free_cc_nodes(dn
);
443 rc
= dlpar_attach_node(dn
);
445 dlpar_release_drc(drc_index
);
446 dlpar_free_cc_nodes(dn
);
449 rc
= dlpar_online_cpu(dn
);
451 cpu_hotplug_driver_unlock();
453 return rc
? rc
: count
;
456 static int dlpar_offline_cpu(struct device_node
*dn
)
460 int len
, nthreads
, i
;
463 intserv
= of_get_property(dn
, "ibm,ppc-interrupt-server#s", &len
);
467 nthreads
= len
/ sizeof(u32
);
469 cpu_maps_update_begin();
470 for (i
= 0; i
< nthreads
; i
++) {
471 for_each_present_cpu(cpu
) {
472 if (get_hard_smp_processor_id(cpu
) != intserv
[i
])
475 if (get_cpu_current_state(cpu
) == CPU_STATE_OFFLINE
)
478 if (get_cpu_current_state(cpu
) == CPU_STATE_ONLINE
) {
479 cpu_maps_update_done();
483 cpu_maps_update_begin();
489 * The cpu is in CPU_STATE_INACTIVE.
490 * Upgrade it's state to CPU_STATE_OFFLINE.
492 set_preferred_offline_state(cpu
, CPU_STATE_OFFLINE
);
493 BUG_ON(plpar_hcall_norets(H_PROD
, intserv
[i
])
498 if (cpu
== num_possible_cpus())
499 printk(KERN_WARNING
"Could not find cpu to offline "
500 "with physical id 0x%x\n", intserv
[i
]);
502 cpu_maps_update_done();
509 static ssize_t
dlpar_cpu_release(const char *buf
, size_t count
)
511 struct device_node
*dn
;
512 const u32
*drc_index
;
515 dn
= of_find_node_by_path(buf
);
519 drc_index
= of_get_property(dn
, "ibm,my-drc-index", NULL
);
525 cpu_hotplug_driver_lock();
526 rc
= dlpar_offline_cpu(dn
);
533 rc
= dlpar_release_drc(*drc_index
);
539 rc
= dlpar_detach_node(dn
);
541 dlpar_acquire_drc(*drc_index
);
547 cpu_hotplug_driver_unlock();
548 return rc
? rc
: count
;
551 static int __init
pseries_dlpar_init(void)
553 ppc_md
.cpu_probe
= dlpar_cpu_probe
;
554 ppc_md
.cpu_release
= dlpar_cpu_release
;
558 machine_device_initcall(pseries
, pseries_dlpar_init
);
560 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */