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