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