domain.c 31 KB

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