blob: ecaa432a99f8cca9e64701290dcc62c5448cc673 [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>
Michael Holzheu46b05d22007-02-21 10:55:21 +010034#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010035#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/sigp.h>
37#include <asm/pgalloc.h>
38#include <asm/irq.h>
39#include <asm/s390_ext.h>
40#include <asm/cpcmd.h>
41#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010042#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044extern volatile int __cpu_logical_map[];
45
46/*
47 * An array with a pointer the lowcore of every CPU.
48 */
49
50struct _lowcore *lowcore_ptr[NR_CPUS];
51
Heiko Carstens255acee2006-02-17 13:52:46 -080052cpumask_t cpu_online_map = CPU_MASK_NONE;
53cpumask_t cpu_possible_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static struct task_struct *current_set[NR_CPUS];
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void smp_ext_bitcall(int, ec_bit_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
Jan Glauber63db6e82007-02-21 10:55:06 +010060 * Structure and data for __smp_call_function_map(). This is designed to
61 * minimise static memory requirements. It also looks cleaner.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63static DEFINE_SPINLOCK(call_lock);
64
65struct call_data_struct {
66 void (*func) (void *info);
67 void *info;
Jan Glauber63db6e82007-02-21 10:55:06 +010068 cpumask_t started;
69 cpumask_t finished;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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
Jan Glauber63db6e82007-02-21 10:55:06 +010084 cpu_set(smp_processor_id(), call_data->started);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 (*func)(info);
86 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +010087 cpu_set(smp_processor_id(), call_data->finished);;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Jan Glauber63db6e82007-02-21 10:55:06 +010090static void __smp_call_function_map(void (*func) (void *info), void *info,
91 int nonatomic, int wait, cpumask_t map)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct call_data_struct data;
Jan Glauber63db6e82007-02-21 10:55:06 +010094 int cpu, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jan Glauber63db6e82007-02-21 10:55:06 +010096 /*
97 * Can deadlock when interrupts are disabled or if in wrong context,
98 * caller must disable preemption
99 */
100 WARN_ON(irqs_disabled() || in_irq() || preemptible());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jan Glauber63db6e82007-02-21 10:55:06 +0100102 /*
103 * Check for local function call. We have to have the same call order
104 * as in on_each_cpu() because of machine_restart_smp().
105 */
106 if (cpu_isset(smp_processor_id(), map)) {
107 local = 1;
108 cpu_clear(smp_processor_id(), map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Jan Glauber63db6e82007-02-21 10:55:06 +0100111 cpus_and(map, map, cpu_online_map);
112 if (cpus_empty(map))
113 goto out;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 data.func = func;
116 data.info = info;
Jan Glauber63db6e82007-02-21 10:55:06 +0100117 data.started = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 data.wait = wait;
119 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +0100120 data.finished = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 spin_lock_bh(&call_lock);
123 call_data = &data;
Jan Glauber63db6e82007-02-21 10:55:06 +0100124
125 for_each_cpu_mask(cpu, map)
126 smp_ext_bitcall(cpu, ec_call_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* Wait for response */
Jan Glauber63db6e82007-02-21 10:55:06 +0100129 while (!cpus_equal(map, data.started))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 cpu_relax();
131
132 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +0100133 while (!cpus_equal(map, data.finished))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 cpu_relax();
135
136 spin_unlock_bh(&call_lock);
Jan Glauber63db6e82007-02-21 10:55:06 +0100137
138out:
139 local_irq_disable();
140 if (local)
141 func(info);
142 local_irq_enable();
143}
144
145/*
146 * smp_call_function:
147 * @func: the function to run; this must be fast and non-blocking
148 * @info: an arbitrary pointer to pass to the function
149 * @nonatomic: unused
150 * @wait: if true, wait (atomically) until function has completed on other CPUs
151 *
152 * Run a function on all other CPUs.
153 *
154 * You must not call this function with disabled interrupts or from a
155 * hardware interrupt handler. Must be called with preemption disabled.
156 * You may call it from a bottom half.
157 */
158int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
159 int wait)
160{
161 cpumask_t map;
162
163 map = cpu_online_map;
164 cpu_clear(smp_processor_id(), map);
165 __smp_call_function_map(func, info, nonatomic, wait, map);
166 return 0;
167}
168EXPORT_SYMBOL(smp_call_function);
169
170/*
171 * smp_call_function_on:
172 * @func: the function to run; this must be fast and non-blocking
173 * @info: an arbitrary pointer to pass to the function
174 * @nonatomic: unused
175 * @wait: if true, wait (atomically) until function has completed on other CPUs
176 * @cpu: the CPU where func should run
177 *
178 * Run a function on one processor.
179 *
180 * You must not call this function with disabled interrupts or from a
181 * hardware interrupt handler. Must be called with preemption disabled.
182 * You may call it from a bottom half.
183 */
184int smp_call_function_on(void (*func) (void *info), void *info, int nonatomic,
185 int wait, int cpu)
186{
187 cpumask_t map = CPU_MASK_NONE;
188
189 cpu_set(cpu, map);
190 __smp_call_function_map(func, info, nonatomic, wait, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193EXPORT_SYMBOL(smp_call_function_on);
194
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100195static void do_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int cpu, rc;
198
199 /* stop all processors */
200 for_each_online_cpu(cpu) {
201 if (cpu == smp_processor_id())
202 continue;
203 do {
204 rc = signal_processor(cpu, sigp_stop);
205 } while (rc == sigp_busy);
206 }
207}
208
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100209static void do_store_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int cpu, rc;
212
213 /* store status of all processors in their lowcores (real 0) */
214 for_each_online_cpu(cpu) {
215 if (cpu == smp_processor_id())
216 continue;
217 do {
218 rc = signal_processor_p(
219 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
220 sigp_store_status_at_address);
221 } while(rc == sigp_busy);
222 }
223}
224
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100225static void do_wait_for_stop(void)
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100226{
227 int cpu;
228
229 /* Wait for all other cpus to enter stopped state */
230 for_each_online_cpu(cpu) {
231 if (cpu == smp_processor_id())
232 continue;
233 while(!smp_cpu_not_running(cpu))
234 cpu_relax();
235 }
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * this function sends a 'stop' sigp to all other CPUs in the system.
240 * it goes straight through.
241 */
242void smp_send_stop(void)
243{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100244 /* Disable all interrupts/machine checks */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100245 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* write magic number to zero page (absolute 0) */
248 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
249
250 /* stop other processors. */
251 do_send_stop();
252
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100253 /* wait until other processors are stopped */
254 do_wait_for_stop();
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* store status of other processors. */
257 do_store_status();
258}
259
260/*
261 * Reboot, halt and power_off routines for SMP.
262 */
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264void machine_restart_smp(char * __unused)
265{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100266 smp_send_stop();
267 do_reipl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270void machine_halt_smp(void)
271{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100272 smp_send_stop();
273 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
274 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
275 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
276 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279void machine_power_off_smp(void)
280{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100281 smp_send_stop();
282 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
283 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
284 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
285 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * This is the main routine where commands issued by other
290 * cpus are handled.
291 */
292
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100293static void do_ext_call_interrupt(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 unsigned long bits;
296
297 /*
298 * handle bit signal external calls
299 *
300 * For the ec_schedule signal we have to do nothing. All the work
301 * is done automatically when we return from the interrupt.
302 */
303 bits = xchg(&S390_lowcore.ext_call_fast, 0);
304
305 if (test_bit(ec_call_function, &bits))
306 do_call_function();
307}
308
309/*
310 * Send an external call sigp to another cpu and return without waiting
311 * for its completion.
312 */
313static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
314{
315 /*
316 * Set signaling bit in lowcore of target cpu and kick it
317 */
318 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700319 while(signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 udelay(10);
321}
322
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800323#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * this function sends a 'purge tlb' signal to another CPU.
326 */
327void smp_ptlb_callback(void *info)
328{
329 local_flush_tlb();
330}
331
332void smp_ptlb_all(void)
333{
334 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
335}
336EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800337#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/*
340 * this function sends a 'reschedule' IPI to another CPU.
341 * it goes straight through and wastes no time serializing
342 * anything. Worst case is that we lose a reschedule ...
343 */
344void smp_send_reschedule(int cpu)
345{
346 smp_ext_bitcall(cpu, ec_schedule);
347}
348
349/*
350 * parameter area for the set/clear control bit callbacks
351 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200352struct ec_creg_mask_parms {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned long orvals[16];
354 unsigned long andvals[16];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200355};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/*
358 * callback for setting/clearing control bits
359 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100360static void smp_ctl_bit_callback(void *info) {
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200361 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long cregs[16];
363 int i;
364
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200365 __ctl_store(cregs, 0, 15);
366 for (i = 0; i <= 15; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200368 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371/*
372 * Set a bit in a control register of all cpus
373 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200374void smp_ctl_set_bit(int cr, int bit)
375{
376 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200378 memset(&parms.orvals, 0, sizeof(parms.orvals));
379 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 parms.orvals[cr] = 1 << bit;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200381 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/*
385 * Clear a bit in a control register of all cpus
386 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200387void smp_ctl_clear_bit(int cr, int bit)
388{
389 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200391 memset(&parms.orvals, 0, sizeof(parms.orvals));
392 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 parms.andvals[cr] = ~(1L << bit);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200394 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * Lets check how many CPUs we have.
399 */
400
Heiko Carstens255acee2006-02-17 13:52:46 -0800401static unsigned int
402__init smp_count_cpus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Heiko Carstens255acee2006-02-17 13:52:46 -0800404 unsigned int cpu, num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 __u16 boot_cpu_addr;
406
407 /*
408 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
409 */
410
411 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
412 current_thread_info()->cpu = 0;
413 num_cpus = 1;
Heiko Carstens255acee2006-02-17 13:52:46 -0800414 for (cpu = 0; cpu <= 65535; cpu++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if ((__u16) cpu == boot_cpu_addr)
416 continue;
Heiko Carstens255acee2006-02-17 13:52:46 -0800417 __cpu_logical_map[1] = (__u16) cpu;
418 if (signal_processor(1, sigp_sense) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sigp_not_operational)
420 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 num_cpus++;
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 printk("Detected %d CPU's\n",(int) num_cpus);
425 printk("Boot cpu address %2X\n", boot_cpu_addr);
Heiko Carstens255acee2006-02-17 13:52:46 -0800426
427 return num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * Activate a secondary processor.
432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433int __devinit start_secondary(void *cpuvoid)
434{
435 /* Setup the cpu */
436 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800437 preempt_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100438 /* Enable TOD clock interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 init_cpu_timer();
440#ifdef CONFIG_VIRT_TIMER
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100441 /* Enable cpu timer interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 init_cpu_vtimer();
443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Enable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100445 pfault_init();
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Mark this cpu as online */
448 cpu_set(smp_processor_id(), cpu_online_map);
449 /* Switch on interrupts */
450 local_irq_enable();
451 /* Print info about this processor */
452 print_cpu_info(&S390_lowcore.cpu_data);
453 /* cpu_idle will call schedule for us */
454 cpu_idle();
455 return 0;
456}
457
458static void __init smp_create_idle(unsigned int cpu)
459{
460 struct task_struct *p;
461
462 /*
463 * don't care about the psw and regs settings since we'll never
464 * reschedule the forked task.
465 */
466 p = fork_idle(cpu);
467 if (IS_ERR(p))
468 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
469 current_set[cpu] = p;
470}
471
472/* Reserving and releasing of CPUs */
473
474static DEFINE_SPINLOCK(smp_reserve_lock);
475static int smp_cpu_reserved[NR_CPUS];
476
477int
478smp_get_cpu(cpumask_t cpu_mask)
479{
480 unsigned long flags;
481 int cpu;
482
483 spin_lock_irqsave(&smp_reserve_lock, flags);
484 /* Try to find an already reserved cpu. */
485 for_each_cpu_mask(cpu, cpu_mask) {
486 if (smp_cpu_reserved[cpu] != 0) {
487 smp_cpu_reserved[cpu]++;
488 /* Found one. */
489 goto out;
490 }
491 }
492 /* Reserve a new cpu from cpu_mask. */
493 for_each_cpu_mask(cpu, cpu_mask) {
494 if (cpu_online(cpu)) {
495 smp_cpu_reserved[cpu]++;
496 goto out;
497 }
498 }
499 cpu = -ENODEV;
500out:
501 spin_unlock_irqrestore(&smp_reserve_lock, flags);
502 return cpu;
503}
504
505void
506smp_put_cpu(int cpu)
507{
508 unsigned long flags;
509
510 spin_lock_irqsave(&smp_reserve_lock, flags);
511 smp_cpu_reserved[cpu]--;
512 spin_unlock_irqrestore(&smp_reserve_lock, flags);
513}
514
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100515static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516cpu_stopped(int cpu)
517{
518 __u32 status;
519
520 /* Check for stopped state */
521 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
522 if (status & 0x40)
523 return 1;
524 }
525 return 0;
526}
527
528/* Upping and downing of CPUs */
529
530int
531__cpu_up(unsigned int cpu)
532{
533 struct task_struct *idle;
534 struct _lowcore *cpu_lowcore;
535 struct stack_frame *sf;
536 sigp_ccode ccode;
537 int curr_cpu;
538
539 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
540 __cpu_logical_map[cpu] = (__u16) curr_cpu;
541 if (cpu_stopped(cpu))
542 break;
543 }
544
545 if (!cpu_stopped(cpu))
546 return -ENODEV;
547
548 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
549 cpu, sigp_set_prefix);
550 if (ccode){
551 printk("sigp_set_prefix failed for cpu %d "
552 "with condition code %d\n",
553 (int) cpu, (int) ccode);
554 return -EIO;
555 }
556
557 idle = current_set[cpu];
558 cpu_lowcore = lowcore_ptr[cpu];
559 cpu_lowcore->kernel_stack = (unsigned long)
Al Viro30af7122006-01-12 01:05:50 -0800560 task_stack_page(idle) + (THREAD_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
562 - sizeof(struct pt_regs)
563 - sizeof(struct stack_frame));
564 memset(sf, 0, sizeof(struct stack_frame));
565 sf->gprs[9] = (unsigned long) sf;
566 cpu_lowcore->save_area[15] = (unsigned long) sf;
567 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200568 asm volatile(
569 " stam 0,15,0(%0)"
570 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
572 cpu_lowcore->current_task = (unsigned long) idle;
573 cpu_lowcore->cpu_data.cpu_nr = cpu;
574 eieio();
Michael Ryan699ff132006-03-24 03:15:17 -0800575
576 while (signal_processor(cpu,sigp_restart) == sigp_busy)
577 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 while (!cpu_online(cpu))
580 cpu_relax();
581 return 0;
582}
583
Heiko Carstens255acee2006-02-17 13:52:46 -0800584static unsigned int __initdata additional_cpus;
Heiko Carstens37a33022006-02-17 13:52:47 -0800585static unsigned int __initdata possible_cpus;
Heiko Carstens255acee2006-02-17 13:52:46 -0800586
587void __init smp_setup_cpu_possible_map(void)
588{
Heiko Carstens54330452006-02-17 13:52:48 -0800589 unsigned int phy_cpus, pos_cpus, cpu;
Heiko Carstens255acee2006-02-17 13:52:46 -0800590
Heiko Carstens54330452006-02-17 13:52:48 -0800591 phy_cpus = smp_count_cpus();
592 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800593
Heiko Carstens37a33022006-02-17 13:52:47 -0800594 if (possible_cpus)
Heiko Carstens54330452006-02-17 13:52:48 -0800595 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800596
Heiko Carstens54330452006-02-17 13:52:48 -0800597 for (cpu = 0; cpu < pos_cpus; cpu++)
Heiko Carstens255acee2006-02-17 13:52:46 -0800598 cpu_set(cpu, cpu_possible_map);
599
Heiko Carstens54330452006-02-17 13:52:48 -0800600 phy_cpus = min(phy_cpus, pos_cpus);
601
602 for (cpu = 0; cpu < phy_cpus; cpu++)
603 cpu_set(cpu, cpu_present_map);
Heiko Carstens255acee2006-02-17 13:52:46 -0800604}
605
606#ifdef CONFIG_HOTPLUG_CPU
607
608static int __init setup_additional_cpus(char *s)
609{
610 additional_cpus = simple_strtoul(s, NULL, 0);
611 return 0;
612}
613early_param("additional_cpus", setup_additional_cpus);
614
Heiko Carstens37a33022006-02-17 13:52:47 -0800615static int __init setup_possible_cpus(char *s)
616{
617 possible_cpus = simple_strtoul(s, NULL, 0);
618 return 0;
619}
620early_param("possible_cpus", setup_possible_cpus);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622int
623__cpu_disable(void)
624{
625 unsigned long flags;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200626 struct ec_creg_mask_parms cr_parms;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700627 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 spin_lock_irqsave(&smp_reserve_lock, flags);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700630 if (smp_cpu_reserved[cpu] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 spin_unlock_irqrestore(&smp_reserve_lock, flags);
632 return -EBUSY;
633 }
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700634 cpu_clear(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Disable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100637 pfault_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200639 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
640 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200642 /* disable all external interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 cr_parms.orvals[0] = 0;
644 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
645 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* disable all I/O interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 cr_parms.orvals[6] = 0;
648 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
649 1<<27 | 1<<26 | 1<<25 | 1<<24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* disable most machine checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 cr_parms.orvals[14] = 0;
652 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 smp_ctl_bit_callback(&cr_parms);
655
656 spin_unlock_irqrestore(&smp_reserve_lock, flags);
657 return 0;
658}
659
660void
661__cpu_die(unsigned int cpu)
662{
663 /* Wait until target cpu is down */
664 while (!smp_cpu_not_running(cpu))
665 cpu_relax();
666 printk("Processor %d spun down\n", cpu);
667}
668
669void
670cpu_die(void)
671{
672 idle_task_exit();
673 signal_processor(smp_processor_id(), sigp_stop);
674 BUG();
675 for(;;);
676}
677
Heiko Carstens255acee2006-02-17 13:52:46 -0800678#endif /* CONFIG_HOTPLUG_CPU */
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * Cycle through the processors and setup structures.
682 */
683
684void __init smp_prepare_cpus(unsigned int max_cpus)
685{
686 unsigned long stack;
687 unsigned int cpu;
688 int i;
689
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700690 /* request the 0x1201 emergency signal external interrupt */
691 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
692 panic("Couldn't request external interrupt 0x1201");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
694 /*
695 * Initialize prefix pages and stacks for all possible cpus
696 */
697 print_cpu_info(&S390_lowcore.cpu_data);
698
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800699 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 lowcore_ptr[i] = (struct _lowcore *)
701 __get_free_pages(GFP_KERNEL|GFP_DMA,
702 sizeof(void*) == 8 ? 1 : 0);
703 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
704 if (lowcore_ptr[i] == NULL || stack == 0ULL)
705 panic("smp_boot_cpus failed to allocate memory\n");
706
707 *(lowcore_ptr[i]) = S390_lowcore;
708 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 stack = __get_free_pages(GFP_KERNEL,0);
710 if (stack == 0ULL)
711 panic("smp_boot_cpus failed to allocate memory\n");
712 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800713#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700714 if (MACHINE_HAS_IEEE) {
715 lowcore_ptr[i]->extended_save_area_addr =
716 (__u32) __get_free_pages(GFP_KERNEL,0);
717 if (lowcore_ptr[i]->extended_save_area_addr == 0)
718 panic("smp_boot_cpus failed to "
719 "allocate memory\n");
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#endif
722 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800723#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700724 if (MACHINE_HAS_IEEE)
725 ctl_set_bit(14, 29); /* enable extended save area */
726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
728
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800729 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (cpu != smp_processor_id())
731 smp_create_idle(cpu);
732}
733
734void __devinit smp_prepare_boot_cpu(void)
735{
736 BUG_ON(smp_processor_id() != 0);
737
738 cpu_set(0, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 S390_lowcore.percpu_offset = __per_cpu_offset[0];
740 current_set[0] = current;
741}
742
743void smp_cpus_done(unsigned int max_cpus)
744{
Heiko Carstens54330452006-02-17 13:52:48 -0800745 cpu_present_map = cpu_possible_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/*
749 * the frequency of the profiling timer can be changed
750 * by writing a multiplier value into /proc/profile.
751 *
752 * usually you want to run this on all CPUs ;)
753 */
754int setup_profiling_timer(unsigned int multiplier)
755{
756 return 0;
757}
758
759static DEFINE_PER_CPU(struct cpu, cpu_devices);
760
761static int __init topology_init(void)
762{
763 int cpu;
764 int ret;
765
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800766 for_each_possible_cpu(cpu) {
Heiko Carstens6721f772007-01-09 10:18:44 +0100767 struct cpu *c = &per_cpu(cpu_devices, cpu);
768
769 c->hotpluggable = 1;
770 ret = register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (ret)
772 printk(KERN_WARNING "topology_init: register_cpu %d "
773 "failed (%d)\n", cpu, ret);
774 }
775 return 0;
776}
777
778subsys_initcall(topology_init);
779
Heiko Carstens255acee2006-02-17 13:52:46 -0800780EXPORT_SYMBOL(cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781EXPORT_SYMBOL(cpu_possible_map);
782EXPORT_SYMBOL(lowcore_ptr);
783EXPORT_SYMBOL(smp_ctl_set_bit);
784EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785EXPORT_SYMBOL(smp_get_cpu);
786EXPORT_SYMBOL(smp_put_cpu);