domain.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * drivers/base/power/domain.c - Common code related to device power domains.
  3. *
  4. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/pm_domain.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/sched.h>
  16. #include <linux/suspend.h>
  17. static LIST_HEAD(gpd_list);
  18. static DEFINE_MUTEX(gpd_list_lock);
  19. #ifdef CONFIG_PM
  20. static struct generic_pm_domain *dev_to_genpd(struct device *dev)
  21. {
  22. if (IS_ERR_OR_NULL(dev->pm_domain))
  23. return ERR_PTR(-EINVAL);
  24. return pd_to_genpd(dev->pm_domain);
  25. }
  26. static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  27. {
  28. if (!WARN_ON(genpd->sd_count == 0))
  29. genpd->sd_count--;
  30. }
  31. static void genpd_acquire_lock(struct generic_pm_domain *genpd)
  32. {
  33. DEFINE_WAIT(wait);
  34. mutex_lock(&genpd->lock);
  35. /*
  36. * Wait for the domain to transition into either the active,
  37. * or the power off state.
  38. */
  39. for (;;) {
  40. prepare_to_wait(&genpd->status_wait_queue, &wait,
  41. TASK_UNINTERRUPTIBLE);
  42. if (genpd->status == GPD_STATE_ACTIVE
  43. || genpd->status == GPD_STATE_POWER_OFF)
  44. break;
  45. mutex_unlock(&genpd->lock);
  46. schedule();
  47. mutex_lock(&genpd->lock);
  48. }
  49. finish_wait(&genpd->status_wait_queue, &wait);
  50. }
  51. static void genpd_release_lock(struct generic_pm_domain *genpd)
  52. {
  53. mutex_unlock(&genpd->lock);
  54. }
  55. static void genpd_set_active(struct generic_pm_domain *genpd)
  56. {
  57. if (genpd->resume_count == 0)
  58. genpd->status = GPD_STATE_ACTIVE;
  59. }
  60. /**
  61. * pm_genpd_poweron - Restore power to a given PM domain and its parents.
  62. * @genpd: PM domain to power up.
  63. *
  64. * Restore power to @genpd and all of its parents so that it is possible to
  65. * resume a device belonging to it.
  66. */
  67. int pm_genpd_poweron(struct generic_pm_domain *genpd)
  68. {
  69. struct generic_pm_domain *parent = genpd->parent;
  70. DEFINE_WAIT(wait);
  71. int ret = 0;
  72. start:
  73. if (parent) {
  74. genpd_acquire_lock(parent);
  75. mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
  76. } else {
  77. mutex_lock(&genpd->lock);
  78. }
  79. if (genpd->status == GPD_STATE_ACTIVE
  80. || (genpd->prepared_count > 0 && genpd->suspend_power_off))
  81. goto out;
  82. if (genpd->status != GPD_STATE_POWER_OFF) {
  83. genpd_set_active(genpd);
  84. goto out;
  85. }
  86. if (parent && parent->status != GPD_STATE_ACTIVE) {
  87. mutex_unlock(&genpd->lock);
  88. genpd_release_lock(parent);
  89. ret = pm_genpd_poweron(parent);
  90. if (ret)
  91. return ret;
  92. goto start;
  93. }
  94. if (genpd->power_on) {
  95. int ret = genpd->power_on(genpd);
  96. if (ret)
  97. goto out;
  98. }
  99. genpd_set_active(genpd);
  100. if (parent)
  101. parent->sd_count++;
  102. out:
  103. mutex_unlock(&genpd->lock);
  104. if (parent)
  105. genpd_release_lock(parent);
  106. return ret;
  107. }
  108. #endif /* CONFIG_PM */
  109. #ifdef CONFIG_PM_RUNTIME
  110. /**
  111. * __pm_genpd_save_device - Save the pre-suspend state of a device.
  112. * @dle: Device list entry of the device to save the state of.
  113. * @genpd: PM domain the device belongs to.
  114. */
  115. static int __pm_genpd_save_device(struct dev_list_entry *dle,
  116. struct generic_pm_domain *genpd)
  117. __releases(&genpd->lock) __acquires(&genpd->lock)
  118. {
  119. struct device *dev = dle->dev;
  120. struct device_driver *drv = dev->driver;
  121. int ret = 0;
  122. if (dle->need_restore)
  123. return 0;
  124. mutex_unlock(&genpd->lock);
  125. if (drv && drv->pm && drv->pm->runtime_suspend) {
  126. if (genpd->start_device)
  127. genpd->start_device(dev);
  128. ret = drv->pm->runtime_suspend(dev);
  129. if (genpd->stop_device)
  130. genpd->stop_device(dev);
  131. }
  132. mutex_lock(&genpd->lock);
  133. if (!ret)
  134. dle->need_restore = true;
  135. return ret;
  136. }
  137. /**
  138. * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
  139. * @dle: Device list entry of the device to restore the state of.
  140. * @genpd: PM domain the device belongs to.
  141. */
  142. static void __pm_genpd_restore_device(struct dev_list_entry *dle,
  143. struct generic_pm_domain *genpd)
  144. __releases(&genpd->lock) __acquires(&genpd->lock)
  145. {
  146. struct device *dev = dle->dev;
  147. struct device_driver *drv = dev->driver;
  148. if (!dle->need_restore)
  149. return;
  150. mutex_unlock(&genpd->lock);
  151. if (drv && drv->pm && drv->pm->runtime_resume) {
  152. if (genpd->start_device)
  153. genpd->start_device(dev);
  154. drv->pm->runtime_resume(dev);
  155. if (genpd->stop_device)
  156. genpd->stop_device(dev);
  157. }
  158. mutex_lock(&genpd->lock);
  159. dle->need_restore = false;
  160. }
  161. /**
  162. * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
  163. * @genpd: PM domain to check.
  164. *
  165. * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
  166. * a "power off" operation, which means that a "power on" has occured in the
  167. * meantime, or if its resume_count field is different from zero, which means
  168. * that one of its devices has been resumed in the meantime.
  169. */
  170. static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
  171. {
  172. return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
  173. }
  174. /**
  175. * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
  176. * @genpd: PM domait to power off.
  177. *
  178. * Queue up the execution of pm_genpd_poweroff() unless it's already been done
  179. * before.
  180. */
  181. void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  182. {
  183. if (!work_pending(&genpd->power_off_work))
  184. queue_work(pm_wq, &genpd->power_off_work);
  185. }
  186. /**
  187. * pm_genpd_poweroff - Remove power from a given PM domain.
  188. * @genpd: PM domain to power down.
  189. *
  190. * If all of the @genpd's devices have been suspended and all of its subdomains
  191. * have been powered down, run the runtime suspend callbacks provided by all of
  192. * the @genpd's devices' drivers and remove power from @genpd.
  193. */
  194. static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
  195. __releases(&genpd->lock) __acquires(&genpd->lock)
  196. {
  197. struct generic_pm_domain *parent;
  198. struct dev_list_entry *dle;
  199. unsigned int not_suspended;
  200. int ret = 0;
  201. start:
  202. /*
  203. * Do not try to power off the domain in the following situations:
  204. * (1) The domain is already in the "power off" state.
  205. * (2) System suspend is in progress.
  206. * (3) One of the domain's devices is being resumed right now.
  207. */
  208. if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
  209. || genpd->resume_count > 0)
  210. return 0;
  211. if (genpd->sd_count > 0)
  212. return -EBUSY;
  213. not_suspended = 0;
  214. list_for_each_entry(dle, &genpd->dev_list, node)
  215. if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
  216. not_suspended++;
  217. if (not_suspended > genpd->in_progress)
  218. return -EBUSY;
  219. if (genpd->poweroff_task) {
  220. /*
  221. * Another instance of pm_genpd_poweroff() is executing
  222. * callbacks, so tell it to start over and return.
  223. */
  224. genpd->status = GPD_STATE_REPEAT;
  225. return 0;
  226. }
  227. if (genpd->gov && genpd->gov->power_down_ok) {
  228. if (!genpd->gov->power_down_ok(&genpd->domain))
  229. return -EAGAIN;
  230. }
  231. genpd->status = GPD_STATE_BUSY;
  232. genpd->poweroff_task = current;
  233. list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
  234. ret = __pm_genpd_save_device(dle, genpd);
  235. if (ret) {
  236. genpd_set_active(genpd);
  237. goto out;
  238. }
  239. if (genpd_abort_poweroff(genpd))
  240. goto out;
  241. if (genpd->status == GPD_STATE_REPEAT) {
  242. genpd->poweroff_task = NULL;
  243. goto start;
  244. }
  245. }
  246. parent = genpd->parent;
  247. if (parent) {
  248. mutex_unlock(&genpd->lock);
  249. genpd_acquire_lock(parent);
  250. mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
  251. if (genpd_abort_poweroff(genpd)) {
  252. genpd_release_lock(parent);
  253. goto out;
  254. }
  255. }
  256. if (genpd->power_off) {
  257. ret = genpd->power_off(genpd);
  258. if (ret == -EBUSY) {
  259. genpd_set_active(genpd);
  260. if (parent)
  261. genpd_release_lock(parent);
  262. goto out;
  263. }
  264. }
  265. genpd->status = GPD_STATE_POWER_OFF;
  266. if (parent) {
  267. genpd_sd_counter_dec(parent);
  268. if (parent->sd_count == 0)
  269. genpd_queue_power_off_work(parent);
  270. genpd_release_lock(parent);
  271. }
  272. out:
  273. genpd->poweroff_task = NULL;
  274. wake_up_all(&genpd->status_wait_queue);
  275. return ret;
  276. }
  277. /**
  278. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  279. * @work: Work structure used for scheduling the execution of this function.
  280. */
  281. static void genpd_power_off_work_fn(struct work_struct *work)
  282. {
  283. struct generic_pm_domain *genpd;
  284. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  285. genpd_acquire_lock(genpd);
  286. pm_genpd_poweroff(genpd);
  287. genpd_release_lock(genpd);
  288. }
  289. /**
  290. * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  291. * @dev: Device to suspend.
  292. *
  293. * Carry out a runtime suspend of a device under the assumption that its
  294. * pm_domain field points to the domain member of an object of type
  295. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  296. */
  297. static int pm_genpd_runtime_suspend(struct device *dev)
  298. {
  299. struct generic_pm_domain *genpd;
  300. dev_dbg(dev, "%s()\n", __func__);
  301. genpd = dev_to_genpd(dev);
  302. if (IS_ERR(genpd))
  303. return -EINVAL;
  304. if (genpd->stop_device) {
  305. int ret = genpd->stop_device(dev);
  306. if (ret)
  307. return ret;
  308. }
  309. mutex_lock(&genpd->lock);
  310. genpd->in_progress++;
  311. pm_genpd_poweroff(genpd);
  312. genpd->in_progress--;
  313. mutex_unlock(&genpd->lock);
  314. return 0;
  315. }
  316. /**
  317. * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  318. * @dev: Device to resume.
  319. * @genpd: PM domain the device belongs to.
  320. */
  321. static void __pm_genpd_runtime_resume(struct device *dev,
  322. struct generic_pm_domain *genpd)
  323. {
  324. struct dev_list_entry *dle;
  325. list_for_each_entry(dle, &genpd->dev_list, node) {
  326. if (dle->dev == dev) {
  327. __pm_genpd_restore_device(dle, genpd);
  328. break;
  329. }
  330. }
  331. }
  332. /**
  333. * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  334. * @dev: Device to resume.
  335. *
  336. * Carry out a runtime resume of a device under the assumption that its
  337. * pm_domain field points to the domain member of an object of type
  338. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  339. */
  340. static int pm_genpd_runtime_resume(struct device *dev)
  341. {
  342. struct generic_pm_domain *genpd;
  343. DEFINE_WAIT(wait);
  344. int ret;
  345. dev_dbg(dev, "%s()\n", __func__);
  346. genpd = dev_to_genpd(dev);
  347. if (IS_ERR(genpd))
  348. return -EINVAL;
  349. ret = pm_genpd_poweron(genpd);
  350. if (ret)
  351. return ret;
  352. mutex_lock(&genpd->lock);
  353. genpd->status = GPD_STATE_BUSY;
  354. genpd->resume_count++;
  355. for (;;) {
  356. prepare_to_wait(&genpd->status_wait_queue, &wait,
  357. TASK_UNINTERRUPTIBLE);
  358. /*
  359. * If current is the powering off task, we have been called
  360. * reentrantly from one of the device callbacks, so we should
  361. * not wait.
  362. */
  363. if (!genpd->poweroff_task || genpd->poweroff_task == current)
  364. break;
  365. mutex_unlock(&genpd->lock);
  366. schedule();
  367. mutex_lock(&genpd->lock);
  368. }
  369. finish_wait(&genpd->status_wait_queue, &wait);
  370. __pm_genpd_runtime_resume(dev, genpd);
  371. genpd->resume_count--;
  372. genpd_set_active(genpd);
  373. wake_up_all(&genpd->status_wait_queue);
  374. mutex_unlock(&genpd->lock);
  375. if (genpd->start_device)
  376. genpd->start_device(dev);
  377. return 0;
  378. }
  379. #else
  380. static inline void genpd_power_off_work_fn(struct work_struct *work) {}
  381. static inline void __pm_genpd_runtime_resume(struct device *dev,
  382. struct generic_pm_domain *genpd) {}
  383. #define pm_genpd_runtime_suspend NULL
  384. #define pm_genpd_runtime_resume NULL
  385. #endif /* CONFIG_PM_RUNTIME */
  386. #ifdef CONFIG_PM_SLEEP
  387. /**
  388. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
  389. * @genpd: PM domain to power off, if possible.
  390. *
  391. * Check if the given PM domain can be powered off (during system suspend or
  392. * hibernation) and do that if so. Also, in that case propagate to its parent.
  393. *
  394. * This function is only called in "noirq" stages of system power transitions,
  395. * so it need not acquire locks (all of the "noirq" callbacks are executed
  396. * sequentially, so it is guaranteed that it will never run twice in parallel).
  397. */
  398. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
  399. {
  400. struct generic_pm_domain *parent = genpd->parent;
  401. if (genpd->status == GPD_STATE_POWER_OFF)
  402. return;
  403. if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
  404. return;
  405. if (genpd->power_off)
  406. genpd->power_off(genpd);
  407. genpd->status = GPD_STATE_POWER_OFF;
  408. if (parent) {
  409. genpd_sd_counter_dec(parent);
  410. pm_genpd_sync_poweroff(parent);
  411. }
  412. }
  413. /**
  414. * resume_needed - Check whether to resume a device before system suspend.
  415. * @dev: Device to check.
  416. * @genpd: PM domain the device belongs to.
  417. *
  418. * There are two cases in which a device that can wake up the system from sleep
  419. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  420. * to wake up the system and it has to remain active for this purpose while the
  421. * system is in the sleep state and (2) if the device is not enabled to wake up
  422. * the system from sleep states and it generally doesn't generate wakeup signals
  423. * by itself (those signals are generated on its behalf by other parts of the
  424. * system). In the latter case it may be necessary to reconfigure the device's
  425. * wakeup settings during system suspend, because it may have been set up to
  426. * signal remote wakeup from the system's working state as needed by runtime PM.
  427. * Return 'true' in either of the above cases.
  428. */
  429. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  430. {
  431. bool active_wakeup;
  432. if (!device_can_wakeup(dev))
  433. return false;
  434. active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
  435. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  436. }
  437. /**
  438. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  439. * @dev: Device to start the transition of.
  440. *
  441. * Start a power transition of a device (during a system-wide power transition)
  442. * under the assumption that its pm_domain field points to the domain member of
  443. * an object of type struct generic_pm_domain representing a PM domain
  444. * consisting of I/O devices.
  445. */
  446. static int pm_genpd_prepare(struct device *dev)
  447. {
  448. struct generic_pm_domain *genpd;
  449. int ret;
  450. dev_dbg(dev, "%s()\n", __func__);
  451. genpd = dev_to_genpd(dev);
  452. if (IS_ERR(genpd))
  453. return -EINVAL;
  454. /*
  455. * If a wakeup request is pending for the device, it should be woken up
  456. * at this point and a system wakeup event should be reported if it's
  457. * set up to wake up the system from sleep states.
  458. */
  459. pm_runtime_get_noresume(dev);
  460. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  461. pm_wakeup_event(dev, 0);
  462. if (pm_wakeup_pending()) {
  463. pm_runtime_put_sync(dev);
  464. return -EBUSY;
  465. }
  466. if (resume_needed(dev, genpd))
  467. pm_runtime_resume(dev);
  468. genpd_acquire_lock(genpd);
  469. if (genpd->prepared_count++ == 0)
  470. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  471. genpd_release_lock(genpd);
  472. if (genpd->suspend_power_off) {
  473. pm_runtime_put_noidle(dev);
  474. return 0;
  475. }
  476. /*
  477. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  478. * so pm_genpd_poweron() will return immediately, but if the device
  479. * is suspended (e.g. it's been stopped by .stop_device()), we need
  480. * to make it operational.
  481. */
  482. pm_runtime_resume(dev);
  483. __pm_runtime_disable(dev, false);
  484. ret = pm_generic_prepare(dev);
  485. if (ret) {
  486. mutex_lock(&genpd->lock);
  487. if (--genpd->prepared_count == 0)
  488. genpd->suspend_power_off = false;
  489. mutex_unlock(&genpd->lock);
  490. pm_runtime_enable(dev);
  491. }
  492. pm_runtime_put_sync(dev);
  493. return ret;
  494. }
  495. /**
  496. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  497. * @dev: Device to suspend.
  498. *
  499. * Suspend a device under the assumption that its pm_domain field points to the
  500. * domain member of an object of type struct generic_pm_domain representing
  501. * a PM domain consisting of I/O devices.
  502. */
  503. static int pm_genpd_suspend(struct device *dev)
  504. {
  505. struct generic_pm_domain *genpd;
  506. dev_dbg(dev, "%s()\n", __func__);
  507. genpd = dev_to_genpd(dev);
  508. if (IS_ERR(genpd))
  509. return -EINVAL;
  510. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  511. }
  512. /**
  513. * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
  514. * @dev: Device to suspend.
  515. *
  516. * Carry out a late suspend of a device under the assumption that its
  517. * pm_domain field points to the domain member of an object of type
  518. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  519. */
  520. static int pm_genpd_suspend_noirq(struct device *dev)
  521. {
  522. struct generic_pm_domain *genpd;
  523. int ret;
  524. dev_dbg(dev, "%s()\n", __func__);
  525. genpd = dev_to_genpd(dev);
  526. if (IS_ERR(genpd))
  527. return -EINVAL;
  528. if (genpd->suspend_power_off)
  529. return 0;
  530. ret = pm_generic_suspend_noirq(dev);
  531. if (ret)
  532. return ret;
  533. if (device_may_wakeup(dev)
  534. && genpd->active_wakeup && genpd->active_wakeup(dev))
  535. return 0;
  536. if (genpd->stop_device)
  537. genpd->stop_device(dev);
  538. /*
  539. * Since all of the "noirq" callbacks are executed sequentially, it is
  540. * guaranteed that this function will never run twice in parallel for
  541. * the same PM domain, so it is not necessary to use locking here.
  542. */
  543. genpd->suspended_count++;
  544. pm_genpd_sync_poweroff(genpd);
  545. return 0;
  546. }
  547. /**
  548. * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
  549. * @dev: Device to resume.
  550. *
  551. * Carry out an early resume of a device under the assumption that its
  552. * pm_domain field points to the domain member of an object of type
  553. * struct generic_pm_domain representing a power domain consisting of I/O
  554. * devices.
  555. */
  556. static int pm_genpd_resume_noirq(struct device *dev)
  557. {
  558. struct generic_pm_domain *genpd;
  559. dev_dbg(dev, "%s()\n", __func__);
  560. genpd = dev_to_genpd(dev);
  561. if (IS_ERR(genpd))
  562. return -EINVAL;
  563. if (genpd->suspend_power_off)
  564. return 0;
  565. /*
  566. * Since all of the "noirq" callbacks are executed sequentially, it is
  567. * guaranteed that this function will never run twice in parallel for
  568. * the same PM domain, so it is not necessary to use locking here.
  569. */
  570. pm_genpd_poweron(genpd);
  571. genpd->suspended_count--;
  572. if (genpd->start_device)
  573. genpd->start_device(dev);
  574. return pm_generic_resume_noirq(dev);
  575. }
  576. /**
  577. * pm_genpd_resume - Resume a device belonging to an I/O power domain.
  578. * @dev: Device to resume.
  579. *
  580. * Resume a device under the assumption that its pm_domain field points to the
  581. * domain member of an object of type struct generic_pm_domain representing
  582. * a power domain consisting of I/O devices.
  583. */
  584. static int pm_genpd_resume(struct device *dev)
  585. {
  586. struct generic_pm_domain *genpd;
  587. dev_dbg(dev, "%s()\n", __func__);
  588. genpd = dev_to_genpd(dev);
  589. if (IS_ERR(genpd))
  590. return -EINVAL;
  591. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  592. }
  593. /**
  594. * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
  595. * @dev: Device to freeze.
  596. *
  597. * Freeze a device under the assumption that its pm_domain field points to the
  598. * domain member of an object of type struct generic_pm_domain representing
  599. * a power domain consisting of I/O devices.
  600. */
  601. static int pm_genpd_freeze(struct device *dev)
  602. {
  603. struct generic_pm_domain *genpd;
  604. dev_dbg(dev, "%s()\n", __func__);
  605. genpd = dev_to_genpd(dev);
  606. if (IS_ERR(genpd))
  607. return -EINVAL;
  608. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  609. }
  610. /**
  611. * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
  612. * @dev: Device to freeze.
  613. *
  614. * Carry out a late freeze of a device under the assumption that its
  615. * pm_domain field points to the domain member of an object of type
  616. * struct generic_pm_domain representing a power domain consisting of I/O
  617. * devices.
  618. */
  619. static int pm_genpd_freeze_noirq(struct device *dev)
  620. {
  621. struct generic_pm_domain *genpd;
  622. int ret;
  623. dev_dbg(dev, "%s()\n", __func__);
  624. genpd = dev_to_genpd(dev);
  625. if (IS_ERR(genpd))
  626. return -EINVAL;
  627. if (genpd->suspend_power_off)
  628. return 0;
  629. ret = pm_generic_freeze_noirq(dev);
  630. if (ret)
  631. return ret;
  632. if (genpd->stop_device)
  633. genpd->stop_device(dev);
  634. return 0;
  635. }
  636. /**
  637. * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
  638. * @dev: Device to thaw.
  639. *
  640. * Carry out an early thaw of a device under the assumption that its
  641. * pm_domain field points to the domain member of an object of type
  642. * struct generic_pm_domain representing a power domain consisting of I/O
  643. * devices.
  644. */
  645. static int pm_genpd_thaw_noirq(struct device *dev)
  646. {
  647. struct generic_pm_domain *genpd;
  648. dev_dbg(dev, "%s()\n", __func__);
  649. genpd = dev_to_genpd(dev);
  650. if (IS_ERR(genpd))
  651. return -EINVAL;
  652. if (genpd->suspend_power_off)
  653. return 0;
  654. if (genpd->start_device)
  655. genpd->start_device(dev);
  656. return pm_generic_thaw_noirq(dev);
  657. }
  658. /**
  659. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  660. * @dev: Device to thaw.
  661. *
  662. * Thaw a device under the assumption that its pm_domain field points to the
  663. * domain member of an object of type struct generic_pm_domain representing
  664. * a power domain consisting of I/O devices.
  665. */
  666. static int pm_genpd_thaw(struct device *dev)
  667. {
  668. struct generic_pm_domain *genpd;
  669. dev_dbg(dev, "%s()\n", __func__);
  670. genpd = dev_to_genpd(dev);
  671. if (IS_ERR(genpd))
  672. return -EINVAL;
  673. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  674. }
  675. /**
  676. * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
  677. * @dev: Device to suspend.
  678. *
  679. * Power off a device under the assumption that its pm_domain field points to
  680. * the domain member of an object of type struct generic_pm_domain representing
  681. * a PM domain consisting of I/O devices.
  682. */
  683. static int pm_genpd_dev_poweroff(struct device *dev)
  684. {
  685. struct generic_pm_domain *genpd;
  686. dev_dbg(dev, "%s()\n", __func__);
  687. genpd = dev_to_genpd(dev);
  688. if (IS_ERR(genpd))
  689. return -EINVAL;
  690. return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
  691. }
  692. /**
  693. * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
  694. * @dev: Device to suspend.
  695. *
  696. * Carry out a late powering off of a device under the assumption that its
  697. * pm_domain field points to the domain member of an object of type
  698. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  699. */
  700. static int pm_genpd_dev_poweroff_noirq(struct device *dev)
  701. {
  702. struct generic_pm_domain *genpd;
  703. int ret;
  704. dev_dbg(dev, "%s()\n", __func__);
  705. genpd = dev_to_genpd(dev);
  706. if (IS_ERR(genpd))
  707. return -EINVAL;
  708. if (genpd->suspend_power_off)
  709. return 0;
  710. ret = pm_generic_poweroff_noirq(dev);
  711. if (ret)
  712. return ret;
  713. if (device_may_wakeup(dev)
  714. && genpd->active_wakeup && genpd->active_wakeup(dev))
  715. return 0;
  716. if (genpd->stop_device)
  717. genpd->stop_device(dev);
  718. /*
  719. * Since all of the "noirq" callbacks are executed sequentially, it is
  720. * guaranteed that this function will never run twice in parallel for
  721. * the same PM domain, so it is not necessary to use locking here.
  722. */
  723. genpd->suspended_count++;
  724. pm_genpd_sync_poweroff(genpd);
  725. return 0;
  726. }
  727. /**
  728. * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
  729. * @dev: Device to resume.
  730. *
  731. * Carry out an early restore of a device under the assumption that its
  732. * pm_domain field points to the domain member of an object of type
  733. * struct generic_pm_domain representing a power domain consisting of I/O
  734. * devices.
  735. */
  736. static int pm_genpd_restore_noirq(struct device *dev)
  737. {
  738. struct generic_pm_domain *genpd;
  739. dev_dbg(dev, "%s()\n", __func__);
  740. genpd = dev_to_genpd(dev);
  741. if (IS_ERR(genpd))
  742. return -EINVAL;
  743. /*
  744. * Since all of the "noirq" callbacks are executed sequentially, it is
  745. * guaranteed that this function will never run twice in parallel for
  746. * the same PM domain, so it is not necessary to use locking here.
  747. */
  748. genpd->status = GPD_STATE_POWER_OFF;
  749. if (genpd->suspend_power_off) {
  750. /*
  751. * The boot kernel might put the domain into the power on state,
  752. * so make sure it really is powered off.
  753. */
  754. if (genpd->power_off)
  755. genpd->power_off(genpd);
  756. return 0;
  757. }
  758. pm_genpd_poweron(genpd);
  759. genpd->suspended_count--;
  760. if (genpd->start_device)
  761. genpd->start_device(dev);
  762. return pm_generic_restore_noirq(dev);
  763. }
  764. /**
  765. * pm_genpd_restore - Restore a device belonging to an I/O power domain.
  766. * @dev: Device to resume.
  767. *
  768. * Restore a device under the assumption that its pm_domain field points to the
  769. * domain member of an object of type struct generic_pm_domain representing
  770. * a power domain consisting of I/O devices.
  771. */
  772. static int pm_genpd_restore(struct device *dev)
  773. {
  774. struct generic_pm_domain *genpd;
  775. dev_dbg(dev, "%s()\n", __func__);
  776. genpd = dev_to_genpd(dev);
  777. if (IS_ERR(genpd))
  778. return -EINVAL;
  779. return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
  780. }
  781. /**
  782. * pm_genpd_complete - Complete power transition of a device in a power domain.
  783. * @dev: Device to complete the transition of.
  784. *
  785. * Complete a power transition of a device (during a system-wide power
  786. * transition) under the assumption that its pm_domain field points to the
  787. * domain member of an object of type struct generic_pm_domain representing
  788. * a power domain consisting of I/O devices.
  789. */
  790. static void pm_genpd_complete(struct device *dev)
  791. {
  792. struct generic_pm_domain *genpd;
  793. bool run_complete;
  794. dev_dbg(dev, "%s()\n", __func__);
  795. genpd = dev_to_genpd(dev);
  796. if (IS_ERR(genpd))
  797. return;
  798. mutex_lock(&genpd->lock);
  799. run_complete = !genpd->suspend_power_off;
  800. if (--genpd->prepared_count == 0)
  801. genpd->suspend_power_off = false;
  802. mutex_unlock(&genpd->lock);
  803. if (run_complete) {
  804. pm_generic_complete(dev);
  805. pm_runtime_set_active(dev);
  806. pm_runtime_enable(dev);
  807. pm_runtime_idle(dev);
  808. }
  809. }
  810. #else
  811. #define pm_genpd_prepare NULL
  812. #define pm_genpd_suspend NULL
  813. #define pm_genpd_suspend_noirq NULL
  814. #define pm_genpd_resume_noirq NULL
  815. #define pm_genpd_resume NULL
  816. #define pm_genpd_freeze NULL
  817. #define pm_genpd_freeze_noirq NULL
  818. #define pm_genpd_thaw_noirq NULL
  819. #define pm_genpd_thaw NULL
  820. #define pm_genpd_dev_poweroff_noirq NULL
  821. #define pm_genpd_dev_poweroff NULL
  822. #define pm_genpd_restore_noirq NULL
  823. #define pm_genpd_restore NULL
  824. #define pm_genpd_complete NULL
  825. #endif /* CONFIG_PM_SLEEP */
  826. /**
  827. * pm_genpd_add_device - Add a device to an I/O PM domain.
  828. * @genpd: PM domain to add the device to.
  829. * @dev: Device to be added.
  830. */
  831. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
  832. {
  833. struct dev_list_entry *dle;
  834. int ret = 0;
  835. dev_dbg(dev, "%s()\n", __func__);
  836. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  837. return -EINVAL;
  838. genpd_acquire_lock(genpd);
  839. if (genpd->status == GPD_STATE_POWER_OFF) {
  840. ret = -EINVAL;
  841. goto out;
  842. }
  843. if (genpd->prepared_count > 0) {
  844. ret = -EAGAIN;
  845. goto out;
  846. }
  847. list_for_each_entry(dle, &genpd->dev_list, node)
  848. if (dle->dev == dev) {
  849. ret = -EINVAL;
  850. goto out;
  851. }
  852. dle = kzalloc(sizeof(*dle), GFP_KERNEL);
  853. if (!dle) {
  854. ret = -ENOMEM;
  855. goto out;
  856. }
  857. dle->dev = dev;
  858. dle->need_restore = false;
  859. list_add_tail(&dle->node, &genpd->dev_list);
  860. genpd->device_count++;
  861. spin_lock_irq(&dev->power.lock);
  862. dev->pm_domain = &genpd->domain;
  863. spin_unlock_irq(&dev->power.lock);
  864. out:
  865. genpd_release_lock(genpd);
  866. return ret;
  867. }
  868. /**
  869. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  870. * @genpd: PM domain to remove the device from.
  871. * @dev: Device to be removed.
  872. */
  873. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  874. struct device *dev)
  875. {
  876. struct dev_list_entry *dle;
  877. int ret = -EINVAL;
  878. dev_dbg(dev, "%s()\n", __func__);
  879. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  880. return -EINVAL;
  881. genpd_acquire_lock(genpd);
  882. if (genpd->prepared_count > 0) {
  883. ret = -EAGAIN;
  884. goto out;
  885. }
  886. list_for_each_entry(dle, &genpd->dev_list, node) {
  887. if (dle->dev != dev)
  888. continue;
  889. spin_lock_irq(&dev->power.lock);
  890. dev->pm_domain = NULL;
  891. spin_unlock_irq(&dev->power.lock);
  892. genpd->device_count--;
  893. list_del(&dle->node);
  894. kfree(dle);
  895. ret = 0;
  896. break;
  897. }
  898. out:
  899. genpd_release_lock(genpd);
  900. return ret;
  901. }
  902. /**
  903. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  904. * @genpd: Master PM domain to add the subdomain to.
  905. * @new_subdomain: Subdomain to be added.
  906. */
  907. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  908. struct generic_pm_domain *new_subdomain)
  909. {
  910. struct generic_pm_domain *subdomain;
  911. int ret = 0;
  912. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
  913. return -EINVAL;
  914. start:
  915. genpd_acquire_lock(genpd);
  916. mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
  917. if (new_subdomain->status != GPD_STATE_POWER_OFF
  918. && new_subdomain->status != GPD_STATE_ACTIVE) {
  919. mutex_unlock(&new_subdomain->lock);
  920. genpd_release_lock(genpd);
  921. goto start;
  922. }
  923. if (genpd->status == GPD_STATE_POWER_OFF
  924. && new_subdomain->status != GPD_STATE_POWER_OFF) {
  925. ret = -EINVAL;
  926. goto out;
  927. }
  928. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  929. if (subdomain == new_subdomain) {
  930. ret = -EINVAL;
  931. goto out;
  932. }
  933. }
  934. list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
  935. new_subdomain->parent = genpd;
  936. if (subdomain->status != GPD_STATE_POWER_OFF)
  937. genpd->sd_count++;
  938. out:
  939. mutex_unlock(&new_subdomain->lock);
  940. genpd_release_lock(genpd);
  941. return ret;
  942. }
  943. /**
  944. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  945. * @genpd: Master PM domain to remove the subdomain from.
  946. * @target: Subdomain to be removed.
  947. */
  948. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  949. struct generic_pm_domain *target)
  950. {
  951. struct generic_pm_domain *subdomain;
  952. int ret = -EINVAL;
  953. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
  954. return -EINVAL;
  955. start:
  956. genpd_acquire_lock(genpd);
  957. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  958. if (subdomain != target)
  959. continue;
  960. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  961. if (subdomain->status != GPD_STATE_POWER_OFF
  962. && subdomain->status != GPD_STATE_ACTIVE) {
  963. mutex_unlock(&subdomain->lock);
  964. genpd_release_lock(genpd);
  965. goto start;
  966. }
  967. list_del(&subdomain->sd_node);
  968. subdomain->parent = NULL;
  969. if (subdomain->status != GPD_STATE_POWER_OFF)
  970. genpd_sd_counter_dec(genpd);
  971. mutex_unlock(&subdomain->lock);
  972. ret = 0;
  973. break;
  974. }
  975. genpd_release_lock(genpd);
  976. return ret;
  977. }
  978. /**
  979. * pm_genpd_init - Initialize a generic I/O PM domain object.
  980. * @genpd: PM domain object to initialize.
  981. * @gov: PM domain governor to associate with the domain (may be NULL).
  982. * @is_off: Initial value of the domain's power_is_off field.
  983. */
  984. void pm_genpd_init(struct generic_pm_domain *genpd,
  985. struct dev_power_governor *gov, bool is_off)
  986. {
  987. if (IS_ERR_OR_NULL(genpd))
  988. return;
  989. INIT_LIST_HEAD(&genpd->sd_node);
  990. genpd->parent = NULL;
  991. INIT_LIST_HEAD(&genpd->dev_list);
  992. INIT_LIST_HEAD(&genpd->sd_list);
  993. mutex_init(&genpd->lock);
  994. genpd->gov = gov;
  995. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  996. genpd->in_progress = 0;
  997. genpd->sd_count = 0;
  998. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  999. init_waitqueue_head(&genpd->status_wait_queue);
  1000. genpd->poweroff_task = NULL;
  1001. genpd->resume_count = 0;
  1002. genpd->device_count = 0;
  1003. genpd->suspended_count = 0;
  1004. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  1005. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  1006. genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
  1007. genpd->domain.ops.prepare = pm_genpd_prepare;
  1008. genpd->domain.ops.suspend = pm_genpd_suspend;
  1009. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  1010. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  1011. genpd->domain.ops.resume = pm_genpd_resume;
  1012. genpd->domain.ops.freeze = pm_genpd_freeze;
  1013. genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
  1014. genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
  1015. genpd->domain.ops.thaw = pm_genpd_thaw;
  1016. genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
  1017. genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
  1018. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  1019. genpd->domain.ops.restore = pm_genpd_restore;
  1020. genpd->domain.ops.complete = pm_genpd_complete;
  1021. mutex_lock(&gpd_list_lock);
  1022. list_add(&genpd->gpd_list_node, &gpd_list);
  1023. mutex_unlock(&gpd_list_lock);
  1024. }
  1025. /**
  1026. * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
  1027. */
  1028. void pm_genpd_poweroff_unused(void)
  1029. {
  1030. struct generic_pm_domain *genpd;
  1031. mutex_lock(&gpd_list_lock);
  1032. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  1033. genpd_queue_power_off_work(genpd);
  1034. mutex_unlock(&gpd_list_lock);
  1035. }