runtime.c 40 KB

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