domain.c 33 KB

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