xref: /titanic_53/usr/src/uts/common/disp/cmt.c (revision 1a77c24bc3a54fb48592de0041508561c5781501)
1fb2f18f8Sesaxe /*
2fb2f18f8Sesaxe  * CDDL HEADER START
3fb2f18f8Sesaxe  *
4fb2f18f8Sesaxe  * The contents of this file are subject to the terms of the
5fb2f18f8Sesaxe  * Common Development and Distribution License (the "License").
6fb2f18f8Sesaxe  * You may not use this file except in compliance with the License.
7fb2f18f8Sesaxe  *
8fb2f18f8Sesaxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fb2f18f8Sesaxe  * or http://www.opensolaris.org/os/licensing.
10fb2f18f8Sesaxe  * See the License for the specific language governing permissions
11fb2f18f8Sesaxe  * and limitations under the License.
12fb2f18f8Sesaxe  *
13fb2f18f8Sesaxe  * When distributing Covered Code, include this CDDL HEADER in each
14fb2f18f8Sesaxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fb2f18f8Sesaxe  * If applicable, add the following below this CDDL HEADER, with the
16fb2f18f8Sesaxe  * fields enclosed by brackets "[]" replaced with your own identifying
17fb2f18f8Sesaxe  * information: Portions Copyright [yyyy] [name of copyright owner]
18fb2f18f8Sesaxe  *
19fb2f18f8Sesaxe  * CDDL HEADER END
20fb2f18f8Sesaxe  */
21fb2f18f8Sesaxe /*
223e81cacfSEric Saxe  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fb2f18f8Sesaxe  * Use is subject to license terms.
24fb2f18f8Sesaxe  */
25fb2f18f8Sesaxe 
26fb2f18f8Sesaxe #include <sys/systm.h>
27fb2f18f8Sesaxe #include <sys/types.h>
28fb2f18f8Sesaxe #include <sys/param.h>
29fb2f18f8Sesaxe #include <sys/thread.h>
30fb2f18f8Sesaxe #include <sys/cpuvar.h>
31fb2f18f8Sesaxe #include <sys/cpupart.h>
32fb2f18f8Sesaxe #include <sys/kmem.h>
33fb2f18f8Sesaxe #include <sys/cmn_err.h>
34fb2f18f8Sesaxe #include <sys/kstat.h>
35fb2f18f8Sesaxe #include <sys/processor.h>
36fb2f18f8Sesaxe #include <sys/disp.h>
37fb2f18f8Sesaxe #include <sys/group.h>
38fb2f18f8Sesaxe #include <sys/pghw.h>
39fb2f18f8Sesaxe #include <sys/bitset.h>
40fb2f18f8Sesaxe #include <sys/lgrp.h>
41fb2f18f8Sesaxe #include <sys/cmt.h>
420e751525SEric Saxe #include <sys/cpu_pm.h>
43fb2f18f8Sesaxe 
44fb2f18f8Sesaxe /*
45fb2f18f8Sesaxe  * CMT scheduler / dispatcher support
46fb2f18f8Sesaxe  *
47fb2f18f8Sesaxe  * This file implements CMT scheduler support using Processor Groups.
48fb2f18f8Sesaxe  * The CMT processor group class creates and maintains the CMT class
49fb2f18f8Sesaxe  * specific processor group pg_cmt_t.
50fb2f18f8Sesaxe  *
51fb2f18f8Sesaxe  * ---------------------------- <-- pg_cmt_t *
52fb2f18f8Sesaxe  * | pghw_t                   |
53fb2f18f8Sesaxe  * ----------------------------
54fb2f18f8Sesaxe  * | CMT class specific data  |
55fb2f18f8Sesaxe  * | - hierarchy linkage      |
56fb2f18f8Sesaxe  * | - CMT load balancing data|
57fb2f18f8Sesaxe  * | - active CPU group/bitset|
58fb2f18f8Sesaxe  * ----------------------------
59fb2f18f8Sesaxe  *
60fb2f18f8Sesaxe  * The scheduler/dispatcher leverages knowledge of the performance
61fb2f18f8Sesaxe  * relevant CMT sharing relationships existing between cpus to implement
620e751525SEric Saxe  * optimized affinity, load balancing, and coalescence policies.
63fb2f18f8Sesaxe  *
64fb2f18f8Sesaxe  * Load balancing policy seeks to improve performance by minimizing
650e751525SEric Saxe  * contention over shared processor resources / facilities, Affinity
660e751525SEric Saxe  * policies seek to improve cache and TLB utilization. Coalescence
670e751525SEric Saxe  * policies improve resource utilization and ultimately power efficiency.
68fb2f18f8Sesaxe  *
69fb2f18f8Sesaxe  * The CMT PGs created by this class are already arranged into a
70fb2f18f8Sesaxe  * hierarchy (which is done in the pghw layer). To implement the top-down
71fb2f18f8Sesaxe  * CMT load balancing algorithm, the CMT PGs additionally maintain
72fb2f18f8Sesaxe  * parent, child and sibling hierarchy relationships.
73fb2f18f8Sesaxe  * Parent PGs always contain a superset of their children(s) resources,
74fb2f18f8Sesaxe  * each PG can have at most one parent, and siblings are the group of PGs
75fb2f18f8Sesaxe  * sharing the same parent.
76fb2f18f8Sesaxe  *
77fb2f18f8Sesaxe  * On NUMA systems, the CMT load balancing algorithm balances across the
78fb2f18f8Sesaxe  * CMT PGs within their respective lgroups. On UMA based system, there
79fb2f18f8Sesaxe  * exists a top level group of PGs to balance across. On NUMA systems multiple
80fb2f18f8Sesaxe  * top level groups are instantiated, where the top level balancing begins by
81fb2f18f8Sesaxe  * balancng across the CMT PGs within their respective (per lgroup) top level
82fb2f18f8Sesaxe  * groups.
83fb2f18f8Sesaxe  */
84a6604450Sesaxe static cmt_lgrp_t	*cmt_lgrps = NULL;	/* cmt_lgrps list head */
85a6604450Sesaxe static cmt_lgrp_t	*cpu0_lgrp = NULL;	/* boot CPU's initial lgrp */
86a6604450Sesaxe 						/* used for null_proc_lpa */
870e751525SEric Saxe cmt_lgrp_t		*cmt_root = NULL;	/* Reference to root cmt pg */
88fb2f18f8Sesaxe 
89a6604450Sesaxe static int		is_cpu0 = 1; /* true if this is boot CPU context */
90a6604450Sesaxe 
91a6604450Sesaxe /*
920e751525SEric Saxe  * Array of hardware sharing relationships that are blacklisted.
930e751525SEric Saxe  * PGs won't be instantiated for blacklisted hardware sharing relationships.
940e751525SEric Saxe  */
950e751525SEric Saxe static int		cmt_hw_blacklisted[PGHW_NUM_COMPONENTS];
960e751525SEric Saxe 
970e751525SEric Saxe /*
98a6604450Sesaxe  * Set this to non-zero to disable CMT scheduling
99a6604450Sesaxe  * This must be done via kmdb -d, as /etc/system will be too late
100a6604450Sesaxe  */
1010e751525SEric Saxe int			cmt_sched_disabled = 0;
102fb2f18f8Sesaxe 
103ef4f35d8SEric Saxe /*
104ef4f35d8SEric Saxe  * Status codes for CMT lineage validation
105ef4f35d8SEric Saxe  * See pg_cmt_lineage_validate() below
106ef4f35d8SEric Saxe  */
107ef4f35d8SEric Saxe typedef enum cmt_lineage_validation {
108ef4f35d8SEric Saxe 	CMT_LINEAGE_VALID,
109ef4f35d8SEric Saxe 	CMT_LINEAGE_NON_CONCENTRIC,
110ef4f35d8SEric Saxe 	CMT_LINEAGE_PG_SPANS_LGRPS,
111ef4f35d8SEric Saxe 	CMT_LINEAGE_NON_PROMOTABLE,
112ef4f35d8SEric Saxe 	CMT_LINEAGE_REPAIRED,
113ef4f35d8SEric Saxe 	CMT_LINEAGE_UNRECOVERABLE
114ef4f35d8SEric Saxe } cmt_lineage_validation_t;
115ef4f35d8SEric Saxe 
116ef4f35d8SEric Saxe /*
117ef4f35d8SEric Saxe  * Status of the current lineage under construction.
118ef4f35d8SEric Saxe  * One must be holding cpu_lock to change this.
119ef4f35d8SEric Saxe  */
120ef4f35d8SEric Saxe cmt_lineage_validation_t	cmt_lineage_status = CMT_LINEAGE_VALID;
121ef4f35d8SEric Saxe 
122ef4f35d8SEric Saxe /*
123ef4f35d8SEric Saxe  * Power domain definitions (on x86) are defined by ACPI, and
124ef4f35d8SEric Saxe  * therefore may be subject to BIOS bugs.
125ef4f35d8SEric Saxe  */
126ef4f35d8SEric Saxe #define	PG_CMT_HW_SUSPECT(hw)	PGHW_IS_PM_DOMAIN(hw)
127ef4f35d8SEric Saxe 
128ef4f35d8SEric Saxe /*
129ef4f35d8SEric Saxe  * Macro to test if PG is managed by the CMT PG class
130ef4f35d8SEric Saxe  */
131ef4f35d8SEric Saxe #define	IS_CMT_PG(pg)	(((pg_t *)(pg))->pg_class->pgc_id == pg_cmt_class_id)
132ef4f35d8SEric Saxe 
133fb2f18f8Sesaxe static pg_cid_t		pg_cmt_class_id;		/* PG class id */
134fb2f18f8Sesaxe 
135fb2f18f8Sesaxe static pg_t		*pg_cmt_alloc();
136fb2f18f8Sesaxe static void		pg_cmt_free(pg_t *);
13747ab0c7cSEric Saxe static void		pg_cmt_cpu_init(cpu_t *, cpu_pg_t *);
13847ab0c7cSEric Saxe static void		pg_cmt_cpu_fini(cpu_t *, cpu_pg_t *);
139fb2f18f8Sesaxe static void		pg_cmt_cpu_active(cpu_t *);
140fb2f18f8Sesaxe static void		pg_cmt_cpu_inactive(cpu_t *);
141fb2f18f8Sesaxe static void		pg_cmt_cpupart_in(cpu_t *, cpupart_t *);
142fb2f18f8Sesaxe static void		pg_cmt_cpupart_move(cpu_t *, cpupart_t *, cpupart_t *);
1430e751525SEric Saxe static char		*pg_cmt_policy_name(pg_t *);
1440e751525SEric Saxe static void		pg_cmt_hier_sort(pg_cmt_t **, int);
1450e751525SEric Saxe static pg_cmt_t		*pg_cmt_hier_rank(pg_cmt_t *, pg_cmt_t *);
146fb2f18f8Sesaxe static int		pg_cmt_cpu_belongs(pg_t *, cpu_t *);
147fb2f18f8Sesaxe static int		pg_cmt_hw(pghw_type_t);
148fb2f18f8Sesaxe static cmt_lgrp_t	*pg_cmt_find_lgrp(lgrp_handle_t);
149a6604450Sesaxe static cmt_lgrp_t	*pg_cmt_lgrp_create(lgrp_handle_t);
1500e751525SEric Saxe static void		cmt_ev_thread_swtch(pg_t *, cpu_t *, hrtime_t,
1510e751525SEric Saxe 			    kthread_t *, kthread_t *);
1520e751525SEric Saxe static void		cmt_ev_thread_swtch_pwr(pg_t *, cpu_t *, hrtime_t,
1530e751525SEric Saxe 			    kthread_t *, kthread_t *);
1540e751525SEric Saxe static void		cmt_ev_thread_remain_pwr(pg_t *, cpu_t *, kthread_t *);
155*1a77c24bSEric Saxe static cmt_lineage_validation_t	pg_cmt_lineage_validate(pg_cmt_t **, int *,
156*1a77c24bSEric Saxe 			    cpu_pg_t *);
157fb2f18f8Sesaxe 
1580e751525SEric Saxe 
1590e751525SEric Saxe /*
160fb2f18f8Sesaxe  * CMT PG ops
161fb2f18f8Sesaxe  */
162fb2f18f8Sesaxe struct pg_ops pg_ops_cmt = {
163fb2f18f8Sesaxe 	pg_cmt_alloc,
164fb2f18f8Sesaxe 	pg_cmt_free,
165fb2f18f8Sesaxe 	pg_cmt_cpu_init,
166fb2f18f8Sesaxe 	pg_cmt_cpu_fini,
167fb2f18f8Sesaxe 	pg_cmt_cpu_active,
168fb2f18f8Sesaxe 	pg_cmt_cpu_inactive,
169fb2f18f8Sesaxe 	pg_cmt_cpupart_in,
170fb2f18f8Sesaxe 	NULL,			/* cpupart_out */
171fb2f18f8Sesaxe 	pg_cmt_cpupart_move,
172fb2f18f8Sesaxe 	pg_cmt_cpu_belongs,
1730e751525SEric Saxe 	pg_cmt_policy_name,
174fb2f18f8Sesaxe };
175fb2f18f8Sesaxe 
176fb2f18f8Sesaxe /*
177fb2f18f8Sesaxe  * Initialize the CMT PG class
178fb2f18f8Sesaxe  */
179fb2f18f8Sesaxe void
180fb2f18f8Sesaxe pg_cmt_class_init(void)
181fb2f18f8Sesaxe {
182fb2f18f8Sesaxe 	if (cmt_sched_disabled)
183fb2f18f8Sesaxe 		return;
184fb2f18f8Sesaxe 
185fb2f18f8Sesaxe 	pg_cmt_class_id = pg_class_register("cmt", &pg_ops_cmt, PGR_PHYSICAL);
186fb2f18f8Sesaxe }
187fb2f18f8Sesaxe 
188fb2f18f8Sesaxe /*
189fb2f18f8Sesaxe  * Called to indicate a new CPU has started up so
190fb2f18f8Sesaxe  * that either t0 or the slave startup thread can
191fb2f18f8Sesaxe  * be accounted for.
192fb2f18f8Sesaxe  */
193fb2f18f8Sesaxe void
194fb2f18f8Sesaxe pg_cmt_cpu_startup(cpu_t *cp)
195fb2f18f8Sesaxe {
1960e751525SEric Saxe 	pg_ev_thread_swtch(cp, gethrtime_unscaled(), cp->cpu_idle_thread,
1970e751525SEric Saxe 	    cp->cpu_thread);
198fb2f18f8Sesaxe }
199fb2f18f8Sesaxe 
200fb2f18f8Sesaxe /*
201fb2f18f8Sesaxe  * Return non-zero if thread can migrate between "from" and "to"
202fb2f18f8Sesaxe  * without a performance penalty
203fb2f18f8Sesaxe  */
204fb2f18f8Sesaxe int
205fb2f18f8Sesaxe pg_cmt_can_migrate(cpu_t *from, cpu_t *to)
206fb2f18f8Sesaxe {
207fb2f18f8Sesaxe 	if (from->cpu_physid->cpu_cacheid ==
208fb2f18f8Sesaxe 	    to->cpu_physid->cpu_cacheid)
209fb2f18f8Sesaxe 		return (1);
210fb2f18f8Sesaxe 	return (0);
211fb2f18f8Sesaxe }
212fb2f18f8Sesaxe 
213fb2f18f8Sesaxe /*
214fb2f18f8Sesaxe  * CMT class specific PG allocation
215fb2f18f8Sesaxe  */
216fb2f18f8Sesaxe static pg_t *
217fb2f18f8Sesaxe pg_cmt_alloc(void)
218fb2f18f8Sesaxe {
219fb2f18f8Sesaxe 	return (kmem_zalloc(sizeof (pg_cmt_t), KM_NOSLEEP));
220fb2f18f8Sesaxe }
221fb2f18f8Sesaxe 
222fb2f18f8Sesaxe /*
223fb2f18f8Sesaxe  * Class specific PG de-allocation
224fb2f18f8Sesaxe  */
225fb2f18f8Sesaxe static void
226fb2f18f8Sesaxe pg_cmt_free(pg_t *pg)
227fb2f18f8Sesaxe {
228fb2f18f8Sesaxe 	ASSERT(pg != NULL);
229fb2f18f8Sesaxe 	ASSERT(IS_CMT_PG(pg));
230fb2f18f8Sesaxe 
231fb2f18f8Sesaxe 	kmem_free((pg_cmt_t *)pg, sizeof (pg_cmt_t));
232fb2f18f8Sesaxe }
233fb2f18f8Sesaxe 
234fb2f18f8Sesaxe /*
2350e751525SEric Saxe  * Given a hardware sharing relationship, return which dispatcher
2360e751525SEric Saxe  * policies should be implemented to optimize performance and efficiency
237fb2f18f8Sesaxe  */
2380e751525SEric Saxe static pg_cmt_policy_t
2390e751525SEric Saxe pg_cmt_policy(pghw_type_t hw)
240fb2f18f8Sesaxe {
2410e751525SEric Saxe 	pg_cmt_policy_t p;
2420e751525SEric Saxe 
2430e751525SEric Saxe 	/*
2440e751525SEric Saxe 	 * Give the platform a chance to override the default
2450e751525SEric Saxe 	 */
2460e751525SEric Saxe 	if ((p = pg_plat_cmt_policy(hw)) != CMT_NO_POLICY)
2470e751525SEric Saxe 		return (p);
2480e751525SEric Saxe 
2490e751525SEric Saxe 	switch (hw) {
2500e751525SEric Saxe 	case PGHW_IPIPE:
2510e751525SEric Saxe 	case PGHW_FPU:
2520e751525SEric Saxe 	case PGHW_CHIP:
2530e751525SEric Saxe 		return (CMT_BALANCE);
2540e751525SEric Saxe 	case PGHW_CACHE:
2550e751525SEric Saxe 		return (CMT_AFFINITY);
2560e751525SEric Saxe 	case PGHW_POW_ACTIVE:
2570e751525SEric Saxe 	case PGHW_POW_IDLE:
2580e751525SEric Saxe 		return (CMT_BALANCE);
2590e751525SEric Saxe 	default:
2600e751525SEric Saxe 		return (CMT_NO_POLICY);
2610e751525SEric Saxe 	}
2620e751525SEric Saxe }
2630e751525SEric Saxe 
2640e751525SEric Saxe /*
2650e751525SEric Saxe  * Rank the importance of optimizing for the pg1 relationship vs.
2660e751525SEric Saxe  * the pg2 relationship.
2670e751525SEric Saxe  */
2680e751525SEric Saxe static pg_cmt_t *
2690e751525SEric Saxe pg_cmt_hier_rank(pg_cmt_t *pg1, pg_cmt_t *pg2)
2700e751525SEric Saxe {
2710e751525SEric Saxe 	pghw_type_t hw1 = ((pghw_t *)pg1)->pghw_hw;
2720e751525SEric Saxe 	pghw_type_t hw2 = ((pghw_t *)pg2)->pghw_hw;
2730e751525SEric Saxe 
2740e751525SEric Saxe 	/*
2750e751525SEric Saxe 	 * A power domain is only important if CPUPM is enabled.
2760e751525SEric Saxe 	 */
2770e751525SEric Saxe 	if (cpupm_get_policy() == CPUPM_POLICY_DISABLED) {
2780e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw1) && !PGHW_IS_PM_DOMAIN(hw2))
2790e751525SEric Saxe 			return (pg2);
2800e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw2) && !PGHW_IS_PM_DOMAIN(hw1))
2810e751525SEric Saxe 			return (pg1);
2820e751525SEric Saxe 	}
2830e751525SEric Saxe 
2840e751525SEric Saxe 	/*
2850e751525SEric Saxe 	 * Otherwise, ask the platform
2860e751525SEric Saxe 	 */
2870e751525SEric Saxe 	if (pg_plat_hw_rank(hw1, hw2) == hw1)
2880e751525SEric Saxe 		return (pg1);
2890e751525SEric Saxe 	else
2900e751525SEric Saxe 		return (pg2);
2910e751525SEric Saxe }
2920e751525SEric Saxe 
2930e751525SEric Saxe /*
2940e751525SEric Saxe  * Initialize CMT callbacks for the given PG
2950e751525SEric Saxe  */
2960e751525SEric Saxe static void
2970e751525SEric Saxe cmt_callback_init(pg_t *pg)
2980e751525SEric Saxe {
2990e751525SEric Saxe 	switch (((pghw_t *)pg)->pghw_hw) {
3000e751525SEric Saxe 	case PGHW_POW_ACTIVE:
3010e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch_pwr;
3020e751525SEric Saxe 		pg->pg_cb.thread_remain = cmt_ev_thread_remain_pwr;
3030e751525SEric Saxe 		break;
3040e751525SEric Saxe 	default:
3050e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch;
3060e751525SEric Saxe 
3070e751525SEric Saxe 	}
3080e751525SEric Saxe }
3090e751525SEric Saxe 
3100e751525SEric Saxe /*
3110e751525SEric Saxe  * Promote PG above it's current parent.
312*1a77c24bSEric Saxe  * This is only legal if PG has an equal or greater number of CPUs than its
313*1a77c24bSEric Saxe  * parent.
314*1a77c24bSEric Saxe  *
315*1a77c24bSEric Saxe  * This routine operates on the CPU specific processor group data (for the CPUs
316*1a77c24bSEric Saxe  * in the PG being promoted), and may be invoked from a context where one CPU's
317*1a77c24bSEric Saxe  * PG data is under construction. In this case the argument "pgdata", if not
318*1a77c24bSEric Saxe  * NULL, is a reference to the CPU's under-construction PG data.
3190e751525SEric Saxe  */
3200e751525SEric Saxe static void
321*1a77c24bSEric Saxe cmt_hier_promote(pg_cmt_t *pg, cpu_pg_t *pgdata)
3220e751525SEric Saxe {
3230e751525SEric Saxe 	pg_cmt_t	*parent;
3240e751525SEric Saxe 	group_t		*children;
3250e751525SEric Saxe 	cpu_t		*cpu;
3260e751525SEric Saxe 	group_iter_t	iter;
3270e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
3280e751525SEric Saxe 	int		r;
3290e751525SEric Saxe 	int		err;
3300e751525SEric Saxe 
3310e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
3320e751525SEric Saxe 
3330e751525SEric Saxe 	parent = pg->cmt_parent;
3340e751525SEric Saxe 	if (parent == NULL) {
3350e751525SEric Saxe 		/*
3360e751525SEric Saxe 		 * Nothing to do
3370e751525SEric Saxe 		 */
3380e751525SEric Saxe 		return;
3390e751525SEric Saxe 	}
3400e751525SEric Saxe 
3410e751525SEric Saxe 	ASSERT(PG_NUM_CPUS((pg_t *)pg) >= PG_NUM_CPUS((pg_t *)parent));
3420e751525SEric Saxe 
3430e751525SEric Saxe 	/*
3440e751525SEric Saxe 	 * We're changing around the hierarchy, which is actively traversed
3450e751525SEric Saxe 	 * by the dispatcher. Pause CPUS to ensure exclusivity.
3460e751525SEric Saxe 	 */
3470e751525SEric Saxe 	pause_cpus(NULL);
3480e751525SEric Saxe 
3490e751525SEric Saxe 	/*
3500e751525SEric Saxe 	 * If necessary, update the parent's sibling set, replacing parent
3510e751525SEric Saxe 	 * with PG.
3520e751525SEric Saxe 	 */
3530e751525SEric Saxe 	if (parent->cmt_siblings) {
3540e751525SEric Saxe 		if (group_remove(parent->cmt_siblings, parent, GRP_NORESIZE)
3550e751525SEric Saxe 		    != -1) {
3560e751525SEric Saxe 			r = group_add(parent->cmt_siblings, pg, GRP_NORESIZE);
3570e751525SEric Saxe 			ASSERT(r != -1);
3580e751525SEric Saxe 		}
3590e751525SEric Saxe 	}
3600e751525SEric Saxe 
3610e751525SEric Saxe 	/*
3620e751525SEric Saxe 	 * If the parent is at the top of the hierarchy, replace it's entry
3630e751525SEric Saxe 	 * in the root lgroup's group of top level PGs.
3640e751525SEric Saxe 	 */
3650e751525SEric Saxe 	if (parent->cmt_parent == NULL &&
3660e751525SEric Saxe 	    parent->cmt_siblings != &cmt_root->cl_pgs) {
3670e751525SEric Saxe 		if (group_remove(&cmt_root->cl_pgs, parent, GRP_NORESIZE)
3680e751525SEric Saxe 		    != -1) {
3690e751525SEric Saxe 			r = group_add(&cmt_root->cl_pgs, pg, GRP_NORESIZE);
3700e751525SEric Saxe 			ASSERT(r != -1);
3710e751525SEric Saxe 		}
3720e751525SEric Saxe 	}
3730e751525SEric Saxe 
3740e751525SEric Saxe 	/*
3750e751525SEric Saxe 	 * We assume (and therefore assert) that the PG being promoted is an
3760e751525SEric Saxe 	 * only child of it's parent. Update the parent's children set
3770e751525SEric Saxe 	 * replacing PG's entry with the parent (since the parent is becoming
3780e751525SEric Saxe 	 * the child). Then have PG and the parent swap children sets.
3790e751525SEric Saxe 	 */
3800e751525SEric Saxe 	ASSERT(GROUP_SIZE(parent->cmt_children) <= 1);
3810e751525SEric Saxe 	if (group_remove(parent->cmt_children, pg, GRP_NORESIZE) != -1) {
3820e751525SEric Saxe 		r = group_add(parent->cmt_children, parent, GRP_NORESIZE);
3830e751525SEric Saxe 		ASSERT(r != -1);
3840e751525SEric Saxe 	}
3850e751525SEric Saxe 
3860e751525SEric Saxe 	children = pg->cmt_children;
3870e751525SEric Saxe 	pg->cmt_children = parent->cmt_children;
3880e751525SEric Saxe 	parent->cmt_children = children;
3890e751525SEric Saxe 
3900e751525SEric Saxe 	/*
3910e751525SEric Saxe 	 * Update the sibling references for PG and it's parent
3920e751525SEric Saxe 	 */
3930e751525SEric Saxe 	pg->cmt_siblings = parent->cmt_siblings;
3940e751525SEric Saxe 	parent->cmt_siblings = pg->cmt_children;
3950e751525SEric Saxe 
3960e751525SEric Saxe 	/*
3970e751525SEric Saxe 	 * Update any cached lineages in the per CPU pg data.
3980e751525SEric Saxe 	 */
3990e751525SEric Saxe 	PG_CPU_ITR_INIT(pg, cpu_iter);
4000e751525SEric Saxe 	while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
4010e751525SEric Saxe 		int		idx;
4020e751525SEric Saxe 		pg_cmt_t	*cpu_pg;
403*1a77c24bSEric Saxe 		cpu_pg_t	*pgd;	/* CPU's PG data */
404*1a77c24bSEric Saxe 
405*1a77c24bSEric Saxe 		/*
406*1a77c24bSEric Saxe 		 * The CPU's whose lineage is under construction still
407*1a77c24bSEric Saxe 		 * references the bootstrap CPU PG data structure.
408*1a77c24bSEric Saxe 		 */
409*1a77c24bSEric Saxe 		if (pg_cpu_is_bootstrapped(cpu))
410*1a77c24bSEric Saxe 			pgd = pgdata;
411*1a77c24bSEric Saxe 		else
412*1a77c24bSEric Saxe 			pgd = cpu->cpu_pg;
4130e751525SEric Saxe 
4140e751525SEric Saxe 		/*
4150e751525SEric Saxe 		 * Iterate over the CPU's PGs updating the children
4160e751525SEric Saxe 		 * of the PG being promoted, since they have a new parent.
4170e751525SEric Saxe 		 */
4180e751525SEric Saxe 		group_iter_init(&iter);
419*1a77c24bSEric Saxe 		while ((cpu_pg = group_iterate(&pgd->cmt_pgs, &iter)) != NULL) {
4200e751525SEric Saxe 			if (cpu_pg->cmt_parent == pg) {
4210e751525SEric Saxe 				cpu_pg->cmt_parent = parent;
4220e751525SEric Saxe 			}
4230e751525SEric Saxe 		}
4240e751525SEric Saxe 
4250e751525SEric Saxe 		/*
4260e751525SEric Saxe 		 * Update the CMT load balancing lineage
4270e751525SEric Saxe 		 */
428*1a77c24bSEric Saxe 		if ((idx = group_find(&pgd->cmt_pgs, (void *)pg)) == -1) {
4290e751525SEric Saxe 			/*
4300e751525SEric Saxe 			 * Unless this is the CPU who's lineage is being
4310e751525SEric Saxe 			 * constructed, the PG being promoted should be
4320e751525SEric Saxe 			 * in the lineage.
4330e751525SEric Saxe 			 */
434*1a77c24bSEric Saxe 			ASSERT(pg_cpu_is_bootstrapped(cpu));
4350e751525SEric Saxe 			continue;
4360e751525SEric Saxe 		}
4370e751525SEric Saxe 
438*1a77c24bSEric Saxe 		ASSERT(GROUP_ACCESS(&pgd->cmt_pgs, idx - 1) == parent);
4390e751525SEric Saxe 		ASSERT(idx > 0);
4400e751525SEric Saxe 
4410e751525SEric Saxe 		/*
4420e751525SEric Saxe 		 * Have the child and the parent swap places in the CPU's
4430e751525SEric Saxe 		 * lineage
4440e751525SEric Saxe 		 */
445*1a77c24bSEric Saxe 		group_remove_at(&pgd->cmt_pgs, idx);
446*1a77c24bSEric Saxe 		group_remove_at(&pgd->cmt_pgs, idx - 1);
447*1a77c24bSEric Saxe 		err = group_add_at(&pgd->cmt_pgs, parent, idx);
4480e751525SEric Saxe 		ASSERT(err == 0);
449*1a77c24bSEric Saxe 		err = group_add_at(&pgd->cmt_pgs, pg, idx - 1);
4500e751525SEric Saxe 		ASSERT(err == 0);
4510e751525SEric Saxe 	}
4520e751525SEric Saxe 
4530e751525SEric Saxe 	/*
4540e751525SEric Saxe 	 * Update the parent references for PG and it's parent
4550e751525SEric Saxe 	 */
4560e751525SEric Saxe 	pg->cmt_parent = parent->cmt_parent;
4570e751525SEric Saxe 	parent->cmt_parent = pg;
4580e751525SEric Saxe 
4590e751525SEric Saxe 	start_cpus();
460fb2f18f8Sesaxe }
461fb2f18f8Sesaxe 
462fb2f18f8Sesaxe /*
463fb2f18f8Sesaxe  * CMT class callback for a new CPU entering the system
464*1a77c24bSEric Saxe  *
465*1a77c24bSEric Saxe  * This routine operates on the CPU specific processor group data (for the CPU
466*1a77c24bSEric Saxe  * being initialized). The argument "pgdata" is a reference to the CPU's PG
467*1a77c24bSEric Saxe  * data to be constructed.
468*1a77c24bSEric Saxe  *
469*1a77c24bSEric Saxe  * cp->cpu_pg is used by the dispatcher to access the CPU's PG data
470*1a77c24bSEric Saxe  * references a "bootstrap" structure. pg_cmt_cpu_init() and the routines it
471*1a77c24bSEric Saxe  * calls must be careful to operate only on the "pgdata" argument, and not
472*1a77c24bSEric Saxe  * cp->cpu_pg.
473fb2f18f8Sesaxe  */
474fb2f18f8Sesaxe static void
475*1a77c24bSEric Saxe pg_cmt_cpu_init(cpu_t *cp, cpu_pg_t *pgdata)
476fb2f18f8Sesaxe {
477fb2f18f8Sesaxe 	pg_cmt_t	*pg;
478fb2f18f8Sesaxe 	group_t		*cmt_pgs;
4790e751525SEric Saxe 	int		levels, level;
480fb2f18f8Sesaxe 	pghw_type_t	hw;
481fb2f18f8Sesaxe 	pg_t		*pg_cache = NULL;
482fb2f18f8Sesaxe 	pg_cmt_t	*cpu_cmt_hier[PGHW_NUM_COMPONENTS];
483fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
484fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
485ef4f35d8SEric Saxe 	cmt_lineage_validation_t	lineage_status;
486fb2f18f8Sesaxe 
487fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
488*1a77c24bSEric Saxe 	ASSERT(pg_cpu_is_bootstrapped(cp));
489fb2f18f8Sesaxe 
4900e751525SEric Saxe 	if (cmt_sched_disabled)
4910e751525SEric Saxe 		return;
4920e751525SEric Saxe 
493fb2f18f8Sesaxe 	/*
494fb2f18f8Sesaxe 	 * A new CPU is coming into the system.
495fb2f18f8Sesaxe 	 * Interrogate the platform to see if the CPU
4960e751525SEric Saxe 	 * has any performance or efficiency relevant
4970e751525SEric Saxe 	 * sharing relationships
498fb2f18f8Sesaxe 	 */
499*1a77c24bSEric Saxe 	cmt_pgs = &pgdata->cmt_pgs;
500*1a77c24bSEric Saxe 	pgdata->cmt_lineage = NULL;
501fb2f18f8Sesaxe 
502fb2f18f8Sesaxe 	bzero(cpu_cmt_hier, sizeof (cpu_cmt_hier));
5030e751525SEric Saxe 	levels = 0;
504fb2f18f8Sesaxe 	for (hw = PGHW_START; hw < PGHW_NUM_COMPONENTS; hw++) {
505fb2f18f8Sesaxe 
5060e751525SEric Saxe 		pg_cmt_policy_t	policy;
5070e751525SEric Saxe 
508fb2f18f8Sesaxe 		/*
5090e751525SEric Saxe 		 * We're only interested in the hw sharing relationships
5100e751525SEric Saxe 		 * for which we know how to optimize.
511fb2f18f8Sesaxe 		 */
5120e751525SEric Saxe 		policy = pg_cmt_policy(hw);
5130e751525SEric Saxe 		if (policy == CMT_NO_POLICY ||
5140e751525SEric Saxe 		    pg_plat_hw_shared(cp, hw) == 0)
515fb2f18f8Sesaxe 			continue;
516fb2f18f8Sesaxe 
517fb2f18f8Sesaxe 		/*
5180e751525SEric Saxe 		 * Continue if the hardware sharing relationship has been
5190e751525SEric Saxe 		 * blacklisted.
5200e751525SEric Saxe 		 */
5210e751525SEric Saxe 		if (cmt_hw_blacklisted[hw]) {
5220e751525SEric Saxe 			continue;
5230e751525SEric Saxe 		}
5240e751525SEric Saxe 
5250e751525SEric Saxe 		/*
526fb2f18f8Sesaxe 		 * Find (or create) the PG associated with
527fb2f18f8Sesaxe 		 * the hw sharing relationship in which cp
528fb2f18f8Sesaxe 		 * belongs.
529fb2f18f8Sesaxe 		 *
530fb2f18f8Sesaxe 		 * Determine if a suitable PG already
531fb2f18f8Sesaxe 		 * exists, or if one needs to be created.
532fb2f18f8Sesaxe 		 */
533fb2f18f8Sesaxe 		pg = (pg_cmt_t *)pghw_place_cpu(cp, hw);
534fb2f18f8Sesaxe 		if (pg == NULL) {
535fb2f18f8Sesaxe 			/*
536fb2f18f8Sesaxe 			 * Create a new one.
537fb2f18f8Sesaxe 			 * Initialize the common...
538fb2f18f8Sesaxe 			 */
539fb2f18f8Sesaxe 			pg = (pg_cmt_t *)pg_create(pg_cmt_class_id);
540fb2f18f8Sesaxe 
541fb2f18f8Sesaxe 			/* ... physical ... */
542fb2f18f8Sesaxe 			pghw_init((pghw_t *)pg, cp, hw);
543fb2f18f8Sesaxe 
544fb2f18f8Sesaxe 			/*
545fb2f18f8Sesaxe 			 * ... and CMT specific portions of the
546fb2f18f8Sesaxe 			 * structure.
547fb2f18f8Sesaxe 			 */
5480e751525SEric Saxe 			pg->cmt_policy = policy;
5490e751525SEric Saxe 
5500e751525SEric Saxe 			/* CMT event callbacks */
5510e751525SEric Saxe 			cmt_callback_init((pg_t *)pg);
5520e751525SEric Saxe 
553fb2f18f8Sesaxe 			bitset_init(&pg->cmt_cpus_actv_set);
554fb2f18f8Sesaxe 			group_create(&pg->cmt_cpus_actv);
555fb2f18f8Sesaxe 		} else {
556fb2f18f8Sesaxe 			ASSERT(IS_CMT_PG(pg));
557fb2f18f8Sesaxe 		}
558fb2f18f8Sesaxe 
559fb2f18f8Sesaxe 		/* Add the CPU to the PG */
560*1a77c24bSEric Saxe 		pg_cpu_add((pg_t *)pg, cp, pgdata);
561fb2f18f8Sesaxe 
562fb2f18f8Sesaxe 		/*
5636890d023SEric Saxe 		 * Ensure capacity of the active CPU group/bitset
564fb2f18f8Sesaxe 		 */
565fb2f18f8Sesaxe 		group_expand(&pg->cmt_cpus_actv,
566fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
567fb2f18f8Sesaxe 
568fb2f18f8Sesaxe 		if (cp->cpu_seqid >=
569fb2f18f8Sesaxe 		    bitset_capacity(&pg->cmt_cpus_actv_set)) {
570fb2f18f8Sesaxe 			bitset_resize(&pg->cmt_cpus_actv_set,
571fb2f18f8Sesaxe 			    cp->cpu_seqid + 1);
572fb2f18f8Sesaxe 		}
573fb2f18f8Sesaxe 
574fb2f18f8Sesaxe 		/*
5750e751525SEric Saxe 		 * Build a lineage of CMT PGs for load balancing / coalescence
576fb2f18f8Sesaxe 		 */
5770e751525SEric Saxe 		if (policy & (CMT_BALANCE | CMT_COALESCE)) {
5780e751525SEric Saxe 			cpu_cmt_hier[levels++] = pg;
579fb2f18f8Sesaxe 		}
580fb2f18f8Sesaxe 
581fb2f18f8Sesaxe 		/* Cache this for later */
582fb2f18f8Sesaxe 		if (hw == PGHW_CACHE)
583fb2f18f8Sesaxe 			pg_cache = (pg_t *)pg;
584fb2f18f8Sesaxe 	}
585fb2f18f8Sesaxe 
5860e751525SEric Saxe 	group_expand(cmt_pgs, levels);
5876890d023SEric Saxe 
5886890d023SEric Saxe 	if (cmt_root == NULL)
5896890d023SEric Saxe 		cmt_root = pg_cmt_lgrp_create(lgrp_plat_root_hand());
590fb2f18f8Sesaxe 
591fb2f18f8Sesaxe 	/*
5920e751525SEric Saxe 	 * Find the lgrp that encapsulates this CPU's CMT hierarchy
5936890d023SEric Saxe 	 */
5946890d023SEric Saxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
5956890d023SEric Saxe 	if ((lgrp = pg_cmt_find_lgrp(lgrp_handle)) == NULL)
5966890d023SEric Saxe 		lgrp = pg_cmt_lgrp_create(lgrp_handle);
5976890d023SEric Saxe 
5986890d023SEric Saxe 	/*
5990e751525SEric Saxe 	 * Ascendingly sort the PGs in the lineage by number of CPUs
6000e751525SEric Saxe 	 */
6010e751525SEric Saxe 	pg_cmt_hier_sort(cpu_cmt_hier, levels);
6020e751525SEric Saxe 
6030e751525SEric Saxe 	/*
6040e751525SEric Saxe 	 * Examine the lineage and validate it.
6050e751525SEric Saxe 	 * This routine will also try to fix the lineage along with the
6060e751525SEric Saxe 	 * rest of the PG hierarchy should it detect an issue.
6070e751525SEric Saxe 	 *
608ef4f35d8SEric Saxe 	 * If it returns anything other than VALID or REPAIRED, an
609ef4f35d8SEric Saxe 	 * unrecoverable error has occurred, and we cannot proceed.
6100e751525SEric Saxe 	 */
611*1a77c24bSEric Saxe 	lineage_status = pg_cmt_lineage_validate(cpu_cmt_hier, &levels, pgdata);
612ef4f35d8SEric Saxe 	if ((lineage_status != CMT_LINEAGE_VALID) &&
613*1a77c24bSEric Saxe 	    (lineage_status != CMT_LINEAGE_REPAIRED)) {
614*1a77c24bSEric Saxe 		/*
615*1a77c24bSEric Saxe 		 * In the case of an unrecoverable error where CMT scheduling
616*1a77c24bSEric Saxe 		 * has been disabled, assert that the under construction CPU's
617*1a77c24bSEric Saxe 		 * PG data has an empty CMT load balancing lineage.
618*1a77c24bSEric Saxe 		 */
619*1a77c24bSEric Saxe 		ASSERT((cmt_sched_disabled == 0) ||
620*1a77c24bSEric Saxe 		    (GROUP_SIZE(&(pgdata->cmt_pgs)) == 0));
6210e751525SEric Saxe 		return;
622*1a77c24bSEric Saxe 	}
6230e751525SEric Saxe 
6240e751525SEric Saxe 	/*
6250e751525SEric Saxe 	 * For existing PGs in the lineage, verify that the parent is
6260e751525SEric Saxe 	 * correct, as the generation in the lineage may have changed
6270e751525SEric Saxe 	 * as a result of the sorting. Start the traversal at the top
6280e751525SEric Saxe 	 * of the lineage, moving down.
6290e751525SEric Saxe 	 */
6300e751525SEric Saxe 	for (level = levels - 1; level >= 0; ) {
6310e751525SEric Saxe 		int reorg;
6320e751525SEric Saxe 
6330e751525SEric Saxe 		reorg = 0;
6340e751525SEric Saxe 		pg = cpu_cmt_hier[level];
6350e751525SEric Saxe 
6360e751525SEric Saxe 		/*
6370e751525SEric Saxe 		 * Promote PGs at an incorrect generation into place.
6380e751525SEric Saxe 		 */
6390e751525SEric Saxe 		while (pg->cmt_parent &&
6400e751525SEric Saxe 		    pg->cmt_parent != cpu_cmt_hier[level + 1]) {
641*1a77c24bSEric Saxe 			cmt_hier_promote(pg, pgdata);
6420e751525SEric Saxe 			reorg++;
6430e751525SEric Saxe 		}
6440e751525SEric Saxe 		if (reorg > 0)
6450e751525SEric Saxe 			level = levels - 1;
6460e751525SEric Saxe 		else
6470e751525SEric Saxe 			level--;
6480e751525SEric Saxe 	}
6490e751525SEric Saxe 
6500e751525SEric Saxe 	/*
6516890d023SEric Saxe 	 * For each of the PGs in the CPU's lineage:
6520e751525SEric Saxe 	 *	- Add an entry in the CPU sorted CMT PG group
6530e751525SEric Saxe 	 *	  which is used for top down CMT load balancing
654fb2f18f8Sesaxe 	 *	- Tie the PG into the CMT hierarchy by connecting
655fb2f18f8Sesaxe 	 *	  it to it's parent and siblings.
656fb2f18f8Sesaxe 	 */
6570e751525SEric Saxe 	for (level = 0; level < levels; level++) {
658fb2f18f8Sesaxe 		uint_t		children;
659fb2f18f8Sesaxe 		int		err;
660fb2f18f8Sesaxe 
661fb2f18f8Sesaxe 		pg = cpu_cmt_hier[level];
6620e751525SEric Saxe 		err = group_add_at(cmt_pgs, pg, levels - level - 1);
663fb2f18f8Sesaxe 		ASSERT(err == 0);
664fb2f18f8Sesaxe 
665fb2f18f8Sesaxe 		if (level == 0)
666*1a77c24bSEric Saxe 			pgdata->cmt_lineage = (pg_t *)pg;
667fb2f18f8Sesaxe 
668fb2f18f8Sesaxe 		if (pg->cmt_siblings != NULL) {
669fb2f18f8Sesaxe 			/* Already initialized */
670fb2f18f8Sesaxe 			ASSERT(pg->cmt_parent == NULL ||
671fb2f18f8Sesaxe 			    pg->cmt_parent == cpu_cmt_hier[level + 1]);
672fb2f18f8Sesaxe 			ASSERT(pg->cmt_siblings == &lgrp->cl_pgs ||
673c416da2dSjb145095 			    ((pg->cmt_parent != NULL) &&
674c416da2dSjb145095 			    pg->cmt_siblings == pg->cmt_parent->cmt_children));
675fb2f18f8Sesaxe 			continue;
676fb2f18f8Sesaxe 		}
677fb2f18f8Sesaxe 
6780e751525SEric Saxe 		if ((level + 1) == levels) {
679fb2f18f8Sesaxe 			pg->cmt_parent = NULL;
6806890d023SEric Saxe 
681fb2f18f8Sesaxe 			pg->cmt_siblings = &lgrp->cl_pgs;
682fb2f18f8Sesaxe 			children = ++lgrp->cl_npgs;
6830e751525SEric Saxe 			if (cmt_root != lgrp)
6846890d023SEric Saxe 				cmt_root->cl_npgs++;
685fb2f18f8Sesaxe 		} else {
686fb2f18f8Sesaxe 			pg->cmt_parent = cpu_cmt_hier[level + 1];
687fb2f18f8Sesaxe 
688fb2f18f8Sesaxe 			/*
689fb2f18f8Sesaxe 			 * A good parent keeps track of their children.
690fb2f18f8Sesaxe 			 * The parent's children group is also the PG's
691fb2f18f8Sesaxe 			 * siblings.
692fb2f18f8Sesaxe 			 */
693fb2f18f8Sesaxe 			if (pg->cmt_parent->cmt_children == NULL) {
694fb2f18f8Sesaxe 				pg->cmt_parent->cmt_children =
695fb2f18f8Sesaxe 				    kmem_zalloc(sizeof (group_t), KM_SLEEP);
696fb2f18f8Sesaxe 				group_create(pg->cmt_parent->cmt_children);
697fb2f18f8Sesaxe 			}
698fb2f18f8Sesaxe 			pg->cmt_siblings = pg->cmt_parent->cmt_children;
699fb2f18f8Sesaxe 			children = ++pg->cmt_parent->cmt_nchildren;
700fb2f18f8Sesaxe 		}
7016890d023SEric Saxe 
702fb2f18f8Sesaxe 		group_expand(pg->cmt_siblings, children);
7036890d023SEric Saxe 		group_expand(&cmt_root->cl_pgs, cmt_root->cl_npgs);
704fb2f18f8Sesaxe 	}
705fb2f18f8Sesaxe 
706fb2f18f8Sesaxe 	/*
707fb2f18f8Sesaxe 	 * Cache the chip and core IDs in the cpu_t->cpu_physid structure
708fb2f18f8Sesaxe 	 * for fast lookups later.
709fb2f18f8Sesaxe 	 */
710fb2f18f8Sesaxe 	if (cp->cpu_physid) {
711fb2f18f8Sesaxe 		cp->cpu_physid->cpu_chipid =
712fb2f18f8Sesaxe 		    pg_plat_hw_instance_id(cp, PGHW_CHIP);
713fb2f18f8Sesaxe 		cp->cpu_physid->cpu_coreid = pg_plat_get_core_id(cp);
714fb2f18f8Sesaxe 
715fb2f18f8Sesaxe 		/*
716fb2f18f8Sesaxe 		 * If this cpu has a PG representing shared cache, then set
717fb2f18f8Sesaxe 		 * cpu_cacheid to that PG's logical id
718fb2f18f8Sesaxe 		 */
719fb2f18f8Sesaxe 		if (pg_cache)
720fb2f18f8Sesaxe 			cp->cpu_physid->cpu_cacheid = pg_cache->pg_id;
721fb2f18f8Sesaxe 	}
722fb2f18f8Sesaxe 
723fb2f18f8Sesaxe 	/* CPU0 only initialization */
724fb2f18f8Sesaxe 	if (is_cpu0) {
725fb2f18f8Sesaxe 		pg_cmt_cpu_startup(cp);
726fb2f18f8Sesaxe 		is_cpu0 = 0;
727a6604450Sesaxe 		cpu0_lgrp = lgrp;
728fb2f18f8Sesaxe 	}
729fb2f18f8Sesaxe 
730fb2f18f8Sesaxe }
731fb2f18f8Sesaxe 
732fb2f18f8Sesaxe /*
733fb2f18f8Sesaxe  * Class callback when a CPU is leaving the system (deletion)
734*1a77c24bSEric Saxe  *
735*1a77c24bSEric Saxe  * "pgdata" is a reference to the CPU's PG data to be deconstructed.
736*1a77c24bSEric Saxe  *
737*1a77c24bSEric Saxe  * cp->cpu_pg is used by the dispatcher to access the CPU's PG data
738*1a77c24bSEric Saxe  * references a "bootstrap" structure across this function's invocation.
739*1a77c24bSEric Saxe  * pg_cmt_cpu_init() and the routines it calls must be careful to operate only
740*1a77c24bSEric Saxe  * on the "pgdata" argument, and not cp->cpu_pg.
741fb2f18f8Sesaxe  */
742fb2f18f8Sesaxe static void
743*1a77c24bSEric Saxe pg_cmt_cpu_fini(cpu_t *cp, cpu_pg_t *pgdata)
744fb2f18f8Sesaxe {
745fb2f18f8Sesaxe 	group_iter_t	i;
746fb2f18f8Sesaxe 	pg_cmt_t	*pg;
747fb2f18f8Sesaxe 	group_t		*pgs, *cmt_pgs;
748fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
749fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
750fb2f18f8Sesaxe 
7510e751525SEric Saxe 	if (cmt_sched_disabled)
7520e751525SEric Saxe 		return;
7530e751525SEric Saxe 
754*1a77c24bSEric Saxe 	ASSERT(pg_cpu_is_bootstrapped(cp));
755*1a77c24bSEric Saxe 
756*1a77c24bSEric Saxe 	pgs = &pgdata->pgs;
757*1a77c24bSEric Saxe 	cmt_pgs = &pgdata->cmt_pgs;
758fb2f18f8Sesaxe 
759fb2f18f8Sesaxe 	/*
760fb2f18f8Sesaxe 	 * Find the lgroup that encapsulates this CPU's CMT hierarchy
761fb2f18f8Sesaxe 	 */
762fb2f18f8Sesaxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
763a6604450Sesaxe 
764fb2f18f8Sesaxe 	lgrp = pg_cmt_find_lgrp(lgrp_handle);
7653e81cacfSEric Saxe 	if (ncpus == 1 && lgrp != cpu0_lgrp) {
766a6604450Sesaxe 		/*
7673e81cacfSEric Saxe 		 * One might wonder how we could be deconfiguring the
7683e81cacfSEric Saxe 		 * only CPU in the system.
769a6604450Sesaxe 		 *
7703e81cacfSEric Saxe 		 * On Starcat systems when null_proc_lpa is detected,
7713e81cacfSEric Saxe 		 * the boot CPU (which is already configured into a leaf
7723e81cacfSEric Saxe 		 * lgroup), is moved into the root lgroup. This is done by
7733e81cacfSEric Saxe 		 * deconfiguring it from both lgroups and processor
7743e81cacfSEric Saxe 		 * groups), and then later reconfiguring it back in.  This
7753e81cacfSEric Saxe 		 * call to pg_cmt_cpu_fini() is part of that deconfiguration.
7763e81cacfSEric Saxe 		 *
7773e81cacfSEric Saxe 		 * This special case is detected by noting that the platform
7783e81cacfSEric Saxe 		 * has changed the CPU's lgrp affiliation (since it now
7793e81cacfSEric Saxe 		 * belongs in the root). In this case, use the cmt_lgrp_t
7803e81cacfSEric Saxe 		 * cached for the boot CPU, since this is what needs to be
7813e81cacfSEric Saxe 		 * torn down.
782a6604450Sesaxe 		 */
783a6604450Sesaxe 		lgrp = cpu0_lgrp;
784a6604450Sesaxe 	}
785fb2f18f8Sesaxe 
7863e81cacfSEric Saxe 	ASSERT(lgrp != NULL);
7873e81cacfSEric Saxe 
788fb2f18f8Sesaxe 	/*
789fb2f18f8Sesaxe 	 * First, clean up anything load balancing specific for each of
790fb2f18f8Sesaxe 	 * the CPU's PGs that participated in CMT load balancing
791fb2f18f8Sesaxe 	 */
792*1a77c24bSEric Saxe 	pg = (pg_cmt_t *)pgdata->cmt_lineage;
793fb2f18f8Sesaxe 	while (pg != NULL) {
794fb2f18f8Sesaxe 
795fb2f18f8Sesaxe 		/*
796fb2f18f8Sesaxe 		 * Remove the PG from the CPU's load balancing lineage
797fb2f18f8Sesaxe 		 */
798fb2f18f8Sesaxe 		(void) group_remove(cmt_pgs, pg, GRP_RESIZE);
799fb2f18f8Sesaxe 
800fb2f18f8Sesaxe 		/*
801fb2f18f8Sesaxe 		 * If it's about to become empty, destroy it's children
802fb2f18f8Sesaxe 		 * group, and remove it's reference from it's siblings.
803fb2f18f8Sesaxe 		 * This is done here (rather than below) to avoid removing
804fb2f18f8Sesaxe 		 * our reference from a PG that we just eliminated.
805fb2f18f8Sesaxe 		 */
806fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 1) {
807fb2f18f8Sesaxe 			if (pg->cmt_children != NULL)
808fb2f18f8Sesaxe 				group_destroy(pg->cmt_children);
809fb2f18f8Sesaxe 			if (pg->cmt_siblings != NULL) {
810fb2f18f8Sesaxe 				if (pg->cmt_siblings == &lgrp->cl_pgs)
811fb2f18f8Sesaxe 					lgrp->cl_npgs--;
812fb2f18f8Sesaxe 				else
813fb2f18f8Sesaxe 					pg->cmt_parent->cmt_nchildren--;
814fb2f18f8Sesaxe 			}
815fb2f18f8Sesaxe 		}
816fb2f18f8Sesaxe 		pg = pg->cmt_parent;
817fb2f18f8Sesaxe 	}
818fb2f18f8Sesaxe 	ASSERT(GROUP_SIZE(cmt_pgs) == 0);
819fb2f18f8Sesaxe 
820fb2f18f8Sesaxe 	/*
821fb2f18f8Sesaxe 	 * Now that the load balancing lineage updates have happened,
822fb2f18f8Sesaxe 	 * remove the CPU from all it's PGs (destroying any that become
823fb2f18f8Sesaxe 	 * empty).
824fb2f18f8Sesaxe 	 */
825fb2f18f8Sesaxe 	group_iter_init(&i);
826fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
827fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
828fb2f18f8Sesaxe 			continue;
829fb2f18f8Sesaxe 
830*1a77c24bSEric Saxe 		pg_cpu_delete((pg_t *)pg, cp, pgdata);
831fb2f18f8Sesaxe 		/*
832fb2f18f8Sesaxe 		 * Deleting the CPU from the PG changes the CPU's
833fb2f18f8Sesaxe 		 * PG group over which we are actively iterating
834fb2f18f8Sesaxe 		 * Re-initialize the iteration
835fb2f18f8Sesaxe 		 */
836fb2f18f8Sesaxe 		group_iter_init(&i);
837fb2f18f8Sesaxe 
838fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 0) {
839fb2f18f8Sesaxe 
840fb2f18f8Sesaxe 			/*
841fb2f18f8Sesaxe 			 * The PG has become zero sized, so destroy it.
842fb2f18f8Sesaxe 			 */
843fb2f18f8Sesaxe 			group_destroy(&pg->cmt_cpus_actv);
844fb2f18f8Sesaxe 			bitset_fini(&pg->cmt_cpus_actv_set);
845fb2f18f8Sesaxe 			pghw_fini((pghw_t *)pg);
846fb2f18f8Sesaxe 
847fb2f18f8Sesaxe 			pg_destroy((pg_t *)pg);
848fb2f18f8Sesaxe 		}
849fb2f18f8Sesaxe 	}
850fb2f18f8Sesaxe }
851fb2f18f8Sesaxe 
852fb2f18f8Sesaxe /*
853fb2f18f8Sesaxe  * Class callback when a CPU is entering a cpu partition
854fb2f18f8Sesaxe  */
855fb2f18f8Sesaxe static void
856fb2f18f8Sesaxe pg_cmt_cpupart_in(cpu_t *cp, cpupart_t *pp)
857fb2f18f8Sesaxe {
858fb2f18f8Sesaxe 	group_t		*pgs;
859fb2f18f8Sesaxe 	pg_t		*pg;
860fb2f18f8Sesaxe 	group_iter_t	i;
861fb2f18f8Sesaxe 
862fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
863fb2f18f8Sesaxe 
8640e751525SEric Saxe 	if (cmt_sched_disabled)
8650e751525SEric Saxe 		return;
8660e751525SEric Saxe 
867fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
868fb2f18f8Sesaxe 
869fb2f18f8Sesaxe 	/*
870fb2f18f8Sesaxe 	 * Ensure that the new partition's PG bitset
871fb2f18f8Sesaxe 	 * is large enough for all CMT PG's to which cp
872fb2f18f8Sesaxe 	 * belongs
873fb2f18f8Sesaxe 	 */
874fb2f18f8Sesaxe 	group_iter_init(&i);
875fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
876fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
877fb2f18f8Sesaxe 			continue;
878fb2f18f8Sesaxe 
879fb2f18f8Sesaxe 		if (bitset_capacity(&pp->cp_cmt_pgs) <= pg->pg_id)
880fb2f18f8Sesaxe 			bitset_resize(&pp->cp_cmt_pgs, pg->pg_id + 1);
881fb2f18f8Sesaxe 	}
882fb2f18f8Sesaxe }
883fb2f18f8Sesaxe 
884fb2f18f8Sesaxe /*
885fb2f18f8Sesaxe  * Class callback when a CPU is actually moving partitions
886fb2f18f8Sesaxe  */
887fb2f18f8Sesaxe static void
888fb2f18f8Sesaxe pg_cmt_cpupart_move(cpu_t *cp, cpupart_t *oldpp, cpupart_t *newpp)
889fb2f18f8Sesaxe {
890fb2f18f8Sesaxe 	cpu_t		*cpp;
891fb2f18f8Sesaxe 	group_t		*pgs;
892fb2f18f8Sesaxe 	pg_t		*pg;
893fb2f18f8Sesaxe 	group_iter_t	pg_iter;
894fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_iter;
895fb2f18f8Sesaxe 	boolean_t	found;
896fb2f18f8Sesaxe 
897fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
898fb2f18f8Sesaxe 
8990e751525SEric Saxe 	if (cmt_sched_disabled)
9000e751525SEric Saxe 		return;
9010e751525SEric Saxe 
902fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
903fb2f18f8Sesaxe 	group_iter_init(&pg_iter);
904fb2f18f8Sesaxe 
905fb2f18f8Sesaxe 	/*
906fb2f18f8Sesaxe 	 * Iterate over the CPUs CMT PGs
907fb2f18f8Sesaxe 	 */
908fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &pg_iter)) != NULL) {
909fb2f18f8Sesaxe 
910fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
911fb2f18f8Sesaxe 			continue;
912fb2f18f8Sesaxe 
913fb2f18f8Sesaxe 		/*
914fb2f18f8Sesaxe 		 * Add the PG to the bitset in the new partition.
915fb2f18f8Sesaxe 		 */
916fb2f18f8Sesaxe 		bitset_add(&newpp->cp_cmt_pgs, pg->pg_id);
917fb2f18f8Sesaxe 
918fb2f18f8Sesaxe 		/*
919fb2f18f8Sesaxe 		 * Remove the PG from the bitset in the old partition
920fb2f18f8Sesaxe 		 * if the last of the PG's CPUs have left.
921fb2f18f8Sesaxe 		 */
922fb2f18f8Sesaxe 		found = B_FALSE;
923fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
924fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_iter)) != NULL) {
925fb2f18f8Sesaxe 			if (cpp == cp)
926fb2f18f8Sesaxe 				continue;
927a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
928a6604450Sesaxe 			    cpp->cpu_part->cp_id == oldpp->cp_id) {
929fb2f18f8Sesaxe 				found = B_TRUE;
930fb2f18f8Sesaxe 				break;
931fb2f18f8Sesaxe 			}
932fb2f18f8Sesaxe 		}
933fb2f18f8Sesaxe 		if (!found)
934fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs, pg->pg_id);
935fb2f18f8Sesaxe 	}
936fb2f18f8Sesaxe }
937fb2f18f8Sesaxe 
938fb2f18f8Sesaxe /*
939fb2f18f8Sesaxe  * Class callback when a CPU becomes active (online)
940fb2f18f8Sesaxe  *
941fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
942fb2f18f8Sesaxe  */
943fb2f18f8Sesaxe static void
944fb2f18f8Sesaxe pg_cmt_cpu_active(cpu_t *cp)
945fb2f18f8Sesaxe {
946fb2f18f8Sesaxe 	int		err;
947fb2f18f8Sesaxe 	group_iter_t	i;
948fb2f18f8Sesaxe 	pg_cmt_t	*pg;
949fb2f18f8Sesaxe 	group_t		*pgs;
950fb2f18f8Sesaxe 
951fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
952fb2f18f8Sesaxe 
9530e751525SEric Saxe 	if (cmt_sched_disabled)
9540e751525SEric Saxe 		return;
9550e751525SEric Saxe 
956fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
957fb2f18f8Sesaxe 	group_iter_init(&i);
958fb2f18f8Sesaxe 
959fb2f18f8Sesaxe 	/*
960fb2f18f8Sesaxe 	 * Iterate over the CPU's PGs
961fb2f18f8Sesaxe 	 */
962fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
963fb2f18f8Sesaxe 
964fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
965fb2f18f8Sesaxe 			continue;
966fb2f18f8Sesaxe 
967fb2f18f8Sesaxe 		err = group_add(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
968fb2f18f8Sesaxe 		ASSERT(err == 0);
969fb2f18f8Sesaxe 
970fb2f18f8Sesaxe 		/*
971fb2f18f8Sesaxe 		 * If this is the first active CPU in the PG, and it
972fb2f18f8Sesaxe 		 * represents a hardware sharing relationship over which
973fb2f18f8Sesaxe 		 * CMT load balancing is performed, add it as a candidate
974fb2f18f8Sesaxe 		 * for balancing with it's siblings.
975fb2f18f8Sesaxe 		 */
976fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 1 &&
9770e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
978fb2f18f8Sesaxe 			err = group_add(pg->cmt_siblings, pg, GRP_NORESIZE);
979fb2f18f8Sesaxe 			ASSERT(err == 0);
9806890d023SEric Saxe 
9816890d023SEric Saxe 			/*
9826890d023SEric Saxe 			 * If this is a top level PG, add it as a balancing
9830e751525SEric Saxe 			 * candidate when balancing within the root lgroup.
9846890d023SEric Saxe 			 */
9850e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
9860e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
9876890d023SEric Saxe 				err = group_add(&cmt_root->cl_pgs, pg,
9886890d023SEric Saxe 				    GRP_NORESIZE);
9896890d023SEric Saxe 				ASSERT(err == 0);
9906890d023SEric Saxe 			}
991fb2f18f8Sesaxe 		}
992fb2f18f8Sesaxe 
993fb2f18f8Sesaxe 		/*
994fb2f18f8Sesaxe 		 * Notate the CPU in the PGs active CPU bitset.
995fb2f18f8Sesaxe 		 * Also notate the PG as being active in it's associated
996fb2f18f8Sesaxe 		 * partition
997fb2f18f8Sesaxe 		 */
998fb2f18f8Sesaxe 		bitset_add(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
999fb2f18f8Sesaxe 		bitset_add(&cp->cpu_part->cp_cmt_pgs, ((pg_t *)pg)->pg_id);
1000fb2f18f8Sesaxe 	}
1001fb2f18f8Sesaxe }
1002fb2f18f8Sesaxe 
1003fb2f18f8Sesaxe /*
1004fb2f18f8Sesaxe  * Class callback when a CPU goes inactive (offline)
1005fb2f18f8Sesaxe  *
1006fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
1007fb2f18f8Sesaxe  */
1008fb2f18f8Sesaxe static void
1009fb2f18f8Sesaxe pg_cmt_cpu_inactive(cpu_t *cp)
1010fb2f18f8Sesaxe {
1011fb2f18f8Sesaxe 	int		err;
1012fb2f18f8Sesaxe 	group_t		*pgs;
1013fb2f18f8Sesaxe 	pg_cmt_t	*pg;
1014fb2f18f8Sesaxe 	cpu_t		*cpp;
1015fb2f18f8Sesaxe 	group_iter_t	i;
1016fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_itr;
1017fb2f18f8Sesaxe 	boolean_t	found;
1018fb2f18f8Sesaxe 
1019fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1020fb2f18f8Sesaxe 
10210e751525SEric Saxe 	if (cmt_sched_disabled)
10220e751525SEric Saxe 		return;
10230e751525SEric Saxe 
1024fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
1025fb2f18f8Sesaxe 	group_iter_init(&i);
1026fb2f18f8Sesaxe 
1027fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
1028fb2f18f8Sesaxe 
1029fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
1030fb2f18f8Sesaxe 			continue;
1031fb2f18f8Sesaxe 
1032fb2f18f8Sesaxe 		/*
1033fb2f18f8Sesaxe 		 * Remove the CPU from the CMT PGs active CPU group
1034fb2f18f8Sesaxe 		 * bitmap
1035fb2f18f8Sesaxe 		 */
1036fb2f18f8Sesaxe 		err = group_remove(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
1037fb2f18f8Sesaxe 		ASSERT(err == 0);
1038fb2f18f8Sesaxe 
1039fb2f18f8Sesaxe 		bitset_del(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
1040fb2f18f8Sesaxe 
1041fb2f18f8Sesaxe 		/*
1042fb2f18f8Sesaxe 		 * If there are no more active CPUs in this PG over which
1043fb2f18f8Sesaxe 		 * load was balanced, remove it as a balancing candidate.
1044fb2f18f8Sesaxe 		 */
1045fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 0 &&
10460e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
1047fb2f18f8Sesaxe 			err = group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
1048fb2f18f8Sesaxe 			ASSERT(err == 0);
10496890d023SEric Saxe 
10500e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
10510e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
10526890d023SEric Saxe 				err = group_remove(&cmt_root->cl_pgs, pg,
10536890d023SEric Saxe 				    GRP_NORESIZE);
10546890d023SEric Saxe 				ASSERT(err == 0);
10556890d023SEric Saxe 			}
1056fb2f18f8Sesaxe 		}
1057fb2f18f8Sesaxe 
1058fb2f18f8Sesaxe 		/*
1059fb2f18f8Sesaxe 		 * Assert the number of active CPUs does not exceed
1060fb2f18f8Sesaxe 		 * the total number of CPUs in the PG
1061fb2f18f8Sesaxe 		 */
1062fb2f18f8Sesaxe 		ASSERT(GROUP_SIZE(&pg->cmt_cpus_actv) <=
1063fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
1064fb2f18f8Sesaxe 
1065fb2f18f8Sesaxe 		/*
1066fb2f18f8Sesaxe 		 * Update the PG bitset in the CPU's old partition
1067fb2f18f8Sesaxe 		 */
1068fb2f18f8Sesaxe 		found = B_FALSE;
1069fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_itr);
1070fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_itr)) != NULL) {
1071fb2f18f8Sesaxe 			if (cpp == cp)
1072fb2f18f8Sesaxe 				continue;
1073a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
1074a6604450Sesaxe 			    cpp->cpu_part->cp_id == cp->cpu_part->cp_id) {
1075fb2f18f8Sesaxe 				found = B_TRUE;
1076fb2f18f8Sesaxe 				break;
1077fb2f18f8Sesaxe 			}
1078fb2f18f8Sesaxe 		}
1079fb2f18f8Sesaxe 		if (!found) {
1080fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs,
1081fb2f18f8Sesaxe 			    ((pg_t *)pg)->pg_id);
1082fb2f18f8Sesaxe 		}
1083fb2f18f8Sesaxe 	}
1084fb2f18f8Sesaxe }
1085fb2f18f8Sesaxe 
1086fb2f18f8Sesaxe /*
1087fb2f18f8Sesaxe  * Return non-zero if the CPU belongs in the given PG
1088fb2f18f8Sesaxe  */
1089fb2f18f8Sesaxe static int
1090fb2f18f8Sesaxe pg_cmt_cpu_belongs(pg_t *pg, cpu_t *cp)
1091fb2f18f8Sesaxe {
1092fb2f18f8Sesaxe 	cpu_t	*pg_cpu;
1093fb2f18f8Sesaxe 
1094fb2f18f8Sesaxe 	pg_cpu = GROUP_ACCESS(&pg->pg_cpus, 0);
1095fb2f18f8Sesaxe 
1096fb2f18f8Sesaxe 	ASSERT(pg_cpu != NULL);
1097fb2f18f8Sesaxe 
1098fb2f18f8Sesaxe 	/*
1099fb2f18f8Sesaxe 	 * The CPU belongs if, given the nature of the hardware sharing
1100fb2f18f8Sesaxe 	 * relationship represented by the PG, the CPU has that
1101fb2f18f8Sesaxe 	 * relationship with some other CPU already in the PG
1102fb2f18f8Sesaxe 	 */
1103fb2f18f8Sesaxe 	if (pg_plat_cpus_share(cp, pg_cpu, ((pghw_t *)pg)->pghw_hw))
1104fb2f18f8Sesaxe 		return (1);
1105fb2f18f8Sesaxe 
1106fb2f18f8Sesaxe 	return (0);
1107fb2f18f8Sesaxe }
1108fb2f18f8Sesaxe 
1109fb2f18f8Sesaxe /*
11100e751525SEric Saxe  * Sort the CPUs CMT hierarchy, where "size" is the number of levels.
1111fb2f18f8Sesaxe  */
1112fb2f18f8Sesaxe static void
11130e751525SEric Saxe pg_cmt_hier_sort(pg_cmt_t **hier, int size)
1114fb2f18f8Sesaxe {
11150e751525SEric Saxe 	int		i, j, inc;
11160e751525SEric Saxe 	pg_t		*tmp;
11170e751525SEric Saxe 	pg_t		**h = (pg_t **)hier;
1118fb2f18f8Sesaxe 
11190e751525SEric Saxe 	/*
11200e751525SEric Saxe 	 * First sort by number of CPUs
11210e751525SEric Saxe 	 */
11220e751525SEric Saxe 	inc = size / 2;
11230e751525SEric Saxe 	while (inc > 0) {
11240e751525SEric Saxe 		for (i = inc; i < size; i++) {
11250e751525SEric Saxe 			j = i;
11260e751525SEric Saxe 			tmp = h[i];
11270e751525SEric Saxe 			while ((j >= inc) &&
11280e751525SEric Saxe 			    (PG_NUM_CPUS(h[j - inc]) > PG_NUM_CPUS(tmp))) {
11290e751525SEric Saxe 				h[j] = h[j - inc];
11300e751525SEric Saxe 				j = j - inc;
11310e751525SEric Saxe 			}
11320e751525SEric Saxe 			h[j] = tmp;
11330e751525SEric Saxe 		}
11340e751525SEric Saxe 		if (inc == 2)
11350e751525SEric Saxe 			inc = 1;
11360e751525SEric Saxe 		else
11370e751525SEric Saxe 			inc = (inc * 5) / 11;
11380e751525SEric Saxe 	}
1139fb2f18f8Sesaxe 
11400e751525SEric Saxe 	/*
11410e751525SEric Saxe 	 * Break ties by asking the platform.
11420e751525SEric Saxe 	 * Determine if h[i] outranks h[i + 1] and if so, swap them.
11430e751525SEric Saxe 	 */
11440e751525SEric Saxe 	for (i = 0; i < size - 1; i++) {
11450e751525SEric Saxe 		if ((PG_NUM_CPUS(h[i]) == PG_NUM_CPUS(h[i + 1])) &&
11460e751525SEric Saxe 		    pg_cmt_hier_rank(hier[i], hier[i + 1]) == hier[i]) {
11470e751525SEric Saxe 			tmp = h[i];
11480e751525SEric Saxe 			h[i] = h[i + 1];
11490e751525SEric Saxe 			h[i + 1] = tmp;
1150fb2f18f8Sesaxe 		}
1151fb2f18f8Sesaxe 	}
1152fb2f18f8Sesaxe }
1153fb2f18f8Sesaxe 
1154fb2f18f8Sesaxe /*
1155fb2f18f8Sesaxe  * Return a cmt_lgrp_t * given an lgroup handle.
1156fb2f18f8Sesaxe  */
1157fb2f18f8Sesaxe static cmt_lgrp_t *
1158fb2f18f8Sesaxe pg_cmt_find_lgrp(lgrp_handle_t hand)
1159fb2f18f8Sesaxe {
1160fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
1161fb2f18f8Sesaxe 
1162fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1163fb2f18f8Sesaxe 
1164fb2f18f8Sesaxe 	lgrp = cmt_lgrps;
1165fb2f18f8Sesaxe 	while (lgrp != NULL) {
1166fb2f18f8Sesaxe 		if (lgrp->cl_hand == hand)
1167a6604450Sesaxe 			break;
1168fb2f18f8Sesaxe 		lgrp = lgrp->cl_next;
1169fb2f18f8Sesaxe 	}
1170a6604450Sesaxe 	return (lgrp);
1171a6604450Sesaxe }
1172fb2f18f8Sesaxe 
1173fb2f18f8Sesaxe /*
1174a6604450Sesaxe  * Create a cmt_lgrp_t with the specified handle.
1175fb2f18f8Sesaxe  */
1176a6604450Sesaxe static cmt_lgrp_t *
1177a6604450Sesaxe pg_cmt_lgrp_create(lgrp_handle_t hand)
1178a6604450Sesaxe {
1179a6604450Sesaxe 	cmt_lgrp_t	*lgrp;
1180a6604450Sesaxe 
1181a6604450Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1182a6604450Sesaxe 
1183fb2f18f8Sesaxe 	lgrp = kmem_zalloc(sizeof (cmt_lgrp_t), KM_SLEEP);
1184fb2f18f8Sesaxe 
1185fb2f18f8Sesaxe 	lgrp->cl_hand = hand;
1186fb2f18f8Sesaxe 	lgrp->cl_npgs = 0;
1187fb2f18f8Sesaxe 	lgrp->cl_next = cmt_lgrps;
1188fb2f18f8Sesaxe 	cmt_lgrps = lgrp;
1189fb2f18f8Sesaxe 	group_create(&lgrp->cl_pgs);
1190fb2f18f8Sesaxe 
1191fb2f18f8Sesaxe 	return (lgrp);
1192fb2f18f8Sesaxe }
11936890d023SEric Saxe 
11946890d023SEric Saxe /*
11950e751525SEric Saxe  * Interfaces to enable and disable power aware dispatching
11960e751525SEric Saxe  * The caller must be holding cpu_lock.
11976890d023SEric Saxe  *
11980e751525SEric Saxe  * Return 0 on success and -1 on failure.
11996890d023SEric Saxe  */
12000e751525SEric Saxe int
12010e751525SEric Saxe cmt_pad_enable(pghw_type_t type)
12026890d023SEric Saxe {
12030e751525SEric Saxe 	group_t		*hwset;
12040e751525SEric Saxe 	group_iter_t	iter;
12050e751525SEric Saxe 	pg_cmt_t	*pg;
12066890d023SEric Saxe 
12070e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
12080e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
12096890d023SEric Saxe 
12100e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL ||
12110e751525SEric Saxe 	    cmt_hw_blacklisted[type]) {
12120e751525SEric Saxe 		/*
12130e751525SEric Saxe 		 * Unable to find any instances of the specified type
12140e751525SEric Saxe 		 * of power domain, or the power domains have been blacklisted.
12150e751525SEric Saxe 		 */
12160e751525SEric Saxe 		return (-1);
12170e751525SEric Saxe 	}
12186890d023SEric Saxe 
12196890d023SEric Saxe 	/*
12200e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
12210e751525SEric Saxe 	 * policy for power/performance optimization.
12220e751525SEric Saxe 	 *
12230e751525SEric Saxe 	 * Simply setting the policy isn't enough in the case where the power
12240e751525SEric Saxe 	 * domain is an only child of another PG. Because the dispatcher walks
12250e751525SEric Saxe 	 * the PG hierarchy in a top down fashion, the higher up PG's policy
12260e751525SEric Saxe 	 * will dominate. So promote the power domain above it's parent if both
12270e751525SEric Saxe 	 * PG and it's parent have the same CPUs to ensure it's policy
12280e751525SEric Saxe 	 * dominates.
12296890d023SEric Saxe 	 */
12300e751525SEric Saxe 	group_iter_init(&iter);
12310e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
12320e751525SEric Saxe 		/*
12330e751525SEric Saxe 		 * If the power domain is an only child to a parent
12340e751525SEric Saxe 		 * not implementing the same policy, promote the child
12350e751525SEric Saxe 		 * above the parent to activate the policy.
12360e751525SEric Saxe 		 */
12370e751525SEric Saxe 		pg->cmt_policy = pg_cmt_policy(((pghw_t *)pg)->pghw_hw);
12380e751525SEric Saxe 		while ((pg->cmt_parent != NULL) &&
12390e751525SEric Saxe 		    (pg->cmt_parent->cmt_policy != pg->cmt_policy) &&
12400e751525SEric Saxe 		    (PG_NUM_CPUS((pg_t *)pg) ==
12410e751525SEric Saxe 		    PG_NUM_CPUS((pg_t *)pg->cmt_parent))) {
1242*1a77c24bSEric Saxe 			cmt_hier_promote(pg, NULL);
12430e751525SEric Saxe 		}
12440e751525SEric Saxe 	}
12450e751525SEric Saxe 
12460e751525SEric Saxe 	return (0);
12470e751525SEric Saxe }
12480e751525SEric Saxe 
12490e751525SEric Saxe int
12500e751525SEric Saxe cmt_pad_disable(pghw_type_t type)
12510e751525SEric Saxe {
12520e751525SEric Saxe 	group_t		*hwset;
12530e751525SEric Saxe 	group_iter_t	iter;
12540e751525SEric Saxe 	pg_cmt_t	*pg;
12550e751525SEric Saxe 	pg_cmt_t	*child;
12560e751525SEric Saxe 
12570e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
12580e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
12590e751525SEric Saxe 
12600e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL) {
12610e751525SEric Saxe 		/*
12620e751525SEric Saxe 		 * Unable to find any instances of the specified type of
12630e751525SEric Saxe 		 * power domain.
12640e751525SEric Saxe 		 */
12650e751525SEric Saxe 		return (-1);
12660e751525SEric Saxe 	}
12670e751525SEric Saxe 	/*
12680e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
12690e751525SEric Saxe 	 * policy for performance optimization (load balancing).
12700e751525SEric Saxe 	 */
12710e751525SEric Saxe 	group_iter_init(&iter);
12720e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
12730e751525SEric Saxe 
12740e751525SEric Saxe 		/*
12750e751525SEric Saxe 		 * If the power domain has an only child that implements
12760e751525SEric Saxe 		 * policy other than load balancing, promote the child
12770e751525SEric Saxe 		 * above the power domain to ensure it's policy dominates.
12780e751525SEric Saxe 		 */
1279f03808b6SEric Saxe 		if (pg->cmt_children != NULL &&
1280f03808b6SEric Saxe 		    GROUP_SIZE(pg->cmt_children) == 1) {
12810e751525SEric Saxe 			child = GROUP_ACCESS(pg->cmt_children, 0);
12820e751525SEric Saxe 			if ((child->cmt_policy & CMT_BALANCE) == 0) {
1283*1a77c24bSEric Saxe 				cmt_hier_promote(child, NULL);
12840e751525SEric Saxe 			}
12850e751525SEric Saxe 		}
12860e751525SEric Saxe 		pg->cmt_policy = CMT_BALANCE;
12870e751525SEric Saxe 	}
12880e751525SEric Saxe 	return (0);
12890e751525SEric Saxe }
12900e751525SEric Saxe 
12910e751525SEric Saxe /* ARGSUSED */
12920e751525SEric Saxe static void
12930e751525SEric Saxe cmt_ev_thread_swtch(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
12940e751525SEric Saxe 		    kthread_t *new)
12950e751525SEric Saxe {
12960e751525SEric Saxe 	pg_cmt_t	*cmt_pg = (pg_cmt_t *)pg;
12970e751525SEric Saxe 
12980e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
12990e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, 1);
13000e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
13010e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, -1);
13020e751525SEric Saxe 	}
13030e751525SEric Saxe }
13040e751525SEric Saxe 
13050e751525SEric Saxe /*
13060e751525SEric Saxe  * Macro to test whether a thread is currently runnable on a CPU in a PG.
13070e751525SEric Saxe  */
13080e751525SEric Saxe #define	THREAD_RUNNABLE_IN_PG(t, pg)					\
13090e751525SEric Saxe 	((t)->t_state == TS_RUN &&					\
13100e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu &&				\
13110e751525SEric Saxe 	    bitset_in_set(&(pg)->cmt_cpus_actv_set,			\
13120e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu->cpu_seqid))
13130e751525SEric Saxe 
13140e751525SEric Saxe static void
13150e751525SEric Saxe cmt_ev_thread_swtch_pwr(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
13160e751525SEric Saxe     kthread_t *new)
13170e751525SEric Saxe {
13180e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
13190e751525SEric Saxe 	cpupm_domain_t	*dom;
13200e751525SEric Saxe 	uint32_t	u;
13210e751525SEric Saxe 
13220e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
13230e751525SEric Saxe 		ASSERT(new != cp->cpu_idle_thread);
13240e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, 1);
13250e751525SEric Saxe 		if (u == 1) {
13260e751525SEric Saxe 			/*
13270e751525SEric Saxe 			 * Notify the CPU power manager that the domain
13280e751525SEric Saxe 			 * is non-idle.
13290e751525SEric Saxe 			 */
13300e751525SEric Saxe 			dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13310e751525SEric Saxe 			cpupm_utilization_event(cp, now, dom,
13320e751525SEric Saxe 			    CPUPM_DOM_BUSY_FROM_IDLE);
13330e751525SEric Saxe 		}
13340e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
13350e751525SEric Saxe 		ASSERT(old != cp->cpu_idle_thread);
13360e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, -1);
13370e751525SEric Saxe 		if (u == 0) {
13380e751525SEric Saxe 			/*
13390e751525SEric Saxe 			 * The domain is idle, notify the CPU power
13400e751525SEric Saxe 			 * manager.
13410e751525SEric Saxe 			 *
13420e751525SEric Saxe 			 * Avoid notifying if the thread is simply migrating
13430e751525SEric Saxe 			 * between CPUs in the domain.
13440e751525SEric Saxe 			 */
13450e751525SEric Saxe 			if (!THREAD_RUNNABLE_IN_PG(old, cmt)) {
13460e751525SEric Saxe 				dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13470e751525SEric Saxe 				cpupm_utilization_event(cp, now, dom,
13480e751525SEric Saxe 				    CPUPM_DOM_IDLE_FROM_BUSY);
13490e751525SEric Saxe 			}
13500e751525SEric Saxe 		}
13510e751525SEric Saxe 	}
13520e751525SEric Saxe }
13530e751525SEric Saxe 
13540e751525SEric Saxe /* ARGSUSED */
13550e751525SEric Saxe static void
13560e751525SEric Saxe cmt_ev_thread_remain_pwr(pg_t *pg, cpu_t *cp, kthread_t *t)
13570e751525SEric Saxe {
13580e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
13590e751525SEric Saxe 	cpupm_domain_t	*dom;
13600e751525SEric Saxe 
13610e751525SEric Saxe 	dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13620e751525SEric Saxe 	cpupm_utilization_event(cp, (hrtime_t)0, dom, CPUPM_DOM_REMAIN_BUSY);
13630e751525SEric Saxe }
13640e751525SEric Saxe 
13650e751525SEric Saxe /*
13660e751525SEric Saxe  * Return the name of the CMT scheduling policy
13670e751525SEric Saxe  * being implemented across this PG
13680e751525SEric Saxe  */
13690e751525SEric Saxe static char *
13700e751525SEric Saxe pg_cmt_policy_name(pg_t *pg)
13710e751525SEric Saxe {
13720e751525SEric Saxe 	pg_cmt_policy_t policy;
13730e751525SEric Saxe 
13740e751525SEric Saxe 	policy = ((pg_cmt_t *)pg)->cmt_policy;
13750e751525SEric Saxe 
13760e751525SEric Saxe 	if (policy & CMT_AFFINITY) {
13770e751525SEric Saxe 		if (policy & CMT_BALANCE)
13780e751525SEric Saxe 			return ("Load Balancing & Affinity");
13790e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13800e751525SEric Saxe 			return ("Load Coalescence & Affinity");
13816890d023SEric Saxe 		else
13820e751525SEric Saxe 			return ("Affinity");
13830e751525SEric Saxe 	} else {
13840e751525SEric Saxe 		if (policy & CMT_BALANCE)
13850e751525SEric Saxe 			return ("Load Balancing");
13860e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13870e751525SEric Saxe 			return ("Load Coalescence");
13880e751525SEric Saxe 		else
13890e751525SEric Saxe 			return ("None");
13900e751525SEric Saxe 	}
13910e751525SEric Saxe }
13926890d023SEric Saxe 
13936890d023SEric Saxe /*
13940e751525SEric Saxe  * Prune PG, and all other instances of PG's hardware sharing relationship
13950e751525SEric Saxe  * from the PG hierarchy.
1396*1a77c24bSEric Saxe  *
1397*1a77c24bSEric Saxe  * This routine operates on the CPU specific processor group data (for the CPUs
1398*1a77c24bSEric Saxe  * in the PG being pruned), and may be invoked from a context where one CPU's
1399*1a77c24bSEric Saxe  * PG data is under construction. In this case the argument "pgdata", if not
1400*1a77c24bSEric Saxe  * NULL, is a reference to the CPU's under-construction PG data.
14016890d023SEric Saxe  */
14020e751525SEric Saxe static int
1403*1a77c24bSEric Saxe pg_cmt_prune(pg_cmt_t *pg_bad, pg_cmt_t **lineage, int *sz, cpu_pg_t *pgdata)
14040e751525SEric Saxe {
14050e751525SEric Saxe 	group_t		*hwset, *children;
14060e751525SEric Saxe 	int		i, j, r, size = *sz;
14070e751525SEric Saxe 	group_iter_t	hw_iter, child_iter;
14080e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
14090e751525SEric Saxe 	pg_cmt_t	*pg, *child;
14100e751525SEric Saxe 	cpu_t		*cpu;
14110e751525SEric Saxe 	int		cap_needed;
14120e751525SEric Saxe 	pghw_type_t	hw;
14136890d023SEric Saxe 
14140e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
14156890d023SEric Saxe 
14160e751525SEric Saxe 	hw = ((pghw_t *)pg_bad)->pghw_hw;
14170e751525SEric Saxe 
14180e751525SEric Saxe 	if (hw == PGHW_POW_ACTIVE) {
14190e751525SEric Saxe 		cmn_err(CE_NOTE, "!Active CPUPM domain groups look suspect. "
14200e751525SEric Saxe 		    "Event Based CPUPM Unavailable");
14210e751525SEric Saxe 	} else if (hw == PGHW_POW_IDLE) {
14220e751525SEric Saxe 		cmn_err(CE_NOTE, "!Idle CPUPM domain groups look suspect. "
14230e751525SEric Saxe 		    "Dispatcher assisted CPUPM disabled.");
14240e751525SEric Saxe 	}
14256890d023SEric Saxe 
14266890d023SEric Saxe 	/*
14270e751525SEric Saxe 	 * Find and eliminate the PG from the lineage.
14286890d023SEric Saxe 	 */
14290e751525SEric Saxe 	for (i = 0; i < size; i++) {
14300e751525SEric Saxe 		if (lineage[i] == pg_bad) {
14310e751525SEric Saxe 			for (j = i; j < size - 1; j++)
14320e751525SEric Saxe 				lineage[j] = lineage[j + 1];
14330e751525SEric Saxe 			*sz = size - 1;
14340e751525SEric Saxe 			break;
14350e751525SEric Saxe 		}
14360e751525SEric Saxe 	}
14370e751525SEric Saxe 
14380e751525SEric Saxe 	/*
14390e751525SEric Saxe 	 * We'll prune all instances of the hardware sharing relationship
14400e751525SEric Saxe 	 * represented by pg. But before we do that (and pause CPUs) we need
14410e751525SEric Saxe 	 * to ensure the hierarchy's groups are properly sized.
14420e751525SEric Saxe 	 */
14430e751525SEric Saxe 	hwset = pghw_set_lookup(hw);
14440e751525SEric Saxe 
14450e751525SEric Saxe 	/*
14460e751525SEric Saxe 	 * Blacklist the hardware so that future groups won't be created.
14470e751525SEric Saxe 	 */
14480e751525SEric Saxe 	cmt_hw_blacklisted[hw] = 1;
14490e751525SEric Saxe 
14500e751525SEric Saxe 	/*
14510e751525SEric Saxe 	 * For each of the PGs being pruned, ensure sufficient capacity in
14520e751525SEric Saxe 	 * the siblings set for the PG's children
14530e751525SEric Saxe 	 */
14540e751525SEric Saxe 	group_iter_init(&hw_iter);
14550e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14560e751525SEric Saxe 		/*
14570e751525SEric Saxe 		 * PG is being pruned, but if it is bringing up more than
14580e751525SEric Saxe 		 * one child, ask for more capacity in the siblings group.
14590e751525SEric Saxe 		 */
14600e751525SEric Saxe 		cap_needed = 0;
14610e751525SEric Saxe 		if (pg->cmt_children &&
14620e751525SEric Saxe 		    GROUP_SIZE(pg->cmt_children) > 1) {
14630e751525SEric Saxe 			cap_needed = GROUP_SIZE(pg->cmt_children) - 1;
14640e751525SEric Saxe 
14650e751525SEric Saxe 			group_expand(pg->cmt_siblings,
14660e751525SEric Saxe 			    GROUP_SIZE(pg->cmt_siblings) + cap_needed);
14670e751525SEric Saxe 
14680e751525SEric Saxe 			/*
14690e751525SEric Saxe 			 * If this is a top level group, also ensure the
14700e751525SEric Saxe 			 * capacity in the root lgrp level CMT grouping.
14710e751525SEric Saxe 			 */
14720e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
14730e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
14740e751525SEric Saxe 				group_expand(&cmt_root->cl_pgs,
14750e751525SEric Saxe 				    GROUP_SIZE(&cmt_root->cl_pgs) + cap_needed);
14760e751525SEric Saxe 			}
14770e751525SEric Saxe 		}
14780e751525SEric Saxe 	}
14790e751525SEric Saxe 
14800e751525SEric Saxe 	/*
14810e751525SEric Saxe 	 * We're operating on the PG hierarchy. Pause CPUs to ensure
14820e751525SEric Saxe 	 * exclusivity with respect to the dispatcher.
14830e751525SEric Saxe 	 */
14840e751525SEric Saxe 	pause_cpus(NULL);
14850e751525SEric Saxe 
14860e751525SEric Saxe 	/*
14870e751525SEric Saxe 	 * Prune all PG instances of the hardware sharing relationship
14880e751525SEric Saxe 	 * represented by pg.
14890e751525SEric Saxe 	 */
14900e751525SEric Saxe 	group_iter_init(&hw_iter);
14910e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14920e751525SEric Saxe 
14930e751525SEric Saxe 		/*
14940e751525SEric Saxe 		 * Remove PG from it's group of siblings, if it's there.
14950e751525SEric Saxe 		 */
14960e751525SEric Saxe 		if (pg->cmt_siblings) {
14970e751525SEric Saxe 			(void) group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
14980e751525SEric Saxe 		}
14990e751525SEric Saxe 		if (pg->cmt_parent == NULL &&
15000e751525SEric Saxe 		    pg->cmt_siblings != &cmt_root->cl_pgs) {
15010e751525SEric Saxe 			(void) group_remove(&cmt_root->cl_pgs, pg,
15020e751525SEric Saxe 			    GRP_NORESIZE);
15030e751525SEric Saxe 		}
15040e751525SEric Saxe 		/*
1505ef4f35d8SEric Saxe 		 * Move PG's children from it's children set to it's parent's
1506ef4f35d8SEric Saxe 		 * children set. Note that the parent's children set, and PG's
1507ef4f35d8SEric Saxe 		 * siblings set are the same thing.
1508ef4f35d8SEric Saxe 		 *
1509ef4f35d8SEric Saxe 		 * Because we are iterating over the same group that we are
1510ef4f35d8SEric Saxe 		 * operating on (removing the children), first add all of PG's
1511ef4f35d8SEric Saxe 		 * children to the parent's children set, and once we are done
1512ef4f35d8SEric Saxe 		 * iterating, empty PG's children set.
15130e751525SEric Saxe 		 */
15140e751525SEric Saxe 		if (pg->cmt_children != NULL) {
15150e751525SEric Saxe 			children = pg->cmt_children;
15160e751525SEric Saxe 
15170e751525SEric Saxe 			group_iter_init(&child_iter);
15180e751525SEric Saxe 			while ((child = group_iterate(children, &child_iter))
15190e751525SEric Saxe 			    != NULL) {
1520ef4f35d8SEric Saxe 				if (pg->cmt_siblings != NULL) {
15210e751525SEric Saxe 					r = group_add(pg->cmt_siblings, child,
15220e751525SEric Saxe 					    GRP_NORESIZE);
15230e751525SEric Saxe 					ASSERT(r == 0);
15240e751525SEric Saxe 				}
15250e751525SEric Saxe 			}
1526ef4f35d8SEric Saxe 			group_empty(pg->cmt_children);
15270e751525SEric Saxe 		}
15280e751525SEric Saxe 
15290e751525SEric Saxe 		/*
15300e751525SEric Saxe 		 * Reset the callbacks to the defaults
15310e751525SEric Saxe 		 */
15320e751525SEric Saxe 		pg_callback_set_defaults((pg_t *)pg);
15330e751525SEric Saxe 
15340e751525SEric Saxe 		/*
15350e751525SEric Saxe 		 * Update all the CPU lineages in each of PG's CPUs
15360e751525SEric Saxe 		 */
15370e751525SEric Saxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
15380e751525SEric Saxe 		while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
15390e751525SEric Saxe 			pg_cmt_t	*cpu_pg;
15400e751525SEric Saxe 			group_iter_t	liter;	/* Iterator for the lineage */
1541*1a77c24bSEric Saxe 			cpu_pg_t	*cpd;	/* CPU's PG data */
1542*1a77c24bSEric Saxe 
1543*1a77c24bSEric Saxe 			/*
1544*1a77c24bSEric Saxe 			 * The CPU's lineage is under construction still
1545*1a77c24bSEric Saxe 			 * references the bootstrap CPU PG data structure.
1546*1a77c24bSEric Saxe 			 */
1547*1a77c24bSEric Saxe 			if (pg_cpu_is_bootstrapped(cpu))
1548*1a77c24bSEric Saxe 				cpd = pgdata;
1549*1a77c24bSEric Saxe 			else
1550*1a77c24bSEric Saxe 				cpd = cpu->cpu_pg;
15510e751525SEric Saxe 
15520e751525SEric Saxe 			/*
15530e751525SEric Saxe 			 * Iterate over the CPU's PGs updating the children
15540e751525SEric Saxe 			 * of the PG being promoted, since they have a new
15550e751525SEric Saxe 			 * parent and siblings set.
15560e751525SEric Saxe 			 */
15570e751525SEric Saxe 			group_iter_init(&liter);
1558*1a77c24bSEric Saxe 			while ((cpu_pg = group_iterate(&cpd->pgs,
1559*1a77c24bSEric Saxe 			    &liter)) != NULL) {
15600e751525SEric Saxe 				if (cpu_pg->cmt_parent == pg) {
15610e751525SEric Saxe 					cpu_pg->cmt_parent = pg->cmt_parent;
15620e751525SEric Saxe 					cpu_pg->cmt_siblings = pg->cmt_siblings;
15630e751525SEric Saxe 				}
15640e751525SEric Saxe 			}
15650e751525SEric Saxe 
15660e751525SEric Saxe 			/*
15670e751525SEric Saxe 			 * Update the CPU's lineages
15680e751525SEric Saxe 			 */
1569*1a77c24bSEric Saxe 			(void) group_remove(&cpd->pgs, pg, GRP_NORESIZE);
1570*1a77c24bSEric Saxe 			(void) group_remove(&cpd->cmt_pgs, pg, GRP_NORESIZE);
15710e751525SEric Saxe 		}
15720e751525SEric Saxe 	}
15730e751525SEric Saxe 	start_cpus();
15740e751525SEric Saxe 	return (0);
15750e751525SEric Saxe }
15760e751525SEric Saxe 
15770e751525SEric Saxe /*
15780e751525SEric Saxe  * Disable CMT scheduling
15790e751525SEric Saxe  */
15800e751525SEric Saxe static void
15810e751525SEric Saxe pg_cmt_disable(void)
15820e751525SEric Saxe {
15830e751525SEric Saxe 	cpu_t		*cpu;
15840e751525SEric Saxe 
1585*1a77c24bSEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1586*1a77c24bSEric Saxe 
15870e751525SEric Saxe 	pause_cpus(NULL);
15880e751525SEric Saxe 	cpu = cpu_list;
15890e751525SEric Saxe 
15906890d023SEric Saxe 	do {
15910e751525SEric Saxe 		if (cpu->cpu_pg)
15920e751525SEric Saxe 			group_empty(&cpu->cpu_pg->cmt_pgs);
15930e751525SEric Saxe 	} while ((cpu = cpu->cpu_next) != cpu_list);
15940e751525SEric Saxe 
15950e751525SEric Saxe 	cmt_sched_disabled = 1;
15960e751525SEric Saxe 	start_cpus();
15970e751525SEric Saxe 	cmn_err(CE_NOTE, "!CMT thread placement optimizations unavailable");
15980e751525SEric Saxe }
15990e751525SEric Saxe 
1600ef4f35d8SEric Saxe /*
1601ef4f35d8SEric Saxe  * CMT lineage validation
1602ef4f35d8SEric Saxe  *
1603ef4f35d8SEric Saxe  * This routine is invoked by pg_cmt_cpu_init() to validate the integrity
1604ef4f35d8SEric Saxe  * of the PGs in a CPU's lineage. This is necessary because it's possible that
1605ef4f35d8SEric Saxe  * some groupings (power domain groupings in particular) may be defined by
1606ef4f35d8SEric Saxe  * sources that are buggy (e.g. BIOS bugs). In such cases, it may not be
1607ef4f35d8SEric Saxe  * possible to integrate those groupings into the CMT PG hierarchy, if doing
1608ef4f35d8SEric Saxe  * so would violate the subset invariant of the hierarchy, which says that
1609ef4f35d8SEric Saxe  * a PG must be subset of its parent (if it has one).
1610ef4f35d8SEric Saxe  *
1611ef4f35d8SEric Saxe  * pg_cmt_lineage_validate()'s purpose is to detect grouping definitions that
1612ef4f35d8SEric Saxe  * would result in a violation of this invariant. If a violation is found,
1613ef4f35d8SEric Saxe  * and the PG is of a grouping type who's definition is known to originate from
1614ef4f35d8SEric Saxe  * suspect sources (BIOS), then pg_cmt_prune() will be invoked to prune the
1615ef4f35d8SEric Saxe  * PG (and all other instances PG's sharing relationship type) from the
1616ef4f35d8SEric Saxe  * hierarchy. Further, future instances of that sharing relationship type won't
1617ef4f35d8SEric Saxe  * be instantiated. If the grouping definition doesn't originate from suspect
1618ef4f35d8SEric Saxe  * sources, then pg_cmt_disable() will be invoked to log an error, and disable
1619ef4f35d8SEric Saxe  * CMT scheduling altogether.
1620ef4f35d8SEric Saxe  *
1621ef4f35d8SEric Saxe  * This routine is invoked after the CPU has been added to the PGs in which
1622ef4f35d8SEric Saxe  * it belongs, but before those PGs have been added to (or had their place
1623ef4f35d8SEric Saxe  * adjusted in) the CMT PG hierarchy.
1624ef4f35d8SEric Saxe  *
1625ef4f35d8SEric Saxe  * The first argument is the CPUs PG lineage (essentially an array of PGs in
1626ef4f35d8SEric Saxe  * which the CPU belongs) that has already been sorted in ascending order
1627ef4f35d8SEric Saxe  * by CPU count. Some of the PGs in the CPUs lineage may already have other
1628ef4f35d8SEric Saxe  * CPUs in them, and have already been integrated into the CMT hierarchy.
1629ef4f35d8SEric Saxe  *
1630ef4f35d8SEric Saxe  * The addition of this new CPU to these pre-existing PGs means that those
1631ef4f35d8SEric Saxe  * PGs may need to be promoted up in the hierarchy to satisfy the subset
1632ef4f35d8SEric Saxe  * invariant. In additon to testing the subset invariant for the lineage,
1633ef4f35d8SEric Saxe  * this routine also verifies that the addition of the new CPU to the
1634ef4f35d8SEric Saxe  * existing PGs wouldn't cause the subset invariant to be violated in
1635ef4f35d8SEric Saxe  * the exiting lineages.
1636ef4f35d8SEric Saxe  *
1637ef4f35d8SEric Saxe  * This routine will normally return one of the following:
1638ef4f35d8SEric Saxe  * CMT_LINEAGE_VALID - There were no problems detected with the lineage.
1639ef4f35d8SEric Saxe  * CMT_LINEAGE_REPAIRED - Problems were detected, but repaired via pruning.
1640ef4f35d8SEric Saxe  *
1641ef4f35d8SEric Saxe  * Otherwise, this routine will return a value indicating which error it
1642ef4f35d8SEric Saxe  * was unable to recover from (and set cmt_lineage_status along the way).
1643*1a77c24bSEric Saxe  *
1644*1a77c24bSEric Saxe  *
1645*1a77c24bSEric Saxe  * This routine operates on the CPU specific processor group data (for the CPU
1646*1a77c24bSEric Saxe  * whose lineage is being validated), which is under-construction.
1647*1a77c24bSEric Saxe  * "pgdata" is a reference to the CPU's under-construction PG data.
1648*1a77c24bSEric Saxe  * This routine must be careful to operate only on "pgdata", and not cp->cpu_pg.
1649ef4f35d8SEric Saxe  */
1650ef4f35d8SEric Saxe static cmt_lineage_validation_t
1651*1a77c24bSEric Saxe pg_cmt_lineage_validate(pg_cmt_t **lineage, int *sz, cpu_pg_t *pgdata)
16520e751525SEric Saxe {
1653ef4f35d8SEric Saxe 	int		i, j, size;
1654ef4f35d8SEric Saxe 	pg_cmt_t	*pg, *pg_next, *pg_bad, *pg_tmp;
16550e751525SEric Saxe 	cpu_t		*cp;
16560e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
1657ef4f35d8SEric Saxe 	lgrp_handle_t	lgrp;
16580e751525SEric Saxe 
16590e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
16600e751525SEric Saxe 
16610e751525SEric Saxe revalidate:
16620e751525SEric Saxe 	size = *sz;
16630e751525SEric Saxe 	pg_bad = NULL;
1664ef4f35d8SEric Saxe 	lgrp = LGRP_NULL_HANDLE;
1665ef4f35d8SEric Saxe 	for (i = 0; i < size; i++) {
16660e751525SEric Saxe 
16670e751525SEric Saxe 		pg = lineage[i];
1668ef4f35d8SEric Saxe 		if (i < size - 1)
1669ef4f35d8SEric Saxe 			pg_next = lineage[i + 1];
1670ef4f35d8SEric Saxe 		else
1671ef4f35d8SEric Saxe 			pg_next = NULL;
16726890d023SEric Saxe 
16736890d023SEric Saxe 		/*
16740e751525SEric Saxe 		 * We assume that the lineage has already been sorted
16750e751525SEric Saxe 		 * by the number of CPUs. In fact, we depend on it.
16766890d023SEric Saxe 		 */
1677ef4f35d8SEric Saxe 		ASSERT(pg_next == NULL ||
1678ef4f35d8SEric Saxe 		    (PG_NUM_CPUS((pg_t *)pg) <= PG_NUM_CPUS((pg_t *)pg_next)));
16796890d023SEric Saxe 
16806890d023SEric Saxe 		/*
1681ef4f35d8SEric Saxe 		 * Check to make sure that the existing parent of PG (if any)
1682ef4f35d8SEric Saxe 		 * is either in the PG's lineage, or the PG has more CPUs than
1683ef4f35d8SEric Saxe 		 * its existing parent and can and should be promoted above its
1684ef4f35d8SEric Saxe 		 * parent.
1685ef4f35d8SEric Saxe 		 *
1686ef4f35d8SEric Saxe 		 * Since the PG topology is in the middle of being changed, we
1687ef4f35d8SEric Saxe 		 * need to check whether the PG's existing parent (if any) is
1688ef4f35d8SEric Saxe 		 * part of its lineage (and therefore should contain the new
1689ef4f35d8SEric Saxe 		 * CPU). If not, it means that the addition of the new CPU
1690ef4f35d8SEric Saxe 		 * should have made this PG have more CPUs than its parent, and
1691ef4f35d8SEric Saxe 		 * this PG should be promoted to be above its existing parent
1692ef4f35d8SEric Saxe 		 * now. We need to verify all of this to defend against a buggy
1693ef4f35d8SEric Saxe 		 * BIOS giving bad power domain CPU groupings. Sigh.
1694ef4f35d8SEric Saxe 		 */
1695ef4f35d8SEric Saxe 		if (pg->cmt_parent) {
1696ef4f35d8SEric Saxe 			/*
1697ef4f35d8SEric Saxe 			 * Determine if cmt_parent is in this lineage
1698ef4f35d8SEric Saxe 			 */
1699ef4f35d8SEric Saxe 			for (j = 0; j < size; j++) {
1700ef4f35d8SEric Saxe 				pg_tmp = lineage[j];
1701ef4f35d8SEric Saxe 				if (pg_tmp == pg->cmt_parent)
1702ef4f35d8SEric Saxe 					break;
1703ef4f35d8SEric Saxe 			}
1704ef4f35d8SEric Saxe 			if (pg_tmp != pg->cmt_parent) {
1705ef4f35d8SEric Saxe 				/*
1706ef4f35d8SEric Saxe 				 * cmt_parent is not in the lineage, verify
1707ef4f35d8SEric Saxe 				 * it is a proper subset of PG.
1708ef4f35d8SEric Saxe 				 */
1709ef4f35d8SEric Saxe 				if (PG_NUM_CPUS((pg_t *)pg->cmt_parent) >=
1710ef4f35d8SEric Saxe 				    PG_NUM_CPUS((pg_t *)pg)) {
1711ef4f35d8SEric Saxe 					/*
1712ef4f35d8SEric Saxe 					 * Not a proper subset if pg has less
1713ef4f35d8SEric Saxe 					 * CPUs than cmt_parent...
1714ef4f35d8SEric Saxe 					 */
1715ef4f35d8SEric Saxe 					cmt_lineage_status =
1716ef4f35d8SEric Saxe 					    CMT_LINEAGE_NON_PROMOTABLE;
1717ef4f35d8SEric Saxe 					goto handle_error;
1718ef4f35d8SEric Saxe 				}
1719ef4f35d8SEric Saxe 			}
1720ef4f35d8SEric Saxe 		}
1721ef4f35d8SEric Saxe 
1722ef4f35d8SEric Saxe 		/*
1723ef4f35d8SEric Saxe 		 * Walk each of the CPUs in the PGs group and perform
1724ef4f35d8SEric Saxe 		 * consistency checks along the way.
17256890d023SEric Saxe 		 */
17260e751525SEric Saxe 		PG_CPU_ITR_INIT((pg_t *)pg, cpu_iter);
17270e751525SEric Saxe 		while ((cp = pg_cpu_next(&cpu_iter)) != NULL) {
1728ef4f35d8SEric Saxe 			/*
1729ef4f35d8SEric Saxe 			 * Verify that there aren't any CPUs contained in PG
1730ef4f35d8SEric Saxe 			 * that the next PG in the lineage (which is larger
1731ef4f35d8SEric Saxe 			 * or same size) doesn't also contain.
1732ef4f35d8SEric Saxe 			 */
1733ef4f35d8SEric Saxe 			if (pg_next != NULL &&
1734ef4f35d8SEric Saxe 			    pg_cpu_find((pg_t *)pg_next, cp) == B_FALSE) {
17350e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_NON_CONCENTRIC;
17360e751525SEric Saxe 				goto handle_error;
17376890d023SEric Saxe 			}
1738ef4f35d8SEric Saxe 
1739ef4f35d8SEric Saxe 			/*
1740ef4f35d8SEric Saxe 			 * Verify that all the CPUs in the PG are in the same
1741ef4f35d8SEric Saxe 			 * lgroup.
1742ef4f35d8SEric Saxe 			 */
1743ef4f35d8SEric Saxe 			if (lgrp == LGRP_NULL_HANDLE) {
1744ef4f35d8SEric Saxe 				lgrp = lgrp_plat_cpu_to_hand(cp->cpu_id);
1745ef4f35d8SEric Saxe 			} else if (lgrp_plat_cpu_to_hand(cp->cpu_id) != lgrp) {
1746ef4f35d8SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_PG_SPANS_LGRPS;
1747ef4f35d8SEric Saxe 				goto handle_error;
1748ef4f35d8SEric Saxe 			}
17490e751525SEric Saxe 		}
17506890d023SEric Saxe 	}
17516890d023SEric Saxe 
17520e751525SEric Saxe handle_error:
1753ef4f35d8SEric Saxe 	/*
1754ef4f35d8SEric Saxe 	 * Some of these validation errors can result when the CPU grouping
1755ef4f35d8SEric Saxe 	 * information is derived from buggy sources (for example, incorrect
1756ef4f35d8SEric Saxe 	 * ACPI tables on x86 systems).
1757ef4f35d8SEric Saxe 	 *
1758ef4f35d8SEric Saxe 	 * We'll try to recover in such cases by pruning out the illegal
1759ef4f35d8SEric Saxe 	 * groupings from the PG hierarchy, which means that we won't optimize
1760ef4f35d8SEric Saxe 	 * for those levels, but we will for the remaining ones.
1761ef4f35d8SEric Saxe 	 */
17620e751525SEric Saxe 	switch (cmt_lineage_status) {
17630e751525SEric Saxe 	case CMT_LINEAGE_VALID:
17640e751525SEric Saxe 	case CMT_LINEAGE_REPAIRED:
17650e751525SEric Saxe 		break;
1766ef4f35d8SEric Saxe 	case CMT_LINEAGE_PG_SPANS_LGRPS:
1767ef4f35d8SEric Saxe 		/*
1768ef4f35d8SEric Saxe 		 * We've detected a PG whose CPUs span lgroups.
1769ef4f35d8SEric Saxe 		 *
1770ef4f35d8SEric Saxe 		 * This isn't supported, as the dispatcher isn't allowed to
1771ef4f35d8SEric Saxe 		 * to do CMT thread placement across lgroups, as this would
1772ef4f35d8SEric Saxe 		 * conflict with policies implementing MPO thread affinity.
1773ef4f35d8SEric Saxe 		 *
1774ef4f35d8SEric Saxe 		 * The handling for this falls through to the next case.
1775ef4f35d8SEric Saxe 		 */
1776ef4f35d8SEric Saxe 	case CMT_LINEAGE_NON_PROMOTABLE:
1777ef4f35d8SEric Saxe 		/*
1778ef4f35d8SEric Saxe 		 * We've detected a PG that already exists in another CPU's
1779ef4f35d8SEric Saxe 		 * lineage that cannot cannot legally be promoted into place
1780ef4f35d8SEric Saxe 		 * without breaking the invariants of the hierarchy.
1781ef4f35d8SEric Saxe 		 */
1782ef4f35d8SEric Saxe 		if (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw)) {
1783*1a77c24bSEric Saxe 			if (pg_cmt_prune(pg, lineage, sz, pgdata) == 0) {
1784ef4f35d8SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_REPAIRED;
1785ef4f35d8SEric Saxe 				goto revalidate;
1786ef4f35d8SEric Saxe 			}
1787ef4f35d8SEric Saxe 		}
1788ef4f35d8SEric Saxe 		/*
1789ef4f35d8SEric Saxe 		 * Something went wrong trying to prune out the bad level.
1790ef4f35d8SEric Saxe 		 * Disable CMT scheduling altogether.
1791ef4f35d8SEric Saxe 		 */
1792ef4f35d8SEric Saxe 		pg_cmt_disable();
1793ef4f35d8SEric Saxe 		break;
17940e751525SEric Saxe 	case CMT_LINEAGE_NON_CONCENTRIC:
17956890d023SEric Saxe 		/*
1796ef4f35d8SEric Saxe 		 * We've detected a non-concentric PG lineage, which means that
1797ef4f35d8SEric Saxe 		 * there's a PG in the lineage that has CPUs that the next PG
1798ef4f35d8SEric Saxe 		 * over in the lineage (which is the same size or larger)
1799ef4f35d8SEric Saxe 		 * doesn't have.
18000e751525SEric Saxe 		 *
1801ef4f35d8SEric Saxe 		 * In this case, we examine the two PGs to see if either
1802ef4f35d8SEric Saxe 		 * grouping is defined by potentially buggy sources.
18030e751525SEric Saxe 		 *
18040e751525SEric Saxe 		 * If one has less CPUs than the other, and contains CPUs
18050e751525SEric Saxe 		 * not found in the parent, and it is an untrusted enumeration,
18060e751525SEric Saxe 		 * then prune it. If both have the same number of CPUs, then
18070e751525SEric Saxe 		 * prune the one that is untrusted.
18080e751525SEric Saxe 		 *
18090e751525SEric Saxe 		 * This process repeats until we have a concentric lineage,
18100e751525SEric Saxe 		 * or we would have to prune out level derived from what we
18110e751525SEric Saxe 		 * thought was a reliable source, in which case CMT scheduling
1812ef4f35d8SEric Saxe 		 * is disabled altogether.
18136890d023SEric Saxe 		 */
1814ef4f35d8SEric Saxe 		if ((PG_NUM_CPUS((pg_t *)pg) < PG_NUM_CPUS((pg_t *)pg_next)) &&
18150e751525SEric Saxe 		    (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw))) {
18160e751525SEric Saxe 			pg_bad = pg;
18170e751525SEric Saxe 		} else if (PG_NUM_CPUS((pg_t *)pg) ==
1818ef4f35d8SEric Saxe 		    PG_NUM_CPUS((pg_t *)pg_next)) {
1819ef4f35d8SEric Saxe 			if (PG_CMT_HW_SUSPECT(((pghw_t *)pg_next)->pghw_hw)) {
1820ef4f35d8SEric Saxe 				pg_bad = pg_next;
18210e751525SEric Saxe 			} else if (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw)) {
18220e751525SEric Saxe 				pg_bad = pg;
18236890d023SEric Saxe 			}
18246890d023SEric Saxe 		}
18250e751525SEric Saxe 		if (pg_bad) {
1826*1a77c24bSEric Saxe 			if (pg_cmt_prune(pg_bad, lineage, sz, pgdata) == 0) {
18270e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_REPAIRED;
18280e751525SEric Saxe 				goto revalidate;
18290e751525SEric Saxe 			}
18300e751525SEric Saxe 		}
18310e751525SEric Saxe 		/*
1832ef4f35d8SEric Saxe 		 * Something went wrong trying to identify and/or prune out
1833ef4f35d8SEric Saxe 		 * the bad level. Disable CMT scheduling altogether.
18340e751525SEric Saxe 		 */
18350e751525SEric Saxe 		pg_cmt_disable();
1836ef4f35d8SEric Saxe 		break;
1837ef4f35d8SEric Saxe 	default:
1838ef4f35d8SEric Saxe 		/*
1839ef4f35d8SEric Saxe 		 * If we're here, we've encountered a validation error for
1840ef4f35d8SEric Saxe 		 * which we don't know how to recover. In this case, disable
1841ef4f35d8SEric Saxe 		 * CMT scheduling altogether.
1842ef4f35d8SEric Saxe 		 */
18430e751525SEric Saxe 		cmt_lineage_status = CMT_LINEAGE_UNRECOVERABLE;
1844ef4f35d8SEric Saxe 		pg_cmt_disable();
18450e751525SEric Saxe 	}
1846ef4f35d8SEric Saxe 	return (cmt_lineage_status);
18476890d023SEric Saxe }
1848