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