runtime.c 33 KB

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