1 // SPDX-License-Identifier: GPL-2.0
3 * Idle functions for s390.
5 * Copyright IBM Corp. 2014
7 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
10 #include <linux/kernel.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/kprobes.h>
13 #include <linux/notifier.h>
14 #include <linux/init.h>
15 #include <linux/cpu.h>
16 #include <linux/sched/cputime.h>
17 #include <trace/events/power.h>
22 static DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
24 void arch_cpu_idle(void)
26 struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
27 unsigned long long idle_time;
28 unsigned long psw_mask, flags;
31 /* Wait for external, I/O or machine check interrupt. */
32 psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT | PSW_MASK_DAT |
33 PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
34 clear_cpu_flag(CIF_NOHZ_DELAY);
36 raw_local_irq_save(flags);
37 /* Call the assembler magic in entry.S */
38 psw_idle(idle, psw_mask);
39 raw_local_irq_restore(flags);
41 /* Account time spent with enabled wait psw loaded as idle time. */
42 raw_write_seqcount_begin(&idle->seqcount);
43 idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
44 idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
45 idle->idle_time += idle_time;
47 account_idle_time(cputime_to_nsecs(idle_time));
48 raw_write_seqcount_end(&idle->seqcount);
49 raw_local_irq_enable();
51 NOKPROBE_SYMBOL(arch_cpu_idle);
53 static ssize_t show_idle_count(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
57 unsigned long long idle_count;
61 seq = read_seqcount_begin(&idle->seqcount);
62 idle_count = READ_ONCE(idle->idle_count);
63 if (READ_ONCE(idle->clock_idle_enter))
65 } while (read_seqcount_retry(&idle->seqcount, seq));
66 return sprintf(buf, "%llu\n", idle_count);
68 DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
70 static ssize_t show_idle_time(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 unsigned long long now, idle_time, idle_enter, idle_exit, in_idle;
74 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
78 seq = read_seqcount_begin(&idle->seqcount);
79 idle_time = READ_ONCE(idle->idle_time);
80 idle_enter = READ_ONCE(idle->clock_idle_enter);
81 idle_exit = READ_ONCE(idle->clock_idle_exit);
82 } while (read_seqcount_retry(&idle->seqcount, seq));
84 now = get_tod_clock();
87 in_idle = idle_exit - idle_enter;
88 } else if (now > idle_enter) {
89 in_idle = now - idle_enter;
93 return sprintf(buf, "%llu\n", idle_time >> 12);
95 DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
97 u64 arch_cpu_idle_time(int cpu)
99 struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
100 unsigned long long now, idle_enter, idle_exit, in_idle;
104 seq = read_seqcount_begin(&idle->seqcount);
105 idle_enter = READ_ONCE(idle->clock_idle_enter);
106 idle_exit = READ_ONCE(idle->clock_idle_exit);
107 } while (read_seqcount_retry(&idle->seqcount, seq));
109 now = get_tod_clock();
112 in_idle = idle_exit - idle_enter;
113 } else if (now > idle_enter) {
114 in_idle = now - idle_enter;
117 return cputime_to_nsecs(in_idle);
120 void arch_cpu_idle_enter(void)
124 void arch_cpu_idle_exit(void)
128 void arch_cpu_idle_dead(void)