runtime.c 47 KB

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