xref: /linux/arch/s390/kernel/idle.c (revision ad5a9e14ec8b4a868fea13a9dfa1fb38b2c35354)
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 
35 	/* Dyntick idle time accounted by nohz/scheduler */
36 	if (!idle->idle_dyntick)
37 		account_idle_time(cputime_to_nsecs(idle_time));
38 }
39 
40 void noinstr arch_cpu_idle(void)
41 {
42 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
43 	unsigned long psw_mask;
44 
45 	/* Wait for external, I/O or machine check interrupt. */
46 	psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT |
47 		   PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
48 	clear_cpu_flag(CIF_NOHZ_DELAY);
49 	set_cpu_flag(CIF_ENABLED_WAIT);
50 	if (smp_cpu_mtid)
51 		stcctm(MT_DIAG, smp_cpu_mtid, (u64 *)&idle->mt_cycles_enter);
52 	idle->clock_idle_enter = get_tod_clock_fast();
53 	idle->timer_idle_enter = get_cpu_timer();
54 	bpon();
55 	__load_psw_mask(psw_mask);
56 }
57 
58 static ssize_t show_idle_count(struct device *dev,
59 			       struct device_attribute *attr, char *buf)
60 {
61 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
62 
63 	return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_count));
64 }
65 DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
66 
67 static ssize_t show_idle_time(struct device *dev,
68 			      struct device_attribute *attr, char *buf)
69 {
70 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
71 
72 	return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_time) >> 12);
73 }
74 DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
75 
76 void arch_cpu_idle_enter(void)
77 {
78 }
79 
80 void arch_cpu_idle_exit(void)
81 {
82 }
83 
84 void __noreturn arch_cpu_idle_dead(void)
85 {
86 	cpu_die();
87 }
88