blob: 4a4a34caec55cdb277806b8e0ddbc787cedc1003 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/vtime.c
3 * Virtual cpu timer based timer functions.
4 *
5 * S390 version
6 * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/time.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/smp.h>
16#include <linux/types.h>
17#include <linux/timex.h>
18#include <linux/notifier.h>
19#include <linux/kernel_stat.h>
20#include <linux/rcupdate.h>
21#include <linux/posix-timers.h>
22
23#include <asm/s390_ext.h>
24#include <asm/timer.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020025#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static ext_int_info_t ext_int_info_timer;
Heiko Carstens2b67fc42007-02-05 21:16:47 +010028static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Update process times based on virtual cpu times stored by entry.S
32 * to the lowcore fields user_timer, system_timer & steal_clock.
33 */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +010034void account_process_tick(struct task_struct *tsk, int user_tick)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 cputime_t cputime;
37 __u64 timer, clock;
38 int rcu_user_flag;
39
40 timer = S390_lowcore.last_update_timer;
41 clock = S390_lowcore.last_update_clock;
42 asm volatile (" STPT %0\n" /* Store current cpu timer value */
43 " STCK %1" /* Store current tod clock value */
44 : "=m" (S390_lowcore.last_update_timer),
45 "=m" (S390_lowcore.last_update_clock) );
46 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
47 S390_lowcore.steal_clock += S390_lowcore.last_update_clock - clock;
48
49 cputime = S390_lowcore.user_timer >> 12;
50 rcu_user_flag = cputime != 0;
51 S390_lowcore.user_timer -= cputime << 12;
52 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky457533a2008-12-31 15:11:37 +010053 account_user_time(tsk, cputime, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 cputime = S390_lowcore.system_timer >> 12;
56 S390_lowcore.system_timer -= cputime << 12;
57 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +010058 if (idle_task(smp_processor_id()) != current)
59 account_system_time(tsk, HARDIRQ_OFFSET, cputime, cputime);
60 else
61 account_idle_time(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 cputime = S390_lowcore.steal_clock;
64 if ((__s64) cputime > 0) {
65 cputime >>= 12;
66 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +010067 if (idle_task(smp_processor_id()) != current)
68 account_steal_time(cputime);
69 else
70 account_idle_time(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74/*
75 * Update process times based on virtual cpu times stored by entry.S
76 * to the lowcore fields user_timer, system_timer & steal_clock.
77 */
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -080078void account_vtime(struct task_struct *tsk)
79{
80 cputime_t cputime;
81 __u64 timer;
82
83 timer = S390_lowcore.last_update_timer;
84 asm volatile (" STPT %0" /* Store current cpu timer value */
85 : "=m" (S390_lowcore.last_update_timer) );
86 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
87
88 cputime = S390_lowcore.user_timer >> 12;
89 S390_lowcore.user_timer -= cputime << 12;
90 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky457533a2008-12-31 15:11:37 +010091 account_user_time(tsk, cputime, cputime);
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -080092
93 cputime = S390_lowcore.system_timer >> 12;
94 S390_lowcore.system_timer -= cputime << 12;
95 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +010096 if (idle_task(smp_processor_id()) != current)
97 account_system_time(tsk, 0, cputime, cputime);
98 else
99 account_idle_time(cputime);
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -0800100}
101
102/*
103 * Update process times based on virtual cpu times stored by entry.S
104 * to the lowcore fields user_timer, system_timer & steal_clock.
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106void account_system_vtime(struct task_struct *tsk)
107{
108 cputime_t cputime;
109 __u64 timer;
110
111 timer = S390_lowcore.last_update_timer;
112 asm volatile (" STPT %0" /* Store current cpu timer value */
113 : "=m" (S390_lowcore.last_update_timer) );
114 S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
115
116 cputime = S390_lowcore.system_timer >> 12;
117 S390_lowcore.system_timer -= cputime << 12;
118 S390_lowcore.steal_clock -= cputime << 12;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100119 if (in_irq() || idle_task(smp_processor_id()) != current)
120 account_system_time(tsk, 0, cputime, cputime);
121 else
122 account_idle_time(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100124EXPORT_SYMBOL_GPL(account_system_vtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126static inline void set_vtimer(__u64 expires)
127{
128 __u64 timer;
129
130 asm volatile (" STPT %0\n" /* Store current cpu timer value */
131 " SPT %1" /* Set new value immediatly afterwards */
132 : "=m" (timer) : "m" (expires) );
133 S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
134 S390_lowcore.last_update_timer = expires;
135
136 /* store expire time for this CPU timer */
Jan Glauberdb77aa52007-04-27 16:01:55 +0200137 __get_cpu_var(virt_cpu_timer).to_expire = expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Heiko Carstens773922e2008-07-14 09:59:06 +0200140void vtime_start_cpu_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct vtimer_queue *vt_list;
143
Jan Glauberdb77aa52007-04-27 16:01:55 +0200144 vt_list = &__get_cpu_var(virt_cpu_timer);
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700145
146 /* CPU timer interrupt is pending, don't reprogramm it */
147 if (vt_list->idle & 1LL<<63)
148 return;
149
150 if (!list_empty(&vt_list->list))
151 set_vtimer(vt_list->idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Heiko Carstens773922e2008-07-14 09:59:06 +0200154void vtime_stop_cpu_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct vtimer_queue *vt_list;
157
Jan Glauberdb77aa52007-04-27 16:01:55 +0200158 vt_list = &__get_cpu_var(virt_cpu_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* nothing to do */
161 if (list_empty(&vt_list->list)) {
162 vt_list->idle = VTIMER_MAX_SLICE;
163 goto fire;
164 }
165
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700166 /* store the actual expire value */
167 asm volatile ("STPT %0" : "=m" (vt_list->idle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700170 * If the CPU timer is negative we don't reprogramm
171 * it because we will get instantly an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700173 if (vt_list->idle & 1LL<<63)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700176 vt_list->offset += vt_list->to_expire - vt_list->idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * We cannot halt the CPU timer, we just write a value that
180 * nearly never expires (only after 71 years) and re-write
181 * the stored expire value if we continue the timer
182 */
183 fire:
184 set_vtimer(VTIMER_MAX_SLICE);
185}
186
187/*
188 * Sorted add to a list. List is linear searched until first bigger
189 * element is found.
190 */
191static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
192{
193 struct vtimer_list *event;
194
195 list_for_each_entry(event, head, entry) {
196 if (event->expires > timer->expires) {
197 list_add_tail(&timer->entry, &event->entry);
198 return;
199 }
200 }
201 list_add_tail(&timer->entry, head);
202}
203
204/*
205 * Do the callback functions of expired vtimer events.
206 * Called from within the interrupt handler.
207 */
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200208static void do_callbacks(struct list_head *cb_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct vtimer_queue *vt_list;
211 struct vtimer_list *event, *tmp;
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200212 void (*fn)(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned long data;
214
215 if (list_empty(cb_list))
216 return;
217
Jan Glauberdb77aa52007-04-27 16:01:55 +0200218 vt_list = &__get_cpu_var(virt_cpu_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 list_for_each_entry_safe(event, tmp, cb_list, entry) {
221 fn = event->function;
222 data = event->data;
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200223 fn(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!event->interval)
226 /* delete one shot timer */
227 list_del_init(&event->entry);
228 else {
229 /* move interval timer back to list */
230 spin_lock(&vt_list->lock);
231 list_del_init(&event->entry);
232 list_add_sorted(event, &vt_list->list);
233 spin_unlock(&vt_list->lock);
234 }
235 }
236}
237
238/*
239 * Handler for the virtual CPU timer.
240 */
Heiko Carstens5a489b92006-10-06 16:38:35 +0200241static void do_cpu_timer_interrupt(__u16 error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 __u64 next, delta;
244 struct vtimer_queue *vt_list;
245 struct vtimer_list *event, *tmp;
246 struct list_head *ptr;
247 /* the callback queue */
248 struct list_head cb_list;
249
250 INIT_LIST_HEAD(&cb_list);
Jan Glauberdb77aa52007-04-27 16:01:55 +0200251 vt_list = &__get_cpu_var(virt_cpu_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* walk timer list, fire all expired events */
254 spin_lock(&vt_list->lock);
255
256 if (vt_list->to_expire < VTIMER_MAX_SLICE)
257 vt_list->offset += vt_list->to_expire;
258
259 list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
260 if (event->expires > vt_list->offset)
261 /* found first unexpired event, leave */
262 break;
263
264 /* re-charge interval timer, we have to add the offset */
265 if (event->interval)
266 event->expires = event->interval + vt_list->offset;
267
268 /* move expired timer to the callback queue */
269 list_move_tail(&event->entry, &cb_list);
270 }
271 spin_unlock(&vt_list->lock);
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200272 do_callbacks(&cb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* next event is first in list */
275 spin_lock(&vt_list->lock);
276 if (!list_empty(&vt_list->list)) {
277 ptr = vt_list->list.next;
278 event = list_entry(ptr, struct vtimer_list, entry);
279 next = event->expires - vt_list->offset;
280
281 /* add the expired time from this interrupt handler
282 * and the callback functions
283 */
284 asm volatile ("STPT %0" : "=m" (delta));
285 delta = 0xffffffffffffffffLL - delta + 1;
286 vt_list->offset += delta;
287 next -= delta;
288 } else {
289 vt_list->offset = 0;
290 next = VTIMER_MAX_SLICE;
291 }
292 spin_unlock(&vt_list->lock);
293 set_vtimer(next);
294}
295
296void init_virt_timer(struct vtimer_list *timer)
297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 timer->function = NULL;
299 INIT_LIST_HEAD(&timer->entry);
300 spin_lock_init(&timer->lock);
301}
302EXPORT_SYMBOL(init_virt_timer);
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline int vtimer_pending(struct vtimer_list *timer)
305{
306 return (!list_empty(&timer->entry));
307}
308
309/*
310 * this function should only run on the specified CPU
311 */
312static void internal_add_vtimer(struct vtimer_list *timer)
313{
314 unsigned long flags;
315 __u64 done;
316 struct vtimer_list *event;
317 struct vtimer_queue *vt_list;
318
319 vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
320 spin_lock_irqsave(&vt_list->lock, flags);
321
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200322 BUG_ON(timer->cpu != smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* if list is empty we only have to set the timer */
325 if (list_empty(&vt_list->list)) {
326 /* reset the offset, this may happen if the last timer was
327 * just deleted by mod_virt_timer and the interrupt
328 * didn't happen until here
329 */
330 vt_list->offset = 0;
331 goto fire;
332 }
333
334 /* save progress */
335 asm volatile ("STPT %0" : "=m" (done));
336
337 /* calculate completed work */
338 done = vt_list->to_expire - done + vt_list->offset;
339 vt_list->offset = 0;
340
341 list_for_each_entry(event, &vt_list->list, entry)
342 event->expires -= done;
343
344 fire:
345 list_add_sorted(timer, &vt_list->list);
346
347 /* get first element, which is the next vtimer slice */
348 event = list_entry(vt_list->list.next, struct vtimer_list, entry);
349
350 set_vtimer(event->expires);
351 spin_unlock_irqrestore(&vt_list->lock, flags);
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200352 /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 put_cpu();
354}
355
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200356static inline void prepare_vtimer(struct vtimer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200358 BUG_ON(!timer->function);
359 BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
360 BUG_ON(vtimer_pending(timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 timer->cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364/*
365 * add_virt_timer - add an oneshot virtual CPU timer
366 */
367void add_virt_timer(void *new)
368{
369 struct vtimer_list *timer;
370
371 timer = (struct vtimer_list *)new;
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200372 prepare_vtimer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 timer->interval = 0;
374 internal_add_vtimer(timer);
375}
376EXPORT_SYMBOL(add_virt_timer);
377
378/*
379 * add_virt_timer_int - add an interval virtual CPU timer
380 */
381void add_virt_timer_periodic(void *new)
382{
383 struct vtimer_list *timer;
384
385 timer = (struct vtimer_list *)new;
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200386 prepare_vtimer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 timer->interval = timer->expires;
388 internal_add_vtimer(timer);
389}
390EXPORT_SYMBOL(add_virt_timer_periodic);
391
392/*
393 * If we change a pending timer the function must be called on the CPU
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200394 * where the timer is running on, e.g. by smp_call_function_single()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
396 * The original mod_timer adds the timer if it is not pending. For compatibility
397 * we do the same. The timer will be added on the current CPU as a oneshot timer.
398 *
399 * returns whether it has modified a pending timer (1) or not (0)
400 */
401int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
402{
403 struct vtimer_queue *vt_list;
404 unsigned long flags;
405 int cpu;
406
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200407 BUG_ON(!timer->function);
408 BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * This is a common optimization triggered by the
412 * networking code - if the timer is re-modified
413 * to be the same thing then just return:
414 */
415 if (timer->expires == expires && vtimer_pending(timer))
416 return 1;
417
418 cpu = get_cpu();
419 vt_list = &per_cpu(virt_cpu_timer, cpu);
420
Martin Schwidefskyca366a32008-07-14 09:59:23 +0200421 /* check if we run on the right CPU */
422 BUG_ON(timer->cpu != cpu);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* disable interrupts before test if timer is pending */
425 spin_lock_irqsave(&vt_list->lock, flags);
426
427 /* if timer isn't pending add it on the current CPU */
428 if (!vtimer_pending(timer)) {
429 spin_unlock_irqrestore(&vt_list->lock, flags);
430 /* we do not activate an interval timer with mod_virt_timer */
431 timer->interval = 0;
432 timer->expires = expires;
433 timer->cpu = cpu;
434 internal_add_vtimer(timer);
435 return 0;
436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 list_del_init(&timer->entry);
439 timer->expires = expires;
440
441 /* also change the interval if we have an interval timer */
442 if (timer->interval)
443 timer->interval = expires;
444
445 /* the timer can't expire anymore so we can release the lock */
446 spin_unlock_irqrestore(&vt_list->lock, flags);
447 internal_add_vtimer(timer);
448 return 1;
449}
450EXPORT_SYMBOL(mod_virt_timer);
451
452/*
453 * delete a virtual timer
454 *
455 * returns whether the deleted timer was pending (1) or not (0)
456 */
457int del_virt_timer(struct vtimer_list *timer)
458{
459 unsigned long flags;
460 struct vtimer_queue *vt_list;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* check if timer is pending */
463 if (!vtimer_pending(timer))
464 return 0;
465
466 vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
467 spin_lock_irqsave(&vt_list->lock, flags);
468
469 /* we don't interrupt a running timer, just let it expire! */
470 list_del_init(&timer->entry);
471
472 /* last timer removed */
473 if (list_empty(&vt_list->list)) {
474 vt_list->to_expire = 0;
475 vt_list->offset = 0;
476 }
477
478 spin_unlock_irqrestore(&vt_list->lock, flags);
479 return 1;
480}
481EXPORT_SYMBOL(del_virt_timer);
482
483/*
484 * Start the virtual CPU timer on the current CPU.
485 */
486void init_cpu_vtimer(void)
487{
488 struct vtimer_queue *vt_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* kick the virtual timer */
491 S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
492 S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
493 asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
494 asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100495
496 /* enable cpu timer interrupts */
497 __ctl_set_bit(0,10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Jan Glauberdb77aa52007-04-27 16:01:55 +0200499 vt_list = &__get_cpu_var(virt_cpu_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 INIT_LIST_HEAD(&vt_list->list);
501 spin_lock_init(&vt_list->lock);
502 vt_list->to_expire = 0;
503 vt_list->offset = 0;
504 vt_list->idle = 0;
505
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508void __init vtime_init(void)
509{
510 /* request the cpu timer external interrupt */
511 if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
512 &ext_int_info_timer) != 0)
513 panic("Couldn't request external interrupt 0x1005");
514
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100515 /* Enable cpu timer interrupts on the boot cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 init_cpu_vtimer();
517}
518