xref: /openbmc/linux/arch/s390/include/asm/cputime.h (revision fd589a8f)
1 /*
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 
12 #include <linux/types.h>
13 #include <linux/percpu.h>
14 #include <linux/spinlock.h>
15 #include <asm/div64.h>
16 
17 /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
18 
19 typedef unsigned long long cputime_t;
20 typedef unsigned long long cputime64_t;
21 
22 #ifndef __s390x__
23 
24 static 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 
36 static 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))
59 #define cputime_to_jiffies(__ct)	(__div((__ct), 4096000000ULL / HZ))
60 #define cputime_to_scaled(__ct)		(__ct)
61 #define jiffies_to_cputime(__hz)	((cputime_t)(__hz) * (4096000000ULL / HZ))
62 
63 #define cputime64_zero			(0ULL)
64 #define cputime64_add(__a, __b)		((__a) + (__b))
65 #define cputime_to_cputime64(__ct)	(__ct)
66 
67 static inline u64
68 cputime64_to_jiffies64(cputime64_t cputime)
69 {
70 	do_div(cputime, 4096000000ULL / HZ);
71 	return cputime;
72 }
73 
74 /*
75  * Convert cputime to milliseconds and back.
76  */
77 static inline unsigned int
78 cputime_to_msecs(const cputime_t cputime)
79 {
80 	return __div(cputime, 4096000);
81 }
82 
83 static inline cputime_t
84 msecs_to_cputime(const unsigned int m)
85 {
86 	return (cputime_t) m * 4096000;
87 }
88 
89 /*
90  * Convert cputime to milliseconds and back.
91  */
92 static inline unsigned int
93 cputime_to_secs(const cputime_t cputime)
94 {
95 	return __div(cputime, 2048000000) >> 1;
96 }
97 
98 static inline cputime_t
99 secs_to_cputime(const unsigned int s)
100 {
101 	return (cputime_t) s * 4096000000ULL;
102 }
103 
104 /*
105  * Convert cputime to timespec and back.
106  */
107 static inline cputime_t
108 timespec_to_cputime(const struct timespec *value)
109 {
110 	return value->tv_nsec * 4096 / 1000 + (u64) value->tv_sec * 4096000000ULL;
111 }
112 
113 static inline void
114 cputime_to_timespec(const cputime_t cputime, struct timespec *value)
115 {
116 #ifndef __s390x__
117 	register_pair rp;
118 
119 	rp.pair = cputime >> 1;
120 	asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
121 	value->tv_nsec = rp.subreg.even * 1000 / 4096;
122 	value->tv_sec = rp.subreg.odd;
123 #else
124 	value->tv_nsec = (cputime % 4096000000ULL) * 1000 / 4096;
125 	value->tv_sec = cputime / 4096000000ULL;
126 #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  */
134 static inline cputime_t
135 timeval_to_cputime(const struct timeval *value)
136 {
137 	return value->tv_usec * 4096 + (u64) value->tv_sec * 4096000000ULL;
138 }
139 
140 static inline void
141 cputime_to_timeval(const cputime_t cputime, struct timeval *value)
142 {
143 #ifndef __s390x__
144 	register_pair rp;
145 
146 	rp.pair = cputime >> 1;
147 	asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
148 	value->tv_usec = rp.subreg.even / 4096;
149 	value->tv_sec = rp.subreg.odd;
150 #else
151 	value->tv_usec = (cputime % 4096000000ULL) / 4096;
152 	value->tv_sec = cputime / 4096000000ULL;
153 #endif
154 }
155 
156 /*
157  * Convert cputime to clock and back.
158  */
159 static inline clock_t
160 cputime_to_clock_t(cputime_t cputime)
161 {
162 	return __div(cputime, 4096000000ULL / USER_HZ);
163 }
164 
165 static inline cputime_t
166 clock_t_to_cputime(unsigned long x)
167 {
168 	return (cputime_t) x * (4096000000ULL / USER_HZ);
169 }
170 
171 /*
172  * Convert cputime64 to clock.
173  */
174 static inline clock_t
175 cputime64_to_clock_t(cputime64_t cputime)
176 {
177        return __div(cputime, 4096000000ULL / USER_HZ);
178 }
179 
180 struct s390_idle_data {
181 	unsigned int sequence;
182 	unsigned long long idle_count;
183 	unsigned long long idle_enter;
184 	unsigned long long idle_time;
185 };
186 
187 DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
188 
189 void vtime_start_cpu(void);
190 cputime64_t s390_get_idle_time(int cpu);
191 
192 #define arch_idle_time(cpu) s390_get_idle_time(cpu)
193 
194 static inline void s390_idle_check(void)
195 {
196 	if ((&__get_cpu_var(s390_idle))->idle_enter != 0ULL)
197 		vtime_start_cpu();
198 }
199 
200 #endif /* _S390_CPUTIME_H */
201