Index: head/sys/kern/subr_rtc.c =================================================================== --- head/sys/kern/subr_rtc.c (revision 329171) +++ head/sys/kern/subr_rtc.c (revision 329172) @@ -1,324 +1,382 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1988 University of Utah. * Copyright (c) 1982, 1990, 1993 * The Regents of the University of California. * Copyright (c) 2011 The FreeBSD Foundation * All rights reserved. * * This code is derived from software contributed to Berkeley by * the Systems Programming Group of the University of Utah Computer * Science Department. * * Portions of this software were developed by Julien Ridoux at the University * of Melbourne under sponsorship from the FreeBSD Foundation. * * 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. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * from: Utah $Hdr: clock.c 1.18 91/01/21$ * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp * and * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 */ /* * Helpers for time-of-day clocks. This is useful for architectures that need * support multiple models of such clocks, and generally serves to make the * code more machine-independent. * If the clock in question can also be used as a time counter, the driver * needs to initiate this. * This code is not yet used by all architectures. */ #include __FBSDID("$FreeBSD$"); #include "opt_ffclock.h" #include #include #include #include #include #include #include #include #include #include #ifdef FFCLOCK #include #endif #include #include "clock_if.h" +static int show_io; +SYSCTL_INT(_debug, OID_AUTO, clock_show_io, CTLFLAG_RWTUN, &show_io, 0, + "Enable debug printing of RTC clock I/O; 1=reads, 2=writes, 3=both."); + /* XXX: should be kern. now, it's no longer machdep. */ static int disable_rtc_set; SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set, CTLFLAG_RW, &disable_rtc_set, 0, "Disallow adjusting time-of-day clock"); /* * An instance of a realtime clock. A list of these tracks all the registered * clocks in the system. * * The resadj member is used to apply a "resolution adjustment" equal to half * the clock's resolution, which is useful mainly on clocks with a whole-second * resolution. Because the clock truncates the fractional part, adding half the * resolution performs 4/5 rounding. The same adjustment is applied to the * times returned from clock_gettime(), because the fraction returned will * always be zero, but on average the actual fraction at the time of the call * should be about .5. */ struct rtc_instance { device_t clockdev; int resolution; int flags; u_int schedns; struct timespec resadj; struct timeout_task stask; LIST_ENTRY(rtc_instance) rtc_entries; }; /* * Clocks are updated using a task running on taskqueue_thread. */ static void settime_task_func(void *arg, int pending); /* * Registered clocks are kept in a list which is sorted by resolution; the more * accurate clocks get the first shot at providing the time. */ LIST_HEAD(rtc_listhead, rtc_instance); static struct rtc_listhead rtc_list = LIST_HEAD_INITIALIZER(rtc_list); static struct sx rtc_list_lock; SX_SYSINIT(rtc_list_lock_init, &rtc_list_lock, "rtc list"); /* * On the task thread, invoke the clock_settime() method of the clock. Do so * holding no locks, so that clock drivers are free to do whatever kind of * locking or sleeping they need to. */ static void settime_task_func(void *arg, int pending) { struct timespec ts; struct rtc_instance *rtc; rtc = arg; if (!(rtc->flags & CLOCKF_SETTIME_NO_TS)) { getnanotime(&ts); if (!(rtc->flags & CLOCKF_SETTIME_NO_ADJ)) { ts.tv_sec -= utc_offset(); timespecadd(&ts, &rtc->resadj); } } else { ts.tv_sec = 0; ts.tv_nsec = 0; } CLOCK_SETTIME(rtc->clockdev, &ts); +} + +static void +clock_dbgprint_hdr(device_t dev, int rw) +{ + struct timespec now; + + getnanotime(&now); + device_printf(dev, "%s at ", (rw & CLOCK_DBG_READ) ? "read " : "write"); + clock_print_ts(&now, 9); + printf(": "); +} + +void +clock_dbgprint_bcd(device_t dev, int rw, const struct bcd_clocktime *bct) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_bcd(bct, 9); + printf("\n"); + } +} + +void +clock_dbgprint_ct(device_t dev, int rw, const struct clocktime *ct) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_ct(ct, 9); + printf("\n"); + } +} + +void +clock_dbgprint_err(device_t dev, int rw, int err) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + printf("error = %d\n", err); + } +} + +void +clock_dbgprint_ts(device_t dev, int rw, const struct timespec *ts) +{ + + if (show_io & rw) { + clock_dbgprint_hdr(dev, rw); + clock_print_ts(ts, 9); + printf("\n"); + } } void clock_register_flags(device_t clockdev, long resolution, int flags) { struct rtc_instance *rtc, *newrtc; newrtc = malloc(sizeof(*newrtc), M_DEVBUF, M_WAITOK); newrtc->clockdev = clockdev; newrtc->resolution = (int)resolution; newrtc->flags = flags; newrtc->schedns = 0; newrtc->resadj.tv_sec = newrtc->resolution / 2 / 1000000; newrtc->resadj.tv_nsec = newrtc->resolution / 2 % 1000000 * 1000; TIMEOUT_TASK_INIT(taskqueue_thread, &newrtc->stask, 0, settime_task_func, newrtc); sx_xlock(&rtc_list_lock); if (LIST_EMPTY(&rtc_list)) { LIST_INSERT_HEAD(&rtc_list, newrtc, rtc_entries); } else { LIST_FOREACH(rtc, &rtc_list, rtc_entries) { if (rtc->resolution > newrtc->resolution) { LIST_INSERT_BEFORE(rtc, newrtc, rtc_entries); break; } else if (LIST_NEXT(rtc, rtc_entries) == NULL) { LIST_INSERT_AFTER(rtc, newrtc, rtc_entries); break; } } } sx_xunlock(&rtc_list_lock); device_printf(clockdev, "registered as a time-of-day clock, resolution %d.%6.6ds\n", newrtc->resolution / 1000000, newrtc->resolution % 1000000); } void clock_register(device_t dev, long res) { clock_register_flags(dev, res, 0); } void clock_unregister(device_t clockdev) { struct rtc_instance *rtc, *tmp; sx_xlock(&rtc_list_lock); LIST_FOREACH_SAFE(rtc, &rtc_list, rtc_entries, tmp) { if (rtc->clockdev == clockdev) { LIST_REMOVE(rtc, rtc_entries); break; } } sx_xunlock(&rtc_list_lock); if (rtc != NULL) { taskqueue_cancel_timeout(taskqueue_thread, &rtc->stask, NULL); taskqueue_drain_timeout(taskqueue_thread, &rtc->stask); free(rtc, M_DEVBUF); } } void clock_schedule(device_t clockdev, u_int offsetns) { struct rtc_instance *rtc; sx_xlock(&rtc_list_lock); LIST_FOREACH(rtc, &rtc_list, rtc_entries) { if (rtc->clockdev == clockdev) { rtc->schedns = offsetns; break; } } sx_xunlock(&rtc_list_lock); } /* * Initialize the system time. Must be called from a context which does not * restrict any locking or sleeping that clock drivers may need to do. * * First attempt to get the time from a registered realtime clock. The clocks * are queried in order of resolution until one provides the time. If no clock * can provide the current time, use the 'base' time provided by the caller, if * non-zero. The 'base' time is potentially highly inaccurate, such as the last * known good value of the system clock, or even a filesystem last-updated * timestamp. It is used to prevent system time from appearing to move * backwards in logs. */ void inittodr(time_t base) { struct timespec ts; struct rtc_instance *rtc; int error; error = ENXIO; sx_xlock(&rtc_list_lock); LIST_FOREACH(rtc, &rtc_list, rtc_entries) { if ((error = CLOCK_GETTIME(rtc->clockdev, &ts)) != 0) continue; if (ts.tv_sec < 0 || ts.tv_nsec < 0) { error = EINVAL; continue; } if (!(rtc->flags & CLOCKF_GETTIME_NO_ADJ)) { timespecadd(&ts, &rtc->resadj); ts.tv_sec += utc_offset(); } if (bootverbose) device_printf(rtc->clockdev, "providing initial system time\n"); break; } sx_xunlock(&rtc_list_lock); /* * Do not report errors from each clock; it is expected that some clocks * cannot provide results in some situations. Only report problems when * no clocks could provide the time. */ if (error != 0) { switch (error) { case ENXIO: printf("Warning: no time-of-day clock registered, "); break; case EINVAL: printf("Warning: bad time from time-of-day clock, "); break; default: printf("Error reading time-of-day clock (%d), ", error); break; } printf("system time will not be set accurately\n"); ts.tv_sec = (base > 0) ? base : -1; ts.tv_nsec = 0; } if (ts.tv_sec >= 0) { tc_setclock(&ts); #ifdef FFCLOCK ffclock_reset_clock(&ts); #endif } } /* * Write system time back to all registered clocks, unless disabled by admin. * This can be called from a context that restricts locking and/or sleeping; the * actual updating is done asynchronously on a task thread. */ void resettodr(void) { struct timespec now; struct rtc_instance *rtc; sbintime_t sbt; long waitns; if (disable_rtc_set) return; sx_xlock(&rtc_list_lock); LIST_FOREACH(rtc, &rtc_list, rtc_entries) { if (rtc->schedns != 0) { getnanotime(&now); waitns = rtc->schedns - now.tv_nsec; if (waitns < 0) waitns += 1000000000; sbt = nstosbt(waitns); } else sbt = 0; taskqueue_enqueue_timeout_sbt(taskqueue_thread, &rtc->stask, -sbt, 0, C_PREL(31)); } sx_xunlock(&rtc_list_lock); } Index: head/sys/sys/clock.h =================================================================== --- head/sys/sys/clock.h (revision 329171) +++ head/sys/sys/clock.h (revision 329172) @@ -1,196 +1,208 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1996 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Gordon W. Ross * * 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. * * $NetBSD: clock_subr.h,v 1.7 2000/10/03 13:41:07 tsutsui Exp $ * * * This file is the central clearing-house for calendrical issues. * * In general the kernel does not know about minutes, hours, days, timezones, * daylight savings time, leap-years and such. All that is theoretically a * matter for userland only. * * Parts of kernel code does however care: badly designed filesystems store * timestamps in local time and RTC chips sometimes track time in a local * timezone instead of UTC and so on. * * All that code should go here for service. * * $FreeBSD$ */ #ifndef _SYS_CLOCK_H_ #define _SYS_CLOCK_H_ #ifdef _KERNEL /* No user serviceable parts */ /* * Timezone info from settimeofday(2), usually not used */ extern int tz_minuteswest; extern int tz_dsttime; int utc_offset(void); /* * Structure to hold the values typically reported by time-of-day clocks, * expressed as binary integers (see below for a BCD version). This can be * passed to the conversion functions to be converted to/from a struct timespec. * * On input, the year is interpreted as follows: * 0 - 69 = 2000 - 2069 * 70 - 99 = 1970 - 1999 * 100 - 199 = 2000 - 2099 (Supports hardware "century bit".) * 200 - 1969 = Invalid. * 1970 - 9999 = Full 4-digit century+year. * * The dow field is ignored (not even validated) on input, but is always * populated with day-of-week on output. * * clock_ct_to_ts() returns EINVAL if any values are out of range. The year * field will always be 4-digit on output. */ struct clocktime { int year; /* year (4 digit year) */ int mon; /* month (1 - 12) */ int day; /* day (1 - 31) */ int hour; /* hour (0 - 23) */ int min; /* minute (0 - 59) */ int sec; /* second (0 - 59) */ int dow; /* day of week (0 - 6; 0 = Sunday) */ long nsec; /* nano seconds */ }; int clock_ct_to_ts(const struct clocktime *, struct timespec *); void clock_ts_to_ct(const struct timespec *, struct clocktime *); /* * Structure to hold the values typically reported by time-of-day clocks, * expressed as BCD. This can be passed to the conversion functions to be * converted to/from a struct timespec. * * The clock_bcd_to_ts() function interprets the values in the year through sec * fields as BCD numbers, and returns EINVAL if any BCD values are out of range. * After conversion to binary, the values are passed to clock_ct_to_ts() and * undergo further validation as described above. Year may be 2 or 4-digit BCD, * interpreted as described above. The nsec field is binary. If the ampm arg * is true, the incoming hour and ispm values are interpreted as 12-hour am/pm * representation of the hour, otherwise hour is interpreted as 24-hour and ispm * is ignored. * * The clock_ts_to_bcd() function converts the timespec to BCD values stored * into year through sec. The value in year will be 4-digit BCD (e.g., * 0x2017). The mon through sec values will be 2-digit BCD. The nsec field will * be binary, and the range of dow makes its binary and BCD values identical. * If the ampm arg is true, the hour and ispm fields are set to the 12-hour * time plus a pm flag, otherwise the hour is set to 24-hour time and ispm is * set to false. */ struct bcd_clocktime { uint16_t year; /* year (2 or 4 digit year) */ uint8_t mon; /* month (1 - 12) */ uint8_t day; /* day (1 - 31) */ uint8_t hour; /* hour (0 - 23 or 1 - 12) */ uint8_t min; /* minute (0 - 59) */ uint8_t sec; /* second (0 - 59) */ uint8_t dow; /* day of week (0 - 6; 0 = Sunday) */ long nsec; /* nanoseconds */ bool ispm; /* true if hour represents pm time */ }; int clock_bcd_to_ts(const struct bcd_clocktime *, struct timespec *, bool ampm); void clock_ts_to_bcd(const struct timespec *, struct bcd_clocktime *, bool ampm); /* * Time-of-day clock functions and flags. These functions might sleep. * * clock_register and clock_unregister() do what they say. Upon return from * unregister, the clock's methods are not running and will not be called again. * * clock_schedule() requests that a registered clock's clock_settime() calls * happen at the given offset into the second. The default is 0, meaning no * specific scheduling. To schedule the call as soon after top-of-second as * possible, specify 1. Each clock has its own schedule, but taskqueue_thread * is shared by many tasks; the timing of the call is not guaranteed. * * Flags: * * CLOCKF_SETTIME_NO_TS * Do not pass a timespec to clock_settime(), the driver obtains its own time * and applies its own adjustments (this flag implies CLOCKF_SETTIME_NO_ADJ). * * CLOCKF_SETTIME_NO_ADJ * Do not apply utc offset and resolution/accuracy adjustments to the value * passed to clock_settime(), the driver applies them itself. * * CLOCKF_GETTIME_NO_ADJ * Do not apply utc offset and resolution/accuracy adjustments to the value * returned from clock_gettime(), the driver has already applied them. */ #define CLOCKF_SETTIME_NO_TS 0x00000001 #define CLOCKF_SETTIME_NO_ADJ 0x00000002 #define CLOCKF_GETTIME_NO_ADJ 0x00000004 void clock_register(device_t _clockdev, long _resolution_us); void clock_register_flags(device_t _clockdev, long _resolution_us, int _flags); void clock_schedule(device_t clockdev, u_int _offsetns); void clock_unregister(device_t _clockdev); /* * BCD to decimal and decimal to BCD. */ #define FROMBCD(x) bcd2bin(x) #define TOBCD(x) bin2bcd(x) /* Some handy constants. */ #define SECDAY (24 * 60 * 60) #define SECYR (SECDAY * 365) /* Traditional POSIX base year */ #define POSIX_BASE_YEAR 1970 void timespec2fattime(const struct timespec *tsp, int utc, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp); void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, struct timespec *tsp); /* * Print a [bcd_]clocktime or timespec, optionally with fractional seconds. The * nsdig argument can range from 0-9, and specifies how many decimal digits to * display for fractional seconds. */ void clock_print_bcd(const struct bcd_clocktime *bct, int nsdig); void clock_print_ct(const struct clocktime *ct, int nsdig); void clock_print_ts(const struct timespec *ts, int nsdig); +/* + * Debugging helpers for RTC clock drivers. Print a [bcd_]clocktime or + * timespec, only if rtc clock debugging has been enabled. The rw argument is + * one of CLOCK_DBG_READ or CLOCK_DBG_WRITE. + */ +#define CLOCK_DBG_READ 0x01 +#define CLOCK_DBG_WRITE 0x02 +void clock_dbgprint_bcd(device_t dev, int rw, const struct bcd_clocktime *bct); +void clock_dbgprint_ct(device_t dev, int rw, const struct clocktime *ct); +void clock_dbgprint_err(device_t dev, int rw, int err); +void clock_dbgprint_ts(device_t dev, int rw, const struct timespec *ts); + #endif /* _KERNEL */ #endif /* !_SYS_CLOCK_H_ */