xref: /freebsd/sys/kern/subr_rtc.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
19454b2d8SWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4d7f7792eSThomas Moestl  * Copyright (c) 1988 University of Utah.
5d7f7792eSThomas Moestl  * Copyright (c) 1982, 1990, 1993
6b0fdc837SLawrence Stewart  *	The Regents of the University of California.
7b0fdc837SLawrence Stewart  * Copyright (c) 2011 The FreeBSD Foundation
8b0fdc837SLawrence Stewart  * All rights reserved.
9d7f7792eSThomas Moestl  *
10d7f7792eSThomas Moestl  * This code is derived from software contributed to Berkeley by
11d7f7792eSThomas Moestl  * the Systems Programming Group of the University of Utah Computer
12d7f7792eSThomas Moestl  * Science Department.
13d7f7792eSThomas Moestl  *
14b0fdc837SLawrence Stewart  * Portions of this software were developed by Julien Ridoux at the University
15b0fdc837SLawrence Stewart  * of Melbourne under sponsorship from the FreeBSD Foundation.
16b0fdc837SLawrence Stewart  *
17d7f7792eSThomas Moestl  * Redistribution and use in source and binary forms, with or without
18d7f7792eSThomas Moestl  * modification, are permitted provided that the following conditions
19d7f7792eSThomas Moestl  * are met:
20d7f7792eSThomas Moestl  * 1. Redistributions of source code must retain the above copyright
21d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer.
22d7f7792eSThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
23d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
24d7f7792eSThomas Moestl  *    documentation and/or other materials provided with the distribution.
2569a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
26d7f7792eSThomas Moestl  *    may be used to endorse or promote products derived from this software
27d7f7792eSThomas Moestl  *    without specific prior written permission.
28d7f7792eSThomas Moestl  *
29d7f7792eSThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30d7f7792eSThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31d7f7792eSThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32d7f7792eSThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33d7f7792eSThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34d7f7792eSThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35d7f7792eSThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36d7f7792eSThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37d7f7792eSThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38d7f7792eSThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39d7f7792eSThomas Moestl  * SUCH DAMAGE.
40d7f7792eSThomas Moestl  *
41d7f7792eSThomas Moestl  *	from: Utah $Hdr: clock.c 1.18 91/01/21$
42d7f7792eSThomas Moestl  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
43d7f7792eSThomas Moestl  *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
44d7f7792eSThomas Moestl  *	and
45d7f7792eSThomas Moestl  *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
46d7f7792eSThomas Moestl  */
47d7f7792eSThomas Moestl 
48d7f7792eSThomas Moestl /*
49d7f7792eSThomas Moestl  * Helpers for time-of-day clocks. This is useful for architectures that need
50d7f7792eSThomas Moestl  * support multiple models of such clocks, and generally serves to make the
51d7f7792eSThomas Moestl  * code more machine-independent.
52d7f7792eSThomas Moestl  * If the clock in question can also be used as a time counter, the driver
53d7f7792eSThomas Moestl  * needs to initiate this.
54d7f7792eSThomas Moestl  * This code is not yet used by all architectures.
55d7f7792eSThomas Moestl  */
56d7f7792eSThomas Moestl 
57677b542eSDavid E. O'Brien #include <sys/cdefs.h>
58677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
59677b542eSDavid E. O'Brien 
60b0fdc837SLawrence Stewart #include "opt_ffclock.h"
61b0fdc837SLawrence Stewart 
62d7f7792eSThomas Moestl #include <sys/param.h>
63d7f7792eSThomas Moestl #include <sys/systm.h>
64d7f7792eSThomas Moestl #include <sys/kernel.h>
65d7f7792eSThomas Moestl #include <sys/bus.h>
66d7f7792eSThomas Moestl #include <sys/clock.h>
673a0e6f92SKonstantin Belousov #include <sys/lock.h>
68a8d7b9d3SIan Lepore #include <sys/malloc.h>
69a8d7b9d3SIan Lepore #include <sys/sx.h>
70d7f7792eSThomas Moestl #include <sys/sysctl.h>
71a8d7b9d3SIan Lepore #include <sys/taskqueue.h>
72b0fdc837SLawrence Stewart #ifdef FFCLOCK
73b0fdc837SLawrence Stewart #include <sys/timeffc.h>
74b0fdc837SLawrence Stewart #endif
75d7f7792eSThomas Moestl #include <sys/timetc.h>
76d7f7792eSThomas Moestl 
77d7f7792eSThomas Moestl #include "clock_if.h"
78d7f7792eSThomas Moestl 
799b4a8ab7SPoul-Henning Kamp /* XXX: should be kern. now, it's no longer machdep.  */
809b4a8ab7SPoul-Henning Kamp static int disable_rtc_set;
810674ea6fSJung-uk Kim SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set,
820674ea6fSJung-uk Kim     0, "Disallow adjusting time-of-day clock");
839b4a8ab7SPoul-Henning Kamp 
84a8d7b9d3SIan Lepore /*
85a8d7b9d3SIan Lepore  * An instance of a realtime clock.  A list of these tracks all the registered
86a8d7b9d3SIan Lepore  * clocks in the system.
87a8d7b9d3SIan Lepore  *
88a8d7b9d3SIan Lepore  * The resadj member is used to apply a "resolution adjustment" equal to half
89a8d7b9d3SIan Lepore  * the clock's resolution, which is useful mainly on clocks with a whole-second
90a8d7b9d3SIan Lepore  * resolution.  Because the clock truncates the fractional part, adding half the
91a8d7b9d3SIan Lepore  * resolution performs 4/5 rounding.  The same adjustment is applied to the
92a8d7b9d3SIan Lepore  * times returned from clock_gettime(), because the fraction returned will
93a8d7b9d3SIan Lepore  * always be zero, but on average the actual fraction at the time of the call
94a8d7b9d3SIan Lepore  * should be about .5.
95a8d7b9d3SIan Lepore  */
96a8d7b9d3SIan Lepore struct rtc_instance {
97a8d7b9d3SIan Lepore 	device_t	clockdev;
98a8d7b9d3SIan Lepore 	int		resolution;
99a8d7b9d3SIan Lepore 	int		flags;
100cd9d9e54SIan Lepore 	u_int		schedns;
101a8d7b9d3SIan Lepore 	struct timespec resadj;
102cd9d9e54SIan Lepore 	struct timeout_task
103cd9d9e54SIan Lepore 			stask;
104a8d7b9d3SIan Lepore 	LIST_ENTRY(rtc_instance)
105a8d7b9d3SIan Lepore 			rtc_entries;
106a8d7b9d3SIan Lepore };
107a8d7b9d3SIan Lepore 
108a8d7b9d3SIan Lepore /*
109a8d7b9d3SIan Lepore  * Clocks are updated using a task running on taskqueue_thread.
110a8d7b9d3SIan Lepore  */
111a8d7b9d3SIan Lepore static void settime_task_func(void *arg, int pending);
112a8d7b9d3SIan Lepore 
113a8d7b9d3SIan Lepore /*
114a8d7b9d3SIan Lepore  * Registered clocks are kept in a list which is sorted by resolution; the more
115a8d7b9d3SIan Lepore  * accurate clocks get the first shot at providing the time.
116a8d7b9d3SIan Lepore  */
117a8d7b9d3SIan Lepore LIST_HEAD(rtc_listhead, rtc_instance);
118a8d7b9d3SIan Lepore static struct rtc_listhead rtc_list = LIST_HEAD_INITIALIZER(rtc_list);
119a8d7b9d3SIan Lepore static struct sx rtc_list_lock;
120a8d7b9d3SIan Lepore SX_SYSINIT(rtc_list_lock_init, &rtc_list_lock, "rtc list");
121a8d7b9d3SIan Lepore 
122a8d7b9d3SIan Lepore /*
123cd9d9e54SIan Lepore  * On the task thread, invoke the clock_settime() method of the clock.  Do so
124cd9d9e54SIan Lepore  * holding no locks, so that clock drivers are free to do whatever kind of
125cd9d9e54SIan Lepore  * locking or sleeping they need to.
126a8d7b9d3SIan Lepore  */
127a8d7b9d3SIan Lepore static void
128a8d7b9d3SIan Lepore settime_task_func(void *arg, int pending)
129a8d7b9d3SIan Lepore {
130a8d7b9d3SIan Lepore 	struct timespec ts;
131a8d7b9d3SIan Lepore 	struct rtc_instance *rtc;
132a8d7b9d3SIan Lepore 
133cd9d9e54SIan Lepore 	rtc = arg;
134a8d7b9d3SIan Lepore 	if (!(rtc->flags & CLOCKF_SETTIME_NO_TS)) {
135a8d7b9d3SIan Lepore 		getnanotime(&ts);
136a8d7b9d3SIan Lepore 		if (!(rtc->flags & CLOCKF_SETTIME_NO_ADJ)) {
137a8d7b9d3SIan Lepore 			ts.tv_sec -= utc_offset();
138a8d7b9d3SIan Lepore 			timespecadd(&ts, &rtc->resadj);
139a8d7b9d3SIan Lepore 		}
140a8d7b9d3SIan Lepore 	} else {
141a8d7b9d3SIan Lepore 		ts.tv_sec  = 0;
142a8d7b9d3SIan Lepore 		ts.tv_nsec = 0;
143a8d7b9d3SIan Lepore 	}
144a8d7b9d3SIan Lepore 	CLOCK_SETTIME(rtc->clockdev, &ts);
145a8d7b9d3SIan Lepore }
146a8d7b9d3SIan Lepore 
147d7f7792eSThomas Moestl void
148a8d7b9d3SIan Lepore clock_register_flags(device_t clockdev, long resolution, int flags)
149a8d7b9d3SIan Lepore {
150a8d7b9d3SIan Lepore 	struct rtc_instance *rtc, *newrtc;
151a8d7b9d3SIan Lepore 
152a8d7b9d3SIan Lepore 	newrtc = malloc(sizeof(*newrtc), M_DEVBUF, M_WAITOK);
153a8d7b9d3SIan Lepore 	newrtc->clockdev = clockdev;
154a8d7b9d3SIan Lepore 	newrtc->resolution = (int)resolution;
155a8d7b9d3SIan Lepore 	newrtc->flags = flags;
156cd9d9e54SIan Lepore 	newrtc->schedns = 0;
157a8d7b9d3SIan Lepore 	newrtc->resadj.tv_sec  = newrtc->resolution / 2 / 1000000;
158a8d7b9d3SIan Lepore 	newrtc->resadj.tv_nsec = newrtc->resolution / 2 % 1000000 * 1000;
159cd9d9e54SIan Lepore 	TIMEOUT_TASK_INIT(taskqueue_thread, &newrtc->stask, 0,
160cd9d9e54SIan Lepore 		    settime_task_func, newrtc);
161a8d7b9d3SIan Lepore 
162a8d7b9d3SIan Lepore 	sx_xlock(&rtc_list_lock);
163a8d7b9d3SIan Lepore 	if (LIST_EMPTY(&rtc_list)) {
164a8d7b9d3SIan Lepore 		LIST_INSERT_HEAD(&rtc_list, newrtc, rtc_entries);
165a8d7b9d3SIan Lepore 	} else {
166a8d7b9d3SIan Lepore 		LIST_FOREACH(rtc, &rtc_list, rtc_entries) {
167a8d7b9d3SIan Lepore 			if (rtc->resolution > newrtc->resolution) {
168a8d7b9d3SIan Lepore 				LIST_INSERT_BEFORE(rtc, newrtc, rtc_entries);
169a8d7b9d3SIan Lepore 				break;
170a8d7b9d3SIan Lepore 			} else if (LIST_NEXT(rtc, rtc_entries) == NULL) {
171a8d7b9d3SIan Lepore 				LIST_INSERT_AFTER(rtc, newrtc, rtc_entries);
172a8d7b9d3SIan Lepore 				break;
173a8d7b9d3SIan Lepore 			}
174a8d7b9d3SIan Lepore 		}
175a8d7b9d3SIan Lepore 	}
176a8d7b9d3SIan Lepore 	sx_xunlock(&rtc_list_lock);
177a8d7b9d3SIan Lepore 
178a8d7b9d3SIan Lepore 	device_printf(clockdev,
179a8d7b9d3SIan Lepore 	    "registered as a time-of-day clock, resolution %d.%6.6ds\n",
180a8d7b9d3SIan Lepore 	    newrtc->resolution / 1000000, newrtc->resolution % 1000000);
181a8d7b9d3SIan Lepore }
182a8d7b9d3SIan Lepore 
183a8d7b9d3SIan Lepore void
184a8d7b9d3SIan Lepore clock_register(device_t dev, long res)
185d7f7792eSThomas Moestl {
186d7f7792eSThomas Moestl 
187a8d7b9d3SIan Lepore 	clock_register_flags(dev, res, 0);
1880674ea6fSJung-uk Kim }
189a8d7b9d3SIan Lepore 
190a8d7b9d3SIan Lepore void
191a8d7b9d3SIan Lepore clock_unregister(device_t clockdev)
192a8d7b9d3SIan Lepore {
193a8d7b9d3SIan Lepore 	struct rtc_instance *rtc, *tmp;
194a8d7b9d3SIan Lepore 
195a8d7b9d3SIan Lepore 	sx_xlock(&rtc_list_lock);
196a8d7b9d3SIan Lepore 	LIST_FOREACH_SAFE(rtc, &rtc_list, rtc_entries, tmp) {
197a8d7b9d3SIan Lepore 		if (rtc->clockdev == clockdev) {
198a8d7b9d3SIan Lepore 			LIST_REMOVE(rtc, rtc_entries);
199cd9d9e54SIan Lepore 			break;
200cd9d9e54SIan Lepore 		}
201cd9d9e54SIan Lepore 	}
202cd9d9e54SIan Lepore 	sx_xunlock(&rtc_list_lock);
203cd9d9e54SIan Lepore 	if (rtc != NULL) {
204cd9d9e54SIan Lepore 		taskqueue_cancel_timeout(taskqueue_thread, &rtc->stask, NULL);
205cd9d9e54SIan Lepore 		taskqueue_drain_timeout(taskqueue_thread, &rtc->stask);
206a8d7b9d3SIan Lepore                 free(rtc, M_DEVBUF);
207d7f7792eSThomas Moestl 	}
208a8d7b9d3SIan Lepore }
209cd9d9e54SIan Lepore 
210cd9d9e54SIan Lepore void
211cd9d9e54SIan Lepore clock_schedule(device_t clockdev, u_int offsetns)
212cd9d9e54SIan Lepore {
213cd9d9e54SIan Lepore 	struct rtc_instance *rtc;
214cd9d9e54SIan Lepore 
215cd9d9e54SIan Lepore 	sx_xlock(&rtc_list_lock);
216cd9d9e54SIan Lepore 	LIST_FOREACH(rtc, &rtc_list, rtc_entries) {
217cd9d9e54SIan Lepore 		if (rtc->clockdev == clockdev) {
218cd9d9e54SIan Lepore 			rtc->schedns = offsetns;
219cd9d9e54SIan Lepore 			break;
220cd9d9e54SIan Lepore 		}
221cd9d9e54SIan Lepore 	}
222a8d7b9d3SIan Lepore 	sx_xunlock(&rtc_list_lock);
223d7f7792eSThomas Moestl }
224d7f7792eSThomas Moestl 
225d7f7792eSThomas Moestl /*
226a8d7b9d3SIan Lepore  * Initialize the system time.  Must be called from a context which does not
227a8d7b9d3SIan Lepore  * restrict any locking or sleeping that clock drivers may need to do.
228a8d7b9d3SIan Lepore  *
229a8d7b9d3SIan Lepore  * First attempt to get the time from a registered realtime clock.  The clocks
230a8d7b9d3SIan Lepore  * are queried in order of resolution until one provides the time.  If no clock
231a8d7b9d3SIan Lepore  * can provide the current time, use the 'base' time provided by the caller, if
232a8d7b9d3SIan Lepore  * non-zero.  The 'base' time is potentially highly inaccurate, such as the last
233a8d7b9d3SIan Lepore  * known good value of the system clock, or even a filesystem last-updated
234a8d7b9d3SIan Lepore  * timestamp.  It is used to prevent system time from appearing to move
235a8d7b9d3SIan Lepore  * backwards in logs.
236d7f7792eSThomas Moestl  */
237d7f7792eSThomas Moestl void
238d7f7792eSThomas Moestl inittodr(time_t base)
239d7f7792eSThomas Moestl {
2400674ea6fSJung-uk Kim 	struct timespec ts;
241a8d7b9d3SIan Lepore 	struct rtc_instance *rtc;
242d7f7792eSThomas Moestl 	int error;
243d7f7792eSThomas Moestl 
244a8d7b9d3SIan Lepore 	error = ENXIO;
245a8d7b9d3SIan Lepore 	sx_xlock(&rtc_list_lock);
246a8d7b9d3SIan Lepore 	LIST_FOREACH(rtc, &rtc_list, rtc_entries) {
247a8d7b9d3SIan Lepore 		if ((error = CLOCK_GETTIME(rtc->clockdev, &ts)) != 0)
248a8d7b9d3SIan Lepore 			continue;
249a8d7b9d3SIan Lepore 		if (ts.tv_sec < 0 || ts.tv_nsec < 0) {
250a8d7b9d3SIan Lepore 			error = EINVAL;
251a8d7b9d3SIan Lepore 			continue;
252d7f7792eSThomas Moestl 		}
253a8d7b9d3SIan Lepore 		if (!(rtc->flags & CLOCKF_GETTIME_NO_ADJ)) {
254a8d7b9d3SIan Lepore 			timespecadd(&ts, &rtc->resadj);
255a8d7b9d3SIan Lepore 			ts.tv_sec += utc_offset();
256d7f7792eSThomas Moestl 		}
257a8d7b9d3SIan Lepore 		if (bootverbose)
258a8d7b9d3SIan Lepore 			device_printf(rtc->clockdev,
259a8d7b9d3SIan Lepore 			    "providing initial system time\n");
260a8d7b9d3SIan Lepore 		break;
261a8d7b9d3SIan Lepore 	}
262a8d7b9d3SIan Lepore 	sx_xunlock(&rtc_list_lock);
263a8d7b9d3SIan Lepore 
264a8d7b9d3SIan Lepore 	/*
265a8d7b9d3SIan Lepore 	 * Do not report errors from each clock; it is expected that some clocks
266a8d7b9d3SIan Lepore 	 * cannot provide results in some situations.  Only report problems when
267a8d7b9d3SIan Lepore 	 * no clocks could provide the time.
268a8d7b9d3SIan Lepore 	 */
269a8d7b9d3SIan Lepore 	if (error != 0) {
270a8d7b9d3SIan Lepore 		switch (error) {
271a8d7b9d3SIan Lepore 		case ENXIO:
272a8d7b9d3SIan Lepore 			printf("Warning: no time-of-day clock registered, ");
273a8d7b9d3SIan Lepore 			break;
274a8d7b9d3SIan Lepore 		case EINVAL:
275a8d7b9d3SIan Lepore 			printf("Warning: bad time from time-of-day clock, ");
276a8d7b9d3SIan Lepore 			break;
277a8d7b9d3SIan Lepore 		default:
278a8d7b9d3SIan Lepore 			printf("Error reading time-of-day clock (%d), ", error);
279a8d7b9d3SIan Lepore 			break;
280a8d7b9d3SIan Lepore 		}
281a8d7b9d3SIan Lepore 		printf("system time will not be set accurately\n");
282a8d7b9d3SIan Lepore 		ts.tv_sec  = (base > 0) ? base : -1;
283a8d7b9d3SIan Lepore 		ts.tv_nsec = 0;
284d7f7792eSThomas Moestl 	}
285d7f7792eSThomas Moestl 
286a8d7b9d3SIan Lepore 	if (ts.tv_sec >= 0) {
287d7f7792eSThomas Moestl 		tc_setclock(&ts);
288b0fdc837SLawrence Stewart #ifdef FFCLOCK
289b0fdc837SLawrence Stewart 		ffclock_reset_clock(&ts);
290b0fdc837SLawrence Stewart #endif
291d7f7792eSThomas Moestl 	}
292d7f7792eSThomas Moestl }
293d7f7792eSThomas Moestl 
294d7f7792eSThomas Moestl /*
295a8d7b9d3SIan Lepore  * Write system time back to all registered clocks, unless disabled by admin.
296a8d7b9d3SIan Lepore  * This can be called from a context that restricts locking and/or sleeping; the
297a8d7b9d3SIan Lepore  * actual updating is done asynchronously on a task thread.
298d7f7792eSThomas Moestl  */
299d7f7792eSThomas Moestl void
3009483543dSWarner Losh resettodr(void)
301d7f7792eSThomas Moestl {
302cd9d9e54SIan Lepore 	struct timespec now;
303cd9d9e54SIan Lepore 	struct rtc_instance *rtc;
304cd9d9e54SIan Lepore 	sbintime_t sbt;
305cd9d9e54SIan Lepore 	long waitns;
306d7f7792eSThomas Moestl 
307a8d7b9d3SIan Lepore 	if (disable_rtc_set)
308d7f7792eSThomas Moestl 		return;
309d7f7792eSThomas Moestl 
310cd9d9e54SIan Lepore 	sx_xlock(&rtc_list_lock);
311cd9d9e54SIan Lepore 	LIST_FOREACH(rtc, &rtc_list, rtc_entries) {
312cd9d9e54SIan Lepore 		if (rtc->schedns != 0) {
313cd9d9e54SIan Lepore 			getnanotime(&now);
314cd9d9e54SIan Lepore 			waitns = rtc->schedns - now.tv_nsec;
315cd9d9e54SIan Lepore 			if (waitns < 0)
316cd9d9e54SIan Lepore 				waitns += 1000000000;
317cd9d9e54SIan Lepore 			sbt = nstosbt(waitns);
318cd9d9e54SIan Lepore 		} else
319cd9d9e54SIan Lepore 			sbt = 0;
320cd9d9e54SIan Lepore 		taskqueue_enqueue_timeout_sbt(taskqueue_thread,
321cd9d9e54SIan Lepore 		    &rtc->stask, -sbt, 0, C_PREL(31));
322cd9d9e54SIan Lepore 	}
323cd9d9e54SIan Lepore 	sx_xunlock(&rtc_list_lock);
324d7f7792eSThomas Moestl }
325