vtime.c 15.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  arch/s390/kernel/vtime.c
 *    Virtual cpu timer based timer functions.
 *
 *  S390 version
 *    Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Jan Glauber <jan.glauber@de.ibm.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/smp.h>
#include <linux/types.h>
#include <linux/timex.h>
#include <linux/notifier.h>
#include <linux/kernel_stat.h>
#include <linux/rcupdate.h>
#include <linux/posix-timers.h>
22
#include <linux/cpu.h>
23
#include <linux/kprobes.h>
L
Linus Torvalds 已提交
24 25 26

#include <asm/s390_ext.h>
#include <asm/timer.h>
H
Heiko Carstens 已提交
27
#include <asm/irq_regs.h>
28
#include <asm/cputime.h>
L
Linus Torvalds 已提交
29

30
static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
L
Linus Torvalds 已提交
31

32
DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

static inline __u64 get_vtimer(void)
{
	__u64 timer;

	asm volatile("STPT %0" : "=m" (timer));
	return timer;
}

static inline void set_vtimer(__u64 expires)
{
	__u64 timer;

	asm volatile ("  STPT %0\n"  /* Store current cpu timer value */
		      "  SPT %1"     /* Set new value immediatly afterwards */
		      : "=m" (timer) : "m" (expires) );
	S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
	S390_lowcore.last_update_timer = expires;
}

L
Linus Torvalds 已提交
53 54 55 56
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
57
static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
L
Linus Torvalds 已提交
58
{
59 60
	struct thread_info *ti = task_thread_info(tsk);
	__u64 timer, clock, user, system, steal;
L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68

	timer = S390_lowcore.last_update_timer;
	clock = S390_lowcore.last_update_clock;
	asm volatile ("  STPT %0\n"    /* Store current cpu timer value */
		      "  STCK %1"      /* Store current tod clock value */
		      : "=m" (S390_lowcore.last_update_timer),
		        "=m" (S390_lowcore.last_update_clock) );
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
69
	S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
L
Linus Torvalds 已提交
70

71 72 73 74
	user = S390_lowcore.user_timer - ti->user_timer;
	S390_lowcore.steal_timer -= user;
	ti->user_timer = S390_lowcore.user_timer;
	account_user_time(tsk, user, user);
L
Linus Torvalds 已提交
75

76 77 78
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
79
	account_system_time(tsk, hardirq_offset, system, system);
L
Linus Torvalds 已提交
80

81 82 83
	steal = S390_lowcore.steal_timer;
	if ((s64) steal > 0) {
		S390_lowcore.steal_timer = 0;
84
		account_steal_time(steal);
L
Linus Torvalds 已提交
85 86 87
	}
}

88
void account_vtime(struct task_struct *prev, struct task_struct *next)
89
{
90 91 92 93 94 95 96 97 98 99
	struct thread_info *ti;

	do_account_vtime(prev, 0);
	ti = task_thread_info(prev);
	ti->user_timer = S390_lowcore.user_timer;
	ti->system_timer = S390_lowcore.system_timer;
	ti = task_thread_info(next);
	S390_lowcore.user_timer = ti->user_timer;
	S390_lowcore.system_timer = ti->system_timer;
}
100

101 102 103
void account_process_tick(struct task_struct *tsk, int user_tick)
{
	do_account_vtime(tsk, HARDIRQ_OFFSET);
104 105
}

L
Linus Torvalds 已提交
106 107 108 109 110 111
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
void account_system_vtime(struct task_struct *tsk)
{
112 113
	struct thread_info *ti = task_thread_info(tsk);
	__u64 timer, system;
L
Linus Torvalds 已提交
114 115

	timer = S390_lowcore.last_update_timer;
116
	S390_lowcore.last_update_timer = get_vtimer();
L
Linus Torvalds 已提交
117 118
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;

119 120 121
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
122
	account_system_time(tsk, 0, system, system);
L
Linus Torvalds 已提交
123
}
124
EXPORT_SYMBOL_GPL(account_system_vtime);
L
Linus Torvalds 已提交
125

