xref: /illumos-gate/usr/src/uts/i86pc/io/hardclk.c (revision ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2ab)
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 2010 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 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/archsystm.h>
35 #include <sys/lockstat.h>
36 
37 #include <sys/clock.h>
38 #include <sys/debug.h>
39 #include <sys/smp_impldefs.h>
40 #include <sys/rtc.h>
41 
42 /*
43  * This file contains all generic part of clock and timer handling.
44  * Specifics are now in separate files and may be overridden by TOD
45  * modules.
46  */
47 
48 char *tod_module_name;		/* Settable in /etc/system */
49 
50 extern void tod_set_prev(timestruc_t);
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 	tod_set_prev(ts);		/* for tod_validate() */
62 	TODOP_SET(tod_ops, ts);
63 	tod_status_set(TOD_SET_DONE);	/* TOD was modified */
64 }
65 
66 /*
67  * Read the current time from the clock chip and convert to UNIX form.
68  * Assumes that the year in the clock chip is valid.
69  * Must be called with tod_lock held.
70  */
71 timestruc_t
72 tod_get(void)
73 {
74 	timestruc_t ts;
75 
76 	ASSERT(MUTEX_HELD(&tod_lock));
77 
78 	ts = TODOP_GET(tod_ops);
79 	ts.tv_sec = tod_validate(ts.tv_sec);
80 	return (ts);
81 }
82 
83 /*
84  * The following wrappers have been added so that locking
85  * can be exported to platform-independent clock routines
86  * (ie adjtime(), clock_setttime()), via a functional interface.
87  */
88 int
89 hr_clock_lock(void)
90 {
91 	ushort_t s;
92 
93 	CLOCK_LOCK(&s);
94 	return (s);
95 }
96 
97 void
98 hr_clock_unlock(int s)
99 {
100 	CLOCK_UNLOCK(s);
101 }
102 
103 /*
104  * Support routines for horrid GMT lag handling
105  */
106 
107 static time_t gmt_lag;		/* offset in seconds of gmt to local time */
108 
109 void
110 sgmtl(time_t arg)
111 {
112 	gmt_lag = arg;
113 }
114 
115 time_t
116 ggmtl(void)
117 {
118 	return (gmt_lag);
119 }
120 
121 /* rtcsync() - set 'time', assuming RTC and GMT lag are correct */
122 
123 void
124 rtcsync(void)
125 {
126 	timestruc_t ts;
127 
128 	mutex_enter(&tod_lock);
129 	ts = TODOP_GET(tod_ops);
130 	set_hrestime(&ts);
131 	mutex_exit(&tod_lock);
132 }
133