vtime.c 8.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *    Virtual cpu timer based timer functions.
 *
4
 *    Copyright IBM Corp. 2004, 2012
L
Linus Torvalds 已提交
5 6 7
 *    Author(s): Jan Glauber <jan.glauber@de.ibm.com>
 */

8 9
#include <linux/kernel_stat.h>
#include <linux/export.h>
L
Linus Torvalds 已提交
10 11
#include <linux/kernel.h>
#include <linux/timex.h>
12 13
#include <linux/types.h>
#include <linux/time.h>
L
Linus Torvalds 已提交
14

15
#include <asm/cputime.h>
16
#include <asm/vtimer.h>
17
#include <asm/vtime.h>
L
Linus Torvalds 已提交
18

19
static void virt_timer_expire(void);
L
Linus Torvalds 已提交
20

21 22 23 24 25 26
static LIST_HEAD(virt_timer_list);
static DEFINE_SPINLOCK(virt_timer_lock);
static atomic64_t virt_timer_current;
static atomic64_t virt_timer_elapsed;

static inline u64 get_vtimer(void)
27
{
28
	u64 timer;
29

30
	asm volatile("stpt %0" : "=m" (timer));
31 32 33
	return timer;
}

34
static inline void set_vtimer(u64 expires)
35
{
36
	u64 timer;
37

38 39 40 41
	asm volatile(
		"	stpt	%0\n"	/* Store current cpu timer value */
		"	spt	%1"	/* Set new value imm. afterwards */
		: "=m" (timer) : "m" (expires));
42 43 44 45
	S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
	S390_lowcore.last_update_timer = expires;
}

46 47 48 49 50 51 52 53 54 55
static inline int virt_timer_forward(u64 elapsed)
{
	BUG_ON(!irqs_disabled());

	if (list_empty(&virt_timer_list))
		return 0;
	elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
	return elapsed >= atomic64_read(&virt_timer_current);
}

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

	timer = S390_lowcore.last_update_timer;
	clock = S390_lowcore.last_update_clock;
67 68
	asm volatile(
		"	stpt	%0\n"	/* Store current cpu timer value */
69 70 71
#ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
		"	stckf	%1"	/* Store current tod clock value */
#else
72
		"	stck	%1"	/* Store current tod clock value */
73
#endif
74 75
		: "=m" (S390_lowcore.last_update_timer),
		  "=m" (S390_lowcore.last_update_clock));
L
Linus Torvalds 已提交
76
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
77
	S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
L
Linus Torvalds 已提交
78

79 80 81 82
	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 已提交
83

84 85 86
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
87
	account_system_time(tsk, hardirq_offset, system, system);
L
Linus Torvalds 已提交
88

89 90 91
	steal = S390_lowcore.steal_timer;
	if ((s64) steal > 0) {
		S390_lowcore.steal_timer = 0;
92
		account_steal_time(steal);
L
Linus Torvalds 已提交
93
	}
94 95

	return virt_timer_forward(user + system);
L
Linus Torvalds 已提交
96 97
}

98
void vtime_task_switch(struct task_struct *prev)
99
{
100 101 102 103 104 105
	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;
106
	ti = task_thread_info(current);
107 108 109
	S390_lowcore.user_timer = ti->user_timer;
	S390_lowcore.system_timer = ti->system_timer;
}
110

111 112 113 114 115 116
/*
 * In s390, accounting pending user time also implies
 * accounting system time in order to correctly compute
 * the stolen time accounting.
 */
void vtime_account_user(struct task_struct *tsk)
117
{
118 119
	if (do_account_vtime(tsk, HARDIRQ_OFFSET))
		virt_timer_expire();
120 121
}

L
Linus Torvalds 已提交
122 123 124 125
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
126
void vtime_account_irq_enter(struct task_struct *tsk)
L
Linus Torvalds 已提交
127
{
128
	struct thread_info *ti = task_thread_info(tsk);
129
	u64 timer, system;
L
Linus Torvalds 已提交
130 131

	timer = S390_lowcore.last_update_timer;
132
	S390_lowcore.last_update_timer = get_vtimer();
L
Linus Torvalds 已提交
133 134
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;

135 136 137
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
138
	account_system_time(tsk, 0, system, system);
139 140

	virt_timer_forward(system);
L
Linus Torvalds 已提交
141
}
142
EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
L
Linus Torvalds 已提交
143

144
void vtime_account_system(struct task_struct *tsk)
145
__attribute__((alias("vtime_account_irq_enter")));
146
EXPORT_SYMBOL_GPL(vtime_account_system);
147

L
Linus Torvalds 已提交
148 149 150 151 152 153
/*
 * 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)
{
154
	struct vtimer_list *tmp;
L
Linus Torvalds 已提交
155

156 157 158
	list_for_each_entry(tmp, head, entry) {
		if (tmp->expires > timer->expires) {
			list_add_tail(&timer->entry, &tmp->entry);
L
Linus Torvalds 已提交
159 160 161 162 163 164 165
			return;
		}
	}
	list_add_tail(&timer->entry, head);
}

/*
166
 * Handler for expired virtual CPU timer.
L
Linus Torvalds 已提交
167
 */
