2 * arch/s390/kernel/vtime.c
3 * Virtual cpu timer based timer functions.
6 * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/time.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/smp.h>
17 #include <linux/types.h>
18 #include <linux/timex.h>
19 #include <linux/notifier.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/rcupdate.h>
22 #include <linux/posix-timers.h>
24 #include <asm/s390_ext.h>
25 #include <asm/timer.h>
27 #define VTIMER_MAGIC (TIMER_MAGIC + 1)
28 static ext_int_info_t ext_int_info_timer;
29 DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
31 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
33 * Update process times based on virtual cpu times stored by entry.S
34 * to the lowcore fields user_timer, system_timer & steal_clock.
36 void account_user_vtime(struct task_struct *tsk)
42 timer = S390_lowcore.last_update_timer;
43 clock = S390_lowcore.last_update_clock;
44 asm volatile (" STPT %0\n" /* Store current cpu timer value */
45 " STCK %1" /* Store current tod clock value */
46 : "=m" (S390_lowcore.last_update_timer),
47 "=m" (S390_lowcore.last_update_clock) );
48 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
49 S390_lowcore.steal_clock += S390_lowcore.last_update_clock - clock;
51 cputime = S390_lowcore.user_timer >> 12;
52 rcu_user_flag = cputime != 0;
53 S390_lowcore.user_timer -= cputime << 12;
54 S390_lowcore.steal_clock -= cputime << 12;
55 account_user_time(tsk, cputime);
57 cputime = S390_lowcore.system_timer >> 12;
58 S390_lowcore.system_timer -= cputime << 12;
59 S390_lowcore.steal_clock -= cputime << 12;
60 account_system_time(tsk, HARDIRQ_OFFSET, cputime);
62 cputime = S390_lowcore.steal_clock;
63 if ((__s64) cputime > 0) {
65 S390_lowcore.steal_clock -= cputime << 12;
66 account_steal_time(tsk, cputime);
70 if (rcu_pending(smp_processor_id()))
71 rcu_check_callbacks(smp_processor_id(), rcu_user_flag);
73 run_posix_cpu_timers(tsk);
77 * Update process times based on virtual cpu times stored by entry.S
78 * to the lowcore fields user_timer, system_timer & steal_clock.
80 void account_system_vtime(struct task_struct *tsk)
85 timer = S390_lowcore.last_update_timer;
86 asm volatile (" STPT %0" /* Store current cpu timer value */
87 : "=m" (S390_lowcore.last_update_timer) );
88 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
90 cputime = S390_lowcore.system_timer >> 12;
91 S390_lowcore.system_timer -= cputime << 12;
92 S390_lowcore.steal_clock -= cputime << 12;
93 account_system_time(tsk, 0, cputime);
96 static inline void set_vtimer(__u64 expires)
100 asm volatile (" STPT %0\n" /* Store current cpu timer value */
101 " SPT %1" /* Set new value immediatly afterwards */
102 : "=m" (timer) : "m" (expires) );
103 S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
104 S390_lowcore.last_update_timer = expires;
106 /* store expire time for this CPU timer */
107 per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
110 static inline void set_vtimer(__u64 expires)
112 S390_lowcore.last_update_timer = expires;
113 asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
115 /* store expire time for this CPU timer */
116 per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
120 static void start_cpu_timer(void)
122 struct vtimer_queue *vt_list;
124 vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
126 /* CPU timer interrupt is pending, don't reprogramm it */
127 if (vt_list->idle & 1LL<<63)
130 if (!list_empty(&vt_list->list))
131 set_vtimer(vt_list->idle);
134 static void stop_cpu_timer(void)
136 struct vtimer_queue *vt_list;
138 vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
141 if (list_empty(&vt_list->list)) {
142 vt_list->idle = VTIMER_MAX_SLICE;
146 /* store the actual expire value */
147 asm volatile ("STPT %0" : "=m" (vt_list->idle));
150 * If the CPU timer is negative we don't reprogramm
151 * it because we will get instantly an interrupt.
153 if (vt_list->idle & 1LL<<63)
156 vt_list->offset += vt_list->to_expire - vt_list->idle;
159 * We cannot halt the CPU timer, we just write a value that
160 * nearly never expires (only after 71 years) and re-write
161 * the stored expire value if we continue the timer
164 set_vtimer(VTIMER_MAX_SLICE);
168 * Sorted add to a list. List is linear searched until first bigger
171 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
173 struct vtimer_list *event;
175 list_for_each_entry(event, head, entry) {
176 if (event->expires > timer->expires) {
177 list_add_tail(&timer->entry, &event->entry);
181 list_add_tail(&timer->entry, head);
185 * Do the callback functions of expired vtimer events.
186 * Called from within the interrupt handler.
188 static void do_callbacks(struct list_head *cb_list, struct pt_regs *regs)
190 struct vtimer_queue *vt_list;
191 struct vtimer_list *event, *tmp;
192 void (*fn)(unsigned long, struct pt_regs*);
195 if (list_empty(cb_list))
198 vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
200 list_for_each_entry_safe(event, tmp, cb_list, entry) {
201 fn = event->function;
205 if (!event->interval)
206 /* delete one shot timer */
207 list_del_init(&event->entry);
209 /* move interval timer back to list */
210 spin_lock(&vt_list->lock);
211 list_del_init(&event->entry);
212 list_add_sorted(event, &vt_list->list);
213 spin_unlock(&vt_list->lock);
219 * Handler for the virtual CPU timer.
221 static void do_cpu_timer_interrupt(struct pt_regs *regs, __u16 error_code)
225 struct vtimer_queue *vt_list;
226 struct vtimer_list *event, *tmp;
227 struct list_head *ptr;
228 /* the callback queue */
229 struct list_head cb_list;
231 INIT_LIST_HEAD(&cb_list);
232 cpu = smp_processor_id();
233 vt_list = &per_cpu(virt_cpu_timer, cpu);
235 /* walk timer list, fire all expired events */
236 spin_lock(&vt_list->lock);
238 if (vt_list->to_expire < VTIMER_MAX_SLICE)
239 vt_list->offset += vt_list->to_expire;
241 list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
242 if (event->expires > vt_list->offset)
243 /* found first unexpired event, leave */
246 /* re-charge interval timer, we have to add the offset */
248 event->expires = event->interval + vt_list->offset;
250 /* move expired timer to the callback queue */
251 list_move_tail(&event->entry, &cb_list);
253 spin_unlock(&vt_list->lock);
254 do_callbacks(&cb_list, regs);
256 /* next event is first in list */
257 spin_lock(&vt_list->lock);
258 if (!list_empty(&vt_list->list)) {
259 ptr = vt_list->list.next;
260 event = list_entry(ptr, struct vtimer_list, entry);
261 next = event->expires - vt_list->offset;
263 /* add the expired time from this interrupt handler
264 * and the callback functions
266 asm volatile ("STPT %0" : "=m" (delta));
267 delta = 0xffffffffffffffffLL - delta + 1;
268 vt_list->offset += delta;
272 next = VTIMER_MAX_SLICE;
274 spin_unlock(&vt_list->lock);
278 void init_virt_timer(struct vtimer_list *timer)
280 timer->magic = VTIMER_MAGIC;
281 timer->function = NULL;
282 INIT_LIST_HEAD(&timer->entry);
283 spin_lock_init(&timer->lock);
285 EXPORT_SYMBOL(init_virt_timer);
287 static inline int check_vtimer(struct vtimer_list *timer)
289 if (timer->magic != VTIMER_MAGIC)
294 static inline int vtimer_pending(struct vtimer_list *timer)
296 return (!list_empty(&timer->entry));
300 * this function should only run on the specified CPU
302 static void internal_add_vtimer(struct vtimer_list *timer)
306 struct vtimer_list *event;
307 struct vtimer_queue *vt_list;
309 vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
310 spin_lock_irqsave(&vt_list->lock, flags);
312 if (timer->cpu != smp_processor_id())
313 printk("internal_add_vtimer: BUG, running on wrong CPU");
315 /* if list is empty we only have to set the timer */
316 if (list_empty(&vt_list->list)) {
317 /* reset the offset, this may happen if the last timer was
318 * just deleted by mod_virt_timer and the interrupt
319 * didn't happen until here
326 asm volatile ("STPT %0" : "=m" (done));
328 /* calculate completed work */
329 done = vt_list->to_expire - done + vt_list->offset;
332 list_for_each_entry(event, &vt_list->list, entry)
333 event->expires -= done;
336 list_add_sorted(timer, &vt_list->list);
338 /* get first element, which is the next vtimer slice */
339 event = list_entry(vt_list->list.next, struct vtimer_list, entry);
341 set_vtimer(event->expires);
342 spin_unlock_irqrestore(&vt_list->lock, flags);
343 /* release CPU aquired in prepare_vtimer or mod_virt_timer() */
347 static inline int prepare_vtimer(struct vtimer_list *timer)
349 if (check_vtimer(timer) || !timer->function) {
350 printk("add_virt_timer: uninitialized timer\n");
354 if (!timer->expires || timer->expires > VTIMER_MAX_SLICE) {
355 printk("add_virt_timer: invalid timer expire value!\n");
359 if (vtimer_pending(timer)) {
360 printk("add_virt_timer: timer pending\n");
364 timer->cpu = get_cpu();
369 * add_virt_timer - add an oneshot virtual CPU timer
371 void add_virt_timer(void *new)
373 struct vtimer_list *timer;
375 timer = (struct vtimer_list *)new;
377 if (prepare_vtimer(timer) < 0)
381 internal_add_vtimer(timer);
383 EXPORT_SYMBOL(add_virt_timer);
386 * add_virt_timer_int - add an interval virtual CPU timer
388 void add_virt_timer_periodic(void *new)
390 struct vtimer_list *timer;
392 timer = (struct vtimer_list *)new;
394 if (prepare_vtimer(timer) < 0)
397 timer->interval = timer->expires;
398 internal_add_vtimer(timer);
400 EXPORT_SYMBOL(add_virt_timer_periodic);
403 * If we change a pending timer the function must be called on the CPU
404 * where the timer is running on, e.g. by smp_call_function_on()
406 * The original mod_timer adds the timer if it is not pending. For compatibility
407 * we do the same. The timer will be added on the current CPU as a oneshot timer.
409 * returns whether it has modified a pending timer (1) or not (0)
411 int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
413 struct vtimer_queue *vt_list;
417 if (check_vtimer(timer) || !timer->function) {
418 printk("mod_virt_timer: uninitialized timer\n");
422 if (!expires || expires > VTIMER_MAX_SLICE) {
423 printk("mod_virt_timer: invalid expire range\n");
428 * This is a common optimization triggered by the
429 * networking code - if the timer is re-modified
430 * to be the same thing then just return:
432 if (timer->expires == expires && vtimer_pending(timer))
436 vt_list = &per_cpu(virt_cpu_timer, cpu);
438 /* disable interrupts before test if timer is pending */
439 spin_lock_irqsave(&vt_list->lock, flags);
441 /* if timer isn't pending add it on the current CPU */
442 if (!vtimer_pending(timer)) {
443 spin_unlock_irqrestore(&vt_list->lock, flags);
444 /* we do not activate an interval timer with mod_virt_timer */
446 timer->expires = expires;
448 internal_add_vtimer(timer);
452 /* check if we run on the right CPU */
453 if (timer->cpu != cpu) {
454 printk("mod_virt_timer: running on wrong CPU, check your code\n");
455 spin_unlock_irqrestore(&vt_list->lock, flags);
460 list_del_init(&timer->entry);
461 timer->expires = expires;
463 /* also change the interval if we have an interval timer */
465 timer->interval = expires;
467 /* the timer can't expire anymore so we can release the lock */
468 spin_unlock_irqrestore(&vt_list->lock, flags);
469 internal_add_vtimer(timer);
472 EXPORT_SYMBOL(mod_virt_timer);
475 * delete a virtual timer
477 * returns whether the deleted timer was pending (1) or not (0)
479 int del_virt_timer(struct vtimer_list *timer)
482 struct vtimer_queue *vt_list;
484 if (check_vtimer(timer)) {
485 printk("del_virt_timer: timer not initialized\n");
489 /* check if timer is pending */
490 if (!vtimer_pending(timer))
493 vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
494 spin_lock_irqsave(&vt_list->lock, flags);
496 /* we don't interrupt a running timer, just let it expire! */
497 list_del_init(&timer->entry);
499 /* last timer removed */
500 if (list_empty(&vt_list->list)) {
501 vt_list->to_expire = 0;
505 spin_unlock_irqrestore(&vt_list->lock, flags);
508 EXPORT_SYMBOL(del_virt_timer);
511 * Start the virtual CPU timer on the current CPU.
513 void init_cpu_vtimer(void)
515 struct vtimer_queue *vt_list;
518 /* kick the virtual timer */
519 S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
520 S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
521 asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
522 asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
523 __ctl_store(cr0, 0, 0);
525 __ctl_load(cr0, 0, 0);
527 vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
528 INIT_LIST_HEAD(&vt_list->list);
529 spin_lock_init(&vt_list->lock);
530 vt_list->to_expire = 0;
536 static int vtimer_idle_notify(struct notifier_block *self,
537 unsigned long action, void *hcpu)
550 static struct notifier_block vtimer_idle_nb = {
551 .notifier_call = vtimer_idle_notify,
554 void __init vtime_init(void)
556 /* request the cpu timer external interrupt */
557 if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
558 &ext_int_info_timer) != 0)
559 panic("Couldn't request external interrupt 0x1005");
561 if (register_idle_notifier(&vtimer_idle_nb))
562 panic("Couldn't register idle notifier");