runtime.c 35 KB

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