blob: ec917d42ee6d2b18264e26fe6407d2ced5da17ed [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)
45#define cputime_max ((~0UL >> 1) - 1)
46#define cputime_add(__a, __b) ((__a) + (__b))
47#define cputime_sub(__a, __b) ((__a) - (__b))
48#define cputime_div(__a, __n) ({ \
49 unsigned long long __div = (__a); \
50 do_div(__div,__n); \
51 __div; \
52})
53#define cputime_halve(__a) ((__a) >> 1)
54#define cputime_eq(__a, __b) ((__a) == (__b))
55#define cputime_gt(__a, __b) ((__a) > (__b))
56#define cputime_ge(__a, __b) ((__a) >= (__b))
57#define cputime_lt(__a, __b) ((__a) < (__b))
58#define cputime_le(__a, __b) ((__a) <= (__b))
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010059#define cputime_to_jiffies(__ct) (__div((__ct), 4096000000ULL / HZ))
Michael Neuling06b8e872008-02-06 01:36:12 -080060#define cputime_to_scaled(__ct) (__ct)
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010061#define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (4096000000ULL / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define cputime64_zero (0ULL)
64#define cputime64_add(__a, __b) ((__a) + (__b))
65#define cputime_to_cputime64(__ct) (__ct)
66
67static inline u64
68cputime64_to_jiffies64(cputime64_t cputime)
69{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010070 do_div(cputime, 4096000000ULL / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return cputime;
72}
73
74/*
75 * Convert cputime to milliseconds and back.
76 */
77static inline unsigned int
78cputime_to_msecs(const cputime_t cputime)
79{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010080 return __div(cputime, 4096000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static inline cputime_t
84msecs_to_cputime(const unsigned int m)
85{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010086 return (cputime_t) m * 4096000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * Convert cputime to milliseconds and back.
91 */
92static inline unsigned int
93cputime_to_secs(const cputime_t cputime)
94{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +010095 return __div(cputime, 2048000000) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static inline cputime_t
99secs_to_cputime(const unsigned int s)
100{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100101 return (cputime_t) s * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/*
105 * Convert cputime to timespec and back.
106 */
107static inline cputime_t
108timespec_to_cputime(const struct timespec *value)
109{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100110 return value->tv_nsec * 4096 / 1000 + (u64) value->tv_sec * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static inline void
114cputime_to_timespec(const cputime_t cputime, struct timespec *value)
115{
116#ifndef __s390x__
117 register_pair rp;
118
119 rp.pair = cputime >> 1;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100120 asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
121 value->tv_nsec = rp.subreg.even * 1000 / 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 value->tv_sec = rp.subreg.odd;
123#else
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100124 value->tv_nsec = (cputime % 4096000000ULL) * 1000 / 4096;
125 value->tv_sec = cputime / 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif
127}
128
129/*
130 * Convert cputime to timeval and back.
131 * Since cputime and timeval have the same resolution (microseconds)
132 * this is easy.
133 */
134static inline cputime_t
135timeval_to_cputime(const struct timeval *value)
136{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100137 return value->tv_usec * 4096 + (u64) value->tv_sec * 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static inline void
141cputime_to_timeval(const cputime_t cputime, struct timeval *value)
142{
143#ifndef __s390x__
144 register_pair rp;
145
146 rp.pair = cputime >> 1;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100147 asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
148 value->tv_usec = rp.subreg.even / 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 value->tv_sec = rp.subreg.odd;
150#else
Christian Borntraegerd5cd0342009-02-19 15:19:00 +0100151 value->tv_usec = (cputime % 4096000000ULL) / 4096;
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100152 value->tv_sec = cputime / 4096000000ULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif
154}
155
156/*
157 * Convert cputime to clock and back.
158 */
159static inline clock_t
160cputime_to_clock_t(cputime_t cputime)
161{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100162 return __div(cputime, 4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static inline cputime_t
166clock_t_to_cputime(unsigned long x)
167{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100168 return (cputime_t) x * (4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
172 * Convert cputime64 to clock.
173 */
174static inline clock_t
175cputime64_to_clock_t(cputime64_t cputime)
176{
Martin Schwidefskyaa5e97c2008-12-31 15:11:39 +0100177 return __div(cputime, 4096000000ULL / USER_HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200180struct s390_idle_data {
181 spinlock_t lock;
182 unsigned long long idle_count;
183 unsigned long long idle_enter;
184 unsigned long long idle_time;
185};
186
187DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
188
189void vtime_start_cpu(void);
Martin Schwidefskye1c80532009-04-23 13:58:08 +0200190cputime64_t s390_get_idle_time(int cpu);
191
192#define arch_idle_time(cpu) s390_get_idle_time(cpu)
193
Martin Schwidefsky76d4e002009-06-12 10:26:21 +0200194static inline void s390_idle_check(void)
195{
196 if ((&__get_cpu_var(s390_idle))->idle_enter != 0ULL)
197 vtime_start_cpu();
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif /* _S390_CPUTIME_H */