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