runtime.c 34 KB

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