blob: 4b3ef7cad11583bf580a6539340e85c4f8e2c5d9 [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
12#include <asm/div64.h>
13
14/* We want to use micro-second resolution. */
15
16typedef unsigned long long cputime_t;
17typedef unsigned long long cputime64_t;
18
19#ifndef __s390x__
20
21static inline unsigned int
22__div(unsigned long long n, unsigned int base)
23{
24 register_pair rp;
25
26 rp.pair = n >> 1;
27 asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1));
28 return rp.subreg.odd;
29}
30
31#else /* __s390x__ */
32
33static inline unsigned int
34__div(unsigned long long n, unsigned int base)
35{
36 return n / base;
37}
38
39#endif /* __s390x__ */
40
41#define cputime_zero (0ULL)
42#define cputime_max ((~0UL >> 1) - 1)
43#define cputime_add(__a, __b) ((__a) + (__b))
44#define cputime_sub(__a, __b) ((__a) - (__b))
45#define cputime_div(__a, __n) ({ \
46 unsigned long long __div = (__a); \
47 do_div(__div,__n); \
48 __div; \
49})
50#define cputime_halve(__a) ((__a) >> 1)
51#define cputime_eq(__a, __b) ((__a) == (__b))
52#define cputime_gt(__a, __b) ((__a) > (__b))
53#define cputime_ge(__a, __b) ((__a) >= (__b))
54#define cputime_lt(__a, __b) ((__a) < (__b))
55#define cputime_le(__a, __b) ((__a) <= (__b))
56#define cputime_to_jiffies(__ct) (__div((__ct), 1000000 / HZ))
57#define jiffies_to_cputime(__hz) ((cputime_t)(__hz) * (1000000 / HZ))
58
59#define cputime64_zero (0ULL)
60#define cputime64_add(__a, __b) ((__a) + (__b))
61#define cputime_to_cputime64(__ct) (__ct)
62
63static inline u64
64cputime64_to_jiffies64(cputime64_t cputime)
65{
66 do_div(cputime, 1000000 / HZ);
67 return cputime;
68}
69
70/*
71 * Convert cputime to milliseconds and back.
72 */
73static inline unsigned int
74cputime_to_msecs(const cputime_t cputime)
75{
76 return __div(cputime, 1000);
77}
78
79static inline cputime_t
80msecs_to_cputime(const unsigned int m)
81{
82 return (cputime_t) m * 1000;
83}
84
85/*
86 * Convert cputime to milliseconds and back.
87 */
88static inline unsigned int
89cputime_to_secs(const cputime_t cputime)
90{
91 return __div(cputime, 1000000);
92}
93
94static inline cputime_t
95secs_to_cputime(const unsigned int s)
96{
97 return (cputime_t) s * 1000000;
98}
99
100/*
101 * Convert cputime to timespec and back.
102 */
103static inline cputime_t
104timespec_to_cputime(const struct timespec *value)
105{
106 return value->tv_nsec / 1000 + (u64) value->tv_sec * 1000000;
107}
108
109static inline void
110cputime_to_timespec(const cputime_t cputime, struct timespec *value)
111{
112#ifndef __s390x__
113 register_pair rp;
114
115 rp.pair = cputime >> 1;
116 asm ("dr %0,%1" : "+d" (rp) : "d" (1000000 >> 1));
117 value->tv_nsec = rp.subreg.even * 1000;
118 value->tv_sec = rp.subreg.odd;
119#else
120 value->tv_nsec = (cputime % 1000000) * 1000;
121 value->tv_sec = cputime / 1000000;
122#endif
123}
124
125/*
126 * Convert cputime to timeval and back.
127 * Since cputime and timeval have the same resolution (microseconds)
128 * this is easy.
129 */
130static inline cputime_t
131timeval_to_cputime(const struct timeval *value)
132{
133 return value->tv_usec + (u64) value->tv_sec * 1000000;
134}
135
136static inline void
137cputime_to_timeval(const cputime_t cputime, struct timeval *value)
138{
139#ifndef __s390x__
140 register_pair rp;
141
142 rp.pair = cputime >> 1;
143 asm ("dr %0,%1" : "+d" (rp) : "d" (1000000 >> 1));
144 value->tv_usec = rp.subreg.even;
145 value->tv_sec = rp.subreg.odd;
146#else
147 value->tv_usec = cputime % 1000000;
148 value->tv_sec = cputime / 1000000;
149#endif
150}
151
152/*
153 * Convert cputime to clock and back.
154 */
155static inline clock_t
156cputime_to_clock_t(cputime_t cputime)
157{
158 return __div(cputime, 1000000 / USER_HZ);
159}
160
161static inline cputime_t
162clock_t_to_cputime(unsigned long x)
163{
164 return (cputime_t) x * (1000000 / USER_HZ);
165}
166
167/*
168 * Convert cputime64 to clock.
169 */
170static inline clock_t
171cputime64_to_clock_t(cputime64_t cputime)
172{
173 return __div(cputime, 1000000 / USER_HZ);
174}
175
176#endif /* _S390_CPUTIME_H */