runtime.c 34 KB

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