core.c 91 KB

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