main.c 27 KB

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