main.c 26 KB

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