domain.c 31 KB

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