Merge branch 'topic/usb-audio' into for-linus
[sfrench/cifs-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/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>
25 #include <asm/irq_regs.h>
26 #include <asm/cpu.h>
27
28 static ext_int_info_t ext_int_info_timer;
29
30 static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
31
32 DEFINE_PER_CPU(struct s390_idle_data, s390_idle) = {
33         .lock = __SPIN_LOCK_UNLOCKED(s390_idle.lock)
34 };
35
36 static inline __u64 get_vtimer(void)
37 {
38         __u64 timer;
39
40         asm volatile("STPT %0" : "=m" (timer));
41         return timer;
42 }
43
44 static inline void set_vtimer(__u64 expires)
45 {
46         __u64 timer;
47
48         asm volatile ("  STPT %0\n"  /* Store current cpu timer value */
49                       "  SPT %1"     /* Set new value immediatly afterwards */
50                       : "=m" (timer) : "m" (expires) );
51         S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
52         S390_lowcore.last_update_timer = expires;
53 }
54
55 /*
56  * Update process times based on virtual cpu times stored by entry.S
57  * to the lowcore fields user_timer, system_timer & steal_clock.
58  */
59 static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
60 {
61         struct thread_info *ti = task_thread_info(tsk);
62         __u64 timer, clock, user, system, steal;
63
64         timer = S390_lowcore.last_update_timer;
65         clock = S390_lowcore.last_update_clock;
66         asm volatile ("  STPT %0\n"    /* Store current cpu timer value */
67                       "  STCK %1"      /* Store current tod clock value */
68                       : "=m" (S390_lowcore.last_update_timer),
69                         "=m" (S390_lowcore.last_update_clock) );
70         S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
71         S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
72
73         user = S390_lowcore.user_timer - ti->user_timer;
74         S390_lowcore.steal_timer -= user;
75         ti->user_timer = S390_lowcore.user_timer;
76         account_user_time(tsk, user, user);
77
78         system = S390_lowcore.system_timer - ti->system_timer;
79         S390_lowcore.steal_timer -= system;
80         ti->system_timer = S390_lowcore.system_timer;
81         account_system_time(tsk, hardirq_offset, system, system);
82
83         steal = S390_lowcore.steal_timer;
84         if ((s64) steal > 0) {
85                 S390_lowcore.steal_timer = 0;
86                 account_steal_time(steal);
87         }
88 }
89
90 void account_vtime(struct task_struct *prev, struct task_struct *next)
91 {
92         struct thread_info *ti;
93
94         do_account_vtime(prev, 0);
95         ti = task_thread_info(prev);
96         ti->user_timer = S390_lowcore.user_timer;
97         ti->system_timer = S390_lowcore.system_timer;
98         ti = task_thread_info(next);
99         S390_lowcore.user_timer = ti->user_timer;
100         S390_lowcore.system_timer = ti->system_timer;
101 }
102
103 void account_process_tick(struct task_struct *tsk, int user_tick)
104 {
105         do_account_vtime(tsk, HARDIRQ_OFFSET);
106 }
107
108 /*
109  * Update process times based on virtual cpu times stored by entry.S
110  * to the lowcore fields user_timer, system_timer & steal_clock.
111  */
112 void account_system_vtime(struct task_struct *tsk)
113 {
114         struct thread_info *ti = task_thread_info(tsk);
115         __u64 timer, system;
116
117         timer = S390_lowcore.last_update_timer;
118         S390_lowcore.last_update_timer = get_vtimer();
119         S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
120
121         system = S390_lowcore.system_timer - ti->system_timer;
122         S390_lowcore.steal_timer -= system;
123         ti->system_timer = S390_lowcore.system_timer;
124         account_system_time(tsk, 0, system, system);
125 }
126 EXPORT_SYMBOL_GPL(account_system_vtime);
127
128 void vtime_start_cpu(void)
129 {
130         struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
131         struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
132         __u64 idle_time, expires;
133
134         /* Account time spent with enabled wait psw loaded as idle time. */
135         idle_time = S390_lowcore.int_clock - idle->idle_enter;
136         account_idle_time(idle_time);
137         S390_lowcore.last_update_clock = S390_lowcore.int_clock;
138
139         /* Account system time spent going idle. */
140         S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle;
141         S390_lowcore.last_update_timer = S390_lowcore.async_enter_timer;
142
143         /* Restart vtime CPU timer */
144         if (vq->do_spt) {
145                 /* Program old expire value but first save progress. */
146                 expires = vq->idle - S390_lowcore.async_enter_timer;
147                 expires += get_vtimer();
148                 set_vtimer(expires);
149         } else {
150                 /* Don't account the CPU timer delta while the cpu was idle. */
151                 vq->elapsed -= vq->idle - S390_lowcore.async_enter_timer;
152         }
153
154         spin_lock(&idle->lock);
155         idle->idle_time += idle_time;
156         idle->idle_enter = 0ULL;
157         idle->idle_count++;
158         spin_unlock(&idle->lock);
159 }
160
161 void vtime_stop_cpu(void)
162 {
163         struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
164         struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
165         psw_t psw;
166
167         /* Wait for external, I/O or machine check interrupt. */
168         psw.mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_IO | PSW_MASK_EXT;
169
170         /* Check if the CPU timer needs to be reprogrammed. */
171         if (vq->do_spt) {
172                 __u64 vmax = VTIMER_MAX_SLICE;
173                 /*
174                  * The inline assembly is equivalent to
175                  *      vq->idle = get_cpu_timer();
176                  *      set_cpu_timer(VTIMER_MAX_SLICE);
177                  *      idle->idle_enter = get_clock();
178                  *      __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
179                  *                         PSW_MASK_IO | PSW_MASK_EXT);
180                  * The difference is that the inline assembly makes sure that
181                  * the last three instruction are stpt, stck and lpsw in that
182                  * order. This is done to increase the precision.
183                  */
184                 asm volatile(
185 #ifndef CONFIG_64BIT
186                         "       basr    1,0\n"
187                         "0:     ahi     1,1f-0b\n"
188                         "       st      1,4(%2)\n"
189 #else /* CONFIG_64BIT */
190                         "       larl    1,1f\n"
191                         "       stg     1,8(%2)\n"
192 #endif /* CONFIG_64BIT */
193                         "       stpt    0(%4)\n"
194                         "       spt     0(%5)\n"
195                         "       stck    0(%3)\n"
196 #ifndef CONFIG_64BIT
197                         "       lpsw    0(%2)\n"
198 #else /* CONFIG_64BIT */
199                         "       lpswe   0(%2)\n"
200 #endif /* CONFIG_64BIT */
201                         "1:"
202                         : "=m" (idle->idle_enter), "=m" (vq->idle)
203                         : "a" (&psw), "a" (&idle->idle_enter),
204                           "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw)
205                         : "memory", "cc", "1");
206         } else {
207                 /*
208                  * The inline assembly is equivalent to
209                  *      vq->idle = get_cpu_timer();
210                  *      idle->idle_enter = get_clock();
211                  *      __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
212                  *                         PSW_MASK_IO | PSW_MASK_EXT);
213                  * The difference is that the inline assembly makes sure that
214                  * the last three instruction are stpt, stck and lpsw in that
215                  * order. This is done to increase the precision.
216                  */
217                 asm volatile(
218 #ifndef CONFIG_64BIT
219                         "       basr    1,0\n"
220                         "0:     ahi     1,1f-0b\n"
221                         "       st      1,4(%2)\n"
222 #else /* CONFIG_64BIT */
223                         "       larl    1,1f\n"
224                         "       stg     1,8(%2)\n"
225 #endif /* CONFIG_64BIT */
226                         "       stpt    0(%4)\n"
227                         "       stck    0(%3)\n"
228 #ifndef CONFIG_64BIT
229                         "       lpsw    0(%2)\n"
230 #else /* CONFIG_64BIT */
231                         "       lpswe   0(%2)\n"
232 #endif /* CONFIG_64BIT */
233                         "1:"
234                         : "=m" (idle->idle_enter), "=m" (vq->idle)
235                         : "a" (&psw), "a" (&idle->idle_enter),
236                           "a" (&vq->idle), "m" (psw)
237                         : "memory", "cc", "1");
238         }
239 }
240
241 /*
242  * Sorted add to a list. List is linear searched until first bigger
243  * element is found.
244  */
245 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
246 {
247         struct vtimer_list *event;
248
249         list_for_each_entry(event, head, entry) {
250                 if (event->expires > timer->expires) {
251                         list_add_tail(&timer->entry, &event->entry);
252                         return;
253                 }
254         }
255         list_add_tail(&timer->entry, head);
256 }
257
258 /*
259  * Do the callback functions of expired vtimer events.
260  * Called from within the interrupt handler.
261  */
262 static void do_callbacks(struct list_head *cb_list)
263 {
264         struct vtimer_queue *vq;
265         struct vtimer_list *event, *tmp;
266
267         if (list_empty(cb_list))
268                 return;
269
270         vq = &__get_cpu_var(virt_cpu_timer);
271
272         list_for_each_entry_safe(event, tmp, cb_list, entry) {
273                 list_del_init(&event->entry);
274                 (event->function)(event->data);
275                 if (event->interval) {
276                         /* Recharge interval timer */
277                         event->expires = event->interval + vq->elapsed;
278                         spin_lock(&vq->lock);
279                         list_add_sorted(event, &vq->list);
280                         spin_unlock(&vq->lock);
281                 }
282         }
283 }
284
285 /*
286  * Handler for the virtual CPU timer.
287  */
288 static void do_cpu_timer_interrupt(__u16 error_code)
289 {
290         struct vtimer_queue *vq;
291         struct vtimer_list *event, *tmp;
292         struct list_head cb_list;       /* the callback queue */
293         __u64 elapsed, next;
294
295         INIT_LIST_HEAD(&cb_list);
296         vq = &__get_cpu_var(virt_cpu_timer);
297
298         /* walk timer list, fire all expired events */
299         spin_lock(&vq->lock);
300
301         elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
302         BUG_ON((s64) elapsed < 0);
303         vq->elapsed = 0;
304         list_for_each_entry_safe(event, tmp, &vq->list, entry) {
305                 if (event->expires < elapsed)
306                         /* move expired timer to the callback queue */
307                         list_move_tail(&event->entry, &cb_list);
308                 else
309                         event->expires -= elapsed;
310         }
311         spin_unlock(&vq->lock);
312
313         vq->do_spt = list_empty(&cb_list);
314         do_callbacks(&cb_list);
315
316         /* next event is first in list */
317         next = VTIMER_MAX_SLICE;
318         spin_lock(&vq->lock);
319         if (!list_empty(&vq->list)) {
320                 event = list_first_entry(&vq->list, struct vtimer_list, entry);
321                 next = event->expires;
322         } else
323                 vq->do_spt = 0;
324         spin_unlock(&vq->lock);
325         /*
326          * To improve precision add the time spent by the
327          * interrupt handler to the elapsed time.
328          * Note: CPU timer counts down and we got an interrupt,
329          *       the current content is negative
330          */
331         elapsed = S390_lowcore.async_enter_timer - get_vtimer();
332         set_vtimer(next - elapsed);
333         vq->timer = next - elapsed;
334         vq->elapsed = elapsed;
335 }
336
337 void init_virt_timer(struct vtimer_list *timer)
338 {
339         timer->function = NULL;
340         INIT_LIST_HEAD(&timer->entry);
341 }
342 EXPORT_SYMBOL(init_virt_timer);
343
344 static inline int vtimer_pending(struct vtimer_list *timer)
345 {
346         return (!list_empty(&timer->entry));
347 }
348
349 /*
350  * this function should only run on the specified CPU
351  */
352 static void internal_add_vtimer(struct vtimer_list *timer)
353 {
354         struct vtimer_queue *vq;
355         unsigned long flags;
356         __u64 left, expires;
357
358         vq = &per_cpu(virt_cpu_timer, timer->cpu);
359         spin_lock_irqsave(&vq->lock, flags);
360
361         BUG_ON(timer->cpu != smp_processor_id());
362
363         if (list_empty(&vq->list)) {
364                 /* First timer on this cpu, just program it. */
365                 list_add(&timer->entry, &vq->list);
366                 set_vtimer(timer->expires);
367                 vq->timer = timer->expires;
368                 vq->elapsed = 0;
369         } else {
370                 /* Check progress of old timers. */
371                 expires = timer->expires;
372                 left = get_vtimer();
373                 if (likely((s64) expires < (s64) left)) {
374                         /* The new timer expires before the current timer. */
375                         set_vtimer(expires);
376                         vq->elapsed += vq->timer - left;
377                         vq->timer = expires;
378                 } else {
379                         vq->elapsed += vq->timer - left;
380                         vq->timer = left;
381                 }
382                 /* Insert new timer into per cpu list. */
383                 timer->expires += vq->elapsed;
384                 list_add_sorted(timer, &vq->list);
385         }
386
387         spin_unlock_irqrestore(&vq->lock, flags);
388         /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
389         put_cpu();
390 }
391
392 static inline void prepare_vtimer(struct vtimer_list *timer)
393 {
394         BUG_ON(!timer->function);
395         BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
396         BUG_ON(vtimer_pending(timer));
397         timer->cpu = get_cpu();
398 }
399
400 /*
401  * add_virt_timer - add an oneshot virtual CPU timer
402  */
403 void add_virt_timer(void *new)
404 {
405         struct vtimer_list *timer;
406
407         timer = (struct vtimer_list *)new;
408         prepare_vtimer(timer);
409         timer->interval = 0;
410         internal_add_vtimer(timer);
411 }
412 EXPORT_SYMBOL(add_virt_timer);
413
414 /*
415  * add_virt_timer_int - add an interval virtual CPU timer
416  */
417 void add_virt_timer_periodic(void *new)
418 {
419         struct vtimer_list *timer;
420
421         timer = (struct vtimer_list *)new;
422         prepare_vtimer(timer);
423         timer->interval = timer->expires;
424         internal_add_vtimer(timer);
425 }
426 EXPORT_SYMBOL(add_virt_timer_periodic);
427
428 /*
429  * If we change a pending timer the function must be called on the CPU
430  * where the timer is running on, e.g. by smp_call_function_single()
431  *
432  * The original mod_timer adds the timer if it is not pending. For
433  * compatibility we do the same. The timer will be added on the current
434  * CPU as a oneshot timer.
435  *
436  * returns whether it has modified a pending timer (1) or not (0)
437  */
438 int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
439 {
440         struct vtimer_queue *vq;
441         unsigned long flags;
442         int cpu;
443
444         BUG_ON(!timer->function);
445         BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
446
447         /*
448          * This is a common optimization triggered by the
449          * networking code - if the timer is re-modified
450          * to be the same thing then just return:
451          */
452         if (timer->expires == expires && vtimer_pending(timer))
453                 return 1;
454
455         cpu = get_cpu();
456         vq = &per_cpu(virt_cpu_timer, cpu);
457
458         /* check if we run on the right CPU */
459         BUG_ON(timer->cpu != cpu);
460
461         /* disable interrupts before test if timer is pending */
462         spin_lock_irqsave(&vq->lock, flags);
463
464         /* if timer isn't pending add it on the current CPU */
465         if (!vtimer_pending(timer)) {
466                 spin_unlock_irqrestore(&vq->lock, flags);
467                 /* we do not activate an interval timer with mod_virt_timer */
468                 timer->interval = 0;
469                 timer->expires = expires;
470                 timer->cpu = cpu;
471                 internal_add_vtimer(timer);
472                 return 0;
473         }
474
475         list_del_init(&timer->entry);
476         timer->expires = expires;
477
478         /* also change the interval if we have an interval timer */
479         if (timer->interval)
480                 timer->interval = expires;
481
482         /* the timer can't expire anymore so we can release the lock */
483         spin_unlock_irqrestore(&vq->lock, flags);
484         internal_add_vtimer(timer);
485         return 1;
486 }
487 EXPORT_SYMBOL(mod_virt_timer);
488
489 /*
490  * delete a virtual timer
491  *
492  * returns whether the deleted timer was pending (1) or not (0)
493  */
494 int del_virt_timer(struct vtimer_list *timer)
495 {
496         unsigned long flags;
497         struct vtimer_queue *vq;
498
499         /* check if timer is pending */
500         if (!vtimer_pending(timer))
501                 return 0;
502
503         vq = &per_cpu(virt_cpu_timer, timer->cpu);
504         spin_lock_irqsave(&vq->lock, flags);
505
506         /* we don't interrupt a running timer, just let it expire! */
507         list_del_init(&timer->entry);
508
509         spin_unlock_irqrestore(&vq->lock, flags);
510         return 1;
511 }
512 EXPORT_SYMBOL(del_virt_timer);
513
514 /*
515  * Start the virtual CPU timer on the current CPU.
516  */
517 void init_cpu_vtimer(void)
518 {
519         struct thread_info *ti = current_thread_info();
520         struct vtimer_queue *vq;
521
522         S390_lowcore.user_timer = ti->user_timer;
523         S390_lowcore.system_timer = ti->system_timer;
524
525         /* kick the virtual timer */
526         asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
527         asm volatile ("STPT %0" : "=m" (S390_lowcore.last_update_timer));
528
529         /* initialize per cpu vtimer structure */
530         vq = &__get_cpu_var(virt_cpu_timer);
531         INIT_LIST_HEAD(&vq->list);
532         spin_lock_init(&vq->lock);
533
534         /* enable cpu timer interrupts */
535         __ctl_set_bit(0,10);
536 }
537
538 void __init vtime_init(void)
539 {
540         /* request the cpu timer external interrupt */
541         if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
542                                               &ext_int_info_timer) != 0)
543                 panic("Couldn't request external interrupt 0x1005");
544
545         /* Enable cpu timer interrupts on the boot cpu. */
546         init_cpu_vtimer();
547 }
548