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