domain.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. * resume_needed - Check whether to resume a device before system suspend.
  394. * @dev: Device to check.
  395. * @genpd: PM domain the device belongs to.
  396. *
  397. * There are two cases in which a device that can wake up the system from sleep
  398. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  399. * to wake up the system and it has to remain active for this purpose while the
  400. * system is in the sleep state and (2) if the device is not enabled to wake up
  401. * the system from sleep states and it generally doesn't generate wakeup signals
  402. * by itself (those signals are generated on its behalf by other parts of the
  403. * system). In the latter case it may be necessary to reconfigure the device's
  404. * wakeup settings during system suspend, because it may have been set up to
  405. * signal remote wakeup from the system's working state as needed by runtime PM.
  406. * Return 'true' in either of the above cases.
  407. */
  408. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  409. {
  410. bool active_wakeup;
  411. if (!device_can_wakeup(dev))
  412. return false;
  413. active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
  414. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  415. }
  416. /**
  417. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  418. * @dev: Device to start the transition of.
  419. *
  420. * Start a power transition of a device (during a system-wide power transition)
  421. * under the assumption that its pm_domain field points to the domain member of
  422. * an object of type struct generic_pm_domain representing a PM domain
  423. * consisting of I/O devices.
  424. */
  425. static int pm_genpd_prepare(struct device *dev)
  426. {
  427. struct generic_pm_domain *genpd;
  428. int ret;
  429. dev_dbg(dev, "%s()\n", __func__);
  430. genpd = dev_to_genpd(dev);
  431. if (IS_ERR(genpd))
  432. return -EINVAL;
  433. /*
  434. * If a wakeup request is pending for the device, it should be woken up
  435. * at this point and a system wakeup event should be reported if it's
  436. * set up to wake up the system from sleep states.
  437. */
  438. pm_runtime_get_noresume(dev);
  439. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  440. pm_wakeup_event(dev, 0);
  441. if (pm_wakeup_pending()) {
  442. pm_runtime_put_sync(dev);
  443. return -EBUSY;
  444. }
  445. if (resume_needed(dev, genpd))
  446. pm_runtime_resume(dev);
  447. genpd_acquire_lock(genpd);
  448. if (genpd->prepared_count++ == 0)
  449. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  450. genpd_release_lock(genpd);
  451. if (genpd->suspend_power_off) {
  452. pm_runtime_put_noidle(dev);
  453. return 0;
  454. }
  455. /*
  456. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  457. * so pm_genpd_poweron() will return immediately, but if the device
  458. * is suspended (e.g. it's been stopped by .stop_device()), we need
  459. * to make it operational.
  460. */
  461. pm_runtime_resume(dev);
  462. __pm_runtime_disable(dev, false);
  463. ret = pm_generic_prepare(dev);
  464. if (ret) {
  465. mutex_lock(&genpd->lock);
  466. if (--genpd->prepared_count == 0)
  467. genpd->suspend_power_off = false;
  468. mutex_unlock(&genpd->lock);
  469. pm_runtime_enable(dev);
  470. }
  471. pm_runtime_put_sync(dev);
  472. return ret;
  473. }
  474. /**
  475. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  476. * @dev: Device to suspend.
  477. *
  478. * Suspend a device under the assumption that its pm_domain field points to the
  479. * domain member of an object of type struct generic_pm_domain representing
  480. * a PM domain consisting of I/O devices.
  481. */
  482. static int pm_genpd_suspend(struct device *dev)
  483. {
  484. struct generic_pm_domain *genpd;
  485. dev_dbg(dev, "%s()\n", __func__);
  486. genpd = dev_to_genpd(dev);
  487. if (IS_ERR(genpd))
  488. return -EINVAL;
  489. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  490. }
  491. /**
  492. * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
  493. * @dev: Device to suspend.
  494. *
  495. * Carry out a late suspend of a device under the assumption that its
  496. * pm_domain field points to the domain member of an object of type
  497. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  498. */
  499. static int pm_genpd_suspend_noirq(struct device *dev)
  500. {
  501. struct generic_pm_domain *genpd;
  502. int ret;
  503. dev_dbg(dev, "%s()\n", __func__);
  504. genpd = dev_to_genpd(dev);
  505. if (IS_ERR(genpd))
  506. return -EINVAL;
  507. if (genpd->suspend_power_off)
  508. return 0;
  509. ret = pm_generic_suspend_noirq(dev);
  510. if (ret)
  511. return ret;
  512. if (device_may_wakeup(dev)
  513. && genpd->active_wakeup && genpd->active_wakeup(dev))
  514. return 0;
  515. if (genpd->stop_device)
  516. genpd->stop_device(dev);
  517. /*
  518. * Since all of the "noirq" callbacks are executed sequentially, it is
  519. * guaranteed that this function will never run twice in parallel for
  520. * the same PM domain, so it is not necessary to use locking here.
  521. */
  522. genpd->suspended_count++;
  523. pm_genpd_sync_poweroff(genpd);
  524. return 0;
  525. }
  526. /**
  527. * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
  528. * @dev: Device to resume.
  529. *
  530. * Carry out an early resume of a device under the assumption that its
  531. * pm_domain field points to the domain member of an object of type
  532. * struct generic_pm_domain representing a power domain consisting of I/O
  533. * devices.
  534. */
  535. static int pm_genpd_resume_noirq(struct device *dev)
  536. {
  537. struct generic_pm_domain *genpd;
  538. dev_dbg(dev, "%s()\n", __func__);
  539. genpd = dev_to_genpd(dev);
  540. if (IS_ERR(genpd))
  541. return -EINVAL;
  542. if (genpd->suspend_power_off)
  543. return 0;
  544. /*
  545. * Since all of the "noirq" callbacks are executed sequentially, it is
  546. * guaranteed that this function will never run twice in parallel for
  547. * the same PM domain, so it is not necessary to use locking here.
  548. */
  549. pm_genpd_poweron(genpd);
  550. genpd->suspended_count--;
  551. if (genpd->start_device)
  552. genpd->start_device(dev);
  553. return pm_generic_resume_noirq(dev);
  554. }
  555. /**
  556. * pm_genpd_resume - Resume a device belonging to an I/O power domain.
  557. * @dev: Device to resume.
  558. *
  559. * Resume a device under the assumption that its pm_domain field points to the
  560. * domain member of an object of type struct generic_pm_domain representing
  561. * a power domain consisting of I/O devices.
  562. */
  563. static int pm_genpd_resume(struct device *dev)
  564. {
  565. struct generic_pm_domain *genpd;
  566. dev_dbg(dev, "%s()\n", __func__);
  567. genpd = dev_to_genpd(dev);
  568. if (IS_ERR(genpd))
  569. return -EINVAL;
  570. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  571. }
  572. /**
  573. * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
  574. * @dev: Device to freeze.
  575. *
  576. * Freeze a device under the assumption that its pm_domain field points to the
  577. * domain member of an object of type struct generic_pm_domain representing
  578. * a power domain consisting of I/O devices.
  579. */
  580. static int pm_genpd_freeze(struct device *dev)
  581. {
  582. struct generic_pm_domain *genpd;
  583. dev_dbg(dev, "%s()\n", __func__);
  584. genpd = dev_to_genpd(dev);
  585. if (IS_ERR(genpd))
  586. return -EINVAL;
  587. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  588. }
  589. /**
  590. * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
  591. * @dev: Device to freeze.
  592. *
  593. * Carry out a late freeze 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_freeze_noirq(struct device *dev)
  599. {
  600. struct generic_pm_domain *genpd;
  601. int ret;
  602. dev_dbg(dev, "%s()\n", __func__);
  603. genpd = dev_to_genpd(dev);
  604. if (IS_ERR(genpd))
  605. return -EINVAL;
  606. if (genpd->suspend_power_off)
  607. return 0;
  608. ret = pm_generic_freeze_noirq(dev);
  609. if (ret)
  610. return ret;
  611. if (genpd->stop_device)
  612. genpd->stop_device(dev);
  613. return 0;
  614. }
  615. /**
  616. * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
  617. * @dev: Device to thaw.
  618. *
  619. * Carry out an early thaw of a device under the assumption that its
  620. * pm_domain field points to the domain member of an object of type
  621. * struct generic_pm_domain representing a power domain consisting of I/O
  622. * devices.
  623. */
  624. static int pm_genpd_thaw_noirq(struct device *dev)
  625. {
  626. struct generic_pm_domain *genpd;
  627. dev_dbg(dev, "%s()\n", __func__);
  628. genpd = dev_to_genpd(dev);
  629. if (IS_ERR(genpd))
  630. return -EINVAL;
  631. if (genpd->suspend_power_off)
  632. return 0;
  633. if (genpd->start_device)
  634. genpd->start_device(dev);
  635. return pm_generic_thaw_noirq(dev);
  636. }
  637. /**
  638. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  639. * @dev: Device to thaw.
  640. *
  641. * Thaw a device under the assumption that its pm_domain field points to the
  642. * domain member of an object of type struct generic_pm_domain representing
  643. * a power domain consisting of I/O devices.
  644. */
  645. static int pm_genpd_thaw(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. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  653. }
  654. /**
  655. * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
  656. * @dev: Device to suspend.
  657. *
  658. * Power off a device under the assumption that its pm_domain field points to
  659. * the domain member of an object of type struct generic_pm_domain representing
  660. * a PM domain consisting of I/O devices.
  661. */
  662. static int pm_genpd_dev_poweroff(struct device *dev)
  663. {
  664. struct generic_pm_domain *genpd;
  665. dev_dbg(dev, "%s()\n", __func__);
  666. genpd = dev_to_genpd(dev);
  667. if (IS_ERR(genpd))
  668. return -EINVAL;
  669. return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
  670. }
  671. /**
  672. * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
  673. * @dev: Device to suspend.
  674. *
  675. * Carry out a late powering off of a device under the assumption that its
  676. * pm_domain field points to the domain member of an object of type
  677. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  678. */
  679. static int pm_genpd_dev_poweroff_noirq(struct device *dev)
  680. {
  681. struct generic_pm_domain *genpd;
  682. int ret;
  683. dev_dbg(dev, "%s()\n", __func__);
  684. genpd = dev_to_genpd(dev);
  685. if (IS_ERR(genpd))
  686. return -EINVAL;
  687. if (genpd->suspend_power_off)
  688. return 0;
  689. ret = pm_generic_poweroff_noirq(dev);
  690. if (ret)
  691. return ret;
  692. if (device_may_wakeup(dev)
  693. && genpd->active_wakeup && genpd->active_wakeup(dev))
  694. return 0;
  695. if (genpd->stop_device)
  696. genpd->stop_device(dev);
  697. /*
  698. * Since all of the "noirq" callbacks are executed sequentially, it is
  699. * guaranteed that this function will never run twice in parallel for
  700. * the same PM domain, so it is not necessary to use locking here.
  701. */
  702. genpd->suspended_count++;
  703. pm_genpd_sync_poweroff(genpd);
  704. return 0;
  705. }
  706. /**
  707. * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
  708. * @dev: Device to resume.
  709. *
  710. * Carry out an early restore of a device under the assumption that its
  711. * pm_domain field points to the domain member of an object of type
  712. * struct generic_pm_domain representing a power domain consisting of I/O
  713. * devices.
  714. */
  715. static int pm_genpd_restore_noirq(struct device *dev)
  716. {
  717. struct generic_pm_domain *genpd;
  718. dev_dbg(dev, "%s()\n", __func__);
  719. genpd = dev_to_genpd(dev);
  720. if (IS_ERR(genpd))
  721. return -EINVAL;
  722. /*
  723. * Since all of the "noirq" callbacks are executed sequentially, it is
  724. * guaranteed that this function will never run twice in parallel for
  725. * the same PM domain, so it is not necessary to use locking here.
  726. */
  727. genpd->status = GPD_STATE_POWER_OFF;
  728. if (genpd->suspend_power_off) {
  729. /*
  730. * The boot kernel might put the domain into the power on state,
  731. * so make sure it really is powered off.
  732. */
  733. if (genpd->power_off)
  734. genpd->power_off(genpd);
  735. return 0;
  736. }
  737. pm_genpd_poweron(genpd);
  738. genpd->suspended_count--;
  739. if (genpd->start_device)
  740. genpd->start_device(dev);
  741. return pm_generic_restore_noirq(dev);
  742. }
  743. /**
  744. * pm_genpd_restore - Restore a device belonging to an I/O power domain.
  745. * @dev: Device to resume.
  746. *
  747. * Restore a device under the assumption that its pm_domain field points to the
  748. * domain member of an object of type struct generic_pm_domain representing
  749. * a power domain consisting of I/O devices.
  750. */
  751. static int pm_genpd_restore(struct device *dev)
  752. {
  753. struct generic_pm_domain *genpd;
  754. dev_dbg(dev, "%s()\n", __func__);
  755. genpd = dev_to_genpd(dev);
  756. if (IS_ERR(genpd))
  757. return -EINVAL;
  758. return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
  759. }
  760. /**
  761. * pm_genpd_complete - Complete power transition of a device in a power domain.
  762. * @dev: Device to complete the transition of.
  763. *
  764. * Complete a power transition of a device (during a system-wide power
  765. * transition) under the assumption that its pm_domain field points to the
  766. * domain member of an object of type struct generic_pm_domain representing
  767. * a power domain consisting of I/O devices.
  768. */
  769. static void pm_genpd_complete(struct device *dev)
  770. {
  771. struct generic_pm_domain *genpd;
  772. bool run_complete;
  773. dev_dbg(dev, "%s()\n", __func__);
  774. genpd = dev_to_genpd(dev);
  775. if (IS_ERR(genpd))
  776. return;
  777. mutex_lock(&genpd->lock);
  778. run_complete = !genpd->suspend_power_off;
  779. if (--genpd->prepared_count == 0)
  780. genpd->suspend_power_off = false;
  781. mutex_unlock(&genpd->lock);
  782. if (run_complete) {
  783. pm_generic_complete(dev);
  784. pm_runtime_set_active(dev);
  785. pm_runtime_enable(dev);
  786. pm_runtime_idle(dev);
  787. }
  788. }
  789. #else
  790. #define pm_genpd_prepare NULL
  791. #define pm_genpd_suspend NULL
  792. #define pm_genpd_suspend_noirq NULL
  793. #define pm_genpd_resume_noirq NULL
  794. #define pm_genpd_resume NULL
  795. #define pm_genpd_freeze NULL
  796. #define pm_genpd_freeze_noirq NULL
  797. #define pm_genpd_thaw_noirq NULL
  798. #define pm_genpd_thaw NULL
  799. #define pm_genpd_dev_poweroff_noirq NULL
  800. #define pm_genpd_dev_poweroff NULL
  801. #define pm_genpd_restore_noirq NULL
  802. #define pm_genpd_restore NULL
  803. #define pm_genpd_complete NULL
  804. #endif /* CONFIG_PM_SLEEP */
  805. /**
  806. * pm_genpd_add_device - Add a device to an I/O PM domain.
  807. * @genpd: PM domain to add the device to.
  808. * @dev: Device to be added.
  809. */
  810. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
  811. {
  812. struct dev_list_entry *dle;
  813. int ret = 0;
  814. dev_dbg(dev, "%s()\n", __func__);
  815. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  816. return -EINVAL;
  817. genpd_acquire_lock(genpd);
  818. if (genpd->status == GPD_STATE_POWER_OFF) {
  819. ret = -EINVAL;
  820. goto out;
  821. }
  822. if (genpd->prepared_count > 0) {
  823. ret = -EAGAIN;
  824. goto out;
  825. }
  826. list_for_each_entry(dle, &genpd->dev_list, node)
  827. if (dle->dev == dev) {
  828. ret = -EINVAL;
  829. goto out;
  830. }
  831. dle = kzalloc(sizeof(*dle), GFP_KERNEL);
  832. if (!dle) {
  833. ret = -ENOMEM;
  834. goto out;
  835. }
  836. dle->dev = dev;
  837. dle->need_restore = false;
  838. list_add_tail(&dle->node, &genpd->dev_list);
  839. genpd->device_count++;
  840. spin_lock_irq(&dev->power.lock);
  841. dev->pm_domain = &genpd->domain;
  842. spin_unlock_irq(&dev->power.lock);
  843. out:
  844. genpd_release_lock(genpd);
  845. return ret;
  846. }
  847. /**
  848. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  849. * @genpd: PM domain to remove the device from.
  850. * @dev: Device to be removed.
  851. */
  852. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  853. struct device *dev)
  854. {
  855. struct dev_list_entry *dle;
  856. int ret = -EINVAL;
  857. dev_dbg(dev, "%s()\n", __func__);
  858. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  859. return -EINVAL;
  860. genpd_acquire_lock(genpd);
  861. if (genpd->prepared_count > 0) {
  862. ret = -EAGAIN;
  863. goto out;
  864. }
  865. list_for_each_entry(dle, &genpd->dev_list, node) {
  866. if (dle->dev != dev)
  867. continue;
  868. spin_lock_irq(&dev->power.lock);
  869. dev->pm_domain = NULL;
  870. spin_unlock_irq(&dev->power.lock);
  871. genpd->device_count--;
  872. list_del(&dle->node);
  873. kfree(dle);
  874. ret = 0;
  875. break;
  876. }
  877. out:
  878. genpd_release_lock(genpd);
  879. return ret;
  880. }
  881. /**
  882. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  883. * @genpd: Master PM domain to add the subdomain to.
  884. * @new_subdomain: Subdomain to be added.
  885. */
  886. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  887. struct generic_pm_domain *new_subdomain)
  888. {
  889. struct generic_pm_domain *subdomain;
  890. int ret = 0;
  891. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
  892. return -EINVAL;
  893. start:
  894. genpd_acquire_lock(genpd);
  895. mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
  896. if (new_subdomain->status != GPD_STATE_POWER_OFF
  897. && new_subdomain->status != GPD_STATE_ACTIVE) {
  898. mutex_unlock(&new_subdomain->lock);
  899. genpd_release_lock(genpd);
  900. goto start;
  901. }
  902. if (genpd->status == GPD_STATE_POWER_OFF
  903. && new_subdomain->status != GPD_STATE_POWER_OFF) {
  904. ret = -EINVAL;
  905. goto out;
  906. }
  907. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  908. if (subdomain == new_subdomain) {
  909. ret = -EINVAL;
  910. goto out;
  911. }
  912. }
  913. list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
  914. new_subdomain->parent = genpd;
  915. if (subdomain->status != GPD_STATE_POWER_OFF)
  916. genpd->sd_count++;
  917. out:
  918. mutex_unlock(&new_subdomain->lock);
  919. genpd_release_lock(genpd);
  920. return ret;
  921. }
  922. /**
  923. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  924. * @genpd: Master PM domain to remove the subdomain from.
  925. * @target: Subdomain to be removed.
  926. */
  927. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  928. struct generic_pm_domain *target)
  929. {
  930. struct generic_pm_domain *subdomain;
  931. int ret = -EINVAL;
  932. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
  933. return -EINVAL;
  934. start:
  935. genpd_acquire_lock(genpd);
  936. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  937. if (subdomain != target)
  938. continue;
  939. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  940. if (subdomain->status != GPD_STATE_POWER_OFF
  941. && subdomain->status != GPD_STATE_ACTIVE) {
  942. mutex_unlock(&subdomain->lock);
  943. genpd_release_lock(genpd);
  944. goto start;
  945. }
  946. list_del(&subdomain->sd_node);
  947. subdomain->parent = NULL;
  948. if (subdomain->status != GPD_STATE_POWER_OFF)
  949. genpd_sd_counter_dec(genpd);
  950. mutex_unlock(&subdomain->lock);
  951. ret = 0;
  952. break;
  953. }
  954. genpd_release_lock(genpd);
  955. return ret;
  956. }
  957. /**
  958. * pm_genpd_init - Initialize a generic I/O PM domain object.
  959. * @genpd: PM domain object to initialize.
  960. * @gov: PM domain governor to associate with the domain (may be NULL).
  961. * @is_off: Initial value of the domain's power_is_off field.
  962. */
  963. void pm_genpd_init(struct generic_pm_domain *genpd,
  964. struct dev_power_governor *gov, bool is_off)
  965. {
  966. if (IS_ERR_OR_NULL(genpd))
  967. return;
  968. INIT_LIST_HEAD(&genpd->sd_node);
  969. genpd->parent = NULL;
  970. INIT_LIST_HEAD(&genpd->dev_list);
  971. INIT_LIST_HEAD(&genpd->sd_list);
  972. mutex_init(&genpd->lock);
  973. genpd->gov = gov;
  974. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  975. genpd->in_progress = 0;
  976. genpd->sd_count = 0;
  977. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  978. init_waitqueue_head(&genpd->status_wait_queue);
  979. genpd->poweroff_task = NULL;
  980. genpd->resume_count = 0;
  981. genpd->device_count = 0;
  982. genpd->suspended_count = 0;
  983. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  984. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  985. genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
  986. genpd->domain.ops.prepare = pm_genpd_prepare;
  987. genpd->domain.ops.suspend = pm_genpd_suspend;
  988. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  989. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  990. genpd->domain.ops.resume = pm_genpd_resume;
  991. genpd->domain.ops.freeze = pm_genpd_freeze;
  992. genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
  993. genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
  994. genpd->domain.ops.thaw = pm_genpd_thaw;
  995. genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
  996. genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
  997. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  998. genpd->domain.ops.restore = pm_genpd_restore;
  999. genpd->domain.ops.complete = pm_genpd_complete;
  1000. }