device_pm.c 31 KB

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