domain.c 51 KB

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