blob: 0814348782966ac99c571990c89975d06aedffe1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-s390/cputime.h
3 *
4 * (C) Copyright IBM Corp. 2004
5 *
6 * Author: Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8
9#ifndef _S390_CPUTIME_H
10#define _S390_CPUTIME_H
11
Martin Schwidefsky76d4e002009-06-12 10:26:21 +020012#include <linux/types.h>
13#include <linux/percpu.h>
14#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/div64.h>
16
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010017/* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19typedef unsigned long long cputime_t;
20typedef unsigned long long cputime64_t;
21
22#ifndef __s390x__
23
24static inline unsigned int
25__div(unsigned long long n, unsigned int base)
26{
27 register_pair rp;
28
29 rp.pair = n >> 1;
30 asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1));
31 return rp.subreg.odd;
32}
33
34#else /* __s390x__ */
35
36static inline unsigned int
37__div(unsigned long long n, unsigned int base)
38{
39 return n / base;
40}
41
42#endif /* __s390x__ */
43
44#define cputime_zero (0ULL)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +020045#define cputime_one_jiffy jiffies_to_cputime(1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define cputime_max ((~0UL >> 1) - 1)
47#define cputime_add(__a, __b) ((__a) + (__b))
48#define cputime_sub(__a, __b) ((__a) - (__b))
49#define cputime_div(__a, __n) ({ \
50 unsigned long long __div = (__a); \
51 do_div(__div,__n); \
52 __div; \
53})
54#define cputime_halve(__a) ((__a) >> 1)
55#define cputime_eq(__a, __b) ((__a) == (__b))
56#define cputime_gt(__a, __b) ((__a) > (__b))
57#define cputime_ge(__a, __b) ((__a) >= (__b))
58#define cputime_lt(__a, __b) ((__a) < (__b))
59#define cputime_le(__a, __b) ((__a) <= (__b))
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010060#define cputime_to_jiffies(__ct) (__div((__ct), 4096000000ULL / HZ))
Michael Neuling06b8e872008-02-06 01:36:12 -080061#define cputime_to_scaled(__ct) (__ct)
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010062#define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (4096000000ULL / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define cputime64_zero (0ULL)
65#define cputime64_add(__a, __b) ((__a) + (__b))
66#define cputime_to_cputime64(__ct) (__ct)
67
68static inline u64
69cputime64_to_jiffies64(cputime64_t cputime)
70{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010071 do_div(cputime, 4096000000ULL / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return cputime;
73}
74
75/*
Michael Holzheud57af9b2010-10-27 15:34:45 -070076 * Convert cputime to microseconds and back.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78static inline unsigned int
Michael Holzheud57af9b2010-10-27 15:34:45 -070079cputime_to_usecs(const cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Michael Holzheud57af9b2010-10-27 15:34:45 -070081 return cputime_div(cputime, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static inline cputime_t
Michael Holzheud57af9b2010-10-27 15:34:45 -070085usecs_to_cputime(const unsigned int m)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Michael Holzheud57af9b2010-10-27 15:34:45 -070087 return (cputime_t) m * 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * Convert cputime to milliseconds and back.
92 */
93static inline unsigned int
94cputime_to_secs(const cputime_t cputime)
95{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010096 return __div(cputime, 2048000000) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static inline cputime_t
100secs_to_cputime(const unsigned int s)
101{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100102 return (cputime_t) s * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * Convert cputime to timespec and back.
107 */
108static inline cputime_t
109timespec_to_cputime(const struct timespec *value)
110{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100111 return value->tv_nsec * 4096 / 1000 + (u64) value->tv_sec * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static inline void
115cputime_to_timespec(const cputime_t cputime, struct timespec *value)
116{
117#ifndef __s390x__
118 register_pair rp;
119
120 rp.pair = cputime >> 1;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100121 asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
122 value->tv_nsec = rp.subreg.even * 1000 / 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 value->tv_sec = rp.subreg.odd;
124#else
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100125 value->tv_nsec = (cputime % 4096000000ULL) * 1000 / 4096;
126 value->tv_sec = cputime / 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif
128}
129
130/*
131 * Convert cputime to timeval and back.
132 * Since cputime and timeval have the same resolution (microseconds)
133 * this is easy.
134 */
135static inline cputime_t
136timeval_to_cputime(const struct timeval *value)
137{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100138 return value->tv_usec * 4096 + (u64) value->tv_sec * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static inline void
142cputime_to_timeval(const cputime_t cputime, struct timeval *value)
143{
144#ifndef __s390x__
145 register_pair rp;
146
147 rp.pair = cputime >> 1;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100148 asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
149 value->tv_usec = rp.subreg.even / 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 value->tv_sec = rp.subreg.odd;
151#else
Christian Borntraegerd5cd0342009-02-19 15:19:00 +0100152 value->tv_usec = (cputime % 4096000000ULL) / 4096;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100153 value->tv_sec = cputime / 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
155}
156
157/*
158 * Convert cputime to clock and back.
159 */
160static inline clock_t
161cputime_to_clock_t(cputime_t cputime)
162{
Martin Schwidefsky70f5dc52009-10-29 15:04:12 +0100163 return cputime_div(cputime, 4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static inline cputime_t
167clock_t_to_cputime(unsigned long x)
168{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100169 return (cputime_t) x * (4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * Convert cputime64 to clock.
174 */
175static inline clock_t
176cputime64_to_clock_t(cputime64_t cputime)
177{
Martin Schwidefsky70f5dc52009-10-29 15:04:12 +0100178 return cputime_div(cputime, 4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200181struct s390_idle_data {
Martin Schwidefskye98bbaa2009-06-22 12:08:20 +0200182 unsigned int sequence;
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200183 unsigned long long idle_count;
184 unsigned long long idle_enter;
185 unsigned long long idle_time;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200186 int nohz_delay;
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200187};
188
189DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
190
Martin Schwidefsky63779812010-05-17 10:00:03 +0200191void vtime_start_cpu(__u64 int_clock, __u64 enter_timer);
Martin Schwidefskye1c80532009-04-23 13:58:08 +0200192cputime64_t s390_get_idle_time(int cpu);
193
194#define arch_idle_time(cpu) s390_get_idle_time(cpu)
195
Martin Schwidefsky63779812010-05-17 10:00:03 +0200196static inline void s390_idle_check(struct pt_regs *regs, __u64 int_clock,
197 __u64 enter_timer)
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200198{
Martin Schwidefsky63779812010-05-17 10:00:03 +0200199 if (regs->psw.mask & PSW_MASK_WAIT)
200 vtime_start_cpu(int_clock, enter_timer);
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200201}
202
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200203static inline int s390_nohz_delay(int cpu)
204{
Heiko Carstens37e89522011-01-05 12:47:55 +0100205 return __get_cpu_var(s390_idle).nohz_delay != 0;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200206}
207
208#define arch_needs_cpu(cpu) s390_nohz_delay(cpu)
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#endif /* _S390_CPUTIME_H */