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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
|
/* $NetBSD: linux_hrtimer.c,v 1.3 2021/12/19 11:55:47 riastradh Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_hrtimer.c,v 1.3 2021/12/19 11:55:47 riastradh Exp $");
#include <sys/types.h>
#include <sys/callout.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
static void hrtimer_fire(void *);
void
hrtimer_init(struct hrtimer *hrt, clockid_t clkid, enum hrtimer_mode mode)
{
KASSERTMSG(clkid == CLOCK_MONOTONIC, "clkid %d", clkid);
callout_init(&hrt->hrt_ch, CALLOUT_MPSAFE);
callout_setfunc(&hrt->hrt_ch, hrtimer_fire, hrt);
hrt->hrt_mode = mode;
}
static void
_hrtimer_schedule(struct hrtimer *hrt)
{
int delta;
switch (hrt->hrt_mode) {
case HRTIMER_MODE_ABS:
panic("absolute hrtimer NYI");
break;
case HRTIMER_MODE_REL:
delta = ktime_to_ms(hrt->hrt_expires);
break;
default:
panic("invalid hrtimer mode %d", hrt->hrt_mode);
}
callout_schedule(&hrt->hrt_ch, delta);
}
static void
hrtimer_fire(void *cookie)
{
struct hrtimer *hrt = cookie;
switch ((*hrt->function)(hrt)) {
case HRTIMER_RESTART:
_hrtimer_schedule(hrt);
break;
case HRTIMER_NORESTART:
break;
}
callout_ack(&hrt->hrt_ch);
}
void
hrtimer_set_expires(struct hrtimer *hrt, ktime_t expires)
{
hrt->hrt_expires = expires;
}
void
hrtimer_add_expires_ns(struct hrtimer *hrt, uint64_t ns)
{
hrt->hrt_expires = ktime_add_ns(hrt->hrt_expires, ns);
}
void
hrtimer_start(struct hrtimer *hrt, ktime_t expires, enum hrtimer_mode mode)
{
hrtimer_start_range_ns(hrt, expires, 0, mode);
}
void
hrtimer_start_range_ns(struct hrtimer *hrt, ktime_t expires, uint64_t range_ns,
enum hrtimer_mode mode)
{
hrt->hrt_expires = expires;
(void)range_ns;
hrt->hrt_mode = mode;
_hrtimer_schedule(hrt);
}
int
hrtimer_cancel(struct hrtimer *hrt)
{
bool active;
/*
* Halt the callout and ascertain whether the hrtimer was
* active when we invoked hrtimer_cancel.
*/
if (callout_halt(&hrt->hrt_ch, NULL)) {
/* Callout expired, meaning it was active. */
active = true;
} else {
/*
* Callout had not yet expired. It will not expire
* now, so callout_pending is now stable and
* corresponds with whether the hrtimer was active or
* not.
*/
active = callout_pending(&hrt->hrt_ch);
}
return active;
}
bool
hrtimer_active(struct hrtimer *hrt)
{
/*
* If the callout has been scheduled, but has not yet fired,
* then it is pending.
*
* If the callout has fired, but has not yet reached
* callout_ack, then it is invoking.
*/
return callout_pending(&hrt->hrt_ch) || callout_invoking(&hrt->hrt_ch);
}
uint64_t
hrtimer_forward(struct hrtimer *hrt, ktime_t now, ktime_t period)
{
uint64_t now_ms, period_ms, expires_ms, nperiods;
KASSERT(!callout_pending(&hrt->hrt_ch));
/*
* Can't get better than 10ms precision (or ~1ms if you set
* HZ=1000) so not much point in doing this arithmetic at finer
* resolution than ms.
*/
now_ms = ktime_to_ms(now);
period_ms = ktime_to_ms(period);
expires_ms = ktime_to_ms(hrt->hrt_expires);
/* If it hasn't yet expired, no overruns. */
if (now_ms < expires_ms)
return 0;
/* Advance it by as many periods as it should have fired. */
/* XXX fenceposts */
nperiods = howmany(now_ms - expires_ms, period_ms);
hrt->hrt_expires = ktime_add_ns(hrt->hrt_expires,
1000000*nperiods*period_ms);
return nperiods;
}
uint64_t
hrtimer_forward_now(struct hrtimer *hrt, ktime_t period)
{
return hrtimer_forward(hrt, ktime_get(), period);
}
|