upload tizen1.0 source
[kernel/linux-2.6.36.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/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"
21
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
28 struct cc_workarea {
29         u32     drc_index;
30         u32     zero;
31         u32     name_offset;
32         u32     prop_length;
33         u32     prop_offset;
34 };
35
36 static void dlpar_free_cc_property(struct property *prop)
37 {
38         kfree(prop->name);
39         kfree(prop->value);
40         kfree(prop);
41 }
42
43 static 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
68 static 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 = kasprintf(GFP_KERNEL, "/%s", name);
83         if (!dn->full_name) {
84                 kfree(dn);
85                 return NULL;
86         }
87
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         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 NEXT_SIBLING:
163                         dn = dlpar_parse_cc_node(ccwa);
164                         if (!dn)
165                                 goto cc_error;
166
167                         dn->parent = last_dn->parent;
168                         last_dn->sibling = dn;
169                         last_dn = dn;
170                         break;
171
172                 case NEXT_CHILD:
173                         dn = dlpar_parse_cc_node(ccwa);
174                         if (!dn)
175                                 goto cc_error;
176
177                         if (!first_dn)
178                                 first_dn = dn;
179                         else {
180                                 dn->parent = last_dn;
181                                 if (last_dn)
182                                         last_dn->child = dn;
183                         }
184
185                         last_dn = dn;
186                         break;
187
188                 case NEXT_PROPERTY:
189                         property = dlpar_parse_cc_property(ccwa);
190                         if (!property)
191                                 goto cc_error;
192
193                         if (!last_dn->properties)
194                                 last_dn->properties = property;
195                         else
196                                 last_property->next = property;
197
198                         last_property = property;
199                         break;
200
201                 case PREV_PARENT:
202                         last_dn = last_dn->parent;
203                         break;
204
205                 case CALL_AGAIN:
206                         break;
207
208                 case MORE_MEMORY:
209                 case ERR_CFG_USE:
210                 default:
211                         printk(KERN_ERR "Unexpected Error (%d) "
212                                "returned from configure-connector\n", rc);
213                         goto cc_error;
214                 }
215         } while (rc);
216
217 cc_error:
218         kfree(data_buf);
219
220         if (rc) {
221                 if (first_dn)
222                         dlpar_free_cc_nodes(first_dn);
223
224                 return NULL;
225         }
226
227         return first_dn;
228 }
229
230 static struct device_node *derive_parent(const char *path)
231 {
232         struct device_node *parent;
233         char *last_slash;
234
235         last_slash = strrchr(path, '/');
236         if (last_slash == path) {
237                 parent = of_find_node_by_path("/");
238         } else {
239                 char *parent_path;
240                 int parent_path_len = last_slash - path + 1;
241                 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
242                 if (!parent_path)
243                         return NULL;
244
245                 strlcpy(parent_path, path, parent_path_len);
246                 parent = of_find_node_by_path(parent_path);
247                 kfree(parent_path);
248         }
249
250         return parent;
251 }
252
253 int dlpar_attach_node(struct device_node *dn)
254 {
255 #ifdef CONFIG_PROC_DEVICETREE
256         struct proc_dir_entry *ent;
257 #endif
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 = blocking_notifier_call_chain(&pSeries_reconfig_chain,
267                                           PSERIES_RECONFIG_ADD, dn);
268         if (rc == NOTIFY_BAD) {
269                 printk(KERN_ERR "Failed to add device node %s\n",
270                        dn->full_name);
271                 return -ENOMEM; /* For now, safe to assume kmalloc failure */
272         }
273
274         of_attach_node(dn);
275
276 #ifdef CONFIG_PROC_DEVICETREE
277         ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
278         if (ent)
279                 proc_device_tree_add_node(dn, ent);
280 #endif
281
282         of_node_put(dn->parent);
283         return 0;
284 }
285
286 int dlpar_detach_node(struct device_node *dn)
287 {
288 #ifdef CONFIG_PROC_DEVICETREE
289         struct device_node *parent = dn->parent;
290         struct property *prop = dn->properties;
291
292         while (prop) {
293                 remove_proc_entry(prop->name, dn->pde);
294                 prop = prop->next;
295         }
296
297         if (dn->pde)
298                 remove_proc_entry(dn->pde->name, parent->pde);
299 #endif
300
301         blocking_notifier_call_chain(&pSeries_reconfig_chain,
302                             PSERIES_RECONFIG_REMOVE, dn);
303         of_detach_node(dn);
304         of_node_put(dn); /* Must decrement the refcount */
305
306         return 0;
307 }
308
309 #define DR_ENTITY_SENSE         9003
310 #define DR_ENTITY_PRESENT       1
311 #define DR_ENTITY_UNUSABLE      2
312 #define ALLOCATION_STATE        9003
313 #define ALLOC_UNUSABLE          0
314 #define ALLOC_USABLE            1
315 #define ISOLATION_STATE         9001
316 #define ISOLATE                 0
317 #define UNISOLATE               1
318
319 int dlpar_acquire_drc(u32 drc_index)
320 {
321         int dr_status, rc;
322
323         rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
324                        DR_ENTITY_SENSE, drc_index);
325         if (rc || dr_status != DR_ENTITY_UNUSABLE)
326                 return -1;
327
328         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
329         if (rc)
330                 return rc;
331
332         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
333         if (rc) {
334                 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
335                 return rc;
336         }
337
338         return 0;
339 }
340
341 int dlpar_release_drc(u32 drc_index)
342 {
343         int dr_status, rc;
344
345         rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
346                        DR_ENTITY_SENSE, drc_index);
347         if (rc || dr_status != DR_ENTITY_PRESENT)
348                 return -1;
349
350         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
351         if (rc)
352                 return rc;
353
354         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
355         if (rc) {
356                 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
357                 return rc;
358         }
359
360         return 0;
361 }
362
363 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
364
365 static int dlpar_online_cpu(struct device_node *dn)
366 {
367         int rc = 0;
368         unsigned int cpu;
369         int len, nthreads, i;
370         const u32 *intserv;
371
372         intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
373         if (!intserv)
374                 return -EINVAL;
375
376         nthreads = len / sizeof(u32);
377
378         cpu_maps_update_begin();
379         for (i = 0; i < nthreads; i++) {
380                 for_each_present_cpu(cpu) {
381                         if (get_hard_smp_processor_id(cpu) != intserv[i])
382                                 continue;
383                         BUG_ON(get_cpu_current_state(cpu)
384                                         != CPU_STATE_OFFLINE);
385                         cpu_maps_update_done();
386                         rc = cpu_up(cpu);
387                         if (rc)
388                                 goto out;
389                         cpu_maps_update_begin();
390
391                         break;
392                 }
393                 if (cpu == num_possible_cpus())
394                         printk(KERN_WARNING "Could not find cpu to online "
395                                "with physical id 0x%x\n", intserv[i]);
396         }
397         cpu_maps_update_done();
398
399 out:
400         return rc;
401
402 }
403
404 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
405 {
406         struct device_node *dn;
407         unsigned long drc_index;
408         char *cpu_name;
409         int rc;
410
411         cpu_hotplug_driver_lock();
412         rc = strict_strtoul(buf, 0, &drc_index);
413         if (rc) {
414                 rc = -EINVAL;
415                 goto out;
416         }
417
418         dn = dlpar_configure_connector(drc_index);
419         if (!dn) {
420                 rc = -EINVAL;
421                 goto out;
422         }
423
424         /* configure-connector reports cpus as living in the base
425          * directory of the device tree.  CPUs actually live in the
426          * cpus directory so we need to fixup the full_name.
427          */
428         cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
429         if (!cpu_name) {
430                 dlpar_free_cc_nodes(dn);
431                 rc = -ENOMEM;
432                 goto out;
433         }
434
435         kfree(dn->full_name);
436         dn->full_name = cpu_name;
437
438         rc = dlpar_acquire_drc(drc_index);
439         if (rc) {
440                 dlpar_free_cc_nodes(dn);
441                 rc = -EINVAL;
442                 goto out;
443         }
444
445         rc = dlpar_attach_node(dn);
446         if (rc) {
447                 dlpar_release_drc(drc_index);
448                 dlpar_free_cc_nodes(dn);
449                 goto out;
450         }
451
452         rc = dlpar_online_cpu(dn);
453 out:
454         cpu_hotplug_driver_unlock();
455
456         return rc ? rc : count;
457 }
458
459 static int dlpar_offline_cpu(struct device_node *dn)
460 {
461         int rc = 0;
462         unsigned int cpu;
463         int len, nthreads, i;
464         const u32 *intserv;
465
466         intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
467         if (!intserv)
468                 return -EINVAL;
469
470         nthreads = len / sizeof(u32);
471
472         cpu_maps_update_begin();
473         for (i = 0; i < nthreads; i++) {
474                 for_each_present_cpu(cpu) {
475                         if (get_hard_smp_processor_id(cpu) != intserv[i])
476                                 continue;
477
478                         if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
479                                 break;
480
481                         if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
482                                 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
483                                 cpu_maps_update_done();
484                                 rc = cpu_down(cpu);
485                                 if (rc)
486                                         goto out;
487                                 cpu_maps_update_begin();
488                                 break;
489
490                         }
491
492                         /*
493                          * The cpu is in CPU_STATE_INACTIVE.
494                          * Upgrade it's state to CPU_STATE_OFFLINE.
495                          */
496                         set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
497                         BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
498                                                                 != H_SUCCESS);
499                         __cpu_die(cpu);
500                         break;
501                 }
502                 if (cpu == num_possible_cpus())
503                         printk(KERN_WARNING "Could not find cpu to offline "
504                                "with physical id 0x%x\n", intserv[i]);
505         }
506         cpu_maps_update_done();
507
508 out:
509         return rc;
510
511 }
512
513 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
514 {
515         struct device_node *dn;
516         const u32 *drc_index;
517         int rc;
518
519         dn = of_find_node_by_path(buf);
520         if (!dn)
521                 return -EINVAL;
522
523         drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
524         if (!drc_index) {
525                 of_node_put(dn);
526                 return -EINVAL;
527         }
528
529         cpu_hotplug_driver_lock();
530         rc = dlpar_offline_cpu(dn);
531         if (rc) {
532                 of_node_put(dn);
533                 rc = -EINVAL;
534                 goto out;
535         }
536
537         rc = dlpar_release_drc(*drc_index);
538         if (rc) {
539                 of_node_put(dn);
540                 goto out;
541         }
542
543         rc = dlpar_detach_node(dn);
544         if (rc) {
545                 dlpar_acquire_drc(*drc_index);
546                 goto out;
547         }
548
549         of_node_put(dn);
550 out:
551         cpu_hotplug_driver_unlock();
552         return rc ? rc : count;
553 }
554
555 static int __init pseries_dlpar_init(void)
556 {
557         ppc_md.cpu_probe = dlpar_cpu_probe;
558         ppc_md.cpu_release = dlpar_cpu_release;
559
560         return 0;
561 }
562 machine_device_initcall(pseries, pseries_dlpar_init);
563
564 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */