xref: /onnv-gate/usr/src/uts/sun4u/ngdr/io/dr_cpu.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
51772Sjl139090  * Common Development and Distribution License (the "License").
61772Sjl139090  * 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  */
21*11311SSurya.Prakki@Sun.COM 
220Sstevel@tonic-gate /*
23*11311SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * CPU support routines for DR
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/note.h>
320Sstevel@tonic-gate #include <sys/debug.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/dditypes.h>
370Sstevel@tonic-gate #include <sys/devops.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
390Sstevel@tonic-gate #include <sys/poll.h>
400Sstevel@tonic-gate #include <sys/conf.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/sunndi.h>
440Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/processor.h>
480Sstevel@tonic-gate #include <sys/cpuvar.h>
490Sstevel@tonic-gate #include <sys/mem_config.h>
500Sstevel@tonic-gate #include <sys/promif.h>
510Sstevel@tonic-gate #include <sys/x_call.h>
520Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
530Sstevel@tonic-gate #include <sys/membar.h>
540Sstevel@tonic-gate #include <sys/stack.h>
550Sstevel@tonic-gate #include <sys/sysmacros.h>
560Sstevel@tonic-gate #include <sys/machsystm.h>
570Sstevel@tonic-gate #include <sys/spitregs.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/archsystm.h>
600Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
610Sstevel@tonic-gate #include <sys/pte.h>
620Sstevel@tonic-gate #include <sys/mmu.h>
630Sstevel@tonic-gate #include <sys/x_call.h>
640Sstevel@tonic-gate #include <sys/cpu_module.h>
651772Sjl139090 #include <sys/cpu_impl.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/autoconf.h>
680Sstevel@tonic-gate #include <sys/cmn_err.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <sys/dr.h>
710Sstevel@tonic-gate #include <sys/dr_util.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifdef _STARFIRE
740Sstevel@tonic-gate #include <sys/starfire.h>
750Sstevel@tonic-gate extern struct cpu	*SIGBCPU;
760Sstevel@tonic-gate #else
770Sstevel@tonic-gate /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
780Sstevel@tonic-gate static char *dr_ie_fmt = "dr_cpu.c %d";
790Sstevel@tonic-gate #endif /* _STARFIRE */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate int
dr_cpu_unit_is_sane(dr_board_t * bp,dr_cpu_unit_t * cp)820Sstevel@tonic-gate dr_cpu_unit_is_sane(dr_board_t *bp, dr_cpu_unit_t *cp)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate #ifdef DEBUG
850Sstevel@tonic-gate 	processorid_t	cpuid;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * cpuid and unit number should never be different
890Sstevel@tonic-gate 	 * than they were at discovery/connect time
900Sstevel@tonic-gate 	 */
910Sstevel@tonic-gate 	ASSERT(drmach_cpu_get_id(cp->sbc_cm.sbdev_id, &cpuid) == 0);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	ASSERT(cp->sbc_cm.sbdev_bp == bp);
940Sstevel@tonic-gate 	ASSERT(cp->sbc_cm.sbdev_type == SBD_COMP_CPU);
950Sstevel@tonic-gate 	ASSERT(cp->sbc_cpu_id == cpuid);
960Sstevel@tonic-gate #else
970Sstevel@tonic-gate 	_NOTE(ARGUNUSED(bp))
980Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cp))
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	return (1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static int
dr_errno2ecode(int error)1050Sstevel@tonic-gate dr_errno2ecode(int error)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	int	rv;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	switch (error) {
1100Sstevel@tonic-gate 	case EBUSY:
1110Sstevel@tonic-gate 		rv = ESBD_BUSY;
1120Sstevel@tonic-gate 		break;
1130Sstevel@tonic-gate 	case EINVAL:
1140Sstevel@tonic-gate 		rv = ESBD_INVAL;
1150Sstevel@tonic-gate 		break;
1160Sstevel@tonic-gate 	case EALREADY:
1170Sstevel@tonic-gate 		rv = ESBD_ALREADY;
1180Sstevel@tonic-gate 		break;
1190Sstevel@tonic-gate 	case ENODEV:
1200Sstevel@tonic-gate 		rv = ESBD_NODEV;
1210Sstevel@tonic-gate 		break;
1220Sstevel@tonic-gate 	case ENOMEM:
1230Sstevel@tonic-gate 		rv = ESBD_NOMEM;
1240Sstevel@tonic-gate 		break;
1250Sstevel@tonic-gate 	default:
1260Sstevel@tonic-gate 		rv = ESBD_INVAL;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	return (rv);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static void
dr_cpu_set_prop(dr_cpu_unit_t * cp)1330Sstevel@tonic-gate dr_cpu_set_prop(dr_cpu_unit_t *cp)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	sbd_error_t	*err;
1360Sstevel@tonic-gate 	dev_info_t	*dip;
1370Sstevel@tonic-gate 	uint64_t	clock_freq;
1380Sstevel@tonic-gate 	int		ecache_size = 0;
1390Sstevel@tonic-gate 	char		*cache_str = NULL;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	err = drmach_get_dip(cp->sbc_cm.sbdev_id, &dip);
1420Sstevel@tonic-gate 	if (err) {
1430Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
1440Sstevel@tonic-gate 		return;
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (dip == NULL) {
1480Sstevel@tonic-gate #ifndef _STARFIRE
1490Sstevel@tonic-gate 		/*
1500Sstevel@tonic-gate 		 * Do not report an error on Starfire since
1510Sstevel@tonic-gate 		 * the dip will not be created until after
1520Sstevel@tonic-gate 		 * the CPU has been configured.
1530Sstevel@tonic-gate 		 */
1540Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&cp->sbc_cm);
1550Sstevel@tonic-gate #endif /* !_STARFIRE */
1560Sstevel@tonic-gate 		return;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* read in the CPU speed */
1601772Sjl139090 
1611772Sjl139090 	/*
1621772Sjl139090 	 * If the property is not found in the CPU node, it has to be
1631772Sjl139090 	 * kept in the core or cmp node so we just keep looking.
1641772Sjl139090 	 */
1655037Sjl139090 	clock_freq = (unsigned int)ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
1665037Sjl139090 	    "clock-frequency", 0);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	ASSERT(clock_freq != 0);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * The ecache property string is not the same
1720Sstevel@tonic-gate 	 * for all CPU implementations.
1730Sstevel@tonic-gate 	 */
1741772Sjl139090 
1750Sstevel@tonic-gate 	switch (cp->sbc_cpu_impl) {
1760Sstevel@tonic-gate 	case BLACKBIRD_IMPL:
1770Sstevel@tonic-gate 	case CHEETAH_IMPL:
1780Sstevel@tonic-gate 	case CHEETAH_PLUS_IMPL:
1790Sstevel@tonic-gate 		cache_str = "ecache-size";
1800Sstevel@tonic-gate 		break;
1810Sstevel@tonic-gate 	case JAGUAR_IMPL:
1821772Sjl139090 	case OLYMPUS_C_IMPL:
1835037Sjl139090 	case JUPITER_IMPL:
1840Sstevel@tonic-gate 		cache_str = "l2-cache-size";
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 	case PANTHER_IMPL:
1870Sstevel@tonic-gate 		cache_str = "l3-cache-size";
1880Sstevel@tonic-gate 		break;
1890Sstevel@tonic-gate 	default:
1900Sstevel@tonic-gate 		cmn_err(CE_WARN, "Unknown cpu implementation=0x%x",
1910Sstevel@tonic-gate 		    cp->sbc_cpu_impl);
1920Sstevel@tonic-gate 		ASSERT(0);
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (cache_str != NULL) {
1970Sstevel@tonic-gate 		/* read in the ecache size */
1981772Sjl139090 		/*
1991772Sjl139090 		 * If the property is not found in the CPU node,
2001772Sjl139090 		 * it has to be kept in the core or cmp node so
2011772Sjl139090 		 * we just keep looking.
2021772Sjl139090 		 */
2031772Sjl139090 
2045037Sjl139090 		ecache_size = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
2055037Sjl139090 		    cache_str, 0);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	ASSERT(ecache_size != 0);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* convert to the proper units */
2110Sstevel@tonic-gate 	cp->sbc_speed = (clock_freq + 500000) / 1000000;
2120Sstevel@tonic-gate 	cp->sbc_ecache = ecache_size / (1024 * 1024);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate void
dr_init_cpu_unit(dr_cpu_unit_t * cp)2160Sstevel@tonic-gate dr_init_cpu_unit(dr_cpu_unit_t *cp)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	sbd_error_t	*err;
2190Sstevel@tonic-gate 	dr_state_t	new_state;
2200Sstevel@tonic-gate 	int		cpuid;
2210Sstevel@tonic-gate 	int		impl;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (DR_DEV_IS_ATTACHED(&cp->sbc_cm)) {
2240Sstevel@tonic-gate 		new_state = DR_STATE_CONFIGURED;
2250Sstevel@tonic-gate 		cp->sbc_cm.sbdev_cond = SBD_COND_OK;
2260Sstevel@tonic-gate 	} else if (DR_DEV_IS_PRESENT(&cp->sbc_cm)) {
2270Sstevel@tonic-gate 		new_state = DR_STATE_CONNECTED;
2280Sstevel@tonic-gate 		cp->sbc_cm.sbdev_cond = SBD_COND_OK;
2290Sstevel@tonic-gate 	} else {
2300Sstevel@tonic-gate 		new_state = DR_STATE_EMPTY;
2310Sstevel@tonic-gate 		cp->sbc_cm.sbdev_cond = SBD_COND_UNKNOWN;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (DR_DEV_IS_PRESENT(&cp->sbc_cm)) {
2350Sstevel@tonic-gate 		err = drmach_cpu_get_id(cp->sbc_cm.sbdev_id, &cpuid);
2360Sstevel@tonic-gate 		if (err) {
2370Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
2380Sstevel@tonic-gate 			new_state = DR_STATE_FATAL;
2390Sstevel@tonic-gate 			goto done;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		err = drmach_cpu_get_impl(cp->sbc_cm.sbdev_id, &impl);
2430Sstevel@tonic-gate 		if (err) {
2440Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
2450Sstevel@tonic-gate 			new_state = DR_STATE_FATAL;
2460Sstevel@tonic-gate 			goto done;
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 	} else {
2490Sstevel@tonic-gate 		cp->sbc_cpu_id = -1;
2500Sstevel@tonic-gate 		cp->sbc_cpu_impl = -1;
2510Sstevel@tonic-gate 		goto done;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	cp->sbc_cpu_id = cpuid;
2550Sstevel@tonic-gate 	cp->sbc_cpu_impl = impl;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* if true at init time, it must always be true */
2580Sstevel@tonic-gate 	ASSERT(dr_cpu_unit_is_sane(cp->sbc_cm.sbdev_bp, cp));
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2610Sstevel@tonic-gate 	if ((cpuid >= 0) && cpu[cpuid])
2620Sstevel@tonic-gate 		cp->sbc_cpu_flags = cpu[cpuid]->cpu_flags;
2630Sstevel@tonic-gate 	else
2640Sstevel@tonic-gate 		cp->sbc_cpu_flags = P_OFFLINE | P_POWEROFF;
2650Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	dr_cpu_set_prop(cp);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate done:
2700Sstevel@tonic-gate 	/* delay transition until fully initialized */
2710Sstevel@tonic-gate 	dr_device_transition(&cp->sbc_cm, new_state);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate int
dr_pre_attach_cpu(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)2750Sstevel@tonic-gate dr_pre_attach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	int		i;
2780Sstevel@tonic-gate 	int		curr_cpu;
2790Sstevel@tonic-gate 	int		next_cpu;
2800Sstevel@tonic-gate 	static fn_t	f = "dr_pre_attach_cpu";
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	for (next_cpu = 0, i = 0; i < devnum; i++) {
2850Sstevel@tonic-gate 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * Print a console message for each attachment
2910Sstevel@tonic-gate 		 * point. For CMP devices, this means that only
2920Sstevel@tonic-gate 		 * one message should be printed, no matter how
2930Sstevel@tonic-gate 		 * many cores are actually present.
2940Sstevel@tonic-gate 		 */
2951772Sjl139090 		curr_cpu = DR_UNUM2SBD_UNUM(up->sbc_cm.sbdev_unum,
2965037Sjl139090 		    SBD_COMP_CPU);
2970Sstevel@tonic-gate 		if (curr_cpu >= next_cpu) {
2980Sstevel@tonic-gate 			cmn_err(CE_CONT, "OS configure %s",
2990Sstevel@tonic-gate 			    up->sbc_cm.sbdev_path);
3000Sstevel@tonic-gate 			next_cpu = curr_cpu + 1;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		if (up->sbc_cm.sbdev_state == DR_STATE_UNCONFIGURED) {
3040Sstevel@tonic-gate 			/*
3050Sstevel@tonic-gate 			 * If we're coming from the UNCONFIGURED
3060Sstevel@tonic-gate 			 * state then the cpu's sigblock will
3070Sstevel@tonic-gate 			 * still be mapped in.  Need to unmap it
3080Sstevel@tonic-gate 			 * before continuing with attachment.
3090Sstevel@tonic-gate 			 */
3105037Sjl139090 			PR_CPU("%s: unmapping sigblk for cpu %d\n", f,
3115037Sjl139090 			    up->sbc_cpu_id);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 			CPU_SGN_MAPOUT(up->sbc_cpu_id);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/*
3180Sstevel@tonic-gate 	 * Block out status threads while creating
3190Sstevel@tonic-gate 	 * devinfo tree branches
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
3223811Sbm42561 	ndi_devi_enter(ddi_root_node(), (int *)(&hp->h_ndi));
3230Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	return (0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*ARGSUSED*/
3290Sstevel@tonic-gate void
dr_attach_cpu(dr_handle_t * hp,dr_common_unit_t * cp)3300Sstevel@tonic-gate dr_attach_cpu(dr_handle_t *hp, dr_common_unit_t *cp)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	sbd_error_t	*err;
3330Sstevel@tonic-gate 	processorid_t	 cpuid;
3340Sstevel@tonic-gate 	int		 rv;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	err = drmach_configure(cp->sbdev_id, 0);
3390Sstevel@tonic-gate 	if (err) {
3400Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
3410Sstevel@tonic-gate 		return;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
3450Sstevel@tonic-gate 	if (err) {
3460Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
3470Sstevel@tonic-gate 
3481772Sjl139090 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
3490Sstevel@tonic-gate 		if (err)
3500Sstevel@tonic-gate 			sbd_err_clear(&err);
3510Sstevel@tonic-gate 	} else if ((rv = cpu_configure(cpuid)) != 0) {
3520Sstevel@tonic-gate 		dr_dev_err(CE_WARN, cp, dr_errno2ecode(rv));
3531772Sjl139090 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
3540Sstevel@tonic-gate 		if (err)
3550Sstevel@tonic-gate 			sbd_err_clear(&err);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * dr_post_attach_cpu
3610Sstevel@tonic-gate  *
3620Sstevel@tonic-gate  * sbd error policy: Does not stop on error.  Processes all units in list.
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate int
dr_post_attach_cpu(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)3650Sstevel@tonic-gate dr_post_attach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	int		i;
3680Sstevel@tonic-gate 	int		errflag = 0;
3690Sstevel@tonic-gate 	static fn_t	f = "dr_post_attach_cpu";
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/* Startup and online newly-attached CPUs */
3740Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
3750Sstevel@tonic-gate 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
3760Sstevel@tonic-gate 		struct cpu	*cp;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		cp = cpu_get(up->sbc_cpu_id);
3810Sstevel@tonic-gate 		if (cp == NULL) {
3820Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: cpu_get failed for cpu %d",
3830Sstevel@tonic-gate 			    f, up->sbc_cpu_id);
3840Sstevel@tonic-gate 			continue;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		if (cpu_is_poweredoff(cp)) {
3880Sstevel@tonic-gate 			if (cpu_poweron(cp) != 0) {
3890Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_CPUSTART);
3900Sstevel@tonic-gate 				errflag = 1;
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 			PR_CPU("%s: cpu %d powered ON\n", f, up->sbc_cpu_id);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		if (cpu_is_offline(cp)) {
3960Sstevel@tonic-gate 			PR_CPU("%s: onlining cpu %d...\n", f, up->sbc_cpu_id);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 			if (cpu_online(cp) != 0) {
3990Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_ONLINE);
4000Sstevel@tonic-gate 				errflag = 1;
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4073811Sbm42561 	ndi_devi_exit(ddi_root_node(), hp->h_ndi);
4080Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (errflag)
4110Sstevel@tonic-gate 		return (-1);
4120Sstevel@tonic-gate 	else
4130Sstevel@tonic-gate 		return (0);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * dr_pre_release_cpu
4180Sstevel@tonic-gate  *
4190Sstevel@tonic-gate  * sbd error policy: Stops on first error.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate int
dr_pre_release_cpu(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)4220Sstevel@tonic-gate dr_pre_release_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	int		c, cix, i, lastoffline = -1, rv = 0;
4250Sstevel@tonic-gate 	processorid_t	cpuid;
4260Sstevel@tonic-gate 	struct cpu	*cp;
4270Sstevel@tonic-gate 	dr_cpu_unit_t	*up;
4280Sstevel@tonic-gate 	dr_devset_t	devset;
4290Sstevel@tonic-gate 	sbd_dev_stat_t	*ds;
4300Sstevel@tonic-gate 	static fn_t	f = "dr_pre_release_cpu";
4310Sstevel@tonic-gate 	int		cpu_flags = 0;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	devset = DR_DEVS_PRESENT(hp->h_bd);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/* allocate status struct storage. */
4360Sstevel@tonic-gate 	ds = (sbd_dev_stat_t *) kmem_zalloc(sizeof (sbd_dev_stat_t) *
4375037Sjl139090 	    MAX_CPU_UNITS_PER_BOARD, KM_SLEEP);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	cix = dr_cpu_status(hp, devset, ds);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	for (i = 0; i < devnum; i++) {
4440Sstevel@tonic-gate 		up = (dr_cpu_unit_t *)devlist[i];
4450Sstevel@tonic-gate 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		/*
4480Sstevel@tonic-gate 		 * The STARCAT platform borrows cpus for use by POST in
4490Sstevel@tonic-gate 		 * iocage testing.  These cpus cannot be unconfigured
4500Sstevel@tonic-gate 		 * while they are in use for the iocage.
4510Sstevel@tonic-gate 		 * This check determines if a CPU is currently in use
4520Sstevel@tonic-gate 		 * for iocage testing, and if so, returns a "Device busy"
4530Sstevel@tonic-gate 		 * error.
4540Sstevel@tonic-gate 		 */
4550Sstevel@tonic-gate 		for (c = 0; c < cix; c++) {
4560Sstevel@tonic-gate 			if (ds[c].d_cpu.cs_unit == up->sbc_cm.sbdev_unum) {
4570Sstevel@tonic-gate 				if (ds[c].d_cpu.cs_busy) {
4585037Sjl139090 					dr_dev_err(CE_WARN, &up->sbc_cm,
4595037Sjl139090 					    ESBD_BUSY);
4600Sstevel@tonic-gate 					rv = -1;
4610Sstevel@tonic-gate 					break;
4620Sstevel@tonic-gate 				}
4630Sstevel@tonic-gate 			}
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 		if (c < cix)
4660Sstevel@tonic-gate 			break;
4670Sstevel@tonic-gate 		cpuid = up->sbc_cpu_id;
4680Sstevel@tonic-gate 		if ((cp = cpu_get(cpuid)) == NULL) {
4690Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
4700Sstevel@tonic-gate 			rv = -1;
4710Sstevel@tonic-gate 			break;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		/* used by dr_cancel_cpu during error flow */
4750Sstevel@tonic-gate 		up->sbc_cpu_flags = cp->cpu_flags;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (CPU_ACTIVE(cp)) {
4780Sstevel@tonic-gate 			if (dr_cmd_flags(hp) & SBD_FLAG_FORCE)
4790Sstevel@tonic-gate 				cpu_flags = CPU_FORCED;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 			PR_CPU("%s: offlining cpu %d\n", f, cpuid);
4820Sstevel@tonic-gate 			if (cpu_offline(cp, cpu_flags)) {
4835037Sjl139090 				PR_CPU("%s: failed to offline cpu %d\n", f,
4845037Sjl139090 				    cpuid);
4850Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
4860Sstevel@tonic-gate 				if (disp_bound_threads(cp, 0)) {
4875037Sjl139090 					cmn_err(CE_WARN, "%s: thread(s) bound "
4885037Sjl139090 					    "to cpu %d", f, cp->cpu_id);
4890Sstevel@tonic-gate 				}
4900Sstevel@tonic-gate 				rv = -1;
4910Sstevel@tonic-gate 				break;
4920Sstevel@tonic-gate 			} else
4930Sstevel@tonic-gate 				lastoffline = i;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		if (!rv) {
4970Sstevel@tonic-gate 			sbd_error_t *err;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 			err = drmach_release(up->sbc_cm.sbdev_id);
5000Sstevel@tonic-gate 			if (err) {
5010Sstevel@tonic-gate 				DRERR_SET_C(&up->sbc_cm.sbdev_error, &err);
5020Sstevel@tonic-gate 				rv = -1;
5030Sstevel@tonic-gate 				break;
5040Sstevel@tonic-gate 			}
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (rv) {
5110Sstevel@tonic-gate 		/*
5120Sstevel@tonic-gate 		 * Need to unwind others since at this level (pre-release)
5130Sstevel@tonic-gate 		 * the device state has not yet transitioned and failures
5140Sstevel@tonic-gate 		 * will prevent us from reaching the "post" release
5150Sstevel@tonic-gate 		 * function where states are normally transitioned.
5160Sstevel@tonic-gate 		 */
5170Sstevel@tonic-gate 		for (i = lastoffline; i >= 0; i--) {
5180Sstevel@tonic-gate 			up = (dr_cpu_unit_t *)devlist[i];
5190Sstevel@tonic-gate 			(void) dr_cancel_cpu(up);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	kmem_free(ds, sizeof (sbd_dev_stat_t) * MAX_CPU_UNITS_PER_BOARD);
5240Sstevel@tonic-gate 	return (rv);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * dr_pre_detach_cpu
5290Sstevel@tonic-gate  *
5300Sstevel@tonic-gate  * sbd error policy: Stops on first error.
5310Sstevel@tonic-gate  */
5320Sstevel@tonic-gate int
dr_pre_detach_cpu(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)5330Sstevel@tonic-gate dr_pre_detach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	int		i;
5380Sstevel@tonic-gate 	int		curr_cpu;
5390Sstevel@tonic-gate 	int		next_cpu;
5400Sstevel@tonic-gate 	int		cpu_flags = 0;
5410Sstevel@tonic-gate 	static fn_t	f = "dr_pre_detach_cpu";
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/*
5460Sstevel@tonic-gate 	 * Block out status threads while destroying devinfo tree
5470Sstevel@tonic-gate 	 * branches
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
5500Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	for (next_cpu = 0, i = 0; i < devnum; i++) {
5530Sstevel@tonic-gate 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
5540Sstevel@tonic-gate 		struct cpu	*cp;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		cp = cpu_get(up->sbc_cpu_id);
5590Sstevel@tonic-gate 		if (cp == NULL)
5600Sstevel@tonic-gate 			continue;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		/*
5630Sstevel@tonic-gate 		 * Print a console message for each attachment
5640Sstevel@tonic-gate 		 * point. For CMP devices, this means that only
5650Sstevel@tonic-gate 		 * one message should be printed, no matter how
5660Sstevel@tonic-gate 		 * many cores are actually present.
5670Sstevel@tonic-gate 		 */
5681772Sjl139090 		curr_cpu = DR_UNUM2SBD_UNUM(up->sbc_cm.sbdev_unum,
5695037Sjl139090 		    SBD_COMP_CPU);
5700Sstevel@tonic-gate 		if (curr_cpu >= next_cpu) {
5710Sstevel@tonic-gate 			cmn_err(CE_CONT, "OS unconfigure %s\n",
5720Sstevel@tonic-gate 			    up->sbc_cm.sbdev_path);
5730Sstevel@tonic-gate 			next_cpu = curr_cpu + 1;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		/*
5770Sstevel@tonic-gate 		 * CPUs were offlined during Release.
5780Sstevel@tonic-gate 		 */
5790Sstevel@tonic-gate 		if (cpu_is_poweredoff(cp)) {
5800Sstevel@tonic-gate 			PR_CPU("%s: cpu %d already powered OFF\n",
5810Sstevel@tonic-gate 			    f, up->sbc_cpu_id);
5820Sstevel@tonic-gate 			continue;
5830Sstevel@tonic-gate 		}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 		if (!cpu_is_offline(cp)) {
5860Sstevel@tonic-gate 			if (dr_cmd_flags(hp) & SBD_FLAG_FORCE)
5870Sstevel@tonic-gate 				cpu_flags = CPU_FORCED;
5880Sstevel@tonic-gate 			/* cpu was onlined after release.  Offline it again */
5890Sstevel@tonic-gate 			PR_CPU("%s: offlining cpu %d\n", f, up->sbc_cpu_id);
5900Sstevel@tonic-gate 			if (cpu_offline(cp, cpu_flags)) {
5910Sstevel@tonic-gate 				PR_CPU("%s: failed to offline cpu %d\n",
5920Sstevel@tonic-gate 				    f, up->sbc_cpu_id);
5930Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
5940Sstevel@tonic-gate 				if (disp_bound_threads(cp, 0)) {
5955037Sjl139090 					cmn_err(CE_WARN, "%s: thread(s) bound "
5965037Sjl139090 					    "to cpu %d", f, cp->cpu_id);
5970Sstevel@tonic-gate 				}
5980Sstevel@tonic-gate 				goto err;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		if (cpu_poweroff(cp) != 0) {
6020Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_CPUSTOP);
6030Sstevel@tonic-gate 			goto err;
6040Sstevel@tonic-gate 		} else {
6050Sstevel@tonic-gate 			PR_CPU("%s: cpu %d powered OFF\n", f, up->sbc_cpu_id);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	return (0);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate err:
6120Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6130Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
6140Sstevel@tonic-gate 	return (-1);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*ARGSUSED*/
6180Sstevel@tonic-gate void
dr_detach_cpu(dr_handle_t * hp,dr_common_unit_t * cp)6190Sstevel@tonic-gate dr_detach_cpu(dr_handle_t *hp, dr_common_unit_t *cp)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	sbd_error_t	*err;
6220Sstevel@tonic-gate 	processorid_t	 cpuid;
6230Sstevel@tonic-gate 	int		 rv;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
6280Sstevel@tonic-gate 	if (err) {
6290Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
6300Sstevel@tonic-gate 	} else if ((rv = cpu_unconfigure(cpuid)) != 0) {
6310Sstevel@tonic-gate 		dr_dev_err(CE_IGNORE, cp, dr_errno2ecode(rv));
6320Sstevel@tonic-gate 	} else {
6331772Sjl139090 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
6340Sstevel@tonic-gate 		if (err) {
6350Sstevel@tonic-gate 			DRERR_SET_C(&cp->sbdev_error, &err);
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*ARGSUSED1*/
6410Sstevel@tonic-gate int
dr_post_detach_cpu(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)6420Sstevel@tonic-gate dr_post_detach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_cpu";
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
6470Sstevel@tonic-gate 	hp->h_ndi = 0;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6500Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	return (0);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate static void
dr_fill_cpu_stat(dr_cpu_unit_t * cp,drmach_status_t * pstat,sbd_cpu_stat_t * csp)6560Sstevel@tonic-gate dr_fill_cpu_stat(dr_cpu_unit_t *cp, drmach_status_t *pstat, sbd_cpu_stat_t *csp)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	ASSERT(cp && pstat && csp);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* Fill in the common status information */
6610Sstevel@tonic-gate 	bzero((caddr_t)csp, sizeof (*csp));
6620Sstevel@tonic-gate 	csp->cs_type = cp->sbc_cm.sbdev_type;
6630Sstevel@tonic-gate 	csp->cs_unit = cp->sbc_cm.sbdev_unum;
664*11311SSurya.Prakki@Sun.COM 	(void) strncpy(csp->cs_name, pstat->type, sizeof (csp->cs_name));
6650Sstevel@tonic-gate 	csp->cs_cond = cp->sbc_cm.sbdev_cond;
6660Sstevel@tonic-gate 	csp->cs_busy = cp->sbc_cm.sbdev_busy | pstat->busy;
6670Sstevel@tonic-gate 	csp->cs_time = cp->sbc_cm.sbdev_time;
6680Sstevel@tonic-gate 	csp->cs_ostate = cp->sbc_cm.sbdev_ostate;
6690Sstevel@tonic-gate 	csp->cs_suspend = 0;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	/* CPU specific status data */
6720Sstevel@tonic-gate 	csp->cs_cpuid = cp->sbc_cpu_id;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate #ifdef _STARFIRE
6750Sstevel@tonic-gate 	csp->cs_isbootproc = (SIGBCPU->cpu_id == cp->sbc_cpu_id) ? 1 : 0;
6760Sstevel@tonic-gate #endif /* _STARFIRE */
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * If the speed and ecache properties have not been
6800Sstevel@tonic-gate 	 * cached yet, read them in from the device tree.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	if ((cp->sbc_speed == 0) || (cp->sbc_ecache == 0))
6830Sstevel@tonic-gate 		dr_cpu_set_prop(cp);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/* use the cached speed and ecache values */
6860Sstevel@tonic-gate 	csp->cs_speed = cp->sbc_speed;
6870Sstevel@tonic-gate 	csp->cs_ecache = cp->sbc_ecache;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
6900Sstevel@tonic-gate 	if (!cpu_get(csp->cs_cpuid)) {
6910Sstevel@tonic-gate 		/* ostate must be UNCONFIGURED */
6920Sstevel@tonic-gate 		csp->cs_cm.c_ostate = SBD_STAT_UNCONFIGURED;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate static void
dr_fill_cmp_stat(sbd_cpu_stat_t * csp,int ncores,int impl,sbd_cmp_stat_t * psp)6980Sstevel@tonic-gate dr_fill_cmp_stat(sbd_cpu_stat_t *csp, int ncores, int impl, sbd_cmp_stat_t *psp)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	int	core;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	ASSERT(csp && psp && (ncores >= 1));
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	bzero((caddr_t)psp, sizeof (*psp));
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * Fill in the common status information based
7080Sstevel@tonic-gate 	 * on the data for the first core.
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	psp->ps_type = SBD_COMP_CMP;
7111772Sjl139090 	psp->ps_unit = DR_UNUM2SBD_UNUM(csp->cs_unit, SBD_COMP_CMP);
712*11311SSurya.Prakki@Sun.COM 	(void) strncpy(psp->ps_name, csp->cs_name, sizeof (psp->ps_name));
7130Sstevel@tonic-gate 	psp->ps_cond = csp->cs_cond;
7140Sstevel@tonic-gate 	psp->ps_busy = csp->cs_busy;
7150Sstevel@tonic-gate 	psp->ps_time = csp->cs_time;
7160Sstevel@tonic-gate 	psp->ps_ostate = csp->cs_ostate;
7170Sstevel@tonic-gate 	psp->ps_suspend = csp->cs_suspend;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* CMP specific status data */
7200Sstevel@tonic-gate 	*psp->ps_cpuid = csp->cs_cpuid;
7210Sstevel@tonic-gate 	psp->ps_ncores = 1;
7220Sstevel@tonic-gate 	psp->ps_speed = csp->cs_speed;
7230Sstevel@tonic-gate 	psp->ps_ecache = csp->cs_ecache;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * Walk through the data for the remaining cores.
7270Sstevel@tonic-gate 	 * Make any adjustments to the common status data,
7280Sstevel@tonic-gate 	 * or the shared CMP specific data if necessary.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	for (core = 1; core < ncores; core++) {
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * The following properties should be the same
7340Sstevel@tonic-gate 		 * for all the cores of the CMP.
7350Sstevel@tonic-gate 		 */
7365037Sjl139090 		ASSERT(psp->ps_unit == DR_UNUM2SBD_UNUM(csp[core].cs_unit,
7375037Sjl139090 		    SBD_COMP_CMP));
7380Sstevel@tonic-gate 		ASSERT(psp->ps_speed == csp[core].cs_speed);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		psp->ps_cpuid[core] = csp[core].cs_cpuid;
7410Sstevel@tonic-gate 		psp->ps_ncores++;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		/*
7440Sstevel@tonic-gate 		 * Jaguar has a split ecache, so the ecache
7450Sstevel@tonic-gate 		 * for each core must be added together to
7460Sstevel@tonic-gate 		 * get the total ecache for the whole chip.
7470Sstevel@tonic-gate 		 */
7480Sstevel@tonic-gate 		if (IS_JAGUAR(impl)) {
7490Sstevel@tonic-gate 			psp->ps_ecache += csp[core].cs_ecache;
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		/* adjust time if necessary */
7530Sstevel@tonic-gate 		if (csp[core].cs_time > psp->ps_time) {
7540Sstevel@tonic-gate 			psp->ps_time = csp[core].cs_time;
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		psp->ps_busy |= csp[core].cs_busy;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		/*
7600Sstevel@tonic-gate 		 * If any of the cores are configured, the
7610Sstevel@tonic-gate 		 * entire CMP is marked as configured.
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		if (csp[core].cs_ostate == SBD_STAT_CONFIGURED) {
7640Sstevel@tonic-gate 			psp->ps_ostate = csp[core].cs_ostate;
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate int
dr_cpu_status(dr_handle_t * hp,dr_devset_t devset,sbd_dev_stat_t * dsp)7700Sstevel@tonic-gate dr_cpu_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	int		cmp;
7730Sstevel@tonic-gate 	int		core;
7740Sstevel@tonic-gate 	int		ncpu;
7750Sstevel@tonic-gate 	dr_board_t	*bp;
7760Sstevel@tonic-gate 	sbd_cpu_stat_t	cstat[MAX_CORES_PER_CMP];
7771772Sjl139090 	int		impl;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	bp = hp->h_bd;
7800Sstevel@tonic-gate 	ncpu = 0;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * Treat every CPU as a CMP. In the case where the
7860Sstevel@tonic-gate 	 * device is not a CMP, treat it as a CMP with only
7870Sstevel@tonic-gate 	 * one core.
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	for (cmp = 0; cmp < MAX_CMP_UNITS_PER_BOARD; cmp++) {
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		int		ncores;
7920Sstevel@tonic-gate 		dr_cpu_unit_t	*cp;
7930Sstevel@tonic-gate 		drmach_status_t	pstat;
7940Sstevel@tonic-gate 		sbd_error_t	*err;
7950Sstevel@tonic-gate 		sbd_cmp_stat_t	*psp;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		if ((devset & DEVSET(SBD_COMP_CMP, cmp)) == 0) {
7980Sstevel@tonic-gate 			continue;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		ncores = 0;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		for (core = 0; core < MAX_CORES_PER_CMP; core++) {
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 			cp = dr_get_cpu_unit(bp, DR_CMP_CORE_UNUM(cmp, core));
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 			if (cp->sbc_cm.sbdev_state == DR_STATE_EMPTY) {
8080Sstevel@tonic-gate 				/* present, but not fully initialized */
8090Sstevel@tonic-gate 				continue;
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 			ASSERT(dr_cpu_unit_is_sane(hp->h_bd, cp));
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 			/* skip if not present */
8150Sstevel@tonic-gate 			if (cp->sbc_cm.sbdev_id == (drmachid_t)0) {
8160Sstevel@tonic-gate 				continue;
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 			/* fetch platform status */
8200Sstevel@tonic-gate 			err = drmach_status(cp->sbc_cm.sbdev_id, &pstat);
8210Sstevel@tonic-gate 			if (err) {
8220Sstevel@tonic-gate 				DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
8230Sstevel@tonic-gate 				continue;
8240Sstevel@tonic-gate 			}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 			dr_fill_cpu_stat(cp, &pstat, &cstat[ncores++]);
8271772Sjl139090 			/*
8281772Sjl139090 			 * We should set impl here because the last core
8291772Sjl139090 			 * found might be EMPTY or not present.
8301772Sjl139090 			 */
8311772Sjl139090 			impl = cp->sbc_cpu_impl;
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		if (ncores == 0) {
8350Sstevel@tonic-gate 			continue;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		/*
8390Sstevel@tonic-gate 		 * Store the data to the outgoing array. If the
8400Sstevel@tonic-gate 		 * device is a CMP, combine all the data for the
8410Sstevel@tonic-gate 		 * cores into a single stat structure.
8420Sstevel@tonic-gate 		 *
8430Sstevel@tonic-gate 		 * The check for a CMP device uses the last core
8440Sstevel@tonic-gate 		 * found, assuming that all cores will have the
8450Sstevel@tonic-gate 		 * same implementation.
8460Sstevel@tonic-gate 		 */
8471772Sjl139090 
8481772Sjl139090 		if (CPU_IMPL_IS_CMP(impl)) {
8490Sstevel@tonic-gate 			psp = (sbd_cmp_stat_t *)dsp;
8501772Sjl139090 			dr_fill_cmp_stat(cstat, ncores, impl, psp);
8510Sstevel@tonic-gate 		} else {
8520Sstevel@tonic-gate 			ASSERT(ncores == 1);
8530Sstevel@tonic-gate 			bcopy(cstat, dsp, sizeof (sbd_cpu_stat_t));
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		dsp++;
8570Sstevel@tonic-gate 		ncpu++;
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	return (ncpu);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * Cancel previous release operation for cpu.
8650Sstevel@tonic-gate  * For cpus this means simply bringing cpus that
8660Sstevel@tonic-gate  * were offline back online.  Note that they had
8670Sstevel@tonic-gate  * to have been online at the time there were
8680Sstevel@tonic-gate  * released.
8690Sstevel@tonic-gate  */
8700Sstevel@tonic-gate int
dr_cancel_cpu(dr_cpu_unit_t * up)8710Sstevel@tonic-gate dr_cancel_cpu(dr_cpu_unit_t *up)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	int		rv = 0;
8740Sstevel@tonic-gate 	static fn_t	f = "dr_cancel_cpu";
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	ASSERT(dr_cpu_unit_is_sane(up->sbc_cm.sbdev_bp, up));
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (cpu_flagged_active(up->sbc_cpu_flags)) {
8790Sstevel@tonic-gate 		struct cpu	*cp;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		/*
8820Sstevel@tonic-gate 		 * CPU had been online, go ahead
8830Sstevel@tonic-gate 		 * bring it back online.
8840Sstevel@tonic-gate 		 */
8855037Sjl139090 		PR_CPU("%s: bringing cpu %d back ONLINE\n", f, up->sbc_cpu_id);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
8880Sstevel@tonic-gate 		cp = cpu[up->sbc_cpu_id];
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		if (cpu_is_poweredoff(cp)) {
8910Sstevel@tonic-gate 			if (cpu_poweron(cp)) {
8920Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s: failed to power-on "
8930Sstevel@tonic-gate 				    "cpu %d", f, up->sbc_cpu_id);
8940Sstevel@tonic-gate 				rv = -1;
8950Sstevel@tonic-gate 			}
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 		if (cpu_is_offline(cp)) {
8990Sstevel@tonic-gate 			if (cpu_online(cp)) {
9000Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s: failed to online cpu %d",
9010Sstevel@tonic-gate 				    f, up->sbc_cpu_id);
9020Sstevel@tonic-gate 				rv = -1;
9030Sstevel@tonic-gate 			}
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		if (cpu_is_online(cp)) {
9070Sstevel@tonic-gate 			if (cpu_flagged_nointr(up->sbc_cpu_flags)) {
9080Sstevel@tonic-gate 				if (cpu_intr_disable(cp) != 0) {
9090Sstevel@tonic-gate 					cmn_err(CE_WARN, "%s: failed to "
9105037Sjl139090 					    "disable interrupts on cpu %d", f,
9115037Sjl139090 					    up->sbc_cpu_id);
9120Sstevel@tonic-gate 				}
9130Sstevel@tonic-gate 			}
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	return (rv);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate int
dr_disconnect_cpu(dr_cpu_unit_t * up)9230Sstevel@tonic-gate dr_disconnect_cpu(dr_cpu_unit_t *up)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	sbd_error_t	*err;
9260Sstevel@tonic-gate 	static fn_t	f = "dr_disconnect_cpu";
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	ASSERT((up->sbc_cm.sbdev_state == DR_STATE_CONNECTED) ||
9315037Sjl139090 	    (up->sbc_cm.sbdev_state == DR_STATE_UNCONFIGURED));
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	ASSERT(dr_cpu_unit_is_sane(up->sbc_cm.sbdev_bp, up));
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (up->sbc_cm.sbdev_state == DR_STATE_CONNECTED) {
9360Sstevel@tonic-gate 		/*
9370Sstevel@tonic-gate 		 * Cpus were never brought in and so are still
9380Sstevel@tonic-gate 		 * effectively disconnected, so nothing to do here.
9390Sstevel@tonic-gate 		 */
9405037Sjl139090 		PR_CPU("%s: cpu %d never brought in\n", f, up->sbc_cpu_id);
9410Sstevel@tonic-gate 		return (0);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	err = drmach_cpu_disconnect(up->sbc_cm.sbdev_id);
9450Sstevel@tonic-gate 	if (err == NULL)
9460Sstevel@tonic-gate 		return (0);
9470Sstevel@tonic-gate 	else {
9480Sstevel@tonic-gate 		DRERR_SET_C(&up->sbc_cm.sbdev_error, &err);
9490Sstevel@tonic-gate 		return (-1);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 	/*NOTREACHED*/
9520Sstevel@tonic-gate }
953