xref: /onnv-gate/usr/src/uts/sun4u/io/hardclk.c (revision 11752:9c475fee0b48)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11752STrevor.Thompson@Sun.COM  * Common Development and Distribution License (the "License").
6*11752STrevor.Thompson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11752STrevor.Thompson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/time.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/clock.h>
320Sstevel@tonic-gate #include <sys/intreg.h>
330Sstevel@tonic-gate #include <sys/x_call.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/promif.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/lockstat.h>
390Sstevel@tonic-gate #include <vm/as.h>
400Sstevel@tonic-gate #include <vm/hat.h>
410Sstevel@tonic-gate #include <sys/intr.h>
420Sstevel@tonic-gate #include <sys/ivintr.h>
430Sstevel@tonic-gate #include <sys/machsystm.h>
440Sstevel@tonic-gate #include <sys/reboot.h>
450Sstevel@tonic-gate #include <sys/membar.h>
460Sstevel@tonic-gate #include <sys/atomic.h>
470Sstevel@tonic-gate #include <sys/cpu_module.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate uint_t sys_clock_mhz = 0;
500Sstevel@tonic-gate uint64_t sys_tick_freq = 0;
510Sstevel@tonic-gate uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
520Sstevel@tonic-gate uint_t scaled_clock_mhz = 0;
530Sstevel@tonic-gate uint_t nsec_per_sys_tick;
540Sstevel@tonic-gate uint_t sticks_per_usec;
550Sstevel@tonic-gate char clock_started = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Hardware watchdog parameters and knobs
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate int watchdog_enable = 0;		/* user knob */
610Sstevel@tonic-gate int watchdog_available = 0;		/* system has a watchdog */
620Sstevel@tonic-gate int watchdog_activated = 0;		/* the watchdog is armed */
630Sstevel@tonic-gate uint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * tod module name and operations
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate struct tod_ops	tod_ops;
690Sstevel@tonic-gate char		*tod_module_name;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate void
clkstart(void)730Sstevel@tonic-gate clkstart(void)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	int ret = 0;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * Now is a good time to activate hardware watchdog (if one exists).
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	mutex_enter(&tod_lock);
810Sstevel@tonic-gate 	if (watchdog_enable)
820Sstevel@tonic-gate 		ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
830Sstevel@tonic-gate 	mutex_exit(&tod_lock);
840Sstevel@tonic-gate 	if (ret != 0)
850Sstevel@tonic-gate 		printf("Hardware watchdog enabled\n");
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * preset the delay constant for drv_usecwait(). This is done for early
900Sstevel@tonic-gate  * use of the le or scsi drivers in the kernel. The default contant
910Sstevel@tonic-gate  * might be too high early on. We can get a pretty good approximation
920Sstevel@tonic-gate  * of this by setting it as:
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * setcpudelay is called twice during the boot process. The first time
970Sstevel@tonic-gate  * is before the TOD driver is loaded so cpu_init_tick_freq cannot
980Sstevel@tonic-gate  * calibrate sys_tick_freq but can only set it to the prom value. The
990Sstevel@tonic-gate  * first call is also before /etc/system is read.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Only call cpu_init_tick_freq the second time around if sys_tick_freq
1020Sstevel@tonic-gate  * has not been tuned via /etc/system.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate void
setcpudelay(void)1050Sstevel@tonic-gate setcpudelay(void)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	static uint64_t sys_tick_freq_save = 0;
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
1100Sstevel@tonic-gate 	 * if it hasn't been explicitly tuned.
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	if (cpu_tick_freq != 0) {
1130Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
1140Sstevel@tonic-gate 		    "tunable, use sys_tick_freq instead");
1150Sstevel@tonic-gate 		sys_tick_freq = cpu_tick_freq;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	if (sys_tick_freq == sys_tick_freq_save) {
1180Sstevel@tonic-gate 		cpu_init_tick_freq();
1190Sstevel@tonic-gate 		sys_tick_freq_save = sys_tick_freq;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	ASSERT(sys_tick_freq != 0);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * See the comments in clock.h for a full description of
1250Sstevel@tonic-gate 	 * nsec_scale.  The "& ~1" operation below ensures that
1260Sstevel@tonic-gate 	 * nsec_scale is always even, so that for *any* value of
1270Sstevel@tonic-gate 	 * %tick, multiplying by nsec_scale clears NPT for free.
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate 	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
1300Sstevel@tonic-gate 	    sys_tick_freq) & ~1;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
1340Sstevel@tonic-gate 	 * version of sys_clock_mhz that we used to program the tick
1350Sstevel@tonic-gate 	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
1360Sstevel@tonic-gate 	 * instead of some whole number like 143
1370Sstevel@tonic-gate 	 */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	scaled_clock_mhz = (sys_tick_freq) / 1000;
1400Sstevel@tonic-gate 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	nsec_per_sys_tick = NANOSEC / sys_tick_freq;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * Pre-calculate number of sticks per usec for drv_usecwait.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (sys_clock_mhz <= 0) {
1500Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid system frequency");
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate timestruc_t
tod_get(void)1550Sstevel@tonic-gate tod_get(void)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	timestruc_t ts = tod_ops.tod_get();
1580Sstevel@tonic-gate 	ts.tv_sec = tod_validate(ts.tv_sec);
1590Sstevel@tonic-gate 	return (ts);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
162*11752STrevor.Thompson@Sun.COM extern void tod_set_prev(timestruc_t);
163*11752STrevor.Thompson@Sun.COM 
1640Sstevel@tonic-gate void
tod_set(timestruc_t ts)1650Sstevel@tonic-gate tod_set(timestruc_t ts)
1660Sstevel@tonic-gate {
167*11752STrevor.Thompson@Sun.COM 	tod_set_prev(ts);		/* for tod_validate() */
1680Sstevel@tonic-gate 	tod_ops.tod_set(ts);
169*11752STrevor.Thompson@Sun.COM 	tod_status_set(TOD_SET_DONE);	/* TOD was modified */
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * The following wrappers have been added so that locking
1750Sstevel@tonic-gate  * can be exported to platform-independent clock routines
1760Sstevel@tonic-gate  * (ie adjtime(), clock_setttime()), via a functional interface.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate int
hr_clock_lock(void)1790Sstevel@tonic-gate hr_clock_lock(void)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	ushort_t s;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	CLOCK_LOCK(&s);
1840Sstevel@tonic-gate 	return (s);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate void
hr_clock_unlock(int s)1880Sstevel@tonic-gate hr_clock_unlock(int s)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	CLOCK_UNLOCK(s);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate  * We don't share the trap table with the prom, so we don't need
1950Sstevel@tonic-gate  * to enable/disable its clock.
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate void
mon_clock_init(void)1980Sstevel@tonic-gate mon_clock_init(void)
1990Sstevel@tonic-gate {}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate void
mon_clock_start(void)2020Sstevel@tonic-gate mon_clock_start(void)
2030Sstevel@tonic-gate {}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate void
mon_clock_stop(void)2060Sstevel@tonic-gate mon_clock_stop(void)
2070Sstevel@tonic-gate {}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate void
mon_clock_share(void)2100Sstevel@tonic-gate mon_clock_share(void)
2110Sstevel@tonic-gate {}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate void
mon_clock_unshare(void)2140Sstevel@tonic-gate mon_clock_unshare(void)
2150Sstevel@tonic-gate {}
216