domain.c 31 KB

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