runtime.c 29 KB

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