PageRenderTime 73ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/base/power/main.c

https://bitbucket.org/zarboz/ville-upstream-test-branch
C | 1148 lines | 797 code | 148 blank | 203 comment | 150 complexity | 46dddf61825b04dda002f9d767ca2d57 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * drivers/base/power/main.c - Where the driver meets power management.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. *
  10. * The driver model core calls device_pm_add() when a device is registered.
  11. * This will initialize the embedded device_pm_info object in the device
  12. * and add it to the list of power-controlled devices. sysfs entries for
  13. * controlling device power management will also be added.
  14. *
  15. * A separate list is used for keeping track of power info, because the power
  16. * domain dependencies may differ from the ancestral dependencies that the
  17. * subsystem list maintains.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/resume-trace.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/sched.h>
  28. #include <linux/async.h>
  29. #include <linux/suspend.h>
  30. #include "../base.h"
  31. #include "power.h"
  32. /*
  33. * The entries in the dpm_list list are in a depth first order, simply
  34. * because children are guaranteed to be discovered after parents, and
  35. * are inserted at the back of the list on discovery.
  36. *
  37. * Since device_pm_add() may be called with a device lock held,
  38. * we must never try to acquire a device lock while holding
  39. * dpm_list_mutex.
  40. */
  41. LIST_HEAD(dpm_list);
  42. LIST_HEAD(dpm_prepared_list);
  43. LIST_HEAD(dpm_suspended_list);
  44. LIST_HEAD(dpm_noirq_list);
  45. struct suspend_stats suspend_stats;
  46. static DEFINE_MUTEX(dpm_list_mtx);
  47. static pm_message_t pm_transition;
  48. static int async_error;
  49. /**
  50. * device_pm_init - Initialize the PM-related part of a device object.
  51. * @dev: Device object being initialized.
  52. */
  53. void device_pm_init(struct device *dev)
  54. {
  55. dev->power.is_prepared = false;
  56. dev->power.is_suspended = false;
  57. init_completion(&dev->power.completion);
  58. complete_all(&dev->power.completion);
  59. dev->power.wakeup = NULL;
  60. spin_lock_init(&dev->power.lock);
  61. pm_runtime_init(dev);
  62. INIT_LIST_HEAD(&dev->power.entry);
  63. dev->power.power_state = PMSG_INVALID;
  64. }
  65. /**
  66. * device_pm_lock - Lock the list of active devices used by the PM core.
  67. */
  68. void device_pm_lock(void)
  69. {
  70. mutex_lock(&dpm_list_mtx);
  71. }
  72. /**
  73. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  74. */
  75. void device_pm_unlock(void)
  76. {
  77. mutex_unlock(&dpm_list_mtx);
  78. }
  79. /**
  80. * device_pm_add - Add a device to the PM core's list of active devices.
  81. * @dev: Device to add to the list.
  82. */
  83. void device_pm_add(struct device *dev)
  84. {
  85. pr_debug("PM: Adding info for %s:%s\n",
  86. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  87. mutex_lock(&dpm_list_mtx);
  88. if (dev->parent && dev->parent->power.is_prepared)
  89. dev_warn(dev, "parent %s should not be sleeping\n",
  90. dev_name(dev->parent));
  91. list_add_tail(&dev->power.entry, &dpm_list);
  92. dev_pm_qos_constraints_init(dev);
  93. mutex_unlock(&dpm_list_mtx);
  94. }
  95. /**
  96. * device_pm_remove - Remove a device from the PM core's list of active devices.
  97. * @dev: Device to be removed from the list.
  98. */
  99. void device_pm_remove(struct device *dev)
  100. {
  101. pr_debug("PM: Removing info for %s:%s\n",
  102. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  103. complete_all(&dev->power.completion);
  104. mutex_lock(&dpm_list_mtx);
  105. dev_pm_qos_constraints_destroy(dev);
  106. list_del_init(&dev->power.entry);
  107. mutex_unlock(&dpm_list_mtx);
  108. device_wakeup_disable(dev);
  109. pm_runtime_remove(dev);
  110. }
  111. /**
  112. * device_pm_move_before - Move device in the PM core's list of active devices.
  113. * @deva: Device to move in dpm_list.
  114. * @devb: Device @deva should come before.
  115. */
  116. void device_pm_move_before(struct device *deva, struct device *devb)
  117. {
  118. pr_debug("PM: Moving %s:%s before %s:%s\n",
  119. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  120. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  121. /* Delete deva from dpm_list and reinsert before devb. */
  122. list_move_tail(&deva->power.entry, &devb->power.entry);
  123. }
  124. /**
  125. * device_pm_move_after - Move device in the PM core's list of active devices.
  126. * @deva: Device to move in dpm_list.
  127. * @devb: Device @deva should come after.
  128. */
  129. void device_pm_move_after(struct device *deva, struct device *devb)
  130. {
  131. pr_debug("PM: Moving %s:%s after %s:%s\n",
  132. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  133. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  134. /* Delete deva from dpm_list and reinsert after devb. */
  135. list_move(&deva->power.entry, &devb->power.entry);
  136. }
  137. /**
  138. * device_pm_move_last - Move device to end of the PM core's list of devices.
  139. * @dev: Device to move in dpm_list.
  140. */
  141. void device_pm_move_last(struct device *dev)
  142. {
  143. pr_debug("PM: Moving %s:%s to end of list\n",
  144. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  145. list_move_tail(&dev->power.entry, &dpm_list);
  146. }
  147. static ktime_t initcall_debug_start(struct device *dev)
  148. {
  149. ktime_t calltime = ktime_set(0, 0);
  150. if (initcall_debug) {
  151. pr_info("calling %s+ @ %i\n",
  152. dev_name(dev), task_pid_nr(current));
  153. calltime = ktime_get();
  154. }
  155. return calltime;
  156. }
  157. static void initcall_debug_report(struct device *dev, ktime_t calltime,
  158. int error)
  159. {
  160. ktime_t delta, rettime;
  161. if (initcall_debug) {
  162. rettime = ktime_get();
  163. delta = ktime_sub(rettime, calltime);
  164. pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
  165. error, (unsigned long long)ktime_to_ns(delta) >> 10);
  166. }
  167. }
  168. /**
  169. * dpm_wait - Wait for a PM operation to complete.
  170. * @dev: Device to wait for.
  171. * @async: If unset, wait only if the device's power.async_suspend flag is set.
  172. */
  173. static void dpm_wait(struct device *dev, bool async)
  174. {
  175. if (!dev)
  176. return;
  177. if (async || (pm_async_enabled && dev->power.async_suspend))
  178. wait_for_completion(&dev->power.completion);
  179. }
  180. static int dpm_wait_fn(struct device *dev, void *async_ptr)
  181. {
  182. dpm_wait(dev, *((bool *)async_ptr));
  183. return 0;
  184. }
  185. static void dpm_wait_for_children(struct device *dev, bool async)
  186. {
  187. device_for_each_child(dev, &async, dpm_wait_fn);
  188. }
  189. /**
  190. * pm_op - Execute the PM operation appropriate for given PM event.
  191. * @dev: Device to handle.
  192. * @ops: PM operations to choose from.
  193. * @state: PM transition of the system being carried out.
  194. */
  195. static int pm_op(struct device *dev,
  196. const struct dev_pm_ops *ops,
  197. pm_message_t state)
  198. {
  199. int error = 0;
  200. ktime_t calltime;
  201. calltime = initcall_debug_start(dev);
  202. switch (state.event) {
  203. #ifdef CONFIG_SUSPEND
  204. case PM_EVENT_SUSPEND:
  205. if (ops->suspend) {
  206. error = ops->suspend(dev);
  207. suspend_report_result(ops->suspend, error);
  208. }
  209. break;
  210. case PM_EVENT_RESUME:
  211. if (ops->resume) {
  212. error = ops->resume(dev);
  213. suspend_report_result(ops->resume, error);
  214. }
  215. break;
  216. #endif /* CONFIG_SUSPEND */
  217. #ifdef CONFIG_HIBERNATE_CALLBACKS
  218. case PM_EVENT_FREEZE:
  219. case PM_EVENT_QUIESCE:
  220. if (ops->freeze) {
  221. error = ops->freeze(dev);
  222. suspend_report_result(ops->freeze, error);
  223. }
  224. break;
  225. case PM_EVENT_HIBERNATE:
  226. if (ops->poweroff) {
  227. error = ops->poweroff(dev);
  228. suspend_report_result(ops->poweroff, error);
  229. }
  230. break;
  231. case PM_EVENT_THAW:
  232. case PM_EVENT_RECOVER:
  233. if (ops->thaw) {
  234. error = ops->thaw(dev);
  235. suspend_report_result(ops->thaw, error);
  236. }
  237. break;
  238. case PM_EVENT_RESTORE:
  239. if (ops->restore) {
  240. error = ops->restore(dev);
  241. suspend_report_result(ops->restore, error);
  242. }
  243. break;
  244. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  245. default:
  246. error = -EINVAL;
  247. }
  248. initcall_debug_report(dev, calltime, error);
  249. return error;
  250. }
  251. /**
  252. * pm_noirq_op - Execute the PM operation appropriate for given PM event.
  253. * @dev: Device to handle.
  254. * @ops: PM operations to choose from.
  255. * @state: PM transition of the system being carried out.
  256. *
  257. * The driver of @dev will not receive interrupts while this function is being
  258. * executed.
  259. */
  260. static int pm_noirq_op(struct device *dev,
  261. const struct dev_pm_ops *ops,
  262. pm_message_t state)
  263. {
  264. int error = 0;
  265. ktime_t calltime = ktime_set(0, 0), delta, rettime;
  266. if (initcall_debug) {
  267. pr_info("calling %s+ @ %i, parent: %s\n",
  268. dev_name(dev), task_pid_nr(current),
  269. dev->parent ? dev_name(dev->parent) : "none");
  270. calltime = ktime_get();
  271. }
  272. switch (state.event) {
  273. #ifdef CONFIG_SUSPEND
  274. case PM_EVENT_SUSPEND:
  275. if (ops->suspend_noirq) {
  276. error = ops->suspend_noirq(dev);
  277. suspend_report_result(ops->suspend_noirq, error);
  278. }
  279. break;
  280. case PM_EVENT_RESUME:
  281. if (ops->resume_noirq) {
  282. error = ops->resume_noirq(dev);
  283. suspend_report_result(ops->resume_noirq, error);
  284. }
  285. break;
  286. #endif /* CONFIG_SUSPEND */
  287. #ifdef CONFIG_HIBERNATE_CALLBACKS
  288. case PM_EVENT_FREEZE:
  289. case PM_EVENT_QUIESCE:
  290. if (ops->freeze_noirq) {
  291. error = ops->freeze_noirq(dev);
  292. suspend_report_result(ops->freeze_noirq, error);
  293. }
  294. break;
  295. case PM_EVENT_HIBERNATE:
  296. if (ops->poweroff_noirq) {
  297. error = ops->poweroff_noirq(dev);
  298. suspend_report_result(ops->poweroff_noirq, error);
  299. }
  300. break;
  301. case PM_EVENT_THAW:
  302. case PM_EVENT_RECOVER:
  303. if (ops->thaw_noirq) {
  304. error = ops->thaw_noirq(dev);
  305. suspend_report_result(ops->thaw_noirq, error);
  306. }
  307. break;
  308. case PM_EVENT_RESTORE:
  309. if (ops->restore_noirq) {
  310. error = ops->restore_noirq(dev);
  311. suspend_report_result(ops->restore_noirq, error);
  312. }
  313. break;
  314. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  315. default:
  316. error = -EINVAL;
  317. }
  318. if (initcall_debug) {
  319. rettime = ktime_get();
  320. delta = ktime_sub(rettime, calltime);
  321. printk("initcall %s_i+ returned %d after %Ld usecs\n",
  322. dev_name(dev), error,
  323. (unsigned long long)ktime_to_ns(delta) >> 10);
  324. }
  325. return error;
  326. }
  327. static char *pm_verb(int event)
  328. {
  329. switch (event) {
  330. case PM_EVENT_SUSPEND:
  331. return "suspend";
  332. case PM_EVENT_RESUME:
  333. return "resume";
  334. case PM_EVENT_FREEZE:
  335. return "freeze";
  336. case PM_EVENT_QUIESCE:
  337. return "quiesce";
  338. case PM_EVENT_HIBERNATE:
  339. return "hibernate";
  340. case PM_EVENT_THAW:
  341. return "thaw";
  342. case PM_EVENT_RESTORE:
  343. return "restore";
  344. case PM_EVENT_RECOVER:
  345. return "recover";
  346. default:
  347. return "(unknown PM event)";
  348. }
  349. }
  350. static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
  351. {
  352. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  353. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  354. ", may wakeup" : "");
  355. }
  356. static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
  357. int error)
  358. {
  359. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  360. dev_name(dev), pm_verb(state.event), info, error);
  361. }
  362. static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
  363. {
  364. ktime_t calltime;
  365. u64 usecs64;
  366. int usecs;
  367. calltime = ktime_get();
  368. usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
  369. do_div(usecs64, NSEC_PER_USEC);
  370. usecs = usecs64;
  371. if (usecs == 0)
  372. usecs = 1;
  373. pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
  374. info ?: "", info ? " " : "", pm_verb(state.event),
  375. usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
  376. }
  377. /*------------------------- Resume routines -------------------------*/
  378. /**
  379. * device_resume_noirq - Execute an "early resume" callback for given device.
  380. * @dev: Device to handle.
  381. * @state: PM transition of the system being carried out.
  382. *
  383. * The driver of @dev will not receive interrupts while this function is being
  384. * executed.
  385. */
  386. static int device_resume_noirq(struct device *dev, pm_message_t state)
  387. {
  388. int error = 0;
  389. TRACE_DEVICE(dev);
  390. TRACE_RESUME(0);
  391. if (dev->pm_domain) {
  392. pm_dev_dbg(dev, state, "EARLY power domain ");
  393. error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
  394. } else if (dev->type && dev->type->pm) {
  395. pm_dev_dbg(dev, state, "EARLY type ");
  396. error = pm_noirq_op(dev, dev->type->pm, state);
  397. } else if (dev->class && dev->class->pm) {
  398. pm_dev_dbg(dev, state, "EARLY class ");
  399. error = pm_noirq_op(dev, dev->class->pm, state);
  400. } else if (dev->bus && dev->bus->pm) {
  401. pm_dev_dbg(dev, state, "EARLY ");
  402. error = pm_noirq_op(dev, dev->bus->pm, state);
  403. }
  404. TRACE_RESUME(error);
  405. return error;
  406. }
  407. /**
  408. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  409. * @state: PM transition of the system being carried out.
  410. *
  411. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  412. * enable device drivers to receive interrupts.
  413. */
  414. void dpm_resume_noirq(pm_message_t state)
  415. {
  416. ktime_t starttime = ktime_get();
  417. mutex_lock(&dpm_list_mtx);
  418. while (!list_empty(&dpm_noirq_list)) {
  419. struct device *dev = to_device(dpm_noirq_list.next);
  420. int error;
  421. get_device(dev);
  422. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  423. mutex_unlock(&dpm_list_mtx);
  424. error = device_resume_noirq(dev, state);
  425. if (error) {
  426. suspend_stats.failed_resume_noirq++;
  427. dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
  428. dpm_save_failed_dev(dev_name(dev));
  429. pm_dev_err(dev, state, " early", error);
  430. }
  431. mutex_lock(&dpm_list_mtx);
  432. put_device(dev);
  433. }
  434. mutex_unlock(&dpm_list_mtx);
  435. dpm_show_time(starttime, state, "early");
  436. resume_device_irqs();
  437. }
  438. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  439. /**
  440. * legacy_resume - Execute a legacy (bus or class) resume callback for device.
  441. * @dev: Device to resume.
  442. * @cb: Resume callback to execute.
  443. */
  444. static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
  445. {
  446. int error;
  447. ktime_t calltime;
  448. calltime = initcall_debug_start(dev);
  449. error = cb(dev);
  450. suspend_report_result(cb, error);
  451. initcall_debug_report(dev, calltime, error);
  452. return error;
  453. }
  454. /**
  455. * device_resume - Execute "resume" callbacks for given device.
  456. * @dev: Device to handle.
  457. * @state: PM transition of the system being carried out.
  458. * @async: If true, the device is being resumed asynchronously.
  459. */
  460. static int device_resume(struct device *dev, pm_message_t state, bool async)
  461. {
  462. int error = 0;
  463. bool put = false;
  464. TRACE_DEVICE(dev);
  465. TRACE_RESUME(0);
  466. dpm_wait(dev->parent, async);
  467. device_lock(dev);
  468. /*
  469. * This is a fib. But we'll allow new children to be added below
  470. * a resumed device, even if the device hasn't been completed yet.
  471. */
  472. dev->power.is_prepared = false;
  473. if (!dev->power.is_suspended)
  474. goto Unlock;
  475. pm_runtime_enable(dev);
  476. put = true;
  477. if (dev->pm_domain) {
  478. pm_dev_dbg(dev, state, "power domain ");
  479. error = pm_op(dev, &dev->pm_domain->ops, state);
  480. goto End;
  481. }
  482. if (dev->type && dev->type->pm) {
  483. pm_dev_dbg(dev, state, "type ");
  484. error = pm_op(dev, dev->type->pm, state);
  485. goto End;
  486. }
  487. if (dev->class) {
  488. if (dev->class->pm) {
  489. pm_dev_dbg(dev, state, "class ");
  490. error = pm_op(dev, dev->class->pm, state);
  491. goto End;
  492. } else if (dev->class->resume) {
  493. pm_dev_dbg(dev, state, "legacy class ");
  494. error = legacy_resume(dev, dev->class->resume);
  495. goto End;
  496. }
  497. }
  498. if (dev->bus) {
  499. if (dev->bus->pm) {
  500. pm_dev_dbg(dev, state, "");
  501. error = pm_op(dev, dev->bus->pm, state);
  502. } else if (dev->bus->resume) {
  503. pm_dev_dbg(dev, state, "legacy ");
  504. error = legacy_resume(dev, dev->bus->resume);
  505. }
  506. }
  507. End:
  508. dev->power.is_suspended = false;
  509. Unlock:
  510. device_unlock(dev);
  511. complete_all(&dev->power.completion);
  512. TRACE_RESUME(error);
  513. if (put)
  514. pm_runtime_put_sync(dev);
  515. return error;
  516. }
  517. static void async_resume(void *data, async_cookie_t cookie)
  518. {
  519. struct device *dev = (struct device *)data;
  520. int error;
  521. error = device_resume(dev, pm_transition, true);
  522. if (error)
  523. pm_dev_err(dev, pm_transition, " async", error);
  524. put_device(dev);
  525. }
  526. static bool is_async(struct device *dev)
  527. {
  528. return dev->power.async_suspend && pm_async_enabled
  529. && !pm_trace_is_enabled();
  530. }
  531. /**
  532. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  533. * @state: PM transition of the system being carried out.
  534. *
  535. * Execute the appropriate "resume" callback for all devices whose status
  536. * indicates that they are suspended.
  537. */
  538. void dpm_resume(pm_message_t state)
  539. {
  540. struct device *dev;
  541. ktime_t starttime = ktime_get();
  542. might_sleep();
  543. mutex_lock(&dpm_list_mtx);
  544. pm_transition = state;
  545. async_error = 0;
  546. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  547. INIT_COMPLETION(dev->power.completion);
  548. if (is_async(dev)) {
  549. get_device(dev);
  550. async_schedule(async_resume, dev);
  551. }
  552. }
  553. while (!list_empty(&dpm_suspended_list)) {
  554. dev = to_device(dpm_suspended_list.next);
  555. get_device(dev);
  556. if (!is_async(dev)) {
  557. int error;
  558. mutex_unlock(&dpm_list_mtx);
  559. error = device_resume(dev, state, false);
  560. if (error) {
  561. suspend_stats.failed_resume++;
  562. dpm_save_failed_step(SUSPEND_RESUME);
  563. dpm_save_failed_dev(dev_name(dev));
  564. pm_dev_err(dev, state, "", error);
  565. }
  566. mutex_lock(&dpm_list_mtx);
  567. }
  568. if (!list_empty(&dev->power.entry))
  569. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  570. put_device(dev);
  571. }
  572. mutex_unlock(&dpm_list_mtx);
  573. async_synchronize_full();
  574. dpm_show_time(starttime, state, NULL);
  575. }
  576. /**
  577. * device_complete - Complete a PM transition for given device.
  578. * @dev: Device to handle.
  579. * @state: PM transition of the system being carried out.
  580. */
  581. static void device_complete(struct device *dev, pm_message_t state)
  582. {
  583. device_lock(dev);
  584. if (dev->pm_domain) {
  585. pm_dev_dbg(dev, state, "completing power domain ");
  586. if (dev->pm_domain->ops.complete)
  587. dev->pm_domain->ops.complete(dev);
  588. } else if (dev->type && dev->type->pm) {
  589. pm_dev_dbg(dev, state, "completing type ");
  590. if (dev->type->pm->complete)
  591. dev->type->pm->complete(dev);
  592. } else if (dev->class && dev->class->pm) {
  593. pm_dev_dbg(dev, state, "completing class ");
  594. if (dev->class->pm->complete)
  595. dev->class->pm->complete(dev);
  596. } else if (dev->bus && dev->bus->pm) {
  597. pm_dev_dbg(dev, state, "completing ");
  598. if (dev->bus->pm->complete)
  599. dev->bus->pm->complete(dev);
  600. }
  601. device_unlock(dev);
  602. }
  603. /**
  604. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  605. * @state: PM transition of the system being carried out.
  606. *
  607. * Execute the ->complete() callbacks for all devices whose PM status is not
  608. * DPM_ON (this allows new devices to be registered).
  609. */
  610. void dpm_complete(pm_message_t state)
  611. {
  612. struct list_head list;
  613. might_sleep();
  614. INIT_LIST_HEAD(&list);
  615. mutex_lock(&dpm_list_mtx);
  616. while (!list_empty(&dpm_prepared_list)) {
  617. struct device *dev = to_device(dpm_prepared_list.prev);
  618. get_device(dev);
  619. dev->power.is_prepared = false;
  620. list_move(&dev->power.entry, &list);
  621. mutex_unlock(&dpm_list_mtx);
  622. device_complete(dev, state);
  623. mutex_lock(&dpm_list_mtx);
  624. put_device(dev);
  625. }
  626. list_splice(&list, &dpm_list);
  627. mutex_unlock(&dpm_list_mtx);
  628. }
  629. /**
  630. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  631. * @state: PM transition of the system being carried out.
  632. *
  633. * Execute "resume" callbacks for all devices and complete the PM transition of
  634. * the system.
  635. */
  636. void dpm_resume_end(pm_message_t state)
  637. {
  638. dpm_resume(state);
  639. dpm_complete(state);
  640. }
  641. EXPORT_SYMBOL_GPL(dpm_resume_end);
  642. /*------------------------- Suspend routines -------------------------*/
  643. /**
  644. * resume_event - Return a "resume" message for given "suspend" sleep state.
  645. * @sleep_state: PM message representing a sleep state.
  646. *
  647. * Return a PM message representing the resume event corresponding to given
  648. * sleep state.
  649. */
  650. static pm_message_t resume_event(pm_message_t sleep_state)
  651. {
  652. switch (sleep_state.event) {
  653. case PM_EVENT_SUSPEND:
  654. return PMSG_RESUME;
  655. case PM_EVENT_FREEZE:
  656. case PM_EVENT_QUIESCE:
  657. return PMSG_RECOVER;
  658. case PM_EVENT_HIBERNATE:
  659. return PMSG_RESTORE;
  660. }
  661. return PMSG_ON;
  662. }
  663. /**
  664. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  665. * @dev: Device to handle.
  666. * @state: PM transition of the system being carried out.
  667. *
  668. * The driver of @dev will not receive interrupts while this function is being
  669. * executed.
  670. */
  671. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  672. {
  673. int error;
  674. if (dev->pm_domain) {
  675. pm_dev_dbg(dev, state, "LATE power domain ");
  676. error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
  677. if (error)
  678. return error;
  679. } else if (dev->type && dev->type->pm) {
  680. pm_dev_dbg(dev, state, "LATE type ");
  681. error = pm_noirq_op(dev, dev->type->pm, state);
  682. if (error)
  683. return error;
  684. } else if (dev->class && dev->class->pm) {
  685. pm_dev_dbg(dev, state, "LATE class ");
  686. error = pm_noirq_op(dev, dev->class->pm, state);
  687. if (error)
  688. return error;
  689. } else if (dev->bus && dev->bus->pm) {
  690. pm_dev_dbg(dev, state, "LATE ");
  691. error = pm_noirq_op(dev, dev->bus->pm, state);
  692. if (error)
  693. return error;
  694. }
  695. return 0;
  696. }
  697. /**
  698. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  699. * @state: PM transition of the system being carried out.
  700. *
  701. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  702. * handlers for all non-sysdev devices.
  703. */
  704. int dpm_suspend_noirq(pm_message_t state)
  705. {
  706. ktime_t starttime = ktime_get();
  707. int error = 0;
  708. suspend_device_irqs();
  709. mutex_lock(&dpm_list_mtx);
  710. while (!list_empty(&dpm_suspended_list)) {
  711. struct device *dev = to_device(dpm_suspended_list.prev);
  712. get_device(dev);
  713. mutex_unlock(&dpm_list_mtx);
  714. error = device_suspend_noirq(dev, state);
  715. mutex_lock(&dpm_list_mtx);
  716. if (error) {
  717. pm_dev_err(dev, state, " late", error);
  718. suspend_stats.failed_suspend_noirq++;
  719. dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
  720. dpm_save_failed_dev(dev_name(dev));
  721. put_device(dev);
  722. break;
  723. }
  724. if (!list_empty(&dev->power.entry))
  725. list_move(&dev->power.entry, &dpm_noirq_list);
  726. put_device(dev);
  727. }
  728. mutex_unlock(&dpm_list_mtx);
  729. if (error)
  730. dpm_resume_noirq(resume_event(state));
  731. else
  732. dpm_show_time(starttime, state, "late");
  733. return error;
  734. }
  735. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  736. /**
  737. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  738. * @dev: Device to suspend.
  739. * @state: PM transition of the system being carried out.
  740. * @cb: Suspend callback to execute.
  741. */
  742. static int legacy_suspend(struct device *dev, pm_message_t state,
  743. int (*cb)(struct device *dev, pm_message_t state))
  744. {
  745. int error;
  746. ktime_t calltime;
  747. calltime = initcall_debug_start(dev);
  748. error = cb(dev, state);
  749. suspend_report_result(cb, error);
  750. initcall_debug_report(dev, calltime, error);
  751. return error;
  752. }
  753. /**
  754. * device_suspend - Execute "suspend" callbacks for given device.
  755. * @dev: Device to handle.
  756. * @state: PM transition of the system being carried out.
  757. * @async: If true, the device is being suspended asynchronously.
  758. */
  759. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  760. {
  761. int error = 0;
  762. dpm_wait_for_children(dev, async);
  763. if (async_error)
  764. goto Complete;
  765. pm_runtime_get_noresume(dev);
  766. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  767. pm_wakeup_event(dev, 0);
  768. if (pm_wakeup_pending()) {
  769. pm_runtime_put_sync(dev);
  770. async_error = -EBUSY;
  771. goto Complete;
  772. }
  773. device_lock(dev);
  774. if (dev->pm_domain) {
  775. pm_dev_dbg(dev, state, "power domain ");
  776. error = pm_op(dev, &dev->pm_domain->ops, state);
  777. goto End;
  778. }
  779. if (dev->type && dev->type->pm) {
  780. pm_dev_dbg(dev, state, "type ");
  781. error = pm_op(dev, dev->type->pm, state);
  782. goto End;
  783. }
  784. if (dev->class) {
  785. if (dev->class->pm) {
  786. pm_dev_dbg(dev, state, "class ");
  787. error = pm_op(dev, dev->class->pm, state);
  788. goto End;
  789. } else if (dev->class->suspend) {
  790. pm_dev_dbg(dev, state, "legacy class ");
  791. error = legacy_suspend(dev, state, dev->class->suspend);
  792. goto End;
  793. }
  794. }
  795. if (dev->bus) {
  796. if (dev->bus->pm) {
  797. pm_dev_dbg(dev, state, "");
  798. error = pm_op(dev, dev->bus->pm, state);
  799. } else if (dev->bus->suspend) {
  800. pm_dev_dbg(dev, state, "legacy ");
  801. error = legacy_suspend(dev, state, dev->bus->suspend);
  802. }
  803. }
  804. End:
  805. if (!error) {
  806. dev->power.is_suspended = true;
  807. if (dev->power.wakeup_path
  808. && dev->parent && !dev->parent->power.ignore_children)
  809. dev->parent->power.wakeup_path = true;
  810. }
  811. device_unlock(dev);
  812. Complete:
  813. complete_all(&dev->power.completion);
  814. if (error) {
  815. pm_runtime_put_sync(dev);
  816. async_error = error;
  817. } else if (dev->power.is_suspended) {
  818. __pm_runtime_disable(dev, false);
  819. }
  820. return error;
  821. }
  822. static void async_suspend(void *data, async_cookie_t cookie)
  823. {
  824. struct device *dev = (struct device *)data;
  825. int error;
  826. error = __device_suspend(dev, pm_transition, true);
  827. if (error) {
  828. dpm_save_failed_dev(dev_name(dev));
  829. pm_dev_err(dev, pm_transition, " async", error);
  830. }
  831. put_device(dev);
  832. }
  833. static int device_suspend(struct device *dev)
  834. {
  835. INIT_COMPLETION(dev->power.completion);
  836. if (pm_async_enabled && dev->power.async_suspend) {
  837. get_device(dev);
  838. async_schedule(async_suspend, dev);
  839. return 0;
  840. }
  841. return __device_suspend(dev, pm_transition, false);
  842. }
  843. /**
  844. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  845. * @state: PM transition of the system being carried out.
  846. */
  847. int dpm_suspend(pm_message_t state)
  848. {
  849. ktime_t starttime = ktime_get();
  850. int error = 0;
  851. might_sleep();
  852. mutex_lock(&dpm_list_mtx);
  853. pm_transition = state;
  854. async_error = 0;
  855. while (!list_empty(&dpm_prepared_list)) {
  856. struct device *dev = to_device(dpm_prepared_list.prev);
  857. get_device(dev);
  858. mutex_unlock(&dpm_list_mtx);
  859. error = device_suspend(dev);
  860. mutex_lock(&dpm_list_mtx);
  861. if (error) {
  862. pm_dev_err(dev, state, "", error);
  863. dpm_save_failed_dev(dev_name(dev));
  864. put_device(dev);
  865. break;
  866. }
  867. if (!list_empty(&dev->power.entry))
  868. list_move(&dev->power.entry, &dpm_suspended_list);
  869. put_device(dev);
  870. if (async_error)
  871. break;
  872. }
  873. mutex_unlock(&dpm_list_mtx);
  874. async_synchronize_full();
  875. if (!error)
  876. error = async_error;
  877. if (error) {
  878. suspend_stats.failed_suspend++;
  879. dpm_save_failed_step(SUSPEND_SUSPEND);
  880. } else
  881. dpm_show_time(starttime, state, NULL);
  882. return error;
  883. }
  884. /**
  885. * device_prepare - Prepare a device for system power transition.
  886. * @dev: Device to handle.
  887. * @state: PM transition of the system being carried out.
  888. *
  889. * Execute the ->prepare() callback(s) for given device. No new children of the
  890. * device may be registered after this function has returned.
  891. */
  892. static int device_prepare(struct device *dev, pm_message_t state)
  893. {
  894. int error = 0;
  895. device_lock(dev);
  896. dev->power.wakeup_path = device_may_wakeup(dev);
  897. if (dev->pm_domain) {
  898. pm_dev_dbg(dev, state, "preparing power domain ");
  899. if (dev->pm_domain->ops.prepare)
  900. error = dev->pm_domain->ops.prepare(dev);
  901. suspend_report_result(dev->pm_domain->ops.prepare, error);
  902. if (error)
  903. goto End;
  904. } else if (dev->type && dev->type->pm) {
  905. pm_dev_dbg(dev, state, "preparing type ");
  906. if (dev->type->pm->prepare)
  907. error = dev->type->pm->prepare(dev);
  908. suspend_report_result(dev->type->pm->prepare, error);
  909. if (error)
  910. goto End;
  911. } else if (dev->class && dev->class->pm) {
  912. pm_dev_dbg(dev, state, "preparing class ");
  913. if (dev->class->pm->prepare)
  914. error = dev->class->pm->prepare(dev);
  915. suspend_report_result(dev->class->pm->prepare, error);
  916. if (error)
  917. goto End;
  918. } else if (dev->bus && dev->bus->pm) {
  919. pm_dev_dbg(dev, state, "preparing ");
  920. if (dev->bus->pm->prepare)
  921. error = dev->bus->pm->prepare(dev);
  922. suspend_report_result(dev->bus->pm->prepare, error);
  923. }
  924. End:
  925. device_unlock(dev);
  926. return error;
  927. }
  928. /**
  929. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  930. * @state: PM transition of the system being carried out.
  931. *
  932. * Execute the ->prepare() callback(s) for all devices.
  933. */
  934. int dpm_prepare(pm_message_t state)
  935. {
  936. int error = 0;
  937. might_sleep();
  938. mutex_lock(&dpm_list_mtx);
  939. while (!list_empty(&dpm_list)) {
  940. struct device *dev = to_device(dpm_list.next);
  941. get_device(dev);
  942. mutex_unlock(&dpm_list_mtx);
  943. error = device_prepare(dev, state);
  944. mutex_lock(&dpm_list_mtx);
  945. if (error) {
  946. if (error == -EAGAIN) {
  947. put_device(dev);
  948. error = 0;
  949. continue;
  950. }
  951. printk(KERN_INFO "PM: Device %s not prepared "
  952. "for power transition: code %d\n",
  953. dev_name(dev), error);
  954. put_device(dev);
  955. break;
  956. }
  957. dev->power.is_prepared = true;
  958. if (!list_empty(&dev->power.entry))
  959. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  960. put_device(dev);
  961. }
  962. mutex_unlock(&dpm_list_mtx);
  963. return error;
  964. }
  965. /**
  966. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  967. * @state: PM transition of the system being carried out.
  968. *
  969. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  970. * callbacks for them.
  971. */
  972. int dpm_suspend_start(pm_message_t state)
  973. {
  974. int error;
  975. error = dpm_prepare(state);
  976. if (error) {
  977. suspend_stats.failed_prepare++;
  978. dpm_save_failed_step(SUSPEND_PREPARE);
  979. } else
  980. error = dpm_suspend(state);
  981. return error;
  982. }
  983. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  984. void __suspend_report_result(const char *function, void *fn, int ret)
  985. {
  986. if (ret)
  987. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  988. }
  989. EXPORT_SYMBOL_GPL(__suspend_report_result);
  990. /**
  991. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  992. * @dev: Device to wait for.
  993. * @subordinate: Device that needs to wait for @dev.
  994. */
  995. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  996. {
  997. dpm_wait(dev, subordinate->power.async_suspend);
  998. return async_error;
  999. }
  1000. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);