Commit | Line | Data |
---|---|---|
c6622f63 PM |
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 | * | |
abf917cd | 11 | * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in |
c6622f63 PM |
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 | ||
abf917cd | 19 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE |
88999ceb | 20 | #include <asm-generic/cputime.h> |
a42548a1 SG |
21 | #ifdef __KERNEL__ |
22 | static inline void setup_cputime_one_jiffy(void) { } | |
23 | #endif | |
c6622f63 PM |
24 | #else |
25 | ||
26 | #include <linux/types.h> | |
27 | #include <linux/time.h> | |
28 | #include <asm/div64.h> | |
29 | #include <asm/time.h> | |
30 | #include <asm/param.h> | |
31 | ||
64861634 MS |
32 | typedef u64 __nocast cputime_t; |
33 | typedef u64 __nocast cputime64_t; | |
c6622f63 | 34 | |
347abad9 RR |
35 | #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new) |
36 | ||
c6622f63 PM |
37 | #ifdef __KERNEL__ |
38 | ||
a42548a1 SG |
39 | /* |
40 | * One jiffy in timebase units computed during initialization | |
41 | */ | |
42 | extern cputime_t cputime_one_jiffy; | |
43 | ||
c6622f63 PM |
44 | /* |
45 | * Convert cputime <-> jiffies | |
46 | */ | |
47 | extern u64 __cputime_jiffies_factor; | |
06b8e878 MN |
48 | DECLARE_PER_CPU(unsigned long, cputime_last_delta); |
49 | DECLARE_PER_CPU(unsigned long, cputime_scaled_last_delta); | |
c6622f63 PM |
50 | |
51 | static inline unsigned long cputime_to_jiffies(const cputime_t ct) | |
52 | { | |
64861634 | 53 | return mulhdu((__force u64) ct, __cputime_jiffies_factor); |
c6622f63 PM |
54 | } |
55 | ||
06b8e878 MN |
56 | /* Estimate the scaled cputime by scaling the real cputime based on |
57 | * the last scaled to real ratio */ | |
58 | static inline cputime_t cputime_to_scaled(const cputime_t ct) | |
59 | { | |
60 | if (cpu_has_feature(CPU_FTR_SPURR) && | |
2999a4b3 | 61 | __this_cpu_read(cputime_last_delta)) |
64861634 | 62 | return (__force u64) ct * |
2999a4b3 CL |
63 | __this_cpu_read(cputime_scaled_last_delta) / |
64 | __this_cpu_read(cputime_last_delta); | |
06b8e878 MN |
65 | return ct; |
66 | } | |
67 | ||
c6622f63 PM |
68 | static inline cputime_t jiffies_to_cputime(const unsigned long jif) |
69 | { | |
64861634 | 70 | u64 ct; |
c6622f63 PM |
71 | unsigned long sec; |
72 | ||
73 | /* have to be a little careful about overflow */ | |
74 | ct = jif % HZ; | |
75 | sec = jif / HZ; | |
76 | if (ct) { | |
77 | ct *= tb_ticks_per_sec; | |
78 | do_div(ct, HZ); | |
79 | } | |
80 | if (sec) | |
81 | ct += (cputime_t) sec * tb_ticks_per_sec; | |
64861634 | 82 | return (__force cputime_t) ct; |
c6622f63 PM |
83 | } |
84 | ||
a42548a1 SG |
85 | static inline void setup_cputime_one_jiffy(void) |
86 | { | |
87 | cputime_one_jiffy = jiffies_to_cputime(1); | |
88 | } | |
89 | ||
a8e0c51c DW |
90 | static inline cputime64_t jiffies64_to_cputime64(const u64 jif) |
91 | { | |
64861634 | 92 | u64 ct; |
c223c903 | 93 | u64 sec = jif; |
a8e0c51c DW |
94 | |
95 | /* have to be a little careful about overflow */ | |
c223c903 | 96 | ct = do_div(sec, HZ); |
a8e0c51c DW |
97 | if (ct) { |
98 | ct *= tb_ticks_per_sec; | |
99 | do_div(ct, HZ); | |
100 | } | |
101 | if (sec) | |
64861634 MS |
102 | ct += (u64) sec * tb_ticks_per_sec; |
103 | return (__force cputime64_t) ct; | |
a8e0c51c DW |
104 | } |
105 | ||
c6622f63 PM |
106 | static inline u64 cputime64_to_jiffies64(const cputime_t ct) |
107 | { | |
64861634 | 108 | return mulhdu((__force u64) ct, __cputime_jiffies_factor); |
c6622f63 PM |
109 | } |
110 | ||
111 | /* | |
d57af9b2 | 112 | * Convert cputime <-> microseconds |
c6622f63 | 113 | */ |
9f5072d4 | 114 | extern u64 __cputime_usec_factor; |
c6622f63 | 115 | |
d57af9b2 | 116 | static inline unsigned long cputime_to_usecs(const cputime_t ct) |
c6622f63 | 117 | { |
e4e88f31 | 118 | return mulhdu((__force u64) ct, __cputime_usec_factor); |
c6622f63 PM |
119 | } |
120 | ||
d57af9b2 | 121 | static inline cputime_t usecs_to_cputime(const unsigned long us) |
c6622f63 | 122 | { |
64861634 | 123 | u64 ct; |
c6622f63 PM |
124 | unsigned long sec; |
125 | ||
126 | /* have to be a little careful about overflow */ | |
d57af9b2 MH |
127 | ct = us % 1000000; |
128 | sec = us / 1000000; | |
c6622f63 PM |
129 | if (ct) { |
130 | ct *= tb_ticks_per_sec; | |
9f5072d4 | 131 | do_div(ct, 1000000); |
c6622f63 PM |
132 | } |
133 | if (sec) | |
134 | ct += (cputime_t) sec * tb_ticks_per_sec; | |
64861634 | 135 | return (__force cputime_t) ct; |
c6622f63 PM |
136 | } |
137 | ||
34845636 AS |
138 | #define usecs_to_cputime64(us) usecs_to_cputime(us) |
139 | ||
c6622f63 PM |
140 | /* |
141 | * Convert cputime <-> seconds | |
142 | */ | |
143 | extern u64 __cputime_sec_factor; | |
144 | ||
145 | static inline unsigned long cputime_to_secs(const cputime_t ct) | |
146 | { | |
64861634 | 147 | return mulhdu((__force u64) ct, __cputime_sec_factor); |
c6622f63 PM |
148 | } |
149 | ||
150 | static inline cputime_t secs_to_cputime(const unsigned long sec) | |
151 | { | |
64861634 | 152 | return (__force cputime_t)((u64) sec * tb_ticks_per_sec); |
c6622f63 PM |
153 | } |
154 | ||
155 | /* | |
156 | * Convert cputime <-> timespec | |
157 | */ | |
158 | static inline void cputime_to_timespec(const cputime_t ct, struct timespec *p) | |
159 | { | |
64861634 | 160 | u64 x = (__force u64) ct; |
c6622f63 PM |
161 | unsigned int frac; |
162 | ||
163 | frac = do_div(x, tb_ticks_per_sec); | |
164 | p->tv_sec = x; | |
165 | x = (u64) frac * 1000000000; | |
166 | do_div(x, tb_ticks_per_sec); | |
167 | p->tv_nsec = x; | |
168 | } | |
169 | ||
170 | static inline cputime_t timespec_to_cputime(const struct timespec *p) | |
171 | { | |
64861634 | 172 | u64 ct; |
c6622f63 PM |
173 | |
174 | ct = (u64) p->tv_nsec * tb_ticks_per_sec; | |
175 | do_div(ct, 1000000000); | |
64861634 | 176 | return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec); |
c6622f63 PM |
177 | } |
178 | ||
179 | /* | |
180 | * Convert cputime <-> timeval | |
181 | */ | |
182 | static inline void cputime_to_timeval(const cputime_t ct, struct timeval *p) | |
183 | { | |
64861634 | 184 | u64 x = (__force u64) ct; |
c6622f63 PM |
185 | unsigned int frac; |
186 | ||
187 | frac = do_div(x, tb_ticks_per_sec); | |
188 | p->tv_sec = x; | |
189 | x = (u64) frac * 1000000; | |
190 | do_div(x, tb_ticks_per_sec); | |
191 | p->tv_usec = x; | |
192 | } | |
193 | ||
194 | static inline cputime_t timeval_to_cputime(const struct timeval *p) | |
195 | { | |
64861634 | 196 | u64 ct; |
c6622f63 PM |
197 | |
198 | ct = (u64) p->tv_usec * tb_ticks_per_sec; | |
199 | do_div(ct, 1000000); | |
64861634 | 200 | return (__force cputime_t)(ct + (u64) p->tv_sec * tb_ticks_per_sec); |
c6622f63 PM |
201 | } |
202 | ||
203 | /* | |
204 | * Convert cputime <-> clock_t (units of 1/USER_HZ seconds) | |
205 | */ | |
206 | extern u64 __cputime_clockt_factor; | |
207 | ||
208 | static inline unsigned long cputime_to_clock_t(const cputime_t ct) | |
209 | { | |
64861634 | 210 | return mulhdu((__force u64) ct, __cputime_clockt_factor); |
c6622f63 PM |
211 | } |
212 | ||
213 | static inline cputime_t clock_t_to_cputime(const unsigned long clk) | |
214 | { | |
64861634 | 215 | u64 ct; |
c6622f63 PM |
216 | unsigned long sec; |
217 | ||
218 | /* have to be a little careful about overflow */ | |
219 | ct = clk % USER_HZ; | |
220 | sec = clk / USER_HZ; | |
221 | if (ct) { | |
222 | ct *= tb_ticks_per_sec; | |
223 | do_div(ct, USER_HZ); | |
224 | } | |
225 | if (sec) | |
64861634 MS |
226 | ct += (u64) sec * tb_ticks_per_sec; |
227 | return (__force cputime_t) ct; | |
c6622f63 PM |
228 | } |
229 | ||
230 | #define cputime64_to_clock_t(ct) cputime_to_clock_t((cputime_t)(ct)) | |
231 | ||
c223c903 CL |
232 | /* |
233 | * PPC64 uses PACA which is task independent for storing accounting data while | |
234 | * PPC32 uses struct thread_info, therefore at task switch the accounting data | |
235 | * has to be populated in the new task | |
236 | */ | |
237 | #ifdef CONFIG_PPC64 | |
e3942ba0 | 238 | static inline void arch_vtime_task_switch(struct task_struct *tsk) { } |
c223c903 CL |
239 | #else |
240 | void arch_vtime_task_switch(struct task_struct *tsk); | |
241 | #endif | |
e3942ba0 | 242 | |
c6622f63 | 243 | #endif /* __KERNEL__ */ |
abf917cd | 244 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ |
c6622f63 | 245 | #endif /* __POWERPC_CPUTIME_H */ |