domain.c 32 KB

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