]>
git.ipfire.org Git - people/arne_f/kernel.git/blob - arch/powerpc/platforms/pseries/dlpar.c
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 static 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
= kzalloc(prop
->length
, GFP_KERNEL
);
60 dlpar_free_cc_property(prop
);
64 memcpy(prop
->value
, value
, prop
->length
);
68 static struct device_node
*dlpar_parse_cc_node(struct cc_workarea
*ccwa
)
70 struct device_node
*dn
;
73 dn
= kzalloc(sizeof(*dn
), GFP_KERNEL
);
77 /* The configure connector reported name does not contain a
78 * preceeding '/', so we allocate a buffer large enough to
79 * prepend this to the full_name.
81 name
= (char *)ccwa
+ ccwa
->name_offset
;
82 dn
->full_name
= kmalloc(strlen(name
) + 2, GFP_KERNEL
);
88 sprintf(dn
->full_name
, "/%s", name
);
92 static void dlpar_free_one_cc_node(struct device_node
*dn
)
94 struct property
*prop
;
96 while (dn
->properties
) {
97 prop
= dn
->properties
;
98 dn
->properties
= prop
->next
;
99 dlpar_free_cc_property(prop
);
102 kfree(dn
->full_name
);
106 static void dlpar_free_cc_nodes(struct device_node
*dn
)
109 dlpar_free_cc_nodes(dn
->child
);
112 dlpar_free_cc_nodes(dn
->sibling
);
114 dlpar_free_one_cc_node(dn
);
117 #define NEXT_SIBLING 1
119 #define NEXT_PROPERTY 3
120 #define PREV_PARENT 4
121 #define MORE_MEMORY 5
122 #define CALL_AGAIN -2
123 #define ERR_CFG_USE -9003
125 struct device_node
*dlpar_configure_connector(u32 drc_index
)
127 struct device_node
*dn
;
128 struct device_node
*first_dn
= NULL
;
129 struct device_node
*last_dn
= NULL
;
130 struct property
*property
;
131 struct property
*last_property
= NULL
;
132 struct cc_workarea
*ccwa
;
136 cc_token
= rtas_token("ibm,configure-connector");
137 if (cc_token
== RTAS_UNKNOWN_SERVICE
)
140 spin_lock(&rtas_data_buf_lock
);
141 ccwa
= (struct cc_workarea
*)&rtas_data_buf
[0];
142 ccwa
->drc_index
= drc_index
;
145 rc
= rtas_call(cc_token
, 2, 1, NULL
, rtas_data_buf
, NULL
);
149 dn
= dlpar_parse_cc_node(ccwa
);
153 dn
->parent
= last_dn
->parent
;
154 last_dn
->sibling
= dn
;
159 dn
= dlpar_parse_cc_node(ccwa
);
166 dn
->parent
= last_dn
;
175 property
= dlpar_parse_cc_property(ccwa
);
179 if (!last_dn
->properties
)
180 last_dn
->properties
= property
;
182 last_property
->next
= property
;
184 last_property
= property
;
188 last_dn
= last_dn
->parent
;
197 printk(KERN_ERR
"Unexpected Error (%d) "
198 "returned from configure-connector\n", rc
);
202 rc
= rtas_call(cc_token
, 2, 1, NULL
, rtas_data_buf
, NULL
);
205 spin_unlock(&rtas_data_buf_lock
);
210 dlpar_free_cc_nodes(first_dn
);
211 spin_unlock(&rtas_data_buf_lock
);
215 static struct device_node
*derive_parent(const char *path
)
217 struct device_node
*parent
;
220 last_slash
= strrchr(path
, '/');
221 if (last_slash
== path
) {
222 parent
= of_find_node_by_path("/");
225 int parent_path_len
= last_slash
- path
+ 1;
226 parent_path
= kmalloc(parent_path_len
, GFP_KERNEL
);
230 strlcpy(parent_path
, path
, parent_path_len
);
231 parent
= of_find_node_by_path(parent_path
);
238 int dlpar_attach_node(struct device_node
*dn
)
240 #ifdef CONFIG_PROC_DEVICETREE
241 struct proc_dir_entry
*ent
;
245 of_node_set_flag(dn
, OF_DYNAMIC
);
246 kref_init(&dn
->kref
);
247 dn
->parent
= derive_parent(dn
->full_name
);
251 rc
= blocking_notifier_call_chain(&pSeries_reconfig_chain
,
252 PSERIES_RECONFIG_ADD
, dn
);
253 if (rc
== NOTIFY_BAD
) {
254 printk(KERN_ERR
"Failed to add device node %s\n",
256 return -ENOMEM
; /* For now, safe to assume kmalloc failure */
261 #ifdef CONFIG_PROC_DEVICETREE
262 ent
= proc_mkdir(strrchr(dn
->full_name
, '/') + 1, dn
->parent
->pde
);
264 proc_device_tree_add_node(dn
, ent
);
267 of_node_put(dn
->parent
);
271 int dlpar_detach_node(struct device_node
*dn
)
273 #ifdef CONFIG_PROC_DEVICETREE
274 struct device_node
*parent
= dn
->parent
;
275 struct property
*prop
= dn
->properties
;
278 remove_proc_entry(prop
->name
, dn
->pde
);
283 remove_proc_entry(dn
->pde
->name
, parent
->pde
);
286 blocking_notifier_call_chain(&pSeries_reconfig_chain
,
287 PSERIES_RECONFIG_REMOVE
, dn
);
289 of_node_put(dn
); /* Must decrement the refcount */
294 #define DR_ENTITY_SENSE 9003
295 #define DR_ENTITY_PRESENT 1
296 #define DR_ENTITY_UNUSABLE 2
297 #define ALLOCATION_STATE 9003
298 #define ALLOC_UNUSABLE 0
299 #define ALLOC_USABLE 1
300 #define ISOLATION_STATE 9001
304 int dlpar_acquire_drc(u32 drc_index
)
308 rc
= rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status
,
309 DR_ENTITY_SENSE
, drc_index
);
310 if (rc
|| dr_status
!= DR_ENTITY_UNUSABLE
)
313 rc
= rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_USABLE
);
317 rc
= rtas_set_indicator(ISOLATION_STATE
, drc_index
, UNISOLATE
);
319 rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_UNUSABLE
);
326 int dlpar_release_drc(u32 drc_index
)
330 rc
= rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status
,
331 DR_ENTITY_SENSE
, drc_index
);
332 if (rc
|| dr_status
!= DR_ENTITY_PRESENT
)
335 rc
= rtas_set_indicator(ISOLATION_STATE
, drc_index
, ISOLATE
);
339 rc
= rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_UNUSABLE
);
341 rtas_set_indicator(ISOLATION_STATE
, drc_index
, UNISOLATE
);
348 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
350 static int dlpar_online_cpu(struct device_node
*dn
)
354 int len
, nthreads
, i
;
357 intserv
= of_get_property(dn
, "ibm,ppc-interrupt-server#s", &len
);
361 nthreads
= len
/ sizeof(u32
);
363 cpu_maps_update_begin();
364 for (i
= 0; i
< nthreads
; i
++) {
365 for_each_present_cpu(cpu
) {
366 if (get_hard_smp_processor_id(cpu
) != intserv
[i
])
368 BUG_ON(get_cpu_current_state(cpu
)
369 != CPU_STATE_OFFLINE
);
370 cpu_maps_update_done();
374 cpu_maps_update_begin();
378 if (cpu
== num_possible_cpus())
379 printk(KERN_WARNING
"Could not find cpu to online "
380 "with physical id 0x%x\n", intserv
[i
]);
382 cpu_maps_update_done();
389 static ssize_t
dlpar_cpu_probe(const char *buf
, size_t count
)
391 struct device_node
*dn
;
392 unsigned long drc_index
;
396 cpu_hotplug_driver_lock();
397 rc
= strict_strtoul(buf
, 0, &drc_index
);
403 dn
= dlpar_configure_connector(drc_index
);
409 /* configure-connector reports cpus as living in the base
410 * directory of the device tree. CPUs actually live in the
411 * cpus directory so we need to fixup the full_name.
413 cpu_name
= kzalloc(strlen(dn
->full_name
) + strlen("/cpus") + 1,
416 dlpar_free_cc_nodes(dn
);
421 sprintf(cpu_name
, "/cpus%s", dn
->full_name
);
422 kfree(dn
->full_name
);
423 dn
->full_name
= cpu_name
;
425 rc
= dlpar_acquire_drc(drc_index
);
427 dlpar_free_cc_nodes(dn
);
432 rc
= dlpar_attach_node(dn
);
434 dlpar_release_drc(drc_index
);
435 dlpar_free_cc_nodes(dn
);
438 rc
= dlpar_online_cpu(dn
);
440 cpu_hotplug_driver_unlock();
442 return rc
? rc
: count
;
445 static int dlpar_offline_cpu(struct device_node
*dn
)
449 int len
, nthreads
, i
;
452 intserv
= of_get_property(dn
, "ibm,ppc-interrupt-server#s", &len
);
456 nthreads
= len
/ sizeof(u32
);
458 cpu_maps_update_begin();
459 for (i
= 0; i
< nthreads
; i
++) {
460 for_each_present_cpu(cpu
) {
461 if (get_hard_smp_processor_id(cpu
) != intserv
[i
])
464 if (get_cpu_current_state(cpu
) == CPU_STATE_OFFLINE
)
467 if (get_cpu_current_state(cpu
) == CPU_STATE_ONLINE
) {
468 cpu_maps_update_done();
472 cpu_maps_update_begin();
478 * The cpu is in CPU_STATE_INACTIVE.
479 * Upgrade it's state to CPU_STATE_OFFLINE.
481 set_preferred_offline_state(cpu
, CPU_STATE_OFFLINE
);
482 BUG_ON(plpar_hcall_norets(H_PROD
, intserv
[i
])
487 if (cpu
== num_possible_cpus())
488 printk(KERN_WARNING
"Could not find cpu to offline "
489 "with physical id 0x%x\n", intserv
[i
]);
491 cpu_maps_update_done();
498 static ssize_t
dlpar_cpu_release(const char *buf
, size_t count
)
500 struct device_node
*dn
;
501 const u32
*drc_index
;
504 dn
= of_find_node_by_path(buf
);
508 drc_index
= of_get_property(dn
, "ibm,my-drc-index", NULL
);
514 cpu_hotplug_driver_lock();
515 rc
= dlpar_offline_cpu(dn
);
522 rc
= dlpar_release_drc(*drc_index
);
528 rc
= dlpar_detach_node(dn
);
530 dlpar_acquire_drc(*drc_index
);
536 cpu_hotplug_driver_unlock();
537 return rc
? rc
: count
;
540 static int __init
pseries_dlpar_init(void)
542 ppc_md
.cpu_probe
= dlpar_cpu_probe
;
543 ppc_md
.cpu_release
= dlpar_cpu_release
;
547 machine_device_initcall(pseries
, pseries_dlpar_init
);
549 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */