domain.c 82 KB

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