]> git.saurik.com Git - apple/xnu.git/blame_incremental - iokit/Kernel/IOServicePM.cpp
xnu-3248.60.10.tar.gz
[apple/xnu.git] / iokit / Kernel / IOServicePM.cpp
... / ...
CommitLineData
1/*
2 * Copyright (c) 1998-2006 Apple Computer, 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#include <IOKit/assert.h>
30#include <IOKit/IOKitDebug.h>
31#include <IOKit/IOLib.h>
32#include <IOKit/IOMessage.h>
33#include <IOKit/IOPlatformExpert.h>
34#include <IOKit/IOService.h>
35#include <IOKit/IOEventSource.h>
36#include <IOKit/IOWorkLoop.h>
37#include <IOKit/IOCommand.h>
38#include <IOKit/IOTimeStamp.h>
39#include <IOKit/IOReportMacros.h>
40
41#include <IOKit/pwr_mgt/IOPMlog.h>
42#include <IOKit/pwr_mgt/IOPMinformee.h>
43#include <IOKit/pwr_mgt/IOPMinformeeList.h>
44#include <IOKit/pwr_mgt/IOPowerConnection.h>
45#include <IOKit/pwr_mgt/RootDomain.h>
46#include <IOKit/pwr_mgt/IOPMPrivate.h>
47
48#include <sys/proc.h>
49#include <sys/proc_internal.h>
50#include <sys/sysctl.h>
51#include <libkern/OSDebug.h>
52#include <kern/thread.h>
53
54// Required for notification instrumentation
55#include "IOServicePrivate.h"
56#include "IOServicePMPrivate.h"
57#include "IOKitKernelInternal.h"
58
59
60static void settle_timer_expired(thread_call_param_t, thread_call_param_t);
61static void idle_timer_expired(thread_call_param_t, thread_call_param_t);
62static void tellKernelClientApplier(OSObject * object, void * arg);
63static void tellAppClientApplier(OSObject * object, void * arg);
64
65static uint64_t computeTimeDeltaNS( const AbsoluteTime * start )
66{
67 AbsoluteTime now;
68 uint64_t nsec;
69
70 clock_get_uptime(&now);
71 SUB_ABSOLUTETIME(&now, start);
72 absolutetime_to_nanoseconds(now, &nsec);
73 return nsec;
74}
75
76#if PM_VARS_SUPPORT
77OSDefineMetaClassAndStructors(IOPMprot, OSObject)
78#endif
79
80//******************************************************************************
81// Globals
82//******************************************************************************
83
84static bool gIOPMInitialized = false;
85static uint32_t gIOPMBusyRequestCount = 0;
86static uint32_t gIOPMWorkInvokeCount = 0;
87static uint32_t gIOPMTickleGeneration = 0;
88static IOWorkLoop * gIOPMWorkLoop = 0;
89static IOPMRequestQueue * gIOPMRequestQueue = 0;
90static IOPMRequestQueue * gIOPMReplyQueue = 0;
91static IOPMWorkQueue * gIOPMWorkQueue = 0;
92static IOPMCompletionQueue * gIOPMCompletionQueue = 0;
93static IOPMRequest * gIOPMRequest = 0;
94static IOService * gIOPMRootNode = 0;
95static IOPlatformExpert * gPlatform = 0;
96
97static char gIOSpinDumpKextName[128];
98static char gIOSpinDumpDelayType[16];
99static uint32_t gIOSpinDumpDelayDuration = 0;
100
101static SYSCTL_STRING(_debug, OID_AUTO, swd_kext_name,
102 CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
103 &gIOSpinDumpKextName, sizeof(gIOSpinDumpKextName), "");
104static SYSCTL_STRING(_debug, OID_AUTO, swd_delay_type,
105 CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
106 &gIOSpinDumpDelayType, sizeof(gIOSpinDumpDelayType), "");
107static SYSCTL_INT(_debug, OID_AUTO, swd_delay_duration,
108 CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
109 &gIOSpinDumpDelayDuration, 0, "");
110
111const OSSymbol * gIOPMPowerClientDevice = 0;
112const OSSymbol * gIOPMPowerClientDriver = 0;
113const OSSymbol * gIOPMPowerClientChildProxy = 0;
114const OSSymbol * gIOPMPowerClientChildren = 0;
115const OSSymbol * gIOPMPowerClientRootDomain = 0;
116
117static const OSSymbol * gIOPMPowerClientAdvisoryTickle = 0;
118static bool gIOPMAdvisoryTickleEnabled = true;
119static thread_t gIOPMWatchDogThread = NULL;
120uint32_t gCanSleepTimeout = 0;
121
122static uint32_t getPMRequestType( void )
123{
124 uint32_t type = kIOPMRequestTypeInvalid;
125 if (gIOPMRequest)
126 type = gIOPMRequest->getType();
127 return type;
128}
129
130static IOPMRequestTag getPMRequestTag( void )
131{
132 IOPMRequestTag tag = 0;
133 if (gIOPMRequest &&
134 (gIOPMRequest->getType() == kIOPMRequestTypeRequestPowerStateOverride))
135 {
136 tag = gIOPMRequest->fRequestTag;
137 }
138 return tag;
139}
140
141SYSCTL_UINT(_kern, OID_AUTO, pmtimeout, CTLFLAG_RW | CTLFLAG_LOCKED, &gCanSleepTimeout, 0, "Power Management Timeout");
142
143//******************************************************************************
144// Macros
145//******************************************************************************
146
147#define PM_ERROR(x...) do { kprintf(x);IOLog(x); \
148 } while (false)
149#define PM_LOG(x...) do { kprintf(x); } while (false)
150
151#define PM_LOG1(x...) do { \
152 if (kIOLogDebugPower & gIOKitDebug) \
153 kprintf(x); } while (false)
154
155#define PM_LOG2(x...) do { \
156 if (kIOLogDebugPower & gIOKitDebug) \
157 kprintf(x); } while (false)
158
159#if 0
160#define PM_LOG3(x...) do { kprintf(x); } while (false)
161#else
162#define PM_LOG3(x...)
163#endif
164
165#define RD_LOG(x...) do { \
166 if ((kIOLogPMRootDomain & gIOKitDebug) && \
167 (getPMRootDomain() == this)) { \
168 kprintf("PMRD: " x); \
169 }} while (false)
170#define PM_ASSERT_IN_GATE(x) \
171do { \
172 assert(gIOPMWorkLoop->inGate()); \
173} while(false)
174
175#define PM_LOCK() IOLockLock(fPMLock)
176#define PM_UNLOCK() IOLockUnlock(fPMLock)
177#define PM_LOCK_SLEEP(event, dl) IOLockSleepDeadline(fPMLock, event, dl, THREAD_UNINT)
178#define PM_LOCK_WAKEUP(event) IOLockWakeup(fPMLock, event, false)
179
180#define us_per_s 1000000
181#define ns_per_us 1000
182#define k30Seconds (30*us_per_s)
183#define k5Seconds ( 5*us_per_s)
184#define kCanSleepMaxTimeReq k30Seconds
185#define kMaxTimeRequested k30Seconds
186#define kMinAckTimeoutTicks (10*1000000)
187#define kIOPMTardyAckSPSKey "IOPMTardyAckSetPowerState"
188#define kIOPMTardyAckPSCKey "IOPMTardyAckPowerStateChange"
189#define kPwrMgtKey "IOPowerManagement"
190
191#define OUR_PMLog(t, a, b) do { \
192 if (gIOKitDebug & kIOLogPower) \
193 pwrMgt->pmPrint(t, a, b); \
194 if (gIOKitTrace & kIOTracePowerMgmt) \
195 pwrMgt->pmTrace(t, a, b); \
196 } while(0)
197
198#define NS_TO_MS(nsec) ((int)((nsec) / 1000000ULL))
199#define NS_TO_US(nsec) ((int)((nsec) / 1000ULL))
200
201#define SUPPORT_IDLE_CANCEL 1
202
203#define kIOPMPowerStateMax 0xFFFFFFFF
204#define kInvalidTicklePowerState kIOPMPowerStateMax
205
206#define kNoTickleCancelWindow (60ULL * 1000ULL * 1000ULL * 1000ULL)
207
208#define IS_PM_ROOT (this == gIOPMRootNode)
209#define IS_ROOT_DOMAIN (getPMRootDomain() == this)
210#define IS_POWER_DROP (StateOrder(fHeadNotePowerState) < StateOrder(fCurrentPowerState))
211#define IS_POWER_RISE (StateOrder(fHeadNotePowerState) > StateOrder(fCurrentPowerState))
212
213// log setPowerStates longer than (ns):
214#if defined(__i386__) || defined(__x86_64__)
215#define LOG_SETPOWER_TIMES (300ULL * 1000ULL * 1000ULL)
216#else
217#define LOG_SETPOWER_TIMES (50ULL * 1000ULL * 1000ULL)
218#endif
219// log app responses longer than (ns):
220#define LOG_APP_RESPONSE_TIMES (100ULL * 1000ULL * 1000ULL)
221// use message tracer to log messages longer than (ns):
222#define LOG_APP_RESPONSE_MSG_TRACER (3 * 1000ULL * 1000ULL * 1000ULL)
223
224enum {
225 kReserveDomainPower = 1
226};
227
228#define MS_PUSH(n) \
229 do { assert(kIOPM_BadMachineState == fSavedMachineState); \
230 assert(kIOPM_BadMachineState != n); \
231 fSavedMachineState = n; } while (false)
232
233#define MS_POP() \
234 do { assert(kIOPM_BadMachineState != fSavedMachineState); \
235 fMachineState = fSavedMachineState; \
236 fSavedMachineState = kIOPM_BadMachineState; } while (false)
237
238#define PM_ACTION_0(a) \
239 do { if (fPMActions.a) { \
240 (fPMActions.a)(fPMActions.target, this, &fPMActions); } \
241 } while (false)
242
243#define PM_ACTION_2(a, x, y) \
244 do { if (fPMActions.a) { \
245 (fPMActions.a)(fPMActions.target, this, &fPMActions, x, y, \
246 getPMRequestTag()); } \
247 } while (false)
248
249#define PM_ACTION_3(a, x, y, z) \
250 do { if (fPMActions.a) { \
251 (fPMActions.a)(fPMActions.target, this, &fPMActions, x, y, z); } \
252 } while (false)
253
254static OSNumber * copyClientIDForNotification(
255 OSObject *object,
256 IOPMInterestContext *context);
257
258static void logClientIDForNotification(
259 OSObject *object,
260 IOPMInterestContext *context,
261 const char *logString);
262
263//*********************************************************************************
264// PM machine states
265//
266// Check kgmacros after modifying machine states.
267//*********************************************************************************
268
269enum {
270 kIOPM_Finished = 0,
271
272 kIOPM_OurChangeTellClientsPowerDown = 1,
273 kIOPM_OurChangeTellUserPMPolicyPowerDown = 2,
274 kIOPM_OurChangeTellPriorityClientsPowerDown = 3,
275 kIOPM_OurChangeNotifyInterestedDriversWillChange = 4,
276 kIOPM_OurChangeSetPowerState = 5,
277 kIOPM_OurChangeWaitForPowerSettle = 6,
278 kIOPM_OurChangeNotifyInterestedDriversDidChange = 7,
279 kIOPM_OurChangeTellCapabilityDidChange = 8,
280 kIOPM_OurChangeFinish = 9,
281
282 kIOPM_ParentChangeTellPriorityClientsPowerDown = 10,
283 kIOPM_ParentChangeNotifyInterestedDriversWillChange = 11,
284 kIOPM_ParentChangeSetPowerState = 12,
285 kIOPM_ParentChangeWaitForPowerSettle = 13,
286 kIOPM_ParentChangeNotifyInterestedDriversDidChange = 14,
287 kIOPM_ParentChangeTellCapabilityDidChange = 15,
288 kIOPM_ParentChangeAcknowledgePowerChange = 16,
289
290 kIOPM_NotifyChildrenStart = 17,
291 kIOPM_NotifyChildrenOrdered = 18,
292 kIOPM_NotifyChildrenDelayed = 19,
293 kIOPM_SyncTellClientsPowerDown = 20,
294 kIOPM_SyncTellPriorityClientsPowerDown = 21,
295 kIOPM_SyncNotifyWillChange = 22,
296 kIOPM_SyncNotifyDidChange = 23,
297 kIOPM_SyncTellCapabilityDidChange = 24,
298 kIOPM_SyncFinish = 25,
299 kIOPM_TellCapabilityChangeDone = 26,
300 kIOPM_DriverThreadCallDone = 27,
301
302 kIOPM_BadMachineState = 0xFFFFFFFF
303};
304
305//*********************************************************************************
306// [public] PMinit
307//
308// Initialize power management.
309//*********************************************************************************
310
311void IOService::PMinit( void )
312{
313 if ( !initialized )
314 {
315 if ( !gIOPMInitialized )
316 {
317 gPlatform = getPlatform();
318 gIOPMWorkLoop = IOWorkLoop::workLoop();
319 if (gIOPMWorkLoop)
320 {
321 gIOPMRequestQueue = IOPMRequestQueue::create(
322 this, OSMemberFunctionCast(IOPMRequestQueue::Action,
323 this, &IOService::actionPMRequestQueue));
324
325 gIOPMReplyQueue = IOPMRequestQueue::create(
326 this, OSMemberFunctionCast(IOPMRequestQueue::Action,
327 this, &IOService::actionPMReplyQueue));
328
329 gIOPMWorkQueue = IOPMWorkQueue::create(this,
330 OSMemberFunctionCast(IOPMWorkQueue::Action, this,
331 &IOService::actionPMWorkQueueInvoke),
332 OSMemberFunctionCast(IOPMWorkQueue::Action, this,
333 &IOService::actionPMWorkQueueRetire));
334
335 gIOPMCompletionQueue = IOPMCompletionQueue::create(
336 this, OSMemberFunctionCast(IOPMCompletionQueue::Action,
337 this, &IOService::actionPMCompletionQueue));
338
339 if (gIOPMWorkLoop->addEventSource(gIOPMRequestQueue) !=
340 kIOReturnSuccess)
341 {
342 gIOPMRequestQueue->release();
343 gIOPMRequestQueue = 0;
344 }
345
346 if (gIOPMWorkLoop->addEventSource(gIOPMReplyQueue) !=
347 kIOReturnSuccess)
348 {
349 gIOPMReplyQueue->release();
350 gIOPMReplyQueue = 0;
351 }
352
353 if (gIOPMWorkLoop->addEventSource(gIOPMWorkQueue) !=
354 kIOReturnSuccess)
355 {
356 gIOPMWorkQueue->release();
357 gIOPMWorkQueue = 0;
358 }
359
360 // Must be added after the work queue, which pushes request
361 // to the completion queue without signaling the work loop.
362 if (gIOPMWorkLoop->addEventSource(gIOPMCompletionQueue) !=
363 kIOReturnSuccess)
364 {
365 gIOPMCompletionQueue->release();
366 gIOPMCompletionQueue = 0;
367 }
368
369 gIOPMPowerClientDevice =
370 OSSymbol::withCStringNoCopy( "DevicePowerState" );
371
372 gIOPMPowerClientDriver =
373 OSSymbol::withCStringNoCopy( "DriverPowerState" );
374
375 gIOPMPowerClientChildProxy =
376 OSSymbol::withCStringNoCopy( "ChildProxyPowerState" );
377
378 gIOPMPowerClientChildren =
379 OSSymbol::withCStringNoCopy( "ChildrenPowerState" );
380
381 gIOPMPowerClientAdvisoryTickle =
382 OSSymbol::withCStringNoCopy( "AdvisoryTicklePowerState" );
383
384 gIOPMPowerClientRootDomain =
385 OSSymbol::withCStringNoCopy( "RootDomainPower" );
386
387 gIOSpinDumpKextName[0] = '\0';
388 gIOSpinDumpDelayType[0] = '\0';
389 }
390
391 if (gIOPMRequestQueue && gIOPMReplyQueue && gIOPMCompletionQueue)
392 gIOPMInitialized = true;
393 }
394 if (!gIOPMInitialized)
395 return;
396
397 pwrMgt = new IOServicePM;
398 pwrMgt->init();
399 setProperty(kPwrMgtKey, pwrMgt);
400
401 queue_init(&pwrMgt->WorkChain);
402 queue_init(&pwrMgt->RequestHead);
403 queue_init(&pwrMgt->PMDriverCallQueue);
404
405 fOwner = this;
406 fPMLock = IOLockAlloc();
407 fInterestedDrivers = new IOPMinformeeList;
408 fInterestedDrivers->initialize();
409 fDesiredPowerState = kPowerStateZero;
410 fDeviceDesire = kPowerStateZero;
411 fInitialPowerChange = true;
412 fInitialSetPowerState = true;
413 fPreviousRequestPowerFlags = 0;
414 fDeviceOverrideEnabled = false;
415 fMachineState = kIOPM_Finished;
416 fSavedMachineState = kIOPM_BadMachineState;
417 fIdleTimerMinPowerState = kPowerStateZero;
418 fActivityLock = IOLockAlloc();
419 fStrictTreeOrder = false;
420 fActivityTicklePowerState = kInvalidTicklePowerState;
421 fAdvisoryTicklePowerState = kInvalidTicklePowerState;
422 fControllingDriver = NULL;
423 fPowerStates = NULL;
424 fNumberOfPowerStates = 0;
425 fCurrentPowerState = kPowerStateZero;
426 fParentsCurrentPowerFlags = 0;
427 fMaxPowerState = kPowerStateZero;
428 fName = getName();
429 fParentsKnowState = false;
430 fSerialNumber = 0;
431 fResponseArray = NULL;
432 fNotifyClientArray = NULL;
433 fCurrentPowerConsumption = kIOPMUnknown;
434 fOverrideMaxPowerState = kIOPMPowerStateMax;
435
436 if (!gIOPMRootNode && (getParentEntry(gIOPowerPlane) == getRegistryRoot()))
437 {
438 gIOPMRootNode = this;
439 fParentsKnowState = true;
440 }
441 else if (getProperty(kIOPMResetPowerStateOnWakeKey) == kOSBooleanTrue)
442 {
443 fResetPowerStateOnWake = true;
444 }
445
446 if (IS_ROOT_DOMAIN)
447 {
448 fWatchdogTimer = thread_call_allocate(
449 &IOService::watchdog_timer_expired, (thread_call_param_t)this);
450 }
451
452 fAckTimer = thread_call_allocate(
453 &IOService::ack_timer_expired, (thread_call_param_t)this);
454 fSettleTimer = thread_call_allocate(
455 &settle_timer_expired, (thread_call_param_t)this);
456 fIdleTimer = thread_call_allocate(
457 &idle_timer_expired, (thread_call_param_t)this);
458 fDriverCallEntry = thread_call_allocate(
459 (thread_call_func_t) &IOService::pmDriverCallout, this);
460 assert(fDriverCallEntry);
461 if (kIOKextSpinDump & gIOKitDebug)
462 {
463 fSpinDumpTimer = thread_call_allocate(
464 &IOService::spindump_timer_expired, (thread_call_param_t)this);
465 }
466
467 // Check for powerChangeDone override.
468 if (OSMemberFunctionCast(void (*)(void),
469 getResourceService(), &IOService::powerChangeDone) !=
470 OSMemberFunctionCast(void (*)(void),
471 this, &IOService::powerChangeDone))
472 {
473 fPCDFunctionOverride = true;
474 }
475
476#if PM_VARS_SUPPORT
477 IOPMprot * prot = new IOPMprot;
478 if (prot)
479 {
480 prot->init();
481 prot->ourName = fName;
482 prot->thePlatform = gPlatform;
483 fPMVars = prot;
484 pm_vars = prot;
485 }
486#else
487 pm_vars = (void *) (uintptr_t) true;
488#endif
489
490 initialized = true;
491 }
492}
493
494//*********************************************************************************
495// [private] PMfree
496//
497// Free the data created by PMinit. Only called from IOService::free().
498//*********************************************************************************
499
500void IOService::PMfree( void )
501{
502 initialized = false;
503 pm_vars = 0;
504
505 if ( pwrMgt )
506 {
507 assert(fMachineState == kIOPM_Finished);
508 assert(fInsertInterestSet == NULL);
509 assert(fRemoveInterestSet == NULL);
510 assert(fNotifyChildArray == NULL);
511 assert(queue_empty(&pwrMgt->RequestHead));
512 assert(queue_empty(&fPMDriverCallQueue));
513
514 if (fWatchdogTimer) {
515 thread_call_cancel(fWatchdogTimer);
516 thread_call_free(fWatchdogTimer);
517 fWatchdogTimer = NULL;
518 }
519
520 if ( fSettleTimer ) {
521 thread_call_cancel(fSettleTimer);
522 thread_call_free(fSettleTimer);
523 fSettleTimer = NULL;
524 }
525 if ( fAckTimer ) {
526 thread_call_cancel(fAckTimer);
527 thread_call_free(fAckTimer);
528 fAckTimer = NULL;
529 }
530 if ( fIdleTimer ) {
531 thread_call_cancel(fIdleTimer);
532 thread_call_free(fIdleTimer);
533 fIdleTimer = NULL;
534 }
535 if ( fDriverCallEntry ) {
536 thread_call_free(fDriverCallEntry);
537 fDriverCallEntry = NULL;
538 }
539 if ( fSpinDumpTimer ) {
540 thread_call_cancel(fSpinDumpTimer);
541 thread_call_free(fSpinDumpTimer);
542 fSpinDumpTimer = NULL;
543 }
544 if ( fPMLock ) {
545 IOLockFree(fPMLock);
546 fPMLock = NULL;
547 }
548 if ( fActivityLock ) {
549 IOLockFree(fActivityLock);
550 fActivityLock = NULL;
551 }
552 if ( fInterestedDrivers ) {
553 fInterestedDrivers->release();
554 fInterestedDrivers = NULL;
555 }
556 if (fDriverCallParamSlots && fDriverCallParamPtr) {
557 IODelete(fDriverCallParamPtr, DriverCallParam, fDriverCallParamSlots);
558 fDriverCallParamPtr = 0;
559 fDriverCallParamSlots = 0;
560 }
561 if ( fResponseArray ) {
562 fResponseArray->release();
563 fResponseArray = NULL;
564 }
565 if ( fNotifyClientArray ) {
566 fNotifyClientArray->release();
567 fNotifyClientArray = NULL;
568 }
569 if (fPowerStates && fNumberOfPowerStates) {
570 IODelete(fPowerStates, IOPMPSEntry, fNumberOfPowerStates);
571 fNumberOfPowerStates = 0;
572 fPowerStates = NULL;
573 }
574 if (fPowerClients) {
575 fPowerClients->release();
576 fPowerClients = 0;
577 }
578
579#if PM_VARS_SUPPORT
580 if (fPMVars)
581 {
582 fPMVars->release();
583 fPMVars = 0;
584 }
585#endif
586
587 pwrMgt->release();
588 pwrMgt = 0;
589 }
590}
591
592void IOService::PMDebug( uint32_t event, uintptr_t param1, uintptr_t param2 )
593{
594 OUR_PMLog(event, param1, param2);
595}
596
597//*********************************************************************************
598// [public] joinPMtree
599//
600// A policy-maker calls its nub here when initializing, to be attached into
601// the power management hierarchy. The default function is to call the
602// platform expert, which knows how to do it. This method is overridden
603// by a nub subclass which may either know how to do it, or may need to
604// take other action.
605//
606// This may be the only "power management" method used in a nub,
607// meaning it may not be initialized for power management.
608//*********************************************************************************
609
610void IOService::joinPMtree( IOService * driver )
611{
612 IOPlatformExpert * platform;
613
614 platform = getPlatform();
615 assert(platform != 0);
616 platform->PMRegisterDevice(this, driver);
617}
618
619#ifndef __LP64__
620//*********************************************************************************
621// [deprecated] youAreRoot
622//
623// Power Managment is informing us that we are the root power domain.
624//*********************************************************************************
625
626IOReturn IOService::youAreRoot( void )
627{
628 return IOPMNoErr;
629}
630#endif /* !__LP64__ */
631
632//*********************************************************************************
633// [public] PMstop
634//
635// Immediately stop driver callouts. Schedule an async stop request to detach
636// from power plane.
637//*********************************************************************************
638
639void IOService::PMstop( void )
640{
641 IOPMRequest * request;
642
643 if (!initialized)
644 return;
645
646 PM_LOCK();
647
648 if (fLockedFlags.PMStop)
649 {
650 PM_LOG2("%s: PMstop() already stopped\n", fName);
651 PM_UNLOCK();
652 return;
653 }
654
655 // Inhibit future driver calls.
656 fLockedFlags.PMStop = true;
657
658 // Wait for all prior driver calls to finish.
659 waitForPMDriverCall();
660
661 PM_UNLOCK();
662
663 // The rest of the work is performed async.
664 request = acquirePMRequest( this, kIOPMRequestTypePMStop );
665 if (request)
666 {
667 PM_LOG2("%s: %p PMstop\n", getName(), OBFUSCATE(this));
668 submitPMRequest( request );
669 }
670}
671
672//*********************************************************************************
673// [private] handlePMstop
674//
675// Disconnect the node from all parents and children in the power plane.
676//*********************************************************************************
677
678void IOService::handlePMstop( IOPMRequest * request )
679{
680 OSIterator * iter;
681 OSObject * next;
682 IOPowerConnection * connection;
683 IOService * theChild;
684 IOService * theParent;
685
686 PM_ASSERT_IN_GATE();
687 PM_LOG2("%s: %p %s start\n", getName(), OBFUSCATE(this), __FUNCTION__);
688
689 // remove driver from prevent system sleep lists
690 getPMRootDomain()->updatePreventIdleSleepList(this, false);
691 getPMRootDomain()->updatePreventSystemSleepList(this, false);
692
693 // remove the property
694 removeProperty(kPwrMgtKey);
695
696 // detach parents
697 iter = getParentIterator(gIOPowerPlane);
698 if ( iter )
699 {
700 while ( (next = iter->getNextObject()) )
701 {
702 if ( (connection = OSDynamicCast(IOPowerConnection, next)) )
703 {
704 theParent = (IOService *)connection->copyParentEntry(gIOPowerPlane);
705 if ( theParent )
706 {
707 theParent->removePowerChild(connection);
708 theParent->release();
709 }
710 }
711 }
712 iter->release();
713 }
714
715 // detach IOConnections
716 detachAbove( gIOPowerPlane );
717
718 // no more power state changes
719 fParentsKnowState = false;
720
721 // detach children
722 iter = getChildIterator(gIOPowerPlane);
723 if ( iter )
724 {
725 while ( (next = iter->getNextObject()) )
726 {
727 if ( (connection = OSDynamicCast(IOPowerConnection, next)) )
728 {
729 theChild = ((IOService *)(connection->copyChildEntry(gIOPowerPlane)));
730 if ( theChild )
731 {
732 // detach nub from child
733 connection->detachFromChild(theChild, gIOPowerPlane);
734 theChild->release();
735 }
736 // detach us from nub
737 detachFromChild(connection, gIOPowerPlane);
738 }
739 }
740 iter->release();
741 }
742
743 // Remove all interested drivers from the list, including the power
744 // controlling driver.
745 //
746 // Usually, the controlling driver and the policy-maker functionality
747 // are implemented by the same object, and without the deregistration,
748 // the object will be holding an extra retain on itself, and cannot
749 // be freed.
750
751 if ( fInterestedDrivers )
752 {
753 IOPMinformeeList * list = fInterestedDrivers;
754 IOPMinformee * item;
755
756 PM_LOCK();
757 while ((item = list->firstInList()))
758 {
759 list->removeFromList(item->whatObject);
760 }
761 PM_UNLOCK();
762 }
763
764 // Clear idle period to prevent idleTimerExpired() from servicing
765 // idle timer expirations.
766
767 fIdleTimerPeriod = 0;
768 if (fIdleTimer && thread_call_cancel(fIdleTimer))
769 release();
770
771 PM_LOG2("%s: %p %s done\n", getName(), OBFUSCATE(this), __FUNCTION__);
772}
773
774//*********************************************************************************
775// [public] addPowerChild
776//
777// Power Management is informing us who our children are.
778//*********************************************************************************
779
780IOReturn IOService::addPowerChild( IOService * child )
781{
782 IOPowerConnection * connection = 0;
783 IOPMRequest * requests[3] = {0, 0, 0};
784 OSIterator * iter;
785 bool ok = true;
786
787 if (!child)
788 return kIOReturnBadArgument;
789
790 if (!initialized || !child->initialized)
791 return IOPMNotYetInitialized;
792
793 OUR_PMLog( kPMLogAddChild, (uintptr_t) child, 0 );
794
795 do {
796 // Is this child already one of our children?
797
798 iter = child->getParentIterator( gIOPowerPlane );
799 if ( iter )
800 {
801 IORegistryEntry * entry;
802 OSObject * next;
803
804 while ((next = iter->getNextObject()))
805 {
806 if ((entry = OSDynamicCast(IORegistryEntry, next)) &&
807 isChild(entry, gIOPowerPlane))
808 {
809 ok = false;
810 break;
811 }
812 }
813 iter->release();
814 }
815 if (!ok)
816 {
817 PM_LOG("%s: %s (%p) is already a child\n",
818 getName(), child->getName(), OBFUSCATE(child));
819 break;
820 }
821
822 // Add the child to the power plane immediately, but the
823 // joining connection is marked as not ready.
824 // We want the child to appear in the power plane before
825 // returning to the caller, but don't want the caller to
826 // block on the PM work loop.
827
828 connection = new IOPowerConnection;
829 if (!connection)
830 break;
831
832 // Create a chain of PM requests to perform the bottom-half
833 // work from the PM work loop.
834
835 requests[0] = acquirePMRequest(
836 /* target */ this,
837 /* type */ kIOPMRequestTypeAddPowerChild1 );
838
839 requests[1] = acquirePMRequest(
840 /* target */ child,
841 /* type */ kIOPMRequestTypeAddPowerChild2 );
842
843 requests[2] = acquirePMRequest(
844 /* target */ this,
845 /* type */ kIOPMRequestTypeAddPowerChild3 );
846
847 if (!requests[0] || !requests[1] || !requests[2])
848 break;
849
850 requests[0]->attachNextRequest( requests[1] );
851 requests[1]->attachNextRequest( requests[2] );
852
853 connection->init();
854 connection->start(this);
855 connection->setAwaitingAck(false);
856 connection->setReadyFlag(false);
857
858 attachToChild( connection, gIOPowerPlane );
859 connection->attachToChild( child, gIOPowerPlane );
860
861 // connection needs to be released
862 requests[0]->fArg0 = connection;
863 requests[1]->fArg0 = connection;
864 requests[2]->fArg0 = connection;
865
866 submitPMRequests( requests, 3 );
867 return kIOReturnSuccess;
868 }
869 while (false);
870
871 if (connection) connection->release();
872 if (requests[0]) releasePMRequest(requests[0]);
873 if (requests[1]) releasePMRequest(requests[1]);
874 if (requests[2]) releasePMRequest(requests[2]);
875
876 // Silent failure, to prevent platform drivers from adding the child
877 // to the root domain.
878
879 return kIOReturnSuccess;
880}
881
882//*********************************************************************************
883// [private] addPowerChild1
884//
885// Step 1/3 of adding a power child. Called on the power parent.
886//*********************************************************************************
887
888void IOService::addPowerChild1( IOPMRequest * request )
889{
890 IOPMPowerStateIndex tempDesire = kPowerStateZero;
891
892 // Make us temporary usable before adding the child.
893
894 PM_ASSERT_IN_GATE();
895 OUR_PMLog( kPMLogMakeUsable, kPMLogMakeUsable, 0 );
896
897 if (fControllingDriver && inPlane(gIOPowerPlane) && fParentsKnowState)
898 {
899 tempDesire = fHighestPowerState;
900 }
901
902 if ((tempDesire != kPowerStateZero) &&
903 (IS_PM_ROOT || (StateOrder(fMaxPowerState) >= StateOrder(tempDesire))))
904 {
905 adjustPowerState(tempDesire);
906 }
907}
908
909//*********************************************************************************
910// [private] addPowerChild2
911//
912// Step 2/3 of adding a power child. Called on the joining child.
913// Execution blocked behind addPowerChild1.
914//*********************************************************************************
915
916void IOService::addPowerChild2( IOPMRequest * request )
917{
918 IOPowerConnection * connection = (IOPowerConnection *) request->fArg0;
919 IOService * parent;
920 IOPMPowerFlags powerFlags;
921 bool knowsState;
922 unsigned long powerState;
923 unsigned long tempDesire;
924
925 PM_ASSERT_IN_GATE();
926 parent = (IOService *) connection->getParentEntry(gIOPowerPlane);
927
928 if (!parent || !inPlane(gIOPowerPlane))
929 {
930 PM_LOG("%s: addPowerChild2 not in power plane\n", getName());
931 return;
932 }
933
934 // Parent will be waiting for us to complete this stage.
935 // It is safe to directly access parent's vars.
936
937 knowsState = (parent->fPowerStates) && (parent->fParentsKnowState);
938 powerState = parent->fCurrentPowerState;
939
940 if (knowsState)
941 powerFlags = parent->fPowerStates[powerState].outputPowerFlags;
942 else
943 powerFlags = 0;
944
945 // Set our power parent.
946
947 OUR_PMLog(kPMLogSetParent, knowsState, powerFlags);
948
949 setParentInfo( powerFlags, connection, knowsState );
950
951 connection->setReadyFlag(true);
952
953 if ( fControllingDriver && fParentsKnowState )
954 {
955 fMaxPowerState = fControllingDriver->maxCapabilityForDomainState(fParentsCurrentPowerFlags);
956 // initially change into the state we are already in
957 tempDesire = fControllingDriver->initialPowerStateForDomainState(fParentsCurrentPowerFlags);
958 fPreviousRequestPowerFlags = (IOPMPowerFlags)(-1);
959 adjustPowerState(tempDesire);
960 }
961
962 getPMRootDomain()->tagPowerPlaneService(this, &fPMActions);
963}
964
965//*********************************************************************************
966// [private] addPowerChild3
967//
968// Step 3/3 of adding a power child. Called on the parent.
969// Execution blocked behind addPowerChild2.
970//*********************************************************************************
971
972void IOService::addPowerChild3( IOPMRequest * request )
973{
974 IOPowerConnection * connection = (IOPowerConnection *) request->fArg0;
975 IOService * child;
976 IOPMrootDomain * rootDomain = getPMRootDomain();
977
978 PM_ASSERT_IN_GATE();
979 child = (IOService *) connection->getChildEntry(gIOPowerPlane);
980
981 if (child && inPlane(gIOPowerPlane))
982 {
983 if ((this != rootDomain) && child->getProperty("IOPMStrictTreeOrder"))
984 {
985 PM_LOG1("%s: strict PM order enforced\n", getName());
986 fStrictTreeOrder = true;
987 }
988
989 if (rootDomain)
990 rootDomain->joinAggressiveness( child );
991 }
992 else
993 {
994 PM_LOG("%s: addPowerChild3 not in power plane\n", getName());
995 }
996
997 connection->release();
998}
999
1000#ifndef __LP64__
1001//*********************************************************************************
1002// [deprecated] setPowerParent
1003//
1004// Power Management is informing us who our parent is.
1005// If we have a controlling driver, find out, given our newly-informed
1006// power domain state, what state it would be in, and then tell it
1007// to assume that state.
1008//*********************************************************************************
1009
1010IOReturn IOService::setPowerParent(
1011 IOPowerConnection * theParent, bool stateKnown, IOPMPowerFlags powerFlags )
1012{
1013 return kIOReturnUnsupported;
1014}
1015#endif /* !__LP64__ */
1016
1017//*********************************************************************************
1018// [public] removePowerChild
1019//
1020// Called on a parent whose child is being removed by PMstop().
1021//*********************************************************************************
1022
1023IOReturn IOService::removePowerChild( IOPowerConnection * theNub )
1024{
1025 IORegistryEntry * theChild;
1026
1027 PM_ASSERT_IN_GATE();
1028 OUR_PMLog( kPMLogRemoveChild, 0, 0 );
1029
1030 theNub->retain();
1031
1032 // detach nub from child
1033 theChild = theNub->copyChildEntry(gIOPowerPlane);
1034 if ( theChild )
1035 {
1036 theNub->detachFromChild(theChild, gIOPowerPlane);
1037 theChild->release();
1038 }
1039 // detach from the nub
1040 detachFromChild(theNub, gIOPowerPlane);
1041
1042 // Are we awaiting an ack from this child?
1043 if ( theNub->getAwaitingAck() )
1044 {
1045 // yes, pretend we got one
1046 theNub->setAwaitingAck(false);
1047 if (fHeadNotePendingAcks != 0 )
1048 {
1049 // that's one fewer ack to worry about
1050 fHeadNotePendingAcks--;
1051
1052 // is that the last?
1053 if ( fHeadNotePendingAcks == 0 )
1054 {
1055 stop_ack_timer();
1056
1057 // This parent may have a request in the work queue that is
1058 // blocked on fHeadNotePendingAcks=0. And removePowerChild()
1059 // is called while executing the child's PMstop request so they
1060 // can occur simultaneously. IOPMWorkQueue::checkForWork() must
1061 // restart and check all request queues again.
1062
1063 gIOPMWorkQueue->incrementProducerCount();
1064 }
1065 }
1066 }
1067
1068 theNub->release();
1069
1070 // A child has gone away, re-scan children desires and clamp bits.
1071 // The fPendingAdjustPowerRequest helps to reduce redundant parent work.
1072
1073 if (!fAdjustPowerScheduled)
1074 {
1075 IOPMRequest * request;
1076 request = acquirePMRequest( this, kIOPMRequestTypeAdjustPowerState );
1077 if (request)
1078 {
1079 submitPMRequest( request );
1080 fAdjustPowerScheduled = true;
1081 }
1082 }
1083
1084 return IOPMNoErr;
1085}
1086
1087//*********************************************************************************
1088// [public] registerPowerDriver
1089//
1090// A driver has called us volunteering to control power to our device.
1091//*********************************************************************************
1092
1093IOReturn IOService::registerPowerDriver(
1094 IOService * powerDriver,
1095 IOPMPowerState * powerStates,
1096 unsigned long numberOfStates )
1097{
1098 IOPMRequest * request;
1099 IOPMPSEntry * powerStatesCopy = 0;
1100 IOPMPowerStateIndex stateOrder;
1101 IOReturn error = kIOReturnSuccess;
1102
1103 if (!initialized)
1104 return IOPMNotYetInitialized;
1105
1106 if (!powerStates || (numberOfStates < 2))
1107 {
1108 OUR_PMLog(kPMLogControllingDriverErr5, numberOfStates, 0);
1109 return kIOReturnBadArgument;
1110 }
1111
1112 if (!powerDriver || !powerDriver->initialized)
1113 {
1114 OUR_PMLog(kPMLogControllingDriverErr4, 0, 0);
1115 return kIOReturnBadArgument;
1116 }
1117
1118 if (powerStates[0].version > kIOPMPowerStateVersion2)
1119 {
1120 OUR_PMLog(kPMLogControllingDriverErr1, powerStates[0].version, 0);
1121 return kIOReturnBadArgument;
1122 }
1123
1124 do {
1125 // Make a copy of the supplied power state array.
1126 powerStatesCopy = IONew(IOPMPSEntry, numberOfStates);
1127 if (!powerStatesCopy)
1128 {
1129 error = kIOReturnNoMemory;
1130 break;
1131 }
1132
1133 // Initialize to bogus values
1134 for (IOPMPowerStateIndex i = 0; i < numberOfStates; i++)
1135 powerStatesCopy[i].stateOrderToIndex = kIOPMPowerStateMax;
1136
1137 for (uint32_t i = 0; i < numberOfStates; i++)
1138 {
1139 powerStatesCopy[i].capabilityFlags = powerStates[i].capabilityFlags;
1140 powerStatesCopy[i].outputPowerFlags = powerStates[i].outputPowerCharacter;
1141 powerStatesCopy[i].inputPowerFlags = powerStates[i].inputPowerRequirement;
1142 powerStatesCopy[i].staticPower = powerStates[i].staticPower;
1143 powerStatesCopy[i].settleUpTime = powerStates[i].settleUpTime;
1144 powerStatesCopy[i].settleDownTime = powerStates[i].settleDownTime;
1145 if (powerStates[i].version >= kIOPMPowerStateVersion2)
1146 stateOrder = powerStates[i].stateOrder;
1147 else
1148 stateOrder = i;
1149
1150 if (stateOrder < numberOfStates)
1151 {
1152 powerStatesCopy[i].stateOrder = stateOrder;
1153 powerStatesCopy[stateOrder].stateOrderToIndex = i;
1154 }
1155 }
1156
1157 for (IOPMPowerStateIndex i = 0; i < numberOfStates; i++)
1158 {
1159 if (powerStatesCopy[i].stateOrderToIndex == kIOPMPowerStateMax)
1160 {
1161 // power state order missing
1162 error = kIOReturnBadArgument;
1163 break;
1164 }
1165 }
1166 if (kIOReturnSuccess != error)
1167 break;
1168
1169 request = acquirePMRequest( this, kIOPMRequestTypeRegisterPowerDriver );
1170 if (!request)
1171 {
1172 error = kIOReturnNoMemory;
1173 break;
1174 }
1175
1176 powerDriver->retain();
1177 request->fArg0 = (void *) powerDriver;
1178 request->fArg1 = (void *) powerStatesCopy;
1179 request->fArg2 = (void *) numberOfStates;
1180
1181 submitPMRequest( request );
1182 return kIOReturnSuccess;
1183 }
1184 while (false);
1185
1186 if (powerStatesCopy)
1187 IODelete(powerStatesCopy, IOPMPSEntry, numberOfStates);
1188
1189 return error;
1190}
1191
1192//*********************************************************************************
1193// [private] handleRegisterPowerDriver
1194//*********************************************************************************
1195
1196void IOService::handleRegisterPowerDriver( IOPMRequest * request )
1197{
1198 IOService * powerDriver = (IOService *) request->fArg0;
1199 IOPMPSEntry * powerStates = (IOPMPSEntry *) request->fArg1;
1200 unsigned long numberOfStates = (unsigned long) request->fArg2;
1201 unsigned long i, stateIndex;
1202 unsigned long lowestPowerState;
1203 IOService * root;
1204 OSIterator * iter;
1205
1206 PM_ASSERT_IN_GATE();
1207 assert(powerStates);
1208 assert(powerDriver);
1209 assert(numberOfStates > 1);
1210
1211 if ( !fNumberOfPowerStates )
1212 {
1213 OUR_PMLog(kPMLogControllingDriver,
1214 (unsigned long) numberOfStates,
1215 (unsigned long) kIOPMPowerStateVersion1);
1216
1217 fPowerStates = powerStates;
1218 fNumberOfPowerStates = numberOfStates;
1219 fControllingDriver = powerDriver;
1220 fCurrentCapabilityFlags = fPowerStates[0].capabilityFlags;
1221
1222 lowestPowerState = fPowerStates[0].stateOrderToIndex;
1223 fHighestPowerState = fPowerStates[numberOfStates - 1].stateOrderToIndex;
1224
1225 // OR'in all the output power flags
1226 fMergedOutputPowerFlags = 0;
1227 fDeviceUsablePowerState = lowestPowerState;
1228 for ( i = 0; i < numberOfStates; i++ )
1229 {
1230 fMergedOutputPowerFlags |= fPowerStates[i].outputPowerFlags;
1231
1232 stateIndex = fPowerStates[i].stateOrderToIndex;
1233 assert(stateIndex < numberOfStates);
1234 if ((fDeviceUsablePowerState == lowestPowerState) &&
1235 (fPowerStates[stateIndex].capabilityFlags & IOPMDeviceUsable))
1236 {
1237 // The minimum power state that the device is usable
1238 fDeviceUsablePowerState = stateIndex;
1239 }
1240 }
1241
1242 // Register powerDriver as interested, unless already done.
1243 // We don't want to register the default implementation since
1244 // it does nothing. One ramification of not always registering
1245 // is the one fewer retain count held.
1246
1247 root = getPlatform()->getProvider();
1248 assert(root);
1249 if (!root ||
1250 ((OSMemberFunctionCast(void (*)(void),
1251 root, &IOService::powerStateDidChangeTo)) !=
1252 ((OSMemberFunctionCast(void (*)(void),
1253 this, &IOService::powerStateDidChangeTo)))) ||
1254 ((OSMemberFunctionCast(void (*)(void),
1255 root, &IOService::powerStateWillChangeTo)) !=
1256 ((OSMemberFunctionCast(void (*)(void),
1257 this, &IOService::powerStateWillChangeTo)))))
1258 {
1259 if (fInterestedDrivers->findItem(powerDriver) == NULL)
1260 {
1261 PM_LOCK();
1262 fInterestedDrivers->appendNewInformee(powerDriver);
1263 PM_UNLOCK();
1264 }
1265 }
1266
1267 // Examine all existing power clients and perform limit check.
1268
1269 if (fPowerClients &&
1270 (iter = OSCollectionIterator::withCollection(fPowerClients)))
1271 {
1272 const OSSymbol * client;
1273 while ((client = (const OSSymbol *) iter->getNextObject()))
1274 {
1275 IOPMPowerStateIndex powerState = getPowerStateForClient(client);
1276 if (powerState >= numberOfStates)
1277 {
1278 updatePowerClient(client, fHighestPowerState);
1279 }
1280 }
1281 iter->release();
1282 }
1283
1284 if ( inPlane(gIOPowerPlane) && fParentsKnowState )
1285 {
1286 IOPMPowerStateIndex tempDesire;
1287 fMaxPowerState = fControllingDriver->maxCapabilityForDomainState(fParentsCurrentPowerFlags);
1288 // initially change into the state we are already in
1289 tempDesire = fControllingDriver->initialPowerStateForDomainState(fParentsCurrentPowerFlags);
1290 adjustPowerState(tempDesire);
1291 }
1292 }
1293 else
1294 {
1295 OUR_PMLog(kPMLogControllingDriverErr2, numberOfStates, 0);
1296 IODelete(powerStates, IOPMPSEntry, numberOfStates);
1297 }
1298
1299 powerDriver->release();
1300}
1301
1302//*********************************************************************************
1303// [public] registerInterestedDriver
1304//
1305// Add the caller to our list of interested drivers and return our current
1306// power state. If we don't have a power-controlling driver yet, we will
1307// call this interested driver again later when we do get a driver and find
1308// out what the current power state of the device is.
1309//*********************************************************************************
1310
1311IOPMPowerFlags IOService::registerInterestedDriver( IOService * driver )
1312{
1313 IOPMRequest * request;
1314 bool signal;
1315
1316 if (!driver || !initialized || !fInterestedDrivers)
1317 return 0;
1318
1319 PM_LOCK();
1320 signal = (!fInsertInterestSet && !fRemoveInterestSet);
1321 if (fInsertInterestSet == NULL)
1322 fInsertInterestSet = OSSet::withCapacity(4);
1323 if (fInsertInterestSet)
1324 {
1325 fInsertInterestSet->setObject(driver);
1326 if (fRemoveInterestSet)
1327 fRemoveInterestSet->removeObject(driver);
1328 }
1329 PM_UNLOCK();
1330
1331 if (signal)
1332 {
1333 request = acquirePMRequest( this, kIOPMRequestTypeInterestChanged );
1334 if (request)
1335 submitPMRequest( request );
1336 }
1337
1338 // This return value cannot be trusted, but return a value
1339 // for those clients that care.
1340
1341 OUR_PMLog(kPMLogInterestedDriver, kIOPMDeviceUsable, 2);
1342 return kIOPMDeviceUsable;
1343}
1344
1345//*********************************************************************************
1346// [public] deRegisterInterestedDriver
1347//*********************************************************************************
1348
1349IOReturn IOService::deRegisterInterestedDriver( IOService * driver )
1350{
1351 IOPMinformee * item;
1352 IOPMRequest * request;
1353 bool signal;
1354
1355 if (!driver)
1356 return kIOReturnBadArgument;
1357 if (!initialized || !fInterestedDrivers)
1358 return IOPMNotPowerManaged;
1359
1360 PM_LOCK();
1361 if (fInsertInterestSet)
1362 {
1363 fInsertInterestSet->removeObject(driver);
1364 }
1365
1366 item = fInterestedDrivers->findItem(driver);
1367 if (!item)
1368 {
1369 PM_UNLOCK();
1370 return kIOReturnNotFound;
1371 }
1372
1373 signal = (!fRemoveInterestSet && !fInsertInterestSet);
1374 if (fRemoveInterestSet == NULL)
1375 fRemoveInterestSet = OSSet::withCapacity(4);
1376 if (fRemoveInterestSet)
1377 {
1378 fRemoveInterestSet->setObject(driver);
1379 if (item->active)
1380 {
1381 item->active = false;
1382 waitForPMDriverCall( driver );
1383 }
1384 }
1385 PM_UNLOCK();
1386
1387 if (signal)
1388 {
1389 request = acquirePMRequest( this, kIOPMRequestTypeInterestChanged );
1390 if (request)
1391 submitPMRequest( request );
1392 }
1393
1394 return IOPMNoErr;
1395}
1396
1397//*********************************************************************************
1398// [private] handleInterestChanged
1399//
1400// Handle interest added or removed.
1401//*********************************************************************************
1402
1403void IOService::handleInterestChanged( IOPMRequest * request )
1404{
1405 IOService * driver;
1406 IOPMinformee * informee;
1407 IOPMinformeeList * list = fInterestedDrivers;
1408
1409 PM_LOCK();
1410
1411 if (fInsertInterestSet)
1412 {
1413 while ((driver = (IOService *) fInsertInterestSet->getAnyObject()))
1414 {
1415 if (list->findItem(driver) == NULL)
1416 {
1417 informee = list->appendNewInformee(driver);
1418 }
1419 fInsertInterestSet->removeObject(driver);
1420 }
1421 fInsertInterestSet->release();
1422 fInsertInterestSet = 0;
1423 }
1424
1425 if (fRemoveInterestSet)
1426 {
1427 while ((driver = (IOService *) fRemoveInterestSet->getAnyObject()))
1428 {
1429 informee = list->findItem(driver);
1430 if (informee)
1431 {
1432 // Clean-up async interest acknowledgement
1433 if (fHeadNotePendingAcks && informee->timer)
1434 {
1435 informee->timer = 0;
1436 fHeadNotePendingAcks--;
1437 }
1438 list->removeFromList(driver);
1439 }
1440 fRemoveInterestSet->removeObject(driver);
1441 }
1442 fRemoveInterestSet->release();
1443 fRemoveInterestSet = 0;
1444 }
1445
1446 PM_UNLOCK();
1447}
1448
1449//*********************************************************************************
1450// [public] acknowledgePowerChange
1451//
1452// After we notified one of the interested drivers or a power-domain child
1453// of an impending change in power, it has called to say it is now
1454// prepared for the change. If this object is the last to
1455// acknowledge this change, we take whatever action we have been waiting
1456// for.
1457// That may include acknowledging to our parent. In this case, we do it
1458// last of all to insure that this doesn't cause the parent to call us some-
1459// where else and alter data we are relying on here (like the very existance
1460// of a "current change note".)
1461//*********************************************************************************
1462
1463IOReturn IOService::acknowledgePowerChange( IOService * whichObject )
1464{
1465 IOPMRequest * request;
1466
1467 if (!initialized)
1468 return IOPMNotYetInitialized;
1469 if (!whichObject)
1470 return kIOReturnBadArgument;
1471
1472 request = acquirePMRequest( this, kIOPMRequestTypeAckPowerChange );
1473 if (!request)
1474 return kIOReturnNoMemory;
1475
1476 whichObject->retain();
1477 request->fArg0 = whichObject;
1478
1479 submitPMRequest( request );
1480 return IOPMNoErr;
1481}
1482
1483//*********************************************************************************
1484// [private] handleAcknowledgePowerChange
1485//*********************************************************************************
1486
1487bool IOService::handleAcknowledgePowerChange( IOPMRequest * request )
1488{
1489 IOPMinformee * informee;
1490 unsigned long childPower = kIOPMUnknown;
1491 IOService * theChild;
1492 IOService * whichObject;
1493 bool all_acked = false;
1494
1495 PM_ASSERT_IN_GATE();
1496 whichObject = (IOService *) request->fArg0;
1497 assert(whichObject);
1498
1499 // one of our interested drivers?
1500 informee = fInterestedDrivers->findItem( whichObject );
1501 if ( informee == NULL )
1502 {
1503 if ( !isChild(whichObject, gIOPowerPlane) )
1504 {
1505 OUR_PMLog(kPMLogAcknowledgeErr1, 0, 0);
1506 goto no_err;
1507 } else {
1508 OUR_PMLog(kPMLogChildAcknowledge, fHeadNotePendingAcks, 0);
1509 }
1510 } else {
1511 OUR_PMLog(kPMLogDriverAcknowledge, fHeadNotePendingAcks, 0);
1512 }
1513
1514 if ( fHeadNotePendingAcks != 0 )
1515 {
1516 assert(fPowerStates != NULL);
1517
1518 // yes, make sure we're expecting acks
1519 if ( informee != NULL )
1520 {
1521 // it's an interested driver
1522 // make sure we're expecting this ack
1523 if ( informee->timer != 0 )
1524 {
1525#if LOG_SETPOWER_TIMES
1526 if (informee->timer > 0)
1527 {
1528 uint64_t nsec = computeTimeDeltaNS(&informee->startTime);
1529 if (nsec > LOG_SETPOWER_TIMES) {
1530 getPMRootDomain()->pmStatsRecordApplicationResponse(
1531 gIOPMStatsDriverPSChangeSlow, informee->whatObject->getName(),
1532 fDriverCallReason, NS_TO_MS(nsec), 0, NULL, fHeadNotePowerState);
1533 }
1534 }
1535#endif
1536 // mark it acked
1537 informee->timer = 0;
1538 // that's one fewer to worry about
1539 fHeadNotePendingAcks--;
1540 } else {
1541 // this driver has already acked
1542 OUR_PMLog(kPMLogAcknowledgeErr2, 0, 0);
1543 }
1544 } else {
1545 // it's a child
1546 // make sure we're expecting this ack
1547 if ( ((IOPowerConnection *)whichObject)->getAwaitingAck() )
1548 {
1549 // that's one fewer to worry about
1550 fHeadNotePendingAcks--;
1551 ((IOPowerConnection *)whichObject)->setAwaitingAck(false);
1552 theChild = (IOService *)whichObject->copyChildEntry(gIOPowerPlane);
1553 if ( theChild )
1554 {
1555 childPower = theChild->currentPowerConsumption();
1556 theChild->release();
1557 }
1558 if ( childPower == kIOPMUnknown )
1559 {
1560 fHeadNotePowerArrayEntry->staticPower = kIOPMUnknown;
1561 } else {
1562 if (fHeadNotePowerArrayEntry->staticPower != kIOPMUnknown)
1563 {
1564 fHeadNotePowerArrayEntry->staticPower += childPower;
1565 }
1566 }
1567 }
1568 }
1569
1570 if ( fHeadNotePendingAcks == 0 ) {
1571 // yes, stop the timer
1572 stop_ack_timer();
1573 // and now we can continue
1574 all_acked = true;
1575 }
1576 } else {
1577 OUR_PMLog(kPMLogAcknowledgeErr3, 0, 0); // not expecting anybody to ack
1578 }
1579
1580no_err:
1581 if (whichObject)
1582 whichObject->release();
1583
1584 return all_acked;
1585}
1586
1587//*********************************************************************************
1588// [public] acknowledgeSetPowerState
1589//
1590// After we instructed our controlling driver to change power states,
1591// it has called to say it has finished doing so.
1592// We continue to process the power state change.
1593//*********************************************************************************
1594
1595IOReturn IOService::acknowledgeSetPowerState( void )
1596{
1597 IOPMRequest * request;
1598
1599 if (!initialized)
1600 return IOPMNotYetInitialized;
1601
1602 request = acquirePMRequest( this, kIOPMRequestTypeAckSetPowerState );
1603 if (!request)
1604 return kIOReturnNoMemory;
1605
1606 submitPMRequest( request );
1607 return kIOReturnSuccess;
1608}
1609
1610//*********************************************************************************
1611// [private] adjustPowerState
1612//*********************************************************************************
1613
1614void IOService::adjustPowerState( uint32_t clamp )
1615{
1616 PM_ASSERT_IN_GATE();
1617 computeDesiredState(clamp, false);
1618 if (fControllingDriver && fParentsKnowState && inPlane(gIOPowerPlane))
1619 {
1620 IOPMPowerChangeFlags changeFlags = kIOPMSelfInitiated;
1621
1622 // Indicate that children desires must be ignored, and do not ask
1623 // apps for permission to drop power. This is used by root domain
1624 // for demand sleep.
1625
1626 if (getPMRequestType() == kIOPMRequestTypeRequestPowerStateOverride)
1627 changeFlags |= (kIOPMIgnoreChildren | kIOPMSkipAskPowerDown);
1628
1629 startPowerChange(
1630 /* flags */ changeFlags,
1631 /* power state */ fDesiredPowerState,
1632 /* domain flags */ 0,
1633 /* connection */ 0,
1634 /* parent flags */ 0);
1635 }
1636}
1637
1638//*********************************************************************************
1639// [public] synchronizePowerTree
1640//*********************************************************************************
1641
1642IOReturn IOService::synchronizePowerTree(
1643 IOOptionBits options,
1644 IOService * notifyRoot )
1645{
1646 IOPMRequest * request_c = 0;
1647 IOPMRequest * request_s;
1648
1649 if (this != getPMRootDomain())
1650 return kIOReturnBadArgument;
1651 if (!initialized)
1652 return kIOPMNotYetInitialized;
1653
1654 OUR_PMLog(kPMLogCSynchronizePowerTree, options, (notifyRoot != 0));
1655
1656 if (notifyRoot)
1657 {
1658 IOPMRequest * nr;
1659
1660 // Cancels don't need to be synchronized.
1661 nr = acquirePMRequest(notifyRoot, kIOPMRequestTypeChildNotifyDelayCancel);
1662 if (nr) submitPMRequest(nr);
1663 nr = acquirePMRequest(getPMRootDomain(), kIOPMRequestTypeChildNotifyDelayCancel);
1664 if (nr) submitPMRequest(nr);
1665 }
1666
1667 request_s = acquirePMRequest( this, kIOPMRequestTypeSynchronizePowerTree );
1668 if (!request_s)
1669 goto error_no_memory;
1670
1671 if (options & kIOPMSyncCancelPowerDown)
1672 request_c = acquirePMRequest( this, kIOPMRequestTypeIdleCancel );
1673 if (request_c)
1674 {
1675 request_c->attachNextRequest( request_s );
1676 submitPMRequest(request_c);
1677 }
1678
1679 request_s->fArg0 = (void *)(uintptr_t) options;
1680 submitPMRequest(request_s);
1681
1682 return kIOReturnSuccess;
1683
1684error_no_memory:
1685 if (request_c) releasePMRequest(request_c);
1686 if (request_s) releasePMRequest(request_s);
1687 return kIOReturnNoMemory;
1688}
1689
1690//*********************************************************************************
1691// [private] handleSynchronizePowerTree
1692//*********************************************************************************
1693
1694void IOService::handleSynchronizePowerTree( IOPMRequest * request )
1695{
1696 PM_ASSERT_IN_GATE();
1697 if (fControllingDriver && fParentsKnowState && inPlane(gIOPowerPlane) &&
1698 (fCurrentPowerState == fHighestPowerState))
1699 {
1700 IOOptionBits options = (uintptr_t) request->fArg0;
1701
1702 startPowerChange(
1703 /* flags */ kIOPMSelfInitiated | kIOPMSynchronize |
1704 (options & kIOPMSyncNoChildNotify),
1705 /* power state */ fCurrentPowerState,
1706 /* domain flags */ 0,
1707 /* connection */ 0,
1708 /* parent flags */ 0);
1709 }
1710}
1711
1712#ifndef __LP64__
1713//*********************************************************************************
1714// [deprecated] powerDomainWillChangeTo
1715//
1716// Called by the power-hierarchy parent notifying of a new power state
1717// in the power domain.
1718// We enqueue a parent power-change to our queue of power changes.
1719// This may or may not cause us to change power, depending on what
1720// kind of change is occuring in the domain.
1721//*********************************************************************************
1722
1723IOReturn IOService::powerDomainWillChangeTo(
1724 IOPMPowerFlags newPowerFlags,
1725 IOPowerConnection * whichParent )
1726{
1727 assert(false);
1728 return kIOReturnUnsupported;
1729}
1730#endif /* !__LP64__ */
1731
1732//*********************************************************************************
1733// [private] handlePowerDomainWillChangeTo
1734//*********************************************************************************
1735
1736void IOService::handlePowerDomainWillChangeTo( IOPMRequest * request )
1737{
1738 IOPMPowerFlags parentPowerFlags = (IOPMPowerFlags) request->fArg0;
1739 IOPowerConnection * whichParent = (IOPowerConnection *) request->fArg1;
1740 IOPMPowerChangeFlags parentChangeFlags = (IOPMPowerChangeFlags)(uintptr_t) request->fArg2;
1741 IOPMPowerChangeFlags myChangeFlags;
1742 OSIterator * iter;
1743 OSObject * next;
1744 IOPowerConnection * connection;
1745 IOPMPowerStateIndex maxPowerState;
1746 IOPMPowerFlags combinedPowerFlags;
1747 bool savedParentsKnowState;
1748 IOReturn result = IOPMAckImplied;
1749
1750 PM_ASSERT_IN_GATE();
1751 OUR_PMLog(kPMLogWillChange, parentPowerFlags, 0);
1752
1753 if (!inPlane(gIOPowerPlane) || !whichParent || !whichParent->getAwaitingAck())
1754 {
1755 PM_LOG("%s::%s not in power tree\n", getName(), __FUNCTION__);
1756 goto exit_no_ack;
1757 }
1758
1759 savedParentsKnowState = fParentsKnowState;
1760
1761 // Combine parents' output power flags.
1762
1763 combinedPowerFlags = 0;
1764
1765 iter = getParentIterator(gIOPowerPlane);
1766 if ( iter )
1767 {
1768 while ( (next = iter->getNextObject()) )
1769 {
1770 if ( (connection = OSDynamicCast(IOPowerConnection, next)) )
1771 {
1772 if ( connection == whichParent )
1773 combinedPowerFlags |= parentPowerFlags;
1774 else
1775 combinedPowerFlags |= connection->parentCurrentPowerFlags();
1776 }
1777 }
1778 iter->release();
1779 }
1780
1781 // If our initial change has yet to occur, then defer the power change
1782 // until after the power domain has completed its power transition.
1783
1784 if ( fControllingDriver && !fInitialPowerChange )
1785 {
1786 maxPowerState = fControllingDriver->maxCapabilityForDomainState(
1787 combinedPowerFlags);
1788
1789 if (parentChangeFlags & kIOPMDomainPowerDrop)
1790 {
1791 // fMaxPowerState set a limit on self-initiated power changes.
1792 // Update it before a parent power drop.
1793 fMaxPowerState = maxPowerState;
1794 }
1795
1796 // Use kIOPMSynchronize below instead of kIOPMRootBroadcastFlags
1797 // to avoid propagating the root change flags if any service must
1798 // change power state due to root's will-change notification.
1799 // Root does not change power state for kIOPMSynchronize.
1800
1801 myChangeFlags = kIOPMParentInitiated | kIOPMDomainWillChange |
1802 (parentChangeFlags & kIOPMSynchronize);
1803
1804 result = startPowerChange(
1805 /* flags */ myChangeFlags,
1806 /* power state */ maxPowerState,
1807 /* domain flags */ combinedPowerFlags,
1808 /* connection */ whichParent,
1809 /* parent flags */ parentPowerFlags);
1810 }
1811
1812 // If parent is dropping power, immediately update the parent's
1813 // capability flags. Any future merging of parent(s) combined
1814 // power flags should account for this power drop.
1815
1816 if (parentChangeFlags & kIOPMDomainPowerDrop)
1817 {
1818 setParentInfo(parentPowerFlags, whichParent, true);
1819 }
1820
1821 // Parent is expecting an ACK from us. If we did not embark on a state
1822 // transition, i.e. startPowerChange() returned IOPMAckImplied. We are
1823 // still required to issue an ACK to our parent.
1824
1825 if (IOPMAckImplied == result)
1826 {
1827 IOService * parent;
1828 parent = (IOService *) whichParent->copyParentEntry(gIOPowerPlane);
1829 assert(parent);
1830 if ( parent )
1831 {
1832 parent->acknowledgePowerChange( whichParent );
1833 parent->release();
1834 }
1835 }
1836
1837exit_no_ack:
1838 // Drop the retain from notifyChild().
1839 if (whichParent) whichParent->release();
1840}
1841
1842#ifndef __LP64__
1843//*********************************************************************************
1844// [deprecated] powerDomainDidChangeTo
1845//
1846// Called by the power-hierarchy parent after the power state of the power domain
1847// has settled at a new level.
1848// We enqueue a parent power-change to our queue of power changes.
1849// This may or may not cause us to change power, depending on what
1850// kind of change is occuring in the domain.
1851//*********************************************************************************
1852
1853IOReturn IOService::powerDomainDidChangeTo(
1854 IOPMPowerFlags newPowerFlags,
1855 IOPowerConnection * whichParent )
1856{
1857 assert(false);
1858 return kIOReturnUnsupported;
1859}
1860#endif /* !__LP64__ */
1861
1862//*********************************************************************************
1863// [private] handlePowerDomainDidChangeTo
1864//*********************************************************************************
1865
1866void IOService::handlePowerDomainDidChangeTo( IOPMRequest * request )
1867{
1868 IOPMPowerFlags parentPowerFlags = (IOPMPowerFlags) request->fArg0;
1869 IOPowerConnection * whichParent = (IOPowerConnection *) request->fArg1;
1870 IOPMPowerChangeFlags parentChangeFlags = (IOPMPowerChangeFlags)(uintptr_t) request->fArg2;
1871 IOPMPowerChangeFlags myChangeFlags;
1872 IOPMPowerStateIndex maxPowerState;
1873 IOPMPowerStateIndex initialDesire = kPowerStateZero;
1874 bool computeDesire = false;
1875 bool desireChanged = false;
1876 bool savedParentsKnowState;
1877 IOReturn result = IOPMAckImplied;
1878
1879 PM_ASSERT_IN_GATE();
1880 OUR_PMLog(kPMLogDidChange, parentPowerFlags, 0);
1881
1882 if (!inPlane(gIOPowerPlane) || !whichParent || !whichParent->getAwaitingAck())
1883 {
1884 PM_LOG("%s::%s not in power tree\n", getName(), __FUNCTION__);
1885 goto exit_no_ack;
1886 }
1887
1888 savedParentsKnowState = fParentsKnowState;
1889
1890 setParentInfo(parentPowerFlags, whichParent, true);
1891
1892 if ( fControllingDriver )
1893 {
1894 maxPowerState = fControllingDriver->maxCapabilityForDomainState(
1895 fParentsCurrentPowerFlags);
1896
1897 if ((parentChangeFlags & kIOPMDomainPowerDrop) == 0)
1898 {
1899 // fMaxPowerState set a limit on self-initiated power changes.
1900 // Update it after a parent power rise.
1901 fMaxPowerState = maxPowerState;
1902 }
1903
1904 if (fInitialPowerChange)
1905 {
1906 computeDesire = true;
1907 initialDesire = fControllingDriver->initialPowerStateForDomainState(
1908 fParentsCurrentPowerFlags);
1909 }
1910 else if (parentChangeFlags & kIOPMRootChangeUp)
1911 {
1912 if (fAdvisoryTickleUsed)
1913 {
1914 // On system wake, re-compute the desired power state since
1915 // gIOPMAdvisoryTickleEnabled will change for a full wake,
1916 // which is an input to computeDesiredState(). This is not
1917 // necessary for a dark wake because powerChangeDone() will
1918 // handle the dark to full wake case, but it does no harm.
1919
1920 desireChanged = true;
1921 }
1922
1923 if (fResetPowerStateOnWake)
1924 {
1925 // Query the driver for the desired power state on system wake.
1926 // Default implementation returns the lowest power state.
1927
1928 IOPMPowerStateIndex wakePowerState =
1929 fControllingDriver->initialPowerStateForDomainState(
1930 kIOPMRootDomainState | kIOPMPowerOn );
1931
1932 // fDesiredPowerState was adjusted before going to sleep
1933 // with fDeviceDesire at min.
1934
1935 if (StateOrder(wakePowerState) > StateOrder(fDesiredPowerState))
1936 {
1937 // Must schedule a power adjustment if we changed the
1938 // device desire. That will update the desired domain
1939 // power on the parent power connection and ping the
1940 // power parent if necessary.
1941
1942 updatePowerClient(gIOPMPowerClientDevice, wakePowerState);
1943 desireChanged = true;
1944 }
1945 }
1946 }
1947
1948 if (computeDesire || desireChanged)
1949 computeDesiredState(initialDesire, false);
1950
1951 // Absorb and propagate parent's broadcast flags
1952 myChangeFlags = kIOPMParentInitiated | kIOPMDomainDidChange |
1953 (parentChangeFlags & kIOPMRootBroadcastFlags);
1954
1955 result = startPowerChange(
1956 /* flags */ myChangeFlags,
1957 /* power state */ maxPowerState,
1958 /* domain flags */ fParentsCurrentPowerFlags,
1959 /* connection */ whichParent,
1960 /* parent flags */ 0);
1961 }
1962
1963 // Parent is expecting an ACK from us. If we did not embark on a state
1964 // transition, i.e. startPowerChange() returned IOPMAckImplied. We are
1965 // still required to issue an ACK to our parent.
1966
1967 if (IOPMAckImplied == result)
1968 {
1969 IOService * parent;
1970 parent = (IOService *) whichParent->copyParentEntry(gIOPowerPlane);
1971 assert(parent);
1972 if ( parent )
1973 {
1974 parent->acknowledgePowerChange( whichParent );
1975 parent->release();
1976 }
1977 }
1978
1979 // If the parent registers its power driver late, then this is the
1980 // first opportunity to tell our parent about our desire. Or if the
1981 // child's desire changed during a parent change notify.
1982
1983 if (fControllingDriver &&
1984 ((!savedParentsKnowState && fParentsKnowState) || desireChanged))
1985 {
1986 PM_LOG1("%s::powerDomainDidChangeTo parentsKnowState %d\n",
1987 getName(), fParentsKnowState);
1988 requestDomainPower( fDesiredPowerState );
1989 }
1990
1991exit_no_ack:
1992 // Drop the retain from notifyChild().
1993 if (whichParent) whichParent->release();
1994}
1995
1996//*********************************************************************************
1997// [private] setParentInfo
1998//
1999// Set our connection data for one specific parent, and then combine all the parent
2000// data together.
2001//*********************************************************************************
2002
2003void IOService::setParentInfo(
2004 IOPMPowerFlags newPowerFlags,
2005 IOPowerConnection * whichParent,
2006 bool knowsState )
2007{
2008 OSIterator * iter;
2009 OSObject * next;
2010 IOPowerConnection * conn;
2011
2012 PM_ASSERT_IN_GATE();
2013
2014 // set our connection data
2015 whichParent->setParentCurrentPowerFlags(newPowerFlags);
2016 whichParent->setParentKnowsState(knowsState);
2017
2018 // recompute our parent info
2019 fParentsCurrentPowerFlags = 0;
2020 fParentsKnowState = true;
2021
2022 iter = getParentIterator(gIOPowerPlane);
2023 if ( iter )
2024 {
2025 while ( (next = iter->getNextObject()) )
2026 {
2027 if ( (conn = OSDynamicCast(IOPowerConnection, next)) )
2028 {
2029 fParentsKnowState &= conn->parentKnowsState();
2030 fParentsCurrentPowerFlags |= conn->parentCurrentPowerFlags();
2031 }
2032 }
2033 iter->release();
2034 }
2035}
2036
2037//******************************************************************************
2038// [private] trackSystemSleepPreventers
2039//******************************************************************************
2040
2041void IOService::trackSystemSleepPreventers(
2042 IOPMPowerStateIndex oldPowerState,
2043 IOPMPowerStateIndex newPowerState,
2044 IOPMPowerChangeFlags changeFlags __unused )
2045{
2046 IOPMPowerFlags oldCapability, newCapability;
2047
2048 oldCapability = fPowerStates[oldPowerState].capabilityFlags &
2049 (kIOPMPreventIdleSleep | kIOPMPreventSystemSleep);
2050 newCapability = fPowerStates[newPowerState].capabilityFlags &
2051 (kIOPMPreventIdleSleep | kIOPMPreventSystemSleep);
2052
2053 if (fHeadNoteChangeFlags & kIOPMInitialPowerChange)
2054 oldCapability = 0;
2055 if (oldCapability == newCapability)
2056 return;
2057
2058 if ((oldCapability ^ newCapability) & kIOPMPreventIdleSleep)
2059 {
2060 bool enablePrevention = ((oldCapability & kIOPMPreventIdleSleep) == 0);
2061 bool idleCancelAllowed = getPMRootDomain()->updatePreventIdleSleepList(
2062 this, enablePrevention);
2063#if SUPPORT_IDLE_CANCEL
2064 if (idleCancelAllowed && enablePrevention)
2065 {
2066 IOPMRequest * cancelRequest;
2067
2068 cancelRequest = acquirePMRequest( getPMRootDomain(), kIOPMRequestTypeIdleCancel );
2069 if (cancelRequest)
2070 {
2071 submitPMRequest( cancelRequest );
2072 }
2073 }
2074#endif
2075 }
2076
2077 if ((oldCapability ^ newCapability) & kIOPMPreventSystemSleep)
2078 {
2079 getPMRootDomain()->updatePreventSystemSleepList(this,
2080 ((oldCapability & kIOPMPreventSystemSleep) == 0));
2081 }
2082}
2083
2084//*********************************************************************************
2085// [public] requestPowerDomainState
2086//
2087// Called on a power parent when a child's power requirement changes.
2088//*********************************************************************************
2089
2090IOReturn IOService::requestPowerDomainState(
2091 IOPMPowerFlags childRequestPowerFlags,
2092 IOPowerConnection * childConnection,
2093 unsigned long specification )
2094{
2095 IOPMPowerStateIndex order, powerState;
2096 IOPMPowerFlags outputPowerFlags;
2097 IOService * child;
2098 IOPMRequest * subRequest;
2099 bool adjustPower = false;
2100
2101 if (!initialized)
2102 return IOPMNotYetInitialized;
2103
2104 if (gIOPMWorkLoop->onThread() == false)
2105 {
2106 PM_LOG("%s::requestPowerDomainState\n", getName());
2107 return kIOReturnSuccess;
2108 }
2109
2110 OUR_PMLog(kPMLogRequestDomain, childRequestPowerFlags, specification);
2111
2112 if (!isChild(childConnection, gIOPowerPlane))
2113 return kIOReturnNotAttached;
2114
2115 if (!fControllingDriver || !fNumberOfPowerStates)
2116 return kIOReturnNotReady;
2117
2118 child = (IOService *) childConnection->getChildEntry(gIOPowerPlane);
2119 assert(child);
2120
2121 // Remove flags from child request which we can't possibly supply
2122 childRequestPowerFlags &= fMergedOutputPowerFlags;
2123
2124 // Merge in the power flags contributed by this power parent
2125 // at its current or impending power state.
2126
2127 outputPowerFlags = fPowerStates[fCurrentPowerState].outputPowerFlags;
2128 if (fMachineState != kIOPM_Finished)
2129 {
2130 if (IS_POWER_DROP && !IS_ROOT_DOMAIN)
2131 {
2132 // Use the lower power state when dropping power.
2133 // Must be careful since a power drop can be cancelled
2134 // from the following states:
2135 // - kIOPM_OurChangeTellClientsPowerDown
2136 // - kIOPM_OurChangeTellPriorityClientsPowerDown
2137 //
2138 // The child must not wait for this parent to raise power
2139 // if the power drop was cancelled. The solution is to cancel
2140 // the power drop if possible, then schedule an adjustment to
2141 // re-evaluate the parent's power state.
2142 //
2143 // Root domain is excluded to avoid idle sleep issues. And allow
2144 // root domain children to pop up when system is going to sleep.
2145
2146 if ((fMachineState == kIOPM_OurChangeTellClientsPowerDown) ||
2147 (fMachineState == kIOPM_OurChangeTellPriorityClientsPowerDown))
2148 {
2149 fDoNotPowerDown = true; // cancel power drop
2150 adjustPower = true; // schedule an adjustment
2151 PM_LOG1("%s: power drop cancelled in state %u by %s\n",
2152 getName(), fMachineState, child->getName());
2153 }
2154 else
2155 {
2156 // Beyond cancellation point, report the impending state.
2157 outputPowerFlags =
2158 fPowerStates[fHeadNotePowerState].outputPowerFlags;
2159 }
2160 }
2161 else if (IS_POWER_RISE)
2162 {
2163 // When raising power, must report the output power flags from
2164 // child's perspective. A child power request may arrive while
2165 // parent is transitioning upwards. If a request arrives after
2166 // setParentInfo() has already recorded the output power flags
2167 // for the next power state, then using the power supplied by
2168 // fCurrentPowerState is incorrect, and might cause the child
2169 // to wait when it should not.
2170
2171 outputPowerFlags = childConnection->parentCurrentPowerFlags();
2172 }
2173 }
2174 child->fHeadNoteDomainTargetFlags |= outputPowerFlags;
2175
2176 // Map child's requested power flags to one of our power state.
2177
2178 for (order = 0; order < fNumberOfPowerStates; order++)
2179 {
2180 powerState = fPowerStates[order].stateOrderToIndex;
2181 if ((fPowerStates[powerState].outputPowerFlags & childRequestPowerFlags)
2182 == childRequestPowerFlags)
2183 break;
2184 }
2185 if (order >= fNumberOfPowerStates)
2186 {
2187 powerState = kPowerStateZero;
2188 }
2189
2190 // Conditions that warrants a power adjustment on this parent.
2191 // Adjust power will also propagate any changes to the child's
2192 // prevent idle/sleep flags towards the root domain.
2193
2194 if (!childConnection->childHasRequestedPower() ||
2195 (powerState != childConnection->getDesiredDomainState()))
2196 adjustPower = true;
2197
2198#if ENABLE_DEBUG_LOGS
2199 if (adjustPower)
2200 {
2201 PM_LOG("requestPowerDomainState[%s]: %s, init %d, %u->%u\n",
2202 getName(), child->getName(),
2203 !childConnection->childHasRequestedPower(),
2204 (uint32_t) childConnection->getDesiredDomainState(),
2205 (uint32_t) powerState);
2206 }
2207#endif
2208
2209 // Record the child's desires on the connection.
2210 childConnection->setChildHasRequestedPower();
2211 childConnection->setDesiredDomainState( powerState );
2212
2213 // Schedule a request to re-evaluate all children desires and
2214 // adjust power state. Submit a request if one wasn't pending,
2215 // or if the current request is part of a call tree.
2216
2217 if (adjustPower && !fDeviceOverrideEnabled &&
2218 (!fAdjustPowerScheduled || gIOPMRequest->getRootRequest()))
2219 {
2220 subRequest = acquirePMRequest(
2221 this, kIOPMRequestTypeAdjustPowerState, gIOPMRequest );
2222 if (subRequest)
2223 {
2224 submitPMRequest( subRequest );
2225 fAdjustPowerScheduled = true;
2226 }
2227 }
2228
2229 return kIOReturnSuccess;
2230}
2231
2232//*********************************************************************************
2233// [public] temporaryPowerClampOn
2234//
2235// A power domain wants to clamp its power on till it has children which
2236// will thendetermine the power domain state.
2237//
2238// We enter the highest state until addPowerChild is called.
2239//*********************************************************************************
2240
2241IOReturn IOService::temporaryPowerClampOn( void )
2242{
2243 return requestPowerState( gIOPMPowerClientChildProxy, kIOPMPowerStateMax );
2244}
2245
2246//*********************************************************************************
2247// [public] makeUsable
2248//
2249// Some client of our device is asking that we become usable. Although
2250// this has not come from a subclassed device object, treat it exactly
2251// as if it had. In this way, subsequent requests for lower power from
2252// a subclassed device object will pre-empt this request.
2253//
2254// We treat this as a subclass object request to switch to the
2255// highest power state.
2256//*********************************************************************************
2257
2258IOReturn IOService::makeUsable( void )
2259{
2260 OUR_PMLog(kPMLogMakeUsable, 0, 0);
2261 return requestPowerState( gIOPMPowerClientDevice, kIOPMPowerStateMax );
2262}
2263
2264//*********************************************************************************
2265// [public] currentCapability
2266//*********************************************************************************
2267
2268IOPMPowerFlags IOService::currentCapability( void )
2269{
2270 if (!initialized)
2271 return IOPMNotPowerManaged;
2272
2273 return fCurrentCapabilityFlags;
2274}
2275
2276//*********************************************************************************
2277// [public] changePowerStateTo
2278//
2279// Called by our power-controlling driver to change power state. The new desired
2280// power state is computed and compared against the current power state. If those
2281// power states differ, then a power state change is initiated.
2282//*********************************************************************************
2283
2284IOReturn IOService::changePowerStateTo( unsigned long ordinal )
2285{
2286 OUR_PMLog(kPMLogChangeStateTo, ordinal, 0);
2287 return requestPowerState( gIOPMPowerClientDriver, ordinal );
2288}
2289
2290//*********************************************************************************
2291// [protected] changePowerStateToPriv
2292//
2293// Called by our driver subclass to change power state. The new desired power
2294// state is computed and compared against the current power state. If those
2295// power states differ, then a power state change is initiated.
2296//*********************************************************************************
2297
2298IOReturn IOService::changePowerStateToPriv( unsigned long ordinal )
2299{
2300 OUR_PMLog(kPMLogChangeStateToPriv, ordinal, 0);
2301 return requestPowerState( gIOPMPowerClientDevice, ordinal );
2302}
2303
2304//*********************************************************************************
2305// [public] changePowerStateWithOverrideTo
2306//
2307// Called by our driver subclass to change power state. The new desired power
2308// state is computed and compared against the current power state. If those
2309// power states differ, then a power state change is initiated.
2310// Override enforced - Children and Driver desires are ignored.
2311//*********************************************************************************
2312
2313IOReturn IOService::changePowerStateWithOverrideTo( IOPMPowerStateIndex ordinal,
2314 IOPMRequestTag tag )
2315{
2316 IOPMRequest * request;
2317
2318 if (!initialized)
2319 return kIOPMNotYetInitialized;
2320
2321 OUR_PMLog(kPMLogChangeStateToPriv, ordinal, 0);
2322
2323 request = acquirePMRequest( this, kIOPMRequestTypeRequestPowerStateOverride );
2324 if (!request)
2325 return kIOReturnNoMemory;
2326
2327 gIOPMPowerClientDevice->retain();
2328 request->fRequestTag = tag;
2329 request->fArg0 = (void *) ordinal;
2330 request->fArg1 = (void *) gIOPMPowerClientDevice;
2331 request->fArg2 = 0;
2332#if NOT_READY
2333 if (action)
2334 request->installCompletionAction( action, target, param );
2335#endif
2336
2337 // Prevent needless downwards power transitions by clamping power
2338 // until the scheduled request is executed.
2339
2340 if (gIOPMWorkLoop->inGate() && (ordinal < fNumberOfPowerStates))
2341 {
2342 fTempClampPowerState = StateMax(fTempClampPowerState, ordinal);
2343 fTempClampCount++;
2344 fOverrideMaxPowerState = ordinal;
2345 request->fArg2 = (void *) (uintptr_t) true;
2346 }
2347
2348 submitPMRequest( request );
2349 return IOPMNoErr;
2350}
2351
2352//*********************************************************************************
2353// [public] changePowerStateForRootDomain
2354//
2355// Adjust the root domain's power desire on the target
2356//*********************************************************************************
2357
2358IOReturn IOService::changePowerStateForRootDomain( IOPMPowerStateIndex ordinal )
2359{
2360 OUR_PMLog(kPMLogChangeStateForRootDomain, ordinal, 0);
2361 return requestPowerState( gIOPMPowerClientRootDomain, ordinal );
2362}
2363
2364//*********************************************************************************
2365// [public for PMRD] quiescePowerTree
2366//
2367// For root domain to issue a request to quiesce the power tree.
2368// Supplied callback invoked upon completion.
2369//*********************************************************************************
2370
2371IOReturn IOService::quiescePowerTree(
2372 void * target, IOPMCompletionAction action, void * param )
2373{
2374 IOPMRequest * request;
2375
2376 if (!initialized)
2377 return kIOPMNotYetInitialized;
2378 if (!target || !action)
2379 return kIOReturnBadArgument;
2380
2381 OUR_PMLog(kPMLogQuiescePowerTree, 0, 0);
2382
2383 // Target the root node instead of root domain. This is to avoid blocking
2384 // the quiesce request behind an existing root domain request in the work
2385 // queue. Root parent and root domain requests in the work queue must not
2386 // block the completion of the quiesce request.
2387
2388 request = acquirePMRequest(gIOPMRootNode, kIOPMRequestTypeQuiescePowerTree);
2389 if (!request)
2390 return kIOReturnNoMemory;
2391
2392 request->installCompletionAction(target, action, param);
2393
2394 // Submit through the normal request flow. This will make sure any request
2395 // already in the request queue will get pushed over to the work queue for
2396 // execution. Any request submitted after this request may not be serviced.
2397
2398 submitPMRequest( request );
2399 return kIOReturnSuccess;
2400}
2401
2402//*********************************************************************************
2403// [private] requestPowerState
2404//*********************************************************************************
2405
2406IOReturn IOService::requestPowerState(
2407 const OSSymbol * client,
2408 uint32_t state )
2409{
2410 IOPMRequest * request;
2411
2412 if (!client)
2413 return kIOReturnBadArgument;
2414 if (!initialized)
2415 return kIOPMNotYetInitialized;
2416
2417 request = acquirePMRequest( this, kIOPMRequestTypeRequestPowerState );
2418 if (!request)
2419 return kIOReturnNoMemory;
2420
2421 client->retain();
2422 request->fArg0 = (void *)(uintptr_t) state;
2423 request->fArg1 = (void *) client;
2424 request->fArg2 = 0;
2425#if NOT_READY
2426 if (action)
2427 request->installCompletionAction( action, target, param );
2428#endif
2429
2430 // Prevent needless downwards power transitions by clamping power
2431 // until the scheduled request is executed.
2432
2433 if (gIOPMWorkLoop->inGate() && (state < fNumberOfPowerStates))
2434 {
2435 fTempClampPowerState = StateMax(fTempClampPowerState, state);
2436 fTempClampCount++;
2437 request->fArg2 = (void *) (uintptr_t) true;
2438 }
2439
2440 submitPMRequest( request );
2441 return IOPMNoErr;
2442}
2443
2444//*********************************************************************************
2445// [private] handleRequestPowerState
2446//*********************************************************************************
2447
2448void IOService::handleRequestPowerState( IOPMRequest * request )
2449{
2450 const OSSymbol * client = (const OSSymbol *) request->fArg1;
2451 uint32_t state = (uint32_t)(uintptr_t) request->fArg0;
2452
2453 PM_ASSERT_IN_GATE();
2454 if (request->fArg2)
2455 {
2456 assert(fTempClampCount != 0);
2457 if (fTempClampCount) fTempClampCount--;
2458 if (!fTempClampCount) fTempClampPowerState = kPowerStateZero;
2459 }
2460
2461 if (fNumberOfPowerStates && (state >= fNumberOfPowerStates))
2462 state = fHighestPowerState;
2463
2464 // The power suppression due to changePowerStateWithOverrideTo() expires
2465 // upon the next "device" power request - changePowerStateToPriv().
2466
2467 if ((getPMRequestType() != kIOPMRequestTypeRequestPowerStateOverride) &&
2468 (client == gIOPMPowerClientDevice))
2469 fOverrideMaxPowerState = kIOPMPowerStateMax;
2470
2471 if ((state == kPowerStateZero) &&
2472 (client != gIOPMPowerClientDevice) &&
2473 (client != gIOPMPowerClientDriver) &&
2474 (client != gIOPMPowerClientChildProxy))
2475 removePowerClient(client);
2476 else
2477 updatePowerClient(client, state);
2478
2479 adjustPowerState();
2480 client->release();
2481}
2482
2483//*********************************************************************************
2484// [private] Helper functions to update/remove power clients.
2485//*********************************************************************************
2486
2487void IOService::updatePowerClient( const OSSymbol * client, uint32_t powerState )
2488{
2489 IOPMPowerStateIndex oldPowerState = kPowerStateZero;
2490
2491 if (!fPowerClients)
2492 fPowerClients = OSDictionary::withCapacity(4);
2493 if (fPowerClients && client)
2494 {
2495 OSNumber * num = (OSNumber *) fPowerClients->getObject(client);
2496 if (num)
2497 {
2498 oldPowerState = num->unsigned32BitValue();
2499 num->setValue(powerState);
2500 }
2501 else
2502 {
2503 num = OSNumber::withNumber(powerState, 32);
2504 if (num)
2505 {
2506 fPowerClients->setObject(client, num);
2507 num->release();
2508 }
2509 }
2510
2511 PM_ACTION_3(actionUpdatePowerClient, client, oldPowerState, powerState);
2512 }
2513}
2514
2515void IOService::removePowerClient( const OSSymbol * client )
2516{
2517 if (fPowerClients && client)
2518 fPowerClients->removeObject(client);
2519}
2520
2521uint32_t IOService::getPowerStateForClient( const OSSymbol * client )
2522{
2523 uint32_t powerState = kPowerStateZero;
2524
2525 if (fPowerClients && client)
2526 {
2527 OSNumber * num = (OSNumber *) fPowerClients->getObject(client);
2528 if (num) powerState = num->unsigned32BitValue();
2529 }
2530 return powerState;
2531}
2532
2533//*********************************************************************************
2534// [protected] powerOverrideOnPriv
2535//*********************************************************************************
2536
2537IOReturn IOService::powerOverrideOnPriv( void )
2538{
2539 IOPMRequest * request;
2540
2541 if (!initialized)
2542 return IOPMNotYetInitialized;
2543
2544 if (gIOPMWorkLoop->inGate())
2545 {
2546 fDeviceOverrideEnabled = true;
2547 return IOPMNoErr;
2548 }
2549
2550 request = acquirePMRequest( this, kIOPMRequestTypePowerOverrideOnPriv );
2551 if (!request)
2552 return kIOReturnNoMemory;
2553
2554 submitPMRequest( request );
2555 return IOPMNoErr;
2556}
2557
2558//*********************************************************************************
2559// [protected] powerOverrideOffPriv
2560//*********************************************************************************
2561
2562IOReturn IOService::powerOverrideOffPriv( void )
2563{
2564 IOPMRequest * request;
2565
2566 if (!initialized)
2567 return IOPMNotYetInitialized;
2568
2569 if (gIOPMWorkLoop->inGate())
2570 {
2571 fDeviceOverrideEnabled = false;
2572 return IOPMNoErr;
2573 }
2574
2575 request = acquirePMRequest( this, kIOPMRequestTypePowerOverrideOffPriv );
2576 if (!request)
2577 return kIOReturnNoMemory;
2578
2579 submitPMRequest( request );
2580 return IOPMNoErr;
2581}
2582
2583//*********************************************************************************
2584// [private] handlePowerOverrideChanged
2585//*********************************************************************************
2586
2587void IOService::handlePowerOverrideChanged( IOPMRequest * request )
2588{
2589 PM_ASSERT_IN_GATE();
2590 if (request->getType() == kIOPMRequestTypePowerOverrideOnPriv)
2591 {
2592 OUR_PMLog(kPMLogOverrideOn, 0, 0);
2593 fDeviceOverrideEnabled = true;
2594 }
2595 else
2596 {
2597 OUR_PMLog(kPMLogOverrideOff, 0, 0);
2598 fDeviceOverrideEnabled = false;
2599 }
2600
2601 adjustPowerState();
2602}
2603
2604//*********************************************************************************
2605// [private] computeDesiredState
2606//*********************************************************************************
2607
2608void IOService::computeDesiredState( unsigned long localClamp, bool computeOnly )
2609{
2610 OSIterator * iter;
2611 OSObject * next;
2612 IOPowerConnection * connection;
2613 uint32_t desiredState = kPowerStateZero;
2614 uint32_t newPowerState = kPowerStateZero;
2615 bool hasChildren = false;
2616
2617 // Desired power state is always 0 without a controlling driver.
2618
2619 if (!fNumberOfPowerStates)
2620 {
2621 fDesiredPowerState = kPowerStateZero;
2622 return;
2623 }
2624
2625 // Examine the children's desired power state.
2626
2627 iter = getChildIterator(gIOPowerPlane);
2628 if (iter)
2629 {
2630 while ((next = iter->getNextObject()))
2631 {
2632 if ((connection = OSDynamicCast(IOPowerConnection, next)))
2633 {
2634 if (connection->getReadyFlag() == false)
2635 {
2636 PM_LOG3("[%s] %s: connection not ready\n",
2637 getName(), __FUNCTION__);
2638 continue;
2639 }
2640 if (connection->childHasRequestedPower())
2641 hasChildren = true;
2642 desiredState = StateMax(connection->getDesiredDomainState(), desiredState);
2643 }
2644 }
2645 iter->release();
2646 }
2647 if (hasChildren)
2648 updatePowerClient(gIOPMPowerClientChildren, desiredState);
2649 else
2650 removePowerClient(gIOPMPowerClientChildren);
2651
2652 // Iterate through all power clients to determine the min power state.
2653
2654 iter = OSCollectionIterator::withCollection(fPowerClients);
2655 if (iter)
2656 {
2657 const OSSymbol * client;
2658 while ((client = (const OSSymbol *) iter->getNextObject()))
2659 {
2660 // Ignore child and driver when override is in effect.
2661 if ((fDeviceOverrideEnabled ||
2662 (getPMRequestType() == kIOPMRequestTypeRequestPowerStateOverride)) &&
2663 ((client == gIOPMPowerClientChildren) ||
2664 (client == gIOPMPowerClientDriver)))
2665 continue;
2666
2667 // Ignore child proxy when children are present.
2668 if (hasChildren && (client == gIOPMPowerClientChildProxy))
2669 continue;
2670
2671 // Advisory tickles are irrelevant unless system is in full wake
2672 if (client == gIOPMPowerClientAdvisoryTickle &&
2673 !gIOPMAdvisoryTickleEnabled)
2674 continue;
2675
2676 desiredState = getPowerStateForClient(client);
2677 assert(desiredState < fNumberOfPowerStates);
2678 PM_LOG1(" %u %s\n",
2679 desiredState, client->getCStringNoCopy());
2680
2681 newPowerState = StateMax(newPowerState, desiredState);
2682
2683 if (client == gIOPMPowerClientDevice)
2684 fDeviceDesire = desiredState;
2685 }
2686 iter->release();
2687 }
2688
2689 // Factor in the temporary power desires.
2690
2691 newPowerState = StateMax(newPowerState, localClamp);
2692 newPowerState = StateMax(newPowerState, fTempClampPowerState);
2693
2694 // Limit check against max power override.
2695
2696 newPowerState = StateMin(newPowerState, fOverrideMaxPowerState);
2697
2698 // Limit check against number of power states.
2699
2700 if (newPowerState >= fNumberOfPowerStates)
2701 newPowerState = fHighestPowerState;
2702
2703 fDesiredPowerState = newPowerState;
2704
2705 PM_LOG1(" temp %u, clamp %u, current %u, new %u\n",
2706 (uint32_t) localClamp, (uint32_t) fTempClampPowerState,
2707 (uint32_t) fCurrentPowerState, newPowerState);
2708
2709 if (!computeOnly)
2710 {
2711 // Restart idle timer if possible when device desire has increased.
2712 // Or if an advisory desire exists.
2713
2714 if (fIdleTimerPeriod && fIdleTimerStopped)
2715 {
2716 restartIdleTimer();
2717 }
2718
2719 // Invalidate cached tickle power state when desires change, and not
2720 // due to a tickle request. In case the driver has requested a lower
2721 // power state, but the tickle is caching a higher power state which
2722 // will drop future tickles until the cached value is lowered or in-
2723 // validated. The invalidation must occur before the power transition
2724 // to avoid dropping a necessary tickle.
2725
2726 if ((getPMRequestType() != kIOPMRequestTypeActivityTickle) &&
2727 (fActivityTicklePowerState != kInvalidTicklePowerState))
2728 {
2729 IOLockLock(fActivityLock);
2730 fActivityTicklePowerState = kInvalidTicklePowerState;
2731 IOLockUnlock(fActivityLock);
2732 }
2733 }
2734}
2735
2736//*********************************************************************************
2737// [public] currentPowerConsumption
2738//
2739//*********************************************************************************
2740
2741unsigned long IOService::currentPowerConsumption( void )
2742{
2743 if (!initialized)
2744 return kIOPMUnknown;
2745
2746 return fCurrentPowerConsumption;
2747}
2748
2749//*********************************************************************************
2750// [deprecated] getPMworkloop
2751//*********************************************************************************
2752
2753IOWorkLoop * IOService::getPMworkloop( void )
2754{
2755 return gIOPMWorkLoop;
2756}
2757
2758#if NOT_YET
2759
2760//*********************************************************************************
2761// Power Parent/Children Applier
2762//*********************************************************************************
2763
2764static void
2765applyToPowerChildren(
2766 IOService * service,
2767 IOServiceApplierFunction applier,
2768 void * context,
2769 IOOptionBits options )
2770{
2771 PM_ASSERT_IN_GATE();
2772
2773 IORegistryEntry * entry;
2774 IORegistryIterator * iter;
2775 IOPowerConnection * connection;
2776 IOService * child;
2777
2778 iter = IORegistryIterator::iterateOver(service, gIOPowerPlane, options);
2779 if (iter)
2780 {
2781 while ((entry = iter->getNextObject()))
2782 {
2783 // Get child of IOPowerConnection objects
2784 if ((connection = OSDynamicCast(IOPowerConnection, entry)))
2785 {
2786 child = (IOService *) connection->copyChildEntry(gIOPowerPlane);
2787 if (child)
2788 {
2789 (*applier)(child, context);
2790 child->release();
2791 }
2792 }
2793 }
2794 iter->release();
2795 }
2796}
2797
2798static void
2799applyToPowerParent(
2800 IOService * service,
2801 IOServiceApplierFunction applier,
2802 void * context,
2803 IOOptionBits options )
2804{
2805 PM_ASSERT_IN_GATE();
2806
2807 IORegistryEntry * entry;
2808 IORegistryIterator * iter;
2809 IOPowerConnection * connection;
2810 IOService * parent;
2811
2812 iter = IORegistryIterator::iterateOver(service, gIOPowerPlane,
2813 options | kIORegistryIterateParents);
2814 if (iter)
2815 {
2816 while ((entry = iter->getNextObject()))
2817 {
2818 // Get child of IOPowerConnection objects
2819 if ((connection = OSDynamicCast(IOPowerConnection, entry)))
2820 {
2821 parent = (IOService *) connection->copyParentEntry(gIOPowerPlane);
2822 if (parent)
2823 {
2824 (*applier)(parent, context);
2825 parent->release();
2826 }
2827 }
2828 }
2829 iter->release();
2830 }
2831}
2832
2833#endif /* NOT_YET */
2834
2835// MARK: -
2836// MARK: Activity Tickle & Idle Timer
2837
2838void IOService::setAdvisoryTickleEnable( bool enable )
2839{
2840 gIOPMAdvisoryTickleEnabled = enable;
2841}
2842
2843//*********************************************************************************
2844// [public] activityTickle
2845//
2846// The tickle with parameter kIOPMSuperclassPolicy1 causes the activity
2847// flag to be set, and the device state checked. If the device has been
2848// powered down, it is powered up again.
2849// The tickle with parameter kIOPMSubclassPolicy is ignored here and
2850// should be intercepted by a subclass.
2851//*********************************************************************************
2852
2853bool IOService::activityTickle( unsigned long type, unsigned long stateNumber )
2854{
2855 IOPMRequest * request;
2856 bool noPowerChange = true;
2857 uint32_t tickleFlags;
2858
2859 if (!initialized)
2860 return true; // no power change
2861
2862 if ((type == kIOPMSuperclassPolicy1) && StateOrder(stateNumber))
2863 {
2864 IOLockLock(fActivityLock);
2865
2866 // Record device activity for the idle timer handler.
2867
2868 fDeviceWasActive = true;
2869 fActivityTickleCount++;
2870 clock_get_uptime(&fDeviceActiveTimestamp);
2871
2872 PM_ACTION_0(actionActivityTickle);
2873
2874 // Record the last tickle power state.
2875 // This helps to filter out redundant tickles as
2876 // this function may be called from the data path.
2877
2878 if ((fActivityTicklePowerState == kInvalidTicklePowerState)
2879 || StateOrder(fActivityTicklePowerState) < StateOrder(stateNumber))
2880 {
2881 fActivityTicklePowerState = stateNumber;
2882 noPowerChange = false;
2883
2884 tickleFlags = kTickleTypeActivity | kTickleTypePowerRise;
2885 request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
2886 if (request)
2887 {
2888 request->fArg0 = (void *) stateNumber;
2889 request->fArg1 = (void *)(uintptr_t) tickleFlags;
2890 request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
2891 submitPMRequest(request);
2892 }
2893 }
2894
2895 IOLockUnlock(fActivityLock);
2896 }
2897
2898 else if ((type == kIOPMActivityTickleTypeAdvisory) &&
2899 ((stateNumber = fDeviceUsablePowerState) != kPowerStateZero))
2900 {
2901 IOLockLock(fActivityLock);
2902
2903 fAdvisoryTickled = true;
2904
2905 if (fAdvisoryTicklePowerState != stateNumber)
2906 {
2907 fAdvisoryTicklePowerState = stateNumber;
2908 noPowerChange = false;
2909
2910 tickleFlags = kTickleTypeAdvisory | kTickleTypePowerRise;
2911 request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
2912 if (request)
2913 {
2914 request->fArg0 = (void *) stateNumber;
2915 request->fArg1 = (void *)(uintptr_t) tickleFlags;
2916 request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
2917 submitPMRequest(request);
2918 }
2919 }
2920
2921 IOLockUnlock(fActivityLock);
2922 }
2923
2924 // Returns false if the activityTickle might cause a transition to a
2925 // higher powered state, true otherwise.
2926
2927 return noPowerChange;
2928}
2929
2930//*********************************************************************************
2931// [private] handleActivityTickle
2932//*********************************************************************************
2933
2934void IOService::handleActivityTickle( IOPMRequest * request )
2935{
2936 uint32_t ticklePowerState = (uint32_t)(uintptr_t) request->fArg0;
2937 uint32_t tickleFlags = (uint32_t)(uintptr_t) request->fArg1;
2938 uint32_t tickleGeneration = (uint32_t)(uintptr_t) request->fArg2;
2939 bool adjustPower = false;
2940
2941 PM_ASSERT_IN_GATE();
2942 if (fResetPowerStateOnWake && (tickleGeneration != gIOPMTickleGeneration))
2943 {
2944 // Drivers that don't want power restored on wake will drop any
2945 // tickles that pre-dates the current system wake. The model is
2946 // that each wake is a fresh start, with power state depressed
2947 // until a new tickle or an explicit power up request from the
2948 // driver. It is possible for the PM work loop to enter the
2949 // system sleep path with tickle requests queued.
2950
2951 return;
2952 }
2953
2954 if (tickleFlags & kTickleTypeActivity)
2955 {
2956 IOPMPowerStateIndex deviceDesireOrder = StateOrder(fDeviceDesire);
2957 uint32_t idleTimerGeneration = ticklePowerState; // kTickleTypePowerDrop
2958
2959 if (tickleFlags & kTickleTypePowerRise)
2960 {
2961 if ((StateOrder(ticklePowerState) > deviceDesireOrder) &&
2962 (ticklePowerState < fNumberOfPowerStates))
2963 {
2964 fIdleTimerMinPowerState = ticklePowerState;
2965 updatePowerClient(gIOPMPowerClientDevice, ticklePowerState);
2966 adjustPower = true;
2967 }
2968 }
2969 else if ((deviceDesireOrder > StateOrder(fIdleTimerMinPowerState)) &&
2970 (idleTimerGeneration == fIdleTimerGeneration))
2971 {
2972 // Power drop due to idle timer expiration.
2973 // Do not allow idle timer to reduce power below tickle power.
2974 // This prevents the idle timer from decreasing the device desire
2975 // to zero and cancelling the effect of a pre-sleep tickle when
2976 // system wakes up to doze state, while the device is unable to
2977 // raise its power state to satisfy the tickle.
2978
2979 deviceDesireOrder--;
2980 if (deviceDesireOrder < fNumberOfPowerStates)
2981 {
2982 ticklePowerState = fPowerStates[deviceDesireOrder].stateOrderToIndex;
2983 updatePowerClient(gIOPMPowerClientDevice, ticklePowerState);
2984 adjustPower = true;
2985 }
2986 }
2987 }
2988 else // advisory tickle
2989 {
2990 if (tickleFlags & kTickleTypePowerRise)
2991 {
2992 if ((ticklePowerState == fDeviceUsablePowerState) &&
2993 (ticklePowerState < fNumberOfPowerStates))
2994 {
2995 updatePowerClient(gIOPMPowerClientAdvisoryTickle, ticklePowerState);
2996 fHasAdvisoryDesire = true;
2997 fAdvisoryTickleUsed = true;
2998 adjustPower = true;
2999 }
3000 else
3001 {
3002 IOLockLock(fActivityLock);
3003 fAdvisoryTicklePowerState = kInvalidTicklePowerState;
3004 IOLockUnlock(fActivityLock);
3005 }
3006 }
3007 else if (fHasAdvisoryDesire)
3008 {
3009 removePowerClient(gIOPMPowerClientAdvisoryTickle);
3010 fHasAdvisoryDesire = false;
3011 adjustPower = true;
3012 }
3013 }
3014
3015 if (adjustPower)
3016 {
3017 adjustPowerState();
3018 }
3019}
3020
3021//******************************************************************************
3022// [public] setIdleTimerPeriod
3023//
3024// A subclass policy-maker is using our standard idleness detection service.
3025// Start the idle timer. Period is in seconds.
3026//******************************************************************************
3027
3028IOReturn IOService::setIdleTimerPeriod( unsigned long period )
3029{
3030 if (!initialized)
3031 return IOPMNotYetInitialized;
3032
3033 OUR_PMLog(kPMLogSetIdleTimerPeriod, period, fIdleTimerPeriod);
3034
3035 IOPMRequest * request =
3036 acquirePMRequest( this, kIOPMRequestTypeSetIdleTimerPeriod );
3037 if (!request)
3038 return kIOReturnNoMemory;
3039
3040 request->fArg0 = (void *) period;
3041 submitPMRequest( request );
3042
3043 return kIOReturnSuccess;
3044}
3045
3046IOReturn IOService::setIgnoreIdleTimer( bool ignore )
3047{
3048 if (!initialized)
3049 return IOPMNotYetInitialized;
3050
3051 OUR_PMLog(kIOPMRequestTypeIgnoreIdleTimer, ignore, 0);
3052
3053 IOPMRequest * request =
3054 acquirePMRequest( this, kIOPMRequestTypeIgnoreIdleTimer );
3055 if (!request)
3056 return kIOReturnNoMemory;
3057
3058 request->fArg0 = (void *) ignore;
3059 submitPMRequest( request );
3060
3061 return kIOReturnSuccess;
3062}
3063
3064//******************************************************************************
3065// [public] nextIdleTimeout
3066//
3067// Returns how many "seconds from now" the device should idle into its
3068// next lowest power state.
3069//******************************************************************************
3070
3071SInt32 IOService::nextIdleTimeout(
3072 AbsoluteTime currentTime,
3073 AbsoluteTime lastActivity,
3074 unsigned int powerState)
3075{
3076 AbsoluteTime delta;
3077 UInt64 delta_ns;
3078 SInt32 delta_secs;
3079 SInt32 delay_secs;
3080
3081 // Calculate time difference using funky macro from clock.h.
3082 delta = currentTime;
3083 SUB_ABSOLUTETIME(&delta, &lastActivity);
3084
3085 // Figure it in seconds.
3086 absolutetime_to_nanoseconds(delta, &delta_ns);
3087 delta_secs = (SInt32)(delta_ns / NSEC_PER_SEC);
3088
3089 // Be paranoid about delta somehow exceeding timer period.
3090 if (delta_secs < (int) fIdleTimerPeriod)
3091 delay_secs = (int) fIdleTimerPeriod - delta_secs;
3092 else
3093 delay_secs = (int) fIdleTimerPeriod;
3094
3095 return (SInt32)delay_secs;
3096}
3097
3098//*********************************************************************************
3099// [public] start_PM_idle_timer
3100//*********************************************************************************
3101
3102void IOService::start_PM_idle_timer( void )
3103{
3104 static const int maxTimeout = 100000;
3105 static const int minTimeout = 1;
3106 AbsoluteTime uptime, deadline;
3107 SInt32 idle_in = 0;
3108 boolean_t pending;
3109
3110 if (!initialized || !fIdleTimerPeriod)
3111 return;
3112
3113 IOLockLock(fActivityLock);
3114
3115 clock_get_uptime(&uptime);
3116
3117 // Subclasses may modify idle sleep algorithm
3118 idle_in = nextIdleTimeout(uptime, fDeviceActiveTimestamp, fCurrentPowerState);
3119
3120 // Check for out-of range responses
3121 if (idle_in > maxTimeout)
3122 {
3123 // use standard implementation
3124 idle_in = IOService::nextIdleTimeout(uptime,
3125 fDeviceActiveTimestamp,
3126 fCurrentPowerState);
3127 } else if (idle_in < minTimeout) {
3128 idle_in = fIdleTimerPeriod;
3129 }
3130
3131 IOLockUnlock(fActivityLock);
3132
3133 fNextIdleTimerPeriod = idle_in;
3134 fIdleTimerStartTime = uptime;
3135
3136 retain();
3137 clock_interval_to_absolutetime_interval(idle_in, kSecondScale, &deadline);
3138 ADD_ABSOLUTETIME(&deadline, &uptime);
3139 pending = thread_call_enter_delayed(fIdleTimer, deadline);
3140 if (pending) release();
3141}
3142
3143//*********************************************************************************
3144// [private] restartIdleTimer
3145//*********************************************************************************
3146
3147void IOService::restartIdleTimer( void )
3148{
3149 if (fDeviceDesire != kPowerStateZero)
3150 {
3151 fIdleTimerStopped = false;
3152 fActivityTickleCount = 0;
3153 start_PM_idle_timer();
3154 }
3155 else if (fHasAdvisoryDesire)
3156 {
3157 fIdleTimerStopped = false;
3158 start_PM_idle_timer();
3159 }
3160 else
3161 {
3162 fIdleTimerStopped = true;
3163 }
3164}
3165
3166//*********************************************************************************
3167// idle_timer_expired
3168//*********************************************************************************
3169
3170static void
3171idle_timer_expired(
3172 thread_call_param_t arg0, thread_call_param_t arg1 )
3173{
3174 IOService * me = (IOService *) arg0;
3175
3176 if (gIOPMWorkLoop)
3177 gIOPMWorkLoop->runAction(
3178 OSMemberFunctionCast(IOWorkLoop::Action, me,
3179 &IOService::idleTimerExpired),
3180 me);
3181
3182 me->release();
3183}
3184
3185//*********************************************************************************
3186// [private] idleTimerExpired
3187//
3188// The idle timer has expired. If there has been activity since the last
3189// expiration, just restart the timer and return. If there has not been
3190// activity, switch to the next lower power state and restart the timer.
3191//*********************************************************************************
3192
3193void IOService::idleTimerExpired( void )
3194{
3195 IOPMRequest * request;
3196 bool restartTimer = true;
3197 uint32_t tickleFlags;
3198
3199 if ( !initialized || !fIdleTimerPeriod || fIdleTimerStopped ||
3200 fLockedFlags.PMStop )
3201 return;
3202
3203 fIdleTimerStartTime = 0;
3204
3205 IOLockLock(fActivityLock);
3206
3207 // Check for device activity (tickles) over last timer period.
3208
3209 if (fDeviceWasActive)
3210 {
3211 // Device was active - do not drop power, restart timer.
3212 fDeviceWasActive = false;
3213 }
3214 else if (!fIdleTimerIgnored)
3215 {
3216 // No device activity - drop power state by one level.
3217 // Decrement the cached tickle power state when possible.
3218 // This value may be kInvalidTicklePowerState before activityTickle()
3219 // is called, but the power drop request must be issued regardless.
3220
3221 if ((fActivityTicklePowerState != kInvalidTicklePowerState) &&
3222 (fActivityTicklePowerState != kPowerStateZero))
3223 fActivityTicklePowerState--;
3224
3225 tickleFlags = kTickleTypeActivity | kTickleTypePowerDrop;
3226 request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
3227 if (request)
3228 {
3229 request->fArg0 = (void *)(uintptr_t) fIdleTimerGeneration;
3230 request->fArg1 = (void *)(uintptr_t) tickleFlags;
3231 request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
3232 submitPMRequest( request );
3233
3234 // Do not restart timer until after the tickle request has been
3235 // processed.
3236
3237 restartTimer = false;
3238 }
3239 }
3240
3241 if (fAdvisoryTickled)
3242 {
3243 fAdvisoryTickled = false;
3244 }
3245 else if (fHasAdvisoryDesire)
3246 {
3247 // Want new tickles to turn into pm request after we drop the lock
3248 fAdvisoryTicklePowerState = kInvalidTicklePowerState;
3249
3250 tickleFlags = kTickleTypeAdvisory | kTickleTypePowerDrop;
3251 request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
3252 if (request)
3253 {
3254 request->fArg0 = (void *)(uintptr_t) fIdleTimerGeneration;
3255 request->fArg1 = (void *)(uintptr_t) tickleFlags;
3256 request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
3257 submitPMRequest( request );
3258
3259 // Do not restart timer until after the tickle request has been
3260 // processed.
3261
3262 restartTimer = false;
3263 }
3264 }
3265
3266 IOLockUnlock(fActivityLock);
3267
3268 if (restartTimer)
3269 start_PM_idle_timer();
3270}
3271
3272#ifndef __LP64__
3273//*********************************************************************************
3274// [deprecated] PM_idle_timer_expiration
3275//*********************************************************************************
3276
3277void IOService::PM_idle_timer_expiration( void )
3278{
3279}
3280
3281//*********************************************************************************
3282// [deprecated] command_received
3283//*********************************************************************************
3284
3285void IOService::command_received( void *statePtr , void *, void * , void * )
3286{
3287}
3288#endif /* !__LP64__ */
3289
3290//*********************************************************************************
3291// [public] setAggressiveness
3292//
3293// Pass on the input parameters to all power domain children. All those which are
3294// power domains will pass it on to their children, etc.
3295//*********************************************************************************
3296
3297IOReturn IOService::setAggressiveness( unsigned long type, unsigned long newLevel )
3298{
3299 return kIOReturnSuccess;
3300}
3301
3302//*********************************************************************************
3303// [public] getAggressiveness
3304//
3305// Called by the user client.
3306//*********************************************************************************
3307
3308IOReturn IOService::getAggressiveness( unsigned long type, unsigned long * currentLevel )
3309{
3310 IOPMrootDomain * rootDomain = getPMRootDomain();
3311
3312 if (!rootDomain)
3313 return kIOReturnNotReady;
3314
3315 return rootDomain->getAggressiveness( type, currentLevel );
3316}
3317
3318//*********************************************************************************
3319// [public] getPowerState
3320//
3321//*********************************************************************************
3322
3323UInt32 IOService::getPowerState( void )
3324{
3325 if (!initialized)
3326 return kPowerStateZero;
3327
3328 return fCurrentPowerState;
3329}
3330
3331#ifndef __LP64__
3332//*********************************************************************************
3333// [deprecated] systemWake
3334//
3335// Pass this to all power domain children. All those which are
3336// power domains will pass it on to their children, etc.
3337//*********************************************************************************
3338
3339IOReturn IOService::systemWake( void )
3340{
3341 OSIterator * iter;
3342 OSObject * next;
3343 IOPowerConnection * connection;
3344 IOService * theChild;
3345
3346 iter = getChildIterator(gIOPowerPlane);
3347 if ( iter )
3348 {
3349 while ( (next = iter->getNextObject()) )
3350 {
3351 if ( (connection = OSDynamicCast(IOPowerConnection, next)) )
3352 {
3353 if (connection->getReadyFlag() == false)
3354 {
3355 PM_LOG3("[%s] %s: connection not ready\n",
3356 getName(), __FUNCTION__);
3357 continue;
3358 }
3359
3360 theChild = (IOService *)connection->copyChildEntry(gIOPowerPlane);
3361 if ( theChild )
3362 {
3363 theChild->systemWake();
3364 theChild->release();
3365 }
3366 }
3367 }
3368 iter->release();
3369 }
3370
3371 if ( fControllingDriver != NULL )
3372 {
3373 if ( fControllingDriver->didYouWakeSystem() )
3374 {
3375 makeUsable();
3376 }
3377 }
3378
3379 return IOPMNoErr;
3380}
3381
3382//*********************************************************************************
3383// [deprecated] temperatureCriticalForZone
3384//*********************************************************************************
3385
3386IOReturn IOService::temperatureCriticalForZone( IOService * whichZone )
3387{
3388 IOService * theParent;
3389 IOService * theNub;
3390
3391 OUR_PMLog(kPMLogCriticalTemp, 0, 0);
3392
3393 if ( inPlane(gIOPowerPlane) && !IS_PM_ROOT )
3394 {
3395 theNub = (IOService *)copyParentEntry(gIOPowerPlane);
3396 if ( theNub )
3397 {
3398 theParent = (IOService *)theNub->copyParentEntry(gIOPowerPlane);
3399 theNub->release();
3400 if ( theParent )
3401 {
3402 theParent->temperatureCriticalForZone(whichZone);
3403 theParent->release();
3404 }
3405 }
3406 }
3407 return IOPMNoErr;
3408}
3409#endif /* !__LP64__ */
3410
3411// MARK: -
3412// MARK: Power Change (Common)
3413
3414//*********************************************************************************
3415// [private] startPowerChange
3416//
3417// All power state changes starts here.
3418//*********************************************************************************
3419
3420IOReturn IOService::startPowerChange(
3421 IOPMPowerChangeFlags changeFlags,
3422 IOPMPowerStateIndex powerState,
3423 IOPMPowerFlags domainFlags,
3424 IOPowerConnection * parentConnection,
3425 IOPMPowerFlags parentFlags )
3426{
3427 PM_ASSERT_IN_GATE();
3428 assert( fMachineState == kIOPM_Finished );
3429 assert( powerState < fNumberOfPowerStates );
3430
3431 if (powerState >= fNumberOfPowerStates)
3432 return IOPMAckImplied;
3433
3434 fIsPreChange = true;
3435 PM_ACTION_2(actionPowerChangeOverride, &powerState, &changeFlags);
3436
3437 if (changeFlags & kIOPMExpireIdleTimer)
3438 {
3439 // Root domain requested removal of tickle influence
3440 if (StateOrder(fDeviceDesire) > StateOrder(powerState))
3441 {
3442 // Reset device desire down to the clamped power state
3443 updatePowerClient(gIOPMPowerClientDevice, powerState);
3444 computeDesiredState(kPowerStateZero, true);
3445
3446 // Invalidate tickle cache so the next tickle will issue a request
3447 IOLockLock(fActivityLock);
3448 fDeviceWasActive = false;
3449 fActivityTicklePowerState = kInvalidTicklePowerState;
3450 IOLockUnlock(fActivityLock);
3451
3452 fIdleTimerMinPowerState = kPowerStateZero;
3453 }
3454 }
3455
3456 // Root domain's override handler may cancel the power change by
3457 // setting the kIOPMNotDone flag.
3458
3459 if (changeFlags & kIOPMNotDone)
3460 return IOPMAckImplied;
3461
3462 // Forks to either Driver or Parent initiated power change paths.
3463
3464 fHeadNoteChangeFlags = changeFlags;
3465 fHeadNotePowerState = powerState;
3466 fHeadNotePowerArrayEntry = &fPowerStates[ powerState ];
3467 fHeadNoteParentConnection = NULL;
3468
3469 if (changeFlags & kIOPMSelfInitiated)
3470 {
3471 if (changeFlags & kIOPMSynchronize)
3472 OurSyncStart();
3473 else
3474 OurChangeStart();
3475 return 0;
3476 }
3477 else
3478 {
3479 assert(changeFlags & kIOPMParentInitiated);
3480 fHeadNoteDomainFlags = domainFlags;
3481 fHeadNoteParentFlags = parentFlags;
3482 fHeadNoteParentConnection = parentConnection;
3483 return ParentChangeStart();
3484 }
3485}
3486
3487//*********************************************************************************
3488// [private] notifyInterestedDrivers
3489//*********************************************************************************
3490
3491bool IOService::notifyInterestedDrivers( void )
3492{
3493 IOPMinformee * informee;
3494 IOPMinformeeList * list = fInterestedDrivers;
3495 DriverCallParam * param;
3496 IOItemCount count;
3497
3498 PM_ASSERT_IN_GATE();
3499 assert( fDriverCallParamCount == 0 );
3500 assert( fHeadNotePendingAcks == 0 );
3501
3502 fHeadNotePendingAcks = 0;
3503
3504 count = list->numberOfItems();
3505 if (!count)
3506 goto done; // no interested drivers
3507
3508 // Allocate an array of interested drivers and their return values
3509 // for the callout thread. Everything else is still "owned" by the
3510 // PM work loop, which can run to process acknowledgePowerChange()
3511 // responses.
3512
3513 param = (DriverCallParam *) fDriverCallParamPtr;
3514 if (count > fDriverCallParamSlots)
3515 {
3516 if (fDriverCallParamSlots)
3517 {
3518 assert(fDriverCallParamPtr);
3519 IODelete(fDriverCallParamPtr, DriverCallParam, fDriverCallParamSlots);
3520 fDriverCallParamPtr = 0;
3521 fDriverCallParamSlots = 0;
3522 }
3523
3524 param = IONew(DriverCallParam, count);
3525 if (!param)
3526 goto done; // no memory
3527
3528 fDriverCallParamPtr = (void *) param;
3529 fDriverCallParamSlots = count;
3530 }
3531
3532 informee = list->firstInList();
3533 assert(informee);
3534 for (IOItemCount i = 0; i < count; i++)
3535 {
3536 informee->timer = -1;
3537 param[i].Target = informee;
3538 informee->retain();
3539 informee = list->nextInList( informee );
3540 }
3541
3542 fDriverCallParamCount = count;
3543 fHeadNotePendingAcks = count;
3544
3545 // Block state machine and wait for callout completion.
3546 assert(!fDriverCallBusy);
3547 fDriverCallBusy = true;
3548 thread_call_enter( fDriverCallEntry );
3549 return true;
3550
3551done:
3552 // Return false if there are no interested drivers or could not schedule
3553 // callout thread due to error.
3554 return false;
3555}
3556
3557//*********************************************************************************
3558// [private] notifyInterestedDriversDone
3559//*********************************************************************************
3560
3561void IOService::notifyInterestedDriversDone( void )
3562{
3563 IOPMinformee * informee;
3564 IOItemCount count;
3565 DriverCallParam * param;
3566 IOReturn result;
3567
3568 PM_ASSERT_IN_GATE();
3569 assert( fDriverCallBusy == false );
3570 assert( fMachineState == kIOPM_DriverThreadCallDone );
3571
3572 param = (DriverCallParam *) fDriverCallParamPtr;
3573 count = fDriverCallParamCount;
3574
3575 if (param && count)
3576 {
3577 for (IOItemCount i = 0; i < count; i++, param++)
3578 {
3579 informee = (IOPMinformee *) param->Target;
3580 result = param->Result;
3581
3582 if ((result == IOPMAckImplied) || (result < 0))
3583 {
3584 // Interested driver return IOPMAckImplied.
3585 // If informee timer is zero, it must have de-registered
3586 // interest during the thread callout. That also drops
3587 // the pending ack count.
3588
3589 if (fHeadNotePendingAcks && informee->timer)
3590 fHeadNotePendingAcks--;
3591
3592 informee->timer = 0;
3593 }
3594 else if (informee->timer)
3595 {
3596 assert(informee->timer == -1);
3597
3598 // Driver has not acked, and has returned a positive result.
3599 // Enforce a minimum permissible timeout value.
3600 // Make the min value large enough so timeout is less likely
3601 // to occur if a driver misinterpreted that the return value
3602 // should be in microsecond units. And make it large enough
3603 // to be noticeable if a driver neglects to ack.
3604
3605 if (result < kMinAckTimeoutTicks)
3606 result = kMinAckTimeoutTicks;
3607
3608 informee->timer = (result / (ACK_TIMER_PERIOD / ns_per_us)) + 1;
3609 }
3610 // else, child has already acked or driver has removed interest,
3611 // and head_note_pendingAcks decremented.
3612 // informee may have been removed from the interested drivers list,
3613 // thus the informee must be retained across the callout.
3614
3615 informee->release();
3616 }
3617
3618 fDriverCallParamCount = 0;
3619
3620 if ( fHeadNotePendingAcks )
3621 {
3622 OUR_PMLog(kPMLogStartAckTimer, 0, 0);
3623 start_ack_timer();
3624 }
3625 }
3626
3627 MS_POP(); // pop the machine state passed to notifyAll()
3628
3629 // If interest acks are outstanding, block the state machine until
3630 // fHeadNotePendingAcks drops to zero before notifying root domain.
3631 // Otherwise notify root domain directly.
3632
3633 if (!fHeadNotePendingAcks)
3634 {
3635 notifyRootDomain();
3636 }
3637 else
3638 {
3639 MS_PUSH(fMachineState);
3640 fMachineState = kIOPM_NotifyChildrenStart;
3641 }
3642}
3643
3644//*********************************************************************************
3645// [private] notifyRootDomain
3646//*********************************************************************************
3647
3648void IOService::notifyRootDomain( void )
3649{
3650 assert( fDriverCallBusy == false );
3651
3652 // Only for root domain in the will-change phase
3653 if (!IS_ROOT_DOMAIN || (fMachineState != kIOPM_OurChangeSetPowerState))
3654 {
3655 notifyChildren();
3656 return;
3657 }
3658
3659 MS_PUSH(fMachineState); // push notifyAll() machine state
3660 fMachineState = kIOPM_DriverThreadCallDone;
3661
3662 // Call IOPMrootDomain::willNotifyPowerChildren() on a thread call
3663 // to avoid a deadlock.
3664 fDriverCallReason = kRootDomainInformPreChange;
3665 fDriverCallBusy = true;
3666 thread_call_enter( fDriverCallEntry );
3667}
3668
3669void IOService::notifyRootDomainDone( void )
3670{
3671 assert( fDriverCallBusy == false );
3672 assert( fMachineState == kIOPM_DriverThreadCallDone );
3673
3674 MS_POP(); // pop notifyAll() machine state
3675 notifyChildren();
3676}
3677
3678//*********************************************************************************
3679// [private] notifyChildren
3680//*********************************************************************************
3681
3682void IOService::notifyChildren( void )
3683{
3684 OSIterator * iter;
3685 OSObject * next;
3686 IOPowerConnection * connection;
3687 OSArray * children = 0;
3688 IOPMrootDomain * rootDomain;
3689 bool delayNotify = false;
3690
3691 if ((fHeadNotePowerState != fCurrentPowerState) &&
3692 (IS_POWER_DROP == fIsPreChange) &&
3693 ((rootDomain = getPMRootDomain()) == this))
3694 {
3695 rootDomain->tracePoint( IS_POWER_DROP ?
3696 kIOPMTracePointSleepPowerPlaneDrivers :
3697 kIOPMTracePointWakePowerPlaneDrivers );
3698 }
3699
3700 if (fStrictTreeOrder)
3701 children = OSArray::withCapacity(8);
3702
3703 // Sum child power consumption in notifyChild()
3704 fHeadNotePowerArrayEntry->staticPower = 0;
3705
3706 iter = getChildIterator(gIOPowerPlane);
3707 if ( iter )
3708 {
3709 while ((next = iter->getNextObject()))
3710 {
3711 if ((connection = OSDynamicCast(IOPowerConnection, next)))
3712 {
3713 if (connection->getReadyFlag() == false)
3714 {
3715 PM_LOG3("[%s] %s: connection not ready\n",
3716 getName(), __FUNCTION__);
3717 continue;
3718 }
3719
3720 // Mechanism to postpone the did-change notification to
3721 // certain power children to order those children last.
3722 // Cannot be used together with strict tree ordering.
3723
3724 if (!fIsPreChange &&
3725 connection->delayChildNotification &&
3726 getPMRootDomain()->shouldDelayChildNotification(this))
3727 {
3728 if (!children)
3729 {
3730 children = OSArray::withCapacity(8);
3731 if (children)
3732 delayNotify = true;
3733 }
3734 if (delayNotify)
3735 {
3736 children->setObject( connection );
3737 continue;
3738 }
3739 }
3740
3741 if (!delayNotify && children)
3742 children->setObject( connection );
3743 else
3744 notifyChild( connection );
3745 }
3746 }
3747 iter->release();
3748 }
3749
3750 if (children && (children->getCount() == 0))
3751 {
3752 children->release();
3753 children = 0;
3754 }
3755 if (children)
3756 {
3757 assert(fNotifyChildArray == 0);
3758 fNotifyChildArray = children;
3759 MS_PUSH(fMachineState);
3760
3761 if (delayNotify)
3762 {
3763 // Block until all non-delayed children have acked their
3764 // notification. Then notify the remaining delayed child
3765 // in the array. This is used to hold off graphics child
3766 // notification while the rest of the system powers up.
3767 // If a hid tickle arrives during this time, the delayed
3768 // children are immediately notified and root domain will
3769 // not clamp power for dark wake.
3770
3771 fMachineState = kIOPM_NotifyChildrenDelayed;
3772 PM_LOG2("%s: %d children in delayed array\n",
3773 getName(), children->getCount());
3774 }
3775 else
3776 {
3777 // Child array created to support strict notification order.
3778 // Notify children in the array one at a time.
3779
3780 fMachineState = kIOPM_NotifyChildrenOrdered;
3781 }
3782 }
3783}
3784
3785//*********************************************************************************
3786// [private] notifyChildrenOrdered
3787//*********************************************************************************
3788
3789void IOService::notifyChildrenOrdered( void )
3790{
3791 PM_ASSERT_IN_GATE();
3792 assert(fNotifyChildArray);
3793 assert(fMachineState == kIOPM_NotifyChildrenOrdered);
3794
3795 // Notify one child, wait for it to ack, then repeat for next child.
3796 // This is a workaround for some drivers with multiple instances at
3797 // the same branch in the power tree, but the driver is slow to power
3798 // up unless the tree ordering is observed. Problem observed only on
3799 // system wake, not on system sleep.
3800 //
3801 // We have the ability to power off in reverse child index order.
3802 // That works nicely on some machines, but not on all HW configs.
3803
3804 if (fNotifyChildArray->getCount())
3805 {
3806 IOPowerConnection * connection;
3807 connection = (IOPowerConnection *) fNotifyChildArray->getObject(0);
3808 notifyChild( connection );
3809 fNotifyChildArray->removeObject(0);
3810 }
3811 else
3812 {
3813 fNotifyChildArray->release();
3814 fNotifyChildArray = 0;
3815
3816 MS_POP(); // pushed by notifyChildren()
3817 }
3818}
3819
3820//*********************************************************************************
3821// [private] notifyChildrenDelayed
3822//*********************************************************************************
3823
3824void IOService::notifyChildrenDelayed( void )
3825{
3826 IOPowerConnection * connection;
3827
3828 PM_ASSERT_IN_GATE();
3829 assert(fNotifyChildArray);
3830 assert(fMachineState == kIOPM_NotifyChildrenDelayed);
3831
3832 // Wait after all non-delayed children and interested drivers have ack'ed,
3833 // then notify all delayed children. If notify delay is canceled, child
3834 // acks may be outstanding with PM blocked on fHeadNotePendingAcks != 0.
3835 // But the handling for either case is identical.
3836
3837 for (int i = 0; ; i++)
3838 {
3839 connection = (IOPowerConnection *) fNotifyChildArray->getObject(i);
3840 if (!connection)
3841 break;
3842
3843 notifyChild( connection );
3844 }
3845
3846 PM_LOG2("%s: notified delayed children\n", getName());
3847 fNotifyChildArray->release();
3848 fNotifyChildArray = 0;
3849
3850 MS_POP(); // pushed by notifyChildren()
3851}
3852
3853//*********************************************************************************
3854// [private] notifyAll
3855//*********************************************************************************
3856
3857IOReturn IOService::notifyAll( uint32_t nextMS )
3858{
3859 // Save the machine state to be restored by notifyInterestedDriversDone()
3860
3861 PM_ASSERT_IN_GATE();
3862 MS_PUSH(nextMS);
3863 fMachineState = kIOPM_DriverThreadCallDone;
3864 fDriverCallReason = fIsPreChange ?
3865 kDriverCallInformPreChange : kDriverCallInformPostChange;
3866
3867 if (!notifyInterestedDrivers())
3868 notifyInterestedDriversDone();
3869
3870 return IOPMWillAckLater;
3871}
3872
3873//*********************************************************************************
3874// [private, static] pmDriverCallout
3875//
3876// Thread call context
3877//*********************************************************************************
3878
3879IOReturn IOService::actionDriverCalloutDone(
3880 OSObject * target,
3881 void * arg0, void * arg1,
3882 void * arg2, void * arg3 )
3883{
3884 IOServicePM * pwrMgt = (IOServicePM *) arg0;
3885
3886 assert( fDriverCallBusy );
3887 fDriverCallBusy = false;
3888
3889 assert(gIOPMWorkQueue);
3890 gIOPMWorkQueue->signalWorkAvailable();
3891
3892 return kIOReturnSuccess;
3893}
3894
3895void IOService::pmDriverCallout( IOService * from )
3896{
3897 assert(from);
3898 switch (from->fDriverCallReason)
3899 {
3900 case kDriverCallSetPowerState:
3901 from->driverSetPowerState();
3902 break;
3903
3904 case kDriverCallInformPreChange:
3905 case kDriverCallInformPostChange:
3906 from->driverInformPowerChange();
3907 break;
3908
3909 case kRootDomainInformPreChange:
3910 getPMRootDomain()->willNotifyPowerChildren(from->fHeadNotePowerState);
3911 break;
3912
3913 default:
3914 panic("IOService::pmDriverCallout bad machine state %x",
3915 from->fDriverCallReason);
3916 }
3917
3918 gIOPMWorkLoop->runAction(actionDriverCalloutDone,
3919 /* target */ from,
3920 /* arg0 */ (void *) from->pwrMgt );
3921}
3922
3923//*********************************************************************************
3924// [private] driverSetPowerState
3925//
3926// Thread call context
3927//*********************************************************************************
3928
3929void IOService::driverSetPowerState( void )
3930{
3931 IOPMPowerStateIndex powerState;
3932 DriverCallParam * param;
3933 IOPMDriverCallEntry callEntry;
3934 AbsoluteTime end;
3935 IOReturn result;
3936 uint32_t oldPowerState = getPowerState();
3937
3938 assert( fDriverCallBusy );
3939 assert( fDriverCallParamPtr );
3940 assert( fDriverCallParamCount == 1 );
3941
3942 param = (DriverCallParam *) fDriverCallParamPtr;
3943 powerState = fHeadNotePowerState;
3944
3945 if (assertPMDriverCall(&callEntry))
3946 {
3947 OUR_PMLog( kPMLogProgramHardware, (uintptr_t) this, powerState);
3948 start_spindump_timer("SetState");
3949 clock_get_uptime(&fDriverCallStartTime);
3950 result = fControllingDriver->setPowerState( powerState, this );
3951 clock_get_uptime(&end);
3952 stop_spindump_timer();
3953 OUR_PMLog((UInt32) -kPMLogProgramHardware, (uintptr_t) this, (UInt32) result);
3954
3955 deassertPMDriverCall(&callEntry);
3956
3957 // Record the most recent max power state residency timings.
3958 // Use with DeviceActiveTimestamp to diagnose tickle issues.
3959 if (powerState == fHighestPowerState)
3960 fMaxPowerStateEntryTime = end;
3961 else if (oldPowerState == fHighestPowerState)
3962 fMaxPowerStateExitTime = end;
3963
3964 if (result < 0)
3965 {
3966 PM_LOG("%s::setPowerState(%p, %lu -> %lu) returned 0x%x\n",
3967 fName, OBFUSCATE(this), fCurrentPowerState, powerState, result);
3968 }
3969
3970#if LOG_SETPOWER_TIMES
3971 if ((result == IOPMAckImplied) || (result < 0))
3972 {
3973 uint64_t nsec;
3974
3975 SUB_ABSOLUTETIME(&end, &fDriverCallStartTime);
3976 absolutetime_to_nanoseconds(end, &nsec);
3977 if (nsec > LOG_SETPOWER_TIMES) {
3978 getPMRootDomain()->pmStatsRecordApplicationResponse(
3979 gIOPMStatsDriverPSChangeSlow,
3980 fName, kDriverCallSetPowerState, NS_TO_MS(nsec), 0, NULL, powerState);
3981 }
3982 }
3983#endif
3984 }
3985 else
3986 result = kIOPMAckImplied;
3987
3988 param->Result = result;
3989}
3990
3991//*********************************************************************************
3992// [private] driverInformPowerChange
3993//
3994// Thread call context
3995//*********************************************************************************
3996
3997void IOService::driverInformPowerChange( void )
3998{
3999 IOPMinformee * informee;
4000 IOService * driver;
4001 DriverCallParam * param;
4002 IOPMDriverCallEntry callEntry;
4003 IOPMPowerFlags powerFlags;
4004 IOPMPowerStateIndex powerState;
4005 AbsoluteTime end;
4006 IOReturn result;
4007 IOItemCount count;
4008
4009 assert( fDriverCallBusy );
4010 assert( fDriverCallParamPtr );
4011 assert( fDriverCallParamCount );
4012
4013 param = (DriverCallParam *) fDriverCallParamPtr;
4014 count = fDriverCallParamCount;
4015
4016 powerFlags = fHeadNotePowerArrayEntry->capabilityFlags;
4017 powerState = fHeadNotePowerState;
4018
4019 for (IOItemCount i = 0; i < count; i++)
4020 {
4021 informee = (IOPMinformee *) param->Target;
4022 driver = informee->whatObject;
4023
4024 if (assertPMDriverCall(&callEntry, 0, informee))
4025 {
4026 if (fDriverCallReason == kDriverCallInformPreChange)
4027 {
4028 OUR_PMLog(kPMLogInformDriverPreChange, (uintptr_t) this, powerState);
4029 start_spindump_timer("WillChange");
4030 clock_get_uptime(&informee->startTime);
4031 result = driver->powerStateWillChangeTo(powerFlags, powerState, this);
4032 clock_get_uptime(&end);
4033 stop_spindump_timer();
4034 OUR_PMLog((UInt32)-kPMLogInformDriverPreChange, (uintptr_t) this, result);
4035 }
4036 else
4037 {
4038 OUR_PMLog(kPMLogInformDriverPostChange, (uintptr_t) this, powerState);
4039 start_spindump_timer("DidChange");
4040 clock_get_uptime(&informee->startTime);
4041 result = driver->powerStateDidChangeTo(powerFlags, powerState, this);
4042 clock_get_uptime(&end);
4043 stop_spindump_timer();
4044 OUR_PMLog((UInt32)-kPMLogInformDriverPostChange, (uintptr_t) this, result);
4045 }
4046
4047 deassertPMDriverCall(&callEntry);
4048
4049#if LOG_SETPOWER_TIMES
4050 if ((result == IOPMAckImplied) || (result < 0))
4051 {
4052 uint64_t nsec;
4053
4054 SUB_ABSOLUTETIME(&end, &informee->startTime);
4055 absolutetime_to_nanoseconds(end, &nsec);
4056 if (nsec > LOG_SETPOWER_TIMES) {
4057 getPMRootDomain()->pmStatsRecordApplicationResponse(
4058 gIOPMStatsDriverPSChangeSlow, driver->getName(),
4059 fDriverCallReason, NS_TO_MS(nsec), 0, NULL, powerState);
4060 }
4061 }
4062#endif
4063 }
4064 else
4065 result = kIOPMAckImplied;
4066
4067 param->Result = result;
4068 param++;
4069 }
4070}
4071
4072//*********************************************************************************
4073// [private] notifyChild
4074//
4075// Notify a power domain child of an upcoming power change.
4076// If the object acknowledges the current change, we return TRUE.
4077//*********************************************************************************
4078
4079bool IOService::notifyChild( IOPowerConnection * theNub )
4080{
4081 IOReturn ret = IOPMAckImplied;
4082 unsigned long childPower;
4083 IOService * theChild;
4084 IOPMRequest * childRequest;
4085 IOPMPowerChangeFlags requestArg2;
4086 int requestType;
4087
4088 PM_ASSERT_IN_GATE();
4089 theChild = (IOService *)(theNub->copyChildEntry(gIOPowerPlane));
4090 if (!theChild)
4091 {
4092 return true;
4093 }
4094
4095 // Unless the child handles the notification immediately and returns
4096 // kIOPMAckImplied, we'll be awaiting their acknowledgement later.
4097 fHeadNotePendingAcks++;
4098 theNub->setAwaitingAck(true);
4099
4100 requestArg2 = fHeadNoteChangeFlags;
4101 if (StateOrder(fHeadNotePowerState) < StateOrder(fCurrentPowerState))
4102 requestArg2 |= kIOPMDomainPowerDrop;
4103
4104 requestType = fIsPreChange ?
4105 kIOPMRequestTypePowerDomainWillChange :
4106 kIOPMRequestTypePowerDomainDidChange;
4107
4108 childRequest = acquirePMRequest( theChild, requestType );
4109 if (childRequest)
4110 {
4111 theNub->retain();
4112 childRequest->fArg0 = (void *) fHeadNotePowerArrayEntry->outputPowerFlags;
4113 childRequest->fArg1 = (void *) theNub;
4114 childRequest->fArg2 = (void *)(uintptr_t) requestArg2;
4115 theChild->submitPMRequest( childRequest );
4116 ret = IOPMWillAckLater;
4117 }
4118 else
4119 {
4120 ret = IOPMAckImplied;
4121 fHeadNotePendingAcks--;
4122 theNub->setAwaitingAck(false);
4123 childPower = theChild->currentPowerConsumption();
4124 if ( childPower == kIOPMUnknown )
4125 {
4126 fHeadNotePowerArrayEntry->staticPower = kIOPMUnknown;
4127 } else {
4128 if (fHeadNotePowerArrayEntry->staticPower != kIOPMUnknown )
4129 fHeadNotePowerArrayEntry->staticPower += childPower;
4130 }
4131 }
4132
4133 theChild->release();
4134 return (IOPMAckImplied == ret);
4135}
4136
4137//*********************************************************************************
4138// [private] notifyControllingDriver
4139//*********************************************************************************
4140
4141bool IOService::notifyControllingDriver( void )
4142{
4143 DriverCallParam * param;
4144
4145 PM_ASSERT_IN_GATE();
4146 assert( fDriverCallParamCount == 0 );
4147 assert( fControllingDriver );
4148
4149 if (fInitialSetPowerState)
4150 {
4151 fInitialSetPowerState = false;
4152 fHeadNoteChangeFlags |= kIOPMInitialPowerChange;
4153
4154 // Driver specified flag to skip the inital setPowerState()
4155 if (fHeadNotePowerArrayEntry->capabilityFlags & kIOPMInitialDeviceState)
4156 {
4157 return false;
4158 }
4159 }
4160
4161 param = (DriverCallParam *) fDriverCallParamPtr;
4162 if (!param)
4163 {
4164 param = IONew(DriverCallParam, 1);
4165 if (!param)
4166 return false; // no memory
4167
4168 fDriverCallParamPtr = (void *) param;
4169 fDriverCallParamSlots = 1;
4170 }
4171
4172 param->Target = fControllingDriver;
4173 fDriverCallParamCount = 1;
4174 fDriverTimer = -1;
4175
4176 // Block state machine and wait for callout completion.
4177 assert(!fDriverCallBusy);
4178 fDriverCallBusy = true;
4179 thread_call_enter( fDriverCallEntry );
4180
4181 return true;
4182}
4183
4184//*********************************************************************************
4185// [private] notifyControllingDriverDone
4186//*********************************************************************************
4187
4188void IOService::notifyControllingDriverDone( void )
4189{
4190 DriverCallParam * param;
4191 IOReturn result;
4192
4193 PM_ASSERT_IN_GATE();
4194 param = (DriverCallParam *) fDriverCallParamPtr;
4195
4196 assert( fDriverCallBusy == false );
4197 assert( fMachineState == kIOPM_DriverThreadCallDone );
4198
4199 if (param && fDriverCallParamCount)
4200 {
4201 assert(fDriverCallParamCount == 1);
4202
4203 // the return value from setPowerState()
4204 result = param->Result;
4205
4206 if ((result == IOPMAckImplied) || (result < 0))
4207 {
4208 fDriverTimer = 0;
4209 }
4210 else if (fDriverTimer)
4211 {
4212 assert(fDriverTimer == -1);
4213
4214 // Driver has not acked, and has returned a positive result.
4215 // Enforce a minimum permissible timeout value.
4216 // Make the min value large enough so timeout is less likely
4217 // to occur if a driver misinterpreted that the return value
4218 // should be in microsecond units. And make it large enough
4219 // to be noticeable if a driver neglects to ack.
4220
4221 if (result < kMinAckTimeoutTicks)
4222 result = kMinAckTimeoutTicks;
4223
4224 fDriverTimer = (result / (ACK_TIMER_PERIOD / ns_per_us)) + 1;
4225 }
4226 // else, child has already acked and driver_timer reset to 0.
4227
4228 fDriverCallParamCount = 0;
4229
4230 if ( fDriverTimer )
4231 {
4232 OUR_PMLog(kPMLogStartAckTimer, 0, 0);
4233 start_ack_timer();
4234 }
4235 }
4236
4237 MS_POP(); // pushed by OurChangeSetPowerState()
4238 fIsPreChange = false;
4239}
4240
4241//*********************************************************************************
4242// [private] all_done
4243//
4244// A power change is done.
4245//*********************************************************************************
4246
4247void IOService::all_done( void )
4248{
4249 IOPMPowerStateIndex prevPowerState;
4250 const IOPMPSEntry * powerStatePtr;
4251 IOPMDriverCallEntry callEntry;
4252 uint32_t prevMachineState = fMachineState;
4253 bool actionCalled = false;
4254 uint64_t ts;
4255
4256 fMachineState = kIOPM_Finished;
4257
4258 if ((fHeadNoteChangeFlags & kIOPMSynchronize) &&
4259 ((prevMachineState == kIOPM_Finished) ||
4260 (prevMachineState == kIOPM_SyncFinish)))
4261 {
4262 // Sync operation and no power change occurred.
4263 // Do not inform driver and clients about this request completion,
4264 // except for the originator (root domain).
4265
4266 PM_ACTION_2(actionPowerChangeDone,
4267 fHeadNotePowerState, fHeadNoteChangeFlags);
4268
4269 if (getPMRequestType() == kIOPMRequestTypeSynchronizePowerTree)
4270 {
4271 powerChangeDone(fCurrentPowerState);
4272 }
4273 else if (fAdvisoryTickleUsed)
4274 {
4275 // Not root domain and advisory tickle target.
4276 // Re-adjust power after power tree sync at the 'did' pass
4277 // to recompute desire and adjust power state between dark
4278 // and full wake transitions. Root domain is responsible
4279 // for calling setAdvisoryTickleEnable() before starting
4280 // the kIOPMSynchronize power change.
4281
4282 if (!fAdjustPowerScheduled &&
4283 (fHeadNoteChangeFlags & kIOPMDomainDidChange))
4284 {
4285 IOPMRequest * request;
4286 request = acquirePMRequest( this, kIOPMRequestTypeAdjustPowerState );
4287 if (request)
4288 {
4289 submitPMRequest( request );
4290 fAdjustPowerScheduled = true;
4291 }
4292 }
4293 }
4294
4295 return;
4296 }
4297
4298 // our power change
4299 if (fHeadNoteChangeFlags & kIOPMSelfInitiated)
4300 {
4301 // power state changed
4302 if ((fHeadNoteChangeFlags & kIOPMNotDone) == 0)
4303 {
4304 trackSystemSleepPreventers(
4305 fCurrentPowerState, fHeadNotePowerState, fHeadNoteChangeFlags);
4306
4307 // we changed, tell our parent
4308 requestDomainPower(fHeadNotePowerState);
4309
4310 // yes, did power raise?
4311 if ( StateOrder(fCurrentPowerState) < StateOrder(fHeadNotePowerState) )
4312 {
4313 // yes, inform clients and apps
4314 tellChangeUp (fHeadNotePowerState);
4315 }
4316 prevPowerState = fCurrentPowerState;
4317 // either way
4318 fCurrentPowerState = fHeadNotePowerState;
4319 PM_LOCK();
4320 if (fReportBuf) {
4321 ts = mach_absolute_time();
4322 STATEREPORT_SETSTATE(fReportBuf, fCurrentPowerState, ts);
4323 }
4324 PM_UNLOCK();
4325#if PM_VARS_SUPPORT
4326 fPMVars->myCurrentState = fCurrentPowerState;
4327#endif
4328 OUR_PMLog(kPMLogChangeDone, fCurrentPowerState, prevPowerState);
4329 PM_ACTION_2(actionPowerChangeDone,
4330 fHeadNotePowerState, fHeadNoteChangeFlags);
4331 actionCalled = true;
4332
4333 powerStatePtr = &fPowerStates[fCurrentPowerState];
4334 fCurrentCapabilityFlags = powerStatePtr->capabilityFlags;
4335 if (fCurrentCapabilityFlags & kIOPMStaticPowerValid)
4336 fCurrentPowerConsumption = powerStatePtr->staticPower;
4337
4338 if (fHeadNoteChangeFlags & kIOPMRootChangeDown)
4339 {
4340 // Bump tickle generation count once the entire tree is down
4341 gIOPMTickleGeneration++;
4342 }
4343
4344 // inform subclass policy-maker
4345 if (fPCDFunctionOverride && fParentsKnowState &&
4346 assertPMDriverCall(&callEntry, kIOPMADC_NoInactiveCheck))
4347 {
4348 powerChangeDone(prevPowerState);
4349 deassertPMDriverCall(&callEntry);
4350 }
4351 }
4352 else if (getPMRequestType() == kIOPMRequestTypeRequestPowerStateOverride)
4353 {
4354 // changePowerStateWithOverrideTo() was cancelled
4355 fOverrideMaxPowerState = kIOPMPowerStateMax;
4356 }
4357 }
4358
4359 // parent-initiated power change
4360 if (fHeadNoteChangeFlags & kIOPMParentInitiated)
4361 {
4362 if (fHeadNoteChangeFlags & kIOPMRootChangeDown)
4363 ParentChangeRootChangeDown();
4364
4365 // power state changed
4366 if ((fHeadNoteChangeFlags & kIOPMNotDone) == 0)
4367 {
4368 trackSystemSleepPreventers(
4369 fCurrentPowerState, fHeadNotePowerState, fHeadNoteChangeFlags);
4370
4371 // did power raise?
4372 if ( StateOrder(fCurrentPowerState) < StateOrder(fHeadNotePowerState) )
4373 {
4374 // yes, inform clients and apps
4375 tellChangeUp (fHeadNotePowerState);
4376 }
4377 // either way
4378 prevPowerState = fCurrentPowerState;
4379 fCurrentPowerState = fHeadNotePowerState;
4380 PM_LOCK();
4381 if (fReportBuf) {
4382 ts = mach_absolute_time();
4383 STATEREPORT_SETSTATE(fReportBuf, fCurrentPowerState, ts);
4384 }
4385 PM_UNLOCK();
4386#if PM_VARS_SUPPORT
4387 fPMVars->myCurrentState = fCurrentPowerState;
4388#endif
4389
4390 OUR_PMLog(kPMLogChangeDone, fCurrentPowerState, prevPowerState);
4391 PM_ACTION_2(actionPowerChangeDone,
4392 fHeadNotePowerState, fHeadNoteChangeFlags);
4393 actionCalled = true;
4394
4395 powerStatePtr = &fPowerStates[fCurrentPowerState];
4396 fCurrentCapabilityFlags = powerStatePtr->capabilityFlags;
4397 if (fCurrentCapabilityFlags & kIOPMStaticPowerValid)
4398 fCurrentPowerConsumption = powerStatePtr->staticPower;
4399
4400 // inform subclass policy-maker
4401 if (fPCDFunctionOverride && fParentsKnowState &&
4402 assertPMDriverCall(&callEntry, kIOPMADC_NoInactiveCheck))
4403 {
4404 powerChangeDone(prevPowerState);
4405 deassertPMDriverCall(&callEntry);
4406 }
4407 }
4408 }
4409
4410 // When power rises enough to satisfy the tickle's desire for more power,
4411 // the condition preventing idle-timer from dropping power is removed.
4412
4413 if (StateOrder(fCurrentPowerState) >= StateOrder(fIdleTimerMinPowerState))
4414 {
4415 fIdleTimerMinPowerState = kPowerStateZero;
4416 }
4417
4418 if (!actionCalled)
4419 {
4420 PM_ACTION_2(actionPowerChangeDone,
4421 fHeadNotePowerState, fHeadNoteChangeFlags);
4422 }
4423}
4424
4425// MARK: -
4426// MARK: Power Change Initiated by Driver
4427
4428//*********************************************************************************
4429// [private] OurChangeStart
4430//
4431// Begin the processing of a power change initiated by us.
4432//*********************************************************************************
4433
4434void IOService::OurChangeStart( void )
4435{
4436 PM_ASSERT_IN_GATE();
4437 OUR_PMLog( kPMLogStartDeviceChange, fHeadNotePowerState, fCurrentPowerState );
4438
4439 // fMaxPowerState is our maximum possible power state based on the current
4440 // power state of our parents. If we are trying to raise power beyond the
4441 // maximum, send an async request for more power to all parents.
4442
4443 if (!IS_PM_ROOT && (StateOrder(fMaxPowerState) < StateOrder(fHeadNotePowerState)))
4444 {
4445 fHeadNoteChangeFlags |= kIOPMNotDone;
4446 requestDomainPower(fHeadNotePowerState);
4447 OurChangeFinish();
4448 return;
4449 }
4450
4451 // Redundant power changes skips to the end of the state machine.
4452
4453 if (!fInitialPowerChange && (fHeadNotePowerState == fCurrentPowerState))
4454 {
4455 OurChangeFinish();
4456 return;
4457 }
4458 fInitialPowerChange = false;
4459
4460 // Change started, but may not complete...
4461 // Can be canceled (power drop) or deferred (power rise).
4462
4463 PM_ACTION_2(actionPowerChangeStart, fHeadNotePowerState, &fHeadNoteChangeFlags);
4464
4465 // Two separate paths, depending if power is being raised or lowered.
4466 // Lowering power is subject to approval by clients of this service.
4467
4468 if (IS_POWER_DROP)
4469 {
4470 fDoNotPowerDown = false;
4471
4472 // Ask for persmission to drop power state
4473 fMachineState = kIOPM_OurChangeTellClientsPowerDown;
4474 fOutOfBandParameter = kNotifyApps;
4475 askChangeDown(fHeadNotePowerState);
4476 }
4477 else
4478 {
4479 // This service is raising power and parents are able to support the
4480 // new power state. However a parent may have already committed to
4481 // drop power, which might force this object to temporarily drop power.
4482 // This results in "oscillations" before the state machines converge
4483 // to a steady state.
4484 //
4485 // To prevent this, a child must make a power reservation against all
4486 // parents before raising power. If the reservation fails, indicating
4487 // that the child will be unable to sustain the higher power state,
4488 // then the child will signal the parent to adjust power, and the child
4489 // will defer its power change.
4490
4491 IOReturn ret;
4492
4493 // Reserve parent power necessary to achieve fHeadNotePowerState.
4494 ret = requestDomainPower( fHeadNotePowerState, kReserveDomainPower );
4495 if (ret != kIOReturnSuccess)
4496 {
4497 // Reservation failed, defer power rise.
4498 fHeadNoteChangeFlags |= kIOPMNotDone;
4499 OurChangeFinish();
4500 return;
4501 }
4502
4503 OurChangeTellCapabilityWillChange();
4504 }
4505}
4506
4507//*********************************************************************************
4508// [private] requestDomainPowerApplier
4509//
4510// Call requestPowerDomainState() on all power parents.
4511//*********************************************************************************
4512
4513struct IOPMRequestDomainPowerContext {
4514 IOService * child; // the requesting child
4515 IOPMPowerFlags requestPowerFlags; // power flags requested by child
4516};
4517
4518static void
4519requestDomainPowerApplier(
4520 IORegistryEntry * entry,
4521 void * inContext )
4522{
4523 IOPowerConnection * connection;
4524 IOService * parent;
4525 IOPMRequestDomainPowerContext * context;
4526
4527 if ((connection = OSDynamicCast(IOPowerConnection, entry)) == 0)
4528 return;
4529 parent = (IOService *) connection->copyParentEntry(gIOPowerPlane);
4530 if (!parent)
4531 return;
4532
4533 assert(inContext);
4534 context = (IOPMRequestDomainPowerContext *) inContext;
4535
4536 if (connection->parentKnowsState() && connection->getReadyFlag())
4537 {
4538 parent->requestPowerDomainState(
4539 context->requestPowerFlags,
4540 connection,
4541 IOPMLowestState);
4542 }
4543
4544 parent->release();
4545}
4546
4547//*********************************************************************************
4548// [private] requestDomainPower
4549//
4550// Called by a power child to broadcast its desired power state to all parents.
4551// If the child self-initiates a power change, it must call this function to
4552// allow its parents to adjust power state.
4553//*********************************************************************************
4554
4555IOReturn IOService::requestDomainPower(
4556 IOPMPowerStateIndex ourPowerState,
4557 IOOptionBits options )
4558{
4559 IOPMPowerFlags requestPowerFlags;
4560 IOPMPowerStateIndex maxPowerState;
4561 IOPMRequestDomainPowerContext context;
4562
4563 PM_ASSERT_IN_GATE();
4564 assert(ourPowerState < fNumberOfPowerStates);
4565 if (ourPowerState >= fNumberOfPowerStates)
4566 return kIOReturnBadArgument;
4567 if (IS_PM_ROOT)
4568 return kIOReturnSuccess;
4569
4570 // Fetch our input power flags for the requested power state.
4571 // Parent request is stated in terms of required power flags.
4572
4573 requestPowerFlags = fPowerStates[ourPowerState].inputPowerFlags;
4574
4575 // Disregard the "previous request" for power reservation.
4576
4577 if (((options & kReserveDomainPower) == 0) &&
4578 (fPreviousRequestPowerFlags == requestPowerFlags))
4579 {
4580 // skip if domain already knows our requirements
4581 goto done;
4582 }
4583 fPreviousRequestPowerFlags = requestPowerFlags;
4584
4585 // The results will be collected by fHeadNoteDomainTargetFlags
4586 context.child = this;
4587 context.requestPowerFlags = requestPowerFlags;
4588 fHeadNoteDomainTargetFlags = 0;
4589 applyToParents(requestDomainPowerApplier, &context, gIOPowerPlane);
4590
4591 if (options & kReserveDomainPower)
4592 {
4593 maxPowerState = fControllingDriver->maxCapabilityForDomainState(
4594 fHeadNoteDomainTargetFlags );
4595
4596 if (StateOrder(maxPowerState) < StateOrder(ourPowerState))
4597 {
4598 PM_LOG1("%s: power desired %u:0x%x got %u:0x%x\n",
4599 getName(),
4600 (uint32_t) ourPowerState, (uint32_t) requestPowerFlags,
4601 (uint32_t) maxPowerState, (uint32_t) fHeadNoteDomainTargetFlags);
4602 return kIOReturnNoPower;
4603 }
4604 }
4605
4606done:
4607 return kIOReturnSuccess;
4608}
4609
4610//*********************************************************************************
4611// [private] OurSyncStart
4612//*********************************************************************************
4613
4614void IOService::OurSyncStart( void )
4615{
4616 PM_ASSERT_IN_GATE();
4617
4618 if (fInitialPowerChange)
4619 return;
4620
4621 PM_ACTION_2(actionPowerChangeStart, fHeadNotePowerState, &fHeadNoteChangeFlags);
4622
4623 if (fHeadNoteChangeFlags & kIOPMNotDone)
4624 {
4625 OurChangeFinish();
4626 return;
4627 }
4628
4629 if (fHeadNoteChangeFlags & kIOPMSyncTellPowerDown)
4630 {
4631 fDoNotPowerDown = false;
4632
4633 // Ask for permission to drop power state
4634 fMachineState = kIOPM_SyncTellClientsPowerDown;
4635 fOutOfBandParameter = kNotifyApps;
4636 askChangeDown(fHeadNotePowerState);
4637 }
4638 else
4639 {
4640 // Only inform capability app and clients.
4641 tellSystemCapabilityChange( kIOPM_SyncNotifyWillChange );
4642 }
4643}
4644
4645//*********************************************************************************
4646// [private] OurChangeTellClientsPowerDown
4647//
4648// All applications and kernel clients have acknowledged our permission to drop
4649// power. Here we notify them that we will lower the power and wait for acks.
4650//*********************************************************************************
4651
4652void IOService::OurChangeTellClientsPowerDown( void )
4653{
4654 if(!IS_ROOT_DOMAIN)
4655 fMachineState = kIOPM_OurChangeTellPriorityClientsPowerDown;
4656 else
4657 {
4658 fMachineState = kIOPM_OurChangeTellUserPMPolicyPowerDown;
4659 }
4660 tellChangeDown1(fHeadNotePowerState);
4661}
4662
4663//*********************************************************************************
4664// [private] OurChangeTellUserPMPolicyPowerDown
4665//
4666// All applications and kernel clients have acknowledged our permission to drop
4667// power. Here we notify power management policy in user-space and wait for acks
4668// one last time before we lower power
4669//*********************************************************************************
4670void IOService::OurChangeTellUserPMPolicyPowerDown ( void )
4671{
4672 fMachineState = kIOPM_OurChangeTellPriorityClientsPowerDown;
4673 fOutOfBandParameter = kNotifyApps;
4674
4675 tellClientsWithResponse(kIOPMMessageLastCallBeforeSleep);
4676}
4677
4678//*********************************************************************************
4679// [private] OurChangeTellPriorityClientsPowerDown
4680//
4681// All applications and kernel clients have acknowledged our intention to drop
4682// power. Here we notify "priority" clients that we are lowering power.
4683//*********************************************************************************
4684
4685void IOService::OurChangeTellPriorityClientsPowerDown( void )
4686{
4687 fMachineState = kIOPM_OurChangeNotifyInterestedDriversWillChange;
4688 tellChangeDown2(fHeadNotePowerState);
4689}
4690
4691//*********************************************************************************
4692// [private] OurChangeTellCapabilityWillChange
4693//
4694// Extra stage for root domain to notify apps and drivers about the
4695// system capability change when raising power state.
4696//*********************************************************************************
4697
4698void IOService::OurChangeTellCapabilityWillChange( void )
4699{
4700 if (!IS_ROOT_DOMAIN)
4701 return OurChangeNotifyInterestedDriversWillChange();
4702
4703 tellSystemCapabilityChange( kIOPM_OurChangeNotifyInterestedDriversWillChange );
4704}
4705
4706//*********************************************************************************
4707// [private] OurChangeNotifyInterestedDriversWillChange
4708//
4709// All applications and kernel clients have acknowledged our power state change.
4710// Here we notify interested drivers pre-change.
4711//*********************************************************************************
4712
4713void IOService::OurChangeNotifyInterestedDriversWillChange( void )
4714{
4715 IOPMrootDomain * rootDomain;
4716 if ((rootDomain = getPMRootDomain()) == this)
4717 {
4718 if (IS_POWER_DROP)
4719 {
4720 rootDomain->tracePoint( kIOPMTracePointSleepWillChangeInterests );
4721 }
4722 else
4723 rootDomain->tracePoint( kIOPMTracePointWakeWillChangeInterests );
4724 }
4725
4726 notifyAll( kIOPM_OurChangeSetPowerState );
4727}
4728
4729//*********************************************************************************
4730// [private] OurChangeSetPowerState
4731//
4732// Instruct our controlling driver to program the hardware for the power state
4733// change. Wait for async completions.
4734//*********************************************************************************
4735
4736void IOService::OurChangeSetPowerState( void )
4737{
4738 MS_PUSH( kIOPM_OurChangeWaitForPowerSettle );
4739 fMachineState = kIOPM_DriverThreadCallDone;
4740 fDriverCallReason = kDriverCallSetPowerState;
4741
4742 if (notifyControllingDriver() == false)
4743 notifyControllingDriverDone();
4744}
4745
4746//*********************************************************************************
4747// [private] OurChangeWaitForPowerSettle
4748//
4749// Our controlling driver has completed the power state change we initiated.
4750// Wait for the driver specified settle time to expire.
4751//*********************************************************************************
4752
4753void IOService::OurChangeWaitForPowerSettle( void )
4754{
4755 fMachineState = kIOPM_OurChangeNotifyInterestedDriversDidChange;
4756 startSettleTimer();
4757}
4758
4759//*********************************************************************************
4760// [private] OurChangeNotifyInterestedDriversDidChange
4761//
4762// Power has settled on a power change we initiated. Here we notify
4763// all our interested drivers post-change.
4764//*********************************************************************************
4765
4766void IOService::OurChangeNotifyInterestedDriversDidChange( void )
4767{
4768 IOPMrootDomain * rootDomain;
4769 if ((rootDomain = getPMRootDomain()) == this)
4770 {
4771 rootDomain->tracePoint( IS_POWER_DROP ?
4772 kIOPMTracePointSleepDidChangeInterests :
4773 kIOPMTracePointWakeDidChangeInterests );
4774 }
4775
4776 notifyAll( kIOPM_OurChangeTellCapabilityDidChange );
4777}
4778
4779//*********************************************************************************
4780// [private] OurChangeTellCapabilityDidChange
4781//
4782// For root domain to notify capability power-change.
4783//*********************************************************************************
4784
4785void IOService::OurChangeTellCapabilityDidChange( void )
4786{
4787 if (!IS_ROOT_DOMAIN)
4788 return OurChangeFinish();
4789
4790 getPMRootDomain()->tracePoint( IS_POWER_DROP ?
4791 kIOPMTracePointSleepCapabilityClients :
4792 kIOPMTracePointWakeCapabilityClients );
4793
4794 tellSystemCapabilityChange( kIOPM_OurChangeFinish );
4795}
4796
4797//*********************************************************************************
4798// [private] OurChangeFinish
4799//
4800// Done with this self-induced power state change.
4801//*********************************************************************************
4802
4803void IOService::OurChangeFinish( void )
4804{
4805 all_done();
4806}
4807
4808// MARK: -
4809// MARK: Power Change Initiated by Parent
4810
4811//*********************************************************************************
4812// [private] ParentChangeStart
4813//
4814// Here we begin the processing of a power change initiated by our parent.
4815//*********************************************************************************
4816
4817IOReturn IOService::ParentChangeStart( void )
4818{
4819 PM_ASSERT_IN_GATE();
4820 OUR_PMLog( kPMLogStartParentChange, fHeadNotePowerState, fCurrentPowerState );
4821
4822 // Root power domain has transitioned to its max power state
4823 if ((fHeadNoteChangeFlags & (kIOPMDomainDidChange | kIOPMRootChangeUp)) ==
4824 (kIOPMDomainDidChange | kIOPMRootChangeUp))
4825 {
4826 // Restart the idle timer stopped by ParentChangeRootChangeDown()
4827 if (fIdleTimerPeriod && fIdleTimerStopped)
4828 {
4829 restartIdleTimer();
4830 }
4831 }
4832
4833 // Power domain is forcing us to lower power
4834 if ( StateOrder(fHeadNotePowerState) < StateOrder(fCurrentPowerState) )
4835 {
4836 PM_ACTION_2(actionPowerChangeStart, fHeadNotePowerState, &fHeadNoteChangeFlags);
4837
4838 // Tell apps and kernel clients
4839 fInitialPowerChange = false;
4840 fMachineState = kIOPM_ParentChangeTellPriorityClientsPowerDown;
4841 tellChangeDown1(fHeadNotePowerState);
4842 return IOPMWillAckLater;
4843 }
4844
4845 // Power domain is allowing us to raise power up to fHeadNotePowerState
4846 if ( StateOrder(fHeadNotePowerState) > StateOrder(fCurrentPowerState) )
4847 {
4848 if ( StateOrder(fDesiredPowerState) > StateOrder(fCurrentPowerState) )
4849 {
4850 if ( StateOrder(fDesiredPowerState) < StateOrder(fHeadNotePowerState) )
4851 {
4852 // We power up, but not all the way
4853 fHeadNotePowerState = fDesiredPowerState;
4854 fHeadNotePowerArrayEntry = &fPowerStates[fDesiredPowerState];
4855 OUR_PMLog(kPMLogAmendParentChange, fHeadNotePowerState, 0);
4856 }
4857 } else {
4858 // We don't need to change
4859 fHeadNotePowerState = fCurrentPowerState;
4860 fHeadNotePowerArrayEntry = &fPowerStates[fCurrentPowerState];
4861 OUR_PMLog(kPMLogAmendParentChange, fHeadNotePowerState, 0);
4862 }
4863 }
4864
4865 if ( fHeadNoteChangeFlags & kIOPMDomainDidChange )
4866 {
4867 if ( StateOrder(fHeadNotePowerState) > StateOrder(fCurrentPowerState) )
4868 {
4869 PM_ACTION_2(actionPowerChangeStart,
4870 fHeadNotePowerState, &fHeadNoteChangeFlags);
4871
4872 // Parent did change up - start our change up
4873 fInitialPowerChange = false;
4874 ParentChangeTellCapabilityWillChange();
4875 return IOPMWillAckLater;
4876 }
4877 else if (fHeadNoteChangeFlags & kIOPMRootBroadcastFlags)
4878 {
4879 // No need to change power state, but broadcast change
4880 // to our children.
4881 fMachineState = kIOPM_SyncNotifyDidChange;
4882 fDriverCallReason = kDriverCallInformPreChange;
4883 fHeadNoteChangeFlags |= kIOPMNotDone;
4884 notifyChildren();
4885 return IOPMWillAckLater;
4886 }
4887 }
4888
4889 // No power state change necessary
4890 fHeadNoteChangeFlags |= kIOPMNotDone;
4891
4892 all_done();
4893 return IOPMAckImplied;
4894}
4895
4896//******************************************************************************
4897// [private] ParentChangeRootChangeDown
4898//
4899// Root domain has finished the transition to the system sleep state. And all
4900// drivers in the power plane should have powered down. Cancel the idle timer,
4901// and also reset the device desire for those drivers that don't want power
4902// automatically restored on wake.
4903//******************************************************************************
4904
4905void IOService::ParentChangeRootChangeDown( void )
4906{
4907 // Always stop the idle timer before root power down
4908 if (fIdleTimerPeriod && !fIdleTimerStopped)
4909 {
4910 fIdleTimerStopped = true;
4911 if (fIdleTimer && thread_call_cancel(fIdleTimer))
4912 release();
4913 }
4914
4915 if (fResetPowerStateOnWake)
4916 {
4917 // Reset device desire down to the lowest power state.
4918 // Advisory tickle desire is intentionally untouched since
4919 // it has no effect until system is promoted to full wake.
4920
4921 if (fDeviceDesire != kPowerStateZero)
4922 {
4923 updatePowerClient(gIOPMPowerClientDevice, kPowerStateZero);
4924 computeDesiredState(kPowerStateZero, true);
4925 requestDomainPower( fDesiredPowerState );
4926 PM_LOG1("%s: tickle desire removed\n", fName);
4927 }
4928
4929 // Invalidate tickle cache so the next tickle will issue a request
4930 IOLockLock(fActivityLock);
4931 fDeviceWasActive = false;
4932 fActivityTicklePowerState = kInvalidTicklePowerState;
4933 IOLockUnlock(fActivityLock);
4934
4935 fIdleTimerMinPowerState = kPowerStateZero;
4936 }
4937 else if (fAdvisoryTickleUsed)
4938 {
4939 // Less aggressive mechanism to accelerate idle timer expiration
4940 // before system sleep. May not always allow the driver to wake
4941 // up from system sleep in the min power state.
4942
4943 AbsoluteTime now;
4944 uint64_t nsec;
4945 bool dropTickleDesire = false;
4946
4947 if (fIdleTimerPeriod && !fIdleTimerIgnored &&
4948 (fIdleTimerMinPowerState == kPowerStateZero) &&
4949 (fDeviceDesire != kPowerStateZero))
4950 {
4951 IOLockLock(fActivityLock);
4952
4953 if (!fDeviceWasActive)
4954 {
4955 // No tickles since the last idle timer expiration.
4956 // Safe to drop the device desire to zero.
4957 dropTickleDesire = true;
4958 }
4959 else
4960 {
4961 // Was tickled since the last idle timer expiration,
4962 // but not in the last minute.
4963 clock_get_uptime(&now);
4964 SUB_ABSOLUTETIME(&now, &fDeviceActiveTimestamp);
4965 absolutetime_to_nanoseconds(now, &nsec);
4966 if (nsec >= kNoTickleCancelWindow)
4967 {
4968 dropTickleDesire = true;
4969 }
4970 }
4971
4972 if (dropTickleDesire)
4973 {
4974 // Force the next tickle to raise power state
4975 fDeviceWasActive = false;
4976 fActivityTicklePowerState = kInvalidTicklePowerState;
4977 }
4978
4979 IOLockUnlock(fActivityLock);
4980 }
4981
4982 if (dropTickleDesire)
4983 {
4984 // Advisory tickle desire is intentionally untouched since
4985 // it has no effect until system is promoted to full wake.
4986
4987 updatePowerClient(gIOPMPowerClientDevice, kPowerStateZero);
4988 computeDesiredState(kPowerStateZero, true);
4989 PM_LOG1("%s: tickle desire dropped\n", fName);
4990 }
4991 }
4992}
4993
4994//*********************************************************************************
4995// [private] ParentChangeTellPriorityClientsPowerDown
4996//
4997// All applications and kernel clients have acknowledged our intention to drop
4998// power. Here we notify "priority" clients that we are lowering power.
4999//*********************************************************************************
5000
5001void IOService::ParentChangeTellPriorityClientsPowerDown( void )
5002{
5003 fMachineState = kIOPM_ParentChangeNotifyInterestedDriversWillChange;
5004 tellChangeDown2(fHeadNotePowerState);
5005}
5006
5007//*********************************************************************************
5008// [private] ParentChangeTellCapabilityWillChange
5009//
5010// All (legacy) applications and kernel clients have acknowledged, extra stage for
5011// root domain to notify apps and drivers about the system capability change.
5012//*********************************************************************************
5013
5014void IOService::ParentChangeTellCapabilityWillChange( void )
5015{
5016 if (!IS_ROOT_DOMAIN)
5017 return ParentChangeNotifyInterestedDriversWillChange();
5018
5019 tellSystemCapabilityChange( kIOPM_ParentChangeNotifyInterestedDriversWillChange );
5020}
5021
5022//*********************************************************************************
5023// [private] ParentChangeNotifyInterestedDriversWillChange
5024//
5025// All applications and kernel clients have acknowledged our power state change.
5026// Here we notify interested drivers pre-change.
5027//*********************************************************************************
5028
5029void IOService::ParentChangeNotifyInterestedDriversWillChange( void )
5030{
5031 notifyAll( kIOPM_ParentChangeSetPowerState );
5032}
5033
5034//*********************************************************************************
5035// [private] ParentChangeSetPowerState
5036//
5037// Instruct our controlling driver to program the hardware for the power state
5038// change. Wait for async completions.
5039//*********************************************************************************
5040
5041void IOService::ParentChangeSetPowerState( void )
5042{
5043 MS_PUSH( kIOPM_ParentChangeWaitForPowerSettle );
5044 fMachineState = kIOPM_DriverThreadCallDone;
5045 fDriverCallReason = kDriverCallSetPowerState;
5046
5047 if (notifyControllingDriver() == false)
5048 notifyControllingDriverDone();
5049}
5050
5051//*********************************************************************************
5052// [private] ParentChangeWaitForPowerSettle
5053//
5054// Our controlling driver has completed the power state change initiated by our
5055// parent. Wait for the driver specified settle time to expire.
5056//*********************************************************************************
5057
5058void IOService::ParentChangeWaitForPowerSettle( void )
5059{
5060 fMachineState = kIOPM_ParentChangeNotifyInterestedDriversDidChange;
5061 startSettleTimer();
5062}
5063
5064//*********************************************************************************
5065// [private] ParentChangeNotifyInterestedDriversDidChange
5066//
5067// Power has settled on a power change initiated by our parent. Here we notify
5068// all our interested drivers post-change.
5069//*********************************************************************************
5070
5071void IOService::ParentChangeNotifyInterestedDriversDidChange( void )
5072{
5073 notifyAll( kIOPM_ParentChangeTellCapabilityDidChange );
5074}
5075
5076//*********************************************************************************
5077// [private] ParentChangeTellCapabilityDidChange
5078//
5079// For root domain to notify capability power-change.
5080//*********************************************************************************
5081
5082void IOService::ParentChangeTellCapabilityDidChange( void )
5083{
5084 if (!IS_ROOT_DOMAIN)
5085 return ParentChangeAcknowledgePowerChange();
5086
5087 tellSystemCapabilityChange( kIOPM_ParentChangeAcknowledgePowerChange );
5088}
5089
5090//*********************************************************************************
5091// [private] ParentAcknowledgePowerChange
5092//
5093// Acknowledge our power parent that our power change is done.
5094//*********************************************************************************
5095
5096void IOService::ParentChangeAcknowledgePowerChange( void )
5097{
5098 IORegistryEntry * nub;
5099 IOService * parent;
5100
5101 nub = fHeadNoteParentConnection;
5102 nub->retain();
5103 all_done();
5104 parent = (IOService *)nub->copyParentEntry(gIOPowerPlane);
5105 if ( parent )
5106 {
5107 parent->acknowledgePowerChange((IOService *)nub);
5108 parent->release();
5109 }
5110 nub->release();
5111}
5112
5113// MARK: -
5114// MARK: Ack and Settle timers
5115
5116//*********************************************************************************
5117// [private] settleTimerExpired
5118//
5119// Power has settled after our last change. Notify interested parties that
5120// there is a new power state.
5121//*********************************************************************************
5122
5123void IOService::settleTimerExpired( void )
5124{
5125 fSettleTimeUS = 0;
5126 gIOPMWorkQueue->signalWorkAvailable();
5127}
5128
5129//*********************************************************************************
5130// settle_timer_expired
5131//
5132// Holds a retain while the settle timer callout is in flight.
5133//*********************************************************************************
5134
5135static void
5136settle_timer_expired( thread_call_param_t arg0, thread_call_param_t arg1 )
5137{
5138 IOService * me = (IOService *) arg0;
5139
5140 if (gIOPMWorkLoop && gIOPMWorkQueue)
5141 {
5142 gIOPMWorkLoop->runAction(
5143 OSMemberFunctionCast(IOWorkLoop::Action, me, &IOService::settleTimerExpired),
5144 me);
5145 }
5146 me->release();
5147}
5148
5149//*********************************************************************************
5150// [private] startSettleTimer
5151//
5152// Calculate a power-settling delay in microseconds and start a timer.
5153//*********************************************************************************
5154
5155void IOService::startSettleTimer( void )
5156{
5157#if NOT_USEFUL
5158 // This function is broken and serves no useful purpose since it never
5159 // updates fSettleTimeUS to a non-zero value to stall the state machine,
5160 // yet it starts a delay timer. It appears no driver relies on a delay
5161 // from settleUpTime and settleDownTime in the power state table.
5162
5163 AbsoluteTime deadline;
5164 IOPMPowerStateIndex stateIndex;
5165 IOPMPowerStateIndex currentOrder, newOrder, i;
5166 uint32_t settleTime = 0;
5167 boolean_t pending;
5168
5169 PM_ASSERT_IN_GATE();
5170
5171 currentOrder = StateOrder(fCurrentPowerState);
5172 newOrder = StateOrder(fHeadNotePowerState);
5173
5174 i = currentOrder;
5175
5176 // lowering power
5177 if ( newOrder < currentOrder )
5178 {
5179 while ( i > newOrder )
5180 {
5181 stateIndex = fPowerStates[i].stateOrderToIndex;
5182 settleTime += (uint32_t) fPowerStates[stateIndex].settleDownTime;
5183 i--;
5184 }
5185 }
5186
5187 // raising power
5188 if ( newOrder > currentOrder )
5189 {
5190 while ( i < newOrder )
5191 {
5192 stateIndex = fPowerStates[i+1].stateOrderToIndex;
5193 settleTime += (uint32_t) fPowerStates[stateIndex].settleUpTime;
5194 i++;
5195 }
5196 }
5197
5198 if (settleTime)
5199 {
5200 retain();
5201 clock_interval_to_deadline(settleTime, kMicrosecondScale, &deadline);
5202 pending = thread_call_enter_delayed(fSettleTimer, deadline);
5203 if (pending) release();
5204 }
5205#endif
5206}
5207
5208//*********************************************************************************
5209// [private] ackTimerTick
5210//
5211// The acknowledgement timeout periodic timer has ticked.
5212// If we are awaiting acks for a power change notification,
5213// we decrement the timer word of each interested driver which hasn't acked.
5214// If a timer word becomes zero, we pretend the driver aknowledged.
5215// If we are waiting for the controlling driver to change the power
5216// state of the hardware, we decrement its timer word, and if it becomes
5217// zero, we pretend the driver acknowledged.
5218//
5219// Returns true if the timer tick made it possible to advance to the next
5220// machine state, false otherwise.
5221//*********************************************************************************
5222
5223#ifndef __LP64__
5224void IOService::ack_timer_ticked ( void )
5225{
5226 assert(false);
5227}
5228#endif /* !__LP64__ */
5229
5230bool IOService::ackTimerTick( void )
5231{
5232 IOPMinformee * nextObject;
5233 bool done = false;
5234
5235 PM_ASSERT_IN_GATE();
5236 switch (fMachineState) {
5237 case kIOPM_OurChangeWaitForPowerSettle:
5238 case kIOPM_ParentChangeWaitForPowerSettle:
5239 // are we waiting for controlling driver to acknowledge?
5240 if ( fDriverTimer > 0 )
5241 {
5242 // yes, decrement timer tick
5243 fDriverTimer--;
5244 if ( fDriverTimer == 0 )
5245 {
5246 // controlling driver is tardy
5247 uint64_t nsec = computeTimeDeltaNS(&fDriverCallStartTime);
5248 OUR_PMLog(kPMLogCtrlDriverTardy, 0, 0);
5249 setProperty(kIOPMTardyAckSPSKey, kOSBooleanTrue);
5250 PM_ERROR("%s::setPowerState(%p, %lu -> %lu) timed out after %d ms\n",
5251 fName, OBFUSCATE(this), fCurrentPowerState, fHeadNotePowerState, NS_TO_MS(nsec));
5252
5253 if (gIOKitDebug & kIOLogDebugPower)
5254 {
5255 panic("%s::setPowerState(%p, %lu -> %lu) timed out after %d ms",
5256 fName, this, fCurrentPowerState, fHeadNotePowerState, NS_TO_MS(nsec));
5257 }
5258 else
5259 {
5260 // Unblock state machine and pretend driver has acked.
5261 done = true;
5262 }
5263 } else {
5264 // still waiting, set timer again
5265 start_ack_timer();
5266 }
5267 }
5268 break;
5269
5270 case kIOPM_NotifyChildrenStart:
5271 // are we waiting for interested parties to acknowledge?
5272 if ( fHeadNotePendingAcks != 0 )
5273 {
5274 // yes, go through the list of interested drivers
5275 nextObject = fInterestedDrivers->firstInList();
5276 // and check each one
5277 while ( nextObject != NULL )
5278 {
5279 if ( nextObject->timer > 0 )
5280 {
5281 nextObject->timer--;
5282 // this one should have acked by now
5283 if ( nextObject->timer == 0 )
5284 {
5285 uint64_t nsec = computeTimeDeltaNS(&nextObject->startTime);
5286 OUR_PMLog(kPMLogIntDriverTardy, 0, 0);
5287 nextObject->whatObject->setProperty(kIOPMTardyAckPSCKey, kOSBooleanTrue);
5288 PM_ERROR("%s::powerState%sChangeTo(%p, %s, %lu -> %lu) timed out after %d ms\n",
5289 nextObject->whatObject->getName(),
5290 (fDriverCallReason == kDriverCallInformPreChange) ? "Will" : "Did",
5291 OBFUSCATE(nextObject->whatObject), fName, fCurrentPowerState, fHeadNotePowerState,
5292 NS_TO_MS(nsec));
5293
5294 // Pretend driver has acked.
5295 fHeadNotePendingAcks--;
5296 }
5297 }
5298 nextObject = fInterestedDrivers->nextInList(nextObject);
5299 }
5300
5301 // is that the last?
5302 if ( fHeadNotePendingAcks == 0 )
5303 {
5304 // yes, we can continue
5305 done = true;
5306 } else {
5307 // no, set timer again
5308 start_ack_timer();
5309 }
5310 }
5311 break;
5312
5313 // TODO: aggreggate this
5314 case kIOPM_OurChangeTellClientsPowerDown:
5315 case kIOPM_OurChangeTellUserPMPolicyPowerDown:
5316 case kIOPM_OurChangeTellPriorityClientsPowerDown:
5317 case kIOPM_OurChangeNotifyInterestedDriversWillChange:
5318 case kIOPM_ParentChangeTellPriorityClientsPowerDown:
5319 case kIOPM_ParentChangeNotifyInterestedDriversWillChange:
5320 case kIOPM_SyncTellClientsPowerDown:
5321 case kIOPM_SyncTellPriorityClientsPowerDown:
5322 case kIOPM_SyncNotifyWillChange:
5323 case kIOPM_TellCapabilityChangeDone:
5324 // apps didn't respond in time
5325 cleanClientResponses(true);
5326 OUR_PMLog(kPMLogClientTardy, 0, 1);
5327 // tardy equates to approval
5328 done = true;
5329 break;
5330
5331 default:
5332 PM_LOG1("%s: unexpected ack timer tick (state = %d)\n",
5333 getName(), fMachineState);
5334 break;
5335 }
5336 return done;
5337}
5338
5339//*********************************************************************************
5340// [private] start_watchdog_timer
5341//*********************************************************************************
5342void IOService::start_watchdog_timer( void )
5343{
5344 AbsoluteTime deadline;
5345 boolean_t pending;
5346 static int timeout = -1;
5347
5348 if (!fWatchdogTimer || (kIOSleepWakeWdogOff & gIOKitDebug))
5349 return;
5350
5351 if (thread_call_isactive(fWatchdogTimer)) return;
5352 if (timeout == -1) {
5353 PE_parse_boot_argn("swd_timeout", &timeout, sizeof(timeout));
5354 }
5355 if (timeout < 60) {
5356 timeout = WATCHDOG_TIMER_PERIOD;
5357 }
5358
5359 clock_interval_to_deadline(timeout, kSecondScale, &deadline);
5360
5361 retain();
5362 pending = thread_call_enter_delayed(fWatchdogTimer, deadline);
5363 if (pending) release();
5364
5365}
5366
5367//*********************************************************************************
5368// [private] stop_watchdog_timer
5369// Returns true if watchdog was enabled and stopped now
5370//*********************************************************************************
5371
5372bool IOService::stop_watchdog_timer( void )
5373{
5374 boolean_t pending;
5375
5376 if (!fWatchdogTimer || (kIOSleepWakeWdogOff & gIOKitDebug))
5377 return false;
5378
5379 pending = thread_call_cancel(fWatchdogTimer);
5380 if (pending) release();
5381
5382 return pending;
5383}
5384
5385//*********************************************************************************
5386// reset_watchdog_timer
5387//*********************************************************************************
5388
5389void IOService::reset_watchdog_timer( void )
5390{
5391 if (stop_watchdog_timer())
5392 start_watchdog_timer();
5393}
5394
5395
5396//*********************************************************************************
5397// [static] watchdog_timer_expired
5398//
5399// Inside PM work loop's gate.
5400//*********************************************************************************
5401
5402void
5403IOService::watchdog_timer_expired( thread_call_param_t arg0, thread_call_param_t arg1 )
5404{
5405 IOService * me = (IOService *) arg0;
5406
5407
5408 gIOPMWatchDogThread = current_thread();
5409 getPMRootDomain()->sleepWakeDebugTrig(true);
5410 gIOPMWatchDogThread = 0;
5411 thread_call_free(me->fWatchdogTimer);
5412 me->fWatchdogTimer = 0;
5413
5414 return ;
5415}
5416
5417
5418//*********************************************************************************
5419// [private] start_ack_timer
5420//*********************************************************************************
5421
5422void IOService::start_ack_timer( void )
5423{
5424 start_ack_timer( ACK_TIMER_PERIOD, kNanosecondScale );
5425}
5426
5427void IOService::start_ack_timer ( UInt32 interval, UInt32 scale )
5428{
5429 AbsoluteTime deadline;
5430 boolean_t pending;
5431
5432 clock_interval_to_deadline(interval, scale, &deadline);
5433
5434 retain();
5435 pending = thread_call_enter_delayed(fAckTimer, deadline);
5436 if (pending) release();
5437
5438 // Stop watchdog if ack is delayed by more than a sec
5439 if (interval * scale > kSecondScale) {
5440 stop_watchdog_timer();
5441 }
5442}
5443
5444//*********************************************************************************
5445// [private] stop_ack_timer
5446//*********************************************************************************
5447
5448void IOService::stop_ack_timer( void )
5449{
5450 boolean_t pending;
5451
5452 pending = thread_call_cancel(fAckTimer);
5453 if (pending) release();
5454
5455 start_watchdog_timer();
5456}
5457
5458//*********************************************************************************
5459// [static] actionAckTimerExpired
5460//
5461// Inside PM work loop's gate.
5462//*********************************************************************************
5463
5464IOReturn
5465IOService::actionAckTimerExpired(
5466 OSObject * target,
5467 void * arg0, void * arg1,
5468 void * arg2, void * arg3 )
5469{
5470 IOService * me = (IOService *) target;
5471 bool done;
5472
5473 // done will be true if the timer tick unblocks the machine state,
5474 // otherwise no need to signal the work loop.
5475
5476 done = me->ackTimerTick();
5477 if (done && gIOPMWorkQueue)
5478 {
5479 gIOPMWorkQueue->signalWorkAvailable();
5480 me->start_watchdog_timer();
5481 }
5482
5483 return kIOReturnSuccess;
5484}
5485
5486//*********************************************************************************
5487// ack_timer_expired
5488//
5489// Thread call function. Holds a retain while the callout is in flight.
5490//*********************************************************************************
5491
5492void
5493IOService::ack_timer_expired( thread_call_param_t arg0, thread_call_param_t arg1 )
5494{
5495 IOService * me = (IOService *) arg0;
5496
5497 if (gIOPMWorkLoop)
5498 {
5499 gIOPMWorkLoop->runAction(&actionAckTimerExpired, me);
5500 }
5501 me->release();
5502}
5503
5504//*********************************************************************************
5505// [private] start_spindump_timer
5506//*********************************************************************************
5507
5508void IOService::start_spindump_timer( const char * delay_type )
5509{
5510 AbsoluteTime deadline;
5511 boolean_t pending;
5512
5513 if (!fSpinDumpTimer || !(kIOKextSpinDump & gIOKitDebug))
5514 return;
5515
5516 if (gIOSpinDumpKextName[0] == '\0' &&
5517 !(PE_parse_boot_argn("swd_kext_name", &gIOSpinDumpKextName,
5518 sizeof(gIOSpinDumpKextName))))
5519 {
5520 return;
5521 }
5522
5523 if (strncmp(gIOSpinDumpKextName, fName, sizeof(gIOSpinDumpKextName)) != 0)
5524 return;
5525
5526 if (gIOSpinDumpDelayType[0] == '\0' &&
5527 !(PE_parse_boot_argn("swd_delay_type", &gIOSpinDumpDelayType,
5528 sizeof(gIOSpinDumpDelayType))))
5529 {
5530 strncpy(gIOSpinDumpDelayType, "SetState", sizeof(gIOSpinDumpDelayType));
5531 }
5532
5533 if (strncmp(delay_type, gIOSpinDumpDelayType, sizeof(gIOSpinDumpDelayType)) != 0)
5534 return;
5535
5536 if (gIOSpinDumpDelayDuration == 0 &&
5537 !(PE_parse_boot_argn("swd_delay_duration", &gIOSpinDumpDelayDuration,
5538 sizeof(gIOSpinDumpDelayDuration))))
5539 {
5540 gIOSpinDumpDelayDuration = 300;
5541 }
5542
5543 clock_interval_to_deadline(gIOSpinDumpDelayDuration, kMillisecondScale, &deadline);
5544
5545 retain();
5546 pending = thread_call_enter_delayed(fSpinDumpTimer, deadline);
5547 if (pending) release();
5548}
5549
5550//*********************************************************************************
5551// [private] stop_spindump_timer
5552//*********************************************************************************
5553
5554void IOService::stop_spindump_timer( void )
5555{
5556 boolean_t pending;
5557
5558 if (!fSpinDumpTimer || !(kIOKextSpinDump & gIOKitDebug))
5559 return;
5560
5561 pending = thread_call_cancel(fSpinDumpTimer);
5562 if (pending) release();
5563}
5564
5565
5566//*********************************************************************************
5567// [static] actionSpinDumpTimerExpired
5568//
5569// Inside PM work loop's gate.
5570//*********************************************************************************
5571
5572IOReturn
5573IOService::actionSpinDumpTimerExpired(
5574 OSObject * target,
5575 void * arg0, void * arg1,
5576 void * arg2, void * arg3 )
5577{
5578 getPMRootDomain()->takeStackshot(false, false, true);
5579
5580 return kIOReturnSuccess;
5581}
5582
5583//*********************************************************************************
5584// spindump_timer_expired
5585//
5586// Thread call function. Holds a retain while the callout is in flight.
5587//*********************************************************************************
5588
5589void
5590IOService::spindump_timer_expired( thread_call_param_t arg0, thread_call_param_t arg1 )
5591{
5592 IOService * me = (IOService *) arg0;
5593
5594 if (gIOPMWorkLoop)
5595 {
5596 gIOPMWorkLoop->runAction(&actionSpinDumpTimerExpired, me);
5597 }
5598 me->release();
5599}
5600
5601// MARK: -
5602// MARK: Client Messaging
5603
5604//*********************************************************************************
5605// [private] tellSystemCapabilityChange
5606//*********************************************************************************
5607
5608void IOService::tellSystemCapabilityChange( uint32_t nextMS )
5609{
5610 MS_PUSH( nextMS );
5611 fMachineState = kIOPM_TellCapabilityChangeDone;
5612 fOutOfBandMessage = kIOMessageSystemCapabilityChange;
5613
5614 if (fIsPreChange)
5615 {
5616 // Notify app first on pre-change.
5617 fOutOfBandParameter = kNotifyCapabilityChangeApps;
5618 }
5619 else
5620 {
5621 // Notify kernel clients first on post-change.
5622 fOutOfBandParameter = kNotifyCapabilityChangePriority;
5623 }
5624
5625 tellClientsWithResponse( fOutOfBandMessage );
5626}
5627
5628//*********************************************************************************
5629// [public] askChangeDown
5630//
5631// Ask registered applications and kernel clients if we can change to a lower
5632// power state.
5633//
5634// Subclass can override this to send a different message type. Parameter is
5635// the destination state number.
5636//
5637// Return true if we don't have to wait for acknowledgements
5638//*********************************************************************************
5639
5640bool IOService::askChangeDown( unsigned long stateNum )
5641{
5642 return tellClientsWithResponse( kIOMessageCanDevicePowerOff );
5643}
5644
5645//*********************************************************************************
5646// [private] tellChangeDown1
5647//
5648// Notify registered applications and kernel clients that we are definitely
5649// dropping power.
5650//
5651// Return true if we don't have to wait for acknowledgements
5652//*********************************************************************************
5653
5654bool IOService::tellChangeDown1( unsigned long stateNum )
5655{
5656 fOutOfBandParameter = kNotifyApps;
5657 return tellChangeDown(stateNum);
5658}
5659
5660//*********************************************************************************
5661// [private] tellChangeDown2
5662//
5663// Notify priority clients that we are definitely dropping power.
5664//
5665// Return true if we don't have to wait for acknowledgements
5666//*********************************************************************************
5667
5668bool IOService::tellChangeDown2( unsigned long stateNum )
5669{
5670 fOutOfBandParameter = kNotifyPriority;
5671 return tellChangeDown(stateNum);
5672}
5673
5674//*********************************************************************************
5675// [public] tellChangeDown
5676//
5677// Notify registered applications and kernel clients that we are definitely
5678// dropping power.
5679//
5680// Subclass can override this to send a different message type. Parameter is
5681// the destination state number.
5682//
5683// Return true if we don't have to wait for acknowledgements
5684//*********************************************************************************
5685
5686bool IOService::tellChangeDown( unsigned long stateNum )
5687{
5688 return tellClientsWithResponse( kIOMessageDeviceWillPowerOff );
5689}
5690
5691//*********************************************************************************
5692// cleanClientResponses
5693//
5694//*********************************************************************************
5695
5696static void logAppTimeouts( OSObject * object, void * arg )
5697{
5698 IOPMInterestContext * context = (IOPMInterestContext *) arg;
5699 OSObject * flag;
5700 unsigned int clientIndex;
5701 int pid = -1;
5702 char name[128];
5703
5704 if (OSDynamicCast(_IOServiceInterestNotifier, object))
5705 {
5706 // Discover the 'counter' value or index assigned to this client
5707 // when it was notified, by searching for the array index of the
5708 // client in an array holding the cached interested clients.
5709
5710 clientIndex = context->notifyClients->getNextIndexOfObject(object, 0);
5711
5712 if ((clientIndex != (unsigned int) -1) &&
5713 (flag = context->responseArray->getObject(clientIndex)) &&
5714 (flag != kOSBooleanTrue))
5715 {
5716 OSNumber *clientID = copyClientIDForNotification(object, context);
5717
5718 name[0] = '\0';
5719 if (clientID) {
5720 pid = clientID->unsigned32BitValue();
5721 proc_name(pid, name, sizeof(name));
5722 clientID->release();
5723 }
5724
5725 PM_ERROR(context->errorLog, pid, name);
5726
5727 // TODO: record message type if possible
5728 IOService::getPMRootDomain()->pmStatsRecordApplicationResponse(
5729 gIOPMStatsApplicationResponseTimedOut,
5730 name, 0, (30*1000), pid, object);
5731
5732 }
5733 }
5734}
5735
5736void IOService::cleanClientResponses( bool logErrors )
5737{
5738 if (logErrors && fResponseArray)
5739 {
5740 switch ( fOutOfBandParameter ) {
5741 case kNotifyApps:
5742 case kNotifyCapabilityChangeApps:
5743 if (fNotifyClientArray)
5744 {
5745 IOPMInterestContext context;
5746
5747 context.responseArray = fResponseArray;
5748 context.notifyClients = fNotifyClientArray;
5749 context.serialNumber = fSerialNumber;
5750 context.messageType = kIOMessageCopyClientID;
5751 context.notifyType = kNotifyApps;
5752 context.isPreChange = fIsPreChange;
5753 context.enableTracing = false;
5754 context.us = this;
5755 context.maxTimeRequested = 0;
5756 context.stateNumber = fHeadNotePowerState;
5757 context.stateFlags = fHeadNotePowerArrayEntry->capabilityFlags;
5758 context.changeFlags = fHeadNoteChangeFlags;
5759 context.errorLog = "PM notification timeout (pid %d, %s)\n";
5760
5761 applyToInterested(gIOAppPowerStateInterest, logAppTimeouts, (void *) &context);
5762 }
5763 break;
5764
5765 default:
5766 // kNotifyPriority, kNotifyCapabilityChangePriority
5767 // TODO: identify the priority client that has not acked
5768 PM_ERROR("PM priority notification timeout\n");
5769 if (gIOKitDebug & kIOLogDebugPower)
5770 {
5771 panic("PM priority notification timeout");
5772 }
5773 break;
5774 }
5775 }
5776
5777 if (fResponseArray)
5778 {
5779 fResponseArray->release();
5780 fResponseArray = NULL;
5781 }
5782 if (fNotifyClientArray)
5783 {
5784 fNotifyClientArray->release();
5785 fNotifyClientArray = NULL;
5786 }
5787}
5788
5789//*********************************************************************************
5790// [protected] tellClientsWithResponse
5791//
5792// Notify registered applications and kernel clients that we are definitely
5793// dropping power.
5794//
5795// Return true if we don't have to wait for acknowledgements
5796//*********************************************************************************
5797
5798bool IOService::tellClientsWithResponse( int messageType )
5799{
5800 IOPMInterestContext context;
5801 bool isRootDomain = IS_ROOT_DOMAIN;
5802 uint32_t maxTimeOut = kMaxTimeRequested;
5803
5804 PM_ASSERT_IN_GATE();
5805 assert( fResponseArray == NULL );
5806 assert( fNotifyClientArray == NULL );
5807
5808 if(messageType == (int)kIOPMMessageLastCallBeforeSleep)
5809 RD_LOG("tellClientsWithResponse( kIOPMMessageLastCallBeforeSleep, %d )\n",
5810 fOutOfBandParameter);
5811 else
5812 RD_LOG("tellClientsWithResponse( %s, %d )\n",
5813 getIOMessageString(messageType), fOutOfBandParameter);
5814
5815 fResponseArray = OSArray::withCapacity( 1 );
5816 if (!fResponseArray)
5817 goto exit;
5818
5819 fResponseArray->setCapacityIncrement(8);
5820 if (++fSerialNumber == 0)
5821 fSerialNumber++;
5822
5823 context.responseArray = fResponseArray;
5824 context.notifyClients = 0;
5825 context.serialNumber = fSerialNumber;
5826 context.messageType = messageType;
5827 context.notifyType = fOutOfBandParameter;
5828 context.isPreChange = fIsPreChange;
5829 context.enableTracing = false;
5830 context.us = this;
5831 context.maxTimeRequested = 0;
5832 context.stateNumber = fHeadNotePowerState;
5833 context.stateFlags = fHeadNotePowerArrayEntry->capabilityFlags;
5834 context.changeFlags = fHeadNoteChangeFlags;
5835 context.messageFilter = (isRootDomain) ?
5836 OSMemberFunctionCast(
5837 IOPMMessageFilter,
5838 this,
5839 &IOPMrootDomain::systemMessageFilter) : 0;
5840
5841 switch ( fOutOfBandParameter ) {
5842 case kNotifyApps:
5843 applyToInterested( gIOAppPowerStateInterest,
5844 pmTellAppWithResponse, (void *) &context );
5845
5846 if (isRootDomain &&
5847 (fMachineState != kIOPM_OurChangeTellClientsPowerDown) &&
5848 (fMachineState != kIOPM_SyncTellClientsPowerDown) &&
5849 (context.messageType != kIOPMMessageLastCallBeforeSleep))
5850 {
5851 // Notify capability app for tellChangeDown1()
5852 // but not for askChangeDown().
5853 context.notifyType = kNotifyCapabilityChangeApps;
5854 context.messageType = kIOMessageSystemCapabilityChange;
5855 applyToInterested( gIOAppPowerStateInterest,
5856 pmTellCapabilityAppWithResponse, (void *) &context );
5857 context.notifyType = fOutOfBandParameter;
5858 context.messageType = messageType;
5859 }
5860 if(context.messageType == kIOMessageCanSystemSleep)
5861 {
5862 maxTimeOut = kCanSleepMaxTimeReq;
5863 if(gCanSleepTimeout)
5864 {
5865 maxTimeOut = (gCanSleepTimeout*us_per_s);
5866 }
5867 }
5868 context.maxTimeRequested = maxTimeOut;
5869 applyToInterested( gIOGeneralInterest,
5870 pmTellClientWithResponse, (void *) &context );
5871
5872 fNotifyClientArray = context.notifyClients;
5873 break;
5874
5875 case kNotifyPriority:
5876 context.enableTracing = isRootDomain;
5877 applyToInterested( gIOPriorityPowerStateInterest,
5878 pmTellClientWithResponse, (void *) &context );
5879
5880 if (isRootDomain)
5881 {
5882 // Notify capability clients for tellChangeDown2().
5883 context.notifyType = kNotifyCapabilityChangePriority;
5884 context.messageType = kIOMessageSystemCapabilityChange;
5885 applyToInterested( gIOPriorityPowerStateInterest,
5886 pmTellCapabilityClientWithResponse, (void *) &context );
5887 }
5888 break;
5889
5890 case kNotifyCapabilityChangeApps:
5891 applyToInterested( gIOAppPowerStateInterest,
5892 pmTellCapabilityAppWithResponse, (void *) &context );
5893 fNotifyClientArray = context.notifyClients;
5894 if(context.messageType == kIOMessageCanSystemSleep)
5895 {
5896 maxTimeOut = kCanSleepMaxTimeReq;
5897 if(gCanSleepTimeout)
5898 {
5899 maxTimeOut = (gCanSleepTimeout*us_per_s);
5900 }
5901 }
5902 context.maxTimeRequested = maxTimeOut;
5903 break;
5904
5905 case kNotifyCapabilityChangePriority:
5906 applyToInterested( gIOPriorityPowerStateInterest,
5907 pmTellCapabilityClientWithResponse, (void *) &context );
5908 break;
5909 }
5910
5911 // do we have to wait for somebody?
5912 if ( !checkForDone() )
5913 {
5914 OUR_PMLog(kPMLogStartAckTimer, context.maxTimeRequested, 0);
5915 if (context.enableTracing)
5916 getPMRootDomain()->traceDetail( context.maxTimeRequested / 1000 );
5917 start_ack_timer( context.maxTimeRequested / 1000, kMillisecondScale );
5918 return false;
5919 }
5920
5921exit:
5922 // everybody responded
5923 if (fResponseArray)
5924 {
5925 fResponseArray->release();
5926 fResponseArray = NULL;
5927 }
5928 if (fNotifyClientArray)
5929 {
5930 fNotifyClientArray->release();
5931 fNotifyClientArray = NULL;
5932 }
5933
5934 return true;
5935}
5936
5937//*********************************************************************************
5938// [static private] pmTellAppWithResponse
5939//
5940// We send a message to an application, and we expect a response, so we compute a
5941// cookie we can identify the response with.
5942//*********************************************************************************
5943
5944void IOService::pmTellAppWithResponse( OSObject * object, void * arg )
5945{
5946 IOPMInterestContext * context = (IOPMInterestContext *) arg;
5947 IOServicePM * pwrMgt = context->us->pwrMgt;
5948 uint32_t msgIndex, msgRef, msgType;
5949 OSNumber *clientID = NULL;
5950 proc_t proc = NULL;
5951 boolean_t proc_suspended = FALSE;
5952 OSObject * waitForReply = kOSBooleanTrue;
5953#if LOG_APP_RESPONSE_TIMES
5954 AbsoluteTime now;
5955#endif
5956
5957 if (!OSDynamicCast(_IOServiceInterestNotifier, object))
5958 return;
5959
5960 if (context->us == getPMRootDomain())
5961 {
5962 if ((clientID = copyClientIDForNotification(object, context)))
5963 {
5964 uint32_t clientPID = clientID->unsigned32BitValue();
5965 clientID->release();
5966 proc = proc_find(clientPID);
5967
5968 if (proc)
5969 {
5970 proc_suspended = get_task_pidsuspended((task_t) proc->task);
5971 proc_rele(proc);
5972
5973 if (proc_suspended)
5974 {
5975 logClientIDForNotification(object, context, "PMTellAppWithResponse - Suspended");
5976 return;
5977 }
5978 }
5979 }
5980 }
5981
5982 if (context->messageFilter &&
5983 !context->messageFilter(context->us, object, context, 0, &waitForReply))
5984 {
5985 if (kIOLogDebugPower & gIOKitDebug)
5986 {
5987 logClientIDForNotification(object, context, "DROP App");
5988 }
5989 return;
5990 }
5991
5992 // Create client array (for tracking purposes) only if the service
5993 // has app clients. Usually only root domain does.
5994 if (0 == context->notifyClients)
5995 context->notifyClients = OSArray::withCapacity( 32 );
5996
5997 msgType = context->messageType;
5998 msgIndex = context->responseArray->getCount();
5999 msgRef = ((context->serialNumber & 0xFFFF) << 16) + (msgIndex & 0xFFFF);
6000
6001 OUR_PMLog(kPMLogAppNotify, msgType, msgRef);
6002 if (kIOLogDebugPower & gIOKitDebug)
6003 {
6004 logClientIDForNotification(object, context, "MESG App");
6005 }
6006
6007 if (waitForReply == kOSBooleanTrue)
6008 {
6009#if LOG_APP_RESPONSE_TIMES
6010 OSNumber * num;
6011 clock_get_uptime(&now);
6012 num = OSNumber::withNumber(AbsoluteTime_to_scalar(&now), sizeof(uint64_t) * 8);
6013 if (num)
6014 {
6015 context->responseArray->setObject(msgIndex, num);
6016 num->release();
6017 }
6018 else
6019#endif
6020 context->responseArray->setObject(msgIndex, kOSBooleanFalse);
6021 }
6022 else
6023 {
6024 context->responseArray->setObject(msgIndex, kOSBooleanTrue);
6025 if (kIOLogDebugPower & gIOKitDebug)
6026 {
6027 logClientIDForNotification(object, context, "App response ignored");
6028 }
6029 }
6030
6031 if (context->notifyClients)
6032 context->notifyClients->setObject(msgIndex, object);
6033
6034 context->us->messageClient(msgType, object, (void *)(uintptr_t) msgRef);
6035}
6036
6037//*********************************************************************************
6038// [static private] pmTellClientWithResponse
6039//
6040// We send a message to an in-kernel client, and we expect a response,
6041// so we compute a cookie we can identify the response with.
6042//*********************************************************************************
6043
6044void IOService::pmTellClientWithResponse( OSObject * object, void * arg )
6045{
6046 IOPowerStateChangeNotification notify;
6047 IOPMInterestContext * context = (IOPMInterestContext *) arg;
6048 OSObject * replied = kOSBooleanTrue;
6049 _IOServiceInterestNotifier * notifier;
6050 uint32_t msgIndex, msgRef, msgType;
6051 IOReturn retCode;
6052
6053 if (context->messageFilter &&
6054 !context->messageFilter(context->us, object, context, 0, 0))
6055 {
6056 if ((kIOLogDebugPower & gIOKitDebug) &&
6057 (OSDynamicCast(_IOServiceInterestNotifier, object)))
6058 {
6059 _IOServiceInterestNotifier *n = (_IOServiceInterestNotifier *) object;
6060 PM_LOG("%s DROP Client %s, notifier %p, handler %p\n",
6061 context->us->getName(),
6062 getIOMessageString(context->messageType),
6063 OBFUSCATE(object), OBFUSCATE(n->handler));
6064 }
6065 return;
6066 }
6067
6068 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
6069 msgType = context->messageType;
6070 msgIndex = context->responseArray->getCount();
6071 msgRef = ((context->serialNumber & 0xFFFF) << 16) + (msgIndex & 0xFFFF);
6072
6073 IOServicePM * pwrMgt = context->us->pwrMgt;
6074 if (gIOKitDebug & kIOLogPower) {
6075 OUR_PMLog(kPMLogClientNotify, msgRef, msgType);
6076 if (OSDynamicCast(IOService, object)) {
6077 const char *who = ((IOService *) object)->getName();
6078 gPlatform->PMLog(who, kPMLogClientNotify, (uintptr_t) object, 0);
6079 }
6080 else if (notifier) {
6081 OUR_PMLog(kPMLogClientNotify, (uintptr_t) notifier->handler, 0);
6082 }
6083 }
6084 if ((kIOLogDebugPower & gIOKitDebug) && notifier)
6085 {
6086 PM_LOG("%s MESG Client %s, notifier %p, handler %p\n",
6087 context->us->getName(),
6088 getIOMessageString(msgType),
6089 OBFUSCATE(object), OBFUSCATE(notifier->handler));
6090 }
6091
6092 notify.powerRef = (void *)(uintptr_t) msgRef;
6093 notify.returnValue = 0;
6094 notify.stateNumber = context->stateNumber;
6095 notify.stateFlags = context->stateFlags;
6096
6097 if (context->enableTracing && (notifier != 0))
6098 {
6099 uint32_t detail = ((msgIndex & 0xff) << 24) |
6100 ((msgType & 0xfff) << 12) |
6101 (((uintptr_t) notifier->handler) & 0xfff);
6102 getPMRootDomain()->traceDetail( detail );
6103 }
6104
6105 retCode = context->us->messageClient(msgType, object, (void *) &notify, sizeof(notify));
6106
6107 if (kIOReturnSuccess == retCode)
6108 {
6109 if (0 == notify.returnValue) {
6110 OUR_PMLog(kPMLogClientAcknowledge, msgRef, (uintptr_t) object);
6111 } else {
6112 replied = kOSBooleanFalse;
6113 if ( notify.returnValue > context->maxTimeRequested )
6114 {
6115 if (notify.returnValue > kPriorityClientMaxWait)
6116 {
6117 context->maxTimeRequested = kPriorityClientMaxWait;
6118 PM_ERROR("%s: client %p returned %llu for %s\n",
6119 context->us->getName(),
6120 notifier ? (void *) OBFUSCATE(notifier->handler) : OBFUSCATE(object),
6121 (uint64_t) notify.returnValue,
6122 getIOMessageString(msgType));
6123 }
6124 else
6125 context->maxTimeRequested = notify.returnValue;
6126 }
6127 }
6128 } else {
6129 // not a client of ours
6130 // so we won't be waiting for response
6131 OUR_PMLog(kPMLogClientAcknowledge, msgRef, 0);
6132 }
6133
6134 context->responseArray->setObject(msgIndex, replied);
6135}
6136
6137//*********************************************************************************
6138// [static private] pmTellCapabilityAppWithResponse
6139//*********************************************************************************
6140
6141void IOService::pmTellCapabilityAppWithResponse( OSObject * object, void * arg )
6142{
6143 IOPMSystemCapabilityChangeParameters msgArg;
6144 IOPMInterestContext * context = (IOPMInterestContext *) arg;
6145 OSObject * replied = kOSBooleanTrue;
6146 IOServicePM * pwrMgt = context->us->pwrMgt;
6147 uint32_t msgIndex, msgRef, msgType;
6148#if LOG_APP_RESPONSE_TIMES
6149 AbsoluteTime now;
6150#endif
6151
6152 if (!OSDynamicCast(_IOServiceInterestNotifier, object))
6153 return;
6154
6155 memset(&msgArg, 0, sizeof(msgArg));
6156 if (context->messageFilter &&
6157 !context->messageFilter(context->us, object, context, &msgArg, &replied))
6158 {
6159 return;
6160 }
6161
6162 // Create client array (for tracking purposes) only if the service
6163 // has app clients. Usually only root domain does.
6164 if (0 == context->notifyClients)
6165 context->notifyClients = OSArray::withCapacity( 32 );
6166
6167 msgType = context->messageType;
6168 msgIndex = context->responseArray->getCount();
6169 msgRef = ((context->serialNumber & 0xFFFF) << 16) + (msgIndex & 0xFFFF);
6170
6171 OUR_PMLog(kPMLogAppNotify, msgType, msgRef);
6172 if (kIOLogDebugPower & gIOKitDebug)
6173 {
6174 // Log client pid/name and client array index.
6175 OSNumber * clientID = NULL;
6176 OSString * clientIDString = NULL;;
6177 context->us->messageClient(kIOMessageCopyClientID, object, &clientID);
6178 if (clientID) {
6179 clientIDString = IOCopyLogNameForPID(clientID->unsigned32BitValue());
6180 }
6181
6182 PM_LOG("%s MESG App(%u) %s, wait %u, %s\n",
6183 context->us->getName(),
6184 msgIndex, getIOMessageString(msgType),
6185 (replied != kOSBooleanTrue),
6186 clientIDString ? clientIDString->getCStringNoCopy() : "");
6187 if (clientID) clientID->release();
6188 if (clientIDString) clientIDString->release();
6189 }
6190
6191 msgArg.notifyRef = msgRef;
6192 msgArg.maxWaitForReply = 0;
6193
6194 if (replied == kOSBooleanTrue)
6195 {
6196 msgArg.notifyRef = 0;
6197 context->responseArray->setObject(msgIndex, kOSBooleanTrue);
6198 if (context->notifyClients)
6199 context->notifyClients->setObject(msgIndex, kOSBooleanTrue);
6200 }
6201 else
6202 {
6203#if LOG_APP_RESPONSE_TIMES
6204 OSNumber * num;
6205 clock_get_uptime(&now);
6206 num = OSNumber::withNumber(AbsoluteTime_to_scalar(&now), sizeof(uint64_t) * 8);
6207 if (num)
6208 {
6209 context->responseArray->setObject(msgIndex, num);
6210 num->release();
6211 }
6212 else
6213#endif
6214 context->responseArray->setObject(msgIndex, kOSBooleanFalse);
6215
6216 if (context->notifyClients)
6217 context->notifyClients->setObject(msgIndex, object);
6218 }
6219
6220 context->us->messageClient(msgType, object, (void *) &msgArg, sizeof(msgArg));
6221}
6222
6223//*********************************************************************************
6224// [static private] pmTellCapabilityClientWithResponse
6225//*********************************************************************************
6226
6227void IOService::pmTellCapabilityClientWithResponse(
6228 OSObject * object, void * arg )
6229{
6230 IOPMSystemCapabilityChangeParameters msgArg;
6231 IOPMInterestContext * context = (IOPMInterestContext *) arg;
6232 OSObject * replied = kOSBooleanTrue;
6233 _IOServiceInterestNotifier * notifier;
6234 uint32_t msgIndex, msgRef, msgType;
6235 IOReturn retCode;
6236
6237 memset(&msgArg, 0, sizeof(msgArg));
6238 if (context->messageFilter &&
6239 !context->messageFilter(context->us, object, context, &msgArg, 0))
6240 {
6241 if ((kIOLogDebugPower & gIOKitDebug) &&
6242 (OSDynamicCast(_IOServiceInterestNotifier, object)))
6243 {
6244 _IOServiceInterestNotifier *n = (_IOServiceInterestNotifier *) object;
6245 PM_LOG("%s DROP Client %s, notifier %p, handler %p\n",
6246 context->us->getName(),
6247 getIOMessageString(context->messageType),
6248 OBFUSCATE(object), OBFUSCATE(n->handler));
6249 }
6250 return;
6251 }
6252
6253 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
6254 msgType = context->messageType;
6255 msgIndex = context->responseArray->getCount();
6256 msgRef = ((context->serialNumber & 0xFFFF) << 16) + (msgIndex & 0xFFFF);
6257
6258 IOServicePM * pwrMgt = context->us->pwrMgt;
6259 if (gIOKitDebug & kIOLogPower) {
6260 OUR_PMLog(kPMLogClientNotify, msgRef, msgType);
6261 if (OSDynamicCast(IOService, object)) {
6262 const char *who = ((IOService *) object)->getName();
6263 gPlatform->PMLog(who, kPMLogClientNotify, (uintptr_t) object, 0);
6264 }
6265 else if (notifier) {
6266 OUR_PMLog(kPMLogClientNotify, (uintptr_t) notifier->handler, 0);
6267 }
6268 }
6269 if ((kIOLogDebugPower & gIOKitDebug) && notifier)
6270 {
6271 PM_LOG("%s MESG Client %s, notifier %p, handler %p\n",
6272 context->us->getName(),
6273 getIOMessageString(msgType),
6274 OBFUSCATE(object), OBFUSCATE(notifier->handler));
6275 }
6276
6277 msgArg.notifyRef = msgRef;
6278 msgArg.maxWaitForReply = 0;
6279
6280 if (context->enableTracing && (notifier != 0))
6281 {
6282 uint32_t detail = ((msgIndex & 0xff) << 24) |
6283 ((msgType & 0xfff) << 12) |
6284 (((uintptr_t) notifier->handler) & 0xfff);
6285 getPMRootDomain()->traceDetail( detail );
6286 }
6287
6288 retCode = context->us->messageClient(
6289 msgType, object, (void *) &msgArg, sizeof(msgArg));
6290
6291 if ( kIOReturnSuccess == retCode )
6292 {
6293 if ( 0 == msgArg.maxWaitForReply )
6294 {
6295 // client doesn't want time to respond
6296 OUR_PMLog(kPMLogClientAcknowledge, msgRef, (uintptr_t) object);
6297 }
6298 else
6299 {
6300 replied = kOSBooleanFalse;
6301 if ( msgArg.maxWaitForReply > context->maxTimeRequested )
6302 {
6303 if (msgArg.maxWaitForReply > kCapabilityClientMaxWait)
6304 {
6305 context->maxTimeRequested = kCapabilityClientMaxWait;
6306 PM_ERROR("%s: client %p returned %u for %s\n",
6307 context->us->getName(),
6308 notifier ? (void *) OBFUSCATE(notifier->handler) : OBFUSCATE(object),
6309 msgArg.maxWaitForReply,
6310 getIOMessageString(msgType));
6311 }
6312 else
6313 context->maxTimeRequested = msgArg.maxWaitForReply;
6314 }
6315 }
6316 }
6317 else
6318 {
6319 // not a client of ours
6320 // so we won't be waiting for response
6321 OUR_PMLog(kPMLogClientAcknowledge, msgRef, 0);
6322 }
6323
6324 context->responseArray->setObject(msgIndex, replied);
6325}
6326
6327//*********************************************************************************
6328// [public] tellNoChangeDown
6329//
6330// Notify registered applications and kernel clients that we are not
6331// dropping power.
6332//
6333// Subclass can override this to send a different message type. Parameter is
6334// the aborted destination state number.
6335//*********************************************************************************
6336
6337void IOService::tellNoChangeDown( unsigned long )
6338{
6339 return tellClients( kIOMessageDeviceWillNotPowerOff );
6340}
6341
6342//*********************************************************************************
6343// [public] tellChangeUp
6344//
6345// Notify registered applications and kernel clients that we are raising power.
6346//
6347// Subclass can override this to send a different message type. Parameter is
6348// the aborted destination state number.
6349//*********************************************************************************
6350
6351void IOService::tellChangeUp( unsigned long )
6352{
6353 return tellClients( kIOMessageDeviceHasPoweredOn );
6354}
6355
6356//*********************************************************************************
6357// [protected] tellClients
6358//
6359// Notify registered applications and kernel clients of something.
6360//*********************************************************************************
6361
6362void IOService::tellClients( int messageType )
6363{
6364 IOPMInterestContext context;
6365
6366 RD_LOG("tellClients( %s )\n", getIOMessageString(messageType));
6367
6368 memset(&context, 0, sizeof(context));
6369 context.messageType = messageType;
6370 context.isPreChange = fIsPreChange;
6371 context.us = this;
6372 context.stateNumber = fHeadNotePowerState;
6373 context.stateFlags = fHeadNotePowerArrayEntry->capabilityFlags;
6374 context.changeFlags = fHeadNoteChangeFlags;
6375 context.messageFilter = (IS_ROOT_DOMAIN) ?
6376 OSMemberFunctionCast(
6377 IOPMMessageFilter,
6378 this,
6379 &IOPMrootDomain::systemMessageFilter) : 0;
6380
6381 context.notifyType = kNotifyPriority;
6382 applyToInterested( gIOPriorityPowerStateInterest,
6383 tellKernelClientApplier, (void *) &context );
6384
6385 context.notifyType = kNotifyApps;
6386 applyToInterested( gIOAppPowerStateInterest,
6387 tellAppClientApplier, (void *) &context );
6388
6389 applyToInterested( gIOGeneralInterest,
6390 tellKernelClientApplier, (void *) &context );
6391}
6392
6393//*********************************************************************************
6394// [private] tellKernelClientApplier
6395//
6396// Message a kernel client.
6397//*********************************************************************************
6398
6399static void tellKernelClientApplier( OSObject * object, void * arg )
6400{
6401 IOPowerStateChangeNotification notify;
6402 IOPMInterestContext * context = (IOPMInterestContext *) arg;
6403
6404 if (context->messageFilter &&
6405 !context->messageFilter(context->us, object, context, 0, 0))
6406 {
6407 if ((kIOLogDebugPower & gIOKitDebug) &&
6408 (OSDynamicCast(_IOServiceInterestNotifier, object)))
6409 {
6410 _IOServiceInterestNotifier *n = (_IOServiceInterestNotifier *) object;
6411 PM_LOG("%s DROP Client %s, notifier %p, handler %p\n",
6412 context->us->getName(),
6413 IOService::getIOMessageString(context->messageType),
6414 OBFUSCATE(object), OBFUSCATE(n->handler));
6415 }
6416 return;
6417 }
6418
6419 notify.powerRef = (void *) 0;
6420 notify.returnValue = 0;
6421 notify.stateNumber = context->stateNumber;
6422 notify.stateFlags = context->stateFlags;
6423
6424 context->us->messageClient(context->messageType, object, &notify, sizeof(notify));
6425
6426 if ((kIOLogDebugPower & gIOKitDebug) &&
6427 (OSDynamicCast(_IOServiceInterestNotifier, object)))
6428 {
6429 _IOServiceInterestNotifier *n = (_IOServiceInterestNotifier *) object;
6430 PM_LOG("%s MESG Client %s, notifier %p, handler %p\n",
6431 context->us->getName(),
6432 IOService::getIOMessageString(context->messageType),
6433 OBFUSCATE(object), OBFUSCATE(n->handler));
6434 }
6435}
6436
6437static OSNumber * copyClientIDForNotification(
6438 OSObject *object,
6439 IOPMInterestContext *context)
6440{
6441 OSNumber *clientID = NULL;
6442 context->us->messageClient(kIOMessageCopyClientID, object, &clientID);
6443 return clientID;
6444}
6445
6446static void logClientIDForNotification(
6447 OSObject *object,
6448 IOPMInterestContext *context,
6449 const char *logString)
6450{
6451 OSString *logClientID = NULL;
6452 OSNumber *clientID = copyClientIDForNotification(object, context);
6453
6454 if (logString)
6455 {
6456 if (clientID)
6457 logClientID = IOCopyLogNameForPID(clientID->unsigned32BitValue());
6458
6459 PM_LOG("%s %s %s, %s\n",
6460 context->us->getName(), logString,
6461 IOService::getIOMessageString(context->messageType),
6462 logClientID ? logClientID->getCStringNoCopy() : "");
6463
6464 if (logClientID)
6465 logClientID->release();
6466 }
6467
6468 if (clientID)
6469 clientID->release();
6470
6471 return;
6472}
6473
6474static void tellAppClientApplier( OSObject * object, void * arg )
6475{
6476 IOPMInterestContext * context = (IOPMInterestContext *) arg;
6477 OSNumber * clientID = NULL;
6478 proc_t proc = NULL;
6479 boolean_t proc_suspended = FALSE;
6480
6481 if (context->us == IOService::getPMRootDomain())
6482 {
6483 if ((clientID = copyClientIDForNotification(object, context)))
6484 {
6485 uint32_t clientPID = clientID->unsigned32BitValue();
6486 clientID->release();
6487 proc = proc_find(clientPID);
6488
6489 if (proc)
6490 {
6491 proc_suspended = get_task_pidsuspended((task_t) proc->task);
6492 proc_rele(proc);
6493
6494 if (proc_suspended)
6495 {
6496 logClientIDForNotification(object, context, "tellAppClientApplier - Suspended");
6497 return;
6498 }
6499 }
6500 }
6501 }
6502
6503 if (context->messageFilter &&
6504 !context->messageFilter(context->us, object, context, 0, 0))
6505 {
6506 if (kIOLogDebugPower & gIOKitDebug)
6507 {
6508 logClientIDForNotification(object, context, "DROP App");
6509 }
6510 return;
6511 }
6512
6513 if (kIOLogDebugPower & gIOKitDebug)
6514 {
6515 logClientIDForNotification(object, context, "MESG App");
6516 }
6517
6518 context->us->messageClient(context->messageType, object, 0);
6519}
6520
6521//*********************************************************************************
6522// [private] checkForDone
6523//*********************************************************************************
6524
6525bool IOService::checkForDone( void )
6526{
6527 int i = 0;
6528 OSObject * theFlag;
6529
6530 if (fResponseArray == NULL) {
6531 return true;
6532 }
6533
6534 for (i = 0; ; i++) {
6535 theFlag = fResponseArray->getObject(i);
6536
6537 if (NULL == theFlag) {
6538 break;
6539 }
6540
6541 if (kOSBooleanTrue != theFlag) {
6542 return false;
6543 }
6544 }
6545 return true;
6546}
6547
6548//*********************************************************************************
6549// [public] responseValid
6550//*********************************************************************************
6551
6552bool IOService::responseValid( uint32_t refcon, int pid )
6553{
6554 UInt16 serialComponent;
6555 UInt16 ordinalComponent;
6556 OSObject * theFlag;
6557 OSObject *object = 0;
6558
6559 serialComponent = (refcon >> 16) & 0xFFFF;
6560 ordinalComponent = (refcon & 0xFFFF);
6561
6562 if ( serialComponent != fSerialNumber )
6563 {
6564 return false;
6565 }
6566
6567 if ( fResponseArray == NULL )
6568 {
6569 return false;
6570 }
6571
6572 theFlag = fResponseArray->getObject(ordinalComponent);
6573
6574 if ( theFlag == 0 )
6575 {
6576 return false;
6577 }
6578
6579 if (fNotifyClientArray)
6580 object = fNotifyClientArray->getObject(ordinalComponent);
6581
6582 OSNumber * num;
6583 if ((num = OSDynamicCast(OSNumber, theFlag)))
6584 {
6585#if LOG_APP_RESPONSE_TIMES
6586 AbsoluteTime now;
6587 AbsoluteTime start;
6588 uint64_t nsec;
6589 char name[128];
6590
6591 name[0] = '\0';
6592 proc_name(pid, name, sizeof(name));
6593 clock_get_uptime(&now);
6594 AbsoluteTime_to_scalar(&start) = num->unsigned64BitValue();
6595 SUB_ABSOLUTETIME(&now, &start);
6596 absolutetime_to_nanoseconds(now, &nsec);
6597
6598 if (kIOLogDebugPower & gIOKitDebug)
6599 {
6600 PM_LOG("Ack(%u) %u ms\n",
6601 (uint32_t) ordinalComponent,
6602 NS_TO_MS(nsec));
6603 }
6604
6605 // > 100 ms
6606 if (nsec > LOG_APP_RESPONSE_TIMES)
6607 {
6608 IOLog("PM response took %d ms (%d, %s)\n", NS_TO_MS(nsec),
6609 pid, name);
6610 }
6611
6612 if (nsec > LOG_APP_RESPONSE_MSG_TRACER)
6613 {
6614 // TODO: populate the messageType argument
6615 getPMRootDomain()->pmStatsRecordApplicationResponse(
6616 gIOPMStatsApplicationResponseSlow,
6617 name, 0, NS_TO_MS(nsec), pid, object);
6618 }
6619 else
6620 {
6621 getPMRootDomain()->pmStatsRecordApplicationResponse(
6622 gIOPMStatsApplicationResponsePrompt,
6623 name, 0, NS_TO_MS(nsec), pid, object);
6624 }
6625
6626#endif
6627 theFlag = kOSBooleanFalse;
6628 }
6629 else if (object) {
6630 getPMRootDomain()->pmStatsRecordApplicationResponse(
6631 gIOPMStatsApplicationResponsePrompt,
6632 0, 0, 0, pid, object);
6633
6634 }
6635
6636 if ( kOSBooleanFalse == theFlag )
6637 {
6638 fResponseArray->replaceObject(ordinalComponent, kOSBooleanTrue);
6639 }
6640
6641 return true;
6642}
6643
6644//*********************************************************************************
6645// [public] allowPowerChange
6646//
6647// Our power state is about to lower, and we have notified applications
6648// and kernel clients, and one of them has acknowledged. If this is the last to do
6649// so, and all acknowledgements are positive, we continue with the power change.
6650//*********************************************************************************
6651
6652IOReturn IOService::allowPowerChange( unsigned long refcon )
6653{
6654 IOPMRequest * request;
6655
6656 if ( !initialized )
6657 {
6658 // we're unloading
6659 return kIOReturnSuccess;
6660 }
6661
6662 request = acquirePMRequest( this, kIOPMRequestTypeAllowPowerChange );
6663 if (!request)
6664 return kIOReturnNoMemory;
6665
6666 request->fArg0 = (void *) refcon;
6667 request->fArg1 = (void *)(uintptr_t) proc_selfpid();
6668 request->fArg2 = (void *) 0;
6669 submitPMRequest( request );
6670
6671 return kIOReturnSuccess;
6672}
6673
6674#ifndef __LP64__
6675IOReturn IOService::serializedAllowPowerChange2( unsigned long refcon )
6676{
6677 // [deprecated] public
6678 return kIOReturnUnsupported;
6679}
6680#endif /* !__LP64__ */
6681
6682//*********************************************************************************
6683// [public] cancelPowerChange
6684//
6685// Our power state is about to lower, and we have notified applications
6686// and kernel clients, and one of them has vetoed the change. If this is the last
6687// client to respond, we abandon the power change.
6688//*********************************************************************************
6689
6690IOReturn IOService::cancelPowerChange( unsigned long refcon )
6691{
6692 IOPMRequest * request;
6693 char name[128];
6694 pid_t pid = proc_selfpid();
6695
6696 if ( !initialized )
6697 {
6698 // we're unloading
6699 return kIOReturnSuccess;
6700 }
6701
6702 name[0] = '\0';
6703 proc_name(pid, name, sizeof(name));
6704 PM_ERROR("PM notification cancel (pid %d, %s)\n", pid, name);
6705
6706 request = acquirePMRequest( this, kIOPMRequestTypeCancelPowerChange );
6707 if (!request)
6708 {
6709 return kIOReturnNoMemory;
6710 }
6711
6712 request->fArg0 = (void *) refcon;
6713 request->fArg1 = (void *)(uintptr_t) proc_selfpid();
6714 request->fArg2 = (void *) OSString::withCString(name);
6715 submitPMRequest( request );
6716
6717 return kIOReturnSuccess;
6718}
6719
6720#ifndef __LP64__
6721IOReturn IOService::serializedCancelPowerChange2( unsigned long refcon )
6722{
6723 // [deprecated] public
6724 return kIOReturnUnsupported;
6725}
6726
6727//*********************************************************************************
6728// PM_Clamp_Timer_Expired
6729//
6730// called when clamp timer expires...set power state to 0.
6731//*********************************************************************************
6732
6733void IOService::PM_Clamp_Timer_Expired( void )
6734{
6735}
6736
6737//*********************************************************************************
6738// clampPowerOn
6739//
6740// Set to highest available power state for a minimum of duration milliseconds
6741//*********************************************************************************
6742
6743void IOService::clampPowerOn( unsigned long duration )
6744{
6745}
6746#endif /* !__LP64__ */
6747
6748//*********************************************************************************
6749// configurePowerStateReport
6750//
6751// Configures the IOStateReport for kPMPowerStateChannel
6752//*********************************************************************************
6753IOReturn IOService::configurePowerStatesReport( IOReportConfigureAction action, void *result )
6754{
6755
6756 IOReturn rc = kIOReturnSuccess;
6757 size_t reportSize;
6758 unsigned long i;
6759 uint64_t ts;
6760
6761 if (!pwrMgt)
6762 return kIOReturnUnsupported;
6763
6764 if (!fNumberOfPowerStates)
6765 return kIOReturnSuccess; // For drivers which are in power plane, but haven't called registerPowerDriver()
6766 PM_LOCK();
6767
6768 switch (action)
6769 {
6770 case kIOReportEnable:
6771 if (fReportBuf)
6772 {
6773 fReportClientCnt++;
6774 break;
6775 }
6776 reportSize = STATEREPORT_BUFSIZE(fNumberOfPowerStates);
6777 fReportBuf = IOMalloc(reportSize);
6778 if (!fReportBuf) {
6779 rc = kIOReturnNoMemory;
6780 break;
6781 }
6782 memset(fReportBuf, 0, reportSize);
6783
6784 STATEREPORT_INIT(fNumberOfPowerStates, fReportBuf, reportSize,
6785 getRegistryEntryID(), kPMPowerStatesChID, kIOReportCategoryPower);
6786
6787 for (i = 0; i < fNumberOfPowerStates; i++) {
6788 unsigned bits = 0;
6789
6790 if (fPowerStates[i].capabilityFlags & kIOPMPowerOn)
6791 bits |= kPMReportPowerOn;
6792 if (fPowerStates[i].capabilityFlags & kIOPMDeviceUsable)
6793 bits |= kPMReportDeviceUsable;
6794 if (fPowerStates[i].capabilityFlags & kIOPMLowPower)
6795 bits |= kPMReportLowPower;
6796
6797 STATEREPORT_SETSTATEID(fReportBuf, i, ((bits & 0xff) << 8) |
6798 ((StateOrder(fMaxPowerState) & 0xf) << 4) | (StateOrder(i) & 0xf));
6799 }
6800 ts = mach_absolute_time();
6801 STATEREPORT_SETSTATE(fReportBuf, fCurrentPowerState, ts);
6802 break;
6803
6804 case kIOReportDisable:
6805 if (fReportClientCnt == 0) {
6806 rc = kIOReturnBadArgument;
6807 break;
6808 }
6809 if (fReportClientCnt == 1)
6810 {
6811 IOFree(fReportBuf, STATEREPORT_BUFSIZE(fNumberOfPowerStates));
6812 fReportBuf = NULL;
6813 }
6814 fReportClientCnt--;
6815 break;
6816
6817 case kIOReportGetDimensions:
6818 if (fReportBuf)
6819 STATEREPORT_UPDATERES(fReportBuf, kIOReportGetDimensions, result);
6820 break;
6821 }
6822
6823 PM_UNLOCK();
6824
6825 return rc;
6826}
6827
6828//*********************************************************************************
6829// updatePowerStateReport
6830//
6831// Updates the IOStateReport for kPMPowerStateChannel
6832//*********************************************************************************
6833IOReturn IOService::updatePowerStatesReport( IOReportConfigureAction action, void *result, void *destination )
6834{
6835 uint32_t size2cpy;
6836 void *data2cpy;
6837 uint64_t ts;
6838 IOReturn rc = kIOReturnSuccess;
6839 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
6840
6841
6842 if (!pwrMgt)
6843 return kIOReturnUnsupported;
6844 if (!fNumberOfPowerStates)
6845 return kIOReturnSuccess;
6846
6847 if ( !result || !dest ) return kIOReturnBadArgument;
6848 PM_LOCK();
6849
6850 switch (action) {
6851 case kIOReportCopyChannelData:
6852 if ( !fReportBuf ) {
6853 rc = kIOReturnNotOpen;
6854 break;
6855 }
6856
6857 ts = mach_absolute_time();
6858 STATEREPORT_UPDATEPREP(fReportBuf, ts, data2cpy, size2cpy);
6859 if (size2cpy > (dest->getCapacity() - dest->getLength()) ) {
6860 rc = kIOReturnOverrun;
6861 break;
6862 }
6863
6864 STATEREPORT_UPDATERES(fReportBuf, kIOReportCopyChannelData, result);
6865 dest->appendBytes(data2cpy, size2cpy);
6866
6867 default:
6868 break;
6869
6870 }
6871
6872 PM_UNLOCK();
6873
6874 return rc;
6875
6876}
6877
6878//*********************************************************************************
6879// configureSimplePowerReport
6880//
6881// Configures the IOSimpleReport for given channel id
6882//*********************************************************************************
6883IOReturn IOService::configureSimplePowerReport(IOReportConfigureAction action, void *result )
6884{
6885
6886 IOReturn rc = kIOReturnSuccess;
6887
6888 if ( !pwrMgt )
6889 return kIOReturnUnsupported;
6890
6891 if ( !fNumberOfPowerStates )
6892 return rc;
6893
6894 switch (action)
6895 {
6896 case kIOReportEnable:
6897 case kIOReportDisable:
6898 break;
6899
6900 case kIOReportGetDimensions:
6901 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
6902 break;
6903 }
6904
6905
6906 return rc;
6907}
6908
6909//*********************************************************************************
6910// updateSimplePowerReport
6911//
6912// Updates the IOSimpleReport for the given chanel id
6913//*********************************************************************************
6914IOReturn IOService::updateSimplePowerReport( IOReportConfigureAction action, void *result, void *destination )
6915{
6916 uint32_t size2cpy;
6917 void *data2cpy;
6918 uint64_t buf[SIMPLEREPORT_BUFSIZE/sizeof(uint64_t)+1]; // Force a 8-byte alignment
6919 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
6920 IOReturn rc = kIOReturnSuccess;
6921 unsigned bits = 0;
6922
6923
6924 if ( !pwrMgt )
6925 return kIOReturnUnsupported;
6926 if ( !result || !dest ) return kIOReturnBadArgument;
6927
6928 if ( !fNumberOfPowerStates )
6929 return rc;
6930 PM_LOCK();
6931
6932 switch (action) {
6933 case kIOReportCopyChannelData:
6934
6935 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), kPMCurrStateChID, kIOReportCategoryPower);
6936
6937 if (fPowerStates[fCurrentPowerState].capabilityFlags & kIOPMPowerOn)
6938 bits |= kPMReportPowerOn;
6939 if (fPowerStates[fCurrentPowerState].capabilityFlags & kIOPMDeviceUsable)
6940 bits |= kPMReportDeviceUsable;
6941 if (fPowerStates[fCurrentPowerState].capabilityFlags & kIOPMLowPower)
6942 bits |= kPMReportLowPower;
6943
6944
6945 SIMPLEREPORT_SETVALUE(buf, ((bits & 0xff) << 8) | ((StateOrder(fMaxPowerState) & 0xf) << 4) |
6946 (StateOrder(fCurrentPowerState) & 0xf));
6947
6948 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
6949 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
6950 rc = kIOReturnOverrun;
6951 break;
6952 }
6953
6954 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
6955 dest->appendBytes(data2cpy, size2cpy);
6956
6957 default:
6958 break;
6959
6960 }
6961
6962 PM_UNLOCK();
6963
6964 return kIOReturnSuccess;
6965
6966}
6967
6968
6969
6970// MARK: -
6971// MARK: Driver Overrides
6972
6973//*********************************************************************************
6974// [public] setPowerState
6975//
6976// Does nothing here. This should be implemented in a subclass driver.
6977//*********************************************************************************
6978
6979IOReturn IOService::setPowerState(
6980 unsigned long powerStateOrdinal, IOService * whatDevice )
6981{
6982 return IOPMNoErr;
6983}
6984
6985//*********************************************************************************
6986// [public] maxCapabilityForDomainState
6987//
6988// Finds the highest power state in the array whose input power requirement
6989// is equal to the input parameter. Where a more intelligent decision is
6990// possible, override this in the subclassed driver.
6991//*********************************************************************************
6992
6993IOPMPowerStateIndex IOService::getPowerStateForDomainFlags( IOPMPowerFlags flags )
6994{
6995 IOPMPowerStateIndex stateIndex;
6996
6997 if (!fNumberOfPowerStates)
6998 return kPowerStateZero;
6999
7000 for ( int order = fNumberOfPowerStates - 1; order >= 0; order-- )
7001 {
7002 stateIndex = fPowerStates[order].stateOrderToIndex;
7003
7004 if ( (flags & fPowerStates[stateIndex].inputPowerFlags) ==
7005 fPowerStates[stateIndex].inputPowerFlags )
7006 {
7007 return stateIndex;
7008 }
7009 }
7010 return kPowerStateZero;
7011}
7012
7013unsigned long IOService::maxCapabilityForDomainState( IOPMPowerFlags domainState )
7014{
7015 return getPowerStateForDomainFlags(domainState);
7016}
7017
7018//*********************************************************************************
7019// [public] initialPowerStateForDomainState
7020//
7021// Called to query the power state for the initial power transition.
7022//*********************************************************************************
7023
7024unsigned long IOService::initialPowerStateForDomainState( IOPMPowerFlags domainState )
7025{
7026 if (fResetPowerStateOnWake && (domainState & kIOPMRootDomainState))
7027 {
7028 // Return lowest power state for any root power domain changes
7029 return kPowerStateZero;
7030 }
7031
7032 return getPowerStateForDomainFlags(domainState);
7033}
7034
7035//*********************************************************************************
7036// [public] powerStateForDomainState
7037//
7038// This method is not called from PM.
7039//*********************************************************************************
7040
7041unsigned long IOService::powerStateForDomainState( IOPMPowerFlags domainState )
7042{
7043 return getPowerStateForDomainFlags(domainState);
7044}
7045
7046#ifndef __LP64__
7047//*********************************************************************************
7048// [deprecated] didYouWakeSystem
7049//
7050// Does nothing here. This should be implemented in a subclass driver.
7051//*********************************************************************************
7052
7053bool IOService::didYouWakeSystem( void )
7054{
7055 return false;
7056}
7057#endif /* !__LP64__ */
7058
7059//*********************************************************************************
7060// [public] powerStateWillChangeTo
7061//
7062// Does nothing here. This should be implemented in a subclass driver.
7063//*********************************************************************************
7064
7065IOReturn IOService::powerStateWillChangeTo( IOPMPowerFlags, unsigned long, IOService * )
7066{
7067 return kIOPMAckImplied;
7068}
7069
7070//*********************************************************************************
7071// [public] powerStateDidChangeTo
7072//
7073// Does nothing here. This should be implemented in a subclass driver.
7074//*********************************************************************************
7075
7076IOReturn IOService::powerStateDidChangeTo( IOPMPowerFlags, unsigned long, IOService * )
7077{
7078 return kIOPMAckImplied;
7079}
7080
7081//*********************************************************************************
7082// [protected] powerChangeDone
7083//
7084// Called from PM work loop thread.
7085// Does nothing here. This should be implemented in a subclass policy-maker.
7086//*********************************************************************************
7087
7088void IOService::powerChangeDone( unsigned long )
7089{
7090}
7091
7092#ifndef __LP64__
7093//*********************************************************************************
7094// [deprecated] newTemperature
7095//
7096// Does nothing here. This should be implemented in a subclass driver.
7097//*********************************************************************************
7098
7099IOReturn IOService::newTemperature( long currentTemp, IOService * whichZone )
7100{
7101 return IOPMNoErr;
7102}
7103#endif /* !__LP64__ */
7104
7105//*********************************************************************************
7106// [public] systemWillShutdown
7107//
7108// System shutdown and restart notification.
7109//*********************************************************************************
7110
7111void IOService::systemWillShutdown( IOOptionBits specifier )
7112{
7113 IOPMrootDomain * rootDomain = IOService::getPMRootDomain();
7114 if (rootDomain)
7115 rootDomain->acknowledgeSystemWillShutdown( this );
7116}
7117
7118// MARK: -
7119// MARK: PM State Machine
7120
7121//*********************************************************************************
7122// [private static] acquirePMRequest
7123//*********************************************************************************
7124
7125IOPMRequest *
7126IOService::acquirePMRequest( IOService * target, IOOptionBits requestType,
7127 IOPMRequest * active )
7128{
7129 IOPMRequest * request;
7130
7131 assert(target);
7132
7133 request = IOPMRequest::create();
7134 if (request)
7135 {
7136 request->init( target, requestType );
7137 if (active)
7138 {
7139 IOPMRequest * root = active->getRootRequest();
7140 if (root) request->attachRootRequest(root);
7141 }
7142 }
7143 else
7144 {
7145 PM_ERROR("%s: No memory for PM request type 0x%x\n",
7146 target->getName(), (uint32_t) requestType);
7147 }
7148 return request;
7149}
7150
7151//*********************************************************************************
7152// [private static] releasePMRequest
7153//*********************************************************************************
7154
7155void IOService::releasePMRequest( IOPMRequest * request )
7156{
7157 if (request)
7158 {
7159 request->reset();
7160 request->release();
7161 }
7162}
7163
7164//*********************************************************************************
7165// [private static] submitPMRequest
7166//*********************************************************************************
7167
7168void IOService::submitPMRequest( IOPMRequest * request )
7169{
7170 assert( request );
7171 assert( gIOPMReplyQueue );
7172 assert( gIOPMRequestQueue );
7173
7174 PM_LOG1("[+ %02lx] %p [%p %s] %p %p %p\n",
7175 (long)request->getType(), OBFUSCATE(request),
7176 OBFUSCATE(request->getTarget()), request->getTarget()->getName(),
7177 OBFUSCATE(request->fArg0),
7178 OBFUSCATE(request->fArg1), OBFUSCATE(request->fArg2));
7179
7180 if (request->isReplyType())
7181 gIOPMReplyQueue->queuePMRequest( request );
7182 else
7183 gIOPMRequestQueue->queuePMRequest( request );
7184}
7185
7186void IOService::submitPMRequests( IOPMRequest ** requests, IOItemCount count )
7187{
7188 assert( requests );
7189 assert( count > 0 );
7190 assert( gIOPMRequestQueue );
7191
7192 for (IOItemCount i = 0; i < count; i++)
7193 {
7194 IOPMRequest * req = requests[i];
7195 PM_LOG1("[+ %02lx] %p [%p %s] %p %p %p\n",
7196 (long)req->getType(), OBFUSCATE(req),
7197 OBFUSCATE(req->getTarget()), req->getTarget()->getName(),
7198 OBFUSCATE(req->fArg0),
7199 OBFUSCATE(req->fArg1), OBFUSCATE(req->fArg2));
7200 }
7201
7202 gIOPMRequestQueue->queuePMRequestChain( requests, count );
7203}
7204
7205//*********************************************************************************
7206// [private] actionPMRequestQueue
7207//
7208// IOPMRequestQueue::checkForWork() passing a new request to the request target.
7209//*********************************************************************************
7210
7211bool IOService::actionPMRequestQueue(
7212 IOPMRequest * request,
7213 IOPMRequestQueue * queue )
7214{
7215 bool more;
7216
7217 if (initialized)
7218 {
7219 // Work queue will immediately execute the request if the per-service
7220 // request queue is empty. Note pwrMgt is the target's IOServicePM.
7221
7222 more = gIOPMWorkQueue->queuePMRequest(request, pwrMgt);
7223 }
7224 else
7225 {
7226 // Calling PM without PMinit() is not allowed, fail the request.
7227 // Need to signal more when completing attached requests.
7228
7229 PM_LOG("%s: PM not initialized\n", getName());
7230 PM_LOG1("[- %02x] %p [%p %s] !initialized\n",
7231 request->getType(), OBFUSCATE(request),
7232 OBFUSCATE(this), getName());
7233
7234 more = gIOPMCompletionQueue->queuePMRequest(request);
7235 if (more) gIOPMWorkQueue->incrementProducerCount();
7236 }
7237
7238 return more;
7239}
7240
7241//*********************************************************************************
7242// [private] actionPMCompletionQueue
7243//
7244// IOPMCompletionQueue::checkForWork() passing a completed request to the
7245// request target.
7246//*********************************************************************************
7247
7248bool IOService::actionPMCompletionQueue(
7249 IOPMRequest * request,
7250 IOPMCompletionQueue * queue )
7251{
7252 bool more = (request->getNextRequest() != 0);
7253 IOPMRequest * root = request->getRootRequest();
7254
7255 if (root && (root != request))
7256 more = true;
7257 if (more)
7258 gIOPMWorkQueue->incrementProducerCount();
7259
7260 releasePMRequest( request );
7261 return more;
7262}
7263
7264//*********************************************************************************
7265// [private] actionPMWorkQueueRetire
7266//
7267// IOPMWorkQueue::checkForWork() passing a retired request to the request target.
7268//*********************************************************************************
7269
7270bool IOService::actionPMWorkQueueRetire( IOPMRequest * request, IOPMWorkQueue * queue )
7271{
7272 assert(request && queue);
7273
7274 PM_LOG1("[- %02x] %p [%p %s] state %d, busy %d\n",
7275 request->getType(), OBFUSCATE(request),
7276 OBFUSCATE(this), getName(),
7277 fMachineState, gIOPMBusyRequestCount);
7278
7279 // Catch requests created by idleTimerExpired()
7280 if (request->getType() == kIOPMRequestTypeActivityTickle)
7281 {
7282 uint32_t tickleFlags = (uint32_t)(uintptr_t) request->fArg1;
7283
7284 if ((tickleFlags & kTickleTypePowerDrop) && fIdleTimerPeriod)
7285 {
7286 restartIdleTimer();
7287 }
7288 else if (tickleFlags == (kTickleTypeActivity | kTickleTypePowerRise))
7289 {
7290 // Invalidate any idle power drop that got queued while
7291 // processing this request.
7292 fIdleTimerGeneration++;
7293 }
7294 }
7295
7296 // When the completed request is linked, tell work queue there is
7297 // more work pending.
7298
7299 return (gIOPMCompletionQueue->queuePMRequest( request ));
7300}
7301
7302//*********************************************************************************
7303// [private] isPMBlocked
7304//
7305// Check if machine state transition is blocked.
7306//*********************************************************************************
7307
7308bool IOService::isPMBlocked( IOPMRequest * request, int count )
7309{
7310 int reason = 0;
7311
7312 do {
7313 if (kIOPM_Finished == fMachineState)
7314 break;
7315
7316 if (kIOPM_DriverThreadCallDone == fMachineState)
7317 {
7318 // 5 = kDriverCallInformPreChange
7319 // 6 = kDriverCallInformPostChange
7320 // 7 = kDriverCallSetPowerState
7321 // 8 = kRootDomainInformPreChange
7322 if (fDriverCallBusy)
7323 reason = 5 + fDriverCallReason;
7324 break;
7325 }
7326
7327 // Waiting on driver's setPowerState() timeout.
7328 if (fDriverTimer)
7329 {
7330 reason = 1; break;
7331 }
7332
7333 // Child or interested driver acks pending.
7334 if (fHeadNotePendingAcks)
7335 {
7336 reason = 2; break;
7337 }
7338
7339 // Waiting on apps or priority power interest clients.
7340 if (fResponseArray)
7341 {
7342 reason = 3; break;
7343 }
7344
7345 // Waiting on settle timer expiration.
7346 if (fSettleTimeUS)
7347 {
7348 reason = 4; break;
7349 }
7350 } while (false);
7351
7352 fWaitReason = reason;
7353
7354 if (reason)
7355 {
7356 if (count)
7357 {
7358 PM_LOG1("[B %02x] %p [%p %s] state %d, reason %d\n",
7359 request->getType(), OBFUSCATE(request),
7360 OBFUSCATE(this), getName(),
7361 fMachineState, reason);
7362 }
7363
7364 return true;
7365 }
7366
7367 return false;
7368}
7369
7370//*********************************************************************************
7371// [private] actionPMWorkQueueInvoke
7372//
7373// IOPMWorkQueue::checkForWork() passing a request to the
7374// request target for execution.
7375//*********************************************************************************
7376
7377bool IOService::actionPMWorkQueueInvoke( IOPMRequest * request, IOPMWorkQueue * queue )
7378{
7379 bool done = false;
7380 int loop = 0;
7381
7382 assert(request && queue);
7383
7384 while (isPMBlocked(request, loop++) == false)
7385 {
7386 PM_LOG1("[W %02x] %p [%p %s] state %d\n",
7387 request->getType(), OBFUSCATE(request),
7388 OBFUSCATE(this), getName(), fMachineState);
7389
7390 gIOPMRequest = request;
7391 gIOPMWorkInvokeCount++;
7392
7393 // Every PM machine states must be handled in one of the cases below.
7394
7395 switch ( fMachineState )
7396 {
7397 case kIOPM_Finished:
7398 start_watchdog_timer();
7399
7400 executePMRequest( request );
7401 break;
7402
7403 case kIOPM_OurChangeTellClientsPowerDown:
7404 // Root domain might self cancel due to assertions.
7405 if (IS_ROOT_DOMAIN)
7406 {
7407 bool cancel = (bool) fDoNotPowerDown;
7408 getPMRootDomain()->askChangeDownDone(
7409 &fHeadNoteChangeFlags, &cancel);
7410 fDoNotPowerDown = cancel;
7411 }
7412
7413 // askChangeDown() done, was it vetoed?
7414 if (!fDoNotPowerDown)
7415 {
7416 // no, we can continue
7417 OurChangeTellClientsPowerDown();
7418 }
7419 else
7420 {
7421 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7422 PM_ERROR("%s: idle cancel, state %u\n", fName, fMachineState);
7423 // yes, rescind the warning
7424 tellNoChangeDown(fHeadNotePowerState);
7425 // mark the change note un-actioned
7426 fHeadNoteChangeFlags |= kIOPMNotDone;
7427 // and we're done
7428 OurChangeFinish();
7429 }
7430 break;
7431
7432 case kIOPM_OurChangeTellUserPMPolicyPowerDown:
7433 // PMRD: tellChangeDown/kNotifyApps done, was it cancelled?
7434 if (fDoNotPowerDown)
7435 {
7436 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7437 PM_ERROR("%s: idle cancel, state %u\n", fName, fMachineState);
7438 // yes, rescind the warning
7439 tellNoChangeDown(fHeadNotePowerState);
7440 // mark the change note un-actioned
7441 fHeadNoteChangeFlags |= kIOPMNotDone;
7442 // and we're done
7443 OurChangeFinish();
7444 }
7445 else
7446 OurChangeTellUserPMPolicyPowerDown();
7447 break;
7448
7449 case kIOPM_OurChangeTellPriorityClientsPowerDown:
7450 // PMRD: LastCallBeforeSleep notify done
7451 // Non-PMRD: tellChangeDown/kNotifyApps done
7452 if (fDoNotPowerDown)
7453 {
7454 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7455 PM_ERROR("%s: idle revert, state %u\n", fName, fMachineState);
7456 // no, tell clients we're back in the old state
7457 tellChangeUp(fCurrentPowerState);
7458 // mark the change note un-actioned
7459 fHeadNoteChangeFlags |= kIOPMNotDone;
7460 // and we're done
7461 OurChangeFinish();
7462 }
7463 else
7464 {
7465 // yes, we can continue
7466 OurChangeTellPriorityClientsPowerDown();
7467 }
7468 break;
7469
7470 case kIOPM_OurChangeNotifyInterestedDriversWillChange:
7471 OurChangeNotifyInterestedDriversWillChange();
7472 break;
7473
7474 case kIOPM_OurChangeSetPowerState:
7475 OurChangeSetPowerState();
7476 break;
7477
7478 case kIOPM_OurChangeWaitForPowerSettle:
7479 OurChangeWaitForPowerSettle();
7480 break;
7481
7482 case kIOPM_OurChangeNotifyInterestedDriversDidChange:
7483 OurChangeNotifyInterestedDriversDidChange();
7484 break;
7485
7486 case kIOPM_OurChangeTellCapabilityDidChange:
7487 OurChangeTellCapabilityDidChange();
7488 break;
7489
7490 case kIOPM_OurChangeFinish:
7491 OurChangeFinish();
7492 break;
7493
7494 case kIOPM_ParentChangeTellPriorityClientsPowerDown:
7495 ParentChangeTellPriorityClientsPowerDown();
7496 break;
7497
7498 case kIOPM_ParentChangeNotifyInterestedDriversWillChange:
7499 ParentChangeNotifyInterestedDriversWillChange();
7500 break;
7501
7502 case kIOPM_ParentChangeSetPowerState:
7503 ParentChangeSetPowerState();
7504 break;
7505
7506 case kIOPM_ParentChangeWaitForPowerSettle:
7507 ParentChangeWaitForPowerSettle();
7508 break;
7509
7510 case kIOPM_ParentChangeNotifyInterestedDriversDidChange:
7511 ParentChangeNotifyInterestedDriversDidChange();
7512 break;
7513
7514 case kIOPM_ParentChangeTellCapabilityDidChange:
7515 ParentChangeTellCapabilityDidChange();
7516 break;
7517
7518 case kIOPM_ParentChangeAcknowledgePowerChange:
7519 ParentChangeAcknowledgePowerChange();
7520 break;
7521
7522 case kIOPM_DriverThreadCallDone:
7523 switch (fDriverCallReason)
7524 {
7525 case kDriverCallInformPreChange:
7526 case kDriverCallInformPostChange:
7527 notifyInterestedDriversDone();
7528 break;
7529 case kDriverCallSetPowerState:
7530 notifyControllingDriverDone();
7531 break;
7532 case kRootDomainInformPreChange:
7533 notifyRootDomainDone();
7534 break;
7535 default:
7536 panic("%s: bad call reason %x",
7537 getName(), fDriverCallReason);
7538 }
7539 break;
7540
7541 case kIOPM_NotifyChildrenOrdered:
7542 notifyChildrenOrdered();
7543 break;
7544
7545 case kIOPM_NotifyChildrenDelayed:
7546 notifyChildrenDelayed();
7547 break;
7548
7549 case kIOPM_NotifyChildrenStart:
7550 // pop notifyAll() state saved by notifyInterestedDriversDone()
7551 MS_POP();
7552 notifyRootDomain();
7553 break;
7554
7555 case kIOPM_SyncTellClientsPowerDown:
7556 // Root domain might self cancel due to assertions.
7557 if (IS_ROOT_DOMAIN)
7558 {
7559 bool cancel = (bool) fDoNotPowerDown;
7560 getPMRootDomain()->askChangeDownDone(
7561 &fHeadNoteChangeFlags, &cancel);
7562 fDoNotPowerDown = cancel;
7563 }
7564 if (!fDoNotPowerDown)
7565 {
7566 fMachineState = kIOPM_SyncTellPriorityClientsPowerDown;
7567 fOutOfBandParameter = kNotifyApps;
7568 tellChangeDown(fHeadNotePowerState);
7569 }
7570 else
7571 {
7572 // Cancelled by IOPMrootDomain::askChangeDownDone() or
7573 // askChangeDown/kNotifyApps
7574 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7575 PM_ERROR("%s: idle cancel, state %u\n", fName, fMachineState);
7576 tellNoChangeDown(fHeadNotePowerState);
7577 fHeadNoteChangeFlags |= kIOPMNotDone;
7578 OurChangeFinish();
7579 }
7580 break;
7581
7582 case kIOPM_SyncTellPriorityClientsPowerDown:
7583 // PMRD: tellChangeDown/kNotifyApps done, was it cancelled?
7584 if (!fDoNotPowerDown)
7585 {
7586 fMachineState = kIOPM_SyncNotifyWillChange;
7587 fOutOfBandParameter = kNotifyPriority;
7588 tellChangeDown(fHeadNotePowerState);
7589 }
7590 else
7591 {
7592 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7593 PM_ERROR("%s: idle revert, state %u\n", fName, fMachineState);
7594 tellChangeUp(fCurrentPowerState);
7595 fHeadNoteChangeFlags |= kIOPMNotDone;
7596 OurChangeFinish();
7597 }
7598 break;
7599
7600 case kIOPM_SyncNotifyWillChange:
7601 if (kIOPMSyncNoChildNotify & fHeadNoteChangeFlags)
7602 {
7603 fMachineState = kIOPM_SyncFinish;
7604 continue;
7605 }
7606 fMachineState = kIOPM_SyncNotifyDidChange;
7607 fDriverCallReason = kDriverCallInformPreChange;
7608 notifyChildren();
7609 break;
7610
7611 case kIOPM_SyncNotifyDidChange:
7612 fIsPreChange = false;
7613
7614 if (fHeadNoteChangeFlags & kIOPMParentInitiated)
7615 {
7616 fMachineState = kIOPM_SyncFinish;
7617 }
7618 else
7619 {
7620 assert(IS_ROOT_DOMAIN);
7621 fMachineState = kIOPM_SyncTellCapabilityDidChange;
7622 }
7623
7624 fDriverCallReason = kDriverCallInformPostChange;
7625 notifyChildren();
7626 break;
7627
7628 case kIOPM_SyncTellCapabilityDidChange:
7629 tellSystemCapabilityChange( kIOPM_SyncFinish );
7630 break;
7631
7632 case kIOPM_SyncFinish:
7633 if (fHeadNoteChangeFlags & kIOPMParentInitiated)
7634 ParentChangeAcknowledgePowerChange();
7635 else
7636 OurChangeFinish();
7637 break;
7638
7639 case kIOPM_TellCapabilityChangeDone:
7640 if (fIsPreChange)
7641 {
7642 if (fOutOfBandParameter == kNotifyCapabilityChangePriority)
7643 {
7644 MS_POP(); // tellSystemCapabilityChange()
7645 continue;
7646 }
7647 fOutOfBandParameter = kNotifyCapabilityChangePriority;
7648 }
7649 else
7650 {
7651 if (fOutOfBandParameter == kNotifyCapabilityChangeApps)
7652 {
7653 MS_POP(); // tellSystemCapabilityChange()
7654 continue;
7655 }
7656 fOutOfBandParameter = kNotifyCapabilityChangeApps;
7657 }
7658 tellClientsWithResponse( fOutOfBandMessage );
7659 break;
7660
7661 default:
7662 panic("PMWorkQueueInvoke: unknown machine state %x",
7663 fMachineState);
7664 }
7665
7666 gIOPMRequest = 0;
7667
7668 if (fMachineState == kIOPM_Finished)
7669 {
7670 stop_watchdog_timer();
7671 done = true;
7672 break;
7673 }
7674 }
7675
7676 return done;
7677}
7678
7679//*********************************************************************************
7680// [private] executePMRequest
7681//*********************************************************************************
7682
7683void IOService::executePMRequest( IOPMRequest * request )
7684{
7685 assert( kIOPM_Finished == fMachineState );
7686
7687 switch (request->getType())
7688 {
7689 case kIOPMRequestTypePMStop:
7690 handlePMstop( request );
7691 break;
7692
7693 case kIOPMRequestTypeAddPowerChild1:
7694 addPowerChild1( request );
7695 break;
7696
7697 case kIOPMRequestTypeAddPowerChild2:
7698 addPowerChild2( request );
7699 break;
7700
7701 case kIOPMRequestTypeAddPowerChild3:
7702 addPowerChild3( request );
7703 break;
7704
7705 case kIOPMRequestTypeRegisterPowerDriver:
7706 handleRegisterPowerDriver( request );
7707 break;
7708
7709 case kIOPMRequestTypeAdjustPowerState:
7710 fAdjustPowerScheduled = false;
7711 adjustPowerState();
7712 break;
7713
7714 case kIOPMRequestTypePowerDomainWillChange:
7715 handlePowerDomainWillChangeTo( request );
7716 break;
7717
7718 case kIOPMRequestTypePowerDomainDidChange:
7719 handlePowerDomainDidChangeTo( request );
7720 break;
7721
7722 case kIOPMRequestTypeRequestPowerState:
7723 case kIOPMRequestTypeRequestPowerStateOverride:
7724 handleRequestPowerState( request );
7725 break;
7726
7727 case kIOPMRequestTypePowerOverrideOnPriv:
7728 case kIOPMRequestTypePowerOverrideOffPriv:
7729 handlePowerOverrideChanged( request );
7730 break;
7731
7732 case kIOPMRequestTypeActivityTickle:
7733 handleActivityTickle( request );
7734 break;
7735
7736 case kIOPMRequestTypeSynchronizePowerTree:
7737 handleSynchronizePowerTree( request );
7738 break;
7739
7740 case kIOPMRequestTypeSetIdleTimerPeriod:
7741 {
7742 fIdleTimerPeriod = (uintptr_t) request->fArg0;
7743 fNextIdleTimerPeriod = fIdleTimerPeriod;
7744 if ((false == fLockedFlags.PMStop) && (fIdleTimerPeriod > 0))
7745 restartIdleTimer();
7746 }
7747 break;
7748
7749 case kIOPMRequestTypeIgnoreIdleTimer:
7750 fIdleTimerIgnored = request->fArg0 ? 1 : 0;
7751 break;
7752
7753 case kIOPMRequestTypeQuiescePowerTree:
7754 gIOPMWorkQueue->finishQuiesceRequest(request);
7755 break;
7756
7757 default:
7758 panic("executePMRequest: unknown request type %x", request->getType());
7759 }
7760}
7761
7762//*********************************************************************************
7763// [private] actionPMReplyQueue
7764//
7765// IOPMRequestQueue::checkForWork() passing a reply-type request to the
7766// request target.
7767//*********************************************************************************
7768
7769bool IOService::actionPMReplyQueue( IOPMRequest * request, IOPMRequestQueue * queue )
7770{
7771 bool more = false;
7772
7773 assert( request && queue );
7774 assert( request->isReplyType() );
7775
7776 PM_LOG1("[A %02x] %p [%p %s] state %d\n",
7777 request->getType(), OBFUSCATE(request),
7778 OBFUSCATE(this), getName(), fMachineState);
7779
7780 switch ( request->getType() )
7781 {
7782 case kIOPMRequestTypeAllowPowerChange:
7783 case kIOPMRequestTypeCancelPowerChange:
7784 // Check if we are expecting this response.
7785 if (responseValid((uint32_t)(uintptr_t) request->fArg0,
7786 (int)(uintptr_t) request->fArg1))
7787 {
7788 if (kIOPMRequestTypeCancelPowerChange == request->getType())
7789 {
7790 // Clients are not allowed to cancel when kIOPMSkipAskPowerDown
7791 // flag is set. Only root domain will set this flag.
7792 // However, there is one exception to this rule. User-space PM
7793 // policy may choose to cancel sleep even after all clients have
7794 // been notified that we will lower power.
7795
7796 if ((fMachineState == kIOPM_OurChangeTellUserPMPolicyPowerDown)
7797 || (fMachineState == kIOPM_OurChangeTellPriorityClientsPowerDown)
7798 || ((fHeadNoteChangeFlags & kIOPMSkipAskPowerDown) == 0))
7799 {
7800 fDoNotPowerDown = true;
7801
7802 OSString * name = (OSString *) request->fArg2;
7803 getPMRootDomain()->pmStatsRecordApplicationResponse(
7804 gIOPMStatsApplicationResponseCancel,
7805 name ? name->getCStringNoCopy() : "", 0,
7806 0, (int)(uintptr_t) request->fArg1, 0);
7807 }
7808 }
7809
7810 if (checkForDone())
7811 {
7812 stop_ack_timer();
7813 cleanClientResponses(false);
7814 more = true;
7815 }
7816 }
7817 // OSString containing app name in Arg2 must be released.
7818 if (request->getType() == kIOPMRequestTypeCancelPowerChange)
7819 {
7820 OSObject * obj = (OSObject *) request->fArg2;
7821 if (obj) obj->release();
7822 }
7823 break;
7824
7825 case kIOPMRequestTypeAckPowerChange:
7826 more = handleAcknowledgePowerChange( request );
7827 break;
7828
7829 case kIOPMRequestTypeAckSetPowerState:
7830 if (fDriverTimer == -1)
7831 {
7832 // driver acked while setPowerState() call is in-flight.
7833 // take this ack, return value from setPowerState() is irrelevant.
7834 OUR_PMLog(kPMLogDriverAcknowledgeSet,
7835 (uintptr_t) this, fDriverTimer);
7836 fDriverTimer = 0;
7837 }
7838 else if (fDriverTimer > 0)
7839 {
7840 // expected ack, stop the timer
7841 stop_ack_timer();
7842
7843#if LOG_SETPOWER_TIMES
7844 uint64_t nsec = computeTimeDeltaNS(&fDriverCallStartTime);
7845 if (nsec > LOG_SETPOWER_TIMES) {
7846 getPMRootDomain()->pmStatsRecordApplicationResponse(
7847 gIOPMStatsDriverPSChangeSlow,
7848 fName, kDriverCallSetPowerState, NS_TO_MS(nsec), 0, NULL, fHeadNotePowerState);
7849 }
7850#endif
7851 OUR_PMLog(kPMLogDriverAcknowledgeSet, (uintptr_t) this, fDriverTimer);
7852 fDriverTimer = 0;
7853 more = true;
7854 }
7855 else
7856 {
7857 // unexpected ack
7858 OUR_PMLog(kPMLogAcknowledgeErr4, (uintptr_t) this, 0);
7859 }
7860 break;
7861
7862 case kIOPMRequestTypeInterestChanged:
7863 handleInterestChanged( request );
7864 more = true;
7865 break;
7866
7867 case kIOPMRequestTypeIdleCancel:
7868 if ((fMachineState == kIOPM_OurChangeTellClientsPowerDown)
7869 || (fMachineState == kIOPM_OurChangeTellUserPMPolicyPowerDown)
7870 || (fMachineState == kIOPM_OurChangeTellPriorityClientsPowerDown)
7871 || (fMachineState == kIOPM_SyncTellClientsPowerDown)
7872 || (fMachineState == kIOPM_SyncTellPriorityClientsPowerDown))
7873 {
7874 OUR_PMLog(kPMLogIdleCancel, (uintptr_t) this, fMachineState);
7875 PM_LOG2("%s: cancel from machine state %d\n",
7876 getName(), fMachineState);
7877 fDoNotPowerDown = true;
7878 // Stop waiting for app replys.
7879 if ((fMachineState == kIOPM_OurChangeTellPriorityClientsPowerDown) ||
7880 (fMachineState == kIOPM_OurChangeTellUserPMPolicyPowerDown) ||
7881 (fMachineState == kIOPM_SyncTellPriorityClientsPowerDown) ||
7882 (fMachineState == kIOPM_SyncTellClientsPowerDown) )
7883 cleanClientResponses(false);
7884 more = true;
7885 }
7886 break;
7887
7888 case kIOPMRequestTypeChildNotifyDelayCancel:
7889 if (fMachineState == kIOPM_NotifyChildrenDelayed)
7890 {
7891 PM_LOG2("%s: delay notify cancelled\n", getName());
7892 notifyChildrenDelayed();
7893 }
7894 break;
7895
7896 default:
7897 panic("PMReplyQueue: unknown reply type %x", request->getType());
7898 }
7899
7900 more |= gIOPMCompletionQueue->queuePMRequest(request);
7901 if (more)
7902 gIOPMWorkQueue->incrementProducerCount();
7903
7904 return more;
7905}
7906
7907//*********************************************************************************
7908// [private] assertPMDriverCall / deassertPMDriverCall
7909//*********************************************************************************
7910
7911bool IOService::assertPMDriverCall(
7912 IOPMDriverCallEntry * entry,
7913 IOOptionBits options,
7914 IOPMinformee * inform )
7915{
7916 IOService * target = 0;
7917 bool ok = false;
7918
7919 if (!initialized)
7920 return false;
7921
7922 PM_LOCK();
7923
7924 if (fLockedFlags.PMStop)
7925 {
7926 goto fail;
7927 }
7928
7929 if (((options & kIOPMADC_NoInactiveCheck) == 0) && isInactive())
7930 {
7931 goto fail;
7932 }
7933
7934 if (inform)
7935 {
7936 if (!inform->active)
7937 {
7938 goto fail;
7939 }
7940 target = inform->whatObject;
7941 if (target->isInactive())
7942 {
7943 goto fail;
7944 }
7945 }
7946
7947 entry->thread = current_thread();
7948 entry->target = target;
7949 queue_enter(&fPMDriverCallQueue, entry, IOPMDriverCallEntry *, link);
7950 ok = true;
7951
7952fail:
7953 PM_UNLOCK();
7954
7955 return ok;
7956}
7957
7958void IOService::deassertPMDriverCall( IOPMDriverCallEntry * entry )
7959{
7960 bool wakeup = false;
7961
7962 PM_LOCK();
7963
7964 assert( !queue_empty(&fPMDriverCallQueue) );
7965 queue_remove(&fPMDriverCallQueue, entry, IOPMDriverCallEntry *, link);
7966 if (fLockedFlags.PMDriverCallWait)
7967 {
7968 wakeup = true;
7969 }
7970
7971 PM_UNLOCK();
7972
7973 if (wakeup)
7974 PM_LOCK_WAKEUP(&fPMDriverCallQueue);
7975}
7976
7977void IOService::waitForPMDriverCall( IOService * target )
7978{
7979 const IOPMDriverCallEntry * entry;
7980 thread_t thread = current_thread();
7981 AbsoluteTime deadline;
7982 int waitResult;
7983 bool log = true;
7984 bool wait;
7985
7986 do {
7987 wait = false;
7988 queue_iterate(&fPMDriverCallQueue, entry, const IOPMDriverCallEntry *, link)
7989 {
7990 // Target of interested driver call
7991 if (target && (target != entry->target))
7992 continue;
7993
7994 if (entry->thread == thread)
7995 {
7996 if (log)
7997 {
7998 PM_LOG("%s: %s(%s) on PM thread\n",
7999 fName, __FUNCTION__, target ? target->getName() : "");
8000 OSReportWithBacktrace("%s: %s(%s) on PM thread\n",
8001 fName, __FUNCTION__, target ? target->getName() : "");
8002 log = false;
8003 }
8004 continue;
8005 }
8006
8007 wait = true;
8008 break;
8009 }
8010
8011 if (wait)
8012 {
8013 fLockedFlags.PMDriverCallWait = true;
8014 clock_interval_to_deadline(15, kSecondScale, &deadline);
8015 waitResult = PM_LOCK_SLEEP(&fPMDriverCallQueue, deadline);
8016 fLockedFlags.PMDriverCallWait = false;
8017 if (THREAD_TIMED_OUT == waitResult)
8018 {
8019 PM_ERROR("%s: waitForPMDriverCall timeout\n", fName);
8020 wait = false;
8021 }
8022 }
8023 } while (wait);
8024}
8025
8026//*********************************************************************************
8027// [private] Debug helpers
8028//*********************************************************************************
8029
8030const char * IOService::getIOMessageString( uint32_t msg )
8031{
8032#define MSG_ENTRY(x) {(int) x, #x}
8033
8034 static const IONamedValue msgNames[] = {
8035 MSG_ENTRY( kIOMessageCanDevicePowerOff ),
8036 MSG_ENTRY( kIOMessageDeviceWillPowerOff ),
8037 MSG_ENTRY( kIOMessageDeviceWillNotPowerOff ),
8038 MSG_ENTRY( kIOMessageDeviceHasPoweredOn ),
8039 MSG_ENTRY( kIOMessageCanSystemPowerOff ),
8040 MSG_ENTRY( kIOMessageSystemWillPowerOff ),
8041 MSG_ENTRY( kIOMessageSystemWillNotPowerOff ),
8042 MSG_ENTRY( kIOMessageCanSystemSleep ),
8043 MSG_ENTRY( kIOMessageSystemWillSleep ),
8044 MSG_ENTRY( kIOMessageSystemWillNotSleep ),
8045 MSG_ENTRY( kIOMessageSystemHasPoweredOn ),
8046 MSG_ENTRY( kIOMessageSystemWillRestart ),
8047 MSG_ENTRY( kIOMessageSystemWillPowerOn ),
8048 MSG_ENTRY( kIOMessageSystemCapabilityChange ),
8049 MSG_ENTRY( kIOPMMessageLastCallBeforeSleep )
8050 };
8051
8052 return IOFindNameForValue(msg, msgNames);
8053}
8054
8055
8056// MARK: -
8057// MARK: IOPMRequest
8058
8059//*********************************************************************************
8060// IOPMRequest Class
8061//
8062// Requests from PM clients, and also used for inter-object messaging within PM.
8063//*********************************************************************************
8064
8065OSDefineMetaClassAndStructors( IOPMRequest, IOCommand );
8066
8067IOPMRequest * IOPMRequest::create( void )
8068{
8069 IOPMRequest * me = OSTypeAlloc(IOPMRequest);
8070 if (me && !me->init(0, kIOPMRequestTypeInvalid))
8071 {
8072 me->release();
8073 me = 0;
8074 }
8075 return me;
8076}
8077
8078bool IOPMRequest::init( IOService * target, IOOptionBits type )
8079{
8080 if (!IOCommand::init())
8081 return false;
8082
8083 fRequestType = type;
8084 fTarget = target;
8085
8086 if (fTarget)
8087 fTarget->retain();
8088
8089 // Root node and root domain requests does not prevent the power tree from
8090 // becoming quiescent.
8091
8092 fIsQuiesceBlocker = ((fTarget != gIOPMRootNode) &&
8093 (fTarget != IOService::getPMRootDomain()));
8094
8095 return true;
8096}
8097
8098void IOPMRequest::reset( void )
8099{
8100 assert( fWorkWaitCount == 0 );
8101 assert( fFreeWaitCount == 0 );
8102
8103 detachNextRequest();
8104 detachRootRequest();
8105
8106 if (fCompletionAction && (fRequestType == kIOPMRequestTypeQuiescePowerTree))
8107 {
8108 // Call the completion on PM work loop context
8109 fCompletionAction(fCompletionTarget, fCompletionParam);
8110 fCompletionAction = 0;
8111 }
8112
8113 fRequestType = kIOPMRequestTypeInvalid;
8114
8115 if (fTarget)
8116 {
8117 fTarget->release();
8118 fTarget = 0;
8119 }
8120}
8121
8122bool IOPMRequest::attachNextRequest( IOPMRequest * next )
8123{
8124 bool ok = false;
8125
8126 if (!fRequestNext)
8127 {
8128 // Postpone the execution of the next request after
8129 // this request.
8130 fRequestNext = next;
8131 fRequestNext->fWorkWaitCount++;
8132#if LOG_REQUEST_ATTACH
8133 PM_LOG("Attached next: %p [0x%x] -> %p [0x%x, %u] %s\n",
8134 OBFUSCATE(this), fRequestType, OBFUSCATE(fRequestNext),
8135 fRequestNext->fRequestType,
8136 (uint32_t) fRequestNext->fWorkWaitCount,
8137 fTarget->getName());
8138#endif
8139 ok = true;
8140 }
8141 return ok;
8142}
8143
8144bool IOPMRequest::detachNextRequest( void )
8145{
8146 bool ok = false;
8147
8148 if (fRequestNext)
8149 {
8150 assert(fRequestNext->fWorkWaitCount);
8151 if (fRequestNext->fWorkWaitCount)
8152 fRequestNext->fWorkWaitCount--;
8153#if LOG_REQUEST_ATTACH
8154 PM_LOG("Detached next: %p [0x%x] -> %p [0x%x, %u] %s\n",
8155 OBFUSCATE(this), fRequestType, OBFUSCATE(fRequestNext),
8156 fRequestNext->fRequestType,
8157 (uint32_t) fRequestNext->fWorkWaitCount,
8158 fTarget->getName());
8159#endif
8160 fRequestNext = 0;
8161 ok = true;
8162 }
8163 return ok;
8164}
8165
8166bool IOPMRequest::attachRootRequest( IOPMRequest * root )
8167{
8168 bool ok = false;
8169
8170 if (!fRequestRoot)
8171 {
8172 // Delay the completion of the root request after
8173 // this request.
8174 fRequestRoot = root;
8175 fRequestRoot->fFreeWaitCount++;
8176#if LOG_REQUEST_ATTACH
8177 PM_LOG("Attached root: %p [0x%x] -> %p [0x%x, %u] %s\n",
8178 OBFUSCATE(this), (uint32_t) fType, OBFUSCATE(fRequestRoot),
8179 (uint32_t) fRequestRoot->fType,
8180 (uint32_t) fRequestRoot->fFreeWaitCount,
8181 fTarget->getName());
8182#endif
8183 ok = true;
8184 }
8185 return ok;
8186}
8187
8188bool IOPMRequest::detachRootRequest( void )
8189{
8190 bool ok = false;
8191
8192 if (fRequestRoot)
8193 {
8194 assert(fRequestRoot->fFreeWaitCount);
8195 if (fRequestRoot->fFreeWaitCount)
8196 fRequestRoot->fFreeWaitCount--;
8197#if LOG_REQUEST_ATTACH
8198 PM_LOG("Detached root: %p [0x%x] -> %p [0x%x, %u] %s\n",
8199 OBFUSCATE(this), (uint32_t) fType, OBFUSCATE(fRequestRoot),
8200 (uint32_t) fRequestRoot->fType,
8201 (uint32_t) fRequestRoot->fFreeWaitCount,
8202 fTarget->getName());
8203#endif
8204 fRequestRoot = 0;
8205 ok = true;
8206 }
8207 return ok;
8208}
8209
8210// MARK: -
8211// MARK: IOPMRequestQueue
8212
8213//*********************************************************************************
8214// IOPMRequestQueue Class
8215//
8216// Global queues. Queues are created once and never released.
8217//*********************************************************************************
8218
8219OSDefineMetaClassAndStructors( IOPMRequestQueue, IOEventSource );
8220
8221IOPMRequestQueue * IOPMRequestQueue::create( IOService * inOwner, Action inAction )
8222{
8223 IOPMRequestQueue * me = OSTypeAlloc(IOPMRequestQueue);
8224 if (me && !me->init(inOwner, inAction))
8225 {
8226 me->release();
8227 me = 0;
8228 }
8229 return me;
8230}
8231
8232bool IOPMRequestQueue::init( IOService * inOwner, Action inAction )
8233{
8234 if (!inAction || !IOEventSource::init(inOwner, (IOEventSourceAction)inAction))
8235 return false;
8236
8237 queue_init(&fQueue);
8238 fLock = IOLockAlloc();
8239 return (fLock != 0);
8240}
8241
8242void IOPMRequestQueue::free( void )
8243{
8244 if (fLock)
8245 {
8246 IOLockFree(fLock);
8247 fLock = 0;
8248 }
8249 return IOEventSource::free();
8250}
8251
8252void IOPMRequestQueue::queuePMRequest( IOPMRequest * request )
8253{
8254 assert(request);
8255 IOLockLock(fLock);
8256 queue_enter(&fQueue, request, typeof(request), fCommandChain);
8257 IOLockUnlock(fLock);
8258 if (workLoop) signalWorkAvailable();
8259}
8260
8261void
8262IOPMRequestQueue::queuePMRequestChain( IOPMRequest ** requests, IOItemCount count )
8263{
8264 IOPMRequest * next;
8265
8266 assert(requests && count);
8267 IOLockLock(fLock);
8268 while (count--)
8269 {
8270 next = *requests;
8271 requests++;
8272 queue_enter(&fQueue, next, typeof(next), fCommandChain);
8273 }
8274 IOLockUnlock(fLock);
8275 if (workLoop) signalWorkAvailable();
8276}
8277
8278bool IOPMRequestQueue::checkForWork( void )
8279{
8280 Action dqAction = (Action) action;
8281 IOPMRequest * request;
8282 IOService * target;
8283 int dequeueCount = 0;
8284 bool more = false;
8285
8286 IOLockLock( fLock );
8287
8288 while (!queue_empty(&fQueue))
8289 {
8290 if (dequeueCount++ >= kMaxDequeueCount)
8291 {
8292 // Allow other queues a chance to work
8293 more = true;
8294 break;
8295 }
8296
8297 queue_remove_first(&fQueue, request, typeof(request), fCommandChain);
8298 IOLockUnlock(fLock);
8299 target = request->getTarget();
8300 assert(target);
8301 more |= (*dqAction)( target, request, this );
8302 IOLockLock( fLock );
8303 }
8304
8305 IOLockUnlock( fLock );
8306 return more;
8307}
8308
8309// MARK: -
8310// MARK: IOPMWorkQueue
8311
8312//*********************************************************************************
8313// IOPMWorkQueue Class
8314//
8315// Queue of IOServicePM objects, each with a queue of IOPMRequest sharing the
8316// same target.
8317//*********************************************************************************
8318
8319OSDefineMetaClassAndStructors( IOPMWorkQueue, IOEventSource );
8320
8321IOPMWorkQueue *
8322IOPMWorkQueue::create( IOService * inOwner, Action invoke, Action retire )
8323{
8324 IOPMWorkQueue * me = OSTypeAlloc(IOPMWorkQueue);
8325 if (me && !me->init(inOwner, invoke, retire))
8326 {
8327 me->release();
8328 me = 0;
8329 }
8330 return me;
8331}
8332
8333bool IOPMWorkQueue::init( IOService * inOwner, Action invoke, Action retire )
8334{
8335 if (!invoke || !retire ||
8336 !IOEventSource::init(inOwner, (IOEventSourceAction)0))
8337 return false;
8338
8339 queue_init(&fWorkQueue);
8340
8341 fInvokeAction = invoke;
8342 fRetireAction = retire;
8343 fConsumerCount = fProducerCount = 0;
8344
8345 return true;
8346}
8347
8348bool IOPMWorkQueue::queuePMRequest( IOPMRequest * request, IOServicePM * pwrMgt )
8349{
8350 queue_head_t * requestQueue;
8351 bool more = false;
8352 bool empty;
8353
8354 assert( request );
8355 assert( pwrMgt );
8356 assert( onThread() );
8357 assert( queue_next(&request->fCommandChain) ==
8358 queue_prev(&request->fCommandChain) );
8359
8360 gIOPMBusyRequestCount++;
8361
8362 if (request->isQuiesceType())
8363 {
8364 if ((request->getTarget() == gIOPMRootNode) && !fQuiesceStartTime)
8365 {
8366 // Attach new quiesce request to all quiesce blockers in the queue
8367 fQuiesceStartTime = mach_absolute_time();
8368 attachQuiesceRequest(request);
8369 fQuiesceRequest = request;
8370 }
8371 }
8372 else if (fQuiesceRequest && request->isQuiesceBlocker())
8373 {
8374 // Attach the new quiesce blocker to the blocked quiesce request
8375 request->attachNextRequest(fQuiesceRequest);
8376 }
8377
8378 // Add new request to the tail of the per-service request queue.
8379 // Then immediately check the request queue to minimize latency
8380 // if the queue was empty.
8381
8382 requestQueue = &pwrMgt->RequestHead;
8383 empty = queue_empty(requestQueue);
8384 queue_enter(requestQueue, request, typeof(request), fCommandChain);
8385 if (empty)
8386 {
8387 more = checkRequestQueue(requestQueue, &empty);
8388 if (!empty)
8389 {
8390 // Request just added is blocked, add its target IOServicePM
8391 // to the work queue.
8392 assert( queue_next(&pwrMgt->WorkChain) ==
8393 queue_prev(&pwrMgt->WorkChain) );
8394
8395 queue_enter(&fWorkQueue, pwrMgt, typeof(pwrMgt), WorkChain);
8396 fQueueLength++;
8397 PM_LOG3("IOPMWorkQueue: [%u] added %s@%p to queue\n",
8398 fQueueLength, pwrMgt->Name, OBFUSCATE(pwrMgt));
8399 }
8400 }
8401
8402 return more;
8403}
8404
8405bool IOPMWorkQueue::checkRequestQueue( queue_head_t * requestQueue, bool * empty )
8406{
8407 IOPMRequest * request;
8408 IOService * target;
8409 bool more = false;
8410 bool done = false;
8411
8412 assert(!queue_empty(requestQueue));
8413 do {
8414 request = (typeof(request)) queue_first(requestQueue);
8415 if (request->isWorkBlocked())
8416 break; // request dispatch blocked on attached request
8417
8418 target = request->getTarget();
8419 if (fInvokeAction)
8420 {
8421 done = (*fInvokeAction)( target, request, this );
8422 }
8423 else
8424 {
8425 PM_LOG("PM request 0x%x dropped\n", request->getType());
8426 done = true;
8427 }
8428 if (!done)
8429 break; // PM state machine blocked
8430
8431 assert(gIOPMBusyRequestCount > 0);
8432 if (gIOPMBusyRequestCount)
8433 gIOPMBusyRequestCount--;
8434
8435 if (request == fQuiesceRequest)
8436 {
8437 fQuiesceRequest = 0;
8438 }
8439
8440 queue_remove_first(requestQueue, request, typeof(request), fCommandChain);
8441 more |= (*fRetireAction)( target, request, this );
8442 done = queue_empty(requestQueue);
8443 } while (!done);
8444
8445 *empty = done;
8446
8447 if (more)
8448 {
8449 // Retired a request that may unblock a previously visited request
8450 // that is still waiting on the work queue. Must trigger another
8451 // queue check.
8452 fProducerCount++;
8453 }
8454
8455 return more;
8456}
8457
8458bool IOPMWorkQueue::checkForWork( void )
8459{
8460 IOServicePM * entry;
8461 IOServicePM * next;
8462 bool more = false;
8463 bool empty;
8464
8465#if WORK_QUEUE_STATS
8466 fStatCheckForWork++;
8467#endif
8468
8469 // Iterate over all IOServicePM entries in the work queue,
8470 // and check each entry's request queue.
8471
8472 while (fConsumerCount != fProducerCount)
8473 {
8474 PM_LOG3("IOPMWorkQueue: checkForWork %u %u\n",
8475 fProducerCount, fConsumerCount);
8476
8477 fConsumerCount = fProducerCount;
8478
8479#if WORK_QUEUE_STATS
8480 if (queue_empty(&fWorkQueue))
8481 {
8482 fStatQueueEmpty++;
8483 break;
8484 }
8485 fStatScanEntries++;
8486 uint32_t cachedWorkCount = gIOPMWorkInvokeCount;
8487#endif
8488
8489 __IGNORE_WCASTALIGN(entry = (typeof(entry)) queue_first(&fWorkQueue));
8490 while (!queue_end(&fWorkQueue, (queue_entry_t) entry))
8491 {
8492 more |= checkRequestQueue(&entry->RequestHead, &empty);
8493
8494 // Get next entry, points to head if current entry is last.
8495 __IGNORE_WCASTALIGN(next = (typeof(next)) queue_next(&entry->WorkChain));
8496
8497 // if request queue is empty, remove IOServicePM from work queue.
8498 if (empty)
8499 {
8500 assert(fQueueLength);
8501 if (fQueueLength) fQueueLength--;
8502 PM_LOG3("IOPMWorkQueue: [%u] removed %s@%p from queue\n",
8503 fQueueLength, entry->Name, OBFUSCATE(entry));
8504 queue_remove(&fWorkQueue, entry, typeof(entry), WorkChain);
8505 }
8506 entry = next;
8507 }
8508
8509#if WORK_QUEUE_STATS
8510 if (cachedWorkCount == gIOPMWorkInvokeCount)
8511 fStatNoWorkDone++;
8512#endif
8513 }
8514
8515 return more;
8516}
8517
8518void IOPMWorkQueue::signalWorkAvailable( void )
8519{
8520 fProducerCount++;
8521 IOEventSource::signalWorkAvailable();
8522}
8523
8524void IOPMWorkQueue::incrementProducerCount( void )
8525{
8526 fProducerCount++;
8527}
8528
8529void IOPMWorkQueue::attachQuiesceRequest( IOPMRequest * quiesceRequest )
8530{
8531 IOServicePM * entry;
8532 IOPMRequest * request;
8533
8534 if (queue_empty(&fWorkQueue))
8535 {
8536 return;
8537 }
8538
8539 queue_iterate(&fWorkQueue, entry, typeof(entry), WorkChain)
8540 {
8541 queue_iterate(&entry->RequestHead, request, typeof(request), fCommandChain)
8542 {
8543 // Attach the quiesce request to any request in the queue that
8544 // is not linked to a next request. These requests will block
8545 // the quiesce request.
8546
8547 if (request->isQuiesceBlocker())
8548 {
8549 request->attachNextRequest(quiesceRequest);
8550 }
8551 }
8552 }
8553}
8554
8555void IOPMWorkQueue::finishQuiesceRequest( IOPMRequest * quiesceRequest )
8556{
8557 if (fQuiesceRequest && (quiesceRequest == fQuiesceRequest) &&
8558 (fQuiesceStartTime != 0))
8559 {
8560 fInvokeAction = 0;
8561 fQuiesceFinishTime = mach_absolute_time();
8562 }
8563}
8564
8565// MARK: -
8566// MARK: IOPMCompletionQueue
8567
8568//*********************************************************************************
8569// IOPMCompletionQueue Class
8570//*********************************************************************************
8571
8572OSDefineMetaClassAndStructors( IOPMCompletionQueue, IOEventSource );
8573
8574IOPMCompletionQueue *
8575IOPMCompletionQueue::create( IOService * inOwner, Action inAction )
8576{
8577 IOPMCompletionQueue * me = OSTypeAlloc(IOPMCompletionQueue);
8578 if (me && !me->init(inOwner, inAction))
8579 {
8580 me->release();
8581 me = 0;
8582 }
8583 return me;
8584}
8585
8586bool IOPMCompletionQueue::init( IOService * inOwner, Action inAction )
8587{
8588 if (!inAction || !IOEventSource::init(inOwner, (IOEventSourceAction)inAction))
8589 return false;
8590
8591 queue_init(&fQueue);
8592 return true;
8593}
8594
8595bool IOPMCompletionQueue::queuePMRequest( IOPMRequest * request )
8596{
8597 bool more;
8598
8599 assert(request);
8600 // unblock dependent request
8601 more = request->detachNextRequest();
8602 queue_enter(&fQueue, request, typeof(request), fCommandChain);
8603 return more;
8604}
8605
8606bool IOPMCompletionQueue::checkForWork( void )
8607{
8608 Action dqAction = (Action) action;
8609 IOPMRequest * request;
8610 IOPMRequest * next;
8611 IOService * target;
8612 bool more = false;
8613
8614 request = (typeof(request)) queue_first(&fQueue);
8615 while (!queue_end(&fQueue, (queue_entry_t) request))
8616 {
8617 next = (typeof(next)) queue_next(&request->fCommandChain);
8618 if (!request->isFreeBlocked())
8619 {
8620 queue_remove(&fQueue, request, typeof(request), fCommandChain);
8621 target = request->getTarget();
8622 assert(target);
8623 more |= (*dqAction)( target, request, this );
8624 }
8625 request = next;
8626 }
8627
8628 return more;
8629}
8630
8631// MARK: -
8632// MARK: IOServicePM
8633
8634OSDefineMetaClassAndStructors(IOServicePM, OSObject)
8635
8636//*********************************************************************************
8637// serialize
8638//
8639// Serialize IOServicePM for debugging.
8640//*********************************************************************************
8641
8642static void
8643setPMProperty( OSDictionary * dict, const char * key, uint64_t value )
8644{
8645 OSNumber * num = OSNumber::withNumber(value, sizeof(value) * 8);
8646 if (num)
8647 {
8648 dict->setObject(key, num);
8649 num->release();
8650 }
8651}
8652
8653IOReturn IOServicePM::gatedSerialize( OSSerialize * s ) const
8654{
8655 OSDictionary * dict;
8656 bool ok = false;
8657 int powerClamp = -1;
8658 int dictSize = 6;
8659
8660 if (IdleTimerPeriod)
8661 dictSize += 4;
8662
8663 if (PMActions.parameter & kPMActionsFlagLimitPower)
8664 {
8665 dictSize += 1;
8666 powerClamp = 0;
8667 if (PMActions.parameter &
8668 (kPMActionsFlagIsDisplayWrangler | kPMActionsFlagIsGraphicsDevice))
8669 powerClamp++;
8670 }
8671
8672#if WORK_QUEUE_STATS
8673 if (gIOPMRootNode == ControllingDriver)
8674 dictSize += 4;
8675#endif
8676
8677 if (PowerClients)
8678 dict = OSDictionary::withDictionary(
8679 PowerClients, PowerClients->getCount() + dictSize);
8680 else
8681 dict = OSDictionary::withCapacity(dictSize);
8682
8683 if (dict)
8684 {
8685 setPMProperty(dict, "CurrentPowerState", CurrentPowerState);
8686 setPMProperty(dict, "CapabilityFlags", CurrentCapabilityFlags);
8687 if (NumberOfPowerStates)
8688 setPMProperty(dict, "MaxPowerState", NumberOfPowerStates-1);
8689 if (DesiredPowerState != CurrentPowerState)
8690 setPMProperty(dict, "DesiredPowerState", DesiredPowerState);
8691 if (kIOPM_Finished != MachineState)
8692 setPMProperty(dict, "MachineState", MachineState);
8693 if (DeviceOverrideEnabled)
8694 dict->setObject("PowerOverrideOn", kOSBooleanTrue);
8695 if (powerClamp >= 0)
8696 setPMProperty(dict, "PowerClamp", powerClamp);
8697
8698 if (IdleTimerPeriod)
8699 {
8700 AbsoluteTime now;
8701 AbsoluteTime delta;
8702 uint64_t nsecs;
8703
8704 clock_get_uptime(&now);
8705
8706 // The idle timer period in milliseconds
8707 setPMProperty(dict, "IdleTimerPeriod", NextIdleTimerPeriod * 1000ULL);
8708
8709 // Number of tickles since the last idle timer expiration
8710 setPMProperty(dict, "ActivityTickles", ActivityTickleCount);
8711
8712 if (AbsoluteTime_to_scalar(&DeviceActiveTimestamp))
8713 {
8714 // Milliseconds since the last activity tickle
8715 delta = now;
8716 SUB_ABSOLUTETIME(&delta, &DeviceActiveTimestamp);
8717 absolutetime_to_nanoseconds(delta, &nsecs);
8718 setPMProperty(dict, "TimeSinceLastTickle", NS_TO_MS(nsecs));
8719 }
8720
8721 if (!IdleTimerStopped && AbsoluteTime_to_scalar(&IdleTimerStartTime))
8722 {
8723 // Idle timer elapsed time in milliseconds
8724 delta = now;
8725 SUB_ABSOLUTETIME(&delta, &IdleTimerStartTime);
8726 absolutetime_to_nanoseconds(delta, &nsecs);
8727 setPMProperty(dict, "IdleTimerElapsedTime", NS_TO_MS(nsecs));
8728 }
8729 }
8730
8731#if WORK_QUEUE_STATS
8732 if (gIOPMRootNode == Owner)
8733 {
8734 setPMProperty(dict, "WQ-CheckForWork",
8735 gIOPMWorkQueue->fStatCheckForWork);
8736 setPMProperty(dict, "WQ-ScanEntries",
8737 gIOPMWorkQueue->fStatScanEntries);
8738 setPMProperty(dict, "WQ-QueueEmpty",
8739 gIOPMWorkQueue->fStatQueueEmpty);
8740 setPMProperty(dict, "WQ-NoWorkDone",
8741 gIOPMWorkQueue->fStatNoWorkDone);
8742 }
8743#endif
8744
8745 if (HasAdvisoryDesire && !gIOPMAdvisoryTickleEnabled)
8746 {
8747 // Don't report advisory tickle when it has no influence
8748 dict->removeObject(gIOPMPowerClientAdvisoryTickle);
8749 }
8750
8751 ok = dict->serialize(s);
8752 dict->release();
8753 }
8754
8755 return (ok ? kIOReturnSuccess : kIOReturnNoMemory);
8756}
8757
8758bool IOServicePM::serialize( OSSerialize * s ) const
8759{
8760 IOReturn ret = kIOReturnNotReady;
8761
8762 if (gIOPMWatchDogThread == current_thread())
8763 {
8764 // Calling without lock as this data is collected for debug purpose, before reboot.
8765 // The workloop is probably already hung in state machine.
8766 ret = gatedSerialize(s);
8767 }
8768 else if (gIOPMWorkLoop)
8769 {
8770 ret = gIOPMWorkLoop->runAction(
8771 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOServicePM::gatedSerialize),
8772 (OSObject *) this, (void *) s);
8773 }
8774
8775 return (kIOReturnSuccess == ret);
8776}
8777
8778void IOServicePM::pmPrint(
8779 uint32_t event,
8780 uintptr_t param1,
8781 uintptr_t param2 ) const
8782{
8783 gPlatform->PMLog(Name, event, param1, param2);
8784}
8785
8786void IOServicePM::pmTrace(
8787 uint32_t event,
8788 uintptr_t param1,
8789 uintptr_t param2 ) const
8790{
8791 const char * who = Name;
8792 uint64_t regId = Owner->getRegistryEntryID();
8793 uintptr_t name = 0;
8794
8795 static const uint32_t sStartStopBitField[] =
8796 { 0x00000000, 0x00000040 }; // Only Program Hardware so far
8797
8798 // Arcane formula from Hacker's Delight by Warren
8799 // abs(x) = ((int) x >> 31) ^ (x + ((int) x >> 31))
8800 uint32_t sgnevent = ((int) event >> 31);
8801 uint32_t absevent = sgnevent ^ (event + sgnevent);
8802 uint32_t code = IODBG_POWER(absevent);
8803
8804 uint32_t bit = 1 << (absevent & 0x1f);
8805 if ((absevent < (sizeof(sStartStopBitField) * 8)) &&
8806 (sStartStopBitField[absevent >> 5] & bit))
8807 {
8808 // Or in the START or END bits, Start = 1 & END = 2
8809 // If sgnevent == 0 then START - 0 => START
8810 // else if sgnevent == -1 then START - -1 => END
8811 code |= DBG_FUNC_START - sgnevent;
8812 }
8813
8814 // Copy the first characters of the name into an uintptr_t
8815 for (uint32_t i = 0; (i < sizeof(uintptr_t) && who[i] != 0); i++)
8816 {
8817 ((char *) &name)[sizeof(uintptr_t) - i - 1] = who[i];
8818 }
8819
8820 IOTimeStampConstant(code, name, (uintptr_t) regId, param1, param2);
8821}
8822