domain.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  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_opp.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/pm_clock.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/sched.h>
  20. #include <linux/suspend.h>
  21. #include <linux/export.h>
  22. #include "power.h"
  23. #define GENPD_RETRY_MAX_MS 250 /* Approximate */
  24. #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
  25. ({ \
  26. type (*__routine)(struct device *__d); \
  27. type __ret = (type)0; \
  28. \
  29. __routine = genpd->dev_ops.callback; \
  30. if (__routine) { \
  31. __ret = __routine(dev); \
  32. } \
  33. __ret; \
  34. })
  35. static LIST_HEAD(gpd_list);
  36. static DEFINE_MUTEX(gpd_list_lock);
  37. struct genpd_lock_ops {
  38. void (*lock)(struct generic_pm_domain *genpd);
  39. void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
  40. int (*lock_interruptible)(struct generic_pm_domain *genpd);
  41. void (*unlock)(struct generic_pm_domain *genpd);
  42. };
  43. static void genpd_lock_mtx(struct generic_pm_domain *genpd)
  44. {
  45. mutex_lock(&genpd->mlock);
  46. }
  47. static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
  48. int depth)
  49. {
  50. mutex_lock_nested(&genpd->mlock, depth);
  51. }
  52. static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
  53. {
  54. return mutex_lock_interruptible(&genpd->mlock);
  55. }
  56. static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
  57. {
  58. return mutex_unlock(&genpd->mlock);
  59. }
  60. static const struct genpd_lock_ops genpd_mtx_ops = {
  61. .lock = genpd_lock_mtx,
  62. .lock_nested = genpd_lock_nested_mtx,
  63. .lock_interruptible = genpd_lock_interruptible_mtx,
  64. .unlock = genpd_unlock_mtx,
  65. };
  66. static void genpd_lock_spin(struct generic_pm_domain *genpd)
  67. __acquires(&genpd->slock)
  68. {
  69. unsigned long flags;
  70. spin_lock_irqsave(&genpd->slock, flags);
  71. genpd->lock_flags = flags;
  72. }
  73. static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
  74. int depth)
  75. __acquires(&genpd->slock)
  76. {
  77. unsigned long flags;
  78. spin_lock_irqsave_nested(&genpd->slock, flags, depth);
  79. genpd->lock_flags = flags;
  80. }
  81. static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
  82. __acquires(&genpd->slock)
  83. {
  84. unsigned long flags;
  85. spin_lock_irqsave(&genpd->slock, flags);
  86. genpd->lock_flags = flags;
  87. return 0;
  88. }
  89. static void genpd_unlock_spin(struct generic_pm_domain *genpd)
  90. __releases(&genpd->slock)
  91. {
  92. spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
  93. }
  94. static const struct genpd_lock_ops genpd_spin_ops = {
  95. .lock = genpd_lock_spin,
  96. .lock_nested = genpd_lock_nested_spin,
  97. .lock_interruptible = genpd_lock_interruptible_spin,
  98. .unlock = genpd_unlock_spin,
  99. };
  100. #define genpd_lock(p) p->lock_ops->lock(p)
  101. #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
  102. #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
  103. #define genpd_unlock(p) p->lock_ops->unlock(p)
  104. #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
  105. #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
  106. #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
  107. #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
  108. static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
  109. const struct generic_pm_domain *genpd)
  110. {
  111. bool ret;
  112. ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
  113. /*
  114. * Warn once if an IRQ safe device is attached to a no sleep domain, as
  115. * to indicate a suboptimal configuration for PM. For an always on
  116. * domain this isn't case, thus don't warn.
  117. */
  118. if (ret && !genpd_is_always_on(genpd))
  119. dev_warn_once(dev, "PM domain %s will not be powered off\n",
  120. genpd->name);
  121. return ret;
  122. }
  123. /*
  124. * Get the generic PM domain for a particular struct device.
  125. * This validates the struct device pointer, the PM domain pointer,
  126. * and checks that the PM domain pointer is a real generic PM domain.
  127. * Any failure results in NULL being returned.
  128. */
  129. static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
  130. {
  131. struct generic_pm_domain *genpd = NULL, *gpd;
  132. if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
  133. return NULL;
  134. mutex_lock(&gpd_list_lock);
  135. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  136. if (&gpd->domain == dev->pm_domain) {
  137. genpd = gpd;
  138. break;
  139. }
  140. }
  141. mutex_unlock(&gpd_list_lock);
  142. return genpd;
  143. }
  144. /*
  145. * This should only be used where we are certain that the pm_domain
  146. * attached to the device is a genpd domain.
  147. */
  148. static struct generic_pm_domain *dev_to_genpd(struct device *dev)
  149. {
  150. if (IS_ERR_OR_NULL(dev->pm_domain))
  151. return ERR_PTR(-EINVAL);
  152. return pd_to_genpd(dev->pm_domain);
  153. }
  154. static int genpd_stop_dev(const struct generic_pm_domain *genpd,
  155. struct device *dev)
  156. {
  157. return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
  158. }
  159. static int genpd_start_dev(const struct generic_pm_domain *genpd,
  160. struct device *dev)
  161. {
  162. return GENPD_DEV_CALLBACK(genpd, int, start, dev);
  163. }
  164. static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  165. {
  166. bool ret = false;
  167. if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  168. ret = !!atomic_dec_and_test(&genpd->sd_count);
  169. return ret;
  170. }
  171. static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
  172. {
  173. atomic_inc(&genpd->sd_count);
  174. smp_mb__after_atomic();
  175. }
  176. #ifdef CONFIG_DEBUG_FS
  177. static void genpd_update_accounting(struct generic_pm_domain *genpd)
  178. {
  179. ktime_t delta, now;
  180. now = ktime_get();
  181. delta = ktime_sub(now, genpd->accounting_time);
  182. /*
  183. * If genpd->status is active, it means we are just
  184. * out of off and so update the idle time and vice
  185. * versa.
  186. */
  187. if (genpd->status == GPD_STATE_ACTIVE) {
  188. int state_idx = genpd->state_idx;
  189. genpd->states[state_idx].idle_time =
  190. ktime_add(genpd->states[state_idx].idle_time, delta);
  191. } else {
  192. genpd->on_time = ktime_add(genpd->on_time, delta);
  193. }
  194. genpd->accounting_time = now;
  195. }
  196. #else
  197. static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
  198. #endif
  199. /**
  200. * dev_pm_genpd_set_performance_state- Set performance state of device's power
  201. * domain.
  202. *
  203. * @dev: Device for which the performance-state needs to be set.
  204. * @state: Target performance state of the device. This can be set as 0 when the
  205. * device doesn't have any performance state constraints left (And so
  206. * the device wouldn't participate anymore to find the target
  207. * performance state of the genpd).
  208. *
  209. * It is assumed that the users guarantee that the genpd wouldn't be detached
  210. * while this routine is getting called.
  211. *
  212. * Returns 0 on success and negative error values on failures.
  213. */
  214. int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
  215. {
  216. struct generic_pm_domain *genpd;
  217. struct generic_pm_domain_data *gpd_data, *pd_data;
  218. struct pm_domain_data *pdd;
  219. unsigned int prev;
  220. int ret = 0;
  221. genpd = dev_to_genpd(dev);
  222. if (IS_ERR(genpd))
  223. return -ENODEV;
  224. if (unlikely(!genpd->set_performance_state))
  225. return -EINVAL;
  226. if (unlikely(!dev->power.subsys_data ||
  227. !dev->power.subsys_data->domain_data)) {
  228. WARN_ON(1);
  229. return -EINVAL;
  230. }
  231. genpd_lock(genpd);
  232. gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
  233. prev = gpd_data->performance_state;
  234. gpd_data->performance_state = state;
  235. /* New requested state is same as Max requested state */
  236. if (state == genpd->performance_state)
  237. goto unlock;
  238. /* New requested state is higher than Max requested state */
  239. if (state > genpd->performance_state)
  240. goto update_state;
  241. /* Traverse all devices within the domain */
  242. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  243. pd_data = to_gpd_data(pdd);
  244. if (pd_data->performance_state > state)
  245. state = pd_data->performance_state;
  246. }
  247. if (state == genpd->performance_state)
  248. goto unlock;
  249. /*
  250. * We aren't propagating performance state changes of a subdomain to its
  251. * masters as we don't have hardware that needs it. Over that, the
  252. * performance states of subdomain and its masters may not have
  253. * one-to-one mapping and would require additional information. We can
  254. * get back to this once we have hardware that needs it. For that
  255. * reason, we don't have to consider performance state of the subdomains
  256. * of genpd here.
  257. */
  258. update_state:
  259. if (genpd_status_on(genpd)) {
  260. ret = genpd->set_performance_state(genpd, state);
  261. if (ret) {
  262. gpd_data->performance_state = prev;
  263. goto unlock;
  264. }
  265. }
  266. genpd->performance_state = state;
  267. unlock:
  268. genpd_unlock(genpd);
  269. return ret;
  270. }
  271. EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
  272. static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
  273. {
  274. unsigned int state_idx = genpd->state_idx;
  275. ktime_t time_start;
  276. s64 elapsed_ns;
  277. int ret;
  278. if (!genpd->power_on)
  279. return 0;
  280. if (!timed)
  281. return genpd->power_on(genpd);
  282. time_start = ktime_get();
  283. ret = genpd->power_on(genpd);
  284. if (ret)
  285. return ret;
  286. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  287. if (unlikely(genpd->set_performance_state)) {
  288. ret = genpd->set_performance_state(genpd, genpd->performance_state);
  289. if (ret) {
  290. pr_warn("%s: Failed to set performance state %d (%d)\n",
  291. genpd->name, genpd->performance_state, ret);
  292. }
  293. }
  294. if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
  295. return ret;
  296. genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
  297. genpd->max_off_time_changed = true;
  298. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  299. genpd->name, "on", elapsed_ns);
  300. return ret;
  301. }
  302. static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
  303. {
  304. unsigned int state_idx = genpd->state_idx;
  305. ktime_t time_start;
  306. s64 elapsed_ns;
  307. int ret;
  308. if (!genpd->power_off)
  309. return 0;
  310. if (!timed)
  311. return genpd->power_off(genpd);
  312. time_start = ktime_get();
  313. ret = genpd->power_off(genpd);
  314. if (ret == -EBUSY)
  315. return ret;
  316. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  317. if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
  318. return ret;
  319. genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
  320. genpd->max_off_time_changed = true;
  321. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  322. genpd->name, "off", elapsed_ns);
  323. return ret;
  324. }
  325. /**
  326. * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
  327. * @genpd: PM domain to power off.
  328. *
  329. * Queue up the execution of genpd_power_off() unless it's already been done
  330. * before.
  331. */
  332. static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  333. {
  334. queue_work(pm_wq, &genpd->power_off_work);
  335. }
  336. /**
  337. * genpd_power_off - Remove power from a given PM domain.
  338. * @genpd: PM domain to power down.
  339. * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
  340. * RPM status of the releated device is in an intermediate state, not yet turned
  341. * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
  342. * be RPM_SUSPENDED, while it tries to power off the PM domain.
  343. *
  344. * If all of the @genpd's devices have been suspended and all of its subdomains
  345. * have been powered down, remove power from @genpd.
  346. */
  347. static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
  348. unsigned int depth)
  349. {
  350. struct pm_domain_data *pdd;
  351. struct gpd_link *link;
  352. unsigned int not_suspended = 0;
  353. /*
  354. * Do not try to power off the domain in the following situations:
  355. * (1) The domain is already in the "power off" state.
  356. * (2) System suspend is in progress.
  357. */
  358. if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
  359. return 0;
  360. /*
  361. * Abort power off for the PM domain in the following situations:
  362. * (1) The domain is configured as always on.
  363. * (2) When the domain has a subdomain being powered on.
  364. */
  365. if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
  366. return -EBUSY;
  367. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  368. enum pm_qos_flags_status stat;
  369. stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
  370. if (stat > PM_QOS_FLAGS_NONE)
  371. return -EBUSY;
  372. /*
  373. * Do not allow PM domain to be powered off, when an IRQ safe
  374. * device is part of a non-IRQ safe domain.
  375. */
  376. if (!pm_runtime_suspended(pdd->dev) ||
  377. irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
  378. not_suspended++;
  379. }
  380. if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
  381. return -EBUSY;
  382. if (genpd->gov && genpd->gov->power_down_ok) {
  383. if (!genpd->gov->power_down_ok(&genpd->domain))
  384. return -EAGAIN;
  385. }
  386. if (genpd->power_off) {
  387. int ret;
  388. if (atomic_read(&genpd->sd_count) > 0)
  389. return -EBUSY;
  390. /*
  391. * If sd_count > 0 at this point, one of the subdomains hasn't
  392. * managed to call genpd_power_on() for the master yet after
  393. * incrementing it. In that case genpd_power_on() will wait
  394. * for us to drop the lock, so we can call .power_off() and let
  395. * the genpd_power_on() restore power for us (this shouldn't
  396. * happen very often).
  397. */
  398. ret = _genpd_power_off(genpd, true);
  399. if (ret)
  400. return ret;
  401. }
  402. genpd->status = GPD_STATE_POWER_OFF;
  403. genpd_update_accounting(genpd);
  404. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  405. genpd_sd_counter_dec(link->master);
  406. genpd_lock_nested(link->master, depth + 1);
  407. genpd_power_off(link->master, false, depth + 1);
  408. genpd_unlock(link->master);
  409. }
  410. return 0;
  411. }
  412. /**
  413. * genpd_power_on - Restore power to a given PM domain and its masters.
  414. * @genpd: PM domain to power up.
  415. * @depth: nesting count for lockdep.
  416. *
  417. * Restore power to @genpd and all of its masters so that it is possible to
  418. * resume a device belonging to it.
  419. */
  420. static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
  421. {
  422. struct gpd_link *link;
  423. int ret = 0;
  424. if (genpd_status_on(genpd))
  425. return 0;
  426. /*
  427. * The list is guaranteed not to change while the loop below is being
  428. * executed, unless one of the masters' .power_on() callbacks fiddles
  429. * with it.
  430. */
  431. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  432. struct generic_pm_domain *master = link->master;
  433. genpd_sd_counter_inc(master);
  434. genpd_lock_nested(master, depth + 1);
  435. ret = genpd_power_on(master, depth + 1);
  436. genpd_unlock(master);
  437. if (ret) {
  438. genpd_sd_counter_dec(master);
  439. goto err;
  440. }
  441. }
  442. ret = _genpd_power_on(genpd, true);
  443. if (ret)
  444. goto err;
  445. genpd->status = GPD_STATE_ACTIVE;
  446. genpd_update_accounting(genpd);
  447. return 0;
  448. err:
  449. list_for_each_entry_continue_reverse(link,
  450. &genpd->slave_links,
  451. slave_node) {
  452. genpd_sd_counter_dec(link->master);
  453. genpd_lock_nested(link->master, depth + 1);
  454. genpd_power_off(link->master, false, depth + 1);
  455. genpd_unlock(link->master);
  456. }
  457. return ret;
  458. }
  459. static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  460. unsigned long val, void *ptr)
  461. {
  462. struct generic_pm_domain_data *gpd_data;
  463. struct device *dev;
  464. gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
  465. dev = gpd_data->base.dev;
  466. for (;;) {
  467. struct generic_pm_domain *genpd;
  468. struct pm_domain_data *pdd;
  469. spin_lock_irq(&dev->power.lock);
  470. pdd = dev->power.subsys_data ?
  471. dev->power.subsys_data->domain_data : NULL;
  472. if (pdd) {
  473. to_gpd_data(pdd)->td.constraint_changed = true;
  474. genpd = dev_to_genpd(dev);
  475. } else {
  476. genpd = ERR_PTR(-ENODATA);
  477. }
  478. spin_unlock_irq(&dev->power.lock);
  479. if (!IS_ERR(genpd)) {
  480. genpd_lock(genpd);
  481. genpd->max_off_time_changed = true;
  482. genpd_unlock(genpd);
  483. }
  484. dev = dev->parent;
  485. if (!dev || dev->power.ignore_children)
  486. break;
  487. }
  488. return NOTIFY_DONE;
  489. }
  490. /**
  491. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  492. * @work: Work structure used for scheduling the execution of this function.
  493. */
  494. static void genpd_power_off_work_fn(struct work_struct *work)
  495. {
  496. struct generic_pm_domain *genpd;
  497. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  498. genpd_lock(genpd);
  499. genpd_power_off(genpd, false, 0);
  500. genpd_unlock(genpd);
  501. }
  502. /**
  503. * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
  504. * @dev: Device to handle.
  505. */
  506. static int __genpd_runtime_suspend(struct device *dev)
  507. {
  508. int (*cb)(struct device *__dev);
  509. if (dev->type && dev->type->pm)
  510. cb = dev->type->pm->runtime_suspend;
  511. else if (dev->class && dev->class->pm)
  512. cb = dev->class->pm->runtime_suspend;
  513. else if (dev->bus && dev->bus->pm)
  514. cb = dev->bus->pm->runtime_suspend;
  515. else
  516. cb = NULL;
  517. if (!cb && dev->driver && dev->driver->pm)
  518. cb = dev->driver->pm->runtime_suspend;
  519. return cb ? cb(dev) : 0;
  520. }
  521. /**
  522. * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
  523. * @dev: Device to handle.
  524. */
  525. static int __genpd_runtime_resume(struct device *dev)
  526. {
  527. int (*cb)(struct device *__dev);
  528. if (dev->type && dev->type->pm)
  529. cb = dev->type->pm->runtime_resume;
  530. else if (dev->class && dev->class->pm)
  531. cb = dev->class->pm->runtime_resume;
  532. else if (dev->bus && dev->bus->pm)
  533. cb = dev->bus->pm->runtime_resume;
  534. else
  535. cb = NULL;
  536. if (!cb && dev->driver && dev->driver->pm)
  537. cb = dev->driver->pm->runtime_resume;
  538. return cb ? cb(dev) : 0;
  539. }
  540. /**
  541. * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  542. * @dev: Device to suspend.
  543. *
  544. * Carry out a runtime suspend of a device under the assumption that its
  545. * pm_domain field points to the domain member of an object of type
  546. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  547. */
  548. static int genpd_runtime_suspend(struct device *dev)
  549. {
  550. struct generic_pm_domain *genpd;
  551. bool (*suspend_ok)(struct device *__dev);
  552. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  553. bool runtime_pm = pm_runtime_enabled(dev);
  554. ktime_t time_start;
  555. s64 elapsed_ns;
  556. int ret;
  557. dev_dbg(dev, "%s()\n", __func__);
  558. genpd = dev_to_genpd(dev);
  559. if (IS_ERR(genpd))
  560. return -EINVAL;
  561. /*
  562. * A runtime PM centric subsystem/driver may re-use the runtime PM
  563. * callbacks for other purposes than runtime PM. In those scenarios
  564. * runtime PM is disabled. Under these circumstances, we shall skip
  565. * validating/measuring the PM QoS latency.
  566. */
  567. suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
  568. if (runtime_pm && suspend_ok && !suspend_ok(dev))
  569. return -EBUSY;
  570. /* Measure suspend latency. */
  571. time_start = 0;
  572. if (runtime_pm)
  573. time_start = ktime_get();
  574. ret = __genpd_runtime_suspend(dev);
  575. if (ret)
  576. return ret;
  577. ret = genpd_stop_dev(genpd, dev);
  578. if (ret) {
  579. __genpd_runtime_resume(dev);
  580. return ret;
  581. }
  582. /* Update suspend latency value if the measured time exceeds it. */
  583. if (runtime_pm) {
  584. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  585. if (elapsed_ns > td->suspend_latency_ns) {
  586. td->suspend_latency_ns = elapsed_ns;
  587. dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
  588. elapsed_ns);
  589. genpd->max_off_time_changed = true;
  590. td->constraint_changed = true;
  591. }
  592. }
  593. /*
  594. * If power.irq_safe is set, this routine may be run with
  595. * IRQs disabled, so suspend only if the PM domain also is irq_safe.
  596. */
  597. if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
  598. return 0;
  599. genpd_lock(genpd);
  600. genpd_power_off(genpd, true, 0);
  601. genpd_unlock(genpd);
  602. return 0;
  603. }
  604. /**
  605. * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  606. * @dev: Device to resume.
  607. *
  608. * Carry out a runtime resume of a device under the assumption that its
  609. * pm_domain field points to the domain member of an object of type
  610. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  611. */
  612. static int genpd_runtime_resume(struct device *dev)
  613. {
  614. struct generic_pm_domain *genpd;
  615. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  616. bool runtime_pm = pm_runtime_enabled(dev);
  617. ktime_t time_start;
  618. s64 elapsed_ns;
  619. int ret;
  620. bool timed = true;
  621. dev_dbg(dev, "%s()\n", __func__);
  622. genpd = dev_to_genpd(dev);
  623. if (IS_ERR(genpd))
  624. return -EINVAL;
  625. /*
  626. * As we don't power off a non IRQ safe domain, which holds
  627. * an IRQ safe device, we don't need to restore power to it.
  628. */
  629. if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
  630. timed = false;
  631. goto out;
  632. }
  633. genpd_lock(genpd);
  634. ret = genpd_power_on(genpd, 0);
  635. genpd_unlock(genpd);
  636. if (ret)
  637. return ret;
  638. out:
  639. /* Measure resume latency. */
  640. time_start = 0;
  641. if (timed && runtime_pm)
  642. time_start = ktime_get();
  643. ret = genpd_start_dev(genpd, dev);
  644. if (ret)
  645. goto err_poweroff;
  646. ret = __genpd_runtime_resume(dev);
  647. if (ret)
  648. goto err_stop;
  649. /* Update resume latency value if the measured time exceeds it. */
  650. if (timed && runtime_pm) {
  651. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  652. if (elapsed_ns > td->resume_latency_ns) {
  653. td->resume_latency_ns = elapsed_ns;
  654. dev_dbg(dev, "resume latency exceeded, %lld ns\n",
  655. elapsed_ns);
  656. genpd->max_off_time_changed = true;
  657. td->constraint_changed = true;
  658. }
  659. }
  660. return 0;
  661. err_stop:
  662. genpd_stop_dev(genpd, dev);
  663. err_poweroff:
  664. if (!pm_runtime_is_irq_safe(dev) ||
  665. (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
  666. genpd_lock(genpd);
  667. genpd_power_off(genpd, true, 0);
  668. genpd_unlock(genpd);
  669. }
  670. return ret;
  671. }
  672. static bool pd_ignore_unused;
  673. static int __init pd_ignore_unused_setup(char *__unused)
  674. {
  675. pd_ignore_unused = true;
  676. return 1;
  677. }
  678. __setup("pd_ignore_unused", pd_ignore_unused_setup);
  679. /**
  680. * genpd_power_off_unused - Power off all PM domains with no devices in use.
  681. */
  682. static int __init genpd_power_off_unused(void)
  683. {
  684. struct generic_pm_domain *genpd;
  685. if (pd_ignore_unused) {
  686. pr_warn("genpd: Not disabling unused power domains\n");
  687. return 0;
  688. }
  689. mutex_lock(&gpd_list_lock);
  690. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  691. genpd_queue_power_off_work(genpd);
  692. mutex_unlock(&gpd_list_lock);
  693. return 0;
  694. }
  695. late_initcall(genpd_power_off_unused);
  696. #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
  697. static bool genpd_present(const struct generic_pm_domain *genpd)
  698. {
  699. const struct generic_pm_domain *gpd;
  700. if (IS_ERR_OR_NULL(genpd))
  701. return false;
  702. list_for_each_entry(gpd, &gpd_list, gpd_list_node)
  703. if (gpd == genpd)
  704. return true;
  705. return false;
  706. }
  707. #endif
  708. #ifdef CONFIG_PM_SLEEP
  709. /**
  710. * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
  711. * @genpd: PM domain to power off, if possible.
  712. * @use_lock: use the lock.
  713. * @depth: nesting count for lockdep.
  714. *
  715. * Check if the given PM domain can be powered off (during system suspend or
  716. * hibernation) and do that if so. Also, in that case propagate to its masters.
  717. *
  718. * This function is only called in "noirq" and "syscore" stages of system power
  719. * transitions. The "noirq" callbacks may be executed asynchronously, thus in
  720. * these cases the lock must be held.
  721. */
  722. static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
  723. unsigned int depth)
  724. {
  725. struct gpd_link *link;
  726. if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
  727. return;
  728. if (genpd->suspended_count != genpd->device_count
  729. || atomic_read(&genpd->sd_count) > 0)
  730. return;
  731. /* Choose the deepest state when suspending */
  732. genpd->state_idx = genpd->state_count - 1;
  733. if (_genpd_power_off(genpd, false))
  734. return;
  735. genpd->status = GPD_STATE_POWER_OFF;
  736. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  737. genpd_sd_counter_dec(link->master);
  738. if (use_lock)
  739. genpd_lock_nested(link->master, depth + 1);
  740. genpd_sync_power_off(link->master, use_lock, depth + 1);
  741. if (use_lock)
  742. genpd_unlock(link->master);
  743. }
  744. }
  745. /**
  746. * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
  747. * @genpd: PM domain to power on.
  748. * @use_lock: use the lock.
  749. * @depth: nesting count for lockdep.
  750. *
  751. * This function is only called in "noirq" and "syscore" stages of system power
  752. * transitions. The "noirq" callbacks may be executed asynchronously, thus in
  753. * these cases the lock must be held.
  754. */
  755. static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
  756. unsigned int depth)
  757. {
  758. struct gpd_link *link;
  759. if (genpd_status_on(genpd))
  760. return;
  761. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  762. genpd_sd_counter_inc(link->master);
  763. if (use_lock)
  764. genpd_lock_nested(link->master, depth + 1);
  765. genpd_sync_power_on(link->master, use_lock, depth + 1);
  766. if (use_lock)
  767. genpd_unlock(link->master);
  768. }
  769. _genpd_power_on(genpd, false);
  770. genpd->status = GPD_STATE_ACTIVE;
  771. }
  772. /**
  773. * resume_needed - Check whether to resume a device before system suspend.
  774. * @dev: Device to check.
  775. * @genpd: PM domain the device belongs to.
  776. *
  777. * There are two cases in which a device that can wake up the system from sleep
  778. * states should be resumed by genpd_prepare(): (1) if the device is enabled
  779. * to wake up the system and it has to remain active for this purpose while the
  780. * system is in the sleep state and (2) if the device is not enabled to wake up
  781. * the system from sleep states and it generally doesn't generate wakeup signals
  782. * by itself (those signals are generated on its behalf by other parts of the
  783. * system). In the latter case it may be necessary to reconfigure the device's
  784. * wakeup settings during system suspend, because it may have been set up to
  785. * signal remote wakeup from the system's working state as needed by runtime PM.
  786. * Return 'true' in either of the above cases.
  787. */
  788. static bool resume_needed(struct device *dev,
  789. const struct generic_pm_domain *genpd)
  790. {
  791. bool active_wakeup;
  792. if (!device_can_wakeup(dev))
  793. return false;
  794. active_wakeup = genpd_is_active_wakeup(genpd);
  795. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  796. }
  797. /**
  798. * genpd_prepare - Start power transition of a device in a PM domain.
  799. * @dev: Device to start the transition of.
  800. *
  801. * Start a power transition of a device (during a system-wide power transition)
  802. * under the assumption that its pm_domain field points to the domain member of
  803. * an object of type struct generic_pm_domain representing a PM domain
  804. * consisting of I/O devices.
  805. */
  806. static int genpd_prepare(struct device *dev)
  807. {
  808. struct generic_pm_domain *genpd;
  809. int ret;
  810. dev_dbg(dev, "%s()\n", __func__);
  811. genpd = dev_to_genpd(dev);
  812. if (IS_ERR(genpd))
  813. return -EINVAL;
  814. /*
  815. * If a wakeup request is pending for the device, it should be woken up
  816. * at this point and a system wakeup event should be reported if it's
  817. * set up to wake up the system from sleep states.
  818. */
  819. if (resume_needed(dev, genpd))
  820. pm_runtime_resume(dev);
  821. genpd_lock(genpd);
  822. if (genpd->prepared_count++ == 0)
  823. genpd->suspended_count = 0;
  824. genpd_unlock(genpd);
  825. ret = pm_generic_prepare(dev);
  826. if (ret < 0) {
  827. genpd_lock(genpd);
  828. genpd->prepared_count--;
  829. genpd_unlock(genpd);
  830. }
  831. /* Never return 1, as genpd don't cope with the direct_complete path. */
  832. return ret >= 0 ? 0 : ret;
  833. }
  834. /**
  835. * genpd_finish_suspend - Completion of suspend or hibernation of device in an
  836. * I/O pm domain.
  837. * @dev: Device to suspend.
  838. * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
  839. *
  840. * Stop the device and remove power from the domain if all devices in it have
  841. * been stopped.
  842. */
  843. static int genpd_finish_suspend(struct device *dev, bool poweroff)
  844. {
  845. struct generic_pm_domain *genpd;
  846. int ret = 0;
  847. genpd = dev_to_genpd(dev);
  848. if (IS_ERR(genpd))
  849. return -EINVAL;
  850. if (poweroff)
  851. ret = pm_generic_poweroff_noirq(dev);
  852. else
  853. ret = pm_generic_suspend_noirq(dev);
  854. if (ret)
  855. return ret;
  856. if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
  857. return 0;
  858. if (genpd->dev_ops.stop && genpd->dev_ops.start &&
  859. !pm_runtime_status_suspended(dev)) {
  860. ret = genpd_stop_dev(genpd, dev);
  861. if (ret) {
  862. if (poweroff)
  863. pm_generic_restore_noirq(dev);
  864. else
  865. pm_generic_resume_noirq(dev);
  866. return ret;
  867. }
  868. }
  869. genpd_lock(genpd);
  870. genpd->suspended_count++;
  871. genpd_sync_power_off(genpd, true, 0);
  872. genpd_unlock(genpd);
  873. return 0;
  874. }
  875. /**
  876. * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  877. * @dev: Device to suspend.
  878. *
  879. * Stop the device and remove power from the domain if all devices in it have
  880. * been stopped.
  881. */
  882. static int genpd_suspend_noirq(struct device *dev)
  883. {
  884. dev_dbg(dev, "%s()\n", __func__);
  885. return genpd_finish_suspend(dev, false);
  886. }
  887. /**
  888. * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  889. * @dev: Device to resume.
  890. *
  891. * Restore power to the device's PM domain, if necessary, and start the device.
  892. */
  893. static int genpd_resume_noirq(struct device *dev)
  894. {
  895. struct generic_pm_domain *genpd;
  896. int ret;
  897. dev_dbg(dev, "%s()\n", __func__);
  898. genpd = dev_to_genpd(dev);
  899. if (IS_ERR(genpd))
  900. return -EINVAL;
  901. if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
  902. return pm_generic_resume_noirq(dev);
  903. genpd_lock(genpd);
  904. genpd_sync_power_on(genpd, true, 0);
  905. genpd->suspended_count--;
  906. genpd_unlock(genpd);
  907. if (genpd->dev_ops.stop && genpd->dev_ops.start &&
  908. !pm_runtime_status_suspended(dev)) {
  909. ret = genpd_start_dev(genpd, dev);
  910. if (ret)
  911. return ret;
  912. }
  913. return pm_generic_resume_noirq(dev);
  914. }
  915. /**
  916. * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  917. * @dev: Device to freeze.
  918. *
  919. * Carry out a late freeze of a device under the assumption that its
  920. * pm_domain field points to the domain member of an object of type
  921. * struct generic_pm_domain representing a power domain consisting of I/O
  922. * devices.
  923. */
  924. static int genpd_freeze_noirq(struct device *dev)
  925. {
  926. const struct generic_pm_domain *genpd;
  927. int ret = 0;
  928. dev_dbg(dev, "%s()\n", __func__);
  929. genpd = dev_to_genpd(dev);
  930. if (IS_ERR(genpd))
  931. return -EINVAL;
  932. ret = pm_generic_freeze_noirq(dev);
  933. if (ret)
  934. return ret;
  935. if (genpd->dev_ops.stop && genpd->dev_ops.start &&
  936. !pm_runtime_status_suspended(dev))
  937. ret = genpd_stop_dev(genpd, dev);
  938. return ret;
  939. }
  940. /**
  941. * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  942. * @dev: Device to thaw.
  943. *
  944. * Start the device, unless power has been removed from the domain already
  945. * before the system transition.
  946. */
  947. static int genpd_thaw_noirq(struct device *dev)
  948. {
  949. const struct generic_pm_domain *genpd;
  950. int ret = 0;
  951. dev_dbg(dev, "%s()\n", __func__);
  952. genpd = dev_to_genpd(dev);
  953. if (IS_ERR(genpd))
  954. return -EINVAL;
  955. if (genpd->dev_ops.stop && genpd->dev_ops.start &&
  956. !pm_runtime_status_suspended(dev)) {
  957. ret = genpd_start_dev(genpd, dev);
  958. if (ret)
  959. return ret;
  960. }
  961. return pm_generic_thaw_noirq(dev);
  962. }
  963. /**
  964. * genpd_poweroff_noirq - Completion of hibernation of device in an
  965. * I/O PM domain.
  966. * @dev: Device to poweroff.
  967. *
  968. * Stop the device and remove power from the domain if all devices in it have
  969. * been stopped.
  970. */
  971. static int genpd_poweroff_noirq(struct device *dev)
  972. {
  973. dev_dbg(dev, "%s()\n", __func__);
  974. return genpd_finish_suspend(dev, true);
  975. }
  976. /**
  977. * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  978. * @dev: Device to resume.
  979. *
  980. * Make sure the domain will be in the same power state as before the
  981. * hibernation the system is resuming from and start the device if necessary.
  982. */
  983. static int genpd_restore_noirq(struct device *dev)
  984. {
  985. struct generic_pm_domain *genpd;
  986. int ret = 0;
  987. dev_dbg(dev, "%s()\n", __func__);
  988. genpd = dev_to_genpd(dev);
  989. if (IS_ERR(genpd))
  990. return -EINVAL;
  991. /*
  992. * At this point suspended_count == 0 means we are being run for the
  993. * first time for the given domain in the present cycle.
  994. */
  995. genpd_lock(genpd);
  996. if (genpd->suspended_count++ == 0)
  997. /*
  998. * The boot kernel might put the domain into arbitrary state,
  999. * so make it appear as powered off to genpd_sync_power_on(),
  1000. * so that it tries to power it on in case it was really off.
  1001. */
  1002. genpd->status = GPD_STATE_POWER_OFF;
  1003. genpd_sync_power_on(genpd, true, 0);
  1004. genpd_unlock(genpd);
  1005. if (genpd->dev_ops.stop && genpd->dev_ops.start &&
  1006. !pm_runtime_status_suspended(dev)) {
  1007. ret = genpd_start_dev(genpd, dev);
  1008. if (ret)
  1009. return ret;
  1010. }
  1011. return pm_generic_restore_noirq(dev);
  1012. }
  1013. /**
  1014. * genpd_complete - Complete power transition of a device in a power domain.
  1015. * @dev: Device to complete the transition of.
  1016. *
  1017. * Complete a power transition of a device (during a system-wide power
  1018. * transition) under the assumption that its pm_domain field points to the
  1019. * domain member of an object of type struct generic_pm_domain representing
  1020. * a power domain consisting of I/O devices.
  1021. */
  1022. static void genpd_complete(struct device *dev)
  1023. {
  1024. struct generic_pm_domain *genpd;
  1025. dev_dbg(dev, "%s()\n", __func__);
  1026. genpd = dev_to_genpd(dev);
  1027. if (IS_ERR(genpd))
  1028. return;
  1029. pm_generic_complete(dev);
  1030. genpd_lock(genpd);
  1031. genpd->prepared_count--;
  1032. if (!genpd->prepared_count)
  1033. genpd_queue_power_off_work(genpd);
  1034. genpd_unlock(genpd);
  1035. }
  1036. /**
  1037. * genpd_syscore_switch - Switch power during system core suspend or resume.
  1038. * @dev: Device that normally is marked as "always on" to switch power for.
  1039. *
  1040. * This routine may only be called during the system core (syscore) suspend or
  1041. * resume phase for devices whose "always on" flags are set.
  1042. */
  1043. static void genpd_syscore_switch(struct device *dev, bool suspend)
  1044. {
  1045. struct generic_pm_domain *genpd;
  1046. genpd = dev_to_genpd(dev);
  1047. if (!genpd_present(genpd))
  1048. return;
  1049. if (suspend) {
  1050. genpd->suspended_count++;
  1051. genpd_sync_power_off(genpd, false, 0);
  1052. } else {
  1053. genpd_sync_power_on(genpd, false, 0);
  1054. genpd->suspended_count--;
  1055. }
  1056. }
  1057. void pm_genpd_syscore_poweroff(struct device *dev)
  1058. {
  1059. genpd_syscore_switch(dev, true);
  1060. }
  1061. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
  1062. void pm_genpd_syscore_poweron(struct device *dev)
  1063. {
  1064. genpd_syscore_switch(dev, false);
  1065. }
  1066. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
  1067. #else /* !CONFIG_PM_SLEEP */
  1068. #define genpd_prepare NULL
  1069. #define genpd_suspend_noirq NULL
  1070. #define genpd_resume_noirq NULL
  1071. #define genpd_freeze_noirq NULL
  1072. #define genpd_thaw_noirq NULL
  1073. #define genpd_poweroff_noirq NULL
  1074. #define genpd_restore_noirq NULL
  1075. #define genpd_complete NULL
  1076. #endif /* CONFIG_PM_SLEEP */
  1077. static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
  1078. struct gpd_timing_data *td)
  1079. {
  1080. struct generic_pm_domain_data *gpd_data;
  1081. int ret;
  1082. ret = dev_pm_get_subsys_data(dev);
  1083. if (ret)
  1084. return ERR_PTR(ret);
  1085. gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
  1086. if (!gpd_data) {
  1087. ret = -ENOMEM;
  1088. goto err_put;
  1089. }
  1090. if (td)
  1091. gpd_data->td = *td;
  1092. gpd_data->base.dev = dev;
  1093. gpd_data->td.constraint_changed = true;
  1094. gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
  1095. gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
  1096. spin_lock_irq(&dev->power.lock);
  1097. if (dev->power.subsys_data->domain_data) {
  1098. ret = -EINVAL;
  1099. goto err_free;
  1100. }
  1101. dev->power.subsys_data->domain_data = &gpd_data->base;
  1102. spin_unlock_irq(&dev->power.lock);
  1103. return gpd_data;
  1104. err_free:
  1105. spin_unlock_irq(&dev->power.lock);
  1106. kfree(gpd_data);
  1107. err_put:
  1108. dev_pm_put_subsys_data(dev);
  1109. return ERR_PTR(ret);
  1110. }
  1111. static void genpd_free_dev_data(struct device *dev,
  1112. struct generic_pm_domain_data *gpd_data)
  1113. {
  1114. spin_lock_irq(&dev->power.lock);
  1115. dev->power.subsys_data->domain_data = NULL;
  1116. spin_unlock_irq(&dev->power.lock);
  1117. kfree(gpd_data);
  1118. dev_pm_put_subsys_data(dev);
  1119. }
  1120. static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  1121. struct gpd_timing_data *td)
  1122. {
  1123. struct generic_pm_domain_data *gpd_data;
  1124. int ret;
  1125. dev_dbg(dev, "%s()\n", __func__);
  1126. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  1127. return -EINVAL;
  1128. gpd_data = genpd_alloc_dev_data(dev, td);
  1129. if (IS_ERR(gpd_data))
  1130. return PTR_ERR(gpd_data);
  1131. genpd_lock(genpd);
  1132. ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
  1133. if (ret)
  1134. goto out;
  1135. dev_pm_domain_set(dev, &genpd->domain);
  1136. genpd->device_count++;
  1137. genpd->max_off_time_changed = true;
  1138. list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
  1139. out:
  1140. genpd_unlock(genpd);
  1141. if (ret)
  1142. genpd_free_dev_data(dev, gpd_data);
  1143. else
  1144. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1145. return ret;
  1146. }
  1147. /**
  1148. * pm_genpd_add_device - Add a device to an I/O PM domain.
  1149. * @genpd: PM domain to add the device to.
  1150. * @dev: Device to be added.
  1151. */
  1152. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
  1153. {
  1154. int ret;
  1155. mutex_lock(&gpd_list_lock);
  1156. ret = genpd_add_device(genpd, dev, NULL);
  1157. mutex_unlock(&gpd_list_lock);
  1158. return ret;
  1159. }
  1160. EXPORT_SYMBOL_GPL(pm_genpd_add_device);
  1161. static int genpd_remove_device(struct generic_pm_domain *genpd,
  1162. struct device *dev)
  1163. {
  1164. struct generic_pm_domain_data *gpd_data;
  1165. struct pm_domain_data *pdd;
  1166. int ret = 0;
  1167. dev_dbg(dev, "%s()\n", __func__);
  1168. pdd = dev->power.subsys_data->domain_data;
  1169. gpd_data = to_gpd_data(pdd);
  1170. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1171. genpd_lock(genpd);
  1172. if (genpd->prepared_count > 0) {
  1173. ret = -EAGAIN;
  1174. goto out;
  1175. }
  1176. genpd->device_count--;
  1177. genpd->max_off_time_changed = true;
  1178. if (genpd->detach_dev)
  1179. genpd->detach_dev(genpd, dev);
  1180. dev_pm_domain_set(dev, NULL);
  1181. list_del_init(&pdd->list_node);
  1182. genpd_unlock(genpd);
  1183. genpd_free_dev_data(dev, gpd_data);
  1184. return 0;
  1185. out:
  1186. genpd_unlock(genpd);
  1187. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1188. return ret;
  1189. }
  1190. /**
  1191. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  1192. * @dev: Device to be removed.
  1193. */
  1194. int pm_genpd_remove_device(struct device *dev)
  1195. {
  1196. struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
  1197. if (!genpd)
  1198. return -EINVAL;
  1199. return genpd_remove_device(genpd, dev);
  1200. }
  1201. EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
  1202. static int genpd_add_subdomain(struct generic_pm_domain *genpd,
  1203. struct generic_pm_domain *subdomain)
  1204. {
  1205. struct gpd_link *link, *itr;
  1206. int ret = 0;
  1207. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
  1208. || genpd == subdomain)
  1209. return -EINVAL;
  1210. /*
  1211. * If the domain can be powered on/off in an IRQ safe
  1212. * context, ensure that the subdomain can also be
  1213. * powered on/off in that context.
  1214. */
  1215. if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
  1216. WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
  1217. genpd->name, subdomain->name);
  1218. return -EINVAL;
  1219. }
  1220. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1221. if (!link)
  1222. return -ENOMEM;
  1223. genpd_lock(subdomain);
  1224. genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
  1225. if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
  1226. ret = -EINVAL;
  1227. goto out;
  1228. }
  1229. list_for_each_entry(itr, &genpd->master_links, master_node) {
  1230. if (itr->slave == subdomain && itr->master == genpd) {
  1231. ret = -EINVAL;
  1232. goto out;
  1233. }
  1234. }
  1235. link->master = genpd;
  1236. list_add_tail(&link->master_node, &genpd->master_links);
  1237. link->slave = subdomain;
  1238. list_add_tail(&link->slave_node, &subdomain->slave_links);
  1239. if (genpd_status_on(subdomain))
  1240. genpd_sd_counter_inc(genpd);
  1241. out:
  1242. genpd_unlock(genpd);
  1243. genpd_unlock(subdomain);
  1244. if (ret)
  1245. kfree(link);
  1246. return ret;
  1247. }
  1248. /**
  1249. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1250. * @genpd: Master PM domain to add the subdomain to.
  1251. * @subdomain: Subdomain to be added.
  1252. */
  1253. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  1254. struct generic_pm_domain *subdomain)
  1255. {
  1256. int ret;
  1257. mutex_lock(&gpd_list_lock);
  1258. ret = genpd_add_subdomain(genpd, subdomain);
  1259. mutex_unlock(&gpd_list_lock);
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
  1263. /**
  1264. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  1265. * @genpd: Master PM domain to remove the subdomain from.
  1266. * @subdomain: Subdomain to be removed.
  1267. */
  1268. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  1269. struct generic_pm_domain *subdomain)
  1270. {
  1271. struct gpd_link *l, *link;
  1272. int ret = -EINVAL;
  1273. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1274. return -EINVAL;
  1275. genpd_lock(subdomain);
  1276. genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
  1277. if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
  1278. pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
  1279. subdomain->name);
  1280. ret = -EBUSY;
  1281. goto out;
  1282. }
  1283. list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
  1284. if (link->slave != subdomain)
  1285. continue;
  1286. list_del(&link->master_node);
  1287. list_del(&link->slave_node);
  1288. kfree(link);
  1289. if (genpd_status_on(subdomain))
  1290. genpd_sd_counter_dec(genpd);
  1291. ret = 0;
  1292. break;
  1293. }
  1294. out:
  1295. genpd_unlock(genpd);
  1296. genpd_unlock(subdomain);
  1297. return ret;
  1298. }
  1299. EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
  1300. static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
  1301. {
  1302. struct genpd_power_state *state;
  1303. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1304. if (!state)
  1305. return -ENOMEM;
  1306. genpd->states = state;
  1307. genpd->state_count = 1;
  1308. genpd->free = state;
  1309. return 0;
  1310. }
  1311. static void genpd_lock_init(struct generic_pm_domain *genpd)
  1312. {
  1313. if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
  1314. spin_lock_init(&genpd->slock);
  1315. genpd->lock_ops = &genpd_spin_ops;
  1316. } else {
  1317. mutex_init(&genpd->mlock);
  1318. genpd->lock_ops = &genpd_mtx_ops;
  1319. }
  1320. }
  1321. /**
  1322. * pm_genpd_init - Initialize a generic I/O PM domain object.
  1323. * @genpd: PM domain object to initialize.
  1324. * @gov: PM domain governor to associate with the domain (may be NULL).
  1325. * @is_off: Initial value of the domain's power_is_off field.
  1326. *
  1327. * Returns 0 on successful initialization, else a negative error code.
  1328. */
  1329. int pm_genpd_init(struct generic_pm_domain *genpd,
  1330. struct dev_power_governor *gov, bool is_off)
  1331. {
  1332. int ret;
  1333. if (IS_ERR_OR_NULL(genpd))
  1334. return -EINVAL;
  1335. INIT_LIST_HEAD(&genpd->master_links);
  1336. INIT_LIST_HEAD(&genpd->slave_links);
  1337. INIT_LIST_HEAD(&genpd->dev_list);
  1338. genpd_lock_init(genpd);
  1339. genpd->gov = gov;
  1340. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  1341. atomic_set(&genpd->sd_count, 0);
  1342. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  1343. genpd->device_count = 0;
  1344. genpd->max_off_time_ns = -1;
  1345. genpd->max_off_time_changed = true;
  1346. genpd->provider = NULL;
  1347. genpd->has_provider = false;
  1348. genpd->accounting_time = ktime_get();
  1349. genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
  1350. genpd->domain.ops.runtime_resume = genpd_runtime_resume;
  1351. genpd->domain.ops.prepare = genpd_prepare;
  1352. genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
  1353. genpd->domain.ops.resume_noirq = genpd_resume_noirq;
  1354. genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
  1355. genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
  1356. genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
  1357. genpd->domain.ops.restore_noirq = genpd_restore_noirq;
  1358. genpd->domain.ops.complete = genpd_complete;
  1359. if (genpd->flags & GENPD_FLAG_PM_CLK) {
  1360. genpd->dev_ops.stop = pm_clk_suspend;
  1361. genpd->dev_ops.start = pm_clk_resume;
  1362. }
  1363. /* Always-on domains must be powered on at initialization. */
  1364. if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
  1365. return -EINVAL;
  1366. /* Use only one "off" state if there were no states declared */
  1367. if (genpd->state_count == 0) {
  1368. ret = genpd_set_default_power_state(genpd);
  1369. if (ret)
  1370. return ret;
  1371. }
  1372. device_initialize(&genpd->dev);
  1373. dev_set_name(&genpd->dev, "%s", genpd->name);
  1374. mutex_lock(&gpd_list_lock);
  1375. list_add(&genpd->gpd_list_node, &gpd_list);
  1376. mutex_unlock(&gpd_list_lock);
  1377. return 0;
  1378. }
  1379. EXPORT_SYMBOL_GPL(pm_genpd_init);
  1380. static int genpd_remove(struct generic_pm_domain *genpd)
  1381. {
  1382. struct gpd_link *l, *link;
  1383. if (IS_ERR_OR_NULL(genpd))
  1384. return -EINVAL;
  1385. genpd_lock(genpd);
  1386. if (genpd->has_provider) {
  1387. genpd_unlock(genpd);
  1388. pr_err("Provider present, unable to remove %s\n", genpd->name);
  1389. return -EBUSY;
  1390. }
  1391. if (!list_empty(&genpd->master_links) || genpd->device_count) {
  1392. genpd_unlock(genpd);
  1393. pr_err("%s: unable to remove %s\n", __func__, genpd->name);
  1394. return -EBUSY;
  1395. }
  1396. list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
  1397. list_del(&link->master_node);
  1398. list_del(&link->slave_node);
  1399. kfree(link);
  1400. }
  1401. list_del(&genpd->gpd_list_node);
  1402. genpd_unlock(genpd);
  1403. cancel_work_sync(&genpd->power_off_work);
  1404. kfree(genpd->free);
  1405. pr_debug("%s: removed %s\n", __func__, genpd->name);
  1406. return 0;
  1407. }
  1408. /**
  1409. * pm_genpd_remove - Remove a generic I/O PM domain
  1410. * @genpd: Pointer to PM domain that is to be removed.
  1411. *
  1412. * To remove the PM domain, this function:
  1413. * - Removes the PM domain as a subdomain to any parent domains,
  1414. * if it was added.
  1415. * - Removes the PM domain from the list of registered PM domains.
  1416. *
  1417. * The PM domain will only be removed, if the associated provider has
  1418. * been removed, it is not a parent to any other PM domain and has no
  1419. * devices associated with it.
  1420. */
  1421. int pm_genpd_remove(struct generic_pm_domain *genpd)
  1422. {
  1423. int ret;
  1424. mutex_lock(&gpd_list_lock);
  1425. ret = genpd_remove(genpd);
  1426. mutex_unlock(&gpd_list_lock);
  1427. return ret;
  1428. }
  1429. EXPORT_SYMBOL_GPL(pm_genpd_remove);
  1430. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  1431. /*
  1432. * Device Tree based PM domain providers.
  1433. *
  1434. * The code below implements generic device tree based PM domain providers that
  1435. * bind device tree nodes with generic PM domains registered in the system.
  1436. *
  1437. * Any driver that registers generic PM domains and needs to support binding of
  1438. * devices to these domains is supposed to register a PM domain provider, which
  1439. * maps a PM domain specifier retrieved from the device tree to a PM domain.
  1440. *
  1441. * Two simple mapping functions have been provided for convenience:
  1442. * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
  1443. * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
  1444. * index.
  1445. */
  1446. /**
  1447. * struct of_genpd_provider - PM domain provider registration structure
  1448. * @link: Entry in global list of PM domain providers
  1449. * @node: Pointer to device tree node of PM domain provider
  1450. * @xlate: Provider-specific xlate callback mapping a set of specifier cells
  1451. * into a PM domain.
  1452. * @data: context pointer to be passed into @xlate callback
  1453. */
  1454. struct of_genpd_provider {
  1455. struct list_head link;
  1456. struct device_node *node;
  1457. genpd_xlate_t xlate;
  1458. void *data;
  1459. };
  1460. /* List of registered PM domain providers. */
  1461. static LIST_HEAD(of_genpd_providers);
  1462. /* Mutex to protect the list above. */
  1463. static DEFINE_MUTEX(of_genpd_mutex);
  1464. /**
  1465. * genpd_xlate_simple() - Xlate function for direct node-domain mapping
  1466. * @genpdspec: OF phandle args to map into a PM domain
  1467. * @data: xlate function private data - pointer to struct generic_pm_domain
  1468. *
  1469. * This is a generic xlate function that can be used to model PM domains that
  1470. * have their own device tree nodes. The private data of xlate function needs
  1471. * to be a valid pointer to struct generic_pm_domain.
  1472. */
  1473. static struct generic_pm_domain *genpd_xlate_simple(
  1474. struct of_phandle_args *genpdspec,
  1475. void *data)
  1476. {
  1477. return data;
  1478. }
  1479. /**
  1480. * genpd_xlate_onecell() - Xlate function using a single index.
  1481. * @genpdspec: OF phandle args to map into a PM domain
  1482. * @data: xlate function private data - pointer to struct genpd_onecell_data
  1483. *
  1484. * This is a generic xlate function that can be used to model simple PM domain
  1485. * controllers that have one device tree node and provide multiple PM domains.
  1486. * A single cell is used as an index into an array of PM domains specified in
  1487. * the genpd_onecell_data struct when registering the provider.
  1488. */
  1489. static struct generic_pm_domain *genpd_xlate_onecell(
  1490. struct of_phandle_args *genpdspec,
  1491. void *data)
  1492. {
  1493. struct genpd_onecell_data *genpd_data = data;
  1494. unsigned int idx = genpdspec->args[0];
  1495. if (genpdspec->args_count != 1)
  1496. return ERR_PTR(-EINVAL);
  1497. if (idx >= genpd_data->num_domains) {
  1498. pr_err("%s: invalid domain index %u\n", __func__, idx);
  1499. return ERR_PTR(-EINVAL);
  1500. }
  1501. if (!genpd_data->domains[idx])
  1502. return ERR_PTR(-ENOENT);
  1503. return genpd_data->domains[idx];
  1504. }
  1505. /**
  1506. * genpd_add_provider() - Register a PM domain provider for a node
  1507. * @np: Device node pointer associated with the PM domain provider.
  1508. * @xlate: Callback for decoding PM domain from phandle arguments.
  1509. * @data: Context pointer for @xlate callback.
  1510. */
  1511. static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
  1512. void *data)
  1513. {
  1514. struct of_genpd_provider *cp;
  1515. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  1516. if (!cp)
  1517. return -ENOMEM;
  1518. cp->node = of_node_get(np);
  1519. cp->data = data;
  1520. cp->xlate = xlate;
  1521. mutex_lock(&of_genpd_mutex);
  1522. list_add(&cp->link, &of_genpd_providers);
  1523. mutex_unlock(&of_genpd_mutex);
  1524. pr_debug("Added domain provider from %pOF\n", np);
  1525. return 0;
  1526. }
  1527. /**
  1528. * of_genpd_add_provider_simple() - Register a simple PM domain provider
  1529. * @np: Device node pointer associated with the PM domain provider.
  1530. * @genpd: Pointer to PM domain associated with the PM domain provider.
  1531. */
  1532. int of_genpd_add_provider_simple(struct device_node *np,
  1533. struct generic_pm_domain *genpd)
  1534. {
  1535. int ret = -EINVAL;
  1536. if (!np || !genpd)
  1537. return -EINVAL;
  1538. mutex_lock(&gpd_list_lock);
  1539. if (!genpd_present(genpd))
  1540. goto unlock;
  1541. genpd->dev.of_node = np;
  1542. /* Parse genpd OPP table */
  1543. if (genpd->set_performance_state) {
  1544. ret = dev_pm_opp_of_add_table(&genpd->dev);
  1545. if (ret) {
  1546. dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
  1547. ret);
  1548. goto unlock;
  1549. }
  1550. }
  1551. ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
  1552. if (ret) {
  1553. if (genpd->set_performance_state)
  1554. dev_pm_opp_of_remove_table(&genpd->dev);
  1555. goto unlock;
  1556. }
  1557. genpd->provider = &np->fwnode;
  1558. genpd->has_provider = true;
  1559. unlock:
  1560. mutex_unlock(&gpd_list_lock);
  1561. return ret;
  1562. }
  1563. EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
  1564. /**
  1565. * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
  1566. * @np: Device node pointer associated with the PM domain provider.
  1567. * @data: Pointer to the data associated with the PM domain provider.
  1568. */
  1569. int of_genpd_add_provider_onecell(struct device_node *np,
  1570. struct genpd_onecell_data *data)
  1571. {
  1572. struct generic_pm_domain *genpd;
  1573. unsigned int i;
  1574. int ret = -EINVAL;
  1575. if (!np || !data)
  1576. return -EINVAL;
  1577. mutex_lock(&gpd_list_lock);
  1578. if (!data->xlate)
  1579. data->xlate = genpd_xlate_onecell;
  1580. for (i = 0; i < data->num_domains; i++) {
  1581. genpd = data->domains[i];
  1582. if (!genpd)
  1583. continue;
  1584. if (!genpd_present(genpd))
  1585. goto error;
  1586. genpd->dev.of_node = np;
  1587. /* Parse genpd OPP table */
  1588. if (genpd->set_performance_state) {
  1589. ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
  1590. if (ret) {
  1591. dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
  1592. i, ret);
  1593. goto error;
  1594. }
  1595. }
  1596. genpd->provider = &np->fwnode;
  1597. genpd->has_provider = true;
  1598. }
  1599. ret = genpd_add_provider(np, data->xlate, data);
  1600. if (ret < 0)
  1601. goto error;
  1602. mutex_unlock(&gpd_list_lock);
  1603. return 0;
  1604. error:
  1605. while (i--) {
  1606. genpd = data->domains[i];
  1607. if (!genpd)
  1608. continue;
  1609. genpd->provider = NULL;
  1610. genpd->has_provider = false;
  1611. if (genpd->set_performance_state)
  1612. dev_pm_opp_of_remove_table(&genpd->dev);
  1613. }
  1614. mutex_unlock(&gpd_list_lock);
  1615. return ret;
  1616. }
  1617. EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
  1618. /**
  1619. * of_genpd_del_provider() - Remove a previously registered PM domain provider
  1620. * @np: Device node pointer associated with the PM domain provider
  1621. */
  1622. void of_genpd_del_provider(struct device_node *np)
  1623. {
  1624. struct of_genpd_provider *cp, *tmp;
  1625. struct generic_pm_domain *gpd;
  1626. mutex_lock(&gpd_list_lock);
  1627. mutex_lock(&of_genpd_mutex);
  1628. list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
  1629. if (cp->node == np) {
  1630. /*
  1631. * For each PM domain associated with the
  1632. * provider, set the 'has_provider' to false
  1633. * so that the PM domain can be safely removed.
  1634. */
  1635. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  1636. if (gpd->provider == &np->fwnode) {
  1637. gpd->has_provider = false;
  1638. if (!gpd->set_performance_state)
  1639. continue;
  1640. dev_pm_opp_of_remove_table(&gpd->dev);
  1641. }
  1642. }
  1643. list_del(&cp->link);
  1644. of_node_put(cp->node);
  1645. kfree(cp);
  1646. break;
  1647. }
  1648. }
  1649. mutex_unlock(&of_genpd_mutex);
  1650. mutex_unlock(&gpd_list_lock);
  1651. }
  1652. EXPORT_SYMBOL_GPL(of_genpd_del_provider);
  1653. /**
  1654. * genpd_get_from_provider() - Look-up PM domain
  1655. * @genpdspec: OF phandle args to use for look-up
  1656. *
  1657. * Looks for a PM domain provider under the node specified by @genpdspec and if
  1658. * found, uses xlate function of the provider to map phandle args to a PM
  1659. * domain.
  1660. *
  1661. * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
  1662. * on failure.
  1663. */
  1664. static struct generic_pm_domain *genpd_get_from_provider(
  1665. struct of_phandle_args *genpdspec)
  1666. {
  1667. struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
  1668. struct of_genpd_provider *provider;
  1669. if (!genpdspec)
  1670. return ERR_PTR(-EINVAL);
  1671. mutex_lock(&of_genpd_mutex);
  1672. /* Check if we have such a provider in our array */
  1673. list_for_each_entry(provider, &of_genpd_providers, link) {
  1674. if (provider->node == genpdspec->np)
  1675. genpd = provider->xlate(genpdspec, provider->data);
  1676. if (!IS_ERR(genpd))
  1677. break;
  1678. }
  1679. mutex_unlock(&of_genpd_mutex);
  1680. return genpd;
  1681. }
  1682. /**
  1683. * of_genpd_add_device() - Add a device to an I/O PM domain
  1684. * @genpdspec: OF phandle args to use for look-up PM domain
  1685. * @dev: Device to be added.
  1686. *
  1687. * Looks-up an I/O PM domain based upon phandle args provided and adds
  1688. * the device to the PM domain. Returns a negative error code on failure.
  1689. */
  1690. int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
  1691. {
  1692. struct generic_pm_domain *genpd;
  1693. int ret;
  1694. mutex_lock(&gpd_list_lock);
  1695. genpd = genpd_get_from_provider(genpdspec);
  1696. if (IS_ERR(genpd)) {
  1697. ret = PTR_ERR(genpd);
  1698. goto out;
  1699. }
  1700. ret = genpd_add_device(genpd, dev, NULL);
  1701. out:
  1702. mutex_unlock(&gpd_list_lock);
  1703. return ret;
  1704. }
  1705. EXPORT_SYMBOL_GPL(of_genpd_add_device);
  1706. /**
  1707. * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1708. * @parent_spec: OF phandle args to use for parent PM domain look-up
  1709. * @subdomain_spec: OF phandle args to use for subdomain look-up
  1710. *
  1711. * Looks-up a parent PM domain and subdomain based upon phandle args
  1712. * provided and adds the subdomain to the parent PM domain. Returns a
  1713. * negative error code on failure.
  1714. */
  1715. int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  1716. struct of_phandle_args *subdomain_spec)
  1717. {
  1718. struct generic_pm_domain *parent, *subdomain;
  1719. int ret;
  1720. mutex_lock(&gpd_list_lock);
  1721. parent = genpd_get_from_provider(parent_spec);
  1722. if (IS_ERR(parent)) {
  1723. ret = PTR_ERR(parent);
  1724. goto out;
  1725. }
  1726. subdomain = genpd_get_from_provider(subdomain_spec);
  1727. if (IS_ERR(subdomain)) {
  1728. ret = PTR_ERR(subdomain);
  1729. goto out;
  1730. }
  1731. ret = genpd_add_subdomain(parent, subdomain);
  1732. out:
  1733. mutex_unlock(&gpd_list_lock);
  1734. return ret;
  1735. }
  1736. EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
  1737. /**
  1738. * of_genpd_remove_last - Remove the last PM domain registered for a provider
  1739. * @provider: Pointer to device structure associated with provider
  1740. *
  1741. * Find the last PM domain that was added by a particular provider and
  1742. * remove this PM domain from the list of PM domains. The provider is
  1743. * identified by the 'provider' device structure that is passed. The PM
  1744. * domain will only be removed, if the provider associated with domain
  1745. * has been removed.
  1746. *
  1747. * Returns a valid pointer to struct generic_pm_domain on success or
  1748. * ERR_PTR() on failure.
  1749. */
  1750. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
  1751. {
  1752. struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
  1753. int ret;
  1754. if (IS_ERR_OR_NULL(np))
  1755. return ERR_PTR(-EINVAL);
  1756. mutex_lock(&gpd_list_lock);
  1757. list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
  1758. if (gpd->provider == &np->fwnode) {
  1759. ret = genpd_remove(gpd);
  1760. genpd = ret ? ERR_PTR(ret) : gpd;
  1761. break;
  1762. }
  1763. }
  1764. mutex_unlock(&gpd_list_lock);
  1765. return genpd;
  1766. }
  1767. EXPORT_SYMBOL_GPL(of_genpd_remove_last);
  1768. static void genpd_release_dev(struct device *dev)
  1769. {
  1770. kfree(dev);
  1771. }
  1772. static struct bus_type genpd_bus_type = {
  1773. .name = "genpd",
  1774. };
  1775. /**
  1776. * genpd_dev_pm_detach - Detach a device from its PM domain.
  1777. * @dev: Device to detach.
  1778. * @power_off: Currently not used
  1779. *
  1780. * Try to locate a corresponding generic PM domain, which the device was
  1781. * attached to previously. If such is found, the device is detached from it.
  1782. */
  1783. static void genpd_dev_pm_detach(struct device *dev, bool power_off)
  1784. {
  1785. struct generic_pm_domain *pd;
  1786. unsigned int i;
  1787. int ret = 0;
  1788. pd = dev_to_genpd(dev);
  1789. if (IS_ERR(pd))
  1790. return;
  1791. dev_dbg(dev, "removing from PM domain %s\n", pd->name);
  1792. for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
  1793. ret = genpd_remove_device(pd, dev);
  1794. if (ret != -EAGAIN)
  1795. break;
  1796. mdelay(i);
  1797. cond_resched();
  1798. }
  1799. if (ret < 0) {
  1800. dev_err(dev, "failed to remove from PM domain %s: %d",
  1801. pd->name, ret);
  1802. return;
  1803. }
  1804. /* Check if PM domain can be powered off after removing this device. */
  1805. genpd_queue_power_off_work(pd);
  1806. /* Unregister the device if it was created by genpd. */
  1807. if (dev->bus == &genpd_bus_type)
  1808. device_unregister(dev);
  1809. }
  1810. static void genpd_dev_pm_sync(struct device *dev)
  1811. {
  1812. struct generic_pm_domain *pd;
  1813. pd = dev_to_genpd(dev);
  1814. if (IS_ERR(pd))
  1815. return;
  1816. genpd_queue_power_off_work(pd);
  1817. }
  1818. static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
  1819. unsigned int index)
  1820. {
  1821. struct of_phandle_args pd_args;
  1822. struct generic_pm_domain *pd;
  1823. int ret;
  1824. ret = of_parse_phandle_with_args(np, "power-domains",
  1825. "#power-domain-cells", index, &pd_args);
  1826. if (ret < 0)
  1827. return ret;
  1828. mutex_lock(&gpd_list_lock);
  1829. pd = genpd_get_from_provider(&pd_args);
  1830. of_node_put(pd_args.np);
  1831. if (IS_ERR(pd)) {
  1832. mutex_unlock(&gpd_list_lock);
  1833. dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
  1834. __func__, PTR_ERR(pd));
  1835. return -EPROBE_DEFER;
  1836. }
  1837. dev_dbg(dev, "adding to PM domain %s\n", pd->name);
  1838. ret = genpd_add_device(pd, dev, NULL);
  1839. mutex_unlock(&gpd_list_lock);
  1840. if (ret < 0) {
  1841. if (ret != -EPROBE_DEFER)
  1842. dev_err(dev, "failed to add to PM domain %s: %d",
  1843. pd->name, ret);
  1844. return ret;
  1845. }
  1846. dev->pm_domain->detach = genpd_dev_pm_detach;
  1847. dev->pm_domain->sync = genpd_dev_pm_sync;
  1848. genpd_lock(pd);
  1849. ret = genpd_power_on(pd, 0);
  1850. genpd_unlock(pd);
  1851. if (ret)
  1852. genpd_remove_device(pd, dev);
  1853. return ret ? -EPROBE_DEFER : 1;
  1854. }
  1855. /**
  1856. * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
  1857. * @dev: Device to attach.
  1858. *
  1859. * Parse device's OF node to find a PM domain specifier. If such is found,
  1860. * attaches the device to retrieved pm_domain ops.
  1861. *
  1862. * Returns 1 on successfully attached PM domain, 0 when the device don't need a
  1863. * PM domain or when multiple power-domains exists for it, else a negative error
  1864. * code. Note that if a power-domain exists for the device, but it cannot be
  1865. * found or turned on, then return -EPROBE_DEFER to ensure that the device is
  1866. * not probed and to re-try again later.
  1867. */
  1868. int genpd_dev_pm_attach(struct device *dev)
  1869. {
  1870. if (!dev->of_node)
  1871. return 0;
  1872. /*
  1873. * Devices with multiple PM domains must be attached separately, as we
  1874. * can only attach one PM domain per device.
  1875. */
  1876. if (of_count_phandle_with_args(dev->of_node, "power-domains",
  1877. "#power-domain-cells") != 1)
  1878. return 0;
  1879. return __genpd_dev_pm_attach(dev, dev->of_node, 0);
  1880. }
  1881. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
  1882. /**
  1883. * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
  1884. * @dev: The device used to lookup the PM domain.
  1885. * @index: The index of the PM domain.
  1886. *
  1887. * Parse device's OF node to find a PM domain specifier at the provided @index.
  1888. * If such is found, creates a virtual device and attaches it to the retrieved
  1889. * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
  1890. * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
  1891. *
  1892. * Returns the created virtual device if successfully attached PM domain, NULL
  1893. * when the device don't need a PM domain, else an ERR_PTR() in case of
  1894. * failures. If a power-domain exists for the device, but cannot be found or
  1895. * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
  1896. * is not probed and to re-try again later.
  1897. */
  1898. struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  1899. unsigned int index)
  1900. {
  1901. struct device *genpd_dev;
  1902. int num_domains;
  1903. int ret;
  1904. if (!dev->of_node)
  1905. return NULL;
  1906. /* Deal only with devices using multiple PM domains. */
  1907. num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
  1908. "#power-domain-cells");
  1909. if (num_domains < 2 || index >= num_domains)
  1910. return NULL;
  1911. /* Allocate and register device on the genpd bus. */
  1912. genpd_dev = kzalloc(sizeof(*genpd_dev), GFP_KERNEL);
  1913. if (!genpd_dev)
  1914. return ERR_PTR(-ENOMEM);
  1915. dev_set_name(genpd_dev, "genpd:%u:%s", index, dev_name(dev));
  1916. genpd_dev->bus = &genpd_bus_type;
  1917. genpd_dev->release = genpd_release_dev;
  1918. ret = device_register(genpd_dev);
  1919. if (ret) {
  1920. kfree(genpd_dev);
  1921. return ERR_PTR(ret);
  1922. }
  1923. /* Try to attach the device to the PM domain at the specified index. */
  1924. ret = __genpd_dev_pm_attach(genpd_dev, dev->of_node, index);
  1925. if (ret < 1) {
  1926. device_unregister(genpd_dev);
  1927. return ret ? ERR_PTR(ret) : NULL;
  1928. }
  1929. pm_runtime_set_active(genpd_dev);
  1930. pm_runtime_enable(genpd_dev);
  1931. return genpd_dev;
  1932. }
  1933. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
  1934. static const struct of_device_id idle_state_match[] = {
  1935. { .compatible = "domain-idle-state", },
  1936. { }
  1937. };
  1938. static int genpd_parse_state(struct genpd_power_state *genpd_state,
  1939. struct device_node *state_node)
  1940. {
  1941. int err;
  1942. u32 residency;
  1943. u32 entry_latency, exit_latency;
  1944. err = of_property_read_u32(state_node, "entry-latency-us",
  1945. &entry_latency);
  1946. if (err) {
  1947. pr_debug(" * %pOF missing entry-latency-us property\n",
  1948. state_node);
  1949. return -EINVAL;
  1950. }
  1951. err = of_property_read_u32(state_node, "exit-latency-us",
  1952. &exit_latency);
  1953. if (err) {
  1954. pr_debug(" * %pOF missing exit-latency-us property\n",
  1955. state_node);
  1956. return -EINVAL;
  1957. }
  1958. err = of_property_read_u32(state_node, "min-residency-us", &residency);
  1959. if (!err)
  1960. genpd_state->residency_ns = 1000 * residency;
  1961. genpd_state->power_on_latency_ns = 1000 * exit_latency;
  1962. genpd_state->power_off_latency_ns = 1000 * entry_latency;
  1963. genpd_state->fwnode = &state_node->fwnode;
  1964. return 0;
  1965. }
  1966. static int genpd_iterate_idle_states(struct device_node *dn,
  1967. struct genpd_power_state *states)
  1968. {
  1969. int ret;
  1970. struct of_phandle_iterator it;
  1971. struct device_node *np;
  1972. int i = 0;
  1973. ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
  1974. if (ret <= 0)
  1975. return ret;
  1976. /* Loop over the phandles until all the requested entry is found */
  1977. of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
  1978. np = it.node;
  1979. if (!of_match_node(idle_state_match, np))
  1980. continue;
  1981. if (states) {
  1982. ret = genpd_parse_state(&states[i], np);
  1983. if (ret) {
  1984. pr_err("Parsing idle state node %pOF failed with err %d\n",
  1985. np, ret);
  1986. of_node_put(np);
  1987. return ret;
  1988. }
  1989. }
  1990. i++;
  1991. }
  1992. return i;
  1993. }
  1994. /**
  1995. * of_genpd_parse_idle_states: Return array of idle states for the genpd.
  1996. *
  1997. * @dn: The genpd device node
  1998. * @states: The pointer to which the state array will be saved.
  1999. * @n: The count of elements in the array returned from this function.
  2000. *
  2001. * Returns the device states parsed from the OF node. The memory for the states
  2002. * is allocated by this function and is the responsibility of the caller to
  2003. * free the memory after use. If no domain idle states is found it returns
  2004. * -EINVAL and in case of errors, a negative error code.
  2005. */
  2006. int of_genpd_parse_idle_states(struct device_node *dn,
  2007. struct genpd_power_state **states, int *n)
  2008. {
  2009. struct genpd_power_state *st;
  2010. int ret;
  2011. ret = genpd_iterate_idle_states(dn, NULL);
  2012. if (ret <= 0)
  2013. return ret < 0 ? ret : -EINVAL;
  2014. st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
  2015. if (!st)
  2016. return -ENOMEM;
  2017. ret = genpd_iterate_idle_states(dn, st);
  2018. if (ret <= 0) {
  2019. kfree(st);
  2020. return ret < 0 ? ret : -EINVAL;
  2021. }
  2022. *states = st;
  2023. *n = ret;
  2024. return 0;
  2025. }
  2026. EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
  2027. /**
  2028. * of_genpd_opp_to_performance_state- Gets performance state of device's
  2029. * power domain corresponding to a DT node's "required-opps" property.
  2030. *
  2031. * @dev: Device for which the performance-state needs to be found.
  2032. * @opp_node: DT node where the "required-opps" property is present. This can be
  2033. * the device node itself (if it doesn't have an OPP table) or a node
  2034. * within the OPP table of a device (if device has an OPP table).
  2035. * @state: Pointer to return performance state.
  2036. *
  2037. * Returns performance state corresponding to the "required-opps" property of
  2038. * a DT node. This calls platform specific genpd->opp_to_performance_state()
  2039. * callback to translate power domain OPP to performance state.
  2040. *
  2041. * Returns performance state on success and 0 on failure.
  2042. */
  2043. unsigned int of_genpd_opp_to_performance_state(struct device *dev,
  2044. struct device_node *opp_node)
  2045. {
  2046. struct generic_pm_domain *genpd;
  2047. struct dev_pm_opp *opp;
  2048. int state = 0;
  2049. genpd = dev_to_genpd(dev);
  2050. if (IS_ERR(genpd))
  2051. return 0;
  2052. if (unlikely(!genpd->set_performance_state))
  2053. return 0;
  2054. genpd_lock(genpd);
  2055. opp = of_dev_pm_opp_find_required_opp(&genpd->dev, opp_node);
  2056. if (IS_ERR(opp)) {
  2057. dev_err(dev, "Failed to find required OPP: %ld\n",
  2058. PTR_ERR(opp));
  2059. goto unlock;
  2060. }
  2061. state = genpd->opp_to_performance_state(genpd, opp);
  2062. dev_pm_opp_put(opp);
  2063. unlock:
  2064. genpd_unlock(genpd);
  2065. return state;
  2066. }
  2067. EXPORT_SYMBOL_GPL(of_genpd_opp_to_performance_state);
  2068. static int __init genpd_bus_init(void)
  2069. {
  2070. return bus_register(&genpd_bus_type);
  2071. }
  2072. core_initcall(genpd_bus_init);
  2073. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  2074. /*** debugfs support ***/
  2075. #ifdef CONFIG_DEBUG_FS
  2076. #include <linux/pm.h>
  2077. #include <linux/device.h>
  2078. #include <linux/debugfs.h>
  2079. #include <linux/seq_file.h>
  2080. #include <linux/init.h>
  2081. #include <linux/kobject.h>
  2082. static struct dentry *genpd_debugfs_dir;
  2083. /*
  2084. * TODO: This function is a slightly modified version of rtpm_status_show
  2085. * from sysfs.c, so generalize it.
  2086. */
  2087. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  2088. {
  2089. static const char * const status_lookup[] = {
  2090. [RPM_ACTIVE] = "active",
  2091. [RPM_RESUMING] = "resuming",
  2092. [RPM_SUSPENDED] = "suspended",
  2093. [RPM_SUSPENDING] = "suspending"
  2094. };
  2095. const char *p = "";
  2096. if (dev->power.runtime_error)
  2097. p = "error";
  2098. else if (dev->power.disable_depth)
  2099. p = "unsupported";
  2100. else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
  2101. p = status_lookup[dev->power.runtime_status];
  2102. else
  2103. WARN_ON(1);
  2104. seq_puts(s, p);
  2105. }
  2106. static int genpd_summary_one(struct seq_file *s,
  2107. struct generic_pm_domain *genpd)
  2108. {
  2109. static const char * const status_lookup[] = {
  2110. [GPD_STATE_ACTIVE] = "on",
  2111. [GPD_STATE_POWER_OFF] = "off"
  2112. };
  2113. struct pm_domain_data *pm_data;
  2114. const char *kobj_path;
  2115. struct gpd_link *link;
  2116. char state[16];
  2117. int ret;
  2118. ret = genpd_lock_interruptible(genpd);
  2119. if (ret)
  2120. return -ERESTARTSYS;
  2121. if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
  2122. goto exit;
  2123. if (!genpd_status_on(genpd))
  2124. snprintf(state, sizeof(state), "%s-%u",
  2125. status_lookup[genpd->status], genpd->state_idx);
  2126. else
  2127. snprintf(state, sizeof(state), "%s",
  2128. status_lookup[genpd->status]);
  2129. seq_printf(s, "%-30s %-15s ", genpd->name, state);
  2130. /*
  2131. * Modifications on the list require holding locks on both
  2132. * master and slave, so we are safe.
  2133. * Also genpd->name is immutable.
  2134. */
  2135. list_for_each_entry(link, &genpd->master_links, master_node) {
  2136. seq_printf(s, "%s", link->slave->name);
  2137. if (!list_is_last(&link->master_node, &genpd->master_links))
  2138. seq_puts(s, ", ");
  2139. }
  2140. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  2141. kobj_path = kobject_get_path(&pm_data->dev->kobj,
  2142. genpd_is_irq_safe(genpd) ?
  2143. GFP_ATOMIC : GFP_KERNEL);
  2144. if (kobj_path == NULL)
  2145. continue;
  2146. seq_printf(s, "\n %-50s ", kobj_path);
  2147. rtpm_status_str(s, pm_data->dev);
  2148. kfree(kobj_path);
  2149. }
  2150. seq_puts(s, "\n");
  2151. exit:
  2152. genpd_unlock(genpd);
  2153. return 0;
  2154. }
  2155. static int genpd_summary_show(struct seq_file *s, void *data)
  2156. {
  2157. struct generic_pm_domain *genpd;
  2158. int ret = 0;
  2159. seq_puts(s, "domain status slaves\n");
  2160. seq_puts(s, " /device runtime status\n");
  2161. seq_puts(s, "----------------------------------------------------------------------\n");
  2162. ret = mutex_lock_interruptible(&gpd_list_lock);
  2163. if (ret)
  2164. return -ERESTARTSYS;
  2165. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  2166. ret = genpd_summary_one(s, genpd);
  2167. if (ret)
  2168. break;
  2169. }
  2170. mutex_unlock(&gpd_list_lock);
  2171. return ret;
  2172. }
  2173. static int genpd_status_show(struct seq_file *s, void *data)
  2174. {
  2175. static const char * const status_lookup[] = {
  2176. [GPD_STATE_ACTIVE] = "on",
  2177. [GPD_STATE_POWER_OFF] = "off"
  2178. };
  2179. struct generic_pm_domain *genpd = s->private;
  2180. int ret = 0;
  2181. ret = genpd_lock_interruptible(genpd);
  2182. if (ret)
  2183. return -ERESTARTSYS;
  2184. if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
  2185. goto exit;
  2186. if (genpd->status == GPD_STATE_POWER_OFF)
  2187. seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
  2188. genpd->state_idx);
  2189. else
  2190. seq_printf(s, "%s\n", status_lookup[genpd->status]);
  2191. exit:
  2192. genpd_unlock(genpd);
  2193. return ret;
  2194. }
  2195. static int genpd_sub_domains_show(struct seq_file *s, void *data)
  2196. {
  2197. struct generic_pm_domain *genpd = s->private;
  2198. struct gpd_link *link;
  2199. int ret = 0;
  2200. ret = genpd_lock_interruptible(genpd);
  2201. if (ret)
  2202. return -ERESTARTSYS;
  2203. list_for_each_entry(link, &genpd->master_links, master_node)
  2204. seq_printf(s, "%s\n", link->slave->name);
  2205. genpd_unlock(genpd);
  2206. return ret;
  2207. }
  2208. static int genpd_idle_states_show(struct seq_file *s, void *data)
  2209. {
  2210. struct generic_pm_domain *genpd = s->private;
  2211. unsigned int i;
  2212. int ret = 0;
  2213. ret = genpd_lock_interruptible(genpd);
  2214. if (ret)
  2215. return -ERESTARTSYS;
  2216. seq_puts(s, "State Time Spent(ms)\n");
  2217. for (i = 0; i < genpd->state_count; i++) {
  2218. ktime_t delta = 0;
  2219. s64 msecs;
  2220. if ((genpd->status == GPD_STATE_POWER_OFF) &&
  2221. (genpd->state_idx == i))
  2222. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2223. msecs = ktime_to_ms(
  2224. ktime_add(genpd->states[i].idle_time, delta));
  2225. seq_printf(s, "S%-13i %lld\n", i, msecs);
  2226. }
  2227. genpd_unlock(genpd);
  2228. return ret;
  2229. }
  2230. static int genpd_active_time_show(struct seq_file *s, void *data)
  2231. {
  2232. struct generic_pm_domain *genpd = s->private;
  2233. ktime_t delta = 0;
  2234. int ret = 0;
  2235. ret = genpd_lock_interruptible(genpd);
  2236. if (ret)
  2237. return -ERESTARTSYS;
  2238. if (genpd->status == GPD_STATE_ACTIVE)
  2239. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2240. seq_printf(s, "%lld ms\n", ktime_to_ms(
  2241. ktime_add(genpd->on_time, delta)));
  2242. genpd_unlock(genpd);
  2243. return ret;
  2244. }
  2245. static int genpd_total_idle_time_show(struct seq_file *s, void *data)
  2246. {
  2247. struct generic_pm_domain *genpd = s->private;
  2248. ktime_t delta = 0, total = 0;
  2249. unsigned int i;
  2250. int ret = 0;
  2251. ret = genpd_lock_interruptible(genpd);
  2252. if (ret)
  2253. return -ERESTARTSYS;
  2254. for (i = 0; i < genpd->state_count; i++) {
  2255. if ((genpd->status == GPD_STATE_POWER_OFF) &&
  2256. (genpd->state_idx == i))
  2257. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2258. total = ktime_add(total, genpd->states[i].idle_time);
  2259. }
  2260. total = ktime_add(total, delta);
  2261. seq_printf(s, "%lld ms\n", ktime_to_ms(total));
  2262. genpd_unlock(genpd);
  2263. return ret;
  2264. }
  2265. static int genpd_devices_show(struct seq_file *s, void *data)
  2266. {
  2267. struct generic_pm_domain *genpd = s->private;
  2268. struct pm_domain_data *pm_data;
  2269. const char *kobj_path;
  2270. int ret = 0;
  2271. ret = genpd_lock_interruptible(genpd);
  2272. if (ret)
  2273. return -ERESTARTSYS;
  2274. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  2275. kobj_path = kobject_get_path(&pm_data->dev->kobj,
  2276. genpd_is_irq_safe(genpd) ?
  2277. GFP_ATOMIC : GFP_KERNEL);
  2278. if (kobj_path == NULL)
  2279. continue;
  2280. seq_printf(s, "%s\n", kobj_path);
  2281. kfree(kobj_path);
  2282. }
  2283. genpd_unlock(genpd);
  2284. return ret;
  2285. }
  2286. static int genpd_perf_state_show(struct seq_file *s, void *data)
  2287. {
  2288. struct generic_pm_domain *genpd = s->private;
  2289. if (genpd_lock_interruptible(genpd))
  2290. return -ERESTARTSYS;
  2291. seq_printf(s, "%u\n", genpd->performance_state);
  2292. genpd_unlock(genpd);
  2293. return 0;
  2294. }
  2295. #define define_genpd_open_function(name) \
  2296. static int genpd_##name##_open(struct inode *inode, struct file *file) \
  2297. { \
  2298. return single_open(file, genpd_##name##_show, inode->i_private); \
  2299. }
  2300. define_genpd_open_function(summary);
  2301. define_genpd_open_function(status);
  2302. define_genpd_open_function(sub_domains);
  2303. define_genpd_open_function(idle_states);
  2304. define_genpd_open_function(active_time);
  2305. define_genpd_open_function(total_idle_time);
  2306. define_genpd_open_function(devices);
  2307. define_genpd_open_function(perf_state);
  2308. #define define_genpd_debugfs_fops(name) \
  2309. static const struct file_operations genpd_##name##_fops = { \
  2310. .open = genpd_##name##_open, \
  2311. .read = seq_read, \
  2312. .llseek = seq_lseek, \
  2313. .release = single_release, \
  2314. }
  2315. define_genpd_debugfs_fops(summary);
  2316. define_genpd_debugfs_fops(status);
  2317. define_genpd_debugfs_fops(sub_domains);
  2318. define_genpd_debugfs_fops(idle_states);
  2319. define_genpd_debugfs_fops(active_time);
  2320. define_genpd_debugfs_fops(total_idle_time);
  2321. define_genpd_debugfs_fops(devices);
  2322. define_genpd_debugfs_fops(perf_state);
  2323. static int __init genpd_debug_init(void)
  2324. {
  2325. struct dentry *d;
  2326. struct generic_pm_domain *genpd;
  2327. genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
  2328. if (!genpd_debugfs_dir)
  2329. return -ENOMEM;
  2330. d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
  2331. genpd_debugfs_dir, NULL, &genpd_summary_fops);
  2332. if (!d)
  2333. return -ENOMEM;
  2334. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  2335. d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
  2336. if (!d)
  2337. return -ENOMEM;
  2338. debugfs_create_file("current_state", 0444,
  2339. d, genpd, &genpd_status_fops);
  2340. debugfs_create_file("sub_domains", 0444,
  2341. d, genpd, &genpd_sub_domains_fops);
  2342. debugfs_create_file("idle_states", 0444,
  2343. d, genpd, &genpd_idle_states_fops);
  2344. debugfs_create_file("active_time", 0444,
  2345. d, genpd, &genpd_active_time_fops);
  2346. debugfs_create_file("total_idle_time", 0444,
  2347. d, genpd, &genpd_total_idle_time_fops);
  2348. debugfs_create_file("devices", 0444,
  2349. d, genpd, &genpd_devices_fops);
  2350. if (genpd->set_performance_state)
  2351. debugfs_create_file("perf_state", 0444,
  2352. d, genpd, &genpd_perf_state_fops);
  2353. }
  2354. return 0;
  2355. }
  2356. late_initcall(genpd_debug_init);
  2357. static void __exit genpd_debug_exit(void)
  2358. {
  2359. debugfs_remove_recursive(genpd_debugfs_dir);
  2360. }
  2361. __exitcall(genpd_debug_exit);
  2362. #endif /* CONFIG_DEBUG_FS */