runtime.c 39 KB

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