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 * this should be treated as POD, as it's byte-copied around
759 * and we cannot rely on d'tor firing at the right time
760 */
761 struct PMAssertStruct {
762 IOPMDriverAssertionID id;
763 IOPMDriverAssertionType assertionBits;
764 uint64_t createdTime;
765 uint64_t modifiedTime;
766 const OSSymbol *ownerString;
767 IOService *ownerService;
768 uint64_t registryEntryID;
769 IOPMDriverAssertionLevel level;
770 uint64_t assertCPUStartTime;
771 uint64_t assertCPUDuration;
772 };
773
774 /*
775 * PMAssertionsTracker
776 * Tracks kernel and user space PM assertions
777 */
778 class PMAssertionsTracker : public OSObject
779 {
780 OSDeclareFinalStructors(PMAssertionsTracker);
781 public:
782 static PMAssertionsTracker *pmAssertionsTracker( IOPMrootDomain * );
783
784 IOReturn createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
785 IOReturn releaseAssertion(IOPMDriverAssertionID);
786 IOReturn setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
787 IOReturn setUserAssertionLevels(IOPMDriverAssertionType);
788
789 OSSharedPtr<OSArray> copyAssertionsArray(void);
790 IOPMDriverAssertionType getActivatedAssertions(void);
791 IOPMDriverAssertionLevel getAssertionLevel(IOPMDriverAssertionType);
792
793 IOReturn handleCreateAssertion(OSValueObject<PMAssertStruct> *);
794 IOReturn handleReleaseAssertion(IOPMDriverAssertionID);
795 IOReturn handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
796 IOReturn handleSetUserAssertionLevels(void * arg0);
797 void publishProperties(void);
798 void reportCPUBitAccounting(void);
799
800 private:
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 // read noidle setting from Device Tree
1583 if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
1584 DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
1585 }
1586
1587 queue_init(&aggressivesQueue);
1588 aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1589 aggressivesData = OSData::withCapacity(
1590 sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1591
1592 featuresDictLock = IOLockAlloc();
1593 settingsCtrlLock = IOLockAlloc();
1594 wakeEventLock = IOLockAlloc();
1595 gHaltLogLock = IOLockAlloc();
1596 setPMRootDomain(this);
1597
1598 extraSleepTimer = thread_call_allocate(
1599 idleSleepTimerExpired,
1600 (thread_call_param_t) this);
1601
1602 powerButtonDown = thread_call_allocate(
1603 powerButtonDownCallout,
1604 (thread_call_param_t) this);
1605
1606 powerButtonUp = thread_call_allocate(
1607 powerButtonUpCallout,
1608 (thread_call_param_t) this);
1609
1610 diskSyncCalloutEntry = thread_call_allocate(
1611 &disk_sync_callout,
1612 (thread_call_param_t) this);
1613 updateConsoleUsersEntry = thread_call_allocate(
1614 &updateConsoleUsersCallout,
1615 (thread_call_param_t) this);
1616
1617 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1618 fullWakeThreadCall = thread_call_allocate_with_options(
1619 OSMemberFunctionCast(thread_call_func_t, this,
1620 &IOPMrootDomain::fullWakeDelayedWork),
1621 (thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1622 THREAD_CALL_OPTIONS_ONCE);
1623 #endif
1624
1625 setProperty(kIOSleepSupportedKey, true);
1626
1627 bzero(&gPMStats, sizeof(gPMStats));
1628
1629 pmTracer = PMTraceWorker::tracer(this);
1630
1631 pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1632
1633 userDisabledAllSleep = false;
1634 systemBooting = true;
1635 idleSleepEnabled = false;
1636 sleepSlider = 0;
1637 idleSleepTimerPending = false;
1638 wrangler = NULL;
1639 clamshellClosed = false;
1640 clamshellExists = false;
1641 #if DISPLAY_WRANGLER_PRESENT
1642 clamshellDisabled = true;
1643 #else
1644 clamshellDisabled = false;
1645 #endif
1646 clamshellIgnoreClose = false;
1647 acAdaptorConnected = true;
1648 clamshellSleepDisableMask = 0;
1649 gWakeReasonString[0] = '\0';
1650
1651 // Initialize to user active.
1652 // Will never transition to user inactive w/o wrangler.
1653 fullWakeReason = kFullWakeReasonLocalUser;
1654 userIsActive = userWasActive = true;
1655 clock_get_uptime(&gUserActiveAbsTime);
1656 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1657
1658 // Set the default system capabilities at boot.
1659 _currentCapability = kIOPMSystemCapabilityCPU |
1660 kIOPMSystemCapabilityGraphics |
1661 kIOPMSystemCapabilityAudio |
1662 kIOPMSystemCapabilityNetwork;
1663
1664 _pendingCapability = _currentCapability;
1665 _desiredCapability = _currentCapability;
1666 _highestCapability = _currentCapability;
1667 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1668
1669 queuedSleepWakeUUIDString = NULL;
1670 initializeBootSessionUUID();
1671 pmStatsAppResponses = OSArray::withCapacity(5);
1672 _statsNameKey = OSSymbol::withCString(kIOPMStatsNameKey);
1673 _statsPIDKey = OSSymbol::withCString(kIOPMStatsPIDKey);
1674 _statsTimeMSKey = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1675 _statsResponseTypeKey = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1676 _statsMessageTypeKey = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1677 _statsPowerCapsKey = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1678 assertOnWakeSecs = -1;// Invalid value to prevent updates
1679
1680 pmStatsLock = IOLockAlloc();
1681 idxPMCPUClamshell = kCPUUnknownIndex;
1682 idxPMCPULimitedPower = kCPUUnknownIndex;
1683
1684 tmpDict = OSDictionary::withCapacity(1);
1685 setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1686
1687 // Set a default "SystemPowerProfileOverrideDict" for platform
1688 // drivers without any overrides.
1689 if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1690 tmpDict = OSDictionary::withCapacity(1);
1691 setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1692 }
1693
1694 settingsCallbacks = OSDictionary::withCapacity(1);
1695
1696 // Create a list of the valid PM settings that we'll relay to
1697 // interested clients in setProperties() => setPMSetting()
1698 allowedPMSettings = OSArray::withObjects(
1699 (const OSObject **)settingsArr,
1700 kRootDomainSettingsCount,
1701 0);
1702
1703 // List of PM settings that should not automatically publish itself
1704 // as a feature when registered by a listener.
1705 noPublishPMSettings = OSArray::withObjects(
1706 (const OSObject **)noPublishSettingsArr,
1707 kRootDomainNoPublishSettingsCount,
1708 0);
1709
1710 fPMSettingsDict = OSDictionary::withCapacity(5);
1711 preventIdleSleepList = OSSet::withCapacity(8);
1712 preventSystemSleepList = OSSet::withCapacity(2);
1713
1714 PMinit(); // creates gIOPMWorkLoop
1715 gIOPMWorkLoop = getIOPMWorkloop();
1716
1717 // Create IOPMPowerStateQueue used to queue external power
1718 // events, and to handle those events on the PM work loop.
1719 pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1720 this, OSMemberFunctionCast(IOEventSource::Action, this,
1721 &IOPMrootDomain::dispatchPowerEvent));
1722 gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1723
1724 _aotMode = 0;
1725 _aotTimerES = IOTimerEventSource::timerEventSource(this,
1726 OSMemberFunctionCast(IOTimerEventSource::Action,
1727 this, &IOPMrootDomain::aotEvaluate));
1728 gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1729
1730 // create our power parent
1731 gPatriarch = new IORootParent;
1732 gPatriarch->init();
1733 gPatriarch->attach(this);
1734 gPatriarch->start(this);
1735 gPatriarch->addPowerChild(this);
1736
1737 registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1738 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1739
1740 // install power change handler
1741 gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1742
1743 #if DISPLAY_WRANGLER_PRESENT
1744 wranglerIdleSettings = OSDictionary::withCapacity(1);
1745 OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1746
1747 if (wranglerIdleSettings && wranglerIdlePeriod) {
1748 wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1749 wranglerIdlePeriod.get());
1750 }
1751
1752 #endif /* DISPLAY_WRANGLER_PRESENT */
1753
1754 lowLatencyAudioNotifierDict = OSDictionary::withCapacity(2);
1755 lowLatencyAudioNotifyStateSym = OSSymbol::withCString("LowLatencyAudioNotifyState");
1756 lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1757 lowLatencyAudioNotifyStateVal = OSNumber::withNumber(0ull, 32);
1758 lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1759
1760 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1761 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1762 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1763 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1764 }
1765
1766 OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1767 setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1768
1769 // IOBacklightDisplay can take a long time to load at boot, or it may
1770 // not load at all if you're booting with clamshell closed. We publish
1771 // 'DisplayDims' here redundantly to get it published early and at all.
1772 OSSharedPtr<OSDictionary> matching;
1773 matching = serviceMatching("IOPMPowerSource");
1774 psIterator = getMatchingServices(matching.get());
1775
1776 if (psIterator && psIterator->getNextObject()) {
1777 // There's at least one battery on the system, so we publish
1778 // 'DisplayDims' support for the LCD.
1779 publishFeature("DisplayDims");
1780 }
1781
1782 // read swd_panic boot-arg
1783 PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1784 gWillShutdownSysctlRegistered = true;
1785
1786 #if HIBERNATION
1787 #if defined(__arm64__)
1788 #endif /* defined(__arm64__) */
1789 IOHibernateSystemInit(this);
1790 #endif
1791
1792 registerService(); // let clients find us
1793
1794 return true;
1795 }
1796
1797 //******************************************************************************
1798 // setProperties
1799 //
1800 // Receive a setProperty call
1801 // The "System Boot" property means the system is completely booted.
1802 //******************************************************************************
1803
1804 IOReturn
setProperties(OSObject * props_obj)1805 IOPMrootDomain::setProperties( OSObject * props_obj )
1806 {
1807 IOReturn return_value = kIOReturnSuccess;
1808 OSDictionary *dict = OSDynamicCast(OSDictionary, props_obj);
1809 OSBoolean *b = NULL;
1810 OSNumber *n = NULL;
1811 const OSSymbol *key = NULL;
1812 OSObject *obj = NULL;
1813 OSSharedPtr<OSCollectionIterator> iter;
1814
1815 if (!dict) {
1816 return kIOReturnBadArgument;
1817 }
1818
1819 bool clientEntitled = false;
1820 {
1821 OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1822 clientEntitled = (obj == kOSBooleanTrue);
1823 }
1824
1825 if (!clientEntitled) {
1826 const char * errorSuffix = NULL;
1827
1828 // IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1829 // That API can set 6 possible keys that are checked below.
1830 if ((dict->getCount() == 1) &&
1831 (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1832 dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1833 dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1834 dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1835 dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1836 dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1837 return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1838 if (return_value != kIOReturnSuccess) {
1839 errorSuffix = "privileged";
1840 }
1841 } else {
1842 return_value = kIOReturnNotPermitted;
1843 errorSuffix = "entitled";
1844 }
1845
1846 if (return_value != kIOReturnSuccess) {
1847 OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1848 DLOG("%s failed, process %s is not %s\n", __func__,
1849 procName ? procName->getCStringNoCopy() : "", errorSuffix);
1850 return return_value;
1851 }
1852 }
1853
1854 OSSharedPtr<const OSSymbol> publish_simulated_battery_string = OSSymbol::withCString("SoftwareSimulatedBatteries");
1855 OSSharedPtr<const OSSymbol> boot_complete_string = OSSymbol::withCString("System Boot Complete");
1856 OSSharedPtr<const OSSymbol> sys_shutdown_string = OSSymbol::withCString("System Shutdown");
1857 OSSharedPtr<const OSSymbol> stall_halt_string = OSSymbol::withCString("StallSystemAtHalt");
1858 OSSharedPtr<const OSSymbol> battery_warning_disabled_string = OSSymbol::withCString("BatteryWarningsDisabled");
1859 OSSharedPtr<const OSSymbol> idle_seconds_string = OSSymbol::withCString("System Idle Seconds");
1860 OSSharedPtr<const OSSymbol> idle_milliseconds_string = OSSymbol::withCString("System Idle Milliseconds");
1861 OSSharedPtr<const OSSymbol> sleepdisabled_string = OSSymbol::withCString("SleepDisabled");
1862 OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1863 OSSharedPtr<const OSSymbol> loginwindow_progress_string = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1864 OSSharedPtr<const OSSymbol> coredisplay_progress_string = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1865 OSSharedPtr<const OSSymbol> coregraphics_progress_string = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1866 #if DEBUG || DEVELOPMENT
1867 OSSharedPtr<const OSSymbol> clamshell_close_string = OSSymbol::withCString("IOPMTestClamshellClose");
1868 OSSharedPtr<const OSSymbol> clamshell_open_string = OSSymbol::withCString("IOPMTestClamshellOpen");
1869 OSSharedPtr<const OSSymbol> ac_detach_string = OSSymbol::withCString("IOPMTestACDetach");
1870 OSSharedPtr<const OSSymbol> ac_attach_string = OSSymbol::withCString("IOPMTestACAttach");
1871 OSSharedPtr<const OSSymbol> desktopmode_set_string = OSSymbol::withCString("IOPMTestDesktopModeSet");
1872 OSSharedPtr<const OSSymbol> desktopmode_remove_string = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1873 #endif
1874
1875 #if HIBERNATION
1876 OSSharedPtr<const OSSymbol> hibernatemode_string = OSSymbol::withCString(kIOHibernateModeKey);
1877 OSSharedPtr<const OSSymbol> hibernatefile_string = OSSymbol::withCString(kIOHibernateFileKey);
1878 OSSharedPtr<const OSSymbol> hibernatefilemin_string = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1879 OSSharedPtr<const OSSymbol> hibernatefilemax_string = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1880 OSSharedPtr<const OSSymbol> hibernatefreeratio_string = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1881 OSSharedPtr<const OSSymbol> hibernatefreetime_string = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1882 #endif
1883
1884 iter = OSCollectionIterator::withCollection(dict);
1885 if (!iter) {
1886 return_value = kIOReturnNoMemory;
1887 goto exit;
1888 }
1889
1890 while ((key = (const OSSymbol *) iter->getNextObject()) &&
1891 (obj = dict->getObject(key))) {
1892 if (key->isEqualTo(publish_simulated_battery_string.get())) {
1893 if (OSDynamicCast(OSBoolean, obj)) {
1894 publishResource(key, kOSBooleanTrue);
1895 }
1896 } else if (key->isEqualTo(idle_seconds_string.get())) {
1897 if ((n = OSDynamicCast(OSNumber, obj))) {
1898 setProperty(key, n);
1899 idleMilliSeconds = n->unsigned32BitValue() * 1000;
1900 }
1901 } else if (key->isEqualTo(idle_milliseconds_string.get())) {
1902 if ((n = OSDynamicCast(OSNumber, obj))) {
1903 setProperty(key, n);
1904 idleMilliSeconds = n->unsigned32BitValue();
1905 }
1906 } else if (key->isEqualTo(boot_complete_string.get())) {
1907 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1908 } else if (key->isEqualTo(sys_shutdown_string.get())) {
1909 if ((b = OSDynamicCast(OSBoolean, obj))) {
1910 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1911 }
1912 } else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1913 setProperty(key, obj);
1914 }
1915 #if HIBERNATION
1916 else if (key->isEqualTo(hibernatemode_string.get()) ||
1917 key->isEqualTo(hibernatefilemin_string.get()) ||
1918 key->isEqualTo(hibernatefilemax_string.get()) ||
1919 key->isEqualTo(hibernatefreeratio_string.get()) ||
1920 key->isEqualTo(hibernatefreetime_string.get())) {
1921 if ((n = OSDynamicCast(OSNumber, obj))) {
1922 setProperty(key, n);
1923 }
1924 } else if (key->isEqualTo(hibernatefile_string.get())) {
1925 OSString * str = OSDynamicCast(OSString, obj);
1926 if (str) {
1927 setProperty(key, str);
1928 }
1929 }
1930 #endif
1931 else if (key->isEqualTo(sleepdisabled_string.get())) {
1932 if ((b = OSDynamicCast(OSBoolean, obj))) {
1933 setProperty(key, b);
1934 pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1935 }
1936 } else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1937 obj->retain();
1938 pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1939 } else if (key->isEqualTo(loginwindow_progress_string.get())) {
1940 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1941 uint32_t data = n->unsigned32BitValue();
1942 pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1943 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1944 }
1945 } else if (key->isEqualTo(coredisplay_progress_string.get())) {
1946 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1947 uint32_t data = n->unsigned32BitValue();
1948 pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1949 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1950 }
1951 } else if (key->isEqualTo(coregraphics_progress_string.get())) {
1952 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1953 uint32_t data = n->unsigned32BitValue();
1954 pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1955 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1956 }
1957 } else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1958 key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1959 key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1960 key->isEqualTo(stall_halt_string.get())) {
1961 if ((b = OSDynamicCast(OSBoolean, obj))) {
1962 setProperty(key, b);
1963 }
1964 } else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1965 key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1966 key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1967 key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1968 if ((n = OSDynamicCast(OSNumber, obj))) {
1969 setProperty(key, n);
1970 }
1971 } else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1972 if (kOSBooleanTrue == obj) {
1973 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1974 } else {
1975 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1976 }
1977 DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
1978 }
1979 #if DEBUG || DEVELOPMENT
1980 else if (key->isEqualTo(clamshell_close_string.get())) {
1981 DLOG("SetProperties: setting clamshell close\n");
1982 UInt32 msg = kIOPMClamshellClosed;
1983 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1984 } else if (key->isEqualTo(clamshell_open_string.get())) {
1985 DLOG("SetProperties: setting clamshell open\n");
1986 UInt32 msg = kIOPMClamshellOpened;
1987 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1988 } else if (key->isEqualTo(ac_detach_string.get())) {
1989 DLOG("SetProperties: setting ac detach\n");
1990 UInt32 msg = kIOPMSetACAdaptorConnected;
1991 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1992 } else if (key->isEqualTo(ac_attach_string.get())) {
1993 DLOG("SetProperties: setting ac attach\n");
1994 UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
1995 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
1996 } else if (key->isEqualTo(desktopmode_set_string.get())) {
1997 DLOG("SetProperties: setting desktopmode");
1998 UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
1999 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2000 } else if (key->isEqualTo(desktopmode_remove_string.get())) {
2001 DLOG("SetProperties: removing desktopmode\n");
2002 UInt32 msg = kIOPMSetDesktopMode;
2003 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2004 }
2005 #endif
2006 // Relay our allowed PM settings onto our registered PM clients
2007 else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2008 return_value = setPMSetting(key, obj);
2009 if (kIOReturnSuccess != return_value) {
2010 break;
2011 }
2012 } else {
2013 DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2014 }
2015 }
2016
2017 exit:
2018 return return_value;
2019 }
2020
2021 // MARK: -
2022 // MARK: Aggressiveness
2023
2024 //******************************************************************************
2025 // setAggressiveness
2026 //
2027 // Override IOService::setAggressiveness()
2028 //******************************************************************************
2029
2030 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2031 IOPMrootDomain::setAggressiveness(
2032 unsigned long type,
2033 unsigned long value )
2034 {
2035 return setAggressiveness( type, value, 0 );
2036 }
2037
2038 /*
2039 * Private setAggressiveness() with an internal options argument.
2040 */
2041 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2042 IOPMrootDomain::setAggressiveness(
2043 unsigned long type,
2044 unsigned long value,
2045 IOOptionBits options )
2046 {
2047 AggressivesRequest * entry;
2048 AggressivesRequest * request;
2049 bool found = false;
2050
2051 if ((type > UINT_MAX) || (value > UINT_MAX)) {
2052 return kIOReturnBadArgument;
2053 }
2054
2055 if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2056 DLOG("setAggressiveness(%x) %s = %u\n",
2057 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2058 } else {
2059 DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2060 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2061 }
2062
2063 request = IOMallocType(AggressivesRequest);
2064 request->options = options;
2065 request->dataType = kAggressivesRequestTypeRecord;
2066 request->data.record.type = (uint32_t) type;
2067 request->data.record.value = (uint32_t) value;
2068
2069 AGGRESSIVES_LOCK();
2070
2071 // Update disk quick spindown flag used by getAggressiveness().
2072 // Never merge requests with quick spindown flags set.
2073
2074 if (options & kAggressivesOptionQuickSpindownEnable) {
2075 gAggressivesState |= kAggressivesStateQuickSpindown;
2076 } else if (options & kAggressivesOptionQuickSpindownDisable) {
2077 gAggressivesState &= ~kAggressivesStateQuickSpindown;
2078 } else {
2079 // Coalesce requests with identical aggressives types.
2080 // Deal with callers that calls us too "aggressively".
2081
2082 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2083 {
2084 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2085 (entry->data.record.type == type) &&
2086 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2087 entry->data.record.value = (uint32_t) value;
2088 found = true;
2089 break;
2090 }
2091 }
2092 }
2093
2094 if (!found) {
2095 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2096 }
2097
2098 AGGRESSIVES_UNLOCK();
2099
2100 if (found) {
2101 IOFreeType(request, AggressivesRequest);
2102 }
2103
2104 if (options & kAggressivesOptionSynchronous) {
2105 handleAggressivesRequests(); // not truly synchronous
2106 } else {
2107 thread_call_enter(aggressivesThreadCall);
2108 }
2109
2110 return kIOReturnSuccess;
2111 }
2112
2113 //******************************************************************************
2114 // getAggressiveness
2115 //
2116 // Override IOService::setAggressiveness()
2117 // Fetch the aggressiveness factor with the given type.
2118 //******************************************************************************
2119
2120 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2121 IOPMrootDomain::getAggressiveness(
2122 unsigned long type,
2123 unsigned long * outLevel )
2124 {
2125 uint32_t value = 0;
2126 int source = 0;
2127
2128 if (!outLevel || (type > UINT_MAX)) {
2129 return kIOReturnBadArgument;
2130 }
2131
2132 AGGRESSIVES_LOCK();
2133
2134 // Disk quick spindown in effect, report value = 1
2135
2136 if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2137 (type == kPMMinutesToSpinDown)) {
2138 value = kAggressivesMinValue;
2139 source = 1;
2140 }
2141
2142 // Consult the pending request queue.
2143
2144 if (!source) {
2145 AggressivesRequest * entry;
2146
2147 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2148 {
2149 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2150 (entry->data.record.type == type) &&
2151 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2152 value = entry->data.record.value;
2153 source = 2;
2154 break;
2155 }
2156 }
2157 }
2158
2159 // Consult the backend records.
2160
2161 if (!source && aggressivesData) {
2162 AggressivesRecord * record;
2163 int i, count;
2164
2165 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2166 record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2167
2168 for (i = 0; i < count; i++, record++) {
2169 if (record->type == type) {
2170 value = record->value;
2171 source = 3;
2172 break;
2173 }
2174 }
2175 }
2176
2177 AGGRESSIVES_UNLOCK();
2178
2179 if (source) {
2180 *outLevel = (unsigned long) value;
2181 return kIOReturnSuccess;
2182 } else {
2183 DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2184 *outLevel = 0; // default return = 0, driver may not check for error
2185 return kIOReturnInvalid;
2186 }
2187 }
2188
2189 //******************************************************************************
2190 // joinAggressiveness
2191 //
2192 // Request from IOService to join future aggressiveness broadcasts.
2193 //******************************************************************************
2194
2195 IOReturn
joinAggressiveness(IOService * service)2196 IOPMrootDomain::joinAggressiveness(
2197 IOService * service )
2198 {
2199 AggressivesRequest * request;
2200
2201 if (!service || (service == this)) {
2202 return kIOReturnBadArgument;
2203 }
2204
2205 DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2206
2207 request = IOMallocType(AggressivesRequest);
2208 request->dataType = kAggressivesRequestTypeService;
2209 request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2210
2211 AGGRESSIVES_LOCK();
2212 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2213 AGGRESSIVES_UNLOCK();
2214
2215 thread_call_enter(aggressivesThreadCall);
2216
2217 return kIOReturnSuccess;
2218 }
2219
2220 //******************************************************************************
2221 // handleAggressivesRequests
2222 //
2223 // Backend thread processes all incoming aggressiveness requests in the queue.
2224 //******************************************************************************
2225
2226 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2227 handleAggressivesFunction(
2228 thread_call_param_t param1,
2229 thread_call_param_t param2 )
2230 {
2231 if (param1) {
2232 ((IOPMrootDomain *) param1)->handleAggressivesRequests();
2233 }
2234 }
2235
2236 void
handleAggressivesRequests(void)2237 IOPMrootDomain::handleAggressivesRequests( void )
2238 {
2239 AggressivesRecord * start;
2240 AggressivesRecord * record;
2241 AggressivesRequest * request;
2242 queue_head_t joinedQueue;
2243 int i, count;
2244 bool broadcast;
2245 bool found;
2246 bool pingSelf = false;
2247
2248 AGGRESSIVES_LOCK();
2249
2250 if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2251 queue_empty(&aggressivesQueue)) {
2252 goto unlock_done;
2253 }
2254
2255 gAggressivesState |= kAggressivesStateBusy;
2256 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2257 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2258
2259 do{
2260 broadcast = false;
2261 queue_init(&joinedQueue);
2262
2263 do{
2264 // Remove request from the incoming queue in FIFO order.
2265 queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2266 switch (request->dataType) {
2267 case kAggressivesRequestTypeRecord:
2268 // Update existing record if found.
2269 found = false;
2270 for (i = 0, record = start; i < count; i++, record++) {
2271 if (record->type == request->data.record.type) {
2272 found = true;
2273
2274 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2275 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2276 broadcast = true;
2277 record->flags |= (kAggressivesRecordFlagMinValue |
2278 kAggressivesRecordFlagModified);
2279 DLOG("disk spindown accelerated, was %u min\n",
2280 record->value);
2281 }
2282 } else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2283 if (record->flags & kAggressivesRecordFlagMinValue) {
2284 broadcast = true;
2285 record->flags |= kAggressivesRecordFlagModified;
2286 record->flags &= ~kAggressivesRecordFlagMinValue;
2287 DLOG("disk spindown restored to %u min\n",
2288 record->value);
2289 }
2290 } else if (record->value != request->data.record.value) {
2291 record->value = request->data.record.value;
2292 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2293 broadcast = true;
2294 record->flags |= kAggressivesRecordFlagModified;
2295 }
2296 }
2297 break;
2298 }
2299 }
2300
2301 // No matching record, append a new record.
2302 if (!found &&
2303 ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2304 AggressivesRecord newRecord;
2305
2306 newRecord.flags = kAggressivesRecordFlagModified;
2307 newRecord.type = request->data.record.type;
2308 newRecord.value = request->data.record.value;
2309 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2310 newRecord.flags |= kAggressivesRecordFlagMinValue;
2311 DLOG("disk spindown accelerated\n");
2312 }
2313
2314 aggressivesData->appendValue(newRecord);
2315
2316 // OSData may have switched to another (larger) buffer.
2317 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2318 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2319 broadcast = true;
2320 }
2321
2322 // Finished processing the request, release it.
2323 IOFreeType(request, AggressivesRequest);
2324 break;
2325
2326 case kAggressivesRequestTypeService:
2327 // synchronizeAggressives() will free request.
2328 queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2329 break;
2330
2331 default:
2332 panic("bad aggressives request type %x", request->dataType);
2333 break;
2334 }
2335 } while (!queue_empty(&aggressivesQueue));
2336
2337 // Release the lock to perform work, with busy flag set.
2338 if (!queue_empty(&joinedQueue) || broadcast) {
2339 AGGRESSIVES_UNLOCK();
2340 if (!queue_empty(&joinedQueue)) {
2341 synchronizeAggressives(&joinedQueue, start, count);
2342 }
2343 if (broadcast) {
2344 broadcastAggressives(start, count);
2345 }
2346 AGGRESSIVES_LOCK();
2347 }
2348
2349 // Remove the modified flag from all records.
2350 for (i = 0, record = start; i < count; i++, record++) {
2351 if ((record->flags & kAggressivesRecordFlagModified) &&
2352 ((record->type == kPMMinutesToDim) ||
2353 (record->type == kPMMinutesToSleep))) {
2354 pingSelf = true;
2355 }
2356
2357 record->flags &= ~kAggressivesRecordFlagModified;
2358 }
2359
2360 // Check the incoming queue again since new entries may have been
2361 // added while lock was released above.
2362 } while (!queue_empty(&aggressivesQueue));
2363
2364 gAggressivesState &= ~kAggressivesStateBusy;
2365
2366 unlock_done:
2367 AGGRESSIVES_UNLOCK();
2368
2369 // Root domain is interested in system and display sleep slider changes.
2370 // Submit a power event to handle those changes on the PM work loop.
2371
2372 if (pingSelf && pmPowerStateQueue) {
2373 pmPowerStateQueue->submitPowerEvent(
2374 kPowerEventPolicyStimulus,
2375 (void *) kStimulusAggressivenessChanged );
2376 }
2377 }
2378
2379 //******************************************************************************
2380 // synchronizeAggressives
2381 //
2382 // Push all known aggressiveness records to one or more IOService.
2383 //******************************************************************************
2384
2385 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2386 IOPMrootDomain::synchronizeAggressives(
2387 queue_head_t * joinedQueue,
2388 const AggressivesRecord * array,
2389 int count )
2390 {
2391 OSSharedPtr<IOService> service;
2392 AggressivesRequest * request;
2393 const AggressivesRecord * record;
2394 IOPMDriverCallEntry callEntry;
2395 uint32_t value;
2396 int i;
2397
2398 while (!queue_empty(joinedQueue)) {
2399 queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2400 if (request->dataType == kAggressivesRequestTypeService) {
2401 // retained by joinAggressiveness(), so take ownership
2402 service = os::move(request->data.service);
2403 } else {
2404 service.reset();
2405 }
2406
2407 IOFreeType(request, AggressivesRequest);
2408 request = NULL;
2409
2410 if (service) {
2411 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2412 for (i = 0, record = array; i < count; i++, record++) {
2413 value = record->value;
2414 if (record->flags & kAggressivesRecordFlagMinValue) {
2415 value = kAggressivesMinValue;
2416 }
2417
2418 _LOG("synchronizeAggressives 0x%x = %u to %s\n",
2419 record->type, value, service->getName());
2420 service->setAggressiveness(record->type, value);
2421 }
2422 service->deassertPMDriverCall(&callEntry);
2423 }
2424 }
2425 }
2426 }
2427
2428 //******************************************************************************
2429 // broadcastAggressives
2430 //
2431 // Traverse PM tree and call setAggressiveness() for records that have changed.
2432 //******************************************************************************
2433
2434 void
broadcastAggressives(const AggressivesRecord * array,int count)2435 IOPMrootDomain::broadcastAggressives(
2436 const AggressivesRecord * array,
2437 int count )
2438 {
2439 OSSharedPtr<IORegistryIterator> iter;
2440 IORegistryEntry *entry;
2441 OSSharedPtr<IORegistryEntry> child;
2442 IOPowerConnection *connect;
2443 IOService *service;
2444 const AggressivesRecord *record;
2445 IOPMDriverCallEntry callEntry;
2446 uint32_t value;
2447 int i;
2448
2449 iter = IORegistryIterator::iterateOver(
2450 this, gIOPowerPlane, kIORegistryIterateRecursively);
2451 if (iter) {
2452 do{
2453 // !! reset the iterator
2454 iter->reset();
2455 while ((entry = iter->getNextObject())) {
2456 connect = OSDynamicCast(IOPowerConnection, entry);
2457 if (!connect || !connect->getReadyFlag()) {
2458 continue;
2459 }
2460
2461 child = connect->copyChildEntry(gIOPowerPlane);
2462 if (child) {
2463 if ((service = OSDynamicCast(IOService, child.get()))) {
2464 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2465 for (i = 0, record = array; i < count; i++, record++) {
2466 if (record->flags & kAggressivesRecordFlagModified) {
2467 value = record->value;
2468 if (record->flags & kAggressivesRecordFlagMinValue) {
2469 value = kAggressivesMinValue;
2470 }
2471 _LOG("broadcastAggressives %x = %u to %s\n",
2472 record->type, value, service->getName());
2473 service->setAggressiveness(record->type, value);
2474 }
2475 }
2476 service->deassertPMDriverCall(&callEntry);
2477 }
2478 }
2479 }
2480 }
2481 }while (!entry && !iter->isValid());
2482 }
2483 }
2484
2485 //*****************************************
2486 // stackshot on power button press
2487 // ***************************************
2488 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2489 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2490 {
2491 /* Power button pressed during wake
2492 * Take a stackshot
2493 */
2494 DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2495 ((IOPMrootDomain *)us)->takeStackshot(false);
2496 }
2497
2498 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2499 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2500 {
2501 /* Power button released.
2502 * Delete any stackshot data
2503 */
2504 DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2505 ((IOPMrootDomain *)us)->deleteStackshot();
2506 }
2507 //*************************************************************************
2508 //
2509
2510 // MARK: -
2511 // MARK: System Sleep
2512
2513 //******************************************************************************
2514 // startIdleSleepTimer
2515 //
2516 //******************************************************************************
2517
2518 void
startIdleSleepTimer(uint32_t inMilliSeconds)2519 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2520 {
2521 AbsoluteTime deadline;
2522
2523 ASSERT_GATED();
2524 if (gNoIdleFlag) {
2525 DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2526 return;
2527 }
2528 if (inMilliSeconds) {
2529 clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2530 thread_call_enter_delayed(extraSleepTimer, deadline);
2531 idleSleepTimerPending = true;
2532 } else {
2533 thread_call_enter(extraSleepTimer);
2534 }
2535 DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2536 }
2537
2538 //******************************************************************************
2539 // cancelIdleSleepTimer
2540 //
2541 //******************************************************************************
2542
2543 void
cancelIdleSleepTimer(void)2544 IOPMrootDomain::cancelIdleSleepTimer( void )
2545 {
2546 ASSERT_GATED();
2547 if (idleSleepTimerPending) {
2548 DLOG("idle timer cancelled\n");
2549 thread_call_cancel(extraSleepTimer);
2550 idleSleepTimerPending = false;
2551
2552 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2553 AbsoluteTime now;
2554 clock_usec_t microsecs;
2555 clock_get_uptime(&now);
2556 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2557 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
2558 if (assertOnWakeReport) {
2559 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2560 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2561 }
2562 }
2563 }
2564 }
2565
2566 //******************************************************************************
2567 // idleSleepTimerExpired
2568 //
2569 //******************************************************************************
2570
2571 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2572 idleSleepTimerExpired(
2573 thread_call_param_t us, thread_call_param_t )
2574 {
2575 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2576 }
2577
2578 //******************************************************************************
2579 // handleSleepTimerExpiration
2580 //
2581 // The time between the sleep idle timeout and the next longest one has elapsed.
2582 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2583 //******************************************************************************
2584
2585 void
handleSleepTimerExpiration(void)2586 IOPMrootDomain::handleSleepTimerExpiration( void )
2587 {
2588 if (!gIOPMWorkLoop->inGate()) {
2589 gIOPMWorkLoop->runAction(
2590 OSMemberFunctionCast(IOWorkLoop::Action, this,
2591 &IOPMrootDomain::handleSleepTimerExpiration),
2592 this);
2593 return;
2594 }
2595
2596 DLOG("sleep timer expired\n");
2597 ASSERT_GATED();
2598
2599 idleSleepTimerPending = false;
2600 setQuickSpinDownTimeout();
2601 adjustPowerState(true);
2602 }
2603
2604 //******************************************************************************
2605 // getTimeToIdleSleep
2606 //
2607 // Returns number of milliseconds left before going into idle sleep.
2608 // Caller has to make sure that idle sleep is allowed at the time of calling
2609 // this function
2610 //******************************************************************************
2611
2612 uint32_t
getTimeToIdleSleep(void)2613 IOPMrootDomain::getTimeToIdleSleep( void )
2614 {
2615 AbsoluteTime now, lastActivityTime;
2616 uint64_t nanos;
2617 uint32_t minutesSinceUserInactive = 0;
2618 uint32_t sleepDelay = 0;
2619
2620 if (!idleSleepEnabled) {
2621 return 0xffffffff;
2622 }
2623
2624 if (userActivityTime) {
2625 lastActivityTime = userActivityTime;
2626 } else {
2627 lastActivityTime = userBecameInactiveTime;
2628 }
2629
2630 // Ignore any lastActivityTime that predates the last system wake.
2631 // The goal is to avoid a sudden idle sleep right after a dark wake
2632 // due to sleepDelay=0 computed below. The alternative 60s minimum
2633 // timeout should be large enough to allow dark wake to complete,
2634 // at which point the idle timer will be promptly cancelled.
2635 clock_get_uptime(&now);
2636 if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2637 (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2638 SUB_ABSOLUTETIME(&now, &lastActivityTime);
2639 absolutetime_to_nanoseconds(now, &nanos);
2640 minutesSinceUserInactive = nanos / (60000000000ULL);
2641
2642 if (minutesSinceUserInactive >= sleepSlider) {
2643 sleepDelay = 0;
2644 } else {
2645 sleepDelay = sleepSlider - minutesSinceUserInactive;
2646 }
2647 } else {
2648 DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2649 lastActivityTime, now, gIOLastWakeAbsTime);
2650 sleepDelay = sleepSlider;
2651 }
2652
2653 DLOG("user inactive %u min, time to idle sleep %u min\n",
2654 minutesSinceUserInactive, sleepDelay);
2655
2656 return sleepDelay * 60 * 1000;
2657 }
2658
2659 //******************************************************************************
2660 // setQuickSpinDownTimeout
2661 //
2662 //******************************************************************************
2663
2664 void
setQuickSpinDownTimeout(void)2665 IOPMrootDomain::setQuickSpinDownTimeout( void )
2666 {
2667 ASSERT_GATED();
2668 setAggressiveness(
2669 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2670 }
2671
2672 //******************************************************************************
2673 // restoreUserSpinDownTimeout
2674 //
2675 //******************************************************************************
2676
2677 void
restoreUserSpinDownTimeout(void)2678 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2679 {
2680 ASSERT_GATED();
2681 setAggressiveness(
2682 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2683 }
2684
2685 //******************************************************************************
2686 // sleepSystem
2687 //
2688 //******************************************************************************
2689
2690 /* public */
2691 IOReturn
sleepSystem(void)2692 IOPMrootDomain::sleepSystem( void )
2693 {
2694 return sleepSystemOptions(NULL);
2695 }
2696
2697 /* private */
2698 IOReturn
sleepSystemOptions(OSDictionary * options)2699 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2700 {
2701 OSObject *obj = NULL;
2702 OSString *reason = NULL;
2703 /* sleepSystem is a public function, and may be called by any kernel driver.
2704 * And that's bad - drivers should sleep the system by calling
2705 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2706 *
2707 * Note that user space app calls to IOPMSleepSystem() will also travel
2708 * this code path and thus be correctly identified as software sleeps.
2709 */
2710
2711 if (options && options->getObject("OSSwitch")) {
2712 // Log specific sleep cause for OS Switch hibernation
2713 return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2714 }
2715
2716 if (options && (obj = options->getObject("Sleep Reason"))) {
2717 reason = OSDynamicCast(OSString, obj);
2718 if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2719 return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2720 }
2721 if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2722 return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2723 }
2724 }
2725
2726 return privateSleepSystem( kIOPMSleepReasonSoftware);
2727 }
2728
2729 /* private */
2730 IOReturn
privateSleepSystem(uint32_t sleepReason)2731 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2732 {
2733 /* Called from both gated and non-gated context */
2734
2735 if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2736 return kIOReturnNotPermitted;
2737 }
2738
2739 pmPowerStateQueue->submitPowerEvent(
2740 kPowerEventPolicyStimulus,
2741 (void *) kStimulusDemandSystemSleep,
2742 sleepReason);
2743
2744 return kIOReturnSuccess;
2745 }
2746
2747 //******************************************************************************
2748 // powerChangeDone
2749 //
2750 // This overrides powerChangeDone in IOService.
2751 //******************************************************************************
2752 void
powerChangeDone(unsigned long previousPowerState)2753 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2754 {
2755 #if !__i386__ && !__x86_64__
2756 uint64_t timeSinceReset = 0;
2757 #endif
2758 uint64_t now;
2759 unsigned long newState;
2760 clock_sec_t secs;
2761 clock_usec_t microsecs;
2762 uint32_t lastDebugWakeSeconds;
2763 clock_sec_t adjWakeTime;
2764 IOPMCalendarStruct nowCalendar;
2765
2766 ASSERT_GATED();
2767 newState = getPowerState();
2768 DLOG("PowerChangeDone: %s->%s\n",
2769 getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2770
2771 if (previousPowerState == newState) {
2772 return;
2773 }
2774
2775 notifierThread = current_thread();
2776 switch (getPowerState()) {
2777 case SLEEP_STATE: {
2778 if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2779 secs = 0;
2780 microsecs = 0;
2781 PEGetUTCTimeOfDay(&secs, µsecs);
2782
2783 adjWakeTime = 0;
2784 if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2785 IOLog("use _calendarWakeAlarmUTC\n");
2786 adjWakeTime = _calendarWakeAlarmUTC;
2787 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2788 IOLog("accelerate _aotWakeTime for exit\n");
2789 adjWakeTime = secs;
2790 } else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2791 IOLog("accelerate _aotWakeTime for assertion\n");
2792 adjWakeTime = secs;
2793 }
2794 if (adjWakeTime) {
2795 IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2796 }
2797
2798 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2799 IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2800
2801 IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2802 assert(kIOReturnSuccess == ret);
2803 }
2804 if (_aotLastWakeTime) {
2805 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2806 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2807 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2808 gWakeReasonString,
2809 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2810 }
2811 }
2812 _aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2813 if (_aotTimerScheduled) {
2814 _aotTimerES->cancelTimeout();
2815 _aotTimerScheduled = false;
2816 }
2817 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2818
2819 // re-enable this timer for next sleep
2820 cancelIdleSleepTimer();
2821
2822 if (clamshellExists) {
2823 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2824 if (gClamshellFlags & kClamshell_WAR_58009435) {
2825 // Disable clamshell sleep until system has completed full wake.
2826 // This prevents a system sleep request (due to a clamshell close)
2827 // from being queued until the end of system full wake - even if
2828 // other clamshell disable bits outside of our control is wrong.
2829 setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2830 }
2831 #endif
2832
2833 // Log the last known clamshell state before system sleep
2834 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2835 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2836 desktopMode, acAdaptorConnected);
2837 }
2838
2839 clock_get_calendar_absolute_and_microtime(&secs, µsecs, &now);
2840 logtime(secs);
2841 gIOLastSleepTime.tv_sec = secs;
2842 gIOLastSleepTime.tv_usec = microsecs;
2843 if (!_aotLastWakeTime) {
2844 gIOLastUserSleepTime = gIOLastSleepTime;
2845 }
2846
2847 gIOLastWakeTime.tv_sec = 0;
2848 gIOLastWakeTime.tv_usec = 0;
2849 gIOLastSleepAbsTime = now;
2850
2851 if (wake2DarkwakeDelay && sleepDelaysReport) {
2852 clock_sec_t wake2DarkwakeSecs, darkwake2SleepSecs;
2853 // Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2854
2855 SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2856 absolutetime_to_microtime(now, &darkwake2SleepSecs, µsecs);
2857 absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, µsecs);
2858 HISTREPORT_TALLYVALUE(sleepDelaysReport,
2859 (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2860
2861 DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2862 wake2DarkwakeDelay = 0;
2863 }
2864 #if HIBERNATION
2865 LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2866
2867 IOHibernateSystemHasSlept();
2868
2869 evaluateSystemSleepPolicyFinal();
2870 #else
2871 LOG("System Sleep\n");
2872 #endif
2873 if (thermalWarningState) {
2874 OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2875 if (event) {
2876 systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2877 }
2878 }
2879 assertOnWakeSecs = 0;
2880 lowBatteryCondition = false;
2881 thermalEmergencyState = false;
2882
2883 #if DEVELOPMENT || DEBUG
2884 extern int g_should_log_clock_adjustments;
2885 if (g_should_log_clock_adjustments) {
2886 clock_sec_t secs = 0;
2887 clock_usec_t microsecs = 0;
2888 uint64_t now_b = mach_absolute_time();
2889
2890 secs = 0;
2891 microsecs = 0;
2892 PEGetUTCTimeOfDay(&secs, µsecs);
2893
2894 uint64_t now_a = mach_absolute_time();
2895 os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2896 __func__, (unsigned long)secs, microsecs, now_b, now_a);
2897 }
2898 #endif
2899
2900 getPlatform()->sleepKernel();
2901
2902 // The CPU(s) are off at this point,
2903 // Code will resume execution here upon wake.
2904
2905 clock_get_uptime(&gIOLastWakeAbsTime);
2906 IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2907 _highestCapability = 0;
2908
2909 #if HIBERNATION
2910 IOHibernateSystemWake();
2911 #endif
2912
2913 // sleep transition complete
2914 gSleepOrShutdownPending = 0;
2915
2916 // trip the reset of the calendar clock
2917 clock_wakeup_calendar();
2918 clock_get_calendar_microtime(&secs, µsecs);
2919 gIOLastWakeTime.tv_sec = secs;
2920 gIOLastWakeTime.tv_usec = microsecs;
2921
2922 // aot
2923 if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2924 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2925 secs = 0;
2926 microsecs = 0;
2927 PEGetUTCTimeOfDay(&secs, µsecs);
2928 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2929 IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2930 _aotMetrics->sleepCount++;
2931 _aotLastWakeTime = gIOLastWakeAbsTime;
2932 if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2933 _aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
2934 = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
2935 _aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
2936 = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
2937 }
2938
2939 if (_aotTestTime) {
2940 if (_aotWakeTimeUTC <= secs) {
2941 _aotTestTime = _aotTestTime + _aotTestInterval;
2942 }
2943 setWakeTime(_aotTestTime);
2944 }
2945 }
2946
2947 #if HIBERNATION
2948 LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
2949 #endif
2950
2951 lastSleepReason = 0;
2952
2953 lastDebugWakeSeconds = _debugWakeSeconds;
2954 _debugWakeSeconds = 0;
2955 _scheduledAlarmMask = 0;
2956 _nextScheduledAlarmType = NULL;
2957
2958 darkWakeExit = false;
2959 darkWakePowerClamped = false;
2960 darkWakePostTickle = false;
2961 darkWakeHibernateError = false;
2962 darkWakeToSleepASAP = true;
2963 darkWakeLogClamp = true;
2964 sleepTimerMaintenance = false;
2965 sleepToStandby = false;
2966 wranglerTickled = false;
2967 userWasActive = false;
2968 isRTCAlarmWake = false;
2969 clamshellIgnoreClose = false;
2970 fullWakeReason = kFullWakeReasonNone;
2971
2972 #if defined(__i386__) || defined(__x86_64__)
2973 kdebugTrace(kPMLogSystemWake, 0, 0, 0);
2974
2975 OSSharedPtr<OSObject> wakeTypeProp = copyProperty(kIOPMRootDomainWakeTypeKey);
2976 OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
2977 OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
2978 OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
2979
2980 if (wakeReason && (wakeReason->getLength() >= 2) &&
2981 gWakeReasonString[0] == '\0') {
2982 WAKEEVENT_LOCK();
2983 // Until the platform driver can claim its wake reasons
2984 strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
2985 sizeof(gWakeReasonString));
2986 if (!gWakeReasonSysctlRegistered) {
2987 gWakeReasonSysctlRegistered = true;
2988 }
2989 WAKEEVENT_UNLOCK();
2990 }
2991
2992 if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
2993 lowBatteryCondition = true;
2994 darkWakeMaintenance = true;
2995 } else {
2996 #if HIBERNATION
2997 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
2998 OSNumber * hibOptions = OSDynamicCast( OSNumber, hibOptionsProp.get());
2999 if (hibernateAborted || ((hibOptions &&
3000 !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
3001 // Hibernate aborted, or EFI brought up graphics
3002 darkWakeExit = true;
3003 if (hibernateAborted) {
3004 DLOG("Hibernation aborted\n");
3005 } else {
3006 DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3007 }
3008 } else
3009 #endif
3010 if (wakeType && (
3011 wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3012 wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3013 // User wake or RTC alarm
3014 darkWakeExit = true;
3015 if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3016 isRTCAlarmWake = true;
3017 }
3018 } else if (wakeType &&
3019 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3020 // SMC standby timer trumps SleepX
3021 darkWakeMaintenance = true;
3022 sleepTimerMaintenance = true;
3023 } else if ((lastDebugWakeSeconds != 0) &&
3024 ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3025 // SleepX before maintenance
3026 darkWakeExit = true;
3027 } else if (wakeType &&
3028 wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3029 darkWakeMaintenance = true;
3030 } else if (wakeType &&
3031 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3032 darkWakeMaintenance = true;
3033 darkWakeSleepService = true;
3034 #if HIBERNATION
3035 if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3036 sleepToStandby = true;
3037 }
3038 #endif
3039 } else if (wakeType &&
3040 wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3041 darkWakeMaintenance = true;
3042 darkWakeHibernateError = true;
3043 } else {
3044 // Unidentified wake source, resume to full wake if debug
3045 // alarm is pending.
3046
3047 if (lastDebugWakeSeconds &&
3048 (!wakeReason || wakeReason->isEqualTo(""))) {
3049 darkWakeExit = true;
3050 }
3051 }
3052 }
3053
3054 if (darkWakeExit) {
3055 darkWakeToSleepASAP = false;
3056 fullWakeReason = kFullWakeReasonLocalUser;
3057 reportUserInput();
3058 } else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3059 handleSetDisplayPowerOn(true);
3060 } else if (!darkWakeMaintenance) {
3061 // Early/late tickle for non-maintenance wake.
3062 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3063 darkWakePostTickle = true;
3064 }
3065 }
3066 #else /* !__i386__ && !__x86_64__ */
3067 timeSinceReset = ml_get_time_since_reset();
3068 kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3069
3070 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3071 wranglerTickled = true;
3072 fullWakeReason = kFullWakeReasonLocalUser;
3073 requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3074 } else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3075 isRTCAlarmWake = true;
3076 fullWakeReason = kFullWakeReasonLocalUser;
3077 requestUserActive(this, "RTC debug alarm");
3078 } else {
3079 #if HIBERNATION
3080 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3081 OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3082 if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3083 fullWakeReason = kFullWakeReasonLocalUser;
3084 requestUserActive(this, "hibernate user wake");
3085 }
3086 #endif
3087 }
3088
3089 // stay awake for at least 30 seconds
3090 startIdleSleepTimer(30 * 1000);
3091 #endif
3092 sleepCnt++;
3093
3094 thread_call_enter(updateConsoleUsersEntry);
3095
3096 // Skip AOT_STATE if we are waking up from an RTC timer.
3097 // This check needs to be done after the epoch change is processed
3098 // and before the changePowerStateWithTagToPriv() call below.
3099 WAKEEVENT_LOCK();
3100 aotShouldExit(false, false);
3101 WAKEEVENT_UNLOCK();
3102
3103 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3104 break;
3105 }
3106 #if !__i386__ && !__x86_64__
3107 case ON_STATE:
3108 case AOT_STATE:
3109 {
3110 DLOG("Force re-evaluating aggressiveness\n");
3111 /* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3112 pmPowerStateQueue->submitPowerEvent(
3113 kPowerEventPolicyStimulus,
3114 (void *) kStimulusNoIdleSleepPreventers );
3115
3116 // After changing to ON_STATE, invalidate any previously queued
3117 // request to change to a state less than ON_STATE. This isn't
3118 // necessary for AOT_STATE or if the device has only one running
3119 // state since the changePowerStateToPriv() issued at the tail
3120 // end of SLEEP_STATE case should take care of that.
3121 if (getPowerState() == ON_STATE) {
3122 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3123 }
3124 break;
3125 }
3126 #endif /* !__i386__ && !__x86_64__ */
3127 }
3128 notifierThread = NULL;
3129 }
3130
3131 //******************************************************************************
3132 // requestPowerDomainState
3133 //
3134 // Extend implementation in IOService. Running on PM work loop thread.
3135 //******************************************************************************
3136
3137 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3138 IOPMrootDomain::requestPowerDomainState(
3139 IOPMPowerFlags childDesire,
3140 IOPowerConnection * childConnection,
3141 unsigned long specification )
3142 {
3143 // Idle and system sleep prevention flags affects driver desire.
3144 // Children desire are irrelevant so they are cleared.
3145
3146 return super::requestPowerDomainState(0, childConnection, specification);
3147 }
3148
3149
3150 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3151 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3152 {
3153 if (!preventers->getCount()) {
3154 return;
3155 }
3156
3157 char *buf_iter = buf + strlen(buf);
3158 char *buf_end = buf + buf_size;
3159
3160 OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3161 OSObject *obj = NULL;
3162
3163 while ((obj = iterator->getNextObject())) {
3164 IOService *srv = OSDynamicCast(IOService, obj);
3165 if (buf_iter < buf_end) {
3166 buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3167 } else {
3168 DLOG("Print buffer exhausted for sleep preventers list\n");
3169 break;
3170 }
3171 }
3172 }
3173
3174 //******************************************************************************
3175 // updatePreventIdleSleepList
3176 //
3177 // Called by IOService on PM work loop.
3178 // Returns true if PM policy recognized the driver's desire to prevent idle
3179 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3180 //******************************************************************************
3181
3182 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3183 IOPMrootDomain::updatePreventIdleSleepList(
3184 IOService * service, bool addNotRemove)
3185 {
3186 unsigned int oldCount;
3187
3188 oldCount = idleSleepPreventersCount();
3189 return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3190 }
3191
3192 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3193 IOPMrootDomain::updatePreventIdleSleepListInternal(
3194 IOService * service, bool addNotRemove, unsigned int oldCount)
3195 {
3196 unsigned int newCount;
3197
3198 ASSERT_GATED();
3199
3200 #if defined(XNU_TARGET_OS_OSX)
3201 // Only the display wrangler and no-idle-sleep kernel assertions
3202 // can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3203 // reported by drivers in their power state table is ignored.
3204 if (service && (service != wrangler) && (service != this)) {
3205 return false;
3206 }
3207 #endif
3208
3209 if (service) {
3210 if (addNotRemove) {
3211 preventIdleSleepList->setObject(service);
3212 DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3213 service->getName(), preventIdleSleepList->getCount());
3214 } else if (preventIdleSleepList->member(service)) {
3215 preventIdleSleepList->removeObject(service);
3216 DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3217 service->getName(), preventIdleSleepList->getCount());
3218 }
3219
3220 if (preventIdleSleepList->getCount()) {
3221 char buf[256] = "Idle Sleep Preventers:";
3222 makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3223 DLOG("%s\n", buf);
3224 }
3225 }
3226
3227 newCount = idleSleepPreventersCount();
3228
3229 if ((oldCount == 0) && (newCount != 0)) {
3230 // Driver added to empty prevent list.
3231 // Update the driver desire to prevent idle sleep.
3232 // Driver desire does not prevent demand sleep.
3233
3234 changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3235 } else if ((oldCount != 0) && (newCount == 0)) {
3236 // Last driver removed from prevent list.
3237 // Drop the driver clamp to allow idle sleep.
3238
3239 changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3240 evaluatePolicy( kStimulusNoIdleSleepPreventers );
3241 }
3242 messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3243 &newCount, sizeof(newCount));
3244
3245 #if defined(XNU_TARGET_OS_OSX)
3246 if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3247 DLOG("Cannot cancel idle sleep\n");
3248 return false; // do not idle-cancel
3249 }
3250 #endif
3251
3252 return true;
3253 }
3254
3255 //******************************************************************************
3256 // startSpinDump
3257 //******************************************************************************
3258
3259 void
startSpinDump(uint32_t spindumpKind)3260 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3261 {
3262 messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3263 }
3264
3265 //******************************************************************************
3266 // preventSystemSleepListUpdate
3267 //
3268 // Called by IOService on PM work loop.
3269 //******************************************************************************
3270
3271 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3272 IOPMrootDomain::updatePreventSystemSleepList(
3273 IOService * service, bool addNotRemove )
3274 {
3275 unsigned int oldCount, newCount;
3276
3277 ASSERT_GATED();
3278 if (this == service) {
3279 return;
3280 }
3281
3282 oldCount = preventSystemSleepList->getCount();
3283 if (addNotRemove) {
3284 preventSystemSleepList->setObject(service);
3285 DLOG("Added %s to system sleep preventers list (Total %u)\n",
3286 service->getName(), preventSystemSleepList->getCount());
3287 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3288 AbsoluteTime now;
3289 clock_usec_t microsecs;
3290 clock_get_uptime(&now);
3291 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3292 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
3293 if (assertOnWakeReport) {
3294 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3295 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3296 }
3297 }
3298 } else if (preventSystemSleepList->member(service)) {
3299 preventSystemSleepList->removeObject(service);
3300 DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3301 service->getName(), preventSystemSleepList->getCount());
3302
3303 if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3304 // Lost all system sleep preventers.
3305 // Send stimulus if system sleep was blocked, and is in dark wake.
3306 evaluatePolicy( kStimulusDarkWakeEvaluate );
3307 }
3308 }
3309
3310 newCount = preventSystemSleepList->getCount();
3311 if (newCount) {
3312 char buf[256] = "System Sleep Preventers:";
3313 makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3314 DLOG("%s\n", buf);
3315 }
3316
3317 messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3318 &newCount, sizeof(newCount));
3319 }
3320
3321 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3322 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3323 {
3324 OSSharedPtr<OSCollectionIterator> iterator;
3325 OSObject *object = NULL;
3326 OSSharedPtr<OSArray> array;
3327
3328 if (!gIOPMWorkLoop->inGate()) {
3329 gIOPMWorkLoop->runAction(
3330 OSMemberFunctionCast(IOWorkLoop::Action, this,
3331 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3332 this, (void *)idleSleepList, (void *)systemSleepList);
3333 return;
3334 }
3335
3336 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3337 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3338 array = OSArray::withCapacity(5);
3339
3340 if (iterator && array) {
3341 while ((object = iterator->getNextObject())) {
3342 IOService *service = OSDynamicCast(IOService, object);
3343 if (service) {
3344 OSSharedPtr<const OSSymbol> name = service->copyName();
3345 if (name) {
3346 array->setObject(name.get());
3347 }
3348 }
3349 }
3350 }
3351 *idleSleepList = array.detach();
3352 }
3353
3354 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3355 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3356 array = OSArray::withCapacity(5);
3357
3358 if (iterator && array) {
3359 while ((object = iterator->getNextObject())) {
3360 IOService *service = OSDynamicCast(IOService, object);
3361 if (service) {
3362 OSSharedPtr<const OSSymbol> name = service->copyName();
3363 if (name) {
3364 array->setObject(name.get());
3365 }
3366 }
3367 }
3368 }
3369 *systemSleepList = array.detach();
3370 }
3371 }
3372
3373 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3374 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3375 {
3376 OSSharedPtr<OSCollectionIterator> iterator;
3377 OSObject *object = NULL;
3378 OSSharedPtr<OSArray> array;
3379
3380 if (!gIOPMWorkLoop->inGate()) {
3381 gIOPMWorkLoop->runAction(
3382 OSMemberFunctionCast(IOWorkLoop::Action, this,
3383 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3384 this, (void *)idleSleepList, (void *)systemSleepList);
3385 return;
3386 }
3387
3388 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3389 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3390 array = OSArray::withCapacity(5);
3391
3392 if (iterator && array) {
3393 while ((object = iterator->getNextObject())) {
3394 IOService *service = OSDynamicCast(IOService, object);
3395 if (service) {
3396 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3397 OSSharedPtr<const OSSymbol> name = service->copyName();
3398 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3399 if (dict && name && id) {
3400 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3401 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3402 array->setObject(dict.get());
3403 }
3404 }
3405 }
3406 }
3407 *idleSleepList = array.detach();
3408 }
3409
3410 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3411 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3412 array = OSArray::withCapacity(5);
3413
3414 if (iterator && array) {
3415 while ((object = iterator->getNextObject())) {
3416 IOService *service = OSDynamicCast(IOService, object);
3417 if (service) {
3418 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3419 OSSharedPtr<const OSSymbol> name = service->copyName();
3420 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3421 if (dict && name && id) {
3422 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3423 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3424 array->setObject(dict.get());
3425 }
3426 }
3427 }
3428 }
3429 *systemSleepList = array.detach();
3430 }
3431 }
3432
3433 //******************************************************************************
3434 // tellChangeDown
3435 //
3436 // Override the superclass implementation to send a different message type.
3437 //******************************************************************************
3438
3439 bool
tellChangeDown(unsigned long stateNum)3440 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3441 {
3442 DLOG("tellChangeDown %s->%s\n",
3443 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3444
3445 if (SLEEP_STATE == stateNum) {
3446 // Legacy apps were already told in the full->dark transition
3447 if (!ignoreTellChangeDown) {
3448 tracePoint( kIOPMTracePointSleepApplications );
3449 } else {
3450 tracePoint( kIOPMTracePointSleepPriorityClients );
3451 }
3452 }
3453
3454 if (!ignoreTellChangeDown) {
3455 userActivityAtSleep = userActivityCount;
3456 DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3457
3458 if (SLEEP_STATE == stateNum) {
3459 hibernateAborted = false;
3460
3461 // Direct callout into OSKext so it can disable kext unloads
3462 // during sleep/wake to prevent deadlocks.
3463 OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3464
3465 IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3466
3467 // Two change downs are sent by IOServicePM. Ignore the 2nd.
3468 // But tellClientsWithResponse() must be called for both.
3469 ignoreTellChangeDown = true;
3470 }
3471 }
3472
3473 return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3474 }
3475
3476 //******************************************************************************
3477 // askChangeDown
3478 //
3479 // Override the superclass implementation to send a different message type.
3480 // This must be idle sleep since we don't ask during any other power change.
3481 //******************************************************************************
3482
3483 bool
askChangeDown(unsigned long stateNum)3484 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3485 {
3486 DLOG("askChangeDown %s->%s\n",
3487 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3488
3489 // Don't log for dark wake entry
3490 if (kSystemTransitionSleep == _systemTransitionType) {
3491 tracePoint( kIOPMTracePointSleepApplications );
3492 }
3493
3494 return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3495 }
3496
3497 //******************************************************************************
3498 // askChangeDownDone
3499 //
3500 // An opportunity for root domain to cancel the power transition,
3501 // possibily due to an assertion created by powerd in response to
3502 // kIOMessageCanSystemSleep.
3503 //
3504 // Idle sleep:
3505 // full -> dark wake transition
3506 // 1. Notify apps and powerd with kIOMessageCanSystemSleep
3507 // 2. askChangeDownDone()
3508 // dark -> sleep transition
3509 // 1. Notify powerd with kIOMessageCanSystemSleep
3510 // 2. askChangeDownDone()
3511 //
3512 // Demand sleep:
3513 // full -> dark wake transition
3514 // 1. Notify powerd with kIOMessageCanSystemSleep
3515 // 2. askChangeDownDone()
3516 // dark -> sleep transition
3517 // 1. Notify powerd with kIOMessageCanSystemSleep
3518 // 2. askChangeDownDone()
3519 //******************************************************************************
3520
3521 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3522 IOPMrootDomain::askChangeDownDone(
3523 IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3524 {
3525 DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3526 *inOutChangeFlags, *cancel,
3527 _systemTransitionType,
3528 _currentCapability, _pendingCapability);
3529
3530 if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3531 // Dark->Sleep transition.
3532 // Check if there are any deny sleep assertions.
3533 // lastSleepReason already set by handleOurPowerChangeStart()
3534
3535 if (!checkSystemCanSleep(lastSleepReason)) {
3536 // Cancel dark wake to sleep transition.
3537 // Must re-scan assertions upon entering dark wake.
3538
3539 *cancel = true;
3540 DLOG("cancel dark->sleep\n");
3541 }
3542 if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3543 uint64_t now = mach_continuous_time();
3544 if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3545 && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3546 *cancel = true;
3547 IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3548 }
3549 }
3550 }
3551 }
3552
3553 //******************************************************************************
3554 // systemDidNotSleep
3555 //
3556 // Work common to both canceled or aborted sleep.
3557 //******************************************************************************
3558
3559 void
systemDidNotSleep(void)3560 IOPMrootDomain::systemDidNotSleep( void )
3561 {
3562 // reset console lock state
3563 thread_call_enter(updateConsoleUsersEntry);
3564
3565 if (idleSleepEnabled) {
3566 if (!wrangler) {
3567 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3568 startIdleSleepTimer(kIdleSleepRetryInterval);
3569 #else
3570 startIdleSleepTimer(idleMilliSeconds);
3571 #endif
3572 } else if (!userIsActive) {
3573 // Manually start the idle sleep timer besides waiting for
3574 // the user to become inactive.
3575 startIdleSleepTimer(kIdleSleepRetryInterval);
3576 }
3577 }
3578
3579 preventTransitionToUserActive(false);
3580 IOService::setAdvisoryTickleEnable( true );
3581
3582 // After idle revert and cancel, send a did-change message to powerd
3583 // to balance the previous will-change message. Kernel clients do not
3584 // need this since sleep cannot be canceled once they are notified.
3585
3586 if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3587 (_pendingCapability != _currentCapability) &&
3588 ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3589 // Differs from a real capability gain change where notifyRef != 0,
3590 // but it is zero here since no response is expected.
3591
3592 IOPMSystemCapabilityChangeParameters params;
3593
3594 bzero(¶ms, sizeof(params));
3595 params.fromCapabilities = _pendingCapability;
3596 params.toCapabilities = _currentCapability;
3597 params.changeFlags = kIOPMSystemCapabilityDidChange;
3598
3599 DLOG("MESG cap %x->%x did change\n",
3600 params.fromCapabilities, params.toCapabilities);
3601 messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3602 ¶ms, sizeof(params));
3603 }
3604 }
3605
3606 //******************************************************************************
3607 // tellNoChangeDown
3608 //
3609 // Notify registered applications and kernel clients that we are not dropping
3610 // power.
3611 //
3612 // We override the superclass implementation so we can send a different message
3613 // type to the client or application being notified.
3614 //
3615 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3616 //******************************************************************************
3617
3618 void
tellNoChangeDown(unsigned long stateNum)3619 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3620 {
3621 DLOG("tellNoChangeDown %s->%s\n",
3622 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3623
3624 // Sleep canceled, clear the sleep trace point.
3625 tracePoint(kIOPMTracePointSystemUp);
3626
3627 systemDidNotSleep();
3628 return tellClients( kIOMessageSystemWillNotSleep );
3629 }
3630
3631 //******************************************************************************
3632 // tellChangeUp
3633 //
3634 // Notify registered applications and kernel clients that we are raising power.
3635 //
3636 // We override the superclass implementation so we can send a different message
3637 // type to the client or application being notified.
3638 //******************************************************************************
3639
3640 void
tellChangeUp(unsigned long stateNum)3641 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3642 {
3643 DLOG("tellChangeUp %s->%s\n",
3644 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3645
3646 ignoreTellChangeDown = false;
3647
3648 if (stateNum == ON_STATE) {
3649 // Direct callout into OSKext so it can disable kext unloads
3650 // during sleep/wake to prevent deadlocks.
3651 OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3652
3653 // Notify platform that sleep was cancelled or resumed.
3654 getPlatform()->callPlatformFunction(
3655 sleepMessagePEFunction.get(), false,
3656 (void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3657 NULL, NULL, NULL);
3658
3659 if (getPowerState() == ON_STATE) {
3660 // Sleep was cancelled by idle cancel or revert
3661 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3662 // rdar://problem/50363791
3663 // If system is in dark wake and sleep is cancelled, do not
3664 // send SystemWillPowerOn/HasPoweredOn messages to kernel
3665 // priority clients. They haven't yet seen a SystemWillSleep
3666 // message before the cancellation. So make sure the kernel
3667 // client bit is cleared in _systemMessageClientMask before
3668 // invoking the tellClients() below. This bit may have been
3669 // set by handleOurPowerChangeStart() anticipating a successful
3670 // sleep and setting the filter mask ahead of time allows the
3671 // SystemWillSleep message to go through.
3672 _systemMessageClientMask &= ~kSystemMessageClientKernel;
3673 }
3674
3675 systemDidNotSleep();
3676 tellClients( kIOMessageSystemWillPowerOn );
3677 }
3678
3679 tracePoint( kIOPMTracePointWakeApplications );
3680 tellClients( kIOMessageSystemHasPoweredOn );
3681 }
3682 }
3683
3684 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3685 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3686 ((params)->fromCapabilities & (flag)) && \
3687 (((params)->toCapabilities & (flag)) == 0))
3688
3689 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3690 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3691 ((params)->toCapabilities & (flag)) && \
3692 (((params)->fromCapabilities & (flag)) == 0))
3693
3694 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3695 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3696 ((params)->fromCapabilities & (flag)) && \
3697 (((params)->toCapabilities & (flag)) == 0))
3698
3699 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3700 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3701 ((params)->toCapabilities & (flag)) && \
3702 (((params)->fromCapabilities & (flag)) == 0))
3703
3704 //******************************************************************************
3705 // sysPowerDownHandler
3706 //
3707 // Perform a vfs sync before system sleep.
3708 //******************************************************************************
3709
3710 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3711 IOPMrootDomain::sysPowerDownHandler(
3712 void * target, void * refCon,
3713 UInt32 messageType, IOService * service,
3714 void * messageArgs, vm_size_t argSize )
3715 {
3716 static UInt32 lastSystemMessageType = 0;
3717 IOReturn ret = 0;
3718
3719 DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3720
3721 // rdar://problem/50363791
3722 // Sanity check to make sure the SystemWill/Has message types are
3723 // received in the expected order for all kernel priority clients.
3724 if (messageType == kIOMessageSystemWillSleep ||
3725 messageType == kIOMessageSystemWillPowerOn ||
3726 messageType == kIOMessageSystemHasPoweredOn) {
3727 switch (messageType) {
3728 case kIOMessageSystemWillPowerOn:
3729 assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3730 break;
3731 case kIOMessageSystemHasPoweredOn:
3732 assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3733 break;
3734 }
3735
3736 lastSystemMessageType = messageType;
3737 }
3738
3739 if (!gRootDomain) {
3740 return kIOReturnUnsupported;
3741 }
3742
3743 if (messageType == kIOMessageSystemCapabilityChange) {
3744 IOPMSystemCapabilityChangeParameters * params =
3745 (IOPMSystemCapabilityChangeParameters *) messageArgs;
3746
3747 // Interested applications have been notified of an impending power
3748 // change and have acked (when applicable).
3749 // This is our chance to save whatever state we can before powering
3750 // down.
3751 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3752 // via callout
3753
3754 DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3755 params->fromCapabilities, params->toCapabilities,
3756 params->changeFlags);
3757
3758 if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3759 // We will ack within 20 seconds
3760 params->maxWaitForReply = 20 * 1000 * 1000;
3761
3762 #if HIBERNATION
3763 gRootDomain->evaluateSystemSleepPolicyEarly();
3764
3765 // add in time we could spend freeing pages
3766 if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3767 params->maxWaitForReply = kCapabilityClientMaxWait;
3768 }
3769 DLOG("sysPowerDownHandler max wait %d s\n",
3770 (int) (params->maxWaitForReply / 1000 / 1000));
3771 #endif
3772
3773 // Notify platform that sleep has begun, after the early
3774 // sleep policy evaluation.
3775 getPlatform()->callPlatformFunction(
3776 sleepMessagePEFunction.get(), false,
3777 (void *)(uintptr_t) kIOMessageSystemWillSleep,
3778 NULL, NULL, NULL);
3779
3780 if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3781 // Purposely delay the ack and hope that shutdown occurs quickly.
3782 // Another option is not to schedule the thread and wait for
3783 // ack timeout...
3784 AbsoluteTime deadline;
3785 clock_interval_to_deadline( 30, kSecondScale, &deadline );
3786 thread_call_enter1_delayed(
3787 gRootDomain->diskSyncCalloutEntry,
3788 (thread_call_param_t)(uintptr_t) params->notifyRef,
3789 deadline );
3790 } else {
3791 thread_call_enter1(
3792 gRootDomain->diskSyncCalloutEntry,
3793 (thread_call_param_t)(uintptr_t) params->notifyRef);
3794 }
3795 }
3796 #if HIBERNATION
3797 else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3798 // We will ack within 110 seconds
3799 params->maxWaitForReply = 110 * 1000 * 1000;
3800
3801 thread_call_enter1(
3802 gRootDomain->diskSyncCalloutEntry,
3803 (thread_call_param_t)(uintptr_t) params->notifyRef);
3804 }
3805 #endif
3806 ret = kIOReturnSuccess;
3807 }
3808
3809 return ret;
3810 }
3811
3812 //******************************************************************************
3813 // handleQueueSleepWakeUUID
3814 //
3815 // Called from IOPMrootDomain when we're initiating a sleep,
3816 // or indirectly from PM configd when PM decides to clear the UUID.
3817 // PM clears the UUID several minutes after successful wake from sleep,
3818 // so that we might associate App spindumps with the immediately previous
3819 // sleep/wake.
3820 //
3821 // @param obj has a retain on it. We're responsible for releasing that retain.
3822 //******************************************************************************
3823
3824 void
handleQueueSleepWakeUUID(OSObject * obj)3825 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3826 {
3827 OSSharedPtr<OSString> str;
3828
3829 if (kOSBooleanFalse == obj) {
3830 handlePublishSleepWakeUUID(false);
3831 } else {
3832 str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3833 if (str) {
3834 // This branch caches the UUID for an upcoming sleep/wake
3835 queuedSleepWakeUUIDString = str;
3836 DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3837 }
3838 }
3839 }
3840 //******************************************************************************
3841 // handlePublishSleepWakeUUID
3842 //
3843 // Called from IOPMrootDomain when we're initiating a sleep,
3844 // or indirectly from PM configd when PM decides to clear the UUID.
3845 // PM clears the UUID several minutes after successful wake from sleep,
3846 // so that we might associate App spindumps with the immediately previous
3847 // sleep/wake.
3848 //******************************************************************************
3849
3850 void
handlePublishSleepWakeUUID(bool shouldPublish)3851 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3852 {
3853 ASSERT_GATED();
3854
3855 /*
3856 * Clear the current UUID
3857 */
3858 if (gSleepWakeUUIDIsSet) {
3859 DLOG("SleepWake UUID cleared\n");
3860
3861 gSleepWakeUUIDIsSet = false;
3862
3863 removeProperty(kIOPMSleepWakeUUIDKey);
3864 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3865 }
3866
3867 /*
3868 * Optionally, publish a new UUID
3869 */
3870 if (queuedSleepWakeUUIDString && shouldPublish) {
3871 OSSharedPtr<OSString> publishThisUUID;
3872
3873 publishThisUUID = queuedSleepWakeUUIDString;
3874
3875 if (publishThisUUID) {
3876 setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3877 }
3878
3879 gSleepWakeUUIDIsSet = true;
3880 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3881
3882 queuedSleepWakeUUIDString.reset();
3883 }
3884 }
3885
3886 //******************************************************************************
3887 // IOPMGetSleepWakeUUIDKey
3888 //
3889 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3890 // To get the full key -- a C string -- the buffer must large enough for
3891 // the end-of-string character.
3892 // The key is expected to be an UUID string
3893 //******************************************************************************
3894
3895 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3896 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3897 {
3898 if (!gSleepWakeUUIDIsSet) {
3899 return false;
3900 }
3901
3902 if (buffer != NULL) {
3903 OSSharedPtr<OSString> string =
3904 OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3905
3906 if (!string) {
3907 *buffer = '\0';
3908 } else {
3909 strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3910 }
3911 }
3912
3913 return true;
3914 }
3915
3916 //******************************************************************************
3917 // lowLatencyAudioNotify
3918 //
3919 // Used to send an update about low latency audio activity to interested
3920 // clients. To keep the overhead minimal the OSDictionary used here
3921 // is initialized at boot.
3922 //******************************************************************************
3923
3924 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3925 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3926 {
3927 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
3928 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
3929 lowLatencyAudioNotifyTimestampVal->setValue(time);
3930 lowLatencyAudioNotifyStateVal->setValue(state);
3931 setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
3932 } else {
3933 DLOG("LowLatencyAudioNotify error\n");
3934 }
3935 return;
3936 }
3937
3938 //******************************************************************************
3939 // IOPMrootDomainRTNotifier
3940 //
3941 // Used by performance controller to update the timestamp and state associated
3942 // with low latency audio activity in the system.
3943 //******************************************************************************
3944
3945 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)3946 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
3947 {
3948 gRootDomain->lowLatencyAudioNotify(time, state);
3949 return;
3950 }
3951
3952 //******************************************************************************
3953 // initializeBootSessionUUID
3954 //
3955 // Initialize the boot session uuid at boot up and sets it into registry.
3956 //******************************************************************************
3957
3958 void
initializeBootSessionUUID(void)3959 IOPMrootDomain::initializeBootSessionUUID(void)
3960 {
3961 uuid_t new_uuid;
3962 uuid_string_t new_uuid_string;
3963
3964 uuid_generate(new_uuid);
3965 uuid_unparse_upper(new_uuid, new_uuid_string);
3966 memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
3967
3968 setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
3969 }
3970
3971 //******************************************************************************
3972 // Root domain uses the private and tagged changePowerState methods for
3973 // tracking and logging purposes.
3974 //******************************************************************************
3975
3976 #define REQUEST_TAG_TO_REASON(x) ((uint16_t)x)
3977
3978 static uint32_t
nextRequestTag(IOPMRequestTag tag)3979 nextRequestTag( IOPMRequestTag tag )
3980 {
3981 static SInt16 msb16 = 1;
3982 uint16_t id = OSAddAtomic16(1, &msb16);
3983 return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
3984 }
3985
3986 // TODO: remove this shim function and exported symbol
3987 IOReturn
changePowerStateTo(unsigned long ordinal)3988 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
3989 {
3990 return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
3991 }
3992
3993 // TODO: remove this shim function and exported symbol
3994 IOReturn
changePowerStateToPriv(unsigned long ordinal)3995 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
3996 {
3997 return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
3998 }
3999
4000 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4001 IOPMrootDomain::changePowerStateWithOverrideTo(
4002 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4003 {
4004 uint32_t tag = nextRequestTag(reason);
4005 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4006
4007 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4008 return kIOReturnUnsupported;
4009 }
4010
4011 return super::changePowerStateWithOverrideTo(ordinal, tag);
4012 }
4013
4014 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4015 IOPMrootDomain::changePowerStateWithTagTo(
4016 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4017 {
4018 uint32_t tag = nextRequestTag(reason);
4019 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4020
4021 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4022 return kIOReturnUnsupported;
4023 }
4024
4025 return super::changePowerStateWithTagTo(ordinal, tag);
4026 }
4027
4028 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4029 IOPMrootDomain::changePowerStateWithTagToPriv(
4030 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4031 {
4032 uint32_t tag = nextRequestTag(reason);
4033 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4034
4035 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4036 return kIOReturnUnsupported;
4037 }
4038
4039 return super::changePowerStateWithTagToPriv(ordinal, tag);
4040 }
4041
4042 //******************************************************************************
4043 // activity detect
4044 //
4045 //******************************************************************************
4046
4047 bool
activitySinceSleep(void)4048 IOPMrootDomain::activitySinceSleep(void)
4049 {
4050 return userActivityCount != userActivityAtSleep;
4051 }
4052
4053 bool
abortHibernation(void)4054 IOPMrootDomain::abortHibernation(void)
4055 {
4056 #if __arm64__
4057 // don't allow hibernation to be aborted on ARM due to user activity
4058 // since once ApplePMGR decides we're hibernating, we can't turn back
4059 // see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4060 return false;
4061 #else
4062 bool ret = activitySinceSleep();
4063
4064 if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4065 DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4066 hibernateAborted = true;
4067 }
4068 return ret;
4069 #endif
4070 }
4071
4072 extern "C" int
hibernate_should_abort(void)4073 hibernate_should_abort(void)
4074 {
4075 if (gRootDomain) {
4076 return gRootDomain->abortHibernation();
4077 } else {
4078 return 0;
4079 }
4080 }
4081
4082 //******************************************************************************
4083 // willNotifyPowerChildren
4084 //
4085 // Called after all interested drivers have all acknowledged the power change,
4086 // but before any power children is informed. Dispatched though a thread call,
4087 // so it is safe to perform work that might block on a sleeping disk. PM state
4088 // machine (not thread) will block w/o timeout until this function returns.
4089 //******************************************************************************
4090
4091 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4092 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4093 {
4094 OSSharedPtr<OSDictionary> dict;
4095 OSSharedPtr<OSNumber> secs;
4096
4097 if (SLEEP_STATE == newPowerState) {
4098 notifierThread = current_thread();
4099 if (!tasksSuspended) {
4100 AbsoluteTime deadline;
4101 tasksSuspended = TRUE;
4102 updateTasksSuspend();
4103
4104 clock_interval_to_deadline(10, kSecondScale, &deadline);
4105 #if defined(XNU_TARGET_OS_OSX)
4106 vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4107 #endif /* defined(XNU_TARGET_OS_OSX) */
4108 }
4109
4110 _aotReadyToFullWake = false;
4111 #if 0
4112 if (_aotLingerTime) {
4113 uint64_t deadline;
4114 IOLog("aot linger no return\n");
4115 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4116 clock_delay_until(deadline);
4117 }
4118 #endif
4119 if (!_aotMode) {
4120 _aotTestTime = 0;
4121 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4122 if (_aotMetrics) {
4123 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4124 }
4125 } else if (!_aotNow && !_debugWakeSeconds) {
4126 _aotNow = true;
4127 _aotPendingFlags = 0;
4128 _aotTasksSuspended = true;
4129 _aotLastWakeTime = 0;
4130 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4131 if (kIOPMAOTModeCycle & _aotMode) {
4132 clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4133 _aotTestTime = mach_continuous_time() + _aotTestInterval;
4134 setWakeTime(_aotTestTime);
4135 }
4136 uint32_t lingerSecs;
4137 if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4138 lingerSecs = 0;
4139 }
4140 clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4141 clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4142 clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4143 }
4144
4145 #if HIBERNATION
4146 IOHibernateSystemSleep();
4147 IOHibernateIOKitSleep();
4148 #endif
4149 if (gRootDomain->activitySinceSleep()) {
4150 dict = OSDictionary::withCapacity(1);
4151 secs = OSNumber::withNumber(1, 32);
4152
4153 if (dict && secs) {
4154 dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4155 gRootDomain->setProperties(dict.get());
4156 MSG("Reverting sleep with relative wake\n");
4157 }
4158 }
4159
4160 notifierThread = NULL;
4161 }
4162 }
4163
4164 //******************************************************************************
4165 // willTellSystemCapabilityDidChange
4166 //
4167 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4168 // domain is raising its power state, immediately after notifying interested
4169 // drivers and power children.
4170 //******************************************************************************
4171
4172 void
willTellSystemCapabilityDidChange(void)4173 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4174 {
4175 if ((_systemTransitionType == kSystemTransitionWake) &&
4176 !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4177 // After powering up drivers, dark->full promotion on the current wake
4178 // transition is no longer possible. That is because the next machine
4179 // state will issue the system capability change messages.
4180 // The darkWakePowerClamped flag may already be set if the system has
4181 // at least one driver that was power clamped due to dark wake.
4182 // This function sets the darkWakePowerClamped flag in case there
4183 // is no power-clamped driver in the system.
4184 //
4185 // Last opportunity to exit dark wake using:
4186 // requestFullWake( kFullWakeReasonLocalUser );
4187
4188 if (!darkWakePowerClamped) {
4189 if (darkWakeLogClamp) {
4190 AbsoluteTime now;
4191 uint64_t nsec;
4192
4193 clock_get_uptime(&now);
4194 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4195 absolutetime_to_nanoseconds(now, &nsec);
4196 DLOG("dark wake promotion disabled at %u ms\n",
4197 ((int)((nsec) / NSEC_PER_MSEC)));
4198 }
4199 darkWakePowerClamped = true;
4200 }
4201 }
4202 }
4203
4204 //******************************************************************************
4205 // sleepOnClamshellClosed
4206 //
4207 // contains the logic to determine if the system should sleep when the clamshell
4208 // is closed.
4209 //******************************************************************************
4210
4211 bool
shouldSleepOnClamshellClosed(void)4212 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4213 {
4214 if (!clamshellExists) {
4215 return false;
4216 }
4217
4218 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4219 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4220
4221 return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4222 }
4223
4224 bool
shouldSleepOnRTCAlarmWake(void)4225 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4226 {
4227 // Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4228 // closed && battery
4229 if (!clamshellExists) {
4230 return false;
4231 }
4232
4233 DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4234 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4235
4236 return !acAdaptorConnected && !clamshellSleepDisableMask;
4237 }
4238
4239 void
sendClientClamshellNotification(void)4240 IOPMrootDomain::sendClientClamshellNotification( void )
4241 {
4242 /* Only broadcast clamshell alert if clamshell exists. */
4243 if (!clamshellExists) {
4244 return;
4245 }
4246
4247 setProperty(kAppleClamshellStateKey,
4248 clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4249
4250 setProperty(kAppleClamshellCausesSleepKey,
4251 shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4252
4253 /* Argument to message is a bitfiel of
4254 * ( kClamshellStateBit | kClamshellSleepBit )
4255 */
4256 messageClients(kIOPMMessageClamshellStateChange,
4257 (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4258 | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4259 }
4260
4261 //******************************************************************************
4262 // getSleepSupported
4263 //
4264 // Deprecated
4265 //******************************************************************************
4266
4267 IOOptionBits
getSleepSupported(void)4268 IOPMrootDomain::getSleepSupported( void )
4269 {
4270 return platformSleepSupport;
4271 }
4272
4273 //******************************************************************************
4274 // setSleepSupported
4275 //
4276 // Deprecated
4277 //******************************************************************************
4278
4279 void
setSleepSupported(IOOptionBits flags)4280 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4281 {
4282 DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4283 OSBitOrAtomic(flags, &platformSleepSupport);
4284 }
4285
4286 //******************************************************************************
4287 // setClamShellSleepDisable
4288 //
4289 //******************************************************************************
4290
4291 void
setClamShellSleepDisable(bool disable,uint32_t bitmask)4292 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4293 {
4294 uint32_t oldMask;
4295
4296 // User client calls this in non-gated context
4297 if (gIOPMWorkLoop->inGate() == false) {
4298 gIOPMWorkLoop->runAction(
4299 OSMemberFunctionCast(IOWorkLoop::Action, this,
4300 &IOPMrootDomain::setClamShellSleepDisable),
4301 (OSObject *) this,
4302 (void *) disable, (void *)(uintptr_t) bitmask);
4303 return;
4304 }
4305
4306 oldMask = clamshellSleepDisableMask;
4307 if (disable) {
4308 clamshellSleepDisableMask |= bitmask;
4309 } else {
4310 clamshellSleepDisableMask &= ~bitmask;
4311 }
4312 DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4313
4314 if (clamshellExists && clamshellClosed &&
4315 (clamshellSleepDisableMask != oldMask) &&
4316 (clamshellSleepDisableMask == 0)) {
4317 handlePowerNotification(kLocalEvalClamshellCommand);
4318 }
4319 }
4320
4321 //******************************************************************************
4322 // wakeFromDoze
4323 //
4324 // Deprecated.
4325 //******************************************************************************
4326
4327 void
wakeFromDoze(void)4328 IOPMrootDomain::wakeFromDoze( void )
4329 {
4330 // Preserve symbol for familes (IOUSBFamily and IOGraphics)
4331 }
4332
4333 //******************************************************************************
4334 // recordRTCAlarm
4335 //
4336 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4337 // should be a dark wake or a full wake. Both Maintenance and SleepService
4338 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4339 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4340 // PMSettings are ignored.
4341 //
4342 // Caller serialized using settingsCtrlLock.
4343 //******************************************************************************
4344
4345 void
recordRTCAlarm(const OSSymbol * type,OSObject * object)4346 IOPMrootDomain::recordRTCAlarm(
4347 const OSSymbol *type,
4348 OSObject *object )
4349 {
4350 uint32_t previousAlarmMask = _scheduledAlarmMask;
4351
4352 if (type == gIOPMSettingDebugWakeRelativeKey) {
4353 OSNumber * n = OSDynamicCast(OSNumber, object);
4354 if (n) {
4355 // Debug wake has highest scheduling priority so it overrides any
4356 // pre-existing alarm.
4357 uint32_t debugSecs = n->unsigned32BitValue();
4358 _nextScheduledAlarmType.reset(type, OSRetain);
4359 _nextScheduledAlarmUTC = debugSecs;
4360
4361 _debugWakeSeconds = debugSecs;
4362 OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4363 DLOG("next alarm (%s) in %u secs\n",
4364 type->getCStringNoCopy(), debugSecs);
4365 }
4366 } else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4367 (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4368 (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4369 OSData * data = OSDynamicCast(OSData, object);
4370 if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4371 const IOPMCalendarStruct * cs;
4372 bool replaceNextAlarm = false;
4373 clock_sec_t secs;
4374
4375 cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4376 secs = IOPMConvertCalendarToSeconds(cs);
4377 DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4378
4379 // Update the next scheduled alarm type
4380 if ((_nextScheduledAlarmType == NULL) ||
4381 ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4382 (secs < _nextScheduledAlarmUTC))) {
4383 replaceNextAlarm = true;
4384 }
4385
4386 if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4387 if (cs->year) {
4388 _calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4389 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4390 } else {
4391 // TODO: can this else-block be removed?
4392 _calendarWakeAlarmUTC = 0;
4393 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4394 }
4395 }
4396 if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4397 OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4398 }
4399 if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4400 OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4401 }
4402
4403 if (replaceNextAlarm) {
4404 _nextScheduledAlarmType.reset(type, OSRetain);
4405 _nextScheduledAlarmUTC = secs;
4406 DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4407 }
4408 }
4409 }
4410
4411 if (_scheduledAlarmMask != previousAlarmMask) {
4412 DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4413 }
4414 }
4415
4416 // MARK: -
4417 // MARK: Features
4418
4419 //******************************************************************************
4420 // publishFeature
4421 //
4422 // Adds a new feature to the supported features dictionary
4423 //******************************************************************************
4424
4425 void
publishFeature(const char * feature)4426 IOPMrootDomain::publishFeature( const char * feature )
4427 {
4428 publishFeature(feature, kRD_AllPowerSources, NULL);
4429 }
4430
4431 //******************************************************************************
4432 // publishFeature (with supported power source specified)
4433 //
4434 // Adds a new feature to the supported features dictionary
4435 //******************************************************************************
4436
4437 void
publishFeature(const char * feature,uint32_t supportedWhere,uint32_t * uniqueFeatureID)4438 IOPMrootDomain::publishFeature(
4439 const char *feature,
4440 uint32_t supportedWhere,
4441 uint32_t *uniqueFeatureID)
4442 {
4443 static uint16_t next_feature_id = 500;
4444
4445 OSSharedPtr<OSNumber> new_feature_data;
4446 OSNumber *existing_feature = NULL;
4447 OSArray *existing_feature_arr_raw = NULL;
4448 OSSharedPtr<OSArray> existing_feature_arr;
4449 OSObject *osObj = NULL;
4450 uint32_t feature_value = 0;
4451
4452 supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4453
4454 if (!supportedWhere) {
4455 // Feature isn't supported anywhere!
4456 return;
4457 }
4458
4459 if (next_feature_id > 5000) {
4460 // Far, far too many features!
4461 return;
4462 }
4463
4464 if (featuresDictLock) {
4465 IOLockLock(featuresDictLock);
4466 }
4467
4468 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4469 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4470 OSSharedPtr<OSDictionary> features;
4471
4472 // Create new features dict if necessary
4473 if (origFeatures) {
4474 features = OSDictionary::withDictionary(origFeatures);
4475 } else {
4476 features = OSDictionary::withCapacity(1);
4477 }
4478
4479 // Create OSNumber to track new feature
4480
4481 next_feature_id += 1;
4482 if (uniqueFeatureID) {
4483 // We don't really mind if the calling kext didn't give us a place
4484 // to stash their unique id. Many kexts don't plan to unload, and thus
4485 // have no need to remove themselves later.
4486 *uniqueFeatureID = next_feature_id;
4487 }
4488
4489 feature_value = (uint32_t)next_feature_id;
4490 feature_value <<= 16;
4491 feature_value += supportedWhere;
4492
4493 new_feature_data = OSNumber::withNumber(
4494 (unsigned long long)feature_value, 32);
4495
4496 // Does features object already exist?
4497 if ((osObj = features->getObject(feature))) {
4498 if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4499 // We need to create an OSArray to hold the now 2 elements.
4500 existing_feature_arr = OSArray::withObjects(
4501 (const OSObject **)&existing_feature, 1, 2);
4502 } else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4503 // Add object to existing array
4504 existing_feature_arr = OSArray::withArray(
4505 existing_feature_arr_raw,
4506 existing_feature_arr_raw->getCount() + 1);
4507 }
4508
4509 if (existing_feature_arr) {
4510 existing_feature_arr->setObject(new_feature_data.get());
4511 features->setObject(feature, existing_feature_arr.get());
4512 }
4513 } else {
4514 // The easy case: no previously existing features listed. We simply
4515 // set the OSNumber at key 'feature' and we're on our way.
4516 features->setObject(feature, new_feature_data.get());
4517 }
4518
4519 setProperty(kRootDomainSupportedFeatures, features.get());
4520
4521 if (featuresDictLock) {
4522 IOLockUnlock(featuresDictLock);
4523 }
4524
4525 // Notify EnergySaver and all those in user space so they might
4526 // re-populate their feature specific UI
4527 if (pmPowerStateQueue) {
4528 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4529 }
4530 }
4531
4532 //******************************************************************************
4533 // removePublishedFeature
4534 //
4535 // Removes previously published feature
4536 //******************************************************************************
4537
4538 IOReturn
removePublishedFeature(uint32_t removeFeatureID)4539 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4540 {
4541 IOReturn ret = kIOReturnError;
4542 uint32_t feature_value = 0;
4543 uint16_t feature_id = 0;
4544 bool madeAChange = false;
4545
4546 OSSymbol *dictKey = NULL;
4547 OSSharedPtr<OSCollectionIterator> dictIterator;
4548 OSArray *arrayMember = NULL;
4549 OSNumber *numberMember = NULL;
4550 OSObject *osObj = NULL;
4551 OSNumber *osNum = NULL;
4552 OSSharedPtr<OSArray> arrayMemberCopy;
4553
4554 if (kBadPMFeatureID == removeFeatureID) {
4555 return kIOReturnNotFound;
4556 }
4557
4558 if (featuresDictLock) {
4559 IOLockLock(featuresDictLock);
4560 }
4561
4562 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4563 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4564 OSSharedPtr<OSDictionary> features;
4565
4566 if (origFeatures) {
4567 // Any modifications to the dictionary are made to the copy to prevent
4568 // races & crashes with userland clients. Dictionary updated
4569 // automically later.
4570 features = OSDictionary::withDictionary(origFeatures);
4571 } else {
4572 features = NULL;
4573 ret = kIOReturnNotFound;
4574 goto exit;
4575 }
4576
4577 // We iterate 'features' dictionary looking for an entry tagged
4578 // with 'removeFeatureID'. If found, we remove it from our tracking
4579 // structures and notify the OS via a general interest message.
4580
4581 dictIterator = OSCollectionIterator::withCollection(features.get());
4582 if (!dictIterator) {
4583 goto exit;
4584 }
4585
4586 while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4587 osObj = features->getObject(dictKey);
4588
4589 // Each Feature is either tracked by an OSNumber
4590 if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4591 feature_value = numberMember->unsigned32BitValue();
4592 feature_id = (uint16_t)(feature_value >> 16);
4593
4594 if (feature_id == (uint16_t)removeFeatureID) {
4595 // Remove this node
4596 features->removeObject(dictKey);
4597 madeAChange = true;
4598 break;
4599 }
4600
4601 // Or tracked by an OSArray of OSNumbers
4602 } else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4603 unsigned int arrayCount = arrayMember->getCount();
4604
4605 for (unsigned int i = 0; i < arrayCount; i++) {
4606 osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4607 if (!osNum) {
4608 continue;
4609 }
4610
4611 feature_value = osNum->unsigned32BitValue();
4612 feature_id = (uint16_t)(feature_value >> 16);
4613
4614 if (feature_id == (uint16_t)removeFeatureID) {
4615 // Remove this node
4616 if (1 == arrayCount) {
4617 // If the array only contains one element, remove
4618 // the whole thing.
4619 features->removeObject(dictKey);
4620 } else {
4621 // Otherwise remove the element from a copy of the array.
4622 arrayMemberCopy = OSArray::withArray(arrayMember);
4623 if (arrayMemberCopy) {
4624 arrayMemberCopy->removeObject(i);
4625 features->setObject(dictKey, arrayMemberCopy.get());
4626 }
4627 }
4628
4629 madeAChange = true;
4630 break;
4631 }
4632 }
4633 }
4634 }
4635
4636 if (madeAChange) {
4637 ret = kIOReturnSuccess;
4638
4639 setProperty(kRootDomainSupportedFeatures, features.get());
4640
4641 // Notify EnergySaver and all those in user space so they might
4642 // re-populate their feature specific UI
4643 if (pmPowerStateQueue) {
4644 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4645 }
4646 } else {
4647 ret = kIOReturnNotFound;
4648 }
4649
4650 exit:
4651 if (featuresDictLock) {
4652 IOLockUnlock(featuresDictLock);
4653 }
4654 return ret;
4655 }
4656
4657 //******************************************************************************
4658 // publishPMSetting (private)
4659 //
4660 // Should only be called by PMSettingObject to publish a PM Setting as a
4661 // supported feature.
4662 //******************************************************************************
4663
4664 void
publishPMSetting(const OSSymbol * feature,uint32_t where,uint32_t * featureID)4665 IOPMrootDomain::publishPMSetting(
4666 const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4667 {
4668 if (noPublishPMSettings &&
4669 (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4670 // Setting found in noPublishPMSettings array
4671 *featureID = kBadPMFeatureID;
4672 return;
4673 }
4674
4675 publishFeature(
4676 feature->getCStringNoCopy(), where, featureID);
4677 }
4678
4679 //******************************************************************************
4680 // setPMSetting (private)
4681 //
4682 // Internal helper to relay PM settings changes from user space to individual
4683 // drivers. Should be called only by IOPMrootDomain::setProperties.
4684 //******************************************************************************
4685
4686 IOReturn
setPMSetting(const OSSymbol * type,OSObject * object)4687 IOPMrootDomain::setPMSetting(
4688 const OSSymbol *type,
4689 OSObject *object )
4690 {
4691 PMSettingCallEntry *entries = NULL;
4692 OSSharedPtr<OSArray> chosen;
4693 const OSArray *array;
4694 PMSettingObject *pmso;
4695 thread_t thisThread;
4696 int i, j, count, capacity;
4697 bool ok = false;
4698 IOReturn ret;
4699
4700 if (NULL == type) {
4701 return kIOReturnBadArgument;
4702 }
4703
4704 PMSETTING_LOCK();
4705
4706 // Update settings dict so changes are visible from copyPMSetting().
4707 fPMSettingsDict->setObject(type, object);
4708
4709 // Prep all PMSetting objects with the given 'type' for callout.
4710 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4711 if (!array || ((capacity = array->getCount()) == 0)) {
4712 goto unlock_exit;
4713 }
4714
4715 // Array to retain PMSetting objects targeted for callout.
4716 chosen = OSArray::withCapacity(capacity);
4717 if (!chosen) {
4718 goto unlock_exit; // error
4719 }
4720 entries = IONew(PMSettingCallEntry, capacity);
4721 if (!entries) {
4722 goto unlock_exit; // error
4723 }
4724 memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4725
4726 thisThread = current_thread();
4727
4728 for (i = 0, j = 0; i < capacity; i++) {
4729 pmso = (PMSettingObject *) array->getObject(i);
4730 if (pmso->disabled) {
4731 continue;
4732 }
4733 entries[j].thread = thisThread;
4734 queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4735 chosen->setObject(pmso);
4736 j++;
4737 }
4738 count = j;
4739 if (!count) {
4740 goto unlock_exit;
4741 }
4742
4743 PMSETTING_UNLOCK();
4744
4745 // Call each pmso in the chosen array.
4746 for (i = 0; i < count; i++) {
4747 pmso = (PMSettingObject *) chosen->getObject(i);
4748 ret = pmso->dispatchPMSetting(type, object);
4749 if (ret == kIOReturnSuccess) {
4750 // At least one setting handler was successful
4751 ok = true;
4752 #if DEVELOPMENT || DEBUG
4753 } else {
4754 // Log the handler and kext that failed
4755 OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4756 if (kextName) {
4757 DLOG("PMSetting(%s) error 0x%x from %s\n",
4758 type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4759 }
4760 #endif
4761 }
4762 }
4763
4764 PMSETTING_LOCK();
4765 for (i = 0; i < count; i++) {
4766 pmso = (PMSettingObject *) chosen->getObject(i);
4767 queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4768 if (pmso->waitThread) {
4769 PMSETTING_WAKEUP(pmso);
4770 }
4771 }
4772
4773 if (ok) {
4774 recordRTCAlarm(type, object);
4775 }
4776 unlock_exit:
4777 PMSETTING_UNLOCK();
4778
4779 if (entries) {
4780 IODelete(entries, PMSettingCallEntry, capacity);
4781 }
4782
4783 return kIOReturnSuccess;
4784 }
4785
4786 //******************************************************************************
4787 // copyPMSetting (public)
4788 //
4789 // Allows kexts to safely read setting values, without being subscribed to
4790 // notifications.
4791 //******************************************************************************
4792
4793 OSSharedPtr<OSObject>
copyPMSetting(OSSymbol * whichSetting)4794 IOPMrootDomain::copyPMSetting(
4795 OSSymbol *whichSetting)
4796 {
4797 OSSharedPtr<OSObject> obj;
4798
4799 if (!whichSetting) {
4800 return NULL;
4801 }
4802
4803 PMSETTING_LOCK();
4804 obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4805 PMSETTING_UNLOCK();
4806
4807 return obj;
4808 }
4809
4810 //******************************************************************************
4811 // registerPMSettingController (public)
4812 //
4813 // direct wrapper to registerPMSettingController with uint32_t power source arg
4814 //******************************************************************************
4815
4816 IOReturn
registerPMSettingController(const OSSymbol * settings[],IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4817 IOPMrootDomain::registerPMSettingController(
4818 const OSSymbol * settings[],
4819 IOPMSettingControllerCallback func,
4820 OSObject *target,
4821 uintptr_t refcon,
4822 OSObject **handle)
4823 {
4824 return registerPMSettingController(
4825 settings,
4826 (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4827 func, target, refcon, handle);
4828 }
4829
4830 //******************************************************************************
4831 // registerPMSettingController (public)
4832 //
4833 // Kexts may register for notifications when a particular setting is changed.
4834 // A list of settings is available in IOPM.h.
4835 // Arguments:
4836 // * settings - An OSArray containing OSSymbols. Caller should populate this
4837 // array with a list of settings caller wants notifications from.
4838 // * func - A C function callback of the type IOPMSettingControllerCallback
4839 // * target - caller may provide an OSObject *, which PM will pass as an
4840 // target to calls to "func"
4841 // * refcon - caller may provide an void *, which PM will pass as an
4842 // argument to calls to "func"
4843 // * handle - This is a return argument. We will populate this pointer upon
4844 // call success. Hold onto this and pass this argument to
4845 // IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4846 // Returns:
4847 // kIOReturnSuccess on success
4848 //******************************************************************************
4849
4850 IOReturn
registerPMSettingController(const OSSymbol * settings[],uint32_t supportedPowerSources,IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4851 IOPMrootDomain::registerPMSettingController(
4852 const OSSymbol * settings[],
4853 uint32_t supportedPowerSources,
4854 IOPMSettingControllerCallback func,
4855 OSObject *target,
4856 uintptr_t refcon,
4857 OSObject **handle)
4858 {
4859 PMSettingObject *pmso = NULL;
4860 OSObject *pmsh = NULL;
4861 int i;
4862
4863 if (NULL == settings ||
4864 NULL == func ||
4865 NULL == handle) {
4866 return kIOReturnBadArgument;
4867 }
4868
4869 pmso = PMSettingObject::pmSettingObject(
4870 (IOPMrootDomain *) this, func, target,
4871 refcon, supportedPowerSources, settings, &pmsh);
4872
4873 if (!pmso) {
4874 *handle = NULL;
4875 return kIOReturnInternalError;
4876 }
4877
4878 PMSETTING_LOCK();
4879 for (i = 0; settings[i]; i++) {
4880 OSSharedPtr<OSArray> newList;
4881 OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4882 if (!list) {
4883 // New array of callbacks for this setting
4884 newList = OSArray::withCapacity(1);
4885 settingsCallbacks->setObject(settings[i], newList.get());
4886 list = newList.get();
4887 }
4888
4889 // Add caller to the callback list
4890 list->setObject(pmso);
4891 }
4892 PMSETTING_UNLOCK();
4893
4894 // Return handle to the caller, the setting object is private.
4895 *handle = pmsh;
4896
4897 return kIOReturnSuccess;
4898 }
4899
4900 //******************************************************************************
4901 // deregisterPMSettingObject (private)
4902 //
4903 // Only called from PMSettingObject.
4904 //******************************************************************************
4905
4906 void
deregisterPMSettingObject(PMSettingObject * pmso)4907 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
4908 {
4909 thread_t thisThread = current_thread();
4910 PMSettingCallEntry *callEntry;
4911 OSSharedPtr<OSCollectionIterator> iter;
4912 OSSymbol *sym;
4913 OSArray *array;
4914 int index;
4915 bool wait;
4916
4917 PMSETTING_LOCK();
4918
4919 pmso->disabled = true;
4920
4921 // Wait for all callout threads to finish.
4922 do {
4923 wait = false;
4924 queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
4925 {
4926 if (callEntry->thread != thisThread) {
4927 wait = true;
4928 break;
4929 }
4930 }
4931 if (wait) {
4932 assert(NULL == pmso->waitThread);
4933 pmso->waitThread = thisThread;
4934 PMSETTING_WAIT(pmso);
4935 pmso->waitThread = NULL;
4936 }
4937 } while (wait);
4938
4939 // Search each PM settings array in the kernel.
4940 iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
4941 if (iter) {
4942 while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
4943 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
4944 index = array->getNextIndexOfObject(pmso, 0);
4945 if (-1 != index) {
4946 array->removeObject(index);
4947 }
4948 }
4949 }
4950
4951 PMSETTING_UNLOCK();
4952
4953 pmso->release();
4954 }
4955
4956 //******************************************************************************
4957 // informCPUStateChange
4958 //
4959 // Call into PM CPU code so that CPU power savings may dynamically adjust for
4960 // running on battery, with the lid closed, etc.
4961 //
4962 // informCPUStateChange is a no-op on non x86 systems
4963 // only x86 has explicit support in the IntelCPUPowerManagement kext
4964 //******************************************************************************
4965
4966 void
informCPUStateChange(uint32_t type,uint32_t value)4967 IOPMrootDomain::informCPUStateChange(
4968 uint32_t type,
4969 uint32_t value )
4970 {
4971 #if defined(__i386__) || defined(__x86_64__)
4972
4973 pmioctlVariableInfo_t varInfoStruct;
4974 int pmCPUret = 0;
4975 const char *varNameStr = NULL;
4976 int32_t *varIndex = NULL;
4977
4978 if (kInformAC == type) {
4979 varNameStr = kIOPMRootDomainBatPowerCString;
4980 varIndex = &idxPMCPULimitedPower;
4981 } else if (kInformLid == type) {
4982 varNameStr = kIOPMRootDomainLidCloseCString;
4983 varIndex = &idxPMCPUClamshell;
4984 } else {
4985 return;
4986 }
4987
4988 // Set the new value!
4989 // pmCPUControl will assign us a new ID if one doesn't exist yet
4990 bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
4991 varInfoStruct.varID = *varIndex;
4992 varInfoStruct.varType = vBool;
4993 varInfoStruct.varInitValue = value;
4994 varInfoStruct.varCurValue = value;
4995 strlcpy((char *)varInfoStruct.varName,
4996 (const char *)varNameStr,
4997 sizeof(varInfoStruct.varName));
4998
4999 // Set!
5000 pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
5001
5002 // pmCPU only assigns numerical id's when a new varName is specified
5003 if ((0 == pmCPUret)
5004 && (*varIndex == kCPUUnknownIndex)) {
5005 // pmCPUControl has assigned us a new variable ID.
5006 // Let's re-read the structure we just SET to learn that ID.
5007 pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5008
5009 if (0 == pmCPUret) {
5010 // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5011 *varIndex = varInfoStruct.varID;
5012 }
5013 }
5014
5015 return;
5016
5017 #endif /* __i386__ || __x86_64__ */
5018 }
5019
5020 // MARK: -
5021 // MARK: Deep Sleep Policy
5022
5023 #if HIBERNATION
5024
5025 //******************************************************************************
5026 // evaluateSystemSleepPolicy
5027 //******************************************************************************
5028
5029 #define kIOPlatformSystemSleepPolicyKey "IOPlatformSystemSleepPolicy"
5030
5031 // Sleep flags
5032 enum {
5033 kIOPMSleepFlagHibernate = 0x00000001,
5034 kIOPMSleepFlagSleepTimerEnable = 0x00000002
5035 };
5036
5037 struct IOPMSystemSleepPolicyEntry {
5038 uint32_t factorMask;
5039 uint32_t factorBits;
5040 uint32_t sleepFlags;
5041 uint32_t wakeEvents;
5042 } __attribute__((packed));
5043
5044 struct IOPMSystemSleepPolicyTable {
5045 uint32_t signature;
5046 uint16_t version;
5047 uint16_t entryCount;
5048 IOPMSystemSleepPolicyEntry entries[];
5049 } __attribute__((packed));
5050
5051 enum {
5052 kIOPMSleepAttributeHibernateSetup = 0x00000001,
5053 kIOPMSleepAttributeHibernateSleep = 0x00000002
5054 };
5055
5056 static uint32_t
getSleepTypeAttributes(uint32_t sleepType)5057 getSleepTypeAttributes( uint32_t sleepType )
5058 {
5059 static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5060 {
5061 /* invalid */ 0,
5062 /* abort */ 0,
5063 /* normal */ 0,
5064 /* safesleep */ kIOPMSleepAttributeHibernateSetup,
5065 /* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5066 /* standby */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5067 /* poweroff */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5068 /* deepidle */ 0
5069 };
5070
5071 if (sleepType >= kIOPMSleepTypeLast) {
5072 return 0;
5073 }
5074
5075 return sleepTypeAttributes[sleepType];
5076 }
5077
5078 bool
evaluateSystemSleepPolicy(IOPMSystemSleepParameters * params,int sleepPhase,uint32_t * hibMode)5079 IOPMrootDomain::evaluateSystemSleepPolicy(
5080 IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5081 {
5082 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5083
5084 static const IONamedValue factorValues[] = {
5085 SLEEP_FACTOR( SleepTimerWake ),
5086 SLEEP_FACTOR( LidOpen ),
5087 SLEEP_FACTOR( ACPower ),
5088 SLEEP_FACTOR( BatteryLow ),
5089 SLEEP_FACTOR( StandbyNoDelay ),
5090 SLEEP_FACTOR( StandbyForced ),
5091 SLEEP_FACTOR( StandbyDisabled ),
5092 SLEEP_FACTOR( USBExternalDevice ),
5093 SLEEP_FACTOR( BluetoothHIDDevice ),
5094 SLEEP_FACTOR( ExternalMediaMounted ),
5095 SLEEP_FACTOR( ThunderboltDevice ),
5096 SLEEP_FACTOR( RTCAlarmScheduled ),
5097 SLEEP_FACTOR( MagicPacketWakeEnabled ),
5098 SLEEP_FACTOR( HibernateForced ),
5099 SLEEP_FACTOR( AutoPowerOffDisabled ),
5100 SLEEP_FACTOR( AutoPowerOffForced ),
5101 SLEEP_FACTOR( ExternalDisplay ),
5102 SLEEP_FACTOR( NetworkKeepAliveActive ),
5103 SLEEP_FACTOR( LocalUserActivity ),
5104 SLEEP_FACTOR( HibernateFailed ),
5105 SLEEP_FACTOR( ThermalWarning ),
5106 SLEEP_FACTOR( DisplayCaptured ),
5107 { 0, NULL }
5108 };
5109
5110 const IOPMSystemSleepPolicyTable * pt;
5111 OSSharedPtr<OSObject> prop;
5112 OSData * policyData;
5113 uint64_t currentFactors = 0;
5114 char currentFactorsBuf[512];
5115 uint32_t standbyDelay = 0;
5116 uint32_t powerOffDelay = 0;
5117 uint32_t powerOffTimer = 0;
5118 uint32_t standbyTimer = 0;
5119 uint32_t mismatch;
5120 bool standbyEnabled;
5121 bool powerOffEnabled;
5122 bool found = false;
5123
5124 // Get platform's sleep policy table
5125 if (!gSleepPolicyHandler) {
5126 prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5127 if (!prop) {
5128 goto done;
5129 }
5130 }
5131
5132 // Fetch additional settings
5133 standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5134 && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5135 powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5136 && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5137 if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5138 powerOffTimer = powerOffDelay;
5139 }
5140 if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5141 standbyTimer = standbyDelay;
5142 }
5143
5144 DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5145 sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5146 powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5147
5148 currentFactorsBuf[0] = 0;
5149 // pmset level overrides
5150 if ((*hibMode & kIOHibernateModeOn) == 0) {
5151 if (!gSleepPolicyHandler) {
5152 standbyEnabled = false;
5153 powerOffEnabled = false;
5154 }
5155 } else if (!(*hibMode & kIOHibernateModeSleep)) {
5156 // Force hibernate (i.e. mode 25)
5157 // If standby is enabled, force standy.
5158 // If poweroff is enabled, force poweroff.
5159 if (standbyEnabled) {
5160 currentFactors |= kIOPMSleepFactorStandbyForced;
5161 } else if (powerOffEnabled) {
5162 currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5163 } else {
5164 currentFactors |= kIOPMSleepFactorHibernateForced;
5165 }
5166 }
5167
5168 // Current factors based on environment and assertions
5169 if (sleepTimerMaintenance) {
5170 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5171 }
5172 if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5173 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5174 }
5175 if (!clamshellClosed) {
5176 currentFactors |= kIOPMSleepFactorLidOpen;
5177 }
5178 if (acAdaptorConnected) {
5179 currentFactors |= kIOPMSleepFactorACPower;
5180 }
5181 if (lowBatteryCondition) {
5182 hibernateMode = 0;
5183 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5184 if ((hibernateMode & kIOHibernateModeOn) == 0) {
5185 DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5186 } else {
5187 currentFactors |= kIOPMSleepFactorBatteryLow;
5188 }
5189 }
5190 if (!standbyDelay || !standbyTimer) {
5191 currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5192 }
5193 if (standbyNixed || !standbyEnabled) {
5194 currentFactors |= kIOPMSleepFactorStandbyDisabled;
5195 }
5196 if (resetTimers) {
5197 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5198 currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5199 }
5200 if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5201 kIOPMDriverAssertionLevelOff) {
5202 currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5203 }
5204 if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5205 kIOPMDriverAssertionLevelOff) {
5206 currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5207 }
5208 if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5209 kIOPMDriverAssertionLevelOff) {
5210 currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5211 }
5212 if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5213 kIOPMDriverAssertionLevelOff) {
5214 currentFactors |= kIOPMSleepFactorThunderboltDevice;
5215 }
5216 if (_scheduledAlarmMask != 0) {
5217 currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5218 }
5219 if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5220 kIOPMDriverAssertionLevelOff) {
5221 currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5222 }
5223 #define TCPKEEPALIVE 1
5224 #if TCPKEEPALIVE
5225 if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5226 kIOPMDriverAssertionLevelOff) {
5227 currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5228 }
5229 #endif
5230 if (!powerOffEnabled) {
5231 currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5232 }
5233 if (desktopMode) {
5234 currentFactors |= kIOPMSleepFactorExternalDisplay;
5235 }
5236 if (userWasActive) {
5237 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5238 }
5239 if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5240 currentFactors |= kIOPMSleepFactorHibernateFailed;
5241 }
5242 if (thermalWarningState) {
5243 currentFactors |= kIOPMSleepFactorThermalWarning;
5244 }
5245
5246 for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5247 uint32_t factor = 1 << factorBit;
5248 if (factor & currentFactors) {
5249 strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5250 strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5251 }
5252 }
5253 DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5254
5255 if (gSleepPolicyHandler) {
5256 uint32_t savedHibernateMode;
5257 IOReturn result;
5258
5259 if (!gSleepPolicyVars) {
5260 gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5261 }
5262 gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5263 gSleepPolicyVars->version = kIOPMSystemSleepPolicyVersion;
5264 gSleepPolicyVars->currentCapability = _currentCapability;
5265 gSleepPolicyVars->highestCapability = _highestCapability;
5266 gSleepPolicyVars->sleepFactors = currentFactors;
5267 gSleepPolicyVars->sleepReason = lastSleepReason;
5268 gSleepPolicyVars->sleepPhase = sleepPhase;
5269 gSleepPolicyVars->standbyDelay = standbyDelay;
5270 gSleepPolicyVars->standbyTimer = standbyTimer;
5271 gSleepPolicyVars->poweroffDelay = powerOffDelay;
5272 gSleepPolicyVars->scheduledAlarms = _scheduledAlarmMask | _userScheduledAlarmMask;
5273 gSleepPolicyVars->poweroffTimer = powerOffTimer;
5274
5275 if (kIOPMSleepPhase0 == sleepPhase) {
5276 // preserve hibernateMode
5277 savedHibernateMode = gSleepPolicyVars->hibernateMode;
5278 gSleepPolicyVars->hibernateMode = *hibMode;
5279 } else if (kIOPMSleepPhase1 == sleepPhase) {
5280 // use original hibernateMode for phase2
5281 gSleepPolicyVars->hibernateMode = *hibMode;
5282 }
5283
5284 result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5285
5286 if (kIOPMSleepPhase0 == sleepPhase) {
5287 // restore hibernateMode
5288 gSleepPolicyVars->hibernateMode = savedHibernateMode;
5289 }
5290
5291 if ((result != kIOReturnSuccess) ||
5292 (kIOPMSleepTypeInvalid == params->sleepType) ||
5293 (params->sleepType >= kIOPMSleepTypeLast) ||
5294 (kIOPMSystemSleepParametersVersion != params->version)) {
5295 MSG("sleep policy handler error\n");
5296 goto done;
5297 }
5298
5299 if ((getSleepTypeAttributes(params->sleepType) &
5300 kIOPMSleepAttributeHibernateSetup) &&
5301 ((*hibMode & kIOHibernateModeOn) == 0)) {
5302 *hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5303 }
5304
5305 DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5306 params->version, params->sleepType, params->sleepFlags,
5307 params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5308 found = true;
5309 goto done;
5310 }
5311
5312 // Policy table is meaningless without standby enabled
5313 if (!standbyEnabled) {
5314 goto done;
5315 }
5316
5317 // Validate the sleep policy table
5318 policyData = OSDynamicCast(OSData, prop.get());
5319 if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5320 goto done;
5321 }
5322
5323 pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5324 if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5325 (pt->version != 1) || (0 == pt->entryCount)) {
5326 goto done;
5327 }
5328
5329 if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5330 (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5331 goto done;
5332 }
5333
5334 for (uint32_t i = 0; i < pt->entryCount; i++) {
5335 const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5336 mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5337
5338 DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5339 entry->factorMask, entry->factorBits,
5340 entry->sleepFlags, entry->wakeEvents, mismatch);
5341 if (mismatch) {
5342 continue;
5343 }
5344
5345 DLOG("^ found match\n");
5346 found = true;
5347
5348 params->version = kIOPMSystemSleepParametersVersion;
5349 params->reserved1 = 1;
5350 if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5351 params->sleepType = kIOPMSleepTypeStandby;
5352 } else {
5353 params->sleepType = kIOPMSleepTypeNormalSleep;
5354 }
5355
5356 params->ecWakeEvents = entry->wakeEvents;
5357 if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5358 if (kIOPMSleepPhase2 == sleepPhase) {
5359 clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5360
5361 if (!_standbyTimerResetSeconds ||
5362 (now_secs <= _standbyTimerResetSeconds)) {
5363 // Reset standby timer adjustment
5364 _standbyTimerResetSeconds = now_secs;
5365 DLOG("standby delay %u, reset %u\n",
5366 standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5367 } else if (standbyDelay) {
5368 // Shorten the standby delay timer
5369 clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5370 if (standbyDelay > elapsed) {
5371 standbyDelay -= elapsed;
5372 } else {
5373 standbyDelay = 1; // must be > 0
5374 }
5375 DLOG("standby delay %u, elapsed %u\n",
5376 standbyDelay, (uint32_t) elapsed);
5377 }
5378 }
5379 params->ecWakeTimer = standbyDelay;
5380 } else if (kIOPMSleepPhase2 == sleepPhase) {
5381 // A sleep that does not enable the sleep timer will reset
5382 // the standby delay adjustment.
5383 _standbyTimerResetSeconds = 0;
5384 }
5385 break;
5386 }
5387
5388 done:
5389 return found;
5390 }
5391
5392 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5393
5394 void
evaluateSystemSleepPolicyEarly(void)5395 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5396 {
5397 // Evaluate early (priority interest phase), before drivers sleep.
5398
5399 DLOG("%s\n", __FUNCTION__);
5400 removeProperty(kIOPMSystemSleepParametersKey);
5401
5402 // Full wake resets the standby timer delay adjustment
5403 if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5404 _standbyTimerResetSeconds = 0;
5405 }
5406
5407 hibernateDisabled = false;
5408 hibernateMode = 0;
5409 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5410
5411 // Save for late evaluation if sleep is aborted
5412 bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5413
5414 if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5415 &hibernateMode)) {
5416 if (!hibernateRetry &&
5417 ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5418 kIOPMSleepAttributeHibernateSetup) == 0)) {
5419 // skip hibernate setup
5420 hibernateDisabled = true;
5421 }
5422 }
5423
5424 // Publish IOPMSystemSleepType
5425 uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5426 if (sleepType == kIOPMSleepTypeInvalid) {
5427 // no sleep policy
5428 sleepType = kIOPMSleepTypeNormalSleep;
5429 if (hibernateMode & kIOHibernateModeOn) {
5430 sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5431 kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5432 }
5433 } else if ((sleepType == kIOPMSleepTypeStandby) &&
5434 (gEarlySystemSleepParams.ecPoweroffTimer)) {
5435 // report the lowest possible sleep state
5436 sleepType = kIOPMSleepTypePowerOff;
5437 }
5438
5439 setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5440 }
5441
5442 void
evaluateSystemSleepPolicyFinal(void)5443 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5444 {
5445 IOPMSystemSleepParameters params;
5446 OSSharedPtr<OSData> paramsData;
5447 bool wakeNow;
5448 // Evaluate sleep policy after sleeping drivers but before platform sleep.
5449
5450 DLOG("%s\n", __FUNCTION__);
5451
5452 bzero(¶ms, sizeof(params));
5453 wakeNow = false;
5454 if (evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase2, &hibernateMode)) {
5455 if ((kIOPMSleepTypeStandby == params.sleepType)
5456 && gIOHibernateStandbyDisabled && gSleepPolicyVars
5457 && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5458 & gSleepPolicyVars->sleepFactors))) {
5459 standbyNixed = true;
5460 wakeNow = true;
5461 }
5462 if (wakeNow
5463 || ((hibernateDisabled || hibernateAborted) &&
5464 (getSleepTypeAttributes(params.sleepType) &
5465 kIOPMSleepAttributeHibernateSetup))) {
5466 // Final evaluation picked a state requiring hibernation,
5467 // but hibernate isn't going to proceed. Arm a short sleep using
5468 // the early non-hibernate sleep parameters.
5469 bcopy(&gEarlySystemSleepParams, ¶ms, sizeof(params));
5470 params.sleepType = kIOPMSleepTypeAbortedSleep;
5471 params.ecWakeTimer = 1;
5472 if (standbyNixed) {
5473 resetTimers = true;
5474 } else {
5475 // Set hibernateRetry flag to force hibernate setup on the
5476 // next sleep.
5477 hibernateRetry = true;
5478 }
5479 DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5480 params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5481 } else {
5482 hibernateRetry = false;
5483 }
5484
5485 if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5486 resetTimers = false;
5487 }
5488
5489 paramsData = OSData::withValue(params);
5490 if (paramsData) {
5491 setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5492 }
5493
5494 if (getSleepTypeAttributes(params.sleepType) &
5495 kIOPMSleepAttributeHibernateSleep) {
5496 // Disable sleep to force hibernation
5497 gIOHibernateMode &= ~kIOHibernateModeSleep;
5498 }
5499 }
5500 }
5501
5502 bool
getHibernateSettings(uint32_t * hibernateModePtr,uint32_t * hibernateFreeRatio,uint32_t * hibernateFreeTime)5503 IOPMrootDomain::getHibernateSettings(
5504 uint32_t * hibernateModePtr,
5505 uint32_t * hibernateFreeRatio,
5506 uint32_t * hibernateFreeTime )
5507 {
5508 // Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5509 // has updated the hibernateDisabled flag.
5510
5511 bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5512 getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5513 getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5514 if (hibernateDisabled) {
5515 *hibernateModePtr = 0;
5516 } else if (gSleepPolicyHandler) {
5517 *hibernateModePtr = hibernateMode;
5518 }
5519 DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5520 return ok;
5521 }
5522
5523 bool
getSleepOption(const char * key,uint32_t * option)5524 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5525 {
5526 OSSharedPtr<OSObject> optionsProp;
5527 OSDictionary * optionsDict;
5528 OSSharedPtr<OSObject> obj;
5529 OSNumber * num;
5530 bool ok = false;
5531
5532 optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5533 optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5534
5535 if (optionsDict) {
5536 obj.reset(optionsDict->getObject(key), OSRetain);
5537 }
5538 if (!obj) {
5539 obj = copyProperty(key);
5540 }
5541 if (obj) {
5542 if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5543 *option = num->unsigned32BitValue();
5544 ok = true;
5545 } else if (OSDynamicCast(OSBoolean, obj.get())) {
5546 *option = (obj == kOSBooleanTrue) ? 1 : 0;
5547 ok = true;
5548 }
5549 }
5550
5551 return ok;
5552 }
5553 #endif /* HIBERNATION */
5554
5555 IOReturn
getSystemSleepType(uint32_t * sleepType,uint32_t * standbyTimer)5556 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5557 {
5558 #if HIBERNATION
5559 IOPMSystemSleepParameters params;
5560 uint32_t hibMode = 0;
5561 bool ok;
5562
5563 if (gIOPMWorkLoop->inGate() == false) {
5564 IOReturn ret = gIOPMWorkLoop->runAction(
5565 OSMemberFunctionCast(IOWorkLoop::Action, this,
5566 &IOPMrootDomain::getSystemSleepType),
5567 (OSObject *) this,
5568 (void *) sleepType, (void *) standbyTimer);
5569 return ret;
5570 }
5571
5572 getSleepOption(kIOHibernateModeKey, &hibMode);
5573 bzero(¶ms, sizeof(params));
5574
5575 ok = evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase0, &hibMode);
5576 if (ok) {
5577 *sleepType = params.sleepType;
5578 if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5579 !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5580 DLOG("Standby delay is not set\n");
5581 *standbyTimer = 0;
5582 }
5583 return kIOReturnSuccess;
5584 }
5585 #endif
5586
5587 return kIOReturnUnsupported;
5588 }
5589
5590 // MARK: -
5591 // MARK: Shutdown and Restart
5592
5593 //******************************************************************************
5594 // handlePlatformHaltRestart
5595 //
5596 //******************************************************************************
5597
5598 // Phases while performing shutdown/restart
5599 typedef enum {
5600 kNotifyDone = 0x00,
5601 kNotifyPriorityClients = 0x10,
5602 kNotifyPowerPlaneDrivers = 0x20,
5603 kNotifyHaltRestartAction = 0x30,
5604 kQuiescePM = 0x40,
5605 } shutdownPhase_t;
5606
5607
5608 struct HaltRestartApplierContext {
5609 IOPMrootDomain * RootDomain;
5610 unsigned long PowerState;
5611 IOPMPowerFlags PowerFlags;
5612 UInt32 MessageType;
5613 UInt32 Counter;
5614 const char * LogString;
5615 shutdownPhase_t phase;
5616
5617 IOServiceInterestHandler handler;
5618 } gHaltRestartCtx;
5619
5620 const char *
shutdownPhase2String(shutdownPhase_t phase)5621 shutdownPhase2String(shutdownPhase_t phase)
5622 {
5623 switch (phase) {
5624 case kNotifyDone:
5625 return "Notifications completed";
5626 case kNotifyPriorityClients:
5627 return "Notifying priority clients";
5628 case kNotifyPowerPlaneDrivers:
5629 return "Notifying power plane drivers";
5630 case kNotifyHaltRestartAction:
5631 return "Notifying HaltRestart action handlers";
5632 case kQuiescePM:
5633 return "Quiescing PM";
5634 default:
5635 return "Unknown";
5636 }
5637 }
5638
5639 static void
platformHaltRestartApplier(OSObject * object,void * context)5640 platformHaltRestartApplier( OSObject * object, void * context )
5641 {
5642 IOPowerStateChangeNotification notify;
5643 HaltRestartApplierContext * ctx;
5644 AbsoluteTime startTime, elapsedTime;
5645 uint32_t deltaTime;
5646
5647 ctx = (HaltRestartApplierContext *) context;
5648
5649 _IOServiceInterestNotifier * notifier;
5650 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5651 memset(¬ify, 0, sizeof(notify));
5652 notify.powerRef = (void *)(uintptr_t)ctx->Counter;
5653 notify.returnValue = 0;
5654 notify.stateNumber = ctx->PowerState;
5655 notify.stateFlags = ctx->PowerFlags;
5656
5657 if (notifier) {
5658 ctx->handler = notifier->handler;
5659 }
5660
5661 clock_get_uptime(&startTime);
5662 ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)¬ify );
5663 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5664
5665 if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5666 LOG("%s handler %p took %u ms\n",
5667 ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5668 halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5669 }
5670
5671 ctx->handler = NULL;
5672 ctx->Counter++;
5673 }
5674
5675 static void
quiescePowerTreeCallback(void * target,void * param)5676 quiescePowerTreeCallback( void * target, void * param )
5677 {
5678 IOLockLock(gPMHaltLock);
5679 gPMQuiesced = true;
5680 thread_wakeup(param);
5681 IOLockUnlock(gPMHaltLock);
5682 }
5683
5684 void
handlePlatformHaltRestart(UInt32 pe_type)5685 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5686 {
5687 AbsoluteTime startTime, elapsedTime;
5688 uint32_t deltaTime;
5689 bool nvramSync = false;
5690
5691 memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5692 gHaltRestartCtx.RootDomain = this;
5693
5694 clock_get_uptime(&startTime);
5695 switch (pe_type) {
5696 case kPEHaltCPU:
5697 case kPEUPSDelayHaltCPU:
5698 gHaltRestartCtx.PowerState = OFF_STATE;
5699 gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5700 gHaltRestartCtx.LogString = "PowerOff";
5701 nvramSync = true;
5702 break;
5703
5704 case kPERestartCPU:
5705 gHaltRestartCtx.PowerState = RESTART_STATE;
5706 gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5707 gHaltRestartCtx.LogString = "Restart";
5708 nvramSync = true;
5709 break;
5710
5711 case kPEPagingOff:
5712 gHaltRestartCtx.PowerState = ON_STATE;
5713 gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5714 gHaltRestartCtx.LogString = "PagingOff";
5715 IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5716 #if HIBERNATION
5717 IOHibernateSystemRestart();
5718 #endif
5719 break;
5720
5721 default:
5722 return;
5723 }
5724
5725 if (nvramSync) {
5726 PESyncNVRAM();
5727 }
5728
5729 gHaltRestartCtx.phase = kNotifyPriorityClients;
5730 // Notify legacy clients
5731 applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5732
5733 // For normal shutdown, turn off File Server Mode.
5734 if (kPEHaltCPU == pe_type) {
5735 OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5736 OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5737 if (setting && num) {
5738 setPMSetting(setting.get(), num.get());
5739 }
5740 }
5741
5742 if (kPEPagingOff != pe_type) {
5743 gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5744 // Notify in power tree order
5745 notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5746 }
5747
5748 gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5749 #if defined(XNU_TARGET_OS_OSX)
5750 IOCPURunPlatformHaltRestartActions(pe_type);
5751 #else /* !defined(XNU_TARGET_OS_OSX) */
5752 if (kPEPagingOff != pe_type) {
5753 IOCPURunPlatformHaltRestartActions(pe_type);
5754 }
5755 #endif /* !defined(XNU_TARGET_OS_OSX) */
5756
5757 // Wait for PM to quiesce
5758 if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5759 gHaltRestartCtx.phase = kQuiescePM;
5760 AbsoluteTime quiesceTime = mach_absolute_time();
5761
5762 IOLockLock(gPMHaltLock);
5763 gPMQuiesced = false;
5764 if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5765 kIOReturnSuccess) {
5766 while (!gPMQuiesced) {
5767 IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5768 }
5769 }
5770 IOLockUnlock(gPMHaltLock);
5771 deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5772 DLOG("PM quiesce took %u ms\n", deltaTime);
5773 halt_log_enter("Quiesce", NULL, elapsedTime);
5774 }
5775 gHaltRestartCtx.phase = kNotifyDone;
5776
5777 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5778 LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5779
5780 halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5781
5782 deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5783 LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5784
5785 if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5786 printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5787 }
5788
5789 checkShutdownTimeout();
5790 }
5791
5792 bool
checkShutdownTimeout()5793 IOPMrootDomain::checkShutdownTimeout()
5794 {
5795 AbsoluteTime elapsedTime;
5796 uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5797
5798 if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5799 return true;
5800 }
5801 return false;
5802 }
5803
5804 void
panicWithShutdownLog(uint32_t timeoutInMs)5805 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5806 {
5807 if (gHaltLog) {
5808 if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5809 halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5810 }
5811 panic("%s timed out in phase '%s'. Total %d ms:%s",
5812 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5813 } else {
5814 panic("%s timed out in phase \'%s\'. Total %d ms",
5815 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5816 }
5817 }
5818
5819 //******************************************************************************
5820 // shutdownSystem
5821 //
5822 //******************************************************************************
5823
5824 IOReturn
shutdownSystem(void)5825 IOPMrootDomain::shutdownSystem( void )
5826 {
5827 return kIOReturnUnsupported;
5828 }
5829
5830 //******************************************************************************
5831 // restartSystem
5832 //
5833 //******************************************************************************
5834
5835 IOReturn
restartSystem(void)5836 IOPMrootDomain::restartSystem( void )
5837 {
5838 return kIOReturnUnsupported;
5839 }
5840
5841 // MARK: -
5842 // MARK: System Capability
5843
5844 //******************************************************************************
5845 // tagPowerPlaneService
5846 //
5847 // Running on PM work loop thread.
5848 //******************************************************************************
5849
5850 void
tagPowerPlaneService(IOService * service,IOPMActions * actions,IOPMPowerStateIndex maxPowerState)5851 IOPMrootDomain::tagPowerPlaneService(
5852 IOService * service,
5853 IOPMActions * actions,
5854 IOPMPowerStateIndex maxPowerState )
5855 {
5856 uint32_t flags = 0;
5857
5858 memset(actions, 0, sizeof(*actions));
5859 actions->target = this;
5860
5861 if (service == this) {
5862 actions->actionPowerChangeStart =
5863 OSMemberFunctionCast(
5864 IOPMActionPowerChangeStart, this,
5865 &IOPMrootDomain::handleOurPowerChangeStart);
5866
5867 actions->actionPowerChangeDone =
5868 OSMemberFunctionCast(
5869 IOPMActionPowerChangeDone, this,
5870 &IOPMrootDomain::handleOurPowerChangeDone);
5871
5872 actions->actionPowerChangeOverride =
5873 OSMemberFunctionCast(
5874 IOPMActionPowerChangeOverride, this,
5875 &IOPMrootDomain::overrideOurPowerChange);
5876 return;
5877 }
5878
5879 #if DISPLAY_WRANGLER_PRESENT
5880 if (NULL != service->metaCast("IODisplayWrangler")) {
5881 // XXX should this really retain?
5882 wrangler.reset(service, OSRetain);
5883 wrangler->registerInterest(gIOGeneralInterest,
5884 &displayWranglerNotification, this, NULL);
5885
5886 // found the display wrangler, check for any display assertions already created
5887 if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5888 DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5889 wrangler->setIgnoreIdleTimer( true );
5890 }
5891 flags |= kPMActionsFlagIsDisplayWrangler;
5892 }
5893 #endif /* DISPLAY_WRANGLER_PRESENT */
5894
5895 if (service->propertyExists("IOPMStrictTreeOrder")) {
5896 flags |= kPMActionsFlagIsGraphicsDriver;
5897 }
5898 if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5899 flags |= kPMActionsFlagIsAudioDriver;
5900 }
5901
5902 // Find the power connection object that is a child of the PCI host
5903 // bridge, and has a graphics/audio device attached below. Mark the
5904 // power branch for delayed child notifications.
5905
5906 if (flags) {
5907 IORegistryEntry * child = service;
5908 IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
5909
5910 while (child != this) {
5911 if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
5912 // Skip delaying notifications and clamping power on external graphics and audio devices.
5913 DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
5914 flags = 0;
5915 break;
5916 }
5917 if ((parent == pciHostBridgeDriver) ||
5918 (parent == this)) {
5919 if (OSDynamicCast(IOPowerConnection, child)) {
5920 IOPowerConnection * conn = (IOPowerConnection *) child;
5921 conn->delayChildNotification = true;
5922 DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
5923 }
5924 break;
5925 }
5926 child = parent;
5927 parent = child->getParentEntry(gIOPowerPlane);
5928 }
5929 }
5930
5931 OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
5932 if (prop) {
5933 OSNumber * num = OSDynamicCast(OSNumber, prop.get());
5934 if (num) {
5935 actions->darkWakePowerState = num->unsigned32BitValue();
5936 if (actions->darkWakePowerState < maxPowerState) {
5937 flags |= kPMActionsFlagHasDarkWakePowerState;
5938 }
5939 }
5940 }
5941
5942
5943 if (flags) {
5944 DLOG("%s tag flags %x\n", service->getName(), flags);
5945 actions->flags |= flags;
5946 actions->actionPowerChangeOverride =
5947 OSMemberFunctionCast(
5948 IOPMActionPowerChangeOverride, this,
5949 &IOPMrootDomain::overridePowerChangeForService);
5950
5951 if (flags & kPMActionsFlagIsDisplayWrangler) {
5952 actions->actionActivityTickle =
5953 OSMemberFunctionCast(
5954 IOPMActionActivityTickle, this,
5955 &IOPMrootDomain::handleActivityTickleForDisplayWrangler);
5956
5957 actions->actionUpdatePowerClient =
5958 OSMemberFunctionCast(
5959 IOPMActionUpdatePowerClient, this,
5960 &IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
5961 }
5962 return;
5963 }
5964
5965 // Locate the first PCI host bridge for PMTrace.
5966 if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
5967 IOService * provider = service->getProvider();
5968 if (OSDynamicCast(IOPlatformDevice, provider) &&
5969 provider->inPlane(gIODTPlane)) {
5970 pciHostBridgeDevice.reset(provider, OSNoRetain);
5971 pciHostBridgeDriver.reset(service, OSNoRetain);
5972 DLOG("PMTrace found PCI host bridge %s->%s\n",
5973 provider->getName(), service->getName());
5974 }
5975 }
5976
5977 // Tag top-level PCI devices. The order of PMinit() call does not
5978 // change across boots and is used as the PCI bit number.
5979 if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
5980 // Would prefer to check built-in property, but tagPowerPlaneService()
5981 // is called before pciDevice->registerService().
5982 IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
5983 if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
5984 int bit = pmTracer->recordTopLevelPCIDevice( service );
5985 if (bit >= 0) {
5986 // Save the assigned bit for fast lookup.
5987 actions->flags |= (bit & kPMActionsPCIBitNumberMask);
5988
5989 actions->actionPowerChangeStart =
5990 OSMemberFunctionCast(
5991 IOPMActionPowerChangeStart, this,
5992 &IOPMrootDomain::handlePowerChangeStartForPCIDevice);
5993
5994 actions->actionPowerChangeDone =
5995 OSMemberFunctionCast(
5996 IOPMActionPowerChangeDone, this,
5997 &IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
5998 }
5999 }
6000 }
6001 }
6002
6003 //******************************************************************************
6004 // PM actions for root domain
6005 //******************************************************************************
6006
6007 void
overrideOurPowerChange(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6008 IOPMrootDomain::overrideOurPowerChange(
6009 IOService * service,
6010 IOPMActions * actions,
6011 const IOPMRequest * request,
6012 IOPMPowerStateIndex * inOutPowerState,
6013 IOPMPowerChangeFlags * inOutChangeFlags )
6014 {
6015 uint32_t changeFlags = *inOutChangeFlags;
6016 uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6017 uint32_t currentPowerState = (uint32_t) getPowerState();
6018
6019 if (request->getTag() == 0) {
6020 // Set a tag for any request that originates from IOServicePM
6021 (const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6022 }
6023
6024 DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6025 getPowerStateString(currentPowerState),
6026 getPowerStateString(desiredPowerState),
6027 _currentCapability, changeFlags,
6028 request->getTag());
6029
6030
6031 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6032 /*
6033 * ASBM send lowBattery notifications every 1 second until the device
6034 * enters hibernation. This queues up multiple sleep requests.
6035 * After the device wakes from hibernation, none of these previously
6036 * queued sleep requests are valid.
6037 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6038 * and is cleared at the very last point in sleep.
6039 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6040 * lowBatteryCondition is invalid
6041 */
6042 if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6043 if (!lowBatteryCondition) {
6044 DLOG("Duplicate lowBattery sleep");
6045 *inOutChangeFlags |= kIOPMNotDone;
6046 return;
6047 }
6048 }
6049 #endif
6050
6051 if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6052 // Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6053 *inOutChangeFlags |= kIOPMNotDone;
6054 return;
6055 }
6056
6057 if (changeFlags & kIOPMParentInitiated) {
6058 // Root parent is permanently pegged at max power,
6059 // a parent initiated power change is unexpected.
6060 *inOutChangeFlags |= kIOPMNotDone;
6061 return;
6062 }
6063
6064 if (desiredPowerState < currentPowerState) {
6065 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6066 // Root domain is dropping power state from ON->SLEEP.
6067 // If system is in full wake, first enter dark wake by
6068 // converting the power drop to a capability change.
6069 // Once in dark wake, transition to sleep state ASAP.
6070
6071 darkWakeToSleepASAP = true;
6072
6073 // Drop graphics and audio capability
6074 _desiredCapability &= ~(
6075 kIOPMSystemCapabilityGraphics |
6076 kIOPMSystemCapabilityAudio);
6077
6078 // Convert to capability change (ON->ON)
6079 *inOutPowerState = getRUN_STATE();
6080 *inOutChangeFlags |= kIOPMSynchronize;
6081
6082 // Revert device desire from SLEEP to ON
6083 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6084 } else {
6085 // System is already in dark wake, ok to drop power state.
6086 // Broadcast root power down to entire tree.
6087 *inOutChangeFlags |= kIOPMRootChangeDown;
6088 }
6089 } else if (desiredPowerState > currentPowerState) {
6090 if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6091 // Broadcast power up when waking from sleep, but not for the
6092 // initial power change at boot by checking for cpu capability.
6093 *inOutChangeFlags |= kIOPMRootChangeUp;
6094 }
6095 }
6096 }
6097
6098 void
handleOurPowerChangeStart(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex newPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6099 IOPMrootDomain::handleOurPowerChangeStart(
6100 IOService * service,
6101 IOPMActions * actions,
6102 const IOPMRequest * request,
6103 IOPMPowerStateIndex newPowerState,
6104 IOPMPowerChangeFlags * inOutChangeFlags )
6105 {
6106 IOPMRequestTag requestTag = request->getTag();
6107 IOPMRequestTag sleepReason;
6108
6109 uint32_t changeFlags = *inOutChangeFlags;
6110 uint32_t currentPowerState = (uint32_t) getPowerState();
6111 bool publishSleepReason = false;
6112
6113 // Check if request has a valid sleep reason
6114 sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6115 if (sleepReason < kIOPMSleepReasonClamshell) {
6116 sleepReason = kIOPMSleepReasonIdle;
6117 }
6118
6119 _systemTransitionType = kSystemTransitionNone;
6120 _systemMessageClientMask = 0;
6121 capabilityLoss = false;
6122 toldPowerdCapWillChange = false;
6123
6124 // Emergency notifications may arrive after the initial sleep request
6125 // has been queued. Override the sleep reason so powerd and others can
6126 // treat this as an emergency sleep.
6127 if (lowBatteryCondition) {
6128 sleepReason = kIOPMSleepReasonLowPower;
6129 } else if (thermalEmergencyState) {
6130 sleepReason = kIOPMSleepReasonThermalEmergency;
6131 }
6132
6133 // 1. Explicit capability change.
6134 if (changeFlags & kIOPMSynchronize) {
6135 if (newPowerState == ON_STATE) {
6136 if (changeFlags & kIOPMSyncNoChildNotify) {
6137 _systemTransitionType = kSystemTransitionNewCapClient;
6138 } else {
6139 _systemTransitionType = kSystemTransitionCapability;
6140 }
6141 }
6142 }
6143 // 2. Going to sleep (cancellation still possible).
6144 else if (newPowerState < currentPowerState) {
6145 _systemTransitionType = kSystemTransitionSleep;
6146 }
6147 // 3. Woke from (idle or demand) sleep.
6148 else if (!systemBooting &&
6149 (changeFlags & kIOPMSelfInitiated) &&
6150 (newPowerState > currentPowerState)) {
6151 _systemTransitionType = kSystemTransitionWake;
6152 _desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6153
6154 // Early exit from dark wake to full (e.g. LID open)
6155 if (kFullWakeReasonNone != fullWakeReason) {
6156 _desiredCapability |= (
6157 kIOPMSystemCapabilityGraphics |
6158 kIOPMSystemCapabilityAudio);
6159
6160 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6161 if (fullWakeReason == kFullWakeReasonLocalUser) {
6162 darkWakeExit = true;
6163 darkWakeToSleepASAP = false;
6164 setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6165 kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6166 }
6167 #endif
6168 }
6169 #if HIBERNATION
6170 IOHibernateSetWakeCapabilities(_desiredCapability);
6171 #endif
6172 }
6173
6174 // Update pending wake capability at the beginning of every
6175 // state transition (including synchronize). This will become
6176 // the current capability at the end of the transition.
6177
6178 if (kSystemTransitionSleep == _systemTransitionType) {
6179 _pendingCapability = 0;
6180 capabilityLoss = true;
6181 } else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6182 _pendingCapability = _desiredCapability |
6183 kIOPMSystemCapabilityCPU |
6184 kIOPMSystemCapabilityNetwork;
6185
6186 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6187 _pendingCapability |= kIOPMSystemCapabilityAudio;
6188 }
6189
6190 if ((kSystemTransitionCapability == _systemTransitionType) &&
6191 (_pendingCapability == _currentCapability)) {
6192 // Cancel the PM state change.
6193 _systemTransitionType = kSystemTransitionNone;
6194 *inOutChangeFlags |= kIOPMNotDone;
6195 }
6196 if (__builtin_popcount(_pendingCapability) <
6197 __builtin_popcount(_currentCapability)) {
6198 capabilityLoss = true;
6199 }
6200 }
6201
6202 // 1. Capability change.
6203 if (kSystemTransitionCapability == _systemTransitionType) {
6204 // Dark to Full transition.
6205 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6206 tracePoint( kIOPMTracePointDarkWakeExit );
6207
6208 #if defined(XNU_TARGET_OS_OSX)
6209 // rdar://problem/65627936
6210 // When a dark->full wake promotion is scheduled before an ON->SLEEP
6211 // power state drop, invalidate any request to drop power state already
6212 // in the queue, including the override variant, unless full wake cannot
6213 // be sustained. Any power state drop queued after this SustainFullWake
6214 // request will not be affected.
6215 if (checkSystemCanSustainFullWake()) {
6216 changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6217 }
6218 #endif
6219
6220 willEnterFullWake();
6221 }
6222
6223 // Full to Dark transition.
6224 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6225 // Clear previous stats
6226 IOLockLock(pmStatsLock);
6227 if (pmStatsAppResponses) {
6228 pmStatsAppResponses = OSArray::withCapacity(5);
6229 }
6230 IOLockUnlock(pmStatsLock);
6231
6232 tracePoint( kIOPMTracePointDarkWakeEntry );
6233 *inOutChangeFlags |= kIOPMSyncTellPowerDown;
6234 _systemMessageClientMask = kSystemMessageClientPowerd |
6235 kSystemMessageClientLegacyApp;
6236
6237 // rdar://15971327
6238 // Prevent user active transitions before notifying clients
6239 // that system will sleep.
6240 preventTransitionToUserActive(true);
6241
6242 IOService::setAdvisoryTickleEnable( false );
6243
6244 // Publish the sleep reason for full to dark wake
6245 publishSleepReason = true;
6246 lastSleepReason = fullToDarkReason = sleepReason;
6247
6248 // Publish a UUID for the Sleep --> Wake cycle
6249 handlePublishSleepWakeUUID(true);
6250 if (sleepDelaysReport) {
6251 clock_get_uptime(&ts_sleepStart);
6252 DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6253 }
6254
6255 darkWakeExit = false;
6256 }
6257 }
6258 // 2. System sleep.
6259 else if (kSystemTransitionSleep == _systemTransitionType) {
6260 // Beginning of a system sleep transition.
6261 // Cancellation is still possible.
6262 tracePoint( kIOPMTracePointSleepStarted );
6263
6264 _systemMessageClientMask = kSystemMessageClientAll;
6265 if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6266 _systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6267 }
6268 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6269 // Kernel priority clients are only notified on the initial
6270 // transition to full wake, so don't notify them unless system
6271 // has gained graphics capability since the last system wake.
6272 _systemMessageClientMask &= ~kSystemMessageClientKernel;
6273 } else {
6274 // System was in full wake, but the downwards power transition is driven
6275 // by a request that originates from IOServicePM, so it isn't tagged with
6276 // a valid system sleep reason.
6277 if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6278 // Publish the same reason for full to dark
6279 sleepReason = fullToDarkReason;
6280 }
6281 }
6282 #if HIBERNATION
6283 gIOHibernateState = 0;
6284 #endif
6285
6286 // Record the reason for dark wake back to sleep
6287 // System may not have ever achieved full wake
6288
6289 publishSleepReason = true;
6290 lastSleepReason = sleepReason;
6291 if (sleepDelaysReport) {
6292 clock_get_uptime(&ts_sleepStart);
6293 DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6294 }
6295 }
6296 // 3. System wake.
6297 else if (kSystemTransitionWake == _systemTransitionType) {
6298 tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6299 // Clear stats about sleep
6300
6301 if (AOT_STATE == newPowerState) {
6302 _pendingCapability = 0;
6303 }
6304
6305 if (AOT_STATE == currentPowerState) {
6306 // Wake events are no longer accepted after waking to AOT_STATE.
6307 // Re-enable wake event acceptance to append wake events claimed
6308 // during the AOT to ON_STATE transition.
6309 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6310 }
6311
6312 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6313 willEnterFullWake();
6314 }
6315 }
6316
6317 // The only location where the sleep reason is published. At this point
6318 // sleep can still be cancelled, but sleep reason should be published
6319 // early for logging purposes.
6320
6321 if (publishSleepReason) {
6322 static const char * IOPMSleepReasons[] =
6323 {
6324 kIOPMClamshellSleepKey,
6325 kIOPMPowerButtonSleepKey,
6326 kIOPMSoftwareSleepKey,
6327 kIOPMOSSwitchHibernationKey,
6328 kIOPMIdleSleepKey,
6329 kIOPMLowPowerSleepKey,
6330 kIOPMThermalEmergencySleepKey,
6331 kIOPMMaintenanceSleepKey,
6332 kIOPMSleepServiceExitKey,
6333 kIOPMDarkWakeThermalEmergencyKey,
6334 kIOPMNotificationWakeExitKey
6335 };
6336
6337 // Record sleep cause in IORegistry
6338 uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6339 if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6340 DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6341 setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6342 }
6343 }
6344
6345 if ((kSystemTransitionNone != _systemTransitionType) &&
6346 (kSystemTransitionNewCapClient != _systemTransitionType)) {
6347 _systemStateGeneration++;
6348 systemDarkWake = false;
6349
6350 DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6351 getPowerStateString(currentPowerState),
6352 getPowerStateString((uint32_t) newPowerState),
6353 _currentCapability, _pendingCapability,
6354 *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6355 requestTag);
6356 }
6357
6358 if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6359 panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6360 }
6361 if (_aotNow && (ON_STATE == newPowerState)) {
6362 WAKEEVENT_LOCK();
6363 aotShouldExit(false, true);
6364 WAKEEVENT_UNLOCK();
6365 aotExit(false);
6366 }
6367 }
6368
6369 void
handleOurPowerChangeDone(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex oldPowerState,IOPMPowerChangeFlags changeFlags)6370 IOPMrootDomain::handleOurPowerChangeDone(
6371 IOService * service,
6372 IOPMActions * actions,
6373 const IOPMRequest * request,
6374 IOPMPowerStateIndex oldPowerState,
6375 IOPMPowerChangeFlags changeFlags )
6376 {
6377 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6378 _systemTransitionType = kSystemTransitionNone;
6379 return;
6380 }
6381
6382 if (_systemTransitionType != kSystemTransitionNone) {
6383 uint32_t currentPowerState = (uint32_t) getPowerState();
6384
6385 if (changeFlags & kIOPMNotDone) {
6386 // Power down was cancelled or vetoed.
6387 _pendingCapability = _currentCapability;
6388 lastSleepReason = 0;
6389
6390 // When sleep is cancelled or reverted, don't report
6391 // the target (lower) power state as the previous state.
6392 oldPowerState = currentPowerState;
6393
6394 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6395 CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6396 #if defined(XNU_TARGET_OS_OSX)
6397 pmPowerStateQueue->submitPowerEvent(
6398 kPowerEventPolicyStimulus,
6399 (void *) kStimulusDarkWakeReentry,
6400 _systemStateGeneration );
6401 #else /* !defined(XNU_TARGET_OS_OSX) */
6402 // On embedded, there are no factors that can prolong a
6403 // "darkWake" when a power down is vetoed. We need to
6404 // promote to "fullWake" at least once so that factors
6405 // that prevent idle sleep can assert themselves if required
6406 pmPowerStateQueue->submitPowerEvent(
6407 kPowerEventPolicyStimulus,
6408 (void *) kStimulusDarkWakeActivityTickle);
6409 #endif /* !defined(XNU_TARGET_OS_OSX) */
6410 }
6411
6412 // Revert device desire to max.
6413 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6414 } else {
6415 // Send message on dark wake to full wake promotion.
6416 // tellChangeUp() handles the normal SLEEP->ON case.
6417
6418 if (kSystemTransitionCapability == _systemTransitionType) {
6419 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6420 lastSleepReason = 0; // stop logging wrangler tickles
6421 tellClients(kIOMessageSystemHasPoweredOn);
6422 }
6423 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6424 // Going dark, reset full wake state
6425 // userIsActive will be cleared by wrangler powering down
6426 fullWakeReason = kFullWakeReasonNone;
6427
6428 if (ts_sleepStart) {
6429 clock_get_uptime(&wake2DarkwakeDelay);
6430 SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6431 DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6432 ts_sleepStart = 0;
6433 }
6434 }
6435 }
6436
6437 // Reset state after exiting from dark wake.
6438
6439 if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6440 CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6441 darkWakeMaintenance = false;
6442 darkWakeToSleepASAP = false;
6443 pciCantSleepValid = false;
6444 darkWakeSleepService = false;
6445
6446 if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6447 // Remove the influence of display power assertion
6448 // before next system wake.
6449 if (wrangler) {
6450 wrangler->changePowerStateForRootDomain(
6451 kWranglerPowerStateMin );
6452 }
6453 removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6454 }
6455 }
6456
6457 // Entered dark mode.
6458
6459 if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6460 (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6461 // Queue an evaluation of whether to remain in dark wake,
6462 // and for how long. This serves the purpose of draining
6463 // any assertions from the queue.
6464
6465 pmPowerStateQueue->submitPowerEvent(
6466 kPowerEventPolicyStimulus,
6467 (void *) kStimulusDarkWakeEntry,
6468 _systemStateGeneration );
6469 }
6470 }
6471
6472 DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6473 getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6474 _currentCapability, _pendingCapability,
6475 changeFlags, _systemStateGeneration, _systemMessageClientMask,
6476 request->getTag());
6477
6478 if ((currentPowerState == ON_STATE) && pmAssertions) {
6479 pmAssertions->reportCPUBitAccounting();
6480 }
6481
6482 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6483 displayWakeCnt++;
6484 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6485 if (clamshellExists && fullWakeThreadCall) {
6486 AbsoluteTime deadline;
6487 clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6488 thread_call_enter_delayed(fullWakeThreadCall, deadline);
6489 }
6490 #endif
6491 } else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6492 darkWakeCnt++;
6493 }
6494
6495 // Update current system capability.
6496 if (_currentCapability != _pendingCapability) {
6497 _currentCapability = _pendingCapability;
6498 }
6499
6500 // Update highest system capability.
6501
6502 _highestCapability |= _currentCapability;
6503
6504 if (darkWakePostTickle &&
6505 (kSystemTransitionWake == _systemTransitionType) &&
6506 (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6507 kDarkWakeFlagPromotionLate) {
6508 darkWakePostTickle = false;
6509 reportUserInput();
6510 } else if (darkWakeExit) {
6511 requestFullWake( kFullWakeReasonLocalUser );
6512 }
6513
6514 // Reset tracepoint at completion of capability change,
6515 // completion of wake transition, and aborted sleep transition.
6516
6517 if ((_systemTransitionType == kSystemTransitionCapability) ||
6518 (_systemTransitionType == kSystemTransitionWake) ||
6519 ((_systemTransitionType == kSystemTransitionSleep) &&
6520 (changeFlags & kIOPMNotDone))) {
6521 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6522 tracePoint( kIOPMTracePointSystemUp );
6523 }
6524
6525 _systemTransitionType = kSystemTransitionNone;
6526 _systemMessageClientMask = 0;
6527 toldPowerdCapWillChange = false;
6528
6529 darkWakeLogClamp = false;
6530
6531 if (lowBatteryCondition) {
6532 privateSleepSystem(kIOPMSleepReasonLowPower);
6533 } else if (thermalEmergencyState) {
6534 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6535 } else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6536 // Request for full wake is removed while system is waking up to full wake
6537 DLOG("DisplayOn fullwake request is removed\n");
6538 handleSetDisplayPowerOn(false);
6539 }
6540
6541 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6542 pmPowerStateQueue->submitPowerEvent(
6543 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6544 }
6545 }
6546 }
6547
6548 //******************************************************************************
6549 // PM actions for graphics and audio.
6550 //******************************************************************************
6551
6552 void
overridePowerChangeForService(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6553 IOPMrootDomain::overridePowerChangeForService(
6554 IOService * service,
6555 IOPMActions * actions,
6556 const IOPMRequest * request,
6557 IOPMPowerStateIndex * inOutPowerState,
6558 IOPMPowerChangeFlags * inOutChangeFlags )
6559 {
6560 uint32_t powerState = (uint32_t) *inOutPowerState;
6561 uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6562 const uint32_t actionFlags = actions->flags;
6563
6564 if (kSystemTransitionNone == _systemTransitionType) {
6565 // Not in midst of a system transition.
6566 // Do not set kPMActionsStatePowerClamped.
6567 } else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6568 bool enableClamp = false;
6569
6570 // For most drivers, enable the clamp during ON->Dark transition
6571 // which has the kIOPMSynchronize flag set in changeFlags.
6572 if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6573 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6574 (changeFlags & kIOPMSynchronize)) {
6575 enableClamp = true;
6576 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6577 ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6578 ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6579 (changeFlags & kIOPMSynchronize)) {
6580 enableClamp = true;
6581 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6582 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6583 (changeFlags & kIOPMSynchronize)) {
6584 enableClamp = true;
6585 } else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6586 (_systemTransitionType == kSystemTransitionSleep)) {
6587 // For graphics drivers, clamp power when entering
6588 // system sleep. Not when dropping to dark wake.
6589 enableClamp = true;
6590 }
6591
6592 if (enableClamp) {
6593 actions->state |= kPMActionsStatePowerClamped;
6594 DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6595 service->getName(), service->getRegistryEntryID(),
6596 _pendingCapability, powerState, changeFlags);
6597 }
6598 } else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6599 bool disableClamp = false;
6600
6601 if ((actionFlags & (
6602 kPMActionsFlagIsDisplayWrangler |
6603 kPMActionsFlagIsGraphicsDriver)) &&
6604 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6605 disableClamp = true;
6606 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6607 (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6608 disableClamp = true;
6609 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6610 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6611 disableClamp = true;
6612 }
6613
6614 if (disableClamp) {
6615 actions->state &= ~kPMActionsStatePowerClamped;
6616 DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6617 service->getName(), service->getRegistryEntryID(),
6618 _pendingCapability, powerState, changeFlags);
6619 }
6620 }
6621
6622 if (actions->state & kPMActionsStatePowerClamped) {
6623 uint32_t maxPowerState = 0;
6624
6625 // Determine the max power state allowed when clamp is enabled
6626 if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6627 // Parent intiated power state changes
6628 if ((service->getPowerState() > maxPowerState) &&
6629 (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6630 maxPowerState++;
6631
6632 // Remove lingering effects of any tickle before entering
6633 // dark wake. It will take a new tickle to return to full
6634 // wake, so the existing tickle state is useless.
6635
6636 if (changeFlags & kIOPMDomainDidChange) {
6637 *inOutChangeFlags |= kIOPMExpireIdleTimer;
6638 }
6639 } else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6640 maxPowerState++;
6641 } else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6642 maxPowerState = actions->darkWakePowerState;
6643 }
6644 } else {
6645 // Deny all self-initiated changes when power is limited.
6646 // Wrangler tickle should never defeat the limiter.
6647 maxPowerState = service->getPowerState();
6648 }
6649
6650 if (powerState > maxPowerState) {
6651 DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6652 service->getName(), service->getRegistryEntryID(),
6653 powerState, maxPowerState, changeFlags);
6654 *inOutPowerState = maxPowerState;
6655
6656 if (darkWakePostTickle &&
6657 (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6658 (changeFlags & kIOPMDomainWillChange) &&
6659 ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6660 kDarkWakeFlagPromotionEarly)) {
6661 darkWakePostTickle = false;
6662 reportUserInput();
6663 }
6664 }
6665
6666 if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6667 if (darkWakeLogClamp) {
6668 AbsoluteTime now;
6669 uint64_t nsec;
6670
6671 clock_get_uptime(&now);
6672 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6673 absolutetime_to_nanoseconds(now, &nsec);
6674 DLOG("dark wake power clamped after %u ms\n",
6675 ((int)((nsec) / NSEC_PER_MSEC)));
6676 }
6677 darkWakePowerClamped = true;
6678 }
6679 }
6680 }
6681
6682 void
handleActivityTickleForDisplayWrangler(IOService * service,IOPMActions * actions)6683 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6684 IOService * service,
6685 IOPMActions * actions )
6686 {
6687 #if DISPLAY_WRANGLER_PRESENT
6688 // Warning: Not running in PM work loop context - don't modify state !!!
6689 // Trap tickle directed to IODisplayWrangler while running with graphics
6690 // capability suppressed.
6691
6692 assert(service == wrangler);
6693
6694 clock_get_uptime(&userActivityTime);
6695 bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6696 || (lastSleepReason == kIOPMSleepReasonMaintenance)
6697 || (lastSleepReason == kIOPMSleepReasonSoftware));
6698 if (aborting) {
6699 userActivityCount++;
6700 DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6701 userActivityCount, lastSleepReason);
6702 }
6703
6704 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6705 DLOG("display wrangler tickled\n");
6706 if (kIOLogPMRootDomain & gIOKitDebug) {
6707 OSReportWithBacktrace("Dark wake display tickle");
6708 }
6709 if (pmPowerStateQueue) {
6710 pmPowerStateQueue->submitPowerEvent(
6711 kPowerEventPolicyStimulus,
6712 (void *) kStimulusDarkWakeActivityTickle,
6713 true /* set wake type */ );
6714 }
6715 }
6716 #endif /* DISPLAY_WRANGLER_PRESENT */
6717 }
6718
6719 void
handleUpdatePowerClientForDisplayWrangler(IOService * service,IOPMActions * actions,const OSSymbol * powerClient,IOPMPowerStateIndex oldPowerState,IOPMPowerStateIndex newPowerState)6720 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6721 IOService * service,
6722 IOPMActions * actions,
6723 const OSSymbol * powerClient,
6724 IOPMPowerStateIndex oldPowerState,
6725 IOPMPowerStateIndex newPowerState )
6726 {
6727 #if DISPLAY_WRANGLER_PRESENT
6728 assert(service == wrangler);
6729
6730 // This function implements half of the user active detection
6731 // by monitoring changes to the display wrangler's device desire.
6732 //
6733 // User becomes active when either:
6734 // 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6735 // in max power state. This desire change in absence of a power state
6736 // change is detected within. This handles the case when user becomes
6737 // active while the display is already lit by setDisplayPowerOn().
6738 //
6739 // 2. Power state change to max, and DeviceDesire is also at max.
6740 // Handled by displayWranglerNotification().
6741 //
6742 // User becomes inactive when DeviceDesire drops to sleep state or below.
6743
6744 DLOG("wrangler %s (ps %u, %u->%u)\n",
6745 powerClient->getCStringNoCopy(),
6746 (uint32_t) service->getPowerState(),
6747 (uint32_t) oldPowerState, (uint32_t) newPowerState);
6748
6749 if (powerClient == gIOPMPowerClientDevice) {
6750 if ((newPowerState > oldPowerState) &&
6751 (newPowerState == kWranglerPowerStateMax) &&
6752 (service->getPowerState() == kWranglerPowerStateMax)) {
6753 evaluatePolicy( kStimulusEnterUserActiveState );
6754 } else if ((newPowerState < oldPowerState) &&
6755 (newPowerState <= kWranglerPowerStateSleep)) {
6756 evaluatePolicy( kStimulusLeaveUserActiveState );
6757 }
6758 }
6759
6760 if (newPowerState <= kWranglerPowerStateSleep) {
6761 evaluatePolicy( kStimulusDisplayWranglerSleep );
6762 } else if (newPowerState == kWranglerPowerStateMax) {
6763 evaluatePolicy( kStimulusDisplayWranglerWake );
6764 }
6765 #endif /* DISPLAY_WRANGLER_PRESENT */
6766 }
6767
6768 //******************************************************************************
6769 // User active state management
6770 //******************************************************************************
6771
6772 void
preventTransitionToUserActive(bool prevent)6773 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6774 {
6775 #if DISPLAY_WRANGLER_PRESENT
6776 _preventUserActive = prevent;
6777 if (wrangler && !_preventUserActive) {
6778 // Allowing transition to user active, but the wrangler may have
6779 // already powered ON in case of sleep cancel/revert. Poll the
6780 // same conditions checked for in displayWranglerNotification()
6781 // to bring the user active state up to date.
6782
6783 if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6784 (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6785 kWranglerPowerStateMax)) {
6786 evaluatePolicy( kStimulusEnterUserActiveState );
6787 }
6788 }
6789 #endif /* DISPLAY_WRANGLER_PRESENT */
6790 }
6791
6792 //******************************************************************************
6793 // Approve usage of delayed child notification by PM.
6794 //******************************************************************************
6795
6796 bool
shouldDelayChildNotification(IOService * service)6797 IOPMrootDomain::shouldDelayChildNotification(
6798 IOService * service )
6799 {
6800 if ((kFullWakeReasonNone == fullWakeReason) &&
6801 (kSystemTransitionWake == _systemTransitionType)) {
6802 DLOG("%s: delay child notify\n", service->getName());
6803 return true;
6804 }
6805 return false;
6806 }
6807
6808 //******************************************************************************
6809 // PM actions for PCI device.
6810 //******************************************************************************
6811
6812 void
handlePowerChangeStartForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags * inOutChangeFlags)6813 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6814 IOService * service,
6815 IOPMActions * actions,
6816 const IOPMRequest * request,
6817 IOPMPowerStateIndex powerState,
6818 IOPMPowerChangeFlags * inOutChangeFlags )
6819 {
6820 pmTracer->tracePCIPowerChange(
6821 PMTraceWorker::kPowerChangeStart,
6822 service, *inOutChangeFlags,
6823 (actions->flags & kPMActionsPCIBitNumberMask));
6824 }
6825
6826 void
handlePowerChangeDoneForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags changeFlags)6827 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6828 IOService * service,
6829 IOPMActions * actions,
6830 const IOPMRequest * request,
6831 IOPMPowerStateIndex powerState,
6832 IOPMPowerChangeFlags changeFlags )
6833 {
6834 pmTracer->tracePCIPowerChange(
6835 PMTraceWorker::kPowerChangeCompleted,
6836 service, changeFlags,
6837 (actions->flags & kPMActionsPCIBitNumberMask));
6838 }
6839
6840 //******************************************************************************
6841 // registerInterest
6842 //
6843 // Override IOService::registerInterest() for root domain clients.
6844 //******************************************************************************
6845
6846 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6847 {
6848 friend class IOPMrootDomain;
6849 OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6850
6851 protected:
6852 uint32_t ackTimeoutCnt;
6853 uint32_t msgType; // Last type seen by the message filter
6854 uint32_t lastSleepWakeMsgType;
6855 uint32_t msgIndex;
6856 uint32_t maxMsgDelayMS;
6857 uint32_t maxAckDelayMS;
6858 uint64_t msgAbsTime;
6859 uint64_t uuid0;
6860 uint64_t uuid1;
6861 OSSharedPtr<const OSSymbol> identifier;
6862 OSSharedPtr<const OSSymbol> clientName;
6863 };
6864
OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier,_IOServiceInterestNotifier)6865 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
6866
6867 OSSharedPtr<IONotifier>
6868 IOPMrootDomain::registerInterest(
6869 const OSSymbol * typeOfInterest,
6870 IOServiceInterestHandler handler,
6871 void * target, void * ref )
6872 {
6873 IOPMServiceInterestNotifier* notifier;
6874 bool isSystemCapabilityClient;
6875 bool isKernelCapabilityClient;
6876 IOReturn rc = kIOReturnError;
6877
6878 isSystemCapabilityClient = typeOfInterest &&
6879 typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
6880
6881 isKernelCapabilityClient = typeOfInterest &&
6882 typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
6883
6884 if (isSystemCapabilityClient) {
6885 typeOfInterest = gIOAppPowerStateInterest;
6886 }
6887
6888 notifier = new IOPMServiceInterestNotifier;
6889 if (!notifier) {
6890 return NULL;
6891 }
6892
6893 if (notifier->init()) {
6894 rc = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
6895 }
6896 if (rc != kIOReturnSuccess) {
6897 OSSafeReleaseNULL(notifier);
6898 return NULL;
6899 }
6900
6901 notifier->ackTimeoutCnt = 0;
6902
6903 if (pmPowerStateQueue) {
6904 if (isSystemCapabilityClient) {
6905 notifier->retain();
6906 if (pmPowerStateQueue->submitPowerEvent(
6907 kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
6908 notifier->release();
6909 }
6910 }
6911
6912 if (isKernelCapabilityClient) {
6913 notifier->retain();
6914 if (pmPowerStateQueue->submitPowerEvent(
6915 kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
6916 notifier->release();
6917 }
6918 }
6919 }
6920
6921 OSSharedPtr<OSData> data;
6922 uint8_t *uuid = NULL;
6923 OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
6924 if (kext) {
6925 data = kext->copyUUID();
6926 }
6927 if (data && (data->getLength() == sizeof(uuid_t))) {
6928 uuid = (uint8_t *)(data->getBytesNoCopy());
6929
6930 notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
6931 ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
6932 ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
6933 notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
6934 ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
6935 ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
6936
6937 notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
6938 }
6939 return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
6940 }
6941
6942 //******************************************************************************
6943 // systemMessageFilter
6944 //
6945 //******************************************************************************
6946
6947 bool
systemMessageFilter(void * object,void * arg1,void * arg2,void * arg3)6948 IOPMrootDomain::systemMessageFilter(
6949 void * object, void * arg1, void * arg2, void * arg3 )
6950 {
6951 const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
6952 bool isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
6953 bool isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
6954 bool isCapClient = false;
6955 bool allow = false;
6956 OSBoolean **waitForReply = (typeof(waitForReply))arg3;
6957 IOPMServiceInterestNotifier *notifier;
6958
6959 notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
6960
6961 do {
6962 // When powerd and kernel priority clients register capability interest,
6963 // the power tree is sync'ed to inform those clients about the current
6964 // system capability. Only allow capability change messages during sync.
6965 if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
6966 (!isCapMsg || !_joinedCapabilityClients ||
6967 !_joinedCapabilityClients->containsObject((OSObject *) object))) {
6968 break;
6969 }
6970
6971 // Capability change message for powerd and kernel clients
6972 if (isCapMsg) {
6973 // Kernel priority clients
6974 if ((context->notifyType == kNotifyPriority) ||
6975 (context->notifyType == kNotifyCapabilityChangePriority)) {
6976 isCapClient = true;
6977 }
6978
6979 // powerd will maintain two client registrations with root domain.
6980 // isCapPowerd will be TRUE for any message targeting the powerd
6981 // exclusive (capability change) interest registration.
6982 if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
6983 isCapClient = true;
6984 }
6985 }
6986
6987 if (isCapClient) {
6988 IOPMSystemCapabilityChangeParameters * capArgs =
6989 (IOPMSystemCapabilityChangeParameters *) arg2;
6990
6991 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6992 capArgs->fromCapabilities = 0;
6993 capArgs->toCapabilities = _currentCapability;
6994 capArgs->changeFlags = 0;
6995 } else {
6996 capArgs->fromCapabilities = _currentCapability;
6997 capArgs->toCapabilities = _pendingCapability;
6998
6999 if (context->isPreChange) {
7000 capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
7001 } else {
7002 capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
7003 }
7004
7005 if (isCapPowerd && context->isPreChange) {
7006 toldPowerdCapWillChange = true;
7007 }
7008 }
7009
7010 // App level capability change messages must only go to powerd.
7011 // Wait for response post-change if capabilitiy is increasing.
7012 // Wait for response pre-change if capability is decreasing.
7013
7014 if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7015 ((capabilityLoss && context->isPreChange) ||
7016 (!capabilityLoss && !context->isPreChange))) {
7017 *waitForReply = kOSBooleanTrue;
7018 }
7019
7020 allow = true;
7021 break;
7022 }
7023
7024 // powerd will always receive CanSystemSleep, even for a demand sleep.
7025 // It will also have a final chance to veto sleep after all clients
7026 // have responded to SystemWillSleep
7027
7028 if ((kIOMessageCanSystemSleep == context->messageType) ||
7029 (kIOMessageSystemWillNotSleep == context->messageType)) {
7030 if (isCapPowerd) {
7031 allow = true;
7032 break;
7033 }
7034
7035 // Demand sleep, don't ask apps for permission
7036 if (context->changeFlags & kIOPMSkipAskPowerDown) {
7037 break;
7038 }
7039 }
7040
7041 if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7042 if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7043 (fullToDarkReason == kIOPMSleepReasonIdle)) {
7044 allow = true;
7045 }
7046 break;
7047 }
7048
7049 // Drop capability change messages for legacy clients.
7050 // Drop legacy system sleep messages for powerd capability interest.
7051 if (isCapMsg || isCapPowerd) {
7052 break;
7053 }
7054
7055 // Not a capability change message.
7056 // Perform message filtering based on _systemMessageClientMask.
7057
7058 if ((context->notifyType == kNotifyApps) &&
7059 (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7060 if (!notifier) {
7061 break;
7062 }
7063
7064 if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7065 (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7066 break; // drop any duplicate WillPowerOn for AOT devices
7067 }
7068
7069 allow = true;
7070
7071 if (waitForReply) {
7072 if (notifier->ackTimeoutCnt >= 3) {
7073 *waitForReply = kOSBooleanFalse;
7074 } else {
7075 *waitForReply = kOSBooleanTrue;
7076 }
7077 }
7078 } else if ((context->notifyType == kNotifyPriority) &&
7079 (_systemMessageClientMask & kSystemMessageClientKernel)) {
7080 allow = true;
7081 }
7082
7083 // Check sleep/wake message ordering
7084 if (allow) {
7085 if (context->messageType == kIOMessageSystemWillSleep ||
7086 context->messageType == kIOMessageSystemWillPowerOn ||
7087 context->messageType == kIOMessageSystemHasPoweredOn) {
7088 notifier->lastSleepWakeMsgType = context->messageType;
7089 }
7090 }
7091 } while (false);
7092
7093 if (allow && isCapMsg && _joinedCapabilityClients) {
7094 _joinedCapabilityClients->removeObject((OSObject *) object);
7095 if (_joinedCapabilityClients->getCount() == 0) {
7096 DMSG("destroyed capability client set %p\n",
7097 OBFUSCATE(_joinedCapabilityClients.get()));
7098 _joinedCapabilityClients.reset();
7099 }
7100 }
7101 if (notifier) {
7102 // Record the last seen message type even if the message is dropped
7103 // for traceFilteredNotification().
7104 notifier->msgType = context->messageType;
7105 }
7106
7107 return allow;
7108 }
7109
7110 //******************************************************************************
7111 // setMaintenanceWakeCalendar
7112 //
7113 //******************************************************************************
7114
7115 IOReturn
setMaintenanceWakeCalendar(const IOPMCalendarStruct * calendar)7116 IOPMrootDomain::setMaintenanceWakeCalendar(
7117 const IOPMCalendarStruct * calendar )
7118 {
7119 OSSharedPtr<OSData> data;
7120 IOReturn ret = 0;
7121
7122 if (!calendar) {
7123 return kIOReturnBadArgument;
7124 }
7125
7126 data = OSData::withValue(*calendar);
7127 if (!data) {
7128 return kIOReturnNoMemory;
7129 }
7130
7131 if (kPMCalendarTypeMaintenance == calendar->selector) {
7132 ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7133 } else if (kPMCalendarTypeSleepService == calendar->selector) {
7134 ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7135 }
7136
7137 return ret;
7138 }
7139
7140 // MARK: -
7141 // MARK: Display Wrangler
7142
7143 //******************************************************************************
7144 // displayWranglerNotification
7145 //
7146 // Handle the notification when the IODisplayWrangler changes power state.
7147 //******************************************************************************
7148
7149 IOReturn
displayWranglerNotification(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgument,vm_size_t argSize)7150 IOPMrootDomain::displayWranglerNotification(
7151 void * target, void * refCon,
7152 UInt32 messageType, IOService * service,
7153 void * messageArgument, vm_size_t argSize )
7154 {
7155 #if DISPLAY_WRANGLER_PRESENT
7156 IOPMPowerStateIndex displayPowerState;
7157 IOPowerStateChangeNotification * params =
7158 (IOPowerStateChangeNotification *) messageArgument;
7159
7160 if ((messageType != kIOMessageDeviceWillPowerOff) &&
7161 (messageType != kIOMessageDeviceHasPoweredOn)) {
7162 return kIOReturnUnsupported;
7163 }
7164
7165 ASSERT_GATED();
7166 if (!gRootDomain) {
7167 return kIOReturnUnsupported;
7168 }
7169
7170 displayPowerState = params->stateNumber;
7171 DLOG("wrangler %s ps %d\n",
7172 getIOMessageString(messageType), (uint32_t) displayPowerState);
7173
7174 switch (messageType) {
7175 case kIOMessageDeviceWillPowerOff:
7176 // Display wrangler has dropped power due to display idle
7177 // or force system sleep.
7178 //
7179 // 4 Display ON kWranglerPowerStateMax
7180 // 3 Display Dim kWranglerPowerStateDim
7181 // 2 Display Sleep kWranglerPowerStateSleep
7182 // 1 Not visible to user
7183 // 0 Not visible to user kWranglerPowerStateMin
7184
7185 if (displayPowerState <= kWranglerPowerStateSleep) {
7186 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7187 }
7188 break;
7189
7190 case kIOMessageDeviceHasPoweredOn:
7191 // Display wrangler has powered on due to user activity
7192 // or wake from sleep.
7193
7194 if (kWranglerPowerStateMax == displayPowerState) {
7195 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7196
7197 // See comment in handleUpdatePowerClientForDisplayWrangler
7198 if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7199 kWranglerPowerStateMax) {
7200 gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7201 }
7202 }
7203 break;
7204 }
7205 #endif /* DISPLAY_WRANGLER_PRESENT */
7206 return kIOReturnUnsupported;
7207 }
7208
7209 //******************************************************************************
7210 // reportUserInput
7211 //
7212 //******************************************************************************
7213
7214 void
updateUserActivity(void)7215 IOPMrootDomain::updateUserActivity( void )
7216 {
7217 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7218 clock_get_uptime(&userActivityTime);
7219 bool aborting = ((lastSleepReason == kIOPMSleepReasonSoftware)
7220 || (lastSleepReason == kIOPMSleepReasonIdle)
7221 || (lastSleepReason == kIOPMSleepReasonMaintenance));
7222 if (aborting) {
7223 userActivityCount++;
7224 DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7225 }
7226 #endif
7227 }
7228 void
reportUserInput(void)7229 IOPMrootDomain::reportUserInput( void )
7230 {
7231 if (wrangler) {
7232 wrangler->activityTickle(0, 0);
7233 }
7234 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7235 // Update user activity
7236 updateUserActivity();
7237
7238 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7239 // update user active abs time
7240 clock_get_uptime(&gUserActiveAbsTime);
7241 pmPowerStateQueue->submitPowerEvent(
7242 kPowerEventPolicyStimulus,
7243 (void *) kStimulusDarkWakeActivityTickle,
7244 true /* set wake type */ );
7245 }
7246 #endif
7247 }
7248
7249 void
requestUserActive(IOService * device,const char * reason)7250 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7251 {
7252 #if DISPLAY_WRANGLER_PRESENT
7253 if (wrangler) {
7254 wrangler->activityTickle(0, 0);
7255 }
7256 #else
7257 if (!device) {
7258 DLOG("requestUserActive: device is null\n");
7259 return;
7260 }
7261 OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7262 uint64_t registryID = device->getRegistryEntryID();
7263
7264 if (!deviceName || !registryID) {
7265 DLOG("requestUserActive: no device name or registry entry\n");
7266 return;
7267 }
7268 const char *name = deviceName->getCStringNoCopy();
7269 char payload[128];
7270 snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7271 DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7272 messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7273 #endif
7274 }
7275
7276 //******************************************************************************
7277 // latchDisplayWranglerTickle
7278 //******************************************************************************
7279
7280 bool
latchDisplayWranglerTickle(bool latch)7281 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7282 {
7283 #if DISPLAY_WRANGLER_PRESENT
7284 if (latch) {
7285 if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7286 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7287 !checkSystemCanSustainFullWake()) {
7288 // Currently in dark wake, and not transitioning to full wake.
7289 // Full wake is unsustainable, so latch the tickle to prevent
7290 // the display from lighting up momentarily.
7291 wranglerTickled = true;
7292 } else {
7293 wranglerTickled = false;
7294 }
7295 } else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7296 wranglerTickled = false;
7297
7298 pmPowerStateQueue->submitPowerEvent(
7299 kPowerEventPolicyStimulus,
7300 (void *) kStimulusDarkWakeActivityTickle );
7301 }
7302
7303 return wranglerTickled;
7304 #else /* ! DISPLAY_WRANGLER_PRESENT */
7305 return false;
7306 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7307 }
7308
7309 //******************************************************************************
7310 // setDisplayPowerOn
7311 //
7312 // For root domain user client
7313 //******************************************************************************
7314
7315 void
setDisplayPowerOn(uint32_t options)7316 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7317 {
7318 pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7319 (void *) NULL, options );
7320 }
7321
7322 // MARK: -
7323 // MARK: System PM Policy
7324
7325 //******************************************************************************
7326 // checkSystemSleepAllowed
7327 //
7328 //******************************************************************************
7329
7330 bool
checkSystemSleepAllowed(IOOptionBits options,uint32_t sleepReason)7331 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7332 uint32_t sleepReason )
7333 {
7334 uint32_t err = 0;
7335
7336 // Conditions that prevent idle and demand system sleep.
7337
7338 do {
7339 if (gSleepDisabledFlag) {
7340 err = kPMConfigPreventSystemSleep;
7341 break;
7342 }
7343
7344 if (userDisabledAllSleep) {
7345 err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7346 break;
7347 }
7348
7349 if (systemBooting || systemShutdown || gWillShutdown) {
7350 err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7351 break;
7352 }
7353
7354 if (options == 0) {
7355 break;
7356 }
7357
7358 // Conditions above pegs the system at full wake.
7359 // Conditions below prevent system sleep but does not prevent
7360 // dark wake, and must be called from gated context.
7361
7362 #if !CONFIG_SLEEP
7363 err = kPMConfigPreventSystemSleep; // 3. config does not support sleep
7364 break;
7365 #endif
7366
7367 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7368 break; // always sleep on low battery or when in thermal warning/emergency state
7369 }
7370
7371 if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7372 break; // always sleep on dark wake thermal emergencies
7373 }
7374
7375 if (preventSystemSleepList->getCount() != 0) {
7376 err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7377 break;
7378 }
7379
7380 if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7381 kIOPMDriverAssertionLevelOn) {
7382 err = kPMCPUAssertion; // 5. CPU assertion
7383 break;
7384 }
7385
7386 if (pciCantSleepValid) {
7387 if (pciCantSleepFlag) {
7388 err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7389 }
7390 break;
7391 } else if (sleepSupportedPEFunction &&
7392 CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7393 IOReturn ret;
7394 OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7395 ret = getPlatform()->callPlatformFunction(
7396 sleepSupportedPEFunction.get(), false,
7397 NULL, NULL, NULL, NULL);
7398 pciCantSleepValid = true;
7399 pciCantSleepFlag = false;
7400 if ((platformSleepSupport & kPCICantSleep) ||
7401 ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7402 err = 6; // 6. PCI card does not support PM
7403 pciCantSleepFlag = true;
7404 break;
7405 }
7406 }
7407 }while (false);
7408
7409 if (err) {
7410 DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7411 return false;
7412 }
7413 return true;
7414 }
7415
7416 bool
checkSystemSleepEnabled(void)7417 IOPMrootDomain::checkSystemSleepEnabled( void )
7418 {
7419 return checkSystemSleepAllowed(0, 0);
7420 }
7421
7422 bool
checkSystemCanSleep(uint32_t sleepReason)7423 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7424 {
7425 ASSERT_GATED();
7426 return checkSystemSleepAllowed(1, sleepReason);
7427 }
7428
7429 //******************************************************************************
7430 // checkSystemCanSustainFullWake
7431 //******************************************************************************
7432
7433 bool
checkSystemCanSustainFullWake(void)7434 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7435 {
7436 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7437 // Low battery wake, or received a low battery notification
7438 // while system is awake. This condition will persist until
7439 // the following wake.
7440 return false;
7441 }
7442
7443 if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7444 // Graphics state is unknown and external display might not be probed.
7445 // Do not incorporate state that requires graphics to be in max power
7446 // such as desktopMode or clamshellDisabled.
7447
7448 if (!acAdaptorConnected) {
7449 DLOG("full wake check: no AC\n");
7450 return false;
7451 }
7452 }
7453 return true;
7454 }
7455
7456 //******************************************************************************
7457 // mustHibernate
7458 //******************************************************************************
7459
7460 #if HIBERNATION
7461
7462 bool
mustHibernate(void)7463 IOPMrootDomain::mustHibernate( void )
7464 {
7465 return lowBatteryCondition || thermalWarningState;
7466 }
7467
7468 #endif /* HIBERNATION */
7469
7470 //******************************************************************************
7471 // AOT
7472 //******************************************************************************
7473
7474 // Tables for accumulated days in year by month, latter used for leap years
7475
7476 static const unsigned int daysbymonth[] =
7477 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7478
7479 static const unsigned int lydaysbymonth[] =
7480 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7481
7482 static int __unused
IOPMConvertSecondsToCalendar(clock_sec_t secs,IOPMCalendarStruct * dt)7483 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7484 {
7485 const unsigned int * dbm = daysbymonth;
7486 clock_sec_t n, x, y, z;
7487
7488 // Calculate seconds, minutes and hours
7489
7490 n = secs % (24 * 3600);
7491 dt->second = n % 60;
7492 n /= 60;
7493 dt->minute = n % 60;
7494 dt->hour = (typeof(dt->hour))(n / 60);
7495
7496 // Calculate day of week
7497
7498 n = secs / (24 * 3600);
7499 // dt->dayWeek = (n + 4) % 7;
7500
7501 // Calculate year
7502 // Rebase from days since Unix epoch (1/1/1970) store in 'n',
7503 // to days since 1/1/1968 to start on 4 year cycle, beginning
7504 // on a leap year.
7505
7506 n += (366 + 365);
7507
7508 // Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7509 // Valid before 2100, since 2100 is not a leap year.
7510
7511 x = n / 1461; // number of 4 year cycles
7512 y = n % 1461; // days into current 4 year cycle
7513 z = 1968 + (4 * x);
7514
7515 // Add in years in the current 4 year cycle
7516
7517 if (y >= 366) {
7518 y -= 366; // days after the leap year
7519 n = y % 365; // days into the current year
7520 z += (1 + y / 365); // years after the past 4-yr cycle
7521 } else {
7522 n = y;
7523 dbm = lydaysbymonth;
7524 }
7525 if (z > 2099) {
7526 return 0;
7527 }
7528
7529 dt->year = (typeof(dt->year))z;
7530
7531 // Adjust remaining days value to start at 1
7532
7533 n += 1;
7534
7535 // Calculate month
7536
7537 for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7538 continue;
7539 }
7540 dt->month = (typeof(dt->month))x;
7541
7542 // Calculate day of month
7543
7544 dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7545
7546 return 1;
7547 }
7548
7549 static clock_sec_t
IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)7550 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7551 {
7552 const unsigned int * dbm = daysbymonth;
7553 long y, secs, days;
7554
7555 if (dt->year < 1970 || dt->month > 12) {
7556 return 0;
7557 }
7558
7559 // Seconds elapsed in the current day
7560
7561 secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7562
7563 // Number of days from 1/1/70 to beginning of current year
7564 // Account for extra day every 4 years starting at 1973
7565
7566 y = dt->year - 1970;
7567 days = (y * 365) + ((y + 1) / 4);
7568
7569 // Change table if current year is a leap year
7570
7571 if ((dt->year % 4) == 0) {
7572 dbm = lydaysbymonth;
7573 }
7574
7575 // Add in days elapsed in the current year
7576
7577 days += (dt->day - 1) + dbm[dt->month - 1];
7578
7579 // Add accumulated days to accumulated seconds
7580
7581 secs += 24 * 3600 * days;
7582
7583 return secs;
7584 }
7585
7586 unsigned long
getRUN_STATE(void)7587 IOPMrootDomain::getRUN_STATE(void)
7588 {
7589 return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7590 }
7591
7592 bool
isAOTMode()7593 IOPMrootDomain::isAOTMode()
7594 {
7595 return _aotNow;
7596 }
7597
7598 IOReturn
setWakeTime(uint64_t wakeContinuousTime)7599 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7600 {
7601 clock_sec_t nowsecs, wakesecs;
7602 clock_usec_t nowmicrosecs, wakemicrosecs;
7603 uint64_t nowAbs, wakeAbs;
7604
7605 clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7606 wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7607 if (wakeAbs < nowAbs) {
7608 printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7609 wakeAbs = nowAbs;
7610 }
7611 wakeAbs -= nowAbs;
7612 absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7613
7614 wakesecs += nowsecs;
7615 wakemicrosecs += nowmicrosecs;
7616 if (wakemicrosecs >= USEC_PER_SEC) {
7617 wakesecs++;
7618 wakemicrosecs -= USEC_PER_SEC;
7619 }
7620 if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7621 wakesecs++;
7622 }
7623
7624 IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7625
7626 if (_aotWakeTimeContinuous != wakeContinuousTime) {
7627 _aotWakeTimeContinuous = wakeContinuousTime;
7628 IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7629 }
7630 _aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7631 _aotWakeTimeUTC = wakesecs;
7632
7633 return kIOReturnSuccess;
7634 }
7635
7636 // assumes WAKEEVENT_LOCK
7637 bool
aotShouldExit(bool checkTimeSet,bool software)7638 IOPMrootDomain::aotShouldExit(bool checkTimeSet, bool software)
7639 {
7640 bool exitNow = false;
7641 const char * reason = "";
7642
7643 if (!_aotNow) {
7644 return false;
7645 }
7646
7647 if (software) {
7648 exitNow = true;
7649 _aotMetrics->softwareRequestCount++;
7650 reason = "software request";
7651 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7652 exitNow = true;
7653 reason = gWakeReasonString;
7654 } else if (checkTimeSet && (kPMCalendarTypeInvalid == _aotWakeTimeCalendar.selector)) {
7655 exitNow = true;
7656 _aotMetrics->noTimeSetCount++;
7657 reason = "flipbook expired";
7658 } else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7659 clock_sec_t sec;
7660 clock_usec_t usec;
7661 clock_get_calendar_microtime(&sec, &usec);
7662 if (_calendarWakeAlarmUTC <= sec) {
7663 exitNow = true;
7664 _aotMetrics->rtcAlarmsCount++;
7665 reason = "user alarm";
7666 }
7667 }
7668 if (exitNow) {
7669 _aotPendingFlags |= kIOPMWakeEventAOTExit;
7670 IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7671 reason,
7672 _aotMetrics->sleepCount,
7673 _aotMetrics->possibleCount,
7674 _aotMetrics->confirmedPossibleCount,
7675 _aotMetrics->rejectedPossibleCount,
7676 _aotMetrics->expiredPossibleCount,
7677 _aotMetrics->noTimeSetCount,
7678 _aotMetrics->rtcAlarmsCount);
7679 }
7680 return exitNow;
7681 }
7682
7683 void
aotExit(bool cps)7684 IOPMrootDomain::aotExit(bool cps)
7685 {
7686 uint32_t savedMessageMask;
7687
7688 ASSERT_GATED();
7689 _aotNow = false;
7690 _aotTasksSuspended = false;
7691 _aotReadyToFullWake = false;
7692 if (_aotTimerScheduled) {
7693 _aotTimerES->cancelTimeout();
7694 _aotTimerScheduled = false;
7695 }
7696 updateTasksSuspend();
7697
7698 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7699 _aotLastWakeTime = 0;
7700 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7701 WAKEEVENT_LOCK();
7702 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7703 gWakeReasonString,
7704 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7705 WAKEEVENT_UNLOCK();
7706 }
7707
7708 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7709
7710 // Preserve the message mask since a system wake transition
7711 // may have already started and initialized the mask.
7712 savedMessageMask = _systemMessageClientMask;
7713 _systemMessageClientMask = kSystemMessageClientLegacyApp;
7714 tellClients(kIOMessageSystemWillPowerOn);
7715 _systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7716
7717 if (cps) {
7718 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7719 }
7720 }
7721
7722 void
aotEvaluate(IOTimerEventSource * timer)7723 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7724 {
7725 bool exitNow;
7726
7727 IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7728
7729 WAKEEVENT_LOCK();
7730 exitNow = aotShouldExit(false, false);
7731 if (timer != NULL) {
7732 _aotTimerScheduled = false;
7733 }
7734 WAKEEVENT_UNLOCK();
7735 if (exitNow) {
7736 aotExit(true);
7737 } else {
7738 #if 0
7739 if (_aotLingerTime) {
7740 uint64_t deadline;
7741 IOLog("aot linger before sleep\n");
7742 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7743 clock_delay_until(deadline);
7744 }
7745 #endif
7746 privateSleepSystem(kIOPMSleepReasonSoftware);
7747 }
7748 }
7749
7750 //******************************************************************************
7751 // adjustPowerState
7752 //
7753 // Conditions that affect our wake/sleep decision has changed.
7754 // If conditions dictate that the system must remain awake, clamp power
7755 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7756 // is TRUE, then remove the power clamp and allow the power state to drop
7757 // to SLEEP_STATE.
7758 //******************************************************************************
7759
7760 void
adjustPowerState(bool sleepASAP)7761 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7762 {
7763 DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7764 getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7765
7766 ASSERT_GATED();
7767
7768 if (_aotNow) {
7769 bool exitNow;
7770
7771 if (AOT_STATE != getPowerState()) {
7772 return;
7773 }
7774 WAKEEVENT_LOCK();
7775 exitNow = aotShouldExit(true, false);
7776 if (!exitNow
7777 && !_aotTimerScheduled
7778 && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7779 _aotTimerScheduled = true;
7780 if (_aotLingerTime) {
7781 _aotTimerES->setTimeout(_aotLingerTime);
7782 } else {
7783 _aotTimerES->setTimeout(800, kMillisecondScale);
7784 }
7785 }
7786 WAKEEVENT_UNLOCK();
7787 if (exitNow) {
7788 aotExit(true);
7789 } else {
7790 _aotReadyToFullWake = true;
7791 if (!_aotTimerScheduled) {
7792 privateSleepSystem(kIOPMSleepReasonSoftware);
7793 }
7794 }
7795 return;
7796 }
7797
7798 if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
7799 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
7800 } else if (sleepASAP) {
7801 changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
7802 }
7803 }
7804
7805 void
handleSetDisplayPowerOn(bool powerOn)7806 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
7807 {
7808 if (powerOn) {
7809 if (!checkSystemCanSustainFullWake()) {
7810 DLOG("System cannot sustain full wake\n");
7811 return;
7812 }
7813
7814 // Force wrangler to max power state. If system is in dark wake
7815 // this alone won't raise the wrangler's power state.
7816 if (wrangler) {
7817 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
7818 }
7819
7820 // System in dark wake, always requesting full wake should
7821 // not have any bad side-effects, even if the request fails.
7822
7823 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
7824 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
7825 requestFullWake( kFullWakeReasonDisplayOn );
7826 }
7827 } else {
7828 // Relenquish desire to power up display.
7829 // Must first transition to state 1 since wrangler doesn't
7830 // power off the displays at state 0. At state 0 the root
7831 // domain is removed from the wrangler's power client list.
7832 if (wrangler) {
7833 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
7834 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
7835 }
7836 }
7837 }
7838
7839 //******************************************************************************
7840 // dispatchPowerEvent
7841 //
7842 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
7843 //******************************************************************************
7844
7845 void
dispatchPowerEvent(uint32_t event,void * arg0,uint64_t arg1)7846 IOPMrootDomain::dispatchPowerEvent(
7847 uint32_t event, void * arg0, uint64_t arg1 )
7848 {
7849 ASSERT_GATED();
7850
7851 switch (event) {
7852 case kPowerEventFeatureChanged:
7853 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7854 messageClients(kIOPMMessageFeatureChange, this);
7855 break;
7856
7857 case kPowerEventReceivedPowerNotification:
7858 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7859 handlePowerNotification((UInt32)(uintptr_t) arg0 );
7860 break;
7861
7862 case kPowerEventSystemBootCompleted:
7863 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7864 if (systemBooting) {
7865 systemBooting = false;
7866
7867 if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
7868 DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
7869 }
7870 if (lowBatteryCondition || thermalEmergencyState) {
7871 if (lowBatteryCondition) {
7872 privateSleepSystem(kIOPMSleepReasonLowPower);
7873 } else {
7874 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
7875 }
7876 // The rest is unnecessary since the system is expected
7877 // to sleep immediately. The following wake will update
7878 // everything.
7879 break;
7880 }
7881
7882 sleepWakeDebugMemAlloc();
7883 saveFailureData2File();
7884
7885 // If lid is closed, re-send lid closed notification
7886 // now that booting is complete.
7887 if (clamshellClosed) {
7888 handlePowerNotification(kLocalEvalClamshellCommand);
7889 }
7890 evaluatePolicy( kStimulusAllowSystemSleepChanged );
7891 }
7892 break;
7893
7894 case kPowerEventSystemShutdown:
7895 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7896 if (kOSBooleanTrue == (OSBoolean *) arg0) {
7897 /* We set systemShutdown = true during shutdown
7898 * to prevent sleep at unexpected times while loginwindow is trying
7899 * to shutdown apps and while the OS is trying to transition to
7900 * complete power of.
7901 *
7902 * Set to true during shutdown, as soon as loginwindow shows
7903 * the "shutdown countdown dialog", through individual app
7904 * termination, and through black screen kernel shutdown.
7905 */
7906 systemShutdown = true;
7907 } else {
7908 /*
7909 * A shutdown was initiated, but then the shutdown
7910 * was cancelled, clearing systemShutdown to false here.
7911 */
7912 systemShutdown = false;
7913 }
7914 break;
7915
7916 case kPowerEventUserDisabledSleep:
7917 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7918 userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
7919 break;
7920
7921 case kPowerEventRegisterSystemCapabilityClient:
7922 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7923
7924 // reset() handles the arg0 == nullptr case for us
7925 systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
7926 /* intentional fall-through */
7927 [[clang::fallthrough]];
7928
7929 case kPowerEventRegisterKernelCapabilityClient:
7930 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7931 if (!_joinedCapabilityClients) {
7932 _joinedCapabilityClients = OSSet::withCapacity(8);
7933 }
7934 if (arg0) {
7935 OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
7936 if (_joinedCapabilityClients) {
7937 _joinedCapabilityClients->setObject(notify.get());
7938 synchronizePowerTree( kIOPMSyncNoChildNotify );
7939 }
7940 }
7941 break;
7942
7943 case kPowerEventPolicyStimulus:
7944 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7945 if (arg0) {
7946 int stimulus = (int)(uintptr_t) arg0;
7947 evaluatePolicy(stimulus, (uint32_t) arg1);
7948 }
7949 break;
7950
7951 case kPowerEventAssertionCreate:
7952 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7953 if (pmAssertions) {
7954 pmAssertions->handleCreateAssertion((OSValueObject<PMAssertStruct> *)arg0);
7955 }
7956 break;
7957
7958
7959 case kPowerEventAssertionRelease:
7960 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7961 if (pmAssertions) {
7962 pmAssertions->handleReleaseAssertion(arg1);
7963 }
7964 break;
7965
7966 case kPowerEventAssertionSetLevel:
7967 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7968 if (pmAssertions) {
7969 pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
7970 }
7971 break;
7972
7973 case kPowerEventQueueSleepWakeUUID:
7974 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7975 handleQueueSleepWakeUUID((OSObject *)arg0);
7976 break;
7977 case kPowerEventPublishSleepWakeUUID:
7978 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7979 handlePublishSleepWakeUUID((bool)arg0);
7980 break;
7981
7982 case kPowerEventSetDisplayPowerOn:
7983 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7984 if (arg1 != 0) {
7985 displayPowerOnRequested = true;
7986 } else {
7987 displayPowerOnRequested = false;
7988 }
7989 handleSetDisplayPowerOn(displayPowerOnRequested);
7990 break;
7991
7992 case kPowerEventPublishWakeType:
7993 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7994
7995 // Don't replace wake type property if already set
7996 if ((arg0 == gIOPMWakeTypeUserKey) ||
7997 !propertyExists(kIOPMRootDomainWakeTypeKey)) {
7998 const char * wakeType = NULL;
7999
8000 if (arg0 == gIOPMWakeTypeUserKey) {
8001 requestUserActive(this, "WakeTypeUser");
8002 wakeType = kIOPMRootDomainWakeTypeUser;
8003 } else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
8004 requestUserActive(this, "WakeTypeAlarm");
8005 wakeType = kIOPMRootDomainWakeTypeAlarm;
8006 } else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
8007 darkWakeSleepService = true;
8008 wakeType = kIOPMRootDomainWakeTypeSleepService;
8009 } else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8010 wakeType = kIOPMRootDomainWakeTypeMaintenance;
8011 }
8012
8013 if (wakeType) {
8014 setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8015 }
8016 }
8017 break;
8018
8019 case kPowerEventAOTEvaluate:
8020 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8021 if (_aotReadyToFullWake) {
8022 aotEvaluate(NULL);
8023 }
8024 break;
8025 }
8026 }
8027
8028 //******************************************************************************
8029 // systemPowerEventOccurred
8030 //
8031 // The power controller is notifying us of a hardware-related power management
8032 // event that we must handle.
8033 //
8034 // systemPowerEventOccurred covers the same functionality that
8035 // receivePowerNotification does; it simply provides a richer API for conveying
8036 // more information.
8037 //******************************************************************************
8038
8039 IOReturn
systemPowerEventOccurred(const OSSymbol * event,uint32_t intValue)8040 IOPMrootDomain::systemPowerEventOccurred(
8041 const OSSymbol *event,
8042 uint32_t intValue)
8043 {
8044 IOReturn attempt = kIOReturnSuccess;
8045 OSSharedPtr<OSNumber> newNumber;
8046
8047 if (!event) {
8048 return kIOReturnBadArgument;
8049 }
8050
8051 newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8052 if (!newNumber) {
8053 return kIOReturnInternalError;
8054 }
8055
8056 attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8057
8058 return attempt;
8059 }
8060
8061 void
setThermalState(OSObject * value)8062 IOPMrootDomain::setThermalState(OSObject *value)
8063 {
8064 OSNumber * num;
8065
8066 if (gIOPMWorkLoop->inGate() == false) {
8067 gIOPMWorkLoop->runAction(
8068 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8069 (OSObject *)this,
8070 (void *)value);
8071
8072 return;
8073 }
8074 if (value && (num = OSDynamicCast(OSNumber, value))) {
8075 thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8076 (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8077 }
8078 }
8079
8080 IOReturn
systemPowerEventOccurred(const OSSymbol * event,OSObject * value)8081 IOPMrootDomain::systemPowerEventOccurred(
8082 const OSSymbol *event,
8083 OSObject *value)
8084 {
8085 OSSharedPtr<OSDictionary> thermalsDict;
8086 bool shouldUpdate = true;
8087
8088 if (!event || !value) {
8089 return kIOReturnBadArgument;
8090 }
8091
8092 // LOCK
8093 // We reuse featuresDict Lock because it already exists and guards
8094 // the very infrequently used publish/remove feature mechanism; so there's zero rsk
8095 // of stepping on that lock.
8096 if (featuresDictLock) {
8097 IOLockLock(featuresDictLock);
8098 }
8099
8100 OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8101 OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8102
8103 if (origThermalsDict) {
8104 thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8105 } else {
8106 thermalsDict = OSDictionary::withCapacity(1);
8107 }
8108
8109 if (!thermalsDict) {
8110 shouldUpdate = false;
8111 goto exit;
8112 }
8113
8114 thermalsDict->setObject(event, value);
8115
8116 setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8117
8118 exit:
8119 // UNLOCK
8120 if (featuresDictLock) {
8121 IOLockUnlock(featuresDictLock);
8122 }
8123
8124 if (shouldUpdate) {
8125 if (event &&
8126 event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8127 setThermalState(value);
8128 }
8129 messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8130 }
8131
8132 return kIOReturnSuccess;
8133 }
8134
8135 //******************************************************************************
8136 // receivePowerNotification
8137 //
8138 // The power controller is notifying us of a hardware-related power management
8139 // event that we must handle. This may be a result of an 'environment' interrupt
8140 // from the power mgt micro.
8141 //******************************************************************************
8142
8143 IOReturn
receivePowerNotification(UInt32 msg)8144 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8145 {
8146 if (msg & kIOPMPowerButton) {
8147 uint32_t currentPhase = pmTracer->getTracePhase();
8148 if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8149 DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8150 swd_flags |= SWD_PWR_BTN_STACKSHOT;
8151 thread_call_enter(powerButtonDown);
8152 } else {
8153 DEBUG_LOG("power button pressed when system is up\n");
8154 }
8155 } else if (msg & kIOPMPowerButtonUp) {
8156 if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8157 swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8158 thread_call_enter(powerButtonUp);
8159 }
8160 } else {
8161 pmPowerStateQueue->submitPowerEvent(
8162 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8163 }
8164 return kIOReturnSuccess;
8165 }
8166
8167 void
handlePowerNotification(UInt32 msg)8168 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8169 {
8170 bool eval_clamshell = false;
8171 bool eval_clamshell_alarm = false;
8172
8173 ASSERT_GATED();
8174
8175 /*
8176 * Local (IOPMrootDomain only) eval clamshell command
8177 */
8178 if (msg & kLocalEvalClamshellCommand) {
8179 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8180 eval_clamshell_alarm = true;
8181
8182 // reset isRTCAlarmWake. This evaluation should happen only once
8183 // on RTC/Alarm wake. Any clamshell events after wake should follow
8184 // the regular evaluation
8185 isRTCAlarmWake = false;
8186 } else {
8187 eval_clamshell = true;
8188 }
8189 }
8190
8191 /*
8192 * Overtemp
8193 */
8194 if (msg & kIOPMOverTemp) {
8195 DLOG("Thermal overtemp message received!\n");
8196 thermalEmergencyState = true;
8197 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8198 }
8199
8200 /*
8201 * Forward DW thermal notification to client, if system is not going to sleep
8202 */
8203 if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8204 DLOG("DarkWake thermal limits message received!\n");
8205 messageClients(kIOPMMessageDarkWakeThermalEmergency);
8206 }
8207
8208 /*
8209 * Sleep Now!
8210 */
8211 if (msg & kIOPMSleepNow) {
8212 privateSleepSystem(kIOPMSleepReasonSoftware);
8213 }
8214
8215 /*
8216 * Power Emergency
8217 */
8218 if (msg & kIOPMPowerEmergency) {
8219 DLOG("Received kIOPMPowerEmergency");
8220 lowBatteryCondition = true;
8221 privateSleepSystem(kIOPMSleepReasonLowPower);
8222 }
8223
8224 /*
8225 * Clamshell OPEN
8226 */
8227 if (msg & kIOPMClamshellOpened) {
8228 DLOG("Clamshell opened\n");
8229 // Received clamshel open message from clamshell controlling driver
8230 // Update our internal state and tell general interest clients
8231 clamshellClosed = false;
8232 clamshellExists = true;
8233
8234 // Don't issue a hid tickle when lid is open and polled on wake
8235 if (msg & kIOPMSetValue) {
8236 setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8237 reportUserInput();
8238 }
8239
8240 // Tell PMCPU
8241 informCPUStateChange(kInformLid, 0);
8242
8243 // Tell general interest clients
8244 sendClientClamshellNotification();
8245
8246 bool aborting = ((lastSleepReason == kIOPMSleepReasonClamshell)
8247 || (lastSleepReason == kIOPMSleepReasonIdle)
8248 || (lastSleepReason == kIOPMSleepReasonMaintenance));
8249 if (aborting) {
8250 userActivityCount++;
8251 }
8252 DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8253 }
8254
8255 /*
8256 * Clamshell CLOSED
8257 * Send the clamshell interest notification since the lid is closing.
8258 */
8259 if (msg & kIOPMClamshellClosed) {
8260 if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8261 clamshellClosed && clamshellExists) {
8262 DLOG("Ignoring redundant Clamshell close event\n");
8263 } else {
8264 DLOG("Clamshell closed\n");
8265 // Received clamshel open message from clamshell controlling driver
8266 // Update our internal state and tell general interest clients
8267 clamshellClosed = true;
8268 clamshellExists = true;
8269
8270 // Ignore all following clamshell close events until the clamshell
8271 // is opened or the system sleeps. When a clamshell close triggers
8272 // a system wake, the lid driver may send us two clamshell close
8273 // events, one for the clamshell close event itself, and a second
8274 // close event when the driver polls the lid state on wake.
8275 clamshellIgnoreClose = true;
8276
8277 // Tell PMCPU
8278 informCPUStateChange(kInformLid, 1);
8279
8280 // Tell general interest clients
8281 sendClientClamshellNotification();
8282
8283 // And set eval_clamshell = so we can attempt
8284 eval_clamshell = true;
8285 }
8286 }
8287
8288 /*
8289 * Set Desktop mode (sent from graphics)
8290 *
8291 * -> reevaluate lid state
8292 */
8293 if (msg & kIOPMSetDesktopMode) {
8294 desktopMode = (0 != (msg & kIOPMSetValue));
8295 msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8296 DLOG("Desktop mode %d\n", desktopMode);
8297
8298 sendClientClamshellNotification();
8299
8300 // Re-evaluate the lid state
8301 eval_clamshell = true;
8302 }
8303
8304 /*
8305 * AC Adaptor connected
8306 *
8307 * -> reevaluate lid state
8308 */
8309 if (msg & kIOPMSetACAdaptorConnected) {
8310 acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8311 msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8312
8313 // Tell CPU PM
8314 informCPUStateChange(kInformAC, !acAdaptorConnected);
8315
8316 // Tell BSD if AC is connected
8317 // 0 == external power source; 1 == on battery
8318 post_sys_powersource(acAdaptorConnected ? 0:1);
8319
8320 sendClientClamshellNotification();
8321
8322 IOUserServer::powerSourceChanged(acAdaptorConnected);
8323
8324 // Re-evaluate the lid state
8325 eval_clamshell = true;
8326
8327 // Lack of AC may have latched a display wrangler tickle.
8328 // This mirrors the hardware's USB wake event latch, where a latched
8329 // USB wake event followed by an AC attach will trigger a full wake.
8330 latchDisplayWranglerTickle( false );
8331
8332 #if HIBERNATION
8333 // AC presence will reset the standy timer delay adjustment.
8334 _standbyTimerResetSeconds = 0;
8335 #endif
8336 if (!userIsActive) {
8337 // Reset userActivityTime when power supply is changed(rdr 13789330)
8338 clock_get_uptime(&userActivityTime);
8339 }
8340 }
8341
8342 /*
8343 * Enable Clamshell (external display disappear)
8344 *
8345 * -> reevaluate lid state
8346 */
8347 if (msg & kIOPMEnableClamshell) {
8348 DLOG("Clamshell enabled\n");
8349
8350 // Re-evaluate the lid state
8351 // System should sleep on external display disappearance
8352 // in lid closed operation.
8353 if (true == clamshellDisabled) {
8354 eval_clamshell = true;
8355
8356 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8357 // Also clear kClamshellSleepDisableInternal when graphics enables
8358 // the clamshell during a full wake. When graphics is behaving as
8359 // expected, this will allow clamshell close to be honored earlier
8360 // rather than waiting for the delayed evaluation.
8361 if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8362 (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8363 CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8364 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8365
8366 // Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8367 // when timer expires which is harmless but useless.
8368 thread_call_cancel(fullWakeThreadCall);
8369 }
8370 #endif
8371 }
8372
8373 clamshellDisabled = false;
8374 sendClientClamshellNotification();
8375 }
8376
8377 /*
8378 * Disable Clamshell (external display appeared)
8379 * We don't bother re-evaluating clamshell state. If the system is awake,
8380 * the lid is probably open.
8381 */
8382 if (msg & kIOPMDisableClamshell) {
8383 DLOG("Clamshell disabled\n");
8384 clamshellDisabled = true;
8385 sendClientClamshellNotification();
8386 }
8387
8388 /*
8389 * Evaluate clamshell and SLEEP if appropriate
8390 */
8391 if (eval_clamshell_alarm && clamshellClosed) {
8392 if (shouldSleepOnRTCAlarmWake()) {
8393 privateSleepSystem(kIOPMSleepReasonClamshell);
8394 }
8395 } else if (eval_clamshell && clamshellClosed) {
8396 if (shouldSleepOnClamshellClosed()) {
8397 privateSleepSystem(kIOPMSleepReasonClamshell);
8398 } else {
8399 evaluatePolicy( kStimulusDarkWakeEvaluate );
8400 }
8401 }
8402
8403 if (msg & kIOPMProModeEngaged) {
8404 int newState = 1;
8405 DLOG("ProModeEngaged\n");
8406 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8407 }
8408
8409 if (msg & kIOPMProModeDisengaged) {
8410 int newState = 0;
8411 DLOG("ProModeDisengaged\n");
8412 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8413 }
8414 }
8415
8416 //******************************************************************************
8417 // evaluatePolicy
8418 //
8419 // Evaluate root-domain policy in response to external changes.
8420 //******************************************************************************
8421
8422 void
evaluatePolicy(int stimulus,uint32_t arg)8423 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8424 {
8425 union {
8426 struct {
8427 int idleSleepEnabled : 1;
8428 int idleSleepDisabled : 1;
8429 int displaySleep : 1;
8430 int sleepDelayChanged : 1;
8431 int evaluateDarkWake : 1;
8432 int adjustPowerState : 1;
8433 int userBecameInactive : 1;
8434 int displaySleepEntry : 1;
8435 } bit;
8436 uint32_t u32;
8437 } flags;
8438
8439
8440 ASSERT_GATED();
8441 flags.u32 = 0;
8442
8443 switch (stimulus) {
8444 case kStimulusDisplayWranglerSleep:
8445 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8446 if (!wranglerPowerOff) {
8447 // wrangler is in sleep state or lower
8448 flags.bit.displaySleep = true;
8449 }
8450 if (!wranglerAsleep) {
8451 // transition from wrangler wake to wrangler sleep
8452 flags.bit.displaySleepEntry = true;
8453 wranglerAsleep = true;
8454 }
8455 break;
8456
8457 case kStimulusDisplayWranglerWake:
8458 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8459 displayIdleForDemandSleep = false;
8460 wranglerPowerOff = false;
8461 wranglerAsleep = false;
8462 break;
8463
8464 case kStimulusEnterUserActiveState:
8465 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8466 if (_preventUserActive) {
8467 DLOG("user active dropped\n");
8468 break;
8469 }
8470 if (!userIsActive) {
8471 userIsActive = true;
8472 userWasActive = true;
8473 clock_get_uptime(&gUserActiveAbsTime);
8474
8475 // Stay awake after dropping demand for display power on
8476 if (kFullWakeReasonDisplayOn == fullWakeReason) {
8477 fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8478 DLOG("User activity while in notification wake\n");
8479 changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8480 }
8481
8482 kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8483 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8484 messageClients(kIOPMMessageUserIsActiveChanged);
8485 }
8486 flags.bit.idleSleepDisabled = true;
8487 break;
8488
8489 case kStimulusLeaveUserActiveState:
8490 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8491 if (userIsActive) {
8492 clock_get_uptime(&gUserInactiveAbsTime);
8493 userIsActive = false;
8494 clock_get_uptime(&userBecameInactiveTime);
8495 flags.bit.userBecameInactive = true;
8496
8497 kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8498 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8499 messageClients(kIOPMMessageUserIsActiveChanged);
8500 }
8501 break;
8502
8503 case kStimulusAggressivenessChanged:
8504 {
8505 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8506 unsigned long aggressiveValue;
8507 uint32_t minutesToIdleSleep = 0;
8508 uint32_t minutesToDisplayDim = 0;
8509 uint32_t minutesDelta = 0;
8510
8511 // Fetch latest display and system sleep slider values.
8512 aggressiveValue = 0;
8513 getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8514 minutesToIdleSleep = (uint32_t) aggressiveValue;
8515
8516 aggressiveValue = 0;
8517 getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8518 minutesToDisplayDim = (uint32_t) aggressiveValue;
8519 DLOG("aggressiveness changed: system %u->%u, display %u\n",
8520 sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8521
8522 DLOG("idle time -> %d ms (ena %d)\n",
8523 idleMilliSeconds, (minutesToIdleSleep != 0));
8524
8525 // How long to wait before sleeping the system once
8526 // the displays turns off is indicated by 'extraSleepDelay'.
8527
8528 if (minutesToIdleSleep > minutesToDisplayDim) {
8529 minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8530 } else if (minutesToIdleSleep == minutesToDisplayDim) {
8531 minutesDelta = 1;
8532 }
8533
8534 if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8535 idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8536 }
8537
8538 if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8539 flags.bit.idleSleepDisabled = true;
8540 idleSleepEnabled = false;
8541 }
8542 #if !defined(XNU_TARGET_OS_OSX)
8543 if (0x7fffffff == minutesToIdleSleep) {
8544 minutesToIdleSleep = idleMilliSeconds / 1000;
8545 }
8546 #endif /* !defined(XNU_TARGET_OS_OSX) */
8547
8548 if (((minutesDelta != extraSleepDelay) ||
8549 (userActivityTime != userActivityTime_prev)) &&
8550 !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8551 flags.bit.sleepDelayChanged = true;
8552 }
8553
8554 if (systemDarkWake && !darkWakeToSleepASAP &&
8555 (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8556 // Reconsider decision to remain in dark wake
8557 flags.bit.evaluateDarkWake = true;
8558 }
8559
8560 sleepSlider = minutesToIdleSleep;
8561 extraSleepDelay = minutesDelta;
8562 userActivityTime_prev = userActivityTime;
8563 } break;
8564
8565 case kStimulusDemandSystemSleep:
8566 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8567 displayIdleForDemandSleep = true;
8568 if (wrangler && wranglerIdleSettings) {
8569 // Request wrangler idle only when demand sleep is triggered
8570 // from full wake.
8571 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8572 wrangler->setProperties(wranglerIdleSettings.get());
8573 DLOG("Requested wrangler idle\n");
8574 }
8575 }
8576 // arg = sleepReason
8577 changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8578 break;
8579
8580 case kStimulusAllowSystemSleepChanged:
8581 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8582 flags.bit.adjustPowerState = true;
8583 break;
8584
8585 case kStimulusDarkWakeActivityTickle:
8586 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8587 // arg == true implies real and not self generated wrangler tickle.
8588 // Update wake type on PM work loop instead of the tickle thread to
8589 // eliminate the possibility of an early tickle clobbering the wake
8590 // type set by the platform driver.
8591 if (arg == true) {
8592 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8593 }
8594
8595 if (!darkWakeExit) {
8596 if (latchDisplayWranglerTickle(true)) {
8597 DLOG("latched tickle\n");
8598 break;
8599 }
8600
8601 darkWakeExit = true;
8602 DLOG("Requesting full wake due to dark wake activity tickle\n");
8603 requestFullWake( kFullWakeReasonLocalUser );
8604 }
8605 break;
8606
8607 case kStimulusDarkWakeEntry:
8608 case kStimulusDarkWakeReentry:
8609 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8610 // Any system transitions since the last dark wake transition
8611 // will invalid the stimulus.
8612
8613 if (arg == _systemStateGeneration) {
8614 DLOG("dark wake entry\n");
8615 systemDarkWake = true;
8616
8617 // Keep wranglerPowerOff an invariant when wrangler is absent
8618 if (wrangler) {
8619 wranglerPowerOff = true;
8620 }
8621
8622 if (kStimulusDarkWakeEntry == stimulus) {
8623 clock_get_uptime(&userBecameInactiveTime);
8624 flags.bit.evaluateDarkWake = true;
8625 if (activitySinceSleep()) {
8626 DLOG("User activity recorded while going to darkwake\n");
8627 reportUserInput();
8628 }
8629 }
8630
8631 // Always accelerate disk spindown while in dark wake,
8632 // even if system does not support/allow sleep.
8633
8634 cancelIdleSleepTimer();
8635 setQuickSpinDownTimeout();
8636 }
8637 break;
8638
8639 case kStimulusDarkWakeEvaluate:
8640 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8641 if (systemDarkWake) {
8642 flags.bit.evaluateDarkWake = true;
8643 }
8644 break;
8645
8646 case kStimulusNoIdleSleepPreventers:
8647 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8648 flags.bit.adjustPowerState = true;
8649 break;
8650 } /* switch(stimulus) */
8651
8652 if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8653 DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8654 darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8655 if (darkWakeToSleepASAP ||
8656 (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8657 uint32_t newSleepReason;
8658
8659 if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8660 // System was previously in full wake. Sleep reason from
8661 // full to dark already recorded in fullToDarkReason.
8662
8663 if (lowBatteryCondition) {
8664 newSleepReason = kIOPMSleepReasonLowPower;
8665 } else if (thermalEmergencyState) {
8666 newSleepReason = kIOPMSleepReasonThermalEmergency;
8667 } else {
8668 newSleepReason = fullToDarkReason;
8669 }
8670 } else {
8671 // In dark wake from system sleep.
8672
8673 if (darkWakeSleepService) {
8674 newSleepReason = kIOPMSleepReasonSleepServiceExit;
8675 } else {
8676 newSleepReason = kIOPMSleepReasonMaintenance;
8677 }
8678 }
8679
8680 if (checkSystemCanSleep(newSleepReason)) {
8681 privateSleepSystem(newSleepReason);
8682 }
8683 } else { // non-maintenance (network) dark wake
8684 if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8685 // Release power clamp, and wait for children idle.
8686 adjustPowerState(true);
8687 } else {
8688 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8689 }
8690 }
8691 }
8692
8693 if (systemDarkWake) {
8694 // The rest are irrelevant while system is in dark wake.
8695 flags.u32 = 0;
8696 }
8697
8698 if ((flags.bit.displaySleepEntry) &&
8699 (kFullWakeReasonDisplayOn == fullWakeReason)) {
8700 // kIOPMSleepReasonNotificationWakeExit
8701 DLOG("Display sleep while in notification wake\n");
8702 changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8703 }
8704
8705 if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8706 bool cancelQuickSpindown = false;
8707
8708 if (flags.bit.sleepDelayChanged) {
8709 // Cancel existing idle sleep timer and quick disk spindown.
8710 // New settings will be applied by the idleSleepEnabled flag
8711 // handler below if idle sleep is enabled.
8712
8713 DLOG("extra sleep timer changed\n");
8714 cancelIdleSleepTimer();
8715 cancelQuickSpindown = true;
8716 } else {
8717 DLOG("user inactive\n");
8718 }
8719
8720 if (!userIsActive && idleSleepEnabled) {
8721 startIdleSleepTimer(getTimeToIdleSleep());
8722 }
8723
8724 if (cancelQuickSpindown) {
8725 restoreUserSpinDownTimeout();
8726 }
8727 }
8728
8729 if (flags.bit.idleSleepEnabled) {
8730 DLOG("idle sleep timer enabled\n");
8731 if (!wrangler) {
8732 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8733 startIdleSleepTimer(getTimeToIdleSleep());
8734 #else
8735 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8736 startIdleSleepTimer( idleMilliSeconds );
8737 #endif
8738 } else {
8739 // Start idle timer if prefs now allow system sleep
8740 // and user is already inactive. Disk spindown is
8741 // accelerated upon timer expiration.
8742
8743 if (!userIsActive) {
8744 startIdleSleepTimer(getTimeToIdleSleep());
8745 }
8746 }
8747 }
8748
8749 if (flags.bit.idleSleepDisabled) {
8750 DLOG("idle sleep timer disabled\n");
8751 cancelIdleSleepTimer();
8752 restoreUserSpinDownTimeout();
8753 adjustPowerState();
8754 }
8755
8756 if (flags.bit.adjustPowerState) {
8757 bool sleepASAP = false;
8758
8759 if (!systemBooting && (0 == idleSleepPreventersCount())) {
8760 if (!wrangler) {
8761 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
8762 if (idleSleepEnabled) {
8763 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8764 if (!extraSleepDelay && !idleSleepTimerPending) {
8765 sleepASAP = true;
8766 }
8767 #else
8768 // stay awake for at least idleMilliSeconds
8769 startIdleSleepTimer(idleMilliSeconds);
8770 #endif
8771 }
8772 } else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake) {
8773 sleepASAP = true;
8774 }
8775 }
8776
8777 adjustPowerState(sleepASAP);
8778 }
8779 }
8780
8781 //******************************************************************************
8782
8783 unsigned int
idleSleepPreventersCount()8784 IOPMrootDomain::idleSleepPreventersCount()
8785 {
8786 if (_aotMode) {
8787 unsigned int count __block;
8788 count = 0;
8789 preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
8790 {
8791 count += (NULL == obj->metaCast("AppleARMBacklight"));
8792 return false;
8793 });
8794 return count;
8795 }
8796
8797 return preventIdleSleepList->getCount();
8798 }
8799
8800
8801 //******************************************************************************
8802 // requestFullWake
8803 //
8804 // Request transition from dark wake to full wake
8805 //******************************************************************************
8806
8807 void
requestFullWake(FullWakeReason reason)8808 IOPMrootDomain::requestFullWake( FullWakeReason reason )
8809 {
8810 uint32_t options = 0;
8811 IOService * pciRoot = NULL;
8812 bool promotion = false;
8813
8814 // System must be in dark wake and a valid reason for entering full wake
8815 if ((kFullWakeReasonNone == reason) ||
8816 (kFullWakeReasonNone != fullWakeReason) ||
8817 (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8818 return;
8819 }
8820
8821 // Will clear reason upon exit from full wake
8822 fullWakeReason = reason;
8823
8824 _desiredCapability |= (kIOPMSystemCapabilityGraphics |
8825 kIOPMSystemCapabilityAudio);
8826
8827 if ((kSystemTransitionWake == _systemTransitionType) &&
8828 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
8829 !darkWakePowerClamped) {
8830 // Promote to full wake while waking up to dark wake due to tickle.
8831 // PM will hold off notifying the graphics subsystem about system wake
8832 // as late as possible, so if a HID tickle does arrive, graphics can
8833 // power up from this same wake transition. Otherwise, the latency to
8834 // power up graphics on the following transition can be huge on certain
8835 // systems. However, once any power clamping has taken effect, it is
8836 // too late to promote the current dark wake transition to a full wake.
8837 _pendingCapability |= (kIOPMSystemCapabilityGraphics |
8838 kIOPMSystemCapabilityAudio);
8839
8840 // Tell the PCI parent of audio and graphics drivers to stop
8841 // delaying the child notifications. Same for root domain.
8842 pciRoot = pciHostBridgeDriver.get();
8843 willEnterFullWake();
8844 promotion = true;
8845 }
8846
8847 // Unsafe to cancel once graphics was powered.
8848 // If system woke from dark wake, the return to sleep can
8849 // be cancelled. "awake -> dark -> sleep" transition
8850 // can be cancelled also, during the "dark -> sleep" phase
8851 // *prior* to driver power down.
8852 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
8853 _pendingCapability == 0) {
8854 options |= kIOPMSyncCancelPowerDown;
8855 }
8856
8857 synchronizePowerTree(options, pciRoot);
8858
8859 if (kFullWakeReasonLocalUser == fullWakeReason) {
8860 // IOGraphics doesn't light the display even though graphics is
8861 // enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
8862 // So, do an explicit activity tickle
8863 if (wrangler) {
8864 wrangler->activityTickle(0, 0);
8865 }
8866 }
8867
8868 // Log a timestamp for the initial full wake request.
8869 // System may not always honor this full wake request.
8870 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8871 AbsoluteTime now;
8872 uint64_t nsec;
8873
8874 clock_get_uptime(&now);
8875 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8876 absolutetime_to_nanoseconds(now, &nsec);
8877 MSG("full wake %s (reason %u) %u ms\n",
8878 promotion ? "promotion" : "request",
8879 fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
8880 }
8881 }
8882
8883 //******************************************************************************
8884 // willEnterFullWake
8885 //
8886 // System will enter full wake from sleep, from dark wake, or from dark
8887 // wake promotion. This function aggregate things that are in common to
8888 // all three full wake transitions.
8889 //
8890 // Assumptions: fullWakeReason was updated
8891 //******************************************************************************
8892
8893 void
willEnterFullWake(void)8894 IOPMrootDomain::willEnterFullWake( void )
8895 {
8896 hibernateRetry = false;
8897 sleepToStandby = false;
8898 standbyNixed = false;
8899 resetTimers = false;
8900 sleepTimerMaintenance = false;
8901
8902 assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
8903
8904 _systemMessageClientMask = kSystemMessageClientPowerd |
8905 kSystemMessageClientLegacyApp;
8906
8907 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
8908 // First time to attain full wake capability since the last wake
8909 _systemMessageClientMask |= kSystemMessageClientKernel;
8910
8911 // Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
8912 setProperty(gIOPMUserTriggeredFullWakeKey.get(),
8913 (kFullWakeReasonLocalUser == fullWakeReason) ?
8914 kOSBooleanTrue : kOSBooleanFalse);
8915 }
8916 #if HIBERNATION
8917 IOHibernateSetWakeCapabilities(_pendingCapability);
8918 #endif
8919
8920 IOService::setAdvisoryTickleEnable( true );
8921 tellClients(kIOMessageSystemWillPowerOn);
8922 preventTransitionToUserActive(false);
8923 }
8924
8925 //******************************************************************************
8926 // fullWakeDelayedWork
8927 //
8928 // System has already entered full wake. Invoked by a delayed thread call.
8929 //******************************************************************************
8930
8931 void
fullWakeDelayedWork(void)8932 IOPMrootDomain::fullWakeDelayedWork( void )
8933 {
8934 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8935 if (!gIOPMWorkLoop->inGate()) {
8936 gIOPMWorkLoop->runAction(
8937 OSMemberFunctionCast(IOWorkLoop::Action, this,
8938 &IOPMrootDomain::fullWakeDelayedWork), this);
8939 return;
8940 }
8941
8942 DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
8943 _currentCapability, _pendingCapability, _highestCapability,
8944 clamshellDisabled, clamshellSleepDisableMask);
8945
8946 if (clamshellExists &&
8947 CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
8948 !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
8949 if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
8950 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8951 } else {
8952 // Not the initial full wake after waking from sleep.
8953 // Evaluate the clamshell for rdar://problem/9157444.
8954 receivePowerNotification(kLocalEvalClamshellCommand);
8955 }
8956 }
8957 #endif
8958 }
8959
8960 //******************************************************************************
8961 // evaluateAssertions
8962 //
8963 //******************************************************************************
8964
8965 // Bitmask of all kernel assertions that prevent system idle sleep.
8966 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
8967 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
8968 (kIOPMDriverAssertionReservedBit7 | \
8969 kIOPMDriverAssertionPreventSystemIdleSleepBit)
8970
8971 void
evaluateAssertions(IOPMDriverAssertionType newAssertions,IOPMDriverAssertionType oldAssertions)8972 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
8973 {
8974 IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
8975
8976 messageClients(kIOPMMessageDriverAssertionsChanged);
8977
8978 if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
8979 if (wrangler) {
8980 bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
8981
8982 DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
8983 wrangler->setIgnoreIdleTimer( value );
8984 }
8985 }
8986
8987 if (changedBits & kIOPMDriverAssertionCPUBit) {
8988 if (_aotNow) {
8989 IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
8990 }
8991 evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
8992 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
8993 AbsoluteTime now;
8994 clock_usec_t microsecs;
8995 clock_get_uptime(&now);
8996 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8997 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
8998 if (assertOnWakeReport) {
8999 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
9000 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
9001 }
9002 }
9003 }
9004
9005 if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
9006 if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
9007 if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9008 DLOG("PreventIdleSleep driver assertion raised\n");
9009 bool ok = updatePreventIdleSleepList(this, true);
9010 if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9011 // Cancel idle sleep if there is one in progress
9012 cancelIdlePowerDown(this);
9013 }
9014 }
9015 } else {
9016 DLOG("PreventIdleSleep driver assertion dropped\n");
9017 updatePreventIdleSleepList(this, false);
9018 }
9019 }
9020 }
9021
9022 // MARK: -
9023 // MARK: Statistics
9024
9025 //******************************************************************************
9026 // pmStats
9027 //
9028 //******************************************************************************
9029
9030 void
pmStatsRecordEvent(int eventIndex,AbsoluteTime timestamp)9031 IOPMrootDomain::pmStatsRecordEvent(
9032 int eventIndex,
9033 AbsoluteTime timestamp)
9034 {
9035 bool starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9036 bool stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9037 uint64_t delta;
9038 uint64_t nsec;
9039 OSSharedPtr<OSData> publishPMStats;
9040
9041 eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9042
9043 absolutetime_to_nanoseconds(timestamp, &nsec);
9044
9045 switch (eventIndex) {
9046 case kIOPMStatsHibernateImageWrite:
9047 if (starting) {
9048 gPMStats.hibWrite.start = nsec;
9049 } else if (stopping) {
9050 gPMStats.hibWrite.stop = nsec;
9051 }
9052
9053 if (stopping) {
9054 delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9055 IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9056 }
9057 break;
9058 case kIOPMStatsHibernateImageRead:
9059 if (starting) {
9060 gPMStats.hibRead.start = nsec;
9061 } else if (stopping) {
9062 gPMStats.hibRead.stop = nsec;
9063 }
9064
9065 if (stopping) {
9066 delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9067 IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9068
9069 publishPMStats = OSData::withValue(gPMStats);
9070 setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9071 bzero(&gPMStats, sizeof(gPMStats));
9072 }
9073 break;
9074 }
9075 }
9076
9077 /*
9078 * Appends a record of the application response to
9079 * IOPMrootDomain::pmStatsAppResponses
9080 */
9081 void
pmStatsRecordApplicationResponse(const OSSymbol * response,const char * name,int messageType,uint32_t delay_ms,uint64_t id,OSObject * object,IOPMPowerStateIndex powerState,bool async)9082 IOPMrootDomain::pmStatsRecordApplicationResponse(
9083 const OSSymbol *response,
9084 const char *name,
9085 int messageType,
9086 uint32_t delay_ms,
9087 uint64_t id,
9088 OSObject *object,
9089 IOPMPowerStateIndex powerState,
9090 bool async)
9091 {
9092 OSSharedPtr<OSDictionary> responseDescription;
9093 OSSharedPtr<OSNumber> delayNum;
9094 OSSharedPtr<OSNumber> powerCaps;
9095 OSSharedPtr<OSNumber> pidNum;
9096 OSSharedPtr<OSNumber> msgNum;
9097 OSSharedPtr<const OSSymbol> appname;
9098 OSSharedPtr<const OSSymbol> sleep;
9099 OSSharedPtr<const OSSymbol> wake;
9100 IOPMServiceInterestNotifier *notify = NULL;
9101
9102 if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9103 if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9104 notify->ackTimeoutCnt++;
9105 } else {
9106 notify->ackTimeoutCnt = 0;
9107 }
9108 }
9109
9110 if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9111 (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9112 return;
9113 }
9114
9115
9116 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9117 kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9118 } else if (notify) {
9119 // User space app or kernel capability client
9120 if (id) {
9121 kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9122 } else {
9123 kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9124 }
9125 notify->msgType = 0;
9126 }
9127
9128 responseDescription = OSDictionary::withCapacity(5);
9129 if (responseDescription) {
9130 if (response) {
9131 responseDescription->setObject(_statsResponseTypeKey.get(), response);
9132 }
9133
9134 msgNum = OSNumber::withNumber(messageType, 32);
9135 if (msgNum) {
9136 responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9137 }
9138
9139 if (!name && notify && notify->identifier) {
9140 name = notify->identifier->getCStringNoCopy();
9141 }
9142
9143 if (name && (strlen(name) > 0)) {
9144 appname = OSSymbol::withCString(name);
9145 if (appname) {
9146 responseDescription->setObject(_statsNameKey.get(), appname.get());
9147 }
9148 }
9149
9150 if (!id && notify) {
9151 id = notify->uuid0;
9152 }
9153 if (id != 0) {
9154 pidNum = OSNumber::withNumber(id, 64);
9155 if (pidNum) {
9156 responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9157 }
9158 }
9159
9160 delayNum = OSNumber::withNumber(delay_ms, 32);
9161 if (delayNum) {
9162 responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9163 }
9164
9165 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9166 powerCaps = OSNumber::withNumber(powerState, 32);
9167
9168 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9169 static const char * driverCallTypes[] = {
9170 [kDriverCallInformPreChange] = "powerStateWillChangeTo",
9171 [kDriverCallInformPostChange] = "powerStateDidChangeTo",
9172 [kDriverCallSetPowerState] = "setPowerState"
9173 };
9174
9175 if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9176 DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9177 name, id, driverCallTypes[messageType], (uint32_t) powerState,
9178 async ? "async " : "", delay_ms);
9179 }
9180 #endif
9181 } else {
9182 powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9183 }
9184 if (powerCaps) {
9185 responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9186 }
9187
9188 sleep = OSSymbol::withCString("Sleep");
9189 wake = OSSymbol::withCString("Wake");
9190 if (_systemTransitionType == kSystemTransitionSleep) {
9191 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9192 } else if (_systemTransitionType == kSystemTransitionWake) {
9193 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9194 } else if (_systemTransitionType == kSystemTransitionCapability) {
9195 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9196 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9197 } else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9198 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9199 }
9200 }
9201
9202 IOLockLock(pmStatsLock);
9203 if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9204 pmStatsAppResponses->setObject(responseDescription.get());
9205 }
9206 IOLockUnlock(pmStatsLock);
9207 }
9208
9209 return;
9210 }
9211
9212 // MARK: -
9213 // MARK: PMTraceWorker
9214
9215 //******************************************************************************
9216 // TracePoint support
9217 //
9218 //******************************************************************************
9219
9220 #define kIOPMRegisterNVRAMTracePointHandlerKey \
9221 "IOPMRegisterNVRAMTracePointHandler"
9222
9223 IOReturn
callPlatformFunction(const OSSymbol * functionName,bool waitForFunction,void * param1,void * param2,void * param3,void * param4)9224 IOPMrootDomain::callPlatformFunction(
9225 const OSSymbol * functionName,
9226 bool waitForFunction,
9227 void * param1, void * param2,
9228 void * param3, void * param4 )
9229 {
9230 if (pmTracer && functionName &&
9231 functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9232 !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9233 uint32_t tracePointPhases, tracePointPCI;
9234 uint64_t statusCode;
9235
9236 pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9237 pmTracer->tracePointTarget = (void *) param2;
9238 tracePointPCI = (uint32_t)(uintptr_t) param3;
9239 tracePointPhases = (uint32_t)(uintptr_t) param4;
9240 if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9241 OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9242 if (node) {
9243 OSSharedPtr<OSObject> bootRomFailureProp;
9244 bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9245 OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9246 uint32_t bootFailureCode;
9247 if (data && data->getLength() == sizeof(bootFailureCode)) {
9248 // Failure code from EFI/BootRom is a four byte structure
9249 memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9250 tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9251 }
9252 }
9253 }
9254 statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9255 if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9256 MSG("Sleep failure code 0x%08x 0x%08x\n",
9257 tracePointPCI, tracePointPhases);
9258 }
9259 setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9260 pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9261
9262 return kIOReturnSuccess;
9263 }
9264 #if HIBERNATION
9265 else if (functionName &&
9266 functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9267 if (gSleepPolicyHandler) {
9268 return kIOReturnExclusiveAccess;
9269 }
9270 if (!param1) {
9271 return kIOReturnBadArgument;
9272 }
9273 gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9274 gSleepPolicyTarget = (void *) param2;
9275 setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9276 return kIOReturnSuccess;
9277 }
9278 #endif
9279
9280 return super::callPlatformFunction(
9281 functionName, waitForFunction, param1, param2, param3, param4);
9282 }
9283
9284 void
kdebugTrace(uint32_t event,uint64_t id,uintptr_t param1,uintptr_t param2,uintptr_t param3)9285 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9286 uintptr_t param1, uintptr_t param2, uintptr_t param3)
9287 {
9288 uint32_t code = IODBG_POWER(event);
9289 uint64_t regId = id;
9290 if (regId == 0) {
9291 regId = getRegistryEntryID();
9292 }
9293 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9294 }
9295
9296 void
tracePoint(uint8_t point)9297 IOPMrootDomain::tracePoint( uint8_t point )
9298 {
9299 if (systemBooting) {
9300 return;
9301 }
9302
9303 if (kIOPMTracePointWakeCapabilityClients == point) {
9304 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9305 }
9306
9307 kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9308 pmTracer->tracePoint(point);
9309 }
9310
9311 static void
kext_log_putc(char c)9312 kext_log_putc(char c)
9313 {
9314 if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9315 return;
9316 }
9317 if (c == '(' || c == '[' || c == ' ') {
9318 c = 0;
9319 gKextNameEnd = true;
9320 }
9321
9322 gKextNameBuf[gKextNamePos++] = c;
9323 }
9324
9325 static int
kext_log(const char * fmt,...)9326 kext_log(const char *fmt, ...)
9327 {
9328 va_list listp;
9329
9330 va_start(listp, fmt);
9331 _doprnt(fmt, &listp, &kext_log_putc, 16);
9332 va_end(listp);
9333
9334 return 0;
9335 }
9336
9337 static OSPtr<const OSSymbol>
copyKextIdentifierWithAddress(vm_address_t address)9338 copyKextIdentifierWithAddress(vm_address_t address)
9339 {
9340 OSSharedPtr<const OSSymbol> identifer;
9341
9342 IOLockLock(gHaltLogLock);
9343
9344 gKextNameEnd = false;
9345 gKextNamePos = 0;
9346 gKextNameBuf[0] = 0;
9347
9348 OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9349 gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9350 identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9351
9352 IOLockUnlock(gHaltLogLock);
9353
9354 return identifer;
9355 }
9356
9357 // Caller serialized using PM workloop
9358 const char *
getNotificationClientName(OSObject * object)9359 IOPMrootDomain::getNotificationClientName(OSObject *object)
9360 {
9361 IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9362 const char *clientName = "UNKNOWN";
9363
9364 if (!notifier->clientName) {
9365 // Check for user client
9366 if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9367 OSNumber *clientID = NULL;
9368 messageClient(kIOMessageCopyClientID, object, &clientID);
9369 if (clientID) {
9370 OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9371 if (string) {
9372 notifier->clientName = OSSymbol::withString(string.get());
9373 }
9374 clientID->release();
9375 }
9376 } else if (notifier->identifier) {
9377 notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9378 }
9379 }
9380
9381 if (notifier->clientName) {
9382 clientName = notifier->clientName->getCStringNoCopy();
9383 }
9384
9385 return clientName;
9386 }
9387
9388 void
traceNotification(OSObject * object,bool start,uint64_t timestamp,uint32_t msgIndex)9389 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9390 {
9391 IOPMServiceInterestNotifier *notifier;
9392
9393 if (systemBooting) {
9394 return;
9395 }
9396 notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9397 if (!notifier) {
9398 return;
9399 }
9400
9401 if (start) {
9402 pmTracer->traceDetail(notifier->uuid0 >> 32);
9403 kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9404 (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9405
9406 // Update notifier state used for response/ack logging
9407 notifier->msgIndex = msgIndex;
9408 notifier->msgAbsTime = timestamp;
9409
9410 if (msgIndex != UINT_MAX) {
9411 DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9412 } else {
9413 DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9414 }
9415
9416 assert(notifierObject == NULL);
9417 notifierThread = current_thread();
9418 notifierObject.reset(notifier, OSRetain);
9419 } else {
9420 uint64_t nsec;
9421 uint32_t delayMS;
9422
9423 SUB_ABSOLUTETIME(×tamp, ¬ifier->msgAbsTime);
9424 absolutetime_to_nanoseconds(timestamp, &nsec);
9425 delayMS = (uint32_t)(nsec / 1000000ULL);
9426 if (delayMS > notifier->maxMsgDelayMS) {
9427 notifier->maxMsgDelayMS = delayMS;
9428 }
9429
9430 assert(notifierObject == notifier);
9431 notifierObject.reset();
9432 notifierThread = NULL;
9433 }
9434 }
9435
9436 void
traceNotificationAck(OSObject * object,uint32_t delay_ms)9437 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9438 {
9439 if (systemBooting) {
9440 return;
9441 }
9442 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9443 if (!notifier) {
9444 return;
9445 }
9446
9447 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9448 (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9449
9450 DLOG("%s[%u] ack from %s took %d ms\n",
9451 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9452 if (delay_ms > notifier->maxAckDelayMS) {
9453 notifier->maxAckDelayMS = delay_ms;
9454 }
9455 }
9456
9457 void
traceNotificationResponse(OSObject * object,uint32_t delay_ms,uint32_t ack_time_us)9458 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9459 {
9460 if (systemBooting) {
9461 return;
9462 }
9463 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9464 if (!notifier) {
9465 return;
9466 }
9467
9468 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9469 (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9470
9471 if (ack_time_us == 0) {
9472 // Client work is done and ack will not be forthcoming
9473 DLOG("%s[%u] response from %s took %d ms\n",
9474 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9475 } else {
9476 // Client needs more time and it must ack within ack_time_us
9477 DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9478 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9479 }
9480 }
9481
9482 void
traceFilteredNotification(OSObject * object)9483 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9484 {
9485 if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9486 return;
9487 }
9488 if (systemBooting) {
9489 return;
9490 }
9491 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9492 if (!notifier) {
9493 return;
9494 }
9495
9496 DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9497 }
9498
9499 void
traceDetail(uint32_t msgType,uint32_t msgIndex,uint32_t delay)9500 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9501 {
9502 if (!systemBooting) {
9503 uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9504 pmTracer->traceDetail( detail );
9505 kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9506 DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9507 }
9508 }
9509
9510 void
configureReportGated(uint64_t channel_id,uint64_t action,void * result)9511 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9512 {
9513 size_t reportSize;
9514 void **report = NULL;
9515 uint32_t bktCnt;
9516 uint32_t bktSize;
9517 uint32_t *clientCnt;
9518
9519 ASSERT_GATED();
9520
9521 report = NULL;
9522 if (channel_id == kAssertDelayChID) {
9523 report = &assertOnWakeReport;
9524 bktCnt = kAssertDelayBcktCnt;
9525 bktSize = kAssertDelayBcktSize;
9526 clientCnt = &assertOnWakeClientCnt;
9527 } else if (channel_id == kSleepDelaysChID) {
9528 report = &sleepDelaysReport;
9529 bktCnt = kSleepDelaysBcktCnt;
9530 bktSize = kSleepDelaysBcktSize;
9531 clientCnt = &sleepDelaysClientCnt;
9532 } else {
9533 assert(false);
9534 return;
9535 }
9536
9537 switch (action) {
9538 case kIOReportEnable:
9539
9540 if (*report) {
9541 (*clientCnt)++;
9542 break;
9543 }
9544
9545 reportSize = HISTREPORT_BUFSIZE(bktCnt);
9546 *report = IOMallocZeroData(reportSize);
9547 if (*report == NULL) {
9548 break;
9549 }
9550 HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9551 getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9552
9553 if (channel_id == kAssertDelayChID) {
9554 assertOnWakeSecs = 0;
9555 }
9556
9557 break;
9558
9559 case kIOReportDisable:
9560 if (*clientCnt == 0) {
9561 break;
9562 }
9563 if (*clientCnt == 1) {
9564 IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9565 *report = NULL;
9566 }
9567 (*clientCnt)--;
9568
9569 if (channel_id == kAssertDelayChID) {
9570 assertOnWakeSecs = -1; // Invalid value to prevent updates
9571 }
9572 break;
9573
9574 case kIOReportGetDimensions:
9575 if (*report) {
9576 HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9577 }
9578 break;
9579 }
9580
9581 return;
9582 }
9583
9584 IOReturn
configureReport(IOReportChannelList * channelList,IOReportConfigureAction action,void * result,void * destination)9585 IOPMrootDomain::configureReport(IOReportChannelList *channelList,
9586 IOReportConfigureAction action,
9587 void *result,
9588 void *destination)
9589 {
9590 unsigned cnt;
9591 uint64_t configAction = (uint64_t)action;
9592
9593 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9594 if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9595 (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9596 (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9597 if (action != kIOReportGetDimensions) {
9598 continue;
9599 }
9600 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9601 } else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9602 (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9603 gIOPMWorkLoop->runAction(
9604 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9605 (OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9606 (void *)configAction, (void *)result);
9607 }
9608 }
9609
9610 return super::configureReport(channelList, action, result, destination);
9611 }
9612
9613 IOReturn
updateReportGated(uint64_t ch_id,void * result,IOBufferMemoryDescriptor * dest)9614 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9615 {
9616 uint32_t size2cpy;
9617 void *data2cpy;
9618 void **report;
9619
9620 ASSERT_GATED();
9621
9622 report = NULL;
9623 if (ch_id == kAssertDelayChID) {
9624 report = &assertOnWakeReport;
9625 } else if (ch_id == kSleepDelaysChID) {
9626 report = &sleepDelaysReport;
9627 } else {
9628 assert(false);
9629 return kIOReturnBadArgument;
9630 }
9631
9632 if (*report == NULL) {
9633 return kIOReturnNotOpen;
9634 }
9635
9636 HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9637 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9638 return kIOReturnOverrun;
9639 }
9640
9641 HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9642 dest->appendBytes(data2cpy, size2cpy);
9643
9644 return kIOReturnSuccess;
9645 }
9646
9647 IOReturn
updateReport(IOReportChannelList * channelList,IOReportUpdateAction action,void * result,void * destination)9648 IOPMrootDomain::updateReport(IOReportChannelList *channelList,
9649 IOReportUpdateAction action,
9650 void *result,
9651 void *destination)
9652 {
9653 uint32_t size2cpy;
9654 void *data2cpy;
9655 uint8_t buf[SIMPLEREPORT_BUFSIZE];
9656 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9657 unsigned cnt;
9658 uint64_t ch_id;
9659
9660 if (action != kIOReportCopyChannelData) {
9661 goto exit;
9662 }
9663
9664 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9665 ch_id = channelList->channels[cnt].channel_id;
9666
9667 if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9668 gIOPMWorkLoop->runAction(
9669 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9670 (OSObject *)this, (void *)ch_id,
9671 (void *)result, (void *)dest);
9672 continue;
9673 } else if ((ch_id == kSleepCntChID) ||
9674 (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9675 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9676 } else {
9677 continue;
9678 }
9679
9680 if (ch_id == kSleepCntChID) {
9681 SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9682 } else if (ch_id == kDarkWkCntChID) {
9683 SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9684 } else if (ch_id == kUserWkCntChID) {
9685 SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9686 }
9687
9688 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9689 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9690 dest->appendBytes(data2cpy, size2cpy);
9691 }
9692
9693 exit:
9694 return super::updateReport(channelList, action, result, destination);
9695 }
9696
9697
9698 //******************************************************************************
9699 // PMTraceWorker Class
9700 //
9701 //******************************************************************************
9702
9703 #undef super
9704 #define super OSObject
OSDefineMetaClassAndStructors(PMTraceWorker,OSObject)9705 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9706
9707 #define kPMBestGuessPCIDevicesCount 25
9708 #define kPMMaxRTCBitfieldSize 32
9709
9710 OSPtr<PMTraceWorker>
9711 PMTraceWorker::tracer(IOPMrootDomain * owner)
9712 {
9713 OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9714 if (!me || !me->init()) {
9715 return NULL;
9716 }
9717
9718 DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9719
9720 // Note that we cannot instantiate the PCI device -> bit mappings here, since
9721 // the IODeviceTree has not yet been created by IOPlatformExpert. We create
9722 // this dictionary lazily.
9723 me->owner = owner;
9724 me->pciDeviceBitMappings = NULL;
9725 me->pmTraceWorkerLock = IOLockAlloc();
9726 me->tracePhase = kIOPMTracePointSystemUp;
9727 me->traceData32 = 0;
9728 me->loginWindowData = 0;
9729 me->coreDisplayData = 0;
9730 me->coreGraphicsData = 0;
9731 return me;
9732 }
9733
9734 void
RTC_TRACE(void)9735 PMTraceWorker::RTC_TRACE(void)
9736 {
9737 if (tracePointHandler && tracePointTarget) {
9738 uint32_t wordA;
9739
9740 IOLockLock(pmTraceWorkerLock);
9741 wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9742 (coreGraphicsData << 8) | tracePhase;
9743 IOLockUnlock(pmTraceWorkerLock);
9744
9745 tracePointHandler( tracePointTarget, traceData32, wordA );
9746 _LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9747 }
9748 #if DEVELOPMENT || DEBUG
9749 if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9750 DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9751 IOLock *l = IOLockAlloc();
9752 IOLockLock(l);
9753 IOLockLock(l);
9754 }
9755 #endif
9756 }
9757
9758 int
recordTopLevelPCIDevice(IOService * pciDevice)9759 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
9760 {
9761 OSSharedPtr<const OSSymbol> deviceName;
9762 int index = -1;
9763
9764 IOLockLock(pmTraceWorkerLock);
9765
9766 if (!pciDeviceBitMappings) {
9767 pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
9768 if (!pciDeviceBitMappings) {
9769 goto exit;
9770 }
9771 }
9772
9773 // Check for bitmask overflow.
9774 if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
9775 goto exit;
9776 }
9777
9778 if ((deviceName = pciDevice->copyName()) &&
9779 (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
9780 pciDeviceBitMappings->setObject(deviceName.get())) {
9781 index = pciDeviceBitMappings->getCount() - 1;
9782 _LOG("PMTrace PCI array: set object %s => %d\n",
9783 deviceName->getCStringNoCopy(), index);
9784 }
9785
9786 if (!addedToRegistry && (index >= 0)) {
9787 addedToRegistry = owner->setProperty("PCITopLevel", this);
9788 }
9789
9790 exit:
9791 IOLockUnlock(pmTraceWorkerLock);
9792 return index;
9793 }
9794
9795 bool
serialize(OSSerialize * s) const9796 PMTraceWorker::serialize(OSSerialize *s) const
9797 {
9798 bool ok = false;
9799 if (pciDeviceBitMappings) {
9800 IOLockLock(pmTraceWorkerLock);
9801 ok = pciDeviceBitMappings->serialize(s);
9802 IOLockUnlock(pmTraceWorkerLock);
9803 }
9804 return ok;
9805 }
9806
9807 void
tracePoint(uint8_t phase)9808 PMTraceWorker::tracePoint(uint8_t phase)
9809 {
9810 // clear trace detail when phase begins
9811 if (tracePhase != phase) {
9812 traceData32 = 0;
9813 }
9814
9815 tracePhase = phase;
9816
9817 DLOG("trace point 0x%02x\n", tracePhase);
9818 RTC_TRACE();
9819 }
9820
9821 void
traceDetail(uint32_t detail)9822 PMTraceWorker::traceDetail(uint32_t detail)
9823 {
9824 if (detail == traceData32) {
9825 return;
9826 }
9827 traceData32 = detail;
9828 RTC_TRACE();
9829 }
9830
9831 void
traceComponentWakeProgress(uint32_t component,uint32_t data)9832 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
9833 {
9834 switch (component) {
9835 case kIOPMLoginWindowProgress:
9836 loginWindowData = data & kIOPMLoginWindowProgressMask;
9837 break;
9838 case kIOPMCoreDisplayProgress:
9839 coreDisplayData = data & kIOPMCoreDisplayProgressMask;
9840 break;
9841 case kIOPMCoreGraphicsProgress:
9842 coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
9843 break;
9844 default:
9845 return;
9846 }
9847
9848 DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
9849 RTC_TRACE();
9850 }
9851
9852 void
tracePCIPowerChange(change_t type,IOService * service,uint32_t changeFlags,uint32_t bitNum)9853 PMTraceWorker::tracePCIPowerChange(
9854 change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
9855 {
9856 uint32_t bitMask;
9857 uint32_t expectedFlag;
9858
9859 // Ignore PCI changes outside of system sleep/wake.
9860 if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
9861 (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
9862 return;
9863 }
9864
9865 // Only record the WillChange transition when going to sleep,
9866 // and the DidChange on the way up.
9867 changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
9868 expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
9869 kIOPMDomainWillChange : kIOPMDomainDidChange;
9870 if (changeFlags != expectedFlag) {
9871 return;
9872 }
9873
9874 // Mark this device off in our bitfield
9875 if (bitNum < kPMMaxRTCBitfieldSize) {
9876 bitMask = (1 << bitNum);
9877
9878 if (kPowerChangeStart == type) {
9879 traceData32 |= bitMask;
9880 _LOG("PMTrace: Device %s started - bit %2d mask 0x%08x => 0x%08x\n",
9881 service->getName(), bitNum, bitMask, traceData32);
9882 owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
9883 } else {
9884 traceData32 &= ~bitMask;
9885 _LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
9886 service->getName(), bitNum, bitMask, traceData32);
9887 owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
9888 }
9889
9890 DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
9891 RTC_TRACE();
9892 }
9893 }
9894
9895 uint64_t
getPMStatusCode()9896 PMTraceWorker::getPMStatusCode()
9897 {
9898 return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
9899 }
9900
9901 uint8_t
getTracePhase()9902 PMTraceWorker::getTracePhase()
9903 {
9904 return tracePhase;
9905 }
9906
9907 uint32_t
getTraceData()9908 PMTraceWorker::getTraceData()
9909 {
9910 return traceData32;
9911 }
9912
9913 // MARK: -
9914 // MARK: PMHaltWorker
9915
9916 //******************************************************************************
9917 // PMHaltWorker Class
9918 //
9919 //******************************************************************************
9920
9921 PMHaltWorker *
worker(void)9922 PMHaltWorker::worker( void )
9923 {
9924 PMHaltWorker * me;
9925 IOThread thread;
9926
9927 do {
9928 me = OSTypeAlloc( PMHaltWorker );
9929 if (!me || !me->init()) {
9930 break;
9931 }
9932
9933 me->lock = IOLockAlloc();
9934 if (!me->lock) {
9935 break;
9936 }
9937
9938 DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
9939 me->retain(); // thread holds extra retain
9940 if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
9941 me->release();
9942 break;
9943 }
9944 thread_deallocate(thread);
9945 return me;
9946 } while (false);
9947
9948 if (me) {
9949 me->release();
9950 }
9951 return NULL;
9952 }
9953
9954 void
free(void)9955 PMHaltWorker::free( void )
9956 {
9957 DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
9958 if (lock) {
9959 IOLockFree(lock);
9960 lock = NULL;
9961 }
9962 return OSObject::free();
9963 }
9964
9965 void
main(void * arg,wait_result_t waitResult)9966 PMHaltWorker::main( void * arg, wait_result_t waitResult )
9967 {
9968 PMHaltWorker * me = (PMHaltWorker *) arg;
9969
9970 IOLockLock( gPMHaltLock );
9971 gPMHaltBusyCount++;
9972 me->depth = gPMHaltDepth;
9973 IOLockUnlock( gPMHaltLock );
9974
9975 while (me->depth >= 0) {
9976 PMHaltWorker::work( me );
9977
9978 IOLockLock( gPMHaltLock );
9979 if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
9980 // This is the last thread to finish work on this level,
9981 // inform everyone to start working on next lower level.
9982 gPMHaltDepth--;
9983 me->depth = gPMHaltDepth;
9984 gPMHaltIdleCount = 0;
9985 thread_wakeup((event_t) &gPMHaltIdleCount);
9986 } else {
9987 // One or more threads are still working on this level,
9988 // this thread must wait.
9989 me->depth = gPMHaltDepth - 1;
9990 do {
9991 IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
9992 } while (me->depth != gPMHaltDepth);
9993 }
9994 IOLockUnlock( gPMHaltLock );
9995 }
9996
9997 // No more work to do, terminate thread
9998 DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
9999 thread_wakeup( &gPMHaltDepth );
10000 me->release();
10001 }
10002
10003 void
work(PMHaltWorker * me)10004 PMHaltWorker::work( PMHaltWorker * me )
10005 {
10006 OSSharedPtr<IOService> service;
10007 OSSet * inner;
10008 AbsoluteTime startTime, elapsedTime;
10009 UInt32 deltaTime;
10010 bool timeout;
10011
10012 while (true) {
10013 timeout = false;
10014
10015 // Claim an unit of work from the shared pool
10016 IOLockLock( gPMHaltLock );
10017 inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10018 if (inner) {
10019 service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10020 if (service) {
10021 inner->removeObject(service.get());
10022 }
10023 }
10024 IOLockUnlock( gPMHaltLock );
10025 if (!service) {
10026 break; // no more work at this depth
10027 }
10028 clock_get_uptime(&startTime);
10029
10030 if (!service->isInactive() &&
10031 service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10032 IOLockLock(me->lock);
10033 me->startTime = startTime;
10034 me->service = service.get();
10035 me->timeout = false;
10036 IOLockUnlock(me->lock);
10037
10038 service->systemWillShutdown( gPMHaltMessageType);
10039
10040 // Wait for driver acknowledgement
10041 IOLockLock(me->lock);
10042 while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10043 IOLockSleep(me->lock, me, THREAD_UNINT);
10044 }
10045 me->service = NULL;
10046 timeout = me->timeout;
10047 IOLockUnlock(me->lock);
10048 }
10049
10050 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10051 if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10052 LOG("%s driver %s (0x%llx) took %u ms\n",
10053 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10054 "PowerOff" : "Restart",
10055 service->getName(), service->getRegistryEntryID(),
10056 (uint32_t) deltaTime );
10057 halt_log_enter("PowerOff/Restart handler completed",
10058 OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10059 elapsedTime);
10060 }
10061
10062 me->visits++;
10063 }
10064 }
10065
10066 void
checkTimeout(PMHaltWorker * me,AbsoluteTime * now)10067 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10068 {
10069 UInt64 nano;
10070 AbsoluteTime startTime;
10071 AbsoluteTime endTime;
10072
10073 endTime = *now;
10074
10075 IOLockLock(me->lock);
10076 if (me->service && !me->timeout) {
10077 startTime = me->startTime;
10078 nano = 0;
10079 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10080 SUB_ABSOLUTETIME(&endTime, &startTime);
10081 absolutetime_to_nanoseconds(endTime, &nano);
10082 }
10083 if (nano > 3000000000ULL) {
10084 me->timeout = true;
10085
10086 halt_log_enter("PowerOff/Restart still waiting on handler",
10087 OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10088 endTime);
10089 MSG("%s still waiting on %s\n",
10090 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ? "PowerOff" : "Restart",
10091 me->service->getName());
10092 }
10093 }
10094 IOLockUnlock(me->lock);
10095 }
10096
10097 //******************************************************************************
10098 // acknowledgeSystemWillShutdown
10099 //
10100 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10101 //******************************************************************************
10102
10103 void
acknowledgeSystemWillShutdown(IOService * from)10104 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10105 {
10106 PMHaltWorker * worker;
10107 OSSharedPtr<OSObject> prop;
10108
10109 if (!from) {
10110 return;
10111 }
10112
10113 //DLOG("%s acknowledged\n", from->getName());
10114 prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10115 if (prop) {
10116 worker = (PMHaltWorker *) prop.get();
10117 IOLockLock(worker->lock);
10118 from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10119 thread_wakeup((event_t) worker);
10120 IOLockUnlock(worker->lock);
10121 } else {
10122 DLOG("%s acknowledged without worker property\n",
10123 from->getName());
10124 }
10125 }
10126
10127
10128 //******************************************************************************
10129 // notifySystemShutdown
10130 //
10131 // Notify all objects in PM tree that system will shutdown or restart
10132 //******************************************************************************
10133
10134 static void
notifySystemShutdown(IOService * root,uint32_t messageType)10135 notifySystemShutdown( IOService * root, uint32_t messageType )
10136 {
10137 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10138 OSSharedPtr<IORegistryIterator> iter;
10139 IORegistryEntry * entry;
10140 IOService * node;
10141 OSSet * inner;
10142 OSSharedPtr<OSSet> newInner;
10143 PMHaltWorker * workers[kPMHaltMaxWorkers];
10144 AbsoluteTime deadline;
10145 unsigned int totalNodes = 0;
10146 unsigned int depth;
10147 unsigned int rootDepth;
10148 unsigned int numWorkers;
10149 unsigned int count;
10150 int waitResult;
10151 void * baseFunc;
10152 bool ok;
10153
10154 DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10155
10156 baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10157
10158 // Iterate the entire PM tree starting from root
10159
10160 rootDepth = root->getDepth( gIOPowerPlane );
10161 if (!rootDepth) {
10162 goto done;
10163 }
10164
10165 // debug - for repeated test runs
10166 while (PMHaltWorker::metaClass->getInstanceCount()) {
10167 IOSleep(1);
10168 }
10169
10170 if (!gPMHaltArray) {
10171 gPMHaltArray = OSArray::withCapacity(40);
10172 if (!gPMHaltArray) {
10173 goto done;
10174 }
10175 } else { // debug
10176 gPMHaltArray->flushCollection();
10177 }
10178
10179 if (!gPMHaltLock) {
10180 gPMHaltLock = IOLockAlloc();
10181 if (!gPMHaltLock) {
10182 goto done;
10183 }
10184 }
10185
10186 if (!gPMHaltClientAcknowledgeKey) {
10187 gPMHaltClientAcknowledgeKey =
10188 OSSymbol::withCStringNoCopy("PMShutdown");
10189 if (!gPMHaltClientAcknowledgeKey) {
10190 goto done;
10191 }
10192 }
10193
10194 gPMHaltMessageType = messageType;
10195
10196 // Depth-first walk of PM plane
10197
10198 iter = IORegistryIterator::iterateOver(
10199 root, gIOPowerPlane, kIORegistryIterateRecursively);
10200
10201 if (iter) {
10202 while ((entry = iter->getNextObject())) {
10203 node = OSDynamicCast(IOService, entry);
10204 if (!node) {
10205 continue;
10206 }
10207
10208 if (baseFunc ==
10209 OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10210 continue;
10211 }
10212
10213 depth = node->getDepth( gIOPowerPlane );
10214 if (depth <= rootDepth) {
10215 continue;
10216 }
10217
10218 ok = false;
10219
10220 // adjust to zero based depth
10221 depth -= (rootDepth + 1);
10222
10223 // gPMHaltArray is an array of containers, each container
10224 // refers to nodes with the same depth.
10225
10226 count = gPMHaltArray->getCount();
10227 while (depth >= count) {
10228 // expand array and insert placeholders
10229 gPMHaltArray->setObject(PLACEHOLDER);
10230 count++;
10231 }
10232 count = gPMHaltArray->getCount();
10233 if (depth < count) {
10234 inner = (OSSet *)gPMHaltArray->getObject(depth);
10235 if (inner == PLACEHOLDER) {
10236 newInner = OSSet::withCapacity(40);
10237 if (newInner) {
10238 gPMHaltArray->replaceObject(depth, newInner.get());
10239 inner = newInner.get();
10240 }
10241 }
10242
10243 // PM nodes that appear more than once in the tree will have
10244 // the same depth, OSSet will refuse to add the node twice.
10245 if (inner) {
10246 ok = inner->setObject(node);
10247 }
10248 }
10249 if (!ok) {
10250 DLOG("Skipped PM node %s\n", node->getName());
10251 }
10252 }
10253 }
10254
10255 // debug only
10256 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10257 count = 0;
10258 if (inner != PLACEHOLDER) {
10259 count = inner->getCount();
10260 }
10261 DLOG("Nodes at depth %u = %u\n", i, count);
10262 }
10263
10264 // strip placeholders (not all depths are populated)
10265 numWorkers = 0;
10266 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10267 if (inner == PLACEHOLDER) {
10268 gPMHaltArray->removeObject(i);
10269 continue;
10270 }
10271 count = inner->getCount();
10272 if (count > numWorkers) {
10273 numWorkers = count;
10274 }
10275 totalNodes += count;
10276 i++;
10277 }
10278
10279 if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10280 goto done;
10281 }
10282
10283 gPMHaltBusyCount = 0;
10284 gPMHaltIdleCount = 0;
10285 gPMHaltDepth = gPMHaltArray->getCount() - 1;
10286
10287 // Create multiple workers (and threads)
10288
10289 if (numWorkers > kPMHaltMaxWorkers) {
10290 numWorkers = kPMHaltMaxWorkers;
10291 }
10292
10293 DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10294 totalNodes, gPMHaltArray->getCount(), numWorkers);
10295
10296 for (unsigned int i = 0; i < numWorkers; i++) {
10297 workers[i] = PMHaltWorker::worker();
10298 }
10299
10300 // Wait for workers to exhaust all available work
10301
10302 IOLockLock(gPMHaltLock);
10303 while (gPMHaltDepth >= 0) {
10304 clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10305
10306 waitResult = IOLockSleepDeadline(
10307 gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10308 if (THREAD_TIMED_OUT == waitResult) {
10309 AbsoluteTime now;
10310 clock_get_uptime(&now);
10311
10312 IOLockUnlock(gPMHaltLock);
10313 for (unsigned int i = 0; i < numWorkers; i++) {
10314 if (workers[i]) {
10315 PMHaltWorker::checkTimeout(workers[i], &now);
10316 }
10317 }
10318 IOLockLock(gPMHaltLock);
10319 }
10320 }
10321 IOLockUnlock(gPMHaltLock);
10322
10323 // Release all workers
10324
10325 for (unsigned int i = 0; i < numWorkers; i++) {
10326 if (workers[i]) {
10327 workers[i]->release();
10328 }
10329 // worker also retained by it's own thread
10330 }
10331
10332 done:
10333 DLOG("%s done\n", __FUNCTION__);
10334 return;
10335 }
10336
10337 // MARK: -
10338 // MARK: Kernel Assertion
10339
10340 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10341
10342 IOPMDriverAssertionID
createPMAssertion(IOPMDriverAssertionType whichAssertionBits,IOPMDriverAssertionLevel assertionLevel,IOService * ownerService,const char * ownerDescription)10343 IOPMrootDomain::createPMAssertion(
10344 IOPMDriverAssertionType whichAssertionBits,
10345 IOPMDriverAssertionLevel assertionLevel,
10346 IOService *ownerService,
10347 const char *ownerDescription)
10348 {
10349 IOReturn ret;
10350 IOPMDriverAssertionID newAssertion;
10351
10352 if (!pmAssertions) {
10353 return 0;
10354 }
10355
10356 ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10357
10358 if (kIOReturnSuccess == ret) {
10359 return newAssertion;
10360 } else {
10361 return 0;
10362 }
10363 }
10364
10365 IOReturn
releasePMAssertion(IOPMDriverAssertionID releaseAssertion)10366 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10367 {
10368 if (!pmAssertions) {
10369 return kIOReturnInternalError;
10370 }
10371
10372 return pmAssertions->releaseAssertion(releaseAssertion);
10373 }
10374
10375
10376 IOReturn
setPMAssertionLevel(IOPMDriverAssertionID assertionID,IOPMDriverAssertionLevel assertionLevel)10377 IOPMrootDomain::setPMAssertionLevel(
10378 IOPMDriverAssertionID assertionID,
10379 IOPMDriverAssertionLevel assertionLevel)
10380 {
10381 return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10382 }
10383
10384 IOPMDriverAssertionLevel
getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)10385 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10386 {
10387 IOPMDriverAssertionType sysLevels;
10388
10389 if (!pmAssertions || whichAssertion == 0) {
10390 return kIOPMDriverAssertionLevelOff;
10391 }
10392
10393 sysLevels = pmAssertions->getActivatedAssertions();
10394
10395 // Check that every bit set in argument 'whichAssertion' is asserted
10396 // in the aggregate bits.
10397 if ((sysLevels & whichAssertion) == whichAssertion) {
10398 return kIOPMDriverAssertionLevelOn;
10399 } else {
10400 return kIOPMDriverAssertionLevelOff;
10401 }
10402 }
10403
10404 IOReturn
setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)10405 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10406 {
10407 if (!pmAssertions) {
10408 return kIOReturnNotFound;
10409 }
10410
10411 return pmAssertions->setUserAssertionLevels(inLevels);
10412 }
10413
10414 bool
serializeProperties(OSSerialize * s) const10415 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10416 {
10417 if (pmAssertions) {
10418 pmAssertions->publishProperties();
10419 }
10420 return IOService::serializeProperties(s);
10421 }
10422
10423 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const10424 IOPMrootDomain::copyProperty( const char * aKey) const
10425 {
10426 OSSharedPtr<OSObject> obj;
10427 obj = IOService::copyProperty(aKey);
10428
10429 if (obj) {
10430 return obj;
10431 }
10432
10433 if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10434 sizeof(kIOPMSleepWakeWdogRebootKey))) {
10435 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10436 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10437 } else {
10438 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10439 }
10440 }
10441
10442 if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10443 sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10444 if (swd_flags & SWD_VALID_LOGS) {
10445 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10446 } else {
10447 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10448 }
10449 }
10450
10451 /*
10452 * XXX: We should get rid of "DesktopMode" property when 'kAppleClamshellCausesSleepKey'
10453 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10454 * issued by DisplayWrangler on darkwake.
10455 */
10456 if (!strcmp(aKey, "DesktopMode")) {
10457 if (desktopMode) {
10458 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10459 } else {
10460 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10461 }
10462 }
10463 if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10464 if (displayIdleForDemandSleep) {
10465 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10466 } else {
10467 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10468 }
10469 }
10470
10471 if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10472 OSSharedPtr<OSArray> array;
10473 WAKEEVENT_LOCK();
10474 if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10475 OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10476 if (collection) {
10477 array = OSDynamicPtrCast<OSArray>(collection);
10478 }
10479 }
10480 WAKEEVENT_UNLOCK();
10481 return os::move(array);
10482 }
10483
10484 if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10485 OSSharedPtr<OSArray> array;
10486 IOLockLock(pmStatsLock);
10487 if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10488 OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10489 if (collection) {
10490 array = OSDynamicPtrCast<OSArray>(collection);
10491 }
10492 }
10493 IOLockUnlock(pmStatsLock);
10494 return os::move(array);
10495 }
10496
10497 if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10498 OSArray *idleSleepList = NULL;
10499 gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10500 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10501 }
10502
10503 if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10504 OSArray *systemSleepList = NULL;
10505 gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10506 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10507 }
10508
10509 if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10510 OSArray *idleSleepList = NULL;
10511 gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10512 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10513 }
10514
10515 if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10516 OSArray *systemSleepList = NULL;
10517 gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10518 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10519 }
10520 return NULL;
10521 }
10522
10523 // MARK: -
10524 // MARK: Wake Event Reporting
10525
10526 void
copyWakeReasonString(char * outBuf,size_t bufSize)10527 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10528 {
10529 WAKEEVENT_LOCK();
10530 strlcpy(outBuf, gWakeReasonString, bufSize);
10531 WAKEEVENT_UNLOCK();
10532 }
10533
10534 void
copyShutdownReasonString(char * outBuf,size_t bufSize)10535 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10536 {
10537 WAKEEVENT_LOCK();
10538 strlcpy(outBuf, gShutdownReasonString, bufSize);
10539 WAKEEVENT_UNLOCK();
10540 }
10541
10542 //******************************************************************************
10543 // acceptSystemWakeEvents
10544 //
10545 // Private control for the acceptance of driver wake event claims.
10546 //******************************************************************************
10547
10548 void
acceptSystemWakeEvents(uint32_t control)10549 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10550 {
10551 bool logWakeReason = false;
10552
10553 WAKEEVENT_LOCK();
10554 switch (control) {
10555 case kAcceptSystemWakeEvents_Enable:
10556 assert(_acceptSystemWakeEvents == false);
10557 if (!_systemWakeEventsArray) {
10558 _systemWakeEventsArray = OSArray::withCapacity(4);
10559 }
10560 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10561 if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10562 gWakeReasonString[0] = '\0';
10563 if (_systemWakeEventsArray) {
10564 _systemWakeEventsArray->flushCollection();
10565 }
10566 }
10567
10568 // Remove stale WakeType property before system sleep
10569 removeProperty(kIOPMRootDomainWakeTypeKey);
10570 removeProperty(kIOPMRootDomainWakeReasonKey);
10571 break;
10572
10573 case kAcceptSystemWakeEvents_Disable:
10574 _acceptSystemWakeEvents = false;
10575 #if defined(XNU_TARGET_OS_OSX)
10576 logWakeReason = (gWakeReasonString[0] != '\0');
10577 #else /* !defined(XNU_TARGET_OS_OSX) */
10578 logWakeReason = gWakeReasonSysctlRegistered;
10579 #if DEVELOPMENT
10580 static int panic_allowed = -1;
10581
10582 if ((panic_allowed == -1) &&
10583 (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10584 panic_allowed = 0;
10585 }
10586
10587 if (panic_allowed) {
10588 size_t i = 0;
10589 // Panic if wake reason is null or empty
10590 for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10591 if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10592 break;
10593 }
10594 }
10595 if (i >= strlen(gWakeReasonString)) {
10596 panic("Wake reason is empty");
10597 }
10598 }
10599 #endif /* DEVELOPMENT */
10600 #endif /* !defined(XNU_TARGET_OS_OSX) */
10601
10602 // publish kIOPMRootDomainWakeReasonKey if not already set
10603 if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10604 setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10605 }
10606 break;
10607
10608 case kAcceptSystemWakeEvents_Reenable:
10609 assert(_acceptSystemWakeEvents == false);
10610 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10611 removeProperty(kIOPMRootDomainWakeReasonKey);
10612 break;
10613 }
10614 WAKEEVENT_UNLOCK();
10615
10616 if (logWakeReason) {
10617 MSG("system wake events: %s\n", gWakeReasonString);
10618 }
10619 }
10620
10621 //******************************************************************************
10622 // claimSystemWakeEvent
10623 //
10624 // For a driver to claim a device is the source/conduit of a system wake event.
10625 //******************************************************************************
10626
10627 void
claimSystemWakeEvent(IOService * device,IOOptionBits flags,const char * reason,OSObject * details)10628 IOPMrootDomain::claimSystemWakeEvent(
10629 IOService * device,
10630 IOOptionBits flags,
10631 const char * reason,
10632 OSObject * details )
10633 {
10634 OSSharedPtr<const OSSymbol> deviceName;
10635 OSSharedPtr<OSNumber> deviceRegId;
10636 OSSharedPtr<OSNumber> claimTime;
10637 OSSharedPtr<OSData> flagsData;
10638 OSSharedPtr<OSString> reasonString;
10639 OSSharedPtr<OSDictionary> dict;
10640 uint64_t timestamp;
10641 bool addWakeReason;
10642
10643 if (!device || !reason) {
10644 return;
10645 }
10646
10647 pmEventTimeStamp(×tamp);
10648
10649 IOOptionBits aotFlags = 0;
10650 bool needAOTEvaluate = FALSE;
10651
10652 if (kIOPMAOTModeAddEventFlags & _aotMode) {
10653 if (!strcmp("hold", reason)
10654 || !strcmp("help", reason)
10655 || !strcmp("menu", reason)
10656 || !strcmp("stockholm", reason)
10657 || !strcmp("ringer", reason)
10658 || !strcmp("ringerab", reason)
10659 || !strcmp("smc0", reason)
10660 || !strcmp("AOP.RTPWakeupAP", reason)
10661 || !strcmp("AOP.RTP_AP_IRQ", reason)
10662 || !strcmp("BT.OutboxNotEmpty", reason)
10663 || !strcmp("WL.OutboxNotEmpty", reason)) {
10664 flags |= kIOPMWakeEventAOTExit;
10665 }
10666 }
10667
10668 #if DEVELOPMENT || DEBUG
10669 if (_aotLingerTime && !strcmp("rtc", reason)) {
10670 flags |= kIOPMWakeEventAOTPossibleExit;
10671 }
10672 #endif /* DEVELOPMENT || DEBUG */
10673
10674 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10675 // Publishing the WakeType is serialized by the PM work loop
10676 if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10677 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10678 (void *) _nextScheduledAlarmType.get());
10679 }
10680
10681 // Workaround for the missing wake HID event
10682 if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10683 if (!strcmp("trackpadkeyboard", reason)) {
10684 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10685 (void *) gIOPMWakeTypeUserKey.get());
10686 }
10687 }
10688 #endif
10689
10690 deviceName = device->copyName(gIOServicePlane);
10691 deviceRegId = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10692 claimTime = OSNumber::withNumber(timestamp, 64);
10693 flagsData = OSData::withValue(flags);
10694 reasonString = OSString::withCString(reason);
10695 dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
10696 if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
10697 goto done;
10698 }
10699
10700 dict->setObject(gIONameKey, deviceName.get());
10701 dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
10702 dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
10703 dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
10704 dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
10705 if (details) {
10706 dict->setObject(kIOPMWakeEventDetailsKey, details);
10707 }
10708
10709 WAKEEVENT_LOCK();
10710 addWakeReason = _acceptSystemWakeEvents;
10711 if (_aotMode) {
10712 IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
10713 }
10714 aotFlags = (kIOPMWakeEventAOTFlags & flags);
10715 aotFlags = (aotFlags & ~_aotPendingFlags);
10716 needAOTEvaluate = false;
10717 if (_aotNow && aotFlags) {
10718 if (kIOPMWakeEventAOTPossibleExit & flags) {
10719 _aotMetrics->possibleCount++;
10720 }
10721 if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
10722 _aotMetrics->confirmedPossibleCount++;
10723 }
10724 if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
10725 _aotMetrics->rejectedPossibleCount++;
10726 }
10727 if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
10728 _aotMetrics->expiredPossibleCount++;
10729 }
10730
10731 _aotPendingFlags |= aotFlags;
10732 addWakeReason = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
10733 needAOTEvaluate = _aotReadyToFullWake;
10734 }
10735 DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
10736 reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
10737 _aotNow, pmTracer->getTracePhase(), addWakeReason);
10738
10739 if (!gWakeReasonSysctlRegistered) {
10740 // Lazy registration until the platform driver stops registering
10741 // the same name.
10742 gWakeReasonSysctlRegistered = true;
10743 }
10744 if (addWakeReason) {
10745 _systemWakeEventsArray->setObject(dict.get());
10746 if (gWakeReasonString[0] != '\0') {
10747 strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
10748 }
10749 strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
10750 }
10751
10752 WAKEEVENT_UNLOCK();
10753 if (needAOTEvaluate) {
10754 // Call aotEvaluate() on PM work loop since it may call
10755 // aotExit() which accesses PM state.
10756 pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
10757 }
10758
10759 done:
10760 return;
10761 }
10762
10763 //******************************************************************************
10764 // claimSystemBootEvent
10765 //
10766 // For a driver to claim a device is the source/conduit of a system boot event.
10767 //******************************************************************************
10768
10769 void
claimSystemBootEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10770 IOPMrootDomain::claimSystemBootEvent(
10771 IOService * device,
10772 IOOptionBits flags,
10773 const char * reason,
10774 __unused OSObject * details )
10775 {
10776 if (!device || !reason) {
10777 return;
10778 }
10779
10780 DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10781 WAKEEVENT_LOCK();
10782 if (!gBootReasonSysctlRegistered) {
10783 // Lazy sysctl registration after setting gBootReasonString
10784 strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
10785 os_atomic_store(&gBootReasonSysctlRegistered, true, release);
10786 }
10787 WAKEEVENT_UNLOCK();
10788 }
10789
10790 //******************************************************************************
10791 // claimSystemShutdownEvent
10792 //
10793 // For drivers to claim a system shutdown event on the ensuing boot.
10794 //******************************************************************************
10795
10796 void
claimSystemShutdownEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10797 IOPMrootDomain::claimSystemShutdownEvent(
10798 IOService * device,
10799 IOOptionBits flags,
10800 const char * reason,
10801 __unused OSObject * details )
10802 {
10803 if (!device || !reason) {
10804 return;
10805 }
10806
10807 DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10808 WAKEEVENT_LOCK();
10809 if (gShutdownReasonString[0] != '\0') {
10810 strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
10811 }
10812 strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
10813
10814 gShutdownReasonSysctlRegistered = true;
10815 WAKEEVENT_UNLOCK();
10816 }
10817
10818 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10819
10820 // MARK: -
10821 // MARK: PMSettingHandle
10822
OSDefineMetaClassAndStructors(PMSettingHandle,OSObject)10823 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
10824
10825 void
10826 PMSettingHandle::free( void )
10827 {
10828 if (pmso) {
10829 pmso->clientHandleFreed();
10830 pmso->release();
10831 pmso = NULL;
10832 }
10833
10834 OSObject::free();
10835 }
10836
10837 // MARK: -
10838 // MARK: PMSettingObject
10839
10840 #undef super
10841 #define super OSObject
OSDefineMetaClassAndFinalStructors(PMSettingObject,OSObject)10842 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
10843
10844 /*
10845 * Static constructor/initializer for PMSettingObject
10846 */
10847 PMSettingObject *PMSettingObject::pmSettingObject(
10848 IOPMrootDomain * parent_arg,
10849 IOPMSettingControllerCallback handler_arg,
10850 OSObject * target_arg,
10851 uintptr_t refcon_arg,
10852 uint32_t supportedPowerSources,
10853 const OSSymbol * settings[],
10854 OSObject * *handle_obj)
10855 {
10856 uint32_t settingCount = 0;
10857 PMSettingObject *pmso = NULL;
10858 PMSettingHandle *pmsh = NULL;
10859
10860 if (!parent_arg || !handler_arg || !settings || !handle_obj) {
10861 return NULL;
10862 }
10863
10864 // count OSSymbol entries in NULL terminated settings array
10865 while (settings[settingCount]) {
10866 settingCount++;
10867 }
10868 if (0 == settingCount) {
10869 return NULL;
10870 }
10871
10872 pmso = new PMSettingObject;
10873 if (!pmso || !pmso->init()) {
10874 goto fail;
10875 }
10876
10877 pmsh = new PMSettingHandle;
10878 if (!pmsh || !pmsh->init()) {
10879 goto fail;
10880 }
10881
10882 queue_init(&pmso->calloutQueue);
10883 pmso->parent = parent_arg;
10884 pmso->func = handler_arg;
10885 pmso->target = target_arg;
10886 pmso->refcon = refcon_arg;
10887 pmso->settingCount = settingCount;
10888
10889 pmso->retain(); // handle holds a retain on pmso
10890 pmsh->pmso = pmso;
10891 pmso->pmsh = pmsh;
10892
10893 pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
10894 if (pmso->publishedFeatureID) {
10895 for (unsigned int i = 0; i < settingCount; i++) {
10896 // Since there is now at least one listener to this setting, publish
10897 // PM root domain support for it.
10898 parent_arg->publishPMSetting( settings[i],
10899 supportedPowerSources, &pmso->publishedFeatureID[i] );
10900 }
10901 }
10902
10903 *handle_obj = pmsh;
10904 return pmso;
10905
10906 fail:
10907 if (pmso) {
10908 pmso->release();
10909 }
10910 if (pmsh) {
10911 pmsh->release();
10912 }
10913 return NULL;
10914 }
10915
10916 void
free(void)10917 PMSettingObject::free( void )
10918 {
10919 if (publishedFeatureID) {
10920 for (const auto& featureID : publishedFeatureID) {
10921 if (featureID) {
10922 parent->removePublishedFeature( featureID );
10923 }
10924 }
10925
10926 publishedFeatureID = {};
10927 }
10928
10929 super::free();
10930 }
10931
10932 IOReturn
dispatchPMSetting(const OSSymbol * type,OSObject * object)10933 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
10934 {
10935 return (*func)(target, type, object, refcon);
10936 }
10937
10938 void
clientHandleFreed(void)10939 PMSettingObject::clientHandleFreed( void )
10940 {
10941 parent->deregisterPMSettingObject(this);
10942 }
10943
10944 // MARK: -
10945 // MARK: PMAssertionsTracker
10946
10947 //*********************************************************************************
10948 //*********************************************************************************
10949 //*********************************************************************************
10950 // class PMAssertionsTracker Implementation
10951
10952 #define kAssertUniqueIDStart 500
10953
10954 PMAssertionsTracker *
pmAssertionsTracker(IOPMrootDomain * rootDomain)10955 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
10956 {
10957 PMAssertionsTracker *me;
10958
10959 me = new PMAssertionsTracker;
10960 if (!me || !me->init()) {
10961 if (me) {
10962 me->release();
10963 }
10964 return NULL;
10965 }
10966
10967 me->owner = rootDomain;
10968 me->issuingUniqueID = kAssertUniqueIDStart;
10969 me->assertionsArray = OSArray::withCapacity(5);
10970 me->assertionsKernel = 0;
10971 me->assertionsUser = 0;
10972 me->assertionsCombined = 0;
10973 me->assertionsArrayLock = IOLockAlloc();
10974 me->tabulateProducerCount = me->tabulateConsumerCount = 0;
10975
10976 assert(me->assertionsArray);
10977 assert(me->assertionsArrayLock);
10978
10979 return me;
10980 }
10981
10982 /* tabulate
10983 * - Update assertionsKernel to reflect the state of all
10984 * assertions in the kernel.
10985 * - Update assertionsCombined to reflect both kernel & user space.
10986 */
10987 void
tabulate(void)10988 PMAssertionsTracker::tabulate(void)
10989 {
10990 int i;
10991 int count;
10992 const PMAssertStruct *_a = nullptr;
10993 OSValueObject<PMAssertStruct> *_d = nullptr;
10994
10995 IOPMDriverAssertionType oldKernel = assertionsKernel;
10996 IOPMDriverAssertionType oldCombined = assertionsCombined;
10997
10998 ASSERT_GATED();
10999
11000 assertionsKernel = 0;
11001 assertionsCombined = 0;
11002
11003 if (!assertionsArray) {
11004 return;
11005 }
11006
11007 if ((count = assertionsArray->getCount())) {
11008 for (i = 0; i < count; i++) {
11009 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11010 if (_d) {
11011 _a = _d->getBytesNoCopy();
11012 if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11013 assertionsKernel |= _a->assertionBits;
11014 }
11015 }
11016 }
11017 }
11018
11019 tabulateProducerCount++;
11020 assertionsCombined = assertionsKernel | assertionsUser;
11021
11022 if ((assertionsKernel != oldKernel) ||
11023 (assertionsCombined != oldCombined)) {
11024 owner->evaluateAssertions(assertionsCombined, oldCombined);
11025 }
11026 }
11027
11028 void
updateCPUBitAccounting(PMAssertStruct * assertStruct)11029 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11030 {
11031 AbsoluteTime now;
11032 uint64_t nsec;
11033
11034 if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11035 (assertStruct->assertCPUStartTime == 0)) {
11036 return;
11037 }
11038
11039 now = mach_absolute_time();
11040 SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11041 absolutetime_to_nanoseconds(now, &nsec);
11042 assertStruct->assertCPUDuration += nsec;
11043 assertStruct->assertCPUStartTime = 0;
11044
11045 if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11046 maxAssertCPUDuration = assertStruct->assertCPUDuration;
11047 maxAssertCPUEntryId = assertStruct->registryEntryID;
11048 }
11049 }
11050
11051 void
reportCPUBitAccounting(void)11052 PMAssertionsTracker::reportCPUBitAccounting( void )
11053 {
11054 const PMAssertStruct *_a = nullptr;
11055 OSValueObject<PMAssertStruct> *_d = nullptr;
11056 int i, count;
11057 AbsoluteTime now;
11058 uint64_t nsec;
11059
11060 ASSERT_GATED();
11061
11062 // Account for drivers that are still holding the CPU assertion
11063 if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11064 now = mach_absolute_time();
11065 if ((count = assertionsArray->getCount())) {
11066 for (i = 0; i < count; i++) {
11067 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11068 if (_d) {
11069 _a = _d->getBytesNoCopy();
11070 if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11071 (_a->level == kIOPMDriverAssertionLevelOn) &&
11072 (_a->assertCPUStartTime != 0)) {
11073 // Don't modify PMAssertStruct, leave that
11074 // for updateCPUBitAccounting()
11075 SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11076 absolutetime_to_nanoseconds(now, &nsec);
11077 nsec += _a->assertCPUDuration;
11078 if (nsec > maxAssertCPUDuration) {
11079 maxAssertCPUDuration = nsec;
11080 maxAssertCPUEntryId = _a->registryEntryID;
11081 }
11082 }
11083 }
11084 }
11085 }
11086 }
11087
11088 if (maxAssertCPUDuration) {
11089 DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11090 (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11091 }
11092
11093 maxAssertCPUDuration = 0;
11094 maxAssertCPUEntryId = 0;
11095 }
11096
11097 void
publishProperties(void)11098 PMAssertionsTracker::publishProperties( void )
11099 {
11100 OSSharedPtr<OSArray> assertionsSummary;
11101
11102 if (tabulateConsumerCount != tabulateProducerCount) {
11103 IOLockLock(assertionsArrayLock);
11104
11105 tabulateConsumerCount = tabulateProducerCount;
11106
11107 /* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11108 */
11109 assertionsSummary = copyAssertionsArray();
11110 if (assertionsSummary) {
11111 owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11112 } else {
11113 owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11114 }
11115
11116 /* Publish the IOPMrootDomain property "DriverPMAssertions"
11117 */
11118 owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11119
11120 IOLockUnlock(assertionsArrayLock);
11121 }
11122 }
11123
11124 PMAssertStruct *
detailsForID(IOPMDriverAssertionID _id,int * index)11125 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11126 {
11127 PMAssertStruct *_a = NULL;
11128 OSValueObject<PMAssertStruct> *_d = nullptr;
11129 int found = -1;
11130 int count = 0;
11131 int i = 0;
11132
11133 if (assertionsArray
11134 && (count = assertionsArray->getCount())) {
11135 for (i = 0; i < count; i++) {
11136 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11137 if (_d) {
11138 _a = _d->getMutableBytesNoCopy();
11139 if (_a && (_id == _a->id)) {
11140 found = i;
11141 break;
11142 }
11143 }
11144 }
11145 }
11146
11147 if (-1 == found) {
11148 return NULL;
11149 } else {
11150 if (index) {
11151 *index = found;
11152 }
11153 return _a;
11154 }
11155 }
11156
11157 /* PMAssertionsTracker::handleCreateAssertion
11158 * Perform assertion work on the PM workloop. Do not call directly.
11159 */
11160 IOReturn
handleCreateAssertion(OSValueObject<PMAssertStruct> * newAssertion)11161 PMAssertionsTracker::handleCreateAssertion(OSValueObject<PMAssertStruct> *newAssertion)
11162 {
11163 PMAssertStruct *assertStruct = nullptr;
11164
11165 ASSERT_GATED();
11166
11167 if (newAssertion) {
11168 IOLockLock(assertionsArrayLock);
11169 assertStruct = newAssertion->getMutableBytesNoCopy();
11170 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11171 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11172 assertStruct->assertCPUStartTime = mach_absolute_time();
11173 }
11174 assertionsArray->setObject(newAssertion);
11175 IOLockUnlock(assertionsArrayLock);
11176 newAssertion->release();
11177
11178 tabulate();
11179 }
11180 return kIOReturnSuccess;
11181 }
11182
11183 /* PMAssertionsTracker::createAssertion
11184 * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11185 * appropiate.
11186 */
11187 IOReturn
createAssertion(IOPMDriverAssertionType which,IOPMDriverAssertionLevel level,IOService * serviceID,const char * whoItIs,IOPMDriverAssertionID * outID)11188 PMAssertionsTracker::createAssertion(
11189 IOPMDriverAssertionType which,
11190 IOPMDriverAssertionLevel level,
11191 IOService *serviceID,
11192 const char *whoItIs,
11193 IOPMDriverAssertionID *outID)
11194 {
11195 OSSharedPtr<OSValueObject<PMAssertStruct> > dataStore;
11196 PMAssertStruct track;
11197
11198 // Warning: trillions and trillions of created assertions may overflow the unique ID.
11199 track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11200 track.level = level;
11201 track.assertionBits = which;
11202
11203 // NB: ownerString is explicitly managed by PMAssertStruct
11204 // it will be released in `handleReleaseAssertion' below
11205 track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11206 track.ownerService = serviceID;
11207 track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11208 track.modifiedTime = 0;
11209 pmEventTimeStamp(&track.createdTime);
11210 track.assertCPUStartTime = 0;
11211 track.assertCPUDuration = 0;
11212
11213 dataStore = OSValueObjectWithValue(track);
11214 if (!dataStore) {
11215 if (track.ownerString) {
11216 track.ownerString->release();
11217 track.ownerString = NULL;
11218 }
11219 return kIOReturnNoMemory;
11220 }
11221
11222 *outID = track.id;
11223
11224 if (owner && owner->pmPowerStateQueue) {
11225 // queue action is responsible for releasing dataStore
11226 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11227 }
11228
11229 return kIOReturnSuccess;
11230 }
11231
11232 /* PMAssertionsTracker::handleReleaseAssertion
11233 * Runs in PM workloop. Do not call directly.
11234 */
11235 IOReturn
handleReleaseAssertion(IOPMDriverAssertionID _id)11236 PMAssertionsTracker::handleReleaseAssertion(
11237 IOPMDriverAssertionID _id)
11238 {
11239 ASSERT_GATED();
11240
11241 int index;
11242 PMAssertStruct *assertStruct = detailsForID(_id, &index);
11243
11244 if (!assertStruct) {
11245 return kIOReturnNotFound;
11246 }
11247
11248 IOLockLock(assertionsArrayLock);
11249
11250 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11251 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11252 updateCPUBitAccounting(assertStruct);
11253 }
11254
11255 if (assertStruct->ownerString) {
11256 assertStruct->ownerString->release();
11257 assertStruct->ownerString = NULL;
11258 }
11259
11260 assertionsArray->removeObject(index);
11261 IOLockUnlock(assertionsArrayLock);
11262
11263 tabulate();
11264 return kIOReturnSuccess;
11265 }
11266
11267 /* PMAssertionsTracker::releaseAssertion
11268 * Releases an assertion and affects system behavior if appropiate.
11269 * Actual work happens on PM workloop.
11270 */
11271 IOReturn
releaseAssertion(IOPMDriverAssertionID _id)11272 PMAssertionsTracker::releaseAssertion(
11273 IOPMDriverAssertionID _id)
11274 {
11275 if (owner && owner->pmPowerStateQueue) {
11276 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11277 }
11278 return kIOReturnSuccess;
11279 }
11280
11281 /* PMAssertionsTracker::handleSetAssertionLevel
11282 * Runs in PM workloop. Do not call directly.
11283 */
11284 IOReturn
handleSetAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11285 PMAssertionsTracker::handleSetAssertionLevel(
11286 IOPMDriverAssertionID _id,
11287 IOPMDriverAssertionLevel _level)
11288 {
11289 PMAssertStruct *assertStruct = detailsForID(_id, NULL);
11290
11291 ASSERT_GATED();
11292
11293 if (!assertStruct) {
11294 return kIOReturnNotFound;
11295 }
11296
11297 IOLockLock(assertionsArrayLock);
11298 pmEventTimeStamp(&assertStruct->modifiedTime);
11299 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11300 (assertStruct->level != _level)) {
11301 if (_level == kIOPMDriverAssertionLevelOn) {
11302 assertStruct->assertCPUStartTime = mach_absolute_time();
11303 } else {
11304 updateCPUBitAccounting(assertStruct);
11305 }
11306 }
11307 assertStruct->level = _level;
11308 IOLockUnlock(assertionsArrayLock);
11309
11310 tabulate();
11311 return kIOReturnSuccess;
11312 }
11313
11314 /* PMAssertionsTracker::setAssertionLevel
11315 */
11316 IOReturn
setAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11317 PMAssertionsTracker::setAssertionLevel(
11318 IOPMDriverAssertionID _id,
11319 IOPMDriverAssertionLevel _level)
11320 {
11321 if (owner && owner->pmPowerStateQueue) {
11322 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11323 (void *)(uintptr_t)_level, _id);
11324 }
11325
11326 return kIOReturnSuccess;
11327 }
11328
11329 IOReturn
handleSetUserAssertionLevels(void * arg0)11330 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11331 {
11332 IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11333
11334 ASSERT_GATED();
11335
11336 if (new_user_levels != assertionsUser) {
11337 DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11338 assertionsUser = new_user_levels;
11339 }
11340
11341 tabulate();
11342 return kIOReturnSuccess;
11343 }
11344
11345 IOReturn
setUserAssertionLevels(IOPMDriverAssertionType new_user_levels)11346 PMAssertionsTracker::setUserAssertionLevels(
11347 IOPMDriverAssertionType new_user_levels)
11348 {
11349 if (gIOPMWorkLoop) {
11350 gIOPMWorkLoop->runAction(
11351 OSMemberFunctionCast(
11352 IOWorkLoop::Action,
11353 this,
11354 &PMAssertionsTracker::handleSetUserAssertionLevels),
11355 this,
11356 (void *) &new_user_levels, NULL, NULL, NULL);
11357 }
11358
11359 return kIOReturnSuccess;
11360 }
11361
11362
11363 OSSharedPtr<OSArray>
copyAssertionsArray(void)11364 PMAssertionsTracker::copyAssertionsArray(void)
11365 {
11366 int count;
11367 int i;
11368 OSSharedPtr<OSArray> outArray = NULL;
11369
11370 if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11371 goto exit;
11372 }
11373 outArray = OSArray::withCapacity(count);
11374 if (!outArray) {
11375 goto exit;
11376 }
11377
11378 for (i = 0; i < count; i++) {
11379 const PMAssertStruct *_a = nullptr;
11380 OSValueObject<PMAssertStruct> *_d = nullptr;
11381 OSSharedPtr<OSDictionary> details;
11382
11383 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11384 if (_d && (_a = _d->getBytesNoCopy())) {
11385 OSSharedPtr<OSNumber> _n;
11386
11387 details = OSDictionary::withCapacity(7);
11388 if (!details) {
11389 continue;
11390 }
11391
11392 outArray->setObject(details.get());
11393
11394 _n = OSNumber::withNumber(_a->id, 64);
11395 if (_n) {
11396 details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11397 }
11398 _n = OSNumber::withNumber(_a->createdTime, 64);
11399 if (_n) {
11400 details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11401 }
11402 _n = OSNumber::withNumber(_a->modifiedTime, 64);
11403 if (_n) {
11404 details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11405 }
11406 _n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11407 if (_n) {
11408 details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11409 }
11410 _n = OSNumber::withNumber(_a->level, 64);
11411 if (_n) {
11412 details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11413 }
11414 _n = OSNumber::withNumber(_a->assertionBits, 64);
11415 if (_n) {
11416 details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11417 }
11418
11419 if (_a->ownerString) {
11420 details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11421 }
11422 }
11423 }
11424
11425 exit:
11426 return os::move(outArray);
11427 }
11428
11429 IOPMDriverAssertionType
getActivatedAssertions(void)11430 PMAssertionsTracker::getActivatedAssertions(void)
11431 {
11432 return assertionsCombined;
11433 }
11434
11435 IOPMDriverAssertionLevel
getAssertionLevel(IOPMDriverAssertionType type)11436 PMAssertionsTracker::getAssertionLevel(
11437 IOPMDriverAssertionType type)
11438 {
11439 // FIXME: unused and also wrong
11440 if (type && ((type & assertionsKernel) == assertionsKernel)) {
11441 return kIOPMDriverAssertionLevelOn;
11442 } else {
11443 return kIOPMDriverAssertionLevelOff;
11444 }
11445 }
11446
11447 //*********************************************************************************
11448 //*********************************************************************************
11449 //*********************************************************************************
11450
11451
11452 static void
pmEventTimeStamp(uint64_t * recordTS)11453 pmEventTimeStamp(uint64_t *recordTS)
11454 {
11455 clock_sec_t tsec;
11456 clock_usec_t tusec;
11457
11458 if (!recordTS) {
11459 return;
11460 }
11461
11462 // We assume tsec fits into 32 bits; 32 bits holds enough
11463 // seconds for 136 years since the epoch in 1970.
11464 clock_get_calendar_microtime(&tsec, &tusec);
11465
11466
11467 // Pack the sec & microsec calendar time into a uint64_t, for fun.
11468 *recordTS = 0;
11469 *recordTS |= (uint32_t)tusec;
11470 *recordTS |= ((uint64_t)tsec << 32);
11471
11472 return;
11473 }
11474
11475 // MARK: -
11476 // MARK: IORootParent
11477
11478 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11479
11480 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11481
11482 // The reason that root domain needs a root parent is to facilitate demand
11483 // sleep, since a power change from the root parent cannot be vetoed.
11484 //
11485 // The above statement is no longer true since root domain now performs
11486 // demand sleep using overrides. But root parent remains to avoid changing
11487 // the power tree stacking. Root parent is parked at the max power state.
11488
11489
11490 static IOPMPowerState patriarchPowerStates[2] =
11491 {
11492 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11493 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11494 };
11495
11496 void
initialize(void)11497 IORootParent::initialize( void )
11498 {
11499
11500 gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11501 gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11502 gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11503 gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11504 gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11505 gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11506 gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11507 gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11508 gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11509 gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11510 gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11511 gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11512 gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11513 gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11514 gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11515 gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11516 gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11517 gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11518 gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11519 gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11520 gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11521 gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11522 gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11523 gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11524 gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11525 gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11526 gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11527 gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11528 gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11529 gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11530 gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11531 gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11532 gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11533 gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11534 gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11535 gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11536 gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11537 gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11538 gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11539 gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11540 gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11541 gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11542 gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11543 gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11544 }
11545
11546 bool
start(IOService * nub)11547 IORootParent::start( IOService * nub )
11548 {
11549 IOService::start(nub);
11550 attachToParent( getRegistryRoot(), gIOPowerPlane );
11551 PMinit();
11552 registerPowerDriver(this, patriarchPowerStates, 2);
11553 makeUsable();
11554 return true;
11555 }
11556
11557 void
shutDownSystem(void)11558 IORootParent::shutDownSystem( void )
11559 {
11560 }
11561
11562 void
restartSystem(void)11563 IORootParent::restartSystem( void )
11564 {
11565 }
11566
11567 void
sleepSystem(void)11568 IORootParent::sleepSystem( void )
11569 {
11570 }
11571
11572 void
dozeSystem(void)11573 IORootParent::dozeSystem( void )
11574 {
11575 }
11576
11577 void
sleepToDoze(void)11578 IORootParent::sleepToDoze( void )
11579 {
11580 }
11581
11582 void
wakeSystem(void)11583 IORootParent::wakeSystem( void )
11584 {
11585 }
11586
11587 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const11588 IORootParent::copyProperty( const char * aKey) const
11589 {
11590 return IOService::copyProperty(aKey);
11591 }
11592
11593 uint32_t
getWatchdogTimeout()11594 IOPMrootDomain::getWatchdogTimeout()
11595 {
11596 if (gSwdSleepWakeTimeout) {
11597 gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11598 }
11599 if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11600 (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11601 return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11602 } else {
11603 return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11604 }
11605 }
11606
11607
11608 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
11609 IOReturn
restartWithStackshot()11610 IOPMrootDomain::restartWithStackshot()
11611 {
11612 takeStackshot(true);
11613
11614 return kIOReturnSuccess;
11615 }
11616
11617 void
sleepWakeDebugTrig(bool wdogTrigger)11618 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
11619 {
11620 takeStackshot(wdogTrigger);
11621 }
11622
11623 void
tracePhase2String(uint32_t tracePhase,const char ** phaseString,const char ** description)11624 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
11625 {
11626 switch (tracePhase) {
11627 case kIOPMTracePointSleepStarted:
11628 *phaseString = "kIOPMTracePointSleepStarted";
11629 *description = "starting sleep";
11630 break;
11631
11632 case kIOPMTracePointSleepApplications:
11633 *phaseString = "kIOPMTracePointSleepApplications";
11634 *description = "notifying applications";
11635 break;
11636
11637 case kIOPMTracePointSleepPriorityClients:
11638 *phaseString = "kIOPMTracePointSleepPriorityClients";
11639 *description = "notifying clients about upcoming system capability changes";
11640 break;
11641
11642 case kIOPMTracePointSleepWillChangeInterests:
11643 *phaseString = "kIOPMTracePointSleepWillChangeInterests";
11644 *description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
11645 break;
11646
11647 case kIOPMTracePointSleepPowerPlaneDrivers:
11648 *phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
11649 *description = "calling power state change callbacks";
11650 break;
11651
11652 case kIOPMTracePointSleepDidChangeInterests:
11653 *phaseString = "kIOPMTracePointSleepDidChangeInterests";
11654 *description = "calling rootDomain's clients about rootDomain's state changes";
11655 break;
11656
11657 case kIOPMTracePointSleepCapabilityClients:
11658 *phaseString = "kIOPMTracePointSleepCapabilityClients";
11659 *description = "notifying clients about current system capabilities";
11660 break;
11661
11662 case kIOPMTracePointSleepPlatformActions:
11663 *phaseString = "kIOPMTracePointSleepPlatformActions";
11664 *description = "calling Quiesce/Sleep action callbacks";
11665 break;
11666
11667 case kIOPMTracePointSleepCPUs:
11668 {
11669 *phaseString = "kIOPMTracePointSleepCPUs";
11670 #if defined(__i386__) || defined(__x86_64__)
11671 /*
11672 * We cannot use the getCPUNumber() method to get the cpu number, since
11673 * that cpu number is unrelated to the cpu number we need (we need the cpu
11674 * number as enumerated by the scheduler, NOT the CPU number enumerated
11675 * by ACPIPlatform as the CPUs are enumerated in MADT order).
11676 * Instead, pass the Mach processor pointer associated with the current
11677 * shutdown target so its associated cpu_id can be used in
11678 * processor_to_datastring.
11679 */
11680 if (currentShutdownTarget != NULL &&
11681 currentShutdownTarget->getMachProcessor() != NULL) {
11682 const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
11683 currentShutdownTarget->getMachProcessor());
11684 *description = sbuf;
11685 } else {
11686 *description = "halting all non-boot CPUs";
11687 }
11688 #else
11689 *description = "halting all non-boot CPUs";
11690 #endif
11691 break;
11692 }
11693 case kIOPMTracePointSleepPlatformDriver:
11694 *phaseString = "kIOPMTracePointSleepPlatformDriver";
11695 *description = "executing platform specific code";
11696 break;
11697
11698 case kIOPMTracePointHibernate:
11699 *phaseString = "kIOPMTracePointHibernate";
11700 *description = "writing the hibernation image";
11701 break;
11702
11703 case kIOPMTracePointSystemSleep:
11704 *phaseString = "kIOPMTracePointSystemSleep";
11705 *description = "in EFI/Bootrom after last point of entry to sleep";
11706 break;
11707
11708 case kIOPMTracePointWakePlatformDriver:
11709 *phaseString = "kIOPMTracePointWakePlatformDriver";
11710 *description = "executing platform specific code";
11711 break;
11712
11713
11714 case kIOPMTracePointWakePlatformActions:
11715 *phaseString = "kIOPMTracePointWakePlatformActions";
11716 *description = "calling Wake action callbacks";
11717 break;
11718
11719 case kIOPMTracePointWakeCPUs:
11720 *phaseString = "kIOPMTracePointWakeCPUs";
11721 *description = "starting non-boot CPUs";
11722 break;
11723
11724 case kIOPMTracePointWakeWillPowerOnClients:
11725 *phaseString = "kIOPMTracePointWakeWillPowerOnClients";
11726 *description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
11727 break;
11728
11729 case kIOPMTracePointWakeWillChangeInterests:
11730 *phaseString = "kIOPMTracePointWakeWillChangeInterests";
11731 *description = "calling rootDomain's clients about upcoming rootDomain's state changes";
11732 break;
11733
11734 case kIOPMTracePointWakeDidChangeInterests:
11735 *phaseString = "kIOPMTracePointWakeDidChangeInterests";
11736 *description = "calling rootDomain's clients about completed rootDomain's state changes";
11737 break;
11738
11739 case kIOPMTracePointWakePowerPlaneDrivers:
11740 *phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
11741 *description = "calling power state change callbacks";
11742 break;
11743
11744 case kIOPMTracePointWakeCapabilityClients:
11745 *phaseString = "kIOPMTracePointWakeCapabilityClients";
11746 *description = "informing clients about current system capabilities";
11747 break;
11748
11749 case kIOPMTracePointWakeApplications:
11750 *phaseString = "kIOPMTracePointWakeApplications";
11751 *description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
11752 break;
11753
11754 case kIOPMTracePointDarkWakeEntry:
11755 *phaseString = "kIOPMTracePointDarkWakeEntry";
11756 *description = "entering darkwake on way to sleep";
11757 break;
11758
11759 case kIOPMTracePointDarkWakeExit:
11760 *phaseString = "kIOPMTracePointDarkWakeExit";
11761 *description = "entering fullwake from darkwake";
11762 break;
11763
11764 default:
11765 *phaseString = NULL;
11766 *description = NULL;
11767 }
11768 }
11769
11770 void
saveFailureData2File()11771 IOPMrootDomain::saveFailureData2File()
11772 {
11773 unsigned int len = 0;
11774 char failureStr[512];
11775 errno_t error;
11776 char *outbuf;
11777 OSNumber *statusCode;
11778 uint64_t pmStatusCode = 0;
11779 uint32_t phaseData = 0;
11780 uint32_t phaseDetail = 0;
11781 bool efiFailure = false;
11782
11783 OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
11784 statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
11785 if (statusCode) {
11786 pmStatusCode = statusCode->unsigned64BitValue();
11787 phaseData = pmStatusCode & 0xFFFFFFFF;
11788 phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
11789 if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
11790 LOG("Sleep Wake failure in EFI\n");
11791 efiFailure = true;
11792 failureStr[0] = 0;
11793 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);
11794 len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
11795 }
11796 }
11797
11798 if (!efiFailure) {
11799 if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
11800 swd_flags |= SWD_BOOT_BY_SW_WDOG;
11801 PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
11802 // dump panic will handle saving nvram data
11803 return;
11804 }
11805
11806 /* Keeping this around for capturing data during power
11807 * button press */
11808
11809 if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
11810 DLOG("No sleep wake failure string\n");
11811 return;
11812 }
11813 if (len == 0) {
11814 DLOG("Ignoring zero byte SleepWake failure string\n");
11815 goto exit;
11816 }
11817
11818 // if PMStatus code is zero, delete stackshot and return
11819 if (statusCode) {
11820 if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
11821 // there was no sleep wake failure
11822 // this can happen if delete stackshot was called
11823 // before take stackshot completed. Let us delete any
11824 // sleep wake failure data in nvram
11825 DLOG("Deleting stackshot on successful wake\n");
11826 deleteStackshot();
11827 return;
11828 }
11829 }
11830
11831 if (len > sizeof(failureStr)) {
11832 len = sizeof(failureStr);
11833 }
11834 failureStr[0] = 0;
11835 PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
11836 }
11837 if (failureStr[0] != 0) {
11838 error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
11839 if (error) {
11840 DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
11841 } else {
11842 DLOG("Saved SleepWake failure string to file.\n");
11843 }
11844 }
11845
11846 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
11847 goto exit;
11848 }
11849
11850 if (swd_buffer) {
11851 unsigned int len = 0;
11852 errno_t error;
11853 char nvram_var_name_buffer[20];
11854 unsigned int concat_len = 0;
11855 swd_hdr *hdr = NULL;
11856
11857
11858 hdr = (swd_hdr *)swd_buffer;
11859 outbuf = (char *)hdr + hdr->spindump_offset;
11860 OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
11861
11862 for (int i = 0; i < 8; i++) {
11863 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
11864 if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
11865 LOG("No SleepWake blob to read beyond chunk %d\n", i);
11866 break;
11867 }
11868 if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
11869 PERemoveNVRAMProperty(nvram_var_name_buffer);
11870 LOG("Could not read the property :-(\n");
11871 break;
11872 }
11873 PERemoveNVRAMProperty(nvram_var_name_buffer);
11874 concat_len += len;
11875 }
11876 LOG("Concatenated length for the SWD blob %d\n", concat_len);
11877
11878 if (concat_len) {
11879 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
11880 if (error) {
11881 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11882 } else {
11883 LOG("Saved SleepWake zipped data to file.\n");
11884 }
11885 } else {
11886 // There is a sleep wake failure string but no stackshot
11887 // Write a placeholder stacks file so that swd runs
11888 snprintf(outbuf, 20, "%s", "No stackshot data\n");
11889 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
11890 if (error) {
11891 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11892 } else {
11893 LOG("Saved SleepWake zipped data to file.\n");
11894 }
11895 }
11896 } else {
11897 LOG("No buffer allocated to save failure stackshot\n");
11898 }
11899
11900
11901 gRootDomain->swd_lock = 0;
11902 exit:
11903 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
11904 return;
11905 }
11906
11907
11908 void
getFailureData(thread_t * thread,char * failureStr,size_t strLen)11909 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
11910 {
11911 OSSharedPtr<IORegistryIterator> iter;
11912 OSSharedPtr<const OSSymbol> kextName = NULL;
11913 IORegistryEntry * entry;
11914 IOService * node;
11915 bool nodeFound = false;
11916
11917 const void * callMethod = NULL;
11918 const char * objectName = NULL;
11919 uint32_t timeout = getWatchdogTimeout();
11920 const char * phaseString = NULL;
11921 const char * phaseDescription = NULL;
11922
11923 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
11924 uint32_t tracePhase = pmTracer->getTracePhase();
11925
11926 *thread = NULL;
11927 if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
11928 snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
11929 } else {
11930 snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
11931 }
11932 tracePhase2String(tracePhase, &phaseString, &phaseDescription);
11933
11934 if (notifierThread) {
11935 if (notifier && (notifier->identifier)) {
11936 objectName = notifier->identifier->getCStringNoCopy();
11937 }
11938 *thread = notifierThread;
11939 } else {
11940 iter = IORegistryIterator::iterateOver(
11941 getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
11942
11943 if (iter) {
11944 while ((entry = iter->getNextObject())) {
11945 node = OSDynamicCast(IOService, entry);
11946 if (!node) {
11947 continue;
11948 }
11949 if (OSDynamicCast(IOPowerConnection, node)) {
11950 continue;
11951 }
11952
11953 if (node->getBlockingDriverCall(thread, &callMethod)) {
11954 nodeFound = true;
11955 break;
11956 }
11957 }
11958 }
11959 if (nodeFound) {
11960 kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
11961 if (kextName) {
11962 objectName = kextName->getCStringNoCopy();
11963 }
11964 }
11965 }
11966 if (phaseDescription) {
11967 strlcat(failureStr, " while ", strLen);
11968 strlcat(failureStr, phaseDescription, strLen);
11969 strlcat(failureStr, ".", strLen);
11970 }
11971 if (objectName) {
11972 strlcat(failureStr, " Suspected bundle: ", strLen);
11973 strlcat(failureStr, objectName, strLen);
11974 strlcat(failureStr, ".", strLen);
11975 }
11976 if (*thread) {
11977 char threadName[40];
11978 snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
11979 strlcat(failureStr, threadName, strLen);
11980 }
11981
11982 DLOG("%s\n", failureStr);
11983 }
11984
11985 struct swd_stackshot_compressed_data {
11986 z_output_func zoutput;
11987 size_t zipped;
11988 uint64_t totalbytes;
11989 uint64_t lastpercent;
11990 IOReturn error;
11991 unsigned outremain;
11992 unsigned outlen;
11993 unsigned writes;
11994 Bytef * outbuf;
11995 };
11996 struct swd_stackshot_compressed_data swd_zip_var = { };
11997
11998 static void *
swd_zs_alloc(void * __unused ref,u_int items,u_int size)11999 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
12000 {
12001 void *result;
12002 LOG("Alloc in zipping %d items of size %d\n", items, size);
12003
12004 result = (void *)(swd_zs_zmem + swd_zs_zoffset);
12005 swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
12006 LOG("Offset %zu\n", swd_zs_zoffset);
12007 return result;
12008 }
12009
12010 static int
swd_zinput(z_streamp strm,Bytef * buf,unsigned size)12011 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12012 {
12013 unsigned len;
12014
12015 len = strm->avail_in;
12016
12017 if (len > size) {
12018 len = size;
12019 }
12020 if (len == 0) {
12021 return 0;
12022 }
12023
12024 if (strm->next_in != (Bytef *) strm) {
12025 memcpy(buf, strm->next_in, len);
12026 } else {
12027 bzero(buf, len);
12028 }
12029
12030 strm->adler = z_crc32(strm->adler, buf, len);
12031
12032 strm->avail_in -= len;
12033 strm->next_in += len;
12034 strm->total_in += len;
12035
12036 return (int)len;
12037 }
12038
12039 static int
swd_zoutput(z_streamp strm,Bytef * buf,unsigned len)12040 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12041 {
12042 unsigned int i = 0;
12043 // if outlen > max size don't add to the buffer
12044 assert(buf != NULL);
12045 if (strm && buf) {
12046 if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12047 LOG("No space to GZIP... not writing to NVRAM\n");
12048 return len;
12049 }
12050 }
12051 for (i = 0; i < len; i++) {
12052 *(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12053 }
12054 swd_zip_var.outlen += len;
12055 return len;
12056 }
12057
12058 static void
swd_zs_free(void * __unused ref,void * __unused ptr)12059 swd_zs_free(void * __unused ref, void * __unused ptr)
12060 {
12061 }
12062
12063 static int
swd_compress(char * inPtr,char * outPtr,size_t numBytes)12064 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12065 {
12066 int wbits = 12;
12067 int memlevel = 3;
12068
12069 if (((unsigned int) numBytes) != numBytes) {
12070 return 0;
12071 }
12072
12073 if (!swd_zs.zalloc) {
12074 swd_zs.zalloc = swd_zs_alloc;
12075 swd_zs.zfree = swd_zs_free;
12076 if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12077 // allocation failed
12078 bzero(&swd_zs, sizeof(swd_zs));
12079 // swd_zs_zoffset = 0;
12080 } else {
12081 LOG("PMRD inited the zlib allocation routines\n");
12082 }
12083 }
12084
12085 swd_zip_var.zipped = 0;
12086 swd_zip_var.totalbytes = 0; // should this be the max that we have?
12087 swd_zip_var.lastpercent = 0;
12088 swd_zip_var.error = kIOReturnSuccess;
12089 swd_zip_var.outremain = 0;
12090 swd_zip_var.outlen = 0;
12091 swd_zip_var.writes = 0;
12092 swd_zip_var.outbuf = (Bytef *)outPtr;
12093
12094 swd_zip_var.totalbytes = numBytes;
12095
12096 swd_zs.avail_in = 0;
12097 swd_zs.next_in = NULL;
12098 swd_zs.avail_out = 0;
12099 swd_zs.next_out = NULL;
12100
12101 deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12102
12103 z_stream *zs;
12104 int zr;
12105 zs = &swd_zs;
12106
12107 while (swd_zip_var.error >= 0) {
12108 if (!zs->avail_in) {
12109 zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12110 zs->avail_in = (unsigned int) numBytes;
12111 }
12112 if (!zs->avail_out) {
12113 zs->next_out = (Bytef *)zs;
12114 zs->avail_out = UINT32_MAX;
12115 }
12116 zr = deflate(zs, Z_NO_FLUSH);
12117 if (Z_STREAM_END == zr) {
12118 break;
12119 }
12120 if (zr != Z_OK) {
12121 LOG("ZERR %d\n", zr);
12122 swd_zip_var.error = zr;
12123 } else {
12124 if (zs->total_in == numBytes) {
12125 break;
12126 }
12127 }
12128 }
12129
12130 //now flush the stream
12131 while (swd_zip_var.error >= 0) {
12132 if (!zs->avail_out) {
12133 zs->next_out = (Bytef *)zs;
12134 zs->avail_out = UINT32_MAX;
12135 }
12136 zr = deflate(zs, Z_FINISH);
12137 if (Z_STREAM_END == zr) {
12138 break;
12139 }
12140 if (zr != Z_OK) {
12141 LOG("ZERR %d\n", zr);
12142 swd_zip_var.error = zr;
12143 } else {
12144 if (zs->total_in == numBytes) {
12145 LOG("Total output size %d\n", swd_zip_var.outlen);
12146 break;
12147 }
12148 }
12149 }
12150
12151 return swd_zip_var.outlen;
12152 }
12153
12154 void
deleteStackshot()12155 IOPMrootDomain::deleteStackshot()
12156 {
12157 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12158 // takeStackshot hasn't completed
12159 return;
12160 }
12161 LOG("Deleting any sleepwake failure data in nvram\n");
12162
12163 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12164 char nvram_var_name_buf[20];
12165 for (int i = 0; i < 8; i++) {
12166 snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12167 if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12168 LOG("Removing %s returned false\n", nvram_var_name_buf);
12169 }
12170 }
12171 // force NVRAM sync
12172 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12173 DLOG("Failed to force nvram sync\n");
12174 }
12175 gRootDomain->swd_lock = 0;
12176 }
12177
12178 void
takeStackshot(bool wdogTrigger)12179 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12180 {
12181 swd_hdr * hdr = NULL;
12182 int cnt = 0;
12183 int max_cnt = 2;
12184 pid_t pid = 0;
12185 kern_return_t kr = KERN_SUCCESS;
12186 uint64_t flags;
12187
12188 char * dstAddr;
12189 uint32_t size;
12190 uint32_t bytesRemaining;
12191 unsigned bytesWritten = 0;
12192
12193 char failureStr[512];
12194 thread_t thread = NULL;
12195 const char * swfPanic = "swfPanic";
12196
12197 uint32_t bufSize;
12198 int success = 0;
12199
12200 #if defined(__i386__) || defined(__x86_64__)
12201 const bool concise = false;
12202 #else
12203 const bool concise = true;
12204 #endif
12205
12206 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12207 return;
12208 }
12209
12210 failureStr[0] = 0;
12211 if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12212 return;
12213 }
12214
12215 if (wdogTrigger) {
12216 getFailureData(&thread, failureStr, sizeof(failureStr));
12217
12218 if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12219 goto skip_stackshot;
12220 }
12221 } else {
12222 AbsoluteTime now;
12223 uint64_t nsec;
12224 clock_get_uptime(&now);
12225 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12226 absolutetime_to_nanoseconds(now, &nsec);
12227 snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12228 }
12229
12230 if (swd_buffer == NULL) {
12231 sleepWakeDebugMemAlloc();
12232 if (swd_buffer == NULL) {
12233 return;
12234 }
12235 }
12236 hdr = (swd_hdr *)swd_buffer;
12237 bufSize = hdr->alloc_size;
12238
12239 dstAddr = (char*)hdr + hdr->spindump_offset;
12240 flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_NO_IO_STATS | STACKSHOT_SAVE_KEXT_LOADINFO | STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY | STACKSHOT_THREAD_WAITINFO;
12241 /* If not wdogTrigger only take kernel tasks stackshot
12242 */
12243 if (wdogTrigger) {
12244 pid = -1;
12245 } else {
12246 pid = 0;
12247 }
12248
12249 /* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12250 * If we run out of space, take stackshot with only kernel task
12251 */
12252 while (success == 0 && cnt < max_cnt) {
12253 bytesRemaining = bufSize - hdr->spindump_offset;
12254 cnt++;
12255 DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12256
12257 size = bytesRemaining;
12258 kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12259 DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12260 kr, pid, size, flags, bytesWritten);
12261 if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12262 if (pid == -1) {
12263 pid = 0;
12264 } else {
12265 LOG("Insufficient buffer size for only kernel task\n");
12266 break;
12267 }
12268 }
12269 if (kr == KERN_SUCCESS) {
12270 if (bytesWritten == 0) {
12271 MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12272 continue;
12273 }
12274 bytesRemaining -= bytesWritten;
12275 hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12276
12277 memset(hdr->reason, 0x20, sizeof(hdr->reason));
12278
12279 // Compress stackshot and save to NVRAM
12280 {
12281 char *outbuf = (char *)swd_compressed_buffer;
12282 int outlen = 0;
12283 int num_chunks = 0;
12284 int max_chunks = 0;
12285 int leftover = 0;
12286 char nvram_var_name_buffer[20];
12287
12288 outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12289
12290 if (outlen) {
12291 max_chunks = outlen / (2096 - 200);
12292 leftover = outlen % (2096 - 200);
12293
12294 if (max_chunks < 8) {
12295 for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12296 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12297 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12298 LOG("Failed to update NVRAM %d\n", num_chunks);
12299 break;
12300 }
12301 }
12302 if (leftover) {
12303 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12304 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12305 LOG("Failed to update NVRAM with leftovers\n");
12306 }
12307 }
12308 success = 1;
12309 LOG("Successfully saved stackshot to NVRAM\n");
12310 } else {
12311 LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12312 if (pid == -1) {
12313 pid = 0;
12314 } else {
12315 LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12316 break;
12317 }
12318 }
12319 }
12320 }
12321 }
12322 }
12323
12324 if (failureStr[0]) {
12325 // append sleep-wake failure code
12326 char traceCode[80];
12327 snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12328 pmTracer->getTraceData(), pmTracer->getTracePhase());
12329 strlcat(failureStr, traceCode, sizeof(failureStr));
12330 if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12331 DLOG("Failed to write SleepWake failure string\n");
12332 }
12333 }
12334
12335 // force NVRAM sync
12336 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12337 DLOG("Failed to force nvram sync\n");
12338 }
12339
12340 skip_stackshot:
12341 if (wdogTrigger) {
12342 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12343 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12344 // If current boot is due to this watch dog trigger restart in previous boot,
12345 // then don't trigger again until at least 1 successful sleep & wake.
12346 if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12347 LOG("Shutting down due to repeated Sleep/Wake failures\n");
12348 if (!tasksSuspended) {
12349 tasksSuspended = TRUE;
12350 updateTasksSuspend();
12351 }
12352 PEHaltRestart(kPEHaltCPU);
12353 return;
12354 }
12355 }
12356 if (gSwdPanic == 0) {
12357 LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12358 if (!tasksSuspended) {
12359 tasksSuspended = TRUE;
12360 updateTasksSuspend();
12361 }
12362 PEHaltRestart(kPERestartCPU);
12363 }
12364 }
12365 if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12366 DLOG("Failed to write SleepWake failure panic key\n");
12367 }
12368 #if defined(__x86_64__)
12369 if (thread) {
12370 panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12371 } else
12372 #endif /* defined(__x86_64__) */
12373 {
12374 panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12375 }
12376 } else {
12377 gRootDomain->swd_lock = 0;
12378 return;
12379 }
12380 }
12381
12382 void
sleepWakeDebugMemAlloc()12383 IOPMrootDomain::sleepWakeDebugMemAlloc()
12384 {
12385 vm_size_t size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12386
12387 swd_hdr *hdr = NULL;
12388 void *bufPtr = NULL;
12389
12390 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12391
12392
12393 if (kIOSleepWakeWdogOff & gIOKitDebug) {
12394 return;
12395 }
12396
12397 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12398 return;
12399 }
12400
12401 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12402 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12403 size);
12404 if (memDesc == NULL) {
12405 DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12406 goto exit;
12407 }
12408
12409 bufPtr = memDesc->getBytesNoCopy();
12410
12411 // Carve out memory for zlib routines
12412 swd_zs_zmem = (vm_offset_t)bufPtr;
12413 bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12414
12415 // Carve out memory for compressed stackshots
12416 swd_compressed_buffer = bufPtr;
12417 bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12418
12419 // Remaining is used for holding stackshot
12420 hdr = (swd_hdr *)bufPtr;
12421 memset(hdr, 0, sizeof(swd_hdr));
12422
12423 hdr->signature = SWD_HDR_SIGNATURE;
12424 hdr->alloc_size = SWD_STACKSHOT_SIZE;
12425
12426 hdr->spindump_offset = sizeof(swd_hdr);
12427 swd_buffer = (void *)hdr;
12428 swd_memDesc = os::move(memDesc);
12429 DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12430
12431 exit:
12432 gRootDomain->swd_lock = 0;
12433 }
12434
12435 void
sleepWakeDebugSpinDumpMemAlloc()12436 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12437 {
12438 #if UNUSED
12439 vm_size_t size = SWD_SPINDUMP_SIZE;
12440
12441 swd_hdr *hdr = NULL;
12442
12443 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12444
12445 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12446 return;
12447 }
12448
12449 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12450 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12451 SWD_SPINDUMP_SIZE);
12452
12453 if (memDesc == NULL) {
12454 DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12455 goto exit;
12456 }
12457
12458
12459 hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12460 memset(hdr, 0, sizeof(swd_hdr));
12461
12462 hdr->signature = SWD_HDR_SIGNATURE;
12463 hdr->alloc_size = size;
12464
12465 hdr->spindump_offset = sizeof(swd_hdr);
12466 swd_spindump_buffer = (void *)hdr;
12467 swd_spindump_memDesc = os::move(memDesc);
12468
12469 exit:
12470 gRootDomain->swd_lock = 0;
12471 #endif /* UNUSED */
12472 }
12473
12474 void
sleepWakeDebugEnableWdog()12475 IOPMrootDomain::sleepWakeDebugEnableWdog()
12476 {
12477 }
12478
12479 bool
sleepWakeDebugIsWdogEnabled()12480 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12481 {
12482 return !systemBooting && !systemShutdown && !gWillShutdown;
12483 }
12484
12485 void
sleepWakeDebugSaveSpinDumpFile()12486 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12487 {
12488 swd_hdr *hdr = NULL;
12489 errno_t error = EIO;
12490
12491 if (swd_spindump_buffer && gSpinDumpBufferFull) {
12492 hdr = (swd_hdr *)swd_spindump_buffer;
12493
12494 error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12495 (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12496
12497 if (error) {
12498 return;
12499 }
12500
12501 sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12502 (char*)hdr + offsetof(swd_hdr, UUID),
12503 sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12504
12505 gSpinDumpBufferFull = false;
12506 }
12507 }
12508
12509 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12510 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12511 {
12512 struct vnode *vp = NULL;
12513 vfs_context_t ctx = vfs_context_create(vfs_context_current());
12514 kauth_cred_t cred = vfs_context_ucred(ctx);
12515 struct vnode_attr va;
12516 errno_t error = EIO;
12517
12518 if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12519 S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12520 LOG("Failed to open the file %s\n", name);
12521 swd_flags |= SWD_FILEOP_ERROR;
12522 goto exit;
12523 }
12524 VATTR_INIT(&va);
12525 VATTR_WANTED(&va, va_nlink);
12526 /* Don't dump to non-regular files or files with links. */
12527 if (vp->v_type != VREG ||
12528 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12529 LOG("Bailing as this is not a regular file\n");
12530 swd_flags |= SWD_FILEOP_ERROR;
12531 goto exit;
12532 }
12533 VATTR_INIT(&va);
12534 VATTR_SET(&va, va_data_size, 0);
12535 vnode_setattr(vp, &va, ctx);
12536
12537
12538 if (buf != NULL) {
12539 error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12540 UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12541 if (error != 0) {
12542 LOG("Failed to save sleep wake log. err 0x%x\n", error);
12543 swd_flags |= SWD_FILEOP_ERROR;
12544 } else {
12545 DLOG("Saved %d bytes to file %s\n", len, name);
12546 }
12547 }
12548
12549 exit:
12550 if (vp) {
12551 vnode_close(vp, FWRITE, ctx);
12552 }
12553 if (ctx) {
12554 vfs_context_rele(ctx);
12555 }
12556
12557 return error;
12558 }
12559
12560 #else /* defined(__i386__) || defined(__x86_64__) */
12561
12562 void
sleepWakeDebugTrig(bool restart)12563 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12564 {
12565 if (restart) {
12566 if (gSwdPanic == 0) {
12567 return;
12568 }
12569 panic("Sleep/Wake hang detected");
12570 return;
12571 }
12572 }
12573
12574 void
takeStackshot(bool restart)12575 IOPMrootDomain::takeStackshot(bool restart)
12576 {
12577 #pragma unused(restart)
12578 }
12579
12580 void
deleteStackshot()12581 IOPMrootDomain::deleteStackshot()
12582 {
12583 }
12584
12585 void
sleepWakeDebugMemAlloc()12586 IOPMrootDomain::sleepWakeDebugMemAlloc()
12587 {
12588 }
12589
12590 void
saveFailureData2File()12591 IOPMrootDomain::saveFailureData2File()
12592 {
12593 }
12594
12595 void
sleepWakeDebugEnableWdog()12596 IOPMrootDomain::sleepWakeDebugEnableWdog()
12597 {
12598 }
12599
12600 bool
sleepWakeDebugIsWdogEnabled()12601 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12602 {
12603 return false;
12604 }
12605
12606 void
sleepWakeDebugSaveSpinDumpFile()12607 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12608 {
12609 }
12610
12611 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12612 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12613 {
12614 return 0;
12615 }
12616
12617 #endif /* defined(__i386__) || defined(__x86_64__) */
12618
12619