domain.c 85 KB

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