main.c 50 KB

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