runtime.c 36 KB

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