runtime.c 41 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 <linux/pm_wakeirq.h>
  13. #include <trace/events/rpm.h>
  14. #include "power.h"
  15. typedef int (*pm_callback_t)(struct device *);
  16. static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
  17. {
  18. pm_callback_t cb;
  19. const struct dev_pm_ops *ops;
  20. if (dev->pm_domain)
  21. ops = &dev->pm_domain->ops;
  22. else if (dev->type && dev->type->pm)
  23. ops = dev->type->pm;
  24. else if (dev->class && dev->class->pm)
  25. ops = dev->class->pm;
  26. else if (dev->bus && dev->bus->pm)
  27. ops = dev->bus->pm;
  28. else
  29. ops = NULL;
  30. if (ops)
  31. cb = *(pm_callback_t *)((void *)ops + cb_offset);
  32. else
  33. cb = NULL;
  34. if (!cb && dev->driver && dev->driver->pm)
  35. cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
  36. return cb;
  37. }
  38. #define RPM_GET_CALLBACK(dev, callback) \
  39. __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
  40. static int rpm_resume(struct device *dev, int rpmflags);
  41. static int rpm_suspend(struct device *dev, int rpmflags);
  42. /**
  43. * update_pm_runtime_accounting - Update the time accounting of power states
  44. * @dev: Device to update the accounting for
  45. *
  46. * In order to be able to have time accounting of the various power states
  47. * (as used by programs such as PowerTOP to show the effectiveness of runtime
  48. * PM), we need to track the time spent in each state.
  49. * update_pm_runtime_accounting must be called each time before the
  50. * runtime_status field is updated, to account the time in the old state
  51. * correctly.
  52. */
  53. void update_pm_runtime_accounting(struct device *dev)
  54. {
  55. unsigned long now = jiffies;
  56. unsigned long delta;
  57. delta = now - dev->power.accounting_timestamp;
  58. dev->power.accounting_timestamp = now;
  59. if (dev->power.disable_depth > 0)
  60. return;
  61. if (dev->power.runtime_status == RPM_SUSPENDED)
  62. dev->power.suspended_jiffies += delta;
  63. else
  64. dev->power.active_jiffies += delta;
  65. }
  66. static void __update_runtime_status(struct device *dev, enum rpm_status status)
  67. {
  68. update_pm_runtime_accounting(dev);
  69. dev->power.runtime_status = status;
  70. }
  71. /**
  72. * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
  73. * @dev: Device to handle.
  74. */
  75. static void pm_runtime_deactivate_timer(struct device *dev)
  76. {
  77. if (dev->power.timer_expires > 0) {
  78. del_timer(&dev->power.suspend_timer);
  79. dev->power.timer_expires = 0;
  80. }
  81. }
  82. /**
  83. * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
  84. * @dev: Device to handle.
  85. */
  86. static void pm_runtime_cancel_pending(struct device *dev)
  87. {
  88. pm_runtime_deactivate_timer(dev);
  89. /*
  90. * In case there's a request pending, make sure its work function will
  91. * return without doing anything.
  92. */
  93. dev->power.request = RPM_REQ_NONE;
  94. }
  95. /*
  96. * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
  97. * @dev: Device to handle.
  98. *
  99. * Compute the autosuspend-delay expiration time based on the device's
  100. * power.last_busy time. If the delay has already expired or is disabled
  101. * (negative) or the power.use_autosuspend flag isn't set, return 0.
  102. * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
  103. *
  104. * This function may be called either with or without dev->power.lock held.
  105. * Either way it can be racy, since power.last_busy may be updated at any time.
  106. */
  107. unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
  108. {
  109. int autosuspend_delay;
  110. long elapsed;
  111. unsigned long last_busy;
  112. unsigned long expires = 0;
  113. if (!dev->power.use_autosuspend)
  114. goto out;
  115. autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
  116. if (autosuspend_delay < 0)
  117. goto out;
  118. last_busy = ACCESS_ONCE(dev->power.last_busy);
  119. elapsed = jiffies - last_busy;
  120. if (elapsed < 0)
  121. goto out; /* jiffies has wrapped around. */
  122. /*
  123. * If the autosuspend_delay is >= 1 second, align the timer by rounding
  124. * up to the nearest second.
  125. */
  126. expires = last_busy + msecs_to_jiffies(autosuspend_delay);
  127. if (autosuspend_delay >= 1000)
  128. expires = round_jiffies(expires);
  129. expires += !expires;
  130. if (elapsed >= expires - last_busy)
  131. expires = 0; /* Already expired. */
  132. out:
  133. return expires;
  134. }
  135. EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
  136. static int dev_memalloc_noio(struct device *dev, void *data)
  137. {
  138. return dev->power.memalloc_noio;
  139. }
  140. /*
  141. * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
  142. * @dev: Device to handle.
  143. * @enable: True for setting the flag and False for clearing the flag.
  144. *
  145. * Set the flag for all devices in the path from the device to the
  146. * root device in the device tree if @enable is true, otherwise clear
  147. * the flag for devices in the path whose siblings don't set the flag.
  148. *
  149. * The function should only be called by block device, or network
  150. * device driver for solving the deadlock problem during runtime
  151. * resume/suspend:
  152. *
  153. * If memory allocation with GFP_KERNEL is called inside runtime
  154. * resume/suspend callback of any one of its ancestors(or the
  155. * block device itself), the deadlock may be triggered inside the
  156. * memory allocation since it might not complete until the block
  157. * device becomes active and the involed page I/O finishes. The
  158. * situation is pointed out first by Alan Stern. Network device
  159. * are involved in iSCSI kind of situation.
  160. *
  161. * The lock of dev_hotplug_mutex is held in the function for handling
  162. * hotplug race because pm_runtime_set_memalloc_noio() may be called
  163. * in async probe().
  164. *
  165. * The function should be called between device_add() and device_del()
  166. * on the affected device(block/network device).
  167. */
  168. void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
  169. {
  170. static DEFINE_MUTEX(dev_hotplug_mutex);
  171. mutex_lock(&dev_hotplug_mutex);
  172. for (;;) {
  173. bool enabled;
  174. /* hold power lock since bitfield is not SMP-safe. */
  175. spin_lock_irq(&dev->power.lock);
  176. enabled = dev->power.memalloc_noio;
  177. dev->power.memalloc_noio = enable;
  178. spin_unlock_irq(&dev->power.lock);
  179. /*
  180. * not need to enable ancestors any more if the device
  181. * has been enabled.
  182. */
  183. if (enabled && enable)
  184. break;
  185. dev = dev->parent;
  186. /*
  187. * clear flag of the parent device only if all the
  188. * children don't set the flag because ancestor's
  189. * flag was set by any one of the descendants.
  190. */
  191. if (!dev || (!enable &&
  192. device_for_each_child(dev, NULL,
  193. dev_memalloc_noio)))
  194. break;
  195. }
  196. mutex_unlock(&dev_hotplug_mutex);
  197. }
  198. EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
  199. /**
  200. * rpm_check_suspend_allowed - Test whether a device may be suspended.
  201. * @dev: Device to test.
  202. */
  203. static int rpm_check_suspend_allowed(struct device *dev)
  204. {
  205. int retval = 0;
  206. if (dev->power.runtime_error)
  207. retval = -EINVAL;
  208. else if (dev->power.disable_depth > 0)
  209. retval = -EACCES;
  210. else if (atomic_read(&dev->power.usage_count) > 0)
  211. retval = -EAGAIN;
  212. else if (!pm_children_suspended(dev))
  213. retval = -EBUSY;
  214. /* Pending resume requests take precedence over suspends. */
  215. else if ((dev->power.deferred_resume
  216. && dev->power.runtime_status == RPM_SUSPENDING)
  217. || (dev->power.request_pending
  218. && dev->power.request == RPM_REQ_RESUME))
  219. retval = -EAGAIN;
  220. else if (__dev_pm_qos_read_value(dev) < 0)
  221. retval = -EPERM;
  222. else if (dev->power.runtime_status == RPM_SUSPENDED)
  223. retval = 1;
  224. return retval;
  225. }
  226. /**
  227. * __rpm_callback - Run a given runtime PM callback for a given device.
  228. * @cb: Runtime PM callback to run.
  229. * @dev: Device to run the callback for.
  230. */
  231. static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
  232. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  233. {
  234. int retval;
  235. if (dev->power.irq_safe)
  236. spin_unlock(&dev->power.lock);
  237. else
  238. spin_unlock_irq(&dev->power.lock);
  239. retval = cb(dev);
  240. if (dev->power.irq_safe)
  241. spin_lock(&dev->power.lock);
  242. else
  243. spin_lock_irq(&dev->power.lock);
  244. return retval;
  245. }
  246. /**
  247. * rpm_idle - Notify device bus type if the device can be suspended.
  248. * @dev: Device to notify the bus type about.
  249. * @rpmflags: Flag bits.
  250. *
  251. * Check if the device's runtime PM status allows it to be suspended. If
  252. * another idle notification has been started earlier, return immediately. If
  253. * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
  254. * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
  255. * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
  256. *
  257. * This function must be called under dev->power.lock with interrupts disabled.
  258. */
  259. static int rpm_idle(struct device *dev, int rpmflags)
  260. {
  261. int (*callback)(struct device *);
  262. int retval;
  263. trace_rpm_idle(dev, rpmflags);
  264. retval = rpm_check_suspend_allowed(dev);
  265. if (retval < 0)
  266. ; /* Conditions are wrong. */
  267. /* Idle notifications are allowed only in the RPM_ACTIVE state. */
  268. else if (dev->power.runtime_status != RPM_ACTIVE)
  269. retval = -EAGAIN;
  270. /*
  271. * Any pending request other than an idle notification takes
  272. * precedence over us, except that the timer may be running.
  273. */
  274. else if (dev->power.request_pending &&
  275. dev->power.request > RPM_REQ_IDLE)
  276. retval = -EAGAIN;
  277. /* Act as though RPM_NOWAIT is always set. */
  278. else if (dev->power.idle_notification)
  279. retval = -EINPROGRESS;
  280. if (retval)
  281. goto out;
  282. /* Pending requests need to be canceled. */
  283. dev->power.request = RPM_REQ_NONE;
  284. if (dev->power.no_callbacks)
  285. goto out;
  286. /* Carry out an asynchronous or a synchronous idle notification. */
  287. if (rpmflags & RPM_ASYNC) {
  288. dev->power.request = RPM_REQ_IDLE;
  289. if (!dev->power.request_pending) {
  290. dev->power.request_pending = true;
  291. queue_work(pm_wq, &dev->power.work);
  292. }
  293. trace_rpm_return_int(dev, _THIS_IP_, 0);
  294. return 0;
  295. }
  296. dev->power.idle_notification = true;
  297. callback = RPM_GET_CALLBACK(dev, runtime_idle);
  298. if (callback)
  299. retval = __rpm_callback(callback, dev);
  300. dev->power.idle_notification = false;
  301. wake_up_all(&dev->power.wait_queue);
  302. out:
  303. trace_rpm_return_int(dev, _THIS_IP_, retval);
  304. return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
  305. }
  306. /**
  307. * rpm_callback - Run a given runtime PM callback for a given device.
  308. * @cb: Runtime PM callback to run.
  309. * @dev: Device to run the callback for.
  310. */
  311. static int rpm_callback(int (*cb)(struct device *), struct device *dev)
  312. {
  313. int retval;
  314. if (!cb)
  315. return -ENOSYS;
  316. if (dev->power.memalloc_noio) {
  317. unsigned int noio_flag;
  318. /*
  319. * Deadlock might be caused if memory allocation with
  320. * GFP_KERNEL happens inside runtime_suspend and
  321. * runtime_resume callbacks of one block device's
  322. * ancestor or the block device itself. Network
  323. * device might be thought as part of iSCSI block
  324. * device, so network device and its ancestor should
  325. * be marked as memalloc_noio too.
  326. */
  327. noio_flag = memalloc_noio_save();
  328. retval = __rpm_callback(cb, dev);
  329. memalloc_noio_restore(noio_flag);
  330. } else {
  331. retval = __rpm_callback(cb, dev);
  332. }
  333. dev->power.runtime_error = retval;
  334. return retval != -EACCES ? retval : -EIO;
  335. }
  336. /**
  337. * rpm_suspend - Carry out runtime suspend of given device.
  338. * @dev: Device to suspend.
  339. * @rpmflags: Flag bits.
  340. *
  341. * Check if the device's runtime PM status allows it to be suspended.
  342. * Cancel a pending idle notification, autosuspend or suspend. If
  343. * another suspend has been started earlier, either return immediately
  344. * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
  345. * flags. If the RPM_ASYNC flag is set then queue a suspend request;
  346. * otherwise run the ->runtime_suspend() callback directly. When
  347. * ->runtime_suspend succeeded, if a deferred resume was requested while
  348. * the callback was running then carry it out, otherwise send an idle
  349. * notification for its parent (if the suspend succeeded and both
  350. * ignore_children of parent->power and irq_safe of dev->power are not set).
  351. * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
  352. * flag is set and the next autosuspend-delay expiration time is in the
  353. * future, schedule another autosuspend attempt.
  354. *
  355. * This function must be called under dev->power.lock with interrupts disabled.
  356. */
  357. static int rpm_suspend(struct device *dev, int rpmflags)
  358. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  359. {
  360. int (*callback)(struct device *);
  361. struct device *parent = NULL;
  362. int retval;
  363. trace_rpm_suspend(dev, rpmflags);
  364. repeat:
  365. retval = rpm_check_suspend_allowed(dev);
  366. if (retval < 0)
  367. ; /* Conditions are wrong. */
  368. /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
  369. else if (dev->power.runtime_status == RPM_RESUMING &&
  370. !(rpmflags & RPM_ASYNC))
  371. retval = -EAGAIN;
  372. if (retval)
  373. goto out;
  374. /* If the autosuspend_delay time hasn't expired yet, reschedule. */
  375. if ((rpmflags & RPM_AUTO)
  376. && dev->power.runtime_status != RPM_SUSPENDING) {
  377. unsigned long expires = pm_runtime_autosuspend_expiration(dev);
  378. if (expires != 0) {
  379. /* Pending requests need to be canceled. */
  380. dev->power.request = RPM_REQ_NONE;
  381. /*
  382. * Optimization: If the timer is already running and is
  383. * set to expire at or before the autosuspend delay,
  384. * avoid the overhead of resetting it. Just let it
  385. * expire; pm_suspend_timer_fn() will take care of the
  386. * rest.
  387. */
  388. if (!(dev->power.timer_expires && time_before_eq(
  389. dev->power.timer_expires, expires))) {
  390. dev->power.timer_expires = expires;
  391. mod_timer(&dev->power.suspend_timer, expires);
  392. }
  393. dev->power.timer_autosuspends = 1;
  394. goto out;
  395. }
  396. }
  397. /* Other scheduled or pending requests need to be canceled. */
  398. pm_runtime_cancel_pending(dev);
  399. if (dev->power.runtime_status == RPM_SUSPENDING) {
  400. DEFINE_WAIT(wait);
  401. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  402. retval = -EINPROGRESS;
  403. goto out;
  404. }
  405. if (dev->power.irq_safe) {
  406. spin_unlock(&dev->power.lock);
  407. cpu_relax();
  408. spin_lock(&dev->power.lock);
  409. goto repeat;
  410. }
  411. /* Wait for the other suspend running in parallel with us. */
  412. for (;;) {
  413. prepare_to_wait(&dev->power.wait_queue, &wait,
  414. TASK_UNINTERRUPTIBLE);
  415. if (dev->power.runtime_status != RPM_SUSPENDING)
  416. break;
  417. spin_unlock_irq(&dev->power.lock);
  418. schedule();
  419. spin_lock_irq(&dev->power.lock);
  420. }
  421. finish_wait(&dev->power.wait_queue, &wait);
  422. goto repeat;
  423. }
  424. if (dev->power.no_callbacks)
  425. goto no_callback; /* Assume success. */
  426. /* Carry out an asynchronous or a synchronous suspend. */
  427. if (rpmflags & RPM_ASYNC) {
  428. dev->power.request = (rpmflags & RPM_AUTO) ?
  429. RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
  430. if (!dev->power.request_pending) {
  431. dev->power.request_pending = true;
  432. queue_work(pm_wq, &dev->power.work);
  433. }
  434. goto out;
  435. }
  436. __update_runtime_status(dev, RPM_SUSPENDING);
  437. callback = RPM_GET_CALLBACK(dev, runtime_suspend);
  438. dev_pm_enable_wake_irq_check(dev, true);
  439. retval = rpm_callback(callback, dev);
  440. if (retval)
  441. goto fail;
  442. no_callback:
  443. __update_runtime_status(dev, RPM_SUSPENDED);
  444. pm_runtime_deactivate_timer(dev);
  445. if (dev->parent) {
  446. parent = dev->parent;
  447. atomic_add_unless(&parent->power.child_count, -1, 0);
  448. }
  449. wake_up_all(&dev->power.wait_queue);
  450. if (dev->power.deferred_resume) {
  451. dev->power.deferred_resume = false;
  452. rpm_resume(dev, 0);
  453. retval = -EAGAIN;
  454. goto out;
  455. }
  456. /* Maybe the parent is now able to suspend. */
  457. if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
  458. spin_unlock(&dev->power.lock);
  459. spin_lock(&parent->power.lock);
  460. rpm_idle(parent, RPM_ASYNC);
  461. spin_unlock(&parent->power.lock);
  462. spin_lock(&dev->power.lock);
  463. }
  464. out:
  465. trace_rpm_return_int(dev, _THIS_IP_, retval);
  466. return retval;
  467. fail:
  468. dev_pm_disable_wake_irq_check(dev);
  469. __update_runtime_status(dev, RPM_ACTIVE);
  470. dev->power.deferred_resume = false;
  471. wake_up_all(&dev->power.wait_queue);
  472. if (retval == -EAGAIN || retval == -EBUSY) {
  473. dev->power.runtime_error = 0;
  474. /*
  475. * If the callback routine failed an autosuspend, and
  476. * if the last_busy time has been updated so that there
  477. * is a new autosuspend expiration time, automatically
  478. * reschedule another autosuspend.
  479. */
  480. if ((rpmflags & RPM_AUTO) &&
  481. pm_runtime_autosuspend_expiration(dev) != 0)
  482. goto repeat;
  483. } else {
  484. pm_runtime_cancel_pending(dev);
  485. }
  486. goto out;
  487. }
  488. /**
  489. * rpm_resume - Carry out runtime resume of given device.
  490. * @dev: Device to resume.
  491. * @rpmflags: Flag bits.
  492. *
  493. * Check if the device's runtime PM status allows it to be resumed. Cancel
  494. * any scheduled or pending requests. If another resume has been started
  495. * earlier, either return immediately or wait for it to finish, depending on the
  496. * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
  497. * parallel with this function, either tell the other process to resume after
  498. * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
  499. * flag is set then queue a resume request; otherwise run the
  500. * ->runtime_resume() callback directly. Queue an idle notification for the
  501. * device if the resume succeeded.
  502. *
  503. * This function must be called under dev->power.lock with interrupts disabled.
  504. */
  505. static int rpm_resume(struct device *dev, int rpmflags)
  506. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  507. {
  508. int (*callback)(struct device *);
  509. struct device *parent = NULL;
  510. int retval = 0;
  511. trace_rpm_resume(dev, rpmflags);
  512. repeat:
  513. if (dev->power.runtime_error)
  514. retval = -EINVAL;
  515. else if (dev->power.disable_depth == 1 && dev->power.is_suspended
  516. && dev->power.runtime_status == RPM_ACTIVE)
  517. retval = 1;
  518. else if (dev->power.disable_depth > 0)
  519. retval = -EACCES;
  520. if (retval)
  521. goto out;
  522. /*
  523. * Other scheduled or pending requests need to be canceled. Small
  524. * optimization: If an autosuspend timer is running, leave it running
  525. * rather than cancelling it now only to restart it again in the near
  526. * future.
  527. */
  528. dev->power.request = RPM_REQ_NONE;
  529. if (!dev->power.timer_autosuspends)
  530. pm_runtime_deactivate_timer(dev);
  531. if (dev->power.runtime_status == RPM_ACTIVE) {
  532. retval = 1;
  533. goto out;
  534. }
  535. if (dev->power.runtime_status == RPM_RESUMING
  536. || dev->power.runtime_status == RPM_SUSPENDING) {
  537. DEFINE_WAIT(wait);
  538. if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
  539. if (dev->power.runtime_status == RPM_SUSPENDING)
  540. dev->power.deferred_resume = true;
  541. else
  542. retval = -EINPROGRESS;
  543. goto out;
  544. }
  545. if (dev->power.irq_safe) {
  546. spin_unlock(&dev->power.lock);
  547. cpu_relax();
  548. spin_lock(&dev->power.lock);
  549. goto repeat;
  550. }
  551. /* Wait for the operation carried out in parallel with us. */
  552. for (;;) {
  553. prepare_to_wait(&dev->power.wait_queue, &wait,
  554. TASK_UNINTERRUPTIBLE);
  555. if (dev->power.runtime_status != RPM_RESUMING
  556. && dev->power.runtime_status != RPM_SUSPENDING)
  557. break;
  558. spin_unlock_irq(&dev->power.lock);
  559. schedule();
  560. spin_lock_irq(&dev->power.lock);
  561. }
  562. finish_wait(&dev->power.wait_queue, &wait);
  563. goto repeat;
  564. }
  565. /*
  566. * See if we can skip waking up the parent. This is safe only if
  567. * power.no_callbacks is set, because otherwise we don't know whether
  568. * the resume will actually succeed.
  569. */
  570. if (dev->power.no_callbacks && !parent && dev->parent) {
  571. spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
  572. if (dev->parent->power.disable_depth > 0
  573. || dev->parent->power.ignore_children
  574. || dev->parent->power.runtime_status == RPM_ACTIVE) {
  575. atomic_inc(&dev->parent->power.child_count);
  576. spin_unlock(&dev->parent->power.lock);
  577. retval = 1;
  578. goto no_callback; /* Assume success. */
  579. }
  580. spin_unlock(&dev->parent->power.lock);
  581. }
  582. /* Carry out an asynchronous or a synchronous resume. */
  583. if (rpmflags & RPM_ASYNC) {
  584. dev->power.request = RPM_REQ_RESUME;
  585. if (!dev->power.request_pending) {
  586. dev->power.request_pending = true;
  587. queue_work(pm_wq, &dev->power.work);
  588. }
  589. retval = 0;
  590. goto out;
  591. }
  592. if (!parent && dev->parent) {
  593. /*
  594. * Increment the parent's usage counter and resume it if
  595. * necessary. Not needed if dev is irq-safe; then the
  596. * parent is permanently resumed.
  597. */
  598. parent = dev->parent;
  599. if (dev->power.irq_safe)
  600. goto skip_parent;
  601. spin_unlock(&dev->power.lock);
  602. pm_runtime_get_noresume(parent);
  603. spin_lock(&parent->power.lock);
  604. /*
  605. * We can resume if the parent's runtime PM is disabled or it
  606. * is set to ignore children.
  607. */
  608. if (!parent->power.disable_depth
  609. && !parent->power.ignore_children) {
  610. rpm_resume(parent, 0);
  611. if (parent->power.runtime_status != RPM_ACTIVE)
  612. retval = -EBUSY;
  613. }
  614. spin_unlock(&parent->power.lock);
  615. spin_lock(&dev->power.lock);
  616. if (retval)
  617. goto out;
  618. goto repeat;
  619. }
  620. skip_parent:
  621. if (dev->power.no_callbacks)
  622. goto no_callback; /* Assume success. */
  623. __update_runtime_status(dev, RPM_RESUMING);
  624. callback = RPM_GET_CALLBACK(dev, runtime_resume);
  625. dev_pm_disable_wake_irq_check(dev);
  626. retval = rpm_callback(callback, dev);
  627. if (retval) {
  628. __update_runtime_status(dev, RPM_SUSPENDED);
  629. pm_runtime_cancel_pending(dev);
  630. dev_pm_enable_wake_irq_check(dev, false);
  631. } else {
  632. no_callback:
  633. __update_runtime_status(dev, RPM_ACTIVE);
  634. pm_runtime_mark_last_busy(dev);
  635. if (parent)
  636. atomic_inc(&parent->power.child_count);
  637. }
  638. wake_up_all(&dev->power.wait_queue);
  639. if (retval >= 0)
  640. rpm_idle(dev, RPM_ASYNC);
  641. out:
  642. if (parent && !dev->power.irq_safe) {
  643. spin_unlock_irq(&dev->power.lock);
  644. pm_runtime_put(parent);
  645. spin_lock_irq(&dev->power.lock);
  646. }
  647. trace_rpm_return_int(dev, _THIS_IP_, retval);
  648. return retval;
  649. }
  650. /**
  651. * pm_runtime_work - Universal runtime PM work function.
  652. * @work: Work structure used for scheduling the execution of this function.
  653. *
  654. * Use @work to get the device object the work is to be done for, determine what
  655. * is to be done and execute the appropriate runtime PM function.
  656. */
  657. static void pm_runtime_work(struct work_struct *work)
  658. {
  659. struct device *dev = container_of(work, struct device, power.work);
  660. enum rpm_request req;
  661. spin_lock_irq(&dev->power.lock);
  662. if (!dev->power.request_pending)
  663. goto out;
  664. req = dev->power.request;
  665. dev->power.request = RPM_REQ_NONE;
  666. dev->power.request_pending = false;
  667. switch (req) {
  668. case RPM_REQ_NONE:
  669. break;
  670. case RPM_REQ_IDLE:
  671. rpm_idle(dev, RPM_NOWAIT);
  672. break;
  673. case RPM_REQ_SUSPEND:
  674. rpm_suspend(dev, RPM_NOWAIT);
  675. break;
  676. case RPM_REQ_AUTOSUSPEND:
  677. rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
  678. break;
  679. case RPM_REQ_RESUME:
  680. rpm_resume(dev, RPM_NOWAIT);
  681. break;
  682. }
  683. out:
  684. spin_unlock_irq(&dev->power.lock);
  685. }
  686. /**
  687. * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
  688. * @data: Device pointer passed by pm_schedule_suspend().
  689. *
  690. * Check if the time is right and queue a suspend request.
  691. */
  692. static void pm_suspend_timer_fn(unsigned long data)
  693. {
  694. struct device *dev = (struct device *)data;
  695. unsigned long flags;
  696. unsigned long expires;
  697. spin_lock_irqsave(&dev->power.lock, flags);
  698. expires = dev->power.timer_expires;
  699. /* If 'expire' is after 'jiffies' we've been called too early. */
  700. if (expires > 0 && !time_after(expires, jiffies)) {
  701. dev->power.timer_expires = 0;
  702. rpm_suspend(dev, dev->power.timer_autosuspends ?
  703. (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
  704. }
  705. spin_unlock_irqrestore(&dev->power.lock, flags);
  706. }
  707. /**
  708. * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
  709. * @dev: Device to suspend.
  710. * @delay: Time to wait before submitting a suspend request, in milliseconds.
  711. */
  712. int pm_schedule_suspend(struct device *dev, unsigned int delay)
  713. {
  714. unsigned long flags;
  715. int retval;
  716. spin_lock_irqsave(&dev->power.lock, flags);
  717. if (!delay) {
  718. retval = rpm_suspend(dev, RPM_ASYNC);
  719. goto out;
  720. }
  721. retval = rpm_check_suspend_allowed(dev);
  722. if (retval)
  723. goto out;
  724. /* Other scheduled or pending requests need to be canceled. */
  725. pm_runtime_cancel_pending(dev);
  726. dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
  727. dev->power.timer_expires += !dev->power.timer_expires;
  728. dev->power.timer_autosuspends = 0;
  729. mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
  730. out:
  731. spin_unlock_irqrestore(&dev->power.lock, flags);
  732. return retval;
  733. }
  734. EXPORT_SYMBOL_GPL(pm_schedule_suspend);
  735. /**
  736. * __pm_runtime_idle - Entry point for runtime idle operations.
  737. * @dev: Device to send idle notification for.
  738. * @rpmflags: Flag bits.
  739. *
  740. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  741. * return immediately if it is larger than zero. Then carry out an idle
  742. * notification, either synchronous or asynchronous.
  743. *
  744. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  745. * or if pm_runtime_irq_safe() has been called.
  746. */
  747. int __pm_runtime_idle(struct device *dev, int rpmflags)
  748. {
  749. unsigned long flags;
  750. int retval;
  751. if (rpmflags & RPM_GET_PUT) {
  752. if (!atomic_dec_and_test(&dev->power.usage_count))
  753. return 0;
  754. }
  755. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  756. spin_lock_irqsave(&dev->power.lock, flags);
  757. retval = rpm_idle(dev, rpmflags);
  758. spin_unlock_irqrestore(&dev->power.lock, flags);
  759. return retval;
  760. }
  761. EXPORT_SYMBOL_GPL(__pm_runtime_idle);
  762. /**
  763. * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
  764. * @dev: Device to suspend.
  765. * @rpmflags: Flag bits.
  766. *
  767. * If the RPM_GET_PUT flag is set, decrement the device's usage count and
  768. * return immediately if it is larger than zero. Then carry out a suspend,
  769. * either synchronous or asynchronous.
  770. *
  771. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  772. * or if pm_runtime_irq_safe() has been called.
  773. */
  774. int __pm_runtime_suspend(struct device *dev, int rpmflags)
  775. {
  776. unsigned long flags;
  777. int retval;
  778. if (rpmflags & RPM_GET_PUT) {
  779. if (!atomic_dec_and_test(&dev->power.usage_count))
  780. return 0;
  781. }
  782. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
  783. spin_lock_irqsave(&dev->power.lock, flags);
  784. retval = rpm_suspend(dev, rpmflags);
  785. spin_unlock_irqrestore(&dev->power.lock, flags);
  786. return retval;
  787. }
  788. EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
  789. /**
  790. * __pm_runtime_resume - Entry point for runtime resume operations.
  791. * @dev: Device to resume.
  792. * @rpmflags: Flag bits.
  793. *
  794. * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
  795. * carry out a resume, either synchronous or asynchronous.
  796. *
  797. * This routine may be called in atomic context if the RPM_ASYNC flag is set,
  798. * or if pm_runtime_irq_safe() has been called.
  799. */
  800. int __pm_runtime_resume(struct device *dev, int rpmflags)
  801. {
  802. unsigned long flags;
  803. int retval;
  804. might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
  805. dev->power.runtime_status != RPM_ACTIVE);
  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 it 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. }
  1188. /**
  1189. * pm_runtime_force_suspend - Force a device into suspend state if needed.
  1190. * @dev: Device to suspend.
  1191. *
  1192. * Disable runtime PM so we safely can check the device's runtime PM status and
  1193. * if it is active, invoke it's .runtime_suspend callback to bring it into
  1194. * suspend state. Keep runtime PM disabled to preserve the state unless we
  1195. * encounter errors.
  1196. *
  1197. * Typically this function may be invoked from a system suspend callback to make
  1198. * sure the device is put into low power state.
  1199. */
  1200. int pm_runtime_force_suspend(struct device *dev)
  1201. {
  1202. int (*callback)(struct device *);
  1203. int ret = 0;
  1204. pm_runtime_disable(dev);
  1205. if (pm_runtime_status_suspended(dev))
  1206. return 0;
  1207. callback = RPM_GET_CALLBACK(dev, runtime_suspend);
  1208. if (!callback) {
  1209. ret = -ENOSYS;
  1210. goto err;
  1211. }
  1212. ret = callback(dev);
  1213. if (ret)
  1214. goto err;
  1215. pm_runtime_set_suspended(dev);
  1216. return 0;
  1217. err:
  1218. pm_runtime_enable(dev);
  1219. return ret;
  1220. }
  1221. EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
  1222. /**
  1223. * pm_runtime_force_resume - Force a device into resume state.
  1224. * @dev: Device to resume.
  1225. *
  1226. * Prior invoking this function we expect the user to have brought the device
  1227. * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
  1228. * those actions and brings the device into full power. We update the runtime PM
  1229. * status and re-enables runtime PM.
  1230. *
  1231. * Typically this function may be invoked from a system resume callback to make
  1232. * sure the device is put into full power state.
  1233. */
  1234. int pm_runtime_force_resume(struct device *dev)
  1235. {
  1236. int (*callback)(struct device *);
  1237. int ret = 0;
  1238. callback = RPM_GET_CALLBACK(dev, runtime_resume);
  1239. if (!callback) {
  1240. ret = -ENOSYS;
  1241. goto out;
  1242. }
  1243. ret = pm_runtime_set_active(dev);
  1244. if (ret)
  1245. goto out;
  1246. ret = callback(dev);
  1247. if (ret) {
  1248. pm_runtime_set_suspended(dev);
  1249. goto out;
  1250. }
  1251. pm_runtime_mark_last_busy(dev);
  1252. out:
  1253. pm_runtime_enable(dev);
  1254. return ret;
  1255. }
  1256. EXPORT_SYMBOL_GPL(pm_runtime_force_resume);