s390/idle: consolidate idle functions and definitions
[firefly-linux-kernel-4.4.55.git] / arch / s390 / kernel / smp.c
1 /*
2  *  SMP related functions
3  *
4  *    Copyright IBM Corp. 1999, 2012
5  *    Author(s): Denis Joseph Barrow,
6  *               Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *               Heiko Carstens <heiko.carstens@de.ibm.com>,
8  *
9  *  based on other smp stuff by
10  *    (c) 1995 Alan Cox, CymruNET Ltd  <alan@cymru.net>
11  *    (c) 1998 Ingo Molnar
12  *
13  * The code outside of smp.c uses logical cpu numbers, only smp.c does
14  * the translation of logical to physical cpu ids. All new code that
15  * operates on physical cpu numbers needs to go into smp.c.
16  */
17
18 #define KMSG_COMPONENT "cpu"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21 #include <linux/workqueue.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/mm.h>
25 #include <linux/err.h>
26 #include <linux/spinlock.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/irqflags.h>
31 #include <linux/cpu.h>
32 #include <linux/slab.h>
33 #include <linux/crash_dump.h>
34 #include <asm/asm-offsets.h>
35 #include <asm/switch_to.h>
36 #include <asm/facility.h>
37 #include <asm/ipl.h>
38 #include <asm/setup.h>
39 #include <asm/irq.h>
40 #include <asm/tlbflush.h>
41 #include <asm/vtimer.h>
42 #include <asm/lowcore.h>
43 #include <asm/sclp.h>
44 #include <asm/vdso.h>
45 #include <asm/debug.h>
46 #include <asm/os_info.h>
47 #include <asm/sigp.h>
48 #include <asm/idle.h>
49 #include "entry.h"
50
51 enum {
52         ec_schedule = 0,
53         ec_call_function_single,
54         ec_stop_cpu,
55 };
56
57 enum {
58         CPU_STATE_STANDBY,
59         CPU_STATE_CONFIGURED,
60 };
61
62 struct pcpu {
63         struct cpu *cpu;
64         struct _lowcore *lowcore;       /* lowcore page(s) for the cpu */
65         unsigned long async_stack;      /* async stack for the cpu */
66         unsigned long panic_stack;      /* panic stack for the cpu */
67         unsigned long ec_mask;          /* bit mask for ec_xxx functions */
68         int state;                      /* physical cpu state */
69         int polarization;               /* physical polarization */
70         u16 address;                    /* physical cpu address */
71 };
72
73 static u8 boot_cpu_type;
74 static u16 boot_cpu_address;
75 static struct pcpu pcpu_devices[NR_CPUS];
76
77 /*
78  * The smp_cpu_state_mutex must be held when changing the state or polarization
79  * member of a pcpu data structure within the pcpu_devices arreay.
80  */
81 DEFINE_MUTEX(smp_cpu_state_mutex);
82
83 /*
84  * Signal processor helper functions.
85  */
86 static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
87 {
88         int cc;
89
90         while (1) {
91                 cc = __pcpu_sigp(addr, order, parm, NULL);
92                 if (cc != SIGP_CC_BUSY)
93                         return cc;
94                 cpu_relax();
95         }
96 }
97
98 static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
99 {
100         int cc, retry;
101
102         for (retry = 0; ; retry++) {
103                 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
104                 if (cc != SIGP_CC_BUSY)
105                         break;
106                 if (retry >= 3)
107                         udelay(10);
108         }
109         return cc;
110 }
111
112 static inline int pcpu_stopped(struct pcpu *pcpu)
113 {
114         u32 uninitialized_var(status);
115
116         if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
117                         0, &status) != SIGP_CC_STATUS_STORED)
118                 return 0;
119         return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
120 }
121
122 static inline int pcpu_running(struct pcpu *pcpu)
123 {
124         if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
125                         0, NULL) != SIGP_CC_STATUS_STORED)
126                 return 1;
127         /* Status stored condition code is equivalent to cpu not running. */
128         return 0;
129 }
130
131 /*
132  * Find struct pcpu by cpu address.
133  */
134 static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
135 {
136         int cpu;
137
138         for_each_cpu(cpu, mask)
139                 if (pcpu_devices[cpu].address == address)
140                         return pcpu_devices + cpu;
141         return NULL;
142 }
143
144 static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
145 {
146         int order;
147
148         if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
149                 return;
150         order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
151         pcpu_sigp_retry(pcpu, order, 0);
152 }
153
154 static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
155 {
156         struct _lowcore *lc;
157
158         if (pcpu != &pcpu_devices[0]) {
159                 pcpu->lowcore = (struct _lowcore *)
160                         __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
161                 pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
162                 pcpu->panic_stack = __get_free_page(GFP_KERNEL);
163                 if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
164                         goto out;
165         }
166         lc = pcpu->lowcore;
167         memcpy(lc, &S390_lowcore, 512);
168         memset((char *) lc + 512, 0, sizeof(*lc) - 512);
169         lc->async_stack = pcpu->async_stack + ASYNC_SIZE
170                 - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
171         lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
172                 - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
173         lc->cpu_nr = cpu;
174         lc->spinlock_lockval = arch_spin_lockval(cpu);
175 #ifndef CONFIG_64BIT
176         if (MACHINE_HAS_IEEE) {
177                 lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
178                 if (!lc->extended_save_area_addr)
179                         goto out;
180         }
181 #else
182         if (vdso_alloc_per_cpu(lc))
183                 goto out;
184 #endif
185         lowcore_ptr[cpu] = lc;
186         pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
187         return 0;
188 out:
189         if (pcpu != &pcpu_devices[0]) {
190                 free_page(pcpu->panic_stack);
191                 free_pages(pcpu->async_stack, ASYNC_ORDER);
192                 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
193         }
194         return -ENOMEM;
195 }
196
197 #ifdef CONFIG_HOTPLUG_CPU
198
199 static void pcpu_free_lowcore(struct pcpu *pcpu)
200 {
201         pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
202         lowcore_ptr[pcpu - pcpu_devices] = NULL;
203 #ifndef CONFIG_64BIT
204         if (MACHINE_HAS_IEEE) {
205                 struct _lowcore *lc = pcpu->lowcore;
206
207                 free_page((unsigned long) lc->extended_save_area_addr);
208                 lc->extended_save_area_addr = 0;
209         }
210 #else
211         vdso_free_per_cpu(pcpu->lowcore);
212 #endif
213         if (pcpu != &pcpu_devices[0]) {
214                 free_page(pcpu->panic_stack);
215                 free_pages(pcpu->async_stack, ASYNC_ORDER);
216                 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
217         }
218 }
219
220 #endif /* CONFIG_HOTPLUG_CPU */
221
222 static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
223 {
224         struct _lowcore *lc = pcpu->lowcore;
225
226         if (MACHINE_HAS_TLB_LC)
227                 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
228         cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
229         atomic_inc(&init_mm.context.attach_count);
230         lc->cpu_nr = cpu;
231         lc->spinlock_lockval = arch_spin_lockval(cpu);
232         lc->percpu_offset = __per_cpu_offset[cpu];
233         lc->kernel_asce = S390_lowcore.kernel_asce;
234         lc->machine_flags = S390_lowcore.machine_flags;
235         lc->ftrace_func = S390_lowcore.ftrace_func;
236         lc->user_timer = lc->system_timer = lc->steal_timer = 0;
237         __ctl_store(lc->cregs_save_area, 0, 15);
238         save_access_regs((unsigned int *) lc->access_regs_save_area);
239         memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
240                MAX_FACILITY_BIT/8);
241 }
242
243 static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
244 {
245         struct _lowcore *lc = pcpu->lowcore;
246         struct thread_info *ti = task_thread_info(tsk);
247
248         lc->kernel_stack = (unsigned long) task_stack_page(tsk)
249                 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
250         lc->thread_info = (unsigned long) task_thread_info(tsk);
251         lc->current_task = (unsigned long) tsk;
252         lc->user_timer = ti->user_timer;
253         lc->system_timer = ti->system_timer;
254         lc->steal_timer = 0;
255 }
256
257 static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
258 {
259         struct _lowcore *lc = pcpu->lowcore;
260
261         lc->restart_stack = lc->kernel_stack;
262         lc->restart_fn = (unsigned long) func;
263         lc->restart_data = (unsigned long) data;
264         lc->restart_source = -1UL;
265         pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
266 }
267
268 /*
269  * Call function via PSW restart on pcpu and stop the current cpu.
270  */
271 static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
272                           void *data, unsigned long stack)
273 {
274         struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
275         unsigned long source_cpu = stap();
276
277         __load_psw_mask(PSW_KERNEL_BITS);
278         if (pcpu->address == source_cpu)
279                 func(data);     /* should not return */
280         /* Stop target cpu (if func returns this stops the current cpu). */
281         pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
282         /* Restart func on the target cpu and stop the current cpu. */
283         mem_assign_absolute(lc->restart_stack, stack);
284         mem_assign_absolute(lc->restart_fn, (unsigned long) func);
285         mem_assign_absolute(lc->restart_data, (unsigned long) data);
286         mem_assign_absolute(lc->restart_source, source_cpu);
287         asm volatile(
288                 "0:     sigp    0,%0,%2 # sigp restart to target cpu\n"
289                 "       brc     2,0b    # busy, try again\n"
290                 "1:     sigp    0,%1,%3 # sigp stop to current cpu\n"
291                 "       brc     2,1b    # busy, try again\n"
292                 : : "d" (pcpu->address), "d" (source_cpu),
293                     "K" (SIGP_RESTART), "K" (SIGP_STOP)
294                 : "0", "1", "cc");
295         for (;;) ;
296 }
297
298 /*
299  * Call function on an online CPU.
300  */
301 void smp_call_online_cpu(void (*func)(void *), void *data)
302 {
303         struct pcpu *pcpu;
304
305         /* Use the current cpu if it is online. */
306         pcpu = pcpu_find_address(cpu_online_mask, stap());
307         if (!pcpu)
308                 /* Use the first online cpu. */
309                 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
310         pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
311 }
312
313 /*
314  * Call function on the ipl CPU.
315  */
316 void smp_call_ipl_cpu(void (*func)(void *), void *data)
317 {
318         pcpu_delegate(&pcpu_devices[0], func, data,
319                       pcpu_devices->panic_stack + PAGE_SIZE);
320 }
321
322 int smp_find_processor_id(u16 address)
323 {
324         int cpu;
325
326         for_each_present_cpu(cpu)
327                 if (pcpu_devices[cpu].address == address)
328                         return cpu;
329         return -1;
330 }
331
332 int smp_vcpu_scheduled(int cpu)
333 {
334         return pcpu_running(pcpu_devices + cpu);
335 }
336
337 void smp_yield_cpu(int cpu)
338 {
339         if (MACHINE_HAS_DIAG9C)
340                 asm volatile("diag %0,0,0x9c"
341                              : : "d" (pcpu_devices[cpu].address));
342         else if (MACHINE_HAS_DIAG44)
343                 asm volatile("diag 0,0,0x44");
344 }
345
346 /*
347  * Send cpus emergency shutdown signal. This gives the cpus the
348  * opportunity to complete outstanding interrupts.
349  */
350 static void smp_emergency_stop(cpumask_t *cpumask)
351 {
352         u64 end;
353         int cpu;
354
355         end = get_tod_clock() + (1000000UL << 12);
356         for_each_cpu(cpu, cpumask) {
357                 struct pcpu *pcpu = pcpu_devices + cpu;
358                 set_bit(ec_stop_cpu, &pcpu->ec_mask);
359                 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
360                                    0, NULL) == SIGP_CC_BUSY &&
361                        get_tod_clock() < end)
362                         cpu_relax();
363         }
364         while (get_tod_clock() < end) {
365                 for_each_cpu(cpu, cpumask)
366                         if (pcpu_stopped(pcpu_devices + cpu))
367                                 cpumask_clear_cpu(cpu, cpumask);
368                 if (cpumask_empty(cpumask))
369                         break;
370                 cpu_relax();
371         }
372 }
373
374 /*
375  * Stop all cpus but the current one.
376  */
377 void smp_send_stop(void)
378 {
379         cpumask_t cpumask;
380         int cpu;
381
382         /* Disable all interrupts/machine checks */
383         __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
384         trace_hardirqs_off();
385
386         debug_set_critical();
387         cpumask_copy(&cpumask, cpu_online_mask);
388         cpumask_clear_cpu(smp_processor_id(), &cpumask);
389
390         if (oops_in_progress)
391                 smp_emergency_stop(&cpumask);
392
393         /* stop all processors */
394         for_each_cpu(cpu, &cpumask) {
395                 struct pcpu *pcpu = pcpu_devices + cpu;
396                 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
397                 while (!pcpu_stopped(pcpu))
398                         cpu_relax();
399         }
400 }
401
402 /*
403  * This is the main routine where commands issued by other
404  * cpus are handled.
405  */
406 static void smp_handle_ext_call(void)
407 {
408         unsigned long bits;
409
410         /* handle bit signal external calls */
411         bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
412         if (test_bit(ec_stop_cpu, &bits))
413                 smp_stop_cpu();
414         if (test_bit(ec_schedule, &bits))
415                 scheduler_ipi();
416         if (test_bit(ec_call_function_single, &bits))
417                 generic_smp_call_function_single_interrupt();
418 }
419
420 static void do_ext_call_interrupt(struct ext_code ext_code,
421                                   unsigned int param32, unsigned long param64)
422 {
423         inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
424         smp_handle_ext_call();
425 }
426
427 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
428 {
429         int cpu;
430
431         for_each_cpu(cpu, mask)
432                 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
433 }
434
435 void arch_send_call_function_single_ipi(int cpu)
436 {
437         pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
438 }
439
440 #ifndef CONFIG_64BIT
441 /*
442  * this function sends a 'purge tlb' signal to another CPU.
443  */
444 static void smp_ptlb_callback(void *info)
445 {
446         __tlb_flush_local();
447 }
448
449 void smp_ptlb_all(void)
450 {
451         on_each_cpu(smp_ptlb_callback, NULL, 1);
452 }
453 EXPORT_SYMBOL(smp_ptlb_all);
454 #endif /* ! CONFIG_64BIT */
455
456 /*
457  * this function sends a 'reschedule' IPI to another CPU.
458  * it goes straight through and wastes no time serializing
459  * anything. Worst case is that we lose a reschedule ...
460  */
461 void smp_send_reschedule(int cpu)
462 {
463         pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
464 }
465
466 /*
467  * parameter area for the set/clear control bit callbacks
468  */
469 struct ec_creg_mask_parms {
470         unsigned long orval;
471         unsigned long andval;
472         int cr;
473 };
474
475 /*
476  * callback for setting/clearing control bits
477  */
478 static void smp_ctl_bit_callback(void *info)
479 {
480         struct ec_creg_mask_parms *pp = info;
481         unsigned long cregs[16];
482
483         __ctl_store(cregs, 0, 15);
484         cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
485         __ctl_load(cregs, 0, 15);
486 }
487
488 /*
489  * Set a bit in a control register of all cpus
490  */
491 void smp_ctl_set_bit(int cr, int bit)
492 {
493         struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
494
495         on_each_cpu(smp_ctl_bit_callback, &parms, 1);
496 }
497 EXPORT_SYMBOL(smp_ctl_set_bit);
498
499 /*
500  * Clear a bit in a control register of all cpus
501  */
502 void smp_ctl_clear_bit(int cr, int bit)
503 {
504         struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
505
506         on_each_cpu(smp_ctl_bit_callback, &parms, 1);
507 }
508 EXPORT_SYMBOL(smp_ctl_clear_bit);
509
510 #ifdef CONFIG_CRASH_DUMP
511
512 static void __init smp_get_save_area(int cpu, u16 address)
513 {
514         void *lc = pcpu_devices[0].lowcore;
515         struct save_area *save_area;
516
517         if (is_kdump_kernel())
518                 return;
519         if (!OLDMEM_BASE && (address == boot_cpu_address ||
520                              ipl_info.type != IPL_TYPE_FCP_DUMP))
521                 return;
522         save_area = dump_save_area_create(cpu);
523         if (!save_area)
524                 panic("could not allocate memory for save area\n");
525         if (address == boot_cpu_address) {
526                 /* Copy the registers of the boot cpu. */
527                 copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
528                                  SAVE_AREA_BASE - PAGE_SIZE, 0);
529                 return;
530         }
531         /* Get the registers of a non-boot cpu. */
532         __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
533         memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
534 }
535
536 int smp_store_status(int cpu)
537 {
538         struct pcpu *pcpu;
539
540         pcpu = pcpu_devices + cpu;
541         if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
542                               0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
543                 return -EIO;
544         return 0;
545 }
546
547 #else /* CONFIG_CRASH_DUMP */
548
549 static inline void smp_get_save_area(int cpu, u16 address) { }
550
551 #endif /* CONFIG_CRASH_DUMP */
552
553 void smp_cpu_set_polarization(int cpu, int val)
554 {
555         pcpu_devices[cpu].polarization = val;
556 }
557
558 int smp_cpu_get_polarization(int cpu)
559 {
560         return pcpu_devices[cpu].polarization;
561 }
562
563 static struct sclp_cpu_info *smp_get_cpu_info(void)
564 {
565         static int use_sigp_detection;
566         struct sclp_cpu_info *info;
567         int address;
568
569         info = kzalloc(sizeof(*info), GFP_KERNEL);
570         if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
571                 use_sigp_detection = 1;
572                 for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
573                         if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
574                             SIGP_CC_NOT_OPERATIONAL)
575                                 continue;
576                         info->cpu[info->configured].address = address;
577                         info->configured++;
578                 }
579                 info->combined = info->configured;
580         }
581         return info;
582 }
583
584 static int smp_add_present_cpu(int cpu);
585
586 static int __smp_rescan_cpus(struct sclp_cpu_info *info, int sysfs_add)
587 {
588         struct pcpu *pcpu;
589         cpumask_t avail;
590         int cpu, nr, i;
591
592         nr = 0;
593         cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
594         cpu = cpumask_first(&avail);
595         for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
596                 if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
597                         continue;
598                 if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
599                         continue;
600                 pcpu = pcpu_devices + cpu;
601                 pcpu->address = info->cpu[i].address;
602                 pcpu->state = (i >= info->configured) ?
603                         CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
604                 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
605                 set_cpu_present(cpu, true);
606                 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
607                         set_cpu_present(cpu, false);
608                 else
609                         nr++;
610                 cpu = cpumask_next(cpu, &avail);
611         }
612         return nr;
613 }
614
615 static void __init smp_detect_cpus(void)
616 {
617         unsigned int cpu, c_cpus, s_cpus;
618         struct sclp_cpu_info *info;
619
620         info = smp_get_cpu_info();
621         if (!info)
622                 panic("smp_detect_cpus failed to allocate memory\n");
623         if (info->has_cpu_type) {
624                 for (cpu = 0; cpu < info->combined; cpu++) {
625                         if (info->cpu[cpu].address != boot_cpu_address)
626                                 continue;
627                         /* The boot cpu dictates the cpu type. */
628                         boot_cpu_type = info->cpu[cpu].type;
629                         break;
630                 }
631         }
632         c_cpus = s_cpus = 0;
633         for (cpu = 0; cpu < info->combined; cpu++) {
634                 if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
635                         continue;
636                 if (cpu < info->configured) {
637                         smp_get_save_area(c_cpus, info->cpu[cpu].address);
638                         c_cpus++;
639                 } else
640                         s_cpus++;
641         }
642         pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
643         get_online_cpus();
644         __smp_rescan_cpus(info, 0);
645         put_online_cpus();
646         kfree(info);
647 }
648
649 /*
650  *      Activate a secondary processor.
651  */
652 static void smp_start_secondary(void *cpuvoid)
653 {
654         S390_lowcore.last_update_clock = get_tod_clock();
655         S390_lowcore.restart_stack = (unsigned long) restart_stack;
656         S390_lowcore.restart_fn = (unsigned long) do_restart;
657         S390_lowcore.restart_data = 0;
658         S390_lowcore.restart_source = -1UL;
659         restore_access_regs(S390_lowcore.access_regs_save_area);
660         __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
661         __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
662         cpu_init();
663         preempt_disable();
664         init_cpu_timer();
665         vtime_init();
666         pfault_init();
667         notify_cpu_starting(smp_processor_id());
668         set_cpu_online(smp_processor_id(), true);
669         inc_irq_stat(CPU_RST);
670         local_irq_enable();
671         cpu_startup_entry(CPUHP_ONLINE);
672 }
673
674 /* Upping and downing of CPUs */
675 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
676 {
677         struct pcpu *pcpu;
678         int rc;
679
680         pcpu = pcpu_devices + cpu;
681         if (pcpu->state != CPU_STATE_CONFIGURED)
682                 return -EIO;
683         if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
684             SIGP_CC_ORDER_CODE_ACCEPTED)
685                 return -EIO;
686
687         rc = pcpu_alloc_lowcore(pcpu, cpu);
688         if (rc)
689                 return rc;
690         pcpu_prepare_secondary(pcpu, cpu);
691         pcpu_attach_task(pcpu, tidle);
692         pcpu_start_fn(pcpu, smp_start_secondary, NULL);
693         while (!cpu_online(cpu))
694                 cpu_relax();
695         return 0;
696 }
697
698 static unsigned int setup_possible_cpus __initdata;
699
700 static int __init _setup_possible_cpus(char *s)
701 {
702         get_option(&s, &setup_possible_cpus);
703         return 0;
704 }
705 early_param("possible_cpus", _setup_possible_cpus);
706
707 #ifdef CONFIG_HOTPLUG_CPU
708
709 int __cpu_disable(void)
710 {
711         unsigned long cregs[16];
712
713         /* Handle possible pending IPIs */
714         smp_handle_ext_call();
715         set_cpu_online(smp_processor_id(), false);
716         /* Disable pseudo page faults on this cpu. */
717         pfault_fini();
718         /* Disable interrupt sources via control register. */
719         __ctl_store(cregs, 0, 15);
720         cregs[0]  &= ~0x0000ee70UL;     /* disable all external interrupts */
721         cregs[6]  &= ~0xff000000UL;     /* disable all I/O interrupts */
722         cregs[14] &= ~0x1f000000UL;     /* disable most machine checks */
723         __ctl_load(cregs, 0, 15);
724         clear_cpu_flag(CIF_NOHZ_DELAY);
725         return 0;
726 }
727
728 void __cpu_die(unsigned int cpu)
729 {
730         struct pcpu *pcpu;
731
732         /* Wait until target cpu is down */
733         pcpu = pcpu_devices + cpu;
734         while (!pcpu_stopped(pcpu))
735                 cpu_relax();
736         pcpu_free_lowcore(pcpu);
737         atomic_dec(&init_mm.context.attach_count);
738         cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
739         if (MACHINE_HAS_TLB_LC)
740                 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
741 }
742
743 void __noreturn cpu_die(void)
744 {
745         idle_task_exit();
746         pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
747         for (;;) ;
748 }
749
750 #endif /* CONFIG_HOTPLUG_CPU */
751
752 void __init smp_fill_possible_mask(void)
753 {
754         unsigned int possible, sclp, cpu;
755
756         sclp = sclp_get_max_cpu() ?: nr_cpu_ids;
757         possible = setup_possible_cpus ?: nr_cpu_ids;
758         possible = min(possible, sclp);
759         for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
760                 set_cpu_possible(cpu, true);
761 }
762
763 void __init smp_prepare_cpus(unsigned int max_cpus)
764 {
765         /* request the 0x1201 emergency signal external interrupt */
766         if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
767                 panic("Couldn't request external interrupt 0x1201");
768         /* request the 0x1202 external call external interrupt */
769         if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
770                 panic("Couldn't request external interrupt 0x1202");
771         smp_detect_cpus();
772 }
773
774 void __init smp_prepare_boot_cpu(void)
775 {
776         struct pcpu *pcpu = pcpu_devices;
777
778         boot_cpu_address = stap();
779         pcpu->state = CPU_STATE_CONFIGURED;
780         pcpu->address = boot_cpu_address;
781         pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
782         pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
783                 + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
784         pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
785                 + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
786         S390_lowcore.percpu_offset = __per_cpu_offset[0];
787         smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
788         set_cpu_present(0, true);
789         set_cpu_online(0, true);
790 }
791
792 void __init smp_cpus_done(unsigned int max_cpus)
793 {
794 }
795
796 void __init smp_setup_processor_id(void)
797 {
798         S390_lowcore.cpu_nr = 0;
799         S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
800 }
801
802 /*
803  * the frequency of the profiling timer can be changed
804  * by writing a multiplier value into /proc/profile.
805  *
806  * usually you want to run this on all CPUs ;)
807  */
808 int setup_profiling_timer(unsigned int multiplier)
809 {
810         return 0;
811 }
812
813 #ifdef CONFIG_HOTPLUG_CPU
814 static ssize_t cpu_configure_show(struct device *dev,
815                                   struct device_attribute *attr, char *buf)
816 {
817         ssize_t count;
818
819         mutex_lock(&smp_cpu_state_mutex);
820         count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
821         mutex_unlock(&smp_cpu_state_mutex);
822         return count;
823 }
824
825 static ssize_t cpu_configure_store(struct device *dev,
826                                    struct device_attribute *attr,
827                                    const char *buf, size_t count)
828 {
829         struct pcpu *pcpu;
830         int cpu, val, rc;
831         char delim;
832
833         if (sscanf(buf, "%d %c", &val, &delim) != 1)
834                 return -EINVAL;
835         if (val != 0 && val != 1)
836                 return -EINVAL;
837         get_online_cpus();
838         mutex_lock(&smp_cpu_state_mutex);
839         rc = -EBUSY;
840         /* disallow configuration changes of online cpus and cpu 0 */
841         cpu = dev->id;
842         if (cpu_online(cpu) || cpu == 0)
843                 goto out;
844         pcpu = pcpu_devices + cpu;
845         rc = 0;
846         switch (val) {
847         case 0:
848                 if (pcpu->state != CPU_STATE_CONFIGURED)
849                         break;
850                 rc = sclp_cpu_deconfigure(pcpu->address);
851                 if (rc)
852                         break;
853                 pcpu->state = CPU_STATE_STANDBY;
854                 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
855                 topology_expect_change();
856                 break;
857         case 1:
858                 if (pcpu->state != CPU_STATE_STANDBY)
859                         break;
860                 rc = sclp_cpu_configure(pcpu->address);
861                 if (rc)
862                         break;
863                 pcpu->state = CPU_STATE_CONFIGURED;
864                 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
865                 topology_expect_change();
866                 break;
867         default:
868                 break;
869         }
870 out:
871         mutex_unlock(&smp_cpu_state_mutex);
872         put_online_cpus();
873         return rc ? rc : count;
874 }
875 static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
876 #endif /* CONFIG_HOTPLUG_CPU */
877
878 static ssize_t show_cpu_address(struct device *dev,
879                                 struct device_attribute *attr, char *buf)
880 {
881         return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
882 }
883 static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
884
885 static struct attribute *cpu_common_attrs[] = {
886 #ifdef CONFIG_HOTPLUG_CPU
887         &dev_attr_configure.attr,
888 #endif
889         &dev_attr_address.attr,
890         NULL,
891 };
892
893 static struct attribute_group cpu_common_attr_group = {
894         .attrs = cpu_common_attrs,
895 };
896
897 static struct attribute *cpu_online_attrs[] = {
898         &dev_attr_idle_count.attr,
899         &dev_attr_idle_time_us.attr,
900         NULL,
901 };
902
903 static struct attribute_group cpu_online_attr_group = {
904         .attrs = cpu_online_attrs,
905 };
906
907 static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
908                           void *hcpu)
909 {
910         unsigned int cpu = (unsigned int)(long)hcpu;
911         struct cpu *c = pcpu_devices[cpu].cpu;
912         struct device *s = &c->dev;
913         int err = 0;
914
915         switch (action & ~CPU_TASKS_FROZEN) {
916         case CPU_ONLINE:
917                 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
918                 break;
919         case CPU_DEAD:
920                 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
921                 break;
922         }
923         return notifier_from_errno(err);
924 }
925
926 static int smp_add_present_cpu(int cpu)
927 {
928         struct device *s;
929         struct cpu *c;
930         int rc;
931
932         c = kzalloc(sizeof(*c), GFP_KERNEL);
933         if (!c)
934                 return -ENOMEM;
935         pcpu_devices[cpu].cpu = c;
936         s = &c->dev;
937         c->hotpluggable = 1;
938         rc = register_cpu(c, cpu);
939         if (rc)
940                 goto out;
941         rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
942         if (rc)
943                 goto out_cpu;
944         if (cpu_online(cpu)) {
945                 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
946                 if (rc)
947                         goto out_online;
948         }
949         rc = topology_cpu_init(c);
950         if (rc)
951                 goto out_topology;
952         return 0;
953
954 out_topology:
955         if (cpu_online(cpu))
956                 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
957 out_online:
958         sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
959 out_cpu:
960 #ifdef CONFIG_HOTPLUG_CPU
961         unregister_cpu(c);
962 #endif
963 out:
964         return rc;
965 }
966
967 #ifdef CONFIG_HOTPLUG_CPU
968
969 int __ref smp_rescan_cpus(void)
970 {
971         struct sclp_cpu_info *info;
972         int nr;
973
974         info = smp_get_cpu_info();
975         if (!info)
976                 return -ENOMEM;
977         get_online_cpus();
978         mutex_lock(&smp_cpu_state_mutex);
979         nr = __smp_rescan_cpus(info, 1);
980         mutex_unlock(&smp_cpu_state_mutex);
981         put_online_cpus();
982         kfree(info);
983         if (nr)
984                 topology_schedule_update();
985         return 0;
986 }
987
988 static ssize_t __ref rescan_store(struct device *dev,
989                                   struct device_attribute *attr,
990                                   const char *buf,
991                                   size_t count)
992 {
993         int rc;
994
995         rc = smp_rescan_cpus();
996         return rc ? rc : count;
997 }
998 static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
999 #endif /* CONFIG_HOTPLUG_CPU */
1000
1001 static int __init s390_smp_init(void)
1002 {
1003         int cpu, rc = 0;
1004
1005 #ifdef CONFIG_HOTPLUG_CPU
1006         rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
1007         if (rc)
1008                 return rc;
1009 #endif
1010         cpu_notifier_register_begin();
1011         for_each_present_cpu(cpu) {
1012                 rc = smp_add_present_cpu(cpu);
1013                 if (rc)
1014                         goto out;
1015         }
1016
1017         __hotcpu_notifier(smp_cpu_notify, 0);
1018
1019 out:
1020         cpu_notifier_register_done();
1021         return rc;
1022 }
1023 subsys_initcall(s390_smp_init);