// SPDX-License-Identifier: GPL-2.0-only1/*2* drivers/acpi/device_pm.c - ACPI device power management routines.3*4* Copyright (C) 2012, Intel Corp.5* Author: Rafael J. Wysocki <[email protected]>6*7* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8*9* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~10*/1112#define pr_fmt(fmt) "PM: " fmt1314#include <linux/acpi.h>15#include <linux/export.h>16#include <linux/mutex.h>17#include <linux/pm_qos.h>18#include <linux/pm_domain.h>19#include <linux/pm_runtime.h>20#include <linux/suspend.h>2122#include "fan.h"23#include "internal.h"2425/**26* acpi_power_state_string - String representation of ACPI device power state.27* @state: ACPI device power state to return the string representation of.28*/29const char *acpi_power_state_string(int state)30{31switch (state) {32case ACPI_STATE_D0:33return "D0";34case ACPI_STATE_D1:35return "D1";36case ACPI_STATE_D2:37return "D2";38case ACPI_STATE_D3_HOT:39return "D3hot";40case ACPI_STATE_D3_COLD:41return "D3cold";42default:43return "(unknown)";44}45}4647static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)48{49unsigned long long psc;50acpi_status status;5152status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);53if (ACPI_FAILURE(status))54return -ENODEV;5556*state = psc;57return 0;58}5960/**61* acpi_device_get_power - Get power state of an ACPI device.62* @device: Device to get the power state of.63* @state: Place to store the power state of the device.64*65* This function does not update the device's power.state field, but it may66* update its parent's power.state field (when the parent's power state is67* unknown and the device's power state turns out to be D0).68*69* Also, it does not update power resource reference counters to ensure that70* the power state returned by it will be persistent and it may return a power71* state shallower than previously set by acpi_device_set_power() for @device72* (if that power state depends on any power resources).73*/74int acpi_device_get_power(struct acpi_device *device, int *state)75{76int result = ACPI_STATE_UNKNOWN;77struct acpi_device *parent;78int error;7980if (!device || !state)81return -EINVAL;8283parent = acpi_dev_parent(device);8485if (!device->flags.power_manageable) {86/* TBD: Non-recursive algorithm for walking up hierarchy. */87*state = parent ? parent->power.state : ACPI_STATE_D0;88goto out;89}9091/*92* Get the device's power state from power resources settings and _PSC,93* if available.94*/95if (device->power.flags.power_resources) {96error = acpi_power_get_inferred_state(device, &result);97if (error)98return error;99}100if (device->power.flags.explicit_get) {101int psc;102103error = acpi_dev_pm_explicit_get(device, &psc);104if (error)105return error;106107/*108* The power resources settings may indicate a power state109* shallower than the actual power state of the device, because110* the same power resources may be referenced by other devices.111*112* For systems predating ACPI 4.0 we assume that D3hot is the113* deepest state that can be supported.114*/115if (psc > result && psc < ACPI_STATE_D3_COLD)116result = psc;117else if (result == ACPI_STATE_UNKNOWN)118result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;119}120121/*122* If we were unsure about the device parent's power state up to this123* point, the fact that the device is in D0 implies that the parent has124* to be in D0 too, except if ignore_parent is set.125*/126if (!device->power.flags.ignore_parent && parent &&127parent->power.state == ACPI_STATE_UNKNOWN &&128result == ACPI_STATE_D0)129parent->power.state = ACPI_STATE_D0;130131*state = result;132133out:134acpi_handle_debug(device->handle, "Power state: %s\n",135acpi_power_state_string(*state));136137return 0;138}139140static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)141{142if (adev->power.states[state].flags.explicit_set) {143char method[5] = { '_', 'P', 'S', '0' + state, '\0' };144acpi_status status;145146status = acpi_evaluate_object(adev->handle, method, NULL, NULL);147if (ACPI_FAILURE(status))148return -ENODEV;149}150return 0;151}152153/**154* acpi_device_set_power - Set power state of an ACPI device.155* @device: Device to set the power state of.156* @state: New power state to set.157*158* Callers must ensure that the device is power manageable before using this159* function.160*/161int acpi_device_set_power(struct acpi_device *device, int state)162{163int target_state = state;164int result = 0;165166if (!device || !device->flags.power_manageable167|| (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))168return -EINVAL;169170acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",171acpi_power_state_string(device->power.state),172acpi_power_state_string(state));173174/* Make sure this is a valid target state */175176/* There is a special case for D0 addressed below. */177if (state > ACPI_STATE_D0 && state == device->power.state)178goto no_change;179180if (state == ACPI_STATE_D3_COLD) {181/*182* For transitions to D3cold we need to execute _PS3 and then183* possibly drop references to the power resources in use.184*/185state = ACPI_STATE_D3_HOT;186/* If D3cold is not supported, use D3hot as the target state. */187if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)188target_state = state;189} else if (!device->power.states[state].flags.valid) {190acpi_handle_debug(device->handle, "Power state %s not supported\n",191acpi_power_state_string(state));192return -ENODEV;193}194195if (!device->power.flags.ignore_parent) {196struct acpi_device *parent;197198parent = acpi_dev_parent(device);199if (parent && state < parent->power.state) {200acpi_handle_debug(device->handle,201"Cannot transition to %s for parent in %s\n",202acpi_power_state_string(state),203acpi_power_state_string(parent->power.state));204return -ENODEV;205}206}207208/*209* Transition Power210* ----------------211* In accordance with ACPI 6, _PSx is executed before manipulating power212* resources, unless the target state is D0, in which case _PS0 is213* supposed to be executed after turning the power resources on.214*/215if (state > ACPI_STATE_D0) {216/*217* According to ACPI 6, devices cannot go from lower-power218* (deeper) states to higher-power (shallower) states.219*/220if (state < device->power.state) {221acpi_handle_debug(device->handle,222"Cannot transition from %s to %s\n",223acpi_power_state_string(device->power.state),224acpi_power_state_string(state));225return -ENODEV;226}227228/*229* If the device goes from D3hot to D3cold, _PS3 has been230* evaluated for it already, so skip it in that case.231*/232if (device->power.state < ACPI_STATE_D3_HOT) {233result = acpi_dev_pm_explicit_set(device, state);234if (result)235goto end;236}237238if (device->power.flags.power_resources)239result = acpi_power_transition(device, target_state);240} else {241int cur_state = device->power.state;242243if (device->power.flags.power_resources) {244result = acpi_power_transition(device, ACPI_STATE_D0);245if (result)246goto end;247}248249if (cur_state == ACPI_STATE_D0) {250int psc;251252/* Nothing to do here if _PSC is not present. */253if (!device->power.flags.explicit_get)254goto no_change;255256/*257* The power state of the device was set to D0 last258* time, but that might have happened before a259* system-wide transition involving the platform260* firmware, so it may be necessary to evaluate _PS0261* for the device here. However, use extra care here262* and evaluate _PSC to check the device's current power263* state, and only invoke _PS0 if the evaluation of _PSC264* is successful and it returns a power state different265* from D0.266*/267result = acpi_dev_pm_explicit_get(device, &psc);268if (result || psc == ACPI_STATE_D0)269goto no_change;270}271272result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);273}274275end:276if (result) {277acpi_handle_debug(device->handle,278"Failed to change power state to %s\n",279acpi_power_state_string(target_state));280} else {281device->power.state = target_state;282acpi_handle_debug(device->handle, "Power state changed to %s\n",283acpi_power_state_string(target_state));284}285286return result;287288no_change:289acpi_handle_debug(device->handle, "Already in %s\n",290acpi_power_state_string(state));291return 0;292}293EXPORT_SYMBOL(acpi_device_set_power);294295int acpi_bus_set_power(acpi_handle handle, int state)296{297struct acpi_device *device = acpi_fetch_acpi_dev(handle);298299if (device)300return acpi_device_set_power(device, state);301302return -ENODEV;303}304EXPORT_SYMBOL(acpi_bus_set_power);305306int acpi_bus_init_power(struct acpi_device *device)307{308int state;309int result;310311if (!device)312return -EINVAL;313314device->power.state = ACPI_STATE_UNKNOWN;315if (!acpi_device_is_present(device)) {316device->flags.initialized = false;317return -ENXIO;318}319320result = acpi_device_get_power(device, &state);321if (result)322return result;323324if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {325/* Reference count the power resources. */326result = acpi_power_on_resources(device, state);327if (result)328return result;329330if (state == ACPI_STATE_D0) {331/*332* If _PSC is not present and the state inferred from333* power resources appears to be D0, it still may be334* necessary to execute _PS0 at this point, because335* another device using the same power resources may336* have been put into D0 previously and that's why we337* see D0 here.338*/339result = acpi_dev_pm_explicit_set(device, state);340if (result)341return result;342}343} else if (state == ACPI_STATE_UNKNOWN) {344/*345* No power resources and missing _PSC? Cross fingers and make346* it D0 in hope that this is what the BIOS put the device into.347* [We tried to force D0 here by executing _PS0, but that broke348* Toshiba P870-303 in a nasty way.]349*/350state = ACPI_STATE_D0;351}352device->power.state = state;353return 0;354}355356/**357* acpi_device_fix_up_power - Force device with missing _PSC into D0.358* @device: Device object whose power state is to be fixed up.359*360* Devices without power resources and _PSC, but having _PS0 and _PS3 defined,361* are assumed to be put into D0 by the BIOS. However, in some cases that may362* not be the case and this function should be used then.363*/364int acpi_device_fix_up_power(struct acpi_device *device)365{366int ret = 0;367368if (!device->power.flags.power_resources369&& !device->power.flags.explicit_get370&& device->power.state == ACPI_STATE_D0)371ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);372373return ret;374}375EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);376377static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used)378{379if (adev->status.present && adev->status.enabled)380acpi_device_fix_up_power(adev);381382return 0;383}384385/**386* acpi_device_fix_up_power_extended - Force device and its children into D0.387* @adev: Parent device object whose power state is to be fixed up.388*389* Call acpi_device_fix_up_power() for @adev and its children so long as they390* are reported as present and enabled.391*/392void acpi_device_fix_up_power_extended(struct acpi_device *adev)393{394acpi_device_fix_up_power(adev);395acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);396}397EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended);398399/**400* acpi_device_fix_up_power_children - Force a device's children into D0.401* @adev: Parent device object whose children's power state is to be fixed up.402*403* Call acpi_device_fix_up_power() for @adev's children so long as they404* are reported as present and enabled.405*/406void acpi_device_fix_up_power_children(struct acpi_device *adev)407{408acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);409}410EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_children);411412int acpi_device_update_power(struct acpi_device *device, int *state_p)413{414int state;415int result;416417if (device->power.state == ACPI_STATE_UNKNOWN) {418result = acpi_bus_init_power(device);419if (!result && state_p)420*state_p = device->power.state;421422return result;423}424425result = acpi_device_get_power(device, &state);426if (result)427return result;428429if (state == ACPI_STATE_UNKNOWN) {430state = ACPI_STATE_D0;431result = acpi_device_set_power(device, state);432if (result)433return result;434} else {435if (device->power.flags.power_resources) {436/*437* We don't need to really switch the state, bu we need438* to update the power resources' reference counters.439*/440result = acpi_power_transition(device, state);441if (result)442return result;443}444device->power.state = state;445}446if (state_p)447*state_p = state;448449return 0;450}451EXPORT_SYMBOL_GPL(acpi_device_update_power);452453int acpi_bus_update_power(acpi_handle handle, int *state_p)454{455struct acpi_device *device = acpi_fetch_acpi_dev(handle);456457if (device)458return acpi_device_update_power(device, state_p);459460return -ENODEV;461}462EXPORT_SYMBOL_GPL(acpi_bus_update_power);463464bool acpi_bus_power_manageable(acpi_handle handle)465{466struct acpi_device *device = acpi_fetch_acpi_dev(handle);467468return device && device->flags.power_manageable;469}470EXPORT_SYMBOL(acpi_bus_power_manageable);471472static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)473{474if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))475return 0;476477acpi_handle_debug(adev->handle, "Power state: %s\n",478acpi_power_state_string(adev->power.state));479480if (adev->power.state == ACPI_STATE_D3_COLD)481return acpi_device_set_power(adev, ACPI_STATE_D0);482483return 0;484}485486/**487* acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR488* @adev: Parent ACPI device object.489*490* Change the power states of the direct children of @adev that are in D3cold491* and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI)492* enumeration code to access them.493*/494void acpi_dev_power_up_children_with_adr(struct acpi_device *adev)495{496acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL);497}498499/**500* acpi_dev_power_state_for_wake - Deepest power state for wakeup signaling501* @adev: ACPI companion of the target device.502*503* Evaluate _S0W for @adev and return the value produced by it or return504* ACPI_STATE_UNKNOWN on errors (including _S0W not present).505*/506u8 acpi_dev_power_state_for_wake(struct acpi_device *adev)507{508unsigned long long state;509acpi_status status;510511status = acpi_evaluate_integer(adev->handle, "_S0W", NULL, &state);512if (ACPI_FAILURE(status))513return ACPI_STATE_UNKNOWN;514515return state;516}517518#ifdef CONFIG_PM519static DEFINE_MUTEX(acpi_pm_notifier_lock);520static DEFINE_MUTEX(acpi_pm_notifier_install_lock);521522void acpi_pm_wakeup_event(struct device *dev)523{524pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());525}526EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);527528static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)529{530struct acpi_device *adev;531532if (val != ACPI_NOTIFY_DEVICE_WAKE)533return;534535acpi_handle_debug(handle, "Wake notify\n");536537adev = acpi_get_acpi_dev(handle);538if (!adev)539return;540541mutex_lock(&acpi_pm_notifier_lock);542543if (adev->wakeup.flags.notifier_present) {544pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());545if (adev->wakeup.context.func) {546acpi_handle_debug(handle, "Running %pS for %s\n",547adev->wakeup.context.func,548dev_name(adev->wakeup.context.dev));549adev->wakeup.context.func(&adev->wakeup.context);550}551}552553mutex_unlock(&acpi_pm_notifier_lock);554555acpi_put_acpi_dev(adev);556}557558/**559* acpi_add_pm_notifier - Register PM notify handler for given ACPI device.560* @adev: ACPI device to add the notify handler for.561* @dev: Device to generate a wakeup event for while handling the notification.562* @func: Work function to execute when handling the notification.563*564* NOTE: @adev need not be a run-wake or wakeup device to be a valid source of565* PM wakeup events. For example, wakeup events may be generated for bridges566* if one of the devices below the bridge is signaling wakeup, even if the567* bridge itself doesn't have a wakeup GPE associated with it.568*/569acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,570void (*func)(struct acpi_device_wakeup_context *context))571{572acpi_status status = AE_ALREADY_EXISTS;573574if (!dev && !func)575return AE_BAD_PARAMETER;576577mutex_lock(&acpi_pm_notifier_install_lock);578579if (adev->wakeup.flags.notifier_present)580goto out;581582status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,583acpi_pm_notify_handler, NULL);584if (ACPI_FAILURE(status))585goto out;586587mutex_lock(&acpi_pm_notifier_lock);588adev->wakeup.ws = wakeup_source_register(&adev->dev,589dev_name(&adev->dev));590adev->wakeup.context.dev = dev;591adev->wakeup.context.func = func;592adev->wakeup.flags.notifier_present = true;593mutex_unlock(&acpi_pm_notifier_lock);594595out:596mutex_unlock(&acpi_pm_notifier_install_lock);597return status;598}599600/**601* acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.602* @adev: ACPI device to remove the notifier from.603*/604acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)605{606acpi_status status = AE_BAD_PARAMETER;607608mutex_lock(&acpi_pm_notifier_install_lock);609610if (!adev->wakeup.flags.notifier_present)611goto out;612613status = acpi_remove_notify_handler(adev->handle,614ACPI_SYSTEM_NOTIFY,615acpi_pm_notify_handler);616if (ACPI_FAILURE(status))617goto out;618619mutex_lock(&acpi_pm_notifier_lock);620adev->wakeup.context.func = NULL;621adev->wakeup.context.dev = NULL;622wakeup_source_unregister(adev->wakeup.ws);623adev->wakeup.flags.notifier_present = false;624mutex_unlock(&acpi_pm_notifier_lock);625626out:627mutex_unlock(&acpi_pm_notifier_install_lock);628return status;629}630631bool acpi_bus_can_wakeup(acpi_handle handle)632{633struct acpi_device *device = acpi_fetch_acpi_dev(handle);634635return device && device->wakeup.flags.valid;636}637EXPORT_SYMBOL(acpi_bus_can_wakeup);638639bool acpi_pm_device_can_wakeup(struct device *dev)640{641struct acpi_device *adev = ACPI_COMPANION(dev);642643return adev ? acpi_device_can_wakeup(adev) : false;644}645646/**647* acpi_dev_pm_get_state - Get preferred power state of ACPI device.648* @dev: Device whose preferred target power state to return.649* @adev: ACPI device node corresponding to @dev.650* @target_state: System state to match the resultant device state.651* @d_min_p: Location to store the highest power state available to the device.652* @d_max_p: Location to store the lowest power state available to the device.653*654* Find the lowest power (highest number) and highest power (lowest number) ACPI655* device power states that the device can be in while the system is in the656* state represented by @target_state. Store the integer numbers representing657* those stats in the memory locations pointed to by @d_max_p and @d_min_p,658* respectively.659*660* Callers must ensure that @dev and @adev are valid pointers and that @adev661* actually corresponds to @dev before using this function.662*663* Returns 0 on success or -ENODATA when one of the ACPI methods fails or664* returns a value that doesn't make sense. The memory locations pointed to by665* @d_max_p and @d_min_p are only modified on success.666*/667static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,668u32 target_state, int *d_min_p, int *d_max_p)669{670char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };671acpi_handle handle = adev->handle;672unsigned long long ret;673int d_min, d_max;674bool wakeup = false;675bool has_sxd = false;676acpi_status status;677678/*679* If the system state is S0, the lowest power state the device can be680* in is D3cold, unless the device has _S0W and is supposed to signal681* wakeup, in which case the return value of _S0W has to be used as the682* lowest power state available to the device.683*/684d_min = ACPI_STATE_D0;685d_max = ACPI_STATE_D3_COLD;686687/*688* If present, _SxD methods return the minimum D-state (highest power689* state) we can use for the corresponding S-states. Otherwise, the690* minimum D-state is D0 (ACPI 3.x).691*/692if (target_state > ACPI_STATE_S0) {693/*694* We rely on acpi_evaluate_integer() not clobbering the integer695* provided if AE_NOT_FOUND is returned.696*/697ret = d_min;698status = acpi_evaluate_integer(handle, method, NULL, &ret);699if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)700|| ret > ACPI_STATE_D3_COLD)701return -ENODATA;702703/*704* We need to handle legacy systems where D3hot and D3cold are705* the same and 3 is returned in both cases, so fall back to706* D3cold if D3hot is not a valid state.707*/708if (!adev->power.states[ret].flags.valid) {709if (ret == ACPI_STATE_D3_HOT)710ret = ACPI_STATE_D3_COLD;711else712return -ENODATA;713}714715if (status == AE_OK)716has_sxd = true;717718d_min = ret;719wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid720&& adev->wakeup.sleep_state >= target_state;721} else if (device_may_wakeup(dev) && dev->power.wakeirq) {722/*723* The ACPI subsystem doesn't manage the wake bit for IRQs724* defined with ExclusiveAndWake and SharedAndWake. Instead we725* expect them to be managed via the PM subsystem. Drivers726* should call dev_pm_set_wake_irq to register an IRQ as a wake727* source.728*729* If a device has a wake IRQ attached we need to check the730* _S0W method to get the correct wake D-state. Otherwise we731* end up putting the device into D3Cold which will more than732* likely disable wake functionality.733*/734wakeup = true;735} else {736/* ACPI GPE is specified in _PRW. */737wakeup = adev->wakeup.flags.valid;738}739740/*741* If _PRW says we can wake up the system from the target sleep state,742* the D-state returned by _SxD is sufficient for that (we assume a743* wakeup-aware driver if wake is set). Still, if _SxW exists744* (ACPI 3.x), it should return the maximum (lowest power) D-state that745* can wake the system. _S0W may be valid, too.746*/747if (wakeup) {748method[3] = 'W';749status = acpi_evaluate_integer(handle, method, NULL, &ret);750if (status == AE_NOT_FOUND) {751/* No _SxW. In this case, the ACPI spec says that we752* must not go into any power state deeper than the753* value returned from _SxD.754*/755if (has_sxd && target_state > ACPI_STATE_S0)756d_max = d_min;757} else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {758/* Fall back to D3cold if ret is not a valid state. */759if (!adev->power.states[ret].flags.valid)760ret = ACPI_STATE_D3_COLD;761762d_max = ret > d_min ? ret : d_min;763} else {764return -ENODATA;765}766}767768if (d_min_p)769*d_min_p = d_min;770771if (d_max_p)772*d_max_p = d_max;773774return 0;775}776777/**778* acpi_pm_device_sleep_state - Get preferred power state of ACPI device.779* @dev: Device whose preferred target power state to return.780* @d_min_p: Location to store the upper limit of the allowed states range.781* @d_max_in: Deepest low-power state to take into consideration.782* Return value: Preferred power state of the device on success, -ENODEV783* if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is784* incorrect, or -ENODATA on ACPI method failure.785*786* The caller must ensure that @dev is valid before using this function.787*/788int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)789{790struct acpi_device *adev;791int ret, d_min, d_max;792793if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)794return -EINVAL;795796if (d_max_in > ACPI_STATE_D2) {797enum pm_qos_flags_status stat;798799stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);800if (stat == PM_QOS_FLAGS_ALL)801d_max_in = ACPI_STATE_D2;802}803804adev = ACPI_COMPANION(dev);805if (!adev) {806dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);807return -ENODEV;808}809810ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),811&d_min, &d_max);812if (ret)813return ret;814815if (d_max_in < d_min)816return -EINVAL;817818if (d_max > d_max_in) {819for (d_max = d_max_in; d_max > d_min; d_max--) {820if (adev->power.states[d_max].flags.valid)821break;822}823}824825if (d_min_p)826*d_min_p = d_min;827828return d_max;829}830EXPORT_SYMBOL(acpi_pm_device_sleep_state);831832/**833* acpi_pm_notify_work_func - ACPI devices wakeup notification work function.834* @context: Device wakeup context.835*/836static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)837{838struct device *dev = context->dev;839840if (dev) {841pm_wakeup_event(dev, 0);842pm_request_resume(dev);843}844}845846static DEFINE_MUTEX(acpi_wakeup_lock);847848static int __acpi_device_wakeup_enable(struct acpi_device *adev,849u32 target_state)850{851struct acpi_device_wakeup *wakeup = &adev->wakeup;852acpi_status status;853int error = 0;854855mutex_lock(&acpi_wakeup_lock);856857/*858* If the device wakeup power is already enabled, disable it and enable859* it again in case it depends on the configuration of subordinate860* devices and the conditions have changed since it was enabled last861* time.862*/863if (wakeup->enable_count > 0)864acpi_disable_wakeup_device_power(adev);865866error = acpi_enable_wakeup_device_power(adev, target_state);867if (error) {868if (wakeup->enable_count > 0) {869acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);870wakeup->enable_count = 0;871}872goto out;873}874875if (wakeup->enable_count > 0)876goto inc;877878status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);879if (ACPI_FAILURE(status)) {880acpi_disable_wakeup_device_power(adev);881error = -EIO;882goto out;883}884885acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",886(unsigned int)wakeup->gpe_number);887888inc:889if (wakeup->enable_count < INT_MAX)890wakeup->enable_count++;891else892acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");893894out:895mutex_unlock(&acpi_wakeup_lock);896return error;897}898899/**900* acpi_device_wakeup_enable - Enable wakeup functionality for device.901* @adev: ACPI device to enable wakeup functionality for.902* @target_state: State the system is transitioning into.903*904* Enable the GPE associated with @adev so that it can generate wakeup signals905* for the device in response to external (remote) events and enable wakeup906* power for it.907*908* Callers must ensure that @adev is a valid ACPI device node before executing909* this function.910*/911static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)912{913return __acpi_device_wakeup_enable(adev, target_state);914}915916/**917* acpi_device_wakeup_disable - Disable wakeup functionality for device.918* @adev: ACPI device to disable wakeup functionality for.919*920* Disable the GPE associated with @adev and disable wakeup power for it.921*922* Callers must ensure that @adev is a valid ACPI device node before executing923* this function.924*/925static void acpi_device_wakeup_disable(struct acpi_device *adev)926{927struct acpi_device_wakeup *wakeup = &adev->wakeup;928929mutex_lock(&acpi_wakeup_lock);930931if (!wakeup->enable_count)932goto out;933934acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);935acpi_disable_wakeup_device_power(adev);936937wakeup->enable_count--;938939out:940mutex_unlock(&acpi_wakeup_lock);941}942943/**944* acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.945* @dev: Device to enable/disable to generate wakeup events.946* @enable: Whether to enable or disable the wakeup functionality.947*/948int acpi_pm_set_device_wakeup(struct device *dev, bool enable)949{950struct acpi_device *adev;951int error;952953adev = ACPI_COMPANION(dev);954if (!adev) {955dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);956return -ENODEV;957}958959if (!acpi_device_can_wakeup(adev))960return -EINVAL;961962if (!enable) {963acpi_device_wakeup_disable(adev);964dev_dbg(dev, "Wakeup disabled by ACPI\n");965return 0;966}967968error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());969if (!error)970dev_dbg(dev, "Wakeup enabled by ACPI\n");971972return error;973}974EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);975976/**977* acpi_dev_pm_low_power - Put ACPI device into a low-power state.978* @dev: Device to put into a low-power state.979* @adev: ACPI device node corresponding to @dev.980* @system_state: System state to choose the device state for.981*/982static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,983u32 system_state)984{985int ret, state;986987if (!acpi_device_power_manageable(adev))988return 0;989990ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);991return ret ? ret : acpi_device_set_power(adev, state);992}993994/**995* acpi_dev_pm_full_power - Put ACPI device into the full-power state.996* @adev: ACPI device node to put into the full-power state.997*/998static int acpi_dev_pm_full_power(struct acpi_device *adev)999{1000return acpi_device_power_manageable(adev) ?1001acpi_device_set_power(adev, ACPI_STATE_D0) : 0;1002}10031004/**1005* acpi_dev_suspend - Put device into a low-power state using ACPI.1006* @dev: Device to put into a low-power state.1007* @wakeup: Whether or not to enable wakeup for the device.1008*1009* Put the given device into a low-power state using the standard ACPI1010* mechanism. Set up remote wakeup if desired, choose the state to put the1011* device into (this checks if remote wakeup is expected to work too), and set1012* the power state of the device.1013*/1014int acpi_dev_suspend(struct device *dev, bool wakeup)1015{1016struct acpi_device *adev = ACPI_COMPANION(dev);1017u32 target_state = acpi_target_system_state();1018int error;10191020if (!adev)1021return 0;10221023if (wakeup && acpi_device_can_wakeup(adev)) {1024error = acpi_device_wakeup_enable(adev, target_state);1025if (error)1026return -EAGAIN;1027} else {1028wakeup = false;1029}10301031error = acpi_dev_pm_low_power(dev, adev, target_state);1032if (error && wakeup)1033acpi_device_wakeup_disable(adev);10341035return error;1036}1037EXPORT_SYMBOL_GPL(acpi_dev_suspend);10381039/**1040* acpi_dev_resume - Put device into the full-power state using ACPI.1041* @dev: Device to put into the full-power state.1042*1043* Put the given device into the full-power state using the standard ACPI1044* mechanism. Set the power state of the device to ACPI D0 and disable wakeup.1045*/1046int acpi_dev_resume(struct device *dev)1047{1048struct acpi_device *adev = ACPI_COMPANION(dev);1049int error;10501051if (!adev)1052return 0;10531054error = acpi_dev_pm_full_power(adev);1055acpi_device_wakeup_disable(adev);1056return error;1057}1058EXPORT_SYMBOL_GPL(acpi_dev_resume);10591060/**1061* acpi_subsys_runtime_suspend - Suspend device using ACPI.1062* @dev: Device to suspend.1063*1064* Carry out the generic runtime suspend procedure for @dev and use ACPI to put1065* it into a runtime low-power state.1066*/1067int acpi_subsys_runtime_suspend(struct device *dev)1068{1069int ret = pm_generic_runtime_suspend(dev);10701071return ret ? ret : acpi_dev_suspend(dev, true);1072}1073EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);10741075/**1076* acpi_subsys_runtime_resume - Resume device using ACPI.1077* @dev: Device to Resume.1078*1079* Use ACPI to put the given device into the full-power state and carry out the1080* generic runtime resume procedure for it.1081*/1082int acpi_subsys_runtime_resume(struct device *dev)1083{1084int ret = acpi_dev_resume(dev);10851086return ret ? ret : pm_generic_runtime_resume(dev);1087}1088EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);10891090#ifdef CONFIG_PM_SLEEP1091static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)1092{1093u32 sys_target = acpi_target_system_state();1094int ret, state;10951096if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&1097device_may_wakeup(dev) != !!adev->wakeup.prepare_count))1098return true;10991100if (sys_target == ACPI_STATE_S0)1101return false;11021103if (adev->power.flags.dsw_present)1104return true;11051106ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);1107if (ret)1108return true;11091110return state != adev->power.state;1111}11121113/**1114* acpi_subsys_prepare - Prepare device for system transition to a sleep state.1115* @dev: Device to prepare.1116*/1117int acpi_subsys_prepare(struct device *dev)1118{1119struct acpi_device *adev = ACPI_COMPANION(dev);11201121dev_pm_set_strict_midlayer(dev, true);11221123if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {1124int ret = dev->driver->pm->prepare(dev);11251126if (ret < 0)1127return ret;11281129if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))1130return 0;1131}11321133return !acpi_dev_needs_resume(dev, adev);1134}1135EXPORT_SYMBOL_GPL(acpi_subsys_prepare);11361137/**1138* acpi_subsys_complete - Finalize device's resume during system resume.1139* @dev: Device to handle.1140*/1141void acpi_subsys_complete(struct device *dev)1142{1143pm_generic_complete(dev);1144/*1145* If the device had been runtime-suspended before the system went into1146* the sleep state it is going out of and it has never been resumed till1147* now, resume it in case the firmware powered it up.1148*/1149if (pm_runtime_suspended(dev) && pm_resume_via_firmware())1150pm_request_resume(dev);11511152dev_pm_set_strict_midlayer(dev, false);1153}1154EXPORT_SYMBOL_GPL(acpi_subsys_complete);11551156/**1157* acpi_subsys_suspend - Run the device driver's suspend callback.1158* @dev: Device to handle.1159*1160* Follow PCI and resume devices from runtime suspend before running their1161* system suspend callbacks, unless the driver can cope with runtime-suspended1162* devices during system suspend and there are no ACPI-specific reasons for1163* resuming them.1164*/1165int acpi_subsys_suspend(struct device *dev)1166{1167if (!dev_pm_smart_suspend(dev) ||1168acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))1169pm_runtime_resume(dev);11701171return pm_generic_suspend(dev);1172}1173EXPORT_SYMBOL_GPL(acpi_subsys_suspend);11741175/**1176* acpi_subsys_suspend_late - Suspend device using ACPI.1177* @dev: Device to suspend.1178*1179* Carry out the generic late suspend procedure for @dev and use ACPI to put1180* it into a low-power state during system transition into a sleep state.1181*/1182int acpi_subsys_suspend_late(struct device *dev)1183{1184int ret;11851186if (dev_pm_skip_suspend(dev))1187return 0;11881189ret = pm_generic_suspend_late(dev);1190return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));1191}1192EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);11931194/**1195* acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.1196* @dev: Device to suspend.1197*/1198int acpi_subsys_suspend_noirq(struct device *dev)1199{1200int ret;12011202if (dev_pm_skip_suspend(dev))1203return 0;12041205ret = pm_generic_suspend_noirq(dev);1206if (ret)1207return ret;12081209/*1210* If the target system sleep state is suspend-to-idle, it is sufficient1211* to check whether or not the device's wakeup settings are good for1212* runtime PM. Otherwise, the pm_resume_via_firmware() check will cause1213* acpi_subsys_complete() to take care of fixing up the device's state1214* anyway, if need be.1215*/1216if (device_can_wakeup(dev) && !device_may_wakeup(dev))1217dev->power.may_skip_resume = false;12181219return 0;1220}1221EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);12221223/**1224* acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.1225* @dev: Device to handle.1226*/1227static int acpi_subsys_resume_noirq(struct device *dev)1228{1229if (dev_pm_skip_resume(dev))1230return 0;12311232return pm_generic_resume_noirq(dev);1233}12341235/**1236* acpi_subsys_resume_early - Resume device using ACPI.1237* @dev: Device to Resume.1238*1239* Use ACPI to put the given device into the full-power state and carry out the1240* generic early resume procedure for it during system transition into the1241* working state, but only do that if device either defines early resume1242* handler, or does not define power operations at all. Otherwise powering up1243* of the device is postponed to the normal resume phase.1244*/1245static int acpi_subsys_resume_early(struct device *dev)1246{1247const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;1248int ret;12491250if (dev_pm_skip_resume(dev))1251return 0;12521253if (pm && !pm->resume_early) {1254dev_dbg(dev, "postponing D0 transition to normal resume stage\n");1255return 0;1256}12571258ret = acpi_dev_resume(dev);1259return ret ? ret : pm_generic_resume_early(dev);1260}12611262/**1263* acpi_subsys_resume - Resume device using ACPI.1264* @dev: Device to Resume.1265*1266* Use ACPI to put the given device into the full-power state if it has not been1267* powered up during early resume phase, and carry out the generic resume1268* procedure for it during system transition into the working state.1269*/1270static int acpi_subsys_resume(struct device *dev)1271{1272const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;1273int ret = 0;12741275if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) {1276dev_dbg(dev, "executing postponed D0 transition\n");1277ret = acpi_dev_resume(dev);1278}12791280return ret ? ret : pm_generic_resume(dev);1281}12821283/**1284* acpi_subsys_freeze - Run the device driver's freeze callback.1285* @dev: Device to handle.1286*/1287int acpi_subsys_freeze(struct device *dev)1288{1289/*1290* Resume all runtime-suspended devices before creating a snapshot1291* image of system memory, because the restore kernel generally cannot1292* be expected to always handle them consistently and they need to be1293* put into the runtime-active metastate during system resume anyway,1294* so it is better to ensure that the state saved in the image will be1295* always consistent with that.1296*/1297pm_runtime_resume(dev);12981299return pm_generic_freeze(dev);1300}1301EXPORT_SYMBOL_GPL(acpi_subsys_freeze);13021303/**1304* acpi_subsys_restore_early - Restore device using ACPI.1305* @dev: Device to restore.1306*/1307int acpi_subsys_restore_early(struct device *dev)1308{1309int ret = acpi_dev_resume(dev);13101311return ret ? ret : pm_generic_restore_early(dev);1312}1313EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);13141315/**1316* acpi_subsys_poweroff - Run the device driver's poweroff callback.1317* @dev: Device to handle.1318*1319* Follow PCI and resume devices from runtime suspend before running their1320* system poweroff callbacks, unless the driver can cope with runtime-suspended1321* devices during system suspend and there are no ACPI-specific reasons for1322* resuming them.1323*/1324int acpi_subsys_poweroff(struct device *dev)1325{1326if (!dev_pm_smart_suspend(dev) ||1327acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))1328pm_runtime_resume(dev);13291330return pm_generic_poweroff(dev);1331}1332EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);13331334/**1335* acpi_subsys_poweroff_late - Run the device driver's poweroff callback.1336* @dev: Device to handle.1337*1338* Carry out the generic late poweroff procedure for @dev and use ACPI to put1339* it into a low-power state during system transition into a sleep state.1340*/1341static int acpi_subsys_poweroff_late(struct device *dev)1342{1343int ret;13441345if (dev_pm_skip_suspend(dev))1346return 0;13471348ret = pm_generic_poweroff_late(dev);1349if (ret)1350return ret;13511352return acpi_dev_suspend(dev, device_may_wakeup(dev));1353}13541355/**1356* acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.1357* @dev: Device to suspend.1358*/1359static int acpi_subsys_poweroff_noirq(struct device *dev)1360{1361if (dev_pm_skip_suspend(dev))1362return 0;13631364return pm_generic_poweroff_noirq(dev);1365}1366#endif /* CONFIG_PM_SLEEP */13671368static void acpi_dev_pm_detach(struct device *dev, bool power_off);13691370static struct dev_pm_domain acpi_general_pm_domain = {1371.ops = {1372.runtime_suspend = acpi_subsys_runtime_suspend,1373.runtime_resume = acpi_subsys_runtime_resume,1374#ifdef CONFIG_PM_SLEEP1375.prepare = acpi_subsys_prepare,1376.complete = acpi_subsys_complete,1377.suspend = acpi_subsys_suspend,1378.resume = acpi_subsys_resume,1379.suspend_late = acpi_subsys_suspend_late,1380.suspend_noirq = acpi_subsys_suspend_noirq,1381.resume_noirq = acpi_subsys_resume_noirq,1382.resume_early = acpi_subsys_resume_early,1383.freeze = acpi_subsys_freeze,1384.poweroff = acpi_subsys_poweroff,1385.poweroff_late = acpi_subsys_poweroff_late,1386.poweroff_noirq = acpi_subsys_poweroff_noirq,1387.restore_early = acpi_subsys_restore_early,1388#endif1389},1390.detach = acpi_dev_pm_detach,1391};13921393/**1394* acpi_dev_pm_detach - Remove ACPI power management from the device.1395* @dev: Device to take care of.1396* @power_off: Whether or not to try to remove power from the device.1397*1398* Remove the device from the general ACPI PM domain and remove its wakeup1399* notifier. If @power_off is set, additionally remove power from the device if1400* possible.1401*1402* Callers must ensure proper synchronization of this function with power1403* management callbacks.1404*/1405static void acpi_dev_pm_detach(struct device *dev, bool power_off)1406{1407struct acpi_device *adev = ACPI_COMPANION(dev);14081409if (adev && dev->pm_domain == &acpi_general_pm_domain) {1410dev_pm_domain_set(dev, NULL);1411acpi_remove_pm_notifier(adev);1412if (power_off) {1413/*1414* If the device's PM QoS resume latency limit or flags1415* have been exposed to user space, they have to be1416* hidden at this point, so that they don't affect the1417* choice of the low-power state to put the device into.1418*/1419dev_pm_qos_hide_latency_limit(dev);1420dev_pm_qos_hide_flags(dev);1421acpi_device_wakeup_disable(adev);1422acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);1423}1424}1425}14261427/**1428* acpi_dev_pm_attach - Prepare device for ACPI power management.1429* @dev: Device to prepare.1430* @power_on: Whether or not to power on the device.1431*1432* If @dev has a valid ACPI handle that has a valid struct acpi_device object1433* attached to it, install a wakeup notification handler for the device and1434* add it to the general ACPI PM domain. If @power_on is set, the device will1435* be put into the ACPI D0 state before the function returns.1436*1437* This assumes that the @dev's bus type uses generic power management callbacks1438* (or doesn't use any power management callbacks at all).1439*1440* Callers must ensure proper synchronization of this function with power1441* management callbacks.1442*/1443int acpi_dev_pm_attach(struct device *dev, bool power_on)1444{1445/*1446* Skip devices whose ACPI companions match the device IDs below,1447* because they require special power management handling incompatible1448* with the generic ACPI PM domain.1449*/1450static const struct acpi_device_id special_pm_ids[] = {1451ACPI_FAN_DEVICE_IDS,1452{}1453};1454struct acpi_device *adev = ACPI_COMPANION(dev);14551456if (!adev || !acpi_match_device_ids(adev, special_pm_ids))1457return 0;14581459/*1460* Only attach the power domain to the first device if the1461* companion is shared by multiple. This is to prevent doing power1462* management twice.1463*/1464if (!acpi_device_is_first_physical_node(adev, dev))1465return 0;14661467acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);1468dev_pm_domain_set(dev, &acpi_general_pm_domain);1469if (power_on) {1470acpi_dev_pm_full_power(adev);1471acpi_device_wakeup_disable(adev);1472}14731474return 1;1475}1476EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);14771478/**1479* acpi_storage_d3 - Check if D3 should be used in the suspend path1480* @dev: Device to check1481*1482* Return %true if the platform firmware wants @dev to be programmed1483* into D3hot or D3cold (if supported) in the suspend path, or %false1484* when there is no specific preference. On some platforms, if this1485* hint is ignored, @dev may remain unresponsive after suspending the1486* platform as a whole.1487*1488* Although the property has storage in the name it actually is1489* applied to the PCIe slot and plugging in a non-storage device the1490* same platform restrictions will likely apply.1491*/1492bool acpi_storage_d3(struct device *dev)1493{1494struct acpi_device *adev = ACPI_COMPANION(dev);1495u8 val;14961497if (force_storage_d3())1498return true;14991500if (!adev)1501return false;1502if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",1503&val))1504return false;1505return val == 1;1506}1507EXPORT_SYMBOL_GPL(acpi_storage_d3);15081509/**1510* acpi_dev_state_d0 - Tell if the device is in D0 power state1511* @dev: Physical device the ACPI power state of which to check1512*1513* On a system without ACPI, return true. On a system with ACPI, return true if1514* the current ACPI power state of the device is D0, or false otherwise.1515*1516* Note that the power state of a device is not well-defined after it has been1517* passed to acpi_device_set_power() and before that function returns, so it is1518* not valid to ask for the ACPI power state of the device in that time frame.1519*1520* This function is intended to be used in a driver's probe or remove1521* function. See Documentation/firmware-guide/acpi/non-d0-probe.rst for1522* more information.1523*/1524bool acpi_dev_state_d0(struct device *dev)1525{1526struct acpi_device *adev = ACPI_COMPANION(dev);15271528if (!adev)1529return true;15301531return adev->power.state == ACPI_STATE_D0;1532}1533EXPORT_SYMBOL_GPL(acpi_dev_state_d0);15341535#endif /* CONFIG_PM */153615371538