13446Smrj /*
23446Smrj * CDDL HEADER START
33446Smrj *
43446Smrj * The contents of this file are subject to the terms of the
53446Smrj * Common Development and Distribution License (the "License").
63446Smrj * You may not use this file except in compliance with the License.
73446Smrj *
83446Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj * or http://www.opensolaris.org/os/licensing.
103446Smrj * See the License for the specific language governing permissions
113446Smrj * and limitations under the License.
123446Smrj *
133446Smrj * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj * If applicable, add the following below this CDDL HEADER, with the
163446Smrj * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj *
193446Smrj * CDDL HEADER END
203446Smrj */
213446Smrj
223446Smrj /*
23*11752STrevor.Thompson@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
243446Smrj * Use is subject to license terms.
253446Smrj */
263446Smrj
273446Smrj /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
283446Smrj /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
293446Smrj /* All Rights Reserved */
303446Smrj
313446Smrj #include <sys/param.h>
323446Smrj #include <sys/time.h>
333446Smrj #include <sys/systm.h>
343446Smrj #include <sys/archsystm.h>
353446Smrj #include <sys/lockstat.h>
363446Smrj
373446Smrj #include <sys/clock.h>
383446Smrj #include <sys/debug.h>
393446Smrj #include <sys/smp_impldefs.h>
403446Smrj #include <sys/rtc.h>
413446Smrj
423446Smrj /*
433446Smrj * This file contains all generic part of clock and timer handling.
443446Smrj * Specifics are now in separate files and may be overridden by TOD
453446Smrj * modules.
463446Smrj */
473446Smrj
483446Smrj char *tod_module_name; /* Settable in /etc/system */
493446Smrj
50*11752STrevor.Thompson@Sun.COM extern void tod_set_prev(timestruc_t);
51*11752STrevor.Thompson@Sun.COM
523446Smrj /*
533446Smrj * Write the specified time into the clock chip.
543446Smrj * Must be called with tod_lock held.
553446Smrj */
563446Smrj void
tod_set(timestruc_t ts)573446Smrj tod_set(timestruc_t ts)
583446Smrj {
593446Smrj ASSERT(MUTEX_HELD(&tod_lock));
603446Smrj
61*11752STrevor.Thompson@Sun.COM tod_set_prev(ts); /* for tod_validate() */
623446Smrj TODOP_SET(tod_ops, ts);
63*11752STrevor.Thompson@Sun.COM tod_status_set(TOD_SET_DONE); /* TOD was modified */
643446Smrj }
653446Smrj
663446Smrj /*
673446Smrj * Read the current time from the clock chip and convert to UNIX form.
683446Smrj * Assumes that the year in the clock chip is valid.
693446Smrj * Must be called with tod_lock held.
703446Smrj */
713446Smrj timestruc_t
tod_get(void)723446Smrj tod_get(void)
733446Smrj {
743446Smrj timestruc_t ts;
753446Smrj
763446Smrj ASSERT(MUTEX_HELD(&tod_lock));
773446Smrj
783446Smrj ts = TODOP_GET(tod_ops);
793446Smrj ts.tv_sec = tod_validate(ts.tv_sec);
803446Smrj return (ts);
813446Smrj }
823446Smrj
833446Smrj /*
843446Smrj * The following wrappers have been added so that locking
853446Smrj * can be exported to platform-independent clock routines
863446Smrj * (ie adjtime(), clock_setttime()), via a functional interface.
873446Smrj */
883446Smrj int
hr_clock_lock(void)893446Smrj hr_clock_lock(void)
903446Smrj {
913446Smrj ushort_t s;
923446Smrj
933446Smrj CLOCK_LOCK(&s);
943446Smrj return (s);
953446Smrj }
963446Smrj
973446Smrj void
hr_clock_unlock(int s)983446Smrj hr_clock_unlock(int s)
993446Smrj {
1003446Smrj CLOCK_UNLOCK(s);
1013446Smrj }
1023446Smrj
1033446Smrj /*
1043446Smrj * Support routines for horrid GMT lag handling
1053446Smrj */
1063446Smrj
1073446Smrj static time_t gmt_lag; /* offset in seconds of gmt to local time */
1083446Smrj
1093446Smrj void
sgmtl(time_t arg)1103446Smrj sgmtl(time_t arg)
1113446Smrj {
1123446Smrj gmt_lag = arg;
1133446Smrj }
1143446Smrj
1153446Smrj time_t
ggmtl(void)1163446Smrj ggmtl(void)
1173446Smrj {
1183446Smrj return (gmt_lag);
1193446Smrj }
1203446Smrj
1213446Smrj /* rtcsync() - set 'time', assuming RTC and GMT lag are correct */
1223446Smrj
1233446Smrj void
rtcsync(void)1243446Smrj rtcsync(void)
1253446Smrj {
1263446Smrj timestruc_t ts;
1273446Smrj
1283446Smrj mutex_enter(&tod_lock);
1293446Smrj ts = TODOP_GET(tod_ops);
1303446Smrj set_hrestime(&ts);
1313446Smrj mutex_exit(&tod_lock);
1323446Smrj }
133