1/*
2 *  Copyright IBM Corp. 2004
3 *
4 *  Author: Martin Schwidefsky <schwidefsky@de.ibm.com>
5 */
6
7#ifndef _S390_CPUTIME_H
8#define _S390_CPUTIME_H
9
10#include <linux/types.h>
11#include <asm/div64.h>
12
13#define CPUTIME_PER_USEC 4096ULL
14#define CPUTIME_PER_SEC (CPUTIME_PER_USEC * USEC_PER_SEC)
15
16/* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
17
18typedef unsigned long long __nocast cputime_t;
19typedef unsigned long long __nocast cputime64_t;
20
21#define cmpxchg_cputime(ptr, old, new) cmpxchg64(ptr, old, new)
22
23static inline unsigned long __div(unsigned long long n, unsigned long base)
24{
25	return n / base;
26}
27
28#define cputime_one_jiffy		jiffies_to_cputime(1)
29
30/*
31 * Convert cputime to jiffies and back.
32 */
33static inline unsigned long cputime_to_jiffies(const cputime_t cputime)
34{
35	return __div((__force unsigned long long) cputime, CPUTIME_PER_SEC / HZ);
36}
37
38static inline cputime_t jiffies_to_cputime(const unsigned int jif)
39{
40	return (__force cputime_t)(jif * (CPUTIME_PER_SEC / HZ));
41}
42
43static inline u64 cputime64_to_jiffies64(cputime64_t cputime)
44{
45	unsigned long long jif = (__force unsigned long long) cputime;
46	do_div(jif, CPUTIME_PER_SEC / HZ);
47	return jif;
48}
49
50static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
51{
52	return (__force cputime64_t)(jif * (CPUTIME_PER_SEC / HZ));
53}
54
55/*
56 * Convert cputime to microseconds and back.
57 */
58static inline unsigned int cputime_to_usecs(const cputime_t cputime)
59{
60	return (__force unsigned long long) cputime >> 12;
61}
62
63static inline cputime_t usecs_to_cputime(const unsigned int m)
64{
65	return (__force cputime_t)(m * CPUTIME_PER_USEC);
66}
67
68#define usecs_to_cputime64(m)		usecs_to_cputime(m)
69
70/*
71 * Convert cputime to milliseconds and back.
72 */
73static inline unsigned int cputime_to_secs(const cputime_t cputime)
74{
75	return __div((__force unsigned long long) cputime, CPUTIME_PER_SEC / 2) >> 1;
76}
77
78static inline cputime_t secs_to_cputime(const unsigned int s)
79{
80	return (__force cputime_t)(s * CPUTIME_PER_SEC);
81}
82
83/*
84 * Convert cputime to timespec and back.
85 */
86static inline cputime_t timespec_to_cputime(const struct timespec *value)
87{
88	unsigned long long ret = value->tv_sec * CPUTIME_PER_SEC;
89	return (__force cputime_t)(ret + __div(value->tv_nsec * CPUTIME_PER_USEC, NSEC_PER_USEC));
90}
91
92static inline void cputime_to_timespec(const cputime_t cputime,
93				       struct timespec *value)
94{
95	unsigned long long __cputime = (__force unsigned long long) cputime;
96	value->tv_nsec = (__cputime % CPUTIME_PER_SEC) * NSEC_PER_USEC / CPUTIME_PER_USEC;
97	value->tv_sec = __cputime / CPUTIME_PER_SEC;
98}
99
100/*
101 * Convert cputime to timeval and back.
102 * Since cputime and timeval have the same resolution (microseconds)
103 * this is easy.
104 */
105static inline cputime_t timeval_to_cputime(const struct timeval *value)
106{
107	unsigned long long ret = value->tv_sec * CPUTIME_PER_SEC;
108	return (__force cputime_t)(ret + value->tv_usec * CPUTIME_PER_USEC);
109}
110
111static inline void cputime_to_timeval(const cputime_t cputime,
112				      struct timeval *value)
113{
114	unsigned long long __cputime = (__force unsigned long long) cputime;
115	value->tv_usec = (__cputime % CPUTIME_PER_SEC) / CPUTIME_PER_USEC;
116	value->tv_sec = __cputime / CPUTIME_PER_SEC;
117}
118
119/*
120 * Convert cputime to clock and back.
121 */
122static inline clock_t cputime_to_clock_t(cputime_t cputime)
123{
124	unsigned long long clock = (__force unsigned long long) cputime;
125	do_div(clock, CPUTIME_PER_SEC / USER_HZ);
126	return clock;
127}
128
129static inline cputime_t clock_t_to_cputime(unsigned long x)
130{
131	return (__force cputime_t)(x * (CPUTIME_PER_SEC / USER_HZ));
132}
133
134/*
135 * Convert cputime64 to clock.
136 */
137static inline clock_t cputime64_to_clock_t(cputime64_t cputime)
138{
139	unsigned long long clock = (__force unsigned long long) cputime;
140	do_div(clock, CPUTIME_PER_SEC / USER_HZ);
141	return clock;
142}
143
144cputime64_t arch_cpu_idle_time(int cpu);
145
146#define arch_idle_time(cpu) arch_cpu_idle_time(cpu)
147
148#endif /* _S390_CPUTIME_H */
149