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