power.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/acpi/power.c - ACPI Power Resources management.
  4. *
  5. * Copyright (C) 2001 - 2015 Intel Corp.
  6. * Author: Andy Grover <andrew.grover@intel.com>
  7. * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  8. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  9. */
  10. /*
  11. * ACPI power-managed devices may be controlled in two ways:
  12. * 1. via "Device Specific (D-State) Control"
  13. * 2. via "Power Resource Control".
  14. * The code below deals with ACPI Power Resources control.
  15. *
  16. * An ACPI "power resource object" represents a software controllable power
  17. * plane, clock plane, or other resource depended on by a device.
  18. *
  19. * A device may rely on multiple power resources, and a power resource
  20. * may be shared by multiple devices.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/acpi.h>
  30. #include "sleep.h"
  31. #include "internal.h"
  32. #define _COMPONENT ACPI_POWER_COMPONENT
  33. ACPI_MODULE_NAME("power");
  34. #define ACPI_POWER_CLASS "power_resource"
  35. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  36. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  37. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  38. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  39. struct acpi_power_dependent_device {
  40. struct device *dev;
  41. struct list_head node;
  42. };
  43. struct acpi_power_resource {
  44. struct acpi_device device;
  45. struct list_head list_node;
  46. char *name;
  47. u32 system_level;
  48. u32 order;
  49. unsigned int ref_count;
  50. bool wakeup_enabled;
  51. struct mutex resource_lock;
  52. struct list_head dependents;
  53. };
  54. struct acpi_power_resource_entry {
  55. struct list_head node;
  56. struct acpi_power_resource *resource;
  57. };
  58. static LIST_HEAD(acpi_power_resource_list);
  59. static DEFINE_MUTEX(power_resource_list_lock);
  60. /* --------------------------------------------------------------------------
  61. Power Resource Management
  62. -------------------------------------------------------------------------- */
  63. static inline
  64. struct acpi_power_resource *to_power_resource(struct acpi_device *device)
  65. {
  66. return container_of(device, struct acpi_power_resource, device);
  67. }
  68. static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
  69. {
  70. struct acpi_device *device;
  71. if (acpi_bus_get_device(handle, &device))
  72. return NULL;
  73. return to_power_resource(device);
  74. }
  75. static int acpi_power_resources_list_add(acpi_handle handle,
  76. struct list_head *list)
  77. {
  78. struct acpi_power_resource *resource = acpi_power_get_context(handle);
  79. struct acpi_power_resource_entry *entry;
  80. if (!resource || !list)
  81. return -EINVAL;
  82. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  83. if (!entry)
  84. return -ENOMEM;
  85. entry->resource = resource;
  86. if (!list_empty(list)) {
  87. struct acpi_power_resource_entry *e;
  88. list_for_each_entry(e, list, node)
  89. if (e->resource->order > resource->order) {
  90. list_add_tail(&entry->node, &e->node);
  91. return 0;
  92. }
  93. }
  94. list_add_tail(&entry->node, list);
  95. return 0;
  96. }
  97. void acpi_power_resources_list_free(struct list_head *list)
  98. {
  99. struct acpi_power_resource_entry *entry, *e;
  100. list_for_each_entry_safe(entry, e, list, node) {
  101. list_del(&entry->node);
  102. kfree(entry);
  103. }
  104. }
  105. static bool acpi_power_resource_is_dup(union acpi_object *package,
  106. unsigned int start, unsigned int i)
  107. {
  108. acpi_handle rhandle, dup;
  109. unsigned int j;
  110. /* The caller is expected to check the package element types */
  111. rhandle = package->package.elements[i].reference.handle;
  112. for (j = start; j < i; j++) {
  113. dup = package->package.elements[j].reference.handle;
  114. if (dup == rhandle)
  115. return true;
  116. }
  117. return false;
  118. }
  119. int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
  120. struct list_head *list)
  121. {
  122. unsigned int i;
  123. int err = 0;
  124. for (i = start; i < package->package.count; i++) {
  125. union acpi_object *element = &package->package.elements[i];
  126. acpi_handle rhandle;
  127. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  128. err = -ENODATA;
  129. break;
  130. }
  131. rhandle = element->reference.handle;
  132. if (!rhandle) {
  133. err = -ENODEV;
  134. break;
  135. }
  136. /* Some ACPI tables contain duplicate power resource references */
  137. if (acpi_power_resource_is_dup(package, start, i))
  138. continue;
  139. err = acpi_add_power_resource(rhandle);
  140. if (err)
  141. break;
  142. err = acpi_power_resources_list_add(rhandle, list);
  143. if (err)
  144. break;
  145. }
  146. if (err)
  147. acpi_power_resources_list_free(list);
  148. return err;
  149. }
  150. static int acpi_power_get_state(acpi_handle handle, int *state)
  151. {
  152. acpi_status status = AE_OK;
  153. unsigned long long sta = 0;
  154. char node_name[5];
  155. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  156. if (!handle || !state)
  157. return -EINVAL;
  158. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  159. if (ACPI_FAILURE(status))
  160. return -ENODEV;
  161. *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
  162. ACPI_POWER_RESOURCE_STATE_OFF;
  163. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  164. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  165. node_name,
  166. *state ? "on" : "off"));
  167. return 0;
  168. }
  169. static int acpi_power_get_list_state(struct list_head *list, int *state)
  170. {
  171. struct acpi_power_resource_entry *entry;
  172. int cur_state;
  173. if (!list || !state)
  174. return -EINVAL;
  175. /* The state of the list is 'on' IFF all resources are 'on'. */
  176. cur_state = 0;
  177. list_for_each_entry(entry, list, node) {
  178. struct acpi_power_resource *resource = entry->resource;
  179. acpi_handle handle = resource->device.handle;
  180. int result;
  181. mutex_lock(&resource->resource_lock);
  182. result = acpi_power_get_state(handle, &cur_state);
  183. mutex_unlock(&resource->resource_lock);
  184. if (result)
  185. return result;
  186. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  187. break;
  188. }
  189. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  190. cur_state ? "on" : "off"));
  191. *state = cur_state;
  192. return 0;
  193. }
  194. static int
  195. acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
  196. struct device *dev)
  197. {
  198. struct acpi_power_dependent_device *dep;
  199. int ret = 0;
  200. mutex_lock(&resource->resource_lock);
  201. list_for_each_entry(dep, &resource->dependents, node) {
  202. /* Only add it once */
  203. if (dep->dev == dev)
  204. goto unlock;
  205. }
  206. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  207. if (!dep) {
  208. ret = -ENOMEM;
  209. goto unlock;
  210. }
  211. dep->dev = dev;
  212. list_add_tail(&dep->node, &resource->dependents);
  213. dev_dbg(dev, "added power dependency to [%s]\n", resource->name);
  214. unlock:
  215. mutex_unlock(&resource->resource_lock);
  216. return ret;
  217. }
  218. static void
  219. acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
  220. struct device *dev)
  221. {
  222. struct acpi_power_dependent_device *dep;
  223. mutex_lock(&resource->resource_lock);
  224. list_for_each_entry(dep, &resource->dependents, node) {
  225. if (dep->dev == dev) {
  226. list_del(&dep->node);
  227. kfree(dep);
  228. dev_dbg(dev, "removed power dependency to [%s]\n",
  229. resource->name);
  230. break;
  231. }
  232. }
  233. mutex_unlock(&resource->resource_lock);
  234. }
  235. /**
  236. * acpi_device_power_add_dependent - Add dependent device of this ACPI device
  237. * @adev: ACPI device pointer
  238. * @dev: Dependent device
  239. *
  240. * If @adev has non-empty _PR0 the @dev is added as dependent device to all
  241. * power resources returned by it. This means that whenever these power
  242. * resources are turned _ON the dependent devices get runtime resumed. This
  243. * is needed for devices such as PCI to allow its driver to re-initialize
  244. * it after it went to D0uninitialized.
  245. *
  246. * If @adev does not have _PR0 this does nothing.
  247. *
  248. * Returns %0 in case of success and negative errno otherwise.
  249. */
  250. int acpi_device_power_add_dependent(struct acpi_device *adev,
  251. struct device *dev)
  252. {
  253. struct acpi_power_resource_entry *entry;
  254. struct list_head *resources;
  255. int ret;
  256. if (!adev->flags.power_manageable)
  257. return 0;
  258. resources = &adev->power.states[ACPI_STATE_D0].resources;
  259. list_for_each_entry(entry, resources, node) {
  260. ret = acpi_power_resource_add_dependent(entry->resource, dev);
  261. if (ret)
  262. goto err;
  263. }
  264. return 0;
  265. err:
  266. list_for_each_entry(entry, resources, node)
  267. acpi_power_resource_remove_dependent(entry->resource, dev);
  268. return ret;
  269. }
  270. /**
  271. * acpi_device_power_remove_dependent - Remove dependent device
  272. * @adev: ACPI device pointer
  273. * @dev: Dependent device
  274. *
  275. * Does the opposite of acpi_device_power_add_dependent() and removes the
  276. * dependent device if it is found. Can be called to @adev that does not
  277. * have _PR0 as well.
  278. */
  279. void acpi_device_power_remove_dependent(struct acpi_device *adev,
  280. struct device *dev)
  281. {
  282. struct acpi_power_resource_entry *entry;
  283. struct list_head *resources;
  284. if (!adev->flags.power_manageable)
  285. return;
  286. resources = &adev->power.states[ACPI_STATE_D0].resources;
  287. list_for_each_entry_reverse(entry, resources, node)
  288. acpi_power_resource_remove_dependent(entry->resource, dev);
  289. }
  290. static int __acpi_power_on(struct acpi_power_resource *resource)
  291. {
  292. struct acpi_power_dependent_device *dep;
  293. acpi_status status = AE_OK;
  294. status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
  295. if (ACPI_FAILURE(status))
  296. return -ENODEV;
  297. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
  298. resource->name));
  299. /*
  300. * If there are other dependents on this power resource we need to
  301. * resume them now so that their drivers can re-initialize the
  302. * hardware properly after it went back to D0.
  303. */
  304. if (list_empty(&resource->dependents) ||
  305. list_is_singular(&resource->dependents))
  306. return 0;
  307. list_for_each_entry(dep, &resource->dependents, node) {
  308. dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
  309. resource->name);
  310. pm_request_resume(dep->dev);
  311. }
  312. return 0;
  313. }
  314. static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
  315. {
  316. int result = 0;
  317. if (resource->ref_count++) {
  318. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  319. "Power resource [%s] already on\n",
  320. resource->name));
  321. } else {
  322. result = __acpi_power_on(resource);
  323. if (result)
  324. resource->ref_count--;
  325. }
  326. return result;
  327. }
  328. static int acpi_power_on(struct acpi_power_resource *resource)
  329. {
  330. int result;
  331. mutex_lock(&resource->resource_lock);
  332. result = acpi_power_on_unlocked(resource);
  333. mutex_unlock(&resource->resource_lock);
  334. return result;
  335. }
  336. static int __acpi_power_off(struct acpi_power_resource *resource)
  337. {
  338. acpi_status status;
  339. status = acpi_evaluate_object(resource->device.handle, "_OFF",
  340. NULL, NULL);
  341. if (ACPI_FAILURE(status))
  342. return -ENODEV;
  343. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
  344. resource->name));
  345. return 0;
  346. }
  347. static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
  348. {
  349. int result = 0;
  350. if (!resource->ref_count) {
  351. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  352. "Power resource [%s] already off\n",
  353. resource->name));
  354. return 0;
  355. }
  356. if (--resource->ref_count) {
  357. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  358. "Power resource [%s] still in use\n",
  359. resource->name));
  360. } else {
  361. result = __acpi_power_off(resource);
  362. if (result)
  363. resource->ref_count++;
  364. }
  365. return result;
  366. }
  367. static int acpi_power_off(struct acpi_power_resource *resource)
  368. {
  369. int result;
  370. mutex_lock(&resource->resource_lock);
  371. result = acpi_power_off_unlocked(resource);
  372. mutex_unlock(&resource->resource_lock);
  373. return result;
  374. }
  375. static int acpi_power_off_list(struct list_head *list)
  376. {
  377. struct acpi_power_resource_entry *entry;
  378. int result = 0;
  379. list_for_each_entry_reverse(entry, list, node) {
  380. result = acpi_power_off(entry->resource);
  381. if (result)
  382. goto err;
  383. }
  384. return 0;
  385. err:
  386. list_for_each_entry_continue(entry, list, node)
  387. acpi_power_on(entry->resource);
  388. return result;
  389. }
  390. static int acpi_power_on_list(struct list_head *list)
  391. {
  392. struct acpi_power_resource_entry *entry;
  393. int result = 0;
  394. list_for_each_entry(entry, list, node) {
  395. result = acpi_power_on(entry->resource);
  396. if (result)
  397. goto err;
  398. }
  399. return 0;
  400. err:
  401. list_for_each_entry_continue_reverse(entry, list, node)
  402. acpi_power_off(entry->resource);
  403. return result;
  404. }
  405. static struct attribute *attrs[] = {
  406. NULL,
  407. };
  408. static const struct attribute_group attr_groups[] = {
  409. [ACPI_STATE_D0] = {
  410. .name = "power_resources_D0",
  411. .attrs = attrs,
  412. },
  413. [ACPI_STATE_D1] = {
  414. .name = "power_resources_D1",
  415. .attrs = attrs,
  416. },
  417. [ACPI_STATE_D2] = {
  418. .name = "power_resources_D2",
  419. .attrs = attrs,
  420. },
  421. [ACPI_STATE_D3_HOT] = {
  422. .name = "power_resources_D3hot",
  423. .attrs = attrs,
  424. },
  425. };
  426. static const struct attribute_group wakeup_attr_group = {
  427. .name = "power_resources_wakeup",
  428. .attrs = attrs,
  429. };
  430. static void acpi_power_hide_list(struct acpi_device *adev,
  431. struct list_head *resources,
  432. const struct attribute_group *attr_group)
  433. {
  434. struct acpi_power_resource_entry *entry;
  435. if (list_empty(resources))
  436. return;
  437. list_for_each_entry_reverse(entry, resources, node) {
  438. struct acpi_device *res_dev = &entry->resource->device;
  439. sysfs_remove_link_from_group(&adev->dev.kobj,
  440. attr_group->name,
  441. dev_name(&res_dev->dev));
  442. }
  443. sysfs_remove_group(&adev->dev.kobj, attr_group);
  444. }
  445. static void acpi_power_expose_list(struct acpi_device *adev,
  446. struct list_head *resources,
  447. const struct attribute_group *attr_group)
  448. {
  449. struct acpi_power_resource_entry *entry;
  450. int ret;
  451. if (list_empty(resources))
  452. return;
  453. ret = sysfs_create_group(&adev->dev.kobj, attr_group);
  454. if (ret)
  455. return;
  456. list_for_each_entry(entry, resources, node) {
  457. struct acpi_device *res_dev = &entry->resource->device;
  458. ret = sysfs_add_link_to_group(&adev->dev.kobj,
  459. attr_group->name,
  460. &res_dev->dev.kobj,
  461. dev_name(&res_dev->dev));
  462. if (ret) {
  463. acpi_power_hide_list(adev, resources, attr_group);
  464. break;
  465. }
  466. }
  467. }
  468. static void acpi_power_expose_hide(struct acpi_device *adev,
  469. struct list_head *resources,
  470. const struct attribute_group *attr_group,
  471. bool expose)
  472. {
  473. if (expose)
  474. acpi_power_expose_list(adev, resources, attr_group);
  475. else
  476. acpi_power_hide_list(adev, resources, attr_group);
  477. }
  478. void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
  479. {
  480. int state;
  481. if (adev->wakeup.flags.valid)
  482. acpi_power_expose_hide(adev, &adev->wakeup.resources,
  483. &wakeup_attr_group, add);
  484. if (!adev->power.flags.power_resources)
  485. return;
  486. for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
  487. acpi_power_expose_hide(adev,
  488. &adev->power.states[state].resources,
  489. &attr_groups[state], add);
  490. }
  491. int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
  492. {
  493. struct acpi_power_resource_entry *entry;
  494. int system_level = 5;
  495. list_for_each_entry(entry, list, node) {
  496. struct acpi_power_resource *resource = entry->resource;
  497. acpi_handle handle = resource->device.handle;
  498. int result;
  499. int state;
  500. mutex_lock(&resource->resource_lock);
  501. result = acpi_power_get_state(handle, &state);
  502. if (result) {
  503. mutex_unlock(&resource->resource_lock);
  504. return result;
  505. }
  506. if (state == ACPI_POWER_RESOURCE_STATE_ON) {
  507. resource->ref_count++;
  508. resource->wakeup_enabled = true;
  509. }
  510. if (system_level > resource->system_level)
  511. system_level = resource->system_level;
  512. mutex_unlock(&resource->resource_lock);
  513. }
  514. *system_level_p = system_level;
  515. return 0;
  516. }
  517. /* --------------------------------------------------------------------------
  518. Device Power Management
  519. -------------------------------------------------------------------------- */
  520. /**
  521. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  522. * ACPI 3.0) _PSW (Power State Wake)
  523. * @dev: Device to handle.
  524. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  525. * @sleep_state: Target sleep state of the system.
  526. * @dev_state: Target power state of the device.
  527. *
  528. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  529. * State Wake) for the device, if present. On failure reset the device's
  530. * wakeup.flags.valid flag.
  531. *
  532. * RETURN VALUE:
  533. * 0 if either _DSW or _PSW has been successfully executed
  534. * 0 if neither _DSW nor _PSW has been found
  535. * -ENODEV if the execution of either _DSW or _PSW has failed
  536. */
  537. int acpi_device_sleep_wake(struct acpi_device *dev,
  538. int enable, int sleep_state, int dev_state)
  539. {
  540. union acpi_object in_arg[3];
  541. struct acpi_object_list arg_list = { 3, in_arg };
  542. acpi_status status = AE_OK;
  543. /*
  544. * Try to execute _DSW first.
  545. *
  546. * Three arguments are needed for the _DSW object:
  547. * Argument 0: enable/disable the wake capabilities
  548. * Argument 1: target system state
  549. * Argument 2: target device state
  550. * When _DSW object is called to disable the wake capabilities, maybe
  551. * the first argument is filled. The values of the other two arguments
  552. * are meaningless.
  553. */
  554. in_arg[0].type = ACPI_TYPE_INTEGER;
  555. in_arg[0].integer.value = enable;
  556. in_arg[1].type = ACPI_TYPE_INTEGER;
  557. in_arg[1].integer.value = sleep_state;
  558. in_arg[2].type = ACPI_TYPE_INTEGER;
  559. in_arg[2].integer.value = dev_state;
  560. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  561. if (ACPI_SUCCESS(status)) {
  562. return 0;
  563. } else if (status != AE_NOT_FOUND) {
  564. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  565. dev->wakeup.flags.valid = 0;
  566. return -ENODEV;
  567. }
  568. /* Execute _PSW */
  569. status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
  570. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  571. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  572. dev->wakeup.flags.valid = 0;
  573. return -ENODEV;
  574. }
  575. return 0;
  576. }
  577. /*
  578. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  579. * 1. Power on the power resources required for the wakeup device
  580. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  581. * State Wake) for the device, if present
  582. */
  583. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  584. {
  585. struct acpi_power_resource_entry *entry;
  586. int err = 0;
  587. if (!dev || !dev->wakeup.flags.valid)
  588. return -EINVAL;
  589. mutex_lock(&acpi_device_lock);
  590. if (dev->wakeup.prepare_count++)
  591. goto out;
  592. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  593. struct acpi_power_resource *resource = entry->resource;
  594. mutex_lock(&resource->resource_lock);
  595. if (!resource->wakeup_enabled) {
  596. err = acpi_power_on_unlocked(resource);
  597. if (!err)
  598. resource->wakeup_enabled = true;
  599. }
  600. mutex_unlock(&resource->resource_lock);
  601. if (err) {
  602. dev_err(&dev->dev,
  603. "Cannot turn wakeup power resources on\n");
  604. dev->wakeup.flags.valid = 0;
  605. goto out;
  606. }
  607. }
  608. /*
  609. * Passing 3 as the third argument below means the device may be
  610. * put into arbitrary power state afterward.
  611. */
  612. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  613. if (err)
  614. dev->wakeup.prepare_count = 0;
  615. out:
  616. mutex_unlock(&acpi_device_lock);
  617. return err;
  618. }
  619. /*
  620. * Shutdown a wakeup device, counterpart of above method
  621. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  622. * State Wake) for the device, if present
  623. * 2. Shutdown down the power resources
  624. */
  625. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  626. {
  627. struct acpi_power_resource_entry *entry;
  628. int err = 0;
  629. if (!dev || !dev->wakeup.flags.valid)
  630. return -EINVAL;
  631. mutex_lock(&acpi_device_lock);
  632. if (--dev->wakeup.prepare_count > 0)
  633. goto out;
  634. /*
  635. * Executing the code below even if prepare_count is already zero when
  636. * the function is called may be useful, for example for initialisation.
  637. */
  638. if (dev->wakeup.prepare_count < 0)
  639. dev->wakeup.prepare_count = 0;
  640. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  641. if (err)
  642. goto out;
  643. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  644. struct acpi_power_resource *resource = entry->resource;
  645. mutex_lock(&resource->resource_lock);
  646. if (resource->wakeup_enabled) {
  647. err = acpi_power_off_unlocked(resource);
  648. if (!err)
  649. resource->wakeup_enabled = false;
  650. }
  651. mutex_unlock(&resource->resource_lock);
  652. if (err) {
  653. dev_err(&dev->dev,
  654. "Cannot turn wakeup power resources off\n");
  655. dev->wakeup.flags.valid = 0;
  656. break;
  657. }
  658. }
  659. out:
  660. mutex_unlock(&acpi_device_lock);
  661. return err;
  662. }
  663. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  664. {
  665. int result = 0;
  666. int list_state = 0;
  667. int i = 0;
  668. if (!device || !state)
  669. return -EINVAL;
  670. /*
  671. * We know a device's inferred power state when all the resources
  672. * required for a given D-state are 'on'.
  673. */
  674. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  675. struct list_head *list = &device->power.states[i].resources;
  676. if (list_empty(list))
  677. continue;
  678. result = acpi_power_get_list_state(list, &list_state);
  679. if (result)
  680. return result;
  681. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  682. *state = i;
  683. return 0;
  684. }
  685. }
  686. *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
  687. ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
  688. return 0;
  689. }
  690. int acpi_power_on_resources(struct acpi_device *device, int state)
  691. {
  692. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
  693. return -EINVAL;
  694. return acpi_power_on_list(&device->power.states[state].resources);
  695. }
  696. int acpi_power_transition(struct acpi_device *device, int state)
  697. {
  698. int result = 0;
  699. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  700. return -EINVAL;
  701. if (device->power.state == state || !device->flags.power_manageable)
  702. return 0;
  703. if ((device->power.state < ACPI_STATE_D0)
  704. || (device->power.state > ACPI_STATE_D3_COLD))
  705. return -ENODEV;
  706. /*
  707. * First we reference all power resources required in the target list
  708. * (e.g. so the device doesn't lose power while transitioning). Then,
  709. * we dereference all power resources used in the current list.
  710. */
  711. if (state < ACPI_STATE_D3_COLD)
  712. result = acpi_power_on_list(
  713. &device->power.states[state].resources);
  714. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  715. acpi_power_off_list(
  716. &device->power.states[device->power.state].resources);
  717. /* We shouldn't change the state unless the above operations succeed. */
  718. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  719. return result;
  720. }
  721. static void acpi_release_power_resource(struct device *dev)
  722. {
  723. struct acpi_device *device = to_acpi_device(dev);
  724. struct acpi_power_resource *resource;
  725. resource = container_of(device, struct acpi_power_resource, device);
  726. mutex_lock(&power_resource_list_lock);
  727. list_del(&resource->list_node);
  728. mutex_unlock(&power_resource_list_lock);
  729. acpi_free_pnp_ids(&device->pnp);
  730. kfree(resource);
  731. }
  732. static ssize_t resource_in_use_show(struct device *dev,
  733. struct device_attribute *attr,
  734. char *buf)
  735. {
  736. struct acpi_power_resource *resource;
  737. resource = to_power_resource(to_acpi_device(dev));
  738. return sprintf(buf, "%u\n", !!resource->ref_count);
  739. }
  740. static DEVICE_ATTR_RO(resource_in_use);
  741. static void acpi_power_sysfs_remove(struct acpi_device *device)
  742. {
  743. device_remove_file(&device->dev, &dev_attr_resource_in_use);
  744. }
  745. static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
  746. {
  747. mutex_lock(&power_resource_list_lock);
  748. if (!list_empty(&acpi_power_resource_list)) {
  749. struct acpi_power_resource *r;
  750. list_for_each_entry(r, &acpi_power_resource_list, list_node)
  751. if (r->order > resource->order) {
  752. list_add_tail(&resource->list_node, &r->list_node);
  753. goto out;
  754. }
  755. }
  756. list_add_tail(&resource->list_node, &acpi_power_resource_list);
  757. out:
  758. mutex_unlock(&power_resource_list_lock);
  759. }
  760. int acpi_add_power_resource(acpi_handle handle)
  761. {
  762. struct acpi_power_resource *resource;
  763. struct acpi_device *device = NULL;
  764. union acpi_object acpi_object;
  765. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  766. acpi_status status;
  767. int state, result = -ENODEV;
  768. acpi_bus_get_device(handle, &device);
  769. if (device)
  770. return 0;
  771. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  772. if (!resource)
  773. return -ENOMEM;
  774. device = &resource->device;
  775. acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
  776. ACPI_STA_DEFAULT);
  777. mutex_init(&resource->resource_lock);
  778. INIT_LIST_HEAD(&resource->list_node);
  779. INIT_LIST_HEAD(&resource->dependents);
  780. resource->name = device->pnp.bus_id;
  781. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  782. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  783. device->power.state = ACPI_STATE_UNKNOWN;
  784. /* Evalute the object to get the system level and resource order. */
  785. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  786. if (ACPI_FAILURE(status))
  787. goto err;
  788. resource->system_level = acpi_object.power_resource.system_level;
  789. resource->order = acpi_object.power_resource.resource_order;
  790. result = acpi_power_get_state(handle, &state);
  791. if (result)
  792. goto err;
  793. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  794. acpi_device_bid(device), state ? "on" : "off");
  795. device->flags.match_driver = true;
  796. result = acpi_device_add(device, acpi_release_power_resource);
  797. if (result)
  798. goto err;
  799. if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
  800. device->remove = acpi_power_sysfs_remove;
  801. acpi_power_add_resource_to_list(resource);
  802. acpi_device_add_finalize(device);
  803. return 0;
  804. err:
  805. acpi_release_power_resource(&device->dev);
  806. return result;
  807. }
  808. #ifdef CONFIG_ACPI_SLEEP
  809. void acpi_resume_power_resources(void)
  810. {
  811. struct acpi_power_resource *resource;
  812. mutex_lock(&power_resource_list_lock);
  813. list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
  814. int result, state;
  815. mutex_lock(&resource->resource_lock);
  816. result = acpi_power_get_state(resource->device.handle, &state);
  817. if (result) {
  818. mutex_unlock(&resource->resource_lock);
  819. continue;
  820. }
  821. if (state == ACPI_POWER_RESOURCE_STATE_OFF
  822. && resource->ref_count) {
  823. dev_info(&resource->device.dev, "Turning ON\n");
  824. __acpi_power_on(resource);
  825. }
  826. mutex_unlock(&resource->resource_lock);
  827. }
  828. mutex_unlock(&power_resource_list_lock);
  829. }
  830. void acpi_turn_off_unused_power_resources(void)
  831. {
  832. struct acpi_power_resource *resource;
  833. mutex_lock(&power_resource_list_lock);
  834. list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
  835. int result, state;
  836. mutex_lock(&resource->resource_lock);
  837. result = acpi_power_get_state(resource->device.handle, &state);
  838. if (result) {
  839. mutex_unlock(&resource->resource_lock);
  840. continue;
  841. }
  842. if (state == ACPI_POWER_RESOURCE_STATE_ON
  843. && !resource->ref_count) {
  844. dev_info(&resource->device.dev, "Turning OFF\n");
  845. __acpi_power_off(resource);
  846. }
  847. mutex_unlock(&resource->resource_lock);
  848. }
  849. mutex_unlock(&power_resource_list_lock);
  850. }
  851. #endif