main.c 53 KB

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