domain.c 32 KB

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