runtime.c 35 KB

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