domain.c 38 KB

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