^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Idle functions for s390.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 2014
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <trace/events/power.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "entry.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void enabled_wait(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long long idle_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long psw_mask, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Wait for external, I/O or machine check interrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT | PSW_MASK_DAT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) clear_cpu_flag(CIF_NOHZ_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Call the assembler magic in entry.S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) psw_idle(idle, psw_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Account time spent with enabled wait psw loaded as idle time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) raw_write_seqcount_begin(&idle->seqcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) idle->idle_time += idle_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) idle->idle_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) account_idle_time(cputime_to_nsecs(idle_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) raw_write_seqcount_end(&idle->seqcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) NOKPROBE_SYMBOL(enabled_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static ssize_t show_idle_count(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long long idle_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) seq = read_seqcount_begin(&idle->seqcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) idle_count = READ_ONCE(idle->idle_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (READ_ONCE(idle->clock_idle_enter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) idle_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } while (read_seqcount_retry(&idle->seqcount, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return sprintf(buf, "%llu\n", idle_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static ssize_t show_idle_time(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long long now, idle_time, idle_enter, idle_exit, in_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) seq = read_seqcount_begin(&idle->seqcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) idle_time = READ_ONCE(idle->idle_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) idle_enter = READ_ONCE(idle->clock_idle_enter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) idle_exit = READ_ONCE(idle->clock_idle_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) } while (read_seqcount_retry(&idle->seqcount, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) in_idle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) now = get_tod_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (idle_enter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (idle_exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) in_idle = idle_exit - idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } else if (now > idle_enter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) in_idle = now - idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) idle_time += in_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return sprintf(buf, "%llu\n", idle_time >> 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u64 arch_cpu_idle_time(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned long long now, idle_enter, idle_exit, in_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) seq = read_seqcount_begin(&idle->seqcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) idle_enter = READ_ONCE(idle->clock_idle_enter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) idle_exit = READ_ONCE(idle->clock_idle_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) } while (read_seqcount_retry(&idle->seqcount, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) in_idle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) now = get_tod_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (idle_enter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (idle_exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) in_idle = idle_exit - idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) } else if (now > idle_enter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) in_idle = now - idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return cputime_to_nsecs(in_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void arch_cpu_idle_enter(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) void arch_cpu_idle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) enabled_wait();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) raw_local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void arch_cpu_idle_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void arch_cpu_idle_dead(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) cpu_die();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }