xref: /titanic_50/usr/src/uts/i86pc/io/hardclk.c (revision 8ef10d209f1443865a8ce5b1eb59838b64a64e67)
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 
50 char *tod_module_name;		/* Settable in /etc/system */
51 
52 /*
53  * Write the specified time into the clock chip.
54  * Must be called with tod_lock held.
55  */
56 void
57 tod_set(timestruc_t ts)
58 {
59 	ASSERT(MUTEX_HELD(&tod_lock));
60 
61 	/*
62 	 * Prevent false alarm in tod_validate() due to tod value change.
63 	 */
64 	tod_fault_reset();
65 	TODOP_SET(tod_ops, ts);
66 }
67 
68 /*
69  * Read the current time from the clock chip and convert to UNIX form.
70  * Assumes that the year in the clock chip is valid.
71  * Must be called with tod_lock held.
72  */
73 timestruc_t
74 tod_get(void)
75 {
76 	timestruc_t ts;
77 
78 	ASSERT(MUTEX_HELD(&tod_lock));
79 
80 	ts = TODOP_GET(tod_ops);
81 	ts.tv_sec = tod_validate(ts.tv_sec);
82 	return (ts);
83 }
84 
85 /*
86  * The following wrappers have been added so that locking
87  * can be exported to platform-independent clock routines
88  * (ie adjtime(), clock_setttime()), via a functional interface.
89  */
90 int
91 hr_clock_lock(void)
92 {
93 	ushort_t s;
94 
95 	CLOCK_LOCK(&s);
96 	return (s);
97 }
98 
99 void
100 hr_clock_unlock(int s)
101 {
102 	CLOCK_UNLOCK(s);
103 }
104 
105 /*
106  * Support routines for horrid GMT lag handling
107  */
108 
109 static time_t gmt_lag;		/* offset in seconds of gmt to local time */
110 
111 void
112 sgmtl(time_t arg)
113 {
114 	gmt_lag = arg;
115 }
116 
117 time_t
118 ggmtl(void)
119 {
120 	return (gmt_lag);
121 }
122 
123 /* rtcsync() - set 'time', assuming RTC and GMT lag are correct */
124 
125 void
126 rtcsync(void)
127 {
128 	timestruc_t ts;
129 
130 	mutex_enter(&tod_lock);
131 	ts = TODOP_GET(tod_ops);
132 	set_hrestime(&ts);
133 	mutex_exit(&tod_lock);
134 }
135