runtime.c 42 KB

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