device_pm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * drivers/acpi/device_pm.c - ACPI device power management routines.
  3. *
  4. * Copyright (C) 2012, Intel Corp.
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <linux/acpi.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm_qos.h>
  24. #include <linux/pm_runtime.h>
  25. #include "internal.h"
  26. #define _COMPONENT ACPI_POWER_COMPONENT
  27. ACPI_MODULE_NAME("device_pm");
  28. /**
  29. * acpi_power_state_string - String representation of ACPI device power state.
  30. * @state: ACPI device power state to return the string representation of.
  31. */
  32. const char *acpi_power_state_string(int state)
  33. {
  34. switch (state) {
  35. case ACPI_STATE_D0:
  36. return "D0";
  37. case ACPI_STATE_D1:
  38. return "D1";
  39. case ACPI_STATE_D2:
  40. return "D2";
  41. case ACPI_STATE_D3_HOT:
  42. return "D3hot";
  43. case ACPI_STATE_D3_COLD:
  44. return "D3cold";
  45. default:
  46. return "(unknown)";
  47. }
  48. }
  49. /**
  50. * acpi_device_get_power - Get power state of an ACPI device.
  51. * @device: Device to get the power state of.
  52. * @state: Place to store the power state of the device.
  53. *
  54. * This function does not update the device's power.state field, but it may
  55. * update its parent's power.state field (when the parent's power state is
  56. * unknown and the device's power state turns out to be D0).
  57. */
  58. int acpi_device_get_power(struct acpi_device *device, int *state)
  59. {
  60. int result = ACPI_STATE_UNKNOWN;
  61. if (!device || !state)
  62. return -EINVAL;
  63. if (!device->flags.power_manageable) {
  64. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  65. *state = device->parent ?
  66. device->parent->power.state : ACPI_STATE_D0;
  67. goto out;
  68. }
  69. /*
  70. * Get the device's power state from power resources settings and _PSC,
  71. * if available.
  72. */
  73. if (device->power.flags.power_resources) {
  74. int error = acpi_power_get_inferred_state(device, &result);
  75. if (error)
  76. return error;
  77. }
  78. if (device->power.flags.explicit_get) {
  79. acpi_handle handle = device->handle;
  80. unsigned long long psc;
  81. acpi_status status;
  82. status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
  83. if (ACPI_FAILURE(status))
  84. return -ENODEV;
  85. /*
  86. * The power resources settings may indicate a power state
  87. * shallower than the actual power state of the device, because
  88. * the same power resources may be referenced by other devices.
  89. *
  90. * For systems predating ACPI 4.0 we assume that D3hot is the
  91. * deepest state that can be supported.
  92. */
  93. if (psc > result && psc < ACPI_STATE_D3_COLD)
  94. result = psc;
  95. else if (result == ACPI_STATE_UNKNOWN)
  96. result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
  97. }
  98. /*
  99. * If we were unsure about the device parent's power state up to this
  100. * point, the fact that the device is in D0 implies that the parent has
  101. * to be in D0 too, except if ignore_parent is set.
  102. */
  103. if (!device->power.flags.ignore_parent && device->parent
  104. && device->parent->power.state == ACPI_STATE_UNKNOWN
  105. && result == ACPI_STATE_D0)
  106. device->parent->power.state = ACPI_STATE_D0;
  107. *state = result;
  108. out:
  109. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  110. device->pnp.bus_id, acpi_power_state_string(*state)));
  111. return 0;
  112. }
  113. static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
  114. {
  115. if (adev->power.states[state].flags.explicit_set) {
  116. char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
  117. acpi_status status;
  118. status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
  119. if (ACPI_FAILURE(status))
  120. return -ENODEV;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * acpi_device_set_power - Set power state of an ACPI device.
  126. * @device: Device to set the power state of.
  127. * @state: New power state to set.
  128. *
  129. * Callers must ensure that the device is power manageable before using this
  130. * function.
  131. */
  132. int acpi_device_set_power(struct acpi_device *device, int state)
  133. {
  134. int target_state = state;
  135. int result = 0;
  136. if (!device || !device->flags.power_manageable
  137. || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  138. return -EINVAL;
  139. /* Make sure this is a valid target state */
  140. if (state == device->power.state) {
  141. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
  142. device->pnp.bus_id,
  143. acpi_power_state_string(state)));
  144. return 0;
  145. }
  146. if (state == ACPI_STATE_D3_COLD) {
  147. /*
  148. * For transitions to D3cold we need to execute _PS3 and then
  149. * possibly drop references to the power resources in use.
  150. */
  151. state = ACPI_STATE_D3_HOT;
  152. /* If _PR3 is not available, use D3hot as the target state. */
  153. if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
  154. target_state = state;
  155. } else if (!device->power.states[state].flags.valid) {
  156. dev_warn(&device->dev, "Power state %s not supported\n",
  157. acpi_power_state_string(state));
  158. return -ENODEV;
  159. }
  160. if (!device->power.flags.ignore_parent &&
  161. device->parent && (state < device->parent->power.state)) {
  162. dev_warn(&device->dev,
  163. "Cannot transition to power state %s for parent in %s\n",
  164. acpi_power_state_string(state),
  165. acpi_power_state_string(device->parent->power.state));
  166. return -ENODEV;
  167. }
  168. /*
  169. * Transition Power
  170. * ----------------
  171. * In accordance with ACPI 6, _PSx is executed before manipulating power
  172. * resources, unless the target state is D0, in which case _PS0 is
  173. * supposed to be executed after turning the power resources on.
  174. */
  175. if (state > ACPI_STATE_D0) {
  176. /*
  177. * According to ACPI 6, devices cannot go from lower-power
  178. * (deeper) states to higher-power (shallower) states.
  179. */
  180. if (state < device->power.state) {
  181. dev_warn(&device->dev, "Cannot transition from %s to %s\n",
  182. acpi_power_state_string(device->power.state),
  183. acpi_power_state_string(state));
  184. return -ENODEV;
  185. }
  186. result = acpi_dev_pm_explicit_set(device, state);
  187. if (result)
  188. goto end;
  189. if (device->power.flags.power_resources)
  190. result = acpi_power_transition(device, target_state);
  191. } else {
  192. if (device->power.flags.power_resources) {
  193. result = acpi_power_transition(device, ACPI_STATE_D0);
  194. if (result)
  195. goto end;
  196. }
  197. result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  198. }
  199. end:
  200. if (result) {
  201. dev_warn(&device->dev, "Failed to change power state to %s\n",
  202. acpi_power_state_string(state));
  203. } else {
  204. device->power.state = target_state;
  205. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  206. "Device [%s] transitioned to %s\n",
  207. device->pnp.bus_id,
  208. acpi_power_state_string(state)));
  209. }
  210. return result;
  211. }
  212. EXPORT_SYMBOL(acpi_device_set_power);
  213. int acpi_bus_set_power(acpi_handle handle, int state)
  214. {
  215. struct acpi_device *device;
  216. int result;
  217. result = acpi_bus_get_device(handle, &device);
  218. if (result)
  219. return result;
  220. return acpi_device_set_power(device, state);
  221. }
  222. EXPORT_SYMBOL(acpi_bus_set_power);
  223. int acpi_bus_init_power(struct acpi_device *device)
  224. {
  225. int state;
  226. int result;
  227. if (!device)
  228. return -EINVAL;
  229. device->power.state = ACPI_STATE_UNKNOWN;
  230. if (!acpi_device_is_present(device))
  231. return -ENXIO;
  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. /* Reference count the power resources. */
  237. result = acpi_power_on_resources(device, state);
  238. if (result)
  239. return result;
  240. if (state == ACPI_STATE_D0) {
  241. /*
  242. * If _PSC is not present and the state inferred from
  243. * power resources appears to be D0, it still may be
  244. * necessary to execute _PS0 at this point, because
  245. * another device using the same power resources may
  246. * have been put into D0 previously and that's why we
  247. * see D0 here.
  248. */
  249. result = acpi_dev_pm_explicit_set(device, state);
  250. if (result)
  251. return result;
  252. }
  253. } else if (state == ACPI_STATE_UNKNOWN) {
  254. /*
  255. * No power resources and missing _PSC? Cross fingers and make
  256. * it D0 in hope that this is what the BIOS put the device into.
  257. * [We tried to force D0 here by executing _PS0, but that broke
  258. * Toshiba P870-303 in a nasty way.]
  259. */
  260. state = ACPI_STATE_D0;
  261. }
  262. device->power.state = state;
  263. return 0;
  264. }
  265. /**
  266. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  267. * @device: Device object whose power state is to be fixed up.
  268. *
  269. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  270. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  271. * not be the case and this function should be used then.
  272. */
  273. int acpi_device_fix_up_power(struct acpi_device *device)
  274. {
  275. int ret = 0;
  276. if (!device->power.flags.power_resources
  277. && !device->power.flags.explicit_get
  278. && device->power.state == ACPI_STATE_D0)
  279. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  280. return ret;
  281. }
  282. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  283. {
  284. int state;
  285. int result;
  286. if (device->power.state == ACPI_STATE_UNKNOWN) {
  287. result = acpi_bus_init_power(device);
  288. if (!result && state_p)
  289. *state_p = device->power.state;
  290. return result;
  291. }
  292. result = acpi_device_get_power(device, &state);
  293. if (result)
  294. return result;
  295. if (state == ACPI_STATE_UNKNOWN) {
  296. state = ACPI_STATE_D0;
  297. result = acpi_device_set_power(device, state);
  298. if (result)
  299. return result;
  300. } else {
  301. if (device->power.flags.power_resources) {
  302. /*
  303. * We don't need to really switch the state, bu we need
  304. * to update the power resources' reference counters.
  305. */
  306. result = acpi_power_transition(device, state);
  307. if (result)
  308. return result;
  309. }
  310. device->power.state = state;
  311. }
  312. if (state_p)
  313. *state_p = state;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  317. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  318. {
  319. struct acpi_device *device;
  320. int result;
  321. result = acpi_bus_get_device(handle, &device);
  322. return result ? result : acpi_device_update_power(device, state_p);
  323. }
  324. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  325. bool acpi_bus_power_manageable(acpi_handle handle)
  326. {
  327. struct acpi_device *device;
  328. int result;
  329. result = acpi_bus_get_device(handle, &device);
  330. return result ? false : device->flags.power_manageable;
  331. }
  332. EXPORT_SYMBOL(acpi_bus_power_manageable);
  333. #ifdef CONFIG_PM
  334. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  335. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  336. {
  337. struct acpi_device *adev;
  338. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  339. return;
  340. adev = acpi_bus_get_acpi_device(handle);
  341. if (!adev)
  342. return;
  343. mutex_lock(&acpi_pm_notifier_lock);
  344. if (adev->wakeup.flags.notifier_present) {
  345. __pm_wakeup_event(adev->wakeup.ws, 0);
  346. if (adev->wakeup.context.work.func)
  347. queue_pm_work(&adev->wakeup.context.work);
  348. }
  349. mutex_unlock(&acpi_pm_notifier_lock);
  350. acpi_bus_put_acpi_device(adev);
  351. }
  352. /**
  353. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  354. * @adev: ACPI device to add the notify handler for.
  355. * @dev: Device to generate a wakeup event for while handling the notification.
  356. * @work_func: Work function to execute when handling the notification.
  357. *
  358. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  359. * PM wakeup events. For example, wakeup events may be generated for bridges
  360. * if one of the devices below the bridge is signaling wakeup, even if the
  361. * bridge itself doesn't have a wakeup GPE associated with it.
  362. */
  363. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  364. void (*work_func)(struct work_struct *work))
  365. {
  366. acpi_status status = AE_ALREADY_EXISTS;
  367. if (!dev && !work_func)
  368. return AE_BAD_PARAMETER;
  369. mutex_lock(&acpi_pm_notifier_lock);
  370. if (adev->wakeup.flags.notifier_present)
  371. goto out;
  372. adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
  373. adev->wakeup.context.dev = dev;
  374. if (work_func)
  375. INIT_WORK(&adev->wakeup.context.work, work_func);
  376. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  377. acpi_pm_notify_handler, NULL);
  378. if (ACPI_FAILURE(status))
  379. goto out;
  380. adev->wakeup.flags.notifier_present = true;
  381. out:
  382. mutex_unlock(&acpi_pm_notifier_lock);
  383. return status;
  384. }
  385. /**
  386. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  387. * @adev: ACPI device to remove the notifier from.
  388. */
  389. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  390. {
  391. acpi_status status = AE_BAD_PARAMETER;
  392. mutex_lock(&acpi_pm_notifier_lock);
  393. if (!adev->wakeup.flags.notifier_present)
  394. goto out;
  395. status = acpi_remove_notify_handler(adev->handle,
  396. ACPI_SYSTEM_NOTIFY,
  397. acpi_pm_notify_handler);
  398. if (ACPI_FAILURE(status))
  399. goto out;
  400. if (adev->wakeup.context.work.func) {
  401. cancel_work_sync(&adev->wakeup.context.work);
  402. adev->wakeup.context.work.func = NULL;
  403. }
  404. adev->wakeup.context.dev = NULL;
  405. wakeup_source_unregister(adev->wakeup.ws);
  406. adev->wakeup.flags.notifier_present = false;
  407. out:
  408. mutex_unlock(&acpi_pm_notifier_lock);
  409. return status;
  410. }
  411. bool acpi_bus_can_wakeup(acpi_handle handle)
  412. {
  413. struct acpi_device *device;
  414. int result;
  415. result = acpi_bus_get_device(handle, &device);
  416. return result ? false : device->wakeup.flags.valid;
  417. }
  418. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  419. /**
  420. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  421. * @dev: Device whose preferred target power state to return.
  422. * @adev: ACPI device node corresponding to @dev.
  423. * @target_state: System state to match the resultant device state.
  424. * @d_min_p: Location to store the highest power state available to the device.
  425. * @d_max_p: Location to store the lowest power state available to the device.
  426. *
  427. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  428. * device power states that the device can be in while the system is in the
  429. * state represented by @target_state. Store the integer numbers representing
  430. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  431. * respectively.
  432. *
  433. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  434. * actually corresponds to @dev before using this function.
  435. *
  436. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  437. * returns a value that doesn't make sense. The memory locations pointed to by
  438. * @d_max_p and @d_min_p are only modified on success.
  439. */
  440. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  441. u32 target_state, int *d_min_p, int *d_max_p)
  442. {
  443. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  444. acpi_handle handle = adev->handle;
  445. unsigned long long ret;
  446. int d_min, d_max;
  447. bool wakeup = false;
  448. acpi_status status;
  449. /*
  450. * If the system state is S0, the lowest power state the device can be
  451. * in is D3cold, unless the device has _S0W and is supposed to signal
  452. * wakeup, in which case the return value of _S0W has to be used as the
  453. * lowest power state available to the device.
  454. */
  455. d_min = ACPI_STATE_D0;
  456. d_max = ACPI_STATE_D3_COLD;
  457. /*
  458. * If present, _SxD methods return the minimum D-state (highest power
  459. * state) we can use for the corresponding S-states. Otherwise, the
  460. * minimum D-state is D0 (ACPI 3.x).
  461. */
  462. if (target_state > ACPI_STATE_S0) {
  463. /*
  464. * We rely on acpi_evaluate_integer() not clobbering the integer
  465. * provided if AE_NOT_FOUND is returned.
  466. */
  467. ret = d_min;
  468. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  469. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  470. || ret > ACPI_STATE_D3_COLD)
  471. return -ENODATA;
  472. /*
  473. * We need to handle legacy systems where D3hot and D3cold are
  474. * the same and 3 is returned in both cases, so fall back to
  475. * D3cold if D3hot is not a valid state.
  476. */
  477. if (!adev->power.states[ret].flags.valid) {
  478. if (ret == ACPI_STATE_D3_HOT)
  479. ret = ACPI_STATE_D3_COLD;
  480. else
  481. return -ENODATA;
  482. }
  483. d_min = ret;
  484. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  485. && adev->wakeup.sleep_state >= target_state;
  486. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  487. PM_QOS_FLAGS_NONE) {
  488. wakeup = adev->wakeup.flags.valid;
  489. }
  490. /*
  491. * If _PRW says we can wake up the system from the target sleep state,
  492. * the D-state returned by _SxD is sufficient for that (we assume a
  493. * wakeup-aware driver if wake is set). Still, if _SxW exists
  494. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  495. * can wake the system. _S0W may be valid, too.
  496. */
  497. if (wakeup) {
  498. method[3] = 'W';
  499. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  500. if (status == AE_NOT_FOUND) {
  501. if (target_state > ACPI_STATE_S0)
  502. d_max = d_min;
  503. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  504. /* Fall back to D3cold if ret is not a valid state. */
  505. if (!adev->power.states[ret].flags.valid)
  506. ret = ACPI_STATE_D3_COLD;
  507. d_max = ret > d_min ? ret : d_min;
  508. } else {
  509. return -ENODATA;
  510. }
  511. }
  512. if (d_min_p)
  513. *d_min_p = d_min;
  514. if (d_max_p)
  515. *d_max_p = d_max;
  516. return 0;
  517. }
  518. /**
  519. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  520. * @dev: Device whose preferred target power state to return.
  521. * @d_min_p: Location to store the upper limit of the allowed states range.
  522. * @d_max_in: Deepest low-power state to take into consideration.
  523. * Return value: Preferred power state of the device on success, -ENODEV
  524. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  525. * incorrect, or -ENODATA on ACPI method failure.
  526. *
  527. * The caller must ensure that @dev is valid before using this function.
  528. */
  529. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  530. {
  531. struct acpi_device *adev;
  532. int ret, d_min, d_max;
  533. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  534. return -EINVAL;
  535. if (d_max_in > ACPI_STATE_D2) {
  536. enum pm_qos_flags_status stat;
  537. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  538. if (stat == PM_QOS_FLAGS_ALL)
  539. d_max_in = ACPI_STATE_D2;
  540. }
  541. adev = ACPI_COMPANION(dev);
  542. if (!adev) {
  543. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  544. return -ENODEV;
  545. }
  546. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  547. &d_min, &d_max);
  548. if (ret)
  549. return ret;
  550. if (d_max_in < d_min)
  551. return -EINVAL;
  552. if (d_max > d_max_in) {
  553. for (d_max = d_max_in; d_max > d_min; d_max--) {
  554. if (adev->power.states[d_max].flags.valid)
  555. break;
  556. }
  557. }
  558. if (d_min_p)
  559. *d_min_p = d_min;
  560. return d_max;
  561. }
  562. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  563. /**
  564. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  565. * @work: Work item to handle.
  566. */
  567. static void acpi_pm_notify_work_func(struct work_struct *work)
  568. {
  569. struct device *dev;
  570. dev = container_of(work, struct acpi_device_wakeup_context, work)->dev;
  571. if (dev) {
  572. pm_wakeup_event(dev, 0);
  573. pm_runtime_resume(dev);
  574. }
  575. }
  576. /**
  577. * acpi_device_wakeup - Enable/disable wakeup functionality for device.
  578. * @adev: ACPI device to enable/disable wakeup functionality for.
  579. * @target_state: State the system is transitioning into.
  580. * @enable: Whether to enable or disable the wakeup functionality.
  581. *
  582. * Enable/disable the GPE associated with @adev so that it can generate
  583. * wakeup signals for the device in response to external (remote) events and
  584. * enable/disable device wakeup power.
  585. *
  586. * Callers must ensure that @adev is a valid ACPI device node before executing
  587. * this function.
  588. */
  589. static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state,
  590. bool enable)
  591. {
  592. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  593. if (enable) {
  594. acpi_status res;
  595. int error;
  596. error = acpi_enable_wakeup_device_power(adev, target_state);
  597. if (error)
  598. return error;
  599. if (adev->wakeup.flags.enabled)
  600. return 0;
  601. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  602. if (ACPI_SUCCESS(res)) {
  603. adev->wakeup.flags.enabled = 1;
  604. } else {
  605. acpi_disable_wakeup_device_power(adev);
  606. return -EIO;
  607. }
  608. } else {
  609. if (adev->wakeup.flags.enabled) {
  610. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  611. adev->wakeup.flags.enabled = 0;
  612. }
  613. acpi_disable_wakeup_device_power(adev);
  614. }
  615. return 0;
  616. }
  617. /**
  618. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  619. * @dev: Device to enable/disable the platform to wake up.
  620. * @enable: Whether to enable or disable the wakeup functionality.
  621. */
  622. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  623. {
  624. struct acpi_device *adev;
  625. if (!device_run_wake(phys_dev))
  626. return -EINVAL;
  627. adev = ACPI_COMPANION(phys_dev);
  628. if (!adev) {
  629. dev_dbg(phys_dev, "ACPI companion missing in %s!\n", __func__);
  630. return -ENODEV;
  631. }
  632. return acpi_device_wakeup(adev, ACPI_STATE_S0, enable);
  633. }
  634. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  635. #ifdef CONFIG_PM_SLEEP
  636. /**
  637. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  638. * @dev: Device to enable/desible to wake up the system from sleep states.
  639. * @enable: Whether to enable or disable @dev to wake up the system.
  640. */
  641. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  642. {
  643. struct acpi_device *adev;
  644. int error;
  645. if (!device_can_wakeup(dev))
  646. return -EINVAL;
  647. adev = ACPI_COMPANION(dev);
  648. if (!adev) {
  649. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  650. return -ENODEV;
  651. }
  652. error = acpi_device_wakeup(adev, acpi_target_system_state(), enable);
  653. if (!error)
  654. dev_info(dev, "System wakeup %s by ACPI\n",
  655. enable ? "enabled" : "disabled");
  656. return error;
  657. }
  658. #endif /* CONFIG_PM_SLEEP */
  659. /**
  660. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  661. * @dev: Device to put into a low-power state.
  662. * @adev: ACPI device node corresponding to @dev.
  663. * @system_state: System state to choose the device state for.
  664. */
  665. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  666. u32 system_state)
  667. {
  668. int ret, state;
  669. if (!acpi_device_power_manageable(adev))
  670. return 0;
  671. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  672. return ret ? ret : acpi_device_set_power(adev, state);
  673. }
  674. /**
  675. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  676. * @adev: ACPI device node to put into the full-power state.
  677. */
  678. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  679. {
  680. return acpi_device_power_manageable(adev) ?
  681. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  682. }
  683. /**
  684. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  685. * @dev: Device to put into a low-power state.
  686. *
  687. * Put the given device into a runtime low-power state using the standard ACPI
  688. * mechanism. Set up remote wakeup if desired, choose the state to put the
  689. * device into (this checks if remote wakeup is expected to work too), and set
  690. * the power state of the device.
  691. */
  692. int acpi_dev_runtime_suspend(struct device *dev)
  693. {
  694. struct acpi_device *adev = ACPI_COMPANION(dev);
  695. bool remote_wakeup;
  696. int error;
  697. if (!adev)
  698. return 0;
  699. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  700. PM_QOS_FLAGS_NONE;
  701. error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup);
  702. if (remote_wakeup && error)
  703. return -EAGAIN;
  704. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  705. if (error)
  706. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  707. return error;
  708. }
  709. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  710. /**
  711. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  712. * @dev: Device to put into the full-power state.
  713. *
  714. * Put the given device into the full-power state using the standard ACPI
  715. * mechanism at run time. Set the power state of the device to ACPI D0 and
  716. * disable remote wakeup.
  717. */
  718. int acpi_dev_runtime_resume(struct device *dev)
  719. {
  720. struct acpi_device *adev = ACPI_COMPANION(dev);
  721. int error;
  722. if (!adev)
  723. return 0;
  724. error = acpi_dev_pm_full_power(adev);
  725. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  726. return error;
  727. }
  728. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  729. /**
  730. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  731. * @dev: Device to suspend.
  732. *
  733. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  734. * it into a runtime low-power state.
  735. */
  736. int acpi_subsys_runtime_suspend(struct device *dev)
  737. {
  738. int ret = pm_generic_runtime_suspend(dev);
  739. return ret ? ret : acpi_dev_runtime_suspend(dev);
  740. }
  741. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  742. /**
  743. * acpi_subsys_runtime_resume - Resume device using ACPI.
  744. * @dev: Device to Resume.
  745. *
  746. * Use ACPI to put the given device into the full-power state and carry out the
  747. * generic runtime resume procedure for it.
  748. */
  749. int acpi_subsys_runtime_resume(struct device *dev)
  750. {
  751. int ret = acpi_dev_runtime_resume(dev);
  752. return ret ? ret : pm_generic_runtime_resume(dev);
  753. }
  754. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  755. #ifdef CONFIG_PM_SLEEP
  756. /**
  757. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  758. * @dev: Device to put into a low-power state.
  759. *
  760. * Put the given device into a low-power state during system transition to a
  761. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  762. * desired, choose the state to put the device into (this checks if system
  763. * wakeup is expected to work too), and set the power state of the device.
  764. */
  765. int acpi_dev_suspend_late(struct device *dev)
  766. {
  767. struct acpi_device *adev = ACPI_COMPANION(dev);
  768. u32 target_state;
  769. bool wakeup;
  770. int error;
  771. if (!adev)
  772. return 0;
  773. target_state = acpi_target_system_state();
  774. wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
  775. error = acpi_device_wakeup(adev, target_state, wakeup);
  776. if (wakeup && error)
  777. return error;
  778. error = acpi_dev_pm_low_power(dev, adev, target_state);
  779. if (error)
  780. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  781. return error;
  782. }
  783. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  784. /**
  785. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  786. * @dev: Device to put into the full-power state.
  787. *
  788. * Put the given device into the full-power state using the standard ACPI
  789. * mechanism during system transition to the working state. Set the power
  790. * state of the device to ACPI D0 and disable remote wakeup.
  791. */
  792. int acpi_dev_resume_early(struct device *dev)
  793. {
  794. struct acpi_device *adev = ACPI_COMPANION(dev);
  795. int error;
  796. if (!adev)
  797. return 0;
  798. error = acpi_dev_pm_full_power(adev);
  799. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  800. return error;
  801. }
  802. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  803. /**
  804. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  805. * @dev: Device to prepare.
  806. */
  807. int acpi_subsys_prepare(struct device *dev)
  808. {
  809. struct acpi_device *adev = ACPI_COMPANION(dev);
  810. u32 sys_target;
  811. int ret, state;
  812. ret = pm_generic_prepare(dev);
  813. if (ret < 0)
  814. return ret;
  815. if (!adev || !pm_runtime_suspended(dev)
  816. || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  817. return 0;
  818. sys_target = acpi_target_system_state();
  819. if (sys_target == ACPI_STATE_S0)
  820. return 1;
  821. if (adev->power.flags.dsw_present)
  822. return 0;
  823. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  824. return !ret && state == adev->power.state;
  825. }
  826. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  827. /**
  828. * acpi_subsys_suspend - Run the device driver's suspend callback.
  829. * @dev: Device to handle.
  830. *
  831. * Follow PCI and resume devices suspended at run time before running their
  832. * system suspend callbacks.
  833. */
  834. int acpi_subsys_suspend(struct device *dev)
  835. {
  836. pm_runtime_resume(dev);
  837. return pm_generic_suspend(dev);
  838. }
  839. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  840. /**
  841. * acpi_subsys_suspend_late - Suspend device using ACPI.
  842. * @dev: Device to suspend.
  843. *
  844. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  845. * it into a low-power state during system transition into a sleep state.
  846. */
  847. int acpi_subsys_suspend_late(struct device *dev)
  848. {
  849. int ret = pm_generic_suspend_late(dev);
  850. return ret ? ret : acpi_dev_suspend_late(dev);
  851. }
  852. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  853. /**
  854. * acpi_subsys_resume_early - Resume device using ACPI.
  855. * @dev: Device to Resume.
  856. *
  857. * Use ACPI to put the given device into the full-power state and carry out the
  858. * generic early resume procedure for it during system transition into the
  859. * working state.
  860. */
  861. int acpi_subsys_resume_early(struct device *dev)
  862. {
  863. int ret = acpi_dev_resume_early(dev);
  864. return ret ? ret : pm_generic_resume_early(dev);
  865. }
  866. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  867. /**
  868. * acpi_subsys_freeze - Run the device driver's freeze callback.
  869. * @dev: Device to handle.
  870. */
  871. int acpi_subsys_freeze(struct device *dev)
  872. {
  873. /*
  874. * This used to be done in acpi_subsys_prepare() for all devices and
  875. * some drivers may depend on it, so do it here. Ideally, however,
  876. * runtime-suspended devices should not be touched during freeze/thaw
  877. * transitions.
  878. */
  879. pm_runtime_resume(dev);
  880. return pm_generic_freeze(dev);
  881. }
  882. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  883. #endif /* CONFIG_PM_SLEEP */
  884. static struct dev_pm_domain acpi_general_pm_domain = {
  885. .ops = {
  886. .runtime_suspend = acpi_subsys_runtime_suspend,
  887. .runtime_resume = acpi_subsys_runtime_resume,
  888. #ifdef CONFIG_PM_SLEEP
  889. .prepare = acpi_subsys_prepare,
  890. .complete = pm_complete_with_resume_check,
  891. .suspend = acpi_subsys_suspend,
  892. .suspend_late = acpi_subsys_suspend_late,
  893. .resume_early = acpi_subsys_resume_early,
  894. .freeze = acpi_subsys_freeze,
  895. .poweroff = acpi_subsys_suspend,
  896. .poweroff_late = acpi_subsys_suspend_late,
  897. .restore_early = acpi_subsys_resume_early,
  898. #endif
  899. },
  900. };
  901. /**
  902. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  903. * @dev: Device to take care of.
  904. * @power_off: Whether or not to try to remove power from the device.
  905. *
  906. * Remove the device from the general ACPI PM domain and remove its wakeup
  907. * notifier. If @power_off is set, additionally remove power from the device if
  908. * possible.
  909. *
  910. * Callers must ensure proper synchronization of this function with power
  911. * management callbacks.
  912. */
  913. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  914. {
  915. struct acpi_device *adev = ACPI_COMPANION(dev);
  916. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  917. dev->pm_domain = NULL;
  918. acpi_remove_pm_notifier(adev);
  919. if (power_off) {
  920. /*
  921. * If the device's PM QoS resume latency limit or flags
  922. * have been exposed to user space, they have to be
  923. * hidden at this point, so that they don't affect the
  924. * choice of the low-power state to put the device into.
  925. */
  926. dev_pm_qos_hide_latency_limit(dev);
  927. dev_pm_qos_hide_flags(dev);
  928. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  929. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  930. }
  931. }
  932. }
  933. /**
  934. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  935. * @dev: Device to prepare.
  936. * @power_on: Whether or not to power on the device.
  937. *
  938. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  939. * attached to it, install a wakeup notification handler for the device and
  940. * add it to the general ACPI PM domain. If @power_on is set, the device will
  941. * be put into the ACPI D0 state before the function returns.
  942. *
  943. * This assumes that the @dev's bus type uses generic power management callbacks
  944. * (or doesn't use any power management callbacks at all).
  945. *
  946. * Callers must ensure proper synchronization of this function with power
  947. * management callbacks.
  948. */
  949. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  950. {
  951. struct acpi_device *adev = ACPI_COMPANION(dev);
  952. if (!adev)
  953. return -ENODEV;
  954. if (dev->pm_domain)
  955. return -EEXIST;
  956. /*
  957. * Only attach the power domain to the first device if the
  958. * companion is shared by multiple. This is to prevent doing power
  959. * management twice.
  960. */
  961. if (!acpi_device_is_first_physical_node(adev, dev))
  962. return -EBUSY;
  963. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  964. dev->pm_domain = &acpi_general_pm_domain;
  965. if (power_on) {
  966. acpi_dev_pm_full_power(adev);
  967. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  968. }
  969. dev->pm_domain->detach = acpi_dev_pm_detach;
  970. return 0;
  971. }
  972. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  973. #endif /* CONFIG_PM */