clean
[linux-2.4.21-pre4.git] / arch / s390 / kernel / smp.c
1 /*
2  *  arch/s390/kernel/smp.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
7  *               Martin Schwidefsky (schwidefsky@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
26 #include <linux/mm.h>
27 #include <linux/spinlock.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/smp_lock.h>
30
31 #include <linux/delay.h>
32 #include <linux/cache.h>
33
34 #include <asm/sigp.h>
35 #include <asm/pgalloc.h>
36 #include <asm/irq.h>
37 #include <asm/s390_ext.h>
38 #include <asm/cpcmd.h>
39
40 /* prototypes */
41 extern int cpu_idle(void * unused);
42
43 extern __u16 boot_cpu_addr;
44 extern volatile int __cpu_logical_map[];
45
46 /*
47  * An array with a pointer the lowcore of every CPU.
48  */
49 static int       max_cpus = NR_CPUS;      /* Setup configured maximum number of CPUs to activate        */
50 int              smp_num_cpus;
51 struct _lowcore *lowcore_ptr[NR_CPUS];
52 cycles_t         cacheflush_time=0;
53 int              smp_threads_ready=0;      /* Set when the idlers are all forked. */
54 static atomic_t  smp_commenced = ATOMIC_INIT(0);
55
56 spinlock_t       kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
57
58 unsigned long    cpu_online_map;
59
60 /*
61  *      Setup routine for controlling SMP activation
62  *
63  *      Command-line option of "nosmp" or "maxcpus=0" will disable SMP
64  *      activation entirely (the MPS table probe still happens, though).
65  *
66  *      Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
67  *      greater than 0, limits the maximum number of CPUs activated in
68  *      SMP mode to <NUM>.
69  */
70
71 static int __init nosmp(char *str)
72 {
73         max_cpus = 0;
74         return 1;
75 }
76
77 __setup("nosmp", nosmp);
78
79 static int __init maxcpus(char *str)
80 {
81         get_option(&str, &max_cpus);
82         return 1;
83 }
84
85 __setup("maxcpus=", maxcpus);
86
87 /*
88  * Reboot, halt and power_off routines for SMP.
89  */
90 extern char vmhalt_cmd[];
91 extern char vmpoff_cmd[];
92
93 extern void reipl(unsigned long devno);
94
95 static sigp_ccode smp_ext_bitcall(int, ec_bit_sig);
96 static void smp_ext_bitcall_others(ec_bit_sig);
97
98 /*
99  * Structure and data for smp_call_function(). This is designed to minimise
100  * static memory requirements. It also looks cleaner.
101  */
102 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
103
104 struct call_data_struct {
105         void (*func) (void *info);
106         void *info;
107         atomic_t started;
108         atomic_t finished;
109         int wait;
110 };
111
112 static struct call_data_struct * call_data;
113
114 /*
115  * 'Call function' interrupt callback
116  */
117 static void do_call_function(void)
118 {
119         void (*func) (void *info) = call_data->func;
120         void *info = call_data->info;
121         int wait = call_data->wait;
122
123         atomic_inc(&call_data->started);
124         (*func)(info);
125         if (wait)
126                 atomic_inc(&call_data->finished);
127 }
128
129 /*
130  * this function sends a 'generic call function' IPI to all other CPUs
131  * in the system.
132  */
133
134 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
135                         int wait)
136 /*
137  * [SUMMARY] Run a function on all other CPUs.
138  * <func> The function to run. This must be fast and non-blocking.
139  * <info> An arbitrary pointer to pass to the function.
140  * <nonatomic> currently unused.
141  * <wait> If true, wait (atomically) until function has completed on other CPUs.
142  * [RETURNS] 0 on success, else a negative status code. Does not return until
143  * remote CPUs are nearly ready to execute <<func>> or are or have executed.
144  *
145  * You must not call this function with disabled interrupts or from a
146  * hardware interrupt handler, you may call it from a bottom half handler.
147  */
148 {
149         struct call_data_struct data;
150         int cpus = smp_num_cpus-1;
151
152         if (!cpus || !atomic_read(&smp_commenced))
153                 return 0;
154
155         data.func = func;
156         data.info = info;
157         atomic_set(&data.started, 0);
158         data.wait = wait;
159         if (wait)
160                 atomic_set(&data.finished, 0);
161
162         spin_lock_bh(&call_lock);
163         call_data = &data;
164         /* Send a message to all other CPUs and wait for them to respond */
165         smp_ext_bitcall_others(ec_call_function);
166
167         /* Wait for response */
168         while (atomic_read(&data.started) != cpus)
169                 barrier();
170
171         if (wait)
172                 while (atomic_read(&data.finished) != cpus)
173                         barrier();
174         spin_unlock_bh(&call_lock);
175
176         return 0;
177 }
178
179 static inline void do_send_stop(void)
180 {
181         u32 dummy;
182         int i;
183
184         /* stop all processors */
185         for (i =  0; i < smp_num_cpus; i++) {
186                 if (smp_processor_id() != i) {
187                         int ccode;
188                         do {
189                                 ccode = signal_processor_ps(
190                                    &dummy,
191                                    0,
192                                    i,
193                                    sigp_stop);
194                         } while(ccode == sigp_busy);
195                 }
196         }
197 }
198
199 static inline void do_store_status(void)
200 {
201         unsigned long low_core_addr;
202         u32 dummy;
203         int i;
204
205         /* store status of all processors in their lowcores (real 0) */
206         for (i =  0; i < smp_num_cpus; i++) {
207                 if (smp_processor_id() != i) {
208                         int ccode;
209                         low_core_addr = (unsigned long)get_cpu_lowcore(i);
210                         do {
211                                 ccode = signal_processor_ps(
212                                    &dummy,
213                                    low_core_addr,
214                                    i,
215                                    sigp_store_status_at_address);
216                         } while(ccode == sigp_busy);
217                 }
218         }
219 }
220
221 /*
222  * this function sends a 'stop' sigp to all other CPUs in the system.
223  * it goes straight through.
224  */
225 void smp_send_stop(void)
226 {
227         /* write magic number to zero page (absolute 0) */
228         get_cpu_lowcore(smp_processor_id())->panic_magic = __PANIC_MAGIC;
229
230         /* stop other processors. */
231         do_send_stop();
232
233         /* store status of other processors. */
234         do_store_status();
235 }
236
237 /*
238  * Reboot, halt and power_off routines for SMP.
239  */
240 static volatile unsigned long cpu_restart_map;
241
242 static void do_machine_restart(void * __unused)
243 {
244         clear_bit(smp_processor_id(), &cpu_restart_map);
245         if (smp_processor_id() == 0) {
246                 /* Wait for all other cpus to enter do_machine_restart. */
247                 while (cpu_restart_map != 0);
248                 /* Store status of other cpus. */
249                 do_store_status();
250                 /*
251                  * Finally call reipl. Because we waited for all other
252                  * cpus to enter this function we know that they do
253                  * not hold any s390irq-locks (the cpus have been
254                  * interrupted by an external interrupt and s390irq
255                  * locks are always held disabled).
256                  */
257                 reipl(S390_lowcore.ipl_device);
258         }
259         signal_processor(smp_processor_id(), sigp_stop);
260 }
261
262 void machine_restart_smp(char * __unused) 
263 {
264         cpu_restart_map = cpu_online_map;
265         smp_call_function(do_machine_restart, NULL, 0, 0);
266         do_machine_restart(NULL);
267 }
268
269 static void do_machine_halt(void * __unused)
270 {
271         if (smp_processor_id() == 0) {
272                 smp_send_stop();
273                 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
274                         cpcmd(vmhalt_cmd, NULL, 0);
275                 signal_processor(smp_processor_id(),
276                                  sigp_stop_and_store_status);
277         }
278         for (;;)
279                 enabled_wait();
280 }
281
282 void machine_halt_smp(void)
283 {
284         smp_call_function(do_machine_halt, NULL, 0, 0);
285         do_machine_halt(NULL);
286 }
287
288 static void do_machine_power_off(void * __unused)
289 {
290         if (smp_processor_id() == 0) {
291                 smp_send_stop();
292                 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
293                         cpcmd(vmpoff_cmd, NULL, 0);
294                 signal_processor(smp_processor_id(),
295                                  sigp_stop_and_store_status);
296         }
297         for (;;)
298                 enabled_wait();
299 }
300
301 void machine_power_off_smp(void)
302 {
303         smp_call_function(do_machine_power_off, NULL, 0, 0);
304         do_machine_power_off(NULL);
305 }
306
307 /*
308  * This is the main routine where commands issued by other
309  * cpus are handled.
310  */
311
312 void do_ext_call_interrupt(struct pt_regs *regs, __u16 code)
313 {
314         int bits;
315
316         /*
317          * handle bit signal external calls
318          *
319          * For the ec_schedule signal we have to do nothing. All the work
320          * is done automatically when we return from the interrupt.
321          */
322         do {
323                 bits = atomic_read(&S390_lowcore.ext_call_fast);
324         } while (atomic_compare_and_swap(bits,0,&S390_lowcore.ext_call_fast));
325
326         if (test_bit(ec_call_function, &bits)) 
327                 do_call_function();
328 }
329
330 /*
331  * Send an external call sigp to another cpu and return without waiting
332  * for its completion.
333  */
334 static sigp_ccode smp_ext_bitcall(int cpu, ec_bit_sig sig)
335 {
336         struct _lowcore *lowcore = get_cpu_lowcore(cpu);
337         sigp_ccode ccode;
338
339         /*
340          * Set signaling bit in lowcore of target cpu and kick it
341          */
342         atomic_set_mask(1<<sig, &lowcore->ext_call_fast);
343         ccode = signal_processor(cpu, sigp_external_call);
344         return ccode;
345 }
346
347 /*
348  * Send an external call sigp to every other cpu in the system and
349  * return without waiting for its completion.
350  */
351 static void smp_ext_bitcall_others(ec_bit_sig sig)
352 {
353         struct _lowcore *lowcore;
354         int i;
355
356         for (i = 0; i < smp_num_cpus; i++) {
357                 if (smp_processor_id() == i)
358                         continue;
359                 lowcore = get_cpu_lowcore(i);
360                 /*
361                  * Set signaling bit in lowcore of target cpu and kick it
362                  */
363                 atomic_set_mask(1<<sig, &lowcore->ext_call_fast);
364                 while (signal_processor(i, sigp_external_call) == sigp_busy)
365                         udelay(10);
366         }
367 }
368
369 /*
370  * this function sends a 'purge tlb' signal to another CPU.
371  */
372 void smp_ptlb_callback(void *info)
373 {
374         local_flush_tlb();
375 }
376
377 void smp_ptlb_all(void)
378 {
379         smp_call_function(smp_ptlb_callback, NULL, 0, 1);
380         local_flush_tlb();
381 }
382
383 /*
384  * this function sends a 'reschedule' IPI to another CPU.
385  * it goes straight through and wastes no time serializing
386  * anything. Worst case is that we lose a reschedule ...
387  */
388
389 void smp_send_reschedule(int cpu)
390 {
391         smp_ext_bitcall(cpu, ec_schedule);
392 }
393
394 /*
395  * parameter area for the set/clear control bit callbacks
396  */
397 typedef struct
398 {
399         __u16 start_ctl;
400         __u16 end_ctl;
401         __u32 orvals[16];
402         __u32 andvals[16];
403 } ec_creg_mask_parms;
404
405 /*
406  * callback for setting/clearing control bits
407  */
408 void smp_ctl_bit_callback(void *info) {
409         ec_creg_mask_parms *pp;
410         u32 cregs[16];
411         int i;
412         
413         pp = (ec_creg_mask_parms *) info;
414         asm volatile ("   bras  1,0f\n"
415                       "   stctl 0,0,0(%0)\n"
416                       "0: ex    %1,0(1)\n"
417                       : : "a" (cregs+pp->start_ctl),
418                           "a" ((pp->start_ctl<<4) + pp->end_ctl)
419                       : "memory", "1" );
420         for (i = pp->start_ctl; i <= pp->end_ctl; i++)
421                 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
422         asm volatile ("   bras  1,0f\n"
423                       "   lctl 0,0,0(%0)\n"
424                       "0: ex    %1,0(1)\n"
425                       : : "a" (cregs+pp->start_ctl),
426                           "a" ((pp->start_ctl<<4) + pp->end_ctl)
427                       : "memory", "1" );
428         return;
429 }
430
431 /*
432  * Set a bit in a control register of all cpus
433  */
434 void smp_ctl_set_bit(int cr, int bit) {
435         ec_creg_mask_parms parms;
436
437         if (atomic_read(&smp_commenced) != 0) {
438                 parms.start_ctl = cr;
439                 parms.end_ctl = cr;
440                 parms.orvals[cr] = 1 << bit;
441                 parms.andvals[cr] = 0xFFFFFFFF;
442                 smp_call_function(smp_ctl_bit_callback, &parms, 0, 1);
443         }
444         __ctl_set_bit(cr, bit);
445 }
446
447 /*
448  * Clear a bit in a control register of all cpus
449  */
450 void smp_ctl_clear_bit(int cr, int bit) {
451         ec_creg_mask_parms parms;
452
453         if (atomic_read(&smp_commenced) != 0) {
454                 parms.start_ctl = cr;
455                 parms.end_ctl = cr;
456                 parms.orvals[cr] = 0x00000000;
457                 parms.andvals[cr] = ~(1 << bit);
458                 smp_call_function(smp_ctl_bit_callback, &parms, 0, 1);
459         }
460         __ctl_clear_bit(cr, bit);
461 }
462
463 /*
464  * Lets check how many CPUs we have.
465  */
466
467 void smp_count_cpus(void)
468 {
469         int curr_cpu;
470
471         current->processor = 0;
472         smp_num_cpus = 1;
473         cpu_online_map = 1;
474         for (curr_cpu = 0;
475              curr_cpu <= 65535 && smp_num_cpus < max_cpus; curr_cpu++) {
476                 if ((__u16) curr_cpu == boot_cpu_addr)
477                         continue;
478                 __cpu_logical_map[smp_num_cpus] = (__u16) curr_cpu;
479                 if (signal_processor(smp_num_cpus, sigp_sense) ==
480                     sigp_not_operational)
481                         continue;
482                 smp_num_cpus++;
483         }
484         printk("Detected %d CPU's\n",(int) smp_num_cpus);
485         printk("Boot cpu address %2X\n", boot_cpu_addr);
486 }
487
488
489 /*
490  *      Activate a secondary processor.
491  */
492 extern void init_cpu_timer(void);
493 extern int pfault_init(void);
494 extern int pfault_token(void);
495
496 int __init start_secondary(void *cpuvoid)
497 {
498         /* Setup the cpu */
499         cpu_init();
500         /* Print info about this processor */
501         print_cpu_info(&safe_get_cpu_lowcore(smp_processor_id())->cpu_data);
502         /* Wait for completion of smp startup */
503         while (!atomic_read(&smp_commenced))
504                 /* nothing */ ;
505         /* init per CPU timer */
506         init_cpu_timer();
507 #ifdef CONFIG_PFAULT
508         /* Enable pfault pseudo page faults on this cpu. */
509         pfault_init();
510 #endif
511         /* cpu_idle will call schedule for us */
512         return cpu_idle(NULL);
513 }
514
515 /*
516  * The restart interrupt handler jumps to start_secondary directly
517  * without the detour over initialize_secondary. We defined it here
518  * so that the linker doesn't complain.
519  */
520 void __init initialize_secondary(void)
521 {
522 }
523
524 static int __init fork_by_hand(void)
525 {
526        struct pt_regs regs;
527        /* don't care about the psw and regs settings since we'll never
528           reschedule the forked task. */
529        memset(&regs,0,sizeof(struct pt_regs));
530        return do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0);
531 }
532
533 static void __init do_boot_cpu(int cpu)
534 {
535         struct task_struct *idle;
536         struct _lowcore    *cpu_lowcore;
537
538         /* We can't use kernel_thread since we must _avoid_ to reschedule
539            the child. */
540         if (fork_by_hand() < 0)
541                 panic("failed fork for CPU %d", cpu);
542
543         /*
544          * We remove it from the pidhash and the runqueue
545          * once we got the process:
546          */
547         idle = init_task.prev_task;
548         if (!idle)
549                 panic("No idle process for CPU %d",cpu);
550         idle->processor = cpu;
551         idle->cpus_runnable = 1 << cpu; /* we schedule the first task manually */
552
553         del_from_runqueue(idle);
554         unhash_process(idle);
555         init_tasks[cpu] = idle;
556
557         cpu_lowcore = get_cpu_lowcore(cpu);
558         cpu_lowcore->save_area[15] = idle->thread.ksp;
559         cpu_lowcore->kernel_stack = (__u32) idle + 8192;
560         __asm__ __volatile__("la    1,%0\n\t"
561                              "stctl 0,15,0(1)\n\t"
562                              "la    1,%1\n\t"
563                              "stam  0,15,0(1)"
564                              : "=m" (cpu_lowcore->cregs_save_area[0]),
565                                "=m" (cpu_lowcore->access_regs_save_area[0])
566                              : : "1", "memory");
567
568         eieio();
569         signal_processor(cpu,sigp_restart);
570         /* Mark this cpu as online */
571         set_bit(cpu, &cpu_online_map);
572 }
573
574 /*
575  *      Architecture specific routine called by the kernel just before init is
576  *      fired off. This allows the BP to have everything in order [we hope].
577  *      At the end of this all the APs will hit the system scheduling and off
578  *      we go. Each AP will load the system gdt's and jump through the kernel
579  *      init into idle(). At this point the scheduler will one day take over
580  *      and give them jobs to do. smp_callin is a standard routine
581  *      we use to track CPUs as they power up.
582  */
583
584 void __init smp_commence(void)
585 {
586         /*
587          *      Lets the callins below out of their loop.
588          */
589         atomic_set(&smp_commenced,1);
590 }
591
592 /*
593  *      Cycle through the processors sending sigp_restart to boot each.
594  */
595
596 void __init smp_boot_cpus(void)
597 {
598         unsigned long async_stack;
599         sigp_ccode   ccode;
600         int i;
601
602         /* request the 0x1202 external interrupt */
603         if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
604                 panic("Couldn't request external interrupt 0x1202");
605         smp_count_cpus();
606         memset(lowcore_ptr,0,sizeof(lowcore_ptr));  
607         
608         /*
609          *      Initialize the logical to physical CPU number mapping
610          */
611         print_cpu_info(&safe_get_cpu_lowcore(0)->cpu_data);
612
613         for(i = 0; i < smp_num_cpus; i++)
614         {
615                 lowcore_ptr[i] = (struct _lowcore *)
616                         __get_free_page(GFP_KERNEL|GFP_DMA);
617                 if (lowcore_ptr[i] == NULL)
618                         panic("smp_boot_cpus failed to "
619                               "allocate prefix memory\n");
620                 async_stack = __get_free_pages(GFP_KERNEL,1);
621                 if (async_stack == 0)
622                         panic("smp_boot_cpus failed to allocate "
623                               "asyncronous interrupt stack\n");
624
625                 memcpy(lowcore_ptr[i], &S390_lowcore, sizeof(struct _lowcore));
626                 lowcore_ptr[i]->async_stack = async_stack + (2 * PAGE_SIZE);
627                 /*
628                  * Most of the parameters are set up when the cpu is
629                  * started up.
630                  */
631                 if (smp_processor_id() == i)
632                         set_prefix((u32) lowcore_ptr[i]);
633                 else {
634                         ccode = signal_processor_p((u32)(lowcore_ptr[i]),
635                                                    i, sigp_set_prefix);
636                         if (ccode)
637                                 /* if this gets troublesome I'll have to do
638                                  * something about it. */
639                                 printk("ccode %d for cpu %d  returned when "
640                                        "setting prefix in smp_boot_cpus not good.\n",
641                                        (int) ccode, (int) i);
642                         else
643                                 do_boot_cpu(i);
644                 }
645         }
646 }
647
648 /*
649  * the frequency of the profiling timer can be changed
650  * by writing a multiplier value into /proc/profile.
651  *
652  * usually you want to run this on all CPUs ;)
653  */
654 int setup_profiling_timer(unsigned int multiplier)
655 {
656         return 0;
657 }
658
659 EXPORT_SYMBOL(lowcore_ptr);
660 EXPORT_SYMBOL(kernel_flag);
661 EXPORT_SYMBOL(smp_ctl_set_bit);
662 EXPORT_SYMBOL(smp_ctl_clear_bit);
663 EXPORT_SYMBOL(smp_num_cpus);
664 EXPORT_SYMBOL(smp_call_function);