domain.c 29 KB

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