168
static void virt_timer_expire(void)
L
Linus Torvalds 已提交
169
{
170 171 172 173 174 175 176 177 178
	struct vtimer_list *timer, *tmp;
	unsigned long elapsed;
	LIST_HEAD(cb_list);

	/* walk timer list, fire all expired timers */
	spin_lock(&virt_timer_lock);
	elapsed = atomic64_read(&virt_timer_elapsed);
	list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
		if (timer->expires < elapsed)
179
			/* move expired timer to the callback queue */
180
			list_move_tail(&timer->entry, &cb_list);
181
		else
182
			timer->expires -= elapsed;
L
Linus Torvalds 已提交
183
	}
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	if (!list_empty(&virt_timer_list)) {
		timer = list_first_entry(&virt_timer_list,
					 struct vtimer_list, entry);
		atomic64_set(&virt_timer_current, timer->expires);
	}
	atomic64_sub(elapsed, &virt_timer_elapsed);
	spin_unlock(&virt_timer_lock);

	/* Do callbacks and recharge periodic timers */
	list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
		list_del_init(&timer->entry);
		timer->function(timer->data);
		if (timer->interval) {
			/* Recharge interval timer */
			timer->expires = timer->interval +
				atomic64_read(&virt_timer_elapsed);
			spin_lock(&virt_timer_lock);
			list_add_sorted(timer, &virt_timer_list);
			spin_unlock(&virt_timer_lock);
		}
M
Martin Schwidefsky 已提交
204
	}
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215
}

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)
{
216
	return !list_empty(&timer->entry);
L
Linus Torvalds 已提交
217 218 219 220
}

static void internal_add_vtimer(struct vtimer_list *timer)
{
221 222 223 224 225
	if (list_empty(&virt_timer_list)) {
		/* First timer, just program it. */
		atomic64_set(&virt_timer_current, timer->expires);
		atomic64_set(&virt_timer_elapsed, 0);
		list_add(&timer->entry, &virt_timer_list);
226
	} else {
227 228 229 230
		/* Update timer against current base. */
		timer->expires += atomic64_read(&virt_timer_elapsed);
		if (likely((s64) timer->expires <
			   (s64) atomic64_read(&virt_timer_current)))
231
			/* The new timer expires before the current timer. */
232 233 234
			atomic64_set(&virt_timer_current, timer->expires);
		/* Insert new timer into the list. */
		list_add_sorted(timer, &virt_timer_list);
L
Linus Torvalds 已提交
235 236 237
	}
}

238
static void __add_vtimer(struct vtimer_list *timer, int periodic)
L
Linus Torvalds 已提交
239
{
240 241 242 243 244 245
	unsigned long flags;

	timer->interval = periodic ? timer->expires : 0;
	spin_lock_irqsave(&virt_timer_lock, flags);
	internal_add_vtimer(timer);
	spin_unlock_irqrestore(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
246 247 248 249 250
}

/*
 * add_virt_timer - add an oneshot virtual CPU timer
 */
251
void add_virt_timer(struct vtimer_list *timer)
L
Linus Torvalds 已提交
252
{
253
	__add_vtimer(timer, 0);
L
Linus Torvalds 已提交
254 255 256 257 258 259
}
EXPORT_SYMBOL(add_virt_timer);

/*
 * add_virt_timer_int - add an interval virtual CPU timer
 */
260
void add_virt_timer_periodic(struct vtimer_list *timer)
L
Linus Torvalds 已提交
261
{
262
	__add_vtimer(timer, 1);
L
Linus Torvalds 已提交
263 264 265
}
EXPORT_SYMBOL(add_virt_timer_periodic);

266
static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
L
Linus Torvalds 已提交
267 268
{
	unsigned long flags;
269
	int rc;
L
Linus Torvalds 已提交
270

271
	BUG_ON(!timer->function);
L
Linus Torvalds 已提交
272 273 274

	if (timer->expires == expires && vtimer_pending(timer))
		return 1;
275 276 277 278 279
	spin_lock_irqsave(&virt_timer_lock, flags);
	rc = vtimer_pending(timer);
	if (rc)
		list_del_init(&timer->entry);
	timer->interval = periodic ? expires : 0;
L
Linus Torvalds 已提交
280 281
	timer->expires = expires;
	internal_add_vtimer(timer);
282 283
	spin_unlock_irqrestore(&virt_timer_lock, flags);
	return rc;
L
Linus Torvalds 已提交
284
}
285 286 287 288

/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
289
int mod_virt_timer(struct vtimer_list *timer, u64 expires)
290 291 292
{
	return __mod_vtimer(timer, expires, 0);
}
L
Linus Torvalds 已提交
293 294
EXPORT_SYMBOL(mod_virt_timer);

295 296 297
/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
298
int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
299 300 301 302 303
{
	return __mod_vtimer(timer, expires, 1);
}
EXPORT_SYMBOL(mod_virt_timer_periodic);

L
Linus Torvalds 已提交
304
/*
305
 * Delete a virtual timer.
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314
 *
 * returns whether the deleted timer was pending (1) or not (0)
 */
int del_virt_timer(struct vtimer_list *timer)
{
	unsigned long flags;

	if (!vtimer_pending(timer))
		return 0;
315
	spin_lock_irqsave(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
316
	list_del_init(&timer->entry);
317
	spin_unlock_irqrestore(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
318 319 320 321 322 323 324
	return 1;
}
EXPORT_SYMBOL(del_virt_timer);

/*
 * Start the virtual CPU timer on the current CPU.
 */
325
void vtime_init(void)
L
Linus Torvalds 已提交
326
{
M
Martin Schwidefsky 已提交
327
	/* set initial cpu timer */
328
	set_vtimer(VTIMER_MAX_SLICE);
L
Linus Torvalds 已提交
329
}