device_pm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * drivers/acpi/device_pm.c - ACPI device power management routines.
  3. *
  4. * Copyright (C) 2012, Intel Corp.
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/acpi.h>
  25. #include <linux/export.h>
  26. #include <linux/mutex.h>
  27. #include <linux/pm_qos.h>
  28. #include <linux/pm_runtime.h>
  29. #include "internal.h"
  30. #define _COMPONENT ACPI_POWER_COMPONENT
  31. ACPI_MODULE_NAME("device_pm");
  32. /**
  33. * acpi_power_state_string - String representation of ACPI device power state.
  34. * @state: ACPI device power state to return the string representation of.
  35. */
  36. const char *acpi_power_state_string(int state)
  37. {
  38. switch (state) {
  39. case ACPI_STATE_D0:
  40. return "D0";
  41. case ACPI_STATE_D1:
  42. return "D1";
  43. case ACPI_STATE_D2:
  44. return "D2";
  45. case ACPI_STATE_D3_HOT:
  46. return "D3hot";
  47. case ACPI_STATE_D3_COLD:
  48. return "D3cold";
  49. default:
  50. return "(unknown)";
  51. }
  52. }
  53. /**
  54. * acpi_device_get_power - Get power state of an ACPI device.
  55. * @device: Device to get the power state of.
  56. * @state: Place to store the power state of the device.
  57. *
  58. * This function does not update the device's power.state field, but it may
  59. * update its parent's power.state field (when the parent's power state is
  60. * unknown and the device's power state turns out to be D0).
  61. */
  62. int acpi_device_get_power(struct acpi_device *device, int *state)
  63. {
  64. int result = ACPI_STATE_UNKNOWN;
  65. if (!device || !state)
  66. return -EINVAL;
  67. if (!device->flags.power_manageable) {
  68. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  69. *state = device->parent ?
  70. device->parent->power.state : ACPI_STATE_D0;
  71. goto out;
  72. }
  73. /*
  74. * Get the device's power state from power resources settings and _PSC,
  75. * if available.
  76. */
  77. if (device->power.flags.power_resources) {
  78. int error = acpi_power_get_inferred_state(device, &result);
  79. if (error)
  80. return error;
  81. }
  82. if (device->power.flags.explicit_get) {
  83. acpi_handle handle = device->handle;
  84. unsigned long long psc;
  85. acpi_status status;
  86. status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
  87. if (ACPI_FAILURE(status))
  88. return -ENODEV;
  89. /*
  90. * The power resources settings may indicate a power state
  91. * shallower than the actual power state of the device, because
  92. * the same power resources may be referenced by other devices.
  93. *
  94. * For systems predating ACPI 4.0 we assume that D3hot is the
  95. * deepest state that can be supported.
  96. */
  97. if (psc > result && psc < ACPI_STATE_D3_COLD)
  98. result = psc;
  99. else if (result == ACPI_STATE_UNKNOWN)
  100. result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
  101. }
  102. /*
  103. * If we were unsure about the device parent's power state up to this
  104. * point, the fact that the device is in D0 implies that the parent has
  105. * to be in D0 too, except if ignore_parent is set.
  106. */
  107. if (!device->power.flags.ignore_parent && device->parent
  108. && device->parent->power.state == ACPI_STATE_UNKNOWN
  109. && result == ACPI_STATE_D0)
  110. device->parent->power.state = ACPI_STATE_D0;
  111. *state = result;
  112. out:
  113. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  114. device->pnp.bus_id, acpi_power_state_string(*state)));
  115. return 0;
  116. }
  117. static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
  118. {
  119. if (adev->power.states[state].flags.explicit_set) {
  120. char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
  121. acpi_status status;
  122. status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
  123. if (ACPI_FAILURE(status))
  124. return -ENODEV;
  125. }
  126. return 0;
  127. }
  128. /**
  129. * acpi_device_set_power - Set power state of an ACPI device.
  130. * @device: Device to set the power state of.
  131. * @state: New power state to set.
  132. *
  133. * Callers must ensure that the device is power manageable before using this
  134. * function.
  135. */
  136. int acpi_device_set_power(struct acpi_device *device, int state)
  137. {
  138. int target_state = state;
  139. int result = 0;
  140. if (!device || !device->flags.power_manageable
  141. || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  142. return -EINVAL;
  143. /* Make sure this is a valid target state */
  144. if (state == device->power.state) {
  145. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
  146. device->pnp.bus_id,
  147. acpi_power_state_string(state)));
  148. return 0;
  149. }
  150. if (state == ACPI_STATE_D3_COLD) {
  151. /*
  152. * For transitions to D3cold we need to execute _PS3 and then
  153. * possibly drop references to the power resources in use.
  154. */
  155. state = ACPI_STATE_D3_HOT;
  156. /* If _PR3 is not available, use D3hot as the target state. */
  157. if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
  158. target_state = state;
  159. } else if (!device->power.states[state].flags.valid) {
  160. dev_warn(&device->dev, "Power state %s not supported\n",
  161. acpi_power_state_string(state));
  162. return -ENODEV;
  163. }
  164. if (!device->power.flags.ignore_parent &&
  165. device->parent && (state < device->parent->power.state)) {
  166. dev_warn(&device->dev,
  167. "Cannot transition to power state %s for parent in %s\n",
  168. acpi_power_state_string(state),
  169. acpi_power_state_string(device->parent->power.state));
  170. return -ENODEV;
  171. }
  172. /*
  173. * Transition Power
  174. * ----------------
  175. * In accordance with ACPI 6, _PSx is executed before manipulating power
  176. * resources, unless the target state is D0, in which case _PS0 is
  177. * supposed to be executed after turning the power resources on.
  178. */
  179. if (state > ACPI_STATE_D0) {
  180. /*
  181. * According to ACPI 6, devices cannot go from lower-power
  182. * (deeper) states to higher-power (shallower) states.
  183. */
  184. if (state < device->power.state) {
  185. dev_warn(&device->dev, "Cannot transition from %s to %s\n",
  186. acpi_power_state_string(device->power.state),
  187. acpi_power_state_string(state));
  188. return -ENODEV;
  189. }
  190. result = acpi_dev_pm_explicit_set(device, state);
  191. if (result)
  192. goto end;
  193. if (device->power.flags.power_resources)
  194. result = acpi_power_transition(device, target_state);
  195. } else {
  196. if (device->power.flags.power_resources) {
  197. result = acpi_power_transition(device, ACPI_STATE_D0);
  198. if (result)
  199. goto end;
  200. }
  201. result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  202. }
  203. end:
  204. if (result) {
  205. dev_warn(&device->dev, "Failed to change power state to %s\n",
  206. acpi_power_state_string(state));
  207. } else {
  208. device->power.state = state;
  209. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  210. "Device [%s] transitioned to %s\n",
  211. device->pnp.bus_id,
  212. acpi_power_state_string(state)));
  213. }
  214. return result;
  215. }
  216. EXPORT_SYMBOL(acpi_device_set_power);
  217. int acpi_bus_set_power(acpi_handle handle, int state)
  218. {
  219. struct acpi_device *device;
  220. int result;
  221. result = acpi_bus_get_device(handle, &device);
  222. if (result)
  223. return result;
  224. return acpi_device_set_power(device, state);
  225. }
  226. EXPORT_SYMBOL(acpi_bus_set_power);
  227. int acpi_bus_init_power(struct acpi_device *device)
  228. {
  229. int state;
  230. int result;
  231. if (!device)
  232. return -EINVAL;
  233. device->power.state = ACPI_STATE_UNKNOWN;
  234. if (!acpi_device_is_present(device))
  235. return -ENXIO;
  236. result = acpi_device_get_power(device, &state);
  237. if (result)
  238. return result;
  239. if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
  240. /* Reference count the power resources. */
  241. result = acpi_power_on_resources(device, state);
  242. if (result)
  243. return result;
  244. if (state == ACPI_STATE_D0) {
  245. /*
  246. * If _PSC is not present and the state inferred from
  247. * power resources appears to be D0, it still may be
  248. * necessary to execute _PS0 at this point, because
  249. * another device using the same power resources may
  250. * have been put into D0 previously and that's why we
  251. * see D0 here.
  252. */
  253. result = acpi_dev_pm_explicit_set(device, state);
  254. if (result)
  255. return result;
  256. }
  257. } else if (state == ACPI_STATE_UNKNOWN) {
  258. /*
  259. * No power resources and missing _PSC? Cross fingers and make
  260. * it D0 in hope that this is what the BIOS put the device into.
  261. * [We tried to force D0 here by executing _PS0, but that broke
  262. * Toshiba P870-303 in a nasty way.]
  263. */
  264. state = ACPI_STATE_D0;
  265. }
  266. device->power.state = state;
  267. return 0;
  268. }
  269. /**
  270. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  271. * @device: Device object whose power state is to be fixed up.
  272. *
  273. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  274. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  275. * not be the case and this function should be used then.
  276. */
  277. int acpi_device_fix_up_power(struct acpi_device *device)
  278. {
  279. int ret = 0;
  280. if (!device->power.flags.power_resources
  281. && !device->power.flags.explicit_get
  282. && device->power.state == ACPI_STATE_D0)
  283. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  284. return ret;
  285. }
  286. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  287. {
  288. int state;
  289. int result;
  290. if (device->power.state == ACPI_STATE_UNKNOWN) {
  291. result = acpi_bus_init_power(device);
  292. if (!result && state_p)
  293. *state_p = device->power.state;
  294. return result;
  295. }
  296. result = acpi_device_get_power(device, &state);
  297. if (result)
  298. return result;
  299. if (state == ACPI_STATE_UNKNOWN) {
  300. state = ACPI_STATE_D0;
  301. result = acpi_device_set_power(device, state);
  302. if (result)
  303. return result;
  304. } else {
  305. if (device->power.flags.power_resources) {
  306. /*
  307. * We don't need to really switch the state, bu we need
  308. * to update the power resources' reference counters.
  309. */
  310. result = acpi_power_transition(device, state);
  311. if (result)
  312. return result;
  313. }
  314. device->power.state = state;
  315. }
  316. if (state_p)
  317. *state_p = state;
  318. return 0;
  319. }
  320. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  321. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  322. {
  323. struct acpi_device *device;
  324. int result;
  325. result = acpi_bus_get_device(handle, &device);
  326. return result ? result : acpi_device_update_power(device, state_p);
  327. }
  328. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  329. bool acpi_bus_power_manageable(acpi_handle handle)
  330. {
  331. struct acpi_device *device;
  332. int result;
  333. result = acpi_bus_get_device(handle, &device);
  334. return result ? false : device->flags.power_manageable;
  335. }
  336. EXPORT_SYMBOL(acpi_bus_power_manageable);
  337. #ifdef CONFIG_PM
  338. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  339. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  340. {
  341. struct acpi_device *adev;
  342. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  343. return;
  344. adev = acpi_bus_get_acpi_device(handle);
  345. if (!adev)
  346. return;
  347. mutex_lock(&acpi_pm_notifier_lock);
  348. if (adev->wakeup.flags.notifier_present) {
  349. __pm_wakeup_event(adev->wakeup.ws, 0);
  350. if (adev->wakeup.context.work.func)
  351. queue_pm_work(&adev->wakeup.context.work);
  352. }
  353. mutex_unlock(&acpi_pm_notifier_lock);
  354. acpi_bus_put_acpi_device(adev);
  355. }
  356. /**
  357. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  358. * @adev: ACPI device to add the notify handler for.
  359. * @dev: Device to generate a wakeup event for while handling the notification.
  360. * @work_func: Work function to execute when handling the notification.
  361. *
  362. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  363. * PM wakeup events. For example, wakeup events may be generated for bridges
  364. * if one of the devices below the bridge is signaling wakeup, even if the
  365. * bridge itself doesn't have a wakeup GPE associated with it.
  366. */
  367. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  368. void (*work_func)(struct work_struct *work))
  369. {
  370. acpi_status status = AE_ALREADY_EXISTS;
  371. if (!dev && !work_func)
  372. return AE_BAD_PARAMETER;
  373. mutex_lock(&acpi_pm_notifier_lock);
  374. if (adev->wakeup.flags.notifier_present)
  375. goto out;
  376. adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
  377. adev->wakeup.context.dev = dev;
  378. if (work_func)
  379. INIT_WORK(&adev->wakeup.context.work, work_func);
  380. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  381. acpi_pm_notify_handler, NULL);
  382. if (ACPI_FAILURE(status))
  383. goto out;
  384. adev->wakeup.flags.notifier_present = true;
  385. out:
  386. mutex_unlock(&acpi_pm_notifier_lock);
  387. return status;
  388. }
  389. /**
  390. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  391. * @adev: ACPI device to remove the notifier from.
  392. */
  393. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  394. {
  395. acpi_status status = AE_BAD_PARAMETER;
  396. mutex_lock(&acpi_pm_notifier_lock);
  397. if (!adev->wakeup.flags.notifier_present)
  398. goto out;
  399. status = acpi_remove_notify_handler(adev->handle,
  400. ACPI_SYSTEM_NOTIFY,
  401. acpi_pm_notify_handler);
  402. if (ACPI_FAILURE(status))
  403. goto out;
  404. if (adev->wakeup.context.work.func) {
  405. cancel_work_sync(&adev->wakeup.context.work);
  406. adev->wakeup.context.work.func = NULL;
  407. }
  408. adev->wakeup.context.dev = NULL;
  409. wakeup_source_unregister(adev->wakeup.ws);
  410. adev->wakeup.flags.notifier_present = false;
  411. out:
  412. mutex_unlock(&acpi_pm_notifier_lock);
  413. return status;
  414. }
  415. bool acpi_bus_can_wakeup(acpi_handle handle)
  416. {
  417. struct acpi_device *device;
  418. int result;
  419. result = acpi_bus_get_device(handle, &device);
  420. return result ? false : device->wakeup.flags.valid;
  421. }
  422. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  423. /**
  424. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  425. * @dev: Device whose preferred target power state to return.
  426. * @adev: ACPI device node corresponding to @dev.
  427. * @target_state: System state to match the resultant device state.
  428. * @d_min_p: Location to store the highest power state available to the device.
  429. * @d_max_p: Location to store the lowest power state available to the device.
  430. *
  431. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  432. * device power states that the device can be in while the system is in the
  433. * state represented by @target_state. Store the integer numbers representing
  434. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  435. * respectively.
  436. *
  437. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  438. * actually corresponds to @dev before using this function.
  439. *
  440. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  441. * returns a value that doesn't make sense. The memory locations pointed to by
  442. * @d_max_p and @d_min_p are only modified on success.
  443. */
  444. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  445. u32 target_state, int *d_min_p, int *d_max_p)
  446. {
  447. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  448. acpi_handle handle = adev->handle;
  449. unsigned long long ret;
  450. int d_min, d_max;
  451. bool wakeup = false;
  452. acpi_status status;
  453. /*
  454. * If the system state is S0, the lowest power state the device can be
  455. * in is D3cold, unless the device has _S0W and is supposed to signal
  456. * wakeup, in which case the return value of _S0W has to be used as the
  457. * lowest power state available to the device.
  458. */
  459. d_min = ACPI_STATE_D0;
  460. d_max = ACPI_STATE_D3_COLD;
  461. /*
  462. * If present, _SxD methods return the minimum D-state (highest power
  463. * state) we can use for the corresponding S-states. Otherwise, the
  464. * minimum D-state is D0 (ACPI 3.x).
  465. */
  466. if (target_state > ACPI_STATE_S0) {
  467. /*
  468. * We rely on acpi_evaluate_integer() not clobbering the integer
  469. * provided if AE_NOT_FOUND is returned.
  470. */
  471. ret = d_min;
  472. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  473. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  474. || ret > ACPI_STATE_D3_COLD)
  475. return -ENODATA;
  476. /*
  477. * We need to handle legacy systems where D3hot and D3cold are
  478. * the same and 3 is returned in both cases, so fall back to
  479. * D3cold if D3hot is not a valid state.
  480. */
  481. if (!adev->power.states[ret].flags.valid) {
  482. if (ret == ACPI_STATE_D3_HOT)
  483. ret = ACPI_STATE_D3_COLD;
  484. else
  485. return -ENODATA;
  486. }
  487. d_min = ret;
  488. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  489. && adev->wakeup.sleep_state >= target_state;
  490. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  491. PM_QOS_FLAGS_NONE) {
  492. wakeup = adev->wakeup.flags.valid;
  493. }
  494. /*
  495. * If _PRW says we can wake up the system from the target sleep state,
  496. * the D-state returned by _SxD is sufficient for that (we assume a
  497. * wakeup-aware driver if wake is set). Still, if _SxW exists
  498. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  499. * can wake the system. _S0W may be valid, too.
  500. */
  501. if (wakeup) {
  502. method[3] = 'W';
  503. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  504. if (status == AE_NOT_FOUND) {
  505. if (target_state > ACPI_STATE_S0)
  506. d_max = d_min;
  507. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  508. /* Fall back to D3cold if ret is not a valid state. */
  509. if (!adev->power.states[ret].flags.valid)
  510. ret = ACPI_STATE_D3_COLD;
  511. d_max = ret > d_min ? ret : d_min;
  512. } else {
  513. return -ENODATA;
  514. }
  515. }
  516. if (d_min_p)
  517. *d_min_p = d_min;
  518. if (d_max_p)
  519. *d_max_p = d_max;
  520. return 0;
  521. }
  522. /**
  523. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  524. * @dev: Device whose preferred target power state to return.
  525. * @d_min_p: Location to store the upper limit of the allowed states range.
  526. * @d_max_in: Deepest low-power state to take into consideration.
  527. * Return value: Preferred power state of the device on success, -ENODEV
  528. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  529. * incorrect, or -ENODATA on ACPI method failure.
  530. *
  531. * The caller must ensure that @dev is valid before using this function.
  532. */
  533. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  534. {
  535. struct acpi_device *adev;
  536. int ret, d_min, d_max;
  537. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  538. return -EINVAL;
  539. if (d_max_in > ACPI_STATE_D2) {
  540. enum pm_qos_flags_status stat;
  541. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  542. if (stat == PM_QOS_FLAGS_ALL)
  543. d_max_in = ACPI_STATE_D2;
  544. }
  545. adev = ACPI_COMPANION(dev);
  546. if (!adev) {
  547. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  548. return -ENODEV;
  549. }
  550. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  551. &d_min, &d_max);
  552. if (ret)
  553. return ret;
  554. if (d_max_in < d_min)
  555. return -EINVAL;
  556. if (d_max > d_max_in) {
  557. for (d_max = d_max_in; d_max > d_min; d_max--) {
  558. if (adev->power.states[d_max].flags.valid)
  559. break;
  560. }
  561. }
  562. if (d_min_p)
  563. *d_min_p = d_min;
  564. return d_max;
  565. }
  566. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  567. /**
  568. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  569. * @work: Work item to handle.
  570. */
  571. static void acpi_pm_notify_work_func(struct work_struct *work)
  572. {
  573. struct device *dev;
  574. dev = container_of(work, struct acpi_device_wakeup_context, work)->dev;
  575. if (dev) {
  576. pm_wakeup_event(dev, 0);
  577. pm_runtime_resume(dev);
  578. }
  579. }
  580. /**
  581. * acpi_device_wakeup - Enable/disable wakeup functionality for device.
  582. * @adev: ACPI device to enable/disable wakeup functionality for.
  583. * @target_state: State the system is transitioning into.
  584. * @enable: Whether to enable or disable the wakeup functionality.
  585. *
  586. * Enable/disable the GPE associated with @adev so that it can generate
  587. * wakeup signals for the device in response to external (remote) events and
  588. * enable/disable device wakeup power.
  589. *
  590. * Callers must ensure that @adev is a valid ACPI device node before executing
  591. * this function.
  592. */
  593. static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state,
  594. bool enable)
  595. {
  596. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  597. if (enable) {
  598. acpi_status res;
  599. int error;
  600. error = acpi_enable_wakeup_device_power(adev, target_state);
  601. if (error)
  602. return error;
  603. if (adev->wakeup.flags.enabled)
  604. return 0;
  605. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  606. if (ACPI_SUCCESS(res)) {
  607. adev->wakeup.flags.enabled = 1;
  608. } else {
  609. acpi_disable_wakeup_device_power(adev);
  610. return -EIO;
  611. }
  612. } else {
  613. if (adev->wakeup.flags.enabled) {
  614. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  615. adev->wakeup.flags.enabled = 0;
  616. }
  617. acpi_disable_wakeup_device_power(adev);
  618. }
  619. return 0;
  620. }
  621. /**
  622. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  623. * @dev: Device to enable/disable the platform to wake up.
  624. * @enable: Whether to enable or disable the wakeup functionality.
  625. */
  626. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  627. {
  628. struct acpi_device *adev;
  629. if (!device_run_wake(phys_dev))
  630. return -EINVAL;
  631. adev = ACPI_COMPANION(phys_dev);
  632. if (!adev) {
  633. dev_dbg(phys_dev, "ACPI companion missing in %s!\n", __func__);
  634. return -ENODEV;
  635. }
  636. return acpi_device_wakeup(adev, ACPI_STATE_S0, enable);
  637. }
  638. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  639. #ifdef CONFIG_PM_SLEEP
  640. /**
  641. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  642. * @dev: Device to enable/desible to wake up the system from sleep states.
  643. * @enable: Whether to enable or disable @dev to wake up the system.
  644. */
  645. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  646. {
  647. struct acpi_device *adev;
  648. int error;
  649. if (!device_can_wakeup(dev))
  650. return -EINVAL;
  651. adev = ACPI_COMPANION(dev);
  652. if (!adev) {
  653. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  654. return -ENODEV;
  655. }
  656. error = acpi_device_wakeup(adev, acpi_target_system_state(), enable);
  657. if (!error)
  658. dev_info(dev, "System wakeup %s by ACPI\n",
  659. enable ? "enabled" : "disabled");
  660. return error;
  661. }
  662. #endif /* CONFIG_PM_SLEEP */
  663. /**
  664. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  665. * @dev: Device to put into a low-power state.
  666. * @adev: ACPI device node corresponding to @dev.
  667. * @system_state: System state to choose the device state for.
  668. */
  669. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  670. u32 system_state)
  671. {
  672. int ret, state;
  673. if (!acpi_device_power_manageable(adev))
  674. return 0;
  675. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  676. return ret ? ret : acpi_device_set_power(adev, state);
  677. }
  678. /**
  679. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  680. * @adev: ACPI device node to put into the full-power state.
  681. */
  682. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  683. {
  684. return acpi_device_power_manageable(adev) ?
  685. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  686. }
  687. /**
  688. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  689. * @dev: Device to put into a low-power state.
  690. *
  691. * Put the given device into a runtime low-power state using the standard ACPI
  692. * mechanism. Set up remote wakeup if desired, choose the state to put the
  693. * device into (this checks if remote wakeup is expected to work too), and set
  694. * the power state of the device.
  695. */
  696. int acpi_dev_runtime_suspend(struct device *dev)
  697. {
  698. struct acpi_device *adev = ACPI_COMPANION(dev);
  699. bool remote_wakeup;
  700. int error;
  701. if (!adev)
  702. return 0;
  703. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  704. PM_QOS_FLAGS_NONE;
  705. error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup);
  706. if (remote_wakeup && error)
  707. return -EAGAIN;
  708. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  709. if (error)
  710. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  711. return error;
  712. }
  713. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  714. /**
  715. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  716. * @dev: Device to put into the full-power state.
  717. *
  718. * Put the given device into the full-power state using the standard ACPI
  719. * mechanism at run time. Set the power state of the device to ACPI D0 and
  720. * disable remote wakeup.
  721. */
  722. int acpi_dev_runtime_resume(struct device *dev)
  723. {
  724. struct acpi_device *adev = ACPI_COMPANION(dev);
  725. int error;
  726. if (!adev)
  727. return 0;
  728. error = acpi_dev_pm_full_power(adev);
  729. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  730. return error;
  731. }
  732. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  733. /**
  734. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  735. * @dev: Device to suspend.
  736. *
  737. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  738. * it into a runtime low-power state.
  739. */
  740. int acpi_subsys_runtime_suspend(struct device *dev)
  741. {
  742. int ret = pm_generic_runtime_suspend(dev);
  743. return ret ? ret : acpi_dev_runtime_suspend(dev);
  744. }
  745. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  746. /**
  747. * acpi_subsys_runtime_resume - Resume device using ACPI.
  748. * @dev: Device to Resume.
  749. *
  750. * Use ACPI to put the given device into the full-power state and carry out the
  751. * generic runtime resume procedure for it.
  752. */
  753. int acpi_subsys_runtime_resume(struct device *dev)
  754. {
  755. int ret = acpi_dev_runtime_resume(dev);
  756. return ret ? ret : pm_generic_runtime_resume(dev);
  757. }
  758. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  759. #ifdef CONFIG_PM_SLEEP
  760. /**
  761. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  762. * @dev: Device to put into a low-power state.
  763. *
  764. * Put the given device into a low-power state during system transition to a
  765. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  766. * desired, choose the state to put the device into (this checks if system
  767. * wakeup is expected to work too), and set the power state of the device.
  768. */
  769. int acpi_dev_suspend_late(struct device *dev)
  770. {
  771. struct acpi_device *adev = ACPI_COMPANION(dev);
  772. u32 target_state;
  773. bool wakeup;
  774. int error;
  775. if (!adev)
  776. return 0;
  777. target_state = acpi_target_system_state();
  778. wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
  779. error = acpi_device_wakeup(adev, target_state, wakeup);
  780. if (wakeup && error)
  781. return error;
  782. error = acpi_dev_pm_low_power(dev, adev, target_state);
  783. if (error)
  784. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  785. return error;
  786. }
  787. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  788. /**
  789. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  790. * @dev: Device to put into the full-power state.
  791. *
  792. * Put the given device into the full-power state using the standard ACPI
  793. * mechanism during system transition to the working state. Set the power
  794. * state of the device to ACPI D0 and disable remote wakeup.
  795. */
  796. int acpi_dev_resume_early(struct device *dev)
  797. {
  798. struct acpi_device *adev = ACPI_COMPANION(dev);
  799. int error;
  800. if (!adev)
  801. return 0;
  802. error = acpi_dev_pm_full_power(adev);
  803. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  804. return error;
  805. }
  806. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  807. /**
  808. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  809. * @dev: Device to prepare.
  810. */
  811. int acpi_subsys_prepare(struct device *dev)
  812. {
  813. struct acpi_device *adev = ACPI_COMPANION(dev);
  814. u32 sys_target;
  815. int ret, state;
  816. ret = pm_generic_prepare(dev);
  817. if (ret < 0)
  818. return ret;
  819. if (!adev || !pm_runtime_suspended(dev)
  820. || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  821. return 0;
  822. sys_target = acpi_target_system_state();
  823. if (sys_target == ACPI_STATE_S0)
  824. return 1;
  825. if (adev->power.flags.dsw_present)
  826. return 0;
  827. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  828. return !ret && state == adev->power.state;
  829. }
  830. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  831. /**
  832. * acpi_subsys_complete - Finalize device's resume during system resume.
  833. * @dev: Device to handle.
  834. */
  835. void acpi_subsys_complete(struct device *dev)
  836. {
  837. pm_generic_complete(dev);
  838. /*
  839. * If the device had been runtime-suspended before the system went into
  840. * the sleep state it is going out of and it has never been resumed till
  841. * now, resume it in case the firmware powered it up.
  842. */
  843. if (dev->power.direct_complete)
  844. pm_request_resume(dev);
  845. }
  846. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  847. /**
  848. * acpi_subsys_suspend - Run the device driver's suspend callback.
  849. * @dev: Device to handle.
  850. *
  851. * Follow PCI and resume devices suspended at run time before running their
  852. * system suspend callbacks.
  853. */
  854. int acpi_subsys_suspend(struct device *dev)
  855. {
  856. pm_runtime_resume(dev);
  857. return pm_generic_suspend(dev);
  858. }
  859. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  860. /**
  861. * acpi_subsys_suspend_late - Suspend device using ACPI.
  862. * @dev: Device to suspend.
  863. *
  864. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  865. * it into a low-power state during system transition into a sleep state.
  866. */
  867. int acpi_subsys_suspend_late(struct device *dev)
  868. {
  869. int ret = pm_generic_suspend_late(dev);
  870. return ret ? ret : acpi_dev_suspend_late(dev);
  871. }
  872. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  873. /**
  874. * acpi_subsys_resume_early - Resume device using ACPI.
  875. * @dev: Device to Resume.
  876. *
  877. * Use ACPI to put the given device into the full-power state and carry out the
  878. * generic early resume procedure for it during system transition into the
  879. * working state.
  880. */
  881. int acpi_subsys_resume_early(struct device *dev)
  882. {
  883. int ret = acpi_dev_resume_early(dev);
  884. return ret ? ret : pm_generic_resume_early(dev);
  885. }
  886. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  887. /**
  888. * acpi_subsys_freeze - Run the device driver's freeze callback.
  889. * @dev: Device to handle.
  890. */
  891. int acpi_subsys_freeze(struct device *dev)
  892. {
  893. /*
  894. * This used to be done in acpi_subsys_prepare() for all devices and
  895. * some drivers may depend on it, so do it here. Ideally, however,
  896. * runtime-suspended devices should not be touched during freeze/thaw
  897. * transitions.
  898. */
  899. pm_runtime_resume(dev);
  900. return pm_generic_freeze(dev);
  901. }
  902. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  903. #endif /* CONFIG_PM_SLEEP */
  904. static struct dev_pm_domain acpi_general_pm_domain = {
  905. .ops = {
  906. .runtime_suspend = acpi_subsys_runtime_suspend,
  907. .runtime_resume = acpi_subsys_runtime_resume,
  908. #ifdef CONFIG_PM_SLEEP
  909. .prepare = acpi_subsys_prepare,
  910. .complete = acpi_subsys_complete,
  911. .suspend = acpi_subsys_suspend,
  912. .suspend_late = acpi_subsys_suspend_late,
  913. .resume_early = acpi_subsys_resume_early,
  914. .freeze = acpi_subsys_freeze,
  915. .poweroff = acpi_subsys_suspend,
  916. .poweroff_late = acpi_subsys_suspend_late,
  917. .restore_early = acpi_subsys_resume_early,
  918. #endif
  919. },
  920. };
  921. /**
  922. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  923. * @dev: Device to take care of.
  924. * @power_off: Whether or not to try to remove power from the device.
  925. *
  926. * Remove the device from the general ACPI PM domain and remove its wakeup
  927. * notifier. If @power_off is set, additionally remove power from the device if
  928. * possible.
  929. *
  930. * Callers must ensure proper synchronization of this function with power
  931. * management callbacks.
  932. */
  933. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  934. {
  935. struct acpi_device *adev = ACPI_COMPANION(dev);
  936. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  937. dev->pm_domain = NULL;
  938. acpi_remove_pm_notifier(adev);
  939. if (power_off) {
  940. /*
  941. * If the device's PM QoS resume latency limit or flags
  942. * have been exposed to user space, they have to be
  943. * hidden at this point, so that they don't affect the
  944. * choice of the low-power state to put the device into.
  945. */
  946. dev_pm_qos_hide_latency_limit(dev);
  947. dev_pm_qos_hide_flags(dev);
  948. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  949. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  950. }
  951. }
  952. }
  953. /**
  954. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  955. * @dev: Device to prepare.
  956. * @power_on: Whether or not to power on the device.
  957. *
  958. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  959. * attached to it, install a wakeup notification handler for the device and
  960. * add it to the general ACPI PM domain. If @power_on is set, the device will
  961. * be put into the ACPI D0 state before the function returns.
  962. *
  963. * This assumes that the @dev's bus type uses generic power management callbacks
  964. * (or doesn't use any power management callbacks at all).
  965. *
  966. * Callers must ensure proper synchronization of this function with power
  967. * management callbacks.
  968. */
  969. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  970. {
  971. struct acpi_device *adev = ACPI_COMPANION(dev);
  972. if (!adev)
  973. return -ENODEV;
  974. if (dev->pm_domain)
  975. return -EEXIST;
  976. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  977. dev->pm_domain = &acpi_general_pm_domain;
  978. if (power_on) {
  979. acpi_dev_pm_full_power(adev);
  980. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  981. }
  982. dev->pm_domain->detach = acpi_dev_pm_detach;
  983. return 0;
  984. }
  985. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  986. #endif /* CONFIG_PM */