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