summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/drm/drm_sun_timer.c
blob: b8039c7fa05a7c958256f23e0eea09e6a3e3a2f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
 * Copyright (c) 2012 Intel Corporation.  All rights reserved.
 */

#include "drm_sun_timer.h"

void
init_timer(struct timer_list *timer)
{
	mutex_init(&timer->lock, NULL, MUTEX_DRIVER, NULL);
}

void
destroy_timer(struct timer_list *timer)
{
	mutex_destroy(&timer->lock);
}

void
setup_timer(struct timer_list *timer, void (*func)(void *), void *arg)
{
	timer->func = func;
	timer->arg = arg;
	timer->expired_time = 0;
}

void
mod_timer(struct timer_list *timer, clock_t expires)
{
	mutex_enter(&timer->lock);
	/*
	 * If timer is already being updated, let previous caller do the work
	 * Note that we must drop timer->lock before the untimeout, as the
	 * callback might call mod_timer and deadlock.  So the guard is
	 * centered around the overloading of the timer->expires element.
	 */
	if (timer->expires == -1) {
		mutex_exit(&timer->lock);
		return;
	}
	timer->expires = -1;
	mutex_exit(&timer->lock);

	(void) untimeout(timer->timer_id);
	timer->expired_time = jiffies + expires;
	timer->timer_id = timeout(timer->func, timer->arg, expires);

	mutex_enter(&timer->lock);
	timer->expires = 0;
	mutex_exit(&timer->lock);
}

void
del_timer(struct timer_list *timer)
{
	(void) untimeout(timer->timer_id);
}

void
test_set_timer(struct timer_list *timer, clock_t expires)
{
	if (time_after(jiffies, timer->expired_time)) {
		timer->expired_time = jiffies + expires;
		timer->timer_id = timeout(timer->func, timer->arg, expires);
	}
}