domain.c 31 KB

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