runtime.c 35 KB

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