blob: 99f1d8185ae2756cdefa5d320c6d46f894b3175e [file] [log] [blame]
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +02001/*
2 * Idle functions for s390.
3 *
4 * Copyright IBM Corp. 2014
5 *
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8
9#include <linux/kernel.h>
10#include <linux/kernel_stat.h>
11#include <linux/kprobes.h>
12#include <linux/notifier.h>
13#include <linux/init.h>
14#include <linux/cpu.h>
15#include <asm/cputime.h>
16#include <asm/nmi.h>
17#include <asm/smp.h>
18#include "entry.h"
19
20static DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
21
Heiko Carstens7a5388d2014-10-22 12:42:38 +020022void enabled_wait(void)
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020023{
Linus Torvalds0429fbc2014-10-15 07:48:18 +020024 struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020025 unsigned long long idle_time;
26 unsigned long psw_mask;
27
28 trace_hardirqs_on();
29
30 /* Wait for external, I/O or machine check interrupt. */
31 psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT | PSW_MASK_DAT |
32 PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
33 clear_cpu_flag(CIF_NOHZ_DELAY);
34
35 /* Call the assembler magic in entry.S */
36 psw_idle(idle, psw_mask);
37
Heiko Carstens200e7c02014-12-01 14:24:41 +010038 trace_hardirqs_off();
39
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020040 /* Account time spent with enabled wait psw loaded as idle time. */
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010041 write_seqcount_begin(&idle->seqcount);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020042 idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
43 idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
44 idle->idle_time += idle_time;
45 idle->idle_count++;
Frederic Weisbecker18b43a92017-01-31 04:09:39 +010046 account_idle_time(cputime_to_nsecs(idle_time));
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010047 write_seqcount_end(&idle->seqcount);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020048}
Heiko Carstens7a5388d2014-10-22 12:42:38 +020049NOKPROBE_SYMBOL(enabled_wait);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020050
51static ssize_t show_idle_count(struct device *dev,
52 struct device_attribute *attr, char *buf)
53{
54 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
55 unsigned long long idle_count;
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010056 unsigned int seq;
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020057
58 do {
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010059 seq = read_seqcount_begin(&idle->seqcount);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020060 idle_count = ACCESS_ONCE(idle->idle_count);
61 if (ACCESS_ONCE(idle->clock_idle_enter))
62 idle_count++;
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010063 } while (read_seqcount_retry(&idle->seqcount, seq));
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020064 return sprintf(buf, "%llu\n", idle_count);
65}
66DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
67
68static ssize_t show_idle_time(struct device *dev,
69 struct device_attribute *attr, char *buf)
70{
71 struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
72 unsigned long long now, idle_time, idle_enter, idle_exit;
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010073 unsigned int seq;
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020074
75 do {
76 now = get_tod_clock();
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010077 seq = read_seqcount_begin(&idle->seqcount);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020078 idle_time = ACCESS_ONCE(idle->idle_time);
79 idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
80 idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010081 } while (read_seqcount_retry(&idle->seqcount, seq));
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020082 idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
83 return sprintf(buf, "%llu\n", idle_time >> 12);
84}
85DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
86
87cputime64_t arch_cpu_idle_time(int cpu)
88{
89 struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
90 unsigned long long now, idle_enter, idle_exit;
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010091 unsigned int seq;
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020092
93 do {
94 now = get_tod_clock();
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010095 seq = read_seqcount_begin(&idle->seqcount);
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020096 idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
97 idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
Frederic Weisbecker1ce21802014-11-28 19:23:34 +010098 } while (read_seqcount_retry(&idle->seqcount, seq));
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020099 return idle_enter ? ((idle_exit ?: now) - idle_enter) : 0;
100}
101
102void arch_cpu_idle_enter(void)
103{
104 local_mcck_disable();
105}
106
107void arch_cpu_idle(void)
108{
109 if (!test_cpu_flag(CIF_MCCK_PENDING))
110 /* Halt the cpu and keep track of cpu time accounting. */
111 enabled_wait();
112 local_irq_enable();
113}
114
115void arch_cpu_idle_exit(void)
116{
117 local_mcck_enable();
118 if (test_cpu_flag(CIF_MCCK_PENDING))
119 s390_handle_mcck();
120}
121
122void arch_cpu_idle_dead(void)
123{
124 cpu_die();
125}