runtime.c 34 KB

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