runtime.c 47 KB

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