49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe/*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * CDDL HEADER START
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * The contents of this file are subject to the terms of the
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Common Development and Distribution License (the "License").
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * You may not use this file except in compliance with the License.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * or http://www.opensolaris.org/os/licensing.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * See the License for the specific language governing permissions
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * and limitations under the License.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * CDDL HEADER END
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe/*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Use is subject to license terms.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/param.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/time.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/systm.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/cmn_err.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/debug.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/clock.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/intreg.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/x_call.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/cpuvar.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/promif.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/mman.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/sysmacros.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/lockstat.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <vm/as.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <vm/hat.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/intr.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/ivintr.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/machsystm.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/reboot.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/membar.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/atomic.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe#include <sys/cpu_module.h>
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t sys_clock_mhz = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint64_t sys_tick_freq = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t cpu_tick_freq = 0; /* deprecated, tune sys_tick_freq instead */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t scaled_clock_mhz = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t nsec_per_sys_tick;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t sticks_per_usec;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowechar clock_started = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe/*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Hardware watchdog parameters and knobs
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweint watchdog_enable = 0; /* user knob */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweint watchdog_available = 0; /* system has a watchdog */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweint watchdog_activated = 0; /* the watchdog is armed */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweuint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe/*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * tod module name and operations
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowestruct tod_ops tod_ops;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowechar *tod_module_name;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowevoid
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Loweclkstart(void)
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe{
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe int ret = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe /*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Now is a good time to activate hardware watchdog (if one exists).
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe mutex_enter(&tod_lock);
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe if (watchdog_enable)
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe mutex_exit(&tod_lock);
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe if (ret != 0)
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe printf("Hardware watchdog enabled\n");
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe}
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe/*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * preset the delay constant for drv_usecwait(). This is done for early
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * use of the le or scsi drivers in the kernel. The default contant
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * might be too high early on. We can get a pretty good approximation
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * of this by setting it as:
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * setcpudelay is called twice during the boot process. The first time
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * is before the TOD driver is loaded so cpu_init_tick_freq cannot
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * calibrate sys_tick_freq but can only set it to the prom value. The
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * first call is also before /etc/system is read.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe *
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * Only call cpu_init_tick_freq the second time around if sys_tick_freq
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * has not been tuned via /etc/system.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowevoid
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowesetcpudelay(void)
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe{
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe static uint64_t sys_tick_freq_save = 0;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe /*
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * We want to allow cpu_tick_freq to be tunable; we'll only set it
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe * if it hasn't been explicitly tuned.
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe */
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe if (cpu_tick_freq != 0) {
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe "tunable, use sys_tick_freq instead");
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe sys_tick_freq = cpu_tick_freq;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe }
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe if (sys_tick_freq == sys_tick_freq_save) {
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe cpu_init_tick_freq();
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe sys_tick_freq_save = sys_tick_freq;
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe }
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe ASSERT(sys_tick_freq != 0);
49d3bc91e27cd871b950d56c01398fa2f2e12ab4Richard Lowe
/*
* See the comments in clock.h for a full description of
* nsec_scale. The "& ~1" operation below ensures that
* nsec_scale is always even, so that for *any* value of
* %tick, multiplying by nsec_scale clears NPT for free.
*/
nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
sys_tick_freq) & ~1;
/*
* scaled_clock_mhz is a more accurated (ie not rounded-off)
* version of sys_clock_mhz that we used to program the tick
* compare register. Just in case sys_tick_freq is like 142.5 Mhz
* instead of some whole number like 143
*/
scaled_clock_mhz = (sys_tick_freq) / 1000;
sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
nsec_per_sys_tick = NANOSEC / sys_tick_freq;
/*
* Pre-calculate number of sticks per usec for drv_usecwait.
*/
sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
if (sys_clock_mhz <= 0) {
cmn_err(CE_WARN, "invalid system frequency");
}
}
timestruc_t
tod_get(void)
{
timestruc_t ts = tod_ops.tod_get();
ts.tv_sec = tod_validate(ts.tv_sec);
return (ts);
}
extern void tod_set_prev(timestruc_t);
void
tod_set(timestruc_t ts)
{
tod_set_prev(ts); /* for tod_validate() */
tod_ops.tod_set(ts);
tod_status_set(TOD_SET_DONE); /* TOD was modified */
}
/*
* The following wrappers have been added so that locking
* can be exported to platform-independent clock routines
* (ie adjtime(), clock_setttime()), via a functional interface.
*/
int
hr_clock_lock(void)
{
ushort_t s;
CLOCK_LOCK(&s);
return (s);
}
void
hr_clock_unlock(int s)
{
CLOCK_UNLOCK(s);
}
/*
* We don't share the trap table with the prom, so we don't need
* to enable/disable its clock.
*/
void
mon_clock_init(void)
{}
void
mon_clock_start(void)
{}
void
mon_clock_stop(void)
{}
void
mon_clock_share(void)
{}
void
mon_clock_unshare(void)
{}