device_pm.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 D3cold is not supported, 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(target_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(target_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. device->flags.initialized = false;
  234. return -ENXIO;
  235. }
  236. result = acpi_device_get_power(device, &state);
  237. if (result)
  238. return result;
  239. if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
  240. /* Reference count the power resources. */
  241. result = acpi_power_on_resources(device, state);
  242. if (result)
  243. return result;
  244. if (state == ACPI_STATE_D0) {
  245. /*
  246. * If _PSC is not present and the state inferred from
  247. * power resources appears to be D0, it still may be
  248. * necessary to execute _PS0 at this point, because
  249. * another device using the same power resources may
  250. * have been put into D0 previously and that's why we
  251. * see D0 here.
  252. */
  253. result = acpi_dev_pm_explicit_set(device, state);
  254. if (result)
  255. return result;
  256. }
  257. } else if (state == ACPI_STATE_UNKNOWN) {
  258. /*
  259. * No power resources and missing _PSC? Cross fingers and make
  260. * it D0 in hope that this is what the BIOS put the device into.
  261. * [We tried to force D0 here by executing _PS0, but that broke
  262. * Toshiba P870-303 in a nasty way.]
  263. */
  264. state = ACPI_STATE_D0;
  265. }
  266. device->power.state = state;
  267. return 0;
  268. }
  269. /**
  270. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  271. * @device: Device object whose power state is to be fixed up.
  272. *
  273. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  274. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  275. * not be the case and this function should be used then.
  276. */
  277. int acpi_device_fix_up_power(struct acpi_device *device)
  278. {
  279. int ret = 0;
  280. if (!device->power.flags.power_resources
  281. && !device->power.flags.explicit_get
  282. && device->power.state == ACPI_STATE_D0)
  283. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
  287. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  288. {
  289. int state;
  290. int result;
  291. if (device->power.state == ACPI_STATE_UNKNOWN) {
  292. result = acpi_bus_init_power(device);
  293. if (!result && state_p)
  294. *state_p = device->power.state;
  295. return result;
  296. }
  297. result = acpi_device_get_power(device, &state);
  298. if (result)
  299. return result;
  300. if (state == ACPI_STATE_UNKNOWN) {
  301. state = ACPI_STATE_D0;
  302. result = acpi_device_set_power(device, state);
  303. if (result)
  304. return result;
  305. } else {
  306. if (device->power.flags.power_resources) {
  307. /*
  308. * We don't need to really switch the state, bu we need
  309. * to update the power resources' reference counters.
  310. */
  311. result = acpi_power_transition(device, state);
  312. if (result)
  313. return result;
  314. }
  315. device->power.state = state;
  316. }
  317. if (state_p)
  318. *state_p = state;
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  322. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  323. {
  324. struct acpi_device *device;
  325. int result;
  326. result = acpi_bus_get_device(handle, &device);
  327. return result ? result : acpi_device_update_power(device, state_p);
  328. }
  329. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  330. bool acpi_bus_power_manageable(acpi_handle handle)
  331. {
  332. struct acpi_device *device;
  333. int result;
  334. result = acpi_bus_get_device(handle, &device);
  335. return result ? false : device->flags.power_manageable;
  336. }
  337. EXPORT_SYMBOL(acpi_bus_power_manageable);
  338. #ifdef CONFIG_PM
  339. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  340. static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
  341. void acpi_pm_wakeup_event(struct device *dev)
  342. {
  343. pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
  344. }
  345. EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
  346. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  347. {
  348. struct acpi_device *adev;
  349. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  350. return;
  351. acpi_handle_debug(handle, "Wake notify\n");
  352. adev = acpi_bus_get_acpi_device(handle);
  353. if (!adev)
  354. return;
  355. mutex_lock(&acpi_pm_notifier_lock);
  356. if (adev->wakeup.flags.notifier_present) {
  357. pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
  358. if (adev->wakeup.context.func) {
  359. acpi_handle_debug(handle, "Running %pF for %s\n",
  360. adev->wakeup.context.func,
  361. dev_name(adev->wakeup.context.dev));
  362. adev->wakeup.context.func(&adev->wakeup.context);
  363. }
  364. }
  365. mutex_unlock(&acpi_pm_notifier_lock);
  366. acpi_bus_put_acpi_device(adev);
  367. }
  368. /**
  369. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  370. * @adev: ACPI device to add the notify handler for.
  371. * @dev: Device to generate a wakeup event for while handling the notification.
  372. * @func: Work function to execute when handling the notification.
  373. *
  374. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  375. * PM wakeup events. For example, wakeup events may be generated for bridges
  376. * if one of the devices below the bridge is signaling wakeup, even if the
  377. * bridge itself doesn't have a wakeup GPE associated with it.
  378. */
  379. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  380. void (*func)(struct acpi_device_wakeup_context *context))
  381. {
  382. acpi_status status = AE_ALREADY_EXISTS;
  383. if (!dev && !func)
  384. return AE_BAD_PARAMETER;
  385. mutex_lock(&acpi_pm_notifier_install_lock);
  386. if (adev->wakeup.flags.notifier_present)
  387. goto out;
  388. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  389. acpi_pm_notify_handler, NULL);
  390. if (ACPI_FAILURE(status))
  391. goto out;
  392. mutex_lock(&acpi_pm_notifier_lock);
  393. adev->wakeup.ws = wakeup_source_register(&adev->dev,
  394. dev_name(&adev->dev));
  395. adev->wakeup.context.dev = dev;
  396. adev->wakeup.context.func = func;
  397. adev->wakeup.flags.notifier_present = true;
  398. mutex_unlock(&acpi_pm_notifier_lock);
  399. out:
  400. mutex_unlock(&acpi_pm_notifier_install_lock);
  401. return status;
  402. }
  403. /**
  404. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  405. * @adev: ACPI device to remove the notifier from.
  406. */
  407. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  408. {
  409. acpi_status status = AE_BAD_PARAMETER;
  410. mutex_lock(&acpi_pm_notifier_install_lock);
  411. if (!adev->wakeup.flags.notifier_present)
  412. goto out;
  413. status = acpi_remove_notify_handler(adev->handle,
  414. ACPI_SYSTEM_NOTIFY,
  415. acpi_pm_notify_handler);
  416. if (ACPI_FAILURE(status))
  417. goto out;
  418. mutex_lock(&acpi_pm_notifier_lock);
  419. adev->wakeup.context.func = NULL;
  420. adev->wakeup.context.dev = NULL;
  421. wakeup_source_unregister(adev->wakeup.ws);
  422. adev->wakeup.flags.notifier_present = false;
  423. mutex_unlock(&acpi_pm_notifier_lock);
  424. out:
  425. mutex_unlock(&acpi_pm_notifier_install_lock);
  426. return status;
  427. }
  428. bool acpi_bus_can_wakeup(acpi_handle handle)
  429. {
  430. struct acpi_device *device;
  431. int result;
  432. result = acpi_bus_get_device(handle, &device);
  433. return result ? false : device->wakeup.flags.valid;
  434. }
  435. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  436. bool acpi_pm_device_can_wakeup(struct device *dev)
  437. {
  438. struct acpi_device *adev = ACPI_COMPANION(dev);
  439. return adev ? acpi_device_can_wakeup(adev) : false;
  440. }
  441. /**
  442. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  443. * @dev: Device whose preferred target power state to return.
  444. * @adev: ACPI device node corresponding to @dev.
  445. * @target_state: System state to match the resultant device state.
  446. * @d_min_p: Location to store the highest power state available to the device.
  447. * @d_max_p: Location to store the lowest power state available to the device.
  448. *
  449. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  450. * device power states that the device can be in while the system is in the
  451. * state represented by @target_state. Store the integer numbers representing
  452. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  453. * respectively.
  454. *
  455. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  456. * actually corresponds to @dev before using this function.
  457. *
  458. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  459. * returns a value that doesn't make sense. The memory locations pointed to by
  460. * @d_max_p and @d_min_p are only modified on success.
  461. */
  462. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  463. u32 target_state, int *d_min_p, int *d_max_p)
  464. {
  465. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  466. acpi_handle handle = adev->handle;
  467. unsigned long long ret;
  468. int d_min, d_max;
  469. bool wakeup = false;
  470. acpi_status status;
  471. /*
  472. * If the system state is S0, the lowest power state the device can be
  473. * in is D3cold, unless the device has _S0W and is supposed to signal
  474. * wakeup, in which case the return value of _S0W has to be used as the
  475. * lowest power state available to the device.
  476. */
  477. d_min = ACPI_STATE_D0;
  478. d_max = ACPI_STATE_D3_COLD;
  479. /*
  480. * If present, _SxD methods return the minimum D-state (highest power
  481. * state) we can use for the corresponding S-states. Otherwise, the
  482. * minimum D-state is D0 (ACPI 3.x).
  483. */
  484. if (target_state > ACPI_STATE_S0) {
  485. /*
  486. * We rely on acpi_evaluate_integer() not clobbering the integer
  487. * provided if AE_NOT_FOUND is returned.
  488. */
  489. ret = d_min;
  490. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  491. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  492. || ret > ACPI_STATE_D3_COLD)
  493. return -ENODATA;
  494. /*
  495. * We need to handle legacy systems where D3hot and D3cold are
  496. * the same and 3 is returned in both cases, so fall back to
  497. * D3cold if D3hot is not a valid state.
  498. */
  499. if (!adev->power.states[ret].flags.valid) {
  500. if (ret == ACPI_STATE_D3_HOT)
  501. ret = ACPI_STATE_D3_COLD;
  502. else
  503. return -ENODATA;
  504. }
  505. d_min = ret;
  506. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  507. && adev->wakeup.sleep_state >= target_state;
  508. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  509. PM_QOS_FLAGS_NONE) {
  510. wakeup = adev->wakeup.flags.valid;
  511. }
  512. /*
  513. * If _PRW says we can wake up the system from the target sleep state,
  514. * the D-state returned by _SxD is sufficient for that (we assume a
  515. * wakeup-aware driver if wake is set). Still, if _SxW exists
  516. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  517. * can wake the system. _S0W may be valid, too.
  518. */
  519. if (wakeup) {
  520. method[3] = 'W';
  521. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  522. if (status == AE_NOT_FOUND) {
  523. if (target_state > ACPI_STATE_S0)
  524. d_max = d_min;
  525. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  526. /* Fall back to D3cold if ret is not a valid state. */
  527. if (!adev->power.states[ret].flags.valid)
  528. ret = ACPI_STATE_D3_COLD;
  529. d_max = ret > d_min ? ret : d_min;
  530. } else {
  531. return -ENODATA;
  532. }
  533. }
  534. if (d_min_p)
  535. *d_min_p = d_min;
  536. if (d_max_p)
  537. *d_max_p = d_max;
  538. return 0;
  539. }
  540. /**
  541. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  542. * @dev: Device whose preferred target power state to return.
  543. * @d_min_p: Location to store the upper limit of the allowed states range.
  544. * @d_max_in: Deepest low-power state to take into consideration.
  545. * Return value: Preferred power state of the device on success, -ENODEV
  546. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  547. * incorrect, or -ENODATA on ACPI method failure.
  548. *
  549. * The caller must ensure that @dev is valid before using this function.
  550. */
  551. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  552. {
  553. struct acpi_device *adev;
  554. int ret, d_min, d_max;
  555. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  556. return -EINVAL;
  557. if (d_max_in > ACPI_STATE_D2) {
  558. enum pm_qos_flags_status stat;
  559. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  560. if (stat == PM_QOS_FLAGS_ALL)
  561. d_max_in = ACPI_STATE_D2;
  562. }
  563. adev = ACPI_COMPANION(dev);
  564. if (!adev) {
  565. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  566. return -ENODEV;
  567. }
  568. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  569. &d_min, &d_max);
  570. if (ret)
  571. return ret;
  572. if (d_max_in < d_min)
  573. return -EINVAL;
  574. if (d_max > d_max_in) {
  575. for (d_max = d_max_in; d_max > d_min; d_max--) {
  576. if (adev->power.states[d_max].flags.valid)
  577. break;
  578. }
  579. }
  580. if (d_min_p)
  581. *d_min_p = d_min;
  582. return d_max;
  583. }
  584. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  585. /**
  586. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  587. * @context: Device wakeup context.
  588. */
  589. static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
  590. {
  591. struct device *dev = context->dev;
  592. if (dev) {
  593. pm_wakeup_event(dev, 0);
  594. pm_request_resume(dev);
  595. }
  596. }
  597. static DEFINE_MUTEX(acpi_wakeup_lock);
  598. static int __acpi_device_wakeup_enable(struct acpi_device *adev,
  599. u32 target_state)
  600. {
  601. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  602. acpi_status status;
  603. int error = 0;
  604. mutex_lock(&acpi_wakeup_lock);
  605. if (wakeup->enable_count >= INT_MAX) {
  606. acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
  607. goto out;
  608. }
  609. if (wakeup->enable_count > 0)
  610. goto inc;
  611. error = acpi_enable_wakeup_device_power(adev, target_state);
  612. if (error)
  613. goto out;
  614. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  615. if (ACPI_FAILURE(status)) {
  616. acpi_disable_wakeup_device_power(adev);
  617. error = -EIO;
  618. goto out;
  619. }
  620. inc:
  621. wakeup->enable_count++;
  622. out:
  623. mutex_unlock(&acpi_wakeup_lock);
  624. return error;
  625. }
  626. /**
  627. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  628. * @adev: ACPI device to enable wakeup functionality for.
  629. * @target_state: State the system is transitioning into.
  630. *
  631. * Enable the GPE associated with @adev so that it can generate wakeup signals
  632. * for the device in response to external (remote) events and enable wakeup
  633. * power for it.
  634. *
  635. * Callers must ensure that @adev is a valid ACPI device node before executing
  636. * this function.
  637. */
  638. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  639. {
  640. return __acpi_device_wakeup_enable(adev, target_state);
  641. }
  642. /**
  643. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  644. * @adev: ACPI device to disable wakeup functionality for.
  645. *
  646. * Disable the GPE associated with @adev and disable wakeup power for it.
  647. *
  648. * Callers must ensure that @adev is a valid ACPI device node before executing
  649. * this function.
  650. */
  651. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  652. {
  653. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  654. mutex_lock(&acpi_wakeup_lock);
  655. if (!wakeup->enable_count)
  656. goto out;
  657. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  658. acpi_disable_wakeup_device_power(adev);
  659. wakeup->enable_count--;
  660. out:
  661. mutex_unlock(&acpi_wakeup_lock);
  662. }
  663. /**
  664. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  665. * @dev: Device to enable/disable to generate wakeup events.
  666. * @enable: Whether to enable or disable the wakeup functionality.
  667. */
  668. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  669. {
  670. struct acpi_device *adev;
  671. int error;
  672. adev = ACPI_COMPANION(dev);
  673. if (!adev) {
  674. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  675. return -ENODEV;
  676. }
  677. if (!acpi_device_can_wakeup(adev))
  678. return -EINVAL;
  679. if (!enable) {
  680. acpi_device_wakeup_disable(adev);
  681. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  682. return 0;
  683. }
  684. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
  685. if (!error)
  686. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  687. return error;
  688. }
  689. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  690. /**
  691. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  692. * @dev: Device to put into a low-power state.
  693. * @adev: ACPI device node corresponding to @dev.
  694. * @system_state: System state to choose the device state for.
  695. */
  696. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  697. u32 system_state)
  698. {
  699. int ret, state;
  700. if (!acpi_device_power_manageable(adev))
  701. return 0;
  702. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  703. return ret ? ret : acpi_device_set_power(adev, state);
  704. }
  705. /**
  706. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  707. * @adev: ACPI device node to put into the full-power state.
  708. */
  709. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  710. {
  711. return acpi_device_power_manageable(adev) ?
  712. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  713. }
  714. /**
  715. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  716. * @dev: Device to put into a low-power state.
  717. *
  718. * Put the given device into a runtime low-power state using the standard ACPI
  719. * mechanism. Set up remote wakeup if desired, choose the state to put the
  720. * device into (this checks if remote wakeup is expected to work too), and set
  721. * the power state of the device.
  722. */
  723. int acpi_dev_runtime_suspend(struct device *dev)
  724. {
  725. struct acpi_device *adev = ACPI_COMPANION(dev);
  726. bool remote_wakeup;
  727. int error;
  728. if (!adev)
  729. return 0;
  730. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  731. PM_QOS_FLAGS_NONE;
  732. if (remote_wakeup) {
  733. error = acpi_device_wakeup_enable(adev, ACPI_STATE_S0);
  734. if (error)
  735. return -EAGAIN;
  736. }
  737. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  738. if (error && remote_wakeup)
  739. acpi_device_wakeup_disable(adev);
  740. return error;
  741. }
  742. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  743. /**
  744. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  745. * @dev: Device to put into the full-power state.
  746. *
  747. * Put the given device into the full-power state using the standard ACPI
  748. * mechanism at run time. Set the power state of the device to ACPI D0 and
  749. * disable remote wakeup.
  750. */
  751. int acpi_dev_runtime_resume(struct device *dev)
  752. {
  753. struct acpi_device *adev = ACPI_COMPANION(dev);
  754. int error;
  755. if (!adev)
  756. return 0;
  757. error = acpi_dev_pm_full_power(adev);
  758. acpi_device_wakeup_disable(adev);
  759. return error;
  760. }
  761. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  762. /**
  763. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  764. * @dev: Device to suspend.
  765. *
  766. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  767. * it into a runtime low-power state.
  768. */
  769. int acpi_subsys_runtime_suspend(struct device *dev)
  770. {
  771. int ret = pm_generic_runtime_suspend(dev);
  772. return ret ? ret : acpi_dev_runtime_suspend(dev);
  773. }
  774. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  775. /**
  776. * acpi_subsys_runtime_resume - Resume device using ACPI.
  777. * @dev: Device to Resume.
  778. *
  779. * Use ACPI to put the given device into the full-power state and carry out the
  780. * generic runtime resume procedure for it.
  781. */
  782. int acpi_subsys_runtime_resume(struct device *dev)
  783. {
  784. int ret = acpi_dev_runtime_resume(dev);
  785. return ret ? ret : pm_generic_runtime_resume(dev);
  786. }
  787. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  788. #ifdef CONFIG_PM_SLEEP
  789. /**
  790. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  791. * @dev: Device to put into a low-power state.
  792. *
  793. * Put the given device into a low-power state during system transition to a
  794. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  795. * desired, choose the state to put the device into (this checks if system
  796. * wakeup is expected to work too), and set the power state of the device.
  797. */
  798. int acpi_dev_suspend_late(struct device *dev)
  799. {
  800. struct acpi_device *adev = ACPI_COMPANION(dev);
  801. u32 target_state;
  802. bool wakeup;
  803. int error;
  804. if (!adev)
  805. return 0;
  806. target_state = acpi_target_system_state();
  807. wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
  808. if (wakeup) {
  809. error = acpi_device_wakeup_enable(adev, target_state);
  810. if (error)
  811. return error;
  812. }
  813. error = acpi_dev_pm_low_power(dev, adev, target_state);
  814. if (error && wakeup)
  815. acpi_device_wakeup_disable(adev);
  816. return error;
  817. }
  818. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  819. /**
  820. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  821. * @dev: Device to put into the full-power state.
  822. *
  823. * Put the given device into the full-power state using the standard ACPI
  824. * mechanism during system transition to the working state. Set the power
  825. * state of the device to ACPI D0 and disable remote wakeup.
  826. */
  827. int acpi_dev_resume_early(struct device *dev)
  828. {
  829. struct acpi_device *adev = ACPI_COMPANION(dev);
  830. int error;
  831. if (!adev)
  832. return 0;
  833. error = acpi_dev_pm_full_power(adev);
  834. acpi_device_wakeup_disable(adev);
  835. return error;
  836. }
  837. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  838. /**
  839. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  840. * @dev: Device to prepare.
  841. */
  842. int acpi_subsys_prepare(struct device *dev)
  843. {
  844. struct acpi_device *adev = ACPI_COMPANION(dev);
  845. u32 sys_target;
  846. int ret, state;
  847. ret = pm_generic_prepare(dev);
  848. if (ret < 0)
  849. return ret;
  850. if (!adev || !pm_runtime_suspended(dev)
  851. || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  852. return 0;
  853. sys_target = acpi_target_system_state();
  854. if (sys_target == ACPI_STATE_S0)
  855. return 1;
  856. if (adev->power.flags.dsw_present)
  857. return 0;
  858. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  859. return !ret && state == adev->power.state;
  860. }
  861. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  862. /**
  863. * acpi_subsys_suspend - Run the device driver's suspend callback.
  864. * @dev: Device to handle.
  865. *
  866. * Follow PCI and resume devices suspended at run time before running their
  867. * system suspend callbacks.
  868. */
  869. int acpi_subsys_suspend(struct device *dev)
  870. {
  871. pm_runtime_resume(dev);
  872. return pm_generic_suspend(dev);
  873. }
  874. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  875. /**
  876. * acpi_subsys_suspend_late - Suspend device using ACPI.
  877. * @dev: Device to suspend.
  878. *
  879. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  880. * it into a low-power state during system transition into a sleep state.
  881. */
  882. int acpi_subsys_suspend_late(struct device *dev)
  883. {
  884. int ret = pm_generic_suspend_late(dev);
  885. return ret ? ret : acpi_dev_suspend_late(dev);
  886. }
  887. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  888. /**
  889. * acpi_subsys_resume_early - Resume device using ACPI.
  890. * @dev: Device to Resume.
  891. *
  892. * Use ACPI to put the given device into the full-power state and carry out the
  893. * generic early resume procedure for it during system transition into the
  894. * working state.
  895. */
  896. int acpi_subsys_resume_early(struct device *dev)
  897. {
  898. int ret = acpi_dev_resume_early(dev);
  899. return ret ? ret : pm_generic_resume_early(dev);
  900. }
  901. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  902. /**
  903. * acpi_subsys_freeze - Run the device driver's freeze callback.
  904. * @dev: Device to handle.
  905. */
  906. int acpi_subsys_freeze(struct device *dev)
  907. {
  908. /*
  909. * This used to be done in acpi_subsys_prepare() for all devices and
  910. * some drivers may depend on it, so do it here. Ideally, however,
  911. * runtime-suspended devices should not be touched during freeze/thaw
  912. * transitions.
  913. */
  914. pm_runtime_resume(dev);
  915. return pm_generic_freeze(dev);
  916. }
  917. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  918. #endif /* CONFIG_PM_SLEEP */
  919. static struct dev_pm_domain acpi_general_pm_domain = {
  920. .ops = {
  921. .runtime_suspend = acpi_subsys_runtime_suspend,
  922. .runtime_resume = acpi_subsys_runtime_resume,
  923. #ifdef CONFIG_PM_SLEEP
  924. .prepare = acpi_subsys_prepare,
  925. .complete = pm_complete_with_resume_check,
  926. .suspend = acpi_subsys_suspend,
  927. .suspend_late = acpi_subsys_suspend_late,
  928. .resume_early = acpi_subsys_resume_early,
  929. .freeze = acpi_subsys_freeze,
  930. .poweroff = acpi_subsys_suspend,
  931. .poweroff_late = acpi_subsys_suspend_late,
  932. .restore_early = acpi_subsys_resume_early,
  933. #endif
  934. },
  935. };
  936. /**
  937. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  938. * @dev: Device to take care of.
  939. * @power_off: Whether or not to try to remove power from the device.
  940. *
  941. * Remove the device from the general ACPI PM domain and remove its wakeup
  942. * notifier. If @power_off is set, additionally remove power from the device if
  943. * possible.
  944. *
  945. * Callers must ensure proper synchronization of this function with power
  946. * management callbacks.
  947. */
  948. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  949. {
  950. struct acpi_device *adev = ACPI_COMPANION(dev);
  951. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  952. dev_pm_domain_set(dev, NULL);
  953. acpi_remove_pm_notifier(adev);
  954. if (power_off) {
  955. /*
  956. * If the device's PM QoS resume latency limit or flags
  957. * have been exposed to user space, they have to be
  958. * hidden at this point, so that they don't affect the
  959. * choice of the low-power state to put the device into.
  960. */
  961. dev_pm_qos_hide_latency_limit(dev);
  962. dev_pm_qos_hide_flags(dev);
  963. acpi_device_wakeup_disable(adev);
  964. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  965. }
  966. }
  967. }
  968. /**
  969. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  970. * @dev: Device to prepare.
  971. * @power_on: Whether or not to power on the device.
  972. *
  973. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  974. * attached to it, install a wakeup notification handler for the device and
  975. * add it to the general ACPI PM domain. If @power_on is set, the device will
  976. * be put into the ACPI D0 state before the function returns.
  977. *
  978. * This assumes that the @dev's bus type uses generic power management callbacks
  979. * (or doesn't use any power management callbacks at all).
  980. *
  981. * Callers must ensure proper synchronization of this function with power
  982. * management callbacks.
  983. */
  984. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  985. {
  986. /*
  987. * Skip devices whose ACPI companions match the device IDs below,
  988. * because they require special power management handling incompatible
  989. * with the generic ACPI PM domain.
  990. */
  991. static const struct acpi_device_id special_pm_ids[] = {
  992. {"PNP0C0B", }, /* Generic ACPI fan */
  993. {"INT3404", }, /* Fan */
  994. {}
  995. };
  996. struct acpi_device *adev = ACPI_COMPANION(dev);
  997. if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
  998. return -ENODEV;
  999. if (dev->pm_domain)
  1000. return -EEXIST;
  1001. /*
  1002. * Only attach the power domain to the first device if the
  1003. * companion is shared by multiple. This is to prevent doing power
  1004. * management twice.
  1005. */
  1006. if (!acpi_device_is_first_physical_node(adev, dev))
  1007. return -EBUSY;
  1008. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1009. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1010. if (power_on) {
  1011. acpi_dev_pm_full_power(adev);
  1012. acpi_device_wakeup_disable(adev);
  1013. }
  1014. dev->pm_domain->detach = acpi_dev_pm_detach;
  1015. return 0;
  1016. }
  1017. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1018. #endif /* CONFIG_PM */