+ }
+
+ request->fArg0 = (void *) ignore;
+ submitPMRequest( request );
+
+ return kIOReturnSuccess;
+}
+
+//******************************************************************************
+// [public] nextIdleTimeout
+//
+// Returns how many "seconds from now" the device should idle into its
+// next lowest power state.
+//******************************************************************************
+
+SInt32
+IOService::nextIdleTimeout(
+ AbsoluteTime currentTime,
+ AbsoluteTime lastActivity,
+ unsigned int powerState)
+{
+ AbsoluteTime delta;
+ UInt64 delta_ns;
+ SInt32 delta_secs;
+ SInt32 delay_secs;
+
+ // Calculate time difference using funky macro from clock.h.
+ delta = currentTime;
+ SUB_ABSOLUTETIME(&delta, &lastActivity);
+
+ // Figure it in seconds.
+ absolutetime_to_nanoseconds(delta, &delta_ns);
+ delta_secs = (SInt32)(delta_ns / NSEC_PER_SEC);
+
+ // Be paranoid about delta somehow exceeding timer period.
+ if (delta_secs < (int) fIdleTimerPeriod) {
+ delay_secs = (int) fIdleTimerPeriod - delta_secs;
+ } else {
+ delay_secs = (int) fIdleTimerPeriod;
+ }
+
+ return (SInt32)delay_secs;
+}
+
+//*********************************************************************************
+// [public] start_PM_idle_timer
+//*********************************************************************************
+
+void
+IOService::start_PM_idle_timer( void )
+{
+ static const int maxTimeout = 100000;
+ static const int minTimeout = 1;
+ AbsoluteTime uptime, deadline;
+ SInt32 idle_in = 0;
+ boolean_t pending;
+
+ if (!initialized || !fIdleTimerPeriod) {
+ return;
+ }
+
+ IOLockLock(fActivityLock);
+
+ clock_get_uptime(&uptime);
+
+ // Subclasses may modify idle sleep algorithm
+ idle_in = nextIdleTimeout(uptime, fDeviceActiveTimestamp, fCurrentPowerState);
+
+ // Check for out-of range responses
+ if (idle_in > maxTimeout) {
+ // use standard implementation
+ idle_in = IOService::nextIdleTimeout(uptime,
+ fDeviceActiveTimestamp,
+ fCurrentPowerState);
+ } else if (idle_in < minTimeout) {
+ idle_in = fIdleTimerPeriod;
+ }
+
+ IOLockUnlock(fActivityLock);
+
+ fNextIdleTimerPeriod = idle_in;
+ fIdleTimerStartTime = uptime;
+
+ retain();
+ clock_interval_to_absolutetime_interval(idle_in, kSecondScale, &deadline);
+ ADD_ABSOLUTETIME(&deadline, &uptime);
+ pending = thread_call_enter_delayed(fIdleTimer, deadline);
+ if (pending) {
+ release();
+ }
+}
+
+//*********************************************************************************
+// [private] restartIdleTimer
+//*********************************************************************************
+
+void
+IOService::restartIdleTimer( void )
+{
+ if (fDeviceDesire != kPowerStateZero) {
+ fIdleTimerStopped = false;
+ fActivityTickleCount = 0;
+ start_PM_idle_timer();
+ } else if (fHasAdvisoryDesire) {
+ fIdleTimerStopped = false;
+ start_PM_idle_timer();
+ } else {
+ fIdleTimerStopped = true;
+ }
+}
+
+//*********************************************************************************
+// idle_timer_expired
+//*********************************************************************************
+
+static void
+idle_timer_expired(
+ thread_call_param_t arg0, thread_call_param_t arg1 )
+{
+ IOService * me = (IOService *) arg0;
+
+ if (gIOPMWorkLoop) {
+ gIOPMWorkLoop->runAction(
+ OSMemberFunctionCast(IOWorkLoop::Action, me,
+ &IOService::idleTimerExpired),
+ me);
+ }
+
+ me->release();
+}
+
+//*********************************************************************************
+// [private] idleTimerExpired
+//
+// The idle timer has expired. If there has been activity since the last
+// expiration, just restart the timer and return. If there has not been
+// activity, switch to the next lower power state and restart the timer.
+//*********************************************************************************
+
+void
+IOService::idleTimerExpired( void )
+{
+ IOPMRequest * request;
+ bool restartTimer = true;
+ uint32_t tickleFlags;
+
+ if (!initialized || !fIdleTimerPeriod || fIdleTimerStopped ||
+ fLockedFlags.PMStop) {
+ return;
+ }
+
+ fIdleTimerStartTime = 0;
+
+ IOLockLock(fActivityLock);
+
+ // Check for device activity (tickles) over last timer period.
+
+ if (fDeviceWasActive) {
+ // Device was active - do not drop power, restart timer.
+ fDeviceWasActive = false;
+ } else if (!fIdleTimerIgnored) {
+ // No device activity - drop power state by one level.
+ // Decrement the cached tickle power state when possible.
+ // This value may be kInvalidTicklePowerState before activityTickle()
+ // is called, but the power drop request must be issued regardless.
+
+ if ((fActivityTicklePowerState != kInvalidTicklePowerState) &&
+ (fActivityTicklePowerState != kPowerStateZero)) {
+ fActivityTicklePowerState--;
+ }
+
+ tickleFlags = kTickleTypeActivity | kTickleTypePowerDrop;
+ request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
+ if (request) {
+ request->fArg0 = (void *)(uintptr_t) fIdleTimerGeneration;
+ request->fArg1 = (void *)(uintptr_t) tickleFlags;
+ request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
+ submitPMRequest( request );
+
+ // Do not restart timer until after the tickle request has been
+ // processed.
+
+ restartTimer = false;
+ }
+ }
+
+ if (fAdvisoryTickled) {
+ fAdvisoryTickled = false;
+ } else if (fHasAdvisoryDesire) {
+ // Want new tickles to turn into pm request after we drop the lock
+ fAdvisoryTicklePowerState = kInvalidTicklePowerState;
+
+ tickleFlags = kTickleTypeAdvisory | kTickleTypePowerDrop;
+ request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
+ if (request) {
+ request->fArg0 = (void *)(uintptr_t) fIdleTimerGeneration;
+ request->fArg1 = (void *)(uintptr_t) tickleFlags;
+ request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
+ submitPMRequest( request );
+
+ // Do not restart timer until after the tickle request has been
+ // processed.
+
+ restartTimer = false;
+ }
+ }
+
+ IOLockUnlock(fActivityLock);
+
+ if (restartTimer) {
+ start_PM_idle_timer();
+ }
+}
+
+#ifndef __LP64__
+//*********************************************************************************
+// [deprecated] PM_idle_timer_expiration
+//*********************************************************************************
+
+void
+IOService::PM_idle_timer_expiration( void )
+{
+}
+
+//*********************************************************************************
+// [deprecated] command_received
+//*********************************************************************************
+
+void
+IOService::command_received( void *statePtr, void *, void *, void * )
+{
+}
+#endif /* !__LP64__ */
+
+//*********************************************************************************
+// [public] setAggressiveness
+//
+// Pass on the input parameters to all power domain children. All those which are
+// power domains will pass it on to their children, etc.
+//*********************************************************************************
+
+IOReturn
+IOService::setAggressiveness( unsigned long type, unsigned long newLevel )
+{
+ return kIOReturnSuccess;
+}
+
+//*********************************************************************************
+// [public] getAggressiveness
+//
+// Called by the user client.
+//*********************************************************************************
+
+IOReturn
+IOService::getAggressiveness( unsigned long type, unsigned long * currentLevel )
+{
+ IOPMrootDomain * rootDomain = getPMRootDomain();
+
+ if (!rootDomain) {
+ return kIOReturnNotReady;
+ }
+
+ return rootDomain->getAggressiveness( type, currentLevel );
+}
+
+//*********************************************************************************
+// [public] getPowerState
+//
+//*********************************************************************************
+
+UInt32
+IOService::getPowerState( void )
+{
+ if (!initialized) {
+ return kPowerStateZero;
+ }
+
+ return fCurrentPowerState;
+}
+
+#ifndef __LP64__
+//*********************************************************************************
+// [deprecated] systemWake
+//
+// Pass this to all power domain children. All those which are
+// power domains will pass it on to their children, etc.
+//*********************************************************************************
+
+IOReturn
+IOService::systemWake( void )
+{
+ OSIterator * iter;
+ OSObject * next;
+ IOPowerConnection * connection;
+ IOService * theChild;
+
+ iter = getChildIterator(gIOPowerPlane);
+ if (iter) {
+ while ((next = iter->getNextObject())) {
+ if ((connection = OSDynamicCast(IOPowerConnection, next))) {
+ if (connection->getReadyFlag() == false) {
+ PM_LOG3("[%s] %s: connection not ready\n",
+ getName(), __FUNCTION__);
+ continue;
+ }
+
+ theChild = (IOService *)connection->copyChildEntry(gIOPowerPlane);
+ if (theChild) {
+ theChild->systemWake();
+ theChild->release();
+ }
+ }
+ }
+ iter->release();
+ }
+
+ if (fControllingDriver != NULL) {
+ if (fControllingDriver->didYouWakeSystem()) {
+ makeUsable();
+ }
+ }
+
+ return IOPMNoErr;
+}
+
+//*********************************************************************************
+// [deprecated] temperatureCriticalForZone
+//*********************************************************************************
+
+IOReturn
+IOService::temperatureCriticalForZone( IOService * whichZone )
+{
+ IOService * theParent;
+ IOService * theNub;
+
+ OUR_PMLog(kPMLogCriticalTemp, 0, 0);
+
+ if (inPlane(gIOPowerPlane) && !IS_PM_ROOT) {
+ theNub = (IOService *)copyParentEntry(gIOPowerPlane);
+ if (theNub) {
+ theParent = (IOService *)theNub->copyParentEntry(gIOPowerPlane);
+ theNub->release();
+ if (theParent) {
+ theParent->temperatureCriticalForZone(whichZone);
+ theParent->release();
+ }
+ }
+ }
+ return IOPMNoErr;
+}
+#endif /* !__LP64__ */
+
+// MARK: -
+// MARK: Power Change (Common)
+
+//*********************************************************************************
+// [private] startPowerChange
+//
+// All power state changes starts here.
+//*********************************************************************************
+
+IOReturn
+IOService::startPowerChange(
+ IOPMPowerChangeFlags changeFlags,
+ IOPMPowerStateIndex powerState,
+ IOPMPowerFlags domainFlags,
+ IOPowerConnection * parentConnection,
+ IOPMPowerFlags parentFlags )
+{
+ uint32_t savedPMActionsParam;
+
+ PM_ASSERT_IN_GATE();
+ assert( fMachineState == kIOPM_Finished );
+ assert( powerState < fNumberOfPowerStates );
+
+ if (powerState >= fNumberOfPowerStates) {
+ return IOPMAckImplied;
+ }
+
+ fIsPreChange = true;
+ savedPMActionsParam = fPMActions.parameter;
+ PM_ACTION_2(actionPowerChangeOverride, &powerState, &changeFlags);
+
+ // rdar://problem/55040032
+ // Schedule a power adjustment after removing the power clamp
+ // to inform our power parent(s) about our latest desired domain
+ // power state. For a self-initiated change, let OurChangeStart()
+ // automatically request parent power when necessary.
+ if (!fAdjustPowerScheduled &&
+ ((changeFlags & kIOPMSelfInitiated) == 0) &&
+ ((fPMActions.parameter & kPMActionsFlagLimitPower) == 0) &&
+ ((savedPMActionsParam & kPMActionsFlagLimitPower) != 0)) {
+ IOPMRequest * request = acquirePMRequest(this, kIOPMRequestTypeAdjustPowerState);
+ if (request) {
+ submitPMRequest(request);
+ fAdjustPowerScheduled = true;
+ }
+ }
+
+ if (changeFlags & kIOPMExpireIdleTimer) {
+ // Root domain requested removal of tickle influence
+ if (StateOrder(fDeviceDesire) > StateOrder(powerState)) {
+ // Reset device desire down to the clamped power state
+ updatePowerClient(gIOPMPowerClientDevice, powerState);
+ computeDesiredState(kPowerStateZero, true);
+
+ // Invalidate tickle cache so the next tickle will issue a request
+ IOLockLock(fActivityLock);
+ fDeviceWasActive = false;
+ fActivityTicklePowerState = kInvalidTicklePowerState;
+ IOLockUnlock(fActivityLock);
+
+ fIdleTimerMinPowerState = kPowerStateZero;
+ }
+ }
+
+ // Root domain's override handler may cancel the power change by
+ // setting the kIOPMNotDone flag.
+
+ if (changeFlags & kIOPMNotDone) {
+ return IOPMAckImplied;
+ }
+
+ // Forks to either Driver or Parent initiated power change paths.
+
+ fHeadNoteChangeFlags = changeFlags;
+ fHeadNotePowerState = powerState;
+ fHeadNotePowerArrayEntry = &fPowerStates[powerState];
+ fHeadNoteParentConnection = NULL;
+
+ if (changeFlags & kIOPMSelfInitiated) {
+ if (changeFlags & kIOPMSynchronize) {
+ OurSyncStart();
+ } else {
+ OurChangeStart();
+ }
+ return 0;
+ } else {
+ assert(changeFlags & kIOPMParentInitiated);
+ fHeadNoteDomainFlags = domainFlags;
+ fHeadNoteParentFlags = parentFlags;
+ fHeadNoteParentConnection = parentConnection;
+ return ParentChangeStart();
+ }
+}
+
+//*********************************************************************************
+// [private] notifyInterestedDrivers
+//*********************************************************************************
+
+bool
+IOService::notifyInterestedDrivers( void )
+{
+ IOPMinformee * informee;
+ IOPMinformeeList * list = fInterestedDrivers;
+ DriverCallParam * param;
+ IOItemCount count;
+ IOItemCount skipCnt = 0;
+
+ PM_ASSERT_IN_GATE();
+ assert( fDriverCallParamCount == 0 );
+ assert( fHeadNotePendingAcks == 0 );
+
+ fHeadNotePendingAcks = 0;
+
+ count = list->numberOfItems();
+ if (!count) {
+ goto done; // no interested drivers
+ }
+ // Allocate an array of interested drivers and their return values
+ // for the callout thread. Everything else is still "owned" by the
+ // PM work loop, which can run to process acknowledgePowerChange()
+ // responses.
+
+ param = (DriverCallParam *) fDriverCallParamPtr;
+ if (count > fDriverCallParamSlots) {
+ if (fDriverCallParamSlots) {
+ assert(fDriverCallParamPtr);
+ IODelete(fDriverCallParamPtr, DriverCallParam, fDriverCallParamSlots);
+ fDriverCallParamPtr = NULL;
+ fDriverCallParamSlots = 0;
+ }
+
+ param = IONew(DriverCallParam, count);
+ if (!param) {
+ goto done; // no memory
+ }
+ fDriverCallParamPtr = (void *) param;
+ fDriverCallParamSlots = count;
+ }
+
+ informee = list->firstInList();
+ assert(informee);
+ for (IOItemCount i = 0; i < count; i++) {
+ if (fInitialSetPowerState || (fHeadNoteChangeFlags & kIOPMInitialPowerChange)) {
+ // Skip notifying self, if 'kIOPMInitialDeviceState' is set and
+ // this is the initial power state change
+ if ((this == informee->whatObject) &&
+ (fHeadNotePowerArrayEntry->capabilityFlags & kIOPMInitialDeviceState)) {
+ skipCnt++;
+ continue;
+ }
+ }
+ informee->timer = -1;
+ param[i].Target = informee;
+ informee->retain();
+ informee = list->nextInList( informee );
+ }
+
+ count -= skipCnt;
+ if (!count) {
+ goto done;
+ }
+ fDriverCallParamCount = count;
+ fHeadNotePendingAcks = count;
+
+ // Block state machine and wait for callout completion.
+ assert(!fDriverCallBusy);
+ fDriverCallBusy = true;
+ thread_call_enter( fDriverCallEntry );
+ return true;
+
+done:
+ // Return false if there are no interested drivers or could not schedule
+ // callout thread due to error.
+ return false;
+}
+
+//*********************************************************************************
+// [private] notifyInterestedDriversDone
+//*********************************************************************************
+
+void
+IOService::notifyInterestedDriversDone( void )
+{
+ IOPMinformee * informee;
+ IOItemCount count;
+ DriverCallParam * param;
+ IOReturn result;
+ int maxTimeout = 0;
+
+ PM_ASSERT_IN_GATE();
+ assert( fDriverCallBusy == false );
+ assert( fMachineState == kIOPM_DriverThreadCallDone );
+
+ param = (DriverCallParam *) fDriverCallParamPtr;
+ count = fDriverCallParamCount;
+
+ if (param && count) {
+ for (IOItemCount i = 0; i < count; i++, param++) {
+ informee = (IOPMinformee *) param->Target;
+ result = param->Result;
+
+ if ((result == IOPMAckImplied) || (result < 0)) {
+ // Interested driver return IOPMAckImplied.
+ // If informee timer is zero, it must have de-registered
+ // interest during the thread callout. That also drops
+ // the pending ack count.
+
+ if (fHeadNotePendingAcks && informee->timer) {
+ fHeadNotePendingAcks--;
+ }
+
+ informee->timer = 0;
+ } else if (informee->timer) {
+ assert(informee->timer == -1);
+
+ // Driver has not acked, and has returned a positive result.
+ // Enforce a minimum permissible timeout value.
+ // Make the min value large enough so timeout is less likely
+ // to occur if a driver misinterpreted that the return value
+ // should be in microsecond units. And make it large enough
+ // to be noticeable if a driver neglects to ack.
+
+ if (result < kMinAckTimeoutTicks) {
+ result = kMinAckTimeoutTicks;
+ }
+
+ informee->timer = (result / (ACK_TIMER_PERIOD / ns_per_us)) + 1;
+ if (result > maxTimeout) {
+ maxTimeout = result;
+ }
+ }
+ // else, child has already acked or driver has removed interest,
+ // and head_note_pendingAcks decremented.
+ // informee may have been removed from the interested drivers list,
+ // thus the informee must be retained across the callout.
+
+ informee->release();
+ }
+
+ fDriverCallParamCount = 0;
+
+ if (fHeadNotePendingAcks) {
+ OUR_PMLog(kPMLogStartAckTimer, 0, 0);
+ start_ack_timer();
+ getPMRootDomain()->reset_watchdog_timer(this, maxTimeout / USEC_PER_SEC + 1);
+ }
+ }
+
+ MS_POP(); // pop the machine state passed to notifyAll()
+
+ // If interest acks are outstanding, block the state machine until
+ // fHeadNotePendingAcks drops to zero before notifying root domain.
+ // Otherwise notify root domain directly.
+
+ if (!fHeadNotePendingAcks) {
+ notifyRootDomain();
+ } else {
+ MS_PUSH(fMachineState);
+ fMachineState = kIOPM_NotifyChildrenStart;
+ }
+}
+
+//*********************************************************************************
+// [private] notifyRootDomain
+//*********************************************************************************
+
+void
+IOService::notifyRootDomain( void )
+{
+ assert( fDriverCallBusy == false );
+
+ // Only for root domain in the will-change phase
+ if (!IS_ROOT_DOMAIN || (fMachineState != kIOPM_OurChangeSetPowerState)) {
+ notifyChildren();
+ return;
+ }
+
+ MS_PUSH(fMachineState); // push notifyAll() machine state
+ fMachineState = kIOPM_DriverThreadCallDone;
+
+ // Call IOPMrootDomain::willNotifyPowerChildren() on a thread call
+ // to avoid a deadlock.
+ fDriverCallReason = kRootDomainInformPreChange;
+ fDriverCallBusy = true;
+ thread_call_enter( fDriverCallEntry );
+}
+
+void
+IOService::notifyRootDomainDone( void )
+{
+ assert( fDriverCallBusy == false );
+ assert( fMachineState == kIOPM_DriverThreadCallDone );
+
+ MS_POP(); // pop notifyAll() machine state
+ notifyChildren();
+}
+
+//*********************************************************************************
+// [private] notifyChildren
+//*********************************************************************************
+
+void
+IOService::notifyChildren( void )
+{
+ OSIterator * iter;
+ OSObject * next;
+ IOPowerConnection * connection;
+ OSArray * children = NULL;
+ IOPMrootDomain * rootDomain;
+ bool delayNotify = false;
+
+ if ((fHeadNotePowerState != fCurrentPowerState) &&
+ (IS_POWER_DROP == fIsPreChange) &&
+ ((rootDomain = getPMRootDomain()) == this)) {
+ rootDomain->tracePoint( IS_POWER_DROP ?
+ kIOPMTracePointSleepPowerPlaneDrivers :
+ kIOPMTracePointWakePowerPlaneDrivers );
+ }
+
+ if (fStrictTreeOrder) {
+ children = OSArray::withCapacity(8);
+ }
+
+ // Sum child power consumption in notifyChild()
+ fHeadNotePowerArrayEntry->staticPower = 0;
+
+ iter = getChildIterator(gIOPowerPlane);
+ if (iter) {
+ while ((next = iter->getNextObject())) {
+ if ((connection = OSDynamicCast(IOPowerConnection, next))) {
+ if (connection->getReadyFlag() == false) {
+ PM_LOG3("[%s] %s: connection not ready\n",
+ getName(), __FUNCTION__);
+ continue;
+ }
+
+ // Mechanism to postpone the did-change notification to
+ // certain power children to order those children last.
+ // Cannot be used together with strict tree ordering.
+
+ if (!fIsPreChange &&
+ connection->delayChildNotification &&
+ getPMRootDomain()->shouldDelayChildNotification(this)) {
+ if (!children) {
+ children = OSArray::withCapacity(8);
+ if (children) {
+ delayNotify = true;
+ }
+ }
+ if (delayNotify) {
+ children->setObject( connection );
+ continue;
+ }
+ }
+
+ if (!delayNotify && children) {
+ children->setObject( connection );
+ } else {
+ notifyChild( connection );
+ }
+ }
+ }
+ iter->release();
+ }
+
+ if (children && (children->getCount() == 0)) {
+ children->release();
+ children = NULL;
+ }
+ if (children) {
+ assert(fNotifyChildArray == NULL);
+ fNotifyChildArray = children;
+ MS_PUSH(fMachineState);
+
+ if (delayNotify) {
+ // Block until all non-delayed children have acked their
+ // notification. Then notify the remaining delayed child
+ // in the array. This is used to hold off graphics child
+ // notification while the rest of the system powers up.
+ // If a hid tickle arrives during this time, the delayed
+ // children are immediately notified and root domain will
+ // not clamp power for dark wake.
+
+ fMachineState = kIOPM_NotifyChildrenDelayed;
+ PM_LOG2("%s: %d children in delayed array\n",
+ getName(), children->getCount());
+ } else {
+ // Child array created to support strict notification order.
+ // Notify children in the array one at a time.
+
+ fMachineState = kIOPM_NotifyChildrenOrdered;
+ }
+ }
+}
+
+//*********************************************************************************
+// [private] notifyChildrenOrdered
+//*********************************************************************************
+
+void
+IOService::notifyChildrenOrdered( void )
+{
+ PM_ASSERT_IN_GATE();
+ assert(fNotifyChildArray);
+ assert(fMachineState == kIOPM_NotifyChildrenOrdered);
+
+ // Notify one child, wait for it to ack, then repeat for next child.
+ // This is a workaround for some drivers with multiple instances at
+ // the same branch in the power tree, but the driver is slow to power
+ // up unless the tree ordering is observed. Problem observed only on
+ // system wake, not on system sleep.
+ //
+ // We have the ability to power off in reverse child index order.
+ // That works nicely on some machines, but not on all HW configs.
+
+ if (fNotifyChildArray->getCount()) {
+ IOPowerConnection * connection;
+ connection = (IOPowerConnection *) fNotifyChildArray->getObject(0);
+ notifyChild( connection );
+ fNotifyChildArray->removeObject(0);
+ } else {
+ fNotifyChildArray->release();
+ fNotifyChildArray = NULL;
+
+ MS_POP(); // pushed by notifyChildren()
+ }
+}
+
+//*********************************************************************************
+// [private] notifyChildrenDelayed
+//*********************************************************************************
+
+void
+IOService::notifyChildrenDelayed( void )
+{
+ IOPowerConnection * connection;
+
+ PM_ASSERT_IN_GATE();
+ assert(fNotifyChildArray);
+ assert(fMachineState == kIOPM_NotifyChildrenDelayed);
+
+ // Wait after all non-delayed children and interested drivers have ack'ed,
+ // then notify all delayed children. If notify delay is canceled, child
+ // acks may be outstanding with PM blocked on fHeadNotePendingAcks != 0.
+ // But the handling for either case is identical.
+
+ for (int i = 0;; i++) {
+ connection = (IOPowerConnection *) fNotifyChildArray->getObject(i);
+ if (!connection) {
+ break;
+ }
+
+ notifyChild( connection );
+ }
+
+ PM_LOG2("%s: notified delayed children\n", getName());
+ fNotifyChildArray->release();
+ fNotifyChildArray = NULL;
+
+ MS_POP(); // pushed by notifyChildren()
+}
+
+//*********************************************************************************
+// [private] notifyAll
+//*********************************************************************************
+
+IOReturn
+IOService::notifyAll( uint32_t nextMS )
+{
+ // Save the machine state to be restored by notifyInterestedDriversDone()
+
+ PM_ASSERT_IN_GATE();
+ MS_PUSH(nextMS);
+ fMachineState = kIOPM_DriverThreadCallDone;
+ fDriverCallReason = fIsPreChange ?
+ kDriverCallInformPreChange : kDriverCallInformPostChange;
+
+ if (!notifyInterestedDrivers()) {
+ notifyInterestedDriversDone();
+ }
+
+ return IOPMWillAckLater;
+}
+
+//*********************************************************************************
+// [private, static] pmDriverCallout
+//
+// Thread call context
+//*********************************************************************************
+
+IOReturn
+IOService::actionDriverCalloutDone(
+ OSObject * target,
+ void * arg0, void * arg1,
+ void * arg2, void * arg3 )
+{
+ IOServicePM * pwrMgt = (IOServicePM *) arg0;
+
+ assert( fDriverCallBusy );
+ fDriverCallBusy = false;
+
+ assert(gIOPMWorkQueue);
+ gIOPMWorkQueue->signalWorkAvailable();
+
+ return kIOReturnSuccess;
+}
+
+void
+IOService::pmDriverCallout( IOService * from )
+{
+ assert(from);
+ switch (from->fDriverCallReason) {
+ case kDriverCallSetPowerState:
+ from->driverSetPowerState();
+ break;
+
+ case kDriverCallInformPreChange:
+ case kDriverCallInformPostChange:
+ from->driverInformPowerChange();
+ break;
+
+ case kRootDomainInformPreChange:
+ getPMRootDomain()->willNotifyPowerChildren(from->fHeadNotePowerState);
+ break;
+
+ default:
+ panic("IOService::pmDriverCallout bad machine state %x",
+ from->fDriverCallReason);
+ }
+
+ gIOPMWorkLoop->runAction(actionDriverCalloutDone,
+ /* target */ from,
+ /* arg0 */ (void *) from->pwrMgt );
+}
+
+//*********************************************************************************
+// [private] driverSetPowerState
+//
+// Thread call context
+//*********************************************************************************
+
+void
+IOService::driverSetPowerState( void )
+{
+ IOPMPowerStateIndex powerState;
+ DriverCallParam * param;
+ IOPMDriverCallEntry callEntry;
+ AbsoluteTime end;
+ IOReturn result;
+ uint32_t oldPowerState = getPowerState();
+
+ assert( fDriverCallBusy );
+ assert( fDriverCallParamPtr );
+ assert( fDriverCallParamCount == 1 );
+
+ param = (DriverCallParam *) fDriverCallParamPtr;
+ powerState = fHeadNotePowerState;
+
+ if (assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetPowerState)) {
+ OUR_PMLogFuncStart(kPMLogProgramHardware, (uintptr_t) this, powerState);
+ clock_get_uptime(&fDriverCallStartTime);
+ result = fControllingDriver->setPowerState( powerState, this );
+ clock_get_uptime(&end);
+ OUR_PMLogFuncEnd(kPMLogProgramHardware, (uintptr_t) this, (UInt32) result);
+
+ deassertPMDriverCall(&callEntry);
+
+ // Record the most recent max power state residency timings.
+ // Use with DeviceActiveTimestamp to diagnose tickle issues.
+ if (powerState == fHighestPowerState) {
+ fMaxPowerStateEntryTime = end;
+ } else if (oldPowerState == fHighestPowerState) {
+ fMaxPowerStateExitTime = end;
+ }
+
+ if (result < 0) {
+ PM_LOG("%s::setPowerState(%p, %lu -> %lu) returned 0x%x\n",
+ fName, OBFUSCATE(this), fCurrentPowerState, powerState, result);
+ }
+
+
+ if ((result == IOPMAckImplied) || (result < 0)) {
+ uint64_t nsec;
+
+ SUB_ABSOLUTETIME(&end, &fDriverCallStartTime);
+ absolutetime_to_nanoseconds(end, &nsec);
+ if (nsec > LOG_SETPOWER_TIMES) {
+ getPMRootDomain()->pmStatsRecordApplicationResponse(
+ gIOPMStatsDriverPSChangeSlow,
+ fName, kDriverCallSetPowerState, NS_TO_MS(nsec), getRegistryEntryID(),
+ NULL, powerState);
+ }
+ }
+ } else {
+ result = kIOPMAckImplied;
+ }
+
+ param->Result = result;
+}
+
+//*********************************************************************************
+// [private] driverInformPowerChange
+//
+// Thread call context
+//*********************************************************************************
+
+void
+IOService::driverInformPowerChange( void )
+{
+ IOPMinformee * informee;
+ IOService * driver;
+ DriverCallParam * param;
+ IOPMDriverCallEntry callEntry;
+ IOPMPowerFlags powerFlags;
+ IOPMPowerStateIndex powerState;
+ AbsoluteTime end;
+ IOReturn result;
+ IOItemCount count;
+ IOOptionBits callMethod = (fDriverCallReason == kDriverCallInformPreChange) ?
+ kIOPMDriverCallMethodWillChange : kIOPMDriverCallMethodDidChange;
+
+ assert( fDriverCallBusy );
+ assert( fDriverCallParamPtr );
+ assert( fDriverCallParamCount );
+
+ param = (DriverCallParam *) fDriverCallParamPtr;
+ count = fDriverCallParamCount;
+
+ powerFlags = fHeadNotePowerArrayEntry->capabilityFlags;
+ powerState = fHeadNotePowerState;
+
+ for (IOItemCount i = 0; i < count; i++) {
+ informee = (IOPMinformee *) param->Target;
+ driver = informee->whatObject;
+
+ if (assertPMDriverCall(&callEntry, callMethod, informee)) {
+ if (fDriverCallReason == kDriverCallInformPreChange) {
+ OUR_PMLogFuncStart(kPMLogInformDriverPreChange, (uintptr_t) this, powerState);
+ clock_get_uptime(&informee->startTime);
+ result = driver->powerStateWillChangeTo(powerFlags, powerState, this);
+ clock_get_uptime(&end);
+ OUR_PMLogFuncEnd(kPMLogInformDriverPreChange, (uintptr_t) this, result);
+ } else {
+ OUR_PMLogFuncStart(kPMLogInformDriverPostChange, (uintptr_t) this, powerState);
+ clock_get_uptime(&informee->startTime);
+ result = driver->powerStateDidChangeTo(powerFlags, powerState, this);
+ clock_get_uptime(&end);
+ OUR_PMLogFuncEnd(kPMLogInformDriverPostChange, (uintptr_t) this, result);
+ }