blob: 2d0b4d68a40a076f970fd458ab09d06785a43ff2 [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>
Nathan Fontenotab519a02009-11-24 21:10:49 +000014#include <linux/notifier.h>
Nathan Fontenotab519a02009-11-24 21:10:49 +000015#include <linux/spinlock.h>
16#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000018#include <linux/of.h>
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +000019#include "offline_states.h"
Nathan Fontenotab519a02009-11-24 21:10:49 +000020
21#include <asm/prom.h>
22#include <asm/machdep.h>
23#include <asm/uaccess.h>
24#include <asm/rtas.h>
Nathan Fontenotab519a02009-11-24 21:10:49 +000025
26struct cc_workarea {
27 u32 drc_index;
28 u32 zero;
29 u32 name_offset;
30 u32 prop_length;
31 u32 prop_offset;
32};
33
Nathan Fontenot20648972010-09-10 09:40:32 +000034void dlpar_free_cc_property(struct property *prop)
Nathan Fontenotab519a02009-11-24 21:10:49 +000035{
36 kfree(prop->name);
37 kfree(prop->value);
38 kfree(prop);
39}
40
41static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
42{
43 struct property *prop;
44 char *name;
45 char *value;
46
47 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
48 if (!prop)
49 return NULL;
50
51 name = (char *)ccwa + ccwa->name_offset;
52 prop->name = kstrdup(name, GFP_KERNEL);
53
54 prop->length = ccwa->prop_length;
55 value = (char *)ccwa + ccwa->prop_offset;
Nishanth Aravamudane72ed6b2010-09-15 08:05:49 +000056 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
Nathan Fontenotab519a02009-11-24 21:10:49 +000057 if (!prop->value) {
58 dlpar_free_cc_property(prop);
59 return NULL;
60 }
61
Nathan Fontenotab519a02009-11-24 21:10:49 +000062 return prop;
63}
64
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -070065static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
66 const char *path)
Nathan Fontenotab519a02009-11-24 21:10:49 +000067{
68 struct device_node *dn;
69 char *name;
70
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -070071 /* If parent node path is "/" advance path to NULL terminator to
72 * prevent double leading slashs in full_name.
73 */
74 if (!path[1])
75 path++;
76
Nathan Fontenotab519a02009-11-24 21:10:49 +000077 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
78 if (!dn)
79 return NULL;
80
Nathan Fontenotab519a02009-11-24 21:10:49 +000081 name = (char *)ccwa + ccwa->name_offset;
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -070082 dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
Nathan Fontenotab519a02009-11-24 21:10:49 +000083 if (!dn->full_name) {
84 kfree(dn);
85 return NULL;
86 }
87
Tyrel Datwyler1578cb72013-08-14 22:23:49 -070088 of_node_set_flag(dn, OF_DYNAMIC);
Tyrel Datwyler97a9a712014-07-10 14:50:57 -040089 of_node_init(dn);
Tyrel Datwyler1578cb72013-08-14 22:23:49 -070090
Nathan Fontenotab519a02009-11-24 21:10:49 +000091 return dn;
92}
93
94static void dlpar_free_one_cc_node(struct device_node *dn)
95{
96 struct property *prop;
97
98 while (dn->properties) {
99 prop = dn->properties;
100 dn->properties = prop->next;
101 dlpar_free_cc_property(prop);
102 }
103
104 kfree(dn->full_name);
105 kfree(dn);
106}
107
Nathan Fontenot20648972010-09-10 09:40:32 +0000108void dlpar_free_cc_nodes(struct device_node *dn)
Nathan Fontenotab519a02009-11-24 21:10:49 +0000109{
110 if (dn->child)
111 dlpar_free_cc_nodes(dn->child);
112
113 if (dn->sibling)
114 dlpar_free_cc_nodes(dn->sibling);
115
116 dlpar_free_one_cc_node(dn);
117}
118
Anton Blanchard9c740022011-08-14 14:30:30 +0000119#define COMPLETE 0
Nathan Fontenotab519a02009-11-24 21:10:49 +0000120#define NEXT_SIBLING 1
121#define NEXT_CHILD 2
122#define NEXT_PROPERTY 3
123#define PREV_PARENT 4
124#define MORE_MEMORY 5
125#define CALL_AGAIN -2
126#define ERR_CFG_USE -9003
127
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700128struct device_node *dlpar_configure_connector(u32 drc_index,
129 struct device_node *parent)
Nathan Fontenotab519a02009-11-24 21:10:49 +0000130{
131 struct device_node *dn;
132 struct device_node *first_dn = NULL;
133 struct device_node *last_dn = NULL;
134 struct property *property;
135 struct property *last_property = NULL;
136 struct cc_workarea *ccwa;
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000137 char *data_buf;
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700138 const char *parent_path = parent->full_name;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000139 int cc_token;
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000140 int rc = -1;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000141
142 cc_token = rtas_token("ibm,configure-connector");
143 if (cc_token == RTAS_UNKNOWN_SERVICE)
144 return NULL;
145
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000146 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
147 if (!data_buf)
148 return NULL;
149
150 ccwa = (struct cc_workarea *)&data_buf[0];
Nathan Fontenotab519a02009-11-24 21:10:49 +0000151 ccwa->drc_index = drc_index;
152 ccwa->zero = 0;
153
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000154 do {
155 /* Since we release the rtas_data_buf lock between configure
156 * connector calls we want to re-populate the rtas_data_buffer
157 * with the contents of the previous call.
158 */
159 spin_lock(&rtas_data_buf_lock);
160
161 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
162 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
163 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
164
165 spin_unlock(&rtas_data_buf_lock);
166
Nathan Fontenotab519a02009-11-24 21:10:49 +0000167 switch (rc) {
Anton Blanchard9c740022011-08-14 14:30:30 +0000168 case COMPLETE:
169 break;
170
Nathan Fontenotab519a02009-11-24 21:10:49 +0000171 case NEXT_SIBLING:
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700172 dn = dlpar_parse_cc_node(ccwa, parent_path);
Nathan Fontenotab519a02009-11-24 21:10:49 +0000173 if (!dn)
174 goto cc_error;
175
176 dn->parent = last_dn->parent;
177 last_dn->sibling = dn;
178 last_dn = dn;
179 break;
180
181 case NEXT_CHILD:
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700182 if (first_dn)
183 parent_path = last_dn->full_name;
184
185 dn = dlpar_parse_cc_node(ccwa, parent_path);
Nathan Fontenotab519a02009-11-24 21:10:49 +0000186 if (!dn)
187 goto cc_error;
188
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700189 if (!first_dn) {
190 dn->parent = parent;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000191 first_dn = dn;
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700192 } else {
Nathan Fontenotab519a02009-11-24 21:10:49 +0000193 dn->parent = last_dn;
194 if (last_dn)
195 last_dn->child = dn;
196 }
197
198 last_dn = dn;
199 break;
200
201 case NEXT_PROPERTY:
202 property = dlpar_parse_cc_property(ccwa);
203 if (!property)
204 goto cc_error;
205
206 if (!last_dn->properties)
207 last_dn->properties = property;
208 else
209 last_property->next = property;
210
211 last_property = property;
212 break;
213
214 case PREV_PARENT:
215 last_dn = last_dn->parent;
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700216 parent_path = last_dn->parent->full_name;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000217 break;
218
219 case CALL_AGAIN:
220 break;
221
222 case MORE_MEMORY:
223 case ERR_CFG_USE:
224 default:
225 printk(KERN_ERR "Unexpected Error (%d) "
226 "returned from configure-connector\n", rc);
227 goto cc_error;
228 }
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000229 } while (rc);
Nathan Fontenotab519a02009-11-24 21:10:49 +0000230
231cc_error:
Nathan Fontenot93f68f12010-08-18 09:58:46 +0000232 kfree(data_buf);
233
234 if (rc) {
235 if (first_dn)
236 dlpar_free_cc_nodes(first_dn);
237
238 return NULL;
239 }
240
241 return first_dn;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000242}
243
244static struct device_node *derive_parent(const char *path)
245{
246 struct device_node *parent;
247 char *last_slash;
248
249 last_slash = strrchr(path, '/');
250 if (last_slash == path) {
251 parent = of_find_node_by_path("/");
252 } else {
253 char *parent_path;
254 int parent_path_len = last_slash - path + 1;
255 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
256 if (!parent_path)
257 return NULL;
258
259 strlcpy(parent_path, path, parent_path_len);
260 parent = of_find_node_by_path(parent_path);
261 kfree(parent_path);
262 }
263
264 return parent;
265}
266
267int dlpar_attach_node(struct device_node *dn)
268{
Nathan Fontenotab519a02009-11-24 21:10:49 +0000269 int rc;
270
Nathan Fontenotab519a02009-11-24 21:10:49 +0000271 dn->parent = derive_parent(dn->full_name);
272 if (!dn->parent)
273 return -ENOMEM;
274
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000275 rc = of_attach_node(dn);
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000276 if (rc) {
Nathan Fontenotab519a02009-11-24 21:10:49 +0000277 printk(KERN_ERR "Failed to add device node %s\n",
278 dn->full_name);
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000279 return rc;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000280 }
281
Nathan Fontenotab519a02009-11-24 21:10:49 +0000282 of_node_put(dn->parent);
283 return 0;
284}
285
286int dlpar_detach_node(struct device_node *dn)
287{
Tyrel Datwyler5935ff42013-08-14 22:23:52 -0700288 struct device_node *child;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000289 int rc;
Nathan Fontenotab519a02009-11-24 21:10:49 +0000290
Tyrel Datwyler5935ff42013-08-14 22:23:52 -0700291 child = of_get_next_child(dn, NULL);
292 while (child) {
293 dlpar_detach_node(child);
294 child = of_get_next_child(dn, child);
295 }
296
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000297 rc = of_detach_node(dn);
298 if (rc)
299 return rc;
300
301 of_node_put(dn); /* Must decrement the refcount */
Nathan Fontenotab519a02009-11-24 21:10:49 +0000302 return 0;
303}
304
305#define DR_ENTITY_SENSE 9003
306#define DR_ENTITY_PRESENT 1
307#define DR_ENTITY_UNUSABLE 2
308#define ALLOCATION_STATE 9003
309#define ALLOC_UNUSABLE 0
310#define ALLOC_USABLE 1
311#define ISOLATION_STATE 9001
312#define ISOLATE 0
313#define UNISOLATE 1
314
315int dlpar_acquire_drc(u32 drc_index)
316{
317 int dr_status, rc;
318
319 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
320 DR_ENTITY_SENSE, drc_index);
321 if (rc || dr_status != DR_ENTITY_UNUSABLE)
322 return -1;
323
324 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
325 if (rc)
326 return rc;
327
328 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
329 if (rc) {
330 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
331 return rc;
332 }
333
334 return 0;
335}
336
337int dlpar_release_drc(u32 drc_index)
338{
339 int dr_status, rc;
340
341 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
342 DR_ENTITY_SENSE, drc_index);
343 if (rc || dr_status != DR_ENTITY_PRESENT)
344 return -1;
345
346 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
347 if (rc)
348 return rc;
349
350 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
351 if (rc) {
352 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
353 return rc;
354 }
355
356 return 0;
357}
358
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000359#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
Nathan Fontenotab519a02009-11-24 21:10:49 +0000360
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000361static int dlpar_online_cpu(struct device_node *dn)
362{
363 int rc = 0;
364 unsigned int cpu;
365 int len, nthreads, i;
366 const u32 *intserv;
367
368 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
369 if (!intserv)
370 return -EINVAL;
371
372 nthreads = len / sizeof(u32);
373
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])
378 continue;
379 BUG_ON(get_cpu_current_state(cpu)
380 != CPU_STATE_OFFLINE);
381 cpu_maps_update_done();
382 rc = cpu_up(cpu);
383 if (rc)
384 goto out;
385 cpu_maps_update_begin();
386
387 break;
388 }
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]);
392 }
393 cpu_maps_update_done();
394
395out:
396 return rc;
397
398}
399
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000400static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
401{
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700402 struct device_node *dn, *parent;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000403 unsigned long drc_index;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000404 int rc;
405
406 rc = strict_strtoul(buf, 0, &drc_index);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600407 if (rc)
408 return -EINVAL;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000409
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700410 parent = of_find_node_by_path("/cpus");
Toshi Kani6dedcca2013-09-25 15:08:27 -0600411 if (!parent)
412 return -ENODEV;
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700413
414 dn = dlpar_configure_connector(drc_index, parent);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600415 if (!dn)
416 return -EINVAL;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000417
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700418 of_node_put(parent);
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000419
420 rc = dlpar_acquire_drc(drc_index);
421 if (rc) {
422 dlpar_free_cc_nodes(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600423 return -EINVAL;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000424 }
425
426 rc = dlpar_attach_node(dn);
427 if (rc) {
428 dlpar_release_drc(drc_index);
429 dlpar_free_cc_nodes(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600430 return rc;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000431 }
432
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000433 rc = dlpar_online_cpu(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600434 if (rc)
435 return rc;
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000436
Toshi Kani6dedcca2013-09-25 15:08:27 -0600437 return count;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000438}
439
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000440static int dlpar_offline_cpu(struct device_node *dn)
441{
442 int rc = 0;
443 unsigned int cpu;
444 int len, nthreads, i;
445 const u32 *intserv;
446
447 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
448 if (!intserv)
449 return -EINVAL;
450
451 nthreads = len / sizeof(u32);
452
453 cpu_maps_update_begin();
454 for (i = 0; i < nthreads; i++) {
455 for_each_present_cpu(cpu) {
456 if (get_hard_smp_processor_id(cpu) != intserv[i])
457 continue;
458
459 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
460 break;
461
462 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
Robert Jenningsceddee22010-07-22 16:43:44 +0000463 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000464 cpu_maps_update_done();
465 rc = cpu_down(cpu);
466 if (rc)
467 goto out;
468 cpu_maps_update_begin();
469 break;
470
471 }
472
473 /*
474 * The cpu is in CPU_STATE_INACTIVE.
475 * Upgrade it's state to CPU_STATE_OFFLINE.
476 */
477 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
478 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
479 != H_SUCCESS);
480 __cpu_die(cpu);
481 break;
482 }
483 if (cpu == num_possible_cpus())
484 printk(KERN_WARNING "Could not find cpu to offline "
485 "with physical id 0x%x\n", intserv[i]);
486 }
487 cpu_maps_update_done();
488
489out:
490 return rc;
491
492}
493
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000494static ssize_t dlpar_cpu_release(const char *buf, size_t count)
495{
496 struct device_node *dn;
497 const u32 *drc_index;
498 int rc;
499
500 dn = of_find_node_by_path(buf);
501 if (!dn)
502 return -EINVAL;
503
504 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
505 if (!drc_index) {
506 of_node_put(dn);
507 return -EINVAL;
508 }
509
Nathan Fontenot275a64f2009-12-08 09:48:44 +0000510 rc = dlpar_offline_cpu(dn);
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000511 if (rc) {
512 of_node_put(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600513 return -EINVAL;
Gautham R Shenoyb6db63d2009-11-26 09:58:55 +0000514 }
515
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000516 rc = dlpar_release_drc(*drc_index);
517 if (rc) {
518 of_node_put(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600519 return rc;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000520 }
521
522 rc = dlpar_detach_node(dn);
523 if (rc) {
524 dlpar_acquire_drc(*drc_index);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600525 return rc;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000526 }
527
528 of_node_put(dn);
Toshi Kani6dedcca2013-09-25 15:08:27 -0600529
530 return count;
Nathan Fontenot1a8061c2009-11-24 21:13:32 +0000531}
532
533static int __init pseries_dlpar_init(void)
534{
535 ppc_md.cpu_probe = dlpar_cpu_probe;
536 ppc_md.cpu_release = dlpar_cpu_release;
537
538 return 0;
539}
540machine_device_initcall(pseries, pseries_dlpar_init);
541
542#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */