]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/pwr_mgt/RootDomain.h
61334b1cda8aab3db9611f2a1505fa6f831e6575
[apple/xnu.git] / iokit / IOKit / pwr_mgt / RootDomain.h
1 /*
2 * Copyright (c) 1998-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifndef _IOKIT_ROOTDOMAIN_H
29 #define _IOKIT_ROOTDOMAIN_H
30
31 #include <IOKit/IOService.h>
32 #include <IOKit/pwr_mgt/IOPM.h>
33 #include <IOKit/IOBufferMemoryDescriptor.h>
34 #include <sys/vnode.h>
35
36 #ifdef XNU_KERNEL_PRIVATE
37
38 #include <IOKit/pwr_mgt/IOPMPrivate.h>
39
40 struct AggressivesRecord;
41 struct IOPMMessageFilterContext;
42 struct IOPMActions;
43 struct IOPMSystemSleepParameters;
44 class PMSettingObject;
45 class PMTraceWorker;
46 class IOPMPowerStateQueue;
47 class RootDomainUserClient;
48 class PMAssertionsTracker;
49 class IOTimerEventSource;
50
51 #define OBFUSCATE(x) (void *)VM_KERNEL_UNSLIDE_OR_PERM(x)
52
53 #endif
54
55 /*!
56 * Types for PM Assertions
57 * For creating, releasing, and getting PM assertion levels.
58 */
59
60 /*! IOPMDriverAssertionType
61 * A bitfield describing a set of assertions. May be used to specify which assertions
62 * to set with <link>IOPMrootDomain::createPMAssertion</link>; or to query which
63 * assertions are set with <link>IOPMrootDomain::releasePMAssertion</link>.
64 */
65 typedef uint64_t IOPMDriverAssertionType;
66
67 /* IOPMDriverAssertionID
68 * Drivers may create PM assertions to request system behavior (keep the system awake,
69 * or keep the display awake). When a driver creates an assertion via
70 * <link>IOPMrootDomain::createPMAssertion</link>, PM returns a handle to
71 * the assertion of type IOPMDriverAssertionID.
72 */
73 typedef uint64_t IOPMDriverAssertionID;
74 #define kIOPMUndefinedDriverAssertionID 0
75
76 /* IOPMDriverAssertionLevel
77 * Possible values for IOPMDriverAssertionLevel are <link>kIOPMDriverAssertionLevelOff</link>
78 * and <link>kIOPMDriverAssertionLevelOn</link>
79 */
80 typedef uint32_t IOPMDriverAssertionLevel;
81 #define kIOPMDriverAssertionLevelOff 0
82 #define kIOPMDriverAssertionLevelOn 255
83
84 /*
85 * Flags for get/setSleepSupported()
86 */
87 enum {
88 kRootDomainSleepNotSupported = 0x00000000,
89 kRootDomainSleepSupported = 0x00000001,
90 kFrameBufferDeepSleepSupported = 0x00000002,
91 kPCICantSleep = 0x00000004
92 };
93
94 /*
95 * IOPMrootDomain registry property keys
96 */
97 #define kRootDomainSupportedFeatures "Supported Features"
98 #define kRootDomainSleepReasonKey "Last Sleep Reason"
99 #define kRootDomainSleepOptionsKey "Last Sleep Options"
100 #define kIOPMRootDomainWakeReasonKey "Wake Reason"
101 #define kIOPMRootDomainWakeTypeKey "Wake Type"
102 #define kIOPMRootDomainPowerStatusKey "Power Status"
103
104 /*
105 * Possible sleep reasons found under kRootDomainSleepReasonsKey
106 */
107 #define kIOPMClamshellSleepKey "Clamshell Sleep"
108 #define kIOPMPowerButtonSleepKey "Power Button Sleep"
109 #define kIOPMSoftwareSleepKey "Software Sleep"
110 #define kIOPMOSSwitchHibernationKey "OS Switch Sleep"
111 #define kIOPMIdleSleepKey "Idle Sleep"
112 #define kIOPMLowPowerSleepKey "Low Power Sleep"
113 #define kIOPMThermalEmergencySleepKey "Thermal Emergency Sleep"
114 #define kIOPMMaintenanceSleepKey "Maintenance Sleep"
115
116 /*
117 * String constants for communication with PM CPU
118 */
119 #define kIOPMRootDomainLidCloseCString "LidClose"
120 #define kIOPMRootDomainBatPowerCString "BatPower"
121
122 /*
123 * Supported Feature bitfields for IOPMrootDomain::publishFeature()
124 */
125 enum {
126 kIOPMSupportedOnAC = (1 << 0),
127 kIOPMSupportedOnBatt = (1 << 1),
128 kIOPMSupportedOnUPS = (1 << 2)
129 };
130
131 typedef IOReturn (*IOPMSettingControllerCallback)
132 (OSObject *target, const OSSymbol *type,
133 OSObject *val, uintptr_t refcon);
134
135 __BEGIN_DECLS
136 IONotifier * registerSleepWakeInterest(
137 IOServiceInterestHandler, void *, void * = NULL);
138
139 IONotifier * registerPrioritySleepWakeInterest(
140 IOServiceInterestHandler handler,
141 void * self, void * ref = NULL);
142
143 IOReturn acknowledgeSleepWakeNotification(void * );
144
145 IOReturn vetoSleepWakeNotification(void * PMrefcon);
146 __END_DECLS
147
148 #define IOPM_ROOTDOMAIN_REV 2
149
150 class IOPMrootDomain : public IOService
151 {
152 OSDeclareFinalStructors(IOPMrootDomain);
153
154 public:
155 static IOPMrootDomain * construct( void );
156
157 virtual bool start( IOService * provider ) APPLE_KEXT_OVERRIDE;
158 virtual IOReturn setAggressiveness( unsigned long, unsigned long ) APPLE_KEXT_OVERRIDE;
159 virtual IOReturn getAggressiveness( unsigned long, unsigned long * ) APPLE_KEXT_OVERRIDE;
160
161 virtual IOReturn sleepSystem( void );
162 IOReturn sleepSystemOptions( OSDictionary *options );
163
164 virtual IOReturn setProperties( OSObject * ) APPLE_KEXT_OVERRIDE;
165 virtual bool serializeProperties( OSSerialize * s ) const APPLE_KEXT_OVERRIDE;
166 virtual OSObject * copyProperty( const char * aKey ) const APPLE_KEXT_OVERRIDE;
167
168 /*! @function systemPowerEventOccurred
169 * @abstract Other drivers may inform IOPMrootDomain of system PM events
170 * @discussion systemPowerEventOccurred is a richer alternative to receivePowerNotification()
171 * Only Apple-owned kexts should have reason to call systemPowerEventOccurred.
172 * @param event An OSSymbol describing the type of power event.
173 * @param intValue A 32-bit integer value associated with the event.
174 * @result kIOReturnSuccess on success */
175
176 IOReturn systemPowerEventOccurred(
177 const OSSymbol *event,
178 uint32_t intValue );
179
180 IOReturn systemPowerEventOccurred(
181 const OSSymbol *event,
182 OSObject *value );
183
184 #ifdef XNU_KERNEL_PRIVATE // Hide doc from public headers
185 /*! @function claimSystemWakeEvent
186 * @abstract Apple-internal SPI to describe system wake events.
187 * @discussion IOKit drivers may call claimSystemWakeEvent() during system wakeup to
188 * provide human readable debug information describing the event(s) that
189 * caused the system to wake.
190 *
191 * - Drivers should call claimSystemWakeEvent before completing
192 * their setPowerState() acknowledgement. IOPMrootDomain stops
193 * collecting wake events when driver wake is complete.
194 *
195 * - It is only appropriate to claim a wake event when the driver
196 * can positively identify its hardware has generated an event
197 * that can wake the system.
198 *
199 * - This call tracks wake events from a non-S0 state (S0i, S3, S4) into S0.
200 * - This call does not track wake events from DarkWake(S0) to FullWake(S0).
201 *
202 * Examples:
203 * (reason = "WiFi.TCPData",
204 * details = "TCPKeepAlive packet arrived from IP 16.2.1.1")
205 * (reason = "WiFi.ScanOffload",
206 * details = "WiFi station 'AppleWiFi' signal dropped below threshold")
207 * (reason = "Enet.LinkToggle",
208 * details = "Ethernet attached")
209 *
210 * @param device The device/nub that is associated with the wake event.
211 *
212 * @param flags Pass kIOPMWakeEventSource if the device is the source
213 * of the wake event. Pass zero if the device is forwarding or
214 * aggregating wake events from multiple sources, e.g. an USB or
215 * Thunderbolt host controller.
216 *
217 * @param reason Caller should pass a human readable C string describing the
218 * wake reason. Please use a string from the list below, or create
219 * your own string matching this format:
220 * [Hardware].[Event]
221 * WiFi.MagicPacket
222 * WiFi.ScanOffload
223 * WiFi.mDNSConflict
224 * WiFi.mDNSService
225 * WiFi.TCPData
226 * WiFi.TCPTimeout
227 * WiFi.FirmwareCrash
228 * Enet.MagicPacket
229 * Enet.mDNSConflict
230 * Enet.mDNSService
231 * Enet.TCPData
232 * Enet.TCPTimeout
233 * Enet.Service
234 * Enet.LinkToggle
235 * Enet.ConflictResolution
236 * Enet.PatternMatch
237 * Enet.Timer
238 * Enet.LinkUpTimeout
239 * Enet.LinkDown
240 * USB.DeviceAttach
241 * USB.DeviceDetach
242 *
243 * @param details Optional details further describing the wake event.
244 * Please pass an OSString defining the event.
245 */
246 #endif
247 void claimSystemWakeEvent( IOService *device,
248 IOOptionBits flags,
249 const char *reason,
250 OSObject *details = NULL );
251
252 virtual IOReturn receivePowerNotification( UInt32 msg );
253
254 virtual void setSleepSupported( IOOptionBits flags );
255
256 virtual IOOptionBits getSleepSupported( void );
257
258 void wakeFromDoze( void );
259
260 // KEXT driver announces support of power management feature
261
262 void publishFeature( const char *feature );
263
264 // KEXT driver announces support of power management feature
265 // And specifies power sources with kIOPMSupportedOn{AC/Batt/UPS} bitfield.
266 // Returns a unique uint32_t identifier for later removing support for this
267 // feature.
268 // NULL is acceptable for uniqueFeatureID for kexts without plans to unload.
269
270 void publishFeature( const char *feature,
271 uint32_t supportedWhere,
272 uint32_t *uniqueFeatureID);
273
274 // KEXT driver announces removal of a previously published power management
275 // feature. Pass 'uniqueFeatureID' returned from publishFeature()
276
277 IOReturn removePublishedFeature( uint32_t removeFeatureID );
278
279 /*! @function copyPMSetting
280 * @abstract Copy the current value for a PM setting. Returns an OSNumber or
281 * OSData depending on the setting.
282 * @param whichSetting Name of the desired setting.
283 * @result OSObject value if valid, NULL otherwise. */
284
285 OSObject * copyPMSetting( OSSymbol *whichSetting );
286
287 /*! @function registerPMSettingController
288 * @abstract Register for callbacks on changes to certain PM settings.
289 * @param settings NULL terminated array of C strings, each string for a PM
290 * setting that the caller is interested in and wants to get callbacks for.
291 * @param callout C function ptr or member function cast as such.
292 * @param target The target of the callback, usually 'this'
293 * @param refcon Will be passed to caller in callback; for caller's use.
294 * @param handle Caller should keep the OSObject * returned here. If non-NULL,
295 * handle will have a retain count of 1 on return. To deregister, pass to
296 * unregisterPMSettingController()
297 * @result kIOReturnSuccess on success. */
298
299 IOReturn registerPMSettingController(
300 const OSSymbol *settings[],
301 IOPMSettingControllerCallback callout,
302 OSObject *target,
303 uintptr_t refcon,
304 OSObject **handle); // out param
305
306 /*! @function registerPMSettingController
307 * @abstract Register for callbacks on changes to certain PM settings.
308 * @param settings NULL terminated array of C strings, each string for a PM
309 * setting that the caller is interested in and wants to get callbacks for.
310 * @param supportedPowerSources bitfield indicating which power sources these
311 * settings are supported for (kIOPMSupportedOnAC, etc.)
312 * @param callout C function ptr or member function cast as such.
313 * @param target The target of the callback, usually 'this'
314 * @param refcon Will be passed to caller in callback; for caller's use.
315 * @param handle Caller should keep the OSObject * returned here. If non-NULL,
316 * handle will have a retain count of 1 on return. To deregister, pass to
317 * unregisterPMSettingController()
318 * @result kIOReturnSuccess on success. */
319
320 IOReturn registerPMSettingController(
321 const OSSymbol *settings[],
322 uint32_t supportedPowerSources,
323 IOPMSettingControllerCallback callout,
324 OSObject *target,
325 uintptr_t refcon,
326 OSObject **handle); // out param
327
328 virtual IONotifier * registerInterest(
329 const OSSymbol * typeOfInterest,
330 IOServiceInterestHandler handler,
331 void * target, void * ref = NULL ) APPLE_KEXT_OVERRIDE;
332
333 virtual IOReturn callPlatformFunction(
334 const OSSymbol *functionName,
335 bool waitForFunction,
336 void *param1, void *param2,
337 void *param3, void *param4 ) APPLE_KEXT_OVERRIDE;
338
339 /*! @function createPMAssertion
340 * @abstract Creates an assertion to influence system power behavior.
341 * @param whichAssertionsBits A bitfield specify the assertion that the caller requests.
342 * @param assertionLevel An integer detailing the initial assertion level, kIOPMDriverAssertionLevelOn
343 * or kIOPMDriverAssertionLevelOff.
344 * @param ownerService A pointer to the caller's IOService class, for tracking.
345 * @param ownerDescription A reverse-DNS string describing the caller's identity and reason.
346 * @result On success, returns a new assertion of type IOPMDriverAssertionID
347 */
348 IOPMDriverAssertionID createPMAssertion(
349 IOPMDriverAssertionType whichAssertionsBits,
350 IOPMDriverAssertionLevel assertionLevel,
351 IOService *ownerService,
352 const char *ownerDescription);
353
354 /* @function setPMAssertionLevel
355 * @abstract Modify the level of a pre-existing assertion.
356 * @discussion Change the value of a PM assertion to influence system behavior,
357 * without undergoing the work required to create or destroy an assertion. Suggested
358 * for clients who will assert and de-assert needs for PM behavior several times over
359 * their lifespan.
360 * @param assertionID An assertion ID previously returned by <link>createPMAssertion</link>
361 * @param assertionLevel The new assertion level.
362 * @result kIOReturnSuccess if it worked; kIOReturnNotFound or other IOReturn error on failure.
363 */
364 IOReturn setPMAssertionLevel(IOPMDriverAssertionID assertionID, IOPMDriverAssertionLevel assertionLevel);
365
366 /*! @function getPMAssertionLevel
367 * @absract Returns the active level of the specified assertion(s).
368 * @discussion Returns <link>kIOPMDriverAssertionLevelOff</link> or
369 * <link>kIOPMDriverAssertionLevelOn</link>. If multiple assertions are specified
370 * in the bitfield, only returns <link>kIOPMDriverAssertionLevelOn</link>
371 * if all assertions are active.
372 * @param whichAssertionBits Bits defining the assertion or assertions the caller is interested in
373 * the level of. If in doubt, pass <link>kIOPMDriverAssertionCPUBit</link> as the argument.
374 * @result Returns <link>kIOPMDriverAssertionLevelOff</link> or
375 * <link>kIOPMDriverAssertionLevelOn</link> indicating the specified assertion's levels, if available.
376 * If the assertions aren't supported on this machine, or aren't recognized by the OS, the
377 * result is undefined.
378 */
379 IOPMDriverAssertionLevel getPMAssertionLevel(IOPMDriverAssertionType whichAssertionBits);
380
381 /*! @function releasePMAssertion
382 * @abstract Removes an assertion to influence system power behavior.
383 * @result On success, returns a new assertion of type IOPMDriverAssertionID *
384 */
385 IOReturn releasePMAssertion(IOPMDriverAssertionID releaseAssertion);
386
387 /*! @function restartWithStackshot
388 * @abstract Take a stackshot of the system and restart the system.
389 * @result Return kIOReturnSuccess if it work, kIOReturnError if the service is not available.
390 */
391 IOReturn restartWithStackshot();
392
393 IOReturn setWakeTime(uint64_t wakeContinuousTime);
394
395 private:
396 unsigned long getRUN_STATE(void);
397
398 virtual IOReturn changePowerStateTo( unsigned long ordinal ) APPLE_KEXT_COMPATIBILITY_OVERRIDE;
399 virtual IOReturn changePowerStateToPriv( unsigned long ordinal );
400 virtual IOReturn requestPowerDomainState( IOPMPowerFlags, IOPowerConnection *, unsigned long ) APPLE_KEXT_OVERRIDE;
401 virtual void powerChangeDone( unsigned long ) APPLE_KEXT_OVERRIDE;
402 virtual bool tellChangeDown( unsigned long ) APPLE_KEXT_OVERRIDE;
403 virtual bool askChangeDown( unsigned long ) APPLE_KEXT_OVERRIDE;
404 virtual void tellChangeUp( unsigned long ) APPLE_KEXT_OVERRIDE;
405 virtual void tellNoChangeDown( unsigned long ) APPLE_KEXT_OVERRIDE;
406 virtual IOReturn configureReport(IOReportChannelList *channels,
407 IOReportConfigureAction action,
408 void *result,
409 void *destination) APPLE_KEXT_OVERRIDE;
410 virtual IOReturn updateReport(IOReportChannelList *channels,
411 IOReportUpdateAction action,
412 void *result,
413 void *destination) APPLE_KEXT_OVERRIDE;
414
415 void configureReportGated(uint64_t channel_id,
416 uint64_t action,
417 void *result);
418 IOReturn updateReportGated(uint64_t ch_id,
419 void *result,
420 IOBufferMemoryDescriptor *dest);
421
422 #ifdef XNU_KERNEL_PRIVATE
423 /* Root Domain internals */
424 public:
425 void tagPowerPlaneService(
426 IOService * service,
427 IOPMActions * actions );
428
429 void overrideOurPowerChange(
430 IOService * service,
431 IOPMActions * actions,
432 IOPMPowerStateIndex * inOutPowerState,
433 IOPMPowerChangeFlags * inOutChangeFlags,
434 IOPMRequestTag requestTag );
435
436 void handleOurPowerChangeStart(
437 IOService * service,
438 IOPMActions * actions,
439 IOPMPowerStateIndex powerState,
440 IOPMPowerChangeFlags * inOutChangeFlags,
441 IOPMRequestTag requestTag );
442
443 void handleOurPowerChangeDone(
444 IOService * service,
445 IOPMActions * actions,
446 IOPMPowerStateIndex powerState,
447 IOPMPowerChangeFlags changeFlags,
448 IOPMRequestTag requestTag );
449
450 void overridePowerChangeForUIService(
451 IOService * service,
452 IOPMActions * actions,
453 IOPMPowerStateIndex * inOutPowerState,
454 IOPMPowerChangeFlags * inOutChangeFlags );
455
456 void handleActivityTickleForDisplayWrangler(
457 IOService * service,
458 IOPMActions * actions );
459
460 void handleUpdatePowerClientForDisplayWrangler(
461 IOService * service,
462 IOPMActions * actions,
463 const OSSymbol * powerClient,
464 IOPMPowerStateIndex oldPowerState,
465 IOPMPowerStateIndex newPowerState );
466
467 bool shouldDelayChildNotification(
468 IOService * service );
469
470 void handlePowerChangeStartForPCIDevice(
471 IOService * service,
472 IOPMActions * actions,
473 IOPMPowerStateIndex powerState,
474 IOPMPowerChangeFlags * inOutChangeFlags );
475
476 void handlePowerChangeDoneForPCIDevice(
477 IOService * service,
478 IOPMActions * actions,
479 IOPMPowerStateIndex powerState,
480 IOPMPowerChangeFlags changeFlags );
481
482 void askChangeDownDone(
483 IOPMPowerChangeFlags * inOutChangeFlags,
484 bool * cancel );
485
486 void handlePublishSleepWakeUUID(
487 bool shouldPublish);
488
489 void handleQueueSleepWakeUUID(
490 OSObject *obj);
491
492 void handleDisplayPowerOn();
493
494 void willNotifyPowerChildren( IOPMPowerStateIndex newPowerState );
495
496 IOReturn setMaintenanceWakeCalendar(
497 const IOPMCalendarStruct * calendar );
498
499 IOReturn getSystemSleepType(uint32_t * sleepType, uint32_t * standbyTimer);
500
501 // Handle callbacks from IOService::systemWillShutdown()
502 void acknowledgeSystemWillShutdown( IOService * from );
503
504 // Handle platform halt and restart notifications
505 void handlePlatformHaltRestart( UInt32 pe_type );
506
507 IOReturn shutdownSystem( void );
508 IOReturn restartSystem( void );
509 void handleSleepTimerExpiration( void );
510
511 bool activitySinceSleep(void);
512 bool abortHibernation(void);
513 void updateConsoleUsers(void);
514
515 IOReturn joinAggressiveness( IOService * service );
516 void handleAggressivesRequests( void );
517
518 void kdebugTrace(uint32_t event, uint64_t regId,
519 uintptr_t param1, uintptr_t param2, uintptr_t param3 = 0);
520 void tracePoint(uint8_t point);
521 void traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay);
522 void traceDetail(OSObject *notifier, bool start);
523 void traceAckDelay(OSObject *notifier, uint32_t response, uint32_t delay_ms);
524
525 void startSpinDump(uint32_t spindumpKind);
526
527 bool systemMessageFilter(
528 void * object, void * arg1, void * arg2, void * arg3 );
529
530 bool updatePreventIdleSleepList(
531 IOService * service, bool addNotRemove );
532 void updatePreventSystemSleepList(
533 IOService * service, bool addNotRemove );
534
535 bool updatePreventIdleSleepListInternal(
536 IOService * service, bool addNotRemove, unsigned int oldCount);
537 unsigned int idleSleepPreventersCount();
538
539 void publishPMSetting(
540 const OSSymbol * feature, uint32_t where, uint32_t * featureID );
541
542 void pmStatsRecordEvent(
543 int eventIndex,
544 AbsoluteTime timestamp);
545
546 void pmStatsRecordApplicationResponse(
547 const OSSymbol *response,
548 const char *name,
549 int messageType,
550 uint32_t delay_ms,
551 uint64_t id,
552 OSObject *object,
553 IOPMPowerStateIndex ps = 0);
554
555 void copyWakeReasonString( char * outBuf, size_t bufSize );
556
557 #if HIBERNATION
558 bool getHibernateSettings(
559 uint32_t * hibernateMode,
560 uint32_t * hibernateFreeRatio,
561 uint32_t * hibernateFreeTime );
562 bool mustHibernate( void );
563 #endif
564 void takeStackshot(bool restart);
565 void sleepWakeDebugTrig(bool restart);
566 void sleepWakeDebugEnableWdog();
567 bool sleepWakeDebugIsWdogEnabled();
568 void sleepWakeDebugSaveSpinDumpFile();
569 bool checkShutdownTimeout();
570 void panicWithShutdownLog(uint32_t timeoutInMs) __abortlike;
571 uint32_t getWatchdogTimeout();
572 void deleteStackshot();
573
574 private:
575 friend class PMSettingObject;
576 friend class RootDomainUserClient;
577 friend class PMAssertionsTracker;
578
579 static IOReturn sysPowerDownHandler( void * target, void * refCon,
580 UInt32 messageType, IOService * service,
581 void * messageArgument, vm_size_t argSize );
582
583 static IOReturn displayWranglerNotification( void * target, void * refCon,
584 UInt32 messageType, IOService * service,
585 void * messageArgument, vm_size_t argSize );
586
587 static IOReturn rootBusyStateChangeHandler( void * target, void * refCon,
588 UInt32 messageType, IOService * service,
589 void * messageArgument, vm_size_t argSize );
590
591 static bool displayWranglerMatchPublished( void * target, void * refCon,
592 IOService * newService,
593 IONotifier * notifier);
594
595 static bool batteryPublished( void * target, void * refCon,
596 IOService * resourceService,
597 IONotifier * notifier);
598
599 void initializeBootSessionUUID( void );
600
601 void fullWakeDelayedWork( void );
602
603 IOService * wrangler;
604 OSDictionary * wranglerIdleSettings;
605
606 IOLock *featuresDictLock;// guards supportedFeatures
607 IOLock *wakeEventLock;
608 IOPMPowerStateQueue *pmPowerStateQueue;
609
610 OSArray *allowedPMSettings;
611 OSArray *noPublishPMSettings;
612 PMTraceWorker *pmTracer;
613 PMAssertionsTracker *pmAssertions;
614
615 // Settings controller info
616 IOLock *settingsCtrlLock;
617 OSDictionary *settingsCallbacks;
618 OSDictionary *fPMSettingsDict;
619
620 IONotifier *_batteryPublishNotifier;
621 IONotifier *_displayWranglerNotifier;
622
623 // Statistics
624 const OSSymbol *_statsNameKey;
625 const OSSymbol *_statsPIDKey;
626 const OSSymbol *_statsTimeMSKey;
627 const OSSymbol *_statsResponseTypeKey;
628 const OSSymbol *_statsMessageTypeKey;
629 const OSSymbol *_statsPowerCapsKey;
630 uint32_t sleepCnt;
631 uint32_t darkWakeCnt;
632 uint32_t displayWakeCnt;
633
634 OSString *queuedSleepWakeUUIDString;
635 OSArray *pmStatsAppResponses;
636 IOLock *pmStatsLock;// guards pmStatsAppResponses
637
638 void *sleepDelaysReport; // report to track time taken to go to sleep
639 uint32_t sleepDelaysClientCnt;// Number of interested clients in sleepDelaysReport
640 uint64_t ts_sleepStart;
641 uint64_t wake2DarkwakeDelay; // Time taken to change from full wake -> Dark wake
642
643
644 void *assertOnWakeReport;// report to track time spent without any assertions held after wake
645 uint32_t assertOnWakeClientCnt;// Number of clients interested in assertOnWakeReport
646 clock_sec_t assertOnWakeSecs; // Num of secs after wake for first assertion
647
648 bool uuidPublished;
649
650 // Pref: idle time before idle sleep
651 bool idleSleepEnabled;
652 unsigned long sleepSlider;
653 unsigned long idleSeconds;
654 uint64_t autoWakeStart;
655 uint64_t autoWakeEnd;
656
657 // Difference between sleepSlider and longestNonSleepSlider
658 unsigned long extraSleepDelay;
659
660 // Used to wait between say display idle and system idle
661 thread_call_t extraSleepTimer;
662 thread_call_t powerButtonDown;
663 thread_call_t powerButtonUp;
664 thread_call_t diskSyncCalloutEntry;
665 thread_call_t fullWakeThreadCall;
666 thread_call_t updateConsoleUsersEntry;
667
668 // Track system capabilities.
669 uint32_t _desiredCapability;
670 uint32_t _currentCapability;
671 uint32_t _pendingCapability;
672 uint32_t _highestCapability;
673 OSSet * _joinedCapabilityClients;
674 uint32_t _systemStateGeneration;
675
676 // Type of clients that can receive system messages.
677 enum {
678 kSystemMessageClientPowerd = 0x01,
679 kSystemMessageClientLegacyApp = 0x02,
680 kSystemMessageClientKernel = 0x04,
681 kSystemMessageClientAll = 0x07
682 };
683 uint32_t _systemMessageClientMask;
684
685 // Power state and capability change transitions.
686 enum {
687 kSystemTransitionNone = 0,
688 kSystemTransitionSleep = 1,
689 kSystemTransitionWake = 2,
690 kSystemTransitionCapability = 3,
691 kSystemTransitionNewCapClient = 4
692 } _systemTransitionType;
693
694 unsigned int systemBooting :1;
695 unsigned int systemShutdown :1;
696 unsigned int systemDarkWake :1;
697 unsigned int clamshellExists :1;
698 unsigned int clamshellClosed :1;
699 unsigned int clamshellDisabled :1;
700 unsigned int desktopMode :1;
701 unsigned int acAdaptorConnected :1;
702
703 unsigned int clamshellSleepDisabled :1;
704 unsigned int idleSleepTimerPending :1;
705 unsigned int userDisabledAllSleep :1;
706 unsigned int ignoreTellChangeDown :1;
707 unsigned int wranglerAsleep :1;
708 unsigned int wranglerTickled :1;
709 unsigned int _preventUserActive :1;
710 unsigned int graphicsSuppressed :1;
711 unsigned int isRTCAlarmWake :1;
712
713 unsigned int capabilityLoss :1;
714 unsigned int pciCantSleepFlag :1;
715 unsigned int pciCantSleepValid :1;
716 unsigned int logGraphicsClamp :1;
717 unsigned int darkWakeToSleepASAP :1;
718 unsigned int darkWakeMaintenance :1;
719 unsigned int darkWakeSleepService :1;
720 unsigned int darkWakePostTickle :1;
721
722 unsigned int sleepTimerMaintenance :1;
723 unsigned int sleepToStandby :1;
724 unsigned int lowBatteryCondition :1;
725 unsigned int hibernateDisabled :1;
726 unsigned int hibernateRetry :1;
727 unsigned int wranglerTickleLatched :1;
728 unsigned int userIsActive :1;
729 unsigned int userWasActive :1;
730
731 unsigned int displayIdleForDemandSleep :1;
732 unsigned int darkWakeHibernateError :1;
733 unsigned int thermalWarningState:1;
734 unsigned int toldPowerdCapWillChange :1;
735 unsigned int displayPowerOnRequested:1;
736
737 uint8_t tasksSuspended;
738 uint8_t tasksSuspendState;
739 uint32_t hibernateMode;
740 AbsoluteTime userActivityTime;
741 AbsoluteTime userActivityTime_prev;
742 uint32_t userActivityCount;
743 uint32_t userActivityAtSleep;
744 uint32_t lastSleepReason;
745 uint32_t fullToDarkReason;
746 uint32_t hibernateAborted;
747 uint8_t standbyNixed;
748 uint8_t resetTimers;
749
750 enum FullWakeReason {
751 kFullWakeReasonNone = 0,
752 kFullWakeReasonLocalUser = 1,
753 kFullWakeReasonDisplayOn = 2,
754 fFullWakeReasonDisplayOnAndLocalUser = 3
755 };
756 uint32_t fullWakeReason;
757
758 // Info for communicating system state changes to PMCPU
759 int32_t idxPMCPUClamshell;
760 int32_t idxPMCPULimitedPower;
761
762 IOOptionBits platformSleepSupport;
763 uint32_t _debugWakeSeconds;
764 uint32_t _lastDebugWakeSeconds;
765
766 queue_head_t aggressivesQueue;
767 thread_call_t aggressivesThreadCall;
768 OSData * aggressivesData;
769
770 AbsoluteTime userBecameInactiveTime;
771
772 // PCI top-level PM trace
773 IOService * pciHostBridgeDevice;
774 IOService * pciHostBridgeDriver;
775
776 IONotifier * systemCapabilityNotifier;
777
778 typedef struct {
779 uint32_t pid;
780 uint32_t refcount;
781 } PMNotifySuspendedStruct;
782
783 uint32_t pmSuspendedCapacity;
784 uint32_t pmSuspendedSize;
785 PMNotifySuspendedStruct *pmSuspendedPIDS;
786
787 OSSet * preventIdleSleepList;
788 OSSet * preventSystemSleepList;
789
790 UInt32 _scheduledAlarms;
791 UInt32 _userScheduledAlarm;
792 clock_sec_t _scheduledAlarmUTC;
793
794 #if HIBERNATION
795 clock_sec_t _standbyTimerResetSeconds;
796 #endif
797 volatile uint32_t swd_lock;/* Lock to access swd_buffer & and its header */
798 void * swd_buffer;/* Memory allocated for dumping sleep/wake logs */
799 uint32_t swd_flags;/* Flags defined in IOPMPrivate.h */
800 void * swd_compressed_buffer;
801 void * swd_spindump_buffer;
802 thread_t notifierThread;
803 OSObject *notifierObject;
804
805 IOBufferMemoryDescriptor *swd_memDesc;
806
807 // Wake Event Reporting
808 OSArray * _systemWakeEventsArray;
809 bool _acceptSystemWakeEvents;
810
811 #if !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146))
812 // AOT --
813 IOPMCalendarStruct _aotWakeTimeCalendar;
814 IOTimerEventSource * _aotTimerES;
815 clock_sec_t _aotWakeTimeUTC;
816 uint64_t _aotTestTime;
817 uint64_t _aotTestInterval;
818 uint32_t _aotPendingFlags;
819 public:
820 IOPMAOTMetrics * _aotMetrics;
821 uint8_t _aotMode;
822 private:
823 uint8_t _aotNow;
824 uint8_t _aotTasksSuspended;
825 uint8_t _aotExit;
826 uint8_t _aotTimerScheduled;
827 uint8_t _aotReadyToFullWake;
828 uint64_t _aotLastWakeTime;
829 uint64_t _aotWakeTimeContinuous;
830 uint64_t _aotWakePreWindow;
831 uint64_t _aotWakePostWindow;
832 uint64_t _aotLingerTime;
833
834 bool aotShouldExit(bool checkTimeSet, bool software);
835 void aotExit(bool cps);
836 void aotEvaluate(IOTimerEventSource * timer);
837 public:
838 bool isAOTMode(void);
839 private:
840 // -- AOT
841 #endif /* !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146)) */
842
843 void updateTasksSuspend(void);
844 int findSuspendedPID(uint32_t pid, uint32_t *outRefCount);
845
846 // IOPMrootDomain internal sleep call
847 IOReturn privateSleepSystem( uint32_t sleepReason );
848 void reportUserInput( void );
849 void setDisableClamShellSleep( bool );
850 bool checkSystemSleepAllowed( IOOptionBits options,
851 uint32_t sleepReason );
852 bool checkSystemSleepEnabled( void );
853 bool checkSystemCanSleep( uint32_t sleepReason );
854 bool checkSystemCanSustainFullWake( void );
855
856 void adjustPowerState( bool sleepASAP = false );
857 void setQuickSpinDownTimeout( void );
858 void restoreUserSpinDownTimeout( void );
859
860 bool shouldSleepOnClamshellClosed(void );
861 bool shouldSleepOnRTCAlarmWake(void );
862 void sendClientClamshellNotification( void );
863
864 // Inform PMCPU of changes to state like lid, AC vs. battery
865 void informCPUStateChange( uint32_t type, uint32_t value );
866
867 void dispatchPowerEvent( uint32_t event, void * arg0, uint64_t arg1 );
868 void handlePowerNotification( UInt32 msg );
869
870 IOReturn setPMSetting(const OSSymbol *, OSObject *);
871
872 void startIdleSleepTimer( uint32_t inSeconds );
873 void cancelIdleSleepTimer( void );
874 uint32_t getTimeToIdleSleep( void );
875
876 IOReturn setAggressiveness(
877 unsigned long type,
878 unsigned long value,
879 IOOptionBits options );
880
881 void synchronizeAggressives(
882 queue_head_t * services,
883 const AggressivesRecord * array,
884 int count );
885
886 void broadcastAggressives(
887 const AggressivesRecord * array,
888 int count );
889
890 IOReturn setPMAssertionUserLevels(IOPMDriverAssertionType);
891
892 void publishSleepWakeUUID( bool shouldPublish );
893
894 void evaluatePolicy( int stimulus, uint32_t arg = 0 );
895 void requestFullWake( FullWakeReason reason );
896 void willEnterFullWake( void );
897
898 void evaluateAssertions(IOPMDriverAssertionType newAssertions,
899 IOPMDriverAssertionType oldAssertions);
900
901 void deregisterPMSettingObject( PMSettingObject * pmso );
902
903 uint32_t checkForValidDebugData(const char *fname, vfs_context_t *ctx,
904 void *tmpBuf, struct vnode **vp);
905 void getFailureData(thread_t *thread, char *failureStr, size_t strLen);
906 void saveFailureData2File();
907 void tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description);
908 void sleepWakeDebugMemAlloc();
909 void sleepWakeDebugSpinDumpMemAlloc();
910 errno_t sleepWakeDebugSaveFile(const char *name, char *buf, int len);
911
912
913 #if HIBERNATION
914 bool getSleepOption( const char * key, uint32_t * option );
915 bool evaluateSystemSleepPolicy( IOPMSystemSleepParameters * p,
916 int phase, uint32_t * hibMode );
917 void evaluateSystemSleepPolicyEarly( void );
918 void evaluateSystemSleepPolicyFinal( void );
919 #endif /* HIBERNATION */
920
921 bool latchDisplayWranglerTickle( bool latch );
922 void setDisplayPowerOn( uint32_t options );
923
924 void acceptSystemWakeEvents( bool accept );
925 void systemDidNotSleep( void );
926 void preventTransitionToUserActive( bool prevent );
927 void setThermalState(OSObject *value);
928 void copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList);
929 void copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList);
930 #endif /* XNU_KERNEL_PRIVATE */
931 };
932
933 #ifdef XNU_KERNEL_PRIVATE
934 class IORootParent : public IOService
935 {
936 OSDeclareFinalStructors(IORootParent);
937
938 public:
939 static void initialize( void );
940 virtual OSObject * copyProperty( const char * aKey ) const APPLE_KEXT_OVERRIDE;
941 bool start( IOService * nub ) APPLE_KEXT_OVERRIDE;
942 void shutDownSystem( void );
943 void restartSystem( void );
944 void sleepSystem( void );
945 void dozeSystem( void );
946 void sleepToDoze( void );
947 void wakeSystem( void );
948 };
949 #endif /* XNU_KERNEL_PRIVATE */
950
951 #endif /* _IOKIT_ROOTDOMAIN_H */