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
nosmp(char * str)71 static int __init nosmp(char *str)
72 {
73 max_cpus = 0;
74 return 1;
75 }
76
77 __setup("nosmp", nosmp);
78
maxcpus(char * str)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 void 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 */
do_call_function(void)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
smp_call_function(void (* func)(void * info),void * info,int nonatomic,int wait)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 /*
180 * Call a function only on one CPU
181 * cpu : the CPU the function should be executed on
182 *
183 * You must not call this function with disabled interrupts or from a
184 * hardware interrupt handler, you may call it from a bottom half handler.
185 */
smp_call_function_on(void (* func)(void * info),void * info,int nonatomic,int wait,int cpu)186 int smp_call_function_on(void (*func) (void *info), void *info,
187 int nonatomic, int wait, int cpu)
188 {
189 struct call_data_struct data;
190
191 if (!atomic_read(&smp_commenced))
192 return 0;
193
194 if (smp_processor_id() == cpu) {
195 /* direct call to function */
196 func(info);
197 return 0;
198 }
199
200 data.func = func;
201 data.info = info;
202
203 atomic_set(&data.started, 0);
204 data.wait = wait;
205 if (wait)
206 atomic_set(&data.finished, 0);
207
208 spin_lock_bh(&call_lock);
209 call_data = &data;
210 smp_ext_bitcall(cpu, ec_call_function);
211
212 /* Wait for response */
213 while (atomic_read(&data.started) != 1)
214 barrier();
215
216 if (wait)
217 while (atomic_read(&data.finished) != 1)
218 barrier();
219
220 spin_unlock_bh(&call_lock);
221 return 0;
222 }
223
224
do_send_stop(void)225 static inline void do_send_stop(void)
226 {
227 unsigned long dummy;
228 int i;
229
230 /* stop all processors */
231 for (i = 0; i < smp_num_cpus; i++) {
232 if (smp_processor_id() != i) {
233 int ccode;
234 do {
235 ccode = signal_processor_ps(
236 &dummy,
237 0,
238 i,
239 sigp_stop);
240 } while(ccode == sigp_busy);
241 }
242 }
243 }
244
do_store_status(void)245 static inline void do_store_status(void)
246 {
247 unsigned long low_core_addr;
248 unsigned long dummy;
249 int i;
250
251 /* store status of all processors in their lowcores (real 0) */
252 for (i = 0; i < smp_num_cpus; i++) {
253 if (smp_processor_id() != i) {
254 int ccode;
255 low_core_addr = (unsigned long)get_cpu_lowcore(i);
256 do {
257 ccode = signal_processor_ps(
258 &dummy,
259 low_core_addr,
260 i,
261 sigp_store_status_at_address);
262 } while(ccode == sigp_busy);
263 }
264 }
265 }
266
267 /*
268 * this function sends a 'stop' sigp to all other CPUs in the system.
269 * it goes straight through.
270 */
smp_send_stop(void)271 void smp_send_stop(void)
272 {
273 /* write magic number to zero page (absolute 0) */
274 get_cpu_lowcore(smp_processor_id())->panic_magic = __PANIC_MAGIC;
275
276 /* stop other processors. */
277 do_send_stop();
278
279 /* store status of other processors. */
280 do_store_status();
281 }
282
283
284 /*
285 * Reboot, halt and power_off routines for SMP.
286 */
287 static volatile unsigned long cpu_restart_map;
288
do_machine_restart(void * __unused)289 static void do_machine_restart(void * __unused)
290 {
291 clear_bit(smp_processor_id(), &cpu_restart_map);
292 if (smp_processor_id() == 0) {
293 /* Wait for all other cpus to enter do_machine_restart. */
294 while (cpu_restart_map != 0);
295 /* Store status of other cpus. */
296 do_store_status();
297 /*
298 * Finally call reipl. Because we waited for all other
299 * cpus to enter this function we know that they do
300 * not hold any s390irq-locks (the cpus have been
301 * interrupted by an external interrupt and s390irq
302 * locks are always held disabled).
303 */
304 reipl(S390_lowcore.ipl_device);
305 }
306 signal_processor(smp_processor_id(), sigp_stop);
307 }
308
machine_restart_smp(char * __unused)309 void machine_restart_smp(char * __unused)
310 {
311 cpu_restart_map = cpu_online_map;
312 smp_call_function(do_machine_restart, NULL, 0, 0);
313 do_machine_restart(NULL);
314 }
315
do_machine_halt(void * __unused)316 static void do_machine_halt(void * __unused)
317 {
318 if (smp_processor_id() == 0) {
319 smp_send_stop();
320 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
321 cpcmd(vmhalt_cmd, NULL, 0);
322 signal_processor(smp_processor_id(),
323 sigp_stop_and_store_status);
324 }
325 for (;;)
326 enabled_wait();
327 }
328
machine_halt_smp(void)329 void machine_halt_smp(void)
330 {
331 smp_call_function(do_machine_halt, NULL, 0, 0);
332 do_machine_halt(NULL);
333 }
334
do_machine_power_off(void * __unused)335 static void do_machine_power_off(void * __unused)
336 {
337 if (smp_processor_id() == 0) {
338 smp_send_stop();
339 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
340 cpcmd(vmpoff_cmd, NULL, 0);
341 signal_processor(smp_processor_id(),
342 sigp_stop_and_store_status);
343 }
344 for (;;)
345 enabled_wait();
346 }
347
machine_power_off_smp(void)348 void machine_power_off_smp(void)
349 {
350 smp_call_function(do_machine_power_off, NULL, 0, 0);
351 do_machine_power_off(NULL);
352 }
353
354 /*
355 * This is the main routine where commands issued by other
356 * cpus are handled.
357 */
358
do_ext_call_interrupt(struct pt_regs * regs,__u16 code)359 void do_ext_call_interrupt(struct pt_regs *regs, __u16 code)
360 {
361 unsigned long bits;
362
363 /*
364 * handle bit signal external calls
365 *
366 * For the ec_schedule signal we have to do nothing. All the work
367 * is done automatically when we return from the interrupt.
368 */
369 bits = xchg(&S390_lowcore.ext_call_fast, 0);
370
371 if (test_bit(ec_call_function, &bits))
372 do_call_function();
373 }
374
375 /*
376 * Send an external call sigp to another cpu and wait
377 * for its completion.
378 */
smp_ext_bitcall(int cpu,ec_bit_sig sig)379 static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
380 {
381 /*
382 * Set signaling bit in lowcore of target cpu and kick it
383 */
384 set_bit(sig, &(get_cpu_lowcore(cpu)->ext_call_fast));
385 while (signal_processor(cpu, sigp_external_call) == sigp_busy)
386 udelay(10);
387 }
388
389 /*
390 * Send an external call sigp to every other cpu in the system and
391 * wait for its completion.
392 */
smp_ext_bitcall_others(ec_bit_sig sig)393 static void smp_ext_bitcall_others(ec_bit_sig sig)
394 {
395 int i;
396
397 for (i = 0; i < smp_num_cpus; i++) {
398 if (smp_processor_id() == i)
399 continue;
400 /*
401 * Set signaling bit in lowcore of target cpu and kick it
402 */
403 set_bit(sig, &(get_cpu_lowcore(i)->ext_call_fast));
404 while (signal_processor(i, sigp_external_call) == sigp_busy)
405 udelay(10);
406 }
407 }
408
409 /*
410 * this function sends a 'reschedule' IPI to another CPU.
411 * it goes straight through and wastes no time serializing
412 * anything. Worst case is that we lose a reschedule ...
413 */
414
smp_send_reschedule(int cpu)415 void smp_send_reschedule(int cpu)
416 {
417 smp_ext_bitcall(cpu, ec_schedule);
418 }
419
420 /*
421 * parameter area for the set/clear control bit callbacks
422 */
423 typedef struct
424 {
425 __u16 start_ctl;
426 __u16 end_ctl;
427 __u64 orvals[16];
428 __u64 andvals[16];
429 } ec_creg_mask_parms;
430
431 /*
432 * callback for setting/clearing control bits
433 */
smp_ctl_bit_callback(void * info)434 void smp_ctl_bit_callback(void *info) {
435 ec_creg_mask_parms *pp;
436 u64 cregs[16];
437 int i;
438
439 pp = (ec_creg_mask_parms *) info;
440 asm volatile (" bras 1,0f\n"
441 " stctg 0,0,0(%0)\n"
442 "0: ex %1,0(1)\n"
443 : : "a" (cregs+pp->start_ctl),
444 "a" ((pp->start_ctl<<4) + pp->end_ctl)
445 : "memory", "1" );
446 for (i = pp->start_ctl; i <= pp->end_ctl; i++)
447 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
448 asm volatile (" bras 1,0f\n"
449 " lctlg 0,0,0(%0)\n"
450 "0: ex %1,0(1)\n"
451 : : "a" (cregs+pp->start_ctl),
452 "a" ((pp->start_ctl<<4) + pp->end_ctl)
453 : "memory", "1" );
454 }
455
456 /*
457 * Set a bit in a control register of all cpus
458 */
smp_ctl_set_bit(int cr,int bit)459 void smp_ctl_set_bit(int cr, int bit) {
460 ec_creg_mask_parms parms;
461
462 if (atomic_read(&smp_commenced) != 0) {
463 parms.start_ctl = cr;
464 parms.end_ctl = cr;
465 parms.orvals[cr] = 1 << bit;
466 parms.andvals[cr] = -1L;
467 smp_call_function(smp_ctl_bit_callback, &parms, 0, 1);
468 }
469 __ctl_set_bit(cr, bit);
470 }
471
472 /*
473 * Clear a bit in a control register of all cpus
474 */
smp_ctl_clear_bit(int cr,int bit)475 void smp_ctl_clear_bit(int cr, int bit) {
476 ec_creg_mask_parms parms;
477
478 if (atomic_read(&smp_commenced) != 0) {
479 parms.start_ctl = cr;
480 parms.end_ctl = cr;
481 parms.orvals[cr] = 0;
482 parms.andvals[cr] = ~(1L << bit);
483 smp_call_function(smp_ctl_bit_callback, &parms, 0, 1);
484 }
485 __ctl_clear_bit(cr, bit);
486 }
487
488
489 /*
490 * Lets check how many CPUs we have.
491 */
492
smp_count_cpus(void)493 void smp_count_cpus(void)
494 {
495 int curr_cpu;
496
497 current->processor = 0;
498 smp_num_cpus = 1;
499 cpu_online_map = 1;
500 for (curr_cpu = 0;
501 curr_cpu <= 65535 && smp_num_cpus < max_cpus; curr_cpu++) {
502 if ((__u16) curr_cpu == boot_cpu_addr)
503 continue;
504 __cpu_logical_map[smp_num_cpus] = (__u16) curr_cpu;
505 if (signal_processor(smp_num_cpus, sigp_sense) ==
506 sigp_not_operational)
507 continue;
508 smp_num_cpus++;
509 }
510 printk("Detected %d CPU's\n",(int) smp_num_cpus);
511 printk("Boot cpu address %2X\n", boot_cpu_addr);
512 }
513
514
515 /*
516 * Activate a secondary processor.
517 */
518 extern void init_cpu_timer(void);
519 extern int pfault_init(void);
520
start_secondary(void * cpuvoid)521 int __init start_secondary(void *cpuvoid)
522 {
523 /* Setup the cpu */
524 cpu_init();
525 /* Print info about this processor */
526 print_cpu_info(&safe_get_cpu_lowcore(smp_processor_id())->cpu_data);
527 /* Wait for completion of smp startup */
528 while (!atomic_read(&smp_commenced))
529 /* nothing */ ;
530 /* init per CPU timer */
531 init_cpu_timer();
532 #ifdef CONFIG_PFAULT
533 /* Enable pfault pseudo page faults on this cpu. */
534 pfault_init();
535 #endif
536 /* cpu_idle will call schedule for us */
537 return cpu_idle(NULL);
538 }
539
540 /*
541 * The restart interrupt handler jumps to start_secondary directly
542 * without the detour over initialize_secondary. We defined it here
543 * so that the linker doesn't complain.
544 */
initialize_secondary(void)545 void __init initialize_secondary(void)
546 {
547 }
548
fork_by_hand(void)549 static int __init fork_by_hand(void)
550 {
551 struct pt_regs regs;
552 /* don't care about the psw and regs settings since we'll never
553 reschedule the forked task. */
554 memset(®s,0,sizeof(struct pt_regs));
555 return do_fork(CLONE_VM|CLONE_PID, 0, ®s, 0);
556 }
557
do_boot_cpu(int cpu)558 static void __init do_boot_cpu(int cpu)
559 {
560 struct task_struct *idle;
561 struct _lowcore *cpu_lowcore;
562
563 /* We can't use kernel_thread since we must _avoid_ to reschedule
564 the child. */
565 if (fork_by_hand() < 0)
566 panic("failed fork for CPU %d", cpu);
567
568 /*
569 * We remove it from the pidhash and the runqueue
570 * once we got the process:
571 */
572 idle = init_task.prev_task;
573 if (!idle)
574 panic("No idle process for CPU %d",cpu);
575 idle->processor = cpu;
576 idle->cpus_runnable = 1 << cpu; /* we schedule the first task manually */
577
578 del_from_runqueue(idle);
579 unhash_process(idle);
580 init_tasks[cpu] = idle;
581
582 cpu_lowcore = get_cpu_lowcore(cpu);
583 cpu_lowcore->save_area[15] = idle->thread.ksp;
584 cpu_lowcore->kernel_stack = (__u64) idle + 16384;
585 __asm__ __volatile__("la 1,%0\n\t"
586 "stctg 0,15,0(1)\n\t"
587 "la 1,%1\n\t"
588 "stam 0,15,0(1)"
589 : "=m" (cpu_lowcore->cregs_save_area[0]),
590 "=m" (cpu_lowcore->access_regs_save_area[0])
591 : : "1", "memory");
592
593 eieio();
594 signal_processor(cpu,sigp_restart);
595 /* Mark this cpu as online. */
596 set_bit(cpu, &cpu_online_map);
597 }
598
599 /*
600 * Architecture specific routine called by the kernel just before init is
601 * fired off. This allows the BP to have everything in order [we hope].
602 * At the end of this all the APs will hit the system scheduling and off
603 * we go. Each AP will load the system gdt's and jump through the kernel
604 * init into idle(). At this point the scheduler will one day take over
605 * and give them jobs to do. smp_callin is a standard routine
606 * we use to track CPUs as they power up.
607 */
608
smp_commence(void)609 void __init smp_commence(void)
610 {
611 /*
612 * Lets the callins below out of their loop.
613 */
614 atomic_set(&smp_commenced,1);
615 }
616
617 /*
618 * Cycle through the processors sending restart sigps to boot each.
619 */
620
smp_boot_cpus(void)621 void __init smp_boot_cpus(void)
622 {
623 struct _lowcore *curr_lowcore;
624 unsigned long async_stack;
625 sigp_ccode ccode;
626 int i;
627
628 /* request the 0x1202 external interrupt */
629 if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
630 panic("Couldn't request external interrupt 0x1202");
631 smp_count_cpus();
632 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
633
634 /*
635 * Initialize the logical to physical CPU number mapping
636 */
637 print_cpu_info(&safe_get_cpu_lowcore(0)->cpu_data);
638
639 for(i = 0; i < smp_num_cpus; i++)
640 {
641 curr_lowcore = (struct _lowcore *)
642 __get_free_pages(GFP_KERNEL|GFP_DMA, 1);
643 if (curr_lowcore == NULL) {
644 printk("smp_boot_cpus failed to allocate prefix memory\n");
645 break;
646 }
647 async_stack = __get_free_pages(GFP_KERNEL,2);
648 if (async_stack == 0) {
649 printk("smp_boot_cpus failed to allocate asyncronous"
650 " interrupt stack\n");
651 free_page((unsigned long) curr_lowcore);
652 break;
653 }
654 lowcore_ptr[i] = curr_lowcore;
655 memcpy(curr_lowcore, &S390_lowcore, sizeof(struct _lowcore));
656 curr_lowcore->async_stack = async_stack + (4 * PAGE_SIZE);
657 /*
658 * Most of the parameters are set up when the cpu is
659 * started up.
660 */
661 if (smp_processor_id() == i)
662 set_prefix((u32)(u64)curr_lowcore);
663 else {
664 ccode = signal_processor_p((u64)(curr_lowcore),
665 i, sigp_set_prefix);
666 if(ccode) {
667 /* if this gets troublesome I'll have to do
668 * something about it. */
669 printk("ccode %d for cpu %d returned when "
670 "setting prefix in smp_boot_cpus not good.\n",
671 (int) ccode, (int) i);
672 }
673 else
674 do_boot_cpu(i);
675 }
676 }
677 }
678
679 /*
680 * the frequency of the profiling timer can be changed
681 * by writing a multiplier value into /proc/profile.
682 *
683 * usually you want to run this on all CPUs ;)
684 */
setup_profiling_timer(unsigned int multiplier)685 int setup_profiling_timer(unsigned int multiplier)
686 {
687 return 0;
688 }
689
690 EXPORT_SYMBOL(lowcore_ptr);
691 EXPORT_SYMBOL(kernel_flag);
692 EXPORT_SYMBOL(smp_ctl_set_bit);
693 EXPORT_SYMBOL(smp_ctl_clear_bit);
694 EXPORT_SYMBOL(smp_num_cpus);
695 EXPORT_SYMBOL(smp_call_function);
696
697