main.c 27 KB

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