main.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  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/export.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/pm-trace.h>
  25. #include <linux/pm_wakeirq.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/sched.h>
  28. #include <linux/sched/debug.h>
  29. #include <linux/async.h>
  30. #include <linux/suspend.h>
  31. #include <trace/events/power.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/cpuidle.h>
  34. #include <linux/timer.h>
  35. #include "../base.h"
  36. #include "power.h"
  37. typedef int (*pm_callback_t)(struct device *);
  38. /*
  39. * The entries in the dpm_list list are in a depth first order, simply
  40. * because children are guaranteed to be discovered after parents, and
  41. * are inserted at the back of the list on discovery.
  42. *
  43. * Since device_pm_add() may be called with a device lock held,
  44. * we must never try to acquire a device lock while holding
  45. * dpm_list_mutex.
  46. */
  47. LIST_HEAD(dpm_list);
  48. static LIST_HEAD(dpm_prepared_list);
  49. static LIST_HEAD(dpm_suspended_list);
  50. static LIST_HEAD(dpm_late_early_list);
  51. static LIST_HEAD(dpm_noirq_list);
  52. struct suspend_stats suspend_stats;
  53. static DEFINE_MUTEX(dpm_list_mtx);
  54. static pm_message_t pm_transition;
  55. static int async_error;
  56. static const char *pm_verb(int event)
  57. {
  58. switch (event) {
  59. case PM_EVENT_SUSPEND:
  60. return "suspend";
  61. case PM_EVENT_RESUME:
  62. return "resume";
  63. case PM_EVENT_FREEZE:
  64. return "freeze";
  65. case PM_EVENT_QUIESCE:
  66. return "quiesce";
  67. case PM_EVENT_HIBERNATE:
  68. return "hibernate";
  69. case PM_EVENT_THAW:
  70. return "thaw";
  71. case PM_EVENT_RESTORE:
  72. return "restore";
  73. case PM_EVENT_RECOVER:
  74. return "recover";
  75. default:
  76. return "(unknown PM event)";
  77. }
  78. }
  79. /**
  80. * device_pm_sleep_init - Initialize system suspend-related device fields.
  81. * @dev: Device object being initialized.
  82. */
  83. void device_pm_sleep_init(struct device *dev)
  84. {
  85. dev->power.is_prepared = false;
  86. dev->power.is_suspended = false;
  87. dev->power.is_noirq_suspended = false;
  88. dev->power.is_late_suspended = false;
  89. init_completion(&dev->power.completion);
  90. complete_all(&dev->power.completion);
  91. dev->power.wakeup = NULL;
  92. INIT_LIST_HEAD(&dev->power.entry);
  93. }
  94. /**
  95. * device_pm_lock - Lock the list of active devices used by the PM core.
  96. */
  97. void device_pm_lock(void)
  98. {
  99. mutex_lock(&dpm_list_mtx);
  100. }
  101. /**
  102. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  103. */
  104. void device_pm_unlock(void)
  105. {
  106. mutex_unlock(&dpm_list_mtx);
  107. }
  108. /**
  109. * device_pm_add - Add a device to the PM core's list of active devices.
  110. * @dev: Device to add to the list.
  111. */
  112. void device_pm_add(struct device *dev)
  113. {
  114. pr_debug("PM: Adding info for %s:%s\n",
  115. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  116. device_pm_check_callbacks(dev);
  117. mutex_lock(&dpm_list_mtx);
  118. if (dev->parent && dev->parent->power.is_prepared)
  119. dev_warn(dev, "parent %s should not be sleeping\n",
  120. dev_name(dev->parent));
  121. list_add_tail(&dev->power.entry, &dpm_list);
  122. dev->power.in_dpm_list = true;
  123. mutex_unlock(&dpm_list_mtx);
  124. }
  125. /**
  126. * device_pm_remove - Remove a device from the PM core's list of active devices.
  127. * @dev: Device to be removed from the list.
  128. */
  129. void device_pm_remove(struct device *dev)
  130. {
  131. pr_debug("PM: Removing info for %s:%s\n",
  132. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  133. complete_all(&dev->power.completion);
  134. mutex_lock(&dpm_list_mtx);
  135. list_del_init(&dev->power.entry);
  136. dev->power.in_dpm_list = false;
  137. mutex_unlock(&dpm_list_mtx);
  138. device_wakeup_disable(dev);
  139. pm_runtime_remove(dev);
  140. device_pm_check_callbacks(dev);
  141. }
  142. /**
  143. * device_pm_move_before - Move device in the PM core's list of active devices.
  144. * @deva: Device to move in dpm_list.
  145. * @devb: Device @deva should come before.
  146. */
  147. void device_pm_move_before(struct device *deva, struct device *devb)
  148. {
  149. pr_debug("PM: Moving %s:%s before %s:%s\n",
  150. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  151. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  152. /* Delete deva from dpm_list and reinsert before devb. */
  153. list_move_tail(&deva->power.entry, &devb->power.entry);
  154. }
  155. /**
  156. * device_pm_move_after - Move device in the PM core's list of active devices.
  157. * @deva: Device to move in dpm_list.
  158. * @devb: Device @deva should come after.
  159. */
  160. void device_pm_move_after(struct device *deva, struct device *devb)
  161. {
  162. pr_debug("PM: Moving %s:%s after %s:%s\n",
  163. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  164. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  165. /* Delete deva from dpm_list and reinsert after devb. */
  166. list_move(&deva->power.entry, &devb->power.entry);
  167. }
  168. /**
  169. * device_pm_move_last - Move device to end of the PM core's list of devices.
  170. * @dev: Device to move in dpm_list.
  171. */
  172. void device_pm_move_last(struct device *dev)
  173. {
  174. pr_debug("PM: Moving %s:%s to end of list\n",
  175. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  176. list_move_tail(&dev->power.entry, &dpm_list);
  177. }
  178. static ktime_t initcall_debug_start(struct device *dev, void *cb)
  179. {
  180. if (!pm_print_times_enabled)
  181. return 0;
  182. dev_info(dev, "calling %pF @ %i, parent: %s\n", cb,
  183. task_pid_nr(current),
  184. dev->parent ? dev_name(dev->parent) : "none");
  185. return ktime_get();
  186. }
  187. static void initcall_debug_report(struct device *dev, ktime_t calltime,
  188. void *cb, int error)
  189. {
  190. ktime_t rettime;
  191. s64 nsecs;
  192. if (!pm_print_times_enabled)
  193. return;
  194. rettime = ktime_get();
  195. nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
  196. dev_info(dev, "%pF returned %d after %Ld usecs\n", cb, error,
  197. (unsigned long long)nsecs >> 10);
  198. }
  199. /**
  200. * dpm_wait - Wait for a PM operation to complete.
  201. * @dev: Device to wait for.
  202. * @async: If unset, wait only if the device's power.async_suspend flag is set.
  203. */
  204. static void dpm_wait(struct device *dev, bool async)
  205. {
  206. if (!dev)
  207. return;
  208. if (async || (pm_async_enabled && dev->power.async_suspend))
  209. wait_for_completion(&dev->power.completion);
  210. }
  211. static int dpm_wait_fn(struct device *dev, void *async_ptr)
  212. {
  213. dpm_wait(dev, *((bool *)async_ptr));
  214. return 0;
  215. }
  216. static void dpm_wait_for_children(struct device *dev, bool async)
  217. {
  218. device_for_each_child(dev, &async, dpm_wait_fn);
  219. }
  220. static void dpm_wait_for_suppliers(struct device *dev, bool async)
  221. {
  222. struct device_link *link;
  223. int idx;
  224. idx = device_links_read_lock();
  225. /*
  226. * If the supplier goes away right after we've checked the link to it,
  227. * we'll wait for its completion to change the state, but that's fine,
  228. * because the only things that will block as a result are the SRCU
  229. * callbacks freeing the link objects for the links in the list we're
  230. * walking.
  231. */
  232. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
  233. if (READ_ONCE(link->status) != DL_STATE_DORMANT)
  234. dpm_wait(link->supplier, async);
  235. device_links_read_unlock(idx);
  236. }
  237. static void dpm_wait_for_superior(struct device *dev, bool async)
  238. {
  239. dpm_wait(dev->parent, async);
  240. dpm_wait_for_suppliers(dev, async);
  241. }
  242. static void dpm_wait_for_consumers(struct device *dev, bool async)
  243. {
  244. struct device_link *link;
  245. int idx;
  246. idx = device_links_read_lock();
  247. /*
  248. * The status of a device link can only be changed from "dormant" by a
  249. * probe, but that cannot happen during system suspend/resume. In
  250. * theory it can change to "dormant" at that time, but then it is
  251. * reasonable to wait for the target device anyway (eg. if it goes
  252. * away, it's better to wait for it to go away completely and then
  253. * continue instead of trying to continue in parallel with its
  254. * unregistration).
  255. */
  256. list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
  257. if (READ_ONCE(link->status) != DL_STATE_DORMANT)
  258. dpm_wait(link->consumer, async);
  259. device_links_read_unlock(idx);
  260. }
  261. static void dpm_wait_for_subordinate(struct device *dev, bool async)
  262. {
  263. dpm_wait_for_children(dev, async);
  264. dpm_wait_for_consumers(dev, async);
  265. }
  266. /**
  267. * pm_op - Return the PM operation appropriate for given PM event.
  268. * @ops: PM operations to choose from.
  269. * @state: PM transition of the system being carried out.
  270. */
  271. static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
  272. {
  273. switch (state.event) {
  274. #ifdef CONFIG_SUSPEND
  275. case PM_EVENT_SUSPEND:
  276. return ops->suspend;
  277. case PM_EVENT_RESUME:
  278. return ops->resume;
  279. #endif /* CONFIG_SUSPEND */
  280. #ifdef CONFIG_HIBERNATE_CALLBACKS
  281. case PM_EVENT_FREEZE:
  282. case PM_EVENT_QUIESCE:
  283. return ops->freeze;
  284. case PM_EVENT_HIBERNATE:
  285. return ops->poweroff;
  286. case PM_EVENT_THAW:
  287. case PM_EVENT_RECOVER:
  288. return ops->thaw;
  289. break;
  290. case PM_EVENT_RESTORE:
  291. return ops->restore;
  292. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  293. }
  294. return NULL;
  295. }
  296. /**
  297. * pm_late_early_op - Return the PM operation appropriate for given PM event.
  298. * @ops: PM operations to choose from.
  299. * @state: PM transition of the system being carried out.
  300. *
  301. * Runtime PM is disabled for @dev while this function is being executed.
  302. */
  303. static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
  304. pm_message_t state)
  305. {
  306. switch (state.event) {
  307. #ifdef CONFIG_SUSPEND
  308. case PM_EVENT_SUSPEND:
  309. return ops->suspend_late;
  310. case PM_EVENT_RESUME:
  311. return ops->resume_early;
  312. #endif /* CONFIG_SUSPEND */
  313. #ifdef CONFIG_HIBERNATE_CALLBACKS
  314. case PM_EVENT_FREEZE:
  315. case PM_EVENT_QUIESCE:
  316. return ops->freeze_late;
  317. case PM_EVENT_HIBERNATE:
  318. return ops->poweroff_late;
  319. case PM_EVENT_THAW:
  320. case PM_EVENT_RECOVER:
  321. return ops->thaw_early;
  322. case PM_EVENT_RESTORE:
  323. return ops->restore_early;
  324. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  325. }
  326. return NULL;
  327. }
  328. /**
  329. * pm_noirq_op - Return the PM operation appropriate for given PM event.
  330. * @ops: PM operations to choose from.
  331. * @state: PM transition of the system being carried out.
  332. *
  333. * The driver of @dev will not receive interrupts while this function is being
  334. * executed.
  335. */
  336. static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
  337. {
  338. switch (state.event) {
  339. #ifdef CONFIG_SUSPEND
  340. case PM_EVENT_SUSPEND:
  341. return ops->suspend_noirq;
  342. case PM_EVENT_RESUME:
  343. return ops->resume_noirq;
  344. #endif /* CONFIG_SUSPEND */
  345. #ifdef CONFIG_HIBERNATE_CALLBACKS
  346. case PM_EVENT_FREEZE:
  347. case PM_EVENT_QUIESCE:
  348. return ops->freeze_noirq;
  349. case PM_EVENT_HIBERNATE:
  350. return ops->poweroff_noirq;
  351. case PM_EVENT_THAW:
  352. case PM_EVENT_RECOVER:
  353. return ops->thaw_noirq;
  354. case PM_EVENT_RESTORE:
  355. return ops->restore_noirq;
  356. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  357. }
  358. return NULL;
  359. }
  360. static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
  361. {
  362. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  363. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  364. ", may wakeup" : "");
  365. }
  366. static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
  367. int error)
  368. {
  369. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  370. dev_name(dev), pm_verb(state.event), info, error);
  371. }
  372. static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
  373. const char *info)
  374. {
  375. ktime_t calltime;
  376. u64 usecs64;
  377. int usecs;
  378. calltime = ktime_get();
  379. usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
  380. do_div(usecs64, NSEC_PER_USEC);
  381. usecs = usecs64;
  382. if (usecs == 0)
  383. usecs = 1;
  384. pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
  385. info ?: "", info ? " " : "", pm_verb(state.event),
  386. error ? "aborted" : "complete",
  387. usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
  388. }
  389. static int dpm_run_callback(pm_callback_t cb, struct device *dev,
  390. pm_message_t state, const char *info)
  391. {
  392. ktime_t calltime;
  393. int error;
  394. if (!cb)
  395. return 0;
  396. calltime = initcall_debug_start(dev, cb);
  397. pm_dev_dbg(dev, state, info);
  398. trace_device_pm_callback_start(dev, info, state.event);
  399. error = cb(dev);
  400. trace_device_pm_callback_end(dev, error);
  401. suspend_report_result(cb, error);
  402. initcall_debug_report(dev, calltime, cb, error);
  403. return error;
  404. }
  405. #ifdef CONFIG_DPM_WATCHDOG
  406. struct dpm_watchdog {
  407. struct device *dev;
  408. struct task_struct *tsk;
  409. struct timer_list timer;
  410. };
  411. #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
  412. struct dpm_watchdog wd
  413. /**
  414. * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
  415. * @data: Watchdog object address.
  416. *
  417. * Called when a driver has timed out suspending or resuming.
  418. * There's not much we can do here to recover so panic() to
  419. * capture a crash-dump in pstore.
  420. */
  421. static void dpm_watchdog_handler(struct timer_list *t)
  422. {
  423. struct dpm_watchdog *wd = from_timer(wd, t, timer);
  424. dev_emerg(wd->dev, "**** DPM device timeout ****\n");
  425. show_stack(wd->tsk, NULL);
  426. panic("%s %s: unrecoverable failure\n",
  427. dev_driver_string(wd->dev), dev_name(wd->dev));
  428. }
  429. /**
  430. * dpm_watchdog_set - Enable pm watchdog for given device.
  431. * @wd: Watchdog. Must be allocated on the stack.
  432. * @dev: Device to handle.
  433. */
  434. static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
  435. {
  436. struct timer_list *timer = &wd->timer;
  437. wd->dev = dev;
  438. wd->tsk = current;
  439. timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
  440. /* use same timeout value for both suspend and resume */
  441. timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
  442. add_timer(timer);
  443. }
  444. /**
  445. * dpm_watchdog_clear - Disable suspend/resume watchdog.
  446. * @wd: Watchdog to disable.
  447. */
  448. static void dpm_watchdog_clear(struct dpm_watchdog *wd)
  449. {
  450. struct timer_list *timer = &wd->timer;
  451. del_timer_sync(timer);
  452. destroy_timer_on_stack(timer);
  453. }
  454. #else
  455. #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
  456. #define dpm_watchdog_set(x, y)
  457. #define dpm_watchdog_clear(x)
  458. #endif
  459. /*------------------------- Resume routines -------------------------*/
  460. /**
  461. * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
  462. * @dev: Target device.
  463. *
  464. * Make the core skip the "early resume" and "resume" phases for @dev.
  465. *
  466. * This function can be called by middle-layer code during the "noirq" phase of
  467. * system resume if necessary, but not by device drivers.
  468. */
  469. void dev_pm_skip_next_resume_phases(struct device *dev)
  470. {
  471. dev->power.is_late_suspended = false;
  472. dev->power.is_suspended = false;
  473. }
  474. /**
  475. * suspend_event - Return a "suspend" message for given "resume" one.
  476. * @resume_msg: PM message representing a system-wide resume transition.
  477. */
  478. static pm_message_t suspend_event(pm_message_t resume_msg)
  479. {
  480. switch (resume_msg.event) {
  481. case PM_EVENT_RESUME:
  482. return PMSG_SUSPEND;
  483. case PM_EVENT_THAW:
  484. case PM_EVENT_RESTORE:
  485. return PMSG_FREEZE;
  486. case PM_EVENT_RECOVER:
  487. return PMSG_HIBERNATE;
  488. }
  489. return PMSG_ON;
  490. }
  491. /**
  492. * dev_pm_may_skip_resume - System-wide device resume optimization check.
  493. * @dev: Target device.
  494. *
  495. * Checks whether or not the device may be left in suspend after a system-wide
  496. * transition to the working state.
  497. */
  498. bool dev_pm_may_skip_resume(struct device *dev)
  499. {
  500. return !dev->power.must_resume && pm_transition.event != PM_EVENT_RESTORE;
  501. }
  502. static pm_callback_t dpm_subsys_resume_noirq_cb(struct device *dev,
  503. pm_message_t state,
  504. const char **info_p)
  505. {
  506. pm_callback_t callback;
  507. const char *info;
  508. if (dev->pm_domain) {
  509. info = "noirq power domain ";
  510. callback = pm_noirq_op(&dev->pm_domain->ops, state);
  511. } else if (dev->type && dev->type->pm) {
  512. info = "noirq type ";
  513. callback = pm_noirq_op(dev->type->pm, state);
  514. } else if (dev->class && dev->class->pm) {
  515. info = "noirq class ";
  516. callback = pm_noirq_op(dev->class->pm, state);
  517. } else if (dev->bus && dev->bus->pm) {
  518. info = "noirq bus ";
  519. callback = pm_noirq_op(dev->bus->pm, state);
  520. } else {
  521. return NULL;
  522. }
  523. if (info_p)
  524. *info_p = info;
  525. return callback;
  526. }
  527. static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
  528. pm_message_t state,
  529. const char **info_p);
  530. static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
  531. pm_message_t state,
  532. const char **info_p);
  533. /**
  534. * device_resume_noirq - Execute a "noirq resume" callback for given device.
  535. * @dev: Device to handle.
  536. * @state: PM transition of the system being carried out.
  537. * @async: If true, the device is being resumed asynchronously.
  538. *
  539. * The driver of @dev will not receive interrupts while this function is being
  540. * executed.
  541. */
  542. static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
  543. {
  544. pm_callback_t callback;
  545. const char *info;
  546. bool skip_resume;
  547. int error = 0;
  548. TRACE_DEVICE(dev);
  549. TRACE_RESUME(0);
  550. if (dev->power.syscore || dev->power.direct_complete)
  551. goto Out;
  552. if (!dev->power.is_noirq_suspended)
  553. goto Out;
  554. dpm_wait_for_superior(dev, async);
  555. skip_resume = dev_pm_may_skip_resume(dev);
  556. callback = dpm_subsys_resume_noirq_cb(dev, state, &info);
  557. if (callback)
  558. goto Run;
  559. if (skip_resume)
  560. goto Skip;
  561. if (dev_pm_smart_suspend_and_suspended(dev)) {
  562. pm_message_t suspend_msg = suspend_event(state);
  563. /*
  564. * If "freeze" callbacks have been skipped during a transition
  565. * related to hibernation, the subsequent "thaw" callbacks must
  566. * be skipped too or bad things may happen. Otherwise, resume
  567. * callbacks are going to be run for the device, so its runtime
  568. * PM status must be changed to reflect the new state after the
  569. * transition under way.
  570. */
  571. if (!dpm_subsys_suspend_late_cb(dev, suspend_msg, NULL) &&
  572. !dpm_subsys_suspend_noirq_cb(dev, suspend_msg, NULL)) {
  573. if (state.event == PM_EVENT_THAW) {
  574. skip_resume = true;
  575. goto Skip;
  576. } else {
  577. pm_runtime_set_active(dev);
  578. }
  579. }
  580. }
  581. if (dev->driver && dev->driver->pm) {
  582. info = "noirq driver ";
  583. callback = pm_noirq_op(dev->driver->pm, state);
  584. }
  585. Run:
  586. error = dpm_run_callback(callback, dev, state, info);
  587. Skip:
  588. dev->power.is_noirq_suspended = false;
  589. if (skip_resume) {
  590. /*
  591. * The device is going to be left in suspend, but it might not
  592. * have been in runtime suspend before the system suspended, so
  593. * its runtime PM status needs to be updated to avoid confusing
  594. * the runtime PM framework when runtime PM is enabled for the
  595. * device again.
  596. */
  597. pm_runtime_set_suspended(dev);
  598. dev_pm_skip_next_resume_phases(dev);
  599. }
  600. Out:
  601. complete_all(&dev->power.completion);
  602. TRACE_RESUME(error);
  603. return error;
  604. }
  605. static bool is_async(struct device *dev)
  606. {
  607. return dev->power.async_suspend && pm_async_enabled
  608. && !pm_trace_is_enabled();
  609. }
  610. static void async_resume_noirq(void *data, async_cookie_t cookie)
  611. {
  612. struct device *dev = (struct device *)data;
  613. int error;
  614. error = device_resume_noirq(dev, pm_transition, true);
  615. if (error)
  616. pm_dev_err(dev, pm_transition, " async", error);
  617. put_device(dev);
  618. }
  619. void dpm_noirq_resume_devices(pm_message_t state)
  620. {
  621. struct device *dev;
  622. ktime_t starttime = ktime_get();
  623. trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
  624. mutex_lock(&dpm_list_mtx);
  625. pm_transition = state;
  626. /*
  627. * Advanced the async threads upfront,
  628. * in case the starting of async threads is
  629. * delayed by non-async resuming devices.
  630. */
  631. list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
  632. reinit_completion(&dev->power.completion);
  633. if (is_async(dev)) {
  634. get_device(dev);
  635. async_schedule(async_resume_noirq, dev);
  636. }
  637. }
  638. while (!list_empty(&dpm_noirq_list)) {
  639. dev = to_device(dpm_noirq_list.next);
  640. get_device(dev);
  641. list_move_tail(&dev->power.entry, &dpm_late_early_list);
  642. mutex_unlock(&dpm_list_mtx);
  643. if (!is_async(dev)) {
  644. int error;
  645. error = device_resume_noirq(dev, state, false);
  646. if (error) {
  647. suspend_stats.failed_resume_noirq++;
  648. dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
  649. dpm_save_failed_dev(dev_name(dev));
  650. pm_dev_err(dev, state, " noirq", error);
  651. }
  652. }
  653. mutex_lock(&dpm_list_mtx);
  654. put_device(dev);
  655. }
  656. mutex_unlock(&dpm_list_mtx);
  657. async_synchronize_full();
  658. dpm_show_time(starttime, state, 0, "noirq");
  659. trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
  660. }
  661. void dpm_noirq_end(void)
  662. {
  663. resume_device_irqs();
  664. device_wakeup_disarm_wake_irqs();
  665. cpuidle_resume();
  666. }
  667. /**
  668. * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
  669. * @state: PM transition of the system being carried out.
  670. *
  671. * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
  672. * allow device drivers' interrupt handlers to be called.
  673. */
  674. void dpm_resume_noirq(pm_message_t state)
  675. {
  676. dpm_noirq_resume_devices(state);
  677. dpm_noirq_end();
  678. }
  679. static pm_callback_t dpm_subsys_resume_early_cb(struct device *dev,
  680. pm_message_t state,
  681. const char **info_p)
  682. {
  683. pm_callback_t callback;
  684. const char *info;
  685. if (dev->pm_domain) {
  686. info = "early power domain ";
  687. callback = pm_late_early_op(&dev->pm_domain->ops, state);
  688. } else if (dev->type && dev->type->pm) {
  689. info = "early type ";
  690. callback = pm_late_early_op(dev->type->pm, state);
  691. } else if (dev->class && dev->class->pm) {
  692. info = "early class ";
  693. callback = pm_late_early_op(dev->class->pm, state);
  694. } else if (dev->bus && dev->bus->pm) {
  695. info = "early bus ";
  696. callback = pm_late_early_op(dev->bus->pm, state);
  697. } else {
  698. return NULL;
  699. }
  700. if (info_p)
  701. *info_p = info;
  702. return callback;
  703. }
  704. /**
  705. * device_resume_early - Execute an "early resume" callback for given device.
  706. * @dev: Device to handle.
  707. * @state: PM transition of the system being carried out.
  708. * @async: If true, the device is being resumed asynchronously.
  709. *
  710. * Runtime PM is disabled for @dev while this function is being executed.
  711. */
  712. static int device_resume_early(struct device *dev, pm_message_t state, bool async)
  713. {
  714. pm_callback_t callback;
  715. const char *info;
  716. int error = 0;
  717. TRACE_DEVICE(dev);
  718. TRACE_RESUME(0);
  719. if (dev->power.syscore || dev->power.direct_complete)
  720. goto Out;
  721. if (!dev->power.is_late_suspended)
  722. goto Out;
  723. dpm_wait_for_superior(dev, async);
  724. callback = dpm_subsys_resume_early_cb(dev, state, &info);
  725. if (!callback && dev->driver && dev->driver->pm) {
  726. info = "early driver ";
  727. callback = pm_late_early_op(dev->driver->pm, state);
  728. }
  729. error = dpm_run_callback(callback, dev, state, info);
  730. dev->power.is_late_suspended = false;
  731. Out:
  732. TRACE_RESUME(error);
  733. pm_runtime_enable(dev);
  734. complete_all(&dev->power.completion);
  735. return error;
  736. }
  737. static void async_resume_early(void *data, async_cookie_t cookie)
  738. {
  739. struct device *dev = (struct device *)data;
  740. int error;
  741. error = device_resume_early(dev, pm_transition, true);
  742. if (error)
  743. pm_dev_err(dev, pm_transition, " async", error);
  744. put_device(dev);
  745. }
  746. /**
  747. * dpm_resume_early - Execute "early resume" callbacks for all devices.
  748. * @state: PM transition of the system being carried out.
  749. */
  750. void dpm_resume_early(pm_message_t state)
  751. {
  752. struct device *dev;
  753. ktime_t starttime = ktime_get();
  754. trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
  755. mutex_lock(&dpm_list_mtx);
  756. pm_transition = state;
  757. /*
  758. * Advanced the async threads upfront,
  759. * in case the starting of async threads is
  760. * delayed by non-async resuming devices.
  761. */
  762. list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
  763. reinit_completion(&dev->power.completion);
  764. if (is_async(dev)) {
  765. get_device(dev);
  766. async_schedule(async_resume_early, dev);
  767. }
  768. }
  769. while (!list_empty(&dpm_late_early_list)) {
  770. dev = to_device(dpm_late_early_list.next);
  771. get_device(dev);
  772. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  773. mutex_unlock(&dpm_list_mtx);
  774. if (!is_async(dev)) {
  775. int error;
  776. error = device_resume_early(dev, state, false);
  777. if (error) {
  778. suspend_stats.failed_resume_early++;
  779. dpm_save_failed_step(SUSPEND_RESUME_EARLY);
  780. dpm_save_failed_dev(dev_name(dev));
  781. pm_dev_err(dev, state, " early", error);
  782. }
  783. }
  784. mutex_lock(&dpm_list_mtx);
  785. put_device(dev);
  786. }
  787. mutex_unlock(&dpm_list_mtx);
  788. async_synchronize_full();
  789. dpm_show_time(starttime, state, 0, "early");
  790. trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
  791. }
  792. /**
  793. * dpm_resume_start - Execute "noirq" and "early" device callbacks.
  794. * @state: PM transition of the system being carried out.
  795. */
  796. void dpm_resume_start(pm_message_t state)
  797. {
  798. dpm_resume_noirq(state);
  799. dpm_resume_early(state);
  800. }
  801. EXPORT_SYMBOL_GPL(dpm_resume_start);
  802. /**
  803. * device_resume - Execute "resume" callbacks for given device.
  804. * @dev: Device to handle.
  805. * @state: PM transition of the system being carried out.
  806. * @async: If true, the device is being resumed asynchronously.
  807. */
  808. static int device_resume(struct device *dev, pm_message_t state, bool async)
  809. {
  810. pm_callback_t callback = NULL;
  811. const char *info = NULL;
  812. int error = 0;
  813. DECLARE_DPM_WATCHDOG_ON_STACK(wd);
  814. TRACE_DEVICE(dev);
  815. TRACE_RESUME(0);
  816. if (dev->power.syscore)
  817. goto Complete;
  818. if (dev->power.direct_complete) {
  819. /* Match the pm_runtime_disable() in __device_suspend(). */
  820. pm_runtime_enable(dev);
  821. goto Complete;
  822. }
  823. dpm_wait_for_superior(dev, async);
  824. dpm_watchdog_set(&wd, dev);
  825. device_lock(dev);
  826. /*
  827. * This is a fib. But we'll allow new children to be added below
  828. * a resumed device, even if the device hasn't been completed yet.
  829. */
  830. dev->power.is_prepared = false;
  831. if (!dev->power.is_suspended)
  832. goto Unlock;
  833. if (dev->pm_domain) {
  834. info = "power domain ";
  835. callback = pm_op(&dev->pm_domain->ops, state);
  836. goto Driver;
  837. }
  838. if (dev->type && dev->type->pm) {
  839. info = "type ";
  840. callback = pm_op(dev->type->pm, state);
  841. goto Driver;
  842. }
  843. if (dev->class && dev->class->pm) {
  844. info = "class ";
  845. callback = pm_op(dev->class->pm, state);
  846. goto Driver;
  847. }
  848. if (dev->bus) {
  849. if (dev->bus->pm) {
  850. info = "bus ";
  851. callback = pm_op(dev->bus->pm, state);
  852. } else if (dev->bus->resume) {
  853. info = "legacy bus ";
  854. callback = dev->bus->resume;
  855. goto End;
  856. }
  857. }
  858. Driver:
  859. if (!callback && dev->driver && dev->driver->pm) {
  860. info = "driver ";
  861. callback = pm_op(dev->driver->pm, state);
  862. }
  863. End:
  864. error = dpm_run_callback(callback, dev, state, info);
  865. dev->power.is_suspended = false;
  866. Unlock:
  867. device_unlock(dev);
  868. dpm_watchdog_clear(&wd);
  869. Complete:
  870. complete_all(&dev->power.completion);
  871. TRACE_RESUME(error);
  872. return error;
  873. }
  874. static void async_resume(void *data, async_cookie_t cookie)
  875. {
  876. struct device *dev = (struct device *)data;
  877. int error;
  878. error = device_resume(dev, pm_transition, true);
  879. if (error)
  880. pm_dev_err(dev, pm_transition, " async", error);
  881. put_device(dev);
  882. }
  883. /**
  884. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  885. * @state: PM transition of the system being carried out.
  886. *
  887. * Execute the appropriate "resume" callback for all devices whose status
  888. * indicates that they are suspended.
  889. */
  890. void dpm_resume(pm_message_t state)
  891. {
  892. struct device *dev;
  893. ktime_t starttime = ktime_get();
  894. trace_suspend_resume(TPS("dpm_resume"), state.event, true);
  895. might_sleep();
  896. mutex_lock(&dpm_list_mtx);
  897. pm_transition = state;
  898. async_error = 0;
  899. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  900. reinit_completion(&dev->power.completion);
  901. if (is_async(dev)) {
  902. get_device(dev);
  903. async_schedule(async_resume, dev);
  904. }
  905. }
  906. while (!list_empty(&dpm_suspended_list)) {
  907. dev = to_device(dpm_suspended_list.next);
  908. get_device(dev);
  909. if (!is_async(dev)) {
  910. int error;
  911. mutex_unlock(&dpm_list_mtx);
  912. error = device_resume(dev, state, false);
  913. if (error) {
  914. suspend_stats.failed_resume++;
  915. dpm_save_failed_step(SUSPEND_RESUME);
  916. dpm_save_failed_dev(dev_name(dev));
  917. pm_dev_err(dev, state, "", error);
  918. }
  919. mutex_lock(&dpm_list_mtx);
  920. }
  921. if (!list_empty(&dev->power.entry))
  922. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  923. put_device(dev);
  924. }
  925. mutex_unlock(&dpm_list_mtx);
  926. async_synchronize_full();
  927. dpm_show_time(starttime, state, 0, NULL);
  928. cpufreq_resume();
  929. trace_suspend_resume(TPS("dpm_resume"), state.event, false);
  930. }
  931. /**
  932. * device_complete - Complete a PM transition for given device.
  933. * @dev: Device to handle.
  934. * @state: PM transition of the system being carried out.
  935. */
  936. static void device_complete(struct device *dev, pm_message_t state)
  937. {
  938. void (*callback)(struct device *) = NULL;
  939. const char *info = NULL;
  940. if (dev->power.syscore)
  941. return;
  942. device_lock(dev);
  943. if (dev->pm_domain) {
  944. info = "completing power domain ";
  945. callback = dev->pm_domain->ops.complete;
  946. } else if (dev->type && dev->type->pm) {
  947. info = "completing type ";
  948. callback = dev->type->pm->complete;
  949. } else if (dev->class && dev->class->pm) {
  950. info = "completing class ";
  951. callback = dev->class->pm->complete;
  952. } else if (dev->bus && dev->bus->pm) {
  953. info = "completing bus ";
  954. callback = dev->bus->pm->complete;
  955. }
  956. if (!callback && dev->driver && dev->driver->pm) {
  957. info = "completing driver ";
  958. callback = dev->driver->pm->complete;
  959. }
  960. if (callback) {
  961. pm_dev_dbg(dev, state, info);
  962. callback(dev);
  963. }
  964. device_unlock(dev);
  965. pm_runtime_put(dev);
  966. }
  967. /**
  968. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  969. * @state: PM transition of the system being carried out.
  970. *
  971. * Execute the ->complete() callbacks for all devices whose PM status is not
  972. * DPM_ON (this allows new devices to be registered).
  973. */
  974. void dpm_complete(pm_message_t state)
  975. {
  976. struct list_head list;
  977. trace_suspend_resume(TPS("dpm_complete"), state.event, true);
  978. might_sleep();
  979. INIT_LIST_HEAD(&list);
  980. mutex_lock(&dpm_list_mtx);
  981. while (!list_empty(&dpm_prepared_list)) {
  982. struct device *dev = to_device(dpm_prepared_list.prev);
  983. get_device(dev);
  984. dev->power.is_prepared = false;
  985. list_move(&dev->power.entry, &list);
  986. mutex_unlock(&dpm_list_mtx);
  987. trace_device_pm_callback_start(dev, "", state.event);
  988. device_complete(dev, state);
  989. trace_device_pm_callback_end(dev, 0);
  990. mutex_lock(&dpm_list_mtx);
  991. put_device(dev);
  992. }
  993. list_splice(&list, &dpm_list);
  994. mutex_unlock(&dpm_list_mtx);
  995. /* Allow device probing and trigger re-probing of deferred devices */
  996. device_unblock_probing();
  997. trace_suspend_resume(TPS("dpm_complete"), state.event, false);
  998. }
  999. /**
  1000. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  1001. * @state: PM transition of the system being carried out.
  1002. *
  1003. * Execute "resume" callbacks for all devices and complete the PM transition of
  1004. * the system.
  1005. */
  1006. void dpm_resume_end(pm_message_t state)
  1007. {
  1008. dpm_resume(state);
  1009. dpm_complete(state);
  1010. }
  1011. EXPORT_SYMBOL_GPL(dpm_resume_end);
  1012. /*------------------------- Suspend routines -------------------------*/
  1013. /**
  1014. * resume_event - Return a "resume" message for given "suspend" sleep state.
  1015. * @sleep_state: PM message representing a sleep state.
  1016. *
  1017. * Return a PM message representing the resume event corresponding to given
  1018. * sleep state.
  1019. */
  1020. static pm_message_t resume_event(pm_message_t sleep_state)
  1021. {
  1022. switch (sleep_state.event) {
  1023. case PM_EVENT_SUSPEND:
  1024. return PMSG_RESUME;
  1025. case PM_EVENT_FREEZE:
  1026. case PM_EVENT_QUIESCE:
  1027. return PMSG_RECOVER;
  1028. case PM_EVENT_HIBERNATE:
  1029. return PMSG_RESTORE;
  1030. }
  1031. return PMSG_ON;
  1032. }
  1033. static void dpm_superior_set_must_resume(struct device *dev)
  1034. {
  1035. struct device_link *link;
  1036. int idx;
  1037. if (dev->parent)
  1038. dev->parent->power.must_resume = true;
  1039. idx = device_links_read_lock();
  1040. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
  1041. link->supplier->power.must_resume = true;
  1042. device_links_read_unlock(idx);
  1043. }
  1044. static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
  1045. pm_message_t state,
  1046. const char **info_p)
  1047. {
  1048. pm_callback_t callback;
  1049. const char *info;
  1050. if (dev->pm_domain) {
  1051. info = "noirq power domain ";
  1052. callback = pm_noirq_op(&dev->pm_domain->ops, state);
  1053. } else if (dev->type && dev->type->pm) {
  1054. info = "noirq type ";
  1055. callback = pm_noirq_op(dev->type->pm, state);
  1056. } else if (dev->class && dev->class->pm) {
  1057. info = "noirq class ";
  1058. callback = pm_noirq_op(dev->class->pm, state);
  1059. } else if (dev->bus && dev->bus->pm) {
  1060. info = "noirq bus ";
  1061. callback = pm_noirq_op(dev->bus->pm, state);
  1062. } else {
  1063. return NULL;
  1064. }
  1065. if (info_p)
  1066. *info_p = info;
  1067. return callback;
  1068. }
  1069. static bool device_must_resume(struct device *dev, pm_message_t state,
  1070. bool no_subsys_suspend_noirq)
  1071. {
  1072. pm_message_t resume_msg = resume_event(state);
  1073. /*
  1074. * If all of the device driver's "noirq", "late" and "early" callbacks
  1075. * are invoked directly by the core, the decision to allow the device to
  1076. * stay in suspend can be based on its current runtime PM status and its
  1077. * wakeup settings.
  1078. */
  1079. if (no_subsys_suspend_noirq &&
  1080. !dpm_subsys_suspend_late_cb(dev, state, NULL) &&
  1081. !dpm_subsys_resume_early_cb(dev, resume_msg, NULL) &&
  1082. !dpm_subsys_resume_noirq_cb(dev, resume_msg, NULL))
  1083. return !pm_runtime_status_suspended(dev) &&
  1084. (resume_msg.event != PM_EVENT_RESUME ||
  1085. (device_can_wakeup(dev) && !device_may_wakeup(dev)));
  1086. /*
  1087. * The only safe strategy here is to require that if the device may not
  1088. * be left in suspend, resume callbacks must be invoked for it.
  1089. */
  1090. return !dev->power.may_skip_resume;
  1091. }
  1092. /**
  1093. * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
  1094. * @dev: Device to handle.
  1095. * @state: PM transition of the system being carried out.
  1096. * @async: If true, the device is being suspended asynchronously.
  1097. *
  1098. * The driver of @dev will not receive interrupts while this function is being
  1099. * executed.
  1100. */
  1101. static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
  1102. {
  1103. pm_callback_t callback;
  1104. const char *info;
  1105. bool no_subsys_cb = false;
  1106. int error = 0;
  1107. TRACE_DEVICE(dev);
  1108. TRACE_SUSPEND(0);
  1109. dpm_wait_for_subordinate(dev, async);
  1110. if (async_error)
  1111. goto Complete;
  1112. if (pm_wakeup_pending()) {
  1113. async_error = -EBUSY;
  1114. goto Complete;
  1115. }
  1116. if (dev->power.syscore || dev->power.direct_complete)
  1117. goto Complete;
  1118. callback = dpm_subsys_suspend_noirq_cb(dev, state, &info);
  1119. if (callback)
  1120. goto Run;
  1121. no_subsys_cb = !dpm_subsys_suspend_late_cb(dev, state, NULL);
  1122. if (dev_pm_smart_suspend_and_suspended(dev) && no_subsys_cb)
  1123. goto Skip;
  1124. if (dev->driver && dev->driver->pm) {
  1125. info = "noirq driver ";
  1126. callback = pm_noirq_op(dev->driver->pm, state);
  1127. }
  1128. Run:
  1129. error = dpm_run_callback(callback, dev, state, info);
  1130. if (error) {
  1131. async_error = error;
  1132. goto Complete;
  1133. }
  1134. Skip:
  1135. dev->power.is_noirq_suspended = true;
  1136. if (dev_pm_test_driver_flags(dev, DPM_FLAG_LEAVE_SUSPENDED)) {
  1137. dev->power.must_resume = dev->power.must_resume ||
  1138. atomic_read(&dev->power.usage_count) > 1 ||
  1139. device_must_resume(dev, state, no_subsys_cb);
  1140. } else {
  1141. dev->power.must_resume = true;
  1142. }
  1143. if (dev->power.must_resume)
  1144. dpm_superior_set_must_resume(dev);
  1145. Complete:
  1146. complete_all(&dev->power.completion);
  1147. TRACE_SUSPEND(error);
  1148. return error;
  1149. }
  1150. static void async_suspend_noirq(void *data, async_cookie_t cookie)
  1151. {
  1152. struct device *dev = (struct device *)data;
  1153. int error;
  1154. error = __device_suspend_noirq(dev, pm_transition, true);
  1155. if (error) {
  1156. dpm_save_failed_dev(dev_name(dev));
  1157. pm_dev_err(dev, pm_transition, " async", error);
  1158. }
  1159. put_device(dev);
  1160. }
  1161. static int device_suspend_noirq(struct device *dev)
  1162. {
  1163. reinit_completion(&dev->power.completion);
  1164. if (is_async(dev)) {
  1165. get_device(dev);
  1166. async_schedule(async_suspend_noirq, dev);
  1167. return 0;
  1168. }
  1169. return __device_suspend_noirq(dev, pm_transition, false);
  1170. }
  1171. void dpm_noirq_begin(void)
  1172. {
  1173. cpuidle_pause();
  1174. device_wakeup_arm_wake_irqs();
  1175. suspend_device_irqs();
  1176. }
  1177. int dpm_noirq_suspend_devices(pm_message_t state)
  1178. {
  1179. ktime_t starttime = ktime_get();
  1180. int error = 0;
  1181. trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
  1182. mutex_lock(&dpm_list_mtx);
  1183. pm_transition = state;
  1184. async_error = 0;
  1185. while (!list_empty(&dpm_late_early_list)) {
  1186. struct device *dev = to_device(dpm_late_early_list.prev);
  1187. get_device(dev);
  1188. mutex_unlock(&dpm_list_mtx);
  1189. error = device_suspend_noirq(dev);
  1190. mutex_lock(&dpm_list_mtx);
  1191. if (error) {
  1192. pm_dev_err(dev, state, " noirq", error);
  1193. dpm_save_failed_dev(dev_name(dev));
  1194. put_device(dev);
  1195. break;
  1196. }
  1197. if (!list_empty(&dev->power.entry))
  1198. list_move(&dev->power.entry, &dpm_noirq_list);
  1199. put_device(dev);
  1200. if (async_error)
  1201. break;
  1202. }
  1203. mutex_unlock(&dpm_list_mtx);
  1204. async_synchronize_full();
  1205. if (!error)
  1206. error = async_error;
  1207. if (error) {
  1208. suspend_stats.failed_suspend_noirq++;
  1209. dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
  1210. }
  1211. dpm_show_time(starttime, state, error, "noirq");
  1212. trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
  1213. return error;
  1214. }
  1215. /**
  1216. * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
  1217. * @state: PM transition of the system being carried out.
  1218. *
  1219. * Prevent device drivers' interrupt handlers from being called and invoke
  1220. * "noirq" suspend callbacks for all non-sysdev devices.
  1221. */
  1222. int dpm_suspend_noirq(pm_message_t state)
  1223. {
  1224. int ret;
  1225. dpm_noirq_begin();
  1226. ret = dpm_noirq_suspend_devices(state);
  1227. if (ret)
  1228. dpm_resume_noirq(resume_event(state));
  1229. return ret;
  1230. }
  1231. static void dpm_propagate_wakeup_to_parent(struct device *dev)
  1232. {
  1233. struct device *parent = dev->parent;
  1234. if (!parent)
  1235. return;
  1236. spin_lock_irq(&parent->power.lock);
  1237. if (dev->power.wakeup_path && !parent->power.ignore_children)
  1238. parent->power.wakeup_path = true;
  1239. spin_unlock_irq(&parent->power.lock);
  1240. }
  1241. static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
  1242. pm_message_t state,
  1243. const char **info_p)
  1244. {
  1245. pm_callback_t callback;
  1246. const char *info;
  1247. if (dev->pm_domain) {
  1248. info = "late power domain ";
  1249. callback = pm_late_early_op(&dev->pm_domain->ops, state);
  1250. } else if (dev->type && dev->type->pm) {
  1251. info = "late type ";
  1252. callback = pm_late_early_op(dev->type->pm, state);
  1253. } else if (dev->class && dev->class->pm) {
  1254. info = "late class ";
  1255. callback = pm_late_early_op(dev->class->pm, state);
  1256. } else if (dev->bus && dev->bus->pm) {
  1257. info = "late bus ";
  1258. callback = pm_late_early_op(dev->bus->pm, state);
  1259. } else {
  1260. return NULL;
  1261. }
  1262. if (info_p)
  1263. *info_p = info;
  1264. return callback;
  1265. }
  1266. /**
  1267. * __device_suspend_late - Execute a "late suspend" callback for given device.
  1268. * @dev: Device to handle.
  1269. * @state: PM transition of the system being carried out.
  1270. * @async: If true, the device is being suspended asynchronously.
  1271. *
  1272. * Runtime PM is disabled for @dev while this function is being executed.
  1273. */
  1274. static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
  1275. {
  1276. pm_callback_t callback;
  1277. const char *info;
  1278. int error = 0;
  1279. TRACE_DEVICE(dev);
  1280. TRACE_SUSPEND(0);
  1281. __pm_runtime_disable(dev, false);
  1282. dpm_wait_for_subordinate(dev, async);
  1283. if (async_error)
  1284. goto Complete;
  1285. if (pm_wakeup_pending()) {
  1286. async_error = -EBUSY;
  1287. goto Complete;
  1288. }
  1289. if (dev->power.syscore || dev->power.direct_complete)
  1290. goto Complete;
  1291. callback = dpm_subsys_suspend_late_cb(dev, state, &info);
  1292. if (callback)
  1293. goto Run;
  1294. if (dev_pm_smart_suspend_and_suspended(dev) &&
  1295. !dpm_subsys_suspend_noirq_cb(dev, state, NULL))
  1296. goto Skip;
  1297. if (dev->driver && dev->driver->pm) {
  1298. info = "late driver ";
  1299. callback = pm_late_early_op(dev->driver->pm, state);
  1300. }
  1301. Run:
  1302. error = dpm_run_callback(callback, dev, state, info);
  1303. if (error) {
  1304. async_error = error;
  1305. goto Complete;
  1306. }
  1307. dpm_propagate_wakeup_to_parent(dev);
  1308. Skip:
  1309. dev->power.is_late_suspended = true;
  1310. Complete:
  1311. TRACE_SUSPEND(error);
  1312. complete_all(&dev->power.completion);
  1313. return error;
  1314. }
  1315. static void async_suspend_late(void *data, async_cookie_t cookie)
  1316. {
  1317. struct device *dev = (struct device *)data;
  1318. int error;
  1319. error = __device_suspend_late(dev, pm_transition, true);
  1320. if (error) {
  1321. dpm_save_failed_dev(dev_name(dev));
  1322. pm_dev_err(dev, pm_transition, " async", error);
  1323. }
  1324. put_device(dev);
  1325. }
  1326. static int device_suspend_late(struct device *dev)
  1327. {
  1328. reinit_completion(&dev->power.completion);
  1329. if (is_async(dev)) {
  1330. get_device(dev);
  1331. async_schedule(async_suspend_late, dev);
  1332. return 0;
  1333. }
  1334. return __device_suspend_late(dev, pm_transition, false);
  1335. }
  1336. /**
  1337. * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
  1338. * @state: PM transition of the system being carried out.
  1339. */
  1340. int dpm_suspend_late(pm_message_t state)
  1341. {
  1342. ktime_t starttime = ktime_get();
  1343. int error = 0;
  1344. trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
  1345. mutex_lock(&dpm_list_mtx);
  1346. pm_transition = state;
  1347. async_error = 0;
  1348. while (!list_empty(&dpm_suspended_list)) {
  1349. struct device *dev = to_device(dpm_suspended_list.prev);
  1350. get_device(dev);
  1351. mutex_unlock(&dpm_list_mtx);
  1352. error = device_suspend_late(dev);
  1353. mutex_lock(&dpm_list_mtx);
  1354. if (!list_empty(&dev->power.entry))
  1355. list_move(&dev->power.entry, &dpm_late_early_list);
  1356. if (error) {
  1357. pm_dev_err(dev, state, " late", error);
  1358. dpm_save_failed_dev(dev_name(dev));
  1359. put_device(dev);
  1360. break;
  1361. }
  1362. put_device(dev);
  1363. if (async_error)
  1364. break;
  1365. }
  1366. mutex_unlock(&dpm_list_mtx);
  1367. async_synchronize_full();
  1368. if (!error)
  1369. error = async_error;
  1370. if (error) {
  1371. suspend_stats.failed_suspend_late++;
  1372. dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
  1373. dpm_resume_early(resume_event(state));
  1374. }
  1375. dpm_show_time(starttime, state, error, "late");
  1376. trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
  1377. return error;
  1378. }
  1379. /**
  1380. * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
  1381. * @state: PM transition of the system being carried out.
  1382. */
  1383. int dpm_suspend_end(pm_message_t state)
  1384. {
  1385. int error = dpm_suspend_late(state);
  1386. if (error)
  1387. return error;
  1388. error = dpm_suspend_noirq(state);
  1389. if (error) {
  1390. dpm_resume_early(resume_event(state));
  1391. return error;
  1392. }
  1393. return 0;
  1394. }
  1395. EXPORT_SYMBOL_GPL(dpm_suspend_end);
  1396. /**
  1397. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  1398. * @dev: Device to suspend.
  1399. * @state: PM transition of the system being carried out.
  1400. * @cb: Suspend callback to execute.
  1401. * @info: string description of caller.
  1402. */
  1403. static int legacy_suspend(struct device *dev, pm_message_t state,
  1404. int (*cb)(struct device *dev, pm_message_t state),
  1405. const char *info)
  1406. {
  1407. int error;
  1408. ktime_t calltime;
  1409. calltime = initcall_debug_start(dev, cb);
  1410. trace_device_pm_callback_start(dev, info, state.event);
  1411. error = cb(dev, state);
  1412. trace_device_pm_callback_end(dev, error);
  1413. suspend_report_result(cb, error);
  1414. initcall_debug_report(dev, calltime, cb, error);
  1415. return error;
  1416. }
  1417. static void dpm_clear_superiors_direct_complete(struct device *dev)
  1418. {
  1419. struct device_link *link;
  1420. int idx;
  1421. if (dev->parent) {
  1422. spin_lock_irq(&dev->parent->power.lock);
  1423. dev->parent->power.direct_complete = false;
  1424. spin_unlock_irq(&dev->parent->power.lock);
  1425. }
  1426. idx = device_links_read_lock();
  1427. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
  1428. spin_lock_irq(&link->supplier->power.lock);
  1429. link->supplier->power.direct_complete = false;
  1430. spin_unlock_irq(&link->supplier->power.lock);
  1431. }
  1432. device_links_read_unlock(idx);
  1433. }
  1434. /**
  1435. * __device_suspend - Execute "suspend" callbacks for given device.
  1436. * @dev: Device to handle.
  1437. * @state: PM transition of the system being carried out.
  1438. * @async: If true, the device is being suspended asynchronously.
  1439. */
  1440. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  1441. {
  1442. pm_callback_t callback = NULL;
  1443. const char *info = NULL;
  1444. int error = 0;
  1445. DECLARE_DPM_WATCHDOG_ON_STACK(wd);
  1446. TRACE_DEVICE(dev);
  1447. TRACE_SUSPEND(0);
  1448. dpm_wait_for_subordinate(dev, async);
  1449. if (async_error) {
  1450. dev->power.direct_complete = false;
  1451. goto Complete;
  1452. }
  1453. /*
  1454. * If a device configured to wake up the system from sleep states
  1455. * has been suspended at run time and there's a resume request pending
  1456. * for it, this is equivalent to the device signaling wakeup, so the
  1457. * system suspend operation should be aborted.
  1458. */
  1459. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  1460. pm_wakeup_event(dev, 0);
  1461. if (pm_wakeup_pending()) {
  1462. dev->power.direct_complete = false;
  1463. async_error = -EBUSY;
  1464. goto Complete;
  1465. }
  1466. if (dev->power.syscore)
  1467. goto Complete;
  1468. if (dev->power.direct_complete) {
  1469. if (pm_runtime_status_suspended(dev)) {
  1470. pm_runtime_disable(dev);
  1471. if (pm_runtime_status_suspended(dev))
  1472. goto Complete;
  1473. pm_runtime_enable(dev);
  1474. }
  1475. dev->power.direct_complete = false;
  1476. }
  1477. dev->power.may_skip_resume = false;
  1478. dev->power.must_resume = false;
  1479. dpm_watchdog_set(&wd, dev);
  1480. device_lock(dev);
  1481. if (dev->pm_domain) {
  1482. info = "power domain ";
  1483. callback = pm_op(&dev->pm_domain->ops, state);
  1484. goto Run;
  1485. }
  1486. if (dev->type && dev->type->pm) {
  1487. info = "type ";
  1488. callback = pm_op(dev->type->pm, state);
  1489. goto Run;
  1490. }
  1491. if (dev->class && dev->class->pm) {
  1492. info = "class ";
  1493. callback = pm_op(dev->class->pm, state);
  1494. goto Run;
  1495. }
  1496. if (dev->bus) {
  1497. if (dev->bus->pm) {
  1498. info = "bus ";
  1499. callback = pm_op(dev->bus->pm, state);
  1500. } else if (dev->bus->suspend) {
  1501. pm_dev_dbg(dev, state, "legacy bus ");
  1502. error = legacy_suspend(dev, state, dev->bus->suspend,
  1503. "legacy bus ");
  1504. goto End;
  1505. }
  1506. }
  1507. Run:
  1508. if (!callback && dev->driver && dev->driver->pm) {
  1509. info = "driver ";
  1510. callback = pm_op(dev->driver->pm, state);
  1511. }
  1512. error = dpm_run_callback(callback, dev, state, info);
  1513. End:
  1514. if (!error) {
  1515. dev->power.is_suspended = true;
  1516. if (device_may_wakeup(dev))
  1517. dev->power.wakeup_path = true;
  1518. dpm_propagate_wakeup_to_parent(dev);
  1519. dpm_clear_superiors_direct_complete(dev);
  1520. }
  1521. device_unlock(dev);
  1522. dpm_watchdog_clear(&wd);
  1523. Complete:
  1524. if (error)
  1525. async_error = error;
  1526. complete_all(&dev->power.completion);
  1527. TRACE_SUSPEND(error);
  1528. return error;
  1529. }
  1530. static void async_suspend(void *data, async_cookie_t cookie)
  1531. {
  1532. struct device *dev = (struct device *)data;
  1533. int error;
  1534. error = __device_suspend(dev, pm_transition, true);
  1535. if (error) {
  1536. dpm_save_failed_dev(dev_name(dev));
  1537. pm_dev_err(dev, pm_transition, " async", error);
  1538. }
  1539. put_device(dev);
  1540. }
  1541. static int device_suspend(struct device *dev)
  1542. {
  1543. reinit_completion(&dev->power.completion);
  1544. if (is_async(dev)) {
  1545. get_device(dev);
  1546. async_schedule(async_suspend, dev);
  1547. return 0;
  1548. }
  1549. return __device_suspend(dev, pm_transition, false);
  1550. }
  1551. /**
  1552. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  1553. * @state: PM transition of the system being carried out.
  1554. */
  1555. int dpm_suspend(pm_message_t state)
  1556. {
  1557. ktime_t starttime = ktime_get();
  1558. int error = 0;
  1559. trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
  1560. might_sleep();
  1561. cpufreq_suspend();
  1562. mutex_lock(&dpm_list_mtx);
  1563. pm_transition = state;
  1564. async_error = 0;
  1565. while (!list_empty(&dpm_prepared_list)) {
  1566. struct device *dev = to_device(dpm_prepared_list.prev);
  1567. get_device(dev);
  1568. mutex_unlock(&dpm_list_mtx);
  1569. error = device_suspend(dev);
  1570. mutex_lock(&dpm_list_mtx);
  1571. if (error) {
  1572. pm_dev_err(dev, state, "", error);
  1573. dpm_save_failed_dev(dev_name(dev));
  1574. put_device(dev);
  1575. break;
  1576. }
  1577. if (!list_empty(&dev->power.entry))
  1578. list_move(&dev->power.entry, &dpm_suspended_list);
  1579. put_device(dev);
  1580. if (async_error)
  1581. break;
  1582. }
  1583. mutex_unlock(&dpm_list_mtx);
  1584. async_synchronize_full();
  1585. if (!error)
  1586. error = async_error;
  1587. if (error) {
  1588. suspend_stats.failed_suspend++;
  1589. dpm_save_failed_step(SUSPEND_SUSPEND);
  1590. }
  1591. dpm_show_time(starttime, state, error, NULL);
  1592. trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
  1593. return error;
  1594. }
  1595. /**
  1596. * device_prepare - Prepare a device for system power transition.
  1597. * @dev: Device to handle.
  1598. * @state: PM transition of the system being carried out.
  1599. *
  1600. * Execute the ->prepare() callback(s) for given device. No new children of the
  1601. * device may be registered after this function has returned.
  1602. */
  1603. static int device_prepare(struct device *dev, pm_message_t state)
  1604. {
  1605. int (*callback)(struct device *) = NULL;
  1606. int ret = 0;
  1607. if (dev->power.syscore)
  1608. return 0;
  1609. WARN_ON(!pm_runtime_enabled(dev) &&
  1610. dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
  1611. DPM_FLAG_LEAVE_SUSPENDED));
  1612. /*
  1613. * If a device's parent goes into runtime suspend at the wrong time,
  1614. * it won't be possible to resume the device. To prevent this we
  1615. * block runtime suspend here, during the prepare phase, and allow
  1616. * it again during the complete phase.
  1617. */
  1618. pm_runtime_get_noresume(dev);
  1619. device_lock(dev);
  1620. dev->power.wakeup_path = false;
  1621. if (dev->power.no_pm_callbacks)
  1622. goto unlock;
  1623. if (dev->pm_domain)
  1624. callback = dev->pm_domain->ops.prepare;
  1625. else if (dev->type && dev->type->pm)
  1626. callback = dev->type->pm->prepare;
  1627. else if (dev->class && dev->class->pm)
  1628. callback = dev->class->pm->prepare;
  1629. else if (dev->bus && dev->bus->pm)
  1630. callback = dev->bus->pm->prepare;
  1631. if (!callback && dev->driver && dev->driver->pm)
  1632. callback = dev->driver->pm->prepare;
  1633. if (callback)
  1634. ret = callback(dev);
  1635. unlock:
  1636. device_unlock(dev);
  1637. if (ret < 0) {
  1638. suspend_report_result(callback, ret);
  1639. pm_runtime_put(dev);
  1640. return ret;
  1641. }
  1642. /*
  1643. * A positive return value from ->prepare() means "this device appears
  1644. * to be runtime-suspended and its state is fine, so if it really is
  1645. * runtime-suspended, you can leave it in that state provided that you
  1646. * will do the same thing with all of its descendants". This only
  1647. * applies to suspend transitions, however.
  1648. */
  1649. spin_lock_irq(&dev->power.lock);
  1650. dev->power.direct_complete = state.event == PM_EVENT_SUSPEND &&
  1651. ((pm_runtime_suspended(dev) && ret > 0) ||
  1652. dev->power.no_pm_callbacks) &&
  1653. !dev_pm_test_driver_flags(dev, DPM_FLAG_NEVER_SKIP);
  1654. spin_unlock_irq(&dev->power.lock);
  1655. return 0;
  1656. }
  1657. /**
  1658. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  1659. * @state: PM transition of the system being carried out.
  1660. *
  1661. * Execute the ->prepare() callback(s) for all devices.
  1662. */
  1663. int dpm_prepare(pm_message_t state)
  1664. {
  1665. int error = 0;
  1666. trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
  1667. might_sleep();
  1668. /*
  1669. * Give a chance for the known devices to complete their probes, before
  1670. * disable probing of devices. This sync point is important at least
  1671. * at boot time + hibernation restore.
  1672. */
  1673. wait_for_device_probe();
  1674. /*
  1675. * It is unsafe if probing of devices will happen during suspend or
  1676. * hibernation and system behavior will be unpredictable in this case.
  1677. * So, let's prohibit device's probing here and defer their probes
  1678. * instead. The normal behavior will be restored in dpm_complete().
  1679. */
  1680. device_block_probing();
  1681. mutex_lock(&dpm_list_mtx);
  1682. while (!list_empty(&dpm_list)) {
  1683. struct device *dev = to_device(dpm_list.next);
  1684. get_device(dev);
  1685. mutex_unlock(&dpm_list_mtx);
  1686. trace_device_pm_callback_start(dev, "", state.event);
  1687. error = device_prepare(dev, state);
  1688. trace_device_pm_callback_end(dev, error);
  1689. mutex_lock(&dpm_list_mtx);
  1690. if (error) {
  1691. if (error == -EAGAIN) {
  1692. put_device(dev);
  1693. error = 0;
  1694. continue;
  1695. }
  1696. printk(KERN_INFO "PM: Device %s not prepared "
  1697. "for power transition: code %d\n",
  1698. dev_name(dev), error);
  1699. put_device(dev);
  1700. break;
  1701. }
  1702. dev->power.is_prepared = true;
  1703. if (!list_empty(&dev->power.entry))
  1704. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  1705. put_device(dev);
  1706. }
  1707. mutex_unlock(&dpm_list_mtx);
  1708. trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
  1709. return error;
  1710. }
  1711. /**
  1712. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  1713. * @state: PM transition of the system being carried out.
  1714. *
  1715. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  1716. * callbacks for them.
  1717. */
  1718. int dpm_suspend_start(pm_message_t state)
  1719. {
  1720. int error;
  1721. error = dpm_prepare(state);
  1722. if (error) {
  1723. suspend_stats.failed_prepare++;
  1724. dpm_save_failed_step(SUSPEND_PREPARE);
  1725. } else
  1726. error = dpm_suspend(state);
  1727. return error;
  1728. }
  1729. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  1730. void __suspend_report_result(const char *function, void *fn, int ret)
  1731. {
  1732. if (ret)
  1733. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  1734. }
  1735. EXPORT_SYMBOL_GPL(__suspend_report_result);
  1736. /**
  1737. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  1738. * @dev: Device to wait for.
  1739. * @subordinate: Device that needs to wait for @dev.
  1740. */
  1741. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  1742. {
  1743. dpm_wait(dev, subordinate->power.async_suspend);
  1744. return async_error;
  1745. }
  1746. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
  1747. /**
  1748. * dpm_for_each_dev - device iterator.
  1749. * @data: data for the callback.
  1750. * @fn: function to be called for each device.
  1751. *
  1752. * Iterate over devices in dpm_list, and call @fn for each device,
  1753. * passing it @data.
  1754. */
  1755. void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
  1756. {
  1757. struct device *dev;
  1758. if (!fn)
  1759. return;
  1760. device_pm_lock();
  1761. list_for_each_entry(dev, &dpm_list, power.entry)
  1762. fn(dev, data);
  1763. device_pm_unlock();
  1764. }
  1765. EXPORT_SYMBOL_GPL(dpm_for_each_dev);
  1766. static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
  1767. {
  1768. if (!ops)
  1769. return true;
  1770. return !ops->prepare &&
  1771. !ops->suspend &&
  1772. !ops->suspend_late &&
  1773. !ops->suspend_noirq &&
  1774. !ops->resume_noirq &&
  1775. !ops->resume_early &&
  1776. !ops->resume &&
  1777. !ops->complete;
  1778. }
  1779. void device_pm_check_callbacks(struct device *dev)
  1780. {
  1781. spin_lock_irq(&dev->power.lock);
  1782. dev->power.no_pm_callbacks =
  1783. (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
  1784. !dev->bus->suspend && !dev->bus->resume)) &&
  1785. (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
  1786. (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
  1787. (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
  1788. (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
  1789. !dev->driver->suspend && !dev->driver->resume));
  1790. spin_unlock_irq(&dev->power.lock);
  1791. }
  1792. bool dev_pm_smart_suspend_and_suspended(struct device *dev)
  1793. {
  1794. return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) &&
  1795. pm_runtime_status_suspended(dev);
  1796. }