linux_hrtimer.c revision 1.2 1 1.2 riastrad /* $NetBSD: linux_hrtimer.c,v 1.2 2021/12/19 11:53:09 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_hrtimer.c,v 1.2 2021/12/19 11:53:09 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <sys/types.h>
33 1.1 riastrad #include <sys/callout.h>
34 1.1 riastrad #include <sys/kmem.h>
35 1.1 riastrad
36 1.1 riastrad #include <linux/hrtimer.h>
37 1.1 riastrad #include <linux/ktime.h>
38 1.1 riastrad
39 1.1 riastrad struct hrtimer_private {
40 1.1 riastrad struct callout ch;
41 1.1 riastrad enum hrtimer_mode mode;
42 1.1 riastrad ktime_t expires;
43 1.1 riastrad };
44 1.1 riastrad
45 1.1 riastrad static void hrtimer_fire(void *);
46 1.1 riastrad
47 1.1 riastrad void
48 1.1 riastrad hrtimer_init(struct hrtimer *hrt, clockid_t clkid, enum hrtimer_mode mode)
49 1.1 riastrad {
50 1.1 riastrad struct hrtimer_private *H;
51 1.1 riastrad
52 1.1 riastrad KASSERTMSG(clkid == CLOCK_MONOTONIC, "clkid %d", clkid);
53 1.1 riastrad
54 1.1 riastrad H = hrt->hrt_private = kmem_zalloc(sizeof(*H), KM_SLEEP);
55 1.1 riastrad
56 1.1 riastrad callout_init(&H->ch, CALLOUT_MPSAFE);
57 1.1 riastrad callout_setfunc(&H->ch, hrtimer_fire, H);
58 1.1 riastrad H->mode = mode;
59 1.1 riastrad }
60 1.1 riastrad
61 1.1 riastrad static void
62 1.1 riastrad _hrtimer_schedule(struct hrtimer *hrt)
63 1.1 riastrad {
64 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
65 1.1 riastrad int delta;
66 1.1 riastrad
67 1.1 riastrad switch (H->mode) {
68 1.1 riastrad case HRTIMER_MODE_ABS:
69 1.1 riastrad panic("absolute hrtimer NYI");
70 1.1 riastrad break;
71 1.1 riastrad case HRTIMER_MODE_REL:
72 1.1 riastrad delta = ktime_to_ms(H->expires);
73 1.1 riastrad break;
74 1.1 riastrad default:
75 1.1 riastrad panic("invalid hrtimer mode %d", H->mode);
76 1.1 riastrad }
77 1.1 riastrad callout_schedule(&H->ch, delta);
78 1.1 riastrad }
79 1.1 riastrad
80 1.1 riastrad static void
81 1.1 riastrad hrtimer_fire(void *cookie)
82 1.1 riastrad {
83 1.1 riastrad struct hrtimer *hrt = cookie;
84 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
85 1.1 riastrad
86 1.1 riastrad switch ((*hrt->function)(hrt)) {
87 1.1 riastrad case HRTIMER_RESTART:
88 1.1 riastrad _hrtimer_schedule(hrt);
89 1.1 riastrad break;
90 1.1 riastrad case HRTIMER_NORESTART:
91 1.1 riastrad break;
92 1.1 riastrad }
93 1.1 riastrad
94 1.1 riastrad callout_ack(&H->ch);
95 1.1 riastrad }
96 1.1 riastrad
97 1.1 riastrad void
98 1.1 riastrad hrtimer_set_expires(struct hrtimer *hrt, ktime_t expires)
99 1.1 riastrad {
100 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
101 1.1 riastrad
102 1.1 riastrad H->expires = expires;
103 1.1 riastrad }
104 1.1 riastrad
105 1.1 riastrad void
106 1.1 riastrad hrtimer_add_expires_ns(struct hrtimer *hrt, uint64_t ns)
107 1.1 riastrad {
108 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
109 1.1 riastrad
110 1.1 riastrad H->expires = ktime_add_ns(H->expires, ns);
111 1.1 riastrad }
112 1.1 riastrad
113 1.1 riastrad void
114 1.1 riastrad hrtimer_start(struct hrtimer *hrt, ktime_t expires, enum hrtimer_mode mode)
115 1.1 riastrad {
116 1.1 riastrad
117 1.1 riastrad hrtimer_start_range_ns(hrt, expires, 0, mode);
118 1.1 riastrad }
119 1.1 riastrad
120 1.1 riastrad void
121 1.1 riastrad hrtimer_start_range_ns(struct hrtimer *hrt, ktime_t expires, uint64_t range_ns,
122 1.1 riastrad enum hrtimer_mode mode)
123 1.1 riastrad {
124 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
125 1.1 riastrad
126 1.1 riastrad H->expires = expires;
127 1.1 riastrad (void)range_ns;
128 1.1 riastrad H->mode = mode;
129 1.1 riastrad _hrtimer_schedule(hrt);
130 1.1 riastrad }
131 1.1 riastrad
132 1.1 riastrad int
133 1.1 riastrad hrtimer_cancel(struct hrtimer *hrt)
134 1.1 riastrad {
135 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
136 1.1 riastrad bool active;
137 1.1 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * Halt the callout and ascertain whether the hrtimer was
140 1.1 riastrad * active when we invoked hrtimer_cancel.
141 1.1 riastrad */
142 1.1 riastrad if (callout_halt(&H->ch, NULL)) {
143 1.1 riastrad /* Callout expired, meaning it was active. */
144 1.1 riastrad active = true;
145 1.1 riastrad } else {
146 1.1 riastrad /*
147 1.1 riastrad * Callout had not yet expired. It will not expire
148 1.1 riastrad * now, so callout_pending is now stable and
149 1.1 riastrad * corresponds with whether the hrtimer was active or
150 1.1 riastrad * not.
151 1.1 riastrad */
152 1.1 riastrad active = callout_pending(&H->ch);
153 1.1 riastrad }
154 1.2 riastrad return active;
155 1.2 riastrad }
156 1.2 riastrad
157 1.2 riastrad void
158 1.2 riastrad hrtimer_destroy(struct hrtimer *hrt)
159 1.2 riastrad {
160 1.2 riastrad struct hrtimer_private *H = hrt->hrt_private;
161 1.1 riastrad
162 1.1 riastrad callout_destroy(&H->ch);
163 1.1 riastrad kmem_free(H, sizeof(*H));
164 1.1 riastrad
165 1.1 riastrad explicit_memset(hrt, 0, sizeof(*hrt)); /* paranoia */
166 1.1 riastrad }
167 1.1 riastrad
168 1.1 riastrad bool
169 1.1 riastrad hrtimer_active(struct hrtimer *hrt)
170 1.1 riastrad {
171 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
172 1.1 riastrad
173 1.1 riastrad /*
174 1.1 riastrad * If the callout has been scheduled, but has not yet fired,
175 1.1 riastrad * then it is pending.
176 1.1 riastrad *
177 1.1 riastrad * If the callout has fired, but has not yet reached
178 1.1 riastrad * callout_ack, then it is invoking.
179 1.1 riastrad */
180 1.1 riastrad return callout_pending(&H->ch) || callout_invoking(&H->ch);
181 1.1 riastrad }
182 1.1 riastrad
183 1.1 riastrad uint64_t
184 1.1 riastrad hrtimer_forward(struct hrtimer *hrt, ktime_t now, ktime_t period)
185 1.1 riastrad {
186 1.1 riastrad struct hrtimer_private *H = hrt->hrt_private;
187 1.1 riastrad uint64_t now_ms, period_ms, expires_ms, nperiods;
188 1.1 riastrad
189 1.1 riastrad KASSERT(!callout_pending(&H->ch));
190 1.1 riastrad
191 1.1 riastrad /*
192 1.1 riastrad * Can't get better than 10ms precision (or ~1ms if you set
193 1.1 riastrad * HZ=1000) so not much point in doing this arithmetic at finer
194 1.1 riastrad * resolution than ms.
195 1.1 riastrad */
196 1.1 riastrad now_ms = ktime_to_ms(now);
197 1.1 riastrad period_ms = ktime_to_ms(period);
198 1.1 riastrad expires_ms = ktime_to_ms(H->expires);
199 1.1 riastrad
200 1.1 riastrad /* If it hasn't yet expired, no overruns. */
201 1.1 riastrad if (now_ms < expires_ms)
202 1.1 riastrad return 0;
203 1.1 riastrad
204 1.1 riastrad /* Advance it by as many periods as it should have fired. */
205 1.1 riastrad /* XXX fenceposts */
206 1.1 riastrad nperiods = howmany(now_ms - expires_ms, period_ms);
207 1.1 riastrad H->expires = ktime_add_ns(H->expires, 1000000*nperiods*period_ms);
208 1.1 riastrad
209 1.1 riastrad return nperiods;
210 1.1 riastrad }
211 1.1 riastrad
212 1.1 riastrad uint64_t
213 1.1 riastrad hrtimer_forward_now(struct hrtimer *hrt, ktime_t period)
214 1.1 riastrad {
215 1.1 riastrad
216 1.1 riastrad return hrtimer_forward(hrt, ktime_get(), period);
217 1.1 riastrad }
218