xref: /openbmc/linux/arch/s390/kernel/idle.c (revision a9cbc1b471d291c865907542394f1c483b93a811)
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>
1756e62a73SSven 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 
2456e62a73SSven Schnelle void account_idle_time_irq(void)
2556e62a73SSven Schnelle {
2656e62a73SSven Schnelle 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
27c0101629SHeiko Carstens 	unsigned long idle_time;
2856e62a73SSven Schnelle 	u64 cycles_new[8];
2956e62a73SSven Schnelle 	int i;
3056e62a73SSven Schnelle 
3156e62a73SSven Schnelle 	clear_cpu_flag(CIF_ENABLED_WAIT);
3256e62a73SSven Schnelle 	if (smp_cpu_mtid) {
3356e62a73SSven Schnelle 		stcctm(MT_DIAG, smp_cpu_mtid, cycles_new);
3456e62a73SSven Schnelle 		for (i = 0; i < smp_cpu_mtid; i++)
3556e62a73SSven Schnelle 			this_cpu_add(mt_cycles[i], cycles_new[i] - idle->mt_cycles_enter[i]);
3656e62a73SSven Schnelle 	}
3756e62a73SSven Schnelle 
3856e62a73SSven Schnelle 	idle->clock_idle_exit = S390_lowcore.int_clock;
3956e62a73SSven Schnelle 	idle->timer_idle_exit = S390_lowcore.sys_enter_timer;
4056e62a73SSven Schnelle 
4156e62a73SSven Schnelle 	S390_lowcore.steal_timer += idle->clock_idle_enter - S390_lowcore.last_update_clock;
4256e62a73SSven Schnelle 	S390_lowcore.last_update_clock = idle->clock_idle_exit;
4356e62a73SSven Schnelle 
4456e62a73SSven Schnelle 	S390_lowcore.system_timer += S390_lowcore.last_update_timer - idle->timer_idle_enter;
4556e62a73SSven Schnelle 	S390_lowcore.last_update_timer = idle->timer_idle_exit;
46c0101629SHeiko Carstens 
47c0101629SHeiko Carstens 	/* Account time spent with enabled wait psw loaded as idle time. */
48c0101629SHeiko Carstens 	raw_write_seqcount_begin(&idle->seqcount);
49c0101629SHeiko Carstens 	idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
50c0101629SHeiko Carstens 	idle->clock_idle_enter = 0;
51c0101629SHeiko Carstens 	idle->clock_idle_exit = 0;
52c0101629SHeiko Carstens 	idle->idle_time += idle_time;
53c0101629SHeiko Carstens 	idle->idle_count++;
54c0101629SHeiko Carstens 	account_idle_time(cputime_to_nsecs(idle_time));
55c0101629SHeiko Carstens 	raw_write_seqcount_end(&idle->seqcount);
5656e62a73SSven Schnelle }
5756e62a73SSven Schnelle 
58*a9cbc1b4SHeiko Carstens void noinstr arch_cpu_idle(void)
59b5f87f15SMartin Schwidefsky {
600429fbc0SLinus Torvalds 	struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
617494755aSHeiko Carstens 	unsigned long psw_mask;
62b5f87f15SMartin Schwidefsky 
63b5f87f15SMartin Schwidefsky 	/* Wait for external, I/O or machine check interrupt. */
64bb1520d5SAlexander Gordeev 	psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT |
65b5f87f15SMartin Schwidefsky 		   PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
66b5f87f15SMartin Schwidefsky 	clear_cpu_flag(CIF_NOHZ_DELAY);
67b5f87f15SMartin Schwidefsky 
687494755aSHeiko Carstens 	/* psw_idle() returns with interrupts disabled. */
69b5f87f15SMartin Schwidefsky 	psw_idle(idle, psw_mask);
7044292c86SHeiko Carstens 	raw_local_irq_enable();
71b5f87f15SMartin Schwidefsky }
72b5f87f15SMartin Schwidefsky 
73b5f87f15SMartin Schwidefsky static ssize_t show_idle_count(struct device *dev,
74b5f87f15SMartin Schwidefsky 				struct device_attribute *attr, char *buf)
75b5f87f15SMartin Schwidefsky {
76b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
77eba8e1afSHeiko Carstens 	unsigned long idle_count;
781ce21804SFrederic Weisbecker 	unsigned int seq;
79b5f87f15SMartin Schwidefsky 
80b5f87f15SMartin Schwidefsky 	do {
811ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
82187b5f41SChristian Borntraeger 		idle_count = READ_ONCE(idle->idle_count);
83187b5f41SChristian Borntraeger 		if (READ_ONCE(idle->clock_idle_enter))
84b5f87f15SMartin Schwidefsky 			idle_count++;
851ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
86eba8e1afSHeiko Carstens 	return sprintf(buf, "%lu\n", idle_count);
87b5f87f15SMartin Schwidefsky }
88b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
89b5f87f15SMartin Schwidefsky 
90b5f87f15SMartin Schwidefsky static ssize_t show_idle_time(struct device *dev,
91b5f87f15SMartin Schwidefsky 				struct device_attribute *attr, char *buf)
92b5f87f15SMartin Schwidefsky {
93eba8e1afSHeiko Carstens 	unsigned long now, idle_time, idle_enter, idle_exit, in_idle;
94b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
951ce21804SFrederic Weisbecker 	unsigned int seq;
96b5f87f15SMartin Schwidefsky 
97b5f87f15SMartin Schwidefsky 	do {
981ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
99187b5f41SChristian Borntraeger 		idle_time = READ_ONCE(idle->idle_time);
100187b5f41SChristian Borntraeger 		idle_enter = READ_ONCE(idle->clock_idle_enter);
101187b5f41SChristian Borntraeger 		idle_exit = READ_ONCE(idle->clock_idle_exit);
1021ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
1033d7efa4eSHeiko Carstens 	in_idle = 0;
1043d7efa4eSHeiko Carstens 	now = get_tod_clock();
1053d7efa4eSHeiko Carstens 	if (idle_enter) {
1063d7efa4eSHeiko Carstens 		if (idle_exit) {
1073d7efa4eSHeiko Carstens 			in_idle = idle_exit - idle_enter;
1083d7efa4eSHeiko Carstens 		} else if (now > idle_enter) {
1093d7efa4eSHeiko Carstens 			in_idle = now - idle_enter;
1103d7efa4eSHeiko Carstens 		}
1113d7efa4eSHeiko Carstens 	}
1123d7efa4eSHeiko Carstens 	idle_time += in_idle;
113eba8e1afSHeiko Carstens 	return sprintf(buf, "%lu\n", idle_time >> 12);
114b5f87f15SMartin Schwidefsky }
115b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
116b5f87f15SMartin Schwidefsky 
11742b425b3SFrederic Weisbecker u64 arch_cpu_idle_time(int cpu)
118b5f87f15SMartin Schwidefsky {
119b5f87f15SMartin Schwidefsky 	struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
120eba8e1afSHeiko Carstens 	unsigned long now, idle_enter, idle_exit, in_idle;
1211ce21804SFrederic Weisbecker 	unsigned int seq;
122b5f87f15SMartin Schwidefsky 
123b5f87f15SMartin Schwidefsky 	do {
1241ce21804SFrederic Weisbecker 		seq = read_seqcount_begin(&idle->seqcount);
125187b5f41SChristian Borntraeger 		idle_enter = READ_ONCE(idle->clock_idle_enter);
126187b5f41SChristian Borntraeger 		idle_exit = READ_ONCE(idle->clock_idle_exit);
1271ce21804SFrederic Weisbecker 	} while (read_seqcount_retry(&idle->seqcount, seq));
1283d7efa4eSHeiko Carstens 	in_idle = 0;
1293d7efa4eSHeiko Carstens 	now = get_tod_clock();
1303d7efa4eSHeiko Carstens 	if (idle_enter) {
1313d7efa4eSHeiko Carstens 		if (idle_exit) {
1323d7efa4eSHeiko Carstens 			in_idle = idle_exit - idle_enter;
1333d7efa4eSHeiko Carstens 		} else if (now > idle_enter) {
1343d7efa4eSHeiko Carstens 			in_idle = now - idle_enter;
1353d7efa4eSHeiko Carstens 		}
1363d7efa4eSHeiko Carstens 	}
1373d7efa4eSHeiko Carstens 	return cputime_to_nsecs(in_idle);
138b5f87f15SMartin Schwidefsky }
139b5f87f15SMartin Schwidefsky 
140b5f87f15SMartin Schwidefsky void arch_cpu_idle_enter(void)
141b5f87f15SMartin Schwidefsky {
142b5f87f15SMartin Schwidefsky }
143b5f87f15SMartin Schwidefsky 
144b5f87f15SMartin Schwidefsky void arch_cpu_idle_exit(void)
145b5f87f15SMartin Schwidefsky {
146b5f87f15SMartin Schwidefsky }
147b5f87f15SMartin Schwidefsky 
148b5f87f15SMartin Schwidefsky void arch_cpu_idle_dead(void)
149b5f87f15SMartin Schwidefsky {
150b5f87f15SMartin Schwidefsky 	cpu_die();
151b5f87f15SMartin Schwidefsky }
152