linux_hrtimer.c revision 1.3 1 1.3 riastrad /* $NetBSD: linux_hrtimer.c,v 1.3 2021/12/19 11:55:47 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.3 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_hrtimer.c,v 1.3 2021/12/19 11:55:47 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
35 1.1 riastrad #include <linux/hrtimer.h>
36 1.1 riastrad #include <linux/ktime.h>
37 1.1 riastrad
38 1.1 riastrad static void hrtimer_fire(void *);
39 1.1 riastrad
40 1.1 riastrad void
41 1.1 riastrad hrtimer_init(struct hrtimer *hrt, clockid_t clkid, enum hrtimer_mode mode)
42 1.1 riastrad {
43 1.1 riastrad
44 1.1 riastrad KASSERTMSG(clkid == CLOCK_MONOTONIC, "clkid %d", clkid);
45 1.1 riastrad
46 1.3 riastrad callout_init(&hrt->hrt_ch, CALLOUT_MPSAFE);
47 1.3 riastrad callout_setfunc(&hrt->hrt_ch, hrtimer_fire, hrt);
48 1.3 riastrad hrt->hrt_mode = mode;
49 1.1 riastrad }
50 1.1 riastrad
51 1.1 riastrad static void
52 1.1 riastrad _hrtimer_schedule(struct hrtimer *hrt)
53 1.1 riastrad {
54 1.1 riastrad int delta;
55 1.1 riastrad
56 1.3 riastrad switch (hrt->hrt_mode) {
57 1.1 riastrad case HRTIMER_MODE_ABS:
58 1.1 riastrad panic("absolute hrtimer NYI");
59 1.1 riastrad break;
60 1.1 riastrad case HRTIMER_MODE_REL:
61 1.3 riastrad delta = ktime_to_ms(hrt->hrt_expires);
62 1.1 riastrad break;
63 1.1 riastrad default:
64 1.3 riastrad panic("invalid hrtimer mode %d", hrt->hrt_mode);
65 1.1 riastrad }
66 1.3 riastrad callout_schedule(&hrt->hrt_ch, delta);
67 1.1 riastrad }
68 1.1 riastrad
69 1.1 riastrad static void
70 1.1 riastrad hrtimer_fire(void *cookie)
71 1.1 riastrad {
72 1.1 riastrad struct hrtimer *hrt = cookie;
73 1.1 riastrad
74 1.1 riastrad switch ((*hrt->function)(hrt)) {
75 1.1 riastrad case HRTIMER_RESTART:
76 1.1 riastrad _hrtimer_schedule(hrt);
77 1.1 riastrad break;
78 1.1 riastrad case HRTIMER_NORESTART:
79 1.1 riastrad break;
80 1.1 riastrad }
81 1.1 riastrad
82 1.3 riastrad callout_ack(&hrt->hrt_ch);
83 1.1 riastrad }
84 1.1 riastrad
85 1.1 riastrad void
86 1.1 riastrad hrtimer_set_expires(struct hrtimer *hrt, ktime_t expires)
87 1.1 riastrad {
88 1.1 riastrad
89 1.3 riastrad hrt->hrt_expires = expires;
90 1.1 riastrad }
91 1.1 riastrad
92 1.1 riastrad void
93 1.1 riastrad hrtimer_add_expires_ns(struct hrtimer *hrt, uint64_t ns)
94 1.1 riastrad {
95 1.1 riastrad
96 1.3 riastrad hrt->hrt_expires = ktime_add_ns(hrt->hrt_expires, ns);
97 1.1 riastrad }
98 1.1 riastrad
99 1.1 riastrad void
100 1.1 riastrad hrtimer_start(struct hrtimer *hrt, ktime_t expires, enum hrtimer_mode mode)
101 1.1 riastrad {
102 1.1 riastrad
103 1.1 riastrad hrtimer_start_range_ns(hrt, expires, 0, mode);
104 1.1 riastrad }
105 1.1 riastrad
106 1.1 riastrad void
107 1.1 riastrad hrtimer_start_range_ns(struct hrtimer *hrt, ktime_t expires, uint64_t range_ns,
108 1.1 riastrad enum hrtimer_mode mode)
109 1.1 riastrad {
110 1.1 riastrad
111 1.3 riastrad hrt->hrt_expires = expires;
112 1.1 riastrad (void)range_ns;
113 1.3 riastrad hrt->hrt_mode = mode;
114 1.1 riastrad _hrtimer_schedule(hrt);
115 1.1 riastrad }
116 1.1 riastrad
117 1.1 riastrad int
118 1.1 riastrad hrtimer_cancel(struct hrtimer *hrt)
119 1.1 riastrad {
120 1.1 riastrad bool active;
121 1.1 riastrad
122 1.1 riastrad /*
123 1.1 riastrad * Halt the callout and ascertain whether the hrtimer was
124 1.1 riastrad * active when we invoked hrtimer_cancel.
125 1.1 riastrad */
126 1.3 riastrad if (callout_halt(&hrt->hrt_ch, NULL)) {
127 1.1 riastrad /* Callout expired, meaning it was active. */
128 1.1 riastrad active = true;
129 1.1 riastrad } else {
130 1.1 riastrad /*
131 1.1 riastrad * Callout had not yet expired. It will not expire
132 1.1 riastrad * now, so callout_pending is now stable and
133 1.1 riastrad * corresponds with whether the hrtimer was active or
134 1.1 riastrad * not.
135 1.1 riastrad */
136 1.3 riastrad active = callout_pending(&hrt->hrt_ch);
137 1.1 riastrad }
138 1.2 riastrad return active;
139 1.2 riastrad }
140 1.2 riastrad
141 1.1 riastrad bool
142 1.1 riastrad hrtimer_active(struct hrtimer *hrt)
143 1.1 riastrad {
144 1.1 riastrad
145 1.1 riastrad /*
146 1.1 riastrad * If the callout has been scheduled, but has not yet fired,
147 1.1 riastrad * then it is pending.
148 1.1 riastrad *
149 1.1 riastrad * If the callout has fired, but has not yet reached
150 1.1 riastrad * callout_ack, then it is invoking.
151 1.1 riastrad */
152 1.3 riastrad return callout_pending(&hrt->hrt_ch) || callout_invoking(&hrt->hrt_ch);
153 1.1 riastrad }
154 1.1 riastrad
155 1.1 riastrad uint64_t
156 1.1 riastrad hrtimer_forward(struct hrtimer *hrt, ktime_t now, ktime_t period)
157 1.1 riastrad {
158 1.1 riastrad uint64_t now_ms, period_ms, expires_ms, nperiods;
159 1.1 riastrad
160 1.3 riastrad KASSERT(!callout_pending(&hrt->hrt_ch));
161 1.1 riastrad
162 1.1 riastrad /*
163 1.1 riastrad * Can't get better than 10ms precision (or ~1ms if you set
164 1.1 riastrad * HZ=1000) so not much point in doing this arithmetic at finer
165 1.1 riastrad * resolution than ms.
166 1.1 riastrad */
167 1.1 riastrad now_ms = ktime_to_ms(now);
168 1.1 riastrad period_ms = ktime_to_ms(period);
169 1.3 riastrad expires_ms = ktime_to_ms(hrt->hrt_expires);
170 1.1 riastrad
171 1.1 riastrad /* If it hasn't yet expired, no overruns. */
172 1.1 riastrad if (now_ms < expires_ms)
173 1.1 riastrad return 0;
174 1.1 riastrad
175 1.1 riastrad /* Advance it by as many periods as it should have fired. */
176 1.1 riastrad /* XXX fenceposts */
177 1.1 riastrad nperiods = howmany(now_ms - expires_ms, period_ms);
178 1.3 riastrad hrt->hrt_expires = ktime_add_ns(hrt->hrt_expires,
179 1.3 riastrad 1000000*nperiods*period_ms);
180 1.1 riastrad
181 1.1 riastrad return nperiods;
182 1.1 riastrad }
183 1.1 riastrad
184 1.1 riastrad uint64_t
185 1.1 riastrad hrtimer_forward_now(struct hrtimer *hrt, ktime_t period)
186 1.1 riastrad {
187 1.1 riastrad
188 1.1 riastrad return hrtimer_forward(hrt, ktime_get(), period);
189 1.1 riastrad }
190