+void
+IOService::trackSystemSleepPreventers(
+ IOPMPowerStateIndex oldPowerState,
+ IOPMPowerStateIndex newPowerState,
+ IOPMPowerChangeFlags changeFlags __unused )
+{
+ IOPMPowerFlags oldCapability, newCapability;
+
+ oldCapability = fPowerStates[oldPowerState].capabilityFlags &
+ (kIOPMPreventIdleSleep | kIOPMPreventSystemSleep);
+ newCapability = fPowerStates[newPowerState].capabilityFlags &
+ (kIOPMPreventIdleSleep | kIOPMPreventSystemSleep);
+
+ if (fHeadNoteChangeFlags & kIOPMInitialPowerChange) {
+ oldCapability = 0;
+ }
+ if (oldCapability == newCapability) {
+ return;
+ }
+
+ if ((oldCapability ^ newCapability) & kIOPMPreventIdleSleep) {
+ bool enablePrevention = ((oldCapability & kIOPMPreventIdleSleep) == 0);
+ bool idleCancelAllowed = getPMRootDomain()->updatePreventIdleSleepList(
+ this, enablePrevention);
+#if SUPPORT_IDLE_CANCEL
+ if (idleCancelAllowed && enablePrevention) {
+ IOPMRequest * cancelRequest;
+
+ cancelRequest = acquirePMRequest( getPMRootDomain(), kIOPMRequestTypeIdleCancel );
+ if (cancelRequest) {
+ submitPMRequest( cancelRequest );
+ }
+ }
+#endif
+ }
+
+ if ((oldCapability ^ newCapability) & kIOPMPreventSystemSleep) {
+ getPMRootDomain()->updatePreventSystemSleepList(this,
+ ((oldCapability & kIOPMPreventSystemSleep) == 0));
+ }
+}
+
+//*********************************************************************************
+// [public] requestPowerDomainState
+//
+// Called on a power parent when a child's power requirement changes.
+//*********************************************************************************
+
+IOReturn
+IOService::requestPowerDomainState(
+ IOPMPowerFlags childRequestPowerFlags,
+ IOPowerConnection * childConnection,
+ unsigned long specification )
+{
+ IOPMPowerStateIndex order, powerState;
+ IOPMPowerFlags outputPowerFlags;
+ IOService * child;
+ IOPMRequest * subRequest;
+ bool adjustPower = false;
+
+ if (!initialized) {
+ return IOPMNotYetInitialized;
+ }
+
+ if (gIOPMWorkLoop->onThread() == false) {
+ PM_LOG("%s::requestPowerDomainState\n", getName());
+ return kIOReturnSuccess;
+ }
+
+ OUR_PMLog(kPMLogRequestDomain, childRequestPowerFlags, specification);
+
+ if (!isChild(childConnection, gIOPowerPlane)) {
+ return kIOReturnNotAttached;
+ }
+
+ if (!fControllingDriver || !fNumberOfPowerStates) {
+ return kIOReturnNotReady;
+ }
+
+ child = (IOService *) childConnection->getChildEntry(gIOPowerPlane);
+ assert(child);
+
+ // Remove flags from child request which we can't possibly supply
+ childRequestPowerFlags &= fMergedOutputPowerFlags;
+
+ // Merge in the power flags contributed by this power parent
+ // at its current or impending power state.
+
+ outputPowerFlags = fPowerStates[fCurrentPowerState].outputPowerFlags;
+ if (fMachineState != kIOPM_Finished) {
+ if (IS_POWER_DROP && !IS_ROOT_DOMAIN) {
+ // Use the lower power state when dropping power.
+ // Must be careful since a power drop can be cancelled
+ // from the following states:
+ // - kIOPM_OurChangeTellClientsPowerDown
+ // - kIOPM_OurChangeTellPriorityClientsPowerDown
+ //
+ // The child must not wait for this parent to raise power
+ // if the power drop was cancelled. The solution is to cancel
+ // the power drop if possible, then schedule an adjustment to
+ // re-evaluate the parent's power state.
+ //
+ // Root domain is excluded to avoid idle sleep issues. And allow
+ // root domain children to pop up when system is going to sleep.
+
+ if ((fMachineState == kIOPM_OurChangeTellClientsPowerDown) ||
+ (fMachineState == kIOPM_OurChangeTellPriorityClientsPowerDown)) {
+ fDoNotPowerDown = true; // cancel power drop
+ adjustPower = true;// schedule an adjustment
+ PM_LOG1("%s: power drop cancelled in state %u by %s\n",
+ getName(), fMachineState, child->getName());
+ } else {
+ // Beyond cancellation point, report the impending state.
+ outputPowerFlags =
+ fPowerStates[fHeadNotePowerState].outputPowerFlags;
+ }
+ } else if (IS_POWER_RISE) {
+ // When raising power, must report the output power flags from
+ // child's perspective. A child power request may arrive while
+ // parent is transitioning upwards. If a request arrives after
+ // setParentInfo() has already recorded the output power flags
+ // for the next power state, then using the power supplied by
+ // fCurrentPowerState is incorrect, and might cause the child
+ // to wait when it should not.
+
+ outputPowerFlags = childConnection->parentCurrentPowerFlags();
+ }
+ }
+ child->fHeadNoteDomainTargetFlags |= outputPowerFlags;
+
+ // Map child's requested power flags to one of our power state.
+
+ for (order = 0; order < fNumberOfPowerStates; order++) {
+ powerState = fPowerStates[order].stateOrderToIndex;
+ if ((fPowerStates[powerState].outputPowerFlags & childRequestPowerFlags)
+ == childRequestPowerFlags) {
+ break;
+ }
+ }
+ if (order >= fNumberOfPowerStates) {
+ powerState = kPowerStateZero;
+ }
+
+ // Conditions that warrants a power adjustment on this parent.
+ // Adjust power will also propagate any changes to the child's
+ // prevent idle/sleep flags towards the root domain.
+
+ if (!childConnection->childHasRequestedPower() ||
+ (powerState != childConnection->getDesiredDomainState())) {
+ adjustPower = true;
+ }
+
+#if ENABLE_DEBUG_LOGS
+ if (adjustPower) {
+ PM_LOG("requestPowerDomainState[%s]: %s, init %d, %u->%u\n",
+ getName(), child->getName(),
+ !childConnection->childHasRequestedPower(),
+ (uint32_t) childConnection->getDesiredDomainState(),
+ (uint32_t) powerState);
+ }
+#endif
+
+ // Record the child's desires on the connection.
+ childConnection->setChildHasRequestedPower();
+ childConnection->setDesiredDomainState( powerState );
+
+ // Schedule a request to re-evaluate all children desires and
+ // adjust power state. Submit a request if one wasn't pending,
+ // or if the current request is part of a call tree.
+
+ if (adjustPower && !fDeviceOverrideEnabled &&
+ (!fAdjustPowerScheduled || gIOPMRequest->getRootRequest())) {
+ subRequest = acquirePMRequest(
+ this, kIOPMRequestTypeAdjustPowerState, gIOPMRequest );
+ if (subRequest) {
+ submitPMRequest( subRequest );
+ fAdjustPowerScheduled = true;
+ }
+ }
+
+ return kIOReturnSuccess;
+}
+
+//*********************************************************************************
+// [public] temporaryPowerClampOn
+//
+// A power domain wants to clamp its power on till it has children which
+// will thendetermine the power domain state.
+//
+// We enter the highest state until addPowerChild is called.
+//*********************************************************************************
+
+IOReturn
+IOService::temporaryPowerClampOn( void )
+{
+ return requestPowerState( gIOPMPowerClientChildProxy, kIOPMPowerStateMax );
+}
+
+//*********************************************************************************
+// [public] makeUsable
+//
+// Some client of our device is asking that we become usable. Although
+// this has not come from a subclassed device object, treat it exactly
+// as if it had. In this way, subsequent requests for lower power from
+// a subclassed device object will pre-empt this request.
+//
+// We treat this as a subclass object request to switch to the
+// highest power state.
+//*********************************************************************************
+
+IOReturn
+IOService::makeUsable( void )
+{
+ OUR_PMLog(kPMLogMakeUsable, 0, 0);
+ return requestPowerState( gIOPMPowerClientDevice, kIOPMPowerStateMax );
+}
+
+//*********************************************************************************
+// [public] currentCapability
+//*********************************************************************************
+
+IOPMPowerFlags
+IOService::currentCapability( void )
+{
+ if (!initialized) {
+ return IOPMNotPowerManaged;
+ }
+
+ return fCurrentCapabilityFlags;
+}
+
+//*********************************************************************************
+// [public] changePowerStateTo
+//
+// Called by our power-controlling driver to change power state. The new desired
+// power state is computed and compared against the current power state. If those
+// power states differ, then a power state change is initiated.
+//*********************************************************************************
+
+IOReturn
+IOService::changePowerStateTo( unsigned long ordinal )
+{
+ OUR_PMLog(kPMLogChangeStateTo, ordinal, 0);
+ return requestPowerState( gIOPMPowerClientDriver, ordinal );
+}
+
+//*********************************************************************************
+// [protected] changePowerStateToPriv
+//
+// Called by our driver subclass to change power state. The new desired power
+// state is computed and compared against the current power state. If those
+// power states differ, then a power state change is initiated.
+//*********************************************************************************
+
+IOReturn
+IOService::changePowerStateToPriv( unsigned long ordinal )
+{
+ OUR_PMLog(kPMLogChangeStateToPriv, ordinal, 0);
+ return requestPowerState( gIOPMPowerClientDevice, ordinal );
+}
+
+//*********************************************************************************
+// [public] changePowerStateWithOverrideTo
+//
+// Called by our driver subclass to change power state. The new desired power
+// state is computed and compared against the current power state. If those
+// power states differ, then a power state change is initiated.
+// Override enforced - Children and Driver desires are ignored.
+//*********************************************************************************
+
+IOReturn
+IOService::changePowerStateWithOverrideTo( IOPMPowerStateIndex ordinal,
+ IOPMRequestTag tag )
+{
+ IOPMRequest * request;
+
+ if (!initialized) {
+ return kIOPMNotYetInitialized;
+ }
+
+ OUR_PMLog(kPMLogChangeStateToPriv, ordinal, 0);
+
+ request = acquirePMRequest( this, kIOPMRequestTypeRequestPowerStateOverride );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ gIOPMPowerClientDevice->retain();
+ request->fRequestTag = tag;
+ request->fArg0 = (void *) ordinal;
+ request->fArg1 = (void *) gIOPMPowerClientDevice;
+ request->fArg2 = NULL;
+#if NOT_READY
+ if (action) {
+ request->installCompletionAction( action, target, param );
+ }
+#endif
+
+ // Prevent needless downwards power transitions by clamping power
+ // until the scheduled request is executed.
+
+ if (gIOPMWorkLoop->inGate() && (ordinal < fNumberOfPowerStates)) {
+ fTempClampPowerState = StateMax(fTempClampPowerState, ordinal);
+ fTempClampCount++;
+ fOverrideMaxPowerState = ordinal;
+ request->fArg2 = (void *) (uintptr_t) true;
+ }
+
+ submitPMRequest( request );
+ return IOPMNoErr;
+}
+
+//*********************************************************************************
+// [public] changePowerStateForRootDomain
+//
+// Adjust the root domain's power desire on the target
+//*********************************************************************************
+
+IOReturn
+IOService::changePowerStateForRootDomain( IOPMPowerStateIndex ordinal )
+{
+ OUR_PMLog(kPMLogChangeStateForRootDomain, ordinal, 0);
+ return requestPowerState( gIOPMPowerClientRootDomain, ordinal );
+}
+
+//*********************************************************************************
+// [public for PMRD] quiescePowerTree
+//
+// For root domain to issue a request to quiesce the power tree.
+// Supplied callback invoked upon completion.
+//*********************************************************************************
+
+IOReturn
+IOService::quiescePowerTree(
+ void * target, IOPMCompletionAction action, void * param )
+{
+ IOPMRequest * request;
+
+ if (!initialized) {
+ return kIOPMNotYetInitialized;
+ }
+ if (!target || !action) {
+ return kIOReturnBadArgument;
+ }
+
+ OUR_PMLog(kPMLogQuiescePowerTree, 0, 0);
+
+ // Target the root node instead of root domain. This is to avoid blocking
+ // the quiesce request behind an existing root domain request in the work
+ // queue. Root parent and root domain requests in the work queue must not
+ // block the completion of the quiesce request.
+
+ request = acquirePMRequest(gIOPMRootNode, kIOPMRequestTypeQuiescePowerTree);
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ request->installCompletionAction(target, action, param);
+
+ // Submit through the normal request flow. This will make sure any request
+ // already in the request queue will get pushed over to the work queue for
+ // execution. Any request submitted after this request may not be serviced.
+
+ submitPMRequest( request );
+ return kIOReturnSuccess;
+}
+
+//*********************************************************************************
+// [private] requestPowerState
+//*********************************************************************************
+
+IOReturn
+IOService::requestPowerState(
+ const OSSymbol * client,
+ uint32_t state )
+{
+ IOPMRequest * request;
+
+ if (!client) {
+ return kIOReturnBadArgument;
+ }
+ if (!initialized) {
+ return kIOPMNotYetInitialized;
+ }
+
+ request = acquirePMRequest( this, kIOPMRequestTypeRequestPowerState );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ client->retain();
+ request->fArg0 = (void *)(uintptr_t) state;
+ request->fArg1 = (void *) client;
+ request->fArg2 = NULL;
+#if NOT_READY
+ if (action) {
+ request->installCompletionAction( action, target, param );
+ }
+#endif
+
+ // Prevent needless downwards power transitions by clamping power
+ // until the scheduled request is executed.
+
+ if (gIOPMWorkLoop->inGate() && (state < fNumberOfPowerStates)) {
+ fTempClampPowerState = StateMax(fTempClampPowerState, state);
+ fTempClampCount++;
+ request->fArg2 = (void *) (uintptr_t) true;
+ }
+
+ submitPMRequest( request );
+ return IOPMNoErr;
+}
+
+//*********************************************************************************
+// [private] handleRequestPowerState
+//*********************************************************************************
+
+void
+IOService::handleRequestPowerState( IOPMRequest * request )
+{
+ const OSSymbol * client = (const OSSymbol *) request->fArg1;
+ uint32_t state = (uint32_t)(uintptr_t) request->fArg0;
+
+ PM_ASSERT_IN_GATE();
+ if (request->fArg2) {
+ assert(fTempClampCount != 0);
+ if (fTempClampCount) {
+ fTempClampCount--;
+ }
+ if (!fTempClampCount) {
+ fTempClampPowerState = kPowerStateZero;
+ }
+ }
+
+ if (fNumberOfPowerStates && (state >= fNumberOfPowerStates)) {
+ state = fHighestPowerState;
+ }
+
+ // The power suppression due to changePowerStateWithOverrideTo() expires
+ // upon the next "device" power request - changePowerStateToPriv().
+
+ if ((getPMRequestType() != kIOPMRequestTypeRequestPowerStateOverride) &&
+ (client == gIOPMPowerClientDevice)) {
+ fOverrideMaxPowerState = kIOPMPowerStateMax;
+ }
+
+ if ((state == kPowerStateZero) &&
+ (client != gIOPMPowerClientDevice) &&
+ (client != gIOPMPowerClientDriver) &&
+ (client != gIOPMPowerClientChildProxy)) {
+ removePowerClient(client);
+ } else {
+ updatePowerClient(client, state);
+ }
+
+ adjustPowerState();
+ client->release();
+}
+
+//*********************************************************************************
+// [private] Helper functions to update/remove power clients.
+//*********************************************************************************
+
+void
+IOService::updatePowerClient( const OSSymbol * client, uint32_t powerState )
+{
+ IOPMPowerStateIndex oldPowerState = kPowerStateZero;
+
+ if (!fPowerClients) {
+ fPowerClients = OSDictionary::withCapacity(4);
+ }
+ if (fPowerClients && client) {
+ OSNumber * num = (OSNumber *) fPowerClients->getObject(client);
+ if (num) {
+ oldPowerState = num->unsigned32BitValue();
+ num->setValue(powerState);
+ } else {
+ num = OSNumber::withNumber(powerState, 32);
+ if (num) {
+ fPowerClients->setObject(client, num);
+ num->release();
+ }
+ }
+
+ PM_ACTION_3(actionUpdatePowerClient, client, oldPowerState, powerState);
+ }
+}
+
+void
+IOService::removePowerClient( const OSSymbol * client )
+{
+ if (fPowerClients && client) {
+ fPowerClients->removeObject(client);
+ }
+}
+
+uint32_t
+IOService::getPowerStateForClient( const OSSymbol * client )
+{
+ uint32_t powerState = kPowerStateZero;
+
+ if (fPowerClients && client) {
+ OSNumber * num = (OSNumber *) fPowerClients->getObject(client);
+ if (num) {
+ powerState = num->unsigned32BitValue();
+ }
+ }
+ return powerState;
+}
+
+//*********************************************************************************
+// [protected] powerOverrideOnPriv
+//*********************************************************************************
+
+IOReturn
+IOService::powerOverrideOnPriv( void )
+{
+ IOPMRequest * request;
+
+ if (!initialized) {
+ return IOPMNotYetInitialized;
+ }
+
+ if (gIOPMWorkLoop->inGate()) {
+ fDeviceOverrideEnabled = true;
+ return IOPMNoErr;
+ }
+
+ request = acquirePMRequest( this, kIOPMRequestTypePowerOverrideOnPriv );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ submitPMRequest( request );
+ return IOPMNoErr;
+}
+
+//*********************************************************************************
+// [protected] powerOverrideOffPriv
+//*********************************************************************************
+
+IOReturn
+IOService::powerOverrideOffPriv( void )
+{
+ IOPMRequest * request;
+
+ if (!initialized) {
+ return IOPMNotYetInitialized;
+ }
+
+ if (gIOPMWorkLoop->inGate()) {
+ fDeviceOverrideEnabled = false;
+ return IOPMNoErr;
+ }
+
+ request = acquirePMRequest( this, kIOPMRequestTypePowerOverrideOffPriv );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ submitPMRequest( request );
+ return IOPMNoErr;
+}
+
+//*********************************************************************************
+// [private] handlePowerOverrideChanged
+//*********************************************************************************
+
+void
+IOService::handlePowerOverrideChanged( IOPMRequest * request )
+{
+ PM_ASSERT_IN_GATE();
+ if (request->getType() == kIOPMRequestTypePowerOverrideOnPriv) {
+ OUR_PMLog(kPMLogOverrideOn, 0, 0);
+ fDeviceOverrideEnabled = true;
+ } else {
+ OUR_PMLog(kPMLogOverrideOff, 0, 0);
+ fDeviceOverrideEnabled = false;
+ }
+
+ adjustPowerState();
+}
+
+//*********************************************************************************
+// [private] computeDesiredState
+//*********************************************************************************
+
+void
+IOService::computeDesiredState( unsigned long localClamp, bool computeOnly )
+{
+ OSIterator * iter;
+ OSObject * next;
+ IOPowerConnection * connection;
+ uint32_t desiredState = kPowerStateZero;
+ uint32_t newPowerState = kPowerStateZero;
+ bool hasChildren = false;
+
+ // Desired power state is always 0 without a controlling driver.
+
+ if (!fNumberOfPowerStates) {
+ fDesiredPowerState = kPowerStateZero;
+ return;
+ }
+
+ // Examine the children's desired power state.
+
+ 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;
+ }
+ if (connection->childHasRequestedPower()) {
+ hasChildren = true;
+ }
+ desiredState = StateMax(connection->getDesiredDomainState(), desiredState);
+ }
+ }
+ iter->release();
+ }
+ if (hasChildren) {
+ updatePowerClient(gIOPMPowerClientChildren, desiredState);
+ } else {
+ removePowerClient(gIOPMPowerClientChildren);
+ }
+
+ // Iterate through all power clients to determine the min power state.
+
+ iter = OSCollectionIterator::withCollection(fPowerClients);
+ if (iter) {
+ const OSSymbol * client;
+ while ((client = (const OSSymbol *) iter->getNextObject())) {
+ // Ignore child and driver when override is in effect.
+ if ((fDeviceOverrideEnabled ||
+ (getPMRequestType() == kIOPMRequestTypeRequestPowerStateOverride)) &&
+ ((client == gIOPMPowerClientChildren) ||
+ (client == gIOPMPowerClientDriver))) {
+ continue;
+ }
+
+ // Ignore child proxy when children are present.
+ if (hasChildren && (client == gIOPMPowerClientChildProxy)) {
+ continue;
+ }
+
+ // Advisory tickles are irrelevant unless system is in full wake
+ if (client == gIOPMPowerClientAdvisoryTickle &&
+ !gIOPMAdvisoryTickleEnabled) {
+ continue;
+ }
+
+ desiredState = getPowerStateForClient(client);
+ assert(desiredState < fNumberOfPowerStates);
+ PM_LOG1(" %u %s\n",
+ desiredState, client->getCStringNoCopy());
+
+ newPowerState = StateMax(newPowerState, desiredState);
+
+ if (client == gIOPMPowerClientDevice) {
+ fDeviceDesire = desiredState;
+ }
+ }
+ iter->release();
+ }
+
+ // Factor in the temporary power desires.
+
+ newPowerState = StateMax(newPowerState, localClamp);
+ newPowerState = StateMax(newPowerState, fTempClampPowerState);
+
+ // Limit check against max power override.
+
+ newPowerState = StateMin(newPowerState, fOverrideMaxPowerState);
+
+ // Limit check against number of power states.
+
+ if (newPowerState >= fNumberOfPowerStates) {
+ newPowerState = fHighestPowerState;
+ }
+
+#if !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146))
+ if (getPMRootDomain()->isAOTMode()) {
+ if ((kIOPMPreventIdleSleep & fPowerStates[newPowerState].capabilityFlags)
+ && !(kIOPMPreventIdleSleep & fPowerStates[fDesiredPowerState].capabilityFlags)) {
+ getPMRootDomain()->claimSystemWakeEvent(this, kIOPMWakeEventAOTExit, getName(), NULL);
+ }
+ }
+#endif /* !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146)) */
+
+ fDesiredPowerState = newPowerState;
+
+ PM_LOG1(" temp %u, clamp %u, current %u, new %u\n",
+ (uint32_t) localClamp, (uint32_t) fTempClampPowerState,
+ (uint32_t) fCurrentPowerState, newPowerState);
+
+ if (!computeOnly) {
+ // Restart idle timer if possible when device desire has increased.
+ // Or if an advisory desire exists.
+
+ if (fIdleTimerPeriod && fIdleTimerStopped) {
+ restartIdleTimer();
+ }
+
+ // Invalidate cached tickle power state when desires change, and not
+ // due to a tickle request. In case the driver has requested a lower
+ // power state, but the tickle is caching a higher power state which
+ // will drop future tickles until the cached value is lowered or in-
+ // validated. The invalidation must occur before the power transition
+ // to avoid dropping a necessary tickle.
+
+ if ((getPMRequestType() != kIOPMRequestTypeActivityTickle) &&
+ (fActivityTicklePowerState != kInvalidTicklePowerState)) {
+ IOLockLock(fActivityLock);
+ fActivityTicklePowerState = kInvalidTicklePowerState;
+ IOLockUnlock(fActivityLock);
+ }
+ }
+}
+
+//*********************************************************************************
+// [public] currentPowerConsumption
+//
+//*********************************************************************************
+
+unsigned long
+IOService::currentPowerConsumption( void )
+{
+ if (!initialized) {
+ return kIOPMUnknown;
+ }
+
+ return fCurrentPowerConsumption;
+}
+
+//*********************************************************************************
+// [deprecated] getPMworkloop
+//*********************************************************************************
+
+#ifndef __LP64__
+IOWorkLoop *
+IOService::getPMworkloop( void )
+{
+ return gIOPMWorkLoop;
+}
+#endif
+
+#if NOT_YET
+
+//*********************************************************************************
+// Power Parent/Children Applier
+//*********************************************************************************
+
+static void
+applyToPowerChildren(
+ IOService * service,
+ IOServiceApplierFunction applier,
+ void * context,
+ IOOptionBits options )
+{
+ PM_ASSERT_IN_GATE();
+
+ IORegistryEntry * entry;
+ IORegistryIterator * iter;
+ IOPowerConnection * connection;
+ IOService * child;
+
+ iter = IORegistryIterator::iterateOver(service, gIOPowerPlane, options);
+ if (iter) {
+ while ((entry = iter->getNextObject())) {
+ // Get child of IOPowerConnection objects
+ if ((connection = OSDynamicCast(IOPowerConnection, entry))) {
+ child = (IOService *) connection->copyChildEntry(gIOPowerPlane);
+ if (child) {
+ (*applier)(child, context);
+ child->release();
+ }
+ }
+ }
+ iter->release();
+ }
+}
+
+static void
+applyToPowerParent(
+ IOService * service,
+ IOServiceApplierFunction applier,
+ void * context,
+ IOOptionBits options )
+{
+ PM_ASSERT_IN_GATE();
+
+ IORegistryEntry * entry;
+ IORegistryIterator * iter;
+ IOPowerConnection * connection;
+ IOService * parent;
+
+ iter = IORegistryIterator::iterateOver(service, gIOPowerPlane,
+ options | kIORegistryIterateParents);
+ if (iter) {
+ while ((entry = iter->getNextObject())) {
+ // Get child of IOPowerConnection objects
+ if ((connection = OSDynamicCast(IOPowerConnection, entry))) {
+ parent = (IOService *) connection->copyParentEntry(gIOPowerPlane);
+ if (parent) {
+ (*applier)(parent, context);
+ parent->release();
+ }
+ }
+ }
+ iter->release();
+ }
+}
+
+#endif /* NOT_YET */
+
+// MARK: -
+// MARK: Activity Tickle & Idle Timer
+
+void
+IOService::setAdvisoryTickleEnable( bool enable )
+{
+ gIOPMAdvisoryTickleEnabled = enable;
+}
+
+//*********************************************************************************
+// [public] activityTickle
+//
+// The tickle with parameter kIOPMSuperclassPolicy1 causes the activity
+// flag to be set, and the device state checked. If the device has been
+// powered down, it is powered up again.
+// The tickle with parameter kIOPMSubclassPolicy is ignored here and
+// should be intercepted by a subclass.
+//*********************************************************************************
+
+bool
+IOService::activityTickle( unsigned long type, unsigned long stateNumber )
+{
+ IOPMRequest * request;
+ bool noPowerChange = true;
+ uint32_t tickleFlags;
+
+ if (!initialized) {
+ return true; // no power change
+ }
+ if ((type == kIOPMSuperclassPolicy1) && StateOrder(stateNumber)) {
+ IOLockLock(fActivityLock);
+
+ // Record device activity for the idle timer handler.
+
+ fDeviceWasActive = true;
+ fActivityTickleCount++;
+ clock_get_uptime(&fDeviceActiveTimestamp);
+
+ PM_ACTION_0(actionActivityTickle);
+
+ // Record the last tickle power state.
+ // This helps to filter out redundant tickles as
+ // this function may be called from the data path.
+
+ if ((fActivityTicklePowerState == kInvalidTicklePowerState)
+ || StateOrder(fActivityTicklePowerState) < StateOrder(stateNumber)) {
+ fActivityTicklePowerState = stateNumber;
+ noPowerChange = false;
+
+ tickleFlags = kTickleTypeActivity | kTickleTypePowerRise;
+ request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
+ if (request) {
+ request->fArg0 = (void *) stateNumber;
+ request->fArg1 = (void *)(uintptr_t) tickleFlags;
+ request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
+ submitPMRequest(request);
+ }
+ }
+
+ IOLockUnlock(fActivityLock);
+ } else if ((type == kIOPMActivityTickleTypeAdvisory) &&
+ ((stateNumber = fDeviceUsablePowerState) != kPowerStateZero)) {
+ IOLockLock(fActivityLock);
+
+ fAdvisoryTickled = true;
+
+ if (fAdvisoryTicklePowerState != stateNumber) {
+ fAdvisoryTicklePowerState = stateNumber;
+ noPowerChange = false;
+
+ tickleFlags = kTickleTypeAdvisory | kTickleTypePowerRise;
+ request = acquirePMRequest( this, kIOPMRequestTypeActivityTickle );
+ if (request) {
+ request->fArg0 = (void *) stateNumber;
+ request->fArg1 = (void *)(uintptr_t) tickleFlags;
+ request->fArg2 = (void *)(uintptr_t) gIOPMTickleGeneration;
+ submitPMRequest(request);
+ }
+ }
+
+ IOLockUnlock(fActivityLock);
+ }
+
+ // Returns false if the activityTickle might cause a transition to a
+ // higher powered state, true otherwise.
+
+ return noPowerChange;
+}
+
+//*********************************************************************************
+// [private] handleActivityTickle
+//*********************************************************************************
+
+void
+IOService::handleActivityTickle( IOPMRequest * request )
+{
+ uint32_t ticklePowerState = (uint32_t)(uintptr_t) request->fArg0;
+ uint32_t tickleFlags = (uint32_t)(uintptr_t) request->fArg1;
+ uint32_t tickleGeneration = (uint32_t)(uintptr_t) request->fArg2;
+ bool adjustPower = false;
+
+ PM_ASSERT_IN_GATE();
+ if (fResetPowerStateOnWake && (tickleGeneration != gIOPMTickleGeneration)) {
+ // Drivers that don't want power restored on wake will drop any
+ // tickles that pre-dates the current system wake. The model is
+ // that each wake is a fresh start, with power state depressed
+ // until a new tickle or an explicit power up request from the
+ // driver. It is possible for the PM work loop to enter the
+ // system sleep path with tickle requests queued.
+
+ return;
+ }
+
+ if (tickleFlags & kTickleTypeActivity) {
+ IOPMPowerStateIndex deviceDesireOrder = StateOrder(fDeviceDesire);
+ uint32_t idleTimerGeneration = ticklePowerState; // kTickleTypePowerDrop
+
+ if (tickleFlags & kTickleTypePowerRise) {
+ if ((StateOrder(ticklePowerState) > deviceDesireOrder) &&
+ (ticklePowerState < fNumberOfPowerStates)) {
+ fIdleTimerMinPowerState = ticklePowerState;
+ updatePowerClient(gIOPMPowerClientDevice, ticklePowerState);
+ adjustPower = true;
+ }
+ } else if ((deviceDesireOrder > StateOrder(fIdleTimerMinPowerState)) &&
+ (idleTimerGeneration == fIdleTimerGeneration)) {
+ // Power drop due to idle timer expiration.
+ // Do not allow idle timer to reduce power below tickle power.
+ // This prevents the idle timer from decreasing the device desire
+ // to zero and cancelling the effect of a pre-sleep tickle when
+ // system wakes up to doze state, while the device is unable to
+ // raise its power state to satisfy the tickle.
+
+ deviceDesireOrder--;
+ if (deviceDesireOrder < fNumberOfPowerStates) {
+ ticklePowerState = fPowerStates[deviceDesireOrder].stateOrderToIndex;
+ updatePowerClient(gIOPMPowerClientDevice, ticklePowerState);
+ adjustPower = true;
+ }
+ }
+ } else { // advisory tickle
+ if (tickleFlags & kTickleTypePowerRise) {
+ if ((ticklePowerState == fDeviceUsablePowerState) &&
+ (ticklePowerState < fNumberOfPowerStates)) {
+ updatePowerClient(gIOPMPowerClientAdvisoryTickle, ticklePowerState);
+ fHasAdvisoryDesire = true;
+ fAdvisoryTickleUsed = true;
+ adjustPower = true;
+ } else {
+ IOLockLock(fActivityLock);
+ fAdvisoryTicklePowerState = kInvalidTicklePowerState;
+ IOLockUnlock(fActivityLock);
+ }
+ } else if (fHasAdvisoryDesire) {
+ removePowerClient(gIOPMPowerClientAdvisoryTickle);
+ fHasAdvisoryDesire = false;
+ adjustPower = true;
+ }
+ }
+
+ if (adjustPower) {
+ adjustPowerState();
+ }
+}
+
+//******************************************************************************
+// [public] setIdleTimerPeriod
+//
+// A subclass policy-maker is using our standard idleness detection service.
+// Start the idle timer. Period is in seconds.
+//******************************************************************************
+
+IOReturn
+IOService::setIdleTimerPeriod( unsigned long period )
+{
+ if (!initialized) {
+ return IOPMNotYetInitialized;
+ }
+
+ OUR_PMLog(kPMLogSetIdleTimerPeriod, period, fIdleTimerPeriod);
+
+ IOPMRequest * request =
+ acquirePMRequest( this, kIOPMRequestTypeSetIdleTimerPeriod );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ request->fArg0 = (void *) period;
+ submitPMRequest( request );
+
+ return kIOReturnSuccess;
+}
+
+IOReturn
+IOService::setIgnoreIdleTimer( bool ignore )
+{
+ if (!initialized) {
+ return IOPMNotYetInitialized;
+ }
+
+ OUR_PMLog(kIOPMRequestTypeIgnoreIdleTimer, ignore, 0);
+
+ IOPMRequest * request =
+ acquirePMRequest( this, kIOPMRequestTypeIgnoreIdleTimer );
+ if (!request) {
+ return kIOReturnNoMemory;
+ }
+
+ 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);
+ }
+
+ deassertPMDriverCall(&callEntry);
+
+
+ if ((result == IOPMAckImplied) || (result < 0)) {
+ uint64_t nsec;
+
+ SUB_ABSOLUTETIME(&end, &informee->startTime);
+ absolutetime_to_nanoseconds(end, &nsec);
+ if (nsec > LOG_SETPOWER_TIMES) {
+ getPMRootDomain()->pmStatsRecordApplicationResponse(
+ gIOPMStatsDriverPSChangeSlow, driver->getName(),
+ fDriverCallReason, NS_TO_MS(nsec), driver->getRegistryEntryID(),
+ NULL, powerState);
+ }
+ }
+ } else {
+ result = kIOPMAckImplied;
+ }
+
+ param->Result = result;
+ param++;
+ }
+}
+
+//*********************************************************************************
+// [private] notifyChild
+//
+// Notify a power domain child of an upcoming power change.
+// If the object acknowledges the current change, we return TRUE.
+//*********************************************************************************
+
+bool
+IOService::notifyChild( IOPowerConnection * theNub )
+{
+ IOReturn ret = IOPMAckImplied;
+ unsigned long childPower;
+ IOService * theChild;
+ IOPMRequest * childRequest;
+ IOPMPowerChangeFlags requestArg2;
+ int requestType;
+
+ PM_ASSERT_IN_GATE();
+ theChild = (IOService *)(theNub->copyChildEntry(gIOPowerPlane));
+ if (!theChild) {
+ return true;
+ }
+
+ // Unless the child handles the notification immediately and returns
+ // kIOPMAckImplied, we'll be awaiting their acknowledgement later.
+ fHeadNotePendingAcks++;
+ theNub->setAwaitingAck(true);
+
+ requestArg2 = fHeadNoteChangeFlags;
+ if (StateOrder(fHeadNotePowerState) < StateOrder(fCurrentPowerState)) {
+ requestArg2 |= kIOPMDomainPowerDrop;
+ }
+
+ requestType = fIsPreChange ?
+ kIOPMRequestTypePowerDomainWillChange :
+ kIOPMRequestTypePowerDomainDidChange;
+
+ childRequest = acquirePMRequest( theChild, requestType );
+ if (childRequest) {
+ theNub->retain();
+ childRequest->fArg0 = (void *) fHeadNotePowerArrayEntry->outputPowerFlags;
+ childRequest->fArg1 = (void *) theNub;
+ childRequest->fArg2 = (void *)(uintptr_t) requestArg2;
+ theChild->submitPMRequest( childRequest );
+ ret = IOPMWillAckLater;
+ } else {
+ ret = IOPMAckImplied;
+ fHeadNotePendingAcks--;
+ theNub->setAwaitingAck(false);
+ childPower = theChild->currentPowerConsumption();
+ if (childPower == kIOPMUnknown) {
+ fHeadNotePowerArrayEntry->staticPower = kIOPMUnknown;
+ } else {
+ if (fHeadNotePowerArrayEntry->staticPower != kIOPMUnknown) {
+ fHeadNotePowerArrayEntry->staticPower += childPower;
+ }
+ }
+ }
+
+ theChild->release();
+ return IOPMAckImplied == ret;
+}
+
+//*********************************************************************************
+// [private] notifyControllingDriver
+//*********************************************************************************
+
+bool
+IOService::notifyControllingDriver( void )
+{
+ DriverCallParam * param;
+
+ PM_ASSERT_IN_GATE();
+ assert( fDriverCallParamCount == 0 );
+ assert( fControllingDriver );
+
+ if (fInitialSetPowerState) {
+ fInitialSetPowerState = false;
+ fHeadNoteChangeFlags |= kIOPMInitialPowerChange;
+
+ // Driver specified flag to skip the inital setPowerState()
+ if (fHeadNotePowerArrayEntry->capabilityFlags & kIOPMInitialDeviceState) {
+ return false;
+ }
+ }
+
+ param = (DriverCallParam *) fDriverCallParamPtr;
+ if (!param) {
+ param = IONew(DriverCallParam, 1);
+ if (!param) {
+ return false; // no memory
+ }
+ fDriverCallParamPtr = (void *) param;
+ fDriverCallParamSlots = 1;
+ }
+
+ param->Target = fControllingDriver;
+ fDriverCallParamCount = 1;
+ fDriverTimer = -1;
+
+ // Block state machine and wait for callout completion.
+ assert(!fDriverCallBusy);
+ fDriverCallBusy = true;
+ thread_call_enter( fDriverCallEntry );
+
+ return true;
+}
+
+//*********************************************************************************
+// [private] notifyControllingDriverDone
+//*********************************************************************************
+
+void
+IOService::notifyControllingDriverDone( void )
+{
+ DriverCallParam * param;
+ IOReturn result;
+
+ PM_ASSERT_IN_GATE();
+ param = (DriverCallParam *) fDriverCallParamPtr;
+
+ assert( fDriverCallBusy == false );
+ assert( fMachineState == kIOPM_DriverThreadCallDone );
+
+ if (param && fDriverCallParamCount) {
+ assert(fDriverCallParamCount == 1);
+
+ // the return value from setPowerState()
+ result = param->Result;
+
+ if ((result == IOPMAckImplied) || (result < 0)) {
+ fDriverTimer = 0;
+ } else if (fDriverTimer) {
+ assert(fDriverTimer == -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;
+ }
+
+ fDriverTimer = (result / (ACK_TIMER_PERIOD / ns_per_us)) + 1;
+ }
+ // else, child has already acked and driver_timer reset to 0.
+
+ fDriverCallParamCount = 0;
+
+ if (fDriverTimer) {
+ OUR_PMLog(kPMLogStartAckTimer, 0, 0);
+ start_ack_timer();
+ getPMRootDomain()->reset_watchdog_timer(this, result / USEC_PER_SEC + 1);
+ }
+ }
+
+ MS_POP(); // pushed by OurChangeSetPowerState()
+ fIsPreChange = false;
+}
+
+//*********************************************************************************
+// [private] all_done
+//
+// A power change is done.
+//*********************************************************************************
+
+void
+IOService::all_done( void )
+{
+ IOPMPowerStateIndex prevPowerState;
+ const IOPMPSEntry * powerStatePtr;
+ IOPMDriverCallEntry callEntry;
+ uint32_t prevMachineState = fMachineState;
+ bool actionCalled = false;
+ uint64_t ts;
+
+ fMachineState = kIOPM_Finished;
+
+ if ((fHeadNoteChangeFlags & kIOPMSynchronize) &&
+ ((prevMachineState == kIOPM_Finished) ||
+ (prevMachineState == kIOPM_SyncFinish))) {
+ // Sync operation and no power change occurred.
+ // Do not inform driver and clients about this request completion,
+ // except for the originator (root domain).
+
+ PM_ACTION_2(actionPowerChangeDone,
+ fHeadNotePowerState, fHeadNoteChangeFlags);
+
+ if (getPMRequestType() == kIOPMRequestTypeSynchronizePowerTree) {
+ powerChangeDone(fCurrentPowerState);
+ } else if (fAdvisoryTickleUsed) {
+ // Not root domain and advisory tickle target.
+ // Re-adjust power after power tree sync at the 'did' pass
+ // to recompute desire and adjust power state between dark
+ // and full wake transitions. Root domain is responsible
+ // for calling setAdvisoryTickleEnable() before starting
+ // the kIOPMSynchronize power change.
+
+ if (!fAdjustPowerScheduled &&
+ (fHeadNoteChangeFlags & kIOPMDomainDidChange)) {
+ IOPMRequest * request;
+ request = acquirePMRequest( this, kIOPMRequestTypeAdjustPowerState );
+ if (request) {
+ submitPMRequest( request );
+ fAdjustPowerScheduled = true;
+ }
+ }
+ }
+
+ return;
+ }
+
+ // our power change
+ if (fHeadNoteChangeFlags & kIOPMSelfInitiated) {
+ // power state changed
+ if ((fHeadNoteChangeFlags & kIOPMNotDone) == 0) {
+ trackSystemSleepPreventers(
+ fCurrentPowerState, fHeadNotePowerState, fHeadNoteChangeFlags);
+
+ // we changed, tell our parent
+ requestDomainPower(fHeadNotePowerState);
+
+ // yes, did power raise?
+ if (StateOrder(fCurrentPowerState) < StateOrder(fHeadNotePowerState)) {
+ // yes, inform clients and apps
+ tellChangeUp(fHeadNotePowerState);
+ }
+ prevPowerState = fCurrentPowerState;
+ // either way
+ fCurrentPowerState = fHeadNotePowerState;
+ PM_LOCK();
+ if (fReportBuf) {
+ ts = mach_absolute_time();
+ STATEREPORT_SETSTATE(fReportBuf, fCurrentPowerState, ts);
+ }
+ PM_UNLOCK();
+#if PM_VARS_SUPPORT
+ fPMVars->myCurrentState = fCurrentPowerState;
+#endif
+ OUR_PMLog(kPMLogChangeDone, fCurrentPowerState, prevPowerState);
+ PM_ACTION_2(actionPowerChangeDone,
+ fHeadNotePowerState, fHeadNoteChangeFlags);
+ actionCalled = true;
+
+ powerStatePtr = &fPowerStates[fCurrentPowerState];
+ fCurrentCapabilityFlags = powerStatePtr->capabilityFlags;
+ if (fCurrentCapabilityFlags & kIOPMStaticPowerValid) {
+ fCurrentPowerConsumption = powerStatePtr->staticPower;
+ }
+
+ if (fHeadNoteChangeFlags & kIOPMRootChangeDown) {
+ // Bump tickle generation count once the entire tree is down
+ gIOPMTickleGeneration++;
+ }
+
+ // inform subclass policy-maker
+ if (fPCDFunctionOverride && fParentsKnowState &&
+ assertPMDriverCall(&callEntry, kIOPMDriverCallMethodChangeDone, NULL, kIOPMDriverCallNoInactiveCheck)) {
+ powerChangeDone(prevPowerState);
+ deassertPMDriverCall(&callEntry);
+ }
+ } else if (getPMRequestType() == kIOPMRequestTypeRequestPowerStateOverride) {
+ // changePowerStateWithOverrideTo() was cancelled
+ fOverrideMaxPowerState = kIOPMPowerStateMax;
+ }
+ }
+
+ // parent-initiated power change
+ if (fHeadNoteChangeFlags & kIOPMParentInitiated) {
+ if (fHeadNoteChangeFlags & kIOPMRootChangeDown) {
+ ParentChangeRootChangeDown();
+ }
+
+ // power state changed
+ if ((fHeadNoteChangeFlags & kIOPMNotDone) == 0) {
+ trackSystemSleepPreventers(
+ fCurrentPowerState, fHeadNotePowerState, fHeadNoteChangeFlags);
+
+ // did power raise?
+ if (StateOrder(fCurrentPowerState) < StateOrder(fHeadNotePowerState)) {
+ // yes, inform clients and apps
+ tellChangeUp(fHeadNotePowerState);
+ }
+ // either way
+ prevPowerState = fCurrentPowerState;
+ fCurrentPowerState = fHeadNotePowerState;
+ PM_LOCK();
+ if (fReportBuf) {
+ ts = mach_absolute_time();
+ STATEREPORT_SETSTATE(fReportBuf, fCurrentPowerState, ts);
+ }
+ PM_UNLOCK();
+#if PM_VARS_SUPPORT
+ fPMVars->myCurrentState = fCurrentPowerState;
+#endif
+
+ OUR_PMLog(kPMLogChangeDone, fCurrentPowerState, prevPowerState);
+ PM_ACTION_2(actionPowerChangeDone,
+ fHeadNotePowerState, fHeadNoteChangeFlags);
+ actionCalled = true;
+
+ powerStatePtr = &fPowerStates[fCurrentPowerState];
+ fCurrentCapabilityFlags = powerStatePtr->capabilityFlags;
+ if (fCurrentCapabilityFlags & kIOPMStaticPowerValid) {
+ fCurrentPowerConsumption = powerStatePtr->staticPower;
+ }
+
+ // inform subclass policy-maker
+ if (fPCDFunctionOverride && fParentsKnowState &&
+ assertPMDriverCall(&callEntry, kIOPMDriverCallMethodChangeDone, NULL, kIOPMDriverCallNoInactiveCheck)) {
+ powerChangeDone(prevPowerState);
+ deassertPMDriverCall(&callEntry);
+ }
+ }
+ }
+
+ // When power rises enough to satisfy the tickle's desire for more power,
+ // the condition preventing idle-timer from dropping power is removed.
+
+ if (StateOrder(fCurrentPowerState) >= StateOrder(fIdleTimerMinPowerState)) {
+ fIdleTimerMinPowerState = kPowerStateZero;
+ }
+
+ if (!actionCalled) {
+ PM_ACTION_2(actionPowerChangeDone,
+ fHeadNotePowerState, fHeadNoteChangeFlags);
+ }
+}
+
+// MARK: -
+// MARK: Power Change Initiated by Driver
+
+//*********************************************************************************
+// [private] OurChangeStart
+//
+// Begin the processing of a power change initiated by us.
+//*********************************************************************************
+
+void
+IOService::OurChangeStart( void )
+{
+ PM_ASSERT_IN_GATE();
+ OUR_PMLog( kPMLogStartDeviceChange, fHeadNotePowerState, fCurrentPowerState );
+
+ // fMaxPowerState is our maximum possible power state based on the current
+ // power state of our parents. If we are trying to raise power beyond the
+ // maximum, send an async request for more power to all parents.
+
+ if (!IS_PM_ROOT && (StateOrder(fMaxPowerState) < StateOrder(fHeadNotePowerState))) {
+ fHeadNoteChangeFlags |= kIOPMNotDone;
+ requestDomainPower(fHeadNotePowerState);
+ OurChangeFinish();
+ return;
+ }
+
+ // Redundant power changes skips to the end of the state machine.
+
+ if (!fInitialPowerChange && (fHeadNotePowerState == fCurrentPowerState)) {
+ OurChangeFinish();
+ return;
+ }
+ fInitialPowerChange = false;
+
+ // Change started, but may not complete...
+ // Can be canceled (power drop) or deferred (power rise).
+
+ PM_ACTION_2(actionPowerChangeStart, fHeadNotePowerState, &fHeadNoteChangeFlags);
+
+ // Two separate paths, depending if power is being raised or lowered.
+ // Lowering power is subject to approval by clients of this service.
+
+ if (IS_POWER_DROP) {
+ fDoNotPowerDown = false;
+
+ // Ask for persmission to drop power state
+ fMachineState = kIOPM_OurChangeTellClientsPowerDown;
+ fOutOfBandParameter = kNotifyApps;
+ askChangeDown(fHeadNotePowerState);
+ } else {
+ // This service is raising power and parents are able to support the
+ // new power state. However a parent may have already committed to
+ // drop power, which might force this object to temporarily drop power.
+ // This results in "oscillations" before the state machines converge
+ // to a steady state.
+ //
+ // To prevent this, a child must make a power reservation against all
+ // parents before raising power. If the reservation fails, indicating
+ // that the child will be unable to sustain the higher power state,
+ // then the child will signal the parent to adjust power, and the child
+ // will defer its power change.
+
+ IOReturn ret;
+
+ // Reserve parent power necessary to achieve fHeadNotePowerState.
+ ret = requestDomainPower( fHeadNotePowerState, kReserveDomainPower );
+ if (ret != kIOReturnSuccess) {
+ // Reservation failed, defer power rise.
+ fHeadNoteChangeFlags |= kIOPMNotDone;
+ OurChangeFinish();
+ return;
+ }
+
+ OurChangeTellCapabilityWillChange();
+ }
+}
+
+//*********************************************************************************
+// [private] requestDomainPowerApplier
+//
+// Call requestPowerDomainState() on all power parents.
+//*********************************************************************************
+
+struct IOPMRequestDomainPowerContext {
+ IOService * child; // the requesting child
+ IOPMPowerFlags requestPowerFlags;// power flags requested by child
+};
+
+static void
+requestDomainPowerApplier(
+ IORegistryEntry * entry,
+ void * inContext )
+{
+ IOPowerConnection * connection;
+ IOService * parent;
+ IOPMRequestDomainPowerContext * context;
+
+ if ((connection = OSDynamicCast(IOPowerConnection, entry)) == NULL) {
+ return;
+ }
+ parent = (IOService *) connection->copyParentEntry(gIOPowerPlane);
+ if (!parent) {
+ return;
+ }
+
+ assert(inContext);
+ context = (IOPMRequestDomainPowerContext *) inContext;
+
+ if (connection->parentKnowsState() && connection->getReadyFlag()) {
+ parent->requestPowerDomainState(
+ context->requestPowerFlags,
+ connection,
+ IOPMLowestState);
+ }
+
+ parent->release();
+}
+
+//*********************************************************************************
+// [private] requestDomainPower
+//
+// Called by a power child to broadcast its desired power state to all parents.
+// If the child self-initiates a power change, it must call this function to
+// allow its parents to adjust power state.
+//*********************************************************************************
+
+IOReturn
+IOService::requestDomainPower(
+ IOPMPowerStateIndex ourPowerState,
+ IOOptionBits options )
+{
+ IOPMPowerFlags requestPowerFlags;
+ IOPMPowerStateIndex maxPowerState;
+ IOPMRequestDomainPowerContext context;
+
+ PM_ASSERT_IN_GATE();
+ assert(ourPowerState < fNumberOfPowerStates);
+ if (ourPowerState >= fNumberOfPowerStates) {
+ return kIOReturnBadArgument;
+ }
+ if (IS_PM_ROOT) {
+ return kIOReturnSuccess;
+ }
+
+ // Fetch our input power flags for the requested power state.
+ // Parent request is stated in terms of required power flags.
+
+ requestPowerFlags = fPowerStates[ourPowerState].inputPowerFlags;
+
+ // Disregard the "previous request" for power reservation.
+
+ if (((options & kReserveDomainPower) == 0) &&
+ (fPreviousRequestPowerFlags == requestPowerFlags)) {
+ // skip if domain already knows our requirements
+ goto done;
+ }
+ fPreviousRequestPowerFlags = requestPowerFlags;
+
+ // The results will be collected by fHeadNoteDomainTargetFlags
+ context.child = this;
+ context.requestPowerFlags = requestPowerFlags;
+ fHeadNoteDomainTargetFlags = 0;
+ applyToParents(requestDomainPowerApplier, &context, gIOPowerPlane);
+
+ if (options & kReserveDomainPower) {
+ maxPowerState = fControllingDriver->maxCapabilityForDomainState(
+ fHeadNoteDomainTargetFlags );
+
+ if (StateOrder(maxPowerState) < StateOrder(ourPowerState)) {
+ PM_LOG1("%s: power desired %u:0x%x got %u:0x%x\n",
+ getName(),
+ (uint32_t) ourPowerState, (uint32_t) requestPowerFlags,
+ (uint32_t) maxPowerState, (uint32_t) fHeadNoteDomainTargetFlags);
+ return kIOReturnNoPower;
+ }
+ }
+
+done:
+ return kIOReturnSuccess;
+}
+
+//*********************************************************************************
+// [private] OurSyncStart
+//*********************************************************************************
+
+void
+IOService::OurSyncStart( void )
+{
+ PM_ASSERT_IN_GATE();
+
+ if (fInitialPowerChange) {
+ return;
+ }
+
+ PM_ACTION_2(actionPowerChangeStart, fHeadNotePowerState, &fHeadNoteChangeFlags);
+
+ if (fHeadNoteChangeFlags & kIOPMNotDone) {
+ OurChangeFinish();
+ return;
+ }
+
+ if (fHeadNoteChangeFlags & kIOPMSyncTellPowerDown) {
+ fDoNotPowerDown = false;
+
+ // Ask for permission to drop power state
+ fMachineState = kIOPM_SyncTellClientsPowerDown;
+ fOutOfBandParameter = kNotifyApps;
+ askChangeDown(fHeadNotePowerState);
+ } else {
+ // Only inform capability app and clients.
+ tellSystemCapabilityChange( kIOPM_SyncNotifyWillChange );
+ }
+}
+
+//*********************************************************************************
+// [private] OurChangeTellClientsPowerDown
+//
+// All applications and kernel clients have acknowledged our permission to drop
+// power. Here we notify them that we will lower the power and wait for acks.
+//*********************************************************************************
+
+void
+IOService::OurChangeTellClientsPowerDown( void )
+{
+ if (!IS_ROOT_DOMAIN) {
+ fMachineState = kIOPM_OurChangeTellPriorityClientsPowerDown;
+ } else {
+ fMachineState = kIOPM_OurChangeTellUserPMPolicyPowerDown;
+ }
+ tellChangeDown1(fHeadNotePowerState);
+}
+
+//*********************************************************************************
+// [private] OurChangeTellUserPMPolicyPowerDown
+//
+// All applications and kernel clients have acknowledged our permission to drop
+// power. Here we notify power management policy in user-space and wait for acks
+// one last time before we lower power
+//*********************************************************************************
+void
+IOService::OurChangeTellUserPMPolicyPowerDown( void )
+{
+ fMachineState = kIOPM_OurChangeTellPriorityClientsPowerDown;
+ fOutOfBandParameter = kNotifyApps;
+
+ tellClientsWithResponse(kIOPMMessageLastCallBeforeSleep);
+}
+
+//*********************************************************************************
+// [private] OurChangeTellPriorityClientsPowerDown
+//
+// All applications and kernel clients have acknowledged our intention to drop
+// power. Here we notify "priority" clients that we are lowering power.
+//*********************************************************************************