runtime.c 35 KB

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