runtime.c 34 KB

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