runtime.c 56 KB

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