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 <console/video_console.h>
64 #include <sys/syslog.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/vnode_internal.h>
68 #include <sys/fcntl.h>
69 #include <os/log.h>
70 #include <pexpert/protos.h>
71 #include <AssertMacros.h>
72
73 #include <sys/time.h>
74 #include "IOServicePrivate.h" // _IOServiceInterestNotifier
75 #include "IOServicePMPrivate.h"
76
77 #include <libkern/zlib.h>
78 #include <os/cpp_util.h>
79 #include <os/atomic_private.h>
80 #include <libkern/c++/OSBoundedArrayRef.h>
81
82 __BEGIN_DECLS
83 #include <mach/shared_region.h>
84 #include <kern/clock.h>
85 __END_DECLS
86
87 #if defined(__i386__) || defined(__x86_64__)
88 __BEGIN_DECLS
89 #include "IOPMrootDomainInternal.h"
90 const char *processor_to_datastring(const char *prefix, processor_t target_processor);
91 __END_DECLS
92 #endif
93
94 #define kIOPMrootDomainClass "IOPMrootDomain"
95 #define LOG_PREFIX "PMRD: "
96
97
98 #define MSG(x...) \
99 do { kprintf(LOG_PREFIX x); IOLog(x); } while (false)
100
101 #define LOG(x...) \
102 do { kprintf(LOG_PREFIX x); } while (false)
103
104 #if DEVELOPMENT || DEBUG
105 #define DEBUG_LOG(x...) do { \
106 if (kIOLogPMRootDomain & gIOKitDebug) \
107 kprintf(LOG_PREFIX x); \
108 os_log_debug(OS_LOG_DEFAULT, LOG_PREFIX x); \
109 } while (false)
110 #else
111 #define DEBUG_LOG(x...)
112 #endif
113
114 #define DLOG(x...) do { \
115 if (kIOLogPMRootDomain & gIOKitDebug) \
116 kprintf(LOG_PREFIX x); \
117 else \
118 os_log(OS_LOG_DEFAULT, LOG_PREFIX x); \
119 } while (false)
120
121 #define DMSG(x...) do { \
122 if (kIOLogPMRootDomain & gIOKitDebug) { \
123 kprintf(LOG_PREFIX x); \
124 } \
125 } while (false)
126
127
128 #define _LOG(x...)
129
130 #define CHECK_THREAD_CONTEXT
131 #ifdef CHECK_THREAD_CONTEXT
132 static IOWorkLoop * gIOPMWorkLoop = NULL;
133 #define ASSERT_GATED() \
134 do { \
135 if (gIOPMWorkLoop && gIOPMWorkLoop->inGate() != true) { \
136 panic("RootDomain: not inside PM gate"); \
137 } \
138 } while(false)
139 #else
140 #define ASSERT_GATED()
141 #endif /* CHECK_THREAD_CONTEXT */
142
143 #define CAP_LOSS(c) \
144 (((_pendingCapability & (c)) == 0) && \
145 ((_currentCapability & (c)) != 0))
146
147 #define CAP_GAIN(c) \
148 (((_currentCapability & (c)) == 0) && \
149 ((_pendingCapability & (c)) != 0))
150
151 #define CAP_CHANGE(c) \
152 (((_currentCapability ^ _pendingCapability) & (c)) != 0)
153
154 #define CAP_CURRENT(c) \
155 ((_currentCapability & (c)) != 0)
156
157 #define CAP_HIGHEST(c) \
158 ((_highestCapability & (c)) != 0)
159
160 #define CAP_PENDING(c) \
161 ((_pendingCapability & (c)) != 0)
162
163 // rdar://problem/9157444
164 #if defined(__i386__) || defined(__x86_64__)
165 #define DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY 20
166 #endif
167
168 // Event types for IOPMPowerStateQueue::submitPowerEvent()
169 enum {
170 kPowerEventFeatureChanged = 1, // 1
171 kPowerEventReceivedPowerNotification, // 2
172 kPowerEventSystemBootCompleted, // 3
173 kPowerEventSystemShutdown, // 4
174 kPowerEventUserDisabledSleep, // 5
175 kPowerEventRegisterSystemCapabilityClient, // 6
176 kPowerEventRegisterKernelCapabilityClient, // 7
177 kPowerEventPolicyStimulus, // 8
178 kPowerEventAssertionCreate, // 9
179 kPowerEventAssertionRelease, // 10
180 kPowerEventAssertionSetLevel, // 11
181 kPowerEventQueueSleepWakeUUID, // 12
182 kPowerEventPublishSleepWakeUUID, // 13
183 kPowerEventSetDisplayPowerOn, // 14
184 kPowerEventPublishWakeType, // 15
185 kPowerEventAOTEvaluate // 16
186 };
187
188 // For evaluatePolicy()
189 // List of stimuli that affects the root domain policy.
190 enum {
191 kStimulusDisplayWranglerSleep, // 0
192 kStimulusDisplayWranglerWake, // 1
193 kStimulusAggressivenessChanged, // 2
194 kStimulusDemandSystemSleep, // 3
195 kStimulusAllowSystemSleepChanged, // 4
196 kStimulusDarkWakeActivityTickle, // 5
197 kStimulusDarkWakeEntry, // 6
198 kStimulusDarkWakeReentry, // 7
199 kStimulusDarkWakeEvaluate, // 8
200 kStimulusNoIdleSleepPreventers, // 9
201 kStimulusEnterUserActiveState, // 10
202 kStimulusLeaveUserActiveState // 11
203 };
204
205 // Internal power state change reasons
206 // Must be less than kIOPMSleepReasonClamshell=101
207 enum {
208 kCPSReasonNone = 0, // 0
209 kCPSReasonInit, // 1
210 kCPSReasonWake, // 2
211 kCPSReasonIdleSleepPrevent, // 3
212 kCPSReasonIdleSleepAllow, // 4
213 kCPSReasonPowerOverride, // 5
214 kCPSReasonPowerDownCancel, // 6
215 kCPSReasonAOTExit, // 7
216 kCPSReasonAdjustPowerState, // 8
217 kCPSReasonDarkWakeCannotSleep, // 9
218 kCPSReasonIdleSleepEnabled, // 10
219 kCPSReasonEvaluatePolicy, // 11
220 kCPSReasonSustainFullWake, // 12
221 kCPSReasonPMInternals = (kIOPMSleepReasonClamshell - 1)
222 };
223
224 extern "C" {
225 IOReturn OSKextSystemSleepOrWake( UInt32 );
226 }
227 extern "C" ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
228 extern "C" addr64_t kvtophys(vm_offset_t va);
229 extern "C" boolean_t kdp_has_polled_corefile();
230
231 static void idleSleepTimerExpired( thread_call_param_t, thread_call_param_t );
232 static void notifySystemShutdown( IOService * root, uint32_t messageType );
233 static void handleAggressivesFunction( thread_call_param_t, thread_call_param_t );
234 static void pmEventTimeStamp(uint64_t *recordTS);
235 static void powerButtonUpCallout( thread_call_param_t, thread_call_param_t );
236 static void powerButtonDownCallout( thread_call_param_t, thread_call_param_t );
237 static OSPtr<const OSSymbol> copyKextIdentifierWithAddress(vm_address_t address);
238
239 static int IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt);
240 static clock_sec_t IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt);
241 #define YMDTF "%04d/%02d/%d %02d:%02d:%02d"
242 #define YMDT(cal) ((int)(cal)->year), (cal)->month, (cal)->day, (cal)->hour, (cal)->minute, (cal)->second
243
244 // "IOPMSetSleepSupported" callPlatformFunction name
245 static OSSharedPtr<const OSSymbol> sleepSupportedPEFunction;
246 static OSSharedPtr<const OSSymbol> sleepMessagePEFunction;
247 static OSSharedPtr<const OSSymbol> gIOPMWakeTypeUserKey;
248
249 static OSSharedPtr<const OSSymbol> gIOPMPSExternalConnectedKey;
250 static OSSharedPtr<const OSSymbol> gIOPMPSExternalChargeCapableKey;
251 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryInstalledKey;
252 static OSSharedPtr<const OSSymbol> gIOPMPSIsChargingKey;
253 static OSSharedPtr<const OSSymbol> gIOPMPSAtWarnLevelKey;
254 static OSSharedPtr<const OSSymbol> gIOPMPSAtCriticalLevelKey;
255 static OSSharedPtr<const OSSymbol> gIOPMPSCurrentCapacityKey;
256 static OSSharedPtr<const OSSymbol> gIOPMPSMaxCapacityKey;
257 static OSSharedPtr<const OSSymbol> gIOPMPSDesignCapacityKey;
258 static OSSharedPtr<const OSSymbol> gIOPMPSTimeRemainingKey;
259 static OSSharedPtr<const OSSymbol> gIOPMPSAmperageKey;
260 static OSSharedPtr<const OSSymbol> gIOPMPSVoltageKey;
261 static OSSharedPtr<const OSSymbol> gIOPMPSCycleCountKey;
262 static OSSharedPtr<const OSSymbol> gIOPMPSMaxErrKey;
263 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterInfoKey;
264 static OSSharedPtr<const OSSymbol> gIOPMPSLocationKey;
265 static OSSharedPtr<const OSSymbol> gIOPMPSErrorConditionKey;
266 static OSSharedPtr<const OSSymbol> gIOPMPSManufacturerKey;
267 static OSSharedPtr<const OSSymbol> gIOPMPSManufactureDateKey;
268 static OSSharedPtr<const OSSymbol> gIOPMPSModelKey;
269 static OSSharedPtr<const OSSymbol> gIOPMPSSerialKey;
270 static OSSharedPtr<const OSSymbol> gIOPMPSLegacyBatteryInfoKey;
271 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryHealthKey;
272 static OSSharedPtr<const OSSymbol> gIOPMPSHealthConfidenceKey;
273 static OSSharedPtr<const OSSymbol> gIOPMPSCapacityEstimatedKey;
274 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryChargeStatusKey;
275 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryTemperatureKey;
276 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsKey;
277 static OSSharedPtr<const OSSymbol> gIOPMPSChargerConfigurationKey;
278 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsIDKey;
279 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsWattsKey;
280 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsRevisionKey;
281 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSerialNumberKey;
282 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsFamilyKey;
283 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsAmperageKey;
284 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsDescriptionKey;
285 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsPMUConfigurationKey;
286 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSourceIDKey;
287 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsErrorFlagsKey;
288 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSharedSourceKey;
289 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsCloakedKey;
290 static OSSharedPtr<const OSSymbol> gIOPMPSInvalidWakeSecondsKey;
291 static OSSharedPtr<const OSSymbol> gIOPMPSPostChargeWaitSecondsKey;
292 static OSSharedPtr<const OSSymbol> gIOPMPSPostDishargeWaitSecondsKey;
293
294 #define kIOSleepSupportedKey "IOSleepSupported"
295 #define kIOPMSystemCapabilitiesKey "System Capabilities"
296 #define kIOPMSystemDefaultOverrideKey "SystemPowerProfileOverrideDict"
297
298 #define kIORequestWranglerIdleKey "IORequestIdle"
299 #define kDefaultWranglerIdlePeriod 1000 // in milliseconds
300
301 #define kIOSleepWakeFailureString "SleepWakeFailureString"
302 #define kIOEFIBootRomFailureKey "wake-failure"
303 #define kIOSleepWakeFailurePanic "SleepWakeFailurePanic"
304
305 #define kRD_AllPowerSources (kIOPMSupportedOnAC \
306 | kIOPMSupportedOnBatt \
307 | kIOPMSupportedOnUPS)
308
309 #define kLocalEvalClamshellCommand (1 << 15)
310 #define kIdleSleepRetryInterval (3 * 60 * 1000)
311
312 #define DISPLAY_WRANGLER_PRESENT (!NO_KERNEL_HID)
313
314 enum {
315 kWranglerPowerStateMin = 0,
316 kWranglerPowerStateSleep = 2,
317 kWranglerPowerStateDim = 3,
318 kWranglerPowerStateMax = 4
319 };
320
321 enum {
322 OFF_STATE = 0,
323 RESTART_STATE = 1,
324 SLEEP_STATE = 2,
325 AOT_STATE = 3,
326 ON_STATE = 4,
327 NUM_POWER_STATES
328 };
329
330 const char *
getPowerStateString(uint32_t state)331 getPowerStateString( uint32_t state )
332 {
333 #define POWER_STATE(x) {(uint32_t) x, #x}
334
335 static const IONamedValue powerStates[] = {
336 POWER_STATE( OFF_STATE ),
337 POWER_STATE( RESTART_STATE ),
338 POWER_STATE( SLEEP_STATE ),
339 POWER_STATE( AOT_STATE ),
340 POWER_STATE( ON_STATE ),
341 { 0, NULL }
342 };
343 return IOFindNameForValue(state, powerStates);
344 }
345
346 #define ON_POWER kIOPMPowerOn
347 #define RESTART_POWER kIOPMRestart
348 #define SLEEP_POWER kIOPMAuxPowerOn
349
350 static IOPMPowerState
351 ourPowerStates[NUM_POWER_STATES] =
352 {
353 { .version = 1,
354 .capabilityFlags = 0,
355 .outputPowerCharacter = 0,
356 .inputPowerRequirement = 0 },
357 { .version = 1,
358 .capabilityFlags = kIOPMRestartCapability,
359 .outputPowerCharacter = kIOPMRestart,
360 .inputPowerRequirement = RESTART_POWER },
361 { .version = 1,
362 .capabilityFlags = kIOPMSleepCapability,
363 .outputPowerCharacter = kIOPMSleep,
364 .inputPowerRequirement = SLEEP_POWER },
365 { .version = 1,
366 .capabilityFlags = kIOPMAOTCapability,
367 .outputPowerCharacter = kIOPMAOTPower,
368 .inputPowerRequirement = ON_POWER },
369 { .version = 1,
370 .capabilityFlags = kIOPMPowerOn,
371 .outputPowerCharacter = kIOPMPowerOn,
372 .inputPowerRequirement = ON_POWER },
373 };
374
375 #define kIOPMRootDomainWakeTypeSleepService "SleepService"
376 #define kIOPMRootDomainWakeTypeMaintenance "Maintenance"
377 #define kIOPMRootDomainWakeTypeSleepTimer "SleepTimer"
378 #define kIOPMrootDomainWakeTypeLowBattery "LowBattery"
379 #define kIOPMRootDomainWakeTypeUser "User"
380 #define kIOPMRootDomainWakeTypeAlarm "Alarm"
381 #define kIOPMRootDomainWakeTypeNetwork "Network"
382 #define kIOPMRootDomainWakeTypeHIDActivity "HID Activity"
383 #define kIOPMRootDomainWakeTypeNotification "Notification"
384 #define kIOPMRootDomainWakeTypeHibernateError "HibernateError"
385
386 // Special interest that entitles the interested client from receiving
387 // all system messages. Only used by powerd.
388 //
389 #define kIOPMSystemCapabilityInterest "IOPMSystemCapabilityInterest"
390
391 // Entitlement required for root domain clients
392 #define kRootDomainEntitlementSetProperty "com.apple.private.iokit.rootdomain-set-property"
393
394 #define WAKEEVENT_LOCK() IOLockLock(wakeEventLock)
395 #define WAKEEVENT_UNLOCK() IOLockUnlock(wakeEventLock)
396
397 /*
398 * Aggressiveness
399 */
400 #define AGGRESSIVES_LOCK() IOLockLock(featuresDictLock)
401 #define AGGRESSIVES_UNLOCK() IOLockUnlock(featuresDictLock)
402
403 #define kAggressivesMinValue 1
404
405 const char *
getAggressivenessTypeString(uint32_t type)406 getAggressivenessTypeString( uint32_t type )
407 {
408 #define AGGRESSIVENESS_TYPE(x) {(uint32_t) x, #x}
409
410 static const IONamedValue aggressivenessTypes[] = {
411 AGGRESSIVENESS_TYPE( kPMGeneralAggressiveness ),
412 AGGRESSIVENESS_TYPE( kPMMinutesToDim ),
413 AGGRESSIVENESS_TYPE( kPMMinutesToSpinDown ),
414 AGGRESSIVENESS_TYPE( kPMMinutesToSleep ),
415 AGGRESSIVENESS_TYPE( kPMEthernetWakeOnLANSettings ),
416 AGGRESSIVENESS_TYPE( kPMSetProcessorSpeed ),
417 AGGRESSIVENESS_TYPE( kPMPowerSource),
418 AGGRESSIVENESS_TYPE( kPMMotionSensor ),
419 AGGRESSIVENESS_TYPE( kPMLastAggressivenessType ),
420 { 0, NULL }
421 };
422 return IOFindNameForValue(type, aggressivenessTypes);
423 }
424
425 enum {
426 kAggressivesStateBusy = 0x01,
427 kAggressivesStateQuickSpindown = 0x02
428 };
429
430 struct AggressivesRecord {
431 uint32_t flags;
432 uint32_t type;
433 uint32_t value;
434 };
435
436 struct AggressivesRequest {
437 queue_chain_t chain;
438 uint32_t options;
439 uint32_t dataType;
440 union {
441 OSSharedPtr<IOService> service;
442 AggressivesRecord record;
443 } data;
444 };
445
446 enum {
447 kAggressivesRequestTypeService = 1,
448 kAggressivesRequestTypeRecord
449 };
450
451 enum {
452 kAggressivesOptionSynchronous = 0x00000001,
453 kAggressivesOptionQuickSpindownEnable = 0x00000100,
454 kAggressivesOptionQuickSpindownDisable = 0x00000200,
455 kAggressivesOptionQuickSpindownMask = 0x00000300
456 };
457
458 enum {
459 kAggressivesRecordFlagModified = 0x00000001,
460 kAggressivesRecordFlagMinValue = 0x00000002
461 };
462
463 // System Sleep Preventers
464
465 enum {
466 kPMUserDisabledAllSleep = 1,
467 kPMSystemRestartBootingInProgress,
468 kPMConfigPreventSystemSleep,
469 kPMChildPreventSystemSleep,
470 kPMCPUAssertion,
471 kPMPCIUnsupported,
472 };
473
474 const char *
getSystemSleepPreventerString(uint32_t preventer)475 getSystemSleepPreventerString( uint32_t preventer )
476 {
477 #define SYSTEM_SLEEP_PREVENTER(x) {(int) x, #x}
478 static const IONamedValue systemSleepPreventers[] = {
479 SYSTEM_SLEEP_PREVENTER( kPMUserDisabledAllSleep ),
480 SYSTEM_SLEEP_PREVENTER( kPMSystemRestartBootingInProgress ),
481 SYSTEM_SLEEP_PREVENTER( kPMConfigPreventSystemSleep ),
482 SYSTEM_SLEEP_PREVENTER( kPMChildPreventSystemSleep ),
483 SYSTEM_SLEEP_PREVENTER( kPMCPUAssertion ),
484 SYSTEM_SLEEP_PREVENTER( kPMPCIUnsupported ),
485 { 0, NULL }
486 };
487 return IOFindNameForValue(preventer, systemSleepPreventers);
488 }
489
490 // gDarkWakeFlags
491 enum {
492 kDarkWakeFlagPromotionNone = 0x0000,
493 kDarkWakeFlagPromotionEarly = 0x0001, // promote before gfx clamp
494 kDarkWakeFlagPromotionLate = 0x0002, // promote after gfx clamp
495 kDarkWakeFlagPromotionMask = 0x0003,
496 kDarkWakeFlagAlarmIsDark = 0x0100,
497 kDarkWakeFlagAudioNotSuppressed = 0x0200,
498 kDarkWakeFlagUserWakeWorkaround = 0x1000
499 };
500
501 // gClamshellFlags
502 // The workaround for 9157444 is enabled at compile time using the
503 // DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY macro and is not represented below.
504 enum {
505 kClamshell_WAR_38378787 = 0x00000001,
506 kClamshell_WAR_47715679 = 0x00000002,
507 kClamshell_WAR_58009435 = 0x00000004
508 };
509
510 // acceptSystemWakeEvents()
511 enum {
512 kAcceptSystemWakeEvents_Disable = 0,
513 kAcceptSystemWakeEvents_Enable,
514 kAcceptSystemWakeEvents_Reenable
515 };
516
517 static IOPMrootDomain * gRootDomain;
518 static IORootParent * gPatriarch;
519 static IONotifier * gSysPowerDownNotifier = NULL;
520 static UInt32 gSleepOrShutdownPending = 0;
521 static UInt32 gWillShutdown = 0;
522 static UInt32 gPagingOff = 0;
523 static UInt32 gSleepWakeUUIDIsSet = false;
524 static uint32_t gAggressivesState = 0;
525 uint32_t gHaltTimeMaxLog;
526 uint32_t gHaltTimeMaxPanic;
527 IOLock * gHaltLogLock;
528 static char * gHaltLog;
529 enum { kHaltLogSize = 2048 };
530 static size_t gHaltLogPos;
531 static uint64_t gHaltStartTime;
532 static char gKextNameBuf[64];
533 static size_t gKextNamePos;
534 static bool gKextNameEnd;
535
536 uuid_string_t bootsessionuuid_string;
537
538 #if defined(XNU_TARGET_OS_OSX)
539 #if DISPLAY_WRANGLER_PRESENT
540 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionNone;
541 #elif CONFIG_ARROW
542 // Enable temporary full wake promotion workarounds
543 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
544 #else
545 // Enable full wake promotion workarounds
546 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
547 #endif
548 #else /* !defined(XNU_TARGET_OS_OSX) */
549 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionEarly;
550 #endif /* !defined(XNU_TARGET_OS_OSX) */
551
552 static uint32_t gNoIdleFlag = 0;
553 static uint32_t gSleepDisabledFlag = 0;
554 static uint32_t gSwdPanic = 1;
555 static uint32_t gSwdSleepTimeout = 0;
556 static uint32_t gSwdWakeTimeout = 0;
557 static uint32_t gSwdSleepWakeTimeout = 0;
558 static PMStatsStruct gPMStats;
559 #if DEVELOPMENT || DEBUG
560 static uint32_t swd_panic_phase;
561 #endif
562
563 static uint32_t gClamshellFlags = 0
564 #if defined(__i386__) || defined(__x86_64__)
565 | kClamshell_WAR_58009435
566 #endif
567 ;
568
569 #if HIBERNATION
570
571 #if defined(__arm64__)
572 static IOReturn
defaultSleepPolicyHandler(void * ctx,const IOPMSystemSleepPolicyVariables * vars,IOPMSystemSleepParameters * params)573 defaultSleepPolicyHandler(void *ctx, const IOPMSystemSleepPolicyVariables *vars, IOPMSystemSleepParameters *params)
574 {
575 uint32_t sleepType = kIOPMSleepTypeDeepIdle;
576
577 assert(vars->signature == kIOPMSystemSleepPolicySignature);
578 assert(vars->version == kIOPMSystemSleepPolicyVersion);
579
580 // Hibernation enabled and either user forced hibernate or low battery sleep
581 if ((vars->hibernateMode & kIOHibernateModeOn) &&
582 (((vars->hibernateMode & kIOHibernateModeSleep) == 0) ||
583 (vars->sleepFactors & kIOPMSleepFactorBatteryLow))) {
584 sleepType = kIOPMSleepTypeHibernate;
585 }
586 params->version = kIOPMSystemSleepParametersVersion;
587 params->sleepType = sleepType;
588 return kIOReturnSuccess;
589 }
590 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = &defaultSleepPolicyHandler;
591 #else /* defined(__arm64__) */
592 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = NULL;
593 #endif /* defined(__arm64__) */
594
595 static IOPMSystemSleepPolicyVariables * gSleepPolicyVars = NULL;
596 static void * gSleepPolicyTarget;
597 #endif
598
599 struct timeval gIOLastSleepTime;
600 struct timeval gIOLastWakeTime;
601 AbsoluteTime gIOLastWakeAbsTime;
602 AbsoluteTime gIOLastSleepAbsTime;
603
604 struct timeval gIOLastUserSleepTime;
605
606 static char gWakeReasonString[128];
607 static char gBootReasonString[80];
608 static char gShutdownReasonString[80];
609 static bool gWakeReasonSysctlRegistered = false;
610 static bool gBootReasonSysctlRegistered = false;
611 static bool gShutdownReasonSysctlRegistered = false;
612 static bool gWillShutdownSysctlRegistered = false;
613 static AbsoluteTime gUserActiveAbsTime;
614 static AbsoluteTime gUserInactiveAbsTime;
615
616 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
617 static bool gSpinDumpBufferFull = false;
618 #endif
619
620 z_stream swd_zs;
621 vm_offset_t swd_zs_zmem;
622 //size_t swd_zs_zsize;
623 size_t swd_zs_zoffset;
624 #if defined(__i386__) || defined(__x86_64__)
625 IOCPU *currentShutdownTarget = NULL;
626 #endif
627
628 static unsigned int gPMHaltBusyCount;
629 static unsigned int gPMHaltIdleCount;
630 static int gPMHaltDepth;
631 static uint32_t gPMHaltMessageType;
632 static IOLock * gPMHaltLock = NULL;
633 static OSSharedPtr<OSArray> gPMHaltArray;
634 static OSSharedPtr<const OSSymbol> gPMHaltClientAcknowledgeKey;
635 static bool gPMQuiesced;
636
637 // Constants used as arguments to IOPMrootDomain::informCPUStateChange
638 #define kCPUUnknownIndex 9999999
639 enum {
640 kInformAC = 0,
641 kInformLid = 1,
642 kInformableCount = 2
643 };
644
645 OSSharedPtr<const OSSymbol> gIOPMStatsResponseTimedOut;
646 OSSharedPtr<const OSSymbol> gIOPMStatsResponseCancel;
647 OSSharedPtr<const OSSymbol> gIOPMStatsResponseSlow;
648 OSSharedPtr<const OSSymbol> gIOPMStatsResponsePrompt;
649 OSSharedPtr<const OSSymbol> gIOPMStatsDriverPSChangeSlow;
650
651 #define kBadPMFeatureID 0
652
653 /*
654 * PMSettingHandle
655 * Opaque handle passed to clients of registerPMSettingController()
656 */
657 class PMSettingHandle : public OSObject
658 {
659 OSDeclareFinalStructors( PMSettingHandle );
660 friend class PMSettingObject;
661
662 private:
663 PMSettingObject *pmso;
664 void free(void) APPLE_KEXT_OVERRIDE;
665 };
666
667 /*
668 * PMSettingObject
669 * Internal object to track each PM setting controller
670 */
671 class PMSettingObject : public OSObject
672 {
673 OSDeclareFinalStructors( PMSettingObject );
674 friend class IOPMrootDomain;
675
676 private:
677 queue_head_t calloutQueue;
678 thread_t waitThread;
679 IOPMrootDomain *parent;
680 PMSettingHandle *pmsh;
681 IOPMSettingControllerCallback func;
682 OSObject *target;
683 uintptr_t refcon;
684 OSDataAllocation<uint32_t> publishedFeatureID;
685 uint32_t settingCount;
686 bool disabled;
687
688 void free(void) APPLE_KEXT_OVERRIDE;
689
690 public:
691 static PMSettingObject *pmSettingObject(
692 IOPMrootDomain *parent_arg,
693 IOPMSettingControllerCallback handler_arg,
694 OSObject *target_arg,
695 uintptr_t refcon_arg,
696 uint32_t supportedPowerSources,
697 const OSSymbol *settings[],
698 OSObject **handle_obj);
699
700 IOReturn dispatchPMSetting(const OSSymbol *type, OSObject *object);
701 void clientHandleFreed(void);
702 };
703
704 struct PMSettingCallEntry {
705 queue_chain_t link;
706 thread_t thread;
707 };
708
709 #define PMSETTING_LOCK() IOLockLock(settingsCtrlLock)
710 #define PMSETTING_UNLOCK() IOLockUnlock(settingsCtrlLock)
711 #define PMSETTING_WAIT(p) IOLockSleep(settingsCtrlLock, p, THREAD_UNINT)
712 #define PMSETTING_WAKEUP(p) IOLockWakeup(settingsCtrlLock, p, true)
713
714 /*
715 * PMTraceWorker
716 * Internal helper object for logging trace points to RTC
717 * IOPMrootDomain and only IOPMrootDomain should instantiate
718 * exactly one of these.
719 */
720
721 typedef void (*IOPMTracePointHandler)(
722 void * target, uint32_t code, uint32_t data );
723
724 class PMTraceWorker : public OSObject
725 {
726 OSDeclareDefaultStructors(PMTraceWorker);
727 public:
728 typedef enum { kPowerChangeStart, kPowerChangeCompleted } change_t;
729
730 static OSPtr<PMTraceWorker> tracer( IOPMrootDomain * );
731 void tracePCIPowerChange(change_t, IOService *, uint32_t, uint32_t);
732 void tracePoint(uint8_t phase);
733 void traceDetail(uint32_t detail);
734 void traceComponentWakeProgress(uint32_t component, uint32_t data);
735 int recordTopLevelPCIDevice(IOService *);
736 void RTC_TRACE(void);
737 virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
738
739 IOPMTracePointHandler tracePointHandler;
740 void * tracePointTarget;
741 uint64_t getPMStatusCode();
742 uint8_t getTracePhase();
743 uint32_t getTraceData();
744 private:
745 IOPMrootDomain *owner;
746 IOLock *pmTraceWorkerLock;
747 OSSharedPtr<OSArray> pciDeviceBitMappings;
748
749 uint8_t addedToRegistry;
750 uint8_t tracePhase;
751 uint32_t traceData32;
752 uint8_t loginWindowData;
753 uint8_t coreDisplayData;
754 uint8_t coreGraphicsData;
755 };
756
757 /*
758 * PMAssertionsTracker
759 * Tracks kernel and user space PM assertions
760 */
761 class PMAssertionsTracker : public OSObject
762 {
763 OSDeclareFinalStructors(PMAssertionsTracker);
764 public:
765 static PMAssertionsTracker *pmAssertionsTracker( IOPMrootDomain * );
766
767 IOReturn createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
768 IOReturn releaseAssertion(IOPMDriverAssertionID);
769 IOReturn setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
770 IOReturn setUserAssertionLevels(IOPMDriverAssertionType);
771
772 OSSharedPtr<OSArray> copyAssertionsArray(void);
773 IOPMDriverAssertionType getActivatedAssertions(void);
774 IOPMDriverAssertionLevel getAssertionLevel(IOPMDriverAssertionType);
775
776 IOReturn handleCreateAssertion(OSData *);
777 IOReturn handleReleaseAssertion(IOPMDriverAssertionID);
778 IOReturn handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
779 IOReturn handleSetUserAssertionLevels(void * arg0);
780 void publishProperties(void);
781 void reportCPUBitAccounting(void);
782
783 private:
784 /*
785 * this should be treated as POD, as it's byte-copied around
786 * and we cannot rely on d'tor firing at the right time
787 */
788 typedef struct {
789 IOPMDriverAssertionID id;
790 IOPMDriverAssertionType assertionBits;
791 uint64_t createdTime;
792 uint64_t modifiedTime;
793 const OSSymbol *ownerString;
794 IOService *ownerService;
795 uint64_t registryEntryID;
796 IOPMDriverAssertionLevel level;
797 uint64_t assertCPUStartTime;
798 uint64_t assertCPUDuration;
799 } PMAssertStruct;
800
801 uint32_t tabulateProducerCount;
802 uint32_t tabulateConsumerCount;
803
804 uint64_t maxAssertCPUDuration;
805 uint64_t maxAssertCPUEntryId;
806
807 PMAssertStruct *detailsForID(IOPMDriverAssertionID, int *);
808 void tabulate(void);
809 void updateCPUBitAccounting(PMAssertStruct * assertStruct);
810
811 IOPMrootDomain *owner;
812 OSSharedPtr<OSArray> assertionsArray;
813 IOLock *assertionsArrayLock;
814 IOPMDriverAssertionID issuingUniqueID __attribute__((aligned(8)));/* aligned for atomic access */
815 IOPMDriverAssertionType assertionsKernel;
816 IOPMDriverAssertionType assertionsUser;
817 IOPMDriverAssertionType assertionsCombined;
818 };
819
820 OSDefineMetaClassAndFinalStructors(PMAssertionsTracker, OSObject);
821
822 /*
823 * PMHaltWorker
824 * Internal helper object for Shutdown/Restart notifications.
825 */
826 #define kPMHaltMaxWorkers 8
827 #define kPMHaltTimeoutMS 100
828
829 class PMHaltWorker : public OSObject
830 {
831 OSDeclareFinalStructors( PMHaltWorker );
832
833 public:
834 IOService * service;// service being worked on
835 AbsoluteTime startTime; // time when work started
836 int depth; // work on nubs at this PM-tree depth
837 int visits; // number of nodes visited (debug)
838 IOLock * lock;
839 bool timeout;// service took too long
840
841 static PMHaltWorker * worker( void );
842 static void main( void * arg, wait_result_t waitResult );
843 static void work( PMHaltWorker * me );
844 static void checkTimeout( PMHaltWorker * me, AbsoluteTime * now );
845 virtual void free( void ) APPLE_KEXT_OVERRIDE;
846 };
847
OSDefineMetaClassAndFinalStructors(PMHaltWorker,OSObject)848 OSDefineMetaClassAndFinalStructors( PMHaltWorker, OSObject )
849
850
851 #define super IOService
852 OSDefineMetaClassAndFinalStructors(IOPMrootDomain, IOService)
853
854 boolean_t
855 IOPMRootDomainGetWillShutdown(void)
856 {
857 return gWillShutdown != 0;
858 }
859
860 static void
IOPMRootDomainWillShutdown(void)861 IOPMRootDomainWillShutdown(void)
862 {
863 if (OSCompareAndSwap(0, 1, &gWillShutdown)) {
864 IOService::willShutdown();
865 for (int i = 0; i < 100; i++) {
866 if (OSCompareAndSwap(0, 1, &gSleepOrShutdownPending)) {
867 break;
868 }
869 IOSleep( 100 );
870 }
871 }
872 }
873
874 extern "C" IONotifier *
registerSleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)875 registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
876 {
877 return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref ).detach();
878 }
879
880 extern "C" IONotifier *
registerPrioritySleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)881 registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
882 {
883 return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref ).detach();
884 }
885
886 extern "C" IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon)887 acknowledgeSleepWakeNotification(void * PMrefcon)
888 {
889 return gRootDomain->allowPowerChange((unsigned long)PMrefcon );
890 }
891
892 extern "C" IOReturn
vetoSleepWakeNotification(void * PMrefcon)893 vetoSleepWakeNotification(void * PMrefcon)
894 {
895 return gRootDomain->cancelPowerChange((unsigned long)PMrefcon );
896 }
897
898 extern "C" IOReturn
rootDomainRestart(void)899 rootDomainRestart( void )
900 {
901 return gRootDomain->restartSystem();
902 }
903
904 extern "C" IOReturn
rootDomainShutdown(void)905 rootDomainShutdown( void )
906 {
907 return gRootDomain->shutdownSystem();
908 }
909
910 static void
halt_log_putc(char c)911 halt_log_putc(char c)
912 {
913 if (gHaltLogPos >= (kHaltLogSize - 2)) {
914 return;
915 }
916 gHaltLog[gHaltLogPos++] = c;
917 }
918
919 extern "C" void
920 _doprnt_log(const char *fmt,
921 va_list *argp,
922 void (*putc)(char),
923 int radix);
924
925 static int
halt_log(const char * fmt,...)926 halt_log(const char *fmt, ...)
927 {
928 va_list listp;
929
930 va_start(listp, fmt);
931 _doprnt_log(fmt, &listp, &halt_log_putc, 16);
932 va_end(listp);
933
934 return 0;
935 }
936
937 extern "C" void
halt_log_enter(const char * what,const void * pc,uint64_t time)938 halt_log_enter(const char * what, const void * pc, uint64_t time)
939 {
940 uint64_t nano, millis;
941
942 if (!gHaltLog) {
943 return;
944 }
945 absolutetime_to_nanoseconds(time, &nano);
946 millis = nano / NSEC_PER_MSEC;
947 if (millis < 100) {
948 return;
949 }
950
951 IOLockLock(gHaltLogLock);
952 if (pc) {
953 halt_log("%s: %qd ms @ 0x%lx, ", what, millis, VM_KERNEL_UNSLIDE(pc));
954 OSKext::printKextsInBacktrace((vm_offset_t *) &pc, 1, &halt_log,
955 OSKext::kPrintKextsLock | OSKext::kPrintKextsUnslide | OSKext::kPrintKextsTerse);
956 } else {
957 halt_log("%s: %qd ms\n", what, millis);
958 }
959
960 gHaltLog[gHaltLogPos] = 0;
961 IOLockUnlock(gHaltLogLock);
962 }
963
964 extern uint32_t gFSState;
965
966 extern "C" void
IOSystemShutdownNotification(int howto,int stage)967 IOSystemShutdownNotification(int howto, int stage)
968 {
969 uint64_t startTime;
970
971 if (kIOSystemShutdownNotificationStageRootUnmount == stage) {
972 #if defined(XNU_TARGET_OS_OSX)
973 uint64_t nano, millis;
974 startTime = mach_absolute_time();
975 IOService::getPlatform()->waitQuiet(30 * NSEC_PER_SEC);
976 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
977 millis = nano / NSEC_PER_MSEC;
978 if (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog)) {
979 printf("waitQuiet() for unmount %qd ms\n", millis);
980 }
981 #endif /* defined(XNU_TARGET_OS_OSX) */
982 return;
983 }
984
985 if (kIOSystemShutdownNotificationTerminateDEXTs == stage) {
986 uint64_t nano, millis;
987 startTime = mach_absolute_time();
988 IOServicePH::systemHalt(howto);
989 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
990 millis = nano / NSEC_PER_MSEC;
991 if (true || (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog))) {
992 printf("IOServicePH::systemHalt took %qd ms\n", millis);
993 }
994 return;
995 }
996
997 assert(kIOSystemShutdownNotificationStageProcessExit == stage);
998
999 IOLockLock(gHaltLogLock);
1000 if (!gHaltLog) {
1001 gHaltLog = IONewData(char, (vm_size_t)kHaltLogSize);
1002 gHaltStartTime = mach_absolute_time();
1003 if (gHaltLog) {
1004 halt_log_putc('\n');
1005 }
1006 }
1007 IOLockUnlock(gHaltLogLock);
1008
1009 startTime = mach_absolute_time();
1010 IOPMRootDomainWillShutdown();
1011 halt_log_enter("IOPMRootDomainWillShutdown", NULL, mach_absolute_time() - startTime);
1012 #if HIBERNATION
1013 startTime = mach_absolute_time();
1014 IOHibernateSystemPostWake(true);
1015 halt_log_enter("IOHibernateSystemPostWake", NULL, mach_absolute_time() - startTime);
1016 #endif
1017 if (OSCompareAndSwap(0, 1, &gPagingOff)) {
1018 gRootDomain->handlePlatformHaltRestart(kPEPagingOff);
1019 }
1020 }
1021
1022 extern "C" int sync_internal(void);
1023
1024 /*
1025 * A device is always in the highest power state which satisfies its driver,
1026 * its policy-maker, and any power children it has, but within the constraint
1027 * of the power state provided by its parent. The driver expresses its desire by
1028 * calling changePowerStateTo(), the policy-maker expresses its desire by calling
1029 * changePowerStateToPriv(), and the children express their desires by calling
1030 * requestPowerDomainState().
1031 *
1032 * The Root Power Domain owns the policy for idle and demand sleep for the system.
1033 * It is a power-managed IOService just like the others in the system.
1034 * It implements several power states which map to what we see as Sleep and On.
1035 *
1036 * The sleep policy is as follows:
1037 * 1. Sleep is prevented if the case is open so that nobody will think the machine
1038 * is off and plug/unplug cards.
1039 * 2. Sleep is prevented if the sleep timeout slider in the prefs panel is zero.
1040 * 3. System cannot Sleep if some object in the tree is in a power state marked
1041 * kIOPMPreventSystemSleep.
1042 *
1043 * These three conditions are enforced using the "driver clamp" by calling
1044 * changePowerStateTo(). For example, if the case is opened,
1045 * changePowerStateTo(ON_STATE) is called to hold the system on regardless
1046 * of the desires of the children of the root or the state of the other clamp.
1047 *
1048 * Demand Sleep is initiated by pressing the front panel power button, closing
1049 * the clamshell, or selecting the menu item. In this case the root's parent
1050 * actually initiates the power state change so that the root domain has no
1051 * choice and does not give applications the opportunity to veto the change.
1052 *
1053 * Idle Sleep occurs if no objects in the tree are in a state marked
1054 * kIOPMPreventIdleSleep. When this is true, the root's children are not holding
1055 * the root on, so it sets the "policy-maker clamp" by calling
1056 * changePowerStateToPriv(ON_STATE) to hold itself on until the sleep timer expires.
1057 * This timer is set for the difference between the sleep timeout slider and the
1058 * display dim timeout slider. When the timer expires, it releases its clamp and
1059 * now nothing is holding it awake, so it falls asleep.
1060 *
1061 * Demand sleep is prevented when the system is booting. When preferences are
1062 * transmitted by the loginwindow at the end of boot, a flag is cleared,
1063 * and this allows subsequent Demand Sleep.
1064 */
1065
1066 //******************************************************************************
1067
1068 IOPMrootDomain *
construct(void)1069 IOPMrootDomain::construct( void )
1070 {
1071 IOPMrootDomain *root;
1072
1073 root = new IOPMrootDomain;
1074 if (root) {
1075 root->init();
1076 }
1077
1078 return root;
1079 }
1080
1081 //******************************************************************************
1082 // updateConsoleUsersCallout
1083 //
1084 //******************************************************************************
1085
1086 static void
updateConsoleUsersCallout(thread_call_param_t p0,thread_call_param_t p1)1087 updateConsoleUsersCallout(thread_call_param_t p0, thread_call_param_t p1)
1088 {
1089 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1090 rootDomain->updateConsoleUsers();
1091 }
1092
1093 void
updateConsoleUsers(void)1094 IOPMrootDomain::updateConsoleUsers(void)
1095 {
1096 IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
1097 if (tasksSuspended) {
1098 tasksSuspended = FALSE;
1099 updateTasksSuspend();
1100 }
1101 }
1102
1103 void
updateTasksSuspend(void)1104 IOPMrootDomain::updateTasksSuspend(void)
1105 {
1106 bool newSuspend;
1107
1108 newSuspend = (tasksSuspended || _aotTasksSuspended);
1109 if (newSuspend == tasksSuspendState) {
1110 return;
1111 }
1112 tasksSuspendState = newSuspend;
1113 tasks_system_suspend(newSuspend);
1114 }
1115
1116 //******************************************************************************
1117
1118 static void
disk_sync_callout(thread_call_param_t p0,thread_call_param_t p1)1119 disk_sync_callout( thread_call_param_t p0, thread_call_param_t p1 )
1120 {
1121 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1122 uint32_t notifyRef = (uint32_t)(uintptr_t) p1;
1123 uint32_t powerState = rootDomain->getPowerState();
1124
1125 DLOG("disk_sync_callout ps=%u\n", powerState);
1126
1127 if (ON_STATE == powerState) {
1128 sync_internal();
1129
1130 #if HIBERNATION
1131 // Block sleep until trim issued on previous wake path is completed.
1132 IOHibernateSystemPostWake(true);
1133 #endif
1134 }
1135 #if HIBERNATION
1136 else {
1137 IOHibernateSystemPostWake(false);
1138
1139 rootDomain->sleepWakeDebugSaveSpinDumpFile();
1140 }
1141 #endif
1142
1143 rootDomain->allowPowerChange(notifyRef);
1144 DLOG("disk_sync_callout finish\n");
1145 }
1146
1147 //******************************************************************************
1148 static UInt32
computeDeltaTimeMS(const AbsoluteTime * startTime,AbsoluteTime * elapsedTime)1149 computeDeltaTimeMS( const AbsoluteTime * startTime, AbsoluteTime * elapsedTime )
1150 {
1151 AbsoluteTime endTime;
1152 UInt64 nano = 0;
1153
1154 clock_get_uptime(&endTime);
1155 if (CMP_ABSOLUTETIME(&endTime, startTime) <= 0) {
1156 *elapsedTime = 0;
1157 } else {
1158 SUB_ABSOLUTETIME(&endTime, startTime);
1159 absolutetime_to_nanoseconds(endTime, &nano);
1160 *elapsedTime = endTime;
1161 }
1162
1163 return (UInt32)(nano / NSEC_PER_MSEC);
1164 }
1165
1166 //******************************************************************************
1167
1168 static int
1169 sysctl_sleepwaketime SYSCTL_HANDLER_ARGS
1170 {
1171 struct timeval *swt = (struct timeval *)arg1;
1172 struct proc *p = req->p;
1173
1174 if (p == kernproc) {
1175 return sysctl_io_opaque(req, swt, sizeof(*swt), NULL);
1176 } else if (proc_is64bit(p)) {
1177 struct user64_timeval t = {};
1178 t.tv_sec = swt->tv_sec;
1179 t.tv_usec = swt->tv_usec;
1180 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1181 } else {
1182 struct user32_timeval t = {};
1183 t.tv_sec = (typeof(t.tv_sec))swt->tv_sec;
1184 t.tv_usec = swt->tv_usec;
1185 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1186 }
1187 }
1188
1189 static SYSCTL_PROC(_kern, OID_AUTO, sleeptime,
1190 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1191 &gIOLastUserSleepTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1192
1193 static SYSCTL_PROC(_kern, OID_AUTO, waketime,
1194 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1195 &gIOLastWakeTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1196
1197 SYSCTL_QUAD(_kern, OID_AUTO, wake_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastWakeAbsTime, "");
1198 SYSCTL_QUAD(_kern, OID_AUTO, sleep_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastSleepAbsTime, "");
1199 SYSCTL_QUAD(_kern, OID_AUTO, useractive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserActiveAbsTime, "");
1200 SYSCTL_QUAD(_kern, OID_AUTO, userinactive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserInactiveAbsTime, "");
1201
1202 static int
1203 sysctl_willshutdown SYSCTL_HANDLER_ARGS
1204 {
1205 int new_value, changed, error;
1206
1207 if (!gWillShutdownSysctlRegistered) {
1208 return ENOENT;
1209 }
1210
1211 error = sysctl_io_number(req, gWillShutdown, sizeof(int), &new_value, &changed);
1212 if (changed) {
1213 if (!gWillShutdown && (new_value == 1)) {
1214 IOPMRootDomainWillShutdown();
1215 } else {
1216 error = EINVAL;
1217 }
1218 }
1219 return error;
1220 }
1221
1222 static SYSCTL_PROC(_kern, OID_AUTO, willshutdown,
1223 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1224 NULL, 0, sysctl_willshutdown, "I", "");
1225
1226 #if defined(XNU_TARGET_OS_OSX)
1227
1228 static int
sysctl_progressmeterenable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1229 sysctl_progressmeterenable
1230 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1231 {
1232 int error;
1233 int new_value, changed;
1234
1235 error = sysctl_io_number(req, vc_progressmeter_enable, sizeof(int), &new_value, &changed);
1236
1237 if (changed) {
1238 vc_enable_progressmeter(new_value);
1239 }
1240
1241 return error;
1242 }
1243
1244 static int
sysctl_progressmeter(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1245 sysctl_progressmeter
1246 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1247 {
1248 int error;
1249 int new_value, changed;
1250
1251 error = sysctl_io_number(req, vc_progressmeter_value, sizeof(int), &new_value, &changed);
1252
1253 if (changed) {
1254 vc_set_progressmeter(new_value);
1255 }
1256
1257 return error;
1258 }
1259
1260 static SYSCTL_PROC(_kern, OID_AUTO, progressmeterenable,
1261 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1262 NULL, 0, sysctl_progressmeterenable, "I", "");
1263
1264 static SYSCTL_PROC(_kern, OID_AUTO, progressmeter,
1265 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1266 NULL, 0, sysctl_progressmeter, "I", "");
1267
1268 #endif /* defined(XNU_TARGET_OS_OSX) */
1269
1270
1271
1272 static int
sysctl_consoleoptions(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1273 sysctl_consoleoptions
1274 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1275 {
1276 int error, changed;
1277 uint32_t new_value;
1278
1279 error = sysctl_io_number(req, vc_user_options.options, sizeof(uint32_t), &new_value, &changed);
1280
1281 if (changed) {
1282 vc_user_options.options = new_value;
1283 }
1284
1285 return error;
1286 }
1287
1288 static SYSCTL_PROC(_kern, OID_AUTO, consoleoptions,
1289 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1290 NULL, 0, sysctl_consoleoptions, "I", "");
1291
1292
1293 static int
1294 sysctl_progressoptions SYSCTL_HANDLER_ARGS
1295 {
1296 return sysctl_io_opaque(req, &vc_user_options, sizeof(vc_user_options), NULL);
1297 }
1298
1299 static SYSCTL_PROC(_kern, OID_AUTO, progressoptions,
1300 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1301 NULL, 0, sysctl_progressoptions, "S,vc_progress_user_options", "");
1302
1303
1304 static int
1305 sysctl_wakereason SYSCTL_HANDLER_ARGS
1306 {
1307 char wr[sizeof(gWakeReasonString)];
1308
1309 wr[0] = '\0';
1310 if (gRootDomain && gWakeReasonSysctlRegistered) {
1311 gRootDomain->copyWakeReasonString(wr, sizeof(wr));
1312 } else {
1313 return ENOENT;
1314 }
1315
1316 return sysctl_io_string(req, wr, 0, 0, NULL);
1317 }
1318
1319 SYSCTL_PROC(_kern, OID_AUTO, wakereason,
1320 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1321 NULL, 0, sysctl_wakereason, "A", "wakereason");
1322
1323 static int
1324 sysctl_bootreason SYSCTL_HANDLER_ARGS
1325 {
1326 if (!os_atomic_load(&gBootReasonSysctlRegistered, acquire)) {
1327 return ENOENT;
1328 }
1329
1330 return sysctl_io_string(req, gBootReasonString, 0, 0, NULL);
1331 }
1332
1333 SYSCTL_PROC(_kern, OID_AUTO, bootreason,
1334 CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1335 NULL, 0, sysctl_bootreason, "A", "");
1336
1337 static int
1338 sysctl_shutdownreason SYSCTL_HANDLER_ARGS
1339 {
1340 char sr[sizeof(gShutdownReasonString)];
1341
1342 sr[0] = '\0';
1343 if (gRootDomain && gShutdownReasonSysctlRegistered) {
1344 gRootDomain->copyShutdownReasonString(sr, sizeof(sr));
1345 } else {
1346 return ENOENT;
1347 }
1348
1349 return sysctl_io_string(req, sr, 0, 0, NULL);
1350 }
1351
1352 SYSCTL_PROC(_kern, OID_AUTO, shutdownreason,
1353 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1354 NULL, 0, sysctl_shutdownreason, "A", "shutdownreason");
1355
1356 static int
1357 sysctl_targettype SYSCTL_HANDLER_ARGS
1358 {
1359 IOService * root;
1360 OSSharedPtr<OSObject> obj;
1361 OSData * data;
1362 char tt[32];
1363
1364 tt[0] = '\0';
1365 root = IOService::getServiceRoot();
1366 if (root && (obj = root->copyProperty(gIODTTargetTypeKey))) {
1367 if ((data = OSDynamicCast(OSData, obj.get()))) {
1368 strlcpy(tt, (const char *) data->getBytesNoCopy(), sizeof(tt));
1369 }
1370 }
1371 return sysctl_io_string(req, tt, 0, 0, NULL);
1372 }
1373
1374 SYSCTL_PROC(_hw, OID_AUTO, targettype,
1375 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1376 NULL, 0, sysctl_targettype, "A", "targettype");
1377
1378 static SYSCTL_INT(_debug, OID_AUTO, noidle, CTLFLAG_RW, &gNoIdleFlag, 0, "");
1379 static SYSCTL_INT(_debug, OID_AUTO, swd_sleep_timeout, CTLFLAG_RW, &gSwdSleepTimeout, 0, "");
1380 static SYSCTL_INT(_debug, OID_AUTO, swd_wake_timeout, CTLFLAG_RW, &gSwdWakeTimeout, 0, "");
1381 static SYSCTL_INT(_debug, OID_AUTO, swd_timeout, CTLFLAG_RW, &gSwdSleepWakeTimeout, 0, "");
1382 static SYSCTL_INT(_debug, OID_AUTO, swd_panic, CTLFLAG_RW, &gSwdPanic, 0, "");
1383 #if DEVELOPMENT || DEBUG
1384 static SYSCTL_INT(_debug, OID_AUTO, swd_panic_phase, CTLFLAG_RW, &swd_panic_phase, 0, "");
1385 #if defined(XNU_TARGET_OS_OSX)
1386 static SYSCTL_INT(_debug, OID_AUTO, clamshell, CTLFLAG_RW, &gClamshellFlags, 0, "");
1387 static SYSCTL_INT(_debug, OID_AUTO, darkwake, CTLFLAG_RW, &gDarkWakeFlags, 0, "");
1388 #endif /* defined(XNU_TARGET_OS_OSX) */
1389 #endif /* DEVELOPMENT || DEBUG */
1390
1391 //******************************************************************************
1392 // AOT
1393
1394 static int
1395 sysctl_aotmetrics SYSCTL_HANDLER_ARGS
1396 {
1397 if (NULL == gRootDomain) {
1398 return ENOENT;
1399 }
1400 if (NULL == gRootDomain->_aotMetrics) {
1401 return ENOENT;
1402 }
1403 return sysctl_io_opaque(req, gRootDomain->_aotMetrics, sizeof(IOPMAOTMetrics), NULL);
1404 }
1405
1406 static SYSCTL_PROC(_kern, OID_AUTO, aotmetrics,
1407 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1408 NULL, 0, sysctl_aotmetrics, "S,IOPMAOTMetrics", "");
1409
1410
1411 static int
update_aotmode(uint32_t mode)1412 update_aotmode(uint32_t mode)
1413 {
1414 int result;
1415
1416 if (!gIOPMWorkLoop) {
1417 return ENOENT;
1418 }
1419 result = gIOPMWorkLoop->runActionBlock(^IOReturn (void) {
1420 unsigned int oldCount;
1421
1422 if (mode && !gRootDomain->_aotMetrics) {
1423 gRootDomain->_aotMetrics = IOMallocType(IOPMAOTMetrics);
1424 }
1425
1426 oldCount = gRootDomain->idleSleepPreventersCount();
1427 gRootDomain->_aotMode = (mode & kIOPMAOTModeMask);
1428 gRootDomain->updatePreventIdleSleepListInternal(NULL, false, oldCount);
1429 return 0;
1430 });
1431 return result;
1432 }
1433
1434 static int
sysctl_aotmodebits(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1435 sysctl_aotmodebits
1436 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1437 {
1438 int error, changed;
1439 uint32_t new_value;
1440
1441 if (NULL == gRootDomain) {
1442 return ENOENT;
1443 }
1444 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1445 if (changed && gIOPMWorkLoop) {
1446 error = update_aotmode(new_value);
1447 }
1448
1449 return error;
1450 }
1451
1452 static SYSCTL_PROC(_kern, OID_AUTO, aotmodebits,
1453 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1454 NULL, 0, sysctl_aotmodebits, "I", "");
1455
1456 static int
sysctl_aotmode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1457 sysctl_aotmode
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 if (new_value) {
1469 new_value = kIOPMAOTModeDefault; // & ~kIOPMAOTModeRespectTimers;
1470 }
1471 error = update_aotmode(new_value);
1472 }
1473
1474 return error;
1475 }
1476
1477 static SYSCTL_PROC(_kern, OID_AUTO, aotmode,
1478 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1479 NULL, 0, sysctl_aotmode, "I", "");
1480
1481 //******************************************************************************
1482
1483 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeCalendarKey;
1484 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeSecondsKey;
1485 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerCalendarKey;
1486 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerSecondsKey;
1487 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugWakeRelativeKey;
1488 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugPowerRelativeKey;
1489 static OSSharedPtr<const OSSymbol> gIOPMSettingMaintenanceWakeCalendarKey;
1490 static OSSharedPtr<const OSSymbol> gIOPMSettingSleepServiceWakeCalendarKey;
1491 static OSSharedPtr<const OSSymbol> gIOPMSettingSilentRunningKey;
1492 static OSSharedPtr<const OSSymbol> gIOPMUserTriggeredFullWakeKey;
1493 static OSSharedPtr<const OSSymbol> gIOPMUserIsActiveKey;
1494 static OSSharedPtr<const OSSymbol> gIOPMSettingLowLatencyAudioModeKey;
1495
1496 //******************************************************************************
1497 // start
1498 //
1499 //******************************************************************************
1500
1501 #define kRootDomainSettingsCount 20
1502 #define kRootDomainNoPublishSettingsCount 4
1503
1504 bool
start(IOService * nub)1505 IOPMrootDomain::start( IOService * nub )
1506 {
1507 OSSharedPtr<OSIterator> psIterator;
1508 OSSharedPtr<OSDictionary> tmpDict;
1509
1510 super::start(nub);
1511
1512 gRootDomain = this;
1513 gIOPMSettingAutoWakeCalendarKey = OSSymbol::withCString(kIOPMSettingAutoWakeCalendarKey);
1514 gIOPMSettingAutoWakeSecondsKey = OSSymbol::withCString(kIOPMSettingAutoWakeSecondsKey);
1515 gIOPMSettingAutoPowerCalendarKey = OSSymbol::withCString(kIOPMSettingAutoPowerCalendarKey);
1516 gIOPMSettingAutoPowerSecondsKey = OSSymbol::withCString(kIOPMSettingAutoPowerSecondsKey);
1517 gIOPMSettingDebugWakeRelativeKey = OSSymbol::withCString(kIOPMSettingDebugWakeRelativeKey);
1518 gIOPMSettingDebugPowerRelativeKey = OSSymbol::withCString(kIOPMSettingDebugPowerRelativeKey);
1519 gIOPMSettingMaintenanceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingMaintenanceWakeCalendarKey);
1520 gIOPMSettingSleepServiceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingSleepServiceWakeCalendarKey);
1521 gIOPMSettingSilentRunningKey = OSSymbol::withCStringNoCopy(kIOPMSettingSilentRunningKey);
1522 gIOPMUserTriggeredFullWakeKey = OSSymbol::withCStringNoCopy(kIOPMUserTriggeredFullWakeKey);
1523 gIOPMUserIsActiveKey = OSSymbol::withCStringNoCopy(kIOPMUserIsActiveKey);
1524 gIOPMSettingLowLatencyAudioModeKey = OSSymbol::withCStringNoCopy(kIOPMSettingLowLatencyAudioModeKey);
1525
1526 gIOPMStatsResponseTimedOut = OSSymbol::withCString(kIOPMStatsResponseTimedOut);
1527 gIOPMStatsResponseCancel = OSSymbol::withCString(kIOPMStatsResponseCancel);
1528 gIOPMStatsResponseSlow = OSSymbol::withCString(kIOPMStatsResponseSlow);
1529 gIOPMStatsResponsePrompt = OSSymbol::withCString(kIOPMStatsResponsePrompt);
1530 gIOPMStatsDriverPSChangeSlow = OSSymbol::withCString(kIOPMStatsDriverPSChangeSlow);
1531
1532 sleepSupportedPEFunction = OSSymbol::withCString("IOPMSetSleepSupported");
1533 sleepMessagePEFunction = OSSymbol::withCString("IOPMSystemSleepMessage");
1534 gIOPMWakeTypeUserKey = OSSymbol::withCStringNoCopy(kIOPMRootDomainWakeTypeUser);
1535
1536 OSSharedPtr<const OSSymbol> settingsArr[kRootDomainSettingsCount] =
1537 {
1538 OSSymbol::withCString(kIOPMSettingSleepOnPowerButtonKey),
1539 gIOPMSettingAutoWakeSecondsKey,
1540 gIOPMSettingAutoPowerSecondsKey,
1541 gIOPMSettingAutoWakeCalendarKey,
1542 gIOPMSettingAutoPowerCalendarKey,
1543 gIOPMSettingDebugWakeRelativeKey,
1544 gIOPMSettingDebugPowerRelativeKey,
1545 OSSymbol::withCString(kIOPMSettingWakeOnRingKey),
1546 OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey),
1547 OSSymbol::withCString(kIOPMSettingWakeOnClamshellKey),
1548 OSSymbol::withCString(kIOPMSettingWakeOnACChangeKey),
1549 OSSymbol::withCString(kIOPMSettingTimeZoneOffsetKey),
1550 OSSymbol::withCString(kIOPMSettingDisplaySleepUsesDimKey),
1551 OSSymbol::withCString(kIOPMSettingMobileMotionModuleKey),
1552 OSSymbol::withCString(kIOPMSettingGraphicsSwitchKey),
1553 OSSymbol::withCString(kIOPMStateConsoleShutdown),
1554 OSSymbol::withCString(kIOPMSettingProModeControl),
1555 OSSymbol::withCString(kIOPMSettingProModeDefer),
1556 gIOPMSettingSilentRunningKey,
1557 gIOPMSettingLowLatencyAudioModeKey,
1558 };
1559
1560 OSSharedPtr<const OSSymbol> noPublishSettingsArr[kRootDomainNoPublishSettingsCount] =
1561 {
1562 OSSymbol::withCString(kIOPMSettingProModeControl),
1563 OSSymbol::withCString(kIOPMSettingProModeDefer),
1564 gIOPMSettingSilentRunningKey,
1565 gIOPMSettingLowLatencyAudioModeKey,
1566 };
1567
1568 #if DEVELOPMENT || DEBUG
1569 #if defined(XNU_TARGET_OS_OSX)
1570 PE_parse_boot_argn("darkwake", &gDarkWakeFlags, sizeof(gDarkWakeFlags));
1571 PE_parse_boot_argn("clamshell", &gClamshellFlags, sizeof(gClamshellFlags));
1572 #endif /* defined(XNU_TARGET_OS_OSX) */
1573 #endif /* DEVELOPMENT || DEBUG */
1574
1575 PE_parse_boot_argn("noidle", &gNoIdleFlag, sizeof(gNoIdleFlag));
1576 PE_parse_boot_argn("swd_sleeptimeout", &gSwdSleepTimeout, sizeof(gSwdSleepTimeout));
1577 PE_parse_boot_argn("swd_waketimeout", &gSwdWakeTimeout, sizeof(gSwdWakeTimeout));
1578 PE_parse_boot_argn("swd_timeout", &gSwdSleepWakeTimeout, sizeof(gSwdSleepWakeTimeout));
1579 PE_parse_boot_argn("haltmspanic", &gHaltTimeMaxPanic, sizeof(gHaltTimeMaxPanic));
1580 PE_parse_boot_argn("haltmslog", &gHaltTimeMaxLog, sizeof(gHaltTimeMaxLog));
1581
1582 queue_init(&aggressivesQueue);
1583 aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1584 aggressivesData = OSData::withCapacity(
1585 sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1586
1587 featuresDictLock = IOLockAlloc();
1588 settingsCtrlLock = IOLockAlloc();
1589 wakeEventLock = IOLockAlloc();
1590 gHaltLogLock = IOLockAlloc();
1591 setPMRootDomain(this);
1592
1593 extraSleepTimer = thread_call_allocate(
1594 idleSleepTimerExpired,
1595 (thread_call_param_t) this);
1596
1597 powerButtonDown = thread_call_allocate(
1598 powerButtonDownCallout,
1599 (thread_call_param_t) this);
1600
1601 powerButtonUp = thread_call_allocate(
1602 powerButtonUpCallout,
1603 (thread_call_param_t) this);
1604
1605 diskSyncCalloutEntry = thread_call_allocate(
1606 &disk_sync_callout,
1607 (thread_call_param_t) this);
1608 updateConsoleUsersEntry = thread_call_allocate(
1609 &updateConsoleUsersCallout,
1610 (thread_call_param_t) this);
1611
1612 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1613 fullWakeThreadCall = thread_call_allocate_with_options(
1614 OSMemberFunctionCast(thread_call_func_t, this,
1615 &IOPMrootDomain::fullWakeDelayedWork),
1616 (thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1617 THREAD_CALL_OPTIONS_ONCE);
1618 #endif
1619
1620 setProperty(kIOSleepSupportedKey, true);
1621
1622 bzero(&gPMStats, sizeof(gPMStats));
1623
1624 pmTracer = PMTraceWorker::tracer(this);
1625
1626 pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1627
1628 userDisabledAllSleep = false;
1629 systemBooting = true;
1630 idleSleepEnabled = false;
1631 sleepSlider = 0;
1632 idleSleepTimerPending = false;
1633 wrangler = NULL;
1634 clamshellClosed = false;
1635 clamshellExists = false;
1636 #if DISPLAY_WRANGLER_PRESENT
1637 clamshellDisabled = true;
1638 #else
1639 clamshellDisabled = false;
1640 #endif
1641 clamshellIgnoreClose = false;
1642 acAdaptorConnected = true;
1643 clamshellSleepDisableMask = 0;
1644 gWakeReasonString[0] = '\0';
1645
1646 // Initialize to user active.
1647 // Will never transition to user inactive w/o wrangler.
1648 fullWakeReason = kFullWakeReasonLocalUser;
1649 userIsActive = userWasActive = true;
1650 clock_get_uptime(&gUserActiveAbsTime);
1651 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1652
1653 // Set the default system capabilities at boot.
1654 _currentCapability = kIOPMSystemCapabilityCPU |
1655 kIOPMSystemCapabilityGraphics |
1656 kIOPMSystemCapabilityAudio |
1657 kIOPMSystemCapabilityNetwork;
1658
1659 _pendingCapability = _currentCapability;
1660 _desiredCapability = _currentCapability;
1661 _highestCapability = _currentCapability;
1662 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1663
1664 queuedSleepWakeUUIDString = NULL;
1665 initializeBootSessionUUID();
1666 pmStatsAppResponses = OSArray::withCapacity(5);
1667 _statsNameKey = OSSymbol::withCString(kIOPMStatsNameKey);
1668 _statsPIDKey = OSSymbol::withCString(kIOPMStatsPIDKey);
1669 _statsTimeMSKey = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1670 _statsResponseTypeKey = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1671 _statsMessageTypeKey = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1672 _statsPowerCapsKey = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1673 assertOnWakeSecs = -1;// Invalid value to prevent updates
1674
1675 pmStatsLock = IOLockAlloc();
1676 idxPMCPUClamshell = kCPUUnknownIndex;
1677 idxPMCPULimitedPower = kCPUUnknownIndex;
1678
1679 tmpDict = OSDictionary::withCapacity(1);
1680 setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1681
1682 // Set a default "SystemPowerProfileOverrideDict" for platform
1683 // drivers without any overrides.
1684 if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1685 tmpDict = OSDictionary::withCapacity(1);
1686 setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1687 }
1688
1689 settingsCallbacks = OSDictionary::withCapacity(1);
1690
1691 // Create a list of the valid PM settings that we'll relay to
1692 // interested clients in setProperties() => setPMSetting()
1693 allowedPMSettings = OSArray::withObjects(
1694 (const OSObject **)settingsArr,
1695 kRootDomainSettingsCount,
1696 0);
1697
1698 // List of PM settings that should not automatically publish itself
1699 // as a feature when registered by a listener.
1700 noPublishPMSettings = OSArray::withObjects(
1701 (const OSObject **)noPublishSettingsArr,
1702 kRootDomainNoPublishSettingsCount,
1703 0);
1704
1705 fPMSettingsDict = OSDictionary::withCapacity(5);
1706 preventIdleSleepList = OSSet::withCapacity(8);
1707 preventSystemSleepList = OSSet::withCapacity(2);
1708
1709 PMinit(); // creates gIOPMWorkLoop
1710 gIOPMWorkLoop = getIOPMWorkloop();
1711
1712 // Create IOPMPowerStateQueue used to queue external power
1713 // events, and to handle those events on the PM work loop.
1714 pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1715 this, OSMemberFunctionCast(IOEventSource::Action, this,
1716 &IOPMrootDomain::dispatchPowerEvent));
1717 gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1718
1719 _aotMode = 0;
1720 _aotTimerES = IOTimerEventSource::timerEventSource(this,
1721 OSMemberFunctionCast(IOTimerEventSource::Action,
1722 this, &IOPMrootDomain::aotEvaluate));
1723 gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1724
1725 // create our power parent
1726 gPatriarch = new IORootParent;
1727 gPatriarch->init();
1728 gPatriarch->attach(this);
1729 gPatriarch->start(this);
1730 gPatriarch->addPowerChild(this);
1731
1732 registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1733 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1734
1735 // install power change handler
1736 gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1737
1738 #if DISPLAY_WRANGLER_PRESENT
1739 wranglerIdleSettings = OSDictionary::withCapacity(1);
1740 OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1741
1742 if (wranglerIdleSettings && wranglerIdlePeriod) {
1743 wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1744 wranglerIdlePeriod.get());
1745 }
1746
1747 #endif /* DISPLAY_WRANGLER_PRESENT */
1748
1749 lowLatencyAudioNotifierDict = OSDictionary::withCapacity(2);
1750 lowLatencyAudioNotifyStateSym = OSSymbol::withCString("LowLatencyAudioNotifyState");
1751 lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1752 lowLatencyAudioNotifyStateVal = OSNumber::withNumber(0ull, 32);
1753 lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1754
1755 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1756 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1757 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1758 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1759 }
1760
1761 OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1762 setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1763
1764 // IOBacklightDisplay can take a long time to load at boot, or it may
1765 // not load at all if you're booting with clamshell closed. We publish
1766 // 'DisplayDims' here redundantly to get it published early and at all.
1767 OSSharedPtr<OSDictionary> matching;
1768 matching = serviceMatching("IOPMPowerSource");
1769 psIterator = getMatchingServices(matching.get());
1770
1771 if (psIterator && psIterator->getNextObject()) {
1772 // There's at least one battery on the system, so we publish
1773 // 'DisplayDims' support for the LCD.
1774 publishFeature("DisplayDims");
1775 }
1776
1777 // read swd_panic boot-arg
1778 PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1779 gWillShutdownSysctlRegistered = true;
1780
1781 #if HIBERNATION
1782 #if defined(__arm64__)
1783 #endif /* defined(__arm64__) */
1784 IOHibernateSystemInit(this);
1785 #endif
1786
1787 registerService(); // let clients find us
1788
1789 return true;
1790 }
1791
1792 //******************************************************************************
1793 // setProperties
1794 //
1795 // Receive a setProperty call
1796 // The "System Boot" property means the system is completely booted.
1797 //******************************************************************************
1798
1799 IOReturn
setProperties(OSObject * props_obj)1800 IOPMrootDomain::setProperties( OSObject * props_obj )
1801 {
1802 IOReturn return_value = kIOReturnSuccess;
1803 OSDictionary *dict = OSDynamicCast(OSDictionary, props_obj);
1804 OSBoolean *b = NULL;
1805 OSNumber *n = NULL;
1806 const OSSymbol *key = NULL;
1807 OSObject *obj = NULL;
1808 OSSharedPtr<OSCollectionIterator> iter;
1809
1810 if (!dict) {
1811 return kIOReturnBadArgument;
1812 }
1813
1814 bool clientEntitled = false;
1815 {
1816 OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1817 clientEntitled = (obj == kOSBooleanTrue);
1818 }
1819
1820 if (!clientEntitled) {
1821 const char * errorSuffix = NULL;
1822
1823 // IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1824 // That API can set 6 possible keys that are checked below.
1825 if ((dict->getCount() == 1) &&
1826 (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1827 dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1828 dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1829 dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1830 dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1831 dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1832 return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1833 if (return_value != kIOReturnSuccess) {
1834 errorSuffix = "privileged";
1835 }
1836 } else {
1837 return_value = kIOReturnNotPermitted;
1838 errorSuffix = "entitled";
1839 }
1840
1841 if (return_value != kIOReturnSuccess) {
1842 OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1843 DLOG("%s failed, process %s is not %s\n", __func__,
1844 procName ? procName->getCStringNoCopy() : "", errorSuffix);
1845 return return_value;
1846 }
1847 }
1848
1849 OSSharedPtr<const OSSymbol> publish_simulated_battery_string = OSSymbol::withCString("SoftwareSimulatedBatteries");
1850 OSSharedPtr<const OSSymbol> boot_complete_string = OSSymbol::withCString("System Boot Complete");
1851 OSSharedPtr<const OSSymbol> sys_shutdown_string = OSSymbol::withCString("System Shutdown");
1852 OSSharedPtr<const OSSymbol> stall_halt_string = OSSymbol::withCString("StallSystemAtHalt");
1853 OSSharedPtr<const OSSymbol> battery_warning_disabled_string = OSSymbol::withCString("BatteryWarningsDisabled");
1854 OSSharedPtr<const OSSymbol> idle_seconds_string = OSSymbol::withCString("System Idle Seconds");
1855 OSSharedPtr<const OSSymbol> idle_milliseconds_string = OSSymbol::withCString("System Idle Milliseconds");
1856 OSSharedPtr<const OSSymbol> sleepdisabled_string = OSSymbol::withCString("SleepDisabled");
1857 OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1858 OSSharedPtr<const OSSymbol> loginwindow_progress_string = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1859 OSSharedPtr<const OSSymbol> coredisplay_progress_string = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1860 OSSharedPtr<const OSSymbol> coregraphics_progress_string = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1861 #if DEBUG || DEVELOPMENT
1862 OSSharedPtr<const OSSymbol> clamshell_close_string = OSSymbol::withCString("IOPMTestClamshellClose");
1863 OSSharedPtr<const OSSymbol> clamshell_open_string = OSSymbol::withCString("IOPMTestClamshellOpen");
1864 OSSharedPtr<const OSSymbol> ac_detach_string = OSSymbol::withCString("IOPMTestACDetach");
1865 OSSharedPtr<const OSSymbol> ac_attach_string = OSSymbol::withCString("IOPMTestACAttach");
1866 OSSharedPtr<const OSSymbol> desktopmode_set_string = OSSymbol::withCString("IOPMTestDesktopModeSet");
1867 OSSharedPtr<const OSSymbol> desktopmode_remove_string = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1868 #endif
1869
1870 #if HIBERNATION
1871 OSSharedPtr<const OSSymbol> hibernatemode_string = OSSymbol::withCString(kIOHibernateModeKey);
1872 OSSharedPtr<const OSSymbol> hibernatefile_string = OSSymbol::withCString(kIOHibernateFileKey);
1873 OSSharedPtr<const OSSymbol> hibernatefilemin_string = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1874 OSSharedPtr<const OSSymbol> hibernatefilemax_string = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1875 OSSharedPtr<const OSSymbol> hibernatefreeratio_string = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1876 OSSharedPtr<const OSSymbol> hibernatefreetime_string = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1877 #endif
1878
1879 iter = OSCollectionIterator::withCollection(dict);
1880 if (!iter) {
1881 return_value = kIOReturnNoMemory;
1882 goto exit;
1883 }
1884
1885 while ((key = (const OSSymbol *) iter->getNextObject()) &&
1886 (obj = dict->getObject(key))) {
1887 if (key->isEqualTo(publish_simulated_battery_string.get())) {
1888 if (OSDynamicCast(OSBoolean, obj)) {
1889 publishResource(key, kOSBooleanTrue);
1890 }
1891 } else if (key->isEqualTo(idle_seconds_string.get())) {
1892 if ((n = OSDynamicCast(OSNumber, obj))) {
1893 setProperty(key, n);
1894 idleMilliSeconds = n->unsigned32BitValue() * 1000;
1895 }
1896 } else if (key->isEqualTo(idle_milliseconds_string.get())) {
1897 if ((n = OSDynamicCast(OSNumber, obj))) {
1898 setProperty(key, n);
1899 idleMilliSeconds = n->unsigned32BitValue();
1900 }
1901 } else if (key->isEqualTo(boot_complete_string.get())) {
1902 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1903 } else if (key->isEqualTo(sys_shutdown_string.get())) {
1904 if ((b = OSDynamicCast(OSBoolean, obj))) {
1905 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1906 }
1907 } else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1908 setProperty(key, obj);
1909 }
1910 #if HIBERNATION
1911 else if (key->isEqualTo(hibernatemode_string.get()) ||
1912 key->isEqualTo(hibernatefilemin_string.get()) ||
1913 key->isEqualTo(hibernatefilemax_string.get()) ||
1914 key->isEqualTo(hibernatefreeratio_string.get()) ||
1915 key->isEqualTo(hibernatefreetime_string.get())) {
1916 if ((n = OSDynamicCast(OSNumber, obj))) {
1917 setProperty(key, n);
1918 }
1919 } else if (key->isEqualTo(hibernatefile_string.get())) {
1920 OSString * str = OSDynamicCast(OSString, obj);
1921 if (str) {
1922 setProperty(key, str);
1923 }
1924 }
1925 #endif
1926 else if (key->isEqualTo(sleepdisabled_string.get())) {
1927 if ((b = OSDynamicCast(OSBoolean, obj))) {
1928 setProperty(key, b);
1929 pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1930 }
1931 } else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1932 obj->retain();
1933 pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1934 } else if (key->isEqualTo(loginwindow_progress_string.get())) {
1935 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1936 uint32_t data = n->unsigned32BitValue();
1937 pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1938 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1939 }
1940 } else if (key->isEqualTo(coredisplay_progress_string.get())) {
1941 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1942 uint32_t data = n->unsigned32BitValue();
1943 pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1944 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1945 }
1946 } else if (key->isEqualTo(coregraphics_progress_string.get())) {
1947 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1948 uint32_t data = n->unsigned32BitValue();
1949 pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1950 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1951 }
1952 } else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1953 key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1954 key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1955 key->isEqualTo(stall_halt_string.get())) {
1956 if ((b = OSDynamicCast(OSBoolean, obj))) {
1957 setProperty(key, b);
1958 }
1959 } else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1960 key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1961 key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1962 key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1963 if ((n = OSDynamicCast(OSNumber, obj))) {
1964 setProperty(key, n);
1965 }
1966 } else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1967 if (kOSBooleanTrue == obj) {
1968 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1969 } else {
1970 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1971 }
1972 DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
1973 }
1974 #if DEBUG || DEVELOPMENT
1975 else if (key->isEqualTo(clamshell_close_string.get())) {
1976 DLOG("SetProperties: setting clamshell close\n");
1977 UInt32 msg = kIOPMClamshellClosed;
1978 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1979 } else if (key->isEqualTo(clamshell_open_string.get())) {
1980 DLOG("SetProperties: setting clamshell open\n");
1981 UInt32 msg = kIOPMClamshellOpened;
1982 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1983 } else if (key->isEqualTo(ac_detach_string.get())) {
1984 DLOG("SetProperties: setting ac detach\n");
1985 UInt32 msg = kIOPMSetACAdaptorConnected;
1986 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1987 } else if (key->isEqualTo(ac_attach_string.get())) {
1988 DLOG("SetProperties: setting ac attach\n");
1989 UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
1990 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1991 } else if (key->isEqualTo(desktopmode_set_string.get())) {
1992 DLOG("SetProperties: setting desktopmode");
1993 UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
1994 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1995 } else if (key->isEqualTo(desktopmode_remove_string.get())) {
1996 DLOG("SetProperties: removing desktopmode\n");
1997 UInt32 msg = kIOPMSetDesktopMode;
1998 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1999 }
2000 #endif
2001 // Relay our allowed PM settings onto our registered PM clients
2002 else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2003 return_value = setPMSetting(key, obj);
2004 if (kIOReturnSuccess != return_value) {
2005 break;
2006 }
2007 } else {
2008 DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2009 }
2010 }
2011
2012 exit:
2013 return return_value;
2014 }
2015
2016 // MARK: -
2017 // MARK: Aggressiveness
2018
2019 //******************************************************************************
2020 // setAggressiveness
2021 //
2022 // Override IOService::setAggressiveness()
2023 //******************************************************************************
2024
2025 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2026 IOPMrootDomain::setAggressiveness(
2027 unsigned long type,
2028 unsigned long value )
2029 {
2030 return setAggressiveness( type, value, 0 );
2031 }
2032
2033 /*
2034 * Private setAggressiveness() with an internal options argument.
2035 */
2036 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2037 IOPMrootDomain::setAggressiveness(
2038 unsigned long type,
2039 unsigned long value,
2040 IOOptionBits options )
2041 {
2042 AggressivesRequest * entry;
2043 AggressivesRequest * request;
2044 bool found = false;
2045
2046 if ((type > UINT_MAX) || (value > UINT_MAX)) {
2047 return kIOReturnBadArgument;
2048 }
2049
2050 if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2051 DLOG("setAggressiveness(%x) %s = %u\n",
2052 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2053 } else {
2054 DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2055 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2056 }
2057
2058 request = IOMallocType(AggressivesRequest);
2059 request->options = options;
2060 request->dataType = kAggressivesRequestTypeRecord;
2061 request->data.record.type = (uint32_t) type;
2062 request->data.record.value = (uint32_t) value;
2063
2064 AGGRESSIVES_LOCK();
2065
2066 // Update disk quick spindown flag used by getAggressiveness().
2067 // Never merge requests with quick spindown flags set.
2068
2069 if (options & kAggressivesOptionQuickSpindownEnable) {
2070 gAggressivesState |= kAggressivesStateQuickSpindown;
2071 } else if (options & kAggressivesOptionQuickSpindownDisable) {
2072 gAggressivesState &= ~kAggressivesStateQuickSpindown;
2073 } else {
2074 // Coalesce requests with identical aggressives types.
2075 // Deal with callers that calls us too "aggressively".
2076
2077 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2078 {
2079 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2080 (entry->data.record.type == type) &&
2081 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2082 entry->data.record.value = (uint32_t) value;
2083 found = true;
2084 break;
2085 }
2086 }
2087 }
2088
2089 if (!found) {
2090 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2091 }
2092
2093 AGGRESSIVES_UNLOCK();
2094
2095 if (found) {
2096 IOFreeType(request, AggressivesRequest);
2097 }
2098
2099 if (options & kAggressivesOptionSynchronous) {
2100 handleAggressivesRequests(); // not truly synchronous
2101 } else {
2102 thread_call_enter(aggressivesThreadCall);
2103 }
2104
2105 return kIOReturnSuccess;
2106 }
2107
2108 //******************************************************************************
2109 // getAggressiveness
2110 //
2111 // Override IOService::setAggressiveness()
2112 // Fetch the aggressiveness factor with the given type.
2113 //******************************************************************************
2114
2115 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2116 IOPMrootDomain::getAggressiveness(
2117 unsigned long type,
2118 unsigned long * outLevel )
2119 {
2120 uint32_t value = 0;
2121 int source = 0;
2122
2123 if (!outLevel || (type > UINT_MAX)) {
2124 return kIOReturnBadArgument;
2125 }
2126
2127 AGGRESSIVES_LOCK();
2128
2129 // Disk quick spindown in effect, report value = 1
2130
2131 if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2132 (type == kPMMinutesToSpinDown)) {
2133 value = kAggressivesMinValue;
2134 source = 1;
2135 }
2136
2137 // Consult the pending request queue.
2138
2139 if (!source) {
2140 AggressivesRequest * entry;
2141
2142 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2143 {
2144 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2145 (entry->data.record.type == type) &&
2146 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2147 value = entry->data.record.value;
2148 source = 2;
2149 break;
2150 }
2151 }
2152 }
2153
2154 // Consult the backend records.
2155
2156 if (!source && aggressivesData) {
2157 AggressivesRecord * record;
2158 int i, count;
2159
2160 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2161 record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2162
2163 for (i = 0; i < count; i++, record++) {
2164 if (record->type == type) {
2165 value = record->value;
2166 source = 3;
2167 break;
2168 }
2169 }
2170 }
2171
2172 AGGRESSIVES_UNLOCK();
2173
2174 if (source) {
2175 *outLevel = (unsigned long) value;
2176 return kIOReturnSuccess;
2177 } else {
2178 DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2179 *outLevel = 0; // default return = 0, driver may not check for error
2180 return kIOReturnInvalid;
2181 }
2182 }
2183
2184 //******************************************************************************
2185 // joinAggressiveness
2186 //
2187 // Request from IOService to join future aggressiveness broadcasts.
2188 //******************************************************************************
2189
2190 IOReturn
joinAggressiveness(IOService * service)2191 IOPMrootDomain::joinAggressiveness(
2192 IOService * service )
2193 {
2194 AggressivesRequest * request;
2195
2196 if (!service || (service == this)) {
2197 return kIOReturnBadArgument;
2198 }
2199
2200 DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2201
2202 request = IOMallocType(AggressivesRequest);
2203 request->dataType = kAggressivesRequestTypeService;
2204 request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2205
2206 AGGRESSIVES_LOCK();
2207 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2208 AGGRESSIVES_UNLOCK();
2209
2210 thread_call_enter(aggressivesThreadCall);
2211
2212 return kIOReturnSuccess;
2213 }
2214
2215 //******************************************************************************
2216 // handleAggressivesRequests
2217 //
2218 // Backend thread processes all incoming aggressiveness requests in the queue.
2219 //******************************************************************************
2220
2221 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2222 handleAggressivesFunction(
2223 thread_call_param_t param1,
2224 thread_call_param_t param2 )
2225 {
2226 if (param1) {
2227 ((IOPMrootDomain *) param1)->handleAggressivesRequests();
2228 }
2229 }
2230
2231 void
handleAggressivesRequests(void)2232 IOPMrootDomain::handleAggressivesRequests( void )
2233 {
2234 AggressivesRecord * start;
2235 AggressivesRecord * record;
2236 AggressivesRequest * request;
2237 queue_head_t joinedQueue;
2238 int i, count;
2239 bool broadcast;
2240 bool found;
2241 bool pingSelf = false;
2242
2243 AGGRESSIVES_LOCK();
2244
2245 if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2246 queue_empty(&aggressivesQueue)) {
2247 goto unlock_done;
2248 }
2249
2250 gAggressivesState |= kAggressivesStateBusy;
2251 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2252 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2253
2254 do{
2255 broadcast = false;
2256 queue_init(&joinedQueue);
2257
2258 do{
2259 // Remove request from the incoming queue in FIFO order.
2260 queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2261 switch (request->dataType) {
2262 case kAggressivesRequestTypeRecord:
2263 // Update existing record if found.
2264 found = false;
2265 for (i = 0, record = start; i < count; i++, record++) {
2266 if (record->type == request->data.record.type) {
2267 found = true;
2268
2269 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2270 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2271 broadcast = true;
2272 record->flags |= (kAggressivesRecordFlagMinValue |
2273 kAggressivesRecordFlagModified);
2274 DLOG("disk spindown accelerated, was %u min\n",
2275 record->value);
2276 }
2277 } else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2278 if (record->flags & kAggressivesRecordFlagMinValue) {
2279 broadcast = true;
2280 record->flags |= kAggressivesRecordFlagModified;
2281 record->flags &= ~kAggressivesRecordFlagMinValue;
2282 DLOG("disk spindown restored to %u min\n",
2283 record->value);
2284 }
2285 } else if (record->value != request->data.record.value) {
2286 record->value = request->data.record.value;
2287 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2288 broadcast = true;
2289 record->flags |= kAggressivesRecordFlagModified;
2290 }
2291 }
2292 break;
2293 }
2294 }
2295
2296 // No matching record, append a new record.
2297 if (!found &&
2298 ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2299 AggressivesRecord newRecord;
2300
2301 newRecord.flags = kAggressivesRecordFlagModified;
2302 newRecord.type = request->data.record.type;
2303 newRecord.value = request->data.record.value;
2304 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2305 newRecord.flags |= kAggressivesRecordFlagMinValue;
2306 DLOG("disk spindown accelerated\n");
2307 }
2308
2309 aggressivesData->appendBytes(&newRecord, sizeof(newRecord));
2310
2311 // OSData may have switched to another (larger) buffer.
2312 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2313 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2314 broadcast = true;
2315 }
2316
2317 // Finished processing the request, release it.
2318 IOFreeType(request, AggressivesRequest);
2319 break;
2320
2321 case kAggressivesRequestTypeService:
2322 // synchronizeAggressives() will free request.
2323 queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2324 break;
2325
2326 default:
2327 panic("bad aggressives request type %x", request->dataType);
2328 break;
2329 }
2330 } while (!queue_empty(&aggressivesQueue));
2331
2332 // Release the lock to perform work, with busy flag set.
2333 if (!queue_empty(&joinedQueue) || broadcast) {
2334 AGGRESSIVES_UNLOCK();
2335 if (!queue_empty(&joinedQueue)) {
2336 synchronizeAggressives(&joinedQueue, start, count);
2337 }
2338 if (broadcast) {
2339 broadcastAggressives(start, count);
2340 }
2341 AGGRESSIVES_LOCK();
2342 }
2343
2344 // Remove the modified flag from all records.
2345 for (i = 0, record = start; i < count; i++, record++) {
2346 if ((record->flags & kAggressivesRecordFlagModified) &&
2347 ((record->type == kPMMinutesToDim) ||
2348 (record->type == kPMMinutesToSleep))) {
2349 pingSelf = true;
2350 }
2351
2352 record->flags &= ~kAggressivesRecordFlagModified;
2353 }
2354
2355 // Check the incoming queue again since new entries may have been
2356 // added while lock was released above.
2357 } while (!queue_empty(&aggressivesQueue));
2358
2359 gAggressivesState &= ~kAggressivesStateBusy;
2360
2361 unlock_done:
2362 AGGRESSIVES_UNLOCK();
2363
2364 // Root domain is interested in system and display sleep slider changes.
2365 // Submit a power event to handle those changes on the PM work loop.
2366
2367 if (pingSelf && pmPowerStateQueue) {
2368 pmPowerStateQueue->submitPowerEvent(
2369 kPowerEventPolicyStimulus,
2370 (void *) kStimulusAggressivenessChanged );
2371 }
2372 }
2373
2374 //******************************************************************************
2375 // synchronizeAggressives
2376 //
2377 // Push all known aggressiveness records to one or more IOService.
2378 //******************************************************************************
2379
2380 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2381 IOPMrootDomain::synchronizeAggressives(
2382 queue_head_t * joinedQueue,
2383 const AggressivesRecord * array,
2384 int count )
2385 {
2386 OSSharedPtr<IOService> service;
2387 AggressivesRequest * request;
2388 const AggressivesRecord * record;
2389 IOPMDriverCallEntry callEntry;
2390 uint32_t value;
2391 int i;
2392
2393 while (!queue_empty(joinedQueue)) {
2394 queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2395 if (request->dataType == kAggressivesRequestTypeService) {
2396 // retained by joinAggressiveness(), so take ownership
2397 service = os::move(request->data.service);
2398 } else {
2399 service.reset();
2400 }
2401
2402 IOFreeType(request, AggressivesRequest);
2403 request = NULL;
2404
2405 if (service) {
2406 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2407 for (i = 0, record = array; i < count; i++, record++) {
2408 value = record->value;
2409 if (record->flags & kAggressivesRecordFlagMinValue) {
2410 value = kAggressivesMinValue;
2411 }
2412
2413 _LOG("synchronizeAggressives 0x%x = %u to %s\n",
2414 record->type, value, service->getName());
2415 service->setAggressiveness(record->type, value);
2416 }
2417 service->deassertPMDriverCall(&callEntry);
2418 }
2419 }
2420 }
2421 }
2422
2423 //******************************************************************************
2424 // broadcastAggressives
2425 //
2426 // Traverse PM tree and call setAggressiveness() for records that have changed.
2427 //******************************************************************************
2428
2429 void
broadcastAggressives(const AggressivesRecord * array,int count)2430 IOPMrootDomain::broadcastAggressives(
2431 const AggressivesRecord * array,
2432 int count )
2433 {
2434 OSSharedPtr<IORegistryIterator> iter;
2435 IORegistryEntry *entry;
2436 OSSharedPtr<IORegistryEntry> child;
2437 IOPowerConnection *connect;
2438 IOService *service;
2439 const AggressivesRecord *record;
2440 IOPMDriverCallEntry callEntry;
2441 uint32_t value;
2442 int i;
2443
2444 iter = IORegistryIterator::iterateOver(
2445 this, gIOPowerPlane, kIORegistryIterateRecursively);
2446 if (iter) {
2447 do{
2448 // !! reset the iterator
2449 iter->reset();
2450 while ((entry = iter->getNextObject())) {
2451 connect = OSDynamicCast(IOPowerConnection, entry);
2452 if (!connect || !connect->getReadyFlag()) {
2453 continue;
2454 }
2455
2456 child = connect->copyChildEntry(gIOPowerPlane);
2457 if (child) {
2458 if ((service = OSDynamicCast(IOService, child.get()))) {
2459 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2460 for (i = 0, record = array; i < count; i++, record++) {
2461 if (record->flags & kAggressivesRecordFlagModified) {
2462 value = record->value;
2463 if (record->flags & kAggressivesRecordFlagMinValue) {
2464 value = kAggressivesMinValue;
2465 }
2466 _LOG("broadcastAggressives %x = %u to %s\n",
2467 record->type, value, service->getName());
2468 service->setAggressiveness(record->type, value);
2469 }
2470 }
2471 service->deassertPMDriverCall(&callEntry);
2472 }
2473 }
2474 }
2475 }
2476 }while (!entry && !iter->isValid());
2477 }
2478 }
2479
2480 //*****************************************
2481 // stackshot on power button press
2482 // ***************************************
2483 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2484 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2485 {
2486 /* Power button pressed during wake
2487 * Take a stackshot
2488 */
2489 DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2490 ((IOPMrootDomain *)us)->takeStackshot(false);
2491 }
2492
2493 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2494 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2495 {
2496 /* Power button released.
2497 * Delete any stackshot data
2498 */
2499 DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2500 ((IOPMrootDomain *)us)->deleteStackshot();
2501 }
2502 //*************************************************************************
2503 //
2504
2505 // MARK: -
2506 // MARK: System Sleep
2507
2508 //******************************************************************************
2509 // startIdleSleepTimer
2510 //
2511 //******************************************************************************
2512
2513 void
startIdleSleepTimer(uint32_t inMilliSeconds)2514 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2515 {
2516 AbsoluteTime deadline;
2517
2518 ASSERT_GATED();
2519 if (gNoIdleFlag) {
2520 DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2521 return;
2522 }
2523 if (inMilliSeconds) {
2524 clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2525 thread_call_enter_delayed(extraSleepTimer, deadline);
2526 idleSleepTimerPending = true;
2527 } else {
2528 thread_call_enter(extraSleepTimer);
2529 }
2530 DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2531 }
2532
2533 //******************************************************************************
2534 // cancelIdleSleepTimer
2535 //
2536 //******************************************************************************
2537
2538 void
cancelIdleSleepTimer(void)2539 IOPMrootDomain::cancelIdleSleepTimer( void )
2540 {
2541 ASSERT_GATED();
2542 if (idleSleepTimerPending) {
2543 DLOG("idle timer cancelled\n");
2544 thread_call_cancel(extraSleepTimer);
2545 idleSleepTimerPending = false;
2546
2547 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2548 AbsoluteTime now;
2549 clock_usec_t microsecs;
2550 clock_get_uptime(&now);
2551 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2552 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
2553 if (assertOnWakeReport) {
2554 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2555 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2556 }
2557 }
2558 }
2559 }
2560
2561 //******************************************************************************
2562 // idleSleepTimerExpired
2563 //
2564 //******************************************************************************
2565
2566 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2567 idleSleepTimerExpired(
2568 thread_call_param_t us, thread_call_param_t )
2569 {
2570 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2571 }
2572
2573 //******************************************************************************
2574 // handleSleepTimerExpiration
2575 //
2576 // The time between the sleep idle timeout and the next longest one has elapsed.
2577 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2578 //******************************************************************************
2579
2580 void
handleSleepTimerExpiration(void)2581 IOPMrootDomain::handleSleepTimerExpiration( void )
2582 {
2583 if (!gIOPMWorkLoop->inGate()) {
2584 gIOPMWorkLoop->runAction(
2585 OSMemberFunctionCast(IOWorkLoop::Action, this,
2586 &IOPMrootDomain::handleSleepTimerExpiration),
2587 this);
2588 return;
2589 }
2590
2591 DLOG("sleep timer expired\n");
2592 ASSERT_GATED();
2593
2594 idleSleepTimerPending = false;
2595 setQuickSpinDownTimeout();
2596 adjustPowerState(true);
2597 }
2598
2599 //******************************************************************************
2600 // getTimeToIdleSleep
2601 //
2602 // Returns number of milliseconds left before going into idle sleep.
2603 // Caller has to make sure that idle sleep is allowed at the time of calling
2604 // this function
2605 //******************************************************************************
2606
2607 uint32_t
getTimeToIdleSleep(void)2608 IOPMrootDomain::getTimeToIdleSleep( void )
2609 {
2610 AbsoluteTime now, lastActivityTime;
2611 uint64_t nanos;
2612 uint32_t minutesSinceUserInactive = 0;
2613 uint32_t sleepDelay = 0;
2614
2615 if (!idleSleepEnabled) {
2616 return 0xffffffff;
2617 }
2618
2619 if (userActivityTime) {
2620 lastActivityTime = userActivityTime;
2621 } else {
2622 lastActivityTime = userBecameInactiveTime;
2623 }
2624
2625 // Ignore any lastActivityTime that predates the last system wake.
2626 // The goal is to avoid a sudden idle sleep right after a dark wake
2627 // due to sleepDelay=0 computed below. The alternative 60s minimum
2628 // timeout should be large enough to allow dark wake to complete,
2629 // at which point the idle timer will be promptly cancelled.
2630 clock_get_uptime(&now);
2631 if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2632 (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2633 SUB_ABSOLUTETIME(&now, &lastActivityTime);
2634 absolutetime_to_nanoseconds(now, &nanos);
2635 minutesSinceUserInactive = nanos / (60000000000ULL);
2636
2637 if (minutesSinceUserInactive >= sleepSlider) {
2638 sleepDelay = 0;
2639 } else {
2640 sleepDelay = sleepSlider - minutesSinceUserInactive;
2641 }
2642 } else {
2643 DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2644 lastActivityTime, now, gIOLastWakeAbsTime);
2645 sleepDelay = sleepSlider;
2646 }
2647
2648 DLOG("user inactive %u min, time to idle sleep %u min\n",
2649 minutesSinceUserInactive, sleepDelay);
2650
2651 return sleepDelay * 60 * 1000;
2652 }
2653
2654 //******************************************************************************
2655 // setQuickSpinDownTimeout
2656 //
2657 //******************************************************************************
2658
2659 void
setQuickSpinDownTimeout(void)2660 IOPMrootDomain::setQuickSpinDownTimeout( void )
2661 {
2662 ASSERT_GATED();
2663 setAggressiveness(
2664 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2665 }
2666
2667 //******************************************************************************
2668 // restoreUserSpinDownTimeout
2669 //
2670 //******************************************************************************
2671
2672 void
restoreUserSpinDownTimeout(void)2673 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2674 {
2675 ASSERT_GATED();
2676 setAggressiveness(
2677 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2678 }
2679
2680 //******************************************************************************
2681 // sleepSystem
2682 //
2683 //******************************************************************************
2684
2685 /* public */
2686 IOReturn
sleepSystem(void)2687 IOPMrootDomain::sleepSystem( void )
2688 {
2689 return sleepSystemOptions(NULL);
2690 }
2691
2692 /* private */
2693 IOReturn
sleepSystemOptions(OSDictionary * options)2694 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2695 {
2696 OSObject *obj = NULL;
2697 OSString *reason = NULL;
2698 /* sleepSystem is a public function, and may be called by any kernel driver.
2699 * And that's bad - drivers should sleep the system by calling
2700 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2701 *
2702 * Note that user space app calls to IOPMSleepSystem() will also travel
2703 * this code path and thus be correctly identified as software sleeps.
2704 */
2705
2706 if (options && options->getObject("OSSwitch")) {
2707 // Log specific sleep cause for OS Switch hibernation
2708 return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2709 }
2710
2711 if (options && (obj = options->getObject("Sleep Reason"))) {
2712 reason = OSDynamicCast(OSString, obj);
2713 if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2714 return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2715 }
2716 if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2717 return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2718 }
2719 }
2720
2721 return privateSleepSystem( kIOPMSleepReasonSoftware);
2722 }
2723
2724 /* private */
2725 IOReturn
privateSleepSystem(uint32_t sleepReason)2726 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2727 {
2728 /* Called from both gated and non-gated context */
2729
2730 if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2731 return kIOReturnNotPermitted;
2732 }
2733
2734 pmPowerStateQueue->submitPowerEvent(
2735 kPowerEventPolicyStimulus,
2736 (void *) kStimulusDemandSystemSleep,
2737 sleepReason);
2738
2739 return kIOReturnSuccess;
2740 }
2741
2742 //******************************************************************************
2743 // powerChangeDone
2744 //
2745 // This overrides powerChangeDone in IOService.
2746 //******************************************************************************
2747 void
powerChangeDone(unsigned long previousPowerState)2748 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2749 {
2750 #if !__i386__ && !__x86_64__
2751 uint64_t timeSinceReset = 0;
2752 #endif
2753 uint64_t now;
2754 unsigned long newState;
2755 clock_sec_t secs;
2756 clock_usec_t microsecs;
2757 uint32_t lastDebugWakeSeconds;
2758 clock_sec_t adjWakeTime;
2759 IOPMCalendarStruct nowCalendar;
2760
2761 ASSERT_GATED();
2762 newState = getPowerState();
2763 DLOG("PowerChangeDone: %s->%s\n",
2764 getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2765
2766 if (previousPowerState == newState) {
2767 return;
2768 }
2769
2770 notifierThread = current_thread();
2771 switch (getPowerState()) {
2772 case SLEEP_STATE: {
2773 if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2774 secs = 0;
2775 microsecs = 0;
2776 PEGetUTCTimeOfDay(&secs, µsecs);
2777
2778 adjWakeTime = 0;
2779 if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2780 IOLog("use _calendarWakeAlarmUTC\n");
2781 adjWakeTime = _calendarWakeAlarmUTC;
2782 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2783 IOLog("accelerate _aotWakeTime for exit\n");
2784 adjWakeTime = secs;
2785 } else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2786 IOLog("accelerate _aotWakeTime for assertion\n");
2787 adjWakeTime = secs;
2788 }
2789 if (adjWakeTime) {
2790 IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2791 }
2792
2793 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2794 IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2795
2796 IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2797 assert(kIOReturnSuccess == ret);
2798 }
2799 if (_aotLastWakeTime) {
2800 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2801 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2802 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2803 gWakeReasonString,
2804 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2805 }
2806 }
2807 _aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2808 if (_aotTimerScheduled) {
2809 _aotTimerES->cancelTimeout();
2810 _aotTimerScheduled = false;
2811 }
2812 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2813
2814 // re-enable this timer for next sleep
2815 cancelIdleSleepTimer();
2816
2817 if (clamshellExists) {
2818 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2819 if (gClamshellFlags & kClamshell_WAR_58009435) {
2820 // Disable clamshell sleep until system has completed full wake.
2821 // This prevents a system sleep request (due to a clamshell close)
2822 // from being queued until the end of system full wake - even if
2823 // other clamshell disable bits outside of our control is wrong.
2824 setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2825 }
2826 #endif
2827
2828 // Log the last known clamshell state before system sleep
2829 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2830 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2831 desktopMode, acAdaptorConnected);
2832 }
2833
2834 clock_get_calendar_absolute_and_microtime(&secs, µsecs, &now);
2835 logtime(secs);
2836 gIOLastSleepTime.tv_sec = secs;
2837 gIOLastSleepTime.tv_usec = microsecs;
2838 if (!_aotLastWakeTime) {
2839 gIOLastUserSleepTime = gIOLastSleepTime;
2840 }
2841
2842 gIOLastWakeTime.tv_sec = 0;
2843 gIOLastWakeTime.tv_usec = 0;
2844 gIOLastSleepAbsTime = now;
2845
2846 if (wake2DarkwakeDelay && sleepDelaysReport) {
2847 clock_sec_t wake2DarkwakeSecs, darkwake2SleepSecs;
2848 // Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2849
2850 SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2851 absolutetime_to_microtime(now, &darkwake2SleepSecs, µsecs);
2852 absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, µsecs);
2853 HISTREPORT_TALLYVALUE(sleepDelaysReport,
2854 (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2855
2856 DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2857 wake2DarkwakeDelay = 0;
2858 }
2859 #if HIBERNATION
2860 LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2861
2862 IOHibernateSystemHasSlept();
2863
2864 evaluateSystemSleepPolicyFinal();
2865 #else
2866 LOG("System Sleep\n");
2867 #endif
2868 if (thermalWarningState) {
2869 OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2870 if (event) {
2871 systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2872 }
2873 }
2874 assertOnWakeSecs = 0;
2875 lowBatteryCondition = false;
2876 thermalEmergencyState = false;
2877
2878 #if DEVELOPMENT || DEBUG
2879 extern int g_should_log_clock_adjustments;
2880 if (g_should_log_clock_adjustments) {
2881 clock_sec_t secs = 0;
2882 clock_usec_t microsecs = 0;
2883 uint64_t now_b = mach_absolute_time();
2884
2885 secs = 0;
2886 microsecs = 0;
2887 PEGetUTCTimeOfDay(&secs, µsecs);
2888
2889 uint64_t now_a = mach_absolute_time();
2890 os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2891 __func__, (unsigned long)secs, microsecs, now_b, now_a);
2892 }
2893 #endif
2894
2895 getPlatform()->sleepKernel();
2896
2897 // The CPU(s) are off at this point,
2898 // Code will resume execution here upon wake.
2899
2900 clock_get_uptime(&gIOLastWakeAbsTime);
2901 IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2902 _highestCapability = 0;
2903
2904 #if HIBERNATION
2905 IOHibernateSystemWake();
2906 #endif
2907
2908 // sleep transition complete
2909 gSleepOrShutdownPending = 0;
2910
2911 // trip the reset of the calendar clock
2912 clock_wakeup_calendar();
2913 clock_get_calendar_microtime(&secs, µsecs);
2914 gIOLastWakeTime.tv_sec = secs;
2915 gIOLastWakeTime.tv_usec = microsecs;
2916
2917 // aot
2918 if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2919 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2920 secs = 0;
2921 microsecs = 0;
2922 PEGetUTCTimeOfDay(&secs, µsecs);
2923 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2924 IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2925 _aotMetrics->sleepCount++;
2926 _aotLastWakeTime = gIOLastWakeAbsTime;
2927 if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2928 _aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
2929 = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
2930 _aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
2931 = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
2932 }
2933
2934 if (_aotTestTime) {
2935 if (_aotWakeTimeUTC <= secs) {
2936 _aotTestTime = _aotTestTime + _aotTestInterval;
2937 }
2938 setWakeTime(_aotTestTime);
2939 }
2940 }
2941
2942 #if HIBERNATION
2943 LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
2944 #endif
2945
2946 lastSleepReason = 0;
2947
2948 lastDebugWakeSeconds = _debugWakeSeconds;
2949 _debugWakeSeconds = 0;
2950 _scheduledAlarmMask = 0;
2951 _nextScheduledAlarmType = NULL;
2952
2953 darkWakeExit = false;
2954 darkWakePowerClamped = false;
2955 darkWakePostTickle = false;
2956 darkWakeHibernateError = false;
2957 darkWakeToSleepASAP = true;
2958 darkWakeLogClamp = true;
2959 sleepTimerMaintenance = false;
2960 sleepToStandby = false;
2961 wranglerTickled = false;
2962 userWasActive = false;
2963 isRTCAlarmWake = false;
2964 clamshellIgnoreClose = false;
2965 fullWakeReason = kFullWakeReasonNone;
2966
2967 #if defined(__i386__) || defined(__x86_64__)
2968 kdebugTrace(kPMLogSystemWake, 0, 0, 0);
2969
2970 OSSharedPtr<OSObject> wakeTypeProp = copyProperty(kIOPMRootDomainWakeTypeKey);
2971 OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
2972 OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
2973 OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
2974
2975 if (wakeReason && (wakeReason->getLength() >= 2) &&
2976 gWakeReasonString[0] == '\0') {
2977 WAKEEVENT_LOCK();
2978 // Until the platform driver can claim its wake reasons
2979 strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
2980 sizeof(gWakeReasonString));
2981 if (!gWakeReasonSysctlRegistered) {
2982 gWakeReasonSysctlRegistered = true;
2983 }
2984 WAKEEVENT_UNLOCK();
2985 }
2986
2987 if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
2988 lowBatteryCondition = true;
2989 darkWakeMaintenance = true;
2990 } else {
2991 #if HIBERNATION
2992 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
2993 OSNumber * hibOptions = OSDynamicCast( OSNumber, hibOptionsProp.get());
2994 if (hibernateAborted || ((hibOptions &&
2995 !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
2996 // Hibernate aborted, or EFI brought up graphics
2997 darkWakeExit = true;
2998 if (hibernateAborted) {
2999 DLOG("Hibernation aborted\n");
3000 } else {
3001 DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3002 }
3003 } else
3004 #endif
3005 if (wakeType && (
3006 wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3007 wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3008 // User wake or RTC alarm
3009 darkWakeExit = true;
3010 if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3011 isRTCAlarmWake = true;
3012 }
3013 } else if (wakeType &&
3014 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3015 // SMC standby timer trumps SleepX
3016 darkWakeMaintenance = true;
3017 sleepTimerMaintenance = true;
3018 } else if ((lastDebugWakeSeconds != 0) &&
3019 ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3020 // SleepX before maintenance
3021 darkWakeExit = true;
3022 } else if (wakeType &&
3023 wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3024 darkWakeMaintenance = true;
3025 } else if (wakeType &&
3026 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3027 darkWakeMaintenance = true;
3028 darkWakeSleepService = true;
3029 #if HIBERNATION
3030 if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3031 sleepToStandby = true;
3032 }
3033 #endif
3034 } else if (wakeType &&
3035 wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3036 darkWakeMaintenance = true;
3037 darkWakeHibernateError = true;
3038 } else {
3039 // Unidentified wake source, resume to full wake if debug
3040 // alarm is pending.
3041
3042 if (lastDebugWakeSeconds &&
3043 (!wakeReason || wakeReason->isEqualTo(""))) {
3044 darkWakeExit = true;
3045 }
3046 }
3047 }
3048
3049 if (darkWakeExit) {
3050 darkWakeToSleepASAP = false;
3051 fullWakeReason = kFullWakeReasonLocalUser;
3052 reportUserInput();
3053 } else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3054 handleSetDisplayPowerOn(true);
3055 } else if (!darkWakeMaintenance) {
3056 // Early/late tickle for non-maintenance wake.
3057 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3058 darkWakePostTickle = true;
3059 }
3060 }
3061 #else /* !__i386__ && !__x86_64__ */
3062 timeSinceReset = ml_get_time_since_reset();
3063 kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3064
3065 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3066 wranglerTickled = true;
3067 fullWakeReason = kFullWakeReasonLocalUser;
3068 requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3069 } else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3070 isRTCAlarmWake = true;
3071 fullWakeReason = kFullWakeReasonLocalUser;
3072 requestUserActive(this, "RTC debug alarm");
3073 } else {
3074 #if HIBERNATION
3075 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3076 OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3077 if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3078 fullWakeReason = kFullWakeReasonLocalUser;
3079 requestUserActive(this, "hibernate user wake");
3080 }
3081 #endif
3082 }
3083
3084 // stay awake for at least 30 seconds
3085 startIdleSleepTimer(30 * 1000);
3086 #endif
3087 sleepCnt++;
3088
3089 thread_call_enter(updateConsoleUsersEntry);
3090
3091 // Skip AOT_STATE if we are waking up from an RTC timer.
3092 // This check needs to be done after the epoch change is processed
3093 // and before the changePowerStateWithTagToPriv() call below.
3094 WAKEEVENT_LOCK();
3095 aotShouldExit(false, false);
3096 WAKEEVENT_UNLOCK();
3097
3098 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3099 break;
3100 }
3101 #if !__i386__ && !__x86_64__
3102 case ON_STATE:
3103 case AOT_STATE:
3104 {
3105 DLOG("Force re-evaluating aggressiveness\n");
3106 /* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3107 pmPowerStateQueue->submitPowerEvent(
3108 kPowerEventPolicyStimulus,
3109 (void *) kStimulusNoIdleSleepPreventers );
3110
3111 // After changing to ON_STATE, invalidate any previously queued
3112 // request to change to a state less than ON_STATE. This isn't
3113 // necessary for AOT_STATE or if the device has only one running
3114 // state since the changePowerStateToPriv() issued at the tail
3115 // end of SLEEP_STATE case should take care of that.
3116 if (getPowerState() == ON_STATE) {
3117 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3118 }
3119 break;
3120 }
3121 #endif /* !__i386__ && !__x86_64__ */
3122 }
3123 notifierThread = NULL;
3124 }
3125
3126 //******************************************************************************
3127 // requestPowerDomainState
3128 //
3129 // Extend implementation in IOService. Running on PM work loop thread.
3130 //******************************************************************************
3131
3132 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3133 IOPMrootDomain::requestPowerDomainState(
3134 IOPMPowerFlags childDesire,
3135 IOPowerConnection * childConnection,
3136 unsigned long specification )
3137 {
3138 // Idle and system sleep prevention flags affects driver desire.
3139 // Children desire are irrelevant so they are cleared.
3140
3141 return super::requestPowerDomainState(0, childConnection, specification);
3142 }
3143
3144
3145 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3146 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3147 {
3148 if (!preventers->getCount()) {
3149 return;
3150 }
3151
3152 char *buf_iter = buf + strlen(buf);
3153 char *buf_end = buf + buf_size;
3154
3155 OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3156 OSObject *obj = NULL;
3157
3158 while ((obj = iterator->getNextObject())) {
3159 IOService *srv = OSDynamicCast(IOService, obj);
3160 if (buf_iter < buf_end) {
3161 buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3162 } else {
3163 DLOG("Print buffer exhausted for sleep preventers list\n");
3164 break;
3165 }
3166 }
3167 }
3168
3169 //******************************************************************************
3170 // updatePreventIdleSleepList
3171 //
3172 // Called by IOService on PM work loop.
3173 // Returns true if PM policy recognized the driver's desire to prevent idle
3174 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3175 //******************************************************************************
3176
3177 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3178 IOPMrootDomain::updatePreventIdleSleepList(
3179 IOService * service, bool addNotRemove)
3180 {
3181 unsigned int oldCount;
3182
3183 oldCount = idleSleepPreventersCount();
3184 return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3185 }
3186
3187 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3188 IOPMrootDomain::updatePreventIdleSleepListInternal(
3189 IOService * service, bool addNotRemove, unsigned int oldCount)
3190 {
3191 unsigned int newCount;
3192
3193 ASSERT_GATED();
3194
3195 #if defined(XNU_TARGET_OS_OSX)
3196 // Only the display wrangler and no-idle-sleep kernel assertions
3197 // can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3198 // reported by drivers in their power state table is ignored.
3199 if (service && (service != wrangler) && (service != this)) {
3200 return false;
3201 }
3202 #endif
3203
3204 if (service) {
3205 if (addNotRemove) {
3206 preventIdleSleepList->setObject(service);
3207 DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3208 service->getName(), preventIdleSleepList->getCount());
3209 } else if (preventIdleSleepList->member(service)) {
3210 preventIdleSleepList->removeObject(service);
3211 DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3212 service->getName(), preventIdleSleepList->getCount());
3213 }
3214
3215 if (preventIdleSleepList->getCount()) {
3216 char buf[256] = "Idle Sleep Preventers:";
3217 makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3218 DLOG("%s\n", buf);
3219 }
3220 }
3221
3222 newCount = idleSleepPreventersCount();
3223
3224 if ((oldCount == 0) && (newCount != 0)) {
3225 // Driver added to empty prevent list.
3226 // Update the driver desire to prevent idle sleep.
3227 // Driver desire does not prevent demand sleep.
3228
3229 changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3230 } else if ((oldCount != 0) && (newCount == 0)) {
3231 // Last driver removed from prevent list.
3232 // Drop the driver clamp to allow idle sleep.
3233
3234 changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3235 evaluatePolicy( kStimulusNoIdleSleepPreventers );
3236 }
3237 messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3238 &newCount, sizeof(newCount));
3239
3240 #if defined(XNU_TARGET_OS_OSX)
3241 if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3242 DLOG("Cannot cancel idle sleep\n");
3243 return false; // do not idle-cancel
3244 }
3245 #endif
3246
3247 return true;
3248 }
3249
3250 //******************************************************************************
3251 // startSpinDump
3252 //******************************************************************************
3253
3254 void
startSpinDump(uint32_t spindumpKind)3255 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3256 {
3257 messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3258 }
3259
3260 //******************************************************************************
3261 // preventSystemSleepListUpdate
3262 //
3263 // Called by IOService on PM work loop.
3264 //******************************************************************************
3265
3266 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3267 IOPMrootDomain::updatePreventSystemSleepList(
3268 IOService * service, bool addNotRemove )
3269 {
3270 unsigned int oldCount, newCount;
3271
3272 ASSERT_GATED();
3273 if (this == service) {
3274 return;
3275 }
3276
3277 oldCount = preventSystemSleepList->getCount();
3278 if (addNotRemove) {
3279 preventSystemSleepList->setObject(service);
3280 DLOG("Added %s to system sleep preventers list (Total %u)\n",
3281 service->getName(), preventSystemSleepList->getCount());
3282 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3283 AbsoluteTime now;
3284 clock_usec_t microsecs;
3285 clock_get_uptime(&now);
3286 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3287 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
3288 if (assertOnWakeReport) {
3289 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3290 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3291 }
3292 }
3293 } else if (preventSystemSleepList->member(service)) {
3294 preventSystemSleepList->removeObject(service);
3295 DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3296 service->getName(), preventSystemSleepList->getCount());
3297
3298 if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3299 // Lost all system sleep preventers.
3300 // Send stimulus if system sleep was blocked, and is in dark wake.
3301 evaluatePolicy( kStimulusDarkWakeEvaluate );
3302 }
3303 }
3304
3305 newCount = preventSystemSleepList->getCount();
3306 if (newCount) {
3307 char buf[256] = "System Sleep Preventers:";
3308 makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3309 DLOG("%s\n", buf);
3310 }
3311
3312 messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3313 &newCount, sizeof(newCount));
3314 }
3315
3316 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3317 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3318 {
3319 OSSharedPtr<OSCollectionIterator> iterator;
3320 OSObject *object = NULL;
3321 OSSharedPtr<OSArray> array;
3322
3323 if (!gIOPMWorkLoop->inGate()) {
3324 gIOPMWorkLoop->runAction(
3325 OSMemberFunctionCast(IOWorkLoop::Action, this,
3326 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3327 this, (void *)idleSleepList, (void *)systemSleepList);
3328 return;
3329 }
3330
3331 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3332 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3333 array = OSArray::withCapacity(5);
3334
3335 if (iterator && array) {
3336 while ((object = iterator->getNextObject())) {
3337 IOService *service = OSDynamicCast(IOService, object);
3338 if (service) {
3339 OSSharedPtr<const OSSymbol> name = service->copyName();
3340 if (name) {
3341 array->setObject(name.get());
3342 }
3343 }
3344 }
3345 }
3346 *idleSleepList = array.detach();
3347 }
3348
3349 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3350 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3351 array = OSArray::withCapacity(5);
3352
3353 if (iterator && array) {
3354 while ((object = iterator->getNextObject())) {
3355 IOService *service = OSDynamicCast(IOService, object);
3356 if (service) {
3357 OSSharedPtr<const OSSymbol> name = service->copyName();
3358 if (name) {
3359 array->setObject(name.get());
3360 }
3361 }
3362 }
3363 }
3364 *systemSleepList = array.detach();
3365 }
3366 }
3367
3368 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3369 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3370 {
3371 OSSharedPtr<OSCollectionIterator> iterator;
3372 OSObject *object = NULL;
3373 OSSharedPtr<OSArray> array;
3374
3375 if (!gIOPMWorkLoop->inGate()) {
3376 gIOPMWorkLoop->runAction(
3377 OSMemberFunctionCast(IOWorkLoop::Action, this,
3378 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3379 this, (void *)idleSleepList, (void *)systemSleepList);
3380 return;
3381 }
3382
3383 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3384 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3385 array = OSArray::withCapacity(5);
3386
3387 if (iterator && array) {
3388 while ((object = iterator->getNextObject())) {
3389 IOService *service = OSDynamicCast(IOService, object);
3390 if (service) {
3391 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3392 OSSharedPtr<const OSSymbol> name = service->copyName();
3393 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3394 if (dict && name && id) {
3395 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3396 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3397 array->setObject(dict.get());
3398 }
3399 }
3400 }
3401 }
3402 *idleSleepList = array.detach();
3403 }
3404
3405 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3406 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3407 array = OSArray::withCapacity(5);
3408
3409 if (iterator && array) {
3410 while ((object = iterator->getNextObject())) {
3411 IOService *service = OSDynamicCast(IOService, object);
3412 if (service) {
3413 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3414 OSSharedPtr<const OSSymbol> name = service->copyName();
3415 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3416 if (dict && name && id) {
3417 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3418 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3419 array->setObject(dict.get());
3420 }
3421 }
3422 }
3423 }
3424 *systemSleepList = array.detach();
3425 }
3426 }
3427
3428 //******************************************************************************
3429 // tellChangeDown
3430 //
3431 // Override the superclass implementation to send a different message type.
3432 //******************************************************************************
3433
3434 bool
tellChangeDown(unsigned long stateNum)3435 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3436 {
3437 DLOG("tellChangeDown %s->%s\n",
3438 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3439
3440 if (SLEEP_STATE == stateNum) {
3441 // Legacy apps were already told in the full->dark transition
3442 if (!ignoreTellChangeDown) {
3443 tracePoint( kIOPMTracePointSleepApplications );
3444 } else {
3445 tracePoint( kIOPMTracePointSleepPriorityClients );
3446 }
3447 }
3448
3449 if (!ignoreTellChangeDown) {
3450 userActivityAtSleep = userActivityCount;
3451 DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3452
3453 if (SLEEP_STATE == stateNum) {
3454 hibernateAborted = false;
3455
3456 // Direct callout into OSKext so it can disable kext unloads
3457 // during sleep/wake to prevent deadlocks.
3458 OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3459
3460 IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3461
3462 // Two change downs are sent by IOServicePM. Ignore the 2nd.
3463 // But tellClientsWithResponse() must be called for both.
3464 ignoreTellChangeDown = true;
3465 }
3466 }
3467
3468 return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3469 }
3470
3471 //******************************************************************************
3472 // askChangeDown
3473 //
3474 // Override the superclass implementation to send a different message type.
3475 // This must be idle sleep since we don't ask during any other power change.
3476 //******************************************************************************
3477
3478 bool
askChangeDown(unsigned long stateNum)3479 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3480 {
3481 DLOG("askChangeDown %s->%s\n",
3482 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3483
3484 // Don't log for dark wake entry
3485 if (kSystemTransitionSleep == _systemTransitionType) {
3486 tracePoint( kIOPMTracePointSleepApplications );
3487 }
3488
3489 return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3490 }
3491
3492 //******************************************************************************
3493 // askChangeDownDone
3494 //
3495 // An opportunity for root domain to cancel the power transition,
3496 // possibily due to an assertion created by powerd in response to
3497 // kIOMessageCanSystemSleep.
3498 //
3499 // Idle sleep:
3500 // full -> dark wake transition
3501 // 1. Notify apps and powerd with kIOMessageCanSystemSleep
3502 // 2. askChangeDownDone()
3503 // dark -> sleep transition
3504 // 1. Notify powerd with kIOMessageCanSystemSleep
3505 // 2. askChangeDownDone()
3506 //
3507 // Demand sleep:
3508 // full -> dark wake transition
3509 // 1. Notify powerd with kIOMessageCanSystemSleep
3510 // 2. askChangeDownDone()
3511 // dark -> sleep transition
3512 // 1. Notify powerd with kIOMessageCanSystemSleep
3513 // 2. askChangeDownDone()
3514 //******************************************************************************
3515
3516 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3517 IOPMrootDomain::askChangeDownDone(
3518 IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3519 {
3520 DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3521 *inOutChangeFlags, *cancel,
3522 _systemTransitionType,
3523 _currentCapability, _pendingCapability);
3524
3525 if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3526 // Dark->Sleep transition.
3527 // Check if there are any deny sleep assertions.
3528 // lastSleepReason already set by handleOurPowerChangeStart()
3529
3530 if (!checkSystemCanSleep(lastSleepReason)) {
3531 // Cancel dark wake to sleep transition.
3532 // Must re-scan assertions upon entering dark wake.
3533
3534 *cancel = true;
3535 DLOG("cancel dark->sleep\n");
3536 }
3537 if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3538 uint64_t now = mach_continuous_time();
3539 if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3540 && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3541 *cancel = true;
3542 IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3543 }
3544 }
3545 }
3546 }
3547
3548 //******************************************************************************
3549 // systemDidNotSleep
3550 //
3551 // Work common to both canceled or aborted sleep.
3552 //******************************************************************************
3553
3554 void
systemDidNotSleep(void)3555 IOPMrootDomain::systemDidNotSleep( void )
3556 {
3557 // reset console lock state
3558 thread_call_enter(updateConsoleUsersEntry);
3559
3560 if (idleSleepEnabled) {
3561 if (!wrangler) {
3562 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3563 startIdleSleepTimer(kIdleSleepRetryInterval);
3564 #else
3565 startIdleSleepTimer(idleMilliSeconds);
3566 #endif
3567 } else if (!userIsActive) {
3568 // Manually start the idle sleep timer besides waiting for
3569 // the user to become inactive.
3570 startIdleSleepTimer(kIdleSleepRetryInterval);
3571 }
3572 }
3573
3574 preventTransitionToUserActive(false);
3575 IOService::setAdvisoryTickleEnable( true );
3576
3577 // After idle revert and cancel, send a did-change message to powerd
3578 // to balance the previous will-change message. Kernel clients do not
3579 // need this since sleep cannot be canceled once they are notified.
3580
3581 if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3582 (_pendingCapability != _currentCapability) &&
3583 ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3584 // Differs from a real capability gain change where notifyRef != 0,
3585 // but it is zero here since no response is expected.
3586
3587 IOPMSystemCapabilityChangeParameters params;
3588
3589 bzero(¶ms, sizeof(params));
3590 params.fromCapabilities = _pendingCapability;
3591 params.toCapabilities = _currentCapability;
3592 params.changeFlags = kIOPMSystemCapabilityDidChange;
3593
3594 DLOG("MESG cap %x->%x did change\n",
3595 params.fromCapabilities, params.toCapabilities);
3596 messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3597 ¶ms, sizeof(params));
3598 }
3599 }
3600
3601 //******************************************************************************
3602 // tellNoChangeDown
3603 //
3604 // Notify registered applications and kernel clients that we are not dropping
3605 // power.
3606 //
3607 // We override the superclass implementation so we can send a different message
3608 // type to the client or application being notified.
3609 //
3610 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3611 //******************************************************************************
3612
3613 void
tellNoChangeDown(unsigned long stateNum)3614 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3615 {
3616 DLOG("tellNoChangeDown %s->%s\n",
3617 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3618
3619 // Sleep canceled, clear the sleep trace point.
3620 tracePoint(kIOPMTracePointSystemUp);
3621
3622 systemDidNotSleep();
3623 return tellClients( kIOMessageSystemWillNotSleep );
3624 }
3625
3626 //******************************************************************************
3627 // tellChangeUp
3628 //
3629 // Notify registered applications and kernel clients that we are raising power.
3630 //
3631 // We override the superclass implementation so we can send a different message
3632 // type to the client or application being notified.
3633 //******************************************************************************
3634
3635 void
tellChangeUp(unsigned long stateNum)3636 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3637 {
3638 DLOG("tellChangeUp %s->%s\n",
3639 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3640
3641 ignoreTellChangeDown = false;
3642
3643 if (stateNum == ON_STATE) {
3644 // Direct callout into OSKext so it can disable kext unloads
3645 // during sleep/wake to prevent deadlocks.
3646 OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3647
3648 // Notify platform that sleep was cancelled or resumed.
3649 getPlatform()->callPlatformFunction(
3650 sleepMessagePEFunction.get(), false,
3651 (void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3652 NULL, NULL, NULL);
3653
3654 if (getPowerState() == ON_STATE) {
3655 // Sleep was cancelled by idle cancel or revert
3656 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3657 // rdar://problem/50363791
3658 // If system is in dark wake and sleep is cancelled, do not
3659 // send SystemWillPowerOn/HasPoweredOn messages to kernel
3660 // priority clients. They haven't yet seen a SystemWillSleep
3661 // message before the cancellation. So make sure the kernel
3662 // client bit is cleared in _systemMessageClientMask before
3663 // invoking the tellClients() below. This bit may have been
3664 // set by handleOurPowerChangeStart() anticipating a successful
3665 // sleep and setting the filter mask ahead of time allows the
3666 // SystemWillSleep message to go through.
3667 _systemMessageClientMask &= ~kSystemMessageClientKernel;
3668 }
3669
3670 systemDidNotSleep();
3671 tellClients( kIOMessageSystemWillPowerOn );
3672 }
3673
3674 tracePoint( kIOPMTracePointWakeApplications );
3675 tellClients( kIOMessageSystemHasPoweredOn );
3676 }
3677 }
3678
3679 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3680 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3681 ((params)->fromCapabilities & (flag)) && \
3682 (((params)->toCapabilities & (flag)) == 0))
3683
3684 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3685 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3686 ((params)->toCapabilities & (flag)) && \
3687 (((params)->fromCapabilities & (flag)) == 0))
3688
3689 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3690 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3691 ((params)->fromCapabilities & (flag)) && \
3692 (((params)->toCapabilities & (flag)) == 0))
3693
3694 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3695 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3696 ((params)->toCapabilities & (flag)) && \
3697 (((params)->fromCapabilities & (flag)) == 0))
3698
3699 //******************************************************************************
3700 // sysPowerDownHandler
3701 //
3702 // Perform a vfs sync before system sleep.
3703 //******************************************************************************
3704
3705 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3706 IOPMrootDomain::sysPowerDownHandler(
3707 void * target, void * refCon,
3708 UInt32 messageType, IOService * service,
3709 void * messageArgs, vm_size_t argSize )
3710 {
3711 static UInt32 lastSystemMessageType = 0;
3712 IOReturn ret = 0;
3713
3714 DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3715
3716 // rdar://problem/50363791
3717 // Sanity check to make sure the SystemWill/Has message types are
3718 // received in the expected order for all kernel priority clients.
3719 if (messageType == kIOMessageSystemWillSleep ||
3720 messageType == kIOMessageSystemWillPowerOn ||
3721 messageType == kIOMessageSystemHasPoweredOn) {
3722 switch (messageType) {
3723 case kIOMessageSystemWillPowerOn:
3724 assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3725 break;
3726 case kIOMessageSystemHasPoweredOn:
3727 assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3728 break;
3729 }
3730
3731 lastSystemMessageType = messageType;
3732 }
3733
3734 if (!gRootDomain) {
3735 return kIOReturnUnsupported;
3736 }
3737
3738 if (messageType == kIOMessageSystemCapabilityChange) {
3739 IOPMSystemCapabilityChangeParameters * params =
3740 (IOPMSystemCapabilityChangeParameters *) messageArgs;
3741
3742 // Interested applications have been notified of an impending power
3743 // change and have acked (when applicable).
3744 // This is our chance to save whatever state we can before powering
3745 // down.
3746 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3747 // via callout
3748
3749 DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3750 params->fromCapabilities, params->toCapabilities,
3751 params->changeFlags);
3752
3753 if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3754 // We will ack within 20 seconds
3755 params->maxWaitForReply = 20 * 1000 * 1000;
3756
3757 #if HIBERNATION
3758 gRootDomain->evaluateSystemSleepPolicyEarly();
3759
3760 // add in time we could spend freeing pages
3761 if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3762 params->maxWaitForReply = kCapabilityClientMaxWait;
3763 }
3764 DLOG("sysPowerDownHandler max wait %d s\n",
3765 (int) (params->maxWaitForReply / 1000 / 1000));
3766 #endif
3767
3768 // Notify platform that sleep has begun, after the early
3769 // sleep policy evaluation.
3770 getPlatform()->callPlatformFunction(
3771 sleepMessagePEFunction.get(), false,
3772 (void *)(uintptr_t) kIOMessageSystemWillSleep,
3773 NULL, NULL, NULL);
3774
3775 if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3776 // Purposely delay the ack and hope that shutdown occurs quickly.
3777 // Another option is not to schedule the thread and wait for
3778 // ack timeout...
3779 AbsoluteTime deadline;
3780 clock_interval_to_deadline( 30, kSecondScale, &deadline );
3781 thread_call_enter1_delayed(
3782 gRootDomain->diskSyncCalloutEntry,
3783 (thread_call_param_t)(uintptr_t) params->notifyRef,
3784 deadline );
3785 } else {
3786 thread_call_enter1(
3787 gRootDomain->diskSyncCalloutEntry,
3788 (thread_call_param_t)(uintptr_t) params->notifyRef);
3789 }
3790 }
3791 #if HIBERNATION
3792 else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3793 // We will ack within 110 seconds
3794 params->maxWaitForReply = 110 * 1000 * 1000;
3795
3796 thread_call_enter1(
3797 gRootDomain->diskSyncCalloutEntry,
3798 (thread_call_param_t)(uintptr_t) params->notifyRef);
3799 }
3800 #endif
3801 ret = kIOReturnSuccess;
3802 }
3803
3804 return ret;
3805 }
3806
3807 //******************************************************************************
3808 // handleQueueSleepWakeUUID
3809 //
3810 // Called from IOPMrootDomain when we're initiating a sleep,
3811 // or indirectly from PM configd when PM decides to clear the UUID.
3812 // PM clears the UUID several minutes after successful wake from sleep,
3813 // so that we might associate App spindumps with the immediately previous
3814 // sleep/wake.
3815 //
3816 // @param obj has a retain on it. We're responsible for releasing that retain.
3817 //******************************************************************************
3818
3819 void
handleQueueSleepWakeUUID(OSObject * obj)3820 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3821 {
3822 OSSharedPtr<OSString> str;
3823
3824 if (kOSBooleanFalse == obj) {
3825 handlePublishSleepWakeUUID(false);
3826 } else {
3827 str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3828 if (str) {
3829 // This branch caches the UUID for an upcoming sleep/wake
3830 queuedSleepWakeUUIDString = str;
3831 DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3832 }
3833 }
3834 }
3835 //******************************************************************************
3836 // handlePublishSleepWakeUUID
3837 //
3838 // Called from IOPMrootDomain when we're initiating a sleep,
3839 // or indirectly from PM configd when PM decides to clear the UUID.
3840 // PM clears the UUID several minutes after successful wake from sleep,
3841 // so that we might associate App spindumps with the immediately previous
3842 // sleep/wake.
3843 //******************************************************************************
3844
3845 void
handlePublishSleepWakeUUID(bool shouldPublish)3846 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3847 {
3848 ASSERT_GATED();
3849
3850 /*
3851 * Clear the current UUID
3852 */
3853 if (gSleepWakeUUIDIsSet) {
3854 DLOG("SleepWake UUID cleared\n");
3855
3856 gSleepWakeUUIDIsSet = false;
3857
3858 removeProperty(kIOPMSleepWakeUUIDKey);
3859 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3860 }
3861
3862 /*
3863 * Optionally, publish a new UUID
3864 */
3865 if (queuedSleepWakeUUIDString && shouldPublish) {
3866 OSSharedPtr<OSString> publishThisUUID;
3867
3868 publishThisUUID = queuedSleepWakeUUIDString;
3869
3870 if (publishThisUUID) {
3871 setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3872 }
3873
3874 gSleepWakeUUIDIsSet = true;
3875 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3876
3877 queuedSleepWakeUUIDString.reset();
3878 }
3879 }
3880
3881 //******************************************************************************
3882 // IOPMGetSleepWakeUUIDKey
3883 //
3884 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3885 // To get the full key -- a C string -- the buffer must large enough for
3886 // the end-of-string character.
3887 // The key is expected to be an UUID string
3888 //******************************************************************************
3889
3890 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3891 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3892 {
3893 if (!gSleepWakeUUIDIsSet) {
3894 return false;
3895 }
3896
3897 if (buffer != NULL) {
3898 OSSharedPtr<OSString> string =
3899 OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3900
3901 if (!string) {
3902 *buffer = '\0';
3903 } else {
3904 strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3905 }
3906 }
3907
3908 return true;
3909 }
3910
3911 //******************************************************************************
3912 // lowLatencyAudioNotify
3913 //
3914 // Used to send an update about low latency audio activity to interested
3915 // clients. To keep the overhead minimal the OSDictionary used here
3916 // is initialized at boot.
3917 //******************************************************************************
3918
3919 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3920 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3921 {
3922 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
3923 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
3924 lowLatencyAudioNotifyTimestampVal->setValue(time);
3925 lowLatencyAudioNotifyStateVal->setValue(state);
3926 setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
3927 } else {
3928 DLOG("LowLatencyAudioNotify error\n");
3929 }
3930 return;
3931 }
3932
3933 //******************************************************************************
3934 // IOPMrootDomainRTNotifier
3935 //
3936 // Used by performance controller to update the timestamp and state associated
3937 // with low latency audio activity in the system.
3938 //******************************************************************************
3939
3940 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)3941 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
3942 {
3943 gRootDomain->lowLatencyAudioNotify(time, state);
3944 return;
3945 }
3946
3947 //******************************************************************************
3948 // initializeBootSessionUUID
3949 //
3950 // Initialize the boot session uuid at boot up and sets it into registry.
3951 //******************************************************************************
3952
3953 void
initializeBootSessionUUID(void)3954 IOPMrootDomain::initializeBootSessionUUID(void)
3955 {
3956 uuid_t new_uuid;
3957 uuid_string_t new_uuid_string;
3958
3959 uuid_generate(new_uuid);
3960 uuid_unparse_upper(new_uuid, new_uuid_string);
3961 memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
3962
3963 setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
3964 }
3965
3966 //******************************************************************************
3967 // Root domain uses the private and tagged changePowerState methods for
3968 // tracking and logging purposes.
3969 //******************************************************************************
3970
3971 #define REQUEST_TAG_TO_REASON(x) ((uint16_t)x)
3972
3973 static uint32_t
nextRequestTag(IOPMRequestTag tag)3974 nextRequestTag( IOPMRequestTag tag )
3975 {
3976 static SInt16 msb16 = 1;
3977 uint16_t id = OSAddAtomic16(1, &msb16);
3978 return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
3979 }
3980
3981 // TODO: remove this shim function and exported symbol
3982 IOReturn
changePowerStateTo(unsigned long ordinal)3983 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
3984 {
3985 return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
3986 }
3987
3988 // TODO: remove this shim function and exported symbol
3989 IOReturn
changePowerStateToPriv(unsigned long ordinal)3990 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
3991 {
3992 return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
3993 }
3994
3995 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)3996 IOPMrootDomain::changePowerStateWithOverrideTo(
3997 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
3998 {
3999 uint32_t tag = nextRequestTag(reason);
4000 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4001
4002 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4003 return kIOReturnUnsupported;
4004 }
4005
4006 return super::changePowerStateWithOverrideTo(ordinal, tag);
4007 }
4008
4009 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4010 IOPMrootDomain::changePowerStateWithTagTo(
4011 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4012 {
4013 uint32_t tag = nextRequestTag(reason);
4014 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4015
4016 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4017 return kIOReturnUnsupported;
4018 }
4019
4020 return super::changePowerStateWithTagTo(ordinal, tag);
4021 }
4022
4023 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4024 IOPMrootDomain::changePowerStateWithTagToPriv(
4025 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4026 {
4027 uint32_t tag = nextRequestTag(reason);
4028 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4029
4030 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4031 return kIOReturnUnsupported;
4032 }
4033
4034 return super::changePowerStateWithTagToPriv(ordinal, tag);
4035 }
4036
4037 //******************************************************************************
4038 // activity detect
4039 //
4040 //******************************************************************************
4041
4042 bool
activitySinceSleep(void)4043 IOPMrootDomain::activitySinceSleep(void)
4044 {
4045 return userActivityCount != userActivityAtSleep;
4046 }
4047
4048 bool
abortHibernation(void)4049 IOPMrootDomain::abortHibernation(void)
4050 {
4051 #if __arm64__
4052 // don't allow hibernation to be aborted on ARM due to user activity
4053 // since once ApplePMGR decides we're hibernating, we can't turn back
4054 // see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4055 return false;
4056 #else
4057 bool ret = activitySinceSleep();
4058
4059 if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4060 DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4061 hibernateAborted = true;
4062 }
4063 return ret;
4064 #endif
4065 }
4066
4067 extern "C" int
hibernate_should_abort(void)4068 hibernate_should_abort(void)
4069 {
4070 if (gRootDomain) {
4071 return gRootDomain->abortHibernation();
4072 } else {
4073 return 0;
4074 }
4075 }
4076
4077 //******************************************************************************
4078 // willNotifyPowerChildren
4079 //
4080 // Called after all interested drivers have all acknowledged the power change,
4081 // but before any power children is informed. Dispatched though a thread call,
4082 // so it is safe to perform work that might block on a sleeping disk. PM state
4083 // machine (not thread) will block w/o timeout until this function returns.
4084 //******************************************************************************
4085
4086 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4087 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4088 {
4089 OSSharedPtr<OSDictionary> dict;
4090 OSSharedPtr<OSNumber> secs;
4091
4092 if (SLEEP_STATE == newPowerState) {
4093 notifierThread = current_thread();
4094 if (!tasksSuspended) {
4095 AbsoluteTime deadline;
4096 tasksSuspended = TRUE;
4097 updateTasksSuspend();
4098
4099 clock_interval_to_deadline(10, kSecondScale, &deadline);
4100 #if defined(XNU_TARGET_OS_OSX)
4101 vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4102 #endif /* defined(XNU_TARGET_OS_OSX) */
4103 }
4104
4105 _aotReadyToFullWake = false;
4106 #if 0
4107 if (_aotLingerTime) {
4108 uint64_t deadline;
4109 IOLog("aot linger no return\n");
4110 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4111 clock_delay_until(deadline);
4112 }
4113 #endif
4114 if (!_aotMode) {
4115 _aotTestTime = 0;
4116 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4117 if (_aotMetrics) {
4118 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4119 }
4120 } else if (!_aotNow && !_debugWakeSeconds) {
4121 _aotNow = true;
4122 _aotPendingFlags = 0;
4123 _aotTasksSuspended = true;
4124 _aotLastWakeTime = 0;
4125 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4126 if (kIOPMAOTModeCycle & _aotMode) {
4127 clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4128 _aotTestTime = mach_continuous_time() + _aotTestInterval;
4129 setWakeTime(_aotTestTime);
4130 }
4131 uint32_t lingerSecs;
4132 if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4133 lingerSecs = 0;
4134 }
4135 clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4136 clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4137 clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4138 }
4139
4140 #if HIBERNATION
4141 IOHibernateSystemSleep();
4142 IOHibernateIOKitSleep();
4143 #endif
4144 if (gRootDomain->activitySinceSleep()) {
4145 dict = OSDictionary::withCapacity(1);
4146 secs = OSNumber::withNumber(1, 32);
4147
4148 if (dict && secs) {
4149 dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4150 gRootDomain->setProperties(dict.get());
4151 MSG("Reverting sleep with relative wake\n");
4152 }
4153 }
4154
4155 notifierThread = NULL;
4156 }
4157 }
4158
4159 //******************************************************************************
4160 // willTellSystemCapabilityDidChange
4161 //
4162 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4163 // domain is raising its power state, immediately after notifying interested
4164 // drivers and power children.
4165 //******************************************************************************
4166
4167 void
willTellSystemCapabilityDidChange(void)4168 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4169 {
4170 if ((_systemTransitionType == kSystemTransitionWake) &&
4171 !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4172 // After powering up drivers, dark->full promotion on the current wake
4173 // transition is no longer possible. That is because the next machine
4174 // state will issue the system capability change messages.
4175 // The darkWakePowerClamped flag may already be set if the system has
4176 // at least one driver that was power clamped due to dark wake.
4177 // This function sets the darkWakePowerClamped flag in case there
4178 // is no power-clamped driver in the system.
4179 //
4180 // Last opportunity to exit dark wake using:
4181 // requestFullWake( kFullWakeReasonLocalUser );
4182
4183 if (!darkWakePowerClamped) {
4184 if (darkWakeLogClamp) {
4185 AbsoluteTime now;
4186 uint64_t nsec;
4187
4188 clock_get_uptime(&now);
4189 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4190 absolutetime_to_nanoseconds(now, &nsec);
4191 DLOG("dark wake promotion disabled at %u ms\n",
4192 ((int)((nsec) / NSEC_PER_MSEC)));
4193 }
4194 darkWakePowerClamped = true;
4195 }
4196 }
4197 }
4198
4199 //******************************************************************************
4200 // sleepOnClamshellClosed
4201 //
4202 // contains the logic to determine if the system should sleep when the clamshell
4203 // is closed.
4204 //******************************************************************************
4205
4206 bool
shouldSleepOnClamshellClosed(void)4207 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4208 {
4209 if (!clamshellExists) {
4210 return false;
4211 }
4212
4213 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4214 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4215
4216 return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4217 }
4218
4219 bool
shouldSleepOnRTCAlarmWake(void)4220 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4221 {
4222 // Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4223 // closed && battery
4224 if (!clamshellExists) {
4225 return false;
4226 }
4227
4228 DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4229 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4230
4231 return !acAdaptorConnected && !clamshellSleepDisableMask;
4232 }
4233
4234 void
sendClientClamshellNotification(void)4235 IOPMrootDomain::sendClientClamshellNotification( void )
4236 {
4237 /* Only broadcast clamshell alert if clamshell exists. */
4238 if (!clamshellExists) {
4239 return;
4240 }
4241
4242 setProperty(kAppleClamshellStateKey,
4243 clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4244
4245 setProperty(kAppleClamshellCausesSleepKey,
4246 shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4247
4248 /* Argument to message is a bitfiel of
4249 * ( kClamshellStateBit | kClamshellSleepBit )
4250 */
4251 messageClients(kIOPMMessageClamshellStateChange,
4252 (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4253 | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4254 }
4255
4256 //******************************************************************************
4257 // getSleepSupported
4258 //
4259 // Deprecated
4260 //******************************************************************************
4261
4262 IOOptionBits
getSleepSupported(void)4263 IOPMrootDomain::getSleepSupported( void )
4264 {
4265 return platformSleepSupport;
4266 }
4267
4268 //******************************************************************************
4269 // setSleepSupported
4270 //
4271 // Deprecated
4272 //******************************************************************************
4273
4274 void
setSleepSupported(IOOptionBits flags)4275 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4276 {
4277 DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4278 OSBitOrAtomic(flags, &platformSleepSupport);
4279 }
4280
4281 //******************************************************************************
4282 // setClamShellSleepDisable
4283 //
4284 //******************************************************************************
4285
4286 void
setClamShellSleepDisable(bool disable,uint32_t bitmask)4287 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4288 {
4289 uint32_t oldMask;
4290
4291 // User client calls this in non-gated context
4292 if (gIOPMWorkLoop->inGate() == false) {
4293 gIOPMWorkLoop->runAction(
4294 OSMemberFunctionCast(IOWorkLoop::Action, this,
4295 &IOPMrootDomain::setClamShellSleepDisable),
4296 (OSObject *) this,
4297 (void *) disable, (void *)(uintptr_t) bitmask);
4298 return;
4299 }
4300
4301 oldMask = clamshellSleepDisableMask;
4302 if (disable) {
4303 clamshellSleepDisableMask |= bitmask;
4304 } else {
4305 clamshellSleepDisableMask &= ~bitmask;
4306 }
4307 DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4308
4309 if (clamshellExists && clamshellClosed &&
4310 (clamshellSleepDisableMask != oldMask) &&
4311 (clamshellSleepDisableMask == 0)) {
4312 handlePowerNotification(kLocalEvalClamshellCommand);
4313 }
4314 }
4315
4316 //******************************************************************************
4317 // wakeFromDoze
4318 //
4319 // Deprecated.
4320 //******************************************************************************
4321
4322 void
wakeFromDoze(void)4323 IOPMrootDomain::wakeFromDoze( void )
4324 {
4325 // Preserve symbol for familes (IOUSBFamily and IOGraphics)
4326 }
4327
4328 //******************************************************************************
4329 // recordRTCAlarm
4330 //
4331 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4332 // should be a dark wake or a full wake. Both Maintenance and SleepService
4333 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4334 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4335 // PMSettings are ignored.
4336 //
4337 // Caller serialized using settingsCtrlLock.
4338 //******************************************************************************
4339
4340 void
recordRTCAlarm(const OSSymbol * type,OSObject * object)4341 IOPMrootDomain::recordRTCAlarm(
4342 const OSSymbol *type,
4343 OSObject *object )
4344 {
4345 uint32_t previousAlarmMask = _scheduledAlarmMask;
4346
4347 if (type == gIOPMSettingDebugWakeRelativeKey) {
4348 OSNumber * n = OSDynamicCast(OSNumber, object);
4349 if (n) {
4350 // Debug wake has highest scheduling priority so it overrides any
4351 // pre-existing alarm.
4352 uint32_t debugSecs = n->unsigned32BitValue();
4353 _nextScheduledAlarmType.reset(type, OSRetain);
4354 _nextScheduledAlarmUTC = debugSecs;
4355
4356 _debugWakeSeconds = debugSecs;
4357 OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4358 DLOG("next alarm (%s) in %u secs\n",
4359 type->getCStringNoCopy(), debugSecs);
4360 }
4361 } else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4362 (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4363 (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4364 OSData * data = OSDynamicCast(OSData, object);
4365 if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4366 const IOPMCalendarStruct * cs;
4367 bool replaceNextAlarm = false;
4368 clock_sec_t secs;
4369
4370 cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4371 secs = IOPMConvertCalendarToSeconds(cs);
4372 DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4373
4374 // Update the next scheduled alarm type
4375 if ((_nextScheduledAlarmType == NULL) ||
4376 ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4377 (secs < _nextScheduledAlarmUTC))) {
4378 replaceNextAlarm = true;
4379 }
4380
4381 if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4382 if (cs->year) {
4383 _calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4384 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4385 } else {
4386 // TODO: can this else-block be removed?
4387 _calendarWakeAlarmUTC = 0;
4388 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4389 }
4390 }
4391 if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4392 OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4393 }
4394 if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4395 OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4396 }
4397
4398 if (replaceNextAlarm) {
4399 _nextScheduledAlarmType.reset(type, OSRetain);
4400 _nextScheduledAlarmUTC = secs;
4401 DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4402 }
4403 }
4404 }
4405
4406 if (_scheduledAlarmMask != previousAlarmMask) {
4407 DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4408 }
4409 }
4410
4411 // MARK: -
4412 // MARK: Features
4413
4414 //******************************************************************************
4415 // publishFeature
4416 //
4417 // Adds a new feature to the supported features dictionary
4418 //******************************************************************************
4419
4420 void
publishFeature(const char * feature)4421 IOPMrootDomain::publishFeature( const char * feature )
4422 {
4423 publishFeature(feature, kRD_AllPowerSources, NULL);
4424 }
4425
4426 //******************************************************************************
4427 // publishFeature (with supported power source specified)
4428 //
4429 // Adds a new feature to the supported features dictionary
4430 //******************************************************************************
4431
4432 void
publishFeature(const char * feature,uint32_t supportedWhere,uint32_t * uniqueFeatureID)4433 IOPMrootDomain::publishFeature(
4434 const char *feature,
4435 uint32_t supportedWhere,
4436 uint32_t *uniqueFeatureID)
4437 {
4438 static uint16_t next_feature_id = 500;
4439
4440 OSSharedPtr<OSNumber> new_feature_data;
4441 OSNumber *existing_feature = NULL;
4442 OSArray *existing_feature_arr_raw = NULL;
4443 OSSharedPtr<OSArray> existing_feature_arr;
4444 OSObject *osObj = NULL;
4445 uint32_t feature_value = 0;
4446
4447 supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4448
4449 if (!supportedWhere) {
4450 // Feature isn't supported anywhere!
4451 return;
4452 }
4453
4454 if (next_feature_id > 5000) {
4455 // Far, far too many features!
4456 return;
4457 }
4458
4459 if (featuresDictLock) {
4460 IOLockLock(featuresDictLock);
4461 }
4462
4463 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4464 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4465 OSSharedPtr<OSDictionary> features;
4466
4467 // Create new features dict if necessary
4468 if (origFeatures) {
4469 features = OSDictionary::withDictionary(origFeatures);
4470 } else {
4471 features = OSDictionary::withCapacity(1);
4472 }
4473
4474 // Create OSNumber to track new feature
4475
4476 next_feature_id += 1;
4477 if (uniqueFeatureID) {
4478 // We don't really mind if the calling kext didn't give us a place
4479 // to stash their unique id. Many kexts don't plan to unload, and thus
4480 // have no need to remove themselves later.
4481 *uniqueFeatureID = next_feature_id;
4482 }
4483
4484 feature_value = (uint32_t)next_feature_id;
4485 feature_value <<= 16;
4486 feature_value += supportedWhere;
4487
4488 new_feature_data = OSNumber::withNumber(
4489 (unsigned long long)feature_value, 32);
4490
4491 // Does features object already exist?
4492 if ((osObj = features->getObject(feature))) {
4493 if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4494 // We need to create an OSArray to hold the now 2 elements.
4495 existing_feature_arr = OSArray::withObjects(
4496 (const OSObject **)&existing_feature, 1, 2);
4497 } else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4498 // Add object to existing array
4499 existing_feature_arr = OSArray::withArray(
4500 existing_feature_arr_raw,
4501 existing_feature_arr_raw->getCount() + 1);
4502 }
4503
4504 if (existing_feature_arr) {
4505 existing_feature_arr->setObject(new_feature_data.get());
4506 features->setObject(feature, existing_feature_arr.get());
4507 }
4508 } else {
4509 // The easy case: no previously existing features listed. We simply
4510 // set the OSNumber at key 'feature' and we're on our way.
4511 features->setObject(feature, new_feature_data.get());
4512 }
4513
4514 setProperty(kRootDomainSupportedFeatures, features.get());
4515
4516 if (featuresDictLock) {
4517 IOLockUnlock(featuresDictLock);
4518 }
4519
4520 // Notify EnergySaver and all those in user space so they might
4521 // re-populate their feature specific UI
4522 if (pmPowerStateQueue) {
4523 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4524 }
4525 }
4526
4527 //******************************************************************************
4528 // removePublishedFeature
4529 //
4530 // Removes previously published feature
4531 //******************************************************************************
4532
4533 IOReturn
removePublishedFeature(uint32_t removeFeatureID)4534 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4535 {
4536 IOReturn ret = kIOReturnError;
4537 uint32_t feature_value = 0;
4538 uint16_t feature_id = 0;
4539 bool madeAChange = false;
4540
4541 OSSymbol *dictKey = NULL;
4542 OSSharedPtr<OSCollectionIterator> dictIterator;
4543 OSArray *arrayMember = NULL;
4544 OSNumber *numberMember = NULL;
4545 OSObject *osObj = NULL;
4546 OSNumber *osNum = NULL;
4547 OSSharedPtr<OSArray> arrayMemberCopy;
4548
4549 if (kBadPMFeatureID == removeFeatureID) {
4550 return kIOReturnNotFound;
4551 }
4552
4553 if (featuresDictLock) {
4554 IOLockLock(featuresDictLock);
4555 }
4556
4557 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4558 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4559 OSSharedPtr<OSDictionary> features;
4560
4561 if (origFeatures) {
4562 // Any modifications to the dictionary are made to the copy to prevent
4563 // races & crashes with userland clients. Dictionary updated
4564 // automically later.
4565 features = OSDictionary::withDictionary(origFeatures);
4566 } else {
4567 features = NULL;
4568 ret = kIOReturnNotFound;
4569 goto exit;
4570 }
4571
4572 // We iterate 'features' dictionary looking for an entry tagged
4573 // with 'removeFeatureID'. If found, we remove it from our tracking
4574 // structures and notify the OS via a general interest message.
4575
4576 dictIterator = OSCollectionIterator::withCollection(features.get());
4577 if (!dictIterator) {
4578 goto exit;
4579 }
4580
4581 while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4582 osObj = features->getObject(dictKey);
4583
4584 // Each Feature is either tracked by an OSNumber
4585 if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4586 feature_value = numberMember->unsigned32BitValue();
4587 feature_id = (uint16_t)(feature_value >> 16);
4588
4589 if (feature_id == (uint16_t)removeFeatureID) {
4590 // Remove this node
4591 features->removeObject(dictKey);
4592 madeAChange = true;
4593 break;
4594 }
4595
4596 // Or tracked by an OSArray of OSNumbers
4597 } else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4598 unsigned int arrayCount = arrayMember->getCount();
4599
4600 for (unsigned int i = 0; i < arrayCount; i++) {
4601 osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4602 if (!osNum) {
4603 continue;
4604 }
4605
4606 feature_value = osNum->unsigned32BitValue();
4607 feature_id = (uint16_t)(feature_value >> 16);
4608
4609 if (feature_id == (uint16_t)removeFeatureID) {
4610 // Remove this node
4611 if (1 == arrayCount) {
4612 // If the array only contains one element, remove
4613 // the whole thing.
4614 features->removeObject(dictKey);
4615 } else {
4616 // Otherwise remove the element from a copy of the array.
4617 arrayMemberCopy = OSArray::withArray(arrayMember);
4618 if (arrayMemberCopy) {
4619 arrayMemberCopy->removeObject(i);
4620 features->setObject(dictKey, arrayMemberCopy.get());
4621 }
4622 }
4623
4624 madeAChange = true;
4625 break;
4626 }
4627 }
4628 }
4629 }
4630
4631 if (madeAChange) {
4632 ret = kIOReturnSuccess;
4633
4634 setProperty(kRootDomainSupportedFeatures, features.get());
4635
4636 // Notify EnergySaver and all those in user space so they might
4637 // re-populate their feature specific UI
4638 if (pmPowerStateQueue) {
4639 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4640 }
4641 } else {
4642 ret = kIOReturnNotFound;
4643 }
4644
4645 exit:
4646 if (featuresDictLock) {
4647 IOLockUnlock(featuresDictLock);
4648 }
4649 return ret;
4650 }
4651
4652 //******************************************************************************
4653 // publishPMSetting (private)
4654 //
4655 // Should only be called by PMSettingObject to publish a PM Setting as a
4656 // supported feature.
4657 //******************************************************************************
4658
4659 void
publishPMSetting(const OSSymbol * feature,uint32_t where,uint32_t * featureID)4660 IOPMrootDomain::publishPMSetting(
4661 const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4662 {
4663 if (noPublishPMSettings &&
4664 (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4665 // Setting found in noPublishPMSettings array
4666 *featureID = kBadPMFeatureID;
4667 return;
4668 }
4669
4670 publishFeature(
4671 feature->getCStringNoCopy(), where, featureID);
4672 }
4673
4674 //******************************************************************************
4675 // setPMSetting (private)
4676 //
4677 // Internal helper to relay PM settings changes from user space to individual
4678 // drivers. Should be called only by IOPMrootDomain::setProperties.
4679 //******************************************************************************
4680
4681 IOReturn
setPMSetting(const OSSymbol * type,OSObject * object)4682 IOPMrootDomain::setPMSetting(
4683 const OSSymbol *type,
4684 OSObject *object )
4685 {
4686 PMSettingCallEntry *entries = NULL;
4687 OSSharedPtr<OSArray> chosen;
4688 const OSArray *array;
4689 PMSettingObject *pmso;
4690 thread_t thisThread;
4691 int i, j, count, capacity;
4692 bool ok = false;
4693 IOReturn ret;
4694
4695 if (NULL == type) {
4696 return kIOReturnBadArgument;
4697 }
4698
4699 PMSETTING_LOCK();
4700
4701 // Update settings dict so changes are visible from copyPMSetting().
4702 fPMSettingsDict->setObject(type, object);
4703
4704 // Prep all PMSetting objects with the given 'type' for callout.
4705 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4706 if (!array || ((capacity = array->getCount()) == 0)) {
4707 goto unlock_exit;
4708 }
4709
4710 // Array to retain PMSetting objects targeted for callout.
4711 chosen = OSArray::withCapacity(capacity);
4712 if (!chosen) {
4713 goto unlock_exit; // error
4714 }
4715 entries = IONew(PMSettingCallEntry, capacity);
4716 if (!entries) {
4717 goto unlock_exit; // error
4718 }
4719 memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4720
4721 thisThread = current_thread();
4722
4723 for (i = 0, j = 0; i < capacity; i++) {
4724 pmso = (PMSettingObject *) array->getObject(i);
4725 if (pmso->disabled) {
4726 continue;
4727 }
4728 entries[j].thread = thisThread;
4729 queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4730 chosen->setObject(pmso);
4731 j++;
4732 }
4733 count = j;
4734 if (!count) {
4735 goto unlock_exit;
4736 }
4737
4738 PMSETTING_UNLOCK();
4739
4740 // Call each pmso in the chosen array.
4741 for (i = 0; i < count; i++) {
4742 pmso = (PMSettingObject *) chosen->getObject(i);
4743 ret = pmso->dispatchPMSetting(type, object);
4744 if (ret == kIOReturnSuccess) {
4745 // At least one setting handler was successful
4746 ok = true;
4747 #if DEVELOPMENT || DEBUG
4748 } else {
4749 // Log the handler and kext that failed
4750 OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4751 if (kextName) {
4752 DLOG("PMSetting(%s) error 0x%x from %s\n",
4753 type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4754 }
4755 #endif
4756 }
4757 }
4758
4759 PMSETTING_LOCK();
4760 for (i = 0; i < count; i++) {
4761 pmso = (PMSettingObject *) chosen->getObject(i);
4762 queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4763 if (pmso->waitThread) {
4764 PMSETTING_WAKEUP(pmso);
4765 }
4766 }
4767
4768 if (ok) {
4769 recordRTCAlarm(type, object);
4770 }
4771 unlock_exit:
4772 PMSETTING_UNLOCK();
4773
4774 if (entries) {
4775 IODelete(entries, PMSettingCallEntry, capacity);
4776 }
4777
4778 return kIOReturnSuccess;
4779 }
4780
4781 //******************************************************************************
4782 // copyPMSetting (public)
4783 //
4784 // Allows kexts to safely read setting values, without being subscribed to
4785 // notifications.
4786 //******************************************************************************
4787
4788 OSSharedPtr<OSObject>
copyPMSetting(OSSymbol * whichSetting)4789 IOPMrootDomain::copyPMSetting(
4790 OSSymbol *whichSetting)
4791 {
4792 OSSharedPtr<OSObject> obj;
4793
4794 if (!whichSetting) {
4795 return NULL;
4796 }
4797
4798 PMSETTING_LOCK();
4799 obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4800 PMSETTING_UNLOCK();
4801
4802 return obj;
4803 }
4804
4805 //******************************************************************************
4806 // registerPMSettingController (public)
4807 //
4808 // direct wrapper to registerPMSettingController with uint32_t power source arg
4809 //******************************************************************************
4810
4811 IOReturn
registerPMSettingController(const OSSymbol * settings[],IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4812 IOPMrootDomain::registerPMSettingController(
4813 const OSSymbol * settings[],
4814 IOPMSettingControllerCallback func,
4815 OSObject *target,
4816 uintptr_t refcon,
4817 OSObject **handle)
4818 {
4819 return registerPMSettingController(
4820 settings,
4821 (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4822 func, target, refcon, handle);
4823 }
4824
4825 //******************************************************************************
4826 // registerPMSettingController (public)
4827 //
4828 // Kexts may register for notifications when a particular setting is changed.
4829 // A list of settings is available in IOPM.h.
4830 // Arguments:
4831 // * settings - An OSArray containing OSSymbols. Caller should populate this
4832 // array with a list of settings caller wants notifications from.
4833 // * func - A C function callback of the type IOPMSettingControllerCallback
4834 // * target - caller may provide an OSObject *, which PM will pass as an
4835 // target to calls to "func"
4836 // * refcon - caller may provide an void *, which PM will pass as an
4837 // argument to calls to "func"
4838 // * handle - This is a return argument. We will populate this pointer upon
4839 // call success. Hold onto this and pass this argument to
4840 // IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4841 // Returns:
4842 // kIOReturnSuccess on success
4843 //******************************************************************************
4844
4845 IOReturn
registerPMSettingController(const OSSymbol * settings[],uint32_t supportedPowerSources,IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4846 IOPMrootDomain::registerPMSettingController(
4847 const OSSymbol * settings[],
4848 uint32_t supportedPowerSources,
4849 IOPMSettingControllerCallback func,
4850 OSObject *target,
4851 uintptr_t refcon,
4852 OSObject **handle)
4853 {
4854 PMSettingObject *pmso = NULL;
4855 OSObject *pmsh = NULL;
4856 int i;
4857
4858 if (NULL == settings ||
4859 NULL == func ||
4860 NULL == handle) {
4861 return kIOReturnBadArgument;
4862 }
4863
4864 pmso = PMSettingObject::pmSettingObject(
4865 (IOPMrootDomain *) this, func, target,
4866 refcon, supportedPowerSources, settings, &pmsh);
4867
4868 if (!pmso) {
4869 *handle = NULL;
4870 return kIOReturnInternalError;
4871 }
4872
4873 PMSETTING_LOCK();
4874 for (i = 0; settings[i]; i++) {
4875 OSSharedPtr<OSArray> newList;
4876 OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4877 if (!list) {
4878 // New array of callbacks for this setting
4879 newList = OSArray::withCapacity(1);
4880 settingsCallbacks->setObject(settings[i], newList.get());
4881 list = newList.get();
4882 }
4883
4884 // Add caller to the callback list
4885 list->setObject(pmso);
4886 }
4887 PMSETTING_UNLOCK();
4888
4889 // Return handle to the caller, the setting object is private.
4890 *handle = pmsh;
4891
4892 return kIOReturnSuccess;
4893 }
4894
4895 //******************************************************************************
4896 // deregisterPMSettingObject (private)
4897 //
4898 // Only called from PMSettingObject.
4899 //******************************************************************************
4900
4901 void
deregisterPMSettingObject(PMSettingObject * pmso)4902 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
4903 {
4904 thread_t thisThread = current_thread();
4905 PMSettingCallEntry *callEntry;
4906 OSSharedPtr<OSCollectionIterator> iter;
4907 OSSymbol *sym;
4908 OSArray *array;
4909 int index;
4910 bool wait;
4911
4912 PMSETTING_LOCK();
4913
4914 pmso->disabled = true;
4915
4916 // Wait for all callout threads to finish.
4917 do {
4918 wait = false;
4919 queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
4920 {
4921 if (callEntry->thread != thisThread) {
4922 wait = true;
4923 break;
4924 }
4925 }
4926 if (wait) {
4927 assert(NULL == pmso->waitThread);
4928 pmso->waitThread = thisThread;
4929 PMSETTING_WAIT(pmso);
4930 pmso->waitThread = NULL;
4931 }
4932 } while (wait);
4933
4934 // Search each PM settings array in the kernel.
4935 iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
4936 if (iter) {
4937 while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
4938 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
4939 index = array->getNextIndexOfObject(pmso, 0);
4940 if (-1 != index) {
4941 array->removeObject(index);
4942 }
4943 }
4944 }
4945
4946 PMSETTING_UNLOCK();
4947
4948 pmso->release();
4949 }
4950
4951 //******************************************************************************
4952 // informCPUStateChange
4953 //
4954 // Call into PM CPU code so that CPU power savings may dynamically adjust for
4955 // running on battery, with the lid closed, etc.
4956 //
4957 // informCPUStateChange is a no-op on non x86 systems
4958 // only x86 has explicit support in the IntelCPUPowerManagement kext
4959 //******************************************************************************
4960
4961 void
informCPUStateChange(uint32_t type,uint32_t value)4962 IOPMrootDomain::informCPUStateChange(
4963 uint32_t type,
4964 uint32_t value )
4965 {
4966 #if defined(__i386__) || defined(__x86_64__)
4967
4968 pmioctlVariableInfo_t varInfoStruct;
4969 int pmCPUret = 0;
4970 const char *varNameStr = NULL;
4971 int32_t *varIndex = NULL;
4972
4973 if (kInformAC == type) {
4974 varNameStr = kIOPMRootDomainBatPowerCString;
4975 varIndex = &idxPMCPULimitedPower;
4976 } else if (kInformLid == type) {
4977 varNameStr = kIOPMRootDomainLidCloseCString;
4978 varIndex = &idxPMCPUClamshell;
4979 } else {
4980 return;
4981 }
4982
4983 // Set the new value!
4984 // pmCPUControl will assign us a new ID if one doesn't exist yet
4985 bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
4986 varInfoStruct.varID = *varIndex;
4987 varInfoStruct.varType = vBool;
4988 varInfoStruct.varInitValue = value;
4989 varInfoStruct.varCurValue = value;
4990 strlcpy((char *)varInfoStruct.varName,
4991 (const char *)varNameStr,
4992 sizeof(varInfoStruct.varName));
4993
4994 // Set!
4995 pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
4996
4997 // pmCPU only assigns numerical id's when a new varName is specified
4998 if ((0 == pmCPUret)
4999 && (*varIndex == kCPUUnknownIndex)) {
5000 // pmCPUControl has assigned us a new variable ID.
5001 // Let's re-read the structure we just SET to learn that ID.
5002 pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5003
5004 if (0 == pmCPUret) {
5005 // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5006 *varIndex = varInfoStruct.varID;
5007 }
5008 }
5009
5010 return;
5011
5012 #endif /* __i386__ || __x86_64__ */
5013 }
5014
5015 // MARK: -
5016 // MARK: Deep Sleep Policy
5017
5018 #if HIBERNATION
5019
5020 //******************************************************************************
5021 // evaluateSystemSleepPolicy
5022 //******************************************************************************
5023
5024 #define kIOPlatformSystemSleepPolicyKey "IOPlatformSystemSleepPolicy"
5025
5026 // Sleep flags
5027 enum {
5028 kIOPMSleepFlagHibernate = 0x00000001,
5029 kIOPMSleepFlagSleepTimerEnable = 0x00000002
5030 };
5031
5032 struct IOPMSystemSleepPolicyEntry {
5033 uint32_t factorMask;
5034 uint32_t factorBits;
5035 uint32_t sleepFlags;
5036 uint32_t wakeEvents;
5037 } __attribute__((packed));
5038
5039 struct IOPMSystemSleepPolicyTable {
5040 uint32_t signature;
5041 uint16_t version;
5042 uint16_t entryCount;
5043 IOPMSystemSleepPolicyEntry entries[];
5044 } __attribute__((packed));
5045
5046 enum {
5047 kIOPMSleepAttributeHibernateSetup = 0x00000001,
5048 kIOPMSleepAttributeHibernateSleep = 0x00000002
5049 };
5050
5051 static uint32_t
getSleepTypeAttributes(uint32_t sleepType)5052 getSleepTypeAttributes( uint32_t sleepType )
5053 {
5054 static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5055 {
5056 /* invalid */ 0,
5057 /* abort */ 0,
5058 /* normal */ 0,
5059 /* safesleep */ kIOPMSleepAttributeHibernateSetup,
5060 /* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5061 /* standby */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5062 /* poweroff */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5063 /* deepidle */ 0
5064 };
5065
5066 if (sleepType >= kIOPMSleepTypeLast) {
5067 return 0;
5068 }
5069
5070 return sleepTypeAttributes[sleepType];
5071 }
5072
5073 bool
evaluateSystemSleepPolicy(IOPMSystemSleepParameters * params,int sleepPhase,uint32_t * hibMode)5074 IOPMrootDomain::evaluateSystemSleepPolicy(
5075 IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5076 {
5077 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5078
5079 static const IONamedValue factorValues[] = {
5080 SLEEP_FACTOR( SleepTimerWake ),
5081 SLEEP_FACTOR( LidOpen ),
5082 SLEEP_FACTOR( ACPower ),
5083 SLEEP_FACTOR( BatteryLow ),
5084 SLEEP_FACTOR( StandbyNoDelay ),
5085 SLEEP_FACTOR( StandbyForced ),
5086 SLEEP_FACTOR( StandbyDisabled ),
5087 SLEEP_FACTOR( USBExternalDevice ),
5088 SLEEP_FACTOR( BluetoothHIDDevice ),
5089 SLEEP_FACTOR( ExternalMediaMounted ),
5090 SLEEP_FACTOR( ThunderboltDevice ),
5091 SLEEP_FACTOR( RTCAlarmScheduled ),
5092 SLEEP_FACTOR( MagicPacketWakeEnabled ),
5093 SLEEP_FACTOR( HibernateForced ),
5094 SLEEP_FACTOR( AutoPowerOffDisabled ),
5095 SLEEP_FACTOR( AutoPowerOffForced ),
5096 SLEEP_FACTOR( ExternalDisplay ),
5097 SLEEP_FACTOR( NetworkKeepAliveActive ),
5098 SLEEP_FACTOR( LocalUserActivity ),
5099 SLEEP_FACTOR( HibernateFailed ),
5100 SLEEP_FACTOR( ThermalWarning ),
5101 SLEEP_FACTOR( DisplayCaptured ),
5102 { 0, NULL }
5103 };
5104
5105 const IOPMSystemSleepPolicyTable * pt;
5106 OSSharedPtr<OSObject> prop;
5107 OSData * policyData;
5108 uint64_t currentFactors = 0;
5109 char currentFactorsBuf[512];
5110 uint32_t standbyDelay = 0;
5111 uint32_t powerOffDelay = 0;
5112 uint32_t powerOffTimer = 0;
5113 uint32_t standbyTimer = 0;
5114 uint32_t mismatch;
5115 bool standbyEnabled;
5116 bool powerOffEnabled;
5117 bool found = false;
5118
5119 // Get platform's sleep policy table
5120 if (!gSleepPolicyHandler) {
5121 prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5122 if (!prop) {
5123 goto done;
5124 }
5125 }
5126
5127 // Fetch additional settings
5128 standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5129 && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5130 powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5131 && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5132 if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5133 powerOffTimer = powerOffDelay;
5134 }
5135 if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5136 standbyTimer = standbyDelay;
5137 }
5138
5139 DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5140 sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5141 powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5142
5143 currentFactorsBuf[0] = 0;
5144 // pmset level overrides
5145 if ((*hibMode & kIOHibernateModeOn) == 0) {
5146 if (!gSleepPolicyHandler) {
5147 standbyEnabled = false;
5148 powerOffEnabled = false;
5149 }
5150 } else if (!(*hibMode & kIOHibernateModeSleep)) {
5151 // Force hibernate (i.e. mode 25)
5152 // If standby is enabled, force standy.
5153 // If poweroff is enabled, force poweroff.
5154 if (standbyEnabled) {
5155 currentFactors |= kIOPMSleepFactorStandbyForced;
5156 } else if (powerOffEnabled) {
5157 currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5158 } else {
5159 currentFactors |= kIOPMSleepFactorHibernateForced;
5160 }
5161 }
5162
5163 // Current factors based on environment and assertions
5164 if (sleepTimerMaintenance) {
5165 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5166 }
5167 if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5168 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5169 }
5170 if (!clamshellClosed) {
5171 currentFactors |= kIOPMSleepFactorLidOpen;
5172 }
5173 if (acAdaptorConnected) {
5174 currentFactors |= kIOPMSleepFactorACPower;
5175 }
5176 if (lowBatteryCondition) {
5177 hibernateMode = 0;
5178 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5179 if ((hibernateMode & kIOHibernateModeOn) == 0) {
5180 DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5181 } else {
5182 currentFactors |= kIOPMSleepFactorBatteryLow;
5183 }
5184 }
5185 if (!standbyDelay || !standbyTimer) {
5186 currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5187 }
5188 if (standbyNixed || !standbyEnabled) {
5189 currentFactors |= kIOPMSleepFactorStandbyDisabled;
5190 }
5191 if (resetTimers) {
5192 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5193 currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5194 }
5195 if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5196 kIOPMDriverAssertionLevelOff) {
5197 currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5198 }
5199 if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5200 kIOPMDriverAssertionLevelOff) {
5201 currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5202 }
5203 if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5204 kIOPMDriverAssertionLevelOff) {
5205 currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5206 }
5207 if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5208 kIOPMDriverAssertionLevelOff) {
5209 currentFactors |= kIOPMSleepFactorThunderboltDevice;
5210 }
5211 if (_scheduledAlarmMask != 0) {
5212 currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5213 }
5214 if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5215 kIOPMDriverAssertionLevelOff) {
5216 currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5217 }
5218 #define TCPKEEPALIVE 1
5219 #if TCPKEEPALIVE
5220 if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5221 kIOPMDriverAssertionLevelOff) {
5222 currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5223 }
5224 #endif
5225 if (!powerOffEnabled) {
5226 currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5227 }
5228 if (desktopMode) {
5229 currentFactors |= kIOPMSleepFactorExternalDisplay;
5230 }
5231 if (userWasActive) {
5232 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5233 }
5234 if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5235 currentFactors |= kIOPMSleepFactorHibernateFailed;
5236 }
5237 if (thermalWarningState) {
5238 currentFactors |= kIOPMSleepFactorThermalWarning;
5239 }
5240
5241 for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5242 uint32_t factor = 1 << factorBit;
5243 if (factor & currentFactors) {
5244 strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5245 strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5246 }
5247 }
5248 DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5249
5250 if (gSleepPolicyHandler) {
5251 uint32_t savedHibernateMode;
5252 IOReturn result;
5253
5254 if (!gSleepPolicyVars) {
5255 gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5256 }
5257 gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5258 gSleepPolicyVars->version = kIOPMSystemSleepPolicyVersion;
5259 gSleepPolicyVars->currentCapability = _currentCapability;
5260 gSleepPolicyVars->highestCapability = _highestCapability;
5261 gSleepPolicyVars->sleepFactors = currentFactors;
5262 gSleepPolicyVars->sleepReason = lastSleepReason;
5263 gSleepPolicyVars->sleepPhase = sleepPhase;
5264 gSleepPolicyVars->standbyDelay = standbyDelay;
5265 gSleepPolicyVars->standbyTimer = standbyTimer;
5266 gSleepPolicyVars->poweroffDelay = powerOffDelay;
5267 gSleepPolicyVars->scheduledAlarms = _scheduledAlarmMask | _userScheduledAlarmMask;
5268 gSleepPolicyVars->poweroffTimer = powerOffTimer;
5269
5270 if (kIOPMSleepPhase0 == sleepPhase) {
5271 // preserve hibernateMode
5272 savedHibernateMode = gSleepPolicyVars->hibernateMode;
5273 gSleepPolicyVars->hibernateMode = *hibMode;
5274 } else if (kIOPMSleepPhase1 == sleepPhase) {
5275 // use original hibernateMode for phase2
5276 gSleepPolicyVars->hibernateMode = *hibMode;
5277 }
5278
5279 result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5280
5281 if (kIOPMSleepPhase0 == sleepPhase) {
5282 // restore hibernateMode
5283 gSleepPolicyVars->hibernateMode = savedHibernateMode;
5284 }
5285
5286 if ((result != kIOReturnSuccess) ||
5287 (kIOPMSleepTypeInvalid == params->sleepType) ||
5288 (params->sleepType >= kIOPMSleepTypeLast) ||
5289 (kIOPMSystemSleepParametersVersion != params->version)) {
5290 MSG("sleep policy handler error\n");
5291 goto done;
5292 }
5293
5294 if ((getSleepTypeAttributes(params->sleepType) &
5295 kIOPMSleepAttributeHibernateSetup) &&
5296 ((*hibMode & kIOHibernateModeOn) == 0)) {
5297 *hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5298 }
5299
5300 DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5301 params->version, params->sleepType, params->sleepFlags,
5302 params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5303 found = true;
5304 goto done;
5305 }
5306
5307 // Policy table is meaningless without standby enabled
5308 if (!standbyEnabled) {
5309 goto done;
5310 }
5311
5312 // Validate the sleep policy table
5313 policyData = OSDynamicCast(OSData, prop.get());
5314 if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5315 goto done;
5316 }
5317
5318 pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5319 if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5320 (pt->version != 1) || (0 == pt->entryCount)) {
5321 goto done;
5322 }
5323
5324 if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5325 (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5326 goto done;
5327 }
5328
5329 for (uint32_t i = 0; i < pt->entryCount; i++) {
5330 const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5331 mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5332
5333 DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5334 entry->factorMask, entry->factorBits,
5335 entry->sleepFlags, entry->wakeEvents, mismatch);
5336 if (mismatch) {
5337 continue;
5338 }
5339
5340 DLOG("^ found match\n");
5341 found = true;
5342
5343 params->version = kIOPMSystemSleepParametersVersion;
5344 params->reserved1 = 1;
5345 if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5346 params->sleepType = kIOPMSleepTypeStandby;
5347 } else {
5348 params->sleepType = kIOPMSleepTypeNormalSleep;
5349 }
5350
5351 params->ecWakeEvents = entry->wakeEvents;
5352 if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5353 if (kIOPMSleepPhase2 == sleepPhase) {
5354 clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5355
5356 if (!_standbyTimerResetSeconds ||
5357 (now_secs <= _standbyTimerResetSeconds)) {
5358 // Reset standby timer adjustment
5359 _standbyTimerResetSeconds = now_secs;
5360 DLOG("standby delay %u, reset %u\n",
5361 standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5362 } else if (standbyDelay) {
5363 // Shorten the standby delay timer
5364 clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5365 if (standbyDelay > elapsed) {
5366 standbyDelay -= elapsed;
5367 } else {
5368 standbyDelay = 1; // must be > 0
5369 }
5370 DLOG("standby delay %u, elapsed %u\n",
5371 standbyDelay, (uint32_t) elapsed);
5372 }
5373 }
5374 params->ecWakeTimer = standbyDelay;
5375 } else if (kIOPMSleepPhase2 == sleepPhase) {
5376 // A sleep that does not enable the sleep timer will reset
5377 // the standby delay adjustment.
5378 _standbyTimerResetSeconds = 0;
5379 }
5380 break;
5381 }
5382
5383 done:
5384 return found;
5385 }
5386
5387 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5388
5389 void
evaluateSystemSleepPolicyEarly(void)5390 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5391 {
5392 // Evaluate early (priority interest phase), before drivers sleep.
5393
5394 DLOG("%s\n", __FUNCTION__);
5395 removeProperty(kIOPMSystemSleepParametersKey);
5396
5397 // Full wake resets the standby timer delay adjustment
5398 if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5399 _standbyTimerResetSeconds = 0;
5400 }
5401
5402 hibernateDisabled = false;
5403 hibernateMode = 0;
5404 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5405
5406 // Save for late evaluation if sleep is aborted
5407 bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5408
5409 if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5410 &hibernateMode)) {
5411 if (!hibernateRetry &&
5412 ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5413 kIOPMSleepAttributeHibernateSetup) == 0)) {
5414 // skip hibernate setup
5415 hibernateDisabled = true;
5416 }
5417 }
5418
5419 // Publish IOPMSystemSleepType
5420 uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5421 if (sleepType == kIOPMSleepTypeInvalid) {
5422 // no sleep policy
5423 sleepType = kIOPMSleepTypeNormalSleep;
5424 if (hibernateMode & kIOHibernateModeOn) {
5425 sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5426 kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5427 }
5428 } else if ((sleepType == kIOPMSleepTypeStandby) &&
5429 (gEarlySystemSleepParams.ecPoweroffTimer)) {
5430 // report the lowest possible sleep state
5431 sleepType = kIOPMSleepTypePowerOff;
5432 }
5433
5434 setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5435 }
5436
5437 void
evaluateSystemSleepPolicyFinal(void)5438 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5439 {
5440 IOPMSystemSleepParameters params;
5441 OSSharedPtr<OSData> paramsData;
5442 bool wakeNow;
5443 // Evaluate sleep policy after sleeping drivers but before platform sleep.
5444
5445 DLOG("%s\n", __FUNCTION__);
5446
5447 bzero(¶ms, sizeof(params));
5448 wakeNow = false;
5449 if (evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase2, &hibernateMode)) {
5450 if ((kIOPMSleepTypeStandby == params.sleepType)
5451 && gIOHibernateStandbyDisabled && gSleepPolicyVars
5452 && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5453 & gSleepPolicyVars->sleepFactors))) {
5454 standbyNixed = true;
5455 wakeNow = true;
5456 }
5457 if (wakeNow
5458 || ((hibernateDisabled || hibernateAborted) &&
5459 (getSleepTypeAttributes(params.sleepType) &
5460 kIOPMSleepAttributeHibernateSetup))) {
5461 // Final evaluation picked a state requiring hibernation,
5462 // but hibernate isn't going to proceed. Arm a short sleep using
5463 // the early non-hibernate sleep parameters.
5464 bcopy(&gEarlySystemSleepParams, ¶ms, sizeof(params));
5465 params.sleepType = kIOPMSleepTypeAbortedSleep;
5466 params.ecWakeTimer = 1;
5467 if (standbyNixed) {
5468 resetTimers = true;
5469 } else {
5470 // Set hibernateRetry flag to force hibernate setup on the
5471 // next sleep.
5472 hibernateRetry = true;
5473 }
5474 DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5475 params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5476 } else {
5477 hibernateRetry = false;
5478 }
5479
5480 if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5481 resetTimers = false;
5482 }
5483
5484 paramsData = OSData::withBytes(¶ms, sizeof(params));
5485 if (paramsData) {
5486 setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5487 }
5488
5489 if (getSleepTypeAttributes(params.sleepType) &
5490 kIOPMSleepAttributeHibernateSleep) {
5491 // Disable sleep to force hibernation
5492 gIOHibernateMode &= ~kIOHibernateModeSleep;
5493 }
5494 }
5495 }
5496
5497 bool
getHibernateSettings(uint32_t * hibernateModePtr,uint32_t * hibernateFreeRatio,uint32_t * hibernateFreeTime)5498 IOPMrootDomain::getHibernateSettings(
5499 uint32_t * hibernateModePtr,
5500 uint32_t * hibernateFreeRatio,
5501 uint32_t * hibernateFreeTime )
5502 {
5503 // Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5504 // has updated the hibernateDisabled flag.
5505
5506 bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5507 getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5508 getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5509 if (hibernateDisabled) {
5510 *hibernateModePtr = 0;
5511 } else if (gSleepPolicyHandler) {
5512 *hibernateModePtr = hibernateMode;
5513 }
5514 DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5515 return ok;
5516 }
5517
5518 bool
getSleepOption(const char * key,uint32_t * option)5519 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5520 {
5521 OSSharedPtr<OSObject> optionsProp;
5522 OSDictionary * optionsDict;
5523 OSSharedPtr<OSObject> obj;
5524 OSNumber * num;
5525 bool ok = false;
5526
5527 optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5528 optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5529
5530 if (optionsDict) {
5531 obj.reset(optionsDict->getObject(key), OSRetain);
5532 }
5533 if (!obj) {
5534 obj = copyProperty(key);
5535 }
5536 if (obj) {
5537 if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5538 *option = num->unsigned32BitValue();
5539 ok = true;
5540 } else if (OSDynamicCast(OSBoolean, obj.get())) {
5541 *option = (obj == kOSBooleanTrue) ? 1 : 0;
5542 ok = true;
5543 }
5544 }
5545
5546 return ok;
5547 }
5548 #endif /* HIBERNATION */
5549
5550 IOReturn
getSystemSleepType(uint32_t * sleepType,uint32_t * standbyTimer)5551 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5552 {
5553 #if HIBERNATION
5554 IOPMSystemSleepParameters params;
5555 uint32_t hibMode = 0;
5556 bool ok;
5557
5558 if (gIOPMWorkLoop->inGate() == false) {
5559 IOReturn ret = gIOPMWorkLoop->runAction(
5560 OSMemberFunctionCast(IOWorkLoop::Action, this,
5561 &IOPMrootDomain::getSystemSleepType),
5562 (OSObject *) this,
5563 (void *) sleepType, (void *) standbyTimer);
5564 return ret;
5565 }
5566
5567 getSleepOption(kIOHibernateModeKey, &hibMode);
5568 bzero(¶ms, sizeof(params));
5569
5570 ok = evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase0, &hibMode);
5571 if (ok) {
5572 *sleepType = params.sleepType;
5573 if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5574 !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5575 DLOG("Standby delay is not set\n");
5576 *standbyTimer = 0;
5577 }
5578 return kIOReturnSuccess;
5579 }
5580 #endif
5581
5582 return kIOReturnUnsupported;
5583 }
5584
5585 // MARK: -
5586 // MARK: Shutdown and Restart
5587
5588 //******************************************************************************
5589 // handlePlatformHaltRestart
5590 //
5591 //******************************************************************************
5592
5593 // Phases while performing shutdown/restart
5594 typedef enum {
5595 kNotifyDone = 0x00,
5596 kNotifyPriorityClients = 0x10,
5597 kNotifyPowerPlaneDrivers = 0x20,
5598 kNotifyHaltRestartAction = 0x30,
5599 kQuiescePM = 0x40,
5600 } shutdownPhase_t;
5601
5602
5603 struct HaltRestartApplierContext {
5604 IOPMrootDomain * RootDomain;
5605 unsigned long PowerState;
5606 IOPMPowerFlags PowerFlags;
5607 UInt32 MessageType;
5608 UInt32 Counter;
5609 const char * LogString;
5610 shutdownPhase_t phase;
5611
5612 IOServiceInterestHandler handler;
5613 } gHaltRestartCtx;
5614
5615 const char *
shutdownPhase2String(shutdownPhase_t phase)5616 shutdownPhase2String(shutdownPhase_t phase)
5617 {
5618 switch (phase) {
5619 case kNotifyDone:
5620 return "Notifications completed";
5621 case kNotifyPriorityClients:
5622 return "Notifying priority clients";
5623 case kNotifyPowerPlaneDrivers:
5624 return "Notifying power plane drivers";
5625 case kNotifyHaltRestartAction:
5626 return "Notifying HaltRestart action handlers";
5627 case kQuiescePM:
5628 return "Quiescing PM";
5629 default:
5630 return "Unknown";
5631 }
5632 }
5633
5634 static void
platformHaltRestartApplier(OSObject * object,void * context)5635 platformHaltRestartApplier( OSObject * object, void * context )
5636 {
5637 IOPowerStateChangeNotification notify;
5638 HaltRestartApplierContext * ctx;
5639 AbsoluteTime startTime, elapsedTime;
5640 uint32_t deltaTime;
5641
5642 ctx = (HaltRestartApplierContext *) context;
5643
5644 _IOServiceInterestNotifier * notifier;
5645 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5646 memset(¬ify, 0, sizeof(notify));
5647 notify.powerRef = (void *)(uintptr_t)ctx->Counter;
5648 notify.returnValue = 0;
5649 notify.stateNumber = ctx->PowerState;
5650 notify.stateFlags = ctx->PowerFlags;
5651
5652 if (notifier) {
5653 ctx->handler = notifier->handler;
5654 }
5655
5656 clock_get_uptime(&startTime);
5657 ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)¬ify );
5658 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5659
5660 if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5661 LOG("%s handler %p took %u ms\n",
5662 ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5663 halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5664 }
5665
5666 ctx->handler = NULL;
5667 ctx->Counter++;
5668 }
5669
5670 static void
quiescePowerTreeCallback(void * target,void * param)5671 quiescePowerTreeCallback( void * target, void * param )
5672 {
5673 IOLockLock(gPMHaltLock);
5674 gPMQuiesced = true;
5675 thread_wakeup(param);
5676 IOLockUnlock(gPMHaltLock);
5677 }
5678
5679 void
handlePlatformHaltRestart(UInt32 pe_type)5680 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5681 {
5682 AbsoluteTime startTime, elapsedTime;
5683 uint32_t deltaTime;
5684
5685 memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5686 gHaltRestartCtx.RootDomain = this;
5687
5688 clock_get_uptime(&startTime);
5689 switch (pe_type) {
5690 case kPEHaltCPU:
5691 case kPEUPSDelayHaltCPU:
5692 gHaltRestartCtx.PowerState = OFF_STATE;
5693 gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5694 gHaltRestartCtx.LogString = "PowerOff";
5695 break;
5696
5697 case kPERestartCPU:
5698 gHaltRestartCtx.PowerState = RESTART_STATE;
5699 gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5700 gHaltRestartCtx.LogString = "Restart";
5701 break;
5702
5703 case kPEPagingOff:
5704 gHaltRestartCtx.PowerState = ON_STATE;
5705 gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5706 gHaltRestartCtx.LogString = "PagingOff";
5707 IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5708 #if HIBERNATION
5709 IOHibernateSystemRestart();
5710 #endif
5711 break;
5712
5713 default:
5714 return;
5715 }
5716
5717 gHaltRestartCtx.phase = kNotifyPriorityClients;
5718 // Notify legacy clients
5719 applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5720
5721 // For normal shutdown, turn off File Server Mode.
5722 if (kPEHaltCPU == pe_type) {
5723 OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5724 OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5725 if (setting && num) {
5726 setPMSetting(setting.get(), num.get());
5727 }
5728 }
5729
5730 if (kPEPagingOff != pe_type) {
5731 gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5732 // Notify in power tree order
5733 notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5734 }
5735
5736 gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5737 #if defined(XNU_TARGET_OS_OSX)
5738 IOCPURunPlatformHaltRestartActions(pe_type);
5739 #else /* !defined(XNU_TARGET_OS_OSX) */
5740 if (kPEPagingOff != pe_type) {
5741 IOCPURunPlatformHaltRestartActions(pe_type);
5742 }
5743 #endif /* !defined(XNU_TARGET_OS_OSX) */
5744
5745 // Wait for PM to quiesce
5746 if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5747 gHaltRestartCtx.phase = kQuiescePM;
5748 AbsoluteTime quiesceTime = mach_absolute_time();
5749
5750 IOLockLock(gPMHaltLock);
5751 gPMQuiesced = false;
5752 if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5753 kIOReturnSuccess) {
5754 while (!gPMQuiesced) {
5755 IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5756 }
5757 }
5758 IOLockUnlock(gPMHaltLock);
5759 deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5760 DLOG("PM quiesce took %u ms\n", deltaTime);
5761 halt_log_enter("Quiesce", NULL, elapsedTime);
5762 }
5763 gHaltRestartCtx.phase = kNotifyDone;
5764
5765 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5766 LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5767
5768 halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5769
5770 deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5771 LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5772
5773 if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5774 printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5775 }
5776
5777 checkShutdownTimeout();
5778 }
5779
5780 bool
checkShutdownTimeout()5781 IOPMrootDomain::checkShutdownTimeout()
5782 {
5783 AbsoluteTime elapsedTime;
5784 uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5785
5786 if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5787 return true;
5788 }
5789 return false;
5790 }
5791
5792 void
panicWithShutdownLog(uint32_t timeoutInMs)5793 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5794 {
5795 if (gHaltLog) {
5796 if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5797 halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5798 }
5799 panic("%s timed out in phase '%s'. Total %d ms:%s",
5800 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5801 } else {
5802 panic("%s timed out in phase \'%s\'. Total %d ms",
5803 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5804 }
5805 }
5806
5807 //******************************************************************************
5808 // shutdownSystem
5809 //
5810 //******************************************************************************
5811
5812 IOReturn
shutdownSystem(void)5813 IOPMrootDomain::shutdownSystem( void )
5814 {
5815 return kIOReturnUnsupported;
5816 }
5817
5818 //******************************************************************************
5819 // restartSystem
5820 //
5821 //******************************************************************************
5822
5823 IOReturn
restartSystem(void)5824 IOPMrootDomain::restartSystem( void )
5825 {
5826 return kIOReturnUnsupported;
5827 }
5828
5829 // MARK: -
5830 // MARK: System Capability
5831
5832 //******************************************************************************
5833 // tagPowerPlaneService
5834 //
5835 // Running on PM work loop thread.
5836 //******************************************************************************
5837
5838 void
tagPowerPlaneService(IOService * service,IOPMActions * actions,IOPMPowerStateIndex maxPowerState)5839 IOPMrootDomain::tagPowerPlaneService(
5840 IOService * service,
5841 IOPMActions * actions,
5842 IOPMPowerStateIndex maxPowerState )
5843 {
5844 uint32_t flags = 0;
5845
5846 memset(actions, 0, sizeof(*actions));
5847 actions->target = this;
5848
5849 if (service == this) {
5850 actions->actionPowerChangeStart =
5851 OSMemberFunctionCast(
5852 IOPMActionPowerChangeStart, this,
5853 &IOPMrootDomain::handleOurPowerChangeStart);
5854
5855 actions->actionPowerChangeDone =
5856 OSMemberFunctionCast(
5857 IOPMActionPowerChangeDone, this,
5858 &IOPMrootDomain::handleOurPowerChangeDone);
5859
5860 actions->actionPowerChangeOverride =
5861 OSMemberFunctionCast(
5862 IOPMActionPowerChangeOverride, this,
5863 &IOPMrootDomain::overrideOurPowerChange);
5864 return;
5865 }
5866
5867 #if DISPLAY_WRANGLER_PRESENT
5868 if (NULL != service->metaCast("IODisplayWrangler")) {
5869 // XXX should this really retain?
5870 wrangler.reset(service, OSRetain);
5871 wrangler->registerInterest(gIOGeneralInterest,
5872 &displayWranglerNotification, this, NULL);
5873
5874 // found the display wrangler, check for any display assertions already created
5875 if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5876 DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5877 wrangler->setIgnoreIdleTimer( true );
5878 }
5879 flags |= kPMActionsFlagIsDisplayWrangler;
5880 }
5881 #endif /* DISPLAY_WRANGLER_PRESENT */
5882
5883 if (service->propertyExists("IOPMStrictTreeOrder")) {
5884 flags |= kPMActionsFlagIsGraphicsDriver;
5885 }
5886 if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5887 flags |= kPMActionsFlagIsAudioDriver;
5888 }
5889
5890 // Find the power connection object that is a child of the PCI host
5891 // bridge, and has a graphics/audio device attached below. Mark the
5892 // power branch for delayed child notifications.
5893
5894 if (flags) {
5895 IORegistryEntry * child = service;
5896 IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
5897
5898 while (child != this) {
5899 if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
5900 // Skip delaying notifications and clamping power on external graphics and audio devices.
5901 DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
5902 flags = 0;
5903 break;
5904 }
5905 if ((parent == pciHostBridgeDriver) ||
5906 (parent == this)) {
5907 if (OSDynamicCast(IOPowerConnection, child)) {
5908 IOPowerConnection * conn = (IOPowerConnection *) child;
5909 conn->delayChildNotification = true;
5910 DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
5911 }
5912 break;
5913 }
5914 child = parent;
5915 parent = child->getParentEntry(gIOPowerPlane);
5916 }
5917 }
5918
5919 OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
5920 if (prop) {
5921 OSNumber * num = OSDynamicCast(OSNumber, prop.get());
5922 if (num) {
5923 actions->darkWakePowerState = num->unsigned32BitValue();
5924 if (actions->darkWakePowerState < maxPowerState) {
5925 flags |= kPMActionsFlagHasDarkWakePowerState;
5926 }
5927 }
5928 }
5929
5930
5931 if (flags) {
5932 DLOG("%s tag flags %x\n", service->getName(), flags);
5933 actions->flags |= flags;
5934 actions->actionPowerChangeOverride =
5935 OSMemberFunctionCast(
5936 IOPMActionPowerChangeOverride, this,
5937 &IOPMrootDomain::overridePowerChangeForService);
5938
5939 if (flags & kPMActionsFlagIsDisplayWrangler) {
5940 actions->actionActivityTickle =
5941 OSMemberFunctionCast(
5942 IOPMActionActivityTickle, this,
5943 &IOPMrootDomain::handleActivityTickleForDisplayWrangler);
5944
5945 actions->actionUpdatePowerClient =
5946 OSMemberFunctionCast(
5947 IOPMActionUpdatePowerClient, this,
5948 &IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
5949 }
5950 return;
5951 }
5952
5953 // Locate the first PCI host bridge for PMTrace.
5954 if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
5955 IOService * provider = service->getProvider();
5956 if (OSDynamicCast(IOPlatformDevice, provider) &&
5957 provider->inPlane(gIODTPlane)) {
5958 pciHostBridgeDevice.reset(provider, OSNoRetain);
5959 pciHostBridgeDriver.reset(service, OSNoRetain);
5960 DLOG("PMTrace found PCI host bridge %s->%s\n",
5961 provider->getName(), service->getName());
5962 }
5963 }
5964
5965 // Tag top-level PCI devices. The order of PMinit() call does not
5966 // change across boots and is used as the PCI bit number.
5967 if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
5968 // Would prefer to check built-in property, but tagPowerPlaneService()
5969 // is called before pciDevice->registerService().
5970 IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
5971 if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
5972 int bit = pmTracer->recordTopLevelPCIDevice( service );
5973 if (bit >= 0) {
5974 // Save the assigned bit for fast lookup.
5975 actions->flags |= (bit & kPMActionsPCIBitNumberMask);
5976
5977 actions->actionPowerChangeStart =
5978 OSMemberFunctionCast(
5979 IOPMActionPowerChangeStart, this,
5980 &IOPMrootDomain::handlePowerChangeStartForPCIDevice);
5981
5982 actions->actionPowerChangeDone =
5983 OSMemberFunctionCast(
5984 IOPMActionPowerChangeDone, this,
5985 &IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
5986 }
5987 }
5988 }
5989 }
5990
5991 //******************************************************************************
5992 // PM actions for root domain
5993 //******************************************************************************
5994
5995 void
overrideOurPowerChange(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)5996 IOPMrootDomain::overrideOurPowerChange(
5997 IOService * service,
5998 IOPMActions * actions,
5999 const IOPMRequest * request,
6000 IOPMPowerStateIndex * inOutPowerState,
6001 IOPMPowerChangeFlags * inOutChangeFlags )
6002 {
6003 uint32_t changeFlags = *inOutChangeFlags;
6004 uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6005 uint32_t currentPowerState = (uint32_t) getPowerState();
6006
6007 if (request->getTag() == 0) {
6008 // Set a tag for any request that originates from IOServicePM
6009 (const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6010 }
6011
6012 DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6013 getPowerStateString(currentPowerState),
6014 getPowerStateString(desiredPowerState),
6015 _currentCapability, changeFlags,
6016 request->getTag());
6017
6018
6019 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6020 /*
6021 * ASBM send lowBattery notifications every 1 second until the device
6022 * enters hibernation. This queues up multiple sleep requests.
6023 * After the device wakes from hibernation, none of these previously
6024 * queued sleep requests are valid.
6025 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6026 * and is cleared at the very last point in sleep.
6027 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6028 * lowBatteryCondition is invalid
6029 */
6030 if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6031 if (!lowBatteryCondition) {
6032 DLOG("Duplicate lowBattery sleep");
6033 *inOutChangeFlags |= kIOPMNotDone;
6034 return;
6035 }
6036 }
6037 #endif
6038
6039 if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6040 // Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6041 *inOutChangeFlags |= kIOPMNotDone;
6042 return;
6043 }
6044
6045 if (changeFlags & kIOPMParentInitiated) {
6046 // Root parent is permanently pegged at max power,
6047 // a parent initiated power change is unexpected.
6048 *inOutChangeFlags |= kIOPMNotDone;
6049 return;
6050 }
6051
6052 if (desiredPowerState < currentPowerState) {
6053 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6054 // Root domain is dropping power state from ON->SLEEP.
6055 // If system is in full wake, first enter dark wake by
6056 // converting the power drop to a capability change.
6057 // Once in dark wake, transition to sleep state ASAP.
6058
6059 darkWakeToSleepASAP = true;
6060
6061 // Drop graphics and audio capability
6062 _desiredCapability &= ~(
6063 kIOPMSystemCapabilityGraphics |
6064 kIOPMSystemCapabilityAudio);
6065
6066 // Convert to capability change (ON->ON)
6067 *inOutPowerState = getRUN_STATE();
6068 *inOutChangeFlags |= kIOPMSynchronize;
6069
6070 // Revert device desire from SLEEP to ON
6071 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6072 } else {
6073 // System is already in dark wake, ok to drop power state.
6074 // Broadcast root power down to entire tree.
6075 *inOutChangeFlags |= kIOPMRootChangeDown;
6076 }
6077 } else if (desiredPowerState > currentPowerState) {
6078 if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6079 // Broadcast power up when waking from sleep, but not for the
6080 // initial power change at boot by checking for cpu capability.
6081 *inOutChangeFlags |= kIOPMRootChangeUp;
6082 }
6083 }
6084 }
6085
6086 void
handleOurPowerChangeStart(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex newPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6087 IOPMrootDomain::handleOurPowerChangeStart(
6088 IOService * service,
6089 IOPMActions * actions,
6090 const IOPMRequest * request,
6091 IOPMPowerStateIndex newPowerState,
6092 IOPMPowerChangeFlags * inOutChangeFlags )
6093 {
6094 IOPMRequestTag requestTag = request->getTag();
6095 IOPMRequestTag sleepReason;
6096
6097 uint32_t changeFlags = *inOutChangeFlags;
6098 uint32_t currentPowerState = (uint32_t) getPowerState();
6099 bool publishSleepReason = false;
6100
6101 // Check if request has a valid sleep reason
6102 sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6103 if (sleepReason < kIOPMSleepReasonClamshell) {
6104 sleepReason = kIOPMSleepReasonIdle;
6105 }
6106
6107 _systemTransitionType = kSystemTransitionNone;
6108 _systemMessageClientMask = 0;
6109 capabilityLoss = false;
6110 toldPowerdCapWillChange = false;
6111
6112 // Emergency notifications may arrive after the initial sleep request
6113 // has been queued. Override the sleep reason so powerd and others can
6114 // treat this as an emergency sleep.
6115 if (lowBatteryCondition) {
6116 sleepReason = kIOPMSleepReasonLowPower;
6117 } else if (thermalEmergencyState) {
6118 sleepReason = kIOPMSleepReasonThermalEmergency;
6119 }
6120
6121 // 1. Explicit capability change.
6122 if (changeFlags & kIOPMSynchronize) {
6123 if (newPowerState == ON_STATE) {
6124 if (changeFlags & kIOPMSyncNoChildNotify) {
6125 _systemTransitionType = kSystemTransitionNewCapClient;
6126 } else {
6127 _systemTransitionType = kSystemTransitionCapability;
6128 }
6129 }
6130 }
6131 // 2. Going to sleep (cancellation still possible).
6132 else if (newPowerState < currentPowerState) {
6133 _systemTransitionType = kSystemTransitionSleep;
6134 }
6135 // 3. Woke from (idle or demand) sleep.
6136 else if (!systemBooting &&
6137 (changeFlags & kIOPMSelfInitiated) &&
6138 (newPowerState > currentPowerState)) {
6139 _systemTransitionType = kSystemTransitionWake;
6140 _desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6141
6142 // Early exit from dark wake to full (e.g. LID open)
6143 if (kFullWakeReasonNone != fullWakeReason) {
6144 _desiredCapability |= (
6145 kIOPMSystemCapabilityGraphics |
6146 kIOPMSystemCapabilityAudio);
6147
6148 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6149 if (fullWakeReason == kFullWakeReasonLocalUser) {
6150 darkWakeExit = true;
6151 darkWakeToSleepASAP = false;
6152 setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6153 kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6154 }
6155 #endif
6156 }
6157 #if HIBERNATION
6158 IOHibernateSetWakeCapabilities(_desiredCapability);
6159 #endif
6160 }
6161
6162 // Update pending wake capability at the beginning of every
6163 // state transition (including synchronize). This will become
6164 // the current capability at the end of the transition.
6165
6166 if (kSystemTransitionSleep == _systemTransitionType) {
6167 _pendingCapability = 0;
6168 capabilityLoss = true;
6169 } else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6170 _pendingCapability = _desiredCapability |
6171 kIOPMSystemCapabilityCPU |
6172 kIOPMSystemCapabilityNetwork;
6173
6174 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6175 _pendingCapability |= kIOPMSystemCapabilityAudio;
6176 }
6177
6178 if ((kSystemTransitionCapability == _systemTransitionType) &&
6179 (_pendingCapability == _currentCapability)) {
6180 // Cancel the PM state change.
6181 _systemTransitionType = kSystemTransitionNone;
6182 *inOutChangeFlags |= kIOPMNotDone;
6183 }
6184 if (__builtin_popcount(_pendingCapability) <
6185 __builtin_popcount(_currentCapability)) {
6186 capabilityLoss = true;
6187 }
6188 }
6189
6190 // 1. Capability change.
6191 if (kSystemTransitionCapability == _systemTransitionType) {
6192 // Dark to Full transition.
6193 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6194 tracePoint( kIOPMTracePointDarkWakeExit );
6195
6196 #if defined(XNU_TARGET_OS_OSX)
6197 // rdar://problem/65627936
6198 // When a dark->full wake promotion is scheduled before an ON->SLEEP
6199 // power state drop, invalidate any request to drop power state already
6200 // in the queue, including the override variant, unless full wake cannot
6201 // be sustained. Any power state drop queued after this SustainFullWake
6202 // request will not be affected.
6203 if (checkSystemCanSustainFullWake()) {
6204 changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6205 }
6206 #endif
6207
6208 willEnterFullWake();
6209 }
6210
6211 // Full to Dark transition.
6212 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6213 // Clear previous stats
6214 IOLockLock(pmStatsLock);
6215 if (pmStatsAppResponses) {
6216 pmStatsAppResponses = OSArray::withCapacity(5);
6217 }
6218 IOLockUnlock(pmStatsLock);
6219
6220 tracePoint( kIOPMTracePointDarkWakeEntry );
6221 *inOutChangeFlags |= kIOPMSyncTellPowerDown;
6222 _systemMessageClientMask = kSystemMessageClientPowerd |
6223 kSystemMessageClientLegacyApp;
6224
6225 // rdar://15971327
6226 // Prevent user active transitions before notifying clients
6227 // that system will sleep.
6228 preventTransitionToUserActive(true);
6229
6230 IOService::setAdvisoryTickleEnable( false );
6231
6232 // Publish the sleep reason for full to dark wake
6233 publishSleepReason = true;
6234 lastSleepReason = fullToDarkReason = sleepReason;
6235
6236 // Publish a UUID for the Sleep --> Wake cycle
6237 handlePublishSleepWakeUUID(true);
6238 if (sleepDelaysReport) {
6239 clock_get_uptime(&ts_sleepStart);
6240 DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6241 }
6242
6243 darkWakeExit = false;
6244 }
6245 }
6246 // 2. System sleep.
6247 else if (kSystemTransitionSleep == _systemTransitionType) {
6248 // Beginning of a system sleep transition.
6249 // Cancellation is still possible.
6250 tracePoint( kIOPMTracePointSleepStarted );
6251
6252 _systemMessageClientMask = kSystemMessageClientAll;
6253 if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6254 _systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6255 }
6256 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6257 // Kernel priority clients are only notified on the initial
6258 // transition to full wake, so don't notify them unless system
6259 // has gained graphics capability since the last system wake.
6260 _systemMessageClientMask &= ~kSystemMessageClientKernel;
6261 } else {
6262 // System was in full wake, but the downwards power transition is driven
6263 // by a request that originates from IOServicePM, so it isn't tagged with
6264 // a valid system sleep reason.
6265 if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6266 // Publish the same reason for full to dark
6267 sleepReason = fullToDarkReason;
6268 }
6269 }
6270 #if HIBERNATION
6271 gIOHibernateState = 0;
6272 #endif
6273
6274 // Record the reason for dark wake back to sleep
6275 // System may not have ever achieved full wake
6276
6277 publishSleepReason = true;
6278 lastSleepReason = sleepReason;
6279 if (sleepDelaysReport) {
6280 clock_get_uptime(&ts_sleepStart);
6281 DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6282 }
6283 }
6284 // 3. System wake.
6285 else if (kSystemTransitionWake == _systemTransitionType) {
6286 tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6287 // Clear stats about sleep
6288
6289 if (AOT_STATE == newPowerState) {
6290 _pendingCapability = 0;
6291 }
6292
6293 if (AOT_STATE == currentPowerState) {
6294 // Wake events are no longer accepted after waking to AOT_STATE.
6295 // Re-enable wake event acceptance to append wake events claimed
6296 // during the AOT to ON_STATE transition.
6297 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6298 }
6299
6300 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6301 willEnterFullWake();
6302 }
6303 }
6304
6305 // The only location where the sleep reason is published. At this point
6306 // sleep can still be cancelled, but sleep reason should be published
6307 // early for logging purposes.
6308
6309 if (publishSleepReason) {
6310 static const char * IOPMSleepReasons[] =
6311 {
6312 kIOPMClamshellSleepKey,
6313 kIOPMPowerButtonSleepKey,
6314 kIOPMSoftwareSleepKey,
6315 kIOPMOSSwitchHibernationKey,
6316 kIOPMIdleSleepKey,
6317 kIOPMLowPowerSleepKey,
6318 kIOPMThermalEmergencySleepKey,
6319 kIOPMMaintenanceSleepKey,
6320 kIOPMSleepServiceExitKey,
6321 kIOPMDarkWakeThermalEmergencyKey,
6322 kIOPMNotificationWakeExitKey
6323 };
6324
6325 // Record sleep cause in IORegistry
6326 uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6327 if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6328 DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6329 setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6330 }
6331 }
6332
6333 if ((kSystemTransitionNone != _systemTransitionType) &&
6334 (kSystemTransitionNewCapClient != _systemTransitionType)) {
6335 _systemStateGeneration++;
6336 systemDarkWake = false;
6337
6338 DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6339 getPowerStateString(currentPowerState),
6340 getPowerStateString((uint32_t) newPowerState),
6341 _currentCapability, _pendingCapability,
6342 *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6343 requestTag);
6344 }
6345
6346 if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6347 panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6348 }
6349 if (_aotNow && (ON_STATE == newPowerState)) {
6350 WAKEEVENT_LOCK();
6351 aotShouldExit(false, true);
6352 WAKEEVENT_UNLOCK();
6353 aotExit(false);
6354 }
6355 }
6356
6357 void
handleOurPowerChangeDone(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex oldPowerState,IOPMPowerChangeFlags changeFlags)6358 IOPMrootDomain::handleOurPowerChangeDone(
6359 IOService * service,
6360 IOPMActions * actions,
6361 const IOPMRequest * request,
6362 IOPMPowerStateIndex oldPowerState,
6363 IOPMPowerChangeFlags changeFlags )
6364 {
6365 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6366 _systemTransitionType = kSystemTransitionNone;
6367 return;
6368 }
6369
6370 if (_systemTransitionType != kSystemTransitionNone) {
6371 uint32_t currentPowerState = (uint32_t) getPowerState();
6372
6373 if (changeFlags & kIOPMNotDone) {
6374 // Power down was cancelled or vetoed.
6375 _pendingCapability = _currentCapability;
6376 lastSleepReason = 0;
6377
6378 // When sleep is cancelled or reverted, don't report
6379 // the target (lower) power state as the previous state.
6380 oldPowerState = currentPowerState;
6381
6382 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6383 CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6384 #if defined(XNU_TARGET_OS_OSX)
6385 pmPowerStateQueue->submitPowerEvent(
6386 kPowerEventPolicyStimulus,
6387 (void *) kStimulusDarkWakeReentry,
6388 _systemStateGeneration );
6389 #else /* !defined(XNU_TARGET_OS_OSX) */
6390 // On embedded, there are no factors that can prolong a
6391 // "darkWake" when a power down is vetoed. We need to
6392 // promote to "fullWake" at least once so that factors
6393 // that prevent idle sleep can assert themselves if required
6394 pmPowerStateQueue->submitPowerEvent(
6395 kPowerEventPolicyStimulus,
6396 (void *) kStimulusDarkWakeActivityTickle);
6397 #endif /* !defined(XNU_TARGET_OS_OSX) */
6398 }
6399
6400 // Revert device desire to max.
6401 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6402 } else {
6403 // Send message on dark wake to full wake promotion.
6404 // tellChangeUp() handles the normal SLEEP->ON case.
6405
6406 if (kSystemTransitionCapability == _systemTransitionType) {
6407 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6408 lastSleepReason = 0; // stop logging wrangler tickles
6409 tellClients(kIOMessageSystemHasPoweredOn);
6410 }
6411 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6412 // Going dark, reset full wake state
6413 // userIsActive will be cleared by wrangler powering down
6414 fullWakeReason = kFullWakeReasonNone;
6415
6416 if (ts_sleepStart) {
6417 clock_get_uptime(&wake2DarkwakeDelay);
6418 SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6419 DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6420 ts_sleepStart = 0;
6421 }
6422 }
6423 }
6424
6425 // Reset state after exiting from dark wake.
6426
6427 if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6428 CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6429 darkWakeMaintenance = false;
6430 darkWakeToSleepASAP = false;
6431 pciCantSleepValid = false;
6432 darkWakeSleepService = false;
6433
6434 if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6435 // Remove the influence of display power assertion
6436 // before next system wake.
6437 if (wrangler) {
6438 wrangler->changePowerStateForRootDomain(
6439 kWranglerPowerStateMin );
6440 }
6441 removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6442 }
6443 }
6444
6445 // Entered dark mode.
6446
6447 if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6448 (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6449 // Queue an evaluation of whether to remain in dark wake,
6450 // and for how long. This serves the purpose of draining
6451 // any assertions from the queue.
6452
6453 pmPowerStateQueue->submitPowerEvent(
6454 kPowerEventPolicyStimulus,
6455 (void *) kStimulusDarkWakeEntry,
6456 _systemStateGeneration );
6457 }
6458 }
6459
6460 DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6461 getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6462 _currentCapability, _pendingCapability,
6463 changeFlags, _systemStateGeneration, _systemMessageClientMask,
6464 request->getTag());
6465
6466 if ((currentPowerState == ON_STATE) && pmAssertions) {
6467 pmAssertions->reportCPUBitAccounting();
6468 }
6469
6470 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6471 displayWakeCnt++;
6472 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6473 if (clamshellExists && fullWakeThreadCall) {
6474 AbsoluteTime deadline;
6475 clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6476 thread_call_enter_delayed(fullWakeThreadCall, deadline);
6477 }
6478 #endif
6479 } else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6480 darkWakeCnt++;
6481 }
6482
6483 // Update current system capability.
6484 if (_currentCapability != _pendingCapability) {
6485 _currentCapability = _pendingCapability;
6486 }
6487
6488 // Update highest system capability.
6489
6490 _highestCapability |= _currentCapability;
6491
6492 if (darkWakePostTickle &&
6493 (kSystemTransitionWake == _systemTransitionType) &&
6494 (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6495 kDarkWakeFlagPromotionLate) {
6496 darkWakePostTickle = false;
6497 reportUserInput();
6498 } else if (darkWakeExit) {
6499 requestFullWake( kFullWakeReasonLocalUser );
6500 }
6501
6502 // Reset tracepoint at completion of capability change,
6503 // completion of wake transition, and aborted sleep transition.
6504
6505 if ((_systemTransitionType == kSystemTransitionCapability) ||
6506 (_systemTransitionType == kSystemTransitionWake) ||
6507 ((_systemTransitionType == kSystemTransitionSleep) &&
6508 (changeFlags & kIOPMNotDone))) {
6509 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6510 tracePoint( kIOPMTracePointSystemUp );
6511 }
6512
6513 _systemTransitionType = kSystemTransitionNone;
6514 _systemMessageClientMask = 0;
6515 toldPowerdCapWillChange = false;
6516
6517 darkWakeLogClamp = false;
6518
6519 if (lowBatteryCondition) {
6520 privateSleepSystem(kIOPMSleepReasonLowPower);
6521 } else if (thermalEmergencyState) {
6522 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6523 } else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6524 // Request for full wake is removed while system is waking up to full wake
6525 DLOG("DisplayOn fullwake request is removed\n");
6526 handleSetDisplayPowerOn(false);
6527 }
6528
6529 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6530 pmPowerStateQueue->submitPowerEvent(
6531 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6532 }
6533 }
6534 }
6535
6536 //******************************************************************************
6537 // PM actions for graphics and audio.
6538 //******************************************************************************
6539
6540 void
overridePowerChangeForService(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6541 IOPMrootDomain::overridePowerChangeForService(
6542 IOService * service,
6543 IOPMActions * actions,
6544 const IOPMRequest * request,
6545 IOPMPowerStateIndex * inOutPowerState,
6546 IOPMPowerChangeFlags * inOutChangeFlags )
6547 {
6548 uint32_t powerState = (uint32_t) *inOutPowerState;
6549 uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6550 const uint32_t actionFlags = actions->flags;
6551
6552 if (kSystemTransitionNone == _systemTransitionType) {
6553 // Not in midst of a system transition.
6554 // Do not set kPMActionsStatePowerClamped.
6555 } else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6556 bool enableClamp = false;
6557
6558 // For most drivers, enable the clamp during ON->Dark transition
6559 // which has the kIOPMSynchronize flag set in changeFlags.
6560 if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6561 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6562 (changeFlags & kIOPMSynchronize)) {
6563 enableClamp = true;
6564 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6565 ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6566 ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6567 (changeFlags & kIOPMSynchronize)) {
6568 enableClamp = true;
6569 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6570 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6571 (changeFlags & kIOPMSynchronize)) {
6572 enableClamp = true;
6573 } else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6574 (_systemTransitionType == kSystemTransitionSleep)) {
6575 // For graphics drivers, clamp power when entering
6576 // system sleep. Not when dropping to dark wake.
6577 enableClamp = true;
6578 }
6579
6580 if (enableClamp) {
6581 actions->state |= kPMActionsStatePowerClamped;
6582 DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6583 service->getName(), service->getRegistryEntryID(),
6584 _pendingCapability, powerState, changeFlags);
6585 }
6586 } else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6587 bool disableClamp = false;
6588
6589 if ((actionFlags & (
6590 kPMActionsFlagIsDisplayWrangler |
6591 kPMActionsFlagIsGraphicsDriver)) &&
6592 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6593 disableClamp = true;
6594 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6595 (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6596 disableClamp = true;
6597 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6598 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6599 disableClamp = true;
6600 }
6601
6602 if (disableClamp) {
6603 actions->state &= ~kPMActionsStatePowerClamped;
6604 DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6605 service->getName(), service->getRegistryEntryID(),
6606 _pendingCapability, powerState, changeFlags);
6607 }
6608 }
6609
6610 if (actions->state & kPMActionsStatePowerClamped) {
6611 uint32_t maxPowerState = 0;
6612
6613 // Determine the max power state allowed when clamp is enabled
6614 if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6615 // Parent intiated power state changes
6616 if ((service->getPowerState() > maxPowerState) &&
6617 (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6618 maxPowerState++;
6619
6620 // Remove lingering effects of any tickle before entering
6621 // dark wake. It will take a new tickle to return to full
6622 // wake, so the existing tickle state is useless.
6623
6624 if (changeFlags & kIOPMDomainDidChange) {
6625 *inOutChangeFlags |= kIOPMExpireIdleTimer;
6626 }
6627 } else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6628 maxPowerState++;
6629 } else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6630 maxPowerState = actions->darkWakePowerState;
6631 }
6632 } else {
6633 // Deny all self-initiated changes when power is limited.
6634 // Wrangler tickle should never defeat the limiter.
6635 maxPowerState = service->getPowerState();
6636 }
6637
6638 if (powerState > maxPowerState) {
6639 DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6640 service->getName(), service->getRegistryEntryID(),
6641 powerState, maxPowerState, changeFlags);
6642 *inOutPowerState = maxPowerState;
6643
6644 if (darkWakePostTickle &&
6645 (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6646 (changeFlags & kIOPMDomainWillChange) &&
6647 ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6648 kDarkWakeFlagPromotionEarly)) {
6649 darkWakePostTickle = false;
6650 reportUserInput();
6651 }
6652 }
6653
6654 if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6655 if (darkWakeLogClamp) {
6656 AbsoluteTime now;
6657 uint64_t nsec;
6658
6659 clock_get_uptime(&now);
6660 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6661 absolutetime_to_nanoseconds(now, &nsec);
6662 DLOG("dark wake power clamped after %u ms\n",
6663 ((int)((nsec) / NSEC_PER_MSEC)));
6664 }
6665 darkWakePowerClamped = true;
6666 }
6667 }
6668 }
6669
6670 void
handleActivityTickleForDisplayWrangler(IOService * service,IOPMActions * actions)6671 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6672 IOService * service,
6673 IOPMActions * actions )
6674 {
6675 #if DISPLAY_WRANGLER_PRESENT
6676 // Warning: Not running in PM work loop context - don't modify state !!!
6677 // Trap tickle directed to IODisplayWrangler while running with graphics
6678 // capability suppressed.
6679
6680 assert(service == wrangler);
6681
6682 clock_get_uptime(&userActivityTime);
6683 bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6684 || (lastSleepReason == kIOPMSleepReasonMaintenance)
6685 || (lastSleepReason == kIOPMSleepReasonSoftware));
6686 if (aborting) {
6687 userActivityCount++;
6688 DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6689 userActivityCount, lastSleepReason);
6690 }
6691
6692 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6693 DLOG("display wrangler tickled\n");
6694 if (kIOLogPMRootDomain & gIOKitDebug) {
6695 OSReportWithBacktrace("Dark wake display tickle");
6696 }
6697 if (pmPowerStateQueue) {
6698 pmPowerStateQueue->submitPowerEvent(
6699 kPowerEventPolicyStimulus,
6700 (void *) kStimulusDarkWakeActivityTickle,
6701 true /* set wake type */ );
6702 }
6703 }
6704 #endif /* DISPLAY_WRANGLER_PRESENT */
6705 }
6706
6707 void
handleUpdatePowerClientForDisplayWrangler(IOService * service,IOPMActions * actions,const OSSymbol * powerClient,IOPMPowerStateIndex oldPowerState,IOPMPowerStateIndex newPowerState)6708 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6709 IOService * service,
6710 IOPMActions * actions,
6711 const OSSymbol * powerClient,
6712 IOPMPowerStateIndex oldPowerState,
6713 IOPMPowerStateIndex newPowerState )
6714 {
6715 #if DISPLAY_WRANGLER_PRESENT
6716 assert(service == wrangler);
6717
6718 // This function implements half of the user active detection
6719 // by monitoring changes to the display wrangler's device desire.
6720 //
6721 // User becomes active when either:
6722 // 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6723 // in max power state. This desire change in absence of a power state
6724 // change is detected within. This handles the case when user becomes
6725 // active while the display is already lit by setDisplayPowerOn().
6726 //
6727 // 2. Power state change to max, and DeviceDesire is also at max.
6728 // Handled by displayWranglerNotification().
6729 //
6730 // User becomes inactive when DeviceDesire drops to sleep state or below.
6731
6732 DLOG("wrangler %s (ps %u, %u->%u)\n",
6733 powerClient->getCStringNoCopy(),
6734 (uint32_t) service->getPowerState(),
6735 (uint32_t) oldPowerState, (uint32_t) newPowerState);
6736
6737 if (powerClient == gIOPMPowerClientDevice) {
6738 if ((newPowerState > oldPowerState) &&
6739 (newPowerState == kWranglerPowerStateMax) &&
6740 (service->getPowerState() == kWranglerPowerStateMax)) {
6741 evaluatePolicy( kStimulusEnterUserActiveState );
6742 } else if ((newPowerState < oldPowerState) &&
6743 (newPowerState <= kWranglerPowerStateSleep)) {
6744 evaluatePolicy( kStimulusLeaveUserActiveState );
6745 }
6746 }
6747
6748 if (newPowerState <= kWranglerPowerStateSleep) {
6749 evaluatePolicy( kStimulusDisplayWranglerSleep );
6750 } else if (newPowerState == kWranglerPowerStateMax) {
6751 evaluatePolicy( kStimulusDisplayWranglerWake );
6752 }
6753 #endif /* DISPLAY_WRANGLER_PRESENT */
6754 }
6755
6756 //******************************************************************************
6757 // User active state management
6758 //******************************************************************************
6759
6760 void
preventTransitionToUserActive(bool prevent)6761 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6762 {
6763 #if DISPLAY_WRANGLER_PRESENT
6764 _preventUserActive = prevent;
6765 if (wrangler && !_preventUserActive) {
6766 // Allowing transition to user active, but the wrangler may have
6767 // already powered ON in case of sleep cancel/revert. Poll the
6768 // same conditions checked for in displayWranglerNotification()
6769 // to bring the user active state up to date.
6770
6771 if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6772 (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6773 kWranglerPowerStateMax)) {
6774 evaluatePolicy( kStimulusEnterUserActiveState );
6775 }
6776 }
6777 #endif /* DISPLAY_WRANGLER_PRESENT */
6778 }
6779
6780 //******************************************************************************
6781 // Approve usage of delayed child notification by PM.
6782 //******************************************************************************
6783
6784 bool
shouldDelayChildNotification(IOService * service)6785 IOPMrootDomain::shouldDelayChildNotification(
6786 IOService * service )
6787 {
6788 if ((kFullWakeReasonNone == fullWakeReason) &&
6789 (kSystemTransitionWake == _systemTransitionType)) {
6790 DLOG("%s: delay child notify\n", service->getName());
6791 return true;
6792 }
6793 return false;
6794 }
6795
6796 //******************************************************************************
6797 // PM actions for PCI device.
6798 //******************************************************************************
6799
6800 void
handlePowerChangeStartForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags * inOutChangeFlags)6801 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6802 IOService * service,
6803 IOPMActions * actions,
6804 const IOPMRequest * request,
6805 IOPMPowerStateIndex powerState,
6806 IOPMPowerChangeFlags * inOutChangeFlags )
6807 {
6808 pmTracer->tracePCIPowerChange(
6809 PMTraceWorker::kPowerChangeStart,
6810 service, *inOutChangeFlags,
6811 (actions->flags & kPMActionsPCIBitNumberMask));
6812 }
6813
6814 void
handlePowerChangeDoneForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags changeFlags)6815 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6816 IOService * service,
6817 IOPMActions * actions,
6818 const IOPMRequest * request,
6819 IOPMPowerStateIndex powerState,
6820 IOPMPowerChangeFlags changeFlags )
6821 {
6822 pmTracer->tracePCIPowerChange(
6823 PMTraceWorker::kPowerChangeCompleted,
6824 service, changeFlags,
6825 (actions->flags & kPMActionsPCIBitNumberMask));
6826 }
6827
6828 //******************************************************************************
6829 // registerInterest
6830 //
6831 // Override IOService::registerInterest() for root domain clients.
6832 //******************************************************************************
6833
6834 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6835 {
6836 friend class IOPMrootDomain;
6837 OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6838
6839 protected:
6840 uint32_t ackTimeoutCnt;
6841 uint32_t msgType; // Last type seen by the message filter
6842 uint32_t lastSleepWakeMsgType;
6843 uint32_t msgIndex;
6844 uint32_t maxMsgDelayMS;
6845 uint32_t maxAckDelayMS;
6846 uint64_t msgAbsTime;
6847 uint64_t uuid0;
6848 uint64_t uuid1;
6849 OSSharedPtr<const OSSymbol> identifier;
6850 OSSharedPtr<const OSSymbol> clientName;
6851 };
6852
OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier,_IOServiceInterestNotifier)6853 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
6854
6855 OSSharedPtr<IONotifier>
6856 IOPMrootDomain::registerInterest(
6857 const OSSymbol * typeOfInterest,
6858 IOServiceInterestHandler handler,
6859 void * target, void * ref )
6860 {
6861 IOPMServiceInterestNotifier* notifier;
6862 bool isSystemCapabilityClient;
6863 bool isKernelCapabilityClient;
6864 IOReturn rc = kIOReturnError;
6865
6866 isSystemCapabilityClient = typeOfInterest &&
6867 typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
6868
6869 isKernelCapabilityClient = typeOfInterest &&
6870 typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
6871
6872 if (isSystemCapabilityClient) {
6873 typeOfInterest = gIOAppPowerStateInterest;
6874 }
6875
6876 notifier = new IOPMServiceInterestNotifier;
6877 if (!notifier) {
6878 return NULL;
6879 }
6880
6881 if (notifier->init()) {
6882 rc = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
6883 }
6884 if (rc != kIOReturnSuccess) {
6885 OSSafeReleaseNULL(notifier);
6886 return NULL;
6887 }
6888
6889 notifier->ackTimeoutCnt = 0;
6890
6891 if (pmPowerStateQueue) {
6892 if (isSystemCapabilityClient) {
6893 notifier->retain();
6894 if (pmPowerStateQueue->submitPowerEvent(
6895 kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
6896 notifier->release();
6897 }
6898 }
6899
6900 if (isKernelCapabilityClient) {
6901 notifier->retain();
6902 if (pmPowerStateQueue->submitPowerEvent(
6903 kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
6904 notifier->release();
6905 }
6906 }
6907 }
6908
6909 OSSharedPtr<OSData> data;
6910 uint8_t *uuid = NULL;
6911 OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
6912 if (kext) {
6913 data = kext->copyUUID();
6914 }
6915 if (data && (data->getLength() == sizeof(uuid_t))) {
6916 uuid = (uint8_t *)(data->getBytesNoCopy());
6917
6918 notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
6919 ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
6920 ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
6921 notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
6922 ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
6923 ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
6924
6925 notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
6926 }
6927 return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
6928 }
6929
6930 //******************************************************************************
6931 // systemMessageFilter
6932 //
6933 //******************************************************************************
6934
6935 bool
systemMessageFilter(void * object,void * arg1,void * arg2,void * arg3)6936 IOPMrootDomain::systemMessageFilter(
6937 void * object, void * arg1, void * arg2, void * arg3 )
6938 {
6939 const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
6940 bool isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
6941 bool isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
6942 bool isCapClient = false;
6943 bool allow = false;
6944 OSBoolean **waitForReply = (typeof(waitForReply))arg3;
6945 IOPMServiceInterestNotifier *notifier;
6946
6947 notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
6948
6949 do {
6950 // When powerd and kernel priority clients register capability interest,
6951 // the power tree is sync'ed to inform those clients about the current
6952 // system capability. Only allow capability change messages during sync.
6953 if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
6954 (!isCapMsg || !_joinedCapabilityClients ||
6955 !_joinedCapabilityClients->containsObject((OSObject *) object))) {
6956 break;
6957 }
6958
6959 // Capability change message for powerd and kernel clients
6960 if (isCapMsg) {
6961 // Kernel priority clients
6962 if ((context->notifyType == kNotifyPriority) ||
6963 (context->notifyType == kNotifyCapabilityChangePriority)) {
6964 isCapClient = true;
6965 }
6966
6967 // powerd will maintain two client registrations with root domain.
6968 // isCapPowerd will be TRUE for any message targeting the powerd
6969 // exclusive (capability change) interest registration.
6970 if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
6971 isCapClient = true;
6972 }
6973 }
6974
6975 if (isCapClient) {
6976 IOPMSystemCapabilityChangeParameters * capArgs =
6977 (IOPMSystemCapabilityChangeParameters *) arg2;
6978
6979 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6980 capArgs->fromCapabilities = 0;
6981 capArgs->toCapabilities = _currentCapability;
6982 capArgs->changeFlags = 0;
6983 } else {
6984 capArgs->fromCapabilities = _currentCapability;
6985 capArgs->toCapabilities = _pendingCapability;
6986
6987 if (context->isPreChange) {
6988 capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
6989 } else {
6990 capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
6991 }
6992
6993 if (isCapPowerd && context->isPreChange) {
6994 toldPowerdCapWillChange = true;
6995 }
6996 }
6997
6998 // App level capability change messages must only go to powerd.
6999 // Wait for response post-change if capabilitiy is increasing.
7000 // Wait for response pre-change if capability is decreasing.
7001
7002 if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7003 ((capabilityLoss && context->isPreChange) ||
7004 (!capabilityLoss && !context->isPreChange))) {
7005 *waitForReply = kOSBooleanTrue;
7006 }
7007
7008 allow = true;
7009 break;
7010 }
7011
7012 // powerd will always receive CanSystemSleep, even for a demand sleep.
7013 // It will also have a final chance to veto sleep after all clients
7014 // have responded to SystemWillSleep
7015
7016 if ((kIOMessageCanSystemSleep == context->messageType) ||
7017 (kIOMessageSystemWillNotSleep == context->messageType)) {
7018 if (isCapPowerd) {
7019 allow = true;
7020 break;
7021 }
7022
7023 // Demand sleep, don't ask apps for permission
7024 if (context->changeFlags & kIOPMSkipAskPowerDown) {
7025 break;
7026 }
7027 }
7028
7029 if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7030 if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7031 (fullToDarkReason == kIOPMSleepReasonIdle)) {
7032 allow = true;
7033 }
7034 break;
7035 }
7036
7037 // Drop capability change messages for legacy clients.
7038 // Drop legacy system sleep messages for powerd capability interest.
7039 if (isCapMsg || isCapPowerd) {
7040 break;
7041 }
7042
7043 // Not a capability change message.
7044 // Perform message filtering based on _systemMessageClientMask.
7045
7046 if ((context->notifyType == kNotifyApps) &&
7047 (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7048 if (!notifier) {
7049 break;
7050 }
7051
7052 if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7053 (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7054 break; // drop any duplicate WillPowerOn for AOT devices
7055 }
7056
7057 allow = true;
7058
7059 if (waitForReply) {
7060 if (notifier->ackTimeoutCnt >= 3) {
7061 *waitForReply = kOSBooleanFalse;
7062 } else {
7063 *waitForReply = kOSBooleanTrue;
7064 }
7065 }
7066 } else if ((context->notifyType == kNotifyPriority) &&
7067 (_systemMessageClientMask & kSystemMessageClientKernel)) {
7068 allow = true;
7069 }
7070
7071 // Check sleep/wake message ordering
7072 if (allow) {
7073 if (context->messageType == kIOMessageSystemWillSleep ||
7074 context->messageType == kIOMessageSystemWillPowerOn ||
7075 context->messageType == kIOMessageSystemHasPoweredOn) {
7076 notifier->lastSleepWakeMsgType = context->messageType;
7077 }
7078 }
7079 } while (false);
7080
7081 if (allow && isCapMsg && _joinedCapabilityClients) {
7082 _joinedCapabilityClients->removeObject((OSObject *) object);
7083 if (_joinedCapabilityClients->getCount() == 0) {
7084 DMSG("destroyed capability client set %p\n",
7085 OBFUSCATE(_joinedCapabilityClients.get()));
7086 _joinedCapabilityClients.reset();
7087 }
7088 }
7089 if (notifier) {
7090 // Record the last seen message type even if the message is dropped
7091 // for traceFilteredNotification().
7092 notifier->msgType = context->messageType;
7093 }
7094
7095 return allow;
7096 }
7097
7098 //******************************************************************************
7099 // setMaintenanceWakeCalendar
7100 //
7101 //******************************************************************************
7102
7103 IOReturn
setMaintenanceWakeCalendar(const IOPMCalendarStruct * calendar)7104 IOPMrootDomain::setMaintenanceWakeCalendar(
7105 const IOPMCalendarStruct * calendar )
7106 {
7107 OSSharedPtr<OSData> data;
7108 IOReturn ret = 0;
7109
7110 if (!calendar) {
7111 return kIOReturnBadArgument;
7112 }
7113
7114 data = OSData::withBytes((void *) calendar, sizeof(*calendar));
7115 if (!data) {
7116 return kIOReturnNoMemory;
7117 }
7118
7119 if (kPMCalendarTypeMaintenance == calendar->selector) {
7120 ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7121 } else if (kPMCalendarTypeSleepService == calendar->selector) {
7122 ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7123 }
7124
7125 return ret;
7126 }
7127
7128 // MARK: -
7129 // MARK: Display Wrangler
7130
7131 //******************************************************************************
7132 // displayWranglerNotification
7133 //
7134 // Handle the notification when the IODisplayWrangler changes power state.
7135 //******************************************************************************
7136
7137 IOReturn
displayWranglerNotification(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgument,vm_size_t argSize)7138 IOPMrootDomain::displayWranglerNotification(
7139 void * target, void * refCon,
7140 UInt32 messageType, IOService * service,
7141 void * messageArgument, vm_size_t argSize )
7142 {
7143 #if DISPLAY_WRANGLER_PRESENT
7144 IOPMPowerStateIndex displayPowerState;
7145 IOPowerStateChangeNotification * params =
7146 (IOPowerStateChangeNotification *) messageArgument;
7147
7148 if ((messageType != kIOMessageDeviceWillPowerOff) &&
7149 (messageType != kIOMessageDeviceHasPoweredOn)) {
7150 return kIOReturnUnsupported;
7151 }
7152
7153 ASSERT_GATED();
7154 if (!gRootDomain) {
7155 return kIOReturnUnsupported;
7156 }
7157
7158 displayPowerState = params->stateNumber;
7159 DLOG("wrangler %s ps %d\n",
7160 getIOMessageString(messageType), (uint32_t) displayPowerState);
7161
7162 switch (messageType) {
7163 case kIOMessageDeviceWillPowerOff:
7164 // Display wrangler has dropped power due to display idle
7165 // or force system sleep.
7166 //
7167 // 4 Display ON kWranglerPowerStateMax
7168 // 3 Display Dim kWranglerPowerStateDim
7169 // 2 Display Sleep kWranglerPowerStateSleep
7170 // 1 Not visible to user
7171 // 0 Not visible to user kWranglerPowerStateMin
7172
7173 if (displayPowerState <= kWranglerPowerStateSleep) {
7174 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7175 }
7176 break;
7177
7178 case kIOMessageDeviceHasPoweredOn:
7179 // Display wrangler has powered on due to user activity
7180 // or wake from sleep.
7181
7182 if (kWranglerPowerStateMax == displayPowerState) {
7183 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7184
7185 // See comment in handleUpdatePowerClientForDisplayWrangler
7186 if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7187 kWranglerPowerStateMax) {
7188 gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7189 }
7190 }
7191 break;
7192 }
7193 #endif /* DISPLAY_WRANGLER_PRESENT */
7194 return kIOReturnUnsupported;
7195 }
7196
7197 //******************************************************************************
7198 // reportUserInput
7199 //
7200 //******************************************************************************
7201
7202 void
updateUserActivity(void)7203 IOPMrootDomain::updateUserActivity( void )
7204 {
7205 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7206 clock_get_uptime(&userActivityTime);
7207 bool aborting = ((lastSleepReason == kIOPMSleepReasonSoftware)
7208 || (lastSleepReason == kIOPMSleepReasonIdle)
7209 || (lastSleepReason == kIOPMSleepReasonMaintenance));
7210 if (aborting) {
7211 userActivityCount++;
7212 DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7213 }
7214 #endif
7215 }
7216 void
reportUserInput(void)7217 IOPMrootDomain::reportUserInput( void )
7218 {
7219 if (wrangler) {
7220 wrangler->activityTickle(0, 0);
7221 }
7222 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7223 // Update user activity
7224 updateUserActivity();
7225
7226 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7227 // update user active abs time
7228 clock_get_uptime(&gUserActiveAbsTime);
7229 pmPowerStateQueue->submitPowerEvent(
7230 kPowerEventPolicyStimulus,
7231 (void *) kStimulusDarkWakeActivityTickle,
7232 true /* set wake type */ );
7233 }
7234 #endif
7235 }
7236
7237 void
requestUserActive(IOService * device,const char * reason)7238 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7239 {
7240 #if DISPLAY_WRANGLER_PRESENT
7241 if (wrangler) {
7242 wrangler->activityTickle(0, 0);
7243 }
7244 #else
7245 if (!device) {
7246 DLOG("requestUserActive: device is null\n");
7247 return;
7248 }
7249 OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7250 uint64_t registryID = device->getRegistryEntryID();
7251
7252 if (!deviceName || !registryID) {
7253 DLOG("requestUserActive: no device name or registry entry\n");
7254 return;
7255 }
7256 const char *name = deviceName->getCStringNoCopy();
7257 char payload[128];
7258 snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7259 DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7260 messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7261 #endif
7262 }
7263
7264 //******************************************************************************
7265 // latchDisplayWranglerTickle
7266 //******************************************************************************
7267
7268 bool
latchDisplayWranglerTickle(bool latch)7269 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7270 {
7271 #if DISPLAY_WRANGLER_PRESENT
7272 if (latch) {
7273 if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7274 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7275 !checkSystemCanSustainFullWake()) {
7276 // Currently in dark wake, and not transitioning to full wake.
7277 // Full wake is unsustainable, so latch the tickle to prevent
7278 // the display from lighting up momentarily.
7279 wranglerTickled = true;
7280 } else {
7281 wranglerTickled = false;
7282 }
7283 } else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7284 wranglerTickled = false;
7285
7286 pmPowerStateQueue->submitPowerEvent(
7287 kPowerEventPolicyStimulus,
7288 (void *) kStimulusDarkWakeActivityTickle );
7289 }
7290
7291 return wranglerTickled;
7292 #else /* ! DISPLAY_WRANGLER_PRESENT */
7293 return false;
7294 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7295 }
7296
7297 //******************************************************************************
7298 // setDisplayPowerOn
7299 //
7300 // For root domain user client
7301 //******************************************************************************
7302
7303 void
setDisplayPowerOn(uint32_t options)7304 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7305 {
7306 pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7307 (void *) NULL, options );
7308 }
7309
7310 // MARK: -
7311 // MARK: System PM Policy
7312
7313 //******************************************************************************
7314 // checkSystemSleepAllowed
7315 //
7316 //******************************************************************************
7317
7318 bool
checkSystemSleepAllowed(IOOptionBits options,uint32_t sleepReason)7319 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7320 uint32_t sleepReason )
7321 {
7322 uint32_t err = 0;
7323
7324 // Conditions that prevent idle and demand system sleep.
7325
7326 do {
7327 if (gSleepDisabledFlag) {
7328 err = kPMConfigPreventSystemSleep;
7329 break;
7330 }
7331
7332 if (userDisabledAllSleep) {
7333 err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7334 break;
7335 }
7336
7337 if (systemBooting || systemShutdown || gWillShutdown) {
7338 err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7339 break;
7340 }
7341
7342 if (options == 0) {
7343 break;
7344 }
7345
7346 // Conditions above pegs the system at full wake.
7347 // Conditions below prevent system sleep but does not prevent
7348 // dark wake, and must be called from gated context.
7349
7350 #if !CONFIG_SLEEP
7351 err = kPMConfigPreventSystemSleep; // 3. config does not support sleep
7352 break;
7353 #endif
7354
7355 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7356 break; // always sleep on low battery or when in thermal warning/emergency state
7357 }
7358
7359 if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7360 break; // always sleep on dark wake thermal emergencies
7361 }
7362
7363 if (preventSystemSleepList->getCount() != 0) {
7364 err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7365 break;
7366 }
7367
7368 if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7369 kIOPMDriverAssertionLevelOn) {
7370 err = kPMCPUAssertion; // 5. CPU assertion
7371 break;
7372 }
7373
7374 if (pciCantSleepValid) {
7375 if (pciCantSleepFlag) {
7376 err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7377 }
7378 break;
7379 } else if (sleepSupportedPEFunction &&
7380 CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7381 IOReturn ret;
7382 OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7383 ret = getPlatform()->callPlatformFunction(
7384 sleepSupportedPEFunction.get(), false,
7385 NULL, NULL, NULL, NULL);
7386 pciCantSleepValid = true;
7387 pciCantSleepFlag = false;
7388 if ((platformSleepSupport & kPCICantSleep) ||
7389 ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7390 err = 6; // 6. PCI card does not support PM
7391 pciCantSleepFlag = true;
7392 break;
7393 }
7394 }
7395 }while (false);
7396
7397 if (err) {
7398 DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7399 return false;
7400 }
7401 return true;
7402 }
7403
7404 bool
checkSystemSleepEnabled(void)7405 IOPMrootDomain::checkSystemSleepEnabled( void )
7406 {
7407 return checkSystemSleepAllowed(0, 0);
7408 }
7409
7410 bool
checkSystemCanSleep(uint32_t sleepReason)7411 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7412 {
7413 ASSERT_GATED();
7414 return checkSystemSleepAllowed(1, sleepReason);
7415 }
7416
7417 //******************************************************************************
7418 // checkSystemCanSustainFullWake
7419 //******************************************************************************
7420
7421 bool
checkSystemCanSustainFullWake(void)7422 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7423 {
7424 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7425 // Low battery wake, or received a low battery notification
7426 // while system is awake. This condition will persist until
7427 // the following wake.
7428 return false;
7429 }
7430
7431 if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7432 // Graphics state is unknown and external display might not be probed.
7433 // Do not incorporate state that requires graphics to be in max power
7434 // such as desktopMode or clamshellDisabled.
7435
7436 if (!acAdaptorConnected) {
7437 DLOG("full wake check: no AC\n");
7438 return false;
7439 }
7440 }
7441 return true;
7442 }
7443
7444 //******************************************************************************
7445 // mustHibernate
7446 //******************************************************************************
7447
7448 #if HIBERNATION
7449
7450 bool
mustHibernate(void)7451 IOPMrootDomain::mustHibernate( void )
7452 {
7453 return lowBatteryCondition || thermalWarningState;
7454 }
7455
7456 #endif /* HIBERNATION */
7457
7458 //******************************************************************************
7459 // AOT
7460 //******************************************************************************
7461
7462 // Tables for accumulated days in year by month, latter used for leap years
7463
7464 static const unsigned int daysbymonth[] =
7465 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7466
7467 static const unsigned int lydaysbymonth[] =
7468 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7469
7470 static int __unused
IOPMConvertSecondsToCalendar(clock_sec_t secs,IOPMCalendarStruct * dt)7471 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7472 {
7473 const unsigned int * dbm = daysbymonth;
7474 clock_sec_t n, x, y, z;
7475
7476 // Calculate seconds, minutes and hours
7477
7478 n = secs % (24 * 3600);
7479 dt->second = n % 60;
7480 n /= 60;
7481 dt->minute = n % 60;
7482 dt->hour = (typeof(dt->hour))(n / 60);
7483
7484 // Calculate day of week
7485
7486 n = secs / (24 * 3600);
7487 // dt->dayWeek = (n + 4) % 7;
7488
7489 // Calculate year
7490 // Rebase from days since Unix epoch (1/1/1970) store in 'n',
7491 // to days since 1/1/1968 to start on 4 year cycle, beginning
7492 // on a leap year.
7493
7494 n += (366 + 365);
7495
7496 // Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7497 // Valid before 2100, since 2100 is not a leap year.
7498
7499 x = n / 1461; // number of 4 year cycles
7500 y = n % 1461; // days into current 4 year cycle
7501 z = 1968 + (4 * x);
7502
7503 // Add in years in the current 4 year cycle
7504
7505 if (y >= 366) {
7506 y -= 366; // days after the leap year
7507 n = y % 365; // days into the current year
7508 z += (1 + y / 365); // years after the past 4-yr cycle
7509 } else {
7510 n = y;
7511 dbm = lydaysbymonth;
7512 }
7513 if (z > 2099) {
7514 return 0;
7515 }
7516
7517 dt->year = (typeof(dt->year))z;
7518
7519 // Adjust remaining days value to start at 1
7520
7521 n += 1;
7522
7523 // Calculate month
7524
7525 for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7526 continue;
7527 }
7528 dt->month = (typeof(dt->month))x;
7529
7530 // Calculate day of month
7531
7532 dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7533
7534 return 1;
7535 }
7536
7537 static clock_sec_t
IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)7538 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7539 {
7540 const unsigned int * dbm = daysbymonth;
7541 long y, secs, days;
7542
7543 if (dt->year < 1970 || dt->month > 12) {
7544 return 0;
7545 }
7546
7547 // Seconds elapsed in the current day
7548
7549 secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7550
7551 // Number of days from 1/1/70 to beginning of current year
7552 // Account for extra day every 4 years starting at 1973
7553
7554 y = dt->year - 1970;
7555 days = (y * 365) + ((y + 1) / 4);
7556
7557 // Change table if current year is a leap year
7558
7559 if ((dt->year % 4) == 0) {
7560 dbm = lydaysbymonth;
7561 }
7562
7563 // Add in days elapsed in the current year
7564
7565 days += (dt->day - 1) + dbm[dt->month - 1];
7566
7567 // Add accumulated days to accumulated seconds
7568
7569 secs += 24 * 3600 * days;
7570
7571 return secs;
7572 }
7573
7574 unsigned long
getRUN_STATE(void)7575 IOPMrootDomain::getRUN_STATE(void)
7576 {
7577 return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7578 }
7579
7580 bool
isAOTMode()7581 IOPMrootDomain::isAOTMode()
7582 {
7583 return _aotNow;
7584 }
7585
7586 IOReturn
setWakeTime(uint64_t wakeContinuousTime)7587 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7588 {
7589 clock_sec_t nowsecs, wakesecs;
7590 clock_usec_t nowmicrosecs, wakemicrosecs;
7591 uint64_t nowAbs, wakeAbs;
7592
7593 clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7594 wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7595 if (wakeAbs < nowAbs) {
7596 printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7597 wakeAbs = nowAbs;
7598 }
7599 wakeAbs -= nowAbs;
7600 absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7601
7602 wakesecs += nowsecs;
7603 wakemicrosecs += nowmicrosecs;
7604 if (wakemicrosecs >= USEC_PER_SEC) {
7605 wakesecs++;
7606 wakemicrosecs -= USEC_PER_SEC;
7607 }
7608 if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7609 wakesecs++;
7610 }
7611
7612 IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7613
7614 if (_aotWakeTimeContinuous != wakeContinuousTime) {
7615 _aotWakeTimeContinuous = wakeContinuousTime;
7616 IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7617 }
7618 _aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7619 _aotWakeTimeUTC = wakesecs;
7620
7621 return kIOReturnSuccess;
7622 }
7623
7624 // assumes WAKEEVENT_LOCK
7625 bool
aotShouldExit(bool checkTimeSet,bool software)7626 IOPMrootDomain::aotShouldExit(bool checkTimeSet, bool software)
7627 {
7628 bool exitNow = false;
7629 const char * reason = "";
7630
7631 if (!_aotNow) {
7632 return false;
7633 }
7634
7635 if (software) {
7636 exitNow = true;
7637 _aotMetrics->softwareRequestCount++;
7638 reason = "software request";
7639 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7640 exitNow = true;
7641 reason = gWakeReasonString;
7642 } else if (checkTimeSet && (kPMCalendarTypeInvalid == _aotWakeTimeCalendar.selector)) {
7643 exitNow = true;
7644 _aotMetrics->noTimeSetCount++;
7645 reason = "flipbook expired";
7646 } else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7647 clock_sec_t sec;
7648 clock_usec_t usec;
7649 clock_get_calendar_microtime(&sec, &usec);
7650 if (_calendarWakeAlarmUTC <= sec) {
7651 exitNow = true;
7652 _aotMetrics->rtcAlarmsCount++;
7653 reason = "user alarm";
7654 }
7655 }
7656 if (exitNow) {
7657 _aotPendingFlags |= kIOPMWakeEventAOTExit;
7658 IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7659 reason,
7660 _aotMetrics->sleepCount,
7661 _aotMetrics->possibleCount,
7662 _aotMetrics->confirmedPossibleCount,
7663 _aotMetrics->rejectedPossibleCount,
7664 _aotMetrics->expiredPossibleCount,
7665 _aotMetrics->noTimeSetCount,
7666 _aotMetrics->rtcAlarmsCount);
7667 }
7668 return exitNow;
7669 }
7670
7671 void
aotExit(bool cps)7672 IOPMrootDomain::aotExit(bool cps)
7673 {
7674 uint32_t savedMessageMask;
7675
7676 ASSERT_GATED();
7677 _aotNow = false;
7678 _aotTasksSuspended = false;
7679 _aotReadyToFullWake = false;
7680 if (_aotTimerScheduled) {
7681 _aotTimerES->cancelTimeout();
7682 _aotTimerScheduled = false;
7683 }
7684 updateTasksSuspend();
7685
7686 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7687 _aotLastWakeTime = 0;
7688 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7689 WAKEEVENT_LOCK();
7690 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7691 gWakeReasonString,
7692 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7693 WAKEEVENT_UNLOCK();
7694 }
7695
7696 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7697
7698 // Preserve the message mask since a system wake transition
7699 // may have already started and initialized the mask.
7700 savedMessageMask = _systemMessageClientMask;
7701 _systemMessageClientMask = kSystemMessageClientLegacyApp;
7702 tellClients(kIOMessageSystemWillPowerOn);
7703 _systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7704
7705 if (cps) {
7706 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7707 }
7708 }
7709
7710 void
aotEvaluate(IOTimerEventSource * timer)7711 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7712 {
7713 bool exitNow;
7714
7715 IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7716
7717 WAKEEVENT_LOCK();
7718 exitNow = aotShouldExit(false, false);
7719 if (timer != NULL) {
7720 _aotTimerScheduled = false;
7721 }
7722 WAKEEVENT_UNLOCK();
7723 if (exitNow) {
7724 aotExit(true);
7725 } else {
7726 #if 0
7727 if (_aotLingerTime) {
7728 uint64_t deadline;
7729 IOLog("aot linger before sleep\n");
7730 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7731 clock_delay_until(deadline);
7732 }
7733 #endif
7734 privateSleepSystem(kIOPMSleepReasonSoftware);
7735 }
7736 }
7737
7738 //******************************************************************************
7739 // adjustPowerState
7740 //
7741 // Conditions that affect our wake/sleep decision has changed.
7742 // If conditions dictate that the system must remain awake, clamp power
7743 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7744 // is TRUE, then remove the power clamp and allow the power state to drop
7745 // to SLEEP_STATE.
7746 //******************************************************************************
7747
7748 void
adjustPowerState(bool sleepASAP)7749 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7750 {
7751 DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7752 getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7753
7754 ASSERT_GATED();
7755
7756 if (_aotNow) {
7757 bool exitNow;
7758
7759 if (AOT_STATE != getPowerState()) {
7760 return;
7761 }
7762 WAKEEVENT_LOCK();
7763 exitNow = aotShouldExit(true, false);
7764 if (!exitNow
7765 && !_aotTimerScheduled
7766 && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7767 _aotTimerScheduled = true;
7768 if (_aotLingerTime) {
7769 _aotTimerES->setTimeout(_aotLingerTime);
7770 } else {
7771 _aotTimerES->setTimeout(800, kMillisecondScale);
7772 }
7773 }
7774 WAKEEVENT_UNLOCK();
7775 if (exitNow) {
7776 aotExit(true);
7777 } else {
7778 _aotReadyToFullWake = true;
7779 if (!_aotTimerScheduled) {
7780 privateSleepSystem(kIOPMSleepReasonSoftware);
7781 }
7782 }
7783 return;
7784 }
7785
7786 if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
7787 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
7788 } else if (sleepASAP) {
7789 changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
7790 }
7791 }
7792
7793 void
handleSetDisplayPowerOn(bool powerOn)7794 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
7795 {
7796 if (powerOn) {
7797 if (!checkSystemCanSustainFullWake()) {
7798 DLOG("System cannot sustain full wake\n");
7799 return;
7800 }
7801
7802 // Force wrangler to max power state. If system is in dark wake
7803 // this alone won't raise the wrangler's power state.
7804 if (wrangler) {
7805 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
7806 }
7807
7808 // System in dark wake, always requesting full wake should
7809 // not have any bad side-effects, even if the request fails.
7810
7811 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
7812 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
7813 requestFullWake( kFullWakeReasonDisplayOn );
7814 }
7815 } else {
7816 // Relenquish desire to power up display.
7817 // Must first transition to state 1 since wrangler doesn't
7818 // power off the displays at state 0. At state 0 the root
7819 // domain is removed from the wrangler's power client list.
7820 if (wrangler) {
7821 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
7822 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
7823 }
7824 }
7825 }
7826
7827 //******************************************************************************
7828 // dispatchPowerEvent
7829 //
7830 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
7831 //******************************************************************************
7832
7833 void
dispatchPowerEvent(uint32_t event,void * arg0,uint64_t arg1)7834 IOPMrootDomain::dispatchPowerEvent(
7835 uint32_t event, void * arg0, uint64_t arg1 )
7836 {
7837 ASSERT_GATED();
7838
7839 switch (event) {
7840 case kPowerEventFeatureChanged:
7841 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7842 messageClients(kIOPMMessageFeatureChange, this);
7843 break;
7844
7845 case kPowerEventReceivedPowerNotification:
7846 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7847 handlePowerNotification((UInt32)(uintptr_t) arg0 );
7848 break;
7849
7850 case kPowerEventSystemBootCompleted:
7851 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7852 if (systemBooting) {
7853 systemBooting = false;
7854
7855 // read noidle setting from Device Tree
7856 if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
7857 DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
7858 }
7859 if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
7860 DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
7861 }
7862 if (lowBatteryCondition || thermalEmergencyState) {
7863 if (lowBatteryCondition) {
7864 privateSleepSystem(kIOPMSleepReasonLowPower);
7865 } else {
7866 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
7867 }
7868 // The rest is unnecessary since the system is expected
7869 // to sleep immediately. The following wake will update
7870 // everything.
7871 break;
7872 }
7873
7874 sleepWakeDebugMemAlloc();
7875 saveFailureData2File();
7876
7877 // If lid is closed, re-send lid closed notification
7878 // now that booting is complete.
7879 if (clamshellClosed) {
7880 handlePowerNotification(kLocalEvalClamshellCommand);
7881 }
7882 evaluatePolicy( kStimulusAllowSystemSleepChanged );
7883 }
7884 break;
7885
7886 case kPowerEventSystemShutdown:
7887 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7888 if (kOSBooleanTrue == (OSBoolean *) arg0) {
7889 /* We set systemShutdown = true during shutdown
7890 * to prevent sleep at unexpected times while loginwindow is trying
7891 * to shutdown apps and while the OS is trying to transition to
7892 * complete power of.
7893 *
7894 * Set to true during shutdown, as soon as loginwindow shows
7895 * the "shutdown countdown dialog", through individual app
7896 * termination, and through black screen kernel shutdown.
7897 */
7898 systemShutdown = true;
7899 } else {
7900 /*
7901 * A shutdown was initiated, but then the shutdown
7902 * was cancelled, clearing systemShutdown to false here.
7903 */
7904 systemShutdown = false;
7905 }
7906 break;
7907
7908 case kPowerEventUserDisabledSleep:
7909 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7910 userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
7911 break;
7912
7913 case kPowerEventRegisterSystemCapabilityClient:
7914 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7915
7916 // reset() handles the arg0 == nullptr case for us
7917 systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
7918 /* intentional fall-through */
7919 [[clang::fallthrough]];
7920
7921 case kPowerEventRegisterKernelCapabilityClient:
7922 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7923 if (!_joinedCapabilityClients) {
7924 _joinedCapabilityClients = OSSet::withCapacity(8);
7925 }
7926 if (arg0) {
7927 OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
7928 if (_joinedCapabilityClients) {
7929 _joinedCapabilityClients->setObject(notify.get());
7930 synchronizePowerTree( kIOPMSyncNoChildNotify );
7931 }
7932 }
7933 break;
7934
7935 case kPowerEventPolicyStimulus:
7936 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7937 if (arg0) {
7938 int stimulus = (int)(uintptr_t) arg0;
7939 evaluatePolicy(stimulus, (uint32_t) arg1);
7940 }
7941 break;
7942
7943 case kPowerEventAssertionCreate:
7944 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7945 if (pmAssertions) {
7946 pmAssertions->handleCreateAssertion((OSData *)arg0);
7947 }
7948 break;
7949
7950
7951 case kPowerEventAssertionRelease:
7952 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7953 if (pmAssertions) {
7954 pmAssertions->handleReleaseAssertion(arg1);
7955 }
7956 break;
7957
7958 case kPowerEventAssertionSetLevel:
7959 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7960 if (pmAssertions) {
7961 pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
7962 }
7963 break;
7964
7965 case kPowerEventQueueSleepWakeUUID:
7966 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7967 handleQueueSleepWakeUUID((OSObject *)arg0);
7968 break;
7969 case kPowerEventPublishSleepWakeUUID:
7970 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7971 handlePublishSleepWakeUUID((bool)arg0);
7972 break;
7973
7974 case kPowerEventSetDisplayPowerOn:
7975 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7976 if (arg1 != 0) {
7977 displayPowerOnRequested = true;
7978 } else {
7979 displayPowerOnRequested = false;
7980 }
7981 handleSetDisplayPowerOn(displayPowerOnRequested);
7982 break;
7983
7984 case kPowerEventPublishWakeType:
7985 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7986
7987 // Don't replace wake type property if already set
7988 if ((arg0 == gIOPMWakeTypeUserKey) ||
7989 !propertyExists(kIOPMRootDomainWakeTypeKey)) {
7990 const char * wakeType = NULL;
7991
7992 if (arg0 == gIOPMWakeTypeUserKey) {
7993 requestUserActive(this, "WakeTypeUser");
7994 wakeType = kIOPMRootDomainWakeTypeUser;
7995 } else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
7996 requestUserActive(this, "WakeTypeAlarm");
7997 wakeType = kIOPMRootDomainWakeTypeAlarm;
7998 } else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
7999 darkWakeSleepService = true;
8000 wakeType = kIOPMRootDomainWakeTypeSleepService;
8001 } else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8002 wakeType = kIOPMRootDomainWakeTypeMaintenance;
8003 }
8004
8005 if (wakeType) {
8006 setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8007 }
8008 }
8009 break;
8010
8011 case kPowerEventAOTEvaluate:
8012 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8013 if (_aotReadyToFullWake) {
8014 aotEvaluate(NULL);
8015 }
8016 break;
8017 }
8018 }
8019
8020 //******************************************************************************
8021 // systemPowerEventOccurred
8022 //
8023 // The power controller is notifying us of a hardware-related power management
8024 // event that we must handle.
8025 //
8026 // systemPowerEventOccurred covers the same functionality that
8027 // receivePowerNotification does; it simply provides a richer API for conveying
8028 // more information.
8029 //******************************************************************************
8030
8031 IOReturn
systemPowerEventOccurred(const OSSymbol * event,uint32_t intValue)8032 IOPMrootDomain::systemPowerEventOccurred(
8033 const OSSymbol *event,
8034 uint32_t intValue)
8035 {
8036 IOReturn attempt = kIOReturnSuccess;
8037 OSSharedPtr<OSNumber> newNumber;
8038
8039 if (!event) {
8040 return kIOReturnBadArgument;
8041 }
8042
8043 newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8044 if (!newNumber) {
8045 return kIOReturnInternalError;
8046 }
8047
8048 attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8049
8050 return attempt;
8051 }
8052
8053 void
setThermalState(OSObject * value)8054 IOPMrootDomain::setThermalState(OSObject *value)
8055 {
8056 OSNumber * num;
8057
8058 if (gIOPMWorkLoop->inGate() == false) {
8059 gIOPMWorkLoop->runAction(
8060 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8061 (OSObject *)this,
8062 (void *)value);
8063
8064 return;
8065 }
8066 if (value && (num = OSDynamicCast(OSNumber, value))) {
8067 thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8068 (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8069 }
8070 }
8071
8072 IOReturn
systemPowerEventOccurred(const OSSymbol * event,OSObject * value)8073 IOPMrootDomain::systemPowerEventOccurred(
8074 const OSSymbol *event,
8075 OSObject *value)
8076 {
8077 OSSharedPtr<OSDictionary> thermalsDict;
8078 bool shouldUpdate = true;
8079
8080 if (!event || !value) {
8081 return kIOReturnBadArgument;
8082 }
8083
8084 // LOCK
8085 // We reuse featuresDict Lock because it already exists and guards
8086 // the very infrequently used publish/remove feature mechanism; so there's zero rsk
8087 // of stepping on that lock.
8088 if (featuresDictLock) {
8089 IOLockLock(featuresDictLock);
8090 }
8091
8092 OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8093 OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8094
8095 if (origThermalsDict) {
8096 thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8097 } else {
8098 thermalsDict = OSDictionary::withCapacity(1);
8099 }
8100
8101 if (!thermalsDict) {
8102 shouldUpdate = false;
8103 goto exit;
8104 }
8105
8106 thermalsDict->setObject(event, value);
8107
8108 setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8109
8110 exit:
8111 // UNLOCK
8112 if (featuresDictLock) {
8113 IOLockUnlock(featuresDictLock);
8114 }
8115
8116 if (shouldUpdate) {
8117 if (event &&
8118 event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8119 setThermalState(value);
8120 }
8121 messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8122 }
8123
8124 return kIOReturnSuccess;
8125 }
8126
8127 //******************************************************************************
8128 // receivePowerNotification
8129 //
8130 // The power controller is notifying us of a hardware-related power management
8131 // event that we must handle. This may be a result of an 'environment' interrupt
8132 // from the power mgt micro.
8133 //******************************************************************************
8134
8135 IOReturn
receivePowerNotification(UInt32 msg)8136 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8137 {
8138 if (msg & kIOPMPowerButton) {
8139 uint32_t currentPhase = pmTracer->getTracePhase();
8140 if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8141 DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8142 swd_flags |= SWD_PWR_BTN_STACKSHOT;
8143 thread_call_enter(powerButtonDown);
8144 } else {
8145 DEBUG_LOG("power button pressed when system is up\n");
8146 }
8147 } else if (msg & kIOPMPowerButtonUp) {
8148 if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8149 swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8150 thread_call_enter(powerButtonUp);
8151 }
8152 } else {
8153 pmPowerStateQueue->submitPowerEvent(
8154 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8155 }
8156 return kIOReturnSuccess;
8157 }
8158
8159 void
handlePowerNotification(UInt32 msg)8160 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8161 {
8162 bool eval_clamshell = false;
8163 bool eval_clamshell_alarm = false;
8164
8165 ASSERT_GATED();
8166
8167 /*
8168 * Local (IOPMrootDomain only) eval clamshell command
8169 */
8170 if (msg & kLocalEvalClamshellCommand) {
8171 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8172 eval_clamshell_alarm = true;
8173
8174 // reset isRTCAlarmWake. This evaluation should happen only once
8175 // on RTC/Alarm wake. Any clamshell events after wake should follow
8176 // the regular evaluation
8177 isRTCAlarmWake = false;
8178 } else {
8179 eval_clamshell = true;
8180 }
8181 }
8182
8183 /*
8184 * Overtemp
8185 */
8186 if (msg & kIOPMOverTemp) {
8187 DLOG("Thermal overtemp message received!\n");
8188 thermalEmergencyState = true;
8189 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8190 }
8191
8192 /*
8193 * Forward DW thermal notification to client, if system is not going to sleep
8194 */
8195 if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8196 DLOG("DarkWake thermal limits message received!\n");
8197 messageClients(kIOPMMessageDarkWakeThermalEmergency);
8198 }
8199
8200 /*
8201 * Sleep Now!
8202 */
8203 if (msg & kIOPMSleepNow) {
8204 privateSleepSystem(kIOPMSleepReasonSoftware);
8205 }
8206
8207 /*
8208 * Power Emergency
8209 */
8210 if (msg & kIOPMPowerEmergency) {
8211 DLOG("Received kIOPMPowerEmergency");
8212 lowBatteryCondition = true;
8213 privateSleepSystem(kIOPMSleepReasonLowPower);
8214 }
8215
8216 /*
8217 * Clamshell OPEN
8218 */
8219 if (msg & kIOPMClamshellOpened) {
8220 DLOG("Clamshell opened\n");
8221 // Received clamshel open message from clamshell controlling driver
8222 // Update our internal state and tell general interest clients
8223 clamshellClosed = false;
8224 clamshellExists = true;
8225
8226 // Don't issue a hid tickle when lid is open and polled on wake
8227 if (msg & kIOPMSetValue) {
8228 setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8229 reportUserInput();
8230 }
8231
8232 // Tell PMCPU
8233 informCPUStateChange(kInformLid, 0);
8234
8235 // Tell general interest clients
8236 sendClientClamshellNotification();
8237
8238 bool aborting = ((lastSleepReason == kIOPMSleepReasonClamshell)
8239 || (lastSleepReason == kIOPMSleepReasonIdle)
8240 || (lastSleepReason == kIOPMSleepReasonMaintenance));
8241 if (aborting) {
8242 userActivityCount++;
8243 }
8244 DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8245 }
8246
8247 /*
8248 * Clamshell CLOSED
8249 * Send the clamshell interest notification since the lid is closing.
8250 */
8251 if (msg & kIOPMClamshellClosed) {
8252 if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8253 clamshellClosed && clamshellExists) {
8254 DLOG("Ignoring redundant Clamshell close event\n");
8255 } else {
8256 DLOG("Clamshell closed\n");
8257 // Received clamshel open message from clamshell controlling driver
8258 // Update our internal state and tell general interest clients
8259 clamshellClosed = true;
8260 clamshellExists = true;
8261
8262 // Ignore all following clamshell close events until the clamshell
8263 // is opened or the system sleeps. When a clamshell close triggers
8264 // a system wake, the lid driver may send us two clamshell close
8265 // events, one for the clamshell close event itself, and a second
8266 // close event when the driver polls the lid state on wake.
8267 clamshellIgnoreClose = true;
8268
8269 // Tell PMCPU
8270 informCPUStateChange(kInformLid, 1);
8271
8272 // Tell general interest clients
8273 sendClientClamshellNotification();
8274
8275 // And set eval_clamshell = so we can attempt
8276 eval_clamshell = true;
8277 }
8278 }
8279
8280 /*
8281 * Set Desktop mode (sent from graphics)
8282 *
8283 * -> reevaluate lid state
8284 */
8285 if (msg & kIOPMSetDesktopMode) {
8286 desktopMode = (0 != (msg & kIOPMSetValue));
8287 msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8288 DLOG("Desktop mode %d\n", desktopMode);
8289
8290 sendClientClamshellNotification();
8291
8292 // Re-evaluate the lid state
8293 eval_clamshell = true;
8294 }
8295
8296 /*
8297 * AC Adaptor connected
8298 *
8299 * -> reevaluate lid state
8300 */
8301 if (msg & kIOPMSetACAdaptorConnected) {
8302 acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8303 msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8304
8305 // Tell CPU PM
8306 informCPUStateChange(kInformAC, !acAdaptorConnected);
8307
8308 // Tell BSD if AC is connected
8309 // 0 == external power source; 1 == on battery
8310 post_sys_powersource(acAdaptorConnected ? 0:1);
8311
8312 sendClientClamshellNotification();
8313
8314 IOUserServer::powerSourceChanged(acAdaptorConnected);
8315
8316 // Re-evaluate the lid state
8317 eval_clamshell = true;
8318
8319 // Lack of AC may have latched a display wrangler tickle.
8320 // This mirrors the hardware's USB wake event latch, where a latched
8321 // USB wake event followed by an AC attach will trigger a full wake.
8322 latchDisplayWranglerTickle( false );
8323
8324 #if HIBERNATION
8325 // AC presence will reset the standy timer delay adjustment.
8326 _standbyTimerResetSeconds = 0;
8327 #endif
8328 if (!userIsActive) {
8329 // Reset userActivityTime when power supply is changed(rdr 13789330)
8330 clock_get_uptime(&userActivityTime);
8331 }
8332 }
8333
8334 /*
8335 * Enable Clamshell (external display disappear)
8336 *
8337 * -> reevaluate lid state
8338 */
8339 if (msg & kIOPMEnableClamshell) {
8340 DLOG("Clamshell enabled\n");
8341
8342 // Re-evaluate the lid state
8343 // System should sleep on external display disappearance
8344 // in lid closed operation.
8345 if (true == clamshellDisabled) {
8346 eval_clamshell = true;
8347
8348 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8349 // Also clear kClamshellSleepDisableInternal when graphics enables
8350 // the clamshell during a full wake. When graphics is behaving as
8351 // expected, this will allow clamshell close to be honored earlier
8352 // rather than waiting for the delayed evaluation.
8353 if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8354 (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8355 CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8356 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8357
8358 // Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8359 // when timer expires which is harmless but useless.
8360 thread_call_cancel(fullWakeThreadCall);
8361 }
8362 #endif
8363 }
8364
8365 clamshellDisabled = false;
8366 sendClientClamshellNotification();
8367 }
8368
8369 /*
8370 * Disable Clamshell (external display appeared)
8371 * We don't bother re-evaluating clamshell state. If the system is awake,
8372 * the lid is probably open.
8373 */
8374 if (msg & kIOPMDisableClamshell) {
8375 DLOG("Clamshell disabled\n");
8376 clamshellDisabled = true;
8377 sendClientClamshellNotification();
8378 }
8379
8380 /*
8381 * Evaluate clamshell and SLEEP if appropriate
8382 */
8383 if (eval_clamshell_alarm && clamshellClosed) {
8384 if (shouldSleepOnRTCAlarmWake()) {
8385 privateSleepSystem(kIOPMSleepReasonClamshell);
8386 }
8387 } else if (eval_clamshell && clamshellClosed) {
8388 if (shouldSleepOnClamshellClosed()) {
8389 privateSleepSystem(kIOPMSleepReasonClamshell);
8390 } else {
8391 evaluatePolicy( kStimulusDarkWakeEvaluate );
8392 }
8393 }
8394
8395 if (msg & kIOPMProModeEngaged) {
8396 int newState = 1;
8397 DLOG("ProModeEngaged\n");
8398 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8399 }
8400
8401 if (msg & kIOPMProModeDisengaged) {
8402 int newState = 0;
8403 DLOG("ProModeDisengaged\n");
8404 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8405 }
8406 }
8407
8408 //******************************************************************************
8409 // evaluatePolicy
8410 //
8411 // Evaluate root-domain policy in response to external changes.
8412 //******************************************************************************
8413
8414 void
evaluatePolicy(int stimulus,uint32_t arg)8415 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8416 {
8417 union {
8418 struct {
8419 int idleSleepEnabled : 1;
8420 int idleSleepDisabled : 1;
8421 int displaySleep : 1;
8422 int sleepDelayChanged : 1;
8423 int evaluateDarkWake : 1;
8424 int adjustPowerState : 1;
8425 int userBecameInactive : 1;
8426 int displaySleepEntry : 1;
8427 } bit;
8428 uint32_t u32;
8429 } flags;
8430
8431
8432 ASSERT_GATED();
8433 flags.u32 = 0;
8434
8435 switch (stimulus) {
8436 case kStimulusDisplayWranglerSleep:
8437 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8438 if (!wranglerPowerOff) {
8439 // wrangler is in sleep state or lower
8440 flags.bit.displaySleep = true;
8441 }
8442 if (!wranglerAsleep) {
8443 // transition from wrangler wake to wrangler sleep
8444 flags.bit.displaySleepEntry = true;
8445 wranglerAsleep = true;
8446 }
8447 break;
8448
8449 case kStimulusDisplayWranglerWake:
8450 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8451 displayIdleForDemandSleep = false;
8452 wranglerPowerOff = false;
8453 wranglerAsleep = false;
8454 break;
8455
8456 case kStimulusEnterUserActiveState:
8457 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8458 if (_preventUserActive) {
8459 DLOG("user active dropped\n");
8460 break;
8461 }
8462 if (!userIsActive) {
8463 userIsActive = true;
8464 userWasActive = true;
8465 clock_get_uptime(&gUserActiveAbsTime);
8466
8467 // Stay awake after dropping demand for display power on
8468 if (kFullWakeReasonDisplayOn == fullWakeReason) {
8469 fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8470 DLOG("User activity while in notification wake\n");
8471 changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8472 }
8473
8474 kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8475 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8476 messageClients(kIOPMMessageUserIsActiveChanged);
8477 }
8478 flags.bit.idleSleepDisabled = true;
8479 break;
8480
8481 case kStimulusLeaveUserActiveState:
8482 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8483 if (userIsActive) {
8484 clock_get_uptime(&gUserInactiveAbsTime);
8485 userIsActive = false;
8486 clock_get_uptime(&userBecameInactiveTime);
8487 flags.bit.userBecameInactive = true;
8488
8489 kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8490 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8491 messageClients(kIOPMMessageUserIsActiveChanged);
8492 }
8493 break;
8494
8495 case kStimulusAggressivenessChanged:
8496 {
8497 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8498 unsigned long aggressiveValue;
8499 uint32_t minutesToIdleSleep = 0;
8500 uint32_t minutesToDisplayDim = 0;
8501 uint32_t minutesDelta = 0;
8502
8503 // Fetch latest display and system sleep slider values.
8504 aggressiveValue = 0;
8505 getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8506 minutesToIdleSleep = (uint32_t) aggressiveValue;
8507
8508 aggressiveValue = 0;
8509 getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8510 minutesToDisplayDim = (uint32_t) aggressiveValue;
8511 DLOG("aggressiveness changed: system %u->%u, display %u\n",
8512 sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8513
8514 DLOG("idle time -> %d ms (ena %d)\n",
8515 idleMilliSeconds, (minutesToIdleSleep != 0));
8516
8517 // How long to wait before sleeping the system once
8518 // the displays turns off is indicated by 'extraSleepDelay'.
8519
8520 if (minutesToIdleSleep > minutesToDisplayDim) {
8521 minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8522 } else if (minutesToIdleSleep == minutesToDisplayDim) {
8523 minutesDelta = 1;
8524 }
8525
8526 if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8527 idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8528 }
8529
8530 if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8531 flags.bit.idleSleepDisabled = true;
8532 idleSleepEnabled = false;
8533 }
8534 #if !defined(XNU_TARGET_OS_OSX)
8535 if (0x7fffffff == minutesToIdleSleep) {
8536 minutesToIdleSleep = idleMilliSeconds / 1000;
8537 }
8538 #endif /* !defined(XNU_TARGET_OS_OSX) */
8539
8540 if (((minutesDelta != extraSleepDelay) ||
8541 (userActivityTime != userActivityTime_prev)) &&
8542 !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8543 flags.bit.sleepDelayChanged = true;
8544 }
8545
8546 if (systemDarkWake && !darkWakeToSleepASAP &&
8547 (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8548 // Reconsider decision to remain in dark wake
8549 flags.bit.evaluateDarkWake = true;
8550 }
8551
8552 sleepSlider = minutesToIdleSleep;
8553 extraSleepDelay = minutesDelta;
8554 userActivityTime_prev = userActivityTime;
8555 } break;
8556
8557 case kStimulusDemandSystemSleep:
8558 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8559 displayIdleForDemandSleep = true;
8560 if (wrangler && wranglerIdleSettings) {
8561 // Request wrangler idle only when demand sleep is triggered
8562 // from full wake.
8563 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8564 wrangler->setProperties(wranglerIdleSettings.get());
8565 DLOG("Requested wrangler idle\n");
8566 }
8567 }
8568 // arg = sleepReason
8569 changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8570 break;
8571
8572 case kStimulusAllowSystemSleepChanged:
8573 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8574 flags.bit.adjustPowerState = true;
8575 break;
8576
8577 case kStimulusDarkWakeActivityTickle:
8578 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8579 // arg == true implies real and not self generated wrangler tickle.
8580 // Update wake type on PM work loop instead of the tickle thread to
8581 // eliminate the possibility of an early tickle clobbering the wake
8582 // type set by the platform driver.
8583 if (arg == true) {
8584 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8585 }
8586
8587 if (!darkWakeExit) {
8588 if (latchDisplayWranglerTickle(true)) {
8589 DLOG("latched tickle\n");
8590 break;
8591 }
8592
8593 darkWakeExit = true;
8594 DLOG("Requesting full wake due to dark wake activity tickle\n");
8595 requestFullWake( kFullWakeReasonLocalUser );
8596 }
8597 break;
8598
8599 case kStimulusDarkWakeEntry:
8600 case kStimulusDarkWakeReentry:
8601 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8602 // Any system transitions since the last dark wake transition
8603 // will invalid the stimulus.
8604
8605 if (arg == _systemStateGeneration) {
8606 DLOG("dark wake entry\n");
8607 systemDarkWake = true;
8608
8609 // Keep wranglerPowerOff an invariant when wrangler is absent
8610 if (wrangler) {
8611 wranglerPowerOff = true;
8612 }
8613
8614 if (kStimulusDarkWakeEntry == stimulus) {
8615 clock_get_uptime(&userBecameInactiveTime);
8616 flags.bit.evaluateDarkWake = true;
8617 if (activitySinceSleep()) {
8618 DLOG("User activity recorded while going to darkwake\n");
8619 reportUserInput();
8620 }
8621 }
8622
8623 // Always accelerate disk spindown while in dark wake,
8624 // even if system does not support/allow sleep.
8625
8626 cancelIdleSleepTimer();
8627 setQuickSpinDownTimeout();
8628 }
8629 break;
8630
8631 case kStimulusDarkWakeEvaluate:
8632 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8633 if (systemDarkWake) {
8634 flags.bit.evaluateDarkWake = true;
8635 }
8636 break;
8637
8638 case kStimulusNoIdleSleepPreventers:
8639 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8640 flags.bit.adjustPowerState = true;
8641 break;
8642 } /* switch(stimulus) */
8643
8644 if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8645 DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8646 darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8647 if (darkWakeToSleepASAP ||
8648 (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8649 uint32_t newSleepReason;
8650
8651 if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8652 // System was previously in full wake. Sleep reason from
8653 // full to dark already recorded in fullToDarkReason.
8654
8655 if (lowBatteryCondition) {
8656 newSleepReason = kIOPMSleepReasonLowPower;
8657 } else if (thermalEmergencyState) {
8658 newSleepReason = kIOPMSleepReasonThermalEmergency;
8659 } else {
8660 newSleepReason = fullToDarkReason;
8661 }
8662 } else {
8663 // In dark wake from system sleep.
8664
8665 if (darkWakeSleepService) {
8666 newSleepReason = kIOPMSleepReasonSleepServiceExit;
8667 } else {
8668 newSleepReason = kIOPMSleepReasonMaintenance;
8669 }
8670 }
8671
8672 if (checkSystemCanSleep(newSleepReason)) {
8673 privateSleepSystem(newSleepReason);
8674 }
8675 } else { // non-maintenance (network) dark wake
8676 if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8677 // Release power clamp, and wait for children idle.
8678 adjustPowerState(true);
8679 } else {
8680 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8681 }
8682 }
8683 }
8684
8685 if (systemDarkWake) {
8686 // The rest are irrelevant while system is in dark wake.
8687 flags.u32 = 0;
8688 }
8689
8690 if ((flags.bit.displaySleepEntry) &&
8691 (kFullWakeReasonDisplayOn == fullWakeReason)) {
8692 // kIOPMSleepReasonNotificationWakeExit
8693 DLOG("Display sleep while in notification wake\n");
8694 changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8695 }
8696
8697 if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8698 bool cancelQuickSpindown = false;
8699
8700 if (flags.bit.sleepDelayChanged) {
8701 // Cancel existing idle sleep timer and quick disk spindown.
8702 // New settings will be applied by the idleSleepEnabled flag
8703 // handler below if idle sleep is enabled.
8704
8705 DLOG("extra sleep timer changed\n");
8706 cancelIdleSleepTimer();
8707 cancelQuickSpindown = true;
8708 } else {
8709 DLOG("user inactive\n");
8710 }
8711
8712 if (!userIsActive && idleSleepEnabled) {
8713 startIdleSleepTimer(getTimeToIdleSleep());
8714 }
8715
8716 if (cancelQuickSpindown) {
8717 restoreUserSpinDownTimeout();
8718 }
8719 }
8720
8721 if (flags.bit.idleSleepEnabled) {
8722 DLOG("idle sleep timer enabled\n");
8723 if (!wrangler) {
8724 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8725 startIdleSleepTimer(getTimeToIdleSleep());
8726 #else
8727 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8728 startIdleSleepTimer( idleMilliSeconds );
8729 #endif
8730 } else {
8731 // Start idle timer if prefs now allow system sleep
8732 // and user is already inactive. Disk spindown is
8733 // accelerated upon timer expiration.
8734
8735 if (!userIsActive) {
8736 startIdleSleepTimer(getTimeToIdleSleep());
8737 }
8738 }
8739 }
8740
8741 if (flags.bit.idleSleepDisabled) {
8742 DLOG("idle sleep timer disabled\n");
8743 cancelIdleSleepTimer();
8744 restoreUserSpinDownTimeout();
8745 adjustPowerState();
8746 }
8747
8748 if (flags.bit.adjustPowerState) {
8749 bool sleepASAP = false;
8750
8751 if (!systemBooting && (0 == idleSleepPreventersCount())) {
8752 if (!wrangler) {
8753 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
8754 if (idleSleepEnabled) {
8755 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8756 if (!extraSleepDelay && !idleSleepTimerPending) {
8757 sleepASAP = true;
8758 }
8759 #else
8760 // stay awake for at least idleMilliSeconds
8761 startIdleSleepTimer(idleMilliSeconds);
8762 #endif
8763 }
8764 } else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake) {
8765 sleepASAP = true;
8766 }
8767 }
8768
8769 adjustPowerState(sleepASAP);
8770 }
8771 }
8772
8773 //******************************************************************************
8774
8775 unsigned int
idleSleepPreventersCount()8776 IOPMrootDomain::idleSleepPreventersCount()
8777 {
8778 if (_aotMode) {
8779 unsigned int count __block;
8780 count = 0;
8781 preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
8782 {
8783 count += (NULL == obj->metaCast("AppleARMBacklight"));
8784 return false;
8785 });
8786 return count;
8787 }
8788
8789 return preventIdleSleepList->getCount();
8790 }
8791
8792
8793 //******************************************************************************
8794 // requestFullWake
8795 //
8796 // Request transition from dark wake to full wake
8797 //******************************************************************************
8798
8799 void
requestFullWake(FullWakeReason reason)8800 IOPMrootDomain::requestFullWake( FullWakeReason reason )
8801 {
8802 uint32_t options = 0;
8803 IOService * pciRoot = NULL;
8804 bool promotion = false;
8805
8806 // System must be in dark wake and a valid reason for entering full wake
8807 if ((kFullWakeReasonNone == reason) ||
8808 (kFullWakeReasonNone != fullWakeReason) ||
8809 (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8810 return;
8811 }
8812
8813 // Will clear reason upon exit from full wake
8814 fullWakeReason = reason;
8815
8816 _desiredCapability |= (kIOPMSystemCapabilityGraphics |
8817 kIOPMSystemCapabilityAudio);
8818
8819 if ((kSystemTransitionWake == _systemTransitionType) &&
8820 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
8821 !darkWakePowerClamped) {
8822 // Promote to full wake while waking up to dark wake due to tickle.
8823 // PM will hold off notifying the graphics subsystem about system wake
8824 // as late as possible, so if a HID tickle does arrive, graphics can
8825 // power up from this same wake transition. Otherwise, the latency to
8826 // power up graphics on the following transition can be huge on certain
8827 // systems. However, once any power clamping has taken effect, it is
8828 // too late to promote the current dark wake transition to a full wake.
8829 _pendingCapability |= (kIOPMSystemCapabilityGraphics |
8830 kIOPMSystemCapabilityAudio);
8831
8832 // Tell the PCI parent of audio and graphics drivers to stop
8833 // delaying the child notifications. Same for root domain.
8834 pciRoot = pciHostBridgeDriver.get();
8835 willEnterFullWake();
8836 promotion = true;
8837 }
8838
8839 // Unsafe to cancel once graphics was powered.
8840 // If system woke from dark wake, the return to sleep can
8841 // be cancelled. "awake -> dark -> sleep" transition
8842 // can be cancelled also, during the "dark -> sleep" phase
8843 // *prior* to driver power down.
8844 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
8845 _pendingCapability == 0) {
8846 options |= kIOPMSyncCancelPowerDown;
8847 }
8848
8849 synchronizePowerTree(options, pciRoot);
8850
8851 if (kFullWakeReasonLocalUser == fullWakeReason) {
8852 // IOGraphics doesn't light the display even though graphics is
8853 // enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
8854 // So, do an explicit activity tickle
8855 if (wrangler) {
8856 wrangler->activityTickle(0, 0);
8857 }
8858 }
8859
8860 // Log a timestamp for the initial full wake request.
8861 // System may not always honor this full wake request.
8862 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8863 AbsoluteTime now;
8864 uint64_t nsec;
8865
8866 clock_get_uptime(&now);
8867 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8868 absolutetime_to_nanoseconds(now, &nsec);
8869 MSG("full wake %s (reason %u) %u ms\n",
8870 promotion ? "promotion" : "request",
8871 fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
8872 }
8873 }
8874
8875 //******************************************************************************
8876 // willEnterFullWake
8877 //
8878 // System will enter full wake from sleep, from dark wake, or from dark
8879 // wake promotion. This function aggregate things that are in common to
8880 // all three full wake transitions.
8881 //
8882 // Assumptions: fullWakeReason was updated
8883 //******************************************************************************
8884
8885 void
willEnterFullWake(void)8886 IOPMrootDomain::willEnterFullWake( void )
8887 {
8888 hibernateRetry = false;
8889 sleepToStandby = false;
8890 standbyNixed = false;
8891 resetTimers = false;
8892 sleepTimerMaintenance = false;
8893
8894 assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
8895
8896 _systemMessageClientMask = kSystemMessageClientPowerd |
8897 kSystemMessageClientLegacyApp;
8898
8899 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
8900 // First time to attain full wake capability since the last wake
8901 _systemMessageClientMask |= kSystemMessageClientKernel;
8902
8903 // Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
8904 setProperty(gIOPMUserTriggeredFullWakeKey.get(),
8905 (kFullWakeReasonLocalUser == fullWakeReason) ?
8906 kOSBooleanTrue : kOSBooleanFalse);
8907 }
8908 #if HIBERNATION
8909 IOHibernateSetWakeCapabilities(_pendingCapability);
8910 #endif
8911
8912 IOService::setAdvisoryTickleEnable( true );
8913 tellClients(kIOMessageSystemWillPowerOn);
8914 preventTransitionToUserActive(false);
8915 }
8916
8917 //******************************************************************************
8918 // fullWakeDelayedWork
8919 //
8920 // System has already entered full wake. Invoked by a delayed thread call.
8921 //******************************************************************************
8922
8923 void
fullWakeDelayedWork(void)8924 IOPMrootDomain::fullWakeDelayedWork( void )
8925 {
8926 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8927 if (!gIOPMWorkLoop->inGate()) {
8928 gIOPMWorkLoop->runAction(
8929 OSMemberFunctionCast(IOWorkLoop::Action, this,
8930 &IOPMrootDomain::fullWakeDelayedWork), this);
8931 return;
8932 }
8933
8934 DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
8935 _currentCapability, _pendingCapability, _highestCapability,
8936 clamshellDisabled, clamshellSleepDisableMask);
8937
8938 if (clamshellExists &&
8939 CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
8940 !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
8941 if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
8942 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8943 } else {
8944 // Not the initial full wake after waking from sleep.
8945 // Evaluate the clamshell for rdar://problem/9157444.
8946 receivePowerNotification(kLocalEvalClamshellCommand);
8947 }
8948 }
8949 #endif
8950 }
8951
8952 //******************************************************************************
8953 // evaluateAssertions
8954 //
8955 //******************************************************************************
8956
8957 // Bitmask of all kernel assertions that prevent system idle sleep.
8958 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
8959 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
8960 (kIOPMDriverAssertionReservedBit7 | \
8961 kIOPMDriverAssertionPreventSystemIdleSleepBit)
8962
8963 void
evaluateAssertions(IOPMDriverAssertionType newAssertions,IOPMDriverAssertionType oldAssertions)8964 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
8965 {
8966 IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
8967
8968 messageClients(kIOPMMessageDriverAssertionsChanged);
8969
8970 if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
8971 if (wrangler) {
8972 bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
8973
8974 DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
8975 wrangler->setIgnoreIdleTimer( value );
8976 }
8977 }
8978
8979 if (changedBits & kIOPMDriverAssertionCPUBit) {
8980 if (_aotNow) {
8981 IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
8982 }
8983 evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
8984 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
8985 AbsoluteTime now;
8986 clock_usec_t microsecs;
8987 clock_get_uptime(&now);
8988 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8989 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
8990 if (assertOnWakeReport) {
8991 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
8992 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
8993 }
8994 }
8995 }
8996
8997 if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
8998 if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
8999 if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9000 DLOG("PreventIdleSleep driver assertion raised\n");
9001 bool ok = updatePreventIdleSleepList(this, true);
9002 if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9003 // Cancel idle sleep if there is one in progress
9004 cancelIdlePowerDown(this);
9005 }
9006 }
9007 } else {
9008 DLOG("PreventIdleSleep driver assertion dropped\n");
9009 updatePreventIdleSleepList(this, false);
9010 }
9011 }
9012 }
9013
9014 // MARK: -
9015 // MARK: Statistics
9016
9017 //******************************************************************************
9018 // pmStats
9019 //
9020 //******************************************************************************
9021
9022 void
pmStatsRecordEvent(int eventIndex,AbsoluteTime timestamp)9023 IOPMrootDomain::pmStatsRecordEvent(
9024 int eventIndex,
9025 AbsoluteTime timestamp)
9026 {
9027 bool starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9028 bool stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9029 uint64_t delta;
9030 uint64_t nsec;
9031 OSSharedPtr<OSData> publishPMStats;
9032
9033 eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9034
9035 absolutetime_to_nanoseconds(timestamp, &nsec);
9036
9037 switch (eventIndex) {
9038 case kIOPMStatsHibernateImageWrite:
9039 if (starting) {
9040 gPMStats.hibWrite.start = nsec;
9041 } else if (stopping) {
9042 gPMStats.hibWrite.stop = nsec;
9043 }
9044
9045 if (stopping) {
9046 delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9047 IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9048 }
9049 break;
9050 case kIOPMStatsHibernateImageRead:
9051 if (starting) {
9052 gPMStats.hibRead.start = nsec;
9053 } else if (stopping) {
9054 gPMStats.hibRead.stop = nsec;
9055 }
9056
9057 if (stopping) {
9058 delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9059 IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9060
9061 publishPMStats = OSData::withBytes(&gPMStats, sizeof(gPMStats));
9062 setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9063 bzero(&gPMStats, sizeof(gPMStats));
9064 }
9065 break;
9066 }
9067 }
9068
9069 /*
9070 * Appends a record of the application response to
9071 * IOPMrootDomain::pmStatsAppResponses
9072 */
9073 void
pmStatsRecordApplicationResponse(const OSSymbol * response,const char * name,int messageType,uint32_t delay_ms,uint64_t id,OSObject * object,IOPMPowerStateIndex powerState,bool async)9074 IOPMrootDomain::pmStatsRecordApplicationResponse(
9075 const OSSymbol *response,
9076 const char *name,
9077 int messageType,
9078 uint32_t delay_ms,
9079 uint64_t id,
9080 OSObject *object,
9081 IOPMPowerStateIndex powerState,
9082 bool async)
9083 {
9084 OSSharedPtr<OSDictionary> responseDescription;
9085 OSSharedPtr<OSNumber> delayNum;
9086 OSSharedPtr<OSNumber> powerCaps;
9087 OSSharedPtr<OSNumber> pidNum;
9088 OSSharedPtr<OSNumber> msgNum;
9089 OSSharedPtr<const OSSymbol> appname;
9090 OSSharedPtr<const OSSymbol> sleep;
9091 OSSharedPtr<const OSSymbol> wake;
9092 IOPMServiceInterestNotifier *notify = NULL;
9093
9094 if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9095 if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9096 notify->ackTimeoutCnt++;
9097 } else {
9098 notify->ackTimeoutCnt = 0;
9099 }
9100 }
9101
9102 if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9103 (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9104 return;
9105 }
9106
9107
9108 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9109 kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9110 } else if (notify) {
9111 // User space app or kernel capability client
9112 if (id) {
9113 kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9114 } else {
9115 kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9116 }
9117 notify->msgType = 0;
9118 }
9119
9120 responseDescription = OSDictionary::withCapacity(5);
9121 if (responseDescription) {
9122 if (response) {
9123 responseDescription->setObject(_statsResponseTypeKey.get(), response);
9124 }
9125
9126 msgNum = OSNumber::withNumber(messageType, 32);
9127 if (msgNum) {
9128 responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9129 }
9130
9131 if (!name && notify && notify->identifier) {
9132 name = notify->identifier->getCStringNoCopy();
9133 }
9134
9135 if (name && (strlen(name) > 0)) {
9136 appname = OSSymbol::withCString(name);
9137 if (appname) {
9138 responseDescription->setObject(_statsNameKey.get(), appname.get());
9139 }
9140 }
9141
9142 if (!id && notify) {
9143 id = notify->uuid0;
9144 }
9145 if (id != 0) {
9146 pidNum = OSNumber::withNumber(id, 64);
9147 if (pidNum) {
9148 responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9149 }
9150 }
9151
9152 delayNum = OSNumber::withNumber(delay_ms, 32);
9153 if (delayNum) {
9154 responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9155 }
9156
9157 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9158 powerCaps = OSNumber::withNumber(powerState, 32);
9159
9160 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9161 static const char * driverCallTypes[] = {
9162 [kDriverCallInformPreChange] = "powerStateWillChangeTo",
9163 [kDriverCallInformPostChange] = "powerStateDidChangeTo",
9164 [kDriverCallSetPowerState] = "setPowerState"
9165 };
9166
9167 if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9168 DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9169 name, id, driverCallTypes[messageType], (uint32_t) powerState,
9170 async ? "async " : "", delay_ms);
9171 }
9172 #endif
9173 } else {
9174 powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9175 }
9176 if (powerCaps) {
9177 responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9178 }
9179
9180 sleep = OSSymbol::withCString("Sleep");
9181 wake = OSSymbol::withCString("Wake");
9182 if (_systemTransitionType == kSystemTransitionSleep) {
9183 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9184 } else if (_systemTransitionType == kSystemTransitionWake) {
9185 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9186 } else if (_systemTransitionType == kSystemTransitionCapability) {
9187 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9188 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9189 } else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9190 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9191 }
9192 }
9193
9194 IOLockLock(pmStatsLock);
9195 if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9196 pmStatsAppResponses->setObject(responseDescription.get());
9197 }
9198 IOLockUnlock(pmStatsLock);
9199 }
9200
9201 return;
9202 }
9203
9204 // MARK: -
9205 // MARK: PMTraceWorker
9206
9207 //******************************************************************************
9208 // TracePoint support
9209 //
9210 //******************************************************************************
9211
9212 #define kIOPMRegisterNVRAMTracePointHandlerKey \
9213 "IOPMRegisterNVRAMTracePointHandler"
9214
9215 IOReturn
callPlatformFunction(const OSSymbol * functionName,bool waitForFunction,void * param1,void * param2,void * param3,void * param4)9216 IOPMrootDomain::callPlatformFunction(
9217 const OSSymbol * functionName,
9218 bool waitForFunction,
9219 void * param1, void * param2,
9220 void * param3, void * param4 )
9221 {
9222 if (pmTracer && functionName &&
9223 functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9224 !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9225 uint32_t tracePointPhases, tracePointPCI;
9226 uint64_t statusCode;
9227
9228 pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9229 pmTracer->tracePointTarget = (void *) param2;
9230 tracePointPCI = (uint32_t)(uintptr_t) param3;
9231 tracePointPhases = (uint32_t)(uintptr_t) param4;
9232 if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9233 OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9234 if (node) {
9235 OSSharedPtr<OSObject> bootRomFailureProp;
9236 bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9237 OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9238 uint32_t bootFailureCode;
9239 if (data && data->getLength() == sizeof(bootFailureCode)) {
9240 // Failure code from EFI/BootRom is a four byte structure
9241 memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9242 tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9243 }
9244 }
9245 }
9246 statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9247 if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9248 MSG("Sleep failure code 0x%08x 0x%08x\n",
9249 tracePointPCI, tracePointPhases);
9250 }
9251 setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9252 pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9253
9254 return kIOReturnSuccess;
9255 }
9256 #if HIBERNATION
9257 else if (functionName &&
9258 functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9259 if (gSleepPolicyHandler) {
9260 return kIOReturnExclusiveAccess;
9261 }
9262 if (!param1) {
9263 return kIOReturnBadArgument;
9264 }
9265 gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9266 gSleepPolicyTarget = (void *) param2;
9267 setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9268 return kIOReturnSuccess;
9269 }
9270 #endif
9271
9272 return super::callPlatformFunction(
9273 functionName, waitForFunction, param1, param2, param3, param4);
9274 }
9275
9276 void
kdebugTrace(uint32_t event,uint64_t id,uintptr_t param1,uintptr_t param2,uintptr_t param3)9277 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9278 uintptr_t param1, uintptr_t param2, uintptr_t param3)
9279 {
9280 uint32_t code = IODBG_POWER(event);
9281 uint64_t regId = id;
9282 if (regId == 0) {
9283 regId = getRegistryEntryID();
9284 }
9285 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9286 }
9287
9288 void
tracePoint(uint8_t point)9289 IOPMrootDomain::tracePoint( uint8_t point )
9290 {
9291 if (systemBooting) {
9292 return;
9293 }
9294
9295 if (kIOPMTracePointWakeCapabilityClients == point) {
9296 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9297 }
9298
9299 kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9300 pmTracer->tracePoint(point);
9301 }
9302
9303 static void
kext_log_putc(char c)9304 kext_log_putc(char c)
9305 {
9306 if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9307 return;
9308 }
9309 if (c == '(' || c == '[' || c == ' ') {
9310 c = 0;
9311 gKextNameEnd = true;
9312 }
9313
9314 gKextNameBuf[gKextNamePos++] = c;
9315 }
9316
9317 static int
kext_log(const char * fmt,...)9318 kext_log(const char *fmt, ...)
9319 {
9320 va_list listp;
9321
9322 va_start(listp, fmt);
9323 _doprnt(fmt, &listp, &kext_log_putc, 16);
9324 va_end(listp);
9325
9326 return 0;
9327 }
9328
9329 static OSPtr<const OSSymbol>
copyKextIdentifierWithAddress(vm_address_t address)9330 copyKextIdentifierWithAddress(vm_address_t address)
9331 {
9332 OSSharedPtr<const OSSymbol> identifer;
9333
9334 IOLockLock(gHaltLogLock);
9335
9336 gKextNameEnd = false;
9337 gKextNamePos = 0;
9338 gKextNameBuf[0] = 0;
9339
9340 OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9341 gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9342 identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9343
9344 IOLockUnlock(gHaltLogLock);
9345
9346 return identifer;
9347 }
9348
9349 // Caller serialized using PM workloop
9350 const char *
getNotificationClientName(OSObject * object)9351 IOPMrootDomain::getNotificationClientName(OSObject *object)
9352 {
9353 IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9354 const char *clientName = "UNKNOWN";
9355
9356 if (!notifier->clientName) {
9357 // Check for user client
9358 if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9359 OSNumber *clientID = NULL;
9360 messageClient(kIOMessageCopyClientID, object, &clientID);
9361 if (clientID) {
9362 OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9363 if (string) {
9364 notifier->clientName = OSSymbol::withString(string.get());
9365 }
9366 clientID->release();
9367 }
9368 } else if (notifier->identifier) {
9369 notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9370 }
9371 }
9372
9373 if (notifier->clientName) {
9374 clientName = notifier->clientName->getCStringNoCopy();
9375 }
9376
9377 return clientName;
9378 }
9379
9380 void
traceNotification(OSObject * object,bool start,uint64_t timestamp,uint32_t msgIndex)9381 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9382 {
9383 IOPMServiceInterestNotifier *notifier;
9384
9385 if (systemBooting) {
9386 return;
9387 }
9388 notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9389 if (!notifier) {
9390 return;
9391 }
9392
9393 if (start) {
9394 pmTracer->traceDetail(notifier->uuid0 >> 32);
9395 kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9396 (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9397
9398 // Update notifier state used for response/ack logging
9399 notifier->msgIndex = msgIndex;
9400 notifier->msgAbsTime = timestamp;
9401
9402 if (msgIndex != UINT_MAX) {
9403 DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9404 } else {
9405 DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9406 }
9407
9408 assert(notifierObject == NULL);
9409 notifierThread = current_thread();
9410 notifierObject.reset(notifier, OSRetain);
9411 } else {
9412 uint64_t nsec;
9413 uint32_t delayMS;
9414
9415 SUB_ABSOLUTETIME(×tamp, ¬ifier->msgAbsTime);
9416 absolutetime_to_nanoseconds(timestamp, &nsec);
9417 delayMS = (uint32_t)(nsec / 1000000ULL);
9418 if (delayMS > notifier->maxMsgDelayMS) {
9419 notifier->maxMsgDelayMS = delayMS;
9420 }
9421
9422 assert(notifierObject == notifier);
9423 notifierObject.reset();
9424 notifierThread = NULL;
9425 }
9426 }
9427
9428 void
traceNotificationAck(OSObject * object,uint32_t delay_ms)9429 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9430 {
9431 if (systemBooting) {
9432 return;
9433 }
9434 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9435 if (!notifier) {
9436 return;
9437 }
9438
9439 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9440 (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9441
9442 DLOG("%s[%u] ack from %s took %d ms\n",
9443 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9444 if (delay_ms > notifier->maxAckDelayMS) {
9445 notifier->maxAckDelayMS = delay_ms;
9446 }
9447 }
9448
9449 void
traceNotificationResponse(OSObject * object,uint32_t delay_ms,uint32_t ack_time_us)9450 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9451 {
9452 if (systemBooting) {
9453 return;
9454 }
9455 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9456 if (!notifier) {
9457 return;
9458 }
9459
9460 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9461 (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9462
9463 if (ack_time_us == 0) {
9464 // Client work is done and ack will not be forthcoming
9465 DLOG("%s[%u] response from %s took %d ms\n",
9466 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9467 } else {
9468 // Client needs more time and it must ack within ack_time_us
9469 DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9470 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9471 }
9472 }
9473
9474 void
traceFilteredNotification(OSObject * object)9475 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9476 {
9477 if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9478 return;
9479 }
9480 if (systemBooting) {
9481 return;
9482 }
9483 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9484 if (!notifier) {
9485 return;
9486 }
9487
9488 DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9489 }
9490
9491 void
traceDetail(uint32_t msgType,uint32_t msgIndex,uint32_t delay)9492 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9493 {
9494 if (!systemBooting) {
9495 uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9496 pmTracer->traceDetail( detail );
9497 kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9498 DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9499 }
9500 }
9501
9502 void
configureReportGated(uint64_t channel_id,uint64_t action,void * result)9503 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9504 {
9505 size_t reportSize;
9506 void **report = NULL;
9507 uint32_t bktCnt;
9508 uint32_t bktSize;
9509 uint32_t *clientCnt;
9510
9511 ASSERT_GATED();
9512
9513 report = NULL;
9514 if (channel_id == kAssertDelayChID) {
9515 report = &assertOnWakeReport;
9516 bktCnt = kAssertDelayBcktCnt;
9517 bktSize = kAssertDelayBcktSize;
9518 clientCnt = &assertOnWakeClientCnt;
9519 } else if (channel_id == kSleepDelaysChID) {
9520 report = &sleepDelaysReport;
9521 bktCnt = kSleepDelaysBcktCnt;
9522 bktSize = kSleepDelaysBcktSize;
9523 clientCnt = &sleepDelaysClientCnt;
9524 } else {
9525 assert(false);
9526 return;
9527 }
9528
9529 switch (action) {
9530 case kIOReportEnable:
9531
9532 if (*report) {
9533 (*clientCnt)++;
9534 break;
9535 }
9536
9537 reportSize = HISTREPORT_BUFSIZE(bktCnt);
9538 *report = IOMallocZeroData(reportSize);
9539 if (*report == NULL) {
9540 break;
9541 }
9542 HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9543 getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9544
9545 if (channel_id == kAssertDelayChID) {
9546 assertOnWakeSecs = 0;
9547 }
9548
9549 break;
9550
9551 case kIOReportDisable:
9552 if (*clientCnt == 0) {
9553 break;
9554 }
9555 if (*clientCnt == 1) {
9556 IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9557 *report = NULL;
9558 }
9559 (*clientCnt)--;
9560
9561 if (channel_id == kAssertDelayChID) {
9562 assertOnWakeSecs = -1; // Invalid value to prevent updates
9563 }
9564 break;
9565
9566 case kIOReportGetDimensions:
9567 if (*report) {
9568 HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9569 }
9570 break;
9571 }
9572
9573 return;
9574 }
9575
9576 IOReturn
configureReport(IOReportChannelList * channelList,IOReportConfigureAction action,void * result,void * destination)9577 IOPMrootDomain::configureReport(IOReportChannelList *channelList,
9578 IOReportConfigureAction action,
9579 void *result,
9580 void *destination)
9581 {
9582 unsigned cnt;
9583 uint64_t configAction = (uint64_t)action;
9584
9585 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9586 if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9587 (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9588 (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9589 if (action != kIOReportGetDimensions) {
9590 continue;
9591 }
9592 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9593 } else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9594 (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9595 gIOPMWorkLoop->runAction(
9596 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9597 (OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9598 (void *)configAction, (void *)result);
9599 }
9600 }
9601
9602 return super::configureReport(channelList, action, result, destination);
9603 }
9604
9605 IOReturn
updateReportGated(uint64_t ch_id,void * result,IOBufferMemoryDescriptor * dest)9606 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9607 {
9608 uint32_t size2cpy;
9609 void *data2cpy;
9610 void **report;
9611
9612 ASSERT_GATED();
9613
9614 report = NULL;
9615 if (ch_id == kAssertDelayChID) {
9616 report = &assertOnWakeReport;
9617 } else if (ch_id == kSleepDelaysChID) {
9618 report = &sleepDelaysReport;
9619 } else {
9620 assert(false);
9621 return kIOReturnBadArgument;
9622 }
9623
9624 if (*report == NULL) {
9625 return kIOReturnNotOpen;
9626 }
9627
9628 HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9629 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9630 return kIOReturnOverrun;
9631 }
9632
9633 HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9634 dest->appendBytes(data2cpy, size2cpy);
9635
9636 return kIOReturnSuccess;
9637 }
9638
9639 IOReturn
updateReport(IOReportChannelList * channelList,IOReportUpdateAction action,void * result,void * destination)9640 IOPMrootDomain::updateReport(IOReportChannelList *channelList,
9641 IOReportUpdateAction action,
9642 void *result,
9643 void *destination)
9644 {
9645 uint32_t size2cpy;
9646 void *data2cpy;
9647 uint8_t buf[SIMPLEREPORT_BUFSIZE];
9648 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9649 unsigned cnt;
9650 uint64_t ch_id;
9651
9652 if (action != kIOReportCopyChannelData) {
9653 goto exit;
9654 }
9655
9656 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9657 ch_id = channelList->channels[cnt].channel_id;
9658
9659 if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9660 gIOPMWorkLoop->runAction(
9661 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9662 (OSObject *)this, (void *)ch_id,
9663 (void *)result, (void *)dest);
9664 continue;
9665 } else if ((ch_id == kSleepCntChID) ||
9666 (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9667 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9668 } else {
9669 continue;
9670 }
9671
9672 if (ch_id == kSleepCntChID) {
9673 SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9674 } else if (ch_id == kDarkWkCntChID) {
9675 SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9676 } else if (ch_id == kUserWkCntChID) {
9677 SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9678 }
9679
9680 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9681 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9682 dest->appendBytes(data2cpy, size2cpy);
9683 }
9684
9685 exit:
9686 return super::updateReport(channelList, action, result, destination);
9687 }
9688
9689
9690 //******************************************************************************
9691 // PMTraceWorker Class
9692 //
9693 //******************************************************************************
9694
9695 #undef super
9696 #define super OSObject
OSDefineMetaClassAndStructors(PMTraceWorker,OSObject)9697 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9698
9699 #define kPMBestGuessPCIDevicesCount 25
9700 #define kPMMaxRTCBitfieldSize 32
9701
9702 OSPtr<PMTraceWorker>
9703 PMTraceWorker::tracer(IOPMrootDomain * owner)
9704 {
9705 OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9706 if (!me || !me->init()) {
9707 return NULL;
9708 }
9709
9710 DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9711
9712 // Note that we cannot instantiate the PCI device -> bit mappings here, since
9713 // the IODeviceTree has not yet been created by IOPlatformExpert. We create
9714 // this dictionary lazily.
9715 me->owner = owner;
9716 me->pciDeviceBitMappings = NULL;
9717 me->pmTraceWorkerLock = IOLockAlloc();
9718 me->tracePhase = kIOPMTracePointSystemUp;
9719 me->traceData32 = 0;
9720 me->loginWindowData = 0;
9721 me->coreDisplayData = 0;
9722 me->coreGraphicsData = 0;
9723 return me;
9724 }
9725
9726 void
RTC_TRACE(void)9727 PMTraceWorker::RTC_TRACE(void)
9728 {
9729 if (tracePointHandler && tracePointTarget) {
9730 uint32_t wordA;
9731
9732 IOLockLock(pmTraceWorkerLock);
9733 wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9734 (coreGraphicsData << 8) | tracePhase;
9735 IOLockUnlock(pmTraceWorkerLock);
9736
9737 tracePointHandler( tracePointTarget, traceData32, wordA );
9738 _LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9739 }
9740 #if DEVELOPMENT || DEBUG
9741 if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9742 DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9743 IOLock *l = IOLockAlloc();
9744 IOLockLock(l);
9745 IOLockLock(l);
9746 }
9747 #endif
9748 }
9749
9750 int
recordTopLevelPCIDevice(IOService * pciDevice)9751 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
9752 {
9753 OSSharedPtr<const OSSymbol> deviceName;
9754 int index = -1;
9755
9756 IOLockLock(pmTraceWorkerLock);
9757
9758 if (!pciDeviceBitMappings) {
9759 pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
9760 if (!pciDeviceBitMappings) {
9761 goto exit;
9762 }
9763 }
9764
9765 // Check for bitmask overflow.
9766 if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
9767 goto exit;
9768 }
9769
9770 if ((deviceName = pciDevice->copyName()) &&
9771 (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
9772 pciDeviceBitMappings->setObject(deviceName.get())) {
9773 index = pciDeviceBitMappings->getCount() - 1;
9774 _LOG("PMTrace PCI array: set object %s => %d\n",
9775 deviceName->getCStringNoCopy(), index);
9776 }
9777
9778 if (!addedToRegistry && (index >= 0)) {
9779 addedToRegistry = owner->setProperty("PCITopLevel", this);
9780 }
9781
9782 exit:
9783 IOLockUnlock(pmTraceWorkerLock);
9784 return index;
9785 }
9786
9787 bool
serialize(OSSerialize * s) const9788 PMTraceWorker::serialize(OSSerialize *s) const
9789 {
9790 bool ok = false;
9791 if (pciDeviceBitMappings) {
9792 IOLockLock(pmTraceWorkerLock);
9793 ok = pciDeviceBitMappings->serialize(s);
9794 IOLockUnlock(pmTraceWorkerLock);
9795 }
9796 return ok;
9797 }
9798
9799 void
tracePoint(uint8_t phase)9800 PMTraceWorker::tracePoint(uint8_t phase)
9801 {
9802 // clear trace detail when phase begins
9803 if (tracePhase != phase) {
9804 traceData32 = 0;
9805 }
9806
9807 tracePhase = phase;
9808
9809 DLOG("trace point 0x%02x\n", tracePhase);
9810 RTC_TRACE();
9811 }
9812
9813 void
traceDetail(uint32_t detail)9814 PMTraceWorker::traceDetail(uint32_t detail)
9815 {
9816 if (detail == traceData32) {
9817 return;
9818 }
9819 traceData32 = detail;
9820 RTC_TRACE();
9821 }
9822
9823 void
traceComponentWakeProgress(uint32_t component,uint32_t data)9824 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
9825 {
9826 switch (component) {
9827 case kIOPMLoginWindowProgress:
9828 loginWindowData = data & kIOPMLoginWindowProgressMask;
9829 break;
9830 case kIOPMCoreDisplayProgress:
9831 coreDisplayData = data & kIOPMCoreDisplayProgressMask;
9832 break;
9833 case kIOPMCoreGraphicsProgress:
9834 coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
9835 break;
9836 default:
9837 return;
9838 }
9839
9840 DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
9841 RTC_TRACE();
9842 }
9843
9844 void
tracePCIPowerChange(change_t type,IOService * service,uint32_t changeFlags,uint32_t bitNum)9845 PMTraceWorker::tracePCIPowerChange(
9846 change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
9847 {
9848 uint32_t bitMask;
9849 uint32_t expectedFlag;
9850
9851 // Ignore PCI changes outside of system sleep/wake.
9852 if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
9853 (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
9854 return;
9855 }
9856
9857 // Only record the WillChange transition when going to sleep,
9858 // and the DidChange on the way up.
9859 changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
9860 expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
9861 kIOPMDomainWillChange : kIOPMDomainDidChange;
9862 if (changeFlags != expectedFlag) {
9863 return;
9864 }
9865
9866 // Mark this device off in our bitfield
9867 if (bitNum < kPMMaxRTCBitfieldSize) {
9868 bitMask = (1 << bitNum);
9869
9870 if (kPowerChangeStart == type) {
9871 traceData32 |= bitMask;
9872 _LOG("PMTrace: Device %s started - bit %2d mask 0x%08x => 0x%08x\n",
9873 service->getName(), bitNum, bitMask, traceData32);
9874 owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
9875 } else {
9876 traceData32 &= ~bitMask;
9877 _LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
9878 service->getName(), bitNum, bitMask, traceData32);
9879 owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
9880 }
9881
9882 DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
9883 RTC_TRACE();
9884 }
9885 }
9886
9887 uint64_t
getPMStatusCode()9888 PMTraceWorker::getPMStatusCode()
9889 {
9890 return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
9891 }
9892
9893 uint8_t
getTracePhase()9894 PMTraceWorker::getTracePhase()
9895 {
9896 return tracePhase;
9897 }
9898
9899 uint32_t
getTraceData()9900 PMTraceWorker::getTraceData()
9901 {
9902 return traceData32;
9903 }
9904
9905 // MARK: -
9906 // MARK: PMHaltWorker
9907
9908 //******************************************************************************
9909 // PMHaltWorker Class
9910 //
9911 //******************************************************************************
9912
9913 PMHaltWorker *
worker(void)9914 PMHaltWorker::worker( void )
9915 {
9916 PMHaltWorker * me;
9917 IOThread thread;
9918
9919 do {
9920 me = OSTypeAlloc( PMHaltWorker );
9921 if (!me || !me->init()) {
9922 break;
9923 }
9924
9925 me->lock = IOLockAlloc();
9926 if (!me->lock) {
9927 break;
9928 }
9929
9930 DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
9931 me->retain(); // thread holds extra retain
9932 if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
9933 me->release();
9934 break;
9935 }
9936 thread_deallocate(thread);
9937 return me;
9938 } while (false);
9939
9940 if (me) {
9941 me->release();
9942 }
9943 return NULL;
9944 }
9945
9946 void
free(void)9947 PMHaltWorker::free( void )
9948 {
9949 DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
9950 if (lock) {
9951 IOLockFree(lock);
9952 lock = NULL;
9953 }
9954 return OSObject::free();
9955 }
9956
9957 void
main(void * arg,wait_result_t waitResult)9958 PMHaltWorker::main( void * arg, wait_result_t waitResult )
9959 {
9960 PMHaltWorker * me = (PMHaltWorker *) arg;
9961
9962 IOLockLock( gPMHaltLock );
9963 gPMHaltBusyCount++;
9964 me->depth = gPMHaltDepth;
9965 IOLockUnlock( gPMHaltLock );
9966
9967 while (me->depth >= 0) {
9968 PMHaltWorker::work( me );
9969
9970 IOLockLock( gPMHaltLock );
9971 if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
9972 // This is the last thread to finish work on this level,
9973 // inform everyone to start working on next lower level.
9974 gPMHaltDepth--;
9975 me->depth = gPMHaltDepth;
9976 gPMHaltIdleCount = 0;
9977 thread_wakeup((event_t) &gPMHaltIdleCount);
9978 } else {
9979 // One or more threads are still working on this level,
9980 // this thread must wait.
9981 me->depth = gPMHaltDepth - 1;
9982 do {
9983 IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
9984 } while (me->depth != gPMHaltDepth);
9985 }
9986 IOLockUnlock( gPMHaltLock );
9987 }
9988
9989 // No more work to do, terminate thread
9990 DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
9991 thread_wakeup( &gPMHaltDepth );
9992 me->release();
9993 }
9994
9995 void
work(PMHaltWorker * me)9996 PMHaltWorker::work( PMHaltWorker * me )
9997 {
9998 OSSharedPtr<IOService> service;
9999 OSSet * inner;
10000 AbsoluteTime startTime, elapsedTime;
10001 UInt32 deltaTime;
10002 bool timeout;
10003
10004 while (true) {
10005 timeout = false;
10006
10007 // Claim an unit of work from the shared pool
10008 IOLockLock( gPMHaltLock );
10009 inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10010 if (inner) {
10011 service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10012 if (service) {
10013 inner->removeObject(service.get());
10014 }
10015 }
10016 IOLockUnlock( gPMHaltLock );
10017 if (!service) {
10018 break; // no more work at this depth
10019 }
10020 clock_get_uptime(&startTime);
10021
10022 if (!service->isInactive() &&
10023 service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10024 IOLockLock(me->lock);
10025 me->startTime = startTime;
10026 me->service = service.get();
10027 me->timeout = false;
10028 IOLockUnlock(me->lock);
10029
10030 service->systemWillShutdown( gPMHaltMessageType);
10031
10032 // Wait for driver acknowledgement
10033 IOLockLock(me->lock);
10034 while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10035 IOLockSleep(me->lock, me, THREAD_UNINT);
10036 }
10037 me->service = NULL;
10038 timeout = me->timeout;
10039 IOLockUnlock(me->lock);
10040 }
10041
10042 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10043 if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10044 LOG("%s driver %s (0x%llx) took %u ms\n",
10045 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10046 "PowerOff" : "Restart",
10047 service->getName(), service->getRegistryEntryID(),
10048 (uint32_t) deltaTime );
10049 halt_log_enter("PowerOff/Restart handler completed",
10050 OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10051 elapsedTime);
10052 }
10053
10054 me->visits++;
10055 }
10056 }
10057
10058 void
checkTimeout(PMHaltWorker * me,AbsoluteTime * now)10059 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10060 {
10061 UInt64 nano;
10062 AbsoluteTime startTime;
10063 AbsoluteTime endTime;
10064
10065 endTime = *now;
10066
10067 IOLockLock(me->lock);
10068 if (me->service && !me->timeout) {
10069 startTime = me->startTime;
10070 nano = 0;
10071 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10072 SUB_ABSOLUTETIME(&endTime, &startTime);
10073 absolutetime_to_nanoseconds(endTime, &nano);
10074 }
10075 if (nano > 3000000000ULL) {
10076 me->timeout = true;
10077
10078 halt_log_enter("PowerOff/Restart still waiting on handler",
10079 OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10080 endTime);
10081 MSG("%s still waiting on %s\n",
10082 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ? "PowerOff" : "Restart",
10083 me->service->getName());
10084 }
10085 }
10086 IOLockUnlock(me->lock);
10087 }
10088
10089 //******************************************************************************
10090 // acknowledgeSystemWillShutdown
10091 //
10092 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10093 //******************************************************************************
10094
10095 void
acknowledgeSystemWillShutdown(IOService * from)10096 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10097 {
10098 PMHaltWorker * worker;
10099 OSSharedPtr<OSObject> prop;
10100
10101 if (!from) {
10102 return;
10103 }
10104
10105 //DLOG("%s acknowledged\n", from->getName());
10106 prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10107 if (prop) {
10108 worker = (PMHaltWorker *) prop.get();
10109 IOLockLock(worker->lock);
10110 from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10111 thread_wakeup((event_t) worker);
10112 IOLockUnlock(worker->lock);
10113 } else {
10114 DLOG("%s acknowledged without worker property\n",
10115 from->getName());
10116 }
10117 }
10118
10119
10120 //******************************************************************************
10121 // notifySystemShutdown
10122 //
10123 // Notify all objects in PM tree that system will shutdown or restart
10124 //******************************************************************************
10125
10126 static void
notifySystemShutdown(IOService * root,uint32_t messageType)10127 notifySystemShutdown( IOService * root, uint32_t messageType )
10128 {
10129 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10130 OSSharedPtr<IORegistryIterator> iter;
10131 IORegistryEntry * entry;
10132 IOService * node;
10133 OSSet * inner;
10134 OSSharedPtr<OSSet> newInner;
10135 PMHaltWorker * workers[kPMHaltMaxWorkers];
10136 AbsoluteTime deadline;
10137 unsigned int totalNodes = 0;
10138 unsigned int depth;
10139 unsigned int rootDepth;
10140 unsigned int numWorkers;
10141 unsigned int count;
10142 int waitResult;
10143 void * baseFunc;
10144 bool ok;
10145
10146 DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10147
10148 baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10149
10150 // Iterate the entire PM tree starting from root
10151
10152 rootDepth = root->getDepth( gIOPowerPlane );
10153 if (!rootDepth) {
10154 goto done;
10155 }
10156
10157 // debug - for repeated test runs
10158 while (PMHaltWorker::metaClass->getInstanceCount()) {
10159 IOSleep(1);
10160 }
10161
10162 if (!gPMHaltArray) {
10163 gPMHaltArray = OSArray::withCapacity(40);
10164 if (!gPMHaltArray) {
10165 goto done;
10166 }
10167 } else { // debug
10168 gPMHaltArray->flushCollection();
10169 }
10170
10171 if (!gPMHaltLock) {
10172 gPMHaltLock = IOLockAlloc();
10173 if (!gPMHaltLock) {
10174 goto done;
10175 }
10176 }
10177
10178 if (!gPMHaltClientAcknowledgeKey) {
10179 gPMHaltClientAcknowledgeKey =
10180 OSSymbol::withCStringNoCopy("PMShutdown");
10181 if (!gPMHaltClientAcknowledgeKey) {
10182 goto done;
10183 }
10184 }
10185
10186 gPMHaltMessageType = messageType;
10187
10188 // Depth-first walk of PM plane
10189
10190 iter = IORegistryIterator::iterateOver(
10191 root, gIOPowerPlane, kIORegistryIterateRecursively);
10192
10193 if (iter) {
10194 while ((entry = iter->getNextObject())) {
10195 node = OSDynamicCast(IOService, entry);
10196 if (!node) {
10197 continue;
10198 }
10199
10200 if (baseFunc ==
10201 OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10202 continue;
10203 }
10204
10205 depth = node->getDepth( gIOPowerPlane );
10206 if (depth <= rootDepth) {
10207 continue;
10208 }
10209
10210 ok = false;
10211
10212 // adjust to zero based depth
10213 depth -= (rootDepth + 1);
10214
10215 // gPMHaltArray is an array of containers, each container
10216 // refers to nodes with the same depth.
10217
10218 count = gPMHaltArray->getCount();
10219 while (depth >= count) {
10220 // expand array and insert placeholders
10221 gPMHaltArray->setObject(PLACEHOLDER);
10222 count++;
10223 }
10224 count = gPMHaltArray->getCount();
10225 if (depth < count) {
10226 inner = (OSSet *)gPMHaltArray->getObject(depth);
10227 if (inner == PLACEHOLDER) {
10228 newInner = OSSet::withCapacity(40);
10229 if (newInner) {
10230 gPMHaltArray->replaceObject(depth, newInner.get());
10231 inner = newInner.get();
10232 }
10233 }
10234
10235 // PM nodes that appear more than once in the tree will have
10236 // the same depth, OSSet will refuse to add the node twice.
10237 if (inner) {
10238 ok = inner->setObject(node);
10239 }
10240 }
10241 if (!ok) {
10242 DLOG("Skipped PM node %s\n", node->getName());
10243 }
10244 }
10245 }
10246
10247 // debug only
10248 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10249 count = 0;
10250 if (inner != PLACEHOLDER) {
10251 count = inner->getCount();
10252 }
10253 DLOG("Nodes at depth %u = %u\n", i, count);
10254 }
10255
10256 // strip placeholders (not all depths are populated)
10257 numWorkers = 0;
10258 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10259 if (inner == PLACEHOLDER) {
10260 gPMHaltArray->removeObject(i);
10261 continue;
10262 }
10263 count = inner->getCount();
10264 if (count > numWorkers) {
10265 numWorkers = count;
10266 }
10267 totalNodes += count;
10268 i++;
10269 }
10270
10271 if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10272 goto done;
10273 }
10274
10275 gPMHaltBusyCount = 0;
10276 gPMHaltIdleCount = 0;
10277 gPMHaltDepth = gPMHaltArray->getCount() - 1;
10278
10279 // Create multiple workers (and threads)
10280
10281 if (numWorkers > kPMHaltMaxWorkers) {
10282 numWorkers = kPMHaltMaxWorkers;
10283 }
10284
10285 DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10286 totalNodes, gPMHaltArray->getCount(), numWorkers);
10287
10288 for (unsigned int i = 0; i < numWorkers; i++) {
10289 workers[i] = PMHaltWorker::worker();
10290 }
10291
10292 // Wait for workers to exhaust all available work
10293
10294 IOLockLock(gPMHaltLock);
10295 while (gPMHaltDepth >= 0) {
10296 clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10297
10298 waitResult = IOLockSleepDeadline(
10299 gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10300 if (THREAD_TIMED_OUT == waitResult) {
10301 AbsoluteTime now;
10302 clock_get_uptime(&now);
10303
10304 IOLockUnlock(gPMHaltLock);
10305 for (unsigned int i = 0; i < numWorkers; i++) {
10306 if (workers[i]) {
10307 PMHaltWorker::checkTimeout(workers[i], &now);
10308 }
10309 }
10310 IOLockLock(gPMHaltLock);
10311 }
10312 }
10313 IOLockUnlock(gPMHaltLock);
10314
10315 // Release all workers
10316
10317 for (unsigned int i = 0; i < numWorkers; i++) {
10318 if (workers[i]) {
10319 workers[i]->release();
10320 }
10321 // worker also retained by it's own thread
10322 }
10323
10324 done:
10325 DLOG("%s done\n", __FUNCTION__);
10326 return;
10327 }
10328
10329 // MARK: -
10330 // MARK: Kernel Assertion
10331
10332 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10333
10334 IOPMDriverAssertionID
createPMAssertion(IOPMDriverAssertionType whichAssertionBits,IOPMDriverAssertionLevel assertionLevel,IOService * ownerService,const char * ownerDescription)10335 IOPMrootDomain::createPMAssertion(
10336 IOPMDriverAssertionType whichAssertionBits,
10337 IOPMDriverAssertionLevel assertionLevel,
10338 IOService *ownerService,
10339 const char *ownerDescription)
10340 {
10341 IOReturn ret;
10342 IOPMDriverAssertionID newAssertion;
10343
10344 if (!pmAssertions) {
10345 return 0;
10346 }
10347
10348 ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10349
10350 if (kIOReturnSuccess == ret) {
10351 return newAssertion;
10352 } else {
10353 return 0;
10354 }
10355 }
10356
10357 IOReturn
releasePMAssertion(IOPMDriverAssertionID releaseAssertion)10358 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10359 {
10360 if (!pmAssertions) {
10361 return kIOReturnInternalError;
10362 }
10363
10364 return pmAssertions->releaseAssertion(releaseAssertion);
10365 }
10366
10367
10368 IOReturn
setPMAssertionLevel(IOPMDriverAssertionID assertionID,IOPMDriverAssertionLevel assertionLevel)10369 IOPMrootDomain::setPMAssertionLevel(
10370 IOPMDriverAssertionID assertionID,
10371 IOPMDriverAssertionLevel assertionLevel)
10372 {
10373 return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10374 }
10375
10376 IOPMDriverAssertionLevel
getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)10377 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10378 {
10379 IOPMDriverAssertionType sysLevels;
10380
10381 if (!pmAssertions || whichAssertion == 0) {
10382 return kIOPMDriverAssertionLevelOff;
10383 }
10384
10385 sysLevels = pmAssertions->getActivatedAssertions();
10386
10387 // Check that every bit set in argument 'whichAssertion' is asserted
10388 // in the aggregate bits.
10389 if ((sysLevels & whichAssertion) == whichAssertion) {
10390 return kIOPMDriverAssertionLevelOn;
10391 } else {
10392 return kIOPMDriverAssertionLevelOff;
10393 }
10394 }
10395
10396 IOReturn
setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)10397 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10398 {
10399 if (!pmAssertions) {
10400 return kIOReturnNotFound;
10401 }
10402
10403 return pmAssertions->setUserAssertionLevels(inLevels);
10404 }
10405
10406 bool
serializeProperties(OSSerialize * s) const10407 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10408 {
10409 if (pmAssertions) {
10410 pmAssertions->publishProperties();
10411 }
10412 return IOService::serializeProperties(s);
10413 }
10414
10415 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const10416 IOPMrootDomain::copyProperty( const char * aKey) const
10417 {
10418 OSSharedPtr<OSObject> obj;
10419 obj = IOService::copyProperty(aKey);
10420
10421 if (obj) {
10422 return obj;
10423 }
10424
10425 if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10426 sizeof(kIOPMSleepWakeWdogRebootKey))) {
10427 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10428 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10429 } else {
10430 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10431 }
10432 }
10433
10434 if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10435 sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10436 if (swd_flags & SWD_VALID_LOGS) {
10437 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10438 } else {
10439 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10440 }
10441 }
10442
10443 /*
10444 * XXX: We should get rid of "DesktopMode" property when 'kAppleClamshellCausesSleepKey'
10445 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10446 * issued by DisplayWrangler on darkwake.
10447 */
10448 if (!strcmp(aKey, "DesktopMode")) {
10449 if (desktopMode) {
10450 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10451 } else {
10452 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10453 }
10454 }
10455 if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10456 if (displayIdleForDemandSleep) {
10457 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10458 } else {
10459 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10460 }
10461 }
10462
10463 if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10464 OSSharedPtr<OSArray> array;
10465 WAKEEVENT_LOCK();
10466 if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10467 OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10468 if (collection) {
10469 array = OSDynamicPtrCast<OSArray>(collection);
10470 }
10471 }
10472 WAKEEVENT_UNLOCK();
10473 return os::move(array);
10474 }
10475
10476 if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10477 OSSharedPtr<OSArray> array;
10478 IOLockLock(pmStatsLock);
10479 if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10480 OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10481 if (collection) {
10482 array = OSDynamicPtrCast<OSArray>(collection);
10483 }
10484 }
10485 IOLockUnlock(pmStatsLock);
10486 return os::move(array);
10487 }
10488
10489 if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10490 OSArray *idleSleepList = NULL;
10491 gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10492 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10493 }
10494
10495 if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10496 OSArray *systemSleepList = NULL;
10497 gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10498 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10499 }
10500
10501 if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10502 OSArray *idleSleepList = NULL;
10503 gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10504 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10505 }
10506
10507 if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10508 OSArray *systemSleepList = NULL;
10509 gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10510 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10511 }
10512 return NULL;
10513 }
10514
10515 // MARK: -
10516 // MARK: Wake Event Reporting
10517
10518 void
copyWakeReasonString(char * outBuf,size_t bufSize)10519 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10520 {
10521 WAKEEVENT_LOCK();
10522 strlcpy(outBuf, gWakeReasonString, bufSize);
10523 WAKEEVENT_UNLOCK();
10524 }
10525
10526 void
copyShutdownReasonString(char * outBuf,size_t bufSize)10527 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10528 {
10529 WAKEEVENT_LOCK();
10530 strlcpy(outBuf, gShutdownReasonString, bufSize);
10531 WAKEEVENT_UNLOCK();
10532 }
10533
10534 //******************************************************************************
10535 // acceptSystemWakeEvents
10536 //
10537 // Private control for the acceptance of driver wake event claims.
10538 //******************************************************************************
10539
10540 void
acceptSystemWakeEvents(uint32_t control)10541 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10542 {
10543 bool logWakeReason = false;
10544
10545 WAKEEVENT_LOCK();
10546 switch (control) {
10547 case kAcceptSystemWakeEvents_Enable:
10548 assert(_acceptSystemWakeEvents == false);
10549 if (!_systemWakeEventsArray) {
10550 _systemWakeEventsArray = OSArray::withCapacity(4);
10551 }
10552 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10553 if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10554 gWakeReasonString[0] = '\0';
10555 if (_systemWakeEventsArray) {
10556 _systemWakeEventsArray->flushCollection();
10557 }
10558 }
10559
10560 // Remove stale WakeType property before system sleep
10561 removeProperty(kIOPMRootDomainWakeTypeKey);
10562 removeProperty(kIOPMRootDomainWakeReasonKey);
10563 break;
10564
10565 case kAcceptSystemWakeEvents_Disable:
10566 _acceptSystemWakeEvents = false;
10567 #if defined(XNU_TARGET_OS_OSX)
10568 logWakeReason = (gWakeReasonString[0] != '\0');
10569 #else /* !defined(XNU_TARGET_OS_OSX) */
10570 logWakeReason = gWakeReasonSysctlRegistered;
10571 #if DEVELOPMENT
10572 static int panic_allowed = -1;
10573
10574 if ((panic_allowed == -1) &&
10575 (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10576 panic_allowed = 0;
10577 }
10578
10579 if (panic_allowed) {
10580 size_t i = 0;
10581 // Panic if wake reason is null or empty
10582 for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10583 if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10584 break;
10585 }
10586 }
10587 if (i >= strlen(gWakeReasonString)) {
10588 panic("Wake reason is empty");
10589 }
10590 }
10591 #endif /* DEVELOPMENT */
10592 #endif /* !defined(XNU_TARGET_OS_OSX) */
10593
10594 // publish kIOPMRootDomainWakeReasonKey if not already set
10595 if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10596 setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10597 }
10598 break;
10599
10600 case kAcceptSystemWakeEvents_Reenable:
10601 assert(_acceptSystemWakeEvents == false);
10602 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10603 removeProperty(kIOPMRootDomainWakeReasonKey);
10604 break;
10605 }
10606 WAKEEVENT_UNLOCK();
10607
10608 if (logWakeReason) {
10609 MSG("system wake events: %s\n", gWakeReasonString);
10610 }
10611 }
10612
10613 //******************************************************************************
10614 // claimSystemWakeEvent
10615 //
10616 // For a driver to claim a device is the source/conduit of a system wake event.
10617 //******************************************************************************
10618
10619 void
claimSystemWakeEvent(IOService * device,IOOptionBits flags,const char * reason,OSObject * details)10620 IOPMrootDomain::claimSystemWakeEvent(
10621 IOService * device,
10622 IOOptionBits flags,
10623 const char * reason,
10624 OSObject * details )
10625 {
10626 OSSharedPtr<const OSSymbol> deviceName;
10627 OSSharedPtr<OSNumber> deviceRegId;
10628 OSSharedPtr<OSNumber> claimTime;
10629 OSSharedPtr<OSData> flagsData;
10630 OSSharedPtr<OSString> reasonString;
10631 OSSharedPtr<OSDictionary> dict;
10632 uint64_t timestamp;
10633 bool addWakeReason;
10634
10635 if (!device || !reason) {
10636 return;
10637 }
10638
10639 pmEventTimeStamp(×tamp);
10640
10641 IOOptionBits aotFlags = 0;
10642 bool needAOTEvaluate = FALSE;
10643
10644 if (kIOPMAOTModeAddEventFlags & _aotMode) {
10645 if (!strcmp("hold", reason)
10646 || !strcmp("help", reason)
10647 || !strcmp("menu", reason)
10648 || !strcmp("stockholm", reason)
10649 || !strcmp("ringer", reason)
10650 || !strcmp("ringerab", reason)
10651 || !strcmp("smc0", reason)
10652 || !strcmp("AOP.RTPWakeupAP", reason)
10653 || !strcmp("AOP.RTP_AP_IRQ", reason)
10654 || !strcmp("BT.OutboxNotEmpty", reason)
10655 || !strcmp("WL.OutboxNotEmpty", reason)) {
10656 flags |= kIOPMWakeEventAOTExit;
10657 }
10658 }
10659
10660 #if DEVELOPMENT || DEBUG
10661 if (_aotLingerTime && !strcmp("rtc", reason)) {
10662 flags |= kIOPMWakeEventAOTPossibleExit;
10663 }
10664 #endif /* DEVELOPMENT || DEBUG */
10665
10666 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10667 // Publishing the WakeType is serialized by the PM work loop
10668 if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10669 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10670 (void *) _nextScheduledAlarmType.get());
10671 }
10672
10673 // Workaround for the missing wake HID event
10674 if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10675 if (!strcmp("trackpadkeyboard", reason)) {
10676 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10677 (void *) gIOPMWakeTypeUserKey.get());
10678 }
10679 }
10680 #endif
10681
10682 deviceName = device->copyName(gIOServicePlane);
10683 deviceRegId = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10684 claimTime = OSNumber::withNumber(timestamp, 64);
10685 flagsData = OSData::withBytes(&flags, sizeof(flags));
10686 reasonString = OSString::withCString(reason);
10687 dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
10688 if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
10689 goto done;
10690 }
10691
10692 dict->setObject(gIONameKey, deviceName.get());
10693 dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
10694 dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
10695 dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
10696 dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
10697 if (details) {
10698 dict->setObject(kIOPMWakeEventDetailsKey, details);
10699 }
10700
10701 WAKEEVENT_LOCK();
10702 addWakeReason = _acceptSystemWakeEvents;
10703 if (_aotMode) {
10704 IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
10705 }
10706 aotFlags = (kIOPMWakeEventAOTFlags & flags);
10707 aotFlags = (aotFlags & ~_aotPendingFlags);
10708 needAOTEvaluate = false;
10709 if (_aotNow && aotFlags) {
10710 if (kIOPMWakeEventAOTPossibleExit & flags) {
10711 _aotMetrics->possibleCount++;
10712 }
10713 if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
10714 _aotMetrics->confirmedPossibleCount++;
10715 }
10716 if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
10717 _aotMetrics->rejectedPossibleCount++;
10718 }
10719 if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
10720 _aotMetrics->expiredPossibleCount++;
10721 }
10722
10723 _aotPendingFlags |= aotFlags;
10724 addWakeReason = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
10725 needAOTEvaluate = _aotReadyToFullWake;
10726 }
10727 DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
10728 reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
10729 _aotNow, pmTracer->getTracePhase(), addWakeReason);
10730
10731 if (!gWakeReasonSysctlRegistered) {
10732 // Lazy registration until the platform driver stops registering
10733 // the same name.
10734 gWakeReasonSysctlRegistered = true;
10735 }
10736 if (addWakeReason) {
10737 _systemWakeEventsArray->setObject(dict.get());
10738 if (gWakeReasonString[0] != '\0') {
10739 strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
10740 }
10741 strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
10742 }
10743
10744 WAKEEVENT_UNLOCK();
10745 if (needAOTEvaluate) {
10746 // Call aotEvaluate() on PM work loop since it may call
10747 // aotExit() which accesses PM state.
10748 pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
10749 }
10750
10751 done:
10752 return;
10753 }
10754
10755 //******************************************************************************
10756 // claimSystemBootEvent
10757 //
10758 // For a driver to claim a device is the source/conduit of a system boot event.
10759 //******************************************************************************
10760
10761 void
claimSystemBootEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10762 IOPMrootDomain::claimSystemBootEvent(
10763 IOService * device,
10764 IOOptionBits flags,
10765 const char * reason,
10766 __unused OSObject * details )
10767 {
10768 if (!device || !reason) {
10769 return;
10770 }
10771
10772 DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10773 WAKEEVENT_LOCK();
10774 if (!gBootReasonSysctlRegistered) {
10775 // Lazy sysctl registration after setting gBootReasonString
10776 strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
10777 os_atomic_store(&gBootReasonSysctlRegistered, true, release);
10778 }
10779 WAKEEVENT_UNLOCK();
10780 }
10781
10782 //******************************************************************************
10783 // claimSystemShutdownEvent
10784 //
10785 // For drivers to claim a system shutdown event on the ensuing boot.
10786 //******************************************************************************
10787
10788 void
claimSystemShutdownEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10789 IOPMrootDomain::claimSystemShutdownEvent(
10790 IOService * device,
10791 IOOptionBits flags,
10792 const char * reason,
10793 __unused OSObject * details )
10794 {
10795 if (!device || !reason) {
10796 return;
10797 }
10798
10799 DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10800 WAKEEVENT_LOCK();
10801 if (gShutdownReasonString[0] != '\0') {
10802 strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
10803 }
10804 strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
10805
10806 gShutdownReasonSysctlRegistered = true;
10807 WAKEEVENT_UNLOCK();
10808 }
10809
10810 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10811
10812 // MARK: -
10813 // MARK: PMSettingHandle
10814
OSDefineMetaClassAndStructors(PMSettingHandle,OSObject)10815 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
10816
10817 void
10818 PMSettingHandle::free( void )
10819 {
10820 if (pmso) {
10821 pmso->clientHandleFreed();
10822 pmso->release();
10823 pmso = NULL;
10824 }
10825
10826 OSObject::free();
10827 }
10828
10829 // MARK: -
10830 // MARK: PMSettingObject
10831
10832 #undef super
10833 #define super OSObject
OSDefineMetaClassAndFinalStructors(PMSettingObject,OSObject)10834 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
10835
10836 /*
10837 * Static constructor/initializer for PMSettingObject
10838 */
10839 PMSettingObject *PMSettingObject::pmSettingObject(
10840 IOPMrootDomain * parent_arg,
10841 IOPMSettingControllerCallback handler_arg,
10842 OSObject * target_arg,
10843 uintptr_t refcon_arg,
10844 uint32_t supportedPowerSources,
10845 const OSSymbol * settings[],
10846 OSObject * *handle_obj)
10847 {
10848 uint32_t settingCount = 0;
10849 PMSettingObject *pmso = NULL;
10850 PMSettingHandle *pmsh = NULL;
10851
10852 if (!parent_arg || !handler_arg || !settings || !handle_obj) {
10853 return NULL;
10854 }
10855
10856 // count OSSymbol entries in NULL terminated settings array
10857 while (settings[settingCount]) {
10858 settingCount++;
10859 }
10860 if (0 == settingCount) {
10861 return NULL;
10862 }
10863
10864 pmso = new PMSettingObject;
10865 if (!pmso || !pmso->init()) {
10866 goto fail;
10867 }
10868
10869 pmsh = new PMSettingHandle;
10870 if (!pmsh || !pmsh->init()) {
10871 goto fail;
10872 }
10873
10874 queue_init(&pmso->calloutQueue);
10875 pmso->parent = parent_arg;
10876 pmso->func = handler_arg;
10877 pmso->target = target_arg;
10878 pmso->refcon = refcon_arg;
10879 pmso->settingCount = settingCount;
10880
10881 pmso->retain(); // handle holds a retain on pmso
10882 pmsh->pmso = pmso;
10883 pmso->pmsh = pmsh;
10884
10885 pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
10886 if (pmso->publishedFeatureID) {
10887 for (unsigned int i = 0; i < settingCount; i++) {
10888 // Since there is now at least one listener to this setting, publish
10889 // PM root domain support for it.
10890 parent_arg->publishPMSetting( settings[i],
10891 supportedPowerSources, &pmso->publishedFeatureID[i] );
10892 }
10893 }
10894
10895 *handle_obj = pmsh;
10896 return pmso;
10897
10898 fail:
10899 if (pmso) {
10900 pmso->release();
10901 }
10902 if (pmsh) {
10903 pmsh->release();
10904 }
10905 return NULL;
10906 }
10907
10908 void
free(void)10909 PMSettingObject::free( void )
10910 {
10911 if (publishedFeatureID) {
10912 for (const auto& featureID : publishedFeatureID) {
10913 if (featureID) {
10914 parent->removePublishedFeature( featureID );
10915 }
10916 }
10917
10918 publishedFeatureID = {};
10919 }
10920
10921 super::free();
10922 }
10923
10924 IOReturn
dispatchPMSetting(const OSSymbol * type,OSObject * object)10925 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
10926 {
10927 return (*func)(target, type, object, refcon);
10928 }
10929
10930 void
clientHandleFreed(void)10931 PMSettingObject::clientHandleFreed( void )
10932 {
10933 parent->deregisterPMSettingObject(this);
10934 }
10935
10936 // MARK: -
10937 // MARK: PMAssertionsTracker
10938
10939 //*********************************************************************************
10940 //*********************************************************************************
10941 //*********************************************************************************
10942 // class PMAssertionsTracker Implementation
10943
10944 #define kAssertUniqueIDStart 500
10945
10946 PMAssertionsTracker *
pmAssertionsTracker(IOPMrootDomain * rootDomain)10947 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
10948 {
10949 PMAssertionsTracker *me;
10950
10951 me = new PMAssertionsTracker;
10952 if (!me || !me->init()) {
10953 if (me) {
10954 me->release();
10955 }
10956 return NULL;
10957 }
10958
10959 me->owner = rootDomain;
10960 me->issuingUniqueID = kAssertUniqueIDStart;
10961 me->assertionsArray = OSArray::withCapacity(5);
10962 me->assertionsKernel = 0;
10963 me->assertionsUser = 0;
10964 me->assertionsCombined = 0;
10965 me->assertionsArrayLock = IOLockAlloc();
10966 me->tabulateProducerCount = me->tabulateConsumerCount = 0;
10967
10968 assert(me->assertionsArray);
10969 assert(me->assertionsArrayLock);
10970
10971 return me;
10972 }
10973
10974 /* tabulate
10975 * - Update assertionsKernel to reflect the state of all
10976 * assertions in the kernel.
10977 * - Update assertionsCombined to reflect both kernel & user space.
10978 */
10979 void
tabulate(void)10980 PMAssertionsTracker::tabulate(void)
10981 {
10982 int i;
10983 int count;
10984 PMAssertStruct *_a = NULL;
10985 OSData *_d = NULL;
10986
10987 IOPMDriverAssertionType oldKernel = assertionsKernel;
10988 IOPMDriverAssertionType oldCombined = assertionsCombined;
10989
10990 ASSERT_GATED();
10991
10992 assertionsKernel = 0;
10993 assertionsCombined = 0;
10994
10995 if (!assertionsArray) {
10996 return;
10997 }
10998
10999 if ((count = assertionsArray->getCount())) {
11000 for (i = 0; i < count; i++) {
11001 _d = OSDynamicCast(OSData, assertionsArray->getObject(i));
11002 if (_d) {
11003 _a = (PMAssertStruct *)_d->getBytesNoCopy();
11004 if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11005 assertionsKernel |= _a->assertionBits;
11006 }
11007 }
11008 }
11009 }
11010
11011 tabulateProducerCount++;
11012 assertionsCombined = assertionsKernel | assertionsUser;
11013
11014 if ((assertionsKernel != oldKernel) ||
11015 (assertionsCombined != oldCombined)) {
11016 owner->evaluateAssertions(assertionsCombined, oldCombined);
11017 }
11018 }
11019
11020 void
updateCPUBitAccounting(PMAssertStruct * assertStruct)11021 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11022 {
11023 AbsoluteTime now;
11024 uint64_t nsec;
11025
11026 if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11027 (assertStruct->assertCPUStartTime == 0)) {
11028 return;
11029 }
11030
11031 now = mach_absolute_time();
11032 SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11033 absolutetime_to_nanoseconds(now, &nsec);
11034 assertStruct->assertCPUDuration += nsec;
11035 assertStruct->assertCPUStartTime = 0;
11036
11037 if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11038 maxAssertCPUDuration = assertStruct->assertCPUDuration;
11039 maxAssertCPUEntryId = assertStruct->registryEntryID;
11040 }
11041 }
11042
11043 void
reportCPUBitAccounting(void)11044 PMAssertionsTracker::reportCPUBitAccounting( void )
11045 {
11046 PMAssertStruct *_a;
11047 OSData *_d;
11048 int i, count;
11049 AbsoluteTime now;
11050 uint64_t nsec;
11051
11052 ASSERT_GATED();
11053
11054 // Account for drivers that are still holding the CPU assertion
11055 if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11056 now = mach_absolute_time();
11057 if ((count = assertionsArray->getCount())) {
11058 for (i = 0; i < count; i++) {
11059 _d = OSDynamicCast(OSData, assertionsArray->getObject(i));
11060 if (_d) {
11061 _a = (PMAssertStruct *)_d->getBytesNoCopy();
11062 if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11063 (_a->level == kIOPMDriverAssertionLevelOn) &&
11064 (_a->assertCPUStartTime != 0)) {
11065 // Don't modify PMAssertStruct, leave that
11066 // for updateCPUBitAccounting()
11067 SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11068 absolutetime_to_nanoseconds(now, &nsec);
11069 nsec += _a->assertCPUDuration;
11070 if (nsec > maxAssertCPUDuration) {
11071 maxAssertCPUDuration = nsec;
11072 maxAssertCPUEntryId = _a->registryEntryID;
11073 }
11074 }
11075 }
11076 }
11077 }
11078 }
11079
11080 if (maxAssertCPUDuration) {
11081 DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11082 (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11083 }
11084
11085 maxAssertCPUDuration = 0;
11086 maxAssertCPUEntryId = 0;
11087 }
11088
11089 void
publishProperties(void)11090 PMAssertionsTracker::publishProperties( void )
11091 {
11092 OSSharedPtr<OSArray> assertionsSummary;
11093
11094 if (tabulateConsumerCount != tabulateProducerCount) {
11095 IOLockLock(assertionsArrayLock);
11096
11097 tabulateConsumerCount = tabulateProducerCount;
11098
11099 /* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11100 */
11101 assertionsSummary = copyAssertionsArray();
11102 if (assertionsSummary) {
11103 owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11104 } else {
11105 owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11106 }
11107
11108 /* Publish the IOPMrootDomain property "DriverPMAssertions"
11109 */
11110 owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11111
11112 IOLockUnlock(assertionsArrayLock);
11113 }
11114 }
11115
11116 PMAssertionsTracker::PMAssertStruct *
detailsForID(IOPMDriverAssertionID _id,int * index)11117 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11118 {
11119 PMAssertStruct *_a = NULL;
11120 OSData *_d = NULL;
11121 int found = -1;
11122 int count = 0;
11123 int i = 0;
11124
11125 if (assertionsArray
11126 && (count = assertionsArray->getCount())) {
11127 for (i = 0; i < count; i++) {
11128 _d = OSDynamicCast(OSData, assertionsArray->getObject(i));
11129 if (_d) {
11130 _a = (PMAssertStruct *)_d->getBytesNoCopy();
11131 if (_a && (_id == _a->id)) {
11132 found = i;
11133 break;
11134 }
11135 }
11136 }
11137 }
11138
11139 if (-1 == found) {
11140 return NULL;
11141 } else {
11142 if (index) {
11143 *index = found;
11144 }
11145 return _a;
11146 }
11147 }
11148
11149 /* PMAssertionsTracker::handleCreateAssertion
11150 * Perform assertion work on the PM workloop. Do not call directly.
11151 */
11152 IOReturn
handleCreateAssertion(OSData * newAssertion)11153 PMAssertionsTracker::handleCreateAssertion(OSData *newAssertion)
11154 {
11155 PMAssertStruct *assertStruct;
11156
11157 ASSERT_GATED();
11158
11159 if (newAssertion) {
11160 IOLockLock(assertionsArrayLock);
11161 assertStruct = (PMAssertStruct *) newAssertion->getBytesNoCopy();
11162 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11163 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11164 assertStruct->assertCPUStartTime = mach_absolute_time();
11165 }
11166 assertionsArray->setObject(newAssertion);
11167 IOLockUnlock(assertionsArrayLock);
11168 newAssertion->release();
11169
11170 tabulate();
11171 }
11172 return kIOReturnSuccess;
11173 }
11174
11175 /* PMAssertionsTracker::createAssertion
11176 * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11177 * appropiate.
11178 */
11179 IOReturn
createAssertion(IOPMDriverAssertionType which,IOPMDriverAssertionLevel level,IOService * serviceID,const char * whoItIs,IOPMDriverAssertionID * outID)11180 PMAssertionsTracker::createAssertion(
11181 IOPMDriverAssertionType which,
11182 IOPMDriverAssertionLevel level,
11183 IOService *serviceID,
11184 const char *whoItIs,
11185 IOPMDriverAssertionID *outID)
11186 {
11187 OSSharedPtr<OSData> dataStore;
11188 PMAssertStruct track;
11189
11190 // Warning: trillions and trillions of created assertions may overflow the unique ID.
11191 track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11192 track.level = level;
11193 track.assertionBits = which;
11194
11195 // NB: ownerString is explicitly managed by PMAssertStruct
11196 // it will be released in `handleReleaseAssertion' below
11197 track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11198 track.ownerService = serviceID;
11199 track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11200 track.modifiedTime = 0;
11201 pmEventTimeStamp(&track.createdTime);
11202 track.assertCPUStartTime = 0;
11203 track.assertCPUDuration = 0;
11204
11205 dataStore = OSData::withBytes(&track, sizeof(PMAssertStruct));
11206 if (!dataStore) {
11207 if (track.ownerString) {
11208 track.ownerString->release();
11209 track.ownerString = NULL;
11210 }
11211 return kIOReturnNoMemory;
11212 }
11213
11214 *outID = track.id;
11215
11216 if (owner && owner->pmPowerStateQueue) {
11217 // queue action is responsible for releasing dataStore
11218 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11219 }
11220
11221 return kIOReturnSuccess;
11222 }
11223
11224 /* PMAssertionsTracker::handleReleaseAssertion
11225 * Runs in PM workloop. Do not call directly.
11226 */
11227 IOReturn
handleReleaseAssertion(IOPMDriverAssertionID _id)11228 PMAssertionsTracker::handleReleaseAssertion(
11229 IOPMDriverAssertionID _id)
11230 {
11231 ASSERT_GATED();
11232
11233 int index;
11234 PMAssertStruct *assertStruct = detailsForID(_id, &index);
11235
11236 if (!assertStruct) {
11237 return kIOReturnNotFound;
11238 }
11239
11240 IOLockLock(assertionsArrayLock);
11241
11242 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11243 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11244 updateCPUBitAccounting(assertStruct);
11245 }
11246
11247 if (assertStruct->ownerString) {
11248 assertStruct->ownerString->release();
11249 assertStruct->ownerString = NULL;
11250 }
11251
11252 assertionsArray->removeObject(index);
11253 IOLockUnlock(assertionsArrayLock);
11254
11255 tabulate();
11256 return kIOReturnSuccess;
11257 }
11258
11259 /* PMAssertionsTracker::releaseAssertion
11260 * Releases an assertion and affects system behavior if appropiate.
11261 * Actual work happens on PM workloop.
11262 */
11263 IOReturn
releaseAssertion(IOPMDriverAssertionID _id)11264 PMAssertionsTracker::releaseAssertion(
11265 IOPMDriverAssertionID _id)
11266 {
11267 if (owner && owner->pmPowerStateQueue) {
11268 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11269 }
11270 return kIOReturnSuccess;
11271 }
11272
11273 /* PMAssertionsTracker::handleSetAssertionLevel
11274 * Runs in PM workloop. Do not call directly.
11275 */
11276 IOReturn
handleSetAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11277 PMAssertionsTracker::handleSetAssertionLevel(
11278 IOPMDriverAssertionID _id,
11279 IOPMDriverAssertionLevel _level)
11280 {
11281 PMAssertStruct *assertStruct = detailsForID(_id, NULL);
11282
11283 ASSERT_GATED();
11284
11285 if (!assertStruct) {
11286 return kIOReturnNotFound;
11287 }
11288
11289 IOLockLock(assertionsArrayLock);
11290 pmEventTimeStamp(&assertStruct->modifiedTime);
11291 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11292 (assertStruct->level != _level)) {
11293 if (_level == kIOPMDriverAssertionLevelOn) {
11294 assertStruct->assertCPUStartTime = mach_absolute_time();
11295 } else {
11296 updateCPUBitAccounting(assertStruct);
11297 }
11298 }
11299 assertStruct->level = _level;
11300 IOLockUnlock(assertionsArrayLock);
11301
11302 tabulate();
11303 return kIOReturnSuccess;
11304 }
11305
11306 /* PMAssertionsTracker::setAssertionLevel
11307 */
11308 IOReturn
setAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11309 PMAssertionsTracker::setAssertionLevel(
11310 IOPMDriverAssertionID _id,
11311 IOPMDriverAssertionLevel _level)
11312 {
11313 if (owner && owner->pmPowerStateQueue) {
11314 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11315 (void *)(uintptr_t)_level, _id);
11316 }
11317
11318 return kIOReturnSuccess;
11319 }
11320
11321 IOReturn
handleSetUserAssertionLevels(void * arg0)11322 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11323 {
11324 IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11325
11326 ASSERT_GATED();
11327
11328 if (new_user_levels != assertionsUser) {
11329 DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11330 assertionsUser = new_user_levels;
11331 }
11332
11333 tabulate();
11334 return kIOReturnSuccess;
11335 }
11336
11337 IOReturn
setUserAssertionLevels(IOPMDriverAssertionType new_user_levels)11338 PMAssertionsTracker::setUserAssertionLevels(
11339 IOPMDriverAssertionType new_user_levels)
11340 {
11341 if (gIOPMWorkLoop) {
11342 gIOPMWorkLoop->runAction(
11343 OSMemberFunctionCast(
11344 IOWorkLoop::Action,
11345 this,
11346 &PMAssertionsTracker::handleSetUserAssertionLevels),
11347 this,
11348 (void *) &new_user_levels, NULL, NULL, NULL);
11349 }
11350
11351 return kIOReturnSuccess;
11352 }
11353
11354
11355 OSSharedPtr<OSArray>
copyAssertionsArray(void)11356 PMAssertionsTracker::copyAssertionsArray(void)
11357 {
11358 int count;
11359 int i;
11360 OSSharedPtr<OSArray> outArray = NULL;
11361
11362 if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11363 goto exit;
11364 }
11365 outArray = OSArray::withCapacity(count);
11366 if (!outArray) {
11367 goto exit;
11368 }
11369
11370 for (i = 0; i < count; i++) {
11371 PMAssertStruct *_a = NULL;
11372 OSData *_d = NULL;
11373 OSSharedPtr<OSDictionary> details;
11374
11375 _d = OSDynamicCast(OSData, assertionsArray->getObject(i));
11376 if (_d && (_a = (PMAssertStruct *)_d->getBytesNoCopy())) {
11377 OSSharedPtr<OSNumber> _n;
11378
11379 details = OSDictionary::withCapacity(7);
11380 if (!details) {
11381 continue;
11382 }
11383
11384 outArray->setObject(details.get());
11385
11386 _n = OSNumber::withNumber(_a->id, 64);
11387 if (_n) {
11388 details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11389 }
11390 _n = OSNumber::withNumber(_a->createdTime, 64);
11391 if (_n) {
11392 details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11393 }
11394 _n = OSNumber::withNumber(_a->modifiedTime, 64);
11395 if (_n) {
11396 details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11397 }
11398 _n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11399 if (_n) {
11400 details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11401 }
11402 _n = OSNumber::withNumber(_a->level, 64);
11403 if (_n) {
11404 details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11405 }
11406 _n = OSNumber::withNumber(_a->assertionBits, 64);
11407 if (_n) {
11408 details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11409 }
11410
11411 if (_a->ownerString) {
11412 details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11413 }
11414 }
11415 }
11416
11417 exit:
11418 return os::move(outArray);
11419 }
11420
11421 IOPMDriverAssertionType
getActivatedAssertions(void)11422 PMAssertionsTracker::getActivatedAssertions(void)
11423 {
11424 return assertionsCombined;
11425 }
11426
11427 IOPMDriverAssertionLevel
getAssertionLevel(IOPMDriverAssertionType type)11428 PMAssertionsTracker::getAssertionLevel(
11429 IOPMDriverAssertionType type)
11430 {
11431 // FIXME: unused and also wrong
11432 if (type && ((type & assertionsKernel) == assertionsKernel)) {
11433 return kIOPMDriverAssertionLevelOn;
11434 } else {
11435 return kIOPMDriverAssertionLevelOff;
11436 }
11437 }
11438
11439 //*********************************************************************************
11440 //*********************************************************************************
11441 //*********************************************************************************
11442
11443
11444 static void
pmEventTimeStamp(uint64_t * recordTS)11445 pmEventTimeStamp(uint64_t *recordTS)
11446 {
11447 clock_sec_t tsec;
11448 clock_usec_t tusec;
11449
11450 if (!recordTS) {
11451 return;
11452 }
11453
11454 // We assume tsec fits into 32 bits; 32 bits holds enough
11455 // seconds for 136 years since the epoch in 1970.
11456 clock_get_calendar_microtime(&tsec, &tusec);
11457
11458
11459 // Pack the sec & microsec calendar time into a uint64_t, for fun.
11460 *recordTS = 0;
11461 *recordTS |= (uint32_t)tusec;
11462 *recordTS |= ((uint64_t)tsec << 32);
11463
11464 return;
11465 }
11466
11467 // MARK: -
11468 // MARK: IORootParent
11469
11470 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11471
11472 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11473
11474 // The reason that root domain needs a root parent is to facilitate demand
11475 // sleep, since a power change from the root parent cannot be vetoed.
11476 //
11477 // The above statement is no longer true since root domain now performs
11478 // demand sleep using overrides. But root parent remains to avoid changing
11479 // the power tree stacking. Root parent is parked at the max power state.
11480
11481
11482 static IOPMPowerState patriarchPowerStates[2] =
11483 {
11484 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11485 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11486 };
11487
11488 void
initialize(void)11489 IORootParent::initialize( void )
11490 {
11491
11492 gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11493 gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11494 gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11495 gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11496 gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11497 gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11498 gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11499 gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11500 gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11501 gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11502 gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11503 gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11504 gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11505 gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11506 gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11507 gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11508 gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11509 gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11510 gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11511 gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11512 gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11513 gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11514 gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11515 gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11516 gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11517 gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11518 gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11519 gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11520 gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11521 gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11522 gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11523 gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11524 gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11525 gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11526 gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11527 gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11528 gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11529 gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11530 gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11531 gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11532 gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11533 gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11534 gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11535 gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11536 }
11537
11538 bool
start(IOService * nub)11539 IORootParent::start( IOService * nub )
11540 {
11541 IOService::start(nub);
11542 attachToParent( getRegistryRoot(), gIOPowerPlane );
11543 PMinit();
11544 registerPowerDriver(this, patriarchPowerStates, 2);
11545 makeUsable();
11546 return true;
11547 }
11548
11549 void
shutDownSystem(void)11550 IORootParent::shutDownSystem( void )
11551 {
11552 }
11553
11554 void
restartSystem(void)11555 IORootParent::restartSystem( void )
11556 {
11557 }
11558
11559 void
sleepSystem(void)11560 IORootParent::sleepSystem( void )
11561 {
11562 }
11563
11564 void
dozeSystem(void)11565 IORootParent::dozeSystem( void )
11566 {
11567 }
11568
11569 void
sleepToDoze(void)11570 IORootParent::sleepToDoze( void )
11571 {
11572 }
11573
11574 void
wakeSystem(void)11575 IORootParent::wakeSystem( void )
11576 {
11577 }
11578
11579 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const11580 IORootParent::copyProperty( const char * aKey) const
11581 {
11582 return IOService::copyProperty(aKey);
11583 }
11584
11585 uint32_t
getWatchdogTimeout()11586 IOPMrootDomain::getWatchdogTimeout()
11587 {
11588 if (gSwdSleepWakeTimeout) {
11589 gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11590 }
11591 if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11592 (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11593 return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11594 } else {
11595 return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11596 }
11597 }
11598
11599
11600 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
11601 IOReturn
restartWithStackshot()11602 IOPMrootDomain::restartWithStackshot()
11603 {
11604 takeStackshot(true);
11605
11606 return kIOReturnSuccess;
11607 }
11608
11609 void
sleepWakeDebugTrig(bool wdogTrigger)11610 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
11611 {
11612 takeStackshot(wdogTrigger);
11613 }
11614
11615 void
tracePhase2String(uint32_t tracePhase,const char ** phaseString,const char ** description)11616 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
11617 {
11618 switch (tracePhase) {
11619 case kIOPMTracePointSleepStarted:
11620 *phaseString = "kIOPMTracePointSleepStarted";
11621 *description = "starting sleep";
11622 break;
11623
11624 case kIOPMTracePointSleepApplications:
11625 *phaseString = "kIOPMTracePointSleepApplications";
11626 *description = "notifying applications";
11627 break;
11628
11629 case kIOPMTracePointSleepPriorityClients:
11630 *phaseString = "kIOPMTracePointSleepPriorityClients";
11631 *description = "notifying clients about upcoming system capability changes";
11632 break;
11633
11634 case kIOPMTracePointSleepWillChangeInterests:
11635 *phaseString = "kIOPMTracePointSleepWillChangeInterests";
11636 *description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
11637 break;
11638
11639 case kIOPMTracePointSleepPowerPlaneDrivers:
11640 *phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
11641 *description = "calling power state change callbacks";
11642 break;
11643
11644 case kIOPMTracePointSleepDidChangeInterests:
11645 *phaseString = "kIOPMTracePointSleepDidChangeInterests";
11646 *description = "calling rootDomain's clients about rootDomain's state changes";
11647 break;
11648
11649 case kIOPMTracePointSleepCapabilityClients:
11650 *phaseString = "kIOPMTracePointSleepCapabilityClients";
11651 *description = "notifying clients about current system capabilities";
11652 break;
11653
11654 case kIOPMTracePointSleepPlatformActions:
11655 *phaseString = "kIOPMTracePointSleepPlatformActions";
11656 *description = "calling Quiesce/Sleep action callbacks";
11657 break;
11658
11659 case kIOPMTracePointSleepCPUs:
11660 {
11661 *phaseString = "kIOPMTracePointSleepCPUs";
11662 #if defined(__i386__) || defined(__x86_64__)
11663 /*
11664 * We cannot use the getCPUNumber() method to get the cpu number, since
11665 * that cpu number is unrelated to the cpu number we need (we need the cpu
11666 * number as enumerated by the scheduler, NOT the CPU number enumerated
11667 * by ACPIPlatform as the CPUs are enumerated in MADT order).
11668 * Instead, pass the Mach processor pointer associated with the current
11669 * shutdown target so its associated cpu_id can be used in
11670 * processor_to_datastring.
11671 */
11672 if (currentShutdownTarget != NULL &&
11673 currentShutdownTarget->getMachProcessor() != NULL) {
11674 const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
11675 currentShutdownTarget->getMachProcessor());
11676 *description = sbuf;
11677 } else {
11678 *description = "halting all non-boot CPUs";
11679 }
11680 #else
11681 *description = "halting all non-boot CPUs";
11682 #endif
11683 break;
11684 }
11685 case kIOPMTracePointSleepPlatformDriver:
11686 *phaseString = "kIOPMTracePointSleepPlatformDriver";
11687 *description = "executing platform specific code";
11688 break;
11689
11690 case kIOPMTracePointHibernate:
11691 *phaseString = "kIOPMTracePointHibernate";
11692 *description = "writing the hibernation image";
11693 break;
11694
11695 case kIOPMTracePointSystemSleep:
11696 *phaseString = "kIOPMTracePointSystemSleep";
11697 *description = "in EFI/Bootrom after last point of entry to sleep";
11698 break;
11699
11700 case kIOPMTracePointWakePlatformDriver:
11701 *phaseString = "kIOPMTracePointWakePlatformDriver";
11702 *description = "executing platform specific code";
11703 break;
11704
11705
11706 case kIOPMTracePointWakePlatformActions:
11707 *phaseString = "kIOPMTracePointWakePlatformActions";
11708 *description = "calling Wake action callbacks";
11709 break;
11710
11711 case kIOPMTracePointWakeCPUs:
11712 *phaseString = "kIOPMTracePointWakeCPUs";
11713 *description = "starting non-boot CPUs";
11714 break;
11715
11716 case kIOPMTracePointWakeWillPowerOnClients:
11717 *phaseString = "kIOPMTracePointWakeWillPowerOnClients";
11718 *description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
11719 break;
11720
11721 case kIOPMTracePointWakeWillChangeInterests:
11722 *phaseString = "kIOPMTracePointWakeWillChangeInterests";
11723 *description = "calling rootDomain's clients about upcoming rootDomain's state changes";
11724 break;
11725
11726 case kIOPMTracePointWakeDidChangeInterests:
11727 *phaseString = "kIOPMTracePointWakeDidChangeInterests";
11728 *description = "calling rootDomain's clients about completed rootDomain's state changes";
11729 break;
11730
11731 case kIOPMTracePointWakePowerPlaneDrivers:
11732 *phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
11733 *description = "calling power state change callbacks";
11734 break;
11735
11736 case kIOPMTracePointWakeCapabilityClients:
11737 *phaseString = "kIOPMTracePointWakeCapabilityClients";
11738 *description = "informing clients about current system capabilities";
11739 break;
11740
11741 case kIOPMTracePointWakeApplications:
11742 *phaseString = "kIOPMTracePointWakeApplications";
11743 *description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
11744 break;
11745
11746 case kIOPMTracePointDarkWakeEntry:
11747 *phaseString = "kIOPMTracePointDarkWakeEntry";
11748 *description = "entering darkwake on way to sleep";
11749 break;
11750
11751 case kIOPMTracePointDarkWakeExit:
11752 *phaseString = "kIOPMTracePointDarkWakeExit";
11753 *description = "entering fullwake from darkwake";
11754 break;
11755
11756 default:
11757 *phaseString = NULL;
11758 *description = NULL;
11759 }
11760 }
11761
11762 void
saveFailureData2File()11763 IOPMrootDomain::saveFailureData2File()
11764 {
11765 unsigned int len = 0;
11766 char failureStr[512];
11767 errno_t error;
11768 char *outbuf;
11769 OSNumber *statusCode;
11770 uint64_t pmStatusCode = 0;
11771 uint32_t phaseData = 0;
11772 uint32_t phaseDetail = 0;
11773 bool efiFailure = false;
11774
11775 OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
11776 statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
11777 if (statusCode) {
11778 pmStatusCode = statusCode->unsigned64BitValue();
11779 phaseData = pmStatusCode & 0xFFFFFFFF;
11780 phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
11781 if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
11782 LOG("Sleep Wake failure in EFI\n");
11783 efiFailure = true;
11784 failureStr[0] = 0;
11785 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);
11786 len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
11787 }
11788 }
11789
11790 if (!efiFailure) {
11791 if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
11792 swd_flags |= SWD_BOOT_BY_SW_WDOG;
11793 PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
11794 // dump panic will handle saving nvram data
11795 return;
11796 }
11797
11798 /* Keeping this around for capturing data during power
11799 * button press */
11800
11801 if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
11802 DLOG("No sleep wake failure string\n");
11803 return;
11804 }
11805 if (len == 0) {
11806 DLOG("Ignoring zero byte SleepWake failure string\n");
11807 goto exit;
11808 }
11809
11810 // if PMStatus code is zero, delete stackshot and return
11811 if (statusCode) {
11812 if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
11813 // there was no sleep wake failure
11814 // this can happen if delete stackshot was called
11815 // before take stackshot completed. Let us delete any
11816 // sleep wake failure data in nvram
11817 DLOG("Deleting stackshot on successful wake\n");
11818 deleteStackshot();
11819 return;
11820 }
11821 }
11822
11823 if (len > sizeof(failureStr)) {
11824 len = sizeof(failureStr);
11825 }
11826 failureStr[0] = 0;
11827 PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
11828 }
11829 if (failureStr[0] != 0) {
11830 error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
11831 if (error) {
11832 DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
11833 } else {
11834 DLOG("Saved SleepWake failure string to file.\n");
11835 }
11836 }
11837
11838 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
11839 goto exit;
11840 }
11841
11842 if (swd_buffer) {
11843 unsigned int len = 0;
11844 errno_t error;
11845 char nvram_var_name_buffer[20];
11846 unsigned int concat_len = 0;
11847 swd_hdr *hdr = NULL;
11848
11849
11850 hdr = (swd_hdr *)swd_buffer;
11851 outbuf = (char *)hdr + hdr->spindump_offset;
11852 OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
11853
11854 for (int i = 0; i < 8; i++) {
11855 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
11856 if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
11857 LOG("No SleepWake blob to read beyond chunk %d\n", i);
11858 break;
11859 }
11860 if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
11861 PERemoveNVRAMProperty(nvram_var_name_buffer);
11862 LOG("Could not read the property :-(\n");
11863 break;
11864 }
11865 PERemoveNVRAMProperty(nvram_var_name_buffer);
11866 concat_len += len;
11867 }
11868 LOG("Concatenated length for the SWD blob %d\n", concat_len);
11869
11870 if (concat_len) {
11871 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
11872 if (error) {
11873 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11874 } else {
11875 LOG("Saved SleepWake zipped data to file.\n");
11876 }
11877 } else {
11878 // There is a sleep wake failure string but no stackshot
11879 // Write a placeholder stacks file so that swd runs
11880 snprintf(outbuf, 20, "%s", "No stackshot data\n");
11881 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
11882 if (error) {
11883 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11884 } else {
11885 LOG("Saved SleepWake zipped data to file.\n");
11886 }
11887 }
11888 } else {
11889 LOG("No buffer allocated to save failure stackshot\n");
11890 }
11891
11892
11893 gRootDomain->swd_lock = 0;
11894 exit:
11895 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
11896 return;
11897 }
11898
11899
11900 void
getFailureData(thread_t * thread,char * failureStr,size_t strLen)11901 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
11902 {
11903 OSSharedPtr<IORegistryIterator> iter;
11904 OSSharedPtr<const OSSymbol> kextName = NULL;
11905 IORegistryEntry * entry;
11906 IOService * node;
11907 bool nodeFound = false;
11908
11909 const void * callMethod = NULL;
11910 const char * objectName = NULL;
11911 uint32_t timeout = getWatchdogTimeout();
11912 const char * phaseString = NULL;
11913 const char * phaseDescription = NULL;
11914
11915 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
11916 uint32_t tracePhase = pmTracer->getTracePhase();
11917
11918 *thread = NULL;
11919 if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
11920 snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
11921 } else {
11922 snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
11923 }
11924 tracePhase2String(tracePhase, &phaseString, &phaseDescription);
11925
11926 if (notifierThread) {
11927 if (notifier && (notifier->identifier)) {
11928 objectName = notifier->identifier->getCStringNoCopy();
11929 }
11930 *thread = notifierThread;
11931 } else {
11932 iter = IORegistryIterator::iterateOver(
11933 getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
11934
11935 if (iter) {
11936 while ((entry = iter->getNextObject())) {
11937 node = OSDynamicCast(IOService, entry);
11938 if (!node) {
11939 continue;
11940 }
11941 if (OSDynamicCast(IOPowerConnection, node)) {
11942 continue;
11943 }
11944
11945 if (node->getBlockingDriverCall(thread, &callMethod)) {
11946 nodeFound = true;
11947 break;
11948 }
11949 }
11950 }
11951 if (nodeFound) {
11952 kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
11953 if (kextName) {
11954 objectName = kextName->getCStringNoCopy();
11955 }
11956 }
11957 }
11958 if (phaseDescription) {
11959 strlcat(failureStr, " while ", strLen);
11960 strlcat(failureStr, phaseDescription, strLen);
11961 strlcat(failureStr, ".", strLen);
11962 }
11963 if (objectName) {
11964 strlcat(failureStr, " Suspected bundle: ", strLen);
11965 strlcat(failureStr, objectName, strLen);
11966 strlcat(failureStr, ".", strLen);
11967 }
11968 if (*thread) {
11969 char threadName[40];
11970 snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
11971 strlcat(failureStr, threadName, strLen);
11972 }
11973
11974 DLOG("%s\n", failureStr);
11975 }
11976
11977 struct swd_stackshot_compressed_data {
11978 z_output_func zoutput;
11979 size_t zipped;
11980 uint64_t totalbytes;
11981 uint64_t lastpercent;
11982 IOReturn error;
11983 unsigned outremain;
11984 unsigned outlen;
11985 unsigned writes;
11986 Bytef * outbuf;
11987 };
11988 struct swd_stackshot_compressed_data swd_zip_var = { };
11989
11990 static void *
swd_zs_alloc(void * __unused ref,u_int items,u_int size)11991 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
11992 {
11993 void *result;
11994 LOG("Alloc in zipping %d items of size %d\n", items, size);
11995
11996 result = (void *)(swd_zs_zmem + swd_zs_zoffset);
11997 swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
11998 LOG("Offset %zu\n", swd_zs_zoffset);
11999 return result;
12000 }
12001
12002 static int
swd_zinput(z_streamp strm,Bytef * buf,unsigned size)12003 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12004 {
12005 unsigned len;
12006
12007 len = strm->avail_in;
12008
12009 if (len > size) {
12010 len = size;
12011 }
12012 if (len == 0) {
12013 return 0;
12014 }
12015
12016 if (strm->next_in != (Bytef *) strm) {
12017 memcpy(buf, strm->next_in, len);
12018 } else {
12019 bzero(buf, len);
12020 }
12021
12022 strm->adler = z_crc32(strm->adler, buf, len);
12023
12024 strm->avail_in -= len;
12025 strm->next_in += len;
12026 strm->total_in += len;
12027
12028 return (int)len;
12029 }
12030
12031 static int
swd_zoutput(z_streamp strm,Bytef * buf,unsigned len)12032 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12033 {
12034 unsigned int i = 0;
12035 // if outlen > max size don't add to the buffer
12036 assert(buf != NULL);
12037 if (strm && buf) {
12038 if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12039 LOG("No space to GZIP... not writing to NVRAM\n");
12040 return len;
12041 }
12042 }
12043 for (i = 0; i < len; i++) {
12044 *(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12045 }
12046 swd_zip_var.outlen += len;
12047 return len;
12048 }
12049
12050 static void
swd_zs_free(void * __unused ref,void * __unused ptr)12051 swd_zs_free(void * __unused ref, void * __unused ptr)
12052 {
12053 }
12054
12055 static int
swd_compress(char * inPtr,char * outPtr,size_t numBytes)12056 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12057 {
12058 int wbits = 12;
12059 int memlevel = 3;
12060
12061 if (((unsigned int) numBytes) != numBytes) {
12062 return 0;
12063 }
12064
12065 if (!swd_zs.zalloc) {
12066 swd_zs.zalloc = swd_zs_alloc;
12067 swd_zs.zfree = swd_zs_free;
12068 if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12069 // allocation failed
12070 bzero(&swd_zs, sizeof(swd_zs));
12071 // swd_zs_zoffset = 0;
12072 } else {
12073 LOG("PMRD inited the zlib allocation routines\n");
12074 }
12075 }
12076
12077 swd_zip_var.zipped = 0;
12078 swd_zip_var.totalbytes = 0; // should this be the max that we have?
12079 swd_zip_var.lastpercent = 0;
12080 swd_zip_var.error = kIOReturnSuccess;
12081 swd_zip_var.outremain = 0;
12082 swd_zip_var.outlen = 0;
12083 swd_zip_var.writes = 0;
12084 swd_zip_var.outbuf = (Bytef *)outPtr;
12085
12086 swd_zip_var.totalbytes = numBytes;
12087
12088 swd_zs.avail_in = 0;
12089 swd_zs.next_in = NULL;
12090 swd_zs.avail_out = 0;
12091 swd_zs.next_out = NULL;
12092
12093 deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12094
12095 z_stream *zs;
12096 int zr;
12097 zs = &swd_zs;
12098
12099 while (swd_zip_var.error >= 0) {
12100 if (!zs->avail_in) {
12101 zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12102 zs->avail_in = (unsigned int) numBytes;
12103 }
12104 if (!zs->avail_out) {
12105 zs->next_out = (Bytef *)zs;
12106 zs->avail_out = UINT32_MAX;
12107 }
12108 zr = deflate(zs, Z_NO_FLUSH);
12109 if (Z_STREAM_END == zr) {
12110 break;
12111 }
12112 if (zr != Z_OK) {
12113 LOG("ZERR %d\n", zr);
12114 swd_zip_var.error = zr;
12115 } else {
12116 if (zs->total_in == numBytes) {
12117 break;
12118 }
12119 }
12120 }
12121
12122 //now flush the stream
12123 while (swd_zip_var.error >= 0) {
12124 if (!zs->avail_out) {
12125 zs->next_out = (Bytef *)zs;
12126 zs->avail_out = UINT32_MAX;
12127 }
12128 zr = deflate(zs, Z_FINISH);
12129 if (Z_STREAM_END == zr) {
12130 break;
12131 }
12132 if (zr != Z_OK) {
12133 LOG("ZERR %d\n", zr);
12134 swd_zip_var.error = zr;
12135 } else {
12136 if (zs->total_in == numBytes) {
12137 LOG("Total output size %d\n", swd_zip_var.outlen);
12138 break;
12139 }
12140 }
12141 }
12142
12143 return swd_zip_var.outlen;
12144 }
12145
12146 void
deleteStackshot()12147 IOPMrootDomain::deleteStackshot()
12148 {
12149 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12150 // takeStackshot hasn't completed
12151 return;
12152 }
12153 LOG("Deleting any sleepwake failure data in nvram\n");
12154
12155 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12156 char nvram_var_name_buf[20];
12157 for (int i = 0; i < 8; i++) {
12158 snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12159 if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12160 LOG("Removing %s returned false\n", nvram_var_name_buf);
12161 }
12162 }
12163 // force NVRAM sync
12164 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12165 DLOG("Failed to force nvram sync\n");
12166 }
12167 gRootDomain->swd_lock = 0;
12168 }
12169
12170 void
takeStackshot(bool wdogTrigger)12171 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12172 {
12173 swd_hdr * hdr = NULL;
12174 int cnt = 0;
12175 int max_cnt = 2;
12176 pid_t pid = 0;
12177 kern_return_t kr = KERN_SUCCESS;
12178 uint64_t flags;
12179
12180 char * dstAddr;
12181 uint32_t size;
12182 uint32_t bytesRemaining;
12183 unsigned bytesWritten = 0;
12184
12185 char failureStr[512];
12186 thread_t thread = NULL;
12187 const char * swfPanic = "swfPanic";
12188
12189 uint32_t bufSize;
12190 int success = 0;
12191
12192 #if defined(__i386__) || defined(__x86_64__)
12193 const bool concise = false;
12194 #else
12195 const bool concise = true;
12196 #endif
12197
12198 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12199 return;
12200 }
12201
12202 failureStr[0] = 0;
12203 if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12204 return;
12205 }
12206
12207 if (wdogTrigger) {
12208 getFailureData(&thread, failureStr, sizeof(failureStr));
12209
12210 if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12211 goto skip_stackshot;
12212 }
12213 } else {
12214 AbsoluteTime now;
12215 uint64_t nsec;
12216 clock_get_uptime(&now);
12217 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12218 absolutetime_to_nanoseconds(now, &nsec);
12219 snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12220 }
12221
12222 if (swd_buffer == NULL) {
12223 sleepWakeDebugMemAlloc();
12224 if (swd_buffer == NULL) {
12225 return;
12226 }
12227 }
12228 hdr = (swd_hdr *)swd_buffer;
12229 bufSize = hdr->alloc_size;
12230
12231 dstAddr = (char*)hdr + hdr->spindump_offset;
12232 flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_NO_IO_STATS | STACKSHOT_SAVE_KEXT_LOADINFO | STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY | STACKSHOT_THREAD_WAITINFO;
12233 /* If not wdogTrigger only take kernel tasks stackshot
12234 */
12235 if (wdogTrigger) {
12236 pid = -1;
12237 } else {
12238 pid = 0;
12239 }
12240
12241 /* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12242 * If we run out of space, take stackshot with only kernel task
12243 */
12244 while (success == 0 && cnt < max_cnt) {
12245 bytesRemaining = bufSize - hdr->spindump_offset;
12246 cnt++;
12247 DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12248
12249 size = bytesRemaining;
12250 kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12251 DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12252 kr, pid, size, flags, bytesWritten);
12253 if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12254 if (pid == -1) {
12255 pid = 0;
12256 } else {
12257 LOG("Insufficient buffer size for only kernel task\n");
12258 break;
12259 }
12260 }
12261 if (kr == KERN_SUCCESS) {
12262 if (bytesWritten == 0) {
12263 MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12264 continue;
12265 }
12266 bytesRemaining -= bytesWritten;
12267 hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12268
12269 memset(hdr->reason, 0x20, sizeof(hdr->reason));
12270
12271 // Compress stackshot and save to NVRAM
12272 {
12273 char *outbuf = (char *)swd_compressed_buffer;
12274 int outlen = 0;
12275 int num_chunks = 0;
12276 int max_chunks = 0;
12277 int leftover = 0;
12278 char nvram_var_name_buffer[20];
12279
12280 outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12281
12282 if (outlen) {
12283 max_chunks = outlen / (2096 - 200);
12284 leftover = outlen % (2096 - 200);
12285
12286 if (max_chunks < 8) {
12287 for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12288 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12289 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12290 LOG("Failed to update NVRAM %d\n", num_chunks);
12291 break;
12292 }
12293 }
12294 if (leftover) {
12295 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12296 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12297 LOG("Failed to update NVRAM with leftovers\n");
12298 }
12299 }
12300 success = 1;
12301 LOG("Successfully saved stackshot to NVRAM\n");
12302 } else {
12303 LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12304 if (pid == -1) {
12305 pid = 0;
12306 } else {
12307 LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12308 break;
12309 }
12310 }
12311 }
12312 }
12313 }
12314 }
12315
12316 if (failureStr[0]) {
12317 // append sleep-wake failure code
12318 char traceCode[80];
12319 snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12320 pmTracer->getTraceData(), pmTracer->getTracePhase());
12321 strlcat(failureStr, traceCode, sizeof(failureStr));
12322 if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12323 DLOG("Failed to write SleepWake failure string\n");
12324 }
12325 }
12326
12327 // force NVRAM sync
12328 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12329 DLOG("Failed to force nvram sync\n");
12330 }
12331
12332 skip_stackshot:
12333 if (wdogTrigger) {
12334 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12335 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12336 // If current boot is due to this watch dog trigger restart in previous boot,
12337 // then don't trigger again until at least 1 successful sleep & wake.
12338 if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12339 LOG("Shutting down due to repeated Sleep/Wake failures\n");
12340 if (!tasksSuspended) {
12341 tasksSuspended = TRUE;
12342 updateTasksSuspend();
12343 }
12344 PEHaltRestart(kPEHaltCPU);
12345 return;
12346 }
12347 }
12348 if (gSwdPanic == 0) {
12349 LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12350 if (!tasksSuspended) {
12351 tasksSuspended = TRUE;
12352 updateTasksSuspend();
12353 }
12354 PEHaltRestart(kPERestartCPU);
12355 }
12356 }
12357 if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12358 DLOG("Failed to write SleepWake failure panic key\n");
12359 }
12360 #if defined(__x86_64__)
12361 if (thread) {
12362 panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12363 } else
12364 #endif /* defined(__x86_64__) */
12365 {
12366 panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12367 }
12368 } else {
12369 gRootDomain->swd_lock = 0;
12370 return;
12371 }
12372 }
12373
12374 void
sleepWakeDebugMemAlloc()12375 IOPMrootDomain::sleepWakeDebugMemAlloc()
12376 {
12377 vm_size_t size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12378
12379 swd_hdr *hdr = NULL;
12380 void *bufPtr = NULL;
12381
12382 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12383
12384
12385 if (kIOSleepWakeWdogOff & gIOKitDebug) {
12386 return;
12387 }
12388
12389 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12390 return;
12391 }
12392
12393 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12394 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12395 size);
12396 if (memDesc == NULL) {
12397 DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12398 goto exit;
12399 }
12400
12401 bufPtr = memDesc->getBytesNoCopy();
12402
12403 // Carve out memory for zlib routines
12404 swd_zs_zmem = (vm_offset_t)bufPtr;
12405 bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12406
12407 // Carve out memory for compressed stackshots
12408 swd_compressed_buffer = bufPtr;
12409 bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12410
12411 // Remaining is used for holding stackshot
12412 hdr = (swd_hdr *)bufPtr;
12413 memset(hdr, 0, sizeof(swd_hdr));
12414
12415 hdr->signature = SWD_HDR_SIGNATURE;
12416 hdr->alloc_size = SWD_STACKSHOT_SIZE;
12417
12418 hdr->spindump_offset = sizeof(swd_hdr);
12419 swd_buffer = (void *)hdr;
12420 swd_memDesc = os::move(memDesc);
12421 DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12422
12423 exit:
12424 gRootDomain->swd_lock = 0;
12425 }
12426
12427 void
sleepWakeDebugSpinDumpMemAlloc()12428 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12429 {
12430 #if UNUSED
12431 vm_size_t size = SWD_SPINDUMP_SIZE;
12432
12433 swd_hdr *hdr = NULL;
12434
12435 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12436
12437 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12438 return;
12439 }
12440
12441 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12442 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12443 SWD_SPINDUMP_SIZE);
12444
12445 if (memDesc == NULL) {
12446 DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12447 goto exit;
12448 }
12449
12450
12451 hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12452 memset(hdr, 0, sizeof(swd_hdr));
12453
12454 hdr->signature = SWD_HDR_SIGNATURE;
12455 hdr->alloc_size = size;
12456
12457 hdr->spindump_offset = sizeof(swd_hdr);
12458 swd_spindump_buffer = (void *)hdr;
12459 swd_spindump_memDesc = os::move(memDesc);
12460
12461 exit:
12462 gRootDomain->swd_lock = 0;
12463 #endif /* UNUSED */
12464 }
12465
12466 void
sleepWakeDebugEnableWdog()12467 IOPMrootDomain::sleepWakeDebugEnableWdog()
12468 {
12469 }
12470
12471 bool
sleepWakeDebugIsWdogEnabled()12472 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12473 {
12474 return !systemBooting && !systemShutdown && !gWillShutdown;
12475 }
12476
12477 void
sleepWakeDebugSaveSpinDumpFile()12478 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12479 {
12480 swd_hdr *hdr = NULL;
12481 errno_t error = EIO;
12482
12483 if (swd_spindump_buffer && gSpinDumpBufferFull) {
12484 hdr = (swd_hdr *)swd_spindump_buffer;
12485
12486 error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12487 (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12488
12489 if (error) {
12490 return;
12491 }
12492
12493 sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12494 (char*)hdr + offsetof(swd_hdr, UUID),
12495 sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12496
12497 gSpinDumpBufferFull = false;
12498 }
12499 }
12500
12501 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12502 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12503 {
12504 struct vnode *vp = NULL;
12505 vfs_context_t ctx = vfs_context_create(vfs_context_current());
12506 kauth_cred_t cred = vfs_context_ucred(ctx);
12507 struct vnode_attr va;
12508 errno_t error = EIO;
12509
12510 if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12511 S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12512 LOG("Failed to open the file %s\n", name);
12513 swd_flags |= SWD_FILEOP_ERROR;
12514 goto exit;
12515 }
12516 VATTR_INIT(&va);
12517 VATTR_WANTED(&va, va_nlink);
12518 /* Don't dump to non-regular files or files with links. */
12519 if (vp->v_type != VREG ||
12520 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12521 LOG("Bailing as this is not a regular file\n");
12522 swd_flags |= SWD_FILEOP_ERROR;
12523 goto exit;
12524 }
12525 VATTR_INIT(&va);
12526 VATTR_SET(&va, va_data_size, 0);
12527 vnode_setattr(vp, &va, ctx);
12528
12529
12530 if (buf != NULL) {
12531 error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12532 UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12533 if (error != 0) {
12534 LOG("Failed to save sleep wake log. err 0x%x\n", error);
12535 swd_flags |= SWD_FILEOP_ERROR;
12536 } else {
12537 DLOG("Saved %d bytes to file %s\n", len, name);
12538 }
12539 }
12540
12541 exit:
12542 if (vp) {
12543 vnode_close(vp, FWRITE, ctx);
12544 }
12545 if (ctx) {
12546 vfs_context_rele(ctx);
12547 }
12548
12549 return error;
12550 }
12551
12552 #else /* defined(__i386__) || defined(__x86_64__) */
12553
12554 void
sleepWakeDebugTrig(bool restart)12555 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12556 {
12557 if (restart) {
12558 if (gSwdPanic == 0) {
12559 return;
12560 }
12561 panic("Sleep/Wake hang detected");
12562 return;
12563 }
12564 }
12565
12566 void
takeStackshot(bool restart)12567 IOPMrootDomain::takeStackshot(bool restart)
12568 {
12569 #pragma unused(restart)
12570 }
12571
12572 void
deleteStackshot()12573 IOPMrootDomain::deleteStackshot()
12574 {
12575 }
12576
12577 void
sleepWakeDebugMemAlloc()12578 IOPMrootDomain::sleepWakeDebugMemAlloc()
12579 {
12580 }
12581
12582 void
saveFailureData2File()12583 IOPMrootDomain::saveFailureData2File()
12584 {
12585 }
12586
12587 void
sleepWakeDebugEnableWdog()12588 IOPMrootDomain::sleepWakeDebugEnableWdog()
12589 {
12590 }
12591
12592 bool
sleepWakeDebugIsWdogEnabled()12593 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12594 {
12595 return false;
12596 }
12597
12598 void
sleepWakeDebugSaveSpinDumpFile()12599 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12600 {
12601 }
12602
12603 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12604 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12605 {
12606 return 0;
12607 }
12608
12609 #endif /* defined(__i386__) || defined(__x86_64__) */
12610
12611