runtime.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * drivers/base/power/runtime.c - Helper functions for device run-time PM
  3. *
  4. * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/jiffies.h>
  11. static int __pm_runtime_resume(struct device *dev, bool from_wq);
  12. static int __pm_request_idle(struct device *dev);
  13. static int __pm_request_resume(struct device *dev);
  14. /**
  15. * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
  16. * @dev: Device to handle.
  17. */
  18. static void pm_runtime_deactivate_timer(struct device *dev)
  19. {
  20. if (dev->power.timer_expires > 0) {
  21. del_timer(&dev->power.suspend_timer);
  22. dev->power.timer_expires = 0;
  23. }
  24. }
  25. /**
  26. * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
  27. * @dev: Device to handle.
  28. */
  29. static void pm_runtime_cancel_pending(struct device *dev)
  30. {
  31. pm_runtime_deactivate_timer(dev);
  32. /*
  33. * In case there's a request pending, make sure its work function will
  34. * return without doing anything.
  35. */
  36. dev->power.request = RPM_REQ_NONE;
  37. }
  38. /**
  39. * __pm_runtime_idle - Notify device bus type if the device can be suspended.
  40. * @dev: Device to notify the bus type about.
  41. *
  42. * This function must be called under dev->power.lock with interrupts disabled.
  43. */
  44. static int __pm_runtime_idle(struct device *dev)
  45. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  46. {
  47. int retval = 0;
  48. if (dev->power.runtime_error)
  49. retval = -EINVAL;
  50. else if (dev->power.idle_notification)
  51. retval = -EINPROGRESS;
  52. else if (atomic_read(&dev->power.usage_count) > 0
  53. || dev->power.disable_depth > 0
  54. || dev->power.runtime_status != RPM_ACTIVE)
  55. retval = -EAGAIN;
  56. else if (!pm_children_suspended(dev))
  57. retval = -EBUSY;
  58. if (retval)
  59. goto out;
  60. if (dev->power.request_pending) {
  61. /*
  62. * If an idle notification request is pending, cancel it. Any
  63. * other pending request takes precedence over us.
  64. */
  65. if (dev->power.request == RPM_REQ_IDLE) {
  66. dev->power.request = RPM_REQ_NONE;
  67. } else if (dev->power.request != RPM_REQ_NONE) {
  68. retval = -EAGAIN;
  69. goto out;
  70. }
  71. }
  72. dev->power.idle_notification = true;
  73. if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_idle) {
  74. spin_unlock_irq(&dev->power.lock);
  75. dev->bus->pm->runtime_idle(dev);
  76. spin_lock_irq(&dev->power.lock);
  77. }
  78. dev->power.idle_notification = false;
  79. wake_up_all(&dev->power.wait_queue);
  80. out:
  81. return retval;
  82. }
  83. /**
  84. * pm_runtime_idle - Notify device bus type if the device can be suspended.
  85. * @dev: Device to notify the bus type about.
  86. */
  87. int pm_runtime_idle(struct device *dev)
  88. {
  89. int retval;
  90. spin_lock_irq(&dev->power.lock);
  91. retval = __pm_runtime_idle(dev);
  92. spin_unlock_irq(&dev->power.lock);
  93. return retval;
  94. }
  95. EXPORT_SYMBOL_GPL(pm_runtime_idle);
  96. /**
  97. * __pm_runtime_suspend - Carry out run-time suspend of given device.
  98. * @dev: Device to suspend.
  99. * @from_wq: If set, the function has been called via pm_wq.
  100. *
  101. * Check if the device can be suspended and run the ->runtime_suspend() callback
  102. * provided by its bus type. If another suspend has been started earlier, wait
  103. * for it to finish. If an idle notification or suspend request is pending or
  104. * scheduled, cancel it.
  105. *
  106. * This function must be called under dev->power.lock with interrupts disabled.
  107. */
  108. int __pm_runtime_suspend(struct device *dev, bool from_wq)
  109. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  110. {
  111. struct device *parent = NULL;
  112. bool notify = false;
  113. int retval = 0;
  114. dev_dbg(dev, "__pm_runtime_suspend()%s!\n",
  115. from_wq ? " from workqueue" : "");
  116. repeat:
  117. if (dev->power.runtime_error) {
  118. retval = -EINVAL;
  119. goto out;
  120. }
  121. /* Pending resume requests take precedence over us. */
  122. if (dev->power.request_pending
  123. && dev->power.request == RPM_REQ_RESUME) {
  124. retval = -EAGAIN;
  125. goto out;
  126. }
  127. /* Other scheduled or pending requests need to be canceled. */
  128. pm_runtime_cancel_pending(dev);
  129. if (dev->power.runtime_status == RPM_SUSPENDED)
  130. retval = 1;
  131. else if (dev->power.runtime_status == RPM_RESUMING
  132. || dev->power.disable_depth > 0
  133. || atomic_read(&dev->power.usage_count) > 0)
  134. retval = -EAGAIN;
  135. else if (!pm_children_suspended(dev))
  136. retval = -EBUSY;
  137. if (retval)
  138. goto out;
  139. if (dev->power.runtime_status == RPM_SUSPENDING) {
  140. DEFINE_WAIT(wait);
  141. if (from_wq) {
  142. retval = -EINPROGRESS;
  143. goto out;
  144. }
  145. /* Wait for the other suspend running in parallel with us. */
  146. for (;;) {
  147. prepare_to_wait(&dev->power.wait_queue, &wait,
  148. TASK_UNINTERRUPTIBLE);
  149. if (dev->power.runtime_status != RPM_SUSPENDING)
  150. break;
  151. spin_unlock_irq(&dev->power.lock);
  152. schedule();
  153. spin_lock_irq(&dev->power.lock);
  154. }
  155. finish_wait(&dev->power.wait_queue, &wait);
  156. goto repeat;
  157. }
  158. dev->power.runtime_status = RPM_SUSPENDING;
  159. if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_suspend) {
  160. spin_unlock_irq(&dev->power.lock);
  161. retval = dev->bus->pm->runtime_suspend(dev);
  162. spin_lock_irq(&dev->power.lock);
  163. dev->power.runtime_error = retval;
  164. } else {
  165. retval = -ENOSYS;
  166. }
  167. if (retval) {
  168. dev->power.runtime_status = RPM_ACTIVE;
  169. pm_runtime_cancel_pending(dev);
  170. dev->power.deferred_resume = false;
  171. if (retval == -EAGAIN || retval == -EBUSY) {
  172. notify = true;
  173. dev->power.runtime_error = 0;
  174. }
  175. } else {
  176. dev->power.runtime_status = RPM_SUSPENDED;
  177. if (dev->parent) {
  178. parent = dev->parent;
  179. atomic_add_unless(&parent->power.child_count, -1, 0);
  180. }
  181. }
  182. wake_up_all(&dev->power.wait_queue);
  183. if (dev->power.deferred_resume) {
  184. dev->power.deferred_resume = false;
  185. __pm_runtime_resume(dev, false);
  186. retval = -EAGAIN;
  187. goto out;
  188. }
  189. if (notify)
  190. __pm_runtime_idle(dev);
  191. if (parent && !parent->power.ignore_children) {
  192. spin_unlock_irq(&dev->power.lock);
  193. pm_request_idle(parent);
  194. spin_lock_irq(&dev->power.lock);
  195. }
  196. out:
  197. dev_dbg(dev, "__pm_runtime_suspend() returns %d!\n", retval);
  198. return retval;
  199. }
  200. /**
  201. * pm_runtime_suspend - Carry out run-time suspend of given device.
  202. * @dev: Device to suspend.
  203. */
  204. int pm_runtime_suspend(struct device *dev)
  205. {
  206. int retval;
  207. spin_lock_irq(&dev->power.lock);
  208. retval = __pm_runtime_suspend(dev, false);
  209. spin_unlock_irq(&dev->power.lock);
  210. return retval;
  211. }
  212. EXPORT_SYMBOL_GPL(pm_runtime_suspend);
  213. /**
  214. * __pm_runtime_resume - Carry out run-time resume of given device.
  215. * @dev: Device to resume.
  216. * @from_wq: If set, the function has been called via pm_wq.
  217. *
  218. * Check if the device can be woken up and run the ->runtime_resume() callback
  219. * provided by its bus type. If another resume has been started earlier, wait
  220. * for it to finish. If there's a suspend running in parallel with this
  221. * function, wait for it to finish and resume the device. Cancel any scheduled
  222. * or pending requests.
  223. *
  224. * This function must be called under dev->power.lock with interrupts disabled.
  225. */
  226. int __pm_runtime_resume(struct device *dev, bool from_wq)
  227. __releases(&dev->power.lock) __acquires(&dev->power.lock)
  228. {
  229. struct device *parent = NULL;
  230. int retval = 0;
  231. dev_dbg(dev, "__pm_runtime_resume()%s!\n",
  232. from_wq ? " from workqueue" : "");
  233. repeat:
  234. if (dev->power.runtime_error) {
  235. retval = -EINVAL;
  236. goto out;
  237. }
  238. pm_runtime_cancel_pending(dev);
  239. if (dev->power.runtime_status == RPM_ACTIVE)
  240. retval = 1;
  241. else if (dev->power.disable_depth > 0)
  242. retval = -EAGAIN;
  243. if (retval)
  244. goto out;
  245. if (dev->power.runtime_status == RPM_RESUMING
  246. || dev->power.runtime_status == RPM_SUSPENDING) {
  247. DEFINE_WAIT(wait);
  248. if (from_wq) {
  249. if (dev->power.runtime_status == RPM_SUSPENDING)
  250. dev->power.deferred_resume = true;
  251. retval = -EINPROGRESS;
  252. goto out;
  253. }
  254. /* Wait for the operation carried out in parallel with us. */
  255. for (;;) {
  256. prepare_to_wait(&dev->power.wait_queue, &wait,
  257. TASK_UNINTERRUPTIBLE);
  258. if (dev->power.runtime_status != RPM_RESUMING
  259. && dev->power.runtime_status != RPM_SUSPENDING)
  260. break;
  261. spin_unlock_irq(&dev->power.lock);
  262. schedule();
  263. spin_lock_irq(&dev->power.lock);
  264. }
  265. finish_wait(&dev->power.wait_queue, &wait);
  266. goto repeat;
  267. }
  268. if (!parent && dev->parent) {
  269. /*
  270. * Increment the parent's resume counter and resume it if
  271. * necessary.
  272. */
  273. parent = dev->parent;
  274. spin_unlock_irq(&dev->power.lock);
  275. pm_runtime_get_noresume(parent);
  276. spin_lock_irq(&parent->power.lock);
  277. /*
  278. * We can resume if the parent's run-time PM is disabled or it
  279. * is set to ignore children.
  280. */
  281. if (!parent->power.disable_depth
  282. && !parent->power.ignore_children) {
  283. __pm_runtime_resume(parent, false);
  284. if (parent->power.runtime_status != RPM_ACTIVE)
  285. retval = -EBUSY;
  286. }
  287. spin_unlock_irq(&parent->power.lock);
  288. spin_lock_irq(&dev->power.lock);
  289. if (retval)
  290. goto out;
  291. goto repeat;
  292. }
  293. dev->power.runtime_status = RPM_RESUMING;
  294. if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_resume) {
  295. spin_unlock_irq(&dev->power.lock);
  296. retval = dev->bus->pm->runtime_resume(dev);
  297. spin_lock_irq(&dev->power.lock);
  298. dev->power.runtime_error = retval;
  299. } else {
  300. retval = -ENOSYS;
  301. }
  302. if (retval) {
  303. dev->power.runtime_status = RPM_SUSPENDED;
  304. pm_runtime_cancel_pending(dev);
  305. } else {
  306. dev->power.runtime_status = RPM_ACTIVE;
  307. if (parent)
  308. atomic_inc(&parent->power.child_count);
  309. }
  310. wake_up_all(&dev->power.wait_queue);
  311. if (!retval)
  312. __pm_request_idle(dev);
  313. out:
  314. if (parent) {
  315. spin_unlock_irq(&dev->power.lock);
  316. pm_runtime_put(parent);
  317. spin_lock_irq(&dev->power.lock);
  318. }
  319. dev_dbg(dev, "__pm_runtime_resume() returns %d!\n", retval);
  320. return retval;
  321. }
  322. /**
  323. * pm_runtime_resume - Carry out run-time resume of given device.
  324. * @dev: Device to suspend.
  325. */
  326. int pm_runtime_resume(struct device *dev)
  327. {
  328. int retval;
  329. spin_lock_irq(&dev->power.lock);
  330. retval = __pm_runtime_resume(dev, false);
  331. spin_unlock_irq(&dev->power.lock);
  332. return retval;
  333. }
  334. EXPORT_SYMBOL_GPL(pm_runtime_resume);
  335. /**
  336. * pm_runtime_work - Universal run-time PM work function.
  337. * @work: Work structure used for scheduling the execution of this function.
  338. *
  339. * Use @work to get the device object the work is to be done for, determine what
  340. * is to be done and execute the appropriate run-time PM function.
  341. */
  342. static void pm_runtime_work(struct work_struct *work)
  343. {
  344. struct device *dev = container_of(work, struct device, power.work);
  345. enum rpm_request req;
  346. spin_lock_irq(&dev->power.lock);
  347. if (!dev->power.request_pending)
  348. goto out;
  349. req = dev->power.request;
  350. dev->power.request = RPM_REQ_NONE;
  351. dev->power.request_pending = false;
  352. switch (req) {
  353. case RPM_REQ_NONE:
  354. break;
  355. case RPM_REQ_IDLE:
  356. __pm_runtime_idle(dev);
  357. break;
  358. case RPM_REQ_SUSPEND:
  359. __pm_runtime_suspend(dev, true);
  360. break;
  361. case RPM_REQ_RESUME:
  362. __pm_runtime_resume(dev, true);
  363. break;
  364. }
  365. out:
  366. spin_unlock_irq(&dev->power.lock);
  367. }
  368. /**
  369. * __pm_request_idle - Submit an idle notification request for given device.
  370. * @dev: Device to handle.
  371. *
  372. * Check if the device's run-time PM status is correct for suspending the device
  373. * and queue up a request to run __pm_runtime_idle() for it.
  374. *
  375. * This function must be called under dev->power.lock with interrupts disabled.
  376. */
  377. static int __pm_request_idle(struct device *dev)
  378. {
  379. int retval = 0;
  380. if (dev->power.runtime_error)
  381. retval = -EINVAL;
  382. else if (atomic_read(&dev->power.usage_count) > 0
  383. || dev->power.disable_depth > 0
  384. || dev->power.runtime_status == RPM_SUSPENDED
  385. || dev->power.runtime_status == RPM_SUSPENDING)
  386. retval = -EAGAIN;
  387. else if (!pm_children_suspended(dev))
  388. retval = -EBUSY;
  389. if (retval)
  390. return retval;
  391. if (dev->power.request_pending) {
  392. /* Any requests other then RPM_REQ_IDLE take precedence. */
  393. if (dev->power.request == RPM_REQ_NONE)
  394. dev->power.request = RPM_REQ_IDLE;
  395. else if (dev->power.request != RPM_REQ_IDLE)
  396. retval = -EAGAIN;
  397. return retval;
  398. }
  399. dev->power.request = RPM_REQ_IDLE;
  400. dev->power.request_pending = true;
  401. queue_work(pm_wq, &dev->power.work);
  402. return retval;
  403. }
  404. /**
  405. * pm_request_idle - Submit an idle notification request for given device.
  406. * @dev: Device to handle.
  407. */
  408. int pm_request_idle(struct device *dev)
  409. {
  410. unsigned long flags;
  411. int retval;
  412. spin_lock_irqsave(&dev->power.lock, flags);
  413. retval = __pm_request_idle(dev);
  414. spin_unlock_irqrestore(&dev->power.lock, flags);
  415. return retval;
  416. }
  417. EXPORT_SYMBOL_GPL(pm_request_idle);
  418. /**
  419. * __pm_request_suspend - Submit a suspend request for given device.
  420. * @dev: Device to suspend.
  421. *
  422. * This function must be called under dev->power.lock with interrupts disabled.
  423. */
  424. static int __pm_request_suspend(struct device *dev)
  425. {
  426. int retval = 0;
  427. if (dev->power.runtime_error)
  428. return -EINVAL;
  429. if (dev->power.runtime_status == RPM_SUSPENDED)
  430. retval = 1;
  431. else if (atomic_read(&dev->power.usage_count) > 0
  432. || dev->power.disable_depth > 0)
  433. retval = -EAGAIN;
  434. else if (dev->power.runtime_status == RPM_SUSPENDING)
  435. retval = -EINPROGRESS;
  436. else if (!pm_children_suspended(dev))
  437. retval = -EBUSY;
  438. if (retval < 0)
  439. return retval;
  440. pm_runtime_deactivate_timer(dev);
  441. if (dev->power.request_pending) {
  442. /*
  443. * Pending resume requests take precedence over us, but we can
  444. * overtake any other pending request.
  445. */
  446. if (dev->power.request == RPM_REQ_RESUME)
  447. retval = -EAGAIN;
  448. else if (dev->power.request != RPM_REQ_SUSPEND)
  449. dev->power.request = retval ?
  450. RPM_REQ_NONE : RPM_REQ_SUSPEND;
  451. return retval;
  452. } else if (retval) {
  453. return retval;
  454. }
  455. dev->power.request = RPM_REQ_SUSPEND;
  456. dev->power.request_pending = true;
  457. queue_work(pm_wq, &dev->power.work);
  458. return 0;
  459. }
  460. /**
  461. * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
  462. * @data: Device pointer passed by pm_schedule_suspend().
  463. *
  464. * Check if the time is right and execute __pm_request_suspend() in that case.
  465. */
  466. static void pm_suspend_timer_fn(unsigned long data)
  467. {
  468. struct device *dev = (struct device *)data;
  469. unsigned long flags;
  470. unsigned long expires;
  471. spin_lock_irqsave(&dev->power.lock, flags);
  472. expires = dev->power.timer_expires;
  473. /* If 'expire' is after 'jiffies' we've been called too early. */
  474. if (expires > 0 && !time_after(expires, jiffies)) {
  475. dev->power.timer_expires = 0;
  476. __pm_request_suspend(dev);
  477. }
  478. spin_unlock_irqrestore(&dev->power.lock, flags);
  479. }
  480. /**
  481. * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
  482. * @dev: Device to suspend.
  483. * @delay: Time to wait before submitting a suspend request, in milliseconds.
  484. */
  485. int pm_schedule_suspend(struct device *dev, unsigned int delay)
  486. {
  487. unsigned long flags;
  488. int retval = 0;
  489. spin_lock_irqsave(&dev->power.lock, flags);
  490. if (dev->power.runtime_error) {
  491. retval = -EINVAL;
  492. goto out;
  493. }
  494. if (!delay) {
  495. retval = __pm_request_suspend(dev);
  496. goto out;
  497. }
  498. pm_runtime_deactivate_timer(dev);
  499. if (dev->power.request_pending) {
  500. /*
  501. * Pending resume requests take precedence over us, but any
  502. * other pending requests have to be canceled.
  503. */
  504. if (dev->power.request == RPM_REQ_RESUME) {
  505. retval = -EAGAIN;
  506. goto out;
  507. }
  508. dev->power.request = RPM_REQ_NONE;
  509. }
  510. if (dev->power.runtime_status == RPM_SUSPENDED)
  511. retval = 1;
  512. else if (dev->power.runtime_status == RPM_SUSPENDING)
  513. retval = -EINPROGRESS;
  514. else if (atomic_read(&dev->power.usage_count) > 0
  515. || dev->power.disable_depth > 0)
  516. retval = -EAGAIN;
  517. else if (!pm_children_suspended(dev))
  518. retval = -EBUSY;
  519. if (retval)
  520. goto out;
  521. dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
  522. mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
  523. out:
  524. spin_unlock_irqrestore(&dev->power.lock, flags);
  525. return retval;
  526. }
  527. EXPORT_SYMBOL_GPL(pm_schedule_suspend);
  528. /**
  529. * pm_request_resume - Submit a resume request for given device.
  530. * @dev: Device to resume.
  531. *
  532. * This function must be called under dev->power.lock with interrupts disabled.
  533. */
  534. static int __pm_request_resume(struct device *dev)
  535. {
  536. int retval = 0;
  537. if (dev->power.runtime_error)
  538. return -EINVAL;
  539. if (dev->power.runtime_status == RPM_ACTIVE)
  540. retval = 1;
  541. else if (dev->power.runtime_status == RPM_RESUMING)
  542. retval = -EINPROGRESS;
  543. else if (dev->power.disable_depth > 0)
  544. retval = -EAGAIN;
  545. if (retval < 0)
  546. return retval;
  547. pm_runtime_deactivate_timer(dev);
  548. if (dev->power.request_pending) {
  549. /* If non-resume request is pending, we can overtake it. */
  550. dev->power.request = retval ? RPM_REQ_NONE : RPM_REQ_RESUME;
  551. return retval;
  552. } else if (retval) {
  553. return retval;
  554. }
  555. dev->power.request = RPM_REQ_RESUME;
  556. dev->power.request_pending = true;
  557. queue_work(pm_wq, &dev->power.work);
  558. return retval;
  559. }
  560. /**
  561. * pm_request_resume - Submit a resume request for given device.
  562. * @dev: Device to resume.
  563. */
  564. int pm_request_resume(struct device *dev)
  565. {
  566. unsigned long flags;
  567. int retval;
  568. spin_lock_irqsave(&dev->power.lock, flags);
  569. retval = __pm_request_resume(dev);
  570. spin_unlock_irqrestore(&dev->power.lock, flags);
  571. return retval;
  572. }
  573. EXPORT_SYMBOL_GPL(pm_request_resume);
  574. /**
  575. * __pm_runtime_get - Reference count a device and wake it up, if necessary.
  576. * @dev: Device to handle.
  577. * @sync: If set and the device is suspended, resume it synchronously.
  578. *
  579. * Increment the usage count of the device and if it was zero previously,
  580. * resume it or submit a resume request for it, depending on the value of @sync.
  581. */
  582. int __pm_runtime_get(struct device *dev, bool sync)
  583. {
  584. int retval = 1;
  585. if (atomic_add_return(1, &dev->power.usage_count) == 1)
  586. retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);
  587. return retval;
  588. }
  589. EXPORT_SYMBOL_GPL(__pm_runtime_get);
  590. /**
  591. * __pm_runtime_put - Decrement the device's usage counter and notify its bus.
  592. * @dev: Device to handle.
  593. * @sync: If the device's bus type is to be notified, do that synchronously.
  594. *
  595. * Decrement the usage count of the device and if it reaches zero, carry out a
  596. * synchronous idle notification or submit an idle notification request for it,
  597. * depending on the value of @sync.
  598. */
  599. int __pm_runtime_put(struct device *dev, bool sync)
  600. {
  601. int retval = 0;
  602. if (atomic_dec_and_test(&dev->power.usage_count))
  603. retval = sync ? pm_runtime_idle(dev) : pm_request_idle(dev);
  604. return retval;
  605. }
  606. EXPORT_SYMBOL_GPL(__pm_runtime_put);
  607. /**
  608. * __pm_runtime_set_status - Set run-time PM status of a device.
  609. * @dev: Device to handle.
  610. * @status: New run-time PM status of the device.
  611. *
  612. * If run-time PM of the device is disabled or its power.runtime_error field is
  613. * different from zero, the status may be changed either to RPM_ACTIVE, or to
  614. * RPM_SUSPENDED, as long as that reflects the actual state of the device.
  615. * However, if the device has a parent and the parent is not active, and the
  616. * parent's power.ignore_children flag is unset, the device's status cannot be
  617. * set to RPM_ACTIVE, so -EBUSY is returned in that case.
  618. *
  619. * If successful, __pm_runtime_set_status() clears the power.runtime_error field
  620. * and the device parent's counter of unsuspended children is modified to
  621. * reflect the new status. If the new status is RPM_SUSPENDED, an idle
  622. * notification request for the parent is submitted.
  623. */
  624. int __pm_runtime_set_status(struct device *dev, unsigned int status)
  625. {
  626. struct device *parent = dev->parent;
  627. unsigned long flags;
  628. bool notify_parent = false;
  629. int error = 0;
  630. if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
  631. return -EINVAL;
  632. spin_lock_irqsave(&dev->power.lock, flags);
  633. if (!dev->power.runtime_error && !dev->power.disable_depth) {
  634. error = -EAGAIN;
  635. goto out;
  636. }
  637. if (dev->power.runtime_status == status)
  638. goto out_set;
  639. if (status == RPM_SUSPENDED) {
  640. /* It always is possible to set the status to 'suspended'. */
  641. if (parent) {
  642. atomic_add_unless(&parent->power.child_count, -1, 0);
  643. notify_parent = !parent->power.ignore_children;
  644. }
  645. goto out_set;
  646. }
  647. if (parent) {
  648. spin_lock_irq(&parent->power.lock);
  649. /*
  650. * It is invalid to put an active child under a parent that is
  651. * not active, has run-time PM enabled and the
  652. * 'power.ignore_children' flag unset.
  653. */
  654. if (!parent->power.disable_depth
  655. && !parent->power.ignore_children
  656. && parent->power.runtime_status != RPM_ACTIVE) {
  657. error = -EBUSY;
  658. } else {
  659. if (dev->power.runtime_status == RPM_SUSPENDED)
  660. atomic_inc(&parent->power.child_count);
  661. }
  662. spin_unlock_irq(&parent->power.lock);
  663. if (error)
  664. goto out;
  665. }
  666. out_set:
  667. dev->power.runtime_status = status;
  668. dev->power.runtime_error = 0;
  669. out:
  670. spin_unlock_irqrestore(&dev->power.lock, flags);
  671. if (notify_parent)
  672. pm_request_idle(parent);
  673. return error;
  674. }
  675. EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
  676. /**
  677. * __pm_runtime_barrier - Cancel pending requests and wait for completions.
  678. * @dev: Device to handle.
  679. *
  680. * Flush all pending requests for the device from pm_wq and wait for all
  681. * run-time PM operations involving the device in progress to complete.
  682. *
  683. * Should be called under dev->power.lock with interrupts disabled.
  684. */
  685. static void __pm_runtime_barrier(struct device *dev)
  686. {
  687. pm_runtime_deactivate_timer(dev);
  688. if (dev->power.request_pending) {
  689. dev->power.request = RPM_REQ_NONE;
  690. spin_unlock_irq(&dev->power.lock);
  691. cancel_work_sync(&dev->power.work);
  692. spin_lock_irq(&dev->power.lock);
  693. dev->power.request_pending = false;
  694. }
  695. if (dev->power.runtime_status == RPM_SUSPENDING
  696. || dev->power.runtime_status == RPM_RESUMING
  697. || dev->power.idle_notification) {
  698. DEFINE_WAIT(wait);
  699. /* Suspend, wake-up or idle notification in progress. */
  700. for (;;) {
  701. prepare_to_wait(&dev->power.wait_queue, &wait,
  702. TASK_UNINTERRUPTIBLE);
  703. if (dev->power.runtime_status != RPM_SUSPENDING
  704. && dev->power.runtime_status != RPM_RESUMING
  705. && !dev->power.idle_notification)
  706. break;
  707. spin_unlock_irq(&dev->power.lock);
  708. schedule();
  709. spin_lock_irq(&dev->power.lock);
  710. }
  711. finish_wait(&dev->power.wait_queue, &wait);
  712. }
  713. }
  714. /**
  715. * pm_runtime_barrier - Flush pending requests and wait for completions.
  716. * @dev: Device to handle.
  717. *
  718. * Prevent the device from being suspended by incrementing its usage counter and
  719. * if there's a pending resume request for the device, wake the device up.
  720. * Next, make sure that all pending requests for the device have been flushed
  721. * from pm_wq and wait for all run-time PM operations involving the device in
  722. * progress to complete.
  723. *
  724. * Return value:
  725. * 1, if there was a resume request pending and the device had to be woken up,
  726. * 0, otherwise
  727. */
  728. int pm_runtime_barrier(struct device *dev)
  729. {
  730. int retval = 0;
  731. pm_runtime_get_noresume(dev);
  732. spin_lock_irq(&dev->power.lock);
  733. if (dev->power.request_pending
  734. && dev->power.request == RPM_REQ_RESUME) {
  735. __pm_runtime_resume(dev, false);
  736. retval = 1;
  737. }
  738. __pm_runtime_barrier(dev);
  739. spin_unlock_irq(&dev->power.lock);
  740. pm_runtime_put_noidle(dev);
  741. return retval;
  742. }
  743. EXPORT_SYMBOL_GPL(pm_runtime_barrier);
  744. /**
  745. * __pm_runtime_disable - Disable run-time PM of a device.
  746. * @dev: Device to handle.
  747. * @check_resume: If set, check if there's a resume request for the device.
  748. *
  749. * Increment power.disable_depth for the device and if was zero previously,
  750. * cancel all pending run-time PM requests for the device and wait for all
  751. * operations in progress to complete. The device can be either active or
  752. * suspended after its run-time PM has been disabled.
  753. *
  754. * If @check_resume is set and there's a resume request pending when
  755. * __pm_runtime_disable() is called and power.disable_depth is zero, the
  756. * function will wake up the device before disabling its run-time PM.
  757. */
  758. void __pm_runtime_disable(struct device *dev, bool check_resume)
  759. {
  760. spin_lock_irq(&dev->power.lock);
  761. if (dev->power.disable_depth > 0) {
  762. dev->power.disable_depth++;
  763. goto out;
  764. }
  765. /*
  766. * Wake up the device if there's a resume request pending, because that
  767. * means there probably is some I/O to process and disabling run-time PM
  768. * shouldn't prevent the device from processing the I/O.
  769. */
  770. if (check_resume && dev->power.request_pending
  771. && dev->power.request == RPM_REQ_RESUME) {
  772. /*
  773. * Prevent suspends and idle notifications from being carried
  774. * out after we have woken up the device.
  775. */
  776. pm_runtime_get_noresume(dev);
  777. __pm_runtime_resume(dev, false);
  778. pm_runtime_put_noidle(dev);
  779. }
  780. if (!dev->power.disable_depth++)
  781. __pm_runtime_barrier(dev);
  782. out:
  783. spin_unlock_irq(&dev->power.lock);
  784. }
  785. EXPORT_SYMBOL_GPL(__pm_runtime_disable);
  786. /**
  787. * pm_runtime_enable - Enable run-time PM of a device.
  788. * @dev: Device to handle.
  789. */
  790. void pm_runtime_enable(struct device *dev)
  791. {
  792. unsigned long flags;
  793. spin_lock_irqsave(&dev->power.lock, flags);
  794. if (dev->power.disable_depth > 0)
  795. dev->power.disable_depth--;
  796. else
  797. dev_warn(dev, "Unbalanced %s!\n", __func__);
  798. spin_unlock_irqrestore(&dev->power.lock, flags);
  799. }
  800. EXPORT_SYMBOL_GPL(pm_runtime_enable);
  801. /**
  802. * pm_runtime_init - Initialize run-time PM fields in given device object.
  803. * @dev: Device object to initialize.
  804. */
  805. void pm_runtime_init(struct device *dev)
  806. {
  807. spin_lock_init(&dev->power.lock);
  808. dev->power.runtime_status = RPM_SUSPENDED;
  809. dev->power.idle_notification = false;
  810. dev->power.disable_depth = 1;
  811. atomic_set(&dev->power.usage_count, 0);
  812. dev->power.runtime_error = 0;
  813. atomic_set(&dev->power.child_count, 0);
  814. pm_suspend_ignore_children(dev, false);
  815. dev->power.request_pending = false;
  816. dev->power.request = RPM_REQ_NONE;
  817. dev->power.deferred_resume = false;
  818. INIT_WORK(&dev->power.work, pm_runtime_work);
  819. dev->power.timer_expires = 0;
  820. setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
  821. (unsigned long)dev);
  822. init_waitqueue_head(&dev->power.wait_queue);
  823. }
  824. /**
  825. * pm_runtime_remove - Prepare for removing a device from device hierarchy.
  826. * @dev: Device object being removed from device hierarchy.
  827. */
  828. void pm_runtime_remove(struct device *dev)
  829. {
  830. __pm_runtime_disable(dev, false);
  831. /* Change the status back to 'suspended' to match the initial status. */
  832. if (dev->power.runtime_status == RPM_ACTIVE)
  833. pm_runtime_set_suspended(dev);
  834. }