x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / s390 / kernel / vtime.c
blobdd7178fbb4f3bd3f32955eeacf6e640f371e06a3
1 /*
2 * Virtual cpu timer based timer functions.
4 * Copyright IBM Corp. 2004, 2012
5 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
6 */
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>
19 #include <asm/smp.h>
21 #include "entry.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)
37 u64 timer;
39 asm volatile("stpt %0" : "=m" (timer));
40 return timer;
43 static inline void set_vtimer(u64 expires)
45 u64 timer;
47 asm volatile(
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))
60 return 0;
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;
69 int i;
71 stcctm5(smp_cpu_mtid + 1, cycles_new);
72 cycles_old = this_cpu_ptr(mt_cycles);
73 fac = 1;
74 mult = div = 0;
75 for (i = 0; i <= smp_cpu_mtid; i++) {
76 delta = cycles_new[i] - cycles_old[i];
77 div += delta;
78 mult *= i + 1;
79 mult += delta * fac;
80 fac *= i + 1;
82 div *= fac;
83 if (div > 0) {
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)
95 u64 delta;
97 delta = new - *tsk_vtime;
98 *tsk_vtime = new;
99 return delta;
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);
108 if (smp_cpu_mtid)
109 return vtime * mult / div;
110 return vtime;
113 static void account_system_index_scaled(struct task_struct *p, u64 cputime,
114 enum cpu_usage_stat index)
116 p->stimescaled += cputime_to_nsecs(scale_vtime(cputime));
117 account_system_index_time(p, cputime_to_nsecs(cputime), index);
121 * Update process times based on virtual cpu times stored by entry.S
122 * to the lowcore fields user_timer, system_timer & steal_clock.
124 static int do_account_vtime(struct task_struct *tsk)
126 u64 timer, clock, user, guest, system, hardirq, softirq, steal;
128 timer = S390_lowcore.last_update_timer;
129 clock = S390_lowcore.last_update_clock;
130 asm volatile(
131 " stpt %0\n" /* Store current cpu timer value */
132 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
133 " stckf %1" /* Store current tod clock value */
134 #else
135 " stck %1" /* Store current tod clock value */
136 #endif
137 : "=m" (S390_lowcore.last_update_timer),
138 "=m" (S390_lowcore.last_update_clock));
139 clock = S390_lowcore.last_update_clock - clock;
140 timer -= S390_lowcore.last_update_timer;
142 if (hardirq_count())
143 S390_lowcore.hardirq_timer += timer;
144 else
145 S390_lowcore.system_timer += timer;
147 /* Update MT utilization calculation */
148 if (smp_cpu_mtid &&
149 time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies)))
150 update_mt_scaling();
152 /* Calculate cputime delta */
153 user = update_tsk_timer(&tsk->thread.user_timer,
154 READ_ONCE(S390_lowcore.user_timer));
155 guest = update_tsk_timer(&tsk->thread.guest_timer,
156 READ_ONCE(S390_lowcore.guest_timer));
157 system = update_tsk_timer(&tsk->thread.system_timer,
158 READ_ONCE(S390_lowcore.system_timer));
159 hardirq = update_tsk_timer(&tsk->thread.hardirq_timer,
160 READ_ONCE(S390_lowcore.hardirq_timer));
161 softirq = update_tsk_timer(&tsk->thread.softirq_timer,
162 READ_ONCE(S390_lowcore.softirq_timer));
163 S390_lowcore.steal_timer +=
164 clock - user - guest - system - hardirq - softirq;
166 /* Push account value */
167 if (user) {
168 account_user_time(tsk, cputime_to_nsecs(user));
169 tsk->utimescaled += cputime_to_nsecs(scale_vtime(user));
172 if (guest) {
173 account_guest_time(tsk, cputime_to_nsecs(guest));
174 tsk->utimescaled += cputime_to_nsecs(scale_vtime(guest));
177 if (system)
178 account_system_index_scaled(tsk, system, CPUTIME_SYSTEM);
179 if (hardirq)
180 account_system_index_scaled(tsk, hardirq, CPUTIME_IRQ);
181 if (softirq)
182 account_system_index_scaled(tsk, softirq, CPUTIME_SOFTIRQ);
184 steal = S390_lowcore.steal_timer;
185 if ((s64) steal > 0) {
186 S390_lowcore.steal_timer = 0;
187 account_steal_time(cputime_to_nsecs(steal));
190 return virt_timer_forward(user + guest + system + hardirq + softirq);
193 void vtime_task_switch(struct task_struct *prev)
195 do_account_vtime(prev);
196 prev->thread.user_timer = S390_lowcore.user_timer;
197 prev->thread.guest_timer = S390_lowcore.guest_timer;
198 prev->thread.system_timer = S390_lowcore.system_timer;
199 prev->thread.hardirq_timer = S390_lowcore.hardirq_timer;
200 prev->thread.softirq_timer = S390_lowcore.softirq_timer;
201 S390_lowcore.user_timer = current->thread.user_timer;
202 S390_lowcore.guest_timer = current->thread.guest_timer;
203 S390_lowcore.system_timer = current->thread.system_timer;
204 S390_lowcore.hardirq_timer = current->thread.hardirq_timer;
205 S390_lowcore.softirq_timer = current->thread.softirq_timer;
209 * In s390, accounting pending user time also implies
210 * accounting system time in order to correctly compute
211 * the stolen time accounting.
213 void vtime_flush(struct task_struct *tsk)
215 if (do_account_vtime(tsk))
216 virt_timer_expire();
220 * Update process times based on virtual cpu times stored by entry.S
221 * to the lowcore fields user_timer, system_timer & steal_clock.
223 void vtime_account_irq_enter(struct task_struct *tsk)
225 u64 timer;
227 timer = S390_lowcore.last_update_timer;
228 S390_lowcore.last_update_timer = get_vtimer();
229 timer -= S390_lowcore.last_update_timer;
231 if ((tsk->flags & PF_VCPU) && (irq_count() == 0))
232 S390_lowcore.guest_timer += timer;
233 else if (hardirq_count())
234 S390_lowcore.hardirq_timer += timer;
235 else if (in_serving_softirq())
236 S390_lowcore.softirq_timer += timer;
237 else
238 S390_lowcore.system_timer += timer;
240 virt_timer_forward(timer);
242 EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
244 void vtime_account_system(struct task_struct *tsk)
245 __attribute__((alias("vtime_account_irq_enter")));
246 EXPORT_SYMBOL_GPL(vtime_account_system);
249 * Sorted add to a list. List is linear searched until first bigger
250 * element is found.
252 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
254 struct vtimer_list *tmp;
256 list_for_each_entry(tmp, head, entry) {
257 if (tmp->expires > timer->expires) {
258 list_add_tail(&timer->entry, &tmp->entry);
259 return;
262 list_add_tail(&timer->entry, head);
266 * Handler for expired virtual CPU timer.
268 static void virt_timer_expire(void)
270 struct vtimer_list *timer, *tmp;
271 unsigned long elapsed;
272 LIST_HEAD(cb_list);
274 /* walk timer list, fire all expired timers */
275 spin_lock(&virt_timer_lock);
276 elapsed = atomic64_read(&virt_timer_elapsed);
277 list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
278 if (timer->expires < elapsed)
279 /* move expired timer to the callback queue */
280 list_move_tail(&timer->entry, &cb_list);
281 else
282 timer->expires -= elapsed;
284 if (!list_empty(&virt_timer_list)) {
285 timer = list_first_entry(&virt_timer_list,
286 struct vtimer_list, entry);
287 atomic64_set(&virt_timer_current, timer->expires);
289 atomic64_sub(elapsed, &virt_timer_elapsed);
290 spin_unlock(&virt_timer_lock);
292 /* Do callbacks and recharge periodic timers */
293 list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
294 list_del_init(&timer->entry);
295 timer->function(timer->data);
296 if (timer->interval) {
297 /* Recharge interval timer */
298 timer->expires = timer->interval +
299 atomic64_read(&virt_timer_elapsed);
300 spin_lock(&virt_timer_lock);
301 list_add_sorted(timer, &virt_timer_list);
302 spin_unlock(&virt_timer_lock);
307 void init_virt_timer(struct vtimer_list *timer)
309 timer->function = NULL;
310 INIT_LIST_HEAD(&timer->entry);
312 EXPORT_SYMBOL(init_virt_timer);
314 static inline int vtimer_pending(struct vtimer_list *timer)
316 return !list_empty(&timer->entry);
319 static void internal_add_vtimer(struct vtimer_list *timer)
321 if (list_empty(&virt_timer_list)) {
322 /* First timer, just program it. */
323 atomic64_set(&virt_timer_current, timer->expires);
324 atomic64_set(&virt_timer_elapsed, 0);
325 list_add(&timer->entry, &virt_timer_list);
326 } else {
327 /* Update timer against current base. */
328 timer->expires += atomic64_read(&virt_timer_elapsed);
329 if (likely((s64) timer->expires <
330 (s64) atomic64_read(&virt_timer_current)))
331 /* The new timer expires before the current timer. */
332 atomic64_set(&virt_timer_current, timer->expires);
333 /* Insert new timer into the list. */
334 list_add_sorted(timer, &virt_timer_list);
338 static void __add_vtimer(struct vtimer_list *timer, int periodic)
340 unsigned long flags;
342 timer->interval = periodic ? timer->expires : 0;
343 spin_lock_irqsave(&virt_timer_lock, flags);
344 internal_add_vtimer(timer);
345 spin_unlock_irqrestore(&virt_timer_lock, flags);
349 * add_virt_timer - add a oneshot virtual CPU timer
351 void add_virt_timer(struct vtimer_list *timer)
353 __add_vtimer(timer, 0);
355 EXPORT_SYMBOL(add_virt_timer);
358 * add_virt_timer_int - add an interval virtual CPU timer
360 void add_virt_timer_periodic(struct vtimer_list *timer)
362 __add_vtimer(timer, 1);
364 EXPORT_SYMBOL(add_virt_timer_periodic);
366 static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
368 unsigned long flags;
369 int rc;
371 BUG_ON(!timer->function);
373 if (timer->expires == expires && vtimer_pending(timer))
374 return 1;
375 spin_lock_irqsave(&virt_timer_lock, flags);
376 rc = vtimer_pending(timer);
377 if (rc)
378 list_del_init(&timer->entry);
379 timer->interval = periodic ? expires : 0;
380 timer->expires = expires;
381 internal_add_vtimer(timer);
382 spin_unlock_irqrestore(&virt_timer_lock, flags);
383 return rc;
387 * returns whether it has modified a pending timer (1) or not (0)
389 int mod_virt_timer(struct vtimer_list *timer, u64 expires)
391 return __mod_vtimer(timer, expires, 0);
393 EXPORT_SYMBOL(mod_virt_timer);
396 * returns whether it has modified a pending timer (1) or not (0)
398 int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
400 return __mod_vtimer(timer, expires, 1);
402 EXPORT_SYMBOL(mod_virt_timer_periodic);
405 * Delete a virtual timer.
407 * returns whether the deleted timer was pending (1) or not (0)
409 int del_virt_timer(struct vtimer_list *timer)
411 unsigned long flags;
413 if (!vtimer_pending(timer))
414 return 0;
415 spin_lock_irqsave(&virt_timer_lock, flags);
416 list_del_init(&timer->entry);
417 spin_unlock_irqrestore(&virt_timer_lock, flags);
418 return 1;
420 EXPORT_SYMBOL(del_virt_timer);
423 * Start the virtual CPU timer on the current CPU.
425 void vtime_init(void)
427 /* set initial cpu timer */
428 set_vtimer(VTIMER_MAX_SLICE);
429 /* Setup initial MT scaling values */
430 if (smp_cpu_mtid) {
431 __this_cpu_write(mt_scaling_jiffies, jiffies);
432 __this_cpu_write(mt_scaling_mult, 1);
433 __this_cpu_write(mt_scaling_div, 1);
434 stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles));