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