1ae115bc7Smrj /*
2ae115bc7Smrj * CDDL HEADER START
3ae115bc7Smrj *
4ae115bc7Smrj * The contents of this file are subject to the terms of the
5ae115bc7Smrj * Common Development and Distribution License (the "License").
6ae115bc7Smrj * You may not use this file except in compliance with the License.
7ae115bc7Smrj *
8ae115bc7Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ae115bc7Smrj * or http://www.opensolaris.org/os/licensing.
10ae115bc7Smrj * See the License for the specific language governing permissions
11ae115bc7Smrj * and limitations under the License.
12ae115bc7Smrj *
13ae115bc7Smrj * When distributing Covered Code, include this CDDL HEADER in each
14ae115bc7Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ae115bc7Smrj * If applicable, add the following below this CDDL HEADER, with the
16ae115bc7Smrj * fields enclosed by brackets "[]" replaced with your own identifying
17ae115bc7Smrj * information: Portions Copyright [yyyy] [name of copyright owner]
18ae115bc7Smrj *
19ae115bc7Smrj * CDDL HEADER END
20ae115bc7Smrj */
21ae115bc7Smrj
22ae115bc7Smrj /*
23*8fc99e42STrevor Thompson * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24ae115bc7Smrj * Use is subject to license terms.
25ae115bc7Smrj */
26ae115bc7Smrj
27ae115bc7Smrj /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
28ae115bc7Smrj /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
29ae115bc7Smrj /* All Rights Reserved */
30ae115bc7Smrj
31ae115bc7Smrj #include <sys/param.h>
32ae115bc7Smrj #include <sys/time.h>
33ae115bc7Smrj #include <sys/systm.h>
34ae115bc7Smrj #include <sys/archsystm.h>
35ae115bc7Smrj #include <sys/lockstat.h>
36ae115bc7Smrj
37ae115bc7Smrj #include <sys/clock.h>
38ae115bc7Smrj #include <sys/debug.h>
39ae115bc7Smrj #include <sys/smp_impldefs.h>
40ae115bc7Smrj #include <sys/rtc.h>
41ae115bc7Smrj
42ae115bc7Smrj /*
43ae115bc7Smrj * This file contains all generic part of clock and timer handling.
44ae115bc7Smrj * Specifics are now in separate files and may be overridden by TOD
45ae115bc7Smrj * modules.
46ae115bc7Smrj */
47ae115bc7Smrj
48ae115bc7Smrj char *tod_module_name; /* Settable in /etc/system */
49ae115bc7Smrj
50*8fc99e42STrevor Thompson extern void tod_set_prev(timestruc_t);
51*8fc99e42STrevor Thompson
52ae115bc7Smrj /*
53ae115bc7Smrj * Write the specified time into the clock chip.
54ae115bc7Smrj * Must be called with tod_lock held.
55ae115bc7Smrj */
56ae115bc7Smrj void
tod_set(timestruc_t ts)57ae115bc7Smrj tod_set(timestruc_t ts)
58ae115bc7Smrj {
59ae115bc7Smrj ASSERT(MUTEX_HELD(&tod_lock));
60ae115bc7Smrj
61*8fc99e42STrevor Thompson tod_set_prev(ts); /* for tod_validate() */
62ae115bc7Smrj TODOP_SET(tod_ops, ts);
63*8fc99e42STrevor Thompson tod_status_set(TOD_SET_DONE); /* TOD was modified */
64ae115bc7Smrj }
65ae115bc7Smrj
66ae115bc7Smrj /*
67ae115bc7Smrj * Read the current time from the clock chip and convert to UNIX form.
68ae115bc7Smrj * Assumes that the year in the clock chip is valid.
69ae115bc7Smrj * Must be called with tod_lock held.
70ae115bc7Smrj */
71ae115bc7Smrj timestruc_t
tod_get(void)72ae115bc7Smrj tod_get(void)
73ae115bc7Smrj {
74ae115bc7Smrj timestruc_t ts;
75ae115bc7Smrj
76ae115bc7Smrj ASSERT(MUTEX_HELD(&tod_lock));
77ae115bc7Smrj
78ae115bc7Smrj ts = TODOP_GET(tod_ops);
79ae115bc7Smrj ts.tv_sec = tod_validate(ts.tv_sec);
80ae115bc7Smrj return (ts);
81ae115bc7Smrj }
82ae115bc7Smrj
83ae115bc7Smrj /*
84ae115bc7Smrj * The following wrappers have been added so that locking
85ae115bc7Smrj * can be exported to platform-independent clock routines
86ae115bc7Smrj * (ie adjtime(), clock_setttime()), via a functional interface.
87ae115bc7Smrj */
88ae115bc7Smrj int
hr_clock_lock(void)89ae115bc7Smrj hr_clock_lock(void)
90ae115bc7Smrj {
91ae115bc7Smrj ushort_t s;
92ae115bc7Smrj
93ae115bc7Smrj CLOCK_LOCK(&s);
94ae115bc7Smrj return (s);
95ae115bc7Smrj }
96ae115bc7Smrj
97ae115bc7Smrj void
hr_clock_unlock(int s)98ae115bc7Smrj hr_clock_unlock(int s)
99ae115bc7Smrj {
100ae115bc7Smrj CLOCK_UNLOCK(s);
101ae115bc7Smrj }
102ae115bc7Smrj
103ae115bc7Smrj /*
104ae115bc7Smrj * Support routines for horrid GMT lag handling
105ae115bc7Smrj */
106ae115bc7Smrj
107ae115bc7Smrj static time_t gmt_lag; /* offset in seconds of gmt to local time */
108ae115bc7Smrj
109ae115bc7Smrj void
sgmtl(time_t arg)110ae115bc7Smrj sgmtl(time_t arg)
111ae115bc7Smrj {
112ae115bc7Smrj gmt_lag = arg;
113ae115bc7Smrj }
114ae115bc7Smrj
115ae115bc7Smrj time_t
ggmtl(void)116ae115bc7Smrj ggmtl(void)
117ae115bc7Smrj {
118ae115bc7Smrj return (gmt_lag);
119ae115bc7Smrj }
120ae115bc7Smrj
121ae115bc7Smrj /* rtcsync() - set 'time', assuming RTC and GMT lag are correct */
122ae115bc7Smrj
123ae115bc7Smrj void
rtcsync(void)124ae115bc7Smrj rtcsync(void)
125ae115bc7Smrj {
126ae115bc7Smrj timestruc_t ts;
127ae115bc7Smrj
128ae115bc7Smrj mutex_enter(&tod_lock);
129ae115bc7Smrj ts = TODOP_GET(tod_ops);
130ae115bc7Smrj set_hrestime(&ts);
131ae115bc7Smrj mutex_exit(&tod_lock);
132ae115bc7Smrj }
133