domain.c 73 KB

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