runtime.c 46 KB

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