power.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. #define pr_fmt(fmt) "ACPI: PM: " fmt
  23. #include <linux/delay.h>
  24. #include <linux/dmi.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/string_choices.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/acpi.h>
  34. #include "sleep.h"
  35. #include "internal.h"
  36. #define ACPI_POWER_CLASS "power_resource"
  37. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  38. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  39. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  40. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  41. struct acpi_power_dependent_device {
  42. struct device *dev;
  43. struct list_head node;
  44. };
  45. struct acpi_power_resource {
  46. struct acpi_device device;
  47. struct list_head list_node;
  48. u32 system_level;
  49. u32 order;
  50. unsigned int ref_count;
  51. u8 state;
  52. struct mutex resource_lock;
  53. struct list_head dependents;
  54. };
  55. struct acpi_power_resource_entry {
  56. struct list_head node;
  57. struct acpi_power_resource *resource;
  58. };
  59. static bool hp_eb_gp12pxp_quirk;
  60. static bool unused_power_resources_quirk;
  61. static LIST_HEAD(acpi_power_resource_list);
  62. static DEFINE_MUTEX(power_resource_list_lock);
  63. /* --------------------------------------------------------------------------
  64. Power Resource Management
  65. -------------------------------------------------------------------------- */
  66. static inline const char *resource_dev_name(struct acpi_power_resource *pr)
  67. {
  68. return dev_name(&pr->device.dev);
  69. }
  70. static inline
  71. struct acpi_power_resource *to_power_resource(struct acpi_device *device)
  72. {
  73. return container_of(device, struct acpi_power_resource, device);
  74. }
  75. static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
  76. {
  77. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  78. if (!device)
  79. return NULL;
  80. return to_power_resource(device);
  81. }
  82. static int acpi_power_resources_list_add(acpi_handle handle,
  83. struct list_head *list)
  84. {
  85. struct acpi_power_resource *resource = acpi_power_get_context(handle);
  86. struct acpi_power_resource_entry *entry;
  87. if (!resource || !list)
  88. return -EINVAL;
  89. entry = kzalloc_obj(*entry);
  90. if (!entry)
  91. return -ENOMEM;
  92. entry->resource = resource;
  93. if (!list_empty(list)) {
  94. struct acpi_power_resource_entry *e;
  95. list_for_each_entry(e, list, node)
  96. if (e->resource->order > resource->order) {
  97. list_add_tail(&entry->node, &e->node);
  98. return 0;
  99. }
  100. }
  101. list_add_tail(&entry->node, list);
  102. return 0;
  103. }
  104. void acpi_power_resources_list_free(struct list_head *list)
  105. {
  106. struct acpi_power_resource_entry *entry, *e;
  107. list_for_each_entry_safe(entry, e, list, node) {
  108. list_del(&entry->node);
  109. kfree(entry);
  110. }
  111. }
  112. static bool acpi_power_resource_is_dup(union acpi_object *package,
  113. unsigned int start, unsigned int i)
  114. {
  115. acpi_handle rhandle, dup;
  116. unsigned int j;
  117. /* The caller is expected to check the package element types */
  118. rhandle = package->package.elements[i].reference.handle;
  119. for (j = start; j < i; j++) {
  120. dup = package->package.elements[j].reference.handle;
  121. if (dup == rhandle)
  122. return true;
  123. }
  124. return false;
  125. }
  126. int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
  127. struct list_head *list)
  128. {
  129. unsigned int i;
  130. int err = 0;
  131. for (i = start; i < package->package.count; i++) {
  132. union acpi_object *element = &package->package.elements[i];
  133. struct acpi_device *rdev;
  134. acpi_handle rhandle;
  135. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  136. err = -ENODATA;
  137. break;
  138. }
  139. rhandle = element->reference.handle;
  140. if (!rhandle) {
  141. err = -ENODEV;
  142. break;
  143. }
  144. /* Some ACPI tables contain duplicate power resource references */
  145. if (acpi_power_resource_is_dup(package, start, i))
  146. continue;
  147. rdev = acpi_add_power_resource(rhandle);
  148. if (!rdev) {
  149. err = -ENODEV;
  150. break;
  151. }
  152. err = acpi_power_resources_list_add(rhandle, list);
  153. if (err)
  154. break;
  155. }
  156. if (err)
  157. acpi_power_resources_list_free(list);
  158. return err;
  159. }
  160. static int __get_state(acpi_handle handle, u8 *state)
  161. {
  162. acpi_status status = AE_OK;
  163. unsigned long long sta = 0;
  164. u8 cur_state;
  165. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  166. if (ACPI_FAILURE(status))
  167. return -ENODEV;
  168. cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON;
  169. acpi_handle_debug(handle, "Power resource is %s\n",
  170. str_on_off(cur_state));
  171. *state = cur_state;
  172. return 0;
  173. }
  174. static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state)
  175. {
  176. if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) {
  177. int ret;
  178. ret = __get_state(resource->device.handle, &resource->state);
  179. if (ret)
  180. return ret;
  181. }
  182. *state = resource->state;
  183. return 0;
  184. }
  185. static int acpi_power_get_list_state(struct list_head *list, u8 *state)
  186. {
  187. struct acpi_power_resource_entry *entry;
  188. u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF;
  189. if (!list || !state)
  190. return -EINVAL;
  191. /* The state of the list is 'on' IFF all resources are 'on'. */
  192. list_for_each_entry(entry, list, node) {
  193. struct acpi_power_resource *resource = entry->resource;
  194. int result;
  195. mutex_lock(&resource->resource_lock);
  196. result = acpi_power_get_state(resource, &cur_state);
  197. mutex_unlock(&resource->resource_lock);
  198. if (result)
  199. return result;
  200. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  201. break;
  202. }
  203. pr_debug("Power resource list is %s\n", str_on_off(cur_state));
  204. *state = cur_state;
  205. return 0;
  206. }
  207. static int
  208. acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
  209. struct device *dev)
  210. {
  211. struct acpi_power_dependent_device *dep;
  212. int ret = 0;
  213. mutex_lock(&resource->resource_lock);
  214. list_for_each_entry(dep, &resource->dependents, node) {
  215. /* Only add it once */
  216. if (dep->dev == dev)
  217. goto unlock;
  218. }
  219. dep = kzalloc_obj(*dep);
  220. if (!dep) {
  221. ret = -ENOMEM;
  222. goto unlock;
  223. }
  224. dep->dev = dev;
  225. list_add_tail(&dep->node, &resource->dependents);
  226. dev_dbg(dev, "added power dependency to [%s]\n",
  227. resource_dev_name(resource));
  228. unlock:
  229. mutex_unlock(&resource->resource_lock);
  230. return ret;
  231. }
  232. static void
  233. acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
  234. struct device *dev)
  235. {
  236. struct acpi_power_dependent_device *dep;
  237. mutex_lock(&resource->resource_lock);
  238. list_for_each_entry(dep, &resource->dependents, node) {
  239. if (dep->dev == dev) {
  240. list_del(&dep->node);
  241. kfree(dep);
  242. dev_dbg(dev, "removed power dependency to [%s]\n",
  243. resource_dev_name(resource));
  244. break;
  245. }
  246. }
  247. mutex_unlock(&resource->resource_lock);
  248. }
  249. /**
  250. * acpi_device_power_add_dependent - Add dependent device of this ACPI device
  251. * @adev: ACPI device pointer
  252. * @dev: Dependent device
  253. *
  254. * If @adev has non-empty _PR0 the @dev is added as dependent device to all
  255. * power resources returned by it. This means that whenever these power
  256. * resources are turned _ON the dependent devices get runtime resumed. This
  257. * is needed for devices such as PCI to allow its driver to re-initialize
  258. * it after it went to D0uninitialized.
  259. *
  260. * If @adev does not have _PR0 this does nothing.
  261. *
  262. * Returns %0 in case of success and negative errno otherwise.
  263. */
  264. int acpi_device_power_add_dependent(struct acpi_device *adev,
  265. struct device *dev)
  266. {
  267. struct acpi_power_resource_entry *entry;
  268. struct list_head *resources;
  269. int ret;
  270. if (!adev->flags.power_manageable)
  271. return 0;
  272. resources = &adev->power.states[ACPI_STATE_D0].resources;
  273. list_for_each_entry(entry, resources, node) {
  274. ret = acpi_power_resource_add_dependent(entry->resource, dev);
  275. if (ret)
  276. goto err;
  277. }
  278. return 0;
  279. err:
  280. list_for_each_entry(entry, resources, node)
  281. acpi_power_resource_remove_dependent(entry->resource, dev);
  282. return ret;
  283. }
  284. /**
  285. * acpi_device_power_remove_dependent - Remove dependent device
  286. * @adev: ACPI device pointer
  287. * @dev: Dependent device
  288. *
  289. * Does the opposite of acpi_device_power_add_dependent() and removes the
  290. * dependent device if it is found. Can be called to @adev that does not
  291. * have _PR0 as well.
  292. */
  293. void acpi_device_power_remove_dependent(struct acpi_device *adev,
  294. struct device *dev)
  295. {
  296. struct acpi_power_resource_entry *entry;
  297. struct list_head *resources;
  298. if (!adev->flags.power_manageable)
  299. return;
  300. resources = &adev->power.states[ACPI_STATE_D0].resources;
  301. list_for_each_entry_reverse(entry, resources, node)
  302. acpi_power_resource_remove_dependent(entry->resource, dev);
  303. }
  304. static int __acpi_power_on(struct acpi_power_resource *resource)
  305. {
  306. acpi_handle handle = resource->device.handle;
  307. struct acpi_power_dependent_device *dep;
  308. acpi_status status = AE_OK;
  309. status = acpi_evaluate_object(handle, "_ON", NULL, NULL);
  310. if (ACPI_FAILURE(status)) {
  311. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  312. return -ENODEV;
  313. }
  314. resource->state = ACPI_POWER_RESOURCE_STATE_ON;
  315. acpi_handle_debug(handle, "Power resource turned on\n");
  316. /*
  317. * If there are other dependents on this power resource we need to
  318. * resume them now so that their drivers can re-initialize the
  319. * hardware properly after it went back to D0.
  320. */
  321. if (list_empty(&resource->dependents) ||
  322. list_is_singular(&resource->dependents))
  323. return 0;
  324. list_for_each_entry(dep, &resource->dependents, node) {
  325. dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
  326. resource_dev_name(resource));
  327. pm_request_resume(dep->dev);
  328. }
  329. return 0;
  330. }
  331. static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
  332. {
  333. int result = 0;
  334. if (resource->ref_count++) {
  335. acpi_handle_debug(resource->device.handle,
  336. "Power resource already on\n");
  337. } else {
  338. result = __acpi_power_on(resource);
  339. if (result)
  340. resource->ref_count--;
  341. }
  342. return result;
  343. }
  344. static int acpi_power_on(struct acpi_power_resource *resource)
  345. {
  346. int result;
  347. mutex_lock(&resource->resource_lock);
  348. result = acpi_power_on_unlocked(resource);
  349. mutex_unlock(&resource->resource_lock);
  350. return result;
  351. }
  352. static int __acpi_power_off(struct acpi_power_resource *resource)
  353. {
  354. acpi_handle handle = resource->device.handle;
  355. acpi_status status;
  356. status = acpi_evaluate_object(handle, "_OFF", NULL, NULL);
  357. if (ACPI_FAILURE(status)) {
  358. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  359. return -ENODEV;
  360. }
  361. resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
  362. acpi_handle_debug(handle, "Power resource turned off\n");
  363. return 0;
  364. }
  365. static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
  366. {
  367. int result = 0;
  368. if (!resource->ref_count) {
  369. acpi_handle_debug(resource->device.handle,
  370. "Power resource already off\n");
  371. return 0;
  372. }
  373. if (--resource->ref_count) {
  374. acpi_handle_debug(resource->device.handle,
  375. "Power resource still in use\n");
  376. } else {
  377. result = __acpi_power_off(resource);
  378. if (result)
  379. resource->ref_count++;
  380. }
  381. return result;
  382. }
  383. static int acpi_power_off(struct acpi_power_resource *resource)
  384. {
  385. int result;
  386. mutex_lock(&resource->resource_lock);
  387. result = acpi_power_off_unlocked(resource);
  388. mutex_unlock(&resource->resource_lock);
  389. return result;
  390. }
  391. static int acpi_power_off_list(struct list_head *list)
  392. {
  393. struct acpi_power_resource_entry *entry;
  394. int result = 0;
  395. list_for_each_entry_reverse(entry, list, node) {
  396. result = acpi_power_off(entry->resource);
  397. if (result)
  398. goto err;
  399. }
  400. return 0;
  401. err:
  402. list_for_each_entry_continue(entry, list, node)
  403. acpi_power_on(entry->resource);
  404. return result;
  405. }
  406. static int acpi_power_on_list(struct list_head *list)
  407. {
  408. struct acpi_power_resource_entry *entry;
  409. int result = 0;
  410. list_for_each_entry(entry, list, node) {
  411. result = acpi_power_on(entry->resource);
  412. if (result)
  413. goto err;
  414. }
  415. return 0;
  416. err:
  417. list_for_each_entry_continue_reverse(entry, list, node)
  418. acpi_power_off(entry->resource);
  419. return result;
  420. }
  421. static struct attribute *attrs[] = {
  422. NULL,
  423. };
  424. static const struct attribute_group attr_groups[] = {
  425. [ACPI_STATE_D0] = {
  426. .name = "power_resources_D0",
  427. .attrs = attrs,
  428. },
  429. [ACPI_STATE_D1] = {
  430. .name = "power_resources_D1",
  431. .attrs = attrs,
  432. },
  433. [ACPI_STATE_D2] = {
  434. .name = "power_resources_D2",
  435. .attrs = attrs,
  436. },
  437. [ACPI_STATE_D3_HOT] = {
  438. .name = "power_resources_D3hot",
  439. .attrs = attrs,
  440. },
  441. };
  442. static const struct attribute_group wakeup_attr_group = {
  443. .name = "power_resources_wakeup",
  444. .attrs = attrs,
  445. };
  446. static void acpi_power_hide_list(struct acpi_device *adev,
  447. struct list_head *resources,
  448. const struct attribute_group *attr_group)
  449. {
  450. struct acpi_power_resource_entry *entry;
  451. if (list_empty(resources))
  452. return;
  453. list_for_each_entry_reverse(entry, resources, node) {
  454. struct acpi_device *res_dev = &entry->resource->device;
  455. sysfs_remove_link_from_group(&adev->dev.kobj,
  456. attr_group->name,
  457. dev_name(&res_dev->dev));
  458. }
  459. sysfs_remove_group(&adev->dev.kobj, attr_group);
  460. }
  461. static void acpi_power_expose_list(struct acpi_device *adev,
  462. struct list_head *resources,
  463. const struct attribute_group *attr_group)
  464. {
  465. struct acpi_power_resource_entry *entry;
  466. int ret;
  467. if (list_empty(resources))
  468. return;
  469. ret = sysfs_create_group(&adev->dev.kobj, attr_group);
  470. if (ret)
  471. return;
  472. list_for_each_entry(entry, resources, node) {
  473. struct acpi_device *res_dev = &entry->resource->device;
  474. ret = sysfs_add_link_to_group(&adev->dev.kobj,
  475. attr_group->name,
  476. &res_dev->dev.kobj,
  477. dev_name(&res_dev->dev));
  478. if (ret) {
  479. acpi_power_hide_list(adev, resources, attr_group);
  480. break;
  481. }
  482. }
  483. }
  484. static void acpi_power_expose_hide(struct acpi_device *adev,
  485. struct list_head *resources,
  486. const struct attribute_group *attr_group,
  487. bool expose)
  488. {
  489. if (expose)
  490. acpi_power_expose_list(adev, resources, attr_group);
  491. else
  492. acpi_power_hide_list(adev, resources, attr_group);
  493. }
  494. void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
  495. {
  496. int state;
  497. if (adev->wakeup.flags.valid)
  498. acpi_power_expose_hide(adev, &adev->wakeup.resources,
  499. &wakeup_attr_group, add);
  500. if (!adev->power.flags.power_resources)
  501. return;
  502. for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
  503. acpi_power_expose_hide(adev,
  504. &adev->power.states[state].resources,
  505. &attr_groups[state], add);
  506. }
  507. int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
  508. {
  509. struct acpi_power_resource_entry *entry;
  510. int system_level = 5;
  511. list_for_each_entry(entry, list, node) {
  512. struct acpi_power_resource *resource = entry->resource;
  513. u8 state;
  514. mutex_lock(&resource->resource_lock);
  515. /*
  516. * Make sure that the power resource state and its reference
  517. * counter value are consistent with each other.
  518. */
  519. if (!resource->ref_count &&
  520. !acpi_power_get_state(resource, &state) &&
  521. state == ACPI_POWER_RESOURCE_STATE_ON)
  522. __acpi_power_off(resource);
  523. if (system_level > resource->system_level)
  524. system_level = resource->system_level;
  525. mutex_unlock(&resource->resource_lock);
  526. }
  527. *system_level_p = system_level;
  528. return 0;
  529. }
  530. /* --------------------------------------------------------------------------
  531. Device Power Management
  532. -------------------------------------------------------------------------- */
  533. /**
  534. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  535. * ACPI 3.0) _PSW (Power State Wake)
  536. * @dev: Device to handle.
  537. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  538. * @sleep_state: Target sleep state of the system.
  539. * @dev_state: Target power state of the device.
  540. *
  541. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  542. * State Wake) for the device, if present. On failure reset the device's
  543. * wakeup.flags.valid flag.
  544. *
  545. * RETURN VALUE:
  546. * 0 if either _DSW or _PSW has been successfully executed
  547. * 0 if neither _DSW nor _PSW has been found
  548. * -ENODEV if the execution of either _DSW or _PSW has failed
  549. */
  550. int acpi_device_sleep_wake(struct acpi_device *dev,
  551. int enable, int sleep_state, int dev_state)
  552. {
  553. union acpi_object in_arg[3];
  554. struct acpi_object_list arg_list = { 3, in_arg };
  555. acpi_status status = AE_OK;
  556. /*
  557. * Try to execute _DSW first.
  558. *
  559. * Three arguments are needed for the _DSW object:
  560. * Argument 0: enable/disable the wake capabilities
  561. * Argument 1: target system state
  562. * Argument 2: target device state
  563. * When _DSW object is called to disable the wake capabilities, maybe
  564. * the first argument is filled. The values of the other two arguments
  565. * are meaningless.
  566. */
  567. in_arg[0].type = ACPI_TYPE_INTEGER;
  568. in_arg[0].integer.value = enable;
  569. in_arg[1].type = ACPI_TYPE_INTEGER;
  570. in_arg[1].integer.value = sleep_state;
  571. in_arg[2].type = ACPI_TYPE_INTEGER;
  572. in_arg[2].integer.value = dev_state;
  573. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  574. if (ACPI_SUCCESS(status)) {
  575. return 0;
  576. } else if (status != AE_NOT_FOUND) {
  577. acpi_handle_info(dev->handle, "_DSW execution failed\n");
  578. dev->wakeup.flags.valid = 0;
  579. return -ENODEV;
  580. }
  581. /* Execute _PSW */
  582. status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
  583. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  584. acpi_handle_info(dev->handle, "_PSW execution failed\n");
  585. dev->wakeup.flags.valid = 0;
  586. return -ENODEV;
  587. }
  588. return 0;
  589. }
  590. /*
  591. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  592. * 1. Power on the power resources required for the wakeup device
  593. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  594. * State Wake) for the device, if present
  595. */
  596. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  597. {
  598. int err = 0;
  599. if (!dev || !dev->wakeup.flags.valid)
  600. return -EINVAL;
  601. mutex_lock(&acpi_device_lock);
  602. dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n",
  603. dev->wakeup.prepare_count);
  604. if (dev->wakeup.prepare_count++)
  605. goto out;
  606. err = acpi_power_on_list(&dev->wakeup.resources);
  607. if (err) {
  608. dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
  609. dev->wakeup.flags.valid = 0;
  610. goto out;
  611. }
  612. /*
  613. * Passing 3 as the third argument below means the device may be
  614. * put into arbitrary power state afterward.
  615. */
  616. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  617. if (err) {
  618. acpi_power_off_list(&dev->wakeup.resources);
  619. dev->wakeup.prepare_count = 0;
  620. goto out;
  621. }
  622. dev_dbg(&dev->dev, "Wakeup power enabled\n");
  623. out:
  624. mutex_unlock(&acpi_device_lock);
  625. return err;
  626. }
  627. /*
  628. * Shutdown a wakeup device, counterpart of above method
  629. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  630. * State Wake) for the device, if present
  631. * 2. Shutdown down the power resources
  632. */
  633. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  634. {
  635. struct acpi_power_resource_entry *entry;
  636. int err = 0;
  637. if (!dev || !dev->wakeup.flags.valid)
  638. return -EINVAL;
  639. mutex_lock(&acpi_device_lock);
  640. dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n",
  641. dev->wakeup.prepare_count);
  642. /* Do nothing if wakeup power has not been enabled for this device. */
  643. if (dev->wakeup.prepare_count <= 0)
  644. goto out;
  645. if (--dev->wakeup.prepare_count > 0)
  646. goto out;
  647. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  648. if (err)
  649. goto out;
  650. /*
  651. * All of the power resources in the list need to be turned off even if
  652. * there are errors.
  653. */
  654. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  655. int ret;
  656. ret = acpi_power_off(entry->resource);
  657. if (ret && !err)
  658. err = ret;
  659. }
  660. if (err) {
  661. dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
  662. dev->wakeup.flags.valid = 0;
  663. goto out;
  664. }
  665. dev_dbg(&dev->dev, "Wakeup power disabled\n");
  666. out:
  667. mutex_unlock(&acpi_device_lock);
  668. return err;
  669. }
  670. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  671. {
  672. u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
  673. int result = 0;
  674. int i = 0;
  675. if (!device || !state)
  676. return -EINVAL;
  677. /*
  678. * We know a device's inferred power state when all the resources
  679. * required for a given D-state are 'on'.
  680. */
  681. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  682. struct list_head *list = &device->power.states[i].resources;
  683. if (list_empty(list))
  684. continue;
  685. result = acpi_power_get_list_state(list, &list_state);
  686. if (result)
  687. return result;
  688. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  689. *state = i;
  690. return 0;
  691. }
  692. }
  693. *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
  694. ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
  695. return 0;
  696. }
  697. int acpi_power_on_resources(struct acpi_device *device, int state)
  698. {
  699. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
  700. return -EINVAL;
  701. return acpi_power_on_list(&device->power.states[state].resources);
  702. }
  703. int acpi_power_transition(struct acpi_device *device, int state)
  704. {
  705. int result = 0;
  706. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  707. return -EINVAL;
  708. if (device->power.state == state || !device->flags.power_manageable)
  709. return 0;
  710. if ((device->power.state < ACPI_STATE_D0)
  711. || (device->power.state > ACPI_STATE_D3_COLD))
  712. return -ENODEV;
  713. /*
  714. * First we reference all power resources required in the target list
  715. * (e.g. so the device doesn't lose power while transitioning). Then,
  716. * we dereference all power resources used in the current list.
  717. */
  718. if (state < ACPI_STATE_D3_COLD)
  719. result = acpi_power_on_list(
  720. &device->power.states[state].resources);
  721. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  722. acpi_power_off_list(
  723. &device->power.states[device->power.state].resources);
  724. /* We shouldn't change the state unless the above operations succeed. */
  725. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  726. return result;
  727. }
  728. static void acpi_release_power_resource(struct device *dev)
  729. {
  730. struct acpi_device *device = to_acpi_device(dev);
  731. struct acpi_power_resource *resource;
  732. resource = container_of(device, struct acpi_power_resource, device);
  733. mutex_lock(&power_resource_list_lock);
  734. list_del(&resource->list_node);
  735. mutex_unlock(&power_resource_list_lock);
  736. acpi_free_pnp_ids(&device->pnp);
  737. kfree(resource);
  738. }
  739. static ssize_t resource_in_use_show(struct device *dev,
  740. struct device_attribute *attr,
  741. char *buf)
  742. {
  743. struct acpi_power_resource *resource;
  744. resource = to_power_resource(to_acpi_device(dev));
  745. return sprintf(buf, "%u\n", !!resource->ref_count);
  746. }
  747. static DEVICE_ATTR_RO(resource_in_use);
  748. static void acpi_power_sysfs_remove(struct acpi_device *device)
  749. {
  750. device_remove_file(&device->dev, &dev_attr_resource_in_use);
  751. }
  752. static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
  753. {
  754. mutex_lock(&power_resource_list_lock);
  755. if (!list_empty(&acpi_power_resource_list)) {
  756. struct acpi_power_resource *r;
  757. list_for_each_entry(r, &acpi_power_resource_list, list_node)
  758. if (r->order > resource->order) {
  759. list_add_tail(&resource->list_node, &r->list_node);
  760. goto out;
  761. }
  762. }
  763. list_add_tail(&resource->list_node, &acpi_power_resource_list);
  764. out:
  765. mutex_unlock(&power_resource_list_lock);
  766. }
  767. struct acpi_device *acpi_add_power_resource(acpi_handle handle)
  768. {
  769. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  770. struct acpi_power_resource *resource;
  771. union acpi_object acpi_object;
  772. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  773. acpi_status status;
  774. u8 state_dummy;
  775. int result;
  776. if (device)
  777. return device;
  778. resource = kzalloc_obj(*resource);
  779. if (!resource)
  780. return NULL;
  781. device = &resource->device;
  782. acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
  783. acpi_release_power_resource);
  784. mutex_init(&resource->resource_lock);
  785. INIT_LIST_HEAD(&resource->list_node);
  786. INIT_LIST_HEAD(&resource->dependents);
  787. strscpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  788. strscpy(acpi_device_class(device), ACPI_POWER_CLASS);
  789. device->power.state = ACPI_STATE_UNKNOWN;
  790. device->flags.match_driver = true;
  791. /* Evaluate the object to get the system level and resource order. */
  792. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  793. if (ACPI_FAILURE(status))
  794. goto err;
  795. resource->system_level = acpi_object.power_resource.system_level;
  796. resource->order = acpi_object.power_resource.resource_order;
  797. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  798. /* Get the initial state or just flip it on if that fails. */
  799. if (acpi_power_get_state(resource, &state_dummy))
  800. __acpi_power_on(resource);
  801. acpi_handle_info(handle, "New power resource\n");
  802. result = acpi_tie_acpi_dev(device);
  803. if (result)
  804. goto err;
  805. result = acpi_device_add(device);
  806. if (result)
  807. goto err;
  808. if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
  809. device->remove = acpi_power_sysfs_remove;
  810. acpi_power_add_resource_to_list(resource);
  811. acpi_device_add_finalize(device);
  812. return device;
  813. err:
  814. acpi_release_power_resource(&device->dev);
  815. return NULL;
  816. }
  817. #ifdef CONFIG_ACPI_SLEEP
  818. static bool resource_is_gp12pxp(acpi_handle handle)
  819. {
  820. const char *path;
  821. bool ret;
  822. path = acpi_handle_path(handle);
  823. ret = path && strcmp(path, "\\_SB_.PCI0.GP12.PXP_") == 0;
  824. kfree(path);
  825. return ret;
  826. }
  827. static void acpi_resume_on_eb_gp12pxp(struct acpi_power_resource *resource)
  828. {
  829. acpi_handle_notice(resource->device.handle,
  830. "HP EB quirk - turning OFF then ON\n");
  831. __acpi_power_off(resource);
  832. __acpi_power_on(resource);
  833. /*
  834. * Use the same delay as DSDT uses in modem _RST method.
  835. *
  836. * Otherwise we get "Unable to change power state from unknown to D0,
  837. * device inaccessible" error for the modem PCI device after thaw.
  838. *
  839. * This power resource is normally being enabled only during thaw (once)
  840. * so this wait is not a performance issue.
  841. */
  842. msleep(200);
  843. }
  844. void acpi_resume_power_resources(void)
  845. {
  846. struct acpi_power_resource *resource;
  847. mutex_lock(&power_resource_list_lock);
  848. list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
  849. int result;
  850. u8 state;
  851. mutex_lock(&resource->resource_lock);
  852. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  853. result = acpi_power_get_state(resource, &state);
  854. if (result) {
  855. mutex_unlock(&resource->resource_lock);
  856. continue;
  857. }
  858. if (state == ACPI_POWER_RESOURCE_STATE_OFF
  859. && resource->ref_count) {
  860. if (hp_eb_gp12pxp_quirk &&
  861. resource_is_gp12pxp(resource->device.handle)) {
  862. acpi_resume_on_eb_gp12pxp(resource);
  863. } else {
  864. acpi_handle_debug(resource->device.handle,
  865. "Turning ON\n");
  866. __acpi_power_on(resource);
  867. }
  868. }
  869. mutex_unlock(&resource->resource_lock);
  870. }
  871. mutex_unlock(&power_resource_list_lock);
  872. }
  873. #endif
  874. static const struct dmi_system_id dmi_hp_elitebook_gp12pxp_quirk[] = {
  875. /*
  876. * This laptop (and possibly similar models too) has power resource called
  877. * "GP12.PXP_" for its WWAN modem.
  878. *
  879. * For this power resource to turn ON power for the modem it needs certain
  880. * internal flag called "ONEN" to be set.
  881. * This flag only gets set from this power resource "_OFF" method, while the
  882. * actual modem power gets turned off during suspend by "GP12.PTS" method
  883. * called from the global "_PTS" (Prepare To Sleep) method.
  884. * On the other hand, this power resource "_OFF" method implementation just
  885. * sets the aforementioned flag without actually doing anything else (it
  886. * doesn't contain any code to actually turn off power).
  887. *
  888. * The above means that when upon hibernation finish we try to set this
  889. * power resource back ON since its "_STA" method returns 0 (while the resource
  890. * is still considered in use) its "_ON" method won't do anything since
  891. * that "ONEN" flag is not set.
  892. * Overall, this means the modem is dead until laptop is rebooted since its
  893. * power has been cut by "_PTS" and its PCI configuration was lost and not able
  894. * to be restored.
  895. *
  896. * The easiest way to workaround the issue is to call this power resource
  897. * "_OFF" method before calling the "_ON" method to make sure the "ONEN"
  898. * flag gets properly set.
  899. */
  900. {
  901. .matches = {
  902. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  903. DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook 855 G7 Notebook PC"),
  904. },
  905. },
  906. {}
  907. };
  908. static const struct dmi_system_id dmi_leave_unused_power_resources_on[] = {
  909. {
  910. /*
  911. * The Toshiba Click Mini has a CPR3 power-resource which must
  912. * be on for the touchscreen to work, but which is not in any
  913. * _PR? lists. The other 2 affected power-resources are no-ops.
  914. */
  915. .matches = {
  916. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  917. DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
  918. },
  919. },
  920. {
  921. /*
  922. * THUNDEROBOT ZERO laptop: Due to its SSDT table bug, power
  923. * resource 'PXP' will be shut down on initialization, making
  924. * the NVMe #2 and the NVIDIA dGPU both unavailable (they're
  925. * both controlled by 'PXP').
  926. */
  927. .matches = {
  928. DMI_MATCH(DMI_SYS_VENDOR, "THUNDEROBOT"),
  929. DMI_MATCH(DMI_PRODUCT_NAME, "ZERO"),
  930. }
  931. },
  932. {}
  933. };
  934. /**
  935. * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
  936. */
  937. void acpi_turn_off_unused_power_resources(void)
  938. {
  939. struct acpi_power_resource *resource;
  940. if (unused_power_resources_quirk)
  941. return;
  942. mutex_lock(&power_resource_list_lock);
  943. list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
  944. mutex_lock(&resource->resource_lock);
  945. if (!resource->ref_count &&
  946. resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
  947. acpi_handle_debug(resource->device.handle, "Turning OFF\n");
  948. __acpi_power_off(resource);
  949. }
  950. mutex_unlock(&resource->resource_lock);
  951. }
  952. mutex_unlock(&power_resource_list_lock);
  953. }
  954. void __init acpi_power_resources_init(void)
  955. {
  956. hp_eb_gp12pxp_quirk = dmi_check_system(dmi_hp_elitebook_gp12pxp_quirk);
  957. unused_power_resources_quirk =
  958. dmi_check_system(dmi_leave_unused_power_resources_on);
  959. }