runtime.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/runtime.c - Helper functions for device runtime PM
  4. *
  5. * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  6. * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
  7. */
  8. #include <linux/sched/mm.h>
  9. #include <linux/ktime.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/export.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/pm_wakeirq.h>
  14. #include <linux/rculist.h>
  15. #include <trace/events/rpm.h>
  16. #include "../base.h"
  17. #include "power.h"
  18. typedef int (*pm_callback_t)(struct device *);
  19. static inline pm_callback_t get_callback_ptr(const void *start, size_t offset)
  20. {
  21. return *(pm_callback_t *)(start + offset);
  22. }
  23. static pm_callback_t __rpm_get_driver_callback(struct device *dev,
  24. size_t cb_offset)
  25. {
  26. if (dev->driver && dev->driver->pm)
  27. return get_callback_ptr(dev->driver->pm, cb_offset);
  28. return NULL;
  29. }
  30. static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
  31. {
  32. const struct dev_pm_ops *ops;
  33. pm_callback_t cb = NULL;
  34. if (dev->pm_domain)
  35. ops = &dev->pm_domain->ops;
  36. else if (dev->type && dev->type->pm)
  37. ops = dev->type->pm;
  38. else if (dev->class && dev->class->pm)
  39. ops = dev->class->pm;
  40. else if (dev->bus && dev->bus->pm)
  41. ops = dev->bus->pm;
  42. else
  43. ops = NULL;
  44. if (ops)
  45. cb = get_callback_ptr(ops, cb_offset);
  46. if (!cb)
  47. cb = __rpm_get_driver_callback(dev, cb_offset);
  48. return cb;
  49. }
  50. #define RPM_GET_CALLBACK(dev, callback) \
  51. __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
  52. static int rpm_resume(struct device *dev, int rpmflags);
  53. static int rpm_suspend(struct device *dev, int rpmflags);
  54. /**
  55. * update_pm_runtime_accounting - Update the time accounting of power states
  56. * @dev: Device to update the accounting for
  57. *
  58. * In order to be able to have time accounting of the various power states
  59. * (as used by programs such as PowerTOP to show the effectiveness of runtime
  60. * PM), we need to track the time spent in each state.
  61. * update_pm_runtime_accounting must be called each time before the
  62. * runtime_status field is updated, to account the time in the old state
  63. * correctly.
  64. */
  65. static void update_pm_runtime_accounting(struct device *dev)
  66. {
  67. u64 now, last, delta;
  68. if (dev->power.disable_depth > 0)
  69. return;
  70. last = dev->power.accounting_timestamp;
  71. now = ktime_get_mono_fast_ns();
  72. dev->power.accounting_timestamp = now;
  73. /*
  74. * Because ktime_get_mono_fast_ns() is not monotonic during
  75. * timekeeping updates, ensure that 'now' is after the last saved
  76. * timestamp.
  77. */
  78. if (now < last)
  79. return;
  80. delta = now - last;
  81. if (dev->power.runtime_status == RPM_SUSPENDED)
  82. dev->power.suspended_time += delta;
  83. else
  84. dev->power.active_time += delta;
  85. }
  86. static void __update_runtime_status(struct device *dev, enum rpm_status status)
  87. {
  88. update_pm_runtime_accounting(dev);
  89. trace_rpm_status(dev, status);
  90. dev->power.runtime_status = status;
  91. }
  92. static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
  93. {
  94. u64 time;
  95. unsigned long flags;
  96. spin_lock_irqsave(&dev->power.lock, flags);
  97. update_pm_runtime_accounting(dev);
  98. time = suspended ? dev->power.suspended_time : dev->power.active_time;
  99. spin_unlock_irqrestore(&dev->power.lock, flags);
  100. return time;
  101. }
  102. u64 pm_runtime_active_time(struct device *dev)
  103. {
  104. return rpm_get_accounted_time(dev, false);
  105. }
  106. u64 pm_runtime_suspended_time(struct device *dev)
  107. {
  108. return rpm_get_accounted_time(dev, true);
  109. }
  110. EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
  111. /**
  112. * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
  113. * @dev: Device to handle.
  114. */
  115. static void pm_runtime_deactivate_timer(struct device *dev)
  116. {
  117. if (dev->power.timer_expires > 0) {
  118. hrtimer_try_to_cancel(&dev->power.suspend_timer);
  119. dev->power.timer_expires = 0;
  120. }
  121. }
  122. /**
  123. * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
  124. * @dev: Device to handle.
  125. */
  126. static void pm_runtime_cancel_pending(struct device *dev)
  127. {
  128. pm_runtime_deactivate_timer(dev);
  129. /*
  130. * In case there's a request pending, make sure its work function will
  131. * return without doing anything.
  132. */
  133. dev->power.request = RPM_REQ_NONE;
  134. }
  135. /*
  136. * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
  137. * @dev: Device to handle.
  138. *
  139. * Compute the autosuspend-delay expiration time based on the device's
  140. * power.last_busy time. If the delay has already expired or is disabled
  141. * (negative) or the power.use_autosuspend flag isn't set, return 0.
  142. * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
  143. *
  144. * This function may be called either with or without dev->power.lock held.
  145. * Either way it can be racy, since power.last_busy may be updated at any time.
  146. */
  147. u64 pm_runtime_autosuspend_expiration(struct device *dev)
  148. {
  149. int autosuspend_delay;
  150. u64 expires;
  151. if (!dev->power.use_autosuspend)
  152. return 0;
  153. autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
  154. if (autosuspend_delay < 0)
  155. return 0;
  156. expires = READ_ONCE(dev->power.last_busy);
  157. expires += (u64)autosuspend_delay * NSEC_PER_MSEC;
  158. if (expires > ktime_get_mono_fast_ns())
  159. return expires; /* Expires in the future */
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
  163. static int dev_memalloc_noio(struct device *dev, void *data)
  164. {
  165. return dev->power.memalloc_noio;
  166. }
  167. /*
  168. * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
  169. * @dev: Device to handle.
  170. * @enable: True for setting the flag and False for clearing the flag.
  171. *
  172. * Set the flag for all devices in the path from the device to the
  173. * root device in the device tree if @enable is true, otherwise clear
  174. * the flag for devices in the path whose siblings don't set the flag.
  175. *
  176. * The function should only be called by block device, or network
  177. * device driver for solving the deadlock problem during runtime
  178. * resume/suspend:
  179. *
  180. * If memory allocation with GFP_KERNEL is called inside runtime
  181. * resume/suspend callback of any one of its ancestors(or the
  182. * block device itself), the deadlock may be triggered inside the
  183. * memory allocation since it might not complete until the block
  184. * device becomes active and the involved page I/O finishes. The
  185. * situation is pointed out first by Alan Stern. Network device
  186. * are involved in iSCSI kind of situation.
  187. *
  188. * The lock of dev_hotplug_mutex is held in the function for handling
  189. * hotplug race because pm_runtime_set_memalloc_noio() may be called
  190. * in async probe().
  191. *
  192. * The function should be called between device_add() and device_del()
  193. * on the affected device(block/network device).
  194. */
  195. void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
  196. {
  197. static DEFINE_MUTEX(dev_hotplug_mutex);
  198. mutex_lock(&dev_hotplug_mutex);
  199. for (;;) {
  200. bool enabled;
  201. /* hold power lock since bitfield is not SMP-safe. */
  202. spin_lock_irq(&dev->power.lock);
  203. enabled = dev->power.memalloc_noio;
  204. dev->power.memalloc_noio = enable;
  205. spin_unlock_irq(&dev->power.lock);
  206. /*
  207. * not need to enable ancestors any more if the device
  208. * has been enabled.
  209. */
  210. if (enabled && enable)
  211. break;
  212. dev = dev->parent;
  213. /*
  214. * clear flag of the parent device only if all the
  215. * children don't set the flag because ancestor's
  216. * flag was set by any one of the descendants.
  217. */
  218. if (!dev || (!enable &&
  219. device_for_each_child(dev, NULL, dev_memalloc_noio)))
  220. break;
  221. }
  222. mutex_unlock(&dev_hotplug_mutex);
  223. }
  224. EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
  225. /**
  226. * rpm_check_suspend_allowed - Test whether a device may be suspended.
  227. * @dev: Device to test.
  228. */
  229. static int rpm_check_suspend_allowed(struct device *dev)
  230. {
  231. int retval = 0;
  232. if (dev->power.runtime_error)
  233. retval = -EINVAL;
  234. else if (dev->power.disable_depth > 0)
  235. retval = -EACCES;
  236. else if (atomic_read(&dev->power.usage_count))
  237. retval = -EAGAIN;
  238. else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count))
  239. retval = -EBUSY;
  240. /* Pending resume requests take precedence over suspends. */
  241. else if ((dev->power.deferred_resume &&
  242. dev->power.runtime_status == RPM_SUSPENDING) ||
  243. (dev->power.request_pending && dev->power.request == RPM_REQ_RESUME))
  244. retval = -EAGAIN;
  245. else if (__dev_pm_qos_resume_latency(dev) == 0)
  246. retval = -EPERM;
  247. else if (dev->power.runtime_status == RPM_SUSPENDED)
  248. retval = 1;
  249. return retval;
  250. }
  251. static int rpm_get_suppliers(struct device *dev)
  252. {
  253. struct device_link *link;
  254. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
  255. device_links_read_lock_held()) {
  256. int retval;
  257. if (!device_link_test(link, DL_FLAG_PM_RUNTIME))
  258. continue;
  259. retval = pm_runtime_get_sync(link->supplier);
  260. /* Ignore suppliers with disabled runtime PM. */
  261. if (retval < 0 && retval != -EACCES) {
  262. pm_runtime_put_noidle(link->supplier);
  263. return retval;
  264. }
  265. refcount_inc(&link->rpm_active);
  266. }
  267. return 0;
  268. }
  269. /**
  270. * pm_runtime_release_supplier - Drop references to device link's supplier.
  271. * @link: Target device link.
  272. *
  273. * Drop all runtime PM references associated with @link to its supplier device.
  274. */
  275. void pm_runtime_release_supplier(struct device_link *link)
  276. {
  277. struct device *supplier = link->supplier;
  278. /*
  279. * The additional power.usage_count check is a safety net in case
  280. * the rpm_active refcount becomes saturated, in which case
  281. * refcount_dec_not_one() would return true forever, but it is not
  282. * strictly necessary.
  283. */
  284. while (refcount_dec_not_one(&link->rpm_active) &&
  285. atomic_read(&supplier->power.usage_count) > 0)
  286. pm_runtime_put_noidle(supplier);
  287. }
  288. static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
  289. {
  290. struct device_link *link;
  291. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
  292. device_links_read_lock_held()) {
  293. pm_runtime_release_supplier(link);
  294. if (try_to_suspend)
  295. pm_request_idle(link->supplier);
  296. }
  297. }
  298. static void rpm_put_suppliers(struct device *dev)
  299. {
  300. __rpm_put_suppliers(dev, true);
  301. }
  302. static void rpm_suspend_suppliers(struct device *dev)
  303. {
  304. struct device_link *link;
  305. int idx = device_links_read_lock();
  306. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
  307. device_links_read_lock_held())
  308. pm_request_idle(link->supplier);
  309. device_links_read_unlock(idx);
  310. }
  311. /**
  312. * __rpm_callback - Run a given runtime PM callback for a given device.
  313. * @cb: Runtime PM callback to run.
  314. * @dev: Device to run the callback for.
  315. */
  316. static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
  317. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  318. {
  319. int retval = 0, idx;
  320. bool use_links = dev->power.links_count > 0;
  321. if (dev->power.irq_safe) {
  322. spin_unlock(&dev->power.lock);
  323. } else {
  324. spin_unlock_irq(&dev->power.lock);
  325. /*
  326. * Resume suppliers if necessary.
  327. *
  328. * The device's runtime PM status cannot change until this
  329. * routine returns, so it is safe to read the status outside of
  330. * the lock.
  331. */
  332. if (use_links && dev->power.runtime_status == RPM_RESUMING) {
  333. idx = device_links_read_lock();
  334. retval = rpm_get_suppliers(dev);
  335. if (retval) {
  336. rpm_put_suppliers(dev);
  337. goto fail;
  338. }
  339. device_links_read_unlock(idx);
  340. }
  341. }
  342. if (cb)
  343. retval = cb(dev);
  344. if (dev->power.irq_safe) {
  345. spin_lock(&dev->power.lock);
  346. } else {
  347. /*
  348. * If the device is suspending and the callback has returned
  349. * success, drop the usage counters of the suppliers that have
  350. * been reference counted on its resume.
  351. *
  352. * Do that if resume fails too.
  353. */
  354. if (use_links &&
  355. ((dev->power.runtime_status == RPM_SUSPENDING && !retval) ||
  356. (dev->power.runtime_status == RPM_RESUMING && retval))) {
  357. idx = device_links_read_lock();
  358. __rpm_put_suppliers(dev, false);
  359. fail:
  360. device_links_read_unlock(idx);
  361. }
  362. spin_lock_irq(&dev->power.lock);
  363. }
  364. return retval;
  365. }
  366. /**
  367. * rpm_callback - Run a given runtime PM callback for a given device.
  368. * @cb: Runtime PM callback to run.
  369. * @dev: Device to run the callback for.
  370. */
  371. static int rpm_callback(int (*cb)(struct device *), struct device *dev)
  372. {
  373. int retval;
  374. if (dev->power.memalloc_noio) {
  375. unsigned int noio_flag;
  376. /*
  377. * Deadlock might be caused if memory allocation with
  378. * GFP_KERNEL happens inside runtime_suspend and
  379. * runtime_resume callbacks of one block device's
  380. * ancestor or the block device itself. Network
  381. * device might be thought as part of iSCSI block
  382. * device, so network device and its ancestor should
  383. * be marked as memalloc_noio too.
  384. */
  385. noio_flag = memalloc_noio_save();
  386. retval = __rpm_callback(cb, dev);
  387. memalloc_noio_restore(noio_flag);
  388. } else {
  389. retval = __rpm_callback(cb, dev);
  390. }
  391. /*
  392. * Since -EACCES means that runtime PM is disabled for the given device,
  393. * it should not be returned by runtime PM callbacks. If it is returned
  394. * nevertheless, assume it to be a transient error and convert it to
  395. * -EAGAIN.
  396. */
  397. if (retval == -EACCES)
  398. retval = -EAGAIN;
  399. if (retval != -EAGAIN && retval != -EBUSY)
  400. dev->power.runtime_error = retval;
  401. return retval;
  402. }
  403. /**
  404. * rpm_idle - Notify device bus type if the device can be suspended.
  405. * @dev: Device to notify the bus type about.
  406. * @rpmflags: Flag bits.
  407. *
  408. * Check if the device's runtime PM status allows it to be suspended. If
  409. * another idle notification has been started earlier, return immediately. If
  410. * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
  411. * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
  412. * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
  413. *
  414. * This function must be called under dev->power.lock with interrupts disabled.
  415. */
  416. static int rpm_idle(struct device *dev, int rpmflags)
  417. {
  418. int (*callback)(struct device *);
  419. int retval;
  420. trace_rpm_idle(dev, rpmflags);
  421. retval = rpm_check_suspend_allowed(dev);
  422. if (retval < 0)
  423. ; /* Conditions are wrong. */
  424. else if ((rpmflags & RPM_GET_PUT) && retval == 1)
  425. ; /* put() is allowed in RPM_SUSPENDED */
  426. /* Idle notifications are allowed only in the RPM_ACTIVE state. */
  427. else if (dev->power.runtime_status != RPM_ACTIVE)
  428. retval = -EAGAIN;
  429. /*
  430. * Any pending request other than an idle notification takes
  431. * precedence over us, except that the timer may be running.
  432. */
  433. else if (dev->power.request_pending &&
  434. dev->power.request > RPM_REQ_IDLE)
  435. retval = -EAGAIN;
  436. /* Act as though RPM_NOWAIT is always set. */
  437. else if (dev->power.idle_notification)
  438. retval = -EINPROGRESS;
  439. if (retval)
  440. goto out;
  441. /* Pending requests need to be canceled. */
  442. dev->power.request = RPM_REQ_NONE;
  443. callback = RPM_GET_CALLBACK(dev, runtime_idle);
  444. /* If no callback assume success. */
  445. if (!callback || dev->power.no_callbacks)
  446. goto out;
  447. /* Carry out an asynchronous or a synchronous idle notification. */
  448. if (rpmflags & RPM_ASYNC) {
  449. dev->power.request = RPM_REQ_IDLE;
  450. if (!dev->power.request_pending) {
  451. dev->power.request_pending = true;
  452. queue_work(pm_wq, &dev->power.work);
  453. }
  454. trace_rpm_return_int(dev, _THIS_IP_, 0);
  455. return 0;
  456. }
  457. dev->power.idle_notification = true;
  458. if (dev->power.irq_safe)
  459. spin_unlock(&dev->power.lock);
  460. else
  461. spin_unlock_irq(&dev->power.lock);
  462. retval = callback(dev);
  463. if (dev->power.irq_safe)
  464. spin_lock(&dev->power.lock);
  465. else
  466. spin_lock_irq(&dev->power.lock);
  467. dev->power.idle_notification = false;
  468. wake_up_all(&dev->power.wait_queue);
  469. out:
  470. trace_rpm_return_int(dev, _THIS_IP_, retval);
  471. return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
  472. }
  473. /**
  474. * rpm_suspend - Carry out runtime suspend of given device.
  475. * @dev: Device to suspend.
  476. * @rpmflags: Flag bits.
  477. *
  478. * Check if the device's runtime PM status allows it to be suspended.
  479. * Cancel a pending idle notification, autosuspend or suspend. If
  480. * another suspend has been started earlier, either return immediately
  481. * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
  482. * flags. If the RPM_ASYNC flag is set then queue a suspend request;
  483. * otherwise run the ->runtime_suspend() callback directly. When
  484. * ->runtime_suspend succeeded, if a deferred resume was requested while
  485. * the callback was running then carry it out, otherwise send an idle
  486. * notification for its parent (if the suspend succeeded and both
  487. * ignore_children of parent->power and irq_safe of dev->power are not set).
  488. * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
  489. * flag is set and the next autosuspend-delay expiration time is in the
  490. * future, schedule another autosuspend attempt.
  491. *
  492. * This function must be called under dev->power.lock with interrupts disabled.
  493. */
  494. static int rpm_suspend(struct device *dev, int rpmflags)
  495. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  496. {
  497. int (*callback)(struct device *);
  498. struct device *parent = NULL;
  499. int retval;
  500. trace_rpm_suspend(dev, rpmflags);
  501. repeat:
  502. retval = rpm_check_suspend_allowed(dev);
  503. if (retval < 0)
  504. goto out; /* Conditions are wrong. */
  505. /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
  506. if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
  507. retval = -EAGAIN;
  508. if (retval)
  509. goto out;
  510. /* If the autosuspend_delay time hasn't expired yet, reschedule. */
  511. if ((rpmflags & RPM_AUTO) && dev->power.runtime_status != RPM_SUSPENDING) {
  512. u64 expires = pm_runtime_autosuspend_expiration(dev);
  513. if (expires != 0) {
  514. /* Pending requests need to be canceled. */
  515. dev->power.request = RPM_REQ_NONE;
  516. /*
  517. * Optimization: If the timer is already running and is
  518. * set to expire at or before the autosuspend delay,
  519. * avoid the overhead of resetting it. Just let it
  520. * expire; pm_suspend_timer_fn() will take care of the
  521. * rest.
  522. */
  523. if (!(dev->power.timer_expires &&
  524. dev->power.timer_expires <= expires)) {
  525. /*
  526. * We add a slack of 25% to gather wakeups
  527. * without sacrificing the granularity.
  528. */
  529. u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
  530. (NSEC_PER_MSEC >> 2);
  531. dev->power.timer_expires = expires;
  532. hrtimer_start_range_ns(&dev->power.suspend_timer,
  533. ns_to_ktime(expires),
  534. slack,
  535. HRTIMER_MODE_ABS);
  536. }
  537. dev->power.timer_autosuspends = 1;
  538. goto out;
  539. }
  540. }
  541. /* Other scheduled or pending requests need to be canceled. */
  542. pm_runtime_cancel_pending(dev);
  543. if (dev->power.runtime_status == RPM_SUSPENDING) {
  544. DEFINE_WAIT(wait);
  545. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  546. retval = -EINPROGRESS;
  547. goto out;
  548. }
  549. if (dev->power.irq_safe) {
  550. spin_unlock(&dev->power.lock);
  551. cpu_relax();
  552. spin_lock(&dev->power.lock);
  553. goto repeat;
  554. }
  555. /* Wait for the other suspend running in parallel with us. */
  556. for (;;) {
  557. prepare_to_wait(&dev->power.wait_queue, &wait,
  558. TASK_UNINTERRUPTIBLE);
  559. if (dev->power.runtime_status != RPM_SUSPENDING)
  560. break;
  561. spin_unlock_irq(&dev->power.lock);
  562. schedule();
  563. spin_lock_irq(&dev->power.lock);
  564. }
  565. finish_wait(&dev->power.wait_queue, &wait);
  566. goto repeat;
  567. }
  568. if (dev->power.no_callbacks)
  569. goto no_callback; /* Assume success. */
  570. /* Carry out an asynchronous or a synchronous suspend. */
  571. if (rpmflags & RPM_ASYNC) {
  572. dev->power.request = (rpmflags & RPM_AUTO) ?
  573. RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
  574. if (!dev->power.request_pending) {
  575. dev->power.request_pending = true;
  576. queue_work(pm_wq, &dev->power.work);
  577. }
  578. goto out;
  579. }
  580. __update_runtime_status(dev, RPM_SUSPENDING);
  581. callback = RPM_GET_CALLBACK(dev, runtime_suspend);
  582. dev_pm_enable_wake_irq_check(dev, true);
  583. retval = rpm_callback(callback, dev);
  584. if (retval)
  585. goto fail;
  586. dev_pm_enable_wake_irq_complete(dev);
  587. no_callback:
  588. __update_runtime_status(dev, RPM_SUSPENDED);
  589. pm_runtime_deactivate_timer(dev);
  590. if (dev->parent) {
  591. parent = dev->parent;
  592. atomic_add_unless(&parent->power.child_count, -1, 0);
  593. }
  594. wake_up_all(&dev->power.wait_queue);
  595. if (dev->power.deferred_resume) {
  596. dev->power.deferred_resume = false;
  597. rpm_resume(dev, 0);
  598. retval = -EAGAIN;
  599. goto out;
  600. }
  601. if (dev->power.irq_safe)
  602. goto out;
  603. /* Maybe the parent is now able to suspend. */
  604. if (parent && !parent->power.ignore_children) {
  605. spin_unlock(&dev->power.lock);
  606. spin_lock(&parent->power.lock);
  607. rpm_idle(parent, RPM_ASYNC);
  608. spin_unlock(&parent->power.lock);
  609. spin_lock(&dev->power.lock);
  610. }
  611. /* Maybe the suppliers are now able to suspend. */
  612. if (dev->power.links_count > 0) {
  613. spin_unlock_irq(&dev->power.lock);
  614. rpm_suspend_suppliers(dev);
  615. spin_lock_irq(&dev->power.lock);
  616. }
  617. out:
  618. trace_rpm_return_int(dev, _THIS_IP_, retval);
  619. return retval;
  620. fail:
  621. dev_pm_disable_wake_irq_check(dev, true);
  622. __update_runtime_status(dev, RPM_ACTIVE);
  623. dev->power.deferred_resume = false;
  624. wake_up_all(&dev->power.wait_queue);
  625. /*
  626. * On transient errors, if the callback routine failed an autosuspend,
  627. * and if the last_busy time has been updated so that there is a new
  628. * autosuspend expiration time, automatically reschedule another
  629. * autosuspend.
  630. */
  631. if (!dev->power.runtime_error && (rpmflags & RPM_AUTO) &&
  632. pm_runtime_autosuspend_expiration(dev) != 0)
  633. goto repeat;
  634. pm_runtime_cancel_pending(dev);
  635. goto out;
  636. }
  637. /**
  638. * rpm_resume - Carry out runtime resume of given device.
  639. * @dev: Device to resume.
  640. * @rpmflags: Flag bits.
  641. *
  642. * Check if the device's runtime PM status allows it to be resumed. Cancel
  643. * any scheduled or pending requests. If another resume has been started
  644. * earlier, either return immediately or wait for it to finish, depending on the
  645. * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
  646. * parallel with this function, either tell the other process to resume after
  647. * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
  648. * flag is set then queue a resume request; otherwise run the
  649. * ->runtime_resume() callback directly. Queue an idle notification for the
  650. * device if the resume succeeded.
  651. *
  652. * This function must be called under dev->power.lock with interrupts disabled.
  653. */
  654. static int rpm_resume(struct device *dev, int rpmflags)
  655. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  656. {
  657. int (*callback)(struct device *);
  658. struct device *parent = NULL;
  659. int retval = 0;
  660. trace_rpm_resume(dev, rpmflags);
  661. repeat:
  662. if (dev->power.runtime_error) {
  663. retval = -EINVAL;
  664. } else if (dev->power.disable_depth > 0) {
  665. if (dev->power.runtime_status == RPM_ACTIVE &&
  666. dev->power.last_status == RPM_ACTIVE)
  667. retval = 1;
  668. else if (rpmflags & RPM_TRANSPARENT)
  669. goto out;
  670. else
  671. retval = -EACCES;
  672. }
  673. if (retval)
  674. goto out;
  675. /*
  676. * Other scheduled or pending requests need to be canceled. Small
  677. * optimization: If an autosuspend timer is running, leave it running
  678. * rather than cancelling it now only to restart it again in the near
  679. * future.
  680. */
  681. dev->power.request = RPM_REQ_NONE;
  682. if (!dev->power.timer_autosuspends)
  683. pm_runtime_deactivate_timer(dev);
  684. if (dev->power.runtime_status == RPM_ACTIVE) {
  685. retval = 1;
  686. goto out;
  687. }
  688. if (dev->power.runtime_status == RPM_RESUMING ||
  689. dev->power.runtime_status == RPM_SUSPENDING) {
  690. DEFINE_WAIT(wait);
  691. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  692. if (dev->power.runtime_status == RPM_SUSPENDING) {
  693. dev->power.deferred_resume = true;
  694. if (rpmflags & RPM_NOWAIT)
  695. retval = -EINPROGRESS;
  696. } else {
  697. retval = -EINPROGRESS;
  698. }
  699. goto out;
  700. }
  701. if (dev->power.irq_safe) {
  702. spin_unlock(&dev->power.lock);
  703. cpu_relax();
  704. spin_lock(&dev->power.lock);
  705. goto repeat;
  706. }
  707. /* Wait for the operation carried out in parallel with us. */
  708. for (;;) {
  709. prepare_to_wait(&dev->power.wait_queue, &wait,
  710. TASK_UNINTERRUPTIBLE);
  711. if (dev->power.runtime_status != RPM_RESUMING &&
  712. dev->power.runtime_status != RPM_SUSPENDING)
  713. break;
  714. spin_unlock_irq(&dev->power.lock);
  715. schedule();
  716. spin_lock_irq(&dev->power.lock);
  717. }
  718. finish_wait(&dev->power.wait_queue, &wait);
  719. goto repeat;
  720. }
  721. /*
  722. * See if we can skip waking up the parent. This is safe only if
  723. * power.no_callbacks is set, because otherwise we don't know whether
  724. * the resume will actually succeed.
  725. */
  726. if (dev->power.no_callbacks && !parent && dev->parent) {
  727. spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
  728. if (dev->parent->power.disable_depth > 0 ||
  729. dev->parent->power.ignore_children ||
  730. dev->parent->power.runtime_status == RPM_ACTIVE) {
  731. atomic_inc(&dev->parent->power.child_count);
  732. spin_unlock(&dev->parent->power.lock);
  733. retval = 1;
  734. goto no_callback; /* Assume success. */
  735. }
  736. spin_unlock(&dev->parent->power.lock);
  737. }
  738. /* Carry out an asynchronous or a synchronous resume. */
  739. if (rpmflags & RPM_ASYNC) {
  740. dev->power.request = RPM_REQ_RESUME;
  741. if (!dev->power.request_pending) {
  742. dev->power.request_pending = true;
  743. queue_work(pm_wq, &dev->power.work);
  744. }
  745. retval = 0;
  746. goto out;
  747. }
  748. if (!parent && dev->parent) {
  749. /*
  750. * Increment the parent's usage counter and resume it if
  751. * necessary. Not needed if dev is irq-safe; then the
  752. * parent is permanently resumed.
  753. */
  754. parent = dev->parent;
  755. if (dev->power.irq_safe)
  756. goto skip_parent;
  757. spin_unlock(&dev->power.lock);
  758. pm_runtime_get_noresume(parent);
  759. spin_lock(&parent->power.lock);
  760. /*
  761. * Resume the parent if it has runtime PM enabled and not been
  762. * set to ignore its children.
  763. */
  764. if (!parent->power.disable_depth &&
  765. !parent->power.ignore_children) {
  766. rpm_resume(parent, 0);
  767. if (parent->power.runtime_status != RPM_ACTIVE)
  768. retval = -EBUSY;
  769. }
  770. spin_unlock(&parent->power.lock);
  771. spin_lock(&dev->power.lock);
  772. if (retval)
  773. goto out;
  774. goto repeat;
  775. }
  776. skip_parent:
  777. if (dev->power.no_callbacks)
  778. goto no_callback; /* Assume success. */
  779. __update_runtime_status(dev, RPM_RESUMING);
  780. callback = RPM_GET_CALLBACK(dev, runtime_resume);
  781. dev_pm_disable_wake_irq_check(dev, false);
  782. retval = rpm_callback(callback, dev);
  783. if (retval) {
  784. __update_runtime_status(dev, RPM_SUSPENDED);
  785. pm_runtime_cancel_pending(dev);
  786. dev_pm_enable_wake_irq_check(dev, false);
  787. } else {
  788. no_callback:
  789. __update_runtime_status(dev, RPM_ACTIVE);
  790. pm_runtime_mark_last_busy(dev);
  791. if (parent)
  792. atomic_inc(&parent->power.child_count);
  793. }
  794. wake_up_all(&dev->power.wait_queue);
  795. if (retval >= 0)
  796. rpm_idle(dev, RPM_ASYNC);
  797. out:
  798. if (parent && !dev->power.irq_safe) {
  799. spin_unlock_irq(&dev->power.lock);
  800. pm_runtime_put(parent);
  801. spin_lock_irq(&dev->power.lock);
  802. }
  803. trace_rpm_return_int(dev, _THIS_IP_, retval);
  804. return retval;
  805. }
  806. /**
  807. * pm_runtime_work - Universal runtime PM work function.
  808. * @work: Work structure used for scheduling the execution of this function.
  809. *
  810. * Use @work to get the device object the work is to be done for, determine what
  811. * is to be done and execute the appropriate runtime PM function.
  812. */
  813. static void pm_runtime_work(struct work_struct *work)
  814. {
  815. struct device *dev = container_of(work, struct device, power.work);
  816. enum rpm_request req;
  817. spin_lock_irq(&dev->power.lock);
  818. if (!dev->power.request_pending)
  819. goto out;
  820. req = dev->power.request;
  821. dev->power.request = RPM_REQ_NONE;
  822. dev->power.request_pending = false;
  823. switch (req) {
  824. case RPM_REQ_NONE:
  825. break;
  826. case RPM_REQ_IDLE:
  827. rpm_idle(dev, RPM_NOWAIT);
  828. break;
  829. case RPM_REQ_SUSPEND:
  830. rpm_suspend(dev, RPM_NOWAIT);
  831. break;
  832. case RPM_REQ_AUTOSUSPEND:
  833. rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
  834. break;
  835. case RPM_REQ_RESUME:
  836. rpm_resume(dev, RPM_NOWAIT);
  837. break;
  838. }
  839. out:
  840. spin_unlock_irq(&dev->power.lock);
  841. }
  842. /**
  843. * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
  844. * @timer: hrtimer used by pm_schedule_suspend().
  845. *
  846. * Check if the time is right and queue a suspend request.
  847. */
  848. static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer)
  849. {
  850. struct device *dev = container_of(timer, struct device, power.suspend_timer);
  851. unsigned long flags;
  852. u64 expires;
  853. spin_lock_irqsave(&dev->power.lock, flags);
  854. expires = dev->power.timer_expires;
  855. /*
  856. * If 'expires' is after the current time, we've been called
  857. * too early.
  858. */
  859. if (expires > 0 && expires <= ktime_get_mono_fast_ns()) {
  860. dev->power.timer_expires = 0;
  861. rpm_suspend(dev, dev->power.timer_autosuspends ?
  862. (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
  863. }
  864. spin_unlock_irqrestore(&dev->power.lock, flags);
  865. return HRTIMER_NORESTART;
  866. }
  867. /**
  868. * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
  869. * @dev: Device to suspend.
  870. * @delay: Time to wait before submitting a suspend request, in milliseconds.
  871. */
  872. int pm_schedule_suspend(struct device *dev, unsigned int delay)
  873. {
  874. unsigned long flags;
  875. u64 expires;
  876. int retval;
  877. spin_lock_irqsave(&dev->power.lock, flags);
  878. if (!delay) {
  879. retval = rpm_suspend(dev, RPM_ASYNC);
  880. goto out;
  881. }
  882. retval = rpm_check_suspend_allowed(dev);
  883. if (retval)
  884. goto out;
  885. /* Other scheduled or pending requests need to be canceled. */
  886. pm_runtime_cancel_pending(dev);
  887. expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
  888. dev->power.timer_expires = expires;
  889. dev->power.timer_autosuspends = 0;
  890. hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
  891. out:
  892. spin_unlock_irqrestore(&dev->power.lock, flags);
  893. return retval;
  894. }
  895. EXPORT_SYMBOL_GPL(pm_schedule_suspend);
  896. static int rpm_drop_usage_count(struct device *dev)
  897. {
  898. int ret;
  899. ret = atomic_sub_return(1, &dev->power.usage_count);
  900. if (ret >= 0)
  901. return ret;
  902. /*
  903. * Because rpm_resume() does not check the usage counter, it will resume
  904. * the device even if the usage counter is 0 or negative, so it is
  905. * sufficient to increment the usage counter here to reverse the change
  906. * made above.
  907. */
  908. atomic_inc(&dev->power.usage_count);
  909. dev_warn(dev, "Runtime PM usage count underflow!\n");
  910. return -EINVAL;
  911. }
  912. /**
  913. * __pm_runtime_idle - Entry point for runtime idle operations.
  914. * @dev: Device to send idle notification for.
  915. * @rpmflags: Flag bits.
  916. *
  917. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  918. * return immediately if it is larger than zero (if it becomes negative, log a
  919. * warning, increment it, and return an error). Then carry out an idle
  920. * notification, either synchronous or asynchronous.
  921. *
  922. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  923. * or if pm_runtime_irq_safe() has been called.
  924. */
  925. int __pm_runtime_idle(struct device *dev, int rpmflags)
  926. {
  927. unsigned long flags;
  928. int retval;
  929. if (rpmflags & RPM_GET_PUT) {
  930. retval = rpm_drop_usage_count(dev);
  931. if (retval < 0) {
  932. return retval;
  933. } else if (retval > 0) {
  934. trace_rpm_usage(dev, rpmflags);
  935. return 0;
  936. }
  937. }
  938. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  939. spin_lock_irqsave(&dev->power.lock, flags);
  940. retval = rpm_idle(dev, rpmflags);
  941. spin_unlock_irqrestore(&dev->power.lock, flags);
  942. return retval;
  943. }
  944. EXPORT_SYMBOL_GPL(__pm_runtime_idle);
  945. /**
  946. * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
  947. * @dev: Device to suspend.
  948. * @rpmflags: Flag bits.
  949. *
  950. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  951. * return immediately if it is larger than zero (if it becomes negative, log a
  952. * warning, increment it, and return an error). Then carry out a suspend,
  953. * either synchronous or asynchronous.
  954. *
  955. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  956. * or if pm_runtime_irq_safe() has been called.
  957. */
  958. int __pm_runtime_suspend(struct device *dev, int rpmflags)
  959. {
  960. unsigned long flags;
  961. int retval;
  962. if (rpmflags & RPM_GET_PUT) {
  963. retval = rpm_drop_usage_count(dev);
  964. if (retval < 0) {
  965. return retval;
  966. } else if (retval > 0) {
  967. trace_rpm_usage(dev, rpmflags);
  968. return 0;
  969. }
  970. }
  971. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  972. spin_lock_irqsave(&dev->power.lock, flags);
  973. retval = rpm_suspend(dev, rpmflags);
  974. spin_unlock_irqrestore(&dev->power.lock, flags);
  975. return retval;
  976. }
  977. EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
  978. /**
  979. * __pm_runtime_resume - Entry point for runtime resume operations.
  980. * @dev: Device to resume.
  981. * @rpmflags: Flag bits.
  982. *
  983. * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
  984. * carry out a resume, either synchronous or asynchronous.
  985. *
  986. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  987. * or if pm_runtime_irq_safe() has been called.
  988. */
  989. int __pm_runtime_resume(struct device *dev, int rpmflags)
  990. {
  991. unsigned long flags;
  992. int retval;
  993. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
  994. dev->power.runtime_status != RPM_ACTIVE);
  995. if (rpmflags & RPM_GET_PUT)
  996. atomic_inc(&dev->power.usage_count);
  997. spin_lock_irqsave(&dev->power.lock, flags);
  998. retval = rpm_resume(dev, rpmflags);
  999. spin_unlock_irqrestore(&dev->power.lock, flags);
  1000. return retval;
  1001. }
  1002. EXPORT_SYMBOL_GPL(__pm_runtime_resume);
  1003. /**
  1004. * pm_runtime_get_conditional - Conditionally bump up device usage counter.
  1005. * @dev: Device to handle.
  1006. * @ign_usage_count: Whether or not to look at the current usage counter value.
  1007. *
  1008. * Return -EINVAL if runtime PM is disabled for @dev.
  1009. *
  1010. * Otherwise, if its runtime PM status is %RPM_ACTIVE and (1) @ign_usage_count
  1011. * is set, or (2) @dev is not ignoring children and its active child count is
  1012. * nonzero, or (3) the runtime PM usage counter of @dev is not zero, increment
  1013. * the usage counter of @dev and return 1.
  1014. *
  1015. * Otherwise, return 0 without changing the usage counter.
  1016. *
  1017. * If @ign_usage_count is %true, this function can be used to prevent suspending
  1018. * the device when its runtime PM status is %RPM_ACTIVE.
  1019. *
  1020. * If @ign_usage_count is %false, this function can be used to prevent
  1021. * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
  1022. * runtime PM usage counter is not zero.
  1023. *
  1024. * The caller is responsible for decrementing the runtime PM usage counter of
  1025. * @dev after this function has returned a positive value for it.
  1026. */
  1027. static int pm_runtime_get_conditional(struct device *dev, bool ign_usage_count)
  1028. {
  1029. unsigned long flags;
  1030. int retval;
  1031. spin_lock_irqsave(&dev->power.lock, flags);
  1032. if (dev->power.disable_depth > 0) {
  1033. retval = -EINVAL;
  1034. } else if (dev->power.runtime_status != RPM_ACTIVE) {
  1035. retval = 0;
  1036. } else if (ign_usage_count || (!dev->power.ignore_children &&
  1037. atomic_read(&dev->power.child_count) > 0)) {
  1038. retval = 1;
  1039. atomic_inc(&dev->power.usage_count);
  1040. } else {
  1041. retval = atomic_inc_not_zero(&dev->power.usage_count);
  1042. }
  1043. trace_rpm_usage(dev, 0);
  1044. spin_unlock_irqrestore(&dev->power.lock, flags);
  1045. return retval;
  1046. }
  1047. /**
  1048. * pm_runtime_get_if_active - Bump up runtime PM usage counter if the device is
  1049. * in active state
  1050. * @dev: Target device.
  1051. *
  1052. * Increment the runtime PM usage counter of @dev if its runtime PM status is
  1053. * %RPM_ACTIVE, in which case it returns 1. If the device is in a different
  1054. * state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the
  1055. * device, in which case also the usage_count will remain unmodified.
  1056. */
  1057. int pm_runtime_get_if_active(struct device *dev)
  1058. {
  1059. return pm_runtime_get_conditional(dev, true);
  1060. }
  1061. EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
  1062. /**
  1063. * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter.
  1064. * @dev: Target device.
  1065. *
  1066. * Increment the runtime PM usage counter of @dev if its runtime PM status is
  1067. * %RPM_ACTIVE and its runtime PM usage counter is greater than 0 or it is not
  1068. * ignoring children and its active child count is nonzero. 1 is returned in
  1069. * this case.
  1070. *
  1071. * If @dev is in a different state or it is not in use (that is, its usage
  1072. * counter is 0, or it is ignoring children, or its active child count is 0),
  1073. * 0 is returned.
  1074. *
  1075. * -EINVAL is returned if runtime PM is disabled for the device, in which case
  1076. * also the usage counter of @dev is not updated.
  1077. */
  1078. int pm_runtime_get_if_in_use(struct device *dev)
  1079. {
  1080. return pm_runtime_get_conditional(dev, false);
  1081. }
  1082. EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use);
  1083. /**
  1084. * __pm_runtime_set_status - Set runtime PM status of a device.
  1085. * @dev: Device to handle.
  1086. * @status: New runtime PM status of the device.
  1087. *
  1088. * If runtime PM of the device is disabled or its power.runtime_error field is
  1089. * different from zero, the status may be changed either to RPM_ACTIVE, or to
  1090. * RPM_SUSPENDED, as long as that reflects the actual state of the device.
  1091. * However, if the device has a parent and the parent is not active, and the
  1092. * parent's power.ignore_children flag is unset, the device's status cannot be
  1093. * set to RPM_ACTIVE, so -EBUSY is returned in that case.
  1094. *
  1095. * If successful, __pm_runtime_set_status() clears the power.runtime_error field
  1096. * and the device parent's counter of unsuspended children is modified to
  1097. * reflect the new status. If the new status is RPM_SUSPENDED, an idle
  1098. * notification request for the parent is submitted.
  1099. *
  1100. * If @dev has any suppliers (as reflected by device links to them), and @status
  1101. * is RPM_ACTIVE, they will be activated upfront and if the activation of one
  1102. * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
  1103. * of the @status value) and the suppliers will be deacticated on exit. The
  1104. * error returned by the failing supplier activation will be returned in that
  1105. * case.
  1106. */
  1107. int __pm_runtime_set_status(struct device *dev, unsigned int status)
  1108. {
  1109. struct device *parent = dev->parent;
  1110. bool notify_parent = false;
  1111. unsigned long flags;
  1112. int error = 0;
  1113. if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
  1114. return -EINVAL;
  1115. spin_lock_irqsave(&dev->power.lock, flags);
  1116. /*
  1117. * Prevent PM-runtime from being enabled for the device or return an
  1118. * error if it is enabled already and working.
  1119. */
  1120. if (dev->power.runtime_error || dev->power.disable_depth)
  1121. dev->power.disable_depth++;
  1122. else
  1123. error = -EAGAIN;
  1124. spin_unlock_irqrestore(&dev->power.lock, flags);
  1125. if (error)
  1126. return error;
  1127. /*
  1128. * If the new status is RPM_ACTIVE, the suppliers can be activated
  1129. * upfront regardless of the current status, because next time
  1130. * rpm_put_suppliers() runs, the rpm_active refcounts of the links
  1131. * involved will be dropped down to one anyway.
  1132. */
  1133. if (status == RPM_ACTIVE) {
  1134. int idx = device_links_read_lock();
  1135. error = rpm_get_suppliers(dev);
  1136. if (error)
  1137. status = RPM_SUSPENDED;
  1138. device_links_read_unlock(idx);
  1139. }
  1140. spin_lock_irqsave(&dev->power.lock, flags);
  1141. if (dev->power.runtime_status == status || !parent)
  1142. goto out_set;
  1143. if (status == RPM_SUSPENDED) {
  1144. atomic_add_unless(&parent->power.child_count, -1, 0);
  1145. notify_parent = !parent->power.ignore_children;
  1146. } else {
  1147. spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
  1148. /*
  1149. * It is invalid to put an active child under a parent that is
  1150. * not active, has runtime PM enabled and the
  1151. * 'power.ignore_children' flag unset.
  1152. */
  1153. if (!parent->power.disable_depth &&
  1154. !parent->power.ignore_children &&
  1155. parent->power.runtime_status != RPM_ACTIVE) {
  1156. dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
  1157. dev_name(dev),
  1158. dev_name(parent));
  1159. error = -EBUSY;
  1160. } else if (dev->power.runtime_status == RPM_SUSPENDED) {
  1161. atomic_inc(&parent->power.child_count);
  1162. }
  1163. spin_unlock(&parent->power.lock);
  1164. if (error) {
  1165. status = RPM_SUSPENDED;
  1166. goto out;
  1167. }
  1168. }
  1169. out_set:
  1170. __update_runtime_status(dev, status);
  1171. if (!error)
  1172. dev->power.runtime_error = 0;
  1173. out:
  1174. spin_unlock_irqrestore(&dev->power.lock, flags);
  1175. if (notify_parent)
  1176. pm_request_idle(parent);
  1177. if (status == RPM_SUSPENDED) {
  1178. int idx = device_links_read_lock();
  1179. rpm_put_suppliers(dev);
  1180. device_links_read_unlock(idx);
  1181. }
  1182. pm_runtime_enable(dev);
  1183. return error;
  1184. }
  1185. EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
  1186. /**
  1187. * __pm_runtime_barrier - Cancel pending requests and wait for completions.
  1188. * @dev: Device to handle.
  1189. *
  1190. * Flush all pending requests for the device from pm_wq and wait for all
  1191. * runtime PM operations involving the device in progress to complete.
  1192. *
  1193. * Should be called under dev->power.lock with interrupts disabled.
  1194. */
  1195. static void __pm_runtime_barrier(struct device *dev)
  1196. {
  1197. pm_runtime_deactivate_timer(dev);
  1198. if (dev->power.request_pending) {
  1199. dev->power.request = RPM_REQ_NONE;
  1200. spin_unlock_irq(&dev->power.lock);
  1201. cancel_work_sync(&dev->power.work);
  1202. spin_lock_irq(&dev->power.lock);
  1203. dev->power.request_pending = false;
  1204. }
  1205. if (dev->power.runtime_status == RPM_SUSPENDING ||
  1206. dev->power.runtime_status == RPM_RESUMING ||
  1207. dev->power.idle_notification) {
  1208. DEFINE_WAIT(wait);
  1209. /* Suspend, wake-up or idle notification in progress. */
  1210. for (;;) {
  1211. prepare_to_wait(&dev->power.wait_queue, &wait,
  1212. TASK_UNINTERRUPTIBLE);
  1213. if (dev->power.runtime_status != RPM_SUSPENDING
  1214. && dev->power.runtime_status != RPM_RESUMING
  1215. && !dev->power.idle_notification)
  1216. break;
  1217. spin_unlock_irq(&dev->power.lock);
  1218. schedule();
  1219. spin_lock_irq(&dev->power.lock);
  1220. }
  1221. finish_wait(&dev->power.wait_queue, &wait);
  1222. }
  1223. }
  1224. /**
  1225. * pm_runtime_barrier - Flush pending requests and wait for completions.
  1226. * @dev: Device to handle.
  1227. *
  1228. * Prevent the device from being suspended by incrementing its usage counter and
  1229. * if there's a pending resume request for the device, wake the device up.
  1230. * Next, make sure that all pending requests for the device have been flushed
  1231. * from pm_wq and wait for all runtime PM operations involving the device in
  1232. * progress to complete.
  1233. */
  1234. void pm_runtime_barrier(struct device *dev)
  1235. {
  1236. pm_runtime_get_noresume(dev);
  1237. spin_lock_irq(&dev->power.lock);
  1238. if (dev->power.request_pending
  1239. && dev->power.request == RPM_REQ_RESUME)
  1240. rpm_resume(dev, 0);
  1241. __pm_runtime_barrier(dev);
  1242. spin_unlock_irq(&dev->power.lock);
  1243. pm_runtime_put_noidle(dev);
  1244. }
  1245. EXPORT_SYMBOL_GPL(pm_runtime_barrier);
  1246. bool pm_runtime_block_if_disabled(struct device *dev)
  1247. {
  1248. bool ret;
  1249. spin_lock_irq(&dev->power.lock);
  1250. ret = !pm_runtime_enabled(dev);
  1251. if (ret && dev->power.last_status == RPM_INVALID)
  1252. dev->power.last_status = RPM_BLOCKED;
  1253. spin_unlock_irq(&dev->power.lock);
  1254. return ret;
  1255. }
  1256. void pm_runtime_unblock(struct device *dev)
  1257. {
  1258. spin_lock_irq(&dev->power.lock);
  1259. if (dev->power.last_status == RPM_BLOCKED)
  1260. dev->power.last_status = RPM_INVALID;
  1261. spin_unlock_irq(&dev->power.lock);
  1262. }
  1263. void __pm_runtime_disable(struct device *dev, bool check_resume)
  1264. {
  1265. spin_lock_irq(&dev->power.lock);
  1266. if (dev->power.disable_depth > 0) {
  1267. dev->power.disable_depth++;
  1268. goto out;
  1269. }
  1270. /*
  1271. * Wake up the device if there's a resume request pending, because that
  1272. * means there probably is some I/O to process and disabling runtime PM
  1273. * shouldn't prevent the device from processing the I/O.
  1274. */
  1275. if (check_resume && dev->power.request_pending &&
  1276. dev->power.request == RPM_REQ_RESUME) {
  1277. /*
  1278. * Prevent suspends and idle notifications from being carried
  1279. * out after we have woken up the device.
  1280. */
  1281. pm_runtime_get_noresume(dev);
  1282. rpm_resume(dev, 0);
  1283. pm_runtime_put_noidle(dev);
  1284. }
  1285. /* Update time accounting before disabling PM-runtime. */
  1286. update_pm_runtime_accounting(dev);
  1287. if (!dev->power.disable_depth++) {
  1288. __pm_runtime_barrier(dev);
  1289. dev->power.last_status = dev->power.runtime_status;
  1290. }
  1291. out:
  1292. spin_unlock_irq(&dev->power.lock);
  1293. }
  1294. EXPORT_SYMBOL_GPL(__pm_runtime_disable);
  1295. /**
  1296. * pm_runtime_enable - Enable runtime PM of a device.
  1297. * @dev: Device to handle.
  1298. */
  1299. void pm_runtime_enable(struct device *dev)
  1300. {
  1301. unsigned long flags;
  1302. spin_lock_irqsave(&dev->power.lock, flags);
  1303. if (!dev->power.disable_depth) {
  1304. dev_warn(dev, "Unbalanced %s!\n", __func__);
  1305. goto out;
  1306. }
  1307. if (--dev->power.disable_depth > 0)
  1308. goto out;
  1309. if (dev->power.last_status == RPM_BLOCKED) {
  1310. dev_warn(dev, "Attempt to enable runtime PM when it is blocked\n");
  1311. dump_stack();
  1312. }
  1313. dev->power.last_status = RPM_INVALID;
  1314. dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
  1315. if (dev->power.runtime_status == RPM_SUSPENDED &&
  1316. !dev->power.ignore_children &&
  1317. atomic_read(&dev->power.child_count) > 0)
  1318. dev_warn(dev, "Enabling runtime PM for inactive device with active children\n");
  1319. out:
  1320. spin_unlock_irqrestore(&dev->power.lock, flags);
  1321. }
  1322. EXPORT_SYMBOL_GPL(pm_runtime_enable);
  1323. static void pm_runtime_set_suspended_action(void *data)
  1324. {
  1325. pm_runtime_set_suspended(data);
  1326. }
  1327. /**
  1328. * devm_pm_runtime_set_active_enabled - set_active version of devm_pm_runtime_enable.
  1329. *
  1330. * @dev: Device to handle.
  1331. */
  1332. int devm_pm_runtime_set_active_enabled(struct device *dev)
  1333. {
  1334. int err;
  1335. err = pm_runtime_set_active(dev);
  1336. if (err)
  1337. return err;
  1338. err = devm_add_action_or_reset(dev, pm_runtime_set_suspended_action, dev);
  1339. if (err)
  1340. return err;
  1341. return devm_pm_runtime_enable(dev);
  1342. }
  1343. EXPORT_SYMBOL_GPL(devm_pm_runtime_set_active_enabled);
  1344. static void pm_runtime_disable_action(void *data)
  1345. {
  1346. pm_runtime_dont_use_autosuspend(data);
  1347. pm_runtime_disable(data);
  1348. }
  1349. /**
  1350. * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable.
  1351. *
  1352. * NOTE: this will also handle calling pm_runtime_dont_use_autosuspend() for
  1353. * you at driver exit time if needed.
  1354. *
  1355. * @dev: Device to handle.
  1356. */
  1357. int devm_pm_runtime_enable(struct device *dev)
  1358. {
  1359. pm_runtime_enable(dev);
  1360. return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev);
  1361. }
  1362. EXPORT_SYMBOL_GPL(devm_pm_runtime_enable);
  1363. static void pm_runtime_put_noidle_action(void *data)
  1364. {
  1365. pm_runtime_put_noidle(data);
  1366. }
  1367. /**
  1368. * devm_pm_runtime_get_noresume - devres-enabled version of pm_runtime_get_noresume.
  1369. *
  1370. * @dev: Device to handle.
  1371. */
  1372. int devm_pm_runtime_get_noresume(struct device *dev)
  1373. {
  1374. pm_runtime_get_noresume(dev);
  1375. return devm_add_action_or_reset(dev, pm_runtime_put_noidle_action, dev);
  1376. }
  1377. EXPORT_SYMBOL_GPL(devm_pm_runtime_get_noresume);
  1378. /**
  1379. * pm_runtime_forbid - Block runtime PM of a device.
  1380. * @dev: Device to handle.
  1381. *
  1382. * Resume @dev if already suspended and block runtime suspend of @dev in such
  1383. * a way that it can be unblocked via the /sys/devices/.../power/control
  1384. * interface, or otherwise by calling pm_runtime_allow().
  1385. *
  1386. * Calling this function many times in a row has the same effect as calling it
  1387. * once.
  1388. */
  1389. void pm_runtime_forbid(struct device *dev)
  1390. {
  1391. spin_lock_irq(&dev->power.lock);
  1392. if (!dev->power.runtime_auto)
  1393. goto out;
  1394. dev->power.runtime_auto = false;
  1395. atomic_inc(&dev->power.usage_count);
  1396. rpm_resume(dev, 0);
  1397. out:
  1398. spin_unlock_irq(&dev->power.lock);
  1399. }
  1400. EXPORT_SYMBOL_GPL(pm_runtime_forbid);
  1401. /**
  1402. * pm_runtime_allow - Unblock runtime PM of a device.
  1403. * @dev: Device to handle.
  1404. *
  1405. * Unblock runtime suspend of @dev after it has been blocked by
  1406. * pm_runtime_forbid() (for instance, if it has been blocked via the
  1407. * /sys/devices/.../power/control interface), check if @dev can be
  1408. * suspended and suspend it in that case.
  1409. *
  1410. * Calling this function many times in a row has the same effect as calling it
  1411. * once.
  1412. */
  1413. void pm_runtime_allow(struct device *dev)
  1414. {
  1415. int ret;
  1416. spin_lock_irq(&dev->power.lock);
  1417. if (dev->power.runtime_auto)
  1418. goto out;
  1419. dev->power.runtime_auto = true;
  1420. ret = rpm_drop_usage_count(dev);
  1421. if (ret == 0)
  1422. rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
  1423. else if (ret > 0)
  1424. trace_rpm_usage(dev, RPM_AUTO | RPM_ASYNC);
  1425. out:
  1426. spin_unlock_irq(&dev->power.lock);
  1427. }
  1428. EXPORT_SYMBOL_GPL(pm_runtime_allow);
  1429. /**
  1430. * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
  1431. * @dev: Device to handle.
  1432. *
  1433. * Set the power.no_callbacks flag, which tells the PM core that this
  1434. * device is power-managed through its parent and has no runtime PM
  1435. * callbacks of its own. The runtime sysfs attributes will be removed.
  1436. */
  1437. void pm_runtime_no_callbacks(struct device *dev)
  1438. {
  1439. spin_lock_irq(&dev->power.lock);
  1440. dev->power.no_callbacks = 1;
  1441. spin_unlock_irq(&dev->power.lock);
  1442. if (device_is_registered(dev))
  1443. rpm_sysfs_remove(dev);
  1444. }
  1445. EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
  1446. /**
  1447. * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
  1448. * @dev: Device to handle
  1449. *
  1450. * Set the power.irq_safe flag, which tells the PM core that the
  1451. * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
  1452. * always be invoked with the spinlock held and interrupts disabled. It also
  1453. * causes the parent's usage counter to be permanently incremented, preventing
  1454. * the parent from runtime suspending -- otherwise an irq-safe child might have
  1455. * to wait for a non-irq-safe parent.
  1456. */
  1457. void pm_runtime_irq_safe(struct device *dev)
  1458. {
  1459. if (dev->parent)
  1460. pm_runtime_get_sync(dev->parent);
  1461. spin_lock_irq(&dev->power.lock);
  1462. dev->power.irq_safe = 1;
  1463. spin_unlock_irq(&dev->power.lock);
  1464. }
  1465. EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
  1466. /**
  1467. * update_autosuspend - Handle a change to a device's autosuspend settings.
  1468. * @dev: Device to handle.
  1469. * @old_delay: The former autosuspend_delay value.
  1470. * @old_use: The former use_autosuspend value.
  1471. *
  1472. * Prevent runtime suspend if the new delay is negative and use_autosuspend is
  1473. * set; otherwise allow it. Send an idle notification if suspends are allowed.
  1474. *
  1475. * This function must be called under dev->power.lock with interrupts disabled.
  1476. */
  1477. static void update_autosuspend(struct device *dev, int old_delay, int old_use)
  1478. {
  1479. int delay = dev->power.autosuspend_delay;
  1480. /* Should runtime suspend be prevented now? */
  1481. if (dev->power.use_autosuspend && delay < 0) {
  1482. /* If it used to be allowed then prevent it. */
  1483. if (!old_use || old_delay >= 0) {
  1484. atomic_inc(&dev->power.usage_count);
  1485. rpm_resume(dev, 0);
  1486. } else {
  1487. trace_rpm_usage(dev, 0);
  1488. }
  1489. }
  1490. /* Runtime suspend should be allowed now. */
  1491. else {
  1492. /* If it used to be prevented then allow it. */
  1493. if (old_use && old_delay < 0)
  1494. atomic_dec(&dev->power.usage_count);
  1495. /* Maybe we can autosuspend now. */
  1496. rpm_idle(dev, RPM_AUTO);
  1497. }
  1498. }
  1499. /**
  1500. * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
  1501. * @dev: Device to handle.
  1502. * @delay: Value of the new delay in milliseconds.
  1503. *
  1504. * Set the device's power.autosuspend_delay value. If it changes to negative
  1505. * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
  1506. * changes the other way, allow runtime suspends.
  1507. */
  1508. void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
  1509. {
  1510. int old_delay, old_use;
  1511. spin_lock_irq(&dev->power.lock);
  1512. old_delay = dev->power.autosuspend_delay;
  1513. old_use = dev->power.use_autosuspend;
  1514. dev->power.autosuspend_delay = delay;
  1515. update_autosuspend(dev, old_delay, old_use);
  1516. spin_unlock_irq(&dev->power.lock);
  1517. }
  1518. EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
  1519. /**
  1520. * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
  1521. * @dev: Device to handle.
  1522. * @use: New value for use_autosuspend.
  1523. *
  1524. * Set the device's power.use_autosuspend flag, and allow or prevent runtime
  1525. * suspends as needed.
  1526. */
  1527. void __pm_runtime_use_autosuspend(struct device *dev, bool use)
  1528. {
  1529. int old_delay, old_use;
  1530. spin_lock_irq(&dev->power.lock);
  1531. old_delay = dev->power.autosuspend_delay;
  1532. old_use = dev->power.use_autosuspend;
  1533. dev->power.use_autosuspend = use;
  1534. update_autosuspend(dev, old_delay, old_use);
  1535. spin_unlock_irq(&dev->power.lock);
  1536. }
  1537. EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
  1538. /**
  1539. * pm_runtime_init - Initialize runtime PM fields in given device object.
  1540. * @dev: Device object to initialize.
  1541. */
  1542. void pm_runtime_init(struct device *dev)
  1543. {
  1544. dev->power.runtime_status = RPM_SUSPENDED;
  1545. dev->power.last_status = RPM_INVALID;
  1546. dev->power.idle_notification = false;
  1547. dev->power.disable_depth = 1;
  1548. atomic_set(&dev->power.usage_count, 0);
  1549. dev->power.runtime_error = 0;
  1550. atomic_set(&dev->power.child_count, 0);
  1551. pm_suspend_ignore_children(dev, false);
  1552. dev->power.runtime_auto = true;
  1553. dev->power.request_pending = false;
  1554. dev->power.request = RPM_REQ_NONE;
  1555. dev->power.deferred_resume = false;
  1556. dev->power.needs_force_resume = false;
  1557. INIT_WORK(&dev->power.work, pm_runtime_work);
  1558. dev->power.timer_expires = 0;
  1559. hrtimer_setup(&dev->power.suspend_timer, pm_suspend_timer_fn, CLOCK_MONOTONIC,
  1560. HRTIMER_MODE_ABS);
  1561. init_waitqueue_head(&dev->power.wait_queue);
  1562. }
  1563. /**
  1564. * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
  1565. * @dev: Device object to re-initialize.
  1566. */
  1567. void pm_runtime_reinit(struct device *dev)
  1568. {
  1569. if (pm_runtime_enabled(dev))
  1570. return;
  1571. if (dev->power.runtime_status == RPM_ACTIVE)
  1572. pm_runtime_set_suspended(dev);
  1573. if (dev->power.irq_safe) {
  1574. spin_lock_irq(&dev->power.lock);
  1575. dev->power.irq_safe = 0;
  1576. spin_unlock_irq(&dev->power.lock);
  1577. if (dev->parent)
  1578. pm_runtime_put(dev->parent);
  1579. }
  1580. /*
  1581. * Clear power.needs_force_resume in case it has been set by
  1582. * pm_runtime_force_suspend() invoked from a driver remove callback.
  1583. */
  1584. dev->power.needs_force_resume = false;
  1585. }
  1586. /**
  1587. * pm_runtime_remove - Prepare for removing a device from device hierarchy.
  1588. * @dev: Device object being removed from device hierarchy.
  1589. */
  1590. void pm_runtime_remove(struct device *dev)
  1591. {
  1592. __pm_runtime_disable(dev, false);
  1593. flush_work(&dev->power.work);
  1594. pm_runtime_reinit(dev);
  1595. }
  1596. /**
  1597. * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
  1598. * @dev: Consumer device.
  1599. */
  1600. void pm_runtime_get_suppliers(struct device *dev)
  1601. {
  1602. struct device_link *link;
  1603. int idx;
  1604. idx = device_links_read_lock();
  1605. dev_for_each_link_to_supplier(link, dev)
  1606. if (device_link_test(link, DL_FLAG_PM_RUNTIME)) {
  1607. link->supplier_preactivated = true;
  1608. pm_runtime_get_sync(link->supplier);
  1609. }
  1610. device_links_read_unlock(idx);
  1611. }
  1612. /**
  1613. * pm_runtime_put_suppliers - Drop references to supplier devices.
  1614. * @dev: Consumer device.
  1615. */
  1616. void pm_runtime_put_suppliers(struct device *dev)
  1617. {
  1618. struct device_link *link;
  1619. int idx;
  1620. idx = device_links_read_lock();
  1621. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
  1622. device_links_read_lock_held())
  1623. if (link->supplier_preactivated) {
  1624. link->supplier_preactivated = false;
  1625. pm_runtime_put(link->supplier);
  1626. }
  1627. device_links_read_unlock(idx);
  1628. }
  1629. void pm_runtime_new_link(struct device *dev)
  1630. {
  1631. spin_lock_irq(&dev->power.lock);
  1632. dev->power.links_count++;
  1633. spin_unlock_irq(&dev->power.lock);
  1634. }
  1635. static void pm_runtime_drop_link_count(struct device *dev)
  1636. {
  1637. spin_lock_irq(&dev->power.lock);
  1638. WARN_ON(dev->power.links_count == 0);
  1639. dev->power.links_count--;
  1640. spin_unlock_irq(&dev->power.lock);
  1641. }
  1642. /**
  1643. * pm_runtime_drop_link - Prepare for device link removal.
  1644. * @link: Device link going away.
  1645. *
  1646. * Drop the link count of the consumer end of @link and decrement the supplier
  1647. * device's runtime PM usage counter as many times as needed to drop all of the
  1648. * PM runtime reference to it from the consumer.
  1649. */
  1650. void pm_runtime_drop_link(struct device_link *link)
  1651. {
  1652. if (!device_link_test(link, DL_FLAG_PM_RUNTIME))
  1653. return;
  1654. pm_runtime_drop_link_count(link->consumer);
  1655. pm_runtime_release_supplier(link);
  1656. pm_request_idle(link->supplier);
  1657. }
  1658. static pm_callback_t get_callback(struct device *dev, size_t cb_offset)
  1659. {
  1660. /*
  1661. * Setting power.strict_midlayer means that the middle layer
  1662. * code does not want its runtime PM callbacks to be invoked via
  1663. * pm_runtime_force_suspend() and pm_runtime_force_resume(), so
  1664. * return a direct pointer to the driver callback in that case.
  1665. */
  1666. if (dev_pm_strict_midlayer_is_set(dev))
  1667. return __rpm_get_driver_callback(dev, cb_offset);
  1668. return __rpm_get_callback(dev, cb_offset);
  1669. }
  1670. #define GET_CALLBACK(dev, callback) \
  1671. get_callback(dev, offsetof(struct dev_pm_ops, callback))
  1672. /**
  1673. * pm_runtime_force_suspend - Force a device into suspend state if needed.
  1674. * @dev: Device to suspend.
  1675. *
  1676. * Disable runtime PM so we safely can check the device's runtime PM status and
  1677. * if it is active, invoke its ->runtime_suspend callback to suspend it and
  1678. * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's
  1679. * usage and children counters don't indicate that the device was in use before
  1680. * the system-wide transition under way, decrement its parent's children counter
  1681. * (if there is a parent). Keep runtime PM disabled to preserve the state
  1682. * unless we encounter errors.
  1683. *
  1684. * Typically this function may be invoked from a system suspend callback to make
  1685. * sure the device is put into low power state and it should only be used during
  1686. * system-wide PM transitions to sleep states. It assumes that the analogous
  1687. * pm_runtime_force_resume() will be used to resume the device.
  1688. */
  1689. int pm_runtime_force_suspend(struct device *dev)
  1690. {
  1691. int (*callback)(struct device *);
  1692. int ret;
  1693. pm_runtime_disable(dev);
  1694. if (pm_runtime_status_suspended(dev) || dev->power.needs_force_resume)
  1695. return 0;
  1696. callback = GET_CALLBACK(dev, runtime_suspend);
  1697. dev_pm_enable_wake_irq_check(dev, true);
  1698. ret = callback ? callback(dev) : 0;
  1699. if (ret)
  1700. goto err;
  1701. dev_pm_enable_wake_irq_complete(dev);
  1702. /*
  1703. * If the device can stay in suspend after the system-wide transition
  1704. * to the working state that will follow, drop the children counter of
  1705. * its parent and the usage counters of its suppliers. Otherwise, set
  1706. * power.needs_force_resume to let pm_runtime_force_resume() know that
  1707. * the device needs to be taken care of and to prevent this function
  1708. * from handling the device again in case the device is passed to it
  1709. * once more subsequently.
  1710. */
  1711. if (pm_runtime_need_not_resume(dev))
  1712. pm_runtime_set_suspended(dev);
  1713. else
  1714. dev->power.needs_force_resume = true;
  1715. return 0;
  1716. err:
  1717. dev_pm_disable_wake_irq_check(dev, true);
  1718. pm_runtime_enable(dev);
  1719. return ret;
  1720. }
  1721. EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
  1722. #ifdef CONFIG_PM_SLEEP
  1723. /**
  1724. * pm_runtime_force_resume - Force a device into resume state if needed.
  1725. * @dev: Device to resume.
  1726. *
  1727. * This function expects that either pm_runtime_force_suspend() has put the
  1728. * device into a low-power state prior to calling it, or the device had been
  1729. * runtime-suspended before the preceding system-wide suspend transition and it
  1730. * was left in suspend during that transition.
  1731. *
  1732. * The actions carried out by pm_runtime_force_suspend(), or by a runtime
  1733. * suspend in general, are reversed and the device is brought back into full
  1734. * power if it is expected to be used on system resume, which is the case when
  1735. * its needs_force_resume flag is set or when its smart_suspend flag is set and
  1736. * its runtime PM status is "active".
  1737. *
  1738. * In other cases, the resume is deferred to be managed via runtime PM.
  1739. *
  1740. * Typically, this function may be invoked from a system resume callback.
  1741. */
  1742. int pm_runtime_force_resume(struct device *dev)
  1743. {
  1744. int (*callback)(struct device *);
  1745. int ret = 0;
  1746. if (!dev->power.needs_force_resume && (!dev_pm_smart_suspend(dev) ||
  1747. pm_runtime_status_suspended(dev)))
  1748. goto out;
  1749. callback = GET_CALLBACK(dev, runtime_resume);
  1750. dev_pm_disable_wake_irq_check(dev, false);
  1751. ret = callback ? callback(dev) : 0;
  1752. if (ret) {
  1753. pm_runtime_set_suspended(dev);
  1754. dev_pm_enable_wake_irq_check(dev, false);
  1755. goto out;
  1756. }
  1757. pm_runtime_mark_last_busy(dev);
  1758. out:
  1759. /*
  1760. * The smart_suspend flag can be cleared here because it is not going
  1761. * to be necessary until the next system-wide suspend transition that
  1762. * will update it again.
  1763. */
  1764. dev->power.smart_suspend = false;
  1765. /*
  1766. * Also clear needs_force_resume to make this function skip devices that
  1767. * have been seen by it once.
  1768. */
  1769. dev->power.needs_force_resume = false;
  1770. pm_runtime_enable(dev);
  1771. return ret;
  1772. }
  1773. EXPORT_SYMBOL_GPL(pm_runtime_force_resume);
  1774. bool pm_runtime_need_not_resume(struct device *dev)
  1775. {
  1776. return atomic_read(&dev->power.usage_count) <= 1 &&
  1777. (atomic_read(&dev->power.child_count) == 0 ||
  1778. dev->power.ignore_children);
  1779. }
  1780. #endif /* CONFIG_PM_SLEEP */