cpudrv.c revision 173531301317dd4f31e83d4785873141e984ab86
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER START
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The contents of this file are subject to the terms of the
5cff782560a1c3cf913ba5574a5123a299f3315emh * Common Development and Distribution License (the "License").
5cff782560a1c3cf913ba5574a5123a299f3315emh * You may not use this file except in compliance with the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5cff782560a1c3cf913ba5574a5123a299f3315emh * or http://www.opensolaris.org/os/licensing.
5cff782560a1c3cf913ba5574a5123a299f3315emh * See the License for the specific language governing permissions
5cff782560a1c3cf913ba5574a5123a299f3315emh * and limitations under the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * When distributing Covered Code, include this CDDL HEADER in each
5cff782560a1c3cf913ba5574a5123a299f3315emh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5cff782560a1c3cf913ba5574a5123a299f3315emh * If applicable, add the following below this CDDL HEADER, with the
5cff782560a1c3cf913ba5574a5123a299f3315emh * fields enclosed by brackets "[]" replaced with your own identifying
5cff782560a1c3cf913ba5574a5123a299f3315emh * information: Portions Copyright [yyyy] [name of copyright owner]
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER END
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Use is subject to license terms.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * CPU Device driver. The driver is not DDI-compliant.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The driver supports following features:
5cff782560a1c3cf913ba5574a5123a299f3315emh * - Power management.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/types.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/param.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/errno.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/modctl.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/kmem.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/conf.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/cmn_err.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/stat.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/debug.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/systm.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/ddi.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/sunddi.h>
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe#include <sys/sdt.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/machsystm.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/x_call.h>
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood#include <sys/cpudrv_mach.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/msacct.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * CPU power management
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The supported power saving model is to slow down the CPU (on SPARC by
5cff782560a1c3cf913ba5574a5123a299f3315emh * dividing the CPU clock and on x86 by dropping down a P-state).
5cff782560a1c3cf913ba5574a5123a299f3315emh * Periodically we determine the amount of time the CPU is running
5cff782560a1c3cf913ba5574a5123a299f3315emh * idle thread and threads in user mode during the last quantum. If the idle
5cff782560a1c3cf913ba5574a5123a299f3315emh * thread was running less than its low water mark for current speed for
5cff782560a1c3cf913ba5574a5123a299f3315emh * number of consecutive sampling periods, or number of running threads in
5cff782560a1c3cf913ba5574a5123a299f3315emh * user mode are above its high water mark, we arrange to go to the higher
5cff782560a1c3cf913ba5574a5123a299f3315emh * speed. If the idle thread was running more than its high water mark without
5cff782560a1c3cf913ba5574a5123a299f3315emh * dropping a number of consecutive times below the mark, and number of threads
5cff782560a1c3cf913ba5574a5123a299f3315emh * running in user mode are below its low water mark, we arrange to go to the
5cff782560a1c3cf913ba5574a5123a299f3315emh * next lower speed. While going down, we go through all the speeds. While
5cff782560a1c3cf913ba5574a5123a299f3315emh * going up we go to the maximum speed to minimize impact on the user, but have
5cff782560a1c3cf913ba5574a5123a299f3315emh * provisions in the driver to go to other speeds.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The driver does not have knowledge of a particular implementation of this
5cff782560a1c3cf913ba5574a5123a299f3315emh * scheme and will work with all CPUs supporting this model. On SPARC, the
5cff782560a1c3cf913ba5574a5123a299f3315emh * driver determines supported speeds by looking at 'clock-divisors' property
5cff782560a1c3cf913ba5574a5123a299f3315emh * created by OBP. On x86, the driver retrieves the supported speeds from
5cff782560a1c3cf913ba5574a5123a299f3315emh * ACPI.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Configuration function prototypes and data structures
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int cpudrv_power(dev_info_t *dip, int comp, int level);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhstruct dev_ops cpudrv_ops = {
5cff782560a1c3cf913ba5574a5123a299f3315emh DEVO_REV, /* rev */
5cff782560a1c3cf913ba5574a5123a299f3315emh 0, /* refcnt */
5cff782560a1c3cf913ba5574a5123a299f3315emh nodev, /* getinfo */
5cff782560a1c3cf913ba5574a5123a299f3315emh nulldev, /* identify */
5cff782560a1c3cf913ba5574a5123a299f3315emh nulldev, /* probe */
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_attach, /* attach */
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_detach, /* detach */
5cff782560a1c3cf913ba5574a5123a299f3315emh nodev, /* reset */
5cff782560a1c3cf913ba5574a5123a299f3315emh (struct cb_ops *)NULL, /* cb_ops */
5cff782560a1c3cf913ba5574a5123a299f3315emh (struct bus_ops *)NULL, /* bus_ops */
193974072f41a843678abf5f61979c748687e66bSherry Moore cpudrv_power, /* power */
193974072f41a843678abf5f61979c748687e66bSherry Moore ddi_quiesce_not_needed, /* quiesce */
5cff782560a1c3cf913ba5574a5123a299f3315emh};
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic struct modldrv modldrv = {
5cff782560a1c3cf913ba5574a5123a299f3315emh &mod_driverops, /* modops */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood "CPU Driver", /* linkinfo */
5cff782560a1c3cf913ba5574a5123a299f3315emh &cpudrv_ops, /* dev_ops */
5cff782560a1c3cf913ba5574a5123a299f3315emh};
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic struct modlinkage modlinkage = {
5cff782560a1c3cf913ba5574a5123a299f3315emh MODREV_1, /* rev */
5cff782560a1c3cf913ba5574a5123a299f3315emh &modldrv, /* linkage */
5cff782560a1c3cf913ba5574a5123a299f3315emh NULL
5cff782560a1c3cf913ba5574a5123a299f3315emh};
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Function prototypes
5cff782560a1c3cf913ba5574a5123a299f3315emh */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodstatic int cpudrv_pm_init_power(cpudrv_devstate_t *cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void cpudrv_pm_free(cpudrv_devstate_t *cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void cpudrv_pm_monitor_disp(void *arg);
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void cpudrv_pm_monitor(void *arg);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver global variables
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhuint_t cpudrv_debug = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emhvoid *cpudrv_state;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_idle_hwm = CPUDRV_PM_IDLE_HWM;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_idle_lwm = CPUDRV_PM_IDLE_LWM;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_idle_buf_zone = CPUDRV_PM_IDLE_BUF_ZONE;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_idle_bhwm_cnt_max = CPUDRV_PM_IDLE_BHWM_CNT_MAX;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_idle_blwm_cnt_max = CPUDRV_PM_IDLE_BLWM_CNT_MAX;
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic uint_t cpudrv_pm_user_hwm = CPUDRV_PM_USER_HWM;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * cpudrv_direct_pm allows user applications to directly control the
5cff782560a1c3cf913ba5574a5123a299f3315emh * power state transitions (direct pm) without following the normal
5cff782560a1c3cf913ba5574a5123a299f3315emh * direct pm protocol. This is needed because the normal protocol
5cff782560a1c3cf913ba5574a5123a299f3315emh * requires that a device only be lowered when it is idle, and be
5cff782560a1c3cf913ba5574a5123a299f3315emh * brought up when it request to do so by calling pm_raise_power().
5cff782560a1c3cf913ba5574a5123a299f3315emh * Ignoring this protocol is harmless for CPU (other than speed).
5cff782560a1c3cf913ba5574a5123a299f3315emh * Moreover it might be the case that CPU is never idle or wants
5cff782560a1c3cf913ba5574a5123a299f3315emh * to be at higher speed because of the addition CPU cycles required
5cff782560a1c3cf913ba5574a5123a299f3315emh * to run the user application.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The driver will still report idle/busy status to the framework. Although
5cff782560a1c3cf913ba5574a5123a299f3315emh * framework will ignore this information for direct pm devices and not
5cff782560a1c3cf913ba5574a5123a299f3315emh * try to bring them down when idle, user applications can still use this
5cff782560a1c3cf913ba5574a5123a299f3315emh * information if they wants.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * In the future, provide an ioctl to control setting of this mode. In
5cff782560a1c3cf913ba5574a5123a299f3315emh * that case, this variable should move to the state structure and
5cff782560a1c3cf913ba5574a5123a299f3315emh * be protected by the lock in the state structure.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint cpudrv_direct_pm = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Arranges for the handler function to be called at the interval suitable
5cff782560a1c3cf913ba5574a5123a299f3315emh * for current speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh#define CPUDRV_PM_MONITOR_INIT(cpudsp) { \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (CPUDRV_PM_POWER_ENABLED(cpudsp)) { \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT(mutex_owned(&(cpudsp)->lock)); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (cpudsp)->cpudrv_pm.timeout_id = \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood timeout(cpudrv_pm_monitor_disp, \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_QUANT_CNT_OTHR : \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood } \
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Arranges for the handler function not to be called back.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh#define CPUDRV_PM_MONITOR_FINI(cpudsp) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh timeout_id_t tmp_tid; \
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(mutex_owned(&(cpudsp)->lock)); \
5cff782560a1c3cf913ba5574a5123a299f3315emh tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpudsp)->cpudrv_pm.timeout_id = 0; \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&(cpudsp)->lock); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (tmp_tid != 0) { \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (void) untimeout(tmp_tid); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood &(cpudsp)->cpudrv_pm.timeout_lock); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood } \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&(cpudsp)->lock); \
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emh_init(void)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_INIT, (" _init: function called\n"));
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((error = ddi_soft_state_init(&cpudrv_state,
5cff782560a1c3cf913ba5574a5123a299f3315emh sizeof (cpudrv_devstate_t), 0)) != 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((error = mod_install(&modlinkage)) != 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_soft_state_fini(&cpudrv_state);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Callbacks used by the PPM driver.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_SET_PPM_CALLBACKS();
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emh_fini(void)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_FINI, (" _fini: function called\n"));
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((error = mod_remove(&modlinkage)) == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_soft_state_fini(&cpudrv_state);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emh_info(struct modinfo *modinfop)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh return (mod_info(&modlinkage, modinfop));
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver attach(9e) entry point.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int instance;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_devstate_t *cpudsp;
5cff782560a1c3cf913ba5574a5123a299f3315emh extern pri_t maxclsyspri;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh instance = ddi_get_instance(dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh switch (cmd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh case DDI_ATTACH:
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "DDI_ATTACH called\n", instance));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (CPUDRV_PM_DISABLED())
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't allocate state", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_DISABLE();
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
5cff782560a1c3cf913ba5574a5123a299f3315emh NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't get state", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_soft_state_free(cpudrv_state, instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_DISABLE();
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudsp->dip = dip;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Find CPU number for this dev_info node.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!cpudrv_pm_get_cpu_id(dip, &(cpudsp->cpu_id))) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't convert dip to cpu_id", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_soft_state_free(cpudrv_state, instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_DISABLE();
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (!cpudrv_mach_pm_init(cpudsp)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_soft_state_free(cpudrv_state, instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_DISABLE();
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (CPUDRV_PM_POWER_ENABLED(cpudsp)) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (cpudrv_pm_init_power(cpudsp) != DDI_SUCCESS) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_DISABLE();
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudrv_pm_free(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ddi_soft_state_free(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_FAILURE);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (cpudrv_pm_comp_create(cpudsp) != DDI_SUCCESS) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_DISABLE();
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudrv_pm_free(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ddi_soft_state_free(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_FAILURE);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (ddi_prop_update_string(DDI_DEV_T_NONE,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_DISABLE();
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudrv_pm_free(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ddi_soft_state_free(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_FAILURE);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Taskq is used to dispatch routine to monitor CPU
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * activities.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudsp->cpudrv_pm.tq = taskq_create_instance(
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood "cpudrv_pm_monitor",
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ddi_get_instance(dip), CPUDRV_PM_TASKQ_THREADS,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (maxclsyspri - 1), CPUDRV_PM_TASKQ_MIN,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_TASKQ_MAX,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood TASKQ_PREPOPULATE|TASKQ_CPR_SAFE);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood MUTEX_DRIVER, NULL);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CV_DEFAULT, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Driver needs to assume that CPU is running at
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * unknown speed at DDI_ATTACH and switch it to the
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * needed speed. We assume that initial needed speed
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * is full speed for us.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * We need to take the lock because cpudrv_pm_monitor()
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * will start running in parallel with attach().
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_enter(&cpudsp->lock);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudsp->cpudrv_pm.cur_spd = NULL;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudsp->cpudrv_pm.pm_started = B_FALSE;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * We don't call pm_raise_power() directly from attach
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * because driver attach for a slave CPU node can
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * happen before the CPU is even initialized. We just
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * start the monitoring system which understands
173531301317dd4f31e83d4785873141e984ab86Mark Haywood * unknown speed and moves CPU to top speed when it
173531301317dd4f31e83d4785873141e984ab86Mark Haywood * has been initialized.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_MONITOR_INIT(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_INSTALL_MAX_CHANGE_HANDLER(cpudsp, dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_report_dev(dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh case DDI_RESUME:
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "DDI_RESUME called\n", instance));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudsp = ddi_get_soft_state(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT(cpudsp != NULL);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Nothing to do for resume, if not doing active PM.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (!CPUDRV_PM_POWER_ENABLED(cpudsp))
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_SUCCESS);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver needs to assume that CPU is running at unknown speed
5cff782560a1c3cf913ba5574a5123a299f3315emh * at DDI_RESUME and switch it to the needed speed. We assume
5cff782560a1c3cf913ba5574a5123a299f3315emh * that the needed speed is full speed for us.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudsp->cpudrv_pm.cur_spd = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_REDEFINE_TOPSPEED(dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh default:
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver detach(9e) entry point.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int instance;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_devstate_t *cpudsp;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh instance = ddi_get_instance(dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh switch (cmd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh case DDI_DETACH:
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "DDI_DETACH called\n", instance));
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * If the only thing supported by the driver is power
5cff782560a1c3cf913ba5574a5123a299f3315emh * management, we can in future enhance the driver and
5cff782560a1c3cf913ba5574a5123a299f3315emh * framework that loads it to unload the driver when
5cff782560a1c3cf913ba5574a5123a299f3315emh * user has disabled CPU power management.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh case DDI_SUSPEND:
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "DDI_SUSPEND called\n", instance));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudsp = ddi_get_soft_state(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT(cpudsp != NULL);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Nothing to do for suspend, if not doing active PM.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (!CPUDRV_PM_POWER_ENABLED(cpudsp))
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_SUCCESS);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * During a checkpoint-resume sequence, framework will
5cff782560a1c3cf913ba5574a5123a299f3315emh * stop interrupts to quiesce kernel activity. This will
5cff782560a1c3cf913ba5574a5123a299f3315emh * leave our monitoring system ineffective. Handle this
5cff782560a1c3cf913ba5574a5123a299f3315emh * by stopping our monitoring system and bringing CPU
5cff782560a1c3cf913ba5574a5123a299f3315emh * to full speed. In case we are in special direct pm
5cff782560a1c3cf913ba5574a5123a299f3315emh * mode, we leave the CPU at whatever speed it is. This
5cff782560a1c3cf913ba5574a5123a299f3315emh * is harmless other than speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
173531301317dd4f31e83d4785873141e984ab86Mark Haywood "cur_spd %d, topspeed %d\n", instance,
173531301317dd4f31e83d4785873141e984ab86Mark Haywood cpupm->cur_spd->pm_level,
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm)->pm_level));
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_FINI(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh
173531301317dd4f31e83d4785873141e984ab86Mark Haywood if (!cpudrv_direct_pm && (cpupm->cur_spd !=
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm))) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->pm_busycnt < 1) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((pm_busy_component(dip, CPUDRV_PM_COMP_NUM)
5cff782560a1c3cf913ba5574a5123a299f3315emh == DDI_SUCCESS)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->pm_busycnt++;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_detach: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: can't busy CPU "
5cff782560a1c3cf913ba5574a5123a299f3315emh "component", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (pm_raise_power(dip, CPUDRV_PM_COMP_NUM,
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm)->pm_level) !=
173531301317dd4f31e83d4785873141e984ab86Mark Haywood DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
173531301317dd4f31e83d4785873141e984ab86Mark Haywood "can't raise CPU power level to %d",
173531301317dd4f31e83d4785873141e984ab86Mark Haywood instance,
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm)->pm_level);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh default:
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver power(9e) entry point.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver's notion of current power is set *only* in power(9e) entry point
5cff782560a1c3cf913ba5574a5123a299f3315emh * after actual power change operation has been successfully completed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh/* ARGSUSED */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_power(dev_info_t *dip, int comp, int level)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh int instance;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_devstate_t *cpudsp;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *new_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh boolean_t is_ready;
5cff782560a1c3cf913ba5574a5123a299f3315emh int ret;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh instance = ddi_get_instance(dip);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
5cff782560a1c3cf913ba5574a5123a299f3315emh instance, level));
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_power: instance %d: can't get state",
5cff782560a1c3cf913ba5574a5123a299f3315emh instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * In normal operation, we fail if we are busy and request is
5cff782560a1c3cf913ba5574a5123a299f3315emh * to lower the power level. We let this go through if the driver
5cff782560a1c3cf913ba5574a5123a299f3315emh * is in special direct pm mode. On x86, we also let this through
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * if the change is due to a request to govern the max speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!cpudrv_direct_pm && (cpupm->pm_busycnt >= 1) &&
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood !cpudrv_pm_is_governor_thread(cpupm)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm->cur_spd != NULL) &&
5cff782560a1c3cf913ba5574a5123a299f3315emh (level < cpupm->cur_spd->pm_level)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh for (new_spd = cpupm->head_spd; new_spd; new_spd = new_spd->down_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (new_spd->pm_level == level)
5cff782560a1c3cf913ba5574a5123a299f3315emh break;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!new_spd) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_power: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't locate new CPU speed", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We currently refuse to power manage if the CPU is not ready to
5cff782560a1c3cf913ba5574a5123a299f3315emh * take cross calls (cross calls fail silently if CPU is not ready
5cff782560a1c3cf913ba5574a5123a299f3315emh * for it).
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Additionally, for x86 platforms we cannot power manage
5cff782560a1c3cf913ba5574a5123a299f3315emh * any one instance, until all instances have been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh * That's because we don't know what the CPU domains look like
5cff782560a1c3cf913ba5574a5123a299f3315emh * until all instances have been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!is_ready) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "CPU not ready for x-calls\n", instance));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood } else if (!(is_ready = cpudrv_pm_power_ready())) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood "waiting for all CPUs to be power manageable\n", instance));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!is_ready) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Execute CPU specific routine on the requested CPU to change its
5cff782560a1c3cf913ba5574a5123a299f3315emh * speed to normal-speed/divisor.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((ret = cpudrv_pm_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_power: cpudrv_pm_change_speed() "
5cff782560a1c3cf913ba5574a5123a299f3315emh "return = %d", ret);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe /*
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe * DTrace probe point for CPU speed change transition
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe */
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe DTRACE_PROBE3(cpu__change__speed, cpudrv_devstate_t *, cpudsp,
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe cpudrv_pm_t *, cpupm, cpudrv_pm_spd_t *, new_spd);
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Reset idle threshold time for the new power level.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm->cur_spd != NULL) && (level < cpupm->cur_spd->pm_level)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (pm_idle_component(dip, CPUDRV_PM_COMP_NUM) ==
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->pm_busycnt >= 1)
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->pm_busycnt--;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
5cff782560a1c3cf913ba5574a5123a299f3315emh "idle CPU component", ddi_get_instance(dip));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Reset various parameters because we are now running at new speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_mstate[CMS_IDLE] = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_mstate[CMS_SYSTEM] = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_mstate[CMS_USER] = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_lbolt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->cur_spd = new_spd;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Initialize the field that will be used for reporting
5cff782560a1c3cf913ba5574a5123a299f3315emh * the supported_frequencies_Hz cpu_info kstat.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
5cff782560a1c3cf913ba5574a5123a299f3315emhset_supp_freqs(cpu_t *cp, cpudrv_pm_t *cpupm)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh char *supp_freqs;
5cff782560a1c3cf913ba5574a5123a299f3315emh char *sfptr;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint64_t *speeds;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh int i;
5cff782560a1c3cf913ba5574a5123a299f3315emh#define UINT64_MAX_STRING (sizeof ("18446744073709551615"))
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh speeds = kmem_zalloc(cpupm->num_spd * sizeof (uint64_t), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh for (i = cpupm->num_spd - 1, spd = cpupm->head_spd; spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh i--, spd = spd->down_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh speeds[i] =
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_SPEED_HZ(cp->cpu_type_info.pi_clock, spd->speed);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh supp_freqs = kmem_zalloc((UINT64_MAX_STRING * cpupm->num_spd),
5cff782560a1c3cf913ba5574a5123a299f3315emh KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh sfptr = supp_freqs;
5cff782560a1c3cf913ba5574a5123a299f3315emh for (i = 0; i < cpupm->num_spd; i++) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (i == cpupm->num_spd - 1) {
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) sprintf(sfptr, "%"PRIu64, speeds[i]);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) sprintf(sfptr, "%"PRIu64":", speeds[i]);
5cff782560a1c3cf913ba5574a5123a299f3315emh sfptr = supp_freqs + strlen(supp_freqs);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh cpu_set_supp_freqs(cp, supp_freqs);
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh kmem_free(supp_freqs, (UINT64_MAX_STRING * cpupm->num_spd));
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(speeds, cpupm->num_spd * sizeof (uint64_t));
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Initialize power management data.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodcpudrv_pm_init_power(cpudrv_devstate_t *cpudsp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *prev_spd = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh int *speeds;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint_t nspeeds;
5cff782560a1c3cf913ba5574a5123a299f3315emh int idle_cnt_percent;
5cff782560a1c3cf913ba5574a5123a299f3315emh int user_cnt_percent;
5cff782560a1c3cf913ba5574a5123a299f3315emh int i;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_GET_SPEEDS(cpudsp, speeds, nspeeds);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (nspeeds < 2) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /* Need at least two speeds to power manage */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->num_spd = nspeeds;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Calculate the watermarks and other parameters based on the
5cff782560a1c3cf913ba5574a5123a299f3315emh * supplied speeds.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * One of the basic assumption is that for X amount of CPU work,
5cff782560a1c3cf913ba5574a5123a299f3315emh * if CPU is slowed down by a factor of N, the time it takes to
5cff782560a1c3cf913ba5574a5123a299f3315emh * do the same work will be N * X.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The driver declares that a CPU is idle and ready for slowed down,
5cff782560a1c3cf913ba5574a5123a299f3315emh * if amount of idle thread is more than the current speed idle_hwm
5cff782560a1c3cf913ba5574a5123a299f3315emh * without dropping below idle_hwm a number of consecutive sampling
5cff782560a1c3cf913ba5574a5123a299f3315emh * intervals and number of running threads in user mode are below
5cff782560a1c3cf913ba5574a5123a299f3315emh * user_lwm. We want to set the current user_lwm such that if we
5cff782560a1c3cf913ba5574a5123a299f3315emh * just switched to the next slower speed with no change in real work
5cff782560a1c3cf913ba5574a5123a299f3315emh * load, the amount of user threads at the slower speed will be such
5cff782560a1c3cf913ba5574a5123a299f3315emh * that it falls below the slower speed's user_hwm. If we didn't do
5cff782560a1c3cf913ba5574a5123a299f3315emh * that then we will just come back to the higher speed as soon as we
5cff782560a1c3cf913ba5574a5123a299f3315emh * go down even with no change in work load.
5cff782560a1c3cf913ba5574a5123a299f3315emh * The user_hwm is a fixed precentage and not calculated dynamically.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * We bring the CPU up if idle thread at current speed is less than
5cff782560a1c3cf913ba5574a5123a299f3315emh * the current speed idle_lwm for a number of consecutive sampling
5cff782560a1c3cf913ba5574a5123a299f3315emh * intervals or user threads are above the user_hwm for the current
5cff782560a1c3cf913ba5574a5123a299f3315emh * speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh for (i = 0; i < nspeeds; i++) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->speed = speeds[i];
5cff782560a1c3cf913ba5574a5123a299f3315emh if (i == 0) { /* normal speed */
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->head_spd = cur_spd;
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm) = cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_NORMAL;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm =
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpudrv_pm_idle_hwm * cur_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh /* can't speed anymore */
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_lwm = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->user_hwm = UINT_MAX;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_OTHR;
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(prev_spd != NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh prev_spd->down_spd = cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->up_spd = cpupm->head_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Let's assume CPU is considered idle at full speed
5cff782560a1c3cf913ba5574a5123a299f3315emh * when it is spending I% of time in running the idle
5cff782560a1c3cf913ba5574a5123a299f3315emh * thread. At full speed, CPU will be busy (100 - I) %
5cff782560a1c3cf913ba5574a5123a299f3315emh * of times. This % of busyness increases by factor of
5cff782560a1c3cf913ba5574a5123a299f3315emh * N as CPU slows down. CPU that is idle I% of times
5cff782560a1c3cf913ba5574a5123a299f3315emh * in full speed, it is idle (100 - ((100 - I) * N)) %
5cff782560a1c3cf913ba5574a5123a299f3315emh * of times in N speed. The idle_lwm is a fixed
5cff782560a1c3cf913ba5574a5123a299f3315emh * percentage. A large value of N may result in
5cff782560a1c3cf913ba5574a5123a299f3315emh * idle_hwm to go below idle_lwm. We need to make sure
5cff782560a1c3cf913ba5574a5123a299f3315emh * that there is at least a buffer zone seperation
5cff782560a1c3cf913ba5574a5123a299f3315emh * between the idle_lwm and idle_hwm values.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh idle_cnt_percent = CPUDRV_PM_IDLE_CNT_PERCENT(
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_idle_hwm, speeds, i);
5cff782560a1c3cf913ba5574a5123a299f3315emh idle_cnt_percent = max(idle_cnt_percent,
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpudrv_pm_idle_lwm + cpudrv_pm_idle_buf_zone));
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm =
5cff782560a1c3cf913ba5574a5123a299f3315emh (idle_cnt_percent * cur_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_lwm =
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpudrv_pm_idle_lwm * cur_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * The lwm for user threads are determined such that
5cff782560a1c3cf913ba5574a5123a299f3315emh * if CPU slows down, the load of work in the
5cff782560a1c3cf913ba5574a5123a299f3315emh * new speed would still keep the CPU at or below the
5cff782560a1c3cf913ba5574a5123a299f3315emh * user_hwm in the new speed. This is to prevent
5cff782560a1c3cf913ba5574a5123a299f3315emh * the quick jump back up to higher speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->user_hwm = (cpudrv_pm_user_hwm *
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh user_cnt_percent = CPUDRV_PM_USER_CNT_PERCENT(
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_user_hwm, speeds, i);
5cff782560a1c3cf913ba5574a5123a299f3315emh prev_spd->user_lwm =
5cff782560a1c3cf913ba5574a5123a299f3315emh (user_cnt_percent * prev_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh prev_spd = cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh /* Slowest speed. Can't slow down anymore */
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm = UINT_MAX;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->user_lwm = -1;
5cff782560a1c3cf913ba5574a5123a299f3315emh#ifdef DEBUG
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: head_spd spd %d, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "num_spd %d\n", ddi_get_instance(cpudsp->dip),
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->head_spd->speed, cpupm->num_spd));
5cff782560a1c3cf913ba5574a5123a299f3315emh for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: speed %d, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "down_spd spd %d, idle_hwm %d, user_lwm %d, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "up_spd spd %d, idle_lwm %d, user_hwm %d, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->speed,
5cff782560a1c3cf913ba5574a5123a299f3315emh (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm, cur_spd->user_lwm,
5cff782560a1c3cf913ba5574a5123a299f3315emh (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_lwm, cur_spd->user_hwm,
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->quant_cnt));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh#endif /* DEBUG */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Free CPU power management data.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_pm_free(cpudrv_devstate_t *cpudsp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *cur_spd, *next_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd = cpupm->head_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh while (cur_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh next_spd = cur_spd->down_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd = next_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh bzero(cpupm, sizeof (cpudrv_pm_t));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood cpudrv_mach_pm_free(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Create pm-components property.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh char **pmc;
5cff782560a1c3cf913ba5574a5123a299f3315emh int size;
5cff782560a1c3cf913ba5574a5123a299f3315emh char name[] = "NAME=CPU Speed";
5cff782560a1c3cf913ba5574a5123a299f3315emh int i, j;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint_t comp_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh int result = DDI_FAILURE;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh size = CPUDRV_PM_COMP_SIZE();
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->num_spd > CPUDRV_PM_COMP_MAX_VAL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "number of speeds exceeded limits",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(cpudsp->dip));
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
5cff782560a1c3cf913ba5574a5123a299f3315emh return (result);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh i--, cur_spd = cur_spd->down_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->pm_level = i;
5cff782560a1c3cf913ba5574a5123a299f3315emh pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh comp_spd = CPUDRV_PM_COMP_SPEED(cpupm, cur_spd);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (comp_spd > CPUDRV_PM_COMP_MAX_VAL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_comp_create: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: speed exceeded limits",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(cpudsp->dip));
5cff782560a1c3cf913ba5574a5123a299f3315emh for (j = cpupm->num_spd; j >= i; j--) {
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc[j], size * sizeof (char));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc, (cpupm->num_spd + 1) *
5cff782560a1c3cf913ba5574a5123a299f3315emh sizeof (char *));
5cff782560a1c3cf913ba5574a5123a299f3315emh return (result);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: pm-components power level %d string '%s'\n",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(cpudsp->dip), i, pmc[i]));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) strcat(pmc[0], name);
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "pm-components component name '%s'\n",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(cpudsp->dip), pmc[0]));
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
5cff782560a1c3cf913ba5574a5123a299f3315emh "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh result = DDI_SUCCESS;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't create pm-components property",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(cpudsp->dip));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh for (i = cpupm->num_spd; i > 0; i--) {
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc[i], size * sizeof (char));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc[0], sizeof (name));
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
5cff782560a1c3cf913ba5574a5123a299f3315emh return (result);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Mark a component idle.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh#define CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm)->pm_busycnt >= 1) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if (pm_idle_component((dip), CPUDRV_PM_COMP_NUM) == \
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: pm_idle_component called\n", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip)))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpupm)->pm_busycnt--; \
5cff782560a1c3cf913ba5574a5123a299f3315emh } else { \
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't idle CPU component", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Marks a component busy in both PM framework and driver state structure.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh#define CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm)->pm_busycnt < 1) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if (pm_busy_component((dip), CPUDRV_PM_COMP_NUM) == \
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: pm_busy_component called\n", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip)))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpupm)->pm_busycnt++; \
5cff782560a1c3cf913ba5574a5123a299f3315emh } else { \
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "can't busy CPU component", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Marks a component busy and calls pm_raise_power().
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh#define CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_level) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh /* \
5cff782560a1c3cf913ba5574a5123a299f3315emh * Mark driver and PM framework busy first so framework doesn't try \
5cff782560a1c3cf913ba5574a5123a299f3315emh * to bring CPU to lower speed when we need to be at higher speed. \
5cff782560a1c3cf913ba5574a5123a299f3315emh */ \
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&(cpudsp)->lock); \
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
5cff782560a1c3cf913ba5574a5123a299f3315emh (new_level))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh if (pm_raise_power((dip), CPUDRV_PM_COMP_NUM, (new_level)) != \
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "raise CPU power level", ddi_get_instance((dip))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&(cpudsp)->lock); \
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * In order to monitor a CPU, we need to hold cpu_lock to access CPU
5cff782560a1c3cf913ba5574a5123a299f3315emh * statistics. Holding cpu_lock is not allowed from a callout routine.
5cff782560a1c3cf913ba5574a5123a299f3315emh * We dispatch a taskq to do that job.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_pm_monitor_disp(void *arg)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_devstate_t *cpudsp = (cpudrv_devstate_t *)arg;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We are here because the last task has scheduled a timeout.
5cff782560a1c3cf913ba5574a5123a299f3315emh * The queue should be empty at this time.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_pm_monitor, arg,
5cff782560a1c3cf913ba5574a5123a299f3315emh TQ_NOSLEEP)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor_disp: failed to "
5cff782560a1c3cf913ba5574a5123a299f3315emh "dispatch the cpudrv_pm_monitor taskq\n"));
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudsp->cpudrv_pm.timeout_count++;
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Monitors each CPU for the amount of time idle thread was running in the
5cff782560a1c3cf913ba5574a5123a299f3315emh * last quantum and arranges for the CPU to go to the lower or higher speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Called at the time interval appropriate for the current speed. The
5cff782560a1c3cf913ba5574a5123a299f3315emh * time interval for normal speed is CPUDRV_PM_QUANT_CNT_NORMAL. The time
5cff782560a1c3cf913ba5574a5123a299f3315emh * interval for other speeds (including unknown speed) is
5cff782560a1c3cf913ba5574a5123a299f3315emh * CPUDRV_PM_QUANT_CNT_OTHR.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
5cff782560a1c3cf913ba5574a5123a299f3315emhcpudrv_pm_monitor(void *arg)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_devstate_t *cpudsp = (cpudrv_devstate_t *)arg;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_t *cpupm;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudrv_pm_spd_t *cur_spd, *new_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpu_t *cp;
5cff782560a1c3cf913ba5574a5123a299f3315emh dev_info_t *dip;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint_t idle_cnt, user_cnt, system_cnt;
5cff782560a1c3cf913ba5574a5123a299f3315emh clock_t lbolt_cnt;
5cff782560a1c3cf913ba5574a5123a299f3315emh hrtime_t msnsecs[NCMSTATES];
5cff782560a1c3cf913ba5574a5123a299f3315emh boolean_t is_ready;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#define GET_CPU_MSTATE_CNT(state, cnt) \
5cff782560a1c3cf913ba5574a5123a299f3315emh msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
5cff782560a1c3cf913ba5574a5123a299f3315emh msnsecs[state] = cpupm->lastquan_mstate[state]; \
5cff782560a1c3cf913ba5574a5123a299f3315emh cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_mstate[state] = msnsecs[state]
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm = &(cpudsp->cpudrv_pm);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->timeout_id == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd = cpupm->cur_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh dip = cpudsp->dip;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We assume that a CPU is initialized and has a valid cpu_t
5cff782560a1c3cf913ba5574a5123a299f3315emh * structure, if it is ready for cross calls. If this changes,
5cff782560a1c3cf913ba5574a5123a299f3315emh * additional checks might be needed.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Additionally, for x86 platforms we cannot power manage
5cff782560a1c3cf913ba5574a5123a299f3315emh * any one instance, until all instances have been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh * That's because we don't know what the CPU domains look like
5cff782560a1c3cf913ba5574a5123a299f3315emh * until all instances have been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!is_ready) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "CPU not ready for x-calls\n", ddi_get_instance(dip)));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood } else if (!(is_ready = cpudrv_pm_power_ready())) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood "waiting for all CPUs to be power manageable\n",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(dip)));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!is_ready) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Make sure that we are busy so that framework doesn't
5cff782560a1c3cf913ba5574a5123a299f3315emh * try to bring us down in this situation.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Make sure that we are still not at unknown power level.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cur_spd == NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "cur_spd is unknown\n", ddi_get_instance(dip)));
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
173531301317dd4f31e83d4785873141e984ab86Mark Haywood CPUDRV_PM_TOPSPEED(cpupm)->pm_level);
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We just changed the speed. Wait till at least next
5cff782560a1c3cf913ba5574a5123a299f3315emh * call to this routine before proceeding ahead.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpu_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cp = cpu_get(cpudsp->cpu_id)) == NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpu_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't get "
5cff782560a1c3cf913ba5574a5123a299f3315emh "cpu_t", ddi_get_instance(dip));
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh if (!cpupm->pm_started) {
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh cpupm->pm_started = B_TRUE;
5cff782560a1c3cf913ba5574a5123a299f3315emh set_supp_freqs(cp, cpupm);
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
4b3651bda421ca87fe4c05950903b87abb7c8525mh get_cpu_mstate(cp, msnsecs);
5cff782560a1c3cf913ba5574a5123a299f3315emh GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
5cff782560a1c3cf913ba5574a5123a299f3315emh GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
5cff782560a1c3cf913ba5574a5123a299f3315emh GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We can't do anything when we have just switched to a state
5cff782560a1c3cf913ba5574a5123a299f3315emh * because there is no valid timestamp.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->lastquan_lbolt == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_lbolt = lbolt;
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpu_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Various watermarks are based on this routine being called back
5cff782560a1c3cf913ba5574a5123a299f3315emh * exactly at the requested period. This is not guaranteed
5cff782560a1c3cf913ba5574a5123a299f3315emh * because this routine is called from a taskq that is dispatched
5cff782560a1c3cf913ba5574a5123a299f3315emh * from a timeout routine. Handle this by finding out how many
5cff782560a1c3cf913ba5574a5123a299f3315emh * ticks have elapsed since the last call (lbolt_cnt) and adjusting
5cff782560a1c3cf913ba5574a5123a299f3315emh * the idle_cnt based on the delay added to the requested period
5cff782560a1c3cf913ba5574a5123a299f3315emh * by timeout and taskq.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh lbolt_cnt = lbolt - cpupm->lastquan_lbolt;
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->lastquan_lbolt = lbolt;
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpu_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Time taken between recording the current counts and
5cff782560a1c3cf913ba5574a5123a299f3315emh * arranging the next call of this routine is an error in our
5cff782560a1c3cf913ba5574a5123a299f3315emh * calculation. We minimize the error by calling
5cff782560a1c3cf913ba5574a5123a299f3315emh * CPUDRV_PM_MONITOR_INIT() here instead of end of this routine.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_pm_monitor: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "idle count %d, user count %d, system count %d, pm_level %d, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
5cff782560a1c3cf913ba5574a5123a299f3315emh system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#ifdef DEBUG
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Notify that timeout and taskq has caused delays and we need to
5cff782560a1c3cf913ba5574a5123a299f3315emh * scale our parameters accordingly.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * To get accurate result, don't turn on other DPRINTFs with
5cff782560a1c3cf913ba5574a5123a299f3315emh * the following DPRINTF. PROM calls generated by other
5cff782560a1c3cf913ba5574a5123a299f3315emh * DPRINTFs changes the timing.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (lbolt_cnt > cur_spd->quant_cnt) {
5cff782560a1c3cf913ba5574a5123a299f3315emh DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_pm_monitor: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "lbolt count %ld > quantum_count %u\n",
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance(dip), lbolt_cnt, cur_spd->quant_cnt));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh#endif /* DEBUG */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Adjust counts based on the delay added by timeout and taskq.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh idle_cnt = (idle_cnt * cur_spd->quant_cnt) / lbolt_cnt;
5cff782560a1c3cf913ba5574a5123a299f3315emh user_cnt = (user_cnt * cur_spd->quant_cnt) / lbolt_cnt;
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_blwm_cnt >= cpudrv_pm_idle_blwm_cnt_max)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_blwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_bhwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * In normal situation, arrange to go to next higher speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh * If we are running in special direct pm mode, we just stay
5cff782560a1c3cf913ba5574a5123a299f3315emh * at the current speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh new_spd = cur_spd->up_spd;
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
5cff782560a1c3cf913ba5574a5123a299f3315emh new_spd->pm_level);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if ((user_cnt <= cur_spd->user_lwm) &&
5cff782560a1c3cf913ba5574a5123a299f3315emh (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cp)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_blwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_bhwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Arrange to go to next lower speed by informing our idle
5cff782560a1c3cf913ba5574a5123a299f3315emh * status to the power management framework.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * If we are between the idle water marks and have not
5cff782560a1c3cf913ba5574a5123a299f3315emh * been here enough consecutive times to be considered
5cff782560a1c3cf913ba5574a5123a299f3315emh * busy, just increment the count and return.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((idle_cnt < cur_spd->idle_hwm) &&
5cff782560a1c3cf913ba5574a5123a299f3315emh (idle_cnt >= cur_spd->idle_lwm) &&
5cff782560a1c3cf913ba5574a5123a299f3315emh (cur_spd->idle_bhwm_cnt < cpudrv_pm_idle_bhwm_cnt_max)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_blwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_bhwm_cnt++;
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (idle_cnt < cur_spd->idle_lwm) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_blwm_cnt++;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_bhwm_cnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Arranges to stay at the current speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emhdo_return:
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpupm->timeout_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(cpupm->timeout_count > 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->timeout_count--;
5cff782560a1c3cf913ba5574a5123a299f3315emh cv_signal(&cpupm->timeout_cv);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpupm->timeout_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh}