xref: /titanic_53/usr/src/uts/common/disp/cmt.c (revision f03808b6f3074fb1f0ee58a6656149dc2a21125c)
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 
103fb2f18f8Sesaxe static pg_cid_t		pg_cmt_class_id;		/* PG class id */
104fb2f18f8Sesaxe 
105fb2f18f8Sesaxe static pg_t		*pg_cmt_alloc();
106fb2f18f8Sesaxe static void		pg_cmt_free(pg_t *);
107fb2f18f8Sesaxe static void		pg_cmt_cpu_init(cpu_t *);
108fb2f18f8Sesaxe static void		pg_cmt_cpu_fini(cpu_t *);
109fb2f18f8Sesaxe static void		pg_cmt_cpu_active(cpu_t *);
110fb2f18f8Sesaxe static void		pg_cmt_cpu_inactive(cpu_t *);
111fb2f18f8Sesaxe static void		pg_cmt_cpupart_in(cpu_t *, cpupart_t *);
112fb2f18f8Sesaxe static void		pg_cmt_cpupart_move(cpu_t *, cpupart_t *, cpupart_t *);
1130e751525SEric Saxe static char		*pg_cmt_policy_name(pg_t *);
1140e751525SEric Saxe static void		pg_cmt_hier_sort(pg_cmt_t **, int);
1150e751525SEric Saxe static pg_cmt_t		*pg_cmt_hier_rank(pg_cmt_t *, pg_cmt_t *);
116fb2f18f8Sesaxe static int		pg_cmt_cpu_belongs(pg_t *, cpu_t *);
117fb2f18f8Sesaxe static int		pg_cmt_hw(pghw_type_t);
118fb2f18f8Sesaxe static cmt_lgrp_t	*pg_cmt_find_lgrp(lgrp_handle_t);
119a6604450Sesaxe static cmt_lgrp_t	*pg_cmt_lgrp_create(lgrp_handle_t);
1200e751525SEric Saxe static int		pg_cmt_lineage_validate(pg_cmt_t **, int *);
1210e751525SEric Saxe static void		cmt_ev_thread_swtch(pg_t *, cpu_t *, hrtime_t,
1220e751525SEric Saxe 			    kthread_t *, kthread_t *);
1230e751525SEric Saxe static void		cmt_ev_thread_swtch_pwr(pg_t *, cpu_t *, hrtime_t,
1240e751525SEric Saxe 			    kthread_t *, kthread_t *);
1250e751525SEric Saxe static void		cmt_ev_thread_remain_pwr(pg_t *, cpu_t *, kthread_t *);
126fb2f18f8Sesaxe 
127fb2f18f8Sesaxe /*
128fb2f18f8Sesaxe  * Macro to test if PG is managed by the CMT PG class
129fb2f18f8Sesaxe  */
130fb2f18f8Sesaxe #define	IS_CMT_PG(pg)	(((pg_t *)(pg))->pg_class->pgc_id == pg_cmt_class_id)
131fb2f18f8Sesaxe 
132fb2f18f8Sesaxe /*
1330e751525SEric Saxe  * Status codes for CMT lineage validation
1340e751525SEric Saxe  * See cmt_lineage_validate() below
1350e751525SEric Saxe  */
1360e751525SEric Saxe typedef enum cmt_lineage_validation {
1370e751525SEric Saxe 	CMT_LINEAGE_VALID,
1380e751525SEric Saxe 	CMT_LINEAGE_NON_CONCENTRIC,
1390e751525SEric Saxe 	CMT_LINEAGE_REPAIRED,
1400e751525SEric Saxe 	CMT_LINEAGE_UNRECOVERABLE
1410e751525SEric Saxe } cmt_lineage_validation_t;
1420e751525SEric Saxe 
1430e751525SEric Saxe /*
1440e751525SEric Saxe  * Status of the current lineage under construction.
1450e751525SEric Saxe  * One must be holding cpu_lock to change this.
1460e751525SEric Saxe  */
1470e751525SEric Saxe static cmt_lineage_validation_t	cmt_lineage_status = CMT_LINEAGE_VALID;
1480e751525SEric Saxe 
1490e751525SEric Saxe /*
1500e751525SEric Saxe  * Power domain definitions (on x86) are defined by ACPI, and
1510e751525SEric Saxe  * therefore may be subject to BIOS bugs.
1520e751525SEric Saxe  */
1530e751525SEric Saxe #define	PG_CMT_HW_SUSPECT(hw)	PGHW_IS_PM_DOMAIN(hw)
1540e751525SEric Saxe 
1550e751525SEric Saxe /*
156fb2f18f8Sesaxe  * CMT PG ops
157fb2f18f8Sesaxe  */
158fb2f18f8Sesaxe struct pg_ops pg_ops_cmt = {
159fb2f18f8Sesaxe 	pg_cmt_alloc,
160fb2f18f8Sesaxe 	pg_cmt_free,
161fb2f18f8Sesaxe 	pg_cmt_cpu_init,
162fb2f18f8Sesaxe 	pg_cmt_cpu_fini,
163fb2f18f8Sesaxe 	pg_cmt_cpu_active,
164fb2f18f8Sesaxe 	pg_cmt_cpu_inactive,
165fb2f18f8Sesaxe 	pg_cmt_cpupart_in,
166fb2f18f8Sesaxe 	NULL,			/* cpupart_out */
167fb2f18f8Sesaxe 	pg_cmt_cpupart_move,
168fb2f18f8Sesaxe 	pg_cmt_cpu_belongs,
1690e751525SEric Saxe 	pg_cmt_policy_name,
170fb2f18f8Sesaxe };
171fb2f18f8Sesaxe 
172fb2f18f8Sesaxe /*
173fb2f18f8Sesaxe  * Initialize the CMT PG class
174fb2f18f8Sesaxe  */
175fb2f18f8Sesaxe void
176fb2f18f8Sesaxe pg_cmt_class_init(void)
177fb2f18f8Sesaxe {
178fb2f18f8Sesaxe 	if (cmt_sched_disabled)
179fb2f18f8Sesaxe 		return;
180fb2f18f8Sesaxe 
181fb2f18f8Sesaxe 	pg_cmt_class_id = pg_class_register("cmt", &pg_ops_cmt, PGR_PHYSICAL);
182fb2f18f8Sesaxe }
183fb2f18f8Sesaxe 
184fb2f18f8Sesaxe /*
185fb2f18f8Sesaxe  * Called to indicate a new CPU has started up so
186fb2f18f8Sesaxe  * that either t0 or the slave startup thread can
187fb2f18f8Sesaxe  * be accounted for.
188fb2f18f8Sesaxe  */
189fb2f18f8Sesaxe void
190fb2f18f8Sesaxe pg_cmt_cpu_startup(cpu_t *cp)
191fb2f18f8Sesaxe {
1920e751525SEric Saxe 	pg_ev_thread_swtch(cp, gethrtime_unscaled(), cp->cpu_idle_thread,
1930e751525SEric Saxe 	    cp->cpu_thread);
194fb2f18f8Sesaxe }
195fb2f18f8Sesaxe 
196fb2f18f8Sesaxe /*
197fb2f18f8Sesaxe  * Return non-zero if thread can migrate between "from" and "to"
198fb2f18f8Sesaxe  * without a performance penalty
199fb2f18f8Sesaxe  */
200fb2f18f8Sesaxe int
201fb2f18f8Sesaxe pg_cmt_can_migrate(cpu_t *from, cpu_t *to)
202fb2f18f8Sesaxe {
203fb2f18f8Sesaxe 	if (from->cpu_physid->cpu_cacheid ==
204fb2f18f8Sesaxe 	    to->cpu_physid->cpu_cacheid)
205fb2f18f8Sesaxe 		return (1);
206fb2f18f8Sesaxe 	return (0);
207fb2f18f8Sesaxe }
208fb2f18f8Sesaxe 
209fb2f18f8Sesaxe /*
210fb2f18f8Sesaxe  * CMT class specific PG allocation
211fb2f18f8Sesaxe  */
212fb2f18f8Sesaxe static pg_t *
213fb2f18f8Sesaxe pg_cmt_alloc(void)
214fb2f18f8Sesaxe {
215fb2f18f8Sesaxe 	return (kmem_zalloc(sizeof (pg_cmt_t), KM_NOSLEEP));
216fb2f18f8Sesaxe }
217fb2f18f8Sesaxe 
218fb2f18f8Sesaxe /*
219fb2f18f8Sesaxe  * Class specific PG de-allocation
220fb2f18f8Sesaxe  */
221fb2f18f8Sesaxe static void
222fb2f18f8Sesaxe pg_cmt_free(pg_t *pg)
223fb2f18f8Sesaxe {
224fb2f18f8Sesaxe 	ASSERT(pg != NULL);
225fb2f18f8Sesaxe 	ASSERT(IS_CMT_PG(pg));
226fb2f18f8Sesaxe 
227fb2f18f8Sesaxe 	kmem_free((pg_cmt_t *)pg, sizeof (pg_cmt_t));
228fb2f18f8Sesaxe }
229fb2f18f8Sesaxe 
230fb2f18f8Sesaxe /*
2310e751525SEric Saxe  * Given a hardware sharing relationship, return which dispatcher
2320e751525SEric Saxe  * policies should be implemented to optimize performance and efficiency
233fb2f18f8Sesaxe  */
2340e751525SEric Saxe static pg_cmt_policy_t
2350e751525SEric Saxe pg_cmt_policy(pghw_type_t hw)
236fb2f18f8Sesaxe {
2370e751525SEric Saxe 	pg_cmt_policy_t p;
2380e751525SEric Saxe 
2390e751525SEric Saxe 	/*
2400e751525SEric Saxe 	 * Give the platform a chance to override the default
2410e751525SEric Saxe 	 */
2420e751525SEric Saxe 	if ((p = pg_plat_cmt_policy(hw)) != CMT_NO_POLICY)
2430e751525SEric Saxe 		return (p);
2440e751525SEric Saxe 
2450e751525SEric Saxe 	switch (hw) {
2460e751525SEric Saxe 	case PGHW_IPIPE:
2470e751525SEric Saxe 	case PGHW_FPU:
2480e751525SEric Saxe 	case PGHW_CHIP:
2490e751525SEric Saxe 		return (CMT_BALANCE);
2500e751525SEric Saxe 	case PGHW_CACHE:
2510e751525SEric Saxe 		return (CMT_AFFINITY);
2520e751525SEric Saxe 	case PGHW_POW_ACTIVE:
2530e751525SEric Saxe 	case PGHW_POW_IDLE:
2540e751525SEric Saxe 		return (CMT_BALANCE);
2550e751525SEric Saxe 	default:
2560e751525SEric Saxe 		return (CMT_NO_POLICY);
2570e751525SEric Saxe 	}
2580e751525SEric Saxe }
2590e751525SEric Saxe 
2600e751525SEric Saxe /*
2610e751525SEric Saxe  * Rank the importance of optimizing for the pg1 relationship vs.
2620e751525SEric Saxe  * the pg2 relationship.
2630e751525SEric Saxe  */
2640e751525SEric Saxe static pg_cmt_t *
2650e751525SEric Saxe pg_cmt_hier_rank(pg_cmt_t *pg1, pg_cmt_t *pg2)
2660e751525SEric Saxe {
2670e751525SEric Saxe 	pghw_type_t hw1 = ((pghw_t *)pg1)->pghw_hw;
2680e751525SEric Saxe 	pghw_type_t hw2 = ((pghw_t *)pg2)->pghw_hw;
2690e751525SEric Saxe 
2700e751525SEric Saxe 	/*
2710e751525SEric Saxe 	 * A power domain is only important if CPUPM is enabled.
2720e751525SEric Saxe 	 */
2730e751525SEric Saxe 	if (cpupm_get_policy() == CPUPM_POLICY_DISABLED) {
2740e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw1) && !PGHW_IS_PM_DOMAIN(hw2))
2750e751525SEric Saxe 			return (pg2);
2760e751525SEric Saxe 		if (PGHW_IS_PM_DOMAIN(hw2) && !PGHW_IS_PM_DOMAIN(hw1))
2770e751525SEric Saxe 			return (pg1);
2780e751525SEric Saxe 	}
2790e751525SEric Saxe 
2800e751525SEric Saxe 	/*
2810e751525SEric Saxe 	 * Otherwise, ask the platform
2820e751525SEric Saxe 	 */
2830e751525SEric Saxe 	if (pg_plat_hw_rank(hw1, hw2) == hw1)
2840e751525SEric Saxe 		return (pg1);
2850e751525SEric Saxe 	else
2860e751525SEric Saxe 		return (pg2);
2870e751525SEric Saxe }
2880e751525SEric Saxe 
2890e751525SEric Saxe /*
2900e751525SEric Saxe  * Initialize CMT callbacks for the given PG
2910e751525SEric Saxe  */
2920e751525SEric Saxe static void
2930e751525SEric Saxe cmt_callback_init(pg_t *pg)
2940e751525SEric Saxe {
2950e751525SEric Saxe 	switch (((pghw_t *)pg)->pghw_hw) {
2960e751525SEric Saxe 	case PGHW_POW_ACTIVE:
2970e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch_pwr;
2980e751525SEric Saxe 		pg->pg_cb.thread_remain = cmt_ev_thread_remain_pwr;
2990e751525SEric Saxe 		break;
3000e751525SEric Saxe 	default:
3010e751525SEric Saxe 		pg->pg_cb.thread_swtch = cmt_ev_thread_swtch;
3020e751525SEric Saxe 
3030e751525SEric Saxe 	}
3040e751525SEric Saxe }
3050e751525SEric Saxe 
3060e751525SEric Saxe /*
3070e751525SEric Saxe  * Promote PG above it's current parent.
3080e751525SEric Saxe  * This is only legal if PG has an equal or greater number of CPUs
3090e751525SEric Saxe  * than it's parent.
3100e751525SEric Saxe  */
3110e751525SEric Saxe static void
3120e751525SEric Saxe cmt_hier_promote(pg_cmt_t *pg)
3130e751525SEric Saxe {
3140e751525SEric Saxe 	pg_cmt_t	*parent;
3150e751525SEric Saxe 	group_t		*children;
3160e751525SEric Saxe 	cpu_t		*cpu;
3170e751525SEric Saxe 	group_iter_t	iter;
3180e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
3190e751525SEric Saxe 	int		r;
3200e751525SEric Saxe 	int		err;
3210e751525SEric Saxe 
3220e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
3230e751525SEric Saxe 
3240e751525SEric Saxe 	parent = pg->cmt_parent;
3250e751525SEric Saxe 	if (parent == NULL) {
3260e751525SEric Saxe 		/*
3270e751525SEric Saxe 		 * Nothing to do
3280e751525SEric Saxe 		 */
3290e751525SEric Saxe 		return;
3300e751525SEric Saxe 	}
3310e751525SEric Saxe 
3320e751525SEric Saxe 	ASSERT(PG_NUM_CPUS((pg_t *)pg) >= PG_NUM_CPUS((pg_t *)parent));
3330e751525SEric Saxe 
3340e751525SEric Saxe 	/*
3350e751525SEric Saxe 	 * We're changing around the hierarchy, which is actively traversed
3360e751525SEric Saxe 	 * by the dispatcher. Pause CPUS to ensure exclusivity.
3370e751525SEric Saxe 	 */
3380e751525SEric Saxe 	pause_cpus(NULL);
3390e751525SEric Saxe 
3400e751525SEric Saxe 	/*
3410e751525SEric Saxe 	 * If necessary, update the parent's sibling set, replacing parent
3420e751525SEric Saxe 	 * with PG.
3430e751525SEric Saxe 	 */
3440e751525SEric Saxe 	if (parent->cmt_siblings) {
3450e751525SEric Saxe 		if (group_remove(parent->cmt_siblings, parent, GRP_NORESIZE)
3460e751525SEric Saxe 		    != -1) {
3470e751525SEric Saxe 			r = group_add(parent->cmt_siblings, pg, GRP_NORESIZE);
3480e751525SEric Saxe 			ASSERT(r != -1);
3490e751525SEric Saxe 		}
3500e751525SEric Saxe 	}
3510e751525SEric Saxe 
3520e751525SEric Saxe 	/*
3530e751525SEric Saxe 	 * If the parent is at the top of the hierarchy, replace it's entry
3540e751525SEric Saxe 	 * in the root lgroup's group of top level PGs.
3550e751525SEric Saxe 	 */
3560e751525SEric Saxe 	if (parent->cmt_parent == NULL &&
3570e751525SEric Saxe 	    parent->cmt_siblings != &cmt_root->cl_pgs) {
3580e751525SEric Saxe 		if (group_remove(&cmt_root->cl_pgs, parent, GRP_NORESIZE)
3590e751525SEric Saxe 		    != -1) {
3600e751525SEric Saxe 			r = group_add(&cmt_root->cl_pgs, pg, GRP_NORESIZE);
3610e751525SEric Saxe 			ASSERT(r != -1);
3620e751525SEric Saxe 		}
3630e751525SEric Saxe 	}
3640e751525SEric Saxe 
3650e751525SEric Saxe 	/*
3660e751525SEric Saxe 	 * We assume (and therefore assert) that the PG being promoted is an
3670e751525SEric Saxe 	 * only child of it's parent. Update the parent's children set
3680e751525SEric Saxe 	 * replacing PG's entry with the parent (since the parent is becoming
3690e751525SEric Saxe 	 * the child). Then have PG and the parent swap children sets.
3700e751525SEric Saxe 	 */
3710e751525SEric Saxe 	ASSERT(GROUP_SIZE(parent->cmt_children) <= 1);
3720e751525SEric Saxe 	if (group_remove(parent->cmt_children, pg, GRP_NORESIZE) != -1) {
3730e751525SEric Saxe 		r = group_add(parent->cmt_children, parent, GRP_NORESIZE);
3740e751525SEric Saxe 		ASSERT(r != -1);
3750e751525SEric Saxe 	}
3760e751525SEric Saxe 
3770e751525SEric Saxe 	children = pg->cmt_children;
3780e751525SEric Saxe 	pg->cmt_children = parent->cmt_children;
3790e751525SEric Saxe 	parent->cmt_children = children;
3800e751525SEric Saxe 
3810e751525SEric Saxe 	/*
3820e751525SEric Saxe 	 * Update the sibling references for PG and it's parent
3830e751525SEric Saxe 	 */
3840e751525SEric Saxe 	pg->cmt_siblings = parent->cmt_siblings;
3850e751525SEric Saxe 	parent->cmt_siblings = pg->cmt_children;
3860e751525SEric Saxe 
3870e751525SEric Saxe 	/*
3880e751525SEric Saxe 	 * Update any cached lineages in the per CPU pg data.
3890e751525SEric Saxe 	 */
3900e751525SEric Saxe 	PG_CPU_ITR_INIT(pg, cpu_iter);
3910e751525SEric Saxe 	while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
3920e751525SEric Saxe 		int		idx;
3930e751525SEric Saxe 		group_t		*pgs;
3940e751525SEric Saxe 		pg_cmt_t	*cpu_pg;
3950e751525SEric Saxe 
3960e751525SEric Saxe 		/*
3970e751525SEric Saxe 		 * Iterate over the CPU's PGs updating the children
3980e751525SEric Saxe 		 * of the PG being promoted, since they have a new parent.
3990e751525SEric Saxe 		 */
4000e751525SEric Saxe 		pgs = &cpu->cpu_pg->pgs;
4010e751525SEric Saxe 		group_iter_init(&iter);
4020e751525SEric Saxe 		while ((cpu_pg = group_iterate(pgs, &iter)) != NULL) {
4030e751525SEric Saxe 			if (cpu_pg->cmt_parent == pg) {
4040e751525SEric Saxe 				cpu_pg->cmt_parent = parent;
4050e751525SEric Saxe 			}
4060e751525SEric Saxe 		}
4070e751525SEric Saxe 
4080e751525SEric Saxe 		/*
4090e751525SEric Saxe 		 * Update the CMT load balancing lineage
4100e751525SEric Saxe 		 */
4110e751525SEric Saxe 		pgs = &cpu->cpu_pg->cmt_pgs;
4120e751525SEric Saxe 		if ((idx = group_find(pgs, (void *)pg)) == -1) {
4130e751525SEric Saxe 			/*
4140e751525SEric Saxe 			 * Unless this is the CPU who's lineage is being
4150e751525SEric Saxe 			 * constructed, the PG being promoted should be
4160e751525SEric Saxe 			 * in the lineage.
4170e751525SEric Saxe 			 */
4180e751525SEric Saxe 			ASSERT(GROUP_SIZE(pgs) == 0);
4190e751525SEric Saxe 			continue;
4200e751525SEric Saxe 		}
4210e751525SEric Saxe 
4220e751525SEric Saxe 		ASSERT(GROUP_ACCESS(pgs, idx - 1) == parent);
4230e751525SEric Saxe 		ASSERT(idx > 0);
4240e751525SEric Saxe 
4250e751525SEric Saxe 		/*
4260e751525SEric Saxe 		 * Have the child and the parent swap places in the CPU's
4270e751525SEric Saxe 		 * lineage
4280e751525SEric Saxe 		 */
4290e751525SEric Saxe 		group_remove_at(pgs, idx);
4300e751525SEric Saxe 		group_remove_at(pgs, idx - 1);
4310e751525SEric Saxe 		err = group_add_at(pgs, parent, idx);
4320e751525SEric Saxe 		ASSERT(err == 0);
4330e751525SEric Saxe 		err = group_add_at(pgs, pg, idx - 1);
4340e751525SEric Saxe 		ASSERT(err == 0);
4350e751525SEric Saxe 	}
4360e751525SEric Saxe 
4370e751525SEric Saxe 	/*
4380e751525SEric Saxe 	 * Update the parent references for PG and it's parent
4390e751525SEric Saxe 	 */
4400e751525SEric Saxe 	pg->cmt_parent = parent->cmt_parent;
4410e751525SEric Saxe 	parent->cmt_parent = pg;
4420e751525SEric Saxe 
4430e751525SEric Saxe 	start_cpus();
444fb2f18f8Sesaxe }
445fb2f18f8Sesaxe 
446fb2f18f8Sesaxe /*
447fb2f18f8Sesaxe  * CMT class callback for a new CPU entering the system
448fb2f18f8Sesaxe  */
449fb2f18f8Sesaxe static void
450fb2f18f8Sesaxe pg_cmt_cpu_init(cpu_t *cp)
451fb2f18f8Sesaxe {
452fb2f18f8Sesaxe 	pg_cmt_t	*pg;
453fb2f18f8Sesaxe 	group_t		*cmt_pgs;
4540e751525SEric Saxe 	int		levels, level;
455fb2f18f8Sesaxe 	pghw_type_t	hw;
456fb2f18f8Sesaxe 	pg_t		*pg_cache = NULL;
457fb2f18f8Sesaxe 	pg_cmt_t	*cpu_cmt_hier[PGHW_NUM_COMPONENTS];
458fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
459fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
460fb2f18f8Sesaxe 
461fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
462fb2f18f8Sesaxe 
4630e751525SEric Saxe 	if (cmt_sched_disabled)
4640e751525SEric Saxe 		return;
4650e751525SEric Saxe 
466fb2f18f8Sesaxe 	/*
467fb2f18f8Sesaxe 	 * A new CPU is coming into the system.
468fb2f18f8Sesaxe 	 * Interrogate the platform to see if the CPU
4690e751525SEric Saxe 	 * has any performance or efficiency relevant
4700e751525SEric Saxe 	 * sharing relationships
471fb2f18f8Sesaxe 	 */
472fb2f18f8Sesaxe 	cmt_pgs = &cp->cpu_pg->cmt_pgs;
473fb2f18f8Sesaxe 	cp->cpu_pg->cmt_lineage = NULL;
474fb2f18f8Sesaxe 
475fb2f18f8Sesaxe 	bzero(cpu_cmt_hier, sizeof (cpu_cmt_hier));
4760e751525SEric Saxe 	levels = 0;
477fb2f18f8Sesaxe 	for (hw = PGHW_START; hw < PGHW_NUM_COMPONENTS; hw++) {
478fb2f18f8Sesaxe 
4790e751525SEric Saxe 		pg_cmt_policy_t	policy;
4800e751525SEric Saxe 
481fb2f18f8Sesaxe 		/*
4820e751525SEric Saxe 		 * We're only interested in the hw sharing relationships
4830e751525SEric Saxe 		 * for which we know how to optimize.
484fb2f18f8Sesaxe 		 */
4850e751525SEric Saxe 		policy = pg_cmt_policy(hw);
4860e751525SEric Saxe 		if (policy == CMT_NO_POLICY ||
4870e751525SEric Saxe 		    pg_plat_hw_shared(cp, hw) == 0)
488fb2f18f8Sesaxe 			continue;
489fb2f18f8Sesaxe 
490fb2f18f8Sesaxe 		/*
4910e751525SEric Saxe 		 * Continue if the hardware sharing relationship has been
4920e751525SEric Saxe 		 * blacklisted.
4930e751525SEric Saxe 		 */
4940e751525SEric Saxe 		if (cmt_hw_blacklisted[hw]) {
4950e751525SEric Saxe 			continue;
4960e751525SEric Saxe 		}
4970e751525SEric Saxe 
4980e751525SEric Saxe 		/*
499fb2f18f8Sesaxe 		 * Find (or create) the PG associated with
500fb2f18f8Sesaxe 		 * the hw sharing relationship in which cp
501fb2f18f8Sesaxe 		 * belongs.
502fb2f18f8Sesaxe 		 *
503fb2f18f8Sesaxe 		 * Determine if a suitable PG already
504fb2f18f8Sesaxe 		 * exists, or if one needs to be created.
505fb2f18f8Sesaxe 		 */
506fb2f18f8Sesaxe 		pg = (pg_cmt_t *)pghw_place_cpu(cp, hw);
507fb2f18f8Sesaxe 		if (pg == NULL) {
508fb2f18f8Sesaxe 			/*
509fb2f18f8Sesaxe 			 * Create a new one.
510fb2f18f8Sesaxe 			 * Initialize the common...
511fb2f18f8Sesaxe 			 */
512fb2f18f8Sesaxe 			pg = (pg_cmt_t *)pg_create(pg_cmt_class_id);
513fb2f18f8Sesaxe 
514fb2f18f8Sesaxe 			/* ... physical ... */
515fb2f18f8Sesaxe 			pghw_init((pghw_t *)pg, cp, hw);
516fb2f18f8Sesaxe 
517fb2f18f8Sesaxe 			/*
518fb2f18f8Sesaxe 			 * ... and CMT specific portions of the
519fb2f18f8Sesaxe 			 * structure.
520fb2f18f8Sesaxe 			 */
5210e751525SEric Saxe 			pg->cmt_policy = policy;
5220e751525SEric Saxe 
5230e751525SEric Saxe 			/* CMT event callbacks */
5240e751525SEric Saxe 			cmt_callback_init((pg_t *)pg);
5250e751525SEric Saxe 
526fb2f18f8Sesaxe 			bitset_init(&pg->cmt_cpus_actv_set);
527fb2f18f8Sesaxe 			group_create(&pg->cmt_cpus_actv);
528fb2f18f8Sesaxe 		} else {
529fb2f18f8Sesaxe 			ASSERT(IS_CMT_PG(pg));
530fb2f18f8Sesaxe 		}
531fb2f18f8Sesaxe 
532fb2f18f8Sesaxe 		/* Add the CPU to the PG */
533fb2f18f8Sesaxe 		pg_cpu_add((pg_t *)pg, cp);
534fb2f18f8Sesaxe 
535fb2f18f8Sesaxe 		/*
5366890d023SEric Saxe 		 * Ensure capacity of the active CPU group/bitset
537fb2f18f8Sesaxe 		 */
538fb2f18f8Sesaxe 		group_expand(&pg->cmt_cpus_actv,
539fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
540fb2f18f8Sesaxe 
541fb2f18f8Sesaxe 		if (cp->cpu_seqid >=
542fb2f18f8Sesaxe 		    bitset_capacity(&pg->cmt_cpus_actv_set)) {
543fb2f18f8Sesaxe 			bitset_resize(&pg->cmt_cpus_actv_set,
544fb2f18f8Sesaxe 			    cp->cpu_seqid + 1);
545fb2f18f8Sesaxe 		}
546fb2f18f8Sesaxe 
547fb2f18f8Sesaxe 		/*
5480e751525SEric Saxe 		 * Build a lineage of CMT PGs for load balancing / coalescence
549fb2f18f8Sesaxe 		 */
5500e751525SEric Saxe 		if (policy & (CMT_BALANCE | CMT_COALESCE)) {
5510e751525SEric Saxe 			cpu_cmt_hier[levels++] = pg;
552fb2f18f8Sesaxe 		}
553fb2f18f8Sesaxe 
554fb2f18f8Sesaxe 		/* Cache this for later */
555fb2f18f8Sesaxe 		if (hw == PGHW_CACHE)
556fb2f18f8Sesaxe 			pg_cache = (pg_t *)pg;
557fb2f18f8Sesaxe 	}
558fb2f18f8Sesaxe 
5590e751525SEric Saxe 	group_expand(cmt_pgs, levels);
5606890d023SEric Saxe 
5616890d023SEric Saxe 	if (cmt_root == NULL)
5626890d023SEric Saxe 		cmt_root = pg_cmt_lgrp_create(lgrp_plat_root_hand());
563fb2f18f8Sesaxe 
564fb2f18f8Sesaxe 	/*
5650e751525SEric Saxe 	 * Find the lgrp that encapsulates this CPU's CMT hierarchy
5666890d023SEric Saxe 	 */
5676890d023SEric Saxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
5686890d023SEric Saxe 	if ((lgrp = pg_cmt_find_lgrp(lgrp_handle)) == NULL)
5696890d023SEric Saxe 		lgrp = pg_cmt_lgrp_create(lgrp_handle);
5706890d023SEric Saxe 
5716890d023SEric Saxe 	/*
5720e751525SEric Saxe 	 * Ascendingly sort the PGs in the lineage by number of CPUs
5730e751525SEric Saxe 	 */
5740e751525SEric Saxe 	pg_cmt_hier_sort(cpu_cmt_hier, levels);
5750e751525SEric Saxe 
5760e751525SEric Saxe 	/*
5770e751525SEric Saxe 	 * Examine the lineage and validate it.
5780e751525SEric Saxe 	 * This routine will also try to fix the lineage along with the
5790e751525SEric Saxe 	 * rest of the PG hierarchy should it detect an issue.
5800e751525SEric Saxe 	 *
5810e751525SEric Saxe 	 * If it returns -1, an unrecoverable error has happened and we
5820e751525SEric Saxe 	 * need to return.
5830e751525SEric Saxe 	 */
5840e751525SEric Saxe 	if (pg_cmt_lineage_validate(cpu_cmt_hier, &levels) < 0)
5850e751525SEric Saxe 		return;
5860e751525SEric Saxe 
5870e751525SEric Saxe 	/*
5880e751525SEric Saxe 	 * For existing PGs in the lineage, verify that the parent is
5890e751525SEric Saxe 	 * correct, as the generation in the lineage may have changed
5900e751525SEric Saxe 	 * as a result of the sorting. Start the traversal at the top
5910e751525SEric Saxe 	 * of the lineage, moving down.
5920e751525SEric Saxe 	 */
5930e751525SEric Saxe 	for (level = levels - 1; level >= 0; ) {
5940e751525SEric Saxe 		int reorg;
5950e751525SEric Saxe 
5960e751525SEric Saxe 		reorg = 0;
5970e751525SEric Saxe 		pg = cpu_cmt_hier[level];
5980e751525SEric Saxe 
5990e751525SEric Saxe 		/*
6000e751525SEric Saxe 		 * Promote PGs at an incorrect generation into place.
6010e751525SEric Saxe 		 */
6020e751525SEric Saxe 		while (pg->cmt_parent &&
6030e751525SEric Saxe 		    pg->cmt_parent != cpu_cmt_hier[level + 1]) {
6040e751525SEric Saxe 			cmt_hier_promote(pg);
6050e751525SEric Saxe 			reorg++;
6060e751525SEric Saxe 		}
6070e751525SEric Saxe 		if (reorg > 0)
6080e751525SEric Saxe 			level = levels - 1;
6090e751525SEric Saxe 		else
6100e751525SEric Saxe 			level--;
6110e751525SEric Saxe 	}
6120e751525SEric Saxe 
6130e751525SEric Saxe 	/*
6146890d023SEric Saxe 	 * For each of the PGs in the CPU's lineage:
6150e751525SEric Saxe 	 *	- Add an entry in the CPU sorted CMT PG group
6160e751525SEric Saxe 	 *	  which is used for top down CMT load balancing
617fb2f18f8Sesaxe 	 *	- Tie the PG into the CMT hierarchy by connecting
618fb2f18f8Sesaxe 	 *	  it to it's parent and siblings.
619fb2f18f8Sesaxe 	 */
6200e751525SEric Saxe 	for (level = 0; level < levels; level++) {
621fb2f18f8Sesaxe 		uint_t		children;
622fb2f18f8Sesaxe 		int		err;
623fb2f18f8Sesaxe 
624fb2f18f8Sesaxe 		pg = cpu_cmt_hier[level];
6250e751525SEric Saxe 		err = group_add_at(cmt_pgs, pg, levels - level - 1);
626fb2f18f8Sesaxe 		ASSERT(err == 0);
627fb2f18f8Sesaxe 
628fb2f18f8Sesaxe 		if (level == 0)
629fb2f18f8Sesaxe 			cp->cpu_pg->cmt_lineage = (pg_t *)pg;
630fb2f18f8Sesaxe 
631fb2f18f8Sesaxe 		if (pg->cmt_siblings != NULL) {
632fb2f18f8Sesaxe 			/* Already initialized */
633fb2f18f8Sesaxe 			ASSERT(pg->cmt_parent == NULL ||
634fb2f18f8Sesaxe 			    pg->cmt_parent == cpu_cmt_hier[level + 1]);
635fb2f18f8Sesaxe 			ASSERT(pg->cmt_siblings == &lgrp->cl_pgs ||
636c416da2dSjb145095 			    ((pg->cmt_parent != NULL) &&
637c416da2dSjb145095 			    pg->cmt_siblings == pg->cmt_parent->cmt_children));
638fb2f18f8Sesaxe 			continue;
639fb2f18f8Sesaxe 		}
640fb2f18f8Sesaxe 
6410e751525SEric Saxe 		if ((level + 1) == levels) {
642fb2f18f8Sesaxe 			pg->cmt_parent = NULL;
6436890d023SEric Saxe 
644fb2f18f8Sesaxe 			pg->cmt_siblings = &lgrp->cl_pgs;
645fb2f18f8Sesaxe 			children = ++lgrp->cl_npgs;
6460e751525SEric Saxe 			if (cmt_root != lgrp)
6476890d023SEric Saxe 				cmt_root->cl_npgs++;
648fb2f18f8Sesaxe 		} else {
649fb2f18f8Sesaxe 			pg->cmt_parent = cpu_cmt_hier[level + 1];
650fb2f18f8Sesaxe 
651fb2f18f8Sesaxe 			/*
652fb2f18f8Sesaxe 			 * A good parent keeps track of their children.
653fb2f18f8Sesaxe 			 * The parent's children group is also the PG's
654fb2f18f8Sesaxe 			 * siblings.
655fb2f18f8Sesaxe 			 */
656fb2f18f8Sesaxe 			if (pg->cmt_parent->cmt_children == NULL) {
657fb2f18f8Sesaxe 				pg->cmt_parent->cmt_children =
658fb2f18f8Sesaxe 				    kmem_zalloc(sizeof (group_t), KM_SLEEP);
659fb2f18f8Sesaxe 				group_create(pg->cmt_parent->cmt_children);
660fb2f18f8Sesaxe 			}
661fb2f18f8Sesaxe 			pg->cmt_siblings = pg->cmt_parent->cmt_children;
662fb2f18f8Sesaxe 			children = ++pg->cmt_parent->cmt_nchildren;
663fb2f18f8Sesaxe 		}
6646890d023SEric Saxe 
665fb2f18f8Sesaxe 		group_expand(pg->cmt_siblings, children);
6666890d023SEric Saxe 		group_expand(&cmt_root->cl_pgs, cmt_root->cl_npgs);
667fb2f18f8Sesaxe 	}
668fb2f18f8Sesaxe 
669fb2f18f8Sesaxe 	/*
670fb2f18f8Sesaxe 	 * Cache the chip and core IDs in the cpu_t->cpu_physid structure
671fb2f18f8Sesaxe 	 * for fast lookups later.
672fb2f18f8Sesaxe 	 */
673fb2f18f8Sesaxe 	if (cp->cpu_physid) {
674fb2f18f8Sesaxe 		cp->cpu_physid->cpu_chipid =
675fb2f18f8Sesaxe 		    pg_plat_hw_instance_id(cp, PGHW_CHIP);
676fb2f18f8Sesaxe 		cp->cpu_physid->cpu_coreid = pg_plat_get_core_id(cp);
677fb2f18f8Sesaxe 
678fb2f18f8Sesaxe 		/*
679fb2f18f8Sesaxe 		 * If this cpu has a PG representing shared cache, then set
680fb2f18f8Sesaxe 		 * cpu_cacheid to that PG's logical id
681fb2f18f8Sesaxe 		 */
682fb2f18f8Sesaxe 		if (pg_cache)
683fb2f18f8Sesaxe 			cp->cpu_physid->cpu_cacheid = pg_cache->pg_id;
684fb2f18f8Sesaxe 	}
685fb2f18f8Sesaxe 
686fb2f18f8Sesaxe 	/* CPU0 only initialization */
687fb2f18f8Sesaxe 	if (is_cpu0) {
688fb2f18f8Sesaxe 		pg_cmt_cpu_startup(cp);
689fb2f18f8Sesaxe 		is_cpu0 = 0;
690a6604450Sesaxe 		cpu0_lgrp = lgrp;
691fb2f18f8Sesaxe 	}
692fb2f18f8Sesaxe 
693fb2f18f8Sesaxe }
694fb2f18f8Sesaxe 
695fb2f18f8Sesaxe /*
696fb2f18f8Sesaxe  * Class callback when a CPU is leaving the system (deletion)
697fb2f18f8Sesaxe  */
698fb2f18f8Sesaxe static void
699fb2f18f8Sesaxe pg_cmt_cpu_fini(cpu_t *cp)
700fb2f18f8Sesaxe {
701fb2f18f8Sesaxe 	group_iter_t	i;
702fb2f18f8Sesaxe 	pg_cmt_t	*pg;
703fb2f18f8Sesaxe 	group_t		*pgs, *cmt_pgs;
704fb2f18f8Sesaxe 	lgrp_handle_t	lgrp_handle;
705fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
706fb2f18f8Sesaxe 
7070e751525SEric Saxe 	if (cmt_sched_disabled)
7080e751525SEric Saxe 		return;
7090e751525SEric Saxe 
710fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
711fb2f18f8Sesaxe 	cmt_pgs = &cp->cpu_pg->cmt_pgs;
712fb2f18f8Sesaxe 
713fb2f18f8Sesaxe 	/*
714fb2f18f8Sesaxe 	 * Find the lgroup that encapsulates this CPU's CMT hierarchy
715fb2f18f8Sesaxe 	 */
716fb2f18f8Sesaxe 	lgrp_handle = lgrp_plat_cpu_to_hand(cp->cpu_id);
717a6604450Sesaxe 
718fb2f18f8Sesaxe 	lgrp = pg_cmt_find_lgrp(lgrp_handle);
7193e81cacfSEric Saxe 	if (ncpus == 1 && lgrp != cpu0_lgrp) {
720a6604450Sesaxe 		/*
7213e81cacfSEric Saxe 		 * One might wonder how we could be deconfiguring the
7223e81cacfSEric Saxe 		 * only CPU in the system.
723a6604450Sesaxe 		 *
7243e81cacfSEric Saxe 		 * On Starcat systems when null_proc_lpa is detected,
7253e81cacfSEric Saxe 		 * the boot CPU (which is already configured into a leaf
7263e81cacfSEric Saxe 		 * lgroup), is moved into the root lgroup. This is done by
7273e81cacfSEric Saxe 		 * deconfiguring it from both lgroups and processor
7283e81cacfSEric Saxe 		 * groups), and then later reconfiguring it back in.  This
7293e81cacfSEric Saxe 		 * call to pg_cmt_cpu_fini() is part of that deconfiguration.
7303e81cacfSEric Saxe 		 *
7313e81cacfSEric Saxe 		 * This special case is detected by noting that the platform
7323e81cacfSEric Saxe 		 * has changed the CPU's lgrp affiliation (since it now
7333e81cacfSEric Saxe 		 * belongs in the root). In this case, use the cmt_lgrp_t
7343e81cacfSEric Saxe 		 * cached for the boot CPU, since this is what needs to be
7353e81cacfSEric Saxe 		 * torn down.
736a6604450Sesaxe 		 */
737a6604450Sesaxe 		lgrp = cpu0_lgrp;
738a6604450Sesaxe 	}
739fb2f18f8Sesaxe 
7403e81cacfSEric Saxe 	ASSERT(lgrp != NULL);
7413e81cacfSEric Saxe 
742fb2f18f8Sesaxe 	/*
743fb2f18f8Sesaxe 	 * First, clean up anything load balancing specific for each of
744fb2f18f8Sesaxe 	 * the CPU's PGs that participated in CMT load balancing
745fb2f18f8Sesaxe 	 */
746fb2f18f8Sesaxe 	pg = (pg_cmt_t *)cp->cpu_pg->cmt_lineage;
747fb2f18f8Sesaxe 	while (pg != NULL) {
748fb2f18f8Sesaxe 
749fb2f18f8Sesaxe 		/*
750fb2f18f8Sesaxe 		 * Remove the PG from the CPU's load balancing lineage
751fb2f18f8Sesaxe 		 */
752fb2f18f8Sesaxe 		(void) group_remove(cmt_pgs, pg, GRP_RESIZE);
753fb2f18f8Sesaxe 
754fb2f18f8Sesaxe 		/*
755fb2f18f8Sesaxe 		 * If it's about to become empty, destroy it's children
756fb2f18f8Sesaxe 		 * group, and remove it's reference from it's siblings.
757fb2f18f8Sesaxe 		 * This is done here (rather than below) to avoid removing
758fb2f18f8Sesaxe 		 * our reference from a PG that we just eliminated.
759fb2f18f8Sesaxe 		 */
760fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 1) {
761fb2f18f8Sesaxe 			if (pg->cmt_children != NULL)
762fb2f18f8Sesaxe 				group_destroy(pg->cmt_children);
763fb2f18f8Sesaxe 			if (pg->cmt_siblings != NULL) {
764fb2f18f8Sesaxe 				if (pg->cmt_siblings == &lgrp->cl_pgs)
765fb2f18f8Sesaxe 					lgrp->cl_npgs--;
766fb2f18f8Sesaxe 				else
767fb2f18f8Sesaxe 					pg->cmt_parent->cmt_nchildren--;
768fb2f18f8Sesaxe 			}
769fb2f18f8Sesaxe 		}
770fb2f18f8Sesaxe 		pg = pg->cmt_parent;
771fb2f18f8Sesaxe 	}
772fb2f18f8Sesaxe 	ASSERT(GROUP_SIZE(cmt_pgs) == 0);
773fb2f18f8Sesaxe 
774fb2f18f8Sesaxe 	/*
775fb2f18f8Sesaxe 	 * Now that the load balancing lineage updates have happened,
776fb2f18f8Sesaxe 	 * remove the CPU from all it's PGs (destroying any that become
777fb2f18f8Sesaxe 	 * empty).
778fb2f18f8Sesaxe 	 */
779fb2f18f8Sesaxe 	group_iter_init(&i);
780fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
781fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
782fb2f18f8Sesaxe 			continue;
783fb2f18f8Sesaxe 
784fb2f18f8Sesaxe 		pg_cpu_delete((pg_t *)pg, cp);
785fb2f18f8Sesaxe 		/*
786fb2f18f8Sesaxe 		 * Deleting the CPU from the PG changes the CPU's
787fb2f18f8Sesaxe 		 * PG group over which we are actively iterating
788fb2f18f8Sesaxe 		 * Re-initialize the iteration
789fb2f18f8Sesaxe 		 */
790fb2f18f8Sesaxe 		group_iter_init(&i);
791fb2f18f8Sesaxe 
792fb2f18f8Sesaxe 		if (GROUP_SIZE(&((pg_t *)pg)->pg_cpus) == 0) {
793fb2f18f8Sesaxe 
794fb2f18f8Sesaxe 			/*
795fb2f18f8Sesaxe 			 * The PG has become zero sized, so destroy it.
796fb2f18f8Sesaxe 			 */
797fb2f18f8Sesaxe 			group_destroy(&pg->cmt_cpus_actv);
798fb2f18f8Sesaxe 			bitset_fini(&pg->cmt_cpus_actv_set);
799fb2f18f8Sesaxe 			pghw_fini((pghw_t *)pg);
800fb2f18f8Sesaxe 
801fb2f18f8Sesaxe 			pg_destroy((pg_t *)pg);
802fb2f18f8Sesaxe 		}
803fb2f18f8Sesaxe 	}
804fb2f18f8Sesaxe }
805fb2f18f8Sesaxe 
806fb2f18f8Sesaxe /*
807fb2f18f8Sesaxe  * Class callback when a CPU is entering a cpu partition
808fb2f18f8Sesaxe  */
809fb2f18f8Sesaxe static void
810fb2f18f8Sesaxe pg_cmt_cpupart_in(cpu_t *cp, cpupart_t *pp)
811fb2f18f8Sesaxe {
812fb2f18f8Sesaxe 	group_t		*pgs;
813fb2f18f8Sesaxe 	pg_t		*pg;
814fb2f18f8Sesaxe 	group_iter_t	i;
815fb2f18f8Sesaxe 
816fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
817fb2f18f8Sesaxe 
8180e751525SEric Saxe 	if (cmt_sched_disabled)
8190e751525SEric Saxe 		return;
8200e751525SEric Saxe 
821fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
822fb2f18f8Sesaxe 
823fb2f18f8Sesaxe 	/*
824fb2f18f8Sesaxe 	 * Ensure that the new partition's PG bitset
825fb2f18f8Sesaxe 	 * is large enough for all CMT PG's to which cp
826fb2f18f8Sesaxe 	 * belongs
827fb2f18f8Sesaxe 	 */
828fb2f18f8Sesaxe 	group_iter_init(&i);
829fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
830fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
831fb2f18f8Sesaxe 			continue;
832fb2f18f8Sesaxe 
833fb2f18f8Sesaxe 		if (bitset_capacity(&pp->cp_cmt_pgs) <= pg->pg_id)
834fb2f18f8Sesaxe 			bitset_resize(&pp->cp_cmt_pgs, pg->pg_id + 1);
835fb2f18f8Sesaxe 	}
836fb2f18f8Sesaxe }
837fb2f18f8Sesaxe 
838fb2f18f8Sesaxe /*
839fb2f18f8Sesaxe  * Class callback when a CPU is actually moving partitions
840fb2f18f8Sesaxe  */
841fb2f18f8Sesaxe static void
842fb2f18f8Sesaxe pg_cmt_cpupart_move(cpu_t *cp, cpupart_t *oldpp, cpupart_t *newpp)
843fb2f18f8Sesaxe {
844fb2f18f8Sesaxe 	cpu_t		*cpp;
845fb2f18f8Sesaxe 	group_t		*pgs;
846fb2f18f8Sesaxe 	pg_t		*pg;
847fb2f18f8Sesaxe 	group_iter_t	pg_iter;
848fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_iter;
849fb2f18f8Sesaxe 	boolean_t	found;
850fb2f18f8Sesaxe 
851fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
852fb2f18f8Sesaxe 
8530e751525SEric Saxe 	if (cmt_sched_disabled)
8540e751525SEric Saxe 		return;
8550e751525SEric Saxe 
856fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
857fb2f18f8Sesaxe 	group_iter_init(&pg_iter);
858fb2f18f8Sesaxe 
859fb2f18f8Sesaxe 	/*
860fb2f18f8Sesaxe 	 * Iterate over the CPUs CMT PGs
861fb2f18f8Sesaxe 	 */
862fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &pg_iter)) != NULL) {
863fb2f18f8Sesaxe 
864fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
865fb2f18f8Sesaxe 			continue;
866fb2f18f8Sesaxe 
867fb2f18f8Sesaxe 		/*
868fb2f18f8Sesaxe 		 * Add the PG to the bitset in the new partition.
869fb2f18f8Sesaxe 		 */
870fb2f18f8Sesaxe 		bitset_add(&newpp->cp_cmt_pgs, pg->pg_id);
871fb2f18f8Sesaxe 
872fb2f18f8Sesaxe 		/*
873fb2f18f8Sesaxe 		 * Remove the PG from the bitset in the old partition
874fb2f18f8Sesaxe 		 * if the last of the PG's CPUs have left.
875fb2f18f8Sesaxe 		 */
876fb2f18f8Sesaxe 		found = B_FALSE;
877fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
878fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_iter)) != NULL) {
879fb2f18f8Sesaxe 			if (cpp == cp)
880fb2f18f8Sesaxe 				continue;
881a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
882a6604450Sesaxe 			    cpp->cpu_part->cp_id == oldpp->cp_id) {
883fb2f18f8Sesaxe 				found = B_TRUE;
884fb2f18f8Sesaxe 				break;
885fb2f18f8Sesaxe 			}
886fb2f18f8Sesaxe 		}
887fb2f18f8Sesaxe 		if (!found)
888fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs, pg->pg_id);
889fb2f18f8Sesaxe 	}
890fb2f18f8Sesaxe }
891fb2f18f8Sesaxe 
892fb2f18f8Sesaxe /*
893fb2f18f8Sesaxe  * Class callback when a CPU becomes active (online)
894fb2f18f8Sesaxe  *
895fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
896fb2f18f8Sesaxe  */
897fb2f18f8Sesaxe static void
898fb2f18f8Sesaxe pg_cmt_cpu_active(cpu_t *cp)
899fb2f18f8Sesaxe {
900fb2f18f8Sesaxe 	int		err;
901fb2f18f8Sesaxe 	group_iter_t	i;
902fb2f18f8Sesaxe 	pg_cmt_t	*pg;
903fb2f18f8Sesaxe 	group_t		*pgs;
904fb2f18f8Sesaxe 
905fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
906fb2f18f8Sesaxe 
9070e751525SEric Saxe 	if (cmt_sched_disabled)
9080e751525SEric Saxe 		return;
9090e751525SEric Saxe 
910fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
911fb2f18f8Sesaxe 	group_iter_init(&i);
912fb2f18f8Sesaxe 
913fb2f18f8Sesaxe 	/*
914fb2f18f8Sesaxe 	 * Iterate over the CPU's PGs
915fb2f18f8Sesaxe 	 */
916fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
917fb2f18f8Sesaxe 
918fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
919fb2f18f8Sesaxe 			continue;
920fb2f18f8Sesaxe 
921fb2f18f8Sesaxe 		err = group_add(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
922fb2f18f8Sesaxe 		ASSERT(err == 0);
923fb2f18f8Sesaxe 
924fb2f18f8Sesaxe 		/*
925fb2f18f8Sesaxe 		 * If this is the first active CPU in the PG, and it
926fb2f18f8Sesaxe 		 * represents a hardware sharing relationship over which
927fb2f18f8Sesaxe 		 * CMT load balancing is performed, add it as a candidate
928fb2f18f8Sesaxe 		 * for balancing with it's siblings.
929fb2f18f8Sesaxe 		 */
930fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 1 &&
9310e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
932fb2f18f8Sesaxe 			err = group_add(pg->cmt_siblings, pg, GRP_NORESIZE);
933fb2f18f8Sesaxe 			ASSERT(err == 0);
9346890d023SEric Saxe 
9356890d023SEric Saxe 			/*
9366890d023SEric Saxe 			 * If this is a top level PG, add it as a balancing
9370e751525SEric Saxe 			 * candidate when balancing within the root lgroup.
9386890d023SEric Saxe 			 */
9390e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
9400e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
9416890d023SEric Saxe 				err = group_add(&cmt_root->cl_pgs, pg,
9426890d023SEric Saxe 				    GRP_NORESIZE);
9436890d023SEric Saxe 				ASSERT(err == 0);
9446890d023SEric Saxe 			}
945fb2f18f8Sesaxe 		}
946fb2f18f8Sesaxe 
947fb2f18f8Sesaxe 		/*
948fb2f18f8Sesaxe 		 * Notate the CPU in the PGs active CPU bitset.
949fb2f18f8Sesaxe 		 * Also notate the PG as being active in it's associated
950fb2f18f8Sesaxe 		 * partition
951fb2f18f8Sesaxe 		 */
952fb2f18f8Sesaxe 		bitset_add(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
953fb2f18f8Sesaxe 		bitset_add(&cp->cpu_part->cp_cmt_pgs, ((pg_t *)pg)->pg_id);
954fb2f18f8Sesaxe 	}
955fb2f18f8Sesaxe }
956fb2f18f8Sesaxe 
957fb2f18f8Sesaxe /*
958fb2f18f8Sesaxe  * Class callback when a CPU goes inactive (offline)
959fb2f18f8Sesaxe  *
960fb2f18f8Sesaxe  * This is called in a context where CPUs are paused
961fb2f18f8Sesaxe  */
962fb2f18f8Sesaxe static void
963fb2f18f8Sesaxe pg_cmt_cpu_inactive(cpu_t *cp)
964fb2f18f8Sesaxe {
965fb2f18f8Sesaxe 	int		err;
966fb2f18f8Sesaxe 	group_t		*pgs;
967fb2f18f8Sesaxe 	pg_cmt_t	*pg;
968fb2f18f8Sesaxe 	cpu_t		*cpp;
969fb2f18f8Sesaxe 	group_iter_t	i;
970fb2f18f8Sesaxe 	pg_cpu_itr_t	cpu_itr;
971fb2f18f8Sesaxe 	boolean_t	found;
972fb2f18f8Sesaxe 
973fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
974fb2f18f8Sesaxe 
9750e751525SEric Saxe 	if (cmt_sched_disabled)
9760e751525SEric Saxe 		return;
9770e751525SEric Saxe 
978fb2f18f8Sesaxe 	pgs = &cp->cpu_pg->pgs;
979fb2f18f8Sesaxe 	group_iter_init(&i);
980fb2f18f8Sesaxe 
981fb2f18f8Sesaxe 	while ((pg = group_iterate(pgs, &i)) != NULL) {
982fb2f18f8Sesaxe 
983fb2f18f8Sesaxe 		if (IS_CMT_PG(pg) == 0)
984fb2f18f8Sesaxe 			continue;
985fb2f18f8Sesaxe 
986fb2f18f8Sesaxe 		/*
987fb2f18f8Sesaxe 		 * Remove the CPU from the CMT PGs active CPU group
988fb2f18f8Sesaxe 		 * bitmap
989fb2f18f8Sesaxe 		 */
990fb2f18f8Sesaxe 		err = group_remove(&pg->cmt_cpus_actv, cp, GRP_NORESIZE);
991fb2f18f8Sesaxe 		ASSERT(err == 0);
992fb2f18f8Sesaxe 
993fb2f18f8Sesaxe 		bitset_del(&pg->cmt_cpus_actv_set, cp->cpu_seqid);
994fb2f18f8Sesaxe 
995fb2f18f8Sesaxe 		/*
996fb2f18f8Sesaxe 		 * If there are no more active CPUs in this PG over which
997fb2f18f8Sesaxe 		 * load was balanced, remove it as a balancing candidate.
998fb2f18f8Sesaxe 		 */
999fb2f18f8Sesaxe 		if (GROUP_SIZE(&pg->cmt_cpus_actv) == 0 &&
10000e751525SEric Saxe 		    (pg->cmt_policy & (CMT_BALANCE | CMT_COALESCE))) {
1001fb2f18f8Sesaxe 			err = group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
1002fb2f18f8Sesaxe 			ASSERT(err == 0);
10036890d023SEric Saxe 
10040e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
10050e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
10066890d023SEric Saxe 				err = group_remove(&cmt_root->cl_pgs, pg,
10076890d023SEric Saxe 				    GRP_NORESIZE);
10086890d023SEric Saxe 				ASSERT(err == 0);
10096890d023SEric Saxe 			}
1010fb2f18f8Sesaxe 		}
1011fb2f18f8Sesaxe 
1012fb2f18f8Sesaxe 		/*
1013fb2f18f8Sesaxe 		 * Assert the number of active CPUs does not exceed
1014fb2f18f8Sesaxe 		 * the total number of CPUs in the PG
1015fb2f18f8Sesaxe 		 */
1016fb2f18f8Sesaxe 		ASSERT(GROUP_SIZE(&pg->cmt_cpus_actv) <=
1017fb2f18f8Sesaxe 		    GROUP_SIZE(&((pg_t *)pg)->pg_cpus));
1018fb2f18f8Sesaxe 
1019fb2f18f8Sesaxe 		/*
1020fb2f18f8Sesaxe 		 * Update the PG bitset in the CPU's old partition
1021fb2f18f8Sesaxe 		 */
1022fb2f18f8Sesaxe 		found = B_FALSE;
1023fb2f18f8Sesaxe 		PG_CPU_ITR_INIT(pg, cpu_itr);
1024fb2f18f8Sesaxe 		while ((cpp = pg_cpu_next(&cpu_itr)) != NULL) {
1025fb2f18f8Sesaxe 			if (cpp == cp)
1026fb2f18f8Sesaxe 				continue;
1027a6604450Sesaxe 			if (CPU_ACTIVE(cpp) &&
1028a6604450Sesaxe 			    cpp->cpu_part->cp_id == cp->cpu_part->cp_id) {
1029fb2f18f8Sesaxe 				found = B_TRUE;
1030fb2f18f8Sesaxe 				break;
1031fb2f18f8Sesaxe 			}
1032fb2f18f8Sesaxe 		}
1033fb2f18f8Sesaxe 		if (!found) {
1034fb2f18f8Sesaxe 			bitset_del(&cp->cpu_part->cp_cmt_pgs,
1035fb2f18f8Sesaxe 			    ((pg_t *)pg)->pg_id);
1036fb2f18f8Sesaxe 		}
1037fb2f18f8Sesaxe 	}
1038fb2f18f8Sesaxe }
1039fb2f18f8Sesaxe 
1040fb2f18f8Sesaxe /*
1041fb2f18f8Sesaxe  * Return non-zero if the CPU belongs in the given PG
1042fb2f18f8Sesaxe  */
1043fb2f18f8Sesaxe static int
1044fb2f18f8Sesaxe pg_cmt_cpu_belongs(pg_t *pg, cpu_t *cp)
1045fb2f18f8Sesaxe {
1046fb2f18f8Sesaxe 	cpu_t	*pg_cpu;
1047fb2f18f8Sesaxe 
1048fb2f18f8Sesaxe 	pg_cpu = GROUP_ACCESS(&pg->pg_cpus, 0);
1049fb2f18f8Sesaxe 
1050fb2f18f8Sesaxe 	ASSERT(pg_cpu != NULL);
1051fb2f18f8Sesaxe 
1052fb2f18f8Sesaxe 	/*
1053fb2f18f8Sesaxe 	 * The CPU belongs if, given the nature of the hardware sharing
1054fb2f18f8Sesaxe 	 * relationship represented by the PG, the CPU has that
1055fb2f18f8Sesaxe 	 * relationship with some other CPU already in the PG
1056fb2f18f8Sesaxe 	 */
1057fb2f18f8Sesaxe 	if (pg_plat_cpus_share(cp, pg_cpu, ((pghw_t *)pg)->pghw_hw))
1058fb2f18f8Sesaxe 		return (1);
1059fb2f18f8Sesaxe 
1060fb2f18f8Sesaxe 	return (0);
1061fb2f18f8Sesaxe }
1062fb2f18f8Sesaxe 
1063fb2f18f8Sesaxe /*
10640e751525SEric Saxe  * Sort the CPUs CMT hierarchy, where "size" is the number of levels.
1065fb2f18f8Sesaxe  */
1066fb2f18f8Sesaxe static void
10670e751525SEric Saxe pg_cmt_hier_sort(pg_cmt_t **hier, int size)
1068fb2f18f8Sesaxe {
10690e751525SEric Saxe 	int		i, j, inc;
10700e751525SEric Saxe 	pg_t		*tmp;
10710e751525SEric Saxe 	pg_t		**h = (pg_t **)hier;
1072fb2f18f8Sesaxe 
10730e751525SEric Saxe 	/*
10740e751525SEric Saxe 	 * First sort by number of CPUs
10750e751525SEric Saxe 	 */
10760e751525SEric Saxe 	inc = size / 2;
10770e751525SEric Saxe 	while (inc > 0) {
10780e751525SEric Saxe 		for (i = inc; i < size; i++) {
10790e751525SEric Saxe 			j = i;
10800e751525SEric Saxe 			tmp = h[i];
10810e751525SEric Saxe 			while ((j >= inc) &&
10820e751525SEric Saxe 			    (PG_NUM_CPUS(h[j - inc]) > PG_NUM_CPUS(tmp))) {
10830e751525SEric Saxe 				h[j] = h[j - inc];
10840e751525SEric Saxe 				j = j - inc;
10850e751525SEric Saxe 			}
10860e751525SEric Saxe 			h[j] = tmp;
10870e751525SEric Saxe 		}
10880e751525SEric Saxe 		if (inc == 2)
10890e751525SEric Saxe 			inc = 1;
10900e751525SEric Saxe 		else
10910e751525SEric Saxe 			inc = (inc * 5) / 11;
10920e751525SEric Saxe 	}
1093fb2f18f8Sesaxe 
10940e751525SEric Saxe 	/*
10950e751525SEric Saxe 	 * Break ties by asking the platform.
10960e751525SEric Saxe 	 * Determine if h[i] outranks h[i + 1] and if so, swap them.
10970e751525SEric Saxe 	 */
10980e751525SEric Saxe 	for (i = 0; i < size - 1; i++) {
10990e751525SEric Saxe 		if ((PG_NUM_CPUS(h[i]) == PG_NUM_CPUS(h[i + 1])) &&
11000e751525SEric Saxe 		    pg_cmt_hier_rank(hier[i], hier[i + 1]) == hier[i]) {
11010e751525SEric Saxe 			tmp = h[i];
11020e751525SEric Saxe 			h[i] = h[i + 1];
11030e751525SEric Saxe 			h[i + 1] = tmp;
1104fb2f18f8Sesaxe 		}
1105fb2f18f8Sesaxe 	}
1106fb2f18f8Sesaxe }
1107fb2f18f8Sesaxe 
1108fb2f18f8Sesaxe /*
1109fb2f18f8Sesaxe  * Return a cmt_lgrp_t * given an lgroup handle.
1110fb2f18f8Sesaxe  */
1111fb2f18f8Sesaxe static cmt_lgrp_t *
1112fb2f18f8Sesaxe pg_cmt_find_lgrp(lgrp_handle_t hand)
1113fb2f18f8Sesaxe {
1114fb2f18f8Sesaxe 	cmt_lgrp_t	*lgrp;
1115fb2f18f8Sesaxe 
1116fb2f18f8Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1117fb2f18f8Sesaxe 
1118fb2f18f8Sesaxe 	lgrp = cmt_lgrps;
1119fb2f18f8Sesaxe 	while (lgrp != NULL) {
1120fb2f18f8Sesaxe 		if (lgrp->cl_hand == hand)
1121a6604450Sesaxe 			break;
1122fb2f18f8Sesaxe 		lgrp = lgrp->cl_next;
1123fb2f18f8Sesaxe 	}
1124a6604450Sesaxe 	return (lgrp);
1125a6604450Sesaxe }
1126fb2f18f8Sesaxe 
1127fb2f18f8Sesaxe /*
1128a6604450Sesaxe  * Create a cmt_lgrp_t with the specified handle.
1129fb2f18f8Sesaxe  */
1130a6604450Sesaxe static cmt_lgrp_t *
1131a6604450Sesaxe pg_cmt_lgrp_create(lgrp_handle_t hand)
1132a6604450Sesaxe {
1133a6604450Sesaxe 	cmt_lgrp_t	*lgrp;
1134a6604450Sesaxe 
1135a6604450Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
1136a6604450Sesaxe 
1137fb2f18f8Sesaxe 	lgrp = kmem_zalloc(sizeof (cmt_lgrp_t), KM_SLEEP);
1138fb2f18f8Sesaxe 
1139fb2f18f8Sesaxe 	lgrp->cl_hand = hand;
1140fb2f18f8Sesaxe 	lgrp->cl_npgs = 0;
1141fb2f18f8Sesaxe 	lgrp->cl_next = cmt_lgrps;
1142fb2f18f8Sesaxe 	cmt_lgrps = lgrp;
1143fb2f18f8Sesaxe 	group_create(&lgrp->cl_pgs);
1144fb2f18f8Sesaxe 
1145fb2f18f8Sesaxe 	return (lgrp);
1146fb2f18f8Sesaxe }
11476890d023SEric Saxe 
11486890d023SEric Saxe /*
11490e751525SEric Saxe  * Interfaces to enable and disable power aware dispatching
11500e751525SEric Saxe  * The caller must be holding cpu_lock.
11516890d023SEric Saxe  *
11520e751525SEric Saxe  * Return 0 on success and -1 on failure.
11536890d023SEric Saxe  */
11540e751525SEric Saxe int
11550e751525SEric Saxe cmt_pad_enable(pghw_type_t type)
11566890d023SEric Saxe {
11570e751525SEric Saxe 	group_t		*hwset;
11580e751525SEric Saxe 	group_iter_t	iter;
11590e751525SEric Saxe 	pg_cmt_t	*pg;
11606890d023SEric Saxe 
11610e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
11620e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
11636890d023SEric Saxe 
11640e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL ||
11650e751525SEric Saxe 	    cmt_hw_blacklisted[type]) {
11660e751525SEric Saxe 		/*
11670e751525SEric Saxe 		 * Unable to find any instances of the specified type
11680e751525SEric Saxe 		 * of power domain, or the power domains have been blacklisted.
11690e751525SEric Saxe 		 */
11700e751525SEric Saxe 		return (-1);
11710e751525SEric Saxe 	}
11726890d023SEric Saxe 
11736890d023SEric Saxe 	/*
11740e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
11750e751525SEric Saxe 	 * policy for power/performance optimization.
11760e751525SEric Saxe 	 *
11770e751525SEric Saxe 	 * Simply setting the policy isn't enough in the case where the power
11780e751525SEric Saxe 	 * domain is an only child of another PG. Because the dispatcher walks
11790e751525SEric Saxe 	 * the PG hierarchy in a top down fashion, the higher up PG's policy
11800e751525SEric Saxe 	 * will dominate. So promote the power domain above it's parent if both
11810e751525SEric Saxe 	 * PG and it's parent have the same CPUs to ensure it's policy
11820e751525SEric Saxe 	 * dominates.
11836890d023SEric Saxe 	 */
11840e751525SEric Saxe 	group_iter_init(&iter);
11850e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
11860e751525SEric Saxe 		/*
11870e751525SEric Saxe 		 * If the power domain is an only child to a parent
11880e751525SEric Saxe 		 * not implementing the same policy, promote the child
11890e751525SEric Saxe 		 * above the parent to activate the policy.
11900e751525SEric Saxe 		 */
11910e751525SEric Saxe 		pg->cmt_policy = pg_cmt_policy(((pghw_t *)pg)->pghw_hw);
11920e751525SEric Saxe 		while ((pg->cmt_parent != NULL) &&
11930e751525SEric Saxe 		    (pg->cmt_parent->cmt_policy != pg->cmt_policy) &&
11940e751525SEric Saxe 		    (PG_NUM_CPUS((pg_t *)pg) ==
11950e751525SEric Saxe 		    PG_NUM_CPUS((pg_t *)pg->cmt_parent))) {
11960e751525SEric Saxe 			cmt_hier_promote(pg);
11970e751525SEric Saxe 		}
11980e751525SEric Saxe 	}
11990e751525SEric Saxe 
12000e751525SEric Saxe 	return (0);
12010e751525SEric Saxe }
12020e751525SEric Saxe 
12030e751525SEric Saxe int
12040e751525SEric Saxe cmt_pad_disable(pghw_type_t type)
12050e751525SEric Saxe {
12060e751525SEric Saxe 	group_t		*hwset;
12070e751525SEric Saxe 	group_iter_t	iter;
12080e751525SEric Saxe 	pg_cmt_t	*pg;
12090e751525SEric Saxe 	pg_cmt_t	*child;
12100e751525SEric Saxe 
12110e751525SEric Saxe 	ASSERT(PGHW_IS_PM_DOMAIN(type));
12120e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
12130e751525SEric Saxe 
12140e751525SEric Saxe 	if ((hwset = pghw_set_lookup(type)) == NULL) {
12150e751525SEric Saxe 		/*
12160e751525SEric Saxe 		 * Unable to find any instances of the specified type of
12170e751525SEric Saxe 		 * power domain.
12180e751525SEric Saxe 		 */
12190e751525SEric Saxe 		return (-1);
12200e751525SEric Saxe 	}
12210e751525SEric Saxe 	/*
12220e751525SEric Saxe 	 * Iterate over the power domains, setting the default dispatcher
12230e751525SEric Saxe 	 * policy for performance optimization (load balancing).
12240e751525SEric Saxe 	 */
12250e751525SEric Saxe 	group_iter_init(&iter);
12260e751525SEric Saxe 	while ((pg = group_iterate(hwset, &iter)) != NULL) {
12270e751525SEric Saxe 
12280e751525SEric Saxe 		/*
12290e751525SEric Saxe 		 * If the power domain has an only child that implements
12300e751525SEric Saxe 		 * policy other than load balancing, promote the child
12310e751525SEric Saxe 		 * above the power domain to ensure it's policy dominates.
12320e751525SEric Saxe 		 */
1233*f03808b6SEric Saxe 		if (pg->cmt_children != NULL &&
1234*f03808b6SEric Saxe 		    GROUP_SIZE(pg->cmt_children) == 1) {
12350e751525SEric Saxe 			child = GROUP_ACCESS(pg->cmt_children, 0);
12360e751525SEric Saxe 			if ((child->cmt_policy & CMT_BALANCE) == 0) {
12370e751525SEric Saxe 				cmt_hier_promote(child);
12380e751525SEric Saxe 			}
12390e751525SEric Saxe 		}
12400e751525SEric Saxe 		pg->cmt_policy = CMT_BALANCE;
12410e751525SEric Saxe 	}
12420e751525SEric Saxe 	return (0);
12430e751525SEric Saxe }
12440e751525SEric Saxe 
12450e751525SEric Saxe /* ARGSUSED */
12460e751525SEric Saxe static void
12470e751525SEric Saxe cmt_ev_thread_swtch(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
12480e751525SEric Saxe 		    kthread_t *new)
12490e751525SEric Saxe {
12500e751525SEric Saxe 	pg_cmt_t	*cmt_pg = (pg_cmt_t *)pg;
12510e751525SEric Saxe 
12520e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
12530e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, 1);
12540e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
12550e751525SEric Saxe 		atomic_add_32(&cmt_pg->cmt_utilization, -1);
12560e751525SEric Saxe 	}
12570e751525SEric Saxe }
12580e751525SEric Saxe 
12590e751525SEric Saxe /*
12600e751525SEric Saxe  * Macro to test whether a thread is currently runnable on a CPU in a PG.
12610e751525SEric Saxe  */
12620e751525SEric Saxe #define	THREAD_RUNNABLE_IN_PG(t, pg)					\
12630e751525SEric Saxe 	((t)->t_state == TS_RUN &&					\
12640e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu &&				\
12650e751525SEric Saxe 	    bitset_in_set(&(pg)->cmt_cpus_actv_set,			\
12660e751525SEric Saxe 	    (t)->t_disp_queue->disp_cpu->cpu_seqid))
12670e751525SEric Saxe 
12680e751525SEric Saxe static void
12690e751525SEric Saxe cmt_ev_thread_swtch_pwr(pg_t *pg, cpu_t *cp, hrtime_t now, kthread_t *old,
12700e751525SEric Saxe     kthread_t *new)
12710e751525SEric Saxe {
12720e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
12730e751525SEric Saxe 	cpupm_domain_t	*dom;
12740e751525SEric Saxe 	uint32_t	u;
12750e751525SEric Saxe 
12760e751525SEric Saxe 	if (old == cp->cpu_idle_thread) {
12770e751525SEric Saxe 		ASSERT(new != cp->cpu_idle_thread);
12780e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, 1);
12790e751525SEric Saxe 		if (u == 1) {
12800e751525SEric Saxe 			/*
12810e751525SEric Saxe 			 * Notify the CPU power manager that the domain
12820e751525SEric Saxe 			 * is non-idle.
12830e751525SEric Saxe 			 */
12840e751525SEric Saxe 			dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
12850e751525SEric Saxe 			cpupm_utilization_event(cp, now, dom,
12860e751525SEric Saxe 			    CPUPM_DOM_BUSY_FROM_IDLE);
12870e751525SEric Saxe 		}
12880e751525SEric Saxe 	} else if (new == cp->cpu_idle_thread) {
12890e751525SEric Saxe 		ASSERT(old != cp->cpu_idle_thread);
12900e751525SEric Saxe 		u = atomic_add_32_nv(&cmt->cmt_utilization, -1);
12910e751525SEric Saxe 		if (u == 0) {
12920e751525SEric Saxe 			/*
12930e751525SEric Saxe 			 * The domain is idle, notify the CPU power
12940e751525SEric Saxe 			 * manager.
12950e751525SEric Saxe 			 *
12960e751525SEric Saxe 			 * Avoid notifying if the thread is simply migrating
12970e751525SEric Saxe 			 * between CPUs in the domain.
12980e751525SEric Saxe 			 */
12990e751525SEric Saxe 			if (!THREAD_RUNNABLE_IN_PG(old, cmt)) {
13000e751525SEric Saxe 				dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13010e751525SEric Saxe 				cpupm_utilization_event(cp, now, dom,
13020e751525SEric Saxe 				    CPUPM_DOM_IDLE_FROM_BUSY);
13030e751525SEric Saxe 			}
13040e751525SEric Saxe 		}
13050e751525SEric Saxe 	}
13060e751525SEric Saxe }
13070e751525SEric Saxe 
13080e751525SEric Saxe /* ARGSUSED */
13090e751525SEric Saxe static void
13100e751525SEric Saxe cmt_ev_thread_remain_pwr(pg_t *pg, cpu_t *cp, kthread_t *t)
13110e751525SEric Saxe {
13120e751525SEric Saxe 	pg_cmt_t	*cmt = (pg_cmt_t *)pg;
13130e751525SEric Saxe 	cpupm_domain_t	*dom;
13140e751525SEric Saxe 
13150e751525SEric Saxe 	dom = (cpupm_domain_t *)cmt->cmt_pg.pghw_handle;
13160e751525SEric Saxe 	cpupm_utilization_event(cp, (hrtime_t)0, dom, CPUPM_DOM_REMAIN_BUSY);
13170e751525SEric Saxe }
13180e751525SEric Saxe 
13190e751525SEric Saxe /*
13200e751525SEric Saxe  * Return the name of the CMT scheduling policy
13210e751525SEric Saxe  * being implemented across this PG
13220e751525SEric Saxe  */
13230e751525SEric Saxe static char *
13240e751525SEric Saxe pg_cmt_policy_name(pg_t *pg)
13250e751525SEric Saxe {
13260e751525SEric Saxe 	pg_cmt_policy_t policy;
13270e751525SEric Saxe 
13280e751525SEric Saxe 	policy = ((pg_cmt_t *)pg)->cmt_policy;
13290e751525SEric Saxe 
13300e751525SEric Saxe 	if (policy & CMT_AFFINITY) {
13310e751525SEric Saxe 		if (policy & CMT_BALANCE)
13320e751525SEric Saxe 			return ("Load Balancing & Affinity");
13330e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13340e751525SEric Saxe 			return ("Load Coalescence & Affinity");
13356890d023SEric Saxe 		else
13360e751525SEric Saxe 			return ("Affinity");
13370e751525SEric Saxe 	} else {
13380e751525SEric Saxe 		if (policy & CMT_BALANCE)
13390e751525SEric Saxe 			return ("Load Balancing");
13400e751525SEric Saxe 		else if (policy & CMT_COALESCE)
13410e751525SEric Saxe 			return ("Load Coalescence");
13420e751525SEric Saxe 		else
13430e751525SEric Saxe 			return ("None");
13440e751525SEric Saxe 	}
13450e751525SEric Saxe }
13466890d023SEric Saxe 
13476890d023SEric Saxe /*
13480e751525SEric Saxe  * Prune PG, and all other instances of PG's hardware sharing relationship
13490e751525SEric Saxe  * from the PG hierarchy.
13506890d023SEric Saxe  */
13510e751525SEric Saxe static int
13520e751525SEric Saxe pg_cmt_prune(pg_cmt_t *pg_bad, pg_cmt_t **lineage, int *sz)
13530e751525SEric Saxe {
13540e751525SEric Saxe 	group_t		*hwset, *children;
13550e751525SEric Saxe 	int		i, j, r, size = *sz;
13560e751525SEric Saxe 	group_iter_t	hw_iter, child_iter;
13570e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
13580e751525SEric Saxe 	pg_cmt_t	*pg, *child;
13590e751525SEric Saxe 	cpu_t		*cpu;
13600e751525SEric Saxe 	int		cap_needed;
13610e751525SEric Saxe 	pghw_type_t	hw;
13626890d023SEric Saxe 
13630e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
13646890d023SEric Saxe 
13650e751525SEric Saxe 	hw = ((pghw_t *)pg_bad)->pghw_hw;
13660e751525SEric Saxe 
13670e751525SEric Saxe 	if (hw == PGHW_POW_ACTIVE) {
13680e751525SEric Saxe 		cmn_err(CE_NOTE, "!Active CPUPM domain groups look suspect. "
13690e751525SEric Saxe 		    "Event Based CPUPM Unavailable");
13700e751525SEric Saxe 	} else if (hw == PGHW_POW_IDLE) {
13710e751525SEric Saxe 		cmn_err(CE_NOTE, "!Idle CPUPM domain groups look suspect. "
13720e751525SEric Saxe 		    "Dispatcher assisted CPUPM disabled.");
13730e751525SEric Saxe 	}
13746890d023SEric Saxe 
13756890d023SEric Saxe 	/*
13760e751525SEric Saxe 	 * Find and eliminate the PG from the lineage.
13776890d023SEric Saxe 	 */
13780e751525SEric Saxe 	for (i = 0; i < size; i++) {
13790e751525SEric Saxe 		if (lineage[i] == pg_bad) {
13800e751525SEric Saxe 			for (j = i; j < size - 1; j++)
13810e751525SEric Saxe 				lineage[j] = lineage[j + 1];
13820e751525SEric Saxe 			*sz = size - 1;
13830e751525SEric Saxe 			break;
13840e751525SEric Saxe 		}
13850e751525SEric Saxe 	}
13860e751525SEric Saxe 
13870e751525SEric Saxe 	/*
13880e751525SEric Saxe 	 * We'll prune all instances of the hardware sharing relationship
13890e751525SEric Saxe 	 * represented by pg. But before we do that (and pause CPUs) we need
13900e751525SEric Saxe 	 * to ensure the hierarchy's groups are properly sized.
13910e751525SEric Saxe 	 */
13920e751525SEric Saxe 	hwset = pghw_set_lookup(hw);
13930e751525SEric Saxe 
13940e751525SEric Saxe 	/*
13950e751525SEric Saxe 	 * Blacklist the hardware so that future groups won't be created.
13960e751525SEric Saxe 	 */
13970e751525SEric Saxe 	cmt_hw_blacklisted[hw] = 1;
13980e751525SEric Saxe 
13990e751525SEric Saxe 	/*
14000e751525SEric Saxe 	 * For each of the PGs being pruned, ensure sufficient capacity in
14010e751525SEric Saxe 	 * the siblings set for the PG's children
14020e751525SEric Saxe 	 */
14030e751525SEric Saxe 	group_iter_init(&hw_iter);
14040e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14050e751525SEric Saxe 		/*
14060e751525SEric Saxe 		 * PG is being pruned, but if it is bringing up more than
14070e751525SEric Saxe 		 * one child, ask for more capacity in the siblings group.
14080e751525SEric Saxe 		 */
14090e751525SEric Saxe 		cap_needed = 0;
14100e751525SEric Saxe 		if (pg->cmt_children &&
14110e751525SEric Saxe 		    GROUP_SIZE(pg->cmt_children) > 1) {
14120e751525SEric Saxe 			cap_needed = GROUP_SIZE(pg->cmt_children) - 1;
14130e751525SEric Saxe 
14140e751525SEric Saxe 			group_expand(pg->cmt_siblings,
14150e751525SEric Saxe 			    GROUP_SIZE(pg->cmt_siblings) + cap_needed);
14160e751525SEric Saxe 
14170e751525SEric Saxe 			/*
14180e751525SEric Saxe 			 * If this is a top level group, also ensure the
14190e751525SEric Saxe 			 * capacity in the root lgrp level CMT grouping.
14200e751525SEric Saxe 			 */
14210e751525SEric Saxe 			if (pg->cmt_parent == NULL &&
14220e751525SEric Saxe 			    pg->cmt_siblings != &cmt_root->cl_pgs) {
14230e751525SEric Saxe 				group_expand(&cmt_root->cl_pgs,
14240e751525SEric Saxe 				    GROUP_SIZE(&cmt_root->cl_pgs) + cap_needed);
14250e751525SEric Saxe 			}
14260e751525SEric Saxe 		}
14270e751525SEric Saxe 	}
14280e751525SEric Saxe 
14290e751525SEric Saxe 	/*
14300e751525SEric Saxe 	 * We're operating on the PG hierarchy. Pause CPUs to ensure
14310e751525SEric Saxe 	 * exclusivity with respect to the dispatcher.
14320e751525SEric Saxe 	 */
14330e751525SEric Saxe 	pause_cpus(NULL);
14340e751525SEric Saxe 
14350e751525SEric Saxe 	/*
14360e751525SEric Saxe 	 * Prune all PG instances of the hardware sharing relationship
14370e751525SEric Saxe 	 * represented by pg.
14380e751525SEric Saxe 	 */
14390e751525SEric Saxe 	group_iter_init(&hw_iter);
14400e751525SEric Saxe 	while ((pg = group_iterate(hwset, &hw_iter)) != NULL) {
14410e751525SEric Saxe 
14420e751525SEric Saxe 		/*
14430e751525SEric Saxe 		 * Remove PG from it's group of siblings, if it's there.
14440e751525SEric Saxe 		 */
14450e751525SEric Saxe 		if (pg->cmt_siblings) {
14460e751525SEric Saxe 			(void) group_remove(pg->cmt_siblings, pg, GRP_NORESIZE);
14470e751525SEric Saxe 		}
14480e751525SEric Saxe 		if (pg->cmt_parent == NULL &&
14490e751525SEric Saxe 		    pg->cmt_siblings != &cmt_root->cl_pgs) {
14500e751525SEric Saxe 			(void) group_remove(&cmt_root->cl_pgs, pg,
14510e751525SEric Saxe 			    GRP_NORESIZE);
14520e751525SEric Saxe 		}
14530e751525SEric Saxe 		/*
14540e751525SEric Saxe 		 * Add PGs children to it's group of siblings.
14550e751525SEric Saxe 		 */
14560e751525SEric Saxe 		if (pg->cmt_children != NULL) {
14570e751525SEric Saxe 			children = pg->cmt_children;
14580e751525SEric Saxe 
14590e751525SEric Saxe 			group_iter_init(&child_iter);
14600e751525SEric Saxe 			while ((child = group_iterate(children, &child_iter))
14610e751525SEric Saxe 			    != NULL) {
14620e751525SEric Saxe 				/*
14630e751525SEric Saxe 				 * Transplant child from it's siblings set to
14640e751525SEric Saxe 				 * PGs.
14650e751525SEric Saxe 				 */
14660e751525SEric Saxe 				if (pg->cmt_siblings != NULL &&
14670e751525SEric Saxe 				    child->cmt_siblings != NULL &&
14680e751525SEric Saxe 				    group_remove(child->cmt_siblings, child,
14690e751525SEric Saxe 				    GRP_NORESIZE) != -1) {
14700e751525SEric Saxe 					r = group_add(pg->cmt_siblings, child,
14710e751525SEric Saxe 					    GRP_NORESIZE);
14720e751525SEric Saxe 					ASSERT(r == 0);
14730e751525SEric Saxe 				}
14740e751525SEric Saxe 			}
14750e751525SEric Saxe 		}
14760e751525SEric Saxe 
14770e751525SEric Saxe 		/*
14780e751525SEric Saxe 		 * Reset the callbacks to the defaults
14790e751525SEric Saxe 		 */
14800e751525SEric Saxe 		pg_callback_set_defaults((pg_t *)pg);
14810e751525SEric Saxe 
14820e751525SEric Saxe 		/*
14830e751525SEric Saxe 		 * Update all the CPU lineages in each of PG's CPUs
14840e751525SEric Saxe 		 */
14850e751525SEric Saxe 		PG_CPU_ITR_INIT(pg, cpu_iter);
14860e751525SEric Saxe 		while ((cpu = pg_cpu_next(&cpu_iter)) != NULL) {
14870e751525SEric Saxe 			group_t		*pgs;
14880e751525SEric Saxe 			pg_cmt_t	*cpu_pg;
14890e751525SEric Saxe 			group_iter_t	liter;	/* Iterator for the lineage */
14900e751525SEric Saxe 
14910e751525SEric Saxe 			/*
14920e751525SEric Saxe 			 * Iterate over the CPU's PGs updating the children
14930e751525SEric Saxe 			 * of the PG being promoted, since they have a new
14940e751525SEric Saxe 			 * parent and siblings set.
14950e751525SEric Saxe 			 */
14960e751525SEric Saxe 			pgs = &cpu->cpu_pg->pgs;
14970e751525SEric Saxe 			group_iter_init(&liter);
14980e751525SEric Saxe 			while ((cpu_pg = group_iterate(pgs, &liter)) != NULL) {
14990e751525SEric Saxe 				if (cpu_pg->cmt_parent == pg) {
15000e751525SEric Saxe 					cpu_pg->cmt_parent = pg->cmt_parent;
15010e751525SEric Saxe 					cpu_pg->cmt_siblings = pg->cmt_siblings;
15020e751525SEric Saxe 				}
15030e751525SEric Saxe 			}
15040e751525SEric Saxe 
15050e751525SEric Saxe 			/*
15060e751525SEric Saxe 			 * Update the CPU's lineages
15070e751525SEric Saxe 			 */
15080e751525SEric Saxe 			pgs = &cpu->cpu_pg->cmt_pgs;
15090e751525SEric Saxe 			(void) group_remove(pgs, pg, GRP_NORESIZE);
15100e751525SEric Saxe 			pgs = &cpu->cpu_pg->pgs;
15110e751525SEric Saxe 			(void) group_remove(pgs, pg, GRP_NORESIZE);
15120e751525SEric Saxe 		}
15130e751525SEric Saxe 	}
15140e751525SEric Saxe 	start_cpus();
15150e751525SEric Saxe 	return (0);
15160e751525SEric Saxe }
15170e751525SEric Saxe 
15180e751525SEric Saxe /*
15190e751525SEric Saxe  * Disable CMT scheduling
15200e751525SEric Saxe  */
15210e751525SEric Saxe static void
15220e751525SEric Saxe pg_cmt_disable(void)
15230e751525SEric Saxe {
15240e751525SEric Saxe 	cpu_t	*cpu;
15250e751525SEric Saxe 
15260e751525SEric Saxe 	pause_cpus(NULL);
15270e751525SEric Saxe 	cpu = cpu_list;
15280e751525SEric Saxe 
15296890d023SEric Saxe 	do {
15300e751525SEric Saxe 		if (cpu->cpu_pg)
15310e751525SEric Saxe 			group_empty(&cpu->cpu_pg->cmt_pgs);
15320e751525SEric Saxe 	} while ((cpu = cpu->cpu_next) != cpu_list);
15330e751525SEric Saxe 
15340e751525SEric Saxe 	cmt_sched_disabled = 1;
15350e751525SEric Saxe 	start_cpus();
15360e751525SEric Saxe 	cmn_err(CE_NOTE, "!CMT thread placement optimizations unavailable");
15370e751525SEric Saxe }
15380e751525SEric Saxe 
15390e751525SEric Saxe static int
15400e751525SEric Saxe pg_cmt_lineage_validate(pg_cmt_t **lineage, int *sz)
15410e751525SEric Saxe {
15420e751525SEric Saxe 	int		i, size;
15430e751525SEric Saxe 	pg_cmt_t	*pg, *parent, *pg_bad;
15440e751525SEric Saxe 	cpu_t		*cp;
15450e751525SEric Saxe 	pg_cpu_itr_t	cpu_iter;
15460e751525SEric Saxe 
15470e751525SEric Saxe 	ASSERT(MUTEX_HELD(&cpu_lock));
15480e751525SEric Saxe 
15490e751525SEric Saxe revalidate:
15500e751525SEric Saxe 	size = *sz;
15510e751525SEric Saxe 	pg_bad = NULL;
15520e751525SEric Saxe 	for (i = 0; i < size - 1; i++) {
15530e751525SEric Saxe 
15540e751525SEric Saxe 		pg = lineage[i];
15550e751525SEric Saxe 		parent = lineage[i + 1];
15566890d023SEric Saxe 
15576890d023SEric Saxe 		/*
15580e751525SEric Saxe 		 * We assume that the lineage has already been sorted
15590e751525SEric Saxe 		 * by the number of CPUs. In fact, we depend on it.
15606890d023SEric Saxe 		 */
15610e751525SEric Saxe 		ASSERT(PG_NUM_CPUS((pg_t *)pg) <= PG_NUM_CPUS((pg_t *)parent));
15626890d023SEric Saxe 
15636890d023SEric Saxe 		/*
15640e751525SEric Saxe 		 * Walk each of the CPUs in the PGs group, and verify that
15650e751525SEric Saxe 		 * the next larger PG contains at least the CPUs in this one.
15666890d023SEric Saxe 		 */
15670e751525SEric Saxe 		PG_CPU_ITR_INIT((pg_t *)pg, cpu_iter);
15680e751525SEric Saxe 		while ((cp = pg_cpu_next(&cpu_iter)) != NULL) {
15690e751525SEric Saxe 			if (pg_cpu_find((pg_t *)parent, cp) == B_FALSE) {
15700e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_NON_CONCENTRIC;
15710e751525SEric Saxe 				goto handle_error;
15726890d023SEric Saxe 			}
15730e751525SEric Saxe 		}
15746890d023SEric Saxe 	}
15756890d023SEric Saxe 
15760e751525SEric Saxe handle_error:
15770e751525SEric Saxe 	switch (cmt_lineage_status) {
15780e751525SEric Saxe 	case CMT_LINEAGE_VALID:
15790e751525SEric Saxe 	case CMT_LINEAGE_REPAIRED:
15800e751525SEric Saxe 		break;
15810e751525SEric Saxe 	case CMT_LINEAGE_NON_CONCENTRIC:
15826890d023SEric Saxe 		/*
15830e751525SEric Saxe 		 * We've detected a non-concentric PG lineage.
15840e751525SEric Saxe 		 *
15850e751525SEric Saxe 		 * This can happen when some of the CPU grouping information
15860e751525SEric Saxe 		 * is derived from buggy sources (for example, incorrect ACPI
15870e751525SEric Saxe 		 * tables on x86 systems).
15880e751525SEric Saxe 		 *
15890e751525SEric Saxe 		 * We attempt to recover from this by pruning out the
15900e751525SEric Saxe 		 * illegal groupings from the PG hierarchy, which means that
15910e751525SEric Saxe 		 * we won't optimize for those levels, but we will for the
15920e751525SEric Saxe 		 * remaining ones.
15930e751525SEric Saxe 		 *
15940e751525SEric Saxe 		 * If a given level has CPUs not found in it's parent, then
15950e751525SEric Saxe 		 * we examine the PG and it's parent to see if either grouping
15960e751525SEric Saxe 		 * is enumerated from potentially buggy sources.
15970e751525SEric Saxe 		 *
15980e751525SEric Saxe 		 * If one has less CPUs than the other, and contains CPUs
15990e751525SEric Saxe 		 * not found in the parent, and it is an untrusted enumeration,
16000e751525SEric Saxe 		 * then prune it. If both have the same number of CPUs, then
16010e751525SEric Saxe 		 * prune the one that is untrusted.
16020e751525SEric Saxe 		 *
16030e751525SEric Saxe 		 * This process repeats until we have a concentric lineage,
16040e751525SEric Saxe 		 * or we would have to prune out level derived from what we
16050e751525SEric Saxe 		 * thought was a reliable source, in which case CMT scheduling
16060e751525SEric Saxe 		 * is disabled all together.
16076890d023SEric Saxe 		 */
16080e751525SEric Saxe 		if ((PG_NUM_CPUS((pg_t *)pg) < PG_NUM_CPUS((pg_t *)parent)) &&
16090e751525SEric Saxe 		    (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw))) {
16100e751525SEric Saxe 			pg_bad = pg;
16110e751525SEric Saxe 		} else if (PG_NUM_CPUS((pg_t *)pg) ==
16120e751525SEric Saxe 		    PG_NUM_CPUS((pg_t *)parent)) {
16130e751525SEric Saxe 			if (PG_CMT_HW_SUSPECT(((pghw_t *)parent)->pghw_hw)) {
16140e751525SEric Saxe 				pg_bad = parent;
16150e751525SEric Saxe 			} else if (PG_CMT_HW_SUSPECT(((pghw_t *)pg)->pghw_hw)) {
16160e751525SEric Saxe 				pg_bad = pg;
16176890d023SEric Saxe 			}
16186890d023SEric Saxe 		}
16190e751525SEric Saxe 		if (pg_bad) {
16200e751525SEric Saxe 			if (pg_cmt_prune(pg_bad, lineage, sz) == 0) {
16210e751525SEric Saxe 				cmt_lineage_status = CMT_LINEAGE_REPAIRED;
16220e751525SEric Saxe 				goto revalidate;
16230e751525SEric Saxe 			}
16240e751525SEric Saxe 		}
16250e751525SEric Saxe 		/*FALLTHROUGH*/
16260e751525SEric Saxe 	default:
16270e751525SEric Saxe 		/*
16280e751525SEric Saxe 		 * If we're here, something has gone wrong in trying to
16290e751525SEric Saxe 		 * recover from a illegal PG hierarchy, or we've encountered
16300e751525SEric Saxe 		 * a validation error for which we don't know how to recover.
16310e751525SEric Saxe 		 * In this case, disable CMT scheduling all together.
16320e751525SEric Saxe 		 */
16330e751525SEric Saxe 		pg_cmt_disable();
16340e751525SEric Saxe 		cmt_lineage_status = CMT_LINEAGE_UNRECOVERABLE;
16350e751525SEric Saxe 		return (-1);
16360e751525SEric Saxe 	}
16370e751525SEric Saxe 	return (0);
16386890d023SEric Saxe }
1639