domain.c 33 KB

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