blob: 7c0143fdf7104dda08b81b7c950f7b695d21b10c [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 Holzheu411ed322007-04-27 16:01:49 +020034#include <linux/bootmem.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010035#include <asm/ipl.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010036#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/sigp.h>
38#include <asm/pgalloc.h>
39#include <asm/irq.h>
40#include <asm/s390_ext.h>
41#include <asm/cpcmd.h>
42#include <asm/tlbflush.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010043#include <asm/timer.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020044#include <asm/lowcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046extern volatile int __cpu_logical_map[];
47
48/*
49 * An array with a pointer the lowcore of every CPU.
50 */
51
52struct _lowcore *lowcore_ptr[NR_CPUS];
53
Heiko Carstens255acee2006-02-17 13:52:46 -080054cpumask_t cpu_online_map = CPU_MASK_NONE;
55cpumask_t cpu_possible_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct task_struct *current_set[NR_CPUS];
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void smp_ext_bitcall(int, ec_bit_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Jan Glauber63db6e82007-02-21 10:55:06 +010062 * Structure and data for __smp_call_function_map(). This is designed to
63 * minimise static memory requirements. It also looks cleaner.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65static DEFINE_SPINLOCK(call_lock);
66
67struct call_data_struct {
68 void (*func) (void *info);
69 void *info;
Jan Glauber63db6e82007-02-21 10:55:06 +010070 cpumask_t started;
71 cpumask_t finished;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int wait;
73};
74
75static struct call_data_struct * call_data;
76
77/*
78 * 'Call function' interrupt callback
79 */
80static void do_call_function(void)
81{
82 void (*func) (void *info) = call_data->func;
83 void *info = call_data->info;
84 int wait = call_data->wait;
85
Jan Glauber63db6e82007-02-21 10:55:06 +010086 cpu_set(smp_processor_id(), call_data->started);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 (*func)(info);
88 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +010089 cpu_set(smp_processor_id(), call_data->finished);;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Jan Glauber63db6e82007-02-21 10:55:06 +010092static void __smp_call_function_map(void (*func) (void *info), void *info,
93 int nonatomic, int wait, cpumask_t map)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct call_data_struct data;
Jan Glauber63db6e82007-02-21 10:55:06 +010096 int cpu, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jan Glauber63db6e82007-02-21 10:55:06 +010098 /*
Heiko Carstens25864162007-03-05 23:35:41 +010099 * Can deadlock when interrupts are disabled or if in wrong context.
Jan Glauber63db6e82007-02-21 10:55:06 +0100100 */
Heiko Carstens25864162007-03-05 23:35:41 +0100101 WARN_ON(irqs_disabled() || in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jan Glauber63db6e82007-02-21 10:55:06 +0100103 /*
104 * Check for local function call. We have to have the same call order
105 * as in on_each_cpu() because of machine_restart_smp().
106 */
107 if (cpu_isset(smp_processor_id(), map)) {
108 local = 1;
109 cpu_clear(smp_processor_id(), map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
Jan Glauber63db6e82007-02-21 10:55:06 +0100112 cpus_and(map, map, cpu_online_map);
113 if (cpus_empty(map))
114 goto out;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 data.func = func;
117 data.info = info;
Jan Glauber63db6e82007-02-21 10:55:06 +0100118 data.started = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 data.wait = wait;
120 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +0100121 data.finished = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 spin_lock_bh(&call_lock);
124 call_data = &data;
Jan Glauber63db6e82007-02-21 10:55:06 +0100125
126 for_each_cpu_mask(cpu, map)
127 smp_ext_bitcall(cpu, ec_call_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Wait for response */
Jan Glauber63db6e82007-02-21 10:55:06 +0100130 while (!cpus_equal(map, data.started))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 cpu_relax();
132
133 if (wait)
Jan Glauber63db6e82007-02-21 10:55:06 +0100134 while (!cpus_equal(map, data.finished))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 cpu_relax();
136
137 spin_unlock_bh(&call_lock);
Jan Glauber63db6e82007-02-21 10:55:06 +0100138
139out:
140 local_irq_disable();
141 if (local)
142 func(info);
143 local_irq_enable();
144}
145
146/*
147 * smp_call_function:
148 * @func: the function to run; this must be fast and non-blocking
149 * @info: an arbitrary pointer to pass to the function
150 * @nonatomic: unused
151 * @wait: if true, wait (atomically) until function has completed on other CPUs
152 *
153 * Run a function on all other CPUs.
154 *
155 * You must not call this function with disabled interrupts or from a
Heiko Carstens25864162007-03-05 23:35:41 +0100156 * hardware interrupt handler. You may call it from a bottom half.
Jan Glauber63db6e82007-02-21 10:55:06 +0100157 */
158int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
159 int wait)
160{
161 cpumask_t map;
162
Heiko Carstens25864162007-03-05 23:35:41 +0100163 preempt_disable();
Jan Glauber63db6e82007-02-21 10:55:06 +0100164 map = cpu_online_map;
165 cpu_clear(smp_processor_id(), map);
166 __smp_call_function_map(func, info, nonatomic, wait, map);
Heiko Carstens25864162007-03-05 23:35:41 +0100167 preempt_enable();
Jan Glauber63db6e82007-02-21 10:55:06 +0100168 return 0;
169}
170EXPORT_SYMBOL(smp_call_function);
171
172/*
173 * smp_call_function_on:
174 * @func: the function to run; this must be fast and non-blocking
175 * @info: an arbitrary pointer to pass to the function
176 * @nonatomic: unused
177 * @wait: if true, wait (atomically) until function has completed on other CPUs
178 * @cpu: the CPU where func should run
179 *
180 * Run a function on one processor.
181 *
182 * You must not call this function with disabled interrupts or from a
Heiko Carstens25864162007-03-05 23:35:41 +0100183 * hardware interrupt handler. You may call it from a bottom half.
Jan Glauber63db6e82007-02-21 10:55:06 +0100184 */
185int smp_call_function_on(void (*func) (void *info), void *info, int nonatomic,
186 int wait, int cpu)
187{
188 cpumask_t map = CPU_MASK_NONE;
189
Heiko Carstens25864162007-03-05 23:35:41 +0100190 preempt_disable();
Jan Glauber63db6e82007-02-21 10:55:06 +0100191 cpu_set(cpu, map);
192 __smp_call_function_map(func, info, nonatomic, wait, map);
Heiko Carstens25864162007-03-05 23:35:41 +0100193 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
195}
196EXPORT_SYMBOL(smp_call_function_on);
197
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100198static void do_send_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int cpu, rc;
201
202 /* stop all processors */
203 for_each_online_cpu(cpu) {
204 if (cpu == smp_processor_id())
205 continue;
206 do {
207 rc = signal_processor(cpu, sigp_stop);
208 } while (rc == sigp_busy);
209 }
210}
211
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100212static void do_store_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int cpu, rc;
215
216 /* store status of all processors in their lowcores (real 0) */
217 for_each_online_cpu(cpu) {
218 if (cpu == smp_processor_id())
219 continue;
220 do {
221 rc = signal_processor_p(
222 (__u32)(unsigned long) lowcore_ptr[cpu], cpu,
223 sigp_store_status_at_address);
224 } while(rc == sigp_busy);
225 }
226}
227
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100228static void do_wait_for_stop(void)
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100229{
230 int cpu;
231
232 /* Wait for all other cpus to enter stopped state */
233 for_each_online_cpu(cpu) {
234 if (cpu == smp_processor_id())
235 continue;
236 while(!smp_cpu_not_running(cpu))
237 cpu_relax();
238 }
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
242 * this function sends a 'stop' sigp to all other CPUs in the system.
243 * it goes straight through.
244 */
245void smp_send_stop(void)
246{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100247 /* Disable all interrupts/machine checks */
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100248 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* write magic number to zero page (absolute 0) */
251 lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
252
253 /* stop other processors. */
254 do_send_stop();
255
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100256 /* wait until other processors are stopped */
257 do_wait_for_stop();
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* store status of other processors. */
260 do_store_status();
261}
262
263/*
264 * Reboot, halt and power_off routines for SMP.
265 */
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267void machine_restart_smp(char * __unused)
268{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100269 smp_send_stop();
270 do_reipl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273void machine_halt_smp(void)
274{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100275 smp_send_stop();
276 if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
277 __cpcmd(vmhalt_cmd, NULL, 0, NULL);
278 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
279 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282void machine_power_off_smp(void)
283{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100284 smp_send_stop();
285 if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
286 __cpcmd(vmpoff_cmd, NULL, 0, NULL);
287 signal_processor(smp_processor_id(), sigp_stop_and_store_status);
288 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * This is the main routine where commands issued by other
293 * cpus are handled.
294 */
295
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100296static void do_ext_call_interrupt(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 unsigned long bits;
299
300 /*
301 * handle bit signal external calls
302 *
303 * For the ec_schedule signal we have to do nothing. All the work
304 * is done automatically when we return from the interrupt.
305 */
306 bits = xchg(&S390_lowcore.ext_call_fast, 0);
307
308 if (test_bit(ec_call_function, &bits))
309 do_call_function();
310}
311
312/*
313 * Send an external call sigp to another cpu and return without waiting
314 * for its completion.
315 */
316static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
317{
318 /*
319 * Set signaling bit in lowcore of target cpu and kick it
320 */
321 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700322 while(signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 udelay(10);
324}
325
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800326#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * this function sends a 'purge tlb' signal to another CPU.
329 */
330void smp_ptlb_callback(void *info)
331{
332 local_flush_tlb();
333}
334
335void smp_ptlb_all(void)
336{
337 on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
338}
339EXPORT_SYMBOL(smp_ptlb_all);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800340#endif /* ! CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/*
343 * this function sends a 'reschedule' IPI to another CPU.
344 * it goes straight through and wastes no time serializing
345 * anything. Worst case is that we lose a reschedule ...
346 */
347void smp_send_reschedule(int cpu)
348{
349 smp_ext_bitcall(cpu, ec_schedule);
350}
351
352/*
353 * parameter area for the set/clear control bit callbacks
354 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200355struct ec_creg_mask_parms {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned long orvals[16];
357 unsigned long andvals[16];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200358};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360/*
361 * callback for setting/clearing control bits
362 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100363static void smp_ctl_bit_callback(void *info) {
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200364 struct ec_creg_mask_parms *pp = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned long cregs[16];
366 int i;
367
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200368 __ctl_store(cregs, 0, 15);
369 for (i = 0; i <= 15; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200371 __ctl_load(cregs, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/*
375 * Set a bit in a control register of all cpus
376 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200377void smp_ctl_set_bit(int cr, int bit)
378{
379 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200381 memset(&parms.orvals, 0, sizeof(parms.orvals));
382 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 parms.orvals[cr] = 1 << bit;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200384 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387/*
388 * Clear a bit in a control register of all cpus
389 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200390void smp_ctl_clear_bit(int cr, int bit)
391{
392 struct ec_creg_mask_parms parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200394 memset(&parms.orvals, 0, sizeof(parms.orvals));
395 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 parms.andvals[cr] = ~(1L << bit);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200397 on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Michael Holzheu411ed322007-04-27 16:01:49 +0200400#if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_ZFCPDUMP_MODULE)
401
402/*
403 * zfcpdump_prefix_array holds prefix registers for the following scenario:
404 * 64 bit zfcpdump kernel and 31 bit kernel which is to be dumped. We have to
405 * save its prefix registers, since they get lost, when switching from 31 bit
406 * to 64 bit.
407 */
408unsigned int zfcpdump_prefix_array[NR_CPUS + 1] \
409 __attribute__((__section__(".data")));
410
411static void __init smp_get_save_areas(void)
412{
413 unsigned int cpu, cpu_num, rc;
414 __u16 boot_cpu_addr;
415
416 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
417 return;
418 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
419 cpu_num = 1;
420 for (cpu = 0; cpu <= 65535; cpu++) {
421 if ((u16) cpu == boot_cpu_addr)
422 continue;
423 __cpu_logical_map[1] = (__u16) cpu;
424 if (signal_processor(1, sigp_sense) == sigp_not_operational)
425 continue;
426 if (cpu_num >= NR_CPUS) {
427 printk("WARNING: Registers for cpu %i are not "
428 "saved, since dump kernel was compiled with"
429 "NR_CPUS=%i!\n", cpu_num, NR_CPUS);
430 continue;
431 }
432 zfcpdump_save_areas[cpu_num] =
433 alloc_bootmem(sizeof(union save_area));
434 while (1) {
435 rc = signal_processor(1, sigp_stop_and_store_status);
436 if (rc != sigp_busy)
437 break;
438 cpu_relax();
439 }
440 memcpy(zfcpdump_save_areas[cpu_num],
441 (void *)(unsigned long) store_prefix() +
442 SAVE_AREA_BASE, SAVE_AREA_SIZE);
443#ifdef __s390x__
444 /* copy original prefix register */
445 zfcpdump_save_areas[cpu_num]->s390x.pref_reg =
446 zfcpdump_prefix_array[cpu_num];
447#endif
448 cpu_num++;
449 }
450}
451
452union save_area *zfcpdump_save_areas[NR_CPUS + 1];
453EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
454
455#else
456#define smp_get_save_areas() do { } while (0)
457#endif
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
460 * Lets check how many CPUs we have.
461 */
462
Heiko Carstens255acee2006-02-17 13:52:46 -0800463static unsigned int
464__init smp_count_cpus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Heiko Carstens255acee2006-02-17 13:52:46 -0800466 unsigned int cpu, num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 __u16 boot_cpu_addr;
468
469 /*
470 * cpu 0 is the boot cpu. See smp_prepare_boot_cpu.
471 */
472
473 boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
474 current_thread_info()->cpu = 0;
475 num_cpus = 1;
Heiko Carstens255acee2006-02-17 13:52:46 -0800476 for (cpu = 0; cpu <= 65535; cpu++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if ((__u16) cpu == boot_cpu_addr)
478 continue;
Heiko Carstens255acee2006-02-17 13:52:46 -0800479 __cpu_logical_map[1] = (__u16) cpu;
480 if (signal_processor(1, sigp_sense) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sigp_not_operational)
482 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 num_cpus++;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 printk("Detected %d CPU's\n",(int) num_cpus);
487 printk("Boot cpu address %2X\n", boot_cpu_addr);
Heiko Carstens255acee2006-02-17 13:52:46 -0800488
489 return num_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * Activate a secondary processor.
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int __devinit start_secondary(void *cpuvoid)
496{
497 /* Setup the cpu */
498 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800499 preempt_disable();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100500 /* Enable TOD clock interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 init_cpu_timer();
502#ifdef CONFIG_VIRT_TIMER
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100503 /* Enable cpu timer interrupts on the secondary cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 init_cpu_vtimer();
505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Enable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100507 pfault_init();
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Mark this cpu as online */
510 cpu_set(smp_processor_id(), cpu_online_map);
511 /* Switch on interrupts */
512 local_irq_enable();
513 /* Print info about this processor */
514 print_cpu_info(&S390_lowcore.cpu_data);
515 /* cpu_idle will call schedule for us */
516 cpu_idle();
517 return 0;
518}
519
520static void __init smp_create_idle(unsigned int cpu)
521{
522 struct task_struct *p;
523
524 /*
525 * don't care about the psw and regs settings since we'll never
526 * reschedule the forked task.
527 */
528 p = fork_idle(cpu);
529 if (IS_ERR(p))
530 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
531 current_set[cpu] = p;
532}
533
534/* Reserving and releasing of CPUs */
535
536static DEFINE_SPINLOCK(smp_reserve_lock);
537static int smp_cpu_reserved[NR_CPUS];
538
539int
540smp_get_cpu(cpumask_t cpu_mask)
541{
542 unsigned long flags;
543 int cpu;
544
545 spin_lock_irqsave(&smp_reserve_lock, flags);
546 /* Try to find an already reserved cpu. */
547 for_each_cpu_mask(cpu, cpu_mask) {
548 if (smp_cpu_reserved[cpu] != 0) {
549 smp_cpu_reserved[cpu]++;
550 /* Found one. */
551 goto out;
552 }
553 }
554 /* Reserve a new cpu from cpu_mask. */
555 for_each_cpu_mask(cpu, cpu_mask) {
556 if (cpu_online(cpu)) {
557 smp_cpu_reserved[cpu]++;
558 goto out;
559 }
560 }
561 cpu = -ENODEV;
562out:
563 spin_unlock_irqrestore(&smp_reserve_lock, flags);
564 return cpu;
565}
566
567void
568smp_put_cpu(int cpu)
569{
570 unsigned long flags;
571
572 spin_lock_irqsave(&smp_reserve_lock, flags);
573 smp_cpu_reserved[cpu]--;
574 spin_unlock_irqrestore(&smp_reserve_lock, flags);
575}
576
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100577static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578cpu_stopped(int cpu)
579{
580 __u32 status;
581
582 /* Check for stopped state */
583 if (signal_processor_ps(&status, 0, cpu, sigp_sense) == sigp_status_stored) {
584 if (status & 0x40)
585 return 1;
586 }
587 return 0;
588}
589
590/* Upping and downing of CPUs */
591
592int
593__cpu_up(unsigned int cpu)
594{
595 struct task_struct *idle;
596 struct _lowcore *cpu_lowcore;
597 struct stack_frame *sf;
598 sigp_ccode ccode;
599 int curr_cpu;
600
601 for (curr_cpu = 0; curr_cpu <= 65535; curr_cpu++) {
602 __cpu_logical_map[cpu] = (__u16) curr_cpu;
603 if (cpu_stopped(cpu))
604 break;
605 }
606
607 if (!cpu_stopped(cpu))
608 return -ENODEV;
609
610 ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
611 cpu, sigp_set_prefix);
612 if (ccode){
613 printk("sigp_set_prefix failed for cpu %d "
614 "with condition code %d\n",
615 (int) cpu, (int) ccode);
616 return -EIO;
617 }
618
619 idle = current_set[cpu];
620 cpu_lowcore = lowcore_ptr[cpu];
621 cpu_lowcore->kernel_stack = (unsigned long)
Al Viro30af7122006-01-12 01:05:50 -0800622 task_stack_page(idle) + (THREAD_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
624 - sizeof(struct pt_regs)
625 - sizeof(struct stack_frame));
626 memset(sf, 0, sizeof(struct stack_frame));
627 sf->gprs[9] = (unsigned long) sf;
628 cpu_lowcore->save_area[15] = (unsigned long) sf;
629 __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200630 asm volatile(
631 " stam 0,15,0(%0)"
632 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
634 cpu_lowcore->current_task = (unsigned long) idle;
635 cpu_lowcore->cpu_data.cpu_nr = cpu;
636 eieio();
Michael Ryan699ff132006-03-24 03:15:17 -0800637
638 while (signal_processor(cpu,sigp_restart) == sigp_busy)
639 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 while (!cpu_online(cpu))
642 cpu_relax();
643 return 0;
644}
645
Heiko Carstens255acee2006-02-17 13:52:46 -0800646static unsigned int __initdata additional_cpus;
Heiko Carstens37a33022006-02-17 13:52:47 -0800647static unsigned int __initdata possible_cpus;
Heiko Carstens255acee2006-02-17 13:52:46 -0800648
649void __init smp_setup_cpu_possible_map(void)
650{
Heiko Carstens54330452006-02-17 13:52:48 -0800651 unsigned int phy_cpus, pos_cpus, cpu;
Heiko Carstens255acee2006-02-17 13:52:46 -0800652
Michael Holzheu411ed322007-04-27 16:01:49 +0200653 smp_get_save_areas();
Heiko Carstens54330452006-02-17 13:52:48 -0800654 phy_cpus = smp_count_cpus();
655 pos_cpus = min(phy_cpus + additional_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800656
Heiko Carstens37a33022006-02-17 13:52:47 -0800657 if (possible_cpus)
Heiko Carstens54330452006-02-17 13:52:48 -0800658 pos_cpus = min(possible_cpus, (unsigned int) NR_CPUS);
Heiko Carstens255acee2006-02-17 13:52:46 -0800659
Heiko Carstens54330452006-02-17 13:52:48 -0800660 for (cpu = 0; cpu < pos_cpus; cpu++)
Heiko Carstens255acee2006-02-17 13:52:46 -0800661 cpu_set(cpu, cpu_possible_map);
662
Heiko Carstens54330452006-02-17 13:52:48 -0800663 phy_cpus = min(phy_cpus, pos_cpus);
664
665 for (cpu = 0; cpu < phy_cpus; cpu++)
666 cpu_set(cpu, cpu_present_map);
Heiko Carstens255acee2006-02-17 13:52:46 -0800667}
668
669#ifdef CONFIG_HOTPLUG_CPU
670
671static int __init setup_additional_cpus(char *s)
672{
673 additional_cpus = simple_strtoul(s, NULL, 0);
674 return 0;
675}
676early_param("additional_cpus", setup_additional_cpus);
677
Heiko Carstens37a33022006-02-17 13:52:47 -0800678static int __init setup_possible_cpus(char *s)
679{
680 possible_cpus = simple_strtoul(s, NULL, 0);
681 return 0;
682}
683early_param("possible_cpus", setup_possible_cpus);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685int
686__cpu_disable(void)
687{
688 unsigned long flags;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200689 struct ec_creg_mask_parms cr_parms;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700690 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 spin_lock_irqsave(&smp_reserve_lock, flags);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700693 if (smp_cpu_reserved[cpu] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_unlock_irqrestore(&smp_reserve_lock, flags);
695 return -EBUSY;
696 }
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700697 cpu_clear(cpu, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* Disable pfault pseudo page faults on this cpu. */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100700 pfault_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200702 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
703 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200705 /* disable all external interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 cr_parms.orvals[0] = 0;
707 cr_parms.andvals[0] = ~(1<<15 | 1<<14 | 1<<13 | 1<<12 |
708 1<<11 | 1<<10 | 1<< 6 | 1<< 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* disable all I/O interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 cr_parms.orvals[6] = 0;
711 cr_parms.andvals[6] = ~(1<<31 | 1<<30 | 1<<29 | 1<<28 |
712 1<<27 | 1<<26 | 1<<25 | 1<<24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* disable most machine checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 cr_parms.orvals[14] = 0;
715 cr_parms.andvals[14] = ~(1<<28 | 1<<27 | 1<<26 | 1<<25 | 1<<24);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 smp_ctl_bit_callback(&cr_parms);
718
719 spin_unlock_irqrestore(&smp_reserve_lock, flags);
720 return 0;
721}
722
723void
724__cpu_die(unsigned int cpu)
725{
726 /* Wait until target cpu is down */
727 while (!smp_cpu_not_running(cpu))
728 cpu_relax();
729 printk("Processor %d spun down\n", cpu);
730}
731
732void
733cpu_die(void)
734{
735 idle_task_exit();
736 signal_processor(smp_processor_id(), sigp_stop);
737 BUG();
738 for(;;);
739}
740
Heiko Carstens255acee2006-02-17 13:52:46 -0800741#endif /* CONFIG_HOTPLUG_CPU */
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/*
744 * Cycle through the processors and setup structures.
745 */
746
747void __init smp_prepare_cpus(unsigned int max_cpus)
748{
749 unsigned long stack;
750 unsigned int cpu;
751 int i;
752
Heiko Carstens99b2d8d2005-07-27 11:45:00 -0700753 /* request the 0x1201 emergency signal external interrupt */
754 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
755 panic("Couldn't request external interrupt 0x1201");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 memset(lowcore_ptr,0,sizeof(lowcore_ptr));
757 /*
758 * Initialize prefix pages and stacks for all possible cpus
759 */
760 print_cpu_info(&S390_lowcore.cpu_data);
761
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800762 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 lowcore_ptr[i] = (struct _lowcore *)
764 __get_free_pages(GFP_KERNEL|GFP_DMA,
765 sizeof(void*) == 8 ? 1 : 0);
766 stack = __get_free_pages(GFP_KERNEL,ASYNC_ORDER);
767 if (lowcore_ptr[i] == NULL || stack == 0ULL)
768 panic("smp_boot_cpus failed to allocate memory\n");
769
770 *(lowcore_ptr[i]) = S390_lowcore;
771 lowcore_ptr[i]->async_stack = stack + (ASYNC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 stack = __get_free_pages(GFP_KERNEL,0);
773 if (stack == 0ULL)
774 panic("smp_boot_cpus failed to allocate memory\n");
775 lowcore_ptr[i]->panic_stack = stack + (PAGE_SIZE);
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800776#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700777 if (MACHINE_HAS_IEEE) {
778 lowcore_ptr[i]->extended_save_area_addr =
779 (__u32) __get_free_pages(GFP_KERNEL,0);
780 if (lowcore_ptr[i]->extended_save_area_addr == 0)
781 panic("smp_boot_cpus failed to "
782 "allocate memory\n");
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#endif
785 }
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800786#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700787 if (MACHINE_HAS_IEEE)
788 ctl_set_bit(14, 29); /* enable extended save area */
789#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
791
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800792 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (cpu != smp_processor_id())
794 smp_create_idle(cpu);
795}
796
797void __devinit smp_prepare_boot_cpu(void)
798{
799 BUG_ON(smp_processor_id() != 0);
800
801 cpu_set(0, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 S390_lowcore.percpu_offset = __per_cpu_offset[0];
803 current_set[0] = current;
804}
805
806void smp_cpus_done(unsigned int max_cpus)
807{
Heiko Carstens54330452006-02-17 13:52:48 -0800808 cpu_present_map = cpu_possible_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * the frequency of the profiling timer can be changed
813 * by writing a multiplier value into /proc/profile.
814 *
815 * usually you want to run this on all CPUs ;)
816 */
817int setup_profiling_timer(unsigned int multiplier)
818{
819 return 0;
820}
821
822static DEFINE_PER_CPU(struct cpu, cpu_devices);
823
824static int __init topology_init(void)
825{
826 int cpu;
827 int ret;
828
KAMEZAWA Hiroyuki97db7fb2006-03-31 02:30:26 -0800829 for_each_possible_cpu(cpu) {
Heiko Carstens6721f772007-01-09 10:18:44 +0100830 struct cpu *c = &per_cpu(cpu_devices, cpu);
831
832 c->hotpluggable = 1;
833 ret = register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (ret)
835 printk(KERN_WARNING "topology_init: register_cpu %d "
836 "failed (%d)\n", cpu, ret);
837 }
838 return 0;
839}
840
841subsys_initcall(topology_init);
842
Heiko Carstens255acee2006-02-17 13:52:46 -0800843EXPORT_SYMBOL(cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844EXPORT_SYMBOL(cpu_possible_map);
845EXPORT_SYMBOL(lowcore_ptr);
846EXPORT_SYMBOL(smp_ctl_set_bit);
847EXPORT_SYMBOL(smp_ctl_clear_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848EXPORT_SYMBOL(smp_get_cpu);
849EXPORT_SYMBOL(smp_put_cpu);