main.c 26 KB

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