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>
156589c93fSSven Schnelle #include <trace/events/power.h>
1656e62a73SSven Schnelle #include <asm/cpu_mf.h>
17c8997020SNicholas Piggin #include <asm/cputime.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
account_idle_time_irq(void)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 if (smp_cpu_mtid) {
3256e62a73SSven Schnelle stcctm(MT_DIAG, smp_cpu_mtid, cycles_new);
3356e62a73SSven Schnelle for (i = 0; i < smp_cpu_mtid; i++)
3456e62a73SSven Schnelle this_cpu_add(mt_cycles[i], cycles_new[i] - idle->mt_cycles_enter[i]);
3556e62a73SSven Schnelle }
3656e62a73SSven Schnelle
37be76ea61SHeiko Carstens idle_time = S390_lowcore.int_clock - idle->clock_idle_enter;
3856e62a73SSven Schnelle
3956e62a73SSven Schnelle S390_lowcore.steal_timer += idle->clock_idle_enter - S390_lowcore.last_update_clock;
40be76ea61SHeiko Carstens S390_lowcore.last_update_clock = S390_lowcore.int_clock;
4156e62a73SSven Schnelle
4256e62a73SSven Schnelle S390_lowcore.system_timer += S390_lowcore.last_update_timer - idle->timer_idle_enter;
43be76ea61SHeiko Carstens S390_lowcore.last_update_timer = S390_lowcore.sys_enter_timer;
44c0101629SHeiko Carstens
45c0101629SHeiko Carstens /* Account time spent with enabled wait psw loaded as idle time. */
46be76ea61SHeiko Carstens WRITE_ONCE(idle->idle_time, READ_ONCE(idle->idle_time) + idle_time);
47be76ea61SHeiko Carstens WRITE_ONCE(idle->idle_count, READ_ONCE(idle->idle_count) + 1);
48c0101629SHeiko Carstens account_idle_time(cputime_to_nsecs(idle_time));
4956e62a73SSven Schnelle }
5056e62a73SSven Schnelle
arch_cpu_idle(void)51a9cbc1b4SHeiko Carstens void noinstr arch_cpu_idle(void)
52b5f87f15SMartin Schwidefsky {
530429fbc0SLinus Torvalds struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
547494755aSHeiko Carstens unsigned long psw_mask;
55b5f87f15SMartin Schwidefsky
56b5f87f15SMartin Schwidefsky /* Wait for external, I/O or machine check interrupt. */
57bb1520d5SAlexander Gordeev psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT |
58b5f87f15SMartin Schwidefsky PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
59b5f87f15SMartin Schwidefsky clear_cpu_flag(CIF_NOHZ_DELAY);
60b5f87f15SMartin Schwidefsky
617494755aSHeiko Carstens /* psw_idle() returns with interrupts disabled. */
62b5f87f15SMartin Schwidefsky psw_idle(idle, psw_mask);
63b5f87f15SMartin Schwidefsky }
64b5f87f15SMartin Schwidefsky
show_idle_count(struct device * dev,struct device_attribute * attr,char * buf)65b5f87f15SMartin Schwidefsky static ssize_t show_idle_count(struct device *dev,
66b5f87f15SMartin Schwidefsky struct device_attribute *attr, char *buf)
67b5f87f15SMartin Schwidefsky {
68b5f87f15SMartin Schwidefsky struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
69b5f87f15SMartin Schwidefsky
70be76ea61SHeiko Carstens return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_count));
71b5f87f15SMartin Schwidefsky }
72b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
73b5f87f15SMartin Schwidefsky
show_idle_time(struct device * dev,struct device_attribute * attr,char * buf)74b5f87f15SMartin Schwidefsky static ssize_t show_idle_time(struct device *dev,
75b5f87f15SMartin Schwidefsky struct device_attribute *attr, char *buf)
76b5f87f15SMartin Schwidefsky {
77b5f87f15SMartin Schwidefsky struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
78b5f87f15SMartin Schwidefsky
79be76ea61SHeiko Carstens return sysfs_emit(buf, "%lu\n", READ_ONCE(idle->idle_time) >> 12);
80b5f87f15SMartin Schwidefsky }
81b5f87f15SMartin Schwidefsky DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
82b5f87f15SMartin Schwidefsky
arch_cpu_idle_enter(void)83b5f87f15SMartin Schwidefsky void arch_cpu_idle_enter(void)
84b5f87f15SMartin Schwidefsky {
85b5f87f15SMartin Schwidefsky }
86b5f87f15SMartin Schwidefsky
arch_cpu_idle_exit(void)87b5f87f15SMartin Schwidefsky void arch_cpu_idle_exit(void)
88b5f87f15SMartin Schwidefsky {
89b5f87f15SMartin Schwidefsky }
90b5f87f15SMartin Schwidefsky
arch_cpu_idle_dead(void)91*071c44e4SJosh Poimboeuf void __noreturn arch_cpu_idle_dead(void)
92b5f87f15SMartin Schwidefsky {
93b5f87f15SMartin Schwidefsky cpu_die();
94b5f87f15SMartin Schwidefsky }
95