GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / powerpc / platforms / pseries / hotplug-cpu.c
1 /*
2  * pseries CPU Hotplug infrastructure.
3  *
4  * Split out from arch/powerpc/platforms/pseries/setup.c
5  *  arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
6  *
7  * Peter Bergner, IBM   March 2001.
8  * Copyright (C) 2001 IBM.
9  * Dave Engebretsen, Peter Bergner, and
10  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
11  * Plus various changes from other IBM teams...
12  *
13  * Copyright (C) 2006 Michael Ellerman, IBM Corporation
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  */
20
21 #define pr_fmt(fmt)     "pseries-hotplug-cpu: " fmt
22
23 #include <linux/kernel.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/sched.h>        /* for idle_task_exit */
27 #include <linux/sched/hotplug.h>
28 #include <linux/cpu.h>
29 #include <linux/of.h>
30 #include <linux/slab.h>
31 #include <asm/prom.h>
32 #include <asm/rtas.h>
33 #include <asm/firmware.h>
34 #include <asm/machdep.h>
35 #include <asm/vdso_datapage.h>
36 #include <asm/xics.h>
37 #include <asm/xive.h>
38 #include <asm/plpar_wrappers.h>
39 #include <asm/topology.h>
40
41 #include "pseries.h"
42 #include "offline_states.h"
43
44 /* This version can't take the spinlock, because it never returns */
45 static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
46
47 static DEFINE_PER_CPU(enum cpu_state_vals, preferred_offline_state) =
48                                                         CPU_STATE_OFFLINE;
49 static DEFINE_PER_CPU(enum cpu_state_vals, current_state) = CPU_STATE_OFFLINE;
50
51 static enum cpu_state_vals default_offline_state = CPU_STATE_OFFLINE;
52
53 static bool cede_offline_enabled __read_mostly = true;
54
55 /*
56  * Enable/disable cede_offline when available.
57  */
58 static int __init setup_cede_offline(char *str)
59 {
60         return (kstrtobool(str, &cede_offline_enabled) == 0);
61 }
62
63 __setup("cede_offline=", setup_cede_offline);
64
65 enum cpu_state_vals get_cpu_current_state(int cpu)
66 {
67         return per_cpu(current_state, cpu);
68 }
69
70 void set_cpu_current_state(int cpu, enum cpu_state_vals state)
71 {
72         per_cpu(current_state, cpu) = state;
73 }
74
75 enum cpu_state_vals get_preferred_offline_state(int cpu)
76 {
77         return per_cpu(preferred_offline_state, cpu);
78 }
79
80 void set_preferred_offline_state(int cpu, enum cpu_state_vals state)
81 {
82         per_cpu(preferred_offline_state, cpu) = state;
83 }
84
85 void set_default_offline_state(int cpu)
86 {
87         per_cpu(preferred_offline_state, cpu) = default_offline_state;
88 }
89
90 static void rtas_stop_self(void)
91 {
92         static struct rtas_args args;
93
94         local_irq_disable();
95
96         BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
97
98         rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
99
100         panic("Alas, I survived.\n");
101 }
102
103 static void pseries_mach_cpu_die(void)
104 {
105         unsigned int cpu = smp_processor_id();
106         unsigned int hwcpu = hard_smp_processor_id();
107         u8 cede_latency_hint = 0;
108
109         local_irq_disable();
110         idle_task_exit();
111         if (xive_enabled())
112                 xive_teardown_cpu();
113         else
114                 xics_teardown_cpu();
115
116         if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
117                 set_cpu_current_state(cpu, CPU_STATE_INACTIVE);
118                 if (ppc_md.suspend_disable_cpu)
119                         ppc_md.suspend_disable_cpu();
120
121                 cede_latency_hint = 2;
122
123                 get_lppaca()->idle = 1;
124                 if (!lppaca_shared_proc(get_lppaca()))
125                         get_lppaca()->donate_dedicated_cpu = 1;
126
127                 while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
128                         while (!prep_irq_for_idle()) {
129                                 local_irq_enable();
130                                 local_irq_disable();
131                         }
132
133                         extended_cede_processor(cede_latency_hint);
134                 }
135
136                 local_irq_disable();
137
138                 if (!lppaca_shared_proc(get_lppaca()))
139                         get_lppaca()->donate_dedicated_cpu = 0;
140                 get_lppaca()->idle = 0;
141
142                 if (get_preferred_offline_state(cpu) == CPU_STATE_ONLINE) {
143                         unregister_slb_shadow(hwcpu);
144
145                         hard_irq_disable();
146                         /*
147                          * Call to start_secondary_resume() will not return.
148                          * Kernel stack will be reset and start_secondary()
149                          * will be called to continue the online operation.
150                          */
151                         start_secondary_resume();
152                 }
153         }
154
155         /* Requested state is CPU_STATE_OFFLINE at this point */
156         WARN_ON(get_preferred_offline_state(cpu) != CPU_STATE_OFFLINE);
157
158         set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
159         unregister_slb_shadow(hwcpu);
160         rtas_stop_self();
161
162         /* Should never get here... */
163         BUG();
164         for(;;);
165 }
166
167 static int pseries_cpu_disable(void)
168 {
169         int cpu = smp_processor_id();
170
171         set_cpu_online(cpu, false);
172         vdso_data->processorCount--;
173
174         /*fix boot_cpuid here*/
175         if (cpu == boot_cpuid)
176                 boot_cpuid = cpumask_any(cpu_online_mask);
177
178         /* FIXME: abstract this to not be platform specific later on */
179         if (xive_enabled())
180                 xive_smp_disable_cpu();
181         else
182                 xics_migrate_irqs_away();
183         return 0;
184 }
185
186 /*
187  * pseries_cpu_die: Wait for the cpu to die.
188  * @cpu: logical processor id of the CPU whose death we're awaiting.
189  *
190  * This function is called from the context of the thread which is performing
191  * the cpu-offline. Here we wait for long enough to allow the cpu in question
192  * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
193  * notifications.
194  *
195  * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
196  * self-destruct.
197  */
198 static void pseries_cpu_die(unsigned int cpu)
199 {
200         int tries;
201         int cpu_status = 1;
202         unsigned int pcpu = get_hard_smp_processor_id(cpu);
203
204         if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
205                 cpu_status = 1;
206                 for (tries = 0; tries < 5000; tries++) {
207                         if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
208                                 cpu_status = 0;
209                                 break;
210                         }
211                         msleep(1);
212                 }
213         } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
214
215                 for (tries = 0; tries < 25; tries++) {
216                         cpu_status = smp_query_cpu_stopped(pcpu);
217                         if (cpu_status == QCSS_STOPPED ||
218                             cpu_status == QCSS_HARDWARE_ERROR)
219                                 break;
220                         cpu_relax();
221                 }
222         }
223
224         if (cpu_status != 0) {
225                 printk("Querying DEAD? cpu %i (%i) shows %i\n",
226                        cpu, pcpu, cpu_status);
227         }
228
229         /* Isolation and deallocation are definitely done by
230          * drslot_chrp_cpu.  If they were not they would be
231          * done here.  Change isolate state to Isolate and
232          * change allocation-state to Unusable.
233          */
234         paca[cpu].cpu_start = 0;
235 }
236
237 /*
238  * Update cpu_present_mask and paca(s) for a new cpu node.  The wrinkle
239  * here is that a cpu device node may represent up to two logical cpus
240  * in the SMT case.  We must honor the assumption in other code that
241  * the logical ids for sibling SMT threads x and y are adjacent, such
242  * that x^1 == y and y^1 == x.
243  */
244 static int pseries_add_processor(struct device_node *np)
245 {
246         unsigned int cpu;
247         cpumask_var_t candidate_mask, tmp;
248         int err = -ENOSPC, len, nthreads, i;
249         const __be32 *intserv;
250
251         intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
252         if (!intserv)
253                 return 0;
254
255         zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
256         zalloc_cpumask_var(&tmp, GFP_KERNEL);
257
258         nthreads = len / sizeof(u32);
259         for (i = 0; i < nthreads; i++)
260                 cpumask_set_cpu(i, tmp);
261
262         cpu_maps_update_begin();
263
264         BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
265
266         /* Get a bitmap of unoccupied slots. */
267         cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
268         if (cpumask_empty(candidate_mask)) {
269                 /* If we get here, it most likely means that NR_CPUS is
270                  * less than the partition's max processors setting.
271                  */
272                 printk(KERN_ERR "Cannot add cpu %pOF; this system configuration"
273                        " supports %d logical cpus.\n", np,
274                        num_possible_cpus());
275                 goto out_unlock;
276         }
277
278         while (!cpumask_empty(tmp))
279                 if (cpumask_subset(tmp, candidate_mask))
280                         /* Found a range where we can insert the new cpu(s) */
281                         break;
282                 else
283                         cpumask_shift_left(tmp, tmp, nthreads);
284
285         if (cpumask_empty(tmp)) {
286                 printk(KERN_ERR "Unable to find space in cpu_present_mask for"
287                        " processor %s with %d thread(s)\n", np->name,
288                        nthreads);
289                 goto out_unlock;
290         }
291
292         for_each_cpu(cpu, tmp) {
293                 BUG_ON(cpu_present(cpu));
294                 set_cpu_present(cpu, true);
295                 set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
296         }
297         err = 0;
298 out_unlock:
299         cpu_maps_update_done();
300         free_cpumask_var(candidate_mask);
301         free_cpumask_var(tmp);
302         return err;
303 }
304
305 /*
306  * Update the present map for a cpu node which is going away, and set
307  * the hard id in the paca(s) to -1 to be consistent with boot time
308  * convention for non-present cpus.
309  */
310 static void pseries_remove_processor(struct device_node *np)
311 {
312         unsigned int cpu;
313         int len, nthreads, i;
314         const __be32 *intserv;
315         u32 thread;
316
317         intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
318         if (!intserv)
319                 return;
320
321         nthreads = len / sizeof(u32);
322
323         cpu_maps_update_begin();
324         for (i = 0; i < nthreads; i++) {
325                 thread = be32_to_cpu(intserv[i]);
326                 for_each_present_cpu(cpu) {
327                         if (get_hard_smp_processor_id(cpu) != thread)
328                                 continue;
329                         BUG_ON(cpu_online(cpu));
330                         set_cpu_present(cpu, false);
331                         set_hard_smp_processor_id(cpu, -1);
332                         update_numa_cpu_lookup_table(cpu, -1);
333                         break;
334                 }
335                 if (cpu >= nr_cpu_ids)
336                         printk(KERN_WARNING "Could not find cpu to remove "
337                                "with physical id 0x%x\n", thread);
338         }
339         cpu_maps_update_done();
340 }
341
342 static int dlpar_online_cpu(struct device_node *dn)
343 {
344         int rc = 0;
345         unsigned int cpu;
346         int len, nthreads, i;
347         const __be32 *intserv;
348         u32 thread;
349
350         intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
351         if (!intserv)
352                 return -EINVAL;
353
354         nthreads = len / sizeof(u32);
355
356         cpu_maps_update_begin();
357         for (i = 0; i < nthreads; i++) {
358                 thread = be32_to_cpu(intserv[i]);
359                 for_each_present_cpu(cpu) {
360                         if (get_hard_smp_processor_id(cpu) != thread)
361                                 continue;
362                         BUG_ON(get_cpu_current_state(cpu)
363                                         != CPU_STATE_OFFLINE);
364                         cpu_maps_update_done();
365                         rc = device_online(get_cpu_device(cpu));
366                         if (rc)
367                                 goto out;
368                         cpu_maps_update_begin();
369
370                         break;
371                 }
372                 if (cpu == num_possible_cpus())
373                         printk(KERN_WARNING "Could not find cpu to online "
374                                "with physical id 0x%x\n", thread);
375         }
376         cpu_maps_update_done();
377
378 out:
379         return rc;
380
381 }
382
383 static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
384 {
385         struct device_node *child = NULL;
386         u32 my_drc_index;
387         bool found;
388         int rc;
389
390         /* Assume cpu doesn't exist */
391         found = false;
392
393         for_each_child_of_node(parent, child) {
394                 rc = of_property_read_u32(child, "ibm,my-drc-index",
395                                           &my_drc_index);
396                 if (rc)
397                         continue;
398
399                 if (my_drc_index == drc_index) {
400                         of_node_put(child);
401                         found = true;
402                         break;
403                 }
404         }
405
406         return found;
407 }
408
409 static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
410 {
411         bool found = false;
412         int rc, index;
413
414         index = 0;
415         while (!found) {
416                 u32 drc;
417
418                 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
419                                                 index++, &drc);
420                 if (rc)
421                         break;
422
423                 if (drc == drc_index)
424                         found = true;
425         }
426
427         return found;
428 }
429
430 static ssize_t dlpar_cpu_add(u32 drc_index)
431 {
432         struct device_node *dn, *parent;
433         int rc, saved_rc;
434
435         pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
436
437         parent = of_find_node_by_path("/cpus");
438         if (!parent) {
439                 pr_warn("Failed to find CPU root node \"/cpus\"\n");
440                 return -ENODEV;
441         }
442
443         if (dlpar_cpu_exists(parent, drc_index)) {
444                 of_node_put(parent);
445                 pr_warn("CPU with drc index %x already exists\n", drc_index);
446                 return -EINVAL;
447         }
448
449         if (!valid_cpu_drc_index(parent, drc_index)) {
450                 of_node_put(parent);
451                 pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
452                 return -EINVAL;
453         }
454
455         rc = dlpar_acquire_drc(drc_index);
456         if (rc) {
457                 pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
458                         rc, drc_index);
459                 of_node_put(parent);
460                 return -EINVAL;
461         }
462
463         dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
464         if (!dn) {
465                 pr_warn("Failed call to configure-connector, drc index: %x\n",
466                         drc_index);
467                 dlpar_release_drc(drc_index);
468                 of_node_put(parent);
469                 return -EINVAL;
470         }
471
472         rc = dlpar_attach_node(dn, parent);
473
474         /* Regardless we are done with parent now */
475         of_node_put(parent);
476
477         if (rc) {
478                 saved_rc = rc;
479                 pr_warn("Failed to attach node %s, rc: %d, drc index: %x\n",
480                         dn->name, rc, drc_index);
481
482                 rc = dlpar_release_drc(drc_index);
483                 if (!rc)
484                         dlpar_free_cc_nodes(dn);
485
486                 return saved_rc;
487         }
488
489         rc = dlpar_online_cpu(dn);
490         if (rc) {
491                 saved_rc = rc;
492                 pr_warn("Failed to online cpu %s, rc: %d, drc index: %x\n",
493                         dn->name, rc, drc_index);
494
495                 rc = dlpar_detach_node(dn);
496                 if (!rc)
497                         dlpar_release_drc(drc_index);
498
499                 return saved_rc;
500         }
501
502         pr_debug("Successfully added CPU %s, drc index: %x\n", dn->name,
503                  drc_index);
504         return rc;
505 }
506
507 static int dlpar_offline_cpu(struct device_node *dn)
508 {
509         int rc = 0;
510         unsigned int cpu;
511         int len, nthreads, i;
512         const __be32 *intserv;
513         u32 thread;
514
515         intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
516         if (!intserv)
517                 return -EINVAL;
518
519         nthreads = len / sizeof(u32);
520
521         cpu_maps_update_begin();
522         for (i = 0; i < nthreads; i++) {
523                 thread = be32_to_cpu(intserv[i]);
524                 for_each_present_cpu(cpu) {
525                         if (get_hard_smp_processor_id(cpu) != thread)
526                                 continue;
527
528                         if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
529                                 break;
530
531                         if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
532                                 set_preferred_offline_state(cpu,
533                                                             CPU_STATE_OFFLINE);
534                                 cpu_maps_update_done();
535                                 rc = device_offline(get_cpu_device(cpu));
536                                 if (rc)
537                                         goto out;
538                                 cpu_maps_update_begin();
539                                 break;
540
541                         }
542
543                         /*
544                          * The cpu is in CPU_STATE_INACTIVE.
545                          * Upgrade it's state to CPU_STATE_OFFLINE.
546                          */
547                         set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
548                         BUG_ON(plpar_hcall_norets(H_PROD, thread)
549                                                                 != H_SUCCESS);
550                         __cpu_die(cpu);
551                         break;
552                 }
553                 if (cpu == num_possible_cpus())
554                         printk(KERN_WARNING "Could not find cpu to offline with physical id 0x%x\n", thread);
555         }
556         cpu_maps_update_done();
557
558 out:
559         return rc;
560
561 }
562
563 static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
564 {
565         int rc;
566
567         pr_debug("Attempting to remove CPU %s, drc index: %x\n",
568                  dn->name, drc_index);
569
570         rc = dlpar_offline_cpu(dn);
571         if (rc) {
572                 pr_warn("Failed to offline CPU %s, rc: %d\n", dn->name, rc);
573                 return -EINVAL;
574         }
575
576         rc = dlpar_release_drc(drc_index);
577         if (rc) {
578                 pr_warn("Failed to release drc (%x) for CPU %s, rc: %d\n",
579                         drc_index, dn->name, rc);
580                 dlpar_online_cpu(dn);
581                 return rc;
582         }
583
584         rc = dlpar_detach_node(dn);
585         if (rc) {
586                 int saved_rc = rc;
587
588                 pr_warn("Failed to detach CPU %s, rc: %d", dn->name, rc);
589
590                 rc = dlpar_acquire_drc(drc_index);
591                 if (!rc)
592                         dlpar_online_cpu(dn);
593
594                 return saved_rc;
595         }
596
597         pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
598         return 0;
599 }
600
601 static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
602 {
603         struct device_node *dn;
604         u32 my_index;
605         int rc;
606
607         for_each_node_by_type(dn, "cpu") {
608                 rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
609                 if (rc)
610                         continue;
611
612                 if (my_index == drc_index)
613                         break;
614         }
615
616         return dn;
617 }
618
619 static int dlpar_cpu_remove_by_index(u32 drc_index)
620 {
621         struct device_node *dn;
622         int rc;
623
624         dn = cpu_drc_index_to_dn(drc_index);
625         if (!dn) {
626                 pr_warn("Cannot find CPU (drc index %x) to remove\n",
627                         drc_index);
628                 return -ENODEV;
629         }
630
631         rc = dlpar_cpu_remove(dn, drc_index);
632         of_node_put(dn);
633         return rc;
634 }
635
636 static int find_dlpar_cpus_to_remove(u32 *cpu_drcs, int cpus_to_remove)
637 {
638         struct device_node *dn;
639         int cpus_found = 0;
640         int rc;
641
642         /* We want to find cpus_to_remove + 1 CPUs to ensure we do not
643          * remove the last CPU.
644          */
645         for_each_node_by_type(dn, "cpu") {
646                 cpus_found++;
647
648                 if (cpus_found > cpus_to_remove) {
649                         of_node_put(dn);
650                         break;
651                 }
652
653                 /* Note that cpus_found is always 1 ahead of the index
654                  * into the cpu_drcs array, so we use cpus_found - 1
655                  */
656                 rc = of_property_read_u32(dn, "ibm,my-drc-index",
657                                           &cpu_drcs[cpus_found - 1]);
658                 if (rc) {
659                         pr_warn("Error occurred getting drc-index for %s\n",
660                                 dn->name);
661                         of_node_put(dn);
662                         return -1;
663                 }
664         }
665
666         if (cpus_found < cpus_to_remove) {
667                 pr_warn("Failed to find enough CPUs (%d of %d) to remove\n",
668                         cpus_found, cpus_to_remove);
669         } else if (cpus_found == cpus_to_remove) {
670                 pr_warn("Cannot remove all CPUs\n");
671         }
672
673         return cpus_found;
674 }
675
676 static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
677 {
678         u32 *cpu_drcs;
679         int cpus_found;
680         int cpus_removed = 0;
681         int i, rc;
682
683         pr_debug("Attempting to hot-remove %d CPUs\n", cpus_to_remove);
684
685         cpu_drcs = kcalloc(cpus_to_remove, sizeof(*cpu_drcs), GFP_KERNEL);
686         if (!cpu_drcs)
687                 return -EINVAL;
688
689         cpus_found = find_dlpar_cpus_to_remove(cpu_drcs, cpus_to_remove);
690         if (cpus_found <= cpus_to_remove) {
691                 kfree(cpu_drcs);
692                 return -EINVAL;
693         }
694
695         for (i = 0; i < cpus_to_remove; i++) {
696                 rc = dlpar_cpu_remove_by_index(cpu_drcs[i]);
697                 if (rc)
698                         break;
699
700                 cpus_removed++;
701         }
702
703         if (cpus_removed != cpus_to_remove) {
704                 pr_warn("CPU hot-remove failed, adding back removed CPUs\n");
705
706                 for (i = 0; i < cpus_removed; i++)
707                         dlpar_cpu_add(cpu_drcs[i]);
708
709                 rc = -EINVAL;
710         } else {
711                 rc = 0;
712         }
713
714         kfree(cpu_drcs);
715         return rc;
716 }
717
718 static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
719 {
720         struct device_node *parent;
721         int cpus_found = 0;
722         int index, rc;
723
724         parent = of_find_node_by_path("/cpus");
725         if (!parent) {
726                 pr_warn("Could not find CPU root node in device tree\n");
727                 kfree(cpu_drcs);
728                 return -1;
729         }
730
731         /* Search the ibm,drc-indexes array for possible CPU drcs to
732          * add. Note that the format of the ibm,drc-indexes array is
733          * the number of entries in the array followed by the array
734          * of drc values so we start looking at index = 1.
735          */
736         index = 1;
737         while (cpus_found < cpus_to_add) {
738                 u32 drc;
739
740                 rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
741                                                 index++, &drc);
742                 if (rc)
743                         break;
744
745                 if (dlpar_cpu_exists(parent, drc))
746                         continue;
747
748                 cpu_drcs[cpus_found++] = drc;
749         }
750
751         of_node_put(parent);
752         return cpus_found;
753 }
754
755 static int dlpar_cpu_add_by_count(u32 cpus_to_add)
756 {
757         u32 *cpu_drcs;
758         int cpus_added = 0;
759         int cpus_found;
760         int i, rc;
761
762         pr_debug("Attempting to hot-add %d CPUs\n", cpus_to_add);
763
764         cpu_drcs = kcalloc(cpus_to_add, sizeof(*cpu_drcs), GFP_KERNEL);
765         if (!cpu_drcs)
766                 return -EINVAL;
767
768         cpus_found = find_dlpar_cpus_to_add(cpu_drcs, cpus_to_add);
769         if (cpus_found < cpus_to_add) {
770                 pr_warn("Failed to find enough CPUs (%d of %d) to add\n",
771                         cpus_found, cpus_to_add);
772                 kfree(cpu_drcs);
773                 return -EINVAL;
774         }
775
776         for (i = 0; i < cpus_to_add; i++) {
777                 rc = dlpar_cpu_add(cpu_drcs[i]);
778                 if (rc)
779                         break;
780
781                 cpus_added++;
782         }
783
784         if (cpus_added < cpus_to_add) {
785                 pr_warn("CPU hot-add failed, removing any added CPUs\n");
786
787                 for (i = 0; i < cpus_added; i++)
788                         dlpar_cpu_remove_by_index(cpu_drcs[i]);
789
790                 rc = -EINVAL;
791         } else {
792                 rc = 0;
793         }
794
795         kfree(cpu_drcs);
796         return rc;
797 }
798
799 int dlpar_cpu_readd(int cpu)
800 {
801         struct device_node *dn;
802         struct device *dev;
803         u32 drc_index;
804         int rc;
805
806         dev = get_cpu_device(cpu);
807         dn = dev->of_node;
808
809         rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
810
811         rc = dlpar_cpu_remove_by_index(drc_index);
812         if (!rc)
813                 rc = dlpar_cpu_add(drc_index);
814
815         return rc;
816 }
817
818 int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
819 {
820         u32 count, drc_index;
821         int rc;
822
823         count = hp_elog->_drc_u.drc_count;
824         drc_index = hp_elog->_drc_u.drc_index;
825
826         lock_device_hotplug();
827
828         switch (hp_elog->action) {
829         case PSERIES_HP_ELOG_ACTION_REMOVE:
830                 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
831                         rc = dlpar_cpu_remove_by_count(count);
832                 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
833                         rc = dlpar_cpu_remove_by_index(drc_index);
834                 else
835                         rc = -EINVAL;
836                 break;
837         case PSERIES_HP_ELOG_ACTION_ADD:
838                 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
839                         rc = dlpar_cpu_add_by_count(count);
840                 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
841                         rc = dlpar_cpu_add(drc_index);
842                 else
843                         rc = -EINVAL;
844                 break;
845         default:
846                 pr_err("Invalid action (%d) specified\n", hp_elog->action);
847                 rc = -EINVAL;
848                 break;
849         }
850
851         unlock_device_hotplug();
852         return rc;
853 }
854
855 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
856
857 static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
858 {
859         u32 drc_index;
860         int rc;
861
862         rc = kstrtou32(buf, 0, &drc_index);
863         if (rc)
864                 return -EINVAL;
865
866         rc = dlpar_cpu_add(drc_index);
867
868         return rc ? rc : count;
869 }
870
871 static ssize_t dlpar_cpu_release(const char *buf, size_t count)
872 {
873         struct device_node *dn;
874         u32 drc_index;
875         int rc;
876
877         dn = of_find_node_by_path(buf);
878         if (!dn)
879                 return -EINVAL;
880
881         rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
882         if (rc) {
883                 of_node_put(dn);
884                 return -EINVAL;
885         }
886
887         rc = dlpar_cpu_remove(dn, drc_index);
888         of_node_put(dn);
889
890         return rc ? rc : count;
891 }
892
893 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
894
895 static int pseries_smp_notifier(struct notifier_block *nb,
896                                 unsigned long action, void *data)
897 {
898         struct of_reconfig_data *rd = data;
899         int err = 0;
900
901         switch (action) {
902         case OF_RECONFIG_ATTACH_NODE:
903                 err = pseries_add_processor(rd->dn);
904                 break;
905         case OF_RECONFIG_DETACH_NODE:
906                 pseries_remove_processor(rd->dn);
907                 break;
908         }
909         return notifier_from_errno(err);
910 }
911
912 static struct notifier_block pseries_smp_nb = {
913         .notifier_call = pseries_smp_notifier,
914 };
915
916 #define MAX_CEDE_LATENCY_LEVELS         4
917 #define CEDE_LATENCY_PARAM_LENGTH       10
918 #define CEDE_LATENCY_PARAM_MAX_LENGTH   \
919         (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
920 #define CEDE_LATENCY_TOKEN              45
921
922 static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
923
924 static int parse_cede_parameters(void)
925 {
926         memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
927         return rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
928                          NULL,
929                          CEDE_LATENCY_TOKEN,
930                          __pa(cede_parameters),
931                          CEDE_LATENCY_PARAM_MAX_LENGTH);
932 }
933
934 static int __init pseries_cpu_hotplug_init(void)
935 {
936         int cpu;
937         int qcss_tok;
938
939 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
940         ppc_md.cpu_probe = dlpar_cpu_probe;
941         ppc_md.cpu_release = dlpar_cpu_release;
942 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
943
944         rtas_stop_self_token = rtas_token("stop-self");
945         qcss_tok = rtas_token("query-cpu-stopped-state");
946
947         if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
948                         qcss_tok == RTAS_UNKNOWN_SERVICE) {
949                 printk(KERN_INFO "CPU Hotplug not supported by firmware "
950                                 "- disabling.\n");
951                 return 0;
952         }
953
954         ppc_md.cpu_die = pseries_mach_cpu_die;
955         smp_ops->cpu_disable = pseries_cpu_disable;
956         smp_ops->cpu_die = pseries_cpu_die;
957
958         /* Processors can be added/removed only on LPAR */
959         if (firmware_has_feature(FW_FEATURE_LPAR)) {
960                 of_reconfig_notifier_register(&pseries_smp_nb);
961                 cpu_maps_update_begin();
962                 if (cede_offline_enabled && parse_cede_parameters() == 0) {
963                         default_offline_state = CPU_STATE_INACTIVE;
964                         for_each_online_cpu(cpu)
965                                 set_default_offline_state(cpu);
966                 }
967                 cpu_maps_update_done();
968         }
969
970         return 0;
971 }
972 machine_arch_initcall(pseries, pseries_cpu_hotplug_init);