device_pm.c 30 KB

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