vtime.c 12.9 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 22 23 24
/*
 *  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>

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

static ext_int_info_t ext_int_info_timer;
28
static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
L
Linus Torvalds 已提交
29 30 31 32 33

/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
34
static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
L
Linus Torvalds 已提交
35
{
36 37
	struct thread_info *ti = task_thread_info(tsk);
	__u64 timer, clock, user, system, steal;
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45

	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;
46
	S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
L
Linus Torvalds 已提交
47

48 49 50 51
	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 已提交
52

53 54 55
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
56
	if (idle_task(smp_processor_id()) != current)
57
		account_system_time(tsk, hardirq_offset, system, system);
58
	else
59
		account_idle_time(system);
L
Linus Torvalds 已提交
60

61 62 63
	steal = S390_lowcore.steal_timer;
	if ((s64) steal > 0) {
		S390_lowcore.steal_timer = 0;
64
		if (idle_task(smp_processor_id()) != current)
65
			account_steal_time(steal);
66
		else
67
			account_idle_time(steal);
L
Linus Torvalds 已提交
68 69 70
	}
}

71
void account_vtime(struct task_struct *prev, struct task_struct *next)
72
{
73 74 75 76 77 78 79 80 81 82
	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;
}
83

84 85 86
void account_process_tick(struct task_struct *tsk, int user_tick)
{
	do_account_vtime(tsk, HARDIRQ_OFFSET);
87 88
}

L
Linus Torvalds 已提交
89 90 91 92 93 94
/*
 * 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)
{
95 96
	struct thread_info *ti = task_thread_info(tsk);
	__u64 timer, system;
L
Linus Torvalds 已提交
97 98 99 100 101 102

	timer = S390_lowcore.last_update_timer;
	asm volatile ("  STPT %0"    /* Store current cpu timer value */
		      : "=m" (S390_lowcore.last_update_timer) );
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;

103 104 105
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
106
	if (in_irq() || idle_task(smp_processor_id()) != current)
107
		account_system_time(tsk, 0, system, system);
108
	else
109
		account_idle_time(system);
L
Linus Torvalds 已提交
110
}
111
EXPORT_SYMBOL_GPL(account_system_vtime);
L
Linus Torvalds 已提交
112 113 114

static inline void set_vtimer(__u64 expires)
{
115
	struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124
	__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;

	/* store expire time for this CPU timer */
125
	vq->to_expire = expires;
L
Linus Torvalds 已提交
126 127
}

128
void vtime_start_cpu_timer(void)
L
Linus Torvalds 已提交
129 130 131
{
	struct vtimer_queue *vt_list;

132
	vt_list = &__get_cpu_var(virt_cpu_timer);
133 134 135 136 137 138 139

	/* CPU timer interrupt is pending, don't reprogramm it */
	if (vt_list->idle & 1LL<<63)
		return;

	if (!list_empty(&vt_list->list))
		set_vtimer(vt_list->idle);
L
Linus Torvalds 已提交
140 141
}

142
void vtime_stop_cpu_timer(void)
L
Linus Torvalds 已提交
143 144 145
{
	struct vtimer_queue *vt_list;

146
	vt_list = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153

	/* nothing to do */
	if (list_empty(&vt_list->list)) {
		vt_list->idle = VTIMER_MAX_SLICE;
		goto fire;
	}

154 155
	/* store the actual expire value */
	asm volatile ("STPT %0" : "=m" (vt_list->idle));
L
Linus Torvalds 已提交
156 157

	/*
158 159
	 * If the CPU timer is negative we don't reprogramm
	 * it because we will get instantly an interrupt.
L
Linus Torvalds 已提交
160
	 */
161
	if (vt_list->idle & 1LL<<63)
L
Linus Torvalds 已提交
162 163
		return;

164
	vt_list->offset += vt_list->to_expire - vt_list->idle;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	/*
	 * We cannot halt the CPU timer, we just write a value that
	 * nearly never expires (only after 71 years) and re-write
	 * the stored expire value if we continue the timer
	 */
 fire:
	set_vtimer(VTIMER_MAX_SLICE);
}

