···204204205205 This attribute has no effect on system-wide suspend/resume and206206 hibernation.207207+208208+What: /sys/devices/.../power/pm_qos_no_power_off209209+Date: September 2012210210+Contact: Rafael J. Wysocki <rjw@sisk.pl>211211+Description:212212+ The /sys/devices/.../power/pm_qos_no_power_off attribute213213+ is used for manipulating the PM QoS "no power off" flag. If214214+ set, this flag indicates to the kernel that power should not215215+ be removed entirely from the device.216216+217217+ Not all drivers support this attribute. If it isn't supported,218218+ it is not present.219219+220220+ This attribute has no effect on system-wide suspend/resume and221221+ hibernation.222222+223223+What: /sys/devices/.../power/pm_qos_remote_wakeup224224+Date: September 2012225225+Contact: Rafael J. Wysocki <rjw@sisk.pl>226226+Description:227227+ The /sys/devices/.../power/pm_qos_remote_wakeup attribute228228+ is used for manipulating the PM QoS "remote wakeup required"229229+ flag. If set, this flag indicates to the kernel that the230230+ device is a source of user events that have to be signaled from231231+ its low-power states.232232+233233+ Not all drivers support this attribute. If it isn't supported,234234+ it is not present.235235+236236+ This attribute has no effect on system-wide suspend/resume and237237+ hibernation.
+1-1
Documentation/power/pm_qos_interface.txt
···9999100100From kernel mode the use of this interface is the following:101101102102-int dev_pm_qos_add_request(device, handle, value):102102+int dev_pm_qos_add_request(device, handle, type, value):103103Will insert an element into the list for that identified device with the104104target value. Upon change to this list the new target is recomputed and any105105registered notifiers are called only if the target value is now different.
+2-1
drivers/acpi/Makefile
···2121acpi-y += osl.o utils.o reboot.o2222acpi-y += nvs.o23232424-# sleep related files2424+# Power management related files2525acpi-y += wakeup.o2626acpi-y += sleep.o2727+acpi-$(CONFIG_PM) += device_pm.o2728acpi-$(CONFIG_ACPI_SLEEP) += proc.o28292930
+18-3
drivers/acpi/bus.c
···257257}258258259259260260-static int __acpi_bus_set_power(struct acpi_device *device, int state)260260+/**261261+ * acpi_device_set_power - Set power state of an ACPI device.262262+ * @device: Device to set the power state of.263263+ * @state: New power state to set.264264+ *265265+ * Callers must ensure that the device is power manageable before using this266266+ * function.267267+ */268268+int acpi_device_set_power(struct acpi_device *device, int state)261269{262270 int result = 0;263271 acpi_status status = AE_OK;···306298 * a lower-powered state.307299 */308300 if (state < device->power.state) {301301+ if (device->power.state >= ACPI_STATE_D3_HOT &&302302+ state != ACPI_STATE_D0) {303303+ printk(KERN_WARNING PREFIX304304+ "Cannot transition to non-D0 state from D3\n");305305+ return -ENODEV;306306+ }309307 if (device->power.flags.power_resources) {310308 result = acpi_power_transition(device, state);311309 if (result)···355341356342 return result;357343}344344+EXPORT_SYMBOL(acpi_device_set_power);358345359346360347int acpi_bus_set_power(acpi_handle handle, int state)···374359 return -ENODEV;375360 }376361377377- return __acpi_bus_set_power(device, state);362362+ return acpi_device_set_power(device, state);378363}379364EXPORT_SYMBOL(acpi_bus_set_power);380365···417402 if (result)418403 return result;419404420420- result = __acpi_bus_set_power(device, state);405405+ result = acpi_device_set_power(device, state);421406 if (!result && state_p)422407 *state_p = state;423408
+668
drivers/acpi/device_pm.c
···11+/*22+ * drivers/acpi/device_pm.c - ACPI device power management routines.33+ *44+ * Copyright (C) 2012, Intel Corp.55+ * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>66+ *77+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~88+ *99+ * This program is free software; you can redistribute it and/or modify1010+ * it under the terms of the GNU General Public License version 2 as published1111+ * by the Free Software Foundation.1212+ *1313+ * This program is distributed in the hope that it will be useful, but1414+ * WITHOUT ANY WARRANTY; without even the implied warranty of1515+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU1616+ * General Public License for more details.1717+ *1818+ * You should have received a copy of the GNU General Public License along1919+ * with this program; if not, write to the Free Software Foundation, Inc.,2020+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.2121+ *2222+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2323+ */2424+2525+#include <linux/device.h>2626+#include <linux/export.h>2727+#include <linux/mutex.h>2828+#include <linux/pm_qos.h>2929+#include <linux/pm_runtime.h>3030+3131+#include <acpi/acpi.h>3232+#include <acpi/acpi_bus.h>3333+3434+static DEFINE_MUTEX(acpi_pm_notifier_lock);3535+3636+/**3737+ * acpi_add_pm_notifier - Register PM notifier for given ACPI device.3838+ * @adev: ACPI device to add the notifier for.3939+ * @context: Context information to pass to the notifier routine.4040+ *4141+ * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of4242+ * PM wakeup events. For example, wakeup events may be generated for bridges4343+ * if one of the devices below the bridge is signaling wakeup, even if the4444+ * bridge itself doesn't have a wakeup GPE associated with it.4545+ */4646+acpi_status acpi_add_pm_notifier(struct acpi_device *adev,4747+ acpi_notify_handler handler, void *context)4848+{4949+ acpi_status status = AE_ALREADY_EXISTS;5050+5151+ mutex_lock(&acpi_pm_notifier_lock);5252+5353+ if (adev->wakeup.flags.notifier_present)5454+ goto out;5555+5656+ status = acpi_install_notify_handler(adev->handle,5757+ ACPI_SYSTEM_NOTIFY,5858+ handler, context);5959+ if (ACPI_FAILURE(status))6060+ goto out;6161+6262+ adev->wakeup.flags.notifier_present = true;6363+6464+ out:6565+ mutex_unlock(&acpi_pm_notifier_lock);6666+ return status;6767+}6868+6969+/**7070+ * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.7171+ * @adev: ACPI device to remove the notifier from.7272+ */7373+acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,7474+ acpi_notify_handler handler)7575+{7676+ acpi_status status = AE_BAD_PARAMETER;7777+7878+ mutex_lock(&acpi_pm_notifier_lock);7979+8080+ if (!adev->wakeup.flags.notifier_present)8181+ goto out;8282+8383+ status = acpi_remove_notify_handler(adev->handle,8484+ ACPI_SYSTEM_NOTIFY,8585+ handler);8686+ if (ACPI_FAILURE(status))8787+ goto out;8888+8989+ adev->wakeup.flags.notifier_present = false;9090+9191+ out:9292+ mutex_unlock(&acpi_pm_notifier_lock);9393+ return status;9494+}9595+9696+/**9797+ * acpi_device_power_state - Get preferred power state of ACPI device.9898+ * @dev: Device whose preferred target power state to return.9999+ * @adev: ACPI device node corresponding to @dev.100100+ * @target_state: System state to match the resultant device state.101101+ * @d_max_in: Deepest low-power state to take into consideration.102102+ * @d_min_p: Location to store the upper limit of the allowed states range.103103+ * Return value: Preferred power state of the device on success, -ENODEV104104+ * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure105105+ *106106+ * Find the lowest power (highest number) ACPI device power state that the107107+ * device can be in while the system is in the state represented by108108+ * @target_state. If @d_min_p is set, the highest power (lowest number) device109109+ * power state that @dev can be in for the given system sleep state is stored110110+ * at the location pointed to by it.111111+ *112112+ * Callers must ensure that @dev and @adev are valid pointers and that @adev113113+ * actually corresponds to @dev before using this function.114114+ */115115+int acpi_device_power_state(struct device *dev, struct acpi_device *adev,116116+ u32 target_state, int d_max_in, int *d_min_p)117117+{118118+ char acpi_method[] = "_SxD";119119+ unsigned long long d_min, d_max;120120+ bool wakeup = false;121121+122122+ if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)123123+ return -EINVAL;124124+125125+ if (d_max_in > ACPI_STATE_D3_HOT) {126126+ enum pm_qos_flags_status stat;127127+128128+ stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);129129+ if (stat == PM_QOS_FLAGS_ALL)130130+ d_max_in = ACPI_STATE_D3_HOT;131131+ }132132+133133+ acpi_method[2] = '0' + target_state;134134+ /*135135+ * If the sleep state is S0, the lowest limit from ACPI is D3,136136+ * but if the device has _S0W, we will use the value from _S0W137137+ * as the lowest limit from ACPI. Finally, we will constrain138138+ * the lowest limit with the specified one.139139+ */140140+ d_min = ACPI_STATE_D0;141141+ d_max = ACPI_STATE_D3;142142+143143+ /*144144+ * If present, _SxD methods return the minimum D-state (highest power145145+ * state) we can use for the corresponding S-states. Otherwise, the146146+ * minimum D-state is D0 (ACPI 3.x).147147+ *148148+ * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer149149+ * provided -- that's our fault recovery, we ignore retval.150150+ */151151+ if (target_state > ACPI_STATE_S0) {152152+ acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);153153+ wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid154154+ && adev->wakeup.sleep_state >= target_state;155155+ } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=156156+ PM_QOS_FLAGS_NONE) {157157+ wakeup = adev->wakeup.flags.valid;158158+ }159159+160160+ /*161161+ * If _PRW says we can wake up the system from the target sleep state,162162+ * the D-state returned by _SxD is sufficient for that (we assume a163163+ * wakeup-aware driver if wake is set). Still, if _SxW exists164164+ * (ACPI 3.x), it should return the maximum (lowest power) D-state that165165+ * can wake the system. _S0W may be valid, too.166166+ */167167+ if (wakeup) {168168+ acpi_status status;169169+170170+ acpi_method[3] = 'W';171171+ status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,172172+ &d_max);173173+ if (ACPI_FAILURE(status)) {174174+ if (target_state != ACPI_STATE_S0 ||175175+ status != AE_NOT_FOUND)176176+ d_max = d_min;177177+ } else if (d_max < d_min) {178178+ /* Warn the user of the broken DSDT */179179+ printk(KERN_WARNING "ACPI: Wrong value from %s\n",180180+ acpi_method);181181+ /* Sanitize it */182182+ d_min = d_max;183183+ }184184+ }185185+186186+ if (d_max_in < d_min)187187+ return -EINVAL;188188+ if (d_min_p)189189+ *d_min_p = d_min;190190+ /* constrain d_max with specified lowest limit (max number) */191191+ if (d_max > d_max_in) {192192+ for (d_max = d_max_in; d_max > d_min; d_max--) {193193+ if (adev->power.states[d_max].flags.valid)194194+ break;195195+ }196196+ }197197+ return d_max;198198+}199199+EXPORT_SYMBOL_GPL(acpi_device_power_state);200200+201201+/**202202+ * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.203203+ * @dev: Device whose preferred target power state to return.204204+ * @d_min_p: Location to store the upper limit of the allowed states range.205205+ * @d_max_in: Deepest low-power state to take into consideration.206206+ * Return value: Preferred power state of the device on success, -ENODEV207207+ * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure208208+ *209209+ * The caller must ensure that @dev is valid before using this function.210210+ */211211+int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)212212+{213213+ acpi_handle handle = DEVICE_ACPI_HANDLE(dev);214214+ struct acpi_device *adev;215215+216216+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {217217+ dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);218218+ return -ENODEV;219219+ }220220+221221+ return acpi_device_power_state(dev, adev, acpi_target_system_state(),222222+ d_max_in, d_min_p);223223+}224224+EXPORT_SYMBOL(acpi_pm_device_sleep_state);225225+226226+#ifdef CONFIG_PM_RUNTIME227227+/**228228+ * acpi_wakeup_device - Wakeup notification handler for ACPI devices.229229+ * @handle: ACPI handle of the device the notification is for.230230+ * @event: Type of the signaled event.231231+ * @context: Device corresponding to @handle.232232+ */233233+static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)234234+{235235+ struct device *dev = context;236236+237237+ if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {238238+ pm_wakeup_event(dev, 0);239239+ pm_runtime_resume(dev);240240+ }241241+}242242+243243+/**244244+ * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.245245+ * @adev: ACPI device to enable/disable the remote wakeup for.246246+ * @enable: Whether to enable or disable the wakeup functionality.247247+ *248248+ * Enable/disable the GPE associated with @adev so that it can generate249249+ * wakeup signals for the device in response to external (remote) events and250250+ * enable/disable device wakeup power.251251+ *252252+ * Callers must ensure that @adev is a valid ACPI device node before executing253253+ * this function.254254+ */255255+int __acpi_device_run_wake(struct acpi_device *adev, bool enable)256256+{257257+ struct acpi_device_wakeup *wakeup = &adev->wakeup;258258+259259+ if (enable) {260260+ acpi_status res;261261+ int error;262262+263263+ error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);264264+ if (error)265265+ return error;266266+267267+ res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);268268+ if (ACPI_FAILURE(res)) {269269+ acpi_disable_wakeup_device_power(adev);270270+ return -EIO;271271+ }272272+ } else {273273+ acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);274274+ acpi_disable_wakeup_device_power(adev);275275+ }276276+ return 0;277277+}278278+279279+/**280280+ * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.281281+ * @dev: Device to enable/disable the platform to wake up.282282+ * @enable: Whether to enable or disable the wakeup functionality.283283+ */284284+int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)285285+{286286+ struct acpi_device *adev;287287+ acpi_handle handle;288288+289289+ if (!device_run_wake(phys_dev))290290+ return -EINVAL;291291+292292+ handle = DEVICE_ACPI_HANDLE(phys_dev);293293+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {294294+ dev_dbg(phys_dev, "ACPI handle without context in %s!\n",295295+ __func__);296296+ return -ENODEV;297297+ }298298+299299+ return __acpi_device_run_wake(adev, enable);300300+}301301+EXPORT_SYMBOL(acpi_pm_device_run_wake);302302+#else303303+static inline void acpi_wakeup_device(acpi_handle handle, u32 event,304304+ void *context) {}305305+#endif /* CONFIG_PM_RUNTIME */306306+307307+ #ifdef CONFIG_PM_SLEEP308308+/**309309+ * __acpi_device_sleep_wake - Enable or disable device to wake up the system.310310+ * @dev: Device to enable/desible to wake up the system.311311+ * @target_state: System state the device is supposed to wake up from.312312+ * @enable: Whether to enable or disable @dev to wake up the system.313313+ */314314+int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,315315+ bool enable)316316+{317317+ return enable ?318318+ acpi_enable_wakeup_device_power(adev, target_state) :319319+ acpi_disable_wakeup_device_power(adev);320320+}321321+322322+/**323323+ * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.324324+ * @dev: Device to enable/desible to wake up the system from sleep states.325325+ * @enable: Whether to enable or disable @dev to wake up the system.326326+ */327327+int acpi_pm_device_sleep_wake(struct device *dev, bool enable)328328+{329329+ acpi_handle handle;330330+ struct acpi_device *adev;331331+ int error;332332+333333+ if (!device_can_wakeup(dev))334334+ return -EINVAL;335335+336336+ handle = DEVICE_ACPI_HANDLE(dev);337337+ if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {338338+ dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);339339+ return -ENODEV;340340+ }341341+342342+ error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),343343+ enable);344344+ if (!error)345345+ dev_info(dev, "System wakeup %s by ACPI\n",346346+ enable ? "enabled" : "disabled");347347+348348+ return error;349349+}350350+#endif /* CONFIG_PM_SLEEP */351351+352352+/**353353+ * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.354354+ * @dev: Device to get the ACPI node for.355355+ */356356+static struct acpi_device *acpi_dev_pm_get_node(struct device *dev)357357+{358358+ acpi_handle handle = DEVICE_ACPI_HANDLE(dev);359359+ struct acpi_device *adev;360360+361361+ return handle && ACPI_SUCCESS(acpi_bus_get_device(handle, &adev)) ?362362+ adev : NULL;363363+}364364+365365+/**366366+ * acpi_dev_pm_low_power - Put ACPI device into a low-power state.367367+ * @dev: Device to put into a low-power state.368368+ * @adev: ACPI device node corresponding to @dev.369369+ * @system_state: System state to choose the device state for.370370+ */371371+static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,372372+ u32 system_state)373373+{374374+ int power_state;375375+376376+ if (!acpi_device_power_manageable(adev))377377+ return 0;378378+379379+ power_state = acpi_device_power_state(dev, adev, system_state,380380+ ACPI_STATE_D3, NULL);381381+ if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)382382+ return -EIO;383383+384384+ return acpi_device_set_power(adev, power_state);385385+}386386+387387+/**388388+ * acpi_dev_pm_full_power - Put ACPI device into the full-power state.389389+ * @adev: ACPI device node to put into the full-power state.390390+ */391391+static int acpi_dev_pm_full_power(struct acpi_device *adev)392392+{393393+ return acpi_device_power_manageable(adev) ?394394+ acpi_device_set_power(adev, ACPI_STATE_D0) : 0;395395+}396396+397397+#ifdef CONFIG_PM_RUNTIME398398+/**399399+ * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.400400+ * @dev: Device to put into a low-power state.401401+ *402402+ * Put the given device into a runtime low-power state using the standard ACPI403403+ * mechanism. Set up remote wakeup if desired, choose the state to put the404404+ * device into (this checks if remote wakeup is expected to work too), and set405405+ * the power state of the device.406406+ */407407+int acpi_dev_runtime_suspend(struct device *dev)408408+{409409+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);410410+ bool remote_wakeup;411411+ int error;412412+413413+ if (!adev)414414+ return 0;415415+416416+ remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >417417+ PM_QOS_FLAGS_NONE;418418+ error = __acpi_device_run_wake(adev, remote_wakeup);419419+ if (remote_wakeup && error)420420+ return -EAGAIN;421421+422422+ error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);423423+ if (error)424424+ __acpi_device_run_wake(adev, false);425425+426426+ return error;427427+}428428+EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);429429+430430+/**431431+ * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.432432+ * @dev: Device to put into the full-power state.433433+ *434434+ * Put the given device into the full-power state using the standard ACPI435435+ * mechanism at run time. Set the power state of the device to ACPI D0 and436436+ * disable remote wakeup.437437+ */438438+int acpi_dev_runtime_resume(struct device *dev)439439+{440440+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);441441+ int error;442442+443443+ if (!adev)444444+ return 0;445445+446446+ error = acpi_dev_pm_full_power(adev);447447+ __acpi_device_run_wake(adev, false);448448+ return error;449449+}450450+EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);451451+452452+/**453453+ * acpi_subsys_runtime_suspend - Suspend device using ACPI.454454+ * @dev: Device to suspend.455455+ *456456+ * Carry out the generic runtime suspend procedure for @dev and use ACPI to put457457+ * it into a runtime low-power state.458458+ */459459+int acpi_subsys_runtime_suspend(struct device *dev)460460+{461461+ int ret = pm_generic_runtime_suspend(dev);462462+ return ret ? ret : acpi_dev_runtime_suspend(dev);463463+}464464+EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);465465+466466+/**467467+ * acpi_subsys_runtime_resume - Resume device using ACPI.468468+ * @dev: Device to Resume.469469+ *470470+ * Use ACPI to put the given device into the full-power state and carry out the471471+ * generic runtime resume procedure for it.472472+ */473473+int acpi_subsys_runtime_resume(struct device *dev)474474+{475475+ int ret = acpi_dev_runtime_resume(dev);476476+ return ret ? ret : pm_generic_runtime_resume(dev);477477+}478478+EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);479479+#endif /* CONFIG_PM_RUNTIME */480480+481481+#ifdef CONFIG_PM_SLEEP482482+/**483483+ * acpi_dev_suspend_late - Put device into a low-power state using ACPI.484484+ * @dev: Device to put into a low-power state.485485+ *486486+ * Put the given device into a low-power state during system transition to a487487+ * sleep state using the standard ACPI mechanism. Set up system wakeup if488488+ * desired, choose the state to put the device into (this checks if system489489+ * wakeup is expected to work too), and set the power state of the device.490490+ */491491+int acpi_dev_suspend_late(struct device *dev)492492+{493493+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);494494+ u32 target_state;495495+ bool wakeup;496496+ int error;497497+498498+ if (!adev)499499+ return 0;500500+501501+ target_state = acpi_target_system_state();502502+ wakeup = device_may_wakeup(dev);503503+ error = __acpi_device_sleep_wake(adev, target_state, wakeup);504504+ if (wakeup && error)505505+ return error;506506+507507+ error = acpi_dev_pm_low_power(dev, adev, target_state);508508+ if (error)509509+ __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);510510+511511+ return error;512512+}513513+EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);514514+515515+/**516516+ * acpi_dev_resume_early - Put device into the full-power state using ACPI.517517+ * @dev: Device to put into the full-power state.518518+ *519519+ * Put the given device into the full-power state using the standard ACPI520520+ * mechanism during system transition to the working state. Set the power521521+ * state of the device to ACPI D0 and disable remote wakeup.522522+ */523523+int acpi_dev_resume_early(struct device *dev)524524+{525525+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);526526+ int error;527527+528528+ if (!adev)529529+ return 0;530530+531531+ error = acpi_dev_pm_full_power(adev);532532+ __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);533533+ return error;534534+}535535+EXPORT_SYMBOL_GPL(acpi_dev_resume_early);536536+537537+/**538538+ * acpi_subsys_prepare - Prepare device for system transition to a sleep state.539539+ * @dev: Device to prepare.540540+ */541541+int acpi_subsys_prepare(struct device *dev)542542+{543543+ /*544544+ * Follow PCI and resume devices suspended at run time before running545545+ * their system suspend callbacks.546546+ */547547+ pm_runtime_resume(dev);548548+ return pm_generic_prepare(dev);549549+}550550+EXPORT_SYMBOL_GPL(acpi_subsys_prepare);551551+552552+/**553553+ * acpi_subsys_suspend_late - Suspend device using ACPI.554554+ * @dev: Device to suspend.555555+ *556556+ * Carry out the generic late suspend procedure for @dev and use ACPI to put557557+ * it into a low-power state during system transition into a sleep state.558558+ */559559+int acpi_subsys_suspend_late(struct device *dev)560560+{561561+ int ret = pm_generic_suspend_late(dev);562562+ return ret ? ret : acpi_dev_suspend_late(dev);563563+}564564+EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);565565+566566+/**567567+ * acpi_subsys_resume_early - Resume device using ACPI.568568+ * @dev: Device to Resume.569569+ *570570+ * Use ACPI to put the given device into the full-power state and carry out the571571+ * generic early resume procedure for it during system transition into the572572+ * working state.573573+ */574574+int acpi_subsys_resume_early(struct device *dev)575575+{576576+ int ret = acpi_dev_resume_early(dev);577577+ return ret ? ret : pm_generic_resume_early(dev);578578+}579579+EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);580580+#endif /* CONFIG_PM_SLEEP */581581+582582+static struct dev_pm_domain acpi_general_pm_domain = {583583+ .ops = {584584+#ifdef CONFIG_PM_RUNTIME585585+ .runtime_suspend = acpi_subsys_runtime_suspend,586586+ .runtime_resume = acpi_subsys_runtime_resume,587587+ .runtime_idle = pm_generic_runtime_idle,588588+#endif589589+#ifdef CONFIG_PM_SLEEP590590+ .prepare = acpi_subsys_prepare,591591+ .suspend_late = acpi_subsys_suspend_late,592592+ .resume_early = acpi_subsys_resume_early,593593+ .poweroff_late = acpi_subsys_suspend_late,594594+ .restore_early = acpi_subsys_resume_early,595595+#endif596596+ },597597+};598598+599599+/**600600+ * acpi_dev_pm_attach - Prepare device for ACPI power management.601601+ * @dev: Device to prepare.602602+ * @power_on: Whether or not to power on the device.603603+ *604604+ * If @dev has a valid ACPI handle that has a valid struct acpi_device object605605+ * attached to it, install a wakeup notification handler for the device and606606+ * add it to the general ACPI PM domain. If @power_on is set, the device will607607+ * be put into the ACPI D0 state before the function returns.608608+ *609609+ * This assumes that the @dev's bus type uses generic power management callbacks610610+ * (or doesn't use any power management callbacks at all).611611+ *612612+ * Callers must ensure proper synchronization of this function with power613613+ * management callbacks.614614+ */615615+int acpi_dev_pm_attach(struct device *dev, bool power_on)616616+{617617+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);618618+619619+ if (!adev)620620+ return -ENODEV;621621+622622+ if (dev->pm_domain)623623+ return -EEXIST;624624+625625+ acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);626626+ dev->pm_domain = &acpi_general_pm_domain;627627+ if (power_on) {628628+ acpi_dev_pm_full_power(adev);629629+ __acpi_device_run_wake(adev, false);630630+ }631631+ return 0;632632+}633633+EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);634634+635635+/**636636+ * acpi_dev_pm_detach - Remove ACPI power management from the device.637637+ * @dev: Device to take care of.638638+ * @power_off: Whether or not to try to remove power from the device.639639+ *640640+ * Remove the device from the general ACPI PM domain and remove its wakeup641641+ * notifier. If @power_off is set, additionally remove power from the device if642642+ * possible.643643+ *644644+ * Callers must ensure proper synchronization of this function with power645645+ * management callbacks.646646+ */647647+void acpi_dev_pm_detach(struct device *dev, bool power_off)648648+{649649+ struct acpi_device *adev = acpi_dev_pm_get_node(dev);650650+651651+ if (adev && dev->pm_domain == &acpi_general_pm_domain) {652652+ dev->pm_domain = NULL;653653+ acpi_remove_pm_notifier(adev, acpi_wakeup_device);654654+ if (power_off) {655655+ /*656656+ * If the device's PM QoS resume latency limit or flags657657+ * have been exposed to user space, they have to be658658+ * hidden at this point, so that they don't affect the659659+ * choice of the low-power state to put the device into.660660+ */661661+ dev_pm_qos_hide_latency_limit(dev);662662+ dev_pm_qos_hide_flags(dev);663663+ __acpi_device_run_wake(adev, false);664664+ acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);665665+ }666666+ }667667+}668668+EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
+8-1
drivers/acpi/scan.c
···10061006 * D3hot is only valid if _PR3 present.10071007 */10081008 if (ps->resources.count ||10091009- (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))10091009+ (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {10101010 ps->flags.valid = 1;10111011+ ps->flags.os_accessible = 1;10121012+ }1011101310121014 ps->power = -1; /* Unknown - driver assigned */10131015 ps->latency = -1; /* Unknown - driver assigned */···10241022 /* Set D3cold's explicit_set flag if _PS3 exists. */10251023 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)10261024 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;10251025+10261026+ /* Presence of _PS3 or _PRx means we can put the device into D3 cold */10271027+ if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||10281028+ device->power.flags.power_resources)10291029+ device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;1027103010281031 acpi_bus_init_power(device);10291032
+6-172
drivers/acpi/sleep.c
···1818#include <linux/reboot.h>1919#include <linux/acpi.h>2020#include <linux/module.h>2121-#include <linux/pm_runtime.h>22212322#include <asm/io.h>2423···80818182#ifdef CONFIG_ACPI_SLEEP8283static u32 acpi_target_sleep_state = ACPI_STATE_S0;8484+8585+u32 acpi_target_system_state(void)8686+{8787+ return acpi_target_sleep_state;8888+}8989+8390static bool pwr_btn_event_pending;84918592/*···685680 return hibernate();686681 return -EINVAL;687682}688688-689689-#ifdef CONFIG_PM690690-/**691691- * acpi_pm_device_sleep_state - return preferred power state of ACPI device692692- * in the system sleep state given by %acpi_target_sleep_state693693- * @dev: device to examine; its driver model wakeup flags control694694- * whether it should be able to wake up the system695695- * @d_min_p: used to store the upper limit of allowed states range696696- * @d_max_in: specify the lowest allowed states697697- * Return value: preferred power state of the device on success, -ENODEV698698- * (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure699699- *700700- * Find the lowest power (highest number) ACPI device power state that701701- * device @dev can be in while the system is in the sleep state represented702702- * by %acpi_target_sleep_state. If @wake is nonzero, the device should be703703- * able to wake up the system from this sleep state. If @d_min_p is set,704704- * the highest power (lowest number) device power state of @dev allowed705705- * in this system sleep state is stored at the location pointed to by it.706706- *707707- * The caller must ensure that @dev is valid before using this function.708708- * The caller is also responsible for figuring out if the device is709709- * supposed to be able to wake up the system and passing this information710710- * via @wake.711711- */712712-713713-int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)714714-{715715- acpi_handle handle = DEVICE_ACPI_HANDLE(dev);716716- struct acpi_device *adev;717717- char acpi_method[] = "_SxD";718718- unsigned long long d_min, d_max;719719-720720- if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)721721- return -EINVAL;722722- if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {723723- printk(KERN_DEBUG "ACPI handle has no context!\n");724724- return -ENODEV;725725- }726726-727727- acpi_method[2] = '0' + acpi_target_sleep_state;728728- /*729729- * If the sleep state is S0, the lowest limit from ACPI is D3,730730- * but if the device has _S0W, we will use the value from _S0W731731- * as the lowest limit from ACPI. Finally, we will constrain732732- * the lowest limit with the specified one.733733- */734734- d_min = ACPI_STATE_D0;735735- d_max = ACPI_STATE_D3;736736-737737- /*738738- * If present, _SxD methods return the minimum D-state (highest power739739- * state) we can use for the corresponding S-states. Otherwise, the740740- * minimum D-state is D0 (ACPI 3.x).741741- *742742- * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer743743- * provided -- that's our fault recovery, we ignore retval.744744- */745745- if (acpi_target_sleep_state > ACPI_STATE_S0)746746- acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);747747-748748- /*749749- * If _PRW says we can wake up the system from the target sleep state,750750- * the D-state returned by _SxD is sufficient for that (we assume a751751- * wakeup-aware driver if wake is set). Still, if _SxW exists752752- * (ACPI 3.x), it should return the maximum (lowest power) D-state that753753- * can wake the system. _S0W may be valid, too.754754- */755755- if (acpi_target_sleep_state == ACPI_STATE_S0 ||756756- (device_may_wakeup(dev) && adev->wakeup.flags.valid &&757757- adev->wakeup.sleep_state >= acpi_target_sleep_state)) {758758- acpi_status status;759759-760760- acpi_method[3] = 'W';761761- status = acpi_evaluate_integer(handle, acpi_method, NULL,762762- &d_max);763763- if (ACPI_FAILURE(status)) {764764- if (acpi_target_sleep_state != ACPI_STATE_S0 ||765765- status != AE_NOT_FOUND)766766- d_max = d_min;767767- } else if (d_max < d_min) {768768- /* Warn the user of the broken DSDT */769769- printk(KERN_WARNING "ACPI: Wrong value from %s\n",770770- acpi_method);771771- /* Sanitize it */772772- d_min = d_max;773773- }774774- }775775-776776- if (d_max_in < d_min)777777- return -EINVAL;778778- if (d_min_p)779779- *d_min_p = d_min;780780- /* constrain d_max with specified lowest limit (max number) */781781- if (d_max > d_max_in) {782782- for (d_max = d_max_in; d_max > d_min; d_max--) {783783- if (adev->power.states[d_max].flags.valid)784784- break;785785- }786786- }787787- return d_max;788788-}789789-EXPORT_SYMBOL(acpi_pm_device_sleep_state);790790-#endif /* CONFIG_PM */791791-792792-#ifdef CONFIG_PM_SLEEP793793-/**794794- * acpi_pm_device_run_wake - Enable/disable wake-up for given device.795795- * @phys_dev: Device to enable/disable the platform to wake-up the system for.796796- * @enable: Whether enable or disable the wake-up functionality.797797- *798798- * Find the ACPI device object corresponding to @pci_dev and try to799799- * enable/disable the GPE associated with it.800800- */801801-int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)802802-{803803- struct acpi_device *dev;804804- acpi_handle handle;805805-806806- if (!device_run_wake(phys_dev))807807- return -EINVAL;808808-809809- handle = DEVICE_ACPI_HANDLE(phys_dev);810810- if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {811811- dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",812812- __func__);813813- return -ENODEV;814814- }815815-816816- if (enable) {817817- acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);818818- acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);819819- } else {820820- acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);821821- acpi_disable_wakeup_device_power(dev);822822- }823823-824824- return 0;825825-}826826-EXPORT_SYMBOL(acpi_pm_device_run_wake);827827-828828-/**829829- * acpi_pm_device_sleep_wake - enable or disable the system wake-up830830- * capability of given device831831- * @dev: device to handle832832- * @enable: 'true' - enable, 'false' - disable the wake-up capability833833- */834834-int acpi_pm_device_sleep_wake(struct device *dev, bool enable)835835-{836836- acpi_handle handle;837837- struct acpi_device *adev;838838- int error;839839-840840- if (!device_can_wakeup(dev))841841- return -EINVAL;842842-843843- handle = DEVICE_ACPI_HANDLE(dev);844844- if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {845845- dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);846846- return -ENODEV;847847- }848848-849849- error = enable ?850850- acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :851851- acpi_disable_wakeup_device_power(adev);852852- if (!error)853853- dev_info(dev, "wake-up capability %s by ACPI\n",854854- enable ? "enabled" : "disabled");855855-856856- return error;857857-}858858-#endif /* CONFIG_PM_SLEEP */859683860684static void acpi_power_off_prepare(void)861685{
···9393extern void rpm_sysfs_remove(struct device *dev);9494extern int wakeup_sysfs_add(struct device *dev);9595extern void wakeup_sysfs_remove(struct device *dev);9696-extern int pm_qos_sysfs_add(struct device *dev);9797-extern void pm_qos_sysfs_remove(struct device *dev);9696+extern int pm_qos_sysfs_add_latency(struct device *dev);9797+extern void pm_qos_sysfs_remove_latency(struct device *dev);9898+extern int pm_qos_sysfs_add_flags(struct device *dev);9999+extern void pm_qos_sysfs_remove_flags(struct device *dev);9810099101#else /* CONFIG_PM */100102
+246-62
drivers/base/power/qos.c
···4040#include <linux/device.h>4141#include <linux/mutex.h>4242#include <linux/export.h>4343+#include <linux/pm_runtime.h>43444445#include "power.h"45464647static DEFINE_MUTEX(dev_pm_qos_mtx);47484849static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);5050+5151+/**5252+ * __dev_pm_qos_flags - Check PM QoS flags for a given device.5353+ * @dev: Device to check the PM QoS flags for.5454+ * @mask: Flags to check against.5555+ *5656+ * This routine must be called with dev->power.lock held.5757+ */5858+enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)5959+{6060+ struct dev_pm_qos *qos = dev->power.qos;6161+ struct pm_qos_flags *pqf;6262+ s32 val;6363+6464+ if (!qos)6565+ return PM_QOS_FLAGS_UNDEFINED;6666+6767+ pqf = &qos->flags;6868+ if (list_empty(&pqf->list))6969+ return PM_QOS_FLAGS_UNDEFINED;7070+7171+ val = pqf->effective_flags & mask;7272+ if (val)7373+ return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;7474+7575+ return PM_QOS_FLAGS_NONE;7676+}7777+7878+/**7979+ * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).8080+ * @dev: Device to check the PM QoS flags for.8181+ * @mask: Flags to check against.8282+ */8383+enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)8484+{8585+ unsigned long irqflags;8686+ enum pm_qos_flags_status ret;8787+8888+ spin_lock_irqsave(&dev->power.lock, irqflags);8989+ ret = __dev_pm_qos_flags(dev, mask);9090+ spin_unlock_irqrestore(&dev->power.lock, irqflags);9191+9292+ return ret;9393+}49945095/**5196 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.···10055 */10156s32 __dev_pm_qos_read_value(struct device *dev)10257{103103- struct pm_qos_constraints *c = dev->power.constraints;104104-105105- return c ? pm_qos_read_value(c) : 0;5858+ return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;10659}1076010861/**···11976 return ret;12077}12178122122-/*123123- * apply_constraint124124- * @req: constraint request to apply125125- * @action: action to perform add/update/remove, of type enum pm_qos_req_action126126- * @value: defines the qos request7979+/**8080+ * apply_constraint - Add/modify/remove device PM QoS request.8181+ * @req: Constraint request to apply8282+ * @action: Action to perform (add/update/remove).8383+ * @value: Value to assign to the QoS request.12784 *12885 * Internal function to update the constraints list using the PM QoS core12986 * code and if needed call the per-device and the global notification13087 * callbacks13188 */13289static int apply_constraint(struct dev_pm_qos_request *req,133133- enum pm_qos_req_action action, int value)9090+ enum pm_qos_req_action action, s32 value)13491{135135- int ret, curr_value;9292+ struct dev_pm_qos *qos = req->dev->power.qos;9393+ int ret;13694137137- ret = pm_qos_update_target(req->dev->power.constraints,138138- &req->node, action, value);139139-140140- if (ret) {141141- /* Call the global callbacks if needed */142142- curr_value = pm_qos_read_value(req->dev->power.constraints);143143- blocking_notifier_call_chain(&dev_pm_notifiers,144144- (unsigned long)curr_value,145145- req);9595+ switch(req->type) {9696+ case DEV_PM_QOS_LATENCY:9797+ ret = pm_qos_update_target(&qos->latency, &req->data.pnode,9898+ action, value);9999+ if (ret) {100100+ value = pm_qos_read_value(&qos->latency);101101+ blocking_notifier_call_chain(&dev_pm_notifiers,102102+ (unsigned long)value,103103+ req);104104+ }105105+ break;106106+ case DEV_PM_QOS_FLAGS:107107+ ret = pm_qos_update_flags(&qos->flags, &req->data.flr,108108+ action, value);109109+ break;110110+ default:111111+ ret = -EINVAL;146112 }147113148114 return ret;···166114 */167115static int dev_pm_qos_constraints_allocate(struct device *dev)168116{117117+ struct dev_pm_qos *qos;169118 struct pm_qos_constraints *c;170119 struct blocking_notifier_head *n;171120172172- c = kzalloc(sizeof(*c), GFP_KERNEL);173173- if (!c)121121+ qos = kzalloc(sizeof(*qos), GFP_KERNEL);122122+ if (!qos)174123 return -ENOMEM;175124176125 n = kzalloc(sizeof(*n), GFP_KERNEL);177126 if (!n) {178178- kfree(c);127127+ kfree(qos);179128 return -ENOMEM;180129 }181130 BLOCKING_INIT_NOTIFIER_HEAD(n);182131132132+ c = &qos->latency;183133 plist_head_init(&c->list);184134 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;185135 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;186136 c->type = PM_QOS_MIN;187137 c->notifiers = n;188138139139+ INIT_LIST_HEAD(&qos->flags.list);140140+189141 spin_lock_irq(&dev->power.lock);190190- dev->power.constraints = c;142142+ dev->power.qos = qos;191143 spin_unlock_irq(&dev->power.lock);192144193145 return 0;···207151void dev_pm_qos_constraints_init(struct device *dev)208152{209153 mutex_lock(&dev_pm_qos_mtx);210210- dev->power.constraints = NULL;154154+ dev->power.qos = NULL;211155 dev->power.power_state = PMSG_ON;212156 mutex_unlock(&dev_pm_qos_mtx);213157}···220164 */221165void dev_pm_qos_constraints_destroy(struct device *dev)222166{167167+ struct dev_pm_qos *qos;223168 struct dev_pm_qos_request *req, *tmp;224169 struct pm_qos_constraints *c;225170···233176 mutex_lock(&dev_pm_qos_mtx);234177235178 dev->power.power_state = PMSG_INVALID;236236- c = dev->power.constraints;237237- if (!c)179179+ qos = dev->power.qos;180180+ if (!qos)238181 goto out;239182183183+ c = &qos->latency;240184 /* Flush the constraints list for the device */241241- plist_for_each_entry_safe(req, tmp, &c->list, node) {185185+ plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {242186 /*243187 * Update constraints list and call the notification244188 * callbacks if needed···249191 }250192251193 spin_lock_irq(&dev->power.lock);252252- dev->power.constraints = NULL;194194+ dev->power.qos = NULL;253195 spin_unlock_irq(&dev->power.lock);254196255197 kfree(c->notifiers);256256- kfree(c);198198+ kfree(qos);257199258200 out:259201 mutex_unlock(&dev_pm_qos_mtx);···263205 * dev_pm_qos_add_request - inserts new qos request into the list264206 * @dev: target device for the constraint265207 * @req: pointer to a preallocated handle208208+ * @type: type of the request266209 * @value: defines the qos request267210 *268211 * This function inserts a new entry in the device constraints list of···277218 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory278219 * to allocate for data structures, -ENODEV if the device has just been removed279220 * from the system.221221+ *222222+ * Callers should ensure that the target device is not RPM_SUSPENDED before223223+ * using this function for requests of type DEV_PM_QOS_FLAGS.280224 */281225int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,282282- s32 value)226226+ enum dev_pm_qos_req_type type, s32 value)283227{284228 int ret = 0;285229···297235298236 mutex_lock(&dev_pm_qos_mtx);299237300300- if (!dev->power.constraints) {238238+ if (!dev->power.qos) {301239 if (dev->power.power_state.event == PM_EVENT_INVALID) {302240 /* The device has been removed from the system. */303241 req->dev = NULL;···313251 }314252 }315253316316- if (!ret)254254+ if (!ret) {255255+ req->type = type;317256 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);257257+ }318258319259 out:320260 mutex_unlock(&dev_pm_qos_mtx);···324260 return ret;325261}326262EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);263263+264264+/**265265+ * __dev_pm_qos_update_request - Modify an existing device PM QoS request.266266+ * @req : PM QoS request to modify.267267+ * @new_value: New value to request.268268+ */269269+static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,270270+ s32 new_value)271271+{272272+ s32 curr_value;273273+ int ret = 0;274274+275275+ if (!req->dev->power.qos)276276+ return -ENODEV;277277+278278+ switch(req->type) {279279+ case DEV_PM_QOS_LATENCY:280280+ curr_value = req->data.pnode.prio;281281+ break;282282+ case DEV_PM_QOS_FLAGS:283283+ curr_value = req->data.flr.flags;284284+ break;285285+ default:286286+ return -EINVAL;287287+ }288288+289289+ if (curr_value != new_value)290290+ ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);291291+292292+ return ret;293293+}327294328295/**329296 * dev_pm_qos_update_request - modifies an existing qos request···370275 * 0 if the aggregated constraint value has not changed,371276 * -EINVAL in case of wrong parameters, -ENODEV if the device has been372277 * removed from the system278278+ *279279+ * Callers should ensure that the target device is not RPM_SUSPENDED before280280+ * using this function for requests of type DEV_PM_QOS_FLAGS.373281 */374374-int dev_pm_qos_update_request(struct dev_pm_qos_request *req,375375- s32 new_value)282282+int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)376283{377377- int ret = 0;284284+ int ret;378285379286 if (!req) /*guard against callers passing in null */380287 return -EINVAL;···386289 return -EINVAL;387290388291 mutex_lock(&dev_pm_qos_mtx);389389-390390- if (req->dev->power.constraints) {391391- if (new_value != req->node.prio)392392- ret = apply_constraint(req, PM_QOS_UPDATE_REQ,393393- new_value);394394- } else {395395- /* Return if the device has been removed */396396- ret = -ENODEV;397397- }398398-292292+ ret = __dev_pm_qos_update_request(req, new_value);399293 mutex_unlock(&dev_pm_qos_mtx);294294+400295 return ret;401296}402297EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);···404315 * 0 if the aggregated constraint value has not changed,405316 * -EINVAL in case of wrong parameters, -ENODEV if the device has been406317 * removed from the system318318+ *319319+ * Callers should ensure that the target device is not RPM_SUSPENDED before320320+ * using this function for requests of type DEV_PM_QOS_FLAGS.407321 */408322int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)409323{···421329422330 mutex_lock(&dev_pm_qos_mtx);423331424424- if (req->dev->power.constraints) {332332+ if (req->dev->power.qos) {425333 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,426334 PM_QOS_DEFAULT_VALUE);427335 memset(req, 0, sizeof(*req));···454362455363 mutex_lock(&dev_pm_qos_mtx);456364457457- if (!dev->power.constraints)365365+ if (!dev->power.qos)458366 ret = dev->power.power_state.event != PM_EVENT_INVALID ?459367 dev_pm_qos_constraints_allocate(dev) : -ENODEV;460368461369 if (!ret)462370 ret = blocking_notifier_chain_register(463463- dev->power.constraints->notifiers, notifier);371371+ dev->power.qos->latency.notifiers, notifier);464372465373 mutex_unlock(&dev_pm_qos_mtx);466374 return ret;···485393 mutex_lock(&dev_pm_qos_mtx);486394487395 /* Silently return if the constraints object is not present. */488488- if (dev->power.constraints)396396+ if (dev->power.qos)489397 retval = blocking_notifier_chain_unregister(490490- dev->power.constraints->notifiers,398398+ dev->power.qos->latency.notifiers,491399 notifier);492400493401 mutex_unlock(&dev_pm_qos_mtx);···541449 ancestor = ancestor->parent;542450543451 if (ancestor)544544- error = dev_pm_qos_add_request(ancestor, req, value);452452+ error = dev_pm_qos_add_request(ancestor, req,453453+ DEV_PM_QOS_LATENCY, value);545454546455 if (error)547456 req->dev = NULL;···552459EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);553460554461#ifdef CONFIG_PM_RUNTIME555555-static void __dev_pm_qos_drop_user_request(struct device *dev)462462+static void __dev_pm_qos_drop_user_request(struct device *dev,463463+ enum dev_pm_qos_req_type type)556464{557557- dev_pm_qos_remove_request(dev->power.pq_req);558558- dev->power.pq_req = NULL;465465+ switch(type) {466466+ case DEV_PM_QOS_LATENCY:467467+ dev_pm_qos_remove_request(dev->power.qos->latency_req);468468+ dev->power.qos->latency_req = NULL;469469+ break;470470+ case DEV_PM_QOS_FLAGS:471471+ dev_pm_qos_remove_request(dev->power.qos->flags_req);472472+ dev->power.qos->flags_req = NULL;473473+ break;474474+ }559475}560476561477/**···580478 if (!device_is_registered(dev) || value < 0)581479 return -EINVAL;582480583583- if (dev->power.pq_req)481481+ if (dev->power.qos && dev->power.qos->latency_req)584482 return -EEXIST;585483586484 req = kzalloc(sizeof(*req), GFP_KERNEL);587485 if (!req)588486 return -ENOMEM;589487590590- ret = dev_pm_qos_add_request(dev, req, value);488488+ ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);591489 if (ret < 0)592490 return ret;593491594594- dev->power.pq_req = req;595595- ret = pm_qos_sysfs_add(dev);492492+ dev->power.qos->latency_req = req;493493+ ret = pm_qos_sysfs_add_latency(dev);596494 if (ret)597597- __dev_pm_qos_drop_user_request(dev);495495+ __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);598496599497 return ret;600498}···606504 */607505void dev_pm_qos_hide_latency_limit(struct device *dev)608506{609609- if (dev->power.pq_req) {610610- pm_qos_sysfs_remove(dev);611611- __dev_pm_qos_drop_user_request(dev);507507+ if (dev->power.qos && dev->power.qos->latency_req) {508508+ pm_qos_sysfs_remove_latency(dev);509509+ __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);612510 }613511}614512EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);513513+514514+/**515515+ * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.516516+ * @dev: Device whose PM QoS flags are to be exposed to user space.517517+ * @val: Initial values of the flags.518518+ */519519+int dev_pm_qos_expose_flags(struct device *dev, s32 val)520520+{521521+ struct dev_pm_qos_request *req;522522+ int ret;523523+524524+ if (!device_is_registered(dev))525525+ return -EINVAL;526526+527527+ if (dev->power.qos && dev->power.qos->flags_req)528528+ return -EEXIST;529529+530530+ req = kzalloc(sizeof(*req), GFP_KERNEL);531531+ if (!req)532532+ return -ENOMEM;533533+534534+ pm_runtime_get_sync(dev);535535+ ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);536536+ if (ret < 0)537537+ goto fail;538538+539539+ dev->power.qos->flags_req = req;540540+ ret = pm_qos_sysfs_add_flags(dev);541541+ if (ret)542542+ __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);543543+544544+fail:545545+ pm_runtime_put(dev);546546+ return ret;547547+}548548+EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);549549+550550+/**551551+ * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.552552+ * @dev: Device whose PM QoS flags are to be hidden from user space.553553+ */554554+void dev_pm_qos_hide_flags(struct device *dev)555555+{556556+ if (dev->power.qos && dev->power.qos->flags_req) {557557+ pm_qos_sysfs_remove_flags(dev);558558+ pm_runtime_get_sync(dev);559559+ __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);560560+ pm_runtime_put(dev);561561+ }562562+}563563+EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);564564+565565+/**566566+ * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.567567+ * @dev: Device to update the PM QoS flags request for.568568+ * @mask: Flags to set/clear.569569+ * @set: Whether to set or clear the flags (true means set).570570+ */571571+int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)572572+{573573+ s32 value;574574+ int ret;575575+576576+ if (!dev->power.qos || !dev->power.qos->flags_req)577577+ return -EINVAL;578578+579579+ pm_runtime_get_sync(dev);580580+ mutex_lock(&dev_pm_qos_mtx);581581+582582+ value = dev_pm_qos_requested_flags(dev);583583+ if (set)584584+ value |= mask;585585+ else586586+ value &= ~mask;587587+588588+ ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);589589+590590+ mutex_unlock(&dev_pm_qos_mtx);591591+ pm_runtime_put(dev);592592+593593+ return ret;594594+}615595#endif /* CONFIG_PM_RUNTIME */
···727727728728 if (!flctl->qos_request) {729729 ret = dev_pm_qos_add_request(&flctl->pdev->dev,730730- &flctl->pm_qos, 100);730730+ &flctl->pm_qos,731731+ DEV_PM_QOS_LATENCY,732732+ 100);731733 if (ret < 0)732734 dev_err(&flctl->pdev->dev,733735 "PM QoS request failed: %d\n", ret);
+11-68
drivers/pci/pci-acpi.c
···17171818#include <linux/pci-acpi.h>1919#include <linux/pm_runtime.h>2020+#include <linux/pm_qos.h>2021#include "pci.h"2121-2222-static DEFINE_MUTEX(pci_acpi_pm_notify_mtx);23222423/**2524 * pci_acpi_wake_bus - Wake-up notification handler for root buses.···6768}68696970/**7070- * add_pm_notifier - Register PM notifier for given ACPI device.7171- * @dev: ACPI device to add the notifier for.7272- * @context: PCI device or bus to check for PME status if an event is signaled.7373- *7474- * NOTE: @dev need not be a run-wake or wake-up device to be a valid source of7575- * PM wake-up events. For example, wake-up events may be generated for bridges7676- * if one of the devices below the bridge is signaling PME, even if the bridge7777- * itself doesn't have a wake-up GPE associated with it.7878- */7979-static acpi_status add_pm_notifier(struct acpi_device *dev,8080- acpi_notify_handler handler,8181- void *context)8282-{8383- acpi_status status = AE_ALREADY_EXISTS;8484-8585- mutex_lock(&pci_acpi_pm_notify_mtx);8686-8787- if (dev->wakeup.flags.notifier_present)8888- goto out;8989-9090- status = acpi_install_notify_handler(dev->handle,9191- ACPI_SYSTEM_NOTIFY,9292- handler, context);9393- if (ACPI_FAILURE(status))9494- goto out;9595-9696- dev->wakeup.flags.notifier_present = true;9797-9898- out:9999- mutex_unlock(&pci_acpi_pm_notify_mtx);100100- return status;101101-}102102-103103-/**104104- * remove_pm_notifier - Unregister PM notifier from given ACPI device.105105- * @dev: ACPI device to remove the notifier from.106106- */107107-static acpi_status remove_pm_notifier(struct acpi_device *dev,108108- acpi_notify_handler handler)109109-{110110- acpi_status status = AE_BAD_PARAMETER;111111-112112- mutex_lock(&pci_acpi_pm_notify_mtx);113113-114114- if (!dev->wakeup.flags.notifier_present)115115- goto out;116116-117117- status = acpi_remove_notify_handler(dev->handle,118118- ACPI_SYSTEM_NOTIFY,119119- handler);120120- if (ACPI_FAILURE(status))121121- goto out;122122-123123- dev->wakeup.flags.notifier_present = false;124124-125125- out:126126- mutex_unlock(&pci_acpi_pm_notify_mtx);127127- return status;128128-}129129-130130-/**13171 * pci_acpi_add_bus_pm_notifier - Register PM notifier for given PCI bus.13272 * @dev: ACPI device to add the notifier for.13373 * @pci_bus: PCI bus to walk checking for PME status if an event is signaled.···74136acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,75137 struct pci_bus *pci_bus)76138{7777- return add_pm_notifier(dev, pci_acpi_wake_bus, pci_bus);139139+ return acpi_add_pm_notifier(dev, pci_acpi_wake_bus, pci_bus);78140}7914180142/**···83145 */84146acpi_status pci_acpi_remove_bus_pm_notifier(struct acpi_device *dev)85147{8686- return remove_pm_notifier(dev, pci_acpi_wake_bus);148148+ return acpi_remove_pm_notifier(dev, pci_acpi_wake_bus);87149}8815089151/**···94156acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,95157 struct pci_dev *pci_dev)96158{9797- return add_pm_notifier(dev, pci_acpi_wake_dev, pci_dev);159159+ return acpi_add_pm_notifier(dev, pci_acpi_wake_dev, pci_dev);98160}99161100162/**···103165 */104166acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)105167{106106- return remove_pm_notifier(dev, pci_acpi_wake_dev);168168+ return acpi_remove_pm_notifier(dev, pci_acpi_wake_dev);107169}108170109171phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)···195257 return -ENODEV;196258197259 switch (state) {260260+ case PCI_D3cold:261261+ if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==262262+ PM_QOS_FLAGS_ALL) {263263+ error = -EBUSY;264264+ break;265265+ }198266 case PCI_D0:199267 case PCI_D1:200268 case PCI_D2:201269 case PCI_D3hot:202202- case PCI_D3cold:203270 error = acpi_bus_set_power(handle, state_conv[state]);204271 }205272
+69-3
include/acpi/acpi_bus.h
···201201struct acpi_device_power_state {202202 struct {203203 u8 valid:1;204204+ u8 os_accessible:1;204205 u8 explicit_set:1; /* _PSx present? */205206 u8 reserved:6;206207 } flags;···340339 unsigned long long *sta);341340int acpi_bus_get_status(struct acpi_device *device);342341int acpi_bus_set_power(acpi_handle handle, int state);342342+int acpi_device_set_power(struct acpi_device *device, int state);343343int acpi_bus_update_power(acpi_handle handle, int *state_p);344344bool acpi_bus_power_manageable(acpi_handle handle);345345bool acpi_bus_can_wakeup(acpi_handle handle);···418416int acpi_disable_wakeup_device_power(struct acpi_device *dev);419417420418#ifdef CONFIG_PM419419+acpi_status acpi_add_pm_notifier(struct acpi_device *adev,420420+ acpi_notify_handler handler, void *context);421421+acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,422422+ acpi_notify_handler handler);423423+int acpi_device_power_state(struct device *dev, struct acpi_device *adev,424424+ u32 target_state, int d_max_in, int *d_min_p);421425int acpi_pm_device_sleep_state(struct device *, int *, int);422426#else423423-static inline int acpi_pm_device_sleep_state(struct device *d, int *p, int m)427427+static inline acpi_status acpi_add_pm_notifier(struct acpi_device *adev,428428+ acpi_notify_handler handler,429429+ void *context)430430+{431431+ return AE_SUPPORT;432432+}433433+static inline acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,434434+ acpi_notify_handler handler)435435+{436436+ return AE_SUPPORT;437437+}438438+static inline int __acpi_device_power_state(int m, int *p)424439{425440 if (p)426441 *p = ACPI_STATE_D0;427442 return (m >= ACPI_STATE_D0 && m <= ACPI_STATE_D3) ? m : ACPI_STATE_D0;428443}444444+static inline int acpi_device_power_state(struct device *dev,445445+ struct acpi_device *adev,446446+ u32 target_state, int d_max_in,447447+ int *d_min_p)448448+{449449+ return __acpi_device_power_state(d_max_in, d_min_p);450450+}451451+static inline int acpi_pm_device_sleep_state(struct device *d, int *p, int m)452452+{453453+ return __acpi_device_power_state(m, p);454454+}455455+#endif456456+457457+#ifdef CONFIG_PM_RUNTIME458458+int __acpi_device_run_wake(struct acpi_device *, bool);459459+int acpi_pm_device_run_wake(struct device *, bool);460460+#else461461+static inline int __acpi_device_run_wake(struct acpi_device *adev, bool en)462462+{463463+ return -ENODEV;464464+}465465+static inline int acpi_pm_device_run_wake(struct device *dev, bool enable)466466+{467467+ return -ENODEV;468468+}429469#endif430470431471#ifdef CONFIG_PM_SLEEP432432-int acpi_pm_device_run_wake(struct device *, bool);472472+int __acpi_device_sleep_wake(struct acpi_device *, u32, bool);433473int acpi_pm_device_sleep_wake(struct device *, bool);434474#else435435-static inline int acpi_pm_device_run_wake(struct device *dev, bool enable)475475+static inline int __acpi_device_sleep_wake(struct acpi_device *adev,476476+ u32 target_state, bool enable)436477{437478 return -ENODEV;438479}···484439 return -ENODEV;485440}486441#endif442442+443443+#ifdef CONFIG_ACPI_SLEEP444444+u32 acpi_target_system_state(void);445445+#else446446+static inline u32 acpi_target_system_state(void) { return ACPI_STATE_S0; }447447+#endif448448+449449+static inline bool acpi_device_power_manageable(struct acpi_device *adev)450450+{451451+ return adev->flags.power_manageable;452452+}453453+454454+static inline bool acpi_device_can_wakeup(struct acpi_device *adev)455455+{456456+ return adev->wakeup.flags.valid;457457+}458458+459459+static inline bool acpi_device_can_poweroff(struct acpi_device *adev)460460+{461461+ return adev->power.states[ACPI_STATE_D3_COLD].flags.os_accessible;462462+}487463488464#else /* CONFIG_ACPI */489465
···213213}214214215215/**216216+ * pm_qos_flags_remove_req - Remove device PM QoS flags request.217217+ * @pqf: Device PM QoS flags set to remove the request from.218218+ * @req: Request to remove from the set.219219+ */220220+static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,221221+ struct pm_qos_flags_request *req)222222+{223223+ s32 val = 0;224224+225225+ list_del(&req->node);226226+ list_for_each_entry(req, &pqf->list, node)227227+ val |= req->flags;228228+229229+ pqf->effective_flags = val;230230+}231231+232232+/**233233+ * pm_qos_update_flags - Update a set of PM QoS flags.234234+ * @pqf: Set of flags to update.235235+ * @req: Request to add to the set, to modify, or to remove from the set.236236+ * @action: Action to take on the set.237237+ * @val: Value of the request to add or modify.238238+ *239239+ * Update the given set of PM QoS flags and call notifiers if the aggregate240240+ * value has changed. Returns 1 if the aggregate constraint value has changed,241241+ * 0 otherwise.242242+ */243243+bool pm_qos_update_flags(struct pm_qos_flags *pqf,244244+ struct pm_qos_flags_request *req,245245+ enum pm_qos_req_action action, s32 val)246246+{247247+ unsigned long irqflags;248248+ s32 prev_value, curr_value;249249+250250+ spin_lock_irqsave(&pm_qos_lock, irqflags);251251+252252+ prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;253253+254254+ switch (action) {255255+ case PM_QOS_REMOVE_REQ:256256+ pm_qos_flags_remove_req(pqf, req);257257+ break;258258+ case PM_QOS_UPDATE_REQ:259259+ pm_qos_flags_remove_req(pqf, req);260260+ case PM_QOS_ADD_REQ:261261+ req->flags = val;262262+ INIT_LIST_HEAD(&req->node);263263+ list_add_tail(&req->node, &pqf->list);264264+ pqf->effective_flags |= val;265265+ break;266266+ default:267267+ /* no action */268268+ ;269269+ }270270+271271+ curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;272272+273273+ spin_unlock_irqrestore(&pm_qos_lock, irqflags);274274+275275+ return prev_value != curr_value;276276+}277277+278278+/**216279 * pm_qos_request - returns current system wide qos expectation217280 * @pm_qos_class: identification of which qos value is requested218281 *