domain.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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/delay.h>
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/pm_qos.h>
  15. #include <linux/pm_clock.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/sched.h>
  19. #include <linux/suspend.h>
  20. #include <linux/export.h>
  21. #define GENPD_RETRY_MAX_MS 250 /* Approximate */
  22. #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
  23. ({ \
  24. type (*__routine)(struct device *__d); \
  25. type __ret = (type)0; \
  26. \
  27. __routine = genpd->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. /*
  36. * Get the generic PM domain for a particular struct device.
  37. * This validates the struct device pointer, the PM domain pointer,
  38. * and checks that the PM domain pointer is a real generic PM domain.
  39. * Any failure results in NULL being returned.
  40. */
  41. struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
  42. {
  43. struct generic_pm_domain *genpd = NULL, *gpd;
  44. if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
  45. return NULL;
  46. mutex_lock(&gpd_list_lock);
  47. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  48. if (&gpd->domain == dev->pm_domain) {
  49. genpd = gpd;
  50. break;
  51. }
  52. }
  53. mutex_unlock(&gpd_list_lock);
  54. return genpd;
  55. }
  56. /*
  57. * This should only be used where we are certain that the pm_domain
  58. * attached to the device is a genpd domain.
  59. */
  60. static struct generic_pm_domain *dev_to_genpd(struct device *dev)
  61. {
  62. if (IS_ERR_OR_NULL(dev->pm_domain))
  63. return ERR_PTR(-EINVAL);
  64. return pd_to_genpd(dev->pm_domain);
  65. }
  66. static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
  67. {
  68. return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
  69. }
  70. static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
  71. {
  72. return GENPD_DEV_CALLBACK(genpd, int, start, dev);
  73. }
  74. static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  75. {
  76. bool ret = false;
  77. if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  78. ret = !!atomic_dec_and_test(&genpd->sd_count);
  79. return ret;
  80. }
  81. static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
  82. {
  83. atomic_inc(&genpd->sd_count);
  84. smp_mb__after_atomic();
  85. }
  86. static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
  87. {
  88. ktime_t time_start;
  89. s64 elapsed_ns;
  90. int ret;
  91. if (!genpd->power_on)
  92. return 0;
  93. if (!timed)
  94. return genpd->power_on(genpd);
  95. time_start = ktime_get();
  96. ret = genpd->power_on(genpd);
  97. if (ret)
  98. return ret;
  99. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  100. if (elapsed_ns <= genpd->power_on_latency_ns)
  101. return ret;
  102. genpd->power_on_latency_ns = elapsed_ns;
  103. genpd->max_off_time_changed = true;
  104. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  105. genpd->name, "on", elapsed_ns);
  106. return ret;
  107. }
  108. static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
  109. {
  110. ktime_t time_start;
  111. s64 elapsed_ns;
  112. int ret;
  113. if (!genpd->power_off)
  114. return 0;
  115. if (!timed)
  116. return genpd->power_off(genpd);
  117. time_start = ktime_get();
  118. ret = genpd->power_off(genpd);
  119. if (ret == -EBUSY)
  120. return ret;
  121. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  122. if (elapsed_ns <= genpd->power_off_latency_ns)
  123. return ret;
  124. genpd->power_off_latency_ns = elapsed_ns;
  125. genpd->max_off_time_changed = true;
  126. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  127. genpd->name, "off", elapsed_ns);
  128. return ret;
  129. }
  130. /**
  131. * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
  132. * @genpd: PM domait to power off.
  133. *
  134. * Queue up the execution of genpd_poweroff() unless it's already been done
  135. * before.
  136. */
  137. static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  138. {
  139. queue_work(pm_wq, &genpd->power_off_work);
  140. }
  141. static int genpd_poweron(struct generic_pm_domain *genpd);
  142. /**
  143. * __genpd_poweron - Restore power to a given PM domain and its masters.
  144. * @genpd: PM domain to power up.
  145. *
  146. * Restore power to @genpd and all of its masters so that it is possible to
  147. * resume a device belonging to it.
  148. */
  149. static int __genpd_poweron(struct generic_pm_domain *genpd)
  150. {
  151. struct gpd_link *link;
  152. int ret = 0;
  153. if (genpd->status == GPD_STATE_ACTIVE
  154. || (genpd->prepared_count > 0 && genpd->suspend_power_off))
  155. return 0;
  156. /*
  157. * The list is guaranteed not to change while the loop below is being
  158. * executed, unless one of the masters' .power_on() callbacks fiddles
  159. * with it.
  160. */
  161. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  162. genpd_sd_counter_inc(link->master);
  163. ret = genpd_poweron(link->master);
  164. if (ret) {
  165. genpd_sd_counter_dec(link->master);
  166. goto err;
  167. }
  168. }
  169. ret = genpd_power_on(genpd, true);
  170. if (ret)
  171. goto err;
  172. genpd->status = GPD_STATE_ACTIVE;
  173. return 0;
  174. err:
  175. list_for_each_entry_continue_reverse(link,
  176. &genpd->slave_links,
  177. slave_node) {
  178. genpd_sd_counter_dec(link->master);
  179. genpd_queue_power_off_work(link->master);
  180. }
  181. return ret;
  182. }
  183. /**
  184. * genpd_poweron - Restore power to a given PM domain and its masters.
  185. * @genpd: PM domain to power up.
  186. */
  187. static int genpd_poweron(struct generic_pm_domain *genpd)
  188. {
  189. int ret;
  190. mutex_lock(&genpd->lock);
  191. ret = __genpd_poweron(genpd);
  192. mutex_unlock(&genpd->lock);
  193. return ret;
  194. }
  195. static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
  196. {
  197. return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
  198. }
  199. static int genpd_restore_dev(struct generic_pm_domain *genpd,
  200. struct device *dev)
  201. {
  202. return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
  203. }
  204. static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  205. unsigned long val, void *ptr)
  206. {
  207. struct generic_pm_domain_data *gpd_data;
  208. struct device *dev;
  209. gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
  210. dev = gpd_data->base.dev;
  211. for (;;) {
  212. struct generic_pm_domain *genpd;
  213. struct pm_domain_data *pdd;
  214. spin_lock_irq(&dev->power.lock);
  215. pdd = dev->power.subsys_data ?
  216. dev->power.subsys_data->domain_data : NULL;
  217. if (pdd && pdd->dev) {
  218. to_gpd_data(pdd)->td.constraint_changed = true;
  219. genpd = dev_to_genpd(dev);
  220. } else {
  221. genpd = ERR_PTR(-ENODATA);
  222. }
  223. spin_unlock_irq(&dev->power.lock);
  224. if (!IS_ERR(genpd)) {
  225. mutex_lock(&genpd->lock);
  226. genpd->max_off_time_changed = true;
  227. mutex_unlock(&genpd->lock);
  228. }
  229. dev = dev->parent;
  230. if (!dev || dev->power.ignore_children)
  231. break;
  232. }
  233. return NOTIFY_DONE;
  234. }
  235. /**
  236. * genpd_poweroff - Remove power from a given PM domain.
  237. * @genpd: PM domain to power down.
  238. * @is_async: PM domain is powered down from a scheduled work
  239. *
  240. * If all of the @genpd's devices have been suspended and all of its subdomains
  241. * have been powered down, remove power from @genpd.
  242. */
  243. static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
  244. {
  245. struct pm_domain_data *pdd;
  246. struct gpd_link *link;
  247. unsigned int not_suspended = 0;
  248. /*
  249. * Do not try to power off the domain in the following situations:
  250. * (1) The domain is already in the "power off" state.
  251. * (2) System suspend is in progress.
  252. */
  253. if (genpd->status == GPD_STATE_POWER_OFF
  254. || genpd->prepared_count > 0)
  255. return 0;
  256. if (atomic_read(&genpd->sd_count) > 0)
  257. return -EBUSY;
  258. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  259. enum pm_qos_flags_status stat;
  260. stat = dev_pm_qos_flags(pdd->dev,
  261. PM_QOS_FLAG_NO_POWER_OFF
  262. | PM_QOS_FLAG_REMOTE_WAKEUP);
  263. if (stat > PM_QOS_FLAGS_NONE)
  264. return -EBUSY;
  265. if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
  266. not_suspended++;
  267. }
  268. if (not_suspended > 1 || (not_suspended == 1 && is_async))
  269. return -EBUSY;
  270. if (genpd->gov && genpd->gov->power_down_ok) {
  271. if (!genpd->gov->power_down_ok(&genpd->domain))
  272. return -EAGAIN;
  273. }
  274. if (genpd->power_off) {
  275. int ret;
  276. if (atomic_read(&genpd->sd_count) > 0)
  277. return -EBUSY;
  278. /*
  279. * If sd_count > 0 at this point, one of the subdomains hasn't
  280. * managed to call genpd_poweron() for the master yet after
  281. * incrementing it. In that case genpd_poweron() will wait
  282. * for us to drop the lock, so we can call .power_off() and let
  283. * the genpd_poweron() restore power for us (this shouldn't
  284. * happen very often).
  285. */
  286. ret = genpd_power_off(genpd, true);
  287. if (ret)
  288. return ret;
  289. }
  290. genpd->status = GPD_STATE_POWER_OFF;
  291. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  292. genpd_sd_counter_dec(link->master);
  293. genpd_queue_power_off_work(link->master);
  294. }
  295. return 0;
  296. }
  297. /**
  298. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  299. * @work: Work structure used for scheduling the execution of this function.
  300. */
  301. static void genpd_power_off_work_fn(struct work_struct *work)
  302. {
  303. struct generic_pm_domain *genpd;
  304. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  305. mutex_lock(&genpd->lock);
  306. genpd_poweroff(genpd, true);
  307. mutex_unlock(&genpd->lock);
  308. }
  309. /**
  310. * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  311. * @dev: Device to suspend.
  312. *
  313. * Carry out a runtime suspend of a device under the assumption that its
  314. * pm_domain field points to the domain member of an object of type
  315. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  316. */
  317. static int pm_genpd_runtime_suspend(struct device *dev)
  318. {
  319. struct generic_pm_domain *genpd;
  320. bool (*stop_ok)(struct device *__dev);
  321. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  322. bool runtime_pm = pm_runtime_enabled(dev);
  323. ktime_t time_start;
  324. s64 elapsed_ns;
  325. int ret;
  326. dev_dbg(dev, "%s()\n", __func__);
  327. genpd = dev_to_genpd(dev);
  328. if (IS_ERR(genpd))
  329. return -EINVAL;
  330. /*
  331. * A runtime PM centric subsystem/driver may re-use the runtime PM
  332. * callbacks for other purposes than runtime PM. In those scenarios
  333. * runtime PM is disabled. Under these circumstances, we shall skip
  334. * validating/measuring the PM QoS latency.
  335. */
  336. stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
  337. if (runtime_pm && stop_ok && !stop_ok(dev))
  338. return -EBUSY;
  339. /* Measure suspend latency. */
  340. if (runtime_pm)
  341. time_start = ktime_get();
  342. ret = genpd_save_dev(genpd, dev);
  343. if (ret)
  344. return ret;
  345. ret = genpd_stop_dev(genpd, dev);
  346. if (ret) {
  347. genpd_restore_dev(genpd, dev);
  348. return ret;
  349. }
  350. /* Update suspend latency value if the measured time exceeds it. */
  351. if (runtime_pm) {
  352. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  353. if (elapsed_ns > td->suspend_latency_ns) {
  354. td->suspend_latency_ns = elapsed_ns;
  355. dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
  356. elapsed_ns);
  357. genpd->max_off_time_changed = true;
  358. td->constraint_changed = true;
  359. }
  360. }
  361. /*
  362. * If power.irq_safe is set, this routine will be run with interrupts
  363. * off, so it can't use mutexes.
  364. */
  365. if (dev->power.irq_safe)
  366. return 0;
  367. mutex_lock(&genpd->lock);
  368. genpd_poweroff(genpd, false);
  369. mutex_unlock(&genpd->lock);
  370. return 0;
  371. }
  372. /**
  373. * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  374. * @dev: Device to resume.
  375. *
  376. * Carry out a runtime resume of a device under the assumption that its
  377. * pm_domain field points to the domain member of an object of type
  378. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  379. */
  380. static int pm_genpd_runtime_resume(struct device *dev)
  381. {
  382. struct generic_pm_domain *genpd;
  383. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  384. bool runtime_pm = pm_runtime_enabled(dev);
  385. ktime_t time_start;
  386. s64 elapsed_ns;
  387. int ret;
  388. bool timed = true;
  389. dev_dbg(dev, "%s()\n", __func__);
  390. genpd = dev_to_genpd(dev);
  391. if (IS_ERR(genpd))
  392. return -EINVAL;
  393. /* If power.irq_safe, the PM domain is never powered off. */
  394. if (dev->power.irq_safe) {
  395. timed = false;
  396. goto out;
  397. }
  398. mutex_lock(&genpd->lock);
  399. ret = __genpd_poweron(genpd);
  400. mutex_unlock(&genpd->lock);
  401. if (ret)
  402. return ret;
  403. out:
  404. /* Measure resume latency. */
  405. if (timed && runtime_pm)
  406. time_start = ktime_get();
  407. genpd_start_dev(genpd, dev);
  408. genpd_restore_dev(genpd, dev);
  409. /* Update resume latency value if the measured time exceeds it. */
  410. if (timed && runtime_pm) {
  411. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  412. if (elapsed_ns > td->resume_latency_ns) {
  413. td->resume_latency_ns = elapsed_ns;
  414. dev_dbg(dev, "resume latency exceeded, %lld ns\n",
  415. elapsed_ns);
  416. genpd->max_off_time_changed = true;
  417. td->constraint_changed = true;
  418. }
  419. }
  420. return 0;
  421. }
  422. static bool pd_ignore_unused;
  423. static int __init pd_ignore_unused_setup(char *__unused)
  424. {
  425. pd_ignore_unused = true;
  426. return 1;
  427. }
  428. __setup("pd_ignore_unused", pd_ignore_unused_setup);
  429. /**
  430. * genpd_poweroff_unused - Power off all PM domains with no devices in use.
  431. */
  432. static int __init genpd_poweroff_unused(void)
  433. {
  434. struct generic_pm_domain *genpd;
  435. if (pd_ignore_unused) {
  436. pr_warn("genpd: Not disabling unused power domains\n");
  437. return 0;
  438. }
  439. mutex_lock(&gpd_list_lock);
  440. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  441. genpd_queue_power_off_work(genpd);
  442. mutex_unlock(&gpd_list_lock);
  443. return 0;
  444. }
  445. late_initcall(genpd_poweroff_unused);
  446. #ifdef CONFIG_PM_SLEEP
  447. /**
  448. * pm_genpd_present - Check if the given PM domain has been initialized.
  449. * @genpd: PM domain to check.
  450. */
  451. static bool pm_genpd_present(const struct generic_pm_domain *genpd)
  452. {
  453. const struct generic_pm_domain *gpd;
  454. if (IS_ERR_OR_NULL(genpd))
  455. return false;
  456. list_for_each_entry(gpd, &gpd_list, gpd_list_node)
  457. if (gpd == genpd)
  458. return true;
  459. return false;
  460. }
  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. /**
  467. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
  468. * @genpd: PM domain to power off, if possible.
  469. * @timed: True if latency measurements are allowed.
  470. *
  471. * Check if the given PM domain can be powered off (during system suspend or
  472. * hibernation) and do that if so. Also, in that case propagate to its masters.
  473. *
  474. * This function is only called in "noirq" and "syscore" stages of system power
  475. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  476. * executed sequentially, so it is guaranteed that it will never run twice in
  477. * parallel).
  478. */
  479. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
  480. bool timed)
  481. {
  482. struct gpd_link *link;
  483. if (genpd->status == GPD_STATE_POWER_OFF)
  484. return;
  485. if (genpd->suspended_count != genpd->device_count
  486. || atomic_read(&genpd->sd_count) > 0)
  487. return;
  488. genpd_power_off(genpd, timed);
  489. genpd->status = GPD_STATE_POWER_OFF;
  490. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  491. genpd_sd_counter_dec(link->master);
  492. pm_genpd_sync_poweroff(link->master, timed);
  493. }
  494. }
  495. /**
  496. * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
  497. * @genpd: PM domain to power on.
  498. * @timed: True if latency measurements are allowed.
  499. *
  500. * This function is only called in "noirq" and "syscore" stages of system power
  501. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  502. * executed sequentially, so it is guaranteed that it will never run twice in
  503. * parallel).
  504. */
  505. static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
  506. bool timed)
  507. {
  508. struct gpd_link *link;
  509. if (genpd->status == GPD_STATE_ACTIVE)
  510. return;
  511. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  512. pm_genpd_sync_poweron(link->master, timed);
  513. genpd_sd_counter_inc(link->master);
  514. }
  515. genpd_power_on(genpd, timed);
  516. genpd->status = GPD_STATE_ACTIVE;
  517. }
  518. /**
  519. * resume_needed - Check whether to resume a device before system suspend.
  520. * @dev: Device to check.
  521. * @genpd: PM domain the device belongs to.
  522. *
  523. * There are two cases in which a device that can wake up the system from sleep
  524. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  525. * to wake up the system and it has to remain active for this purpose while the
  526. * system is in the sleep state and (2) if the device is not enabled to wake up
  527. * the system from sleep states and it generally doesn't generate wakeup signals
  528. * by itself (those signals are generated on its behalf by other parts of the
  529. * system). In the latter case it may be necessary to reconfigure the device's
  530. * wakeup settings during system suspend, because it may have been set up to
  531. * signal remote wakeup from the system's working state as needed by runtime PM.
  532. * Return 'true' in either of the above cases.
  533. */
  534. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  535. {
  536. bool active_wakeup;
  537. if (!device_can_wakeup(dev))
  538. return false;
  539. active_wakeup = genpd_dev_active_wakeup(genpd, dev);
  540. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  541. }
  542. /**
  543. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  544. * @dev: Device to start the transition of.
  545. *
  546. * Start a power transition of a device (during a system-wide power transition)
  547. * under the assumption that its pm_domain field points to the domain member of
  548. * an object of type struct generic_pm_domain representing a PM domain
  549. * consisting of I/O devices.
  550. */
  551. static int pm_genpd_prepare(struct device *dev)
  552. {
  553. struct generic_pm_domain *genpd;
  554. int ret;
  555. dev_dbg(dev, "%s()\n", __func__);
  556. genpd = dev_to_genpd(dev);
  557. if (IS_ERR(genpd))
  558. return -EINVAL;
  559. /*
  560. * If a wakeup request is pending for the device, it should be woken up
  561. * at this point and a system wakeup event should be reported if it's
  562. * set up to wake up the system from sleep states.
  563. */
  564. pm_runtime_get_noresume(dev);
  565. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  566. pm_wakeup_event(dev, 0);
  567. if (pm_wakeup_pending()) {
  568. pm_runtime_put(dev);
  569. return -EBUSY;
  570. }
  571. if (resume_needed(dev, genpd))
  572. pm_runtime_resume(dev);
  573. mutex_lock(&genpd->lock);
  574. if (genpd->prepared_count++ == 0) {
  575. genpd->suspended_count = 0;
  576. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  577. }
  578. mutex_unlock(&genpd->lock);
  579. if (genpd->suspend_power_off) {
  580. pm_runtime_put_noidle(dev);
  581. return 0;
  582. }
  583. /*
  584. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  585. * so genpd_poweron() will return immediately, but if the device
  586. * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
  587. * to make it operational.
  588. */
  589. pm_runtime_resume(dev);
  590. __pm_runtime_disable(dev, false);
  591. ret = pm_generic_prepare(dev);
  592. if (ret) {
  593. mutex_lock(&genpd->lock);
  594. if (--genpd->prepared_count == 0)
  595. genpd->suspend_power_off = false;
  596. mutex_unlock(&genpd->lock);
  597. pm_runtime_enable(dev);
  598. }
  599. pm_runtime_put(dev);
  600. return ret;
  601. }
  602. /**
  603. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  604. * @dev: Device to suspend.
  605. *
  606. * Suspend a device under the assumption that its pm_domain field points to the
  607. * domain member of an object of type struct generic_pm_domain representing
  608. * a PM domain consisting of I/O devices.
  609. */
  610. static int pm_genpd_suspend(struct device *dev)
  611. {
  612. struct generic_pm_domain *genpd;
  613. dev_dbg(dev, "%s()\n", __func__);
  614. genpd = dev_to_genpd(dev);
  615. if (IS_ERR(genpd))
  616. return -EINVAL;
  617. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  618. }
  619. /**
  620. * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
  621. * @dev: Device to suspend.
  622. *
  623. * Carry out a late suspend of a device under the assumption that its
  624. * pm_domain field points to the domain member of an object of type
  625. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  626. */
  627. static int pm_genpd_suspend_late(struct device *dev)
  628. {
  629. struct generic_pm_domain *genpd;
  630. dev_dbg(dev, "%s()\n", __func__);
  631. genpd = dev_to_genpd(dev);
  632. if (IS_ERR(genpd))
  633. return -EINVAL;
  634. return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
  635. }
  636. /**
  637. * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  638. * @dev: Device to suspend.
  639. *
  640. * Stop the device and remove power from the domain if all devices in it have
  641. * been stopped.
  642. */
  643. static int pm_genpd_suspend_noirq(struct device *dev)
  644. {
  645. struct generic_pm_domain *genpd;
  646. dev_dbg(dev, "%s()\n", __func__);
  647. genpd = dev_to_genpd(dev);
  648. if (IS_ERR(genpd))
  649. return -EINVAL;
  650. if (genpd->suspend_power_off
  651. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  652. return 0;
  653. genpd_stop_dev(genpd, dev);
  654. /*
  655. * Since all of the "noirq" callbacks are executed sequentially, it is
  656. * guaranteed that this function will never run twice in parallel for
  657. * the same PM domain, so it is not necessary to use locking here.
  658. */
  659. genpd->suspended_count++;
  660. pm_genpd_sync_poweroff(genpd, true);
  661. return 0;
  662. }
  663. /**
  664. * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  665. * @dev: Device to resume.
  666. *
  667. * Restore power to the device's PM domain, if necessary, and start the device.
  668. */
  669. static int pm_genpd_resume_noirq(struct device *dev)
  670. {
  671. struct generic_pm_domain *genpd;
  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. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  678. return 0;
  679. /*
  680. * Since all of the "noirq" callbacks are executed sequentially, it is
  681. * guaranteed that this function will never run twice in parallel for
  682. * the same PM domain, so it is not necessary to use locking here.
  683. */
  684. pm_genpd_sync_poweron(genpd, true);
  685. genpd->suspended_count--;
  686. return genpd_start_dev(genpd, dev);
  687. }
  688. /**
  689. * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
  690. * @dev: Device to resume.
  691. *
  692. * Carry out an early resume of a device under the assumption that its
  693. * pm_domain field points to the domain member of an object of type
  694. * struct generic_pm_domain representing a power domain consisting of I/O
  695. * devices.
  696. */
  697. static int pm_genpd_resume_early(struct device *dev)
  698. {
  699. struct generic_pm_domain *genpd;
  700. dev_dbg(dev, "%s()\n", __func__);
  701. genpd = dev_to_genpd(dev);
  702. if (IS_ERR(genpd))
  703. return -EINVAL;
  704. return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
  705. }
  706. /**
  707. * pm_genpd_resume - Resume of device in an I/O PM domain.
  708. * @dev: Device to resume.
  709. *
  710. * Resume a device under the assumption that its pm_domain field points to the
  711. * domain member of an object of type struct generic_pm_domain representing
  712. * a power domain consisting of I/O devices.
  713. */
  714. static int pm_genpd_resume(struct device *dev)
  715. {
  716. struct generic_pm_domain *genpd;
  717. dev_dbg(dev, "%s()\n", __func__);
  718. genpd = dev_to_genpd(dev);
  719. if (IS_ERR(genpd))
  720. return -EINVAL;
  721. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  722. }
  723. /**
  724. * pm_genpd_freeze - Freezing a device in an I/O PM domain.
  725. * @dev: Device to freeze.
  726. *
  727. * Freeze a device under the assumption that its pm_domain field points to the
  728. * domain member of an object of type struct generic_pm_domain representing
  729. * a power domain consisting of I/O devices.
  730. */
  731. static int pm_genpd_freeze(struct device *dev)
  732. {
  733. struct generic_pm_domain *genpd;
  734. dev_dbg(dev, "%s()\n", __func__);
  735. genpd = dev_to_genpd(dev);
  736. if (IS_ERR(genpd))
  737. return -EINVAL;
  738. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  739. }
  740. /**
  741. * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
  742. * @dev: Device to freeze.
  743. *
  744. * Carry out a late freeze of a device under the assumption that its
  745. * pm_domain field points to the domain member of an object of type
  746. * struct generic_pm_domain representing a power domain consisting of I/O
  747. * devices.
  748. */
  749. static int pm_genpd_freeze_late(struct device *dev)
  750. {
  751. struct generic_pm_domain *genpd;
  752. dev_dbg(dev, "%s()\n", __func__);
  753. genpd = dev_to_genpd(dev);
  754. if (IS_ERR(genpd))
  755. return -EINVAL;
  756. return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
  757. }
  758. /**
  759. * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  760. * @dev: Device to freeze.
  761. *
  762. * Carry out a late freeze of a device under the assumption that its
  763. * pm_domain field points to the domain member of an object of type
  764. * struct generic_pm_domain representing a power domain consisting of I/O
  765. * devices.
  766. */
  767. static int pm_genpd_freeze_noirq(struct device *dev)
  768. {
  769. struct generic_pm_domain *genpd;
  770. dev_dbg(dev, "%s()\n", __func__);
  771. genpd = dev_to_genpd(dev);
  772. if (IS_ERR(genpd))
  773. return -EINVAL;
  774. return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
  775. }
  776. /**
  777. * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  778. * @dev: Device to thaw.
  779. *
  780. * Start the device, unless power has been removed from the domain already
  781. * before the system transition.
  782. */
  783. static int pm_genpd_thaw_noirq(struct device *dev)
  784. {
  785. struct generic_pm_domain *genpd;
  786. dev_dbg(dev, "%s()\n", __func__);
  787. genpd = dev_to_genpd(dev);
  788. if (IS_ERR(genpd))
  789. return -EINVAL;
  790. return genpd->suspend_power_off ?
  791. 0 : genpd_start_dev(genpd, dev);
  792. }
  793. /**
  794. * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
  795. * @dev: Device to thaw.
  796. *
  797. * Carry out an early thaw of a device under the assumption that its
  798. * pm_domain field points to the domain member of an object of type
  799. * struct generic_pm_domain representing a power domain consisting of I/O
  800. * devices.
  801. */
  802. static int pm_genpd_thaw_early(struct device *dev)
  803. {
  804. struct generic_pm_domain *genpd;
  805. dev_dbg(dev, "%s()\n", __func__);
  806. genpd = dev_to_genpd(dev);
  807. if (IS_ERR(genpd))
  808. return -EINVAL;
  809. return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
  810. }
  811. /**
  812. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  813. * @dev: Device to thaw.
  814. *
  815. * Thaw a device under the assumption that its pm_domain field points to the
  816. * domain member of an object of type struct generic_pm_domain representing
  817. * a power domain consisting of I/O devices.
  818. */
  819. static int pm_genpd_thaw(struct device *dev)
  820. {
  821. struct generic_pm_domain *genpd;
  822. dev_dbg(dev, "%s()\n", __func__);
  823. genpd = dev_to_genpd(dev);
  824. if (IS_ERR(genpd))
  825. return -EINVAL;
  826. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  827. }
  828. /**
  829. * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  830. * @dev: Device to resume.
  831. *
  832. * Make sure the domain will be in the same power state as before the
  833. * hibernation the system is resuming from and start the device if necessary.
  834. */
  835. static int pm_genpd_restore_noirq(struct device *dev)
  836. {
  837. struct generic_pm_domain *genpd;
  838. dev_dbg(dev, "%s()\n", __func__);
  839. genpd = dev_to_genpd(dev);
  840. if (IS_ERR(genpd))
  841. return -EINVAL;
  842. /*
  843. * Since all of the "noirq" callbacks are executed sequentially, it is
  844. * guaranteed that this function will never run twice in parallel for
  845. * the same PM domain, so it is not necessary to use locking here.
  846. *
  847. * At this point suspended_count == 0 means we are being run for the
  848. * first time for the given domain in the present cycle.
  849. */
  850. if (genpd->suspended_count++ == 0) {
  851. /*
  852. * The boot kernel might put the domain into arbitrary state,
  853. * so make it appear as powered off to pm_genpd_sync_poweron(),
  854. * so that it tries to power it on in case it was really off.
  855. */
  856. genpd->status = GPD_STATE_POWER_OFF;
  857. if (genpd->suspend_power_off) {
  858. /*
  859. * If the domain was off before the hibernation, make
  860. * sure it will be off going forward.
  861. */
  862. genpd_power_off(genpd, true);
  863. return 0;
  864. }
  865. }
  866. if (genpd->suspend_power_off)
  867. return 0;
  868. pm_genpd_sync_poweron(genpd, true);
  869. return genpd_start_dev(genpd, dev);
  870. }
  871. /**
  872. * pm_genpd_complete - Complete power transition of a device in a power domain.
  873. * @dev: Device to complete the transition of.
  874. *
  875. * Complete a power transition of a device (during a system-wide power
  876. * transition) under the assumption that its pm_domain field points to the
  877. * domain member of an object of type struct generic_pm_domain representing
  878. * a power domain consisting of I/O devices.
  879. */
  880. static void pm_genpd_complete(struct device *dev)
  881. {
  882. struct generic_pm_domain *genpd;
  883. bool run_complete;
  884. dev_dbg(dev, "%s()\n", __func__);
  885. genpd = dev_to_genpd(dev);
  886. if (IS_ERR(genpd))
  887. return;
  888. mutex_lock(&genpd->lock);
  889. run_complete = !genpd->suspend_power_off;
  890. if (--genpd->prepared_count == 0)
  891. genpd->suspend_power_off = false;
  892. mutex_unlock(&genpd->lock);
  893. if (run_complete) {
  894. pm_generic_complete(dev);
  895. pm_runtime_set_active(dev);
  896. pm_runtime_enable(dev);
  897. pm_request_idle(dev);
  898. }
  899. }
  900. /**
  901. * genpd_syscore_switch - Switch power during system core suspend or resume.
  902. * @dev: Device that normally is marked as "always on" to switch power for.
  903. *
  904. * This routine may only be called during the system core (syscore) suspend or
  905. * resume phase for devices whose "always on" flags are set.
  906. */
  907. static void genpd_syscore_switch(struct device *dev, bool suspend)
  908. {
  909. struct generic_pm_domain *genpd;
  910. genpd = dev_to_genpd(dev);
  911. if (!pm_genpd_present(genpd))
  912. return;
  913. if (suspend) {
  914. genpd->suspended_count++;
  915. pm_genpd_sync_poweroff(genpd, false);
  916. } else {
  917. pm_genpd_sync_poweron(genpd, false);
  918. genpd->suspended_count--;
  919. }
  920. }
  921. void pm_genpd_syscore_poweroff(struct device *dev)
  922. {
  923. genpd_syscore_switch(dev, true);
  924. }
  925. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
  926. void pm_genpd_syscore_poweron(struct device *dev)
  927. {
  928. genpd_syscore_switch(dev, false);
  929. }
  930. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
  931. #else /* !CONFIG_PM_SLEEP */
  932. #define pm_genpd_prepare NULL
  933. #define pm_genpd_suspend NULL
  934. #define pm_genpd_suspend_late NULL
  935. #define pm_genpd_suspend_noirq NULL
  936. #define pm_genpd_resume_early NULL
  937. #define pm_genpd_resume_noirq NULL
  938. #define pm_genpd_resume NULL
  939. #define pm_genpd_freeze NULL
  940. #define pm_genpd_freeze_late NULL
  941. #define pm_genpd_freeze_noirq NULL
  942. #define pm_genpd_thaw_early NULL
  943. #define pm_genpd_thaw_noirq NULL
  944. #define pm_genpd_thaw NULL
  945. #define pm_genpd_restore_noirq NULL
  946. #define pm_genpd_complete NULL
  947. #endif /* CONFIG_PM_SLEEP */
  948. static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
  949. struct generic_pm_domain *genpd,
  950. struct gpd_timing_data *td)
  951. {
  952. struct generic_pm_domain_data *gpd_data;
  953. int ret;
  954. ret = dev_pm_get_subsys_data(dev);
  955. if (ret)
  956. return ERR_PTR(ret);
  957. gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
  958. if (!gpd_data) {
  959. ret = -ENOMEM;
  960. goto err_put;
  961. }
  962. if (td)
  963. gpd_data->td = *td;
  964. gpd_data->base.dev = dev;
  965. gpd_data->td.constraint_changed = true;
  966. gpd_data->td.effective_constraint_ns = -1;
  967. gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
  968. spin_lock_irq(&dev->power.lock);
  969. if (dev->power.subsys_data->domain_data) {
  970. ret = -EINVAL;
  971. goto err_free;
  972. }
  973. dev->power.subsys_data->domain_data = &gpd_data->base;
  974. spin_unlock_irq(&dev->power.lock);
  975. return gpd_data;
  976. err_free:
  977. spin_unlock_irq(&dev->power.lock);
  978. kfree(gpd_data);
  979. err_put:
  980. dev_pm_put_subsys_data(dev);
  981. return ERR_PTR(ret);
  982. }
  983. static void genpd_free_dev_data(struct device *dev,
  984. struct generic_pm_domain_data *gpd_data)
  985. {
  986. spin_lock_irq(&dev->power.lock);
  987. dev->power.subsys_data->domain_data = NULL;
  988. spin_unlock_irq(&dev->power.lock);
  989. kfree(gpd_data);
  990. dev_pm_put_subsys_data(dev);
  991. }
  992. /**
  993. * __pm_genpd_add_device - Add a device to an I/O PM domain.
  994. * @genpd: PM domain to add the device to.
  995. * @dev: Device to be added.
  996. * @td: Set of PM QoS timing parameters to attach to the device.
  997. */
  998. int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  999. struct gpd_timing_data *td)
  1000. {
  1001. struct generic_pm_domain_data *gpd_data;
  1002. int ret = 0;
  1003. dev_dbg(dev, "%s()\n", __func__);
  1004. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  1005. return -EINVAL;
  1006. gpd_data = genpd_alloc_dev_data(dev, genpd, td);
  1007. if (IS_ERR(gpd_data))
  1008. return PTR_ERR(gpd_data);
  1009. mutex_lock(&genpd->lock);
  1010. if (genpd->prepared_count > 0) {
  1011. ret = -EAGAIN;
  1012. goto out;
  1013. }
  1014. ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
  1015. if (ret)
  1016. goto out;
  1017. dev->pm_domain = &genpd->domain;
  1018. genpd->device_count++;
  1019. genpd->max_off_time_changed = true;
  1020. list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
  1021. out:
  1022. mutex_unlock(&genpd->lock);
  1023. if (ret)
  1024. genpd_free_dev_data(dev, gpd_data);
  1025. else
  1026. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1027. return ret;
  1028. }
  1029. /**
  1030. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  1031. * @genpd: PM domain to remove the device from.
  1032. * @dev: Device to be removed.
  1033. */
  1034. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  1035. struct device *dev)
  1036. {
  1037. struct generic_pm_domain_data *gpd_data;
  1038. struct pm_domain_data *pdd;
  1039. int ret = 0;
  1040. dev_dbg(dev, "%s()\n", __func__);
  1041. if (!genpd || genpd != pm_genpd_lookup_dev(dev))
  1042. return -EINVAL;
  1043. /* The above validation also means we have existing domain_data. */
  1044. pdd = dev->power.subsys_data->domain_data;
  1045. gpd_data = to_gpd_data(pdd);
  1046. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1047. mutex_lock(&genpd->lock);
  1048. if (genpd->prepared_count > 0) {
  1049. ret = -EAGAIN;
  1050. goto out;
  1051. }
  1052. genpd->device_count--;
  1053. genpd->max_off_time_changed = true;
  1054. if (genpd->detach_dev)
  1055. genpd->detach_dev(genpd, dev);
  1056. dev->pm_domain = NULL;
  1057. list_del_init(&pdd->list_node);
  1058. mutex_unlock(&genpd->lock);
  1059. genpd_free_dev_data(dev, gpd_data);
  1060. return 0;
  1061. out:
  1062. mutex_unlock(&genpd->lock);
  1063. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1064. return ret;
  1065. }
  1066. /**
  1067. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1068. * @genpd: Master PM domain to add the subdomain to.
  1069. * @subdomain: Subdomain to be added.
  1070. */
  1071. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  1072. struct generic_pm_domain *subdomain)
  1073. {
  1074. struct gpd_link *link, *itr;
  1075. int ret = 0;
  1076. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
  1077. || genpd == subdomain)
  1078. return -EINVAL;
  1079. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1080. if (!link)
  1081. return -ENOMEM;
  1082. mutex_lock(&genpd->lock);
  1083. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1084. if (genpd->status == GPD_STATE_POWER_OFF
  1085. && subdomain->status != GPD_STATE_POWER_OFF) {
  1086. ret = -EINVAL;
  1087. goto out;
  1088. }
  1089. list_for_each_entry(itr, &genpd->master_links, master_node) {
  1090. if (itr->slave == subdomain && itr->master == genpd) {
  1091. ret = -EINVAL;
  1092. goto out;
  1093. }
  1094. }
  1095. link->master = genpd;
  1096. list_add_tail(&link->master_node, &genpd->master_links);
  1097. link->slave = subdomain;
  1098. list_add_tail(&link->slave_node, &subdomain->slave_links);
  1099. if (subdomain->status != GPD_STATE_POWER_OFF)
  1100. genpd_sd_counter_inc(genpd);
  1101. out:
  1102. mutex_unlock(&subdomain->lock);
  1103. mutex_unlock(&genpd->lock);
  1104. if (ret)
  1105. kfree(link);
  1106. return ret;
  1107. }
  1108. EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
  1109. /**
  1110. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  1111. * @genpd: Master PM domain to remove the subdomain from.
  1112. * @subdomain: Subdomain to be removed.
  1113. */
  1114. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  1115. struct generic_pm_domain *subdomain)
  1116. {
  1117. struct gpd_link *l, *link;
  1118. int ret = -EINVAL;
  1119. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1120. return -EINVAL;
  1121. mutex_lock(&genpd->lock);
  1122. if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
  1123. pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
  1124. subdomain->name);
  1125. ret = -EBUSY;
  1126. goto out;
  1127. }
  1128. list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
  1129. if (link->slave != subdomain)
  1130. continue;
  1131. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1132. list_del(&link->master_node);
  1133. list_del(&link->slave_node);
  1134. kfree(link);
  1135. if (subdomain->status != GPD_STATE_POWER_OFF)
  1136. genpd_sd_counter_dec(genpd);
  1137. mutex_unlock(&subdomain->lock);
  1138. ret = 0;
  1139. break;
  1140. }
  1141. out:
  1142. mutex_unlock(&genpd->lock);
  1143. return ret;
  1144. }
  1145. EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
  1146. /* Default device callbacks for generic PM domains. */
  1147. /**
  1148. * pm_genpd_default_save_state - Default "save device state" for PM domains.
  1149. * @dev: Device to handle.
  1150. */
  1151. static int pm_genpd_default_save_state(struct device *dev)
  1152. {
  1153. int (*cb)(struct device *__dev);
  1154. if (dev->type && dev->type->pm)
  1155. cb = dev->type->pm->runtime_suspend;
  1156. else if (dev->class && dev->class->pm)
  1157. cb = dev->class->pm->runtime_suspend;
  1158. else if (dev->bus && dev->bus->pm)
  1159. cb = dev->bus->pm->runtime_suspend;
  1160. else
  1161. cb = NULL;
  1162. if (!cb && dev->driver && dev->driver->pm)
  1163. cb = dev->driver->pm->runtime_suspend;
  1164. return cb ? cb(dev) : 0;
  1165. }
  1166. /**
  1167. * pm_genpd_default_restore_state - Default PM domains "restore device state".
  1168. * @dev: Device to handle.
  1169. */
  1170. static int pm_genpd_default_restore_state(struct device *dev)
  1171. {
  1172. int (*cb)(struct device *__dev);
  1173. if (dev->type && dev->type->pm)
  1174. cb = dev->type->pm->runtime_resume;
  1175. else if (dev->class && dev->class->pm)
  1176. cb = dev->class->pm->runtime_resume;
  1177. else if (dev->bus && dev->bus->pm)
  1178. cb = dev->bus->pm->runtime_resume;
  1179. else
  1180. cb = NULL;
  1181. if (!cb && dev->driver && dev->driver->pm)
  1182. cb = dev->driver->pm->runtime_resume;
  1183. return cb ? cb(dev) : 0;
  1184. }
  1185. /**
  1186. * pm_genpd_init - Initialize a generic I/O PM domain object.
  1187. * @genpd: PM domain object to initialize.
  1188. * @gov: PM domain governor to associate with the domain (may be NULL).
  1189. * @is_off: Initial value of the domain's power_is_off field.
  1190. */
  1191. void pm_genpd_init(struct generic_pm_domain *genpd,
  1192. struct dev_power_governor *gov, bool is_off)
  1193. {
  1194. if (IS_ERR_OR_NULL(genpd))
  1195. return;
  1196. INIT_LIST_HEAD(&genpd->master_links);
  1197. INIT_LIST_HEAD(&genpd->slave_links);
  1198. INIT_LIST_HEAD(&genpd->dev_list);
  1199. mutex_init(&genpd->lock);
  1200. genpd->gov = gov;
  1201. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  1202. atomic_set(&genpd->sd_count, 0);
  1203. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  1204. genpd->device_count = 0;
  1205. genpd->max_off_time_ns = -1;
  1206. genpd->max_off_time_changed = true;
  1207. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  1208. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  1209. genpd->domain.ops.prepare = pm_genpd_prepare;
  1210. genpd->domain.ops.suspend = pm_genpd_suspend;
  1211. genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
  1212. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  1213. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  1214. genpd->domain.ops.resume_early = pm_genpd_resume_early;
  1215. genpd->domain.ops.resume = pm_genpd_resume;
  1216. genpd->domain.ops.freeze = pm_genpd_freeze;
  1217. genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
  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_early = pm_genpd_thaw_early;
  1221. genpd->domain.ops.thaw = pm_genpd_thaw;
  1222. genpd->domain.ops.poweroff = pm_genpd_suspend;
  1223. genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
  1224. genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
  1225. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  1226. genpd->domain.ops.restore_early = pm_genpd_resume_early;
  1227. genpd->domain.ops.restore = pm_genpd_resume;
  1228. genpd->domain.ops.complete = pm_genpd_complete;
  1229. genpd->dev_ops.save_state = pm_genpd_default_save_state;
  1230. genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
  1231. if (genpd->flags & GENPD_FLAG_PM_CLK) {
  1232. genpd->dev_ops.stop = pm_clk_suspend;
  1233. genpd->dev_ops.start = pm_clk_resume;
  1234. }
  1235. mutex_lock(&gpd_list_lock);
  1236. list_add(&genpd->gpd_list_node, &gpd_list);
  1237. mutex_unlock(&gpd_list_lock);
  1238. }
  1239. EXPORT_SYMBOL_GPL(pm_genpd_init);
  1240. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  1241. /*
  1242. * Device Tree based PM domain providers.
  1243. *
  1244. * The code below implements generic device tree based PM domain providers that
  1245. * bind device tree nodes with generic PM domains registered in the system.
  1246. *
  1247. * Any driver that registers generic PM domains and needs to support binding of
  1248. * devices to these domains is supposed to register a PM domain provider, which
  1249. * maps a PM domain specifier retrieved from the device tree to a PM domain.
  1250. *
  1251. * Two simple mapping functions have been provided for convenience:
  1252. * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
  1253. * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
  1254. * index.
  1255. */
  1256. /**
  1257. * struct of_genpd_provider - PM domain provider registration structure
  1258. * @link: Entry in global list of PM domain providers
  1259. * @node: Pointer to device tree node of PM domain provider
  1260. * @xlate: Provider-specific xlate callback mapping a set of specifier cells
  1261. * into a PM domain.
  1262. * @data: context pointer to be passed into @xlate callback
  1263. */
  1264. struct of_genpd_provider {
  1265. struct list_head link;
  1266. struct device_node *node;
  1267. genpd_xlate_t xlate;
  1268. void *data;
  1269. };
  1270. /* List of registered PM domain providers. */
  1271. static LIST_HEAD(of_genpd_providers);
  1272. /* Mutex to protect the list above. */
  1273. static DEFINE_MUTEX(of_genpd_mutex);
  1274. /**
  1275. * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
  1276. * @genpdspec: OF phandle args to map into a PM domain
  1277. * @data: xlate function private data - pointer to struct generic_pm_domain
  1278. *
  1279. * This is a generic xlate function that can be used to model PM domains that
  1280. * have their own device tree nodes. The private data of xlate function needs
  1281. * to be a valid pointer to struct generic_pm_domain.
  1282. */
  1283. struct generic_pm_domain *__of_genpd_xlate_simple(
  1284. struct of_phandle_args *genpdspec,
  1285. void *data)
  1286. {
  1287. if (genpdspec->args_count != 0)
  1288. return ERR_PTR(-EINVAL);
  1289. return data;
  1290. }
  1291. EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
  1292. /**
  1293. * __of_genpd_xlate_onecell() - Xlate function using a single index.
  1294. * @genpdspec: OF phandle args to map into a PM domain
  1295. * @data: xlate function private data - pointer to struct genpd_onecell_data
  1296. *
  1297. * This is a generic xlate function that can be used to model simple PM domain
  1298. * controllers that have one device tree node and provide multiple PM domains.
  1299. * A single cell is used as an index into an array of PM domains specified in
  1300. * the genpd_onecell_data struct when registering the provider.
  1301. */
  1302. struct generic_pm_domain *__of_genpd_xlate_onecell(
  1303. struct of_phandle_args *genpdspec,
  1304. void *data)
  1305. {
  1306. struct genpd_onecell_data *genpd_data = data;
  1307. unsigned int idx = genpdspec->args[0];
  1308. if (genpdspec->args_count != 1)
  1309. return ERR_PTR(-EINVAL);
  1310. if (idx >= genpd_data->num_domains) {
  1311. pr_err("%s: invalid domain index %u\n", __func__, idx);
  1312. return ERR_PTR(-EINVAL);
  1313. }
  1314. if (!genpd_data->domains[idx])
  1315. return ERR_PTR(-ENOENT);
  1316. return genpd_data->domains[idx];
  1317. }
  1318. EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
  1319. /**
  1320. * __of_genpd_add_provider() - Register a PM domain provider for a node
  1321. * @np: Device node pointer associated with the PM domain provider.
  1322. * @xlate: Callback for decoding PM domain from phandle arguments.
  1323. * @data: Context pointer for @xlate callback.
  1324. */
  1325. int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
  1326. void *data)
  1327. {
  1328. struct of_genpd_provider *cp;
  1329. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  1330. if (!cp)
  1331. return -ENOMEM;
  1332. cp->node = of_node_get(np);
  1333. cp->data = data;
  1334. cp->xlate = xlate;
  1335. mutex_lock(&of_genpd_mutex);
  1336. list_add(&cp->link, &of_genpd_providers);
  1337. mutex_unlock(&of_genpd_mutex);
  1338. pr_debug("Added domain provider from %s\n", np->full_name);
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
  1342. /**
  1343. * of_genpd_del_provider() - Remove a previously registered PM domain provider
  1344. * @np: Device node pointer associated with the PM domain provider
  1345. */
  1346. void of_genpd_del_provider(struct device_node *np)
  1347. {
  1348. struct of_genpd_provider *cp, *tmp;
  1349. mutex_lock(&of_genpd_mutex);
  1350. list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
  1351. if (cp->node == np) {
  1352. list_del(&cp->link);
  1353. of_node_put(cp->node);
  1354. kfree(cp);
  1355. break;
  1356. }
  1357. }
  1358. mutex_unlock(&of_genpd_mutex);
  1359. }
  1360. EXPORT_SYMBOL_GPL(of_genpd_del_provider);
  1361. /**
  1362. * of_genpd_get_from_provider() - Look-up PM domain
  1363. * @genpdspec: OF phandle args to use for look-up
  1364. *
  1365. * Looks for a PM domain provider under the node specified by @genpdspec and if
  1366. * found, uses xlate function of the provider to map phandle args to a PM
  1367. * domain.
  1368. *
  1369. * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
  1370. * on failure.
  1371. */
  1372. struct generic_pm_domain *of_genpd_get_from_provider(
  1373. struct of_phandle_args *genpdspec)
  1374. {
  1375. struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
  1376. struct of_genpd_provider *provider;
  1377. mutex_lock(&of_genpd_mutex);
  1378. /* Check if we have such a provider in our array */
  1379. list_for_each_entry(provider, &of_genpd_providers, link) {
  1380. if (provider->node == genpdspec->np)
  1381. genpd = provider->xlate(genpdspec, provider->data);
  1382. if (!IS_ERR(genpd))
  1383. break;
  1384. }
  1385. mutex_unlock(&of_genpd_mutex);
  1386. return genpd;
  1387. }
  1388. EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
  1389. /**
  1390. * genpd_dev_pm_detach - Detach a device from its PM domain.
  1391. * @dev: Device to detach.
  1392. * @power_off: Currently not used
  1393. *
  1394. * Try to locate a corresponding generic PM domain, which the device was
  1395. * attached to previously. If such is found, the device is detached from it.
  1396. */
  1397. static void genpd_dev_pm_detach(struct device *dev, bool power_off)
  1398. {
  1399. struct generic_pm_domain *pd;
  1400. unsigned int i;
  1401. int ret = 0;
  1402. pd = pm_genpd_lookup_dev(dev);
  1403. if (!pd)
  1404. return;
  1405. dev_dbg(dev, "removing from PM domain %s\n", pd->name);
  1406. for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
  1407. ret = pm_genpd_remove_device(pd, dev);
  1408. if (ret != -EAGAIN)
  1409. break;
  1410. mdelay(i);
  1411. cond_resched();
  1412. }
  1413. if (ret < 0) {
  1414. dev_err(dev, "failed to remove from PM domain %s: %d",
  1415. pd->name, ret);
  1416. return;
  1417. }
  1418. /* Check if PM domain can be powered off after removing this device. */
  1419. genpd_queue_power_off_work(pd);
  1420. }
  1421. static void genpd_dev_pm_sync(struct device *dev)
  1422. {
  1423. struct generic_pm_domain *pd;
  1424. pd = dev_to_genpd(dev);
  1425. if (IS_ERR(pd))
  1426. return;
  1427. genpd_queue_power_off_work(pd);
  1428. }
  1429. /**
  1430. * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
  1431. * @dev: Device to attach.
  1432. *
  1433. * Parse device's OF node to find a PM domain specifier. If such is found,
  1434. * attaches the device to retrieved pm_domain ops.
  1435. *
  1436. * Both generic and legacy Samsung-specific DT bindings are supported to keep
  1437. * backwards compatibility with existing DTBs.
  1438. *
  1439. * Returns 0 on successfully attached PM domain or negative error code. Note
  1440. * that if a power-domain exists for the device, but it cannot be found or
  1441. * turned on, then return -EPROBE_DEFER to ensure that the device is not
  1442. * probed and to re-try again later.
  1443. */
  1444. int genpd_dev_pm_attach(struct device *dev)
  1445. {
  1446. struct of_phandle_args pd_args;
  1447. struct generic_pm_domain *pd;
  1448. unsigned int i;
  1449. int ret;
  1450. if (!dev->of_node)
  1451. return -ENODEV;
  1452. if (dev->pm_domain)
  1453. return -EEXIST;
  1454. ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
  1455. "#power-domain-cells", 0, &pd_args);
  1456. if (ret < 0) {
  1457. if (ret != -ENOENT)
  1458. return ret;
  1459. /*
  1460. * Try legacy Samsung-specific bindings
  1461. * (for backwards compatibility of DT ABI)
  1462. */
  1463. pd_args.args_count = 0;
  1464. pd_args.np = of_parse_phandle(dev->of_node,
  1465. "samsung,power-domain", 0);
  1466. if (!pd_args.np)
  1467. return -ENOENT;
  1468. }
  1469. pd = of_genpd_get_from_provider(&pd_args);
  1470. of_node_put(pd_args.np);
  1471. if (IS_ERR(pd)) {
  1472. dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
  1473. __func__, PTR_ERR(pd));
  1474. return -EPROBE_DEFER;
  1475. }
  1476. dev_dbg(dev, "adding to PM domain %s\n", pd->name);
  1477. for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
  1478. ret = pm_genpd_add_device(pd, dev);
  1479. if (ret != -EAGAIN)
  1480. break;
  1481. mdelay(i);
  1482. cond_resched();
  1483. }
  1484. if (ret < 0) {
  1485. dev_err(dev, "failed to add to PM domain %s: %d",
  1486. pd->name, ret);
  1487. goto out;
  1488. }
  1489. dev->pm_domain->detach = genpd_dev_pm_detach;
  1490. dev->pm_domain->sync = genpd_dev_pm_sync;
  1491. ret = genpd_poweron(pd);
  1492. out:
  1493. return ret ? -EPROBE_DEFER : 0;
  1494. }
  1495. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
  1496. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  1497. /*** debugfs support ***/
  1498. #ifdef CONFIG_PM_ADVANCED_DEBUG
  1499. #include <linux/pm.h>
  1500. #include <linux/device.h>
  1501. #include <linux/debugfs.h>
  1502. #include <linux/seq_file.h>
  1503. #include <linux/init.h>
  1504. #include <linux/kobject.h>
  1505. static struct dentry *pm_genpd_debugfs_dir;
  1506. /*
  1507. * TODO: This function is a slightly modified version of rtpm_status_show
  1508. * from sysfs.c, so generalize it.
  1509. */
  1510. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  1511. {
  1512. static const char * const status_lookup[] = {
  1513. [RPM_ACTIVE] = "active",
  1514. [RPM_RESUMING] = "resuming",
  1515. [RPM_SUSPENDED] = "suspended",
  1516. [RPM_SUSPENDING] = "suspending"
  1517. };
  1518. const char *p = "";
  1519. if (dev->power.runtime_error)
  1520. p = "error";
  1521. else if (dev->power.disable_depth)
  1522. p = "unsupported";
  1523. else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
  1524. p = status_lookup[dev->power.runtime_status];
  1525. else
  1526. WARN_ON(1);
  1527. seq_puts(s, p);
  1528. }
  1529. static int pm_genpd_summary_one(struct seq_file *s,
  1530. struct generic_pm_domain *genpd)
  1531. {
  1532. static const char * const status_lookup[] = {
  1533. [GPD_STATE_ACTIVE] = "on",
  1534. [GPD_STATE_POWER_OFF] = "off"
  1535. };
  1536. struct pm_domain_data *pm_data;
  1537. const char *kobj_path;
  1538. struct gpd_link *link;
  1539. int ret;
  1540. ret = mutex_lock_interruptible(&genpd->lock);
  1541. if (ret)
  1542. return -ERESTARTSYS;
  1543. if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
  1544. goto exit;
  1545. seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
  1546. /*
  1547. * Modifications on the list require holding locks on both
  1548. * master and slave, so we are safe.
  1549. * Also genpd->name is immutable.
  1550. */
  1551. list_for_each_entry(link, &genpd->master_links, master_node) {
  1552. seq_printf(s, "%s", link->slave->name);
  1553. if (!list_is_last(&link->master_node, &genpd->master_links))
  1554. seq_puts(s, ", ");
  1555. }
  1556. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  1557. kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
  1558. if (kobj_path == NULL)
  1559. continue;
  1560. seq_printf(s, "\n %-50s ", kobj_path);
  1561. rtpm_status_str(s, pm_data->dev);
  1562. kfree(kobj_path);
  1563. }
  1564. seq_puts(s, "\n");
  1565. exit:
  1566. mutex_unlock(&genpd->lock);
  1567. return 0;
  1568. }
  1569. static int pm_genpd_summary_show(struct seq_file *s, void *data)
  1570. {
  1571. struct generic_pm_domain *genpd;
  1572. int ret = 0;
  1573. seq_puts(s, "domain status slaves\n");
  1574. seq_puts(s, " /device runtime status\n");
  1575. seq_puts(s, "----------------------------------------------------------------------\n");
  1576. ret = mutex_lock_interruptible(&gpd_list_lock);
  1577. if (ret)
  1578. return -ERESTARTSYS;
  1579. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  1580. ret = pm_genpd_summary_one(s, genpd);
  1581. if (ret)
  1582. break;
  1583. }
  1584. mutex_unlock(&gpd_list_lock);
  1585. return ret;
  1586. }
  1587. static int pm_genpd_summary_open(struct inode *inode, struct file *file)
  1588. {
  1589. return single_open(file, pm_genpd_summary_show, NULL);
  1590. }
  1591. static const struct file_operations pm_genpd_summary_fops = {
  1592. .open = pm_genpd_summary_open,
  1593. .read = seq_read,
  1594. .llseek = seq_lseek,
  1595. .release = single_release,
  1596. };
  1597. static int __init pm_genpd_debug_init(void)
  1598. {
  1599. struct dentry *d;
  1600. pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
  1601. if (!pm_genpd_debugfs_dir)
  1602. return -ENOMEM;
  1603. d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
  1604. pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
  1605. if (!d)
  1606. return -ENOMEM;
  1607. return 0;
  1608. }
  1609. late_initcall(pm_genpd_debug_init);
  1610. static void __exit pm_genpd_debug_exit(void)
  1611. {
  1612. debugfs_remove_recursive(pm_genpd_debugfs_dir);
  1613. }
  1614. __exitcall(pm_genpd_debug_exit);
  1615. #endif /* CONFIG_PM_ADVANCED_DEBUG */