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 // Minimum time in milliseconds after AP wake that we allow idle timer to expire.
313 // We impose this minimum to avoid race conditions in the AP wake path where
314 // userspace clients are not able to acquire power assertions before the idle timer expires.
315 #define kMinimumTimeBeforeIdleSleep 1000
316
317 #define DISPLAY_WRANGLER_PRESENT (!NO_KERNEL_HID)
318
319 enum {
320 kWranglerPowerStateMin = 0,
321 kWranglerPowerStateSleep = 2,
322 kWranglerPowerStateDim = 3,
323 kWranglerPowerStateMax = 4
324 };
325
326 enum {
327 OFF_STATE = 0,
328 RESTART_STATE = 1,
329 SLEEP_STATE = 2,
330 AOT_STATE = 3,
331 ON_STATE = 4,
332 NUM_POWER_STATES
333 };
334
335 const char *
getPowerStateString(uint32_t state)336 getPowerStateString( uint32_t state )
337 {
338 #define POWER_STATE(x) {(uint32_t) x, #x}
339
340 static const IONamedValue powerStates[] = {
341 POWER_STATE( OFF_STATE ),
342 POWER_STATE( RESTART_STATE ),
343 POWER_STATE( SLEEP_STATE ),
344 POWER_STATE( AOT_STATE ),
345 POWER_STATE( ON_STATE ),
346 { 0, NULL }
347 };
348 return IOFindNameForValue(state, powerStates);
349 }
350
351 #define ON_POWER kIOPMPowerOn
352 #define RESTART_POWER kIOPMRestart
353 #define SLEEP_POWER kIOPMAuxPowerOn
354
355 static IOPMPowerState
356 ourPowerStates[NUM_POWER_STATES] =
357 {
358 { .version = 1,
359 .capabilityFlags = 0,
360 .outputPowerCharacter = 0,
361 .inputPowerRequirement = 0 },
362 { .version = 1,
363 .capabilityFlags = kIOPMRestartCapability,
364 .outputPowerCharacter = kIOPMRestart,
365 .inputPowerRequirement = RESTART_POWER },
366 { .version = 1,
367 .capabilityFlags = kIOPMSleepCapability,
368 .outputPowerCharacter = kIOPMSleep,
369 .inputPowerRequirement = SLEEP_POWER },
370 { .version = 1,
371 .capabilityFlags = kIOPMAOTCapability,
372 .outputPowerCharacter = kIOPMAOTPower,
373 .inputPowerRequirement = ON_POWER },
374 { .version = 1,
375 .capabilityFlags = kIOPMPowerOn,
376 .outputPowerCharacter = kIOPMPowerOn,
377 .inputPowerRequirement = ON_POWER },
378 };
379
380 #define kIOPMRootDomainWakeTypeSleepService "SleepService"
381 #define kIOPMRootDomainWakeTypeMaintenance "Maintenance"
382 #define kIOPMRootDomainWakeTypeSleepTimer "SleepTimer"
383 #define kIOPMrootDomainWakeTypeLowBattery "LowBattery"
384 #define kIOPMRootDomainWakeTypeUser "User"
385 #define kIOPMRootDomainWakeTypeAlarm "Alarm"
386 #define kIOPMRootDomainWakeTypeNetwork "Network"
387 #define kIOPMRootDomainWakeTypeHIDActivity "HID Activity"
388 #define kIOPMRootDomainWakeTypeNotification "Notification"
389 #define kIOPMRootDomainWakeTypeHibernateError "HibernateError"
390
391 // Special interest that entitles the interested client from receiving
392 // all system messages. Only used by powerd.
393 //
394 #define kIOPMSystemCapabilityInterest "IOPMSystemCapabilityInterest"
395
396 // Entitlement required for root domain clients
397 #define kRootDomainEntitlementSetProperty "com.apple.private.iokit.rootdomain-set-property"
398
399 #define WAKEEVENT_LOCK() IOLockLock(wakeEventLock)
400 #define WAKEEVENT_UNLOCK() IOLockUnlock(wakeEventLock)
401
402 /*
403 * Aggressiveness
404 */
405 #define AGGRESSIVES_LOCK() IOLockLock(featuresDictLock)
406 #define AGGRESSIVES_UNLOCK() IOLockUnlock(featuresDictLock)
407
408 #define kAggressivesMinValue 1
409
410 const char *
getAggressivenessTypeString(uint32_t type)411 getAggressivenessTypeString( uint32_t type )
412 {
413 #define AGGRESSIVENESS_TYPE(x) {(uint32_t) x, #x}
414
415 static const IONamedValue aggressivenessTypes[] = {
416 AGGRESSIVENESS_TYPE( kPMGeneralAggressiveness ),
417 AGGRESSIVENESS_TYPE( kPMMinutesToDim ),
418 AGGRESSIVENESS_TYPE( kPMMinutesToSpinDown ),
419 AGGRESSIVENESS_TYPE( kPMMinutesToSleep ),
420 AGGRESSIVENESS_TYPE( kPMEthernetWakeOnLANSettings ),
421 AGGRESSIVENESS_TYPE( kPMSetProcessorSpeed ),
422 AGGRESSIVENESS_TYPE( kPMPowerSource),
423 AGGRESSIVENESS_TYPE( kPMMotionSensor ),
424 AGGRESSIVENESS_TYPE( kPMLastAggressivenessType ),
425 { 0, NULL }
426 };
427 return IOFindNameForValue(type, aggressivenessTypes);
428 }
429
430 enum {
431 kAggressivesStateBusy = 0x01,
432 kAggressivesStateQuickSpindown = 0x02
433 };
434
435 struct AggressivesRecord {
436 uint32_t flags;
437 uint32_t type;
438 uint32_t value;
439 };
440
441 struct AggressivesRequest {
442 queue_chain_t chain;
443 uint32_t options;
444 uint32_t dataType;
445 union {
446 OSSharedPtr<IOService> service;
447 AggressivesRecord record;
448 } data;
449 };
450
451 enum {
452 kAggressivesRequestTypeService = 1,
453 kAggressivesRequestTypeRecord
454 };
455
456 enum {
457 kAggressivesOptionSynchronous = 0x00000001,
458 kAggressivesOptionQuickSpindownEnable = 0x00000100,
459 kAggressivesOptionQuickSpindownDisable = 0x00000200,
460 kAggressivesOptionQuickSpindownMask = 0x00000300
461 };
462
463 enum {
464 kAggressivesRecordFlagModified = 0x00000001,
465 kAggressivesRecordFlagMinValue = 0x00000002
466 };
467
468 // System Sleep Preventers
469
470 enum {
471 kPMUserDisabledAllSleep = 1,
472 kPMSystemRestartBootingInProgress,
473 kPMConfigPreventSystemSleep,
474 kPMChildPreventSystemSleep,
475 kPMCPUAssertion,
476 kPMPCIUnsupported,
477 };
478
479 const char *
getSystemSleepPreventerString(uint32_t preventer)480 getSystemSleepPreventerString( uint32_t preventer )
481 {
482 #define SYSTEM_SLEEP_PREVENTER(x) {(int) x, #x}
483 static const IONamedValue systemSleepPreventers[] = {
484 SYSTEM_SLEEP_PREVENTER( kPMUserDisabledAllSleep ),
485 SYSTEM_SLEEP_PREVENTER( kPMSystemRestartBootingInProgress ),
486 SYSTEM_SLEEP_PREVENTER( kPMConfigPreventSystemSleep ),
487 SYSTEM_SLEEP_PREVENTER( kPMChildPreventSystemSleep ),
488 SYSTEM_SLEEP_PREVENTER( kPMCPUAssertion ),
489 SYSTEM_SLEEP_PREVENTER( kPMPCIUnsupported ),
490 { 0, NULL }
491 };
492 return IOFindNameForValue(preventer, systemSleepPreventers);
493 }
494
495 // gDarkWakeFlags
496 enum {
497 kDarkWakeFlagPromotionNone = 0x0000,
498 kDarkWakeFlagPromotionEarly = 0x0001, // promote before gfx clamp
499 kDarkWakeFlagPromotionLate = 0x0002, // promote after gfx clamp
500 kDarkWakeFlagPromotionMask = 0x0003,
501 kDarkWakeFlagAlarmIsDark = 0x0100,
502 kDarkWakeFlagAudioNotSuppressed = 0x0200,
503 kDarkWakeFlagUserWakeWorkaround = 0x1000
504 };
505
506 // gClamshellFlags
507 // The workaround for 9157444 is enabled at compile time using the
508 // DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY macro and is not represented below.
509 enum {
510 kClamshell_WAR_38378787 = 0x00000001,
511 kClamshell_WAR_47715679 = 0x00000002,
512 kClamshell_WAR_58009435 = 0x00000004
513 };
514
515 // acceptSystemWakeEvents()
516 enum {
517 kAcceptSystemWakeEvents_Disable = 0,
518 kAcceptSystemWakeEvents_Enable,
519 kAcceptSystemWakeEvents_Reenable
520 };
521
522 static IOPMrootDomain * gRootDomain;
523 static IORootParent * gPatriarch;
524 static IONotifier * gSysPowerDownNotifier = NULL;
525 static UInt32 gSleepOrShutdownPending = 0;
526 static UInt32 gWillShutdown = 0;
527 static UInt32 gPagingOff = 0;
528 static UInt32 gSleepWakeUUIDIsSet = false;
529 static uint32_t gAggressivesState = 0;
530 uint32_t gHaltTimeMaxLog;
531 uint32_t gHaltTimeMaxPanic;
532 IOLock * gHaltLogLock;
533 static char * gHaltLog;
534 enum { kHaltLogSize = 2048 };
535 static size_t gHaltLogPos;
536 static uint64_t gHaltStartTime;
537 static char gKextNameBuf[64];
538 static size_t gKextNamePos;
539 static bool gKextNameEnd;
540
541 uuid_string_t bootsessionuuid_string;
542
543 #if defined(XNU_TARGET_OS_OSX)
544 #if DISPLAY_WRANGLER_PRESENT
545 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionNone;
546 #elif defined(__arm64__)
547 // Enable temporary full wake promotion workarounds
548 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
549 #else
550 // Enable full wake promotion workarounds
551 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
552 #endif
553 #else /* !defined(XNU_TARGET_OS_OSX) */
554 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionEarly;
555 #endif /* !defined(XNU_TARGET_OS_OSX) */
556
557 static uint32_t gNoIdleFlag = 0;
558 static uint32_t gSleepDisabledFlag = 0;
559 static uint32_t gSwdPanic = 1;
560 static uint32_t gSwdSleepTimeout = 0;
561 static uint32_t gSwdWakeTimeout = 0;
562 static uint32_t gSwdSleepWakeTimeout = 0;
563 static PMStatsStruct gPMStats;
564 #if DEVELOPMENT || DEBUG
565 static uint32_t swd_panic_phase;
566 #endif
567
568 static uint32_t gClamshellFlags = 0
569 #if defined(__i386__) || defined(__x86_64__)
570 | kClamshell_WAR_58009435
571 #endif
572 ;
573
574 #if HIBERNATION
575
576 #if defined(__arm64__)
577 static IOReturn
defaultSleepPolicyHandler(void * ctx,const IOPMSystemSleepPolicyVariables * vars,IOPMSystemSleepParameters * params)578 defaultSleepPolicyHandler(void *ctx, const IOPMSystemSleepPolicyVariables *vars, IOPMSystemSleepParameters *params)
579 {
580 uint32_t sleepType = kIOPMSleepTypeDeepIdle;
581
582 assert(vars->signature == kIOPMSystemSleepPolicySignature);
583 assert(vars->version == kIOPMSystemSleepPolicyVersion);
584
585 // Hibernation enabled and either user forced hibernate or low battery sleep
586 if ((vars->hibernateMode & kIOHibernateModeOn) &&
587 (((vars->hibernateMode & kIOHibernateModeSleep) == 0) ||
588 (vars->sleepFactors & kIOPMSleepFactorBatteryLow))) {
589 sleepType = kIOPMSleepTypeHibernate;
590 }
591 params->version = kIOPMSystemSleepParametersVersion;
592 params->sleepType = sleepType;
593 return kIOReturnSuccess;
594 }
595 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = &defaultSleepPolicyHandler;
596 #else /* defined(__arm64__) */
597 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = NULL;
598 #endif /* defined(__arm64__) */
599
600 static IOPMSystemSleepPolicyVariables * gSleepPolicyVars = NULL;
601 static void * gSleepPolicyTarget;
602 #endif
603
604 struct timeval gIOLastSleepTime;
605 struct timeval gIOLastWakeTime;
606 AbsoluteTime gIOLastWakeAbsTime;
607 AbsoluteTime gIOLastSleepAbsTime;
608
609 struct timeval gIOLastUserSleepTime;
610
611 static char gWakeReasonString[128];
612 static char gBootReasonString[80];
613 static char gShutdownReasonString[80];
614 static bool gWakeReasonSysctlRegistered = false;
615 static bool gBootReasonSysctlRegistered = false;
616 static bool gShutdownReasonSysctlRegistered = false;
617 static bool gWillShutdownSysctlRegistered = false;
618 static AbsoluteTime gUserActiveAbsTime;
619 static AbsoluteTime gUserInactiveAbsTime;
620
621 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
622 static bool gSpinDumpBufferFull = false;
623 #endif
624
625 z_stream swd_zs;
626 vm_offset_t swd_zs_zmem;
627 //size_t swd_zs_zsize;
628 size_t swd_zs_zoffset;
629 #if defined(__i386__) || defined(__x86_64__)
630 IOCPU *currentShutdownTarget = NULL;
631 #endif
632
633 static unsigned int gPMHaltBusyCount;
634 static unsigned int gPMHaltIdleCount;
635 static int gPMHaltDepth;
636 static uint32_t gPMHaltMessageType;
637 static IOLock * gPMHaltLock = NULL;
638 static OSSharedPtr<OSArray> gPMHaltArray;
639 static OSSharedPtr<const OSSymbol> gPMHaltClientAcknowledgeKey;
640 static bool gPMQuiesced;
641
642 // Constants used as arguments to IOPMrootDomain::informCPUStateChange
643 #define kCPUUnknownIndex 9999999
644 enum {
645 kInformAC = 0,
646 kInformLid = 1,
647 kInformableCount = 2
648 };
649
650 OSSharedPtr<const OSSymbol> gIOPMStatsResponseTimedOut;
651 OSSharedPtr<const OSSymbol> gIOPMStatsResponseCancel;
652 OSSharedPtr<const OSSymbol> gIOPMStatsResponseSlow;
653 OSSharedPtr<const OSSymbol> gIOPMStatsResponsePrompt;
654 OSSharedPtr<const OSSymbol> gIOPMStatsDriverPSChangeSlow;
655
656 #define kBadPMFeatureID 0
657
658 /*
659 * PMSettingHandle
660 * Opaque handle passed to clients of registerPMSettingController()
661 */
662 class PMSettingHandle : public OSObject
663 {
664 OSDeclareFinalStructors( PMSettingHandle );
665 friend class PMSettingObject;
666
667 private:
668 PMSettingObject *pmso;
669 void free(void) APPLE_KEXT_OVERRIDE;
670 };
671
672 /*
673 * PMSettingObject
674 * Internal object to track each PM setting controller
675 */
676 class PMSettingObject : public OSObject
677 {
678 OSDeclareFinalStructors( PMSettingObject );
679 friend class IOPMrootDomain;
680
681 private:
682 queue_head_t calloutQueue;
683 thread_t waitThread;
684 IOPMrootDomain *parent;
685 PMSettingHandle *pmsh;
686 IOPMSettingControllerCallback func;
687 OSObject *target;
688 uintptr_t refcon;
689 OSDataAllocation<uint32_t> publishedFeatureID;
690 uint32_t settingCount;
691 bool disabled;
692
693 void free(void) APPLE_KEXT_OVERRIDE;
694
695 public:
696 static PMSettingObject *pmSettingObject(
697 IOPMrootDomain *parent_arg,
698 IOPMSettingControllerCallback handler_arg,
699 OSObject *target_arg,
700 uintptr_t refcon_arg,
701 uint32_t supportedPowerSources,
702 const OSSymbol *settings[],
703 OSObject **handle_obj);
704
705 IOReturn dispatchPMSetting(const OSSymbol *type, OSObject *object);
706 void clientHandleFreed(void);
707 };
708
709 struct PMSettingCallEntry {
710 queue_chain_t link;
711 thread_t thread;
712 };
713
714 #define PMSETTING_LOCK() IOLockLock(settingsCtrlLock)
715 #define PMSETTING_UNLOCK() IOLockUnlock(settingsCtrlLock)
716 #define PMSETTING_WAIT(p) IOLockSleep(settingsCtrlLock, p, THREAD_UNINT)
717 #define PMSETTING_WAKEUP(p) IOLockWakeup(settingsCtrlLock, p, true)
718
719 /*
720 * PMTraceWorker
721 * Internal helper object for logging trace points to RTC
722 * IOPMrootDomain and only IOPMrootDomain should instantiate
723 * exactly one of these.
724 */
725
726 typedef void (*IOPMTracePointHandler)(
727 void * target, uint32_t code, uint32_t data );
728
729 class PMTraceWorker : public OSObject
730 {
731 OSDeclareDefaultStructors(PMTraceWorker);
732 public:
733 typedef enum { kPowerChangeStart, kPowerChangeCompleted } change_t;
734
735 static OSPtr<PMTraceWorker> tracer( IOPMrootDomain * );
736 void tracePCIPowerChange(change_t, IOService *, uint32_t, uint32_t);
737 void tracePoint(uint8_t phase);
738 void traceDetail(uint32_t detail);
739 void traceComponentWakeProgress(uint32_t component, uint32_t data);
740 int recordTopLevelPCIDevice(IOService *);
741 void RTC_TRACE(void);
742 virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
743
744 IOPMTracePointHandler tracePointHandler;
745 void * tracePointTarget;
746 uint64_t getPMStatusCode();
747 uint8_t getTracePhase();
748 uint32_t getTraceData();
749 private:
750 IOPMrootDomain *owner;
751 IOLock *pmTraceWorkerLock;
752 OSSharedPtr<OSArray> pciDeviceBitMappings;
753
754 uint8_t addedToRegistry;
755 uint8_t tracePhase;
756 uint32_t traceData32;
757 uint8_t loginWindowData;
758 uint8_t coreDisplayData;
759 uint8_t coreGraphicsData;
760 };
761
762 /*
763 * this should be treated as POD, as it's byte-copied around
764 * and we cannot rely on d'tor firing at the right time
765 */
766 struct PMAssertStruct {
767 IOPMDriverAssertionID id;
768 IOPMDriverAssertionType assertionBits;
769 uint64_t createdTime;
770 uint64_t modifiedTime;
771 const OSSymbol *ownerString;
772 IOService *ownerService;
773 uint64_t registryEntryID;
774 IOPMDriverAssertionLevel level;
775 uint64_t assertCPUStartTime;
776 uint64_t assertCPUDuration;
777 };
778 OSDefineValueObjectForDependentType(PMAssertStruct)
779
780 /*
781 * PMAssertionsTracker
782 * Tracks kernel and user space PM assertions
783 */
784 class PMAssertionsTracker : public OSObject
785 {
786 OSDeclareFinalStructors(PMAssertionsTracker);
787 public:
788 static PMAssertionsTracker *pmAssertionsTracker( IOPMrootDomain * );
789
790 IOReturn createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
791 IOReturn releaseAssertion(IOPMDriverAssertionID);
792 IOReturn setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
793 IOReturn setUserAssertionLevels(IOPMDriverAssertionType);
794
795 OSSharedPtr<OSArray> copyAssertionsArray(void);
796 IOPMDriverAssertionType getActivatedAssertions(void);
797 IOPMDriverAssertionLevel getAssertionLevel(IOPMDriverAssertionType);
798
799 IOReturn handleCreateAssertion(OSValueObject<PMAssertStruct> *);
800 IOReturn handleReleaseAssertion(IOPMDriverAssertionID);
801 IOReturn handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
802 IOReturn handleSetUserAssertionLevels(void * arg0);
803 void publishProperties(void);
804 void reportCPUBitAccounting(void);
805
806 private:
807 uint32_t tabulateProducerCount;
808 uint32_t tabulateConsumerCount;
809
810 uint64_t maxAssertCPUDuration;
811 uint64_t maxAssertCPUEntryId;
812
813 PMAssertStruct *detailsForID(IOPMDriverAssertionID, int *);
814 void tabulate(void);
815 void updateCPUBitAccounting(PMAssertStruct * assertStruct);
816
817 IOPMrootDomain *owner;
818 OSSharedPtr<OSArray> assertionsArray;
819 IOLock *assertionsArrayLock;
820 IOPMDriverAssertionID issuingUniqueID __attribute__((aligned(8)));/* aligned for atomic access */
821 IOPMDriverAssertionType assertionsKernel;
822 IOPMDriverAssertionType assertionsUser;
823 IOPMDriverAssertionType assertionsCombined;
824 };
825
826 OSDefineMetaClassAndFinalStructors(PMAssertionsTracker, OSObject);
827
828 /*
829 * PMHaltWorker
830 * Internal helper object for Shutdown/Restart notifications.
831 */
832 #define kPMHaltMaxWorkers 8
833 #define kPMHaltTimeoutMS 100
834
835 class PMHaltWorker : public OSObject
836 {
837 OSDeclareFinalStructors( PMHaltWorker );
838
839 public:
840 IOService * service;// service being worked on
841 AbsoluteTime startTime; // time when work started
842 int depth; // work on nubs at this PM-tree depth
843 int visits; // number of nodes visited (debug)
844 IOLock * lock;
845 bool timeout;// service took too long
846
847 static PMHaltWorker * worker( void );
848 static void main( void * arg, wait_result_t waitResult );
849 static void work( PMHaltWorker * me );
850 static void checkTimeout( PMHaltWorker * me, AbsoluteTime * now );
851 virtual void free( void ) APPLE_KEXT_OVERRIDE;
852 };
853
OSDefineMetaClassAndFinalStructors(PMHaltWorker,OSObject)854 OSDefineMetaClassAndFinalStructors( PMHaltWorker, OSObject )
855
856
857 #define super IOService
858 OSDefineMetaClassAndFinalStructors(IOPMrootDomain, IOService)
859
860 boolean_t
861 IOPMRootDomainGetWillShutdown(void)
862 {
863 return gWillShutdown != 0;
864 }
865
866 static void
IOPMRootDomainWillShutdown(void)867 IOPMRootDomainWillShutdown(void)
868 {
869 if (OSCompareAndSwap(0, 1, &gWillShutdown)) {
870 IOService::willShutdown();
871 for (int i = 0; i < 100; i++) {
872 if (OSCompareAndSwap(0, 1, &gSleepOrShutdownPending)) {
873 break;
874 }
875 IOSleep( 100 );
876 }
877 }
878 }
879
880 extern "C" IONotifier *
registerSleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)881 registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
882 {
883 return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref ).detach();
884 }
885
886 extern "C" IONotifier *
registerPrioritySleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)887 registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
888 {
889 return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref ).detach();
890 }
891
892 extern "C" IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon)893 acknowledgeSleepWakeNotification(void * PMrefcon)
894 {
895 return gRootDomain->allowPowerChange((unsigned long)PMrefcon );
896 }
897
898 extern "C" IOReturn
vetoSleepWakeNotification(void * PMrefcon)899 vetoSleepWakeNotification(void * PMrefcon)
900 {
901 return gRootDomain->cancelPowerChange((unsigned long)PMrefcon );
902 }
903
904 extern "C" IOReturn
rootDomainRestart(void)905 rootDomainRestart( void )
906 {
907 return gRootDomain->restartSystem();
908 }
909
910 extern "C" IOReturn
rootDomainShutdown(void)911 rootDomainShutdown( void )
912 {
913 return gRootDomain->shutdownSystem();
914 }
915
916 static void
halt_log_putc(char c)917 halt_log_putc(char c)
918 {
919 if (gHaltLogPos >= (kHaltLogSize - 2)) {
920 return;
921 }
922 gHaltLog[gHaltLogPos++] = c;
923 }
924
925 extern "C" void
926 _doprnt_log(const char *fmt,
927 va_list *argp,
928 void (*putc)(char),
929 int radix);
930
931 static int
halt_log(const char * fmt,...)932 halt_log(const char *fmt, ...)
933 {
934 va_list listp;
935
936 va_start(listp, fmt);
937 _doprnt_log(fmt, &listp, &halt_log_putc, 16);
938 va_end(listp);
939
940 return 0;
941 }
942
943 extern "C" void
halt_log_enter(const char * what,const void * pc,uint64_t time)944 halt_log_enter(const char * what, const void * pc, uint64_t time)
945 {
946 uint64_t nano, millis;
947
948 if (!gHaltLog) {
949 return;
950 }
951 absolutetime_to_nanoseconds(time, &nano);
952 millis = nano / NSEC_PER_MSEC;
953 if (millis < 100) {
954 return;
955 }
956
957 IOLockLock(gHaltLogLock);
958 if (pc) {
959 halt_log("%s: %qd ms @ 0x%lx, ", what, millis, VM_KERNEL_UNSLIDE(pc));
960 OSKext::printKextsInBacktrace((vm_offset_t *) &pc, 1, &halt_log,
961 OSKext::kPrintKextsLock | OSKext::kPrintKextsUnslide | OSKext::kPrintKextsTerse);
962 } else {
963 halt_log("%s: %qd ms\n", what, millis);
964 }
965
966 gHaltLog[gHaltLogPos] = 0;
967 IOLockUnlock(gHaltLogLock);
968 }
969
970 extern uint32_t gFSState;
971
972 extern "C" void
IOSystemShutdownNotification(int howto,int stage)973 IOSystemShutdownNotification(int howto, int stage)
974 {
975 uint64_t startTime;
976
977 if (kIOSystemShutdownNotificationStageRootUnmount == stage) {
978 #if defined(XNU_TARGET_OS_OSX)
979 uint64_t nano, millis;
980 startTime = mach_absolute_time();
981 IOService::getPlatform()->waitQuiet(30 * NSEC_PER_SEC);
982 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
983 millis = nano / NSEC_PER_MSEC;
984 if (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog)) {
985 printf("waitQuiet() for unmount %qd ms\n", millis);
986 }
987 #endif /* defined(XNU_TARGET_OS_OSX) */
988 return;
989 }
990
991 if (kIOSystemShutdownNotificationTerminateDEXTs == stage) {
992 uint64_t nano, millis;
993 startTime = mach_absolute_time();
994 IOServicePH::systemHalt(howto);
995 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
996 millis = nano / NSEC_PER_MSEC;
997 if (true || (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog))) {
998 printf("IOServicePH::systemHalt took %qd ms\n", millis);
999 }
1000 return;
1001 }
1002
1003 assert(kIOSystemShutdownNotificationStageProcessExit == stage);
1004
1005 IOLockLock(gHaltLogLock);
1006 if (!gHaltLog) {
1007 gHaltLog = IONewData(char, (vm_size_t)kHaltLogSize);
1008 gHaltStartTime = mach_absolute_time();
1009 if (gHaltLog) {
1010 halt_log_putc('\n');
1011 }
1012 }
1013 IOLockUnlock(gHaltLogLock);
1014
1015 startTime = mach_absolute_time();
1016 IOPMRootDomainWillShutdown();
1017 halt_log_enter("IOPMRootDomainWillShutdown", NULL, mach_absolute_time() - startTime);
1018 #if HIBERNATION
1019 startTime = mach_absolute_time();
1020 IOHibernateSystemPostWake(true);
1021 halt_log_enter("IOHibernateSystemPostWake", NULL, mach_absolute_time() - startTime);
1022 #endif
1023 if (OSCompareAndSwap(0, 1, &gPagingOff)) {
1024 gRootDomain->handlePlatformHaltRestart(kPEPagingOff);
1025 }
1026 }
1027
1028 extern "C" int sync_internal(void);
1029
1030 /*
1031 * A device is always in the highest power state which satisfies its driver,
1032 * its policy-maker, and any power children it has, but within the constraint
1033 * of the power state provided by its parent. The driver expresses its desire by
1034 * calling changePowerStateTo(), the policy-maker expresses its desire by calling
1035 * changePowerStateToPriv(), and the children express their desires by calling
1036 * requestPowerDomainState().
1037 *
1038 * The Root Power Domain owns the policy for idle and demand sleep for the system.
1039 * It is a power-managed IOService just like the others in the system.
1040 * It implements several power states which map to what we see as Sleep and On.
1041 *
1042 * The sleep policy is as follows:
1043 * 1. Sleep is prevented if the case is open so that nobody will think the machine
1044 * is off and plug/unplug cards.
1045 * 2. Sleep is prevented if the sleep timeout slider in the prefs panel is zero.
1046 * 3. System cannot Sleep if some object in the tree is in a power state marked
1047 * kIOPMPreventSystemSleep.
1048 *
1049 * These three conditions are enforced using the "driver clamp" by calling
1050 * changePowerStateTo(). For example, if the case is opened,
1051 * changePowerStateTo(ON_STATE) is called to hold the system on regardless
1052 * of the desires of the children of the root or the state of the other clamp.
1053 *
1054 * Demand Sleep is initiated by pressing the front panel power button, closing
1055 * the clamshell, or selecting the menu item. In this case the root's parent
1056 * actually initiates the power state change so that the root domain has no
1057 * choice and does not give applications the opportunity to veto the change.
1058 *
1059 * Idle Sleep occurs if no objects in the tree are in a state marked
1060 * kIOPMPreventIdleSleep. When this is true, the root's children are not holding
1061 * the root on, so it sets the "policy-maker clamp" by calling
1062 * changePowerStateToPriv(ON_STATE) to hold itself on until the sleep timer expires.
1063 * This timer is set for the difference between the sleep timeout slider and the
1064 * display dim timeout slider. When the timer expires, it releases its clamp and
1065 * now nothing is holding it awake, so it falls asleep.
1066 *
1067 * Demand sleep is prevented when the system is booting. When preferences are
1068 * transmitted by the loginwindow at the end of boot, a flag is cleared,
1069 * and this allows subsequent Demand Sleep.
1070 */
1071
1072 //******************************************************************************
1073
1074 IOPMrootDomain *
construct(void)1075 IOPMrootDomain::construct( void )
1076 {
1077 IOPMrootDomain *root;
1078
1079 root = new IOPMrootDomain;
1080 if (root) {
1081 root->init();
1082 }
1083
1084 return root;
1085 }
1086
1087 //******************************************************************************
1088 // updateConsoleUsersCallout
1089 //
1090 //******************************************************************************
1091
1092 static void
updateConsoleUsersCallout(thread_call_param_t p0,thread_call_param_t p1)1093 updateConsoleUsersCallout(thread_call_param_t p0, thread_call_param_t p1)
1094 {
1095 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1096 rootDomain->updateConsoleUsers();
1097 }
1098
1099 void
updateConsoleUsers(void)1100 IOPMrootDomain::updateConsoleUsers(void)
1101 {
1102 IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
1103 updateTasksSuspend(kTasksSuspendUnsuspended, kTasksSuspendNoChange);
1104 }
1105
1106 bool
updateTasksSuspend(int newTasksSuspended,int newAOTTasksSuspended)1107 IOPMrootDomain::updateTasksSuspend(int newTasksSuspended, int newAOTTasksSuspended)
1108 {
1109 bool newSuspend;
1110
1111 WAKEEVENT_LOCK();
1112 if (newTasksSuspended != kTasksSuspendNoChange) {
1113 tasksSuspended = (newTasksSuspended != kTasksSuspendUnsuspended);
1114 }
1115 if (newAOTTasksSuspended != kTasksSuspendNoChange) {
1116 _aotTasksSuspended = (newAOTTasksSuspended != kTasksSuspendUnsuspended);
1117 }
1118 newSuspend = (tasksSuspended || _aotTasksSuspended);
1119 if (newSuspend == tasksSuspendState) {
1120 WAKEEVENT_UNLOCK();
1121 return false;
1122 }
1123 tasksSuspendState = newSuspend;
1124 WAKEEVENT_UNLOCK();
1125 tasks_system_suspend(newSuspend);
1126 return true;
1127 }
1128
1129 //******************************************************************************
1130
1131 static void
disk_sync_callout(thread_call_param_t p0,thread_call_param_t p1)1132 disk_sync_callout( thread_call_param_t p0, thread_call_param_t p1 )
1133 {
1134 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1135 uint32_t notifyRef = (uint32_t)(uintptr_t) p1;
1136 uint32_t powerState = rootDomain->getPowerState();
1137
1138 DLOG("disk_sync_callout ps=%u\n", powerState);
1139
1140 if (ON_STATE == powerState) {
1141 sync_internal();
1142
1143 #if HIBERNATION
1144 // Block sleep until trim issued on previous wake path is completed.
1145 IOHibernateSystemPostWake(true);
1146 #endif
1147 }
1148 #if HIBERNATION
1149 else {
1150 IOHibernateSystemPostWake(false);
1151
1152 rootDomain->sleepWakeDebugSaveSpinDumpFile();
1153 }
1154 #endif
1155
1156 rootDomain->allowPowerChange(notifyRef);
1157 DLOG("disk_sync_callout finish\n");
1158 }
1159
1160 //******************************************************************************
1161 static UInt32
computeDeltaTimeMS(const AbsoluteTime * startTime,AbsoluteTime * elapsedTime)1162 computeDeltaTimeMS( const AbsoluteTime * startTime, AbsoluteTime * elapsedTime )
1163 {
1164 AbsoluteTime endTime;
1165 UInt64 nano = 0;
1166
1167 clock_get_uptime(&endTime);
1168 if (CMP_ABSOLUTETIME(&endTime, startTime) <= 0) {
1169 *elapsedTime = 0;
1170 } else {
1171 SUB_ABSOLUTETIME(&endTime, startTime);
1172 absolutetime_to_nanoseconds(endTime, &nano);
1173 *elapsedTime = endTime;
1174 }
1175
1176 return (UInt32)(nano / NSEC_PER_MSEC);
1177 }
1178
1179 //******************************************************************************
1180
1181 static int
1182 sysctl_sleepwaketime SYSCTL_HANDLER_ARGS
1183 {
1184 struct timeval *swt = (struct timeval *)arg1;
1185 struct proc *p = req->p;
1186
1187 if (p == kernproc) {
1188 return sysctl_io_opaque(req, swt, sizeof(*swt), NULL);
1189 } else if (proc_is64bit(p)) {
1190 struct user64_timeval t = {};
1191 t.tv_sec = swt->tv_sec;
1192 t.tv_usec = swt->tv_usec;
1193 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1194 } else {
1195 struct user32_timeval t = {};
1196 t.tv_sec = (typeof(t.tv_sec))swt->tv_sec;
1197 t.tv_usec = swt->tv_usec;
1198 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1199 }
1200 }
1201
1202 static SYSCTL_PROC(_kern, OID_AUTO, sleeptime,
1203 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1204 &gIOLastUserSleepTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1205
1206 static SYSCTL_PROC(_kern, OID_AUTO, waketime,
1207 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1208 &gIOLastWakeTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1209
1210 SYSCTL_QUAD(_kern, OID_AUTO, wake_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastWakeAbsTime, "");
1211 SYSCTL_QUAD(_kern, OID_AUTO, sleep_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastSleepAbsTime, "");
1212 SYSCTL_QUAD(_kern, OID_AUTO, useractive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserActiveAbsTime, "");
1213 SYSCTL_QUAD(_kern, OID_AUTO, userinactive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserInactiveAbsTime, "");
1214
1215 static int
1216 sysctl_willshutdown SYSCTL_HANDLER_ARGS
1217 {
1218 int new_value, changed, error;
1219
1220 if (!gWillShutdownSysctlRegistered) {
1221 return ENOENT;
1222 }
1223
1224 error = sysctl_io_number(req, gWillShutdown, sizeof(int), &new_value, &changed);
1225 if (changed) {
1226 if (!gWillShutdown && (new_value == 1)) {
1227 IOPMRootDomainWillShutdown();
1228 } else {
1229 error = EINVAL;
1230 }
1231 }
1232 return error;
1233 }
1234
1235 static SYSCTL_PROC(_kern, OID_AUTO, willshutdown,
1236 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1237 NULL, 0, sysctl_willshutdown, "I", "");
1238
1239 #if defined(XNU_TARGET_OS_OSX)
1240
1241 static int
sysctl_progressmeterenable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1242 sysctl_progressmeterenable
1243 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1244 {
1245 int error;
1246 int new_value, changed;
1247
1248 error = sysctl_io_number(req, vc_progressmeter_enable, sizeof(int), &new_value, &changed);
1249
1250 if (changed) {
1251 vc_enable_progressmeter(new_value);
1252 }
1253
1254 return error;
1255 }
1256
1257 static int
sysctl_progressmeter(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1258 sysctl_progressmeter
1259 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1260 {
1261 int error;
1262 int new_value, changed;
1263
1264 error = sysctl_io_number(req, vc_progressmeter_value, sizeof(int), &new_value, &changed);
1265
1266 if (changed) {
1267 vc_set_progressmeter(new_value);
1268 }
1269
1270 return error;
1271 }
1272
1273 static SYSCTL_PROC(_kern, OID_AUTO, progressmeterenable,
1274 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1275 NULL, 0, sysctl_progressmeterenable, "I", "");
1276
1277 static SYSCTL_PROC(_kern, OID_AUTO, progressmeter,
1278 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1279 NULL, 0, sysctl_progressmeter, "I", "");
1280
1281 #endif /* defined(XNU_TARGET_OS_OSX) */
1282
1283
1284
1285 static int
sysctl_consoleoptions(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1286 sysctl_consoleoptions
1287 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1288 {
1289 int error, changed;
1290 uint32_t new_value;
1291
1292 error = sysctl_io_number(req, vc_user_options.options, sizeof(uint32_t), &new_value, &changed);
1293
1294 if (changed) {
1295 vc_user_options.options = new_value;
1296 }
1297
1298 return error;
1299 }
1300
1301 static SYSCTL_PROC(_kern, OID_AUTO, consoleoptions,
1302 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1303 NULL, 0, sysctl_consoleoptions, "I", "");
1304
1305
1306 static int
1307 sysctl_progressoptions SYSCTL_HANDLER_ARGS
1308 {
1309 return sysctl_io_opaque(req, &vc_user_options, sizeof(vc_user_options), NULL);
1310 }
1311
1312 static SYSCTL_PROC(_kern, OID_AUTO, progressoptions,
1313 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1314 NULL, 0, sysctl_progressoptions, "S,vc_progress_user_options", "");
1315
1316
1317 static int
1318 sysctl_wakereason SYSCTL_HANDLER_ARGS
1319 {
1320 char wr[sizeof(gWakeReasonString)];
1321
1322 wr[0] = '\0';
1323 if (gRootDomain && gWakeReasonSysctlRegistered) {
1324 gRootDomain->copyWakeReasonString(wr, sizeof(wr));
1325 } else {
1326 return ENOENT;
1327 }
1328
1329 return sysctl_io_string(req, wr, 0, 0, NULL);
1330 }
1331
1332 SYSCTL_PROC(_kern, OID_AUTO, wakereason,
1333 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1334 NULL, 0, sysctl_wakereason, "A", "wakereason");
1335
1336 static int
1337 sysctl_bootreason SYSCTL_HANDLER_ARGS
1338 {
1339 if (!os_atomic_load(&gBootReasonSysctlRegistered, acquire)) {
1340 return ENOENT;
1341 }
1342
1343 return sysctl_io_string(req, gBootReasonString, 0, 0, NULL);
1344 }
1345
1346 SYSCTL_PROC(_kern, OID_AUTO, bootreason,
1347 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1348 NULL, 0, sysctl_bootreason, "A", "");
1349
1350 static int
1351 sysctl_shutdownreason SYSCTL_HANDLER_ARGS
1352 {
1353 char sr[sizeof(gShutdownReasonString)];
1354
1355 sr[0] = '\0';
1356 if (gRootDomain && gShutdownReasonSysctlRegistered) {
1357 gRootDomain->copyShutdownReasonString(sr, sizeof(sr));
1358 } else {
1359 return ENOENT;
1360 }
1361
1362 return sysctl_io_string(req, sr, 0, 0, NULL);
1363 }
1364
1365 SYSCTL_PROC(_kern, OID_AUTO, shutdownreason,
1366 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1367 NULL, 0, sysctl_shutdownreason, "A", "shutdownreason");
1368
1369 static int
1370 sysctl_targettype SYSCTL_HANDLER_ARGS
1371 {
1372 IOService * root;
1373 OSSharedPtr<OSObject> obj;
1374 OSData * data;
1375 char tt[32];
1376
1377 tt[0] = '\0';
1378 root = IOService::getServiceRoot();
1379 if (root && (obj = root->copyProperty(gIODTTargetTypeKey))) {
1380 if ((data = OSDynamicCast(OSData, obj.get()))) {
1381 strlcpy(tt, (const char *) data->getBytesNoCopy(), sizeof(tt));
1382 }
1383 }
1384 return sysctl_io_string(req, tt, 0, 0, NULL);
1385 }
1386
1387 SYSCTL_PROC(_hw, OID_AUTO, targettype,
1388 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1389 NULL, 0, sysctl_targettype, "A", "targettype");
1390
1391 static SYSCTL_INT(_debug, OID_AUTO, noidle, CTLFLAG_RW, &gNoIdleFlag, 0, "");
1392 static SYSCTL_INT(_debug, OID_AUTO, swd_sleep_timeout, CTLFLAG_RW, &gSwdSleepTimeout, 0, "");
1393 static SYSCTL_INT(_debug, OID_AUTO, swd_wake_timeout, CTLFLAG_RW, &gSwdWakeTimeout, 0, "");
1394 static SYSCTL_INT(_debug, OID_AUTO, swd_timeout, CTLFLAG_RW, &gSwdSleepWakeTimeout, 0, "");
1395 static SYSCTL_INT(_debug, OID_AUTO, swd_panic, CTLFLAG_RW, &gSwdPanic, 0, "");
1396 #if DEVELOPMENT || DEBUG
1397 static SYSCTL_INT(_debug, OID_AUTO, swd_panic_phase, CTLFLAG_RW, &swd_panic_phase, 0, "");
1398 #if defined(XNU_TARGET_OS_OSX)
1399 static SYSCTL_INT(_debug, OID_AUTO, clamshell, CTLFLAG_RW, &gClamshellFlags, 0, "");
1400 static SYSCTL_INT(_debug, OID_AUTO, darkwake, CTLFLAG_RW, &gDarkWakeFlags, 0, "");
1401 #endif /* defined(XNU_TARGET_OS_OSX) */
1402 #endif /* DEVELOPMENT || DEBUG */
1403
1404 //******************************************************************************
1405 // AOT
1406
1407 static int
1408 sysctl_aotmetrics SYSCTL_HANDLER_ARGS
1409 {
1410 if (NULL == gRootDomain) {
1411 return ENOENT;
1412 }
1413 if (NULL == gRootDomain->_aotMetrics) {
1414 IOPMAOTMetrics nullMetrics = {};
1415 return sysctl_io_opaque(req, &nullMetrics, sizeof(IOPMAOTMetrics), NULL);
1416 }
1417 return sysctl_io_opaque(req, gRootDomain->_aotMetrics, sizeof(IOPMAOTMetrics), NULL);
1418 }
1419
1420 static SYSCTL_PROC(_kern, OID_AUTO, aotmetrics,
1421 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1422 NULL, 0, sysctl_aotmetrics, "S,IOPMAOTMetrics", "");
1423
1424
1425 static int
update_aotmode(uint32_t mode)1426 update_aotmode(uint32_t mode)
1427 {
1428 int result;
1429
1430 if (!gIOPMWorkLoop) {
1431 return ENOENT;
1432 }
1433 result = gIOPMWorkLoop->runActionBlock(^IOReturn (void) {
1434 unsigned int oldCount;
1435
1436 if (mode && !gRootDomain->_aotMetrics) {
1437 gRootDomain->_aotMetrics = IOMallocType(IOPMAOTMetrics);
1438 }
1439
1440 oldCount = gRootDomain->idleSleepPreventersCount();
1441 gRootDomain->_aotMode = (mode & kIOPMAOTModeMask);
1442 gRootDomain->updatePreventIdleSleepListInternal(NULL, false, oldCount);
1443 return 0;
1444 });
1445 return result;
1446 }
1447
1448 static int
sysctl_aotmodebits(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1449 sysctl_aotmodebits
1450 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1451 {
1452 int error, changed;
1453 uint32_t new_value;
1454
1455 if (NULL == gRootDomain) {
1456 return ENOENT;
1457 }
1458 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1459 if (changed && gIOPMWorkLoop) {
1460 error = update_aotmode(new_value);
1461 }
1462
1463 return error;
1464 }
1465
1466 static SYSCTL_PROC(_kern, OID_AUTO, aotmodebits,
1467 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1468 NULL, 0, sysctl_aotmodebits, "I", "");
1469
1470 static int
sysctl_aotmode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1471 sysctl_aotmode
1472 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1473 {
1474 int error, changed;
1475 uint32_t new_value;
1476
1477 if (NULL == gRootDomain) {
1478 return ENOENT;
1479 }
1480 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1481 if (changed && gIOPMWorkLoop) {
1482 if (new_value) {
1483 new_value = kIOPMAOTModeDefault; // & ~kIOPMAOTModeRespectTimers;
1484 }
1485 error = update_aotmode(new_value);
1486 }
1487
1488 return error;
1489 }
1490
1491 static SYSCTL_PROC(_kern, OID_AUTO, aotmode,
1492 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1493 NULL, 0, sysctl_aotmode, "I", "");
1494
1495 //******************************************************************************
1496
1497 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeCalendarKey;
1498 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeSecondsKey;
1499 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerCalendarKey;
1500 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerSecondsKey;
1501 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugWakeRelativeKey;
1502 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugPowerRelativeKey;
1503 static OSSharedPtr<const OSSymbol> gIOPMSettingMaintenanceWakeCalendarKey;
1504 static OSSharedPtr<const OSSymbol> gIOPMSettingSleepServiceWakeCalendarKey;
1505 static OSSharedPtr<const OSSymbol> gIOPMSettingSilentRunningKey;
1506 static OSSharedPtr<const OSSymbol> gIOPMUserTriggeredFullWakeKey;
1507 static OSSharedPtr<const OSSymbol> gIOPMUserIsActiveKey;
1508 static OSSharedPtr<const OSSymbol> gIOPMSettingLowLatencyAudioModeKey;
1509
1510 //******************************************************************************
1511 // start
1512 //
1513 //******************************************************************************
1514
1515 #define kRootDomainSettingsCount 20
1516 #define kRootDomainNoPublishSettingsCount 4
1517
1518 bool
start(IOService * nub)1519 IOPMrootDomain::start( IOService * nub )
1520 {
1521 OSSharedPtr<OSIterator> psIterator;
1522 OSSharedPtr<OSDictionary> tmpDict;
1523
1524 super::start(nub);
1525
1526 gRootDomain = this;
1527 gIOPMSettingAutoWakeCalendarKey = OSSymbol::withCString(kIOPMSettingAutoWakeCalendarKey);
1528 gIOPMSettingAutoWakeSecondsKey = OSSymbol::withCString(kIOPMSettingAutoWakeSecondsKey);
1529 gIOPMSettingAutoPowerCalendarKey = OSSymbol::withCString(kIOPMSettingAutoPowerCalendarKey);
1530 gIOPMSettingAutoPowerSecondsKey = OSSymbol::withCString(kIOPMSettingAutoPowerSecondsKey);
1531 gIOPMSettingDebugWakeRelativeKey = OSSymbol::withCString(kIOPMSettingDebugWakeRelativeKey);
1532 gIOPMSettingDebugPowerRelativeKey = OSSymbol::withCString(kIOPMSettingDebugPowerRelativeKey);
1533 gIOPMSettingMaintenanceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingMaintenanceWakeCalendarKey);
1534 gIOPMSettingSleepServiceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingSleepServiceWakeCalendarKey);
1535 gIOPMSettingSilentRunningKey = OSSymbol::withCStringNoCopy(kIOPMSettingSilentRunningKey);
1536 gIOPMUserTriggeredFullWakeKey = OSSymbol::withCStringNoCopy(kIOPMUserTriggeredFullWakeKey);
1537 gIOPMUserIsActiveKey = OSSymbol::withCStringNoCopy(kIOPMUserIsActiveKey);
1538 gIOPMSettingLowLatencyAudioModeKey = OSSymbol::withCStringNoCopy(kIOPMSettingLowLatencyAudioModeKey);
1539
1540 gIOPMStatsResponseTimedOut = OSSymbol::withCString(kIOPMStatsResponseTimedOut);
1541 gIOPMStatsResponseCancel = OSSymbol::withCString(kIOPMStatsResponseCancel);
1542 gIOPMStatsResponseSlow = OSSymbol::withCString(kIOPMStatsResponseSlow);
1543 gIOPMStatsResponsePrompt = OSSymbol::withCString(kIOPMStatsResponsePrompt);
1544 gIOPMStatsDriverPSChangeSlow = OSSymbol::withCString(kIOPMStatsDriverPSChangeSlow);
1545
1546 sleepSupportedPEFunction = OSSymbol::withCString("IOPMSetSleepSupported");
1547 sleepMessagePEFunction = OSSymbol::withCString("IOPMSystemSleepMessage");
1548 gIOPMWakeTypeUserKey = OSSymbol::withCStringNoCopy(kIOPMRootDomainWakeTypeUser);
1549
1550 OSSharedPtr<const OSSymbol> settingsArr[kRootDomainSettingsCount] =
1551 {
1552 OSSymbol::withCString(kIOPMSettingSleepOnPowerButtonKey),
1553 gIOPMSettingAutoWakeSecondsKey,
1554 gIOPMSettingAutoPowerSecondsKey,
1555 gIOPMSettingAutoWakeCalendarKey,
1556 gIOPMSettingAutoPowerCalendarKey,
1557 gIOPMSettingDebugWakeRelativeKey,
1558 gIOPMSettingDebugPowerRelativeKey,
1559 OSSymbol::withCString(kIOPMSettingWakeOnRingKey),
1560 OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey),
1561 OSSymbol::withCString(kIOPMSettingWakeOnClamshellKey),
1562 OSSymbol::withCString(kIOPMSettingWakeOnACChangeKey),
1563 OSSymbol::withCString(kIOPMSettingTimeZoneOffsetKey),
1564 OSSymbol::withCString(kIOPMSettingDisplaySleepUsesDimKey),
1565 OSSymbol::withCString(kIOPMSettingMobileMotionModuleKey),
1566 OSSymbol::withCString(kIOPMSettingGraphicsSwitchKey),
1567 OSSymbol::withCString(kIOPMStateConsoleShutdown),
1568 OSSymbol::withCString(kIOPMSettingProModeControl),
1569 OSSymbol::withCString(kIOPMSettingProModeDefer),
1570 gIOPMSettingSilentRunningKey,
1571 gIOPMSettingLowLatencyAudioModeKey,
1572 };
1573
1574 OSSharedPtr<const OSSymbol> noPublishSettingsArr[kRootDomainNoPublishSettingsCount] =
1575 {
1576 OSSymbol::withCString(kIOPMSettingProModeControl),
1577 OSSymbol::withCString(kIOPMSettingProModeDefer),
1578 gIOPMSettingSilentRunningKey,
1579 gIOPMSettingLowLatencyAudioModeKey,
1580 };
1581
1582 #if DEVELOPMENT || DEBUG
1583 #if defined(XNU_TARGET_OS_OSX)
1584 PE_parse_boot_argn("darkwake", &gDarkWakeFlags, sizeof(gDarkWakeFlags));
1585 PE_parse_boot_argn("clamshell", &gClamshellFlags, sizeof(gClamshellFlags));
1586 #endif /* defined(XNU_TARGET_OS_OSX) */
1587 #endif /* DEVELOPMENT || DEBUG */
1588
1589 PE_parse_boot_argn("noidle", &gNoIdleFlag, sizeof(gNoIdleFlag));
1590 PE_parse_boot_argn("swd_sleeptimeout", &gSwdSleepTimeout, sizeof(gSwdSleepTimeout));
1591 PE_parse_boot_argn("swd_waketimeout", &gSwdWakeTimeout, sizeof(gSwdWakeTimeout));
1592 PE_parse_boot_argn("swd_timeout", &gSwdSleepWakeTimeout, sizeof(gSwdSleepWakeTimeout));
1593 PE_parse_boot_argn("haltmspanic", &gHaltTimeMaxPanic, sizeof(gHaltTimeMaxPanic));
1594 PE_parse_boot_argn("haltmslog", &gHaltTimeMaxLog, sizeof(gHaltTimeMaxLog));
1595
1596 // read noidle setting from Device Tree
1597 if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
1598 DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
1599 }
1600
1601 queue_init(&aggressivesQueue);
1602 aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1603 aggressivesData = OSData::withCapacity(
1604 sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1605
1606 featuresDictLock = IOLockAlloc();
1607 settingsCtrlLock = IOLockAlloc();
1608 wakeEventLock = IOLockAlloc();
1609 gHaltLogLock = IOLockAlloc();
1610
1611 extraSleepTimer = thread_call_allocate(
1612 idleSleepTimerExpired,
1613 (thread_call_param_t) this);
1614
1615 powerButtonDown = thread_call_allocate(
1616 powerButtonDownCallout,
1617 (thread_call_param_t) this);
1618
1619 powerButtonUp = thread_call_allocate(
1620 powerButtonUpCallout,
1621 (thread_call_param_t) this);
1622
1623 diskSyncCalloutEntry = thread_call_allocate(
1624 &disk_sync_callout,
1625 (thread_call_param_t) this);
1626 updateConsoleUsersEntry = thread_call_allocate(
1627 &updateConsoleUsersCallout,
1628 (thread_call_param_t) this);
1629
1630 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1631 fullWakeThreadCall = thread_call_allocate_with_options(
1632 OSMemberFunctionCast(thread_call_func_t, this,
1633 &IOPMrootDomain::fullWakeDelayedWork),
1634 (thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1635 THREAD_CALL_OPTIONS_ONCE);
1636 #endif
1637
1638 setProperty(kIOSleepSupportedKey, true);
1639
1640 bzero(&gPMStats, sizeof(gPMStats));
1641
1642 pmTracer = PMTraceWorker::tracer(this);
1643
1644 pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1645
1646 userDisabledAllSleep = false;
1647 systemBooting = true;
1648 idleSleepEnabled = false;
1649 sleepSlider = 0;
1650 idleSleepTimerPending = false;
1651 wrangler = NULL;
1652 clamshellClosed = false;
1653 clamshellExists = false;
1654 #if DISPLAY_WRANGLER_PRESENT
1655 clamshellDisabled = true;
1656 #else
1657 clamshellDisabled = false;
1658 #endif
1659 clamshellIgnoreClose = false;
1660 acAdaptorConnected = true;
1661 clamshellSleepDisableMask = 0;
1662 gWakeReasonString[0] = '\0';
1663
1664 // Initialize to user active.
1665 // Will never transition to user inactive w/o wrangler.
1666 fullWakeReason = kFullWakeReasonLocalUser;
1667 userIsActive = userWasActive = true;
1668 clock_get_uptime(&gUserActiveAbsTime);
1669 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1670
1671 // Set the default system capabilities at boot.
1672 _currentCapability = kIOPMSystemCapabilityCPU |
1673 kIOPMSystemCapabilityGraphics |
1674 kIOPMSystemCapabilityAudio |
1675 kIOPMSystemCapabilityNetwork;
1676
1677 _pendingCapability = _currentCapability;
1678 _desiredCapability = _currentCapability;
1679 _highestCapability = _currentCapability;
1680 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1681
1682 queuedSleepWakeUUIDString = NULL;
1683 initializeBootSessionUUID();
1684 pmStatsAppResponses = OSArray::withCapacity(5);
1685 _statsNameKey = OSSymbol::withCString(kIOPMStatsNameKey);
1686 _statsPIDKey = OSSymbol::withCString(kIOPMStatsPIDKey);
1687 _statsTimeMSKey = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1688 _statsResponseTypeKey = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1689 _statsMessageTypeKey = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1690 _statsPowerCapsKey = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1691 assertOnWakeSecs = -1;// Invalid value to prevent updates
1692
1693 pmStatsLock = IOLockAlloc();
1694 idxPMCPUClamshell = kCPUUnknownIndex;
1695 idxPMCPULimitedPower = kCPUUnknownIndex;
1696
1697 tmpDict = OSDictionary::withCapacity(1);
1698 setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1699
1700 // Set a default "SystemPowerProfileOverrideDict" for platform
1701 // drivers without any overrides.
1702 if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1703 tmpDict = OSDictionary::withCapacity(1);
1704 setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1705 }
1706
1707 settingsCallbacks = OSDictionary::withCapacity(1);
1708
1709 // Create a list of the valid PM settings that we'll relay to
1710 // interested clients in setProperties() => setPMSetting()
1711 allowedPMSettings = OSArray::withObjects(
1712 (const OSObject **)settingsArr,
1713 kRootDomainSettingsCount,
1714 0);
1715
1716 // List of PM settings that should not automatically publish itself
1717 // as a feature when registered by a listener.
1718 noPublishPMSettings = OSArray::withObjects(
1719 (const OSObject **)noPublishSettingsArr,
1720 kRootDomainNoPublishSettingsCount,
1721 0);
1722
1723 fPMSettingsDict = OSDictionary::withCapacity(5);
1724 preventIdleSleepList = OSSet::withCapacity(8);
1725 preventSystemSleepList = OSSet::withCapacity(2);
1726
1727 PMinit(); // creates gIOPMWorkLoop
1728 gIOPMWorkLoop = getIOPMWorkloop();
1729
1730 // Create IOPMPowerStateQueue used to queue external power
1731 // events, and to handle those events on the PM work loop.
1732 pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1733 this, OSMemberFunctionCast(IOEventSource::Action, this,
1734 &IOPMrootDomain::dispatchPowerEvent));
1735 gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1736
1737 _aotMode = 0;
1738 _aotTimerES = IOTimerEventSource::timerEventSource(this,
1739 OSMemberFunctionCast(IOTimerEventSource::Action,
1740 this, &IOPMrootDomain::aotEvaluate));
1741 gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1742
1743 // Avoid publishing service early so gIOPMWorkLoop is
1744 // guaranteed to be initialized by rootDomain.
1745 setPMRootDomain(this);
1746
1747 // create our power parent
1748 gPatriarch = new IORootParent;
1749 gPatriarch->init();
1750 gPatriarch->attach(this);
1751 gPatriarch->start(this);
1752 gPatriarch->addPowerChild(this);
1753
1754 registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1755 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1756
1757 // install power change handler
1758 gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1759
1760 #if DISPLAY_WRANGLER_PRESENT
1761 wranglerIdleSettings = OSDictionary::withCapacity(1);
1762 OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1763
1764 if (wranglerIdleSettings && wranglerIdlePeriod) {
1765 wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1766 wranglerIdlePeriod.get());
1767 }
1768
1769 #endif /* DISPLAY_WRANGLER_PRESENT */
1770
1771 lowLatencyAudioNotifierDict = OSDictionary::withCapacity(2);
1772 lowLatencyAudioNotifyStateSym = OSSymbol::withCString("LowLatencyAudioNotifyState");
1773 lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1774 lowLatencyAudioNotifyStateVal = OSNumber::withNumber(0ull, 32);
1775 lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1776
1777 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1778 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1779 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1780 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1781 }
1782
1783 OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1784 setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1785
1786 // IOBacklightDisplay can take a long time to load at boot, or it may
1787 // not load at all if you're booting with clamshell closed. We publish
1788 // 'DisplayDims' here redundantly to get it published early and at all.
1789 OSSharedPtr<OSDictionary> matching;
1790 matching = serviceMatching("IOPMPowerSource");
1791 psIterator = getMatchingServices(matching.get());
1792
1793 if (psIterator && psIterator->getNextObject()) {
1794 // There's at least one battery on the system, so we publish
1795 // 'DisplayDims' support for the LCD.
1796 publishFeature("DisplayDims");
1797 }
1798
1799 // read swd_panic boot-arg
1800 PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1801 gWillShutdownSysctlRegistered = true;
1802
1803 #if HIBERNATION
1804 #if defined(__arm64__)
1805 #endif /* defined(__arm64__) */
1806 IOHibernateSystemInit(this);
1807 #endif
1808
1809 registerService(); // let clients find us
1810
1811 return true;
1812 }
1813
1814 //******************************************************************************
1815 // setProperties
1816 //
1817 // Receive a setProperty call
1818 // The "System Boot" property means the system is completely booted.
1819 //******************************************************************************
1820
1821 IOReturn
setProperties(OSObject * props_obj)1822 IOPMrootDomain::setProperties( OSObject * props_obj )
1823 {
1824 IOReturn return_value = kIOReturnSuccess;
1825 OSDictionary *dict = OSDynamicCast(OSDictionary, props_obj);
1826 OSBoolean *b = NULL;
1827 OSNumber *n = NULL;
1828 const OSSymbol *key = NULL;
1829 OSObject *obj = NULL;
1830 OSSharedPtr<OSCollectionIterator> iter;
1831
1832 if (!dict) {
1833 return kIOReturnBadArgument;
1834 }
1835
1836 bool clientEntitled = false;
1837 {
1838 OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1839 clientEntitled = (obj == kOSBooleanTrue);
1840 }
1841
1842 if (!clientEntitled) {
1843 const char * errorSuffix = NULL;
1844
1845 // IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1846 // That API can set 6 possible keys that are checked below.
1847 if ((dict->getCount() == 1) &&
1848 (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1849 dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1850 dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1851 dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1852 dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1853 dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1854 return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1855 if (return_value != kIOReturnSuccess) {
1856 errorSuffix = "privileged";
1857 }
1858 } else {
1859 return_value = kIOReturnNotPermitted;
1860 errorSuffix = "entitled";
1861 }
1862
1863 if (return_value != kIOReturnSuccess) {
1864 OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1865 DLOG("%s failed, process %s is not %s\n", __func__,
1866 procName ? procName->getCStringNoCopy() : "", errorSuffix);
1867 return return_value;
1868 }
1869 }
1870
1871 OSSharedPtr<const OSSymbol> publish_simulated_battery_string = OSSymbol::withCString("SoftwareSimulatedBatteries");
1872 OSSharedPtr<const OSSymbol> boot_complete_string = OSSymbol::withCString("System Boot Complete");
1873 OSSharedPtr<const OSSymbol> sys_shutdown_string = OSSymbol::withCString("System Shutdown");
1874 OSSharedPtr<const OSSymbol> stall_halt_string = OSSymbol::withCString("StallSystemAtHalt");
1875 OSSharedPtr<const OSSymbol> battery_warning_disabled_string = OSSymbol::withCString("BatteryWarningsDisabled");
1876 OSSharedPtr<const OSSymbol> idle_seconds_string = OSSymbol::withCString("System Idle Seconds");
1877 OSSharedPtr<const OSSymbol> idle_milliseconds_string = OSSymbol::withCString("System Idle Milliseconds");
1878 OSSharedPtr<const OSSymbol> sleepdisabled_string = OSSymbol::withCString("SleepDisabled");
1879 OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1880 OSSharedPtr<const OSSymbol> loginwindow_progress_string = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1881 OSSharedPtr<const OSSymbol> coredisplay_progress_string = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1882 OSSharedPtr<const OSSymbol> coregraphics_progress_string = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1883 #if DEBUG || DEVELOPMENT
1884 OSSharedPtr<const OSSymbol> clamshell_close_string = OSSymbol::withCString("IOPMTestClamshellClose");
1885 OSSharedPtr<const OSSymbol> clamshell_open_string = OSSymbol::withCString("IOPMTestClamshellOpen");
1886 OSSharedPtr<const OSSymbol> ac_detach_string = OSSymbol::withCString("IOPMTestACDetach");
1887 OSSharedPtr<const OSSymbol> ac_attach_string = OSSymbol::withCString("IOPMTestACAttach");
1888 OSSharedPtr<const OSSymbol> desktopmode_set_string = OSSymbol::withCString("IOPMTestDesktopModeSet");
1889 OSSharedPtr<const OSSymbol> desktopmode_remove_string = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1890 #endif
1891
1892 #if HIBERNATION
1893 OSSharedPtr<const OSSymbol> hibernatemode_string = OSSymbol::withCString(kIOHibernateModeKey);
1894 OSSharedPtr<const OSSymbol> hibernatefile_string = OSSymbol::withCString(kIOHibernateFileKey);
1895 OSSharedPtr<const OSSymbol> hibernatefilemin_string = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1896 OSSharedPtr<const OSSymbol> hibernatefilemax_string = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1897 OSSharedPtr<const OSSymbol> hibernatefreeratio_string = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1898 OSSharedPtr<const OSSymbol> hibernatefreetime_string = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1899 #endif
1900
1901 iter = OSCollectionIterator::withCollection(dict);
1902 if (!iter) {
1903 return_value = kIOReturnNoMemory;
1904 goto exit;
1905 }
1906
1907 while ((key = (const OSSymbol *) iter->getNextObject()) &&
1908 (obj = dict->getObject(key))) {
1909 if (key->isEqualTo(publish_simulated_battery_string.get())) {
1910 if (OSDynamicCast(OSBoolean, obj)) {
1911 publishResource(key, kOSBooleanTrue);
1912 }
1913 } else if (key->isEqualTo(idle_seconds_string.get())) {
1914 if ((n = OSDynamicCast(OSNumber, obj))) {
1915 setProperty(key, n);
1916 idleMilliSeconds = n->unsigned32BitValue() * 1000;
1917 }
1918 } else if (key->isEqualTo(idle_milliseconds_string.get())) {
1919 if ((n = OSDynamicCast(OSNumber, obj))) {
1920 setProperty(key, n);
1921 idleMilliSeconds = n->unsigned32BitValue();
1922 }
1923 } else if (key->isEqualTo(boot_complete_string.get())) {
1924 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1925 } else if (key->isEqualTo(sys_shutdown_string.get())) {
1926 if ((b = OSDynamicCast(OSBoolean, obj))) {
1927 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1928 }
1929 } else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1930 setProperty(key, obj);
1931 }
1932 #if HIBERNATION
1933 else if (key->isEqualTo(hibernatemode_string.get()) ||
1934 key->isEqualTo(hibernatefilemin_string.get()) ||
1935 key->isEqualTo(hibernatefilemax_string.get()) ||
1936 key->isEqualTo(hibernatefreeratio_string.get()) ||
1937 key->isEqualTo(hibernatefreetime_string.get())) {
1938 if ((n = OSDynamicCast(OSNumber, obj))) {
1939 setProperty(key, n);
1940 }
1941 } else if (key->isEqualTo(hibernatefile_string.get())) {
1942 OSString * str = OSDynamicCast(OSString, obj);
1943 if (str) {
1944 setProperty(key, str);
1945 }
1946 }
1947 #endif
1948 else if (key->isEqualTo(sleepdisabled_string.get())) {
1949 if ((b = OSDynamicCast(OSBoolean, obj))) {
1950 setProperty(key, b);
1951 pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1952 }
1953 } else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1954 obj->retain();
1955 pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1956 } else if (key->isEqualTo(loginwindow_progress_string.get())) {
1957 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1958 uint32_t data = n->unsigned32BitValue();
1959 pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1960 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1961 }
1962 } else if (key->isEqualTo(coredisplay_progress_string.get())) {
1963 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1964 uint32_t data = n->unsigned32BitValue();
1965 pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1966 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1967 }
1968 } else if (key->isEqualTo(coregraphics_progress_string.get())) {
1969 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1970 uint32_t data = n->unsigned32BitValue();
1971 pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1972 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1973 }
1974 } else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1975 key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1976 key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1977 key->isEqualTo(stall_halt_string.get())) {
1978 if ((b = OSDynamicCast(OSBoolean, obj))) {
1979 setProperty(key, b);
1980 }
1981 } else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1982 key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1983 key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1984 key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1985 if ((n = OSDynamicCast(OSNumber, obj))) {
1986 setProperty(key, n);
1987 }
1988 } else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1989 if (kOSBooleanTrue == obj) {
1990 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1991 } else {
1992 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1993 }
1994 DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
1995 }
1996 #if DEBUG || DEVELOPMENT
1997 else if (key->isEqualTo(clamshell_close_string.get())) {
1998 DLOG("SetProperties: setting clamshell close\n");
1999 UInt32 msg = kIOPMClamshellClosed;
2000 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2001 } else if (key->isEqualTo(clamshell_open_string.get())) {
2002 DLOG("SetProperties: setting clamshell open\n");
2003 UInt32 msg = kIOPMClamshellOpened;
2004 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2005 } else if (key->isEqualTo(ac_detach_string.get())) {
2006 DLOG("SetProperties: setting ac detach\n");
2007 UInt32 msg = kIOPMSetACAdaptorConnected;
2008 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2009 } else if (key->isEqualTo(ac_attach_string.get())) {
2010 DLOG("SetProperties: setting ac attach\n");
2011 UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
2012 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2013 } else if (key->isEqualTo(desktopmode_set_string.get())) {
2014 DLOG("SetProperties: setting desktopmode");
2015 UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
2016 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2017 } else if (key->isEqualTo(desktopmode_remove_string.get())) {
2018 DLOG("SetProperties: removing desktopmode\n");
2019 UInt32 msg = kIOPMSetDesktopMode;
2020 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2021 }
2022 #endif
2023 // Relay our allowed PM settings onto our registered PM clients
2024 else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2025 return_value = setPMSetting(key, obj);
2026 if (kIOReturnSuccess != return_value) {
2027 break;
2028 }
2029 } else {
2030 DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2031 }
2032 }
2033
2034 exit:
2035 return return_value;
2036 }
2037
2038 // MARK: -
2039 // MARK: Aggressiveness
2040
2041 //******************************************************************************
2042 // setAggressiveness
2043 //
2044 // Override IOService::setAggressiveness()
2045 //******************************************************************************
2046
2047 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2048 IOPMrootDomain::setAggressiveness(
2049 unsigned long type,
2050 unsigned long value )
2051 {
2052 return setAggressiveness( type, value, 0 );
2053 }
2054
2055 /*
2056 * Private setAggressiveness() with an internal options argument.
2057 */
2058 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2059 IOPMrootDomain::setAggressiveness(
2060 unsigned long type,
2061 unsigned long value,
2062 IOOptionBits options )
2063 {
2064 AggressivesRequest * entry;
2065 AggressivesRequest * request;
2066 bool found = false;
2067
2068 if ((type > UINT_MAX) || (value > UINT_MAX)) {
2069 return kIOReturnBadArgument;
2070 }
2071
2072 if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2073 DLOG("setAggressiveness(%x) %s = %u\n",
2074 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2075 } else {
2076 DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2077 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2078 }
2079
2080 request = IOMallocType(AggressivesRequest);
2081 request->options = options;
2082 request->dataType = kAggressivesRequestTypeRecord;
2083 request->data.record.type = (uint32_t) type;
2084 request->data.record.value = (uint32_t) value;
2085
2086 AGGRESSIVES_LOCK();
2087
2088 // Update disk quick spindown flag used by getAggressiveness().
2089 // Never merge requests with quick spindown flags set.
2090
2091 if (options & kAggressivesOptionQuickSpindownEnable) {
2092 gAggressivesState |= kAggressivesStateQuickSpindown;
2093 } else if (options & kAggressivesOptionQuickSpindownDisable) {
2094 gAggressivesState &= ~kAggressivesStateQuickSpindown;
2095 } else {
2096 // Coalesce requests with identical aggressives types.
2097 // Deal with callers that calls us too "aggressively".
2098
2099 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2100 {
2101 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2102 (entry->data.record.type == type) &&
2103 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2104 entry->data.record.value = (uint32_t) value;
2105 found = true;
2106 break;
2107 }
2108 }
2109 }
2110
2111 if (!found) {
2112 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2113 }
2114
2115 AGGRESSIVES_UNLOCK();
2116
2117 if (found) {
2118 IOFreeType(request, AggressivesRequest);
2119 }
2120
2121 if (options & kAggressivesOptionSynchronous) {
2122 handleAggressivesRequests(); // not truly synchronous
2123 } else {
2124 thread_call_enter(aggressivesThreadCall);
2125 }
2126
2127 return kIOReturnSuccess;
2128 }
2129
2130 //******************************************************************************
2131 // getAggressiveness
2132 //
2133 // Override IOService::setAggressiveness()
2134 // Fetch the aggressiveness factor with the given type.
2135 //******************************************************************************
2136
2137 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2138 IOPMrootDomain::getAggressiveness(
2139 unsigned long type,
2140 unsigned long * outLevel )
2141 {
2142 uint32_t value = 0;
2143 int source = 0;
2144
2145 if (!outLevel || (type > UINT_MAX)) {
2146 return kIOReturnBadArgument;
2147 }
2148
2149 AGGRESSIVES_LOCK();
2150
2151 // Disk quick spindown in effect, report value = 1
2152
2153 if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2154 (type == kPMMinutesToSpinDown)) {
2155 value = kAggressivesMinValue;
2156 source = 1;
2157 }
2158
2159 // Consult the pending request queue.
2160
2161 if (!source) {
2162 AggressivesRequest * entry;
2163
2164 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2165 {
2166 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2167 (entry->data.record.type == type) &&
2168 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2169 value = entry->data.record.value;
2170 source = 2;
2171 break;
2172 }
2173 }
2174 }
2175
2176 // Consult the backend records.
2177
2178 if (!source && aggressivesData) {
2179 AggressivesRecord * record;
2180 int i, count;
2181
2182 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2183 record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2184
2185 for (i = 0; i < count; i++, record++) {
2186 if (record->type == type) {
2187 value = record->value;
2188 source = 3;
2189 break;
2190 }
2191 }
2192 }
2193
2194 AGGRESSIVES_UNLOCK();
2195
2196 if (source) {
2197 *outLevel = (unsigned long) value;
2198 return kIOReturnSuccess;
2199 } else {
2200 DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2201 *outLevel = 0; // default return = 0, driver may not check for error
2202 return kIOReturnInvalid;
2203 }
2204 }
2205
2206 //******************************************************************************
2207 // joinAggressiveness
2208 //
2209 // Request from IOService to join future aggressiveness broadcasts.
2210 //******************************************************************************
2211
2212 IOReturn
joinAggressiveness(IOService * service)2213 IOPMrootDomain::joinAggressiveness(
2214 IOService * service )
2215 {
2216 AggressivesRequest * request;
2217
2218 if (!service || (service == this)) {
2219 return kIOReturnBadArgument;
2220 }
2221
2222 DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2223
2224 request = IOMallocType(AggressivesRequest);
2225 request->dataType = kAggressivesRequestTypeService;
2226 request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2227
2228 AGGRESSIVES_LOCK();
2229 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2230 AGGRESSIVES_UNLOCK();
2231
2232 thread_call_enter(aggressivesThreadCall);
2233
2234 return kIOReturnSuccess;
2235 }
2236
2237 //******************************************************************************
2238 // handleAggressivesRequests
2239 //
2240 // Backend thread processes all incoming aggressiveness requests in the queue.
2241 //******************************************************************************
2242
2243 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2244 handleAggressivesFunction(
2245 thread_call_param_t param1,
2246 thread_call_param_t param2 )
2247 {
2248 if (param1) {
2249 ((IOPMrootDomain *) param1)->handleAggressivesRequests();
2250 }
2251 }
2252
2253 void
handleAggressivesRequests(void)2254 IOPMrootDomain::handleAggressivesRequests( void )
2255 {
2256 AggressivesRecord * start;
2257 AggressivesRecord * record;
2258 AggressivesRequest * request;
2259 queue_head_t joinedQueue;
2260 int i, count;
2261 bool broadcast;
2262 bool found;
2263 bool pingSelf = false;
2264
2265 AGGRESSIVES_LOCK();
2266
2267 if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2268 queue_empty(&aggressivesQueue)) {
2269 goto unlock_done;
2270 }
2271
2272 gAggressivesState |= kAggressivesStateBusy;
2273 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2274 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2275
2276 do{
2277 broadcast = false;
2278 queue_init(&joinedQueue);
2279
2280 do{
2281 // Remove request from the incoming queue in FIFO order.
2282 queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2283 switch (request->dataType) {
2284 case kAggressivesRequestTypeRecord:
2285 // Update existing record if found.
2286 found = false;
2287 for (i = 0, record = start; i < count; i++, record++) {
2288 if (record->type == request->data.record.type) {
2289 found = true;
2290
2291 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2292 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2293 broadcast = true;
2294 record->flags |= (kAggressivesRecordFlagMinValue |
2295 kAggressivesRecordFlagModified);
2296 DLOG("disk spindown accelerated, was %u min\n",
2297 record->value);
2298 }
2299 } else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2300 if (record->flags & kAggressivesRecordFlagMinValue) {
2301 broadcast = true;
2302 record->flags |= kAggressivesRecordFlagModified;
2303 record->flags &= ~kAggressivesRecordFlagMinValue;
2304 DLOG("disk spindown restored to %u min\n",
2305 record->value);
2306 }
2307 } else if (record->value != request->data.record.value) {
2308 record->value = request->data.record.value;
2309 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2310 broadcast = true;
2311 record->flags |= kAggressivesRecordFlagModified;
2312 }
2313 }
2314 break;
2315 }
2316 }
2317
2318 // No matching record, append a new record.
2319 if (!found &&
2320 ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2321 AggressivesRecord newRecord;
2322
2323 newRecord.flags = kAggressivesRecordFlagModified;
2324 newRecord.type = request->data.record.type;
2325 newRecord.value = request->data.record.value;
2326 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2327 newRecord.flags |= kAggressivesRecordFlagMinValue;
2328 DLOG("disk spindown accelerated\n");
2329 }
2330
2331 aggressivesData->appendValue(newRecord);
2332
2333 // OSData may have switched to another (larger) buffer.
2334 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2335 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2336 broadcast = true;
2337 }
2338
2339 // Finished processing the request, release it.
2340 IOFreeType(request, AggressivesRequest);
2341 break;
2342
2343 case kAggressivesRequestTypeService:
2344 // synchronizeAggressives() will free request.
2345 queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2346 break;
2347
2348 default:
2349 panic("bad aggressives request type %x", request->dataType);
2350 break;
2351 }
2352 } while (!queue_empty(&aggressivesQueue));
2353
2354 // Release the lock to perform work, with busy flag set.
2355 if (!queue_empty(&joinedQueue) || broadcast) {
2356 AGGRESSIVES_UNLOCK();
2357 if (!queue_empty(&joinedQueue)) {
2358 synchronizeAggressives(&joinedQueue, start, count);
2359 }
2360 if (broadcast) {
2361 broadcastAggressives(start, count);
2362 }
2363 AGGRESSIVES_LOCK();
2364 }
2365
2366 // Remove the modified flag from all records.
2367 for (i = 0, record = start; i < count; i++, record++) {
2368 if ((record->flags & kAggressivesRecordFlagModified) &&
2369 ((record->type == kPMMinutesToDim) ||
2370 (record->type == kPMMinutesToSleep))) {
2371 pingSelf = true;
2372 }
2373
2374 record->flags &= ~kAggressivesRecordFlagModified;
2375 }
2376
2377 // Check the incoming queue again since new entries may have been
2378 // added while lock was released above.
2379 } while (!queue_empty(&aggressivesQueue));
2380
2381 gAggressivesState &= ~kAggressivesStateBusy;
2382
2383 unlock_done:
2384 AGGRESSIVES_UNLOCK();
2385
2386 // Root domain is interested in system and display sleep slider changes.
2387 // Submit a power event to handle those changes on the PM work loop.
2388
2389 if (pingSelf && pmPowerStateQueue) {
2390 pmPowerStateQueue->submitPowerEvent(
2391 kPowerEventPolicyStimulus,
2392 (void *) kStimulusAggressivenessChanged );
2393 }
2394 }
2395
2396 //******************************************************************************
2397 // synchronizeAggressives
2398 //
2399 // Push all known aggressiveness records to one or more IOService.
2400 //******************************************************************************
2401
2402 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2403 IOPMrootDomain::synchronizeAggressives(
2404 queue_head_t * joinedQueue,
2405 const AggressivesRecord * array,
2406 int count )
2407 {
2408 OSSharedPtr<IOService> service;
2409 AggressivesRequest * request;
2410 const AggressivesRecord * record;
2411 IOPMDriverCallEntry callEntry;
2412 uint32_t value;
2413 int i;
2414
2415 while (!queue_empty(joinedQueue)) {
2416 queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2417 if (request->dataType == kAggressivesRequestTypeService) {
2418 // retained by joinAggressiveness(), so take ownership
2419 service = os::move(request->data.service);
2420 } else {
2421 service.reset();
2422 }
2423
2424 IOFreeType(request, AggressivesRequest);
2425 request = NULL;
2426
2427 if (service) {
2428 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2429 for (i = 0, record = array; i < count; i++, record++) {
2430 value = record->value;
2431 if (record->flags & kAggressivesRecordFlagMinValue) {
2432 value = kAggressivesMinValue;
2433 }
2434
2435 _LOG("synchronizeAggressives 0x%x = %u to %s\n",
2436 record->type, value, service->getName());
2437 service->setAggressiveness(record->type, value);
2438 }
2439 service->deassertPMDriverCall(&callEntry);
2440 }
2441 }
2442 }
2443 }
2444
2445 //******************************************************************************
2446 // broadcastAggressives
2447 //
2448 // Traverse PM tree and call setAggressiveness() for records that have changed.
2449 //******************************************************************************
2450
2451 void
broadcastAggressives(const AggressivesRecord * array,int count)2452 IOPMrootDomain::broadcastAggressives(
2453 const AggressivesRecord * array,
2454 int count )
2455 {
2456 OSSharedPtr<IORegistryIterator> iter;
2457 IORegistryEntry *entry;
2458 OSSharedPtr<IORegistryEntry> child;
2459 IOPowerConnection *connect;
2460 IOService *service;
2461 const AggressivesRecord *record;
2462 IOPMDriverCallEntry callEntry;
2463 uint32_t value;
2464 int i;
2465
2466 iter = IORegistryIterator::iterateOver(
2467 this, gIOPowerPlane, kIORegistryIterateRecursively);
2468 if (iter) {
2469 do{
2470 // !! reset the iterator
2471 iter->reset();
2472 while ((entry = iter->getNextObject())) {
2473 connect = OSDynamicCast(IOPowerConnection, entry);
2474 if (!connect || !connect->getReadyFlag()) {
2475 continue;
2476 }
2477
2478 child = connect->copyChildEntry(gIOPowerPlane);
2479 if (child) {
2480 if ((service = OSDynamicCast(IOService, child.get()))) {
2481 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2482 for (i = 0, record = array; i < count; i++, record++) {
2483 if (record->flags & kAggressivesRecordFlagModified) {
2484 value = record->value;
2485 if (record->flags & kAggressivesRecordFlagMinValue) {
2486 value = kAggressivesMinValue;
2487 }
2488 _LOG("broadcastAggressives %x = %u to %s\n",
2489 record->type, value, service->getName());
2490 service->setAggressiveness(record->type, value);
2491 }
2492 }
2493 service->deassertPMDriverCall(&callEntry);
2494 }
2495 }
2496 }
2497 }
2498 }while (!entry && !iter->isValid());
2499 }
2500 }
2501
2502 //*****************************************
2503 // stackshot on power button press
2504 // ***************************************
2505 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2506 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2507 {
2508 /* Power button pressed during wake
2509 * Take a stackshot
2510 */
2511 DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2512 ((IOPMrootDomain *)us)->takeStackshot(false);
2513 }
2514
2515 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2516 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2517 {
2518 /* Power button released.
2519 * Delete any stackshot data
2520 */
2521 DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2522 ((IOPMrootDomain *)us)->deleteStackshot();
2523 }
2524 //*************************************************************************
2525 //
2526
2527 // MARK: -
2528 // MARK: System Sleep
2529
2530 //******************************************************************************
2531 // startIdleSleepTimer
2532 //
2533 //******************************************************************************
2534
2535 void
startIdleSleepTimer(uint32_t inMilliSeconds)2536 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2537 {
2538 AbsoluteTime deadline;
2539
2540 ASSERT_GATED();
2541 if (gNoIdleFlag) {
2542 DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2543 return;
2544 }
2545 if (inMilliSeconds) {
2546 if (inMilliSeconds < kMinimumTimeBeforeIdleSleep) {
2547 AbsoluteTime now;
2548 uint64_t nsec_since_wake;
2549 uint64_t msec_since_wake;
2550
2551 // Adjust idle timer so it will not expire until atleast kMinimumTimeBeforeIdleSleep milliseconds
2552 // after the most recent AP wake.
2553 clock_get_uptime(&now);
2554 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2555 absolutetime_to_nanoseconds(now, &nsec_since_wake);
2556 msec_since_wake = nsec_since_wake / NSEC_PER_MSEC;
2557
2558 if (msec_since_wake < kMinimumTimeBeforeIdleSleep) {
2559 uint32_t newIdleTimer = kMinimumTimeBeforeIdleSleep - (uint32_t)msec_since_wake;
2560
2561 // Ensure that our new idle timer is not less than inMilliSeconds,
2562 // as we should only be increasing the timer duration, not decreasing it
2563 if (newIdleTimer > inMilliSeconds) {
2564 DLOG("startIdleSleepTimer increasing timeout from %u to %u\n", inMilliSeconds, newIdleTimer);
2565 inMilliSeconds = newIdleTimer;
2566 }
2567 }
2568 }
2569 clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2570 thread_call_enter_delayed(extraSleepTimer, deadline);
2571 idleSleepTimerPending = true;
2572 } else {
2573 thread_call_enter(extraSleepTimer);
2574 }
2575 DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2576 }
2577
2578 //******************************************************************************
2579 // cancelIdleSleepTimer
2580 //
2581 //******************************************************************************
2582
2583 void
cancelIdleSleepTimer(void)2584 IOPMrootDomain::cancelIdleSleepTimer( void )
2585 {
2586 ASSERT_GATED();
2587 if (idleSleepTimerPending) {
2588 DLOG("idle timer cancelled\n");
2589 thread_call_cancel(extraSleepTimer);
2590 idleSleepTimerPending = false;
2591
2592 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2593 AbsoluteTime now;
2594 clock_usec_t microsecs;
2595 clock_get_uptime(&now);
2596 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2597 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
2598 if (assertOnWakeReport) {
2599 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2600 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2601 }
2602 }
2603 }
2604 }
2605
2606 //******************************************************************************
2607 // idleSleepTimerExpired
2608 //
2609 //******************************************************************************
2610
2611 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2612 idleSleepTimerExpired(
2613 thread_call_param_t us, thread_call_param_t )
2614 {
2615 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2616 }
2617
2618 //******************************************************************************
2619 // handleSleepTimerExpiration
2620 //
2621 // The time between the sleep idle timeout and the next longest one has elapsed.
2622 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2623 //******************************************************************************
2624
2625 void
handleSleepTimerExpiration(void)2626 IOPMrootDomain::handleSleepTimerExpiration( void )
2627 {
2628 if (!gIOPMWorkLoop->inGate()) {
2629 gIOPMWorkLoop->runAction(
2630 OSMemberFunctionCast(IOWorkLoop::Action, this,
2631 &IOPMrootDomain::handleSleepTimerExpiration),
2632 this);
2633 return;
2634 }
2635
2636 DLOG("sleep timer expired\n");
2637 ASSERT_GATED();
2638
2639 idleSleepTimerPending = false;
2640 setQuickSpinDownTimeout();
2641 adjustPowerState(true);
2642 }
2643
2644 //******************************************************************************
2645 // getTimeToIdleSleep
2646 //
2647 // Returns number of milliseconds left before going into idle sleep.
2648 // Caller has to make sure that idle sleep is allowed at the time of calling
2649 // this function
2650 //******************************************************************************
2651
2652 uint32_t
getTimeToIdleSleep(void)2653 IOPMrootDomain::getTimeToIdleSleep( void )
2654 {
2655 AbsoluteTime now, lastActivityTime;
2656 uint64_t nanos;
2657 uint32_t minutesSinceUserInactive = 0;
2658 uint32_t sleepDelay = 0;
2659
2660 if (!idleSleepEnabled) {
2661 return 0xffffffff;
2662 }
2663
2664 if (userActivityTime) {
2665 lastActivityTime = userActivityTime;
2666 } else {
2667 lastActivityTime = userBecameInactiveTime;
2668 }
2669
2670 // Ignore any lastActivityTime that predates the last system wake.
2671 // The goal is to avoid a sudden idle sleep right after a dark wake
2672 // due to sleepDelay=0 computed below. The alternative 60s minimum
2673 // timeout should be large enough to allow dark wake to complete,
2674 // at which point the idle timer will be promptly cancelled.
2675 clock_get_uptime(&now);
2676 if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2677 (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2678 SUB_ABSOLUTETIME(&now, &lastActivityTime);
2679 absolutetime_to_nanoseconds(now, &nanos);
2680 minutesSinceUserInactive = nanos / (60000000000ULL);
2681
2682 if (minutesSinceUserInactive >= sleepSlider) {
2683 sleepDelay = 0;
2684 } else {
2685 sleepDelay = sleepSlider - minutesSinceUserInactive;
2686 }
2687 } else {
2688 DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2689 lastActivityTime, now, gIOLastWakeAbsTime);
2690 sleepDelay = sleepSlider;
2691 }
2692
2693 DLOG("user inactive %u min, time to idle sleep %u min\n",
2694 minutesSinceUserInactive, sleepDelay);
2695
2696 return sleepDelay * 60 * 1000;
2697 }
2698
2699 //******************************************************************************
2700 // setQuickSpinDownTimeout
2701 //
2702 //******************************************************************************
2703
2704 void
setQuickSpinDownTimeout(void)2705 IOPMrootDomain::setQuickSpinDownTimeout( void )
2706 {
2707 ASSERT_GATED();
2708 setAggressiveness(
2709 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2710 }
2711
2712 //******************************************************************************
2713 // restoreUserSpinDownTimeout
2714 //
2715 //******************************************************************************
2716
2717 void
restoreUserSpinDownTimeout(void)2718 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2719 {
2720 ASSERT_GATED();
2721 setAggressiveness(
2722 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2723 }
2724
2725 //******************************************************************************
2726 // sleepSystem
2727 //
2728 //******************************************************************************
2729
2730 /* public */
2731 IOReturn
sleepSystem(void)2732 IOPMrootDomain::sleepSystem( void )
2733 {
2734 return sleepSystemOptions(NULL);
2735 }
2736
2737 /* private */
2738 IOReturn
sleepSystemOptions(OSDictionary * options)2739 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2740 {
2741 OSObject *obj = NULL;
2742 OSString *reason = NULL;
2743 /* sleepSystem is a public function, and may be called by any kernel driver.
2744 * And that's bad - drivers should sleep the system by calling
2745 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2746 *
2747 * Note that user space app calls to IOPMSleepSystem() will also travel
2748 * this code path and thus be correctly identified as software sleeps.
2749 */
2750
2751 if (options && options->getObject("OSSwitch")) {
2752 // Log specific sleep cause for OS Switch hibernation
2753 return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2754 }
2755
2756 if (options && (obj = options->getObject("Sleep Reason"))) {
2757 reason = OSDynamicCast(OSString, obj);
2758 if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2759 return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2760 }
2761 if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2762 return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2763 }
2764 }
2765
2766 return privateSleepSystem( kIOPMSleepReasonSoftware);
2767 }
2768
2769 /* private */
2770 IOReturn
privateSleepSystem(uint32_t sleepReason)2771 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2772 {
2773 /* Called from both gated and non-gated context */
2774
2775 if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2776 return kIOReturnNotPermitted;
2777 }
2778
2779 pmPowerStateQueue->submitPowerEvent(
2780 kPowerEventPolicyStimulus,
2781 (void *) kStimulusDemandSystemSleep,
2782 sleepReason);
2783
2784 return kIOReturnSuccess;
2785 }
2786
2787 //******************************************************************************
2788 // powerChangeDone
2789 //
2790 // This overrides powerChangeDone in IOService.
2791 //******************************************************************************
2792 void
powerChangeDone(unsigned long previousPowerState)2793 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2794 {
2795 #if !__i386__ && !__x86_64__
2796 uint64_t timeSinceReset = 0;
2797 #endif
2798 uint64_t now;
2799 unsigned long newState;
2800 clock_sec_t secs;
2801 clock_usec_t microsecs;
2802 uint32_t lastDebugWakeSeconds;
2803 clock_sec_t adjWakeTime;
2804 IOPMCalendarStruct nowCalendar;
2805
2806 ASSERT_GATED();
2807 newState = getPowerState();
2808 DLOG("PowerChangeDone: %s->%s\n",
2809 getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2810
2811 if (previousPowerState == newState) {
2812 return;
2813 }
2814
2815 notifierThread = current_thread();
2816 switch (getPowerState()) {
2817 case SLEEP_STATE: {
2818 if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2819 secs = 0;
2820 microsecs = 0;
2821 PEGetUTCTimeOfDay(&secs, µsecs);
2822
2823 adjWakeTime = 0;
2824 if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2825 IOLog("use _calendarWakeAlarmUTC\n");
2826 adjWakeTime = _calendarWakeAlarmUTC;
2827 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2828 IOLog("accelerate _aotWakeTime for exit\n");
2829 adjWakeTime = secs;
2830 } else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2831 IOLog("accelerate _aotWakeTime for assertion\n");
2832 adjWakeTime = secs;
2833 }
2834 if (adjWakeTime) {
2835 IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2836 }
2837
2838 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2839 IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2840
2841 IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2842 assert(kIOReturnSuccess == ret);
2843 }
2844 if (_aotLastWakeTime) {
2845 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2846 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2847 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2848 gWakeReasonString,
2849 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2850 }
2851 }
2852 _aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2853 if (_aotTimerScheduled) {
2854 _aotTimerES->cancelTimeout();
2855 _aotTimerScheduled = false;
2856 }
2857 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2858
2859 // re-enable this timer for next sleep
2860 cancelIdleSleepTimer();
2861
2862 if (clamshellExists) {
2863 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2864 if (gClamshellFlags & kClamshell_WAR_58009435) {
2865 // Disable clamshell sleep until system has completed full wake.
2866 // This prevents a system sleep request (due to a clamshell close)
2867 // from being queued until the end of system full wake - even if
2868 // other clamshell disable bits outside of our control is wrong.
2869 setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2870 }
2871 #endif
2872
2873 // Log the last known clamshell state before system sleep
2874 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2875 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2876 desktopMode, acAdaptorConnected);
2877 }
2878
2879 clock_get_calendar_absolute_and_microtime(&secs, µsecs, &now);
2880 logtime(secs);
2881 gIOLastSleepTime.tv_sec = secs;
2882 gIOLastSleepTime.tv_usec = microsecs;
2883 if (!_aotLastWakeTime) {
2884 gIOLastUserSleepTime = gIOLastSleepTime;
2885 }
2886
2887 gIOLastWakeTime.tv_sec = 0;
2888 gIOLastWakeTime.tv_usec = 0;
2889 gIOLastSleepAbsTime = now;
2890
2891 if (wake2DarkwakeDelay && sleepDelaysReport) {
2892 clock_sec_t wake2DarkwakeSecs, darkwake2SleepSecs;
2893 // Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2894
2895 SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2896 absolutetime_to_microtime(now, &darkwake2SleepSecs, µsecs);
2897 absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, µsecs);
2898 HISTREPORT_TALLYVALUE(sleepDelaysReport,
2899 (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2900
2901 DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2902 wake2DarkwakeDelay = 0;
2903 }
2904 #if HIBERNATION
2905 LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2906
2907 IOHibernateSystemHasSlept();
2908
2909 evaluateSystemSleepPolicyFinal();
2910 #else
2911 LOG("System Sleep\n");
2912 #endif
2913 if (thermalWarningState) {
2914 OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2915 if (event) {
2916 systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2917 }
2918 }
2919 assertOnWakeSecs = 0;
2920 lowBatteryCondition = false;
2921 thermalEmergencyState = false;
2922
2923 #if DEVELOPMENT || DEBUG
2924 extern int g_should_log_clock_adjustments;
2925 if (g_should_log_clock_adjustments) {
2926 clock_sec_t secs = 0;
2927 clock_usec_t microsecs = 0;
2928 uint64_t now_b = mach_absolute_time();
2929
2930 secs = 0;
2931 microsecs = 0;
2932 PEGetUTCTimeOfDay(&secs, µsecs);
2933
2934 uint64_t now_a = mach_absolute_time();
2935 os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2936 __func__, (unsigned long)secs, microsecs, now_b, now_a);
2937 }
2938 #endif
2939
2940 getPlatform()->sleepKernel();
2941
2942 // The CPU(s) are off at this point,
2943 // Code will resume execution here upon wake.
2944
2945 clock_get_uptime(&gIOLastWakeAbsTime);
2946 IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2947 _highestCapability = 0;
2948
2949 #if HIBERNATION
2950 IOHibernateSystemWake();
2951 #endif
2952
2953 // sleep transition complete
2954 gSleepOrShutdownPending = 0;
2955
2956 // trip the reset of the calendar clock
2957 clock_wakeup_calendar();
2958 clock_get_calendar_microtime(&secs, µsecs);
2959 gIOLastWakeTime.tv_sec = secs;
2960 gIOLastWakeTime.tv_usec = microsecs;
2961
2962 // aot
2963 if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2964 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2965 secs = 0;
2966 microsecs = 0;
2967 PEGetUTCTimeOfDay(&secs, µsecs);
2968 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2969 IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2970 _aotMetrics->sleepCount++;
2971 _aotLastWakeTime = gIOLastWakeAbsTime;
2972 if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2973 _aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
2974 = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
2975 _aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
2976 = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
2977 }
2978
2979 if (_aotTestTime) {
2980 if (_aotWakeTimeUTC <= secs) {
2981 _aotTestTime = _aotTestTime + _aotTestInterval;
2982 }
2983 setWakeTime(_aotTestTime);
2984 }
2985 }
2986
2987 #if HIBERNATION
2988 LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
2989 #endif
2990
2991 lastSleepReason = 0;
2992
2993 lastDebugWakeSeconds = _debugWakeSeconds;
2994 _debugWakeSeconds = 0;
2995 _scheduledAlarmMask = 0;
2996 _nextScheduledAlarmType = NULL;
2997
2998 darkWakeExit = false;
2999 darkWakePowerClamped = false;
3000 darkWakePostTickle = false;
3001 darkWakeHibernateError = false;
3002 darkWakeToSleepASAP = true;
3003 darkWakeLogClamp = true;
3004 sleepTimerMaintenance = false;
3005 sleepToStandby = false;
3006 wranglerTickled = false;
3007 userWasActive = false;
3008 isRTCAlarmWake = false;
3009 clamshellIgnoreClose = false;
3010 fullWakeReason = kFullWakeReasonNone;
3011
3012 #if defined(__i386__) || defined(__x86_64__)
3013 kdebugTrace(kPMLogSystemWake, 0, 0, 0);
3014
3015 OSSharedPtr<OSObject> wakeTypeProp = copyProperty(kIOPMRootDomainWakeTypeKey);
3016 OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
3017 OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
3018 OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
3019
3020 if (wakeReason && (wakeReason->getLength() >= 2) &&
3021 gWakeReasonString[0] == '\0') {
3022 WAKEEVENT_LOCK();
3023 // Until the platform driver can claim its wake reasons
3024 strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
3025 sizeof(gWakeReasonString));
3026 if (!gWakeReasonSysctlRegistered) {
3027 gWakeReasonSysctlRegistered = true;
3028 }
3029 WAKEEVENT_UNLOCK();
3030 }
3031
3032 if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
3033 lowBatteryCondition = true;
3034 darkWakeMaintenance = true;
3035 } else {
3036 #if HIBERNATION
3037 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3038 OSNumber * hibOptions = OSDynamicCast( OSNumber, hibOptionsProp.get());
3039 if (hibernateAborted || ((hibOptions &&
3040 !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
3041 // Hibernate aborted, or EFI brought up graphics
3042 darkWakeExit = true;
3043 if (hibernateAborted) {
3044 DLOG("Hibernation aborted\n");
3045 } else {
3046 DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3047 }
3048 } else
3049 #endif
3050 if (wakeType && (
3051 wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3052 wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3053 // User wake or RTC alarm
3054 darkWakeExit = true;
3055 if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3056 isRTCAlarmWake = true;
3057 }
3058 } else if (wakeType &&
3059 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3060 // SMC standby timer trumps SleepX
3061 darkWakeMaintenance = true;
3062 sleepTimerMaintenance = true;
3063 } else if ((lastDebugWakeSeconds != 0) &&
3064 ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3065 // SleepX before maintenance
3066 darkWakeExit = true;
3067 } else if (wakeType &&
3068 wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3069 darkWakeMaintenance = true;
3070 } else if (wakeType &&
3071 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3072 darkWakeMaintenance = true;
3073 darkWakeSleepService = true;
3074 #if HIBERNATION
3075 if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3076 sleepToStandby = true;
3077 }
3078 #endif
3079 } else if (wakeType &&
3080 wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3081 darkWakeMaintenance = true;
3082 darkWakeHibernateError = true;
3083 } else {
3084 // Unidentified wake source, resume to full wake if debug
3085 // alarm is pending.
3086
3087 if (lastDebugWakeSeconds &&
3088 (!wakeReason || wakeReason->isEqualTo(""))) {
3089 darkWakeExit = true;
3090 }
3091 }
3092 }
3093
3094 if (darkWakeExit) {
3095 darkWakeToSleepASAP = false;
3096 fullWakeReason = kFullWakeReasonLocalUser;
3097 reportUserInput();
3098 } else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3099 handleSetDisplayPowerOn(true);
3100 } else if (!darkWakeMaintenance) {
3101 // Early/late tickle for non-maintenance wake.
3102 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3103 darkWakePostTickle = true;
3104 }
3105 }
3106 #else /* !__i386__ && !__x86_64__ */
3107 timeSinceReset = ml_get_time_since_reset();
3108 kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3109
3110 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3111 wranglerTickled = true;
3112 fullWakeReason = kFullWakeReasonLocalUser;
3113 requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3114 } else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3115 isRTCAlarmWake = true;
3116 fullWakeReason = kFullWakeReasonLocalUser;
3117 requestUserActive(this, "RTC debug alarm");
3118 } else {
3119 #if HIBERNATION
3120 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3121 OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3122 if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3123 fullWakeReason = kFullWakeReasonLocalUser;
3124 requestUserActive(this, "hibernate user wake");
3125 }
3126 #endif
3127 }
3128
3129 // stay awake for at least 30 seconds
3130 startIdleSleepTimer(30 * 1000);
3131 #endif
3132 sleepCnt++;
3133
3134 thread_call_enter(updateConsoleUsersEntry);
3135
3136 // Skip AOT_STATE if we are waking up from an RTC timer.
3137 // This check needs to be done after the epoch change is processed
3138 // and before the changePowerStateWithTagToPriv() call below.
3139 WAKEEVENT_LOCK();
3140 aotShouldExit(false, false);
3141 WAKEEVENT_UNLOCK();
3142
3143 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3144 break;
3145 }
3146 #if !__i386__ && !__x86_64__
3147 case ON_STATE:
3148 case AOT_STATE:
3149 {
3150 DLOG("Force re-evaluating aggressiveness\n");
3151 /* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3152 pmPowerStateQueue->submitPowerEvent(
3153 kPowerEventPolicyStimulus,
3154 (void *) kStimulusNoIdleSleepPreventers );
3155
3156 // After changing to ON_STATE, invalidate any previously queued
3157 // request to change to a state less than ON_STATE. This isn't
3158 // necessary for AOT_STATE or if the device has only one running
3159 // state since the changePowerStateToPriv() issued at the tail
3160 // end of SLEEP_STATE case should take care of that.
3161 if (getPowerState() == ON_STATE) {
3162 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3163 }
3164 break;
3165 }
3166 #endif /* !__i386__ && !__x86_64__ */
3167 }
3168 notifierThread = NULL;
3169 }
3170
3171 //******************************************************************************
3172 // requestPowerDomainState
3173 //
3174 // Extend implementation in IOService. Running on PM work loop thread.
3175 //******************************************************************************
3176
3177 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3178 IOPMrootDomain::requestPowerDomainState(
3179 IOPMPowerFlags childDesire,
3180 IOPowerConnection * childConnection,
3181 unsigned long specification )
3182 {
3183 // Idle and system sleep prevention flags affects driver desire.
3184 // Children desire are irrelevant so they are cleared.
3185
3186 return super::requestPowerDomainState(0, childConnection, specification);
3187 }
3188
3189
3190 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3191 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3192 {
3193 if (!preventers->getCount()) {
3194 return;
3195 }
3196
3197 char *buf_iter = buf + strlen(buf);
3198 char *buf_end = buf + buf_size;
3199
3200 OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3201 OSObject *obj = NULL;
3202
3203 while ((obj = iterator->getNextObject())) {
3204 IOService *srv = OSDynamicCast(IOService, obj);
3205 if (buf_iter < buf_end) {
3206 buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3207 } else {
3208 DLOG("Print buffer exhausted for sleep preventers list\n");
3209 break;
3210 }
3211 }
3212 }
3213
3214 //******************************************************************************
3215 // updatePreventIdleSleepList
3216 //
3217 // Called by IOService on PM work loop.
3218 // Returns true if PM policy recognized the driver's desire to prevent idle
3219 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3220 //******************************************************************************
3221
3222 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3223 IOPMrootDomain::updatePreventIdleSleepList(
3224 IOService * service, bool addNotRemove)
3225 {
3226 unsigned int oldCount;
3227
3228 oldCount = idleSleepPreventersCount();
3229 return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3230 }
3231
3232 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3233 IOPMrootDomain::updatePreventIdleSleepListInternal(
3234 IOService * service, bool addNotRemove, unsigned int oldCount)
3235 {
3236 unsigned int newCount;
3237
3238 ASSERT_GATED();
3239
3240 #if defined(XNU_TARGET_OS_OSX)
3241 // Only the display wrangler and no-idle-sleep kernel assertions
3242 // can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3243 // reported by drivers in their power state table is ignored.
3244 if (service && (service != wrangler) && (service != this)) {
3245 return false;
3246 }
3247 #endif
3248
3249 if (service) {
3250 if (addNotRemove) {
3251 preventIdleSleepList->setObject(service);
3252 DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3253 service->getName(), preventIdleSleepList->getCount());
3254 } else if (preventIdleSleepList->member(service)) {
3255 preventIdleSleepList->removeObject(service);
3256 DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3257 service->getName(), preventIdleSleepList->getCount());
3258 }
3259
3260 if (preventIdleSleepList->getCount()) {
3261 char buf[256] = "Idle Sleep Preventers:";
3262 makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3263 DLOG("%s\n", buf);
3264 }
3265 }
3266
3267 newCount = idleSleepPreventersCount();
3268
3269 if ((oldCount == 0) && (newCount != 0)) {
3270 // Driver added to empty prevent list.
3271 // Update the driver desire to prevent idle sleep.
3272 // Driver desire does not prevent demand sleep.
3273
3274 changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3275 } else if ((oldCount != 0) && (newCount == 0)) {
3276 // Last driver removed from prevent list.
3277 // Drop the driver clamp to allow idle sleep.
3278
3279 changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3280 evaluatePolicy( kStimulusNoIdleSleepPreventers );
3281 }
3282 messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3283 &newCount, sizeof(newCount));
3284
3285 #if defined(XNU_TARGET_OS_OSX)
3286 if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3287 DLOG("Cannot cancel idle sleep\n");
3288 return false; // do not idle-cancel
3289 }
3290 #endif
3291
3292 return true;
3293 }
3294
3295 //******************************************************************************
3296 // startSpinDump
3297 //******************************************************************************
3298
3299 void
startSpinDump(uint32_t spindumpKind)3300 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3301 {
3302 messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3303 }
3304
3305 //******************************************************************************
3306 // preventSystemSleepListUpdate
3307 //
3308 // Called by IOService on PM work loop.
3309 //******************************************************************************
3310
3311 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3312 IOPMrootDomain::updatePreventSystemSleepList(
3313 IOService * service, bool addNotRemove )
3314 {
3315 unsigned int oldCount, newCount;
3316
3317 ASSERT_GATED();
3318 if (this == service) {
3319 return;
3320 }
3321
3322 oldCount = preventSystemSleepList->getCount();
3323 if (addNotRemove) {
3324 preventSystemSleepList->setObject(service);
3325 DLOG("Added %s to system sleep preventers list (Total %u)\n",
3326 service->getName(), preventSystemSleepList->getCount());
3327 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3328 AbsoluteTime now;
3329 clock_usec_t microsecs;
3330 clock_get_uptime(&now);
3331 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3332 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
3333 if (assertOnWakeReport) {
3334 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3335 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3336 }
3337 }
3338 } else if (preventSystemSleepList->member(service)) {
3339 preventSystemSleepList->removeObject(service);
3340 DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3341 service->getName(), preventSystemSleepList->getCount());
3342
3343 if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3344 // Lost all system sleep preventers.
3345 // Send stimulus if system sleep was blocked, and is in dark wake.
3346 evaluatePolicy( kStimulusDarkWakeEvaluate );
3347 }
3348 }
3349
3350 newCount = preventSystemSleepList->getCount();
3351 if (newCount) {
3352 char buf[256] = "System Sleep Preventers:";
3353 makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3354 DLOG("%s\n", buf);
3355 }
3356
3357 messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3358 &newCount, sizeof(newCount));
3359 }
3360
3361 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3362 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3363 {
3364 OSSharedPtr<OSCollectionIterator> iterator;
3365 OSObject *object = NULL;
3366 OSSharedPtr<OSArray> array;
3367
3368 if (!gIOPMWorkLoop->inGate()) {
3369 gIOPMWorkLoop->runAction(
3370 OSMemberFunctionCast(IOWorkLoop::Action, this,
3371 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3372 this, (void *)idleSleepList, (void *)systemSleepList);
3373 return;
3374 }
3375
3376 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3377 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3378 array = OSArray::withCapacity(5);
3379
3380 if (iterator && array) {
3381 while ((object = iterator->getNextObject())) {
3382 IOService *service = OSDynamicCast(IOService, object);
3383 if (service) {
3384 OSSharedPtr<const OSSymbol> name = service->copyName();
3385 if (name) {
3386 array->setObject(name.get());
3387 }
3388 }
3389 }
3390 }
3391 *idleSleepList = array.detach();
3392 }
3393
3394 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3395 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3396 array = OSArray::withCapacity(5);
3397
3398 if (iterator && array) {
3399 while ((object = iterator->getNextObject())) {
3400 IOService *service = OSDynamicCast(IOService, object);
3401 if (service) {
3402 OSSharedPtr<const OSSymbol> name = service->copyName();
3403 if (name) {
3404 array->setObject(name.get());
3405 }
3406 }
3407 }
3408 }
3409 *systemSleepList = array.detach();
3410 }
3411 }
3412
3413 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3414 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3415 {
3416 OSSharedPtr<OSCollectionIterator> iterator;
3417 OSObject *object = NULL;
3418 OSSharedPtr<OSArray> array;
3419
3420 if (!gIOPMWorkLoop->inGate()) {
3421 gIOPMWorkLoop->runAction(
3422 OSMemberFunctionCast(IOWorkLoop::Action, this,
3423 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3424 this, (void *)idleSleepList, (void *)systemSleepList);
3425 return;
3426 }
3427
3428 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3429 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3430 array = OSArray::withCapacity(5);
3431
3432 if (iterator && array) {
3433 while ((object = iterator->getNextObject())) {
3434 IOService *service = OSDynamicCast(IOService, object);
3435 if (service) {
3436 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3437 OSSharedPtr<const OSSymbol> name = service->copyName();
3438 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3439 if (dict && name && id) {
3440 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3441 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3442 array->setObject(dict.get());
3443 }
3444 }
3445 }
3446 }
3447 *idleSleepList = array.detach();
3448 }
3449
3450 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3451 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3452 array = OSArray::withCapacity(5);
3453
3454 if (iterator && array) {
3455 while ((object = iterator->getNextObject())) {
3456 IOService *service = OSDynamicCast(IOService, object);
3457 if (service) {
3458 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3459 OSSharedPtr<const OSSymbol> name = service->copyName();
3460 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3461 if (dict && name && id) {
3462 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3463 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3464 array->setObject(dict.get());
3465 }
3466 }
3467 }
3468 }
3469 *systemSleepList = array.detach();
3470 }
3471 }
3472
3473 //******************************************************************************
3474 // tellChangeDown
3475 //
3476 // Override the superclass implementation to send a different message type.
3477 //******************************************************************************
3478
3479 bool
tellChangeDown(unsigned long stateNum)3480 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3481 {
3482 DLOG("tellChangeDown %s->%s\n",
3483 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3484
3485 if (SLEEP_STATE == stateNum) {
3486 // Legacy apps were already told in the full->dark transition
3487 if (!ignoreTellChangeDown) {
3488 tracePoint( kIOPMTracePointSleepApplications );
3489 } else {
3490 tracePoint( kIOPMTracePointSleepPriorityClients );
3491 }
3492 }
3493
3494 if (!ignoreTellChangeDown) {
3495 userActivityAtSleep = userActivityCount;
3496 DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3497
3498 if (SLEEP_STATE == stateNum) {
3499 hibernateAborted = false;
3500
3501 // Direct callout into OSKext so it can disable kext unloads
3502 // during sleep/wake to prevent deadlocks.
3503 OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3504
3505 IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3506
3507 // Two change downs are sent by IOServicePM. Ignore the 2nd.
3508 // But tellClientsWithResponse() must be called for both.
3509 ignoreTellChangeDown = true;
3510 }
3511 }
3512
3513 return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3514 }
3515
3516 //******************************************************************************
3517 // askChangeDown
3518 //
3519 // Override the superclass implementation to send a different message type.
3520 // This must be idle sleep since we don't ask during any other power change.
3521 //******************************************************************************
3522
3523 bool
askChangeDown(unsigned long stateNum)3524 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3525 {
3526 DLOG("askChangeDown %s->%s\n",
3527 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3528
3529 // Don't log for dark wake entry
3530 if (kSystemTransitionSleep == _systemTransitionType) {
3531 tracePoint( kIOPMTracePointSleepApplications );
3532 }
3533
3534 return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3535 }
3536
3537 //******************************************************************************
3538 // askChangeDownDone
3539 //
3540 // An opportunity for root domain to cancel the power transition,
3541 // possibily due to an assertion created by powerd in response to
3542 // kIOMessageCanSystemSleep.
3543 //
3544 // Idle sleep:
3545 // full -> dark wake transition
3546 // 1. Notify apps and powerd with kIOMessageCanSystemSleep
3547 // 2. askChangeDownDone()
3548 // dark -> sleep transition
3549 // 1. Notify powerd with kIOMessageCanSystemSleep
3550 // 2. askChangeDownDone()
3551 //
3552 // Demand sleep:
3553 // full -> dark wake transition
3554 // 1. Notify powerd with kIOMessageCanSystemSleep
3555 // 2. askChangeDownDone()
3556 // dark -> sleep transition
3557 // 1. Notify powerd with kIOMessageCanSystemSleep
3558 // 2. askChangeDownDone()
3559 //******************************************************************************
3560
3561 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3562 IOPMrootDomain::askChangeDownDone(
3563 IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3564 {
3565 DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3566 *inOutChangeFlags, *cancel,
3567 _systemTransitionType,
3568 _currentCapability, _pendingCapability);
3569
3570 if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3571 // Dark->Sleep transition.
3572 // Check if there are any deny sleep assertions.
3573 // lastSleepReason already set by handleOurPowerChangeStart()
3574
3575 if (!checkSystemCanSleep(lastSleepReason)) {
3576 // Cancel dark wake to sleep transition.
3577 // Must re-scan assertions upon entering dark wake.
3578
3579 *cancel = true;
3580 DLOG("cancel dark->sleep\n");
3581 }
3582 if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3583 uint64_t now = mach_continuous_time();
3584 if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3585 && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3586 *cancel = true;
3587 IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3588 }
3589 }
3590 }
3591 }
3592
3593 //******************************************************************************
3594 // systemDidNotSleep
3595 //
3596 // Work common to both canceled or aborted sleep.
3597 //******************************************************************************
3598
3599 void
systemDidNotSleep(void)3600 IOPMrootDomain::systemDidNotSleep( void )
3601 {
3602 // reset console lock state
3603 thread_call_enter(updateConsoleUsersEntry);
3604
3605 if (idleSleepEnabled) {
3606 if (!wrangler) {
3607 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3608 startIdleSleepTimer(kIdleSleepRetryInterval);
3609 #else
3610 startIdleSleepTimer(idleMilliSeconds);
3611 #endif
3612 } else if (!userIsActive) {
3613 // Manually start the idle sleep timer besides waiting for
3614 // the user to become inactive.
3615 startIdleSleepTimer(kIdleSleepRetryInterval);
3616 }
3617 }
3618
3619 preventTransitionToUserActive(false);
3620 IOService::setAdvisoryTickleEnable( true );
3621
3622 // After idle revert and cancel, send a did-change message to powerd
3623 // to balance the previous will-change message. Kernel clients do not
3624 // need this since sleep cannot be canceled once they are notified.
3625
3626 if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3627 (_pendingCapability != _currentCapability) &&
3628 ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3629 // Differs from a real capability gain change where notifyRef != 0,
3630 // but it is zero here since no response is expected.
3631
3632 IOPMSystemCapabilityChangeParameters params;
3633
3634 bzero(¶ms, sizeof(params));
3635 params.fromCapabilities = _pendingCapability;
3636 params.toCapabilities = _currentCapability;
3637 params.changeFlags = kIOPMSystemCapabilityDidChange;
3638
3639 DLOG("MESG cap %x->%x did change\n",
3640 params.fromCapabilities, params.toCapabilities);
3641 messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3642 ¶ms, sizeof(params));
3643 }
3644 }
3645
3646 //******************************************************************************
3647 // tellNoChangeDown
3648 //
3649 // Notify registered applications and kernel clients that we are not dropping
3650 // power.
3651 //
3652 // We override the superclass implementation so we can send a different message
3653 // type to the client or application being notified.
3654 //
3655 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3656 //******************************************************************************
3657
3658 void
tellNoChangeDown(unsigned long stateNum)3659 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3660 {
3661 DLOG("tellNoChangeDown %s->%s\n",
3662 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3663
3664 // Sleep canceled, clear the sleep trace point.
3665 tracePoint(kIOPMTracePointSystemUp);
3666
3667 systemDidNotSleep();
3668 return tellClients( kIOMessageSystemWillNotSleep );
3669 }
3670
3671 //******************************************************************************
3672 // tellChangeUp
3673 //
3674 // Notify registered applications and kernel clients that we are raising power.
3675 //
3676 // We override the superclass implementation so we can send a different message
3677 // type to the client or application being notified.
3678 //******************************************************************************
3679
3680 void
tellChangeUp(unsigned long stateNum)3681 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3682 {
3683 DLOG("tellChangeUp %s->%s\n",
3684 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3685
3686 ignoreTellChangeDown = false;
3687
3688 if (stateNum == ON_STATE) {
3689 // Direct callout into OSKext so it can disable kext unloads
3690 // during sleep/wake to prevent deadlocks.
3691 OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3692
3693 // Notify platform that sleep was cancelled or resumed.
3694 getPlatform()->callPlatformFunction(
3695 sleepMessagePEFunction.get(), false,
3696 (void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3697 NULL, NULL, NULL);
3698
3699 if (getPowerState() == ON_STATE) {
3700 // Sleep was cancelled by idle cancel or revert
3701 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3702 // rdar://problem/50363791
3703 // If system is in dark wake and sleep is cancelled, do not
3704 // send SystemWillPowerOn/HasPoweredOn messages to kernel
3705 // priority clients. They haven't yet seen a SystemWillSleep
3706 // message before the cancellation. So make sure the kernel
3707 // client bit is cleared in _systemMessageClientMask before
3708 // invoking the tellClients() below. This bit may have been
3709 // set by handleOurPowerChangeStart() anticipating a successful
3710 // sleep and setting the filter mask ahead of time allows the
3711 // SystemWillSleep message to go through.
3712 _systemMessageClientMask &= ~kSystemMessageClientKernel;
3713 }
3714
3715 systemDidNotSleep();
3716 tellClients( kIOMessageSystemWillPowerOn );
3717 }
3718
3719 tracePoint( kIOPMTracePointWakeApplications );
3720 tellClients( kIOMessageSystemHasPoweredOn );
3721 } else if (stateNum == AOT_STATE) {
3722 if (getPowerState() == AOT_STATE) {
3723 // Sleep was cancelled by idle cancel or revert
3724 startIdleSleepTimer(idleMilliSeconds);
3725 }
3726 }
3727 }
3728
3729 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3730 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3731 ((params)->fromCapabilities & (flag)) && \
3732 (((params)->toCapabilities & (flag)) == 0))
3733
3734 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3735 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3736 ((params)->toCapabilities & (flag)) && \
3737 (((params)->fromCapabilities & (flag)) == 0))
3738
3739 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3740 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3741 ((params)->fromCapabilities & (flag)) && \
3742 (((params)->toCapabilities & (flag)) == 0))
3743
3744 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3745 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3746 ((params)->toCapabilities & (flag)) && \
3747 (((params)->fromCapabilities & (flag)) == 0))
3748
3749 //******************************************************************************
3750 // sysPowerDownHandler
3751 //
3752 // Perform a vfs sync before system sleep.
3753 //******************************************************************************
3754
3755 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3756 IOPMrootDomain::sysPowerDownHandler(
3757 void * target, void * refCon,
3758 UInt32 messageType, IOService * service,
3759 void * messageArgs, vm_size_t argSize )
3760 {
3761 static UInt32 lastSystemMessageType = 0;
3762 IOReturn ret = 0;
3763
3764 DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3765
3766 // rdar://problem/50363791
3767 // Sanity check to make sure the SystemWill/Has message types are
3768 // received in the expected order for all kernel priority clients.
3769 if (messageType == kIOMessageSystemWillSleep ||
3770 messageType == kIOMessageSystemWillPowerOn ||
3771 messageType == kIOMessageSystemHasPoweredOn) {
3772 switch (messageType) {
3773 case kIOMessageSystemWillPowerOn:
3774 assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3775 break;
3776 case kIOMessageSystemHasPoweredOn:
3777 assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3778 break;
3779 }
3780
3781 lastSystemMessageType = messageType;
3782 }
3783
3784 if (!gRootDomain) {
3785 return kIOReturnUnsupported;
3786 }
3787
3788 if (messageType == kIOMessageSystemCapabilityChange) {
3789 IOPMSystemCapabilityChangeParameters * params =
3790 (IOPMSystemCapabilityChangeParameters *) messageArgs;
3791
3792 // Interested applications have been notified of an impending power
3793 // change and have acked (when applicable).
3794 // This is our chance to save whatever state we can before powering
3795 // down.
3796 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3797 // via callout
3798
3799 DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3800 params->fromCapabilities, params->toCapabilities,
3801 params->changeFlags);
3802
3803 if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3804 // We will ack within 20 seconds
3805 params->maxWaitForReply = 20 * 1000 * 1000;
3806
3807 #if HIBERNATION
3808 gRootDomain->evaluateSystemSleepPolicyEarly();
3809
3810 // add in time we could spend freeing pages
3811 if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3812 params->maxWaitForReply = kCapabilityClientMaxWait;
3813 }
3814 DLOG("sysPowerDownHandler max wait %d s\n",
3815 (int) (params->maxWaitForReply / 1000 / 1000));
3816 #endif
3817
3818 // Notify platform that sleep has begun, after the early
3819 // sleep policy evaluation.
3820 getPlatform()->callPlatformFunction(
3821 sleepMessagePEFunction.get(), false,
3822 (void *)(uintptr_t) kIOMessageSystemWillSleep,
3823 NULL, NULL, NULL);
3824
3825 if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3826 // Purposely delay the ack and hope that shutdown occurs quickly.
3827 // Another option is not to schedule the thread and wait for
3828 // ack timeout...
3829 AbsoluteTime deadline;
3830 clock_interval_to_deadline( 30, kSecondScale, &deadline );
3831 thread_call_enter1_delayed(
3832 gRootDomain->diskSyncCalloutEntry,
3833 (thread_call_param_t)(uintptr_t) params->notifyRef,
3834 deadline );
3835 } else {
3836 thread_call_enter1(
3837 gRootDomain->diskSyncCalloutEntry,
3838 (thread_call_param_t)(uintptr_t) params->notifyRef);
3839 }
3840 }
3841 #if HIBERNATION
3842 else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3843 // We will ack within 110 seconds
3844 params->maxWaitForReply = 110 * 1000 * 1000;
3845
3846 thread_call_enter1(
3847 gRootDomain->diskSyncCalloutEntry,
3848 (thread_call_param_t)(uintptr_t) params->notifyRef);
3849 }
3850 #endif
3851 ret = kIOReturnSuccess;
3852 }
3853
3854 return ret;
3855 }
3856
3857 //******************************************************************************
3858 // handleQueueSleepWakeUUID
3859 //
3860 // Called from IOPMrootDomain when we're initiating a sleep,
3861 // or indirectly from PM configd when PM decides to clear the UUID.
3862 // PM clears the UUID several minutes after successful wake from sleep,
3863 // so that we might associate App spindumps with the immediately previous
3864 // sleep/wake.
3865 //
3866 // @param obj has a retain on it. We're responsible for releasing that retain.
3867 //******************************************************************************
3868
3869 void
handleQueueSleepWakeUUID(OSObject * obj)3870 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3871 {
3872 OSSharedPtr<OSString> str;
3873
3874 if (kOSBooleanFalse == obj) {
3875 handlePublishSleepWakeUUID(false);
3876 } else {
3877 str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3878 if (str) {
3879 // This branch caches the UUID for an upcoming sleep/wake
3880 queuedSleepWakeUUIDString = str;
3881 DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3882 }
3883 }
3884 }
3885 //******************************************************************************
3886 // handlePublishSleepWakeUUID
3887 //
3888 // Called from IOPMrootDomain when we're initiating a sleep,
3889 // or indirectly from PM configd when PM decides to clear the UUID.
3890 // PM clears the UUID several minutes after successful wake from sleep,
3891 // so that we might associate App spindumps with the immediately previous
3892 // sleep/wake.
3893 //******************************************************************************
3894
3895 void
handlePublishSleepWakeUUID(bool shouldPublish)3896 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3897 {
3898 ASSERT_GATED();
3899
3900 /*
3901 * Clear the current UUID
3902 */
3903 if (gSleepWakeUUIDIsSet) {
3904 DLOG("SleepWake UUID cleared\n");
3905
3906 gSleepWakeUUIDIsSet = false;
3907
3908 removeProperty(kIOPMSleepWakeUUIDKey);
3909 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3910 }
3911
3912 /*
3913 * Optionally, publish a new UUID
3914 */
3915 if (queuedSleepWakeUUIDString && shouldPublish) {
3916 OSSharedPtr<OSString> publishThisUUID;
3917
3918 publishThisUUID = queuedSleepWakeUUIDString;
3919
3920 if (publishThisUUID) {
3921 setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3922 }
3923
3924 gSleepWakeUUIDIsSet = true;
3925 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3926
3927 queuedSleepWakeUUIDString.reset();
3928 }
3929 }
3930
3931 //******************************************************************************
3932 // IOPMGetSleepWakeUUIDKey
3933 //
3934 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3935 // To get the full key -- a C string -- the buffer must large enough for
3936 // the end-of-string character.
3937 // The key is expected to be an UUID string
3938 //******************************************************************************
3939
3940 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3941 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3942 {
3943 if (!gSleepWakeUUIDIsSet) {
3944 return false;
3945 }
3946
3947 if (buffer != NULL) {
3948 OSSharedPtr<OSString> string =
3949 OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3950
3951 if (!string) {
3952 *buffer = '\0';
3953 } else {
3954 strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3955 }
3956 }
3957
3958 return true;
3959 }
3960
3961 //******************************************************************************
3962 // lowLatencyAudioNotify
3963 //
3964 // Used to send an update about low latency audio activity to interested
3965 // clients. To keep the overhead minimal the OSDictionary used here
3966 // is initialized at boot.
3967 //******************************************************************************
3968
3969 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3970 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3971 {
3972 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
3973 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
3974 lowLatencyAudioNotifyTimestampVal->setValue(time);
3975 lowLatencyAudioNotifyStateVal->setValue(state);
3976 setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
3977 } else {
3978 DLOG("LowLatencyAudioNotify error\n");
3979 }
3980 return;
3981 }
3982
3983 //******************************************************************************
3984 // IOPMrootDomainRTNotifier
3985 //
3986 // Used by performance controller to update the timestamp and state associated
3987 // with low latency audio activity in the system.
3988 //******************************************************************************
3989
3990 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)3991 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
3992 {
3993 gRootDomain->lowLatencyAudioNotify(time, state);
3994 return;
3995 }
3996
3997 //******************************************************************************
3998 // initializeBootSessionUUID
3999 //
4000 // Initialize the boot session uuid at boot up and sets it into registry.
4001 //******************************************************************************
4002
4003 void
initializeBootSessionUUID(void)4004 IOPMrootDomain::initializeBootSessionUUID(void)
4005 {
4006 uuid_t new_uuid;
4007 uuid_string_t new_uuid_string;
4008
4009 uuid_generate(new_uuid);
4010 uuid_unparse_upper(new_uuid, new_uuid_string);
4011 memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
4012
4013 setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
4014 }
4015
4016 //******************************************************************************
4017 // Root domain uses the private and tagged changePowerState methods for
4018 // tracking and logging purposes.
4019 //******************************************************************************
4020
4021 #define REQUEST_TAG_TO_REASON(x) ((uint16_t)x)
4022
4023 static uint32_t
nextRequestTag(IOPMRequestTag tag)4024 nextRequestTag( IOPMRequestTag tag )
4025 {
4026 static SInt16 msb16 = 1;
4027 uint16_t id = OSAddAtomic16(1, &msb16);
4028 return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
4029 }
4030
4031 // TODO: remove this shim function and exported symbol
4032 IOReturn
changePowerStateTo(unsigned long ordinal)4033 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
4034 {
4035 return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
4036 }
4037
4038 // TODO: remove this shim function and exported symbol
4039 IOReturn
changePowerStateToPriv(unsigned long ordinal)4040 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
4041 {
4042 return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
4043 }
4044
4045 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4046 IOPMrootDomain::changePowerStateWithOverrideTo(
4047 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4048 {
4049 uint32_t tag = nextRequestTag(reason);
4050 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4051
4052 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4053 return kIOReturnUnsupported;
4054 }
4055
4056 return super::changePowerStateWithOverrideTo(ordinal, tag);
4057 }
4058
4059 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4060 IOPMrootDomain::changePowerStateWithTagTo(
4061 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4062 {
4063 uint32_t tag = nextRequestTag(reason);
4064 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4065
4066 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4067 return kIOReturnUnsupported;
4068 }
4069
4070 return super::changePowerStateWithTagTo(ordinal, tag);
4071 }
4072
4073 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4074 IOPMrootDomain::changePowerStateWithTagToPriv(
4075 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4076 {
4077 uint32_t tag = nextRequestTag(reason);
4078 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4079
4080 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4081 return kIOReturnUnsupported;
4082 }
4083
4084 return super::changePowerStateWithTagToPriv(ordinal, tag);
4085 }
4086
4087 //******************************************************************************
4088 // activity detect
4089 //
4090 //******************************************************************************
4091
4092 bool
activitySinceSleep(void)4093 IOPMrootDomain::activitySinceSleep(void)
4094 {
4095 return userActivityCount != userActivityAtSleep;
4096 }
4097
4098 bool
abortHibernation(void)4099 IOPMrootDomain::abortHibernation(void)
4100 {
4101 #if __arm64__
4102 // don't allow hibernation to be aborted on ARM due to user activity
4103 // since once ApplePMGR decides we're hibernating, we can't turn back
4104 // see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4105 return false;
4106 #else
4107 bool ret = activitySinceSleep();
4108
4109 if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4110 DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4111 hibernateAborted = true;
4112 }
4113 return ret;
4114 #endif
4115 }
4116
4117 extern "C" int
hibernate_should_abort(void)4118 hibernate_should_abort(void)
4119 {
4120 if (gRootDomain) {
4121 return gRootDomain->abortHibernation();
4122 } else {
4123 return 0;
4124 }
4125 }
4126
4127 //******************************************************************************
4128 // willNotifyPowerChildren
4129 //
4130 // Called after all interested drivers have all acknowledged the power change,
4131 // but before any power children is informed. Dispatched though a thread call,
4132 // so it is safe to perform work that might block on a sleeping disk. PM state
4133 // machine (not thread) will block w/o timeout until this function returns.
4134 //******************************************************************************
4135
4136 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4137 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4138 {
4139 OSSharedPtr<OSDictionary> dict;
4140 OSSharedPtr<OSNumber> secs;
4141
4142 if (SLEEP_STATE == newPowerState) {
4143 notifierThread = current_thread();
4144 if (updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange)) {
4145 AbsoluteTime deadline;
4146
4147 clock_interval_to_deadline(10, kSecondScale, &deadline);
4148 #if defined(XNU_TARGET_OS_OSX)
4149 vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4150 #endif /* defined(XNU_TARGET_OS_OSX) */
4151 }
4152
4153 _aotReadyToFullWake = false;
4154 #if 0
4155 if (_aotLingerTime) {
4156 uint64_t deadline;
4157 IOLog("aot linger no return\n");
4158 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4159 clock_delay_until(deadline);
4160 }
4161 #endif
4162 if (!_aotMode) {
4163 _aotTestTime = 0;
4164 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4165 _aotLastWakeTime = 0;
4166 if (_aotMetrics) {
4167 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4168 }
4169 } else if (!_aotNow && !_debugWakeSeconds) {
4170 _aotNow = true;
4171 _aotPendingFlags = 0;
4172 _aotTasksSuspended = true;
4173 _aotLastWakeTime = 0;
4174 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4175 if (kIOPMAOTModeCycle & _aotMode) {
4176 clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4177 _aotTestTime = mach_continuous_time() + _aotTestInterval;
4178 setWakeTime(_aotTestTime);
4179 }
4180 uint32_t lingerSecs;
4181 if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4182 lingerSecs = 0;
4183 }
4184 clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4185 clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4186 clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4187 }
4188
4189 #if HIBERNATION
4190 IOHibernateSystemSleep();
4191 IOHibernateIOKitSleep();
4192 #endif
4193 if (gRootDomain->activitySinceSleep()) {
4194 dict = OSDictionary::withCapacity(1);
4195 secs = OSNumber::withNumber(1, 32);
4196
4197 if (dict && secs) {
4198 dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4199 gRootDomain->setProperties(dict.get());
4200 MSG("Reverting sleep with relative wake\n");
4201 }
4202 }
4203
4204 notifierThread = NULL;
4205 }
4206 }
4207
4208 //******************************************************************************
4209 // willTellSystemCapabilityDidChange
4210 //
4211 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4212 // domain is raising its power state, immediately after notifying interested
4213 // drivers and power children.
4214 //******************************************************************************
4215
4216 void
willTellSystemCapabilityDidChange(void)4217 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4218 {
4219 if ((_systemTransitionType == kSystemTransitionWake) &&
4220 !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4221 // After powering up drivers, dark->full promotion on the current wake
4222 // transition is no longer possible. That is because the next machine
4223 // state will issue the system capability change messages.
4224 // The darkWakePowerClamped flag may already be set if the system has
4225 // at least one driver that was power clamped due to dark wake.
4226 // This function sets the darkWakePowerClamped flag in case there
4227 // is no power-clamped driver in the system.
4228 //
4229 // Last opportunity to exit dark wake using:
4230 // requestFullWake( kFullWakeReasonLocalUser );
4231
4232 if (!darkWakePowerClamped) {
4233 if (darkWakeLogClamp) {
4234 AbsoluteTime now;
4235 uint64_t nsec;
4236
4237 clock_get_uptime(&now);
4238 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4239 absolutetime_to_nanoseconds(now, &nsec);
4240 DLOG("dark wake promotion disabled at %u ms\n",
4241 ((int)((nsec) / NSEC_PER_MSEC)));
4242 }
4243 darkWakePowerClamped = true;
4244 }
4245 }
4246 }
4247
4248 //******************************************************************************
4249 // sleepOnClamshellClosed
4250 //
4251 // contains the logic to determine if the system should sleep when the clamshell
4252 // is closed.
4253 //******************************************************************************
4254
4255 bool
shouldSleepOnClamshellClosed(void)4256 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4257 {
4258 if (!clamshellExists) {
4259 return false;
4260 }
4261
4262 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4263 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4264
4265 return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4266 }
4267
4268 bool
shouldSleepOnRTCAlarmWake(void)4269 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4270 {
4271 // Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4272 // closed && battery
4273 if (!clamshellExists) {
4274 return false;
4275 }
4276
4277 DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4278 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4279
4280 return !acAdaptorConnected && !clamshellSleepDisableMask;
4281 }
4282
4283 void
sendClientClamshellNotification(void)4284 IOPMrootDomain::sendClientClamshellNotification( void )
4285 {
4286 /* Only broadcast clamshell alert if clamshell exists. */
4287 if (!clamshellExists) {
4288 return;
4289 }
4290
4291 setProperty(kAppleClamshellStateKey,
4292 clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4293
4294 setProperty(kAppleClamshellCausesSleepKey,
4295 shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4296
4297 /* Argument to message is a bitfiel of
4298 * ( kClamshellStateBit | kClamshellSleepBit )
4299 */
4300 messageClients(kIOPMMessageClamshellStateChange,
4301 (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4302 | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4303 }
4304
4305 //******************************************************************************
4306 // getSleepSupported
4307 //
4308 // Deprecated
4309 //******************************************************************************
4310
4311 IOOptionBits
getSleepSupported(void)4312 IOPMrootDomain::getSleepSupported( void )
4313 {
4314 return platformSleepSupport;
4315 }
4316
4317 //******************************************************************************
4318 // setSleepSupported
4319 //
4320 // Deprecated
4321 //******************************************************************************
4322
4323 void
setSleepSupported(IOOptionBits flags)4324 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4325 {
4326 DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4327 OSBitOrAtomic(flags, &platformSleepSupport);
4328 }
4329
4330 //******************************************************************************
4331 // setClamShellSleepDisable
4332 //
4333 //******************************************************************************
4334
4335 void
setClamShellSleepDisable(bool disable,uint32_t bitmask)4336 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4337 {
4338 uint32_t oldMask;
4339
4340 // User client calls this in non-gated context
4341 if (gIOPMWorkLoop->inGate() == false) {
4342 gIOPMWorkLoop->runAction(
4343 OSMemberFunctionCast(IOWorkLoop::Action, this,
4344 &IOPMrootDomain::setClamShellSleepDisable),
4345 (OSObject *) this,
4346 (void *) disable, (void *)(uintptr_t) bitmask);
4347 return;
4348 }
4349
4350 oldMask = clamshellSleepDisableMask;
4351 if (disable) {
4352 clamshellSleepDisableMask |= bitmask;
4353 } else {
4354 clamshellSleepDisableMask &= ~bitmask;
4355 }
4356 DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4357
4358 if (clamshellExists && clamshellClosed &&
4359 (clamshellSleepDisableMask != oldMask) &&
4360 (clamshellSleepDisableMask == 0)) {
4361 handlePowerNotification(kLocalEvalClamshellCommand);
4362 }
4363 }
4364
4365 //******************************************************************************
4366 // wakeFromDoze
4367 //
4368 // Deprecated.
4369 //******************************************************************************
4370
4371 void
wakeFromDoze(void)4372 IOPMrootDomain::wakeFromDoze( void )
4373 {
4374 // Preserve symbol for familes (IOUSBFamily and IOGraphics)
4375 }
4376
4377 //******************************************************************************
4378 // recordRTCAlarm
4379 //
4380 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4381 // should be a dark wake or a full wake. Both Maintenance and SleepService
4382 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4383 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4384 // PMSettings are ignored.
4385 //
4386 // Caller serialized using settingsCtrlLock.
4387 //******************************************************************************
4388
4389 void
recordRTCAlarm(const OSSymbol * type,OSObject * object)4390 IOPMrootDomain::recordRTCAlarm(
4391 const OSSymbol *type,
4392 OSObject *object )
4393 {
4394 uint32_t previousAlarmMask = _scheduledAlarmMask;
4395
4396 if (type == gIOPMSettingDebugWakeRelativeKey) {
4397 OSNumber * n = OSDynamicCast(OSNumber, object);
4398 if (n) {
4399 // Debug wake has highest scheduling priority so it overrides any
4400 // pre-existing alarm.
4401 uint32_t debugSecs = n->unsigned32BitValue();
4402 _nextScheduledAlarmType.reset(type, OSRetain);
4403 _nextScheduledAlarmUTC = debugSecs;
4404
4405 _debugWakeSeconds = debugSecs;
4406 OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4407 DLOG("next alarm (%s) in %u secs\n",
4408 type->getCStringNoCopy(), debugSecs);
4409 }
4410 } else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4411 (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4412 (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4413 OSData * data = OSDynamicCast(OSData, object);
4414 if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4415 const IOPMCalendarStruct * cs;
4416 bool replaceNextAlarm = false;
4417 clock_sec_t secs;
4418
4419 cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4420 secs = IOPMConvertCalendarToSeconds(cs);
4421 DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4422
4423 // Update the next scheduled alarm type
4424 if ((_nextScheduledAlarmType == NULL) ||
4425 ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4426 (secs < _nextScheduledAlarmUTC))) {
4427 replaceNextAlarm = true;
4428 }
4429
4430 if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4431 if (cs->year) {
4432 _calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4433 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4434 } else {
4435 // TODO: can this else-block be removed?
4436 _calendarWakeAlarmUTC = 0;
4437 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4438 }
4439 }
4440 if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4441 OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4442 }
4443 if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4444 OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4445 }
4446
4447 if (replaceNextAlarm) {
4448 _nextScheduledAlarmType.reset(type, OSRetain);
4449 _nextScheduledAlarmUTC = secs;
4450 DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4451 }
4452 }
4453 }
4454
4455 if (_scheduledAlarmMask != previousAlarmMask) {
4456 DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4457 }
4458 }
4459
4460 // MARK: -
4461 // MARK: Features
4462
4463 //******************************************************************************
4464 // publishFeature
4465 //
4466 // Adds a new feature to the supported features dictionary
4467 //******************************************************************************
4468
4469 void
publishFeature(const char * feature)4470 IOPMrootDomain::publishFeature( const char * feature )
4471 {
4472 publishFeature(feature, kRD_AllPowerSources, NULL);
4473 }
4474
4475 //******************************************************************************
4476 // publishFeature (with supported power source specified)
4477 //
4478 // Adds a new feature to the supported features dictionary
4479 //******************************************************************************
4480
4481 void
publishFeature(const char * feature,uint32_t supportedWhere,uint32_t * uniqueFeatureID)4482 IOPMrootDomain::publishFeature(
4483 const char *feature,
4484 uint32_t supportedWhere,
4485 uint32_t *uniqueFeatureID)
4486 {
4487 static uint16_t next_feature_id = 500;
4488
4489 OSSharedPtr<OSNumber> new_feature_data;
4490 OSNumber *existing_feature = NULL;
4491 OSArray *existing_feature_arr_raw = NULL;
4492 OSSharedPtr<OSArray> existing_feature_arr;
4493 OSObject *osObj = NULL;
4494 uint32_t feature_value = 0;
4495
4496 supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4497
4498 if (!supportedWhere) {
4499 // Feature isn't supported anywhere!
4500 return;
4501 }
4502
4503 if (next_feature_id > 5000) {
4504 // Far, far too many features!
4505 return;
4506 }
4507
4508 if (featuresDictLock) {
4509 IOLockLock(featuresDictLock);
4510 }
4511
4512 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4513 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4514 OSSharedPtr<OSDictionary> features;
4515
4516 // Create new features dict if necessary
4517 if (origFeatures) {
4518 features = OSDictionary::withDictionary(origFeatures);
4519 } else {
4520 features = OSDictionary::withCapacity(1);
4521 }
4522
4523 // Create OSNumber to track new feature
4524
4525 next_feature_id += 1;
4526 if (uniqueFeatureID) {
4527 // We don't really mind if the calling kext didn't give us a place
4528 // to stash their unique id. Many kexts don't plan to unload, and thus
4529 // have no need to remove themselves later.
4530 *uniqueFeatureID = next_feature_id;
4531 }
4532
4533 feature_value = (uint32_t)next_feature_id;
4534 feature_value <<= 16;
4535 feature_value += supportedWhere;
4536
4537 new_feature_data = OSNumber::withNumber(
4538 (unsigned long long)feature_value, 32);
4539
4540 // Does features object already exist?
4541 if ((osObj = features->getObject(feature))) {
4542 if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4543 // We need to create an OSArray to hold the now 2 elements.
4544 existing_feature_arr = OSArray::withObjects(
4545 (const OSObject **)&existing_feature, 1, 2);
4546 } else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4547 // Add object to existing array
4548 existing_feature_arr = OSArray::withArray(
4549 existing_feature_arr_raw,
4550 existing_feature_arr_raw->getCount() + 1);
4551 }
4552
4553 if (existing_feature_arr) {
4554 existing_feature_arr->setObject(new_feature_data.get());
4555 features->setObject(feature, existing_feature_arr.get());
4556 }
4557 } else {
4558 // The easy case: no previously existing features listed. We simply
4559 // set the OSNumber at key 'feature' and we're on our way.
4560 features->setObject(feature, new_feature_data.get());
4561 }
4562
4563 setProperty(kRootDomainSupportedFeatures, features.get());
4564
4565 if (featuresDictLock) {
4566 IOLockUnlock(featuresDictLock);
4567 }
4568
4569 // Notify EnergySaver and all those in user space so they might
4570 // re-populate their feature specific UI
4571 if (pmPowerStateQueue) {
4572 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4573 }
4574 }
4575
4576 //******************************************************************************
4577 // removePublishedFeature
4578 //
4579 // Removes previously published feature
4580 //******************************************************************************
4581
4582 IOReturn
removePublishedFeature(uint32_t removeFeatureID)4583 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4584 {
4585 IOReturn ret = kIOReturnError;
4586 uint32_t feature_value = 0;
4587 uint16_t feature_id = 0;
4588 bool madeAChange = false;
4589
4590 OSSymbol *dictKey = NULL;
4591 OSSharedPtr<OSCollectionIterator> dictIterator;
4592 OSArray *arrayMember = NULL;
4593 OSNumber *numberMember = NULL;
4594 OSObject *osObj = NULL;
4595 OSNumber *osNum = NULL;
4596 OSSharedPtr<OSArray> arrayMemberCopy;
4597
4598 if (kBadPMFeatureID == removeFeatureID) {
4599 return kIOReturnNotFound;
4600 }
4601
4602 if (featuresDictLock) {
4603 IOLockLock(featuresDictLock);
4604 }
4605
4606 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4607 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4608 OSSharedPtr<OSDictionary> features;
4609
4610 if (origFeatures) {
4611 // Any modifications to the dictionary are made to the copy to prevent
4612 // races & crashes with userland clients. Dictionary updated
4613 // automically later.
4614 features = OSDictionary::withDictionary(origFeatures);
4615 } else {
4616 features = NULL;
4617 ret = kIOReturnNotFound;
4618 goto exit;
4619 }
4620
4621 // We iterate 'features' dictionary looking for an entry tagged
4622 // with 'removeFeatureID'. If found, we remove it from our tracking
4623 // structures and notify the OS via a general interest message.
4624
4625 dictIterator = OSCollectionIterator::withCollection(features.get());
4626 if (!dictIterator) {
4627 goto exit;
4628 }
4629
4630 while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4631 osObj = features->getObject(dictKey);
4632
4633 // Each Feature is either tracked by an OSNumber
4634 if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4635 feature_value = numberMember->unsigned32BitValue();
4636 feature_id = (uint16_t)(feature_value >> 16);
4637
4638 if (feature_id == (uint16_t)removeFeatureID) {
4639 // Remove this node
4640 features->removeObject(dictKey);
4641 madeAChange = true;
4642 break;
4643 }
4644
4645 // Or tracked by an OSArray of OSNumbers
4646 } else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4647 unsigned int arrayCount = arrayMember->getCount();
4648
4649 for (unsigned int i = 0; i < arrayCount; i++) {
4650 osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4651 if (!osNum) {
4652 continue;
4653 }
4654
4655 feature_value = osNum->unsigned32BitValue();
4656 feature_id = (uint16_t)(feature_value >> 16);
4657
4658 if (feature_id == (uint16_t)removeFeatureID) {
4659 // Remove this node
4660 if (1 == arrayCount) {
4661 // If the array only contains one element, remove
4662 // the whole thing.
4663 features->removeObject(dictKey);
4664 } else {
4665 // Otherwise remove the element from a copy of the array.
4666 arrayMemberCopy = OSArray::withArray(arrayMember);
4667 if (arrayMemberCopy) {
4668 arrayMemberCopy->removeObject(i);
4669 features->setObject(dictKey, arrayMemberCopy.get());
4670 }
4671 }
4672
4673 madeAChange = true;
4674 break;
4675 }
4676 }
4677 }
4678 }
4679
4680 if (madeAChange) {
4681 ret = kIOReturnSuccess;
4682
4683 setProperty(kRootDomainSupportedFeatures, features.get());
4684
4685 // Notify EnergySaver and all those in user space so they might
4686 // re-populate their feature specific UI
4687 if (pmPowerStateQueue) {
4688 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4689 }
4690 } else {
4691 ret = kIOReturnNotFound;
4692 }
4693
4694 exit:
4695 if (featuresDictLock) {
4696 IOLockUnlock(featuresDictLock);
4697 }
4698 return ret;
4699 }
4700
4701 //******************************************************************************
4702 // publishPMSetting (private)
4703 //
4704 // Should only be called by PMSettingObject to publish a PM Setting as a
4705 // supported feature.
4706 //******************************************************************************
4707
4708 void
publishPMSetting(const OSSymbol * feature,uint32_t where,uint32_t * featureID)4709 IOPMrootDomain::publishPMSetting(
4710 const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4711 {
4712 if (noPublishPMSettings &&
4713 (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4714 // Setting found in noPublishPMSettings array
4715 *featureID = kBadPMFeatureID;
4716 return;
4717 }
4718
4719 publishFeature(
4720 feature->getCStringNoCopy(), where, featureID);
4721 }
4722
4723 //******************************************************************************
4724 // setPMSetting (private)
4725 //
4726 // Internal helper to relay PM settings changes from user space to individual
4727 // drivers. Should be called only by IOPMrootDomain::setProperties.
4728 //******************************************************************************
4729
4730 IOReturn
setPMSetting(const OSSymbol * type,OSObject * object)4731 IOPMrootDomain::setPMSetting(
4732 const OSSymbol *type,
4733 OSObject *object )
4734 {
4735 PMSettingCallEntry *entries = NULL;
4736 OSSharedPtr<OSArray> chosen;
4737 const OSArray *array;
4738 PMSettingObject *pmso;
4739 thread_t thisThread;
4740 int i, j, count, capacity;
4741 bool ok = false;
4742 IOReturn ret;
4743
4744 if (NULL == type) {
4745 return kIOReturnBadArgument;
4746 }
4747
4748 PMSETTING_LOCK();
4749
4750 // Update settings dict so changes are visible from copyPMSetting().
4751 fPMSettingsDict->setObject(type, object);
4752
4753 // Prep all PMSetting objects with the given 'type' for callout.
4754 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4755 if (!array || ((capacity = array->getCount()) == 0)) {
4756 goto unlock_exit;
4757 }
4758
4759 // Array to retain PMSetting objects targeted for callout.
4760 chosen = OSArray::withCapacity(capacity);
4761 if (!chosen) {
4762 goto unlock_exit; // error
4763 }
4764 entries = IONew(PMSettingCallEntry, capacity);
4765 if (!entries) {
4766 goto unlock_exit; // error
4767 }
4768 memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4769
4770 thisThread = current_thread();
4771
4772 for (i = 0, j = 0; i < capacity; i++) {
4773 pmso = (PMSettingObject *) array->getObject(i);
4774 if (pmso->disabled) {
4775 continue;
4776 }
4777 entries[j].thread = thisThread;
4778 queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4779 chosen->setObject(pmso);
4780 j++;
4781 }
4782 count = j;
4783 if (!count) {
4784 goto unlock_exit;
4785 }
4786
4787 PMSETTING_UNLOCK();
4788
4789 // Call each pmso in the chosen array.
4790 for (i = 0; i < count; i++) {
4791 pmso = (PMSettingObject *) chosen->getObject(i);
4792 ret = pmso->dispatchPMSetting(type, object);
4793 if (ret == kIOReturnSuccess) {
4794 // At least one setting handler was successful
4795 ok = true;
4796 #if DEVELOPMENT || DEBUG
4797 } else {
4798 // Log the handler and kext that failed
4799 OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4800 if (kextName) {
4801 DLOG("PMSetting(%s) error 0x%x from %s\n",
4802 type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4803 }
4804 #endif
4805 }
4806 }
4807
4808 PMSETTING_LOCK();
4809 for (i = 0; i < count; i++) {
4810 pmso = (PMSettingObject *) chosen->getObject(i);
4811 queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4812 if (pmso->waitThread) {
4813 PMSETTING_WAKEUP(pmso);
4814 }
4815 }
4816
4817 if (ok) {
4818 recordRTCAlarm(type, object);
4819 }
4820 unlock_exit:
4821 PMSETTING_UNLOCK();
4822
4823 if (entries) {
4824 IODelete(entries, PMSettingCallEntry, capacity);
4825 }
4826
4827 return kIOReturnSuccess;
4828 }
4829
4830 //******************************************************************************
4831 // copyPMSetting (public)
4832 //
4833 // Allows kexts to safely read setting values, without being subscribed to
4834 // notifications.
4835 //******************************************************************************
4836
4837 OSSharedPtr<OSObject>
copyPMSetting(OSSymbol * whichSetting)4838 IOPMrootDomain::copyPMSetting(
4839 OSSymbol *whichSetting)
4840 {
4841 OSSharedPtr<OSObject> obj;
4842
4843 if (!whichSetting) {
4844 return NULL;
4845 }
4846
4847 PMSETTING_LOCK();
4848 obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4849 PMSETTING_UNLOCK();
4850
4851 return obj;
4852 }
4853
4854 //******************************************************************************
4855 // registerPMSettingController (public)
4856 //
4857 // direct wrapper to registerPMSettingController with uint32_t power source arg
4858 //******************************************************************************
4859
4860 IOReturn
registerPMSettingController(const OSSymbol * settings[],IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4861 IOPMrootDomain::registerPMSettingController(
4862 const OSSymbol * settings[],
4863 IOPMSettingControllerCallback func,
4864 OSObject *target,
4865 uintptr_t refcon,
4866 OSObject **handle)
4867 {
4868 return registerPMSettingController(
4869 settings,
4870 (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4871 func, target, refcon, handle);
4872 }
4873
4874 //******************************************************************************
4875 // registerPMSettingController (public)
4876 //
4877 // Kexts may register for notifications when a particular setting is changed.
4878 // A list of settings is available in IOPM.h.
4879 // Arguments:
4880 // * settings - An OSArray containing OSSymbols. Caller should populate this
4881 // array with a list of settings caller wants notifications from.
4882 // * func - A C function callback of the type IOPMSettingControllerCallback
4883 // * target - caller may provide an OSObject *, which PM will pass as an
4884 // target to calls to "func"
4885 // * refcon - caller may provide an void *, which PM will pass as an
4886 // argument to calls to "func"
4887 // * handle - This is a return argument. We will populate this pointer upon
4888 // call success. Hold onto this and pass this argument to
4889 // IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4890 // Returns:
4891 // kIOReturnSuccess on success
4892 //******************************************************************************
4893
4894 IOReturn
registerPMSettingController(const OSSymbol * settings[],uint32_t supportedPowerSources,IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4895 IOPMrootDomain::registerPMSettingController(
4896 const OSSymbol * settings[],
4897 uint32_t supportedPowerSources,
4898 IOPMSettingControllerCallback func,
4899 OSObject *target,
4900 uintptr_t refcon,
4901 OSObject **handle)
4902 {
4903 PMSettingObject *pmso = NULL;
4904 OSObject *pmsh = NULL;
4905 int i;
4906
4907 if (NULL == settings ||
4908 NULL == func ||
4909 NULL == handle) {
4910 return kIOReturnBadArgument;
4911 }
4912
4913 pmso = PMSettingObject::pmSettingObject(
4914 (IOPMrootDomain *) this, func, target,
4915 refcon, supportedPowerSources, settings, &pmsh);
4916
4917 if (!pmso) {
4918 *handle = NULL;
4919 return kIOReturnInternalError;
4920 }
4921
4922 PMSETTING_LOCK();
4923 for (i = 0; settings[i]; i++) {
4924 OSSharedPtr<OSArray> newList;
4925 OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4926 if (!list) {
4927 // New array of callbacks for this setting
4928 newList = OSArray::withCapacity(1);
4929 settingsCallbacks->setObject(settings[i], newList.get());
4930 list = newList.get();
4931 }
4932
4933 // Add caller to the callback list
4934 list->setObject(pmso);
4935 }
4936 PMSETTING_UNLOCK();
4937
4938 // Return handle to the caller, the setting object is private.
4939 *handle = pmsh;
4940
4941 return kIOReturnSuccess;
4942 }
4943
4944 //******************************************************************************
4945 // deregisterPMSettingObject (private)
4946 //
4947 // Only called from PMSettingObject.
4948 //******************************************************************************
4949
4950 void
deregisterPMSettingObject(PMSettingObject * pmso)4951 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
4952 {
4953 thread_t thisThread = current_thread();
4954 PMSettingCallEntry *callEntry;
4955 OSSharedPtr<OSCollectionIterator> iter;
4956 OSSymbol *sym;
4957 OSArray *array;
4958 int index;
4959 bool wait;
4960
4961 PMSETTING_LOCK();
4962
4963 pmso->disabled = true;
4964
4965 // Wait for all callout threads to finish.
4966 do {
4967 wait = false;
4968 queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
4969 {
4970 if (callEntry->thread != thisThread) {
4971 wait = true;
4972 break;
4973 }
4974 }
4975 if (wait) {
4976 assert(NULL == pmso->waitThread);
4977 pmso->waitThread = thisThread;
4978 PMSETTING_WAIT(pmso);
4979 pmso->waitThread = NULL;
4980 }
4981 } while (wait);
4982
4983 // Search each PM settings array in the kernel.
4984 iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
4985 if (iter) {
4986 while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
4987 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
4988 index = array->getNextIndexOfObject(pmso, 0);
4989 if (-1 != index) {
4990 array->removeObject(index);
4991 }
4992 }
4993 }
4994
4995 PMSETTING_UNLOCK();
4996
4997 pmso->release();
4998 }
4999
5000 //******************************************************************************
5001 // informCPUStateChange
5002 //
5003 // Call into PM CPU code so that CPU power savings may dynamically adjust for
5004 // running on battery, with the lid closed, etc.
5005 //
5006 // informCPUStateChange is a no-op on non x86 systems
5007 // only x86 has explicit support in the IntelCPUPowerManagement kext
5008 //******************************************************************************
5009
5010 void
informCPUStateChange(uint32_t type,uint32_t value)5011 IOPMrootDomain::informCPUStateChange(
5012 uint32_t type,
5013 uint32_t value )
5014 {
5015 #if defined(__i386__) || defined(__x86_64__)
5016
5017 pmioctlVariableInfo_t varInfoStruct;
5018 int pmCPUret = 0;
5019 const char *varNameStr = NULL;
5020 int32_t *varIndex = NULL;
5021
5022 if (kInformAC == type) {
5023 varNameStr = kIOPMRootDomainBatPowerCString;
5024 varIndex = &idxPMCPULimitedPower;
5025 } else if (kInformLid == type) {
5026 varNameStr = kIOPMRootDomainLidCloseCString;
5027 varIndex = &idxPMCPUClamshell;
5028 } else {
5029 return;
5030 }
5031
5032 // Set the new value!
5033 // pmCPUControl will assign us a new ID if one doesn't exist yet
5034 bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
5035 varInfoStruct.varID = *varIndex;
5036 varInfoStruct.varType = vBool;
5037 varInfoStruct.varInitValue = value;
5038 varInfoStruct.varCurValue = value;
5039 strlcpy((char *)varInfoStruct.varName,
5040 (const char *)varNameStr,
5041 sizeof(varInfoStruct.varName));
5042
5043 // Set!
5044 pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
5045
5046 // pmCPU only assigns numerical id's when a new varName is specified
5047 if ((0 == pmCPUret)
5048 && (*varIndex == kCPUUnknownIndex)) {
5049 // pmCPUControl has assigned us a new variable ID.
5050 // Let's re-read the structure we just SET to learn that ID.
5051 pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5052
5053 if (0 == pmCPUret) {
5054 // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5055 *varIndex = varInfoStruct.varID;
5056 }
5057 }
5058
5059 return;
5060
5061 #endif /* __i386__ || __x86_64__ */
5062 }
5063
5064 // MARK: -
5065 // MARK: Deep Sleep Policy
5066
5067 #if HIBERNATION
5068
5069 //******************************************************************************
5070 // evaluateSystemSleepPolicy
5071 //******************************************************************************
5072
5073 #define kIOPlatformSystemSleepPolicyKey "IOPlatformSystemSleepPolicy"
5074
5075 // Sleep flags
5076 enum {
5077 kIOPMSleepFlagHibernate = 0x00000001,
5078 kIOPMSleepFlagSleepTimerEnable = 0x00000002
5079 };
5080
5081 struct IOPMSystemSleepPolicyEntry {
5082 uint32_t factorMask;
5083 uint32_t factorBits;
5084 uint32_t sleepFlags;
5085 uint32_t wakeEvents;
5086 } __attribute__((packed));
5087
5088 struct IOPMSystemSleepPolicyTable {
5089 uint32_t signature;
5090 uint16_t version;
5091 uint16_t entryCount;
5092 IOPMSystemSleepPolicyEntry entries[];
5093 } __attribute__((packed));
5094
5095 enum {
5096 kIOPMSleepAttributeHibernateSetup = 0x00000001,
5097 kIOPMSleepAttributeHibernateSleep = 0x00000002
5098 };
5099
5100 static uint32_t
getSleepTypeAttributes(uint32_t sleepType)5101 getSleepTypeAttributes( uint32_t sleepType )
5102 {
5103 static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5104 {
5105 /* invalid */ 0,
5106 /* abort */ 0,
5107 /* normal */ 0,
5108 /* safesleep */ kIOPMSleepAttributeHibernateSetup,
5109 /* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5110 /* standby */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5111 /* poweroff */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5112 /* deepidle */ 0
5113 };
5114
5115 if (sleepType >= kIOPMSleepTypeLast) {
5116 return 0;
5117 }
5118
5119 return sleepTypeAttributes[sleepType];
5120 }
5121
5122 bool
evaluateSystemSleepPolicy(IOPMSystemSleepParameters * params,int sleepPhase,uint32_t * hibMode)5123 IOPMrootDomain::evaluateSystemSleepPolicy(
5124 IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5125 {
5126 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5127
5128 static const IONamedValue factorValues[] = {
5129 SLEEP_FACTOR( SleepTimerWake ),
5130 SLEEP_FACTOR( LidOpen ),
5131 SLEEP_FACTOR( ACPower ),
5132 SLEEP_FACTOR( BatteryLow ),
5133 SLEEP_FACTOR( StandbyNoDelay ),
5134 SLEEP_FACTOR( StandbyForced ),
5135 SLEEP_FACTOR( StandbyDisabled ),
5136 SLEEP_FACTOR( USBExternalDevice ),
5137 SLEEP_FACTOR( BluetoothHIDDevice ),
5138 SLEEP_FACTOR( ExternalMediaMounted ),
5139 SLEEP_FACTOR( ThunderboltDevice ),
5140 SLEEP_FACTOR( RTCAlarmScheduled ),
5141 SLEEP_FACTOR( MagicPacketWakeEnabled ),
5142 SLEEP_FACTOR( HibernateForced ),
5143 SLEEP_FACTOR( AutoPowerOffDisabled ),
5144 SLEEP_FACTOR( AutoPowerOffForced ),
5145 SLEEP_FACTOR( ExternalDisplay ),
5146 SLEEP_FACTOR( NetworkKeepAliveActive ),
5147 SLEEP_FACTOR( LocalUserActivity ),
5148 SLEEP_FACTOR( HibernateFailed ),
5149 SLEEP_FACTOR( ThermalWarning ),
5150 SLEEP_FACTOR( DisplayCaptured ),
5151 { 0, NULL }
5152 };
5153
5154 const IOPMSystemSleepPolicyTable * pt;
5155 OSSharedPtr<OSObject> prop;
5156 OSData * policyData;
5157 uint64_t currentFactors = 0;
5158 char currentFactorsBuf[512];
5159 uint32_t standbyDelay = 0;
5160 uint32_t powerOffDelay = 0;
5161 uint32_t powerOffTimer = 0;
5162 uint32_t standbyTimer = 0;
5163 uint32_t mismatch;
5164 bool standbyEnabled;
5165 bool powerOffEnabled;
5166 bool found = false;
5167
5168 // Get platform's sleep policy table
5169 if (!gSleepPolicyHandler) {
5170 prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5171 if (!prop) {
5172 goto done;
5173 }
5174 }
5175
5176 // Fetch additional settings
5177 standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5178 && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5179 powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5180 && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5181 if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5182 powerOffTimer = powerOffDelay;
5183 }
5184 if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5185 standbyTimer = standbyDelay;
5186 }
5187
5188 DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5189 sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5190 powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5191
5192 currentFactorsBuf[0] = 0;
5193 // pmset level overrides
5194 if ((*hibMode & kIOHibernateModeOn) == 0) {
5195 if (!gSleepPolicyHandler) {
5196 standbyEnabled = false;
5197 powerOffEnabled = false;
5198 }
5199 } else if (!(*hibMode & kIOHibernateModeSleep)) {
5200 // Force hibernate (i.e. mode 25)
5201 // If standby is enabled, force standy.
5202 // If poweroff is enabled, force poweroff.
5203 if (standbyEnabled) {
5204 currentFactors |= kIOPMSleepFactorStandbyForced;
5205 } else if (powerOffEnabled) {
5206 currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5207 } else {
5208 currentFactors |= kIOPMSleepFactorHibernateForced;
5209 }
5210 }
5211
5212 // Current factors based on environment and assertions
5213 if (sleepTimerMaintenance) {
5214 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5215 }
5216 if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5217 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5218 }
5219 if (!clamshellClosed) {
5220 currentFactors |= kIOPMSleepFactorLidOpen;
5221 }
5222 if (acAdaptorConnected) {
5223 currentFactors |= kIOPMSleepFactorACPower;
5224 }
5225 if (lowBatteryCondition) {
5226 hibernateMode = 0;
5227 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5228 if ((hibernateMode & kIOHibernateModeOn) == 0) {
5229 DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5230 } else {
5231 currentFactors |= kIOPMSleepFactorBatteryLow;
5232 }
5233 }
5234 if (!standbyDelay || !standbyTimer) {
5235 currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5236 }
5237 if (standbyNixed || !standbyEnabled) {
5238 currentFactors |= kIOPMSleepFactorStandbyDisabled;
5239 }
5240 if (resetTimers) {
5241 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5242 currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5243 }
5244 if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5245 kIOPMDriverAssertionLevelOff) {
5246 currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5247 }
5248 if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5249 kIOPMDriverAssertionLevelOff) {
5250 currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5251 }
5252 if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5253 kIOPMDriverAssertionLevelOff) {
5254 currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5255 }
5256 if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5257 kIOPMDriverAssertionLevelOff) {
5258 currentFactors |= kIOPMSleepFactorThunderboltDevice;
5259 }
5260 if (_scheduledAlarmMask != 0) {
5261 currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5262 }
5263 if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5264 kIOPMDriverAssertionLevelOff) {
5265 currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5266 }
5267 #define TCPKEEPALIVE 1
5268 #if TCPKEEPALIVE
5269 if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5270 kIOPMDriverAssertionLevelOff) {
5271 currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5272 }
5273 #endif
5274 if (!powerOffEnabled) {
5275 currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5276 }
5277 if (desktopMode) {
5278 currentFactors |= kIOPMSleepFactorExternalDisplay;
5279 }
5280 if (userWasActive) {
5281 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5282 }
5283 if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5284 currentFactors |= kIOPMSleepFactorHibernateFailed;
5285 }
5286 if (thermalWarningState) {
5287 currentFactors |= kIOPMSleepFactorThermalWarning;
5288 }
5289
5290 for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5291 uint32_t factor = 1 << factorBit;
5292 if (factor & currentFactors) {
5293 strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5294 strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5295 }
5296 }
5297 DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5298
5299 if (gSleepPolicyHandler) {
5300 uint32_t savedHibernateMode;
5301 IOReturn result;
5302
5303 if (!gSleepPolicyVars) {
5304 gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5305 }
5306 gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5307 gSleepPolicyVars->version = kIOPMSystemSleepPolicyVersion;
5308 gSleepPolicyVars->currentCapability = _currentCapability;
5309 gSleepPolicyVars->highestCapability = _highestCapability;
5310 gSleepPolicyVars->sleepFactors = currentFactors;
5311 gSleepPolicyVars->sleepReason = lastSleepReason;
5312 gSleepPolicyVars->sleepPhase = sleepPhase;
5313 gSleepPolicyVars->standbyDelay = standbyDelay;
5314 gSleepPolicyVars->standbyTimer = standbyTimer;
5315 gSleepPolicyVars->poweroffDelay = powerOffDelay;
5316 gSleepPolicyVars->scheduledAlarms = _scheduledAlarmMask | _userScheduledAlarmMask;
5317 gSleepPolicyVars->poweroffTimer = powerOffTimer;
5318
5319 if (kIOPMSleepPhase0 == sleepPhase) {
5320 // preserve hibernateMode
5321 savedHibernateMode = gSleepPolicyVars->hibernateMode;
5322 gSleepPolicyVars->hibernateMode = *hibMode;
5323 } else if (kIOPMSleepPhase1 == sleepPhase) {
5324 // use original hibernateMode for phase2
5325 gSleepPolicyVars->hibernateMode = *hibMode;
5326 }
5327
5328 result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5329
5330 if (kIOPMSleepPhase0 == sleepPhase) {
5331 // restore hibernateMode
5332 gSleepPolicyVars->hibernateMode = savedHibernateMode;
5333 }
5334
5335 if ((result != kIOReturnSuccess) ||
5336 (kIOPMSleepTypeInvalid == params->sleepType) ||
5337 (params->sleepType >= kIOPMSleepTypeLast) ||
5338 (kIOPMSystemSleepParametersVersion != params->version)) {
5339 MSG("sleep policy handler error\n");
5340 goto done;
5341 }
5342
5343 if ((getSleepTypeAttributes(params->sleepType) &
5344 kIOPMSleepAttributeHibernateSetup) &&
5345 ((*hibMode & kIOHibernateModeOn) == 0)) {
5346 *hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5347 }
5348
5349 DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5350 params->version, params->sleepType, params->sleepFlags,
5351 params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5352 found = true;
5353 goto done;
5354 }
5355
5356 // Policy table is meaningless without standby enabled
5357 if (!standbyEnabled) {
5358 goto done;
5359 }
5360
5361 // Validate the sleep policy table
5362 policyData = OSDynamicCast(OSData, prop.get());
5363 if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5364 goto done;
5365 }
5366
5367 pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5368 if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5369 (pt->version != 1) || (0 == pt->entryCount)) {
5370 goto done;
5371 }
5372
5373 if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5374 (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5375 goto done;
5376 }
5377
5378 for (uint32_t i = 0; i < pt->entryCount; i++) {
5379 const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5380 mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5381
5382 DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5383 entry->factorMask, entry->factorBits,
5384 entry->sleepFlags, entry->wakeEvents, mismatch);
5385 if (mismatch) {
5386 continue;
5387 }
5388
5389 DLOG("^ found match\n");
5390 found = true;
5391
5392 params->version = kIOPMSystemSleepParametersVersion;
5393 params->reserved1 = 1;
5394 if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5395 params->sleepType = kIOPMSleepTypeStandby;
5396 } else {
5397 params->sleepType = kIOPMSleepTypeNormalSleep;
5398 }
5399
5400 params->ecWakeEvents = entry->wakeEvents;
5401 if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5402 if (kIOPMSleepPhase2 == sleepPhase) {
5403 clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5404
5405 if (!_standbyTimerResetSeconds ||
5406 (now_secs <= _standbyTimerResetSeconds)) {
5407 // Reset standby timer adjustment
5408 _standbyTimerResetSeconds = now_secs;
5409 DLOG("standby delay %u, reset %u\n",
5410 standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5411 } else if (standbyDelay) {
5412 // Shorten the standby delay timer
5413 clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5414 if (standbyDelay > elapsed) {
5415 standbyDelay -= elapsed;
5416 } else {
5417 standbyDelay = 1; // must be > 0
5418 }
5419 DLOG("standby delay %u, elapsed %u\n",
5420 standbyDelay, (uint32_t) elapsed);
5421 }
5422 }
5423 params->ecWakeTimer = standbyDelay;
5424 } else if (kIOPMSleepPhase2 == sleepPhase) {
5425 // A sleep that does not enable the sleep timer will reset
5426 // the standby delay adjustment.
5427 _standbyTimerResetSeconds = 0;
5428 }
5429 break;
5430 }
5431
5432 done:
5433 return found;
5434 }
5435
5436 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5437
5438 void
evaluateSystemSleepPolicyEarly(void)5439 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5440 {
5441 // Evaluate early (priority interest phase), before drivers sleep.
5442
5443 DLOG("%s\n", __FUNCTION__);
5444 removeProperty(kIOPMSystemSleepParametersKey);
5445
5446 // Full wake resets the standby timer delay adjustment
5447 if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5448 _standbyTimerResetSeconds = 0;
5449 }
5450
5451 hibernateDisabled = false;
5452 hibernateMode = 0;
5453 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5454
5455 // Save for late evaluation if sleep is aborted
5456 bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5457
5458 if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5459 &hibernateMode)) {
5460 if (!hibernateRetry &&
5461 ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5462 kIOPMSleepAttributeHibernateSetup) == 0)) {
5463 // skip hibernate setup
5464 hibernateDisabled = true;
5465 }
5466 }
5467
5468 // Publish IOPMSystemSleepType
5469 uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5470 if (sleepType == kIOPMSleepTypeInvalid) {
5471 // no sleep policy
5472 sleepType = kIOPMSleepTypeNormalSleep;
5473 if (hibernateMode & kIOHibernateModeOn) {
5474 sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5475 kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5476 }
5477 } else if ((sleepType == kIOPMSleepTypeStandby) &&
5478 (gEarlySystemSleepParams.ecPoweroffTimer)) {
5479 // report the lowest possible sleep state
5480 sleepType = kIOPMSleepTypePowerOff;
5481 }
5482
5483 setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5484 }
5485
5486 void
evaluateSystemSleepPolicyFinal(void)5487 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5488 {
5489 IOPMSystemSleepParameters params;
5490 OSSharedPtr<OSData> paramsData;
5491 bool wakeNow;
5492 // Evaluate sleep policy after sleeping drivers but before platform sleep.
5493
5494 DLOG("%s\n", __FUNCTION__);
5495
5496 bzero(¶ms, sizeof(params));
5497 wakeNow = false;
5498 if (evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase2, &hibernateMode)) {
5499 if ((kIOPMSleepTypeStandby == params.sleepType)
5500 && gIOHibernateStandbyDisabled && gSleepPolicyVars
5501 && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5502 & gSleepPolicyVars->sleepFactors))) {
5503 standbyNixed = true;
5504 wakeNow = true;
5505 }
5506 if (wakeNow
5507 || ((hibernateDisabled || hibernateAborted) &&
5508 (getSleepTypeAttributes(params.sleepType) &
5509 kIOPMSleepAttributeHibernateSetup))) {
5510 // Final evaluation picked a state requiring hibernation,
5511 // but hibernate isn't going to proceed. Arm a short sleep using
5512 // the early non-hibernate sleep parameters.
5513 bcopy(&gEarlySystemSleepParams, ¶ms, sizeof(params));
5514 params.sleepType = kIOPMSleepTypeAbortedSleep;
5515 params.ecWakeTimer = 1;
5516 if (standbyNixed) {
5517 resetTimers = true;
5518 } else {
5519 // Set hibernateRetry flag to force hibernate setup on the
5520 // next sleep.
5521 hibernateRetry = true;
5522 }
5523 DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5524 params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5525 } else {
5526 hibernateRetry = false;
5527 }
5528
5529 if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5530 resetTimers = false;
5531 }
5532
5533 paramsData = OSData::withValue(params);
5534 if (paramsData) {
5535 setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5536 }
5537
5538 if (getSleepTypeAttributes(params.sleepType) &
5539 kIOPMSleepAttributeHibernateSleep) {
5540 // Disable sleep to force hibernation
5541 gIOHibernateMode &= ~kIOHibernateModeSleep;
5542 }
5543 }
5544 }
5545
5546 bool
getHibernateSettings(uint32_t * hibernateModePtr,uint32_t * hibernateFreeRatio,uint32_t * hibernateFreeTime)5547 IOPMrootDomain::getHibernateSettings(
5548 uint32_t * hibernateModePtr,
5549 uint32_t * hibernateFreeRatio,
5550 uint32_t * hibernateFreeTime )
5551 {
5552 // Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5553 // has updated the hibernateDisabled flag.
5554
5555 bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5556 getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5557 getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5558 if (hibernateDisabled) {
5559 *hibernateModePtr = 0;
5560 } else if (gSleepPolicyHandler) {
5561 *hibernateModePtr = hibernateMode;
5562 }
5563 DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5564 return ok;
5565 }
5566
5567 bool
getSleepOption(const char * key,uint32_t * option)5568 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5569 {
5570 OSSharedPtr<OSObject> optionsProp;
5571 OSDictionary * optionsDict;
5572 OSSharedPtr<OSObject> obj;
5573 OSNumber * num;
5574 bool ok = false;
5575
5576 optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5577 optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5578
5579 if (optionsDict) {
5580 obj.reset(optionsDict->getObject(key), OSRetain);
5581 }
5582 if (!obj) {
5583 obj = copyProperty(key);
5584 }
5585 if (obj) {
5586 if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5587 *option = num->unsigned32BitValue();
5588 ok = true;
5589 } else if (OSDynamicCast(OSBoolean, obj.get())) {
5590 *option = (obj == kOSBooleanTrue) ? 1 : 0;
5591 ok = true;
5592 }
5593 }
5594
5595 return ok;
5596 }
5597 #endif /* HIBERNATION */
5598
5599 IOReturn
getSystemSleepType(uint32_t * sleepType,uint32_t * standbyTimer)5600 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5601 {
5602 #if HIBERNATION
5603 IOPMSystemSleepParameters params;
5604 uint32_t hibMode = 0;
5605 bool ok;
5606
5607 if (gIOPMWorkLoop->inGate() == false) {
5608 IOReturn ret = gIOPMWorkLoop->runAction(
5609 OSMemberFunctionCast(IOWorkLoop::Action, this,
5610 &IOPMrootDomain::getSystemSleepType),
5611 (OSObject *) this,
5612 (void *) sleepType, (void *) standbyTimer);
5613 return ret;
5614 }
5615
5616 getSleepOption(kIOHibernateModeKey, &hibMode);
5617 bzero(¶ms, sizeof(params));
5618
5619 ok = evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase0, &hibMode);
5620 if (ok) {
5621 *sleepType = params.sleepType;
5622 if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5623 !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5624 DLOG("Standby delay is not set\n");
5625 *standbyTimer = 0;
5626 }
5627 return kIOReturnSuccess;
5628 }
5629 #endif
5630
5631 return kIOReturnUnsupported;
5632 }
5633
5634 // MARK: -
5635 // MARK: Shutdown and Restart
5636
5637 //******************************************************************************
5638 // handlePlatformHaltRestart
5639 //
5640 //******************************************************************************
5641
5642 // Phases while performing shutdown/restart
5643 typedef enum {
5644 kNotifyDone = 0x00,
5645 kNotifyPriorityClients = 0x10,
5646 kNotifyPowerPlaneDrivers = 0x20,
5647 kNotifyHaltRestartAction = 0x30,
5648 kQuiescePM = 0x40,
5649 } shutdownPhase_t;
5650
5651
5652 struct HaltRestartApplierContext {
5653 IOPMrootDomain * RootDomain;
5654 unsigned long PowerState;
5655 IOPMPowerFlags PowerFlags;
5656 UInt32 MessageType;
5657 UInt32 Counter;
5658 const char * LogString;
5659 shutdownPhase_t phase;
5660
5661 IOServiceInterestHandler handler;
5662 } gHaltRestartCtx;
5663
5664 const char *
shutdownPhase2String(shutdownPhase_t phase)5665 shutdownPhase2String(shutdownPhase_t phase)
5666 {
5667 switch (phase) {
5668 case kNotifyDone:
5669 return "Notifications completed";
5670 case kNotifyPriorityClients:
5671 return "Notifying priority clients";
5672 case kNotifyPowerPlaneDrivers:
5673 return "Notifying power plane drivers";
5674 case kNotifyHaltRestartAction:
5675 return "Notifying HaltRestart action handlers";
5676 case kQuiescePM:
5677 return "Quiescing PM";
5678 default:
5679 return "Unknown";
5680 }
5681 }
5682
5683 static void
platformHaltRestartApplier(OSObject * object,void * context)5684 platformHaltRestartApplier( OSObject * object, void * context )
5685 {
5686 IOPowerStateChangeNotification notify;
5687 HaltRestartApplierContext * ctx;
5688 AbsoluteTime startTime, elapsedTime;
5689 uint32_t deltaTime;
5690
5691 ctx = (HaltRestartApplierContext *) context;
5692
5693 _IOServiceInterestNotifier * notifier;
5694 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5695 memset(¬ify, 0, sizeof(notify));
5696 notify.powerRef = (void *)(uintptr_t)ctx->Counter;
5697 notify.returnValue = 0;
5698 notify.stateNumber = ctx->PowerState;
5699 notify.stateFlags = ctx->PowerFlags;
5700
5701 if (notifier) {
5702 ctx->handler = notifier->handler;
5703 }
5704
5705 clock_get_uptime(&startTime);
5706 ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)¬ify );
5707 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5708
5709 if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5710 LOG("%s handler %p took %u ms\n",
5711 ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5712 halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5713 }
5714
5715 ctx->handler = NULL;
5716 ctx->Counter++;
5717 }
5718
5719 static void
quiescePowerTreeCallback(void * target,void * param)5720 quiescePowerTreeCallback( void * target, void * param )
5721 {
5722 IOLockLock(gPMHaltLock);
5723 gPMQuiesced = true;
5724 thread_wakeup(param);
5725 IOLockUnlock(gPMHaltLock);
5726 }
5727
5728 void
handlePlatformHaltRestart(UInt32 pe_type)5729 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5730 {
5731 AbsoluteTime startTime, elapsedTime;
5732 uint32_t deltaTime;
5733 bool nvramSync = false;
5734
5735 memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5736 gHaltRestartCtx.RootDomain = this;
5737
5738 clock_get_uptime(&startTime);
5739 switch (pe_type) {
5740 case kPEHaltCPU:
5741 case kPEUPSDelayHaltCPU:
5742 gHaltRestartCtx.PowerState = OFF_STATE;
5743 gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5744 gHaltRestartCtx.LogString = "PowerOff";
5745 nvramSync = true;
5746 break;
5747
5748 case kPERestartCPU:
5749 gHaltRestartCtx.PowerState = RESTART_STATE;
5750 gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5751 gHaltRestartCtx.LogString = "Restart";
5752 nvramSync = true;
5753 break;
5754
5755 case kPEPagingOff:
5756 gHaltRestartCtx.PowerState = ON_STATE;
5757 gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5758 gHaltRestartCtx.LogString = "PagingOff";
5759 IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5760 #if HIBERNATION
5761 IOHibernateSystemRestart();
5762 #endif
5763 break;
5764
5765 default:
5766 return;
5767 }
5768
5769 if (nvramSync) {
5770 PESyncNVRAM();
5771 }
5772
5773 gHaltRestartCtx.phase = kNotifyPriorityClients;
5774 // Notify legacy clients
5775 applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5776
5777 // For normal shutdown, turn off File Server Mode.
5778 if (kPEHaltCPU == pe_type) {
5779 OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5780 OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5781 if (setting && num) {
5782 setPMSetting(setting.get(), num.get());
5783 }
5784 }
5785
5786 if (kPEPagingOff != pe_type) {
5787 gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5788 // Notify in power tree order
5789 notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5790 }
5791
5792 gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5793 #if defined(XNU_TARGET_OS_OSX)
5794 IOCPURunPlatformHaltRestartActions(pe_type);
5795 #else /* !defined(XNU_TARGET_OS_OSX) */
5796 if (kPEPagingOff != pe_type) {
5797 IOCPURunPlatformHaltRestartActions(pe_type);
5798 }
5799 #endif /* !defined(XNU_TARGET_OS_OSX) */
5800
5801 // Wait for PM to quiesce
5802 if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5803 gHaltRestartCtx.phase = kQuiescePM;
5804 AbsoluteTime quiesceTime = mach_absolute_time();
5805
5806 IOLockLock(gPMHaltLock);
5807 gPMQuiesced = false;
5808 if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5809 kIOReturnSuccess) {
5810 while (!gPMQuiesced) {
5811 IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5812 }
5813 }
5814 IOLockUnlock(gPMHaltLock);
5815 deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5816 DLOG("PM quiesce took %u ms\n", deltaTime);
5817 halt_log_enter("Quiesce", NULL, elapsedTime);
5818 }
5819 gHaltRestartCtx.phase = kNotifyDone;
5820
5821 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5822 LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5823
5824 halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5825
5826 deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5827 LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5828
5829 if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5830 printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5831 }
5832
5833 checkShutdownTimeout();
5834 }
5835
5836 bool
checkShutdownTimeout()5837 IOPMrootDomain::checkShutdownTimeout()
5838 {
5839 AbsoluteTime elapsedTime;
5840 uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5841
5842 if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5843 return true;
5844 }
5845 return false;
5846 }
5847
5848 void
panicWithShutdownLog(uint32_t timeoutInMs)5849 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5850 {
5851 if (gHaltLog) {
5852 if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5853 halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5854 }
5855 panic("%s timed out in phase '%s'. Total %d ms:%s",
5856 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5857 } else {
5858 panic("%s timed out in phase \'%s\'. Total %d ms",
5859 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5860 }
5861 }
5862
5863 //******************************************************************************
5864 // shutdownSystem
5865 //
5866 //******************************************************************************
5867
5868 IOReturn
shutdownSystem(void)5869 IOPMrootDomain::shutdownSystem( void )
5870 {
5871 return kIOReturnUnsupported;
5872 }
5873
5874 //******************************************************************************
5875 // restartSystem
5876 //
5877 //******************************************************************************
5878
5879 IOReturn
restartSystem(void)5880 IOPMrootDomain::restartSystem( void )
5881 {
5882 return kIOReturnUnsupported;
5883 }
5884
5885 // MARK: -
5886 // MARK: System Capability
5887
5888 //******************************************************************************
5889 // tagPowerPlaneService
5890 //
5891 // Running on PM work loop thread.
5892 //******************************************************************************
5893
5894 void
tagPowerPlaneService(IOService * service,IOPMActions * actions,IOPMPowerStateIndex maxPowerState)5895 IOPMrootDomain::tagPowerPlaneService(
5896 IOService * service,
5897 IOPMActions * actions,
5898 IOPMPowerStateIndex maxPowerState )
5899 {
5900 uint32_t flags = 0;
5901
5902 memset(actions, 0, sizeof(*actions));
5903 actions->target = this;
5904
5905 if (service == this) {
5906 actions->actionPowerChangeStart =
5907 OSMemberFunctionCast(
5908 IOPMActionPowerChangeStart, this,
5909 &IOPMrootDomain::handleOurPowerChangeStart);
5910
5911 actions->actionPowerChangeDone =
5912 OSMemberFunctionCast(
5913 IOPMActionPowerChangeDone, this,
5914 &IOPMrootDomain::handleOurPowerChangeDone);
5915
5916 actions->actionPowerChangeOverride =
5917 OSMemberFunctionCast(
5918 IOPMActionPowerChangeOverride, this,
5919 &IOPMrootDomain::overrideOurPowerChange);
5920 return;
5921 }
5922
5923 #if DISPLAY_WRANGLER_PRESENT
5924 if (NULL != service->metaCast("IODisplayWrangler")) {
5925 // XXX should this really retain?
5926 wrangler.reset(service, OSRetain);
5927 wrangler->registerInterest(gIOGeneralInterest,
5928 &displayWranglerNotification, this, NULL);
5929
5930 // found the display wrangler, check for any display assertions already created
5931 if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5932 DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5933 wrangler->setIgnoreIdleTimer( true );
5934 }
5935 flags |= kPMActionsFlagIsDisplayWrangler;
5936 }
5937 #endif /* DISPLAY_WRANGLER_PRESENT */
5938
5939 if (service->propertyExists("IOPMStrictTreeOrder")) {
5940 flags |= kPMActionsFlagIsGraphicsDriver;
5941 }
5942 if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5943 flags |= kPMActionsFlagIsAudioDriver;
5944 }
5945
5946 // Find the power connection object that is a child of the PCI host
5947 // bridge, and has a graphics/audio device attached below. Mark the
5948 // power branch for delayed child notifications.
5949
5950 if (flags) {
5951 IORegistryEntry * child = service;
5952 IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
5953
5954 while (child != this) {
5955 if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
5956 // Skip delaying notifications and clamping power on external graphics and audio devices.
5957 DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
5958 flags = 0;
5959 break;
5960 }
5961 if ((parent == pciHostBridgeDriver) ||
5962 (parent == this)) {
5963 if (OSDynamicCast(IOPowerConnection, child)) {
5964 IOPowerConnection * conn = (IOPowerConnection *) child;
5965 conn->delayChildNotification = true;
5966 DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
5967 }
5968 break;
5969 }
5970 child = parent;
5971 parent = child->getParentEntry(gIOPowerPlane);
5972 }
5973 }
5974
5975 OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
5976 if (prop) {
5977 OSNumber * num = OSDynamicCast(OSNumber, prop.get());
5978 if (num) {
5979 actions->darkWakePowerState = num->unsigned32BitValue();
5980 if (actions->darkWakePowerState < maxPowerState) {
5981 flags |= kPMActionsFlagHasDarkWakePowerState;
5982 }
5983 }
5984 }
5985
5986
5987 if (flags) {
5988 DLOG("%s tag flags %x\n", service->getName(), flags);
5989 actions->flags |= flags;
5990 actions->actionPowerChangeOverride =
5991 OSMemberFunctionCast(
5992 IOPMActionPowerChangeOverride, this,
5993 &IOPMrootDomain::overridePowerChangeForService);
5994
5995 if (flags & kPMActionsFlagIsDisplayWrangler) {
5996 actions->actionActivityTickle =
5997 OSMemberFunctionCast(
5998 IOPMActionActivityTickle, this,
5999 &IOPMrootDomain::handleActivityTickleForDisplayWrangler);
6000
6001 actions->actionUpdatePowerClient =
6002 OSMemberFunctionCast(
6003 IOPMActionUpdatePowerClient, this,
6004 &IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
6005 }
6006 return;
6007 }
6008
6009 // Locate the first PCI host bridge for PMTrace.
6010 if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
6011 IOService * provider = service->getProvider();
6012 if (OSDynamicCast(IOPlatformDevice, provider) &&
6013 provider->inPlane(gIODTPlane)) {
6014 pciHostBridgeDevice.reset(provider, OSNoRetain);
6015 pciHostBridgeDriver.reset(service, OSNoRetain);
6016 DLOG("PMTrace found PCI host bridge %s->%s\n",
6017 provider->getName(), service->getName());
6018 }
6019 }
6020
6021 // Tag top-level PCI devices. The order of PMinit() call does not
6022 // change across boots and is used as the PCI bit number.
6023 if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
6024 // Would prefer to check built-in property, but tagPowerPlaneService()
6025 // is called before pciDevice->registerService().
6026 IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
6027 if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
6028 int bit = pmTracer->recordTopLevelPCIDevice( service );
6029 if (bit >= 0) {
6030 // Save the assigned bit for fast lookup.
6031 actions->flags |= (bit & kPMActionsPCIBitNumberMask);
6032
6033 actions->actionPowerChangeStart =
6034 OSMemberFunctionCast(
6035 IOPMActionPowerChangeStart, this,
6036 &IOPMrootDomain::handlePowerChangeStartForPCIDevice);
6037
6038 actions->actionPowerChangeDone =
6039 OSMemberFunctionCast(
6040 IOPMActionPowerChangeDone, this,
6041 &IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
6042 }
6043 }
6044 }
6045 }
6046
6047 //******************************************************************************
6048 // PM actions for root domain
6049 //******************************************************************************
6050
6051 void
overrideOurPowerChange(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6052 IOPMrootDomain::overrideOurPowerChange(
6053 IOService * service,
6054 IOPMActions * actions,
6055 const IOPMRequest * request,
6056 IOPMPowerStateIndex * inOutPowerState,
6057 IOPMPowerChangeFlags * inOutChangeFlags )
6058 {
6059 uint32_t changeFlags = *inOutChangeFlags;
6060 uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6061 uint32_t currentPowerState = (uint32_t) getPowerState();
6062
6063 if (request->getTag() == 0) {
6064 // Set a tag for any request that originates from IOServicePM
6065 (const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6066 }
6067
6068 DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6069 getPowerStateString(currentPowerState),
6070 getPowerStateString(desiredPowerState),
6071 _currentCapability, changeFlags,
6072 request->getTag());
6073
6074
6075 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6076 /*
6077 * ASBM send lowBattery notifications every 1 second until the device
6078 * enters hibernation. This queues up multiple sleep requests.
6079 * After the device wakes from hibernation, none of these previously
6080 * queued sleep requests are valid.
6081 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6082 * and is cleared at the very last point in sleep.
6083 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6084 * lowBatteryCondition is invalid
6085 */
6086 if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6087 if (!lowBatteryCondition) {
6088 DLOG("Duplicate lowBattery sleep");
6089 *inOutChangeFlags |= kIOPMNotDone;
6090 return;
6091 }
6092 }
6093 #endif
6094
6095 if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6096 // Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6097 *inOutChangeFlags |= kIOPMNotDone;
6098 return;
6099 }
6100
6101 if (changeFlags & kIOPMParentInitiated) {
6102 // Root parent is permanently pegged at max power,
6103 // a parent initiated power change is unexpected.
6104 *inOutChangeFlags |= kIOPMNotDone;
6105 return;
6106 }
6107
6108 if (desiredPowerState < currentPowerState) {
6109 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6110 // Root domain is dropping power state from ON->SLEEP.
6111 // If system is in full wake, first enter dark wake by
6112 // converting the power drop to a capability change.
6113 // Once in dark wake, transition to sleep state ASAP.
6114
6115 darkWakeToSleepASAP = true;
6116
6117 // Drop graphics and audio capability
6118 _desiredCapability &= ~(
6119 kIOPMSystemCapabilityGraphics |
6120 kIOPMSystemCapabilityAudio);
6121
6122 // Convert to capability change (ON->ON)
6123 *inOutPowerState = getRUN_STATE();
6124 *inOutChangeFlags |= kIOPMSynchronize;
6125
6126 // Revert device desire from SLEEP to ON
6127 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6128 } else {
6129 // System is already in dark wake, ok to drop power state.
6130 // Broadcast root power down to entire tree.
6131 *inOutChangeFlags |= kIOPMRootChangeDown;
6132 }
6133 } else if (desiredPowerState > currentPowerState) {
6134 if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6135 // Broadcast power up when waking from sleep, but not for the
6136 // initial power change at boot by checking for cpu capability.
6137 *inOutChangeFlags |= kIOPMRootChangeUp;
6138 }
6139 }
6140 }
6141
6142 void
handleOurPowerChangeStart(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex newPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6143 IOPMrootDomain::handleOurPowerChangeStart(
6144 IOService * service,
6145 IOPMActions * actions,
6146 const IOPMRequest * request,
6147 IOPMPowerStateIndex newPowerState,
6148 IOPMPowerChangeFlags * inOutChangeFlags )
6149 {
6150 IOPMRequestTag requestTag = request->getTag();
6151 IOPMRequestTag sleepReason;
6152
6153 uint32_t changeFlags = *inOutChangeFlags;
6154 uint32_t currentPowerState = (uint32_t) getPowerState();
6155 bool publishSleepReason = false;
6156
6157 // Check if request has a valid sleep reason
6158 sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6159 if (sleepReason < kIOPMSleepReasonClamshell) {
6160 sleepReason = kIOPMSleepReasonIdle;
6161 }
6162
6163 _systemTransitionType = kSystemTransitionNone;
6164 _systemMessageClientMask = 0;
6165 capabilityLoss = false;
6166 toldPowerdCapWillChange = false;
6167
6168 // Emergency notifications may arrive after the initial sleep request
6169 // has been queued. Override the sleep reason so powerd and others can
6170 // treat this as an emergency sleep.
6171 if (lowBatteryCondition) {
6172 sleepReason = kIOPMSleepReasonLowPower;
6173 } else if (thermalEmergencyState) {
6174 sleepReason = kIOPMSleepReasonThermalEmergency;
6175 }
6176
6177 // 1. Explicit capability change.
6178 if (changeFlags & kIOPMSynchronize) {
6179 if (newPowerState == ON_STATE) {
6180 if (changeFlags & kIOPMSyncNoChildNotify) {
6181 _systemTransitionType = kSystemTransitionNewCapClient;
6182 } else {
6183 _systemTransitionType = kSystemTransitionCapability;
6184 }
6185 }
6186 }
6187 // 2. Going to sleep (cancellation still possible).
6188 else if (newPowerState < currentPowerState) {
6189 _systemTransitionType = kSystemTransitionSleep;
6190 }
6191 // 3. Woke from (idle or demand) sleep.
6192 else if (!systemBooting &&
6193 (changeFlags & kIOPMSelfInitiated) &&
6194 (newPowerState > currentPowerState)) {
6195 _systemTransitionType = kSystemTransitionWake;
6196 _desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6197
6198 // Early exit from dark wake to full (e.g. LID open)
6199 if (kFullWakeReasonNone != fullWakeReason) {
6200 _desiredCapability |= (
6201 kIOPMSystemCapabilityGraphics |
6202 kIOPMSystemCapabilityAudio);
6203
6204 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6205 if (fullWakeReason == kFullWakeReasonLocalUser) {
6206 darkWakeExit = true;
6207 darkWakeToSleepASAP = false;
6208 setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6209 kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6210 }
6211 #endif
6212 }
6213 #if HIBERNATION
6214 IOHibernateSetWakeCapabilities(_desiredCapability);
6215 #endif
6216 }
6217
6218 // Update pending wake capability at the beginning of every
6219 // state transition (including synchronize). This will become
6220 // the current capability at the end of the transition.
6221
6222 if (kSystemTransitionSleep == _systemTransitionType) {
6223 _pendingCapability = 0;
6224 capabilityLoss = true;
6225 } else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6226 _pendingCapability = _desiredCapability |
6227 kIOPMSystemCapabilityCPU |
6228 kIOPMSystemCapabilityNetwork;
6229
6230 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6231 _pendingCapability |= kIOPMSystemCapabilityAudio;
6232 }
6233
6234 if ((kSystemTransitionCapability == _systemTransitionType) &&
6235 (_pendingCapability == _currentCapability)) {
6236 // Cancel the PM state change.
6237 _systemTransitionType = kSystemTransitionNone;
6238 *inOutChangeFlags |= kIOPMNotDone;
6239 }
6240 if (__builtin_popcount(_pendingCapability) <
6241 __builtin_popcount(_currentCapability)) {
6242 capabilityLoss = true;
6243 }
6244 }
6245
6246 // 1. Capability change.
6247 if (kSystemTransitionCapability == _systemTransitionType) {
6248 // Dark to Full transition.
6249 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6250 tracePoint( kIOPMTracePointDarkWakeExit );
6251
6252 #if defined(XNU_TARGET_OS_OSX)
6253 // rdar://problem/65627936
6254 // When a dark->full wake promotion is scheduled before an ON->SLEEP
6255 // power state drop, invalidate any request to drop power state already
6256 // in the queue, including the override variant, unless full wake cannot
6257 // be sustained. Any power state drop queued after this SustainFullWake
6258 // request will not be affected.
6259 if (checkSystemCanSustainFullWake()) {
6260 changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6261 }
6262 #endif
6263
6264 willEnterFullWake();
6265 }
6266
6267 // Full to Dark transition.
6268 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6269 // Clear previous stats
6270 IOLockLock(pmStatsLock);
6271 if (pmStatsAppResponses) {
6272 pmStatsAppResponses = OSArray::withCapacity(5);
6273 }
6274 IOLockUnlock(pmStatsLock);
6275
6276 tracePoint( kIOPMTracePointDarkWakeEntry );
6277 *inOutChangeFlags |= kIOPMSyncTellPowerDown;
6278 _systemMessageClientMask = kSystemMessageClientPowerd |
6279 kSystemMessageClientLegacyApp;
6280
6281 // rdar://15971327
6282 // Prevent user active transitions before notifying clients
6283 // that system will sleep.
6284 preventTransitionToUserActive(true);
6285
6286 IOService::setAdvisoryTickleEnable( false );
6287
6288 // Publish the sleep reason for full to dark wake
6289 publishSleepReason = true;
6290 lastSleepReason = fullToDarkReason = sleepReason;
6291
6292 // Publish a UUID for the Sleep --> Wake cycle
6293 handlePublishSleepWakeUUID(true);
6294 if (sleepDelaysReport) {
6295 clock_get_uptime(&ts_sleepStart);
6296 DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6297 }
6298
6299 darkWakeExit = false;
6300 }
6301 }
6302 // 2. System sleep.
6303 else if (kSystemTransitionSleep == _systemTransitionType) {
6304 // Beginning of a system sleep transition.
6305 // Cancellation is still possible.
6306 tracePoint( kIOPMTracePointSleepStarted );
6307
6308 _systemMessageClientMask = kSystemMessageClientAll;
6309 if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6310 _systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6311 }
6312 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6313 // Kernel priority clients are only notified on the initial
6314 // transition to full wake, so don't notify them unless system
6315 // has gained graphics capability since the last system wake.
6316 _systemMessageClientMask &= ~kSystemMessageClientKernel;
6317 } else {
6318 // System was in full wake, but the downwards power transition is driven
6319 // by a request that originates from IOServicePM, so it isn't tagged with
6320 // a valid system sleep reason.
6321 if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6322 // Publish the same reason for full to dark
6323 sleepReason = fullToDarkReason;
6324 }
6325 }
6326 #if HIBERNATION
6327 gIOHibernateState = 0;
6328 #endif
6329
6330 // Record the reason for dark wake back to sleep
6331 // System may not have ever achieved full wake
6332
6333 publishSleepReason = true;
6334 lastSleepReason = sleepReason;
6335 if (sleepDelaysReport) {
6336 clock_get_uptime(&ts_sleepStart);
6337 DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6338 }
6339 }
6340 // 3. System wake.
6341 else if (kSystemTransitionWake == _systemTransitionType) {
6342 tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6343 // Clear stats about sleep
6344
6345 if (AOT_STATE == newPowerState) {
6346 _pendingCapability = 0;
6347 }
6348
6349 if (AOT_STATE == currentPowerState) {
6350 // Wake events are no longer accepted after waking to AOT_STATE.
6351 // Re-enable wake event acceptance to append wake events claimed
6352 // during the AOT to ON_STATE transition.
6353 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6354 }
6355
6356 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6357 willEnterFullWake();
6358 }
6359 }
6360
6361 // The only location where the sleep reason is published. At this point
6362 // sleep can still be cancelled, but sleep reason should be published
6363 // early for logging purposes.
6364
6365 if (publishSleepReason) {
6366 static const char * IOPMSleepReasons[] =
6367 {
6368 kIOPMClamshellSleepKey,
6369 kIOPMPowerButtonSleepKey,
6370 kIOPMSoftwareSleepKey,
6371 kIOPMOSSwitchHibernationKey,
6372 kIOPMIdleSleepKey,
6373 kIOPMLowPowerSleepKey,
6374 kIOPMThermalEmergencySleepKey,
6375 kIOPMMaintenanceSleepKey,
6376 kIOPMSleepServiceExitKey,
6377 kIOPMDarkWakeThermalEmergencyKey,
6378 kIOPMNotificationWakeExitKey
6379 };
6380
6381 // Record sleep cause in IORegistry
6382 uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6383 if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6384 DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6385 setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6386 }
6387 }
6388
6389 if ((kSystemTransitionNone != _systemTransitionType) &&
6390 (kSystemTransitionNewCapClient != _systemTransitionType)) {
6391 _systemStateGeneration++;
6392 systemDarkWake = false;
6393
6394 DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6395 getPowerStateString(currentPowerState),
6396 getPowerStateString((uint32_t) newPowerState),
6397 _currentCapability, _pendingCapability,
6398 *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6399 requestTag);
6400 }
6401
6402 if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6403 panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6404 }
6405 if (_aotNow && (ON_STATE == newPowerState)) {
6406 WAKEEVENT_LOCK();
6407 aotShouldExit(false, true);
6408 WAKEEVENT_UNLOCK();
6409 aotExit(false);
6410 }
6411 }
6412
6413 void
handleOurPowerChangeDone(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex oldPowerState,IOPMPowerChangeFlags changeFlags)6414 IOPMrootDomain::handleOurPowerChangeDone(
6415 IOService * service,
6416 IOPMActions * actions,
6417 const IOPMRequest * request,
6418 IOPMPowerStateIndex oldPowerState,
6419 IOPMPowerChangeFlags changeFlags )
6420 {
6421 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6422 _systemTransitionType = kSystemTransitionNone;
6423 return;
6424 }
6425
6426 if (_systemTransitionType != kSystemTransitionNone) {
6427 uint32_t currentPowerState = (uint32_t) getPowerState();
6428
6429 if (changeFlags & kIOPMNotDone) {
6430 // Power down was cancelled or vetoed.
6431 _pendingCapability = _currentCapability;
6432 lastSleepReason = 0;
6433
6434 // When sleep is cancelled or reverted, don't report
6435 // the target (lower) power state as the previous state.
6436 oldPowerState = currentPowerState;
6437
6438 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6439 CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6440 #if defined(XNU_TARGET_OS_OSX)
6441 pmPowerStateQueue->submitPowerEvent(
6442 kPowerEventPolicyStimulus,
6443 (void *) kStimulusDarkWakeReentry,
6444 _systemStateGeneration );
6445 #else /* !defined(XNU_TARGET_OS_OSX) */
6446 // On embedded, there are no factors that can prolong a
6447 // "darkWake" when a power down is vetoed. We need to
6448 // promote to "fullWake" at least once so that factors
6449 // that prevent idle sleep can assert themselves if required
6450 pmPowerStateQueue->submitPowerEvent(
6451 kPowerEventPolicyStimulus,
6452 (void *) kStimulusDarkWakeActivityTickle);
6453 #endif /* !defined(XNU_TARGET_OS_OSX) */
6454 }
6455
6456 // Revert device desire to max.
6457 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6458 } else {
6459 // Send message on dark wake to full wake promotion.
6460 // tellChangeUp() handles the normal SLEEP->ON case.
6461
6462 if (kSystemTransitionCapability == _systemTransitionType) {
6463 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6464 lastSleepReason = 0; // stop logging wrangler tickles
6465 tellClients(kIOMessageSystemHasPoweredOn);
6466 }
6467 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6468 // Going dark, reset full wake state
6469 // userIsActive will be cleared by wrangler powering down
6470 fullWakeReason = kFullWakeReasonNone;
6471
6472 if (ts_sleepStart) {
6473 clock_get_uptime(&wake2DarkwakeDelay);
6474 SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6475 DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6476 ts_sleepStart = 0;
6477 }
6478 }
6479 }
6480
6481 // Reset state after exiting from dark wake.
6482
6483 if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6484 CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6485 darkWakeMaintenance = false;
6486 darkWakeToSleepASAP = false;
6487 pciCantSleepValid = false;
6488 darkWakeSleepService = false;
6489
6490 if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6491 // Remove the influence of display power assertion
6492 // before next system wake.
6493 if (wrangler) {
6494 wrangler->changePowerStateForRootDomain(
6495 kWranglerPowerStateMin );
6496 }
6497 removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6498 }
6499 }
6500
6501 // Entered dark mode.
6502
6503 if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6504 (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6505 // Queue an evaluation of whether to remain in dark wake,
6506 // and for how long. This serves the purpose of draining
6507 // any assertions from the queue.
6508
6509 pmPowerStateQueue->submitPowerEvent(
6510 kPowerEventPolicyStimulus,
6511 (void *) kStimulusDarkWakeEntry,
6512 _systemStateGeneration );
6513 }
6514 }
6515
6516 DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6517 getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6518 _currentCapability, _pendingCapability,
6519 changeFlags, _systemStateGeneration, _systemMessageClientMask,
6520 request->getTag());
6521
6522 if ((currentPowerState == ON_STATE) && pmAssertions) {
6523 pmAssertions->reportCPUBitAccounting();
6524 }
6525
6526 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6527 displayWakeCnt++;
6528 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6529 if (clamshellExists && fullWakeThreadCall) {
6530 AbsoluteTime deadline;
6531 clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6532 thread_call_enter_delayed(fullWakeThreadCall, deadline);
6533 }
6534 #endif
6535 } else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6536 darkWakeCnt++;
6537 }
6538
6539 // Update current system capability.
6540 if (_currentCapability != _pendingCapability) {
6541 _currentCapability = _pendingCapability;
6542 }
6543
6544 // Update highest system capability.
6545
6546 _highestCapability |= _currentCapability;
6547
6548 if (darkWakePostTickle &&
6549 (kSystemTransitionWake == _systemTransitionType) &&
6550 (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6551 kDarkWakeFlagPromotionLate) {
6552 darkWakePostTickle = false;
6553 reportUserInput();
6554 } else if (darkWakeExit) {
6555 requestFullWake( kFullWakeReasonLocalUser );
6556 }
6557
6558 // Reset tracepoint at completion of capability change,
6559 // completion of wake transition, and aborted sleep transition.
6560
6561 if ((_systemTransitionType == kSystemTransitionCapability) ||
6562 (_systemTransitionType == kSystemTransitionWake) ||
6563 ((_systemTransitionType == kSystemTransitionSleep) &&
6564 (changeFlags & kIOPMNotDone))) {
6565 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6566 tracePoint( kIOPMTracePointSystemUp );
6567 }
6568
6569 _systemTransitionType = kSystemTransitionNone;
6570 _systemMessageClientMask = 0;
6571 toldPowerdCapWillChange = false;
6572
6573 darkWakeLogClamp = false;
6574
6575 if (lowBatteryCondition) {
6576 privateSleepSystem(kIOPMSleepReasonLowPower);
6577 } else if (thermalEmergencyState) {
6578 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6579 } else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6580 // Request for full wake is removed while system is waking up to full wake
6581 DLOG("DisplayOn fullwake request is removed\n");
6582 handleSetDisplayPowerOn(false);
6583 }
6584
6585 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6586 pmPowerStateQueue->submitPowerEvent(
6587 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6588 }
6589 }
6590 }
6591
6592 //******************************************************************************
6593 // PM actions for graphics and audio.
6594 //******************************************************************************
6595
6596 void
overridePowerChangeForService(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6597 IOPMrootDomain::overridePowerChangeForService(
6598 IOService * service,
6599 IOPMActions * actions,
6600 const IOPMRequest * request,
6601 IOPMPowerStateIndex * inOutPowerState,
6602 IOPMPowerChangeFlags * inOutChangeFlags )
6603 {
6604 uint32_t powerState = (uint32_t) *inOutPowerState;
6605 uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6606 const uint32_t actionFlags = actions->flags;
6607
6608 if (kSystemTransitionNone == _systemTransitionType) {
6609 // Not in midst of a system transition.
6610 // Do not set kPMActionsStatePowerClamped.
6611 } else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6612 bool enableClamp = false;
6613
6614 // For most drivers, enable the clamp during ON->Dark transition
6615 // which has the kIOPMSynchronize flag set in changeFlags.
6616 if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6617 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6618 (changeFlags & kIOPMSynchronize)) {
6619 enableClamp = true;
6620 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6621 ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6622 ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6623 (changeFlags & kIOPMSynchronize)) {
6624 enableClamp = true;
6625 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6626 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6627 (changeFlags & kIOPMSynchronize)) {
6628 enableClamp = true;
6629 } else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6630 (_systemTransitionType == kSystemTransitionSleep)) {
6631 // For graphics drivers, clamp power when entering
6632 // system sleep. Not when dropping to dark wake.
6633 enableClamp = true;
6634 }
6635
6636 if (enableClamp) {
6637 actions->state |= kPMActionsStatePowerClamped;
6638 DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6639 service->getName(), service->getRegistryEntryID(),
6640 _pendingCapability, powerState, changeFlags);
6641 }
6642 } else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6643 bool disableClamp = false;
6644
6645 if ((actionFlags & (
6646 kPMActionsFlagIsDisplayWrangler |
6647 kPMActionsFlagIsGraphicsDriver)) &&
6648 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6649 disableClamp = true;
6650 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6651 (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6652 disableClamp = true;
6653 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6654 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6655 disableClamp = true;
6656 }
6657
6658 if (disableClamp) {
6659 actions->state &= ~kPMActionsStatePowerClamped;
6660 DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6661 service->getName(), service->getRegistryEntryID(),
6662 _pendingCapability, powerState, changeFlags);
6663 }
6664 }
6665
6666 if (actions->state & kPMActionsStatePowerClamped) {
6667 uint32_t maxPowerState = 0;
6668
6669 // Determine the max power state allowed when clamp is enabled
6670 if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6671 // Parent intiated power state changes
6672 if ((service->getPowerState() > maxPowerState) &&
6673 (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6674 maxPowerState++;
6675
6676 // Remove lingering effects of any tickle before entering
6677 // dark wake. It will take a new tickle to return to full
6678 // wake, so the existing tickle state is useless.
6679
6680 if (changeFlags & kIOPMDomainDidChange) {
6681 *inOutChangeFlags |= kIOPMExpireIdleTimer;
6682 }
6683 } else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6684 maxPowerState++;
6685 } else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6686 maxPowerState = actions->darkWakePowerState;
6687 }
6688 } else {
6689 // Deny all self-initiated changes when power is limited.
6690 // Wrangler tickle should never defeat the limiter.
6691 maxPowerState = service->getPowerState();
6692 }
6693
6694 if (powerState > maxPowerState) {
6695 DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6696 service->getName(), service->getRegistryEntryID(),
6697 powerState, maxPowerState, changeFlags);
6698 *inOutPowerState = maxPowerState;
6699
6700 if (darkWakePostTickle &&
6701 (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6702 (changeFlags & kIOPMDomainWillChange) &&
6703 ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6704 kDarkWakeFlagPromotionEarly)) {
6705 darkWakePostTickle = false;
6706 reportUserInput();
6707 }
6708 }
6709
6710 if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6711 if (darkWakeLogClamp) {
6712 AbsoluteTime now;
6713 uint64_t nsec;
6714
6715 clock_get_uptime(&now);
6716 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6717 absolutetime_to_nanoseconds(now, &nsec);
6718 DLOG("dark wake power clamped after %u ms\n",
6719 ((int)((nsec) / NSEC_PER_MSEC)));
6720 }
6721 darkWakePowerClamped = true;
6722 }
6723 }
6724 }
6725
6726 void
handleActivityTickleForDisplayWrangler(IOService * service,IOPMActions * actions)6727 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6728 IOService * service,
6729 IOPMActions * actions )
6730 {
6731 #if DISPLAY_WRANGLER_PRESENT
6732 // Warning: Not running in PM work loop context - don't modify state !!!
6733 // Trap tickle directed to IODisplayWrangler while running with graphics
6734 // capability suppressed.
6735
6736 assert(service == wrangler);
6737
6738 clock_get_uptime(&userActivityTime);
6739 bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6740 || (lastSleepReason == kIOPMSleepReasonMaintenance)
6741 || (lastSleepReason == kIOPMSleepReasonSoftware));
6742 if (aborting) {
6743 userActivityCount++;
6744 DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6745 userActivityCount, lastSleepReason);
6746 }
6747
6748 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6749 DLOG("display wrangler tickled\n");
6750 if (kIOLogPMRootDomain & gIOKitDebug) {
6751 OSReportWithBacktrace("Dark wake display tickle");
6752 }
6753 if (pmPowerStateQueue) {
6754 pmPowerStateQueue->submitPowerEvent(
6755 kPowerEventPolicyStimulus,
6756 (void *) kStimulusDarkWakeActivityTickle,
6757 true /* set wake type */ );
6758 }
6759 }
6760 #endif /* DISPLAY_WRANGLER_PRESENT */
6761 }
6762
6763 void
handleUpdatePowerClientForDisplayWrangler(IOService * service,IOPMActions * actions,const OSSymbol * powerClient,IOPMPowerStateIndex oldPowerState,IOPMPowerStateIndex newPowerState)6764 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6765 IOService * service,
6766 IOPMActions * actions,
6767 const OSSymbol * powerClient,
6768 IOPMPowerStateIndex oldPowerState,
6769 IOPMPowerStateIndex newPowerState )
6770 {
6771 #if DISPLAY_WRANGLER_PRESENT
6772 assert(service == wrangler);
6773
6774 // This function implements half of the user active detection
6775 // by monitoring changes to the display wrangler's device desire.
6776 //
6777 // User becomes active when either:
6778 // 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6779 // in max power state. This desire change in absence of a power state
6780 // change is detected within. This handles the case when user becomes
6781 // active while the display is already lit by setDisplayPowerOn().
6782 //
6783 // 2. Power state change to max, and DeviceDesire is also at max.
6784 // Handled by displayWranglerNotification().
6785 //
6786 // User becomes inactive when DeviceDesire drops to sleep state or below.
6787
6788 DLOG("wrangler %s (ps %u, %u->%u)\n",
6789 powerClient->getCStringNoCopy(),
6790 (uint32_t) service->getPowerState(),
6791 (uint32_t) oldPowerState, (uint32_t) newPowerState);
6792
6793 if (powerClient == gIOPMPowerClientDevice) {
6794 if ((newPowerState > oldPowerState) &&
6795 (newPowerState == kWranglerPowerStateMax) &&
6796 (service->getPowerState() == kWranglerPowerStateMax)) {
6797 evaluatePolicy( kStimulusEnterUserActiveState );
6798 } else if ((newPowerState < oldPowerState) &&
6799 (newPowerState <= kWranglerPowerStateSleep)) {
6800 evaluatePolicy( kStimulusLeaveUserActiveState );
6801 }
6802 }
6803
6804 if (newPowerState <= kWranglerPowerStateSleep) {
6805 evaluatePolicy( kStimulusDisplayWranglerSleep );
6806 } else if (newPowerState == kWranglerPowerStateMax) {
6807 evaluatePolicy( kStimulusDisplayWranglerWake );
6808 }
6809 #endif /* DISPLAY_WRANGLER_PRESENT */
6810 }
6811
6812 //******************************************************************************
6813 // User active state management
6814 //******************************************************************************
6815
6816 void
preventTransitionToUserActive(bool prevent)6817 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6818 {
6819 #if DISPLAY_WRANGLER_PRESENT
6820 _preventUserActive = prevent;
6821 if (wrangler && !_preventUserActive) {
6822 // Allowing transition to user active, but the wrangler may have
6823 // already powered ON in case of sleep cancel/revert. Poll the
6824 // same conditions checked for in displayWranglerNotification()
6825 // to bring the user active state up to date.
6826
6827 if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6828 (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6829 kWranglerPowerStateMax)) {
6830 evaluatePolicy( kStimulusEnterUserActiveState );
6831 }
6832 }
6833 #endif /* DISPLAY_WRANGLER_PRESENT */
6834 }
6835
6836 //******************************************************************************
6837 // Approve usage of delayed child notification by PM.
6838 //******************************************************************************
6839
6840 bool
shouldDelayChildNotification(IOService * service)6841 IOPMrootDomain::shouldDelayChildNotification(
6842 IOService * service )
6843 {
6844 if ((kFullWakeReasonNone == fullWakeReason) &&
6845 (kSystemTransitionWake == _systemTransitionType)) {
6846 DLOG("%s: delay child notify\n", service->getName());
6847 return true;
6848 }
6849 return false;
6850 }
6851
6852 //******************************************************************************
6853 // PM actions for PCI device.
6854 //******************************************************************************
6855
6856 void
handlePowerChangeStartForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags * inOutChangeFlags)6857 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6858 IOService * service,
6859 IOPMActions * actions,
6860 const IOPMRequest * request,
6861 IOPMPowerStateIndex powerState,
6862 IOPMPowerChangeFlags * inOutChangeFlags )
6863 {
6864 pmTracer->tracePCIPowerChange(
6865 PMTraceWorker::kPowerChangeStart,
6866 service, *inOutChangeFlags,
6867 (actions->flags & kPMActionsPCIBitNumberMask));
6868 }
6869
6870 void
handlePowerChangeDoneForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags changeFlags)6871 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6872 IOService * service,
6873 IOPMActions * actions,
6874 const IOPMRequest * request,
6875 IOPMPowerStateIndex powerState,
6876 IOPMPowerChangeFlags changeFlags )
6877 {
6878 pmTracer->tracePCIPowerChange(
6879 PMTraceWorker::kPowerChangeCompleted,
6880 service, changeFlags,
6881 (actions->flags & kPMActionsPCIBitNumberMask));
6882 }
6883
6884 //******************************************************************************
6885 // registerInterest
6886 //
6887 // Override IOService::registerInterest() for root domain clients.
6888 //******************************************************************************
6889
6890 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6891 {
6892 friend class IOPMrootDomain;
6893 OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6894
6895 protected:
6896 uint32_t ackTimeoutCnt;
6897 uint32_t msgType; // Last type seen by the message filter
6898 uint32_t lastSleepWakeMsgType;
6899 uint32_t msgIndex;
6900 uint32_t maxMsgDelayMS;
6901 uint32_t maxAckDelayMS;
6902 uint64_t msgAbsTime;
6903 uint64_t uuid0;
6904 uint64_t uuid1;
6905 OSSharedPtr<const OSSymbol> identifier;
6906 OSSharedPtr<const OSSymbol> clientName;
6907 };
6908
OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier,_IOServiceInterestNotifier)6909 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
6910
6911 OSSharedPtr<IONotifier>
6912 IOPMrootDomain::registerInterest(
6913 const OSSymbol * typeOfInterest,
6914 IOServiceInterestHandler handler,
6915 void * target, void * ref )
6916 {
6917 IOPMServiceInterestNotifier* notifier;
6918 bool isSystemCapabilityClient;
6919 bool isKernelCapabilityClient;
6920 IOReturn rc = kIOReturnError;
6921
6922 isSystemCapabilityClient = typeOfInterest &&
6923 typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
6924
6925 isKernelCapabilityClient = typeOfInterest &&
6926 typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
6927
6928 if (isSystemCapabilityClient) {
6929 typeOfInterest = gIOAppPowerStateInterest;
6930 }
6931
6932 notifier = new IOPMServiceInterestNotifier;
6933 if (!notifier) {
6934 return NULL;
6935 }
6936
6937 if (notifier->init()) {
6938 rc = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
6939 }
6940 if (rc != kIOReturnSuccess) {
6941 OSSafeReleaseNULL(notifier);
6942 return NULL;
6943 }
6944
6945 notifier->ackTimeoutCnt = 0;
6946
6947 if (pmPowerStateQueue) {
6948 if (isSystemCapabilityClient) {
6949 notifier->retain();
6950 if (pmPowerStateQueue->submitPowerEvent(
6951 kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
6952 notifier->release();
6953 }
6954 }
6955
6956 if (isKernelCapabilityClient) {
6957 notifier->retain();
6958 if (pmPowerStateQueue->submitPowerEvent(
6959 kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
6960 notifier->release();
6961 }
6962 }
6963 }
6964
6965 OSSharedPtr<OSData> data;
6966 uint8_t *uuid = NULL;
6967 OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
6968 if (kext) {
6969 data = kext->copyUUID();
6970 }
6971 if (data && (data->getLength() == sizeof(uuid_t))) {
6972 uuid = (uint8_t *)(data->getBytesNoCopy());
6973
6974 notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
6975 ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
6976 ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
6977 notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
6978 ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
6979 ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
6980
6981 notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
6982 }
6983 return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
6984 }
6985
6986 //******************************************************************************
6987 // systemMessageFilter
6988 //
6989 //******************************************************************************
6990
6991 bool
systemMessageFilter(void * object,void * arg1,void * arg2,void * arg3)6992 IOPMrootDomain::systemMessageFilter(
6993 void * object, void * arg1, void * arg2, void * arg3 )
6994 {
6995 const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
6996 bool isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
6997 bool isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
6998 bool isCapClient = false;
6999 bool allow = false;
7000 OSBoolean **waitForReply = (typeof(waitForReply))arg3;
7001 IOPMServiceInterestNotifier *notifier;
7002
7003 notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
7004
7005 do {
7006 // When powerd and kernel priority clients register capability interest,
7007 // the power tree is sync'ed to inform those clients about the current
7008 // system capability. Only allow capability change messages during sync.
7009 if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
7010 (!isCapMsg || !_joinedCapabilityClients ||
7011 !_joinedCapabilityClients->containsObject((OSObject *) object))) {
7012 break;
7013 }
7014
7015 // Capability change message for powerd and kernel clients
7016 if (isCapMsg) {
7017 // Kernel priority clients
7018 if ((context->notifyType == kNotifyPriority) ||
7019 (context->notifyType == kNotifyCapabilityChangePriority)) {
7020 isCapClient = true;
7021 }
7022
7023 // powerd will maintain two client registrations with root domain.
7024 // isCapPowerd will be TRUE for any message targeting the powerd
7025 // exclusive (capability change) interest registration.
7026 if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
7027 isCapClient = true;
7028 }
7029 }
7030
7031 if (isCapClient) {
7032 IOPMSystemCapabilityChangeParameters * capArgs =
7033 (IOPMSystemCapabilityChangeParameters *) arg2;
7034
7035 if (kSystemTransitionNewCapClient == _systemTransitionType) {
7036 capArgs->fromCapabilities = 0;
7037 capArgs->toCapabilities = _currentCapability;
7038 capArgs->changeFlags = 0;
7039 } else {
7040 capArgs->fromCapabilities = _currentCapability;
7041 capArgs->toCapabilities = _pendingCapability;
7042
7043 if (context->isPreChange) {
7044 capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
7045 } else {
7046 capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
7047 }
7048
7049 if (isCapPowerd && context->isPreChange) {
7050 toldPowerdCapWillChange = true;
7051 }
7052 }
7053
7054 // App level capability change messages must only go to powerd.
7055 // Wait for response post-change if capabilitiy is increasing.
7056 // Wait for response pre-change if capability is decreasing.
7057
7058 if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7059 ((capabilityLoss && context->isPreChange) ||
7060 (!capabilityLoss && !context->isPreChange))) {
7061 *waitForReply = kOSBooleanTrue;
7062 }
7063
7064 allow = true;
7065 break;
7066 }
7067
7068 // powerd will always receive CanSystemSleep, even for a demand sleep.
7069 // It will also have a final chance to veto sleep after all clients
7070 // have responded to SystemWillSleep
7071
7072 if ((kIOMessageCanSystemSleep == context->messageType) ||
7073 (kIOMessageSystemWillNotSleep == context->messageType)) {
7074 if (isCapPowerd) {
7075 allow = true;
7076 break;
7077 }
7078
7079 // Demand sleep, don't ask apps for permission
7080 if (context->changeFlags & kIOPMSkipAskPowerDown) {
7081 break;
7082 }
7083 }
7084
7085 if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7086 if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7087 (fullToDarkReason == kIOPMSleepReasonIdle)) {
7088 allow = true;
7089 }
7090 break;
7091 }
7092
7093 // Drop capability change messages for legacy clients.
7094 // Drop legacy system sleep messages for powerd capability interest.
7095 if (isCapMsg || isCapPowerd) {
7096 break;
7097 }
7098
7099 // Not a capability change message.
7100 // Perform message filtering based on _systemMessageClientMask.
7101
7102 if ((context->notifyType == kNotifyApps) &&
7103 (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7104 if (!notifier) {
7105 break;
7106 }
7107
7108 if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7109 (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7110 break; // drop any duplicate WillPowerOn for AOT devices
7111 }
7112
7113 allow = true;
7114
7115 if (waitForReply) {
7116 if (notifier->ackTimeoutCnt >= 3) {
7117 *waitForReply = kOSBooleanFalse;
7118 } else {
7119 *waitForReply = kOSBooleanTrue;
7120 }
7121 }
7122 } else if ((context->notifyType == kNotifyPriority) &&
7123 (_systemMessageClientMask & kSystemMessageClientKernel)) {
7124 allow = true;
7125 }
7126
7127 // Check sleep/wake message ordering
7128 if (allow) {
7129 if (context->messageType == kIOMessageSystemWillSleep ||
7130 context->messageType == kIOMessageSystemWillPowerOn ||
7131 context->messageType == kIOMessageSystemHasPoweredOn) {
7132 notifier->lastSleepWakeMsgType = context->messageType;
7133 }
7134 }
7135 } while (false);
7136
7137 if (allow && isCapMsg && _joinedCapabilityClients) {
7138 _joinedCapabilityClients->removeObject((OSObject *) object);
7139 if (_joinedCapabilityClients->getCount() == 0) {
7140 DMSG("destroyed capability client set %p\n",
7141 OBFUSCATE(_joinedCapabilityClients.get()));
7142 _joinedCapabilityClients.reset();
7143 }
7144 }
7145 if (notifier) {
7146 // Record the last seen message type even if the message is dropped
7147 // for traceFilteredNotification().
7148 notifier->msgType = context->messageType;
7149 }
7150
7151 return allow;
7152 }
7153
7154 //******************************************************************************
7155 // setMaintenanceWakeCalendar
7156 //
7157 //******************************************************************************
7158
7159 IOReturn
setMaintenanceWakeCalendar(const IOPMCalendarStruct * calendar)7160 IOPMrootDomain::setMaintenanceWakeCalendar(
7161 const IOPMCalendarStruct * calendar )
7162 {
7163 OSSharedPtr<OSData> data;
7164 IOReturn ret = 0;
7165
7166 if (!calendar) {
7167 return kIOReturnBadArgument;
7168 }
7169
7170 data = OSData::withValue(*calendar);
7171 if (!data) {
7172 return kIOReturnNoMemory;
7173 }
7174
7175 if (kPMCalendarTypeMaintenance == calendar->selector) {
7176 ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7177 } else if (kPMCalendarTypeSleepService == calendar->selector) {
7178 ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7179 }
7180
7181 return ret;
7182 }
7183
7184 // MARK: -
7185 // MARK: Display Wrangler
7186
7187 //******************************************************************************
7188 // displayWranglerNotification
7189 //
7190 // Handle the notification when the IODisplayWrangler changes power state.
7191 //******************************************************************************
7192
7193 IOReturn
displayWranglerNotification(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgument,vm_size_t argSize)7194 IOPMrootDomain::displayWranglerNotification(
7195 void * target, void * refCon,
7196 UInt32 messageType, IOService * service,
7197 void * messageArgument, vm_size_t argSize )
7198 {
7199 #if DISPLAY_WRANGLER_PRESENT
7200 IOPMPowerStateIndex displayPowerState;
7201 IOPowerStateChangeNotification * params =
7202 (IOPowerStateChangeNotification *) messageArgument;
7203
7204 if ((messageType != kIOMessageDeviceWillPowerOff) &&
7205 (messageType != kIOMessageDeviceHasPoweredOn)) {
7206 return kIOReturnUnsupported;
7207 }
7208
7209 ASSERT_GATED();
7210 if (!gRootDomain) {
7211 return kIOReturnUnsupported;
7212 }
7213
7214 displayPowerState = params->stateNumber;
7215 DLOG("wrangler %s ps %d\n",
7216 getIOMessageString(messageType), (uint32_t) displayPowerState);
7217
7218 switch (messageType) {
7219 case kIOMessageDeviceWillPowerOff:
7220 // Display wrangler has dropped power due to display idle
7221 // or force system sleep.
7222 //
7223 // 4 Display ON kWranglerPowerStateMax
7224 // 3 Display Dim kWranglerPowerStateDim
7225 // 2 Display Sleep kWranglerPowerStateSleep
7226 // 1 Not visible to user
7227 // 0 Not visible to user kWranglerPowerStateMin
7228
7229 if (displayPowerState <= kWranglerPowerStateSleep) {
7230 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7231 }
7232 break;
7233
7234 case kIOMessageDeviceHasPoweredOn:
7235 // Display wrangler has powered on due to user activity
7236 // or wake from sleep.
7237
7238 if (kWranglerPowerStateMax == displayPowerState) {
7239 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7240
7241 // See comment in handleUpdatePowerClientForDisplayWrangler
7242 if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7243 kWranglerPowerStateMax) {
7244 gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7245 }
7246 }
7247 break;
7248 }
7249 #endif /* DISPLAY_WRANGLER_PRESENT */
7250 return kIOReturnUnsupported;
7251 }
7252
7253 //******************************************************************************
7254 // reportUserInput
7255 //
7256 //******************************************************************************
7257
7258 void
updateUserActivity(void)7259 IOPMrootDomain::updateUserActivity( void )
7260 {
7261 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7262 clock_get_uptime(&userActivityTime);
7263 bool aborting = ((lastSleepReason == kIOPMSleepReasonSoftware)
7264 || (lastSleepReason == kIOPMSleepReasonIdle)
7265 || (lastSleepReason == kIOPMSleepReasonMaintenance));
7266 if (aborting) {
7267 userActivityCount++;
7268 DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7269 }
7270 #endif
7271 }
7272 void
reportUserInput(void)7273 IOPMrootDomain::reportUserInput( void )
7274 {
7275 if (wrangler) {
7276 wrangler->activityTickle(0, 0);
7277 }
7278 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7279 // Update user activity
7280 updateUserActivity();
7281
7282 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7283 // update user active abs time
7284 clock_get_uptime(&gUserActiveAbsTime);
7285 pmPowerStateQueue->submitPowerEvent(
7286 kPowerEventPolicyStimulus,
7287 (void *) kStimulusDarkWakeActivityTickle,
7288 true /* set wake type */ );
7289 }
7290 #endif
7291 }
7292
7293 void
requestUserActive(IOService * device,const char * reason)7294 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7295 {
7296 #if DISPLAY_WRANGLER_PRESENT
7297 if (wrangler) {
7298 wrangler->activityTickle(0, 0);
7299 }
7300 #else
7301 if (!device) {
7302 DLOG("requestUserActive: device is null\n");
7303 return;
7304 }
7305 OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7306 uint64_t registryID = device->getRegistryEntryID();
7307
7308 if (!deviceName || !registryID) {
7309 DLOG("requestUserActive: no device name or registry entry\n");
7310 return;
7311 }
7312 const char *name = deviceName->getCStringNoCopy();
7313 char payload[128];
7314 snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7315 DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7316 messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7317 #endif
7318 }
7319
7320 //******************************************************************************
7321 // latchDisplayWranglerTickle
7322 //******************************************************************************
7323
7324 bool
latchDisplayWranglerTickle(bool latch)7325 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7326 {
7327 #if DISPLAY_WRANGLER_PRESENT
7328 if (latch) {
7329 if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7330 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7331 !checkSystemCanSustainFullWake()) {
7332 // Currently in dark wake, and not transitioning to full wake.
7333 // Full wake is unsustainable, so latch the tickle to prevent
7334 // the display from lighting up momentarily.
7335 wranglerTickled = true;
7336 } else {
7337 wranglerTickled = false;
7338 }
7339 } else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7340 wranglerTickled = false;
7341
7342 pmPowerStateQueue->submitPowerEvent(
7343 kPowerEventPolicyStimulus,
7344 (void *) kStimulusDarkWakeActivityTickle );
7345 }
7346
7347 return wranglerTickled;
7348 #else /* ! DISPLAY_WRANGLER_PRESENT */
7349 return false;
7350 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7351 }
7352
7353 //******************************************************************************
7354 // setDisplayPowerOn
7355 //
7356 // For root domain user client
7357 //******************************************************************************
7358
7359 void
setDisplayPowerOn(uint32_t options)7360 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7361 {
7362 pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7363 (void *) NULL, options );
7364 }
7365
7366 // MARK: -
7367 // MARK: System PM Policy
7368
7369 //******************************************************************************
7370 // checkSystemSleepAllowed
7371 //
7372 //******************************************************************************
7373
7374 bool
checkSystemSleepAllowed(IOOptionBits options,uint32_t sleepReason)7375 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7376 uint32_t sleepReason )
7377 {
7378 uint32_t err = 0;
7379
7380 // Conditions that prevent idle and demand system sleep.
7381
7382 do {
7383 if (gSleepDisabledFlag) {
7384 err = kPMConfigPreventSystemSleep;
7385 break;
7386 }
7387
7388 if (userDisabledAllSleep) {
7389 err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7390 break;
7391 }
7392
7393 if (systemBooting || systemShutdown || gWillShutdown) {
7394 err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7395 break;
7396 }
7397
7398 if (options == 0) {
7399 break;
7400 }
7401
7402 // Conditions above pegs the system at full wake.
7403 // Conditions below prevent system sleep but does not prevent
7404 // dark wake, and must be called from gated context.
7405
7406 #if !CONFIG_SLEEP
7407 err = kPMConfigPreventSystemSleep; // 3. config does not support sleep
7408 break;
7409 #endif
7410
7411 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7412 break; // always sleep on low battery or when in thermal warning/emergency state
7413 }
7414
7415 if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7416 break; // always sleep on dark wake thermal emergencies
7417 }
7418
7419 if (preventSystemSleepList->getCount() != 0) {
7420 err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7421 break;
7422 }
7423
7424 if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7425 kIOPMDriverAssertionLevelOn) {
7426 err = kPMCPUAssertion; // 5. CPU assertion
7427 break;
7428 }
7429
7430 if (pciCantSleepValid) {
7431 if (pciCantSleepFlag) {
7432 err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7433 }
7434 break;
7435 } else if (sleepSupportedPEFunction &&
7436 CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7437 IOReturn ret;
7438 OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7439 ret = getPlatform()->callPlatformFunction(
7440 sleepSupportedPEFunction.get(), false,
7441 NULL, NULL, NULL, NULL);
7442 pciCantSleepValid = true;
7443 pciCantSleepFlag = false;
7444 if ((platformSleepSupport & kPCICantSleep) ||
7445 ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7446 err = 6; // 6. PCI card does not support PM
7447 pciCantSleepFlag = true;
7448 break;
7449 }
7450 }
7451 }while (false);
7452
7453 if (err) {
7454 DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7455 return false;
7456 }
7457 return true;
7458 }
7459
7460 bool
checkSystemSleepEnabled(void)7461 IOPMrootDomain::checkSystemSleepEnabled( void )
7462 {
7463 return checkSystemSleepAllowed(0, 0);
7464 }
7465
7466 bool
checkSystemCanSleep(uint32_t sleepReason)7467 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7468 {
7469 ASSERT_GATED();
7470 return checkSystemSleepAllowed(1, sleepReason);
7471 }
7472
7473 //******************************************************************************
7474 // checkSystemCanSustainFullWake
7475 //******************************************************************************
7476
7477 bool
checkSystemCanSustainFullWake(void)7478 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7479 {
7480 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7481 // Low battery wake, or received a low battery notification
7482 // while system is awake. This condition will persist until
7483 // the following wake.
7484 return false;
7485 }
7486
7487 if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7488 // Graphics state is unknown and external display might not be probed.
7489 // Do not incorporate state that requires graphics to be in max power
7490 // such as desktopMode or clamshellDisabled.
7491
7492 if (!acAdaptorConnected) {
7493 DLOG("full wake check: no AC\n");
7494 return false;
7495 }
7496 }
7497 return true;
7498 }
7499
7500 //******************************************************************************
7501 // mustHibernate
7502 //******************************************************************************
7503
7504 #if HIBERNATION
7505
7506 bool
mustHibernate(void)7507 IOPMrootDomain::mustHibernate( void )
7508 {
7509 return lowBatteryCondition || thermalWarningState;
7510 }
7511
7512 #endif /* HIBERNATION */
7513
7514 //******************************************************************************
7515 // AOT
7516 //******************************************************************************
7517
7518 // Tables for accumulated days in year by month, latter used for leap years
7519
7520 static const unsigned int daysbymonth[] =
7521 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7522
7523 static const unsigned int lydaysbymonth[] =
7524 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7525
7526 static int __unused
IOPMConvertSecondsToCalendar(clock_sec_t secs,IOPMCalendarStruct * dt)7527 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7528 {
7529 const unsigned int * dbm = daysbymonth;
7530 clock_sec_t n, x, y, z;
7531
7532 // Calculate seconds, minutes and hours
7533
7534 n = secs % (24 * 3600);
7535 dt->second = n % 60;
7536 n /= 60;
7537 dt->minute = n % 60;
7538 dt->hour = (typeof(dt->hour))(n / 60);
7539
7540 // Calculate day of week
7541
7542 n = secs / (24 * 3600);
7543 // dt->dayWeek = (n + 4) % 7;
7544
7545 // Calculate year
7546 // Rebase from days since Unix epoch (1/1/1970) store in 'n',
7547 // to days since 1/1/1968 to start on 4 year cycle, beginning
7548 // on a leap year.
7549
7550 n += (366 + 365);
7551
7552 // Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7553 // Valid before 2100, since 2100 is not a leap year.
7554
7555 x = n / 1461; // number of 4 year cycles
7556 y = n % 1461; // days into current 4 year cycle
7557 z = 1968 + (4 * x);
7558
7559 // Add in years in the current 4 year cycle
7560
7561 if (y >= 366) {
7562 y -= 366; // days after the leap year
7563 n = y % 365; // days into the current year
7564 z += (1 + y / 365); // years after the past 4-yr cycle
7565 } else {
7566 n = y;
7567 dbm = lydaysbymonth;
7568 }
7569 if (z > 2099) {
7570 return 0;
7571 }
7572
7573 dt->year = (typeof(dt->year))z;
7574
7575 // Adjust remaining days value to start at 1
7576
7577 n += 1;
7578
7579 // Calculate month
7580
7581 for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7582 continue;
7583 }
7584 dt->month = (typeof(dt->month))x;
7585
7586 // Calculate day of month
7587
7588 dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7589
7590 return 1;
7591 }
7592
7593 static clock_sec_t
IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)7594 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7595 {
7596 const unsigned int * dbm = daysbymonth;
7597 long y, secs, days;
7598
7599 if (dt->year < 1970 || dt->month > 12) {
7600 return 0;
7601 }
7602
7603 // Seconds elapsed in the current day
7604
7605 secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7606
7607 // Number of days from 1/1/70 to beginning of current year
7608 // Account for extra day every 4 years starting at 1973
7609
7610 y = dt->year - 1970;
7611 days = (y * 365) + ((y + 1) / 4);
7612
7613 // Change table if current year is a leap year
7614
7615 if ((dt->year % 4) == 0) {
7616 dbm = lydaysbymonth;
7617 }
7618
7619 // Add in days elapsed in the current year
7620
7621 days += (dt->day - 1) + dbm[dt->month - 1];
7622
7623 // Add accumulated days to accumulated seconds
7624
7625 secs += 24 * 3600 * days;
7626
7627 return secs;
7628 }
7629
7630 unsigned long
getRUN_STATE(void)7631 IOPMrootDomain::getRUN_STATE(void)
7632 {
7633 return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7634 }
7635
7636 bool
isAOTMode()7637 IOPMrootDomain::isAOTMode()
7638 {
7639 return _aotNow;
7640 }
7641
7642 IOReturn
setWakeTime(uint64_t wakeContinuousTime)7643 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7644 {
7645 clock_sec_t nowsecs, wakesecs;
7646 clock_usec_t nowmicrosecs, wakemicrosecs;
7647 uint64_t nowAbs, wakeAbs;
7648
7649 if (!_aotMode) {
7650 return kIOReturnNotReady;
7651 }
7652
7653 clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7654 wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7655 if (wakeAbs < nowAbs) {
7656 printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7657 wakeAbs = nowAbs;
7658 }
7659 wakeAbs -= nowAbs;
7660 absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7661
7662 wakesecs += nowsecs;
7663 wakemicrosecs += nowmicrosecs;
7664 if (wakemicrosecs >= USEC_PER_SEC) {
7665 wakesecs++;
7666 wakemicrosecs -= USEC_PER_SEC;
7667 }
7668 if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7669 wakesecs++;
7670 }
7671
7672 IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7673
7674 if (_aotWakeTimeContinuous != wakeContinuousTime) {
7675 _aotWakeTimeContinuous = wakeContinuousTime;
7676 IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7677 }
7678 _aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7679 _aotWakeTimeUTC = wakesecs;
7680
7681 return kIOReturnSuccess;
7682 }
7683
7684 // assumes WAKEEVENT_LOCK
7685 bool
aotShouldExit(bool checkTimeSet,bool software)7686 IOPMrootDomain::aotShouldExit(bool checkTimeSet, bool software)
7687 {
7688 bool exitNow = false;
7689 const char * reason = "";
7690
7691 if (!_aotNow) {
7692 return false;
7693 }
7694
7695 if (software) {
7696 exitNow = true;
7697 _aotMetrics->softwareRequestCount++;
7698 reason = "software request";
7699 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7700 exitNow = true;
7701 reason = gWakeReasonString;
7702 } else if (checkTimeSet && (kPMCalendarTypeInvalid == _aotWakeTimeCalendar.selector)) {
7703 exitNow = true;
7704 _aotMetrics->noTimeSetCount++;
7705 reason = "flipbook expired";
7706 } else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7707 clock_sec_t sec;
7708 clock_usec_t usec;
7709 clock_get_calendar_microtime(&sec, &usec);
7710 if (_calendarWakeAlarmUTC <= sec) {
7711 exitNow = true;
7712 _aotMetrics->rtcAlarmsCount++;
7713 reason = "user alarm";
7714 }
7715 }
7716 if (exitNow) {
7717 _aotPendingFlags |= kIOPMWakeEventAOTExit;
7718 IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7719 reason,
7720 _aotMetrics->sleepCount,
7721 _aotMetrics->possibleCount,
7722 _aotMetrics->confirmedPossibleCount,
7723 _aotMetrics->rejectedPossibleCount,
7724 _aotMetrics->expiredPossibleCount,
7725 _aotMetrics->noTimeSetCount,
7726 _aotMetrics->rtcAlarmsCount);
7727 }
7728 return exitNow;
7729 }
7730
7731 void
aotExit(bool cps)7732 IOPMrootDomain::aotExit(bool cps)
7733 {
7734 uint32_t savedMessageMask;
7735
7736 ASSERT_GATED();
7737 _aotNow = false;
7738 _aotReadyToFullWake = false;
7739 if (_aotTimerScheduled) {
7740 _aotTimerES->cancelTimeout();
7741 _aotTimerScheduled = false;
7742 }
7743 updateTasksSuspend(kTasksSuspendNoChange, kTasksSuspendUnsuspended);
7744
7745 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7746 _aotLastWakeTime = 0;
7747 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7748 WAKEEVENT_LOCK();
7749 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7750 gWakeReasonString,
7751 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7752 WAKEEVENT_UNLOCK();
7753 }
7754
7755 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7756
7757 // Preserve the message mask since a system wake transition
7758 // may have already started and initialized the mask.
7759 savedMessageMask = _systemMessageClientMask;
7760 _systemMessageClientMask = kSystemMessageClientLegacyApp;
7761 tellClients(kIOMessageSystemWillPowerOn);
7762 _systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7763
7764 if (cps) {
7765 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7766 }
7767 }
7768
7769 void
aotEvaluate(IOTimerEventSource * timer)7770 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7771 {
7772 bool exitNow;
7773
7774 IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7775
7776 WAKEEVENT_LOCK();
7777 exitNow = aotShouldExit(false, false);
7778 if (timer != NULL) {
7779 _aotTimerScheduled = false;
7780 }
7781 WAKEEVENT_UNLOCK();
7782 if (exitNow) {
7783 aotExit(true);
7784 } else {
7785 #if 0
7786 if (_aotLingerTime) {
7787 uint64_t deadline;
7788 IOLog("aot linger before sleep\n");
7789 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7790 clock_delay_until(deadline);
7791 }
7792 #endif
7793 privateSleepSystem(kIOPMSleepReasonSoftware);
7794 }
7795 }
7796
7797 //******************************************************************************
7798 // adjustPowerState
7799 //
7800 // Conditions that affect our wake/sleep decision has changed.
7801 // If conditions dictate that the system must remain awake, clamp power
7802 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7803 // is TRUE, then remove the power clamp and allow the power state to drop
7804 // to SLEEP_STATE.
7805 //******************************************************************************
7806
7807 void
adjustPowerState(bool sleepASAP)7808 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7809 {
7810 DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7811 getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7812
7813 ASSERT_GATED();
7814
7815 if (_aotNow) {
7816 bool exitNow;
7817
7818 if (AOT_STATE != getPowerState()) {
7819 return;
7820 }
7821 if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
7822 // Don't try to force sleep during AOT while IOMobileFramebuffer is holding a power assertion.
7823 // Doing so will result in the sleep being cancelled anyway,
7824 // but this check avoids unnecessary thrashing in the power state engine.
7825 return;
7826 }
7827 WAKEEVENT_LOCK();
7828 exitNow = aotShouldExit(true, false);
7829 if (!exitNow
7830 && !_aotTimerScheduled
7831 && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7832 _aotTimerScheduled = true;
7833 if (_aotLingerTime) {
7834 _aotTimerES->setTimeout(_aotLingerTime);
7835 } else {
7836 _aotTimerES->setTimeout(800, kMillisecondScale);
7837 }
7838 }
7839 WAKEEVENT_UNLOCK();
7840 if (exitNow) {
7841 aotExit(true);
7842 } else {
7843 _aotReadyToFullWake = true;
7844 if (!_aotTimerScheduled) {
7845 privateSleepSystem(kIOPMSleepReasonSoftware);
7846 }
7847 }
7848 return;
7849 }
7850
7851 if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
7852 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
7853 } else if (sleepASAP) {
7854 changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
7855 }
7856 }
7857
7858 void
handleSetDisplayPowerOn(bool powerOn)7859 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
7860 {
7861 if (powerOn) {
7862 if (!checkSystemCanSustainFullWake()) {
7863 DLOG("System cannot sustain full wake\n");
7864 return;
7865 }
7866
7867 // Force wrangler to max power state. If system is in dark wake
7868 // this alone won't raise the wrangler's power state.
7869 if (wrangler) {
7870 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
7871 }
7872
7873 // System in dark wake, always requesting full wake should
7874 // not have any bad side-effects, even if the request fails.
7875
7876 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
7877 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
7878 requestFullWake( kFullWakeReasonDisplayOn );
7879 }
7880 } else {
7881 // Relenquish desire to power up display.
7882 // Must first transition to state 1 since wrangler doesn't
7883 // power off the displays at state 0. At state 0 the root
7884 // domain is removed from the wrangler's power client list.
7885 if (wrangler) {
7886 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
7887 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
7888 }
7889 }
7890 }
7891
7892 //******************************************************************************
7893 // dispatchPowerEvent
7894 //
7895 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
7896 //******************************************************************************
7897
7898 void
dispatchPowerEvent(uint32_t event,void * arg0,uint64_t arg1)7899 IOPMrootDomain::dispatchPowerEvent(
7900 uint32_t event, void * arg0, uint64_t arg1 )
7901 {
7902 ASSERT_GATED();
7903
7904 switch (event) {
7905 case kPowerEventFeatureChanged:
7906 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7907 messageClients(kIOPMMessageFeatureChange, this);
7908 break;
7909
7910 case kPowerEventReceivedPowerNotification:
7911 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7912 handlePowerNotification((UInt32)(uintptr_t) arg0 );
7913 break;
7914
7915 case kPowerEventSystemBootCompleted:
7916 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7917 if (systemBooting) {
7918 systemBooting = false;
7919
7920 if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
7921 DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
7922 }
7923 if (lowBatteryCondition || thermalEmergencyState) {
7924 if (lowBatteryCondition) {
7925 privateSleepSystem(kIOPMSleepReasonLowPower);
7926 } else {
7927 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
7928 }
7929 // The rest is unnecessary since the system is expected
7930 // to sleep immediately. The following wake will update
7931 // everything.
7932 break;
7933 }
7934
7935 sleepWakeDebugMemAlloc();
7936 saveFailureData2File();
7937
7938 // If lid is closed, re-send lid closed notification
7939 // now that booting is complete.
7940 if (clamshellClosed) {
7941 handlePowerNotification(kLocalEvalClamshellCommand);
7942 }
7943 evaluatePolicy( kStimulusAllowSystemSleepChanged );
7944 }
7945 break;
7946
7947 case kPowerEventSystemShutdown:
7948 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7949 if (kOSBooleanTrue == (OSBoolean *) arg0) {
7950 /* We set systemShutdown = true during shutdown
7951 * to prevent sleep at unexpected times while loginwindow is trying
7952 * to shutdown apps and while the OS is trying to transition to
7953 * complete power of.
7954 *
7955 * Set to true during shutdown, as soon as loginwindow shows
7956 * the "shutdown countdown dialog", through individual app
7957 * termination, and through black screen kernel shutdown.
7958 */
7959 systemShutdown = true;
7960 } else {
7961 /*
7962 * A shutdown was initiated, but then the shutdown
7963 * was cancelled, clearing systemShutdown to false here.
7964 */
7965 systemShutdown = false;
7966 }
7967 break;
7968
7969 case kPowerEventUserDisabledSleep:
7970 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7971 userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
7972 break;
7973
7974 case kPowerEventRegisterSystemCapabilityClient:
7975 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7976
7977 // reset() handles the arg0 == nullptr case for us
7978 systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
7979 /* intentional fall-through */
7980 [[clang::fallthrough]];
7981
7982 case kPowerEventRegisterKernelCapabilityClient:
7983 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7984 if (!_joinedCapabilityClients) {
7985 _joinedCapabilityClients = OSSet::withCapacity(8);
7986 }
7987 if (arg0) {
7988 OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
7989 if (_joinedCapabilityClients) {
7990 _joinedCapabilityClients->setObject(notify.get());
7991 synchronizePowerTree( kIOPMSyncNoChildNotify );
7992 }
7993 }
7994 break;
7995
7996 case kPowerEventPolicyStimulus:
7997 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7998 if (arg0) {
7999 int stimulus = (int)(uintptr_t) arg0;
8000 evaluatePolicy(stimulus, (uint32_t) arg1);
8001 }
8002 break;
8003
8004 case kPowerEventAssertionCreate:
8005 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8006 if (pmAssertions) {
8007 pmAssertions->handleCreateAssertion((OSValueObject<PMAssertStruct> *)arg0);
8008 }
8009 break;
8010
8011
8012 case kPowerEventAssertionRelease:
8013 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8014 if (pmAssertions) {
8015 pmAssertions->handleReleaseAssertion(arg1);
8016 }
8017 break;
8018
8019 case kPowerEventAssertionSetLevel:
8020 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8021 if (pmAssertions) {
8022 pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
8023 }
8024 break;
8025
8026 case kPowerEventQueueSleepWakeUUID:
8027 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8028 handleQueueSleepWakeUUID((OSObject *)arg0);
8029 break;
8030 case kPowerEventPublishSleepWakeUUID:
8031 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8032 handlePublishSleepWakeUUID((bool)arg0);
8033 break;
8034
8035 case kPowerEventSetDisplayPowerOn:
8036 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8037 if (arg1 != 0) {
8038 displayPowerOnRequested = true;
8039 } else {
8040 displayPowerOnRequested = false;
8041 }
8042 handleSetDisplayPowerOn(displayPowerOnRequested);
8043 break;
8044
8045 case kPowerEventPublishWakeType:
8046 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8047
8048 // Don't replace wake type property if already set
8049 if ((arg0 == gIOPMWakeTypeUserKey) ||
8050 !propertyExists(kIOPMRootDomainWakeTypeKey)) {
8051 const char * wakeType = NULL;
8052
8053 if (arg0 == gIOPMWakeTypeUserKey) {
8054 requestUserActive(this, "WakeTypeUser");
8055 wakeType = kIOPMRootDomainWakeTypeUser;
8056 } else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
8057 requestUserActive(this, "WakeTypeAlarm");
8058 wakeType = kIOPMRootDomainWakeTypeAlarm;
8059 } else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
8060 darkWakeSleepService = true;
8061 wakeType = kIOPMRootDomainWakeTypeSleepService;
8062 } else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8063 wakeType = kIOPMRootDomainWakeTypeMaintenance;
8064 }
8065
8066 if (wakeType) {
8067 setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8068 }
8069 }
8070 break;
8071
8072 case kPowerEventAOTEvaluate:
8073 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8074 if (_aotReadyToFullWake) {
8075 aotEvaluate(NULL);
8076 }
8077 break;
8078 }
8079 }
8080
8081 //******************************************************************************
8082 // systemPowerEventOccurred
8083 //
8084 // The power controller is notifying us of a hardware-related power management
8085 // event that we must handle.
8086 //
8087 // systemPowerEventOccurred covers the same functionality that
8088 // receivePowerNotification does; it simply provides a richer API for conveying
8089 // more information.
8090 //******************************************************************************
8091
8092 IOReturn
systemPowerEventOccurred(const OSSymbol * event,uint32_t intValue)8093 IOPMrootDomain::systemPowerEventOccurred(
8094 const OSSymbol *event,
8095 uint32_t intValue)
8096 {
8097 IOReturn attempt = kIOReturnSuccess;
8098 OSSharedPtr<OSNumber> newNumber;
8099
8100 if (!event) {
8101 return kIOReturnBadArgument;
8102 }
8103
8104 newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8105 if (!newNumber) {
8106 return kIOReturnInternalError;
8107 }
8108
8109 attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8110
8111 return attempt;
8112 }
8113
8114 void
setThermalState(OSObject * value)8115 IOPMrootDomain::setThermalState(OSObject *value)
8116 {
8117 OSNumber * num;
8118
8119 if (gIOPMWorkLoop->inGate() == false) {
8120 gIOPMWorkLoop->runAction(
8121 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8122 (OSObject *)this,
8123 (void *)value);
8124
8125 return;
8126 }
8127 if (value && (num = OSDynamicCast(OSNumber, value))) {
8128 thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8129 (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8130 }
8131 }
8132
8133 IOReturn
systemPowerEventOccurred(const OSSymbol * event,OSObject * value)8134 IOPMrootDomain::systemPowerEventOccurred(
8135 const OSSymbol *event,
8136 OSObject *value)
8137 {
8138 OSSharedPtr<OSDictionary> thermalsDict;
8139 bool shouldUpdate = true;
8140
8141 if (!event || !value) {
8142 return kIOReturnBadArgument;
8143 }
8144
8145 // LOCK
8146 // We reuse featuresDict Lock because it already exists and guards
8147 // the very infrequently used publish/remove feature mechanism; so there's zero rsk
8148 // of stepping on that lock.
8149 if (featuresDictLock) {
8150 IOLockLock(featuresDictLock);
8151 }
8152
8153 OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8154 OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8155
8156 if (origThermalsDict) {
8157 thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8158 } else {
8159 thermalsDict = OSDictionary::withCapacity(1);
8160 }
8161
8162 if (!thermalsDict) {
8163 shouldUpdate = false;
8164 goto exit;
8165 }
8166
8167 thermalsDict->setObject(event, value);
8168
8169 setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8170
8171 exit:
8172 // UNLOCK
8173 if (featuresDictLock) {
8174 IOLockUnlock(featuresDictLock);
8175 }
8176
8177 if (shouldUpdate) {
8178 if (event &&
8179 event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8180 setThermalState(value);
8181 }
8182 messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8183 }
8184
8185 return kIOReturnSuccess;
8186 }
8187
8188 //******************************************************************************
8189 // receivePowerNotification
8190 //
8191 // The power controller is notifying us of a hardware-related power management
8192 // event that we must handle. This may be a result of an 'environment' interrupt
8193 // from the power mgt micro.
8194 //******************************************************************************
8195
8196 IOReturn
receivePowerNotification(UInt32 msg)8197 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8198 {
8199 if (msg & kIOPMPowerButton) {
8200 uint32_t currentPhase = pmTracer->getTracePhase();
8201 if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8202 DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8203 swd_flags |= SWD_PWR_BTN_STACKSHOT;
8204 thread_call_enter(powerButtonDown);
8205 } else {
8206 DEBUG_LOG("power button pressed when system is up\n");
8207 }
8208 } else if (msg & kIOPMPowerButtonUp) {
8209 if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8210 swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8211 thread_call_enter(powerButtonUp);
8212 }
8213 } else {
8214 pmPowerStateQueue->submitPowerEvent(
8215 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8216 }
8217 return kIOReturnSuccess;
8218 }
8219
8220 void
handlePowerNotification(UInt32 msg)8221 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8222 {
8223 bool eval_clamshell = false;
8224 bool eval_clamshell_alarm = false;
8225
8226 ASSERT_GATED();
8227
8228 /*
8229 * Local (IOPMrootDomain only) eval clamshell command
8230 */
8231 if (msg & kLocalEvalClamshellCommand) {
8232 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8233 eval_clamshell_alarm = true;
8234
8235 // reset isRTCAlarmWake. This evaluation should happen only once
8236 // on RTC/Alarm wake. Any clamshell events after wake should follow
8237 // the regular evaluation
8238 isRTCAlarmWake = false;
8239 } else {
8240 eval_clamshell = true;
8241 }
8242 }
8243
8244 /*
8245 * Overtemp
8246 */
8247 if (msg & kIOPMOverTemp) {
8248 DLOG("Thermal overtemp message received!\n");
8249 thermalEmergencyState = true;
8250 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8251 }
8252
8253 /*
8254 * Forward DW thermal notification to client, if system is not going to sleep
8255 */
8256 if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8257 DLOG("DarkWake thermal limits message received!\n");
8258 messageClients(kIOPMMessageDarkWakeThermalEmergency);
8259 }
8260
8261 /*
8262 * Sleep Now!
8263 */
8264 if (msg & kIOPMSleepNow) {
8265 privateSleepSystem(kIOPMSleepReasonSoftware);
8266 }
8267
8268 /*
8269 * Power Emergency
8270 */
8271 if (msg & kIOPMPowerEmergency) {
8272 DLOG("Received kIOPMPowerEmergency");
8273 lowBatteryCondition = true;
8274 privateSleepSystem(kIOPMSleepReasonLowPower);
8275 }
8276
8277 /*
8278 * Clamshell OPEN
8279 */
8280 if (msg & kIOPMClamshellOpened) {
8281 DLOG("Clamshell opened\n");
8282 // Received clamshel open message from clamshell controlling driver
8283 // Update our internal state and tell general interest clients
8284 clamshellClosed = false;
8285 clamshellExists = true;
8286
8287 // Don't issue a hid tickle when lid is open and polled on wake
8288 if (msg & kIOPMSetValue) {
8289 setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8290 reportUserInput();
8291 }
8292
8293 // Tell PMCPU
8294 informCPUStateChange(kInformLid, 0);
8295
8296 // Tell general interest clients
8297 sendClientClamshellNotification();
8298
8299 bool aborting = ((lastSleepReason == kIOPMSleepReasonClamshell)
8300 || (lastSleepReason == kIOPMSleepReasonIdle)
8301 || (lastSleepReason == kIOPMSleepReasonMaintenance));
8302 if (aborting) {
8303 userActivityCount++;
8304 }
8305 DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8306 }
8307
8308 /*
8309 * Clamshell CLOSED
8310 * Send the clamshell interest notification since the lid is closing.
8311 */
8312 if (msg & kIOPMClamshellClosed) {
8313 if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8314 clamshellClosed && clamshellExists) {
8315 DLOG("Ignoring redundant Clamshell close event\n");
8316 } else {
8317 DLOG("Clamshell closed\n");
8318 // Received clamshel open message from clamshell controlling driver
8319 // Update our internal state and tell general interest clients
8320 clamshellClosed = true;
8321 clamshellExists = true;
8322
8323 // Ignore all following clamshell close events until the clamshell
8324 // is opened or the system sleeps. When a clamshell close triggers
8325 // a system wake, the lid driver may send us two clamshell close
8326 // events, one for the clamshell close event itself, and a second
8327 // close event when the driver polls the lid state on wake.
8328 clamshellIgnoreClose = true;
8329
8330 // Tell PMCPU
8331 informCPUStateChange(kInformLid, 1);
8332
8333 // Tell general interest clients
8334 sendClientClamshellNotification();
8335
8336 // And set eval_clamshell = so we can attempt
8337 eval_clamshell = true;
8338 }
8339 }
8340
8341 /*
8342 * Set Desktop mode (sent from graphics)
8343 *
8344 * -> reevaluate lid state
8345 */
8346 if (msg & kIOPMSetDesktopMode) {
8347 desktopMode = (0 != (msg & kIOPMSetValue));
8348 msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8349 DLOG("Desktop mode %d\n", desktopMode);
8350
8351 sendClientClamshellNotification();
8352
8353 // Re-evaluate the lid state
8354 eval_clamshell = true;
8355 }
8356
8357 /*
8358 * AC Adaptor connected
8359 *
8360 * -> reevaluate lid state
8361 */
8362 if (msg & kIOPMSetACAdaptorConnected) {
8363 acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8364 msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8365
8366 // Tell CPU PM
8367 informCPUStateChange(kInformAC, !acAdaptorConnected);
8368
8369 // Tell BSD if AC is connected
8370 // 0 == external power source; 1 == on battery
8371 post_sys_powersource(acAdaptorConnected ? 0:1);
8372
8373 sendClientClamshellNotification();
8374
8375 IOUserServer::powerSourceChanged(acAdaptorConnected);
8376
8377 // Re-evaluate the lid state
8378 eval_clamshell = true;
8379
8380 // Lack of AC may have latched a display wrangler tickle.
8381 // This mirrors the hardware's USB wake event latch, where a latched
8382 // USB wake event followed by an AC attach will trigger a full wake.
8383 latchDisplayWranglerTickle( false );
8384
8385 #if HIBERNATION
8386 // AC presence will reset the standy timer delay adjustment.
8387 _standbyTimerResetSeconds = 0;
8388 #endif
8389 if (!userIsActive) {
8390 // Reset userActivityTime when power supply is changed(rdr 13789330)
8391 clock_get_uptime(&userActivityTime);
8392 }
8393 }
8394
8395 /*
8396 * Enable Clamshell (external display disappear)
8397 *
8398 * -> reevaluate lid state
8399 */
8400 if (msg & kIOPMEnableClamshell) {
8401 DLOG("Clamshell enabled\n");
8402
8403 // Re-evaluate the lid state
8404 // System should sleep on external display disappearance
8405 // in lid closed operation.
8406 if (true == clamshellDisabled) {
8407 eval_clamshell = true;
8408
8409 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8410 // Also clear kClamshellSleepDisableInternal when graphics enables
8411 // the clamshell during a full wake. When graphics is behaving as
8412 // expected, this will allow clamshell close to be honored earlier
8413 // rather than waiting for the delayed evaluation.
8414 if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8415 (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8416 CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8417 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8418
8419 // Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8420 // when timer expires which is harmless but useless.
8421 thread_call_cancel(fullWakeThreadCall);
8422 }
8423 #endif
8424 }
8425
8426 clamshellDisabled = false;
8427 sendClientClamshellNotification();
8428 }
8429
8430 /*
8431 * Disable Clamshell (external display appeared)
8432 * We don't bother re-evaluating clamshell state. If the system is awake,
8433 * the lid is probably open.
8434 */
8435 if (msg & kIOPMDisableClamshell) {
8436 DLOG("Clamshell disabled\n");
8437 clamshellDisabled = true;
8438 sendClientClamshellNotification();
8439 }
8440
8441 /*
8442 * Evaluate clamshell and SLEEP if appropriate
8443 */
8444 if (eval_clamshell_alarm && clamshellClosed) {
8445 if (shouldSleepOnRTCAlarmWake()) {
8446 privateSleepSystem(kIOPMSleepReasonClamshell);
8447 }
8448 } else if (eval_clamshell && clamshellClosed) {
8449 if (shouldSleepOnClamshellClosed()) {
8450 privateSleepSystem(kIOPMSleepReasonClamshell);
8451 } else {
8452 evaluatePolicy( kStimulusDarkWakeEvaluate );
8453 }
8454 }
8455
8456 if (msg & kIOPMProModeEngaged) {
8457 int newState = 1;
8458 DLOG("ProModeEngaged\n");
8459 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8460 }
8461
8462 if (msg & kIOPMProModeDisengaged) {
8463 int newState = 0;
8464 DLOG("ProModeDisengaged\n");
8465 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8466 }
8467 }
8468
8469 //******************************************************************************
8470 // evaluatePolicy
8471 //
8472 // Evaluate root-domain policy in response to external changes.
8473 //******************************************************************************
8474
8475 void
evaluatePolicy(int stimulus,uint32_t arg)8476 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8477 {
8478 union {
8479 struct {
8480 int idleSleepEnabled : 1;
8481 int idleSleepDisabled : 1;
8482 int displaySleep : 1;
8483 int sleepDelayChanged : 1;
8484 int evaluateDarkWake : 1;
8485 int adjustPowerState : 1;
8486 int userBecameInactive : 1;
8487 int displaySleepEntry : 1;
8488 } bit;
8489 uint32_t u32;
8490 } flags;
8491
8492
8493 ASSERT_GATED();
8494 flags.u32 = 0;
8495
8496 switch (stimulus) {
8497 case kStimulusDisplayWranglerSleep:
8498 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8499 if (!wranglerPowerOff) {
8500 // wrangler is in sleep state or lower
8501 flags.bit.displaySleep = true;
8502 }
8503 if (!wranglerAsleep) {
8504 // transition from wrangler wake to wrangler sleep
8505 flags.bit.displaySleepEntry = true;
8506 wranglerAsleep = true;
8507 }
8508 break;
8509
8510 case kStimulusDisplayWranglerWake:
8511 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8512 displayIdleForDemandSleep = false;
8513 wranglerPowerOff = false;
8514 wranglerAsleep = false;
8515 break;
8516
8517 case kStimulusEnterUserActiveState:
8518 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8519 if (_preventUserActive) {
8520 DLOG("user active dropped\n");
8521 break;
8522 }
8523 if (!userIsActive) {
8524 userIsActive = true;
8525 userWasActive = true;
8526 clock_get_uptime(&gUserActiveAbsTime);
8527
8528 // Stay awake after dropping demand for display power on
8529 if (kFullWakeReasonDisplayOn == fullWakeReason) {
8530 fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8531 DLOG("User activity while in notification wake\n");
8532 changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8533 }
8534
8535 kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8536 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8537 messageClients(kIOPMMessageUserIsActiveChanged);
8538 }
8539 flags.bit.idleSleepDisabled = true;
8540 break;
8541
8542 case kStimulusLeaveUserActiveState:
8543 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8544 if (userIsActive) {
8545 clock_get_uptime(&gUserInactiveAbsTime);
8546 userIsActive = false;
8547 clock_get_uptime(&userBecameInactiveTime);
8548 flags.bit.userBecameInactive = true;
8549
8550 kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8551 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8552 messageClients(kIOPMMessageUserIsActiveChanged);
8553 }
8554 break;
8555
8556 case kStimulusAggressivenessChanged:
8557 {
8558 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8559 unsigned long aggressiveValue;
8560 uint32_t minutesToIdleSleep = 0;
8561 uint32_t minutesToDisplayDim = 0;
8562 uint32_t minutesDelta = 0;
8563
8564 // Fetch latest display and system sleep slider values.
8565 aggressiveValue = 0;
8566 getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8567 minutesToIdleSleep = (uint32_t) aggressiveValue;
8568
8569 aggressiveValue = 0;
8570 getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8571 minutesToDisplayDim = (uint32_t) aggressiveValue;
8572 DLOG("aggressiveness changed: system %u->%u, display %u\n",
8573 sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8574
8575 DLOG("idle time -> %d ms (ena %d)\n",
8576 idleMilliSeconds, (minutesToIdleSleep != 0));
8577
8578 // How long to wait before sleeping the system once
8579 // the displays turns off is indicated by 'extraSleepDelay'.
8580
8581 if (minutesToIdleSleep > minutesToDisplayDim) {
8582 minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8583 } else if (minutesToIdleSleep == minutesToDisplayDim) {
8584 minutesDelta = 1;
8585 }
8586
8587 if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8588 idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8589 }
8590
8591 if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8592 flags.bit.idleSleepDisabled = true;
8593 idleSleepEnabled = false;
8594 }
8595 #if !defined(XNU_TARGET_OS_OSX)
8596 if (0x7fffffff == minutesToIdleSleep) {
8597 minutesToIdleSleep = idleMilliSeconds / 1000;
8598 }
8599 #endif /* !defined(XNU_TARGET_OS_OSX) */
8600
8601 if (((minutesDelta != extraSleepDelay) ||
8602 (userActivityTime != userActivityTime_prev)) &&
8603 !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8604 flags.bit.sleepDelayChanged = true;
8605 }
8606
8607 if (systemDarkWake && !darkWakeToSleepASAP &&
8608 (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8609 // Reconsider decision to remain in dark wake
8610 flags.bit.evaluateDarkWake = true;
8611 }
8612
8613 sleepSlider = minutesToIdleSleep;
8614 extraSleepDelay = minutesDelta;
8615 userActivityTime_prev = userActivityTime;
8616 } break;
8617
8618 case kStimulusDemandSystemSleep:
8619 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8620 displayIdleForDemandSleep = true;
8621 if (wrangler && wranglerIdleSettings) {
8622 // Request wrangler idle only when demand sleep is triggered
8623 // from full wake.
8624 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8625 wrangler->setProperties(wranglerIdleSettings.get());
8626 DLOG("Requested wrangler idle\n");
8627 }
8628 }
8629 // arg = sleepReason
8630 changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8631 break;
8632
8633 case kStimulusAllowSystemSleepChanged:
8634 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8635 flags.bit.adjustPowerState = true;
8636 break;
8637
8638 case kStimulusDarkWakeActivityTickle:
8639 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8640 // arg == true implies real and not self generated wrangler tickle.
8641 // Update wake type on PM work loop instead of the tickle thread to
8642 // eliminate the possibility of an early tickle clobbering the wake
8643 // type set by the platform driver.
8644 if (arg == true) {
8645 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8646 }
8647
8648 if (!darkWakeExit) {
8649 if (latchDisplayWranglerTickle(true)) {
8650 DLOG("latched tickle\n");
8651 break;
8652 }
8653
8654 darkWakeExit = true;
8655 DLOG("Requesting full wake due to dark wake activity tickle\n");
8656 requestFullWake( kFullWakeReasonLocalUser );
8657 }
8658 break;
8659
8660 case kStimulusDarkWakeEntry:
8661 case kStimulusDarkWakeReentry:
8662 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8663 // Any system transitions since the last dark wake transition
8664 // will invalid the stimulus.
8665
8666 if (arg == _systemStateGeneration) {
8667 DLOG("dark wake entry\n");
8668 systemDarkWake = true;
8669
8670 // Keep wranglerPowerOff an invariant when wrangler is absent
8671 if (wrangler) {
8672 wranglerPowerOff = true;
8673 }
8674
8675 if (kStimulusDarkWakeEntry == stimulus) {
8676 clock_get_uptime(&userBecameInactiveTime);
8677 flags.bit.evaluateDarkWake = true;
8678 if (activitySinceSleep()) {
8679 DLOG("User activity recorded while going to darkwake\n");
8680 reportUserInput();
8681 }
8682 }
8683
8684 // Always accelerate disk spindown while in dark wake,
8685 // even if system does not support/allow sleep.
8686
8687 cancelIdleSleepTimer();
8688 setQuickSpinDownTimeout();
8689 }
8690 break;
8691
8692 case kStimulusDarkWakeEvaluate:
8693 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8694 if (systemDarkWake) {
8695 flags.bit.evaluateDarkWake = true;
8696 }
8697 break;
8698
8699 case kStimulusNoIdleSleepPreventers:
8700 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8701 flags.bit.adjustPowerState = true;
8702 break;
8703 } /* switch(stimulus) */
8704
8705 if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8706 DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8707 darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8708 if (darkWakeToSleepASAP ||
8709 (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8710 uint32_t newSleepReason;
8711
8712 if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8713 // System was previously in full wake. Sleep reason from
8714 // full to dark already recorded in fullToDarkReason.
8715
8716 if (lowBatteryCondition) {
8717 newSleepReason = kIOPMSleepReasonLowPower;
8718 } else if (thermalEmergencyState) {
8719 newSleepReason = kIOPMSleepReasonThermalEmergency;
8720 } else {
8721 newSleepReason = fullToDarkReason;
8722 }
8723 } else {
8724 // In dark wake from system sleep.
8725
8726 if (darkWakeSleepService) {
8727 newSleepReason = kIOPMSleepReasonSleepServiceExit;
8728 } else {
8729 newSleepReason = kIOPMSleepReasonMaintenance;
8730 }
8731 }
8732
8733 if (checkSystemCanSleep(newSleepReason)) {
8734 privateSleepSystem(newSleepReason);
8735 }
8736 } else { // non-maintenance (network) dark wake
8737 if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8738 // Release power clamp, and wait for children idle.
8739 adjustPowerState(true);
8740 } else {
8741 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8742 }
8743 }
8744 }
8745
8746 if (systemDarkWake) {
8747 // The rest are irrelevant while system is in dark wake.
8748 flags.u32 = 0;
8749 }
8750
8751 if ((flags.bit.displaySleepEntry) &&
8752 (kFullWakeReasonDisplayOn == fullWakeReason)) {
8753 // kIOPMSleepReasonNotificationWakeExit
8754 DLOG("Display sleep while in notification wake\n");
8755 changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8756 }
8757
8758 if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8759 bool cancelQuickSpindown = false;
8760
8761 if (flags.bit.sleepDelayChanged) {
8762 // Cancel existing idle sleep timer and quick disk spindown.
8763 // New settings will be applied by the idleSleepEnabled flag
8764 // handler below if idle sleep is enabled.
8765
8766 DLOG("extra sleep timer changed\n");
8767 cancelIdleSleepTimer();
8768 cancelQuickSpindown = true;
8769 } else {
8770 DLOG("user inactive\n");
8771 }
8772
8773 if (!userIsActive && idleSleepEnabled) {
8774 startIdleSleepTimer(getTimeToIdleSleep());
8775 }
8776
8777 if (cancelQuickSpindown) {
8778 restoreUserSpinDownTimeout();
8779 }
8780 }
8781
8782 if (flags.bit.idleSleepEnabled) {
8783 DLOG("idle sleep timer enabled\n");
8784 if (!wrangler) {
8785 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8786 startIdleSleepTimer(getTimeToIdleSleep());
8787 #else
8788 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8789 startIdleSleepTimer( idleMilliSeconds );
8790 #endif
8791 } else {
8792 // Start idle timer if prefs now allow system sleep
8793 // and user is already inactive. Disk spindown is
8794 // accelerated upon timer expiration.
8795
8796 if (!userIsActive) {
8797 startIdleSleepTimer(getTimeToIdleSleep());
8798 }
8799 }
8800 }
8801
8802 if (flags.bit.idleSleepDisabled) {
8803 DLOG("idle sleep timer disabled\n");
8804 cancelIdleSleepTimer();
8805 restoreUserSpinDownTimeout();
8806 adjustPowerState();
8807 }
8808
8809 if (flags.bit.adjustPowerState) {
8810 bool sleepASAP = false;
8811
8812 if (!systemBooting && (0 == idleSleepPreventersCount())) {
8813 if (!wrangler) {
8814 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
8815 if (idleSleepEnabled) {
8816 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8817 if (!extraSleepDelay && !idleSleepTimerPending) {
8818 sleepASAP = true;
8819 }
8820 #else
8821 // stay awake for at least idleMilliSeconds
8822 startIdleSleepTimer(idleMilliSeconds);
8823 #endif
8824 }
8825 } else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake) {
8826 sleepASAP = true;
8827 }
8828 }
8829
8830 adjustPowerState(sleepASAP);
8831 }
8832 }
8833
8834 //******************************************************************************
8835
8836 unsigned int
idleSleepPreventersCount()8837 IOPMrootDomain::idleSleepPreventersCount()
8838 {
8839 if (_aotMode) {
8840 unsigned int count __block;
8841 count = 0;
8842 preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
8843 {
8844 count += (NULL == obj->metaCast("AppleARMBacklight"));
8845 return false;
8846 });
8847 return count;
8848 }
8849
8850 return preventIdleSleepList->getCount();
8851 }
8852
8853
8854 //******************************************************************************
8855 // requestFullWake
8856 //
8857 // Request transition from dark wake to full wake
8858 //******************************************************************************
8859
8860 void
requestFullWake(FullWakeReason reason)8861 IOPMrootDomain::requestFullWake( FullWakeReason reason )
8862 {
8863 uint32_t options = 0;
8864 IOService * pciRoot = NULL;
8865 bool promotion = false;
8866
8867 // System must be in dark wake and a valid reason for entering full wake
8868 if ((kFullWakeReasonNone == reason) ||
8869 (kFullWakeReasonNone != fullWakeReason) ||
8870 (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8871 return;
8872 }
8873
8874 // Will clear reason upon exit from full wake
8875 fullWakeReason = reason;
8876
8877 _desiredCapability |= (kIOPMSystemCapabilityGraphics |
8878 kIOPMSystemCapabilityAudio);
8879
8880 if ((kSystemTransitionWake == _systemTransitionType) &&
8881 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
8882 !darkWakePowerClamped) {
8883 // Promote to full wake while waking up to dark wake due to tickle.
8884 // PM will hold off notifying the graphics subsystem about system wake
8885 // as late as possible, so if a HID tickle does arrive, graphics can
8886 // power up from this same wake transition. Otherwise, the latency to
8887 // power up graphics on the following transition can be huge on certain
8888 // systems. However, once any power clamping has taken effect, it is
8889 // too late to promote the current dark wake transition to a full wake.
8890 _pendingCapability |= (kIOPMSystemCapabilityGraphics |
8891 kIOPMSystemCapabilityAudio);
8892
8893 // Tell the PCI parent of audio and graphics drivers to stop
8894 // delaying the child notifications. Same for root domain.
8895 pciRoot = pciHostBridgeDriver.get();
8896 willEnterFullWake();
8897 promotion = true;
8898 }
8899
8900 // Unsafe to cancel once graphics was powered.
8901 // If system woke from dark wake, the return to sleep can
8902 // be cancelled. "awake -> dark -> sleep" transition
8903 // can be cancelled also, during the "dark -> sleep" phase
8904 // *prior* to driver power down.
8905 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
8906 _pendingCapability == 0) {
8907 options |= kIOPMSyncCancelPowerDown;
8908 }
8909
8910 synchronizePowerTree(options, pciRoot);
8911
8912 if (kFullWakeReasonLocalUser == fullWakeReason) {
8913 // IOGraphics doesn't light the display even though graphics is
8914 // enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
8915 // So, do an explicit activity tickle
8916 if (wrangler) {
8917 wrangler->activityTickle(0, 0);
8918 }
8919 }
8920
8921 // Log a timestamp for the initial full wake request.
8922 // System may not always honor this full wake request.
8923 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8924 AbsoluteTime now;
8925 uint64_t nsec;
8926
8927 clock_get_uptime(&now);
8928 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8929 absolutetime_to_nanoseconds(now, &nsec);
8930 MSG("full wake %s (reason %u) %u ms\n",
8931 promotion ? "promotion" : "request",
8932 fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
8933 }
8934 }
8935
8936 //******************************************************************************
8937 // willEnterFullWake
8938 //
8939 // System will enter full wake from sleep, from dark wake, or from dark
8940 // wake promotion. This function aggregate things that are in common to
8941 // all three full wake transitions.
8942 //
8943 // Assumptions: fullWakeReason was updated
8944 //******************************************************************************
8945
8946 void
willEnterFullWake(void)8947 IOPMrootDomain::willEnterFullWake( void )
8948 {
8949 hibernateRetry = false;
8950 sleepToStandby = false;
8951 standbyNixed = false;
8952 resetTimers = false;
8953 sleepTimerMaintenance = false;
8954
8955 assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
8956
8957 _systemMessageClientMask = kSystemMessageClientPowerd |
8958 kSystemMessageClientLegacyApp;
8959
8960 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
8961 // First time to attain full wake capability since the last wake
8962 _systemMessageClientMask |= kSystemMessageClientKernel;
8963
8964 // Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
8965 setProperty(gIOPMUserTriggeredFullWakeKey.get(),
8966 (kFullWakeReasonLocalUser == fullWakeReason) ?
8967 kOSBooleanTrue : kOSBooleanFalse);
8968 }
8969 #if HIBERNATION
8970 IOHibernateSetWakeCapabilities(_pendingCapability);
8971 #endif
8972
8973 IOService::setAdvisoryTickleEnable( true );
8974 tellClients(kIOMessageSystemWillPowerOn);
8975 preventTransitionToUserActive(false);
8976 }
8977
8978 //******************************************************************************
8979 // fullWakeDelayedWork
8980 //
8981 // System has already entered full wake. Invoked by a delayed thread call.
8982 //******************************************************************************
8983
8984 void
fullWakeDelayedWork(void)8985 IOPMrootDomain::fullWakeDelayedWork( void )
8986 {
8987 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8988 if (!gIOPMWorkLoop->inGate()) {
8989 gIOPMWorkLoop->runAction(
8990 OSMemberFunctionCast(IOWorkLoop::Action, this,
8991 &IOPMrootDomain::fullWakeDelayedWork), this);
8992 return;
8993 }
8994
8995 DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
8996 _currentCapability, _pendingCapability, _highestCapability,
8997 clamshellDisabled, clamshellSleepDisableMask);
8998
8999 if (clamshellExists &&
9000 CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
9001 !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
9002 if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
9003 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
9004 } else {
9005 // Not the initial full wake after waking from sleep.
9006 // Evaluate the clamshell for rdar://problem/9157444.
9007 receivePowerNotification(kLocalEvalClamshellCommand);
9008 }
9009 }
9010 #endif
9011 }
9012
9013 //******************************************************************************
9014 // evaluateAssertions
9015 //
9016 //******************************************************************************
9017
9018 // Bitmask of all kernel assertions that prevent system idle sleep.
9019 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
9020 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
9021 (kIOPMDriverAssertionReservedBit7 | \
9022 kIOPMDriverAssertionPreventSystemIdleSleepBit)
9023
9024 void
evaluateAssertions(IOPMDriverAssertionType newAssertions,IOPMDriverAssertionType oldAssertions)9025 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
9026 {
9027 IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
9028
9029 messageClients(kIOPMMessageDriverAssertionsChanged);
9030
9031 if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
9032 if (wrangler) {
9033 bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
9034
9035 DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
9036 wrangler->setIgnoreIdleTimer( value );
9037 }
9038 }
9039
9040 if (changedBits & kIOPMDriverAssertionCPUBit) {
9041 if (_aotNow) {
9042 IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
9043 }
9044 evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
9045 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
9046 AbsoluteTime now;
9047 clock_usec_t microsecs;
9048 clock_get_uptime(&now);
9049 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9050 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
9051 if (assertOnWakeReport) {
9052 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
9053 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
9054 }
9055 }
9056 }
9057
9058 if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
9059 if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
9060 if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9061 DLOG("PreventIdleSleep driver assertion raised\n");
9062 bool ok = updatePreventIdleSleepList(this, true);
9063 if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9064 // Cancel idle sleep if there is one in progress
9065 cancelIdlePowerDown(this);
9066 }
9067 }
9068 } else {
9069 DLOG("PreventIdleSleep driver assertion dropped\n");
9070 updatePreventIdleSleepList(this, false);
9071 }
9072 }
9073 }
9074
9075 // MARK: -
9076 // MARK: Statistics
9077
9078 //******************************************************************************
9079 // pmStats
9080 //
9081 //******************************************************************************
9082
9083 void
pmStatsRecordEvent(int eventIndex,AbsoluteTime timestamp)9084 IOPMrootDomain::pmStatsRecordEvent(
9085 int eventIndex,
9086 AbsoluteTime timestamp)
9087 {
9088 bool starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9089 bool stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9090 uint64_t delta;
9091 uint64_t nsec;
9092 OSSharedPtr<OSData> publishPMStats;
9093
9094 eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9095
9096 absolutetime_to_nanoseconds(timestamp, &nsec);
9097
9098 switch (eventIndex) {
9099 case kIOPMStatsHibernateImageWrite:
9100 if (starting) {
9101 gPMStats.hibWrite.start = nsec;
9102 } else if (stopping) {
9103 gPMStats.hibWrite.stop = nsec;
9104 }
9105
9106 if (stopping) {
9107 delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9108 IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9109 }
9110 break;
9111 case kIOPMStatsHibernateImageRead:
9112 if (starting) {
9113 gPMStats.hibRead.start = nsec;
9114 } else if (stopping) {
9115 gPMStats.hibRead.stop = nsec;
9116 }
9117
9118 if (stopping) {
9119 delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9120 IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9121
9122 publishPMStats = OSData::withValue(gPMStats);
9123 setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9124 bzero(&gPMStats, sizeof(gPMStats));
9125 }
9126 break;
9127 }
9128 }
9129
9130 /*
9131 * Appends a record of the application response to
9132 * IOPMrootDomain::pmStatsAppResponses
9133 */
9134 void
pmStatsRecordApplicationResponse(const OSSymbol * response,const char * name,int messageType,uint32_t delay_ms,uint64_t id,OSObject * object,IOPMPowerStateIndex powerState,bool async)9135 IOPMrootDomain::pmStatsRecordApplicationResponse(
9136 const OSSymbol *response,
9137 const char *name,
9138 int messageType,
9139 uint32_t delay_ms,
9140 uint64_t id,
9141 OSObject *object,
9142 IOPMPowerStateIndex powerState,
9143 bool async)
9144 {
9145 OSSharedPtr<OSDictionary> responseDescription;
9146 OSSharedPtr<OSNumber> delayNum;
9147 OSSharedPtr<OSNumber> powerCaps;
9148 OSSharedPtr<OSNumber> pidNum;
9149 OSSharedPtr<OSNumber> msgNum;
9150 OSSharedPtr<const OSSymbol> appname;
9151 OSSharedPtr<const OSSymbol> sleep;
9152 OSSharedPtr<const OSSymbol> wake;
9153 IOPMServiceInterestNotifier *notify = NULL;
9154
9155 if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9156 if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9157 notify->ackTimeoutCnt++;
9158 } else {
9159 notify->ackTimeoutCnt = 0;
9160 }
9161 }
9162
9163 if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9164 (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9165 return;
9166 }
9167
9168
9169 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9170 kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9171 } else if (notify) {
9172 // User space app or kernel capability client
9173 if (id) {
9174 kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9175 } else {
9176 kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9177 }
9178 notify->msgType = 0;
9179 }
9180
9181 responseDescription = OSDictionary::withCapacity(5);
9182 if (responseDescription) {
9183 if (response) {
9184 responseDescription->setObject(_statsResponseTypeKey.get(), response);
9185 }
9186
9187 msgNum = OSNumber::withNumber(messageType, 32);
9188 if (msgNum) {
9189 responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9190 }
9191
9192 if (!name && notify && notify->identifier) {
9193 name = notify->identifier->getCStringNoCopy();
9194 }
9195
9196 if (name && (strlen(name) > 0)) {
9197 appname = OSSymbol::withCString(name);
9198 if (appname) {
9199 responseDescription->setObject(_statsNameKey.get(), appname.get());
9200 }
9201 }
9202
9203 if (!id && notify) {
9204 id = notify->uuid0;
9205 }
9206 if (id != 0) {
9207 pidNum = OSNumber::withNumber(id, 64);
9208 if (pidNum) {
9209 responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9210 }
9211 }
9212
9213 delayNum = OSNumber::withNumber(delay_ms, 32);
9214 if (delayNum) {
9215 responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9216 }
9217
9218 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9219 powerCaps = OSNumber::withNumber(powerState, 32);
9220
9221 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9222 static const char * driverCallTypes[] = {
9223 [kDriverCallInformPreChange] = "powerStateWillChangeTo",
9224 [kDriverCallInformPostChange] = "powerStateDidChangeTo",
9225 [kDriverCallSetPowerState] = "setPowerState"
9226 };
9227
9228 if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9229 DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9230 name, id, driverCallTypes[messageType], (uint32_t) powerState,
9231 async ? "async " : "", delay_ms);
9232 }
9233 #endif
9234 } else {
9235 powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9236 }
9237 if (powerCaps) {
9238 responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9239 }
9240
9241 sleep = OSSymbol::withCString("Sleep");
9242 wake = OSSymbol::withCString("Wake");
9243 if (_systemTransitionType == kSystemTransitionSleep) {
9244 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9245 } else if (_systemTransitionType == kSystemTransitionWake) {
9246 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9247 } else if (_systemTransitionType == kSystemTransitionCapability) {
9248 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9249 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9250 } else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9251 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9252 }
9253 }
9254
9255 IOLockLock(pmStatsLock);
9256 if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9257 pmStatsAppResponses->setObject(responseDescription.get());
9258 }
9259 IOLockUnlock(pmStatsLock);
9260 }
9261
9262 return;
9263 }
9264
9265 // MARK: -
9266 // MARK: PMTraceWorker
9267
9268 //******************************************************************************
9269 // TracePoint support
9270 //
9271 //******************************************************************************
9272
9273 #define kIOPMRegisterNVRAMTracePointHandlerKey \
9274 "IOPMRegisterNVRAMTracePointHandler"
9275
9276 IOReturn
callPlatformFunction(const OSSymbol * functionName,bool waitForFunction,void * param1,void * param2,void * param3,void * param4)9277 IOPMrootDomain::callPlatformFunction(
9278 const OSSymbol * functionName,
9279 bool waitForFunction,
9280 void * param1, void * param2,
9281 void * param3, void * param4 )
9282 {
9283 if (pmTracer && functionName &&
9284 functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9285 !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9286 uint32_t tracePointPhases, tracePointPCI;
9287 uint64_t statusCode;
9288
9289 pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9290 pmTracer->tracePointTarget = (void *) param2;
9291 tracePointPCI = (uint32_t)(uintptr_t) param3;
9292 tracePointPhases = (uint32_t)(uintptr_t) param4;
9293 if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9294 OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9295 if (node) {
9296 OSSharedPtr<OSObject> bootRomFailureProp;
9297 bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9298 OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9299 uint32_t bootFailureCode;
9300 if (data && data->getLength() == sizeof(bootFailureCode)) {
9301 // Failure code from EFI/BootRom is a four byte structure
9302 memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9303 tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9304 }
9305 }
9306 }
9307 statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9308 if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9309 MSG("Sleep failure code 0x%08x 0x%08x\n",
9310 tracePointPCI, tracePointPhases);
9311 }
9312 setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9313 pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9314
9315 return kIOReturnSuccess;
9316 }
9317 #if HIBERNATION
9318 else if (functionName &&
9319 functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9320 if (gSleepPolicyHandler) {
9321 return kIOReturnExclusiveAccess;
9322 }
9323 if (!param1) {
9324 return kIOReturnBadArgument;
9325 }
9326 gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9327 gSleepPolicyTarget = (void *) param2;
9328 setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9329 return kIOReturnSuccess;
9330 }
9331 #endif
9332
9333 return super::callPlatformFunction(
9334 functionName, waitForFunction, param1, param2, param3, param4);
9335 }
9336
9337 void
kdebugTrace(uint32_t event,uint64_t id,uintptr_t param1,uintptr_t param2,uintptr_t param3)9338 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9339 uintptr_t param1, uintptr_t param2, uintptr_t param3)
9340 {
9341 uint32_t code = IODBG_POWER(event);
9342 uint64_t regId = id;
9343 if (regId == 0) {
9344 regId = getRegistryEntryID();
9345 }
9346 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9347 }
9348
9349 void
tracePoint(uint8_t point)9350 IOPMrootDomain::tracePoint( uint8_t point )
9351 {
9352 if (systemBooting) {
9353 return;
9354 }
9355
9356 if (kIOPMTracePointWakeCapabilityClients == point) {
9357 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9358 }
9359
9360 kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9361 pmTracer->tracePoint(point);
9362 }
9363
9364 static void
kext_log_putc(char c)9365 kext_log_putc(char c)
9366 {
9367 if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9368 return;
9369 }
9370 if (c == '(' || c == '[' || c == ' ') {
9371 c = 0;
9372 gKextNameEnd = true;
9373 }
9374
9375 gKextNameBuf[gKextNamePos++] = c;
9376 }
9377
9378 static int
kext_log(const char * fmt,...)9379 kext_log(const char *fmt, ...)
9380 {
9381 va_list listp;
9382
9383 va_start(listp, fmt);
9384 _doprnt(fmt, &listp, &kext_log_putc, 16);
9385 va_end(listp);
9386
9387 return 0;
9388 }
9389
9390 static OSPtr<const OSSymbol>
copyKextIdentifierWithAddress(vm_address_t address)9391 copyKextIdentifierWithAddress(vm_address_t address)
9392 {
9393 OSSharedPtr<const OSSymbol> identifer;
9394
9395 IOLockLock(gHaltLogLock);
9396
9397 gKextNameEnd = false;
9398 gKextNamePos = 0;
9399 gKextNameBuf[0] = 0;
9400
9401 OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9402 gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9403 identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9404
9405 IOLockUnlock(gHaltLogLock);
9406
9407 return identifer;
9408 }
9409
9410 // Caller serialized using PM workloop
9411 const char *
getNotificationClientName(OSObject * object)9412 IOPMrootDomain::getNotificationClientName(OSObject *object)
9413 {
9414 IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9415 const char *clientName = "UNKNOWN";
9416
9417 if (!notifier->clientName) {
9418 // Check for user client
9419 if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9420 OSNumber *clientID = NULL;
9421 messageClient(kIOMessageCopyClientID, object, &clientID);
9422 if (clientID) {
9423 OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9424 if (string) {
9425 notifier->clientName = OSSymbol::withString(string.get());
9426 }
9427 clientID->release();
9428 }
9429 } else if (notifier->identifier) {
9430 notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9431 }
9432 }
9433
9434 if (notifier->clientName) {
9435 clientName = notifier->clientName->getCStringNoCopy();
9436 }
9437
9438 return clientName;
9439 }
9440
9441 void
traceNotification(OSObject * object,bool start,uint64_t timestamp,uint32_t msgIndex)9442 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9443 {
9444 IOPMServiceInterestNotifier *notifier;
9445
9446 if (systemBooting) {
9447 return;
9448 }
9449 notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9450 if (!notifier) {
9451 return;
9452 }
9453
9454 if (start) {
9455 pmTracer->traceDetail(notifier->uuid0 >> 32);
9456 kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9457 (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9458
9459 // Update notifier state used for response/ack logging
9460 notifier->msgIndex = msgIndex;
9461 notifier->msgAbsTime = timestamp;
9462
9463 if (msgIndex != UINT_MAX) {
9464 DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9465 } else {
9466 DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9467 }
9468
9469 assert(notifierObject == NULL);
9470 notifierThread = current_thread();
9471 notifierObject.reset(notifier, OSRetain);
9472 } else {
9473 uint64_t nsec;
9474 uint32_t delayMS;
9475
9476 SUB_ABSOLUTETIME(×tamp, ¬ifier->msgAbsTime);
9477 absolutetime_to_nanoseconds(timestamp, &nsec);
9478 delayMS = (uint32_t)(nsec / 1000000ULL);
9479 if (delayMS > notifier->maxMsgDelayMS) {
9480 notifier->maxMsgDelayMS = delayMS;
9481 }
9482
9483 assert(notifierObject == notifier);
9484 notifierObject.reset();
9485 notifierThread = NULL;
9486 }
9487 }
9488
9489 void
traceNotificationAck(OSObject * object,uint32_t delay_ms)9490 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9491 {
9492 if (systemBooting) {
9493 return;
9494 }
9495 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9496 if (!notifier) {
9497 return;
9498 }
9499
9500 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9501 (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9502
9503 DLOG("%s[%u] ack from %s took %d ms\n",
9504 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9505 if (delay_ms > notifier->maxAckDelayMS) {
9506 notifier->maxAckDelayMS = delay_ms;
9507 }
9508 }
9509
9510 void
traceNotificationResponse(OSObject * object,uint32_t delay_ms,uint32_t ack_time_us)9511 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9512 {
9513 if (systemBooting) {
9514 return;
9515 }
9516 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9517 if (!notifier) {
9518 return;
9519 }
9520
9521 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9522 (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9523
9524 if (ack_time_us == 0) {
9525 // Client work is done and ack will not be forthcoming
9526 DLOG("%s[%u] response from %s took %d ms\n",
9527 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9528 } else {
9529 // Client needs more time and it must ack within ack_time_us
9530 DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9531 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9532 }
9533 }
9534
9535 void
traceFilteredNotification(OSObject * object)9536 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9537 {
9538 if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9539 return;
9540 }
9541 if (systemBooting) {
9542 return;
9543 }
9544 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9545 if (!notifier) {
9546 return;
9547 }
9548
9549 DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9550 }
9551
9552 void
traceDetail(uint32_t msgType,uint32_t msgIndex,uint32_t delay)9553 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9554 {
9555 if (!systemBooting) {
9556 uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9557 pmTracer->traceDetail( detail );
9558 kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9559 DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9560 }
9561 }
9562
9563 void
configureReportGated(uint64_t channel_id,uint64_t action,void * result)9564 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9565 {
9566 size_t reportSize;
9567 void **report = NULL;
9568 uint32_t bktCnt;
9569 uint32_t bktSize;
9570 uint32_t *clientCnt;
9571
9572 ASSERT_GATED();
9573
9574 report = NULL;
9575 if (channel_id == kAssertDelayChID) {
9576 report = &assertOnWakeReport;
9577 bktCnt = kAssertDelayBcktCnt;
9578 bktSize = kAssertDelayBcktSize;
9579 clientCnt = &assertOnWakeClientCnt;
9580 } else if (channel_id == kSleepDelaysChID) {
9581 report = &sleepDelaysReport;
9582 bktCnt = kSleepDelaysBcktCnt;
9583 bktSize = kSleepDelaysBcktSize;
9584 clientCnt = &sleepDelaysClientCnt;
9585 } else {
9586 assert(false);
9587 return;
9588 }
9589
9590 switch (action) {
9591 case kIOReportEnable:
9592
9593 if (*report) {
9594 (*clientCnt)++;
9595 break;
9596 }
9597
9598 reportSize = HISTREPORT_BUFSIZE(bktCnt);
9599 *report = IOMallocZeroData(reportSize);
9600 if (*report == NULL) {
9601 break;
9602 }
9603 HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9604 getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9605
9606 if (channel_id == kAssertDelayChID) {
9607 assertOnWakeSecs = 0;
9608 }
9609
9610 break;
9611
9612 case kIOReportDisable:
9613 if (*clientCnt == 0) {
9614 break;
9615 }
9616 if (*clientCnt == 1) {
9617 IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9618 *report = NULL;
9619 }
9620 (*clientCnt)--;
9621
9622 if (channel_id == kAssertDelayChID) {
9623 assertOnWakeSecs = -1; // Invalid value to prevent updates
9624 }
9625 break;
9626
9627 case kIOReportGetDimensions:
9628 if (*report) {
9629 HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9630 }
9631 break;
9632 }
9633
9634 return;
9635 }
9636
9637 IOReturn
configureReport(IOReportChannelList * channelList,IOReportConfigureAction action,void * result,void * destination)9638 IOPMrootDomain::configureReport(IOReportChannelList *channelList,
9639 IOReportConfigureAction action,
9640 void *result,
9641 void *destination)
9642 {
9643 unsigned cnt;
9644 uint64_t configAction = (uint64_t)action;
9645
9646 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9647 if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9648 (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9649 (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9650 if (action != kIOReportGetDimensions) {
9651 continue;
9652 }
9653 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9654 } else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9655 (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9656 gIOPMWorkLoop->runAction(
9657 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9658 (OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9659 (void *)configAction, (void *)result);
9660 }
9661 }
9662
9663 return super::configureReport(channelList, action, result, destination);
9664 }
9665
9666 IOReturn
updateReportGated(uint64_t ch_id,void * result,IOBufferMemoryDescriptor * dest)9667 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9668 {
9669 uint32_t size2cpy;
9670 void *data2cpy;
9671 void **report;
9672
9673 ASSERT_GATED();
9674
9675 report = NULL;
9676 if (ch_id == kAssertDelayChID) {
9677 report = &assertOnWakeReport;
9678 } else if (ch_id == kSleepDelaysChID) {
9679 report = &sleepDelaysReport;
9680 } else {
9681 assert(false);
9682 return kIOReturnBadArgument;
9683 }
9684
9685 if (*report == NULL) {
9686 return kIOReturnNotOpen;
9687 }
9688
9689 HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9690 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9691 return kIOReturnOverrun;
9692 }
9693
9694 HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9695 dest->appendBytes(data2cpy, size2cpy);
9696
9697 return kIOReturnSuccess;
9698 }
9699
9700 IOReturn
updateReport(IOReportChannelList * channelList,IOReportUpdateAction action,void * result,void * destination)9701 IOPMrootDomain::updateReport(IOReportChannelList *channelList,
9702 IOReportUpdateAction action,
9703 void *result,
9704 void *destination)
9705 {
9706 uint32_t size2cpy;
9707 void *data2cpy;
9708 uint8_t buf[SIMPLEREPORT_BUFSIZE];
9709 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9710 unsigned cnt;
9711 uint64_t ch_id;
9712
9713 if (action != kIOReportCopyChannelData) {
9714 goto exit;
9715 }
9716
9717 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9718 ch_id = channelList->channels[cnt].channel_id;
9719
9720 if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9721 gIOPMWorkLoop->runAction(
9722 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9723 (OSObject *)this, (void *)ch_id,
9724 (void *)result, (void *)dest);
9725 continue;
9726 } else if ((ch_id == kSleepCntChID) ||
9727 (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9728 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9729 } else {
9730 continue;
9731 }
9732
9733 if (ch_id == kSleepCntChID) {
9734 SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9735 } else if (ch_id == kDarkWkCntChID) {
9736 SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9737 } else if (ch_id == kUserWkCntChID) {
9738 SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9739 }
9740
9741 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9742 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9743 dest->appendBytes(data2cpy, size2cpy);
9744 }
9745
9746 exit:
9747 return super::updateReport(channelList, action, result, destination);
9748 }
9749
9750
9751 //******************************************************************************
9752 // PMTraceWorker Class
9753 //
9754 //******************************************************************************
9755
9756 #undef super
9757 #define super OSObject
OSDefineMetaClassAndStructors(PMTraceWorker,OSObject)9758 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9759
9760 #define kPMBestGuessPCIDevicesCount 25
9761 #define kPMMaxRTCBitfieldSize 32
9762
9763 OSPtr<PMTraceWorker>
9764 PMTraceWorker::tracer(IOPMrootDomain * owner)
9765 {
9766 OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9767 if (!me || !me->init()) {
9768 return NULL;
9769 }
9770
9771 DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9772
9773 // Note that we cannot instantiate the PCI device -> bit mappings here, since
9774 // the IODeviceTree has not yet been created by IOPlatformExpert. We create
9775 // this dictionary lazily.
9776 me->owner = owner;
9777 me->pciDeviceBitMappings = NULL;
9778 me->pmTraceWorkerLock = IOLockAlloc();
9779 me->tracePhase = kIOPMTracePointSystemUp;
9780 me->traceData32 = 0;
9781 me->loginWindowData = 0;
9782 me->coreDisplayData = 0;
9783 me->coreGraphicsData = 0;
9784 return me;
9785 }
9786
9787 void
RTC_TRACE(void)9788 PMTraceWorker::RTC_TRACE(void)
9789 {
9790 if (tracePointHandler && tracePointTarget) {
9791 uint32_t wordA;
9792
9793 IOLockLock(pmTraceWorkerLock);
9794 wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9795 (coreGraphicsData << 8) | tracePhase;
9796 IOLockUnlock(pmTraceWorkerLock);
9797
9798 tracePointHandler( tracePointTarget, traceData32, wordA );
9799 _LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9800 }
9801 #if DEVELOPMENT || DEBUG
9802 if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9803 DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9804 IOLock *l = IOLockAlloc();
9805 IOLockLock(l);
9806 IOLockLock(l);
9807 }
9808 #endif
9809 }
9810
9811 int
recordTopLevelPCIDevice(IOService * pciDevice)9812 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
9813 {
9814 OSSharedPtr<const OSSymbol> deviceName;
9815 int index = -1;
9816
9817 IOLockLock(pmTraceWorkerLock);
9818
9819 if (!pciDeviceBitMappings) {
9820 pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
9821 if (!pciDeviceBitMappings) {
9822 goto exit;
9823 }
9824 }
9825
9826 // Check for bitmask overflow.
9827 if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
9828 goto exit;
9829 }
9830
9831 if ((deviceName = pciDevice->copyName()) &&
9832 (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
9833 pciDeviceBitMappings->setObject(deviceName.get())) {
9834 index = pciDeviceBitMappings->getCount() - 1;
9835 _LOG("PMTrace PCI array: set object %s => %d\n",
9836 deviceName->getCStringNoCopy(), index);
9837 }
9838
9839 if (!addedToRegistry && (index >= 0)) {
9840 addedToRegistry = owner->setProperty("PCITopLevel", this);
9841 }
9842
9843 exit:
9844 IOLockUnlock(pmTraceWorkerLock);
9845 return index;
9846 }
9847
9848 bool
serialize(OSSerialize * s) const9849 PMTraceWorker::serialize(OSSerialize *s) const
9850 {
9851 bool ok = false;
9852 if (pciDeviceBitMappings) {
9853 IOLockLock(pmTraceWorkerLock);
9854 ok = pciDeviceBitMappings->serialize(s);
9855 IOLockUnlock(pmTraceWorkerLock);
9856 }
9857 return ok;
9858 }
9859
9860 void
tracePoint(uint8_t phase)9861 PMTraceWorker::tracePoint(uint8_t phase)
9862 {
9863 // clear trace detail when phase begins
9864 if (tracePhase != phase) {
9865 traceData32 = 0;
9866 }
9867
9868 tracePhase = phase;
9869
9870 DLOG("trace point 0x%02x\n", tracePhase);
9871 RTC_TRACE();
9872 }
9873
9874 void
traceDetail(uint32_t detail)9875 PMTraceWorker::traceDetail(uint32_t detail)
9876 {
9877 if (detail == traceData32) {
9878 return;
9879 }
9880 traceData32 = detail;
9881 RTC_TRACE();
9882 }
9883
9884 void
traceComponentWakeProgress(uint32_t component,uint32_t data)9885 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
9886 {
9887 switch (component) {
9888 case kIOPMLoginWindowProgress:
9889 loginWindowData = data & kIOPMLoginWindowProgressMask;
9890 break;
9891 case kIOPMCoreDisplayProgress:
9892 coreDisplayData = data & kIOPMCoreDisplayProgressMask;
9893 break;
9894 case kIOPMCoreGraphicsProgress:
9895 coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
9896 break;
9897 default:
9898 return;
9899 }
9900
9901 DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
9902 RTC_TRACE();
9903 }
9904
9905 void
tracePCIPowerChange(change_t type,IOService * service,uint32_t changeFlags,uint32_t bitNum)9906 PMTraceWorker::tracePCIPowerChange(
9907 change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
9908 {
9909 uint32_t bitMask;
9910 uint32_t expectedFlag;
9911
9912 // Ignore PCI changes outside of system sleep/wake.
9913 if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
9914 (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
9915 return;
9916 }
9917
9918 // Only record the WillChange transition when going to sleep,
9919 // and the DidChange on the way up.
9920 changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
9921 expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
9922 kIOPMDomainWillChange : kIOPMDomainDidChange;
9923 if (changeFlags != expectedFlag) {
9924 return;
9925 }
9926
9927 // Mark this device off in our bitfield
9928 if (bitNum < kPMMaxRTCBitfieldSize) {
9929 bitMask = (1 << bitNum);
9930
9931 if (kPowerChangeStart == type) {
9932 traceData32 |= bitMask;
9933 _LOG("PMTrace: Device %s started - bit %2d mask 0x%08x => 0x%08x\n",
9934 service->getName(), bitNum, bitMask, traceData32);
9935 owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
9936 } else {
9937 traceData32 &= ~bitMask;
9938 _LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
9939 service->getName(), bitNum, bitMask, traceData32);
9940 owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
9941 }
9942
9943 DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
9944 RTC_TRACE();
9945 }
9946 }
9947
9948 uint64_t
getPMStatusCode()9949 PMTraceWorker::getPMStatusCode()
9950 {
9951 return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
9952 }
9953
9954 uint8_t
getTracePhase()9955 PMTraceWorker::getTracePhase()
9956 {
9957 return tracePhase;
9958 }
9959
9960 uint32_t
getTraceData()9961 PMTraceWorker::getTraceData()
9962 {
9963 return traceData32;
9964 }
9965
9966 // MARK: -
9967 // MARK: PMHaltWorker
9968
9969 //******************************************************************************
9970 // PMHaltWorker Class
9971 //
9972 //******************************************************************************
9973
9974 PMHaltWorker *
worker(void)9975 PMHaltWorker::worker( void )
9976 {
9977 PMHaltWorker * me;
9978 IOThread thread;
9979
9980 do {
9981 me = OSTypeAlloc( PMHaltWorker );
9982 if (!me || !me->init()) {
9983 break;
9984 }
9985
9986 me->lock = IOLockAlloc();
9987 if (!me->lock) {
9988 break;
9989 }
9990
9991 DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
9992 me->retain(); // thread holds extra retain
9993 if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
9994 me->release();
9995 break;
9996 }
9997 thread_deallocate(thread);
9998 return me;
9999 } while (false);
10000
10001 if (me) {
10002 me->release();
10003 }
10004 return NULL;
10005 }
10006
10007 void
free(void)10008 PMHaltWorker::free( void )
10009 {
10010 DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
10011 if (lock) {
10012 IOLockFree(lock);
10013 lock = NULL;
10014 }
10015 return OSObject::free();
10016 }
10017
10018 void
main(void * arg,wait_result_t waitResult)10019 PMHaltWorker::main( void * arg, wait_result_t waitResult )
10020 {
10021 PMHaltWorker * me = (PMHaltWorker *) arg;
10022
10023 IOLockLock( gPMHaltLock );
10024 gPMHaltBusyCount++;
10025 me->depth = gPMHaltDepth;
10026 IOLockUnlock( gPMHaltLock );
10027
10028 while (me->depth >= 0) {
10029 PMHaltWorker::work( me );
10030
10031 IOLockLock( gPMHaltLock );
10032 if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
10033 // This is the last thread to finish work on this level,
10034 // inform everyone to start working on next lower level.
10035 gPMHaltDepth--;
10036 me->depth = gPMHaltDepth;
10037 gPMHaltIdleCount = 0;
10038 thread_wakeup((event_t) &gPMHaltIdleCount);
10039 } else {
10040 // One or more threads are still working on this level,
10041 // this thread must wait.
10042 me->depth = gPMHaltDepth - 1;
10043 do {
10044 IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
10045 } while (me->depth != gPMHaltDepth);
10046 }
10047 IOLockUnlock( gPMHaltLock );
10048 }
10049
10050 // No more work to do, terminate thread
10051 DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
10052 thread_wakeup( &gPMHaltDepth );
10053 me->release();
10054 }
10055
10056 void
work(PMHaltWorker * me)10057 PMHaltWorker::work( PMHaltWorker * me )
10058 {
10059 OSSharedPtr<IOService> service;
10060 OSSet * inner;
10061 AbsoluteTime startTime, elapsedTime;
10062 UInt32 deltaTime;
10063 bool timeout;
10064
10065 while (true) {
10066 timeout = false;
10067
10068 // Claim an unit of work from the shared pool
10069 IOLockLock( gPMHaltLock );
10070 inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10071 if (inner) {
10072 service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10073 if (service) {
10074 inner->removeObject(service.get());
10075 }
10076 }
10077 IOLockUnlock( gPMHaltLock );
10078 if (!service) {
10079 break; // no more work at this depth
10080 }
10081 clock_get_uptime(&startTime);
10082
10083 if (!service->isInactive() &&
10084 service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10085 IOLockLock(me->lock);
10086 me->startTime = startTime;
10087 me->service = service.get();
10088 me->timeout = false;
10089 IOLockUnlock(me->lock);
10090
10091 service->systemWillShutdown( gPMHaltMessageType);
10092
10093 // Wait for driver acknowledgement
10094 IOLockLock(me->lock);
10095 while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10096 IOLockSleep(me->lock, me, THREAD_UNINT);
10097 }
10098 me->service = NULL;
10099 timeout = me->timeout;
10100 IOLockUnlock(me->lock);
10101 }
10102
10103 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10104 if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10105 LOG("%s driver %s (0x%llx) took %u ms\n",
10106 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10107 "PowerOff" : "Restart",
10108 service->getName(), service->getRegistryEntryID(),
10109 (uint32_t) deltaTime );
10110 halt_log_enter("PowerOff/Restart handler completed",
10111 OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10112 elapsedTime);
10113 }
10114
10115 me->visits++;
10116 }
10117 }
10118
10119 void
checkTimeout(PMHaltWorker * me,AbsoluteTime * now)10120 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10121 {
10122 UInt64 nano;
10123 AbsoluteTime startTime;
10124 AbsoluteTime endTime;
10125
10126 endTime = *now;
10127
10128 IOLockLock(me->lock);
10129 if (me->service && !me->timeout) {
10130 startTime = me->startTime;
10131 nano = 0;
10132 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10133 SUB_ABSOLUTETIME(&endTime, &startTime);
10134 absolutetime_to_nanoseconds(endTime, &nano);
10135 }
10136 if (nano > 3000000000ULL) {
10137 me->timeout = true;
10138
10139 halt_log_enter("PowerOff/Restart still waiting on handler",
10140 OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10141 endTime);
10142 MSG("%s still waiting on %s\n",
10143 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ? "PowerOff" : "Restart",
10144 me->service->getName());
10145 }
10146 }
10147 IOLockUnlock(me->lock);
10148 }
10149
10150 //******************************************************************************
10151 // acknowledgeSystemWillShutdown
10152 //
10153 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10154 //******************************************************************************
10155
10156 void
acknowledgeSystemWillShutdown(IOService * from)10157 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10158 {
10159 PMHaltWorker * worker;
10160 OSSharedPtr<OSObject> prop;
10161
10162 if (!from) {
10163 return;
10164 }
10165
10166 //DLOG("%s acknowledged\n", from->getName());
10167 prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10168 if (prop) {
10169 worker = (PMHaltWorker *) prop.get();
10170 IOLockLock(worker->lock);
10171 from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10172 thread_wakeup((event_t) worker);
10173 IOLockUnlock(worker->lock);
10174 } else {
10175 DLOG("%s acknowledged without worker property\n",
10176 from->getName());
10177 }
10178 }
10179
10180
10181 //******************************************************************************
10182 // notifySystemShutdown
10183 //
10184 // Notify all objects in PM tree that system will shutdown or restart
10185 //******************************************************************************
10186
10187 static void
notifySystemShutdown(IOService * root,uint32_t messageType)10188 notifySystemShutdown( IOService * root, uint32_t messageType )
10189 {
10190 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10191 OSSharedPtr<IORegistryIterator> iter;
10192 IORegistryEntry * entry;
10193 IOService * node;
10194 OSSet * inner;
10195 OSSharedPtr<OSSet> newInner;
10196 PMHaltWorker * workers[kPMHaltMaxWorkers];
10197 AbsoluteTime deadline;
10198 unsigned int totalNodes = 0;
10199 unsigned int depth;
10200 unsigned int rootDepth;
10201 unsigned int numWorkers;
10202 unsigned int count;
10203 int waitResult;
10204 void * baseFunc;
10205 bool ok;
10206
10207 DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10208
10209 baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10210
10211 // Iterate the entire PM tree starting from root
10212
10213 rootDepth = root->getDepth( gIOPowerPlane );
10214 if (!rootDepth) {
10215 goto done;
10216 }
10217
10218 // debug - for repeated test runs
10219 while (PMHaltWorker::metaClass->getInstanceCount()) {
10220 IOSleep(1);
10221 }
10222
10223 if (!gPMHaltArray) {
10224 gPMHaltArray = OSArray::withCapacity(40);
10225 if (!gPMHaltArray) {
10226 goto done;
10227 }
10228 } else { // debug
10229 gPMHaltArray->flushCollection();
10230 }
10231
10232 if (!gPMHaltLock) {
10233 gPMHaltLock = IOLockAlloc();
10234 if (!gPMHaltLock) {
10235 goto done;
10236 }
10237 }
10238
10239 if (!gPMHaltClientAcknowledgeKey) {
10240 gPMHaltClientAcknowledgeKey =
10241 OSSymbol::withCStringNoCopy("PMShutdown");
10242 if (!gPMHaltClientAcknowledgeKey) {
10243 goto done;
10244 }
10245 }
10246
10247 gPMHaltMessageType = messageType;
10248
10249 // Depth-first walk of PM plane
10250
10251 iter = IORegistryIterator::iterateOver(
10252 root, gIOPowerPlane, kIORegistryIterateRecursively);
10253
10254 if (iter) {
10255 while ((entry = iter->getNextObject())) {
10256 node = OSDynamicCast(IOService, entry);
10257 if (!node) {
10258 continue;
10259 }
10260
10261 if (baseFunc ==
10262 OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10263 continue;
10264 }
10265
10266 depth = node->getDepth( gIOPowerPlane );
10267 if (depth <= rootDepth) {
10268 continue;
10269 }
10270
10271 ok = false;
10272
10273 // adjust to zero based depth
10274 depth -= (rootDepth + 1);
10275
10276 // gPMHaltArray is an array of containers, each container
10277 // refers to nodes with the same depth.
10278
10279 count = gPMHaltArray->getCount();
10280 while (depth >= count) {
10281 // expand array and insert placeholders
10282 gPMHaltArray->setObject(PLACEHOLDER);
10283 count++;
10284 }
10285 count = gPMHaltArray->getCount();
10286 if (depth < count) {
10287 inner = (OSSet *)gPMHaltArray->getObject(depth);
10288 if (inner == PLACEHOLDER) {
10289 newInner = OSSet::withCapacity(40);
10290 if (newInner) {
10291 gPMHaltArray->replaceObject(depth, newInner.get());
10292 inner = newInner.get();
10293 }
10294 }
10295
10296 // PM nodes that appear more than once in the tree will have
10297 // the same depth, OSSet will refuse to add the node twice.
10298 if (inner) {
10299 ok = inner->setObject(node);
10300 }
10301 }
10302 if (!ok) {
10303 DLOG("Skipped PM node %s\n", node->getName());
10304 }
10305 }
10306 }
10307
10308 // debug only
10309 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10310 count = 0;
10311 if (inner != PLACEHOLDER) {
10312 count = inner->getCount();
10313 }
10314 DLOG("Nodes at depth %u = %u\n", i, count);
10315 }
10316
10317 // strip placeholders (not all depths are populated)
10318 numWorkers = 0;
10319 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10320 if (inner == PLACEHOLDER) {
10321 gPMHaltArray->removeObject(i);
10322 continue;
10323 }
10324 count = inner->getCount();
10325 if (count > numWorkers) {
10326 numWorkers = count;
10327 }
10328 totalNodes += count;
10329 i++;
10330 }
10331
10332 if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10333 goto done;
10334 }
10335
10336 gPMHaltBusyCount = 0;
10337 gPMHaltIdleCount = 0;
10338 gPMHaltDepth = gPMHaltArray->getCount() - 1;
10339
10340 // Create multiple workers (and threads)
10341
10342 if (numWorkers > kPMHaltMaxWorkers) {
10343 numWorkers = kPMHaltMaxWorkers;
10344 }
10345
10346 DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10347 totalNodes, gPMHaltArray->getCount(), numWorkers);
10348
10349 for (unsigned int i = 0; i < numWorkers; i++) {
10350 workers[i] = PMHaltWorker::worker();
10351 }
10352
10353 // Wait for workers to exhaust all available work
10354
10355 IOLockLock(gPMHaltLock);
10356 while (gPMHaltDepth >= 0) {
10357 clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10358
10359 waitResult = IOLockSleepDeadline(
10360 gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10361 if (THREAD_TIMED_OUT == waitResult) {
10362 AbsoluteTime now;
10363 clock_get_uptime(&now);
10364
10365 IOLockUnlock(gPMHaltLock);
10366 for (unsigned int i = 0; i < numWorkers; i++) {
10367 if (workers[i]) {
10368 PMHaltWorker::checkTimeout(workers[i], &now);
10369 }
10370 }
10371 IOLockLock(gPMHaltLock);
10372 }
10373 }
10374 IOLockUnlock(gPMHaltLock);
10375
10376 // Release all workers
10377
10378 for (unsigned int i = 0; i < numWorkers; i++) {
10379 if (workers[i]) {
10380 workers[i]->release();
10381 }
10382 // worker also retained by it's own thread
10383 }
10384
10385 done:
10386 DLOG("%s done\n", __FUNCTION__);
10387 return;
10388 }
10389
10390 // MARK: -
10391 // MARK: Kernel Assertion
10392
10393 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10394
10395 IOPMDriverAssertionID
createPMAssertion(IOPMDriverAssertionType whichAssertionBits,IOPMDriverAssertionLevel assertionLevel,IOService * ownerService,const char * ownerDescription)10396 IOPMrootDomain::createPMAssertion(
10397 IOPMDriverAssertionType whichAssertionBits,
10398 IOPMDriverAssertionLevel assertionLevel,
10399 IOService *ownerService,
10400 const char *ownerDescription)
10401 {
10402 IOReturn ret;
10403 IOPMDriverAssertionID newAssertion;
10404
10405 if (!pmAssertions) {
10406 return 0;
10407 }
10408
10409 ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10410
10411 if (kIOReturnSuccess == ret) {
10412 #if (DEVELOPMENT || DEBUG)
10413 if (_aotNow) {
10414 OSReportWithBacktrace("IOPMrootDomain::createPMAssertion(0x%qx)", newAssertion);
10415 }
10416 #endif /* (DEVELOPMENT || DEBUG) */
10417 return newAssertion;
10418 } else {
10419 return 0;
10420 }
10421 }
10422
10423 IOReturn
releasePMAssertion(IOPMDriverAssertionID releaseAssertion)10424 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10425 {
10426 #if (DEVELOPMENT || DEBUG)
10427 if (_aotNow) {
10428 OSReportWithBacktrace("IOPMrootDomain::releasePMAssertion(0x%qx)", releaseAssertion);
10429 }
10430 #endif /* (DEVELOPMENT || DEBUG) */
10431 if (!pmAssertions) {
10432 return kIOReturnInternalError;
10433 }
10434 return pmAssertions->releaseAssertion(releaseAssertion);
10435 }
10436
10437
10438 IOReturn
setPMAssertionLevel(IOPMDriverAssertionID assertionID,IOPMDriverAssertionLevel assertionLevel)10439 IOPMrootDomain::setPMAssertionLevel(
10440 IOPMDriverAssertionID assertionID,
10441 IOPMDriverAssertionLevel assertionLevel)
10442 {
10443 return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10444 }
10445
10446 IOPMDriverAssertionLevel
getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)10447 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10448 {
10449 IOPMDriverAssertionType sysLevels;
10450
10451 if (!pmAssertions || whichAssertion == 0) {
10452 return kIOPMDriverAssertionLevelOff;
10453 }
10454
10455 sysLevels = pmAssertions->getActivatedAssertions();
10456
10457 // Check that every bit set in argument 'whichAssertion' is asserted
10458 // in the aggregate bits.
10459 if ((sysLevels & whichAssertion) == whichAssertion) {
10460 return kIOPMDriverAssertionLevelOn;
10461 } else {
10462 return kIOPMDriverAssertionLevelOff;
10463 }
10464 }
10465
10466 IOReturn
setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)10467 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10468 {
10469 if (!pmAssertions) {
10470 return kIOReturnNotFound;
10471 }
10472
10473 return pmAssertions->setUserAssertionLevels(inLevels);
10474 }
10475
10476 bool
serializeProperties(OSSerialize * s) const10477 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10478 {
10479 if (pmAssertions) {
10480 pmAssertions->publishProperties();
10481 }
10482 return IOService::serializeProperties(s);
10483 }
10484
10485 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const10486 IOPMrootDomain::copyProperty( const char * aKey) const
10487 {
10488 OSSharedPtr<OSObject> obj;
10489 obj = IOService::copyProperty(aKey);
10490
10491 if (obj) {
10492 return obj;
10493 }
10494
10495 if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10496 sizeof(kIOPMSleepWakeWdogRebootKey))) {
10497 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10498 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10499 } else {
10500 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10501 }
10502 }
10503
10504 if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10505 sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10506 if (swd_flags & SWD_VALID_LOGS) {
10507 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10508 } else {
10509 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10510 }
10511 }
10512
10513 /*
10514 * XXX: We should get rid of "DesktopMode" property when 'kAppleClamshellCausesSleepKey'
10515 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10516 * issued by DisplayWrangler on darkwake.
10517 */
10518 if (!strcmp(aKey, "DesktopMode")) {
10519 if (desktopMode) {
10520 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10521 } else {
10522 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10523 }
10524 }
10525 if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10526 if (displayIdleForDemandSleep) {
10527 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10528 } else {
10529 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10530 }
10531 }
10532
10533 if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10534 OSSharedPtr<OSArray> array;
10535 WAKEEVENT_LOCK();
10536 if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10537 OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10538 if (collection) {
10539 array = OSDynamicPtrCast<OSArray>(collection);
10540 }
10541 }
10542 WAKEEVENT_UNLOCK();
10543 return os::move(array);
10544 }
10545
10546 if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10547 OSSharedPtr<OSArray> array;
10548 IOLockLock(pmStatsLock);
10549 if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10550 OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10551 if (collection) {
10552 array = OSDynamicPtrCast<OSArray>(collection);
10553 }
10554 }
10555 IOLockUnlock(pmStatsLock);
10556 return os::move(array);
10557 }
10558
10559 if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10560 OSArray *idleSleepList = NULL;
10561 gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10562 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10563 }
10564
10565 if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10566 OSArray *systemSleepList = NULL;
10567 gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10568 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10569 }
10570
10571 if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10572 OSArray *idleSleepList = NULL;
10573 gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10574 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10575 }
10576
10577 if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10578 OSArray *systemSleepList = NULL;
10579 gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10580 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10581 }
10582 return NULL;
10583 }
10584
10585 // MARK: -
10586 // MARK: Wake Event Reporting
10587
10588 void
copyWakeReasonString(char * outBuf,size_t bufSize)10589 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10590 {
10591 WAKEEVENT_LOCK();
10592 strlcpy(outBuf, gWakeReasonString, bufSize);
10593 WAKEEVENT_UNLOCK();
10594 }
10595
10596 void
copyShutdownReasonString(char * outBuf,size_t bufSize)10597 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10598 {
10599 WAKEEVENT_LOCK();
10600 strlcpy(outBuf, gShutdownReasonString, bufSize);
10601 WAKEEVENT_UNLOCK();
10602 }
10603
10604 //******************************************************************************
10605 // acceptSystemWakeEvents
10606 //
10607 // Private control for the acceptance of driver wake event claims.
10608 //******************************************************************************
10609
10610 void
acceptSystemWakeEvents(uint32_t control)10611 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10612 {
10613 bool logWakeReason = false;
10614
10615 WAKEEVENT_LOCK();
10616 switch (control) {
10617 case kAcceptSystemWakeEvents_Enable:
10618 assert(_acceptSystemWakeEvents == false);
10619 if (!_systemWakeEventsArray) {
10620 _systemWakeEventsArray = OSArray::withCapacity(4);
10621 }
10622 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10623 if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10624 gWakeReasonString[0] = '\0';
10625 if (_systemWakeEventsArray) {
10626 _systemWakeEventsArray->flushCollection();
10627 }
10628 }
10629
10630 // Remove stale WakeType property before system sleep
10631 removeProperty(kIOPMRootDomainWakeTypeKey);
10632 removeProperty(kIOPMRootDomainWakeReasonKey);
10633 break;
10634
10635 case kAcceptSystemWakeEvents_Disable:
10636 _acceptSystemWakeEvents = false;
10637 #if defined(XNU_TARGET_OS_OSX)
10638 logWakeReason = (gWakeReasonString[0] != '\0');
10639 #else /* !defined(XNU_TARGET_OS_OSX) */
10640 logWakeReason = gWakeReasonSysctlRegistered;
10641 #if DEVELOPMENT
10642 static int panic_allowed = -1;
10643
10644 if ((panic_allowed == -1) &&
10645 (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10646 panic_allowed = 0;
10647 }
10648
10649 if (panic_allowed) {
10650 size_t i = 0;
10651 // Panic if wake reason is null or empty
10652 for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10653 if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10654 break;
10655 }
10656 }
10657 if (i >= strlen(gWakeReasonString)) {
10658 panic("Wake reason is empty");
10659 }
10660 }
10661 #endif /* DEVELOPMENT */
10662 #endif /* !defined(XNU_TARGET_OS_OSX) */
10663
10664 // publish kIOPMRootDomainWakeReasonKey if not already set
10665 if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10666 setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10667 }
10668 break;
10669
10670 case kAcceptSystemWakeEvents_Reenable:
10671 assert(_acceptSystemWakeEvents == false);
10672 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10673 removeProperty(kIOPMRootDomainWakeReasonKey);
10674 break;
10675 }
10676 WAKEEVENT_UNLOCK();
10677
10678 if (logWakeReason) {
10679 MSG("system wake events: %s\n", gWakeReasonString);
10680 }
10681 }
10682
10683 //******************************************************************************
10684 // claimSystemWakeEvent
10685 //
10686 // For a driver to claim a device is the source/conduit of a system wake event.
10687 //******************************************************************************
10688
10689 void
claimSystemWakeEvent(IOService * device,IOOptionBits flags,const char * reason,OSObject * details)10690 IOPMrootDomain::claimSystemWakeEvent(
10691 IOService * device,
10692 IOOptionBits flags,
10693 const char * reason,
10694 OSObject * details )
10695 {
10696 OSSharedPtr<const OSSymbol> deviceName;
10697 OSSharedPtr<OSNumber> deviceRegId;
10698 OSSharedPtr<OSNumber> claimTime;
10699 OSSharedPtr<OSData> flagsData;
10700 OSSharedPtr<OSString> reasonString;
10701 OSSharedPtr<OSDictionary> dict;
10702 uint64_t timestamp;
10703 bool addWakeReason;
10704
10705 if (!device || !reason) {
10706 return;
10707 }
10708
10709 pmEventTimeStamp(×tamp);
10710
10711 IOOptionBits aotFlags = 0;
10712 bool needAOTEvaluate = FALSE;
10713
10714 if (kIOPMAOTModeAddEventFlags & _aotMode) {
10715 if (!strcmp("hold", reason)
10716 || !strcmp("help", reason)
10717 || !strcmp("menu", reason)
10718 || !strcmp("stockholm", reason)
10719 || !strcmp("ringer", reason)
10720 || !strcmp("ringerab", reason)
10721 || !strcmp("smc0", reason)
10722 || !strcmp("AOP.RTPWakeupAP", reason)
10723 || !strcmp("AOP.RTP_AP_IRQ", reason)
10724 || !strcmp("BT.OutboxNotEmpty", reason)
10725 || !strcmp("WL.OutboxNotEmpty", reason)) {
10726 flags |= kIOPMWakeEventAOTExit;
10727 }
10728 }
10729
10730 #if DEVELOPMENT || DEBUG
10731 if (_aotLingerTime && !strcmp("rtc", reason)) {
10732 flags |= kIOPMWakeEventAOTPossibleExit;
10733 }
10734 #endif /* DEVELOPMENT || DEBUG */
10735
10736 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10737 // Publishing the WakeType is serialized by the PM work loop
10738 if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10739 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10740 (void *) _nextScheduledAlarmType.get());
10741 }
10742
10743 // Workaround for the missing wake HID event
10744 if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10745 if (!strcmp("trackpadkeyboard", reason)) {
10746 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10747 (void *) gIOPMWakeTypeUserKey.get());
10748 }
10749 }
10750 #endif
10751
10752 deviceName = device->copyName(gIOServicePlane);
10753 deviceRegId = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10754 claimTime = OSNumber::withNumber(timestamp, 64);
10755 flagsData = OSData::withValue(flags);
10756 reasonString = OSString::withCString(reason);
10757 dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
10758 if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
10759 goto done;
10760 }
10761
10762 dict->setObject(gIONameKey, deviceName.get());
10763 dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
10764 dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
10765 dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
10766 dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
10767 if (details) {
10768 dict->setObject(kIOPMWakeEventDetailsKey, details);
10769 }
10770
10771 WAKEEVENT_LOCK();
10772 addWakeReason = _acceptSystemWakeEvents;
10773 if (_aotMode) {
10774 IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
10775 }
10776 aotFlags = (kIOPMWakeEventAOTFlags & flags);
10777 aotFlags = (aotFlags & ~_aotPendingFlags);
10778 needAOTEvaluate = false;
10779 if (_aotNow && aotFlags) {
10780 if (kIOPMWakeEventAOTPossibleExit & flags) {
10781 _aotMetrics->possibleCount++;
10782 }
10783 if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
10784 _aotMetrics->confirmedPossibleCount++;
10785 }
10786 if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
10787 _aotMetrics->rejectedPossibleCount++;
10788 }
10789 if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
10790 _aotMetrics->expiredPossibleCount++;
10791 }
10792
10793 _aotPendingFlags |= aotFlags;
10794 addWakeReason = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
10795 needAOTEvaluate = _aotReadyToFullWake;
10796 }
10797 DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
10798 reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
10799 _aotNow, pmTracer->getTracePhase(), addWakeReason);
10800
10801 if (!gWakeReasonSysctlRegistered) {
10802 // Lazy registration until the platform driver stops registering
10803 // the same name.
10804 gWakeReasonSysctlRegistered = true;
10805 }
10806 if (addWakeReason) {
10807 _systemWakeEventsArray->setObject(dict.get());
10808 if (gWakeReasonString[0] != '\0') {
10809 strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
10810 }
10811 strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
10812 }
10813
10814 WAKEEVENT_UNLOCK();
10815 if (needAOTEvaluate) {
10816 // Call aotEvaluate() on PM work loop since it may call
10817 // aotExit() which accesses PM state.
10818 pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
10819 }
10820
10821 done:
10822 return;
10823 }
10824
10825 //******************************************************************************
10826 // claimSystemBootEvent
10827 //
10828 // For a driver to claim a device is the source/conduit of a system boot event.
10829 //******************************************************************************
10830
10831 void
claimSystemBootEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10832 IOPMrootDomain::claimSystemBootEvent(
10833 IOService * device,
10834 IOOptionBits flags,
10835 const char * reason,
10836 __unused OSObject * details )
10837 {
10838 if (!device || !reason) {
10839 return;
10840 }
10841
10842 DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10843 WAKEEVENT_LOCK();
10844 if (!gBootReasonSysctlRegistered) {
10845 // Lazy sysctl registration after setting gBootReasonString
10846 strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
10847 os_atomic_store(&gBootReasonSysctlRegistered, true, release);
10848 }
10849 WAKEEVENT_UNLOCK();
10850 }
10851
10852 //******************************************************************************
10853 // claimSystemShutdownEvent
10854 //
10855 // For drivers to claim a system shutdown event on the ensuing boot.
10856 //******************************************************************************
10857
10858 void
claimSystemShutdownEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10859 IOPMrootDomain::claimSystemShutdownEvent(
10860 IOService * device,
10861 IOOptionBits flags,
10862 const char * reason,
10863 __unused OSObject * details )
10864 {
10865 if (!device || !reason) {
10866 return;
10867 }
10868
10869 DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10870 WAKEEVENT_LOCK();
10871 if (gShutdownReasonString[0] != '\0') {
10872 strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
10873 }
10874 strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
10875
10876 gShutdownReasonSysctlRegistered = true;
10877 WAKEEVENT_UNLOCK();
10878 }
10879
10880 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10881
10882 // MARK: -
10883 // MARK: PMSettingHandle
10884
OSDefineMetaClassAndStructors(PMSettingHandle,OSObject)10885 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
10886
10887 void
10888 PMSettingHandle::free( void )
10889 {
10890 if (pmso) {
10891 pmso->clientHandleFreed();
10892 pmso->release();
10893 pmso = NULL;
10894 }
10895
10896 OSObject::free();
10897 }
10898
10899 // MARK: -
10900 // MARK: PMSettingObject
10901
10902 #undef super
10903 #define super OSObject
OSDefineMetaClassAndFinalStructors(PMSettingObject,OSObject)10904 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
10905
10906 /*
10907 * Static constructor/initializer for PMSettingObject
10908 */
10909 PMSettingObject *PMSettingObject::pmSettingObject(
10910 IOPMrootDomain * parent_arg,
10911 IOPMSettingControllerCallback handler_arg,
10912 OSObject * target_arg,
10913 uintptr_t refcon_arg,
10914 uint32_t supportedPowerSources,
10915 const OSSymbol * settings[],
10916 OSObject * *handle_obj)
10917 {
10918 uint32_t settingCount = 0;
10919 PMSettingObject *pmso = NULL;
10920 PMSettingHandle *pmsh = NULL;
10921
10922 if (!parent_arg || !handler_arg || !settings || !handle_obj) {
10923 return NULL;
10924 }
10925
10926 // count OSSymbol entries in NULL terminated settings array
10927 while (settings[settingCount]) {
10928 settingCount++;
10929 }
10930 if (0 == settingCount) {
10931 return NULL;
10932 }
10933
10934 pmso = new PMSettingObject;
10935 if (!pmso || !pmso->init()) {
10936 goto fail;
10937 }
10938
10939 pmsh = new PMSettingHandle;
10940 if (!pmsh || !pmsh->init()) {
10941 goto fail;
10942 }
10943
10944 queue_init(&pmso->calloutQueue);
10945 pmso->parent = parent_arg;
10946 pmso->func = handler_arg;
10947 pmso->target = target_arg;
10948 pmso->refcon = refcon_arg;
10949 pmso->settingCount = settingCount;
10950
10951 pmso->retain(); // handle holds a retain on pmso
10952 pmsh->pmso = pmso;
10953 pmso->pmsh = pmsh;
10954
10955 pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
10956 if (pmso->publishedFeatureID) {
10957 for (unsigned int i = 0; i < settingCount; i++) {
10958 // Since there is now at least one listener to this setting, publish
10959 // PM root domain support for it.
10960 parent_arg->publishPMSetting( settings[i],
10961 supportedPowerSources, &pmso->publishedFeatureID[i] );
10962 }
10963 }
10964
10965 *handle_obj = pmsh;
10966 return pmso;
10967
10968 fail:
10969 if (pmso) {
10970 pmso->release();
10971 }
10972 if (pmsh) {
10973 pmsh->release();
10974 }
10975 return NULL;
10976 }
10977
10978 void
free(void)10979 PMSettingObject::free( void )
10980 {
10981 if (publishedFeatureID) {
10982 for (const auto& featureID : publishedFeatureID) {
10983 if (featureID) {
10984 parent->removePublishedFeature( featureID );
10985 }
10986 }
10987
10988 publishedFeatureID = {};
10989 }
10990
10991 super::free();
10992 }
10993
10994 IOReturn
dispatchPMSetting(const OSSymbol * type,OSObject * object)10995 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
10996 {
10997 return (*func)(target, type, object, refcon);
10998 }
10999
11000 void
clientHandleFreed(void)11001 PMSettingObject::clientHandleFreed( void )
11002 {
11003 parent->deregisterPMSettingObject(this);
11004 }
11005
11006 // MARK: -
11007 // MARK: PMAssertionsTracker
11008
11009 //*********************************************************************************
11010 //*********************************************************************************
11011 //*********************************************************************************
11012 // class PMAssertionsTracker Implementation
11013
11014 #define kAssertUniqueIDStart 500
11015
11016 PMAssertionsTracker *
pmAssertionsTracker(IOPMrootDomain * rootDomain)11017 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
11018 {
11019 PMAssertionsTracker *me;
11020
11021 me = new PMAssertionsTracker;
11022 if (!me || !me->init()) {
11023 if (me) {
11024 me->release();
11025 }
11026 return NULL;
11027 }
11028
11029 me->owner = rootDomain;
11030 me->issuingUniqueID = kAssertUniqueIDStart;
11031 me->assertionsArray = OSArray::withCapacity(5);
11032 me->assertionsKernel = 0;
11033 me->assertionsUser = 0;
11034 me->assertionsCombined = 0;
11035 me->assertionsArrayLock = IOLockAlloc();
11036 me->tabulateProducerCount = me->tabulateConsumerCount = 0;
11037
11038 assert(me->assertionsArray);
11039 assert(me->assertionsArrayLock);
11040
11041 return me;
11042 }
11043
11044 /* tabulate
11045 * - Update assertionsKernel to reflect the state of all
11046 * assertions in the kernel.
11047 * - Update assertionsCombined to reflect both kernel & user space.
11048 */
11049 void
tabulate(void)11050 PMAssertionsTracker::tabulate(void)
11051 {
11052 int i;
11053 int count;
11054 const PMAssertStruct *_a = nullptr;
11055 OSValueObject<PMAssertStruct> *_d = nullptr;
11056
11057 IOPMDriverAssertionType oldKernel = assertionsKernel;
11058 IOPMDriverAssertionType oldCombined = assertionsCombined;
11059
11060 ASSERT_GATED();
11061
11062 assertionsKernel = 0;
11063 assertionsCombined = 0;
11064
11065 if (!assertionsArray) {
11066 return;
11067 }
11068
11069 if ((count = assertionsArray->getCount())) {
11070 for (i = 0; i < count; i++) {
11071 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11072 if (_d) {
11073 _a = _d->getBytesNoCopy();
11074 if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11075 assertionsKernel |= _a->assertionBits;
11076 }
11077 }
11078 }
11079 }
11080
11081 tabulateProducerCount++;
11082 assertionsCombined = assertionsKernel | assertionsUser;
11083
11084 if ((assertionsKernel != oldKernel) ||
11085 (assertionsCombined != oldCombined)) {
11086 owner->evaluateAssertions(assertionsCombined, oldCombined);
11087 }
11088 }
11089
11090 void
updateCPUBitAccounting(PMAssertStruct * assertStruct)11091 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11092 {
11093 AbsoluteTime now;
11094 uint64_t nsec;
11095
11096 if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11097 (assertStruct->assertCPUStartTime == 0)) {
11098 return;
11099 }
11100
11101 now = mach_absolute_time();
11102 SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11103 absolutetime_to_nanoseconds(now, &nsec);
11104 assertStruct->assertCPUDuration += nsec;
11105 assertStruct->assertCPUStartTime = 0;
11106
11107 if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11108 maxAssertCPUDuration = assertStruct->assertCPUDuration;
11109 maxAssertCPUEntryId = assertStruct->registryEntryID;
11110 }
11111 }
11112
11113 void
reportCPUBitAccounting(void)11114 PMAssertionsTracker::reportCPUBitAccounting( void )
11115 {
11116 const PMAssertStruct *_a = nullptr;
11117 OSValueObject<PMAssertStruct> *_d = nullptr;
11118 int i, count;
11119 AbsoluteTime now;
11120 uint64_t nsec;
11121
11122 ASSERT_GATED();
11123
11124 // Account for drivers that are still holding the CPU assertion
11125 if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11126 now = mach_absolute_time();
11127 if ((count = assertionsArray->getCount())) {
11128 for (i = 0; i < count; i++) {
11129 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11130 if (_d) {
11131 _a = _d->getBytesNoCopy();
11132 if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11133 (_a->level == kIOPMDriverAssertionLevelOn) &&
11134 (_a->assertCPUStartTime != 0)) {
11135 // Don't modify PMAssertStruct, leave that
11136 // for updateCPUBitAccounting()
11137 SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11138 absolutetime_to_nanoseconds(now, &nsec);
11139 nsec += _a->assertCPUDuration;
11140 if (nsec > maxAssertCPUDuration) {
11141 maxAssertCPUDuration = nsec;
11142 maxAssertCPUEntryId = _a->registryEntryID;
11143 }
11144 }
11145 }
11146 }
11147 }
11148 }
11149
11150 if (maxAssertCPUDuration) {
11151 DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11152 (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11153 }
11154
11155 maxAssertCPUDuration = 0;
11156 maxAssertCPUEntryId = 0;
11157 }
11158
11159 void
publishProperties(void)11160 PMAssertionsTracker::publishProperties( void )
11161 {
11162 OSSharedPtr<OSArray> assertionsSummary;
11163
11164 if (tabulateConsumerCount != tabulateProducerCount) {
11165 IOLockLock(assertionsArrayLock);
11166
11167 tabulateConsumerCount = tabulateProducerCount;
11168
11169 /* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11170 */
11171 assertionsSummary = copyAssertionsArray();
11172 if (assertionsSummary) {
11173 owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11174 } else {
11175 owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11176 }
11177
11178 /* Publish the IOPMrootDomain property "DriverPMAssertions"
11179 */
11180 owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11181
11182 IOLockUnlock(assertionsArrayLock);
11183 }
11184 }
11185
11186 PMAssertStruct *
detailsForID(IOPMDriverAssertionID _id,int * index)11187 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11188 {
11189 PMAssertStruct *_a = NULL;
11190 OSValueObject<PMAssertStruct> *_d = nullptr;
11191 int found = -1;
11192 int count = 0;
11193 int i = 0;
11194
11195 if (assertionsArray
11196 && (count = assertionsArray->getCount())) {
11197 for (i = 0; i < count; i++) {
11198 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11199 if (_d) {
11200 _a = _d->getMutableBytesNoCopy();
11201 if (_a && (_id == _a->id)) {
11202 found = i;
11203 break;
11204 }
11205 }
11206 }
11207 }
11208
11209 if (-1 == found) {
11210 return NULL;
11211 } else {
11212 if (index) {
11213 *index = found;
11214 }
11215 return _a;
11216 }
11217 }
11218
11219 /* PMAssertionsTracker::handleCreateAssertion
11220 * Perform assertion work on the PM workloop. Do not call directly.
11221 */
11222 IOReturn
handleCreateAssertion(OSValueObject<PMAssertStruct> * newAssertion)11223 PMAssertionsTracker::handleCreateAssertion(OSValueObject<PMAssertStruct> *newAssertion)
11224 {
11225 PMAssertStruct *assertStruct = nullptr;
11226
11227 ASSERT_GATED();
11228
11229 if (newAssertion) {
11230 IOLockLock(assertionsArrayLock);
11231 assertStruct = newAssertion->getMutableBytesNoCopy();
11232 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11233 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11234 assertStruct->assertCPUStartTime = mach_absolute_time();
11235 }
11236 assertionsArray->setObject(newAssertion);
11237 IOLockUnlock(assertionsArrayLock);
11238 newAssertion->release();
11239
11240 tabulate();
11241 }
11242 return kIOReturnSuccess;
11243 }
11244
11245 /* PMAssertionsTracker::createAssertion
11246 * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11247 * appropiate.
11248 */
11249 IOReturn
createAssertion(IOPMDriverAssertionType which,IOPMDriverAssertionLevel level,IOService * serviceID,const char * whoItIs,IOPMDriverAssertionID * outID)11250 PMAssertionsTracker::createAssertion(
11251 IOPMDriverAssertionType which,
11252 IOPMDriverAssertionLevel level,
11253 IOService *serviceID,
11254 const char *whoItIs,
11255 IOPMDriverAssertionID *outID)
11256 {
11257 OSSharedPtr<OSValueObject<PMAssertStruct> > dataStore;
11258 PMAssertStruct track;
11259
11260 // Warning: trillions and trillions of created assertions may overflow the unique ID.
11261 track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11262 track.level = level;
11263 track.assertionBits = which;
11264
11265 // NB: ownerString is explicitly managed by PMAssertStruct
11266 // it will be released in `handleReleaseAssertion' below
11267 track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11268 track.ownerService = serviceID;
11269 track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11270 track.modifiedTime = 0;
11271 pmEventTimeStamp(&track.createdTime);
11272 track.assertCPUStartTime = 0;
11273 track.assertCPUDuration = 0;
11274
11275 dataStore = OSValueObjectWithValue(track);
11276 if (!dataStore) {
11277 if (track.ownerString) {
11278 track.ownerString->release();
11279 track.ownerString = NULL;
11280 }
11281 return kIOReturnNoMemory;
11282 }
11283
11284 *outID = track.id;
11285
11286 if (owner && owner->pmPowerStateQueue) {
11287 // queue action is responsible for releasing dataStore
11288 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11289 }
11290
11291 return kIOReturnSuccess;
11292 }
11293
11294 /* PMAssertionsTracker::handleReleaseAssertion
11295 * Runs in PM workloop. Do not call directly.
11296 */
11297 IOReturn
handleReleaseAssertion(IOPMDriverAssertionID _id)11298 PMAssertionsTracker::handleReleaseAssertion(
11299 IOPMDriverAssertionID _id)
11300 {
11301 ASSERT_GATED();
11302
11303 int index;
11304 PMAssertStruct *assertStruct = detailsForID(_id, &index);
11305
11306 if (!assertStruct) {
11307 return kIOReturnNotFound;
11308 }
11309
11310 IOLockLock(assertionsArrayLock);
11311
11312 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11313 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11314 updateCPUBitAccounting(assertStruct);
11315 }
11316
11317 if (assertStruct->ownerString) {
11318 assertStruct->ownerString->release();
11319 assertStruct->ownerString = NULL;
11320 }
11321
11322 assertionsArray->removeObject(index);
11323 IOLockUnlock(assertionsArrayLock);
11324
11325 tabulate();
11326 return kIOReturnSuccess;
11327 }
11328
11329 /* PMAssertionsTracker::releaseAssertion
11330 * Releases an assertion and affects system behavior if appropiate.
11331 * Actual work happens on PM workloop.
11332 */
11333 IOReturn
releaseAssertion(IOPMDriverAssertionID _id)11334 PMAssertionsTracker::releaseAssertion(
11335 IOPMDriverAssertionID _id)
11336 {
11337 if (owner && owner->pmPowerStateQueue) {
11338 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11339 }
11340 return kIOReturnSuccess;
11341 }
11342
11343 /* PMAssertionsTracker::handleSetAssertionLevel
11344 * Runs in PM workloop. Do not call directly.
11345 */
11346 IOReturn
handleSetAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11347 PMAssertionsTracker::handleSetAssertionLevel(
11348 IOPMDriverAssertionID _id,
11349 IOPMDriverAssertionLevel _level)
11350 {
11351 PMAssertStruct *assertStruct = detailsForID(_id, NULL);
11352
11353 ASSERT_GATED();
11354
11355 if (!assertStruct) {
11356 return kIOReturnNotFound;
11357 }
11358
11359 IOLockLock(assertionsArrayLock);
11360 pmEventTimeStamp(&assertStruct->modifiedTime);
11361 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11362 (assertStruct->level != _level)) {
11363 if (_level == kIOPMDriverAssertionLevelOn) {
11364 assertStruct->assertCPUStartTime = mach_absolute_time();
11365 } else {
11366 updateCPUBitAccounting(assertStruct);
11367 }
11368 }
11369 assertStruct->level = _level;
11370 IOLockUnlock(assertionsArrayLock);
11371
11372 tabulate();
11373 return kIOReturnSuccess;
11374 }
11375
11376 /* PMAssertionsTracker::setAssertionLevel
11377 */
11378 IOReturn
setAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11379 PMAssertionsTracker::setAssertionLevel(
11380 IOPMDriverAssertionID _id,
11381 IOPMDriverAssertionLevel _level)
11382 {
11383 if (owner && owner->pmPowerStateQueue) {
11384 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11385 (void *)(uintptr_t)_level, _id);
11386 }
11387
11388 return kIOReturnSuccess;
11389 }
11390
11391 IOReturn
handleSetUserAssertionLevels(void * arg0)11392 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11393 {
11394 IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11395
11396 ASSERT_GATED();
11397
11398 if (new_user_levels != assertionsUser) {
11399 DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11400 assertionsUser = new_user_levels;
11401 }
11402
11403 tabulate();
11404 return kIOReturnSuccess;
11405 }
11406
11407 IOReturn
setUserAssertionLevels(IOPMDriverAssertionType new_user_levels)11408 PMAssertionsTracker::setUserAssertionLevels(
11409 IOPMDriverAssertionType new_user_levels)
11410 {
11411 if (gIOPMWorkLoop) {
11412 gIOPMWorkLoop->runAction(
11413 OSMemberFunctionCast(
11414 IOWorkLoop::Action,
11415 this,
11416 &PMAssertionsTracker::handleSetUserAssertionLevels),
11417 this,
11418 (void *) &new_user_levels, NULL, NULL, NULL);
11419 }
11420
11421 return kIOReturnSuccess;
11422 }
11423
11424
11425 OSSharedPtr<OSArray>
copyAssertionsArray(void)11426 PMAssertionsTracker::copyAssertionsArray(void)
11427 {
11428 int count;
11429 int i;
11430 OSSharedPtr<OSArray> outArray = NULL;
11431
11432 if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11433 goto exit;
11434 }
11435 outArray = OSArray::withCapacity(count);
11436 if (!outArray) {
11437 goto exit;
11438 }
11439
11440 for (i = 0; i < count; i++) {
11441 const PMAssertStruct *_a = nullptr;
11442 OSValueObject<PMAssertStruct> *_d = nullptr;
11443 OSSharedPtr<OSDictionary> details;
11444
11445 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11446 if (_d && (_a = _d->getBytesNoCopy())) {
11447 OSSharedPtr<OSNumber> _n;
11448
11449 details = OSDictionary::withCapacity(7);
11450 if (!details) {
11451 continue;
11452 }
11453
11454 outArray->setObject(details.get());
11455
11456 _n = OSNumber::withNumber(_a->id, 64);
11457 if (_n) {
11458 details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11459 }
11460 _n = OSNumber::withNumber(_a->createdTime, 64);
11461 if (_n) {
11462 details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11463 }
11464 _n = OSNumber::withNumber(_a->modifiedTime, 64);
11465 if (_n) {
11466 details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11467 }
11468 _n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11469 if (_n) {
11470 details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11471 }
11472 _n = OSNumber::withNumber(_a->level, 64);
11473 if (_n) {
11474 details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11475 }
11476 _n = OSNumber::withNumber(_a->assertionBits, 64);
11477 if (_n) {
11478 details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11479 }
11480
11481 if (_a->ownerString) {
11482 details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11483 }
11484 }
11485 }
11486
11487 exit:
11488 return os::move(outArray);
11489 }
11490
11491 IOPMDriverAssertionType
getActivatedAssertions(void)11492 PMAssertionsTracker::getActivatedAssertions(void)
11493 {
11494 return assertionsCombined;
11495 }
11496
11497 IOPMDriverAssertionLevel
getAssertionLevel(IOPMDriverAssertionType type)11498 PMAssertionsTracker::getAssertionLevel(
11499 IOPMDriverAssertionType type)
11500 {
11501 // FIXME: unused and also wrong
11502 if (type && ((type & assertionsKernel) == assertionsKernel)) {
11503 return kIOPMDriverAssertionLevelOn;
11504 } else {
11505 return kIOPMDriverAssertionLevelOff;
11506 }
11507 }
11508
11509 //*********************************************************************************
11510 //*********************************************************************************
11511 //*********************************************************************************
11512
11513
11514 static void
pmEventTimeStamp(uint64_t * recordTS)11515 pmEventTimeStamp(uint64_t *recordTS)
11516 {
11517 clock_sec_t tsec;
11518 clock_usec_t tusec;
11519
11520 if (!recordTS) {
11521 return;
11522 }
11523
11524 // We assume tsec fits into 32 bits; 32 bits holds enough
11525 // seconds for 136 years since the epoch in 1970.
11526 clock_get_calendar_microtime(&tsec, &tusec);
11527
11528
11529 // Pack the sec & microsec calendar time into a uint64_t, for fun.
11530 *recordTS = 0;
11531 *recordTS |= (uint32_t)tusec;
11532 *recordTS |= ((uint64_t)tsec << 32);
11533
11534 return;
11535 }
11536
11537 // MARK: -
11538 // MARK: IORootParent
11539
11540 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11541
11542 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11543
11544 // The reason that root domain needs a root parent is to facilitate demand
11545 // sleep, since a power change from the root parent cannot be vetoed.
11546 //
11547 // The above statement is no longer true since root domain now performs
11548 // demand sleep using overrides. But root parent remains to avoid changing
11549 // the power tree stacking. Root parent is parked at the max power state.
11550
11551
11552 static IOPMPowerState patriarchPowerStates[2] =
11553 {
11554 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11555 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11556 };
11557
11558 void
initialize(void)11559 IORootParent::initialize( void )
11560 {
11561
11562 gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11563 gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11564 gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11565 gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11566 gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11567 gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11568 gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11569 gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11570 gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11571 gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11572 gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11573 gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11574 gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11575 gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11576 gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11577 gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11578 gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11579 gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11580 gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11581 gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11582 gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11583 gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11584 gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11585 gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11586 gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11587 gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11588 gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11589 gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11590 gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11591 gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11592 gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11593 gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11594 gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11595 gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11596 gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11597 gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11598 gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11599 gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11600 gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11601 gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11602 gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11603 gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11604 gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11605 gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11606 }
11607
11608 bool
start(IOService * nub)11609 IORootParent::start( IOService * nub )
11610 {
11611 IOService::start(nub);
11612 attachToParent( getRegistryRoot(), gIOPowerPlane );
11613 PMinit();
11614 registerPowerDriver(this, patriarchPowerStates, 2);
11615 makeUsable();
11616 return true;
11617 }
11618
11619 void
shutDownSystem(void)11620 IORootParent::shutDownSystem( void )
11621 {
11622 }
11623
11624 void
restartSystem(void)11625 IORootParent::restartSystem( void )
11626 {
11627 }
11628
11629 void
sleepSystem(void)11630 IORootParent::sleepSystem( void )
11631 {
11632 }
11633
11634 void
dozeSystem(void)11635 IORootParent::dozeSystem( void )
11636 {
11637 }
11638
11639 void
sleepToDoze(void)11640 IORootParent::sleepToDoze( void )
11641 {
11642 }
11643
11644 void
wakeSystem(void)11645 IORootParent::wakeSystem( void )
11646 {
11647 }
11648
11649 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const11650 IORootParent::copyProperty( const char * aKey) const
11651 {
11652 return IOService::copyProperty(aKey);
11653 }
11654
11655 uint32_t
getWatchdogTimeout()11656 IOPMrootDomain::getWatchdogTimeout()
11657 {
11658 if (gSwdSleepWakeTimeout) {
11659 gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11660 }
11661 if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11662 (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11663 return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11664 } else {
11665 return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11666 }
11667 }
11668
11669
11670 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
11671 IOReturn
restartWithStackshot()11672 IOPMrootDomain::restartWithStackshot()
11673 {
11674 takeStackshot(true);
11675
11676 return kIOReturnSuccess;
11677 }
11678
11679 void
sleepWakeDebugTrig(bool wdogTrigger)11680 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
11681 {
11682 takeStackshot(wdogTrigger);
11683 }
11684
11685 void
tracePhase2String(uint32_t tracePhase,const char ** phaseString,const char ** description)11686 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
11687 {
11688 switch (tracePhase) {
11689 case kIOPMTracePointSleepStarted:
11690 *phaseString = "kIOPMTracePointSleepStarted";
11691 *description = "starting sleep";
11692 break;
11693
11694 case kIOPMTracePointSleepApplications:
11695 *phaseString = "kIOPMTracePointSleepApplications";
11696 *description = "notifying applications";
11697 break;
11698
11699 case kIOPMTracePointSleepPriorityClients:
11700 *phaseString = "kIOPMTracePointSleepPriorityClients";
11701 *description = "notifying clients about upcoming system capability changes";
11702 break;
11703
11704 case kIOPMTracePointSleepWillChangeInterests:
11705 *phaseString = "kIOPMTracePointSleepWillChangeInterests";
11706 *description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
11707 break;
11708
11709 case kIOPMTracePointSleepPowerPlaneDrivers:
11710 *phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
11711 *description = "calling power state change callbacks";
11712 break;
11713
11714 case kIOPMTracePointSleepDidChangeInterests:
11715 *phaseString = "kIOPMTracePointSleepDidChangeInterests";
11716 *description = "calling rootDomain's clients about rootDomain's state changes";
11717 break;
11718
11719 case kIOPMTracePointSleepCapabilityClients:
11720 *phaseString = "kIOPMTracePointSleepCapabilityClients";
11721 *description = "notifying clients about current system capabilities";
11722 break;
11723
11724 case kIOPMTracePointSleepPlatformActions:
11725 *phaseString = "kIOPMTracePointSleepPlatformActions";
11726 *description = "calling Quiesce/Sleep action callbacks";
11727 break;
11728
11729 case kIOPMTracePointSleepCPUs:
11730 {
11731 *phaseString = "kIOPMTracePointSleepCPUs";
11732 #if defined(__i386__) || defined(__x86_64__)
11733 /*
11734 * We cannot use the getCPUNumber() method to get the cpu number, since
11735 * that cpu number is unrelated to the cpu number we need (we need the cpu
11736 * number as enumerated by the scheduler, NOT the CPU number enumerated
11737 * by ACPIPlatform as the CPUs are enumerated in MADT order).
11738 * Instead, pass the Mach processor pointer associated with the current
11739 * shutdown target so its associated cpu_id can be used in
11740 * processor_to_datastring.
11741 */
11742 if (currentShutdownTarget != NULL &&
11743 currentShutdownTarget->getMachProcessor() != NULL) {
11744 const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
11745 currentShutdownTarget->getMachProcessor());
11746 *description = sbuf;
11747 } else {
11748 *description = "halting all non-boot CPUs";
11749 }
11750 #else
11751 *description = "halting all non-boot CPUs";
11752 #endif
11753 break;
11754 }
11755 case kIOPMTracePointSleepPlatformDriver:
11756 *phaseString = "kIOPMTracePointSleepPlatformDriver";
11757 *description = "executing platform specific code";
11758 break;
11759
11760 case kIOPMTracePointHibernate:
11761 *phaseString = "kIOPMTracePointHibernate";
11762 *description = "writing the hibernation image";
11763 break;
11764
11765 case kIOPMTracePointSystemSleep:
11766 *phaseString = "kIOPMTracePointSystemSleep";
11767 *description = "in EFI/Bootrom after last point of entry to sleep";
11768 break;
11769
11770 case kIOPMTracePointWakePlatformDriver:
11771 *phaseString = "kIOPMTracePointWakePlatformDriver";
11772 *description = "executing platform specific code";
11773 break;
11774
11775
11776 case kIOPMTracePointWakePlatformActions:
11777 *phaseString = "kIOPMTracePointWakePlatformActions";
11778 *description = "calling Wake action callbacks";
11779 break;
11780
11781 case kIOPMTracePointWakeCPUs:
11782 *phaseString = "kIOPMTracePointWakeCPUs";
11783 *description = "starting non-boot CPUs";
11784 break;
11785
11786 case kIOPMTracePointWakeWillPowerOnClients:
11787 *phaseString = "kIOPMTracePointWakeWillPowerOnClients";
11788 *description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
11789 break;
11790
11791 case kIOPMTracePointWakeWillChangeInterests:
11792 *phaseString = "kIOPMTracePointWakeWillChangeInterests";
11793 *description = "calling rootDomain's clients about upcoming rootDomain's state changes";
11794 break;
11795
11796 case kIOPMTracePointWakeDidChangeInterests:
11797 *phaseString = "kIOPMTracePointWakeDidChangeInterests";
11798 *description = "calling rootDomain's clients about completed rootDomain's state changes";
11799 break;
11800
11801 case kIOPMTracePointWakePowerPlaneDrivers:
11802 *phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
11803 *description = "calling power state change callbacks";
11804 break;
11805
11806 case kIOPMTracePointWakeCapabilityClients:
11807 *phaseString = "kIOPMTracePointWakeCapabilityClients";
11808 *description = "informing clients about current system capabilities";
11809 break;
11810
11811 case kIOPMTracePointWakeApplications:
11812 *phaseString = "kIOPMTracePointWakeApplications";
11813 *description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
11814 break;
11815
11816 case kIOPMTracePointDarkWakeEntry:
11817 *phaseString = "kIOPMTracePointDarkWakeEntry";
11818 *description = "entering darkwake on way to sleep";
11819 break;
11820
11821 case kIOPMTracePointDarkWakeExit:
11822 *phaseString = "kIOPMTracePointDarkWakeExit";
11823 *description = "entering fullwake from darkwake";
11824 break;
11825
11826 default:
11827 *phaseString = NULL;
11828 *description = NULL;
11829 }
11830 }
11831
11832 void
saveFailureData2File()11833 IOPMrootDomain::saveFailureData2File()
11834 {
11835 unsigned int len = 0;
11836 char failureStr[512];
11837 errno_t error;
11838 char *outbuf;
11839 OSNumber *statusCode;
11840 uint64_t pmStatusCode = 0;
11841 uint32_t phaseData = 0;
11842 uint32_t phaseDetail = 0;
11843 bool efiFailure = false;
11844
11845 OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
11846 statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
11847 if (statusCode) {
11848 pmStatusCode = statusCode->unsigned64BitValue();
11849 phaseData = pmStatusCode & 0xFFFFFFFF;
11850 phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
11851 if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
11852 LOG("Sleep Wake failure in EFI\n");
11853 efiFailure = true;
11854 failureStr[0] = 0;
11855 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);
11856 len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
11857 }
11858 }
11859
11860 if (!efiFailure) {
11861 if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
11862 swd_flags |= SWD_BOOT_BY_SW_WDOG;
11863 PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
11864 // dump panic will handle saving nvram data
11865 return;
11866 }
11867
11868 /* Keeping this around for capturing data during power
11869 * button press */
11870
11871 if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
11872 DLOG("No sleep wake failure string\n");
11873 return;
11874 }
11875 if (len == 0) {
11876 DLOG("Ignoring zero byte SleepWake failure string\n");
11877 goto exit;
11878 }
11879
11880 // if PMStatus code is zero, delete stackshot and return
11881 if (statusCode) {
11882 if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
11883 // there was no sleep wake failure
11884 // this can happen if delete stackshot was called
11885 // before take stackshot completed. Let us delete any
11886 // sleep wake failure data in nvram
11887 DLOG("Deleting stackshot on successful wake\n");
11888 deleteStackshot();
11889 return;
11890 }
11891 }
11892
11893 if (len > sizeof(failureStr)) {
11894 len = sizeof(failureStr);
11895 }
11896 failureStr[0] = 0;
11897 PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
11898 }
11899 if (failureStr[0] != 0) {
11900 error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
11901 if (error) {
11902 DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
11903 } else {
11904 DLOG("Saved SleepWake failure string to file.\n");
11905 }
11906 }
11907
11908 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
11909 goto exit;
11910 }
11911
11912 if (swd_buffer) {
11913 unsigned int len = 0;
11914 errno_t error;
11915 char nvram_var_name_buffer[20];
11916 unsigned int concat_len = 0;
11917 swd_hdr *hdr = NULL;
11918
11919
11920 hdr = (swd_hdr *)swd_buffer;
11921 outbuf = (char *)hdr + hdr->spindump_offset;
11922 OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
11923
11924 for (int i = 0; i < 8; i++) {
11925 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
11926 if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
11927 LOG("No SleepWake blob to read beyond chunk %d\n", i);
11928 break;
11929 }
11930 if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
11931 PERemoveNVRAMProperty(nvram_var_name_buffer);
11932 LOG("Could not read the property :-(\n");
11933 break;
11934 }
11935 PERemoveNVRAMProperty(nvram_var_name_buffer);
11936 concat_len += len;
11937 }
11938 LOG("Concatenated length for the SWD blob %d\n", concat_len);
11939
11940 if (concat_len) {
11941 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
11942 if (error) {
11943 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11944 } else {
11945 LOG("Saved SleepWake zipped data to file.\n");
11946 }
11947 } else {
11948 // There is a sleep wake failure string but no stackshot
11949 // Write a placeholder stacks file so that swd runs
11950 snprintf(outbuf, 20, "%s", "No stackshot data\n");
11951 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
11952 if (error) {
11953 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11954 } else {
11955 LOG("Saved SleepWake zipped data to file.\n");
11956 }
11957 }
11958 } else {
11959 LOG("No buffer allocated to save failure stackshot\n");
11960 }
11961
11962
11963 gRootDomain->swd_lock = 0;
11964 exit:
11965 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
11966 return;
11967 }
11968
11969
11970 void
getFailureData(thread_t * thread,char * failureStr,size_t strLen)11971 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
11972 {
11973 OSSharedPtr<IORegistryIterator> iter;
11974 OSSharedPtr<const OSSymbol> kextName = NULL;
11975 IORegistryEntry * entry;
11976 IOService * node;
11977 bool nodeFound = false;
11978
11979 const void * callMethod = NULL;
11980 const char * objectName = NULL;
11981 uint32_t timeout = getWatchdogTimeout();
11982 const char * phaseString = NULL;
11983 const char * phaseDescription = NULL;
11984
11985 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
11986 uint32_t tracePhase = pmTracer->getTracePhase();
11987
11988 *thread = NULL;
11989 if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
11990 snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
11991 } else {
11992 snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
11993 }
11994 tracePhase2String(tracePhase, &phaseString, &phaseDescription);
11995
11996 if (notifierThread) {
11997 if (notifier && (notifier->identifier)) {
11998 objectName = notifier->identifier->getCStringNoCopy();
11999 }
12000 *thread = notifierThread;
12001 } else {
12002 iter = IORegistryIterator::iterateOver(
12003 getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
12004
12005 if (iter) {
12006 while ((entry = iter->getNextObject())) {
12007 node = OSDynamicCast(IOService, entry);
12008 if (!node) {
12009 continue;
12010 }
12011 if (OSDynamicCast(IOPowerConnection, node)) {
12012 continue;
12013 }
12014
12015 if (node->getBlockingDriverCall(thread, &callMethod)) {
12016 nodeFound = true;
12017 break;
12018 }
12019 }
12020 }
12021 if (nodeFound) {
12022 kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
12023 if (kextName) {
12024 objectName = kextName->getCStringNoCopy();
12025 }
12026 }
12027 }
12028 if (phaseDescription) {
12029 strlcat(failureStr, " while ", strLen);
12030 strlcat(failureStr, phaseDescription, strLen);
12031 strlcat(failureStr, ".", strLen);
12032 }
12033 if (objectName) {
12034 strlcat(failureStr, " Suspected bundle: ", strLen);
12035 strlcat(failureStr, objectName, strLen);
12036 strlcat(failureStr, ".", strLen);
12037 }
12038 if (*thread) {
12039 char threadName[40];
12040 snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
12041 strlcat(failureStr, threadName, strLen);
12042 }
12043
12044 DLOG("%s\n", failureStr);
12045 }
12046
12047 struct swd_stackshot_compressed_data {
12048 z_output_func zoutput;
12049 size_t zipped;
12050 uint64_t totalbytes;
12051 uint64_t lastpercent;
12052 IOReturn error;
12053 unsigned outremain;
12054 unsigned outlen;
12055 unsigned writes;
12056 Bytef * outbuf;
12057 };
12058 struct swd_stackshot_compressed_data swd_zip_var = { };
12059
12060 static void *
swd_zs_alloc(void * __unused ref,u_int items,u_int size)12061 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
12062 {
12063 void *result;
12064 LOG("Alloc in zipping %d items of size %d\n", items, size);
12065
12066 result = (void *)(swd_zs_zmem + swd_zs_zoffset);
12067 swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
12068 LOG("Offset %zu\n", swd_zs_zoffset);
12069 return result;
12070 }
12071
12072 static int
swd_zinput(z_streamp strm,Bytef * buf,unsigned size)12073 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12074 {
12075 unsigned len;
12076
12077 len = strm->avail_in;
12078
12079 if (len > size) {
12080 len = size;
12081 }
12082 if (len == 0) {
12083 return 0;
12084 }
12085
12086 if (strm->next_in != (Bytef *) strm) {
12087 memcpy(buf, strm->next_in, len);
12088 } else {
12089 bzero(buf, len);
12090 }
12091
12092 strm->adler = z_crc32(strm->adler, buf, len);
12093
12094 strm->avail_in -= len;
12095 strm->next_in += len;
12096 strm->total_in += len;
12097
12098 return (int)len;
12099 }
12100
12101 static int
swd_zoutput(z_streamp strm,Bytef * buf,unsigned len)12102 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12103 {
12104 unsigned int i = 0;
12105 // if outlen > max size don't add to the buffer
12106 assert(buf != NULL);
12107 if (strm && buf) {
12108 if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12109 LOG("No space to GZIP... not writing to NVRAM\n");
12110 return len;
12111 }
12112 }
12113 for (i = 0; i < len; i++) {
12114 *(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12115 }
12116 swd_zip_var.outlen += len;
12117 return len;
12118 }
12119
12120 static void
swd_zs_free(void * __unused ref,void * __unused ptr)12121 swd_zs_free(void * __unused ref, void * __unused ptr)
12122 {
12123 }
12124
12125 static int
swd_compress(char * inPtr,char * outPtr,size_t numBytes)12126 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12127 {
12128 int wbits = 12;
12129 int memlevel = 3;
12130
12131 if (((unsigned int) numBytes) != numBytes) {
12132 return 0;
12133 }
12134
12135 if (!swd_zs.zalloc) {
12136 swd_zs.zalloc = swd_zs_alloc;
12137 swd_zs.zfree = swd_zs_free;
12138 if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12139 // allocation failed
12140 bzero(&swd_zs, sizeof(swd_zs));
12141 // swd_zs_zoffset = 0;
12142 } else {
12143 LOG("PMRD inited the zlib allocation routines\n");
12144 }
12145 }
12146
12147 swd_zip_var.zipped = 0;
12148 swd_zip_var.totalbytes = 0; // should this be the max that we have?
12149 swd_zip_var.lastpercent = 0;
12150 swd_zip_var.error = kIOReturnSuccess;
12151 swd_zip_var.outremain = 0;
12152 swd_zip_var.outlen = 0;
12153 swd_zip_var.writes = 0;
12154 swd_zip_var.outbuf = (Bytef *)outPtr;
12155
12156 swd_zip_var.totalbytes = numBytes;
12157
12158 swd_zs.avail_in = 0;
12159 swd_zs.next_in = NULL;
12160 swd_zs.avail_out = 0;
12161 swd_zs.next_out = NULL;
12162
12163 deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12164
12165 z_stream *zs;
12166 int zr;
12167 zs = &swd_zs;
12168
12169 while (swd_zip_var.error >= 0) {
12170 if (!zs->avail_in) {
12171 zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12172 zs->avail_in = (unsigned int) numBytes;
12173 }
12174 if (!zs->avail_out) {
12175 zs->next_out = (Bytef *)zs;
12176 zs->avail_out = UINT32_MAX;
12177 }
12178 zr = deflate(zs, Z_NO_FLUSH);
12179 if (Z_STREAM_END == zr) {
12180 break;
12181 }
12182 if (zr != Z_OK) {
12183 LOG("ZERR %d\n", zr);
12184 swd_zip_var.error = zr;
12185 } else {
12186 if (zs->total_in == numBytes) {
12187 break;
12188 }
12189 }
12190 }
12191
12192 //now flush the stream
12193 while (swd_zip_var.error >= 0) {
12194 if (!zs->avail_out) {
12195 zs->next_out = (Bytef *)zs;
12196 zs->avail_out = UINT32_MAX;
12197 }
12198 zr = deflate(zs, Z_FINISH);
12199 if (Z_STREAM_END == zr) {
12200 break;
12201 }
12202 if (zr != Z_OK) {
12203 LOG("ZERR %d\n", zr);
12204 swd_zip_var.error = zr;
12205 } else {
12206 if (zs->total_in == numBytes) {
12207 LOG("Total output size %d\n", swd_zip_var.outlen);
12208 break;
12209 }
12210 }
12211 }
12212
12213 return swd_zip_var.outlen;
12214 }
12215
12216 void
deleteStackshot()12217 IOPMrootDomain::deleteStackshot()
12218 {
12219 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12220 // takeStackshot hasn't completed
12221 return;
12222 }
12223 LOG("Deleting any sleepwake failure data in nvram\n");
12224
12225 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12226 char nvram_var_name_buf[20];
12227 for (int i = 0; i < 8; i++) {
12228 snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12229 if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12230 LOG("Removing %s returned false\n", nvram_var_name_buf);
12231 }
12232 }
12233 // force NVRAM sync
12234 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12235 DLOG("Failed to force nvram sync\n");
12236 }
12237 gRootDomain->swd_lock = 0;
12238 }
12239
12240 void
takeStackshot(bool wdogTrigger)12241 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12242 {
12243 swd_hdr * hdr = NULL;
12244 int cnt = 0;
12245 int max_cnt;
12246 pid_t pid = 0;
12247 kern_return_t kr = KERN_SUCCESS;
12248 uint64_t flags;
12249
12250 char * dstAddr;
12251 uint32_t size;
12252 uint32_t bytesRemaining;
12253 unsigned bytesWritten = 0;
12254
12255 char failureStr[512];
12256 thread_t thread = NULL;
12257 const char * swfPanic = "swfPanic";
12258
12259 uint32_t bufSize;
12260 int success = 0;
12261
12262 #if defined(__i386__) || defined(__x86_64__)
12263 const bool concise = false;
12264 #else
12265 const bool concise = true;
12266 #endif
12267
12268 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12269 return;
12270 }
12271
12272 failureStr[0] = 0;
12273 if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12274 return;
12275 }
12276
12277 if (wdogTrigger) {
12278 getFailureData(&thread, failureStr, sizeof(failureStr));
12279
12280 if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12281 goto skip_stackshot;
12282 }
12283 } else {
12284 AbsoluteTime now;
12285 uint64_t nsec;
12286 clock_get_uptime(&now);
12287 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12288 absolutetime_to_nanoseconds(now, &nsec);
12289 snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12290 }
12291
12292 if (swd_buffer == NULL) {
12293 sleepWakeDebugMemAlloc();
12294 if (swd_buffer == NULL) {
12295 return;
12296 }
12297 }
12298 hdr = (swd_hdr *)swd_buffer;
12299 bufSize = hdr->alloc_size;
12300
12301 dstAddr = (char*)hdr + hdr->spindump_offset;
12302 flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_NO_IO_STATS | STACKSHOT_SAVE_KEXT_LOADINFO | STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY | STACKSHOT_THREAD_WAITINFO | STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12303
12304 /* If not wdogTrigger only take kernel tasks stackshot
12305 */
12306 if (wdogTrigger) {
12307 pid = -1;
12308 max_cnt = 3;
12309 } else {
12310 pid = 0;
12311 max_cnt = 2;
12312 }
12313
12314 /* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12315 * If we run out of space, take stackshot with only kernel task
12316 */
12317 while (success == 0 && cnt < max_cnt) {
12318 bytesRemaining = bufSize - hdr->spindump_offset;
12319 cnt++;
12320 DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12321
12322 size = bytesRemaining;
12323 kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12324 DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12325 kr, pid, size, flags, bytesWritten);
12326 if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12327 if (pid == -1) {
12328 pid = 0;
12329 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12330 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12331 } else {
12332 LOG("Insufficient buffer size for only kernel task\n");
12333 break;
12334 }
12335 }
12336 if (kr == KERN_SUCCESS) {
12337 if (bytesWritten == 0) {
12338 MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12339 continue;
12340 }
12341 bytesRemaining -= bytesWritten;
12342 hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12343
12344 memset(hdr->reason, 0x20, sizeof(hdr->reason));
12345
12346 // Compress stackshot and save to NVRAM
12347 {
12348 char *outbuf = (char *)swd_compressed_buffer;
12349 int outlen = 0;
12350 int num_chunks = 0;
12351 int max_chunks = 0;
12352 int leftover = 0;
12353 char nvram_var_name_buffer[20];
12354
12355 outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12356
12357 if (outlen) {
12358 max_chunks = outlen / (2096 - 200);
12359 leftover = outlen % (2096 - 200);
12360
12361 if (max_chunks < 8) {
12362 for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12363 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12364 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12365 LOG("Failed to update NVRAM %d\n", num_chunks);
12366 break;
12367 }
12368 }
12369 if (leftover) {
12370 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12371 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12372 LOG("Failed to update NVRAM with leftovers\n");
12373 }
12374 }
12375 success = 1;
12376 LOG("Successfully saved stackshot to NVRAM\n");
12377 } else {
12378 if (pid == -1) {
12379 LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12380 pid = 0;
12381 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12382 LOG("Compressed failure stackshot of kernel+dexts is too large size=%d bytes\n", outlen);
12383 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12384 } else {
12385 LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12386 break;
12387 }
12388 }
12389 }
12390 }
12391 }
12392 }
12393
12394 if (failureStr[0]) {
12395 // append sleep-wake failure code
12396 char traceCode[80];
12397 snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12398 pmTracer->getTraceData(), pmTracer->getTracePhase());
12399 strlcat(failureStr, traceCode, sizeof(failureStr));
12400 if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12401 DLOG("Failed to write SleepWake failure string\n");
12402 }
12403 }
12404
12405 // force NVRAM sync
12406 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12407 DLOG("Failed to force nvram sync\n");
12408 }
12409
12410 skip_stackshot:
12411 if (wdogTrigger) {
12412 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12413 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12414 // If current boot is due to this watch dog trigger restart in previous boot,
12415 // then don't trigger again until at least 1 successful sleep & wake.
12416 if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12417 LOG("Shutting down due to repeated Sleep/Wake failures\n");
12418 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12419 PEHaltRestart(kPEHaltCPU);
12420 return;
12421 }
12422 }
12423 if (gSwdPanic == 0) {
12424 LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12425 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12426 PEHaltRestart(kPERestartCPU);
12427 }
12428 }
12429 if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12430 DLOG("Failed to write SleepWake failure panic key\n");
12431 }
12432 #if defined(__x86_64__)
12433 if (thread) {
12434 panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12435 } else
12436 #endif /* defined(__x86_64__) */
12437 {
12438 panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12439 }
12440 } else {
12441 gRootDomain->swd_lock = 0;
12442 return;
12443 }
12444 }
12445
12446 void
sleepWakeDebugMemAlloc()12447 IOPMrootDomain::sleepWakeDebugMemAlloc()
12448 {
12449 vm_size_t size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12450
12451 swd_hdr *hdr = NULL;
12452 void *bufPtr = NULL;
12453
12454 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12455
12456
12457 if (kIOSleepWakeWdogOff & gIOKitDebug) {
12458 return;
12459 }
12460
12461 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12462 return;
12463 }
12464
12465 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12466 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12467 size);
12468 if (memDesc == NULL) {
12469 DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12470 goto exit;
12471 }
12472
12473 bufPtr = memDesc->getBytesNoCopy();
12474
12475 // Carve out memory for zlib routines
12476 swd_zs_zmem = (vm_offset_t)bufPtr;
12477 bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12478
12479 // Carve out memory for compressed stackshots
12480 swd_compressed_buffer = bufPtr;
12481 bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12482
12483 // Remaining is used for holding stackshot
12484 hdr = (swd_hdr *)bufPtr;
12485 memset(hdr, 0, sizeof(swd_hdr));
12486
12487 hdr->signature = SWD_HDR_SIGNATURE;
12488 hdr->alloc_size = SWD_STACKSHOT_SIZE;
12489
12490 hdr->spindump_offset = sizeof(swd_hdr);
12491 swd_buffer = (void *)hdr;
12492 swd_memDesc = os::move(memDesc);
12493 DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12494
12495 exit:
12496 gRootDomain->swd_lock = 0;
12497 }
12498
12499 void
sleepWakeDebugSpinDumpMemAlloc()12500 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12501 {
12502 #if UNUSED
12503 vm_size_t size = SWD_SPINDUMP_SIZE;
12504
12505 swd_hdr *hdr = NULL;
12506
12507 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12508
12509 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12510 return;
12511 }
12512
12513 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12514 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12515 SWD_SPINDUMP_SIZE);
12516
12517 if (memDesc == NULL) {
12518 DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12519 goto exit;
12520 }
12521
12522
12523 hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12524 memset(hdr, 0, sizeof(swd_hdr));
12525
12526 hdr->signature = SWD_HDR_SIGNATURE;
12527 hdr->alloc_size = size;
12528
12529 hdr->spindump_offset = sizeof(swd_hdr);
12530 swd_spindump_buffer = (void *)hdr;
12531 swd_spindump_memDesc = os::move(memDesc);
12532
12533 exit:
12534 gRootDomain->swd_lock = 0;
12535 #endif /* UNUSED */
12536 }
12537
12538 void
sleepWakeDebugEnableWdog()12539 IOPMrootDomain::sleepWakeDebugEnableWdog()
12540 {
12541 }
12542
12543 bool
sleepWakeDebugIsWdogEnabled()12544 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12545 {
12546 return !systemBooting && !systemShutdown && !gWillShutdown;
12547 }
12548
12549 void
sleepWakeDebugSaveSpinDumpFile()12550 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12551 {
12552 swd_hdr *hdr = NULL;
12553 errno_t error = EIO;
12554
12555 if (swd_spindump_buffer && gSpinDumpBufferFull) {
12556 hdr = (swd_hdr *)swd_spindump_buffer;
12557
12558 error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12559 (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12560
12561 if (error) {
12562 return;
12563 }
12564
12565 sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12566 (char*)hdr + offsetof(swd_hdr, UUID),
12567 sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12568
12569 gSpinDumpBufferFull = false;
12570 }
12571 }
12572
12573 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12574 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12575 {
12576 struct vnode *vp = NULL;
12577 vfs_context_t ctx = vfs_context_create(vfs_context_current());
12578 kauth_cred_t cred = vfs_context_ucred(ctx);
12579 struct vnode_attr va;
12580 errno_t error = EIO;
12581
12582 if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12583 S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12584 LOG("Failed to open the file %s\n", name);
12585 swd_flags |= SWD_FILEOP_ERROR;
12586 goto exit;
12587 }
12588 VATTR_INIT(&va);
12589 VATTR_WANTED(&va, va_nlink);
12590 /* Don't dump to non-regular files or files with links. */
12591 if (vp->v_type != VREG ||
12592 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12593 LOG("Bailing as this is not a regular file\n");
12594 swd_flags |= SWD_FILEOP_ERROR;
12595 goto exit;
12596 }
12597 VATTR_INIT(&va);
12598 VATTR_SET(&va, va_data_size, 0);
12599 vnode_setattr(vp, &va, ctx);
12600
12601
12602 if (buf != NULL) {
12603 error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12604 UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12605 if (error != 0) {
12606 LOG("Failed to save sleep wake log. err 0x%x\n", error);
12607 swd_flags |= SWD_FILEOP_ERROR;
12608 } else {
12609 DLOG("Saved %d bytes to file %s\n", len, name);
12610 }
12611 }
12612
12613 exit:
12614 if (vp) {
12615 vnode_close(vp, FWRITE, ctx);
12616 }
12617 if (ctx) {
12618 vfs_context_rele(ctx);
12619 }
12620
12621 return error;
12622 }
12623
12624 #else /* defined(__i386__) || defined(__x86_64__) */
12625
12626 void
sleepWakeDebugTrig(bool restart)12627 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12628 {
12629 if (restart) {
12630 if (gSwdPanic == 0) {
12631 return;
12632 }
12633 panic("Sleep/Wake hang detected");
12634 return;
12635 }
12636 }
12637
12638 void
takeStackshot(bool restart)12639 IOPMrootDomain::takeStackshot(bool restart)
12640 {
12641 #pragma unused(restart)
12642 }
12643
12644 void
deleteStackshot()12645 IOPMrootDomain::deleteStackshot()
12646 {
12647 }
12648
12649 void
sleepWakeDebugMemAlloc()12650 IOPMrootDomain::sleepWakeDebugMemAlloc()
12651 {
12652 }
12653
12654 void
saveFailureData2File()12655 IOPMrootDomain::saveFailureData2File()
12656 {
12657 }
12658
12659 void
sleepWakeDebugEnableWdog()12660 IOPMrootDomain::sleepWakeDebugEnableWdog()
12661 {
12662 }
12663
12664 bool
sleepWakeDebugIsWdogEnabled()12665 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12666 {
12667 return false;
12668 }
12669
12670 void
sleepWakeDebugSaveSpinDumpFile()12671 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12672 {
12673 }
12674
12675 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12676 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12677 {
12678 return 0;
12679 }
12680
12681 #endif /* defined(__i386__) || defined(__x86_64__) */
12682
12683