device_pm.c 29 KB

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