126
void __kprobes vtime_start_cpu(__u64 int_clock, __u64 enter_timer)
L
Linus Torvalds 已提交
127
{
128
	struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
129
	struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
130 131
	__u64 idle_time, expires;

132 133 134
	if (idle->idle_enter == 0ULL)
		return;

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 139
	S390_lowcore.steal_timer +=
		idle->idle_enter - S390_lowcore.last_update_clock;
140
	S390_lowcore.last_update_clock = int_clock;
141 142 143

	/* Account system time spent going idle. */
	S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle;
144
	S390_lowcore.last_update_timer = enter_timer;
145 146 147 148

	/* Restart vtime CPU timer */
	if (vq->do_spt) {
		/* Program old expire value but first save progress. */
149
		expires = vq->idle - enter_timer;
150 151 152 153
		expires += get_vtimer();
		set_vtimer(expires);
	} else {
		/* Don't account the CPU timer delta while the cpu was idle. */
154
		vq->elapsed -= vq->idle - enter_timer;
155
	}
156

157 158
	idle->sequence++;
	smp_wmb();
159 160 161
	idle->idle_time += idle_time;
	idle->idle_enter = 0ULL;
	idle->idle_count++;
162 163
	smp_wmb();
	idle->sequence++;
L
Linus Torvalds 已提交
164 165
}

166
void __kprobes vtime_stop_cpu(void)
L
Linus Torvalds 已提交
167
{
168 169 170 171 172 173 174
	struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
	struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
	psw_t psw;

	/* Wait for external, I/O or machine check interrupt. */
	psw.mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_IO | PSW_MASK_EXT;

175 176
	idle->nohz_delay = 0;

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	/* Check if the CPU timer needs to be reprogrammed. */
	if (vq->do_spt) {
		__u64 vmax = VTIMER_MAX_SLICE;
		/*
		 * The inline assembly is equivalent to
		 *	vq->idle = get_cpu_timer();
		 *	set_cpu_timer(VTIMER_MAX_SLICE);
		 *	idle->idle_enter = get_clock();
		 *	__load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
		 *			   PSW_MASK_IO | PSW_MASK_EXT);
		 * The difference is that the inline assembly makes sure that
		 * the last three instruction are stpt, stck and lpsw in that
		 * order. This is done to increase the precision.
		 */
		asm volatile(
#ifndef CONFIG_64BIT
			"	basr	1,0\n"
			"0:	ahi	1,1f-0b\n"
			"	st	1,4(%2)\n"
#else /* CONFIG_64BIT */
			"	larl	1,1f\n"
			"	stg	1,8(%2)\n"
#endif /* CONFIG_64BIT */
			"	stpt	0(%4)\n"
			"	spt	0(%5)\n"
			"	stck	0(%3)\n"
#ifndef CONFIG_64BIT
			"	lpsw	0(%2)\n"
#else /* CONFIG_64BIT */
			"	lpswe	0(%2)\n"
#endif /* CONFIG_64BIT */
			"1:"
			: "=m" (idle->idle_enter), "=m" (vq->idle)
			: "a" (&psw), "a" (&idle->idle_enter),
			  "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw)
			: "memory", "cc", "1");
	} else {
		/*
		 * The inline assembly is equivalent to
		 *	vq->idle = get_cpu_timer();
		 *	idle->idle_enter = get_clock();
		 *	__load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
		 *			   PSW_MASK_IO | PSW_MASK_EXT);
		 * The difference is that the inline assembly makes sure that
		 * the last three instruction are stpt, stck and lpsw in that
		 * order. This is done to increase the precision.
		 */
		asm volatile(
#ifndef CONFIG_64BIT
			"	basr	1,0\n"
			"0:	ahi	1,1f-0b\n"
			"	st	1,4(%2)\n"
#else /* CONFIG_64BIT */
			"	larl	1,1f\n"
			"	stg	1,8(%2)\n"
#endif /* CONFIG_64BIT */
			"	stpt	0(%4)\n"
			"	stck	0(%3)\n"
#ifndef CONFIG_64BIT
			"	lpsw	0(%2)\n"
#else /* CONFIG_64BIT */
			"	lpswe	0(%2)\n"
#endif /* CONFIG_64BIT */
			"1:"
			: "=m" (idle->idle_enter), "=m" (vq->idle)
			: "a" (&psw), "a" (&idle->idle_enter),
			  "a" (&vq->idle), "m" (psw)
			: "memory", "cc", "1");
L
Linus Torvalds 已提交
245 246 247
	}
}

