xref: /onnv-gate/usr/src/uts/sun4u/excalibur/io/xcalppm.c (revision 11311:639e7bc0b42f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
67656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11311SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Platform Power Management driver for SUNW,Sun-Blade-1000
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #include <sys/modctl.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
340Sstevel@tonic-gate #include <sys/ppmvar.h>
350Sstevel@tonic-gate #include <sys/ppmio.h>
360Sstevel@tonic-gate #include <sys/xcalppm_reg.h>
370Sstevel@tonic-gate #include <sys/xcalppm_var.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/epm.h>
400Sstevel@tonic-gate #include <sys/archsystm.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/cheetahregs.h>
430Sstevel@tonic-gate #include <sys/us3_module.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Locking Considerations
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * To look at and/or modify xcppm_domain fields or elements of its list of
490Sstevel@tonic-gate  * xcppm_dev structures the domain_lock for the affected domain must be held.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * When the autopm framework needs to change the power of a component of a
520Sstevel@tonic-gate  * device, it needs to hold the associated power lock (see discussion at
530Sstevel@tonic-gate  * top of uts/common/os/sunpm.c).
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * If the framework needs to lock a dev/cmpt for a device which this ppm
560Sstevel@tonic-gate  * has claimed, xcppm_ctlops will be called with PMR_PPM_LOCK_POWER.  Ppm
570Sstevel@tonic-gate  * needs to be involved because, due to platform constraints, changing the
580Sstevel@tonic-gate  * power of one device may require that other devices be changed in the same
590Sstevel@tonic-gate  * operation.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * In some domains (e.g., cpus) the power lock must be acquired for all the
620Sstevel@tonic-gate  * affected devices to avoid possible corruption of the power states.  The
630Sstevel@tonic-gate  * joint change must be an atomic operation.  Ppm handles this by acquiring
640Sstevel@tonic-gate  * the domain lock, then walking the list of affected devices and acquiring
650Sstevel@tonic-gate  * the power lock for each of them.  To unlock, the list is traversed and
660Sstevel@tonic-gate  * each of the power locks is freed, followed by freeing the domain lock.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * For other domains ppm will only be changing the power of a single device
690Sstevel@tonic-gate  * that is known to the framework.  In these cases, the locking is done by
700Sstevel@tonic-gate  * acquiring the domain lock and directly calling the framework routine for
710Sstevel@tonic-gate  * getting a single power lock.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int	xcppm_attach(dev_info_t *, ddi_attach_cmd_t);
750Sstevel@tonic-gate static int	xcppm_detach(dev_info_t *, ddi_detach_cmd_t);
760Sstevel@tonic-gate static int	xcppm_ctlops(dev_info_t *, dev_info_t *,
770Sstevel@tonic-gate 		    ddi_ctl_enum_t, void *, void *);
780Sstevel@tonic-gate static void	xcppm_dev_init(ppm_dev_t *);
790Sstevel@tonic-gate static void	xcppm_dev_fini(ppm_dev_t *);
800Sstevel@tonic-gate static void	xcppm_iocset(uint8_t);
810Sstevel@tonic-gate static uint8_t	xcppm_iocget(void);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Note: 1394 and pciupa were originally required to be LOCK_ALL domains.
850Sstevel@tonic-gate  * However, the underlying nexus drivers aren't able to do power mgmt
860Sstevel@tonic-gate  * (because of hw implementation issues).  The locking protocol for these
870Sstevel@tonic-gate  * domains is changed to LOCK_ONE to simplify other code.  The domain
880Sstevel@tonic-gate  * code itself will be removed in the future.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static ppm_domain_t xcppm_1394 = { "domain_1394",	PPMD_LOCK_ONE };
910Sstevel@tonic-gate static ppm_domain_t xcppm_cpu  = { "domain_cpu",	PPMD_LOCK_ALL };
920Sstevel@tonic-gate static ppm_domain_t xcppm_fet  = { "domain_powerfet",	PPMD_LOCK_ONE };
930Sstevel@tonic-gate static ppm_domain_t xcppm_upa  = { "domain_pciupa",	PPMD_LOCK_ONE };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate ppm_domain_t *ppm_domains[] = {
960Sstevel@tonic-gate 	&xcppm_1394,
970Sstevel@tonic-gate 	&xcppm_cpu,
980Sstevel@tonic-gate 	&xcppm_fet,
990Sstevel@tonic-gate 	&xcppm_upa,
1000Sstevel@tonic-gate 	NULL
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct ppm_funcs ppmf = {
1050Sstevel@tonic-gate 	xcppm_dev_init,			/* dev_init */
1060Sstevel@tonic-gate 	xcppm_dev_fini,			/* dev_fini */
1070Sstevel@tonic-gate 	xcppm_iocset,			/* iocset */
1080Sstevel@tonic-gate 	xcppm_iocget,			/* iocget */
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * The order of entries must be from slowest to fastest and in
1140Sstevel@tonic-gate  * one-to-one correspondence with the cpu_level array.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate static const uint16_t bbc_estar_control_masks[] = {
1170Sstevel@tonic-gate 	BBC_ESTAR_SLOW, BBC_ESTAR_MEDIUM, BBC_ESTAR_FAST
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate int bbc_delay = 10;			/* microsec */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Configuration data structures
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate static struct cb_ops xcppm_cb_ops = {
1270Sstevel@tonic-gate 	ppm_open,		/* open */
1280Sstevel@tonic-gate 	ppm_close,		/* close */
1290Sstevel@tonic-gate 	nodev,			/* strategy */
1300Sstevel@tonic-gate 	nodev,			/* print */
1310Sstevel@tonic-gate 	nodev,			/* dump */
1320Sstevel@tonic-gate 	nodev,			/* read */
1330Sstevel@tonic-gate 	nodev,			/* write */
1340Sstevel@tonic-gate 	ppm_ioctl,		/* ioctl */
1350Sstevel@tonic-gate 	nodev,			/* devmap */
1360Sstevel@tonic-gate 	nodev,			/* mmap */
1370Sstevel@tonic-gate 	nodev,			/* segmap */
1380Sstevel@tonic-gate 	nochpoll,		/* poll */
1390Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1400Sstevel@tonic-gate 	NULL,			/* streamtab */
1410Sstevel@tonic-gate 	D_MP | D_NEW,		/* driver compatibility flag */
1420Sstevel@tonic-gate 	CB_REV,			/* cb_ops revision */
1430Sstevel@tonic-gate 	nodev,			/* async read */
1440Sstevel@tonic-gate 	nodev			/* async write */
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static struct bus_ops xcppm_bus_ops = {
1480Sstevel@tonic-gate 	BUSO_REV,
1490Sstevel@tonic-gate 	0,
1500Sstevel@tonic-gate 	0,
1510Sstevel@tonic-gate 	0,
1520Sstevel@tonic-gate 	0,
1530Sstevel@tonic-gate 	0,
1540Sstevel@tonic-gate 	ddi_no_dma_map,
1550Sstevel@tonic-gate 	ddi_no_dma_allochdl,
1560Sstevel@tonic-gate 	ddi_no_dma_freehdl,
1570Sstevel@tonic-gate 	ddi_no_dma_bindhdl,
1580Sstevel@tonic-gate 	ddi_no_dma_unbindhdl,
1590Sstevel@tonic-gate 	ddi_no_dma_flush,
1600Sstevel@tonic-gate 	ddi_no_dma_win,
1610Sstevel@tonic-gate 	ddi_no_dma_mctl,
1620Sstevel@tonic-gate 	xcppm_ctlops,
1630Sstevel@tonic-gate 	0,
1640Sstevel@tonic-gate 	0,			/* (*bus_get_eventcookie)();	*/
1650Sstevel@tonic-gate 	0,			/* (*bus_add_eventcall)();	*/
1660Sstevel@tonic-gate 	0,			/* (*bus_remove_eventcall)();	*/
1670Sstevel@tonic-gate 	0			/* (*bus_post_event)();		*/
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static struct dev_ops xcppm_ops = {
1710Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1720Sstevel@tonic-gate 	0,			/* refcnt */
1730Sstevel@tonic-gate 	ppm_getinfo,		/* info */
1740Sstevel@tonic-gate 	nulldev,		/* identify */
1750Sstevel@tonic-gate 	nulldev,		/* probe */
1760Sstevel@tonic-gate 	xcppm_attach,		/* attach */
1770Sstevel@tonic-gate 	xcppm_detach,		/* detach */
1780Sstevel@tonic-gate 	nodev,			/* reset */
1790Sstevel@tonic-gate 	&xcppm_cb_ops,		/* driver operations */
1800Sstevel@tonic-gate 	&xcppm_bus_ops,		/* bus operations */
1810Sstevel@tonic-gate 	NULL,			/* power */
1827656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static struct modldrv modldrv = {
1880Sstevel@tonic-gate 	&mod_driverops,		/* type of module - pseudo */
1897656SSherry.Moore@Sun.COM 	"platform pm driver",
1900Sstevel@tonic-gate 	&xcppm_ops
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static struct modlinkage modlinkage = {
1940Sstevel@tonic-gate 	MODREV_1,
1950Sstevel@tonic-gate 	&modldrv,
1960Sstevel@tonic-gate 	NULL
1970Sstevel@tonic-gate };
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
_init(void)2010Sstevel@tonic-gate _init(void)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	return (ppm_init(&modlinkage, sizeof (xcppm_unit_t), "xc"));
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate int
_fini(void)2080Sstevel@tonic-gate _fini(void)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	return (EBUSY);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2150Sstevel@tonic-gate _info(struct modinfo *modinfop)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static int
xcppm_map_all_regs(dev_info_t * dip)2220Sstevel@tonic-gate xcppm_map_all_regs(dev_info_t *dip)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	ddi_device_acc_attr_t attr_be, attr_le;
2250Sstevel@tonic-gate 	int rv0, rv1, rv2, rv3;
2260Sstevel@tonic-gate 	xcppm_unit_t *unitp;
2270Sstevel@tonic-gate 	caddr_t base_addr;
2280Sstevel@tonic-gate 	uint8_t data8;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
2310Sstevel@tonic-gate 	attr_be.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2320Sstevel@tonic-gate 	attr_be.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
2330Sstevel@tonic-gate 	attr_be.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	attr_le.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2360Sstevel@tonic-gate 	attr_le.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
2370Sstevel@tonic-gate 	attr_le.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	rv0 = ddi_regs_map_setup(dip, 0, &base_addr, 0, 0, &attr_be,
2400Sstevel@tonic-gate 	    &unitp->hndls.bbc_estar_ctrl);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	unitp->regs.bbc_estar_ctrl = (uint16_t *)(base_addr +
2430Sstevel@tonic-gate 	    BBC_ESTAR_CTRL_OFFSET);
2440Sstevel@tonic-gate 	unitp->regs.bbc_assert_change = (uint32_t *)(base_addr +
2450Sstevel@tonic-gate 	    BBC_ASSERT_CHANGE_OFFSET);
2460Sstevel@tonic-gate 	unitp->regs.bbc_pll_settle = (uint32_t *)(base_addr +
2470Sstevel@tonic-gate 	    BBC_PLL_SETTLE_OFFSET);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	rv1 = ddi_regs_map_setup(dip, 1,
2500Sstevel@tonic-gate 	    (caddr_t *)&unitp->regs.rio_mode_auxio,
2510Sstevel@tonic-gate 	    0, 0, &attr_le, &unitp->hndls.rio_mode_auxio);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	rv2 = ddi_regs_map_setup(dip, 2, &base_addr,
2540Sstevel@tonic-gate 	    0, 0, &attr_le, &unitp->hndls.gpio_bank_select);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	unitp->regs.gpio_bank_sel_index = (uint8_t *)(base_addr +
2570Sstevel@tonic-gate 	    GPIO_BANK_SEL_INDEX_OFFSET);
2580Sstevel@tonic-gate 	unitp->regs.gpio_bank_sel_data = (uint8_t *)(base_addr +
2590Sstevel@tonic-gate 	    GPIO_BANK_SEL_DATA_OFFSET);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	rv3 = ddi_regs_map_setup(dip, 3, &base_addr, 0, 0, &attr_le,
2620Sstevel@tonic-gate 	    &unitp->hndls.gpio_data_ports);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	unitp->regs.gpio_port1_data = (uint8_t *)(base_addr +
2650Sstevel@tonic-gate 	    GPIO_PORT1_DATA_OFFSET);
2660Sstevel@tonic-gate 	unitp->regs.gpio_port2_data = (uint8_t *)(base_addr +
2670Sstevel@tonic-gate 	    GPIO_PORT2_DATA_OFFSET);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (rv0 != DDI_SUCCESS || rv1 != DDI_SUCCESS ||
2700Sstevel@tonic-gate 	    rv2 != DDI_SUCCESS || rv3 != DDI_SUCCESS) {
2710Sstevel@tonic-gate 		if (rv0 == DDI_SUCCESS)
2720Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.bbc_estar_ctrl);
2730Sstevel@tonic-gate 		if (rv1 == DDI_SUCCESS)
2740Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.rio_mode_auxio);
2750Sstevel@tonic-gate 		if (rv2 == DDI_SUCCESS)
2760Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.gpio_bank_select);
2770Sstevel@tonic-gate 		if (rv3 == DDI_SUCCESS)
2780Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.gpio_data_ports);
2790Sstevel@tonic-gate 		return (DDI_FAILURE);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Ppm uses GPIO bits in Bank 0.  Make sure Bank 0 is selected.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	data8 = SIO_CONFIG2_INDEX;
2860Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
2870Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_index, data8);
2880Sstevel@tonic-gate 	data8 = XCPPM_GET8(unitp->hndls.gpio_bank_select,
2890Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_data);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	data8 &= 0x7f;	/* Set Bit7 to zero */
2920Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
2930Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_data, data8);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	return (DDI_SUCCESS);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static int
xcppm_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3000Sstevel@tonic-gate xcppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate #ifdef DEBUG
3030Sstevel@tonic-gate 	char *str = "xcppm_attach";
3040Sstevel@tonic-gate #endif
3050Sstevel@tonic-gate 	xcppm_unit_t *unitp;
3060Sstevel@tonic-gate 	ppm_domain_t **dompp;
3070Sstevel@tonic-gate 	int retval;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	DPRINTF(D_ATTACH, ("%s: attach cmd %d\n", str, cmd));
3100Sstevel@tonic-gate 	retval = DDI_SUCCESS;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	switch (cmd) {
3130Sstevel@tonic-gate 	case DDI_ATTACH:
3140Sstevel@tonic-gate 		if (ppm_inst != -1) {
3150Sstevel@tonic-gate 			DPRINTF(D_ERROR,
3160Sstevel@tonic-gate 			    ("%s: instance already attached\n", str));
3170Sstevel@tonic-gate 			return (DDI_FAILURE);
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 		ppm_inst = ddi_get_instance(dip);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		/*
3220Sstevel@tonic-gate 		 * Allocate and initialize soft state structure
3230Sstevel@tonic-gate 		 */
3240Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(ppm_statep, ppm_inst) != 0)
3250Sstevel@tonic-gate 			return (DDI_FAILURE);
3260Sstevel@tonic-gate 		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3270Sstevel@tonic-gate 		mutex_init(&unitp->unit_lock, NULL, MUTEX_DRIVER, NULL);
3280Sstevel@tonic-gate 		mutex_init(&unitp->creator_lock, NULL, MUTEX_DRIVER, NULL);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "ppm", S_IFCHR,
3310Sstevel@tonic-gate 		    ppm_inst, "ddi_ppm", 0) == DDI_FAILURE) {
3320Sstevel@tonic-gate 			ddi_soft_state_free(ppm_statep, ppm_inst);
3330Sstevel@tonic-gate 			DPRINTF(D_ERROR,
334*11311SSurya.Prakki@Sun.COM 			    ("%s: Can't create minor for 0x%p\n", str,
335*11311SSurya.Prakki@Sun.COM 			    (void *)dip));
3360Sstevel@tonic-gate 			return (DDI_FAILURE);
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 		ddi_report_dev(dip);
3390Sstevel@tonic-gate 		unitp->dip = dip;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		if (retval = ppm_create_db(dip))
3420Sstevel@tonic-gate 			return (retval);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		/*
3450Sstevel@tonic-gate 		 * Map all of the registers under the ppm node.
3460Sstevel@tonic-gate 		 */
3470Sstevel@tonic-gate 		if (xcppm_map_all_regs(dip) != DDI_SUCCESS)
3480Sstevel@tonic-gate 			return (DDI_FAILURE);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		if ((retval =
3510Sstevel@tonic-gate 		    pm_register_ppm(ppm_claim_dev, dip)) != DDI_SUCCESS) {
3520Sstevel@tonic-gate 			DPRINTF(D_ERROR,
3530Sstevel@tonic-gate 			    ("%s: can't register ppm handler\n", str));
3540Sstevel@tonic-gate 			return (retval);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		for (dompp = ppm_domains; *dompp; dompp++)
3580Sstevel@tonic-gate 			mutex_init(&(*dompp)->lock, NULL, MUTEX_DRIVER, NULL);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		break;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	case DDI_RESUME:
3630Sstevel@tonic-gate 		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3640Sstevel@tonic-gate 		mutex_enter(&unitp->unit_lock);
3650Sstevel@tonic-gate 		unitp->state &= ~XCPPM_ST_SUSPENDED;
3660Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	default:
3700Sstevel@tonic-gate 		cmn_err(CE_CONT, "xcppm_attach: unknown "
371*11311SSurya.Prakki@Sun.COM 		    "attach command %d, dip 0x%p\n", cmd, (void *)dip);
3720Sstevel@tonic-gate 		retval = DDI_FAILURE;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	return (retval);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * set the front panel LED:
3810Sstevel@tonic-gate  * PPM_LEDON turns it on, PPM_LEDOFF turns it off.
3820Sstevel@tonic-gate  * for GPIO register: 0x0 means led-on, 0x2 means led-off.
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate static void
xcppm_set_led(int action)3850Sstevel@tonic-gate xcppm_set_led(int action)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	xcppm_unit_t *unitp;
3880Sstevel@tonic-gate 	uint8_t	reg;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	ASSERT(action == PPM_LEDON || action == PPM_LEDOFF);
3910Sstevel@tonic-gate 	DPRINTF(D_LED, ("xcppm_set_led: Turn LED %s\n",
3920Sstevel@tonic-gate 	    (action == PPM_LEDON) ? "on" : "off"));
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3950Sstevel@tonic-gate 	reg = XCPPM_GET8(unitp->hndls.gpio_data_ports,
3960Sstevel@tonic-gate 	    unitp->regs.gpio_port1_data);
3970Sstevel@tonic-gate 	if (action == PPM_LEDON)
3980Sstevel@tonic-gate 		reg &= ~LED;
3990Sstevel@tonic-gate 	else
4000Sstevel@tonic-gate 		reg |= LED;
4010Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
4020Sstevel@tonic-gate 	    unitp->regs.gpio_port1_data, reg);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static void
xcppm_blink_led(void * action)4070Sstevel@tonic-gate xcppm_blink_led(void *action)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4100Sstevel@tonic-gate 	int new_action;
4110Sstevel@tonic-gate 	clock_t intvl;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4140Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
4150Sstevel@tonic-gate 	if (unitp->led_tid == 0) {
4160Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
4170Sstevel@tonic-gate 		return;
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
420796Smathue 	if ((int)(uintptr_t)action == PPM_LEDON) {
4210Sstevel@tonic-gate 		new_action = PPM_LEDOFF;
4220Sstevel@tonic-gate 		intvl = PPM_LEDOFF_INTERVAL;
4230Sstevel@tonic-gate 	} else {
424796Smathue 		ASSERT((int)(uintptr_t)action == PPM_LEDOFF);
4250Sstevel@tonic-gate 		new_action = PPM_LEDON;
4260Sstevel@tonic-gate 		intvl = PPM_LEDON_INTERVAL;
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	xcppm_set_led(new_action);
430796Smathue 	unitp->led_tid = timeout(xcppm_blink_led, (void *)(uintptr_t)new_action,
431796Smathue 	    intvl);
4320Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static void
xcppm_freeze_led(void * action)4370Sstevel@tonic-gate xcppm_freeze_led(void *action)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4400Sstevel@tonic-gate 	timeout_id_t tid;
4410Sstevel@tonic-gate 
442796Smathue 	DPRINTF(D_LOWEST, ("xcppm_freeze_led: action %d\n",
443796Smathue 	    (int)(uintptr_t)action));
4440Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4450Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
4460Sstevel@tonic-gate 	tid = unitp->led_tid;
4470Sstevel@tonic-gate 	unitp->led_tid = 0;
4480Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
449*11311SSurya.Prakki@Sun.COM 	(void) untimeout(tid);
4500Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
451796Smathue 	xcppm_set_led((int)(uintptr_t)action);
4520Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /* ARGSUSED */
4570Sstevel@tonic-gate static int
xcppm_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4580Sstevel@tonic-gate xcppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4630Sstevel@tonic-gate 	DPRINTF(D_DETACH, ("xcppm_detach: cmd %d\n", cmd));
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	switch (cmd) {
4660Sstevel@tonic-gate 	case DDI_DETACH:
4670Sstevel@tonic-gate 		return (DDI_FAILURE);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	case DDI_SUSPEND:
4700Sstevel@tonic-gate 		mutex_enter(&unitp->unit_lock);
4710Sstevel@tonic-gate 		unitp->state |= XCPPM_ST_SUSPENDED;
4720Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * Suspend requires that timeout callouts to be canceled.
4760Sstevel@tonic-gate 		 * Turning off the LED blinking will cancel the timeout.
4770Sstevel@tonic-gate 		 */
4780Sstevel@tonic-gate 		xcppm_freeze_led((void *)PPM_LEDON);
4790Sstevel@tonic-gate 		return (DDI_SUCCESS);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	default:
4820Sstevel@tonic-gate 		return (DDI_FAILURE);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate  * Device we claimed has detached.  We must get rid of
4890Sstevel@tonic-gate  * our state which was used to track this device.
4900Sstevel@tonic-gate  */
4910Sstevel@tonic-gate static void
xcppm_detach_ctlop(dev_info_t * dip,power_req_t * reqp)4920Sstevel@tonic-gate xcppm_detach_ctlop(dev_info_t *dip, power_req_t *reqp)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate 	ppm_dev_t *ppmd;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
4970Sstevel@tonic-gate 	if (ppmd == NULL || reqp->req.ppm_config_req.result != DDI_SUCCESS)
4980Sstevel@tonic-gate 		return;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	ppm_rem_dev(dip);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * The system is being resumed from a cpr suspend operation and this
5060Sstevel@tonic-gate  * device's attach entry will be called shortly.  The driver will set
5070Sstevel@tonic-gate  * the device's power to a conventional starting value, and we need to
5080Sstevel@tonic-gate  * stay in sync and set our private copy to the same value.
5090Sstevel@tonic-gate  */
5100Sstevel@tonic-gate /* ARGSUSED */
5110Sstevel@tonic-gate static void
xcppm_resume_ctlop(dev_info_t * dip,power_req_t * reqp)5120Sstevel@tonic-gate xcppm_resume_ctlop(dev_info_t *dip, power_req_t *reqp)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	ppm_domain_t *domp;
5150Sstevel@tonic-gate 	ppm_dev_t *ppmd;
5160Sstevel@tonic-gate 	int powered;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
5190Sstevel@tonic-gate 	if (ppmd == NULL)
5200Sstevel@tonic-gate 		return;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Maintain correct powered count for domain which cares
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	powered = 0;
5260Sstevel@tonic-gate 	domp = ppmd->domp;
5270Sstevel@tonic-gate 	mutex_enter(&domp->lock);
5280Sstevel@tonic-gate 	if (domp == &xcppm_fet) {
5290Sstevel@tonic-gate 		for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
5300Sstevel@tonic-gate 			if (ppmd->dip == dip && ppmd->level)
5310Sstevel@tonic-gate 				powered++;
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		/*
5350Sstevel@tonic-gate 		 * If this device was powered off when the system was
5360Sstevel@tonic-gate 		 * suspended, this resume acts like a power-on transition,
5370Sstevel@tonic-gate 		 * so we adjust the count.
5380Sstevel@tonic-gate 		 */
5390Sstevel@tonic-gate 		if (powered == 0)
5400Sstevel@tonic-gate 			domp->pwr_cnt++;
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
5440Sstevel@tonic-gate 		if (ppmd->dip == dip)
5450Sstevel@tonic-gate 			ppmd->level = ppmd->rplvl = PM_LEVEL_UNKNOWN;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	mutex_exit(&domp->lock);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * Change the power level for a component of a device.  If the change
5530Sstevel@tonic-gate  * arg is true, we call the framework to actually change the device's
5540Sstevel@tonic-gate  * power; otherwise, we just update our own copy of the power level.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static int
xcppm_set_level(ppm_dev_t * ppmd,int cmpt,int level,boolean_t change)5570Sstevel@tonic-gate xcppm_set_level(ppm_dev_t *ppmd, int cmpt, int level, boolean_t change)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate #ifdef DEBUG
5600Sstevel@tonic-gate 	char *str = "xcppm_set_level";
5610Sstevel@tonic-gate #endif
5620Sstevel@tonic-gate 	int ret;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	ret = DDI_SUCCESS;
5650Sstevel@tonic-gate 	if (change)
5660Sstevel@tonic-gate 		ret = pm_power(ppmd->dip, cmpt, level);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	DPRINTF(D_SETLVL, ("%s: \"%s\" change=%d, old %d, new %d, ret %d\n",
5690Sstevel@tonic-gate 	    str, ppmd->path, change, ppmd->level, level, ret));
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
5720Sstevel@tonic-gate 		ppmd->level = level;
5730Sstevel@tonic-gate 		ppmd->rplvl = PM_LEVEL_UNKNOWN;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	return (ret);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate static int
xcppm_change_power_level(ppm_dev_t * ppmd,int cmpt,int level)5810Sstevel@tonic-gate xcppm_change_power_level(ppm_dev_t *ppmd, int cmpt, int level)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	return (xcppm_set_level(ppmd, cmpt, level, B_TRUE));
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate static int
xcppm_record_level_change(ppm_dev_t * ppmd,int cmpt,int level)5880Sstevel@tonic-gate xcppm_record_level_change(ppm_dev_t *ppmd, int cmpt, int level)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	return (xcppm_set_level(ppmd, cmpt, level, B_FALSE));
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static uint8_t
xcppm_gpio_port2(int action,uint8_t pos)5950Sstevel@tonic-gate xcppm_gpio_port2(int action, uint8_t pos)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate #ifdef DEBUG
5980Sstevel@tonic-gate 	char *str = "xcppm_gpio_port2";
5990Sstevel@tonic-gate #endif
6000Sstevel@tonic-gate 	xcppm_unit_t *unitp;
6010Sstevel@tonic-gate 	uint8_t data8, buf8;
6020Sstevel@tonic-gate 	uint8_t	ret;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
6050Sstevel@tonic-gate 	mutex_enter(&unitp->gpio_lock);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	data8 = buf8 = XCPPM_GET8(unitp->hndls.gpio_data_ports,
6080Sstevel@tonic-gate 	    unitp->regs.gpio_port2_data);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	switch (action) {
6110Sstevel@tonic-gate 	case XCPPM_GETBIT:
6120Sstevel@tonic-gate 		ret = data8 & pos;
6130Sstevel@tonic-gate 		DPRINTF(D_GPIO, ("%s: READ: GPIO Bank2 value 0x%x\n",
6140Sstevel@tonic-gate 		    str, buf8));
6150Sstevel@tonic-gate 		break;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	case XCPPM_SETBIT:
6180Sstevel@tonic-gate 	case XCPPM_CLRBIT:
6190Sstevel@tonic-gate 		if (action == XCPPM_SETBIT)
6200Sstevel@tonic-gate 			data8 |= pos;
6210Sstevel@tonic-gate 		else
6220Sstevel@tonic-gate 			data8 &= ~pos;
6230Sstevel@tonic-gate 		XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
6240Sstevel@tonic-gate 		    unitp->regs.gpio_port2_data, data8);
6250Sstevel@tonic-gate 		ret = data8 & pos;
6260Sstevel@tonic-gate 		DPRINTF(D_GPIO, ("%s: %s: GPIO Bank2 "
6270Sstevel@tonic-gate 		    "bit 0x%x changed from 0x%x to 0x%x\n",
6280Sstevel@tonic-gate 		    str, (action == XCPPM_SETBIT) ? "UP" : "DOWN",
6290Sstevel@tonic-gate 		    pos, buf8, data8));
6300Sstevel@tonic-gate 		break;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	default:
6330Sstevel@tonic-gate 		cmn_err(CE_PANIC, "xcalppm: unrecognized register "
6340Sstevel@tonic-gate 		    "IO command %d\n", action);
6350Sstevel@tonic-gate 		break;
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 	mutex_exit(&unitp->gpio_lock);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	return (ret);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * Raise the power level of a subrange of cpus.  Used when cpu driver
6450Sstevel@tonic-gate  * failed an attempt to lower the power of a cpu (probably because
6460Sstevel@tonic-gate  * it got busy).  Need to revert the ones we already changed.
6470Sstevel@tonic-gate  *
6480Sstevel@tonic-gate  * ecpup = the ppm_dev_t for the cpu which failed to lower power
6490Sstevel@tonic-gate  * level = power level to reset prior cpus to
6500Sstevel@tonic-gate  */
6510Sstevel@tonic-gate static void
xcppm_revert_cpu_power(ppm_dev_t * ecpup,int level)6520Sstevel@tonic-gate xcppm_revert_cpu_power(ppm_dev_t *ecpup, int level)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	ppm_dev_t *cpup;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	for (cpup = xcppm_cpu.devlist; cpup != ecpup; cpup = cpup->next) {
6570Sstevel@tonic-gate 		DPRINTF(D_CPU, ("xrcp: \"%s\", revert to level %d\n",
6580Sstevel@tonic-gate 		    cpup->path, level));
6590Sstevel@tonic-gate 		(void) xcppm_change_power_level(cpup, 0, level);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate  * Switch the DC/DC converter.  Clearing the GPIO bit in SuperI/O puts
6650Sstevel@tonic-gate  * the converter in low power mode and setting the bit puts it back in
6660Sstevel@tonic-gate  * normal mode.
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate static void
xcppm_switch_dcdc_converter(int action)6690Sstevel@tonic-gate xcppm_switch_dcdc_converter(int action)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	int tries = XCPPM_VCL_TRIES;
6720Sstevel@tonic-gate 	uint_t spl;
6730Sstevel@tonic-gate 	uint64_t stick_begin, stick_end;
6740Sstevel@tonic-gate 	uint64_t tick_begin, tick_end;
6750Sstevel@tonic-gate 	uint64_t cur_speed_ratio, full_speed_ratio;
6760Sstevel@tonic-gate 	static int xcppm_dcdc_lpm;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	switch (action) {
6790Sstevel@tonic-gate 	case XCPPM_SETBIT:
6800Sstevel@tonic-gate 		if (xcppm_dcdc_lpm) {
6810Sstevel@tonic-gate 			DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
6820Sstevel@tonic-gate 			    "switch to normal power mode.\n"));
6830Sstevel@tonic-gate 			(void) xcppm_gpio_port2(action, HIGHPWR);
6840Sstevel@tonic-gate 			xcppm_dcdc_lpm = 0;
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		break;
6870Sstevel@tonic-gate 	case XCPPM_CLRBIT:
6880Sstevel@tonic-gate 		/*
6890Sstevel@tonic-gate 		 * In some fast CPU configurations, DC/DC converter was
6900Sstevel@tonic-gate 		 * put in low power mode before CPUs made the transition
6910Sstevel@tonic-gate 		 * to 1/32 of clock speed.  In those cases, system was
6920Sstevel@tonic-gate 		 * shut down by hardware for protection.  To resolve that
6930Sstevel@tonic-gate 		 * problem, we make sure CPUs have made the clock transition
6940Sstevel@tonic-gate 		 * before the DC/DC converter has been put to low power mode.
6950Sstevel@tonic-gate 		 */
6960Sstevel@tonic-gate 		ASSERT(xcppm_dcdc_lpm == 0);
6970Sstevel@tonic-gate 		kpreempt_disable();
6980Sstevel@tonic-gate 		full_speed_ratio = cpunodes[CPU->cpu_id].clock_freq /
6990Sstevel@tonic-gate 		    sys_tick_freq;
7000Sstevel@tonic-gate 		while (tries) {
7010Sstevel@tonic-gate 			spl = ddi_enter_critical();
7020Sstevel@tonic-gate 			tick_begin = gettick_counter();
7030Sstevel@tonic-gate 			stick_timestamp((int64_t *)&stick_begin);
7040Sstevel@tonic-gate 			ddi_exit_critical(spl);
7050Sstevel@tonic-gate 			drv_usecwait(XCPPM_VCL_DELAY);
7060Sstevel@tonic-gate 			spl = ddi_enter_critical();
7070Sstevel@tonic-gate 			tick_end = gettick_counter();
7080Sstevel@tonic-gate 			stick_timestamp((int64_t *)&stick_end);
7090Sstevel@tonic-gate 			ddi_exit_critical(spl);
7100Sstevel@tonic-gate 			cur_speed_ratio = (tick_end - tick_begin) /
7110Sstevel@tonic-gate 			    (stick_end - stick_begin);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * tick/stick at current speed should at most be
7150Sstevel@tonic-gate 			 * equal to full-speed tick/stick, adjusted with
7160Sstevel@tonic-gate 			 * full/lowest clock speed ratio.  If not, speed
7170Sstevel@tonic-gate 			 * transition has not happened yet.
7180Sstevel@tonic-gate 			 */
7190Sstevel@tonic-gate 			if (cur_speed_ratio <= ((full_speed_ratio /
7200Sstevel@tonic-gate 			    XCPPM_VCL_DIVISOR) + 1)) {
7210Sstevel@tonic-gate 				DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
7220Sstevel@tonic-gate 				    "switch to low power mode.\n"));
7230Sstevel@tonic-gate 				(void) xcppm_gpio_port2(action, HIGHPWR);
7240Sstevel@tonic-gate 				xcppm_dcdc_lpm = 1;
7250Sstevel@tonic-gate 				break;
7260Sstevel@tonic-gate 			}
7270Sstevel@tonic-gate 			DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: CPU "
7280Sstevel@tonic-gate 			    "has not made transition to lowest speed yet "
7290Sstevel@tonic-gate 			    "(%d)\n", tries));
7300Sstevel@tonic-gate 			tries--;
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		kpreempt_enable();
7330Sstevel@tonic-gate 		break;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate static void
xcppm_rio_mode(xcppm_unit_t * unitp,int mode)7380Sstevel@tonic-gate xcppm_rio_mode(xcppm_unit_t *unitp, int mode)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	uint32_t data32, buf32;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	mutex_enter(&unitp->gpio_lock);
7430Sstevel@tonic-gate 	data32 = buf32 = XCPPM_GET32(unitp->hndls.rio_mode_auxio,
7440Sstevel@tonic-gate 	    unitp->regs.rio_mode_auxio);
7450Sstevel@tonic-gate 	if (mode == XCPPM_SETBIT)
7460Sstevel@tonic-gate 		data32 |= RIO_BBC_ESTAR_MODE;
7470Sstevel@tonic-gate 	else
7480Sstevel@tonic-gate 		data32 &= ~RIO_BBC_ESTAR_MODE;
7490Sstevel@tonic-gate 	XCPPM_SETGET32(unitp->hndls.rio_mode_auxio,
7500Sstevel@tonic-gate 	    unitp->regs.rio_mode_auxio, data32);
7510Sstevel@tonic-gate 	mutex_exit(&unitp->gpio_lock);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	DPRINTF(D_CPU, ("xcppm_rio_mode: %s: change from 0x%x to 0x%x\n",
7540Sstevel@tonic-gate 	    (mode == XCPPM_SETBIT) ? "DOWN" : "UP", buf32, data32));
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate  * change the power level of all cpus to the arg value;
7600Sstevel@tonic-gate  * the caller needs to ensure that a legal transition is requested.
7610Sstevel@tonic-gate  */
7620Sstevel@tonic-gate static int
xcppm_change_cpu_power(int newlevel)7630Sstevel@tonic-gate xcppm_change_cpu_power(int newlevel)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate #ifdef DEBUG
7660Sstevel@tonic-gate 	char *str = "xcppm_ccp";
7670Sstevel@tonic-gate #endif
7680Sstevel@tonic-gate 	int index, level, oldlevel;
7690Sstevel@tonic-gate 	int lowest, highest;
7700Sstevel@tonic-gate 	int undo_flag, ret;
7710Sstevel@tonic-gate 	int speedup, incr;
7720Sstevel@tonic-gate 	uint32_t data32;
7730Sstevel@tonic-gate 	uint16_t data16;
7740Sstevel@tonic-gate 	xcppm_unit_t *unitp;
7750Sstevel@tonic-gate 	ppm_dev_t *cpup;
7760Sstevel@tonic-gate 	dev_info_t *dip;
7770Sstevel@tonic-gate 	char *chstr;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
7800Sstevel@tonic-gate 	ASSERT(unitp);
7810Sstevel@tonic-gate 	cpup = xcppm_cpu.devlist;
7820Sstevel@tonic-gate 	lowest = cpup->lowest;
7830Sstevel@tonic-gate 	highest = cpup->highest;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * not all cpus may have transitioned to a known level by this time
7870Sstevel@tonic-gate 	 */
7880Sstevel@tonic-gate 	oldlevel = (cpup->level == PM_LEVEL_UNKNOWN) ? highest : cpup->level;
7890Sstevel@tonic-gate 	dip = cpup->dip;
7900Sstevel@tonic-gate 	ASSERT(dip);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	DPRINTF(D_CPU, ("%s: old %d, new %d, highest %d, lowest %d\n",
7930Sstevel@tonic-gate 	    str, oldlevel, newlevel, highest, lowest));
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (newlevel > oldlevel) {
7960Sstevel@tonic-gate 		chstr = "UP";
7970Sstevel@tonic-gate 		speedup = 1;
7980Sstevel@tonic-gate 		incr = 1;
7990Sstevel@tonic-gate 	} else if (newlevel < oldlevel) {
8000Sstevel@tonic-gate 		chstr = "DOWN";
8010Sstevel@tonic-gate 		speedup = 0;
8020Sstevel@tonic-gate 		incr = -1;
8030Sstevel@tonic-gate 	} else
8040Sstevel@tonic-gate 		return (DDI_SUCCESS);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	undo_flag = 0;
8070Sstevel@tonic-gate 	if (speedup) {
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * If coming up from lowest power level, set the E*
8100Sstevel@tonic-gate 		 * mode bit in GPIO to make power supply efficient
8110Sstevel@tonic-gate 		 * at normal power.
8120Sstevel@tonic-gate 		 */
8130Sstevel@tonic-gate 		if (oldlevel == cpup->lowest) {
8140Sstevel@tonic-gate 			xcppm_switch_dcdc_converter(XCPPM_SETBIT);
8150Sstevel@tonic-gate 			undo_flag = 1;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 	} else {
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * set BBC Estar mode bit in RIO AUXIO register
8200Sstevel@tonic-gate 		 */
8210Sstevel@tonic-gate 		if (oldlevel == highest) {
8220Sstevel@tonic-gate 			xcppm_rio_mode(unitp, XCPPM_SETBIT);
8230Sstevel@tonic-gate 			undo_flag = 1;
8240Sstevel@tonic-gate 		}
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * this loop will execute 1x or 2x depending on
8290Sstevel@tonic-gate 	 * number of times we need to change clock rates
8300Sstevel@tonic-gate 	 */
8310Sstevel@tonic-gate 	for (level = oldlevel+incr; level != newlevel+incr; level += incr) {
8320Sstevel@tonic-gate 		for (cpup = xcppm_cpu.devlist; cpup; cpup = cpup->next) {
8330Sstevel@tonic-gate 			if (cpup->level == level)
8340Sstevel@tonic-gate 				continue;
8350Sstevel@tonic-gate 			ret = xcppm_change_power_level(cpup, 0, level);
8360Sstevel@tonic-gate 			DPRINTF(D_CPU, ("%s: \"%s\", %s to level %d, ret %d\n",
8370Sstevel@tonic-gate 			    str, cpup->path, chstr, cpup->level, ret));
8380Sstevel@tonic-gate 			if (ret == DDI_SUCCESS)
8390Sstevel@tonic-gate 				continue;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 			/*
8420Sstevel@tonic-gate 			 * if the driver was unable to lower cpu speed,
8430Sstevel@tonic-gate 			 * the cpu probably got busy; set the previous
8440Sstevel@tonic-gate 			 * cpus back to the original level
8450Sstevel@tonic-gate 			 */
8460Sstevel@tonic-gate 			if (speedup == 0)
8470Sstevel@tonic-gate 				xcppm_revert_cpu_power(cpup, level + 1);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 			if (undo_flag) {
8500Sstevel@tonic-gate 				if (speedup)
8510Sstevel@tonic-gate 					xcppm_switch_dcdc_converter(
8520Sstevel@tonic-gate 					    XCPPM_CLRBIT);
8530Sstevel@tonic-gate 				else
8540Sstevel@tonic-gate 					xcppm_rio_mode(unitp, XCPPM_CLRBIT);
8550Sstevel@tonic-gate 			}
8560Sstevel@tonic-gate 			return (ret);
8570Sstevel@tonic-gate 		}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		index = level - 1;
8600Sstevel@tonic-gate 		spm_change_schizo_speed(index);
8610Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: safari config reg changed\n", str));
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		/*
8640Sstevel@tonic-gate 		 * set the delay times for changing to this rate
8650Sstevel@tonic-gate 		 */
8660Sstevel@tonic-gate 		data32 = XCPPM_BBC_DELAY(index);
8670Sstevel@tonic-gate 		XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
8680Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_assert_change, data32);
8690Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote E* Assert Change Time "
8700Sstevel@tonic-gate 		    "(t1) = 0x%x\n", str, chstr, data32));
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		data32 = XCPPM_BBC_DELAY(index);
8730Sstevel@tonic-gate 		XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
8740Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_pll_settle, data32);
8750Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote E* PLL Settle Time "
8760Sstevel@tonic-gate 		    "(t4) = 0x%x\n", str, chstr, data32));
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		data16 = bbc_estar_control_masks[index];
8790Sstevel@tonic-gate 		XCPPM_SETGET16(unitp->hndls.bbc_estar_ctrl,
8800Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_estar_ctrl, data16);
8810Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote BCC E* Control = 0x%x\n",
8820Sstevel@tonic-gate 		    str, chstr, data16));
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/*
8860Sstevel@tonic-gate 	 * clear CPU Estar Mode bit in the gpio register
8870Sstevel@tonic-gate 	 */
8880Sstevel@tonic-gate 	if (speedup) {
8890Sstevel@tonic-gate 		if (newlevel == highest)
8900Sstevel@tonic-gate 			xcppm_rio_mode(unitp, XCPPM_CLRBIT);
8910Sstevel@tonic-gate 	} else {
8920Sstevel@tonic-gate 		if (newlevel == lowest)
8930Sstevel@tonic-gate 			xcppm_switch_dcdc_converter(XCPPM_CLRBIT);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	return (DDI_SUCCESS);
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate /*
9010Sstevel@tonic-gate  * Process a request to change the power level of a cpu.  If all cpus
9020Sstevel@tonic-gate  * don't want to be at the same power yet, or if we are currently
9030Sstevel@tonic-gate  * refusing slowdown requests due to thermal stress, just cache the
9040Sstevel@tonic-gate  * request.  Otherwise, make the change for all cpus.
9050Sstevel@tonic-gate  */
9060Sstevel@tonic-gate /* ARGSUSED */
9070Sstevel@tonic-gate static int
xcppm_manage_cpus(dev_info_t * dip,power_req_t * reqp,int * result)9080Sstevel@tonic-gate xcppm_manage_cpus(dev_info_t *dip, power_req_t *reqp, int *result)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate #ifdef DEBUG
9110Sstevel@tonic-gate 	char *str = "xcppm_manage_cpus";
9120Sstevel@tonic-gate #endif
9130Sstevel@tonic-gate 	int old, new, ret, kmflag;
9140Sstevel@tonic-gate 	ppm_dev_t *ppmd;
9150Sstevel@tonic-gate 	pm_ppm_devlist_t *devlist = NULL, *p;
9160Sstevel@tonic-gate 	int		do_rescan = 0;
9170Sstevel@tonic-gate 	dev_info_t	*rescan_dip;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	*result = DDI_SUCCESS;
9200Sstevel@tonic-gate 	switch (reqp->request_type) {
9210Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
9220Sstevel@tonic-gate 		break;
9230Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
9240Sstevel@tonic-gate 		/* cpu driver can`t change cpu power level by itself */
9250Sstevel@tonic-gate 	default:
9260Sstevel@tonic-gate 		return (DDI_FAILURE);
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
9300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
9310Sstevel@tonic-gate 	old = reqp->req.ppm_set_power_req.old_level;
9320Sstevel@tonic-gate 	new = reqp->req.ppm_set_power_req.new_level;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * At power on, the cpus are at full speed.  There is no hardware
9360Sstevel@tonic-gate 	 * transition needed for going from unknown to full.  However, the
9370Sstevel@tonic-gate 	 * state of the pm framework and cpu driver needs to be adjusted.
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 	if (ppmd->level == PM_LEVEL_UNKNOWN && new == ppmd->highest) {
9400Sstevel@tonic-gate 		*result = ret = xcppm_change_power_level(ppmd, 0, new);
9410Sstevel@tonic-gate 		if (ret != DDI_SUCCESS) {
9420Sstevel@tonic-gate 			DPRINTF(D_CPU, ("%s: Failed to change "
9430Sstevel@tonic-gate 			    "power level to %d\n", str, new));
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 		return (ret);
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (new == ppmd->level) {
9490Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: already at power level %d\n", str, new));
9500Sstevel@tonic-gate 		return (DDI_SUCCESS);
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	ppmd->rplvl = new;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * A request from lower to higher level transition is granted and
9570Sstevel@tonic-gate 	 * made effective on both cpus. For more than two cpu platform model,
9580Sstevel@tonic-gate 	 * the following code needs to be modified to remember the rest of
9590Sstevel@tonic-gate 	 * the unsoliciting cpus to be rescan'ed.
9600Sstevel@tonic-gate 	 * A request from higher to lower must be agreed by all cpus.
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 	for (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
9630Sstevel@tonic-gate 		if (ppmd->rplvl == new)
9640Sstevel@tonic-gate 			continue;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 		if (new < old) {
9670Sstevel@tonic-gate 			DPRINTF(D_SOME, ("%s: not all cpus want to go down to "
9680Sstevel@tonic-gate 			    "level %d yet\n", str, new));
9690Sstevel@tonic-gate 			return (DDI_SUCCESS);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		/*
9730Sstevel@tonic-gate 		 * If a single cpu requests power up, honor the request
9740Sstevel@tonic-gate 		 * by powering up both cpus.
9750Sstevel@tonic-gate 		 */
9760Sstevel@tonic-gate 		if (new > old) {
9770Sstevel@tonic-gate 			DPRINTF(D_SOME, ("%s: powering up device(%s@%s, %p) "
9780Sstevel@tonic-gate 			    "because of request from dip(%s@%s, %p), "
9790Sstevel@tonic-gate 			    "need pm_rescan\n", str, PM_NAME(ppmd->dip),
9800Sstevel@tonic-gate 			    PM_ADDR(ppmd->dip), (void *)ppmd->dip,
9810Sstevel@tonic-gate 			    PM_NAME(dip), PM_ADDR(dip), (void *)dip))
9820Sstevel@tonic-gate 			do_rescan++;
9830Sstevel@tonic-gate 			rescan_dip = ppmd->dip;
9840Sstevel@tonic-gate 			break;
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	ret = xcppm_change_cpu_power(new);
9890Sstevel@tonic-gate 	*result = ret;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
9920Sstevel@tonic-gate 		if (reqp->req.ppm_set_power_req.canblock == PM_CANBLOCK_BLOCK)
9930Sstevel@tonic-gate 			kmflag = KM_SLEEP;
9940Sstevel@tonic-gate 		else
9950Sstevel@tonic-gate 			kmflag = KM_NOSLEEP;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		for (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
9980Sstevel@tonic-gate 			if (ppmd->dip == dip)
9990Sstevel@tonic-gate 				continue;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 			if ((p = kmem_zalloc(sizeof (pm_ppm_devlist_t),
10020Sstevel@tonic-gate 			    kmflag)) == NULL) {
10030Sstevel@tonic-gate 				break;
10040Sstevel@tonic-gate 			}
10050Sstevel@tonic-gate 			p->ppd_who = ppmd->dip;
10060Sstevel@tonic-gate 			p->ppd_cmpt = ppmd->cmpt;
10070Sstevel@tonic-gate 			p->ppd_old_level = old;
10080Sstevel@tonic-gate 			p->ppd_new_level = new;
10090Sstevel@tonic-gate 			p->ppd_next = devlist;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			devlist = p;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 		reqp->req.ppm_set_power_req.cookie = (void *) devlist;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 		if (do_rescan > 0)
10160Sstevel@tonic-gate 			pm_rescan(rescan_dip);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	return (ret);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate /*
10240Sstevel@tonic-gate  * If powering off and all devices in this domain will now be off,
10250Sstevel@tonic-gate  * shut off common power.  If powering up and no devices up yet,
10260Sstevel@tonic-gate  * turn on common power.  Always make the requested power level
10270Sstevel@tonic-gate  * change for the target device.
10280Sstevel@tonic-gate  */
10290Sstevel@tonic-gate static int
xcppm_manage_fet(dev_info_t * dip,power_req_t * reqp,int * result)10300Sstevel@tonic-gate xcppm_manage_fet(dev_info_t *dip, power_req_t *reqp, int *result)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate #ifdef DEBUG
10330Sstevel@tonic-gate 	char *str = "xcppm_manage_fet";
10340Sstevel@tonic-gate #endif
10350Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
10360Sstevel@tonic-gate 	int new, old, cmpt, incr = 0;
10370Sstevel@tonic-gate 	ppm_dev_t *ppmd;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
10400Sstevel@tonic-gate 	DPRINTF(D_FET, ("%s: \"%s\", req %s\n", str,
10410Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	*result = DDI_SUCCESS;	/* change later for failures */
10440Sstevel@tonic-gate 	switch (reqp->request_type) {
10450Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
10460Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
10470Sstevel@tonic-gate 		old = reqp->req.ppm_set_power_req.old_level;
10480Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
10490Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
10500Sstevel@tonic-gate 		break;
10510Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
10520Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
10530Sstevel@tonic-gate 		old = reqp->req.ppm_notify_level_req.old_level;
10540Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
10550Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
10560Sstevel@tonic-gate 		break;
10570Sstevel@tonic-gate 	default:
10580Sstevel@tonic-gate 		return (*result = DDI_FAILURE);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/* This is common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
10630Sstevel@tonic-gate 	DPRINTF(D_FET, ("%s: \"%s\", old %d, new %d\n",
10640Sstevel@tonic-gate 	    str, ppmd->path, old, new));
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	ASSERT(old == ppmd->level);
10670Sstevel@tonic-gate 	if (new == ppmd->level)
10680Sstevel@tonic-gate 		return (DDI_SUCCESS);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	PPM_LOCK_DOMAIN(ppmd->domp);
10710Sstevel@tonic-gate 	/*
10720Sstevel@tonic-gate 	 * Devices in this domain are known to have 0 (off) as their
10730Sstevel@tonic-gate 	 * lowest power level.  We use this fact to simplify the logic.
10740Sstevel@tonic-gate 	 */
10750Sstevel@tonic-gate 	if (new > 0) {
10760Sstevel@tonic-gate 		if (ppmd->domp->pwr_cnt == 0)
10770Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_SETBIT, DRVON);
10780Sstevel@tonic-gate 		if (old == 0) {
10790Sstevel@tonic-gate 			ppmd->domp->pwr_cnt++;
10800Sstevel@tonic-gate 			incr = 1;
10810Sstevel@tonic-gate 			DPRINTF(D_FET, ("%s: UP cnt = %d\n",
10820Sstevel@tonic-gate 			    str, ppmd->domp->pwr_cnt));
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	PPM_UNLOCK_DOMAIN(ppmd->domp);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	ASSERT(ppmd->domp->pwr_cnt > 0);
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
10910Sstevel@tonic-gate 		DPRINTF(D_FET, ("%s: \"%s\" power change failed \n",
10920Sstevel@tonic-gate 		    str, ppmd->path));
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	PPM_LOCK_DOMAIN(ppmd->domp);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * Decr the power count in two cases:
10990Sstevel@tonic-gate 	 *
11000Sstevel@tonic-gate 	 *   1) request was to power device down and was successful
11010Sstevel@tonic-gate 	 *   2) request was to power up (we pre-incremented count), but failed.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	if ((*result == DDI_SUCCESS && ppmd->level == 0) ||
11040Sstevel@tonic-gate 	    (*result != DDI_SUCCESS && incr)) {
11050Sstevel@tonic-gate 		ASSERT(ppmd->domp->pwr_cnt > 0);
11060Sstevel@tonic-gate 		ppmd->domp->pwr_cnt--;
11070Sstevel@tonic-gate 		DPRINTF(D_FET, ("%s: DN cnt = %d\n", str, ppmd->domp->pwr_cnt));
11080Sstevel@tonic-gate 		if (ppmd->domp->pwr_cnt == 0)
11090Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, DRVON);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	PPM_UNLOCK_DOMAIN(ppmd->domp);
11130Sstevel@tonic-gate 	ASSERT(ppmd->domp->pwr_cnt >= 0);
11140Sstevel@tonic-gate 	return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate  * Since UPA64S relies on PCI B staying at nominal 33MHz in order to
11200Sstevel@tonic-gate  * have its interrupt pulse function properly, we ensure
11210Sstevel@tonic-gate  * - Lowering PCI B only if UPA64S is at low power, otherwise defer
11220Sstevel@tonic-gate  *   the action until UPA64S goes down; hence right after UPA64S goes
11230Sstevel@tonic-gate  *   down, perform the deferred action for PCI B;
11240Sstevel@tonic-gate  * - Always raise PCI B power prior to raising UPA64S power.
11250Sstevel@tonic-gate  *
11260Sstevel@tonic-gate  * Both UPA64S and PCI B devices are considered each other's dependency
11270Sstevel@tonic-gate  * device whenever actual power transition is handled (PMR_PPM_SET_POWER).
11280Sstevel@tonic-gate  */
11290Sstevel@tonic-gate static int
xcppm_manage_pciupa(dev_info_t * dip,power_req_t * reqp,int * result)11300Sstevel@tonic-gate xcppm_manage_pciupa(dev_info_t *dip, power_req_t *reqp, int *result)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate #ifdef DEBUG
11330Sstevel@tonic-gate 	char *str = "xcppm_manage_pciupa";
11340Sstevel@tonic-gate #endif
11350Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
11360Sstevel@tonic-gate 	uint_t flags = 0, co_flags = 0;
11370Sstevel@tonic-gate 	ppm_dev_t *ppmd, *codev;
11380Sstevel@tonic-gate 	int new, cmpt, retval;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
11410Sstevel@tonic-gate 	DPRINTF(D_PCIUPA, ("%s: \"%s\", req %s\n", str,
11420Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	*result = DDI_SUCCESS;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	switch (reqp->request_type) {
11470Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
11480Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
11490Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
11500Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
11510Sstevel@tonic-gate 		break;
11520Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
11530Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
11540Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
11550Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
11560Sstevel@tonic-gate 		break;
11570Sstevel@tonic-gate 	default:
11580Sstevel@tonic-gate 		*result = DDI_FAILURE;
11590Sstevel@tonic-gate 		return (DDI_FAILURE);
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
11630Sstevel@tonic-gate 	ASSERT(ppmd);	/* since it should be locked already */
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (new == ppmd->level)
11660Sstevel@tonic-gate 		return (DDI_SUCCESS);
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	DPRINTF(D_PCIUPA, ("%s: \"%s\", levels: current %d, new %d\n",
11690Sstevel@tonic-gate 	    str, ppmd->path, ppmd->level, new));
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * find power-wise co-related device
11730Sstevel@tonic-gate 	 */
11740Sstevel@tonic-gate 	flags =  ppmd->flags;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate #ifdef DEBUG
11770Sstevel@tonic-gate 	if (flags & ~(XCPPMF_PCIB|XCPPMF_UPA))
1178796Smathue 		DPRINTF(D_ERROR, ("%s: invalid ppmd->flags value 0x%x\n", str,
11790Sstevel@tonic-gate 		    ppmd->flags));
11800Sstevel@tonic-gate #endif
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (flags == XCPPMF_UPA)
11830Sstevel@tonic-gate 		co_flags = XCPPMF_PCIB;
11840Sstevel@tonic-gate 	else if (flags == XCPPMF_PCIB)
11850Sstevel@tonic-gate 		co_flags = XCPPMF_UPA;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	for (codev = ppmd->domp->devlist; codev; codev = codev->next)
11880Sstevel@tonic-gate 		if ((codev->cmpt == 0) && (codev->flags == co_flags))
11890Sstevel@tonic-gate 			break;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (new > ppmd->level) {
11920Sstevel@tonic-gate 		/*
11930Sstevel@tonic-gate 		 * Raise power level -
11940Sstevel@tonic-gate 		 * pre-raising: upa ensure pci is powered up.
11950Sstevel@tonic-gate 		 */
11960Sstevel@tonic-gate 		if ((flags == XCPPMF_UPA) && codev &&
11970Sstevel@tonic-gate 		    (codev->level != codev->highest)) {
11980Sstevel@tonic-gate 			if ((retval = xcppm_change_power_level(codev,
11990Sstevel@tonic-gate 			    0, codev->highest)) != DDI_SUCCESS &&
12000Sstevel@tonic-gate 			    codev->level != codev->highest) {
12010Sstevel@tonic-gate 				*result = retval;
12020Sstevel@tonic-gate 				return (DDI_FAILURE);
12030Sstevel@tonic-gate 			}
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 		if ((retval = (*pwr_func)(ppmd, 0, new)) != DDI_SUCCESS) {
12060Sstevel@tonic-gate 			*result = retval;
12070Sstevel@tonic-gate 			return (DDI_FAILURE);
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 	} else if (new < ppmd->level) {
12100Sstevel@tonic-gate 		/*
12110Sstevel@tonic-gate 		 * Lower power level
12120Sstevel@tonic-gate 		 *
12130Sstevel@tonic-gate 		 * once upa is attached, pci checks upa level:
12140Sstevel@tonic-gate 		 * if upa is at high level, defer the request and return.
12150Sstevel@tonic-gate 		 * otherwise, set power level then check and lower pci level.
12160Sstevel@tonic-gate 		 */
12170Sstevel@tonic-gate 		if ((flags == XCPPMF_PCIB) && codev &&
12180Sstevel@tonic-gate 		    (codev->level != codev->lowest)) {
12190Sstevel@tonic-gate 			ppmd->rplvl = new;
12200Sstevel@tonic-gate 			return (DDI_SUCCESS);
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 		if ((retval = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
12230Sstevel@tonic-gate 		    ppmd->level != new) {
12240Sstevel@tonic-gate 			*result = retval;
12250Sstevel@tonic-gate 			return (DDI_FAILURE);
12260Sstevel@tonic-gate 		}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 		if (flags == XCPPMF_UPA) {
12290Sstevel@tonic-gate 			if (codev && (codev->rplvl != PM_LEVEL_UNKNOWN) &&
12300Sstevel@tonic-gate 			    (codev->rplvl < codev->level)) {
12310Sstevel@tonic-gate 				DPRINTF(D_PCIUPA, ("%s: codev \"%s\" "
12320Sstevel@tonic-gate 				    "rplvl %d level %d\n", str, codev->path,
12330Sstevel@tonic-gate 				    codev->rplvl, codev->level));
12340Sstevel@tonic-gate 				if ((retval = xcppm_change_power_level(
12350Sstevel@tonic-gate 				    codev, 0, codev->rplvl)) != DDI_SUCCESS) {
12360Sstevel@tonic-gate 					*result = retval;
12370Sstevel@tonic-gate 					return (DDI_FAILURE);
12380Sstevel@tonic-gate 				}
12390Sstevel@tonic-gate 			}
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	return (DDI_SUCCESS);
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate  * When all of the children of the 1394 nexus are idle, a call will be
12490Sstevel@tonic-gate  * made to the nexus driver's own power entry point to lower power.  Ppm
12500Sstevel@tonic-gate  * intercepts this and kills 1394 cable power (since the driver doesn't
12510Sstevel@tonic-gate  * have access to the required register).  Similar logic applies when
12520Sstevel@tonic-gate  * coming up from the state where all the children were off.
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate static int
xcppm_manage_1394(dev_info_t * dip,power_req_t * reqp,int * result)12550Sstevel@tonic-gate xcppm_manage_1394(dev_info_t *dip, power_req_t *reqp, int *result)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate #ifdef DEBUG
12580Sstevel@tonic-gate 	char *str = "xcppm_manage_1394";
12590Sstevel@tonic-gate #endif
12600Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
12610Sstevel@tonic-gate 	int new, old, cmpt;
12620Sstevel@tonic-gate 	ppm_dev_t *ppmd;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
12650Sstevel@tonic-gate 	DPRINTF(D_1394, ("%s: \"%s\", req %s\n", str,
12660Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	switch (reqp->request_type) {
12690Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
12700Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
12710Sstevel@tonic-gate 		old = reqp->req.ppm_set_power_req.old_level;
12720Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
12730Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
12740Sstevel@tonic-gate 		break;
12750Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
12760Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
12770Sstevel@tonic-gate 		old = reqp->req.ppm_notify_level_req.old_level;
12780Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
12790Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
12800Sstevel@tonic-gate 		break;
12810Sstevel@tonic-gate 	default:
12820Sstevel@tonic-gate 		return (*result = DDI_FAILURE);
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
12870Sstevel@tonic-gate 	DPRINTF(D_1394, ("%s: dev %s@%s, old %d new %d\n", str,
12880Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), old, new));
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	ASSERT(ppmd);	/* since it must already be locked */
12910Sstevel@tonic-gate 	ASSERT(old == ppmd->level);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	if (new == ppmd->level)
12940Sstevel@tonic-gate 		return (*result = DDI_SUCCESS);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	/* the reduce power case */
12970Sstevel@tonic-gate 	if (cmpt == 0 && new < ppmd->level) {
12980Sstevel@tonic-gate 		if ((*result =
12990Sstevel@tonic-gate 		    (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
13000Sstevel@tonic-gate 			return (DDI_FAILURE);
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 		if (new == ppmd->lowest)
13030Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
13040Sstevel@tonic-gate 		ppmd->level = new;
13050Sstevel@tonic-gate 		return (DDI_SUCCESS);
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/* the increase power case */
13090Sstevel@tonic-gate 	if (cmpt == 0 && new > ppmd->level) {
13100Sstevel@tonic-gate 		if (ppmd->level == ppmd->lowest) {
13110Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_SETBIT, CPEN);
13120Sstevel@tonic-gate 			delay(1);
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 		/*
13150Sstevel@tonic-gate 		 * Even if pwr_func fails we need to check current level again
13160Sstevel@tonic-gate 		 * because it could have been changed by an intervening
13170Sstevel@tonic-gate 		 * POWER_CHANGE_NOTIFY operation.
13180Sstevel@tonic-gate 		 */
13190Sstevel@tonic-gate 		if ((*result =
13200Sstevel@tonic-gate 		    (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
13210Sstevel@tonic-gate 		    ppmd->level == ppmd->lowest) {
13220Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
13230Sstevel@tonic-gate 		} else {
13240Sstevel@tonic-gate 			ppmd->level = new;
13250Sstevel@tonic-gate 		}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 		return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/*
13310Sstevel@tonic-gate 	 * We get here if component was non-zero.  This is not what we
13320Sstevel@tonic-gate 	 * expect.  Let the device deal with it and just pass back the
13330Sstevel@tonic-gate 	 * result.
13340Sstevel@tonic-gate 	 */
13350Sstevel@tonic-gate 	*result = xcppm_change_power_level(ppmd, cmpt, new);
13360Sstevel@tonic-gate 	return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate  * lock, unlock, or trylock for one power mutex
13420Sstevel@tonic-gate  */
13430Sstevel@tonic-gate static void
xcppm_lock_one(ppm_dev_t * ppmd,power_req_t * reqp,int * iresp)13440Sstevel@tonic-gate xcppm_lock_one(ppm_dev_t *ppmd, power_req_t *reqp, int *iresp)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	switch (reqp->request_type) {
13470Sstevel@tonic-gate 	case PMR_PPM_LOCK_POWER:
13480Sstevel@tonic-gate 		pm_lock_power_single(ppmd->dip,
13490Sstevel@tonic-gate 		    reqp->req.ppm_lock_power_req.circp);
13500Sstevel@tonic-gate 		break;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	case PMR_PPM_UNLOCK_POWER:
13530Sstevel@tonic-gate 		pm_unlock_power_single(ppmd->dip,
13540Sstevel@tonic-gate 		    reqp->req.ppm_unlock_power_req.circ);
13550Sstevel@tonic-gate 		break;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	case PMR_PPM_TRY_LOCK_POWER:
13580Sstevel@tonic-gate 		*iresp = pm_try_locking_power_single(ppmd->dip,
13590Sstevel@tonic-gate 		    reqp->req.ppm_lock_power_req.circp);
13600Sstevel@tonic-gate 		break;
13610Sstevel@tonic-gate 	}
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * lock, unlock, or trylock all devices within a domain.
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate static void
xcppm_lock_all(ppm_domain_t * domp,power_req_t * reqp,int * iresp)13690Sstevel@tonic-gate xcppm_lock_all(ppm_domain_t *domp, power_req_t *reqp, int *iresp)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	/*
13720Sstevel@tonic-gate 	 * To simplify the implementation we let all the devices
13730Sstevel@tonic-gate 	 * in the domain be represented by a single device (dip).
13740Sstevel@tonic-gate 	 * We use the first device in the domain's devlist.  This
13750Sstevel@tonic-gate 	 * is safe because we return with the domain lock held
13760Sstevel@tonic-gate 	 * which prevents the list from changing.
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	if (reqp->request_type == PMR_PPM_LOCK_POWER) {
13790Sstevel@tonic-gate 		if (!MUTEX_HELD(&domp->lock))
13800Sstevel@tonic-gate 			mutex_enter(&domp->lock);
13810Sstevel@tonic-gate 		domp->refcnt++;
13820Sstevel@tonic-gate 		ASSERT(domp->devlist != NULL);
13830Sstevel@tonic-gate 		pm_lock_power_single(domp->devlist->dip,
13840Sstevel@tonic-gate 		    reqp->req.ppm_lock_power_req.circp);
13850Sstevel@tonic-gate 		/* domain lock remains held */
13860Sstevel@tonic-gate 		return;
13870Sstevel@tonic-gate 	} else if (reqp->request_type == PMR_PPM_UNLOCK_POWER) {
13880Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&domp->lock));
13890Sstevel@tonic-gate 		ASSERT(domp->devlist != NULL);
13900Sstevel@tonic-gate 		pm_unlock_power_single(domp->devlist->dip,
13910Sstevel@tonic-gate 		    reqp->req.ppm_unlock_power_req.circ);
13920Sstevel@tonic-gate 		if (--domp->refcnt == 0)
13930Sstevel@tonic-gate 			mutex_exit(&domp->lock);
13940Sstevel@tonic-gate 		return;
13950Sstevel@tonic-gate 	}
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	ASSERT(reqp->request_type == PMR_PPM_TRY_LOCK_POWER);
13980Sstevel@tonic-gate 	if (!MUTEX_HELD(&domp->lock))
13990Sstevel@tonic-gate 		if (!mutex_tryenter(&domp->lock)) {
14000Sstevel@tonic-gate 			*iresp = 0;
14010Sstevel@tonic-gate 			return;
14020Sstevel@tonic-gate 		}
14030Sstevel@tonic-gate 	*iresp = pm_try_locking_power_single(domp->devlist->dip,
14040Sstevel@tonic-gate 	    reqp->req.ppm_lock_power_req.circp);
14050Sstevel@tonic-gate 	if (*iresp)
14060Sstevel@tonic-gate 		domp->refcnt++;
14070Sstevel@tonic-gate 	else
14080Sstevel@tonic-gate 		mutex_exit(&domp->lock);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate /*
14130Sstevel@tonic-gate  * The pm framework calls us here to manage power for a device.
14140Sstevel@tonic-gate  * We maintain state which tells us whether we need to turn off/on
14150Sstevel@tonic-gate  * system board power components based on the status of all the devices
14160Sstevel@tonic-gate  * sharing a component.
14170Sstevel@tonic-gate  *
14180Sstevel@tonic-gate  */
14190Sstevel@tonic-gate /* ARGSUSED */
14200Sstevel@tonic-gate static int
xcppm_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)14210Sstevel@tonic-gate xcppm_ctlops(dev_info_t *dip, dev_info_t *rdip,
14220Sstevel@tonic-gate     ddi_ctl_enum_t ctlop, void *arg, void *result)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 	power_req_t *reqp = arg;
14250Sstevel@tonic-gate 	xcppm_unit_t *unitp;
14260Sstevel@tonic-gate 	ppm_domain_t *domp;
14270Sstevel@tonic-gate 	ppm_dev_t *ppmd;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate #ifdef DEBUG
14300Sstevel@tonic-gate 	char path[MAXPATHLEN], *ctlstr, *str = "xcppm_ctlops";
14310Sstevel@tonic-gate 	uint_t mask = ppm_debug & (D_CTLOPS1 | D_CTLOPS2);
14320Sstevel@tonic-gate 	if (mask && (ctlstr = ppm_get_ctlstr(reqp->request_type, mask))) {
14330Sstevel@tonic-gate 		prom_printf("%s: \"%s\", %s\n", str,
14340Sstevel@tonic-gate 		    ddi_pathname(rdip, path), ctlstr);
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate #endif
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (ctlop != DDI_CTLOPS_POWER)
14390Sstevel@tonic-gate 		return (DDI_FAILURE);
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	switch (reqp->request_type) {
14420Sstevel@tonic-gate 	case PMR_PPM_UNMANAGE:
14430Sstevel@tonic-gate 	case PMR_PPM_PRE_PROBE:
14440Sstevel@tonic-gate 	case PMR_PPM_POST_PROBE:
14450Sstevel@tonic-gate 	case PMR_PPM_PRE_ATTACH:
14460Sstevel@tonic-gate 	case PMR_PPM_PRE_DETACH:
14470Sstevel@tonic-gate 		return (DDI_SUCCESS);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	/*
14500Sstevel@tonic-gate 	 * There is no hardware configuration required to be done on this
14510Sstevel@tonic-gate 	 * platform prior to installing drivers.
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	case PMR_PPM_INIT_CHILD:
14540Sstevel@tonic-gate 	case PMR_PPM_UNINIT_CHILD:
14550Sstevel@tonic-gate 		return (DDI_SUCCESS);
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	case PMR_PPM_ALL_LOWEST:
14580Sstevel@tonic-gate 		DPRINTF(D_LOWEST, ("%s: all devices at lowest power = %d\n",
14590Sstevel@tonic-gate 		    str, reqp->req.ppm_all_lowest_req.mode));
14600Sstevel@tonic-gate 		if (reqp->req.ppm_all_lowest_req.mode == PM_ALL_LOWEST) {
14610Sstevel@tonic-gate 			unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
14620Sstevel@tonic-gate 			mutex_enter(&unitp->unit_lock);
14630Sstevel@tonic-gate 			if (unitp->state & XCPPM_ST_SUSPENDED) {
14640Sstevel@tonic-gate 				mutex_exit(&unitp->unit_lock);
14650Sstevel@tonic-gate 				return (DDI_SUCCESS);
14660Sstevel@tonic-gate 			}
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 			xcppm_set_led(PPM_LEDON);
14690Sstevel@tonic-gate 			unitp->led_tid = timeout(xcppm_blink_led,
14700Sstevel@tonic-gate 			    (void *)PPM_LEDON, PPM_LEDON_INTERVAL);
14710Sstevel@tonic-gate 			mutex_exit(&unitp->unit_lock);
14720Sstevel@tonic-gate 			DPRINTF(D_LOWEST, ("%s: LED blink started\n", str));
14730Sstevel@tonic-gate 		} else {
14740Sstevel@tonic-gate 			xcppm_freeze_led((void *)PPM_LEDON);
14750Sstevel@tonic-gate 			DPRINTF(D_LOWEST, ("%s: LED freeze ON\n", str));
14760Sstevel@tonic-gate 		}
14770Sstevel@tonic-gate 		return (DDI_SUCCESS);
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	case PMR_PPM_POST_ATTACH:
14800Sstevel@tonic-gate 		/*
14810Sstevel@tonic-gate 		 * After a successful attach, if we haven't already created
14820Sstevel@tonic-gate 		 * our private data structure for this device, ppm_get_dev()
14830Sstevel@tonic-gate 		 * will force it to be created.
14840Sstevel@tonic-gate 		 */
14850Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
14860Sstevel@tonic-gate 		if (reqp->req.ppm_config_req.result != DDI_SUCCESS) {
14870Sstevel@tonic-gate 			if (ppmd)
14880Sstevel@tonic-gate 				ppm_rem_dev(rdip);
14890Sstevel@tonic-gate 		} else if (!ppmd) {
14900Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
14910Sstevel@tonic-gate 			ASSERT(domp);
14920Sstevel@tonic-gate 			(void) ppm_get_dev(rdip, domp);
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 		return (DDI_SUCCESS);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	case PMR_PPM_POST_DETACH:
14970Sstevel@tonic-gate 		xcppm_detach_ctlop(rdip, reqp);
14980Sstevel@tonic-gate 		*(int *)result = DDI_SUCCESS;
14990Sstevel@tonic-gate 		return (DDI_SUCCESS);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	case PMR_PPM_PRE_RESUME:
15020Sstevel@tonic-gate 		xcppm_resume_ctlop(rdip, reqp);
15030Sstevel@tonic-gate 		return (DDI_SUCCESS);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	case PMR_PPM_UNLOCK_POWER:
15060Sstevel@tonic-gate 	case PMR_PPM_TRY_LOCK_POWER:
15070Sstevel@tonic-gate 	case PMR_PPM_LOCK_POWER:
15080Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15090Sstevel@tonic-gate 		if (ppmd)
15100Sstevel@tonic-gate 			domp = ppmd->domp;
15110Sstevel@tonic-gate 		else if (reqp->request_type != PMR_PPM_UNLOCK_POWER) {
15120Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15130Sstevel@tonic-gate 			ASSERT(domp);
15140Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15150Sstevel@tonic-gate 		}
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 		ASSERT(domp->dflags == PPMD_LOCK_ALL ||
15180Sstevel@tonic-gate 		    domp->dflags == PPMD_LOCK_ONE);
15190Sstevel@tonic-gate 		DPRINTF(D_LOCKS, ("xcppm_lock_%s: \"%s\", %s\n",
15200Sstevel@tonic-gate 		    (domp->dflags == PPMD_LOCK_ALL) ? "all" : "one",
15210Sstevel@tonic-gate 		    ppmd->path, ppm_get_ctlstr(reqp->request_type, D_LOCKS)));
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		if (domp->dflags == PPMD_LOCK_ALL)
15240Sstevel@tonic-gate 			xcppm_lock_all(domp, reqp, result);
15250Sstevel@tonic-gate 		else
15260Sstevel@tonic-gate 			xcppm_lock_one(ppmd, reqp, result);
15270Sstevel@tonic-gate 		return (DDI_SUCCESS);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	case PMR_PPM_POWER_LOCK_OWNER:
15300Sstevel@tonic-gate 		ASSERT(reqp->req.ppm_power_lock_owner_req.who == rdip);
15310Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15320Sstevel@tonic-gate 		if (ppmd)
15330Sstevel@tonic-gate 			domp = ppmd->domp;
15340Sstevel@tonic-gate 		else {
15350Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15360Sstevel@tonic-gate 			ASSERT(domp);
15370Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15380Sstevel@tonic-gate 		}
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 		/*
15410Sstevel@tonic-gate 		 * In case of LOCK_ALL, effective owner of the power lock
15420Sstevel@tonic-gate 		 * is the owner of the domain lock. otherwise, it is the owner
15430Sstevel@tonic-gate 		 * of the power lock.
15440Sstevel@tonic-gate 		 */
15450Sstevel@tonic-gate 		if (domp->dflags & PPMD_LOCK_ALL)
15460Sstevel@tonic-gate 			reqp->req.ppm_power_lock_owner_req.owner =
15470Sstevel@tonic-gate 			    mutex_owner(&domp->lock);
15480Sstevel@tonic-gate 		else {
15490Sstevel@tonic-gate 			reqp->req.ppm_power_lock_owner_req.owner =
15500Sstevel@tonic-gate 			    DEVI(rdip)->devi_busy_thread;
15510Sstevel@tonic-gate 		}
15520Sstevel@tonic-gate 		return (DDI_SUCCESS);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	default:
15550Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15560Sstevel@tonic-gate 		if (ppmd == NULL) {
15570Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15580Sstevel@tonic-gate 			ASSERT(domp);
15590Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate #ifdef DEBUG
15630Sstevel@tonic-gate 		if ((reqp->request_type == PMR_PPM_SET_POWER) &&
15640Sstevel@tonic-gate 		    (ppm_debug & D_SETPWR)) {
15650Sstevel@tonic-gate 			prom_printf("%s: \"%s\", PMR_PPM_SET_POWER\n",
15660Sstevel@tonic-gate 			    str, ppmd->path);
15670Sstevel@tonic-gate 		}
15680Sstevel@tonic-gate #endif
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 		if (ppmd->domp == &xcppm_cpu)
15710Sstevel@tonic-gate 			return (xcppm_manage_cpus(rdip, reqp, result));
15720Sstevel@tonic-gate 		else if (ppmd->domp == &xcppm_fet)
15730Sstevel@tonic-gate 			return (xcppm_manage_fet(rdip, reqp, result));
15740Sstevel@tonic-gate 		else if (ppmd->domp == &xcppm_upa)
15750Sstevel@tonic-gate 			return (xcppm_manage_pciupa(rdip, reqp, result));
15760Sstevel@tonic-gate 		else {
15770Sstevel@tonic-gate 			ASSERT(ppmd->domp == &xcppm_1394);
15780Sstevel@tonic-gate 			return (xcppm_manage_1394(rdip, reqp, result));
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate /*
15850Sstevel@tonic-gate  * Initialize our private version of real power level
15860Sstevel@tonic-gate  * as well as lowest and highest levels the device supports;
15870Sstevel@tonic-gate  * see ppmf and ppm_add_dev
15880Sstevel@tonic-gate  */
15890Sstevel@tonic-gate static void
xcppm_dev_init(ppm_dev_t * ppmd)15900Sstevel@tonic-gate xcppm_dev_init(ppm_dev_t *ppmd)
15910Sstevel@tonic-gate {
15920Sstevel@tonic-gate 	struct pm_component *dcomps;
15930Sstevel@tonic-gate 	struct pm_comp *pm_comp;
15940Sstevel@tonic-gate 	dev_info_t *dip;
15950Sstevel@tonic-gate 	int maxi;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
15980Sstevel@tonic-gate 	ppmd->level = PM_LEVEL_UNKNOWN;
15990Sstevel@tonic-gate 	ppmd->rplvl = PM_LEVEL_UNKNOWN;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	dip = ppmd->dip;
16020Sstevel@tonic-gate 	/*
16030Sstevel@tonic-gate 	 * ppm exists to handle power-manageable devices which require
16040Sstevel@tonic-gate 	 * special handling on the current platform.  However, a
16050Sstevel@tonic-gate 	 * driver for such a device may choose not to support power
16060Sstevel@tonic-gate 	 * management on a particular load/attach.  In this case we
16070Sstevel@tonic-gate 	 * we create a structure to represent a single-component device
16080Sstevel@tonic-gate 	 * for which "level" = PM_LEVEL_UNKNOWN and "lowest" = 0
16090Sstevel@tonic-gate 	 * are effectively constant.
16100Sstevel@tonic-gate 	 */
16110Sstevel@tonic-gate 	if (PM_GET_PM_INFO(dip)) {
16120Sstevel@tonic-gate 		dcomps = DEVI(dip)->devi_pm_components;
16130Sstevel@tonic-gate 		pm_comp = &dcomps[ppmd->cmpt].pmc_comp;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 		ppmd->lowest = pm_comp->pmc_lvals[0];
16160Sstevel@tonic-gate 		ASSERT(ppmd->lowest >= 0);
16170Sstevel@tonic-gate 		maxi = pm_comp->pmc_numlevels - 1;
16180Sstevel@tonic-gate 		ppmd->highest = pm_comp->pmc_lvals[maxi];
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/*
16220Sstevel@tonic-gate 	 * add any domain-specific initialization here
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	if (ppmd->domp == &xcppm_fet) {
16250Sstevel@tonic-gate 		/*
16260Sstevel@tonic-gate 		 * when a new device is added to domain_powefet
16270Sstevel@tonic-gate 		 * it is counted here as being powered up.
16280Sstevel@tonic-gate 		 */
16290Sstevel@tonic-gate 		ppmd->domp->pwr_cnt++;
16300Sstevel@tonic-gate 		DPRINTF(D_FET, ("xcppm_dev_init: UP cnt = %d\n",
16310Sstevel@tonic-gate 		    ppmd->domp->pwr_cnt));
16320Sstevel@tonic-gate 	} else if (ppmd->domp == &xcppm_upa) {
16330Sstevel@tonic-gate 		/*
16340Sstevel@tonic-gate 		 * There may be a better way to determine the device type
16350Sstevel@tonic-gate 		 * instead of comparing to hard coded string names.
16360Sstevel@tonic-gate 		 */
16370Sstevel@tonic-gate 		if (strstr(ppmd->path, "pci@8,700000"))
16380Sstevel@tonic-gate 			ppmd->flags = XCPPMF_PCIB;
16390Sstevel@tonic-gate 		else if (strstr(ppmd->path, "upa@8,480000"))
16400Sstevel@tonic-gate 			ppmd->flags = XCPPMF_UPA;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate  * see ppmf and ppm_rem_dev
16470Sstevel@tonic-gate  */
16480Sstevel@tonic-gate static void
xcppm_dev_fini(ppm_dev_t * ppmd)16490Sstevel@tonic-gate xcppm_dev_fini(ppm_dev_t *ppmd)
16500Sstevel@tonic-gate {
16510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
16520Sstevel@tonic-gate 	if (ppmd->domp == &xcppm_fet) {
16530Sstevel@tonic-gate 		if (ppmd->level != ppmd->lowest) {
16540Sstevel@tonic-gate 			ppmd->domp->pwr_cnt--;
16550Sstevel@tonic-gate 			DPRINTF(D_FET, ("xcppm_dev_fini: DN cnt = %d\n",
16560Sstevel@tonic-gate 			    ppmd->domp->pwr_cnt));
16570Sstevel@tonic-gate 		};
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate /*
16630Sstevel@tonic-gate  * see ppmf and ppm_ioctl, PPMIOCSET
16640Sstevel@tonic-gate  */
16650Sstevel@tonic-gate static void
xcppm_iocset(uint8_t value)16660Sstevel@tonic-gate xcppm_iocset(uint8_t value)
16670Sstevel@tonic-gate {
16680Sstevel@tonic-gate 	int action;
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	if (value == PPM_IDEV_POWER_ON)
16710Sstevel@tonic-gate 		action = XCPPM_SETBIT;
16720Sstevel@tonic-gate 	else if (value == PPM_IDEV_POWER_OFF)
16730Sstevel@tonic-gate 		action = XCPPM_CLRBIT;
16740Sstevel@tonic-gate 	(void) xcppm_gpio_port2(action, DRVON);
16750Sstevel@tonic-gate }
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate /*
16790Sstevel@tonic-gate  * see ppmf and ppm_ioctl, PPMIOCGET
16800Sstevel@tonic-gate  */
16810Sstevel@tonic-gate static uint8_t
xcppm_iocget(void)16820Sstevel@tonic-gate xcppm_iocget(void)
16830Sstevel@tonic-gate {
16840Sstevel@tonic-gate 	uint8_t bit;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	bit = xcppm_gpio_port2(XCPPM_GETBIT, DRVON);
16870Sstevel@tonic-gate 	return ((bit == DRVON) ? PPM_IDEV_POWER_ON : PPM_IDEV_POWER_OFF);
16880Sstevel@tonic-gate }
1689