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. #else
  379. static inline void genpd_power_off_work_fn(struct work_struct *work) {}
  380. static inline void __pm_genpd_runtime_resume(struct device *dev,
  381. struct generic_pm_domain *genpd) {}
  382. #define pm_genpd_runtime_suspend NULL
  383. #define pm_genpd_runtime_resume NULL
  384. #endif /* CONFIG_PM_RUNTIME */
  385. #ifdef CONFIG_PM_SLEEP
  386. /**
  387. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
  388. * @genpd: PM domain to power off, if possible.
  389. *
  390. * Check if the given PM domain can be powered off (during system suspend or
  391. * hibernation) and do that if so. Also, in that case propagate to its parent.
  392. *
  393. * This function is only called in "noirq" stages of system power transitions,
  394. * so it need not acquire locks (all of the "noirq" callbacks are executed
  395. * sequentially, so it is guaranteed that it will never run twice in parallel).
  396. */
  397. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
  398. {
  399. struct generic_pm_domain *parent = genpd->parent;
  400. if (genpd->status == GPD_STATE_POWER_OFF)
  401. return;
  402. if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
  403. return;
  404. if (genpd->power_off)
  405. genpd->power_off(genpd);
  406. genpd->status = GPD_STATE_POWER_OFF;
  407. if (parent) {
  408. genpd_sd_counter_dec(parent);
  409. pm_genpd_sync_poweroff(parent);
  410. }
  411. }
  412. /**
  413. * resume_needed - Check whether to resume a device before system suspend.
  414. * @dev: Device to check.
  415. * @genpd: PM domain the device belongs to.
  416. *
  417. * There are two cases in which a device that can wake up the system from sleep
  418. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  419. * to wake up the system and it has to remain active for this purpose while the
  420. * system is in the sleep state and (2) if the device is not enabled to wake up
  421. * the system from sleep states and it generally doesn't generate wakeup signals
  422. * by itself (those signals are generated on its behalf by other parts of the
  423. * system). In the latter case it may be necessary to reconfigure the device's
  424. * wakeup settings during system suspend, because it may have been set up to
  425. * signal remote wakeup from the system's working state as needed by runtime PM.
  426. * Return 'true' in either of the above cases.
  427. */
  428. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  429. {
  430. bool active_wakeup;
  431. if (!device_can_wakeup(dev))
  432. return false;
  433. active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
  434. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  435. }
  436. /**
  437. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  438. * @dev: Device to start the transition of.
  439. *
  440. * Start a power transition of a device (during a system-wide power transition)
  441. * under the assumption that its pm_domain field points to the domain member of
  442. * an object of type struct generic_pm_domain representing a PM domain
  443. * consisting of I/O devices.
  444. */
  445. static int pm_genpd_prepare(struct device *dev)
  446. {
  447. struct generic_pm_domain *genpd;
  448. int ret;
  449. dev_dbg(dev, "%s()\n", __func__);
  450. genpd = dev_to_genpd(dev);
  451. if (IS_ERR(genpd))
  452. return -EINVAL;
  453. /*
  454. * If a wakeup request is pending for the device, it should be woken up
  455. * at this point and a system wakeup event should be reported if it's
  456. * set up to wake up the system from sleep states.
  457. */
  458. pm_runtime_get_noresume(dev);
  459. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  460. pm_wakeup_event(dev, 0);
  461. if (pm_wakeup_pending()) {
  462. pm_runtime_put_sync(dev);
  463. return -EBUSY;
  464. }
  465. if (resume_needed(dev, genpd))
  466. pm_runtime_resume(dev);
  467. genpd_acquire_lock(genpd);
  468. if (genpd->prepared_count++ == 0)
  469. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  470. genpd_release_lock(genpd);
  471. if (genpd->suspend_power_off) {
  472. pm_runtime_put_noidle(dev);
  473. return 0;
  474. }
  475. /*
  476. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  477. * so pm_genpd_poweron() will return immediately, but if the device
  478. * is suspended (e.g. it's been stopped by .stop_device()), we need
  479. * to make it operational.
  480. */
  481. pm_runtime_resume(dev);
  482. __pm_runtime_disable(dev, false);
  483. ret = pm_generic_prepare(dev);
  484. if (ret) {
  485. mutex_lock(&genpd->lock);
  486. if (--genpd->prepared_count == 0)
  487. genpd->suspend_power_off = false;
  488. mutex_unlock(&genpd->lock);
  489. pm_runtime_enable(dev);
  490. }
  491. pm_runtime_put_sync(dev);
  492. return ret;
  493. }
  494. /**
  495. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  496. * @dev: Device to suspend.
  497. *
  498. * Suspend a device under the assumption that its pm_domain field points to the
  499. * domain member of an object of type struct generic_pm_domain representing
  500. * a PM domain consisting of I/O devices.
  501. */
  502. static int pm_genpd_suspend(struct device *dev)
  503. {
  504. struct generic_pm_domain *genpd;
  505. dev_dbg(dev, "%s()\n", __func__);
  506. genpd = dev_to_genpd(dev);
  507. if (IS_ERR(genpd))
  508. return -EINVAL;
  509. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  510. }
  511. /**
  512. * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
  513. * @dev: Device to suspend.
  514. *
  515. * Carry out a late suspend of a device under the assumption that its
  516. * pm_domain field points to the domain member of an object of type
  517. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  518. */
  519. static int pm_genpd_suspend_noirq(struct device *dev)
  520. {
  521. struct generic_pm_domain *genpd;
  522. int ret;
  523. dev_dbg(dev, "%s()\n", __func__);
  524. genpd = dev_to_genpd(dev);
  525. if (IS_ERR(genpd))
  526. return -EINVAL;
  527. if (genpd->suspend_power_off)
  528. return 0;
  529. ret = pm_generic_suspend_noirq(dev);
  530. if (ret)
  531. return ret;
  532. if (device_may_wakeup(dev)
  533. && genpd->active_wakeup && genpd->active_wakeup(dev))
  534. return 0;
  535. if (genpd->stop_device)
  536. genpd->stop_device(dev);
  537. /*
  538. * Since all of the "noirq" callbacks are executed sequentially, it is
  539. * guaranteed that this function will never run twice in parallel for
  540. * the same PM domain, so it is not necessary to use locking here.
  541. */
  542. genpd->suspended_count++;
  543. pm_genpd_sync_poweroff(genpd);
  544. return 0;
  545. }
  546. /**
  547. * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
  548. * @dev: Device to resume.
  549. *
  550. * Carry out an early resume of a device under the assumption that its
  551. * pm_domain field points to the domain member of an object of type
  552. * struct generic_pm_domain representing a power domain consisting of I/O
  553. * devices.
  554. */
  555. static int pm_genpd_resume_noirq(struct device *dev)
  556. {
  557. struct generic_pm_domain *genpd;
  558. dev_dbg(dev, "%s()\n", __func__);
  559. genpd = dev_to_genpd(dev);
  560. if (IS_ERR(genpd))
  561. return -EINVAL;
  562. if (genpd->suspend_power_off)
  563. return 0;
  564. /*
  565. * Since all of the "noirq" callbacks are executed sequentially, it is
  566. * guaranteed that this function will never run twice in parallel for
  567. * the same PM domain, so it is not necessary to use locking here.
  568. */
  569. pm_genpd_poweron(genpd);
  570. genpd->suspended_count--;
  571. if (genpd->start_device)
  572. genpd->start_device(dev);
  573. return pm_generic_resume_noirq(dev);
  574. }
  575. /**
  576. * pm_genpd_resume - Resume a device belonging to an I/O power domain.
  577. * @dev: Device to resume.
  578. *
  579. * Resume a device under the assumption that its pm_domain field points to the
  580. * domain member of an object of type struct generic_pm_domain representing
  581. * a power domain consisting of I/O devices.
  582. */
  583. static int pm_genpd_resume(struct device *dev)
  584. {
  585. struct generic_pm_domain *genpd;
  586. dev_dbg(dev, "%s()\n", __func__);
  587. genpd = dev_to_genpd(dev);
  588. if (IS_ERR(genpd))
  589. return -EINVAL;
  590. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  591. }
  592. /**
  593. * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
  594. * @dev: Device to freeze.
  595. *
  596. * Freeze a device under the assumption that its pm_domain field points to the
  597. * domain member of an object of type struct generic_pm_domain representing
  598. * a power domain consisting of I/O devices.
  599. */
  600. static int pm_genpd_freeze(struct device *dev)
  601. {
  602. struct generic_pm_domain *genpd;
  603. dev_dbg(dev, "%s()\n", __func__);
  604. genpd = dev_to_genpd(dev);
  605. if (IS_ERR(genpd))
  606. return -EINVAL;
  607. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  608. }
  609. /**
  610. * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
  611. * @dev: Device to freeze.
  612. *
  613. * Carry out a late freeze of a device under the assumption that its
  614. * pm_domain field points to the domain member of an object of type
  615. * struct generic_pm_domain representing a power domain consisting of I/O
  616. * devices.
  617. */
  618. static int pm_genpd_freeze_noirq(struct device *dev)
  619. {
  620. struct generic_pm_domain *genpd;
  621. int ret;
  622. dev_dbg(dev, "%s()\n", __func__);
  623. genpd = dev_to_genpd(dev);
  624. if (IS_ERR(genpd))
  625. return -EINVAL;
  626. if (genpd->suspend_power_off)
  627. return 0;
  628. ret = pm_generic_freeze_noirq(dev);
  629. if (ret)
  630. return ret;
  631. if (genpd->stop_device)
  632. genpd->stop_device(dev);
  633. return 0;
  634. }
  635. /**
  636. * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
  637. * @dev: Device to thaw.
  638. *
  639. * Carry out an early thaw of a device under the assumption that its
  640. * pm_domain field points to the domain member of an object of type
  641. * struct generic_pm_domain representing a power domain consisting of I/O
  642. * devices.
  643. */
  644. static int pm_genpd_thaw_noirq(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. if (genpd->suspend_power_off)
  652. return 0;
  653. if (genpd->start_device)
  654. genpd->start_device(dev);
  655. return pm_generic_thaw_noirq(dev);
  656. }
  657. /**
  658. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  659. * @dev: Device to thaw.
  660. *
  661. * Thaw a device under the assumption that its pm_domain field points to the
  662. * domain member of an object of type struct generic_pm_domain representing
  663. * a power domain consisting of I/O devices.
  664. */
  665. static int pm_genpd_thaw(struct device *dev)
  666. {
  667. struct generic_pm_domain *genpd;
  668. dev_dbg(dev, "%s()\n", __func__);
  669. genpd = dev_to_genpd(dev);
  670. if (IS_ERR(genpd))
  671. return -EINVAL;
  672. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  673. }
  674. /**
  675. * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
  676. * @dev: Device to suspend.
  677. *
  678. * Power off a device under the assumption that its pm_domain field points to
  679. * the domain member of an object of type struct generic_pm_domain representing
  680. * a PM domain consisting of I/O devices.
  681. */
  682. static int pm_genpd_dev_poweroff(struct device *dev)
  683. {
  684. struct generic_pm_domain *genpd;
  685. dev_dbg(dev, "%s()\n", __func__);
  686. genpd = dev_to_genpd(dev);
  687. if (IS_ERR(genpd))
  688. return -EINVAL;
  689. return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
  690. }
  691. /**
  692. * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
  693. * @dev: Device to suspend.
  694. *
  695. * Carry out a late powering off of a device under the assumption that its
  696. * pm_domain field points to the domain member of an object of type
  697. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  698. */
  699. static int pm_genpd_dev_poweroff_noirq(struct device *dev)
  700. {
  701. struct generic_pm_domain *genpd;
  702. int ret;
  703. dev_dbg(dev, "%s()\n", __func__);
  704. genpd = dev_to_genpd(dev);
  705. if (IS_ERR(genpd))
  706. return -EINVAL;
  707. if (genpd->suspend_power_off)
  708. return 0;
  709. ret = pm_generic_poweroff_noirq(dev);
  710. if (ret)
  711. return ret;
  712. if (device_may_wakeup(dev)
  713. && genpd->active_wakeup && genpd->active_wakeup(dev))
  714. return 0;
  715. if (genpd->stop_device)
  716. genpd->stop_device(dev);
  717. /*
  718. * Since all of the "noirq" callbacks are executed sequentially, it is
  719. * guaranteed that this function will never run twice in parallel for
  720. * the same PM domain, so it is not necessary to use locking here.
  721. */
  722. genpd->suspended_count++;
  723. pm_genpd_sync_poweroff(genpd);
  724. return 0;
  725. }
  726. /**
  727. * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
  728. * @dev: Device to resume.
  729. *
  730. * Carry out an early restore of a device under the assumption that its
  731. * pm_domain field points to the domain member of an object of type
  732. * struct generic_pm_domain representing a power domain consisting of I/O
  733. * devices.
  734. */
  735. static int pm_genpd_restore_noirq(struct device *dev)
  736. {
  737. struct generic_pm_domain *genpd;
  738. dev_dbg(dev, "%s()\n", __func__);
  739. genpd = dev_to_genpd(dev);
  740. if (IS_ERR(genpd))
  741. return -EINVAL;
  742. /*
  743. * Since all of the "noirq" callbacks are executed sequentially, it is
  744. * guaranteed that this function will never run twice in parallel for
  745. * the same PM domain, so it is not necessary to use locking here.
  746. */
  747. genpd->status = GPD_STATE_POWER_OFF;
  748. if (genpd->suspend_power_off) {
  749. /*
  750. * The boot kernel might put the domain into the power on state,
  751. * so make sure it really is powered off.
  752. */
  753. if (genpd->power_off)
  754. genpd->power_off(genpd);
  755. return 0;
  756. }
  757. pm_genpd_poweron(genpd);
  758. genpd->suspended_count--;
  759. if (genpd->start_device)
  760. genpd->start_device(dev);
  761. return pm_generic_restore_noirq(dev);
  762. }
  763. /**
  764. * pm_genpd_restore - Restore a device belonging to an I/O power domain.
  765. * @dev: Device to resume.
  766. *
  767. * Restore a device under the assumption that its pm_domain field points to the
  768. * domain member of an object of type struct generic_pm_domain representing
  769. * a power domain consisting of I/O devices.
  770. */
  771. static int pm_genpd_restore(struct device *dev)
  772. {
  773. struct generic_pm_domain *genpd;
  774. dev_dbg(dev, "%s()\n", __func__);
  775. genpd = dev_to_genpd(dev);
  776. if (IS_ERR(genpd))
  777. return -EINVAL;
  778. return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
  779. }
  780. /**
  781. * pm_genpd_complete - Complete power transition of a device in a power domain.
  782. * @dev: Device to complete the transition of.
  783. *
  784. * Complete a power transition of a device (during a system-wide power
  785. * transition) under the assumption that its pm_domain field points to the
  786. * domain member of an object of type struct generic_pm_domain representing
  787. * a power domain consisting of I/O devices.
  788. */
  789. static void pm_genpd_complete(struct device *dev)
  790. {
  791. struct generic_pm_domain *genpd;
  792. bool run_complete;
  793. dev_dbg(dev, "%s()\n", __func__);
  794. genpd = dev_to_genpd(dev);
  795. if (IS_ERR(genpd))
  796. return;
  797. mutex_lock(&genpd->lock);
  798. run_complete = !genpd->suspend_power_off;
  799. if (--genpd->prepared_count == 0)
  800. genpd->suspend_power_off = false;
  801. mutex_unlock(&genpd->lock);
  802. if (run_complete) {
  803. pm_generic_complete(dev);
  804. pm_runtime_set_active(dev);
  805. pm_runtime_enable(dev);
  806. pm_runtime_idle(dev);
  807. }
  808. }
  809. #else
  810. #define pm_genpd_prepare NULL
  811. #define pm_genpd_suspend NULL
  812. #define pm_genpd_suspend_noirq NULL
  813. #define pm_genpd_resume_noirq NULL
  814. #define pm_genpd_resume NULL
  815. #define pm_genpd_freeze NULL
  816. #define pm_genpd_freeze_noirq NULL
  817. #define pm_genpd_thaw_noirq NULL
  818. #define pm_genpd_thaw NULL
  819. #define pm_genpd_dev_poweroff_noirq NULL
  820. #define pm_genpd_dev_poweroff NULL
  821. #define pm_genpd_restore_noirq NULL
  822. #define pm_genpd_restore NULL
  823. #define pm_genpd_complete NULL
  824. #endif /* CONFIG_PM_SLEEP */
  825. /**
  826. * pm_genpd_add_device - Add a device to an I/O PM domain.
  827. * @genpd: PM domain to add the device to.
  828. * @dev: Device to be added.
  829. */
  830. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
  831. {
  832. struct dev_list_entry *dle;
  833. int ret = 0;
  834. dev_dbg(dev, "%s()\n", __func__);
  835. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  836. return -EINVAL;
  837. genpd_acquire_lock(genpd);
  838. if (genpd->status == GPD_STATE_POWER_OFF) {
  839. ret = -EINVAL;
  840. goto out;
  841. }
  842. if (genpd->prepared_count > 0) {
  843. ret = -EAGAIN;
  844. goto out;
  845. }
  846. list_for_each_entry(dle, &genpd->dev_list, node)
  847. if (dle->dev == dev) {
  848. ret = -EINVAL;
  849. goto out;
  850. }
  851. dle = kzalloc(sizeof(*dle), GFP_KERNEL);
  852. if (!dle) {
  853. ret = -ENOMEM;
  854. goto out;
  855. }
  856. dle->dev = dev;
  857. dle->need_restore = false;
  858. list_add_tail(&dle->node, &genpd->dev_list);
  859. genpd->device_count++;
  860. spin_lock_irq(&dev->power.lock);
  861. dev->pm_domain = &genpd->domain;
  862. spin_unlock_irq(&dev->power.lock);
  863. out:
  864. genpd_release_lock(genpd);
  865. return ret;
  866. }
  867. /**
  868. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  869. * @genpd: PM domain to remove the device from.
  870. * @dev: Device to be removed.
  871. */
  872. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  873. struct device *dev)
  874. {
  875. struct dev_list_entry *dle;
  876. int ret = -EINVAL;
  877. dev_dbg(dev, "%s()\n", __func__);
  878. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  879. return -EINVAL;
  880. genpd_acquire_lock(genpd);
  881. if (genpd->prepared_count > 0) {
  882. ret = -EAGAIN;
  883. goto out;
  884. }
  885. list_for_each_entry(dle, &genpd->dev_list, node) {
  886. if (dle->dev != dev)
  887. continue;
  888. spin_lock_irq(&dev->power.lock);
  889. dev->pm_domain = NULL;
  890. spin_unlock_irq(&dev->power.lock);
  891. genpd->device_count--;
  892. list_del(&dle->node);
  893. kfree(dle);
  894. ret = 0;
  895. break;
  896. }
  897. out:
  898. genpd_release_lock(genpd);
  899. return ret;
  900. }
  901. /**
  902. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  903. * @genpd: Master PM domain to add the subdomain to.
  904. * @new_subdomain: Subdomain to be added.
  905. */
  906. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  907. struct generic_pm_domain *new_subdomain)
  908. {
  909. struct generic_pm_domain *subdomain;
  910. int ret = 0;
  911. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
  912. return -EINVAL;
  913. start:
  914. genpd_acquire_lock(genpd);
  915. mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
  916. if (new_subdomain->status != GPD_STATE_POWER_OFF
  917. && new_subdomain->status != GPD_STATE_ACTIVE) {
  918. mutex_unlock(&new_subdomain->lock);
  919. genpd_release_lock(genpd);
  920. goto start;
  921. }
  922. if (genpd->status == GPD_STATE_POWER_OFF
  923. && new_subdomain->status != GPD_STATE_POWER_OFF) {
  924. ret = -EINVAL;
  925. goto out;
  926. }
  927. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  928. if (subdomain == new_subdomain) {
  929. ret = -EINVAL;
  930. goto out;
  931. }
  932. }
  933. list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
  934. new_subdomain->parent = genpd;
  935. if (subdomain->status != GPD_STATE_POWER_OFF)
  936. genpd->sd_count++;
  937. out:
  938. mutex_unlock(&new_subdomain->lock);
  939. genpd_release_lock(genpd);
  940. return ret;
  941. }
  942. /**
  943. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  944. * @genpd: Master PM domain to remove the subdomain from.
  945. * @target: Subdomain to be removed.
  946. */
  947. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  948. struct generic_pm_domain *target)
  949. {
  950. struct generic_pm_domain *subdomain;
  951. int ret = -EINVAL;
  952. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
  953. return -EINVAL;
  954. start:
  955. genpd_acquire_lock(genpd);
  956. list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
  957. if (subdomain != target)
  958. continue;
  959. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  960. if (subdomain->status != GPD_STATE_POWER_OFF
  961. && subdomain->status != GPD_STATE_ACTIVE) {
  962. mutex_unlock(&subdomain->lock);
  963. genpd_release_lock(genpd);
  964. goto start;
  965. }
  966. list_del(&subdomain->sd_node);
  967. subdomain->parent = NULL;
  968. if (subdomain->status != GPD_STATE_POWER_OFF)
  969. genpd_sd_counter_dec(genpd);
  970. mutex_unlock(&subdomain->lock);
  971. ret = 0;
  972. break;
  973. }
  974. genpd_release_lock(genpd);
  975. return ret;
  976. }
  977. /**
  978. * pm_genpd_init - Initialize a generic I/O PM domain object.
  979. * @genpd: PM domain object to initialize.
  980. * @gov: PM domain governor to associate with the domain (may be NULL).
  981. * @is_off: Initial value of the domain's power_is_off field.
  982. */
  983. void pm_genpd_init(struct generic_pm_domain *genpd,
  984. struct dev_power_governor *gov, bool is_off)
  985. {
  986. if (IS_ERR_OR_NULL(genpd))
  987. return;
  988. INIT_LIST_HEAD(&genpd->sd_node);
  989. genpd->parent = NULL;
  990. INIT_LIST_HEAD(&genpd->dev_list);
  991. INIT_LIST_HEAD(&genpd->sd_list);
  992. mutex_init(&genpd->lock);
  993. genpd->gov = gov;
  994. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  995. genpd->in_progress = 0;
  996. genpd->sd_count = 0;
  997. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  998. init_waitqueue_head(&genpd->status_wait_queue);
  999. genpd->poweroff_task = NULL;
  1000. genpd->resume_count = 0;
  1001. genpd->device_count = 0;
  1002. genpd->suspended_count = 0;
  1003. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  1004. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  1005. genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
  1006. genpd->domain.ops.prepare = pm_genpd_prepare;
  1007. genpd->domain.ops.suspend = pm_genpd_suspend;
  1008. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  1009. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  1010. genpd->domain.ops.resume = pm_genpd_resume;
  1011. genpd->domain.ops.freeze = pm_genpd_freeze;
  1012. genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
  1013. genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
  1014. genpd->domain.ops.thaw = pm_genpd_thaw;
  1015. genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
  1016. genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
  1017. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  1018. genpd->domain.ops.restore = pm_genpd_restore;
  1019. genpd->domain.ops.complete = pm_genpd_complete;
  1020. mutex_lock(&gpd_list_lock);
  1021. list_add(&genpd->gpd_list_node, &gpd_list);
  1022. mutex_unlock(&gpd_list_lock);
  1023. }
  1024. /**
  1025. * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
  1026. */
  1027. void pm_genpd_poweroff_unused(void)
  1028. {
  1029. struct generic_pm_domain *genpd;
  1030. mutex_lock(&gpd_list_lock);
  1031. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  1032. genpd_queue_power_off_work(genpd);
  1033. mutex_unlock(&gpd_list_lock);
  1034. }