runtime.c 25 KB

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