xref: /titanic_51/usr/src/uts/sun4u/io/hardclk.c (revision 8fc99e42676a23421c75e76660640f9765d693b1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8fc99e42STrevor Thompson  * Common Development and Distribution License (the "License").
6*8fc99e42STrevor Thompson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8fc99e42STrevor Thompson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/time.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/debug.h>
317c478bd9Sstevel@tonic-gate #include <sys/clock.h>
327c478bd9Sstevel@tonic-gate #include <sys/intreg.h>
337c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
357c478bd9Sstevel@tonic-gate #include <sys/promif.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
397c478bd9Sstevel@tonic-gate #include <vm/as.h>
407c478bd9Sstevel@tonic-gate #include <vm/hat.h>
417c478bd9Sstevel@tonic-gate #include <sys/intr.h>
427c478bd9Sstevel@tonic-gate #include <sys/ivintr.h>
437c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
457c478bd9Sstevel@tonic-gate #include <sys/membar.h>
467c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
477c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate uint_t sys_clock_mhz = 0;
507c478bd9Sstevel@tonic-gate uint64_t sys_tick_freq = 0;
517c478bd9Sstevel@tonic-gate uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
527c478bd9Sstevel@tonic-gate uint_t scaled_clock_mhz = 0;
537c478bd9Sstevel@tonic-gate uint_t nsec_per_sys_tick;
547c478bd9Sstevel@tonic-gate uint_t sticks_per_usec;
557c478bd9Sstevel@tonic-gate char clock_started = 0;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Hardware watchdog parameters and knobs
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate int watchdog_enable = 0;		/* user knob */
617c478bd9Sstevel@tonic-gate int watchdog_available = 0;		/* system has a watchdog */
627c478bd9Sstevel@tonic-gate int watchdog_activated = 0;		/* the watchdog is armed */
637c478bd9Sstevel@tonic-gate uint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * tod module name and operations
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate struct tod_ops	tod_ops;
697c478bd9Sstevel@tonic-gate char		*tod_module_name;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate void
737c478bd9Sstevel@tonic-gate clkstart(void)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int ret = 0;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	/*
787c478bd9Sstevel@tonic-gate 	 * Now is a good time to activate hardware watchdog (if one exists).
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
817c478bd9Sstevel@tonic-gate 	if (watchdog_enable)
827c478bd9Sstevel@tonic-gate 		ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
837c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
847c478bd9Sstevel@tonic-gate 	if (ret != 0)
857c478bd9Sstevel@tonic-gate 		printf("Hardware watchdog enabled\n");
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * preset the delay constant for drv_usecwait(). This is done for early
907c478bd9Sstevel@tonic-gate  * use of the le or scsi drivers in the kernel. The default contant
917c478bd9Sstevel@tonic-gate  * might be too high early on. We can get a pretty good approximation
927c478bd9Sstevel@tonic-gate  * of this by setting it as:
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * setcpudelay is called twice during the boot process. The first time
977c478bd9Sstevel@tonic-gate  * is before the TOD driver is loaded so cpu_init_tick_freq cannot
987c478bd9Sstevel@tonic-gate  * calibrate sys_tick_freq but can only set it to the prom value. The
997c478bd9Sstevel@tonic-gate  * first call is also before /etc/system is read.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * Only call cpu_init_tick_freq the second time around if sys_tick_freq
1027c478bd9Sstevel@tonic-gate  * has not been tuned via /etc/system.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate void
1057c478bd9Sstevel@tonic-gate setcpudelay(void)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	static uint64_t sys_tick_freq_save = 0;
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
1107c478bd9Sstevel@tonic-gate 	 * if it hasn't been explicitly tuned.
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	if (cpu_tick_freq != 0) {
1137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
1147c478bd9Sstevel@tonic-gate 		    "tunable, use sys_tick_freq instead");
1157c478bd9Sstevel@tonic-gate 		sys_tick_freq = cpu_tick_freq;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	if (sys_tick_freq == sys_tick_freq_save) {
1187c478bd9Sstevel@tonic-gate 		cpu_init_tick_freq();
1197c478bd9Sstevel@tonic-gate 		sys_tick_freq_save = sys_tick_freq;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	ASSERT(sys_tick_freq != 0);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * See the comments in clock.h for a full description of
1257c478bd9Sstevel@tonic-gate 	 * nsec_scale.  The "& ~1" operation below ensures that
1267c478bd9Sstevel@tonic-gate 	 * nsec_scale is always even, so that for *any* value of
1277c478bd9Sstevel@tonic-gate 	 * %tick, multiplying by nsec_scale clears NPT for free.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
1307c478bd9Sstevel@tonic-gate 	    sys_tick_freq) & ~1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
1347c478bd9Sstevel@tonic-gate 	 * version of sys_clock_mhz that we used to program the tick
1357c478bd9Sstevel@tonic-gate 	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
1367c478bd9Sstevel@tonic-gate 	 * instead of some whole number like 143
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	scaled_clock_mhz = (sys_tick_freq) / 1000;
1407c478bd9Sstevel@tonic-gate 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	nsec_per_sys_tick = NANOSEC / sys_tick_freq;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Pre-calculate number of sticks per usec for drv_usecwait.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (sys_clock_mhz <= 0) {
1507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid system frequency");
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate timestruc_t
1557c478bd9Sstevel@tonic-gate tod_get(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	timestruc_t ts = tod_ops.tod_get();
1587c478bd9Sstevel@tonic-gate 	ts.tv_sec = tod_validate(ts.tv_sec);
1597c478bd9Sstevel@tonic-gate 	return (ts);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
162*8fc99e42STrevor Thompson extern void tod_set_prev(timestruc_t);
163*8fc99e42STrevor Thompson 
1647c478bd9Sstevel@tonic-gate void
1657c478bd9Sstevel@tonic-gate tod_set(timestruc_t ts)
1667c478bd9Sstevel@tonic-gate {
167*8fc99e42STrevor Thompson 	tod_set_prev(ts);		/* for tod_validate() */
1687c478bd9Sstevel@tonic-gate 	tod_ops.tod_set(ts);
169*8fc99e42STrevor Thompson 	tod_status_set(TOD_SET_DONE);	/* TOD was modified */
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * The following wrappers have been added so that locking
1757c478bd9Sstevel@tonic-gate  * can be exported to platform-independent clock routines
1767c478bd9Sstevel@tonic-gate  * (ie adjtime(), clock_setttime()), via a functional interface.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate int
1797c478bd9Sstevel@tonic-gate hr_clock_lock(void)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	ushort_t s;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	CLOCK_LOCK(&s);
1847c478bd9Sstevel@tonic-gate 	return (s);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate void
1887c478bd9Sstevel@tonic-gate hr_clock_unlock(int s)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	CLOCK_UNLOCK(s);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * We don't share the trap table with the prom, so we don't need
1957c478bd9Sstevel@tonic-gate  * to enable/disable its clock.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate void
1987c478bd9Sstevel@tonic-gate mon_clock_init(void)
1997c478bd9Sstevel@tonic-gate {}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate void
2027c478bd9Sstevel@tonic-gate mon_clock_start(void)
2037c478bd9Sstevel@tonic-gate {}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate void
2067c478bd9Sstevel@tonic-gate mon_clock_stop(void)
2077c478bd9Sstevel@tonic-gate {}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate void
2107c478bd9Sstevel@tonic-gate mon_clock_share(void)
2117c478bd9Sstevel@tonic-gate {}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate void
2147c478bd9Sstevel@tonic-gate mon_clock_unshare(void)
2157c478bd9Sstevel@tonic-gate {}
216