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