PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/base/power/main.c

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