device_pm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. /* No power resources and missing _PSC? Try to force D0. */
  244. state = ACPI_STATE_D0;
  245. result = acpi_dev_pm_explicit_set(device, state);
  246. if (result)
  247. return result;
  248. }
  249. device->power.state = state;
  250. return 0;
  251. }
  252. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  253. {
  254. struct acpi_device *device;
  255. int state;
  256. int result;
  257. result = acpi_bus_get_device(handle, &device);
  258. if (result)
  259. return result;
  260. result = acpi_device_get_power(device, &state);
  261. if (result)
  262. return result;
  263. if (state == ACPI_STATE_UNKNOWN)
  264. state = ACPI_STATE_D0;
  265. result = acpi_device_set_power(device, state);
  266. if (!result && state_p)
  267. *state_p = state;
  268. return result;
  269. }
  270. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  271. bool acpi_bus_power_manageable(acpi_handle handle)
  272. {
  273. struct acpi_device *device;
  274. int result;
  275. result = acpi_bus_get_device(handle, &device);
  276. return result ? false : device->flags.power_manageable;
  277. }
  278. EXPORT_SYMBOL(acpi_bus_power_manageable);
  279. #ifdef CONFIG_PM
  280. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  281. /**
  282. * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
  283. * @adev: ACPI device to add the notifier for.
  284. * @context: Context information to pass to the notifier routine.
  285. *
  286. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  287. * PM wakeup events. For example, wakeup events may be generated for bridges
  288. * if one of the devices below the bridge is signaling wakeup, even if the
  289. * bridge itself doesn't have a wakeup GPE associated with it.
  290. */
  291. acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
  292. acpi_notify_handler handler, void *context)
  293. {
  294. acpi_status status = AE_ALREADY_EXISTS;
  295. mutex_lock(&acpi_pm_notifier_lock);
  296. if (adev->wakeup.flags.notifier_present)
  297. goto out;
  298. status = acpi_install_notify_handler(adev->handle,
  299. ACPI_SYSTEM_NOTIFY,
  300. handler, context);
  301. if (ACPI_FAILURE(status))
  302. goto out;
  303. adev->wakeup.flags.notifier_present = true;
  304. out:
  305. mutex_unlock(&acpi_pm_notifier_lock);
  306. return status;
  307. }
  308. /**
  309. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  310. * @adev: ACPI device to remove the notifier from.
  311. */
  312. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
  313. acpi_notify_handler handler)
  314. {
  315. acpi_status status = AE_BAD_PARAMETER;
  316. mutex_lock(&acpi_pm_notifier_lock);
  317. if (!adev->wakeup.flags.notifier_present)
  318. goto out;
  319. status = acpi_remove_notify_handler(adev->handle,
  320. ACPI_SYSTEM_NOTIFY,
  321. handler);
  322. if (ACPI_FAILURE(status))
  323. goto out;
  324. adev->wakeup.flags.notifier_present = false;
  325. out:
  326. mutex_unlock(&acpi_pm_notifier_lock);
  327. return status;
  328. }
  329. bool acpi_bus_can_wakeup(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->wakeup.flags.valid;
  335. }
  336. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  337. /**
  338. * acpi_device_power_state - Get preferred power state of ACPI device.
  339. * @dev: Device whose preferred target power state to return.
  340. * @adev: ACPI device node corresponding to @dev.
  341. * @target_state: System state to match the resultant device state.
  342. * @d_max_in: Deepest low-power state to take into consideration.
  343. * @d_min_p: Location to store the upper limit of the allowed states range.
  344. * Return value: Preferred power state of the device on success, -ENODEV
  345. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  346. *
  347. * Find the lowest power (highest number) ACPI device power state that the
  348. * device can be in while the system is in the state represented by
  349. * @target_state. If @d_min_p is set, the highest power (lowest number) device
  350. * power state that @dev can be in for the given system sleep state is stored
  351. * at the location pointed to by it.
  352. *
  353. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  354. * actually corresponds to @dev before using this function.
  355. */
  356. int acpi_device_power_state(struct device *dev, struct acpi_device *adev,
  357. u32 target_state, int d_max_in, int *d_min_p)
  358. {
  359. char acpi_method[] = "_SxD";
  360. unsigned long long d_min, d_max;
  361. bool wakeup = false;
  362. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
  363. return -EINVAL;
  364. if (d_max_in > ACPI_STATE_D3_HOT) {
  365. enum pm_qos_flags_status stat;
  366. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  367. if (stat == PM_QOS_FLAGS_ALL)
  368. d_max_in = ACPI_STATE_D3_HOT;
  369. }
  370. acpi_method[2] = '0' + target_state;
  371. /*
  372. * If the sleep state is S0, the lowest limit from ACPI is D3,
  373. * but if the device has _S0W, we will use the value from _S0W
  374. * as the lowest limit from ACPI. Finally, we will constrain
  375. * the lowest limit with the specified one.
  376. */
  377. d_min = ACPI_STATE_D0;
  378. d_max = ACPI_STATE_D3;
  379. /*
  380. * If present, _SxD methods return the minimum D-state (highest power
  381. * state) we can use for the corresponding S-states. Otherwise, the
  382. * minimum D-state is D0 (ACPI 3.x).
  383. *
  384. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  385. * provided -- that's our fault recovery, we ignore retval.
  386. */
  387. if (target_state > ACPI_STATE_S0) {
  388. acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);
  389. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  390. && adev->wakeup.sleep_state >= target_state;
  391. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  392. PM_QOS_FLAGS_NONE) {
  393. wakeup = adev->wakeup.flags.valid;
  394. }
  395. /*
  396. * If _PRW says we can wake up the system from the target sleep state,
  397. * the D-state returned by _SxD is sufficient for that (we assume a
  398. * wakeup-aware driver if wake is set). Still, if _SxW exists
  399. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  400. * can wake the system. _S0W may be valid, too.
  401. */
  402. if (wakeup) {
  403. acpi_status status;
  404. acpi_method[3] = 'W';
  405. status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
  406. &d_max);
  407. if (ACPI_FAILURE(status)) {
  408. if (target_state != ACPI_STATE_S0 ||
  409. status != AE_NOT_FOUND)
  410. d_max = d_min;
  411. } else if (d_max < d_min) {
  412. /* Warn the user of the broken DSDT */
  413. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  414. acpi_method);
  415. /* Sanitize it */
  416. d_min = d_max;
  417. }
  418. }
  419. if (d_max_in < d_min)
  420. return -EINVAL;
  421. if (d_min_p)
  422. *d_min_p = d_min;
  423. /* constrain d_max with specified lowest limit (max number) */
  424. if (d_max > d_max_in) {
  425. for (d_max = d_max_in; d_max > d_min; d_max--) {
  426. if (adev->power.states[d_max].flags.valid)
  427. break;
  428. }
  429. }
  430. return d_max;
  431. }
  432. EXPORT_SYMBOL_GPL(acpi_device_power_state);
  433. /**
  434. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  435. * @dev: Device whose preferred target power state to return.
  436. * @d_min_p: Location to store the upper limit of the allowed states range.
  437. * @d_max_in: Deepest low-power state to take into consideration.
  438. * Return value: Preferred power state of the device on success, -ENODEV
  439. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  440. *
  441. * The caller must ensure that @dev is valid before using this function.
  442. */
  443. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  444. {
  445. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  446. struct acpi_device *adev;
  447. if (!handle || acpi_bus_get_device(handle, &adev)) {
  448. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  449. return -ENODEV;
  450. }
  451. return acpi_device_power_state(dev, adev, acpi_target_system_state(),
  452. d_max_in, d_min_p);
  453. }
  454. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  455. #ifdef CONFIG_PM_RUNTIME
  456. /**
  457. * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
  458. * @handle: ACPI handle of the device the notification is for.
  459. * @event: Type of the signaled event.
  460. * @context: Device corresponding to @handle.
  461. */
  462. static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
  463. {
  464. struct device *dev = context;
  465. if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
  466. pm_wakeup_event(dev, 0);
  467. pm_runtime_resume(dev);
  468. }
  469. }
  470. /**
  471. * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
  472. * @adev: ACPI device to enable/disable the remote wakeup for.
  473. * @enable: Whether to enable or disable the wakeup functionality.
  474. *
  475. * Enable/disable the GPE associated with @adev so that it can generate
  476. * wakeup signals for the device in response to external (remote) events and
  477. * enable/disable device wakeup power.
  478. *
  479. * Callers must ensure that @adev is a valid ACPI device node before executing
  480. * this function.
  481. */
  482. int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
  483. {
  484. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  485. if (enable) {
  486. acpi_status res;
  487. int error;
  488. error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
  489. if (error)
  490. return error;
  491. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  492. if (ACPI_FAILURE(res)) {
  493. acpi_disable_wakeup_device_power(adev);
  494. return -EIO;
  495. }
  496. } else {
  497. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  498. acpi_disable_wakeup_device_power(adev);
  499. }
  500. return 0;
  501. }
  502. /**
  503. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  504. * @dev: Device to enable/disable the platform to wake up.
  505. * @enable: Whether to enable or disable the wakeup functionality.
  506. */
  507. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  508. {
  509. struct acpi_device *adev;
  510. acpi_handle handle;
  511. if (!device_run_wake(phys_dev))
  512. return -EINVAL;
  513. handle = DEVICE_ACPI_HANDLE(phys_dev);
  514. if (!handle || acpi_bus_get_device(handle, &adev)) {
  515. dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
  516. __func__);
  517. return -ENODEV;
  518. }
  519. return __acpi_device_run_wake(adev, enable);
  520. }
  521. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  522. #else
  523. static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
  524. void *context) {}
  525. #endif /* CONFIG_PM_RUNTIME */
  526. #ifdef CONFIG_PM_SLEEP
  527. /**
  528. * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
  529. * @dev: Device to enable/desible to wake up the system.
  530. * @target_state: System state the device is supposed to wake up from.
  531. * @enable: Whether to enable or disable @dev to wake up the system.
  532. */
  533. int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
  534. bool enable)
  535. {
  536. return enable ?
  537. acpi_enable_wakeup_device_power(adev, target_state) :
  538. acpi_disable_wakeup_device_power(adev);
  539. }
  540. /**
  541. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  542. * @dev: Device to enable/desible to wake up the system from sleep states.
  543. * @enable: Whether to enable or disable @dev to wake up the system.
  544. */
  545. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  546. {
  547. acpi_handle handle;
  548. struct acpi_device *adev;
  549. int error;
  550. if (!device_can_wakeup(dev))
  551. return -EINVAL;
  552. handle = DEVICE_ACPI_HANDLE(dev);
  553. if (!handle || acpi_bus_get_device(handle, &adev)) {
  554. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  555. return -ENODEV;
  556. }
  557. error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
  558. enable);
  559. if (!error)
  560. dev_info(dev, "System wakeup %s by ACPI\n",
  561. enable ? "enabled" : "disabled");
  562. return error;
  563. }
  564. #endif /* CONFIG_PM_SLEEP */
  565. /**
  566. * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
  567. * @dev: Device to get the ACPI node for.
  568. */
  569. struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
  570. {
  571. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  572. struct acpi_device *adev;
  573. return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
  574. }
  575. /**
  576. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  577. * @dev: Device to put into a low-power state.
  578. * @adev: ACPI device node corresponding to @dev.
  579. * @system_state: System state to choose the device state for.
  580. */
  581. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  582. u32 system_state)
  583. {
  584. int power_state;
  585. if (!acpi_device_power_manageable(adev))
  586. return 0;
  587. power_state = acpi_device_power_state(dev, adev, system_state,
  588. ACPI_STATE_D3, NULL);
  589. if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
  590. return -EIO;
  591. return acpi_device_set_power(adev, power_state);
  592. }
  593. /**
  594. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  595. * @adev: ACPI device node to put into the full-power state.
  596. */
  597. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  598. {
  599. return acpi_device_power_manageable(adev) ?
  600. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  601. }
  602. #ifdef CONFIG_PM_RUNTIME
  603. /**
  604. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  605. * @dev: Device to put into a low-power state.
  606. *
  607. * Put the given device into a runtime low-power state using the standard ACPI
  608. * mechanism. Set up remote wakeup if desired, choose the state to put the
  609. * device into (this checks if remote wakeup is expected to work too), and set
  610. * the power state of the device.
  611. */
  612. int acpi_dev_runtime_suspend(struct device *dev)
  613. {
  614. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  615. bool remote_wakeup;
  616. int error;
  617. if (!adev)
  618. return 0;
  619. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  620. PM_QOS_FLAGS_NONE;
  621. error = __acpi_device_run_wake(adev, remote_wakeup);
  622. if (remote_wakeup && error)
  623. return -EAGAIN;
  624. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  625. if (error)
  626. __acpi_device_run_wake(adev, false);
  627. return error;
  628. }
  629. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  630. /**
  631. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  632. * @dev: Device to put into the full-power state.
  633. *
  634. * Put the given device into the full-power state using the standard ACPI
  635. * mechanism at run time. Set the power state of the device to ACPI D0 and
  636. * disable remote wakeup.
  637. */
  638. int acpi_dev_runtime_resume(struct device *dev)
  639. {
  640. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  641. int error;
  642. if (!adev)
  643. return 0;
  644. error = acpi_dev_pm_full_power(adev);
  645. __acpi_device_run_wake(adev, false);
  646. return error;
  647. }
  648. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  649. /**
  650. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  651. * @dev: Device to suspend.
  652. *
  653. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  654. * it into a runtime low-power state.
  655. */
  656. int acpi_subsys_runtime_suspend(struct device *dev)
  657. {
  658. int ret = pm_generic_runtime_suspend(dev);
  659. return ret ? ret : acpi_dev_runtime_suspend(dev);
  660. }
  661. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  662. /**
  663. * acpi_subsys_runtime_resume - Resume device using ACPI.
  664. * @dev: Device to Resume.
  665. *
  666. * Use ACPI to put the given device into the full-power state and carry out the
  667. * generic runtime resume procedure for it.
  668. */
  669. int acpi_subsys_runtime_resume(struct device *dev)
  670. {
  671. int ret = acpi_dev_runtime_resume(dev);
  672. return ret ? ret : pm_generic_runtime_resume(dev);
  673. }
  674. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  675. #endif /* CONFIG_PM_RUNTIME */
  676. #ifdef CONFIG_PM_SLEEP
  677. /**
  678. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  679. * @dev: Device to put into a low-power state.
  680. *
  681. * Put the given device into a low-power state during system transition to a
  682. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  683. * desired, choose the state to put the device into (this checks if system
  684. * wakeup is expected to work too), and set the power state of the device.
  685. */
  686. int acpi_dev_suspend_late(struct device *dev)
  687. {
  688. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  689. u32 target_state;
  690. bool wakeup;
  691. int error;
  692. if (!adev)
  693. return 0;
  694. target_state = acpi_target_system_state();
  695. wakeup = device_may_wakeup(dev);
  696. error = __acpi_device_sleep_wake(adev, target_state, wakeup);
  697. if (wakeup && error)
  698. return error;
  699. error = acpi_dev_pm_low_power(dev, adev, target_state);
  700. if (error)
  701. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  702. return error;
  703. }
  704. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  705. /**
  706. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  707. * @dev: Device to put into the full-power state.
  708. *
  709. * Put the given device into the full-power state using the standard ACPI
  710. * mechanism during system transition to the working state. Set the power
  711. * state of the device to ACPI D0 and disable remote wakeup.
  712. */
  713. int acpi_dev_resume_early(struct device *dev)
  714. {
  715. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  716. int error;
  717. if (!adev)
  718. return 0;
  719. error = acpi_dev_pm_full_power(adev);
  720. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  721. return error;
  722. }
  723. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  724. /**
  725. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  726. * @dev: Device to prepare.
  727. */
  728. int acpi_subsys_prepare(struct device *dev)
  729. {
  730. /*
  731. * Follow PCI and resume devices suspended at run time before running
  732. * their system suspend callbacks.
  733. */
  734. pm_runtime_resume(dev);
  735. return pm_generic_prepare(dev);
  736. }
  737. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  738. /**
  739. * acpi_subsys_suspend_late - Suspend device using ACPI.
  740. * @dev: Device to suspend.
  741. *
  742. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  743. * it into a low-power state during system transition into a sleep state.
  744. */
  745. int acpi_subsys_suspend_late(struct device *dev)
  746. {
  747. int ret = pm_generic_suspend_late(dev);
  748. return ret ? ret : acpi_dev_suspend_late(dev);
  749. }
  750. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  751. /**
  752. * acpi_subsys_resume_early - Resume device using ACPI.
  753. * @dev: Device to Resume.
  754. *
  755. * Use ACPI to put the given device into the full-power state and carry out the
  756. * generic early resume procedure for it during system transition into the
  757. * working state.
  758. */
  759. int acpi_subsys_resume_early(struct device *dev)
  760. {
  761. int ret = acpi_dev_resume_early(dev);
  762. return ret ? ret : pm_generic_resume_early(dev);
  763. }
  764. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  765. #endif /* CONFIG_PM_SLEEP */
  766. static struct dev_pm_domain acpi_general_pm_domain = {
  767. .ops = {
  768. #ifdef CONFIG_PM_RUNTIME
  769. .runtime_suspend = acpi_subsys_runtime_suspend,
  770. .runtime_resume = acpi_subsys_runtime_resume,
  771. .runtime_idle = pm_generic_runtime_idle,
  772. #endif
  773. #ifdef CONFIG_PM_SLEEP
  774. .prepare = acpi_subsys_prepare,
  775. .suspend_late = acpi_subsys_suspend_late,
  776. .resume_early = acpi_subsys_resume_early,
  777. .poweroff_late = acpi_subsys_suspend_late,
  778. .restore_early = acpi_subsys_resume_early,
  779. #endif
  780. },
  781. };
  782. /**
  783. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  784. * @dev: Device to prepare.
  785. * @power_on: Whether or not to power on the device.
  786. *
  787. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  788. * attached to it, install a wakeup notification handler for the device and
  789. * add it to the general ACPI PM domain. If @power_on is set, the device will
  790. * be put into the ACPI D0 state before the function returns.
  791. *
  792. * This assumes that the @dev's bus type uses generic power management callbacks
  793. * (or doesn't use any power management callbacks at all).
  794. *
  795. * Callers must ensure proper synchronization of this function with power
  796. * management callbacks.
  797. */
  798. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  799. {
  800. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  801. if (!adev)
  802. return -ENODEV;
  803. if (dev->pm_domain)
  804. return -EEXIST;
  805. acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
  806. dev->pm_domain = &acpi_general_pm_domain;
  807. if (power_on) {
  808. acpi_dev_pm_full_power(adev);
  809. __acpi_device_run_wake(adev, false);
  810. }
  811. return 0;
  812. }
  813. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  814. /**
  815. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  816. * @dev: Device to take care of.
  817. * @power_off: Whether or not to try to remove power from the device.
  818. *
  819. * Remove the device from the general ACPI PM domain and remove its wakeup
  820. * notifier. If @power_off is set, additionally remove power from the device if
  821. * possible.
  822. *
  823. * Callers must ensure proper synchronization of this function with power
  824. * management callbacks.
  825. */
  826. void acpi_dev_pm_detach(struct device *dev, bool power_off)
  827. {
  828. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  829. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  830. dev->pm_domain = NULL;
  831. acpi_remove_pm_notifier(adev, acpi_wakeup_device);
  832. if (power_off) {
  833. /*
  834. * If the device's PM QoS resume latency limit or flags
  835. * have been exposed to user space, they have to be
  836. * hidden at this point, so that they don't affect the
  837. * choice of the low-power state to put the device into.
  838. */
  839. dev_pm_qos_hide_latency_limit(dev);
  840. dev_pm_qos_hide_flags(dev);
  841. __acpi_device_run_wake(adev, false);
  842. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  843. }
  844. }
  845. }
  846. EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
  847. /**
  848. * acpi_dev_pm_add_dependent - Add physical device depending for PM.
  849. * @handle: Handle of ACPI device node.
  850. * @depdev: Device depending on that node for PM.
  851. */
  852. void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev)
  853. {
  854. struct acpi_device_physical_node *dep;
  855. struct acpi_device *adev;
  856. if (!depdev || acpi_bus_get_device(handle, &adev))
  857. return;
  858. mutex_lock(&adev->physical_node_lock);
  859. list_for_each_entry(dep, &adev->power_dependent, node)
  860. if (dep->dev == depdev)
  861. goto out;
  862. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  863. if (dep) {
  864. dep->dev = depdev;
  865. list_add_tail(&dep->node, &adev->power_dependent);
  866. }
  867. out:
  868. mutex_unlock(&adev->physical_node_lock);
  869. }
  870. EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent);
  871. /**
  872. * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
  873. * @handle: Handle of ACPI device node.
  874. * @depdev: Device depending on that node for PM.
  875. */
  876. void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev)
  877. {
  878. struct acpi_device_physical_node *dep;
  879. struct acpi_device *adev;
  880. if (!depdev || acpi_bus_get_device(handle, &adev))
  881. return;
  882. mutex_lock(&adev->physical_node_lock);
  883. list_for_each_entry(dep, &adev->power_dependent, node)
  884. if (dep->dev == depdev) {
  885. list_del(&dep->node);
  886. kfree(dep);
  887. break;
  888. }
  889. mutex_unlock(&adev->physical_node_lock);
  890. }
  891. EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent);
  892. #endif /* CONFIG_PM */