runtime.c 38 KB

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