xref: /illumos-gate/usr/src/uts/sun4u/io/todopl.c (revision bbf21555)
125cf1a30Sjl /*
225cf1a30Sjl  * CDDL HEADER START
325cf1a30Sjl  *
425cf1a30Sjl  * The contents of this file are subject to the terms of the
525cf1a30Sjl  * Common Development and Distribution License (the "License").
625cf1a30Sjl  * You may not use this file except in compliance with the License.
725cf1a30Sjl  *
825cf1a30Sjl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925cf1a30Sjl  * or http://www.opensolaris.org/os/licensing.
1025cf1a30Sjl  * See the License for the specific language governing permissions
1125cf1a30Sjl  * and limitations under the License.
1225cf1a30Sjl  *
1325cf1a30Sjl  * When distributing Covered Code, include this CDDL HEADER in each
1425cf1a30Sjl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525cf1a30Sjl  * If applicable, add the following below this CDDL HEADER, with the
1625cf1a30Sjl  * fields enclosed by brackets "[]" replaced with your own identifying
1725cf1a30Sjl  * information: Portions Copyright [yyyy] [name of copyright owner]
1825cf1a30Sjl  *
1925cf1a30Sjl  * CDDL HEADER END
2025cf1a30Sjl  */
2125cf1a30Sjl /*
2225cf1a30Sjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2325cf1a30Sjl  * Use is subject to license terms.
2425cf1a30Sjl  */
2525cf1a30Sjl 
2625cf1a30Sjl /*
2725cf1a30Sjl  * tod driver module for OPL (implements a soft tod)
2825cf1a30Sjl  */
2925cf1a30Sjl 
3025cf1a30Sjl #include <sys/types.h>
3125cf1a30Sjl #include <sys/param.h>
3225cf1a30Sjl #include <sys/sysmacros.h>
3325cf1a30Sjl #include <sys/systm.h>
3425cf1a30Sjl #include <sys/errno.h>
3525cf1a30Sjl #include <sys/modctl.h>
3625cf1a30Sjl #include <sys/autoconf.h>
3725cf1a30Sjl #include <sys/debug.h>
3825cf1a30Sjl #include <sys/clock.h>
3925cf1a30Sjl #include <sys/cmn_err.h>
4025cf1a30Sjl #include <sys/prom_plat.h>
4125cf1a30Sjl #include <sys/cpuvar.h>
4225cf1a30Sjl #include <sys/opl_module.h>
4325cf1a30Sjl 
4425cf1a30Sjl /*
4525cf1a30Sjl  * Debug stuff
4625cf1a30Sjl  */
4725cf1a30Sjl #ifdef DEBUG
4825cf1a30Sjl int todopl_debug = 0;
4925cf1a30Sjl #define	TODOPL_DEBUG(args)  if (todopl_debug) cmn_err args
5025cf1a30Sjl #else
5125cf1a30Sjl #define	TODOPL_DEBUG(args)
5225cf1a30Sjl #endif
5325cf1a30Sjl 
5425cf1a30Sjl #define	abs(x)	((x) < 0 ? -(x) : (x))
5525cf1a30Sjl 
5625cf1a30Sjl #define	TODOPL_SET_THRESHOLD	30
5725cf1a30Sjl 
5825cf1a30Sjl static timestruc_t	todopl_get(void);
5925cf1a30Sjl static void		todopl_set(timestruc_t);
6025cf1a30Sjl static uint_t		todopl_set_watchdog_timer(uint_t);
6125cf1a30Sjl static uint_t		todopl_clear_watchdog_timer(void);
6225cf1a30Sjl static void		todopl_set_power_alarm(timestruc_t);
6325cf1a30Sjl static void		todopl_clear_power_alarm(void);
6425cf1a30Sjl static uint64_t		todopl_get_cpufrequency(void);
6525cf1a30Sjl 
6625cf1a30Sjl /*
6725cf1a30Sjl  * Module linkage information for the kernel.
6825cf1a30Sjl  */
6925cf1a30Sjl static struct modlmisc modlmisc = {
7025cf1a30Sjl 	&mod_miscops, "Soft tod module for OPL 1.11"
7125cf1a30Sjl };
7225cf1a30Sjl 
7325cf1a30Sjl static struct modlinkage modlinkage = {
7425cf1a30Sjl 	MODREV_1, (void *)&modlmisc, NULL
7525cf1a30Sjl };
7625cf1a30Sjl 
7725cf1a30Sjl /*
7825cf1a30Sjl  * The TOD OPL logic description.
7925cf1a30Sjl  *
8025cf1a30Sjl  * The todopl driver uses promif functions prom_opl_get_tod() and
8125cf1a30Sjl  * prom_opl_set_diff(). These functions call FJSV,get-tod and
8225cf1a30Sjl  * FJSV,set-domain-time OBP client services.
8325cf1a30Sjl  *
8425cf1a30Sjl  * At the system boot or reboot:
8525cf1a30Sjl  *
8625cf1a30Sjl  *    FJSV,tod-get
8725cf1a30Sjl  * OS  --------->   OBP     SCF I/F
8825cf1a30Sjl  *                         ----------->  XSCF
8925cf1a30Sjl  *                         <-----------
9025cf1a30Sjl  *     <--------            time, diff
9125cf1a30Sjl  *    time+diff, stick
9225cf1a30Sjl  *
9325cf1a30Sjl  * Note that on first powerup domain boot, diff is zero.
9425cf1a30Sjl  *
95*bbf21555SRichard Lowe  * When system updates the time via date(1):
9625cf1a30Sjl  *
9725cf1a30Sjl  *   FJSV,set-domain-time
9825cf1a30Sjl  * OS   --------->   OBP                      SRAM
9925cf1a30Sjl  *      diff_delta        diff += diff_delta ------------->  XSCF
10025cf1a30Sjl  *
10125cf1a30Sjl  * diff_delta = new time -  current domain time (hrestime)
10225cf1a30Sjl  *
10325cf1a30Sjl  *
10425cf1a30Sjl  * In theory, FJSV,get-tod and FJSV,set-domain-time should never fails.
10525cf1a30Sjl  * But, if call to FJSV,get-tod fails on boot, the domain will be unable
10625cf1a30Sjl  * to calculate "diff" properly and synchronization between Domain and
10725cf1a30Sjl  * SP will be broken. In this particular case, we notify users that
10825cf1a30Sjl  * "there is no time synchronization" and the logic will attempt to
10925cf1a30Sjl  * resync with the SP whenever the OS tries to do a TOD update.
110*bbf21555SRichard Lowe  * (e.g. via date(1) or NTP).
11125cf1a30Sjl  */
11225cf1a30Sjl 
11325cf1a30Sjl static	int enable_time_sync = 1;
11425cf1a30Sjl 
11525cf1a30Sjl int
_init(void)11625cf1a30Sjl _init(void)
11725cf1a30Sjl {
11825cf1a30Sjl 	int64_t	stick;
11925cf1a30Sjl 	time_t	obp_time = 0;
12025cf1a30Sjl 	int64_t obp_stick;
12125cf1a30Sjl 
12225cf1a30Sjl 	if (strcmp(tod_module_name, "todopl") == 0) {
12325cf1a30Sjl 		/*
12425cf1a30Sjl 		 * Get TOD time from OBP and adjust it.
12525cf1a30Sjl 		 */
12625cf1a30Sjl 		prom_opl_get_tod(&obp_time, &obp_stick);
12725cf1a30Sjl 
12825cf1a30Sjl 		TODOPL_DEBUG((CE_NOTE, "todopl: OBP time 0x%lx stick 0x%lx\n",
12925cf1a30Sjl 			obp_time, obp_stick));
13025cf1a30Sjl 
13125cf1a30Sjl 		if (obp_time != 0) {
13225cf1a30Sjl 			/*
13325cf1a30Sjl 			 * adjust OBP time by stick counts
13425cf1a30Sjl 			 */
13525cf1a30Sjl 			stick_timestamp(&stick);
13625cf1a30Sjl 			obp_time += ((stick - obp_stick) / system_clock_freq);
13725cf1a30Sjl 
13825cf1a30Sjl 			TODOPL_DEBUG((CE_NOTE,
13925cf1a30Sjl 				"todopl: cpu stick 0x%lx sys_time 0x%lx\n",
14025cf1a30Sjl 				stick, obp_time));
14125cf1a30Sjl 		} else {
14225cf1a30Sjl 			/*
14325cf1a30Sjl 			 * A date of zero causes the root filesystem driver
14425cf1a30Sjl 			 * to try to set the date from the last shutdown.
14525cf1a30Sjl 			 */
14625cf1a30Sjl 			enable_time_sync = 0;
14725cf1a30Sjl 			cmn_err(CE_WARN, "Initial date is invalid.");
14825cf1a30Sjl 			cmn_err(CE_CONT, "Attempting to set the date and time "
14925cf1a30Sjl 				"based on the last shutdown.\n");
15025cf1a30Sjl 			cmn_err(CE_CONT, "The time could not be synchronized "
15125cf1a30Sjl 				"between Domain and Service Processor.\n");
15225cf1a30Sjl 			cmn_err(CE_CONT, "Please inspect the date and time and "
15325cf1a30Sjl 				"correct if necessary.\n");
15425cf1a30Sjl 		}
15525cf1a30Sjl 
15625cf1a30Sjl 		hrestime.tv_sec = obp_time;
15725cf1a30Sjl 
15825cf1a30Sjl 		/*
15925cf1a30Sjl 		 * Check that the date has not overflowed a 32-bit integer.
16025cf1a30Sjl 		 */
16125cf1a30Sjl 		if (TIMESPEC_OVERFLOW(&hrestime)) {
16225cf1a30Sjl 			cmn_err(CE_WARN, "Date overflow detected.");
16325cf1a30Sjl 			cmn_err(CE_CONT, "Attempting to set the date and time "
16425cf1a30Sjl 				"based on the last shutdown.\n");
16525cf1a30Sjl 			cmn_err(CE_CONT, "Please inspect the date and time and "
16625cf1a30Sjl 				"correct if necessary.\n");
16725cf1a30Sjl 
16825cf1a30Sjl 			hrestime.tv_sec = (time_t)0;
16925cf1a30Sjl 		}
17025cf1a30Sjl 
17125cf1a30Sjl 		tod_ops.tod_get = todopl_get;
17225cf1a30Sjl 		tod_ops.tod_set = todopl_set;
17325cf1a30Sjl 		tod_ops.tod_set_watchdog_timer = todopl_set_watchdog_timer;
17425cf1a30Sjl 		tod_ops.tod_clear_watchdog_timer = todopl_clear_watchdog_timer;
17525cf1a30Sjl 		tod_ops.tod_set_power_alarm = todopl_set_power_alarm;
17625cf1a30Sjl 		tod_ops.tod_clear_power_alarm = todopl_clear_power_alarm;
17725cf1a30Sjl 		tod_ops.tod_get_cpufrequency = todopl_get_cpufrequency;
17825cf1a30Sjl 
17925cf1a30Sjl 		/*
18025cf1a30Sjl 		 * Flag warning if user tried to use hardware watchdog
18125cf1a30Sjl 		 */
18225cf1a30Sjl 		if (watchdog_enable) {
18325cf1a30Sjl 			cmn_err(CE_WARN, "Hardware watchdog unavailable");
18425cf1a30Sjl 		}
18525cf1a30Sjl 	}
18625cf1a30Sjl 
18725cf1a30Sjl 	return (mod_install(&modlinkage));
18825cf1a30Sjl }
18925cf1a30Sjl 
19025cf1a30Sjl int
_fini(void)19125cf1a30Sjl _fini(void)
19225cf1a30Sjl {
19325cf1a30Sjl 	if (strcmp(tod_module_name, "todopl") == 0)
19425cf1a30Sjl 		return (EBUSY);
19525cf1a30Sjl 	else
19625cf1a30Sjl 		return (mod_remove(&modlinkage));
19725cf1a30Sjl }
19825cf1a30Sjl 
19925cf1a30Sjl int
_info(struct modinfo * modinfop)20025cf1a30Sjl _info(struct modinfo *modinfop)
20125cf1a30Sjl {
20225cf1a30Sjl 	return (mod_info(&modlinkage, modinfop));
20325cf1a30Sjl }
20425cf1a30Sjl 
20525cf1a30Sjl 
20625cf1a30Sjl /*
20725cf1a30Sjl  * OPL tod_get is simplified to return hrestime
20825cf1a30Sjl  * Must be called with tod_lock held.
20925cf1a30Sjl  */
21025cf1a30Sjl static timestruc_t
todopl_get(void)21125cf1a30Sjl todopl_get(void)
21225cf1a30Sjl {
21325cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
21425cf1a30Sjl 	return (hrestime);
21525cf1a30Sjl }
21625cf1a30Sjl 
21725cf1a30Sjl /*
21825cf1a30Sjl  * Must be called with tod_lock held.
21925cf1a30Sjl  *
22025cf1a30Sjl  * When running NTP, tod_set is called at least once per second in order
22125cf1a30Sjl  * to update the hardware clock. To minimize pressure on SP, we want only
222*bbf21555SRichard Lowe  * to record significant time changes on the SP (when date(1) is run).
22325cf1a30Sjl  * We have 30 seconds threshold requirement before recording the time change.
22425cf1a30Sjl  */
22525cf1a30Sjl /* ARGSUSED */
22625cf1a30Sjl static void
todopl_set(timestruc_t ts)22725cf1a30Sjl todopl_set(timestruc_t ts)
22825cf1a30Sjl {
22925cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
23025cf1a30Sjl 
23125cf1a30Sjl 	if (abs(ts.tv_sec - hrestime.tv_sec) > TODOPL_SET_THRESHOLD) {
23225cf1a30Sjl 		/*
23325cf1a30Sjl 		 * Send time difference to SP
23425cf1a30Sjl 		 */
23525cf1a30Sjl 		if (enable_time_sync)
23625cf1a30Sjl 			prom_opl_set_diff(ts.tv_sec - hrestime.tv_sec);
23725cf1a30Sjl 		else {
23825cf1a30Sjl 			/*
23925cf1a30Sjl 			 * We did not get a successful initial time
24025cf1a30Sjl 			 * update/sync from the SP via OBP during boot.
24125cf1a30Sjl 			 * Try again here.
24225cf1a30Sjl 			 */
24325cf1a30Sjl 			time_t  obp_time = 0;
24425cf1a30Sjl 			int64_t obp_stick;
24525cf1a30Sjl 			int64_t stick;
24625cf1a30Sjl 
24725cf1a30Sjl 			prom_opl_get_tod(&obp_time, &obp_stick);
24825cf1a30Sjl 
24925cf1a30Sjl 			if (obp_time != 0) {
25025cf1a30Sjl 				/*
25125cf1a30Sjl 				 * adjust OBP time by stick counts
25225cf1a30Sjl 				 */
25325cf1a30Sjl 				stick_timestamp(&stick);
25425cf1a30Sjl 				obp_time += ((stick - obp_stick) /
25525cf1a30Sjl 					system_clock_freq);
25625cf1a30Sjl 
25725cf1a30Sjl 				/*
25825cf1a30Sjl 				 * Sync up by computing the diff using the
25925cf1a30Sjl 				 * newly acquired SP/OBP reference time
26025cf1a30Sjl 				 */
26125cf1a30Sjl 				prom_opl_set_diff(ts.tv_sec - obp_time);
26225cf1a30Sjl 
26325cf1a30Sjl 				enable_time_sync = 1;
26425cf1a30Sjl 			}
26525cf1a30Sjl 		}
26625cf1a30Sjl 		TODOPL_DEBUG((CE_NOTE, "todopl_set: new domain time 0x%lx\n",
26725cf1a30Sjl 			ts.tv_sec));
26825cf1a30Sjl 	}
26925cf1a30Sjl }
27025cf1a30Sjl 
27125cf1a30Sjl /*
27225cf1a30Sjl  * No watchdog function.
27325cf1a30Sjl  */
27425cf1a30Sjl /* ARGSUSED */
27525cf1a30Sjl static uint_t
todopl_set_watchdog_timer(uint_t timeoutval)27625cf1a30Sjl todopl_set_watchdog_timer(uint_t timeoutval)
27725cf1a30Sjl {
27825cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
27925cf1a30Sjl 	return (0);
28025cf1a30Sjl }
28125cf1a30Sjl 
28225cf1a30Sjl /*
28325cf1a30Sjl  * No watchdog function
28425cf1a30Sjl  */
28525cf1a30Sjl static uint_t
todopl_clear_watchdog_timer(void)28625cf1a30Sjl todopl_clear_watchdog_timer(void)
28725cf1a30Sjl {
28825cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
28925cf1a30Sjl 	return (0);
29025cf1a30Sjl }
29125cf1a30Sjl 
29225cf1a30Sjl /*
29325cf1a30Sjl  * Null function.
29425cf1a30Sjl  */
29525cf1a30Sjl /* ARGSUSED */
29625cf1a30Sjl static void
todopl_set_power_alarm(timestruc_t ts)29725cf1a30Sjl todopl_set_power_alarm(timestruc_t ts)
29825cf1a30Sjl {
29925cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
30025cf1a30Sjl }
30125cf1a30Sjl 
30225cf1a30Sjl /*
30325cf1a30Sjl  * Null function
30425cf1a30Sjl  */
30525cf1a30Sjl static void
todopl_clear_power_alarm()30625cf1a30Sjl todopl_clear_power_alarm()
30725cf1a30Sjl {
30825cf1a30Sjl 	ASSERT(MUTEX_HELD(&tod_lock));
30925cf1a30Sjl }
31025cf1a30Sjl 
31125cf1a30Sjl /*
31225cf1a30Sjl  * Get clock freq from the cpunode.  This function is only called
31325cf1a30Sjl  * when use_stick = 0, otherwise, system_clock_freq gets used instead.
31425cf1a30Sjl  */
31525cf1a30Sjl uint64_t
todopl_get_cpufrequency(void)31625cf1a30Sjl todopl_get_cpufrequency(void)
31725cf1a30Sjl {
31825cf1a30Sjl 	return (cpunodes[CPU->cpu_id].clock_freq);
31925cf1a30Sjl }
320