2 * Virtual cpu timer based timer functions.
4 * Copyright IBM Corp. 2004, 2012
5 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
8 #include <linux/kernel_stat.h>
9 #include <linux/sched/cputime.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/timex.h>
13 #include <linux/types.h>
14 #include <linux/time.h>
16 #include <asm/vtimer.h>
17 #include <asm/vtime.h>
18 #include <asm/cpu_mf.h>
23 static void virt_timer_expire(void);
25 static LIST_HEAD(virt_timer_list
);
26 static DEFINE_SPINLOCK(virt_timer_lock
);
27 static atomic64_t virt_timer_current
;
28 static atomic64_t virt_timer_elapsed
;
30 DEFINE_PER_CPU(u64
, mt_cycles
[8]);
31 static DEFINE_PER_CPU(u64
, mt_scaling_mult
) = { 1 };
32 static DEFINE_PER_CPU(u64
, mt_scaling_div
) = { 1 };
33 static DEFINE_PER_CPU(u64
, mt_scaling_jiffies
);
35 static inline u64
get_vtimer(void)
39 asm volatile("stpt %0" : "=m" (timer
));
43 static inline void set_vtimer(u64 expires
)
48 " stpt %0\n" /* Store current cpu timer value */
49 " spt %1" /* Set new value imm. afterwards */
50 : "=m" (timer
) : "m" (expires
));
51 S390_lowcore
.system_timer
+= S390_lowcore
.last_update_timer
- timer
;
52 S390_lowcore
.last_update_timer
= expires
;
55 static inline int virt_timer_forward(u64 elapsed
)
57 BUG_ON(!irqs_disabled());
59 if (list_empty(&virt_timer_list
))
61 elapsed
= atomic64_add_return(elapsed
, &virt_timer_elapsed
);
62 return elapsed
>= atomic64_read(&virt_timer_current
);
65 static void update_mt_scaling(void)
67 u64 cycles_new
[8], *cycles_old
;
68 u64 delta
, fac
, mult
, div
;
71 stcctm5(smp_cpu_mtid
+ 1, cycles_new
);
72 cycles_old
= this_cpu_ptr(mt_cycles
);
75 for (i
= 0; i
<= smp_cpu_mtid
; i
++) {
76 delta
= cycles_new
[i
] - cycles_old
[i
];
84 /* Update scaling factor */
85 __this_cpu_write(mt_scaling_mult
, mult
);
86 __this_cpu_write(mt_scaling_div
, div
);
87 memcpy(cycles_old
, cycles_new
,
88 sizeof(u64
) * (smp_cpu_mtid
+ 1));
90 __this_cpu_write(mt_scaling_jiffies
, jiffies_64
);
93 static inline u64
update_tsk_timer(unsigned long *tsk_vtime
, u64
new)
97 delta
= new - *tsk_vtime
;
103 static inline u64
scale_vtime(u64 vtime
)
105 u64 mult
= __this_cpu_read(mt_scaling_mult
);
106 u64 div
= __this_cpu_read(mt_scaling_div
);
109 return vtime
* mult
/ div
;
113 static void account_system_index_scaled(struct task_struct
*p
,
114 u64 cputime
, u64 scaled
,
115 enum cpu_usage_stat index
)
117 p
->stimescaled
+= cputime_to_nsecs(scaled
);
118 account_system_index_time(p
, cputime_to_nsecs(cputime
), index
);
122 * Update process times based on virtual cpu times stored by entry.S
123 * to the lowcore fields user_timer, system_timer & steal_clock.
125 static int do_account_vtime(struct task_struct
*tsk
)
127 u64 timer
, clock
, user
, guest
, system
, hardirq
, softirq
, steal
;
129 timer
= S390_lowcore
.last_update_timer
;
130 clock
= S390_lowcore
.last_update_clock
;
132 " stpt %0\n" /* Store current cpu timer value */
133 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
134 " stckf %1" /* Store current tod clock value */
136 " stck %1" /* Store current tod clock value */
138 : "=m" (S390_lowcore
.last_update_timer
),
139 "=m" (S390_lowcore
.last_update_clock
));
140 clock
= S390_lowcore
.last_update_clock
- clock
;
141 timer
-= S390_lowcore
.last_update_timer
;
144 S390_lowcore
.hardirq_timer
+= timer
;
146 S390_lowcore
.system_timer
+= timer
;
148 /* Update MT utilization calculation */
150 time_after64(jiffies_64
, this_cpu_read(mt_scaling_jiffies
)))
153 /* Calculate cputime delta */
154 user
= update_tsk_timer(&tsk
->thread
.user_timer
,
155 READ_ONCE(S390_lowcore
.user_timer
));
156 guest
= update_tsk_timer(&tsk
->thread
.guest_timer
,
157 READ_ONCE(S390_lowcore
.guest_timer
));
158 system
= update_tsk_timer(&tsk
->thread
.system_timer
,
159 READ_ONCE(S390_lowcore
.system_timer
));
160 hardirq
= update_tsk_timer(&tsk
->thread
.hardirq_timer
,
161 READ_ONCE(S390_lowcore
.hardirq_timer
));
162 softirq
= update_tsk_timer(&tsk
->thread
.softirq_timer
,
163 READ_ONCE(S390_lowcore
.softirq_timer
));
164 S390_lowcore
.steal_timer
+=
165 clock
- user
- guest
- system
- hardirq
- softirq
;
167 /* Push account value */
169 account_user_time(tsk
, cputime_to_nsecs(user
));
170 tsk
->utimescaled
+= cputime_to_nsecs(scale_vtime(user
));
174 account_guest_time(tsk
, cputime_to_nsecs(guest
));
175 tsk
->utimescaled
+= cputime_to_nsecs(scale_vtime(guest
));
179 account_system_index_scaled(tsk
, system
, scale_vtime(system
),
182 account_system_index_scaled(tsk
, hardirq
, scale_vtime(hardirq
),
185 account_system_index_scaled(tsk
, softirq
, scale_vtime(softirq
),
188 steal
= S390_lowcore
.steal_timer
;
189 if ((s64
) steal
> 0) {
190 S390_lowcore
.steal_timer
= 0;
191 account_steal_time(cputime_to_nsecs(steal
));
194 return virt_timer_forward(user
+ guest
+ system
+ hardirq
+ softirq
);
197 void vtime_task_switch(struct task_struct
*prev
)
199 do_account_vtime(prev
);
200 prev
->thread
.user_timer
= S390_lowcore
.user_timer
;
201 prev
->thread
.guest_timer
= S390_lowcore
.guest_timer
;
202 prev
->thread
.system_timer
= S390_lowcore
.system_timer
;
203 prev
->thread
.hardirq_timer
= S390_lowcore
.hardirq_timer
;
204 prev
->thread
.softirq_timer
= S390_lowcore
.softirq_timer
;
205 S390_lowcore
.user_timer
= current
->thread
.user_timer
;
206 S390_lowcore
.guest_timer
= current
->thread
.guest_timer
;
207 S390_lowcore
.system_timer
= current
->thread
.system_timer
;
208 S390_lowcore
.hardirq_timer
= current
->thread
.hardirq_timer
;
209 S390_lowcore
.softirq_timer
= current
->thread
.softirq_timer
;
213 * In s390, accounting pending user time also implies
214 * accounting system time in order to correctly compute
215 * the stolen time accounting.
217 void vtime_flush(struct task_struct
*tsk
)
219 if (do_account_vtime(tsk
))
224 * Update process times based on virtual cpu times stored by entry.S
225 * to the lowcore fields user_timer, system_timer & steal_clock.
227 void vtime_account_irq_enter(struct task_struct
*tsk
)
231 timer
= S390_lowcore
.last_update_timer
;
232 S390_lowcore
.last_update_timer
= get_vtimer();
233 timer
-= S390_lowcore
.last_update_timer
;
235 if ((tsk
->flags
& PF_VCPU
) && (irq_count() == 0))
236 S390_lowcore
.guest_timer
+= timer
;
237 else if (hardirq_count())
238 S390_lowcore
.hardirq_timer
+= timer
;
239 else if (in_serving_softirq())
240 S390_lowcore
.softirq_timer
+= timer
;
242 S390_lowcore
.system_timer
+= timer
;
244 virt_timer_forward(timer
);
246 EXPORT_SYMBOL_GPL(vtime_account_irq_enter
);
248 void vtime_account_system(struct task_struct
*tsk
)
249 __attribute__((alias("vtime_account_irq_enter")));
250 EXPORT_SYMBOL_GPL(vtime_account_system
);
253 * Sorted add to a list. List is linear searched until first bigger
256 static void list_add_sorted(struct vtimer_list
*timer
, struct list_head
*head
)
258 struct vtimer_list
*tmp
;
260 list_for_each_entry(tmp
, head
, entry
) {
261 if (tmp
->expires
> timer
->expires
) {
262 list_add_tail(&timer
->entry
, &tmp
->entry
);
266 list_add_tail(&timer
->entry
, head
);
270 * Handler for expired virtual CPU timer.
272 static void virt_timer_expire(void)
274 struct vtimer_list
*timer
, *tmp
;
275 unsigned long elapsed
;
278 /* walk timer list, fire all expired timers */
279 spin_lock(&virt_timer_lock
);
280 elapsed
= atomic64_read(&virt_timer_elapsed
);
281 list_for_each_entry_safe(timer
, tmp
, &virt_timer_list
, entry
) {
282 if (timer
->expires
< elapsed
)
283 /* move expired timer to the callback queue */
284 list_move_tail(&timer
->entry
, &cb_list
);
286 timer
->expires
-= elapsed
;
288 if (!list_empty(&virt_timer_list
)) {
289 timer
= list_first_entry(&virt_timer_list
,
290 struct vtimer_list
, entry
);
291 atomic64_set(&virt_timer_current
, timer
->expires
);
293 atomic64_sub(elapsed
, &virt_timer_elapsed
);
294 spin_unlock(&virt_timer_lock
);
296 /* Do callbacks and recharge periodic timers */
297 list_for_each_entry_safe(timer
, tmp
, &cb_list
, entry
) {
298 list_del_init(&timer
->entry
);
299 timer
->function(timer
->data
);
300 if (timer
->interval
) {
301 /* Recharge interval timer */
302 timer
->expires
= timer
->interval
+
303 atomic64_read(&virt_timer_elapsed
);
304 spin_lock(&virt_timer_lock
);
305 list_add_sorted(timer
, &virt_timer_list
);
306 spin_unlock(&virt_timer_lock
);
311 void init_virt_timer(struct vtimer_list
*timer
)
313 timer
->function
= NULL
;
314 INIT_LIST_HEAD(&timer
->entry
);
316 EXPORT_SYMBOL(init_virt_timer
);
318 static inline int vtimer_pending(struct vtimer_list
*timer
)
320 return !list_empty(&timer
->entry
);
323 static void internal_add_vtimer(struct vtimer_list
*timer
)
325 if (list_empty(&virt_timer_list
)) {
326 /* First timer, just program it. */
327 atomic64_set(&virt_timer_current
, timer
->expires
);
328 atomic64_set(&virt_timer_elapsed
, 0);
329 list_add(&timer
->entry
, &virt_timer_list
);
331 /* Update timer against current base. */
332 timer
->expires
+= atomic64_read(&virt_timer_elapsed
);
333 if (likely((s64
) timer
->expires
<
334 (s64
) atomic64_read(&virt_timer_current
)))
335 /* The new timer expires before the current timer. */
336 atomic64_set(&virt_timer_current
, timer
->expires
);
337 /* Insert new timer into the list. */
338 list_add_sorted(timer
, &virt_timer_list
);
342 static void __add_vtimer(struct vtimer_list
*timer
, int periodic
)
346 timer
->interval
= periodic
? timer
->expires
: 0;
347 spin_lock_irqsave(&virt_timer_lock
, flags
);
348 internal_add_vtimer(timer
);
349 spin_unlock_irqrestore(&virt_timer_lock
, flags
);
353 * add_virt_timer - add a oneshot virtual CPU timer
355 void add_virt_timer(struct vtimer_list
*timer
)
357 __add_vtimer(timer
, 0);
359 EXPORT_SYMBOL(add_virt_timer
);
362 * add_virt_timer_int - add an interval virtual CPU timer
364 void add_virt_timer_periodic(struct vtimer_list
*timer
)
366 __add_vtimer(timer
, 1);
368 EXPORT_SYMBOL(add_virt_timer_periodic
);
370 static int __mod_vtimer(struct vtimer_list
*timer
, u64 expires
, int periodic
)
375 BUG_ON(!timer
->function
);
377 if (timer
->expires
== expires
&& vtimer_pending(timer
))
379 spin_lock_irqsave(&virt_timer_lock
, flags
);
380 rc
= vtimer_pending(timer
);
382 list_del_init(&timer
->entry
);
383 timer
->interval
= periodic
? expires
: 0;
384 timer
->expires
= expires
;
385 internal_add_vtimer(timer
);
386 spin_unlock_irqrestore(&virt_timer_lock
, flags
);
391 * returns whether it has modified a pending timer (1) or not (0)
393 int mod_virt_timer(struct vtimer_list
*timer
, u64 expires
)
395 return __mod_vtimer(timer
, expires
, 0);
397 EXPORT_SYMBOL(mod_virt_timer
);
400 * returns whether it has modified a pending timer (1) or not (0)
402 int mod_virt_timer_periodic(struct vtimer_list
*timer
, u64 expires
)
404 return __mod_vtimer(timer
, expires
, 1);
406 EXPORT_SYMBOL(mod_virt_timer_periodic
);
409 * Delete a virtual timer.
411 * returns whether the deleted timer was pending (1) or not (0)
413 int del_virt_timer(struct vtimer_list
*timer
)
417 if (!vtimer_pending(timer
))
419 spin_lock_irqsave(&virt_timer_lock
, flags
);
420 list_del_init(&timer
->entry
);
421 spin_unlock_irqrestore(&virt_timer_lock
, flags
);
424 EXPORT_SYMBOL(del_virt_timer
);
427 * Start the virtual CPU timer on the current CPU.
429 void vtime_init(void)
431 /* set initial cpu timer */
432 set_vtimer(VTIMER_MAX_SLICE
);
433 /* Setup initial MT scaling values */
435 __this_cpu_write(mt_scaling_jiffies
, jiffies
);
436 __this_cpu_write(mt_scaling_mult
, 1);
437 __this_cpu_write(mt_scaling_div
, 1);
438 stcctm5(smp_cpu_mtid
+ 1, this_cpu_ptr(mt_cycles
));