core.c 100 KB

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