device_pm.c 31 KB

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