main.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. /*
  2. * drivers/base/power/main.c - Where the driver meets power management.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. *
  10. * The driver model core calls device_pm_add() when a device is registered.
  11. * This will initialize the embedded device_pm_info object in the device
  12. * and add it to the list of power-controlled devices. sysfs entries for
  13. * controlling device power management will also be added.
  14. *
  15. * A separate list is used for keeping track of power info, because the power
  16. * domain dependencies may differ from the ancestral dependencies that the
  17. * subsystem list maintains.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/pm-trace.h>
  26. #include <linux/pm_wakeirq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sched.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 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. mutex_lock(&dpm_list_mtx);
  117. if (dev->parent && dev->parent->power.is_prepared)
  118. dev_warn(dev, "parent %s should not be sleeping\n",
  119. dev_name(dev->parent));
  120. list_add_tail(&dev->power.entry, &dpm_list);
  121. mutex_unlock(&dpm_list_mtx);
  122. }
  123. /**
  124. * device_pm_remove - Remove a device from the PM core's list of active devices.
  125. * @dev: Device to be removed from the list.
  126. */
  127. void device_pm_remove(struct device *dev)
  128. {
  129. pr_debug("PM: Removing info for %s:%s\n",
  130. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  131. complete_all(&dev->power.completion);
  132. mutex_lock(&dpm_list_mtx);
  133. list_del_init(&dev->power.entry);
  134. mutex_unlock(&dpm_list_mtx);
  135. device_wakeup_disable(dev);
  136. pm_runtime_remove(dev);
  137. }
  138. /**
  139. * device_pm_move_before - Move device in the PM core's list of active devices.
  140. * @deva: Device to move in dpm_list.
  141. * @devb: Device @deva should come before.
  142. */
  143. void device_pm_move_before(struct device *deva, struct device *devb)
  144. {
  145. pr_debug("PM: Moving %s:%s before %s:%s\n",
  146. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  147. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  148. /* Delete deva from dpm_list and reinsert before devb. */
  149. list_move_tail(&deva->power.entry, &devb->power.entry);
  150. }
  151. /**
  152. * device_pm_move_after - Move device in the PM core's list of active devices.
  153. * @deva: Device to move in dpm_list.
  154. * @devb: Device @deva should come after.
  155. */
  156. void device_pm_move_after(struct device *deva, struct device *devb)
  157. {
  158. pr_debug("PM: Moving %s:%s after %s:%s\n",
  159. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  160. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  161. /* Delete deva from dpm_list and reinsert after devb. */
  162. list_move(&deva->power.entry, &devb->power.entry);
  163. }
  164. /**
  165. * device_pm_move_last - Move device to end of the PM core's list of devices.
  166. * @dev: Device to move in dpm_list.
  167. */
  168. void device_pm_move_last(struct device *dev)
  169. {
  170. pr_debug("PM: Moving %s:%s to end of list\n",
  171. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  172. list_move_tail(&dev->power.entry, &dpm_list);
  173. }
  174. static ktime_t initcall_debug_start(struct device *dev)
  175. {
  176. ktime_t calltime = ktime_set(0, 0);
  177. if (pm_print_times_enabled) {
  178. pr_info("calling %s+ @ %i, parent: %s\n",
  179. dev_name(dev), task_pid_nr(current),
  180. dev->parent ? dev_name(dev->parent) : "none");
  181. calltime = ktime_get();
  182. }
  183. return calltime;
  184. }
  185. static void initcall_debug_report(struct device *dev, ktime_t calltime,
  186. int error, pm_message_t state, char *info)
  187. {
  188. ktime_t rettime;
  189. s64 nsecs;
  190. rettime = ktime_get();
  191. nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
  192. if (pm_print_times_enabled) {
  193. pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
  194. error, (unsigned long long)nsecs >> 10);
  195. }
  196. }
  197. /**
  198. * dpm_wait - Wait for a PM operation to complete.
  199. * @dev: Device to wait for.
  200. * @async: If unset, wait only if the device's power.async_suspend flag is set.
  201. */
  202. static void dpm_wait(struct device *dev, bool async)
  203. {
  204. if (!dev)
  205. return;
  206. if (async || (pm_async_enabled && dev->power.async_suspend))
  207. wait_for_completion(&dev->power.completion);
  208. }
  209. static int dpm_wait_fn(struct device *dev, void *async_ptr)
  210. {
  211. dpm_wait(dev, *((bool *)async_ptr));
  212. return 0;
  213. }
  214. static void dpm_wait_for_children(struct device *dev, bool async)
  215. {
  216. device_for_each_child(dev, &async, dpm_wait_fn);
  217. }
  218. /**
  219. * pm_op - Return the PM operation appropriate for given PM event.
  220. * @ops: PM operations to choose from.
  221. * @state: PM transition of the system being carried out.
  222. */
  223. static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
  224. {
  225. switch (state.event) {
  226. #ifdef CONFIG_SUSPEND
  227. case PM_EVENT_SUSPEND:
  228. return ops->suspend;
  229. case PM_EVENT_RESUME:
  230. return ops->resume;
  231. #endif /* CONFIG_SUSPEND */
  232. #ifdef CONFIG_HIBERNATE_CALLBACKS
  233. case PM_EVENT_FREEZE:
  234. case PM_EVENT_QUIESCE:
  235. return ops->freeze;
  236. case PM_EVENT_HIBERNATE:
  237. return ops->poweroff;
  238. case PM_EVENT_THAW:
  239. case PM_EVENT_RECOVER:
  240. return ops->thaw;
  241. break;
  242. case PM_EVENT_RESTORE:
  243. return ops->restore;
  244. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  245. }
  246. return NULL;
  247. }
  248. /**
  249. * pm_late_early_op - Return the PM operation appropriate for given PM event.
  250. * @ops: PM operations to choose from.
  251. * @state: PM transition of the system being carried out.
  252. *
  253. * Runtime PM is disabled for @dev while this function is being executed.
  254. */
  255. static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
  256. pm_message_t state)
  257. {
  258. switch (state.event) {
  259. #ifdef CONFIG_SUSPEND
  260. case PM_EVENT_SUSPEND:
  261. return ops->suspend_late;
  262. case PM_EVENT_RESUME:
  263. return ops->resume_early;
  264. #endif /* CONFIG_SUSPEND */
  265. #ifdef CONFIG_HIBERNATE_CALLBACKS
  266. case PM_EVENT_FREEZE:
  267. case PM_EVENT_QUIESCE:
  268. return ops->freeze_late;
  269. case PM_EVENT_HIBERNATE:
  270. return ops->poweroff_late;
  271. case PM_EVENT_THAW:
  272. case PM_EVENT_RECOVER:
  273. return ops->thaw_early;
  274. case PM_EVENT_RESTORE:
  275. return ops->restore_early;
  276. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  277. }
  278. return NULL;
  279. }
  280. /**
  281. * pm_noirq_op - Return the PM operation appropriate for given PM event.
  282. * @ops: PM operations to choose from.
  283. * @state: PM transition of the system being carried out.
  284. *
  285. * The driver of @dev will not receive interrupts while this function is being
  286. * executed.
  287. */
  288. static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
  289. {
  290. switch (state.event) {
  291. #ifdef CONFIG_SUSPEND
  292. case PM_EVENT_SUSPEND:
  293. return ops->suspend_noirq;
  294. case PM_EVENT_RESUME:
  295. return ops->resume_noirq;
  296. #endif /* CONFIG_SUSPEND */
  297. #ifdef CONFIG_HIBERNATE_CALLBACKS
  298. case PM_EVENT_FREEZE:
  299. case PM_EVENT_QUIESCE:
  300. return ops->freeze_noirq;
  301. case PM_EVENT_HIBERNATE:
  302. return ops->poweroff_noirq;
  303. case PM_EVENT_THAW:
  304. case PM_EVENT_RECOVER:
  305. return ops->thaw_noirq;
  306. case PM_EVENT_RESTORE:
  307. return ops->restore_noirq;
  308. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  309. }
  310. return NULL;
  311. }
  312. static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
  313. {
  314. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  315. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  316. ", may wakeup" : "");
  317. }
  318. static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
  319. int error)
  320. {
  321. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  322. dev_name(dev), pm_verb(state.event), info, error);
  323. }
  324. static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
  325. {
  326. ktime_t calltime;
  327. u64 usecs64;
  328. int usecs;
  329. calltime = ktime_get();
  330. usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
  331. do_div(usecs64, NSEC_PER_USEC);
  332. usecs = usecs64;
  333. if (usecs == 0)
  334. usecs = 1;
  335. pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
  336. info ?: "", info ? " " : "", pm_verb(state.event),
  337. usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
  338. }
  339. static int dpm_run_callback(pm_callback_t cb, struct device *dev,
  340. pm_message_t state, char *info)
  341. {
  342. ktime_t calltime;
  343. int error;
  344. if (!cb)
  345. return 0;
  346. calltime = initcall_debug_start(dev);
  347. pm_dev_dbg(dev, state, info);
  348. trace_device_pm_callback_start(dev, info, state.event);
  349. error = cb(dev);
  350. trace_device_pm_callback_end(dev, error);
  351. suspend_report_result(cb, error);
  352. initcall_debug_report(dev, calltime, error, state, info);
  353. return error;
  354. }
  355. #ifdef CONFIG_DPM_WATCHDOG
  356. struct dpm_watchdog {
  357. struct device *dev;
  358. struct task_struct *tsk;
  359. struct timer_list timer;
  360. };
  361. #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
  362. struct dpm_watchdog wd
  363. /**
  364. * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
  365. * @data: Watchdog object address.
  366. *
  367. * Called when a driver has timed out suspending or resuming.
  368. * There's not much we can do here to recover so panic() to
  369. * capture a crash-dump in pstore.
  370. */
  371. static void dpm_watchdog_handler(unsigned long data)
  372. {
  373. struct dpm_watchdog *wd = (void *)data;
  374. dev_emerg(wd->dev, "**** DPM device timeout ****\n");
  375. show_stack(wd->tsk, NULL);
  376. panic("%s %s: unrecoverable failure\n",
  377. dev_driver_string(wd->dev), dev_name(wd->dev));
  378. }
  379. /**
  380. * dpm_watchdog_set - Enable pm watchdog for given device.
  381. * @wd: Watchdog. Must be allocated on the stack.
  382. * @dev: Device to handle.
  383. */
  384. static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
  385. {
  386. struct timer_list *timer = &wd->timer;
  387. wd->dev = dev;
  388. wd->tsk = current;
  389. init_timer_on_stack(timer);
  390. /* use same timeout value for both suspend and resume */
  391. timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
  392. timer->function = dpm_watchdog_handler;
  393. timer->data = (unsigned long)wd;
  394. add_timer(timer);
  395. }
  396. /**
  397. * dpm_watchdog_clear - Disable suspend/resume watchdog.
  398. * @wd: Watchdog to disable.
  399. */
  400. static void dpm_watchdog_clear(struct dpm_watchdog *wd)
  401. {
  402. struct timer_list *timer = &wd->timer;
  403. del_timer_sync(timer);
  404. destroy_timer_on_stack(timer);
  405. }
  406. #else
  407. #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
  408. #define dpm_watchdog_set(x, y)
  409. #define dpm_watchdog_clear(x)
  410. #endif
  411. /*------------------------- Resume routines -------------------------*/
  412. /**
  413. * device_resume_noirq - Execute an "early resume" callback for given device.
  414. * @dev: Device to handle.
  415. * @state: PM transition of the system being carried out.
  416. * @async: If true, the device is being resumed asynchronously.
  417. *
  418. * The driver of @dev will not receive interrupts while this function is being
  419. * executed.
  420. */
  421. static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
  422. {
  423. pm_callback_t callback = NULL;
  424. char *info = NULL;
  425. int error = 0;
  426. TRACE_DEVICE(dev);
  427. TRACE_RESUME(0);
  428. if (dev->power.syscore || dev->power.direct_complete)
  429. goto Out;
  430. if (!dev->power.is_noirq_suspended)
  431. goto Out;
  432. dpm_wait(dev->parent, async);
  433. if (dev->pm_domain) {
  434. info = "noirq power domain ";
  435. callback = pm_noirq_op(&dev->pm_domain->ops, state);
  436. } else if (dev->type && dev->type->pm) {
  437. info = "noirq type ";
  438. callback = pm_noirq_op(dev->type->pm, state);
  439. } else if (dev->class && dev->class->pm) {
  440. info = "noirq class ";
  441. callback = pm_noirq_op(dev->class->pm, state);
  442. } else if (dev->bus && dev->bus->pm) {
  443. info = "noirq bus ";
  444. callback = pm_noirq_op(dev->bus->pm, state);
  445. }
  446. if (!callback && dev->driver && dev->driver->pm) {
  447. info = "noirq driver ";
  448. callback = pm_noirq_op(dev->driver->pm, state);
  449. }
  450. error = dpm_run_callback(callback, dev, state, info);
  451. dev->power.is_noirq_suspended = false;
  452. Out:
  453. complete_all(&dev->power.completion);
  454. TRACE_RESUME(error);
  455. return error;
  456. }
  457. static bool is_async(struct device *dev)
  458. {
  459. return dev->power.async_suspend && pm_async_enabled
  460. && !pm_trace_is_enabled();
  461. }
  462. static void async_resume_noirq(void *data, async_cookie_t cookie)
  463. {
  464. struct device *dev = (struct device *)data;
  465. int error;
  466. error = device_resume_noirq(dev, pm_transition, true);
  467. if (error)
  468. pm_dev_err(dev, pm_transition, " async", error);
  469. put_device(dev);
  470. }
  471. /**
  472. * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
  473. * @state: PM transition of the system being carried out.
  474. *
  475. * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
  476. * enable device drivers to receive interrupts.
  477. */
  478. void dpm_resume_noirq(pm_message_t state)
  479. {
  480. struct device *dev;
  481. ktime_t starttime = ktime_get();
  482. trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
  483. mutex_lock(&dpm_list_mtx);
  484. pm_transition = state;
  485. /*
  486. * Advanced the async threads upfront,
  487. * in case the starting of async threads is
  488. * delayed by non-async resuming devices.
  489. */
  490. list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
  491. reinit_completion(&dev->power.completion);
  492. if (is_async(dev)) {
  493. get_device(dev);
  494. async_schedule(async_resume_noirq, dev);
  495. }
  496. }
  497. while (!list_empty(&dpm_noirq_list)) {
  498. dev = to_device(dpm_noirq_list.next);
  499. get_device(dev);
  500. list_move_tail(&dev->power.entry, &dpm_late_early_list);
  501. mutex_unlock(&dpm_list_mtx);
  502. if (!is_async(dev)) {
  503. int error;
  504. error = device_resume_noirq(dev, state, false);
  505. if (error) {
  506. suspend_stats.failed_resume_noirq++;
  507. dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
  508. dpm_save_failed_dev(dev_name(dev));
  509. pm_dev_err(dev, state, " noirq", error);
  510. }
  511. }
  512. mutex_lock(&dpm_list_mtx);
  513. put_device(dev);
  514. }
  515. mutex_unlock(&dpm_list_mtx);
  516. async_synchronize_full();
  517. dpm_show_time(starttime, state, "noirq");
  518. resume_device_irqs();
  519. device_wakeup_disarm_wake_irqs();
  520. cpuidle_resume();
  521. trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
  522. }
  523. /**
  524. * device_resume_early - Execute an "early resume" callback for given device.
  525. * @dev: Device to handle.
  526. * @state: PM transition of the system being carried out.
  527. * @async: If true, the device is being resumed asynchronously.
  528. *
  529. * Runtime PM is disabled for @dev while this function is being executed.
  530. */
  531. static int device_resume_early(struct device *dev, pm_message_t state, bool async)
  532. {
  533. pm_callback_t callback = NULL;
  534. char *info = NULL;
  535. int error = 0;
  536. TRACE_DEVICE(dev);
  537. TRACE_RESUME(0);
  538. if (dev->power.syscore || dev->power.direct_complete)
  539. goto Out;
  540. if (!dev->power.is_late_suspended)
  541. goto Out;
  542. dpm_wait(dev->parent, async);
  543. if (dev->pm_domain) {
  544. info = "early power domain ";
  545. callback = pm_late_early_op(&dev->pm_domain->ops, state);
  546. } else if (dev->type && dev->type->pm) {
  547. info = "early type ";
  548. callback = pm_late_early_op(dev->type->pm, state);
  549. } else if (dev->class && dev->class->pm) {
  550. info = "early class ";
  551. callback = pm_late_early_op(dev->class->pm, state);
  552. } else if (dev->bus && dev->bus->pm) {
  553. info = "early bus ";
  554. callback = pm_late_early_op(dev->bus->pm, state);
  555. }
  556. if (!callback && dev->driver && dev->driver->pm) {
  557. info = "early driver ";
  558. callback = pm_late_early_op(dev->driver->pm, state);
  559. }
  560. error = dpm_run_callback(callback, dev, state, info);
  561. dev->power.is_late_suspended = false;
  562. Out:
  563. TRACE_RESUME(error);
  564. pm_runtime_enable(dev);
  565. complete_all(&dev->power.completion);
  566. return error;
  567. }
  568. static void async_resume_early(void *data, async_cookie_t cookie)
  569. {
  570. struct device *dev = (struct device *)data;
  571. int error;
  572. error = device_resume_early(dev, pm_transition, true);
  573. if (error)
  574. pm_dev_err(dev, pm_transition, " async", error);
  575. put_device(dev);
  576. }
  577. /**
  578. * dpm_resume_early - Execute "early resume" callbacks for all devices.
  579. * @state: PM transition of the system being carried out.
  580. */
  581. void dpm_resume_early(pm_message_t state)
  582. {
  583. struct device *dev;
  584. ktime_t starttime = ktime_get();
  585. trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
  586. mutex_lock(&dpm_list_mtx);
  587. pm_transition = state;
  588. /*
  589. * Advanced the async threads upfront,
  590. * in case the starting of async threads is
  591. * delayed by non-async resuming devices.
  592. */
  593. list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
  594. reinit_completion(&dev->power.completion);
  595. if (is_async(dev)) {
  596. get_device(dev);
  597. async_schedule(async_resume_early, dev);
  598. }
  599. }
  600. while (!list_empty(&dpm_late_early_list)) {
  601. dev = to_device(dpm_late_early_list.next);
  602. get_device(dev);
  603. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  604. mutex_unlock(&dpm_list_mtx);
  605. if (!is_async(dev)) {
  606. int error;
  607. error = device_resume_early(dev, state, false);
  608. if (error) {
  609. suspend_stats.failed_resume_early++;
  610. dpm_save_failed_step(SUSPEND_RESUME_EARLY);
  611. dpm_save_failed_dev(dev_name(dev));
  612. pm_dev_err(dev, state, " early", error);
  613. }
  614. }
  615. mutex_lock(&dpm_list_mtx);
  616. put_device(dev);
  617. }
  618. mutex_unlock(&dpm_list_mtx);
  619. async_synchronize_full();
  620. dpm_show_time(starttime, state, "early");
  621. trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
  622. }
  623. /**
  624. * dpm_resume_start - Execute "noirq" and "early" device callbacks.
  625. * @state: PM transition of the system being carried out.
  626. */
  627. void dpm_resume_start(pm_message_t state)
  628. {
  629. dpm_resume_noirq(state);
  630. dpm_resume_early(state);
  631. }
  632. EXPORT_SYMBOL_GPL(dpm_resume_start);
  633. /**
  634. * device_resume - Execute "resume" callbacks for given device.
  635. * @dev: Device to handle.
  636. * @state: PM transition of the system being carried out.
  637. * @async: If true, the device is being resumed asynchronously.
  638. */
  639. static int device_resume(struct device *dev, pm_message_t state, bool async)
  640. {
  641. pm_callback_t callback = NULL;
  642. char *info = NULL;
  643. int error = 0;
  644. DECLARE_DPM_WATCHDOG_ON_STACK(wd);
  645. TRACE_DEVICE(dev);
  646. TRACE_RESUME(0);
  647. if (dev->power.syscore)
  648. goto Complete;
  649. if (dev->power.direct_complete) {
  650. /* Match the pm_runtime_disable() in __device_suspend(). */
  651. pm_runtime_enable(dev);
  652. goto Complete;
  653. }
  654. dpm_wait(dev->parent, async);
  655. dpm_watchdog_set(&wd, dev);
  656. device_lock(dev);
  657. /*
  658. * This is a fib. But we'll allow new children to be added below
  659. * a resumed device, even if the device hasn't been completed yet.
  660. */
  661. dev->power.is_prepared = false;
  662. if (!dev->power.is_suspended)
  663. goto Unlock;
  664. if (dev->pm_domain) {
  665. info = "power domain ";
  666. callback = pm_op(&dev->pm_domain->ops, state);
  667. goto Driver;
  668. }
  669. if (dev->type && dev->type->pm) {
  670. info = "type ";
  671. callback = pm_op(dev->type->pm, state);
  672. goto Driver;
  673. }
  674. if (dev->class) {
  675. if (dev->class->pm) {
  676. info = "class ";
  677. callback = pm_op(dev->class->pm, state);
  678. goto Driver;
  679. } else if (dev->class->resume) {
  680. info = "legacy class ";
  681. callback = dev->class->resume;
  682. goto End;
  683. }
  684. }
  685. if (dev->bus) {
  686. if (dev->bus->pm) {
  687. info = "bus ";
  688. callback = pm_op(dev->bus->pm, state);
  689. } else if (dev->bus->resume) {
  690. info = "legacy bus ";
  691. callback = dev->bus->resume;
  692. goto End;
  693. }
  694. }
  695. Driver:
  696. if (!callback && dev->driver && dev->driver->pm) {
  697. info = "driver ";
  698. callback = pm_op(dev->driver->pm, state);
  699. }
  700. End:
  701. error = dpm_run_callback(callback, dev, state, info);
  702. dev->power.is_suspended = false;
  703. Unlock:
  704. device_unlock(dev);
  705. dpm_watchdog_clear(&wd);
  706. Complete:
  707. complete_all(&dev->power.completion);
  708. TRACE_RESUME(error);
  709. return error;
  710. }
  711. static void async_resume(void *data, async_cookie_t cookie)
  712. {
  713. struct device *dev = (struct device *)data;
  714. int error;
  715. error = device_resume(dev, pm_transition, true);
  716. if (error)
  717. pm_dev_err(dev, pm_transition, " async", error);
  718. put_device(dev);
  719. }
  720. /**
  721. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  722. * @state: PM transition of the system being carried out.
  723. *
  724. * Execute the appropriate "resume" callback for all devices whose status
  725. * indicates that they are suspended.
  726. */
  727. void dpm_resume(pm_message_t state)
  728. {
  729. struct device *dev;
  730. ktime_t starttime = ktime_get();
  731. trace_suspend_resume(TPS("dpm_resume"), state.event, true);
  732. might_sleep();
  733. mutex_lock(&dpm_list_mtx);
  734. pm_transition = state;
  735. async_error = 0;
  736. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  737. reinit_completion(&dev->power.completion);
  738. if (is_async(dev)) {
  739. get_device(dev);
  740. async_schedule(async_resume, dev);
  741. }
  742. }
  743. while (!list_empty(&dpm_suspended_list)) {
  744. dev = to_device(dpm_suspended_list.next);
  745. get_device(dev);
  746. if (!is_async(dev)) {
  747. int error;
  748. mutex_unlock(&dpm_list_mtx);
  749. error = device_resume(dev, state, false);
  750. if (error) {
  751. suspend_stats.failed_resume++;
  752. dpm_save_failed_step(SUSPEND_RESUME);
  753. dpm_save_failed_dev(dev_name(dev));
  754. pm_dev_err(dev, state, "", error);
  755. }
  756. mutex_lock(&dpm_list_mtx);
  757. }
  758. if (!list_empty(&dev->power.entry))
  759. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  760. put_device(dev);
  761. }
  762. mutex_unlock(&dpm_list_mtx);
  763. async_synchronize_full();
  764. dpm_show_time(starttime, state, NULL);
  765. cpufreq_resume();
  766. trace_suspend_resume(TPS("dpm_resume"), state.event, false);
  767. }
  768. /**
  769. * device_complete - Complete a PM transition for given device.
  770. * @dev: Device to handle.
  771. * @state: PM transition of the system being carried out.
  772. */
  773. static void device_complete(struct device *dev, pm_message_t state)
  774. {
  775. void (*callback)(struct device *) = NULL;
  776. char *info = NULL;
  777. if (dev->power.syscore)
  778. return;
  779. device_lock(dev);
  780. if (dev->pm_domain) {
  781. info = "completing power domain ";
  782. callback = dev->pm_domain->ops.complete;
  783. } else if (dev->type && dev->type->pm) {
  784. info = "completing type ";
  785. callback = dev->type->pm->complete;
  786. } else if (dev->class && dev->class->pm) {
  787. info = "completing class ";
  788. callback = dev->class->pm->complete;
  789. } else if (dev->bus && dev->bus->pm) {
  790. info = "completing bus ";
  791. callback = dev->bus->pm->complete;
  792. }
  793. if (!callback && dev->driver && dev->driver->pm) {
  794. info = "completing driver ";
  795. callback = dev->driver->pm->complete;
  796. }
  797. if (callback) {
  798. pm_dev_dbg(dev, state, info);
  799. callback(dev);
  800. }
  801. device_unlock(dev);
  802. pm_runtime_put(dev);
  803. }
  804. /**
  805. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  806. * @state: PM transition of the system being carried out.
  807. *
  808. * Execute the ->complete() callbacks for all devices whose PM status is not
  809. * DPM_ON (this allows new devices to be registered).
  810. */
  811. void dpm_complete(pm_message_t state)
  812. {
  813. struct list_head list;
  814. trace_suspend_resume(TPS("dpm_complete"), state.event, true);
  815. might_sleep();
  816. INIT_LIST_HEAD(&list);
  817. mutex_lock(&dpm_list_mtx);
  818. while (!list_empty(&dpm_prepared_list)) {
  819. struct device *dev = to_device(dpm_prepared_list.prev);
  820. get_device(dev);
  821. dev->power.is_prepared = false;
  822. list_move(&dev->power.entry, &list);
  823. mutex_unlock(&dpm_list_mtx);
  824. trace_device_pm_callback_start(dev, "", state.event);
  825. device_complete(dev, state);
  826. trace_device_pm_callback_end(dev, 0);
  827. mutex_lock(&dpm_list_mtx);
  828. put_device(dev);
  829. }
  830. list_splice(&list, &dpm_list);
  831. mutex_unlock(&dpm_list_mtx);
  832. trace_suspend_resume(TPS("dpm_complete"), state.event, false);
  833. }
  834. /**
  835. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  836. * @state: PM transition of the system being carried out.
  837. *
  838. * Execute "resume" callbacks for all devices and complete the PM transition of
  839. * the system.
  840. */
  841. void dpm_resume_end(pm_message_t state)
  842. {
  843. dpm_resume(state);
  844. dpm_complete(state);
  845. }
  846. EXPORT_SYMBOL_GPL(dpm_resume_end);
  847. /*------------------------- Suspend routines -------------------------*/
  848. /**
  849. * resume_event - Return a "resume" message for given "suspend" sleep state.
  850. * @sleep_state: PM message representing a sleep state.
  851. *
  852. * Return a PM message representing the resume event corresponding to given
  853. * sleep state.
  854. */
  855. static pm_message_t resume_event(pm_message_t sleep_state)
  856. {
  857. switch (sleep_state.event) {
  858. case PM_EVENT_SUSPEND:
  859. return PMSG_RESUME;
  860. case PM_EVENT_FREEZE:
  861. case PM_EVENT_QUIESCE:
  862. return PMSG_RECOVER;
  863. case PM_EVENT_HIBERNATE:
  864. return PMSG_RESTORE;
  865. }
  866. return PMSG_ON;
  867. }
  868. /**
  869. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  870. * @dev: Device to handle.
  871. * @state: PM transition of the system being carried out.
  872. * @async: If true, the device is being suspended asynchronously.
  873. *
  874. * The driver of @dev will not receive interrupts while this function is being
  875. * executed.
  876. */
  877. static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
  878. {
  879. pm_callback_t callback = NULL;
  880. char *info = NULL;
  881. int error = 0;
  882. TRACE_DEVICE(dev);
  883. TRACE_SUSPEND(0);
  884. dpm_wait_for_children(dev, async);
  885. if (async_error)
  886. goto Complete;
  887. if (pm_wakeup_pending()) {
  888. async_error = -EBUSY;
  889. goto Complete;
  890. }
  891. if (dev->power.syscore || dev->power.direct_complete)
  892. goto Complete;
  893. if (dev->pm_domain) {
  894. info = "noirq power domain ";
  895. callback = pm_noirq_op(&dev->pm_domain->ops, state);
  896. } else if (dev->type && dev->type->pm) {
  897. info = "noirq type ";
  898. callback = pm_noirq_op(dev->type->pm, state);
  899. } else if (dev->class && dev->class->pm) {
  900. info = "noirq class ";
  901. callback = pm_noirq_op(dev->class->pm, state);
  902. } else if (dev->bus && dev->bus->pm) {
  903. info = "noirq bus ";
  904. callback = pm_noirq_op(dev->bus->pm, state);
  905. }
  906. if (!callback && dev->driver && dev->driver->pm) {
  907. info = "noirq driver ";
  908. callback = pm_noirq_op(dev->driver->pm, state);
  909. }
  910. error = dpm_run_callback(callback, dev, state, info);
  911. if (!error)
  912. dev->power.is_noirq_suspended = true;
  913. else
  914. async_error = error;
  915. Complete:
  916. complete_all(&dev->power.completion);
  917. TRACE_SUSPEND(error);
  918. return error;
  919. }
  920. static void async_suspend_noirq(void *data, async_cookie_t cookie)
  921. {
  922. struct device *dev = (struct device *)data;
  923. int error;
  924. error = __device_suspend_noirq(dev, pm_transition, true);
  925. if (error) {
  926. dpm_save_failed_dev(dev_name(dev));
  927. pm_dev_err(dev, pm_transition, " async", error);
  928. }
  929. put_device(dev);
  930. }
  931. static int device_suspend_noirq(struct device *dev)
  932. {
  933. reinit_completion(&dev->power.completion);
  934. if (is_async(dev)) {
  935. get_device(dev);
  936. async_schedule(async_suspend_noirq, dev);
  937. return 0;
  938. }
  939. return __device_suspend_noirq(dev, pm_transition, false);
  940. }
  941. /**
  942. * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
  943. * @state: PM transition of the system being carried out.
  944. *
  945. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  946. * handlers for all non-sysdev devices.
  947. */
  948. int dpm_suspend_noirq(pm_message_t state)
  949. {
  950. ktime_t starttime = ktime_get();
  951. int error = 0;
  952. trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
  953. cpuidle_pause();
  954. device_wakeup_arm_wake_irqs();
  955. suspend_device_irqs();
  956. mutex_lock(&dpm_list_mtx);
  957. pm_transition = state;
  958. async_error = 0;
  959. while (!list_empty(&dpm_late_early_list)) {
  960. struct device *dev = to_device(dpm_late_early_list.prev);
  961. get_device(dev);
  962. mutex_unlock(&dpm_list_mtx);
  963. error = device_suspend_noirq(dev);
  964. mutex_lock(&dpm_list_mtx);
  965. if (error) {
  966. pm_dev_err(dev, state, " noirq", error);
  967. dpm_save_failed_dev(dev_name(dev));
  968. put_device(dev);
  969. break;
  970. }
  971. if (!list_empty(&dev->power.entry))
  972. list_move(&dev->power.entry, &dpm_noirq_list);
  973. put_device(dev);
  974. if (async_error)
  975. break;
  976. }
  977. mutex_unlock(&dpm_list_mtx);
  978. async_synchronize_full();
  979. if (!error)
  980. error = async_error;
  981. if (error) {
  982. suspend_stats.failed_suspend_noirq++;
  983. dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
  984. dpm_resume_noirq(resume_event(state));
  985. } else {
  986. dpm_show_time(starttime, state, "noirq");
  987. }
  988. trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
  989. return error;
  990. }
  991. /**
  992. * device_suspend_late - Execute a "late suspend" callback for given device.
  993. * @dev: Device to handle.
  994. * @state: PM transition of the system being carried out.
  995. * @async: If true, the device is being suspended asynchronously.
  996. *
  997. * Runtime PM is disabled for @dev while this function is being executed.
  998. */
  999. static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
  1000. {
  1001. pm_callback_t callback = NULL;
  1002. char *info = NULL;
  1003. int error = 0;
  1004. TRACE_DEVICE(dev);
  1005. TRACE_SUSPEND(0);
  1006. __pm_runtime_disable(dev, false);
  1007. dpm_wait_for_children(dev, async);
  1008. if (async_error)
  1009. goto Complete;
  1010. if (pm_wakeup_pending()) {
  1011. async_error = -EBUSY;
  1012. goto Complete;
  1013. }
  1014. if (dev->power.syscore || dev->power.direct_complete)
  1015. goto Complete;
  1016. if (dev->pm_domain) {
  1017. info = "late power domain ";
  1018. callback = pm_late_early_op(&dev->pm_domain->ops, state);
  1019. } else if (dev->type && dev->type->pm) {
  1020. info = "late type ";
  1021. callback = pm_late_early_op(dev->type->pm, state);
  1022. } else if (dev->class && dev->class->pm) {
  1023. info = "late class ";
  1024. callback = pm_late_early_op(dev->class->pm, state);
  1025. } else if (dev->bus && dev->bus->pm) {
  1026. info = "late bus ";
  1027. callback = pm_late_early_op(dev->bus->pm, state);
  1028. }
  1029. if (!callback && dev->driver && dev->driver->pm) {
  1030. info = "late driver ";
  1031. callback = pm_late_early_op(dev->driver->pm, state);
  1032. }
  1033. error = dpm_run_callback(callback, dev, state, info);
  1034. if (!error)
  1035. dev->power.is_late_suspended = true;
  1036. else
  1037. async_error = error;
  1038. Complete:
  1039. TRACE_SUSPEND(error);
  1040. complete_all(&dev->power.completion);
  1041. return error;
  1042. }
  1043. static void async_suspend_late(void *data, async_cookie_t cookie)
  1044. {
  1045. struct device *dev = (struct device *)data;
  1046. int error;
  1047. error = __device_suspend_late(dev, pm_transition, true);
  1048. if (error) {
  1049. dpm_save_failed_dev(dev_name(dev));
  1050. pm_dev_err(dev, pm_transition, " async", error);
  1051. }
  1052. put_device(dev);
  1053. }
  1054. static int device_suspend_late(struct device *dev)
  1055. {
  1056. reinit_completion(&dev->power.completion);
  1057. if (is_async(dev)) {
  1058. get_device(dev);
  1059. async_schedule(async_suspend_late, dev);
  1060. return 0;
  1061. }
  1062. return __device_suspend_late(dev, pm_transition, false);
  1063. }
  1064. /**
  1065. * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
  1066. * @state: PM transition of the system being carried out.
  1067. */
  1068. int dpm_suspend_late(pm_message_t state)
  1069. {
  1070. ktime_t starttime = ktime_get();
  1071. int error = 0;
  1072. trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
  1073. mutex_lock(&dpm_list_mtx);
  1074. pm_transition = state;
  1075. async_error = 0;
  1076. while (!list_empty(&dpm_suspended_list)) {
  1077. struct device *dev = to_device(dpm_suspended_list.prev);
  1078. get_device(dev);
  1079. mutex_unlock(&dpm_list_mtx);
  1080. error = device_suspend_late(dev);
  1081. mutex_lock(&dpm_list_mtx);
  1082. if (!list_empty(&dev->power.entry))
  1083. list_move(&dev->power.entry, &dpm_late_early_list);
  1084. if (error) {
  1085. pm_dev_err(dev, state, " late", error);
  1086. dpm_save_failed_dev(dev_name(dev));
  1087. put_device(dev);
  1088. break;
  1089. }
  1090. put_device(dev);
  1091. if (async_error)
  1092. break;
  1093. }
  1094. mutex_unlock(&dpm_list_mtx);
  1095. async_synchronize_full();
  1096. if (!error)
  1097. error = async_error;
  1098. if (error) {
  1099. suspend_stats.failed_suspend_late++;
  1100. dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
  1101. dpm_resume_early(resume_event(state));
  1102. } else {
  1103. dpm_show_time(starttime, state, "late");
  1104. }
  1105. trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
  1106. return error;
  1107. }
  1108. /**
  1109. * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
  1110. * @state: PM transition of the system being carried out.
  1111. */
  1112. int dpm_suspend_end(pm_message_t state)
  1113. {
  1114. int error = dpm_suspend_late(state);
  1115. if (error)
  1116. return error;
  1117. error = dpm_suspend_noirq(state);
  1118. if (error) {
  1119. dpm_resume_early(resume_event(state));
  1120. return error;
  1121. }
  1122. return 0;
  1123. }
  1124. EXPORT_SYMBOL_GPL(dpm_suspend_end);
  1125. /**
  1126. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  1127. * @dev: Device to suspend.
  1128. * @state: PM transition of the system being carried out.
  1129. * @cb: Suspend callback to execute.
  1130. * @info: string description of caller.
  1131. */
  1132. static int legacy_suspend(struct device *dev, pm_message_t state,
  1133. int (*cb)(struct device *dev, pm_message_t state),
  1134. char *info)
  1135. {
  1136. int error;
  1137. ktime_t calltime;
  1138. calltime = initcall_debug_start(dev);
  1139. trace_device_pm_callback_start(dev, info, state.event);
  1140. error = cb(dev, state);
  1141. trace_device_pm_callback_end(dev, error);
  1142. suspend_report_result(cb, error);
  1143. initcall_debug_report(dev, calltime, error, state, info);
  1144. return error;
  1145. }
  1146. /**
  1147. * device_suspend - Execute "suspend" callbacks for given device.
  1148. * @dev: Device to handle.
  1149. * @state: PM transition of the system being carried out.
  1150. * @async: If true, the device is being suspended asynchronously.
  1151. */
  1152. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  1153. {
  1154. pm_callback_t callback = NULL;
  1155. char *info = NULL;
  1156. int error = 0;
  1157. DECLARE_DPM_WATCHDOG_ON_STACK(wd);
  1158. TRACE_DEVICE(dev);
  1159. TRACE_SUSPEND(0);
  1160. dpm_wait_for_children(dev, async);
  1161. if (async_error) {
  1162. dev->power.direct_complete = false;
  1163. goto Complete;
  1164. }
  1165. /*
  1166. * If a device configured to wake up the system from sleep states
  1167. * has been suspended at run time and there's a resume request pending
  1168. * for it, this is equivalent to the device signaling wakeup, so the
  1169. * system suspend operation should be aborted.
  1170. */
  1171. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  1172. pm_wakeup_event(dev, 0);
  1173. if (pm_wakeup_pending()) {
  1174. dev->power.direct_complete = false;
  1175. async_error = -EBUSY;
  1176. goto Complete;
  1177. }
  1178. if (dev->power.syscore)
  1179. goto Complete;
  1180. if (dev->power.direct_complete) {
  1181. if (pm_runtime_status_suspended(dev)) {
  1182. pm_runtime_disable(dev);
  1183. if (pm_runtime_status_suspended(dev))
  1184. goto Complete;
  1185. pm_runtime_enable(dev);
  1186. }
  1187. dev->power.direct_complete = false;
  1188. }
  1189. dpm_watchdog_set(&wd, dev);
  1190. device_lock(dev);
  1191. if (dev->pm_domain) {
  1192. info = "power domain ";
  1193. callback = pm_op(&dev->pm_domain->ops, state);
  1194. goto Run;
  1195. }
  1196. if (dev->type && dev->type->pm) {
  1197. info = "type ";
  1198. callback = pm_op(dev->type->pm, state);
  1199. goto Run;
  1200. }
  1201. if (dev->class) {
  1202. if (dev->class->pm) {
  1203. info = "class ";
  1204. callback = pm_op(dev->class->pm, state);
  1205. goto Run;
  1206. } else if (dev->class->suspend) {
  1207. pm_dev_dbg(dev, state, "legacy class ");
  1208. error = legacy_suspend(dev, state, dev->class->suspend,
  1209. "legacy class ");
  1210. goto End;
  1211. }
  1212. }
  1213. if (dev->bus) {
  1214. if (dev->bus->pm) {
  1215. info = "bus ";
  1216. callback = pm_op(dev->bus->pm, state);
  1217. } else if (dev->bus->suspend) {
  1218. pm_dev_dbg(dev, state, "legacy bus ");
  1219. error = legacy_suspend(dev, state, dev->bus->suspend,
  1220. "legacy bus ");
  1221. goto End;
  1222. }
  1223. }
  1224. Run:
  1225. if (!callback && dev->driver && dev->driver->pm) {
  1226. info = "driver ";
  1227. callback = pm_op(dev->driver->pm, state);
  1228. }
  1229. error = dpm_run_callback(callback, dev, state, info);
  1230. End:
  1231. if (!error) {
  1232. struct device *parent = dev->parent;
  1233. dev->power.is_suspended = true;
  1234. if (parent) {
  1235. spin_lock_irq(&parent->power.lock);
  1236. dev->parent->power.direct_complete = false;
  1237. if (dev->power.wakeup_path
  1238. && !dev->parent->power.ignore_children)
  1239. dev->parent->power.wakeup_path = true;
  1240. spin_unlock_irq(&parent->power.lock);
  1241. }
  1242. }
  1243. device_unlock(dev);
  1244. dpm_watchdog_clear(&wd);
  1245. Complete:
  1246. complete_all(&dev->power.completion);
  1247. if (error)
  1248. async_error = error;
  1249. TRACE_SUSPEND(error);
  1250. return error;
  1251. }
  1252. static void async_suspend(void *data, async_cookie_t cookie)
  1253. {
  1254. struct device *dev = (struct device *)data;
  1255. int error;
  1256. error = __device_suspend(dev, pm_transition, true);
  1257. if (error) {
  1258. dpm_save_failed_dev(dev_name(dev));
  1259. pm_dev_err(dev, pm_transition, " async", error);
  1260. }
  1261. put_device(dev);
  1262. }
  1263. static int device_suspend(struct device *dev)
  1264. {
  1265. reinit_completion(&dev->power.completion);
  1266. if (is_async(dev)) {
  1267. get_device(dev);
  1268. async_schedule(async_suspend, dev);
  1269. return 0;
  1270. }
  1271. return __device_suspend(dev, pm_transition, false);
  1272. }
  1273. /**
  1274. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  1275. * @state: PM transition of the system being carried out.
  1276. */
  1277. int dpm_suspend(pm_message_t state)
  1278. {
  1279. ktime_t starttime = ktime_get();
  1280. int error = 0;
  1281. trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
  1282. might_sleep();
  1283. cpufreq_suspend();
  1284. mutex_lock(&dpm_list_mtx);
  1285. pm_transition = state;
  1286. async_error = 0;
  1287. while (!list_empty(&dpm_prepared_list)) {
  1288. struct device *dev = to_device(dpm_prepared_list.prev);
  1289. get_device(dev);
  1290. mutex_unlock(&dpm_list_mtx);
  1291. error = device_suspend(dev);
  1292. mutex_lock(&dpm_list_mtx);
  1293. if (error) {
  1294. pm_dev_err(dev, state, "", error);
  1295. dpm_save_failed_dev(dev_name(dev));
  1296. put_device(dev);
  1297. break;
  1298. }
  1299. if (!list_empty(&dev->power.entry))
  1300. list_move(&dev->power.entry, &dpm_suspended_list);
  1301. put_device(dev);
  1302. if (async_error)
  1303. break;
  1304. }
  1305. mutex_unlock(&dpm_list_mtx);
  1306. async_synchronize_full();
  1307. if (!error)
  1308. error = async_error;
  1309. if (error) {
  1310. suspend_stats.failed_suspend++;
  1311. dpm_save_failed_step(SUSPEND_SUSPEND);
  1312. } else
  1313. dpm_show_time(starttime, state, NULL);
  1314. trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
  1315. return error;
  1316. }
  1317. /**
  1318. * device_prepare - Prepare a device for system power transition.
  1319. * @dev: Device to handle.
  1320. * @state: PM transition of the system being carried out.
  1321. *
  1322. * Execute the ->prepare() callback(s) for given device. No new children of the
  1323. * device may be registered after this function has returned.
  1324. */
  1325. static int device_prepare(struct device *dev, pm_message_t state)
  1326. {
  1327. int (*callback)(struct device *) = NULL;
  1328. char *info = NULL;
  1329. int ret = 0;
  1330. if (dev->power.syscore)
  1331. return 0;
  1332. /*
  1333. * If a device's parent goes into runtime suspend at the wrong time,
  1334. * it won't be possible to resume the device. To prevent this we
  1335. * block runtime suspend here, during the prepare phase, and allow
  1336. * it again during the complete phase.
  1337. */
  1338. pm_runtime_get_noresume(dev);
  1339. device_lock(dev);
  1340. dev->power.wakeup_path = device_may_wakeup(dev);
  1341. if (dev->pm_domain) {
  1342. info = "preparing power domain ";
  1343. callback = dev->pm_domain->ops.prepare;
  1344. } else if (dev->type && dev->type->pm) {
  1345. info = "preparing type ";
  1346. callback = dev->type->pm->prepare;
  1347. } else if (dev->class && dev->class->pm) {
  1348. info = "preparing class ";
  1349. callback = dev->class->pm->prepare;
  1350. } else if (dev->bus && dev->bus->pm) {
  1351. info = "preparing bus ";
  1352. callback = dev->bus->pm->prepare;
  1353. }
  1354. if (!callback && dev->driver && dev->driver->pm) {
  1355. info = "preparing driver ";
  1356. callback = dev->driver->pm->prepare;
  1357. }
  1358. if (callback)
  1359. ret = callback(dev);
  1360. device_unlock(dev);
  1361. if (ret < 0) {
  1362. suspend_report_result(callback, ret);
  1363. pm_runtime_put(dev);
  1364. return ret;
  1365. }
  1366. /*
  1367. * A positive return value from ->prepare() means "this device appears
  1368. * to be runtime-suspended and its state is fine, so if it really is
  1369. * runtime-suspended, you can leave it in that state provided that you
  1370. * will do the same thing with all of its descendants". This only
  1371. * applies to suspend transitions, however.
  1372. */
  1373. spin_lock_irq(&dev->power.lock);
  1374. dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
  1375. spin_unlock_irq(&dev->power.lock);
  1376. return 0;
  1377. }
  1378. /**
  1379. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  1380. * @state: PM transition of the system being carried out.
  1381. *
  1382. * Execute the ->prepare() callback(s) for all devices.
  1383. */
  1384. int dpm_prepare(pm_message_t state)
  1385. {
  1386. int error = 0;
  1387. trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
  1388. might_sleep();
  1389. mutex_lock(&dpm_list_mtx);
  1390. while (!list_empty(&dpm_list)) {
  1391. struct device *dev = to_device(dpm_list.next);
  1392. get_device(dev);
  1393. mutex_unlock(&dpm_list_mtx);
  1394. trace_device_pm_callback_start(dev, "", state.event);
  1395. error = device_prepare(dev, state);
  1396. trace_device_pm_callback_end(dev, error);
  1397. mutex_lock(&dpm_list_mtx);
  1398. if (error) {
  1399. if (error == -EAGAIN) {
  1400. put_device(dev);
  1401. error = 0;
  1402. continue;
  1403. }
  1404. printk(KERN_INFO "PM: Device %s not prepared "
  1405. "for power transition: code %d\n",
  1406. dev_name(dev), error);
  1407. put_device(dev);
  1408. break;
  1409. }
  1410. dev->power.is_prepared = true;
  1411. if (!list_empty(&dev->power.entry))
  1412. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  1413. put_device(dev);
  1414. }
  1415. mutex_unlock(&dpm_list_mtx);
  1416. trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
  1417. return error;
  1418. }
  1419. /**
  1420. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  1421. * @state: PM transition of the system being carried out.
  1422. *
  1423. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  1424. * callbacks for them.
  1425. */
  1426. int dpm_suspend_start(pm_message_t state)
  1427. {
  1428. int error;
  1429. error = dpm_prepare(state);
  1430. if (error) {
  1431. suspend_stats.failed_prepare++;
  1432. dpm_save_failed_step(SUSPEND_PREPARE);
  1433. } else
  1434. error = dpm_suspend(state);
  1435. return error;
  1436. }
  1437. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  1438. void __suspend_report_result(const char *function, void *fn, int ret)
  1439. {
  1440. if (ret)
  1441. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  1442. }
  1443. EXPORT_SYMBOL_GPL(__suspend_report_result);
  1444. /**
  1445. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  1446. * @dev: Device to wait for.
  1447. * @subordinate: Device that needs to wait for @dev.
  1448. */
  1449. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  1450. {
  1451. dpm_wait(dev, subordinate->power.async_suspend);
  1452. return async_error;
  1453. }
  1454. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
  1455. /**
  1456. * dpm_for_each_dev - device iterator.
  1457. * @data: data for the callback.
  1458. * @fn: function to be called for each device.
  1459. *
  1460. * Iterate over devices in dpm_list, and call @fn for each device,
  1461. * passing it @data.
  1462. */
  1463. void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
  1464. {
  1465. struct device *dev;
  1466. if (!fn)
  1467. return;
  1468. device_pm_lock();
  1469. list_for_each_entry(dev, &dpm_list, power.entry)
  1470. fn(dev, data);
  1471. device_pm_unlock();
  1472. }
  1473. EXPORT_SYMBOL_GPL(dpm_for_each_dev);