blob: 1540a41d1a8534ef33719c46ae71dba5ce095d43 [file] [log] [blame]
Nathan Fontenotab519a02009-11-24 21:10:49 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +000020#include "offline_states.h"
Nathan Fontenotab519a02009-11-24 21:10:49 +000021
22#include <asm/prom.h>
23#include <asm/machdep.h>
24#include <asm/uaccess.h>
25#include <asm/rtas.h>
26#include <asm/pSeries_reconfig.h>
27
28struct cc_workarea {
29 u32 drc_index;
30 u32 zero;
31 u32 name_offset;
32 u32 prop_length;
33 u32 prop_offset;
34};
35
36static void dlpar_free_cc_property(struct property *prop)
37{
38 kfree(prop->name);
39 kfree(prop->value);
40 kfree(prop);
41}
42
43static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
44{
45 struct property *prop;
46 char *name;
47 char *value;
48
49 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
50 if (!prop)
51 return NULL;
52
53 name = (char *)ccwa + ccwa->name_offset;
54 prop->name = kstrdup(name, GFP_KERNEL);
55
56 prop->length = ccwa->prop_length;
57 value = (char *)ccwa + ccwa->prop_offset;
58 prop->value = kzalloc(prop->length, GFP_KERNEL);
59 if (!prop->value) {
60 dlpar_free_cc_property(prop);
61 return NULL;
62 }
63
64 memcpy(prop->value, value, prop->length);
65 return prop;
66}
67
68static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
69{
70 struct device_node *dn;
71 char *name;
72
73 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
74 if (!dn)
75 return NULL;
76
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.
80 */
81 name = (char *)ccwa + ccwa->name_offset;
82 dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL);
83 if (!dn->full_name) {
84 kfree(dn);
85 return NULL;
86 }
87
88 sprintf(dn->full_name, "/%s", name);
89 return dn;
90}
91
92static void dlpar_free_one_cc_node(struct device_node *dn)
93{
94 struct property *prop;
95
96 while (dn->properties) {
97 prop = dn->properties;
98 dn->properties = prop->next;
99 dlpar_free_cc_property(prop);
100 }
101
102 kfree(dn->full_name);
103 kfree(dn);
104}
105
106static void dlpar_free_cc_nodes(struct device_node *dn)
107{
108 if (dn->child)
109 dlpar_free_cc_nodes(dn->child);
110
111 if (dn->sibling)
112 dlpar_free_cc_nodes(dn->sibling);
113
114 dlpar_free_one_cc_node(dn);
115}
116
117#define NEXT_SIBLING 1
118#define NEXT_CHILD 2
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
124
125struct device_node *dlpar_configure_connector(u32 drc_index)
126{
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;
133 int cc_token;
134 int rc;
135
136 cc_token = rtas_token("ibm,configure-connector");
137 if (cc_token == RTAS_UNKNOWN_SERVICE)
138 return NULL;
139
140 spin_lock(&rtas_data_buf_lock);
141 ccwa = (struct cc_workarea *)&rtas_data_buf[0];
142 ccwa->drc_index = drc_index;
143 ccwa->zero = 0;
144
145 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
146 while (rc) {
147 switch (rc) {
148 case NEXT_SIBLING:
149 dn = dlpar_parse_cc_node(ccwa);
150 if (!dn)
151 goto cc_error;
152
153 dn->parent = last_dn->parent;
154 last_dn->sibling = dn;
155 last_dn = dn;
156 break;
157
158 case NEXT_CHILD:
159 dn = dlpar_parse_cc_node(ccwa);
160 if (!dn)
161 goto cc_error;
162
163 if (!first_dn)
164 first_dn = dn;
165 else {
166 dn->parent = last_dn;
167 if (last_dn)
168 last_dn->child = dn;
169 }
170
171 last_dn = dn;
172 break;
173
174 case NEXT_PROPERTY:
175 property = dlpar_parse_cc_property(ccwa);
176 if (!property)
177 goto cc_error;
178
179 if (!last_dn->properties)
180 last_dn->properties = property;
181 else
182 last_property->next = property;
183
184 last_property = property;
185 break;
186
187 case PREV_PARENT:
188 last_dn = last_dn->parent;
189 break;
190
191 case CALL_AGAIN:
192 break;
193
194 case MORE_MEMORY:
195 case ERR_CFG_USE:
196 default:
197 printk(KERN_ERR "Unexpected Error (%d) "
198 "returned from configure-connector\n", rc);
199 goto cc_error;
200 }
201
202 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
203 }
204
205 spin_unlock(&rtas_data_buf_lock);
206 return first_dn;
207
208cc_error:
209 if (first_dn)
210 dlpar_free_cc_nodes(first_dn);
211 spin_unlock(&rtas_data_buf_lock);
212 return NULL;
213}
214
215static struct device_node *derive_parent(const char *path)
216{
217 struct device_node *parent;
218 char *last_slash;
219
220 last_slash = strrchr(path, '/');
221 if (last_slash == path) {
222 parent = of_find_node_by_path("/");
223 } else {
224 char *parent_path;
225 int parent_path_len = last_slash - path + 1;
226 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
227 if (!parent_path)
228 return NULL;
229
230 strlcpy(parent_path, path, parent_path_len);
231 parent = of_find_node_by_path(parent_path);
232 kfree(parent_path);
233 }
234
235 return parent;
236}
237
238int dlpar_attach_node(struct device_node *dn)
239{
FUJITA Tomonori46150a02010-01-05 00:49:02 +0000240#ifdef CONFIG_PROC_DEVICETREE
Nathan Fontenotab519a02009-11-24 21:10:49 +0000241 struct proc_dir_entry *ent;
FUJITA Tomonori46150a02010-01-05 00:49:02 +0000242#endif
Nathan Fontenotab519a02009-11-24 21:10:49 +0000243 int rc;
244
245 of_node_set_flag(dn, OF_DYNAMIC);
246 kref_init(&dn->kref);
247 dn->parent = derive_parent(dn->full_name);
248 if (!dn->parent)
249 return -ENOMEM;
250
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",
255 dn->full_name);
256 return -ENOMEM; /* For now, safe to assume kmalloc failure */
257 }
258
259 of_attach_node(dn);
260
261#ifdef CONFIG_PROC_DEVICETREE
262 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
263 if (ent)
264 proc_device_tree_add_node(dn, ent);
265#endif
266
267 of_node_put(dn->parent);
268 return 0;
269}
270
271int dlpar_detach_node(struct device_node *dn)
272{
FUJITA Tomonori46150a02010-01-05 00:49:02 +0000273#ifdef CONFIG_PROC_DEVICETREE
Nathan Fontenotab519a02009-11-24 21:10:49 +0000274 struct device_node *parent = dn->parent;
275 struct property *prop = dn->properties;
276
Nathan Fontenotab519a02009-11-24 21:10:49 +0000277 while (prop) {
278 remove_proc_entry(prop->name, dn->pde);
279 prop = prop->next;
280 }
281
282 if (dn->pde)
283 remove_proc_entry(dn->pde->name, parent->pde);
284#endif
285
286 blocking_notifier_call_chain(&pSeries_reconfig_chain,
287 PSERIES_RECONFIG_REMOVE, dn);
288 of_detach_node(dn);
289 of_node_put(dn); /* Must decrement the refcount */
290
291 return 0;
292}
293
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
301#define ISOLATE 0
302#define UNISOLATE 1
303
304int dlpar_acquire_drc(u32 drc_index)
305{
306 int dr_status, rc;
307
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)
311 return -1;
312
313 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
314 if (rc)
315 return rc;
316
317 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
318 if (rc) {
319 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
320 return rc;
321 }
322
323 return 0;
324}
325
326int dlpar_release_drc(u32 drc_index)
327{
328 int dr_status, rc;
329
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)
333 return -1;
334
335 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
336 if (rc)
337 return rc;
338
339 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
340 if (rc) {
341 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
342 return rc;
343 }
344
345 return 0;
346}
347
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000348#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
Nathan Fontenotab519a02009-11-24 21:10:49 +0000349
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000350static int dlpar_online_cpu(struct device_node *dn)
351{
352 int rc = 0;
353 unsigned int cpu;
354 int len, nthreads, i;
355 const u32 *intserv;
356
357 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
358 if (!intserv)
359 return -EINVAL;
360
361 nthreads = len / sizeof(u32);
362
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])
367 continue;
368 BUG_ON(get_cpu_current_state(cpu)
369 != CPU_STATE_OFFLINE);
370 cpu_maps_update_done();
371 rc = cpu_up(cpu);
372 if (rc)
373 goto out;
374 cpu_maps_update_begin();
375
376 break;
377 }
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]);
381 }
382 cpu_maps_update_done();
383
384out:
385 return rc;
386
387}
388
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000389static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
390{
391 struct device_node *dn;
392 unsigned long drc_index;
393 char *cpu_name;
394 int rc;
395
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000396 cpu_hotplug_driver_lock();
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000397 rc = strict_strtoul(buf, 0, &drc_index);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000398 if (rc) {
399 rc = -EINVAL;
400 goto out;
401 }
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000402
403 dn = dlpar_configure_connector(drc_index);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000404 if (!dn) {
405 rc = -EINVAL;
406 goto out;
407 }
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000408
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.
412 */
413 cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1,
414 GFP_KERNEL);
415 if (!cpu_name) {
416 dlpar_free_cc_nodes(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000417 rc = -ENOMEM;
418 goto out;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000419 }
420
421 sprintf(cpu_name, "/cpus%s", dn->full_name);
422 kfree(dn->full_name);
423 dn->full_name = cpu_name;
424
425 rc = dlpar_acquire_drc(drc_index);
426 if (rc) {
427 dlpar_free_cc_nodes(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000428 rc = -EINVAL;
429 goto out;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000430 }
431
432 rc = dlpar_attach_node(dn);
433 if (rc) {
434 dlpar_release_drc(drc_index);
435 dlpar_free_cc_nodes(dn);
Julia Lawalla7df5c52010-04-02 02:47:13 +0000436 goto out;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000437 }
438
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000439 rc = dlpar_online_cpu(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000440out:
441 cpu_hotplug_driver_unlock();
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000442
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000443 return rc ? rc : count;
444}
445
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000446static int dlpar_offline_cpu(struct device_node *dn)
447{
448 int rc = 0;
449 unsigned int cpu;
450 int len, nthreads, i;
451 const u32 *intserv;
452
453 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
454 if (!intserv)
455 return -EINVAL;
456
457 nthreads = len / sizeof(u32);
458
459 cpu_maps_update_begin();
460 for (i = 0; i < nthreads; i++) {
461 for_each_present_cpu(cpu) {
462 if (get_hard_smp_processor_id(cpu) != intserv[i])
463 continue;
464
465 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
466 break;
467
468 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
469 cpu_maps_update_done();
470 rc = cpu_down(cpu);
471 if (rc)
472 goto out;
473 cpu_maps_update_begin();
474 break;
475
476 }
477
478 /*
479 * The cpu is in CPU_STATE_INACTIVE.
480 * Upgrade it's state to CPU_STATE_OFFLINE.
481 */
482 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
483 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
484 != H_SUCCESS);
485 __cpu_die(cpu);
486 break;
487 }
488 if (cpu == num_possible_cpus())
489 printk(KERN_WARNING "Could not find cpu to offline "
490 "with physical id 0x%x\n", intserv[i]);
491 }
492 cpu_maps_update_done();
493
494out:
495 return rc;
496
497}
498
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000499static ssize_t dlpar_cpu_release(const char *buf, size_t count)
500{
501 struct device_node *dn;
502 const u32 *drc_index;
503 int rc;
504
505 dn = of_find_node_by_path(buf);
506 if (!dn)
507 return -EINVAL;
508
509 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
510 if (!drc_index) {
511 of_node_put(dn);
512 return -EINVAL;
513 }
514
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000515 cpu_hotplug_driver_lock();
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000516 rc = dlpar_offline_cpu(dn);
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000517 if (rc) {
518 of_node_put(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000519 rc = -EINVAL;
520 goto out;
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000521 }
522
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000523 rc = dlpar_release_drc(*drc_index);
524 if (rc) {
525 of_node_put(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000526 goto out;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000527 }
528
529 rc = dlpar_detach_node(dn);
530 if (rc) {
531 dlpar_acquire_drc(*drc_index);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000532 goto out;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000533 }
534
535 of_node_put(dn);
Gautham R Shenoy51badeb2009-11-26 09:59:05 +0000536out:
537 cpu_hotplug_driver_unlock();
538 return rc ? rc : count;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000539}
540
541static int __init pseries_dlpar_init(void)
542{
543 ppc_md.cpu_probe = dlpar_cpu_probe;
544 ppc_md.cpu_release = dlpar_cpu_release;
545
546 return 0;
547}
548machine_device_initcall(pseries, pseries_dlpar_init);
549
550#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */