main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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/mutex.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/resume-trace.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sched.h>
  27. #include <linux/async.h>
  28. #include <linux/suspend.h>
  29. #include "../base.h"
  30. #include "power.h"
  31. /*
  32. * The entries in the dpm_list list are in a depth first order, simply
  33. * because children are guaranteed to be discovered after parents, and
  34. * are inserted at the back of the list on discovery.
  35. *
  36. * Since device_pm_add() may be called with a device lock held,
  37. * we must never try to acquire a device lock while holding
  38. * dpm_list_mutex.
  39. */
  40. LIST_HEAD(dpm_list);
  41. LIST_HEAD(dpm_prepared_list);
  42. LIST_HEAD(dpm_suspended_list);
  43. LIST_HEAD(dpm_noirq_list);
  44. static DEFINE_MUTEX(dpm_list_mtx);
  45. static pm_message_t pm_transition;
  46. static int async_error;
  47. /**
  48. * device_pm_init - Initialize the PM-related part of a device object.
  49. * @dev: Device object being initialized.
  50. */
  51. void device_pm_init(struct device *dev)
  52. {
  53. dev->power.in_suspend = false;
  54. init_completion(&dev->power.completion);
  55. complete_all(&dev->power.completion);
  56. dev->power.wakeup = NULL;
  57. spin_lock_init(&dev->power.lock);
  58. pm_runtime_init(dev);
  59. }
  60. /**
  61. * device_pm_lock - Lock the list of active devices used by the PM core.
  62. */
  63. void device_pm_lock(void)
  64. {
  65. mutex_lock(&dpm_list_mtx);
  66. }
  67. /**
  68. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  69. */
  70. void device_pm_unlock(void)
  71. {
  72. mutex_unlock(&dpm_list_mtx);
  73. }
  74. /**
  75. * device_pm_add - Add a device to the PM core's list of active devices.
  76. * @dev: Device to add to the list.
  77. */
  78. void device_pm_add(struct device *dev)
  79. {
  80. pr_debug("PM: Adding info for %s:%s\n",
  81. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  82. mutex_lock(&dpm_list_mtx);
  83. if (dev->parent && dev->parent->power.in_suspend)
  84. dev_warn(dev, "parent %s should not be sleeping\n",
  85. dev_name(dev->parent));
  86. list_add_tail(&dev->power.entry, &dpm_list);
  87. mutex_unlock(&dpm_list_mtx);
  88. }
  89. /**
  90. * device_pm_remove - Remove a device from the PM core's list of active devices.
  91. * @dev: Device to be removed from the list.
  92. */
  93. void device_pm_remove(struct device *dev)
  94. {
  95. pr_debug("PM: Removing info for %s:%s\n",
  96. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  97. complete_all(&dev->power.completion);
  98. mutex_lock(&dpm_list_mtx);
  99. list_del_init(&dev->power.entry);
  100. mutex_unlock(&dpm_list_mtx);
  101. device_wakeup_disable(dev);
  102. pm_runtime_remove(dev);
  103. }
  104. /**
  105. * device_pm_move_before - Move device in the PM core's list of active devices.
  106. * @deva: Device to move in dpm_list.
  107. * @devb: Device @deva should come before.
  108. */
  109. void device_pm_move_before(struct device *deva, struct device *devb)
  110. {
  111. pr_debug("PM: Moving %s:%s before %s:%s\n",
  112. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  113. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  114. /* Delete deva from dpm_list and reinsert before devb. */
  115. list_move_tail(&deva->power.entry, &devb->power.entry);
  116. }
  117. /**
  118. * device_pm_move_after - Move device in the PM core's list of active devices.
  119. * @deva: Device to move in dpm_list.
  120. * @devb: Device @deva should come after.
  121. */
  122. void device_pm_move_after(struct device *deva, struct device *devb)
  123. {
  124. pr_debug("PM: Moving %s:%s after %s:%s\n",
  125. deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
  126. devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
  127. /* Delete deva from dpm_list and reinsert after devb. */
  128. list_move(&deva->power.entry, &devb->power.entry);
  129. }
  130. /**
  131. * device_pm_move_last - Move device to end of the PM core's list of devices.
  132. * @dev: Device to move in dpm_list.
  133. */
  134. void device_pm_move_last(struct device *dev)
  135. {
  136. pr_debug("PM: Moving %s:%s to end of list\n",
  137. dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
  138. list_move_tail(&dev->power.entry, &dpm_list);
  139. }
  140. static ktime_t initcall_debug_start(struct device *dev)
  141. {
  142. ktime_t calltime = ktime_set(0, 0);
  143. if (initcall_debug) {
  144. pr_info("calling %s+ @ %i\n",
  145. dev_name(dev), task_pid_nr(current));
  146. calltime = ktime_get();
  147. }
  148. return calltime;
  149. }
  150. static void initcall_debug_report(struct device *dev, ktime_t calltime,
  151. int error)
  152. {
  153. ktime_t delta, rettime;
  154. if (initcall_debug) {
  155. rettime = ktime_get();
  156. delta = ktime_sub(rettime, calltime);
  157. pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
  158. error, (unsigned long long)ktime_to_ns(delta) >> 10);
  159. }
  160. }
  161. /**
  162. * dpm_wait - Wait for a PM operation to complete.
  163. * @dev: Device to wait for.
  164. * @async: If unset, wait only if the device's power.async_suspend flag is set.
  165. */
  166. static void dpm_wait(struct device *dev, bool async)
  167. {
  168. if (!dev)
  169. return;
  170. if (async || (pm_async_enabled && dev->power.async_suspend))
  171. wait_for_completion(&dev->power.completion);
  172. }
  173. static int dpm_wait_fn(struct device *dev, void *async_ptr)
  174. {
  175. dpm_wait(dev, *((bool *)async_ptr));
  176. return 0;
  177. }
  178. static void dpm_wait_for_children(struct device *dev, bool async)
  179. {
  180. device_for_each_child(dev, &async, dpm_wait_fn);
  181. }
  182. /**
  183. * pm_op - Execute the PM operation appropriate for given PM event.
  184. * @dev: Device to handle.
  185. * @ops: PM operations to choose from.
  186. * @state: PM transition of the system being carried out.
  187. */
  188. static int pm_op(struct device *dev,
  189. const struct dev_pm_ops *ops,
  190. pm_message_t state)
  191. {
  192. int error = 0;
  193. ktime_t calltime;
  194. calltime = initcall_debug_start(dev);
  195. switch (state.event) {
  196. #ifdef CONFIG_SUSPEND
  197. case PM_EVENT_SUSPEND:
  198. if (ops->suspend) {
  199. error = ops->suspend(dev);
  200. suspend_report_result(ops->suspend, error);
  201. }
  202. break;
  203. case PM_EVENT_RESUME:
  204. if (ops->resume) {
  205. error = ops->resume(dev);
  206. suspend_report_result(ops->resume, error);
  207. }
  208. break;
  209. #endif /* CONFIG_SUSPEND */
  210. #ifdef CONFIG_HIBERNATION
  211. case PM_EVENT_FREEZE:
  212. case PM_EVENT_QUIESCE:
  213. if (ops->freeze) {
  214. error = ops->freeze(dev);
  215. suspend_report_result(ops->freeze, error);
  216. }
  217. break;
  218. case PM_EVENT_HIBERNATE:
  219. if (ops->poweroff) {
  220. error = ops->poweroff(dev);
  221. suspend_report_result(ops->poweroff, error);
  222. }
  223. break;
  224. case PM_EVENT_THAW:
  225. case PM_EVENT_RECOVER:
  226. if (ops->thaw) {
  227. error = ops->thaw(dev);
  228. suspend_report_result(ops->thaw, error);
  229. }
  230. break;
  231. case PM_EVENT_RESTORE:
  232. if (ops->restore) {
  233. error = ops->restore(dev);
  234. suspend_report_result(ops->restore, error);
  235. }
  236. break;
  237. #endif /* CONFIG_HIBERNATION */
  238. default:
  239. error = -EINVAL;
  240. }
  241. initcall_debug_report(dev, calltime, error);
  242. return error;
  243. }
  244. /**
  245. * pm_noirq_op - Execute the PM operation appropriate for given PM event.
  246. * @dev: Device to handle.
  247. * @ops: PM operations to choose from.
  248. * @state: PM transition of the system being carried out.
  249. *
  250. * The driver of @dev will not receive interrupts while this function is being
  251. * executed.
  252. */
  253. static int pm_noirq_op(struct device *dev,
  254. const struct dev_pm_ops *ops,
  255. pm_message_t state)
  256. {
  257. int error = 0;
  258. ktime_t calltime = ktime_set(0, 0), delta, rettime;
  259. if (initcall_debug) {
  260. pr_info("calling %s+ @ %i, parent: %s\n",
  261. dev_name(dev), task_pid_nr(current),
  262. dev->parent ? dev_name(dev->parent) : "none");
  263. calltime = ktime_get();
  264. }
  265. switch (state.event) {
  266. #ifdef CONFIG_SUSPEND
  267. case PM_EVENT_SUSPEND:
  268. if (ops->suspend_noirq) {
  269. error = ops->suspend_noirq(dev);
  270. suspend_report_result(ops->suspend_noirq, error);
  271. }
  272. break;
  273. case PM_EVENT_RESUME:
  274. if (ops->resume_noirq) {
  275. error = ops->resume_noirq(dev);
  276. suspend_report_result(ops->resume_noirq, error);
  277. }
  278. break;
  279. #endif /* CONFIG_SUSPEND */
  280. #ifdef CONFIG_HIBERNATION
  281. case PM_EVENT_FREEZE:
  282. case PM_EVENT_QUIESCE:
  283. if (ops->freeze_noirq) {
  284. error = ops->freeze_noirq(dev);
  285. suspend_report_result(ops->freeze_noirq, error);
  286. }
  287. break;
  288. case PM_EVENT_HIBERNATE:
  289. if (ops->poweroff_noirq) {
  290. error = ops->poweroff_noirq(dev);
  291. suspend_report_result(ops->poweroff_noirq, error);
  292. }
  293. break;
  294. case PM_EVENT_THAW:
  295. case PM_EVENT_RECOVER:
  296. if (ops->thaw_noirq) {
  297. error = ops->thaw_noirq(dev);
  298. suspend_report_result(ops->thaw_noirq, error);
  299. }
  300. break;
  301. case PM_EVENT_RESTORE:
  302. if (ops->restore_noirq) {
  303. error = ops->restore_noirq(dev);
  304. suspend_report_result(ops->restore_noirq, error);
  305. }
  306. break;
  307. #endif /* CONFIG_HIBERNATION */
  308. default:
  309. error = -EINVAL;
  310. }
  311. if (initcall_debug) {
  312. rettime = ktime_get();
  313. delta = ktime_sub(rettime, calltime);
  314. printk("initcall %s_i+ returned %d after %Ld usecs\n",
  315. dev_name(dev), error,
  316. (unsigned long long)ktime_to_ns(delta) >> 10);
  317. }
  318. return error;
  319. }
  320. static char *pm_verb(int event)
  321. {
  322. switch (event) {
  323. case PM_EVENT_SUSPEND:
  324. return "suspend";
  325. case PM_EVENT_RESUME:
  326. return "resume";
  327. case PM_EVENT_FREEZE:
  328. return "freeze";
  329. case PM_EVENT_QUIESCE:
  330. return "quiesce";
  331. case PM_EVENT_HIBERNATE:
  332. return "hibernate";
  333. case PM_EVENT_THAW:
  334. return "thaw";
  335. case PM_EVENT_RESTORE:
  336. return "restore";
  337. case PM_EVENT_RECOVER:
  338. return "recover";
  339. default:
  340. return "(unknown PM event)";
  341. }
  342. }
  343. static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
  344. {
  345. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  346. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  347. ", may wakeup" : "");
  348. }
  349. static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
  350. int error)
  351. {
  352. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  353. dev_name(dev), pm_verb(state.event), info, error);
  354. }
  355. static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
  356. {
  357. ktime_t calltime;
  358. u64 usecs64;
  359. int usecs;
  360. calltime = ktime_get();
  361. usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
  362. do_div(usecs64, NSEC_PER_USEC);
  363. usecs = usecs64;
  364. if (usecs == 0)
  365. usecs = 1;
  366. pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
  367. info ?: "", info ? " " : "", pm_verb(state.event),
  368. usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
  369. }
  370. /*------------------------- Resume routines -------------------------*/
  371. /**
  372. * device_resume_noirq - Execute an "early resume" callback for given device.
  373. * @dev: Device to handle.
  374. * @state: PM transition of the system being carried out.
  375. *
  376. * The driver of @dev will not receive interrupts while this function is being
  377. * executed.
  378. */
  379. static int device_resume_noirq(struct device *dev, pm_message_t state)
  380. {
  381. int error = 0;
  382. TRACE_DEVICE(dev);
  383. TRACE_RESUME(0);
  384. if (dev->pwr_domain) {
  385. pm_dev_dbg(dev, state, "EARLY power domain ");
  386. pm_noirq_op(dev, &dev->pwr_domain->ops, state);
  387. }
  388. if (dev->bus && dev->bus->pm) {
  389. pm_dev_dbg(dev, state, "EARLY ");
  390. error = pm_noirq_op(dev, dev->bus->pm, state);
  391. if (error)
  392. goto End;
  393. }
  394. if (dev->type && dev->type->pm) {
  395. pm_dev_dbg(dev, state, "EARLY type ");
  396. error = pm_noirq_op(dev, dev->type->pm, state);
  397. if (error)
  398. goto End;
  399. }
  400. if (dev->class && dev->class->pm) {
  401. pm_dev_dbg(dev, state, "EARLY class ");
  402. error = pm_noirq_op(dev, dev->class->pm, state);
  403. }
  404. End:
  405. TRACE_RESUME(error);
  406. return error;
  407. }
  408. /**
  409. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  410. * @state: PM transition of the system being carried out.
  411. *
  412. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  413. * enable device drivers to receive interrupts.
  414. */
  415. void dpm_resume_noirq(pm_message_t state)
  416. {
  417. ktime_t starttime = ktime_get();
  418. mutex_lock(&dpm_list_mtx);
  419. while (!list_empty(&dpm_noirq_list)) {
  420. struct device *dev = to_device(dpm_noirq_list.next);
  421. int error;
  422. get_device(dev);
  423. list_move_tail(&dev->power.entry, &dpm_suspended_list);
  424. mutex_unlock(&dpm_list_mtx);
  425. error = device_resume_noirq(dev, state);
  426. if (error)
  427. pm_dev_err(dev, state, " early", error);
  428. mutex_lock(&dpm_list_mtx);
  429. put_device(dev);
  430. }
  431. mutex_unlock(&dpm_list_mtx);
  432. dpm_show_time(starttime, state, "early");
  433. resume_device_irqs();
  434. }
  435. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  436. /**
  437. * legacy_resume - Execute a legacy (bus or class) resume callback for device.
  438. * @dev: Device to resume.
  439. * @cb: Resume callback to execute.
  440. */
  441. static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
  442. {
  443. int error;
  444. ktime_t calltime;
  445. calltime = initcall_debug_start(dev);
  446. error = cb(dev);
  447. suspend_report_result(cb, error);
  448. initcall_debug_report(dev, calltime, error);
  449. return error;
  450. }
  451. /**
  452. * device_resume - Execute "resume" callbacks for given device.
  453. * @dev: Device to handle.
  454. * @state: PM transition of the system being carried out.
  455. * @async: If true, the device is being resumed asynchronously.
  456. */
  457. static int device_resume(struct device *dev, pm_message_t state, bool async)
  458. {
  459. int error = 0;
  460. TRACE_DEVICE(dev);
  461. TRACE_RESUME(0);
  462. dpm_wait(dev->parent, async);
  463. device_lock(dev);
  464. dev->power.in_suspend = false;
  465. if (dev->pwr_domain) {
  466. pm_dev_dbg(dev, state, "power domain ");
  467. pm_op(dev, &dev->pwr_domain->ops, state);
  468. }
  469. if (dev->bus) {
  470. if (dev->bus->pm) {
  471. pm_dev_dbg(dev, state, "");
  472. error = pm_op(dev, dev->bus->pm, state);
  473. } else if (dev->bus->resume) {
  474. pm_dev_dbg(dev, state, "legacy ");
  475. error = legacy_resume(dev, dev->bus->resume);
  476. }
  477. if (error)
  478. goto End;
  479. }
  480. if (dev->type) {
  481. if (dev->type->pm) {
  482. pm_dev_dbg(dev, state, "type ");
  483. error = pm_op(dev, dev->type->pm, state);
  484. }
  485. if (error)
  486. goto End;
  487. }
  488. if (dev->class) {
  489. if (dev->class->pm) {
  490. pm_dev_dbg(dev, state, "class ");
  491. error = pm_op(dev, dev->class->pm, state);
  492. } else if (dev->class->resume) {
  493. pm_dev_dbg(dev, state, "legacy class ");
  494. error = legacy_resume(dev, dev->class->resume);
  495. }
  496. }
  497. End:
  498. device_unlock(dev);
  499. complete_all(&dev->power.completion);
  500. TRACE_RESUME(error);
  501. return error;
  502. }
  503. static void async_resume(void *data, async_cookie_t cookie)
  504. {
  505. struct device *dev = (struct device *)data;
  506. int error;
  507. error = device_resume(dev, pm_transition, true);
  508. if (error)
  509. pm_dev_err(dev, pm_transition, " async", error);
  510. put_device(dev);
  511. }
  512. static bool is_async(struct device *dev)
  513. {
  514. return dev->power.async_suspend && pm_async_enabled
  515. && !pm_trace_is_enabled();
  516. }
  517. /**
  518. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  519. * @state: PM transition of the system being carried out.
  520. *
  521. * Execute the appropriate "resume" callback for all devices whose status
  522. * indicates that they are suspended.
  523. */
  524. static void dpm_resume(pm_message_t state)
  525. {
  526. struct device *dev;
  527. ktime_t starttime = ktime_get();
  528. mutex_lock(&dpm_list_mtx);
  529. pm_transition = state;
  530. async_error = 0;
  531. list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
  532. INIT_COMPLETION(dev->power.completion);
  533. if (is_async(dev)) {
  534. get_device(dev);
  535. async_schedule(async_resume, dev);
  536. }
  537. }
  538. while (!list_empty(&dpm_suspended_list)) {
  539. dev = to_device(dpm_suspended_list.next);
  540. get_device(dev);
  541. if (!is_async(dev)) {
  542. int error;
  543. mutex_unlock(&dpm_list_mtx);
  544. error = device_resume(dev, state, false);
  545. if (error)
  546. pm_dev_err(dev, state, "", error);
  547. mutex_lock(&dpm_list_mtx);
  548. }
  549. if (!list_empty(&dev->power.entry))
  550. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  551. put_device(dev);
  552. }
  553. mutex_unlock(&dpm_list_mtx);
  554. async_synchronize_full();
  555. dpm_show_time(starttime, state, NULL);
  556. }
  557. /**
  558. * device_complete - Complete a PM transition for given device.
  559. * @dev: Device to handle.
  560. * @state: PM transition of the system being carried out.
  561. */
  562. static void device_complete(struct device *dev, pm_message_t state)
  563. {
  564. device_lock(dev);
  565. if (dev->pwr_domain && dev->pwr_domain->ops.complete) {
  566. pm_dev_dbg(dev, state, "completing power domain ");
  567. dev->pwr_domain->ops.complete(dev);
  568. }
  569. if (dev->class && dev->class->pm && dev->class->pm->complete) {
  570. pm_dev_dbg(dev, state, "completing class ");
  571. dev->class->pm->complete(dev);
  572. }
  573. if (dev->type && dev->type->pm && dev->type->pm->complete) {
  574. pm_dev_dbg(dev, state, "completing type ");
  575. dev->type->pm->complete(dev);
  576. }
  577. if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
  578. pm_dev_dbg(dev, state, "completing ");
  579. dev->bus->pm->complete(dev);
  580. }
  581. device_unlock(dev);
  582. }
  583. /**
  584. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  585. * @state: PM transition of the system being carried out.
  586. *
  587. * Execute the ->complete() callbacks for all devices whose PM status is not
  588. * DPM_ON (this allows new devices to be registered).
  589. */
  590. static void dpm_complete(pm_message_t state)
  591. {
  592. struct list_head list;
  593. INIT_LIST_HEAD(&list);
  594. mutex_lock(&dpm_list_mtx);
  595. while (!list_empty(&dpm_prepared_list)) {
  596. struct device *dev = to_device(dpm_prepared_list.prev);
  597. get_device(dev);
  598. dev->power.in_suspend = false;
  599. list_move(&dev->power.entry, &list);
  600. mutex_unlock(&dpm_list_mtx);
  601. device_complete(dev, state);
  602. mutex_lock(&dpm_list_mtx);
  603. put_device(dev);
  604. }
  605. list_splice(&list, &dpm_list);
  606. mutex_unlock(&dpm_list_mtx);
  607. }
  608. /**
  609. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  610. * @state: PM transition of the system being carried out.
  611. *
  612. * Execute "resume" callbacks for all devices and complete the PM transition of
  613. * the system.
  614. */
  615. void dpm_resume_end(pm_message_t state)
  616. {
  617. might_sleep();
  618. dpm_resume(state);
  619. dpm_complete(state);
  620. }
  621. EXPORT_SYMBOL_GPL(dpm_resume_end);
  622. /*------------------------- Suspend routines -------------------------*/
  623. /**
  624. * resume_event - Return a "resume" message for given "suspend" sleep state.
  625. * @sleep_state: PM message representing a sleep state.
  626. *
  627. * Return a PM message representing the resume event corresponding to given
  628. * sleep state.
  629. */
  630. static pm_message_t resume_event(pm_message_t sleep_state)
  631. {
  632. switch (sleep_state.event) {
  633. case PM_EVENT_SUSPEND:
  634. return PMSG_RESUME;
  635. case PM_EVENT_FREEZE:
  636. case PM_EVENT_QUIESCE:
  637. return PMSG_RECOVER;
  638. case PM_EVENT_HIBERNATE:
  639. return PMSG_RESTORE;
  640. }
  641. return PMSG_ON;
  642. }
  643. /**
  644. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  645. * @dev: Device to handle.
  646. * @state: PM transition of the system being carried out.
  647. *
  648. * The driver of @dev will not receive interrupts while this function is being
  649. * executed.
  650. */
  651. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  652. {
  653. int error = 0;
  654. if (dev->class && dev->class->pm) {
  655. pm_dev_dbg(dev, state, "LATE class ");
  656. error = pm_noirq_op(dev, dev->class->pm, state);
  657. if (error)
  658. goto End;
  659. }
  660. if (dev->type && dev->type->pm) {
  661. pm_dev_dbg(dev, state, "LATE type ");
  662. error = pm_noirq_op(dev, dev->type->pm, state);
  663. if (error)
  664. goto End;
  665. }
  666. if (dev->bus && dev->bus->pm) {
  667. pm_dev_dbg(dev, state, "LATE ");
  668. error = pm_noirq_op(dev, dev->bus->pm, state);
  669. if (error)
  670. goto End;
  671. }
  672. if (dev->pwr_domain) {
  673. pm_dev_dbg(dev, state, "LATE power domain ");
  674. pm_noirq_op(dev, &dev->pwr_domain->ops, state);
  675. }
  676. End:
  677. return error;
  678. }
  679. /**
  680. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  681. * @state: PM transition of the system being carried out.
  682. *
  683. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  684. * handlers for all non-sysdev devices.
  685. */
  686. int dpm_suspend_noirq(pm_message_t state)
  687. {
  688. ktime_t starttime = ktime_get();
  689. int error = 0;
  690. suspend_device_irqs();
  691. mutex_lock(&dpm_list_mtx);
  692. while (!list_empty(&dpm_suspended_list)) {
  693. struct device *dev = to_device(dpm_suspended_list.prev);
  694. get_device(dev);
  695. mutex_unlock(&dpm_list_mtx);
  696. error = device_suspend_noirq(dev, state);
  697. mutex_lock(&dpm_list_mtx);
  698. if (error) {
  699. pm_dev_err(dev, state, " late", error);
  700. put_device(dev);
  701. break;
  702. }
  703. if (!list_empty(&dev->power.entry))
  704. list_move(&dev->power.entry, &dpm_noirq_list);
  705. put_device(dev);
  706. }
  707. mutex_unlock(&dpm_list_mtx);
  708. if (error)
  709. dpm_resume_noirq(resume_event(state));
  710. else
  711. dpm_show_time(starttime, state, "late");
  712. return error;
  713. }
  714. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  715. /**
  716. * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
  717. * @dev: Device to suspend.
  718. * @state: PM transition of the system being carried out.
  719. * @cb: Suspend callback to execute.
  720. */
  721. static int legacy_suspend(struct device *dev, pm_message_t state,
  722. int (*cb)(struct device *dev, pm_message_t state))
  723. {
  724. int error;
  725. ktime_t calltime;
  726. calltime = initcall_debug_start(dev);
  727. error = cb(dev, state);
  728. suspend_report_result(cb, error);
  729. initcall_debug_report(dev, calltime, error);
  730. return error;
  731. }
  732. /**
  733. * device_suspend - Execute "suspend" callbacks for given device.
  734. * @dev: Device to handle.
  735. * @state: PM transition of the system being carried out.
  736. * @async: If true, the device is being suspended asynchronously.
  737. */
  738. static int __device_suspend(struct device *dev, pm_message_t state, bool async)
  739. {
  740. int error = 0;
  741. dpm_wait_for_children(dev, async);
  742. device_lock(dev);
  743. if (async_error)
  744. goto End;
  745. if (pm_wakeup_pending()) {
  746. async_error = -EBUSY;
  747. goto End;
  748. }
  749. if (dev->class) {
  750. if (dev->class->pm) {
  751. pm_dev_dbg(dev, state, "class ");
  752. error = pm_op(dev, dev->class->pm, state);
  753. } else if (dev->class->suspend) {
  754. pm_dev_dbg(dev, state, "legacy class ");
  755. error = legacy_suspend(dev, state, dev->class->suspend);
  756. }
  757. if (error)
  758. goto End;
  759. }
  760. if (dev->type) {
  761. if (dev->type->pm) {
  762. pm_dev_dbg(dev, state, "type ");
  763. error = pm_op(dev, dev->type->pm, state);
  764. }
  765. if (error)
  766. goto End;
  767. }
  768. if (dev->bus) {
  769. if (dev->bus->pm) {
  770. pm_dev_dbg(dev, state, "");
  771. error = pm_op(dev, dev->bus->pm, state);
  772. } else if (dev->bus->suspend) {
  773. pm_dev_dbg(dev, state, "legacy ");
  774. error = legacy_suspend(dev, state, dev->bus->suspend);
  775. }
  776. if (error)
  777. goto End;
  778. }
  779. if (dev->pwr_domain) {
  780. pm_dev_dbg(dev, state, "power domain ");
  781. pm_op(dev, &dev->pwr_domain->ops, state);
  782. }
  783. End:
  784. device_unlock(dev);
  785. complete_all(&dev->power.completion);
  786. if (error)
  787. async_error = error;
  788. return error;
  789. }
  790. static void async_suspend(void *data, async_cookie_t cookie)
  791. {
  792. struct device *dev = (struct device *)data;
  793. int error;
  794. error = __device_suspend(dev, pm_transition, true);
  795. if (error)
  796. pm_dev_err(dev, pm_transition, " async", error);
  797. put_device(dev);
  798. }
  799. static int device_suspend(struct device *dev)
  800. {
  801. INIT_COMPLETION(dev->power.completion);
  802. if (pm_async_enabled && dev->power.async_suspend) {
  803. get_device(dev);
  804. async_schedule(async_suspend, dev);
  805. return 0;
  806. }
  807. return __device_suspend(dev, pm_transition, false);
  808. }
  809. /**
  810. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  811. * @state: PM transition of the system being carried out.
  812. */
  813. static int dpm_suspend(pm_message_t state)
  814. {
  815. ktime_t starttime = ktime_get();
  816. int error = 0;
  817. mutex_lock(&dpm_list_mtx);
  818. pm_transition = state;
  819. async_error = 0;
  820. while (!list_empty(&dpm_prepared_list)) {
  821. struct device *dev = to_device(dpm_prepared_list.prev);
  822. get_device(dev);
  823. mutex_unlock(&dpm_list_mtx);
  824. error = device_suspend(dev);
  825. mutex_lock(&dpm_list_mtx);
  826. if (error) {
  827. pm_dev_err(dev, state, "", error);
  828. put_device(dev);
  829. break;
  830. }
  831. if (!list_empty(&dev->power.entry))
  832. list_move(&dev->power.entry, &dpm_suspended_list);
  833. put_device(dev);
  834. if (async_error)
  835. break;
  836. }
  837. mutex_unlock(&dpm_list_mtx);
  838. async_synchronize_full();
  839. if (!error)
  840. error = async_error;
  841. if (!error)
  842. dpm_show_time(starttime, state, NULL);
  843. return error;
  844. }
  845. /**
  846. * device_prepare - Prepare a device for system power transition.
  847. * @dev: Device to handle.
  848. * @state: PM transition of the system being carried out.
  849. *
  850. * Execute the ->prepare() callback(s) for given device. No new children of the
  851. * device may be registered after this function has returned.
  852. */
  853. static int device_prepare(struct device *dev, pm_message_t state)
  854. {
  855. int error = 0;
  856. device_lock(dev);
  857. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  858. pm_dev_dbg(dev, state, "preparing ");
  859. error = dev->bus->pm->prepare(dev);
  860. suspend_report_result(dev->bus->pm->prepare, error);
  861. if (error)
  862. goto End;
  863. }
  864. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  865. pm_dev_dbg(dev, state, "preparing type ");
  866. error = dev->type->pm->prepare(dev);
  867. suspend_report_result(dev->type->pm->prepare, error);
  868. if (error)
  869. goto End;
  870. }
  871. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  872. pm_dev_dbg(dev, state, "preparing class ");
  873. error = dev->class->pm->prepare(dev);
  874. suspend_report_result(dev->class->pm->prepare, error);
  875. if (error)
  876. goto End;
  877. }
  878. if (dev->pwr_domain && dev->pwr_domain->ops.prepare) {
  879. pm_dev_dbg(dev, state, "preparing power domain ");
  880. dev->pwr_domain->ops.prepare(dev);
  881. }
  882. End:
  883. device_unlock(dev);
  884. return error;
  885. }
  886. /**
  887. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  888. * @state: PM transition of the system being carried out.
  889. *
  890. * Execute the ->prepare() callback(s) for all devices.
  891. */
  892. static int dpm_prepare(pm_message_t state)
  893. {
  894. int error = 0;
  895. mutex_lock(&dpm_list_mtx);
  896. while (!list_empty(&dpm_list)) {
  897. struct device *dev = to_device(dpm_list.next);
  898. get_device(dev);
  899. mutex_unlock(&dpm_list_mtx);
  900. pm_runtime_get_noresume(dev);
  901. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  902. pm_wakeup_event(dev, 0);
  903. pm_runtime_put_sync(dev);
  904. error = pm_wakeup_pending() ?
  905. -EBUSY : device_prepare(dev, state);
  906. mutex_lock(&dpm_list_mtx);
  907. if (error) {
  908. if (error == -EAGAIN) {
  909. put_device(dev);
  910. error = 0;
  911. continue;
  912. }
  913. printk(KERN_INFO "PM: Device %s not prepared "
  914. "for power transition: code %d\n",
  915. dev_name(dev), error);
  916. put_device(dev);
  917. break;
  918. }
  919. dev->power.in_suspend = true;
  920. if (!list_empty(&dev->power.entry))
  921. list_move_tail(&dev->power.entry, &dpm_prepared_list);
  922. put_device(dev);
  923. }
  924. mutex_unlock(&dpm_list_mtx);
  925. return error;
  926. }
  927. /**
  928. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  929. * @state: PM transition of the system being carried out.
  930. *
  931. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  932. * callbacks for them.
  933. */
  934. int dpm_suspend_start(pm_message_t state)
  935. {
  936. int error;
  937. might_sleep();
  938. error = dpm_prepare(state);
  939. if (!error)
  940. error = dpm_suspend(state);
  941. return error;
  942. }
  943. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  944. void __suspend_report_result(const char *function, void *fn, int ret)
  945. {
  946. if (ret)
  947. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  948. }
  949. EXPORT_SYMBOL_GPL(__suspend_report_result);
  950. /**
  951. * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
  952. * @dev: Device to wait for.
  953. * @subordinate: Device that needs to wait for @dev.
  954. */
  955. int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
  956. {
  957. dpm_wait(dev, subordinate->power.async_suspend);
  958. return async_error;
  959. }
  960. EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);