main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 intialize 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. }
  60. /**
  61. * device_pm_lock - Lock the list of active devices used by the PM core.
  62. */
  63. void device_pm_lock(void)
  64. {
  65. mutex_lock(&dpm_list_mtx);
  66. }
  67. /**
  68. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  69. */
  70. void device_pm_unlock(void)
  71. {
  72. mutex_unlock(&dpm_list_mtx);
  73. }
  74. /**
  75. * device_pm_add - Add a device to the PM core's list of active devices.
  76. * @dev: Device to add to the list.
  77. */
  78. void device_pm_add(struct device *dev)
  79. {
  80. pr_debug("PM: Adding info for %s:%s\n",
  81. dev->bus ? dev->bus->name : "No Bus",
  82. kobject_name(&dev->kobj));
  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",
  98. kobject_name(&dev->kobj));
  99. complete_all(&dev->power.completion);
  100. mutex_lock(&dpm_list_mtx);
  101. list_del_init(&dev->power.entry);
  102. mutex_unlock(&dpm_list_mtx);
  103. device_wakeup_disable(dev);
  104. pm_runtime_remove(dev);
  105. }
  106. /**
  107. * device_pm_move_before - Move device in the PM core's list of active devices.
  108. * @deva: Device to move in dpm_list.
  109. * @devb: Device @deva should come before.
  110. */
  111. void device_pm_move_before(struct device *deva, struct device *devb)
  112. {
  113. pr_debug("PM: Moving %s:%s before %s:%s\n",
  114. deva->bus ? deva->bus->name : "No Bus",
  115. kobject_name(&deva->kobj),
  116. devb->bus ? devb->bus->name : "No Bus",
  117. kobject_name(&devb->kobj));
  118. /* Delete deva from dpm_list and reinsert before devb. */
  119. list_move_tail(&deva->power.entry, &devb->power.entry);
  120. }
  121. /**
  122. * device_pm_move_after - Move device in the PM core's list of active devices.
  123. * @deva: Device to move in dpm_list.
  124. * @devb: Device @deva should come after.
  125. */
  126. void device_pm_move_after(struct device *deva, struct device *devb)
  127. {
  128. pr_debug("PM: Moving %s:%s after %s:%s\n",
  129. deva->bus ? deva->bus->name : "No Bus",
  130. kobject_name(&deva->kobj),
  131. devb->bus ? devb->bus->name : "No Bus",
  132. kobject_name(&devb->kobj));
  133. /* Delete deva from dpm_list and reinsert after devb. */
  134. list_move(&deva->power.entry, &devb->power.entry);
  135. }
  136. /**
  137. * device_pm_move_last - Move device to end of the PM core's list of devices.
  138. * @dev: Device to move in dpm_list.
  139. */
  140. void device_pm_move_last(struct device *dev)
  141. {
  142. pr_debug("PM: Moving %s:%s to end of list\n",
  143. dev->bus ? dev->bus->name : "No Bus",
  144. kobject_name(&dev->kobj));
  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_HIBERNATION
  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_HIBERNATION */
  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_HIBERNATION
  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_HIBERNATION */
  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. kobject_name(&dev->kobj), 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->bus && dev->bus->pm) {
  392. pm_dev_dbg(dev, state, "EARLY ");
  393. error = pm_noirq_op(dev, dev->bus->pm, state);
  394. if (error)
  395. goto End;
  396. }
  397. if (dev->type && dev->type->pm) {
  398. pm_dev_dbg(dev, state, "EARLY type ");
  399. error = pm_noirq_op(dev, dev->type->pm, state);
  400. if (error)
  401. goto End;
  402. }
  403. if (dev->class && dev->class->pm) {
  404. pm_dev_dbg(dev, state, "EARLY class ");
  405. error = pm_noirq_op(dev, dev->class->pm, state);
  406. }
  407. End:
  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. dev->power.in_suspend = false;
  468. if (dev->bus) {
  469. if (dev->bus->pm) {
  470. pm_dev_dbg(dev, state, "");
  471. error = pm_op(dev, dev->bus->pm, state);
  472. } else if (dev->bus->resume) {
  473. pm_dev_dbg(dev, state, "legacy ");
  474. error = legacy_resume(dev, dev->bus->resume);
  475. }
  476. if (error)
  477. goto End;
  478. }
  479. if (dev->type) {
  480. if (dev->type->pm) {
  481. pm_dev_dbg(dev, state, "type ");
  482. error = pm_op(dev, dev->type->pm, state);
  483. }
  484. if (error)
  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. } else if (dev->class->resume) {
  492. pm_dev_dbg(dev, state, "legacy class ");
  493. error = legacy_resume(dev, dev->class->resume);
  494. }
  495. }
  496. End:
  497. device_unlock(dev);
  498. complete_all(&dev->power.completion);
  499. TRACE_RESUME(error);
  500. return error;
  501. }
  502. static void async_resume(void *data, async_cookie_t cookie)
  503. {
  504. struct device *dev = (struct device *)data;
  505. int error;
  506. error = device_resume(dev, pm_transition, true);
  507. if (error)
  508. pm_dev_err(dev, pm_transition, " async", error);
  509. put_device(dev);
  510. }
  511. static bool is_async(struct device *dev)
  512. {
  513. return dev->power.async_suspend && pm_async_enabled
  514. && !pm_trace_is_enabled();
  515. }
  516. /**
  517. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  518. * @state: PM transition of the system being carried out.
  519. *
  520. * Execute the appropriate "resume" callback for all devices whose status
  521. * indicates that they are suspended.
  522. */
  523. static void dpm_resume(pm_message_t state)
  524. {
  525. struct device *dev;
  526. ktime_t starttime = ktime_get();
  527. mutex_lock(&dpm_list_mtx);
  528. pm_transition = state;
  529. async_error = 0;
  530. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  531. INIT_COMPLETION(dev->power.completion);
  532. if (is_async(dev)) {
  533. get_device(dev);
  534. async_schedule(async_resume, dev);
  535. }
  536. }
  537. while (!list_empty(&dpm_suspended_list)) {
  538. dev = to_device(dpm_suspended_list.next);
  539. get_device(dev);
  540. if (!is_async(dev)) {
  541. int error;
  542. mutex_unlock(&dpm_list_mtx);
  543. error = device_resume(dev, state, false);
  544. if (error)
  545. pm_dev_err(dev, state, "", error);
  546. mutex_lock(&dpm_list_mtx);
  547. }
  548. if (!list_empty(&dev->power.entry))
  549. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  550. put_device(dev);
  551. }
  552. mutex_unlock(&dpm_list_mtx);
  553. async_synchronize_full();
  554. dpm_show_time(starttime, state, NULL);
  555. }
  556. /**
  557. * device_complete - Complete a PM transition for given device.
  558. * @dev: Device to handle.
  559. * @state: PM transition of the system being carried out.
  560. */
  561. static void device_complete(struct device *dev, pm_message_t state)
  562. {
  563. device_lock(dev);
  564. if (dev->class && dev->class->pm && dev->class->pm->complete) {
  565. pm_dev_dbg(dev, state, "completing class ");
  566. dev->class->pm->complete(dev);
  567. }
  568. if (dev->type && dev->type->pm && dev->type->pm->complete) {
  569. pm_dev_dbg(dev, state, "completing type ");
  570. dev->type->pm->complete(dev);
  571. }
  572. if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
  573. pm_dev_dbg(dev, state, "completing ");
  574. dev->bus->pm->complete(dev);
  575. }
  576. device_unlock(dev);
  577. }
  578. /**
  579. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  580. * @state: PM transition of the system being carried out.
  581. *
  582. * Execute the ->complete() callbacks for all devices whose PM status is not
  583. * DPM_ON (this allows new devices to be registered).
  584. */
  585. static void dpm_complete(pm_message_t state)
  586. {
  587. struct list_head list;
  588. INIT_LIST_HEAD(&list);
  589. mutex_lock(&dpm_list_mtx);
  590. while (!list_empty(&dpm_prepared_list)) {
  591. struct device *dev = to_device(dpm_prepared_list.prev);
  592. get_device(dev);
  593. dev->power.in_suspend = false;
  594. list_move(&dev->power.entry, &list);
  595. mutex_unlock(&dpm_list_mtx);
  596. device_complete(dev, state);
  597. pm_runtime_put_sync(dev);
  598. mutex_lock(&dpm_list_mtx);
  599. put_device(dev);
  600. }
  601. list_splice(&list, &dpm_list);
  602. mutex_unlock(&dpm_list_mtx);
  603. }
  604. /**
  605. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  606. * @state: PM transition of the system being carried out.
  607. *
  608. * Execute "resume" callbacks for all devices and complete the PM transition of
  609. * the system.
  610. */
  611. void dpm_resume_end(pm_message_t state)
  612. {
  613. might_sleep();
  614. dpm_resume(state);
  615. dpm_complete(state);
  616. }
  617. EXPORT_SYMBOL_GPL(dpm_resume_end);
  618. /*------------------------- Suspend routines -------------------------*/
  619. /**
  620. * resume_event - Return a "resume" message for given "suspend" sleep state.
  621. * @sleep_state: PM message representing a sleep state.
  622. *
  623. * Return a PM message representing the resume event corresponding to given
  624. * sleep state.
  625. */
  626. static pm_message_t resume_event(pm_message_t sleep_state)
  627. {
  628. switch (sleep_state.event) {
  629. case PM_EVENT_SUSPEND:
  630. return PMSG_RESUME;
  631. case PM_EVENT_FREEZE:
  632. case PM_EVENT_QUIESCE:
  633. return PMSG_RECOVER;
  634. case PM_EVENT_HIBERNATE:
  635. return PMSG_RESTORE;
  636. }
  637. return PMSG_ON;
  638. }
  639. /**
  640. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  641. * @dev: Device to handle.
  642. * @state: PM transition of the system being carried out.
  643. *
  644. * The driver of @dev will not receive interrupts while this function is being
  645. * executed.
  646. */
  647. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  648. {
  649. int error = 0;
  650. if (dev->class && dev->class->pm) {
  651. pm_dev_dbg(dev, state, "LATE class ");
  652. error = pm_noirq_op(dev, dev->class->pm, state);
  653. if (error)
  654. goto End;
  655. }
  656. if (dev->type && dev->type->pm) {
  657. pm_dev_dbg(dev, state, "LATE type ");
  658. error = pm_noirq_op(dev, dev->type->pm, state);
  659. if (error)
  660. goto End;
  661. }
  662. if (dev->bus && dev->bus->pm) {
  663. pm_dev_dbg(dev, state, "LATE ");
  664. error = pm_noirq_op(dev, dev->bus->pm, state);
  665. }
  666. End:
  667. return error;
  668. }
  669. /**
  670. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  671. * @state: PM transition of the system being carried out.
  672. *
  673. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  674. * handlers for all non-sysdev devices.
  675. */
  676. int dpm_suspend_noirq(pm_message_t state)
  677. {
  678. ktime_t starttime = ktime_get();
  679. int error = 0;
  680. suspend_device_irqs();
  681. mutex_lock(&dpm_list_mtx);
  682. while (!list_empty(&dpm_suspended_list)) {
  683. struct device *dev = to_device(dpm_suspended_list.prev);
  684. get_device(dev);
  685. mutex_unlock(&dpm_list_mtx);
  686. error = device_suspend_noirq(dev, state);
  687. mutex_lock(&dpm_list_mtx);
  688. if (error) {
  689. pm_dev_err(dev, state, " late", error);
  690. put_device(dev);
  691. break;
  692. }
  693. if (!list_empty(&dev->power.entry))
  694. list_move(&dev->power.entry, &dpm_noirq_list);
  695. put_device(dev);
  696. }
  697. mutex_unlock(&dpm_list_mtx);
  698. if (error)
  699. dpm_resume_noirq(resume_event(state));
  700. else
  701. dpm_show_time(starttime, state, "late");
  702. return error;
  703. }
  704. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  705. /**
  706. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  707. * @dev: Device to suspend.
  708. * @state: PM transition of the system being carried out.
  709. * @cb: Suspend callback to execute.
  710. */
  711. static int legacy_suspend(struct device *dev, pm_message_t state,
  712. int (*cb)(struct device *dev, pm_message_t state))
  713. {
  714. int error;
  715. ktime_t calltime;
  716. calltime = initcall_debug_start(dev);
  717. error = cb(dev, state);
  718. suspend_report_result(cb, error);
  719. initcall_debug_report(dev, calltime, error);
  720. return error;
  721. }
  722. /**
  723. * device_suspend - Execute "suspend" callbacks for given device.
  724. * @dev: Device to handle.
  725. * @state: PM transition of the system being carried out.
  726. * @async: If true, the device is being suspended asynchronously.
  727. */
  728. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  729. {
  730. int error = 0;
  731. dpm_wait_for_children(dev, async);
  732. device_lock(dev);
  733. if (async_error)
  734. goto End;
  735. if (pm_wakeup_pending()) {
  736. async_error = -EBUSY;
  737. goto End;
  738. }
  739. if (dev->class) {
  740. if (dev->class->pm) {
  741. pm_dev_dbg(dev, state, "class ");
  742. error = pm_op(dev, dev->class->pm, state);
  743. } else if (dev->class->suspend) {
  744. pm_dev_dbg(dev, state, "legacy class ");
  745. error = legacy_suspend(dev, state, dev->class->suspend);
  746. }
  747. if (error)
  748. goto End;
  749. }
  750. if (dev->type) {
  751. if (dev->type->pm) {
  752. pm_dev_dbg(dev, state, "type ");
  753. error = pm_op(dev, dev->type->pm, state);
  754. }
  755. if (error)
  756. goto End;
  757. }
  758. if (dev->bus) {
  759. if (dev->bus->pm) {
  760. pm_dev_dbg(dev, state, "");
  761. error = pm_op(dev, dev->bus->pm, state);
  762. } else if (dev->bus->suspend) {
  763. pm_dev_dbg(dev, state, "legacy ");
  764. error = legacy_suspend(dev, state, dev->bus->suspend);
  765. }
  766. }
  767. End:
  768. device_unlock(dev);
  769. complete_all(&dev->power.completion);
  770. if (error)
  771. async_error = error;
  772. return error;
  773. }
  774. static void async_suspend(void *data, async_cookie_t cookie)
  775. {
  776. struct device *dev = (struct device *)data;
  777. int error;
  778. error = __device_suspend(dev, pm_transition, true);
  779. if (error)
  780. pm_dev_err(dev, pm_transition, " async", error);
  781. put_device(dev);
  782. }
  783. static int device_suspend(struct device *dev)
  784. {
  785. INIT_COMPLETION(dev->power.completion);
  786. if (pm_async_enabled && dev->power.async_suspend) {
  787. get_device(dev);
  788. async_schedule(async_suspend, dev);
  789. return 0;
  790. }
  791. return __device_suspend(dev, pm_transition, false);
  792. }
  793. /**
  794. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  795. * @state: PM transition of the system being carried out.
  796. */
  797. static int dpm_suspend(pm_message_t state)
  798. {
  799. ktime_t starttime = ktime_get();
  800. int error = 0;
  801. mutex_lock(&dpm_list_mtx);
  802. pm_transition = state;
  803. async_error = 0;
  804. while (!list_empty(&dpm_prepared_list)) {
  805. struct device *dev = to_device(dpm_prepared_list.prev);
  806. get_device(dev);
  807. mutex_unlock(&dpm_list_mtx);
  808. error = device_suspend(dev);
  809. mutex_lock(&dpm_list_mtx);
  810. if (error) {
  811. pm_dev_err(dev, state, "", error);
  812. put_device(dev);
  813. break;
  814. }
  815. if (!list_empty(&dev->power.entry))
  816. list_move(&dev->power.entry, &dpm_suspended_list);
  817. put_device(dev);
  818. if (async_error)
  819. break;
  820. }
  821. mutex_unlock(&dpm_list_mtx);
  822. async_synchronize_full();
  823. if (!error)
  824. error = async_error;
  825. if (!error)
  826. dpm_show_time(starttime, state, NULL);
  827. return error;
  828. }
  829. /**
  830. * device_prepare - Prepare a device for system power transition.
  831. * @dev: Device to handle.
  832. * @state: PM transition of the system being carried out.
  833. *
  834. * Execute the ->prepare() callback(s) for given device. No new children of the
  835. * device may be registered after this function has returned.
  836. */
  837. static int device_prepare(struct device *dev, pm_message_t state)
  838. {
  839. int error = 0;
  840. device_lock(dev);
  841. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  842. pm_dev_dbg(dev, state, "preparing ");
  843. error = dev->bus->pm->prepare(dev);
  844. suspend_report_result(dev->bus->pm->prepare, error);
  845. if (error)
  846. goto End;
  847. }
  848. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  849. pm_dev_dbg(dev, state, "preparing type ");
  850. error = dev->type->pm->prepare(dev);
  851. suspend_report_result(dev->type->pm->prepare, error);
  852. if (error)
  853. goto End;
  854. }
  855. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  856. pm_dev_dbg(dev, state, "preparing class ");
  857. error = dev->class->pm->prepare(dev);
  858. suspend_report_result(dev->class->pm->prepare, error);
  859. }
  860. End:
  861. device_unlock(dev);
  862. return error;
  863. }
  864. /**
  865. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  866. * @state: PM transition of the system being carried out.
  867. *
  868. * Execute the ->prepare() callback(s) for all devices.
  869. */
  870. static int dpm_prepare(pm_message_t state)
  871. {
  872. int error = 0;
  873. mutex_lock(&dpm_list_mtx);
  874. while (!list_empty(&dpm_list)) {
  875. struct device *dev = to_device(dpm_list.next);
  876. get_device(dev);
  877. mutex_unlock(&dpm_list_mtx);
  878. pm_runtime_get_noresume(dev);
  879. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  880. pm_wakeup_event(dev, 0);
  881. if (pm_wakeup_pending()) {
  882. pm_runtime_put_sync(dev);
  883. error = -EBUSY;
  884. } else {
  885. error = device_prepare(dev, state);
  886. }
  887. mutex_lock(&dpm_list_mtx);
  888. if (error) {
  889. if (error == -EAGAIN) {
  890. put_device(dev);
  891. error = 0;
  892. continue;
  893. }
  894. printk(KERN_INFO "PM: Device %s not prepared "
  895. "for power transition: code %d\n",
  896. kobject_name(&dev->kobj), error);
  897. put_device(dev);
  898. break;
  899. }
  900. dev->power.in_suspend = true;
  901. if (!list_empty(&dev->power.entry))
  902. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  903. put_device(dev);
  904. }
  905. mutex_unlock(&dpm_list_mtx);
  906. return error;
  907. }
  908. /**
  909. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  910. * @state: PM transition of the system being carried out.
  911. *
  912. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  913. * callbacks for them.
  914. */
  915. int dpm_suspend_start(pm_message_t state)
  916. {
  917. int error;
  918. might_sleep();
  919. error = dpm_prepare(state);
  920. if (!error)
  921. error = dpm_suspend(state);
  922. return error;
  923. }
  924. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  925. void __suspend_report_result(const char *function, void *fn, int ret)
  926. {
  927. if (ret)
  928. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  929. }
  930. EXPORT_SYMBOL_GPL(__suspend_report_result);
  931. /**
  932. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  933. * @dev: Device to wait for.
  934. * @subordinate: Device that needs to wait for @dev.
  935. */
  936. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  937. {
  938. dpm_wait(dev, subordinate->power.async_suspend);
  939. return async_error;
  940. }
  941. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);