runtime.c 35 KB

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