248 249 250 251
cputime64_t s390_get_idle_time(int cpu)
{
	struct s390_idle_data *idle;
	unsigned long long now, idle_time, idle_enter;
252
	unsigned int sequence;
253 254

	idle = &per_cpu(s390_idle, cpu);
255

256
	now = get_clock();
257 258 259 260 261
repeat:
	sequence = idle->sequence;
	smp_rmb();
	if (sequence & 1)
		goto repeat;
262 263 264 265
	idle_time = 0;
	idle_enter = idle->idle_enter;
	if (idle_enter != 0ULL && idle_enter < now)
		idle_time = now - idle_enter;
266 267 268
	smp_rmb();
	if (idle->sequence != sequence)
		goto repeat;
269 270 271
	return idle_time;
}

L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/*
 * Sorted add to a list. List is linear searched until first bigger
 * element is found.
 */
static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
{
	struct vtimer_list *event;

	list_for_each_entry(event, head, entry) {
		if (event->expires > timer->expires) {
			list_add_tail(&timer->entry, &event->entry);
			return;
		}
	}
	list_add_tail(&timer->entry, head);
}

/*
 * Do the callback functions of expired vtimer events.
 * Called from within the interrupt handler.
 */
H
Heiko Carstens 已提交
293
static void do_callbacks(struct list_head *cb_list)
L
Linus Torvalds 已提交
294
{
295
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
296 297 298 299 300
	struct vtimer_list *event, *tmp;

	if (list_empty(cb_list))
		return;

301
	vq = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
302 303

	list_for_each_entry_safe(event, tmp, cb_list, entry) {
304 305 306 307 308 309 310 311
		list_del_init(&event->entry);
		(event->function)(event->data);
		if (event->interval) {
			/* Recharge interval timer */
			event->expires = event->interval + vq->elapsed;
			spin_lock(&vq->lock);
			list_add_sorted(event, &vq->list);
			spin_unlock(&vq->lock);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318
		}
	}
}

/*
 * Handler for the virtual CPU timer.
 */
319 320
static void do_cpu_timer_interrupt(unsigned int ext_int_code,
				   unsigned int param32, unsigned long param64)
