1 /*
2 * Copyright (c) 1998-2021 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
29 #define IOKIT_ENABLE_SHARED_PTR
30
31 #include <libkern/c++/OSAllocation.h>
32 #include <libkern/c++/OSKext.h>
33 #include <libkern/c++/OSMetaClass.h>
34 #include <libkern/OSAtomic.h>
35 #include <libkern/OSDebug.h>
36 #include <IOKit/IOWorkLoop.h>
37 #include <IOKit/IOCommandGate.h>
38 #include <IOKit/IOTimerEventSource.h>
39 #include <IOKit/IOPlatformExpert.h>
40 #include <IOKit/IOCPU.h>
41 #include <IOKit/IOPlatformActions.h>
42 #include <IOKit/IOKitDebug.h>
43 #include <IOKit/IOTimeStamp.h>
44 #include <IOKit/pwr_mgt/IOPMlog.h>
45 #include <IOKit/pwr_mgt/RootDomain.h>
46 #include <IOKit/pwr_mgt/IOPMPrivate.h>
47 #include <IOKit/IODeviceTreeSupport.h>
48 #include <IOKit/IOMessage.h>
49 #include <IOKit/IOReturn.h>
50 #include <IOKit/IONVRAM.h>
51 #include "RootDomainUserClient.h"
52 #include "IOKit/pwr_mgt/IOPowerConnection.h"
53 #include "IOPMPowerStateQueue.h"
54 #include <IOKit/IOCatalogue.h>
55 #include <IOKit/IOReportMacros.h>
56 #include <IOKit/IOLib.h>
57 #include <IOKit/IOKitKeys.h>
58 #include <IOKit/IOUserServer.h>
59 #include "IOKitKernelInternal.h"
60 #if HIBERNATION
61 #include <IOKit/IOHibernatePrivate.h>
62 #endif /* HIBERNATION */
63 #include <machine/machine_routines.h>
64 #include <console/video_console.h>
65 #include <sys/syslog.h>
66 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/fcntl.h>
70 #include <os/log.h>
71 #include <pexpert/protos.h>
72 #include <AssertMacros.h>
73
74 #include <sys/time.h>
75 #include "IOServicePrivate.h" // _IOServiceInterestNotifier
76 #include "IOServicePMPrivate.h"
77
78 #include <libkern/zlib.h>
79 #include <os/cpp_util.h>
80 #include <os/atomic_private.h>
81 #include <libkern/c++/OSBoundedArrayRef.h>
82
83 #if DEVELOPMENT || DEBUG
84 #include <os/system_event_log.h>
85 #endif /* DEVELOPMENT || DEBUG */
86
87 __BEGIN_DECLS
88 #include <mach/shared_region.h>
89 #include <kern/clock.h>
90 #include <vm/vm_pageout_xnu.h>
91 __END_DECLS
92
93 #if defined(__i386__) || defined(__x86_64__)
94 __BEGIN_DECLS
95 #include "IOPMrootDomainInternal.h"
96 const char *processor_to_datastring(const char *prefix, processor_t target_processor);
97 __END_DECLS
98 #endif
99
100 #define kIOPMrootDomainClass "IOPMrootDomain"
101 #define LOG_PREFIX "PMRD: "
102
103
104 #define MSG(x...) \
105 do { kprintf(LOG_PREFIX x); IOLog(x); } while (false)
106
107 #define LOG(x...) \
108 do { kprintf(LOG_PREFIX x); } while (false)
109
110 #if DEVELOPMENT || DEBUG
111 #define DEBUG_LOG(x...) do { \
112 if (kIOLogPMRootDomain & gIOKitDebug) \
113 kprintf(LOG_PREFIX x); \
114 os_log_debug(OS_LOG_DEFAULT, LOG_PREFIX x); \
115 } while (false)
116 #else
117 #define DEBUG_LOG(x...)
118 #endif
119
120 #define DLOG(x...) do { \
121 if (kIOLogPMRootDomain & gIOKitDebug) \
122 IOLog(LOG_PREFIX x); \
123 else \
124 os_log(OS_LOG_DEFAULT, LOG_PREFIX x); \
125 } while (false)
126
127 #define DMSG(x...) do { \
128 if (kIOLogPMRootDomain & gIOKitDebug) { \
129 kprintf(LOG_PREFIX x); \
130 } \
131 } while (false)
132
133
134 #define _LOG(x...)
135
136 #define CHECK_THREAD_CONTEXT
137 #ifdef CHECK_THREAD_CONTEXT
138 static IOWorkLoop * gIOPMWorkLoop = NULL;
139 #define ASSERT_GATED() \
140 do { \
141 if (gIOPMWorkLoop && gIOPMWorkLoop->inGate() != true) { \
142 panic("RootDomain: not inside PM gate"); \
143 } \
144 } while(false)
145 #else
146 #define ASSERT_GATED()
147 #endif /* CHECK_THREAD_CONTEXT */
148
149 #define CAP_LOSS(c) \
150 (((_pendingCapability & (c)) == 0) && \
151 ((_currentCapability & (c)) != 0))
152
153 #define CAP_GAIN(c) \
154 (((_currentCapability & (c)) == 0) && \
155 ((_pendingCapability & (c)) != 0))
156
157 #define CAP_CHANGE(c) \
158 (((_currentCapability ^ _pendingCapability) & (c)) != 0)
159
160 #define CAP_CURRENT(c) \
161 ((_currentCapability & (c)) != 0)
162
163 #define CAP_HIGHEST(c) \
164 ((_highestCapability & (c)) != 0)
165
166 #define CAP_PENDING(c) \
167 ((_pendingCapability & (c)) != 0)
168
169 // rdar://problem/9157444
170 #if defined(__i386__) || defined(__x86_64__)
171 #define DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY 20
172 #endif
173
174 // Event types for IOPMPowerStateQueue::submitPowerEvent()
175 enum {
176 kPowerEventFeatureChanged = 1, // 1
177 kPowerEventReceivedPowerNotification, // 2
178 kPowerEventSystemBootCompleted, // 3
179 kPowerEventSystemShutdown, // 4
180 kPowerEventUserDisabledSleep, // 5
181 kPowerEventRegisterSystemCapabilityClient, // 6
182 kPowerEventRegisterKernelCapabilityClient, // 7
183 kPowerEventPolicyStimulus, // 8
184 kPowerEventAssertionCreate, // 9
185 kPowerEventAssertionRelease, // 10
186 kPowerEventAssertionSetLevel, // 11
187 kPowerEventQueueSleepWakeUUID, // 12
188 kPowerEventPublishSleepWakeUUID, // 13
189 kPowerEventSetDisplayPowerOn, // 14
190 kPowerEventPublishWakeType, // 15
191 kPowerEventAOTEvaluate // 16
192 };
193
194 // For evaluatePolicy()
195 // List of stimuli that affects the root domain policy.
196 enum {
197 kStimulusDisplayWranglerSleep, // 0
198 kStimulusDisplayWranglerWake, // 1
199 kStimulusAggressivenessChanged, // 2
200 kStimulusDemandSystemSleep, // 3
201 kStimulusAllowSystemSleepChanged, // 4
202 kStimulusDarkWakeActivityTickle, // 5
203 kStimulusDarkWakeEntry, // 6
204 kStimulusDarkWakeReentry, // 7
205 kStimulusDarkWakeEvaluate, // 8
206 kStimulusNoIdleSleepPreventers, // 9
207 kStimulusEnterUserActiveState, // 10
208 kStimulusLeaveUserActiveState // 11
209 };
210
211 // Internal power state change reasons
212 // Must be less than kIOPMSleepReasonClamshell=101
213 enum {
214 kCPSReasonNone = 0, // 0
215 kCPSReasonInit, // 1
216 kCPSReasonWake, // 2
217 kCPSReasonIdleSleepPrevent, // 3
218 kCPSReasonIdleSleepAllow, // 4
219 kCPSReasonPowerOverride, // 5
220 kCPSReasonPowerDownCancel, // 6
221 kCPSReasonAOTExit, // 7
222 kCPSReasonAdjustPowerState, // 8
223 kCPSReasonDarkWakeCannotSleep, // 9
224 kCPSReasonIdleSleepEnabled, // 10
225 kCPSReasonEvaluatePolicy, // 11
226 kCPSReasonSustainFullWake, // 12
227 kCPSReasonPMInternals = (kIOPMSleepReasonClamshell - 1)
228 };
229
230 extern "C" {
231 IOReturn OSKextSystemSleepOrWake( UInt32 );
232 }
233 extern "C" ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
234 extern "C" addr64_t kvtophys(vm_offset_t va);
235 extern "C" boolean_t kdp_has_polled_corefile();
236
237 static void idleSleepTimerExpired( thread_call_param_t, thread_call_param_t );
238 static void notifySystemShutdown( IOService * root, uint32_t messageType );
239 static void handleAggressivesFunction( thread_call_param_t, thread_call_param_t );
240 static void pmEventTimeStamp(uint64_t *recordTS);
241 static void powerButtonUpCallout( thread_call_param_t, thread_call_param_t );
242 static void powerButtonDownCallout( thread_call_param_t, thread_call_param_t );
243 static OSPtr<const OSSymbol> copyKextIdentifierWithAddress(vm_address_t address);
244
245 static int IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt);
246 static clock_sec_t IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt);
247 #define YMDTF "%04d/%02d/%d %02d:%02d:%02d"
248 #define YMDT(cal) ((int)(cal)->year), (cal)->month, (cal)->day, (cal)->hour, (cal)->minute, (cal)->second
249
250 // "IOPMSetSleepSupported" callPlatformFunction name
251 static OSSharedPtr<const OSSymbol> sleepSupportedPEFunction;
252 static OSSharedPtr<const OSSymbol> sleepMessagePEFunction;
253 static OSSharedPtr<const OSSymbol> gIOPMWakeTypeUserKey;
254
255 static OSSharedPtr<const OSSymbol> gIOPMPSExternalConnectedKey;
256 static OSSharedPtr<const OSSymbol> gIOPMPSExternalChargeCapableKey;
257 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryInstalledKey;
258 static OSSharedPtr<const OSSymbol> gIOPMPSIsChargingKey;
259 static OSSharedPtr<const OSSymbol> gIOPMPSAtWarnLevelKey;
260 static OSSharedPtr<const OSSymbol> gIOPMPSAtCriticalLevelKey;
261 static OSSharedPtr<const OSSymbol> gIOPMPSCurrentCapacityKey;
262 static OSSharedPtr<const OSSymbol> gIOPMPSMaxCapacityKey;
263 static OSSharedPtr<const OSSymbol> gIOPMPSDesignCapacityKey;
264 static OSSharedPtr<const OSSymbol> gIOPMPSTimeRemainingKey;
265 static OSSharedPtr<const OSSymbol> gIOPMPSAmperageKey;
266 static OSSharedPtr<const OSSymbol> gIOPMPSVoltageKey;
267 static OSSharedPtr<const OSSymbol> gIOPMPSCycleCountKey;
268 static OSSharedPtr<const OSSymbol> gIOPMPSMaxErrKey;
269 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterInfoKey;
270 static OSSharedPtr<const OSSymbol> gIOPMPSLocationKey;
271 static OSSharedPtr<const OSSymbol> gIOPMPSErrorConditionKey;
272 static OSSharedPtr<const OSSymbol> gIOPMPSManufacturerKey;
273 static OSSharedPtr<const OSSymbol> gIOPMPSManufactureDateKey;
274 static OSSharedPtr<const OSSymbol> gIOPMPSModelKey;
275 static OSSharedPtr<const OSSymbol> gIOPMPSSerialKey;
276 static OSSharedPtr<const OSSymbol> gIOPMPSLegacyBatteryInfoKey;
277 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryHealthKey;
278 static OSSharedPtr<const OSSymbol> gIOPMPSHealthConfidenceKey;
279 static OSSharedPtr<const OSSymbol> gIOPMPSCapacityEstimatedKey;
280 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryChargeStatusKey;
281 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryTemperatureKey;
282 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsKey;
283 static OSSharedPtr<const OSSymbol> gIOPMPSChargerConfigurationKey;
284 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsIDKey;
285 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsWattsKey;
286 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsRevisionKey;
287 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSerialNumberKey;
288 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsFamilyKey;
289 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsAmperageKey;
290 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsDescriptionKey;
291 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsPMUConfigurationKey;
292 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSourceIDKey;
293 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsErrorFlagsKey;
294 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSharedSourceKey;
295 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsCloakedKey;
296 static OSSharedPtr<const OSSymbol> gIOPMPSInvalidWakeSecondsKey;
297 static OSSharedPtr<const OSSymbol> gIOPMPSPostChargeWaitSecondsKey;
298 static OSSharedPtr<const OSSymbol> gIOPMPSPostDishargeWaitSecondsKey;
299
300 #define kIOSleepSupportedKey "IOSleepSupported"
301 #define kIOPMSystemCapabilitiesKey "System Capabilities"
302 #define kIOPMSystemDefaultOverrideKey "SystemPowerProfileOverrideDict"
303
304 #define kIORequestWranglerIdleKey "IORequestIdle"
305 #define kDefaultWranglerIdlePeriod 1000 // in milliseconds
306
307 #define kIOSleepWakeFailureString "SleepWakeFailureString"
308 #define kIOEFIBootRomFailureKey "wake-failure"
309 #define kIOSleepWakeFailurePanic "SleepWakeFailurePanic"
310
311 #define kRD_AllPowerSources (kIOPMSupportedOnAC \
312 | kIOPMSupportedOnBatt \
313 | kIOPMSupportedOnUPS)
314
315 #define kLocalEvalClamshellCommand (1 << 15)
316 #define kIdleSleepRetryInterval (3 * 60 * 1000)
317
318 // Minimum time in milliseconds after AP wake that we allow idle timer to expire.
319 // We impose this minimum to avoid race conditions in the AP wake path where
320 // userspace clients are not able to acquire power assertions before the idle timer expires.
321 #define kMinimumTimeBeforeIdleSleep 1000
322
323 #define DISPLAY_WRANGLER_PRESENT (!NO_KERNEL_HID)
324
325 enum {
326 kWranglerPowerStateMin = 0,
327 kWranglerPowerStateSleep = 2,
328 kWranglerPowerStateDim = 3,
329 kWranglerPowerStateMax = 4
330 };
331
332 enum {
333 OFF_STATE = 0,
334 RESTART_STATE = 1,
335 SLEEP_STATE = 2,
336 AOT_STATE = 3,
337 ON_STATE = 4,
338 NUM_POWER_STATES
339 };
340
341 const char *
getPowerStateString(uint32_t state)342 getPowerStateString( uint32_t state )
343 {
344 #define POWER_STATE(x) {(uint32_t) x, #x}
345
346 static const IONamedValue powerStates[] = {
347 POWER_STATE( OFF_STATE ),
348 POWER_STATE( RESTART_STATE ),
349 POWER_STATE( SLEEP_STATE ),
350 POWER_STATE( AOT_STATE ),
351 POWER_STATE( ON_STATE ),
352 { 0, NULL }
353 };
354 return IOFindNameForValue(state, powerStates);
355 }
356
357 #define ON_POWER kIOPMPowerOn
358 #define RESTART_POWER kIOPMRestart
359 #define SLEEP_POWER kIOPMAuxPowerOn
360
361 static IOPMPowerState
362 ourPowerStates[NUM_POWER_STATES] =
363 {
364 { .version = 1,
365 .capabilityFlags = 0,
366 .outputPowerCharacter = 0,
367 .inputPowerRequirement = 0 },
368 { .version = 1,
369 .capabilityFlags = kIOPMRestartCapability,
370 .outputPowerCharacter = kIOPMRestart,
371 .inputPowerRequirement = RESTART_POWER },
372 { .version = 1,
373 .capabilityFlags = kIOPMSleepCapability,
374 .outputPowerCharacter = kIOPMSleep,
375 .inputPowerRequirement = SLEEP_POWER },
376 { .version = 1,
377 .capabilityFlags = kIOPMAOTCapability,
378 .outputPowerCharacter = kIOPMAOTPower,
379 .inputPowerRequirement = ON_POWER },
380 { .version = 1,
381 .capabilityFlags = kIOPMPowerOn,
382 .outputPowerCharacter = kIOPMPowerOn,
383 .inputPowerRequirement = ON_POWER },
384 };
385
386 #define kIOPMRootDomainWakeTypeSleepService "SleepService"
387 #define kIOPMRootDomainWakeTypeMaintenance "Maintenance"
388 #define kIOPMRootDomainWakeTypeSleepTimer "SleepTimer"
389 #define kIOPMrootDomainWakeTypeLowBattery "LowBattery"
390 #define kIOPMRootDomainWakeTypeUser "User"
391 #define kIOPMRootDomainWakeTypeAlarm "Alarm"
392 #define kIOPMRootDomainWakeTypeNetwork "Network"
393 #define kIOPMRootDomainWakeTypeHIDActivity "HID Activity"
394 #define kIOPMRootDomainWakeTypeNotification "Notification"
395 #define kIOPMRootDomainWakeTypeHibernateError "HibernateError"
396
397 // Special interest that entitles the interested client from receiving
398 // all system messages. Only used by powerd.
399 //
400 #define kIOPMSystemCapabilityInterest "IOPMSystemCapabilityInterest"
401
402 // Entitlement required for root domain clients
403 #define kRootDomainEntitlementSetProperty "com.apple.private.iokit.rootdomain-set-property"
404
405 #define WAKEEVENT_LOCK() IOLockLock(wakeEventLock)
406 #define WAKEEVENT_UNLOCK() IOLockUnlock(wakeEventLock)
407
408 /*
409 * Aggressiveness
410 */
411 #define AGGRESSIVES_LOCK() IOLockLock(featuresDictLock)
412 #define AGGRESSIVES_UNLOCK() IOLockUnlock(featuresDictLock)
413
414 #define kAggressivesMinValue 1
415
416 const char *
getAggressivenessTypeString(uint32_t type)417 getAggressivenessTypeString( uint32_t type )
418 {
419 #define AGGRESSIVENESS_TYPE(x) {(uint32_t) x, #x}
420
421 static const IONamedValue aggressivenessTypes[] = {
422 AGGRESSIVENESS_TYPE( kPMGeneralAggressiveness ),
423 AGGRESSIVENESS_TYPE( kPMMinutesToDim ),
424 AGGRESSIVENESS_TYPE( kPMMinutesToSpinDown ),
425 AGGRESSIVENESS_TYPE( kPMMinutesToSleep ),
426 AGGRESSIVENESS_TYPE( kPMEthernetWakeOnLANSettings ),
427 AGGRESSIVENESS_TYPE( kPMSetProcessorSpeed ),
428 AGGRESSIVENESS_TYPE( kPMPowerSource),
429 AGGRESSIVENESS_TYPE( kPMMotionSensor ),
430 AGGRESSIVENESS_TYPE( kPMLastAggressivenessType ),
431 { 0, NULL }
432 };
433 return IOFindNameForValue(type, aggressivenessTypes);
434 }
435
436 enum {
437 kAggressivesStateBusy = 0x01,
438 kAggressivesStateQuickSpindown = 0x02
439 };
440
441 struct AggressivesRecord {
442 uint32_t flags;
443 uint32_t type;
444 uint32_t value;
445 };
446
447 struct AggressivesRequest {
448 queue_chain_t chain;
449 uint32_t options;
450 uint32_t dataType;
451 union {
452 OSSharedPtr<IOService> service;
453 AggressivesRecord record;
454 } data;
455 };
456
457 enum {
458 kAggressivesRequestTypeService = 1,
459 kAggressivesRequestTypeRecord
460 };
461
462 enum {
463 kAggressivesOptionSynchronous = 0x00000001,
464 kAggressivesOptionQuickSpindownEnable = 0x00000100,
465 kAggressivesOptionQuickSpindownDisable = 0x00000200,
466 kAggressivesOptionQuickSpindownMask = 0x00000300
467 };
468
469 enum {
470 kAggressivesRecordFlagModified = 0x00000001,
471 kAggressivesRecordFlagMinValue = 0x00000002
472 };
473
474 // System Sleep Preventers
475
476 enum {
477 kPMUserDisabledAllSleep = 1,
478 kPMSystemRestartBootingInProgress,
479 kPMConfigPreventSystemSleep,
480 kPMChildPreventSystemSleep,
481 kPMCPUAssertion,
482 kPMPCIUnsupported,
483 kPMDKNotReady,
484 };
485
486 const char *
getSystemSleepPreventerString(uint32_t preventer)487 getSystemSleepPreventerString( uint32_t preventer )
488 {
489 #define SYSTEM_SLEEP_PREVENTER(x) {(int) x, #x}
490 static const IONamedValue systemSleepPreventers[] = {
491 SYSTEM_SLEEP_PREVENTER( kPMUserDisabledAllSleep ),
492 SYSTEM_SLEEP_PREVENTER( kPMSystemRestartBootingInProgress ),
493 SYSTEM_SLEEP_PREVENTER( kPMConfigPreventSystemSleep ),
494 SYSTEM_SLEEP_PREVENTER( kPMChildPreventSystemSleep ),
495 SYSTEM_SLEEP_PREVENTER( kPMCPUAssertion ),
496 SYSTEM_SLEEP_PREVENTER( kPMPCIUnsupported ),
497 SYSTEM_SLEEP_PREVENTER( kPMDKNotReady ),
498 { 0, NULL }
499 };
500 return IOFindNameForValue(preventer, systemSleepPreventers);
501 }
502
503 // gDarkWakeFlags
504 enum {
505 kDarkWakeFlagPromotionNone = 0x0000,
506 kDarkWakeFlagPromotionEarly = 0x0001, // promote before gfx clamp
507 kDarkWakeFlagPromotionLate = 0x0002, // promote after gfx clamp
508 kDarkWakeFlagPromotionMask = 0x0003,
509 kDarkWakeFlagAlarmIsDark = 0x0100,
510 kDarkWakeFlagAudioNotSuppressed = 0x0200,
511 kDarkWakeFlagUserWakeWorkaround = 0x1000
512 };
513
514 // gClamshellFlags
515 // The workaround for 9157444 is enabled at compile time using the
516 // DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY macro and is not represented below.
517 enum {
518 kClamshell_WAR_38378787 = 0x00000001,
519 kClamshell_WAR_47715679 = 0x00000002,
520 kClamshell_WAR_58009435 = 0x00000004
521 };
522
523 // acceptSystemWakeEvents()
524 enum {
525 kAcceptSystemWakeEvents_Disable = 0,
526 kAcceptSystemWakeEvents_Enable,
527 kAcceptSystemWakeEvents_Reenable
528 };
529
530 static IOPMrootDomain * gRootDomain;
531 static IORootParent * gPatriarch;
532 static IONotifier * gSysPowerDownNotifier = NULL;
533 static UInt32 gSleepOrShutdownPending = 0;
534 static UInt32 gWillShutdown = 0;
535 static UInt32 gPagingOff = 0;
536 static UInt32 gSleepWakeUUIDIsSet = false;
537 static uint32_t gAggressivesState = 0;
538 uint32_t gHaltTimeMaxLog;
539 uint32_t gHaltTimeMaxPanic;
540 IOLock * gHaltLogLock;
541 static char * gHaltLog;
542 enum { kHaltLogSize = 2048 };
543 static size_t gHaltLogPos;
544 static uint64_t gHaltStartTime;
545 static char gKextNameBuf[64];
546 static size_t gKextNamePos;
547 static bool gKextNameEnd;
548
549 uuid_string_t bootsessionuuid_string;
550
551 #if defined(XNU_TARGET_OS_OSX)
552 #if DISPLAY_WRANGLER_PRESENT
553 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionNone;
554 #elif defined(__arm64__)
555 // Enable temporary full wake promotion workarounds
556 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
557 #else
558 // Enable full wake promotion workarounds
559 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
560 #endif
561 #else /* !defined(XNU_TARGET_OS_OSX) */
562 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionEarly;
563 #endif /* !defined(XNU_TARGET_OS_OSX) */
564
565 static uint32_t gNoIdleFlag = 0;
566 static uint32_t gSleepDisabledFlag = 0;
567 static uint32_t gSwdPanic = 1;
568 static uint32_t gSwdSleepTimeout = 0;
569 static uint32_t gSwdWakeTimeout = 0;
570 static uint32_t gSwdSleepWakeTimeout = 0;
571 static PMStatsStruct gPMStats;
572 #if DEVELOPMENT || DEBUG
573 static uint32_t swd_panic_phase;
574 #endif
575
576 static uint32_t gClamshellFlags = 0
577 #if defined(__i386__) || defined(__x86_64__)
578 | kClamshell_WAR_58009435
579 #endif
580 ;
581
582 #if HIBERNATION
583
584 #if defined(__arm64__)
585 static IOReturn
defaultSleepPolicyHandler(void * ctx,const IOPMSystemSleepPolicyVariables * vars,IOPMSystemSleepParameters * params)586 defaultSleepPolicyHandler(void *ctx, const IOPMSystemSleepPolicyVariables *vars, IOPMSystemSleepParameters *params)
587 {
588 uint32_t sleepType = kIOPMSleepTypeDeepIdle;
589
590 assert(vars->signature == kIOPMSystemSleepPolicySignature);
591 assert(vars->version == kIOPMSystemSleepPolicyVersion);
592
593 // Hibernation enabled and either user forced hibernate or low battery sleep
594 if ((vars->hibernateMode & kIOHibernateModeOn) &&
595 (((vars->hibernateMode & kIOHibernateModeSleep) == 0) ||
596 (vars->sleepFactors & kIOPMSleepFactorBatteryLow))) {
597 sleepType = kIOPMSleepTypeHibernate;
598 }
599 params->version = kIOPMSystemSleepParametersVersion;
600 params->sleepType = sleepType;
601 return kIOReturnSuccess;
602 }
603 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = &defaultSleepPolicyHandler;
604 #else /* defined(__arm64__) */
605 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = NULL;
606 #endif /* defined(__arm64__) */
607
608 static IOPMSystemSleepPolicyVariables * gSleepPolicyVars = NULL;
609 static void * gSleepPolicyTarget;
610 #endif
611
612 struct timeval gIOLastSleepTime;
613 struct timeval gIOLastWakeTime;
614 AbsoluteTime gIOLastWakeAbsTime;
615 AbsoluteTime gIOLastSleepAbsTime;
616
617 struct timeval gIOLastUserSleepTime;
618
619 static char gWakeReasonString[128];
620 static char gBootReasonString[80];
621 static char gShutdownReasonString[80];
622 static bool gWakeReasonSysctlRegistered = false;
623 static bool gBootReasonSysctlRegistered = false;
624 static bool gShutdownReasonSysctlRegistered = false;
625 static bool gWillShutdownSysctlRegistered = false;
626 static AbsoluteTime gUserActiveAbsTime;
627 static AbsoluteTime gUserInactiveAbsTime;
628
629 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
630 static bool gSpinDumpBufferFull = false;
631 #endif
632
633 z_stream swd_zs;
634 vm_offset_t swd_zs_zmem;
635 //size_t swd_zs_zsize;
636 size_t swd_zs_zoffset;
637 #if defined(__i386__) || defined(__x86_64__)
638 IOCPU *currentShutdownTarget = NULL;
639 #endif
640
641 static unsigned int gPMHaltBusyCount;
642 static unsigned int gPMHaltIdleCount;
643 static int gPMHaltDepth;
644 static uint32_t gPMHaltMessageType;
645 static IOLock * gPMHaltLock = NULL;
646 static OSSharedPtr<OSArray> gPMHaltArray;
647 static OSSharedPtr<const OSSymbol> gPMHaltClientAcknowledgeKey;
648 static bool gPMQuiesced;
649
650 // Constants used as arguments to IOPMrootDomain::informCPUStateChange
651 #define kCPUUnknownIndex 9999999
652 enum {
653 kInformAC = 0,
654 kInformLid = 1,
655 kInformableCount = 2
656 };
657
658 OSSharedPtr<const OSSymbol> gIOPMStatsResponseTimedOut;
659 OSSharedPtr<const OSSymbol> gIOPMStatsResponseCancel;
660 OSSharedPtr<const OSSymbol> gIOPMStatsResponseSlow;
661 OSSharedPtr<const OSSymbol> gIOPMStatsResponsePrompt;
662 OSSharedPtr<const OSSymbol> gIOPMStatsDriverPSChangeSlow;
663
664 #define kBadPMFeatureID 0
665
666 /*
667 * PMSettingHandle
668 * Opaque handle passed to clients of registerPMSettingController()
669 */
670 class PMSettingHandle : public OSObject
671 {
672 OSDeclareFinalStructors( PMSettingHandle );
673 friend class PMSettingObject;
674
675 private:
676 PMSettingObject *pmso;
677 void free(void) APPLE_KEXT_OVERRIDE;
678 };
679
680 /*
681 * PMSettingObject
682 * Internal object to track each PM setting controller
683 */
684 class PMSettingObject : public OSObject
685 {
686 OSDeclareFinalStructors( PMSettingObject );
687 friend class IOPMrootDomain;
688
689 private:
690 queue_head_t calloutQueue;
691 thread_t waitThread;
692 IOPMrootDomain *parent;
693 PMSettingHandle *pmsh;
694 IOPMSettingControllerCallback func;
695 OSObject *target;
696 uintptr_t refcon;
697 OSDataAllocation<uint32_t> publishedFeatureID;
698 uint32_t settingCount;
699 bool disabled;
700
701 void free(void) APPLE_KEXT_OVERRIDE;
702
703 public:
704 static PMSettingObject *pmSettingObject(
705 IOPMrootDomain *parent_arg,
706 IOPMSettingControllerCallback handler_arg,
707 OSObject *target_arg,
708 uintptr_t refcon_arg,
709 uint32_t supportedPowerSources,
710 const OSSymbol *settings[],
711 OSObject **handle_obj);
712
713 IOReturn dispatchPMSetting(const OSSymbol *type, OSObject *object);
714 void clientHandleFreed(void);
715 };
716
717 struct PMSettingCallEntry {
718 queue_chain_t link;
719 thread_t thread;
720 };
721
722 #define PMSETTING_LOCK() IOLockLock(settingsCtrlLock)
723 #define PMSETTING_UNLOCK() IOLockUnlock(settingsCtrlLock)
724 #define PMSETTING_WAIT(p) IOLockSleep(settingsCtrlLock, p, THREAD_UNINT)
725 #define PMSETTING_WAKEUP(p) IOLockWakeup(settingsCtrlLock, p, true)
726
727 /*
728 * PMTraceWorker
729 * Internal helper object for logging trace points to RTC
730 * IOPMrootDomain and only IOPMrootDomain should instantiate
731 * exactly one of these.
732 */
733
734 typedef void (*IOPMTracePointHandler)(
735 void * target, uint32_t code, uint32_t data );
736
737 class PMTraceWorker : public OSObject
738 {
739 OSDeclareDefaultStructors(PMTraceWorker);
740 public:
741 typedef enum { kPowerChangeStart, kPowerChangeCompleted } change_t;
742
743 static OSPtr<PMTraceWorker> tracer( IOPMrootDomain * );
744 void tracePCIPowerChange(change_t, IOService *, uint32_t, uint32_t);
745 void tracePoint(uint8_t phase);
746 void traceDetail(uint32_t detail);
747 void traceComponentWakeProgress(uint32_t component, uint32_t data);
748 int recordTopLevelPCIDevice(IOService *);
749 void RTC_TRACE(void);
750 virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
751
752 IOPMTracePointHandler tracePointHandler;
753 void * tracePointTarget;
754 uint64_t getPMStatusCode();
755 uint8_t getTracePhase();
756 uint32_t getTraceData();
757 private:
758 IOPMrootDomain *owner;
759 IOLock *pmTraceWorkerLock;
760 OSSharedPtr<OSArray> pciDeviceBitMappings;
761
762 uint8_t addedToRegistry;
763 uint8_t tracePhase;
764 uint32_t traceData32;
765 uint8_t loginWindowData;
766 uint8_t coreDisplayData;
767 uint8_t coreGraphicsData;
768 };
769
770 /*
771 * this should be treated as POD, as it's byte-copied around
772 * and we cannot rely on d'tor firing at the right time
773 */
774 struct PMAssertStruct {
775 IOPMDriverAssertionID id;
776 IOPMDriverAssertionType assertionBits;
777 uint64_t createdTime;
778 uint64_t modifiedTime;
779 const OSSymbol *ownerString;
780 IOService *ownerService;
781 uint64_t registryEntryID;
782 IOPMDriverAssertionLevel level;
783 uint64_t assertCPUStartTime;
784 uint64_t assertCPUDuration;
785 };
786 OSDefineValueObjectForDependentType(PMAssertStruct)
787
788 /*
789 * PMAssertionsTracker
790 * Tracks kernel and user space PM assertions
791 */
792 class PMAssertionsTracker : public OSObject
793 {
794 OSDeclareFinalStructors(PMAssertionsTracker);
795 public:
796 static PMAssertionsTracker *pmAssertionsTracker( IOPMrootDomain * );
797
798 IOReturn createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
799 IOReturn releaseAssertion(IOPMDriverAssertionID);
800 IOReturn setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
801 IOReturn setUserAssertionLevels(IOPMDriverAssertionType);
802
803 OSSharedPtr<OSArray> copyAssertionsArray(void);
804 IOPMDriverAssertionType getActivatedAssertions(void);
805 IOPMDriverAssertionLevel getAssertionLevel(IOPMDriverAssertionType);
806
807 IOReturn handleCreateAssertion(OSValueObject<PMAssertStruct> *);
808 IOReturn handleReleaseAssertion(IOPMDriverAssertionID);
809 IOReturn handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
810 IOReturn handleSetUserAssertionLevels(void * arg0);
811 void publishProperties(void);
812 void reportCPUBitAccounting(void);
813 PMAssertStruct *detailsForID(IOPMDriverAssertionID, int *);
814
815 private:
816 uint32_t tabulateProducerCount;
817 uint32_t tabulateConsumerCount;
818
819 uint64_t maxAssertCPUDuration;
820 uint64_t maxAssertCPUEntryId;
821
822 void tabulate(void);
823 void updateCPUBitAccounting(PMAssertStruct * assertStruct);
824
825 IOPMrootDomain *owner;
826 OSSharedPtr<OSArray> assertionsArray;
827 IOLock *assertionsArrayLock;
828 IOPMDriverAssertionID issuingUniqueID __attribute__((aligned(8)));/* aligned for atomic access */
829 IOPMDriverAssertionType assertionsKernel;
830 IOPMDriverAssertionType assertionsUser;
831 IOPMDriverAssertionType assertionsCombined;
832 };
833
834 OSDefineMetaClassAndFinalStructors(PMAssertionsTracker, OSObject);
835
836 /*
837 * PMHaltWorker
838 * Internal helper object for Shutdown/Restart notifications.
839 */
840 #define kPMHaltMaxWorkers 8
841 #define kPMHaltTimeoutMS 100
842
843 class PMHaltWorker : public OSObject
844 {
845 OSDeclareFinalStructors( PMHaltWorker );
846
847 public:
848 IOService * service;// service being worked on
849 AbsoluteTime startTime; // time when work started
850 int depth; // work on nubs at this PM-tree depth
851 int visits; // number of nodes visited (debug)
852 IOLock * lock;
853 bool timeout;// service took too long
854
855 static PMHaltWorker * worker( void );
856 static void main( void * arg, wait_result_t waitResult );
857 static void work( PMHaltWorker * me );
858 static void checkTimeout( PMHaltWorker * me, AbsoluteTime * now );
859 virtual void free( void ) APPLE_KEXT_OVERRIDE;
860 };
861
OSDefineMetaClassAndFinalStructors(PMHaltWorker,OSObject)862 OSDefineMetaClassAndFinalStructors( PMHaltWorker, OSObject )
863
864
865 #define super IOService
866 OSDefineMetaClassAndFinalStructors(IOPMrootDomain, IOService)
867
868 boolean_t
869 IOPMRootDomainGetWillShutdown(void)
870 {
871 return gWillShutdown != 0;
872 }
873
874 static void
IOPMRootDomainWillShutdown(void)875 IOPMRootDomainWillShutdown(void)
876 {
877 if (OSCompareAndSwap(0, 1, &gWillShutdown)) {
878 IOService::willShutdown();
879 for (int i = 0; i < 100; i++) {
880 if (OSCompareAndSwap(0, 1, &gSleepOrShutdownPending)) {
881 break;
882 }
883 IOSleep( 100 );
884 }
885 }
886 }
887
888 extern "C" IONotifier *
registerSleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)889 registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
890 {
891 return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref ).detach();
892 }
893
894 extern "C" IONotifier *
registerPrioritySleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)895 registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
896 {
897 return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref ).detach();
898 }
899
900 extern "C" IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon)901 acknowledgeSleepWakeNotification(void * PMrefcon)
902 {
903 return gRootDomain->allowPowerChange((unsigned long)PMrefcon );
904 }
905
906 extern "C" IOReturn
vetoSleepWakeNotification(void * PMrefcon)907 vetoSleepWakeNotification(void * PMrefcon)
908 {
909 return gRootDomain->cancelPowerChange((unsigned long)PMrefcon );
910 }
911
912 extern "C" IOReturn
rootDomainRestart(void)913 rootDomainRestart( void )
914 {
915 return gRootDomain->restartSystem();
916 }
917
918 extern "C" IOReturn
rootDomainShutdown(void)919 rootDomainShutdown( void )
920 {
921 return gRootDomain->shutdownSystem();
922 }
923
924 static void
halt_log_putc(char c)925 halt_log_putc(char c)
926 {
927 if (gHaltLogPos >= (kHaltLogSize - 2)) {
928 return;
929 }
930 gHaltLog[gHaltLogPos++] = c;
931 }
932
933 extern "C" void
934 _doprnt_log(const char *fmt,
935 va_list *argp,
936 void (*putc)(char),
937 int radix);
938
939 static int
halt_log(const char * fmt,...)940 halt_log(const char *fmt, ...)
941 {
942 va_list listp;
943
944 va_start(listp, fmt);
945 _doprnt_log(fmt, &listp, &halt_log_putc, 16);
946 va_end(listp);
947
948 return 0;
949 }
950
951 extern "C" void
halt_log_enter(const char * what,const void * pc,uint64_t time)952 halt_log_enter(const char * what, const void * pc, uint64_t time)
953 {
954 uint64_t nano, millis;
955
956 if (!gHaltLog) {
957 return;
958 }
959 absolutetime_to_nanoseconds(time, &nano);
960 millis = nano / NSEC_PER_MSEC;
961 if (millis < 100) {
962 return;
963 }
964
965 IOLockLock(gHaltLogLock);
966 if (pc) {
967 halt_log("%s: %qd ms @ 0x%lx, ", what, millis, VM_KERNEL_UNSLIDE(pc));
968 OSKext::printKextsInBacktrace((vm_offset_t *) &pc, 1, &halt_log,
969 OSKext::kPrintKextsLock | OSKext::kPrintKextsUnslide | OSKext::kPrintKextsTerse);
970 } else {
971 halt_log("%s: %qd ms\n", what, millis);
972 }
973
974 gHaltLog[gHaltLogPos] = 0;
975 IOLockUnlock(gHaltLogLock);
976 }
977
978 extern uint32_t gFSState;
979
980 extern "C" void
IOSystemShutdownNotification(int howto,int stage)981 IOSystemShutdownNotification(int howto, int stage)
982 {
983 uint64_t startTime;
984
985 if (kIOSystemShutdownNotificationStageRootUnmount == stage) {
986 #if defined(XNU_TARGET_OS_OSX)
987 uint64_t nano, millis;
988 startTime = mach_absolute_time();
989 IOService::getPlatform()->waitQuiet(30 * NSEC_PER_SEC);
990 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
991 millis = nano / NSEC_PER_MSEC;
992 if (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog)) {
993 printf("waitQuiet() for unmount %qd ms\n", millis);
994 }
995 #endif /* defined(XNU_TARGET_OS_OSX) */
996 return;
997 }
998
999 if (kIOSystemShutdownNotificationTerminateDEXTs == stage) {
1000 uint64_t nano, millis;
1001 startTime = mach_absolute_time();
1002 IOServicePH::systemHalt(howto);
1003 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
1004 millis = nano / NSEC_PER_MSEC;
1005 if (true || (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog))) {
1006 printf("IOServicePH::systemHalt took %qd ms\n", millis);
1007 }
1008 return;
1009 }
1010
1011 assert(kIOSystemShutdownNotificationStageProcessExit == stage);
1012
1013 IOLockLock(gHaltLogLock);
1014 if (!gHaltLog) {
1015 gHaltLog = IONewData(char, (vm_size_t)kHaltLogSize);
1016 gHaltStartTime = mach_absolute_time();
1017 if (gHaltLog) {
1018 halt_log_putc('\n');
1019 }
1020 }
1021 IOLockUnlock(gHaltLogLock);
1022
1023 startTime = mach_absolute_time();
1024 IOPMRootDomainWillShutdown();
1025 halt_log_enter("IOPMRootDomainWillShutdown", NULL, mach_absolute_time() - startTime);
1026 #if HIBERNATION
1027 startTime = mach_absolute_time();
1028 IOHibernateSystemPostWake(true);
1029 halt_log_enter("IOHibernateSystemPostWake", NULL, mach_absolute_time() - startTime);
1030 #endif
1031 if (OSCompareAndSwap(0, 1, &gPagingOff)) {
1032 gRootDomain->handlePlatformHaltRestart(kPEPagingOff);
1033 }
1034 }
1035
1036 extern "C" int sync_internal(void);
1037
1038 /*
1039 * A device is always in the highest power state which satisfies its driver,
1040 * its policy-maker, and any power children it has, but within the constraint
1041 * of the power state provided by its parent. The driver expresses its desire by
1042 * calling changePowerStateTo(), the policy-maker expresses its desire by calling
1043 * changePowerStateToPriv(), and the children express their desires by calling
1044 * requestPowerDomainState().
1045 *
1046 * The Root Power Domain owns the policy for idle and demand sleep for the system.
1047 * It is a power-managed IOService just like the others in the system.
1048 * It implements several power states which map to what we see as Sleep and On.
1049 *
1050 * The sleep policy is as follows:
1051 * 1. Sleep is prevented if the case is open so that nobody will think the machine
1052 * is off and plug/unplug cards.
1053 * 2. Sleep is prevented if the sleep timeout slider in the prefs panel is zero.
1054 * 3. System cannot Sleep if some object in the tree is in a power state marked
1055 * kIOPMPreventSystemSleep.
1056 *
1057 * These three conditions are enforced using the "driver clamp" by calling
1058 * changePowerStateTo(). For example, if the case is opened,
1059 * changePowerStateTo(ON_STATE) is called to hold the system on regardless
1060 * of the desires of the children of the root or the state of the other clamp.
1061 *
1062 * Demand Sleep is initiated by pressing the front panel power button, closing
1063 * the clamshell, or selecting the menu item. In this case the root's parent
1064 * actually initiates the power state change so that the root domain has no
1065 * choice and does not give applications the opportunity to veto the change.
1066 *
1067 * Idle Sleep occurs if no objects in the tree are in a state marked
1068 * kIOPMPreventIdleSleep. When this is true, the root's children are not holding
1069 * the root on, so it sets the "policy-maker clamp" by calling
1070 * changePowerStateToPriv(ON_STATE) to hold itself on until the sleep timer expires.
1071 * This timer is set for the difference between the sleep timeout slider and the
1072 * display dim timeout slider. When the timer expires, it releases its clamp and
1073 * now nothing is holding it awake, so it falls asleep.
1074 *
1075 * Demand sleep is prevented when the system is booting. When preferences are
1076 * transmitted by the loginwindow at the end of boot, a flag is cleared,
1077 * and this allows subsequent Demand Sleep.
1078 */
1079
1080 //******************************************************************************
1081
1082 IOPMrootDomain *
construct(void)1083 IOPMrootDomain::construct( void )
1084 {
1085 IOPMrootDomain *root;
1086
1087 root = new IOPMrootDomain;
1088 if (root) {
1089 root->init();
1090 }
1091
1092 return root;
1093 }
1094
1095 //******************************************************************************
1096 // updateConsoleUsersCallout
1097 //
1098 //******************************************************************************
1099
1100 static void
updateConsoleUsersCallout(thread_call_param_t p0,thread_call_param_t p1)1101 updateConsoleUsersCallout(thread_call_param_t p0, thread_call_param_t p1)
1102 {
1103 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1104 rootDomain->updateConsoleUsers();
1105 }
1106
1107 void
updateConsoleUsers(void)1108 IOPMrootDomain::updateConsoleUsers(void)
1109 {
1110 IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
1111 updateTasksSuspend(kTasksSuspendUnsuspended, kTasksSuspendNoChange);
1112 }
1113
1114 bool
updateTasksSuspend(int newTasksSuspended,int newAOTTasksSuspended)1115 IOPMrootDomain::updateTasksSuspend(int newTasksSuspended, int newAOTTasksSuspended)
1116 {
1117 bool newSuspend;
1118
1119 WAKEEVENT_LOCK();
1120 if (newTasksSuspended != kTasksSuspendNoChange) {
1121 tasksSuspended = (newTasksSuspended != kTasksSuspendUnsuspended);
1122 }
1123 if (newAOTTasksSuspended != kTasksSuspendNoChange) {
1124 _aotTasksSuspended = (newAOTTasksSuspended != kTasksSuspendUnsuspended);
1125 }
1126 newSuspend = (tasksSuspended || _aotTasksSuspended);
1127 if (newSuspend == tasksSuspendState) {
1128 WAKEEVENT_UNLOCK();
1129 return false;
1130 }
1131 tasksSuspendState = newSuspend;
1132 WAKEEVENT_UNLOCK();
1133 tasks_system_suspend(newSuspend);
1134 return true;
1135 }
1136
1137 //******************************************************************************
1138
1139 static void
disk_sync_callout(thread_call_param_t p0,thread_call_param_t p1)1140 disk_sync_callout( thread_call_param_t p0, thread_call_param_t p1 )
1141 {
1142 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1143 uint32_t notifyRef = (uint32_t)(uintptr_t) p1;
1144 uint32_t powerState = rootDomain->getPowerState();
1145
1146 DLOG("disk_sync_callout ps=%u\n", powerState);
1147
1148 if (ON_STATE == powerState) {
1149 sync_internal();
1150
1151 #if HIBERNATION
1152 // Block sleep until trim issued on previous wake path is completed.
1153 IOHibernateSystemPostWake(true);
1154 #endif
1155 }
1156 #if HIBERNATION
1157 else {
1158 IOHibernateSystemPostWake(false);
1159
1160 rootDomain->sleepWakeDebugSaveSpinDumpFile();
1161 }
1162 #endif
1163
1164 rootDomain->allowPowerChange(notifyRef);
1165 DLOG("disk_sync_callout finish\n");
1166 }
1167
1168 //******************************************************************************
1169 static UInt32
computeDeltaTimeMS(const AbsoluteTime * startTime,AbsoluteTime * elapsedTime)1170 computeDeltaTimeMS( const AbsoluteTime * startTime, AbsoluteTime * elapsedTime )
1171 {
1172 AbsoluteTime endTime;
1173 UInt64 nano = 0;
1174
1175 clock_get_uptime(&endTime);
1176 if (CMP_ABSOLUTETIME(&endTime, startTime) <= 0) {
1177 *elapsedTime = 0;
1178 } else {
1179 SUB_ABSOLUTETIME(&endTime, startTime);
1180 absolutetime_to_nanoseconds(endTime, &nano);
1181 *elapsedTime = endTime;
1182 }
1183
1184 return (UInt32)(nano / NSEC_PER_MSEC);
1185 }
1186
1187 //******************************************************************************
1188
1189 static int
1190 sysctl_sleepwaketime SYSCTL_HANDLER_ARGS
1191 {
1192 struct timeval *swt = (struct timeval *)arg1;
1193 struct proc *p = req->p;
1194
1195 if (p == kernproc) {
1196 return sysctl_io_opaque(req, swt, sizeof(*swt), NULL);
1197 } else if (proc_is64bit(p)) {
1198 struct user64_timeval t = {};
1199 t.tv_sec = swt->tv_sec;
1200 t.tv_usec = swt->tv_usec;
1201 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1202 } else {
1203 struct user32_timeval t = {};
1204 t.tv_sec = (typeof(t.tv_sec))swt->tv_sec;
1205 t.tv_usec = swt->tv_usec;
1206 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1207 }
1208 }
1209
1210 static SYSCTL_PROC(_kern, OID_AUTO, sleeptime,
1211 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1212 &gIOLastUserSleepTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1213
1214 static SYSCTL_PROC(_kern, OID_AUTO, waketime,
1215 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1216 &gIOLastWakeTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1217
1218 SYSCTL_QUAD(_kern, OID_AUTO, wake_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastWakeAbsTime, "");
1219 SYSCTL_QUAD(_kern, OID_AUTO, sleep_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastSleepAbsTime, "");
1220 SYSCTL_QUAD(_kern, OID_AUTO, useractive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserActiveAbsTime, "");
1221 SYSCTL_QUAD(_kern, OID_AUTO, userinactive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserInactiveAbsTime, "");
1222
1223 static int
1224 sysctl_willshutdown SYSCTL_HANDLER_ARGS
1225 {
1226 int new_value, changed, error;
1227
1228 if (!gWillShutdownSysctlRegistered) {
1229 return ENOENT;
1230 }
1231
1232 error = sysctl_io_number(req, gWillShutdown, sizeof(int), &new_value, &changed);
1233 if (changed) {
1234 if (!gWillShutdown && (new_value == 1)) {
1235 IOPMRootDomainWillShutdown();
1236 } else {
1237 error = EINVAL;
1238 }
1239 }
1240 return error;
1241 }
1242
1243 static SYSCTL_PROC(_kern, OID_AUTO, willshutdown,
1244 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1245 NULL, 0, sysctl_willshutdown, "I", "");
1246
1247 #if defined(XNU_TARGET_OS_OSX)
1248
1249 static int
sysctl_progressmeterenable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1250 sysctl_progressmeterenable
1251 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1252 {
1253 int error;
1254 int new_value, changed;
1255
1256 error = sysctl_io_number(req, vc_progressmeter_enable, sizeof(int), &new_value, &changed);
1257
1258 if (changed) {
1259 vc_enable_progressmeter(new_value);
1260 }
1261
1262 return error;
1263 }
1264
1265 static int
sysctl_progressmeter(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1266 sysctl_progressmeter
1267 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1268 {
1269 int error;
1270 int new_value, changed;
1271
1272 error = sysctl_io_number(req, vc_progressmeter_value, sizeof(int), &new_value, &changed);
1273
1274 if (changed) {
1275 vc_set_progressmeter(new_value);
1276 }
1277
1278 return error;
1279 }
1280
1281 static SYSCTL_PROC(_kern, OID_AUTO, progressmeterenable,
1282 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1283 NULL, 0, sysctl_progressmeterenable, "I", "");
1284
1285 static SYSCTL_PROC(_kern, OID_AUTO, progressmeter,
1286 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1287 NULL, 0, sysctl_progressmeter, "I", "");
1288
1289 #endif /* defined(XNU_TARGET_OS_OSX) */
1290
1291
1292
1293 static int
sysctl_consoleoptions(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1294 sysctl_consoleoptions
1295 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1296 {
1297 int error, changed;
1298 uint32_t new_value;
1299
1300 error = sysctl_io_number(req, vc_user_options.options, sizeof(uint32_t), &new_value, &changed);
1301
1302 if (changed) {
1303 vc_user_options.options = new_value;
1304 }
1305
1306 return error;
1307 }
1308
1309 static SYSCTL_PROC(_kern, OID_AUTO, consoleoptions,
1310 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1311 NULL, 0, sysctl_consoleoptions, "I", "");
1312
1313
1314 static int
1315 sysctl_progressoptions SYSCTL_HANDLER_ARGS
1316 {
1317 return sysctl_io_opaque(req, &vc_user_options, sizeof(vc_user_options), NULL);
1318 }
1319
1320 static SYSCTL_PROC(_kern, OID_AUTO, progressoptions,
1321 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1322 NULL, 0, sysctl_progressoptions, "S,vc_progress_user_options", "");
1323
1324
1325 static int
1326 sysctl_wakereason SYSCTL_HANDLER_ARGS
1327 {
1328 char wr[sizeof(gWakeReasonString)];
1329
1330 wr[0] = '\0';
1331 if (gRootDomain && gWakeReasonSysctlRegistered) {
1332 gRootDomain->copyWakeReasonString(wr, sizeof(wr));
1333 } else {
1334 return ENOENT;
1335 }
1336
1337 return sysctl_io_string(req, wr, 0, 0, NULL);
1338 }
1339
1340 SYSCTL_PROC(_kern, OID_AUTO, wakereason,
1341 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1342 NULL, 0, sysctl_wakereason, "A", "wakereason");
1343
1344 static int
1345 sysctl_bootreason SYSCTL_HANDLER_ARGS
1346 {
1347 if (!os_atomic_load(&gBootReasonSysctlRegistered, acquire)) {
1348 return ENOENT;
1349 }
1350
1351 return sysctl_io_string(req, gBootReasonString, 0, 0, NULL);
1352 }
1353
1354 SYSCTL_PROC(_kern, OID_AUTO, bootreason,
1355 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1356 NULL, 0, sysctl_bootreason, "A", "");
1357
1358 static int
1359 sysctl_shutdownreason SYSCTL_HANDLER_ARGS
1360 {
1361 char sr[sizeof(gShutdownReasonString)];
1362
1363 sr[0] = '\0';
1364 if (gRootDomain && gShutdownReasonSysctlRegistered) {
1365 gRootDomain->copyShutdownReasonString(sr, sizeof(sr));
1366 } else {
1367 return ENOENT;
1368 }
1369
1370 return sysctl_io_string(req, sr, 0, 0, NULL);
1371 }
1372
1373 SYSCTL_PROC(_kern, OID_AUTO, shutdownreason,
1374 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1375 NULL, 0, sysctl_shutdownreason, "A", "shutdownreason");
1376
1377 static int
1378 sysctl_targettype SYSCTL_HANDLER_ARGS
1379 {
1380 IOService * root;
1381 OSSharedPtr<OSObject> obj;
1382 OSData * data;
1383 char tt[32];
1384
1385 tt[0] = '\0';
1386 root = IOService::getServiceRoot();
1387 if (root && (obj = root->copyProperty(gIODTTargetTypeKey))) {
1388 if ((data = OSDynamicCast(OSData, obj.get()))) {
1389 strlcpy(tt, (const char *) data->getBytesNoCopy(), sizeof(tt));
1390 }
1391 }
1392 return sysctl_io_string(req, tt, 0, 0, NULL);
1393 }
1394
1395 SYSCTL_PROC(_hw, OID_AUTO, targettype,
1396 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1397 NULL, 0, sysctl_targettype, "A", "targettype");
1398
1399 static SYSCTL_INT(_debug, OID_AUTO, noidle, CTLFLAG_RW, &gNoIdleFlag, 0, "");
1400 static SYSCTL_INT(_debug, OID_AUTO, swd_sleep_timeout, CTLFLAG_RW, &gSwdSleepTimeout, 0, "");
1401 static SYSCTL_INT(_debug, OID_AUTO, swd_wake_timeout, CTLFLAG_RW, &gSwdWakeTimeout, 0, "");
1402 static SYSCTL_INT(_debug, OID_AUTO, swd_timeout, CTLFLAG_RW, &gSwdSleepWakeTimeout, 0, "");
1403 static SYSCTL_INT(_debug, OID_AUTO, swd_panic, CTLFLAG_RW, &gSwdPanic, 0, "");
1404 #if DEVELOPMENT || DEBUG
1405 static SYSCTL_INT(_debug, OID_AUTO, swd_panic_phase, CTLFLAG_RW, &swd_panic_phase, 0, "");
1406 #if defined(XNU_TARGET_OS_OSX)
1407 static SYSCTL_INT(_debug, OID_AUTO, clamshell, CTLFLAG_RW, &gClamshellFlags, 0, "");
1408 static SYSCTL_INT(_debug, OID_AUTO, darkwake, CTLFLAG_RW, &gDarkWakeFlags, 0, "");
1409 #endif /* defined(XNU_TARGET_OS_OSX) */
1410 #endif /* DEVELOPMENT || DEBUG */
1411
1412 //******************************************************************************
1413 // AOT
1414
1415 static int
1416 sysctl_aotmetrics SYSCTL_HANDLER_ARGS
1417 {
1418 if (NULL == gRootDomain) {
1419 return ENOENT;
1420 }
1421 if (NULL == gRootDomain->_aotMetrics) {
1422 IOPMAOTMetrics nullMetrics = {};
1423 return sysctl_io_opaque(req, &nullMetrics, sizeof(IOPMAOTMetrics), NULL);
1424 }
1425 return sysctl_io_opaque(req, gRootDomain->_aotMetrics, sizeof(IOPMAOTMetrics), NULL);
1426 }
1427
1428 static SYSCTL_PROC(_kern, OID_AUTO, aotmetrics,
1429 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1430 NULL, 0, sysctl_aotmetrics, "S,IOPMAOTMetrics", "");
1431
1432
1433 static int
update_aotmode(uint32_t mode)1434 update_aotmode(uint32_t mode)
1435 {
1436 int result;
1437
1438 if (!gIOPMWorkLoop) {
1439 return ENOENT;
1440 }
1441 result = gIOPMWorkLoop->runActionBlock(^IOReturn (void) {
1442 unsigned int oldCount;
1443
1444 if (mode && !gRootDomain->_aotMetrics) {
1445 gRootDomain->_aotMetrics = IOMallocType(IOPMAOTMetrics);
1446 }
1447
1448 oldCount = gRootDomain->idleSleepPreventersCount();
1449 gRootDomain->_aotMode = (mode & kIOPMAOTModeMask);
1450 gRootDomain->updatePreventIdleSleepListInternal(NULL, false, oldCount);
1451 return 0;
1452 });
1453 return result;
1454 }
1455
1456 static int
sysctl_aotmodebits(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1457 sysctl_aotmodebits
1458 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1459 {
1460 int error, changed;
1461 uint32_t new_value;
1462
1463 if (NULL == gRootDomain) {
1464 return ENOENT;
1465 }
1466 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1467 if (changed && gIOPMWorkLoop) {
1468 error = update_aotmode(new_value);
1469 }
1470
1471 return error;
1472 }
1473
1474 static SYSCTL_PROC(_kern, OID_AUTO, aotmodebits,
1475 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1476 NULL, 0, sysctl_aotmodebits, "I", "");
1477
1478 static int
sysctl_aotmode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1479 sysctl_aotmode
1480 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1481 {
1482 int error, changed;
1483 uint32_t new_value;
1484
1485 if (NULL == gRootDomain) {
1486 return ENOENT;
1487 }
1488 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1489 if (changed && gIOPMWorkLoop) {
1490 if (new_value) {
1491 new_value = kIOPMAOTModeDefault; // & ~kIOPMAOTModeRespectTimers;
1492 }
1493 error = update_aotmode(new_value);
1494 }
1495
1496 return error;
1497 }
1498
1499 static SYSCTL_PROC(_kern, OID_AUTO, aotmode,
1500 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1501 NULL, 0, sysctl_aotmode, "I", "");
1502
1503 //******************************************************************************
1504
1505 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeCalendarKey;
1506 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeSecondsKey;
1507 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerCalendarKey;
1508 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerSecondsKey;
1509 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugWakeRelativeKey;
1510 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugPowerRelativeKey;
1511 static OSSharedPtr<const OSSymbol> gIOPMSettingMaintenanceWakeCalendarKey;
1512 static OSSharedPtr<const OSSymbol> gIOPMSettingSleepServiceWakeCalendarKey;
1513 static OSSharedPtr<const OSSymbol> gIOPMSettingSilentRunningKey;
1514 static OSSharedPtr<const OSSymbol> gIOPMUserTriggeredFullWakeKey;
1515 static OSSharedPtr<const OSSymbol> gIOPMUserIsActiveKey;
1516 static OSSharedPtr<const OSSymbol> gIOPMSettingLowLatencyAudioModeKey;
1517
1518 //******************************************************************************
1519 // start
1520 //
1521 //******************************************************************************
1522
1523 #define kRootDomainSettingsCount 20
1524 #define kRootDomainNoPublishSettingsCount 4
1525
1526 bool
start(IOService * nub)1527 IOPMrootDomain::start( IOService * nub )
1528 {
1529 OSSharedPtr<OSIterator> psIterator;
1530 OSSharedPtr<OSDictionary> tmpDict;
1531
1532 super::start(nub);
1533
1534 gRootDomain = this;
1535 gIOPMSettingAutoWakeCalendarKey = OSSymbol::withCString(kIOPMSettingAutoWakeCalendarKey);
1536 gIOPMSettingAutoWakeSecondsKey = OSSymbol::withCString(kIOPMSettingAutoWakeSecondsKey);
1537 gIOPMSettingAutoPowerCalendarKey = OSSymbol::withCString(kIOPMSettingAutoPowerCalendarKey);
1538 gIOPMSettingAutoPowerSecondsKey = OSSymbol::withCString(kIOPMSettingAutoPowerSecondsKey);
1539 gIOPMSettingDebugWakeRelativeKey = OSSymbol::withCString(kIOPMSettingDebugWakeRelativeKey);
1540 gIOPMSettingDebugPowerRelativeKey = OSSymbol::withCString(kIOPMSettingDebugPowerRelativeKey);
1541 gIOPMSettingMaintenanceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingMaintenanceWakeCalendarKey);
1542 gIOPMSettingSleepServiceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingSleepServiceWakeCalendarKey);
1543 gIOPMSettingSilentRunningKey = OSSymbol::withCStringNoCopy(kIOPMSettingSilentRunningKey);
1544 gIOPMUserTriggeredFullWakeKey = OSSymbol::withCStringNoCopy(kIOPMUserTriggeredFullWakeKey);
1545 gIOPMUserIsActiveKey = OSSymbol::withCStringNoCopy(kIOPMUserIsActiveKey);
1546 gIOPMSettingLowLatencyAudioModeKey = OSSymbol::withCStringNoCopy(kIOPMSettingLowLatencyAudioModeKey);
1547
1548 gIOPMStatsResponseTimedOut = OSSymbol::withCString(kIOPMStatsResponseTimedOut);
1549 gIOPMStatsResponseCancel = OSSymbol::withCString(kIOPMStatsResponseCancel);
1550 gIOPMStatsResponseSlow = OSSymbol::withCString(kIOPMStatsResponseSlow);
1551 gIOPMStatsResponsePrompt = OSSymbol::withCString(kIOPMStatsResponsePrompt);
1552 gIOPMStatsDriverPSChangeSlow = OSSymbol::withCString(kIOPMStatsDriverPSChangeSlow);
1553
1554 sleepSupportedPEFunction = OSSymbol::withCString("IOPMSetSleepSupported");
1555 sleepMessagePEFunction = OSSymbol::withCString("IOPMSystemSleepMessage");
1556 gIOPMWakeTypeUserKey = OSSymbol::withCStringNoCopy(kIOPMRootDomainWakeTypeUser);
1557
1558 OSSharedPtr<const OSSymbol> settingsArr[kRootDomainSettingsCount] =
1559 {
1560 OSSymbol::withCString(kIOPMSettingSleepOnPowerButtonKey),
1561 gIOPMSettingAutoWakeSecondsKey,
1562 gIOPMSettingAutoPowerSecondsKey,
1563 gIOPMSettingAutoWakeCalendarKey,
1564 gIOPMSettingAutoPowerCalendarKey,
1565 gIOPMSettingDebugWakeRelativeKey,
1566 gIOPMSettingDebugPowerRelativeKey,
1567 OSSymbol::withCString(kIOPMSettingWakeOnRingKey),
1568 OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey),
1569 OSSymbol::withCString(kIOPMSettingWakeOnClamshellKey),
1570 OSSymbol::withCString(kIOPMSettingWakeOnACChangeKey),
1571 OSSymbol::withCString(kIOPMSettingTimeZoneOffsetKey),
1572 OSSymbol::withCString(kIOPMSettingDisplaySleepUsesDimKey),
1573 OSSymbol::withCString(kIOPMSettingMobileMotionModuleKey),
1574 OSSymbol::withCString(kIOPMSettingGraphicsSwitchKey),
1575 OSSymbol::withCString(kIOPMStateConsoleShutdown),
1576 OSSymbol::withCString(kIOPMSettingProModeControl),
1577 OSSymbol::withCString(kIOPMSettingProModeDefer),
1578 gIOPMSettingSilentRunningKey,
1579 gIOPMSettingLowLatencyAudioModeKey,
1580 };
1581
1582 OSSharedPtr<const OSSymbol> noPublishSettingsArr[kRootDomainNoPublishSettingsCount] =
1583 {
1584 OSSymbol::withCString(kIOPMSettingProModeControl),
1585 OSSymbol::withCString(kIOPMSettingProModeDefer),
1586 gIOPMSettingSilentRunningKey,
1587 gIOPMSettingLowLatencyAudioModeKey,
1588 };
1589
1590 #if DEVELOPMENT || DEBUG
1591 #if defined(XNU_TARGET_OS_OSX)
1592 PE_parse_boot_argn("darkwake", &gDarkWakeFlags, sizeof(gDarkWakeFlags));
1593 PE_parse_boot_argn("clamshell", &gClamshellFlags, sizeof(gClamshellFlags));
1594 #endif /* defined(XNU_TARGET_OS_OSX) */
1595 #endif /* DEVELOPMENT || DEBUG */
1596
1597 PE_parse_boot_argn("noidle", &gNoIdleFlag, sizeof(gNoIdleFlag));
1598 PE_parse_boot_argn("swd_sleeptimeout", &gSwdSleepTimeout, sizeof(gSwdSleepTimeout));
1599 PE_parse_boot_argn("swd_waketimeout", &gSwdWakeTimeout, sizeof(gSwdWakeTimeout));
1600 PE_parse_boot_argn("swd_timeout", &gSwdSleepWakeTimeout, sizeof(gSwdSleepWakeTimeout));
1601 PE_parse_boot_argn("haltmspanic", &gHaltTimeMaxPanic, sizeof(gHaltTimeMaxPanic));
1602 PE_parse_boot_argn("haltmslog", &gHaltTimeMaxLog, sizeof(gHaltTimeMaxLog));
1603
1604 // read noidle setting from Device Tree
1605 if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
1606 DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
1607 }
1608
1609 queue_init(&aggressivesQueue);
1610 aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1611 aggressivesData = OSData::withCapacity(
1612 sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1613
1614 featuresDictLock = IOLockAlloc();
1615 settingsCtrlLock = IOLockAlloc();
1616 wakeEventLock = IOLockAlloc();
1617 gHaltLogLock = IOLockAlloc();
1618 setPMRootDomain(this);
1619
1620 extraSleepTimer = thread_call_allocate(
1621 idleSleepTimerExpired,
1622 (thread_call_param_t) this);
1623
1624 powerButtonDown = thread_call_allocate(
1625 powerButtonDownCallout,
1626 (thread_call_param_t) this);
1627
1628 powerButtonUp = thread_call_allocate(
1629 powerButtonUpCallout,
1630 (thread_call_param_t) this);
1631
1632 diskSyncCalloutEntry = thread_call_allocate(
1633 &disk_sync_callout,
1634 (thread_call_param_t) this);
1635 updateConsoleUsersEntry = thread_call_allocate(
1636 &updateConsoleUsersCallout,
1637 (thread_call_param_t) this);
1638
1639 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1640 fullWakeThreadCall = thread_call_allocate_with_options(
1641 OSMemberFunctionCast(thread_call_func_t, this,
1642 &IOPMrootDomain::fullWakeDelayedWork),
1643 (thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1644 THREAD_CALL_OPTIONS_ONCE);
1645 #endif
1646
1647 setProperty(kIOSleepSupportedKey, true);
1648
1649 bzero(&gPMStats, sizeof(gPMStats));
1650
1651 pmTracer = PMTraceWorker::tracer(this);
1652
1653 pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1654
1655 userDisabledAllSleep = false;
1656 systemBooting = true;
1657 idleSleepEnabled = false;
1658 idleSleepRevertible = true;
1659 sleepSlider = 0;
1660 idleSleepTimerPending = false;
1661 wrangler = NULL;
1662 clamshellClosed = false;
1663 clamshellExists = false;
1664 #if DISPLAY_WRANGLER_PRESENT
1665 clamshellDisabled = true;
1666 #else
1667 clamshellDisabled = false;
1668 #endif
1669 clamshellIgnoreClose = false;
1670 acAdaptorConnected = true;
1671 clamshellSleepDisableMask = 0;
1672 gWakeReasonString[0] = '\0';
1673
1674 // Initialize to user active.
1675 // Will never transition to user inactive w/o wrangler.
1676 fullWakeReason = kFullWakeReasonLocalUser;
1677 userIsActive = userWasActive = true;
1678 clock_get_uptime(&gUserActiveAbsTime);
1679 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1680
1681 // Set the default system capabilities at boot.
1682 _currentCapability = kIOPMSystemCapabilityCPU |
1683 kIOPMSystemCapabilityGraphics |
1684 kIOPMSystemCapabilityAudio |
1685 kIOPMSystemCapabilityNetwork;
1686
1687 _pendingCapability = _currentCapability;
1688 _desiredCapability = _currentCapability;
1689 _highestCapability = _currentCapability;
1690 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1691
1692 queuedSleepWakeUUIDString = NULL;
1693 initializeBootSessionUUID();
1694 pmStatsAppResponses = OSArray::withCapacity(5);
1695 _statsNameKey = OSSymbol::withCString(kIOPMStatsNameKey);
1696 _statsPIDKey = OSSymbol::withCString(kIOPMStatsPIDKey);
1697 _statsTimeMSKey = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1698 _statsResponseTypeKey = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1699 _statsMessageTypeKey = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1700 _statsPowerCapsKey = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1701 assertOnWakeSecs = -1;// Invalid value to prevent updates
1702
1703 pmStatsLock = IOLockAlloc();
1704 idxPMCPUClamshell = kCPUUnknownIndex;
1705 idxPMCPULimitedPower = kCPUUnknownIndex;
1706
1707 tmpDict = OSDictionary::withCapacity(1);
1708 setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1709
1710 // Set a default "SystemPowerProfileOverrideDict" for platform
1711 // drivers without any overrides.
1712 if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1713 tmpDict = OSDictionary::withCapacity(1);
1714 setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1715 }
1716
1717 settingsCallbacks = OSDictionary::withCapacity(1);
1718
1719 // Create a list of the valid PM settings that we'll relay to
1720 // interested clients in setProperties() => setPMSetting()
1721 allowedPMSettings = OSArray::withObjects(
1722 (const OSObject **)settingsArr,
1723 kRootDomainSettingsCount,
1724 0);
1725
1726 // List of PM settings that should not automatically publish itself
1727 // as a feature when registered by a listener.
1728 noPublishPMSettings = OSArray::withObjects(
1729 (const OSObject **)noPublishSettingsArr,
1730 kRootDomainNoPublishSettingsCount,
1731 0);
1732
1733 fPMSettingsDict = OSDictionary::withCapacity(5);
1734 preventIdleSleepList = OSSet::withCapacity(8);
1735 preventSystemSleepList = OSSet::withCapacity(2);
1736
1737 PMinit(); // creates gIOPMWorkLoop
1738 gIOPMWorkLoop = getIOPMWorkloop();
1739
1740 // Create IOPMPowerStateQueue used to queue external power
1741 // events, and to handle those events on the PM work loop.
1742 pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1743 this, OSMemberFunctionCast(IOEventSource::Action, this,
1744 &IOPMrootDomain::dispatchPowerEvent));
1745 gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1746
1747 _aotMode = 0;
1748 _aotTimerES = IOTimerEventSource::timerEventSource(this,
1749 OSMemberFunctionCast(IOTimerEventSource::Action,
1750 this, &IOPMrootDomain::aotEvaluate));
1751 gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1752
1753 // Avoid publishing service early so gIOPMWorkLoop is
1754 // guaranteed to be initialized by rootDomain.
1755 publishPMRootDomain();
1756
1757 // create our power parent
1758 gPatriarch = new IORootParent;
1759 gPatriarch->init();
1760 gPatriarch->attach(this);
1761 gPatriarch->start(this);
1762 gPatriarch->addPowerChild(this);
1763
1764 registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1765 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1766
1767 // install power change handler
1768 gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1769
1770 #if DISPLAY_WRANGLER_PRESENT
1771 wranglerIdleSettings = OSDictionary::withCapacity(1);
1772 OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1773
1774 if (wranglerIdleSettings && wranglerIdlePeriod) {
1775 wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1776 wranglerIdlePeriod.get());
1777 }
1778
1779 #endif /* DISPLAY_WRANGLER_PRESENT */
1780
1781 lowLatencyAudioNotifierDict = OSDictionary::withCapacity(2);
1782 lowLatencyAudioNotifyStateSym = OSSymbol::withCString("LowLatencyAudioNotifyState");
1783 lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1784 lowLatencyAudioNotifyStateVal = OSNumber::withNumber(0ull, 32);
1785 lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1786
1787 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1788 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1789 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1790 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1791 }
1792
1793 OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1794 setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1795
1796 // IOBacklightDisplay can take a long time to load at boot, or it may
1797 // not load at all if you're booting with clamshell closed. We publish
1798 // 'DisplayDims' here redundantly to get it published early and at all.
1799 OSSharedPtr<OSDictionary> matching;
1800 matching = serviceMatching("IOPMPowerSource");
1801 psIterator = getMatchingServices(matching.get());
1802
1803 if (psIterator && psIterator->getNextObject()) {
1804 // There's at least one battery on the system, so we publish
1805 // 'DisplayDims' support for the LCD.
1806 publishFeature("DisplayDims");
1807 }
1808
1809 // read swd_panic boot-arg
1810 PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1811 gWillShutdownSysctlRegistered = true;
1812
1813 #if HIBERNATION
1814 IOHibernateSystemInit(this);
1815 #endif
1816
1817 registerService(); // let clients find us
1818
1819 return true;
1820 }
1821
1822 //******************************************************************************
1823 // setProperties
1824 //
1825 // Receive a setProperty call
1826 // The "System Boot" property means the system is completely booted.
1827 //******************************************************************************
1828
1829 IOReturn
setProperties(OSObject * props_obj)1830 IOPMrootDomain::setProperties( OSObject * props_obj )
1831 {
1832 IOReturn return_value = kIOReturnSuccess;
1833 OSDictionary *dict = OSDynamicCast(OSDictionary, props_obj);
1834 OSBoolean *b = NULL;
1835 OSNumber *n = NULL;
1836 const OSSymbol *key = NULL;
1837 OSObject *obj = NULL;
1838 OSSharedPtr<OSCollectionIterator> iter;
1839
1840 if (!dict) {
1841 return kIOReturnBadArgument;
1842 }
1843
1844 bool clientEntitled = false;
1845 {
1846 OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1847 clientEntitled = (obj == kOSBooleanTrue);
1848 }
1849
1850 if (!clientEntitled) {
1851 const char * errorSuffix = NULL;
1852
1853 // IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1854 // That API can set 6 possible keys that are checked below.
1855 if ((dict->getCount() == 1) &&
1856 (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1857 dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1858 dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1859 dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1860 dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1861 dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1862 return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1863 if (return_value != kIOReturnSuccess) {
1864 errorSuffix = "privileged";
1865 }
1866 } else {
1867 return_value = kIOReturnNotPermitted;
1868 errorSuffix = "entitled";
1869 }
1870
1871 if (return_value != kIOReturnSuccess) {
1872 OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1873 DLOG("%s failed, process %s is not %s\n", __func__,
1874 procName ? procName->getCStringNoCopy() : "", errorSuffix);
1875 return return_value;
1876 }
1877 }
1878
1879 OSSharedPtr<const OSSymbol> publish_simulated_battery_string = OSSymbol::withCString("SoftwareSimulatedBatteries");
1880 OSSharedPtr<const OSSymbol> boot_complete_string = OSSymbol::withCString("System Boot Complete");
1881 OSSharedPtr<const OSSymbol> sys_shutdown_string = OSSymbol::withCString("System Shutdown");
1882 OSSharedPtr<const OSSymbol> stall_halt_string = OSSymbol::withCString("StallSystemAtHalt");
1883 OSSharedPtr<const OSSymbol> battery_warning_disabled_string = OSSymbol::withCString("BatteryWarningsDisabled");
1884 OSSharedPtr<const OSSymbol> idle_seconds_string = OSSymbol::withCString("System Idle Seconds");
1885 OSSharedPtr<const OSSymbol> idle_milliseconds_string = OSSymbol::withCString("System Idle Milliseconds");
1886 OSSharedPtr<const OSSymbol> sleepdisabled_string = OSSymbol::withCString("SleepDisabled");
1887 OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1888 OSSharedPtr<const OSSymbol> loginwindow_progress_string = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1889 OSSharedPtr<const OSSymbol> coredisplay_progress_string = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1890 OSSharedPtr<const OSSymbol> coregraphics_progress_string = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1891 #if DEBUG || DEVELOPMENT
1892 OSSharedPtr<const OSSymbol> clamshell_close_string = OSSymbol::withCString("IOPMTestClamshellClose");
1893 OSSharedPtr<const OSSymbol> clamshell_open_string = OSSymbol::withCString("IOPMTestClamshellOpen");
1894 OSSharedPtr<const OSSymbol> ac_detach_string = OSSymbol::withCString("IOPMTestACDetach");
1895 OSSharedPtr<const OSSymbol> ac_attach_string = OSSymbol::withCString("IOPMTestACAttach");
1896 OSSharedPtr<const OSSymbol> desktopmode_set_string = OSSymbol::withCString("IOPMTestDesktopModeSet");
1897 OSSharedPtr<const OSSymbol> desktopmode_remove_string = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1898 #endif
1899
1900 #if HIBERNATION
1901 OSSharedPtr<const OSSymbol> hibernatemode_string = OSSymbol::withCString(kIOHibernateModeKey);
1902 OSSharedPtr<const OSSymbol> hibernatefile_string = OSSymbol::withCString(kIOHibernateFileKey);
1903 OSSharedPtr<const OSSymbol> hibernatefilemin_string = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1904 OSSharedPtr<const OSSymbol> hibernatefilemax_string = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1905 OSSharedPtr<const OSSymbol> hibernatefreeratio_string = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1906 OSSharedPtr<const OSSymbol> hibernatefreetime_string = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1907 #endif
1908
1909 iter = OSCollectionIterator::withCollection(dict);
1910 if (!iter) {
1911 return_value = kIOReturnNoMemory;
1912 goto exit;
1913 }
1914
1915 while ((key = (const OSSymbol *) iter->getNextObject()) &&
1916 (obj = dict->getObject(key))) {
1917 if (key->isEqualTo(publish_simulated_battery_string.get())) {
1918 if (OSDynamicCast(OSBoolean, obj)) {
1919 publishResource(key, kOSBooleanTrue);
1920 }
1921 } else if (key->isEqualTo(idle_seconds_string.get())) {
1922 if ((n = OSDynamicCast(OSNumber, obj))) {
1923 setProperty(key, n);
1924 idleMilliSeconds = n->unsigned32BitValue() * 1000;
1925 }
1926 } else if (key->isEqualTo(idle_milliseconds_string.get())) {
1927 if ((n = OSDynamicCast(OSNumber, obj))) {
1928 setProperty(key, n);
1929 idleMilliSeconds = n->unsigned32BitValue();
1930 }
1931 } else if (key->isEqualTo(boot_complete_string.get())) {
1932 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1933 } else if (key->isEqualTo(sys_shutdown_string.get())) {
1934 if ((b = OSDynamicCast(OSBoolean, obj))) {
1935 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1936 }
1937 } else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1938 setProperty(key, obj);
1939 }
1940 #if HIBERNATION
1941 else if (key->isEqualTo(hibernatemode_string.get()) ||
1942 key->isEqualTo(hibernatefilemin_string.get()) ||
1943 key->isEqualTo(hibernatefilemax_string.get()) ||
1944 key->isEqualTo(hibernatefreeratio_string.get()) ||
1945 key->isEqualTo(hibernatefreetime_string.get())) {
1946 if ((n = OSDynamicCast(OSNumber, obj))) {
1947 setProperty(key, n);
1948 }
1949 } else if (key->isEqualTo(hibernatefile_string.get())) {
1950 OSString * str = OSDynamicCast(OSString, obj);
1951 if (str) {
1952 setProperty(key, str);
1953 }
1954 }
1955 #endif
1956 else if (key->isEqualTo(sleepdisabled_string.get())) {
1957 if ((b = OSDynamicCast(OSBoolean, obj))) {
1958 setProperty(key, b);
1959 pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1960 }
1961 } else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1962 obj->retain();
1963 pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1964 } else if (key->isEqualTo(loginwindow_progress_string.get())) {
1965 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1966 uint32_t data = n->unsigned32BitValue();
1967 pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1968 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1969 }
1970 } else if (key->isEqualTo(coredisplay_progress_string.get())) {
1971 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1972 uint32_t data = n->unsigned32BitValue();
1973 pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1974 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1975 }
1976 } else if (key->isEqualTo(coregraphics_progress_string.get())) {
1977 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1978 uint32_t data = n->unsigned32BitValue();
1979 pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1980 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1981 }
1982 } else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1983 key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1984 key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1985 key->isEqualTo(stall_halt_string.get())) {
1986 if ((b = OSDynamicCast(OSBoolean, obj))) {
1987 setProperty(key, b);
1988 }
1989 } else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1990 key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1991 key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1992 key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1993 if ((n = OSDynamicCast(OSNumber, obj))) {
1994 setProperty(key, n);
1995 }
1996 } else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1997 if (kOSBooleanTrue == obj) {
1998 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1999 } else {
2000 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
2001 }
2002 DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
2003 }
2004 #if DEBUG || DEVELOPMENT
2005 else if (key->isEqualTo(clamshell_close_string.get())) {
2006 DLOG("SetProperties: setting clamshell close\n");
2007 UInt32 msg = kIOPMClamshellClosed;
2008 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2009 } else if (key->isEqualTo(clamshell_open_string.get())) {
2010 DLOG("SetProperties: setting clamshell open\n");
2011 UInt32 msg = kIOPMClamshellOpened;
2012 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2013 } else if (key->isEqualTo(ac_detach_string.get())) {
2014 DLOG("SetProperties: setting ac detach\n");
2015 UInt32 msg = kIOPMSetACAdaptorConnected;
2016 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2017 } else if (key->isEqualTo(ac_attach_string.get())) {
2018 DLOG("SetProperties: setting ac attach\n");
2019 UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
2020 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2021 } else if (key->isEqualTo(desktopmode_set_string.get())) {
2022 DLOG("SetProperties: setting desktopmode");
2023 UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
2024 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2025 } else if (key->isEqualTo(desktopmode_remove_string.get())) {
2026 DLOG("SetProperties: removing desktopmode\n");
2027 UInt32 msg = kIOPMSetDesktopMode;
2028 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2029 }
2030 #endif
2031 // Relay our allowed PM settings onto our registered PM clients
2032 else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2033 return_value = setPMSetting(key, obj);
2034 if (kIOReturnSuccess != return_value) {
2035 break;
2036 }
2037 } else {
2038 DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2039 }
2040 }
2041
2042 exit:
2043 return return_value;
2044 }
2045
2046 // MARK: -
2047 // MARK: Aggressiveness
2048
2049 //******************************************************************************
2050 // setAggressiveness
2051 //
2052 // Override IOService::setAggressiveness()
2053 //******************************************************************************
2054
2055 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2056 IOPMrootDomain::setAggressiveness(
2057 unsigned long type,
2058 unsigned long value )
2059 {
2060 return setAggressiveness( type, value, 0 );
2061 }
2062
2063 /*
2064 * Private setAggressiveness() with an internal options argument.
2065 */
2066 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2067 IOPMrootDomain::setAggressiveness(
2068 unsigned long type,
2069 unsigned long value,
2070 IOOptionBits options )
2071 {
2072 AggressivesRequest * entry;
2073 AggressivesRequest * request;
2074 bool found = false;
2075
2076 if ((type > UINT_MAX) || (value > UINT_MAX)) {
2077 return kIOReturnBadArgument;
2078 }
2079
2080 if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2081 DLOG("setAggressiveness(%x) %s = %u\n",
2082 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2083 } else {
2084 DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2085 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2086 }
2087
2088 request = IOMallocType(AggressivesRequest);
2089 request->options = options;
2090 request->dataType = kAggressivesRequestTypeRecord;
2091 request->data.record.type = (uint32_t) type;
2092 request->data.record.value = (uint32_t) value;
2093
2094 AGGRESSIVES_LOCK();
2095
2096 // Update disk quick spindown flag used by getAggressiveness().
2097 // Never merge requests with quick spindown flags set.
2098
2099 if (options & kAggressivesOptionQuickSpindownEnable) {
2100 gAggressivesState |= kAggressivesStateQuickSpindown;
2101 } else if (options & kAggressivesOptionQuickSpindownDisable) {
2102 gAggressivesState &= ~kAggressivesStateQuickSpindown;
2103 } else {
2104 // Coalesce requests with identical aggressives types.
2105 // Deal with callers that calls us too "aggressively".
2106
2107 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2108 {
2109 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2110 (entry->data.record.type == type) &&
2111 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2112 entry->data.record.value = (uint32_t) value;
2113 found = true;
2114 break;
2115 }
2116 }
2117 }
2118
2119 if (!found) {
2120 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2121 }
2122
2123 AGGRESSIVES_UNLOCK();
2124
2125 if (found) {
2126 IOFreeType(request, AggressivesRequest);
2127 }
2128
2129 if (options & kAggressivesOptionSynchronous) {
2130 handleAggressivesRequests(); // not truly synchronous
2131 } else {
2132 thread_call_enter(aggressivesThreadCall);
2133 }
2134
2135 return kIOReturnSuccess;
2136 }
2137
2138 //******************************************************************************
2139 // getAggressiveness
2140 //
2141 // Override IOService::setAggressiveness()
2142 // Fetch the aggressiveness factor with the given type.
2143 //******************************************************************************
2144
2145 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2146 IOPMrootDomain::getAggressiveness(
2147 unsigned long type,
2148 unsigned long * outLevel )
2149 {
2150 uint32_t value = 0;
2151 int source = 0;
2152
2153 if (!outLevel || (type > UINT_MAX)) {
2154 return kIOReturnBadArgument;
2155 }
2156
2157 AGGRESSIVES_LOCK();
2158
2159 // Disk quick spindown in effect, report value = 1
2160
2161 if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2162 (type == kPMMinutesToSpinDown)) {
2163 value = kAggressivesMinValue;
2164 source = 1;
2165 }
2166
2167 // Consult the pending request queue.
2168
2169 if (!source) {
2170 AggressivesRequest * entry;
2171
2172 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2173 {
2174 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2175 (entry->data.record.type == type) &&
2176 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2177 value = entry->data.record.value;
2178 source = 2;
2179 break;
2180 }
2181 }
2182 }
2183
2184 // Consult the backend records.
2185
2186 if (!source && aggressivesData) {
2187 AggressivesRecord * record;
2188 int i, count;
2189
2190 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2191 record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2192
2193 for (i = 0; i < count; i++, record++) {
2194 if (record->type == type) {
2195 value = record->value;
2196 source = 3;
2197 break;
2198 }
2199 }
2200 }
2201
2202 AGGRESSIVES_UNLOCK();
2203
2204 if (source) {
2205 *outLevel = (unsigned long) value;
2206 return kIOReturnSuccess;
2207 } else {
2208 DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2209 *outLevel = 0; // default return = 0, driver may not check for error
2210 return kIOReturnInvalid;
2211 }
2212 }
2213
2214 //******************************************************************************
2215 // joinAggressiveness
2216 //
2217 // Request from IOService to join future aggressiveness broadcasts.
2218 //******************************************************************************
2219
2220 IOReturn
joinAggressiveness(IOService * service)2221 IOPMrootDomain::joinAggressiveness(
2222 IOService * service )
2223 {
2224 AggressivesRequest * request;
2225
2226 if (!service || (service == this)) {
2227 return kIOReturnBadArgument;
2228 }
2229
2230 DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2231
2232 request = IOMallocType(AggressivesRequest);
2233 request->dataType = kAggressivesRequestTypeService;
2234 request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2235
2236 AGGRESSIVES_LOCK();
2237 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2238 AGGRESSIVES_UNLOCK();
2239
2240 thread_call_enter(aggressivesThreadCall);
2241
2242 return kIOReturnSuccess;
2243 }
2244
2245 //******************************************************************************
2246 // handleAggressivesRequests
2247 //
2248 // Backend thread processes all incoming aggressiveness requests in the queue.
2249 //******************************************************************************
2250
2251 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2252 handleAggressivesFunction(
2253 thread_call_param_t param1,
2254 thread_call_param_t param2 )
2255 {
2256 if (param1) {
2257 ((IOPMrootDomain *) param1)->handleAggressivesRequests();
2258 }
2259 }
2260
2261 void
handleAggressivesRequests(void)2262 IOPMrootDomain::handleAggressivesRequests( void )
2263 {
2264 AggressivesRecord * start;
2265 AggressivesRecord * record;
2266 AggressivesRequest * request;
2267 queue_head_t joinedQueue;
2268 int i, count;
2269 bool broadcast;
2270 bool found;
2271 bool pingSelf = false;
2272
2273 AGGRESSIVES_LOCK();
2274
2275 if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2276 queue_empty(&aggressivesQueue)) {
2277 goto unlock_done;
2278 }
2279
2280 gAggressivesState |= kAggressivesStateBusy;
2281 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2282 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2283
2284 do{
2285 broadcast = false;
2286 queue_init(&joinedQueue);
2287
2288 do{
2289 // Remove request from the incoming queue in FIFO order.
2290 queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2291 switch (request->dataType) {
2292 case kAggressivesRequestTypeRecord:
2293 // Update existing record if found.
2294 found = false;
2295 for (i = 0, record = start; i < count; i++, record++) {
2296 if (record->type == request->data.record.type) {
2297 found = true;
2298
2299 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2300 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2301 broadcast = true;
2302 record->flags |= (kAggressivesRecordFlagMinValue |
2303 kAggressivesRecordFlagModified);
2304 DLOG("disk spindown accelerated, was %u min\n",
2305 record->value);
2306 }
2307 } else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2308 if (record->flags & kAggressivesRecordFlagMinValue) {
2309 broadcast = true;
2310 record->flags |= kAggressivesRecordFlagModified;
2311 record->flags &= ~kAggressivesRecordFlagMinValue;
2312 DLOG("disk spindown restored to %u min\n",
2313 record->value);
2314 }
2315 } else if (record->value != request->data.record.value) {
2316 record->value = request->data.record.value;
2317 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2318 broadcast = true;
2319 record->flags |= kAggressivesRecordFlagModified;
2320 }
2321 }
2322 break;
2323 }
2324 }
2325
2326 // No matching record, append a new record.
2327 if (!found &&
2328 ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2329 AggressivesRecord newRecord;
2330
2331 newRecord.flags = kAggressivesRecordFlagModified;
2332 newRecord.type = request->data.record.type;
2333 newRecord.value = request->data.record.value;
2334 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2335 newRecord.flags |= kAggressivesRecordFlagMinValue;
2336 DLOG("disk spindown accelerated\n");
2337 }
2338
2339 aggressivesData->appendValue(newRecord);
2340
2341 // OSData may have switched to another (larger) buffer.
2342 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2343 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2344 broadcast = true;
2345 }
2346
2347 // Finished processing the request, release it.
2348 IOFreeType(request, AggressivesRequest);
2349 break;
2350
2351 case kAggressivesRequestTypeService:
2352 // synchronizeAggressives() will free request.
2353 queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2354 break;
2355
2356 default:
2357 panic("bad aggressives request type %x", request->dataType);
2358 break;
2359 }
2360 } while (!queue_empty(&aggressivesQueue));
2361
2362 // Release the lock to perform work, with busy flag set.
2363 if (!queue_empty(&joinedQueue) || broadcast) {
2364 AGGRESSIVES_UNLOCK();
2365 if (!queue_empty(&joinedQueue)) {
2366 synchronizeAggressives(&joinedQueue, start, count);
2367 }
2368 if (broadcast) {
2369 broadcastAggressives(start, count);
2370 }
2371 AGGRESSIVES_LOCK();
2372 }
2373
2374 // Remove the modified flag from all records.
2375 for (i = 0, record = start; i < count; i++, record++) {
2376 if ((record->flags & kAggressivesRecordFlagModified) &&
2377 ((record->type == kPMMinutesToDim) ||
2378 (record->type == kPMMinutesToSleep))) {
2379 pingSelf = true;
2380 }
2381
2382 record->flags &= ~kAggressivesRecordFlagModified;
2383 }
2384
2385 // Check the incoming queue again since new entries may have been
2386 // added while lock was released above.
2387 } while (!queue_empty(&aggressivesQueue));
2388
2389 gAggressivesState &= ~kAggressivesStateBusy;
2390
2391 unlock_done:
2392 AGGRESSIVES_UNLOCK();
2393
2394 // Root domain is interested in system and display sleep slider changes.
2395 // Submit a power event to handle those changes on the PM work loop.
2396
2397 if (pingSelf && pmPowerStateQueue) {
2398 pmPowerStateQueue->submitPowerEvent(
2399 kPowerEventPolicyStimulus,
2400 (void *) kStimulusAggressivenessChanged );
2401 }
2402 }
2403
2404 //******************************************************************************
2405 // synchronizeAggressives
2406 //
2407 // Push all known aggressiveness records to one or more IOService.
2408 //******************************************************************************
2409
2410 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2411 IOPMrootDomain::synchronizeAggressives(
2412 queue_head_t * joinedQueue,
2413 const AggressivesRecord * array,
2414 int count )
2415 {
2416 OSSharedPtr<IOService> service;
2417 AggressivesRequest * request;
2418 const AggressivesRecord * record;
2419 IOPMDriverCallEntry callEntry;
2420 uint32_t value;
2421 int i;
2422
2423 while (!queue_empty(joinedQueue)) {
2424 queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2425 if (request->dataType == kAggressivesRequestTypeService) {
2426 // retained by joinAggressiveness(), so take ownership
2427 service = os::move(request->data.service);
2428 } else {
2429 service.reset();
2430 }
2431
2432 IOFreeType(request, AggressivesRequest);
2433 request = NULL;
2434
2435 if (service) {
2436 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2437 for (i = 0, record = array; i < count; i++, record++) {
2438 value = record->value;
2439 if (record->flags & kAggressivesRecordFlagMinValue) {
2440 value = kAggressivesMinValue;
2441 }
2442
2443 _LOG("synchronizeAggressives 0x%x = %u to %s\n",
2444 record->type, value, service->getName());
2445 service->setAggressiveness(record->type, value);
2446 }
2447 service->deassertPMDriverCall(&callEntry);
2448 }
2449 }
2450 }
2451 }
2452
2453 //******************************************************************************
2454 // broadcastAggressives
2455 //
2456 // Traverse PM tree and call setAggressiveness() for records that have changed.
2457 //******************************************************************************
2458
2459 void
broadcastAggressives(const AggressivesRecord * array,int count)2460 IOPMrootDomain::broadcastAggressives(
2461 const AggressivesRecord * array,
2462 int count )
2463 {
2464 OSSharedPtr<IORegistryIterator> iter;
2465 IORegistryEntry *entry;
2466 OSSharedPtr<IORegistryEntry> child;
2467 IOPowerConnection *connect;
2468 IOService *service;
2469 const AggressivesRecord *record;
2470 IOPMDriverCallEntry callEntry;
2471 uint32_t value;
2472 int i;
2473
2474 iter = IORegistryIterator::iterateOver(
2475 this, gIOPowerPlane, kIORegistryIterateRecursively);
2476 if (iter) {
2477 do{
2478 // !! reset the iterator
2479 iter->reset();
2480 while ((entry = iter->getNextObject())) {
2481 connect = OSDynamicCast(IOPowerConnection, entry);
2482 if (!connect || !connect->getReadyFlag()) {
2483 continue;
2484 }
2485
2486 child = connect->copyChildEntry(gIOPowerPlane);
2487 if (child) {
2488 if ((service = OSDynamicCast(IOService, child.get()))) {
2489 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2490 for (i = 0, record = array; i < count; i++, record++) {
2491 if (record->flags & kAggressivesRecordFlagModified) {
2492 value = record->value;
2493 if (record->flags & kAggressivesRecordFlagMinValue) {
2494 value = kAggressivesMinValue;
2495 }
2496 _LOG("broadcastAggressives %x = %u to %s\n",
2497 record->type, value, service->getName());
2498 service->setAggressiveness(record->type, value);
2499 }
2500 }
2501 service->deassertPMDriverCall(&callEntry);
2502 }
2503 }
2504 }
2505 }
2506 }while (!entry && !iter->isValid());
2507 }
2508 }
2509
2510 //*****************************************
2511 // stackshot on power button press
2512 // ***************************************
2513 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2514 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2515 {
2516 /* Power button pressed during wake
2517 * Take a stackshot
2518 */
2519 DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2520 ((IOPMrootDomain *)us)->takeStackshot(false);
2521 }
2522
2523 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2524 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2525 {
2526 /* Power button released.
2527 * Delete any stackshot data
2528 */
2529 DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2530 ((IOPMrootDomain *)us)->deleteStackshot();
2531 }
2532 //*************************************************************************
2533 //
2534
2535 // MARK: -
2536 // MARK: System Sleep
2537
2538 //******************************************************************************
2539 // startIdleSleepTimer
2540 //
2541 //******************************************************************************
2542
2543 void
startIdleSleepTimer(uint32_t inMilliSeconds)2544 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2545 {
2546 AbsoluteTime deadline;
2547
2548 ASSERT_GATED();
2549 if (gNoIdleFlag) {
2550 DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2551 return;
2552 }
2553 if (inMilliSeconds) {
2554 if (inMilliSeconds < kMinimumTimeBeforeIdleSleep) {
2555 AbsoluteTime now;
2556 uint64_t nsec_since_wake;
2557 uint64_t msec_since_wake;
2558
2559 // Adjust idle timer so it will not expire until atleast kMinimumTimeBeforeIdleSleep milliseconds
2560 // after the most recent AP wake.
2561 clock_get_uptime(&now);
2562 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2563 absolutetime_to_nanoseconds(now, &nsec_since_wake);
2564 msec_since_wake = nsec_since_wake / NSEC_PER_MSEC;
2565
2566 if (msec_since_wake < kMinimumTimeBeforeIdleSleep) {
2567 uint32_t newIdleTimer = kMinimumTimeBeforeIdleSleep - (uint32_t)msec_since_wake;
2568
2569 // Ensure that our new idle timer is not less than inMilliSeconds,
2570 // as we should only be increasing the timer duration, not decreasing it
2571 if (newIdleTimer > inMilliSeconds) {
2572 DLOG("startIdleSleepTimer increasing timeout from %u to %u\n", inMilliSeconds, newIdleTimer);
2573 inMilliSeconds = newIdleTimer;
2574 }
2575 }
2576 }
2577 clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2578 thread_call_enter_delayed(extraSleepTimer, deadline);
2579 idleSleepTimerPending = true;
2580 } else {
2581 thread_call_enter(extraSleepTimer);
2582 }
2583 DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2584 }
2585
2586 //******************************************************************************
2587 // cancelIdleSleepTimer
2588 //
2589 //******************************************************************************
2590
2591 void
cancelIdleSleepTimer(void)2592 IOPMrootDomain::cancelIdleSleepTimer( void )
2593 {
2594 ASSERT_GATED();
2595 if (idleSleepTimerPending) {
2596 DLOG("idle timer cancelled\n");
2597 thread_call_cancel(extraSleepTimer);
2598 idleSleepTimerPending = false;
2599
2600 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2601 AbsoluteTime now;
2602 clock_usec_t microsecs;
2603 clock_get_uptime(&now);
2604 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2605 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
2606 if (assertOnWakeReport) {
2607 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2608 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2609 }
2610 }
2611 }
2612 }
2613
2614 //******************************************************************************
2615 // idleSleepTimerExpired
2616 //
2617 //******************************************************************************
2618
2619 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2620 idleSleepTimerExpired(
2621 thread_call_param_t us, thread_call_param_t )
2622 {
2623 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2624 }
2625
2626 //******************************************************************************
2627 // handleSleepTimerExpiration
2628 //
2629 // The time between the sleep idle timeout and the next longest one has elapsed.
2630 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2631 //******************************************************************************
2632
2633 void
handleSleepTimerExpiration(void)2634 IOPMrootDomain::handleSleepTimerExpiration( void )
2635 {
2636 if (!gIOPMWorkLoop->inGate()) {
2637 gIOPMWorkLoop->runAction(
2638 OSMemberFunctionCast(IOWorkLoop::Action, this,
2639 &IOPMrootDomain::handleSleepTimerExpiration),
2640 this);
2641 return;
2642 }
2643
2644 DLOG("sleep timer expired\n");
2645 ASSERT_GATED();
2646
2647 idleSleepTimerPending = false;
2648 setQuickSpinDownTimeout();
2649 adjustPowerState(true);
2650 }
2651
2652 //******************************************************************************
2653 // getTimeToIdleSleep
2654 //
2655 // Returns number of milliseconds left before going into idle sleep.
2656 // Caller has to make sure that idle sleep is allowed at the time of calling
2657 // this function
2658 //******************************************************************************
2659
2660 uint32_t
getTimeToIdleSleep(void)2661 IOPMrootDomain::getTimeToIdleSleep( void )
2662 {
2663 AbsoluteTime now, lastActivityTime;
2664 uint64_t nanos;
2665 uint32_t minutesSinceUserInactive = 0;
2666 uint32_t sleepDelay = 0;
2667
2668 if (!idleSleepEnabled) {
2669 return 0xffffffff;
2670 }
2671
2672 if (userActivityTime) {
2673 lastActivityTime = userActivityTime;
2674 } else {
2675 lastActivityTime = userBecameInactiveTime;
2676 }
2677
2678 // Ignore any lastActivityTime that predates the last system wake.
2679 // The goal is to avoid a sudden idle sleep right after a dark wake
2680 // due to sleepDelay=0 computed below. The alternative 60s minimum
2681 // timeout should be large enough to allow dark wake to complete,
2682 // at which point the idle timer will be promptly cancelled.
2683 clock_get_uptime(&now);
2684 if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2685 (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2686 SUB_ABSOLUTETIME(&now, &lastActivityTime);
2687 absolutetime_to_nanoseconds(now, &nanos);
2688 minutesSinceUserInactive = nanos / (60000000000ULL);
2689
2690 if (minutesSinceUserInactive >= sleepSlider) {
2691 sleepDelay = 0;
2692 } else {
2693 sleepDelay = sleepSlider - minutesSinceUserInactive;
2694 }
2695 } else {
2696 DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2697 lastActivityTime, now, gIOLastWakeAbsTime);
2698 sleepDelay = sleepSlider;
2699 }
2700
2701 DLOG("user inactive %u min, time to idle sleep %u min\n",
2702 minutesSinceUserInactive, sleepDelay);
2703
2704 return sleepDelay * 60 * 1000;
2705 }
2706
2707 //******************************************************************************
2708 // setQuickSpinDownTimeout
2709 //
2710 //******************************************************************************
2711
2712 void
setQuickSpinDownTimeout(void)2713 IOPMrootDomain::setQuickSpinDownTimeout( void )
2714 {
2715 ASSERT_GATED();
2716 setAggressiveness(
2717 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2718 }
2719
2720 //******************************************************************************
2721 // restoreUserSpinDownTimeout
2722 //
2723 //******************************************************************************
2724
2725 void
restoreUserSpinDownTimeout(void)2726 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2727 {
2728 ASSERT_GATED();
2729 setAggressiveness(
2730 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2731 }
2732
2733 //******************************************************************************
2734 // sleepSystem
2735 //
2736 //******************************************************************************
2737
2738 /* public */
2739 IOReturn
sleepSystem(void)2740 IOPMrootDomain::sleepSystem( void )
2741 {
2742 return sleepSystemOptions(NULL);
2743 }
2744
2745 /* private */
2746 IOReturn
sleepSystemOptions(OSDictionary * options)2747 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2748 {
2749 OSObject *obj = NULL;
2750 OSString *reason = NULL;
2751 /* sleepSystem is a public function, and may be called by any kernel driver.
2752 * And that's bad - drivers should sleep the system by calling
2753 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2754 *
2755 * Note that user space app calls to IOPMSleepSystem() will also travel
2756 * this code path and thus be correctly identified as software sleeps.
2757 */
2758
2759 if (options && options->getObject("OSSwitch")) {
2760 // Log specific sleep cause for OS Switch hibernation
2761 return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2762 }
2763
2764 if (options && (obj = options->getObject("Sleep Reason"))) {
2765 reason = OSDynamicCast(OSString, obj);
2766 if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2767 return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2768 }
2769 if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2770 return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2771 }
2772 }
2773
2774 return privateSleepSystem( kIOPMSleepReasonSoftware);
2775 }
2776
2777 /* private */
2778 IOReturn
privateSleepSystem(uint32_t sleepReason)2779 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2780 {
2781 /* Called from both gated and non-gated context */
2782
2783 if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2784 return kIOReturnNotPermitted;
2785 }
2786
2787 pmPowerStateQueue->submitPowerEvent(
2788 kPowerEventPolicyStimulus,
2789 (void *) kStimulusDemandSystemSleep,
2790 sleepReason);
2791
2792 return kIOReturnSuccess;
2793 }
2794
2795 //******************************************************************************
2796 // powerChangeDone
2797 //
2798 // This overrides powerChangeDone in IOService.
2799 //******************************************************************************
2800 void
powerChangeDone(unsigned long previousPowerState)2801 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2802 {
2803 #if !__i386__ && !__x86_64__
2804 uint64_t timeSinceReset = 0;
2805 #endif
2806 uint64_t now;
2807 unsigned long newState;
2808 clock_sec_t secs;
2809 clock_usec_t microsecs;
2810 uint32_t lastDebugWakeSeconds;
2811 clock_sec_t adjWakeTime;
2812 IOPMCalendarStruct nowCalendar;
2813
2814 ASSERT_GATED();
2815 newState = getPowerState();
2816 DLOG("PowerChangeDone: %s->%s\n",
2817 getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2818
2819 if (previousPowerState == newState) {
2820 return;
2821 }
2822
2823 notifierThread = current_thread();
2824 switch (getPowerState()) {
2825 case SLEEP_STATE: {
2826 if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2827 secs = 0;
2828 microsecs = 0;
2829 PEGetUTCTimeOfDay(&secs, µsecs);
2830
2831 adjWakeTime = 0;
2832 if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2833 IOLog("use _calendarWakeAlarmUTC\n");
2834 adjWakeTime = _calendarWakeAlarmUTC;
2835 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2836 IOLog("accelerate _aotWakeTime for exit\n");
2837 adjWakeTime = secs;
2838 } else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2839 IOLog("accelerate _aotWakeTime for assertion\n");
2840 adjWakeTime = secs;
2841 }
2842 if (adjWakeTime) {
2843 IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2844 }
2845
2846 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2847 IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2848
2849 IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2850 assert(kIOReturnSuccess == ret);
2851 }
2852 if (_aotLastWakeTime) {
2853 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2854 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2855 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2856 gWakeReasonString,
2857 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2858 }
2859 }
2860 _aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2861 if (_aotTimerScheduled) {
2862 _aotTimerES->cancelTimeout();
2863 _aotTimerScheduled = false;
2864 }
2865 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2866
2867 // re-enable this timer for next sleep
2868 cancelIdleSleepTimer();
2869
2870 if (clamshellExists) {
2871 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2872 if (gClamshellFlags & kClamshell_WAR_58009435) {
2873 // Disable clamshell sleep until system has completed full wake.
2874 // This prevents a system sleep request (due to a clamshell close)
2875 // from being queued until the end of system full wake - even if
2876 // other clamshell disable bits outside of our control is wrong.
2877 setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2878 }
2879 #endif
2880
2881 // Log the last known clamshell state before system sleep
2882 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2883 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2884 desktopMode, acAdaptorConnected);
2885 }
2886
2887 clock_get_calendar_absolute_and_microtime(&secs, µsecs, &now);
2888 logtime(secs);
2889 gIOLastSleepTime.tv_sec = secs;
2890 gIOLastSleepTime.tv_usec = microsecs;
2891 if (!_aotLastWakeTime) {
2892 gIOLastUserSleepTime = gIOLastSleepTime;
2893 }
2894
2895 gIOLastWakeTime.tv_sec = 0;
2896 gIOLastWakeTime.tv_usec = 0;
2897 gIOLastSleepAbsTime = now;
2898
2899 if (wake2DarkwakeDelay && sleepDelaysReport) {
2900 clock_sec_t wake2DarkwakeSecs, darkwake2SleepSecs;
2901 // Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2902
2903 SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2904 absolutetime_to_microtime(now, &darkwake2SleepSecs, µsecs);
2905 absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, µsecs);
2906 HISTREPORT_TALLYVALUE(sleepDelaysReport,
2907 (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2908
2909 DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2910 wake2DarkwakeDelay = 0;
2911 }
2912 #if HIBERNATION
2913 LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2914 #if (DEVELOPMENT || DEBUG)
2915 record_system_event(SYSTEM_EVENT_TYPE_INFO,
2916 SYSTEM_EVENT_SUBSYSTEM_PMRD,
2917 "System State",
2918 gIOHibernateState ? "Enter Hibernate" : "Enter Sleep"
2919 );
2920 #endif /* DEVELOPMENT || DEBUG */
2921 IOHibernateSystemHasSlept();
2922
2923 evaluateSystemSleepPolicyFinal();
2924 #else
2925 LOG("System Sleep\n");
2926 #if (DEVELOPMENT || DEBUG)
2927 record_system_event(SYSTEM_EVENT_TYPE_INFO,
2928 SYSTEM_EVENT_SUBSYSTEM_PMRD,
2929 "System State", "Enter Sleep");
2930 #endif /* DEVELOPMENT || DEBUG */
2931 #endif
2932 if (thermalWarningState) {
2933 OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2934 if (event) {
2935 systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2936 }
2937 }
2938 assertOnWakeSecs = 0;
2939 lowBatteryCondition = false;
2940 thermalEmergencyState = false;
2941
2942 #if DEVELOPMENT || DEBUG
2943 extern int g_should_log_clock_adjustments;
2944 if (g_should_log_clock_adjustments) {
2945 clock_sec_t secs = 0;
2946 clock_usec_t microsecs = 0;
2947 uint64_t now_b = mach_absolute_time();
2948
2949 secs = 0;
2950 microsecs = 0;
2951 PEGetUTCTimeOfDay(&secs, µsecs);
2952
2953 uint64_t now_a = mach_absolute_time();
2954 os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2955 __func__, (unsigned long)secs, microsecs, now_b, now_a);
2956 }
2957 #endif
2958
2959 getPlatform()->sleepKernel();
2960
2961 // The CPU(s) are off at this point,
2962 // Code will resume execution here upon wake.
2963
2964 clock_get_uptime(&gIOLastWakeAbsTime);
2965 IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2966 #if DEVELOPMENT || DEBUG
2967 record_system_event(SYSTEM_EVENT_TYPE_INFO,
2968 SYSTEM_EVENT_SUBSYSTEM_PMRD,
2969 "System State", "Waking Up"
2970 );
2971 #endif /* DEVELOPMENT || DEBUG */
2972 _highestCapability = 0;
2973
2974 #if HIBERNATION
2975 IOHibernateSystemWake();
2976 #endif
2977
2978 // sleep transition complete
2979 gSleepOrShutdownPending = 0;
2980
2981 // trip the reset of the calendar clock
2982 clock_wakeup_calendar();
2983 clock_get_calendar_microtime(&secs, µsecs);
2984 gIOLastWakeTime.tv_sec = secs;
2985 gIOLastWakeTime.tv_usec = microsecs;
2986
2987 // aot
2988 if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2989 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2990 secs = 0;
2991 microsecs = 0;
2992 PEGetUTCTimeOfDay(&secs, µsecs);
2993 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2994 IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2995 _aotMetrics->sleepCount++;
2996 _aotLastWakeTime = gIOLastWakeAbsTime;
2997 if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2998 _aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
2999 = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
3000 _aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
3001 = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
3002 }
3003
3004 if (_aotTestTime) {
3005 if (_aotWakeTimeUTC <= secs) {
3006 _aotTestTime = _aotTestTime + _aotTestInterval;
3007 }
3008 setWakeTime(_aotTestTime);
3009 }
3010 }
3011
3012 #if HIBERNATION
3013 LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
3014 #endif
3015
3016 lastSleepReason = 0;
3017
3018 lastDebugWakeSeconds = _debugWakeSeconds;
3019 _debugWakeSeconds = 0;
3020 _scheduledAlarmMask = 0;
3021 _nextScheduledAlarmType = NULL;
3022
3023 darkWakeExit = false;
3024 darkWakePowerClamped = false;
3025 darkWakePostTickle = false;
3026 darkWakeHibernateError = false;
3027 darkWakeToSleepASAP = true;
3028 darkWakeLogClamp = true;
3029 sleepTimerMaintenance = false;
3030 sleepToStandby = false;
3031 wranglerTickled = false;
3032 userWasActive = false;
3033 isRTCAlarmWake = false;
3034 clamshellIgnoreClose = false;
3035 fullWakeReason = kFullWakeReasonNone;
3036 idleSleepRevertible = true;
3037
3038 #if defined(__i386__) || defined(__x86_64__)
3039 kdebugTrace(kPMLogSystemWake, 0, 0, 0);
3040
3041 OSSharedPtr<OSObject> wakeTypeProp = copyProperty(kIOPMRootDomainWakeTypeKey);
3042 OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
3043 OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
3044 OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
3045
3046 if (wakeReason && (wakeReason->getLength() >= 2) &&
3047 gWakeReasonString[0] == '\0') {
3048 WAKEEVENT_LOCK();
3049 // Until the platform driver can claim its wake reasons
3050 strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
3051 sizeof(gWakeReasonString));
3052 if (!gWakeReasonSysctlRegistered) {
3053 gWakeReasonSysctlRegistered = true;
3054 }
3055 WAKEEVENT_UNLOCK();
3056 }
3057
3058 if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
3059 lowBatteryCondition = true;
3060 darkWakeMaintenance = true;
3061 } else {
3062 #if HIBERNATION
3063 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3064 OSNumber * hibOptions = OSDynamicCast( OSNumber, hibOptionsProp.get());
3065 if (hibernateAborted || ((hibOptions &&
3066 !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
3067 // Hibernate aborted, or EFI brought up graphics
3068 darkWakeExit = true;
3069 if (hibernateAborted) {
3070 DLOG("Hibernation aborted\n");
3071 } else {
3072 DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3073 }
3074 } else
3075 #endif
3076 if (wakeType && (
3077 wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3078 wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3079 // User wake or RTC alarm
3080 darkWakeExit = true;
3081 if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3082 isRTCAlarmWake = true;
3083 }
3084 } else if (wakeType &&
3085 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3086 // SMC standby timer trumps SleepX
3087 darkWakeMaintenance = true;
3088 sleepTimerMaintenance = true;
3089 } else if ((lastDebugWakeSeconds != 0) &&
3090 ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3091 // SleepX before maintenance
3092 darkWakeExit = true;
3093 } else if (wakeType &&
3094 wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3095 darkWakeMaintenance = true;
3096 } else if (wakeType &&
3097 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3098 darkWakeMaintenance = true;
3099 darkWakeSleepService = true;
3100 #if HIBERNATION
3101 if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3102 sleepToStandby = true;
3103 }
3104 #endif
3105 } else if (wakeType &&
3106 wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3107 darkWakeMaintenance = true;
3108 darkWakeHibernateError = true;
3109 } else {
3110 // Unidentified wake source, resume to full wake if debug
3111 // alarm is pending.
3112
3113 if (lastDebugWakeSeconds &&
3114 (!wakeReason || wakeReason->isEqualTo(""))) {
3115 darkWakeExit = true;
3116 }
3117 }
3118 }
3119
3120 if (darkWakeExit) {
3121 darkWakeToSleepASAP = false;
3122 fullWakeReason = kFullWakeReasonLocalUser;
3123 reportUserInput();
3124 } else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3125 handleSetDisplayPowerOn(true);
3126 } else if (!darkWakeMaintenance) {
3127 // Early/late tickle for non-maintenance wake.
3128 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3129 darkWakePostTickle = true;
3130 }
3131 }
3132 #else /* !__i386__ && !__x86_64__ */
3133 timeSinceReset = ml_get_time_since_reset();
3134 kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3135
3136 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3137 wranglerTickled = true;
3138 fullWakeReason = kFullWakeReasonLocalUser;
3139 requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3140 } else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3141 isRTCAlarmWake = true;
3142 fullWakeReason = kFullWakeReasonLocalUser;
3143 requestUserActive(this, "RTC debug alarm");
3144 } else {
3145 #if HIBERNATION
3146 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3147 OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3148 if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3149 fullWakeReason = kFullWakeReasonLocalUser;
3150 requestUserActive(this, "hibernate user wake");
3151 }
3152 #endif
3153 }
3154
3155 // stay awake for at least 30 seconds
3156 startIdleSleepTimer(30 * 1000);
3157 #endif
3158 sleepCnt++;
3159
3160 thread_call_enter(updateConsoleUsersEntry);
3161
3162 // Skip AOT_STATE if we are waking up from an RTC timer.
3163 // This check needs to be done after the epoch change is processed
3164 // and before the changePowerStateWithTagToPriv() call below.
3165 WAKEEVENT_LOCK();
3166 aotShouldExit(false);
3167 WAKEEVENT_UNLOCK();
3168
3169 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3170 break;
3171 }
3172 #if !__i386__ && !__x86_64__
3173 case ON_STATE:
3174 case AOT_STATE:
3175 {
3176 DLOG("Force re-evaluating aggressiveness\n");
3177 /* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3178 pmPowerStateQueue->submitPowerEvent(
3179 kPowerEventPolicyStimulus,
3180 (void *) kStimulusNoIdleSleepPreventers );
3181
3182 // After changing to ON_STATE, invalidate any previously queued
3183 // request to change to a state less than ON_STATE. This isn't
3184 // necessary for AOT_STATE or if the device has only one running
3185 // state since the changePowerStateToPriv() issued at the tail
3186 // end of SLEEP_STATE case should take care of that.
3187 if (getPowerState() == ON_STATE) {
3188 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3189 }
3190 break;
3191 }
3192 #endif /* !__i386__ && !__x86_64__ */
3193 }
3194 notifierThread = NULL;
3195 }
3196
3197 //******************************************************************************
3198 // requestPowerDomainState
3199 //
3200 // Extend implementation in IOService. Running on PM work loop thread.
3201 //******************************************************************************
3202
3203 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3204 IOPMrootDomain::requestPowerDomainState(
3205 IOPMPowerFlags childDesire,
3206 IOPowerConnection * childConnection,
3207 unsigned long specification )
3208 {
3209 // Idle and system sleep prevention flags affects driver desire.
3210 // Children desire are irrelevant so they are cleared.
3211
3212 return super::requestPowerDomainState(0, childConnection, specification);
3213 }
3214
3215
3216 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3217 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3218 {
3219 if (!preventers->getCount()) {
3220 return;
3221 }
3222
3223 char *buf_iter = buf + strlen(buf);
3224 char *buf_end = buf + buf_size;
3225
3226 OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3227 OSObject *obj = NULL;
3228
3229 while ((obj = iterator->getNextObject())) {
3230 IOService *srv = OSDynamicCast(IOService, obj);
3231 if (buf_iter < buf_end) {
3232 buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3233 } else {
3234 DLOG("Print buffer exhausted for sleep preventers list\n");
3235 break;
3236 }
3237 }
3238 }
3239
3240 //******************************************************************************
3241 // updatePreventIdleSleepList
3242 //
3243 // Called by IOService on PM work loop.
3244 // Returns true if PM policy recognized the driver's desire to prevent idle
3245 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3246 //******************************************************************************
3247
3248 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3249 IOPMrootDomain::updatePreventIdleSleepList(
3250 IOService * service, bool addNotRemove)
3251 {
3252 unsigned int oldCount;
3253
3254 oldCount = idleSleepPreventersCount();
3255 return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3256 }
3257
3258 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3259 IOPMrootDomain::updatePreventIdleSleepListInternal(
3260 IOService * service, bool addNotRemove, unsigned int oldCount)
3261 {
3262 unsigned int newCount;
3263
3264 ASSERT_GATED();
3265
3266 #if defined(XNU_TARGET_OS_OSX)
3267 // Only the display wrangler and no-idle-sleep kernel assertions
3268 // can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3269 // reported by drivers in their power state table is ignored.
3270 if (service && (service != wrangler) && (service != this)) {
3271 return false;
3272 }
3273 #endif
3274
3275 if (service) {
3276 if (addNotRemove) {
3277 preventIdleSleepList->setObject(service);
3278 DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3279 service->getName(), preventIdleSleepList->getCount());
3280 } else if (preventIdleSleepList->member(service)) {
3281 preventIdleSleepList->removeObject(service);
3282 DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3283 service->getName(), preventIdleSleepList->getCount());
3284 }
3285
3286 if (preventIdleSleepList->getCount()) {
3287 char buf[256] = "Idle Sleep Preventers:";
3288 makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3289 DLOG("%s\n", buf);
3290 }
3291 }
3292
3293 newCount = idleSleepPreventersCount();
3294
3295 if ((oldCount == 0) && (newCount != 0)) {
3296 // Driver added to empty prevent list.
3297 // Update the driver desire to prevent idle sleep.
3298 // Driver desire does not prevent demand sleep.
3299
3300 changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3301 } else if ((oldCount != 0) && (newCount == 0)) {
3302 // Last driver removed from prevent list.
3303 // Drop the driver clamp to allow idle sleep.
3304
3305 changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3306 evaluatePolicy( kStimulusNoIdleSleepPreventers );
3307 }
3308 messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3309 &newCount, sizeof(newCount));
3310
3311 #if defined(XNU_TARGET_OS_OSX)
3312 if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3313 DLOG("Cannot cancel idle sleep\n");
3314 return false; // do not idle-cancel
3315 }
3316 #endif
3317
3318 return true;
3319 }
3320
3321 //******************************************************************************
3322 // startSpinDump
3323 //******************************************************************************
3324
3325 void
startSpinDump(uint32_t spindumpKind)3326 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3327 {
3328 messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3329 }
3330
3331 //******************************************************************************
3332 // preventSystemSleepListUpdate
3333 //
3334 // Called by IOService on PM work loop.
3335 //******************************************************************************
3336
3337 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3338 IOPMrootDomain::updatePreventSystemSleepList(
3339 IOService * service, bool addNotRemove )
3340 {
3341 unsigned int oldCount, newCount;
3342
3343 ASSERT_GATED();
3344 if (this == service) {
3345 return;
3346 }
3347
3348 oldCount = preventSystemSleepList->getCount();
3349 if (addNotRemove) {
3350 preventSystemSleepList->setObject(service);
3351 DLOG("Added %s to system sleep preventers list (Total %u)\n",
3352 service->getName(), preventSystemSleepList->getCount());
3353 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3354 AbsoluteTime now;
3355 clock_usec_t microsecs;
3356 clock_get_uptime(&now);
3357 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3358 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
3359 if (assertOnWakeReport) {
3360 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3361 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3362 }
3363 }
3364 } else if (preventSystemSleepList->member(service)) {
3365 preventSystemSleepList->removeObject(service);
3366 DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3367 service->getName(), preventSystemSleepList->getCount());
3368
3369 if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3370 // Lost all system sleep preventers.
3371 // Send stimulus if system sleep was blocked, and is in dark wake.
3372 evaluatePolicy( kStimulusDarkWakeEvaluate );
3373 }
3374 }
3375
3376 newCount = preventSystemSleepList->getCount();
3377 if (newCount) {
3378 char buf[256] = "System Sleep Preventers:";
3379 makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3380 DLOG("%s\n", buf);
3381 }
3382
3383 messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3384 &newCount, sizeof(newCount));
3385 }
3386
3387 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3388 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3389 {
3390 OSSharedPtr<OSCollectionIterator> iterator;
3391 OSObject *object = NULL;
3392 OSSharedPtr<OSArray> array;
3393
3394 if (!gIOPMWorkLoop->inGate()) {
3395 gIOPMWorkLoop->runAction(
3396 OSMemberFunctionCast(IOWorkLoop::Action, this,
3397 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3398 this, (void *)idleSleepList, (void *)systemSleepList);
3399 return;
3400 }
3401
3402 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3403 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3404 array = OSArray::withCapacity(5);
3405
3406 if (iterator && array) {
3407 while ((object = iterator->getNextObject())) {
3408 IOService *service = OSDynamicCast(IOService, object);
3409 if (service) {
3410 OSSharedPtr<const OSSymbol> name = service->copyName();
3411 if (name) {
3412 array->setObject(name.get());
3413 }
3414 }
3415 }
3416 }
3417 *idleSleepList = array.detach();
3418 }
3419
3420 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3421 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3422 array = OSArray::withCapacity(5);
3423
3424 if (iterator && array) {
3425 while ((object = iterator->getNextObject())) {
3426 IOService *service = OSDynamicCast(IOService, object);
3427 if (service) {
3428 OSSharedPtr<const OSSymbol> name = service->copyName();
3429 if (name) {
3430 array->setObject(name.get());
3431 }
3432 }
3433 }
3434 }
3435 *systemSleepList = array.detach();
3436 }
3437 }
3438
3439 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3440 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3441 {
3442 OSSharedPtr<OSCollectionIterator> iterator;
3443 OSObject *object = NULL;
3444 OSSharedPtr<OSArray> array;
3445
3446 if (!gIOPMWorkLoop->inGate()) {
3447 gIOPMWorkLoop->runAction(
3448 OSMemberFunctionCast(IOWorkLoop::Action, this,
3449 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3450 this, (void *)idleSleepList, (void *)systemSleepList);
3451 return;
3452 }
3453
3454 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3455 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3456 array = OSArray::withCapacity(5);
3457
3458 if (iterator && array) {
3459 while ((object = iterator->getNextObject())) {
3460 IOService *service = OSDynamicCast(IOService, object);
3461 if (service) {
3462 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3463 OSSharedPtr<const OSSymbol> name = service->copyName();
3464 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3465 if (dict && name && id) {
3466 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3467 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3468 array->setObject(dict.get());
3469 }
3470 }
3471 }
3472 }
3473 *idleSleepList = array.detach();
3474 }
3475
3476 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3477 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3478 array = OSArray::withCapacity(5);
3479
3480 if (iterator && array) {
3481 while ((object = iterator->getNextObject())) {
3482 IOService *service = OSDynamicCast(IOService, object);
3483 if (service) {
3484 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3485 OSSharedPtr<const OSSymbol> name = service->copyName();
3486 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3487 if (dict && name && id) {
3488 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3489 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3490 array->setObject(dict.get());
3491 }
3492 }
3493 }
3494 }
3495 *systemSleepList = array.detach();
3496 }
3497 }
3498
3499 //******************************************************************************
3500 // tellChangeDown
3501 //
3502 // Override the superclass implementation to send a different message type.
3503 //******************************************************************************
3504
3505 bool
tellChangeDown(unsigned long stateNum)3506 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3507 {
3508 DLOG("tellChangeDown %s->%s\n",
3509 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3510
3511 if (SLEEP_STATE == stateNum) {
3512 // Legacy apps were already told in the full->dark transition
3513 if (!ignoreTellChangeDown) {
3514 tracePoint( kIOPMTracePointSleepApplications );
3515 } else {
3516 tracePoint( kIOPMTracePointSleepPriorityClients );
3517 }
3518 }
3519
3520 if (!ignoreTellChangeDown) {
3521 userActivityAtSleep = userActivityCount;
3522 DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3523
3524 if (SLEEP_STATE == stateNum) {
3525 hibernateAborted = false;
3526
3527 // Direct callout into OSKext so it can disable kext unloads
3528 // during sleep/wake to prevent deadlocks.
3529 OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3530
3531 IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3532
3533 // Two change downs are sent by IOServicePM. Ignore the 2nd.
3534 // But tellClientsWithResponse() must be called for both.
3535 ignoreTellChangeDown = true;
3536 }
3537 }
3538
3539 return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3540 }
3541
3542 //******************************************************************************
3543 // askChangeDown
3544 //
3545 // Override the superclass implementation to send a different message type.
3546 // This must be idle sleep since we don't ask during any other power change.
3547 //******************************************************************************
3548
3549 bool
askChangeDown(unsigned long stateNum)3550 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3551 {
3552 DLOG("askChangeDown %s->%s\n",
3553 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3554
3555 // Don't log for dark wake entry
3556 if (kSystemTransitionSleep == _systemTransitionType) {
3557 tracePoint( kIOPMTracePointSleepApplications );
3558 }
3559
3560 return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3561 }
3562
3563 //******************************************************************************
3564 // askChangeDownDone
3565 //
3566 // An opportunity for root domain to cancel the power transition,
3567 // possibily due to an assertion created by powerd in response to
3568 // kIOMessageCanSystemSleep.
3569 //
3570 // Idle sleep:
3571 // full -> dark wake transition
3572 // 1. Notify apps and powerd with kIOMessageCanSystemSleep
3573 // 2. askChangeDownDone()
3574 // dark -> sleep transition
3575 // 1. Notify powerd with kIOMessageCanSystemSleep
3576 // 2. askChangeDownDone()
3577 //
3578 // Demand sleep:
3579 // full -> dark wake transition
3580 // 1. Notify powerd with kIOMessageCanSystemSleep
3581 // 2. askChangeDownDone()
3582 // dark -> sleep transition
3583 // 1. Notify powerd with kIOMessageCanSystemSleep
3584 // 2. askChangeDownDone()
3585 //******************************************************************************
3586
3587 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3588 IOPMrootDomain::askChangeDownDone(
3589 IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3590 {
3591 DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3592 *inOutChangeFlags, *cancel,
3593 _systemTransitionType,
3594 _currentCapability, _pendingCapability);
3595
3596 if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3597 // Dark->Sleep transition.
3598 // Check if there are any deny sleep assertions.
3599 // lastSleepReason already set by handleOurPowerChangeStart()
3600
3601 if (!checkSystemCanSleep(lastSleepReason)) {
3602 // Cancel dark wake to sleep transition.
3603 // Must re-scan assertions upon entering dark wake.
3604
3605 *cancel = true;
3606 DLOG("cancel dark->sleep\n");
3607 }
3608 if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3609 uint64_t now = mach_continuous_time();
3610 if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3611 && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3612 *cancel = true;
3613 IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3614 }
3615 }
3616 }
3617 }
3618
3619 //******************************************************************************
3620 // systemDidNotSleep
3621 //
3622 // Work common to both canceled or aborted sleep.
3623 //******************************************************************************
3624
3625 void
systemDidNotSleep(void)3626 IOPMrootDomain::systemDidNotSleep( void )
3627 {
3628 // reset console lock state
3629 thread_call_enter(updateConsoleUsersEntry);
3630
3631 if (idleSleepEnabled) {
3632 if (!wrangler) {
3633 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3634 startIdleSleepTimer(kIdleSleepRetryInterval);
3635 #else
3636 startIdleSleepTimer(idleMilliSeconds);
3637 #endif
3638 } else if (!userIsActive) {
3639 // Manually start the idle sleep timer besides waiting for
3640 // the user to become inactive.
3641 startIdleSleepTimer(kIdleSleepRetryInterval);
3642 }
3643 }
3644
3645 preventTransitionToUserActive(false);
3646 IOService::setAdvisoryTickleEnable( true );
3647 idleSleepRevertible = true;
3648
3649 // After idle revert and cancel, send a did-change message to powerd
3650 // to balance the previous will-change message. Kernel clients do not
3651 // need this since sleep cannot be canceled once they are notified.
3652
3653 if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3654 (_pendingCapability != _currentCapability) &&
3655 ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3656 // Differs from a real capability gain change where notifyRef != 0,
3657 // but it is zero here since no response is expected.
3658
3659 IOPMSystemCapabilityChangeParameters params;
3660
3661 bzero(¶ms, sizeof(params));
3662 params.fromCapabilities = _pendingCapability;
3663 params.toCapabilities = _currentCapability;
3664 params.changeFlags = kIOPMSystemCapabilityDidChange;
3665
3666 DLOG("MESG cap %x->%x did change\n",
3667 params.fromCapabilities, params.toCapabilities);
3668 messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3669 ¶ms, sizeof(params));
3670 }
3671 }
3672
3673 //******************************************************************************
3674 // tellNoChangeDown
3675 //
3676 // Notify registered applications and kernel clients that we are not dropping
3677 // power.
3678 //
3679 // We override the superclass implementation so we can send a different message
3680 // type to the client or application being notified.
3681 //
3682 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3683 //******************************************************************************
3684
3685 void
tellNoChangeDown(unsigned long stateNum)3686 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3687 {
3688 DLOG("tellNoChangeDown %s->%s\n",
3689 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3690
3691 // Sleep canceled, clear the sleep trace point.
3692 tracePoint(kIOPMTracePointSystemUp);
3693
3694 systemDidNotSleep();
3695 return tellClients( kIOMessageSystemWillNotSleep );
3696 }
3697
3698 //******************************************************************************
3699 // tellChangeUp
3700 //
3701 // Notify registered applications and kernel clients that we are raising power.
3702 //
3703 // We override the superclass implementation so we can send a different message
3704 // type to the client or application being notified.
3705 //******************************************************************************
3706
3707 void
tellChangeUp(unsigned long stateNum)3708 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3709 {
3710 DLOG("tellChangeUp %s->%s\n",
3711 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3712
3713 ignoreTellChangeDown = false;
3714
3715 if (stateNum == ON_STATE) {
3716 // Direct callout into OSKext so it can disable kext unloads
3717 // during sleep/wake to prevent deadlocks.
3718 OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3719
3720 // Notify platform that sleep was cancelled or resumed.
3721 getPlatform()->callPlatformFunction(
3722 sleepMessagePEFunction.get(), false,
3723 (void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3724 NULL, NULL, NULL);
3725
3726 if (getPowerState() == ON_STATE) {
3727 // Sleep was cancelled by idle cancel or revert
3728 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3729 // rdar://problem/50363791
3730 // If system is in dark wake and sleep is cancelled, do not
3731 // send SystemWillPowerOn/HasPoweredOn messages to kernel
3732 // priority clients. They haven't yet seen a SystemWillSleep
3733 // message before the cancellation. So make sure the kernel
3734 // client bit is cleared in _systemMessageClientMask before
3735 // invoking the tellClients() below. This bit may have been
3736 // set by handleOurPowerChangeStart() anticipating a successful
3737 // sleep and setting the filter mask ahead of time allows the
3738 // SystemWillSleep message to go through.
3739 _systemMessageClientMask &= ~kSystemMessageClientKernel;
3740 }
3741
3742 systemDidNotSleep();
3743 tellClients( kIOMessageSystemWillPowerOn );
3744 }
3745
3746 tracePoint( kIOPMTracePointWakeApplications );
3747 tellClients( kIOMessageSystemHasPoweredOn );
3748 } else if (stateNum == AOT_STATE) {
3749 if (getPowerState() == AOT_STATE) {
3750 // Sleep was cancelled by idle cancel or revert
3751 startIdleSleepTimer(idleMilliSeconds);
3752 }
3753 }
3754 }
3755
3756 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3757 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3758 ((params)->fromCapabilities & (flag)) && \
3759 (((params)->toCapabilities & (flag)) == 0))
3760
3761 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3762 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3763 ((params)->toCapabilities & (flag)) && \
3764 (((params)->fromCapabilities & (flag)) == 0))
3765
3766 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3767 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3768 ((params)->fromCapabilities & (flag)) && \
3769 (((params)->toCapabilities & (flag)) == 0))
3770
3771 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3772 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3773 ((params)->toCapabilities & (flag)) && \
3774 (((params)->fromCapabilities & (flag)) == 0))
3775
3776 //******************************************************************************
3777 // sysPowerDownHandler
3778 //
3779 // Perform a vfs sync before system sleep.
3780 //******************************************************************************
3781
3782 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3783 IOPMrootDomain::sysPowerDownHandler(
3784 void * target, void * refCon,
3785 UInt32 messageType, IOService * service,
3786 void * messageArgs, vm_size_t argSize )
3787 {
3788 static UInt32 lastSystemMessageType = 0;
3789 IOReturn ret = 0;
3790
3791 DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3792
3793 // rdar://problem/50363791
3794 // Sanity check to make sure the SystemWill/Has message types are
3795 // received in the expected order for all kernel priority clients.
3796 if (messageType == kIOMessageSystemWillSleep ||
3797 messageType == kIOMessageSystemWillPowerOn ||
3798 messageType == kIOMessageSystemHasPoweredOn) {
3799 switch (messageType) {
3800 case kIOMessageSystemWillPowerOn:
3801 assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3802 break;
3803 case kIOMessageSystemHasPoweredOn:
3804 assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3805 break;
3806 }
3807
3808 lastSystemMessageType = messageType;
3809 }
3810
3811 if (!gRootDomain) {
3812 return kIOReturnUnsupported;
3813 }
3814
3815 if (messageType == kIOMessageSystemCapabilityChange) {
3816 IOPMSystemCapabilityChangeParameters * params =
3817 (IOPMSystemCapabilityChangeParameters *) messageArgs;
3818
3819 // Interested applications have been notified of an impending power
3820 // change and have acked (when applicable).
3821 // This is our chance to save whatever state we can before powering
3822 // down.
3823 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3824 // via callout
3825
3826 DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3827 params->fromCapabilities, params->toCapabilities,
3828 params->changeFlags);
3829
3830 if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3831 // We will ack within 20 seconds
3832 params->maxWaitForReply = 20 * 1000 * 1000;
3833
3834 #if HIBERNATION
3835 gRootDomain->evaluateSystemSleepPolicyEarly();
3836
3837 // add in time we could spend freeing pages
3838 if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3839 params->maxWaitForReply = kCapabilityClientMaxWait;
3840 }
3841 DLOG("sysPowerDownHandler max wait %d s\n",
3842 (int) (params->maxWaitForReply / 1000 / 1000));
3843 #endif
3844
3845 // Notify platform that sleep has begun, after the early
3846 // sleep policy evaluation.
3847 getPlatform()->callPlatformFunction(
3848 sleepMessagePEFunction.get(), false,
3849 (void *)(uintptr_t) kIOMessageSystemWillSleep,
3850 NULL, NULL, NULL);
3851
3852 if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3853 // Purposely delay the ack and hope that shutdown occurs quickly.
3854 // Another option is not to schedule the thread and wait for
3855 // ack timeout...
3856 AbsoluteTime deadline;
3857 clock_interval_to_deadline( 30, kSecondScale, &deadline );
3858 thread_call_enter1_delayed(
3859 gRootDomain->diskSyncCalloutEntry,
3860 (thread_call_param_t)(uintptr_t) params->notifyRef,
3861 deadline );
3862 } else {
3863 thread_call_enter1(
3864 gRootDomain->diskSyncCalloutEntry,
3865 (thread_call_param_t)(uintptr_t) params->notifyRef);
3866 }
3867 }
3868 #if HIBERNATION
3869 else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3870 // We will ack within 110 seconds
3871 params->maxWaitForReply = 110 * 1000 * 1000;
3872
3873 thread_call_enter1(
3874 gRootDomain->diskSyncCalloutEntry,
3875 (thread_call_param_t)(uintptr_t) params->notifyRef);
3876 }
3877 #endif
3878 ret = kIOReturnSuccess;
3879 }
3880
3881 return ret;
3882 }
3883
3884 //******************************************************************************
3885 // handleQueueSleepWakeUUID
3886 //
3887 // Called from IOPMrootDomain when we're initiating a sleep,
3888 // or indirectly from PM configd when PM decides to clear the UUID.
3889 // PM clears the UUID several minutes after successful wake from sleep,
3890 // so that we might associate App spindumps with the immediately previous
3891 // sleep/wake.
3892 //
3893 // @param obj has a retain on it. We're responsible for releasing that retain.
3894 //******************************************************************************
3895
3896 void
handleQueueSleepWakeUUID(OSObject * obj)3897 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3898 {
3899 OSSharedPtr<OSString> str;
3900
3901 if (kOSBooleanFalse == obj) {
3902 handlePublishSleepWakeUUID(false);
3903 } else {
3904 str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3905 if (str) {
3906 // This branch caches the UUID for an upcoming sleep/wake
3907 queuedSleepWakeUUIDString = str;
3908 DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3909 }
3910 }
3911 }
3912 //******************************************************************************
3913 // handlePublishSleepWakeUUID
3914 //
3915 // Called from IOPMrootDomain when we're initiating a sleep,
3916 // or indirectly from PM configd when PM decides to clear the UUID.
3917 // PM clears the UUID several minutes after successful wake from sleep,
3918 // so that we might associate App spindumps with the immediately previous
3919 // sleep/wake.
3920 //******************************************************************************
3921
3922 void
handlePublishSleepWakeUUID(bool shouldPublish)3923 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3924 {
3925 ASSERT_GATED();
3926
3927 /*
3928 * Clear the current UUID
3929 */
3930 if (gSleepWakeUUIDIsSet) {
3931 DLOG("SleepWake UUID cleared\n");
3932
3933 gSleepWakeUUIDIsSet = false;
3934
3935 removeProperty(kIOPMSleepWakeUUIDKey);
3936 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3937 }
3938
3939 /*
3940 * Optionally, publish a new UUID
3941 */
3942 if (queuedSleepWakeUUIDString && shouldPublish) {
3943 OSSharedPtr<OSString> publishThisUUID;
3944
3945 publishThisUUID = queuedSleepWakeUUIDString;
3946
3947 if (publishThisUUID) {
3948 setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3949 }
3950
3951 gSleepWakeUUIDIsSet = true;
3952 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3953
3954 queuedSleepWakeUUIDString.reset();
3955 }
3956 }
3957
3958 //******************************************************************************
3959 // IOPMGetSleepWakeUUIDKey
3960 //
3961 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3962 // To get the full key -- a C string -- the buffer must large enough for
3963 // the end-of-string character.
3964 // The key is expected to be an UUID string
3965 //******************************************************************************
3966
3967 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3968 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3969 {
3970 if (!gSleepWakeUUIDIsSet) {
3971 return false;
3972 }
3973
3974 if (buffer != NULL) {
3975 OSSharedPtr<OSString> string =
3976 OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3977
3978 if (!string) {
3979 *buffer = '\0';
3980 } else {
3981 strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3982 }
3983 }
3984
3985 return true;
3986 }
3987
3988 //******************************************************************************
3989 // lowLatencyAudioNotify
3990 //
3991 // Used to send an update about low latency audio activity to interested
3992 // clients. To keep the overhead minimal the OSDictionary used here
3993 // is initialized at boot.
3994 //******************************************************************************
3995
3996 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3997 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3998 {
3999 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
4000 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
4001 lowLatencyAudioNotifyTimestampVal->setValue(time);
4002 lowLatencyAudioNotifyStateVal->setValue(state);
4003 setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
4004 } else {
4005 DLOG("LowLatencyAudioNotify error\n");
4006 }
4007 return;
4008 }
4009
4010 //******************************************************************************
4011 // IOPMrootDomainRTNotifier
4012 //
4013 // Used by performance controller to update the timestamp and state associated
4014 // with low latency audio activity in the system.
4015 //******************************************************************************
4016
4017 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)4018 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
4019 {
4020 gRootDomain->lowLatencyAudioNotify(time, state);
4021 return;
4022 }
4023
4024 //******************************************************************************
4025 // initializeBootSessionUUID
4026 //
4027 // Initialize the boot session uuid at boot up and sets it into registry.
4028 //******************************************************************************
4029
4030 void
initializeBootSessionUUID(void)4031 IOPMrootDomain::initializeBootSessionUUID(void)
4032 {
4033 uuid_t new_uuid;
4034 uuid_string_t new_uuid_string;
4035
4036 uuid_generate(new_uuid);
4037 uuid_unparse_upper(new_uuid, new_uuid_string);
4038 memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
4039
4040 setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
4041 }
4042
4043 //******************************************************************************
4044 // Root domain uses the private and tagged changePowerState methods for
4045 // tracking and logging purposes.
4046 //******************************************************************************
4047
4048 #define REQUEST_TAG_TO_REASON(x) ((uint16_t)x)
4049
4050 static uint32_t
nextRequestTag(IOPMRequestTag tag)4051 nextRequestTag( IOPMRequestTag tag )
4052 {
4053 static SInt16 msb16 = 1;
4054 uint16_t id = OSAddAtomic16(1, &msb16);
4055 return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
4056 }
4057
4058 // TODO: remove this shim function and exported symbol
4059 IOReturn
changePowerStateTo(unsigned long ordinal)4060 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
4061 {
4062 return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
4063 }
4064
4065 // TODO: remove this shim function and exported symbol
4066 IOReturn
changePowerStateToPriv(unsigned long ordinal)4067 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
4068 {
4069 return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
4070 }
4071
4072 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4073 IOPMrootDomain::changePowerStateWithOverrideTo(
4074 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4075 {
4076 uint32_t tag = nextRequestTag(reason);
4077 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4078
4079 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4080 return kIOReturnUnsupported;
4081 }
4082
4083 return super::changePowerStateWithOverrideTo(ordinal, tag);
4084 }
4085
4086 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4087 IOPMrootDomain::changePowerStateWithTagTo(
4088 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4089 {
4090 uint32_t tag = nextRequestTag(reason);
4091 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4092
4093 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4094 return kIOReturnUnsupported;
4095 }
4096
4097 return super::changePowerStateWithTagTo(ordinal, tag);
4098 }
4099
4100 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4101 IOPMrootDomain::changePowerStateWithTagToPriv(
4102 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4103 {
4104 uint32_t tag = nextRequestTag(reason);
4105 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4106
4107 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4108 return kIOReturnUnsupported;
4109 }
4110
4111 return super::changePowerStateWithTagToPriv(ordinal, tag);
4112 }
4113
4114 //******************************************************************************
4115 // activity detect
4116 //
4117 //******************************************************************************
4118
4119 bool
activitySinceSleep(void)4120 IOPMrootDomain::activitySinceSleep(void)
4121 {
4122 return userActivityCount != userActivityAtSleep;
4123 }
4124
4125 bool
abortHibernation(void)4126 IOPMrootDomain::abortHibernation(void)
4127 {
4128 #if __arm64__
4129 // don't allow hibernation to be aborted on ARM due to user activity
4130 // since once ApplePMGR decides we're hibernating, we can't turn back
4131 // see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4132 return false;
4133 #else
4134 bool ret = activitySinceSleep();
4135
4136 if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4137 DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4138 hibernateAborted = true;
4139 }
4140 return ret;
4141 #endif
4142 }
4143
4144 extern "C" int
hibernate_should_abort(void)4145 hibernate_should_abort(void)
4146 {
4147 if (gRootDomain) {
4148 return gRootDomain->abortHibernation();
4149 } else {
4150 return 0;
4151 }
4152 }
4153
4154 //******************************************************************************
4155 // scheduleImmediateDebugWake
4156 //
4157 // Schedule a wake with RTC to wake us back up immediately after we sleep.
4158 // Useful when a cancel request comes in past the revert point on the sleep path
4159 //******************************************************************************
4160
4161 void
scheduleImmediateDebugWake(void)4162 IOPMrootDomain::scheduleImmediateDebugWake( void )
4163 {
4164 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(1);
4165 OSSharedPtr<OSNumber> secs = OSNumber::withNumber(1, 32);
4166
4167 if (dict && secs) {
4168 dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4169 gRootDomain->setProperties(dict.get());
4170 MSG("Reverting sleep with relative wake\n");
4171 }
4172 }
4173
4174 //******************************************************************************
4175 // willNotifyPowerChildren
4176 //
4177 // Called after all interested drivers have all acknowledged the power change,
4178 // but before any power children is informed. Dispatched though a thread call,
4179 // so it is safe to perform work that might block on a sleeping disk. PM state
4180 // machine (not thread) will block w/o timeout until this function returns.
4181 //******************************************************************************
4182
4183 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4184 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4185 {
4186 if (SLEEP_STATE == newPowerState) {
4187 notifierThread = current_thread();
4188 if (updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange)) {
4189 AbsoluteTime deadline;
4190
4191 clock_interval_to_deadline(10, kSecondScale, &deadline);
4192 #if defined(XNU_TARGET_OS_OSX)
4193 vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4194 #endif /* defined(XNU_TARGET_OS_OSX) */
4195 }
4196
4197 _aotReadyToFullWake = false;
4198 #if 0
4199 if (_aotLingerTime) {
4200 uint64_t deadline;
4201 IOLog("aot linger no return\n");
4202 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4203 clock_delay_until(deadline);
4204 }
4205 #endif
4206 if (!_aotMode) {
4207 _aotTestTime = 0;
4208 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4209 _aotLastWakeTime = 0;
4210 if (_aotMetrics) {
4211 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4212 }
4213 } else if (!_aotNow && !_debugWakeSeconds) {
4214 _aotNow = true;
4215 _aotPendingFlags = 0;
4216 _aotTasksSuspended = true;
4217 _aotLastWakeTime = 0;
4218 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4219 if (kIOPMAOTModeCycle & _aotMode) {
4220 clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4221 _aotTestTime = mach_continuous_time() + _aotTestInterval;
4222 setWakeTime(_aotTestTime);
4223 }
4224 uint32_t lingerSecs;
4225 if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4226 lingerSecs = 0;
4227 }
4228 clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4229 clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4230 clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4231 }
4232
4233 #if HIBERNATION
4234 // Adjust watchdog for IOHibernateSystemSleep
4235 int defaultTimeout = getWatchdogTimeout();
4236 int timeout = defaultTimeout > WATCHDOG_HIBERNATION_TIMEOUT ?
4237 defaultTimeout : WATCHDOG_HIBERNATION_TIMEOUT;
4238 reset_watchdog_timer(timeout);
4239
4240 IOHibernateSystemSleep();
4241 IOHibernateIOKitSleep();
4242 #endif
4243 #if defined(__arm64__) && HIBERNATION
4244 // On AS, hibernation cannot be aborted. Resetting RTC to 1s during hibernation upon detecting
4245 // user activity is pointless (we are likely to spend >1s hibernating). It also clears existing
4246 // alarms, which can mess with cycler tools.
4247 if (gRootDomain->activitySinceSleep() && gIOHibernateState == kIOHibernateStateInactive) {
4248 #else /* defined(__arm64__) && HIBERNATION */
4249 // On non-AS, hibernation can be aborted if user activity is detected. So continue to reset the
4250 // RTC alarm (even during hibernation) so we can immediately wake from regular S2R if needed.
4251 if (gRootDomain->activitySinceSleep()) {
4252 #endif /* defined(__arm64__) && HIBERNATION */
4253 scheduleImmediateDebugWake();
4254 }
4255
4256 notifierThread = NULL;
4257 }
4258 }
4259
4260 //******************************************************************************
4261 // willTellSystemCapabilityDidChange
4262 //
4263 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4264 // domain is raising its power state, immediately after notifying interested
4265 // drivers and power children.
4266 //******************************************************************************
4267
4268 void
4269 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4270 {
4271 if ((_systemTransitionType == kSystemTransitionWake) &&
4272 !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4273 // After powering up drivers, dark->full promotion on the current wake
4274 // transition is no longer possible. That is because the next machine
4275 // state will issue the system capability change messages.
4276 // The darkWakePowerClamped flag may already be set if the system has
4277 // at least one driver that was power clamped due to dark wake.
4278 // This function sets the darkWakePowerClamped flag in case there
4279 // is no power-clamped driver in the system.
4280 //
4281 // Last opportunity to exit dark wake using:
4282 // requestFullWake( kFullWakeReasonLocalUser );
4283
4284 if (!darkWakePowerClamped) {
4285 if (darkWakeLogClamp) {
4286 AbsoluteTime now;
4287 uint64_t nsec;
4288
4289 clock_get_uptime(&now);
4290 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4291 absolutetime_to_nanoseconds(now, &nsec);
4292 DLOG("dark wake promotion disabled at %u ms\n",
4293 ((int)((nsec) / NSEC_PER_MSEC)));
4294 }
4295 darkWakePowerClamped = true;
4296 }
4297 }
4298 }
4299
4300 //******************************************************************************
4301 // sleepOnClamshellClosed
4302 //
4303 // contains the logic to determine if the system should sleep when the clamshell
4304 // is closed.
4305 //******************************************************************************
4306
4307 bool
4308 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4309 {
4310 if (!clamshellExists) {
4311 return false;
4312 }
4313
4314 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4315 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4316
4317 return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4318 }
4319
4320 bool
4321 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4322 {
4323 // Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4324 // closed && battery
4325 if (!clamshellExists) {
4326 return false;
4327 }
4328
4329 DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4330 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4331
4332 return !acAdaptorConnected && !clamshellSleepDisableMask;
4333 }
4334
4335 void
4336 IOPMrootDomain::sendClientClamshellNotification( void )
4337 {
4338 /* Only broadcast clamshell alert if clamshell exists. */
4339 if (!clamshellExists) {
4340 return;
4341 }
4342
4343 setProperty(kAppleClamshellStateKey,
4344 clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4345
4346 setProperty(kAppleClamshellCausesSleepKey,
4347 shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4348
4349 /* Argument to message is a bitfiel of
4350 * ( kClamshellStateBit | kClamshellSleepBit )
4351 */
4352 messageClients(kIOPMMessageClamshellStateChange,
4353 (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4354 | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4355 }
4356
4357 //******************************************************************************
4358 // getSleepSupported
4359 //
4360 // Deprecated
4361 //******************************************************************************
4362
4363 IOOptionBits
4364 IOPMrootDomain::getSleepSupported( void )
4365 {
4366 return platformSleepSupport;
4367 }
4368
4369 //******************************************************************************
4370 // setSleepSupported
4371 //
4372 // Deprecated
4373 //******************************************************************************
4374
4375 void
4376 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4377 {
4378 DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4379 OSBitOrAtomic(flags, &platformSleepSupport);
4380 }
4381
4382 //******************************************************************************
4383 // setClamShellSleepDisable
4384 //
4385 //******************************************************************************
4386
4387 void
4388 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4389 {
4390 uint32_t oldMask;
4391
4392 // User client calls this in non-gated context
4393 if (gIOPMWorkLoop->inGate() == false) {
4394 gIOPMWorkLoop->runAction(
4395 OSMemberFunctionCast(IOWorkLoop::Action, this,
4396 &IOPMrootDomain::setClamShellSleepDisable),
4397 (OSObject *) this,
4398 (void *) disable, (void *)(uintptr_t) bitmask);
4399 return;
4400 }
4401
4402 oldMask = clamshellSleepDisableMask;
4403 if (disable) {
4404 clamshellSleepDisableMask |= bitmask;
4405 } else {
4406 clamshellSleepDisableMask &= ~bitmask;
4407 }
4408 DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4409
4410 if (clamshellExists && clamshellClosed &&
4411 (clamshellSleepDisableMask != oldMask) &&
4412 (clamshellSleepDisableMask == 0)) {
4413 handlePowerNotification(kLocalEvalClamshellCommand);
4414 }
4415 }
4416
4417 //******************************************************************************
4418 // wakeFromDoze
4419 //
4420 // Deprecated.
4421 //******************************************************************************
4422
4423 void
4424 IOPMrootDomain::wakeFromDoze( void )
4425 {
4426 // Preserve symbol for familes (IOUSBFamily and IOGraphics)
4427 }
4428
4429 //******************************************************************************
4430 // recordRTCAlarm
4431 //
4432 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4433 // should be a dark wake or a full wake. Both Maintenance and SleepService
4434 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4435 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4436 // PMSettings are ignored.
4437 //
4438 // Caller serialized using settingsCtrlLock.
4439 //******************************************************************************
4440
4441 void
4442 IOPMrootDomain::recordRTCAlarm(
4443 const OSSymbol *type,
4444 OSObject *object )
4445 {
4446 uint32_t previousAlarmMask = _scheduledAlarmMask;
4447
4448 if (type == gIOPMSettingDebugWakeRelativeKey) {
4449 OSNumber * n = OSDynamicCast(OSNumber, object);
4450 if (n) {
4451 // Debug wake has highest scheduling priority so it overrides any
4452 // pre-existing alarm.
4453 uint32_t debugSecs = n->unsigned32BitValue();
4454 _nextScheduledAlarmType.reset(type, OSRetain);
4455 _nextScheduledAlarmUTC = debugSecs;
4456
4457 _debugWakeSeconds = debugSecs;
4458 OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4459 DLOG("next alarm (%s) in %u secs\n",
4460 type->getCStringNoCopy(), debugSecs);
4461 }
4462 } else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4463 (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4464 (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4465 OSData * data = OSDynamicCast(OSData, object);
4466 if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4467 const IOPMCalendarStruct * cs;
4468 bool replaceNextAlarm = false;
4469 clock_sec_t secs;
4470
4471 cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4472 secs = IOPMConvertCalendarToSeconds(cs);
4473 DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4474
4475 // Update the next scheduled alarm type
4476 if ((_nextScheduledAlarmType == NULL) ||
4477 ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4478 (secs < _nextScheduledAlarmUTC))) {
4479 replaceNextAlarm = true;
4480 }
4481
4482 if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4483 if (cs->year) {
4484 _calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4485 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4486 } else {
4487 // TODO: can this else-block be removed?
4488 _calendarWakeAlarmUTC = 0;
4489 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4490 }
4491 }
4492 if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4493 OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4494 }
4495 if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4496 OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4497 }
4498
4499 if (replaceNextAlarm) {
4500 _nextScheduledAlarmType.reset(type, OSRetain);
4501 _nextScheduledAlarmUTC = secs;
4502 DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4503 }
4504 }
4505 }
4506
4507 if (_scheduledAlarmMask != previousAlarmMask) {
4508 DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4509 }
4510 }
4511
4512 // MARK: -
4513 // MARK: Features
4514
4515 //******************************************************************************
4516 // publishFeature
4517 //
4518 // Adds a new feature to the supported features dictionary
4519 //******************************************************************************
4520
4521 void
4522 IOPMrootDomain::publishFeature( const char * feature )
4523 {
4524 publishFeature(feature, kRD_AllPowerSources, NULL);
4525 }
4526
4527 //******************************************************************************
4528 // publishFeature (with supported power source specified)
4529 //
4530 // Adds a new feature to the supported features dictionary
4531 //******************************************************************************
4532
4533 void
4534 IOPMrootDomain::publishFeature(
4535 const char *feature,
4536 uint32_t supportedWhere,
4537 uint32_t *uniqueFeatureID)
4538 {
4539 static uint16_t next_feature_id = 500;
4540
4541 OSSharedPtr<OSNumber> new_feature_data;
4542 OSNumber *existing_feature = NULL;
4543 OSArray *existing_feature_arr_raw = NULL;
4544 OSSharedPtr<OSArray> existing_feature_arr;
4545 OSObject *osObj = NULL;
4546 uint32_t feature_value = 0;
4547
4548 supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4549
4550 if (!supportedWhere) {
4551 // Feature isn't supported anywhere!
4552 return;
4553 }
4554
4555 if (next_feature_id > 5000) {
4556 // Far, far too many features!
4557 return;
4558 }
4559
4560 if (featuresDictLock) {
4561 IOLockLock(featuresDictLock);
4562 }
4563
4564 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4565 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4566 OSSharedPtr<OSDictionary> features;
4567
4568 // Create new features dict if necessary
4569 if (origFeatures) {
4570 features = OSDictionary::withDictionary(origFeatures);
4571 } else {
4572 features = OSDictionary::withCapacity(1);
4573 }
4574
4575 // Create OSNumber to track new feature
4576
4577 next_feature_id += 1;
4578 if (uniqueFeatureID) {
4579 // We don't really mind if the calling kext didn't give us a place
4580 // to stash their unique id. Many kexts don't plan to unload, and thus
4581 // have no need to remove themselves later.
4582 *uniqueFeatureID = next_feature_id;
4583 }
4584
4585 feature_value = (uint32_t)next_feature_id;
4586 feature_value <<= 16;
4587 feature_value += supportedWhere;
4588
4589 new_feature_data = OSNumber::withNumber(
4590 (unsigned long long)feature_value, 32);
4591
4592 // Does features object already exist?
4593 if ((osObj = features->getObject(feature))) {
4594 if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4595 // We need to create an OSArray to hold the now 2 elements.
4596 existing_feature_arr = OSArray::withObjects(
4597 (const OSObject **)&existing_feature, 1, 2);
4598 } else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4599 // Add object to existing array
4600 existing_feature_arr = OSArray::withArray(
4601 existing_feature_arr_raw,
4602 existing_feature_arr_raw->getCount() + 1);
4603 }
4604
4605 if (existing_feature_arr) {
4606 existing_feature_arr->setObject(new_feature_data.get());
4607 features->setObject(feature, existing_feature_arr.get());
4608 }
4609 } else {
4610 // The easy case: no previously existing features listed. We simply
4611 // set the OSNumber at key 'feature' and we're on our way.
4612 features->setObject(feature, new_feature_data.get());
4613 }
4614
4615 setProperty(kRootDomainSupportedFeatures, features.get());
4616
4617 if (featuresDictLock) {
4618 IOLockUnlock(featuresDictLock);
4619 }
4620
4621 // Notify EnergySaver and all those in user space so they might
4622 // re-populate their feature specific UI
4623 if (pmPowerStateQueue) {
4624 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4625 }
4626 }
4627
4628 //******************************************************************************
4629 // removePublishedFeature
4630 //
4631 // Removes previously published feature
4632 //******************************************************************************
4633
4634 IOReturn
4635 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4636 {
4637 IOReturn ret = kIOReturnError;
4638 uint32_t feature_value = 0;
4639 uint16_t feature_id = 0;
4640 bool madeAChange = false;
4641
4642 OSSymbol *dictKey = NULL;
4643 OSSharedPtr<OSCollectionIterator> dictIterator;
4644 OSArray *arrayMember = NULL;
4645 OSNumber *numberMember = NULL;
4646 OSObject *osObj = NULL;
4647 OSNumber *osNum = NULL;
4648 OSSharedPtr<OSArray> arrayMemberCopy;
4649
4650 if (kBadPMFeatureID == removeFeatureID) {
4651 return kIOReturnNotFound;
4652 }
4653
4654 if (featuresDictLock) {
4655 IOLockLock(featuresDictLock);
4656 }
4657
4658 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4659 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4660 OSSharedPtr<OSDictionary> features;
4661
4662 if (origFeatures) {
4663 // Any modifications to the dictionary are made to the copy to prevent
4664 // races & crashes with userland clients. Dictionary updated
4665 // automically later.
4666 features = OSDictionary::withDictionary(origFeatures);
4667 } else {
4668 features = NULL;
4669 ret = kIOReturnNotFound;
4670 goto exit;
4671 }
4672
4673 // We iterate 'features' dictionary looking for an entry tagged
4674 // with 'removeFeatureID'. If found, we remove it from our tracking
4675 // structures and notify the OS via a general interest message.
4676
4677 dictIterator = OSCollectionIterator::withCollection(features.get());
4678 if (!dictIterator) {
4679 goto exit;
4680 }
4681
4682 while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4683 osObj = features->getObject(dictKey);
4684
4685 // Each Feature is either tracked by an OSNumber
4686 if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4687 feature_value = numberMember->unsigned32BitValue();
4688 feature_id = (uint16_t)(feature_value >> 16);
4689
4690 if (feature_id == (uint16_t)removeFeatureID) {
4691 // Remove this node
4692 features->removeObject(dictKey);
4693 madeAChange = true;
4694 break;
4695 }
4696
4697 // Or tracked by an OSArray of OSNumbers
4698 } else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4699 unsigned int arrayCount = arrayMember->getCount();
4700
4701 for (unsigned int i = 0; i < arrayCount; i++) {
4702 osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4703 if (!osNum) {
4704 continue;
4705 }
4706
4707 feature_value = osNum->unsigned32BitValue();
4708 feature_id = (uint16_t)(feature_value >> 16);
4709
4710 if (feature_id == (uint16_t)removeFeatureID) {
4711 // Remove this node
4712 if (1 == arrayCount) {
4713 // If the array only contains one element, remove
4714 // the whole thing.
4715 features->removeObject(dictKey);
4716 } else {
4717 // Otherwise remove the element from a copy of the array.
4718 arrayMemberCopy = OSArray::withArray(arrayMember);
4719 if (arrayMemberCopy) {
4720 arrayMemberCopy->removeObject(i);
4721 features->setObject(dictKey, arrayMemberCopy.get());
4722 }
4723 }
4724
4725 madeAChange = true;
4726 break;
4727 }
4728 }
4729 }
4730 }
4731
4732 if (madeAChange) {
4733 ret = kIOReturnSuccess;
4734
4735 setProperty(kRootDomainSupportedFeatures, features.get());
4736
4737 // Notify EnergySaver and all those in user space so they might
4738 // re-populate their feature specific UI
4739 if (pmPowerStateQueue) {
4740 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4741 }
4742 } else {
4743 ret = kIOReturnNotFound;
4744 }
4745
4746 exit:
4747 if (featuresDictLock) {
4748 IOLockUnlock(featuresDictLock);
4749 }
4750 return ret;
4751 }
4752
4753 //******************************************************************************
4754 // publishPMSetting (private)
4755 //
4756 // Should only be called by PMSettingObject to publish a PM Setting as a
4757 // supported feature.
4758 //******************************************************************************
4759
4760 void
4761 IOPMrootDomain::publishPMSetting(
4762 const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4763 {
4764 if (noPublishPMSettings &&
4765 (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4766 // Setting found in noPublishPMSettings array
4767 *featureID = kBadPMFeatureID;
4768 return;
4769 }
4770
4771 publishFeature(
4772 feature->getCStringNoCopy(), where, featureID);
4773 }
4774
4775 //******************************************************************************
4776 // setPMSetting (private)
4777 //
4778 // Internal helper to relay PM settings changes from user space to individual
4779 // drivers. Should be called only by IOPMrootDomain::setProperties.
4780 //******************************************************************************
4781
4782 IOReturn
4783 IOPMrootDomain::setPMSetting(
4784 const OSSymbol *type,
4785 OSObject *object )
4786 {
4787 PMSettingCallEntry *entries = NULL;
4788 OSSharedPtr<OSArray> chosen;
4789 const OSArray *array;
4790 PMSettingObject *pmso;
4791 thread_t thisThread;
4792 int i, j, count, capacity;
4793 bool ok = false;
4794 IOReturn ret;
4795
4796 if (NULL == type) {
4797 return kIOReturnBadArgument;
4798 }
4799
4800 PMSETTING_LOCK();
4801
4802 // Update settings dict so changes are visible from copyPMSetting().
4803 fPMSettingsDict->setObject(type, object);
4804
4805 // Prep all PMSetting objects with the given 'type' for callout.
4806 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4807 if (!array || ((capacity = array->getCount()) == 0)) {
4808 goto unlock_exit;
4809 }
4810
4811 // Array to retain PMSetting objects targeted for callout.
4812 chosen = OSArray::withCapacity(capacity);
4813 if (!chosen) {
4814 goto unlock_exit; // error
4815 }
4816 entries = IONew(PMSettingCallEntry, capacity);
4817 if (!entries) {
4818 goto unlock_exit; // error
4819 }
4820 memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4821
4822 thisThread = current_thread();
4823
4824 for (i = 0, j = 0; i < capacity; i++) {
4825 pmso = (PMSettingObject *) array->getObject(i);
4826 if (pmso->disabled) {
4827 continue;
4828 }
4829 entries[j].thread = thisThread;
4830 queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4831 chosen->setObject(pmso);
4832 j++;
4833 }
4834 count = j;
4835 if (!count) {
4836 goto unlock_exit;
4837 }
4838
4839 PMSETTING_UNLOCK();
4840
4841 // Call each pmso in the chosen array.
4842 for (i = 0; i < count; i++) {
4843 pmso = (PMSettingObject *) chosen->getObject(i);
4844 ret = pmso->dispatchPMSetting(type, object);
4845 if (ret == kIOReturnSuccess) {
4846 // At least one setting handler was successful
4847 ok = true;
4848 #if DEVELOPMENT || DEBUG
4849 } else {
4850 // Log the handler and kext that failed
4851 OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4852 if (kextName) {
4853 DLOG("PMSetting(%s) error 0x%x from %s\n",
4854 type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4855 }
4856 #endif
4857 }
4858 }
4859
4860 PMSETTING_LOCK();
4861 for (i = 0; i < count; i++) {
4862 pmso = (PMSettingObject *) chosen->getObject(i);
4863 queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4864 if (pmso->waitThread) {
4865 PMSETTING_WAKEUP(pmso);
4866 }
4867 }
4868
4869 if (ok) {
4870 recordRTCAlarm(type, object);
4871 }
4872 unlock_exit:
4873 PMSETTING_UNLOCK();
4874
4875 if (entries) {
4876 IODelete(entries, PMSettingCallEntry, capacity);
4877 }
4878
4879 return kIOReturnSuccess;
4880 }
4881
4882 //******************************************************************************
4883 // copyPMSetting (public)
4884 //
4885 // Allows kexts to safely read setting values, without being subscribed to
4886 // notifications.
4887 //******************************************************************************
4888
4889 OSSharedPtr<OSObject>
4890 IOPMrootDomain::copyPMSetting(
4891 OSSymbol *whichSetting)
4892 {
4893 OSSharedPtr<OSObject> obj;
4894
4895 if (!whichSetting) {
4896 return NULL;
4897 }
4898
4899 PMSETTING_LOCK();
4900 obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4901 PMSETTING_UNLOCK();
4902
4903 return obj;
4904 }
4905
4906 //******************************************************************************
4907 // registerPMSettingController (public)
4908 //
4909 // direct wrapper to registerPMSettingController with uint32_t power source arg
4910 //******************************************************************************
4911
4912 IOReturn
4913 IOPMrootDomain::registerPMSettingController(
4914 const OSSymbol * settings[],
4915 IOPMSettingControllerCallback func,
4916 OSObject *target,
4917 uintptr_t refcon,
4918 OSObject **handle)
4919 {
4920 return registerPMSettingController(
4921 settings,
4922 (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4923 func, target, refcon, handle);
4924 }
4925
4926 //******************************************************************************
4927 // registerPMSettingController (public)
4928 //
4929 // Kexts may register for notifications when a particular setting is changed.
4930 // A list of settings is available in IOPM.h.
4931 // Arguments:
4932 // * settings - An OSArray containing OSSymbols. Caller should populate this
4933 // array with a list of settings caller wants notifications from.
4934 // * func - A C function callback of the type IOPMSettingControllerCallback
4935 // * target - caller may provide an OSObject *, which PM will pass as an
4936 // target to calls to "func"
4937 // * refcon - caller may provide an void *, which PM will pass as an
4938 // argument to calls to "func"
4939 // * handle - This is a return argument. We will populate this pointer upon
4940 // call success. Hold onto this and pass this argument to
4941 // IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4942 // Returns:
4943 // kIOReturnSuccess on success
4944 //******************************************************************************
4945
4946 IOReturn
4947 IOPMrootDomain::registerPMSettingController(
4948 const OSSymbol * settings[],
4949 uint32_t supportedPowerSources,
4950 IOPMSettingControllerCallback func,
4951 OSObject *target,
4952 uintptr_t refcon,
4953 OSObject **handle)
4954 {
4955 PMSettingObject *pmso = NULL;
4956 OSObject *pmsh = NULL;
4957 int i;
4958
4959 if (NULL == settings ||
4960 NULL == func ||
4961 NULL == handle) {
4962 return kIOReturnBadArgument;
4963 }
4964
4965 pmso = PMSettingObject::pmSettingObject(
4966 (IOPMrootDomain *) this, func, target,
4967 refcon, supportedPowerSources, settings, &pmsh);
4968
4969 if (!pmso) {
4970 *handle = NULL;
4971 return kIOReturnInternalError;
4972 }
4973
4974 PMSETTING_LOCK();
4975 for (i = 0; settings[i]; i++) {
4976 OSSharedPtr<OSArray> newList;
4977 OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4978 if (!list) {
4979 // New array of callbacks for this setting
4980 newList = OSArray::withCapacity(1);
4981 settingsCallbacks->setObject(settings[i], newList.get());
4982 list = newList.get();
4983 }
4984
4985 // Add caller to the callback list
4986 list->setObject(pmso);
4987 }
4988 PMSETTING_UNLOCK();
4989
4990 // Return handle to the caller, the setting object is private.
4991 *handle = pmsh;
4992
4993 return kIOReturnSuccess;
4994 }
4995
4996 //******************************************************************************
4997 // deregisterPMSettingObject (private)
4998 //
4999 // Only called from PMSettingObject.
5000 //******************************************************************************
5001
5002 void
5003 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
5004 {
5005 thread_t thisThread = current_thread();
5006 PMSettingCallEntry *callEntry;
5007 OSSharedPtr<OSCollectionIterator> iter;
5008 OSSymbol *sym;
5009 OSArray *array;
5010 int index;
5011 bool wait;
5012
5013 PMSETTING_LOCK();
5014
5015 pmso->disabled = true;
5016
5017 // Wait for all callout threads to finish.
5018 do {
5019 wait = false;
5020 queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
5021 {
5022 if (callEntry->thread != thisThread) {
5023 wait = true;
5024 break;
5025 }
5026 }
5027 if (wait) {
5028 assert(NULL == pmso->waitThread);
5029 pmso->waitThread = thisThread;
5030 PMSETTING_WAIT(pmso);
5031 pmso->waitThread = NULL;
5032 }
5033 } while (wait);
5034
5035 // Search each PM settings array in the kernel.
5036 iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
5037 if (iter) {
5038 while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
5039 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
5040 index = array->getNextIndexOfObject(pmso, 0);
5041 if (-1 != index) {
5042 array->removeObject(index);
5043 }
5044 }
5045 }
5046
5047 PMSETTING_UNLOCK();
5048
5049 pmso->release();
5050 }
5051
5052 //******************************************************************************
5053 // informCPUStateChange
5054 //
5055 // Call into PM CPU code so that CPU power savings may dynamically adjust for
5056 // running on battery, with the lid closed, etc.
5057 //
5058 // informCPUStateChange is a no-op on non x86 systems
5059 // only x86 has explicit support in the IntelCPUPowerManagement kext
5060 //******************************************************************************
5061
5062 void
5063 IOPMrootDomain::informCPUStateChange(
5064 uint32_t type,
5065 uint32_t value )
5066 {
5067 #if defined(__i386__) || defined(__x86_64__)
5068
5069 pmioctlVariableInfo_t varInfoStruct;
5070 int pmCPUret = 0;
5071 const char *varNameStr = NULL;
5072 int32_t *varIndex = NULL;
5073
5074 if (kInformAC == type) {
5075 varNameStr = kIOPMRootDomainBatPowerCString;
5076 varIndex = &idxPMCPULimitedPower;
5077 } else if (kInformLid == type) {
5078 varNameStr = kIOPMRootDomainLidCloseCString;
5079 varIndex = &idxPMCPUClamshell;
5080 } else {
5081 return;
5082 }
5083
5084 // Set the new value!
5085 // pmCPUControl will assign us a new ID if one doesn't exist yet
5086 bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
5087 varInfoStruct.varID = *varIndex;
5088 varInfoStruct.varType = vBool;
5089 varInfoStruct.varInitValue = value;
5090 varInfoStruct.varCurValue = value;
5091 strlcpy((char *)varInfoStruct.varName,
5092 (const char *)varNameStr,
5093 sizeof(varInfoStruct.varName));
5094
5095 // Set!
5096 pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
5097
5098 // pmCPU only assigns numerical id's when a new varName is specified
5099 if ((0 == pmCPUret)
5100 && (*varIndex == kCPUUnknownIndex)) {
5101 // pmCPUControl has assigned us a new variable ID.
5102 // Let's re-read the structure we just SET to learn that ID.
5103 pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5104
5105 if (0 == pmCPUret) {
5106 // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5107 *varIndex = varInfoStruct.varID;
5108 }
5109 }
5110
5111 return;
5112
5113 #endif /* __i386__ || __x86_64__ */
5114 }
5115
5116 // MARK: -
5117 // MARK: Deep Sleep Policy
5118
5119 #if HIBERNATION
5120
5121 //******************************************************************************
5122 // evaluateSystemSleepPolicy
5123 //******************************************************************************
5124
5125 #define kIOPlatformSystemSleepPolicyKey "IOPlatformSystemSleepPolicy"
5126
5127 // Sleep flags
5128 enum {
5129 kIOPMSleepFlagHibernate = 0x00000001,
5130 kIOPMSleepFlagSleepTimerEnable = 0x00000002
5131 };
5132
5133 struct IOPMSystemSleepPolicyEntry {
5134 uint32_t factorMask;
5135 uint32_t factorBits;
5136 uint32_t sleepFlags;
5137 uint32_t wakeEvents;
5138 } __attribute__((packed));
5139
5140 struct IOPMSystemSleepPolicyTable {
5141 uint32_t signature;
5142 uint16_t version;
5143 uint16_t entryCount;
5144 IOPMSystemSleepPolicyEntry entries[];
5145 } __attribute__((packed));
5146
5147 enum {
5148 kIOPMSleepAttributeHibernateSetup = 0x00000001,
5149 kIOPMSleepAttributeHibernateSleep = 0x00000002
5150 };
5151
5152 static uint32_t
5153 getSleepTypeAttributes( uint32_t sleepType )
5154 {
5155 static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5156 {
5157 /* invalid */ 0,
5158 /* abort */ 0,
5159 /* normal */ 0,
5160 /* safesleep */ kIOPMSleepAttributeHibernateSetup,
5161 /* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5162 /* standby */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5163 /* poweroff */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5164 /* deepidle */ 0
5165 };
5166
5167 if (sleepType >= kIOPMSleepTypeLast) {
5168 return 0;
5169 }
5170
5171 return sleepTypeAttributes[sleepType];
5172 }
5173
5174 bool
5175 IOPMrootDomain::evaluateSystemSleepPolicy(
5176 IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5177 {
5178 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5179
5180 static const IONamedValue factorValues[] = {
5181 SLEEP_FACTOR( SleepTimerWake ),
5182 SLEEP_FACTOR( LidOpen ),
5183 SLEEP_FACTOR( ACPower ),
5184 SLEEP_FACTOR( BatteryLow ),
5185 SLEEP_FACTOR( StandbyNoDelay ),
5186 SLEEP_FACTOR( StandbyForced ),
5187 SLEEP_FACTOR( StandbyDisabled ),
5188 SLEEP_FACTOR( USBExternalDevice ),
5189 SLEEP_FACTOR( BluetoothHIDDevice ),
5190 SLEEP_FACTOR( ExternalMediaMounted ),
5191 SLEEP_FACTOR( ThunderboltDevice ),
5192 SLEEP_FACTOR( RTCAlarmScheduled ),
5193 SLEEP_FACTOR( MagicPacketWakeEnabled ),
5194 SLEEP_FACTOR( HibernateForced ),
5195 SLEEP_FACTOR( AutoPowerOffDisabled ),
5196 SLEEP_FACTOR( AutoPowerOffForced ),
5197 SLEEP_FACTOR( ExternalDisplay ),
5198 SLEEP_FACTOR( NetworkKeepAliveActive ),
5199 SLEEP_FACTOR( LocalUserActivity ),
5200 SLEEP_FACTOR( HibernateFailed ),
5201 SLEEP_FACTOR( ThermalWarning ),
5202 SLEEP_FACTOR( DisplayCaptured ),
5203 { 0, NULL }
5204 };
5205
5206 const IOPMSystemSleepPolicyTable * pt;
5207 OSSharedPtr<OSObject> prop;
5208 OSData * policyData;
5209 uint64_t currentFactors = 0;
5210 char currentFactorsBuf[512];
5211 uint32_t standbyDelay = 0;
5212 uint32_t powerOffDelay = 0;
5213 uint32_t powerOffTimer = 0;
5214 uint32_t standbyTimer = 0;
5215 uint32_t mismatch;
5216 bool standbyEnabled;
5217 bool powerOffEnabled;
5218 bool found = false;
5219
5220 // Get platform's sleep policy table
5221 if (!gSleepPolicyHandler) {
5222 prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5223 if (!prop) {
5224 goto done;
5225 }
5226 }
5227
5228 // Fetch additional settings
5229 standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5230 && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5231 powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5232 && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5233 if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5234 powerOffTimer = powerOffDelay;
5235 }
5236 if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5237 standbyTimer = standbyDelay;
5238 }
5239
5240 DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5241 sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5242 powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5243
5244 currentFactorsBuf[0] = 0;
5245 // pmset level overrides
5246 if ((*hibMode & kIOHibernateModeOn) == 0) {
5247 if (!gSleepPolicyHandler) {
5248 standbyEnabled = false;
5249 powerOffEnabled = false;
5250 }
5251 } else if (!(*hibMode & kIOHibernateModeSleep)) {
5252 // Force hibernate (i.e. mode 25)
5253 // If standby is enabled, force standy.
5254 // If poweroff is enabled, force poweroff.
5255 if (standbyEnabled) {
5256 currentFactors |= kIOPMSleepFactorStandbyForced;
5257 } else if (powerOffEnabled) {
5258 currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5259 } else {
5260 currentFactors |= kIOPMSleepFactorHibernateForced;
5261 }
5262 }
5263
5264 // Current factors based on environment and assertions
5265 if (sleepTimerMaintenance) {
5266 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5267 }
5268 if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5269 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5270 }
5271 if (!clamshellClosed) {
5272 currentFactors |= kIOPMSleepFactorLidOpen;
5273 }
5274 if (acAdaptorConnected) {
5275 currentFactors |= kIOPMSleepFactorACPower;
5276 }
5277 if (lowBatteryCondition) {
5278 hibernateMode = 0;
5279 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5280 if ((hibernateMode & kIOHibernateModeOn) == 0) {
5281 DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5282 } else {
5283 currentFactors |= kIOPMSleepFactorBatteryLow;
5284 }
5285 }
5286 if (!standbyDelay || !standbyTimer) {
5287 currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5288 }
5289 if (standbyNixed || !standbyEnabled) {
5290 currentFactors |= kIOPMSleepFactorStandbyDisabled;
5291 }
5292 if (resetTimers) {
5293 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5294 currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5295 }
5296 if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5297 kIOPMDriverAssertionLevelOff) {
5298 currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5299 }
5300 if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5301 kIOPMDriverAssertionLevelOff) {
5302 currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5303 }
5304 if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5305 kIOPMDriverAssertionLevelOff) {
5306 currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5307 }
5308 if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5309 kIOPMDriverAssertionLevelOff) {
5310 currentFactors |= kIOPMSleepFactorThunderboltDevice;
5311 }
5312 if (_scheduledAlarmMask != 0) {
5313 currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5314 }
5315 if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5316 kIOPMDriverAssertionLevelOff) {
5317 currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5318 }
5319 #define TCPKEEPALIVE 1
5320 #if TCPKEEPALIVE
5321 if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5322 kIOPMDriverAssertionLevelOff) {
5323 currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5324 }
5325 #endif
5326 if (!powerOffEnabled) {
5327 currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5328 }
5329 if (desktopMode) {
5330 currentFactors |= kIOPMSleepFactorExternalDisplay;
5331 }
5332 if (userWasActive) {
5333 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5334 }
5335 if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5336 currentFactors |= kIOPMSleepFactorHibernateFailed;
5337 }
5338 if (thermalWarningState) {
5339 currentFactors |= kIOPMSleepFactorThermalWarning;
5340 }
5341
5342 for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5343 uint32_t factor = 1 << factorBit;
5344 if (factor & currentFactors) {
5345 strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5346 strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5347 }
5348 }
5349 DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5350
5351 if (gSleepPolicyHandler) {
5352 uint32_t savedHibernateMode;
5353 IOReturn result;
5354
5355 if (!gSleepPolicyVars) {
5356 gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5357 }
5358 gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5359 gSleepPolicyVars->version = kIOPMSystemSleepPolicyVersion;
5360 gSleepPolicyVars->currentCapability = _currentCapability;
5361 gSleepPolicyVars->highestCapability = _highestCapability;
5362 gSleepPolicyVars->sleepFactors = currentFactors;
5363 gSleepPolicyVars->sleepReason = lastSleepReason;
5364 gSleepPolicyVars->sleepPhase = sleepPhase;
5365 gSleepPolicyVars->standbyDelay = standbyDelay;
5366 gSleepPolicyVars->standbyTimer = standbyTimer;
5367 gSleepPolicyVars->poweroffDelay = powerOffDelay;
5368 gSleepPolicyVars->scheduledAlarms = _scheduledAlarmMask | _userScheduledAlarmMask;
5369 gSleepPolicyVars->poweroffTimer = powerOffTimer;
5370
5371 if (kIOPMSleepPhase0 == sleepPhase) {
5372 // preserve hibernateMode
5373 savedHibernateMode = gSleepPolicyVars->hibernateMode;
5374 gSleepPolicyVars->hibernateMode = *hibMode;
5375 } else if (kIOPMSleepPhase1 == sleepPhase) {
5376 // use original hibernateMode for phase2
5377 gSleepPolicyVars->hibernateMode = *hibMode;
5378 }
5379
5380 result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5381
5382 if (kIOPMSleepPhase0 == sleepPhase) {
5383 // restore hibernateMode
5384 gSleepPolicyVars->hibernateMode = savedHibernateMode;
5385 }
5386
5387 if ((result != kIOReturnSuccess) ||
5388 (kIOPMSleepTypeInvalid == params->sleepType) ||
5389 (params->sleepType >= kIOPMSleepTypeLast) ||
5390 (kIOPMSystemSleepParametersVersion != params->version)) {
5391 MSG("sleep policy handler error\n");
5392 goto done;
5393 }
5394
5395 if ((getSleepTypeAttributes(params->sleepType) &
5396 kIOPMSleepAttributeHibernateSetup) &&
5397 ((*hibMode & kIOHibernateModeOn) == 0)) {
5398 *hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5399 }
5400
5401 DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5402 params->version, params->sleepType, params->sleepFlags,
5403 params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5404 found = true;
5405 goto done;
5406 }
5407
5408 // Policy table is meaningless without standby enabled
5409 if (!standbyEnabled) {
5410 goto done;
5411 }
5412
5413 // Validate the sleep policy table
5414 policyData = OSDynamicCast(OSData, prop.get());
5415 if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5416 goto done;
5417 }
5418
5419 pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5420 if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5421 (pt->version != 1) || (0 == pt->entryCount)) {
5422 goto done;
5423 }
5424
5425 if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5426 (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5427 goto done;
5428 }
5429
5430 for (uint32_t i = 0; i < pt->entryCount; i++) {
5431 const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5432 mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5433
5434 DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5435 entry->factorMask, entry->factorBits,
5436 entry->sleepFlags, entry->wakeEvents, mismatch);
5437 if (mismatch) {
5438 continue;
5439 }
5440
5441 DLOG("^ found match\n");
5442 found = true;
5443
5444 params->version = kIOPMSystemSleepParametersVersion;
5445 params->reserved1 = 1;
5446 if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5447 params->sleepType = kIOPMSleepTypeStandby;
5448 } else {
5449 params->sleepType = kIOPMSleepTypeNormalSleep;
5450 }
5451
5452 params->ecWakeEvents = entry->wakeEvents;
5453 if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5454 if (kIOPMSleepPhase2 == sleepPhase) {
5455 clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5456
5457 if (!_standbyTimerResetSeconds ||
5458 (now_secs <= _standbyTimerResetSeconds)) {
5459 // Reset standby timer adjustment
5460 _standbyTimerResetSeconds = now_secs;
5461 DLOG("standby delay %u, reset %u\n",
5462 standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5463 } else if (standbyDelay) {
5464 // Shorten the standby delay timer
5465 clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5466 if (standbyDelay > elapsed) {
5467 standbyDelay -= elapsed;
5468 } else {
5469 standbyDelay = 1; // must be > 0
5470 }
5471 DLOG("standby delay %u, elapsed %u\n",
5472 standbyDelay, (uint32_t) elapsed);
5473 }
5474 }
5475 params->ecWakeTimer = standbyDelay;
5476 } else if (kIOPMSleepPhase2 == sleepPhase) {
5477 // A sleep that does not enable the sleep timer will reset
5478 // the standby delay adjustment.
5479 _standbyTimerResetSeconds = 0;
5480 }
5481 break;
5482 }
5483
5484 done:
5485 return found;
5486 }
5487
5488 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5489
5490 void
5491 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5492 {
5493 // Evaluate early (priority interest phase), before drivers sleep.
5494
5495 DLOG("%s\n", __FUNCTION__);
5496 removeProperty(kIOPMSystemSleepParametersKey);
5497
5498 // Full wake resets the standby timer delay adjustment
5499 if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5500 _standbyTimerResetSeconds = 0;
5501 }
5502
5503 hibernateDisabled = false;
5504 hibernateMode = 0;
5505 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5506
5507 // Save for late evaluation if sleep is aborted
5508 bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5509
5510 if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5511 &hibernateMode)) {
5512 if (!hibernateRetry &&
5513 ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5514 kIOPMSleepAttributeHibernateSetup) == 0)) {
5515 // skip hibernate setup
5516 hibernateDisabled = true;
5517 }
5518 }
5519
5520 // Publish IOPMSystemSleepType
5521 uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5522 if (sleepType == kIOPMSleepTypeInvalid) {
5523 // no sleep policy
5524 sleepType = kIOPMSleepTypeNormalSleep;
5525 if (hibernateMode & kIOHibernateModeOn) {
5526 sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5527 kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5528 }
5529 } else if ((sleepType == kIOPMSleepTypeStandby) &&
5530 (gEarlySystemSleepParams.ecPoweroffTimer)) {
5531 // report the lowest possible sleep state
5532 sleepType = kIOPMSleepTypePowerOff;
5533 }
5534
5535 setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5536 }
5537
5538 void
5539 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5540 {
5541 IOPMSystemSleepParameters params;
5542 OSSharedPtr<OSData> paramsData;
5543 bool wakeNow;
5544 // Evaluate sleep policy after sleeping drivers but before platform sleep.
5545
5546 DLOG("%s\n", __FUNCTION__);
5547
5548 bzero(¶ms, sizeof(params));
5549 wakeNow = false;
5550 if (evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase2, &hibernateMode)) {
5551 if ((kIOPMSleepTypeStandby == params.sleepType)
5552 && gIOHibernateStandbyDisabled && gSleepPolicyVars
5553 && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5554 & gSleepPolicyVars->sleepFactors))) {
5555 standbyNixed = true;
5556 wakeNow = true;
5557 }
5558 if (wakeNow
5559 || ((hibernateDisabled || hibernateAborted) &&
5560 (getSleepTypeAttributes(params.sleepType) &
5561 kIOPMSleepAttributeHibernateSetup))) {
5562 // Final evaluation picked a state requiring hibernation,
5563 // but hibernate isn't going to proceed. Arm a short sleep using
5564 // the early non-hibernate sleep parameters.
5565 bcopy(&gEarlySystemSleepParams, ¶ms, sizeof(params));
5566 params.sleepType = kIOPMSleepTypeAbortedSleep;
5567 params.ecWakeTimer = 1;
5568 if (standbyNixed) {
5569 resetTimers = true;
5570 } else {
5571 // Set hibernateRetry flag to force hibernate setup on the
5572 // next sleep.
5573 hibernateRetry = true;
5574 }
5575 DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5576 params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5577 } else {
5578 hibernateRetry = false;
5579 }
5580
5581 if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5582 resetTimers = false;
5583 }
5584
5585 paramsData = OSData::withValue(params);
5586 if (paramsData) {
5587 setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5588 }
5589
5590 if (getSleepTypeAttributes(params.sleepType) &
5591 kIOPMSleepAttributeHibernateSleep) {
5592 // Disable sleep to force hibernation
5593 gIOHibernateMode &= ~kIOHibernateModeSleep;
5594 }
5595 }
5596 }
5597
5598 bool
5599 IOPMrootDomain::getHibernateSettings(
5600 uint32_t * hibernateModePtr,
5601 uint32_t * hibernateFreeRatio,
5602 uint32_t * hibernateFreeTime )
5603 {
5604 // Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5605 // has updated the hibernateDisabled flag.
5606
5607 bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5608 getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5609 getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5610 if (hibernateDisabled) {
5611 *hibernateModePtr = 0;
5612 } else if (gSleepPolicyHandler) {
5613 *hibernateModePtr = hibernateMode;
5614 }
5615 DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5616 return ok;
5617 }
5618
5619 bool
5620 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5621 {
5622 OSSharedPtr<OSObject> optionsProp;
5623 OSDictionary * optionsDict;
5624 OSSharedPtr<OSObject> obj;
5625 OSNumber * num;
5626 bool ok = false;
5627
5628 optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5629 optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5630
5631 if (optionsDict) {
5632 obj.reset(optionsDict->getObject(key), OSRetain);
5633 }
5634 if (!obj) {
5635 obj = copyProperty(key);
5636 }
5637 if (obj) {
5638 if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5639 *option = num->unsigned32BitValue();
5640 ok = true;
5641 } else if (OSDynamicCast(OSBoolean, obj.get())) {
5642 *option = (obj == kOSBooleanTrue) ? 1 : 0;
5643 ok = true;
5644 }
5645 }
5646
5647 return ok;
5648 }
5649 #endif /* HIBERNATION */
5650
5651 IOReturn
5652 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5653 {
5654 #if HIBERNATION
5655 IOPMSystemSleepParameters params;
5656 uint32_t hibMode = 0;
5657 bool ok;
5658
5659 if (gIOPMWorkLoop->inGate() == false) {
5660 IOReturn ret = gIOPMWorkLoop->runAction(
5661 OSMemberFunctionCast(IOWorkLoop::Action, this,
5662 &IOPMrootDomain::getSystemSleepType),
5663 (OSObject *) this,
5664 (void *) sleepType, (void *) standbyTimer);
5665 return ret;
5666 }
5667
5668 getSleepOption(kIOHibernateModeKey, &hibMode);
5669 bzero(¶ms, sizeof(params));
5670
5671 ok = evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase0, &hibMode);
5672 if (ok) {
5673 *sleepType = params.sleepType;
5674 if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5675 !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5676 DLOG("Standby delay is not set\n");
5677 *standbyTimer = 0;
5678 }
5679 return kIOReturnSuccess;
5680 }
5681 #endif
5682
5683 return kIOReturnUnsupported;
5684 }
5685
5686 // MARK: -
5687 // MARK: Shutdown and Restart
5688
5689 //******************************************************************************
5690 // handlePlatformHaltRestart
5691 //
5692 //******************************************************************************
5693
5694 // Phases while performing shutdown/restart
5695 typedef enum {
5696 kNotifyDone = 0x00,
5697 kNotifyPriorityClients = 0x10,
5698 kNotifyPowerPlaneDrivers = 0x20,
5699 kNotifyHaltRestartAction = 0x30,
5700 kQuiescePM = 0x40,
5701 } shutdownPhase_t;
5702
5703
5704 struct HaltRestartApplierContext {
5705 IOPMrootDomain * RootDomain;
5706 unsigned long PowerState;
5707 IOPMPowerFlags PowerFlags;
5708 UInt32 MessageType;
5709 UInt32 Counter;
5710 const char * LogString;
5711 shutdownPhase_t phase;
5712
5713 IOServiceInterestHandler handler;
5714 } gHaltRestartCtx;
5715
5716 const char *
5717 shutdownPhase2String(shutdownPhase_t phase)
5718 {
5719 switch (phase) {
5720 case kNotifyDone:
5721 return "Notifications completed";
5722 case kNotifyPriorityClients:
5723 return "Notifying priority clients";
5724 case kNotifyPowerPlaneDrivers:
5725 return "Notifying power plane drivers";
5726 case kNotifyHaltRestartAction:
5727 return "Notifying HaltRestart action handlers";
5728 case kQuiescePM:
5729 return "Quiescing PM";
5730 default:
5731 return "Unknown";
5732 }
5733 }
5734
5735 static void
5736 platformHaltRestartApplier( OSObject * object, void * context )
5737 {
5738 IOPowerStateChangeNotification notify;
5739 HaltRestartApplierContext * ctx;
5740 AbsoluteTime startTime, elapsedTime;
5741 uint32_t deltaTime;
5742
5743 ctx = (HaltRestartApplierContext *) context;
5744
5745 _IOServiceInterestNotifier * notifier;
5746 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5747 memset(¬ify, 0, sizeof(notify));
5748 notify.powerRef = (void *)(uintptr_t)ctx->Counter;
5749 notify.returnValue = 0;
5750 notify.stateNumber = ctx->PowerState;
5751 notify.stateFlags = ctx->PowerFlags;
5752
5753 if (notifier) {
5754 ctx->handler = notifier->handler;
5755 }
5756
5757 clock_get_uptime(&startTime);
5758 ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)¬ify );
5759 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5760
5761 if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5762 LOG("%s handler %p took %u ms\n",
5763 ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5764 halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5765 }
5766
5767 ctx->handler = NULL;
5768 ctx->Counter++;
5769 }
5770
5771 static void
5772 quiescePowerTreeCallback( void * target, void * param )
5773 {
5774 IOLockLock(gPMHaltLock);
5775 gPMQuiesced = true;
5776 thread_wakeup(param);
5777 IOLockUnlock(gPMHaltLock);
5778 }
5779
5780 void
5781 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5782 {
5783 AbsoluteTime startTime, elapsedTime;
5784 uint32_t deltaTime;
5785 bool nvramSync = false;
5786
5787 memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5788 gHaltRestartCtx.RootDomain = this;
5789
5790 clock_get_uptime(&startTime);
5791 switch (pe_type) {
5792 case kPEHaltCPU:
5793 case kPEUPSDelayHaltCPU:
5794 gHaltRestartCtx.PowerState = OFF_STATE;
5795 gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5796 gHaltRestartCtx.LogString = "PowerOff";
5797 nvramSync = true;
5798 break;
5799
5800 case kPERestartCPU:
5801 gHaltRestartCtx.PowerState = RESTART_STATE;
5802 gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5803 gHaltRestartCtx.LogString = "Restart";
5804 nvramSync = true;
5805 break;
5806
5807 case kPEPagingOff:
5808 gHaltRestartCtx.PowerState = ON_STATE;
5809 gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5810 gHaltRestartCtx.LogString = "PagingOff";
5811 IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5812 #if HIBERNATION
5813 IOHibernateSystemRestart();
5814 #endif
5815 break;
5816
5817 default:
5818 return;
5819 }
5820
5821 if (nvramSync) {
5822 PESyncNVRAM();
5823 }
5824
5825 gHaltRestartCtx.phase = kNotifyPriorityClients;
5826 // Notify legacy clients
5827 applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5828
5829 // For normal shutdown, turn off File Server Mode.
5830 if (kPEHaltCPU == pe_type) {
5831 OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5832 OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5833 if (setting && num) {
5834 setPMSetting(setting.get(), num.get());
5835 }
5836 }
5837
5838 if (kPEPagingOff != pe_type) {
5839 gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5840 // Notify in power tree order
5841 notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5842 }
5843
5844 gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5845 #if defined(XNU_TARGET_OS_OSX)
5846 IOCPURunPlatformHaltRestartActions(pe_type);
5847 #else /* !defined(XNU_TARGET_OS_OSX) */
5848 if (kPEPagingOff != pe_type) {
5849 IOCPURunPlatformHaltRestartActions(pe_type);
5850 }
5851 #endif /* !defined(XNU_TARGET_OS_OSX) */
5852
5853 // Wait for PM to quiesce
5854 if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5855 gHaltRestartCtx.phase = kQuiescePM;
5856 AbsoluteTime quiesceTime = mach_absolute_time();
5857
5858 IOLockLock(gPMHaltLock);
5859 gPMQuiesced = false;
5860 if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5861 kIOReturnSuccess) {
5862 while (!gPMQuiesced) {
5863 IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5864 }
5865 }
5866 IOLockUnlock(gPMHaltLock);
5867 deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5868 DLOG("PM quiesce took %u ms\n", deltaTime);
5869 halt_log_enter("Quiesce", NULL, elapsedTime);
5870 }
5871 gHaltRestartCtx.phase = kNotifyDone;
5872
5873 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5874 LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5875
5876 halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5877
5878 deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5879 LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5880
5881 if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5882 printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5883 }
5884
5885 checkShutdownTimeout();
5886 }
5887
5888 bool
5889 IOPMrootDomain::checkShutdownTimeout()
5890 {
5891 AbsoluteTime elapsedTime;
5892 uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5893
5894 if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5895 return true;
5896 }
5897 return false;
5898 }
5899
5900 void
5901 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5902 {
5903 if (gHaltLog) {
5904 if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5905 halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5906 }
5907 panic("%s timed out in phase '%s'. Total %d ms:%s",
5908 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5909 } else {
5910 panic("%s timed out in phase \'%s\'. Total %d ms",
5911 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5912 }
5913 }
5914
5915 //******************************************************************************
5916 // shutdownSystem
5917 //
5918 //******************************************************************************
5919
5920 IOReturn
5921 IOPMrootDomain::shutdownSystem( void )
5922 {
5923 return kIOReturnUnsupported;
5924 }
5925
5926 //******************************************************************************
5927 // restartSystem
5928 //
5929 //******************************************************************************
5930
5931 IOReturn
5932 IOPMrootDomain::restartSystem( void )
5933 {
5934 return kIOReturnUnsupported;
5935 }
5936
5937 // MARK: -
5938 // MARK: System Capability
5939
5940 //******************************************************************************
5941 // tagPowerPlaneService
5942 //
5943 // Running on PM work loop thread.
5944 //******************************************************************************
5945
5946 void
5947 IOPMrootDomain::tagPowerPlaneService(
5948 IOService * service,
5949 IOPMActions * actions,
5950 IOPMPowerStateIndex maxPowerState )
5951 {
5952 uint32_t flags = 0;
5953
5954 memset(actions, 0, sizeof(*actions));
5955 actions->target = this;
5956
5957 if (service == this) {
5958 actions->actionPowerChangeStart =
5959 OSMemberFunctionCast(
5960 IOPMActionPowerChangeStart, this,
5961 &IOPMrootDomain::handleOurPowerChangeStart);
5962
5963 actions->actionPowerChangeDone =
5964 OSMemberFunctionCast(
5965 IOPMActionPowerChangeDone, this,
5966 &IOPMrootDomain::handleOurPowerChangeDone);
5967
5968 actions->actionPowerChangeOverride =
5969 OSMemberFunctionCast(
5970 IOPMActionPowerChangeOverride, this,
5971 &IOPMrootDomain::overrideOurPowerChange);
5972 return;
5973 }
5974
5975 #if DISPLAY_WRANGLER_PRESENT
5976 if (NULL != service->metaCast("IODisplayWrangler")) {
5977 // XXX should this really retain?
5978 wrangler.reset(service, OSRetain);
5979 wrangler->registerInterest(gIOGeneralInterest,
5980 &displayWranglerNotification, this, NULL);
5981
5982 // found the display wrangler, check for any display assertions already created
5983 if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5984 DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5985 wrangler->setIgnoreIdleTimer( true );
5986 }
5987 flags |= kPMActionsFlagIsDisplayWrangler;
5988 }
5989 #endif /* DISPLAY_WRANGLER_PRESENT */
5990
5991 if (service->propertyExists("IOPMStrictTreeOrder")) {
5992 flags |= kPMActionsFlagIsGraphicsDriver;
5993 }
5994 if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5995 flags |= kPMActionsFlagIsAudioDriver;
5996 }
5997
5998 // Find the power connection object that is a child of the PCI host
5999 // bridge, and has a graphics/audio device attached below. Mark the
6000 // power branch for delayed child notifications.
6001
6002 if (flags) {
6003 IORegistryEntry * child = service;
6004 IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
6005
6006 while (child != this) {
6007 if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
6008 // Skip delaying notifications and clamping power on external graphics and audio devices.
6009 DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
6010 flags = 0;
6011 break;
6012 }
6013 if ((parent == pciHostBridgeDriver) ||
6014 (parent == this)) {
6015 if (OSDynamicCast(IOPowerConnection, child)) {
6016 IOPowerConnection * conn = (IOPowerConnection *) child;
6017 conn->delayChildNotification = true;
6018 DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
6019 }
6020 break;
6021 }
6022 child = parent;
6023 parent = child->getParentEntry(gIOPowerPlane);
6024 }
6025 }
6026
6027 OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
6028 if (prop) {
6029 OSNumber * num = OSDynamicCast(OSNumber, prop.get());
6030 if (num) {
6031 actions->darkWakePowerState = num->unsigned32BitValue();
6032 if (actions->darkWakePowerState < maxPowerState) {
6033 flags |= kPMActionsFlagHasDarkWakePowerState;
6034 }
6035 }
6036 }
6037
6038
6039 if (flags) {
6040 DLOG("%s tag flags %x\n", service->getName(), flags);
6041 actions->flags |= flags;
6042 actions->actionPowerChangeOverride =
6043 OSMemberFunctionCast(
6044 IOPMActionPowerChangeOverride, this,
6045 &IOPMrootDomain::overridePowerChangeForService);
6046
6047 if (flags & kPMActionsFlagIsDisplayWrangler) {
6048 actions->actionActivityTickle =
6049 OSMemberFunctionCast(
6050 IOPMActionActivityTickle, this,
6051 &IOPMrootDomain::handleActivityTickleForDisplayWrangler);
6052
6053 actions->actionUpdatePowerClient =
6054 OSMemberFunctionCast(
6055 IOPMActionUpdatePowerClient, this,
6056 &IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
6057 }
6058 return;
6059 }
6060
6061 // Locate the first PCI host bridge for PMTrace.
6062 if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
6063 IOService * provider = service->getProvider();
6064 if (OSDynamicCast(IOPlatformDevice, provider) &&
6065 provider->inPlane(gIODTPlane)) {
6066 pciHostBridgeDevice.reset(provider, OSNoRetain);
6067 pciHostBridgeDriver.reset(service, OSNoRetain);
6068 DLOG("PMTrace found PCI host bridge %s->%s\n",
6069 provider->getName(), service->getName());
6070 }
6071 }
6072
6073 // Tag top-level PCI devices. The order of PMinit() call does not
6074 // change across boots and is used as the PCI bit number.
6075 if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
6076 // Would prefer to check built-in property, but tagPowerPlaneService()
6077 // is called before pciDevice->registerService().
6078 IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
6079 if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
6080 int bit = pmTracer->recordTopLevelPCIDevice( service );
6081 if (bit >= 0) {
6082 // Save the assigned bit for fast lookup.
6083 actions->flags |= (bit & kPMActionsPCIBitNumberMask);
6084
6085 actions->actionPowerChangeStart =
6086 OSMemberFunctionCast(
6087 IOPMActionPowerChangeStart, this,
6088 &IOPMrootDomain::handlePowerChangeStartForPCIDevice);
6089
6090 actions->actionPowerChangeDone =
6091 OSMemberFunctionCast(
6092 IOPMActionPowerChangeDone, this,
6093 &IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
6094 }
6095 }
6096 }
6097 }
6098
6099 //******************************************************************************
6100 // PM actions for root domain
6101 //******************************************************************************
6102
6103 void
6104 IOPMrootDomain::overrideOurPowerChange(
6105 IOService * service,
6106 IOPMActions * actions,
6107 const IOPMRequest * request,
6108 IOPMPowerStateIndex * inOutPowerState,
6109 IOPMPowerChangeFlags * inOutChangeFlags )
6110 {
6111 uint32_t changeFlags = *inOutChangeFlags;
6112 uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6113 uint32_t currentPowerState = (uint32_t) getPowerState();
6114
6115 if (request->getTag() == 0) {
6116 // Set a tag for any request that originates from IOServicePM
6117 (const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6118 }
6119
6120 DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6121 getPowerStateString(currentPowerState),
6122 getPowerStateString(desiredPowerState),
6123 _currentCapability, changeFlags,
6124 request->getTag());
6125
6126
6127 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6128 /*
6129 * ASBM send lowBattery notifications every 1 second until the device
6130 * enters hibernation. This queues up multiple sleep requests.
6131 * After the device wakes from hibernation, none of these previously
6132 * queued sleep requests are valid.
6133 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6134 * and is cleared at the very last point in sleep.
6135 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6136 * lowBatteryCondition is invalid
6137 */
6138 if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6139 if (!lowBatteryCondition) {
6140 DLOG("Duplicate lowBattery sleep");
6141 *inOutChangeFlags |= kIOPMNotDone;
6142 return;
6143 }
6144 }
6145 #endif
6146
6147 if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6148 // Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6149 *inOutChangeFlags |= kIOPMNotDone;
6150 return;
6151 }
6152
6153 if (changeFlags & kIOPMParentInitiated) {
6154 // Root parent is permanently pegged at max power,
6155 // a parent initiated power change is unexpected.
6156 *inOutChangeFlags |= kIOPMNotDone;
6157 return;
6158 }
6159
6160 #if HIBERNATION && defined(__arm64__)
6161 if (lowBatteryCondition && (desiredPowerState < currentPowerState)) {
6162 if (!ml_is_secure_hib_supported()) {
6163 // If hibernation is unsupported, reject sleep requests to avoid
6164 // racing with system shutdown.
6165 *inOutChangeFlags |= kIOPMNotDone;
6166 return;
6167 }
6168 }
6169 #endif /* HIBERNATION && defined(__arm64__) */
6170
6171 if (desiredPowerState < currentPowerState) {
6172 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6173 // Root domain is dropping power state from ON->SLEEP.
6174 // If system is in full wake, first enter dark wake by
6175 // converting the power drop to a capability change.
6176 // Once in dark wake, transition to sleep state ASAP.
6177
6178 darkWakeToSleepASAP = true;
6179
6180 // Drop graphics and audio capability
6181 _desiredCapability &= ~(
6182 kIOPMSystemCapabilityGraphics |
6183 kIOPMSystemCapabilityAudio);
6184
6185 // Convert to capability change (ON->ON)
6186 *inOutPowerState = getRUN_STATE();
6187 *inOutChangeFlags |= kIOPMSynchronize;
6188
6189 // Revert device desire from SLEEP to ON
6190 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6191 } else {
6192 // System is already in dark wake, ok to drop power state.
6193 // Broadcast root power down to entire tree.
6194 *inOutChangeFlags |= kIOPMRootChangeDown;
6195 }
6196 } else if (desiredPowerState > currentPowerState) {
6197 if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6198 // Broadcast power up when waking from sleep, but not for the
6199 // initial power change at boot by checking for cpu capability.
6200 *inOutChangeFlags |= kIOPMRootChangeUp;
6201 }
6202 }
6203 }
6204
6205 void
6206 IOPMrootDomain::handleOurPowerChangeStart(
6207 IOService * service,
6208 IOPMActions * actions,
6209 const IOPMRequest * request,
6210 IOPMPowerStateIndex newPowerState,
6211 IOPMPowerChangeFlags * inOutChangeFlags )
6212 {
6213 IOPMRequestTag requestTag = request->getTag();
6214 IOPMRequestTag sleepReason;
6215
6216 uint32_t changeFlags = *inOutChangeFlags;
6217 uint32_t currentPowerState = (uint32_t) getPowerState();
6218 bool publishSleepReason = false;
6219
6220 // Check if request has a valid sleep reason
6221 sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6222 if (sleepReason < kIOPMSleepReasonClamshell) {
6223 sleepReason = kIOPMSleepReasonIdle;
6224 }
6225
6226 _systemTransitionType = kSystemTransitionNone;
6227 _systemMessageClientMask = 0;
6228 capabilityLoss = false;
6229 toldPowerdCapWillChange = false;
6230
6231 // Emergency notifications may arrive after the initial sleep request
6232 // has been queued. Override the sleep reason so powerd and others can
6233 // treat this as an emergency sleep.
6234 if (lowBatteryCondition) {
6235 sleepReason = kIOPMSleepReasonLowPower;
6236 } else if (thermalEmergencyState) {
6237 sleepReason = kIOPMSleepReasonThermalEmergency;
6238 }
6239
6240 // 1. Explicit capability change.
6241 if (changeFlags & kIOPMSynchronize) {
6242 if (newPowerState == ON_STATE) {
6243 if (changeFlags & kIOPMSyncNoChildNotify) {
6244 _systemTransitionType = kSystemTransitionNewCapClient;
6245 } else {
6246 _systemTransitionType = kSystemTransitionCapability;
6247 }
6248 }
6249 }
6250 // 2. Going to sleep (cancellation still possible).
6251 else if (newPowerState < currentPowerState) {
6252 _systemTransitionType = kSystemTransitionSleep;
6253 }
6254 // 3. Woke from (idle or demand) sleep.
6255 else if (!systemBooting &&
6256 (changeFlags & kIOPMSelfInitiated) &&
6257 (newPowerState > currentPowerState)) {
6258 _systemTransitionType = kSystemTransitionWake;
6259 _desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6260
6261 // Early exit from dark wake to full (e.g. LID open)
6262 if (kFullWakeReasonNone != fullWakeReason) {
6263 _desiredCapability |= (
6264 kIOPMSystemCapabilityGraphics |
6265 kIOPMSystemCapabilityAudio);
6266
6267 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6268 if (fullWakeReason == kFullWakeReasonLocalUser) {
6269 darkWakeExit = true;
6270 darkWakeToSleepASAP = false;
6271 setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6272 kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6273 }
6274 #endif
6275 }
6276 #if HIBERNATION
6277 IOHibernateSetWakeCapabilities(_desiredCapability);
6278 #endif
6279 }
6280
6281 // Update pending wake capability at the beginning of every
6282 // state transition (including synchronize). This will become
6283 // the current capability at the end of the transition.
6284
6285 if (kSystemTransitionSleep == _systemTransitionType) {
6286 _pendingCapability = 0;
6287 capabilityLoss = true;
6288 } else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6289 _pendingCapability = _desiredCapability |
6290 kIOPMSystemCapabilityCPU |
6291 kIOPMSystemCapabilityNetwork;
6292
6293 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6294 _pendingCapability |= kIOPMSystemCapabilityAudio;
6295 }
6296
6297 if ((kSystemTransitionCapability == _systemTransitionType) &&
6298 (_pendingCapability == _currentCapability)) {
6299 // Cancel the PM state change.
6300 _systemTransitionType = kSystemTransitionNone;
6301 *inOutChangeFlags |= kIOPMNotDone;
6302 }
6303 if (__builtin_popcount(_pendingCapability) <
6304 __builtin_popcount(_currentCapability)) {
6305 capabilityLoss = true;
6306 }
6307 }
6308
6309 // 1. Capability change.
6310 if (kSystemTransitionCapability == _systemTransitionType) {
6311 // Dark to Full transition.
6312 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6313 tracePoint( kIOPMTracePointDarkWakeExit );
6314
6315 #if defined(XNU_TARGET_OS_OSX)
6316 // rdar://problem/65627936
6317 // When a dark->full wake promotion is scheduled before an ON->SLEEP
6318 // power state drop, invalidate any request to drop power state already
6319 // in the queue, including the override variant, unless full wake cannot
6320 // be sustained. Any power state drop queued after this SustainFullWake
6321 // request will not be affected.
6322 if (checkSystemCanSustainFullWake()) {
6323 changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6324 }
6325 #endif
6326
6327 willEnterFullWake();
6328 }
6329
6330 // Full to Dark transition.
6331 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6332 // Clear previous stats
6333 IOLockLock(pmStatsLock);
6334 if (pmStatsAppResponses) {
6335 pmStatsAppResponses = OSArray::withCapacity(5);
6336 }
6337 IOLockUnlock(pmStatsLock);
6338
6339 tracePoint( kIOPMTracePointDarkWakeEntry );
6340 *inOutChangeFlags |= kIOPMSyncTellPowerDown;
6341 _systemMessageClientMask = kSystemMessageClientPowerd |
6342 kSystemMessageClientLegacyApp;
6343
6344 // rdar://15971327
6345 // Prevent user active transitions before notifying clients
6346 // that system will sleep.
6347 preventTransitionToUserActive(true);
6348
6349 IOService::setAdvisoryTickleEnable( false );
6350
6351 // Publish the sleep reason for full to dark wake
6352 publishSleepReason = true;
6353 lastSleepReason = fullToDarkReason = sleepReason;
6354
6355 // Publish a UUID for the Sleep --> Wake cycle
6356 handlePublishSleepWakeUUID(true);
6357 if (sleepDelaysReport) {
6358 clock_get_uptime(&ts_sleepStart);
6359 DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6360 }
6361
6362 darkWakeExit = false;
6363 }
6364 }
6365 // 2. System sleep.
6366 else if (kSystemTransitionSleep == _systemTransitionType) {
6367 // Beginning of a system sleep transition.
6368 // Cancellation is still possible.
6369 tracePoint( kIOPMTracePointSleepStarted );
6370
6371 _systemMessageClientMask = kSystemMessageClientAll;
6372 if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6373 _systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6374 }
6375 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6376 // Kernel priority clients are only notified on the initial
6377 // transition to full wake, so don't notify them unless system
6378 // has gained graphics capability since the last system wake.
6379 _systemMessageClientMask &= ~kSystemMessageClientKernel;
6380 } else {
6381 // System was in full wake, but the downwards power transition is driven
6382 // by a request that originates from IOServicePM, so it isn't tagged with
6383 // a valid system sleep reason.
6384 if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6385 // Publish the same reason for full to dark
6386 sleepReason = fullToDarkReason;
6387 }
6388 }
6389 #if HIBERNATION
6390 gIOHibernateState = 0;
6391 #endif
6392
6393 // Record the reason for dark wake back to sleep
6394 // System may not have ever achieved full wake
6395
6396 publishSleepReason = true;
6397 lastSleepReason = sleepReason;
6398 if (sleepDelaysReport) {
6399 clock_get_uptime(&ts_sleepStart);
6400 DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6401 }
6402 }
6403 // 3. System wake.
6404 else if (kSystemTransitionWake == _systemTransitionType) {
6405 tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6406 // Clear stats about sleep
6407
6408 if (AOT_STATE == newPowerState) {
6409 _pendingCapability = 0;
6410 }
6411
6412 if (AOT_STATE == currentPowerState) {
6413 // Wake events are no longer accepted after waking to AOT_STATE.
6414 // Re-enable wake event acceptance to append wake events claimed
6415 // during the AOT to ON_STATE transition.
6416 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6417 }
6418
6419 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6420 willEnterFullWake();
6421 }
6422 }
6423
6424 // The only location where the sleep reason is published. At this point
6425 // sleep can still be cancelled, but sleep reason should be published
6426 // early for logging purposes.
6427
6428 if (publishSleepReason) {
6429 static const char * IOPMSleepReasons[] =
6430 {
6431 kIOPMClamshellSleepKey,
6432 kIOPMPowerButtonSleepKey,
6433 kIOPMSoftwareSleepKey,
6434 kIOPMOSSwitchHibernationKey,
6435 kIOPMIdleSleepKey,
6436 kIOPMLowPowerSleepKey,
6437 kIOPMThermalEmergencySleepKey,
6438 kIOPMMaintenanceSleepKey,
6439 kIOPMSleepServiceExitKey,
6440 kIOPMDarkWakeThermalEmergencyKey,
6441 kIOPMNotificationWakeExitKey
6442 };
6443
6444 // Record sleep cause in IORegistry
6445 uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6446 if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6447 DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6448 #if DEVELOPMENT || DEBUG
6449 record_system_event(SYSTEM_EVENT_TYPE_INFO,
6450 SYSTEM_EVENT_SUBSYSTEM_PMRD,
6451 "Sleep Reason", "%s\n", IOPMSleepReasons[reasonIndex]
6452 );
6453 #endif /* DEVELOPMENT || DEBUG */
6454 setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6455 }
6456 }
6457
6458 if ((kSystemTransitionNone != _systemTransitionType) &&
6459 (kSystemTransitionNewCapClient != _systemTransitionType)) {
6460 _systemStateGeneration++;
6461 systemDarkWake = false;
6462
6463 DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6464 getPowerStateString(currentPowerState),
6465 getPowerStateString((uint32_t) newPowerState),
6466 _currentCapability, _pendingCapability,
6467 *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6468 requestTag);
6469 #if DEVELOPMENT || DEBUG
6470 if (currentPowerState != (uint32_t) newPowerState) {
6471 record_system_event(SYSTEM_EVENT_TYPE_INFO,
6472 SYSTEM_EVENT_SUBSYSTEM_PMRD,
6473 "Start Power State Trans.",
6474 "(%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6475 getPowerStateString(currentPowerState),
6476 getPowerStateString((uint32_t) newPowerState),
6477 _currentCapability,
6478 _pendingCapability,
6479 *inOutChangeFlags,
6480 _systemStateGeneration,
6481 _systemMessageClientMask,
6482 requestTag
6483 );
6484 }
6485 #endif /* DEVELOPMENT || DEBUG */
6486 }
6487
6488 if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6489 panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6490 }
6491 if (_aotNow && (ON_STATE == newPowerState)) {
6492 WAKEEVENT_LOCK();
6493 aotShouldExit(true);
6494 WAKEEVENT_UNLOCK();
6495 aotExit(false);
6496 }
6497 }
6498
6499 void
6500 IOPMrootDomain::handleOurPowerChangeDone(
6501 IOService * service,
6502 IOPMActions * actions,
6503 const IOPMRequest * request,
6504 IOPMPowerStateIndex oldPowerState,
6505 IOPMPowerChangeFlags changeFlags )
6506 {
6507 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6508 _systemTransitionType = kSystemTransitionNone;
6509 return;
6510 }
6511
6512 if (_systemTransitionType != kSystemTransitionNone) {
6513 uint32_t currentPowerState = (uint32_t) getPowerState();
6514
6515 if (changeFlags & kIOPMNotDone) {
6516 // Power down was cancelled or vetoed.
6517 _pendingCapability = _currentCapability;
6518 lastSleepReason = 0;
6519
6520 // When sleep is cancelled or reverted, don't report
6521 // the target (lower) power state as the previous state.
6522 oldPowerState = currentPowerState;
6523
6524 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6525 CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6526 #if defined(XNU_TARGET_OS_OSX)
6527 pmPowerStateQueue->submitPowerEvent(
6528 kPowerEventPolicyStimulus,
6529 (void *) kStimulusDarkWakeReentry,
6530 _systemStateGeneration );
6531 #else /* !defined(XNU_TARGET_OS_OSX) */
6532 // On embedded, there are no factors that can prolong a
6533 // "darkWake" when a power down is vetoed. We need to
6534 // promote to "fullWake" at least once so that factors
6535 // that prevent idle sleep can assert themselves if required
6536 pmPowerStateQueue->submitPowerEvent(
6537 kPowerEventPolicyStimulus,
6538 (void *) kStimulusDarkWakeActivityTickle);
6539 #endif /* !defined(XNU_TARGET_OS_OSX) */
6540 }
6541
6542 // Revert device desire to max.
6543 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6544 } else {
6545 // Send message on dark wake to full wake promotion.
6546 // tellChangeUp() handles the normal SLEEP->ON case.
6547
6548 if (kSystemTransitionCapability == _systemTransitionType) {
6549 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6550 lastSleepReason = 0; // stop logging wrangler tickles
6551 tellClients(kIOMessageSystemHasPoweredOn);
6552 }
6553 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6554 // Going dark, reset full wake state
6555 // userIsActive will be cleared by wrangler powering down
6556 fullWakeReason = kFullWakeReasonNone;
6557
6558 if (ts_sleepStart) {
6559 clock_get_uptime(&wake2DarkwakeDelay);
6560 SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6561 DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6562 ts_sleepStart = 0;
6563 }
6564 }
6565 }
6566
6567 // Reset state after exiting from dark wake.
6568
6569 if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6570 CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6571 darkWakeMaintenance = false;
6572 darkWakeToSleepASAP = false;
6573 pciCantSleepValid = false;
6574 darkWakeSleepService = false;
6575
6576 if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6577 // Remove the influence of display power assertion
6578 // before next system wake.
6579 if (wrangler) {
6580 wrangler->changePowerStateForRootDomain(
6581 kWranglerPowerStateMin );
6582 }
6583 removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6584 }
6585 }
6586
6587 // Entered dark mode.
6588
6589 if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6590 (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6591 // Queue an evaluation of whether to remain in dark wake,
6592 // and for how long. This serves the purpose of draining
6593 // any assertions from the queue.
6594
6595 pmPowerStateQueue->submitPowerEvent(
6596 kPowerEventPolicyStimulus,
6597 (void *) kStimulusDarkWakeEntry,
6598 _systemStateGeneration );
6599 }
6600 }
6601
6602 #if DEVELOPMENT || DEBUG
6603 if (currentPowerState != (uint32_t) oldPowerState) {
6604 record_system_event(SYSTEM_EVENT_TYPE_INFO,
6605 SYSTEM_EVENT_SUBSYSTEM_PMRD,
6606 "Finish Power State Trans.",
6607 "(%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6608 getPowerStateString((uint32_t)oldPowerState),
6609 getPowerStateString(currentPowerState),
6610 _currentCapability,
6611 _pendingCapability,
6612 changeFlags,
6613 _systemStateGeneration,
6614 _systemMessageClientMask,
6615 request->getTag()
6616 );
6617 }
6618 #endif /* DEVELOPMENT || DEBUG */
6619
6620 DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6621 getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6622 _currentCapability, _pendingCapability,
6623 changeFlags, _systemStateGeneration, _systemMessageClientMask,
6624 request->getTag());
6625
6626 if ((currentPowerState == ON_STATE) && pmAssertions) {
6627 pmAssertions->reportCPUBitAccounting();
6628 }
6629
6630 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6631 displayWakeCnt++;
6632 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6633 if (clamshellExists && fullWakeThreadCall) {
6634 AbsoluteTime deadline;
6635 clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6636 thread_call_enter_delayed(fullWakeThreadCall, deadline);
6637 }
6638 #endif
6639 } else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6640 darkWakeCnt++;
6641 }
6642
6643 // Update current system capability.
6644 if (_currentCapability != _pendingCapability) {
6645 _currentCapability = _pendingCapability;
6646 }
6647
6648 // Update highest system capability.
6649
6650 _highestCapability |= _currentCapability;
6651
6652 if (darkWakePostTickle &&
6653 (kSystemTransitionWake == _systemTransitionType) &&
6654 (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6655 kDarkWakeFlagPromotionLate) {
6656 darkWakePostTickle = false;
6657 reportUserInput();
6658 } else if (darkWakeExit) {
6659 requestFullWake( kFullWakeReasonLocalUser );
6660 }
6661
6662 // Reset tracepoint at completion of capability change,
6663 // completion of wake transition, and aborted sleep transition.
6664
6665 if ((_systemTransitionType == kSystemTransitionCapability) ||
6666 (_systemTransitionType == kSystemTransitionWake) ||
6667 ((_systemTransitionType == kSystemTransitionSleep) &&
6668 (changeFlags & kIOPMNotDone))) {
6669 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6670 tracePoint( kIOPMTracePointSystemUp );
6671 }
6672
6673 _systemTransitionType = kSystemTransitionNone;
6674 _systemMessageClientMask = 0;
6675 toldPowerdCapWillChange = false;
6676
6677 darkWakeLogClamp = false;
6678
6679 if (lowBatteryCondition) {
6680 privateSleepSystem(kIOPMSleepReasonLowPower);
6681 } else if (thermalEmergencyState) {
6682 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6683 } else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6684 // Request for full wake is removed while system is waking up to full wake
6685 DLOG("DisplayOn fullwake request is removed\n");
6686 handleSetDisplayPowerOn(false);
6687 }
6688
6689 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6690 pmPowerStateQueue->submitPowerEvent(
6691 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6692 }
6693 }
6694 }
6695
6696 //******************************************************************************
6697 // PM actions for graphics and audio.
6698 //******************************************************************************
6699
6700 void
6701 IOPMrootDomain::overridePowerChangeForService(
6702 IOService * service,
6703 IOPMActions * actions,
6704 const IOPMRequest * request,
6705 IOPMPowerStateIndex * inOutPowerState,
6706 IOPMPowerChangeFlags * inOutChangeFlags )
6707 {
6708 uint32_t powerState = (uint32_t) *inOutPowerState;
6709 uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6710 const uint32_t actionFlags = actions->flags;
6711
6712 if (kSystemTransitionNone == _systemTransitionType) {
6713 // Not in midst of a system transition.
6714 // Do not set kPMActionsStatePowerClamped.
6715 } else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6716 bool enableClamp = false;
6717
6718 // For most drivers, enable the clamp during ON->Dark transition
6719 // which has the kIOPMSynchronize flag set in changeFlags.
6720 if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6721 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6722 (changeFlags & kIOPMSynchronize)) {
6723 enableClamp = true;
6724 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6725 ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6726 ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6727 (changeFlags & kIOPMSynchronize)) {
6728 enableClamp = true;
6729 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6730 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6731 (changeFlags & kIOPMSynchronize)) {
6732 enableClamp = true;
6733 } else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6734 (_systemTransitionType == kSystemTransitionSleep)) {
6735 // For graphics drivers, clamp power when entering
6736 // system sleep. Not when dropping to dark wake.
6737 enableClamp = true;
6738 }
6739
6740 if (enableClamp) {
6741 actions->state |= kPMActionsStatePowerClamped;
6742 DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6743 service->getName(), service->getRegistryEntryID(),
6744 _pendingCapability, powerState, changeFlags);
6745 }
6746 } else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6747 bool disableClamp = false;
6748
6749 if ((actionFlags & (
6750 kPMActionsFlagIsDisplayWrangler |
6751 kPMActionsFlagIsGraphicsDriver)) &&
6752 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6753 disableClamp = true;
6754 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6755 (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6756 disableClamp = true;
6757 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6758 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6759 disableClamp = true;
6760 }
6761
6762 if (disableClamp) {
6763 actions->state &= ~kPMActionsStatePowerClamped;
6764 DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6765 service->getName(), service->getRegistryEntryID(),
6766 _pendingCapability, powerState, changeFlags);
6767 }
6768 }
6769
6770 if (actions->state & kPMActionsStatePowerClamped) {
6771 uint32_t maxPowerState = 0;
6772
6773 // Determine the max power state allowed when clamp is enabled
6774 if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6775 // Parent intiated power state changes
6776 if ((service->getPowerState() > maxPowerState) &&
6777 (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6778 maxPowerState++;
6779
6780 // Remove lingering effects of any tickle before entering
6781 // dark wake. It will take a new tickle to return to full
6782 // wake, so the existing tickle state is useless.
6783
6784 if (changeFlags & kIOPMDomainDidChange) {
6785 *inOutChangeFlags |= kIOPMExpireIdleTimer;
6786 }
6787 } else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6788 maxPowerState++;
6789 } else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6790 maxPowerState = actions->darkWakePowerState;
6791 }
6792 } else {
6793 // Deny all self-initiated changes when power is limited.
6794 // Wrangler tickle should never defeat the limiter.
6795 maxPowerState = service->getPowerState();
6796 }
6797
6798 if (powerState > maxPowerState) {
6799 DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6800 service->getName(), service->getRegistryEntryID(),
6801 powerState, maxPowerState, changeFlags);
6802 *inOutPowerState = maxPowerState;
6803
6804 if (darkWakePostTickle &&
6805 (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6806 (changeFlags & kIOPMDomainWillChange) &&
6807 ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6808 kDarkWakeFlagPromotionEarly)) {
6809 darkWakePostTickle = false;
6810 reportUserInput();
6811 }
6812 }
6813
6814 if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6815 if (darkWakeLogClamp) {
6816 AbsoluteTime now;
6817 uint64_t nsec;
6818
6819 clock_get_uptime(&now);
6820 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6821 absolutetime_to_nanoseconds(now, &nsec);
6822 DLOG("dark wake power clamped after %u ms\n",
6823 ((int)((nsec) / NSEC_PER_MSEC)));
6824 }
6825 darkWakePowerClamped = true;
6826 }
6827 }
6828 }
6829
6830 void
6831 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6832 IOService * service,
6833 IOPMActions * actions )
6834 {
6835 #if DISPLAY_WRANGLER_PRESENT
6836 // Warning: Not running in PM work loop context - don't modify state !!!
6837 // Trap tickle directed to IODisplayWrangler while running with graphics
6838 // capability suppressed.
6839
6840 assert(service == wrangler);
6841
6842 clock_get_uptime(&userActivityTime);
6843 bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6844 || (lastSleepReason == kIOPMSleepReasonMaintenance)
6845 || (lastSleepReason == kIOPMSleepReasonSoftware));
6846 if (aborting) {
6847 userActivityCount++;
6848 DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6849 userActivityCount, lastSleepReason);
6850 }
6851
6852 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6853 DLOG("display wrangler tickled\n");
6854 if (kIOLogPMRootDomain & gIOKitDebug) {
6855 OSReportWithBacktrace("Dark wake display tickle");
6856 }
6857 if (pmPowerStateQueue) {
6858 pmPowerStateQueue->submitPowerEvent(
6859 kPowerEventPolicyStimulus,
6860 (void *) kStimulusDarkWakeActivityTickle,
6861 true /* set wake type */ );
6862 }
6863 }
6864 #endif /* DISPLAY_WRANGLER_PRESENT */
6865 }
6866
6867 void
6868 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6869 IOService * service,
6870 IOPMActions * actions,
6871 const OSSymbol * powerClient,
6872 IOPMPowerStateIndex oldPowerState,
6873 IOPMPowerStateIndex newPowerState )
6874 {
6875 #if DISPLAY_WRANGLER_PRESENT
6876 assert(service == wrangler);
6877
6878 // This function implements half of the user active detection
6879 // by monitoring changes to the display wrangler's device desire.
6880 //
6881 // User becomes active when either:
6882 // 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6883 // in max power state. This desire change in absence of a power state
6884 // change is detected within. This handles the case when user becomes
6885 // active while the display is already lit by setDisplayPowerOn().
6886 //
6887 // 2. Power state change to max, and DeviceDesire is also at max.
6888 // Handled by displayWranglerNotification().
6889 //
6890 // User becomes inactive when DeviceDesire drops to sleep state or below.
6891
6892 DLOG("wrangler %s (ps %u, %u->%u)\n",
6893 powerClient->getCStringNoCopy(),
6894 (uint32_t) service->getPowerState(),
6895 (uint32_t) oldPowerState, (uint32_t) newPowerState);
6896
6897 if (powerClient == gIOPMPowerClientDevice) {
6898 if ((newPowerState > oldPowerState) &&
6899 (newPowerState == kWranglerPowerStateMax) &&
6900 (service->getPowerState() == kWranglerPowerStateMax)) {
6901 evaluatePolicy( kStimulusEnterUserActiveState );
6902 } else if ((newPowerState < oldPowerState) &&
6903 (newPowerState <= kWranglerPowerStateSleep)) {
6904 evaluatePolicy( kStimulusLeaveUserActiveState );
6905 }
6906 }
6907
6908 if (newPowerState <= kWranglerPowerStateSleep) {
6909 evaluatePolicy( kStimulusDisplayWranglerSleep );
6910 } else if (newPowerState == kWranglerPowerStateMax) {
6911 evaluatePolicy( kStimulusDisplayWranglerWake );
6912 }
6913 #endif /* DISPLAY_WRANGLER_PRESENT */
6914 }
6915
6916 //******************************************************************************
6917 // User active state management
6918 //******************************************************************************
6919
6920 void
6921 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6922 {
6923 #if DISPLAY_WRANGLER_PRESENT
6924 _preventUserActive = prevent;
6925 if (wrangler && !_preventUserActive) {
6926 // Allowing transition to user active, but the wrangler may have
6927 // already powered ON in case of sleep cancel/revert. Poll the
6928 // same conditions checked for in displayWranglerNotification()
6929 // to bring the user active state up to date.
6930
6931 if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6932 (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6933 kWranglerPowerStateMax)) {
6934 evaluatePolicy( kStimulusEnterUserActiveState );
6935 }
6936 }
6937 #endif /* DISPLAY_WRANGLER_PRESENT */
6938 }
6939
6940 //******************************************************************************
6941 // Approve usage of delayed child notification by PM.
6942 //******************************************************************************
6943
6944 bool
6945 IOPMrootDomain::shouldDelayChildNotification(
6946 IOService * service )
6947 {
6948 if ((kFullWakeReasonNone == fullWakeReason) &&
6949 (kSystemTransitionWake == _systemTransitionType)) {
6950 DLOG("%s: delay child notify\n", service->getName());
6951 return true;
6952 }
6953 return false;
6954 }
6955
6956 //******************************************************************************
6957 // PM actions for PCI device.
6958 //******************************************************************************
6959
6960 void
6961 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6962 IOService * service,
6963 IOPMActions * actions,
6964 const IOPMRequest * request,
6965 IOPMPowerStateIndex powerState,
6966 IOPMPowerChangeFlags * inOutChangeFlags )
6967 {
6968 pmTracer->tracePCIPowerChange(
6969 PMTraceWorker::kPowerChangeStart,
6970 service, *inOutChangeFlags,
6971 (actions->flags & kPMActionsPCIBitNumberMask));
6972 }
6973
6974 void
6975 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6976 IOService * service,
6977 IOPMActions * actions,
6978 const IOPMRequest * request,
6979 IOPMPowerStateIndex powerState,
6980 IOPMPowerChangeFlags changeFlags )
6981 {
6982 pmTracer->tracePCIPowerChange(
6983 PMTraceWorker::kPowerChangeCompleted,
6984 service, changeFlags,
6985 (actions->flags & kPMActionsPCIBitNumberMask));
6986 }
6987
6988 //******************************************************************************
6989 // registerInterest
6990 //
6991 // Override IOService::registerInterest() for root domain clients.
6992 //******************************************************************************
6993
6994 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6995 {
6996 friend class IOPMrootDomain;
6997 OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6998
6999 protected:
7000 uint32_t ackTimeoutCnt;
7001 uint32_t msgType; // Last type seen by the message filter
7002 uint32_t lastSleepWakeMsgType;
7003 uint32_t msgIndex;
7004 uint32_t maxMsgDelayMS;
7005 uint32_t maxAckDelayMS;
7006 uint64_t msgAbsTime;
7007 uint64_t uuid0;
7008 uint64_t uuid1;
7009 OSSharedPtr<const OSSymbol> identifier;
7010 OSSharedPtr<const OSSymbol> clientName;
7011 };
7012
7013 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
7014
7015 OSSharedPtr<IONotifier>
7016 IOPMrootDomain::registerInterest(
7017 const OSSymbol * typeOfInterest,
7018 IOServiceInterestHandler handler,
7019 void * target, void * ref )
7020 {
7021 IOPMServiceInterestNotifier* notifier;
7022 bool isSystemCapabilityClient;
7023 bool isKernelCapabilityClient;
7024 IOReturn rc = kIOReturnError;
7025
7026 isSystemCapabilityClient = typeOfInterest &&
7027 typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
7028
7029 isKernelCapabilityClient = typeOfInterest &&
7030 typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
7031
7032 if (isSystemCapabilityClient) {
7033 typeOfInterest = gIOAppPowerStateInterest;
7034 }
7035
7036 notifier = new IOPMServiceInterestNotifier;
7037 if (!notifier) {
7038 return NULL;
7039 }
7040
7041 if (notifier->init()) {
7042 rc = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
7043 }
7044 if (rc != kIOReturnSuccess) {
7045 OSSafeReleaseNULL(notifier);
7046 return NULL;
7047 }
7048
7049 notifier->ackTimeoutCnt = 0;
7050
7051 if (pmPowerStateQueue) {
7052 if (isSystemCapabilityClient) {
7053 notifier->retain();
7054 if (pmPowerStateQueue->submitPowerEvent(
7055 kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
7056 notifier->release();
7057 }
7058 }
7059
7060 if (isKernelCapabilityClient) {
7061 notifier->retain();
7062 if (pmPowerStateQueue->submitPowerEvent(
7063 kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
7064 notifier->release();
7065 }
7066 }
7067 }
7068
7069 OSSharedPtr<OSData> data;
7070 uint8_t *uuid = NULL;
7071 OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
7072 if (kext) {
7073 data = kext->copyUUID();
7074 }
7075 if (data && (data->getLength() == sizeof(uuid_t))) {
7076 uuid = (uint8_t *)(data->getBytesNoCopy());
7077
7078 notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
7079 ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
7080 ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
7081 notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
7082 ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
7083 ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
7084
7085 notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
7086 }
7087 return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
7088 }
7089
7090 //******************************************************************************
7091 // systemMessageFilter
7092 //
7093 //******************************************************************************
7094
7095 bool
7096 IOPMrootDomain::systemMessageFilter(
7097 void * object, void * arg1, void * arg2, void * arg3 )
7098 {
7099 const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
7100 bool isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
7101 bool isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
7102 bool isCapClient = false;
7103 bool allow = false;
7104 OSBoolean **waitForReply = (typeof(waitForReply))arg3;
7105 IOPMServiceInterestNotifier *notifier;
7106
7107 notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
7108
7109 do {
7110 // When powerd and kernel priority clients register capability interest,
7111 // the power tree is sync'ed to inform those clients about the current
7112 // system capability. Only allow capability change messages during sync.
7113 if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
7114 (!isCapMsg || !_joinedCapabilityClients ||
7115 !_joinedCapabilityClients->containsObject((OSObject *) object))) {
7116 break;
7117 }
7118
7119 // Capability change message for powerd and kernel clients
7120 if (isCapMsg) {
7121 // Kernel priority clients
7122 if ((context->notifyType == kNotifyPriority) ||
7123 (context->notifyType == kNotifyCapabilityChangePriority)) {
7124 isCapClient = true;
7125 }
7126
7127 // powerd will maintain two client registrations with root domain.
7128 // isCapPowerd will be TRUE for any message targeting the powerd
7129 // exclusive (capability change) interest registration.
7130 if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
7131 isCapClient = true;
7132 }
7133 }
7134
7135 if (isCapClient) {
7136 IOPMSystemCapabilityChangeParameters * capArgs =
7137 (IOPMSystemCapabilityChangeParameters *) arg2;
7138
7139 if (kSystemTransitionNewCapClient == _systemTransitionType) {
7140 capArgs->fromCapabilities = 0;
7141 capArgs->toCapabilities = _currentCapability;
7142 capArgs->changeFlags = 0;
7143 } else {
7144 capArgs->fromCapabilities = _currentCapability;
7145 capArgs->toCapabilities = _pendingCapability;
7146
7147 if (context->isPreChange) {
7148 capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
7149 } else {
7150 capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
7151 }
7152
7153 if (isCapPowerd && context->isPreChange) {
7154 toldPowerdCapWillChange = true;
7155 }
7156 }
7157
7158 // App level capability change messages must only go to powerd.
7159 // Wait for response post-change if capabilitiy is increasing.
7160 // Wait for response pre-change if capability is decreasing.
7161
7162 if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7163 ((capabilityLoss && context->isPreChange) ||
7164 (!capabilityLoss && !context->isPreChange))) {
7165 *waitForReply = kOSBooleanTrue;
7166 }
7167
7168 allow = true;
7169 break;
7170 }
7171
7172 // powerd will always receive CanSystemSleep, even for a demand sleep.
7173 // It will also have a final chance to veto sleep after all clients
7174 // have responded to SystemWillSleep
7175
7176 if ((kIOMessageCanSystemSleep == context->messageType) ||
7177 (kIOMessageSystemWillNotSleep == context->messageType)) {
7178 if (isCapPowerd) {
7179 allow = true;
7180 break;
7181 }
7182
7183 // Demand sleep, don't ask apps for permission
7184 if (context->changeFlags & kIOPMSkipAskPowerDown) {
7185 break;
7186 }
7187 }
7188
7189 if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7190 if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7191 (fullToDarkReason == kIOPMSleepReasonIdle)) {
7192 allow = true;
7193 }
7194 break;
7195 }
7196
7197 // Drop capability change messages for legacy clients.
7198 // Drop legacy system sleep messages for powerd capability interest.
7199 if (isCapMsg || isCapPowerd) {
7200 break;
7201 }
7202
7203 // Not a capability change message.
7204 // Perform message filtering based on _systemMessageClientMask.
7205
7206 if ((context->notifyType == kNotifyApps) &&
7207 (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7208 if (!notifier) {
7209 break;
7210 }
7211
7212 if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7213 (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7214 break; // drop any duplicate WillPowerOn for AOT devices
7215 }
7216
7217 allow = true;
7218
7219 if (waitForReply) {
7220 if (notifier->ackTimeoutCnt >= 3) {
7221 *waitForReply = kOSBooleanFalse;
7222 } else {
7223 *waitForReply = kOSBooleanTrue;
7224 }
7225 }
7226 } else if ((context->notifyType == kNotifyPriority) &&
7227 (_systemMessageClientMask & kSystemMessageClientKernel)) {
7228 allow = true;
7229 }
7230
7231 // Check sleep/wake message ordering
7232 if (allow) {
7233 if (context->messageType == kIOMessageSystemWillSleep ||
7234 context->messageType == kIOMessageSystemWillPowerOn ||
7235 context->messageType == kIOMessageSystemHasPoweredOn) {
7236 notifier->lastSleepWakeMsgType = context->messageType;
7237 }
7238 }
7239 } while (false);
7240
7241 if (allow && isCapMsg && _joinedCapabilityClients) {
7242 _joinedCapabilityClients->removeObject((OSObject *) object);
7243 if (_joinedCapabilityClients->getCount() == 0) {
7244 DMSG("destroyed capability client set %p\n",
7245 OBFUSCATE(_joinedCapabilityClients.get()));
7246 _joinedCapabilityClients.reset();
7247 }
7248 }
7249 if (notifier) {
7250 // Record the last seen message type even if the message is dropped
7251 // for traceFilteredNotification().
7252 notifier->msgType = context->messageType;
7253 }
7254
7255 return allow;
7256 }
7257
7258 //******************************************************************************
7259 // setMaintenanceWakeCalendar
7260 //
7261 //******************************************************************************
7262
7263 IOReturn
7264 IOPMrootDomain::setMaintenanceWakeCalendar(
7265 const IOPMCalendarStruct * calendar )
7266 {
7267 OSSharedPtr<OSData> data;
7268 IOReturn ret = 0;
7269
7270 if (!calendar) {
7271 return kIOReturnBadArgument;
7272 }
7273
7274 data = OSData::withValue(*calendar);
7275 if (!data) {
7276 return kIOReturnNoMemory;
7277 }
7278
7279 if (kPMCalendarTypeMaintenance == calendar->selector) {
7280 ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7281 } else if (kPMCalendarTypeSleepService == calendar->selector) {
7282 ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7283 }
7284
7285 return ret;
7286 }
7287
7288 // MARK: -
7289 // MARK: Display Wrangler
7290
7291 //******************************************************************************
7292 // displayWranglerNotification
7293 //
7294 // Handle the notification when the IODisplayWrangler changes power state.
7295 //******************************************************************************
7296
7297 IOReturn
7298 IOPMrootDomain::displayWranglerNotification(
7299 void * target, void * refCon,
7300 UInt32 messageType, IOService * service,
7301 void * messageArgument, vm_size_t argSize )
7302 {
7303 #if DISPLAY_WRANGLER_PRESENT
7304 IOPMPowerStateIndex displayPowerState;
7305 IOPowerStateChangeNotification * params =
7306 (IOPowerStateChangeNotification *) messageArgument;
7307
7308 if ((messageType != kIOMessageDeviceWillPowerOff) &&
7309 (messageType != kIOMessageDeviceHasPoweredOn)) {
7310 return kIOReturnUnsupported;
7311 }
7312
7313 ASSERT_GATED();
7314 if (!gRootDomain) {
7315 return kIOReturnUnsupported;
7316 }
7317
7318 displayPowerState = params->stateNumber;
7319 DLOG("wrangler %s ps %d\n",
7320 getIOMessageString(messageType), (uint32_t) displayPowerState);
7321
7322 switch (messageType) {
7323 case kIOMessageDeviceWillPowerOff:
7324 // Display wrangler has dropped power due to display idle
7325 // or force system sleep.
7326 //
7327 // 4 Display ON kWranglerPowerStateMax
7328 // 3 Display Dim kWranglerPowerStateDim
7329 // 2 Display Sleep kWranglerPowerStateSleep
7330 // 1 Not visible to user
7331 // 0 Not visible to user kWranglerPowerStateMin
7332
7333 if (displayPowerState <= kWranglerPowerStateSleep) {
7334 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7335 }
7336 break;
7337
7338 case kIOMessageDeviceHasPoweredOn:
7339 // Display wrangler has powered on due to user activity
7340 // or wake from sleep.
7341
7342 if (kWranglerPowerStateMax == displayPowerState) {
7343 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7344
7345 // See comment in handleUpdatePowerClientForDisplayWrangler
7346 if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7347 kWranglerPowerStateMax) {
7348 gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7349 }
7350 }
7351 break;
7352 }
7353 #endif /* DISPLAY_WRANGLER_PRESENT */
7354 return kIOReturnUnsupported;
7355 }
7356
7357 //******************************************************************************
7358 // reportUserInput
7359 //
7360 //******************************************************************************
7361
7362 void
7363 IOPMrootDomain::updateUserActivity( void )
7364 {
7365 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7366 clock_get_uptime(&userActivityTime);
7367 bool aborting = ((lastSleepReason == kIOPMSleepReasonSoftware)
7368 || (lastSleepReason == kIOPMSleepReasonIdle)
7369 || (lastSleepReason == kIOPMSleepReasonMaintenance));
7370 if (aborting) {
7371 userActivityCount++;
7372 DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7373 }
7374 #endif
7375 }
7376 void
7377 IOPMrootDomain::reportUserInput( void )
7378 {
7379 if (wrangler) {
7380 wrangler->activityTickle(0, 0);
7381 }
7382 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7383 // Update user activity
7384 updateUserActivity();
7385
7386 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7387 // update user active abs time
7388 clock_get_uptime(&gUserActiveAbsTime);
7389 pmPowerStateQueue->submitPowerEvent(
7390 kPowerEventPolicyStimulus,
7391 (void *) kStimulusDarkWakeActivityTickle,
7392 true /* set wake type */ );
7393 }
7394 #endif
7395 }
7396
7397 void
7398 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7399 {
7400 #if DISPLAY_WRANGLER_PRESENT
7401 if (wrangler) {
7402 wrangler->activityTickle(0, 0);
7403 }
7404 #else
7405 if (!device) {
7406 DLOG("requestUserActive: device is null\n");
7407 return;
7408 }
7409 OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7410 uint64_t registryID = device->getRegistryEntryID();
7411
7412 if (!deviceName || !registryID) {
7413 DLOG("requestUserActive: no device name or registry entry\n");
7414 return;
7415 }
7416 const char *name = deviceName->getCStringNoCopy();
7417 char payload[128];
7418 snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7419 DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7420 messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7421 #endif
7422 }
7423
7424 //******************************************************************************
7425 // latchDisplayWranglerTickle
7426 //******************************************************************************
7427
7428 bool
7429 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7430 {
7431 #if DISPLAY_WRANGLER_PRESENT
7432 if (latch) {
7433 if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7434 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7435 !checkSystemCanSustainFullWake()) {
7436 // Currently in dark wake, and not transitioning to full wake.
7437 // Full wake is unsustainable, so latch the tickle to prevent
7438 // the display from lighting up momentarily.
7439 wranglerTickled = true;
7440 } else {
7441 wranglerTickled = false;
7442 }
7443 } else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7444 wranglerTickled = false;
7445
7446 pmPowerStateQueue->submitPowerEvent(
7447 kPowerEventPolicyStimulus,
7448 (void *) kStimulusDarkWakeActivityTickle );
7449 }
7450
7451 return wranglerTickled;
7452 #else /* ! DISPLAY_WRANGLER_PRESENT */
7453 return false;
7454 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7455 }
7456
7457 //******************************************************************************
7458 // setDisplayPowerOn
7459 //
7460 // For root domain user client
7461 //******************************************************************************
7462
7463 void
7464 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7465 {
7466 pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7467 (void *) NULL, options );
7468 }
7469
7470 // MARK: -
7471 // MARK: System PM Policy
7472
7473 //******************************************************************************
7474 // checkSystemSleepAllowed
7475 //
7476 //******************************************************************************
7477
7478 bool
7479 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7480 uint32_t sleepReason )
7481 {
7482 uint32_t err = 0;
7483
7484 // Conditions that prevent idle and demand system sleep.
7485
7486 do {
7487 if (gSleepDisabledFlag) {
7488 err = kPMConfigPreventSystemSleep;
7489 break;
7490 }
7491
7492 if (userDisabledAllSleep) {
7493 err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7494 break;
7495 }
7496
7497 if (systemBooting || systemShutdown || gWillShutdown) {
7498 err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7499 break;
7500 }
7501
7502 if (options == 0) {
7503 break;
7504 }
7505
7506 // Conditions above pegs the system at full wake.
7507 // Conditions below prevent system sleep but does not prevent
7508 // dark wake, and must be called from gated context.
7509
7510 #if !CONFIG_SLEEP
7511 err = kPMConfigPreventSystemSleep; // 3. config does not support sleep
7512 break;
7513 #endif
7514
7515 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7516 break; // always sleep on low battery or when in thermal warning/emergency state
7517 }
7518
7519 if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7520 break; // always sleep on dark wake thermal emergencies
7521 }
7522
7523 if (preventSystemSleepList->getCount() != 0) {
7524 err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7525 break;
7526 }
7527
7528 if (_driverKitMatchingAssertionCount != 0) {
7529 err = kPMCPUAssertion;
7530 break;
7531 }
7532
7533 // Check for any dexts currently being added to the PM tree. Sleeping while
7534 // this is in flight can cause IOServicePH to timeout.
7535 if (!IOServicePH::checkPMReady()) {
7536 #if !defined(XNU_TARGET_OS_OSX)
7537 // 116893363: kPMDKNotReady sleep cancellations often leaves embedded devices
7538 // in dark wake for long periods of time, which causes issues as apps were
7539 // already informed of sleep during the f->9 transition. As a temporary
7540 // measure, always full wake if we hit this specific condition.
7541 pmPowerStateQueue->submitPowerEvent(
7542 kPowerEventPolicyStimulus,
7543 (void *) kStimulusDarkWakeActivityTickle);
7544 #endif
7545 err = kPMDKNotReady;
7546 break;
7547 }
7548
7549 if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7550 kIOPMDriverAssertionLevelOn) {
7551 err = kPMCPUAssertion; // 5. CPU assertion
7552 break;
7553 }
7554
7555 if (pciCantSleepValid) {
7556 if (pciCantSleepFlag) {
7557 err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7558 }
7559 break;
7560 } else if (sleepSupportedPEFunction &&
7561 CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7562 IOReturn ret;
7563 OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7564 ret = getPlatform()->callPlatformFunction(
7565 sleepSupportedPEFunction.get(), false,
7566 NULL, NULL, NULL, NULL);
7567 pciCantSleepValid = true;
7568 pciCantSleepFlag = false;
7569 if ((platformSleepSupport & kPCICantSleep) ||
7570 ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7571 err = 6; // 6. PCI card does not support PM
7572 pciCantSleepFlag = true;
7573 break;
7574 }
7575 }
7576 }while (false);
7577
7578 if (err) {
7579 DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7580 return false;
7581 }
7582 return true;
7583 }
7584
7585 bool
7586 IOPMrootDomain::checkSystemSleepEnabled( void )
7587 {
7588 return checkSystemSleepAllowed(0, 0);
7589 }
7590
7591 bool
7592 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7593 {
7594 ASSERT_GATED();
7595 return checkSystemSleepAllowed(1, sleepReason);
7596 }
7597
7598 //******************************************************************************
7599 // checkSystemCanSustainFullWake
7600 //******************************************************************************
7601
7602 bool
7603 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7604 {
7605 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7606 // Low battery wake, or received a low battery notification
7607 // while system is awake. This condition will persist until
7608 // the following wake.
7609 return false;
7610 }
7611
7612 if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7613 // Graphics state is unknown and external display might not be probed.
7614 // Do not incorporate state that requires graphics to be in max power
7615 // such as desktopMode or clamshellDisabled.
7616
7617 if (!acAdaptorConnected) {
7618 DLOG("full wake check: no AC\n");
7619 return false;
7620 }
7621 }
7622 return true;
7623 }
7624
7625 //******************************************************************************
7626 // checkSystemCanAbortIdleSleep
7627 //******************************************************************************
7628
7629 bool
7630 IOPMrootDomain::checkSystemCanAbortIdleSleep( void )
7631 {
7632 bool abortableSleepType = ((lastSleepReason == kIOPMSleepReasonIdle)
7633 || (lastSleepReason == 0));
7634 return idleSleepRevertible && abortableSleepType;
7635 }
7636
7637 //******************************************************************************
7638 // attemptIdleSleepAbort
7639 //******************************************************************************
7640
7641 bool
7642 IOPMrootDomain::attemptIdleSleepAbort( void )
7643 {
7644 if (!gIOPMWorkLoop->inGate()) {
7645 bool ret = gIOPMWorkLoop->runAction(
7646 OSMemberFunctionCast(IOWorkLoop::Action, this,
7647 &IOPMrootDomain::attemptIdleSleepAbort),
7648 this);
7649 return ret;
7650 }
7651
7652 bool canAbort = checkSystemCanAbortIdleSleep();
7653 if (canAbort) {
7654 cancelIdlePowerDownSync();
7655 } else if (lastSleepReason == kIOPMSleepReasonIdle) {
7656 scheduleImmediateDebugWake();
7657 }
7658
7659 return canAbort;
7660 }
7661
7662 //******************************************************************************
7663 // setIdleSleepRevertible
7664 //******************************************************************************
7665
7666 void
7667 IOPMrootDomain::setIdleSleepRevertible( bool revertible )
7668 {
7669 idleSleepRevertible = revertible;
7670 }
7671
7672 //******************************************************************************
7673 // mustHibernate
7674 //******************************************************************************
7675
7676 #if HIBERNATION
7677
7678 bool
7679 IOPMrootDomain::mustHibernate( void )
7680 {
7681 return lowBatteryCondition || thermalWarningState;
7682 }
7683
7684 #endif /* HIBERNATION */
7685
7686 //******************************************************************************
7687 // AOT
7688 //******************************************************************************
7689
7690 // Tables for accumulated days in year by month, latter used for leap years
7691
7692 static const unsigned int daysbymonth[] =
7693 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7694
7695 static const unsigned int lydaysbymonth[] =
7696 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7697
7698 static int __unused
7699 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7700 {
7701 const unsigned int * dbm = daysbymonth;
7702 clock_sec_t n, x, y, z;
7703
7704 // Calculate seconds, minutes and hours
7705
7706 n = secs % (24 * 3600);
7707 dt->second = n % 60;
7708 n /= 60;
7709 dt->minute = n % 60;
7710 dt->hour = (typeof(dt->hour))(n / 60);
7711
7712 // Calculate day of week
7713
7714 n = secs / (24 * 3600);
7715 // dt->dayWeek = (n + 4) % 7;
7716
7717 // Calculate year
7718 // Rebase from days since Unix epoch (1/1/1970) store in 'n',
7719 // to days since 1/1/1968 to start on 4 year cycle, beginning
7720 // on a leap year.
7721
7722 n += (366 + 365);
7723
7724 // Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7725 // Valid before 2100, since 2100 is not a leap year.
7726
7727 x = n / 1461; // number of 4 year cycles
7728 y = n % 1461; // days into current 4 year cycle
7729 z = 1968 + (4 * x);
7730
7731 // Add in years in the current 4 year cycle
7732
7733 if (y >= 366) {
7734 y -= 366; // days after the leap year
7735 n = y % 365; // days into the current year
7736 z += (1 + y / 365); // years after the past 4-yr cycle
7737 } else {
7738 n = y;
7739 dbm = lydaysbymonth;
7740 }
7741 if (z > 2099) {
7742 return 0;
7743 }
7744
7745 dt->year = (typeof(dt->year))z;
7746
7747 // Adjust remaining days value to start at 1
7748
7749 n += 1;
7750
7751 // Calculate month
7752
7753 for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7754 continue;
7755 }
7756 dt->month = (typeof(dt->month))x;
7757
7758 // Calculate day of month
7759
7760 dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7761
7762 return 1;
7763 }
7764
7765 static clock_sec_t
7766 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7767 {
7768 const unsigned int * dbm = daysbymonth;
7769 long y, secs, days;
7770
7771 if (dt->year < 1970 || dt->month > 12) {
7772 return 0;
7773 }
7774
7775 // Seconds elapsed in the current day
7776
7777 secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7778
7779 // Number of days from 1/1/70 to beginning of current year
7780 // Account for extra day every 4 years starting at 1973
7781
7782 y = dt->year - 1970;
7783 days = (y * 365) + ((y + 1) / 4);
7784
7785 // Change table if current year is a leap year
7786
7787 if ((dt->year % 4) == 0) {
7788 dbm = lydaysbymonth;
7789 }
7790
7791 // Add in days elapsed in the current year
7792
7793 days += (dt->day - 1) + dbm[dt->month - 1];
7794
7795 // Add accumulated days to accumulated seconds
7796
7797 secs += 24 * 3600 * days;
7798
7799 return secs;
7800 }
7801
7802 unsigned long
7803 IOPMrootDomain::getRUN_STATE(void)
7804 {
7805 return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7806 }
7807
7808 bool
7809 IOPMrootDomain::isAOTMode()
7810 {
7811 return _aotNow;
7812 }
7813
7814 IOReturn
7815 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7816 {
7817 clock_sec_t nowsecs, wakesecs;
7818 clock_usec_t nowmicrosecs, wakemicrosecs;
7819 uint64_t nowAbs, wakeAbs;
7820
7821 if (!_aotMode) {
7822 return kIOReturnNotReady;
7823 }
7824
7825 clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7826 wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7827 if (wakeAbs < nowAbs) {
7828 printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7829 wakeAbs = nowAbs;
7830 }
7831 wakeAbs -= nowAbs;
7832 absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7833
7834 wakesecs += nowsecs;
7835 wakemicrosecs += nowmicrosecs;
7836 if (wakemicrosecs >= USEC_PER_SEC) {
7837 wakesecs++;
7838 wakemicrosecs -= USEC_PER_SEC;
7839 }
7840 if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7841 wakesecs++;
7842 }
7843
7844 IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7845
7846 if (_aotWakeTimeContinuous != wakeContinuousTime) {
7847 _aotWakeTimeContinuous = wakeContinuousTime;
7848 IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7849 }
7850 _aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7851 _aotWakeTimeUTC = wakesecs;
7852
7853 return kIOReturnSuccess;
7854 }
7855
7856 // assumes WAKEEVENT_LOCK
7857 bool
7858 IOPMrootDomain::aotShouldExit(bool software)
7859 {
7860 bool exitNow = false;
7861 const char * reason = "";
7862
7863 if (!_aotNow) {
7864 return false;
7865 }
7866
7867 if (software) {
7868 exitNow = true;
7869 _aotMetrics->softwareRequestCount++;
7870 reason = "software request";
7871 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7872 exitNow = true;
7873 reason = gWakeReasonString;
7874 } else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7875 clock_sec_t sec;
7876 clock_usec_t usec;
7877 clock_get_calendar_microtime(&sec, &usec);
7878 if (_calendarWakeAlarmUTC <= sec) {
7879 exitNow = true;
7880 _aotMetrics->rtcAlarmsCount++;
7881 reason = "user alarm";
7882 }
7883 }
7884 if (exitNow) {
7885 _aotPendingFlags |= kIOPMWakeEventAOTExit;
7886 IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7887 reason,
7888 _aotMetrics->sleepCount,
7889 _aotMetrics->possibleCount,
7890 _aotMetrics->confirmedPossibleCount,
7891 _aotMetrics->rejectedPossibleCount,
7892 _aotMetrics->expiredPossibleCount,
7893 _aotMetrics->noTimeSetCount,
7894 _aotMetrics->rtcAlarmsCount);
7895 }
7896 return exitNow;
7897 }
7898
7899 void
7900 IOPMrootDomain::aotExit(bool cps)
7901 {
7902 uint32_t savedMessageMask;
7903
7904 ASSERT_GATED();
7905 _aotNow = false;
7906 _aotReadyToFullWake = false;
7907 if (_aotTimerScheduled) {
7908 _aotTimerES->cancelTimeout();
7909 _aotTimerScheduled = false;
7910 }
7911 updateTasksSuspend(kTasksSuspendNoChange, kTasksSuspendUnsuspended);
7912
7913 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7914 _aotLastWakeTime = 0;
7915 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7916 WAKEEVENT_LOCK();
7917 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7918 gWakeReasonString,
7919 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7920 WAKEEVENT_UNLOCK();
7921 }
7922
7923 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7924
7925 // Preserve the message mask since a system wake transition
7926 // may have already started and initialized the mask.
7927 savedMessageMask = _systemMessageClientMask;
7928 _systemMessageClientMask = kSystemMessageClientLegacyApp;
7929 tellClients(kIOMessageSystemWillPowerOn);
7930 _systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7931
7932 if (cps) {
7933 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7934 }
7935 }
7936
7937 void
7938 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7939 {
7940 bool exitNow;
7941
7942 IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7943
7944 WAKEEVENT_LOCK();
7945 exitNow = aotShouldExit(false);
7946 if (timer != NULL) {
7947 _aotTimerScheduled = false;
7948 }
7949 WAKEEVENT_UNLOCK();
7950 if (exitNow) {
7951 aotExit(true);
7952 } else {
7953 #if 0
7954 if (_aotLingerTime) {
7955 uint64_t deadline;
7956 IOLog("aot linger before sleep\n");
7957 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7958 clock_delay_until(deadline);
7959 }
7960 #endif
7961 privateSleepSystem(kIOPMSleepReasonSoftware);
7962 }
7963 }
7964
7965 //******************************************************************************
7966 // adjustPowerState
7967 //
7968 // Conditions that affect our wake/sleep decision has changed.
7969 // If conditions dictate that the system must remain awake, clamp power
7970 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7971 // is TRUE, then remove the power clamp and allow the power state to drop
7972 // to SLEEP_STATE.
7973 //******************************************************************************
7974
7975 void
7976 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7977 {
7978 DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7979 getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7980
7981 ASSERT_GATED();
7982
7983 if (_aotNow) {
7984 bool exitNow;
7985
7986 if (AOT_STATE != getPowerState()) {
7987 return;
7988 }
7989 WAKEEVENT_LOCK();
7990 exitNow = aotShouldExit(false);
7991 if (!exitNow
7992 && !_aotTimerScheduled
7993 && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7994 _aotTimerScheduled = true;
7995 if (_aotLingerTime) {
7996 _aotTimerES->setTimeout(_aotLingerTime);
7997 } else {
7998 _aotTimerES->setTimeout(800, kMillisecondScale);
7999 }
8000 }
8001 WAKEEVENT_UNLOCK();
8002 if (exitNow) {
8003 aotExit(true);
8004 } else {
8005 _aotReadyToFullWake = true;
8006 if (!_aotTimerScheduled) {
8007 if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
8008 // Don't try to force sleep during AOT while IOMobileFramebuffer is holding a power assertion.
8009 // Doing so will result in the sleep being cancelled anyway,
8010 // but this check avoids unnecessary thrashing in the power state engine.
8011 return;
8012 }
8013 privateSleepSystem(kIOPMSleepReasonSoftware);
8014 }
8015 }
8016 return;
8017 }
8018
8019 if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
8020 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
8021 } else if (sleepASAP) {
8022 changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
8023 }
8024 }
8025
8026 void
8027 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
8028 {
8029 if (powerOn) {
8030 if (!checkSystemCanSustainFullWake()) {
8031 DLOG("System cannot sustain full wake\n");
8032 return;
8033 }
8034
8035 // Force wrangler to max power state. If system is in dark wake
8036 // this alone won't raise the wrangler's power state.
8037 if (wrangler) {
8038 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
8039 }
8040
8041 // System in dark wake, always requesting full wake should
8042 // not have any bad side-effects, even if the request fails.
8043
8044 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8045 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
8046 requestFullWake( kFullWakeReasonDisplayOn );
8047 }
8048 } else {
8049 // Relenquish desire to power up display.
8050 // Must first transition to state 1 since wrangler doesn't
8051 // power off the displays at state 0. At state 0 the root
8052 // domain is removed from the wrangler's power client list.
8053 if (wrangler) {
8054 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
8055 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
8056 }
8057 }
8058 }
8059
8060 TUNABLE(bool, test_sleep_in_vm, "test_sleep_in_vm", false);
8061
8062 //******************************************************************************
8063 // dispatchPowerEvent
8064 //
8065 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
8066 //******************************************************************************
8067
8068 void
8069 IOPMrootDomain::dispatchPowerEvent(
8070 uint32_t event, void * arg0, uint64_t arg1 )
8071 {
8072 ASSERT_GATED();
8073
8074 switch (event) {
8075 case kPowerEventFeatureChanged:
8076 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8077 messageClients(kIOPMMessageFeatureChange, this);
8078 break;
8079
8080 case kPowerEventReceivedPowerNotification:
8081 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8082 handlePowerNotification((UInt32)(uintptr_t) arg0 );
8083 break;
8084
8085 case kPowerEventSystemBootCompleted:
8086 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8087 if (systemBooting) {
8088 systemBooting = false;
8089
8090 if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
8091 DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
8092 if (test_sleep_in_vm && gSleepDisabledFlag) {
8093 DLOG("Clearing gSleepDisabledFlag due to test_sleep_in_vm boot-arg\n");
8094 gSleepDisabledFlag = 0;
8095 }
8096 }
8097
8098 if (lowBatteryCondition || thermalEmergencyState) {
8099 if (lowBatteryCondition) {
8100 privateSleepSystem(kIOPMSleepReasonLowPower);
8101 } else {
8102 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8103 }
8104 // The rest is unnecessary since the system is expected
8105 // to sleep immediately. The following wake will update
8106 // everything.
8107 break;
8108 }
8109
8110 sleepWakeDebugMemAlloc();
8111 saveFailureData2File();
8112
8113 // If lid is closed, re-send lid closed notification
8114 // now that booting is complete.
8115 if (clamshellClosed) {
8116 handlePowerNotification(kLocalEvalClamshellCommand);
8117 }
8118 evaluatePolicy( kStimulusAllowSystemSleepChanged );
8119 }
8120 break;
8121
8122 case kPowerEventSystemShutdown:
8123 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8124 if (kOSBooleanTrue == (OSBoolean *) arg0) {
8125 /* We set systemShutdown = true during shutdown
8126 * to prevent sleep at unexpected times while loginwindow is trying
8127 * to shutdown apps and while the OS is trying to transition to
8128 * complete power of.
8129 *
8130 * Set to true during shutdown, as soon as loginwindow shows
8131 * the "shutdown countdown dialog", through individual app
8132 * termination, and through black screen kernel shutdown.
8133 */
8134 systemShutdown = true;
8135 } else {
8136 /*
8137 * A shutdown was initiated, but then the shutdown
8138 * was cancelled, clearing systemShutdown to false here.
8139 */
8140 systemShutdown = false;
8141 }
8142 break;
8143
8144 case kPowerEventUserDisabledSleep:
8145 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8146 userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
8147 break;
8148
8149 case kPowerEventRegisterSystemCapabilityClient:
8150 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8151
8152 // reset() handles the arg0 == nullptr case for us
8153 systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
8154 /* intentional fall-through */
8155 [[clang::fallthrough]];
8156
8157 case kPowerEventRegisterKernelCapabilityClient:
8158 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8159 if (!_joinedCapabilityClients) {
8160 _joinedCapabilityClients = OSSet::withCapacity(8);
8161 }
8162 if (arg0) {
8163 OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
8164 if (_joinedCapabilityClients) {
8165 _joinedCapabilityClients->setObject(notify.get());
8166 synchronizePowerTree( kIOPMSyncNoChildNotify );
8167 }
8168 }
8169 break;
8170
8171 case kPowerEventPolicyStimulus:
8172 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8173 if (arg0) {
8174 int stimulus = (int)(uintptr_t) arg0;
8175 evaluatePolicy(stimulus, (uint32_t) arg1);
8176 }
8177 break;
8178
8179 case kPowerEventAssertionCreate:
8180 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8181 if (pmAssertions) {
8182 pmAssertions->handleCreateAssertion((OSValueObject<PMAssertStruct> *)arg0);
8183 }
8184 break;
8185
8186
8187 case kPowerEventAssertionRelease:
8188 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8189 if (pmAssertions) {
8190 pmAssertions->handleReleaseAssertion(arg1);
8191 }
8192 break;
8193
8194 case kPowerEventAssertionSetLevel:
8195 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8196 if (pmAssertions) {
8197 pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
8198 }
8199 break;
8200
8201 case kPowerEventQueueSleepWakeUUID:
8202 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8203 handleQueueSleepWakeUUID((OSObject *)arg0);
8204 break;
8205 case kPowerEventPublishSleepWakeUUID:
8206 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8207 handlePublishSleepWakeUUID((bool)arg0);
8208 break;
8209
8210 case kPowerEventSetDisplayPowerOn:
8211 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8212 if (arg1 != 0) {
8213 displayPowerOnRequested = true;
8214 } else {
8215 displayPowerOnRequested = false;
8216 }
8217 handleSetDisplayPowerOn(displayPowerOnRequested);
8218 break;
8219
8220 case kPowerEventPublishWakeType:
8221 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8222
8223 // Don't replace wake type property if already set
8224 if ((arg0 == gIOPMWakeTypeUserKey) ||
8225 !propertyExists(kIOPMRootDomainWakeTypeKey)) {
8226 const char * wakeType = NULL;
8227
8228 if (arg0 == gIOPMWakeTypeUserKey) {
8229 requestUserActive(this, "WakeTypeUser");
8230 wakeType = kIOPMRootDomainWakeTypeUser;
8231 } else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
8232 if (!(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
8233 requestUserActive(this, "WakeTypeAlarm");
8234 }
8235 wakeType = kIOPMRootDomainWakeTypeAlarm;
8236 } else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
8237 darkWakeSleepService = true;
8238 wakeType = kIOPMRootDomainWakeTypeSleepService;
8239 } else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8240 wakeType = kIOPMRootDomainWakeTypeMaintenance;
8241 }
8242
8243 if (wakeType) {
8244 setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8245 }
8246 }
8247 break;
8248
8249 case kPowerEventAOTEvaluate:
8250 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8251 if (_aotReadyToFullWake) {
8252 aotEvaluate(NULL);
8253 }
8254 break;
8255 }
8256 }
8257
8258 //******************************************************************************
8259 // systemPowerEventOccurred
8260 //
8261 // The power controller is notifying us of a hardware-related power management
8262 // event that we must handle.
8263 //
8264 // systemPowerEventOccurred covers the same functionality that
8265 // receivePowerNotification does; it simply provides a richer API for conveying
8266 // more information.
8267 //******************************************************************************
8268
8269 IOReturn
8270 IOPMrootDomain::systemPowerEventOccurred(
8271 const OSSymbol *event,
8272 uint32_t intValue)
8273 {
8274 IOReturn attempt = kIOReturnSuccess;
8275 OSSharedPtr<OSNumber> newNumber;
8276
8277 if (!event) {
8278 return kIOReturnBadArgument;
8279 }
8280
8281 newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8282 if (!newNumber) {
8283 return kIOReturnInternalError;
8284 }
8285
8286 attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8287
8288 return attempt;
8289 }
8290
8291 void
8292 IOPMrootDomain::setThermalState(OSObject *value)
8293 {
8294 OSNumber * num;
8295
8296 if (gIOPMWorkLoop->inGate() == false) {
8297 gIOPMWorkLoop->runAction(
8298 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8299 (OSObject *)this,
8300 (void *)value);
8301
8302 return;
8303 }
8304 if (value && (num = OSDynamicCast(OSNumber, value))) {
8305 thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8306 (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8307 }
8308 }
8309
8310 IOReturn
8311 IOPMrootDomain::systemPowerEventOccurred(
8312 const OSSymbol *event,
8313 OSObject *value)
8314 {
8315 OSSharedPtr<OSDictionary> thermalsDict;
8316 bool shouldUpdate = true;
8317
8318 if (!event || !value) {
8319 return kIOReturnBadArgument;
8320 }
8321
8322 // LOCK
8323 // We reuse featuresDict Lock because it already exists and guards
8324 // the very infrequently used publish/remove feature mechanism; so there's zero rsk
8325 // of stepping on that lock.
8326 if (featuresDictLock) {
8327 IOLockLock(featuresDictLock);
8328 }
8329
8330 OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8331 OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8332
8333 if (origThermalsDict) {
8334 thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8335 } else {
8336 thermalsDict = OSDictionary::withCapacity(1);
8337 }
8338
8339 if (!thermalsDict) {
8340 shouldUpdate = false;
8341 goto exit;
8342 }
8343
8344 thermalsDict->setObject(event, value);
8345
8346 setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8347
8348 exit:
8349 // UNLOCK
8350 if (featuresDictLock) {
8351 IOLockUnlock(featuresDictLock);
8352 }
8353
8354 if (shouldUpdate) {
8355 if (event &&
8356 event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8357 setThermalState(value);
8358 }
8359 messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8360 }
8361
8362 return kIOReturnSuccess;
8363 }
8364
8365 //******************************************************************************
8366 // receivePowerNotification
8367 //
8368 // The power controller is notifying us of a hardware-related power management
8369 // event that we must handle. This may be a result of an 'environment' interrupt
8370 // from the power mgt micro.
8371 //******************************************************************************
8372
8373 IOReturn
8374 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8375 {
8376 if (msg & kIOPMPowerButton) {
8377 uint32_t currentPhase = pmTracer->getTracePhase();
8378 if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8379 DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8380 swd_flags |= SWD_PWR_BTN_STACKSHOT;
8381 thread_call_enter(powerButtonDown);
8382 } else {
8383 DEBUG_LOG("power button pressed when system is up\n");
8384 }
8385 } else if (msg & kIOPMPowerButtonUp) {
8386 if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8387 swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8388 thread_call_enter(powerButtonUp);
8389 }
8390 } else {
8391 pmPowerStateQueue->submitPowerEvent(
8392 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8393 }
8394 return kIOReturnSuccess;
8395 }
8396
8397 void
8398 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8399 {
8400 bool eval_clamshell = false;
8401 bool eval_clamshell_alarm = false;
8402
8403 ASSERT_GATED();
8404
8405 /*
8406 * Local (IOPMrootDomain only) eval clamshell command
8407 */
8408 if (msg & kLocalEvalClamshellCommand) {
8409 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8410 eval_clamshell_alarm = true;
8411
8412 // reset isRTCAlarmWake. This evaluation should happen only once
8413 // on RTC/Alarm wake. Any clamshell events after wake should follow
8414 // the regular evaluation
8415 isRTCAlarmWake = false;
8416 } else {
8417 eval_clamshell = true;
8418 }
8419 }
8420
8421 /*
8422 * Overtemp
8423 */
8424 if (msg & kIOPMOverTemp) {
8425 DLOG("Thermal overtemp message received!\n");
8426 thermalEmergencyState = true;
8427 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8428 }
8429
8430 /*
8431 * Forward DW thermal notification to client, if system is not going to sleep
8432 */
8433 if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8434 DLOG("DarkWake thermal limits message received!\n");
8435 messageClients(kIOPMMessageDarkWakeThermalEmergency);
8436 }
8437
8438 /*
8439 * Sleep Now!
8440 */
8441 if (msg & kIOPMSleepNow) {
8442 privateSleepSystem(kIOPMSleepReasonSoftware);
8443 }
8444
8445 /*
8446 * Power Emergency
8447 */
8448 if (msg & kIOPMPowerEmergency) {
8449 DLOG("Received kIOPMPowerEmergency");
8450 #if HIBERNATION && defined(__arm64__)
8451 if (!ml_is_secure_hib_supported()) {
8452 // Wait for the next low battery notification if the system state is
8453 // in transition.
8454 if ((_systemTransitionType == kSystemTransitionNone) &&
8455 CAP_CURRENT(kIOPMSystemCapabilityCPU) &&
8456 !systemBooting && !systemShutdown && !gWillShutdown) {
8457 // Setting lowBatteryCondition will prevent system sleep
8458 lowBatteryCondition = true;
8459
8460 // Notify userspace to initiate system shutdown
8461 messageClients(kIOPMMessageRequestSystemShutdown);
8462 }
8463 } else {
8464 lowBatteryCondition = true;
8465 privateSleepSystem(kIOPMSleepReasonLowPower);
8466 }
8467 #else /* HIBERNATION && defined(__arm64__) */
8468 lowBatteryCondition = true;
8469 privateSleepSystem(kIOPMSleepReasonLowPower);
8470 #endif /* HIBERNATION && defined(__arm64__) */
8471 }
8472
8473 /*
8474 * Clamshell OPEN
8475 */
8476 if (msg & kIOPMClamshellOpened) {
8477 DLOG("Clamshell opened\n");
8478 // Received clamshel open message from clamshell controlling driver
8479 // Update our internal state and tell general interest clients
8480 clamshellClosed = false;
8481 clamshellExists = true;
8482
8483 // Don't issue a hid tickle when lid is open and polled on wake
8484 if (msg & kIOPMSetValue) {
8485 setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8486 reportUserInput();
8487 }
8488
8489 // Tell PMCPU
8490 informCPUStateChange(kInformLid, 0);
8491
8492 // Tell general interest clients
8493 sendClientClamshellNotification();
8494
8495 bool aborting = ((lastSleepReason == kIOPMSleepReasonClamshell)
8496 || (lastSleepReason == kIOPMSleepReasonIdle)
8497 || (lastSleepReason == kIOPMSleepReasonMaintenance));
8498 if (aborting) {
8499 userActivityCount++;
8500 }
8501 DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8502 }
8503
8504 /*
8505 * Clamshell CLOSED
8506 * Send the clamshell interest notification since the lid is closing.
8507 */
8508 if (msg & kIOPMClamshellClosed) {
8509 if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8510 clamshellClosed && clamshellExists) {
8511 DLOG("Ignoring redundant Clamshell close event\n");
8512 } else {
8513 DLOG("Clamshell closed\n");
8514 // Received clamshel open message from clamshell controlling driver
8515 // Update our internal state and tell general interest clients
8516 clamshellClosed = true;
8517 clamshellExists = true;
8518
8519 // Ignore all following clamshell close events until the clamshell
8520 // is opened or the system sleeps. When a clamshell close triggers
8521 // a system wake, the lid driver may send us two clamshell close
8522 // events, one for the clamshell close event itself, and a second
8523 // close event when the driver polls the lid state on wake.
8524 clamshellIgnoreClose = true;
8525
8526 // Tell PMCPU
8527 informCPUStateChange(kInformLid, 1);
8528
8529 // Tell general interest clients
8530 sendClientClamshellNotification();
8531
8532 // And set eval_clamshell = so we can attempt
8533 eval_clamshell = true;
8534 }
8535 }
8536
8537 /*
8538 * Set Desktop mode (sent from graphics)
8539 *
8540 * -> reevaluate lid state
8541 */
8542 if (msg & kIOPMSetDesktopMode) {
8543 desktopMode = (0 != (msg & kIOPMSetValue));
8544 msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8545 DLOG("Desktop mode %d\n", desktopMode);
8546
8547 sendClientClamshellNotification();
8548
8549 // Re-evaluate the lid state
8550 eval_clamshell = true;
8551 }
8552
8553 /*
8554 * AC Adaptor connected
8555 *
8556 * -> reevaluate lid state
8557 */
8558 if (msg & kIOPMSetACAdaptorConnected) {
8559 acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8560 msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8561
8562 // Tell CPU PM
8563 informCPUStateChange(kInformAC, !acAdaptorConnected);
8564
8565 // Tell BSD if AC is connected
8566 // 0 == external power source; 1 == on battery
8567 post_sys_powersource(acAdaptorConnected ? 0:1);
8568
8569 sendClientClamshellNotification();
8570
8571 IOUserServer::powerSourceChanged(acAdaptorConnected);
8572
8573 // Re-evaluate the lid state
8574 eval_clamshell = true;
8575
8576 // Lack of AC may have latched a display wrangler tickle.
8577 // This mirrors the hardware's USB wake event latch, where a latched
8578 // USB wake event followed by an AC attach will trigger a full wake.
8579 latchDisplayWranglerTickle( false );
8580
8581 #if HIBERNATION
8582 // AC presence will reset the standy timer delay adjustment.
8583 _standbyTimerResetSeconds = 0;
8584 #endif
8585 if (!userIsActive) {
8586 // Reset userActivityTime when power supply is changed(rdr 13789330)
8587 clock_get_uptime(&userActivityTime);
8588 }
8589 }
8590
8591 /*
8592 * Enable Clamshell (external display disappear)
8593 *
8594 * -> reevaluate lid state
8595 */
8596 if (msg & kIOPMEnableClamshell) {
8597 DLOG("Clamshell enabled\n");
8598
8599 // Re-evaluate the lid state
8600 // System should sleep on external display disappearance
8601 // in lid closed operation.
8602 if (true == clamshellDisabled) {
8603 eval_clamshell = true;
8604
8605 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8606 // Also clear kClamshellSleepDisableInternal when graphics enables
8607 // the clamshell during a full wake. When graphics is behaving as
8608 // expected, this will allow clamshell close to be honored earlier
8609 // rather than waiting for the delayed evaluation.
8610 if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8611 (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8612 CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8613 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8614
8615 // Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8616 // when timer expires which is harmless but useless.
8617 thread_call_cancel(fullWakeThreadCall);
8618 }
8619 #endif
8620 }
8621
8622 clamshellDisabled = false;
8623 sendClientClamshellNotification();
8624 }
8625
8626 /*
8627 * Disable Clamshell (external display appeared)
8628 * We don't bother re-evaluating clamshell state. If the system is awake,
8629 * the lid is probably open.
8630 */
8631 if (msg & kIOPMDisableClamshell) {
8632 DLOG("Clamshell disabled\n");
8633 clamshellDisabled = true;
8634 sendClientClamshellNotification();
8635 }
8636
8637 /*
8638 * Evaluate clamshell and SLEEP if appropriate
8639 */
8640 if (eval_clamshell_alarm && clamshellClosed) {
8641 if (shouldSleepOnRTCAlarmWake()) {
8642 privateSleepSystem(kIOPMSleepReasonClamshell);
8643 }
8644 } else if (eval_clamshell && clamshellClosed) {
8645 if (shouldSleepOnClamshellClosed()) {
8646 privateSleepSystem(kIOPMSleepReasonClamshell);
8647 } else {
8648 evaluatePolicy( kStimulusDarkWakeEvaluate );
8649 }
8650 }
8651
8652 if (msg & kIOPMProModeEngaged) {
8653 int newState = 1;
8654 DLOG("ProModeEngaged\n");
8655 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8656 }
8657
8658 if (msg & kIOPMProModeDisengaged) {
8659 int newState = 0;
8660 DLOG("ProModeDisengaged\n");
8661 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8662 }
8663 }
8664
8665 //******************************************************************************
8666 // evaluatePolicy
8667 //
8668 // Evaluate root-domain policy in response to external changes.
8669 //******************************************************************************
8670
8671 void
8672 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8673 {
8674 union {
8675 struct {
8676 int idleSleepEnabled : 1;
8677 int idleSleepDisabled : 1;
8678 int displaySleep : 1;
8679 int sleepDelayChanged : 1;
8680 int evaluateDarkWake : 1;
8681 int adjustPowerState : 1;
8682 int userBecameInactive : 1;
8683 int displaySleepEntry : 1;
8684 } bit;
8685 uint32_t u32;
8686 } flags;
8687
8688
8689 ASSERT_GATED();
8690 flags.u32 = 0;
8691
8692 switch (stimulus) {
8693 case kStimulusDisplayWranglerSleep:
8694 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8695 if (!wranglerPowerOff) {
8696 // wrangler is in sleep state or lower
8697 flags.bit.displaySleep = true;
8698 }
8699 if (!wranglerAsleep) {
8700 // transition from wrangler wake to wrangler sleep
8701 flags.bit.displaySleepEntry = true;
8702 wranglerAsleep = true;
8703 }
8704 break;
8705
8706 case kStimulusDisplayWranglerWake:
8707 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8708 displayIdleForDemandSleep = false;
8709 wranglerPowerOff = false;
8710 wranglerAsleep = false;
8711 break;
8712
8713 case kStimulusEnterUserActiveState:
8714 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8715 if (_preventUserActive) {
8716 DLOG("user active dropped\n");
8717 break;
8718 }
8719 if (!userIsActive) {
8720 userIsActive = true;
8721 userWasActive = true;
8722 clock_get_uptime(&gUserActiveAbsTime);
8723
8724 // Stay awake after dropping demand for display power on
8725 if (kFullWakeReasonDisplayOn == fullWakeReason) {
8726 fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8727 DLOG("User activity while in notification wake\n");
8728 changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8729 }
8730
8731 kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8732 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8733 messageClients(kIOPMMessageUserIsActiveChanged);
8734 }
8735 flags.bit.idleSleepDisabled = true;
8736 break;
8737
8738 case kStimulusLeaveUserActiveState:
8739 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8740 if (userIsActive) {
8741 clock_get_uptime(&gUserInactiveAbsTime);
8742 userIsActive = false;
8743 clock_get_uptime(&userBecameInactiveTime);
8744 flags.bit.userBecameInactive = true;
8745
8746 kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8747 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8748 messageClients(kIOPMMessageUserIsActiveChanged);
8749 }
8750 break;
8751
8752 case kStimulusAggressivenessChanged:
8753 {
8754 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8755 unsigned long aggressiveValue;
8756 uint32_t minutesToIdleSleep = 0;
8757 uint32_t minutesToDisplayDim = 0;
8758 uint32_t minutesDelta = 0;
8759
8760 // Fetch latest display and system sleep slider values.
8761 aggressiveValue = 0;
8762 getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8763 minutesToIdleSleep = (uint32_t) aggressiveValue;
8764
8765 aggressiveValue = 0;
8766 getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8767 minutesToDisplayDim = (uint32_t) aggressiveValue;
8768 DLOG("aggressiveness changed: system %u->%u, display %u\n",
8769 sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8770
8771 DLOG("idle time -> %d ms (ena %d)\n",
8772 idleMilliSeconds, (minutesToIdleSleep != 0));
8773
8774 // How long to wait before sleeping the system once
8775 // the displays turns off is indicated by 'extraSleepDelay'.
8776
8777 if (minutesToIdleSleep > minutesToDisplayDim) {
8778 minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8779 } else if (minutesToIdleSleep == minutesToDisplayDim) {
8780 minutesDelta = 1;
8781 }
8782
8783 if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8784 idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8785 }
8786
8787 if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8788 flags.bit.idleSleepDisabled = true;
8789 idleSleepEnabled = false;
8790 }
8791 #if !defined(XNU_TARGET_OS_OSX)
8792 if (0x7fffffff == minutesToIdleSleep) {
8793 minutesToIdleSleep = idleMilliSeconds / 1000;
8794 }
8795 #endif /* !defined(XNU_TARGET_OS_OSX) */
8796
8797 if (((minutesDelta != extraSleepDelay) ||
8798 (userActivityTime != userActivityTime_prev)) &&
8799 !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8800 flags.bit.sleepDelayChanged = true;
8801 }
8802
8803 if (systemDarkWake && !darkWakeToSleepASAP &&
8804 (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8805 // Reconsider decision to remain in dark wake
8806 flags.bit.evaluateDarkWake = true;
8807 }
8808
8809 sleepSlider = minutesToIdleSleep;
8810 extraSleepDelay = minutesDelta;
8811 userActivityTime_prev = userActivityTime;
8812 } break;
8813
8814 case kStimulusDemandSystemSleep:
8815 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8816 displayIdleForDemandSleep = true;
8817 if (wrangler && wranglerIdleSettings) {
8818 // Request wrangler idle only when demand sleep is triggered
8819 // from full wake.
8820 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8821 wrangler->setProperties(wranglerIdleSettings.get());
8822 DLOG("Requested wrangler idle\n");
8823 }
8824 }
8825 // arg = sleepReason
8826 changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8827 break;
8828
8829 case kStimulusAllowSystemSleepChanged:
8830 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8831 flags.bit.adjustPowerState = true;
8832 break;
8833
8834 case kStimulusDarkWakeActivityTickle:
8835 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8836 // arg == true implies real and not self generated wrangler tickle.
8837 // Update wake type on PM work loop instead of the tickle thread to
8838 // eliminate the possibility of an early tickle clobbering the wake
8839 // type set by the platform driver.
8840 if (arg == true) {
8841 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8842 }
8843
8844 if (!darkWakeExit) {
8845 if (latchDisplayWranglerTickle(true)) {
8846 DLOG("latched tickle\n");
8847 break;
8848 }
8849
8850 darkWakeExit = true;
8851 DLOG("Requesting full wake due to dark wake activity tickle\n");
8852 requestFullWake( kFullWakeReasonLocalUser );
8853 }
8854 break;
8855
8856 case kStimulusDarkWakeEntry:
8857 case kStimulusDarkWakeReentry:
8858 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8859 // Any system transitions since the last dark wake transition
8860 // will invalid the stimulus.
8861
8862 if (arg == _systemStateGeneration) {
8863 DLOG("dark wake entry\n");
8864 systemDarkWake = true;
8865
8866 // Keep wranglerPowerOff an invariant when wrangler is absent
8867 if (wrangler) {
8868 wranglerPowerOff = true;
8869 }
8870
8871 if (kStimulusDarkWakeEntry == stimulus) {
8872 clock_get_uptime(&userBecameInactiveTime);
8873 flags.bit.evaluateDarkWake = true;
8874 if (activitySinceSleep()) {
8875 DLOG("User activity recorded while going to darkwake\n");
8876 reportUserInput();
8877 }
8878 }
8879
8880 // Always accelerate disk spindown while in dark wake,
8881 // even if system does not support/allow sleep.
8882
8883 cancelIdleSleepTimer();
8884 setQuickSpinDownTimeout();
8885 }
8886 break;
8887
8888 case kStimulusDarkWakeEvaluate:
8889 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8890 if (systemDarkWake) {
8891 flags.bit.evaluateDarkWake = true;
8892 }
8893 break;
8894
8895 case kStimulusNoIdleSleepPreventers:
8896 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8897 flags.bit.adjustPowerState = true;
8898 break;
8899 } /* switch(stimulus) */
8900
8901 if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8902 DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8903 darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8904 if (darkWakeToSleepASAP ||
8905 (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8906 uint32_t newSleepReason;
8907
8908 if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8909 // System was previously in full wake. Sleep reason from
8910 // full to dark already recorded in fullToDarkReason.
8911
8912 if (lowBatteryCondition) {
8913 newSleepReason = kIOPMSleepReasonLowPower;
8914 } else if (thermalEmergencyState) {
8915 newSleepReason = kIOPMSleepReasonThermalEmergency;
8916 } else {
8917 newSleepReason = fullToDarkReason;
8918 }
8919 } else {
8920 // In dark wake from system sleep.
8921
8922 if (darkWakeSleepService) {
8923 newSleepReason = kIOPMSleepReasonSleepServiceExit;
8924 } else {
8925 newSleepReason = kIOPMSleepReasonMaintenance;
8926 }
8927 }
8928
8929 if (checkSystemCanSleep(newSleepReason)) {
8930 privateSleepSystem(newSleepReason);
8931 }
8932 } else { // non-maintenance (network) dark wake
8933 if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8934 // Release power clamp, and wait for children idle.
8935 adjustPowerState(true);
8936 } else {
8937 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8938 }
8939 }
8940 }
8941
8942 if (systemDarkWake) {
8943 // The rest are irrelevant while system is in dark wake.
8944 flags.u32 = 0;
8945 }
8946
8947 if ((flags.bit.displaySleepEntry) &&
8948 (kFullWakeReasonDisplayOn == fullWakeReason)) {
8949 // kIOPMSleepReasonNotificationWakeExit
8950 DLOG("Display sleep while in notification wake\n");
8951 changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8952 }
8953
8954 if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8955 bool cancelQuickSpindown = false;
8956
8957 if (flags.bit.sleepDelayChanged) {
8958 // Cancel existing idle sleep timer and quick disk spindown.
8959 // New settings will be applied by the idleSleepEnabled flag
8960 // handler below if idle sleep is enabled.
8961
8962 DLOG("extra sleep timer changed\n");
8963 cancelIdleSleepTimer();
8964 cancelQuickSpindown = true;
8965 } else {
8966 DLOG("user inactive\n");
8967 }
8968
8969 if (!userIsActive && idleSleepEnabled) {
8970 startIdleSleepTimer(getTimeToIdleSleep());
8971 }
8972
8973 if (cancelQuickSpindown) {
8974 restoreUserSpinDownTimeout();
8975 }
8976 }
8977
8978 if (flags.bit.idleSleepEnabled) {
8979 DLOG("idle sleep timer enabled\n");
8980 if (!wrangler) {
8981 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8982 startIdleSleepTimer(getTimeToIdleSleep());
8983 #else
8984 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8985 startIdleSleepTimer( idleMilliSeconds );
8986 #endif
8987 } else {
8988 // Start idle timer if prefs now allow system sleep
8989 // and user is already inactive. Disk spindown is
8990 // accelerated upon timer expiration.
8991
8992 if (!userIsActive) {
8993 startIdleSleepTimer(getTimeToIdleSleep());
8994 }
8995 }
8996 }
8997
8998 if (flags.bit.idleSleepDisabled) {
8999 DLOG("idle sleep timer disabled\n");
9000 cancelIdleSleepTimer();
9001 restoreUserSpinDownTimeout();
9002 adjustPowerState();
9003 }
9004
9005 if (flags.bit.adjustPowerState) {
9006 bool sleepASAP = false;
9007
9008 if (!systemBooting && (0 == idleSleepPreventersCount())) {
9009 if (!wrangler) {
9010 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
9011 if (idleSleepEnabled) {
9012 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
9013 if (!extraSleepDelay && !idleSleepTimerPending && !gNoIdleFlag) {
9014 sleepASAP = true;
9015 }
9016 #else
9017 // stay awake for at least idleMilliSeconds
9018 startIdleSleepTimer(idleMilliSeconds);
9019 #endif
9020 }
9021 } else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake && !gNoIdleFlag) {
9022 sleepASAP = true;
9023 }
9024 }
9025
9026 adjustPowerState(sleepASAP);
9027 }
9028 }
9029
9030 //******************************************************************************
9031
9032 unsigned int
9033 IOPMrootDomain::idleSleepPreventersCount()
9034 {
9035 if (_aotMode) {
9036 unsigned int count __block;
9037 count = 0;
9038 preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
9039 {
9040 count += (NULL == obj->metaCast("AppleARMBacklight"));
9041 return false;
9042 });
9043 return count;
9044 }
9045
9046 return preventIdleSleepList->getCount();
9047 }
9048
9049
9050 //******************************************************************************
9051 // requestFullWake
9052 //
9053 // Request transition from dark wake to full wake
9054 //******************************************************************************
9055
9056 void
9057 IOPMrootDomain::requestFullWake( FullWakeReason reason )
9058 {
9059 uint32_t options = 0;
9060 IOService * pciRoot = NULL;
9061 bool promotion = false;
9062
9063 // System must be in dark wake and a valid reason for entering full wake
9064 if ((kFullWakeReasonNone == reason) ||
9065 (kFullWakeReasonNone != fullWakeReason) ||
9066 (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
9067 return;
9068 }
9069
9070 // Will clear reason upon exit from full wake
9071 fullWakeReason = reason;
9072
9073 _desiredCapability |= (kIOPMSystemCapabilityGraphics |
9074 kIOPMSystemCapabilityAudio);
9075
9076 if ((kSystemTransitionWake == _systemTransitionType) &&
9077 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
9078 !darkWakePowerClamped) {
9079 // Promote to full wake while waking up to dark wake due to tickle.
9080 // PM will hold off notifying the graphics subsystem about system wake
9081 // as late as possible, so if a HID tickle does arrive, graphics can
9082 // power up from this same wake transition. Otherwise, the latency to
9083 // power up graphics on the following transition can be huge on certain
9084 // systems. However, once any power clamping has taken effect, it is
9085 // too late to promote the current dark wake transition to a full wake.
9086 _pendingCapability |= (kIOPMSystemCapabilityGraphics |
9087 kIOPMSystemCapabilityAudio);
9088
9089 // Tell the PCI parent of audio and graphics drivers to stop
9090 // delaying the child notifications. Same for root domain.
9091 pciRoot = pciHostBridgeDriver.get();
9092 willEnterFullWake();
9093 promotion = true;
9094 }
9095
9096 // Unsafe to cancel once graphics was powered.
9097 // If system woke from dark wake, the return to sleep can
9098 // be cancelled. "awake -> dark -> sleep" transition
9099 // can be cancelled also, during the "dark -> sleep" phase
9100 // *prior* to driver power down.
9101 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
9102 _pendingCapability == 0) {
9103 options |= kIOPMSyncCancelPowerDown;
9104 }
9105
9106 synchronizePowerTree(options, pciRoot);
9107
9108 if (kFullWakeReasonLocalUser == fullWakeReason) {
9109 // IOGraphics doesn't light the display even though graphics is
9110 // enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
9111 // So, do an explicit activity tickle
9112 if (wrangler) {
9113 wrangler->activityTickle(0, 0);
9114 }
9115 }
9116
9117 // Log a timestamp for the initial full wake request.
9118 // System may not always honor this full wake request.
9119 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
9120 AbsoluteTime now;
9121 uint64_t nsec;
9122
9123 clock_get_uptime(&now);
9124 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9125 absolutetime_to_nanoseconds(now, &nsec);
9126 MSG("full wake %s (reason %u) %u ms\n",
9127 promotion ? "promotion" : "request",
9128 fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
9129 }
9130 }
9131
9132 //******************************************************************************
9133 // willEnterFullWake
9134 //
9135 // System will enter full wake from sleep, from dark wake, or from dark
9136 // wake promotion. This function aggregate things that are in common to
9137 // all three full wake transitions.
9138 //
9139 // Assumptions: fullWakeReason was updated
9140 //******************************************************************************
9141
9142 void
9143 IOPMrootDomain::willEnterFullWake( void )
9144 {
9145 hibernateRetry = false;
9146 sleepToStandby = false;
9147 standbyNixed = false;
9148 resetTimers = false;
9149 sleepTimerMaintenance = false;
9150
9151 assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
9152
9153 _systemMessageClientMask = kSystemMessageClientPowerd |
9154 kSystemMessageClientLegacyApp;
9155
9156 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
9157 // First time to attain full wake capability since the last wake
9158 _systemMessageClientMask |= kSystemMessageClientKernel;
9159
9160 // Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
9161 setProperty(gIOPMUserTriggeredFullWakeKey.get(),
9162 (kFullWakeReasonLocalUser == fullWakeReason) ?
9163 kOSBooleanTrue : kOSBooleanFalse);
9164 }
9165 #if HIBERNATION
9166 IOHibernateSetWakeCapabilities(_pendingCapability);
9167 #endif
9168
9169 IOService::setAdvisoryTickleEnable( true );
9170 tellClients(kIOMessageSystemWillPowerOn);
9171 preventTransitionToUserActive(false);
9172 }
9173
9174 //******************************************************************************
9175 // fullWakeDelayedWork
9176 //
9177 // System has already entered full wake. Invoked by a delayed thread call.
9178 //******************************************************************************
9179
9180 void
9181 IOPMrootDomain::fullWakeDelayedWork( void )
9182 {
9183 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
9184 if (!gIOPMWorkLoop->inGate()) {
9185 gIOPMWorkLoop->runAction(
9186 OSMemberFunctionCast(IOWorkLoop::Action, this,
9187 &IOPMrootDomain::fullWakeDelayedWork), this);
9188 return;
9189 }
9190
9191 DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
9192 _currentCapability, _pendingCapability, _highestCapability,
9193 clamshellDisabled, clamshellSleepDisableMask);
9194
9195 if (clamshellExists &&
9196 CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
9197 !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
9198 if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
9199 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
9200 } else {
9201 // Not the initial full wake after waking from sleep.
9202 // Evaluate the clamshell for rdar://problem/9157444.
9203 receivePowerNotification(kLocalEvalClamshellCommand);
9204 }
9205 }
9206 #endif
9207 }
9208
9209 //******************************************************************************
9210 // evaluateAssertions
9211 //
9212 //******************************************************************************
9213
9214 // Bitmask of all kernel assertions that prevent system idle sleep.
9215 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
9216 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
9217 (kIOPMDriverAssertionReservedBit7 | \
9218 kIOPMDriverAssertionPreventSystemIdleSleepBit)
9219
9220 void
9221 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
9222 {
9223 IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
9224
9225 messageClients(kIOPMMessageDriverAssertionsChanged);
9226
9227 if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
9228 if (wrangler) {
9229 bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
9230
9231 DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
9232 wrangler->setIgnoreIdleTimer( value );
9233 }
9234 }
9235
9236 if (changedBits & kIOPMDriverAssertionCPUBit) {
9237 if (_aotNow) {
9238 IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
9239 }
9240 evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
9241 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
9242 AbsoluteTime now;
9243 clock_usec_t microsecs;
9244 clock_get_uptime(&now);
9245 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9246 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
9247 if (assertOnWakeReport) {
9248 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
9249 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
9250 }
9251 }
9252 }
9253
9254 if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
9255 if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
9256 if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9257 DLOG("PreventIdleSleep driver assertion raised\n");
9258 bool ok = updatePreventIdleSleepList(this, true);
9259 if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9260 // Cancel idle sleep if there is one in progress
9261 cancelIdlePowerDown(this);
9262 }
9263 }
9264 } else {
9265 DLOG("PreventIdleSleep driver assertion dropped\n");
9266 updatePreventIdleSleepList(this, false);
9267 }
9268 }
9269 }
9270
9271 // MARK: -
9272 // MARK: Statistics
9273
9274 //******************************************************************************
9275 // pmStats
9276 //
9277 //******************************************************************************
9278
9279 void
9280 IOPMrootDomain::pmStatsRecordEvent(
9281 int eventIndex,
9282 AbsoluteTime timestamp)
9283 {
9284 bool starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9285 bool stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9286 uint64_t delta;
9287 uint64_t nsec;
9288 OSSharedPtr<OSData> publishPMStats;
9289
9290 eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9291
9292 absolutetime_to_nanoseconds(timestamp, &nsec);
9293
9294 switch (eventIndex) {
9295 case kIOPMStatsHibernateImageWrite:
9296 if (starting) {
9297 gPMStats.hibWrite.start = nsec;
9298 } else if (stopping) {
9299 gPMStats.hibWrite.stop = nsec;
9300 }
9301
9302 if (stopping) {
9303 delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9304 IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9305 }
9306 break;
9307 case kIOPMStatsHibernateImageRead:
9308 if (starting) {
9309 gPMStats.hibRead.start = nsec;
9310 } else if (stopping) {
9311 gPMStats.hibRead.stop = nsec;
9312 }
9313
9314 if (stopping) {
9315 delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9316 IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9317
9318 publishPMStats = OSData::withValue(gPMStats);
9319 setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9320 bzero(&gPMStats, sizeof(gPMStats));
9321 }
9322 break;
9323 }
9324 }
9325
9326 /*
9327 * Appends a record of the application response to
9328 * IOPMrootDomain::pmStatsAppResponses
9329 */
9330 void
9331 IOPMrootDomain::pmStatsRecordApplicationResponse(
9332 const OSSymbol *response,
9333 const char *name,
9334 int messageType,
9335 uint32_t delay_ms,
9336 uint64_t id,
9337 OSObject *object,
9338 IOPMPowerStateIndex powerState,
9339 bool async)
9340 {
9341 OSSharedPtr<OSDictionary> responseDescription;
9342 OSSharedPtr<OSNumber> delayNum;
9343 OSSharedPtr<OSNumber> powerCaps;
9344 OSSharedPtr<OSNumber> pidNum;
9345 OSSharedPtr<OSNumber> msgNum;
9346 OSSharedPtr<const OSSymbol> appname;
9347 OSSharedPtr<const OSSymbol> sleep;
9348 OSSharedPtr<const OSSymbol> wake;
9349 IOPMServiceInterestNotifier *notify = NULL;
9350
9351 if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9352 if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9353 notify->ackTimeoutCnt++;
9354 } else {
9355 notify->ackTimeoutCnt = 0;
9356 }
9357 }
9358
9359 if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9360 (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9361 return;
9362 }
9363
9364
9365 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9366 kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9367 } else if (notify) {
9368 // User space app or kernel capability client
9369 if (id) {
9370 kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9371 } else {
9372 kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9373 }
9374 notify->msgType = 0;
9375 }
9376
9377 responseDescription = OSDictionary::withCapacity(5);
9378 if (responseDescription) {
9379 if (response) {
9380 responseDescription->setObject(_statsResponseTypeKey.get(), response);
9381 }
9382
9383 msgNum = OSNumber::withNumber(messageType, 32);
9384 if (msgNum) {
9385 responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9386 }
9387
9388 if (!name && notify && notify->identifier) {
9389 name = notify->identifier->getCStringNoCopy();
9390 }
9391
9392 if (name && (strlen(name) > 0)) {
9393 appname = OSSymbol::withCString(name);
9394 if (appname) {
9395 responseDescription->setObject(_statsNameKey.get(), appname.get());
9396 }
9397 }
9398
9399 if (!id && notify) {
9400 id = notify->uuid0;
9401 }
9402 pidNum = OSNumber::withNumber(id, 64);
9403 if (pidNum) {
9404 responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9405 }
9406
9407 delayNum = OSNumber::withNumber(delay_ms, 32);
9408 if (delayNum) {
9409 responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9410 }
9411
9412 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9413 powerCaps = OSNumber::withNumber(powerState, 32);
9414
9415 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9416 static const char * driverCallTypes[] = {
9417 [kDriverCallInformPreChange] = "powerStateWillChangeTo",
9418 [kDriverCallInformPostChange] = "powerStateDidChangeTo",
9419 [kDriverCallSetPowerState] = "setPowerState"
9420 };
9421
9422 if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9423 DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9424 name, id, driverCallTypes[messageType], (uint32_t) powerState,
9425 async ? "async " : "", delay_ms);
9426 }
9427 #endif
9428 } else {
9429 powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9430 }
9431 if (powerCaps) {
9432 responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9433 }
9434
9435 sleep = OSSymbol::withCString("Sleep");
9436 wake = OSSymbol::withCString("Wake");
9437 if (_systemTransitionType == kSystemTransitionSleep) {
9438 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9439 } else if (_systemTransitionType == kSystemTransitionWake) {
9440 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9441 } else if (_systemTransitionType == kSystemTransitionCapability) {
9442 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9443 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9444 } else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9445 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9446 }
9447 }
9448
9449 IOLockLock(pmStatsLock);
9450 if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9451 pmStatsAppResponses->setObject(responseDescription.get());
9452 }
9453 IOLockUnlock(pmStatsLock);
9454 }
9455
9456 return;
9457 }
9458
9459 // MARK: -
9460 // MARK: PMTraceWorker
9461
9462 //******************************************************************************
9463 // TracePoint support
9464 //
9465 //******************************************************************************
9466
9467 #define kIOPMRegisterNVRAMTracePointHandlerKey \
9468 "IOPMRegisterNVRAMTracePointHandler"
9469
9470 IOReturn
9471 IOPMrootDomain::callPlatformFunction(
9472 const OSSymbol * functionName,
9473 bool waitForFunction,
9474 void * param1, void * param2,
9475 void * param3, void * param4 )
9476 {
9477 if (pmTracer && functionName &&
9478 functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9479 !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9480 uint32_t tracePointPhases, tracePointPCI;
9481 uint64_t statusCode;
9482
9483 pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9484 pmTracer->tracePointTarget = (void *) param2;
9485 tracePointPCI = (uint32_t)(uintptr_t) param3;
9486 tracePointPhases = (uint32_t)(uintptr_t) param4;
9487 if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9488 OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9489 if (node) {
9490 OSSharedPtr<OSObject> bootRomFailureProp;
9491 bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9492 OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9493 uint32_t bootFailureCode;
9494 if (data && data->getLength() == sizeof(bootFailureCode)) {
9495 // Failure code from EFI/BootRom is a four byte structure
9496 memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9497 tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9498 }
9499 }
9500 }
9501 statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9502 if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9503 MSG("Sleep failure code 0x%08x 0x%08x\n",
9504 tracePointPCI, tracePointPhases);
9505 }
9506 setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9507 pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9508
9509 return kIOReturnSuccess;
9510 }
9511 #if HIBERNATION
9512 else if (functionName &&
9513 functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9514 if (gSleepPolicyHandler) {
9515 return kIOReturnExclusiveAccess;
9516 }
9517 if (!param1) {
9518 return kIOReturnBadArgument;
9519 }
9520 gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9521 gSleepPolicyTarget = (void *) param2;
9522 setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9523 return kIOReturnSuccess;
9524 }
9525 #endif
9526
9527 return super::callPlatformFunction(
9528 functionName, waitForFunction, param1, param2, param3, param4);
9529 }
9530
9531 void
9532 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9533 uintptr_t param1, uintptr_t param2, uintptr_t param3)
9534 {
9535 uint32_t code = IODBG_POWER(event);
9536 uint64_t regId = id;
9537 if (regId == 0) {
9538 regId = getRegistryEntryID();
9539 }
9540 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9541 }
9542
9543 void
9544 IOPMrootDomain::tracePoint( uint8_t point )
9545 {
9546 if (systemBooting) {
9547 return;
9548 }
9549
9550 if (kIOPMTracePointWakeCapabilityClients == point) {
9551 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9552 }
9553
9554 kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9555 pmTracer->tracePoint(point);
9556 }
9557
9558 static void
9559 kext_log_putc(char c)
9560 {
9561 if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9562 return;
9563 }
9564 if (c == '(' || c == '[' || c == ' ') {
9565 c = 0;
9566 gKextNameEnd = true;
9567 }
9568
9569 gKextNameBuf[gKextNamePos++] = c;
9570 }
9571
9572 static int
9573 kext_log(const char *fmt, ...)
9574 {
9575 va_list listp;
9576
9577 va_start(listp, fmt);
9578 _doprnt(fmt, &listp, &kext_log_putc, 16);
9579 va_end(listp);
9580
9581 return 0;
9582 }
9583
9584 static OSPtr<const OSSymbol>
9585 copyKextIdentifierWithAddress(vm_address_t address)
9586 {
9587 OSSharedPtr<const OSSymbol> identifer;
9588
9589 IOLockLock(gHaltLogLock);
9590
9591 gKextNameEnd = false;
9592 gKextNamePos = 0;
9593 gKextNameBuf[0] = 0;
9594
9595 OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9596 gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9597 identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9598
9599 IOLockUnlock(gHaltLogLock);
9600
9601 return identifer;
9602 }
9603
9604 // Caller serialized using PM workloop
9605 const char *
9606 IOPMrootDomain::getNotificationClientName(OSObject *object)
9607 {
9608 IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9609 const char *clientName = "UNKNOWN";
9610
9611 if (!notifier->clientName) {
9612 // Check for user client
9613 if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9614 OSNumber *clientID = NULL;
9615 messageClient(kIOMessageCopyClientID, object, &clientID);
9616 if (clientID) {
9617 OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9618 if (string) {
9619 notifier->clientName = OSSymbol::withString(string.get());
9620 }
9621 clientID->release();
9622 }
9623 } else if (notifier->identifier) {
9624 notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9625 }
9626 }
9627
9628 if (notifier->clientName) {
9629 clientName = notifier->clientName->getCStringNoCopy();
9630 }
9631
9632 return clientName;
9633 }
9634
9635 void
9636 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9637 {
9638 IOPMServiceInterestNotifier *notifier;
9639
9640 if (systemBooting) {
9641 return;
9642 }
9643 notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9644 if (!notifier) {
9645 return;
9646 }
9647
9648 if (start) {
9649 pmTracer->traceDetail(notifier->uuid0 >> 32);
9650 kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9651 (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9652
9653 // Update notifier state used for response/ack logging
9654 notifier->msgIndex = msgIndex;
9655 notifier->msgAbsTime = timestamp;
9656
9657 if (msgIndex != UINT_MAX) {
9658 DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9659 } else {
9660 DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9661 }
9662
9663 assert(notifierObject == NULL);
9664 notifierThread = current_thread();
9665 notifierObject.reset(notifier, OSRetain);
9666 } else {
9667 uint64_t nsec;
9668 uint32_t delayMS;
9669
9670 SUB_ABSOLUTETIME(×tamp, ¬ifier->msgAbsTime);
9671 absolutetime_to_nanoseconds(timestamp, &nsec);
9672 delayMS = (uint32_t)(nsec / 1000000ULL);
9673 if (delayMS > notifier->maxMsgDelayMS) {
9674 notifier->maxMsgDelayMS = delayMS;
9675 }
9676
9677 assert(notifierObject == notifier);
9678 notifierObject.reset();
9679 notifierThread = NULL;
9680 }
9681 }
9682
9683 void
9684 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9685 {
9686 if (systemBooting) {
9687 return;
9688 }
9689 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9690 if (!notifier) {
9691 return;
9692 }
9693
9694 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9695 (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9696
9697 DLOG("%s[%u] ack from %s took %d ms\n",
9698 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9699 if (delay_ms > notifier->maxAckDelayMS) {
9700 notifier->maxAckDelayMS = delay_ms;
9701 }
9702 }
9703
9704 void
9705 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9706 {
9707 if (systemBooting) {
9708 return;
9709 }
9710 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9711 if (!notifier) {
9712 return;
9713 }
9714
9715 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9716 (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9717
9718 if (ack_time_us == 0) {
9719 // Client work is done and ack will not be forthcoming
9720 DLOG("%s[%u] response from %s took %d ms\n",
9721 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9722 } else {
9723 // Client needs more time and it must ack within ack_time_us
9724 DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9725 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9726 }
9727 }
9728
9729 void
9730 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9731 {
9732 if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9733 return;
9734 }
9735 if (systemBooting) {
9736 return;
9737 }
9738 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9739 if (!notifier) {
9740 return;
9741 }
9742
9743 DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9744 }
9745
9746 void
9747 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9748 {
9749 if (!systemBooting) {
9750 uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9751 pmTracer->traceDetail( detail );
9752 kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9753 DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9754 }
9755 }
9756
9757 void
9758 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9759 {
9760 size_t reportSize;
9761 void **report = NULL;
9762 uint32_t bktCnt;
9763 uint32_t bktSize;
9764 uint32_t *clientCnt;
9765
9766 ASSERT_GATED();
9767
9768 report = NULL;
9769 if (channel_id == kAssertDelayChID) {
9770 report = &assertOnWakeReport;
9771 bktCnt = kAssertDelayBcktCnt;
9772 bktSize = kAssertDelayBcktSize;
9773 clientCnt = &assertOnWakeClientCnt;
9774 } else if (channel_id == kSleepDelaysChID) {
9775 report = &sleepDelaysReport;
9776 bktCnt = kSleepDelaysBcktCnt;
9777 bktSize = kSleepDelaysBcktSize;
9778 clientCnt = &sleepDelaysClientCnt;
9779 } else {
9780 assert(false);
9781 return;
9782 }
9783
9784 switch (action) {
9785 case kIOReportEnable:
9786
9787 if (*report) {
9788 (*clientCnt)++;
9789 break;
9790 }
9791
9792 reportSize = HISTREPORT_BUFSIZE(bktCnt);
9793 *report = IOMallocZeroData(reportSize);
9794 if (*report == NULL) {
9795 break;
9796 }
9797 HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9798 getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9799
9800 if (channel_id == kAssertDelayChID) {
9801 assertOnWakeSecs = 0;
9802 }
9803
9804 break;
9805
9806 case kIOReportDisable:
9807 if (*clientCnt == 0) {
9808 break;
9809 }
9810 if (*clientCnt == 1) {
9811 IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9812 *report = NULL;
9813 }
9814 (*clientCnt)--;
9815
9816 if (channel_id == kAssertDelayChID) {
9817 assertOnWakeSecs = -1; // Invalid value to prevent updates
9818 }
9819 break;
9820
9821 case kIOReportGetDimensions:
9822 if (*report) {
9823 HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9824 }
9825 break;
9826 }
9827
9828 return;
9829 }
9830
9831 IOReturn
9832 IOPMrootDomain::configureReport(IOReportChannelList *channelList,
9833 IOReportConfigureAction action,
9834 void *result,
9835 void *destination)
9836 {
9837 unsigned cnt;
9838 uint64_t configAction = (uint64_t)action;
9839
9840 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9841 if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9842 (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9843 (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9844 if (action != kIOReportGetDimensions) {
9845 continue;
9846 }
9847 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9848 } else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9849 (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9850 gIOPMWorkLoop->runAction(
9851 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9852 (OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9853 (void *)configAction, (void *)result);
9854 }
9855 }
9856
9857 return super::configureReport(channelList, action, result, destination);
9858 }
9859
9860 IOReturn
9861 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9862 {
9863 uint32_t size2cpy;
9864 void *data2cpy;
9865 void **report;
9866
9867 ASSERT_GATED();
9868
9869 report = NULL;
9870 if (ch_id == kAssertDelayChID) {
9871 report = &assertOnWakeReport;
9872 } else if (ch_id == kSleepDelaysChID) {
9873 report = &sleepDelaysReport;
9874 } else {
9875 assert(false);
9876 return kIOReturnBadArgument;
9877 }
9878
9879 if (*report == NULL) {
9880 return kIOReturnNotOpen;
9881 }
9882
9883 HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9884 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9885 return kIOReturnOverrun;
9886 }
9887
9888 HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9889 dest->appendBytes(data2cpy, size2cpy);
9890
9891 return kIOReturnSuccess;
9892 }
9893
9894 IOReturn
9895 IOPMrootDomain::updateReport(IOReportChannelList *channelList,
9896 IOReportUpdateAction action,
9897 void *result,
9898 void *destination)
9899 {
9900 uint32_t size2cpy;
9901 void *data2cpy;
9902 uint8_t buf[SIMPLEREPORT_BUFSIZE];
9903 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9904 unsigned cnt;
9905 uint64_t ch_id;
9906
9907 if (action != kIOReportCopyChannelData) {
9908 goto exit;
9909 }
9910
9911 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9912 ch_id = channelList->channels[cnt].channel_id;
9913
9914 if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9915 gIOPMWorkLoop->runAction(
9916 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9917 (OSObject *)this, (void *)ch_id,
9918 (void *)result, (void *)dest);
9919 continue;
9920 } else if ((ch_id == kSleepCntChID) ||
9921 (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9922 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9923 } else {
9924 continue;
9925 }
9926
9927 if (ch_id == kSleepCntChID) {
9928 SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9929 } else if (ch_id == kDarkWkCntChID) {
9930 SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9931 } else if (ch_id == kUserWkCntChID) {
9932 SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9933 }
9934
9935 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9936 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9937 dest->appendBytes(data2cpy, size2cpy);
9938 }
9939
9940 exit:
9941 return super::updateReport(channelList, action, result, destination);
9942 }
9943
9944
9945 //******************************************************************************
9946 // PMTraceWorker Class
9947 //
9948 //******************************************************************************
9949
9950 #undef super
9951 #define super OSObject
9952 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9953
9954 #define kPMBestGuessPCIDevicesCount 25
9955 #define kPMMaxRTCBitfieldSize 32
9956
9957 OSPtr<PMTraceWorker>
9958 PMTraceWorker::tracer(IOPMrootDomain * owner)
9959 {
9960 OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9961 if (!me || !me->init()) {
9962 return NULL;
9963 }
9964
9965 DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9966
9967 // Note that we cannot instantiate the PCI device -> bit mappings here, since
9968 // the IODeviceTree has not yet been created by IOPlatformExpert. We create
9969 // this dictionary lazily.
9970 me->owner = owner;
9971 me->pciDeviceBitMappings = NULL;
9972 me->pmTraceWorkerLock = IOLockAlloc();
9973 me->tracePhase = kIOPMTracePointSystemUp;
9974 me->traceData32 = 0;
9975 me->loginWindowData = 0;
9976 me->coreDisplayData = 0;
9977 me->coreGraphicsData = 0;
9978 return me;
9979 }
9980
9981 void
9982 PMTraceWorker::RTC_TRACE(void)
9983 {
9984 if (tracePointHandler && tracePointTarget) {
9985 uint32_t wordA;
9986
9987 IOLockLock(pmTraceWorkerLock);
9988 wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9989 (coreGraphicsData << 8) | tracePhase;
9990 IOLockUnlock(pmTraceWorkerLock);
9991
9992 tracePointHandler( tracePointTarget, traceData32, wordA );
9993 _LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9994 }
9995 #if DEVELOPMENT || DEBUG
9996 if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9997 DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9998 IOLock *l = IOLockAlloc();
9999 IOLockLock(l);
10000 IOLockLock(l);
10001 }
10002 #endif /* DEVELOPMENT || DEBUG */
10003 }
10004
10005 int
10006 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
10007 {
10008 OSSharedPtr<const OSSymbol> deviceName;
10009 int index = -1;
10010
10011 IOLockLock(pmTraceWorkerLock);
10012
10013 if (!pciDeviceBitMappings) {
10014 pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
10015 if (!pciDeviceBitMappings) {
10016 goto exit;
10017 }
10018 }
10019
10020 // Check for bitmask overflow.
10021 if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
10022 goto exit;
10023 }
10024
10025 if ((deviceName = pciDevice->copyName()) &&
10026 (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
10027 pciDeviceBitMappings->setObject(deviceName.get())) {
10028 index = pciDeviceBitMappings->getCount() - 1;
10029 _LOG("PMTrace PCI array: set object %s => %d\n",
10030 deviceName->getCStringNoCopy(), index);
10031 }
10032
10033 if (!addedToRegistry && (index >= 0)) {
10034 addedToRegistry = owner->setProperty("PCITopLevel", this);
10035 }
10036
10037 exit:
10038 IOLockUnlock(pmTraceWorkerLock);
10039 return index;
10040 }
10041
10042 bool
10043 PMTraceWorker::serialize(OSSerialize *s) const
10044 {
10045 bool ok = false;
10046 if (pciDeviceBitMappings) {
10047 IOLockLock(pmTraceWorkerLock);
10048 ok = pciDeviceBitMappings->serialize(s);
10049 IOLockUnlock(pmTraceWorkerLock);
10050 }
10051 return ok;
10052 }
10053
10054 void
10055 PMTraceWorker::tracePoint(uint8_t phase)
10056 {
10057 // clear trace detail when phase begins
10058 if (tracePhase != phase) {
10059 traceData32 = 0;
10060 }
10061
10062 tracePhase = phase;
10063
10064 DLOG("trace point 0x%02x\n", tracePhase);
10065 RTC_TRACE();
10066 }
10067
10068 void
10069 PMTraceWorker::traceDetail(uint32_t detail)
10070 {
10071 if (detail == traceData32) {
10072 return;
10073 }
10074 traceData32 = detail;
10075 RTC_TRACE();
10076 }
10077
10078 void
10079 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
10080 {
10081 switch (component) {
10082 case kIOPMLoginWindowProgress:
10083 loginWindowData = data & kIOPMLoginWindowProgressMask;
10084 break;
10085 case kIOPMCoreDisplayProgress:
10086 coreDisplayData = data & kIOPMCoreDisplayProgressMask;
10087 break;
10088 case kIOPMCoreGraphicsProgress:
10089 coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
10090 break;
10091 default:
10092 return;
10093 }
10094
10095 DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
10096 RTC_TRACE();
10097 }
10098
10099 void
10100 PMTraceWorker::tracePCIPowerChange(
10101 change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
10102 {
10103 uint32_t bitMask;
10104 uint32_t expectedFlag;
10105
10106 // Ignore PCI changes outside of system sleep/wake.
10107 if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
10108 (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
10109 return;
10110 }
10111
10112 // Only record the WillChange transition when going to sleep,
10113 // and the DidChange on the way up.
10114 changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
10115 expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
10116 kIOPMDomainWillChange : kIOPMDomainDidChange;
10117 if (changeFlags != expectedFlag) {
10118 return;
10119 }
10120
10121 // Mark this device off in our bitfield
10122 if (bitNum < kPMMaxRTCBitfieldSize) {
10123 bitMask = (1 << bitNum);
10124
10125 if (kPowerChangeStart == type) {
10126 traceData32 |= bitMask;
10127 _LOG("PMTrace: Device %s started - bit %2d mask 0x%08x => 0x%08x\n",
10128 service->getName(), bitNum, bitMask, traceData32);
10129 owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
10130 } else {
10131 traceData32 &= ~bitMask;
10132 _LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
10133 service->getName(), bitNum, bitMask, traceData32);
10134 owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
10135 }
10136
10137 DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
10138 RTC_TRACE();
10139 }
10140 }
10141
10142 uint64_t
10143 PMTraceWorker::getPMStatusCode()
10144 {
10145 return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
10146 }
10147
10148 uint8_t
10149 PMTraceWorker::getTracePhase()
10150 {
10151 return tracePhase;
10152 }
10153
10154 uint32_t
10155 PMTraceWorker::getTraceData()
10156 {
10157 return traceData32;
10158 }
10159
10160 // MARK: -
10161 // MARK: PMHaltWorker
10162
10163 //******************************************************************************
10164 // PMHaltWorker Class
10165 //
10166 //******************************************************************************
10167
10168 PMHaltWorker *
10169 PMHaltWorker::worker( void )
10170 {
10171 PMHaltWorker * me;
10172 IOThread thread;
10173
10174 do {
10175 me = OSTypeAlloc( PMHaltWorker );
10176 if (!me || !me->init()) {
10177 break;
10178 }
10179
10180 me->lock = IOLockAlloc();
10181 if (!me->lock) {
10182 break;
10183 }
10184
10185 DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
10186 me->retain(); // thread holds extra retain
10187 if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
10188 me->release();
10189 break;
10190 }
10191 thread_deallocate(thread);
10192 return me;
10193 } while (false);
10194
10195 if (me) {
10196 me->release();
10197 }
10198 return NULL;
10199 }
10200
10201 void
10202 PMHaltWorker::free( void )
10203 {
10204 DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
10205 if (lock) {
10206 IOLockFree(lock);
10207 lock = NULL;
10208 }
10209 return OSObject::free();
10210 }
10211
10212 void
10213 PMHaltWorker::main( void * arg, wait_result_t waitResult )
10214 {
10215 PMHaltWorker * me = (PMHaltWorker *) arg;
10216
10217 IOLockLock( gPMHaltLock );
10218 gPMHaltBusyCount++;
10219 me->depth = gPMHaltDepth;
10220 IOLockUnlock( gPMHaltLock );
10221
10222 while (me->depth >= 0) {
10223 PMHaltWorker::work( me );
10224
10225 IOLockLock( gPMHaltLock );
10226 if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
10227 // This is the last thread to finish work on this level,
10228 // inform everyone to start working on next lower level.
10229 gPMHaltDepth--;
10230 me->depth = gPMHaltDepth;
10231 gPMHaltIdleCount = 0;
10232 thread_wakeup((event_t) &gPMHaltIdleCount);
10233 } else {
10234 // One or more threads are still working on this level,
10235 // this thread must wait.
10236 me->depth = gPMHaltDepth - 1;
10237 do {
10238 IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
10239 } while (me->depth != gPMHaltDepth);
10240 }
10241 IOLockUnlock( gPMHaltLock );
10242 }
10243
10244 // No more work to do, terminate thread
10245 DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
10246 thread_wakeup( &gPMHaltDepth );
10247 me->release();
10248 }
10249
10250 void
10251 PMHaltWorker::work( PMHaltWorker * me )
10252 {
10253 OSSharedPtr<IOService> service;
10254 OSSet * inner;
10255 AbsoluteTime startTime, elapsedTime;
10256 UInt32 deltaTime;
10257 bool timeout;
10258
10259 while (true) {
10260 timeout = false;
10261
10262 // Claim an unit of work from the shared pool
10263 IOLockLock( gPMHaltLock );
10264 inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10265 if (inner) {
10266 service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10267 if (service) {
10268 inner->removeObject(service.get());
10269 }
10270 }
10271 IOLockUnlock( gPMHaltLock );
10272 if (!service) {
10273 break; // no more work at this depth
10274 }
10275 clock_get_uptime(&startTime);
10276
10277 if (!service->isInactive() &&
10278 service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10279 IOLockLock(me->lock);
10280 me->startTime = startTime;
10281 me->service = service.get();
10282 me->timeout = false;
10283 IOLockUnlock(me->lock);
10284
10285 service->systemWillShutdown( gPMHaltMessageType);
10286
10287 // Wait for driver acknowledgement
10288 IOLockLock(me->lock);
10289 while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10290 IOLockSleep(me->lock, me, THREAD_UNINT);
10291 }
10292 me->service = NULL;
10293 timeout = me->timeout;
10294 IOLockUnlock(me->lock);
10295 }
10296
10297 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10298 if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10299 LOG("%s driver %s (0x%llx) took %u ms\n",
10300 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10301 "PowerOff" : "Restart",
10302 service->getName(), service->getRegistryEntryID(),
10303 (uint32_t) deltaTime );
10304 halt_log_enter("PowerOff/Restart handler completed",
10305 OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10306 elapsedTime);
10307 }
10308
10309 me->visits++;
10310 }
10311 }
10312
10313 void
10314 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10315 {
10316 UInt64 nano;
10317 AbsoluteTime startTime;
10318 AbsoluteTime endTime;
10319
10320 endTime = *now;
10321
10322 IOLockLock(me->lock);
10323 if (me->service && !me->timeout) {
10324 startTime = me->startTime;
10325 nano = 0;
10326 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10327 SUB_ABSOLUTETIME(&endTime, &startTime);
10328 absolutetime_to_nanoseconds(endTime, &nano);
10329 }
10330 if (nano > 3000000000ULL) {
10331 me->timeout = true;
10332
10333 halt_log_enter("PowerOff/Restart still waiting on handler",
10334 OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10335 endTime);
10336 MSG("%s still waiting on %s\n",
10337 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ? "PowerOff" : "Restart",
10338 me->service->getName());
10339 }
10340 }
10341 IOLockUnlock(me->lock);
10342 }
10343
10344 //******************************************************************************
10345 // acknowledgeSystemWillShutdown
10346 //
10347 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10348 //******************************************************************************
10349
10350 void
10351 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10352 {
10353 PMHaltWorker * worker;
10354 OSSharedPtr<OSObject> prop;
10355
10356 if (!from) {
10357 return;
10358 }
10359
10360 //DLOG("%s acknowledged\n", from->getName());
10361 prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10362 if (prop) {
10363 worker = (PMHaltWorker *) prop.get();
10364 IOLockLock(worker->lock);
10365 from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10366 thread_wakeup((event_t) worker);
10367 IOLockUnlock(worker->lock);
10368 } else {
10369 DLOG("%s acknowledged without worker property\n",
10370 from->getName());
10371 }
10372 }
10373
10374
10375 //******************************************************************************
10376 // notifySystemShutdown
10377 //
10378 // Notify all objects in PM tree that system will shutdown or restart
10379 //******************************************************************************
10380
10381 static void
10382 notifySystemShutdown( IOService * root, uint32_t messageType )
10383 {
10384 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10385 OSSharedPtr<IORegistryIterator> iter;
10386 IORegistryEntry * entry;
10387 IOService * node;
10388 OSSet * inner;
10389 OSSharedPtr<OSSet> newInner;
10390 PMHaltWorker * workers[kPMHaltMaxWorkers];
10391 AbsoluteTime deadline;
10392 unsigned int totalNodes = 0;
10393 unsigned int depth;
10394 unsigned int rootDepth;
10395 unsigned int numWorkers;
10396 unsigned int count;
10397 int waitResult;
10398 void * baseFunc;
10399 bool ok;
10400
10401 DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10402
10403 baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10404
10405 // Iterate the entire PM tree starting from root
10406
10407 rootDepth = root->getDepth( gIOPowerPlane );
10408 if (!rootDepth) {
10409 goto done;
10410 }
10411
10412 // debug - for repeated test runs
10413 while (PMHaltWorker::metaClass->getInstanceCount()) {
10414 IOSleep(1);
10415 }
10416
10417 if (!gPMHaltArray) {
10418 gPMHaltArray = OSArray::withCapacity(40);
10419 if (!gPMHaltArray) {
10420 goto done;
10421 }
10422 } else { // debug
10423 gPMHaltArray->flushCollection();
10424 }
10425
10426 if (!gPMHaltLock) {
10427 gPMHaltLock = IOLockAlloc();
10428 if (!gPMHaltLock) {
10429 goto done;
10430 }
10431 }
10432
10433 if (!gPMHaltClientAcknowledgeKey) {
10434 gPMHaltClientAcknowledgeKey =
10435 OSSymbol::withCStringNoCopy("PMShutdown");
10436 if (!gPMHaltClientAcknowledgeKey) {
10437 goto done;
10438 }
10439 }
10440
10441 gPMHaltMessageType = messageType;
10442
10443 // Depth-first walk of PM plane
10444
10445 iter = IORegistryIterator::iterateOver(
10446 root, gIOPowerPlane, kIORegistryIterateRecursively);
10447
10448 if (iter) {
10449 while ((entry = iter->getNextObject())) {
10450 node = OSDynamicCast(IOService, entry);
10451 if (!node) {
10452 continue;
10453 }
10454
10455 if (baseFunc ==
10456 OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10457 continue;
10458 }
10459
10460 depth = node->getDepth( gIOPowerPlane );
10461 if (depth <= rootDepth) {
10462 continue;
10463 }
10464
10465 ok = false;
10466
10467 // adjust to zero based depth
10468 depth -= (rootDepth + 1);
10469
10470 // gPMHaltArray is an array of containers, each container
10471 // refers to nodes with the same depth.
10472
10473 count = gPMHaltArray->getCount();
10474 while (depth >= count) {
10475 // expand array and insert placeholders
10476 gPMHaltArray->setObject(PLACEHOLDER);
10477 count++;
10478 }
10479 count = gPMHaltArray->getCount();
10480 if (depth < count) {
10481 inner = (OSSet *)gPMHaltArray->getObject(depth);
10482 if (inner == PLACEHOLDER) {
10483 newInner = OSSet::withCapacity(40);
10484 if (newInner) {
10485 gPMHaltArray->replaceObject(depth, newInner.get());
10486 inner = newInner.get();
10487 }
10488 }
10489
10490 // PM nodes that appear more than once in the tree will have
10491 // the same depth, OSSet will refuse to add the node twice.
10492 if (inner) {
10493 ok = inner->setObject(node);
10494 }
10495 }
10496 if (!ok) {
10497 DLOG("Skipped PM node %s\n", node->getName());
10498 }
10499 }
10500 }
10501
10502 // debug only
10503 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10504 count = 0;
10505 if (inner != PLACEHOLDER) {
10506 count = inner->getCount();
10507 }
10508 DLOG("Nodes at depth %u = %u\n", i, count);
10509 }
10510
10511 // strip placeholders (not all depths are populated)
10512 numWorkers = 0;
10513 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10514 if (inner == PLACEHOLDER) {
10515 gPMHaltArray->removeObject(i);
10516 continue;
10517 }
10518 count = inner->getCount();
10519 if (count > numWorkers) {
10520 numWorkers = count;
10521 }
10522 totalNodes += count;
10523 i++;
10524 }
10525
10526 if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10527 goto done;
10528 }
10529
10530 gPMHaltBusyCount = 0;
10531 gPMHaltIdleCount = 0;
10532 gPMHaltDepth = gPMHaltArray->getCount() - 1;
10533
10534 // Create multiple workers (and threads)
10535
10536 if (numWorkers > kPMHaltMaxWorkers) {
10537 numWorkers = kPMHaltMaxWorkers;
10538 }
10539
10540 DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10541 totalNodes, gPMHaltArray->getCount(), numWorkers);
10542
10543 for (unsigned int i = 0; i < numWorkers; i++) {
10544 workers[i] = PMHaltWorker::worker();
10545 }
10546
10547 // Wait for workers to exhaust all available work
10548
10549 IOLockLock(gPMHaltLock);
10550 while (gPMHaltDepth >= 0) {
10551 clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10552
10553 waitResult = IOLockSleepDeadline(
10554 gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10555 if (THREAD_TIMED_OUT == waitResult) {
10556 AbsoluteTime now;
10557 clock_get_uptime(&now);
10558
10559 IOLockUnlock(gPMHaltLock);
10560 for (unsigned int i = 0; i < numWorkers; i++) {
10561 if (workers[i]) {
10562 PMHaltWorker::checkTimeout(workers[i], &now);
10563 }
10564 }
10565 IOLockLock(gPMHaltLock);
10566 }
10567 }
10568 IOLockUnlock(gPMHaltLock);
10569
10570 // Release all workers
10571
10572 for (unsigned int i = 0; i < numWorkers; i++) {
10573 if (workers[i]) {
10574 workers[i]->release();
10575 }
10576 // worker also retained by it's own thread
10577 }
10578
10579 done:
10580 DLOG("%s done\n", __FUNCTION__);
10581 return;
10582 }
10583
10584 // MARK: -
10585 // MARK: Kernel Assertion
10586
10587 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10588
10589 IOPMDriverAssertionID
10590 IOPMrootDomain::createPMAssertion(
10591 IOPMDriverAssertionType whichAssertionBits,
10592 IOPMDriverAssertionLevel assertionLevel,
10593 IOService *ownerService,
10594 const char *ownerDescription)
10595 {
10596 IOReturn ret;
10597 IOPMDriverAssertionID newAssertion;
10598
10599 if (!pmAssertions) {
10600 return 0;
10601 }
10602
10603 ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10604
10605 if (kIOReturnSuccess == ret) {
10606 #if (DEVELOPMENT || DEBUG)
10607 if (_aotNow || (kIOLogPMRootDomain & gIOKitDebug)) {
10608 const char *serviceName = (ownerService && ownerService->reserved) ? ownerService->getName() : NULL;
10609 OSReportWithBacktrace("PMRD: createPMAssertion(0x%qx) %s (%s)", newAssertion,
10610 serviceName, ownerDescription);
10611 }
10612 #endif /* (DEVELOPMENT || DEBUG) */
10613 return newAssertion;
10614 } else {
10615 return 0;
10616 }
10617 }
10618
10619 IOReturn
10620 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10621 {
10622 #if (DEVELOPMENT || DEBUG)
10623 if (_aotNow || (kIOLogPMRootDomain & gIOKitDebug)) {
10624 PMAssertStruct *details = pmAssertions->detailsForID(releaseAssertion, NULL);
10625 if (details) {
10626 const char *serviceName = (details->ownerService && details->ownerService->reserved) ?
10627 details->ownerService->getName() : NULL;
10628 const char *ownerString = details->ownerString ? details->ownerString->getCStringNoCopy() : NULL;
10629 OSReportWithBacktrace("PMRD: releasePMAssertion(0x%qx) %s (%s)", releaseAssertion, serviceName, ownerString);
10630 } else {
10631 OSReportWithBacktrace("PMRD: releasePMAssertion(0x%qx)", releaseAssertion);
10632 }
10633 }
10634 #endif /* (DEVELOPMENT || DEBUG) */
10635 if (!pmAssertions) {
10636 return kIOReturnInternalError;
10637 }
10638 return pmAssertions->releaseAssertion(releaseAssertion);
10639 }
10640
10641
10642 IOReturn
10643 IOPMrootDomain::setPMAssertionLevel(
10644 IOPMDriverAssertionID assertionID,
10645 IOPMDriverAssertionLevel assertionLevel)
10646 {
10647 return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10648 }
10649
10650 IOPMDriverAssertionLevel
10651 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10652 {
10653 IOPMDriverAssertionType sysLevels;
10654
10655 if (!pmAssertions || whichAssertion == 0) {
10656 return kIOPMDriverAssertionLevelOff;
10657 }
10658
10659 sysLevels = pmAssertions->getActivatedAssertions();
10660
10661 // Check that every bit set in argument 'whichAssertion' is asserted
10662 // in the aggregate bits.
10663 if ((sysLevels & whichAssertion) == whichAssertion) {
10664 return kIOPMDriverAssertionLevelOn;
10665 } else {
10666 return kIOPMDriverAssertionLevelOff;
10667 }
10668 }
10669
10670 IOReturn
10671 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10672 {
10673 if (!pmAssertions) {
10674 return kIOReturnNotFound;
10675 }
10676
10677 return pmAssertions->setUserAssertionLevels(inLevels);
10678 }
10679
10680 IOReturn
10681 IOPMrootDomain::acquireDriverKitMatchingAssertion()
10682 {
10683 return gIOPMWorkLoop->runActionBlock(^{
10684 if (_driverKitMatchingAssertionCount != 0) {
10685 _driverKitMatchingAssertionCount++;
10686 return kIOReturnSuccess;
10687 } else {
10688 if (kSystemTransitionSleep == _systemTransitionType) {
10689 // system going to sleep
10690 return kIOReturnBusy;
10691 } else {
10692 // createPMAssertion is asynchronous.
10693 // we must also set _driverKitMatchingAssertionCount under the PM workloop lock so that we can cancel sleep immediately
10694 // The assertion is used so that on release, we reevaluate all assertions
10695 _driverKitMatchingAssertion = createPMAssertion(kIOPMDriverAssertionCPUBit, kIOPMDriverAssertionLevelOn, this, "DK matching");
10696 if (_driverKitMatchingAssertion != kIOPMUndefinedDriverAssertionID) {
10697 _driverKitMatchingAssertionCount = 1;
10698 return kIOReturnSuccess;
10699 } else {
10700 return kIOReturnBusy;
10701 }
10702 }
10703 }
10704 });
10705 }
10706
10707 void
10708 IOPMrootDomain::releaseDriverKitMatchingAssertion()
10709 {
10710 gIOPMWorkLoop->runActionBlock(^{
10711 if (_driverKitMatchingAssertionCount != 0) {
10712 _driverKitMatchingAssertionCount--;
10713 if (_driverKitMatchingAssertionCount == 0) {
10714 releasePMAssertion(_driverKitMatchingAssertion);
10715 _driverKitMatchingAssertion = kIOPMUndefinedDriverAssertionID;
10716 }
10717 } else {
10718 panic("Over-release of driverkit matching assertion");
10719 }
10720 return kIOReturnSuccess;
10721 });
10722 }
10723
10724 bool
10725 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10726 {
10727 if (pmAssertions) {
10728 pmAssertions->publishProperties();
10729 }
10730 return IOService::serializeProperties(s);
10731 }
10732
10733 OSSharedPtr<OSObject>
10734 IOPMrootDomain::copyProperty( const char * aKey) const
10735 {
10736 OSSharedPtr<OSObject> obj;
10737 obj = IOService::copyProperty(aKey);
10738
10739 if (obj) {
10740 return obj;
10741 }
10742
10743 if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10744 sizeof(kIOPMSleepWakeWdogRebootKey))) {
10745 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10746 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10747 } else {
10748 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10749 }
10750 }
10751
10752 if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10753 sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10754 if (swd_flags & SWD_VALID_LOGS) {
10755 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10756 } else {
10757 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10758 }
10759 }
10760
10761 /*
10762 * XXX: We should get rid of "DesktopMode" property when 'kAppleClamshellCausesSleepKey'
10763 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10764 * issued by DisplayWrangler on darkwake.
10765 */
10766 if (!strcmp(aKey, "DesktopMode")) {
10767 if (desktopMode) {
10768 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10769 } else {
10770 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10771 }
10772 }
10773 if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10774 if (displayIdleForDemandSleep) {
10775 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10776 } else {
10777 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10778 }
10779 }
10780
10781 if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10782 OSSharedPtr<OSArray> array;
10783 WAKEEVENT_LOCK();
10784 if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10785 OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10786 if (collection) {
10787 array = OSDynamicPtrCast<OSArray>(collection);
10788 }
10789 }
10790 WAKEEVENT_UNLOCK();
10791 return os::move(array);
10792 }
10793
10794 if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10795 OSSharedPtr<OSArray> array;
10796 IOLockLock(pmStatsLock);
10797 if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10798 OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10799 if (collection) {
10800 array = OSDynamicPtrCast<OSArray>(collection);
10801 }
10802 }
10803 IOLockUnlock(pmStatsLock);
10804 return os::move(array);
10805 }
10806
10807 if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10808 OSArray *idleSleepList = NULL;
10809 gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10810 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10811 }
10812
10813 if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10814 OSArray *systemSleepList = NULL;
10815 gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10816 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10817 }
10818
10819 if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10820 OSArray *idleSleepList = NULL;
10821 gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10822 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10823 }
10824
10825 if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10826 OSArray *systemSleepList = NULL;
10827 gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10828 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10829 }
10830 return NULL;
10831 }
10832
10833 // MARK: -
10834 // MARK: Wake Event Reporting
10835
10836 void
10837 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10838 {
10839 WAKEEVENT_LOCK();
10840 strlcpy(outBuf, gWakeReasonString, bufSize);
10841 WAKEEVENT_UNLOCK();
10842 }
10843
10844 void
10845 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10846 {
10847 WAKEEVENT_LOCK();
10848 strlcpy(outBuf, gShutdownReasonString, bufSize);
10849 WAKEEVENT_UNLOCK();
10850 }
10851
10852 //******************************************************************************
10853 // acceptSystemWakeEvents
10854 //
10855 // Private control for the acceptance of driver wake event claims.
10856 //******************************************************************************
10857
10858 void
10859 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10860 {
10861 bool logWakeReason = false;
10862
10863 WAKEEVENT_LOCK();
10864 switch (control) {
10865 case kAcceptSystemWakeEvents_Enable:
10866 assert(_acceptSystemWakeEvents == false);
10867 if (!_systemWakeEventsArray) {
10868 _systemWakeEventsArray = OSArray::withCapacity(4);
10869 }
10870 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10871 if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10872 gWakeReasonString[0] = '\0';
10873 if (_systemWakeEventsArray) {
10874 _systemWakeEventsArray->flushCollection();
10875 }
10876 }
10877
10878 // Remove stale WakeType property before system sleep
10879 removeProperty(kIOPMRootDomainWakeTypeKey);
10880 removeProperty(kIOPMRootDomainWakeReasonKey);
10881 break;
10882
10883 case kAcceptSystemWakeEvents_Disable:
10884 _acceptSystemWakeEvents = false;
10885 #if defined(XNU_TARGET_OS_OSX)
10886 logWakeReason = (gWakeReasonString[0] != '\0');
10887 #else /* !defined(XNU_TARGET_OS_OSX) */
10888 logWakeReason = gWakeReasonSysctlRegistered;
10889 #if DEVELOPMENT
10890 static int panic_allowed = -1;
10891
10892 if ((panic_allowed == -1) &&
10893 (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10894 panic_allowed = 0;
10895 }
10896
10897 if (panic_allowed) {
10898 size_t i = 0;
10899 // Panic if wake reason is null or empty
10900 for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10901 if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10902 break;
10903 }
10904 }
10905 if (i >= strlen(gWakeReasonString)) {
10906 panic("Wake reason is empty");
10907 }
10908 }
10909 #endif /* DEVELOPMENT */
10910 #endif /* !defined(XNU_TARGET_OS_OSX) */
10911
10912 // publish kIOPMRootDomainWakeReasonKey if not already set
10913 if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10914 setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10915 }
10916 break;
10917
10918 case kAcceptSystemWakeEvents_Reenable:
10919 assert(_acceptSystemWakeEvents == false);
10920 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10921 removeProperty(kIOPMRootDomainWakeReasonKey);
10922 break;
10923 }
10924 WAKEEVENT_UNLOCK();
10925
10926 if (logWakeReason) {
10927 MSG("system wake events: %s\n", gWakeReasonString);
10928 }
10929 }
10930
10931 //******************************************************************************
10932 // claimSystemWakeEvent
10933 //
10934 // For a driver to claim a device is the source/conduit of a system wake event.
10935 //******************************************************************************
10936
10937 void
10938 IOPMrootDomain::claimSystemWakeEvent(
10939 IOService * device,
10940 IOOptionBits flags,
10941 const char * reason,
10942 OSObject * details )
10943 {
10944 OSSharedPtr<const OSSymbol> deviceName;
10945 OSSharedPtr<OSNumber> deviceRegId;
10946 OSSharedPtr<OSNumber> claimTime;
10947 OSSharedPtr<OSData> flagsData;
10948 OSSharedPtr<OSString> reasonString;
10949 OSSharedPtr<OSDictionary> dict;
10950 uint64_t timestamp;
10951 bool addWakeReason;
10952
10953 if (!device || !reason) {
10954 return;
10955 }
10956
10957 pmEventTimeStamp(×tamp);
10958
10959 uint64_t args[3] = {};
10960 strlcpy((char *)args, reason, sizeof(args));
10961 kdebugTrace(kPMLogClaimSystemWake, args[0], args[1], args[2], device->getRegistryEntryID());
10962
10963 IOOptionBits aotFlags = 0;
10964 bool needAOTEvaluate = FALSE;
10965
10966 if (kIOPMAOTModeAddEventFlags & _aotMode) {
10967 // Only allow lingering in AOT_STATE for the two wake reasons used for the wrist raise gesture.
10968 if (strcmp("AOP.OutboxNotEmpty", reason) && strcmp("spu_gesture", reason)) {
10969 flags |= kIOPMWakeEventAOTExit;
10970 }
10971 }
10972
10973 #if DEVELOPMENT || DEBUG
10974 if (_aotLingerTime && !strcmp("rtc", reason)) {
10975 flags |= kIOPMWakeEventAOTPossibleExit;
10976 }
10977 #endif /* DEVELOPMENT || DEBUG */
10978
10979 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10980 // Publishing the WakeType is serialized by the PM work loop
10981 if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10982 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10983 (void *) _nextScheduledAlarmType.get());
10984 }
10985
10986 // Workaround for the missing wake HID event
10987 if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10988 if (!strcmp("trackpadkeyboard", reason)) {
10989 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10990 (void *) gIOPMWakeTypeUserKey.get());
10991 }
10992 }
10993 #endif
10994
10995 deviceName = device->copyName(gIOServicePlane);
10996 deviceRegId = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10997 claimTime = OSNumber::withNumber(timestamp, 64);
10998 flagsData = OSData::withValue(flags);
10999 reasonString = OSString::withCString(reason);
11000 dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
11001 if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
11002 goto done;
11003 }
11004
11005 dict->setObject(gIONameKey, deviceName.get());
11006 dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
11007 dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
11008 dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
11009 dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
11010 if (details) {
11011 dict->setObject(kIOPMWakeEventDetailsKey, details);
11012 }
11013
11014 WAKEEVENT_LOCK();
11015 addWakeReason = _acceptSystemWakeEvents;
11016 if (_aotMode) {
11017 IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
11018 }
11019 aotFlags = (kIOPMWakeEventAOTFlags & flags);
11020 aotFlags = (aotFlags & ~_aotPendingFlags);
11021 needAOTEvaluate = false;
11022 if (_aotNow && aotFlags) {
11023 if (kIOPMWakeEventAOTPossibleExit & flags) {
11024 _aotMetrics->possibleCount++;
11025 }
11026 if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
11027 _aotMetrics->confirmedPossibleCount++;
11028 }
11029 if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
11030 _aotMetrics->rejectedPossibleCount++;
11031 }
11032 if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
11033 _aotMetrics->expiredPossibleCount++;
11034 }
11035
11036 _aotPendingFlags |= aotFlags;
11037 addWakeReason = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
11038 needAOTEvaluate = _aotReadyToFullWake;
11039 }
11040 DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
11041 reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
11042 _aotNow, pmTracer->getTracePhase(), addWakeReason);
11043
11044 #if DEVELOPMENT || DEBUG
11045 if (addWakeReason) {
11046 record_system_event(SYSTEM_EVENT_TYPE_INFO,
11047 SYSTEM_EVENT_SUBSYSTEM_PMRD,
11048 "Report System Wake Event",
11049 "Reason: %s Flags: 0x%x Device: %s, DeviceRegEntry: 0x%llx\n",
11050 reason,
11051 (int)flags,
11052 deviceName->getCStringNoCopy(),
11053 device->getRegistryEntryID()
11054 );
11055 }
11056 #endif /* DEVELOPMENT || DEBUG */
11057
11058 if (!gWakeReasonSysctlRegistered) {
11059 // Lazy registration until the platform driver stops registering
11060 // the same name.
11061 gWakeReasonSysctlRegistered = true;
11062 }
11063 if (addWakeReason) {
11064 _systemWakeEventsArray->setObject(dict.get());
11065 if (gWakeReasonString[0] != '\0') {
11066 strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
11067 }
11068 strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
11069 }
11070
11071 WAKEEVENT_UNLOCK();
11072 if (needAOTEvaluate) {
11073 // Call aotEvaluate() on PM work loop since it may call
11074 // aotExit() which accesses PM state.
11075 pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
11076 }
11077
11078 done:
11079 return;
11080 }
11081
11082 //******************************************************************************
11083 // claimSystemBootEvent
11084 //
11085 // For a driver to claim a device is the source/conduit of a system boot event.
11086 //******************************************************************************
11087
11088 void
11089 IOPMrootDomain::claimSystemBootEvent(
11090 IOService * device,
11091 IOOptionBits flags,
11092 const char * reason,
11093 __unused OSObject * details )
11094 {
11095 if (!device || !reason) {
11096 return;
11097 }
11098
11099 DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
11100 #if DEVELOPMENT || DEBUG
11101 record_system_event(SYSTEM_EVENT_TYPE_INFO,
11102 SYSTEM_EVENT_SUBSYSTEM_PMRD,
11103 "Report System Boot Device",
11104 "Reason: %s Flags: 0x%x Device: %s",
11105 reason,
11106 (int)flags,
11107 device->getName()
11108 );
11109 #endif /* DEVELOPMENT || DEBUG */
11110 WAKEEVENT_LOCK();
11111 if (!gBootReasonSysctlRegistered) {
11112 // Lazy sysctl registration after setting gBootReasonString
11113 strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
11114 os_atomic_store(&gBootReasonSysctlRegistered, true, release);
11115 }
11116 WAKEEVENT_UNLOCK();
11117 }
11118
11119 //******************************************************************************
11120 // claimSystemShutdownEvent
11121 //
11122 // For drivers to claim a system shutdown event on the ensuing boot.
11123 //******************************************************************************
11124
11125 void
11126 IOPMrootDomain::claimSystemShutdownEvent(
11127 IOService * device,
11128 IOOptionBits flags,
11129 const char * reason,
11130 __unused OSObject * details )
11131 {
11132 if (!device || !reason) {
11133 return;
11134 }
11135
11136 DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
11137 #if DEVELOPMENT || DEBUG
11138 record_system_event(SYSTEM_EVENT_TYPE_INFO,
11139 SYSTEM_EVENT_SUBSYSTEM_PMRD,
11140 "Report System Shutdown Cause From Previous Boot",
11141 "Reason: %s Flags: 0x%x Device: %s",
11142 reason,
11143 (int)flags,
11144 device->getName()
11145 );
11146 #endif /* DEVELOPMENT || DEBUG */
11147 WAKEEVENT_LOCK();
11148 if (gShutdownReasonString[0] != '\0') {
11149 strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
11150 }
11151 strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
11152
11153 gShutdownReasonSysctlRegistered = true;
11154 WAKEEVENT_UNLOCK();
11155 }
11156
11157 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11158
11159 // MARK: -
11160 // MARK: PMSettingHandle
11161
11162 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
11163
11164 void
11165 PMSettingHandle::free( void )
11166 {
11167 if (pmso) {
11168 pmso->clientHandleFreed();
11169 pmso->release();
11170 pmso = NULL;
11171 }
11172
11173 OSObject::free();
11174 }
11175
11176 // MARK: -
11177 // MARK: PMSettingObject
11178
11179 #undef super
11180 #define super OSObject
11181 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
11182
11183 /*
11184 * Static constructor/initializer for PMSettingObject
11185 */
11186 PMSettingObject *PMSettingObject::pmSettingObject(
11187 IOPMrootDomain * parent_arg,
11188 IOPMSettingControllerCallback handler_arg,
11189 OSObject * target_arg,
11190 uintptr_t refcon_arg,
11191 uint32_t supportedPowerSources,
11192 const OSSymbol * settings[],
11193 OSObject * *handle_obj)
11194 {
11195 uint32_t settingCount = 0;
11196 PMSettingObject *pmso = NULL;
11197 PMSettingHandle *pmsh = NULL;
11198
11199 if (!parent_arg || !handler_arg || !settings || !handle_obj) {
11200 return NULL;
11201 }
11202
11203 // count OSSymbol entries in NULL terminated settings array
11204 while (settings[settingCount]) {
11205 settingCount++;
11206 }
11207 if (0 == settingCount) {
11208 return NULL;
11209 }
11210
11211 pmso = new PMSettingObject;
11212 if (!pmso || !pmso->init()) {
11213 goto fail;
11214 }
11215
11216 pmsh = new PMSettingHandle;
11217 if (!pmsh || !pmsh->init()) {
11218 goto fail;
11219 }
11220
11221 queue_init(&pmso->calloutQueue);
11222 pmso->parent = parent_arg;
11223 pmso->func = handler_arg;
11224 pmso->target = target_arg;
11225 pmso->refcon = refcon_arg;
11226 pmso->settingCount = settingCount;
11227
11228 pmso->retain(); // handle holds a retain on pmso
11229 pmsh->pmso = pmso;
11230 pmso->pmsh = pmsh;
11231
11232 pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
11233 if (pmso->publishedFeatureID) {
11234 for (unsigned int i = 0; i < settingCount; i++) {
11235 // Since there is now at least one listener to this setting, publish
11236 // PM root domain support for it.
11237 parent_arg->publishPMSetting( settings[i],
11238 supportedPowerSources, &pmso->publishedFeatureID[i] );
11239 }
11240 }
11241
11242 *handle_obj = pmsh;
11243 return pmso;
11244
11245 fail:
11246 if (pmso) {
11247 pmso->release();
11248 }
11249 if (pmsh) {
11250 pmsh->release();
11251 }
11252 return NULL;
11253 }
11254
11255 void
11256 PMSettingObject::free( void )
11257 {
11258 if (publishedFeatureID) {
11259 for (const auto& featureID : publishedFeatureID) {
11260 if (featureID) {
11261 parent->removePublishedFeature( featureID );
11262 }
11263 }
11264
11265 publishedFeatureID = {};
11266 }
11267
11268 super::free();
11269 }
11270
11271 IOReturn
11272 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
11273 {
11274 return (*func)(target, type, object, refcon);
11275 }
11276
11277 void
11278 PMSettingObject::clientHandleFreed( void )
11279 {
11280 parent->deregisterPMSettingObject(this);
11281 }
11282
11283 // MARK: -
11284 // MARK: PMAssertionsTracker
11285
11286 //*********************************************************************************
11287 //*********************************************************************************
11288 //*********************************************************************************
11289 // class PMAssertionsTracker Implementation
11290
11291 #define kAssertUniqueIDStart 500
11292
11293 PMAssertionsTracker *
11294 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
11295 {
11296 PMAssertionsTracker *me;
11297
11298 me = new PMAssertionsTracker;
11299 if (!me || !me->init()) {
11300 if (me) {
11301 me->release();
11302 }
11303 return NULL;
11304 }
11305
11306 me->owner = rootDomain;
11307 me->issuingUniqueID = kAssertUniqueIDStart;
11308 me->assertionsArray = OSArray::withCapacity(5);
11309 me->assertionsKernel = 0;
11310 me->assertionsUser = 0;
11311 me->assertionsCombined = 0;
11312 me->assertionsArrayLock = IOLockAlloc();
11313 me->tabulateProducerCount = me->tabulateConsumerCount = 0;
11314
11315 assert(me->assertionsArray);
11316 assert(me->assertionsArrayLock);
11317
11318 return me;
11319 }
11320
11321 /* tabulate
11322 * - Update assertionsKernel to reflect the state of all
11323 * assertions in the kernel.
11324 * - Update assertionsCombined to reflect both kernel & user space.
11325 */
11326 void
11327 PMAssertionsTracker::tabulate(void)
11328 {
11329 int i;
11330 int count;
11331 const PMAssertStruct *_a = nullptr;
11332 OSValueObject<PMAssertStruct> *_d = nullptr;
11333
11334 IOPMDriverAssertionType oldKernel = assertionsKernel;
11335 IOPMDriverAssertionType oldCombined = assertionsCombined;
11336
11337 ASSERT_GATED();
11338
11339 assertionsKernel = 0;
11340 assertionsCombined = 0;
11341
11342 if (!assertionsArray) {
11343 return;
11344 }
11345
11346 if ((count = assertionsArray->getCount())) {
11347 for (i = 0; i < count; i++) {
11348 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11349 if (_d) {
11350 _a = _d->getBytesNoCopy();
11351 if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11352 assertionsKernel |= _a->assertionBits;
11353 }
11354 }
11355 }
11356 }
11357
11358 tabulateProducerCount++;
11359 assertionsCombined = assertionsKernel | assertionsUser;
11360
11361 if ((assertionsKernel != oldKernel) ||
11362 (assertionsCombined != oldCombined)) {
11363 owner->evaluateAssertions(assertionsCombined, oldCombined);
11364 }
11365 }
11366
11367 void
11368 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11369 {
11370 AbsoluteTime now;
11371 uint64_t nsec;
11372
11373 if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11374 (assertStruct->assertCPUStartTime == 0)) {
11375 return;
11376 }
11377
11378 now = mach_absolute_time();
11379 SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11380 absolutetime_to_nanoseconds(now, &nsec);
11381 assertStruct->assertCPUDuration += nsec;
11382 assertStruct->assertCPUStartTime = 0;
11383
11384 if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11385 maxAssertCPUDuration = assertStruct->assertCPUDuration;
11386 maxAssertCPUEntryId = assertStruct->registryEntryID;
11387 }
11388 }
11389
11390 void
11391 PMAssertionsTracker::reportCPUBitAccounting( void )
11392 {
11393 const PMAssertStruct *_a = nullptr;
11394 OSValueObject<PMAssertStruct> *_d = nullptr;
11395 int i, count;
11396 AbsoluteTime now;
11397 uint64_t nsec;
11398
11399 ASSERT_GATED();
11400
11401 // Account for drivers that are still holding the CPU assertion
11402 if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11403 now = mach_absolute_time();
11404 if ((count = assertionsArray->getCount())) {
11405 for (i = 0; i < count; i++) {
11406 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11407 if (_d) {
11408 _a = _d->getBytesNoCopy();
11409 if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11410 (_a->level == kIOPMDriverAssertionLevelOn) &&
11411 (_a->assertCPUStartTime != 0)) {
11412 // Don't modify PMAssertStruct, leave that
11413 // for updateCPUBitAccounting()
11414 SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11415 absolutetime_to_nanoseconds(now, &nsec);
11416 nsec += _a->assertCPUDuration;
11417 if (nsec > maxAssertCPUDuration) {
11418 maxAssertCPUDuration = nsec;
11419 maxAssertCPUEntryId = _a->registryEntryID;
11420 }
11421 }
11422 }
11423 }
11424 }
11425 }
11426
11427 if (maxAssertCPUDuration) {
11428 DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11429 (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11430 }
11431
11432 maxAssertCPUDuration = 0;
11433 maxAssertCPUEntryId = 0;
11434 }
11435
11436 void
11437 PMAssertionsTracker::publishProperties( void )
11438 {
11439 OSSharedPtr<OSArray> assertionsSummary;
11440
11441 if (tabulateConsumerCount != tabulateProducerCount) {
11442 IOLockLock(assertionsArrayLock);
11443
11444 tabulateConsumerCount = tabulateProducerCount;
11445
11446 /* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11447 */
11448 assertionsSummary = copyAssertionsArray();
11449 if (assertionsSummary) {
11450 owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11451 } else {
11452 owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11453 }
11454
11455 /* Publish the IOPMrootDomain property "DriverPMAssertions"
11456 */
11457 owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11458
11459 IOLockUnlock(assertionsArrayLock);
11460 }
11461 }
11462
11463 PMAssertStruct *
11464 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11465 {
11466 PMAssertStruct *_a = NULL;
11467 OSValueObject<PMAssertStruct> *_d = nullptr;
11468 int found = -1;
11469 int count = 0;
11470 int i = 0;
11471
11472 if (assertionsArray
11473 && (count = assertionsArray->getCount())) {
11474 for (i = 0; i < count; i++) {
11475 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11476 if (_d) {
11477 _a = _d->getMutableBytesNoCopy();
11478 if (_a && (_id == _a->id)) {
11479 found = i;
11480 break;
11481 }
11482 }
11483 }
11484 }
11485
11486 if (-1 == found) {
11487 return NULL;
11488 } else {
11489 if (index) {
11490 *index = found;
11491 }
11492 return _a;
11493 }
11494 }
11495
11496 /* PMAssertionsTracker::handleCreateAssertion
11497 * Perform assertion work on the PM workloop. Do not call directly.
11498 */
11499 IOReturn
11500 PMAssertionsTracker::handleCreateAssertion(OSValueObject<PMAssertStruct> *newAssertion)
11501 {
11502 PMAssertStruct *assertStruct = nullptr;
11503
11504 ASSERT_GATED();
11505
11506 if (newAssertion) {
11507 IOLockLock(assertionsArrayLock);
11508 assertStruct = newAssertion->getMutableBytesNoCopy();
11509 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11510 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11511 assertStruct->assertCPUStartTime = mach_absolute_time();
11512 }
11513 assertionsArray->setObject(newAssertion);
11514 IOLockUnlock(assertionsArrayLock);
11515 newAssertion->release();
11516
11517 tabulate();
11518 }
11519 return kIOReturnSuccess;
11520 }
11521
11522 /* PMAssertionsTracker::createAssertion
11523 * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11524 * appropiate.
11525 */
11526 IOReturn
11527 PMAssertionsTracker::createAssertion(
11528 IOPMDriverAssertionType which,
11529 IOPMDriverAssertionLevel level,
11530 IOService *serviceID,
11531 const char *whoItIs,
11532 IOPMDriverAssertionID *outID)
11533 {
11534 OSSharedPtr<OSValueObject<PMAssertStruct> > dataStore;
11535 PMAssertStruct track;
11536
11537 // Warning: trillions and trillions of created assertions may overflow the unique ID.
11538 track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11539 track.level = level;
11540 track.assertionBits = which;
11541
11542 // NB: ownerString is explicitly managed by PMAssertStruct
11543 // it will be released in `handleReleaseAssertion' below
11544 track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11545 track.ownerService = serviceID;
11546 track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11547 track.modifiedTime = 0;
11548 pmEventTimeStamp(&track.createdTime);
11549 track.assertCPUStartTime = 0;
11550 track.assertCPUDuration = 0;
11551
11552 dataStore = OSValueObjectWithValue(track);
11553 if (!dataStore) {
11554 if (track.ownerString) {
11555 track.ownerString->release();
11556 track.ownerString = NULL;
11557 }
11558 return kIOReturnNoMemory;
11559 }
11560
11561 *outID = track.id;
11562
11563 if (owner && owner->pmPowerStateQueue) {
11564 // queue action is responsible for releasing dataStore
11565 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11566 }
11567
11568 return kIOReturnSuccess;
11569 }
11570
11571 /* PMAssertionsTracker::handleReleaseAssertion
11572 * Runs in PM workloop. Do not call directly.
11573 */
11574 IOReturn
11575 PMAssertionsTracker::handleReleaseAssertion(
11576 IOPMDriverAssertionID _id)
11577 {
11578 ASSERT_GATED();
11579
11580 int index;
11581 PMAssertStruct *assertStruct = detailsForID(_id, &index);
11582
11583 if (!assertStruct) {
11584 return kIOReturnNotFound;
11585 }
11586
11587 IOLockLock(assertionsArrayLock);
11588
11589 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11590 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11591 updateCPUBitAccounting(assertStruct);
11592 }
11593
11594 if (assertStruct->ownerString) {
11595 assertStruct->ownerString->release();
11596 assertStruct->ownerString = NULL;
11597 }
11598
11599 assertionsArray->removeObject(index);
11600 IOLockUnlock(assertionsArrayLock);
11601
11602 tabulate();
11603 return kIOReturnSuccess;
11604 }
11605
11606 /* PMAssertionsTracker::releaseAssertion
11607 * Releases an assertion and affects system behavior if appropiate.
11608 * Actual work happens on PM workloop.
11609 */
11610 IOReturn
11611 PMAssertionsTracker::releaseAssertion(
11612 IOPMDriverAssertionID _id)
11613 {
11614 if (owner && owner->pmPowerStateQueue) {
11615 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11616 }
11617 return kIOReturnSuccess;
11618 }
11619
11620 /* PMAssertionsTracker::handleSetAssertionLevel
11621 * Runs in PM workloop. Do not call directly.
11622 */
11623 IOReturn
11624 PMAssertionsTracker::handleSetAssertionLevel(
11625 IOPMDriverAssertionID _id,
11626 IOPMDriverAssertionLevel _level)
11627 {
11628 PMAssertStruct *assertStruct = detailsForID(_id, NULL);
11629
11630 ASSERT_GATED();
11631
11632 if (!assertStruct) {
11633 return kIOReturnNotFound;
11634 }
11635
11636 IOLockLock(assertionsArrayLock);
11637 pmEventTimeStamp(&assertStruct->modifiedTime);
11638 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11639 (assertStruct->level != _level)) {
11640 if (_level == kIOPMDriverAssertionLevelOn) {
11641 assertStruct->assertCPUStartTime = mach_absolute_time();
11642 } else {
11643 updateCPUBitAccounting(assertStruct);
11644 }
11645 }
11646 assertStruct->level = _level;
11647 IOLockUnlock(assertionsArrayLock);
11648
11649 tabulate();
11650 return kIOReturnSuccess;
11651 }
11652
11653 /* PMAssertionsTracker::setAssertionLevel
11654 */
11655 IOReturn
11656 PMAssertionsTracker::setAssertionLevel(
11657 IOPMDriverAssertionID _id,
11658 IOPMDriverAssertionLevel _level)
11659 {
11660 if (owner && owner->pmPowerStateQueue) {
11661 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11662 (void *)(uintptr_t)_level, _id);
11663 }
11664
11665 return kIOReturnSuccess;
11666 }
11667
11668 IOReturn
11669 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11670 {
11671 IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11672
11673 ASSERT_GATED();
11674
11675 if (new_user_levels != assertionsUser) {
11676 DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11677 assertionsUser = new_user_levels;
11678 }
11679
11680 tabulate();
11681 return kIOReturnSuccess;
11682 }
11683
11684 IOReturn
11685 PMAssertionsTracker::setUserAssertionLevels(
11686 IOPMDriverAssertionType new_user_levels)
11687 {
11688 if (gIOPMWorkLoop) {
11689 gIOPMWorkLoop->runAction(
11690 OSMemberFunctionCast(
11691 IOWorkLoop::Action,
11692 this,
11693 &PMAssertionsTracker::handleSetUserAssertionLevels),
11694 this,
11695 (void *) &new_user_levels, NULL, NULL, NULL);
11696 }
11697
11698 return kIOReturnSuccess;
11699 }
11700
11701
11702 OSSharedPtr<OSArray>
11703 PMAssertionsTracker::copyAssertionsArray(void)
11704 {
11705 int count;
11706 int i;
11707 OSSharedPtr<OSArray> outArray = NULL;
11708
11709 if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11710 goto exit;
11711 }
11712 outArray = OSArray::withCapacity(count);
11713 if (!outArray) {
11714 goto exit;
11715 }
11716
11717 for (i = 0; i < count; i++) {
11718 const PMAssertStruct *_a = nullptr;
11719 OSValueObject<PMAssertStruct> *_d = nullptr;
11720 OSSharedPtr<OSDictionary> details;
11721
11722 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11723 if (_d && (_a = _d->getBytesNoCopy())) {
11724 OSSharedPtr<OSNumber> _n;
11725
11726 details = OSDictionary::withCapacity(7);
11727 if (!details) {
11728 continue;
11729 }
11730
11731 outArray->setObject(details.get());
11732
11733 _n = OSNumber::withNumber(_a->id, 64);
11734 if (_n) {
11735 details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11736 }
11737 _n = OSNumber::withNumber(_a->createdTime, 64);
11738 if (_n) {
11739 details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11740 }
11741 _n = OSNumber::withNumber(_a->modifiedTime, 64);
11742 if (_n) {
11743 details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11744 }
11745 _n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11746 if (_n) {
11747 details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11748 }
11749 _n = OSNumber::withNumber(_a->level, 64);
11750 if (_n) {
11751 details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11752 }
11753 _n = OSNumber::withNumber(_a->assertionBits, 64);
11754 if (_n) {
11755 details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11756 }
11757
11758 if (_a->ownerString) {
11759 details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11760 }
11761 }
11762 }
11763
11764 exit:
11765 return os::move(outArray);
11766 }
11767
11768 IOPMDriverAssertionType
11769 PMAssertionsTracker::getActivatedAssertions(void)
11770 {
11771 return assertionsCombined;
11772 }
11773
11774 IOPMDriverAssertionLevel
11775 PMAssertionsTracker::getAssertionLevel(
11776 IOPMDriverAssertionType type)
11777 {
11778 // FIXME: unused and also wrong
11779 if (type && ((type & assertionsKernel) == assertionsKernel)) {
11780 return kIOPMDriverAssertionLevelOn;
11781 } else {
11782 return kIOPMDriverAssertionLevelOff;
11783 }
11784 }
11785
11786 //*********************************************************************************
11787 //*********************************************************************************
11788 //*********************************************************************************
11789
11790
11791 static void
11792 pmEventTimeStamp(uint64_t *recordTS)
11793 {
11794 clock_sec_t tsec;
11795 clock_usec_t tusec;
11796
11797 if (!recordTS) {
11798 return;
11799 }
11800
11801 // We assume tsec fits into 32 bits; 32 bits holds enough
11802 // seconds for 136 years since the epoch in 1970.
11803 clock_get_calendar_microtime(&tsec, &tusec);
11804
11805
11806 // Pack the sec & microsec calendar time into a uint64_t, for fun.
11807 *recordTS = 0;
11808 *recordTS |= (uint32_t)tusec;
11809 *recordTS |= ((uint64_t)tsec << 32);
11810
11811 return;
11812 }
11813
11814 // MARK: -
11815 // MARK: IORootParent
11816
11817 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11818
11819 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11820
11821 // The reason that root domain needs a root parent is to facilitate demand
11822 // sleep, since a power change from the root parent cannot be vetoed.
11823 //
11824 // The above statement is no longer true since root domain now performs
11825 // demand sleep using overrides. But root parent remains to avoid changing
11826 // the power tree stacking. Root parent is parked at the max power state.
11827
11828
11829 static IOPMPowerState patriarchPowerStates[2] =
11830 {
11831 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11832 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11833 };
11834
11835 void
11836 IORootParent::initialize( void )
11837 {
11838
11839 gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11840 gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11841 gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11842 gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11843 gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11844 gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11845 gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11846 gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11847 gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11848 gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11849 gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11850 gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11851 gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11852 gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11853 gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11854 gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11855 gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11856 gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11857 gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11858 gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11859 gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11860 gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11861 gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11862 gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11863 gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11864 gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11865 gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11866 gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11867 gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11868 gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11869 gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11870 gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11871 gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11872 gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11873 gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11874 gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11875 gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11876 gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11877 gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11878 gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11879 gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11880 gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11881 gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11882 gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11883 }
11884
11885 bool
11886 IORootParent::start( IOService * nub )
11887 {
11888 IOService::start(nub);
11889 attachToParent( getRegistryRoot(), gIOPowerPlane );
11890 PMinit();
11891 registerPowerDriver(this, patriarchPowerStates, 2);
11892 makeUsable();
11893 return true;
11894 }
11895
11896 void
11897 IORootParent::shutDownSystem( void )
11898 {
11899 }
11900
11901 void
11902 IORootParent::restartSystem( void )
11903 {
11904 }
11905
11906 void
11907 IORootParent::sleepSystem( void )
11908 {
11909 }
11910
11911 void
11912 IORootParent::dozeSystem( void )
11913 {
11914 }
11915
11916 void
11917 IORootParent::sleepToDoze( void )
11918 {
11919 }
11920
11921 void
11922 IORootParent::wakeSystem( void )
11923 {
11924 }
11925
11926 OSSharedPtr<OSObject>
11927 IORootParent::copyProperty( const char * aKey) const
11928 {
11929 return IOService::copyProperty(aKey);
11930 }
11931
11932 uint32_t
11933 IOPMrootDomain::getWatchdogTimeout()
11934 {
11935 if (gSwdSleepWakeTimeout) {
11936 gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11937 }
11938 if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11939 (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11940 return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11941 } else {
11942 return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11943 }
11944 }
11945
11946
11947 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
11948 IOReturn
11949 IOPMrootDomain::restartWithStackshot()
11950 {
11951 takeStackshot(true);
11952
11953 return kIOReturnSuccess;
11954 }
11955
11956 void
11957 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
11958 {
11959 takeStackshot(wdogTrigger);
11960 }
11961
11962 void
11963 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
11964 {
11965 switch (tracePhase) {
11966 case kIOPMTracePointSleepStarted:
11967 *phaseString = "kIOPMTracePointSleepStarted";
11968 *description = "starting sleep";
11969 break;
11970
11971 case kIOPMTracePointSleepApplications:
11972 *phaseString = "kIOPMTracePointSleepApplications";
11973 *description = "notifying applications";
11974 break;
11975
11976 case kIOPMTracePointSleepPriorityClients:
11977 *phaseString = "kIOPMTracePointSleepPriorityClients";
11978 *description = "notifying clients about upcoming system capability changes";
11979 break;
11980
11981 case kIOPMTracePointSleepWillChangeInterests:
11982 *phaseString = "kIOPMTracePointSleepWillChangeInterests";
11983 *description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
11984 break;
11985
11986 case kIOPMTracePointSleepPowerPlaneDrivers:
11987 *phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
11988 *description = "calling power state change callbacks";
11989 break;
11990
11991 case kIOPMTracePointSleepDidChangeInterests:
11992 *phaseString = "kIOPMTracePointSleepDidChangeInterests";
11993 *description = "calling rootDomain's clients about rootDomain's state changes";
11994 break;
11995
11996 case kIOPMTracePointSleepCapabilityClients:
11997 *phaseString = "kIOPMTracePointSleepCapabilityClients";
11998 *description = "notifying clients about current system capabilities";
11999 break;
12000
12001 case kIOPMTracePointSleepPlatformActions:
12002 *phaseString = "kIOPMTracePointSleepPlatformActions";
12003 *description = "calling Quiesce/Sleep action callbacks";
12004 break;
12005
12006 case kIOPMTracePointSleepCPUs:
12007 {
12008 *phaseString = "kIOPMTracePointSleepCPUs";
12009 #if defined(__i386__) || defined(__x86_64__)
12010 /*
12011 * We cannot use the getCPUNumber() method to get the cpu number, since
12012 * that cpu number is unrelated to the cpu number we need (we need the cpu
12013 * number as enumerated by the scheduler, NOT the CPU number enumerated
12014 * by ACPIPlatform as the CPUs are enumerated in MADT order).
12015 * Instead, pass the Mach processor pointer associated with the current
12016 * shutdown target so its associated cpu_id can be used in
12017 * processor_to_datastring.
12018 */
12019 if (currentShutdownTarget != NULL &&
12020 currentShutdownTarget->getMachProcessor() != NULL) {
12021 const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
12022 currentShutdownTarget->getMachProcessor());
12023 *description = sbuf;
12024 } else {
12025 *description = "halting all non-boot CPUs";
12026 }
12027 #else
12028 *description = "halting all non-boot CPUs";
12029 #endif
12030 break;
12031 }
12032 case kIOPMTracePointSleepPlatformDriver:
12033 *phaseString = "kIOPMTracePointSleepPlatformDriver";
12034 *description = "executing platform specific code";
12035 break;
12036
12037 case kIOPMTracePointHibernate:
12038 *phaseString = "kIOPMTracePointHibernate";
12039 *description = "writing the hibernation image";
12040 break;
12041
12042 case kIOPMTracePointSystemSleep:
12043 *phaseString = "kIOPMTracePointSystemSleep";
12044 *description = "in EFI/Bootrom after last point of entry to sleep";
12045 break;
12046
12047 case kIOPMTracePointWakePlatformDriver:
12048 *phaseString = "kIOPMTracePointWakePlatformDriver";
12049 *description = "executing platform specific code";
12050 break;
12051
12052
12053 case kIOPMTracePointWakePlatformActions:
12054 *phaseString = "kIOPMTracePointWakePlatformActions";
12055 *description = "calling Wake action callbacks";
12056 break;
12057
12058 case kIOPMTracePointWakeCPUs:
12059 *phaseString = "kIOPMTracePointWakeCPUs";
12060 *description = "starting non-boot CPUs";
12061 break;
12062
12063 case kIOPMTracePointWakeWillPowerOnClients:
12064 *phaseString = "kIOPMTracePointWakeWillPowerOnClients";
12065 *description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
12066 break;
12067
12068 case kIOPMTracePointWakeWillChangeInterests:
12069 *phaseString = "kIOPMTracePointWakeWillChangeInterests";
12070 *description = "calling rootDomain's clients about upcoming rootDomain's state changes";
12071 break;
12072
12073 case kIOPMTracePointWakeDidChangeInterests:
12074 *phaseString = "kIOPMTracePointWakeDidChangeInterests";
12075 *description = "calling rootDomain's clients about completed rootDomain's state changes";
12076 break;
12077
12078 case kIOPMTracePointWakePowerPlaneDrivers:
12079 *phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
12080 *description = "calling power state change callbacks";
12081 break;
12082
12083 case kIOPMTracePointWakeCapabilityClients:
12084 *phaseString = "kIOPMTracePointWakeCapabilityClients";
12085 *description = "informing clients about current system capabilities";
12086 break;
12087
12088 case kIOPMTracePointWakeApplications:
12089 *phaseString = "kIOPMTracePointWakeApplications";
12090 *description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
12091 break;
12092
12093 case kIOPMTracePointDarkWakeEntry:
12094 *phaseString = "kIOPMTracePointDarkWakeEntry";
12095 *description = "entering darkwake on way to sleep";
12096 break;
12097
12098 case kIOPMTracePointDarkWakeExit:
12099 *phaseString = "kIOPMTracePointDarkWakeExit";
12100 *description = "entering fullwake from darkwake";
12101 break;
12102
12103 default:
12104 *phaseString = NULL;
12105 *description = NULL;
12106 }
12107 }
12108
12109 void
12110 IOPMrootDomain::saveFailureData2File()
12111 {
12112 unsigned int len = 0;
12113 char failureStr[512];
12114 errno_t error;
12115 char *outbuf;
12116 OSNumber *statusCode;
12117 uint64_t pmStatusCode = 0;
12118 uint32_t phaseData = 0;
12119 uint32_t phaseDetail = 0;
12120 bool efiFailure = false;
12121
12122 OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
12123 statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
12124 if (statusCode) {
12125 pmStatusCode = statusCode->unsigned64BitValue();
12126 phaseData = pmStatusCode & 0xFFFFFFFF;
12127 phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
12128 if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
12129 LOG("Sleep Wake failure in EFI\n");
12130 efiFailure = true;
12131 failureStr[0] = 0;
12132 snprintf(failureStr, sizeof(failureStr), "Sleep Wake failure in EFI\n\nFailure code:: 0x%08x 0x%08x\n\nPlease IGNORE the below stackshot\n", phaseDetail, phaseData);
12133 len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
12134 }
12135 }
12136
12137 if (!efiFailure) {
12138 if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
12139 swd_flags |= SWD_BOOT_BY_SW_WDOG;
12140 PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
12141 // dump panic will handle saving nvram data
12142 return;
12143 }
12144
12145 /* Keeping this around for capturing data during power
12146 * button press */
12147
12148 if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
12149 DLOG("No sleep wake failure string\n");
12150 return;
12151 }
12152 if (len == 0) {
12153 DLOG("Ignoring zero byte SleepWake failure string\n");
12154 goto exit;
12155 }
12156
12157 // if PMStatus code is zero, delete stackshot and return
12158 if (statusCode) {
12159 if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
12160 // there was no sleep wake failure
12161 // this can happen if delete stackshot was called
12162 // before take stackshot completed. Let us delete any
12163 // sleep wake failure data in nvram
12164 DLOG("Deleting stackshot on successful wake\n");
12165 deleteStackshot();
12166 return;
12167 }
12168 }
12169
12170 if (len > sizeof(failureStr)) {
12171 len = sizeof(failureStr);
12172 }
12173 failureStr[0] = 0;
12174 PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
12175 }
12176 if (failureStr[0] != 0) {
12177 error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
12178 if (error) {
12179 DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
12180 } else {
12181 DLOG("Saved SleepWake failure string to file.\n");
12182 }
12183 }
12184
12185 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12186 goto exit;
12187 }
12188
12189 if (swd_buffer) {
12190 unsigned int len = 0;
12191 errno_t error;
12192 char nvram_var_name_buffer[20];
12193 unsigned int concat_len = 0;
12194 swd_hdr *hdr = NULL;
12195
12196
12197 hdr = (swd_hdr *)swd_buffer;
12198 outbuf = (char *)hdr + hdr->spindump_offset;
12199 OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
12200
12201 for (int i = 0; i < 8; i++) {
12202 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12203 if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
12204 LOG("No SleepWake blob to read beyond chunk %d\n", i);
12205 break;
12206 }
12207 if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
12208 PERemoveNVRAMProperty(nvram_var_name_buffer);
12209 LOG("Could not read the property :-(\n");
12210 break;
12211 }
12212 PERemoveNVRAMProperty(nvram_var_name_buffer);
12213 concat_len += len;
12214 }
12215 LOG("Concatenated length for the SWD blob %d\n", concat_len);
12216
12217 if (concat_len) {
12218 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
12219 if (error) {
12220 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
12221 } else {
12222 LOG("Saved SleepWake zipped data to file.\n");
12223 }
12224 } else {
12225 // There is a sleep wake failure string but no stackshot
12226 // Write a placeholder stacks file so that swd runs
12227 snprintf(outbuf, 20, "%s", "No stackshot data\n");
12228 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
12229 if (error) {
12230 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
12231 } else {
12232 LOG("Saved SleepWake zipped data to file.\n");
12233 }
12234 }
12235 } else {
12236 LOG("No buffer allocated to save failure stackshot\n");
12237 }
12238
12239
12240 gRootDomain->swd_lock = 0;
12241 exit:
12242 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12243 return;
12244 }
12245
12246
12247 void
12248 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
12249 {
12250 OSSharedPtr<IORegistryIterator> iter;
12251 OSSharedPtr<const OSSymbol> kextName = NULL;
12252 IORegistryEntry * entry;
12253 IOService * node;
12254 bool nodeFound = false;
12255
12256 const void * callMethod = NULL;
12257 const char * objectName = NULL;
12258 uint32_t timeout = getWatchdogTimeout();
12259 const char * phaseString = NULL;
12260 const char * phaseDescription = NULL;
12261
12262 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
12263 uint32_t tracePhase = pmTracer->getTracePhase();
12264
12265 *thread = NULL;
12266 if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
12267 snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
12268 } else {
12269 snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
12270 }
12271 tracePhase2String(tracePhase, &phaseString, &phaseDescription);
12272
12273 if (notifierThread) {
12274 if (notifier && (notifier->identifier)) {
12275 objectName = notifier->identifier->getCStringNoCopy();
12276 }
12277 *thread = notifierThread;
12278 } else {
12279 iter = IORegistryIterator::iterateOver(
12280 getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
12281
12282 if (iter) {
12283 while ((entry = iter->getNextObject())) {
12284 node = OSDynamicCast(IOService, entry);
12285 if (!node) {
12286 continue;
12287 }
12288 if (OSDynamicCast(IOPowerConnection, node)) {
12289 continue;
12290 }
12291
12292 if (node->getBlockingDriverCall(thread, &callMethod)) {
12293 nodeFound = true;
12294 break;
12295 }
12296 }
12297 }
12298 if (nodeFound) {
12299 kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
12300 if (kextName) {
12301 objectName = kextName->getCStringNoCopy();
12302 }
12303 }
12304 }
12305 if (phaseDescription) {
12306 strlcat(failureStr, " while ", strLen);
12307 strlcat(failureStr, phaseDescription, strLen);
12308 strlcat(failureStr, ".", strLen);
12309 }
12310 if (objectName) {
12311 strlcat(failureStr, " Suspected bundle: ", strLen);
12312 strlcat(failureStr, objectName, strLen);
12313 strlcat(failureStr, ".", strLen);
12314 }
12315 if (*thread) {
12316 char threadName[40];
12317 snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
12318 strlcat(failureStr, threadName, strLen);
12319 }
12320
12321 DLOG("%s\n", failureStr);
12322 }
12323
12324 struct swd_stackshot_compressed_data {
12325 z_output_func zoutput;
12326 size_t zipped;
12327 uint64_t totalbytes;
12328 uint64_t lastpercent;
12329 IOReturn error;
12330 unsigned outremain;
12331 unsigned outlen;
12332 unsigned writes;
12333 Bytef * outbuf;
12334 };
12335 struct swd_stackshot_compressed_data swd_zip_var = { };
12336
12337 static void *
12338 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
12339 {
12340 void *result;
12341 LOG("Alloc in zipping %d items of size %d\n", items, size);
12342
12343 result = (void *)(swd_zs_zmem + swd_zs_zoffset);
12344 swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
12345 LOG("Offset %zu\n", swd_zs_zoffset);
12346 return result;
12347 }
12348
12349 static int
12350 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12351 {
12352 unsigned len;
12353
12354 len = strm->avail_in;
12355
12356 if (len > size) {
12357 len = size;
12358 }
12359 if (len == 0) {
12360 return 0;
12361 }
12362
12363 if (strm->next_in != (Bytef *) strm) {
12364 memcpy(buf, strm->next_in, len);
12365 } else {
12366 bzero(buf, len);
12367 }
12368
12369 strm->adler = z_crc32(strm->adler, buf, len);
12370
12371 strm->avail_in -= len;
12372 strm->next_in += len;
12373 strm->total_in += len;
12374
12375 return (int)len;
12376 }
12377
12378 static int
12379 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12380 {
12381 unsigned int i = 0;
12382 // if outlen > max size don't add to the buffer
12383 assert(buf != NULL);
12384 if (strm && buf) {
12385 if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12386 LOG("No space to GZIP... not writing to NVRAM\n");
12387 return len;
12388 }
12389 }
12390 for (i = 0; i < len; i++) {
12391 *(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12392 }
12393 swd_zip_var.outlen += len;
12394 return len;
12395 }
12396
12397 static void
12398 swd_zs_free(void * __unused ref, void * __unused ptr)
12399 {
12400 }
12401
12402 static int
12403 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12404 {
12405 int wbits = 12;
12406 int memlevel = 3;
12407
12408 if (((unsigned int) numBytes) != numBytes) {
12409 return 0;
12410 }
12411
12412 if (!swd_zs.zalloc) {
12413 swd_zs.zalloc = swd_zs_alloc;
12414 swd_zs.zfree = swd_zs_free;
12415 if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12416 // allocation failed
12417 bzero(&swd_zs, sizeof(swd_zs));
12418 // swd_zs_zoffset = 0;
12419 } else {
12420 LOG("PMRD inited the zlib allocation routines\n");
12421 }
12422 }
12423
12424 swd_zip_var.zipped = 0;
12425 swd_zip_var.totalbytes = 0; // should this be the max that we have?
12426 swd_zip_var.lastpercent = 0;
12427 swd_zip_var.error = kIOReturnSuccess;
12428 swd_zip_var.outremain = 0;
12429 swd_zip_var.outlen = 0;
12430 swd_zip_var.writes = 0;
12431 swd_zip_var.outbuf = (Bytef *)outPtr;
12432
12433 swd_zip_var.totalbytes = numBytes;
12434
12435 swd_zs.avail_in = 0;
12436 swd_zs.next_in = NULL;
12437 swd_zs.avail_out = 0;
12438 swd_zs.next_out = NULL;
12439
12440 deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12441
12442 z_stream *zs;
12443 int zr;
12444 zs = &swd_zs;
12445
12446 while (swd_zip_var.error >= 0) {
12447 if (!zs->avail_in) {
12448 zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12449 zs->avail_in = (unsigned int) numBytes;
12450 }
12451 if (!zs->avail_out) {
12452 zs->next_out = (Bytef *)zs;
12453 zs->avail_out = UINT32_MAX;
12454 }
12455 zr = deflate(zs, Z_NO_FLUSH);
12456 if (Z_STREAM_END == zr) {
12457 break;
12458 }
12459 if (zr != Z_OK) {
12460 LOG("ZERR %d\n", zr);
12461 swd_zip_var.error = zr;
12462 } else {
12463 if (zs->total_in == numBytes) {
12464 break;
12465 }
12466 }
12467 }
12468
12469 //now flush the stream
12470 while (swd_zip_var.error >= 0) {
12471 if (!zs->avail_out) {
12472 zs->next_out = (Bytef *)zs;
12473 zs->avail_out = UINT32_MAX;
12474 }
12475 zr = deflate(zs, Z_FINISH);
12476 if (Z_STREAM_END == zr) {
12477 break;
12478 }
12479 if (zr != Z_OK) {
12480 LOG("ZERR %d\n", zr);
12481 swd_zip_var.error = zr;
12482 } else {
12483 if (zs->total_in == numBytes) {
12484 LOG("Total output size %d\n", swd_zip_var.outlen);
12485 break;
12486 }
12487 }
12488 }
12489
12490 return swd_zip_var.outlen;
12491 }
12492
12493 void
12494 IOPMrootDomain::deleteStackshot()
12495 {
12496 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12497 // takeStackshot hasn't completed
12498 return;
12499 }
12500 LOG("Deleting any sleepwake failure data in nvram\n");
12501
12502 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12503 char nvram_var_name_buf[20];
12504 for (int i = 0; i < 8; i++) {
12505 snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12506 if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12507 LOG("Removing %s returned false\n", nvram_var_name_buf);
12508 }
12509 }
12510 // force NVRAM sync
12511 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12512 DLOG("Failed to force nvram sync\n");
12513 }
12514 gRootDomain->swd_lock = 0;
12515 }
12516
12517 void
12518 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12519 {
12520 swd_hdr * hdr = NULL;
12521 int cnt = 0;
12522 int max_cnt;
12523 pid_t pid = 0;
12524 kern_return_t kr = KERN_SUCCESS;
12525 uint64_t flags;
12526
12527 char * dstAddr;
12528 uint32_t size;
12529 uint32_t bytesRemaining;
12530 unsigned bytesWritten = 0;
12531
12532 char failureStr[512];
12533 thread_t thread = NULL;
12534 const char * swfPanic = "swfPanic";
12535
12536 uint32_t bufSize;
12537 int success = 0;
12538
12539 #if defined(__i386__) || defined(__x86_64__)
12540 const bool concise = false;
12541 #else
12542 const bool concise = true;
12543 #endif
12544
12545 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12546 return;
12547 }
12548
12549 failureStr[0] = 0;
12550 if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12551 return;
12552 }
12553
12554 if (wdogTrigger) {
12555 getFailureData(&thread, failureStr, sizeof(failureStr));
12556
12557 if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12558 goto skip_stackshot;
12559 }
12560 } else {
12561 AbsoluteTime now;
12562 uint64_t nsec;
12563 clock_get_uptime(&now);
12564 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12565 absolutetime_to_nanoseconds(now, &nsec);
12566 snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12567 }
12568
12569 if (swd_buffer == NULL) {
12570 sleepWakeDebugMemAlloc();
12571 if (swd_buffer == NULL) {
12572 return;
12573 }
12574 }
12575 hdr = (swd_hdr *)swd_buffer;
12576 bufSize = hdr->alloc_size;
12577
12578 dstAddr = (char*)hdr + hdr->spindump_offset;
12579 flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_NO_IO_STATS | STACKSHOT_SAVE_KEXT_LOADINFO | STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY | STACKSHOT_THREAD_WAITINFO | STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12580
12581 /* If not wdogTrigger only take kernel tasks stackshot
12582 */
12583 if (wdogTrigger) {
12584 pid = -1;
12585 max_cnt = 3;
12586 } else {
12587 pid = 0;
12588 max_cnt = 2;
12589 }
12590
12591 /* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12592 * If we run out of space, take stackshot with only kernel task
12593 */
12594 while (success == 0 && cnt < max_cnt) {
12595 bytesRemaining = bufSize - hdr->spindump_offset;
12596 cnt++;
12597 DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12598
12599 size = bytesRemaining;
12600 kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12601 DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12602 kr, pid, size, flags, bytesWritten);
12603 if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12604 if (pid == -1) {
12605 pid = 0;
12606 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12607 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12608 } else {
12609 LOG("Insufficient buffer size for only kernel task\n");
12610 break;
12611 }
12612 }
12613 if (kr == KERN_SUCCESS) {
12614 if (bytesWritten == 0) {
12615 MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12616 continue;
12617 }
12618 bytesRemaining -= bytesWritten;
12619 hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12620
12621 memset(hdr->reason, 0x20, sizeof(hdr->reason));
12622
12623 // Compress stackshot and save to NVRAM
12624 {
12625 char *outbuf = (char *)swd_compressed_buffer;
12626 int outlen = 0;
12627 int num_chunks = 0;
12628 int max_chunks = 0;
12629 int leftover = 0;
12630 char nvram_var_name_buffer[20];
12631
12632 outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12633
12634 if (outlen) {
12635 max_chunks = outlen / (2096 - 200);
12636 leftover = outlen % (2096 - 200);
12637
12638 if (max_chunks < 8) {
12639 for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12640 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12641 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12642 LOG("Failed to update NVRAM %d\n", num_chunks);
12643 break;
12644 }
12645 }
12646 if (leftover) {
12647 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12648 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12649 LOG("Failed to update NVRAM with leftovers\n");
12650 }
12651 }
12652 success = 1;
12653 LOG("Successfully saved stackshot to NVRAM\n");
12654 } else {
12655 if (pid == -1) {
12656 LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12657 pid = 0;
12658 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12659 LOG("Compressed failure stackshot of kernel+dexts is too large size=%d bytes\n", outlen);
12660 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12661 } else {
12662 LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12663 break;
12664 }
12665 }
12666 }
12667 }
12668 }
12669 }
12670
12671 if (failureStr[0]) {
12672 // append sleep-wake failure code
12673 char traceCode[80];
12674 snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12675 pmTracer->getTraceData(), pmTracer->getTracePhase());
12676 strlcat(failureStr, traceCode, sizeof(failureStr));
12677 if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12678 DLOG("Failed to write SleepWake failure string\n");
12679 }
12680 }
12681
12682 // force NVRAM sync
12683 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12684 DLOG("Failed to force nvram sync\n");
12685 }
12686
12687 skip_stackshot:
12688 if (wdogTrigger) {
12689 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12690 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12691 // If current boot is due to this watch dog trigger restart in previous boot,
12692 // then don't trigger again until at least 1 successful sleep & wake.
12693 if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12694 LOG("Shutting down due to repeated Sleep/Wake failures\n");
12695 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12696 PEHaltRestart(kPEHaltCPU);
12697 return;
12698 }
12699 }
12700 if (gSwdPanic == 0) {
12701 LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12702 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12703 PEHaltRestart(kPERestartCPU);
12704 }
12705 }
12706 if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12707 DLOG("Failed to write SleepWake failure panic key\n");
12708 }
12709 #if defined(__x86_64__)
12710 if (thread) {
12711 panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12712 } else
12713 #endif /* defined(__x86_64__) */
12714 {
12715 panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12716 }
12717 } else {
12718 gRootDomain->swd_lock = 0;
12719 return;
12720 }
12721 }
12722
12723 void
12724 IOPMrootDomain::sleepWakeDebugMemAlloc()
12725 {
12726 vm_size_t size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12727
12728 swd_hdr *hdr = NULL;
12729 void *bufPtr = NULL;
12730
12731 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12732
12733
12734 if (kIOSleepWakeWdogOff & gIOKitDebug) {
12735 return;
12736 }
12737
12738 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12739 return;
12740 }
12741
12742 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12743 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12744 size);
12745 if (memDesc == NULL) {
12746 DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12747 goto exit;
12748 }
12749
12750 bufPtr = memDesc->getBytesNoCopy();
12751
12752 // Carve out memory for zlib routines
12753 swd_zs_zmem = (vm_offset_t)bufPtr;
12754 bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12755
12756 // Carve out memory for compressed stackshots
12757 swd_compressed_buffer = bufPtr;
12758 bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12759
12760 // Remaining is used for holding stackshot
12761 hdr = (swd_hdr *)bufPtr;
12762 memset(hdr, 0, sizeof(swd_hdr));
12763
12764 hdr->signature = SWD_HDR_SIGNATURE;
12765 hdr->alloc_size = SWD_STACKSHOT_SIZE;
12766
12767 hdr->spindump_offset = sizeof(swd_hdr);
12768 swd_buffer = (void *)hdr;
12769 swd_memDesc = os::move(memDesc);
12770 DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12771
12772 exit:
12773 gRootDomain->swd_lock = 0;
12774 }
12775
12776 void
12777 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12778 {
12779 #if UNUSED
12780 vm_size_t size = SWD_SPINDUMP_SIZE;
12781
12782 swd_hdr *hdr = NULL;
12783
12784 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12785
12786 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12787 return;
12788 }
12789
12790 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12791 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12792 SWD_SPINDUMP_SIZE);
12793
12794 if (memDesc == NULL) {
12795 DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12796 goto exit;
12797 }
12798
12799
12800 hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12801 memset(hdr, 0, sizeof(swd_hdr));
12802
12803 hdr->signature = SWD_HDR_SIGNATURE;
12804 hdr->alloc_size = size;
12805
12806 hdr->spindump_offset = sizeof(swd_hdr);
12807 swd_spindump_buffer = (void *)hdr;
12808 swd_spindump_memDesc = os::move(memDesc);
12809
12810 exit:
12811 gRootDomain->swd_lock = 0;
12812 #endif /* UNUSED */
12813 }
12814
12815 void
12816 IOPMrootDomain::sleepWakeDebugEnableWdog()
12817 {
12818 }
12819
12820 bool
12821 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12822 {
12823 return !systemBooting && !systemShutdown && !gWillShutdown;
12824 }
12825
12826 void
12827 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12828 {
12829 swd_hdr *hdr = NULL;
12830 errno_t error = EIO;
12831
12832 if (swd_spindump_buffer && gSpinDumpBufferFull) {
12833 hdr = (swd_hdr *)swd_spindump_buffer;
12834
12835 error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12836 (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12837
12838 if (error) {
12839 return;
12840 }
12841
12842 sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12843 (char*)hdr + offsetof(swd_hdr, UUID),
12844 sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12845
12846 gSpinDumpBufferFull = false;
12847 }
12848 }
12849
12850 errno_t
12851 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12852 {
12853 struct vnode *vp = NULL;
12854 vfs_context_t ctx = vfs_context_create(vfs_context_current());
12855 kauth_cred_t cred = vfs_context_ucred(ctx);
12856 struct vnode_attr va;
12857 errno_t error = EIO;
12858
12859 if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12860 S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12861 LOG("Failed to open the file %s\n", name);
12862 swd_flags |= SWD_FILEOP_ERROR;
12863 goto exit;
12864 }
12865 VATTR_INIT(&va);
12866 VATTR_WANTED(&va, va_nlink);
12867 /* Don't dump to non-regular files or files with links. */
12868 if (vp->v_type != VREG ||
12869 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12870 LOG("Bailing as this is not a regular file\n");
12871 swd_flags |= SWD_FILEOP_ERROR;
12872 goto exit;
12873 }
12874 VATTR_INIT(&va);
12875 VATTR_SET(&va, va_data_size, 0);
12876 vnode_setattr(vp, &va, ctx);
12877
12878
12879 if (buf != NULL) {
12880 error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12881 UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12882 if (error != 0) {
12883 LOG("Failed to save sleep wake log. err 0x%x\n", error);
12884 swd_flags |= SWD_FILEOP_ERROR;
12885 } else {
12886 DLOG("Saved %d bytes to file %s\n", len, name);
12887 }
12888 }
12889
12890 exit:
12891 if (vp) {
12892 vnode_close(vp, FWRITE, ctx);
12893 }
12894 if (ctx) {
12895 vfs_context_rele(ctx);
12896 }
12897
12898 return error;
12899 }
12900
12901 #else /* defined(__i386__) || defined(__x86_64__) */
12902
12903 void
12904 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12905 {
12906 if (restart) {
12907 if (gSwdPanic == 0) {
12908 return;
12909 }
12910 panic("Sleep/Wake hang detected");
12911 return;
12912 }
12913 }
12914
12915 void
12916 IOPMrootDomain::takeStackshot(bool restart)
12917 {
12918 #pragma unused(restart)
12919 }
12920
12921 void
12922 IOPMrootDomain::deleteStackshot()
12923 {
12924 }
12925
12926 void
12927 IOPMrootDomain::sleepWakeDebugMemAlloc()
12928 {
12929 }
12930
12931 void
12932 IOPMrootDomain::saveFailureData2File()
12933 {
12934 }
12935
12936 void
12937 IOPMrootDomain::sleepWakeDebugEnableWdog()
12938 {
12939 }
12940
12941 bool
12942 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12943 {
12944 return false;
12945 }
12946
12947 void
12948 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12949 {
12950 }
12951
12952 errno_t
12953 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12954 {
12955 return 0;
12956 }
12957
12958 #endif /* defined(__i386__) || defined(__x86_64__) */
12959
12960