runtime.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * drivers/base/power/runtime.c - Helper functions for device runtime PM
  3. *
  4. * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  5. * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/pm_runtime.h>
  11. #include <trace/events/rpm.h>
  12. #include "power.h"
  13. static int rpm_resume(struct device *dev, int rpmflags);
  14. static int rpm_suspend(struct device *dev, int rpmflags);
  15. /**
  16. * update_pm_runtime_accounting - Update the time accounting of power states
  17. * @dev: Device to update the accounting for
  18. *
  19. * In order to be able to have time accounting of the various power states
  20. * (as used by programs such as PowerTOP to show the effectiveness of runtime
  21. * PM), we need to track the time spent in each state.
  22. * update_pm_runtime_accounting must be called each time before the
  23. * runtime_status field is updated, to account the time in the old state
  24. * correctly.
  25. */
  26. void update_pm_runtime_accounting(struct device *dev)
  27. {
  28. unsigned long now = jiffies;
  29. int delta;
  30. delta = now - dev->power.accounting_timestamp;
  31. if (delta < 0)
  32. delta = 0;
  33. dev->power.accounting_timestamp = now;
  34. if (dev->power.disable_depth > 0)
  35. return;
  36. if (dev->power.runtime_status == RPM_SUSPENDED)
  37. dev->power.suspended_jiffies += delta;
  38. else
  39. dev->power.active_jiffies += delta;
  40. }
  41. static void __update_runtime_status(struct device *dev, enum rpm_status status)
  42. {
  43. update_pm_runtime_accounting(dev);
  44. dev->power.runtime_status = status;
  45. }
  46. /**
  47. * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
  48. * @dev: Device to handle.
  49. */
  50. static void pm_runtime_deactivate_timer(struct device *dev)
  51. {
  52. if (dev->power.timer_expires > 0) {
  53. del_timer(&dev->power.suspend_timer);
  54. dev->power.timer_expires = 0;
  55. }
  56. }
  57. /**
  58. * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
  59. * @dev: Device to handle.
  60. */
  61. static void pm_runtime_cancel_pending(struct device *dev)
  62. {
  63. pm_runtime_deactivate_timer(dev);
  64. /*
  65. * In case there's a request pending, make sure its work function will
  66. * return without doing anything.
  67. */
  68. dev->power.request = RPM_REQ_NONE;
  69. }
  70. /*
  71. * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
  72. * @dev: Device to handle.
  73. *
  74. * Compute the autosuspend-delay expiration time based on the device's
  75. * power.last_busy time. If the delay has already expired or is disabled
  76. * (negative) or the power.use_autosuspend flag isn't set, return 0.
  77. * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
  78. *
  79. * This function may be called either with or without dev->power.lock held.
  80. * Either way it can be racy, since power.last_busy may be updated at any time.
  81. */
  82. unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
  83. {
  84. int autosuspend_delay;
  85. long elapsed;
  86. unsigned long last_busy;
  87. unsigned long expires = 0;
  88. if (!dev->power.use_autosuspend)
  89. goto out;
  90. autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
  91. if (autosuspend_delay < 0)
  92. goto out;
  93. last_busy = ACCESS_ONCE(dev->power.last_busy);
  94. elapsed = jiffies - last_busy;
  95. if (elapsed < 0)
  96. goto out; /* jiffies has wrapped around. */
  97. /*
  98. * If the autosuspend_delay is >= 1 second, align the timer by rounding
  99. * up to the nearest second.
  100. */
  101. expires = last_busy + msecs_to_jiffies(autosuspend_delay);
  102. if (autosuspend_delay >= 1000)
  103. expires = round_jiffies(expires);
  104. expires += !expires;
  105. if (elapsed >= expires - last_busy)
  106. expires = 0; /* Already expired. */
  107. out:
  108. return expires;
  109. }
  110. EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
  111. /**
  112. * rpm_check_suspend_allowed - Test whether a device may be suspended.
  113. * @dev: Device to test.
  114. */
  115. static int rpm_check_suspend_allowed(struct device *dev)
  116. {
  117. int retval = 0;
  118. if (dev->power.runtime_error)
  119. retval = -EINVAL;
  120. else if (dev->power.disable_depth > 0)
  121. retval = -EACCES;
  122. else if (atomic_read(&dev->power.usage_count) > 0)
  123. retval = -EAGAIN;
  124. else if (!pm_children_suspended(dev))
  125. retval = -EBUSY;
  126. /* Pending resume requests take precedence over suspends. */
  127. else if ((dev->power.deferred_resume
  128. && dev->power.runtime_status == RPM_SUSPENDING)
  129. || (dev->power.request_pending
  130. && dev->power.request == RPM_REQ_RESUME))
  131. retval = -EAGAIN;
  132. else if (dev->power.runtime_status == RPM_SUSPENDED)
  133. retval = 1;
  134. return retval;
  135. }
  136. /**
  137. * __rpm_callback - Run a given runtime PM callback for a given device.
  138. * @cb: Runtime PM callback to run.
  139. * @dev: Device to run the callback for.
  140. */
  141. static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
  142. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  143. {
  144. int retval;
  145. if (dev->power.irq_safe)
  146. spin_unlock(&dev->power.lock);
  147. else
  148. spin_unlock_irq(&dev->power.lock);
  149. retval = cb(dev);
  150. if (dev->power.irq_safe)
  151. spin_lock(&dev->power.lock);
  152. else
  153. spin_lock_irq(&dev->power.lock);
  154. return retval;
  155. }
  156. /**
  157. * rpm_idle - Notify device bus type if the device can be suspended.
  158. * @dev: Device to notify the bus type about.
  159. * @rpmflags: Flag bits.
  160. *
  161. * Check if the device's runtime PM status allows it to be suspended. If
  162. * another idle notification has been started earlier, return immediately. If
  163. * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
  164. * run the ->runtime_idle() callback directly.
  165. *
  166. * This function must be called under dev->power.lock with interrupts disabled.
  167. */
  168. static int rpm_idle(struct device *dev, int rpmflags)
  169. {
  170. int (*callback)(struct device *);
  171. int retval;
  172. trace_rpm_idle(dev, rpmflags);
  173. retval = rpm_check_suspend_allowed(dev);
  174. if (retval < 0)
  175. ; /* Conditions are wrong. */
  176. /* Idle notifications are allowed only in the RPM_ACTIVE state. */
  177. else if (dev->power.runtime_status != RPM_ACTIVE)
  178. retval = -EAGAIN;
  179. /*
  180. * Any pending request other than an idle notification takes
  181. * precedence over us, except that the timer may be running.
  182. */
  183. else if (dev->power.request_pending &&
  184. dev->power.request > RPM_REQ_IDLE)
  185. retval = -EAGAIN;
  186. /* Act as though RPM_NOWAIT is always set. */
  187. else if (dev->power.idle_notification)
  188. retval = -EINPROGRESS;
  189. if (retval)
  190. goto out;
  191. /* Pending requests need to be canceled. */
  192. dev->power.request = RPM_REQ_NONE;
  193. if (dev->power.no_callbacks) {
  194. /* Assume ->runtime_idle() callback would have suspended. */
  195. retval = rpm_suspend(dev, rpmflags);
  196. goto out;
  197. }
  198. /* Carry out an asynchronous or a synchronous idle notification. */
  199. if (rpmflags & RPM_ASYNC) {
  200. dev->power.request = RPM_REQ_IDLE;
  201. if (!dev->power.request_pending) {
  202. dev->power.request_pending = true;
  203. queue_work(pm_wq, &dev->power.work);
  204. }
  205. goto out;
  206. }
  207. dev->power.idle_notification = true;
  208. if (dev->pm_domain)
  209. callback = dev->pm_domain->ops.runtime_idle;
  210. else if (dev->type && dev->type->pm)
  211. callback = dev->type->pm->runtime_idle;
  212. else if (dev->class && dev->class->pm)
  213. callback = dev->class->pm->runtime_idle;
  214. else if (dev->bus && dev->bus->pm)
  215. callback = dev->bus->pm->runtime_idle;
  216. else
  217. callback = NULL;
  218. if (callback)
  219. __rpm_callback(callback, dev);
  220. dev->power.idle_notification = false;
  221. wake_up_all(&dev->power.wait_queue);
  222. out:
  223. trace_rpm_return_int(dev, _THIS_IP_, retval);
  224. return retval;
  225. }
  226. /**
  227. * rpm_callback - Run a given runtime PM callback for a given device.
  228. * @cb: Runtime PM callback to run.
  229. * @dev: Device to run the callback for.
  230. */
  231. static int rpm_callback(int (*cb)(struct device *), struct device *dev)
  232. {
  233. int retval;
  234. if (!cb)
  235. return -ENOSYS;
  236. retval = __rpm_callback(cb, dev);
  237. dev->power.runtime_error = retval;
  238. return retval != -EACCES ? retval : -EIO;
  239. }
  240. /**
  241. * rpm_suspend - Carry out runtime suspend of given device.
  242. * @dev: Device to suspend.
  243. * @rpmflags: Flag bits.
  244. *
  245. * Check if the device's runtime PM status allows it to be suspended.
  246. * Cancel a pending idle notification, autosuspend or suspend. If
  247. * another suspend has been started earlier, either return immediately
  248. * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
  249. * flags. If the RPM_ASYNC flag is set then queue a suspend request;
  250. * otherwise run the ->runtime_suspend() callback directly. If a deferred
  251. * resume was requested while the callback was running then carry it out;
  252. * otherwise send an idle notification for its parent (if the suspend
  253. * succeeded and both ignore_children of parent->power and irq_safe of
  254. * dev->power are not set).
  255. *
  256. * This function must be called under dev->power.lock with interrupts disabled.
  257. */
  258. static int rpm_suspend(struct device *dev, int rpmflags)
  259. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  260. {
  261. int (*callback)(struct device *);
  262. struct device *parent = NULL;
  263. int retval;
  264. trace_rpm_suspend(dev, rpmflags);
  265. repeat:
  266. retval = rpm_check_suspend_allowed(dev);
  267. if (retval < 0)
  268. ; /* Conditions are wrong. */
  269. /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
  270. else if (dev->power.runtime_status == RPM_RESUMING &&
  271. !(rpmflags & RPM_ASYNC))
  272. retval = -EAGAIN;
  273. if (retval)
  274. goto out;
  275. /* If the autosuspend_delay time hasn't expired yet, reschedule. */
  276. if ((rpmflags & RPM_AUTO)
  277. && dev->power.runtime_status != RPM_SUSPENDING) {
  278. unsigned long expires = pm_runtime_autosuspend_expiration(dev);
  279. if (expires != 0) {
  280. /* Pending requests need to be canceled. */
  281. dev->power.request = RPM_REQ_NONE;
  282. /*
  283. * Optimization: If the timer is already running and is
  284. * set to expire at or before the autosuspend delay,
  285. * avoid the overhead of resetting it. Just let it
  286. * expire; pm_suspend_timer_fn() will take care of the
  287. * rest.
  288. */
  289. if (!(dev->power.timer_expires && time_before_eq(
  290. dev->power.timer_expires, expires))) {
  291. dev->power.timer_expires = expires;
  292. mod_timer(&dev->power.suspend_timer, expires);
  293. }
  294. dev->power.timer_autosuspends = 1;
  295. goto out;
  296. }
  297. }
  298. /* Other scheduled or pending requests need to be canceled. */
  299. pm_runtime_cancel_pending(dev);
  300. if (dev->power.runtime_status == RPM_SUSPENDING) {
  301. DEFINE_WAIT(wait);
  302. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  303. retval = -EINPROGRESS;
  304. goto out;
  305. }
  306. if (dev->power.irq_safe) {
  307. spin_unlock(&dev->power.lock);
  308. cpu_relax();
  309. spin_lock(&dev->power.lock);
  310. goto repeat;
  311. }
  312. /* Wait for the other suspend running in parallel with us. */
  313. for (;;) {
  314. prepare_to_wait(&dev->power.wait_queue, &wait,
  315. TASK_UNINTERRUPTIBLE);
  316. if (dev->power.runtime_status != RPM_SUSPENDING)
  317. break;
  318. spin_unlock_irq(&dev->power.lock);
  319. schedule();
  320. spin_lock_irq(&dev->power.lock);
  321. }
  322. finish_wait(&dev->power.wait_queue, &wait);
  323. goto repeat;
  324. }
  325. dev->power.deferred_resume = false;
  326. if (dev->power.no_callbacks)
  327. goto no_callback; /* Assume success. */
  328. /* Carry out an asynchronous or a synchronous suspend. */
  329. if (rpmflags & RPM_ASYNC) {
  330. dev->power.request = (rpmflags & RPM_AUTO) ?
  331. RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
  332. if (!dev->power.request_pending) {
  333. dev->power.request_pending = true;
  334. queue_work(pm_wq, &dev->power.work);
  335. }
  336. goto out;
  337. }
  338. __update_runtime_status(dev, RPM_SUSPENDING);
  339. if (dev->pm_domain)
  340. callback = dev->pm_domain->ops.runtime_suspend;
  341. else if (dev->type && dev->type->pm)
  342. callback = dev->type->pm->runtime_suspend;
  343. else if (dev->class && dev->class->pm)
  344. callback = dev->class->pm->runtime_suspend;
  345. else if (dev->bus && dev->bus->pm)
  346. callback = dev->bus->pm->runtime_suspend;
  347. else
  348. callback = NULL;
  349. retval = rpm_callback(callback, dev);
  350. if (retval) {
  351. __update_runtime_status(dev, RPM_ACTIVE);
  352. dev->power.deferred_resume = false;
  353. if (retval == -EAGAIN || retval == -EBUSY)
  354. dev->power.runtime_error = 0;
  355. else
  356. pm_runtime_cancel_pending(dev);
  357. } else {
  358. no_callback:
  359. __update_runtime_status(dev, RPM_SUSPENDED);
  360. pm_runtime_deactivate_timer(dev);
  361. if (dev->parent) {
  362. parent = dev->parent;
  363. atomic_add_unless(&parent->power.child_count, -1, 0);
  364. }
  365. }
  366. wake_up_all(&dev->power.wait_queue);
  367. if (dev->power.deferred_resume) {
  368. rpm_resume(dev, 0);
  369. retval = -EAGAIN;
  370. goto out;
  371. }
  372. /* Maybe the parent is now able to suspend. */
  373. if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
  374. spin_unlock(&dev->power.lock);
  375. spin_lock(&parent->power.lock);
  376. rpm_idle(parent, RPM_ASYNC);
  377. spin_unlock(&parent->power.lock);
  378. spin_lock(&dev->power.lock);
  379. }
  380. out:
  381. trace_rpm_return_int(dev, _THIS_IP_, retval);
  382. return retval;
  383. }
  384. /**
  385. * rpm_resume - Carry out runtime resume of given device.
  386. * @dev: Device to resume.
  387. * @rpmflags: Flag bits.
  388. *
  389. * Check if the device's runtime PM status allows it to be resumed. Cancel
  390. * any scheduled or pending requests. If another resume has been started
  391. * earlier, either return immediately or wait for it to finish, depending on the
  392. * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
  393. * parallel with this function, either tell the other process to resume after
  394. * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
  395. * flag is set then queue a resume request; otherwise run the
  396. * ->runtime_resume() callback directly. Queue an idle notification for the
  397. * device if the resume succeeded.
  398. *
  399. * This function must be called under dev->power.lock with interrupts disabled.
  400. */
  401. static int rpm_resume(struct device *dev, int rpmflags)
  402. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  403. {
  404. int (*callback)(struct device *);
  405. struct device *parent = NULL;
  406. int retval = 0;
  407. trace_rpm_resume(dev, rpmflags);
  408. repeat:
  409. if (dev->power.runtime_error)
  410. retval = -EINVAL;
  411. else if (dev->power.disable_depth > 0)
  412. retval = -EACCES;
  413. if (retval)
  414. goto out;
  415. /*
  416. * Other scheduled or pending requests need to be canceled. Small
  417. * optimization: If an autosuspend timer is running, leave it running
  418. * rather than cancelling it now only to restart it again in the near
  419. * future.
  420. */
  421. dev->power.request = RPM_REQ_NONE;
  422. if (!dev->power.timer_autosuspends)
  423. pm_runtime_deactivate_timer(dev);
  424. if (dev->power.runtime_status == RPM_ACTIVE) {
  425. retval = 1;
  426. goto out;
  427. }
  428. if (dev->power.runtime_status == RPM_RESUMING
  429. || dev->power.runtime_status == RPM_SUSPENDING) {
  430. DEFINE_WAIT(wait);
  431. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  432. if (dev->power.runtime_status == RPM_SUSPENDING)
  433. dev->power.deferred_resume = true;
  434. else
  435. retval = -EINPROGRESS;
  436. goto out;
  437. }
  438. if (dev->power.irq_safe) {
  439. spin_unlock(&dev->power.lock);
  440. cpu_relax();
  441. spin_lock(&dev->power.lock);
  442. goto repeat;
  443. }
  444. /* Wait for the operation carried out in parallel with us. */
  445. for (;;) {
  446. prepare_to_wait(&dev->power.wait_queue, &wait,
  447. TASK_UNINTERRUPTIBLE);
  448. if (dev->power.runtime_status != RPM_RESUMING
  449. && dev->power.runtime_status != RPM_SUSPENDING)
  450. break;
  451. spin_unlock_irq(&dev->power.lock);
  452. schedule();
  453. spin_lock_irq(&dev->power.lock);
  454. }
  455. finish_wait(&dev->power.wait_queue, &wait);
  456. goto repeat;
  457. }
  458. /*
  459. * See if we can skip waking up the parent. This is safe only if
  460. * power.no_callbacks is set, because otherwise we don't know whether
  461. * the resume will actually succeed.
  462. */
  463. if (dev->power.no_callbacks && !parent && dev->parent) {
  464. spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
  465. if (dev->parent->power.disable_depth > 0
  466. || dev->parent->power.ignore_children
  467. || dev->parent->power.runtime_status == RPM_ACTIVE) {
  468. atomic_inc(&dev->parent->power.child_count);
  469. spin_unlock(&dev->parent->power.lock);
  470. goto no_callback; /* Assume success. */
  471. }
  472. spin_unlock(&dev->parent->power.lock);
  473. }
  474. /* Carry out an asynchronous or a synchronous resume. */
  475. if (rpmflags & RPM_ASYNC) {
  476. dev->power.request = RPM_REQ_RESUME;
  477. if (!dev->power.request_pending) {
  478. dev->power.request_pending = true;
  479. queue_work(pm_wq, &dev->power.work);
  480. }
  481. retval = 0;
  482. goto out;
  483. }
  484. if (!parent && dev->parent) {
  485. /*
  486. * Increment the parent's usage counter and resume it if
  487. * necessary. Not needed if dev is irq-safe; then the
  488. * parent is permanently resumed.
  489. */
  490. parent = dev->parent;
  491. if (dev->power.irq_safe)
  492. goto skip_parent;
  493. spin_unlock(&dev->power.lock);
  494. pm_runtime_get_noresume(parent);
  495. spin_lock(&parent->power.lock);
  496. /*
  497. * We can resume if the parent's runtime PM is disabled or it
  498. * is set to ignore children.
  499. */
  500. if (!parent->power.disable_depth
  501. && !parent->power.ignore_children) {
  502. rpm_resume(parent, 0);
  503. if (parent->power.runtime_status != RPM_ACTIVE)
  504. retval = -EBUSY;
  505. }
  506. spin_unlock(&parent->power.lock);
  507. spin_lock(&dev->power.lock);
  508. if (retval)
  509. goto out;
  510. goto repeat;
  511. }
  512. skip_parent:
  513. if (dev->power.no_callbacks)
  514. goto no_callback; /* Assume success. */
  515. __update_runtime_status(dev, RPM_RESUMING);
  516. if (dev->pm_domain)
  517. callback = dev->pm_domain->ops.runtime_resume;
  518. else if (dev->type && dev->type->pm)
  519. callback = dev->type->pm->runtime_resume;
  520. else if (dev->class && dev->class->pm)
  521. callback = dev->class->pm->runtime_resume;
  522. else if (dev->bus && dev->bus->pm)
  523. callback = dev->bus->pm->runtime_resume;
  524. else
  525. callback = NULL;
  526. retval = rpm_callback(callback, dev);
  527. if (retval) {
  528. __update_runtime_status(dev, RPM_SUSPENDED);
  529. pm_runtime_cancel_pending(dev);
  530. } else {
  531. no_callback:
  532. __update_runtime_status(dev, RPM_ACTIVE);
  533. if (parent)
  534. atomic_inc(&parent->power.child_count);
  535. }
  536. wake_up_all(&dev->power.wait_queue);
  537. if (!retval)
  538. rpm_idle(dev, RPM_ASYNC);
  539. out:
  540. if (parent && !dev->power.irq_safe) {
  541. spin_unlock_irq(&dev->power.lock);
  542. pm_runtime_put(parent);
  543. spin_lock_irq(&dev->power.lock);
  544. }
  545. trace_rpm_return_int(dev, _THIS_IP_, retval);
  546. return retval;
  547. }
  548. /**
  549. * pm_runtime_work - Universal runtime PM work function.
  550. * @work: Work structure used for scheduling the execution of this function.
  551. *
  552. * Use @work to get the device object the work is to be done for, determine what
  553. * is to be done and execute the appropriate runtime PM function.
  554. */
  555. static void pm_runtime_work(struct work_struct *work)
  556. {
  557. struct device *dev = container_of(work, struct device, power.work);
  558. enum rpm_request req;
  559. spin_lock_irq(&dev->power.lock);
  560. if (!dev->power.request_pending)
  561. goto out;
  562. req = dev->power.request;
  563. dev->power.request = RPM_REQ_NONE;
  564. dev->power.request_pending = false;
  565. switch (req) {
  566. case RPM_REQ_NONE:
  567. break;
  568. case RPM_REQ_IDLE:
  569. rpm_idle(dev, RPM_NOWAIT);
  570. break;
  571. case RPM_REQ_SUSPEND:
  572. rpm_suspend(dev, RPM_NOWAIT);
  573. break;
  574. case RPM_REQ_AUTOSUSPEND:
  575. rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
  576. break;
  577. case RPM_REQ_RESUME:
  578. rpm_resume(dev, RPM_NOWAIT);
  579. break;
  580. }
  581. out:
  582. spin_unlock_irq(&dev->power.lock);
  583. }
  584. /**
  585. * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
  586. * @data: Device pointer passed by pm_schedule_suspend().
  587. *
  588. * Check if the time is right and queue a suspend request.
  589. */
  590. static void pm_suspend_timer_fn(unsigned long data)
  591. {
  592. struct device *dev = (struct device *)data;
  593. unsigned long flags;
  594. unsigned long expires;
  595. spin_lock_irqsave(&dev->power.lock, flags);
  596. expires = dev->power.timer_expires;
  597. /* If 'expire' is after 'jiffies' we've been called too early. */
  598. if (expires > 0 && !time_after(expires, jiffies)) {
  599. dev->power.timer_expires = 0;
  600. rpm_suspend(dev, dev->power.timer_autosuspends ?
  601. (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
  602. }
  603. spin_unlock_irqrestore(&dev->power.lock, flags);
  604. }
  605. /**
  606. * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
  607. * @dev: Device to suspend.
  608. * @delay: Time to wait before submitting a suspend request, in milliseconds.
  609. */
  610. int pm_schedule_suspend(struct device *dev, unsigned int delay)
  611. {
  612. unsigned long flags;
  613. int retval;
  614. spin_lock_irqsave(&dev->power.lock, flags);
  615. if (!delay) {
  616. retval = rpm_suspend(dev, RPM_ASYNC);
  617. goto out;
  618. }
  619. retval = rpm_check_suspend_allowed(dev);
  620. if (retval)
  621. goto out;
  622. /* Other scheduled or pending requests need to be canceled. */
  623. pm_runtime_cancel_pending(dev);
  624. dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
  625. dev->power.timer_expires += !dev->power.timer_expires;
  626. dev->power.timer_autosuspends = 0;
  627. mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
  628. out:
  629. spin_unlock_irqrestore(&dev->power.lock, flags);
  630. return retval;
  631. }
  632. EXPORT_SYMBOL_GPL(pm_schedule_suspend);
  633. /**
  634. * __pm_runtime_idle - Entry point for runtime idle operations.
  635. * @dev: Device to send idle notification for.
  636. * @rpmflags: Flag bits.
  637. *
  638. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  639. * return immediately if it is larger than zero. Then carry out an idle
  640. * notification, either synchronous or asynchronous.
  641. *
  642. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  643. * or if pm_runtime_irq_safe() has been called.
  644. */
  645. int __pm_runtime_idle(struct device *dev, int rpmflags)
  646. {
  647. unsigned long flags;
  648. int retval;
  649. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  650. if (rpmflags & RPM_GET_PUT) {
  651. if (!atomic_dec_and_test(&dev->power.usage_count))
  652. return 0;
  653. }
  654. spin_lock_irqsave(&dev->power.lock, flags);
  655. retval = rpm_idle(dev, rpmflags);
  656. spin_unlock_irqrestore(&dev->power.lock, flags);
  657. return retval;
  658. }
  659. EXPORT_SYMBOL_GPL(__pm_runtime_idle);
  660. /**
  661. * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
  662. * @dev: Device to suspend.
  663. * @rpmflags: Flag bits.
  664. *
  665. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  666. * return immediately if it is larger than zero. Then carry out a suspend,
  667. * either synchronous or asynchronous.
  668. *
  669. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  670. * or if pm_runtime_irq_safe() has been called.
  671. */
  672. int __pm_runtime_suspend(struct device *dev, int rpmflags)
  673. {
  674. unsigned long flags;
  675. int retval;
  676. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  677. if (rpmflags & RPM_GET_PUT) {
  678. if (!atomic_dec_and_test(&dev->power.usage_count))
  679. return 0;
  680. }
  681. spin_lock_irqsave(&dev->power.lock, flags);
  682. retval = rpm_suspend(dev, rpmflags);
  683. spin_unlock_irqrestore(&dev->power.lock, flags);
  684. return retval;
  685. }
  686. EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
  687. /**
  688. * __pm_runtime_resume - Entry point for runtime resume operations.
  689. * @dev: Device to resume.
  690. * @rpmflags: Flag bits.
  691. *
  692. * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
  693. * carry out a resume, either synchronous or asynchronous.
  694. *
  695. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  696. * or if pm_runtime_irq_safe() has been called.
  697. */
  698. int __pm_runtime_resume(struct device *dev, int rpmflags)
  699. {
  700. unsigned long flags;
  701. int retval;
  702. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  703. if (rpmflags & RPM_GET_PUT)
  704. atomic_inc(&dev->power.usage_count);
  705. spin_lock_irqsave(&dev->power.lock, flags);
  706. retval = rpm_resume(dev, rpmflags);
  707. spin_unlock_irqrestore(&dev->power.lock, flags);
  708. return retval;
  709. }
  710. EXPORT_SYMBOL_GPL(__pm_runtime_resume);
  711. /**
  712. * __pm_runtime_set_status - Set runtime PM status of a device.
  713. * @dev: Device to handle.
  714. * @status: New runtime PM status of the device.
  715. *
  716. * If runtime PM of the device is disabled or its power.runtime_error field is
  717. * different from zero, the status may be changed either to RPM_ACTIVE, or to
  718. * RPM_SUSPENDED, as long as that reflects the actual state of the device.
  719. * However, if the device has a parent and the parent is not active, and the
  720. * parent's power.ignore_children flag is unset, the device's status cannot be
  721. * set to RPM_ACTIVE, so -EBUSY is returned in that case.
  722. *
  723. * If successful, __pm_runtime_set_status() clears the power.runtime_error field
  724. * and the device parent's counter of unsuspended children is modified to
  725. * reflect the new status. If the new status is RPM_SUSPENDED, an idle
  726. * notification request for the parent is submitted.
  727. */
  728. int __pm_runtime_set_status(struct device *dev, unsigned int status)
  729. {
  730. struct device *parent = dev->parent;
  731. unsigned long flags;
  732. bool notify_parent = false;
  733. int error = 0;
  734. if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
  735. return -EINVAL;
  736. spin_lock_irqsave(&dev->power.lock, flags);
  737. if (!dev->power.runtime_error && !dev->power.disable_depth) {
  738. error = -EAGAIN;
  739. goto out;
  740. }
  741. if (dev->power.runtime_status == status)
  742. goto out_set;
  743. if (status == RPM_SUSPENDED) {
  744. /* It always is possible to set the status to 'suspended'. */
  745. if (parent) {
  746. atomic_add_unless(&parent->power.child_count, -1, 0);
  747. notify_parent = !parent->power.ignore_children;
  748. }
  749. goto out_set;
  750. }
  751. if (parent) {
  752. spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
  753. /*
  754. * It is invalid to put an active child under a parent that is
  755. * not active, has runtime PM enabled and the
  756. * 'power.ignore_children' flag unset.
  757. */
  758. if (!parent->power.disable_depth
  759. && !parent->power.ignore_children
  760. && parent->power.runtime_status != RPM_ACTIVE)
  761. error = -EBUSY;
  762. else if (dev->power.runtime_status == RPM_SUSPENDED)
  763. atomic_inc(&parent->power.child_count);
  764. spin_unlock(&parent->power.lock);
  765. if (error)
  766. goto out;
  767. }
  768. out_set:
  769. __update_runtime_status(dev, status);
  770. dev->power.runtime_error = 0;
  771. out:
  772. spin_unlock_irqrestore(&dev->power.lock, flags);
  773. if (notify_parent)
  774. pm_request_idle(parent);
  775. return error;
  776. }
  777. EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
  778. /**
  779. * __pm_runtime_barrier - Cancel pending requests and wait for completions.
  780. * @dev: Device to handle.
  781. *
  782. * Flush all pending requests for the device from pm_wq and wait for all
  783. * runtime PM operations involving the device in progress to complete.
  784. *
  785. * Should be called under dev->power.lock with interrupts disabled.
  786. */
  787. static void __pm_runtime_barrier(struct device *dev)
  788. {
  789. pm_runtime_deactivate_timer(dev);
  790. if (dev->power.request_pending) {
  791. dev->power.request = RPM_REQ_NONE;
  792. spin_unlock_irq(&dev->power.lock);
  793. cancel_work_sync(&dev->power.work);
  794. spin_lock_irq(&dev->power.lock);
  795. dev->power.request_pending = false;
  796. }
  797. if (dev->power.runtime_status == RPM_SUSPENDING
  798. || dev->power.runtime_status == RPM_RESUMING
  799. || dev->power.idle_notification) {
  800. DEFINE_WAIT(wait);
  801. /* Suspend, wake-up or idle notification in progress. */
  802. for (;;) {
  803. prepare_to_wait(&dev->power.wait_queue, &wait,
  804. TASK_UNINTERRUPTIBLE);
  805. if (dev->power.runtime_status != RPM_SUSPENDING
  806. && dev->power.runtime_status != RPM_RESUMING
  807. && !dev->power.idle_notification)
  808. break;
  809. spin_unlock_irq(&dev->power.lock);
  810. schedule();
  811. spin_lock_irq(&dev->power.lock);
  812. }
  813. finish_wait(&dev->power.wait_queue, &wait);
  814. }
  815. }
  816. /**
  817. * pm_runtime_barrier - Flush pending requests and wait for completions.
  818. * @dev: Device to handle.
  819. *
  820. * Prevent the device from being suspended by incrementing its usage counter and
  821. * if there's a pending resume request for the device, wake the device up.
  822. * Next, make sure that all pending requests for the device have been flushed
  823. * from pm_wq and wait for all runtime PM operations involving the device in
  824. * progress to complete.
  825. *
  826. * Return value:
  827. * 1, if there was a resume request pending and the device had to be woken up,
  828. * 0, otherwise
  829. */
  830. int pm_runtime_barrier(struct device *dev)
  831. {
  832. int retval = 0;
  833. pm_runtime_get_noresume(dev);
  834. spin_lock_irq(&dev->power.lock);
  835. if (dev->power.request_pending
  836. && dev->power.request == RPM_REQ_RESUME) {
  837. rpm_resume(dev, 0);
  838. retval = 1;
  839. }
  840. __pm_runtime_barrier(dev);
  841. spin_unlock_irq(&dev->power.lock);
  842. pm_runtime_put_noidle(dev);
  843. return retval;
  844. }
  845. EXPORT_SYMBOL_GPL(pm_runtime_barrier);
  846. /**
  847. * __pm_runtime_disable - Disable runtime PM of a device.
  848. * @dev: Device to handle.
  849. * @check_resume: If set, check if there's a resume request for the device.
  850. *
  851. * Increment power.disable_depth for the device and if was zero previously,
  852. * cancel all pending runtime PM requests for the device and wait for all
  853. * operations in progress to complete. The device can be either active or
  854. * suspended after its runtime PM has been disabled.
  855. *
  856. * If @check_resume is set and there's a resume request pending when
  857. * __pm_runtime_disable() is called and power.disable_depth is zero, the
  858. * function will wake up the device before disabling its runtime PM.
  859. */
  860. void __pm_runtime_disable(struct device *dev, bool check_resume)
  861. {
  862. spin_lock_irq(&dev->power.lock);
  863. if (dev->power.disable_depth > 0) {
  864. dev->power.disable_depth++;
  865. goto out;
  866. }
  867. /*
  868. * Wake up the device if there's a resume request pending, because that
  869. * means there probably is some I/O to process and disabling runtime PM
  870. * shouldn't prevent the device from processing the I/O.
  871. */
  872. if (check_resume && dev->power.request_pending
  873. && dev->power.request == RPM_REQ_RESUME) {
  874. /*
  875. * Prevent suspends and idle notifications from being carried
  876. * out after we have woken up the device.
  877. */
  878. pm_runtime_get_noresume(dev);
  879. rpm_resume(dev, 0);
  880. pm_runtime_put_noidle(dev);
  881. }
  882. if (!dev->power.disable_depth++)
  883. __pm_runtime_barrier(dev);
  884. out:
  885. spin_unlock_irq(&dev->power.lock);
  886. }
  887. EXPORT_SYMBOL_GPL(__pm_runtime_disable);
  888. /**
  889. * pm_runtime_enable - Enable runtime PM of a device.
  890. * @dev: Device to handle.
  891. */
  892. void pm_runtime_enable(struct device *dev)
  893. {
  894. unsigned long flags;
  895. spin_lock_irqsave(&dev->power.lock, flags);
  896. if (dev->power.disable_depth > 0)
  897. dev->power.disable_depth--;
  898. else
  899. dev_warn(dev, "Unbalanced %s!\n", __func__);
  900. spin_unlock_irqrestore(&dev->power.lock, flags);
  901. }
  902. EXPORT_SYMBOL_GPL(pm_runtime_enable);
  903. /**
  904. * pm_runtime_forbid - Block runtime PM of a device.
  905. * @dev: Device to handle.
  906. *
  907. * Increase the device's usage count and clear its power.runtime_auto flag,
  908. * so that it cannot be suspended at run time until pm_runtime_allow() is called
  909. * for it.
  910. */
  911. void pm_runtime_forbid(struct device *dev)
  912. {
  913. spin_lock_irq(&dev->power.lock);
  914. if (!dev->power.runtime_auto)
  915. goto out;
  916. dev->power.runtime_auto = false;
  917. atomic_inc(&dev->power.usage_count);
  918. rpm_resume(dev, 0);
  919. out:
  920. spin_unlock_irq(&dev->power.lock);
  921. }
  922. EXPORT_SYMBOL_GPL(pm_runtime_forbid);
  923. /**
  924. * pm_runtime_allow - Unblock runtime PM of a device.
  925. * @dev: Device to handle.
  926. *
  927. * Decrease the device's usage count and set its power.runtime_auto flag.
  928. */
  929. void pm_runtime_allow(struct device *dev)
  930. {
  931. spin_lock_irq(&dev->power.lock);
  932. if (dev->power.runtime_auto)
  933. goto out;
  934. dev->power.runtime_auto = true;
  935. if (atomic_dec_and_test(&dev->power.usage_count))
  936. rpm_idle(dev, RPM_AUTO);
  937. out:
  938. spin_unlock_irq(&dev->power.lock);
  939. }
  940. EXPORT_SYMBOL_GPL(pm_runtime_allow);
  941. /**
  942. * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
  943. * @dev: Device to handle.
  944. *
  945. * Set the power.no_callbacks flag, which tells the PM core that this
  946. * device is power-managed through its parent and has no runtime PM
  947. * callbacks of its own. The runtime sysfs attributes will be removed.
  948. */
  949. void pm_runtime_no_callbacks(struct device *dev)
  950. {
  951. spin_lock_irq(&dev->power.lock);
  952. dev->power.no_callbacks = 1;
  953. spin_unlock_irq(&dev->power.lock);
  954. if (device_is_registered(dev))
  955. rpm_sysfs_remove(dev);
  956. }
  957. EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
  958. /**
  959. * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
  960. * @dev: Device to handle
  961. *
  962. * Set the power.irq_safe flag, which tells the PM core that the
  963. * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
  964. * always be invoked with the spinlock held and interrupts disabled. It also
  965. * causes the parent's usage counter to be permanently incremented, preventing
  966. * the parent from runtime suspending -- otherwise an irq-safe child might have
  967. * to wait for a non-irq-safe parent.
  968. */
  969. void pm_runtime_irq_safe(struct device *dev)
  970. {
  971. if (dev->parent)
  972. pm_runtime_get_sync(dev->parent);
  973. spin_lock_irq(&dev->power.lock);
  974. dev->power.irq_safe = 1;
  975. spin_unlock_irq(&dev->power.lock);
  976. }
  977. EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
  978. /**
  979. * update_autosuspend - Handle a change to a device's autosuspend settings.
  980. * @dev: Device to handle.
  981. * @old_delay: The former autosuspend_delay value.
  982. * @old_use: The former use_autosuspend value.
  983. *
  984. * Prevent runtime suspend if the new delay is negative and use_autosuspend is
  985. * set; otherwise allow it. Send an idle notification if suspends are allowed.
  986. *
  987. * This function must be called under dev->power.lock with interrupts disabled.
  988. */
  989. static void update_autosuspend(struct device *dev, int old_delay, int old_use)
  990. {
  991. int delay = dev->power.autosuspend_delay;
  992. /* Should runtime suspend be prevented now? */
  993. if (dev->power.use_autosuspend && delay < 0) {
  994. /* If it used to be allowed then prevent it. */
  995. if (!old_use || old_delay >= 0) {
  996. atomic_inc(&dev->power.usage_count);
  997. rpm_resume(dev, 0);
  998. }
  999. }
  1000. /* Runtime suspend should be allowed now. */
  1001. else {
  1002. /* If it used to be prevented then allow it. */
  1003. if (old_use && old_delay < 0)
  1004. atomic_dec(&dev->power.usage_count);
  1005. /* Maybe we can autosuspend now. */
  1006. rpm_idle(dev, RPM_AUTO);
  1007. }
  1008. }
  1009. /**
  1010. * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
  1011. * @dev: Device to handle.
  1012. * @delay: Value of the new delay in milliseconds.
  1013. *
  1014. * Set the device's power.autosuspend_delay value. If it changes to negative
  1015. * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
  1016. * changes the other way, allow runtime suspends.
  1017. */
  1018. void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
  1019. {
  1020. int old_delay, old_use;
  1021. spin_lock_irq(&dev->power.lock);
  1022. old_delay = dev->power.autosuspend_delay;
  1023. old_use = dev->power.use_autosuspend;
  1024. dev->power.autosuspend_delay = delay;
  1025. update_autosuspend(dev, old_delay, old_use);
  1026. spin_unlock_irq(&dev->power.lock);
  1027. }
  1028. EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
  1029. /**
  1030. * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
  1031. * @dev: Device to handle.
  1032. * @use: New value for use_autosuspend.
  1033. *
  1034. * Set the device's power.use_autosuspend flag, and allow or prevent runtime
  1035. * suspends as needed.
  1036. */
  1037. void __pm_runtime_use_autosuspend(struct device *dev, bool use)
  1038. {
  1039. int old_delay, old_use;
  1040. spin_lock_irq(&dev->power.lock);
  1041. old_delay = dev->power.autosuspend_delay;
  1042. old_use = dev->power.use_autosuspend;
  1043. dev->power.use_autosuspend = use;
  1044. update_autosuspend(dev, old_delay, old_use);
  1045. spin_unlock_irq(&dev->power.lock);
  1046. }
  1047. EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
  1048. /**
  1049. * pm_runtime_init - Initialize runtime PM fields in given device object.
  1050. * @dev: Device object to initialize.
  1051. */
  1052. void pm_runtime_init(struct device *dev)
  1053. {
  1054. dev->power.runtime_status = RPM_SUSPENDED;
  1055. dev->power.idle_notification = false;
  1056. dev->power.disable_depth = 1;
  1057. atomic_set(&dev->power.usage_count, 0);
  1058. dev->power.runtime_error = 0;
  1059. atomic_set(&dev->power.child_count, 0);
  1060. pm_suspend_ignore_children(dev, false);
  1061. dev->power.runtime_auto = true;
  1062. dev->power.request_pending = false;
  1063. dev->power.request = RPM_REQ_NONE;
  1064. dev->power.deferred_resume = false;
  1065. dev->power.accounting_timestamp = jiffies;
  1066. INIT_WORK(&dev->power.work, pm_runtime_work);
  1067. dev->power.timer_expires = 0;
  1068. setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
  1069. (unsigned long)dev);
  1070. init_waitqueue_head(&dev->power.wait_queue);
  1071. }
  1072. /**
  1073. * pm_runtime_remove - Prepare for removing a device from device hierarchy.
  1074. * @dev: Device object being removed from device hierarchy.
  1075. */
  1076. void pm_runtime_remove(struct device *dev)
  1077. {
  1078. __pm_runtime_disable(dev, false);
  1079. /* Change the status back to 'suspended' to match the initial status. */
  1080. if (dev->power.runtime_status == RPM_ACTIVE)
  1081. pm_runtime_set_suspended(dev);
  1082. if (dev->power.irq_safe && dev->parent)
  1083. pm_runtime_put_sync(dev->parent);
  1084. }