device_pm.c 37 KB

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