runtime.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 == 1 && dev->power.is_suspended
  432. && dev->power.runtime_status == RPM_ACTIVE)
  433. retval = 1;
  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. }