runtime.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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/mm.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. if (rpmflags & RPM_GET_PUT) {
  815. if (!atomic_dec_and_test(&dev->power.usage_count))
  816. return 0;
  817. }
  818. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  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. if (rpmflags & RPM_GET_PUT) {
  842. if (!atomic_dec_and_test(&dev->power.usage_count))
  843. return 0;
  844. }
  845. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  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. dev->power.runtime_status != RPM_ACTIVE);
  869. if (rpmflags & RPM_GET_PUT)
  870. atomic_inc(&dev->power.usage_count);
  871. spin_lock_irqsave(&dev->power.lock, flags);
  872. retval = rpm_resume(dev, rpmflags);
  873. spin_unlock_irqrestore(&dev->power.lock, flags);
  874. return retval;
  875. }
  876. EXPORT_SYMBOL_GPL(__pm_runtime_resume);
  877. /**
  878. * pm_runtime_get_if_in_use - Conditionally bump up the device's usage counter.
  879. * @dev: Device to handle.
  880. *
  881. * Return -EINVAL if runtime PM is disabled for the device.
  882. *
  883. * If that's not the case and if the device's runtime PM status is RPM_ACTIVE
  884. * and the runtime PM usage counter is nonzero, increment the counter and
  885. * return 1. Otherwise return 0 without changing the counter.
  886. */
  887. int pm_runtime_get_if_in_use(struct device *dev)
  888. {
  889. unsigned long flags;
  890. int retval;
  891. spin_lock_irqsave(&dev->power.lock, flags);
  892. retval = dev->power.disable_depth > 0 ? -EINVAL :
  893. dev->power.runtime_status == RPM_ACTIVE
  894. && atomic_inc_not_zero(&dev->power.usage_count);
  895. spin_unlock_irqrestore(&dev->power.lock, flags);
  896. return retval;
  897. }
  898. EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use);
  899. /**
  900. * __pm_runtime_set_status - Set runtime PM status of a device.
  901. * @dev: Device to handle.
  902. * @status: New runtime PM status of the device.
  903. *
  904. * If runtime PM of the device is disabled or its power.runtime_error field is
  905. * different from zero, the status may be changed either to RPM_ACTIVE, or to
  906. * RPM_SUSPENDED, as long as that reflects the actual state of the device.
  907. * However, if the device has a parent and the parent is not active, and the
  908. * parent's power.ignore_children flag is unset, the device's status cannot be
  909. * set to RPM_ACTIVE, so -EBUSY is returned in that case.
  910. *
  911. * If successful, __pm_runtime_set_status() clears the power.runtime_error field
  912. * and the device parent's counter of unsuspended children is modified to
  913. * reflect the new status. If the new status is RPM_SUSPENDED, an idle
  914. * notification request for the parent is submitted.
  915. */
  916. int __pm_runtime_set_status(struct device *dev, unsigned int status)
  917. {
  918. struct device *parent = dev->parent;
  919. unsigned long flags;
  920. bool notify_parent = false;
  921. int error = 0;
  922. if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
  923. return -EINVAL;
  924. spin_lock_irqsave(&dev->power.lock, flags);
  925. if (!dev->power.runtime_error && !dev->power.disable_depth) {
  926. error = -EAGAIN;
  927. goto out;
  928. }
  929. if (dev->power.runtime_status == status)
  930. goto out_set;
  931. if (status == RPM_SUSPENDED) {
  932. /*
  933. * It is invalid to suspend a device with an active child,
  934. * unless it has been set to ignore its children.
  935. */
  936. if (!dev->power.ignore_children &&
  937. atomic_read(&dev->power.child_count)) {
  938. dev_err(dev, "runtime PM trying to suspend device but active child\n");
  939. error = -EBUSY;
  940. goto out;
  941. }
  942. if (parent) {
  943. atomic_add_unless(&parent->power.child_count, -1, 0);
  944. notify_parent = !parent->power.ignore_children;
  945. }
  946. goto out_set;
  947. }
  948. if (parent) {
  949. spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
  950. /*
  951. * It is invalid to put an active child under a parent that is
  952. * not active, has runtime PM enabled and the
  953. * 'power.ignore_children' flag unset.
  954. */
  955. if (!parent->power.disable_depth
  956. && !parent->power.ignore_children
  957. && parent->power.runtime_status != RPM_ACTIVE) {
  958. dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
  959. dev_name(dev),
  960. dev_name(parent));
  961. error = -EBUSY;
  962. } else if (dev->power.runtime_status == RPM_SUSPENDED) {
  963. atomic_inc(&parent->power.child_count);
  964. }
  965. spin_unlock(&parent->power.lock);
  966. if (error)
  967. goto out;
  968. }
  969. out_set:
  970. __update_runtime_status(dev, status);
  971. dev->power.runtime_error = 0;
  972. out:
  973. spin_unlock_irqrestore(&dev->power.lock, flags);
  974. if (notify_parent)
  975. pm_request_idle(parent);
  976. return error;
  977. }
  978. EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
  979. /**
  980. * __pm_runtime_barrier - Cancel pending requests and wait for completions.
  981. * @dev: Device to handle.
  982. *
  983. * Flush all pending requests for the device from pm_wq and wait for all
  984. * runtime PM operations involving the device in progress to complete.
  985. *
  986. * Should be called under dev->power.lock with interrupts disabled.
  987. */
  988. static void __pm_runtime_barrier(struct device *dev)
  989. {
  990. pm_runtime_deactivate_timer(dev);
  991. if (dev->power.request_pending) {
  992. dev->power.request = RPM_REQ_NONE;
  993. spin_unlock_irq(&dev->power.lock);
  994. cancel_work_sync(&dev->power.work);
  995. spin_lock_irq(&dev->power.lock);
  996. dev->power.request_pending = false;
  997. }
  998. if (dev->power.runtime_status == RPM_SUSPENDING
  999. || dev->power.runtime_status == RPM_RESUMING
  1000. || dev->power.idle_notification) {
  1001. DEFINE_WAIT(wait);
  1002. /* Suspend, wake-up or idle notification in progress. */
  1003. for (;;) {
  1004. prepare_to_wait(&dev->power.wait_queue, &wait,
  1005. TASK_UNINTERRUPTIBLE);
  1006. if (dev->power.runtime_status != RPM_SUSPENDING
  1007. && dev->power.runtime_status != RPM_RESUMING
  1008. && !dev->power.idle_notification)
  1009. break;
  1010. spin_unlock_irq(&dev->power.lock);
  1011. schedule();
  1012. spin_lock_irq(&dev->power.lock);
  1013. }
  1014. finish_wait(&dev->power.wait_queue, &wait);
  1015. }
  1016. }
  1017. /**
  1018. * pm_runtime_barrier - Flush pending requests and wait for completions.
  1019. * @dev: Device to handle.
  1020. *
  1021. * Prevent the device from being suspended by incrementing its usage counter and
  1022. * if there's a pending resume request for the device, wake the device up.
  1023. * Next, make sure that all pending requests for the device have been flushed
  1024. * from pm_wq and wait for all runtime PM operations involving the device in
  1025. * progress to complete.
  1026. *
  1027. * Return value:
  1028. * 1, if there was a resume request pending and the device had to be woken up,
  1029. * 0, otherwise
  1030. */
  1031. int pm_runtime_barrier(struct device *dev)
  1032. {
  1033. int retval = 0;
  1034. pm_runtime_get_noresume(dev);
  1035. spin_lock_irq(&dev->power.lock);
  1036. if (dev->power.request_pending
  1037. && dev->power.request == RPM_REQ_RESUME) {
  1038. rpm_resume(dev, 0);
  1039. retval = 1;
  1040. }
  1041. __pm_runtime_barrier(dev);
  1042. spin_unlock_irq(&dev->power.lock);
  1043. pm_runtime_put_noidle(dev);
  1044. return retval;
  1045. }
  1046. EXPORT_SYMBOL_GPL(pm_runtime_barrier);
  1047. /**
  1048. * __pm_runtime_disable - Disable runtime PM of a device.
  1049. * @dev: Device to handle.
  1050. * @check_resume: If set, check if there's a resume request for the device.
  1051. *
  1052. * Increment power.disable_depth for the device and if it was zero previously,
  1053. * cancel all pending runtime PM requests for the device and wait for all
  1054. * operations in progress to complete. The device can be either active or
  1055. * suspended after its runtime PM has been disabled.
  1056. *
  1057. * If @check_resume is set and there's a resume request pending when
  1058. * __pm_runtime_disable() is called and power.disable_depth is zero, the
  1059. * function will wake up the device before disabling its runtime PM.
  1060. */
  1061. void __pm_runtime_disable(struct device *dev, bool check_resume)
  1062. {
  1063. spin_lock_irq(&dev->power.lock);
  1064. if (dev->power.disable_depth > 0) {
  1065. dev->power.disable_depth++;
  1066. goto out;
  1067. }
  1068. /*
  1069. * Wake up the device if there's a resume request pending, because that
  1070. * means there probably is some I/O to process and disabling runtime PM
  1071. * shouldn't prevent the device from processing the I/O.
  1072. */
  1073. if (check_resume && dev->power.request_pending
  1074. && dev->power.request == RPM_REQ_RESUME) {
  1075. /*
  1076. * Prevent suspends and idle notifications from being carried
  1077. * out after we have woken up the device.
  1078. */
  1079. pm_runtime_get_noresume(dev);
  1080. rpm_resume(dev, 0);
  1081. pm_runtime_put_noidle(dev);
  1082. }
  1083. if (!dev->power.disable_depth++)
  1084. __pm_runtime_barrier(dev);
  1085. out:
  1086. spin_unlock_irq(&dev->power.lock);
  1087. }
  1088. EXPORT_SYMBOL_GPL(__pm_runtime_disable);
  1089. /**
  1090. * pm_runtime_enable - Enable runtime PM of a device.
  1091. * @dev: Device to handle.
  1092. */
  1093. void pm_runtime_enable(struct device *dev)
  1094. {
  1095. unsigned long flags;
  1096. spin_lock_irqsave(&dev->power.lock, flags);
  1097. if (dev->power.disable_depth > 0)
  1098. dev->power.disable_depth--;
  1099. else
  1100. dev_warn(dev, "Unbalanced %s!\n", __func__);
  1101. spin_unlock_irqrestore(&dev->power.lock, flags);
  1102. }
  1103. EXPORT_SYMBOL_GPL(pm_runtime_enable);
  1104. /**
  1105. * pm_runtime_forbid - Block runtime PM of a device.
  1106. * @dev: Device to handle.
  1107. *
  1108. * Increase the device's usage count and clear its power.runtime_auto flag,
  1109. * so that it cannot be suspended at run time until pm_runtime_allow() is called
  1110. * for it.
  1111. */
  1112. void pm_runtime_forbid(struct device *dev)
  1113. {
  1114. spin_lock_irq(&dev->power.lock);
  1115. if (!dev->power.runtime_auto)
  1116. goto out;
  1117. dev->power.runtime_auto = false;
  1118. atomic_inc(&dev->power.usage_count);
  1119. rpm_resume(dev, 0);
  1120. out:
  1121. spin_unlock_irq(&dev->power.lock);
  1122. }
  1123. EXPORT_SYMBOL_GPL(pm_runtime_forbid);
  1124. /**
  1125. * pm_runtime_allow - Unblock runtime PM of a device.
  1126. * @dev: Device to handle.
  1127. *
  1128. * Decrease the device's usage count and set its power.runtime_auto flag.
  1129. */
  1130. void pm_runtime_allow(struct device *dev)
  1131. {
  1132. spin_lock_irq(&dev->power.lock);
  1133. if (dev->power.runtime_auto)
  1134. goto out;
  1135. dev->power.runtime_auto = true;
  1136. if (atomic_dec_and_test(&dev->power.usage_count))
  1137. rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
  1138. out:
  1139. spin_unlock_irq(&dev->power.lock);
  1140. }
  1141. EXPORT_SYMBOL_GPL(pm_runtime_allow);
  1142. /**
  1143. * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
  1144. * @dev: Device to handle.
  1145. *
  1146. * Set the power.no_callbacks flag, which tells the PM core that this
  1147. * device is power-managed through its parent and has no runtime PM
  1148. * callbacks of its own. The runtime sysfs attributes will be removed.
  1149. */
  1150. void pm_runtime_no_callbacks(struct device *dev)
  1151. {
  1152. spin_lock_irq(&dev->power.lock);
  1153. dev->power.no_callbacks = 1;
  1154. spin_unlock_irq(&dev->power.lock);
  1155. if (device_is_registered(dev))
  1156. rpm_sysfs_remove(dev);
  1157. }
  1158. EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
  1159. /**
  1160. * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
  1161. * @dev: Device to handle
  1162. *
  1163. * Set the power.irq_safe flag, which tells the PM core that the
  1164. * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
  1165. * always be invoked with the spinlock held and interrupts disabled. It also
  1166. * causes the parent's usage counter to be permanently incremented, preventing
  1167. * the parent from runtime suspending -- otherwise an irq-safe child might have
  1168. * to wait for a non-irq-safe parent.
  1169. */
  1170. void pm_runtime_irq_safe(struct device *dev)
  1171. {
  1172. if (dev->parent)
  1173. pm_runtime_get_sync(dev->parent);
  1174. spin_lock_irq(&dev->power.lock);
  1175. dev->power.irq_safe = 1;
  1176. spin_unlock_irq(&dev->power.lock);
  1177. }
  1178. EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
  1179. /**
  1180. * update_autosuspend - Handle a change to a device's autosuspend settings.
  1181. * @dev: Device to handle.
  1182. * @old_delay: The former autosuspend_delay value.
  1183. * @old_use: The former use_autosuspend value.
  1184. *
  1185. * Prevent runtime suspend if the new delay is negative and use_autosuspend is
  1186. * set; otherwise allow it. Send an idle notification if suspends are allowed.
  1187. *
  1188. * This function must be called under dev->power.lock with interrupts disabled.
  1189. */
  1190. static void update_autosuspend(struct device *dev, int old_delay, int old_use)
  1191. {
  1192. int delay = dev->power.autosuspend_delay;
  1193. /* Should runtime suspend be prevented now? */
  1194. if (dev->power.use_autosuspend && delay < 0) {
  1195. /* If it used to be allowed then prevent it. */
  1196. if (!old_use || old_delay >= 0) {
  1197. atomic_inc(&dev->power.usage_count);
  1198. rpm_resume(dev, 0);
  1199. }
  1200. }
  1201. /* Runtime suspend should be allowed now. */
  1202. else {
  1203. /* If it used to be prevented then allow it. */
  1204. if (old_use && old_delay < 0)
  1205. atomic_dec(&dev->power.usage_count);
  1206. /* Maybe we can autosuspend now. */
  1207. rpm_idle(dev, RPM_AUTO);
  1208. }
  1209. }
  1210. /**
  1211. * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
  1212. * @dev: Device to handle.
  1213. * @delay: Value of the new delay in milliseconds.
  1214. *
  1215. * Set the device's power.autosuspend_delay value. If it changes to negative
  1216. * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
  1217. * changes the other way, allow runtime suspends.
  1218. */
  1219. void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
  1220. {
  1221. int old_delay, old_use;
  1222. spin_lock_irq(&dev->power.lock);
  1223. old_delay = dev->power.autosuspend_delay;
  1224. old_use = dev->power.use_autosuspend;
  1225. dev->power.autosuspend_delay = delay;
  1226. update_autosuspend(dev, old_delay, old_use);
  1227. spin_unlock_irq(&dev->power.lock);
  1228. }
  1229. EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
  1230. /**
  1231. * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
  1232. * @dev: Device to handle.
  1233. * @use: New value for use_autosuspend.
  1234. *
  1235. * Set the device's power.use_autosuspend flag, and allow or prevent runtime
  1236. * suspends as needed.
  1237. */
  1238. void __pm_runtime_use_autosuspend(struct device *dev, bool use)
  1239. {
  1240. int old_delay, old_use;
  1241. spin_lock_irq(&dev->power.lock);
  1242. old_delay = dev->power.autosuspend_delay;
  1243. old_use = dev->power.use_autosuspend;
  1244. dev->power.use_autosuspend = use;
  1245. update_autosuspend(dev, old_delay, old_use);
  1246. spin_unlock_irq(&dev->power.lock);
  1247. }
  1248. EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
  1249. /**
  1250. * pm_runtime_init - Initialize runtime PM fields in given device object.
  1251. * @dev: Device object to initialize.
  1252. */
  1253. void pm_runtime_init(struct device *dev)
  1254. {
  1255. dev->power.runtime_status = RPM_SUSPENDED;
  1256. dev->power.idle_notification = false;
  1257. dev->power.disable_depth = 1;
  1258. atomic_set(&dev->power.usage_count, 0);
  1259. dev->power.runtime_error = 0;
  1260. atomic_set(&dev->power.child_count, 0);
  1261. pm_suspend_ignore_children(dev, false);
  1262. dev->power.runtime_auto = true;
  1263. dev->power.request_pending = false;
  1264. dev->power.request = RPM_REQ_NONE;
  1265. dev->power.deferred_resume = false;
  1266. dev->power.accounting_timestamp = jiffies;
  1267. INIT_WORK(&dev->power.work, pm_runtime_work);
  1268. dev->power.timer_expires = 0;
  1269. setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
  1270. (unsigned long)dev);
  1271. init_waitqueue_head(&dev->power.wait_queue);
  1272. }
  1273. /**
  1274. * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
  1275. * @dev: Device object to re-initialize.
  1276. */
  1277. void pm_runtime_reinit(struct device *dev)
  1278. {
  1279. if (!pm_runtime_enabled(dev)) {
  1280. if (dev->power.runtime_status == RPM_ACTIVE)
  1281. pm_runtime_set_suspended(dev);
  1282. if (dev->power.irq_safe) {
  1283. spin_lock_irq(&dev->power.lock);
  1284. dev->power.irq_safe = 0;
  1285. spin_unlock_irq(&dev->power.lock);
  1286. if (dev->parent)
  1287. pm_runtime_put(dev->parent);
  1288. }
  1289. }
  1290. }
  1291. /**
  1292. * pm_runtime_remove - Prepare for removing a device from device hierarchy.
  1293. * @dev: Device object being removed from device hierarchy.
  1294. */
  1295. void pm_runtime_remove(struct device *dev)
  1296. {
  1297. __pm_runtime_disable(dev, false);
  1298. pm_runtime_reinit(dev);
  1299. }
  1300. /**
  1301. * pm_runtime_clean_up_links - Prepare links to consumers for driver removal.
  1302. * @dev: Device whose driver is going to be removed.
  1303. *
  1304. * Check links from this device to any consumers and if any of them have active
  1305. * runtime PM references to the device, drop the usage counter of the device
  1306. * (once per link).
  1307. *
  1308. * Links with the DL_FLAG_STATELESS flag set are ignored.
  1309. *
  1310. * Since the device is guaranteed to be runtime-active at the point this is
  1311. * called, nothing else needs to be done here.
  1312. *
  1313. * Moreover, this is called after device_links_busy() has returned 'false', so
  1314. * the status of each link is guaranteed to be DL_STATE_SUPPLIER_UNBIND and
  1315. * therefore rpm_active can't be manipulated concurrently.
  1316. */
  1317. void pm_runtime_clean_up_links(struct device *dev)
  1318. {
  1319. struct device_link *link;
  1320. int idx;
  1321. idx = device_links_read_lock();
  1322. list_for_each_entry_rcu(link, &dev->links.consumers, s_node) {
  1323. if (link->flags & DL_FLAG_STATELESS)
  1324. continue;
  1325. if (link->rpm_active) {
  1326. pm_runtime_put_noidle(dev);
  1327. link->rpm_active = false;
  1328. }
  1329. }
  1330. device_links_read_unlock(idx);
  1331. }
  1332. /**
  1333. * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
  1334. * @dev: Consumer device.
  1335. */
  1336. void pm_runtime_get_suppliers(struct device *dev)
  1337. {
  1338. struct device_link *link;
  1339. int idx;
  1340. idx = device_links_read_lock();
  1341. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
  1342. if (link->flags & DL_FLAG_PM_RUNTIME)
  1343. pm_runtime_get_sync(link->supplier);
  1344. device_links_read_unlock(idx);
  1345. }
  1346. /**
  1347. * pm_runtime_put_suppliers - Drop references to supplier devices.
  1348. * @dev: Consumer device.
  1349. */
  1350. void pm_runtime_put_suppliers(struct device *dev)
  1351. {
  1352. struct device_link *link;
  1353. int idx;
  1354. idx = device_links_read_lock();
  1355. list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
  1356. if (link->flags & DL_FLAG_PM_RUNTIME)
  1357. pm_runtime_put(link->supplier);
  1358. device_links_read_unlock(idx);
  1359. }
  1360. void pm_runtime_new_link(struct device *dev)
  1361. {
  1362. spin_lock_irq(&dev->power.lock);
  1363. dev->power.links_count++;
  1364. spin_unlock_irq(&dev->power.lock);
  1365. }
  1366. void pm_runtime_drop_link(struct device *dev)
  1367. {
  1368. spin_lock_irq(&dev->power.lock);
  1369. WARN_ON(dev->power.links_count == 0);
  1370. dev->power.links_count--;
  1371. spin_unlock_irq(&dev->power.lock);
  1372. }
  1373. /**
  1374. * pm_runtime_force_suspend - Force a device into suspend state if needed.
  1375. * @dev: Device to suspend.
  1376. *
  1377. * Disable runtime PM so we safely can check the device's runtime PM status and
  1378. * if it is active, invoke it's .runtime_suspend callback to bring it into
  1379. * suspend state. Keep runtime PM disabled to preserve the state unless we
  1380. * encounter errors.
  1381. *
  1382. * Typically this function may be invoked from a system suspend callback to make
  1383. * sure the device is put into low power state.
  1384. */
  1385. int pm_runtime_force_suspend(struct device *dev)
  1386. {
  1387. int (*callback)(struct device *);
  1388. int ret = 0;
  1389. pm_runtime_disable(dev);
  1390. if (pm_runtime_status_suspended(dev))
  1391. return 0;
  1392. callback = RPM_GET_CALLBACK(dev, runtime_suspend);
  1393. if (!callback) {
  1394. ret = -ENOSYS;
  1395. goto err;
  1396. }
  1397. ret = callback(dev);
  1398. if (ret)
  1399. goto err;
  1400. /*
  1401. * Increase the runtime PM usage count for the device's parent, in case
  1402. * when we find the device being used when system suspend was invoked.
  1403. * This informs pm_runtime_force_resume() to resume the parent
  1404. * immediately, which is needed to be able to resume its children,
  1405. * when not deferring the resume to be managed via runtime PM.
  1406. */
  1407. if (dev->parent && atomic_read(&dev->power.usage_count) > 1)
  1408. pm_runtime_get_noresume(dev->parent);
  1409. pm_runtime_set_suspended(dev);
  1410. return 0;
  1411. err:
  1412. pm_runtime_enable(dev);
  1413. return ret;
  1414. }
  1415. EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
  1416. /**
  1417. * pm_runtime_force_resume - Force a device into resume state if needed.
  1418. * @dev: Device to resume.
  1419. *
  1420. * Prior invoking this function we expect the user to have brought the device
  1421. * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
  1422. * those actions and brings the device into full power, if it is expected to be
  1423. * used on system resume. To distinguish that, we check whether the runtime PM
  1424. * usage count is greater than 1 (the PM core increases the usage count in the
  1425. * system PM prepare phase), as that indicates a real user (such as a subsystem,
  1426. * driver, userspace, etc.) is using it. If that is the case, the device is
  1427. * expected to be used on system resume as well, so then we resume it. In the
  1428. * other case, we defer the resume to be managed via runtime PM.
  1429. *
  1430. * Typically this function may be invoked from a system resume callback.
  1431. */
  1432. int pm_runtime_force_resume(struct device *dev)
  1433. {
  1434. int (*callback)(struct device *);
  1435. int ret = 0;
  1436. callback = RPM_GET_CALLBACK(dev, runtime_resume);
  1437. if (!callback) {
  1438. ret = -ENOSYS;
  1439. goto out;
  1440. }
  1441. if (!pm_runtime_status_suspended(dev))
  1442. goto out;
  1443. /*
  1444. * Decrease the parent's runtime PM usage count, if we increased it
  1445. * during system suspend in pm_runtime_force_suspend().
  1446. */
  1447. if (atomic_read(&dev->power.usage_count) > 1) {
  1448. if (dev->parent)
  1449. pm_runtime_put_noidle(dev->parent);
  1450. } else {
  1451. goto out;
  1452. }
  1453. ret = pm_runtime_set_active(dev);
  1454. if (ret)
  1455. goto out;
  1456. ret = callback(dev);
  1457. if (ret) {
  1458. pm_runtime_set_suspended(dev);
  1459. goto out;
  1460. }
  1461. pm_runtime_mark_last_busy(dev);
  1462. out:
  1463. pm_runtime_enable(dev);
  1464. return ret;
  1465. }
  1466. EXPORT_SYMBOL_GPL(pm_runtime_force_resume);