Merge git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-drm-fsl-dcu.git] / include / asm-powerpc / cputime.h
1 /*
2  * Definitions for measuring cputime on powerpc machines.
3  *
4  * Copyright (C) 2006 Paul Mackerras, IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  * If we have CONFIG_VIRT_CPU_ACCOUNTING, we measure cpu time in
12  * the same units as the timebase.  Otherwise we measure cpu time
13  * in jiffies using the generic definitions.
14  */
15
16 #ifndef __POWERPC_CPUTIME_H
17 #define __POWERPC_CPUTIME_H
18
19 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
20 #include <asm-generic/cputime.h>
21 #else
22
23 #include <linux/types.h>
24 #include <linux/time.h>
25 #include <asm/div64.h>
26 #include <asm/time.h>
27 #include <asm/param.h>
28
29 typedef u64 cputime_t;
30 typedef u64 cputime64_t;
31
32 #define cputime_zero                    ((cputime_t)0)
33 #define cputime_max                     ((~((cputime_t)0) >> 1) - 1)
34 #define cputime_add(__a, __b)           ((__a) +  (__b))
35 #define cputime_sub(__a, __b)           ((__a) -  (__b))
36 #define cputime_div(__a, __n)           ((__a) /  (__n))
37 #define cputime_halve(__a)              ((__a) >> 1)
38 #define cputime_eq(__a, __b)            ((__a) == (__b))
39 #define cputime_gt(__a, __b)            ((__a) >  (__b))
40 #define cputime_ge(__a, __b)            ((__a) >= (__b))
41 #define cputime_lt(__a, __b)            ((__a) <  (__b))
42 #define cputime_le(__a, __b)            ((__a) <= (__b))
43
44 #define cputime64_zero                  ((cputime64_t)0)
45 #define cputime64_add(__a, __b)         ((__a) + (__b))
46 #define cputime64_sub(__a, __b)         ((__a) - (__b))
47 #define cputime_to_cputime64(__ct)      (__ct)
48
49 #ifdef __KERNEL__
50
51 /*
52  * Convert cputime <-> jiffies
53  */
54 extern u64 __cputime_jiffies_factor;
55
56 static inline unsigned long cputime_to_jiffies(const cputime_t ct)
57 {
58         return mulhdu(ct, __cputime_jiffies_factor);
59 }
60
61 static inline cputime_t jiffies_to_cputime(const unsigned long jif)
62 {
63         cputime_t ct;
64         unsigned long sec;
65
66         /* have to be a little careful about overflow */
67         ct = jif % HZ;
68         sec = jif / HZ;
69         if (ct) {
70                 ct *= tb_ticks_per_sec;
71                 do_div(ct, HZ);
72         }
73         if (sec)
74                 ct += (cputime_t) sec * tb_ticks_per_sec;
75         return ct;
76 }
77
78 static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
79 {
80         cputime_t ct;
81         u64 sec;
82
83         /* have to be a little careful about overflow */
84         ct = jif % HZ;
85         sec = jif / HZ;
86         if (ct) {
87                 ct *= tb_ticks_per_sec;
88                 do_div(ct, HZ);
89         }
90         if (sec)
91                 ct += (cputime_t) sec * tb_ticks_per_sec;
92         return ct;
93 }
94
95 static inline u64 cputime64_to_jiffies64(const cputime_t ct)
96 {
97         return mulhdu(ct, __cputime_jiffies_factor);
98 }
99
100 /*
101  * Convert cputime <-> milliseconds
102  */
103 extern u64 __cputime_msec_factor;
104
105 static inline unsigned long cputime_to_msecs(const cputime_t ct)
106 {
107         return mulhdu(ct, __cputime_msec_factor);
108 }
109
110 static inline cputime_t msecs_to_cputime(const unsigned long ms)
111 {
112         cputime_t ct;
113         unsigned long sec;
114
115         /* have to be a little careful about overflow */
116         ct = ms % 1000;
117         sec = ms / 1000;
118         if (ct) {
119                 ct *= tb_ticks_per_sec;
120                 do_div(ct, 1000);
121         }
122         if (sec)
123                 ct += (cputime_t) sec * tb_ticks_per_sec;
124         return ct;
125 }
126
127 /*
128  * Convert cputime <-> seconds
129  */
130 extern u64 __cputime_sec_factor;
131
132 static inline unsigned long cputime_to_secs(const cputime_t ct)
133 {
134         return mulhdu(ct, __cputime_sec_factor);
135 }
136
137 static inline cputime_t secs_to_cputime(const unsigned long sec)
138 {
139         return (cputime_t) sec * tb_ticks_per_sec;
140 }
141
142 /*
143  * Convert cputime <-> timespec
144  */
145 static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p)
146 {
147         u64 x = ct;
148         unsigned int frac;
149
150         frac = do_div(x, tb_ticks_per_sec);
151         p->tv_sec = x;
152         x = (u64) frac * 1000000000;
153         do_div(x, tb_ticks_per_sec);
154         p->tv_nsec = x;
155 }
156
157 static inline cputime_t timespec_to_cputime(const struct timespec *p)
158 {
159         cputime_t ct;
160
161         ct = (u64) p->tv_nsec * tb_ticks_per_sec;
162         do_div(ct, 1000000000);
163         return ct + (u64) p->tv_sec * tb_ticks_per_sec;
164 }
165
166 /*
167  * Convert cputime <-> timeval
168  */
169 static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p)
170 {
171         u64 x = ct;
172         unsigned int frac;
173
174         frac = do_div(x, tb_ticks_per_sec);
175         p->tv_sec = x;
176         x = (u64) frac * 1000000;
177         do_div(x, tb_ticks_per_sec);
178         p->tv_usec = x;
179 }
180
181 static inline cputime_t timeval_to_cputime(const struct timeval *p)
182 {
183         cputime_t ct;
184
185         ct = (u64) p->tv_usec * tb_ticks_per_sec;
186         do_div(ct, 1000000);
187         return ct + (u64) p->tv_sec * tb_ticks_per_sec;
188 }
189
190 /*
191  * Convert cputime <-> clock_t (units of 1/USER_HZ seconds)
192  */
193 extern u64 __cputime_clockt_factor;
194
195 static inline unsigned long cputime_to_clock_t(const cputime_t ct)
196 {
197         return mulhdu(ct, __cputime_clockt_factor);
198 }
199
200 static inline cputime_t clock_t_to_cputime(const unsigned long clk)
201 {
202         cputime_t ct;
203         unsigned long sec;
204
205         /* have to be a little careful about overflow */
206         ct = clk % USER_HZ;
207         sec = clk / USER_HZ;
208         if (ct) {
209                 ct *= tb_ticks_per_sec;
210                 do_div(ct, USER_HZ);
211         }
212         if (sec)
213                 ct += (cputime_t) sec * tb_ticks_per_sec;
214         return ct;
215 }
216
217 #define cputime64_to_clock_t(ct)        cputime_to_clock_t((cputime_t)(ct))
218
219 #endif /* __KERNEL__ */
220 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
221 #endif /* __POWERPC_CPUTIME_H */