domain.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  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;
  846. genpd = dev_to_genpd(dev);
  847. if (IS_ERR(genpd))
  848. return -EINVAL;
  849. if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
  850. return 0;
  851. if (poweroff)
  852. ret = pm_generic_poweroff_noirq(dev);
  853. else
  854. ret = pm_generic_suspend_noirq(dev);
  855. if (ret)
  856. return ret;
  857. if (genpd->dev_ops.stop && genpd->dev_ops.start) {
  858. ret = pm_runtime_force_suspend(dev);
  859. if (ret)
  860. return ret;
  861. }
  862. genpd_lock(genpd);
  863. genpd->suspended_count++;
  864. genpd_sync_power_off(genpd, true, 0);
  865. genpd_unlock(genpd);
  866. return 0;
  867. }
  868. /**
  869. * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  870. * @dev: Device to suspend.
  871. *
  872. * Stop the device and remove power from the domain if all devices in it have
  873. * been stopped.
  874. */
  875. static int genpd_suspend_noirq(struct device *dev)
  876. {
  877. dev_dbg(dev, "%s()\n", __func__);
  878. return genpd_finish_suspend(dev, false);
  879. }
  880. /**
  881. * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  882. * @dev: Device to resume.
  883. *
  884. * Restore power to the device's PM domain, if necessary, and start the device.
  885. */
  886. static int genpd_resume_noirq(struct device *dev)
  887. {
  888. struct generic_pm_domain *genpd;
  889. int ret = 0;
  890. dev_dbg(dev, "%s()\n", __func__);
  891. genpd = dev_to_genpd(dev);
  892. if (IS_ERR(genpd))
  893. return -EINVAL;
  894. if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
  895. return 0;
  896. genpd_lock(genpd);
  897. genpd_sync_power_on(genpd, true, 0);
  898. genpd->suspended_count--;
  899. genpd_unlock(genpd);
  900. if (genpd->dev_ops.stop && genpd->dev_ops.start)
  901. ret = pm_runtime_force_resume(dev);
  902. ret = pm_generic_resume_noirq(dev);
  903. if (ret)
  904. return ret;
  905. return ret;
  906. }
  907. /**
  908. * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  909. * @dev: Device to freeze.
  910. *
  911. * Carry out a late freeze of a device under the assumption that its
  912. * pm_domain field points to the domain member of an object of type
  913. * struct generic_pm_domain representing a power domain consisting of I/O
  914. * devices.
  915. */
  916. static int genpd_freeze_noirq(struct device *dev)
  917. {
  918. const struct generic_pm_domain *genpd;
  919. int ret = 0;
  920. dev_dbg(dev, "%s()\n", __func__);
  921. genpd = dev_to_genpd(dev);
  922. if (IS_ERR(genpd))
  923. return -EINVAL;
  924. ret = pm_generic_freeze_noirq(dev);
  925. if (ret)
  926. return ret;
  927. if (genpd->dev_ops.stop && genpd->dev_ops.start)
  928. ret = pm_runtime_force_suspend(dev);
  929. return ret;
  930. }
  931. /**
  932. * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  933. * @dev: Device to thaw.
  934. *
  935. * Start the device, unless power has been removed from the domain already
  936. * before the system transition.
  937. */
  938. static int genpd_thaw_noirq(struct device *dev)
  939. {
  940. const struct generic_pm_domain *genpd;
  941. int ret = 0;
  942. dev_dbg(dev, "%s()\n", __func__);
  943. genpd = dev_to_genpd(dev);
  944. if (IS_ERR(genpd))
  945. return -EINVAL;
  946. if (genpd->dev_ops.stop && genpd->dev_ops.start) {
  947. ret = pm_runtime_force_resume(dev);
  948. if (ret)
  949. return ret;
  950. }
  951. return pm_generic_thaw_noirq(dev);
  952. }
  953. /**
  954. * genpd_poweroff_noirq - Completion of hibernation of device in an
  955. * I/O PM domain.
  956. * @dev: Device to poweroff.
  957. *
  958. * Stop the device and remove power from the domain if all devices in it have
  959. * been stopped.
  960. */
  961. static int genpd_poweroff_noirq(struct device *dev)
  962. {
  963. dev_dbg(dev, "%s()\n", __func__);
  964. return genpd_finish_suspend(dev, true);
  965. }
  966. /**
  967. * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  968. * @dev: Device to resume.
  969. *
  970. * Make sure the domain will be in the same power state as before the
  971. * hibernation the system is resuming from and start the device if necessary.
  972. */
  973. static int genpd_restore_noirq(struct device *dev)
  974. {
  975. struct generic_pm_domain *genpd;
  976. int ret = 0;
  977. dev_dbg(dev, "%s()\n", __func__);
  978. genpd = dev_to_genpd(dev);
  979. if (IS_ERR(genpd))
  980. return -EINVAL;
  981. /*
  982. * At this point suspended_count == 0 means we are being run for the
  983. * first time for the given domain in the present cycle.
  984. */
  985. genpd_lock(genpd);
  986. if (genpd->suspended_count++ == 0)
  987. /*
  988. * The boot kernel might put the domain into arbitrary state,
  989. * so make it appear as powered off to genpd_sync_power_on(),
  990. * so that it tries to power it on in case it was really off.
  991. */
  992. genpd->status = GPD_STATE_POWER_OFF;
  993. genpd_sync_power_on(genpd, true, 0);
  994. genpd_unlock(genpd);
  995. if (genpd->dev_ops.stop && genpd->dev_ops.start) {
  996. ret = pm_runtime_force_resume(dev);
  997. if (ret)
  998. return ret;
  999. }
  1000. return pm_generic_restore_noirq(dev);
  1001. }
  1002. /**
  1003. * genpd_complete - Complete power transition of a device in a power domain.
  1004. * @dev: Device to complete the transition of.
  1005. *
  1006. * Complete a power transition of a device (during a system-wide power
  1007. * transition) under the assumption that its pm_domain field points to the
  1008. * domain member of an object of type struct generic_pm_domain representing
  1009. * a power domain consisting of I/O devices.
  1010. */
  1011. static void genpd_complete(struct device *dev)
  1012. {
  1013. struct generic_pm_domain *genpd;
  1014. dev_dbg(dev, "%s()\n", __func__);
  1015. genpd = dev_to_genpd(dev);
  1016. if (IS_ERR(genpd))
  1017. return;
  1018. pm_generic_complete(dev);
  1019. genpd_lock(genpd);
  1020. genpd->prepared_count--;
  1021. if (!genpd->prepared_count)
  1022. genpd_queue_power_off_work(genpd);
  1023. genpd_unlock(genpd);
  1024. }
  1025. /**
  1026. * genpd_syscore_switch - Switch power during system core suspend or resume.
  1027. * @dev: Device that normally is marked as "always on" to switch power for.
  1028. *
  1029. * This routine may only be called during the system core (syscore) suspend or
  1030. * resume phase for devices whose "always on" flags are set.
  1031. */
  1032. static void genpd_syscore_switch(struct device *dev, bool suspend)
  1033. {
  1034. struct generic_pm_domain *genpd;
  1035. genpd = dev_to_genpd(dev);
  1036. if (!genpd_present(genpd))
  1037. return;
  1038. if (suspend) {
  1039. genpd->suspended_count++;
  1040. genpd_sync_power_off(genpd, false, 0);
  1041. } else {
  1042. genpd_sync_power_on(genpd, false, 0);
  1043. genpd->suspended_count--;
  1044. }
  1045. }
  1046. void pm_genpd_syscore_poweroff(struct device *dev)
  1047. {
  1048. genpd_syscore_switch(dev, true);
  1049. }
  1050. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
  1051. void pm_genpd_syscore_poweron(struct device *dev)
  1052. {
  1053. genpd_syscore_switch(dev, false);
  1054. }
  1055. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
  1056. #else /* !CONFIG_PM_SLEEP */
  1057. #define genpd_prepare NULL
  1058. #define genpd_suspend_noirq NULL
  1059. #define genpd_resume_noirq NULL
  1060. #define genpd_freeze_noirq NULL
  1061. #define genpd_thaw_noirq NULL
  1062. #define genpd_poweroff_noirq NULL
  1063. #define genpd_restore_noirq NULL
  1064. #define genpd_complete NULL
  1065. #endif /* CONFIG_PM_SLEEP */
  1066. static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
  1067. struct generic_pm_domain *genpd,
  1068. struct gpd_timing_data *td)
  1069. {
  1070. struct generic_pm_domain_data *gpd_data;
  1071. int ret;
  1072. ret = dev_pm_get_subsys_data(dev);
  1073. if (ret)
  1074. return ERR_PTR(ret);
  1075. gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
  1076. if (!gpd_data) {
  1077. ret = -ENOMEM;
  1078. goto err_put;
  1079. }
  1080. if (td)
  1081. gpd_data->td = *td;
  1082. gpd_data->base.dev = dev;
  1083. gpd_data->td.constraint_changed = true;
  1084. gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
  1085. gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
  1086. spin_lock_irq(&dev->power.lock);
  1087. if (dev->power.subsys_data->domain_data) {
  1088. ret = -EINVAL;
  1089. goto err_free;
  1090. }
  1091. dev->power.subsys_data->domain_data = &gpd_data->base;
  1092. spin_unlock_irq(&dev->power.lock);
  1093. return gpd_data;
  1094. err_free:
  1095. spin_unlock_irq(&dev->power.lock);
  1096. kfree(gpd_data);
  1097. err_put:
  1098. dev_pm_put_subsys_data(dev);
  1099. return ERR_PTR(ret);
  1100. }
  1101. static void genpd_free_dev_data(struct device *dev,
  1102. struct generic_pm_domain_data *gpd_data)
  1103. {
  1104. spin_lock_irq(&dev->power.lock);
  1105. dev->power.subsys_data->domain_data = NULL;
  1106. spin_unlock_irq(&dev->power.lock);
  1107. kfree(gpd_data);
  1108. dev_pm_put_subsys_data(dev);
  1109. }
  1110. static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  1111. struct gpd_timing_data *td)
  1112. {
  1113. struct generic_pm_domain_data *gpd_data;
  1114. int ret = 0;
  1115. dev_dbg(dev, "%s()\n", __func__);
  1116. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  1117. return -EINVAL;
  1118. gpd_data = genpd_alloc_dev_data(dev, genpd, td);
  1119. if (IS_ERR(gpd_data))
  1120. return PTR_ERR(gpd_data);
  1121. genpd_lock(genpd);
  1122. if (genpd->prepared_count > 0) {
  1123. ret = -EAGAIN;
  1124. goto out;
  1125. }
  1126. ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
  1127. if (ret)
  1128. goto out;
  1129. dev_pm_domain_set(dev, &genpd->domain);
  1130. genpd->device_count++;
  1131. genpd->max_off_time_changed = true;
  1132. list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
  1133. out:
  1134. genpd_unlock(genpd);
  1135. if (ret)
  1136. genpd_free_dev_data(dev, gpd_data);
  1137. else
  1138. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1139. return ret;
  1140. }
  1141. /**
  1142. * __pm_genpd_add_device - Add a device to an I/O PM domain.
  1143. * @genpd: PM domain to add the device to.
  1144. * @dev: Device to be added.
  1145. * @td: Set of PM QoS timing parameters to attach to the device.
  1146. */
  1147. int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  1148. struct gpd_timing_data *td)
  1149. {
  1150. int ret;
  1151. mutex_lock(&gpd_list_lock);
  1152. ret = genpd_add_device(genpd, dev, td);
  1153. mutex_unlock(&gpd_list_lock);
  1154. return ret;
  1155. }
  1156. EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
  1157. static int genpd_remove_device(struct generic_pm_domain *genpd,
  1158. struct device *dev)
  1159. {
  1160. struct generic_pm_domain_data *gpd_data;
  1161. struct pm_domain_data *pdd;
  1162. int ret = 0;
  1163. dev_dbg(dev, "%s()\n", __func__);
  1164. pdd = dev->power.subsys_data->domain_data;
  1165. gpd_data = to_gpd_data(pdd);
  1166. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1167. genpd_lock(genpd);
  1168. if (genpd->prepared_count > 0) {
  1169. ret = -EAGAIN;
  1170. goto out;
  1171. }
  1172. genpd->device_count--;
  1173. genpd->max_off_time_changed = true;
  1174. if (genpd->detach_dev)
  1175. genpd->detach_dev(genpd, dev);
  1176. dev_pm_domain_set(dev, NULL);
  1177. list_del_init(&pdd->list_node);
  1178. genpd_unlock(genpd);
  1179. genpd_free_dev_data(dev, gpd_data);
  1180. return 0;
  1181. out:
  1182. genpd_unlock(genpd);
  1183. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1184. return ret;
  1185. }
  1186. /**
  1187. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  1188. * @genpd: PM domain to remove the device from.
  1189. * @dev: Device to be removed.
  1190. */
  1191. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  1192. struct device *dev)
  1193. {
  1194. if (!genpd || genpd != genpd_lookup_dev(dev))
  1195. return -EINVAL;
  1196. return genpd_remove_device(genpd, dev);
  1197. }
  1198. EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
  1199. static int genpd_add_subdomain(struct generic_pm_domain *genpd,
  1200. struct generic_pm_domain *subdomain)
  1201. {
  1202. struct gpd_link *link, *itr;
  1203. int ret = 0;
  1204. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
  1205. || genpd == subdomain)
  1206. return -EINVAL;
  1207. /*
  1208. * If the domain can be powered on/off in an IRQ safe
  1209. * context, ensure that the subdomain can also be
  1210. * powered on/off in that context.
  1211. */
  1212. if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
  1213. WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
  1214. genpd->name, subdomain->name);
  1215. return -EINVAL;
  1216. }
  1217. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1218. if (!link)
  1219. return -ENOMEM;
  1220. genpd_lock(subdomain);
  1221. genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
  1222. if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
  1223. ret = -EINVAL;
  1224. goto out;
  1225. }
  1226. list_for_each_entry(itr, &genpd->master_links, master_node) {
  1227. if (itr->slave == subdomain && itr->master == genpd) {
  1228. ret = -EINVAL;
  1229. goto out;
  1230. }
  1231. }
  1232. link->master = genpd;
  1233. list_add_tail(&link->master_node, &genpd->master_links);
  1234. link->slave = subdomain;
  1235. list_add_tail(&link->slave_node, &subdomain->slave_links);
  1236. if (genpd_status_on(subdomain))
  1237. genpd_sd_counter_inc(genpd);
  1238. out:
  1239. genpd_unlock(genpd);
  1240. genpd_unlock(subdomain);
  1241. if (ret)
  1242. kfree(link);
  1243. return ret;
  1244. }
  1245. /**
  1246. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1247. * @genpd: Master PM domain to add the subdomain to.
  1248. * @subdomain: Subdomain to be added.
  1249. */
  1250. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  1251. struct generic_pm_domain *subdomain)
  1252. {
  1253. int ret;
  1254. mutex_lock(&gpd_list_lock);
  1255. ret = genpd_add_subdomain(genpd, subdomain);
  1256. mutex_unlock(&gpd_list_lock);
  1257. return ret;
  1258. }
  1259. EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
  1260. /**
  1261. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  1262. * @genpd: Master PM domain to remove the subdomain from.
  1263. * @subdomain: Subdomain to be removed.
  1264. */
  1265. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  1266. struct generic_pm_domain *subdomain)
  1267. {
  1268. struct gpd_link *l, *link;
  1269. int ret = -EINVAL;
  1270. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1271. return -EINVAL;
  1272. genpd_lock(subdomain);
  1273. genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
  1274. if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
  1275. pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
  1276. subdomain->name);
  1277. ret = -EBUSY;
  1278. goto out;
  1279. }
  1280. list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
  1281. if (link->slave != subdomain)
  1282. continue;
  1283. list_del(&link->master_node);
  1284. list_del(&link->slave_node);
  1285. kfree(link);
  1286. if (genpd_status_on(subdomain))
  1287. genpd_sd_counter_dec(genpd);
  1288. ret = 0;
  1289. break;
  1290. }
  1291. out:
  1292. genpd_unlock(genpd);
  1293. genpd_unlock(subdomain);
  1294. return ret;
  1295. }
  1296. EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
  1297. static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
  1298. {
  1299. struct genpd_power_state *state;
  1300. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1301. if (!state)
  1302. return -ENOMEM;
  1303. genpd->states = state;
  1304. genpd->state_count = 1;
  1305. genpd->free = state;
  1306. return 0;
  1307. }
  1308. static void genpd_lock_init(struct generic_pm_domain *genpd)
  1309. {
  1310. if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
  1311. spin_lock_init(&genpd->slock);
  1312. genpd->lock_ops = &genpd_spin_ops;
  1313. } else {
  1314. mutex_init(&genpd->mlock);
  1315. genpd->lock_ops = &genpd_mtx_ops;
  1316. }
  1317. }
  1318. /**
  1319. * pm_genpd_init - Initialize a generic I/O PM domain object.
  1320. * @genpd: PM domain object to initialize.
  1321. * @gov: PM domain governor to associate with the domain (may be NULL).
  1322. * @is_off: Initial value of the domain's power_is_off field.
  1323. *
  1324. * Returns 0 on successful initialization, else a negative error code.
  1325. */
  1326. int pm_genpd_init(struct generic_pm_domain *genpd,
  1327. struct dev_power_governor *gov, bool is_off)
  1328. {
  1329. int ret;
  1330. if (IS_ERR_OR_NULL(genpd))
  1331. return -EINVAL;
  1332. INIT_LIST_HEAD(&genpd->master_links);
  1333. INIT_LIST_HEAD(&genpd->slave_links);
  1334. INIT_LIST_HEAD(&genpd->dev_list);
  1335. genpd_lock_init(genpd);
  1336. genpd->gov = gov;
  1337. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  1338. atomic_set(&genpd->sd_count, 0);
  1339. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  1340. genpd->device_count = 0;
  1341. genpd->max_off_time_ns = -1;
  1342. genpd->max_off_time_changed = true;
  1343. genpd->provider = NULL;
  1344. genpd->has_provider = false;
  1345. genpd->accounting_time = ktime_get();
  1346. genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
  1347. genpd->domain.ops.runtime_resume = genpd_runtime_resume;
  1348. genpd->domain.ops.prepare = genpd_prepare;
  1349. genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
  1350. genpd->domain.ops.resume_noirq = genpd_resume_noirq;
  1351. genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
  1352. genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
  1353. genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
  1354. genpd->domain.ops.restore_noirq = genpd_restore_noirq;
  1355. genpd->domain.ops.complete = genpd_complete;
  1356. if (genpd->flags & GENPD_FLAG_PM_CLK) {
  1357. genpd->dev_ops.stop = pm_clk_suspend;
  1358. genpd->dev_ops.start = pm_clk_resume;
  1359. }
  1360. /* Always-on domains must be powered on at initialization. */
  1361. if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
  1362. return -EINVAL;
  1363. /* Use only one "off" state if there were no states declared */
  1364. if (genpd->state_count == 0) {
  1365. ret = genpd_set_default_power_state(genpd);
  1366. if (ret)
  1367. return ret;
  1368. }
  1369. mutex_lock(&gpd_list_lock);
  1370. list_add(&genpd->gpd_list_node, &gpd_list);
  1371. mutex_unlock(&gpd_list_lock);
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL_GPL(pm_genpd_init);
  1375. static int genpd_remove(struct generic_pm_domain *genpd)
  1376. {
  1377. struct gpd_link *l, *link;
  1378. if (IS_ERR_OR_NULL(genpd))
  1379. return -EINVAL;
  1380. genpd_lock(genpd);
  1381. if (genpd->has_provider) {
  1382. genpd_unlock(genpd);
  1383. pr_err("Provider present, unable to remove %s\n", genpd->name);
  1384. return -EBUSY;
  1385. }
  1386. if (!list_empty(&genpd->master_links) || genpd->device_count) {
  1387. genpd_unlock(genpd);
  1388. pr_err("%s: unable to remove %s\n", __func__, genpd->name);
  1389. return -EBUSY;
  1390. }
  1391. list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
  1392. list_del(&link->master_node);
  1393. list_del(&link->slave_node);
  1394. kfree(link);
  1395. }
  1396. list_del(&genpd->gpd_list_node);
  1397. genpd_unlock(genpd);
  1398. cancel_work_sync(&genpd->power_off_work);
  1399. kfree(genpd->free);
  1400. pr_debug("%s: removed %s\n", __func__, genpd->name);
  1401. return 0;
  1402. }
  1403. /**
  1404. * pm_genpd_remove - Remove a generic I/O PM domain
  1405. * @genpd: Pointer to PM domain that is to be removed.
  1406. *
  1407. * To remove the PM domain, this function:
  1408. * - Removes the PM domain as a subdomain to any parent domains,
  1409. * if it was added.
  1410. * - Removes the PM domain from the list of registered PM domains.
  1411. *
  1412. * The PM domain will only be removed, if the associated provider has
  1413. * been removed, it is not a parent to any other PM domain and has no
  1414. * devices associated with it.
  1415. */
  1416. int pm_genpd_remove(struct generic_pm_domain *genpd)
  1417. {
  1418. int ret;
  1419. mutex_lock(&gpd_list_lock);
  1420. ret = genpd_remove(genpd);
  1421. mutex_unlock(&gpd_list_lock);
  1422. return ret;
  1423. }
  1424. EXPORT_SYMBOL_GPL(pm_genpd_remove);
  1425. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  1426. /*
  1427. * Device Tree based PM domain providers.
  1428. *
  1429. * The code below implements generic device tree based PM domain providers that
  1430. * bind device tree nodes with generic PM domains registered in the system.
  1431. *
  1432. * Any driver that registers generic PM domains and needs to support binding of
  1433. * devices to these domains is supposed to register a PM domain provider, which
  1434. * maps a PM domain specifier retrieved from the device tree to a PM domain.
  1435. *
  1436. * Two simple mapping functions have been provided for convenience:
  1437. * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
  1438. * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
  1439. * index.
  1440. */
  1441. /**
  1442. * struct of_genpd_provider - PM domain provider registration structure
  1443. * @link: Entry in global list of PM domain providers
  1444. * @node: Pointer to device tree node of PM domain provider
  1445. * @xlate: Provider-specific xlate callback mapping a set of specifier cells
  1446. * into a PM domain.
  1447. * @data: context pointer to be passed into @xlate callback
  1448. */
  1449. struct of_genpd_provider {
  1450. struct list_head link;
  1451. struct device_node *node;
  1452. genpd_xlate_t xlate;
  1453. void *data;
  1454. };
  1455. /* List of registered PM domain providers. */
  1456. static LIST_HEAD(of_genpd_providers);
  1457. /* Mutex to protect the list above. */
  1458. static DEFINE_MUTEX(of_genpd_mutex);
  1459. /**
  1460. * genpd_xlate_simple() - Xlate function for direct node-domain mapping
  1461. * @genpdspec: OF phandle args to map into a PM domain
  1462. * @data: xlate function private data - pointer to struct generic_pm_domain
  1463. *
  1464. * This is a generic xlate function that can be used to model PM domains that
  1465. * have their own device tree nodes. The private data of xlate function needs
  1466. * to be a valid pointer to struct generic_pm_domain.
  1467. */
  1468. static struct generic_pm_domain *genpd_xlate_simple(
  1469. struct of_phandle_args *genpdspec,
  1470. void *data)
  1471. {
  1472. return data;
  1473. }
  1474. /**
  1475. * genpd_xlate_onecell() - Xlate function using a single index.
  1476. * @genpdspec: OF phandle args to map into a PM domain
  1477. * @data: xlate function private data - pointer to struct genpd_onecell_data
  1478. *
  1479. * This is a generic xlate function that can be used to model simple PM domain
  1480. * controllers that have one device tree node and provide multiple PM domains.
  1481. * A single cell is used as an index into an array of PM domains specified in
  1482. * the genpd_onecell_data struct when registering the provider.
  1483. */
  1484. static struct generic_pm_domain *genpd_xlate_onecell(
  1485. struct of_phandle_args *genpdspec,
  1486. void *data)
  1487. {
  1488. struct genpd_onecell_data *genpd_data = data;
  1489. unsigned int idx = genpdspec->args[0];
  1490. if (genpdspec->args_count != 1)
  1491. return ERR_PTR(-EINVAL);
  1492. if (idx >= genpd_data->num_domains) {
  1493. pr_err("%s: invalid domain index %u\n", __func__, idx);
  1494. return ERR_PTR(-EINVAL);
  1495. }
  1496. if (!genpd_data->domains[idx])
  1497. return ERR_PTR(-ENOENT);
  1498. return genpd_data->domains[idx];
  1499. }
  1500. /**
  1501. * genpd_add_provider() - Register a PM domain provider for a node
  1502. * @np: Device node pointer associated with the PM domain provider.
  1503. * @xlate: Callback for decoding PM domain from phandle arguments.
  1504. * @data: Context pointer for @xlate callback.
  1505. */
  1506. static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
  1507. void *data)
  1508. {
  1509. struct of_genpd_provider *cp;
  1510. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  1511. if (!cp)
  1512. return -ENOMEM;
  1513. cp->node = of_node_get(np);
  1514. cp->data = data;
  1515. cp->xlate = xlate;
  1516. mutex_lock(&of_genpd_mutex);
  1517. list_add(&cp->link, &of_genpd_providers);
  1518. mutex_unlock(&of_genpd_mutex);
  1519. pr_debug("Added domain provider from %pOF\n", np);
  1520. return 0;
  1521. }
  1522. /**
  1523. * of_genpd_add_provider_simple() - Register a simple PM domain provider
  1524. * @np: Device node pointer associated with the PM domain provider.
  1525. * @genpd: Pointer to PM domain associated with the PM domain provider.
  1526. */
  1527. int of_genpd_add_provider_simple(struct device_node *np,
  1528. struct generic_pm_domain *genpd)
  1529. {
  1530. int ret = -EINVAL;
  1531. if (!np || !genpd)
  1532. return -EINVAL;
  1533. mutex_lock(&gpd_list_lock);
  1534. if (genpd_present(genpd)) {
  1535. ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
  1536. if (!ret) {
  1537. genpd->provider = &np->fwnode;
  1538. genpd->has_provider = true;
  1539. }
  1540. }
  1541. mutex_unlock(&gpd_list_lock);
  1542. return ret;
  1543. }
  1544. EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
  1545. /**
  1546. * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
  1547. * @np: Device node pointer associated with the PM domain provider.
  1548. * @data: Pointer to the data associated with the PM domain provider.
  1549. */
  1550. int of_genpd_add_provider_onecell(struct device_node *np,
  1551. struct genpd_onecell_data *data)
  1552. {
  1553. unsigned int i;
  1554. int ret = -EINVAL;
  1555. if (!np || !data)
  1556. return -EINVAL;
  1557. mutex_lock(&gpd_list_lock);
  1558. if (!data->xlate)
  1559. data->xlate = genpd_xlate_onecell;
  1560. for (i = 0; i < data->num_domains; i++) {
  1561. if (!data->domains[i])
  1562. continue;
  1563. if (!genpd_present(data->domains[i]))
  1564. goto error;
  1565. data->domains[i]->provider = &np->fwnode;
  1566. data->domains[i]->has_provider = true;
  1567. }
  1568. ret = genpd_add_provider(np, data->xlate, data);
  1569. if (ret < 0)
  1570. goto error;
  1571. mutex_unlock(&gpd_list_lock);
  1572. return 0;
  1573. error:
  1574. while (i--) {
  1575. if (!data->domains[i])
  1576. continue;
  1577. data->domains[i]->provider = NULL;
  1578. data->domains[i]->has_provider = false;
  1579. }
  1580. mutex_unlock(&gpd_list_lock);
  1581. return ret;
  1582. }
  1583. EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
  1584. /**
  1585. * of_genpd_del_provider() - Remove a previously registered PM domain provider
  1586. * @np: Device node pointer associated with the PM domain provider
  1587. */
  1588. void of_genpd_del_provider(struct device_node *np)
  1589. {
  1590. struct of_genpd_provider *cp, *tmp;
  1591. struct generic_pm_domain *gpd;
  1592. mutex_lock(&gpd_list_lock);
  1593. mutex_lock(&of_genpd_mutex);
  1594. list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
  1595. if (cp->node == np) {
  1596. /*
  1597. * For each PM domain associated with the
  1598. * provider, set the 'has_provider' to false
  1599. * so that the PM domain can be safely removed.
  1600. */
  1601. list_for_each_entry(gpd, &gpd_list, gpd_list_node)
  1602. if (gpd->provider == &np->fwnode)
  1603. gpd->has_provider = false;
  1604. list_del(&cp->link);
  1605. of_node_put(cp->node);
  1606. kfree(cp);
  1607. break;
  1608. }
  1609. }
  1610. mutex_unlock(&of_genpd_mutex);
  1611. mutex_unlock(&gpd_list_lock);
  1612. }
  1613. EXPORT_SYMBOL_GPL(of_genpd_del_provider);
  1614. /**
  1615. * genpd_get_from_provider() - Look-up PM domain
  1616. * @genpdspec: OF phandle args to use for look-up
  1617. *
  1618. * Looks for a PM domain provider under the node specified by @genpdspec and if
  1619. * found, uses xlate function of the provider to map phandle args to a PM
  1620. * domain.
  1621. *
  1622. * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
  1623. * on failure.
  1624. */
  1625. static struct generic_pm_domain *genpd_get_from_provider(
  1626. struct of_phandle_args *genpdspec)
  1627. {
  1628. struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
  1629. struct of_genpd_provider *provider;
  1630. if (!genpdspec)
  1631. return ERR_PTR(-EINVAL);
  1632. mutex_lock(&of_genpd_mutex);
  1633. /* Check if we have such a provider in our array */
  1634. list_for_each_entry(provider, &of_genpd_providers, link) {
  1635. if (provider->node == genpdspec->np)
  1636. genpd = provider->xlate(genpdspec, provider->data);
  1637. if (!IS_ERR(genpd))
  1638. break;
  1639. }
  1640. mutex_unlock(&of_genpd_mutex);
  1641. return genpd;
  1642. }
  1643. /**
  1644. * of_genpd_add_device() - Add a device to an I/O PM domain
  1645. * @genpdspec: OF phandle args to use for look-up PM domain
  1646. * @dev: Device to be added.
  1647. *
  1648. * Looks-up an I/O PM domain based upon phandle args provided and adds
  1649. * the device to the PM domain. Returns a negative error code on failure.
  1650. */
  1651. int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
  1652. {
  1653. struct generic_pm_domain *genpd;
  1654. int ret;
  1655. mutex_lock(&gpd_list_lock);
  1656. genpd = genpd_get_from_provider(genpdspec);
  1657. if (IS_ERR(genpd)) {
  1658. ret = PTR_ERR(genpd);
  1659. goto out;
  1660. }
  1661. ret = genpd_add_device(genpd, dev, NULL);
  1662. out:
  1663. mutex_unlock(&gpd_list_lock);
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL_GPL(of_genpd_add_device);
  1667. /**
  1668. * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1669. * @parent_spec: OF phandle args to use for parent PM domain look-up
  1670. * @subdomain_spec: OF phandle args to use for subdomain look-up
  1671. *
  1672. * Looks-up a parent PM domain and subdomain based upon phandle args
  1673. * provided and adds the subdomain to the parent PM domain. Returns a
  1674. * negative error code on failure.
  1675. */
  1676. int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  1677. struct of_phandle_args *subdomain_spec)
  1678. {
  1679. struct generic_pm_domain *parent, *subdomain;
  1680. int ret;
  1681. mutex_lock(&gpd_list_lock);
  1682. parent = genpd_get_from_provider(parent_spec);
  1683. if (IS_ERR(parent)) {
  1684. ret = PTR_ERR(parent);
  1685. goto out;
  1686. }
  1687. subdomain = genpd_get_from_provider(subdomain_spec);
  1688. if (IS_ERR(subdomain)) {
  1689. ret = PTR_ERR(subdomain);
  1690. goto out;
  1691. }
  1692. ret = genpd_add_subdomain(parent, subdomain);
  1693. out:
  1694. mutex_unlock(&gpd_list_lock);
  1695. return ret;
  1696. }
  1697. EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
  1698. /**
  1699. * of_genpd_remove_last - Remove the last PM domain registered for a provider
  1700. * @provider: Pointer to device structure associated with provider
  1701. *
  1702. * Find the last PM domain that was added by a particular provider and
  1703. * remove this PM domain from the list of PM domains. The provider is
  1704. * identified by the 'provider' device structure that is passed. The PM
  1705. * domain will only be removed, if the provider associated with domain
  1706. * has been removed.
  1707. *
  1708. * Returns a valid pointer to struct generic_pm_domain on success or
  1709. * ERR_PTR() on failure.
  1710. */
  1711. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
  1712. {
  1713. struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
  1714. int ret;
  1715. if (IS_ERR_OR_NULL(np))
  1716. return ERR_PTR(-EINVAL);
  1717. mutex_lock(&gpd_list_lock);
  1718. list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
  1719. if (gpd->provider == &np->fwnode) {
  1720. ret = genpd_remove(gpd);
  1721. genpd = ret ? ERR_PTR(ret) : gpd;
  1722. break;
  1723. }
  1724. }
  1725. mutex_unlock(&gpd_list_lock);
  1726. return genpd;
  1727. }
  1728. EXPORT_SYMBOL_GPL(of_genpd_remove_last);
  1729. /**
  1730. * genpd_dev_pm_detach - Detach a device from its PM domain.
  1731. * @dev: Device to detach.
  1732. * @power_off: Currently not used
  1733. *
  1734. * Try to locate a corresponding generic PM domain, which the device was
  1735. * attached to previously. If such is found, the device is detached from it.
  1736. */
  1737. static void genpd_dev_pm_detach(struct device *dev, bool power_off)
  1738. {
  1739. struct generic_pm_domain *pd;
  1740. unsigned int i;
  1741. int ret = 0;
  1742. pd = dev_to_genpd(dev);
  1743. if (IS_ERR(pd))
  1744. return;
  1745. dev_dbg(dev, "removing from PM domain %s\n", pd->name);
  1746. for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
  1747. ret = genpd_remove_device(pd, dev);
  1748. if (ret != -EAGAIN)
  1749. break;
  1750. mdelay(i);
  1751. cond_resched();
  1752. }
  1753. if (ret < 0) {
  1754. dev_err(dev, "failed to remove from PM domain %s: %d",
  1755. pd->name, ret);
  1756. return;
  1757. }
  1758. /* Check if PM domain can be powered off after removing this device. */
  1759. genpd_queue_power_off_work(pd);
  1760. }
  1761. static void genpd_dev_pm_sync(struct device *dev)
  1762. {
  1763. struct generic_pm_domain *pd;
  1764. pd = dev_to_genpd(dev);
  1765. if (IS_ERR(pd))
  1766. return;
  1767. genpd_queue_power_off_work(pd);
  1768. }
  1769. /**
  1770. * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
  1771. * @dev: Device to attach.
  1772. *
  1773. * Parse device's OF node to find a PM domain specifier. If such is found,
  1774. * attaches the device to retrieved pm_domain ops.
  1775. *
  1776. * Both generic and legacy Samsung-specific DT bindings are supported to keep
  1777. * backwards compatibility with existing DTBs.
  1778. *
  1779. * Returns 0 on successfully attached PM domain or negative error code. Note
  1780. * that if a power-domain exists for the device, but it cannot be found or
  1781. * turned on, then return -EPROBE_DEFER to ensure that the device is not
  1782. * probed and to re-try again later.
  1783. */
  1784. int genpd_dev_pm_attach(struct device *dev)
  1785. {
  1786. struct of_phandle_args pd_args;
  1787. struct generic_pm_domain *pd;
  1788. unsigned int i;
  1789. int ret;
  1790. if (!dev->of_node)
  1791. return -ENODEV;
  1792. if (dev->pm_domain)
  1793. return -EEXIST;
  1794. ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
  1795. "#power-domain-cells", 0, &pd_args);
  1796. if (ret < 0) {
  1797. if (ret != -ENOENT)
  1798. return ret;
  1799. /*
  1800. * Try legacy Samsung-specific bindings
  1801. * (for backwards compatibility of DT ABI)
  1802. */
  1803. pd_args.args_count = 0;
  1804. pd_args.np = of_parse_phandle(dev->of_node,
  1805. "samsung,power-domain", 0);
  1806. if (!pd_args.np)
  1807. return -ENOENT;
  1808. }
  1809. mutex_lock(&gpd_list_lock);
  1810. pd = genpd_get_from_provider(&pd_args);
  1811. of_node_put(pd_args.np);
  1812. if (IS_ERR(pd)) {
  1813. mutex_unlock(&gpd_list_lock);
  1814. dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
  1815. __func__, PTR_ERR(pd));
  1816. return -EPROBE_DEFER;
  1817. }
  1818. dev_dbg(dev, "adding to PM domain %s\n", pd->name);
  1819. for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
  1820. ret = genpd_add_device(pd, dev, NULL);
  1821. if (ret != -EAGAIN)
  1822. break;
  1823. mdelay(i);
  1824. cond_resched();
  1825. }
  1826. mutex_unlock(&gpd_list_lock);
  1827. if (ret < 0) {
  1828. if (ret != -EPROBE_DEFER)
  1829. dev_err(dev, "failed to add to PM domain %s: %d",
  1830. pd->name, ret);
  1831. goto out;
  1832. }
  1833. dev->pm_domain->detach = genpd_dev_pm_detach;
  1834. dev->pm_domain->sync = genpd_dev_pm_sync;
  1835. genpd_lock(pd);
  1836. ret = genpd_power_on(pd, 0);
  1837. genpd_unlock(pd);
  1838. out:
  1839. return ret ? -EPROBE_DEFER : 0;
  1840. }
  1841. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
  1842. static const struct of_device_id idle_state_match[] = {
  1843. { .compatible = "domain-idle-state", },
  1844. { }
  1845. };
  1846. static int genpd_parse_state(struct genpd_power_state *genpd_state,
  1847. struct device_node *state_node)
  1848. {
  1849. int err;
  1850. u32 residency;
  1851. u32 entry_latency, exit_latency;
  1852. err = of_property_read_u32(state_node, "entry-latency-us",
  1853. &entry_latency);
  1854. if (err) {
  1855. pr_debug(" * %pOF missing entry-latency-us property\n",
  1856. state_node);
  1857. return -EINVAL;
  1858. }
  1859. err = of_property_read_u32(state_node, "exit-latency-us",
  1860. &exit_latency);
  1861. if (err) {
  1862. pr_debug(" * %pOF missing exit-latency-us property\n",
  1863. state_node);
  1864. return -EINVAL;
  1865. }
  1866. err = of_property_read_u32(state_node, "min-residency-us", &residency);
  1867. if (!err)
  1868. genpd_state->residency_ns = 1000 * residency;
  1869. genpd_state->power_on_latency_ns = 1000 * exit_latency;
  1870. genpd_state->power_off_latency_ns = 1000 * entry_latency;
  1871. genpd_state->fwnode = &state_node->fwnode;
  1872. return 0;
  1873. }
  1874. /**
  1875. * of_genpd_parse_idle_states: Return array of idle states for the genpd.
  1876. *
  1877. * @dn: The genpd device node
  1878. * @states: The pointer to which the state array will be saved.
  1879. * @n: The count of elements in the array returned from this function.
  1880. *
  1881. * Returns the device states parsed from the OF node. The memory for the states
  1882. * is allocated by this function and is the responsibility of the caller to
  1883. * free the memory after use.
  1884. */
  1885. int of_genpd_parse_idle_states(struct device_node *dn,
  1886. struct genpd_power_state **states, int *n)
  1887. {
  1888. struct genpd_power_state *st;
  1889. struct device_node *np;
  1890. int i = 0;
  1891. int err, ret;
  1892. int count;
  1893. struct of_phandle_iterator it;
  1894. const struct of_device_id *match_id;
  1895. count = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
  1896. if (count <= 0)
  1897. return -EINVAL;
  1898. st = kcalloc(count, sizeof(*st), GFP_KERNEL);
  1899. if (!st)
  1900. return -ENOMEM;
  1901. /* Loop over the phandles until all the requested entry is found */
  1902. of_for_each_phandle(&it, err, dn, "domain-idle-states", NULL, 0) {
  1903. np = it.node;
  1904. match_id = of_match_node(idle_state_match, np);
  1905. if (!match_id)
  1906. continue;
  1907. ret = genpd_parse_state(&st[i++], np);
  1908. if (ret) {
  1909. pr_err
  1910. ("Parsing idle state node %pOF failed with err %d\n",
  1911. np, ret);
  1912. of_node_put(np);
  1913. kfree(st);
  1914. return ret;
  1915. }
  1916. }
  1917. *n = i;
  1918. if (!i)
  1919. kfree(st);
  1920. else
  1921. *states = st;
  1922. return 0;
  1923. }
  1924. EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
  1925. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  1926. /*** debugfs support ***/
  1927. #ifdef CONFIG_DEBUG_FS
  1928. #include <linux/pm.h>
  1929. #include <linux/device.h>
  1930. #include <linux/debugfs.h>
  1931. #include <linux/seq_file.h>
  1932. #include <linux/init.h>
  1933. #include <linux/kobject.h>
  1934. static struct dentry *genpd_debugfs_dir;
  1935. /*
  1936. * TODO: This function is a slightly modified version of rtpm_status_show
  1937. * from sysfs.c, so generalize it.
  1938. */
  1939. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  1940. {
  1941. static const char * const status_lookup[] = {
  1942. [RPM_ACTIVE] = "active",
  1943. [RPM_RESUMING] = "resuming",
  1944. [RPM_SUSPENDED] = "suspended",
  1945. [RPM_SUSPENDING] = "suspending"
  1946. };
  1947. const char *p = "";
  1948. if (dev->power.runtime_error)
  1949. p = "error";
  1950. else if (dev->power.disable_depth)
  1951. p = "unsupported";
  1952. else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
  1953. p = status_lookup[dev->power.runtime_status];
  1954. else
  1955. WARN_ON(1);
  1956. seq_puts(s, p);
  1957. }
  1958. static int genpd_summary_one(struct seq_file *s,
  1959. struct generic_pm_domain *genpd)
  1960. {
  1961. static const char * const status_lookup[] = {
  1962. [GPD_STATE_ACTIVE] = "on",
  1963. [GPD_STATE_POWER_OFF] = "off"
  1964. };
  1965. struct pm_domain_data *pm_data;
  1966. const char *kobj_path;
  1967. struct gpd_link *link;
  1968. char state[16];
  1969. int ret;
  1970. ret = genpd_lock_interruptible(genpd);
  1971. if (ret)
  1972. return -ERESTARTSYS;
  1973. if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
  1974. goto exit;
  1975. if (!genpd_status_on(genpd))
  1976. snprintf(state, sizeof(state), "%s-%u",
  1977. status_lookup[genpd->status], genpd->state_idx);
  1978. else
  1979. snprintf(state, sizeof(state), "%s",
  1980. status_lookup[genpd->status]);
  1981. seq_printf(s, "%-30s %-15s ", genpd->name, state);
  1982. /*
  1983. * Modifications on the list require holding locks on both
  1984. * master and slave, so we are safe.
  1985. * Also genpd->name is immutable.
  1986. */
  1987. list_for_each_entry(link, &genpd->master_links, master_node) {
  1988. seq_printf(s, "%s", link->slave->name);
  1989. if (!list_is_last(&link->master_node, &genpd->master_links))
  1990. seq_puts(s, ", ");
  1991. }
  1992. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  1993. kobj_path = kobject_get_path(&pm_data->dev->kobj,
  1994. genpd_is_irq_safe(genpd) ?
  1995. GFP_ATOMIC : GFP_KERNEL);
  1996. if (kobj_path == NULL)
  1997. continue;
  1998. seq_printf(s, "\n %-50s ", kobj_path);
  1999. rtpm_status_str(s, pm_data->dev);
  2000. kfree(kobj_path);
  2001. }
  2002. seq_puts(s, "\n");
  2003. exit:
  2004. genpd_unlock(genpd);
  2005. return 0;
  2006. }
  2007. static int genpd_summary_show(struct seq_file *s, void *data)
  2008. {
  2009. struct generic_pm_domain *genpd;
  2010. int ret = 0;
  2011. seq_puts(s, "domain status slaves\n");
  2012. seq_puts(s, " /device runtime status\n");
  2013. seq_puts(s, "----------------------------------------------------------------------\n");
  2014. ret = mutex_lock_interruptible(&gpd_list_lock);
  2015. if (ret)
  2016. return -ERESTARTSYS;
  2017. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  2018. ret = genpd_summary_one(s, genpd);
  2019. if (ret)
  2020. break;
  2021. }
  2022. mutex_unlock(&gpd_list_lock);
  2023. return ret;
  2024. }
  2025. static int genpd_status_show(struct seq_file *s, void *data)
  2026. {
  2027. static const char * const status_lookup[] = {
  2028. [GPD_STATE_ACTIVE] = "on",
  2029. [GPD_STATE_POWER_OFF] = "off"
  2030. };
  2031. struct generic_pm_domain *genpd = s->private;
  2032. int ret = 0;
  2033. ret = genpd_lock_interruptible(genpd);
  2034. if (ret)
  2035. return -ERESTARTSYS;
  2036. if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
  2037. goto exit;
  2038. if (genpd->status == GPD_STATE_POWER_OFF)
  2039. seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
  2040. genpd->state_idx);
  2041. else
  2042. seq_printf(s, "%s\n", status_lookup[genpd->status]);
  2043. exit:
  2044. genpd_unlock(genpd);
  2045. return ret;
  2046. }
  2047. static int genpd_sub_domains_show(struct seq_file *s, void *data)
  2048. {
  2049. struct generic_pm_domain *genpd = s->private;
  2050. struct gpd_link *link;
  2051. int ret = 0;
  2052. ret = genpd_lock_interruptible(genpd);
  2053. if (ret)
  2054. return -ERESTARTSYS;
  2055. list_for_each_entry(link, &genpd->master_links, master_node)
  2056. seq_printf(s, "%s\n", link->slave->name);
  2057. genpd_unlock(genpd);
  2058. return ret;
  2059. }
  2060. static int genpd_idle_states_show(struct seq_file *s, void *data)
  2061. {
  2062. struct generic_pm_domain *genpd = s->private;
  2063. unsigned int i;
  2064. int ret = 0;
  2065. ret = genpd_lock_interruptible(genpd);
  2066. if (ret)
  2067. return -ERESTARTSYS;
  2068. seq_puts(s, "State Time Spent(ms)\n");
  2069. for (i = 0; i < genpd->state_count; i++) {
  2070. ktime_t delta = 0;
  2071. s64 msecs;
  2072. if ((genpd->status == GPD_STATE_POWER_OFF) &&
  2073. (genpd->state_idx == i))
  2074. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2075. msecs = ktime_to_ms(
  2076. ktime_add(genpd->states[i].idle_time, delta));
  2077. seq_printf(s, "S%-13i %lld\n", i, msecs);
  2078. }
  2079. genpd_unlock(genpd);
  2080. return ret;
  2081. }
  2082. static int genpd_active_time_show(struct seq_file *s, void *data)
  2083. {
  2084. struct generic_pm_domain *genpd = s->private;
  2085. ktime_t delta = 0;
  2086. int ret = 0;
  2087. ret = genpd_lock_interruptible(genpd);
  2088. if (ret)
  2089. return -ERESTARTSYS;
  2090. if (genpd->status == GPD_STATE_ACTIVE)
  2091. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2092. seq_printf(s, "%lld ms\n", ktime_to_ms(
  2093. ktime_add(genpd->on_time, delta)));
  2094. genpd_unlock(genpd);
  2095. return ret;
  2096. }
  2097. static int genpd_total_idle_time_show(struct seq_file *s, void *data)
  2098. {
  2099. struct generic_pm_domain *genpd = s->private;
  2100. ktime_t delta = 0, total = 0;
  2101. unsigned int i;
  2102. int ret = 0;
  2103. ret = genpd_lock_interruptible(genpd);
  2104. if (ret)
  2105. return -ERESTARTSYS;
  2106. for (i = 0; i < genpd->state_count; i++) {
  2107. if ((genpd->status == GPD_STATE_POWER_OFF) &&
  2108. (genpd->state_idx == i))
  2109. delta = ktime_sub(ktime_get(), genpd->accounting_time);
  2110. total = ktime_add(total, genpd->states[i].idle_time);
  2111. }
  2112. total = ktime_add(total, delta);
  2113. seq_printf(s, "%lld ms\n", ktime_to_ms(total));
  2114. genpd_unlock(genpd);
  2115. return ret;
  2116. }
  2117. static int genpd_devices_show(struct seq_file *s, void *data)
  2118. {
  2119. struct generic_pm_domain *genpd = s->private;
  2120. struct pm_domain_data *pm_data;
  2121. const char *kobj_path;
  2122. int ret = 0;
  2123. ret = genpd_lock_interruptible(genpd);
  2124. if (ret)
  2125. return -ERESTARTSYS;
  2126. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  2127. kobj_path = kobject_get_path(&pm_data->dev->kobj,
  2128. genpd_is_irq_safe(genpd) ?
  2129. GFP_ATOMIC : GFP_KERNEL);
  2130. if (kobj_path == NULL)
  2131. continue;
  2132. seq_printf(s, "%s\n", kobj_path);
  2133. kfree(kobj_path);
  2134. }
  2135. genpd_unlock(genpd);
  2136. return ret;
  2137. }
  2138. #define define_genpd_open_function(name) \
  2139. static int genpd_##name##_open(struct inode *inode, struct file *file) \
  2140. { \
  2141. return single_open(file, genpd_##name##_show, inode->i_private); \
  2142. }
  2143. define_genpd_open_function(summary);
  2144. define_genpd_open_function(status);
  2145. define_genpd_open_function(sub_domains);
  2146. define_genpd_open_function(idle_states);
  2147. define_genpd_open_function(active_time);
  2148. define_genpd_open_function(total_idle_time);
  2149. define_genpd_open_function(devices);
  2150. #define define_genpd_debugfs_fops(name) \
  2151. static const struct file_operations genpd_##name##_fops = { \
  2152. .open = genpd_##name##_open, \
  2153. .read = seq_read, \
  2154. .llseek = seq_lseek, \
  2155. .release = single_release, \
  2156. }
  2157. define_genpd_debugfs_fops(summary);
  2158. define_genpd_debugfs_fops(status);
  2159. define_genpd_debugfs_fops(sub_domains);
  2160. define_genpd_debugfs_fops(idle_states);
  2161. define_genpd_debugfs_fops(active_time);
  2162. define_genpd_debugfs_fops(total_idle_time);
  2163. define_genpd_debugfs_fops(devices);
  2164. static int __init genpd_debug_init(void)
  2165. {
  2166. struct dentry *d;
  2167. struct generic_pm_domain *genpd;
  2168. genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
  2169. if (!genpd_debugfs_dir)
  2170. return -ENOMEM;
  2171. d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
  2172. genpd_debugfs_dir, NULL, &genpd_summary_fops);
  2173. if (!d)
  2174. return -ENOMEM;
  2175. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  2176. d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
  2177. if (!d)
  2178. return -ENOMEM;
  2179. debugfs_create_file("current_state", 0444,
  2180. d, genpd, &genpd_status_fops);
  2181. debugfs_create_file("sub_domains", 0444,
  2182. d, genpd, &genpd_sub_domains_fops);
  2183. debugfs_create_file("idle_states", 0444,
  2184. d, genpd, &genpd_idle_states_fops);
  2185. debugfs_create_file("active_time", 0444,
  2186. d, genpd, &genpd_active_time_fops);
  2187. debugfs_create_file("total_idle_time", 0444,
  2188. d, genpd, &genpd_total_idle_time_fops);
  2189. debugfs_create_file("devices", 0444,
  2190. d, genpd, &genpd_devices_fops);
  2191. }
  2192. return 0;
  2193. }
  2194. late_initcall(genpd_debug_init);
  2195. static void __exit genpd_debug_exit(void)
  2196. {
  2197. debugfs_remove_recursive(genpd_debugfs_dir);
  2198. }
  2199. __exitcall(genpd_debug_exit);
  2200. #endif /* CONFIG_DEBUG_FS */