domain.c 67 KB

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