L
Linus Torvalds 已提交
321
{
322
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
323
	struct vtimer_list *event, *tmp;
324 325
	struct list_head cb_list;	/* the callback queue */
	__u64 elapsed, next;
L
Linus Torvalds 已提交
326

327
	kstat_cpu(smp_processor_id()).irqs[EXTINT_TMR]++;
L
Linus Torvalds 已提交
328
	INIT_LIST_HEAD(&cb_list);
329
	vq = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
330 331

	/* walk timer list, fire all expired events */
332 333 334 335 336 337 338 339 340 341 342
	spin_lock(&vq->lock);

	elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
	BUG_ON((s64) elapsed < 0);
	vq->elapsed = 0;
	list_for_each_entry_safe(event, tmp, &vq->list, entry) {
		if (event->expires < elapsed)
			/* move expired timer to the callback queue */
			list_move_tail(&event->entry, &cb_list);
		else
			event->expires -= elapsed;
L
Linus Torvalds 已提交
343
	}
344 345 346
	spin_unlock(&vq->lock);

	vq->do_spt = list_empty(&cb_list);
H
Heiko Carstens 已提交
347
	do_callbacks(&cb_list);
L
Linus Torvalds 已提交
348 349

	/* next event is first in list */
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	next = VTIMER_MAX_SLICE;
	spin_lock(&vq->lock);
	if (!list_empty(&vq->list)) {
		event = list_first_entry(&vq->list, struct vtimer_list, entry);
		next = event->expires;
	} else
		vq->do_spt = 0;
	spin_unlock(&vq->lock);
	/*
	 * To improve precision add the time spent by the
	 * interrupt handler to the elapsed time.
	 * Note: CPU timer counts down and we got an interrupt,
	 *	 the current content is negative
	 */
	elapsed = S390_lowcore.async_enter_timer - get_vtimer();
	set_vtimer(next - elapsed);
	vq->timer = next - elapsed;
	vq->elapsed = elapsed;
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

void init_virt_timer(struct vtimer_list *timer)
{
	timer->function = NULL;
	INIT_LIST_HEAD(&timer->entry);
}
EXPORT_SYMBOL(init_virt_timer);

static inline int vtimer_pending(struct vtimer_list *timer)
{
	return (!list_empty(&timer->entry));
}

/*
 * this function should only run on the specified CPU
 */
static void internal_add_vtimer(struct vtimer_list *timer)
{
387
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
388
	unsigned long flags;
389
	__u64 left, expires;
L
Linus Torvalds 已提交
390

391 392
	vq = &per_cpu(virt_cpu_timer, timer->cpu);
	spin_lock_irqsave(&vq->lock, flags);
L
Linus Torvalds 已提交
393

394
	BUG_ON(timer->cpu != smp_processor_id());
L
Linus Torvalds 已提交
395

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	if (list_empty(&vq->list)) {
		/* First timer on this cpu, just program it. */
		list_add(&timer->entry, &vq->list);
		set_vtimer(timer->expires);
		vq->timer = timer->expires;
		vq->elapsed = 0;
	} else {
		/* Check progress of old timers. */
		expires = timer->expires;
		left = get_vtimer();
		if (likely((s64) expires < (s64) left)) {
			/* The new timer expires before the current timer. */
			set_vtimer(expires);
			vq->elapsed += vq->timer - left;
			vq->timer = expires;
		} else {
			vq->elapsed += vq->timer - left;
			vq->timer = left;
		}
		/* Insert new timer into per cpu list. */
		timer->expires += vq->elapsed;
		list_add_sorted(timer, &vq->list);
L
Linus Torvalds 已提交
418 419
	}

420
	spin_unlock_irqrestore(&vq->lock, flags);
A
Andreas Mohr 已提交
421
	/* release CPU acquired in prepare_vtimer or mod_virt_timer() */
L
Linus Torvalds 已提交
422 423 424
	put_cpu();
}

425
static inline void prepare_vtimer(struct vtimer_list *timer)
L
Linus Torvalds 已提交
426
{
427 428 429
	BUG_ON(!timer->function);
	BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
	BUG_ON(vtimer_pending(timer));
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440
	timer->cpu = get_cpu();
}

/*
 * add_virt_timer - add an oneshot virtual CPU timer
 */
void add_virt_timer(void *new)
{
	struct vtimer_list *timer;

	timer = (struct vtimer_list *)new;
441
	prepare_vtimer(timer);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454
	timer->interval = 0;
	internal_add_vtimer(timer);
}
EXPORT_SYMBOL(add_virt_timer);

/*
 * add_virt_timer_int - add an interval virtual CPU timer
 */
void add_virt_timer_periodic(void *new)
{
	struct vtimer_list *timer;

	timer = (struct vtimer_list *)new;
455
	prepare_vtimer(timer);
L
Linus Torvalds 已提交
456 457 458 459 460
	timer->interval = timer->expires;
	internal_add_vtimer(timer);
}
EXPORT_SYMBOL(add_virt_timer_periodic);

461
int __mod_vtimer(struct vtimer_list *timer, __u64 expires, int periodic)
L
Linus Torvalds 已提交
462
{
463
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
464 465 466
	unsigned long flags;
	int cpu;

467 468
	BUG_ON(!timer->function);
	BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
L
Linus Torvalds 已提交
469 470 471 472 473

	if (timer->expires == expires && vtimer_pending(timer))
		return 1;

	cpu = get_cpu();
474
	vq = &per_cpu(virt_cpu_timer, cpu);
L
Linus Torvalds 已提交
475 476

	/* disable interrupts before test if timer is pending */
477
	spin_lock_irqsave(&vq->lock, flags);
L
Linus Torvalds 已提交
478 479 480

	/* if timer isn't pending add it on the current CPU */
	if (!vtimer_pending(timer)) {
481
		spin_unlock_irqrestore(&vq->lock, flags);
482 483 484 485 486

		if (periodic)
			timer->interval = expires;
		else
			timer->interval = 0;
L
Linus Torvalds 已提交
487 488 489 490 491 492
		timer->expires = expires;
		timer->cpu = cpu;
		internal_add_vtimer(timer);
		return 0;
	}

493 494 495
	/* check if we run on the right CPU */
	BUG_ON(timer->cpu != cpu);

L
Linus Torvalds 已提交
496 497
	list_del_init(&timer->entry);
	timer->expires = expires;
498
	if (periodic)
L
Linus Torvalds 已提交
499 500 501
		timer->interval = expires;

	/* the timer can't expire anymore so we can release the lock */
502
	spin_unlock_irqrestore(&vq->lock, flags);
L
Linus Torvalds 已提交
503 504 505
	internal_add_vtimer(timer);
	return 1;
}
506 507 508 509 510 511 512 513 514 515 516

/*
 * If we change a pending timer the function must be called on the CPU
 * where the timer is running on.
 *
 * returns whether it has modified a pending timer (1) or not (0)
 */
int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
{
	return __mod_vtimer(timer, expires, 0);
}
L
Linus Torvalds 已提交
517 518
EXPORT_SYMBOL(mod_virt_timer);

519 520 521 522 523 524 525 526 527 528 529 530
/*
 * If we change a pending timer the function must be called on the CPU
 * where the timer is running on.
 *
 * returns whether it has modified a pending timer (1) or not (0)
 */
int mod_virt_timer_periodic(struct vtimer_list *timer, __u64 expires)
{
	return __mod_vtimer(timer, expires, 1);
}
EXPORT_SYMBOL(mod_virt_timer_periodic);

L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538
/*
 * delete a virtual timer
 *
 * returns whether the deleted timer was pending (1) or not (0)
 */
int del_virt_timer(struct vtimer_list *timer)
{
	unsigned long flags;
539
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
540 541 542 543 544

	/* check if timer is pending */
	if (!vtimer_pending(timer))
		return 0;

545 546
	vq = &per_cpu(virt_cpu_timer, timer->cpu);
	spin_lock_irqsave(&vq->lock, flags);
L
Linus Torvalds 已提交
547 548 549 550

	/* we don't interrupt a running timer, just let it expire! */
	list_del_init(&timer->entry);

551
	spin_unlock_irqrestore(&vq->lock, flags);
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560
	return 1;
}
EXPORT_SYMBOL(del_virt_timer);

/*
 * Start the virtual CPU timer on the current CPU.
 */
void init_cpu_vtimer(void)
{
561
	struct vtimer_queue *vq;
L
Linus Torvalds 已提交
562

563 564 565 566
	/* initialize per cpu vtimer structure */
	vq = &__get_cpu_var(virt_cpu_timer);
	INIT_LIST_HEAD(&vq->list);
	spin_lock_init(&vq->lock);
M
Martin Schwidefsky 已提交
567 568 569

	/* enable cpu timer interrupts */
	__ctl_set_bit(0,10);
L
Linus Torvalds 已提交
570 571
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
static int __cpuinit s390_nohz_notify(struct notifier_block *self,
				      unsigned long action, void *hcpu)
{
	struct s390_idle_data *idle;
	long cpu = (long) hcpu;

	idle = &per_cpu(s390_idle, cpu);
	switch (action) {
	case CPU_DYING:
	case CPU_DYING_FROZEN:
		idle->nohz_delay = 0;
	default:
		break;
	}
	return NOTIFY_OK;
}

L
Linus Torvalds 已提交
589 590 591
void __init vtime_init(void)
{
	/* request the cpu timer external interrupt */
592
	if (register_external_interrupt(0x1005, do_cpu_timer_interrupt))
L
Linus Torvalds 已提交
593 594
		panic("Couldn't request external interrupt 0x1005");

M
Martin Schwidefsky 已提交
595
	/* Enable cpu timer interrupts on the boot cpu. */
L
Linus Torvalds 已提交
596
	init_cpu_vtimer();
597
	cpu_notifier(s390_nohz_notify, 0);
L
Linus Torvalds 已提交
598 599
}