vserver 1.9.5.x5
[linux-2.6.git] / arch / s390 / kernel / vtime.c
1 /*
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
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
23 #include <asm/s390_ext.h>
24 #include <asm/timer.h>
25
26 #define VTIMER_MAGIC (TIMER_MAGIC + 1)
27 static ext_int_info_t ext_int_info_timer;
28 DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
29
30 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
31 /*
32  * Update process times based on virtual cpu times stored by entry.S
33  * to the lowcore fields user_timer, system_timer & steal_clock.
34  */
35 void account_user_vtime(struct task_struct *tsk)
36 {
37         cputime_t cputime;
38         __u64 timer, clock;
39         int rcu_user_flag;
40
41         timer = S390_lowcore.last_update_timer;
42         clock = S390_lowcore.last_update_clock;
43         asm volatile ("  STPT %0\n"    /* Store current cpu timer value */
44                       "  STCK %1"      /* Store current tod clock value */
45                       : "=m" (S390_lowcore.last_update_timer),
46                         "=m" (S390_lowcore.last_update_clock) );
47         S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
48         S390_lowcore.steal_clock += S390_lowcore.last_update_clock - clock;
49
50         cputime = S390_lowcore.user_timer >> 12;
51         rcu_user_flag = cputime != 0;
52         S390_lowcore.user_timer -= cputime << 12;
53         S390_lowcore.steal_clock -= cputime << 12;
54         account_user_time(tsk, cputime);
55
56         cputime =  S390_lowcore.system_timer >> 12;
57         S390_lowcore.system_timer -= cputime << 12;
58         S390_lowcore.steal_clock -= cputime << 12;
59         account_system_time(tsk, HARDIRQ_OFFSET, cputime);
60
61         cputime = S390_lowcore.steal_clock;
62         if ((__s64) cputime > 0) {
63                 cputime >>= 12;
64                 S390_lowcore.steal_clock -= cputime << 12;
65                 account_steal_time(tsk, cputime);
66         }
67
68         run_local_timers();
69         if (rcu_pending(smp_processor_id()))
70                 rcu_check_callbacks(smp_processor_id(), rcu_user_flag);
71         scheduler_tick();
72 }
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  */
78 void account_system_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.system_timer >> 12;
89         S390_lowcore.system_timer -= cputime << 12;
90         S390_lowcore.steal_clock -= cputime << 12;
91         account_system_time(tsk, 0, cputime);
92 }
93
94 static inline void set_vtimer(__u64 expires)
95 {
96         __u64 timer;
97
98         asm volatile ("  STPT %0\n"  /* Store current cpu timer value */
99                       "  SPT %1"     /* Set new value immediatly afterwards */
100                       : "=m" (timer) : "m" (expires) );
101         S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
102         S390_lowcore.last_update_timer = expires;
103
104         /* store expire time for this CPU timer */
105         per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
106 }
107 #else
108 static inline void set_vtimer(__u64 expires)
109 {
110         S390_lowcore.last_update_timer = expires;
111         asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
112
113         /* store expire time for this CPU timer */
114         per_cpu(virt_cpu_timer, smp_processor_id()).to_expire = expires;
115 }
116 #endif
117
118 static void start_cpu_timer(void)
119 {
120         struct vtimer_queue *vt_list;
121
122         vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
123         set_vtimer(vt_list->idle);
124 }
125
126 static void stop_cpu_timer(void)
127 {
128         __u64 done;
129         struct vtimer_queue *vt_list;
130
131         vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
132
133         /* nothing to do */
134         if (list_empty(&vt_list->list)) {
135                 vt_list->idle = VTIMER_MAX_SLICE;
136                 goto fire;
137         }
138
139         /* store progress */
140         asm volatile ("STPT %0" : "=m" (done));
141
142         /*
143          * If done is negative we do not stop the CPU timer
144          * because we will get instantly an interrupt that
145          * will start the CPU timer again.
146          */
147         if (done & 1LL<<63)
148                 return;
149         else
150                 vt_list->offset += vt_list->to_expire - done;
151
152         /* save the actual expire value */
153         vt_list->idle = done;
154
155         /*
156          * We cannot halt the CPU timer, we just write a value that
157          * nearly never expires (only after 71 years) and re-write
158          * the stored expire value if we continue the timer
159          */
160  fire:
161         set_vtimer(VTIMER_MAX_SLICE);
162 }
163
164 /*
165  * Sorted add to a list. List is linear searched until first bigger
166  * element is found.
167  */
168 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
169 {
170         struct vtimer_list *event;
171
172         list_for_each_entry(event, head, entry) {
173                 if (event->expires > timer->expires) {
174                         list_add_tail(&timer->entry, &event->entry);
175                         return;
176                 }
177         }
178         list_add_tail(&timer->entry, head);
179 }
180
181 /*
182  * Do the callback functions of expired vtimer events.
183  * Called from within the interrupt handler.
184  */
185 static void do_callbacks(struct list_head *cb_list, struct pt_regs *regs)
186 {
187         struct vtimer_queue *vt_list;
188         struct vtimer_list *event, *tmp;
189         void (*fn)(unsigned long, struct pt_regs*);
190         unsigned long data;
191
192         if (list_empty(cb_list))
193                 return;
194
195         vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
196
197         list_for_each_entry_safe(event, tmp, cb_list, entry) {
198                 fn = event->function;
199                 data = event->data;
200                 fn(data, regs);
201
202                 if (!event->interval)
203                         /* delete one shot timer */
204                         list_del_init(&event->entry);
205                 else {
206                         /* move interval timer back to list */
207                         spin_lock(&vt_list->lock);
208                         list_del_init(&event->entry);
209                         list_add_sorted(event, &vt_list->list);
210                         spin_unlock(&vt_list->lock);
211                 }
212         }
213 }
214
215 /*
216  * Handler for the virtual CPU timer.
217  */
218 static void do_cpu_timer_interrupt(struct pt_regs *regs, __u16 error_code)
219 {
220         int cpu;
221         __u64 next, delta;
222         struct vtimer_queue *vt_list;
223         struct vtimer_list *event, *tmp;
224         struct list_head *ptr;
225         /* the callback queue */
226         struct list_head cb_list;
227
228         INIT_LIST_HEAD(&cb_list);
229         cpu = smp_processor_id();
230         vt_list = &per_cpu(virt_cpu_timer, cpu);
231
232         /* walk timer list, fire all expired events */
233         spin_lock(&vt_list->lock);
234
235         if (vt_list->to_expire < VTIMER_MAX_SLICE)
236                 vt_list->offset += vt_list->to_expire;
237
238         list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
239                 if (event->expires > vt_list->offset)
240                         /* found first unexpired event, leave */
241                         break;
242
243                 /* re-charge interval timer, we have to add the offset */
244                 if (event->interval)
245                         event->expires = event->interval + vt_list->offset;
246
247                 /* move expired timer to the callback queue */
248                 list_move_tail(&event->entry, &cb_list);
249         }
250         spin_unlock(&vt_list->lock);
251         do_callbacks(&cb_list, regs);
252
253         /* next event is first in list */
254         spin_lock(&vt_list->lock);
255         if (!list_empty(&vt_list->list)) {
256                 ptr = vt_list->list.next;
257                 event = list_entry(ptr, struct vtimer_list, entry);
258                 next = event->expires - vt_list->offset;
259
260                 /* add the expired time from this interrupt handler
261                  * and the callback functions
262                  */
263                 asm volatile ("STPT %0" : "=m" (delta));
264                 delta = 0xffffffffffffffffLL - delta + 1;
265                 vt_list->offset += delta;
266                 next -= delta;
267         } else {
268                 vt_list->offset = 0;
269                 next = VTIMER_MAX_SLICE;
270         }
271         spin_unlock(&vt_list->lock);
272         set_vtimer(next);
273 }
274
275 void init_virt_timer(struct vtimer_list *timer)
276 {
277         timer->magic = VTIMER_MAGIC;
278         timer->function = NULL;
279         INIT_LIST_HEAD(&timer->entry);
280         spin_lock_init(&timer->lock);
281 }
282 EXPORT_SYMBOL(init_virt_timer);
283
284 static inline int check_vtimer(struct vtimer_list *timer)
285 {
286         if (timer->magic != VTIMER_MAGIC)
287                 return -EINVAL;
288         return 0;
289 }
290
291 static inline int vtimer_pending(struct vtimer_list *timer)
292 {
293         return (!list_empty(&timer->entry));
294 }
295
296 /*
297  * this function should only run on the specified CPU
298  */
299 static void internal_add_vtimer(struct vtimer_list *timer)
300 {
301         unsigned long flags;
302         __u64 done;
303         struct vtimer_list *event;
304         struct vtimer_queue *vt_list;
305
306         vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
307         spin_lock_irqsave(&vt_list->lock, flags);
308
309         if (timer->cpu != smp_processor_id())
310                 printk("internal_add_vtimer: BUG, running on wrong CPU");
311
312         /* if list is empty we only have to set the timer */
313         if (list_empty(&vt_list->list)) {
314                 /* reset the offset, this may happen if the last timer was
315                  * just deleted by mod_virt_timer and the interrupt
316                  * didn't happen until here
317                  */
318                 vt_list->offset = 0;
319                 goto fire;
320         }
321
322         /* save progress */
323         asm volatile ("STPT %0" : "=m" (done));
324
325         /* calculate completed work */
326         done = vt_list->to_expire - done + vt_list->offset;
327         vt_list->offset = 0;
328
329         list_for_each_entry(event, &vt_list->list, entry)
330                 event->expires -= done;
331
332  fire:
333         list_add_sorted(timer, &vt_list->list);
334
335         /* get first element, which is the next vtimer slice */
336         event = list_entry(vt_list->list.next, struct vtimer_list, entry);
337
338         set_vtimer(event->expires);
339         spin_unlock_irqrestore(&vt_list->lock, flags);
340         /* release CPU aquired in prepare_vtimer or mod_virt_timer() */
341         put_cpu();
342 }
343
344 static inline int prepare_vtimer(struct vtimer_list *timer)
345 {
346         if (check_vtimer(timer) || !timer->function) {
347                 printk("add_virt_timer: uninitialized timer\n");
348                 return -EINVAL;
349         }
350
351         if (!timer->expires || timer->expires > VTIMER_MAX_SLICE) {
352                 printk("add_virt_timer: invalid timer expire value!\n");
353                 return -EINVAL;
354         }
355
356         if (vtimer_pending(timer)) {
357                 printk("add_virt_timer: timer pending\n");
358                 return -EBUSY;
359         }
360
361         timer->cpu = get_cpu();
362         return 0;
363 }
364
365 /*
366  * add_virt_timer - add an oneshot virtual CPU timer
367  */
368 void add_virt_timer(void *new)
369 {
370         struct vtimer_list *timer;
371
372         timer = (struct vtimer_list *)new;
373
374         if (prepare_vtimer(timer) < 0)
375                 return;
376
377         timer->interval = 0;
378         internal_add_vtimer(timer);
379 }
380 EXPORT_SYMBOL(add_virt_timer);
381
382 /*
383  * add_virt_timer_int - add an interval virtual CPU timer
384  */
385 void add_virt_timer_periodic(void *new)
386 {
387         struct vtimer_list *timer;
388
389         timer = (struct vtimer_list *)new;
390
391         if (prepare_vtimer(timer) < 0)
392                 return;
393
394         timer->interval = timer->expires;
395         internal_add_vtimer(timer);
396 }
397 EXPORT_SYMBOL(add_virt_timer_periodic);
398
399 /*
400  * If we change a pending timer the function must be called on the CPU
401  * where the timer is running on, e.g. by smp_call_function_on()
402  *
403  * The original mod_timer adds the timer if it is not pending. For compatibility
404  * we do the same. The timer will be added on the current CPU as a oneshot timer.
405  *
406  * returns whether it has modified a pending timer (1) or not (0)
407  */
408 int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
409 {
410         struct vtimer_queue *vt_list;
411         unsigned long flags;
412         int cpu;
413
414         if (check_vtimer(timer) || !timer->function) {
415                 printk("mod_virt_timer: uninitialized timer\n");
416                 return  -EINVAL;
417         }
418
419         if (!expires || expires > VTIMER_MAX_SLICE) {
420                 printk("mod_virt_timer: invalid expire range\n");
421                 return -EINVAL;
422         }
423
424         /*
425          * This is a common optimization triggered by the
426          * networking code - if the timer is re-modified
427          * to be the same thing then just return:
428          */
429         if (timer->expires == expires && vtimer_pending(timer))
430                 return 1;
431
432         cpu = get_cpu();
433         vt_list = &per_cpu(virt_cpu_timer, cpu);
434
435         /* disable interrupts before test if timer is pending */
436         spin_lock_irqsave(&vt_list->lock, flags);
437
438         /* if timer isn't pending add it on the current CPU */
439         if (!vtimer_pending(timer)) {
440                 spin_unlock_irqrestore(&vt_list->lock, flags);
441                 /* we do not activate an interval timer with mod_virt_timer */
442                 timer->interval = 0;
443                 timer->expires = expires;
444                 timer->cpu = cpu;
445                 internal_add_vtimer(timer);
446                 return 0;
447         }
448
449         /* check if we run on the right CPU */
450         if (timer->cpu != cpu) {
451                 printk("mod_virt_timer: running on wrong CPU, check your code\n");
452                 spin_unlock_irqrestore(&vt_list->lock, flags);
453                 put_cpu();
454                 return -EINVAL;
455         }
456
457         list_del_init(&timer->entry);
458         timer->expires = expires;
459
460         /* also change the interval if we have an interval timer */
461         if (timer->interval)
462                 timer->interval = expires;
463
464         /* the timer can't expire anymore so we can release the lock */
465         spin_unlock_irqrestore(&vt_list->lock, flags);
466         internal_add_vtimer(timer);
467         return 1;
468 }
469 EXPORT_SYMBOL(mod_virt_timer);
470
471 /*
472  * delete a virtual timer
473  *
474  * returns whether the deleted timer was pending (1) or not (0)
475  */
476 int del_virt_timer(struct vtimer_list *timer)
477 {
478         unsigned long flags;
479         struct vtimer_queue *vt_list;
480
481         if (check_vtimer(timer)) {
482                 printk("del_virt_timer: timer not initialized\n");
483                 return -EINVAL;
484         }
485
486         /* check if timer is pending */
487         if (!vtimer_pending(timer))
488                 return 0;
489
490         vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
491         spin_lock_irqsave(&vt_list->lock, flags);
492
493         /* we don't interrupt a running timer, just let it expire! */
494         list_del_init(&timer->entry);
495
496         /* last timer removed */
497         if (list_empty(&vt_list->list)) {
498                 vt_list->to_expire = 0;
499                 vt_list->offset = 0;
500         }
501
502         spin_unlock_irqrestore(&vt_list->lock, flags);
503         return 1;
504 }
505 EXPORT_SYMBOL(del_virt_timer);
506
507 /*
508  * Start the virtual CPU timer on the current CPU.
509  */
510 void init_cpu_vtimer(void)
511 {
512         struct vtimer_queue *vt_list;
513         unsigned long cr0;
514
515         /* kick the virtual timer */
516         S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
517         S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
518         asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
519         asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
520         __ctl_store(cr0, 0, 0);
521         cr0 |= 0x400;
522         __ctl_load(cr0, 0, 0);
523
524         vt_list = &per_cpu(virt_cpu_timer, smp_processor_id());
525         INIT_LIST_HEAD(&vt_list->list);
526         spin_lock_init(&vt_list->lock);
527         vt_list->to_expire = 0;
528         vt_list->offset = 0;
529         vt_list->idle = 0;
530
531 }
532
533 static int vtimer_idle_notify(struct notifier_block *self,
534                               unsigned long action, void *hcpu)
535 {
536         switch (action) {
537         case CPU_IDLE:
538                 stop_cpu_timer();
539                 break;
540         case CPU_NOT_IDLE:
541                 start_cpu_timer();
542                 break;
543         }
544         return NOTIFY_OK;
545 }
546
547 static struct notifier_block vtimer_idle_nb = {
548         .notifier_call = vtimer_idle_notify,
549 };
550
551 void __init vtime_init(void)
552 {
553         /* request the cpu timer external interrupt */
554         if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
555                                               &ext_int_info_timer) != 0)
556                 panic("Couldn't request external interrupt 0x1005");
557
558         if (register_idle_notifier(&vtimer_idle_nb))
559                 panic("Couldn't register idle notifier");
560
561         init_cpu_vtimer();
562 }
563