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