e4572601e91e0cf2d7f1d22103406b5761845399
[linux-3.10.git] / arch / s390 / kernel / smp.c
1 /*
2  *  arch/s390/kernel/smp.c
3  *
4  *    Copyright IBM Corp. 1999, 2009
5  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
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  * We work with logical cpu numbering everywhere we can. The only
14  * functions using the real cpu address (got from STAP) are the sigp
15  * functions. For all other functions we use the identity mapping.
16  * That means that cpu_number_map[i] == i for every cpu. cpu_number_map is
17  * used e.g. to find the idle task belonging to a logical cpu. Every array
18  * in the kernel is sorted by the logical cpu number and not by the physical
19  * one which is causing all the confusion with __cpu_logical_map and
20  * cpu_number_map in other architectures.
21  */
22
23 #define KMSG_COMPONENT "cpu"
24 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
25
26 #include <linux/workqueue.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/mm.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/delay.h>
34 #include <linux/cache.h>
35 #include <linux/interrupt.h>
36 #include <linux/irqflags.h>
37 #include <linux/cpu.h>
38 #include <linux/timex.h>
39 #include <linux/bootmem.h>
40 #include <linux/slab.h>
41 #include <asm/asm-offsets.h>
42 #include <asm/ipl.h>
43 #include <asm/setup.h>
44 #include <asm/sigp.h>
45 #include <asm/pgalloc.h>
46 #include <asm/irq.h>
47 #include <asm/cpcmd.h>
48 #include <asm/tlbflush.h>
49 #include <asm/timer.h>
50 #include <asm/lowcore.h>
51 #include <asm/sclp.h>
52 #include <asm/cputime.h>
53 #include <asm/vdso.h>
54 #include <asm/cpu.h>
55 #include "entry.h"
56
57 /* logical cpu to cpu address */
58 unsigned short __cpu_logical_map[NR_CPUS];
59
60 static struct task_struct *current_set[NR_CPUS];
61
62 static u8 smp_cpu_type;
63 static int smp_use_sigp_detection;
64
65 enum s390_cpu_state {
66         CPU_STATE_STANDBY,
67         CPU_STATE_CONFIGURED,
68 };
69
70 DEFINE_MUTEX(smp_cpu_state_mutex);
71 int smp_cpu_polarization[NR_CPUS];
72 static int smp_cpu_state[NR_CPUS];
73 static int cpu_management;
74
75 static DEFINE_PER_CPU(struct cpu, cpu_devices);
76
77 static void smp_ext_bitcall(int, int);
78
79 static int raw_cpu_stopped(int cpu)
80 {
81         u32 status;
82
83         switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
84         case sigp_status_stored:
85                 /* Check for stopped and check stop state */
86                 if (status & 0x50)
87                         return 1;
88                 break;
89         default:
90                 break;
91         }
92         return 0;
93 }
94
95 static inline int cpu_stopped(int cpu)
96 {
97         return raw_cpu_stopped(cpu_logical_map(cpu));
98 }
99
100 /*
101  * Ensure that PSW restart is done on an online CPU
102  */
103 void smp_restart_with_online_cpu(void)
104 {
105         int cpu;
106
107         for_each_online_cpu(cpu) {
108                 if (stap() == __cpu_logical_map[cpu]) {
109                         /* We are online: Enable DAT again and return */
110                         __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
111                         return;
112                 }
113         }
114         /* We are not online: Do PSW restart on an online CPU */
115         while (sigp(cpu, sigp_restart) == sigp_busy)
116                 cpu_relax();
117         /* And stop ourself */
118         while (raw_sigp(stap(), sigp_stop) == sigp_busy)
119                 cpu_relax();
120         for (;;);
121 }
122
123 void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
124 {
125         struct _lowcore *lc, *current_lc;
126         struct stack_frame *sf;
127         struct pt_regs *regs;
128         unsigned long sp;
129
130         if (smp_processor_id() == 0)
131                 func(data);
132         __load_psw_mask(PSW_BASE_BITS | PSW_DEFAULT_KEY);
133         /* Disable lowcore protection */
134         __ctl_clear_bit(0, 28);
135         current_lc = lowcore_ptr[smp_processor_id()];
136         lc = lowcore_ptr[0];
137         if (!lc)
138                 lc = current_lc;
139         lc->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
140         lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
141         if (!cpu_online(0))
142                 smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
143         while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
144                 cpu_relax();
145         sp = lc->panic_stack;
146         sp -= sizeof(struct pt_regs);
147         regs = (struct pt_regs *) sp;
148         memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
149         regs->psw = lc->psw_save_area;
150         sp -= STACK_FRAME_OVERHEAD;
151         sf = (struct stack_frame *) sp;
152         sf->back_chain = regs->gprs[15];
153         smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
154 }
155
156 void smp_send_stop(void)
157 {
158         int cpu, rc;
159
160         /* Disable all interrupts/machine checks */
161         __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
162         trace_hardirqs_off();
163
164         /* stop all processors */
165         for_each_online_cpu(cpu) {
166                 if (cpu == smp_processor_id())
167                         continue;
168                 do {
169                         rc = sigp(cpu, sigp_stop);
170                 } while (rc == sigp_busy);
171
172                 while (!cpu_stopped(cpu))
173                         cpu_relax();
174         }
175 }
176
177 /*
178  * This is the main routine where commands issued by other
179  * cpus are handled.
180  */
181
182 static void do_ext_call_interrupt(unsigned int ext_int_code,
183                                   unsigned int param32, unsigned long param64)
184 {
185         unsigned long bits;
186
187         kstat_cpu(smp_processor_id()).irqs[EXTINT_IPI]++;
188         /*
189          * handle bit signal external calls
190          */
191         bits = xchg(&S390_lowcore.ext_call_fast, 0);
192
193         if (test_bit(ec_schedule, &bits))
194                 scheduler_ipi();
195
196         if (test_bit(ec_call_function, &bits))
197                 generic_smp_call_function_interrupt();
198
199         if (test_bit(ec_call_function_single, &bits))
200                 generic_smp_call_function_single_interrupt();
201 }
202
203 /*
204  * Send an external call sigp to another cpu and return without waiting
205  * for its completion.
206  */
207 static void smp_ext_bitcall(int cpu, int sig)
208 {
209         /*
210          * Set signaling bit in lowcore of target cpu and kick it
211          */
212         set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
213         while (sigp(cpu, sigp_emergency_signal) == sigp_busy)
214                 udelay(10);
215 }
216
217 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
218 {
219         int cpu;
220
221         for_each_cpu(cpu, mask)
222                 smp_ext_bitcall(cpu, ec_call_function);
223 }
224
225 void arch_send_call_function_single_ipi(int cpu)
226 {
227         smp_ext_bitcall(cpu, ec_call_function_single);
228 }
229
230 #ifndef CONFIG_64BIT
231 /*
232  * this function sends a 'purge tlb' signal to another CPU.
233  */
234 static void smp_ptlb_callback(void *info)
235 {
236         __tlb_flush_local();
237 }
238
239 void smp_ptlb_all(void)
240 {
241         on_each_cpu(smp_ptlb_callback, NULL, 1);
242 }
243 EXPORT_SYMBOL(smp_ptlb_all);
244 #endif /* ! CONFIG_64BIT */
245
246 /*
247  * this function sends a 'reschedule' IPI to another CPU.
248  * it goes straight through and wastes no time serializing
249  * anything. Worst case is that we lose a reschedule ...
250  */
251 void smp_send_reschedule(int cpu)
252 {
253         smp_ext_bitcall(cpu, ec_schedule);
254 }
255
256 /*
257  * parameter area for the set/clear control bit callbacks
258  */
259 struct ec_creg_mask_parms {
260         unsigned long orvals[16];
261         unsigned long andvals[16];
262 };
263
264 /*
265  * callback for setting/clearing control bits
266  */
267 static void smp_ctl_bit_callback(void *info)
268 {
269         struct ec_creg_mask_parms *pp = info;
270         unsigned long cregs[16];
271         int i;
272
273         __ctl_store(cregs, 0, 15);
274         for (i = 0; i <= 15; i++)
275                 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
276         __ctl_load(cregs, 0, 15);
277 }
278
279 /*
280  * Set a bit in a control register of all cpus
281  */
282 void smp_ctl_set_bit(int cr, int bit)
283 {
284         struct ec_creg_mask_parms parms;
285
286         memset(&parms.orvals, 0, sizeof(parms.orvals));
287         memset(&parms.andvals, 0xff, sizeof(parms.andvals));
288         parms.orvals[cr] = 1UL << bit;
289         on_each_cpu(smp_ctl_bit_callback, &parms, 1);
290 }
291 EXPORT_SYMBOL(smp_ctl_set_bit);
292
293 /*
294  * Clear a bit in a control register of all cpus
295  */
296 void smp_ctl_clear_bit(int cr, int bit)
297 {
298         struct ec_creg_mask_parms parms;
299
300         memset(&parms.orvals, 0, sizeof(parms.orvals));
301         memset(&parms.andvals, 0xff, sizeof(parms.andvals));
302         parms.andvals[cr] = ~(1UL << bit);
303         on_each_cpu(smp_ctl_bit_callback, &parms, 1);
304 }
305 EXPORT_SYMBOL(smp_ctl_clear_bit);
306
307 #ifdef CONFIG_ZFCPDUMP
308
309 static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
310 {
311         if (ipl_info.type != IPL_TYPE_FCP_DUMP)
312                 return;
313         if (cpu >= NR_CPUS) {
314                 pr_warning("CPU %i exceeds the maximum %i and is excluded from "
315                            "the dump\n", cpu, NR_CPUS - 1);
316                 return;
317         }
318         zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
319         while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
320                 cpu_relax();
321         memcpy_real(zfcpdump_save_areas[cpu],
322                     (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
323                     sizeof(struct save_area));
324 }
325
326 struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
327 EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
328
329 #else
330
331 static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
332
333 #endif /* CONFIG_ZFCPDUMP */
334
335 static int cpu_known(int cpu_id)
336 {
337         int cpu;
338
339         for_each_present_cpu(cpu) {
340                 if (__cpu_logical_map[cpu] == cpu_id)
341                         return 1;
342         }
343         return 0;
344 }
345
346 static int smp_rescan_cpus_sigp(cpumask_t avail)
347 {
348         int cpu_id, logical_cpu;
349
350         logical_cpu = cpumask_first(&avail);
351         if (logical_cpu >= nr_cpu_ids)
352                 return 0;
353         for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
354                 if (cpu_known(cpu_id))
355                         continue;
356                 __cpu_logical_map[logical_cpu] = cpu_id;
357                 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
358                 if (!cpu_stopped(logical_cpu))
359                         continue;
360                 set_cpu_present(logical_cpu, true);
361                 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
362                 logical_cpu = cpumask_next(logical_cpu, &avail);
363                 if (logical_cpu >= nr_cpu_ids)
364                         break;
365         }
366         return 0;
367 }
368
369 static int smp_rescan_cpus_sclp(cpumask_t avail)
370 {
371         struct sclp_cpu_info *info;
372         int cpu_id, logical_cpu, cpu;
373         int rc;
374
375         logical_cpu = cpumask_first(&avail);
376         if (logical_cpu >= nr_cpu_ids)
377                 return 0;
378         info = kmalloc(sizeof(*info), GFP_KERNEL);
379         if (!info)
380                 return -ENOMEM;
381         rc = sclp_get_cpu_info(info);
382         if (rc)
383                 goto out;
384         for (cpu = 0; cpu < info->combined; cpu++) {
385                 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
386                         continue;
387                 cpu_id = info->cpu[cpu].address;
388                 if (cpu_known(cpu_id))
389                         continue;
390                 __cpu_logical_map[logical_cpu] = cpu_id;
391                 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
392                 set_cpu_present(logical_cpu, true);
393                 if (cpu >= info->configured)
394                         smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
395                 else
396                         smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
397                 logical_cpu = cpumask_next(logical_cpu, &avail);
398                 if (logical_cpu >= nr_cpu_ids)
399                         break;
400         }
401 out:
402         kfree(info);
403         return rc;
404 }
405
406 static int __smp_rescan_cpus(void)
407 {
408         cpumask_t avail;
409
410         cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
411         if (smp_use_sigp_detection)
412                 return smp_rescan_cpus_sigp(avail);
413         else
414                 return smp_rescan_cpus_sclp(avail);
415 }
416
417 static void __init smp_detect_cpus(void)
418 {
419         unsigned int cpu, c_cpus, s_cpus;
420         struct sclp_cpu_info *info;
421         u16 boot_cpu_addr, cpu_addr;
422
423         c_cpus = 1;
424         s_cpus = 0;
425         boot_cpu_addr = __cpu_logical_map[0];
426         info = kmalloc(sizeof(*info), GFP_KERNEL);
427         if (!info)
428                 panic("smp_detect_cpus failed to allocate memory\n");
429         /* Use sigp detection algorithm if sclp doesn't work. */
430         if (sclp_get_cpu_info(info)) {
431                 smp_use_sigp_detection = 1;
432                 for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
433                         if (cpu == boot_cpu_addr)
434                                 continue;
435                         if (!raw_cpu_stopped(cpu))
436                                 continue;
437                         smp_get_save_area(c_cpus, cpu);
438                         c_cpus++;
439                 }
440                 goto out;
441         }
442
443         if (info->has_cpu_type) {
444                 for (cpu = 0; cpu < info->combined; cpu++) {
445                         if (info->cpu[cpu].address == boot_cpu_addr) {
446                                 smp_cpu_type = info->cpu[cpu].type;
447                                 break;
448                         }
449                 }
450         }
451
452         for (cpu = 0; cpu < info->combined; cpu++) {
453                 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
454                         continue;
455                 cpu_addr = info->cpu[cpu].address;
456                 if (cpu_addr == boot_cpu_addr)
457                         continue;
458                 if (!raw_cpu_stopped(cpu_addr)) {
459                         s_cpus++;
460                         continue;
461                 }
462                 smp_get_save_area(c_cpus, cpu_addr);
463                 c_cpus++;
464         }
465 out:
466         kfree(info);
467         pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
468         get_online_cpus();
469         __smp_rescan_cpus();
470         put_online_cpus();
471 }
472
473 /*
474  *      Activate a secondary processor.
475  */
476 int __cpuinit start_secondary(void *cpuvoid)
477 {
478         cpu_init();
479         preempt_disable();
480         init_cpu_timer();
481         init_cpu_vtimer();
482         pfault_init();
483
484         notify_cpu_starting(smp_processor_id());
485         ipi_call_lock();
486         set_cpu_online(smp_processor_id(), true);
487         ipi_call_unlock();
488         __ctl_clear_bit(0, 28); /* Disable lowcore protection */
489         S390_lowcore.restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
490         S390_lowcore.restart_psw.addr =
491                 PSW_ADDR_AMODE | (unsigned long) psw_restart_int_handler;
492         __ctl_set_bit(0, 28); /* Enable lowcore protection */
493         /*
494          * Wait until the cpu which brought this one up marked it
495          * active before enabling interrupts.
496          */
497         while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
498                 cpu_relax();
499         local_irq_enable();
500         /* cpu_idle will call schedule for us */
501         cpu_idle();
502         return 0;
503 }
504
505 struct create_idle {
506         struct work_struct work;
507         struct task_struct *idle;
508         struct completion done;
509         int cpu;
510 };
511
512 static void __cpuinit smp_fork_idle(struct work_struct *work)
513 {
514         struct create_idle *c_idle;
515
516         c_idle = container_of(work, struct create_idle, work);
517         c_idle->idle = fork_idle(c_idle->cpu);
518         complete(&c_idle->done);
519 }
520
521 static int __cpuinit smp_alloc_lowcore(int cpu)
522 {
523         unsigned long async_stack, panic_stack;
524         struct _lowcore *lowcore;
525
526         lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
527         if (!lowcore)
528                 return -ENOMEM;
529         async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
530         panic_stack = __get_free_page(GFP_KERNEL);
531         if (!panic_stack || !async_stack)
532                 goto out;
533         memcpy(lowcore, &S390_lowcore, 512);
534         memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
535         lowcore->async_stack = async_stack + ASYNC_SIZE;
536         lowcore->panic_stack = panic_stack + PAGE_SIZE;
537         lowcore->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
538         lowcore->restart_psw.addr =
539                 PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
540         if (user_mode != HOME_SPACE_MODE)
541                 lowcore->restart_psw.mask |= PSW_ASC_HOME;
542 #ifndef CONFIG_64BIT
543         if (MACHINE_HAS_IEEE) {
544                 unsigned long save_area;
545
546                 save_area = get_zeroed_page(GFP_KERNEL);
547                 if (!save_area)
548                         goto out;
549                 lowcore->extended_save_area_addr = (u32) save_area;
550         }
551 #else
552         if (vdso_alloc_per_cpu(cpu, lowcore))
553                 goto out;
554 #endif
555         lowcore_ptr[cpu] = lowcore;
556         return 0;
557
558 out:
559         free_page(panic_stack);
560         free_pages(async_stack, ASYNC_ORDER);
561         free_pages((unsigned long) lowcore, LC_ORDER);
562         return -ENOMEM;
563 }
564
565 static void smp_free_lowcore(int cpu)
566 {
567         struct _lowcore *lowcore;
568
569         lowcore = lowcore_ptr[cpu];
570 #ifndef CONFIG_64BIT
571         if (MACHINE_HAS_IEEE)
572                 free_page((unsigned long) lowcore->extended_save_area_addr);
573 #else
574         vdso_free_per_cpu(cpu, lowcore);
575 #endif
576         free_page(lowcore->panic_stack - PAGE_SIZE);
577         free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
578         free_pages((unsigned long) lowcore, LC_ORDER);
579         lowcore_ptr[cpu] = NULL;
580 }
581
582 /* Upping and downing of CPUs */
583 int __cpuinit __cpu_up(unsigned int cpu)
584 {
585         struct _lowcore *cpu_lowcore;
586         struct create_idle c_idle;
587         struct task_struct *idle;
588         struct stack_frame *sf;
589         u32 lowcore;
590         int ccode;
591
592         if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
593                 return -EIO;
594         idle = current_set[cpu];
595         if (!idle) {
596                 c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
597                 INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
598                 c_idle.cpu = cpu;
599                 schedule_work(&c_idle.work);
600                 wait_for_completion(&c_idle.done);
601                 if (IS_ERR(c_idle.idle))
602                         return PTR_ERR(c_idle.idle);
603                 idle = c_idle.idle;
604                 current_set[cpu] = c_idle.idle;
605         }
606         init_idle(idle, cpu);
607         if (smp_alloc_lowcore(cpu))
608                 return -ENOMEM;
609         do {
610                 ccode = sigp(cpu, sigp_initial_cpu_reset);
611                 if (ccode == sigp_busy)
612                         udelay(10);
613                 if (ccode == sigp_not_operational)
614                         goto err_out;
615         } while (ccode == sigp_busy);
616
617         lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
618         while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
619                 udelay(10);
620
621         cpu_lowcore = lowcore_ptr[cpu];
622         cpu_lowcore->kernel_stack = (unsigned long)
623                 task_stack_page(idle) + THREAD_SIZE;
624         cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
625         sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
626                                      - sizeof(struct pt_regs)
627                                      - sizeof(struct stack_frame));
628         memset(sf, 0, sizeof(struct stack_frame));
629         sf->gprs[9] = (unsigned long) sf;
630         cpu_lowcore->save_area[15] = (unsigned long) sf;
631         __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
632         atomic_inc(&init_mm.context.attach_count);
633         asm volatile(
634                 "       stam    0,15,0(%0)"
635                 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
636         cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
637         cpu_lowcore->current_task = (unsigned long) idle;
638         cpu_lowcore->cpu_nr = cpu;
639         cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
640         cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
641         cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
642         memcpy(cpu_lowcore->stfle_fac_list, S390_lowcore.stfle_fac_list,
643                MAX_FACILITY_BIT/8);
644         eieio();
645
646         while (sigp(cpu, sigp_restart) == sigp_busy)
647                 udelay(10);
648
649         while (!cpu_online(cpu))
650                 cpu_relax();
651         return 0;
652
653 err_out:
654         smp_free_lowcore(cpu);
655         return -EIO;
656 }
657
658 static int __init setup_possible_cpus(char *s)
659 {
660         int pcpus, cpu;
661
662         pcpus = simple_strtoul(s, NULL, 0);
663         init_cpu_possible(cpumask_of(0));
664         for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
665                 set_cpu_possible(cpu, true);
666         return 0;
667 }
668 early_param("possible_cpus", setup_possible_cpus);
669
670 #ifdef CONFIG_HOTPLUG_CPU
671
672 int __cpu_disable(void)
673 {
674         struct ec_creg_mask_parms cr_parms;
675         int cpu = smp_processor_id();
676
677         set_cpu_online(cpu, false);
678
679         /* Disable pfault pseudo page faults on this cpu. */
680         pfault_fini();
681
682         memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
683         memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
684
685         /* disable all external interrupts */
686         cr_parms.orvals[0] = 0;
687         cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 11 |
688                                 1 << 10 | 1 <<  9 | 1 <<  6 | 1 <<  5 |
689                                 1 <<  4);
690         /* disable all I/O interrupts */
691         cr_parms.orvals[6] = 0;
692         cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
693                                 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
694         /* disable most machine checks */
695         cr_parms.orvals[14] = 0;
696         cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
697                                  1 << 25 | 1 << 24);
698
699         smp_ctl_bit_callback(&cr_parms);
700
701         return 0;
702 }
703
704 void __cpu_die(unsigned int cpu)
705 {
706         /* Wait until target cpu is down */
707         while (!cpu_stopped(cpu))
708                 cpu_relax();
709         while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
710                 udelay(10);
711         smp_free_lowcore(cpu);
712         atomic_dec(&init_mm.context.attach_count);
713 }
714
715 void __noreturn cpu_die(void)
716 {
717         idle_task_exit();
718         while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
719                 cpu_relax();
720         for (;;);
721 }
722
723 #endif /* CONFIG_HOTPLUG_CPU */
724
725 void __init smp_prepare_cpus(unsigned int max_cpus)
726 {
727 #ifndef CONFIG_64BIT
728         unsigned long save_area = 0;
729 #endif
730         unsigned long async_stack, panic_stack;
731         struct _lowcore *lowcore;
732
733         smp_detect_cpus();
734
735         /* request the 0x1201 emergency signal external interrupt */
736         if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
737                 panic("Couldn't request external interrupt 0x1201");
738
739         /* Reallocate current lowcore, but keep its contents. */
740         lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
741         panic_stack = __get_free_page(GFP_KERNEL);
742         async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
743         BUG_ON(!lowcore || !panic_stack || !async_stack);
744 #ifndef CONFIG_64BIT
745         if (MACHINE_HAS_IEEE)
746                 save_area = get_zeroed_page(GFP_KERNEL);
747 #endif
748         local_irq_disable();
749         local_mcck_disable();
750         lowcore_ptr[smp_processor_id()] = lowcore;
751         *lowcore = S390_lowcore;
752         lowcore->panic_stack = panic_stack + PAGE_SIZE;
753         lowcore->async_stack = async_stack + ASYNC_SIZE;
754 #ifndef CONFIG_64BIT
755         if (MACHINE_HAS_IEEE)
756                 lowcore->extended_save_area_addr = (u32) save_area;
757 #endif
758         set_prefix((u32)(unsigned long) lowcore);
759         local_mcck_enable();
760         local_irq_enable();
761 #ifdef CONFIG_64BIT
762         if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
763                 BUG();
764 #endif
765 }
766
767 void __init smp_prepare_boot_cpu(void)
768 {
769         BUG_ON(smp_processor_id() != 0);
770
771         current_thread_info()->cpu = 0;
772         set_cpu_present(0, true);
773         set_cpu_online(0, true);
774         S390_lowcore.percpu_offset = __per_cpu_offset[0];
775         current_set[0] = current;
776         smp_cpu_state[0] = CPU_STATE_CONFIGURED;
777         smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
778 }
779
780 void __init smp_cpus_done(unsigned int max_cpus)
781 {
782 }
783
784 void __init smp_setup_processor_id(void)
785 {
786         S390_lowcore.cpu_nr = 0;
787         __cpu_logical_map[0] = stap();
788 }
789
790 /*
791  * the frequency of the profiling timer can be changed
792  * by writing a multiplier value into /proc/profile.
793  *
794  * usually you want to run this on all CPUs ;)
795  */
796 int setup_profiling_timer(unsigned int multiplier)
797 {
798         return 0;
799 }
800
801 #ifdef CONFIG_HOTPLUG_CPU
802 static ssize_t cpu_configure_show(struct sys_device *dev,
803                                 struct sysdev_attribute *attr, char *buf)
804 {
805         ssize_t count;
806
807         mutex_lock(&smp_cpu_state_mutex);
808         count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
809         mutex_unlock(&smp_cpu_state_mutex);
810         return count;
811 }
812
813 static ssize_t cpu_configure_store(struct sys_device *dev,
814                                   struct sysdev_attribute *attr,
815                                   const char *buf, size_t count)
816 {
817         int cpu = dev->id;
818         int val, rc;
819         char delim;
820
821         if (sscanf(buf, "%d %c", &val, &delim) != 1)
822                 return -EINVAL;
823         if (val != 0 && val != 1)
824                 return -EINVAL;
825
826         get_online_cpus();
827         mutex_lock(&smp_cpu_state_mutex);
828         rc = -EBUSY;
829         /* disallow configuration changes of online cpus and cpu 0 */
830         if (cpu_online(cpu) || cpu == 0)
831                 goto out;
832         rc = 0;
833         switch (val) {
834         case 0:
835                 if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
836                         rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
837                         if (!rc) {
838                                 smp_cpu_state[cpu] = CPU_STATE_STANDBY;
839                                 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
840                         }
841                 }
842                 break;
843         case 1:
844                 if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
845                         rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
846                         if (!rc) {
847                                 smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
848                                 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
849                         }
850                 }
851                 break;
852         default:
853                 break;
854         }
855 out:
856         mutex_unlock(&smp_cpu_state_mutex);
857         put_online_cpus();
858         return rc ? rc : count;
859 }
860 static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
861 #endif /* CONFIG_HOTPLUG_CPU */
862
863 static ssize_t cpu_polarization_show(struct sys_device *dev,
864                                      struct sysdev_attribute *attr, char *buf)
865 {
866         int cpu = dev->id;
867         ssize_t count;
868
869         mutex_lock(&smp_cpu_state_mutex);
870         switch (smp_cpu_polarization[cpu]) {
871         case POLARIZATION_HRZ:
872                 count = sprintf(buf, "horizontal\n");
873                 break;
874         case POLARIZATION_VL:
875                 count = sprintf(buf, "vertical:low\n");
876                 break;
877         case POLARIZATION_VM:
878                 count = sprintf(buf, "vertical:medium\n");
879                 break;
880         case POLARIZATION_VH:
881                 count = sprintf(buf, "vertical:high\n");
882                 break;
883         default:
884                 count = sprintf(buf, "unknown\n");
885                 break;
886         }
887         mutex_unlock(&smp_cpu_state_mutex);
888         return count;
889 }
890 static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
891
892 static ssize_t show_cpu_address(struct sys_device *dev,
893                                 struct sysdev_attribute *attr, char *buf)
894 {
895         return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
896 }
897 static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
898
899
900 static struct attribute *cpu_common_attrs[] = {
901 #ifdef CONFIG_HOTPLUG_CPU
902         &attr_configure.attr,
903 #endif
904         &attr_address.attr,
905         &attr_polarization.attr,
906         NULL,
907 };
908
909 static struct attribute_group cpu_common_attr_group = {
910         .attrs = cpu_common_attrs,
911 };
912
913 static ssize_t show_capability(struct sys_device *dev,
914                                 struct sysdev_attribute *attr, char *buf)
915 {
916         unsigned int capability;
917         int rc;
918
919         rc = get_cpu_capability(&capability);
920         if (rc)
921                 return rc;
922         return sprintf(buf, "%u\n", capability);
923 }
924 static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
925
926 static ssize_t show_idle_count(struct sys_device *dev,
927                                 struct sysdev_attribute *attr, char *buf)
928 {
929         struct s390_idle_data *idle;
930         unsigned long long idle_count;
931         unsigned int sequence;
932
933         idle = &per_cpu(s390_idle, dev->id);
934 repeat:
935         sequence = idle->sequence;
936         smp_rmb();
937         if (sequence & 1)
938                 goto repeat;
939         idle_count = idle->idle_count;
940         if (idle->idle_enter)
941                 idle_count++;
942         smp_rmb();
943         if (idle->sequence != sequence)
944                 goto repeat;
945         return sprintf(buf, "%llu\n", idle_count);
946 }
947 static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
948
949 static ssize_t show_idle_time(struct sys_device *dev,
950                                 struct sysdev_attribute *attr, char *buf)
951 {
952         struct s390_idle_data *idle;
953         unsigned long long now, idle_time, idle_enter;
954         unsigned int sequence;
955
956         idle = &per_cpu(s390_idle, dev->id);
957         now = get_clock();
958 repeat:
959         sequence = idle->sequence;
960         smp_rmb();
961         if (sequence & 1)
962                 goto repeat;
963         idle_time = idle->idle_time;
964         idle_enter = idle->idle_enter;
965         if (idle_enter != 0ULL && idle_enter < now)
966                 idle_time += now - idle_enter;
967         smp_rmb();
968         if (idle->sequence != sequence)
969                 goto repeat;
970         return sprintf(buf, "%llu\n", idle_time >> 12);
971 }
972 static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
973
974 static struct attribute *cpu_online_attrs[] = {
975         &attr_capability.attr,
976         &attr_idle_count.attr,
977         &attr_idle_time_us.attr,
978         NULL,
979 };
980
981 static struct attribute_group cpu_online_attr_group = {
982         .attrs = cpu_online_attrs,
983 };
984
985 static int __cpuinit smp_cpu_notify(struct notifier_block *self,
986                                     unsigned long action, void *hcpu)
987 {
988         unsigned int cpu = (unsigned int)(long)hcpu;
989         struct cpu *c = &per_cpu(cpu_devices, cpu);
990         struct sys_device *s = &c->sysdev;
991         struct s390_idle_data *idle;
992         int err = 0;
993
994         switch (action) {
995         case CPU_ONLINE:
996         case CPU_ONLINE_FROZEN:
997                 idle = &per_cpu(s390_idle, cpu);
998                 memset(idle, 0, sizeof(struct s390_idle_data));
999                 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1000                 break;
1001         case CPU_DEAD:
1002         case CPU_DEAD_FROZEN:
1003                 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1004                 break;
1005         }
1006         return notifier_from_errno(err);
1007 }
1008
1009 static struct notifier_block __cpuinitdata smp_cpu_nb = {
1010         .notifier_call = smp_cpu_notify,
1011 };
1012
1013 static int __devinit smp_add_present_cpu(int cpu)
1014 {
1015         struct cpu *c = &per_cpu(cpu_devices, cpu);
1016         struct sys_device *s = &c->sysdev;
1017         int rc;
1018
1019         c->hotpluggable = 1;
1020         rc = register_cpu(c, cpu);
1021         if (rc)
1022                 goto out;
1023         rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1024         if (rc)
1025                 goto out_cpu;
1026         if (!cpu_online(cpu))
1027                 goto out;
1028         rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1029         if (!rc)
1030                 return 0;
1031         sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1032 out_cpu:
1033 #ifdef CONFIG_HOTPLUG_CPU
1034         unregister_cpu(c);
1035 #endif
1036 out:
1037         return rc;
1038 }
1039
1040 #ifdef CONFIG_HOTPLUG_CPU
1041
1042 int __ref smp_rescan_cpus(void)
1043 {
1044         cpumask_t newcpus;
1045         int cpu;
1046         int rc;
1047
1048         get_online_cpus();
1049         mutex_lock(&smp_cpu_state_mutex);
1050         cpumask_copy(&newcpus, cpu_present_mask);
1051         rc = __smp_rescan_cpus();
1052         if (rc)
1053                 goto out;
1054         cpumask_andnot(&newcpus, cpu_present_mask, &newcpus);
1055         for_each_cpu(cpu, &newcpus) {
1056                 rc = smp_add_present_cpu(cpu);
1057                 if (rc)
1058                         set_cpu_present(cpu, false);
1059         }
1060         rc = 0;
1061 out:
1062         mutex_unlock(&smp_cpu_state_mutex);
1063         put_online_cpus();
1064         if (!cpumask_empty(&newcpus))
1065                 topology_schedule_update();
1066         return rc;
1067 }
1068
1069 static ssize_t __ref rescan_store(struct sysdev_class *class,
1070                                   struct sysdev_class_attribute *attr,
1071                                   const char *buf,
1072                                   size_t count)
1073 {
1074         int rc;
1075
1076         rc = smp_rescan_cpus();
1077         return rc ? rc : count;
1078 }
1079 static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
1080 #endif /* CONFIG_HOTPLUG_CPU */
1081
1082 static ssize_t dispatching_show(struct sysdev_class *class,
1083                                 struct sysdev_class_attribute *attr,
1084                                 char *buf)
1085 {
1086         ssize_t count;
1087
1088         mutex_lock(&smp_cpu_state_mutex);
1089         count = sprintf(buf, "%d\n", cpu_management);
1090         mutex_unlock(&smp_cpu_state_mutex);
1091         return count;
1092 }
1093
1094 static ssize_t dispatching_store(struct sysdev_class *dev,
1095                                  struct sysdev_class_attribute *attr,
1096                                  const char *buf,
1097                                  size_t count)
1098 {
1099         int val, rc;
1100         char delim;
1101
1102         if (sscanf(buf, "%d %c", &val, &delim) != 1)
1103                 return -EINVAL;
1104         if (val != 0 && val != 1)
1105                 return -EINVAL;
1106         rc = 0;
1107         get_online_cpus();
1108         mutex_lock(&smp_cpu_state_mutex);
1109         if (cpu_management == val)
1110                 goto out;
1111         rc = topology_set_cpu_management(val);
1112         if (!rc)
1113                 cpu_management = val;
1114 out:
1115         mutex_unlock(&smp_cpu_state_mutex);
1116         put_online_cpus();
1117         return rc ? rc : count;
1118 }
1119 static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
1120                          dispatching_store);
1121
1122 static int __init topology_init(void)
1123 {
1124         int cpu;
1125         int rc;
1126
1127         register_cpu_notifier(&smp_cpu_nb);
1128
1129 #ifdef CONFIG_HOTPLUG_CPU
1130         rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
1131         if (rc)
1132                 return rc;
1133 #endif
1134         rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
1135         if (rc)
1136                 return rc;
1137         for_each_present_cpu(cpu) {
1138                 rc = smp_add_present_cpu(cpu);
1139                 if (rc)
1140                         return rc;
1141         }
1142         return 0;
1143 }
1144 subsys_initcall(topology_init);