runtime.c 36 KB

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