runtime.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. spin_lock_init(&dev->power.lock);
  914. dev->power.runtime_status = RPM_SUSPENDED;
  915. dev->power.idle_notification = false;
  916. dev->power.disable_depth = 1;
  917. atomic_set(&dev->power.usage_count, 0);
  918. dev->power.runtime_error = 0;
  919. atomic_set(&dev->power.child_count, 0);
  920. pm_suspend_ignore_children(dev, false);
  921. dev->power.runtime_auto = true;
  922. dev->power.request_pending = false;
  923. dev->power.request = RPM_REQ_NONE;
  924. dev->power.deferred_resume = false;
  925. dev->power.accounting_timestamp = jiffies;
  926. INIT_WORK(&dev->power.work, pm_runtime_work);
  927. dev->power.timer_expires = 0;
  928. setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
  929. (unsigned long)dev);
  930. init_waitqueue_head(&dev->power.wait_queue);
  931. }
  932. /**
  933. * pm_runtime_remove - Prepare for removing a device from device hierarchy.
  934. * @dev: Device object being removed from device hierarchy.
  935. */
  936. void pm_runtime_remove(struct device *dev)
  937. {
  938. __pm_runtime_disable(dev, false);
  939. /* Change the status back to 'suspended' to match the initial status. */
  940. if (dev->power.runtime_status == RPM_ACTIVE)
  941. pm_runtime_set_suspended(dev);
  942. }