Commit | Line | Data |
---|---|---|
ab519a01 NF |
1 | /* |
2 | * Support for dynamic reconfiguration for PCI, Memory, and CPU | |
3 | * Hotplug and Dynamic Logical Partitioning on RPA platforms. | |
4 | * | |
5 | * Copyright (C) 2009 Nathan Fontenot | |
6 | * Copyright (C) 2009 IBM Corporation | |
7 | * | |
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. | |
11 | */ | |
12 | ||
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> | |
b6db63d1 | 19 | #include "offline_states.h" |
ab519a01 NF |
20 | |
21 | #include <asm/prom.h> | |
22 | #include <asm/machdep.h> | |
23 | #include <asm/uaccess.h> | |
24 | #include <asm/rtas.h> | |
25 | #include <asm/pSeries_reconfig.h> | |
26 | ||
27 | struct cc_workarea { | |
28 | u32 drc_index; | |
29 | u32 zero; | |
30 | u32 name_offset; | |
31 | u32 prop_length; | |
32 | u32 prop_offset; | |
33 | }; | |
34 | ||
35 | static void dlpar_free_cc_property(struct property *prop) | |
36 | { | |
37 | kfree(prop->name); | |
38 | kfree(prop->value); | |
39 | kfree(prop); | |
40 | } | |
41 | ||
42 | static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa) | |
43 | { | |
44 | struct property *prop; | |
45 | char *name; | |
46 | char *value; | |
47 | ||
48 | prop = kzalloc(sizeof(*prop), GFP_KERNEL); | |
49 | if (!prop) | |
50 | return NULL; | |
51 | ||
52 | name = (char *)ccwa + ccwa->name_offset; | |
53 | prop->name = kstrdup(name, GFP_KERNEL); | |
54 | ||
55 | prop->length = ccwa->prop_length; | |
56 | value = (char *)ccwa + ccwa->prop_offset; | |
57 | prop->value = kzalloc(prop->length, GFP_KERNEL); | |
58 | if (!prop->value) { | |
59 | dlpar_free_cc_property(prop); | |
60 | return NULL; | |
61 | } | |
62 | ||
63 | memcpy(prop->value, value, prop->length); | |
64 | return prop; | |
65 | } | |
66 | ||
67 | static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa) | |
68 | { | |
69 | struct device_node *dn; | |
70 | char *name; | |
71 | ||
72 | dn = kzalloc(sizeof(*dn), GFP_KERNEL); | |
73 | if (!dn) | |
74 | return NULL; | |
75 | ||
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. | |
79 | */ | |
80 | name = (char *)ccwa + ccwa->name_offset; | |
81 | dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL); | |
82 | if (!dn->full_name) { | |
83 | kfree(dn); | |
84 | return NULL; | |
85 | } | |
86 | ||
87 | sprintf(dn->full_name, "/%s", name); | |
88 | return dn; | |
89 | } | |
90 | ||
91 | static void dlpar_free_one_cc_node(struct device_node *dn) | |
92 | { | |
93 | struct property *prop; | |
94 | ||
95 | while (dn->properties) { | |
96 | prop = dn->properties; | |
97 | dn->properties = prop->next; | |
98 | dlpar_free_cc_property(prop); | |
99 | } | |
100 | ||
101 | kfree(dn->full_name); | |
102 | kfree(dn); | |
103 | } | |
104 | ||
105 | static void dlpar_free_cc_nodes(struct device_node *dn) | |
106 | { | |
107 | if (dn->child) | |
108 | dlpar_free_cc_nodes(dn->child); | |
109 | ||
110 | if (dn->sibling) | |
111 | dlpar_free_cc_nodes(dn->sibling); | |
112 | ||
113 | dlpar_free_one_cc_node(dn); | |
114 | } | |
115 | ||
116 | #define NEXT_SIBLING 1 | |
117 | #define NEXT_CHILD 2 | |
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 | |
123 | ||
124 | struct device_node *dlpar_configure_connector(u32 drc_index) | |
125 | { | |
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; | |
132 | int cc_token; | |
133 | int rc; | |
134 | ||
135 | cc_token = rtas_token("ibm,configure-connector"); | |
136 | if (cc_token == RTAS_UNKNOWN_SERVICE) | |
137 | return NULL; | |
138 | ||
139 | spin_lock(&rtas_data_buf_lock); | |
140 | ccwa = (struct cc_workarea *)&rtas_data_buf[0]; | |
141 | ccwa->drc_index = drc_index; | |
142 | ccwa->zero = 0; | |
143 | ||
144 | rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL); | |
145 | while (rc) { | |
146 | switch (rc) { | |
147 | case NEXT_SIBLING: | |
148 | dn = dlpar_parse_cc_node(ccwa); | |
149 | if (!dn) | |
150 | goto cc_error; | |
151 | ||
152 | dn->parent = last_dn->parent; | |
153 | last_dn->sibling = dn; | |
154 | last_dn = dn; | |
155 | break; | |
156 | ||
157 | case NEXT_CHILD: | |
158 | dn = dlpar_parse_cc_node(ccwa); | |
159 | if (!dn) | |
160 | goto cc_error; | |
161 | ||
162 | if (!first_dn) | |
163 | first_dn = dn; | |
164 | else { | |
165 | dn->parent = last_dn; | |
166 | if (last_dn) | |
167 | last_dn->child = dn; | |
168 | } | |
169 | ||
170 | last_dn = dn; | |
171 | break; | |
172 | ||
173 | case NEXT_PROPERTY: | |
174 | property = dlpar_parse_cc_property(ccwa); | |
175 | if (!property) | |
176 | goto cc_error; | |
177 | ||
178 | if (!last_dn->properties) | |
179 | last_dn->properties = property; | |
180 | else | |
181 | last_property->next = property; | |
182 | ||
183 | last_property = property; | |
184 | break; | |
185 | ||
186 | case PREV_PARENT: | |
187 | last_dn = last_dn->parent; | |
188 | break; | |
189 | ||
190 | case CALL_AGAIN: | |
191 | break; | |
192 | ||
193 | case MORE_MEMORY: | |
194 | case ERR_CFG_USE: | |
195 | default: | |
196 | printk(KERN_ERR "Unexpected Error (%d) " | |
197 | "returned from configure-connector\n", rc); | |
198 | goto cc_error; | |
199 | } | |
200 | ||
201 | rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL); | |
202 | } | |
203 | ||
204 | spin_unlock(&rtas_data_buf_lock); | |
205 | return first_dn; | |
206 | ||
207 | cc_error: | |
208 | if (first_dn) | |
209 | dlpar_free_cc_nodes(first_dn); | |
210 | spin_unlock(&rtas_data_buf_lock); | |
211 | return NULL; | |
212 | } | |
213 | ||
214 | static struct device_node *derive_parent(const char *path) | |
215 | { | |
216 | struct device_node *parent; | |
217 | char *last_slash; | |
218 | ||
219 | last_slash = strrchr(path, '/'); | |
220 | if (last_slash == path) { | |
221 | parent = of_find_node_by_path("/"); | |
222 | } else { | |
223 | char *parent_path; | |
224 | int parent_path_len = last_slash - path + 1; | |
225 | parent_path = kmalloc(parent_path_len, GFP_KERNEL); | |
226 | if (!parent_path) | |
227 | return NULL; | |
228 | ||
229 | strlcpy(parent_path, path, parent_path_len); | |
230 | parent = of_find_node_by_path(parent_path); | |
231 | kfree(parent_path); | |
232 | } | |
233 | ||
234 | return parent; | |
235 | } | |
236 | ||
237 | int dlpar_attach_node(struct device_node *dn) | |
238 | { | |
239 | struct proc_dir_entry *ent; | |
240 | int rc; | |
241 | ||
242 | of_node_set_flag(dn, OF_DYNAMIC); | |
243 | kref_init(&dn->kref); | |
244 | dn->parent = derive_parent(dn->full_name); | |
245 | if (!dn->parent) | |
246 | return -ENOMEM; | |
247 | ||
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", | |
252 | dn->full_name); | |
253 | return -ENOMEM; /* For now, safe to assume kmalloc failure */ | |
254 | } | |
255 | ||
256 | of_attach_node(dn); | |
257 | ||
258 | #ifdef CONFIG_PROC_DEVICETREE | |
259 | ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde); | |
260 | if (ent) | |
261 | proc_device_tree_add_node(dn, ent); | |
262 | #endif | |
263 | ||
264 | of_node_put(dn->parent); | |
265 | return 0; | |
266 | } | |
267 | ||
268 | int dlpar_detach_node(struct device_node *dn) | |
269 | { | |
270 | struct device_node *parent = dn->parent; | |
271 | struct property *prop = dn->properties; | |
272 | ||
273 | #ifdef CONFIG_PROC_DEVICETREE | |
274 | while (prop) { | |
275 | remove_proc_entry(prop->name, dn->pde); | |
276 | prop = prop->next; | |
277 | } | |
278 | ||
279 | if (dn->pde) | |
280 | remove_proc_entry(dn->pde->name, parent->pde); | |
281 | #endif | |
282 | ||
283 | blocking_notifier_call_chain(&pSeries_reconfig_chain, | |
284 | PSERIES_RECONFIG_REMOVE, dn); | |
285 | of_detach_node(dn); | |
286 | of_node_put(dn); /* Must decrement the refcount */ | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
b6db63d1 GS |
291 | int online_node_cpus(struct device_node *dn) |
292 | { | |
293 | int rc = 0; | |
294 | unsigned int cpu; | |
295 | int len, nthreads, i; | |
296 | const u32 *intserv; | |
297 | ||
298 | intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len); | |
299 | if (!intserv) | |
300 | return -EINVAL; | |
301 | ||
302 | nthreads = len / sizeof(u32); | |
303 | ||
304 | cpu_maps_update_begin(); | |
305 | for (i = 0; i < nthreads; i++) { | |
306 | for_each_present_cpu(cpu) { | |
307 | if (get_hard_smp_processor_id(cpu) != intserv[i]) | |
308 | continue; | |
309 | BUG_ON(get_cpu_current_state(cpu) | |
310 | != CPU_STATE_OFFLINE); | |
311 | cpu_maps_update_done(); | |
312 | rc = cpu_up(cpu); | |
313 | if (rc) | |
314 | goto out; | |
315 | cpu_maps_update_begin(); | |
316 | ||
317 | break; | |
318 | } | |
319 | if (cpu == num_possible_cpus()) | |
320 | printk(KERN_WARNING "Could not find cpu to online " | |
321 | "with physical id 0x%x\n", intserv[i]); | |
322 | } | |
323 | cpu_maps_update_done(); | |
324 | ||
325 | out: | |
326 | return rc; | |
327 | ||
328 | } | |
329 | ||
330 | int offline_node_cpus(struct device_node *dn) | |
331 | { | |
332 | int rc = 0; | |
333 | unsigned int cpu; | |
334 | int len, nthreads, i; | |
335 | const u32 *intserv; | |
336 | ||
337 | intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len); | |
338 | if (!intserv) | |
339 | return -EINVAL; | |
340 | ||
341 | nthreads = len / sizeof(u32); | |
342 | ||
343 | cpu_maps_update_begin(); | |
344 | for (i = 0; i < nthreads; i++) { | |
345 | for_each_present_cpu(cpu) { | |
346 | if (get_hard_smp_processor_id(cpu) != intserv[i]) | |
347 | continue; | |
348 | ||
349 | if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE) | |
350 | break; | |
351 | ||
352 | if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) { | |
353 | cpu_maps_update_done(); | |
354 | rc = cpu_down(cpu); | |
355 | if (rc) | |
356 | goto out; | |
357 | cpu_maps_update_begin(); | |
358 | break; | |
359 | ||
360 | } | |
361 | ||
362 | /* | |
363 | * The cpu is in CPU_STATE_INACTIVE. | |
364 | * Upgrade it's state to CPU_STATE_OFFLINE. | |
365 | */ | |
366 | set_preferred_offline_state(cpu, CPU_STATE_OFFLINE); | |
367 | BUG_ON(plpar_hcall_norets(H_PROD, intserv[i]) | |
368 | != H_SUCCESS); | |
369 | __cpu_die(cpu); | |
370 | break; | |
371 | } | |
372 | if (cpu == num_possible_cpus()) | |
373 | printk(KERN_WARNING "Could not find cpu to offline " | |
374 | "with physical id 0x%x\n", intserv[i]); | |
375 | } | |
376 | cpu_maps_update_done(); | |
377 | ||
378 | out: | |
379 | return rc; | |
380 | ||
381 | } | |
382 | ||
ab519a01 NF |
383 | #define DR_ENTITY_SENSE 9003 |
384 | #define DR_ENTITY_PRESENT 1 | |
385 | #define DR_ENTITY_UNUSABLE 2 | |
386 | #define ALLOCATION_STATE 9003 | |
387 | #define ALLOC_UNUSABLE 0 | |
388 | #define ALLOC_USABLE 1 | |
389 | #define ISOLATION_STATE 9001 | |
390 | #define ISOLATE 0 | |
391 | #define UNISOLATE 1 | |
392 | ||
393 | int dlpar_acquire_drc(u32 drc_index) | |
394 | { | |
395 | int dr_status, rc; | |
396 | ||
397 | rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, | |
398 | DR_ENTITY_SENSE, drc_index); | |
399 | if (rc || dr_status != DR_ENTITY_UNUSABLE) | |
400 | return -1; | |
401 | ||
402 | rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE); | |
403 | if (rc) | |
404 | return rc; | |
405 | ||
406 | rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); | |
407 | if (rc) { | |
408 | rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); | |
409 | return rc; | |
410 | } | |
411 | ||
412 | return 0; | |
413 | } | |
414 | ||
415 | int dlpar_release_drc(u32 drc_index) | |
416 | { | |
417 | int dr_status, rc; | |
418 | ||
419 | rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, | |
420 | DR_ENTITY_SENSE, drc_index); | |
421 | if (rc || dr_status != DR_ENTITY_PRESENT) | |
422 | return -1; | |
423 | ||
424 | rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE); | |
425 | if (rc) | |
426 | return rc; | |
427 | ||
428 | rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); | |
429 | if (rc) { | |
430 | rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); | |
431 | return rc; | |
432 | } | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
1a8061c4 | 437 | #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE |
ab519a01 | 438 | |
51badebd GS |
439 | static DEFINE_MUTEX(pseries_cpu_hotplug_mutex); |
440 | ||
441 | void cpu_hotplug_driver_lock() | |
442 | { | |
443 | mutex_lock(&pseries_cpu_hotplug_mutex); | |
444 | } | |
445 | ||
446 | void cpu_hotplug_driver_unlock() | |
447 | { | |
448 | mutex_unlock(&pseries_cpu_hotplug_mutex); | |
449 | } | |
450 | ||
1a8061c4 NF |
451 | static ssize_t dlpar_cpu_probe(const char *buf, size_t count) |
452 | { | |
453 | struct device_node *dn; | |
454 | unsigned long drc_index; | |
455 | char *cpu_name; | |
456 | int rc; | |
457 | ||
51badebd | 458 | cpu_hotplug_driver_lock(); |
1a8061c4 | 459 | rc = strict_strtoul(buf, 0, &drc_index); |
51badebd GS |
460 | if (rc) { |
461 | rc = -EINVAL; | |
462 | goto out; | |
463 | } | |
1a8061c4 NF |
464 | |
465 | dn = dlpar_configure_connector(drc_index); | |
51badebd GS |
466 | if (!dn) { |
467 | rc = -EINVAL; | |
468 | goto out; | |
469 | } | |
1a8061c4 NF |
470 | |
471 | /* configure-connector reports cpus as living in the base | |
472 | * directory of the device tree. CPUs actually live in the | |
473 | * cpus directory so we need to fixup the full_name. | |
474 | */ | |
475 | cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1, | |
476 | GFP_KERNEL); | |
477 | if (!cpu_name) { | |
478 | dlpar_free_cc_nodes(dn); | |
51badebd GS |
479 | rc = -ENOMEM; |
480 | goto out; | |
1a8061c4 NF |
481 | } |
482 | ||
483 | sprintf(cpu_name, "/cpus%s", dn->full_name); | |
484 | kfree(dn->full_name); | |
485 | dn->full_name = cpu_name; | |
486 | ||
487 | rc = dlpar_acquire_drc(drc_index); | |
488 | if (rc) { | |
489 | dlpar_free_cc_nodes(dn); | |
51badebd GS |
490 | rc = -EINVAL; |
491 | goto out; | |
1a8061c4 NF |
492 | } |
493 | ||
494 | rc = dlpar_attach_node(dn); | |
495 | if (rc) { | |
496 | dlpar_release_drc(drc_index); | |
497 | dlpar_free_cc_nodes(dn); | |
498 | } | |
499 | ||
b6db63d1 | 500 | rc = online_node_cpus(dn); |
51badebd GS |
501 | out: |
502 | cpu_hotplug_driver_unlock(); | |
b6db63d1 | 503 | |
1a8061c4 NF |
504 | return rc ? rc : count; |
505 | } | |
506 | ||
507 | static ssize_t dlpar_cpu_release(const char *buf, size_t count) | |
508 | { | |
509 | struct device_node *dn; | |
510 | const u32 *drc_index; | |
511 | int rc; | |
512 | ||
513 | dn = of_find_node_by_path(buf); | |
514 | if (!dn) | |
515 | return -EINVAL; | |
516 | ||
517 | drc_index = of_get_property(dn, "ibm,my-drc-index", NULL); | |
518 | if (!drc_index) { | |
519 | of_node_put(dn); | |
520 | return -EINVAL; | |
521 | } | |
522 | ||
51badebd | 523 | cpu_hotplug_driver_lock(); |
b6db63d1 GS |
524 | rc = offline_node_cpus(dn); |
525 | if (rc) { | |
526 | of_node_put(dn); | |
51badebd GS |
527 | rc = -EINVAL; |
528 | goto out; | |
b6db63d1 GS |
529 | } |
530 | ||
1a8061c4 NF |
531 | rc = dlpar_release_drc(*drc_index); |
532 | if (rc) { | |
533 | of_node_put(dn); | |
51badebd | 534 | goto out; |
1a8061c4 NF |
535 | } |
536 | ||
537 | rc = dlpar_detach_node(dn); | |
538 | if (rc) { | |
539 | dlpar_acquire_drc(*drc_index); | |
51badebd | 540 | goto out; |
1a8061c4 NF |
541 | } |
542 | ||
543 | of_node_put(dn); | |
51badebd GS |
544 | out: |
545 | cpu_hotplug_driver_unlock(); | |
546 | return rc ? rc : count; | |
1a8061c4 NF |
547 | } |
548 | ||
549 | static int __init pseries_dlpar_init(void) | |
550 | { | |
551 | ppc_md.cpu_probe = dlpar_cpu_probe; | |
552 | ppc_md.cpu_release = dlpar_cpu_release; | |
553 | ||
554 | return 0; | |
555 | } | |
556 | machine_device_initcall(pseries, pseries_dlpar_init); | |
557 | ||
558 | #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */ |