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/*
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Use is subject to license terms.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood/*
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * Copyright (c) 2009, Intel Corporation.
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * All Rights Reserved.
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood */
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>
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#include <sys/epm.h>
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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic int cpudrv_init(cpudrv_devstate_t *cpudsp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic void cpudrv_free(cpudrv_devstate_t *cpudsp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic int cpudrv_comp_create(cpudrv_devstate_t *cpudsp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic void cpudrv_monitor_disp(void *arg);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic void cpudrv_monitor(void *arg);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Driver global variables
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhuint_t cpudrv_debug = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emhvoid *cpudrv_state;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_idle_hwm = CPUDRV_IDLE_HWM;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_idle_lwm = CPUDRV_IDLE_LWM;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_idle_buf_zone = CPUDRV_IDLE_BUF_ZONE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_idle_bhwm_cnt_max = CPUDRV_IDLE_BHWM_CNT_MAX;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_idle_blwm_cnt_max = CPUDRV_IDLE_BLWM_CNT_MAX;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxestatic uint_t cpudrv_user_hwm = CPUDRV_USER_HWM;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxeboolean_t cpudrv_enabled = B_TRUE;
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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#define CPUDRV_MONITOR_INIT(cpudsp) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpudrv_is_enabled(cpudsp)) { \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT(mutex_owned(&(cpudsp)->lock)); \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (cpudsp)->cpudrv_pm.timeout_id = \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe timeout(cpudrv_monitor_disp, \
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#define CPUDRV_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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
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));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!cpudrv_is_enabled(NULL))
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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh cpudsp->dip = dip;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Find CPU number for this dev_info node.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!cpudrv_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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpudrv_is_enabled(cpudsp)) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpudrv_init(cpudsp) != DDI_SUCCESS) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_free(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ddi_soft_state_free(cpudrv_state, instance);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (DDI_FAILURE);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpudrv_comp_create(cpudsp) != DDI_SUCCESS) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_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) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_enabled = B_FALSE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_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 */
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cpudsp->cpudrv_pm.tq = ddi_taskq_create(dip,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood "cpudrv_monitor", CPUDRV_TASKQ_THREADS,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood TASKQ_DEFAULTPRI, 0);
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 /*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * We need to take the lock because cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_INIT(cpudsp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
5cff782560a1c3cf913ba5574a5123a299f3315emh
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood if (!cpudrv_mach_init(cpudsp)) {
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood "cpudrv_mach_init failed", instance);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cpudrv_enabled = B_FALSE;
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cpudrv_free(cpudsp);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood ddi_soft_state_free(cpudrv_state, instance);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood return (DDI_FAILURE);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood }
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_INSTALL_MAX_CHANGE_HANDLER(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood (void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood DDI_NO_AUTODETACH, 1);
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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!cpudrv_is_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;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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));
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood#if defined(__x86)
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cpudsp = ddi_get_soft_state(cpudrv_state, instance);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood ASSERT(cpudsp != NULL);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood /*
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * Nothing to do for detach, if no doing active PM.
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood */
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood if (!cpudrv_is_enabled(cpudsp))
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood return (DDI_SUCCESS);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood /*
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * uninstall PPC/_TPC change notification handler
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood */
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood CPUDRV_UNINSTALL_MAX_CHANGE_HANDLER(cpudsp);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood /*
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * destruct platform specific resource
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood */
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood if (!cpudrv_mach_fini(cpudsp))
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood return (DDI_FAILURE);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood mutex_enter(&cpudsp->lock);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood CPUDRV_MONITOR_FINI(cpudsp);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cv_destroy(&cpudsp->cpudrv_pm.timeout_cv);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood mutex_destroy(&cpudsp->cpudrv_pm.timeout_lock);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood ddi_taskq_destroy(cpudsp->cpudrv_pm.tq);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood cpudrv_free(cpudsp);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood mutex_exit(&cpudsp->lock);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood mutex_destroy(&cpudsp->lock);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood ddi_soft_state_free(cpudrv_state, instance);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood (void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood DDI_NO_AUTODETACH, 0);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood return (DDI_SUCCESS);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood#else
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);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood#endif
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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!cpudrv_is_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,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_TOPSPEED(cpupm)->pm_level));
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_FINI(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh
173531301317dd4f31e83d4785873141e984ab86Mark Haywood if (!cpudrv_direct_pm && (cpupm->cur_spd !=
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_TOPSPEED(cpupm))) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (cpupm->pm_busycnt < 1) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((pm_busy_component(dip, CPUDRV_COMP_NUM)
5cff782560a1c3cf913ba5574a5123a299f3315emh == DDI_SUCCESS)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh cpupm->pm_busycnt++;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (pm_raise_power(dip, CPUDRV_COMP_NUM,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_TOPSPEED(cpupm)->pm_level) !=
173531301317dd4f31e83d4785873141e984ab86Mark Haywood DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_pm_t *cpudrvpm;
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));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe "get state", instance);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com /*
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com * We're not ready until we can get a cpu_t
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com is_ready = (cpudrv_get_cpu(cpudsp) == DDI_SUCCESS);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm = &(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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!cpudrv_direct_pm && (cpudrvpm->pm_busycnt >= 1) &&
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe !cpudrv_is_governor_thread(cpudrvpm)) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((cpudrvpm->cur_spd != NULL) &&
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (level < cpudrvpm->cur_spd->pm_level)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe for (new_spd = cpudrvpm->head_spd; new_spd; new_spd =
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe new_spd->down_spd) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (new_spd->pm_level == level)
5cff782560a1c3cf913ba5574a5123a299f3315emh break;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!new_spd) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
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 *
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * Additionally, for x86 platforms we cannot power manage an instance,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * until it has been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (is_ready) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com is_ready = CPUDRV_XCALL_IS_READY(cpudsp->cpu_id);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (!is_ready) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com "CPU not ready for x-calls\n", instance));
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com } else if (!(is_ready = cpudrv_power_ready(cpudsp->cp))) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com "waiting for all CPUs to be power manageable\n",
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com instance));
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!is_ready) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Execute CPU specific routine on the requested CPU to
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * change its speed to normal-speed/divisor.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((ret = cpudrv_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_power: "
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe "cpudrv_change_speed() return = %d", ret);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_FAILURE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Reset idle threshold time for the new power level.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((cpudrvpm->cur_spd != NULL) && (level <
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->cur_spd->pm_level)) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (pm_idle_component(dip, CPUDRV_COMP_NUM) ==
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpudrvpm->pm_busycnt >= 1)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->pm_busycnt--;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } else {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_power: instance %d: "
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe "can't idle CPU component",
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe ddi_get_instance(dip));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Reset various parameters because we are now running at new speed.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->lastquan_mstate[CMS_IDLE] = 0;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->lastquan_mstate[CMS_SYSTEM] = 0;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->lastquan_mstate[CMS_USER] = 0;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->lastquan_ticks = 0;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrvpm->cur_spd = new_spd;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Initialize power management data.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxecpudrv_init(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
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_GET_SPEEDS(cpudsp, speeds, nspeeds);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (nspeeds < 2) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /* Need at least two speeds to power manage */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_TOPSPEED(cpupm) = cur_spd;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cur_spd->quant_cnt = CPUDRV_QUANT_CNT_NORMAL;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm =
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (cpudrv_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 {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cur_spd->quant_cnt = CPUDRV_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe idle_cnt_percent = CPUDRV_IDLE_CNT_PERCENT(
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_idle_hwm, speeds, i);
5cff782560a1c3cf913ba5574a5123a299f3315emh idle_cnt_percent = max(idle_cnt_percent,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (cpudrv_idle_lwm + cpudrv_idle_buf_zone));
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_hwm =
5cff782560a1c3cf913ba5574a5123a299f3315emh (idle_cnt_percent * cur_spd->quant_cnt) / 100;
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->idle_lwm =
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cur_spd->user_hwm = (cpudrv_user_hwm *
5cff782560a1c3cf913ba5574a5123a299f3315emh cur_spd->quant_cnt) / 100;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe user_cnt_percent = CPUDRV_USER_CNT_PERCENT(
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_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
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_INIT, ("cpudrv_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) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_INIT, ("cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_FREE_SPEEDS(speeds, nspeeds);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (DDI_SUCCESS);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Free CPU power management data.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxecpudrv_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));
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Create pm-components property.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxecpudrv_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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe size = CPUDRV_COMP_SIZE();
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (cpupm->num_spd > CPUDRV_COMP_MAX_VAL) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe comp_spd = CPUDRV_COMP_SPEED(cpupm, cur_spd);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (comp_spd > CPUDRV_COMP_MAX_VAL) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_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 }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_COMP_CREATE, ("cpudrv_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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_COMP_CREATE, ("cpudrv_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 {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#define CPUDRV_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm)->pm_busycnt >= 1) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (pm_idle_component((dip), CPUDRV_COMP_NUM) == \
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: pm_idle_component called\n", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip)))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpupm)->pm_busycnt--; \
5cff782560a1c3cf913ba5574a5123a299f3315emh } else { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#define CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((cpupm)->pm_busycnt < 1) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (pm_busy_component((dip), CPUDRV_COMP_NUM) == \
5cff782560a1c3cf913ba5574a5123a299f3315emh DDI_SUCCESS) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "instance %d: pm_busy_component called\n", \
5cff782560a1c3cf913ba5574a5123a299f3315emh ddi_get_instance((dip)))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh (cpupm)->pm_busycnt++; \
5cff782560a1c3cf913ba5574a5123a299f3315emh } else { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_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 */
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood#define CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_spd) { \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood int ret; \
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 */ \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&(cpudsp)->lock); \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood (new_spd->pm_level))); \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood ret = pm_raise_power((dip), CPUDRV_COMP_NUM, (new_spd->pm_level)); \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood if (ret != DDI_SUCCESS) { \
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cmn_err(CE_WARN, "cpudrv_monitor: instance %d: can't " \
5cff782560a1c3cf913ba5574a5123a299f3315emh "raise CPU power level", ddi_get_instance((dip))); \
5cff782560a1c3cf913ba5574a5123a299f3315emh } \
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&(cpudsp)->lock); \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood if (ret == DDI_SUCCESS && cpudsp->cpudrv_pm.cur_spd == NULL) { \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood cpudsp->cpudrv_pm.cur_spd = new_spd; \
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood } \
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
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxecpudrv_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);
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood if ((ddi_taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_monitor, arg,
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood DDI_NOSLEEP)) != DDI_SUCCESS) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR, ("cpudrv_monitor_disp: failed to "
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe "dispatch the cpudrv_monitor taskq\n"));
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&cpudsp->lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * time interval for normal speed is CPUDRV_QUANT_CNT_NORMAL. The time
5cff782560a1c3cf913ba5574a5123a299f3315emh * interval for other speeds (including unknown speed) is
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * CPUDRV_QUANT_CNT_OTHR.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic void
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxecpudrv_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 dev_info_t *dip;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint_t idle_cnt, user_cnt, system_cnt;
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood clock_t ticks;
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood uint_t tick_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
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com /*
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com * We're not ready until we can get a cpu_t
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com is_ready = (cpudrv_get_cpu(cpudsp) == DDI_SUCCESS);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
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 *
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * Additionally, for x86 platforms we cannot power manage an
444f66e774d0e4f449866c7f5e64095f2fb0def0Mark Haywood * instance, until it has been initialized.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (is_ready) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com is_ready = CPUDRV_XCALL_IS_READY(cpudsp->cpu_id);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (!is_ready) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com "CPU not ready for x-calls\n",
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com ddi_get_instance(dip)));
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com } else if (!(is_ready = cpudrv_power_ready(cpudsp->cp))) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com "waiting for all CPUs to be power manageable\n",
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com ddi_get_instance(dip)));
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com }
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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
5cff782560a1c3cf913ba5574a5123a299f3315emh "cur_spd is unknown\n", ddi_get_instance(dip)));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood CPUDRV_TOPSPEED(cpupm));
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We just changed the speed. Wait till at least next
5cff782560a1c3cf913ba5574a5123a299f3315emh * call to this routine before proceeding ahead.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_INIT(cpudsp);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&cpudsp->lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh goto do_return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh if (!cpupm->pm_started) {
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh cpupm->pm_started = B_TRUE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cpudrv_set_supp_freqs(cpudsp);
68afbec1fabe0d352bb5ab4ed82c44b58ec651fbmh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe get_cpu_mstate(cpudsp->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 */
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood if (cpupm->lastquan_ticks == 0) {
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood cpupm->lastquan_ticks = NSEC_TO_TICK(gethrtime());
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood * ticks have elapsed since the last call and adjusting
5cff782560a1c3cf913ba5574a5123a299f3315emh * the idle_cnt based on the delay added to the requested period
5cff782560a1c3cf913ba5574a5123a299f3315emh * by timeout and taskq.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood ticks = NSEC_TO_TICK(gethrtime());
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood tick_cnt = ticks - cpupm->lastquan_ticks;
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood ASSERT(tick_cnt != 0);
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood cpupm->lastquan_ticks = ticks;
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
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
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * CPUDRV_MONITOR_INIT() here instead of end of this routine.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_INIT(cpudsp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_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 */
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood if (tick_cnt > cur_spd->quant_cnt) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_monitor: instance %d: "
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood "tick count %d > quantum_count %u\n",
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood ddi_get_instance(dip), tick_cnt, cur_spd->quant_cnt));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh#endif /* DEBUG */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Adjust counts based on the delay added by timeout and taskq.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood idle_cnt = (idle_cnt * cur_spd->quant_cnt) / tick_cnt;
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood user_cnt = (user_cnt * cur_spd->quant_cnt) / tick_cnt;
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cur_spd->idle_blwm_cnt >= cpudrv_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) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh new_spd = cur_spd->up_spd;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
67bdf3b0f9c03ddb09508476025689fb2ca68f45Mark Haywood new_spd);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if ((user_cnt <= cur_spd->user_lwm) &&
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cpudsp->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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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) &&
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (cur_spd->idle_bhwm_cnt < cpudrv_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 */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe CPUDRV_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}
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com/*
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com * get cpu_t structure for cpudrv_devstate_t
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.comint
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.comcpudrv_get_cpu(cpudrv_devstate_t *cpudsp)
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com{
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com ASSERT(cpudsp != NULL);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com /*
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com * return DDI_SUCCESS if cpudrv_devstate_t
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com * already contains cpu_t structure
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com */
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (cpudsp->cp != NULL)
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com return (DDI_SUCCESS);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (MUTEX_HELD(&cpu_lock)) {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com cpudsp->cp = cpu_get(cpudsp->cpu_id);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com } else {
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com mutex_enter(&cpu_lock);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com cpudsp->cp = cpu_get(cpudsp->cpu_id);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com mutex_exit(&cpu_lock);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com }
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com if (cpudsp->cp == NULL)
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com return (DDI_FAILURE);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com return (DDI_SUCCESS);
6af9d45285383ab606043218665b664d95c2e9c8aubrey.li@intel.com}