domain.c 51 KB

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