xref: /openbmc/linux/arch/s390/kernel/idle.c (revision 56e62a73702836017564eaacd5212e4d0fa1c01d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b5f87f15SMartin Schwidefsky /*
3b5f87f15SMartin Schwidefsky  * Idle functions for s390.
4b5f87f15SMartin Schwidefsky  *
5b5f87f15SMartin Schwidefsky  * Copyright IBM Corp. 2014
6b5f87f15SMartin Schwidefsky  *
7b5f87f15SMartin Schwidefsky  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8b5f87f15SMartin Schwidefsky  */
9b5f87f15SMartin Schwidefsky 
10b5f87f15SMartin Schwidefsky #include <linux/kernel.h>
11b5f87f15SMartin Schwidefsky #include <linux/kernel_stat.h>
12b5f87f15SMartin Schwidefsky #include <linux/notifier.h>
13b5f87f15SMartin Schwidefsky #include <linux/init.h>
14b5f87f15SMartin Schwidefsky #include <linux/cpu.h>
1532ef5517SIngo Molnar #include <linux/sched/cputime.h>
166589c93fSSven Schnelle #include <trace/events/power.h>
17*56e62a73SSven Schnelle #include <asm/cpu_mf.h>
18b5f87f15SMartin Schwidefsky #include <asm/nmi.h>
19b5f87f15SMartin Schwidefsky #include <asm/smp.h>
20b5f87f15SMartin Schwidefsky #include "entry.h"
21b5f87f15SMartin Schwidefsky 
22b5f87f15SMartin Schwidefsky static DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
23b5f87f15SMartin Schwidefsky 
24*56e62a73SSven Schnelle void account_idle_time_irq(void)
25*56e62a73SSven Schnelle {
26*56e62a73SSven Schnelle 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
27*56e62a73SSven Schnelle 	u64 cycles_new[8];
28*56e62a73SSven Schnelle 	int i;
29*56e62a73SSven Schnelle 
30*56e62a73SSven Schnelle 	clear_cpu_flag(CIF_ENABLED_WAIT);
31*56e62a73SSven Schnelle 	if (smp_cpu_mtid) {
32*56e62a73SSven Schnelle 		stcctm(MT_DIAG, smp_cpu_mtid, cycles_new);
33*56e62a73SSven Schnelle 		for (i = 0; i < smp_cpu_mtid; i++)
34*56e62a73SSven Schnelle 			this_cpu_add(mt_cycles[i], cycles_new[i] - idle->mt_cycles_enter[i]);
35*56e62a73SSven Schnelle 	}
36*56e62a73SSven Schnelle 
37*56e62a73SSven Schnelle 	idle->clock_idle_exit = S390_lowcore.int_clock;
38*56e62a73SSven Schnelle 	idle->timer_idle_exit = S390_lowcore.sys_enter_timer;
39*56e62a73SSven Schnelle 
40*56e62a73SSven Schnelle 	S390_lowcore.steal_timer += idle->clock_idle_enter - S390_lowcore.last_update_clock;
41*56e62a73SSven Schnelle 	S390_lowcore.last_update_clock = idle->clock_idle_exit;
42*56e62a73SSven Schnelle 
43*56e62a73SSven Schnelle 	S390_lowcore.system_timer += S390_lowcore.last_update_timer - idle->timer_idle_enter;
44*56e62a73SSven Schnelle 	S390_lowcore.last_update_timer = idle->timer_idle_exit;
45*56e62a73SSven Schnelle }
46*56e62a73SSven Schnelle 
4744292c86SHeiko Carstens void arch_cpu_idle(void)
48b5f87f15SMartin Schwidefsky {
490429fbc0SLinus Torvalds 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
50b5f87f15SMartin Schwidefsky 	unsigned long long idle_time;
517494755aSHeiko Carstens 	unsigned long psw_mask;
52b5f87f15SMartin Schwidefsky 
53b5f87f15SMartin Schwidefsky 	/* Wait for external, I/O or machine check interrupt. */
54b5f87f15SMartin Schwidefsky 	psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT | PSW_MASK_DAT |
55b5f87f15SMartin Schwidefsky 		PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
56b5f87f15SMartin Schwidefsky 	clear_cpu_flag(CIF_NOHZ_DELAY);
57b5f87f15SMartin Schwidefsky 
587494755aSHeiko Carstens 	/* psw_idle() returns with interrupts disabled. */
59b5f87f15SMartin Schwidefsky 	psw_idle(idle, psw_mask);
60200e7c0fSHeiko Carstens 
61b5f87f15SMartin Schwidefsky 	/* Account time spent with enabled wait psw loaded as idle time. */
62ca589ea8SPeter Zijlstra 	raw_write_seqcount_begin(&idle->seqcount);
63b5f87f15SMartin Schwidefsky 	idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
64b5f87f15SMartin Schwidefsky 	idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
65b5f87f15SMartin Schwidefsky 	idle->idle_time += idle_time;
66b5f87f15SMartin Schwidefsky 	idle->idle_count++;
6718b43a9bSFrederic Weisbecker 	account_idle_time(cputime_to_nsecs(idle_time));
68ca589ea8SPeter Zijlstra 	raw_write_seqcount_end(&idle->seqcount);
6944292c86SHeiko Carstens 	raw_local_irq_enable();
70b5f87f15SMartin Schwidefsky }
71b5f87f15SMartin Schwidefsky 
72b5f87f15SMartin Schwidefsky static ssize_t show_idle_count(struct device *dev,
73b5f87f15SMartin Schwidefsky 				struct device_attribute *attr, char *buf)
74b5f87f15SMartin Schwidefsky {
75b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
76b5f87f15SMartin Schwidefsky 	unsigned long long idle_count;
771ce21804SFrederic Weisbecker 	unsigned int seq;
78b5f87f15SMartin Schwidefsky 
79b5f87f15SMartin Schwidefsky 	do {
801ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
81187b5f41SChristian Borntraeger 		idle_count = READ_ONCE(idle->idle_count);
82187b5f41SChristian Borntraeger 		if (READ_ONCE(idle->clock_idle_enter))
83b5f87f15SMartin Schwidefsky 			idle_count++;
841ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
85b5f87f15SMartin Schwidefsky 	return sprintf(buf, "%llu\n", idle_count);
86b5f87f15SMartin Schwidefsky }
87b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
88b5f87f15SMartin Schwidefsky 
89b5f87f15SMartin Schwidefsky static ssize_t show_idle_time(struct device *dev,
90b5f87f15SMartin Schwidefsky 				struct device_attribute *attr, char *buf)
91b5f87f15SMartin Schwidefsky {
923d7efa4eSHeiko Carstens 	unsigned long long now, idle_time, idle_enter, idle_exit, in_idle;
93b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
941ce21804SFrederic Weisbecker 	unsigned int seq;
95b5f87f15SMartin Schwidefsky 
96b5f87f15SMartin Schwidefsky 	do {
971ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
98187b5f41SChristian Borntraeger 		idle_time = READ_ONCE(idle->idle_time);
99187b5f41SChristian Borntraeger 		idle_enter = READ_ONCE(idle->clock_idle_enter);
100187b5f41SChristian Borntraeger 		idle_exit = READ_ONCE(idle->clock_idle_exit);
1011ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
1023d7efa4eSHeiko Carstens 	in_idle = 0;
1033d7efa4eSHeiko Carstens 	now = get_tod_clock();
1043d7efa4eSHeiko Carstens 	if (idle_enter) {
1053d7efa4eSHeiko Carstens 		if (idle_exit) {
1063d7efa4eSHeiko Carstens 			in_idle = idle_exit - idle_enter;
1073d7efa4eSHeiko Carstens 		} else if (now > idle_enter) {
1083d7efa4eSHeiko Carstens 			in_idle = now - idle_enter;
1093d7efa4eSHeiko Carstens 		}
1103d7efa4eSHeiko Carstens 	}
1113d7efa4eSHeiko Carstens 	idle_time += in_idle;
112b5f87f15SMartin Schwidefsky 	return sprintf(buf, "%llu\n", idle_time >> 12);
113b5f87f15SMartin Schwidefsky }
114b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
115b5f87f15SMartin Schwidefsky 
11642b425b3SFrederic Weisbecker u64 arch_cpu_idle_time(int cpu)
117b5f87f15SMartin Schwidefsky {
118b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
1193d7efa4eSHeiko Carstens 	unsigned long long now, idle_enter, idle_exit, in_idle;
1201ce21804SFrederic Weisbecker 	unsigned int seq;
121b5f87f15SMartin Schwidefsky 
122b5f87f15SMartin Schwidefsky 	do {
1231ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
124187b5f41SChristian Borntraeger 		idle_enter = READ_ONCE(idle->clock_idle_enter);
125187b5f41SChristian Borntraeger 		idle_exit = READ_ONCE(idle->clock_idle_exit);
1261ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
1273d7efa4eSHeiko Carstens 	in_idle = 0;
1283d7efa4eSHeiko Carstens 	now = get_tod_clock();
1293d7efa4eSHeiko Carstens 	if (idle_enter) {
1303d7efa4eSHeiko Carstens 		if (idle_exit) {
1313d7efa4eSHeiko Carstens 			in_idle = idle_exit - idle_enter;
1323d7efa4eSHeiko Carstens 		} else if (now > idle_enter) {
1333d7efa4eSHeiko Carstens 			in_idle = now - idle_enter;
1343d7efa4eSHeiko Carstens 		}
1353d7efa4eSHeiko Carstens 	}
1363d7efa4eSHeiko Carstens 	return cputime_to_nsecs(in_idle);
137b5f87f15SMartin Schwidefsky }
138b5f87f15SMartin Schwidefsky 
139b5f87f15SMartin Schwidefsky void arch_cpu_idle_enter(void)
140b5f87f15SMartin Schwidefsky {
141b5f87f15SMartin Schwidefsky }
142b5f87f15SMartin Schwidefsky 
143b5f87f15SMartin Schwidefsky void arch_cpu_idle_exit(void)
144b5f87f15SMartin Schwidefsky {
145b5f87f15SMartin Schwidefsky }
146b5f87f15SMartin Schwidefsky 
147b5f87f15SMartin Schwidefsky void arch_cpu_idle_dead(void)
148b5f87f15SMartin Schwidefsky {
149b5f87f15SMartin Schwidefsky 	cpu_die();
150b5f87f15SMartin Schwidefsky }
151