Merge branch 'akpm'
[linux-2.6/next.git] / arch / s390 / kernel / smp.c
blobe4572601e91e0cf2d7f1d22103406b5761845399
1 /*
2 * arch/s390/kernel/smp.c
4 * Copyright IBM Corp. 1999, 2009
5 * 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)
9 * based on other smp stuff by
10 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
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.
23 #define KMSG_COMPONENT "cpu"
24 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
26 #include <linux/workqueue.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/mm.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/delay.h>
34 #include <linux/cache.h>
35 #include <linux/interrupt.h>
36 #include <linux/irqflags.h>
37 #include <linux/cpu.h>
38 #include <linux/timex.h>
39 #include <linux/bootmem.h>
40 #include <linux/slab.h>
41 #include <asm/asm-offsets.h>
42 #include <asm/ipl.h>
43 #include <asm/setup.h>
44 #include <asm/sigp.h>
45 #include <asm/pgalloc.h>
46 #include <asm/irq.h>
47 #include <asm/cpcmd.h>
48 #include <asm/tlbflush.h>
49 #include <asm/timer.h>
50 #include <asm/lowcore.h>
51 #include <asm/sclp.h>
52 #include <asm/cputime.h>
53 #include <asm/vdso.h>
54 #include <asm/cpu.h>
55 #include "entry.h"
57 /* logical cpu to cpu address */
58 unsigned short __cpu_logical_map[NR_CPUS];
60 static struct task_struct *current_set[NR_CPUS];
62 static u8 smp_cpu_type;
63 static int smp_use_sigp_detection;
65 enum s390_cpu_state {
66 CPU_STATE_STANDBY,
67 CPU_STATE_CONFIGURED,
70 DEFINE_MUTEX(smp_cpu_state_mutex);
71 int smp_cpu_polarization[NR_CPUS];
72 static int smp_cpu_state[NR_CPUS];
73 static int cpu_management;
75 static DEFINE_PER_CPU(struct cpu, cpu_devices);
77 static void smp_ext_bitcall(int, int);
79 static int raw_cpu_stopped(int cpu)
81 u32 status;
83 switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
84 case sigp_status_stored:
85 /* Check for stopped and check stop state */
86 if (status & 0x50)
87 return 1;
88 break;
89 default:
90 break;
92 return 0;
95 static inline int cpu_stopped(int cpu)
97 return raw_cpu_stopped(cpu_logical_map(cpu));
101 * Ensure that PSW restart is done on an online CPU
103 void smp_restart_with_online_cpu(void)
105 int cpu;
107 for_each_online_cpu(cpu) {
108 if (stap() == __cpu_logical_map[cpu]) {
109 /* We are online: Enable DAT again and return */
110 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
111 return;
114 /* We are not online: Do PSW restart on an online CPU */
115 while (sigp(cpu, sigp_restart) == sigp_busy)
116 cpu_relax();
117 /* And stop ourself */
118 while (raw_sigp(stap(), sigp_stop) == sigp_busy)
119 cpu_relax();
120 for (;;);
123 void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
125 struct _lowcore *lc, *current_lc;
126 struct stack_frame *sf;
127 struct pt_regs *regs;
128 unsigned long sp;
130 if (smp_processor_id() == 0)
131 func(data);
132 __load_psw_mask(PSW_BASE_BITS | PSW_DEFAULT_KEY);
133 /* Disable lowcore protection */
134 __ctl_clear_bit(0, 28);
135 current_lc = lowcore_ptr[smp_processor_id()];
136 lc = lowcore_ptr[0];
137 if (!lc)
138 lc = current_lc;
139 lc->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
140 lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
141 if (!cpu_online(0))
142 smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
143 while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
144 cpu_relax();
145 sp = lc->panic_stack;
146 sp -= sizeof(struct pt_regs);
147 regs = (struct pt_regs *) sp;
148 memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
149 regs->psw = lc->psw_save_area;
150 sp -= STACK_FRAME_OVERHEAD;
151 sf = (struct stack_frame *) sp;
152 sf->back_chain = regs->gprs[15];
153 smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
156 void smp_send_stop(void)
158 int cpu, rc;
160 /* Disable all interrupts/machine checks */
161 __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
162 trace_hardirqs_off();
164 /* stop all processors */
165 for_each_online_cpu(cpu) {
166 if (cpu == smp_processor_id())
167 continue;
168 do {
169 rc = sigp(cpu, sigp_stop);
170 } while (rc == sigp_busy);
172 while (!cpu_stopped(cpu))
173 cpu_relax();
178 * This is the main routine where commands issued by other
179 * cpus are handled.
182 static void do_ext_call_interrupt(unsigned int ext_int_code,
183 unsigned int param32, unsigned long param64)
185 unsigned long bits;
187 kstat_cpu(smp_processor_id()).irqs[EXTINT_IPI]++;
189 * handle bit signal external calls
191 bits = xchg(&S390_lowcore.ext_call_fast, 0);
193 if (test_bit(ec_schedule, &bits))
194 scheduler_ipi();
196 if (test_bit(ec_call_function, &bits))
197 generic_smp_call_function_interrupt();
199 if (test_bit(ec_call_function_single, &bits))
200 generic_smp_call_function_single_interrupt();
204 * Send an external call sigp to another cpu and return without waiting
205 * for its completion.
207 static void smp_ext_bitcall(int cpu, int sig)
210 * Set signaling bit in lowcore of target cpu and kick it
212 set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
213 while (sigp(cpu, sigp_emergency_signal) == sigp_busy)
214 udelay(10);
217 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
219 int cpu;
221 for_each_cpu(cpu, mask)
222 smp_ext_bitcall(cpu, ec_call_function);
225 void arch_send_call_function_single_ipi(int cpu)
227 smp_ext_bitcall(cpu, ec_call_function_single);
230 #ifndef CONFIG_64BIT
232 * this function sends a 'purge tlb' signal to another CPU.
234 static void smp_ptlb_callback(void *info)
236 __tlb_flush_local();
239 void smp_ptlb_all(void)
241 on_each_cpu(smp_ptlb_callback, NULL, 1);
243 EXPORT_SYMBOL(smp_ptlb_all);
244 #endif /* ! CONFIG_64BIT */
247 * this function sends a 'reschedule' IPI to another CPU.
248 * it goes straight through and wastes no time serializing
249 * anything. Worst case is that we lose a reschedule ...
251 void smp_send_reschedule(int cpu)
253 smp_ext_bitcall(cpu, ec_schedule);
257 * parameter area for the set/clear control bit callbacks
259 struct ec_creg_mask_parms {
260 unsigned long orvals[16];
261 unsigned long andvals[16];
265 * callback for setting/clearing control bits
267 static void smp_ctl_bit_callback(void *info)
269 struct ec_creg_mask_parms *pp = info;
270 unsigned long cregs[16];
271 int i;
273 __ctl_store(cregs, 0, 15);
274 for (i = 0; i <= 15; i++)
275 cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
276 __ctl_load(cregs, 0, 15);
280 * Set a bit in a control register of all cpus
282 void smp_ctl_set_bit(int cr, int bit)
284 struct ec_creg_mask_parms parms;
286 memset(&parms.orvals, 0, sizeof(parms.orvals));
287 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
288 parms.orvals[cr] = 1UL << bit;
289 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
291 EXPORT_SYMBOL(smp_ctl_set_bit);
294 * Clear a bit in a control register of all cpus
296 void smp_ctl_clear_bit(int cr, int bit)
298 struct ec_creg_mask_parms parms;
300 memset(&parms.orvals, 0, sizeof(parms.orvals));
301 memset(&parms.andvals, 0xff, sizeof(parms.andvals));
302 parms.andvals[cr] = ~(1UL << bit);
303 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
305 EXPORT_SYMBOL(smp_ctl_clear_bit);
307 #ifdef CONFIG_ZFCPDUMP
309 static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
311 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
312 return;
313 if (cpu >= NR_CPUS) {
314 pr_warning("CPU %i exceeds the maximum %i and is excluded from "
315 "the dump\n", cpu, NR_CPUS - 1);
316 return;
318 zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
319 while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
320 cpu_relax();
321 memcpy_real(zfcpdump_save_areas[cpu],
322 (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
323 sizeof(struct save_area));
326 struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
327 EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
329 #else
331 static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
333 #endif /* CONFIG_ZFCPDUMP */
335 static int cpu_known(int cpu_id)
337 int cpu;
339 for_each_present_cpu(cpu) {
340 if (__cpu_logical_map[cpu] == cpu_id)
341 return 1;
343 return 0;
346 static int smp_rescan_cpus_sigp(cpumask_t avail)
348 int cpu_id, logical_cpu;
350 logical_cpu = cpumask_first(&avail);
351 if (logical_cpu >= nr_cpu_ids)
352 return 0;
353 for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
354 if (cpu_known(cpu_id))
355 continue;
356 __cpu_logical_map[logical_cpu] = cpu_id;
357 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
358 if (!cpu_stopped(logical_cpu))
359 continue;
360 set_cpu_present(logical_cpu, true);
361 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
362 logical_cpu = cpumask_next(logical_cpu, &avail);
363 if (logical_cpu >= nr_cpu_ids)
364 break;
366 return 0;
369 static int smp_rescan_cpus_sclp(cpumask_t avail)
371 struct sclp_cpu_info *info;
372 int cpu_id, logical_cpu, cpu;
373 int rc;
375 logical_cpu = cpumask_first(&avail);
376 if (logical_cpu >= nr_cpu_ids)
377 return 0;
378 info = kmalloc(sizeof(*info), GFP_KERNEL);
379 if (!info)
380 return -ENOMEM;
381 rc = sclp_get_cpu_info(info);
382 if (rc)
383 goto out;
384 for (cpu = 0; cpu < info->combined; cpu++) {
385 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
386 continue;
387 cpu_id = info->cpu[cpu].address;
388 if (cpu_known(cpu_id))
389 continue;
390 __cpu_logical_map[logical_cpu] = cpu_id;
391 smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
392 set_cpu_present(logical_cpu, true);
393 if (cpu >= info->configured)
394 smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
395 else
396 smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
397 logical_cpu = cpumask_next(logical_cpu, &avail);
398 if (logical_cpu >= nr_cpu_ids)
399 break;
401 out:
402 kfree(info);
403 return rc;
406 static int __smp_rescan_cpus(void)
408 cpumask_t avail;
410 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
411 if (smp_use_sigp_detection)
412 return smp_rescan_cpus_sigp(avail);
413 else
414 return smp_rescan_cpus_sclp(avail);
417 static void __init smp_detect_cpus(void)
419 unsigned int cpu, c_cpus, s_cpus;
420 struct sclp_cpu_info *info;
421 u16 boot_cpu_addr, cpu_addr;
423 c_cpus = 1;
424 s_cpus = 0;
425 boot_cpu_addr = __cpu_logical_map[0];
426 info = kmalloc(sizeof(*info), GFP_KERNEL);
427 if (!info)
428 panic("smp_detect_cpus failed to allocate memory\n");
429 /* Use sigp detection algorithm if sclp doesn't work. */
430 if (sclp_get_cpu_info(info)) {
431 smp_use_sigp_detection = 1;
432 for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
433 if (cpu == boot_cpu_addr)
434 continue;
435 if (!raw_cpu_stopped(cpu))
436 continue;
437 smp_get_save_area(c_cpus, cpu);
438 c_cpus++;
440 goto out;
443 if (info->has_cpu_type) {
444 for (cpu = 0; cpu < info->combined; cpu++) {
445 if (info->cpu[cpu].address == boot_cpu_addr) {
446 smp_cpu_type = info->cpu[cpu].type;
447 break;
452 for (cpu = 0; cpu < info->combined; cpu++) {
453 if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
454 continue;
455 cpu_addr = info->cpu[cpu].address;
456 if (cpu_addr == boot_cpu_addr)
457 continue;
458 if (!raw_cpu_stopped(cpu_addr)) {
459 s_cpus++;
460 continue;
462 smp_get_save_area(c_cpus, cpu_addr);
463 c_cpus++;
465 out:
466 kfree(info);
467 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
468 get_online_cpus();
469 __smp_rescan_cpus();
470 put_online_cpus();
474 * Activate a secondary processor.
476 int __cpuinit start_secondary(void *cpuvoid)
478 cpu_init();
479 preempt_disable();
480 init_cpu_timer();
481 init_cpu_vtimer();
482 pfault_init();
484 notify_cpu_starting(smp_processor_id());
485 ipi_call_lock();
486 set_cpu_online(smp_processor_id(), true);
487 ipi_call_unlock();
488 __ctl_clear_bit(0, 28); /* Disable lowcore protection */
489 S390_lowcore.restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
490 S390_lowcore.restart_psw.addr =
491 PSW_ADDR_AMODE | (unsigned long) psw_restart_int_handler;
492 __ctl_set_bit(0, 28); /* Enable lowcore protection */
494 * Wait until the cpu which brought this one up marked it
495 * active before enabling interrupts.
497 while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
498 cpu_relax();
499 local_irq_enable();
500 /* cpu_idle will call schedule for us */
501 cpu_idle();
502 return 0;
505 struct create_idle {
506 struct work_struct work;
507 struct task_struct *idle;
508 struct completion done;
509 int cpu;
512 static void __cpuinit smp_fork_idle(struct work_struct *work)
514 struct create_idle *c_idle;
516 c_idle = container_of(work, struct create_idle, work);
517 c_idle->idle = fork_idle(c_idle->cpu);
518 complete(&c_idle->done);
521 static int __cpuinit smp_alloc_lowcore(int cpu)
523 unsigned long async_stack, panic_stack;
524 struct _lowcore *lowcore;
526 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
527 if (!lowcore)
528 return -ENOMEM;
529 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
530 panic_stack = __get_free_page(GFP_KERNEL);
531 if (!panic_stack || !async_stack)
532 goto out;
533 memcpy(lowcore, &S390_lowcore, 512);
534 memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
535 lowcore->async_stack = async_stack + ASYNC_SIZE;
536 lowcore->panic_stack = panic_stack + PAGE_SIZE;
537 lowcore->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
538 lowcore->restart_psw.addr =
539 PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
540 if (user_mode != HOME_SPACE_MODE)
541 lowcore->restart_psw.mask |= PSW_ASC_HOME;
542 #ifndef CONFIG_64BIT
543 if (MACHINE_HAS_IEEE) {
544 unsigned long save_area;
546 save_area = get_zeroed_page(GFP_KERNEL);
547 if (!save_area)
548 goto out;
549 lowcore->extended_save_area_addr = (u32) save_area;
551 #else
552 if (vdso_alloc_per_cpu(cpu, lowcore))
553 goto out;
554 #endif
555 lowcore_ptr[cpu] = lowcore;
556 return 0;
558 out:
559 free_page(panic_stack);
560 free_pages(async_stack, ASYNC_ORDER);
561 free_pages((unsigned long) lowcore, LC_ORDER);
562 return -ENOMEM;
565 static void smp_free_lowcore(int cpu)
567 struct _lowcore *lowcore;
569 lowcore = lowcore_ptr[cpu];
570 #ifndef CONFIG_64BIT
571 if (MACHINE_HAS_IEEE)
572 free_page((unsigned long) lowcore->extended_save_area_addr);
573 #else
574 vdso_free_per_cpu(cpu, lowcore);
575 #endif
576 free_page(lowcore->panic_stack - PAGE_SIZE);
577 free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
578 free_pages((unsigned long) lowcore, LC_ORDER);
579 lowcore_ptr[cpu] = NULL;
582 /* Upping and downing of CPUs */
583 int __cpuinit __cpu_up(unsigned int cpu)
585 struct _lowcore *cpu_lowcore;
586 struct create_idle c_idle;
587 struct task_struct *idle;
588 struct stack_frame *sf;
589 u32 lowcore;
590 int ccode;
592 if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
593 return -EIO;
594 idle = current_set[cpu];
595 if (!idle) {
596 c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
597 INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
598 c_idle.cpu = cpu;
599 schedule_work(&c_idle.work);
600 wait_for_completion(&c_idle.done);
601 if (IS_ERR(c_idle.idle))
602 return PTR_ERR(c_idle.idle);
603 idle = c_idle.idle;
604 current_set[cpu] = c_idle.idle;
606 init_idle(idle, cpu);
607 if (smp_alloc_lowcore(cpu))
608 return -ENOMEM;
609 do {
610 ccode = sigp(cpu, sigp_initial_cpu_reset);
611 if (ccode == sigp_busy)
612 udelay(10);
613 if (ccode == sigp_not_operational)
614 goto err_out;
615 } while (ccode == sigp_busy);
617 lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
618 while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
619 udelay(10);
621 cpu_lowcore = lowcore_ptr[cpu];
622 cpu_lowcore->kernel_stack = (unsigned long)
623 task_stack_page(idle) + THREAD_SIZE;
624 cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
625 sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
626 - sizeof(struct pt_regs)
627 - sizeof(struct stack_frame));
628 memset(sf, 0, sizeof(struct stack_frame));
629 sf->gprs[9] = (unsigned long) sf;
630 cpu_lowcore->save_area[15] = (unsigned long) sf;
631 __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
632 atomic_inc(&init_mm.context.attach_count);
633 asm volatile(
634 " stam 0,15,0(%0)"
635 : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
636 cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
637 cpu_lowcore->current_task = (unsigned long) idle;
638 cpu_lowcore->cpu_nr = cpu;
639 cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
640 cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
641 cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
642 memcpy(cpu_lowcore->stfle_fac_list, S390_lowcore.stfle_fac_list,
643 MAX_FACILITY_BIT/8);
644 eieio();
646 while (sigp(cpu, sigp_restart) == sigp_busy)
647 udelay(10);
649 while (!cpu_online(cpu))
650 cpu_relax();
651 return 0;
653 err_out:
654 smp_free_lowcore(cpu);
655 return -EIO;
658 static int __init setup_possible_cpus(char *s)
660 int pcpus, cpu;
662 pcpus = simple_strtoul(s, NULL, 0);
663 init_cpu_possible(cpumask_of(0));
664 for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
665 set_cpu_possible(cpu, true);
666 return 0;
668 early_param("possible_cpus", setup_possible_cpus);
670 #ifdef CONFIG_HOTPLUG_CPU
672 int __cpu_disable(void)
674 struct ec_creg_mask_parms cr_parms;
675 int cpu = smp_processor_id();
677 set_cpu_online(cpu, false);
679 /* Disable pfault pseudo page faults on this cpu. */
680 pfault_fini();
682 memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
683 memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
685 /* disable all external interrupts */
686 cr_parms.orvals[0] = 0;
687 cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 11 |
688 1 << 10 | 1 << 9 | 1 << 6 | 1 << 5 |
689 1 << 4);
690 /* disable all I/O interrupts */
691 cr_parms.orvals[6] = 0;
692 cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
693 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
694 /* disable most machine checks */
695 cr_parms.orvals[14] = 0;
696 cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
697 1 << 25 | 1 << 24);
699 smp_ctl_bit_callback(&cr_parms);
701 return 0;
704 void __cpu_die(unsigned int cpu)
706 /* Wait until target cpu is down */
707 while (!cpu_stopped(cpu))
708 cpu_relax();
709 while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
710 udelay(10);
711 smp_free_lowcore(cpu);
712 atomic_dec(&init_mm.context.attach_count);
715 void __noreturn cpu_die(void)
717 idle_task_exit();
718 while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
719 cpu_relax();
720 for (;;);
723 #endif /* CONFIG_HOTPLUG_CPU */
725 void __init smp_prepare_cpus(unsigned int max_cpus)
727 #ifndef CONFIG_64BIT
728 unsigned long save_area = 0;
729 #endif
730 unsigned long async_stack, panic_stack;
731 struct _lowcore *lowcore;
733 smp_detect_cpus();
735 /* request the 0x1201 emergency signal external interrupt */
736 if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
737 panic("Couldn't request external interrupt 0x1201");
739 /* Reallocate current lowcore, but keep its contents. */
740 lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
741 panic_stack = __get_free_page(GFP_KERNEL);
742 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
743 BUG_ON(!lowcore || !panic_stack || !async_stack);
744 #ifndef CONFIG_64BIT
745 if (MACHINE_HAS_IEEE)
746 save_area = get_zeroed_page(GFP_KERNEL);
747 #endif
748 local_irq_disable();
749 local_mcck_disable();
750 lowcore_ptr[smp_processor_id()] = lowcore;
751 *lowcore = S390_lowcore;
752 lowcore->panic_stack = panic_stack + PAGE_SIZE;
753 lowcore->async_stack = async_stack + ASYNC_SIZE;
754 #ifndef CONFIG_64BIT
755 if (MACHINE_HAS_IEEE)
756 lowcore->extended_save_area_addr = (u32) save_area;
757 #endif
758 set_prefix((u32)(unsigned long) lowcore);
759 local_mcck_enable();
760 local_irq_enable();
761 #ifdef CONFIG_64BIT
762 if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
763 BUG();
764 #endif
767 void __init smp_prepare_boot_cpu(void)
769 BUG_ON(smp_processor_id() != 0);
771 current_thread_info()->cpu = 0;
772 set_cpu_present(0, true);
773 set_cpu_online(0, true);
774 S390_lowcore.percpu_offset = __per_cpu_offset[0];
775 current_set[0] = current;
776 smp_cpu_state[0] = CPU_STATE_CONFIGURED;
777 smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
780 void __init smp_cpus_done(unsigned int max_cpus)
784 void __init smp_setup_processor_id(void)
786 S390_lowcore.cpu_nr = 0;
787 __cpu_logical_map[0] = stap();
791 * the frequency of the profiling timer can be changed
792 * by writing a multiplier value into /proc/profile.
794 * usually you want to run this on all CPUs ;)
796 int setup_profiling_timer(unsigned int multiplier)
798 return 0;
801 #ifdef CONFIG_HOTPLUG_CPU
802 static ssize_t cpu_configure_show(struct sys_device *dev,
803 struct sysdev_attribute *attr, char *buf)
805 ssize_t count;
807 mutex_lock(&smp_cpu_state_mutex);
808 count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
809 mutex_unlock(&smp_cpu_state_mutex);
810 return count;
813 static ssize_t cpu_configure_store(struct sys_device *dev,
814 struct sysdev_attribute *attr,
815 const char *buf, size_t count)
817 int cpu = dev->id;
818 int val, rc;
819 char delim;
821 if (sscanf(buf, "%d %c", &val, &delim) != 1)
822 return -EINVAL;
823 if (val != 0 && val != 1)
824 return -EINVAL;
826 get_online_cpus();
827 mutex_lock(&smp_cpu_state_mutex);
828 rc = -EBUSY;
829 /* disallow configuration changes of online cpus and cpu 0 */
830 if (cpu_online(cpu) || cpu == 0)
831 goto out;
832 rc = 0;
833 switch (val) {
834 case 0:
835 if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
836 rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
837 if (!rc) {
838 smp_cpu_state[cpu] = CPU_STATE_STANDBY;
839 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
842 break;
843 case 1:
844 if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
845 rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
846 if (!rc) {
847 smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
848 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
851 break;
852 default:
853 break;
855 out:
856 mutex_unlock(&smp_cpu_state_mutex);
857 put_online_cpus();
858 return rc ? rc : count;
860 static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
861 #endif /* CONFIG_HOTPLUG_CPU */
863 static ssize_t cpu_polarization_show(struct sys_device *dev,
864 struct sysdev_attribute *attr, char *buf)
866 int cpu = dev->id;
867 ssize_t count;
869 mutex_lock(&smp_cpu_state_mutex);
870 switch (smp_cpu_polarization[cpu]) {
871 case POLARIZATION_HRZ:
872 count = sprintf(buf, "horizontal\n");
873 break;
874 case POLARIZATION_VL:
875 count = sprintf(buf, "vertical:low\n");
876 break;
877 case POLARIZATION_VM:
878 count = sprintf(buf, "vertical:medium\n");
879 break;
880 case POLARIZATION_VH:
881 count = sprintf(buf, "vertical:high\n");
882 break;
883 default:
884 count = sprintf(buf, "unknown\n");
885 break;
887 mutex_unlock(&smp_cpu_state_mutex);
888 return count;
890 static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
892 static ssize_t show_cpu_address(struct sys_device *dev,
893 struct sysdev_attribute *attr, char *buf)
895 return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
897 static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
900 static struct attribute *cpu_common_attrs[] = {
901 #ifdef CONFIG_HOTPLUG_CPU
902 &attr_configure.attr,
903 #endif
904 &attr_address.attr,
905 &attr_polarization.attr,
906 NULL,
909 static struct attribute_group cpu_common_attr_group = {
910 .attrs = cpu_common_attrs,
913 static ssize_t show_capability(struct sys_device *dev,
914 struct sysdev_attribute *attr, char *buf)
916 unsigned int capability;
917 int rc;
919 rc = get_cpu_capability(&capability);
920 if (rc)
921 return rc;
922 return sprintf(buf, "%u\n", capability);
924 static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
926 static ssize_t show_idle_count(struct sys_device *dev,
927 struct sysdev_attribute *attr, char *buf)
929 struct s390_idle_data *idle;
930 unsigned long long idle_count;
931 unsigned int sequence;
933 idle = &per_cpu(s390_idle, dev->id);
934 repeat:
935 sequence = idle->sequence;
936 smp_rmb();
937 if (sequence & 1)
938 goto repeat;
939 idle_count = idle->idle_count;
940 if (idle->idle_enter)
941 idle_count++;
942 smp_rmb();
943 if (idle->sequence != sequence)
944 goto repeat;
945 return sprintf(buf, "%llu\n", idle_count);
947 static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
949 static ssize_t show_idle_time(struct sys_device *dev,
950 struct sysdev_attribute *attr, char *buf)
952 struct s390_idle_data *idle;
953 unsigned long long now, idle_time, idle_enter;
954 unsigned int sequence;
956 idle = &per_cpu(s390_idle, dev->id);
957 now = get_clock();
958 repeat:
959 sequence = idle->sequence;
960 smp_rmb();
961 if (sequence & 1)
962 goto repeat;
963 idle_time = idle->idle_time;
964 idle_enter = idle->idle_enter;
965 if (idle_enter != 0ULL && idle_enter < now)
966 idle_time += now - idle_enter;
967 smp_rmb();
968 if (idle->sequence != sequence)
969 goto repeat;
970 return sprintf(buf, "%llu\n", idle_time >> 12);
972 static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
974 static struct attribute *cpu_online_attrs[] = {
975 &attr_capability.attr,
976 &attr_idle_count.attr,
977 &attr_idle_time_us.attr,
978 NULL,
981 static struct attribute_group cpu_online_attr_group = {
982 .attrs = cpu_online_attrs,
985 static int __cpuinit smp_cpu_notify(struct notifier_block *self,
986 unsigned long action, void *hcpu)
988 unsigned int cpu = (unsigned int)(long)hcpu;
989 struct cpu *c = &per_cpu(cpu_devices, cpu);
990 struct sys_device *s = &c->sysdev;
991 struct s390_idle_data *idle;
992 int err = 0;
994 switch (action) {
995 case CPU_ONLINE:
996 case CPU_ONLINE_FROZEN:
997 idle = &per_cpu(s390_idle, cpu);
998 memset(idle, 0, sizeof(struct s390_idle_data));
999 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1000 break;
1001 case CPU_DEAD:
1002 case CPU_DEAD_FROZEN:
1003 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1004 break;
1006 return notifier_from_errno(err);
1009 static struct notifier_block __cpuinitdata smp_cpu_nb = {
1010 .notifier_call = smp_cpu_notify,
1013 static int __devinit smp_add_present_cpu(int cpu)
1015 struct cpu *c = &per_cpu(cpu_devices, cpu);
1016 struct sys_device *s = &c->sysdev;
1017 int rc;
1019 c->hotpluggable = 1;
1020 rc = register_cpu(c, cpu);
1021 if (rc)
1022 goto out;
1023 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1024 if (rc)
1025 goto out_cpu;
1026 if (!cpu_online(cpu))
1027 goto out;
1028 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1029 if (!rc)
1030 return 0;
1031 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1032 out_cpu:
1033 #ifdef CONFIG_HOTPLUG_CPU
1034 unregister_cpu(c);
1035 #endif
1036 out:
1037 return rc;
1040 #ifdef CONFIG_HOTPLUG_CPU
1042 int __ref smp_rescan_cpus(void)
1044 cpumask_t newcpus;
1045 int cpu;
1046 int rc;
1048 get_online_cpus();
1049 mutex_lock(&smp_cpu_state_mutex);
1050 cpumask_copy(&newcpus, cpu_present_mask);
1051 rc = __smp_rescan_cpus();
1052 if (rc)
1053 goto out;
1054 cpumask_andnot(&newcpus, cpu_present_mask, &newcpus);
1055 for_each_cpu(cpu, &newcpus) {
1056 rc = smp_add_present_cpu(cpu);
1057 if (rc)
1058 set_cpu_present(cpu, false);
1060 rc = 0;
1061 out:
1062 mutex_unlock(&smp_cpu_state_mutex);
1063 put_online_cpus();
1064 if (!cpumask_empty(&newcpus))
1065 topology_schedule_update();
1066 return rc;
1069 static ssize_t __ref rescan_store(struct sysdev_class *class,
1070 struct sysdev_class_attribute *attr,
1071 const char *buf,
1072 size_t count)
1074 int rc;
1076 rc = smp_rescan_cpus();
1077 return rc ? rc : count;
1079 static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
1080 #endif /* CONFIG_HOTPLUG_CPU */
1082 static ssize_t dispatching_show(struct sysdev_class *class,
1083 struct sysdev_class_attribute *attr,
1084 char *buf)
1086 ssize_t count;
1088 mutex_lock(&smp_cpu_state_mutex);
1089 count = sprintf(buf, "%d\n", cpu_management);
1090 mutex_unlock(&smp_cpu_state_mutex);
1091 return count;
1094 static ssize_t dispatching_store(struct sysdev_class *dev,
1095 struct sysdev_class_attribute *attr,
1096 const char *buf,
1097 size_t count)
1099 int val, rc;
1100 char delim;
1102 if (sscanf(buf, "%d %c", &val, &delim) != 1)
1103 return -EINVAL;
1104 if (val != 0 && val != 1)
1105 return -EINVAL;
1106 rc = 0;
1107 get_online_cpus();
1108 mutex_lock(&smp_cpu_state_mutex);
1109 if (cpu_management == val)
1110 goto out;
1111 rc = topology_set_cpu_management(val);
1112 if (!rc)
1113 cpu_management = val;
1114 out:
1115 mutex_unlock(&smp_cpu_state_mutex);
1116 put_online_cpus();
1117 return rc ? rc : count;
1119 static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
1120 dispatching_store);
1122 static int __init topology_init(void)
1124 int cpu;
1125 int rc;
1127 register_cpu_notifier(&smp_cpu_nb);
1129 #ifdef CONFIG_HOTPLUG_CPU
1130 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
1131 if (rc)
1132 return rc;
1133 #endif
1134 rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
1135 if (rc)
1136 return rc;
1137 for_each_present_cpu(cpu) {
1138 rc = smp_add_present_cpu(cpu);
1139 if (rc)
1140 return rc;
1142 return 0;
1144 subsys_initcall(topology_init);