blob: 65b52320d145661f245f836318d51b7bfb71a317 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/smp.c
3 *
Heiko Carstens255acee2006-02-17 13:52:46 -08004 * Copyright (C) IBM Corp. 1999,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
26#include <linux/spinlock.h>
27#include <linux/kernel_stat.h>
28#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
30#include <linux/cache.h>
31#include <linux/interrupt.h>
32#include <linux/cpu.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010033#include <linux/timex.h>
34#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/sigp.h>
36#include <asm/pgalloc.h>
37#include <asm/irq.h>
38#include <asm/s390_ext.h>
39#include <asm/cpcmd.h>
40#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010041#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043extern volatile int __cpu_logical_map[];
44
45/*
46 * An array with a pointer the lowcore of every CPU.
47 */
48
49struct _lowcore *lowcore_ptr[NR_CPUS];
50
Heiko Carstens255acee2006-02-17 13:52:46 -080051cpumask_t cpu_online_map = CPU_MASK_NONE;
52cpumask_t cpu_possible_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static struct task_struct *current_set[NR_CPUS];
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static void smp_ext_bitcall(int, ec_bit_sig);
57static void smp_ext_bitcall_others(ec_bit_sig);
58
59/*
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200605B * Structure and data for smp_call_function(). This is designed to minimise
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * static memory requirements. It also looks cleaner.
62 */
63static DEFINE_SPINLOCK(call_lock);
64
65struct call_data_struct {
66 void (*func) (void *info);
67 void *info;
68 atomic_t started;
69 atomic_t finished;
70 int wait;
71};
72
73static struct call_data_struct * call_data;
74
75/*
76 * 'Call function' interrupt callback
77 */
78static void do_call_function(void)
79{
80 void (*func) (void *info) = call_data->func;
81 void *info = call_data->info;
82 int wait = call_data->wait;
83
84 atomic_inc(&call_data->started);
85 (*func)(info);
86 if (wait)
87 atomic_inc(&call_data->finished);
88}
89
90/*
91 * this function sends a 'generic call function' IPI to all other CPUs
92 * in the system.
93 */
94
95int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
96 int wait)
97/*
98 * [SUMMARY] Run a function on all other CPUs.
99 * <func> The function to run. This must be fast and non-blocking.
100 * <info> An arbitrary pointer to pass to the function.
101 * <nonatomic> currently unused.
102 * <wait> If true, wait (atomically) until function has completed on other CPUs.
103 * [RETURNS] 0 on success, else a negative status code. Does not return until
104 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
105 *
106 * You must not call this function with disabled interrupts or from a
107 * hardware interrupt handler or from a bottom half handler.
108 */
109{
110 struct call_data_struct data;
111 int cpus = num_online_cpus()-1;
112
113 if (cpus <= 0)
114 return 0;
115
116 /* Can deadlock when called with interrupts disabled */
117 WARN_ON(irqs_disabled());
118
119 data.func = func;
120 data.info = info;
121 atomic_set(&data.started, 0);
122 data.wait = wait;
123 if (wait)
124 atomic_set(&data.finished, 0);
125
126 spin_lock(&call_lock);
127 call_data = &data;
128 /* Send a message to all other CPUs and wait for them to respond */
129 smp_ext_bitcall_others(ec_call_function);
130
131 /* Wait for response */
132 while (atomic_read(&data.started) != cpus)
133 cpu_relax();
134
135 if (wait)
136 while (atomic_read(&data.finished) != cpus)
137 cpu_relax();
138 spin_unlock(&call_lock);
139
140 return 0;
141}
142
143/*
144 * Call a function on one CPU
145 * cpu : the CPU the function should be executed on
146 *
147 * You must not call this function with disabled interrupts or from a
148 * hardware interrupt handler. You may call it from a bottom half.
149 *
150 * It is guaranteed that the called function runs on the specified CPU,
151 * preemption is disabled.
152 */
153int smp_call_function_on(void (*func) (void *info), void *info,
154 int nonatomic, int wait, int cpu)
155{
156 struct call_data_struct data;
157 int curr_cpu;
158
159 if (!cpu_online(cpu))
160 return -EINVAL;
161
162 /* disable preemption for local function call */
163 curr_cpu = get_cpu();
164
165 if (curr_cpu == cpu) {
166 /* direct call to function */
167 func(info);
168 put_cpu();
169 return 0;
170 }
171
172 data.func = func;
173 data.info = info;
174 atomic_set(&data.started, 0);
175 data.wait = wait;
176 if (wait)
177 atomic_set(&data.finished, 0);
178
179 spin_lock_bh(&call_lock);
180 call_data = &data;
181 smp_ext_bitcall(cpu, ec_call_function);
182
183 /* Wait for response */
184 while (atomic_read(&data.started) != 1)
185 cpu_relax();
186
187 if (wait)
188 while (atomic_read(&data.finished) != 1)
189 cpu_relax();
190
191 spin_unlock_bh(&call_lock);
192 put_cpu();
193 return 0;
194}
195EXPORT_SYMBOL(smp_call_function_on);
196
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100197static void do_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int cpu, rc;
200
201 /* stop all processors */
202 for_each_online_cpu(cpu) {
203 if (cpu == smp_processor_id())
204 continue;
205 do {
206 rc = signal_processor(cpu, sigp_stop);
207 } while (rc == sigp_busy);
208 }
209}
210
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100211static void do_store_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int cpu, rc;
214
215 /* store status of all processors in their lowcores (real 0) */
216 for_each_online_cpu(cpu) {
217 if (cpu == smp_processor_id())
218 continue;
219 do {
220 rc = signal_processor_p(
221 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
222 sigp_store_status_at_address);
223 } while(rc == sigp_busy);
224 }
225}
226
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100227static void do_wait_for_stop(void)
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100228{
229 int cpu;
230
231 /* Wait for all other cpus to enter stopped state */
232 for_each_online_cpu(cpu) {
233 if (cpu == smp_processor_id())
234 continue;
235 while(!smp_cpu_not_running(cpu))
236 cpu_relax();
237 }
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * this function sends a 'stop' sigp to all other CPUs in the system.
242 * it goes straight through.
243 */
244void smp_send_stop(void)
245{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100246 /* Disable all interrupts/machine checks */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100247 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* write magic number to zero page (absolute 0) */
250 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
251
252 /* stop other processors. */
253 do_send_stop();
254
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100255 /* wait until other processors are stopped */
256 do_wait_for_stop();
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* store status of other processors. */
259 do_store_status();
260}
261
262/*
263 * Reboot, halt and power_off routines for SMP.
264 */
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266void machine_restart_smp(char * __unused)
267{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100268 smp_send_stop();
269 do_reipl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272void machine_halt_smp(void)
273{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100274 smp_send_stop();
275 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
276 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
277 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
278 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281void machine_power_off_smp(void)
282{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100283 smp_send_stop();
284 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
285 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
286 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
287 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * This is the main routine where commands issued by other
292 * cpus are handled.
293 */
294
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100295static void do_ext_call_interrupt(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 unsigned long bits;
298
299 /*
300 * handle bit signal external calls
301 *
302 * For the ec_schedule signal we have to do nothing. All the work
303 * is done automatically when we return from the interrupt.
304 */
305 bits = xchg(&S390_lowcore.ext_call_fast, 0);
306
307 if (test_bit(ec_call_function, &bits))
308 do_call_function();
309}
310
311/*
312 * Send an external call sigp to another cpu and return without waiting
313 * for its completion.
314 */
315static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
316{
317 /*
318 * Set signaling bit in lowcore of target cpu and kick it
319 */
320 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700321 while(signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 udelay(10);
323}
324
325/*
326 * Send an external call sigp to every other cpu in the system and
327 * return without waiting for its completion.
328 */
329static void smp_ext_bitcall_others(ec_bit_sig sig)
330{
331 int cpu;
332
333 for_each_online_cpu(cpu) {
334 if (cpu == smp_processor_id())
335 continue;
336 /*
337 * Set signaling bit in lowcore of target cpu and kick it
338 */
339 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700340 while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 udelay(10);
342 }
343}
344
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800345#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * this function sends a 'purge tlb' signal to another CPU.
348 */
349void smp_ptlb_callback(void *info)
350{
351 local_flush_tlb();
352}
353
354void smp_ptlb_all(void)
355{
356 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
357}
358EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800359#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/*
362 * this function sends a 'reschedule' IPI to another CPU.
363 * it goes straight through and wastes no time serializing
364 * anything. Worst case is that we lose a reschedule ...
365 */
366void smp_send_reschedule(int cpu)
367{
368 smp_ext_bitcall(cpu, ec_schedule);
369}
370
371/*
372 * parameter area for the set/clear control bit callbacks
373 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200374struct ec_creg_mask_parms {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned long orvals[16];
376 unsigned long andvals[16];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200377};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/*
380 * callback for setting/clearing control bits
381 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100382static void smp_ctl_bit_callback(void *info) {
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200383 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned long cregs[16];
385 int i;
386
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200387 __ctl_store(cregs, 0, 15);
388 for (i = 0; i <= 15; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200390 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
394 * Set a bit in a control register of all cpus
395 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200396void smp_ctl_set_bit(int cr, int bit)
397{
398 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200400 memset(&parms.orvals, 0, sizeof(parms.orvals));
401 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 parms.orvals[cr] = 1 << bit;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200403 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/*
407 * Clear a bit in a control register of all cpus
408 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200409void smp_ctl_clear_bit(int cr, int bit)
410{
411 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200413 memset(&parms.orvals, 0, sizeof(parms.orvals));
414 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 parms.andvals[cr] = ~(1L << bit);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200416 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
420 * Lets check how many CPUs we have.
421 */
422
Heiko Carstens255acee2006-02-17 13:52:46 -0800423static unsigned int
424__init smp_count_cpus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Heiko Carstens255acee2006-02-17 13:52:46 -0800426 unsigned int cpu, num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 __u16 boot_cpu_addr;
428
429 /*
430 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
431 */
432
433 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
434 current_thread_info()->cpu = 0;
435 num_cpus = 1;
Heiko Carstens255acee2006-02-17 13:52:46 -0800436 for (cpu = 0; cpu <= 65535; cpu++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if ((__u16) cpu == boot_cpu_addr)
438 continue;
Heiko Carstens255acee2006-02-17 13:52:46 -0800439 __cpu_logical_map[1] = (__u16) cpu;
440 if (signal_processor(1, sigp_sense) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sigp_not_operational)
442 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 num_cpus++;
444 }
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 printk("Detected %d CPU's\n",(int) num_cpus);
447 printk("Boot cpu address %2X\n", boot_cpu_addr);
Heiko Carstens255acee2006-02-17 13:52:46 -0800448
449 return num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/*
453 * Activate a secondary processor.
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455int __devinit start_secondary(void *cpuvoid)
456{
457 /* Setup the cpu */
458 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800459 preempt_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100460 /* Enable TOD clock interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 init_cpu_timer();
462#ifdef CONFIG_VIRT_TIMER
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100463 /* Enable cpu timer interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 init_cpu_vtimer();
465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Enable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100467 pfault_init();
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Mark this cpu as online */
470 cpu_set(smp_processor_id(), cpu_online_map);
471 /* Switch on interrupts */
472 local_irq_enable();
473 /* Print info about this processor */
474 print_cpu_info(&S390_lowcore.cpu_data);
475 /* cpu_idle will call schedule for us */
476 cpu_idle();
477 return 0;
478}
479
480static void __init smp_create_idle(unsigned int cpu)
481{
482 struct task_struct *p;
483
484 /*
485 * don't care about the psw and regs settings since we'll never
486 * reschedule the forked task.
487 */
488 p = fork_idle(cpu);
489 if (IS_ERR(p))
490 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
491 current_set[cpu] = p;
492}
493
494/* Reserving and releasing of CPUs */
495
496static DEFINE_SPINLOCK(smp_reserve_lock);
497static int smp_cpu_reserved[NR_CPUS];
498
499int
500smp_get_cpu(cpumask_t cpu_mask)
501{
502 unsigned long flags;
503 int cpu;
504
505 spin_lock_irqsave(&smp_reserve_lock, flags);
506 /* Try to find an already reserved cpu. */
507 for_each_cpu_mask(cpu, cpu_mask) {
508 if (smp_cpu_reserved[cpu] != 0) {
509 smp_cpu_reserved[cpu]++;
510 /* Found one. */
511 goto out;
512 }
513 }
514 /* Reserve a new cpu from cpu_mask. */
515 for_each_cpu_mask(cpu, cpu_mask) {
516 if (cpu_online(cpu)) {
517 smp_cpu_reserved[cpu]++;
518 goto out;
519 }
520 }
521 cpu = -ENODEV;
522out:
523 spin_unlock_irqrestore(&smp_reserve_lock, flags);
524 return cpu;
525}
526
527void
528smp_put_cpu(int cpu)
529{
530 unsigned long flags;
531
532 spin_lock_irqsave(&smp_reserve_lock, flags);
533 smp_cpu_reserved[cpu]--;
534 spin_unlock_irqrestore(&smp_reserve_lock, flags);
535}
536
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100537static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538cpu_stopped(int cpu)
539{
540 __u32 status;
541
542 /* Check for stopped state */
543 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
544 if (status & 0x40)
545 return 1;
546 }
547 return 0;
548}
549
550/* Upping and downing of CPUs */
551
552int
553__cpu_up(unsigned int cpu)
554{
555 struct task_struct *idle;
556 struct _lowcore *cpu_lowcore;
557 struct stack_frame *sf;
558 sigp_ccode ccode;
559 int curr_cpu;
560
561 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
562 __cpu_logical_map[cpu] = (__u16) curr_cpu;
563 if (cpu_stopped(cpu))
564 break;
565 }
566
567 if (!cpu_stopped(cpu))
568 return -ENODEV;
569
570 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
571 cpu, sigp_set_prefix);
572 if (ccode){
573 printk("sigp_set_prefix failed for cpu %d "
574 "with condition code %d\n",
575 (int) cpu, (int) ccode);
576 return -EIO;
577 }
578
579 idle = current_set[cpu];
580 cpu_lowcore = lowcore_ptr[cpu];
581 cpu_lowcore->kernel_stack = (unsigned long)
Al Viro30af7122006-01-12 01:05:50 -0800582 task_stack_page(idle) + (THREAD_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
584 - sizeof(struct pt_regs)
585 - sizeof(struct stack_frame));
586 memset(sf, 0, sizeof(struct stack_frame));
587 sf->gprs[9] = (unsigned long) sf;
588 cpu_lowcore->save_area[15] = (unsigned long) sf;
589 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200590 asm volatile(
591 " stam 0,15,0(%0)"
592 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
594 cpu_lowcore->current_task = (unsigned long) idle;
595 cpu_lowcore->cpu_data.cpu_nr = cpu;
596 eieio();
Michael Ryan699ff132006-03-24 03:15:17 -0800597
598 while (signal_processor(cpu,sigp_restart) == sigp_busy)
599 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 while (!cpu_online(cpu))
602 cpu_relax();
603 return 0;
604}
605
Heiko Carstens255acee2006-02-17 13:52:46 -0800606static unsigned int __initdata additional_cpus;
Heiko Carstens37a33022006-02-17 13:52:47 -0800607static unsigned int __initdata possible_cpus;
Heiko Carstens255acee2006-02-17 13:52:46 -0800608
609void __init smp_setup_cpu_possible_map(void)
610{
Heiko Carstens54330452006-02-17 13:52:48 -0800611 unsigned int phy_cpus, pos_cpus, cpu;
Heiko Carstens255acee2006-02-17 13:52:46 -0800612
Heiko Carstens54330452006-02-17 13:52:48 -0800613 phy_cpus = smp_count_cpus();
614 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800615
Heiko Carstens37a33022006-02-17 13:52:47 -0800616 if (possible_cpus)
Heiko Carstens54330452006-02-17 13:52:48 -0800617 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800618
Heiko Carstens54330452006-02-17 13:52:48 -0800619 for (cpu = 0; cpu < pos_cpus; cpu++)
Heiko Carstens255acee2006-02-17 13:52:46 -0800620 cpu_set(cpu, cpu_possible_map);
621
Heiko Carstens54330452006-02-17 13:52:48 -0800622 phy_cpus = min(phy_cpus, pos_cpus);
623
624 for (cpu = 0; cpu < phy_cpus; cpu++)
625 cpu_set(cpu, cpu_present_map);
Heiko Carstens255acee2006-02-17 13:52:46 -0800626}
627
628#ifdef CONFIG_HOTPLUG_CPU
629
630static int __init setup_additional_cpus(char *s)
631{
632 additional_cpus = simple_strtoul(s, NULL, 0);
633 return 0;
634}
635early_param("additional_cpus", setup_additional_cpus);
636
Heiko Carstens37a33022006-02-17 13:52:47 -0800637static int __init setup_possible_cpus(char *s)
638{
639 possible_cpus = simple_strtoul(s, NULL, 0);
640 return 0;
641}
642early_param("possible_cpus", setup_possible_cpus);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644int
645__cpu_disable(void)
646{
647 unsigned long flags;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200648 struct ec_creg_mask_parms cr_parms;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700649 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 spin_lock_irqsave(&smp_reserve_lock, flags);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700652 if (smp_cpu_reserved[cpu] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 spin_unlock_irqrestore(&smp_reserve_lock, flags);
654 return -EBUSY;
655 }
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700656 cpu_clear(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* Disable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100659 pfault_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200661 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
662 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200664 /* disable all external interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 cr_parms.orvals[0] = 0;
666 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
667 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* disable all I/O interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 cr_parms.orvals[6] = 0;
670 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
671 1<<27 | 1<<26 | 1<<25 | 1<<24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* disable most machine checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 cr_parms.orvals[14] = 0;
674 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 smp_ctl_bit_callback(&cr_parms);
677
678 spin_unlock_irqrestore(&smp_reserve_lock, flags);
679 return 0;
680}
681
682void
683__cpu_die(unsigned int cpu)
684{
685 /* Wait until target cpu is down */
686 while (!smp_cpu_not_running(cpu))
687 cpu_relax();
688 printk("Processor %d spun down\n", cpu);
689}
690
691void
692cpu_die(void)
693{
694 idle_task_exit();
695 signal_processor(smp_processor_id(), sigp_stop);
696 BUG();
697 for(;;);
698}
699
Heiko Carstens255acee2006-02-17 13:52:46 -0800700#endif /* CONFIG_HOTPLUG_CPU */
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
703 * Cycle through the processors and setup structures.
704 */
705
706void __init smp_prepare_cpus(unsigned int max_cpus)
707{
708 unsigned long stack;
709 unsigned int cpu;
710 int i;
711
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700712 /* request the 0x1201 emergency signal external interrupt */
713 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
714 panic("Couldn't request external interrupt 0x1201");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
716 /*
717 * Initialize prefix pages and stacks for all possible cpus
718 */
719 print_cpu_info(&S390_lowcore.cpu_data);
720
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800721 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 lowcore_ptr[i] = (struct _lowcore *)
723 __get_free_pages(GFP_KERNEL|GFP_DMA,
724 sizeof(void*) == 8 ? 1 : 0);
725 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
726 if (lowcore_ptr[i] == NULL || stack == 0ULL)
727 panic("smp_boot_cpus failed to allocate memory\n");
728
729 *(lowcore_ptr[i]) = S390_lowcore;
730 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 stack = __get_free_pages(GFP_KERNEL,0);
732 if (stack == 0ULL)
733 panic("smp_boot_cpus failed to allocate memory\n");
734 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800735#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700736 if (MACHINE_HAS_IEEE) {
737 lowcore_ptr[i]->extended_save_area_addr =
738 (__u32) __get_free_pages(GFP_KERNEL,0);
739 if (lowcore_ptr[i]->extended_save_area_addr == 0)
740 panic("smp_boot_cpus failed to "
741 "allocate memory\n");
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743#endif
744 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800745#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700746 if (MACHINE_HAS_IEEE)
747 ctl_set_bit(14, 29); /* enable extended save area */
748#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
750
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800751 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (cpu != smp_processor_id())
753 smp_create_idle(cpu);
754}
755
756void __devinit smp_prepare_boot_cpu(void)
757{
758 BUG_ON(smp_processor_id() != 0);
759
760 cpu_set(0, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 S390_lowcore.percpu_offset = __per_cpu_offset[0];
762 current_set[0] = current;
763}
764
765void smp_cpus_done(unsigned int max_cpus)
766{
Heiko Carstens54330452006-02-17 13:52:48 -0800767 cpu_present_map = cpu_possible_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * the frequency of the profiling timer can be changed
772 * by writing a multiplier value into /proc/profile.
773 *
774 * usually you want to run this on all CPUs ;)
775 */
776int setup_profiling_timer(unsigned int multiplier)
777{
778 return 0;
779}
780
781static DEFINE_PER_CPU(struct cpu, cpu_devices);
782
783static int __init topology_init(void)
784{
785 int cpu;
786 int ret;
787
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800788 for_each_possible_cpu(cpu) {
Heiko Carstens6721f772007-01-09 10:18:44 +0100789 struct cpu *c = &per_cpu(cpu_devices, cpu);
790
791 c->hotpluggable = 1;
792 ret = register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (ret)
794 printk(KERN_WARNING "topology_init: register_cpu %d "
795 "failed (%d)\n", cpu, ret);
796 }
797 return 0;
798}
799
800subsys_initcall(topology_init);
801
Heiko Carstens255acee2006-02-17 13:52:46 -0800802EXPORT_SYMBOL(cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803EXPORT_SYMBOL(cpu_possible_map);
804EXPORT_SYMBOL(lowcore_ptr);
805EXPORT_SYMBOL(smp_ctl_set_bit);
806EXPORT_SYMBOL(smp_ctl_clear_bit);
807EXPORT_SYMBOL(smp_call_function);
808EXPORT_SYMBOL(smp_get_cpu);
809EXPORT_SYMBOL(smp_put_cpu);