device_pm.c 30 KB

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