runtime.c 39 KB

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