xref: /illumos-gate/usr/src/uts/common/sys/epm.h (revision 6fa29843813e354e472ca1ef80590ab80e2362b7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright 2023 Oxide Computer Company
28  */
29 
30 #ifndef	_SYS_EPM_H
31 #define	_SYS_EPM_H
32 
33 #include <sys/pm.h>
34 #include <sys/dditypes.h>
35 #include <sys/devops.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/taskq.h>
38 #include <sys/tzfile.h>
39 
40 /*
41  * XXXX
42  * Do we really need this include?  It may be leftover from early CPUPM code.
43  * #include <sys/processor.h>
44  */
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 #ifdef	_KERNEL
51 
52 /*
53  * epm.h:	Function prototypes and data structs for kernel pm functions.
54  */
55 
56 void e_pm_props(dev_info_t *);
57 int e_new_pm_props(dev_info_t *);
58 
59 #define	PM_LEVEL_UPONLY (-2)	/* only raise power level */
60 #define	PM_LEVEL_DOWNONLY (-3)	/* only lower power level */
61 #define	PM_LEVEL_EXACT (-4)	/* wants exact power level */
62 
63 /*
64  * Values used by e_pm_props and friends, found in devi_pm_flags
65  */
66 #define	PMC_NEEDS_SR		0x00001	/* do suspend/resume despite no "reg" */
67 #define	PMC_NO_SR		0x00002	/* don't suspend/resume despite "reg" */
68 #define	PMC_PARENTAL_SR		0x00004	/* call up tree to suspend/resume */
69 #define	PMC_WANTS_NOTIFY	0x00008	/* notify if child pwr level changes */
70 #define	PMC_BC			0x00010	/* no pm-components, backwards compat */
71 #define	PMC_COMPONENTS_DONE	0x00020 /* parsed pm-components */
72 #define	PMC_COMPONENTS_FAILED	0x00040 /* failed parsing pm-components */
73 #define	PMC_SUSPENDED		0x00080 /* device has been suspended */
74 #define	PMC_DEF_THRESH		0x00100 /* thresholds are default */
75 #define	PMC_DEV_THRESH		0x00200 /* SET_THRESHOLD ioctl seen */
76 #define	PMC_COMP_THRESH		0x00400 /* relative threshold set */
77 #define	PMC_NEXDEF_THRESH	0x00800 /* relative threshold set for nexus */
78 #define	PMC_NOPMKID		0x01000 /* non-pm'd child of pm'd parent */
79 #define	PMC_NO_INVOL		0x02000 /* no pm without driver's consent */
80 #define	PMC_VOLPMD		0x04000 /* went down voluntarily */
81 #define	PMC_SKIP_BRINGUP	0x08000 /* Skipped a dependency bringup */
82 
83 /*
84  * A node which is the console frame buffer, and should not be powered down
85  * automatically because the OBP driver doesn't know how to power it back up
86  * before using it  (can remove this when prom calls back into kernel to do
87  * io to console).
88  */
89 #define	PMC_CONSOLE_FB		0x10000	/* console framebuffer */
90 #define	PMC_NOINVOL_DONE	0x20000 /* processed by pm_noinvol_specd() */
91 #define	PMC_DRIVER_REMOVED	0x40000 /* driver is removed	*/
92 #define	PMC_CPU_DEVICE		0x80000 /* device is a power manageable CPU */
93 #define	PMC_CPU_THRESH		0x100000 /* cpu threshold set */
94 
95 #define	PMC_THRESH_ALL	(PMC_DEF_THRESH | PMC_DEV_THRESH | \
96     PMC_COMP_THRESH | PMC_NEXDEF_THRESH | PMC_CPU_THRESH)
97 #define	PMC_THRESH_NONE	~(PMC_THRESH_ALL)
98 
99 /* Flags for the component */
100 #define	PM_POWER_OP		0x00001	/* set power in process */
101 #define	PM_PHC_WHILE_SET_POWER	0x00002	/* phc and set power deadlock */
102 
103 /*
104  * One of these is attached to each devinfo that is autopm'd.
105  */
106 typedef struct pm_scan {
107 	int		ps_idle_down;	/* PMID_XXX flags */
108 	int		ps_scan_flags;	/* scan flags, defined below */
109 	timeout_id_t	ps_scan_id;	/* per dip scan timeout id */
110 } pm_scan_t;
111 
112 /*
113  * ps_scan_flags may take the following values, plus possibly
114  * more defined.
115  */
116 #define	PM_SCANNING		0x100	/* scanning: pm_scan_dev is active */
117 #define	PM_SCAN_AGAIN		0x200
118 #define	PM_SCAN_STOP		0x400
119 #define	PM_SCAN_DISPATCHED	0x800
120 
121 #define	PM_DEFAULT_MIN_SCAN	((clock_t)15) /* Minimum scan interval in sec */
122 #define	PM_CPU_MIN_SCAN		((clock_t)1) /* Minimum interval for CPUs */
123 
124 /*
125  * Power management component definitions, used for tracking idleness of
126  * devices.  An array of these hangs off the devi_pm_components member of the
127  * dev_info struct (if initialized by driver and/or auto-pm)
128  * The array of these structs is followed in the same kmem_zalloc'd chunk by
129  * the names pointed to by the structs.
130  */
131 
132 /*
133  * This (sub-)struct contains all the info extracted from the pm-components
134  * property for each component (name of component, names and values of power
135  * levels supported).  It is in a separate structure to allow it to be handled
136  * as a struct assignment.
137  */
138 typedef struct pm_comp {
139 	char	*pmc_name;		/* name of component */
140 	int	pmc_numlevels;		/* number of power levels supported */
141 	int	*pmc_lvals;		/* numerical values of levels */
142 	int	*pmc_thresh;		/* thresholds in secs, last INT_MAX */
143 	char	**pmc_lnames;		/* human readable names of levels */
144 	/*
145 	 * This part is just bookkeeping for the storage space involved above
146 	 * used for copying and freeing the struct members.  This because C
147 	 * is really an assembler at heart.
148 	 */
149 	size_t	pmc_name_sz;		/* size of name string		*/
150 	char	*pmc_lname_buf;		/* buffer holding *pmc_lnames	*/
151 	size_t	pmc_lnames_sz;		/* total size of pmc_lname_buf	*/
152 } pm_comp_t;
153 
154 /*
155  * Here we have the rest of what we need to know about a component.
156  */
157 typedef struct pm_component {
158 	uint_t pmc_flags;		/* flags this component */
159 	uint_t pmc_busycount;		/* for nesting busy calls */
160 	time_t pmc_timestamp;		/* timestamp */
161 	uint_t pmc_norm_pwr;		/* normal power index (or value) */
162 	int pmc_cur_pwr;		/* current power index (or value)  */
163 	int pmc_phc_pwr;		/* prev. value of curpwr (deadlock) */
164 	pm_comp_t pmc_comp;		/* component description */
165 } pm_component_t;
166 
167 /*
168  * All members of this struct are protected by PM_LOCK_DIP(dip).
169  *
170  * kidsupcnt counts (the number of components of new-style children at non-zero
171  * level (unknown counts as non-zero)) + (the number of old-style children with
172  * component 0 at non-zero level) for parents that have not asked for
173  * notification.  When kidsupcnt is 0 for a nexus node, then pm scans it,
174  * otherwise it leaves it alone.
175  * Parents that ask for notification always get get scanned,
176  * so we keep their kidsupcnt at zero.
177  */
178 typedef struct pm_info {
179 	uint_t		pmi_dev_pm_state; /* PM state of a device */
180 	int		pmi_clone;	/* owner for direct pm'd devs */
181 	int		pmi_levels[2];	/* storage space for 2 levels */
182 	int		*pmi_lp;	/* storage space for >2 levels */
183 	kcondvar_t	pmi_cv;		/* condvar for direct PM access */
184 } pm_info_t;
185 
186 /*
187  * Work request structure for the dependency processing thread.
188  */
189 typedef struct pm_dep_wk {
190 	int		pdw_type;		/* Type of request */
191 	int		pdw_wait;		/* caller waits for result */
192 	int		pdw_done;		/* set when req is done */
193 	int		pdw_ret;		/* return value to caller */
194 	int		pdw_pwr;		/* pwr level of keeper */
195 	kcondvar_t	pdw_cv;			/* cv to wake up caller */
196 	struct		pm_dep_wk *pdw_next;	/* next element */
197 	char		*pdw_keeper;
198 	char		*pdw_kept;
199 } pm_dep_wk_t;
200 
201 /*
202  * Types of work, depends on when it gets called:
203  */
204 #define	PM_DEP_WK_POWER_ON		1	/* power on */
205 #define	PM_DEP_WK_POWER_OFF		2	/* power off */
206 #define	PM_DEP_WK_DETACH		3	/* detach */
207 #define	PM_DEP_WK_REMOVE_DEP		4	/* dependency removed */
208 #define	PM_DEP_WK_BRINGUP_SELF		5	/* released from direct PM */
209 #define	PM_DEP_WK_RECORD_KEEPER		6	/* PM_ADD_DEPENDENT */
210 #define	PM_DEP_WK_RECORD_KEEPER_PROP	7	/* PM_ADD_DEPENDENT_PROP */
211 #define	PM_DEP_WK_KEPT			8	/* dep. work as a kept */
212 #define	PM_DEP_WK_KEEPER		9	/* dep. work as a keeper */
213 #define	PM_DEP_WK_ATTACH		10	/* when dip is attached */
214 #define	PM_DEP_WK_CHECK_KEPT		11	/* check if this is a kept */
215 #define	PM_DEP_WK_CPR_SUSPEND		12	/* Suspend dep. during CPR */
216 #define	PM_DEP_WK_CPR_RESUME		13	/* Resume dep. after CPR */
217 
218 /*
219  * Wait for dependency work to finish or not.
220  */
221 #define	PM_DEP_WAIT	1
222 #define	PM_DEP_NOWAIT	0
223 
224 typedef enum pm_canblock
225 {
226 	PM_CANBLOCK_BLOCK,	/* wait for controlling process action */
227 	PM_CANBLOCK_FAIL,	/* don't wait, fail request */
228 	PM_CANBLOCK_BYPASS	/* don't wait, ignore controlling process */
229 } pm_canblock_t;
230 
231 typedef enum pm_cpupm
232 {
233 	PM_CPUPM_NOTSET,	/* no specific treatment of CPU devices */
234 	PM_CPUPM_POLLING,	/* CPUPM enabled: polling mode */
235 	PM_CPUPM_EVENT,		/* CPUPM enabled: event driven mode */
236 	PM_CPUPM_DISABLE	/* do not power manage CPU devices */
237 } pm_cpupm_t;
238 
239 #define	PPM(dip) ((dev_info_t *)DEVI(dip)->devi_pm_ppm)
240 
241 /*
242  * The power request struct uses for the DDI_CTLOPS_POWER busctl.
243  *
244  * Note: When changing this enum it is necessary to maintain binary
245  * compatibility with older versions.  To insure that, add new values only
246  * at the end and refrain from deleting any existing values.
247  */
248 typedef enum {
249 	PMR_SET_POWER = 1,		/* called ddi_power (obsolete)	*/
250 	PMR_SUSPEND,			/* parental suspend		*/
251 	PMR_RESUME,			/* parental resume		*/
252 	PMR_PRE_SET_POWER,		/* parent's "pre" notification	*/
253 	PMR_POST_SET_POWER,		/* parent's "post" notification	*/
254 	PMR_PPM_SET_POWER,		/* platform pm set power	*/
255 	PMR_PPM_ATTACH,			/* ppm attach notify - unused	*/
256 	PMR_PPM_DETACH,			/* ppm detach notify - unused   */
257 	PMR_PPM_POWER_CHANGE_NOTIFY,	/* ppm level change notify	*/
258 	PMR_REPORT_PMCAP,		/* report pm capability		*/
259 	PMR_CHANGED_POWER,		/* parent's power_has_changed notif. */
260 	PMR_PPM_PRE_PROBE,		/* ppm pre probe notify		*/
261 	PMR_PPM_POST_PROBE,		/* ppm post probe notify	*/
262 	PMR_PPM_PRE_ATTACH,		/* ppm pre attach notify	*/
263 	PMR_PPM_POST_ATTACH,		/* ppm post pm attach notify	*/
264 	PMR_PPM_PRE_DETACH,		/* ppm pre pm detach notify	*/
265 	PMR_PPM_POST_DETACH,		/* ppm post pm detach notify	*/
266 	PMR_PPM_UNMANAGE,		/* device being unmanaged	*/
267 	PMR_PPM_PRE_RESUME,		/* ppm resume notify		*/
268 	PMR_PPM_ALL_LOWEST,		/* ppm all lowest power notify	*/
269 	PMR_PPM_LOCK_POWER,		/* ppm lock power		*/
270 	PMR_PPM_UNLOCK_POWER,		/* ppm unlock power		*/
271 	PMR_PPM_TRY_LOCK_POWER,		/* ppm try lock power		*/
272 	PMR_PPM_INIT_CHILD,		/* ppm init child notify	*/
273 	PMR_PPM_UNINIT_CHILD,		/* ppm uninit child notify	*/
274 	PMR_PPM_POWER_LOCK_OWNER,	/* ppm power lock owner's address */
275 	PMR_PPM_ENTER_SX,		/* ppm: enter ACPI S[2-4] state	*/
276 	PMR_PPM_EXIT_SX,		/* ppm: enter ACPI S[2-4] state	*/
277 	PMR_PPM_SEARCH_LIST		/* ppm: search tuple list	*/
278 } pm_request_type;
279 
280 /*
281  * When changing the elements of the union below it is necessary to
282  * maintain binary compatibility with older versions.  Refrain from
283  * deleting existing elements of the union or modifying their contents.
284  * Avoid increasing the total size of this structure if new elements
285  * must be added.
286  */
287 typedef struct power_req {
288 	pm_request_type request_type;
289 	union req {
290 		/*
291 		 * PMR_SET_POWER (obsolete)
292 		 */
293 		struct set_power_req {
294 			dev_info_t	*who;
295 			int		cmpt;
296 			int		level;
297 		} set_power_req;
298 		/*
299 		 * PMR_SUSPEND
300 		 */
301 		struct suspend_req {
302 			dev_info_t	*who;
303 			ddi_detach_cmd_t cmd;
304 		} suspend_req;
305 		/*
306 		 * PMR_PPM_PRE_RESUME or PMR_RESUME
307 		 */
308 		struct resume_req {
309 			dev_info_t	*who;
310 			ddi_attach_cmd_t cmd;
311 		} resume_req;
312 		/*
313 		 * PMR_PRE_SET_POWER
314 		 */
315 		struct pre_set_power_req {
316 			dev_info_t	*who;
317 			int		cmpt;
318 			int		old_level;
319 			int		new_level;
320 		} pre_set_power_req;
321 		/*
322 		 * PMR_POST_SET_POWER
323 		 */
324 		struct post_set_power_req {
325 			dev_info_t	*who;
326 			int		cmpt;
327 			int		old_level;
328 			int		new_level;
329 			int		result;		/* driver's return */
330 		} post_set_power_req;
331 		/*
332 		 * PMR_PPM_SET_POWER
333 		 */
334 		struct ppm_set_power_req {
335 			dev_info_t	*who;
336 			int		cmpt;
337 			int		old_level;
338 			int		new_level;
339 			pm_canblock_t	canblock;
340 			void		*cookie;
341 		} ppm_set_power_req;
342 		/*
343 		 * PMR_PPM_POWER_CHANGE_NOTIFY
344 		 */
345 		struct ppm_notify_level_req {
346 			dev_info_t	*who;
347 			int		cmpt;
348 			int		old_level;
349 			int		new_level;
350 		} ppm_notify_level_req;
351 		/*
352 		 * PMR_REPORT_PMCAP
353 		 */
354 		struct report_pmcap_req {
355 			dev_info_t	*who;
356 			int		cap;
357 			void		*arg;
358 		} report_pmcap_req;
359 		/*
360 		 * PMR_CHANGED_POWER
361 		 */
362 		struct changed_power_req {
363 			dev_info_t	*who;
364 			int		cmpt;
365 			int		old_level;
366 			int		new_level;
367 			int		result;
368 		} changed_power_req;
369 		/*
370 		 * PMR_PPM_PRE_PROBE, PMR_PPM_POST_PROBE, PMR_PPM_PRE_ATTACH,
371 		 * PMR_PPM_POST_ATTACH, PMR_PPM_PRE_DETACH, PMR_PPM_POST_DETACH
372 		 * PMR_PPM_INIT_CHILD, PMR_PPM_UNINIT_CHILD, or PMR_PPM_UNMANAGE
373 		 */
374 		struct ppm_config_req {
375 			dev_info_t	*who;
376 			int		result;		/* post only */
377 		} ppm_config_req;
378 		/*
379 		 * PMR_PPM_ALL_LOWEST
380 		 */
381 		struct ppm_all_lowest_req {
382 			int		mode;
383 		} ppm_all_lowest_req;
384 		/*
385 		 * PMR_PPM_LOCK_POWER, PMR_PPM_TRY_LOCK_POWER
386 		 */
387 		struct ppm_lock_power_req {
388 			dev_info_t	*who;
389 		} ppm_lock_power_req;
390 		/*
391 		 * PMR_PPM_UNLOCK_POWER
392 		 */
393 		struct ppm_unlock_power_req {
394 			dev_info_t	*who;
395 		} ppm_unlock_power_req;
396 		/*
397 		 * PMR_PPM_POWER_LOCK_OWNER
398 		 */
399 		struct ppm_power_lock_owner_req {
400 			dev_info_t	*who;
401 			kthread_t	*owner;
402 		} ppm_power_lock_owner_req;
403 		/*
404 		 * PMR_PPM_POWER_ENTER_SX
405 		 */
406 		struct ppm_power_enter_sx_req {
407 			int	sx_state;	/* S3, S4 */
408 			int	test_point;	/* test point */
409 			uint64_t wakephys;	/* restart vector phys addr */
410 			void *psr;		/* PSM (apic) state buffer */
411 		} ppm_power_enter_sx_req;
412 		/*
413 		 * PMR_PPM_SEARCH_LIST
414 		 */
415 		struct ppm_search_list {
416 			pm_searchargs_t *searchlist;
417 			int		result;
418 		} ppm_search_list_req;
419 	} req;
420 } power_req_t;
421 
422 #define	S3	3
423 #define	S4	4
424 
425 extern int cpr_test_point;
426 extern major_t cpr_device;
427 
428 #define	LOOP_BACK_NONE	0
429 #define	LOOP_BACK_PASS	1
430 #define	LOOP_BACK_FAIL	2
431 #define	FORCE_SUSPEND_TO_RAM	3
432 #define	DEVICE_SUSPEND_TO_RAM	4
433 
434 /*
435  * Struct passed as arg to appm_ioctl
436  */
437 typedef struct s3_args {
438 	int		s3a_state;	/* S3, S4 */
439 	int		s3a_test_point;	/* test point */
440 	uint64_t	s3a_wakephys;	/* restart vector physical addr */
441 	void		*s3a_psr;	/* apic state save buffer */
442 } s3a_t;
443 
444 /*
445  * Structure used by the following bus_power operations:
446  *
447  *	BUS_POWER_PRE_NOTIFICATION
448  *	BUS_POWER_POST_NOTIFICATION
449  *	BUS_POWER_CHILD_PWRCHG
450  */
451 typedef struct pm_bp_child_pwrchg {
452 	dev_info_t	*bpc_dip;	/* dip of the target device */
453 	char		*bpc_path;	/* path to the target device */
454 	int		bpc_comp;	/* component changing power */
455 	int		bpc_olevel;	/* old power level */
456 	int		bpc_nlevel;	/* new power level */
457 	void		*bpc_private;	/* PM framework private */
458 } pm_bp_child_pwrchg_t;
459 
460 /*
461  * Structure used by the BUS_POWER_NEXUS_PWRUP operation
462  */
463 typedef struct pm_bp_nexus_pwrup {
464 	dev_info_t	*bpn_dip;	/* dip of the nexus device */
465 	int		bpn_comp;	/* component powering up */
466 	int		bpn_level;	/* new power level */
467 	void		*bpn_private;	/* PM framework private */
468 } pm_bp_nexus_pwrup_t;
469 
470 /*
471  * Structure used by the BUS_POWER_HAS_CHANGED operation
472  */
473 typedef struct pm_bp_has_changed {
474 	dev_info_t	*bphc_dip;	/* dip of the target device */
475 	char		*bphc_path;	/* path to the target device */
476 	int		bphc_comp;	/* component changing power */
477 	int		bphc_olevel;	/* old power level */
478 	int		bphc_nlevel;	/* new power level */
479 	void		*bphc_private;	/* PM framework private */
480 } pm_bp_has_changed_t;
481 
482 /*
483  * Commands indicating which activity is requiring an
484  * update to the noinvol counters.
485  */
486 #define	PM_BP_NOINVOL_ATTACH	1
487 #define	PM_BP_NOINVOL_DETACH	2
488 #define	PM_BP_NOINVOL_REMDRV	3
489 #define	PM_BP_NOINVOL_CFB	4
490 #define	PM_BP_NOINVOL_POWER	5
491 
492 /*
493  * Structure used by the BUS_POWER_NOINVOL operation.
494  */
495 typedef struct pm_bp_noinvol {
496 	dev_info_t	*bpni_dip;	/* dip of the target device */
497 	char		*bpni_path;	/* path to the target device */
498 	int		bpni_cmd;	/* how to update the counters */
499 	int		bpni_volpmd;	/* volpmd of target device */
500 	int		bpni_wasvolpmd;	/* whether to update volpmd */
501 	void		*bpni_private;	/* PM framework private */
502 } pm_bp_noinvol_t;
503 
504 /*
505  * This struct is used by the code that makes a PMR_PPM_SET_POWER request
506  * to ppm. Devices that changed power other than the primary device (which
507  * was requested) are passed back to the pm framework through this
508  * structure.
509  */
510 typedef struct pm_ppm_devlist {
511 	dev_info_t	*ppd_who;
512 	int		ppd_cmpt;
513 	int		ppd_old_level;
514 	int		ppd_new_level;
515 	struct pm_ppm_devlist	*ppd_next;
516 } pm_ppm_devlist_t;
517 
518 /*
519  * This struct is used by the code that brings up parents and notifies
520  * ppm drivers across probe/attach/detach (pm_pre/post_probe/attach/detach())
521  */
522 typedef struct pm_ppm_cookie {
523 	dev_info_t		*ppc_dip;	/* dip of target node */
524 	dev_info_t		*ppc_pdip;	/* parent's dip */
525 	dev_info_t		*ppc_ppm;	/* interested ppm driver */
526 	int			ppc_cmd;	/* attach/detach cmd */
527 } pm_ppm_cookie_t;
528 
529 /*
530  * This struct records one dependency (a device keeps another or others up)
531  * pdr_size includes size of strings.
532  */
533 typedef struct pm_dep_rec {
534 	char *pdr_keeper;		/* physpath of device keeping up */
535 	char *pdr_kept;			/* physpath or property name */
536 	char **pdr_kept_paths;		/* array of kept devices' paths */
537 	struct pm_dep_rec *pdr_next;	/* next dependency device */
538 	size_t pdr_size;		/* size to kmem_free */
539 	major_t pdr_major;		/* major of kept driver (not props) */
540 	int pdr_isprop;			/* true if kept is property name */
541 	int pdr_kept_count;		/* how many kept altogether */
542 	int pdr_satisfied;		/* true if in force (not properties) */
543 } pm_pdr_t;
544 
545 
546 /*
547  * This struct records threshold information about a single component
548  */
549 typedef struct pm_thresh_entry {
550 	int pte_numthresh;
551 	int *pte_thresh;
552 } pm_pte_t;
553 
554 /*
555  * Note that this header and its array of entry structs with their arrays
556  * of thresholds and string storage for physpath are all kmem_alloced in one
557  * chunk for easy freeing ptr_size is the size of that chunk
558  */
559 typedef struct pm_thresh_rec {
560 	char			*ptr_physpath;	/* identifies node */
561 	struct pm_thresh_rec	*ptr_next;
562 	int			ptr_numcomps;	/* number of components */
563 	size_t			ptr_size;	/* total size for kmem_free */
564 	pm_pte_t		*ptr_entries;
565 } pm_thresh_rec_t;
566 
567 /*
568  * pmi_dev_pm_state state bits:
569  */
570 
571 /*
572  * a direct-pm device, not scanned, but controlled by a process
573  */
574 #define	PM_DIRECT	0x1
575 /*
576  * autopm is suspended while waiting to see if detach succeeds
577  */
578 #define	PM_DETACHING	0x2
579 
580 /*
581  * An all_to_normal operation for an autopm device that is detaching, is
582  * deferred in case the detach fails.
583  */
584 #define	PM_ALLNORM_DEFERRED	0x4
585 
586 #define	PM_GET_PM_INFO(dip) (DEVI(dip)->devi_pm_info)
587 #define	PM_GET_PM_SCAN(dip) (DEVI(dip)->devi_pm_scan)
588 
589 #define	PM_NUMCMPTS(dip) (DEVI(dip)->devi_pm_num_components)
590 #define	PM_CP(dip, comp) (&DEVI(dip)->devi_pm_components[comp])
591 
592 /*
593  * Returns true if the device specified by dip is directly power managed
594  */
595 #define	PM_ISDIRECT(dip) \
596 	(((pm_info_t *)PM_GET_PM_INFO(dip))->pmi_dev_pm_state & PM_DIRECT)
597 
598 /*
599  * Returns true if the device specified by dip is an old node for which we
600  * provide backwards compatible behavior (e.g. no pm-components property).
601  */
602 #define	PM_ISBC(dip) (DEVI(dip)->devi_pm_flags & PMC_BC)
603 
604 /*
605  * Returns true if we have skipped a dependency bringup on this dip.
606  */
607 #define	PM_SKBU(dip) (DEVI(dip)->devi_pm_flags & PMC_SKIP_BRINGUP)
608 
609 /*
610  * Returns true if device specified by dip is a power manageable CPU.
611  */
612 #define	PM_ISCPU(dip) (DEVI(dip)->devi_pm_flags & PMC_CPU_DEVICE)
613 
614 /*
615  * Returns true if cpupm is enabled in event driven mode.
616  */
617 #define	PM_EVENT_CPUPM (cpupm == PM_CPUPM_EVENT)
618 
619 /*
620  * Returns true if cpupm is enabled in polling mode.
621  */
622 #define	PM_POLLING_CPUPM (cpupm == PM_CPUPM_POLLING)
623 
624 /*
625  * Returns true if cpupm operating using the default mode.
626  */
627 #define	PM_DEFAULT_CPUPM (cpupm == cpupm_default_mode)
628 
629 /*
630  * Returns true if is disabled.
631  */
632 #define	PM_CPUPM_DISABLED (cpupm == PM_CPUPM_DISABLE)
633 
634 /*
635  * If ((autopm is enabled and
636  *	(CPUPM is not disabled and we're not in event mode, or it isn't a cpu))
637  *	  OR
638  *	(CPUPM are enabled and it is one))
639  */
640 #define	PM_SCANABLE(dip) ((autopm_enabled && \
641 	((!PM_CPUPM_DISABLED && !PM_EVENT_CPUPM) || !PM_ISCPU(dip))) || \
642 	(PM_POLLING_CPUPM && PM_ISCPU(dip)))
643 
644 #define	PM_NOT_ALL_LOWEST	0x0	/* not all components are at lowest */
645 #define	PM_ALL_LOWEST		0x1	/* all components are at lowest lvl */
646 
647 #define	PM_ADDR(dip)	(ddi_get_name_addr(dip) ? ddi_get_name_addr(dip) : "")
648 #define	PM_NAME(dip)	(ddi_binding_name(dip))
649 #define	PM_NODE(dip)	(ddi_node_name(dip))
650 #define	PM_INST(dip)	(ddi_get_instance(dip))
651 #define	PM_DEVICE(dip)	PM_NAME(dip), PM_ADDR(dip), PM_NODE(dip), PM_INST(dip)
652 
653 #ifdef	DEBUG
654 /*
655  * Flags passed to PMD to enable debug printfs.  If the same flag is set in
656  * pm_debug below then the message is printed.  The most generally useful
657  * ones are the first 3 or 4.
658  */
659 #define	PMD_ERROR	0x0000001
660 #define	PMD_FAIL	0x0000002
661 #define	PMD_IOCTL	0x0000004
662 #define	PMD_SCAN	0x0000008
663 #define	PMD_RESCAN	0x0000010
664 #define	PMD_REMINFO	0x0000020
665 #define	PMD_NAMETODIP	0x0000040
666 #define	PMD_CLOSE	0x0000080
667 #define	PMD_DIN		0x0000100	/* Dev Is Needed */
668 #define	PMD_PMC		0x0000200	/* for testing with sun4m pmc driver */
669 #define	PMD_PPM		0x0000400
670 #define	PMD_DEP		0x0000800	/* dependency processing */
671 #define	PMD_IDLEDOWN	0x0001000
672 #define	PMD_SET		0x0002000
673 #define	PMD_BRING	0x0004000
674 #define	PMD_ALLNORM	0x0008000
675 #define	PMD_REMDEV	0x0010000
676 #define	PMD_LEVEL	0x0020000
677 #define	PMD_THRESH	0x0040000
678 #define	PMD_DPM		0x0080000	/* Direct Power Management */
679 #define	PMD_NORM	0x0100000
680 #define	PMD_STATS	0x0200000
681 #define	PMD_DEREG	0x0400000
682 #define	PMD_KEEPS	0x0800000
683 #define	PMD_KIDSUP	0x1000000
684 #define	PMD_TCHECK	0x2000000
685 #define	PMD_NOINVOL	0x4000000
686 #define	PMD_CFB		0x8000000	/* console fb pm */
687 #define	PMD_DHR		0x10000000	/* driver hold/rele changes */
688 #define	PMD_PIL		0x20000000	/* print out PIL when calling power */
689 #define	PMD_PHC		0x40000000	/* pm_power_has_changed messages */
690 #define	PMD_LOCK	0x80000000
691 #define	PMD_SX		0x80000000	/* ACPI S[1234] states */
692 #define	PMD_PROTO	PMD_SX		/* and other Prototype stuff */
693 
694 extern uint_t	pm_debug;
695 extern uint_t	pm_divertdebug;
696 /*PRINTFLIKE1*/
697 extern void	pm_log(const char *fmt, ...) __KPRINTFLIKE(1);
698 
699 #if !defined(__sparc)
700 /*
701  * On non-sparc machines, PMDDEBUG isn't as big a deal as Sparc, so we
702  * define PMDDEUG here for use on non-sparc platforms.
703  */
704 #define	PMDDEBUG
705 #endif /* !__sparc */
706 
707 #ifdef PMDDEBUG
708 #define	PMD(level, arglist) {			\
709 	if (pm_debug & (level)) {		\
710 		pm_log arglist;			\
711 	}					\
712 }
713 #else /* !PMDDEBUG */
714 #define	PMD(level, arglist)	((void)0);
715 #endif /* PMDDEBUG */
716 #ifndef	sparc
717 extern clock_t pt_sleep;
718 /* code is char hex number to display on POST LED */
719 #define	PT(code) {outb(0x80, (char)code); drv_usecwait(pt_sleep); }
720 #else
721 #define	PT(code)
722 #endif
723 #else
724 #define	PMD(level, arglist)
725 #define	PT(code)
726 #endif
727 /*
728  * Code	Value	Indication
729  *
730  */
731 #define	PT_SPL7		0x01	/* pm_suspend spl7 */
732 #define	PT_PMSRET	0x02	/* pm_suspend returns */
733 #define	PT_PPMCTLOP	0x03	/* invoking ppm_ctlops */
734 #define	PT_ACPISDEV	0x04	/* acpi suspend devices */
735 #define	PT_IC		0x05	/* acpi intr_clear */
736 #define	PT_1to1		0x06	/* 1:1 mapping */
737 #define	PT_SC		0x07	/* save context */
738 #define	PT_SWV		0x08	/* set waking vector */
739 #define	PT_SWV_FAIL	0x09	/* set waking vector failed */
740 #define	PT_EWE		0x0a	/* enable wake events */
741 #define	PT_EWE_FAIL	0x0b	/* enable wake events failed */
742 #define	PT_RTCW		0x0c	/* setting rtc wakeup */
743 #define	PT_RTCW_FAIL	0x0d	/* setting rtc wakeup failed */
744 #define	PT_TOD		0x0e	/* setting tod */
745 #define	PT_SXP		0x0f	/* sx prep */
746 #define	PT_SXE		0x10	/* sx enter */
747 #define	PT_SXE_FAIL	0x11	/* sx enter failed */
748 #define	PT_INSOM	0x12	/* insomnia label */
749 #define	PT_WOKE		0x20	/* woke up */
750 #define	PT_UNDO1to1	0x21	/* Undo 1:1 mapping */
751 #define	PT_LSS		0x22	/* leave sleep state */
752 #define	PT_LSS_FAIL	0x23	/* leave sleep state failed */
753 #define	PT_CPB		0x24	/* clear power button event */
754 #define	PT_CPB_FAIL	0x25	/* clear power button event failed */
755 #define	PT_DRTC_FAIL	0x26	/* disable rtc fails */
756 #define	PT_ACPIREINIT	0x27	/* reinit apic */
757 #define	PT_ACPIRESTORE	0x28	/* restore apic */
758 #define	PT_INTRRESTORE	0x28	/* restore interrupts */
759 #define	PT_RESDEV	0x2a	/* ressume acpi devices */
760 #define	PT_CPU		0x2b	/* init_cpu_syscall */
761 #define	PT_PRESUME	0x30	/* pm_resume entered */
762 #define	PT_RSUS		0x31	/* pm_resume "suspended" */
763 #define	PT_RKERN	0x32	/* pm_resume "kernel" */
764 #define	PT_RDRV		0x33	/* pm_resume "driver" */
765 #define	PT_RDRV_FAIL	0x34	/* pm_resume "driver" failed */
766 #define	PT_RRNOINVOL	0x35	/* pm_resume "reattach_noinvol" */
767 #define	PT_RUSER	0x36	/* pm_resume "user" */
768 #define	PT_RAPMSIG	0x37	/* pm_resume APM/SRN signal */
769 #define	PT_RMPO		0x38	/* pm_resume "mp_online" */
770 #define	PT_RDONE	0x39	/* pm_resume done */
771 
772 extern void	pm_detaching(dev_info_t *);
773 extern void	pm_detach_failed(dev_info_t *);
774 extern int	pm_power(dev_info_t *, int, int);
775 extern int	pm_unmanage(dev_info_t *);
776 extern void	pm_rem_info(dev_info_t *);
777 extern int	pm_get_norm_pwrs(dev_info_t *, int **, size_t *);
778 extern dev_info_t *pm_name_to_dip(char *, int);
779 extern int	pm_power_up(dev_info_t *, int, int, int, pm_info_t *);
780 extern int	pm_default_idle_threshold;
781 extern void	pm_set_device_threshold(dev_info_t *, int, int);
782 extern int	pm_valid_power(dev_info_t *, int, int);
783 extern void	pm_lock_power(dev_info_t *);
784 extern void	pm_unlock_power(dev_info_t *);
785 extern int	pm_try_locking_power(dev_info_t *);
786 extern void	pm_lock_power_single(dev_info_t *);
787 extern void	pm_unlock_power_single(dev_info_t *);
788 extern int	pm_try_locking_power_single(dev_info_t *);
789 extern int	pm_isbc(dev_info_t *dip);
790 extern int	pm_isdirect(dev_info_t *dip);
791 extern int	pm_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t o,
792 			void *a, void *v);
793 extern int	pm_noinvol_detached(char *);
794 extern int	pm_init_child(dev_info_t *);
795 extern int	pm_uninit_child(dev_info_t *);
796 
797 extern int	pm_all_to_normal(dev_info_t *, pm_canblock_t);
798 extern int	pm_set_power(dev_info_t *, int, int, int, pm_canblock_t, int,
799 			int *);
800 extern void	pm_scan_init(dev_info_t *dip);
801 extern void	pm_scan_fini(dev_info_t *dip);
802 extern void	pm_scan_stop(dev_info_t *dip);
803 extern int	pm_scan_stop_walk(dev_info_t *dip, void *);
804 extern void	pm_scan(void *);
805 extern time_t	pm_scan_dev(dev_info_t *dip);
806 extern void	pm_rescan(void *);
807 extern int	pm_rescan_walk(dev_info_t *, void *);
808 extern void	pm_forget_power_level(dev_info_t *);
809 extern int	pm_pre_config(dev_info_t *, char *);
810 extern int	pm_pre_unconfig(dev_info_t *, int, int *, char *);
811 extern void	pm_post_config(dev_info_t *, char *);
812 extern void	pm_post_unconfig(dev_info_t *, int, char *);
813 extern void	pm_pre_probe(dev_info_t *, pm_ppm_cookie_t *);
814 extern void	pm_post_probe(pm_ppm_cookie_t *, int, int);
815 extern void	pm_post_attach(pm_ppm_cookie_t *, int);
816 extern void	pm_pre_attach(dev_info_t *, pm_ppm_cookie_t *,
817 			ddi_attach_cmd_t);
818 extern void	pm_pre_detach(dev_info_t *, ddi_detach_cmd_t,
819 			pm_ppm_cookie_t *);
820 extern void	pm_post_detach(pm_ppm_cookie_t *, int);
821 extern int	pm_powerup(dev_info_t *);
822 extern int	pm_all_at_normal(dev_info_t *);
823 extern int	pm_busop_bus_power(dev_info_t *, void *,
824 		    pm_bus_power_op_t, void *, void *);
825 extern void	pm_hold_power(dev_info_t *);
826 extern void	pm_rele_power(dev_info_t *);
827 extern void	pm_driver_removed(major_t);
828 extern void	pm_borrow_lock(kthread_t *);
829 extern void	pm_return_lock(void);
830 extern int	pm_reattach_noinvol(void);
831 extern void	pm_reattach_noinvol_fini();
832 extern void	pm_restore_direct_levels(void);
833 extern void	pm_save_direct_levels(void);
834 extern void	pm_cfb_setup(const char *);
835 extern void	pm_proceed(dev_info_t *, int, int, int);
836 extern void	pm_get_timestamps(dev_info_t *, time_t *);
837 extern void	pm_deregister_watcher(int, dev_info_t *);
838 extern void	pm_dispatch_to_dep_thread(int, char *, char *, int, int *, int);
839 extern int	e_pm_valid_comp(dev_info_t *, int, pm_component_t **);
840 extern int	e_pm_valid_info(dev_info_t *, pm_info_t **);
841 extern int	e_pm_valid_power(dev_info_t *, int, int);
842 extern void	pm_init_locks(void);
843 extern int	pm_register_ppm(int (*)(dev_info_t *), dev_info_t *);
844 extern int	pm_is_cfb(dev_info_t *);
845 #ifdef	DEBUG
846 extern int	pm_cfb_is_up(void);
847 #endif
848 
849 #ifdef DIPLOCKDEBUG
850 #define	PM_LOCK_DIP(dip)	{ PMD(PMD_LOCK, ("dip lock %s@%s(%s#%d) " \
851 				    "%s %d\n", PM_DEVICE(dip),		  \
852 				    __FILE__, __LINE__))		  \
853 				    mutex_enter(&DEVI(dip)->devi_pm_lock); }
854 #define	PM_UNLOCK_DIP(dip)	{ PMD(PMD_LOCK, ("dip unlock %s@%s(%s#%d) " \
855 				    "%s %d\n", PM_DEVICE(dip),		    \
856 				    __FILE__, __LINE__))		    \
857 				    mutex_exit(&DEVI(dip)->devi_pm_lock); }
858 #else
859 #define	PM_LOCK_DIP(dip)	mutex_enter(&DEVI(dip)->devi_pm_lock)
860 #define	PM_UNLOCK_DIP(dip)	mutex_exit(&DEVI(dip)->devi_pm_lock)
861 #endif
862 
863 /*
864  * These are the same DEBUG or not
865  */
866 #define	PM_LOCK_BUSY(dip)	mutex_enter(&DEVI(dip)->devi_pm_busy_lock)
867 #define	PM_UNLOCK_BUSY(dip)	mutex_exit(&DEVI(dip)->devi_pm_busy_lock)
868 #define	PM_LOCK_POWER(dip)	pm_lock_power(dip)
869 #define	PM_UNLOCK_POWER(dip)	pm_unlock_power(dip)
870 #define	PM_TRY_LOCK_POWER(dip)	pm_try_locking_power(dip)
871 #define	PM_IAM_LOCKING_DIP(dip)	(mutex_owned(&DEVI(dip)->devi_pm_lock))
872 
873 #define	PM_DEFAULT_SYS_IDLENESS	1800	/* 30 minutes */
874 
875 /*
876  * Codes put into the pr_retval field of pm_rsvp_t that tell pm_block()
877  * how to proceed
878  */
879 #define	PMP_SUCCEED	0x1	/* return success, the process did it */
880 #define	PMP_FAIL	0x2	/* return fail, process did something else */
881 #define	PMP_RELEASE	0x3	/* let it go, the process has lost interest */
882 				/* also arg to pm_proceed to signal this */
883 /*
884  * Values of "style" for e_pm_manage and pm_premanage
885  */
886 #define	PM_STYLE_NEW		0
887 #define	PM_STYLE_UNKNOWN	1
888 
889 /*
890  * Arg passed to pm_proceed that results in PMP_SUCCEED or PMP_FAIL being set
891  * in pr_retval depending on what is pending
892  */
893 #define	PMP_SETPOWER	0x4
894 
895 #define	PM_MAX_CLONE	256
896 
897 typedef struct pm_rsvp {
898 	dev_info_t	*pr_dip;
899 	int		pr_comp;
900 	int		pr_newlevel;
901 	int		pr_oldlevel;
902 	kcondvar_t	pr_cv;		/* a place to sleep */
903 	int		pr_retval;	/* what to do when you wake up */
904 	struct pm_rsvp	*pr_next;
905 	struct pm_rsvp	*pr_prev;
906 } pm_rsvp_t;
907 
908 typedef struct psce {	/* pm_state_change_entries */
909 	struct pm_state_change		*psce_first;
910 	struct pm_state_change		*psce_in;
911 	struct pm_state_change		*psce_out;
912 	struct pm_state_change		*psce_last;
913 	int				psce_overruns;
914 	int				psce_references;
915 	kmutex_t			psce_lock;
916 } psce_t;
917 
918 typedef struct pscc {			/* pm_state_change_control */
919 	int		pscc_clone;
920 	dev_info_t	*pscc_dip;
921 	psce_t		*pscc_entries;
922 	struct pscc	*pscc_next;
923 	struct pscc	*pscc_prev;
924 } pscc_t;
925 
926 #define	PSCCOUNT 128	/* number of state change entries kept per process */
927 
928 /*
929  * Struct used to track the existence of devices exporting the
930  * no-involuntary-power-cycles property, and remember things from their
931  * devinfo node for later attach.
932  */
933 typedef struct pm_noinvol {
934 	struct pm_noinvol	*ni_next;
935 	char			*ni_path;
936 	major_t			ni_major;	/* for attaching at cpr time */
937 	uint_t			ni_flags;	/* selected PMC_* values */
938 	uint_t			ni_noinvolpm;	/* saved noinvolpm count */
939 	uint_t			ni_volpmd;	/* saved volpmd count */
940 	uint_t			ni_wasvolpmd;	/* was vol pm'd at detach */
941 	size_t			ni_size;
942 	int			ni_persistent;	/* still around */
943 } pm_noinvol_t;
944 
945 #define	PMID_IOCTIMER		0x1		/* pm_ioctl sets during timer */
946 #define	PMID_CFBTIMER		0x2		/* cfb sets during timer */
947 #define	PMID_IOCSCAN		0x4		/* pm_ioctl sets during scan */
948 #define	PMID_CFBSCAN		0x8		/* cfb sets during scan */
949 
950 #define	PMID_IOC		(PMID_IOCTIMER | PMID_IOCSCAN)
951 #define	PMID_CFB		(PMID_CFBTIMER | PMID_CFBSCAN)
952 #define	PMID_TIMERS		(PMID_IOCTIMER | PMID_CFBTIMER)
953 #define	PMID_SCANS		(PMID_IOCSCAN | PMID_CFBSCAN)
954 #define	PMID_SCANS_SHIFT	2
955 #define	PMID_SET_SCANS(pmid)	(pmid) |= (((pmid) & PMID_TIMERS) <<	\
956 				    PMID_SCANS_SHIFT);
957 #define	PMID_IS_IOC(pmid)	((pmid) & PMID_IOC)
958 #define	PMID_IS_CFB(pmid, dip)	(((pmid) & PMID_CFB) &&			\
959 				    (DEVI(dip)->devi_pm_flags &		\
960 				    (PMC_DEF_THRESH | PMC_NEXDEF_THRESH)))
961 #define	PM_IS_PID(dip)	(PMID_IS_IOC(PM_GET_PM_SCAN(dip)->ps_idle_down) || \
962 	PMID_IS_CFB(PM_GET_PM_SCAN(dip)->ps_idle_down, dip))
963 #define	PM_IS_CFB(dip)		(DEVI(dip)->devi_pm_flags & PMC_CONSOLE_FB)
964 #define	PM_KUC(dip)		(DEVI(dip)->devi_pm_kidsupcnt)
965 #define	PM_CURPOWER(dip, comp)	cur_power(PM_CP(dip, comp))
966 
967 #define	PM_WANTS_NOTIFICATION(dip)					\
968 	(DEVI(dip)->devi_pm_flags & PMC_WANTS_NOTIFY)
969 
970 #define	PM_HAS_BUS_POWER(dip)						\
971 	((DEVI(dip)->devi_ops->devo_bus_ops != NULL) &&			\
972 	(DEVI(dip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_7) &&\
973 	(DEVI(dip)->devi_ops->devo_bus_ops->bus_power != NULL))
974 
975 #define	PM_BUS_POWER_FUNC(dip)						\
976 	DEVI(dip)->devi_ops->devo_bus_ops->bus_power
977 
978 /*
979  * Structure used to pass down sunpm's private data variables
980  * through the bus_power bus_op calls
981  */
982 typedef struct pm_sp_misc {
983 	pm_canblock_t   pspm_canblock;
984 	int pspm_scan;
985 	int *pspm_errnop;
986 	int pspm_direction;
987 } pm_sp_misc_t;
988 
989 /*
990  * This structure is used in validating that the power level
991  * of the descendents are off, while a device is powered off.
992  */
993 typedef struct pm_desc_pwrchk {
994 	dev_info_t *pdpc_dip;
995 	int pdpc_par_involved;
996 } pm_desc_pwrchk_t;
997 
998 
999 /*
1000  * These defines are used by pm_trans_check() to calculate time.
1001  * Mostly copied from "tzfile.h".
1002  */
1003 #define	DC_SPY			(SECSPERDAY * DAYSPERNYEAR)
1004 #define	DC_SPW			(SECSPERDAY * DAYSPERWEEK)
1005 #define	DC_SPD			SECSPERDAY
1006 
1007 #define	DC_SCSI_YEAR_LEN	4		/* YYYY */
1008 #define	DC_SCSI_WEEK_LEN	2		/* WW */
1009 #define	DC_SCSI_NPY		5		/* # power-cycle years */
1010 
1011 #endif	/* _KERNEL */
1012 
1013 #ifdef	__cplusplus
1014 }
1015 #endif
1016 
1017 #endif /* _SYS_EPM_H */
1018