runtime.c 34 KB

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