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