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