device_pm.c 31 KB

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