runtime.c 34 KB

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