xref: /titanic_50/usr/src/uts/common/disp/cmt.c (revision 47ab0c7c6702159d8bb84e3b1533d9f9843dd568)
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 *);
137*47ab0c7cSEric Saxe static void		pg_cmt_cpu_init(cpu_t *, cpu_pg_t *);
138*47ab0c7cSEric 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 *);
155ef4f35d8SEric Saxe static cmt_lineage_validation_t	pg_cmt_lineage_validate(pg_cmt_t **, int *);
156fb2f18f8Sesaxe 
1570e751525SEric Saxe 
1580e751525SEric Saxe /*
159fb2f18f8Sesaxe  * CMT PG ops
160fb2f18f8Sesaxe  */
161fb2f18f8Sesaxe struct pg_ops pg_ops_cmt = {
162fb2f18f8Sesaxe 	pg_cmt_alloc,
163fb2f18f8Sesaxe 	pg_cmt_free,
164fb2f18f8Sesaxe 	pg_cmt_cpu_init,
165fb2f18f8Sesaxe 	pg_cmt_cpu_fini,
166fb2f18f8Sesaxe 	pg_cmt_cpu_active,
167fb2f18f8Sesaxe 	pg_cmt_cpu_inactive,
168fb2f18f8Sesaxe 	pg_cmt_cpupart_in,
169fb2f18f8Sesaxe 	NULL,			/* cpupart_out */
170fb2f18f8Sesaxe 	pg_cmt_cpupart_move,
171fb2f18f8Sesaxe 	pg_cmt_cpu_belongs,
1720e751525SEric Saxe 	pg_cmt_policy_name,
173fb2f18f8Sesaxe };
174fb2f18f8Sesaxe 
175fb2f18f8Sesaxe /*
176fb2f18f8Sesaxe  * Initialize the CMT PG class
177fb2f18f8Sesaxe  */
178fb2f18f8Sesaxe void
179fb2f18f8Sesaxe pg_cmt_class_init(void)
180fb2f18f8Sesaxe {
181fb2f18f8Sesaxe 	if (cmt_sched_disabled)
182fb2f18f8Sesaxe 		return;
183fb2f18f8Sesaxe 
184fb2f18f8Sesaxe 	pg_cmt_class_id = pg_class_register("cmt", &pg_ops_cmt, PGR_PHYSICAL);
185fb2f18f8Sesaxe }
186fb2f18f8Sesaxe 
187fb2f18f8Sesaxe /*
188fb2f18f8Sesaxe  * Called to indicate a new CPU has started up so
189fb2f18f8Sesaxe  * that either t0 or the slave startup thread can
190fb2f18f8Sesaxe  * be accounted for.
191fb2f18f8Sesaxe  */
192fb2f18f8Sesaxe void
193fb2f18f8Sesaxe pg_cmt_cpu_startup(cpu_t *cp)
194fb2f18f8Sesaxe {
1950e751525SEric Saxe 	pg_ev_thread_swtch(cp, gethrtime_unscaled(), cp->cpu_idle_thread,
1960e751525SEric Saxe 	    cp->cpu_thread);
197fb2f18f8Sesaxe }
198fb2f18f8Sesaxe 
199fb2f18f8Sesaxe /*
200fb2f18f8Sesaxe  * Return non-zero if thread can migrate between "from" and "to"
201fb2f18f8Sesaxe  * without a performance penalty
202fb2f18f8Sesaxe  */
203fb2f18f8Sesaxe int
204fb2f18f8Sesaxe pg_cmt_can_migrate(cpu_t *from, cpu_t *to)
205fb2f18f8Sesaxe {
206fb2f18f8Sesaxe 	if (from->cpu_physid->cpu_cacheid ==
207fb2f18f8Sesaxe 	    to->cpu_physid->cpu_cacheid)
208fb2f18f8Sesaxe 		return (1);
209fb2f18f8Sesaxe 	return (0);
210fb2f18f8Sesaxe }
211fb2f18f8Sesaxe 
212fb2f18f8Sesaxe /*
213fb2f18f8Sesaxe  * CMT class specific PG allocation
214fb2f18f8Sesaxe  */
215fb2f18f8Sesaxe static pg_t *
216fb2f18f8Sesaxe pg_cmt_alloc(void)
217fb2f18f8Sesaxe {
218fb2f18f8Sesaxe 	return (kmem_zalloc(sizeof (pg_cmt_t), KM_NOSLEEP));
219fb2f18f8Sesaxe }
220fb2f18f8Sesaxe 
221fb2f18f8Sesaxe /*
222fb2f18f8Sesaxe  * Class specific PG de-allocation
223fb2f18f8Sesaxe  */
224fb2f18f8Sesaxe static void
225fb2f18f8Sesaxe pg_cmt_free(pg_t *pg)
226fb2f18f8Sesaxe {
227fb2f18f8Sesaxe 	ASSERT(pg != NULL);
228fb2f18f8Sesaxe 	ASSERT(IS_CMT_PG(pg));
229fb2f18f8Sesaxe 
230fb2f18f8Sesaxe 	kmem_free((pg_cmt_t *)pg, sizeof (pg_cmt_t));
231fb2f18f8Sesaxe }
232fb2f18f8Sesaxe 
233fb2f18f8Sesaxe /*
2340e751525SEric Saxe  * Given a hardware sharing relationship, return which dispatcher
2350e751525SEric Saxe  * policies should be implemented to optimize performance and efficiency
236fb2f18f8Sesaxe  */
2370e751525SEric Saxe static pg_cmt_policy_t
2380e751525SEric Saxe pg_cmt_policy(pghw_type_t hw)
239fb2f18f8Sesaxe {
2400e751525SEric Saxe 	pg_cmt_policy_t p;
2410e751525SEric Saxe 
2420e751525SEric Saxe 	/*
2430e751525SEric Saxe 	 * Give the platform a chance to override the default
2440e751525SEric Saxe 	 */
2450e751525SEric Saxe 	if ((p = pg_plat_cmt_policy(hw)) != CMT_NO_POLICY)
2460e751525SEric Saxe 		return (p);
2470e751525SEric Saxe 
2480e751525SEric Saxe 	switch (hw) {
2490e751525SEric Saxe 	case PGHW_IPIPE:
2500e751525SEric Saxe 	case PGHW_FPU:
2510e751525SEric Saxe 	case PGHW_CHIP:
2520e751525SEric Saxe 		return (CMT_BALANCE);
2530e751525SEric Saxe 	case PGHW_CACHE:
2540e751525SEric Saxe 		return (CMT_AFFINITY);
2550e751525SEric Saxe 	case PGHW_POW_ACTIVE:
2560e751525SEric Saxe 	case PGHW_POW_IDLE:
2570e751525SEric Saxe 		return (CMT_BALANCE);
2580e751525SEric Saxe 	default:
2590e751525SEric Saxe 		return (CMT_NO_POLICY);
2600e751525SEric Saxe 	}
2610e751525SEric Saxe }
2620e751525SEric Saxe 
2630e751525SEric Saxe /*
2640e751525SEric Saxe  * Rank the importance of optimizing for the pg1 relationship vs.
2650e751525SEric Saxe  * the pg2 relationship.
2660e751525SEric Saxe  */
2670e751525SEric Saxe static pg_cmt_t *
2680e751525SEric Saxe pg_cmt_hier_rank(pg_cmt_t *pg1, pg_cmt_t *pg2)
2690e751525SEric Saxe {
2700e751525SEric Saxe 	pghw_type_t hw1 = ((pghw_t *)pg1)->pghw_hw;
2710e751525SEric Saxe 	pghw_type_t hw2 = ((pghw_t *)pg2)->pghw_hw;
2720e751525SEric Saxe 
2730e751525SEric Saxe 	/*
2740e751525SEric Saxe 	 * A power domain is only important if CPUPM is enabled.
2750e751525SEric Saxe 	 */
2760e751525SEric Saxe 	if (cpupm_get_policy() == CPUPM_POLICY_DISABLED) {
2770e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw1) && !PGHW_IS_PM_DOMAIN(hw2))
2780e751525SEric Saxe 			return (pg2);
2790e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw2) && !PGHW_IS_PM_DOMAIN(hw1))
2800e751525SEric Saxe 			return (pg1);
2810e751525SEric Saxe 	}
2820e751525SEric Saxe 
2830e751525SEric Saxe 	/*
2840e751525SEric Saxe 	 * Otherwise, ask the platform
2850e751525SEric Saxe 	 */
2860e751525SEric Saxe 	if (pg_plat_hw_rank(hw1, hw2) == hw1)
2870e751525SEric Saxe 		return (pg1);
2880e751525SEric Saxe 	else
2890e751525SEric Saxe 		return (pg2);
2900e751525SEric Saxe }
2910e751525SEric Saxe 
2920e751525SEric Saxe /*
2930e751525SEric Saxe  * Initialize CMT callbacks for the given PG
2940e751525SEric Saxe  */
2950e751525SEric Saxe static void
2960e751525SEric Saxe cmt_callback_init(pg_t *pg)
2970e751525SEric Saxe {
2980e751525SEric Saxe 	switch (((pghw_t *)pg)->pghw_hw) {
2990e751525SEric Saxe 	case PGHW_POW_ACTIVE:
3000e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch_pwr;
3010e751525SEric Saxe 		pg->pg_cb.thread_remain = cmt_ev_thread_remain_pwr;
3020e751525SEric Saxe 		break;
3030e751525SEric Saxe 	default:
3040e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch;
3050e751525SEric Saxe 
3060e751525SEric Saxe 	}
3070e751525SEric Saxe }
3080e751525SEric Saxe 
3090e751525SEric Saxe /*
3100e751525SEric Saxe  * Promote PG above it's current parent.
3110e751525SEric Saxe  * This is only legal if PG has an equal or greater number of CPUs
3120e751525SEric Saxe  * than it's parent.
3130e751525SEric Saxe  */
3140e751525SEric Saxe static void
3150e751525SEric Saxe cmt_hier_promote(pg_cmt_t *pg)
3160e751525SEric Saxe {
3170e751525SEric Saxe 	pg_cmt_t	*parent;
3180e751525SEric Saxe 	group_t		*children;
3190e751525SEric Saxe 	cpu_t		*cpu;
3200e751525SEric Saxe 	group_iter_t	iter;
3210e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
3220e751525SEric Saxe 	int		r;
3230e751525SEric Saxe 	int		err;
3240e751525SEric Saxe 
3250e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
3260e751525SEric Saxe 
3270e751525SEric Saxe 	parent = pg->cmt_parent;
3280e751525SEric Saxe 	if (parent == NULL) {
3290e751525SEric Saxe 		/*
3300e751525SEric Saxe 		 * Nothing to do
3310e751525SEric Saxe 		 */
3320e751525SEric Saxe 		return;
3330e751525SEric Saxe 	}
3340e751525SEric Saxe 
3350e751525SEric Saxe 	ASSERT(PG_NUM_CPUS((pg_t *)pg) >= PG_NUM_CPUS((pg_t *)parent));
3360e751525SEric Saxe 
3370e751525SEric Saxe 	/*
3380e751525SEric Saxe 	 * We're changing around the hierarchy, which is actively traversed
3390e751525SEric Saxe 	 * by the dispatcher. Pause CPUS to ensure exclusivity.
3400e751525SEric Saxe 	 */
3410e751525SEric Saxe 	pause_cpus(NULL);
3420e751525SEric Saxe 
3430e751525SEric Saxe 	/*
3440e751525SEric Saxe 	 * If necessary, update the parent's sibling set, replacing parent
3450e751525SEric Saxe 	 * with PG.
3460e751525SEric Saxe 	 */
3470e751525SEric Saxe 	if (parent->cmt_siblings) {
3480e751525SEric Saxe 		if (group_remove(parent->cmt_siblings, parent, GRP_NORESIZE)
3490e751525SEric Saxe 		    != -1) {
3500e751525SEric Saxe 			r = group_add(parent->cmt_siblings, pg, GRP_NORESIZE);
3510e751525SEric Saxe 			ASSERT(r != -1);
3520e751525SEric Saxe 		}
3530e751525SEric Saxe 	}
3540e751525SEric Saxe 
3550e751525SEric Saxe 	/*
3560e751525SEric Saxe 	 * If the parent is at the top of the hierarchy, replace it's entry
3570e751525SEric Saxe 	 * in the root lgroup's group of top level PGs.
3580e751525SEric Saxe 	 */
3590e751525SEric Saxe 	if (parent->cmt_parent == NULL &&
3600e751525SEric Saxe 	    parent->cmt_siblings != &cmt_root->cl_pgs) {
3610e751525SEric Saxe 		if (group_remove(&cmt_root->cl_pgs, parent, GRP_NORESIZE)
3620e751525SEric Saxe 		    != -1) {
3630e751525SEric Saxe 			r = group_add(&cmt_root->cl_pgs, pg, GRP_NORESIZE);
3640e751525SEric Saxe 			ASSERT(r != -1);
3650e751525SEric Saxe 		}
3660e751525SEric Saxe 	}
3670e751525SEric Saxe 
3680e751525SEric Saxe 	/*
3690e751525SEric Saxe 	 * We assume (and therefore assert) that the PG being promoted is an
3700e751525SEric Saxe 	 * only child of it's parent. Update the parent's children set
3710e751525SEric Saxe 	 * replacing PG's entry with the parent (since the parent is becoming
3720e751525SEric Saxe 	 * the child). Then have PG and the parent swap children sets.
3730e751525SEric Saxe 	 */
3740e751525SEric Saxe 	ASSERT(GROUP_SIZE(parent->cmt_children) <= 1);
3750e751525SEric Saxe 	if (group_remove(parent->cmt_children, pg, GRP_NORESIZE) != -1) {
3760e751525SEric Saxe 		r = group_add(parent->cmt_children, parent, GRP_NORESIZE);
3770e751525SEric Saxe 		ASSERT(r != -1);
3780e751525SEric Saxe 	}
3790e751525SEric Saxe 
3800e751525SEric Saxe 	children = pg->cmt_children;
3810e751525SEric Saxe 	pg->cmt_children = parent->cmt_children;
3820e751525SEric Saxe 	parent->cmt_children = children;
3830e751525SEric Saxe 
3840e751525SEric Saxe 	/*
3850e751525SEric Saxe 	 * Update the sibling references for PG and it's parent
3860e751525SEric Saxe 	 */
3870e751525SEric Saxe 	pg->cmt_siblings = parent->cmt_siblings;
3880e751525SEric Saxe 	parent->cmt_siblings = pg->cmt_children;
3890e751525SEric Saxe 
3900e751525SEric Saxe 	/*
3910e751525SEric Saxe 	 * Update any cached lineages in the per CPU pg data.
3920e751525SEric Saxe 	 */
3930e751525SEric Saxe 	PG_CPU_ITR_INIT(pg, cpu_iter);
3940e751525SEric Saxe 	while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
3950e751525SEric Saxe 		int		idx;
3960e751525SEric Saxe 		group_t		*pgs;
3970e751525SEric Saxe 		pg_cmt_t	*cpu_pg;
3980e751525SEric Saxe 
3990e751525SEric Saxe 		/*
4000e751525SEric Saxe 		 * Iterate over the CPU's PGs updating the children
4010e751525SEric Saxe 		 * of the PG being promoted, since they have a new parent.
4020e751525SEric Saxe 		 */
4030e751525SEric Saxe 		pgs = &cpu->cpu_pg->pgs;
4040e751525SEric Saxe 		group_iter_init(&iter);
4050e751525SEric Saxe 		while ((cpu_pg = group_iterate(pgs, &iter)) != NULL) {
4060e751525SEric Saxe 			if (cpu_pg->cmt_parent == pg) {
4070e751525SEric Saxe 				cpu_pg->cmt_parent = parent;
4080e751525SEric Saxe 			}
4090e751525SEric Saxe 		}
4100e751525SEric Saxe 
4110e751525SEric Saxe 		/*
4120e751525SEric Saxe 		 * Update the CMT load balancing lineage
4130e751525SEric Saxe 		 */
4140e751525SEric Saxe 		pgs = &cpu->cpu_pg->cmt_pgs;
4150e751525SEric Saxe 		if ((idx = group_find(pgs, (void *)pg)) == -1) {
4160e751525SEric Saxe 			/*
4170e751525SEric Saxe 			 * Unless this is the CPU who's lineage is being
4180e751525SEric Saxe 			 * constructed, the PG being promoted should be
4190e751525SEric Saxe 			 * in the lineage.
4200e751525SEric Saxe 			 */
4210e751525SEric Saxe 			ASSERT(GROUP_SIZE(pgs) == 0);
4220e751525SEric Saxe 			continue;
4230e751525SEric Saxe 		}
4240e751525SEric Saxe 
4250e751525SEric Saxe 		ASSERT(GROUP_ACCESS(pgs, idx - 1) == parent);
4260e751525SEric Saxe 		ASSERT(idx > 0);
4270e751525SEric Saxe 
4280e751525SEric Saxe 		/*
4290e751525SEric Saxe 		 * Have the child and the parent swap places in the CPU's
4300e751525SEric Saxe 		 * lineage
4310e751525SEric Saxe 		 */
4320e751525SEric Saxe 		group_remove_at(pgs, idx);
4330e751525SEric Saxe 		group_remove_at(pgs, idx - 1);
4340e751525SEric Saxe 		err = group_add_at(pgs, parent, idx);
4350e751525SEric Saxe 		ASSERT(err == 0);
4360e751525SEric Saxe 		err = group_add_at(pgs, pg, idx - 1);
4370e751525SEric Saxe 		ASSERT(err == 0);
4380e751525SEric Saxe 	}
4390e751525SEric Saxe 
4400e751525SEric Saxe 	/*
4410e751525SEric Saxe 	 * Update the parent references for PG and it's parent
4420e751525SEric Saxe 	 */
4430e751525SEric Saxe 	pg->cmt_parent = parent->cmt_parent;
4440e751525SEric Saxe 	parent->cmt_parent = pg;
4450e751525SEric Saxe 
4460e751525SEric Saxe 	start_cpus();
447fb2f18f8Sesaxe }
448fb2f18f8Sesaxe 
449fb2f18f8Sesaxe /*
450fb2f18f8Sesaxe  * CMT class callback for a new CPU entering the system
451fb2f18f8Sesaxe  */
452fb2f18f8Sesaxe static void
453*47ab0c7cSEric Saxe pg_cmt_cpu_init(cpu_t *cp, cpu_pg_t *cpu_pg)
454fb2f18f8Sesaxe {
455fb2f18f8Sesaxe 	pg_cmt_t	*pg;
456fb2f18f8Sesaxe 	group_t		*cmt_pgs;
4570e751525SEric Saxe 	int		levels, level;
458fb2f18f8Sesaxe 	pghw_type_t	hw;
459fb2f18f8Sesaxe 	pg_t		*pg_cache = NULL;
460fb2f18f8Sesaxe 	pg_cmt_t	*cpu_cmt_hier[PGHW_NUM_COMPONENTS];
461fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
462fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
463ef4f35d8SEric Saxe 	cmt_lineage_validation_t	lineage_status;
464fb2f18f8Sesaxe 
465fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
466fb2f18f8Sesaxe 
4670e751525SEric Saxe 	if (cmt_sched_disabled)
4680e751525SEric Saxe 		return;
4690e751525SEric Saxe 
470fb2f18f8Sesaxe 	/*
471fb2f18f8Sesaxe 	 * A new CPU is coming into the system.
472fb2f18f8Sesaxe 	 * Interrogate the platform to see if the CPU
4730e751525SEric Saxe 	 * has any performance or efficiency relevant
4740e751525SEric Saxe 	 * sharing relationships
475fb2f18f8Sesaxe 	 */
476*47ab0c7cSEric Saxe 	cmt_pgs = &cpu_pg->cmt_pgs;
477*47ab0c7cSEric Saxe 	cpu_pg->cmt_lineage = NULL;
478fb2f18f8Sesaxe 
479fb2f18f8Sesaxe 	bzero(cpu_cmt_hier, sizeof (cpu_cmt_hier));
4800e751525SEric Saxe 	levels = 0;
481fb2f18f8Sesaxe 	for (hw = PGHW_START; hw < PGHW_NUM_COMPONENTS; hw++) {
482fb2f18f8Sesaxe 
4830e751525SEric Saxe 		pg_cmt_policy_t	policy;
4840e751525SEric Saxe 
485fb2f18f8Sesaxe 		/*
4860e751525SEric Saxe 		 * We're only interested in the hw sharing relationships
4870e751525SEric Saxe 		 * for which we know how to optimize.
488fb2f18f8Sesaxe 		 */
4890e751525SEric Saxe 		policy = pg_cmt_policy(hw);
4900e751525SEric Saxe 		if (policy == CMT_NO_POLICY ||
4910e751525SEric Saxe 		    pg_plat_hw_shared(cp, hw) == 0)
492fb2f18f8Sesaxe 			continue;
493fb2f18f8Sesaxe 
494fb2f18f8Sesaxe 		/*
4950e751525SEric Saxe 		 * Continue if the hardware sharing relationship has been
4960e751525SEric Saxe 		 * blacklisted.
4970e751525SEric Saxe 		 */
4980e751525SEric Saxe 		if (cmt_hw_blacklisted[hw]) {
4990e751525SEric Saxe 			continue;
5000e751525SEric Saxe 		}
5010e751525SEric Saxe 
5020e751525SEric Saxe 		/*
503fb2f18f8Sesaxe 		 * Find (or create) the PG associated with
504fb2f18f8Sesaxe 		 * the hw sharing relationship in which cp
505fb2f18f8Sesaxe 		 * belongs.
506fb2f18f8Sesaxe 		 *
507fb2f18f8Sesaxe 		 * Determine if a suitable PG already
508fb2f18f8Sesaxe 		 * exists, or if one needs to be created.
509fb2f18f8Sesaxe 		 */
510fb2f18f8Sesaxe 		pg = (pg_cmt_t *)pghw_place_cpu(cp, hw);
511fb2f18f8Sesaxe 		if (pg == NULL) {
512fb2f18f8Sesaxe 			/*
513fb2f18f8Sesaxe 			 * Create a new one.
514fb2f18f8Sesaxe 			 * Initialize the common...
515fb2f18f8Sesaxe 			 */
516fb2f18f8Sesaxe 			pg = (pg_cmt_t *)pg_create(pg_cmt_class_id);
517fb2f18f8Sesaxe 
518fb2f18f8Sesaxe 			/* ... physical ... */
519fb2f18f8Sesaxe 			pghw_init((pghw_t *)pg, cp, hw);
520fb2f18f8Sesaxe 
521fb2f18f8Sesaxe 			/*
522fb2f18f8Sesaxe 			 * ... and CMT specific portions of the
523fb2f18f8Sesaxe 			 * structure.
524fb2f18f8Sesaxe 			 */
5250e751525SEric Saxe 			pg->cmt_policy = policy;
5260e751525SEric Saxe 
5270e751525SEric Saxe 			/* CMT event callbacks */
5280e751525SEric Saxe 			cmt_callback_init((pg_t *)pg);
5290e751525SEric Saxe 
530fb2f18f8Sesaxe 			bitset_init(&pg->cmt_cpus_actv_set);
531fb2f18f8Sesaxe 			group_create(&pg->cmt_cpus_actv);
532fb2f18f8Sesaxe 		} else {
533fb2f18f8Sesaxe 			ASSERT(IS_CMT_PG(pg));
534fb2f18f8Sesaxe 		}
535fb2f18f8Sesaxe 
536fb2f18f8Sesaxe 		/* Add the CPU to the PG */
537*47ab0c7cSEric Saxe 		pg_cpu_add((pg_t *)pg, cp, cpu_pg);
538fb2f18f8Sesaxe 
539fb2f18f8Sesaxe 		/*
5406890d023SEric Saxe 		 * Ensure capacity of the active CPU group/bitset
541fb2f18f8Sesaxe 		 */
542fb2f18f8Sesaxe 		group_expand(&pg->cmt_cpus_actv,
543fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
544fb2f18f8Sesaxe 
545fb2f18f8Sesaxe 		if (cp->cpu_seqid >=
546fb2f18f8Sesaxe 		    bitset_capacity(&pg->cmt_cpus_actv_set)) {
547fb2f18f8Sesaxe 			bitset_resize(&pg->cmt_cpus_actv_set,
548fb2f18f8Sesaxe 			    cp->cpu_seqid + 1);
549fb2f18f8Sesaxe 		}
550fb2f18f8Sesaxe 
551fb2f18f8Sesaxe 		/*
5520e751525SEric Saxe 		 * Build a lineage of CMT PGs for load balancing / coalescence
553fb2f18f8Sesaxe 		 */
5540e751525SEric Saxe 		if (policy & (CMT_BALANCE | CMT_COALESCE)) {
5550e751525SEric Saxe 			cpu_cmt_hier[levels++] = pg;
556fb2f18f8Sesaxe 		}
557fb2f18f8Sesaxe 
558fb2f18f8Sesaxe 		/* Cache this for later */
559fb2f18f8Sesaxe 		if (hw == PGHW_CACHE)
560fb2f18f8Sesaxe 			pg_cache = (pg_t *)pg;
561fb2f18f8Sesaxe 	}
562fb2f18f8Sesaxe 
5630e751525SEric Saxe 	group_expand(cmt_pgs, levels);
5646890d023SEric Saxe 
5656890d023SEric Saxe 	if (cmt_root == NULL)
5666890d023SEric Saxe 		cmt_root = pg_cmt_lgrp_create(lgrp_plat_root_hand());
567fb2f18f8Sesaxe 
568fb2f18f8Sesaxe 	/*
5690e751525SEric Saxe 	 * Find the lgrp that encapsulates this CPU's CMT hierarchy
5706890d023SEric Saxe 	 */
5716890d023SEric Saxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
5726890d023SEric Saxe 	if ((lgrp = pg_cmt_find_lgrp(lgrp_handle)) == NULL)
5736890d023SEric Saxe 		lgrp = pg_cmt_lgrp_create(lgrp_handle);
5746890d023SEric Saxe 
5756890d023SEric Saxe 	/*
5760e751525SEric Saxe 	 * Ascendingly sort the PGs in the lineage by number of CPUs
5770e751525SEric Saxe 	 */
5780e751525SEric Saxe 	pg_cmt_hier_sort(cpu_cmt_hier, levels);
5790e751525SEric Saxe 
5800e751525SEric Saxe 	/*
5810e751525SEric Saxe 	 * Examine the lineage and validate it.
5820e751525SEric Saxe 	 * This routine will also try to fix the lineage along with the
5830e751525SEric Saxe 	 * rest of the PG hierarchy should it detect an issue.
5840e751525SEric Saxe 	 *
585ef4f35d8SEric Saxe 	 * If it returns anything other than VALID or REPAIRED, an
586ef4f35d8SEric Saxe 	 * unrecoverable error has occurred, and we cannot proceed.
5870e751525SEric Saxe 	 */
588ef4f35d8SEric Saxe 	lineage_status = pg_cmt_lineage_validate(cpu_cmt_hier, &levels);
589ef4f35d8SEric Saxe 	if ((lineage_status != CMT_LINEAGE_VALID) &&
590ef4f35d8SEric Saxe 	    (lineage_status != CMT_LINEAGE_REPAIRED))
5910e751525SEric Saxe 		return;
5920e751525SEric Saxe 
5930e751525SEric Saxe 	/*
5940e751525SEric Saxe 	 * For existing PGs in the lineage, verify that the parent is
5950e751525SEric Saxe 	 * correct, as the generation in the lineage may have changed
5960e751525SEric Saxe 	 * as a result of the sorting. Start the traversal at the top
5970e751525SEric Saxe 	 * of the lineage, moving down.
5980e751525SEric Saxe 	 */
5990e751525SEric Saxe 	for (level = levels - 1; level >= 0; ) {
6000e751525SEric Saxe 		int reorg;
6010e751525SEric Saxe 
6020e751525SEric Saxe 		reorg = 0;
6030e751525SEric Saxe 		pg = cpu_cmt_hier[level];
6040e751525SEric Saxe 
6050e751525SEric Saxe 		/*
6060e751525SEric Saxe 		 * Promote PGs at an incorrect generation into place.
6070e751525SEric Saxe 		 */
6080e751525SEric Saxe 		while (pg->cmt_parent &&
6090e751525SEric Saxe 		    pg->cmt_parent != cpu_cmt_hier[level + 1]) {
6100e751525SEric Saxe 			cmt_hier_promote(pg);
6110e751525SEric Saxe 			reorg++;
6120e751525SEric Saxe 		}
6130e751525SEric Saxe 		if (reorg > 0)
6140e751525SEric Saxe 			level = levels - 1;
6150e751525SEric Saxe 		else
6160e751525SEric Saxe 			level--;
6170e751525SEric Saxe 	}
6180e751525SEric Saxe 
6190e751525SEric Saxe 	/*
6206890d023SEric Saxe 	 * For each of the PGs in the CPU's lineage:
6210e751525SEric Saxe 	 *	- Add an entry in the CPU sorted CMT PG group
6220e751525SEric Saxe 	 *	  which is used for top down CMT load balancing
623fb2f18f8Sesaxe 	 *	- Tie the PG into the CMT hierarchy by connecting
624fb2f18f8Sesaxe 	 *	  it to it's parent and siblings.
625fb2f18f8Sesaxe 	 */
6260e751525SEric Saxe 	for (level = 0; level < levels; level++) {
627fb2f18f8Sesaxe 		uint_t		children;
628fb2f18f8Sesaxe 		int		err;
629fb2f18f8Sesaxe 
630fb2f18f8Sesaxe 		pg = cpu_cmt_hier[level];
6310e751525SEric Saxe 		err = group_add_at(cmt_pgs, pg, levels - level - 1);
632fb2f18f8Sesaxe 		ASSERT(err == 0);
633fb2f18f8Sesaxe 
634fb2f18f8Sesaxe 		if (level == 0)
635*47ab0c7cSEric Saxe 			cpu_pg->cmt_lineage = (pg_t *)pg;
636fb2f18f8Sesaxe 
637fb2f18f8Sesaxe 		if (pg->cmt_siblings != NULL) {
638fb2f18f8Sesaxe 			/* Already initialized */
639fb2f18f8Sesaxe 			ASSERT(pg->cmt_parent == NULL ||
640fb2f18f8Sesaxe 			    pg->cmt_parent == cpu_cmt_hier[level + 1]);
641fb2f18f8Sesaxe 			ASSERT(pg->cmt_siblings == &lgrp->cl_pgs ||
642c416da2dSjb145095 			    ((pg->cmt_parent != NULL) &&
643c416da2dSjb145095 			    pg->cmt_siblings == pg->cmt_parent->cmt_children));
644fb2f18f8Sesaxe 			continue;
645fb2f18f8Sesaxe 		}
646fb2f18f8Sesaxe 
6470e751525SEric Saxe 		if ((level + 1) == levels) {
648fb2f18f8Sesaxe 			pg->cmt_parent = NULL;
6496890d023SEric Saxe 
650fb2f18f8Sesaxe 			pg->cmt_siblings = &lgrp->cl_pgs;
651fb2f18f8Sesaxe 			children = ++lgrp->cl_npgs;
6520e751525SEric Saxe 			if (cmt_root != lgrp)
6536890d023SEric Saxe 				cmt_root->cl_npgs++;
654fb2f18f8Sesaxe 		} else {
655fb2f18f8Sesaxe 			pg->cmt_parent = cpu_cmt_hier[level + 1];
656fb2f18f8Sesaxe 
657fb2f18f8Sesaxe 			/*
658fb2f18f8Sesaxe 			 * A good parent keeps track of their children.
659fb2f18f8Sesaxe 			 * The parent's children group is also the PG's
660fb2f18f8Sesaxe 			 * siblings.
661fb2f18f8Sesaxe 			 */
662fb2f18f8Sesaxe 			if (pg->cmt_parent->cmt_children == NULL) {
663fb2f18f8Sesaxe 				pg->cmt_parent->cmt_children =
664fb2f18f8Sesaxe 				    kmem_zalloc(sizeof (group_t), KM_SLEEP);
665fb2f18f8Sesaxe 				group_create(pg->cmt_parent->cmt_children);
666fb2f18f8Sesaxe 			}
667fb2f18f8Sesaxe 			pg->cmt_siblings = pg->cmt_parent->cmt_children;
668fb2f18f8Sesaxe 			children = ++pg->cmt_parent->cmt_nchildren;
669fb2f18f8Sesaxe 		}
6706890d023SEric Saxe 
671fb2f18f8Sesaxe 		group_expand(pg->cmt_siblings, children);
6726890d023SEric Saxe 		group_expand(&cmt_root->cl_pgs, cmt_root->cl_npgs);
673fb2f18f8Sesaxe 	}
674fb2f18f8Sesaxe 
675fb2f18f8Sesaxe 	/*
676fb2f18f8Sesaxe 	 * Cache the chip and core IDs in the cpu_t->cpu_physid structure
677fb2f18f8Sesaxe 	 * for fast lookups later.
678fb2f18f8Sesaxe 	 */
679fb2f18f8Sesaxe 	if (cp->cpu_physid) {
680fb2f18f8Sesaxe 		cp->cpu_physid->cpu_chipid =
681fb2f18f8Sesaxe 		    pg_plat_hw_instance_id(cp, PGHW_CHIP);
682fb2f18f8Sesaxe 		cp->cpu_physid->cpu_coreid = pg_plat_get_core_id(cp);
683fb2f18f8Sesaxe 
684fb2f18f8Sesaxe 		/*
685fb2f18f8Sesaxe 		 * If this cpu has a PG representing shared cache, then set
686fb2f18f8Sesaxe 		 * cpu_cacheid to that PG's logical id
687fb2f18f8Sesaxe 		 */
688fb2f18f8Sesaxe 		if (pg_cache)
689fb2f18f8Sesaxe 			cp->cpu_physid->cpu_cacheid = pg_cache->pg_id;
690fb2f18f8Sesaxe 	}
691fb2f18f8Sesaxe 
692fb2f18f8Sesaxe 	/* CPU0 only initialization */
693fb2f18f8Sesaxe 	if (is_cpu0) {
694fb2f18f8Sesaxe 		pg_cmt_cpu_startup(cp);
695fb2f18f8Sesaxe 		is_cpu0 = 0;
696a6604450Sesaxe 		cpu0_lgrp = lgrp;
697fb2f18f8Sesaxe 	}
698fb2f18f8Sesaxe 
699fb2f18f8Sesaxe }
700fb2f18f8Sesaxe 
701fb2f18f8Sesaxe /*
702fb2f18f8Sesaxe  * Class callback when a CPU is leaving the system (deletion)
703fb2f18f8Sesaxe  */
704fb2f18f8Sesaxe static void
705*47ab0c7cSEric Saxe pg_cmt_cpu_fini(cpu_t *cp, cpu_pg_t *cpu_pg)
706fb2f18f8Sesaxe {
707fb2f18f8Sesaxe 	group_iter_t	i;
708fb2f18f8Sesaxe 	pg_cmt_t	*pg;
709fb2f18f8Sesaxe 	group_t		*pgs, *cmt_pgs;
710fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
711fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
712fb2f18f8Sesaxe 
7130e751525SEric Saxe 	if (cmt_sched_disabled)
7140e751525SEric Saxe 		return;
7150e751525SEric Saxe 
716*47ab0c7cSEric Saxe 	pgs = &cpu_pg->pgs;
717*47ab0c7cSEric Saxe 	cmt_pgs = &cpu_pg->cmt_pgs;
718fb2f18f8Sesaxe 
719fb2f18f8Sesaxe 	/*
720fb2f18f8Sesaxe 	 * Find the lgroup that encapsulates this CPU's CMT hierarchy
721fb2f18f8Sesaxe 	 */
722fb2f18f8Sesaxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
723a6604450Sesaxe 
724fb2f18f8Sesaxe 	lgrp = pg_cmt_find_lgrp(lgrp_handle);
7253e81cacfSEric Saxe 	if (ncpus == 1 && lgrp != cpu0_lgrp) {
726a6604450Sesaxe 		/*
7273e81cacfSEric Saxe 		 * One might wonder how we could be deconfiguring the
7283e81cacfSEric Saxe 		 * only CPU in the system.
729a6604450Sesaxe 		 *
7303e81cacfSEric Saxe 		 * On Starcat systems when null_proc_lpa is detected,
7313e81cacfSEric Saxe 		 * the boot CPU (which is already configured into a leaf
7323e81cacfSEric Saxe 		 * lgroup), is moved into the root lgroup. This is done by
7333e81cacfSEric Saxe 		 * deconfiguring it from both lgroups and processor
7343e81cacfSEric Saxe 		 * groups), and then later reconfiguring it back in.  This
7353e81cacfSEric Saxe 		 * call to pg_cmt_cpu_fini() is part of that deconfiguration.
7363e81cacfSEric Saxe 		 *
7373e81cacfSEric Saxe 		 * This special case is detected by noting that the platform
7383e81cacfSEric Saxe 		 * has changed the CPU's lgrp affiliation (since it now
7393e81cacfSEric Saxe 		 * belongs in the root). In this case, use the cmt_lgrp_t
7403e81cacfSEric Saxe 		 * cached for the boot CPU, since this is what needs to be
7413e81cacfSEric Saxe 		 * torn down.
742a6604450Sesaxe 		 */
743a6604450Sesaxe 		lgrp = cpu0_lgrp;
744a6604450Sesaxe 	}
745fb2f18f8Sesaxe 
7463e81cacfSEric Saxe 	ASSERT(lgrp != NULL);
7473e81cacfSEric Saxe 
748fb2f18f8Sesaxe 	/*
749fb2f18f8Sesaxe 	 * First, clean up anything load balancing specific for each of
750fb2f18f8Sesaxe 	 * the CPU's PGs that participated in CMT load balancing
751fb2f18f8Sesaxe 	 */
752*47ab0c7cSEric Saxe 	pg = (pg_cmt_t *)cpu_pg->cmt_lineage;
753fb2f18f8Sesaxe 	while (pg != NULL) {
754fb2f18f8Sesaxe 
755fb2f18f8Sesaxe 		/*
756fb2f18f8Sesaxe 		 * Remove the PG from the CPU's load balancing lineage
757fb2f18f8Sesaxe 		 */
758fb2f18f8Sesaxe 		(void) group_remove(cmt_pgs, pg, GRP_RESIZE);
759fb2f18f8Sesaxe 
760fb2f18f8Sesaxe 		/*
761fb2f18f8Sesaxe 		 * If it's about to become empty, destroy it's children
762fb2f18f8Sesaxe 		 * group, and remove it's reference from it's siblings.
763fb2f18f8Sesaxe 		 * This is done here (rather than below) to avoid removing
764fb2f18f8Sesaxe 		 * our reference from a PG that we just eliminated.
765fb2f18f8Sesaxe 		 */
766fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 1) {
767fb2f18f8Sesaxe 			if (pg->cmt_children != NULL)
768fb2f18f8Sesaxe 				group_destroy(pg->cmt_children);
769fb2f18f8Sesaxe 			if (pg->cmt_siblings != NULL) {
770fb2f18f8Sesaxe 				if (pg->cmt_siblings == &lgrp->cl_pgs)
771fb2f18f8Sesaxe 					lgrp->cl_npgs--;
772fb2f18f8Sesaxe 				else
773fb2f18f8Sesaxe 					pg->cmt_parent->cmt_nchildren--;
774fb2f18f8Sesaxe 			}
775fb2f18f8Sesaxe 		}
776fb2f18f8Sesaxe 		pg = pg->cmt_parent;
777fb2f18f8Sesaxe 	}
778fb2f18f8Sesaxe 	ASSERT(GROUP_SIZE(cmt_pgs) == 0);
779fb2f18f8Sesaxe 
780fb2f18f8Sesaxe 	/*
781fb2f18f8Sesaxe 	 * Now that the load balancing lineage updates have happened,
782fb2f18f8Sesaxe 	 * remove the CPU from all it's PGs (destroying any that become
783fb2f18f8Sesaxe 	 * empty).
784fb2f18f8Sesaxe 	 */
785fb2f18f8Sesaxe 	group_iter_init(&i);
786fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
787fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
788fb2f18f8Sesaxe 			continue;
789fb2f18f8Sesaxe 
790*47ab0c7cSEric Saxe 		pg_cpu_delete((pg_t *)pg, cp, cpu_pg);
791fb2f18f8Sesaxe 		/*
792fb2f18f8Sesaxe 		 * Deleting the CPU from the PG changes the CPU's
793fb2f18f8Sesaxe 		 * PG group over which we are actively iterating
794fb2f18f8Sesaxe 		 * Re-initialize the iteration
795fb2f18f8Sesaxe 		 */
796fb2f18f8Sesaxe 		group_iter_init(&i);
797fb2f18f8Sesaxe 
798fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 0) {
799fb2f18f8Sesaxe 
800fb2f18f8Sesaxe 			/*
801fb2f18f8Sesaxe 			 * The PG has become zero sized, so destroy it.
802fb2f18f8Sesaxe 			 */
803fb2f18f8Sesaxe 			group_destroy(&pg->cmt_cpus_actv);
804fb2f18f8Sesaxe 			bitset_fini(&pg->cmt_cpus_actv_set);
805fb2f18f8Sesaxe 			pghw_fini((pghw_t *)pg);
806fb2f18f8Sesaxe 
807fb2f18f8Sesaxe 			pg_destroy((pg_t *)pg);
808fb2f18f8Sesaxe 		}
809fb2f18f8Sesaxe 	}
810fb2f18f8Sesaxe }
811fb2f18f8Sesaxe 
812fb2f18f8Sesaxe /*
813fb2f18f8Sesaxe  * Class callback when a CPU is entering a cpu partition
814fb2f18f8Sesaxe  */
815fb2f18f8Sesaxe static void
816fb2f18f8Sesaxe pg_cmt_cpupart_in(cpu_t *cp, cpupart_t *pp)
817fb2f18f8Sesaxe {
818fb2f18f8Sesaxe 	group_t		*pgs;
819fb2f18f8Sesaxe 	pg_t		*pg;
820fb2f18f8Sesaxe 	group_iter_t	i;
821fb2f18f8Sesaxe 
822fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
823fb2f18f8Sesaxe 
8240e751525SEric Saxe 	if (cmt_sched_disabled)
8250e751525SEric Saxe 		return;
8260e751525SEric Saxe 
827fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
828fb2f18f8Sesaxe 
829fb2f18f8Sesaxe 	/*
830fb2f18f8Sesaxe 	 * Ensure that the new partition's PG bitset
831fb2f18f8Sesaxe 	 * is large enough for all CMT PG's to which cp
832fb2f18f8Sesaxe 	 * belongs
833fb2f18f8Sesaxe 	 */
834fb2f18f8Sesaxe 	group_iter_init(&i);
835fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
836fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
837fb2f18f8Sesaxe 			continue;
838fb2f18f8Sesaxe 
839fb2f18f8Sesaxe 		if (bitset_capacity(&pp->cp_cmt_pgs) <= pg->pg_id)
840fb2f18f8Sesaxe 			bitset_resize(&pp->cp_cmt_pgs, pg->pg_id + 1);
841fb2f18f8Sesaxe 	}
842fb2f18f8Sesaxe }
843fb2f18f8Sesaxe 
844fb2f18f8Sesaxe /*
845fb2f18f8Sesaxe  * Class callback when a CPU is actually moving partitions
846fb2f18f8Sesaxe  */
847fb2f18f8Sesaxe static void
848fb2f18f8Sesaxe pg_cmt_cpupart_move(cpu_t *cp, cpupart_t *oldpp, cpupart_t *newpp)
849fb2f18f8Sesaxe {
850fb2f18f8Sesaxe 	cpu_t		*cpp;
851fb2f18f8Sesaxe 	group_t		*pgs;
852fb2f18f8Sesaxe 	pg_t		*pg;
853fb2f18f8Sesaxe 	group_iter_t	pg_iter;
854fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_iter;
855fb2f18f8Sesaxe 	boolean_t	found;
856fb2f18f8Sesaxe 
857fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
858fb2f18f8Sesaxe 
8590e751525SEric Saxe 	if (cmt_sched_disabled)
8600e751525SEric Saxe 		return;
8610e751525SEric Saxe 
862fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
863fb2f18f8Sesaxe 	group_iter_init(&pg_iter);
864fb2f18f8Sesaxe 
865fb2f18f8Sesaxe 	/*
866fb2f18f8Sesaxe 	 * Iterate over the CPUs CMT PGs
867fb2f18f8Sesaxe 	 */
868fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &pg_iter)) != NULL) {
869fb2f18f8Sesaxe 
870fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
871fb2f18f8Sesaxe 			continue;
872fb2f18f8Sesaxe 
873fb2f18f8Sesaxe 		/*
874fb2f18f8Sesaxe 		 * Add the PG to the bitset in the new partition.
875fb2f18f8Sesaxe 		 */
876fb2f18f8Sesaxe 		bitset_add(&newpp->cp_cmt_pgs, pg->pg_id);
877fb2f18f8Sesaxe 
878fb2f18f8Sesaxe 		/*
879fb2f18f8Sesaxe 		 * Remove the PG from the bitset in the old partition
880fb2f18f8Sesaxe 		 * if the last of the PG's CPUs have left.
881fb2f18f8Sesaxe 		 */
882fb2f18f8Sesaxe 		found = B_FALSE;
883fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
884fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_iter)) != NULL) {
885fb2f18f8Sesaxe 			if (cpp == cp)
886fb2f18f8Sesaxe 				continue;
887a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
888a6604450Sesaxe 			    cpp->cpu_part->cp_id == oldpp->cp_id) {
889fb2f18f8Sesaxe 				found = B_TRUE;
890fb2f18f8Sesaxe 				break;
891fb2f18f8Sesaxe 			}
892fb2f18f8Sesaxe 		}
893fb2f18f8Sesaxe 		if (!found)
894fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs, pg->pg_id);
895fb2f18f8Sesaxe 	}
896fb2f18f8Sesaxe }
897fb2f18f8Sesaxe 
898fb2f18f8Sesaxe /*
899fb2f18f8Sesaxe  * Class callback when a CPU becomes active (online)
900fb2f18f8Sesaxe  *
901fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
902fb2f18f8Sesaxe  */
903fb2f18f8Sesaxe static void
904fb2f18f8Sesaxe pg_cmt_cpu_active(cpu_t *cp)
905fb2f18f8Sesaxe {
906fb2f18f8Sesaxe 	int		err;
907fb2f18f8Sesaxe 	group_iter_t	i;
908fb2f18f8Sesaxe 	pg_cmt_t	*pg;
909fb2f18f8Sesaxe 	group_t		*pgs;
910fb2f18f8Sesaxe 
911fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
912fb2f18f8Sesaxe 
9130e751525SEric Saxe 	if (cmt_sched_disabled)
9140e751525SEric Saxe 		return;
9150e751525SEric Saxe 
916fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
917fb2f18f8Sesaxe 	group_iter_init(&i);
918fb2f18f8Sesaxe 
919fb2f18f8Sesaxe 	/*
920fb2f18f8Sesaxe 	 * Iterate over the CPU's PGs
921fb2f18f8Sesaxe 	 */
922fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
923fb2f18f8Sesaxe 
924fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
925fb2f18f8Sesaxe 			continue;
926fb2f18f8Sesaxe 
927fb2f18f8Sesaxe 		err = group_add(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
928fb2f18f8Sesaxe 		ASSERT(err == 0);
929fb2f18f8Sesaxe 
930fb2f18f8Sesaxe 		/*
931fb2f18f8Sesaxe 		 * If this is the first active CPU in the PG, and it
932fb2f18f8Sesaxe 		 * represents a hardware sharing relationship over which
933fb2f18f8Sesaxe 		 * CMT load balancing is performed, add it as a candidate
934fb2f18f8Sesaxe 		 * for balancing with it's siblings.
935fb2f18f8Sesaxe 		 */
936fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 1 &&
9370e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
938fb2f18f8Sesaxe 			err = group_add(pg->cmt_siblings, pg, GRP_NORESIZE);
939fb2f18f8Sesaxe 			ASSERT(err == 0);
9406890d023SEric Saxe 
9416890d023SEric Saxe 			/*
9426890d023SEric Saxe 			 * If this is a top level PG, add it as a balancing
9430e751525SEric Saxe 			 * candidate when balancing within the root lgroup.
9446890d023SEric Saxe 			 */
9450e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
9460e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
9476890d023SEric Saxe 				err = group_add(&cmt_root->cl_pgs, pg,
9486890d023SEric Saxe 				    GRP_NORESIZE);
9496890d023SEric Saxe 				ASSERT(err == 0);
9506890d023SEric Saxe 			}
951fb2f18f8Sesaxe 		}
952fb2f18f8Sesaxe 
953fb2f18f8Sesaxe 		/*
954fb2f18f8Sesaxe 		 * Notate the CPU in the PGs active CPU bitset.
955fb2f18f8Sesaxe 		 * Also notate the PG as being active in it's associated
956fb2f18f8Sesaxe 		 * partition
957fb2f18f8Sesaxe 		 */
958fb2f18f8Sesaxe 		bitset_add(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
959fb2f18f8Sesaxe 		bitset_add(&cp->cpu_part->cp_cmt_pgs, ((pg_t *)pg)->pg_id);
960fb2f18f8Sesaxe 	}
961fb2f18f8Sesaxe }
962fb2f18f8Sesaxe 
963fb2f18f8Sesaxe /*
964fb2f18f8Sesaxe  * Class callback when a CPU goes inactive (offline)
965fb2f18f8Sesaxe  *
966fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
967fb2f18f8Sesaxe  */
968fb2f18f8Sesaxe static void
969fb2f18f8Sesaxe pg_cmt_cpu_inactive(cpu_t *cp)
970fb2f18f8Sesaxe {
971fb2f18f8Sesaxe 	int		err;
972fb2f18f8Sesaxe 	group_t		*pgs;
973fb2f18f8Sesaxe 	pg_cmt_t	*pg;
974fb2f18f8Sesaxe 	cpu_t		*cpp;
975fb2f18f8Sesaxe 	group_iter_t	i;
976fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_itr;
977fb2f18f8Sesaxe 	boolean_t	found;
978fb2f18f8Sesaxe 
979fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
980fb2f18f8Sesaxe 
9810e751525SEric Saxe 	if (cmt_sched_disabled)
9820e751525SEric Saxe 		return;
9830e751525SEric Saxe 
984fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
985fb2f18f8Sesaxe 	group_iter_init(&i);
986fb2f18f8Sesaxe 
987fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
988fb2f18f8Sesaxe 
989fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
990fb2f18f8Sesaxe 			continue;
991fb2f18f8Sesaxe 
992fb2f18f8Sesaxe 		/*
993fb2f18f8Sesaxe 		 * Remove the CPU from the CMT PGs active CPU group
994fb2f18f8Sesaxe 		 * bitmap
995fb2f18f8Sesaxe 		 */
996fb2f18f8Sesaxe 		err = group_remove(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
997fb2f18f8Sesaxe 		ASSERT(err == 0);
998fb2f18f8Sesaxe 
999fb2f18f8Sesaxe 		bitset_del(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
1000fb2f18f8Sesaxe 
1001fb2f18f8Sesaxe 		/*
1002fb2f18f8Sesaxe 		 * If there are no more active CPUs in this PG over which
1003fb2f18f8Sesaxe 		 * load was balanced, remove it as a balancing candidate.
1004fb2f18f8Sesaxe 		 */
1005fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 0 &&
10060e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
1007fb2f18f8Sesaxe 			err = group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
1008fb2f18f8Sesaxe 			ASSERT(err == 0);
10096890d023SEric Saxe 
10100e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
10110e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
10126890d023SEric Saxe 				err = group_remove(&cmt_root->cl_pgs, pg,
10136890d023SEric Saxe 				    GRP_NORESIZE);
10146890d023SEric Saxe 				ASSERT(err == 0);
10156890d023SEric Saxe 			}
1016fb2f18f8Sesaxe 		}
1017fb2f18f8Sesaxe 
1018fb2f18f8Sesaxe 		/*
1019fb2f18f8Sesaxe 		 * Assert the number of active CPUs does not exceed
1020fb2f18f8Sesaxe 		 * the total number of CPUs in the PG
1021fb2f18f8Sesaxe 		 */
1022fb2f18f8Sesaxe 		ASSERT(GROUP_SIZE(&pg->cmt_cpus_actv) <=
1023fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
1024fb2f18f8Sesaxe 
1025fb2f18f8Sesaxe 		/*
1026fb2f18f8Sesaxe 		 * Update the PG bitset in the CPU's old partition
1027fb2f18f8Sesaxe 		 */
1028fb2f18f8Sesaxe 		found = B_FALSE;
1029fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_itr);
1030fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_itr)) != NULL) {
1031fb2f18f8Sesaxe 			if (cpp == cp)
1032fb2f18f8Sesaxe 				continue;
1033a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
1034a6604450Sesaxe 			    cpp->cpu_part->cp_id == cp->cpu_part->cp_id) {
1035fb2f18f8Sesaxe 				found = B_TRUE;
1036fb2f18f8Sesaxe 				break;
1037fb2f18f8Sesaxe 			}
1038fb2f18f8Sesaxe 		}
1039fb2f18f8Sesaxe 		if (!found) {
1040fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs,
1041fb2f18f8Sesaxe 			    ((pg_t *)pg)->pg_id);
1042fb2f18f8Sesaxe 		}
1043fb2f18f8Sesaxe 	}
1044fb2f18f8Sesaxe }
1045fb2f18f8Sesaxe 
1046fb2f18f8Sesaxe /*
1047fb2f18f8Sesaxe  * Return non-zero if the CPU belongs in the given PG
1048fb2f18f8Sesaxe  */
1049fb2f18f8Sesaxe static int
1050fb2f18f8Sesaxe pg_cmt_cpu_belongs(pg_t *pg, cpu_t *cp)
1051fb2f18f8Sesaxe {
1052fb2f18f8Sesaxe 	cpu_t	*pg_cpu;
1053fb2f18f8Sesaxe 
1054fb2f18f8Sesaxe 	pg_cpu = GROUP_ACCESS(&pg->pg_cpus, 0);
1055fb2f18f8Sesaxe 
1056fb2f18f8Sesaxe 	ASSERT(pg_cpu != NULL);
1057fb2f18f8Sesaxe 
1058fb2f18f8Sesaxe 	/*
1059fb2f18f8Sesaxe 	 * The CPU belongs if, given the nature of the hardware sharing
1060fb2f18f8Sesaxe 	 * relationship represented by the PG, the CPU has that
1061fb2f18f8Sesaxe 	 * relationship with some other CPU already in the PG
1062fb2f18f8Sesaxe 	 */
1063fb2f18f8Sesaxe 	if (pg_plat_cpus_share(cp, pg_cpu, ((pghw_t *)pg)->pghw_hw))
1064fb2f18f8Sesaxe 		return (1);
1065fb2f18f8Sesaxe 
1066fb2f18f8Sesaxe 	return (0);
1067fb2f18f8Sesaxe }
1068fb2f18f8Sesaxe 
1069fb2f18f8Sesaxe /*
10700e751525SEric Saxe  * Sort the CPUs CMT hierarchy, where "size" is the number of levels.
1071fb2f18f8Sesaxe  */
1072fb2f18f8Sesaxe static void
10730e751525SEric Saxe pg_cmt_hier_sort(pg_cmt_t **hier, int size)
1074fb2f18f8Sesaxe {
10750e751525SEric Saxe 	int		i, j, inc;
10760e751525SEric Saxe 	pg_t		*tmp;
10770e751525SEric Saxe 	pg_t		**h = (pg_t **)hier;
1078fb2f18f8Sesaxe 
10790e751525SEric Saxe 	/*
10800e751525SEric Saxe 	 * First sort by number of CPUs
10810e751525SEric Saxe 	 */
10820e751525SEric Saxe 	inc = size / 2;
10830e751525SEric Saxe 	while (inc > 0) {
10840e751525SEric Saxe 		for (i = inc; i < size; i++) {
10850e751525SEric Saxe 			j = i;
10860e751525SEric Saxe 			tmp = h[i];
10870e751525SEric Saxe 			while ((j >= inc) &&
10880e751525SEric Saxe 			    (PG_NUM_CPUS(h[j - inc]) > PG_NUM_CPUS(tmp))) {
10890e751525SEric Saxe 				h[j] = h[j - inc];
10900e751525SEric Saxe 				j = j - inc;
10910e751525SEric Saxe 			}
10920e751525SEric Saxe 			h[j] = tmp;
10930e751525SEric Saxe 		}
10940e751525SEric Saxe 		if (inc == 2)
10950e751525SEric Saxe 			inc = 1;
10960e751525SEric Saxe 		else
10970e751525SEric Saxe 			inc = (inc * 5) / 11;
10980e751525SEric Saxe 	}
1099fb2f18f8Sesaxe 
11000e751525SEric Saxe 	/*
11010e751525SEric Saxe 	 * Break ties by asking the platform.
11020e751525SEric Saxe 	 * Determine if h[i] outranks h[i + 1] and if so, swap them.
11030e751525SEric Saxe 	 */
11040e751525SEric Saxe 	for (i = 0; i < size - 1; i++) {
11050e751525SEric Saxe 		if ((PG_NUM_CPUS(h[i]) == PG_NUM_CPUS(h[i + 1])) &&
11060e751525SEric Saxe 		    pg_cmt_hier_rank(hier[i], hier[i + 1]) == hier[i]) {
11070e751525SEric Saxe 			tmp = h[i];
11080e751525SEric Saxe 			h[i] = h[i + 1];
11090e751525SEric Saxe 			h[i + 1] = tmp;
1110fb2f18f8Sesaxe 		}
1111fb2f18f8Sesaxe 	}
1112fb2f18f8Sesaxe }
1113fb2f18f8Sesaxe 
1114fb2f18f8Sesaxe /*
1115fb2f18f8Sesaxe  * Return a cmt_lgrp_t * given an lgroup handle.
1116fb2f18f8Sesaxe  */
1117fb2f18f8Sesaxe static cmt_lgrp_t *
1118fb2f18f8Sesaxe pg_cmt_find_lgrp(lgrp_handle_t hand)
1119fb2f18f8Sesaxe {
1120fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
1121fb2f18f8Sesaxe 
1122fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1123fb2f18f8Sesaxe 
1124fb2f18f8Sesaxe 	lgrp = cmt_lgrps;
1125fb2f18f8Sesaxe 	while (lgrp != NULL) {
1126fb2f18f8Sesaxe 		if (lgrp->cl_hand == hand)
1127a6604450Sesaxe 			break;
1128fb2f18f8Sesaxe 		lgrp = lgrp->cl_next;
1129fb2f18f8Sesaxe 	}
1130a6604450Sesaxe 	return (lgrp);
1131a6604450Sesaxe }
1132fb2f18f8Sesaxe 
1133fb2f18f8Sesaxe /*
1134a6604450Sesaxe  * Create a cmt_lgrp_t with the specified handle.
1135fb2f18f8Sesaxe  */
1136a6604450Sesaxe static cmt_lgrp_t *
1137a6604450Sesaxe pg_cmt_lgrp_create(lgrp_handle_t hand)
1138a6604450Sesaxe {
1139a6604450Sesaxe 	cmt_lgrp_t	*lgrp;
1140a6604450Sesaxe 
1141a6604450Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1142a6604450Sesaxe 
1143fb2f18f8Sesaxe 	lgrp = kmem_zalloc(sizeof (cmt_lgrp_t), KM_SLEEP);
1144fb2f18f8Sesaxe 
1145fb2f18f8Sesaxe 	lgrp->cl_hand = hand;
1146fb2f18f8Sesaxe 	lgrp->cl_npgs = 0;
1147fb2f18f8Sesaxe 	lgrp->cl_next = cmt_lgrps;
1148fb2f18f8Sesaxe 	cmt_lgrps = lgrp;
1149fb2f18f8Sesaxe 	group_create(&lgrp->cl_pgs);
1150fb2f18f8Sesaxe 
1151fb2f18f8Sesaxe 	return (lgrp);
1152fb2f18f8Sesaxe }
11536890d023SEric Saxe 
11546890d023SEric Saxe /*
11550e751525SEric Saxe  * Interfaces to enable and disable power aware dispatching
11560e751525SEric Saxe  * The caller must be holding cpu_lock.
11576890d023SEric Saxe  *
11580e751525SEric Saxe  * Return 0 on success and -1 on failure.
11596890d023SEric Saxe  */
11600e751525SEric Saxe int
11610e751525SEric Saxe cmt_pad_enable(pghw_type_t type)
11626890d023SEric Saxe {
11630e751525SEric Saxe 	group_t		*hwset;
11640e751525SEric Saxe 	group_iter_t	iter;
11650e751525SEric Saxe 	pg_cmt_t	*pg;
11666890d023SEric Saxe 
11670e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
11680e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
11696890d023SEric Saxe 
11700e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL ||
11710e751525SEric Saxe 	    cmt_hw_blacklisted[type]) {
11720e751525SEric Saxe 		/*
11730e751525SEric Saxe 		 * Unable to find any instances of the specified type
11740e751525SEric Saxe 		 * of power domain, or the power domains have been blacklisted.
11750e751525SEric Saxe 		 */
11760e751525SEric Saxe 		return (-1);
11770e751525SEric Saxe 	}
11786890d023SEric Saxe 
11796890d023SEric Saxe 	/*
11800e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
11810e751525SEric Saxe 	 * policy for power/performance optimization.
11820e751525SEric Saxe 	 *
11830e751525SEric Saxe 	 * Simply setting the policy isn't enough in the case where the power
11840e751525SEric Saxe 	 * domain is an only child of another PG. Because the dispatcher walks
11850e751525SEric Saxe 	 * the PG hierarchy in a top down fashion, the higher up PG's policy
11860e751525SEric Saxe 	 * will dominate. So promote the power domain above it's parent if both
11870e751525SEric Saxe 	 * PG and it's parent have the same CPUs to ensure it's policy
11880e751525SEric Saxe 	 * dominates.
11896890d023SEric Saxe 	 */
11900e751525SEric Saxe 	group_iter_init(&iter);
11910e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
11920e751525SEric Saxe 		/*
11930e751525SEric Saxe 		 * If the power domain is an only child to a parent
11940e751525SEric Saxe 		 * not implementing the same policy, promote the child
11950e751525SEric Saxe 		 * above the parent to activate the policy.
11960e751525SEric Saxe 		 */
11970e751525SEric Saxe 		pg->cmt_policy = pg_cmt_policy(((pghw_t *)pg)->pghw_hw);
11980e751525SEric Saxe 		while ((pg->cmt_parent != NULL) &&
11990e751525SEric Saxe 		    (pg->cmt_parent->cmt_policy != pg->cmt_policy) &&
12000e751525SEric Saxe 		    (PG_NUM_CPUS((pg_t *)pg) ==
12010e751525SEric Saxe 		    PG_NUM_CPUS((pg_t *)pg->cmt_parent))) {
12020e751525SEric Saxe 			cmt_hier_promote(pg);
12030e751525SEric Saxe 		}
12040e751525SEric Saxe 	}
12050e751525SEric Saxe 
12060e751525SEric Saxe 	return (0);
12070e751525SEric Saxe }
12080e751525SEric Saxe 
12090e751525SEric Saxe int
12100e751525SEric Saxe cmt_pad_disable(pghw_type_t type)
12110e751525SEric Saxe {
12120e751525SEric Saxe 	group_t		*hwset;
12130e751525SEric Saxe 	group_iter_t	iter;
12140e751525SEric Saxe 	pg_cmt_t	*pg;
12150e751525SEric Saxe 	pg_cmt_t	*child;
12160e751525SEric Saxe 
12170e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
12180e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
12190e751525SEric Saxe 
12200e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL) {
12210e751525SEric Saxe 		/*
12220e751525SEric Saxe 		 * Unable to find any instances of the specified type of
12230e751525SEric Saxe 		 * power domain.
12240e751525SEric Saxe 		 */
12250e751525SEric Saxe 		return (-1);
12260e751525SEric Saxe 	}
12270e751525SEric Saxe 	/*
12280e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
12290e751525SEric Saxe 	 * policy for performance optimization (load balancing).
12300e751525SEric Saxe 	 */
12310e751525SEric Saxe 	group_iter_init(&iter);
12320e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
12330e751525SEric Saxe 
12340e751525SEric Saxe 		/*
12350e751525SEric Saxe 		 * If the power domain has an only child that implements
12360e751525SEric Saxe 		 * policy other than load balancing, promote the child
12370e751525SEric Saxe 		 * above the power domain to ensure it's policy dominates.
12380e751525SEric Saxe 		 */
1239f03808b6SEric Saxe 		if (pg->cmt_children != NULL &&
1240f03808b6SEric Saxe 		    GROUP_SIZE(pg->cmt_children) == 1) {
12410e751525SEric Saxe 			child = GROUP_ACCESS(pg->cmt_children, 0);
12420e751525SEric Saxe 			if ((child->cmt_policy & CMT_BALANCE) == 0) {
12430e751525SEric Saxe 				cmt_hier_promote(child);
12440e751525SEric Saxe 			}
12450e751525SEric Saxe 		}
12460e751525SEric Saxe 		pg->cmt_policy = CMT_BALANCE;
12470e751525SEric Saxe 	}
12480e751525SEric Saxe 	return (0);
12490e751525SEric Saxe }
12500e751525SEric Saxe 
12510e751525SEric Saxe /* ARGSUSED */
12520e751525SEric Saxe static void
12530e751525SEric Saxe cmt_ev_thread_swtch(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
12540e751525SEric Saxe 		    kthread_t *new)
12550e751525SEric Saxe {
12560e751525SEric Saxe 	pg_cmt_t	*cmt_pg = (pg_cmt_t *)pg;
12570e751525SEric Saxe 
12580e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
12590e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, 1);
12600e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
12610e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, -1);
12620e751525SEric Saxe 	}
12630e751525SEric Saxe }
12640e751525SEric Saxe 
12650e751525SEric Saxe /*
12660e751525SEric Saxe  * Macro to test whether a thread is currently runnable on a CPU in a PG.
12670e751525SEric Saxe  */
12680e751525SEric Saxe #define	THREAD_RUNNABLE_IN_PG(t, pg)					\
12690e751525SEric Saxe 	((t)->t_state == TS_RUN &&					\
12700e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu &&				\
12710e751525SEric Saxe 	    bitset_in_set(&(pg)->cmt_cpus_actv_set,			\
12720e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu->cpu_seqid))
12730e751525SEric Saxe 
12740e751525SEric Saxe static void
12750e751525SEric Saxe cmt_ev_thread_swtch_pwr(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
12760e751525SEric Saxe     kthread_t *new)
12770e751525SEric Saxe {
12780e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
12790e751525SEric Saxe 	cpupm_domain_t	*dom;
12800e751525SEric Saxe 	uint32_t	u;
12810e751525SEric Saxe 
12820e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
12830e751525SEric Saxe 		ASSERT(new != cp->cpu_idle_thread);
12840e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, 1);
12850e751525SEric Saxe 		if (u == 1) {
12860e751525SEric Saxe 			/*
12870e751525SEric Saxe 			 * Notify the CPU power manager that the domain
12880e751525SEric Saxe 			 * is non-idle.
12890e751525SEric Saxe 			 */
12900e751525SEric Saxe 			dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
12910e751525SEric Saxe 			cpupm_utilization_event(cp, now, dom,
12920e751525SEric Saxe 			    CPUPM_DOM_BUSY_FROM_IDLE);
12930e751525SEric Saxe 		}
12940e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
12950e751525SEric Saxe 		ASSERT(old != cp->cpu_idle_thread);
12960e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, -1);
12970e751525SEric Saxe 		if (u == 0) {
12980e751525SEric Saxe 			/*
12990e751525SEric Saxe 			 * The domain is idle, notify the CPU power
13000e751525SEric Saxe 			 * manager.
13010e751525SEric Saxe 			 *
13020e751525SEric Saxe 			 * Avoid notifying if the thread is simply migrating
13030e751525SEric Saxe 			 * between CPUs in the domain.
13040e751525SEric Saxe 			 */
13050e751525SEric Saxe 			if (!THREAD_RUNNABLE_IN_PG(old, cmt)) {
13060e751525SEric Saxe 				dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13070e751525SEric Saxe 				cpupm_utilization_event(cp, now, dom,
13080e751525SEric Saxe 				    CPUPM_DOM_IDLE_FROM_BUSY);
13090e751525SEric Saxe 			}
13100e751525SEric Saxe 		}
13110e751525SEric Saxe 	}
13120e751525SEric Saxe }
13130e751525SEric Saxe 
13140e751525SEric Saxe /* ARGSUSED */
13150e751525SEric Saxe static void
13160e751525SEric Saxe cmt_ev_thread_remain_pwr(pg_t *pg, cpu_t *cp, kthread_t *t)
13170e751525SEric Saxe {
13180e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
13190e751525SEric Saxe 	cpupm_domain_t	*dom;
13200e751525SEric Saxe 
13210e751525SEric Saxe 	dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13220e751525SEric Saxe 	cpupm_utilization_event(cp, (hrtime_t)0, dom, CPUPM_DOM_REMAIN_BUSY);
13230e751525SEric Saxe }
13240e751525SEric Saxe 
13250e751525SEric Saxe /*
13260e751525SEric Saxe  * Return the name of the CMT scheduling policy
13270e751525SEric Saxe  * being implemented across this PG
13280e751525SEric Saxe  */
13290e751525SEric Saxe static char *
13300e751525SEric Saxe pg_cmt_policy_name(pg_t *pg)
13310e751525SEric Saxe {
13320e751525SEric Saxe 	pg_cmt_policy_t policy;
13330e751525SEric Saxe 
13340e751525SEric Saxe 	policy = ((pg_cmt_t *)pg)->cmt_policy;
13350e751525SEric Saxe 
13360e751525SEric Saxe 	if (policy & CMT_AFFINITY) {
13370e751525SEric Saxe 		if (policy & CMT_BALANCE)
13380e751525SEric Saxe 			return ("Load Balancing & Affinity");
13390e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13400e751525SEric Saxe 			return ("Load Coalescence & Affinity");
13416890d023SEric Saxe 		else
13420e751525SEric Saxe 			return ("Affinity");
13430e751525SEric Saxe 	} else {
13440e751525SEric Saxe 		if (policy & CMT_BALANCE)
13450e751525SEric Saxe 			return ("Load Balancing");
13460e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13470e751525SEric Saxe 			return ("Load Coalescence");
13480e751525SEric Saxe 		else
13490e751525SEric Saxe 			return ("None");
13500e751525SEric Saxe 	}
13510e751525SEric Saxe }
13526890d023SEric Saxe 
13536890d023SEric Saxe /*
13540e751525SEric Saxe  * Prune PG, and all other instances of PG's hardware sharing relationship
13550e751525SEric Saxe  * from the PG hierarchy.
13566890d023SEric Saxe  */
13570e751525SEric Saxe static int
13580e751525SEric Saxe pg_cmt_prune(pg_cmt_t *pg_bad, pg_cmt_t **lineage, int *sz)
13590e751525SEric Saxe {
13600e751525SEric Saxe 	group_t		*hwset, *children;
13610e751525SEric Saxe 	int		i, j, r, size = *sz;
13620e751525SEric Saxe 	group_iter_t	hw_iter, child_iter;
13630e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
13640e751525SEric Saxe 	pg_cmt_t	*pg, *child;
13650e751525SEric Saxe 	cpu_t		*cpu;
13660e751525SEric Saxe 	int		cap_needed;
13670e751525SEric Saxe 	pghw_type_t	hw;
13686890d023SEric Saxe 
13690e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
13706890d023SEric Saxe 
13710e751525SEric Saxe 	hw = ((pghw_t *)pg_bad)->pghw_hw;
13720e751525SEric Saxe 
13730e751525SEric Saxe 	if (hw == PGHW_POW_ACTIVE) {
13740e751525SEric Saxe 		cmn_err(CE_NOTE, "!Active CPUPM domain groups look suspect. "
13750e751525SEric Saxe 		    "Event Based CPUPM Unavailable");
13760e751525SEric Saxe 	} else if (hw == PGHW_POW_IDLE) {
13770e751525SEric Saxe 		cmn_err(CE_NOTE, "!Idle CPUPM domain groups look suspect. "
13780e751525SEric Saxe 		    "Dispatcher assisted CPUPM disabled.");
13790e751525SEric Saxe 	}
13806890d023SEric Saxe 
13816890d023SEric Saxe 	/*
13820e751525SEric Saxe 	 * Find and eliminate the PG from the lineage.
13836890d023SEric Saxe 	 */
13840e751525SEric Saxe 	for (i = 0; i < size; i++) {
13850e751525SEric Saxe 		if (lineage[i] == pg_bad) {
13860e751525SEric Saxe 			for (j = i; j < size - 1; j++)
13870e751525SEric Saxe 				lineage[j] = lineage[j + 1];
13880e751525SEric Saxe 			*sz = size - 1;
13890e751525SEric Saxe 			break;
13900e751525SEric Saxe 		}
13910e751525SEric Saxe 	}
13920e751525SEric Saxe 
13930e751525SEric Saxe 	/*
13940e751525SEric Saxe 	 * We'll prune all instances of the hardware sharing relationship
13950e751525SEric Saxe 	 * represented by pg. But before we do that (and pause CPUs) we need
13960e751525SEric Saxe 	 * to ensure the hierarchy's groups are properly sized.
13970e751525SEric Saxe 	 */
13980e751525SEric Saxe 	hwset = pghw_set_lookup(hw);
13990e751525SEric Saxe 
14000e751525SEric Saxe 	/*
14010e751525SEric Saxe 	 * Blacklist the hardware so that future groups won't be created.
14020e751525SEric Saxe 	 */
14030e751525SEric Saxe 	cmt_hw_blacklisted[hw] = 1;
14040e751525SEric Saxe 
14050e751525SEric Saxe 	/*
14060e751525SEric Saxe 	 * For each of the PGs being pruned, ensure sufficient capacity in
14070e751525SEric Saxe 	 * the siblings set for the PG's children
14080e751525SEric Saxe 	 */
14090e751525SEric Saxe 	group_iter_init(&hw_iter);
14100e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14110e751525SEric Saxe 		/*
14120e751525SEric Saxe 		 * PG is being pruned, but if it is bringing up more than
14130e751525SEric Saxe 		 * one child, ask for more capacity in the siblings group.
14140e751525SEric Saxe 		 */
14150e751525SEric Saxe 		cap_needed = 0;
14160e751525SEric Saxe 		if (pg->cmt_children &&
14170e751525SEric Saxe 		    GROUP_SIZE(pg->cmt_children) > 1) {
14180e751525SEric Saxe 			cap_needed = GROUP_SIZE(pg->cmt_children) - 1;
14190e751525SEric Saxe 
14200e751525SEric Saxe 			group_expand(pg->cmt_siblings,
14210e751525SEric Saxe 			    GROUP_SIZE(pg->cmt_siblings) + cap_needed);
14220e751525SEric Saxe 
14230e751525SEric Saxe 			/*
14240e751525SEric Saxe 			 * If this is a top level group, also ensure the
14250e751525SEric Saxe 			 * capacity in the root lgrp level CMT grouping.
14260e751525SEric Saxe 			 */
14270e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
14280e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
14290e751525SEric Saxe 				group_expand(&cmt_root->cl_pgs,
14300e751525SEric Saxe 				    GROUP_SIZE(&cmt_root->cl_pgs) + cap_needed);
14310e751525SEric Saxe 			}
14320e751525SEric Saxe 		}
14330e751525SEric Saxe 	}
14340e751525SEric Saxe 
14350e751525SEric Saxe 	/*
14360e751525SEric Saxe 	 * We're operating on the PG hierarchy. Pause CPUs to ensure
14370e751525SEric Saxe 	 * exclusivity with respect to the dispatcher.
14380e751525SEric Saxe 	 */
14390e751525SEric Saxe 	pause_cpus(NULL);
14400e751525SEric Saxe 
14410e751525SEric Saxe 	/*
14420e751525SEric Saxe 	 * Prune all PG instances of the hardware sharing relationship
14430e751525SEric Saxe 	 * represented by pg.
14440e751525SEric Saxe 	 */
14450e751525SEric Saxe 	group_iter_init(&hw_iter);
14460e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14470e751525SEric Saxe 
14480e751525SEric Saxe 		/*
14490e751525SEric Saxe 		 * Remove PG from it's group of siblings, if it's there.
14500e751525SEric Saxe 		 */
14510e751525SEric Saxe 		if (pg->cmt_siblings) {
14520e751525SEric Saxe 			(void) group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
14530e751525SEric Saxe 		}
14540e751525SEric Saxe 		if (pg->cmt_parent == NULL &&
14550e751525SEric Saxe 		    pg->cmt_siblings != &cmt_root->cl_pgs) {
14560e751525SEric Saxe 			(void) group_remove(&cmt_root->cl_pgs, pg,
14570e751525SEric Saxe 			    GRP_NORESIZE);
14580e751525SEric Saxe 		}
14590e751525SEric Saxe 		/*
1460ef4f35d8SEric Saxe 		 * Move PG's children from it's children set to it's parent's
1461ef4f35d8SEric Saxe 		 * children set. Note that the parent's children set, and PG's
1462ef4f35d8SEric Saxe 		 * siblings set are the same thing.
1463ef4f35d8SEric Saxe 		 *
1464ef4f35d8SEric Saxe 		 * Because we are iterating over the same group that we are
1465ef4f35d8SEric Saxe 		 * operating on (removing the children), first add all of PG's
1466ef4f35d8SEric Saxe 		 * children to the parent's children set, and once we are done
1467ef4f35d8SEric Saxe 		 * iterating, empty PG's children set.
14680e751525SEric Saxe 		 */
14690e751525SEric Saxe 		if (pg->cmt_children != NULL) {
14700e751525SEric Saxe 			children = pg->cmt_children;
14710e751525SEric Saxe 
14720e751525SEric Saxe 			group_iter_init(&child_iter);
14730e751525SEric Saxe 			while ((child = group_iterate(children, &child_iter))
14740e751525SEric Saxe 			    != NULL) {
1475ef4f35d8SEric Saxe 				if (pg->cmt_siblings != NULL) {
14760e751525SEric Saxe 					r = group_add(pg->cmt_siblings, child,
14770e751525SEric Saxe 					    GRP_NORESIZE);
14780e751525SEric Saxe 					ASSERT(r == 0);
14790e751525SEric Saxe 				}
14800e751525SEric Saxe 			}
1481ef4f35d8SEric Saxe 			group_empty(pg->cmt_children);
14820e751525SEric Saxe 		}
14830e751525SEric Saxe 
14840e751525SEric Saxe 		/*
14850e751525SEric Saxe 		 * Reset the callbacks to the defaults
14860e751525SEric Saxe 		 */
14870e751525SEric Saxe 		pg_callback_set_defaults((pg_t *)pg);
14880e751525SEric Saxe 
14890e751525SEric Saxe 		/*
14900e751525SEric Saxe 		 * Update all the CPU lineages in each of PG's CPUs
14910e751525SEric Saxe 		 */
14920e751525SEric Saxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
14930e751525SEric Saxe 		while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
14940e751525SEric Saxe 			group_t		*pgs;
14950e751525SEric Saxe 			pg_cmt_t	*cpu_pg;
14960e751525SEric Saxe 			group_iter_t	liter;	/* Iterator for the lineage */
14970e751525SEric Saxe 
14980e751525SEric Saxe 			/*
14990e751525SEric Saxe 			 * Iterate over the CPU's PGs updating the children
15000e751525SEric Saxe 			 * of the PG being promoted, since they have a new
15010e751525SEric Saxe 			 * parent and siblings set.
15020e751525SEric Saxe 			 */
15030e751525SEric Saxe 			pgs = &cpu->cpu_pg->pgs;
15040e751525SEric Saxe 			group_iter_init(&liter);
15050e751525SEric Saxe 			while ((cpu_pg = group_iterate(pgs, &liter)) != NULL) {
15060e751525SEric Saxe 				if (cpu_pg->cmt_parent == pg) {
15070e751525SEric Saxe 					cpu_pg->cmt_parent = pg->cmt_parent;
15080e751525SEric Saxe 					cpu_pg->cmt_siblings = pg->cmt_siblings;
15090e751525SEric Saxe 				}
15100e751525SEric Saxe 			}
15110e751525SEric Saxe 
15120e751525SEric Saxe 			/*
15130e751525SEric Saxe 			 * Update the CPU's lineages
15140e751525SEric Saxe 			 */
15150e751525SEric Saxe 			pgs = &cpu->cpu_pg->cmt_pgs;
15160e751525SEric Saxe 			(void) group_remove(pgs, pg, GRP_NORESIZE);
15170e751525SEric Saxe 			pgs = &cpu->cpu_pg->pgs;
15180e751525SEric Saxe 			(void) group_remove(pgs, pg, GRP_NORESIZE);
15190e751525SEric Saxe 		}
15200e751525SEric Saxe 	}
15210e751525SEric Saxe 	start_cpus();
15220e751525SEric Saxe 	return (0);
15230e751525SEric Saxe }
15240e751525SEric Saxe 
15250e751525SEric Saxe /*
15260e751525SEric Saxe  * Disable CMT scheduling
15270e751525SEric Saxe  */
15280e751525SEric Saxe static void
15290e751525SEric Saxe pg_cmt_disable(void)
15300e751525SEric Saxe {
15310e751525SEric Saxe 	cpu_t	*cpu;
15320e751525SEric Saxe 
15330e751525SEric Saxe 	pause_cpus(NULL);
15340e751525SEric Saxe 	cpu = cpu_list;
15350e751525SEric Saxe 
15366890d023SEric Saxe 	do {
15370e751525SEric Saxe 		if (cpu->cpu_pg)
15380e751525SEric Saxe 			group_empty(&cpu->cpu_pg->cmt_pgs);
15390e751525SEric Saxe 	} while ((cpu = cpu->cpu_next) != cpu_list);
15400e751525SEric Saxe 
15410e751525SEric Saxe 	cmt_sched_disabled = 1;
15420e751525SEric Saxe 	start_cpus();
15430e751525SEric Saxe 	cmn_err(CE_NOTE, "!CMT thread placement optimizations unavailable");
15440e751525SEric Saxe }
15450e751525SEric Saxe 
1546ef4f35d8SEric Saxe /*
1547ef4f35d8SEric Saxe  * CMT lineage validation
1548ef4f35d8SEric Saxe  *
1549ef4f35d8SEric Saxe  * This routine is invoked by pg_cmt_cpu_init() to validate the integrity
1550ef4f35d8SEric Saxe  * of the PGs in a CPU's lineage. This is necessary because it's possible that
1551ef4f35d8SEric Saxe  * some groupings (power domain groupings in particular) may be defined by
1552ef4f35d8SEric Saxe  * sources that are buggy (e.g. BIOS bugs). In such cases, it may not be
1553ef4f35d8SEric Saxe  * possible to integrate those groupings into the CMT PG hierarchy, if doing
1554ef4f35d8SEric Saxe  * so would violate the subset invariant of the hierarchy, which says that
1555ef4f35d8SEric Saxe  * a PG must be subset of its parent (if it has one).
1556ef4f35d8SEric Saxe  *
1557ef4f35d8SEric Saxe  * pg_cmt_lineage_validate()'s purpose is to detect grouping definitions that
1558ef4f35d8SEric Saxe  * would result in a violation of this invariant. If a violation is found,
1559ef4f35d8SEric Saxe  * and the PG is of a grouping type who's definition is known to originate from
1560ef4f35d8SEric Saxe  * suspect sources (BIOS), then pg_cmt_prune() will be invoked to prune the
1561ef4f35d8SEric Saxe  * PG (and all other instances PG's sharing relationship type) from the
1562ef4f35d8SEric Saxe  * hierarchy. Further, future instances of that sharing relationship type won't
1563ef4f35d8SEric Saxe  * be instantiated. If the grouping definition doesn't originate from suspect
1564ef4f35d8SEric Saxe  * sources, then pg_cmt_disable() will be invoked to log an error, and disable
1565ef4f35d8SEric Saxe  * CMT scheduling altogether.
1566ef4f35d8SEric Saxe  *
1567ef4f35d8SEric Saxe  * This routine is invoked after the CPU has been added to the PGs in which
1568ef4f35d8SEric Saxe  * it belongs, but before those PGs have been added to (or had their place
1569ef4f35d8SEric Saxe  * adjusted in) the CMT PG hierarchy.
1570ef4f35d8SEric Saxe  *
1571ef4f35d8SEric Saxe  * The first argument is the CPUs PG lineage (essentially an array of PGs in
1572ef4f35d8SEric Saxe  * which the CPU belongs) that has already been sorted in ascending order
1573ef4f35d8SEric Saxe  * by CPU count. Some of the PGs in the CPUs lineage may already have other
1574ef4f35d8SEric Saxe  * CPUs in them, and have already been integrated into the CMT hierarchy.
1575ef4f35d8SEric Saxe  *
1576ef4f35d8SEric Saxe  * The addition of this new CPU to these pre-existing PGs means that those
1577ef4f35d8SEric Saxe  * PGs may need to be promoted up in the hierarchy to satisfy the subset
1578ef4f35d8SEric Saxe  * invariant. In additon to testing the subset invariant for the lineage,
1579ef4f35d8SEric Saxe  * this routine also verifies that the addition of the new CPU to the
1580ef4f35d8SEric Saxe  * existing PGs wouldn't cause the subset invariant to be violated in
1581ef4f35d8SEric Saxe  * the exiting lineages.
1582ef4f35d8SEric Saxe  *
1583ef4f35d8SEric Saxe  * This routine will normally return one of the following:
1584ef4f35d8SEric Saxe  * CMT_LINEAGE_VALID - There were no problems detected with the lineage.
1585ef4f35d8SEric Saxe  * CMT_LINEAGE_REPAIRED - Problems were detected, but repaired via pruning.
1586ef4f35d8SEric Saxe  *
1587ef4f35d8SEric Saxe  * Otherwise, this routine will return a value indicating which error it
1588ef4f35d8SEric Saxe  * was unable to recover from (and set cmt_lineage_status along the way).
1589ef4f35d8SEric Saxe  */
1590ef4f35d8SEric Saxe static cmt_lineage_validation_t
15910e751525SEric Saxe pg_cmt_lineage_validate(pg_cmt_t **lineage, int *sz)
15920e751525SEric Saxe {
1593ef4f35d8SEric Saxe 	int		i, j, size;
1594ef4f35d8SEric Saxe 	pg_cmt_t	*pg, *pg_next, *pg_bad, *pg_tmp;
15950e751525SEric Saxe 	cpu_t		*cp;
15960e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
1597ef4f35d8SEric Saxe 	lgrp_handle_t	lgrp;
15980e751525SEric Saxe 
15990e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
16000e751525SEric Saxe 
16010e751525SEric Saxe revalidate:
16020e751525SEric Saxe 	size = *sz;
16030e751525SEric Saxe 	pg_bad = NULL;
1604ef4f35d8SEric Saxe 	lgrp = LGRP_NULL_HANDLE;
1605ef4f35d8SEric Saxe 	for (i = 0; i < size; i++) {
16060e751525SEric Saxe 
16070e751525SEric Saxe 		pg = lineage[i];
1608ef4f35d8SEric Saxe 		if (i < size - 1)
1609ef4f35d8SEric Saxe 			pg_next = lineage[i + 1];
1610ef4f35d8SEric Saxe 		else
1611ef4f35d8SEric Saxe 			pg_next = NULL;
16126890d023SEric Saxe 
16136890d023SEric Saxe 		/*
16140e751525SEric Saxe 		 * We assume that the lineage has already been sorted
16150e751525SEric Saxe 		 * by the number of CPUs. In fact, we depend on it.
16166890d023SEric Saxe 		 */
1617ef4f35d8SEric Saxe 		ASSERT(pg_next == NULL ||
1618ef4f35d8SEric Saxe 		    (PG_NUM_CPUS((pg_t *)pg) <= PG_NUM_CPUS((pg_t *)pg_next)));
16196890d023SEric Saxe 
16206890d023SEric Saxe 		/*
1621ef4f35d8SEric Saxe 		 * Check to make sure that the existing parent of PG (if any)
1622ef4f35d8SEric Saxe 		 * is either in the PG's lineage, or the PG has more CPUs than
1623ef4f35d8SEric Saxe 		 * its existing parent and can and should be promoted above its
1624ef4f35d8SEric Saxe 		 * parent.
1625ef4f35d8SEric Saxe 		 *
1626ef4f35d8SEric Saxe 		 * Since the PG topology is in the middle of being changed, we
1627ef4f35d8SEric Saxe 		 * need to check whether the PG's existing parent (if any) is
1628ef4f35d8SEric Saxe 		 * part of its lineage (and therefore should contain the new
1629ef4f35d8SEric Saxe 		 * CPU). If not, it means that the addition of the new CPU
1630ef4f35d8SEric Saxe 		 * should have made this PG have more CPUs than its parent, and
1631ef4f35d8SEric Saxe 		 * this PG should be promoted to be above its existing parent
1632ef4f35d8SEric Saxe 		 * now. We need to verify all of this to defend against a buggy
1633ef4f35d8SEric Saxe 		 * BIOS giving bad power domain CPU groupings. Sigh.
1634ef4f35d8SEric Saxe 		 */
1635ef4f35d8SEric Saxe 		if (pg->cmt_parent) {
1636ef4f35d8SEric Saxe 			/*
1637ef4f35d8SEric Saxe 			 * Determine if cmt_parent is in this lineage
1638ef4f35d8SEric Saxe 			 */
1639ef4f35d8SEric Saxe 			for (j = 0; j < size; j++) {
1640ef4f35d8SEric Saxe 				pg_tmp = lineage[j];
1641ef4f35d8SEric Saxe 				if (pg_tmp == pg->cmt_parent)
1642ef4f35d8SEric Saxe 					break;
1643ef4f35d8SEric Saxe 			}
1644ef4f35d8SEric Saxe 			if (pg_tmp != pg->cmt_parent) {
1645ef4f35d8SEric Saxe 				/*
1646ef4f35d8SEric Saxe 				 * cmt_parent is not in the lineage, verify
1647ef4f35d8SEric Saxe 				 * it is a proper subset of PG.
1648ef4f35d8SEric Saxe 				 */
1649ef4f35d8SEric Saxe 				if (PG_NUM_CPUS((pg_t *)pg->cmt_parent) >=
1650ef4f35d8SEric Saxe 				    PG_NUM_CPUS((pg_t *)pg)) {
1651ef4f35d8SEric Saxe 					/*
1652ef4f35d8SEric Saxe 					 * Not a proper subset if pg has less
1653ef4f35d8SEric Saxe 					 * CPUs than cmt_parent...
1654ef4f35d8SEric Saxe 					 */
1655ef4f35d8SEric Saxe 					cmt_lineage_status =
1656ef4f35d8SEric Saxe 					    CMT_LINEAGE_NON_PROMOTABLE;
1657ef4f35d8SEric Saxe 					goto handle_error;
1658ef4f35d8SEric Saxe 				}
1659ef4f35d8SEric Saxe 			}
1660ef4f35d8SEric Saxe 		}
1661ef4f35d8SEric Saxe 
1662ef4f35d8SEric Saxe 		/*
1663ef4f35d8SEric Saxe 		 * Walk each of the CPUs in the PGs group and perform
1664ef4f35d8SEric Saxe 		 * consistency checks along the way.
16656890d023SEric Saxe 		 */
16660e751525SEric Saxe 		PG_CPU_ITR_INIT((pg_t *)pg, cpu_iter);
16670e751525SEric Saxe 		while ((cp = pg_cpu_next(&cpu_iter)) != NULL) {
1668ef4f35d8SEric Saxe 			/*
1669ef4f35d8SEric Saxe 			 * Verify that there aren't any CPUs contained in PG
1670ef4f35d8SEric Saxe 			 * that the next PG in the lineage (which is larger
1671ef4f35d8SEric Saxe 			 * or same size) doesn't also contain.
1672ef4f35d8SEric Saxe 			 */
1673ef4f35d8SEric Saxe 			if (pg_next != NULL &&
1674ef4f35d8SEric Saxe 			    pg_cpu_find((pg_t *)pg_next, cp) == B_FALSE) {
16750e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_NON_CONCENTRIC;
16760e751525SEric Saxe 				goto handle_error;
16776890d023SEric Saxe 			}
1678ef4f35d8SEric Saxe 
1679ef4f35d8SEric Saxe 			/*
1680ef4f35d8SEric Saxe 			 * Verify that all the CPUs in the PG are in the same
1681ef4f35d8SEric Saxe 			 * lgroup.
1682ef4f35d8SEric Saxe 			 */
1683ef4f35d8SEric Saxe 			if (lgrp == LGRP_NULL_HANDLE) {
1684ef4f35d8SEric Saxe 				lgrp = lgrp_plat_cpu_to_hand(cp->cpu_id);
1685ef4f35d8SEric Saxe 			} else if (lgrp_plat_cpu_to_hand(cp->cpu_id) != lgrp) {
1686ef4f35d8SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_PG_SPANS_LGRPS;
1687ef4f35d8SEric Saxe 				goto handle_error;
1688ef4f35d8SEric Saxe 			}
16890e751525SEric Saxe 		}
16906890d023SEric Saxe 	}
16916890d023SEric Saxe 
16920e751525SEric Saxe handle_error:
1693ef4f35d8SEric Saxe 	/*
1694ef4f35d8SEric Saxe 	 * Some of these validation errors can result when the CPU grouping
1695ef4f35d8SEric Saxe 	 * information is derived from buggy sources (for example, incorrect
1696ef4f35d8SEric Saxe 	 * ACPI tables on x86 systems).
1697ef4f35d8SEric Saxe 	 *
1698ef4f35d8SEric Saxe 	 * We'll try to recover in such cases by pruning out the illegal
1699ef4f35d8SEric Saxe 	 * groupings from the PG hierarchy, which means that we won't optimize
1700ef4f35d8SEric Saxe 	 * for those levels, but we will for the remaining ones.
1701ef4f35d8SEric Saxe 	 */
17020e751525SEric Saxe 	switch (cmt_lineage_status) {
17030e751525SEric Saxe 	case CMT_LINEAGE_VALID:
17040e751525SEric Saxe 	case CMT_LINEAGE_REPAIRED:
17050e751525SEric Saxe 		break;
1706ef4f35d8SEric Saxe 	case CMT_LINEAGE_PG_SPANS_LGRPS:
1707ef4f35d8SEric Saxe 		/*
1708ef4f35d8SEric Saxe 		 * We've detected a PG whose CPUs span lgroups.
1709ef4f35d8SEric Saxe 		 *
1710ef4f35d8SEric Saxe 		 * This isn't supported, as the dispatcher isn't allowed to
1711ef4f35d8SEric Saxe 		 * to do CMT thread placement across lgroups, as this would
1712ef4f35d8SEric Saxe 		 * conflict with policies implementing MPO thread affinity.
1713ef4f35d8SEric Saxe 		 *
1714ef4f35d8SEric Saxe 		 * The handling for this falls through to the next case.
1715ef4f35d8SEric Saxe 		 */
1716ef4f35d8SEric Saxe 	case CMT_LINEAGE_NON_PROMOTABLE:
1717ef4f35d8SEric Saxe 		/*
1718ef4f35d8SEric Saxe 		 * We've detected a PG that already exists in another CPU's
1719ef4f35d8SEric Saxe 		 * lineage that cannot cannot legally be promoted into place
1720ef4f35d8SEric Saxe 		 * without breaking the invariants of the hierarchy.
1721ef4f35d8SEric Saxe 		 */
1722ef4f35d8SEric Saxe 		if (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw)) {
1723ef4f35d8SEric Saxe 			if (pg_cmt_prune(pg, lineage, sz) == 0) {
1724ef4f35d8SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_REPAIRED;
1725ef4f35d8SEric Saxe 				goto revalidate;
1726ef4f35d8SEric Saxe 			}
1727ef4f35d8SEric Saxe 		}
1728ef4f35d8SEric Saxe 		/*
1729ef4f35d8SEric Saxe 		 * Something went wrong trying to prune out the bad level.
1730ef4f35d8SEric Saxe 		 * Disable CMT scheduling altogether.
1731ef4f35d8SEric Saxe 		 */
1732ef4f35d8SEric Saxe 		pg_cmt_disable();
1733ef4f35d8SEric Saxe 		break;
17340e751525SEric Saxe 	case CMT_LINEAGE_NON_CONCENTRIC:
17356890d023SEric Saxe 		/*
1736ef4f35d8SEric Saxe 		 * We've detected a non-concentric PG lineage, which means that
1737ef4f35d8SEric Saxe 		 * there's a PG in the lineage that has CPUs that the next PG
1738ef4f35d8SEric Saxe 		 * over in the lineage (which is the same size or larger)
1739ef4f35d8SEric Saxe 		 * doesn't have.
17400e751525SEric Saxe 		 *
1741ef4f35d8SEric Saxe 		 * In this case, we examine the two PGs to see if either
1742ef4f35d8SEric Saxe 		 * grouping is defined by potentially buggy sources.
17430e751525SEric Saxe 		 *
17440e751525SEric Saxe 		 * If one has less CPUs than the other, and contains CPUs
17450e751525SEric Saxe 		 * not found in the parent, and it is an untrusted enumeration,
17460e751525SEric Saxe 		 * then prune it. If both have the same number of CPUs, then
17470e751525SEric Saxe 		 * prune the one that is untrusted.
17480e751525SEric Saxe 		 *
17490e751525SEric Saxe 		 * This process repeats until we have a concentric lineage,
17500e751525SEric Saxe 		 * or we would have to prune out level derived from what we
17510e751525SEric Saxe 		 * thought was a reliable source, in which case CMT scheduling
1752ef4f35d8SEric Saxe 		 * is disabled altogether.
17536890d023SEric Saxe 		 */
1754ef4f35d8SEric Saxe 		if ((PG_NUM_CPUS((pg_t *)pg) < PG_NUM_CPUS((pg_t *)pg_next)) &&
17550e751525SEric Saxe 		    (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw))) {
17560e751525SEric Saxe 			pg_bad = pg;
17570e751525SEric Saxe 		} else if (PG_NUM_CPUS((pg_t *)pg) ==
1758ef4f35d8SEric Saxe 		    PG_NUM_CPUS((pg_t *)pg_next)) {
1759ef4f35d8SEric Saxe 			if (PG_CMT_HW_SUSPECT(((pghw_t *)pg_next)->pghw_hw)) {
1760ef4f35d8SEric Saxe 				pg_bad = pg_next;
17610e751525SEric Saxe 			} else if (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw)) {
17620e751525SEric Saxe 				pg_bad = pg;
17636890d023SEric Saxe 			}
17646890d023SEric Saxe 		}
17650e751525SEric Saxe 		if (pg_bad) {
17660e751525SEric Saxe 			if (pg_cmt_prune(pg_bad, lineage, sz) == 0) {
17670e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_REPAIRED;
17680e751525SEric Saxe 				goto revalidate;
17690e751525SEric Saxe 			}
17700e751525SEric Saxe 		}
17710e751525SEric Saxe 		/*
1772ef4f35d8SEric Saxe 		 * Something went wrong trying to identify and/or prune out
1773ef4f35d8SEric Saxe 		 * the bad level. Disable CMT scheduling altogether.
17740e751525SEric Saxe 		 */
17750e751525SEric Saxe 		pg_cmt_disable();
1776ef4f35d8SEric Saxe 		break;
1777ef4f35d8SEric Saxe 	default:
1778ef4f35d8SEric Saxe 		/*
1779ef4f35d8SEric Saxe 		 * If we're here, we've encountered a validation error for
1780ef4f35d8SEric Saxe 		 * which we don't know how to recover. In this case, disable
1781ef4f35d8SEric Saxe 		 * CMT scheduling altogether.
1782ef4f35d8SEric Saxe 		 */
17830e751525SEric Saxe 		cmt_lineage_status = CMT_LINEAGE_UNRECOVERABLE;
1784ef4f35d8SEric Saxe 		pg_cmt_disable();
17850e751525SEric Saxe 	}
1786ef4f35d8SEric Saxe 	return (cmt_lineage_status);
17876890d023SEric Saxe }
1788