xref: /linux/arch/s390/kernel/idle.c (revision f41941aab3acd33f13d65a2ae496329bc8ae4de0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Idle functions for s390.
4  *
5  * Copyright IBM Corp. 2014
6  *
7  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/notifier.h>
13 #include <linux/init.h>
14 #include <linux/cpu.h>
15 #include <trace/events/power.h>
16 #include <asm/cpu_mf.h>
17 #include <asm/cputime.h>
18 #include <asm/idle.h>
19 #include <asm/nmi.h>
20 #include <asm/smp.h>
21 
22 DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
23 
24 void account_idle_time_irq(void)
25 {
26 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
27 	unsigned long idle_time;
28 
29 	idle_time = get_lowcore()->int_clock - idle->clock_idle_enter;
30 
31 	/* Account time spent with enabled wait psw loaded as idle time. */
32 	__atomic64_add(idle_time, &idle->idle_time);
33 	__atomic64_add_const(1, &idle->idle_count);
34 	account_idle_time(cputime_to_nsecs(idle_time));
35 }
36 
37 void noinstr arch_cpu_idle(void)
38 {
39 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
40 	unsigned long psw_mask;
41 
42 	/* Wait for external, I/O or machine check interrupt. */
43 	psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT |
44 		   PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
45 	clear_cpu_flag(CIF_NOHZ_DELAY);
46 	set_cpu_flag(CIF_ENABLED_WAIT);
47 	if (smp_cpu_mtid)
48 		stcctm(MT_DIAG, smp_cpu_mtid, (u64 *)&idle->mt_cycles_enter);
49 	idle->clock_idle_enter = get_tod_clock_fast();
50 	idle->timer_idle_enter = get_cpu_timer();
51 	bpon();
52 	__load_psw_mask(psw_mask);
53 }
54 
55 static ssize_t show_idle_count(struct device *dev,
56 			       struct device_attribute *attr, char *buf)
57 {
58 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
59 
60 	return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_count));
61 }
62 DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
63 
64 static ssize_t show_idle_time(struct device *dev,
65 			      struct device_attribute *attr, char *buf)
66 {
67 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
68 
69 	return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_time) >> 12);
70 }
71 DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
72 
73 void arch_cpu_idle_enter(void)
74 {
75 }
76 
77 void arch_cpu_idle_exit(void)
78 {
79 }
80 
81 void __noreturn arch_cpu_idle_dead(void)
82 {
83 	cpu_die();
84 }
85