main.c 33 KB

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