/*
 * 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 已提交
196
static void do_callbacks(struct list_head *cb_list)
L
Linus Torvalds 已提交
197 198 199
{
	struct vtimer_queue *vt_list;
	struct vtimer_list *event, *tmp;
H
Heiko Carstens 已提交
200
	void (*fn)(unsigned long);
L
Linus Torvalds 已提交
201 202 203 204 205
	unsigned long data;

	if (list_empty(cb_list))
		return;

206
	vt_list = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
207 208 209 210

	list_for_each_entry_safe(event, tmp, cb_list, entry) {
		fn = event->function;
		data = event->data;
H
Heiko Carstens 已提交
211
		fn(data);
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

		if (!event->interval)
			/* delete one shot timer */
			list_del_init(&event->entry);
		else {
			/* move interval timer back to list */
			spin_lock(&vt_list->lock);
			list_del_init(&event->entry);
			list_add_sorted(event, &vt_list->list);
			spin_unlock(&vt_list->lock);
		}
	}
}

/*
 * Handler for the virtual CPU timer.
 */
H
Heiko Carstens 已提交
229
static void do_cpu_timer_interrupt(__u16 error_code)
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238
{
	__u64 next, delta;
	struct vtimer_queue *vt_list;
	struct vtimer_list *event, *tmp;
	struct list_head *ptr;
	/* the callback queue */
	struct list_head cb_list;

	INIT_LIST_HEAD(&cb_list);
239
	vt_list = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	/* walk timer list, fire all expired events */
	spin_lock(&vt_list->lock);

	if (vt_list->to_expire < VTIMER_MAX_SLICE)
		vt_list->offset += vt_list->to_expire;

	list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
		if (event->expires > vt_list->offset)
			/* found first unexpired event, leave */
			break;

		/* re-charge interval timer, we have to add the offset */
		if (event->interval)
			event->expires = event->interval + vt_list->offset;

		/* move expired timer to the callback queue */
		list_move_tail(&event->entry, &cb_list);
	}
	spin_unlock(&vt_list->lock);
H
Heiko Carstens 已提交
260
	do_callbacks(&cb_list);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

	/* next event is first in list */
	spin_lock(&vt_list->lock);
	if (!list_empty(&vt_list->list)) {
		ptr = vt_list->list.next;
		event = list_entry(ptr, struct vtimer_list, entry);
		next = event->expires - vt_list->offset;

		/* add the expired time from this interrupt handler
		 * and the callback functions
		 */
		asm volatile ("STPT %0" : "=m" (delta));
		delta = 0xffffffffffffffffLL - delta + 1;
		vt_list->offset += delta;
		next -= delta;
	} else {
		vt_list->offset = 0;
		next = VTIMER_MAX_SLICE;
	}
	spin_unlock(&vt_list->lock);
	set_vtimer(next);
}

void init_virt_timer(struct vtimer_list *timer)
{
	timer->function = NULL;
	INIT_LIST_HEAD(&timer->entry);
	spin_lock_init(&timer->lock);
}
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)
{
	unsigned long flags;
	__u64 done;
	struct vtimer_list *event;
	struct vtimer_queue *vt_list;

	vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
	spin_lock_irqsave(&vt_list->lock, flags);

310
	BUG_ON(timer->cpu != smp_processor_id());
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

	/* if list is empty we only have to set the timer */
	if (list_empty(&vt_list->list)) {
		/* reset the offset, this may happen if the last timer was
		 * just deleted by mod_virt_timer and the interrupt
		 * didn't happen until here
		 */
		vt_list->offset = 0;
		goto fire;
	}

	/* save progress */
	asm volatile ("STPT %0" : "=m" (done));

	/* calculate completed work */
	done = vt_list->to_expire - done + vt_list->offset;
	vt_list->offset = 0;

	list_for_each_entry(event, &vt_list->list, entry)
		event->expires -= done;

 fire:
	list_add_sorted(timer, &vt_list->list);

	/* get first element, which is the next vtimer slice */
	event = list_entry(vt_list->list.next, struct vtimer_list, entry);

	set_vtimer(event->expires);
	spin_unlock_irqrestore(&vt_list->lock, flags);
A
Andreas Mohr 已提交
340
	/* release CPU acquired in prepare_vtimer or mod_virt_timer() */
L
Linus Torvalds 已提交
341 342 343
	put_cpu();
}

