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