xref: /onnv-gate/usr/src/uts/common/os/pg.c (revision 9438:c82dba6fad5d)
13434Sesaxe /*
23434Sesaxe  * CDDL HEADER START
33434Sesaxe  *
43434Sesaxe  * The contents of this file are subject to the terms of the
53434Sesaxe  * Common Development and Distribution License (the "License").
63434Sesaxe  * You may not use this file except in compliance with the License.
73434Sesaxe  *
83434Sesaxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93434Sesaxe  * or http://www.opensolaris.org/os/licensing.
103434Sesaxe  * See the License for the specific language governing permissions
113434Sesaxe  * and limitations under the License.
123434Sesaxe  *
133434Sesaxe  * When distributing Covered Code, include this CDDL HEADER in each
143434Sesaxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153434Sesaxe  * If applicable, add the following below this CDDL HEADER, with the
163434Sesaxe  * fields enclosed by brackets "[]" replaced with your own identifying
173434Sesaxe  * information: Portions Copyright [yyyy] [name of copyright owner]
183434Sesaxe  *
193434Sesaxe  * CDDL HEADER END
203434Sesaxe  */
213434Sesaxe /*
228906SEric.Saxe@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
233434Sesaxe  * Use is subject to license terms.
243434Sesaxe  */
253434Sesaxe 
263434Sesaxe #include <sys/systm.h>
273434Sesaxe #include <sys/types.h>
283434Sesaxe #include <sys/param.h>
293434Sesaxe #include <sys/thread.h>
303434Sesaxe #include <sys/cpuvar.h>
313434Sesaxe #include <sys/cpupart.h>
323434Sesaxe #include <sys/kmem.h>
333434Sesaxe #include <sys/cmn_err.h>
343434Sesaxe #include <sys/kstat.h>
353434Sesaxe #include <sys/processor.h>
363434Sesaxe #include <sys/disp.h>
373434Sesaxe #include <sys/group.h>
383434Sesaxe #include <sys/pg.h>
393434Sesaxe 
403434Sesaxe /*
413434Sesaxe  * Processor groups
423434Sesaxe  *
433434Sesaxe  * With the introduction of Chip Multi-Threaded (CMT) processor architectures,
443434Sesaxe  * it is no longer necessarily true that a given physical processor module
453434Sesaxe  * will present itself as a single schedulable entity (cpu_t). Rather, each
463434Sesaxe  * chip and/or processor core may present itself as one or more "logical" CPUs.
473434Sesaxe  *
483434Sesaxe  * The logical CPUs presented may share physical components such as caches,
493434Sesaxe  * data pipes, execution pipelines, FPUs, etc. It is advantageous to have the
503434Sesaxe  * kernel be aware of the relationships existing between logical CPUs so that
513434Sesaxe  * the appropriate optmizations may be employed.
523434Sesaxe  *
533434Sesaxe  * The processor group abstraction represents a set of logical CPUs that
543434Sesaxe  * generally share some sort of physical or characteristic relationship.
553434Sesaxe  *
563434Sesaxe  * In the case of a physical sharing relationship, the CPUs in the group may
573434Sesaxe  * share a pipeline, cache or floating point unit. In the case of a logical
583434Sesaxe  * relationship, a PG may represent the set of CPUs in a processor set, or the
593434Sesaxe  * set of CPUs running at a particular clock speed.
603434Sesaxe  *
613434Sesaxe  * The generic processor group structure, pg_t, contains the elements generic
623434Sesaxe  * to a group of CPUs. Depending on the nature of the CPU relationship
633434Sesaxe  * (LOGICAL or PHYSICAL), a pointer to a pg may be recast to a "view" of that
643434Sesaxe  * PG where more specific data is represented.
653434Sesaxe  *
663434Sesaxe  * As an example, a PG representing a PHYSICAL relationship, may be recast to
673434Sesaxe  * a pghw_t, where data further describing the hardware sharing relationship
683434Sesaxe  * is maintained. See pghw.c and pghw.h for details on physical PGs.
693434Sesaxe  *
703434Sesaxe  * At this time a more specialized casting of a PG representing a LOGICAL
713434Sesaxe  * relationship has not been implemented, but the architecture allows for this
723434Sesaxe  * in the future.
733434Sesaxe  *
743434Sesaxe  * Processor Group Classes
753434Sesaxe  *
763434Sesaxe  * Processor group consumers may wish to maintain and associate specific
773434Sesaxe  * data with the PGs they create. For this reason, a mechanism for creating
783434Sesaxe  * class specific PGs exists. Classes may overload the default functions for
793434Sesaxe  * creating, destroying, and associating CPUs with PGs, and may also register
803434Sesaxe  * class specific callbacks to be invoked when the CPU related system
813434Sesaxe  * configuration changes. Class specific data is stored/associated with
823434Sesaxe  * PGs by incorporating the pg_t (or pghw_t, as appropriate), as the first
833434Sesaxe  * element of a class specific PG object. In memory, such a structure may look
843434Sesaxe  * like:
853434Sesaxe  *
863434Sesaxe  * ----------------------- - - -
873434Sesaxe  * | common              | | | |  <--(pg_t *)
883434Sesaxe  * ----------------------- | | -
893434Sesaxe  * | HW specific         | | | <-----(pghw_t *)
903434Sesaxe  * ----------------------- | -
913434Sesaxe  * | class specific      | | <-------(pg_cmt_t *)
923434Sesaxe  * ----------------------- -
933434Sesaxe  *
943434Sesaxe  * Access to the PG class specific data can be had by casting a pointer to
953434Sesaxe  * it's class specific view.
963434Sesaxe  */
973434Sesaxe 
983434Sesaxe static pg_t		*pg_alloc_default(pg_class_t);
993434Sesaxe static void		pg_free_default(pg_t *);
1008906SEric.Saxe@Sun.COM static void		pg_null_op();
1013434Sesaxe 
1023434Sesaxe /*
1033434Sesaxe  * Bootstrap CPU specific PG data
1043434Sesaxe  * See pg_cpu_bootstrap()
1053434Sesaxe  */
1063434Sesaxe static cpu_pg_t		bootstrap_pg_data;
1073434Sesaxe 
1083434Sesaxe /*
1093434Sesaxe  * Bitset of allocated PG ids (they are sequential)
1103434Sesaxe  * and the next free id in the set.
1113434Sesaxe  */
1123434Sesaxe static bitset_t		pg_id_set;
1133434Sesaxe static pgid_t		pg_id_next = 0;
1143434Sesaxe 
1153434Sesaxe /*
1163434Sesaxe  * Default and externed PG ops vectors
1173434Sesaxe  */
1183434Sesaxe static struct pg_ops pg_ops_default = {
1193434Sesaxe 	pg_alloc_default,	/* alloc */
1203434Sesaxe 	pg_free_default,	/* free */
1213434Sesaxe 	NULL,			/* cpu_init */
1223434Sesaxe 	NULL,			/* cpu_fini */
1233434Sesaxe 	NULL,			/* cpu_active */
1243434Sesaxe 	NULL,			/* cpu_inactive */
1253434Sesaxe 	NULL,			/* cpupart_in */
1263434Sesaxe 	NULL,			/* cpupart_out */
1273434Sesaxe 	NULL,			/* cpupart_move */
1283434Sesaxe 	NULL,			/* cpu_belongs */
1298906SEric.Saxe@Sun.COM 	NULL,			/* policy_name */
1308906SEric.Saxe@Sun.COM };
1318906SEric.Saxe@Sun.COM 
1328906SEric.Saxe@Sun.COM static struct pg_cb_ops pg_cb_ops_default = {
1338906SEric.Saxe@Sun.COM 	pg_null_op,		/* thread_swtch */
1348906SEric.Saxe@Sun.COM 	pg_null_op,		/* thread_remain */
1353434Sesaxe };
1363434Sesaxe 
1373434Sesaxe /*
1383434Sesaxe  * Class specific PG allocation callbacks
1393434Sesaxe  */
1403434Sesaxe #define	PG_ALLOC(class)							\
1413434Sesaxe 	(pg_classes[class].pgc_ops->alloc ?				\
1423434Sesaxe 	    pg_classes[class].pgc_ops->alloc() :			\
1433434Sesaxe 	    pg_classes[pg_default_cid].pgc_ops->alloc())
1443434Sesaxe 
1453434Sesaxe #define	PG_FREE(pg)							\
1463434Sesaxe 	((pg)->pg_class->pgc_ops->free ?				\
1473434Sesaxe 	    (pg)->pg_class->pgc_ops->free(pg) :				\
1483434Sesaxe 	    pg_classes[pg_default_cid].pgc_ops->free(pg))		\
1493434Sesaxe 
1503434Sesaxe 
1513434Sesaxe /*
1528906SEric.Saxe@Sun.COM  * Class specific PG policy name
1538906SEric.Saxe@Sun.COM  */
1548906SEric.Saxe@Sun.COM #define	PG_POLICY_NAME(pg)						\
1558906SEric.Saxe@Sun.COM 	((pg)->pg_class->pgc_ops->policy_name ?				\
1568906SEric.Saxe@Sun.COM 	    (pg)->pg_class->pgc_ops->policy_name(pg) : NULL)		\
1578906SEric.Saxe@Sun.COM 
1588906SEric.Saxe@Sun.COM /*
1593434Sesaxe  * Class specific membership test callback
1603434Sesaxe  */
1613434Sesaxe #define	PG_CPU_BELONGS(pg, cp)						\
1623434Sesaxe 	((pg)->pg_class->pgc_ops->cpu_belongs ?				\
1633434Sesaxe 	    (pg)->pg_class->pgc_ops->cpu_belongs(pg, cp) : 0)		\
1643434Sesaxe 
1653434Sesaxe /*
1663434Sesaxe  * CPU configuration callbacks
1673434Sesaxe  */
1689352SEric.Saxe@Sun.COM #define	PG_CPU_INIT(class, cp, cpu_pg)					\
1693434Sesaxe {									\
1703434Sesaxe 	if (pg_classes[class].pgc_ops->cpu_init)			\
1719352SEric.Saxe@Sun.COM 		pg_classes[class].pgc_ops->cpu_init(cp, cpu_pg);	\
1723434Sesaxe }
1733434Sesaxe 
1749352SEric.Saxe@Sun.COM #define	PG_CPU_FINI(class, cp, cpu_pg)					\
1753434Sesaxe {									\
1763434Sesaxe 	if (pg_classes[class].pgc_ops->cpu_fini)			\
1779352SEric.Saxe@Sun.COM 		pg_classes[class].pgc_ops->cpu_fini(cp, cpu_pg);	\
1783434Sesaxe }
1793434Sesaxe 
1803434Sesaxe #define	PG_CPU_ACTIVE(class, cp)					\
1813434Sesaxe {									\
1823434Sesaxe 	if (pg_classes[class].pgc_ops->cpu_active)			\
1833434Sesaxe 		pg_classes[class].pgc_ops->cpu_active(cp);		\
1843434Sesaxe }
1853434Sesaxe 
1863434Sesaxe #define	PG_CPU_INACTIVE(class, cp)					\
1873434Sesaxe {									\
1883434Sesaxe 	if (pg_classes[class].pgc_ops->cpu_inactive)			\
1893434Sesaxe 		pg_classes[class].pgc_ops->cpu_inactive(cp);		\
1903434Sesaxe }
1913434Sesaxe 
1923434Sesaxe /*
1933434Sesaxe  * CPU / cpupart configuration callbacks
1943434Sesaxe  */
1953434Sesaxe #define	PG_CPUPART_IN(class, cp, pp)					\
1963434Sesaxe {									\
1973434Sesaxe 	if (pg_classes[class].pgc_ops->cpupart_in)			\
1983434Sesaxe 		pg_classes[class].pgc_ops->cpupart_in(cp, pp);		\
1993434Sesaxe }
2003434Sesaxe 
2013434Sesaxe #define	PG_CPUPART_OUT(class, cp, pp)					\
2023434Sesaxe {									\
2033434Sesaxe 	if (pg_classes[class].pgc_ops->cpupart_out)			\
2043434Sesaxe 		pg_classes[class].pgc_ops->cpupart_out(cp, pp);		\
2053434Sesaxe }
2063434Sesaxe 
2073434Sesaxe #define	PG_CPUPART_MOVE(class, cp, old, new)				\
2083434Sesaxe {									\
2093434Sesaxe 	if (pg_classes[class].pgc_ops->cpupart_move)			\
2103434Sesaxe 		pg_classes[class].pgc_ops->cpupart_move(cp, old, new);	\
2113434Sesaxe }
2123434Sesaxe 
2133434Sesaxe 
2143434Sesaxe 
2153434Sesaxe static pg_class_t	*pg_classes;
2163434Sesaxe static int		pg_nclasses;
2173434Sesaxe 
2183434Sesaxe static pg_cid_t		pg_default_cid;
2193434Sesaxe 
2203434Sesaxe /*
2218906SEric.Saxe@Sun.COM  * Initialze common PG subsystem.
2223434Sesaxe  */
2233434Sesaxe void
2243434Sesaxe pg_init(void)
2253434Sesaxe {
2268906SEric.Saxe@Sun.COM 	extern void pg_cmt_class_init();
2278906SEric.Saxe@Sun.COM 
2283434Sesaxe 	pg_default_cid =
2293434Sesaxe 	    pg_class_register("default", &pg_ops_default, PGR_LOGICAL);
2308906SEric.Saxe@Sun.COM 
2318906SEric.Saxe@Sun.COM 	/*
2328906SEric.Saxe@Sun.COM 	 * Initialize classes to allow them to register with the framework
2338906SEric.Saxe@Sun.COM 	 */
2348906SEric.Saxe@Sun.COM 	pg_cmt_class_init();
2358906SEric.Saxe@Sun.COM 
2368906SEric.Saxe@Sun.COM 	pg_cpu0_init();
2373434Sesaxe }
2383434Sesaxe 
2393434Sesaxe /*
2403434Sesaxe  * Perform CPU 0 initialization
2413434Sesaxe  */
2423434Sesaxe void
2433434Sesaxe pg_cpu0_init(void)
2443434Sesaxe {
2453434Sesaxe 	extern void pghw_physid_create();
2463434Sesaxe 
2473434Sesaxe 	/*
2483434Sesaxe 	 * Create the physical ID cache for the boot CPU
2493434Sesaxe 	 */
2503434Sesaxe 	pghw_physid_create(CPU);
2513434Sesaxe 
2523434Sesaxe 	/*
2533434Sesaxe 	 * pg_cpu_* require that cpu_lock be held
2543434Sesaxe 	 */
2553434Sesaxe 	mutex_enter(&cpu_lock);
2563434Sesaxe 
2573434Sesaxe 	pg_cpu_init(CPU);
2583434Sesaxe 	pg_cpupart_in(CPU, &cp_default);
2593434Sesaxe 	pg_cpu_active(CPU);
2603434Sesaxe 
2613434Sesaxe 	mutex_exit(&cpu_lock);
2623434Sesaxe }
2633434Sesaxe 
2643434Sesaxe /*
2653676Sesaxe  * Invoked when topology for CPU0 changes
2663676Sesaxe  * post pg_cpu0_init().
2673676Sesaxe  *
2683676Sesaxe  * Currently happens as a result of null_proc_lpa
2693676Sesaxe  * on Starcat.
2703676Sesaxe  */
2713676Sesaxe void
2723676Sesaxe pg_cpu0_reinit(void)
2733676Sesaxe {
2743676Sesaxe 	mutex_enter(&cpu_lock);
2753676Sesaxe 	pg_cpu_inactive(CPU);
2763676Sesaxe 	pg_cpupart_out(CPU, &cp_default);
2773676Sesaxe 	pg_cpu_fini(CPU);
2783676Sesaxe 
2793676Sesaxe 	pg_cpu_init(CPU);
2803676Sesaxe 	pg_cpupart_in(CPU, &cp_default);
2813676Sesaxe 	pg_cpu_active(CPU);
2823676Sesaxe 	mutex_exit(&cpu_lock);
2833676Sesaxe }
2843676Sesaxe 
2853676Sesaxe /*
2863434Sesaxe  * Register a new PG class
2873434Sesaxe  */
2883434Sesaxe pg_cid_t
2893434Sesaxe pg_class_register(char *name, struct pg_ops *ops, pg_relation_t relation)
2903434Sesaxe {
2913434Sesaxe 	pg_class_t	*newclass;
2923434Sesaxe 	pg_class_t	*classes_old;
2933434Sesaxe 	id_t		cid;
2943434Sesaxe 
2953434Sesaxe 	mutex_enter(&cpu_lock);
2963434Sesaxe 
2973434Sesaxe 	/*
2983434Sesaxe 	 * Allocate a new pg_class_t in the pg_classes array
2993434Sesaxe 	 */
3003434Sesaxe 	if (pg_nclasses == 0) {
3013434Sesaxe 		pg_classes = kmem_zalloc(sizeof (pg_class_t), KM_SLEEP);
3023434Sesaxe 	} else {
3033434Sesaxe 		classes_old = pg_classes;
3043434Sesaxe 		pg_classes =
3053434Sesaxe 		    kmem_zalloc(sizeof (pg_class_t) * (pg_nclasses + 1),
3068906SEric.Saxe@Sun.COM 		    KM_SLEEP);
3073434Sesaxe 		(void) kcopy(classes_old, pg_classes,
3083434Sesaxe 		    sizeof (pg_class_t) * pg_nclasses);
3093434Sesaxe 		kmem_free(classes_old, sizeof (pg_class_t) * pg_nclasses);
3103434Sesaxe 	}
3113434Sesaxe 
3123434Sesaxe 	cid = pg_nclasses++;
3133434Sesaxe 	newclass = &pg_classes[cid];
3143434Sesaxe 
3153434Sesaxe 	(void) strncpy(newclass->pgc_name, name, PG_CLASS_NAME_MAX);
3163434Sesaxe 	newclass->pgc_id = cid;
3173434Sesaxe 	newclass->pgc_ops = ops;
3183434Sesaxe 	newclass->pgc_relation = relation;
3193434Sesaxe 
3203434Sesaxe 	mutex_exit(&cpu_lock);
3213434Sesaxe 
3223434Sesaxe 	return (cid);
3233434Sesaxe }
3243434Sesaxe 
3253434Sesaxe /*
3263434Sesaxe  * Try to find an existing pg in set in which to place cp.
3273434Sesaxe  * Returns the pg if found, and NULL otherwise.
3283434Sesaxe  * In the event that the CPU could belong to multiple
3293434Sesaxe  * PGs in the set, the first matching PG will be returned.
3303434Sesaxe  */
3313434Sesaxe pg_t *
3323434Sesaxe pg_cpu_find_pg(cpu_t *cp, group_t *set)
3333434Sesaxe {
3343434Sesaxe 	pg_t		*pg;
3353434Sesaxe 	group_iter_t	i;
3363434Sesaxe 
3373434Sesaxe 	group_iter_init(&i);
3383434Sesaxe 	while ((pg = group_iterate(set, &i)) != NULL) {
3393434Sesaxe 		/*
3403434Sesaxe 		 * Ask the class if the CPU belongs here
3413434Sesaxe 		 */
3423434Sesaxe 		if (PG_CPU_BELONGS(pg, cp))
3433434Sesaxe 			return (pg);
3443434Sesaxe 	}
3453434Sesaxe 	return (NULL);
3463434Sesaxe }
3473434Sesaxe 
3483434Sesaxe /*
3493434Sesaxe  * Iterate over the CPUs in a PG after initializing
3503434Sesaxe  * the iterator with PG_CPU_ITR_INIT()
3513434Sesaxe  */
3523434Sesaxe cpu_t *
3533434Sesaxe pg_cpu_next(pg_cpu_itr_t *itr)
3543434Sesaxe {
3553434Sesaxe 	cpu_t		*cpu;
3563434Sesaxe 	pg_t		*pg = itr->pg;
3573434Sesaxe 
3583434Sesaxe 	cpu = group_iterate(&pg->pg_cpus, &itr->position);
3593434Sesaxe 	return (cpu);
3603434Sesaxe }
3613434Sesaxe 
3623434Sesaxe /*
3638906SEric.Saxe@Sun.COM  * Test if a given PG contains a given CPU
3648906SEric.Saxe@Sun.COM  */
3658906SEric.Saxe@Sun.COM boolean_t
3668906SEric.Saxe@Sun.COM pg_cpu_find(pg_t *pg, cpu_t *cp)
3678906SEric.Saxe@Sun.COM {
3688906SEric.Saxe@Sun.COM 	if (group_find(&pg->pg_cpus, cp) == (uint_t)-1)
3698906SEric.Saxe@Sun.COM 		return (B_FALSE);
3708906SEric.Saxe@Sun.COM 
3718906SEric.Saxe@Sun.COM 	return (B_TRUE);
3728906SEric.Saxe@Sun.COM }
3738906SEric.Saxe@Sun.COM 
3748906SEric.Saxe@Sun.COM /*
3758906SEric.Saxe@Sun.COM  * Set the PGs callbacks to the default
3768906SEric.Saxe@Sun.COM  */
3778906SEric.Saxe@Sun.COM void
3788906SEric.Saxe@Sun.COM pg_callback_set_defaults(pg_t *pg)
3798906SEric.Saxe@Sun.COM {
3808906SEric.Saxe@Sun.COM 	bcopy(&pg_cb_ops_default, &pg->pg_cb, sizeof (struct pg_cb_ops));
3818906SEric.Saxe@Sun.COM }
3828906SEric.Saxe@Sun.COM 
3838906SEric.Saxe@Sun.COM /*
3843434Sesaxe  * Create a PG of a given class.
3853434Sesaxe  * This routine may block.
3863434Sesaxe  */
3873434Sesaxe pg_t *
3883434Sesaxe pg_create(pg_cid_t cid)
3893434Sesaxe {
3903434Sesaxe 	pg_t	*pg;
3913434Sesaxe 	pgid_t	id;
3923434Sesaxe 
3933434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
3943434Sesaxe 
3953434Sesaxe 	/*
3963434Sesaxe 	 * Call the class specific PG allocation routine
3973434Sesaxe 	 */
3983434Sesaxe 	pg = PG_ALLOC(cid);
3993434Sesaxe 	pg->pg_class = &pg_classes[cid];
4003434Sesaxe 	pg->pg_relation = pg->pg_class->pgc_relation;
4013434Sesaxe 
4023434Sesaxe 	/*
4033434Sesaxe 	 * Find the next free sequential pg id
4043434Sesaxe 	 */
4053434Sesaxe 	do {
4063434Sesaxe 		if (pg_id_next >= bitset_capacity(&pg_id_set))
4073434Sesaxe 			bitset_resize(&pg_id_set, pg_id_next + 1);
4083434Sesaxe 		id = pg_id_next++;
4093434Sesaxe 	} while (bitset_in_set(&pg_id_set, id));
4103434Sesaxe 
4113434Sesaxe 	pg->pg_id = id;
4123434Sesaxe 	bitset_add(&pg_id_set, pg->pg_id);
4133434Sesaxe 
4143434Sesaxe 	/*
4153434Sesaxe 	 * Create the PG's CPU group
4163434Sesaxe 	 */
4173434Sesaxe 	group_create(&pg->pg_cpus);
4183434Sesaxe 
4198906SEric.Saxe@Sun.COM 	/*
4208906SEric.Saxe@Sun.COM 	 * Initialize the events ops vector
4218906SEric.Saxe@Sun.COM 	 */
4228906SEric.Saxe@Sun.COM 	pg_callback_set_defaults(pg);
4238906SEric.Saxe@Sun.COM 
4243434Sesaxe 	return (pg);
4253434Sesaxe }
4263434Sesaxe 
4273434Sesaxe /*
4283434Sesaxe  * Destroy a PG.
4293434Sesaxe  * This routine may block.
4303434Sesaxe  */
4313434Sesaxe void
4323434Sesaxe pg_destroy(pg_t *pg)
4333434Sesaxe {
4343434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
4353434Sesaxe 
4363434Sesaxe 	group_destroy(&pg->pg_cpus);
4373434Sesaxe 
4383434Sesaxe 	/*
4393434Sesaxe 	 * Unassign the pg_id
4403434Sesaxe 	 */
4413434Sesaxe 	if (pg_id_next > pg->pg_id)
4423434Sesaxe 		pg_id_next = pg->pg_id;
4433434Sesaxe 	bitset_del(&pg_id_set, pg->pg_id);
4443434Sesaxe 
4453434Sesaxe 	/*
4463434Sesaxe 	 * Invoke the class specific de-allocation routine
4473434Sesaxe 	 */
4483434Sesaxe 	PG_FREE(pg);
4493434Sesaxe }
4503434Sesaxe 
4513434Sesaxe /*
4523434Sesaxe  * Add the CPU "cp" to processor group "pg"
4533434Sesaxe  * This routine may block.
4543434Sesaxe  */
4553434Sesaxe void
4569352SEric.Saxe@Sun.COM pg_cpu_add(pg_t *pg, cpu_t *cp, cpu_pg_t *cpu_pg)
4573434Sesaxe {
4583434Sesaxe 	int	err;
4593434Sesaxe 
4603434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
4613434Sesaxe 
4623434Sesaxe 	/* This adds the CPU to the PG's CPU group */
4633434Sesaxe 	err = group_add(&pg->pg_cpus, cp, GRP_RESIZE);
4643434Sesaxe 	ASSERT(err == 0);
4653434Sesaxe 
4669352SEric.Saxe@Sun.COM 	/*
4679352SEric.Saxe@Sun.COM 	 * The CPU should be referencing the bootstrap PG data still
4689352SEric.Saxe@Sun.COM 	 * at this point, since this routine may block causing us to
4699352SEric.Saxe@Sun.COM 	 * enter the dispatcher.
4709352SEric.Saxe@Sun.COM 	 */
471*9438SEric.Saxe@Sun.COM 	ASSERT(pg_cpu_is_bootstrapped(cp));
4729352SEric.Saxe@Sun.COM 
4733434Sesaxe 	/* This adds the PG to the CPUs PG group */
4749352SEric.Saxe@Sun.COM 	err = group_add(&cpu_pg->pgs, pg, GRP_RESIZE);
4753434Sesaxe 	ASSERT(err == 0);
4763434Sesaxe }
4773434Sesaxe 
4783434Sesaxe /*
4793434Sesaxe  * Remove "cp" from "pg".
4803434Sesaxe  * This routine may block.
4813434Sesaxe  */
4823434Sesaxe void
4839352SEric.Saxe@Sun.COM pg_cpu_delete(pg_t *pg, cpu_t *cp, cpu_pg_t *cpu_pg)
4843434Sesaxe {
4853434Sesaxe 	int	err;
4863434Sesaxe 
4873434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
4883434Sesaxe 
4893434Sesaxe 	/* Remove the CPU from the PG */
4903434Sesaxe 	err = group_remove(&pg->pg_cpus, cp, GRP_RESIZE);
4913434Sesaxe 	ASSERT(err == 0);
4923434Sesaxe 
4939352SEric.Saxe@Sun.COM 	/*
4949352SEric.Saxe@Sun.COM 	 * The CPU should be referencing the bootstrap PG data still
4959352SEric.Saxe@Sun.COM 	 * at this point, since this routine may block causing us to
4969352SEric.Saxe@Sun.COM 	 * enter the dispatcher.
4979352SEric.Saxe@Sun.COM 	 */
498*9438SEric.Saxe@Sun.COM 	ASSERT(pg_cpu_is_bootstrapped(cp));
4999352SEric.Saxe@Sun.COM 
5003434Sesaxe 	/* Remove the PG from the CPU's PG group */
5019352SEric.Saxe@Sun.COM 	err = group_remove(&cpu_pg->pgs, pg, GRP_RESIZE);
5023434Sesaxe 	ASSERT(err == 0);
5033434Sesaxe }
5043434Sesaxe 
5053434Sesaxe /*
5063434Sesaxe  * Allocate a CPU's PG data. This hangs off struct cpu at cpu_pg
5073434Sesaxe  */
5083434Sesaxe static cpu_pg_t *
5093434Sesaxe pg_cpu_data_alloc(void)
5103434Sesaxe {
5113434Sesaxe 	cpu_pg_t	*pgd;
5123434Sesaxe 
5133434Sesaxe 	pgd = kmem_zalloc(sizeof (cpu_pg_t), KM_SLEEP);
5143434Sesaxe 	group_create(&pgd->pgs);
5153434Sesaxe 	group_create(&pgd->cmt_pgs);
5163434Sesaxe 
5173434Sesaxe 	return (pgd);
5183434Sesaxe }
5193434Sesaxe 
5203434Sesaxe /*
5213434Sesaxe  * Free the CPU's PG data.
5223434Sesaxe  */
5233434Sesaxe static void
5243434Sesaxe pg_cpu_data_free(cpu_pg_t *pgd)
5253434Sesaxe {
5263434Sesaxe 	group_destroy(&pgd->pgs);
5273434Sesaxe 	group_destroy(&pgd->cmt_pgs);
5283434Sesaxe 	kmem_free(pgd, sizeof (cpu_pg_t));
5293434Sesaxe }
5303434Sesaxe 
5313434Sesaxe /*
5323434Sesaxe  * A new CPU is coming into the system, either via booting or DR.
5333434Sesaxe  * Allocate it's PG data, and notify all registered classes about
5343434Sesaxe  * the new CPU.
5353434Sesaxe  *
5363434Sesaxe  * This routine may block.
5373434Sesaxe  */
5383434Sesaxe void
5393434Sesaxe pg_cpu_init(cpu_t *cp)
5403434Sesaxe {
5413434Sesaxe 	pg_cid_t	i;
5429352SEric.Saxe@Sun.COM 	cpu_pg_t	*cpu_pg;
5433434Sesaxe 
5443434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
5453434Sesaxe 
5463434Sesaxe 	/*
5473434Sesaxe 	 * Allocate and size the per CPU pg data
5489352SEric.Saxe@Sun.COM 	 *
5499352SEric.Saxe@Sun.COM 	 * The CPU's PG data will be populated by the various
5509352SEric.Saxe@Sun.COM 	 * PG classes during the invocation of the PG_CPU_INIT()
5519352SEric.Saxe@Sun.COM 	 * callback below.
5529352SEric.Saxe@Sun.COM 	 *
5539352SEric.Saxe@Sun.COM 	 * Since the we could block and enter the dispatcher during
5549352SEric.Saxe@Sun.COM 	 * this process, the CPU will continue to reference the bootstrap
5559352SEric.Saxe@Sun.COM 	 * PG data until all the initialization completes.
5563434Sesaxe 	 */
557*9438SEric.Saxe@Sun.COM 	ASSERT(pg_cpu_is_bootstrapped(cp));
5589352SEric.Saxe@Sun.COM 
5599352SEric.Saxe@Sun.COM 	cpu_pg = pg_cpu_data_alloc();
5603434Sesaxe 
5613434Sesaxe 	/*
5623434Sesaxe 	 * Notify all registered classes about the new CPU
5633434Sesaxe 	 */
5643434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
5659352SEric.Saxe@Sun.COM 		PG_CPU_INIT(i, cp, cpu_pg);
5669352SEric.Saxe@Sun.COM 
5679352SEric.Saxe@Sun.COM 	/*
5689352SEric.Saxe@Sun.COM 	 * The CPU's PG data is now ready to use.
5699352SEric.Saxe@Sun.COM 	 */
5709352SEric.Saxe@Sun.COM 	cp->cpu_pg = cpu_pg;
5713434Sesaxe }
5723434Sesaxe 
5733434Sesaxe /*
5743434Sesaxe  * This CPU is being deleted from the system. Notify the classes
5753434Sesaxe  * and free up the CPU's PG data.
5763434Sesaxe  */
5773434Sesaxe void
5783434Sesaxe pg_cpu_fini(cpu_t *cp)
5793434Sesaxe {
5803434Sesaxe 	pg_cid_t	i;
5819352SEric.Saxe@Sun.COM 	cpu_pg_t	*cpu_pg;
5823434Sesaxe 
5833434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
5843434Sesaxe 
5859352SEric.Saxe@Sun.COM 	cpu_pg = cp->cpu_pg;
5869352SEric.Saxe@Sun.COM 
5873434Sesaxe 	/*
5883434Sesaxe 	 * This can happen if the CPU coming into the system
5893434Sesaxe 	 * failed to power on.
5903434Sesaxe 	 */
591*9438SEric.Saxe@Sun.COM 	if (cpu_pg == NULL || pg_cpu_is_bootstrapped(cp))
5923434Sesaxe 		return;
5933434Sesaxe 
5949352SEric.Saxe@Sun.COM 	/*
5959352SEric.Saxe@Sun.COM 	 * Have the CPU reference the bootstrap PG data to survive
5969352SEric.Saxe@Sun.COM 	 * the dispatcher should it block from here on out.
5979352SEric.Saxe@Sun.COM 	 */
598*9438SEric.Saxe@Sun.COM 	pg_cpu_bootstrap(cp);
5999352SEric.Saxe@Sun.COM 
6003434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
6019352SEric.Saxe@Sun.COM 		PG_CPU_FINI(i, cp, cpu_pg);
6023434Sesaxe 
6039352SEric.Saxe@Sun.COM 	pg_cpu_data_free(cpu_pg);
6043434Sesaxe }
6053434Sesaxe 
6063434Sesaxe /*
6073434Sesaxe  * This CPU is becoming active (online)
6083434Sesaxe  * This routine may not block as it is called from paused CPUs
6093434Sesaxe  * context.
6103434Sesaxe  */
6113434Sesaxe void
6123434Sesaxe pg_cpu_active(cpu_t *cp)
6133434Sesaxe {
6143434Sesaxe 	pg_cid_t	i;
6153434Sesaxe 
6163434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
6173434Sesaxe 
6183434Sesaxe 	/*
6193434Sesaxe 	 * Notify all registered classes about the new CPU
6203434Sesaxe 	 */
6213434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
6223434Sesaxe 		PG_CPU_ACTIVE(i, cp);
6233434Sesaxe }
6243434Sesaxe 
6253434Sesaxe /*
6263434Sesaxe  * This CPU is going inactive (offline)
6273434Sesaxe  * This routine may not block, as it is called from paused
6283434Sesaxe  * CPUs context.
6293434Sesaxe  */
6303434Sesaxe void
6313434Sesaxe pg_cpu_inactive(cpu_t *cp)
6323434Sesaxe {
6333434Sesaxe 	pg_cid_t	i;
6343434Sesaxe 
6353434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
6363434Sesaxe 
6373434Sesaxe 	/*
6383434Sesaxe 	 * Notify all registered classes about the new CPU
6393434Sesaxe 	 */
6403434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
6413434Sesaxe 		PG_CPU_INACTIVE(i, cp);
6423434Sesaxe }
6433434Sesaxe 
6443434Sesaxe /*
6453434Sesaxe  * Invoked when the CPU is about to move into the partition
6463434Sesaxe  * This routine may block.
6473434Sesaxe  */
6483434Sesaxe void
6493434Sesaxe pg_cpupart_in(cpu_t *cp, cpupart_t *pp)
6503434Sesaxe {
6513434Sesaxe 	int	i;
6523434Sesaxe 
6533434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
6543434Sesaxe 
6553434Sesaxe 	/*
6563434Sesaxe 	 * Notify all registered classes that the
6573434Sesaxe 	 * CPU is about to enter the CPU partition
6583434Sesaxe 	 */
6593434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
6603434Sesaxe 		PG_CPUPART_IN(i, cp, pp);
6613434Sesaxe }
6623434Sesaxe 
6633434Sesaxe /*
6643434Sesaxe  * Invoked when the CPU is about to move out of the partition
6653434Sesaxe  * This routine may block.
6663434Sesaxe  */
6673434Sesaxe /*ARGSUSED*/
6683434Sesaxe void
6693434Sesaxe pg_cpupart_out(cpu_t *cp, cpupart_t *pp)
6703434Sesaxe {
6713434Sesaxe 	int	i;
6723434Sesaxe 
6733434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
6743434Sesaxe 
6753434Sesaxe 	/*
6763434Sesaxe 	 * Notify all registered classes that the
6773434Sesaxe 	 * CPU is about to leave the CPU partition
6783434Sesaxe 	 */
6793434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
6803434Sesaxe 		PG_CPUPART_OUT(i, cp, pp);
6813434Sesaxe }
6823434Sesaxe 
6833434Sesaxe /*
6843434Sesaxe  * Invoked when the CPU is *moving* partitions.
6853434Sesaxe  *
6863434Sesaxe  * This routine may not block, as it is called from paused CPUs
6873434Sesaxe  * context.
6883434Sesaxe  */
6893434Sesaxe void
6903434Sesaxe pg_cpupart_move(cpu_t *cp, cpupart_t *oldpp, cpupart_t *newpp)
6913434Sesaxe {
6923434Sesaxe 	int	i;
6933434Sesaxe 
6943434Sesaxe 	ASSERT(MUTEX_HELD(&cpu_lock));
6953434Sesaxe 
6963434Sesaxe 	/*
6973434Sesaxe 	 * Notify all registered classes that the
6983434Sesaxe 	 * CPU is about to leave the CPU partition
6993434Sesaxe 	 */
7003434Sesaxe 	for (i = 0; i < pg_nclasses; i++)
7013434Sesaxe 		PG_CPUPART_MOVE(i, cp, oldpp, newpp);
7023434Sesaxe }
7033434Sesaxe 
7043434Sesaxe /*
7058906SEric.Saxe@Sun.COM  * Return a class specific string describing a policy implemented
7068906SEric.Saxe@Sun.COM  * across this PG
7078906SEric.Saxe@Sun.COM  */
7088906SEric.Saxe@Sun.COM char *
7098906SEric.Saxe@Sun.COM pg_policy_name(pg_t *pg)
7108906SEric.Saxe@Sun.COM {
7118906SEric.Saxe@Sun.COM 	char *str;
7128906SEric.Saxe@Sun.COM 	if ((str = PG_POLICY_NAME(pg)) != NULL)
7138906SEric.Saxe@Sun.COM 		return (str);
7148906SEric.Saxe@Sun.COM 
7158906SEric.Saxe@Sun.COM 	return ("N/A");
7168906SEric.Saxe@Sun.COM }
7178906SEric.Saxe@Sun.COM 
7188906SEric.Saxe@Sun.COM /*
7193434Sesaxe  * Provide the specified CPU a bootstrap pg
7203434Sesaxe  * This is needed to allow sane behaviour if any PG consuming
7213434Sesaxe  * code needs to deal with a partially initialized CPU
7223434Sesaxe  */
7233434Sesaxe void
7243434Sesaxe pg_cpu_bootstrap(cpu_t *cp)
7253434Sesaxe {
7263434Sesaxe 	cp->cpu_pg = &bootstrap_pg_data;
7273434Sesaxe }
7283434Sesaxe 
729*9438SEric.Saxe@Sun.COM /*
730*9438SEric.Saxe@Sun.COM  * Return non-zero if the specified CPU is bootstrapped,
731*9438SEric.Saxe@Sun.COM  * which means it's CPU specific PG data has not yet been
732*9438SEric.Saxe@Sun.COM  * fully constructed.
733*9438SEric.Saxe@Sun.COM  */
734*9438SEric.Saxe@Sun.COM int
735*9438SEric.Saxe@Sun.COM pg_cpu_is_bootstrapped(cpu_t *cp)
736*9438SEric.Saxe@Sun.COM {
737*9438SEric.Saxe@Sun.COM 	return (cp->cpu_pg == &bootstrap_pg_data);
738*9438SEric.Saxe@Sun.COM }
739*9438SEric.Saxe@Sun.COM 
7403434Sesaxe /*ARGSUSED*/
7413434Sesaxe static pg_t *
7423434Sesaxe pg_alloc_default(pg_class_t class)
7433434Sesaxe {
7443434Sesaxe 	return (kmem_zalloc(sizeof (pg_t), KM_SLEEP));
7453434Sesaxe }
7463434Sesaxe 
7473434Sesaxe /*ARGSUSED*/
7483434Sesaxe static void
7493434Sesaxe pg_free_default(struct pg *pg)
7503434Sesaxe {
7513434Sesaxe 	kmem_free(pg, sizeof (pg_t));
7523434Sesaxe }
7538906SEric.Saxe@Sun.COM 
7548906SEric.Saxe@Sun.COM static void
7558906SEric.Saxe@Sun.COM pg_null_op()
7568906SEric.Saxe@Sun.COM {
7578906SEric.Saxe@Sun.COM }
7588906SEric.Saxe@Sun.COM 
7598906SEric.Saxe@Sun.COM /*
7608906SEric.Saxe@Sun.COM  * Invoke the "thread switch" callback for each of the CPU's PGs
7618906SEric.Saxe@Sun.COM  * This is invoked from the dispatcher swtch() routine, which is called
7628906SEric.Saxe@Sun.COM  * when a thread running an a CPU should switch to another thread.
7638906SEric.Saxe@Sun.COM  * "cp" is the CPU on which the thread switch is happening
7648906SEric.Saxe@Sun.COM  * "now" is an unscaled hrtime_t timestamp taken in swtch()
7658906SEric.Saxe@Sun.COM  * "old" and "new" are the outgoing and incoming threads, respectively.
7668906SEric.Saxe@Sun.COM  */
7678906SEric.Saxe@Sun.COM void
7688906SEric.Saxe@Sun.COM pg_ev_thread_swtch(struct cpu *cp, hrtime_t now, kthread_t *old, kthread_t *new)
7698906SEric.Saxe@Sun.COM {
7708906SEric.Saxe@Sun.COM 	int	i, sz;
7718906SEric.Saxe@Sun.COM 	group_t	*grp;
7728906SEric.Saxe@Sun.COM 	pg_t	*pg;
7738906SEric.Saxe@Sun.COM 
7748906SEric.Saxe@Sun.COM 	grp = &cp->cpu_pg->pgs;
7758906SEric.Saxe@Sun.COM 	sz = GROUP_SIZE(grp);
7768906SEric.Saxe@Sun.COM 	for (i = 0; i < sz; i++) {
7778906SEric.Saxe@Sun.COM 		pg = GROUP_ACCESS(grp, i);
7788906SEric.Saxe@Sun.COM 		pg->pg_cb.thread_swtch(pg, cp, now, old, new);
7798906SEric.Saxe@Sun.COM 	}
7808906SEric.Saxe@Sun.COM }
7818906SEric.Saxe@Sun.COM 
7828906SEric.Saxe@Sun.COM /*
7838906SEric.Saxe@Sun.COM  * Invoke the "thread remain" callback for each of the CPU's PGs.
7848906SEric.Saxe@Sun.COM  * This is called from the dispatcher's swtch() routine when a thread
7858906SEric.Saxe@Sun.COM  * running on the CPU "cp" is switching to itself, which can happen as an
7868906SEric.Saxe@Sun.COM  * artifact of the thread's timeslice expiring.
7878906SEric.Saxe@Sun.COM  */
7888906SEric.Saxe@Sun.COM void
7898906SEric.Saxe@Sun.COM pg_ev_thread_remain(struct cpu *cp, kthread_t *t)
7908906SEric.Saxe@Sun.COM {
7918906SEric.Saxe@Sun.COM 	int	i, sz;
7928906SEric.Saxe@Sun.COM 	group_t	*grp;
7938906SEric.Saxe@Sun.COM 	pg_t	*pg;
7948906SEric.Saxe@Sun.COM 
7958906SEric.Saxe@Sun.COM 	grp = &cp->cpu_pg->pgs;
7968906SEric.Saxe@Sun.COM 	sz = GROUP_SIZE(grp);
7978906SEric.Saxe@Sun.COM 	for (i = 0; i < sz; i++) {
7988906SEric.Saxe@Sun.COM 		pg = GROUP_ACCESS(grp, i);
7998906SEric.Saxe@Sun.COM 		pg->pg_cb.thread_remain(pg, cp, t);
8008906SEric.Saxe@Sun.COM 	}
8018906SEric.Saxe@Sun.COM }
802