344
static inline void prepare_vtimer(struct vtimer_list *timer)
L
Linus Torvalds 已提交
345
{
346 347 348
	BUG_ON(!timer->function);
	BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
	BUG_ON(vtimer_pending(timer));
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359
	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;
360
	prepare_vtimer(timer);
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373
	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;
374
	prepare_vtimer(timer);
L
Linus Torvalds 已提交
375 376 377 378 379 380 381
	timer->interval = timer->expires;
	internal_add_vtimer(timer);
}
EXPORT_SYMBOL(add_virt_timer_periodic);

/*
 * If we change a pending timer the function must be called on the CPU
382
 * where the timer is running on, e.g. by smp_call_function_single()
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394
 *
 * The original mod_timer adds the timer if it is not pending. For compatibility
 * we do the same. The timer will be added on the current CPU as a oneshot timer.
 *
 * returns whether it has modified a pending timer (1) or not (0)
 */
int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
{
	struct vtimer_queue *vt_list;
	unsigned long flags;
	int cpu;

395 396
	BUG_ON(!timer->function);
	BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406 407 408

	/*
	 * This is a common optimization triggered by the
	 * networking code - if the timer is re-modified
	 * to be the same thing then just return:
	 */
	if (timer->expires == expires && vtimer_pending(timer))
		return 1;

	cpu = get_cpu();
	vt_list = &per_cpu(virt_cpu_timer, cpu);

409 410 411
	/* check if we run on the right CPU */
	BUG_ON(timer->cpu != cpu);

L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	/* disable interrupts before test if timer is pending */
	spin_lock_irqsave(&vt_list->lock, flags);

	/* if timer isn't pending add it on the current CPU */
	if (!vtimer_pending(timer)) {
		spin_unlock_irqrestore(&vt_list->lock, flags);
		/* we do not activate an interval timer with mod_virt_timer */
		timer->interval = 0;
		timer->expires = expires;
		timer->cpu = cpu;
		internal_add_vtimer(timer);
		return 0;
	}

	list_del_init(&timer->entry);
	timer->expires = expires;

	/* also change the interval if we have an interval timer */
	if (timer->interval)
		timer->interval = expires;

	/* the timer can't expire anymore so we can release the lock */
	spin_unlock_irqrestore(&vt_list->lock, flags);
	internal_add_vtimer(timer);
	return 1;
}
EXPORT_SYMBOL(mod_virt_timer);

/*
 * 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;
	struct vtimer_queue *vt_list;

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

	vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
	spin_lock_irqsave(&vt_list->lock, flags);

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

	/* last timer removed */
	if (list_empty(&vt_list->list)) {
		vt_list->to_expire = 0;
		vt_list->offset = 0;
	}

	spin_unlock_irqrestore(&vt_list->lock, flags);
	return 1;
}
EXPORT_SYMBOL(del_virt_timer);

/*
 * Start the virtual CPU timer on the current CPU.
 */
void init_cpu_vtimer(void)
{
	struct vtimer_queue *vt_list;

	/* kick the virtual timer */
	S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
	S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
	asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
482
	asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
M
Martin Schwidefsky 已提交
483 484 485

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

487
	vt_list = &__get_cpu_var(virt_cpu_timer);
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	INIT_LIST_HEAD(&vt_list->list);
	spin_lock_init(&vt_list->lock);
	vt_list->to_expire = 0;
	vt_list->offset = 0;
	vt_list->idle = 0;

}

void __init vtime_init(void)
{
	/* request the cpu timer external interrupt */
	if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
					      &ext_int_info_timer) != 0)
		panic("Couldn't request external interrupt 0x1005");

M
Martin Schwidefsky 已提交
503
	/* Enable cpu timer interrupts on the boot cpu. */
L
Linus Torvalds 已提交
504 505 506
	init_cpu_vtimer();
}