main.c 27 KB

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