xref: /openbmc/linux/arch/powerpc/include/asm/cputime.h (revision 8730046c)
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 
50 static inline unsigned long cputime_to_jiffies(const cputime_t ct)
51 {
52 	return mulhdu((__force u64) ct, __cputime_jiffies_factor);
53 }
54 
55 static inline cputime_t jiffies_to_cputime(const unsigned long jif)
56 {
57 	u64 ct;
58 	unsigned long sec;
59 
60 	/* have to be a little careful about overflow */
61 	ct = jif % HZ;
62 	sec = jif / HZ;
63 	if (ct) {
64 		ct *= tb_ticks_per_sec;
65 		do_div(ct, HZ);
66 	}
67 	if (sec)
68 		ct += (cputime_t) sec * tb_ticks_per_sec;
69 	return (__force cputime_t) ct;
70 }
71 
72 static inline void setup_cputime_one_jiffy(void)
73 {
74 	cputime_one_jiffy = jiffies_to_cputime(1);
75 }
76 
77 static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
78 {
79 	u64 ct;
80 	u64 sec = jif;
81 
82 	/* have to be a little careful about overflow */
83 	ct = do_div(sec, HZ);
84 	if (ct) {
85 		ct *= tb_ticks_per_sec;
86 		do_div(ct, HZ);
87 	}
88 	if (sec)
89 		ct += (u64) sec * tb_ticks_per_sec;
90 	return (__force cputime64_t) ct;
91 }
92 
93 static inline u64 cputime64_to_jiffies64(const cputime_t ct)
94 {
95 	return mulhdu((__force u64) ct, __cputime_jiffies_factor);
96 }
97 
98 /*
99  * Convert cputime <-> microseconds
100  */
101 extern u64 __cputime_usec_factor;
102 
103 static inline unsigned long cputime_to_usecs(const cputime_t ct)
104 {
105 	return mulhdu((__force u64) ct, __cputime_usec_factor);
106 }
107 
108 static inline cputime_t usecs_to_cputime(const unsigned long us)
109 {
110 	u64 ct;
111 	unsigned long sec;
112 
113 	/* have to be a little careful about overflow */
114 	ct = us % 1000000;
115 	sec = us / 1000000;
116 	if (ct) {
117 		ct *= tb_ticks_per_sec;
118 		do_div(ct, 1000000);
119 	}
120 	if (sec)
121 		ct += (cputime_t) sec * tb_ticks_per_sec;
122 	return (__force cputime_t) ct;
123 }
124 
125 #define usecs_to_cputime64(us)		usecs_to_cputime(us)
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((__force u64) ct, __cputime_sec_factor);
135 }
136 
137 static inline cputime_t secs_to_cputime(const unsigned long sec)
138 {
139 	return (__force cputime_t)((u64) 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 = (__force u64) 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 	u64 ct;
160 
161 	ct = (u64) p->tv_nsec * tb_ticks_per_sec;
162 	do_div(ct, 1000000000);
163 	return (__force cputime_t)(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 = (__force u64) 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 	u64 ct;
184 
185 	ct = (u64) p->tv_usec * tb_ticks_per_sec;
186 	do_div(ct, 1000000);
187 	return (__force cputime_t)(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((__force u64) ct, __cputime_clockt_factor);
198 }
199 
200 static inline cputime_t clock_t_to_cputime(const unsigned long clk)
201 {
202 	u64 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 += (u64) sec * tb_ticks_per_sec;
214 	return (__force cputime_t) ct;
215 }
216 
217 #define cputime64_to_clock_t(ct)	cputime_to_clock_t((cputime_t)(ct))
218 
219 /*
220  * PPC64 uses PACA which is task independent for storing accounting data while
221  * PPC32 uses struct thread_info, therefore at task switch the accounting data
222  * has to be populated in the new task
223  */
224 #ifdef CONFIG_PPC64
225 static inline void arch_vtime_task_switch(struct task_struct *tsk) { }
226 #else
227 void arch_vtime_task_switch(struct task_struct *tsk);
228 #endif
229 
230 #endif /* __KERNEL__ */
231 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
232 #endif /* __POWERPC_CPUTIME_H */
233