domain.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  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/kernel.h>
  9. #include <linux/io.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/pm_domain.h>
  13. #include <linux/pm_qos.h>
  14. #include <linux/pm_clock.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/sched.h>
  18. #include <linux/suspend.h>
  19. #include <linux/export.h>
  20. #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
  21. ({ \
  22. type (*__routine)(struct device *__d); \
  23. type __ret = (type)0; \
  24. \
  25. __routine = genpd->dev_ops.callback; \
  26. if (__routine) { \
  27. __ret = __routine(dev); \
  28. } \
  29. __ret; \
  30. })
  31. #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
  32. ({ \
  33. ktime_t __start = ktime_get(); \
  34. type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
  35. s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
  36. struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
  37. if (!__retval && __elapsed > __td->field) { \
  38. __td->field = __elapsed; \
  39. dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
  40. __elapsed); \
  41. genpd->max_off_time_changed = true; \
  42. __td->constraint_changed = true; \
  43. } \
  44. __retval; \
  45. })
  46. static LIST_HEAD(gpd_list);
  47. static DEFINE_MUTEX(gpd_list_lock);
  48. static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
  49. {
  50. struct generic_pm_domain *genpd = NULL, *gpd;
  51. if (IS_ERR_OR_NULL(domain_name))
  52. return NULL;
  53. mutex_lock(&gpd_list_lock);
  54. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  55. if (!strcmp(gpd->name, domain_name)) {
  56. genpd = gpd;
  57. break;
  58. }
  59. }
  60. mutex_unlock(&gpd_list_lock);
  61. return genpd;
  62. }
  63. /*
  64. * Get the generic PM domain for a particular struct device.
  65. * This validates the struct device pointer, the PM domain pointer,
  66. * and checks that the PM domain pointer is a real generic PM domain.
  67. * Any failure results in NULL being returned.
  68. */
  69. struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
  70. {
  71. struct generic_pm_domain *genpd = NULL, *gpd;
  72. if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
  73. return NULL;
  74. mutex_lock(&gpd_list_lock);
  75. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  76. if (&gpd->domain == dev->pm_domain) {
  77. genpd = gpd;
  78. break;
  79. }
  80. }
  81. mutex_unlock(&gpd_list_lock);
  82. return genpd;
  83. }
  84. /*
  85. * This should only be used where we are certain that the pm_domain
  86. * attached to the device is a genpd domain.
  87. */
  88. static struct generic_pm_domain *dev_to_genpd(struct device *dev)
  89. {
  90. if (IS_ERR_OR_NULL(dev->pm_domain))
  91. return ERR_PTR(-EINVAL);
  92. return pd_to_genpd(dev->pm_domain);
  93. }
  94. static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
  95. {
  96. return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
  97. stop_latency_ns, "stop");
  98. }
  99. static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
  100. {
  101. return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
  102. start_latency_ns, "start");
  103. }
  104. static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  105. {
  106. bool ret = false;
  107. if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  108. ret = !!atomic_dec_and_test(&genpd->sd_count);
  109. return ret;
  110. }
  111. static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
  112. {
  113. atomic_inc(&genpd->sd_count);
  114. smp_mb__after_atomic();
  115. }
  116. static void genpd_acquire_lock(struct generic_pm_domain *genpd)
  117. {
  118. DEFINE_WAIT(wait);
  119. mutex_lock(&genpd->lock);
  120. /*
  121. * Wait for the domain to transition into either the active,
  122. * or the power off state.
  123. */
  124. for (;;) {
  125. prepare_to_wait(&genpd->status_wait_queue, &wait,
  126. TASK_UNINTERRUPTIBLE);
  127. if (genpd->status == GPD_STATE_ACTIVE
  128. || genpd->status == GPD_STATE_POWER_OFF)
  129. break;
  130. mutex_unlock(&genpd->lock);
  131. schedule();
  132. mutex_lock(&genpd->lock);
  133. }
  134. finish_wait(&genpd->status_wait_queue, &wait);
  135. }
  136. static void genpd_release_lock(struct generic_pm_domain *genpd)
  137. {
  138. mutex_unlock(&genpd->lock);
  139. }
  140. static void genpd_set_active(struct generic_pm_domain *genpd)
  141. {
  142. if (genpd->resume_count == 0)
  143. genpd->status = GPD_STATE_ACTIVE;
  144. }
  145. static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
  146. {
  147. s64 usecs64;
  148. if (!genpd->cpuidle_data)
  149. return;
  150. usecs64 = genpd->power_on_latency_ns;
  151. do_div(usecs64, NSEC_PER_USEC);
  152. usecs64 += genpd->cpuidle_data->saved_exit_latency;
  153. genpd->cpuidle_data->idle_state->exit_latency = usecs64;
  154. }
  155. static int genpd_power_on(struct generic_pm_domain *genpd)
  156. {
  157. ktime_t time_start;
  158. s64 elapsed_ns;
  159. int ret;
  160. if (!genpd->power_on)
  161. return 0;
  162. time_start = ktime_get();
  163. ret = genpd->power_on(genpd);
  164. if (ret)
  165. return ret;
  166. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  167. if (elapsed_ns <= genpd->power_on_latency_ns)
  168. return ret;
  169. genpd->power_on_latency_ns = elapsed_ns;
  170. genpd->max_off_time_changed = true;
  171. genpd_recalc_cpu_exit_latency(genpd);
  172. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  173. genpd->name, "on", elapsed_ns);
  174. return ret;
  175. }
  176. static int genpd_power_off(struct generic_pm_domain *genpd)
  177. {
  178. ktime_t time_start;
  179. s64 elapsed_ns;
  180. int ret;
  181. if (!genpd->power_off)
  182. return 0;
  183. time_start = ktime_get();
  184. ret = genpd->power_off(genpd);
  185. if (ret == -EBUSY)
  186. return ret;
  187. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  188. if (elapsed_ns <= genpd->power_off_latency_ns)
  189. return ret;
  190. genpd->power_off_latency_ns = elapsed_ns;
  191. genpd->max_off_time_changed = true;
  192. pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
  193. genpd->name, "off", elapsed_ns);
  194. return ret;
  195. }
  196. /**
  197. * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
  198. * @genpd: PM domain to power up.
  199. *
  200. * Restore power to @genpd and all of its masters so that it is possible to
  201. * resume a device belonging to it.
  202. */
  203. static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
  204. __releases(&genpd->lock) __acquires(&genpd->lock)
  205. {
  206. struct gpd_link *link;
  207. DEFINE_WAIT(wait);
  208. int ret = 0;
  209. /* If the domain's master is being waited for, we have to wait too. */
  210. for (;;) {
  211. prepare_to_wait(&genpd->status_wait_queue, &wait,
  212. TASK_UNINTERRUPTIBLE);
  213. if (genpd->status != GPD_STATE_WAIT_MASTER)
  214. break;
  215. mutex_unlock(&genpd->lock);
  216. schedule();
  217. mutex_lock(&genpd->lock);
  218. }
  219. finish_wait(&genpd->status_wait_queue, &wait);
  220. if (genpd->status == GPD_STATE_ACTIVE
  221. || (genpd->prepared_count > 0 && genpd->suspend_power_off))
  222. return 0;
  223. if (genpd->status != GPD_STATE_POWER_OFF) {
  224. genpd_set_active(genpd);
  225. return 0;
  226. }
  227. if (genpd->cpuidle_data) {
  228. cpuidle_pause_and_lock();
  229. genpd->cpuidle_data->idle_state->disabled = true;
  230. cpuidle_resume_and_unlock();
  231. goto out;
  232. }
  233. /*
  234. * The list is guaranteed not to change while the loop below is being
  235. * executed, unless one of the masters' .power_on() callbacks fiddles
  236. * with it.
  237. */
  238. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  239. genpd_sd_counter_inc(link->master);
  240. genpd->status = GPD_STATE_WAIT_MASTER;
  241. mutex_unlock(&genpd->lock);
  242. ret = pm_genpd_poweron(link->master);
  243. mutex_lock(&genpd->lock);
  244. /*
  245. * The "wait for parent" status is guaranteed not to change
  246. * while the master is powering on.
  247. */
  248. genpd->status = GPD_STATE_POWER_OFF;
  249. wake_up_all(&genpd->status_wait_queue);
  250. if (ret) {
  251. genpd_sd_counter_dec(link->master);
  252. goto err;
  253. }
  254. }
  255. ret = genpd_power_on(genpd);
  256. if (ret)
  257. goto err;
  258. out:
  259. genpd_set_active(genpd);
  260. return 0;
  261. err:
  262. list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
  263. genpd_sd_counter_dec(link->master);
  264. return ret;
  265. }
  266. /**
  267. * pm_genpd_poweron - Restore power to a given PM domain and its masters.
  268. * @genpd: PM domain to power up.
  269. */
  270. int pm_genpd_poweron(struct generic_pm_domain *genpd)
  271. {
  272. int ret;
  273. mutex_lock(&genpd->lock);
  274. ret = __pm_genpd_poweron(genpd);
  275. mutex_unlock(&genpd->lock);
  276. return ret;
  277. }
  278. /**
  279. * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
  280. * @domain_name: Name of the PM domain to power up.
  281. */
  282. int pm_genpd_name_poweron(const char *domain_name)
  283. {
  284. struct generic_pm_domain *genpd;
  285. genpd = pm_genpd_lookup_name(domain_name);
  286. return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
  287. }
  288. static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
  289. struct device *dev)
  290. {
  291. return GENPD_DEV_CALLBACK(genpd, int, start, dev);
  292. }
  293. static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
  294. {
  295. return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
  296. save_state_latency_ns, "state save");
  297. }
  298. static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
  299. {
  300. return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
  301. restore_state_latency_ns,
  302. "state restore");
  303. }
  304. static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  305. unsigned long val, void *ptr)
  306. {
  307. struct generic_pm_domain_data *gpd_data;
  308. struct device *dev;
  309. gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
  310. dev = gpd_data->base.dev;
  311. for (;;) {
  312. struct generic_pm_domain *genpd;
  313. struct pm_domain_data *pdd;
  314. spin_lock_irq(&dev->power.lock);
  315. pdd = dev->power.subsys_data ?
  316. dev->power.subsys_data->domain_data : NULL;
  317. if (pdd && pdd->dev) {
  318. to_gpd_data(pdd)->td.constraint_changed = true;
  319. genpd = dev_to_genpd(dev);
  320. } else {
  321. genpd = ERR_PTR(-ENODATA);
  322. }
  323. spin_unlock_irq(&dev->power.lock);
  324. if (!IS_ERR(genpd)) {
  325. mutex_lock(&genpd->lock);
  326. genpd->max_off_time_changed = true;
  327. mutex_unlock(&genpd->lock);
  328. }
  329. dev = dev->parent;
  330. if (!dev || dev->power.ignore_children)
  331. break;
  332. }
  333. return NOTIFY_DONE;
  334. }
  335. /**
  336. * __pm_genpd_save_device - Save the pre-suspend state of a device.
  337. * @pdd: Domain data of the device to save the state of.
  338. * @genpd: PM domain the device belongs to.
  339. */
  340. static int __pm_genpd_save_device(struct pm_domain_data *pdd,
  341. struct generic_pm_domain *genpd)
  342. __releases(&genpd->lock) __acquires(&genpd->lock)
  343. {
  344. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  345. struct device *dev = pdd->dev;
  346. int ret = 0;
  347. if (gpd_data->need_restore > 0)
  348. return 0;
  349. /*
  350. * If the value of the need_restore flag is still unknown at this point,
  351. * we trust that pm_genpd_poweroff() has verified that the device is
  352. * already runtime PM suspended.
  353. */
  354. if (gpd_data->need_restore < 0) {
  355. gpd_data->need_restore = 1;
  356. return 0;
  357. }
  358. mutex_unlock(&genpd->lock);
  359. genpd_start_dev(genpd, dev);
  360. ret = genpd_save_dev(genpd, dev);
  361. genpd_stop_dev(genpd, dev);
  362. mutex_lock(&genpd->lock);
  363. if (!ret)
  364. gpd_data->need_restore = 1;
  365. return ret;
  366. }
  367. /**
  368. * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
  369. * @pdd: Domain data of the device to restore the state of.
  370. * @genpd: PM domain the device belongs to.
  371. */
  372. static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
  373. struct generic_pm_domain *genpd)
  374. __releases(&genpd->lock) __acquires(&genpd->lock)
  375. {
  376. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  377. struct device *dev = pdd->dev;
  378. int need_restore = gpd_data->need_restore;
  379. gpd_data->need_restore = 0;
  380. mutex_unlock(&genpd->lock);
  381. genpd_start_dev(genpd, dev);
  382. /*
  383. * Call genpd_restore_dev() for recently added devices too (need_restore
  384. * is negative then).
  385. */
  386. if (need_restore)
  387. genpd_restore_dev(genpd, dev);
  388. mutex_lock(&genpd->lock);
  389. }
  390. /**
  391. * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
  392. * @genpd: PM domain to check.
  393. *
  394. * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
  395. * a "power off" operation, which means that a "power on" has occured in the
  396. * meantime, or if its resume_count field is different from zero, which means
  397. * that one of its devices has been resumed in the meantime.
  398. */
  399. static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
  400. {
  401. return genpd->status == GPD_STATE_WAIT_MASTER
  402. || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
  403. }
  404. /**
  405. * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
  406. * @genpd: PM domait to power off.
  407. *
  408. * Queue up the execution of pm_genpd_poweroff() unless it's already been done
  409. * before.
  410. */
  411. static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  412. {
  413. queue_work(pm_wq, &genpd->power_off_work);
  414. }
  415. /**
  416. * pm_genpd_poweroff - Remove power from a given PM domain.
  417. * @genpd: PM domain to power down.
  418. *
  419. * If all of the @genpd's devices have been suspended and all of its subdomains
  420. * have been powered down, run the runtime suspend callbacks provided by all of
  421. * the @genpd's devices' drivers and remove power from @genpd.
  422. */
  423. static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
  424. __releases(&genpd->lock) __acquires(&genpd->lock)
  425. {
  426. struct pm_domain_data *pdd;
  427. struct gpd_link *link;
  428. unsigned int not_suspended;
  429. int ret = 0;
  430. start:
  431. /*
  432. * Do not try to power off the domain in the following situations:
  433. * (1) The domain is already in the "power off" state.
  434. * (2) The domain is waiting for its master to power up.
  435. * (3) One of the domain's devices is being resumed right now.
  436. * (4) System suspend is in progress.
  437. */
  438. if (genpd->status == GPD_STATE_POWER_OFF
  439. || genpd->status == GPD_STATE_WAIT_MASTER
  440. || genpd->resume_count > 0 || genpd->prepared_count > 0)
  441. return 0;
  442. if (atomic_read(&genpd->sd_count) > 0)
  443. return -EBUSY;
  444. not_suspended = 0;
  445. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  446. enum pm_qos_flags_status stat;
  447. stat = dev_pm_qos_flags(pdd->dev,
  448. PM_QOS_FLAG_NO_POWER_OFF
  449. | PM_QOS_FLAG_REMOTE_WAKEUP);
  450. if (stat > PM_QOS_FLAGS_NONE)
  451. return -EBUSY;
  452. if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
  453. || pdd->dev->power.irq_safe))
  454. not_suspended++;
  455. }
  456. if (not_suspended > genpd->in_progress)
  457. return -EBUSY;
  458. if (genpd->poweroff_task) {
  459. /*
  460. * Another instance of pm_genpd_poweroff() is executing
  461. * callbacks, so tell it to start over and return.
  462. */
  463. genpd->status = GPD_STATE_REPEAT;
  464. return 0;
  465. }
  466. if (genpd->gov && genpd->gov->power_down_ok) {
  467. if (!genpd->gov->power_down_ok(&genpd->domain))
  468. return -EAGAIN;
  469. }
  470. genpd->status = GPD_STATE_BUSY;
  471. genpd->poweroff_task = current;
  472. list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
  473. ret = atomic_read(&genpd->sd_count) == 0 ?
  474. __pm_genpd_save_device(pdd, genpd) : -EBUSY;
  475. if (genpd_abort_poweroff(genpd))
  476. goto out;
  477. if (ret) {
  478. genpd_set_active(genpd);
  479. goto out;
  480. }
  481. if (genpd->status == GPD_STATE_REPEAT) {
  482. genpd->poweroff_task = NULL;
  483. goto start;
  484. }
  485. }
  486. if (genpd->cpuidle_data) {
  487. /*
  488. * If cpuidle_data is set, cpuidle should turn the domain off
  489. * when the CPU in it is idle. In that case we don't decrement
  490. * the subdomain counts of the master domains, so that power is
  491. * not removed from the current domain prematurely as a result
  492. * of cutting off the masters' power.
  493. */
  494. genpd->status = GPD_STATE_POWER_OFF;
  495. cpuidle_pause_and_lock();
  496. genpd->cpuidle_data->idle_state->disabled = false;
  497. cpuidle_resume_and_unlock();
  498. goto out;
  499. }
  500. if (genpd->power_off) {
  501. if (atomic_read(&genpd->sd_count) > 0) {
  502. ret = -EBUSY;
  503. goto out;
  504. }
  505. /*
  506. * If sd_count > 0 at this point, one of the subdomains hasn't
  507. * managed to call pm_genpd_poweron() for the master yet after
  508. * incrementing it. In that case pm_genpd_poweron() will wait
  509. * for us to drop the lock, so we can call .power_off() and let
  510. * the pm_genpd_poweron() restore power for us (this shouldn't
  511. * happen very often).
  512. */
  513. ret = genpd_power_off(genpd);
  514. if (ret == -EBUSY) {
  515. genpd_set_active(genpd);
  516. goto out;
  517. }
  518. }
  519. genpd->status = GPD_STATE_POWER_OFF;
  520. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  521. genpd_sd_counter_dec(link->master);
  522. genpd_queue_power_off_work(link->master);
  523. }
  524. out:
  525. genpd->poweroff_task = NULL;
  526. wake_up_all(&genpd->status_wait_queue);
  527. return ret;
  528. }
  529. /**
  530. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  531. * @work: Work structure used for scheduling the execution of this function.
  532. */
  533. static void genpd_power_off_work_fn(struct work_struct *work)
  534. {
  535. struct generic_pm_domain *genpd;
  536. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  537. genpd_acquire_lock(genpd);
  538. pm_genpd_poweroff(genpd);
  539. genpd_release_lock(genpd);
  540. }
  541. /**
  542. * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  543. * @dev: Device to suspend.
  544. *
  545. * Carry out a runtime suspend of a device under the assumption that its
  546. * pm_domain field points to the domain member of an object of type
  547. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  548. */
  549. static int pm_genpd_runtime_suspend(struct device *dev)
  550. {
  551. struct generic_pm_domain *genpd;
  552. struct generic_pm_domain_data *gpd_data;
  553. bool (*stop_ok)(struct device *__dev);
  554. int ret;
  555. dev_dbg(dev, "%s()\n", __func__);
  556. genpd = dev_to_genpd(dev);
  557. if (IS_ERR(genpd))
  558. return -EINVAL;
  559. stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
  560. if (stop_ok && !stop_ok(dev))
  561. return -EBUSY;
  562. ret = genpd_stop_dev(genpd, dev);
  563. if (ret)
  564. return ret;
  565. /*
  566. * If power.irq_safe is set, this routine will be run with interrupts
  567. * off, so it can't use mutexes.
  568. */
  569. if (dev->power.irq_safe)
  570. return 0;
  571. mutex_lock(&genpd->lock);
  572. /*
  573. * If we have an unknown state of the need_restore flag, it means none
  574. * of the runtime PM callbacks has been invoked yet. Let's update the
  575. * flag to reflect that the current state is active.
  576. */
  577. gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
  578. if (gpd_data->need_restore < 0)
  579. gpd_data->need_restore = 0;
  580. genpd->in_progress++;
  581. pm_genpd_poweroff(genpd);
  582. genpd->in_progress--;
  583. mutex_unlock(&genpd->lock);
  584. return 0;
  585. }
  586. /**
  587. * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  588. * @dev: Device to resume.
  589. *
  590. * Carry out a runtime resume of a device under the assumption that its
  591. * pm_domain field points to the domain member of an object of type
  592. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  593. */
  594. static int pm_genpd_runtime_resume(struct device *dev)
  595. {
  596. struct generic_pm_domain *genpd;
  597. DEFINE_WAIT(wait);
  598. int ret;
  599. dev_dbg(dev, "%s()\n", __func__);
  600. genpd = dev_to_genpd(dev);
  601. if (IS_ERR(genpd))
  602. return -EINVAL;
  603. /* If power.irq_safe, the PM domain is never powered off. */
  604. if (dev->power.irq_safe)
  605. return genpd_start_dev_no_timing(genpd, dev);
  606. mutex_lock(&genpd->lock);
  607. ret = __pm_genpd_poweron(genpd);
  608. if (ret) {
  609. mutex_unlock(&genpd->lock);
  610. return ret;
  611. }
  612. genpd->status = GPD_STATE_BUSY;
  613. genpd->resume_count++;
  614. for (;;) {
  615. prepare_to_wait(&genpd->status_wait_queue, &wait,
  616. TASK_UNINTERRUPTIBLE);
  617. /*
  618. * If current is the powering off task, we have been called
  619. * reentrantly from one of the device callbacks, so we should
  620. * not wait.
  621. */
  622. if (!genpd->poweroff_task || genpd->poweroff_task == current)
  623. break;
  624. mutex_unlock(&genpd->lock);
  625. schedule();
  626. mutex_lock(&genpd->lock);
  627. }
  628. finish_wait(&genpd->status_wait_queue, &wait);
  629. __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
  630. genpd->resume_count--;
  631. genpd_set_active(genpd);
  632. wake_up_all(&genpd->status_wait_queue);
  633. mutex_unlock(&genpd->lock);
  634. return 0;
  635. }
  636. static bool pd_ignore_unused;
  637. static int __init pd_ignore_unused_setup(char *__unused)
  638. {
  639. pd_ignore_unused = true;
  640. return 1;
  641. }
  642. __setup("pd_ignore_unused", pd_ignore_unused_setup);
  643. /**
  644. * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
  645. */
  646. void pm_genpd_poweroff_unused(void)
  647. {
  648. struct generic_pm_domain *genpd;
  649. if (pd_ignore_unused) {
  650. pr_warn("genpd: Not disabling unused power domains\n");
  651. return;
  652. }
  653. mutex_lock(&gpd_list_lock);
  654. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  655. genpd_queue_power_off_work(genpd);
  656. mutex_unlock(&gpd_list_lock);
  657. }
  658. static int __init genpd_poweroff_unused(void)
  659. {
  660. pm_genpd_poweroff_unused();
  661. return 0;
  662. }
  663. late_initcall(genpd_poweroff_unused);
  664. #ifdef CONFIG_PM_SLEEP
  665. /**
  666. * pm_genpd_present - Check if the given PM domain has been initialized.
  667. * @genpd: PM domain to check.
  668. */
  669. static bool pm_genpd_present(const struct generic_pm_domain *genpd)
  670. {
  671. const struct generic_pm_domain *gpd;
  672. if (IS_ERR_OR_NULL(genpd))
  673. return false;
  674. list_for_each_entry(gpd, &gpd_list, gpd_list_node)
  675. if (gpd == genpd)
  676. return true;
  677. return false;
  678. }
  679. static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
  680. struct device *dev)
  681. {
  682. return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
  683. }
  684. /**
  685. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
  686. * @genpd: PM domain to power off, if possible.
  687. *
  688. * Check if the given PM domain can be powered off (during system suspend or
  689. * hibernation) and do that if so. Also, in that case propagate to its masters.
  690. *
  691. * This function is only called in "noirq" and "syscore" stages of system power
  692. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  693. * executed sequentially, so it is guaranteed that it will never run twice in
  694. * parallel).
  695. */
  696. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
  697. {
  698. struct gpd_link *link;
  699. if (genpd->status == GPD_STATE_POWER_OFF)
  700. return;
  701. if (genpd->suspended_count != genpd->device_count
  702. || atomic_read(&genpd->sd_count) > 0)
  703. return;
  704. genpd_power_off(genpd);
  705. genpd->status = GPD_STATE_POWER_OFF;
  706. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  707. genpd_sd_counter_dec(link->master);
  708. pm_genpd_sync_poweroff(link->master);
  709. }
  710. }
  711. /**
  712. * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
  713. * @genpd: PM domain to power on.
  714. *
  715. * This function is only called in "noirq" and "syscore" stages of system power
  716. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  717. * executed sequentially, so it is guaranteed that it will never run twice in
  718. * parallel).
  719. */
  720. static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
  721. {
  722. struct gpd_link *link;
  723. if (genpd->status != GPD_STATE_POWER_OFF)
  724. return;
  725. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  726. pm_genpd_sync_poweron(link->master);
  727. genpd_sd_counter_inc(link->master);
  728. }
  729. genpd_power_on(genpd);
  730. genpd->status = GPD_STATE_ACTIVE;
  731. }
  732. /**
  733. * resume_needed - Check whether to resume a device before system suspend.
  734. * @dev: Device to check.
  735. * @genpd: PM domain the device belongs to.
  736. *
  737. * There are two cases in which a device that can wake up the system from sleep
  738. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  739. * to wake up the system and it has to remain active for this purpose while the
  740. * system is in the sleep state and (2) if the device is not enabled to wake up
  741. * the system from sleep states and it generally doesn't generate wakeup signals
  742. * by itself (those signals are generated on its behalf by other parts of the
  743. * system). In the latter case it may be necessary to reconfigure the device's
  744. * wakeup settings during system suspend, because it may have been set up to
  745. * signal remote wakeup from the system's working state as needed by runtime PM.
  746. * Return 'true' in either of the above cases.
  747. */
  748. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  749. {
  750. bool active_wakeup;
  751. if (!device_can_wakeup(dev))
  752. return false;
  753. active_wakeup = genpd_dev_active_wakeup(genpd, dev);
  754. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  755. }
  756. /**
  757. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  758. * @dev: Device to start the transition of.
  759. *
  760. * Start a power transition of a device (during a system-wide power transition)
  761. * under the assumption that its pm_domain field points to the domain member of
  762. * an object of type struct generic_pm_domain representing a PM domain
  763. * consisting of I/O devices.
  764. */
  765. static int pm_genpd_prepare(struct device *dev)
  766. {
  767. struct generic_pm_domain *genpd;
  768. int ret;
  769. dev_dbg(dev, "%s()\n", __func__);
  770. genpd = dev_to_genpd(dev);
  771. if (IS_ERR(genpd))
  772. return -EINVAL;
  773. /*
  774. * If a wakeup request is pending for the device, it should be woken up
  775. * at this point and a system wakeup event should be reported if it's
  776. * set up to wake up the system from sleep states.
  777. */
  778. pm_runtime_get_noresume(dev);
  779. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  780. pm_wakeup_event(dev, 0);
  781. if (pm_wakeup_pending()) {
  782. pm_runtime_put(dev);
  783. return -EBUSY;
  784. }
  785. if (resume_needed(dev, genpd))
  786. pm_runtime_resume(dev);
  787. genpd_acquire_lock(genpd);
  788. if (genpd->prepared_count++ == 0) {
  789. genpd->suspended_count = 0;
  790. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  791. }
  792. genpd_release_lock(genpd);
  793. if (genpd->suspend_power_off) {
  794. pm_runtime_put_noidle(dev);
  795. return 0;
  796. }
  797. /*
  798. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  799. * so pm_genpd_poweron() will return immediately, but if the device
  800. * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
  801. * to make it operational.
  802. */
  803. pm_runtime_resume(dev);
  804. __pm_runtime_disable(dev, false);
  805. ret = pm_generic_prepare(dev);
  806. if (ret) {
  807. mutex_lock(&genpd->lock);
  808. if (--genpd->prepared_count == 0)
  809. genpd->suspend_power_off = false;
  810. mutex_unlock(&genpd->lock);
  811. pm_runtime_enable(dev);
  812. }
  813. pm_runtime_put(dev);
  814. return ret;
  815. }
  816. /**
  817. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  818. * @dev: Device to suspend.
  819. *
  820. * Suspend a device under the assumption that its pm_domain field points to the
  821. * domain member of an object of type struct generic_pm_domain representing
  822. * a PM domain consisting of I/O devices.
  823. */
  824. static int pm_genpd_suspend(struct device *dev)
  825. {
  826. struct generic_pm_domain *genpd;
  827. dev_dbg(dev, "%s()\n", __func__);
  828. genpd = dev_to_genpd(dev);
  829. if (IS_ERR(genpd))
  830. return -EINVAL;
  831. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  832. }
  833. /**
  834. * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
  835. * @dev: Device to suspend.
  836. *
  837. * Carry out a late suspend of a device under the assumption that its
  838. * pm_domain field points to the domain member of an object of type
  839. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  840. */
  841. static int pm_genpd_suspend_late(struct device *dev)
  842. {
  843. struct generic_pm_domain *genpd;
  844. dev_dbg(dev, "%s()\n", __func__);
  845. genpd = dev_to_genpd(dev);
  846. if (IS_ERR(genpd))
  847. return -EINVAL;
  848. return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
  849. }
  850. /**
  851. * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  852. * @dev: Device to suspend.
  853. *
  854. * Stop the device and remove power from the domain if all devices in it have
  855. * been stopped.
  856. */
  857. static int pm_genpd_suspend_noirq(struct device *dev)
  858. {
  859. struct generic_pm_domain *genpd;
  860. dev_dbg(dev, "%s()\n", __func__);
  861. genpd = dev_to_genpd(dev);
  862. if (IS_ERR(genpd))
  863. return -EINVAL;
  864. if (genpd->suspend_power_off
  865. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  866. return 0;
  867. genpd_stop_dev(genpd, dev);
  868. /*
  869. * Since all of the "noirq" callbacks are executed sequentially, it is
  870. * guaranteed that this function will never run twice in parallel for
  871. * the same PM domain, so it is not necessary to use locking here.
  872. */
  873. genpd->suspended_count++;
  874. pm_genpd_sync_poweroff(genpd);
  875. return 0;
  876. }
  877. /**
  878. * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  879. * @dev: Device to resume.
  880. *
  881. * Restore power to the device's PM domain, if necessary, and start the device.
  882. */
  883. static int pm_genpd_resume_noirq(struct device *dev)
  884. {
  885. struct generic_pm_domain *genpd;
  886. dev_dbg(dev, "%s()\n", __func__);
  887. genpd = dev_to_genpd(dev);
  888. if (IS_ERR(genpd))
  889. return -EINVAL;
  890. if (genpd->suspend_power_off
  891. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  892. return 0;
  893. /*
  894. * Since all of the "noirq" callbacks are executed sequentially, it is
  895. * guaranteed that this function will never run twice in parallel for
  896. * the same PM domain, so it is not necessary to use locking here.
  897. */
  898. pm_genpd_sync_poweron(genpd);
  899. genpd->suspended_count--;
  900. return genpd_start_dev(genpd, dev);
  901. }
  902. /**
  903. * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
  904. * @dev: Device to resume.
  905. *
  906. * Carry out an early resume of a device under the assumption that its
  907. * pm_domain field points to the domain member of an object of type
  908. * struct generic_pm_domain representing a power domain consisting of I/O
  909. * devices.
  910. */
  911. static int pm_genpd_resume_early(struct device *dev)
  912. {
  913. struct generic_pm_domain *genpd;
  914. dev_dbg(dev, "%s()\n", __func__);
  915. genpd = dev_to_genpd(dev);
  916. if (IS_ERR(genpd))
  917. return -EINVAL;
  918. return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
  919. }
  920. /**
  921. * pm_genpd_resume - Resume of device in an I/O PM domain.
  922. * @dev: Device to resume.
  923. *
  924. * Resume a device under the assumption that its pm_domain field points to the
  925. * domain member of an object of type struct generic_pm_domain representing
  926. * a power domain consisting of I/O devices.
  927. */
  928. static int pm_genpd_resume(struct device *dev)
  929. {
  930. struct generic_pm_domain *genpd;
  931. dev_dbg(dev, "%s()\n", __func__);
  932. genpd = dev_to_genpd(dev);
  933. if (IS_ERR(genpd))
  934. return -EINVAL;
  935. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  936. }
  937. /**
  938. * pm_genpd_freeze - Freezing a device in an I/O PM domain.
  939. * @dev: Device to freeze.
  940. *
  941. * Freeze a device under the assumption that its pm_domain field points to the
  942. * domain member of an object of type struct generic_pm_domain representing
  943. * a power domain consisting of I/O devices.
  944. */
  945. static int pm_genpd_freeze(struct device *dev)
  946. {
  947. struct generic_pm_domain *genpd;
  948. dev_dbg(dev, "%s()\n", __func__);
  949. genpd = dev_to_genpd(dev);
  950. if (IS_ERR(genpd))
  951. return -EINVAL;
  952. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  953. }
  954. /**
  955. * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
  956. * @dev: Device to freeze.
  957. *
  958. * Carry out a late freeze of a device under the assumption that its
  959. * pm_domain field points to the domain member of an object of type
  960. * struct generic_pm_domain representing a power domain consisting of I/O
  961. * devices.
  962. */
  963. static int pm_genpd_freeze_late(struct device *dev)
  964. {
  965. struct generic_pm_domain *genpd;
  966. dev_dbg(dev, "%s()\n", __func__);
  967. genpd = dev_to_genpd(dev);
  968. if (IS_ERR(genpd))
  969. return -EINVAL;
  970. return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
  971. }
  972. /**
  973. * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  974. * @dev: Device to freeze.
  975. *
  976. * Carry out a late freeze of a device under the assumption that its
  977. * pm_domain field points to the domain member of an object of type
  978. * struct generic_pm_domain representing a power domain consisting of I/O
  979. * devices.
  980. */
  981. static int pm_genpd_freeze_noirq(struct device *dev)
  982. {
  983. struct generic_pm_domain *genpd;
  984. dev_dbg(dev, "%s()\n", __func__);
  985. genpd = dev_to_genpd(dev);
  986. if (IS_ERR(genpd))
  987. return -EINVAL;
  988. return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
  989. }
  990. /**
  991. * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  992. * @dev: Device to thaw.
  993. *
  994. * Start the device, unless power has been removed from the domain already
  995. * before the system transition.
  996. */
  997. static int pm_genpd_thaw_noirq(struct device *dev)
  998. {
  999. struct generic_pm_domain *genpd;
  1000. dev_dbg(dev, "%s()\n", __func__);
  1001. genpd = dev_to_genpd(dev);
  1002. if (IS_ERR(genpd))
  1003. return -EINVAL;
  1004. return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
  1005. }
  1006. /**
  1007. * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
  1008. * @dev: Device to thaw.
  1009. *
  1010. * Carry out an early thaw of a device under the assumption that its
  1011. * pm_domain field points to the domain member of an object of type
  1012. * struct generic_pm_domain representing a power domain consisting of I/O
  1013. * devices.
  1014. */
  1015. static int pm_genpd_thaw_early(struct device *dev)
  1016. {
  1017. struct generic_pm_domain *genpd;
  1018. dev_dbg(dev, "%s()\n", __func__);
  1019. genpd = dev_to_genpd(dev);
  1020. if (IS_ERR(genpd))
  1021. return -EINVAL;
  1022. return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
  1023. }
  1024. /**
  1025. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  1026. * @dev: Device to thaw.
  1027. *
  1028. * Thaw a device under the assumption that its pm_domain field points to the
  1029. * domain member of an object of type struct generic_pm_domain representing
  1030. * a power domain consisting of I/O devices.
  1031. */
  1032. static int pm_genpd_thaw(struct device *dev)
  1033. {
  1034. struct generic_pm_domain *genpd;
  1035. dev_dbg(dev, "%s()\n", __func__);
  1036. genpd = dev_to_genpd(dev);
  1037. if (IS_ERR(genpd))
  1038. return -EINVAL;
  1039. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  1040. }
  1041. /**
  1042. * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  1043. * @dev: Device to resume.
  1044. *
  1045. * Make sure the domain will be in the same power state as before the
  1046. * hibernation the system is resuming from and start the device if necessary.
  1047. */
  1048. static int pm_genpd_restore_noirq(struct device *dev)
  1049. {
  1050. struct generic_pm_domain *genpd;
  1051. dev_dbg(dev, "%s()\n", __func__);
  1052. genpd = dev_to_genpd(dev);
  1053. if (IS_ERR(genpd))
  1054. return -EINVAL;
  1055. /*
  1056. * Since all of the "noirq" callbacks are executed sequentially, it is
  1057. * guaranteed that this function will never run twice in parallel for
  1058. * the same PM domain, so it is not necessary to use locking here.
  1059. *
  1060. * At this point suspended_count == 0 means we are being run for the
  1061. * first time for the given domain in the present cycle.
  1062. */
  1063. if (genpd->suspended_count++ == 0) {
  1064. /*
  1065. * The boot kernel might put the domain into arbitrary state,
  1066. * so make it appear as powered off to pm_genpd_sync_poweron(),
  1067. * so that it tries to power it on in case it was really off.
  1068. */
  1069. genpd->status = GPD_STATE_POWER_OFF;
  1070. if (genpd->suspend_power_off) {
  1071. /*
  1072. * If the domain was off before the hibernation, make
  1073. * sure it will be off going forward.
  1074. */
  1075. genpd_power_off(genpd);
  1076. return 0;
  1077. }
  1078. }
  1079. if (genpd->suspend_power_off)
  1080. return 0;
  1081. pm_genpd_sync_poweron(genpd);
  1082. return genpd_start_dev(genpd, dev);
  1083. }
  1084. /**
  1085. * pm_genpd_complete - Complete power transition of a device in a power domain.
  1086. * @dev: Device to complete the transition of.
  1087. *
  1088. * Complete a power transition of a device (during a system-wide power
  1089. * transition) under the assumption that its pm_domain field points to the
  1090. * domain member of an object of type struct generic_pm_domain representing
  1091. * a power domain consisting of I/O devices.
  1092. */
  1093. static void pm_genpd_complete(struct device *dev)
  1094. {
  1095. struct generic_pm_domain *genpd;
  1096. bool run_complete;
  1097. dev_dbg(dev, "%s()\n", __func__);
  1098. genpd = dev_to_genpd(dev);
  1099. if (IS_ERR(genpd))
  1100. return;
  1101. mutex_lock(&genpd->lock);
  1102. run_complete = !genpd->suspend_power_off;
  1103. if (--genpd->prepared_count == 0)
  1104. genpd->suspend_power_off = false;
  1105. mutex_unlock(&genpd->lock);
  1106. if (run_complete) {
  1107. pm_generic_complete(dev);
  1108. pm_runtime_set_active(dev);
  1109. pm_runtime_enable(dev);
  1110. pm_request_idle(dev);
  1111. }
  1112. }
  1113. /**
  1114. * genpd_syscore_switch - Switch power during system core suspend or resume.
  1115. * @dev: Device that normally is marked as "always on" to switch power for.
  1116. *
  1117. * This routine may only be called during the system core (syscore) suspend or
  1118. * resume phase for devices whose "always on" flags are set.
  1119. */
  1120. static void genpd_syscore_switch(struct device *dev, bool suspend)
  1121. {
  1122. struct generic_pm_domain *genpd;
  1123. genpd = dev_to_genpd(dev);
  1124. if (!pm_genpd_present(genpd))
  1125. return;
  1126. if (suspend) {
  1127. genpd->suspended_count++;
  1128. pm_genpd_sync_poweroff(genpd);
  1129. } else {
  1130. pm_genpd_sync_poweron(genpd);
  1131. genpd->suspended_count--;
  1132. }
  1133. }
  1134. void pm_genpd_syscore_poweroff(struct device *dev)
  1135. {
  1136. genpd_syscore_switch(dev, true);
  1137. }
  1138. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
  1139. void pm_genpd_syscore_poweron(struct device *dev)
  1140. {
  1141. genpd_syscore_switch(dev, false);
  1142. }
  1143. EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
  1144. #else /* !CONFIG_PM_SLEEP */
  1145. #define pm_genpd_prepare NULL
  1146. #define pm_genpd_suspend NULL
  1147. #define pm_genpd_suspend_late NULL
  1148. #define pm_genpd_suspend_noirq NULL
  1149. #define pm_genpd_resume_early NULL
  1150. #define pm_genpd_resume_noirq NULL
  1151. #define pm_genpd_resume NULL
  1152. #define pm_genpd_freeze NULL
  1153. #define pm_genpd_freeze_late NULL
  1154. #define pm_genpd_freeze_noirq NULL
  1155. #define pm_genpd_thaw_early NULL
  1156. #define pm_genpd_thaw_noirq NULL
  1157. #define pm_genpd_thaw NULL
  1158. #define pm_genpd_restore_noirq NULL
  1159. #define pm_genpd_complete NULL
  1160. #endif /* CONFIG_PM_SLEEP */
  1161. static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
  1162. struct generic_pm_domain *genpd,
  1163. struct gpd_timing_data *td)
  1164. {
  1165. struct generic_pm_domain_data *gpd_data;
  1166. int ret;
  1167. ret = dev_pm_get_subsys_data(dev);
  1168. if (ret)
  1169. return ERR_PTR(ret);
  1170. gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
  1171. if (!gpd_data) {
  1172. ret = -ENOMEM;
  1173. goto err_put;
  1174. }
  1175. if (td)
  1176. gpd_data->td = *td;
  1177. gpd_data->base.dev = dev;
  1178. gpd_data->need_restore = -1;
  1179. gpd_data->td.constraint_changed = true;
  1180. gpd_data->td.effective_constraint_ns = -1;
  1181. gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
  1182. spin_lock_irq(&dev->power.lock);
  1183. if (dev->power.subsys_data->domain_data) {
  1184. ret = -EINVAL;
  1185. goto err_free;
  1186. }
  1187. dev->power.subsys_data->domain_data = &gpd_data->base;
  1188. dev->pm_domain = &genpd->domain;
  1189. spin_unlock_irq(&dev->power.lock);
  1190. return gpd_data;
  1191. err_free:
  1192. spin_unlock_irq(&dev->power.lock);
  1193. kfree(gpd_data);
  1194. err_put:
  1195. dev_pm_put_subsys_data(dev);
  1196. return ERR_PTR(ret);
  1197. }
  1198. static void genpd_free_dev_data(struct device *dev,
  1199. struct generic_pm_domain_data *gpd_data)
  1200. {
  1201. spin_lock_irq(&dev->power.lock);
  1202. dev->pm_domain = NULL;
  1203. dev->power.subsys_data->domain_data = NULL;
  1204. spin_unlock_irq(&dev->power.lock);
  1205. kfree(gpd_data);
  1206. dev_pm_put_subsys_data(dev);
  1207. }
  1208. /**
  1209. * __pm_genpd_add_device - Add a device to an I/O PM domain.
  1210. * @genpd: PM domain to add the device to.
  1211. * @dev: Device to be added.
  1212. * @td: Set of PM QoS timing parameters to attach to the device.
  1213. */
  1214. int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  1215. struct gpd_timing_data *td)
  1216. {
  1217. struct generic_pm_domain_data *gpd_data;
  1218. int ret = 0;
  1219. dev_dbg(dev, "%s()\n", __func__);
  1220. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  1221. return -EINVAL;
  1222. gpd_data = genpd_alloc_dev_data(dev, genpd, td);
  1223. if (IS_ERR(gpd_data))
  1224. return PTR_ERR(gpd_data);
  1225. genpd_acquire_lock(genpd);
  1226. if (genpd->prepared_count > 0) {
  1227. ret = -EAGAIN;
  1228. goto out;
  1229. }
  1230. ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
  1231. if (ret)
  1232. goto out;
  1233. genpd->device_count++;
  1234. genpd->max_off_time_changed = true;
  1235. list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
  1236. out:
  1237. genpd_release_lock(genpd);
  1238. if (ret)
  1239. genpd_free_dev_data(dev, gpd_data);
  1240. else
  1241. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1242. return ret;
  1243. }
  1244. /**
  1245. * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
  1246. * @domain_name: Name of the PM domain to add the device to.
  1247. * @dev: Device to be added.
  1248. * @td: Set of PM QoS timing parameters to attach to the device.
  1249. */
  1250. int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
  1251. struct gpd_timing_data *td)
  1252. {
  1253. return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
  1254. }
  1255. /**
  1256. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  1257. * @genpd: PM domain to remove the device from.
  1258. * @dev: Device to be removed.
  1259. */
  1260. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  1261. struct device *dev)
  1262. {
  1263. struct generic_pm_domain_data *gpd_data;
  1264. struct pm_domain_data *pdd;
  1265. int ret = 0;
  1266. dev_dbg(dev, "%s()\n", __func__);
  1267. if (!genpd || genpd != pm_genpd_lookup_dev(dev))
  1268. return -EINVAL;
  1269. /* The above validation also means we have existing domain_data. */
  1270. pdd = dev->power.subsys_data->domain_data;
  1271. gpd_data = to_gpd_data(pdd);
  1272. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1273. genpd_acquire_lock(genpd);
  1274. if (genpd->prepared_count > 0) {
  1275. ret = -EAGAIN;
  1276. goto out;
  1277. }
  1278. genpd->device_count--;
  1279. genpd->max_off_time_changed = true;
  1280. if (genpd->detach_dev)
  1281. genpd->detach_dev(genpd, dev);
  1282. list_del_init(&pdd->list_node);
  1283. genpd_release_lock(genpd);
  1284. genpd_free_dev_data(dev, gpd_data);
  1285. return 0;
  1286. out:
  1287. genpd_release_lock(genpd);
  1288. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1289. return ret;
  1290. }
  1291. /**
  1292. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1293. * @genpd: Master PM domain to add the subdomain to.
  1294. * @subdomain: Subdomain to be added.
  1295. */
  1296. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  1297. struct generic_pm_domain *subdomain)
  1298. {
  1299. struct gpd_link *link;
  1300. int ret = 0;
  1301. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
  1302. || genpd == subdomain)
  1303. return -EINVAL;
  1304. start:
  1305. genpd_acquire_lock(genpd);
  1306. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1307. if (subdomain->status != GPD_STATE_POWER_OFF
  1308. && subdomain->status != GPD_STATE_ACTIVE) {
  1309. mutex_unlock(&subdomain->lock);
  1310. genpd_release_lock(genpd);
  1311. goto start;
  1312. }
  1313. if (genpd->status == GPD_STATE_POWER_OFF
  1314. && subdomain->status != GPD_STATE_POWER_OFF) {
  1315. ret = -EINVAL;
  1316. goto out;
  1317. }
  1318. list_for_each_entry(link, &genpd->master_links, master_node) {
  1319. if (link->slave == subdomain && link->master == genpd) {
  1320. ret = -EINVAL;
  1321. goto out;
  1322. }
  1323. }
  1324. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1325. if (!link) {
  1326. ret = -ENOMEM;
  1327. goto out;
  1328. }
  1329. link->master = genpd;
  1330. list_add_tail(&link->master_node, &genpd->master_links);
  1331. link->slave = subdomain;
  1332. list_add_tail(&link->slave_node, &subdomain->slave_links);
  1333. if (subdomain->status != GPD_STATE_POWER_OFF)
  1334. genpd_sd_counter_inc(genpd);
  1335. out:
  1336. mutex_unlock(&subdomain->lock);
  1337. genpd_release_lock(genpd);
  1338. return ret;
  1339. }
  1340. /**
  1341. * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
  1342. * @master_name: Name of the master PM domain to add the subdomain to.
  1343. * @subdomain_name: Name of the subdomain to be added.
  1344. */
  1345. int pm_genpd_add_subdomain_names(const char *master_name,
  1346. const char *subdomain_name)
  1347. {
  1348. struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
  1349. if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
  1350. return -EINVAL;
  1351. mutex_lock(&gpd_list_lock);
  1352. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  1353. if (!master && !strcmp(gpd->name, master_name))
  1354. master = gpd;
  1355. if (!subdomain && !strcmp(gpd->name, subdomain_name))
  1356. subdomain = gpd;
  1357. if (master && subdomain)
  1358. break;
  1359. }
  1360. mutex_unlock(&gpd_list_lock);
  1361. return pm_genpd_add_subdomain(master, subdomain);
  1362. }
  1363. /**
  1364. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  1365. * @genpd: Master PM domain to remove the subdomain from.
  1366. * @subdomain: Subdomain to be removed.
  1367. */
  1368. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  1369. struct generic_pm_domain *subdomain)
  1370. {
  1371. struct gpd_link *link;
  1372. int ret = -EINVAL;
  1373. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1374. return -EINVAL;
  1375. start:
  1376. genpd_acquire_lock(genpd);
  1377. list_for_each_entry(link, &genpd->master_links, master_node) {
  1378. if (link->slave != subdomain)
  1379. continue;
  1380. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1381. if (subdomain->status != GPD_STATE_POWER_OFF
  1382. && subdomain->status != GPD_STATE_ACTIVE) {
  1383. mutex_unlock(&subdomain->lock);
  1384. genpd_release_lock(genpd);
  1385. goto start;
  1386. }
  1387. list_del(&link->master_node);
  1388. list_del(&link->slave_node);
  1389. kfree(link);
  1390. if (subdomain->status != GPD_STATE_POWER_OFF)
  1391. genpd_sd_counter_dec(genpd);
  1392. mutex_unlock(&subdomain->lock);
  1393. ret = 0;
  1394. break;
  1395. }
  1396. genpd_release_lock(genpd);
  1397. return ret;
  1398. }
  1399. /**
  1400. * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
  1401. * @genpd: PM domain to be connected with cpuidle.
  1402. * @state: cpuidle state this domain can disable/enable.
  1403. *
  1404. * Make a PM domain behave as though it contained a CPU core, that is, instead
  1405. * of calling its power down routine it will enable the given cpuidle state so
  1406. * that the cpuidle subsystem can power it down (if possible and desirable).
  1407. */
  1408. int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
  1409. {
  1410. struct cpuidle_driver *cpuidle_drv;
  1411. struct gpd_cpuidle_data *cpuidle_data;
  1412. struct cpuidle_state *idle_state;
  1413. int ret = 0;
  1414. if (IS_ERR_OR_NULL(genpd) || state < 0)
  1415. return -EINVAL;
  1416. genpd_acquire_lock(genpd);
  1417. if (genpd->cpuidle_data) {
  1418. ret = -EEXIST;
  1419. goto out;
  1420. }
  1421. cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
  1422. if (!cpuidle_data) {
  1423. ret = -ENOMEM;
  1424. goto out;
  1425. }
  1426. cpuidle_drv = cpuidle_driver_ref();
  1427. if (!cpuidle_drv) {
  1428. ret = -ENODEV;
  1429. goto err_drv;
  1430. }
  1431. if (cpuidle_drv->state_count <= state) {
  1432. ret = -EINVAL;
  1433. goto err;
  1434. }
  1435. idle_state = &cpuidle_drv->states[state];
  1436. if (!idle_state->disabled) {
  1437. ret = -EAGAIN;
  1438. goto err;
  1439. }
  1440. cpuidle_data->idle_state = idle_state;
  1441. cpuidle_data->saved_exit_latency = idle_state->exit_latency;
  1442. genpd->cpuidle_data = cpuidle_data;
  1443. genpd_recalc_cpu_exit_latency(genpd);
  1444. out:
  1445. genpd_release_lock(genpd);
  1446. return ret;
  1447. err:
  1448. cpuidle_driver_unref();
  1449. err_drv:
  1450. kfree(cpuidle_data);
  1451. goto out;
  1452. }
  1453. /**
  1454. * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
  1455. * @name: Name of the domain to connect to cpuidle.
  1456. * @state: cpuidle state this domain can manipulate.
  1457. */
  1458. int pm_genpd_name_attach_cpuidle(const char *name, int state)
  1459. {
  1460. return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
  1461. }
  1462. /**
  1463. * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
  1464. * @genpd: PM domain to remove the cpuidle connection from.
  1465. *
  1466. * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
  1467. * given PM domain.
  1468. */
  1469. int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
  1470. {
  1471. struct gpd_cpuidle_data *cpuidle_data;
  1472. struct cpuidle_state *idle_state;
  1473. int ret = 0;
  1474. if (IS_ERR_OR_NULL(genpd))
  1475. return -EINVAL;
  1476. genpd_acquire_lock(genpd);
  1477. cpuidle_data = genpd->cpuidle_data;
  1478. if (!cpuidle_data) {
  1479. ret = -ENODEV;
  1480. goto out;
  1481. }
  1482. idle_state = cpuidle_data->idle_state;
  1483. if (!idle_state->disabled) {
  1484. ret = -EAGAIN;
  1485. goto out;
  1486. }
  1487. idle_state->exit_latency = cpuidle_data->saved_exit_latency;
  1488. cpuidle_driver_unref();
  1489. genpd->cpuidle_data = NULL;
  1490. kfree(cpuidle_data);
  1491. out:
  1492. genpd_release_lock(genpd);
  1493. return ret;
  1494. }
  1495. /**
  1496. * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
  1497. * @name: Name of the domain to disconnect cpuidle from.
  1498. */
  1499. int pm_genpd_name_detach_cpuidle(const char *name)
  1500. {
  1501. return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
  1502. }
  1503. /* Default device callbacks for generic PM domains. */
  1504. /**
  1505. * pm_genpd_default_save_state - Default "save device state" for PM domains.
  1506. * @dev: Device to handle.
  1507. */
  1508. static int pm_genpd_default_save_state(struct device *dev)
  1509. {
  1510. int (*cb)(struct device *__dev);
  1511. if (dev->type && dev->type->pm)
  1512. cb = dev->type->pm->runtime_suspend;
  1513. else if (dev->class && dev->class->pm)
  1514. cb = dev->class->pm->runtime_suspend;
  1515. else if (dev->bus && dev->bus->pm)
  1516. cb = dev->bus->pm->runtime_suspend;
  1517. else
  1518. cb = NULL;
  1519. if (!cb && dev->driver && dev->driver->pm)
  1520. cb = dev->driver->pm->runtime_suspend;
  1521. return cb ? cb(dev) : 0;
  1522. }
  1523. /**
  1524. * pm_genpd_default_restore_state - Default PM domains "restore device state".
  1525. * @dev: Device to handle.
  1526. */
  1527. static int pm_genpd_default_restore_state(struct device *dev)
  1528. {
  1529. int (*cb)(struct device *__dev);
  1530. if (dev->type && dev->type->pm)
  1531. cb = dev->type->pm->runtime_resume;
  1532. else if (dev->class && dev->class->pm)
  1533. cb = dev->class->pm->runtime_resume;
  1534. else if (dev->bus && dev->bus->pm)
  1535. cb = dev->bus->pm->runtime_resume;
  1536. else
  1537. cb = NULL;
  1538. if (!cb && dev->driver && dev->driver->pm)
  1539. cb = dev->driver->pm->runtime_resume;
  1540. return cb ? cb(dev) : 0;
  1541. }
  1542. /**
  1543. * pm_genpd_init - Initialize a generic I/O PM domain object.
  1544. * @genpd: PM domain object to initialize.
  1545. * @gov: PM domain governor to associate with the domain (may be NULL).
  1546. * @is_off: Initial value of the domain's power_is_off field.
  1547. */
  1548. void pm_genpd_init(struct generic_pm_domain *genpd,
  1549. struct dev_power_governor *gov, bool is_off)
  1550. {
  1551. if (IS_ERR_OR_NULL(genpd))
  1552. return;
  1553. INIT_LIST_HEAD(&genpd->master_links);
  1554. INIT_LIST_HEAD(&genpd->slave_links);
  1555. INIT_LIST_HEAD(&genpd->dev_list);
  1556. mutex_init(&genpd->lock);
  1557. genpd->gov = gov;
  1558. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  1559. genpd->in_progress = 0;
  1560. atomic_set(&genpd->sd_count, 0);
  1561. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  1562. init_waitqueue_head(&genpd->status_wait_queue);
  1563. genpd->poweroff_task = NULL;
  1564. genpd->resume_count = 0;
  1565. genpd->device_count = 0;
  1566. genpd->max_off_time_ns = -1;
  1567. genpd->max_off_time_changed = true;
  1568. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  1569. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  1570. genpd->domain.ops.prepare = pm_genpd_prepare;
  1571. genpd->domain.ops.suspend = pm_genpd_suspend;
  1572. genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
  1573. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  1574. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  1575. genpd->domain.ops.resume_early = pm_genpd_resume_early;
  1576. genpd->domain.ops.resume = pm_genpd_resume;
  1577. genpd->domain.ops.freeze = pm_genpd_freeze;
  1578. genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
  1579. genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
  1580. genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
  1581. genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
  1582. genpd->domain.ops.thaw = pm_genpd_thaw;
  1583. genpd->domain.ops.poweroff = pm_genpd_suspend;
  1584. genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
  1585. genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
  1586. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  1587. genpd->domain.ops.restore_early = pm_genpd_resume_early;
  1588. genpd->domain.ops.restore = pm_genpd_resume;
  1589. genpd->domain.ops.complete = pm_genpd_complete;
  1590. genpd->dev_ops.save_state = pm_genpd_default_save_state;
  1591. genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
  1592. if (genpd->flags & GENPD_FLAG_PM_CLK) {
  1593. genpd->dev_ops.stop = pm_clk_suspend;
  1594. genpd->dev_ops.start = pm_clk_resume;
  1595. }
  1596. mutex_lock(&gpd_list_lock);
  1597. list_add(&genpd->gpd_list_node, &gpd_list);
  1598. mutex_unlock(&gpd_list_lock);
  1599. }
  1600. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  1601. /*
  1602. * Device Tree based PM domain providers.
  1603. *
  1604. * The code below implements generic device tree based PM domain providers that
  1605. * bind device tree nodes with generic PM domains registered in the system.
  1606. *
  1607. * Any driver that registers generic PM domains and needs to support binding of
  1608. * devices to these domains is supposed to register a PM domain provider, which
  1609. * maps a PM domain specifier retrieved from the device tree to a PM domain.
  1610. *
  1611. * Two simple mapping functions have been provided for convenience:
  1612. * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
  1613. * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
  1614. * index.
  1615. */
  1616. /**
  1617. * struct of_genpd_provider - PM domain provider registration structure
  1618. * @link: Entry in global list of PM domain providers
  1619. * @node: Pointer to device tree node of PM domain provider
  1620. * @xlate: Provider-specific xlate callback mapping a set of specifier cells
  1621. * into a PM domain.
  1622. * @data: context pointer to be passed into @xlate callback
  1623. */
  1624. struct of_genpd_provider {
  1625. struct list_head link;
  1626. struct device_node *node;
  1627. genpd_xlate_t xlate;
  1628. void *data;
  1629. };
  1630. /* List of registered PM domain providers. */
  1631. static LIST_HEAD(of_genpd_providers);
  1632. /* Mutex to protect the list above. */
  1633. static DEFINE_MUTEX(of_genpd_mutex);
  1634. /**
  1635. * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
  1636. * @genpdspec: OF phandle args to map into a PM domain
  1637. * @data: xlate function private data - pointer to struct generic_pm_domain
  1638. *
  1639. * This is a generic xlate function that can be used to model PM domains that
  1640. * have their own device tree nodes. The private data of xlate function needs
  1641. * to be a valid pointer to struct generic_pm_domain.
  1642. */
  1643. struct generic_pm_domain *__of_genpd_xlate_simple(
  1644. struct of_phandle_args *genpdspec,
  1645. void *data)
  1646. {
  1647. if (genpdspec->args_count != 0)
  1648. return ERR_PTR(-EINVAL);
  1649. return data;
  1650. }
  1651. EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
  1652. /**
  1653. * __of_genpd_xlate_onecell() - Xlate function using a single index.
  1654. * @genpdspec: OF phandle args to map into a PM domain
  1655. * @data: xlate function private data - pointer to struct genpd_onecell_data
  1656. *
  1657. * This is a generic xlate function that can be used to model simple PM domain
  1658. * controllers that have one device tree node and provide multiple PM domains.
  1659. * A single cell is used as an index into an array of PM domains specified in
  1660. * the genpd_onecell_data struct when registering the provider.
  1661. */
  1662. struct generic_pm_domain *__of_genpd_xlate_onecell(
  1663. struct of_phandle_args *genpdspec,
  1664. void *data)
  1665. {
  1666. struct genpd_onecell_data *genpd_data = data;
  1667. unsigned int idx = genpdspec->args[0];
  1668. if (genpdspec->args_count != 1)
  1669. return ERR_PTR(-EINVAL);
  1670. if (idx >= genpd_data->num_domains) {
  1671. pr_err("%s: invalid domain index %u\n", __func__, idx);
  1672. return ERR_PTR(-EINVAL);
  1673. }
  1674. if (!genpd_data->domains[idx])
  1675. return ERR_PTR(-ENOENT);
  1676. return genpd_data->domains[idx];
  1677. }
  1678. EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
  1679. /**
  1680. * __of_genpd_add_provider() - Register a PM domain provider for a node
  1681. * @np: Device node pointer associated with the PM domain provider.
  1682. * @xlate: Callback for decoding PM domain from phandle arguments.
  1683. * @data: Context pointer for @xlate callback.
  1684. */
  1685. int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
  1686. void *data)
  1687. {
  1688. struct of_genpd_provider *cp;
  1689. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  1690. if (!cp)
  1691. return -ENOMEM;
  1692. cp->node = of_node_get(np);
  1693. cp->data = data;
  1694. cp->xlate = xlate;
  1695. mutex_lock(&of_genpd_mutex);
  1696. list_add(&cp->link, &of_genpd_providers);
  1697. mutex_unlock(&of_genpd_mutex);
  1698. pr_debug("Added domain provider from %s\n", np->full_name);
  1699. return 0;
  1700. }
  1701. EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
  1702. /**
  1703. * of_genpd_del_provider() - Remove a previously registered PM domain provider
  1704. * @np: Device node pointer associated with the PM domain provider
  1705. */
  1706. void of_genpd_del_provider(struct device_node *np)
  1707. {
  1708. struct of_genpd_provider *cp;
  1709. mutex_lock(&of_genpd_mutex);
  1710. list_for_each_entry(cp, &of_genpd_providers, link) {
  1711. if (cp->node == np) {
  1712. list_del(&cp->link);
  1713. of_node_put(cp->node);
  1714. kfree(cp);
  1715. break;
  1716. }
  1717. }
  1718. mutex_unlock(&of_genpd_mutex);
  1719. }
  1720. EXPORT_SYMBOL_GPL(of_genpd_del_provider);
  1721. /**
  1722. * of_genpd_get_from_provider() - Look-up PM domain
  1723. * @genpdspec: OF phandle args to use for look-up
  1724. *
  1725. * Looks for a PM domain provider under the node specified by @genpdspec and if
  1726. * found, uses xlate function of the provider to map phandle args to a PM
  1727. * domain.
  1728. *
  1729. * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
  1730. * on failure.
  1731. */
  1732. struct generic_pm_domain *of_genpd_get_from_provider(
  1733. struct of_phandle_args *genpdspec)
  1734. {
  1735. struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
  1736. struct of_genpd_provider *provider;
  1737. mutex_lock(&of_genpd_mutex);
  1738. /* Check if we have such a provider in our array */
  1739. list_for_each_entry(provider, &of_genpd_providers, link) {
  1740. if (provider->node == genpdspec->np)
  1741. genpd = provider->xlate(genpdspec, provider->data);
  1742. if (!IS_ERR(genpd))
  1743. break;
  1744. }
  1745. mutex_unlock(&of_genpd_mutex);
  1746. return genpd;
  1747. }
  1748. EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
  1749. /**
  1750. * genpd_dev_pm_detach - Detach a device from its PM domain.
  1751. * @dev: Device to attach.
  1752. * @power_off: Currently not used
  1753. *
  1754. * Try to locate a corresponding generic PM domain, which the device was
  1755. * attached to previously. If such is found, the device is detached from it.
  1756. */
  1757. static void genpd_dev_pm_detach(struct device *dev, bool power_off)
  1758. {
  1759. struct generic_pm_domain *pd;
  1760. int ret = 0;
  1761. pd = pm_genpd_lookup_dev(dev);
  1762. if (!pd)
  1763. return;
  1764. dev_dbg(dev, "removing from PM domain %s\n", pd->name);
  1765. while (1) {
  1766. ret = pm_genpd_remove_device(pd, dev);
  1767. if (ret != -EAGAIN)
  1768. break;
  1769. cond_resched();
  1770. }
  1771. if (ret < 0) {
  1772. dev_err(dev, "failed to remove from PM domain %s: %d",
  1773. pd->name, ret);
  1774. return;
  1775. }
  1776. /* Check if PM domain can be powered off after removing this device. */
  1777. genpd_queue_power_off_work(pd);
  1778. }
  1779. static void genpd_dev_pm_sync(struct device *dev)
  1780. {
  1781. struct generic_pm_domain *pd;
  1782. pd = dev_to_genpd(dev);
  1783. if (IS_ERR(pd))
  1784. return;
  1785. genpd_queue_power_off_work(pd);
  1786. }
  1787. /**
  1788. * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
  1789. * @dev: Device to attach.
  1790. *
  1791. * Parse device's OF node to find a PM domain specifier. If such is found,
  1792. * attaches the device to retrieved pm_domain ops.
  1793. *
  1794. * Both generic and legacy Samsung-specific DT bindings are supported to keep
  1795. * backwards compatibility with existing DTBs.
  1796. *
  1797. * Returns 0 on successfully attached PM domain or negative error code.
  1798. */
  1799. int genpd_dev_pm_attach(struct device *dev)
  1800. {
  1801. struct of_phandle_args pd_args;
  1802. struct generic_pm_domain *pd;
  1803. int ret;
  1804. if (!dev->of_node)
  1805. return -ENODEV;
  1806. if (dev->pm_domain)
  1807. return -EEXIST;
  1808. ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
  1809. "#power-domain-cells", 0, &pd_args);
  1810. if (ret < 0) {
  1811. if (ret != -ENOENT)
  1812. return ret;
  1813. /*
  1814. * Try legacy Samsung-specific bindings
  1815. * (for backwards compatibility of DT ABI)
  1816. */
  1817. pd_args.args_count = 0;
  1818. pd_args.np = of_parse_phandle(dev->of_node,
  1819. "samsung,power-domain", 0);
  1820. if (!pd_args.np)
  1821. return -ENOENT;
  1822. }
  1823. pd = of_genpd_get_from_provider(&pd_args);
  1824. if (IS_ERR(pd)) {
  1825. dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
  1826. __func__, PTR_ERR(pd));
  1827. of_node_put(dev->of_node);
  1828. return PTR_ERR(pd);
  1829. }
  1830. dev_dbg(dev, "adding to PM domain %s\n", pd->name);
  1831. while (1) {
  1832. ret = pm_genpd_add_device(pd, dev);
  1833. if (ret != -EAGAIN)
  1834. break;
  1835. cond_resched();
  1836. }
  1837. if (ret < 0) {
  1838. dev_err(dev, "failed to add to PM domain %s: %d",
  1839. pd->name, ret);
  1840. of_node_put(dev->of_node);
  1841. return ret;
  1842. }
  1843. dev->pm_domain->detach = genpd_dev_pm_detach;
  1844. dev->pm_domain->sync = genpd_dev_pm_sync;
  1845. pm_genpd_poweron(pd);
  1846. return 0;
  1847. }
  1848. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
  1849. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  1850. /*** debugfs support ***/
  1851. #ifdef CONFIG_PM_ADVANCED_DEBUG
  1852. #include <linux/pm.h>
  1853. #include <linux/device.h>
  1854. #include <linux/debugfs.h>
  1855. #include <linux/seq_file.h>
  1856. #include <linux/init.h>
  1857. #include <linux/kobject.h>
  1858. static struct dentry *pm_genpd_debugfs_dir;
  1859. /*
  1860. * TODO: This function is a slightly modified version of rtpm_status_show
  1861. * from sysfs.c, so generalize it.
  1862. */
  1863. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  1864. {
  1865. static const char * const status_lookup[] = {
  1866. [RPM_ACTIVE] = "active",
  1867. [RPM_RESUMING] = "resuming",
  1868. [RPM_SUSPENDED] = "suspended",
  1869. [RPM_SUSPENDING] = "suspending"
  1870. };
  1871. const char *p = "";
  1872. if (dev->power.runtime_error)
  1873. p = "error";
  1874. else if (dev->power.disable_depth)
  1875. p = "unsupported";
  1876. else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
  1877. p = status_lookup[dev->power.runtime_status];
  1878. else
  1879. WARN_ON(1);
  1880. seq_puts(s, p);
  1881. }
  1882. static int pm_genpd_summary_one(struct seq_file *s,
  1883. struct generic_pm_domain *genpd)
  1884. {
  1885. static const char * const status_lookup[] = {
  1886. [GPD_STATE_ACTIVE] = "on",
  1887. [GPD_STATE_WAIT_MASTER] = "wait-master",
  1888. [GPD_STATE_BUSY] = "busy",
  1889. [GPD_STATE_REPEAT] = "off-in-progress",
  1890. [GPD_STATE_POWER_OFF] = "off"
  1891. };
  1892. struct pm_domain_data *pm_data;
  1893. const char *kobj_path;
  1894. struct gpd_link *link;
  1895. int ret;
  1896. ret = mutex_lock_interruptible(&genpd->lock);
  1897. if (ret)
  1898. return -ERESTARTSYS;
  1899. if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
  1900. goto exit;
  1901. seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
  1902. /*
  1903. * Modifications on the list require holding locks on both
  1904. * master and slave, so we are safe.
  1905. * Also genpd->name is immutable.
  1906. */
  1907. list_for_each_entry(link, &genpd->master_links, master_node) {
  1908. seq_printf(s, "%s", link->slave->name);
  1909. if (!list_is_last(&link->master_node, &genpd->master_links))
  1910. seq_puts(s, ", ");
  1911. }
  1912. list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
  1913. kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
  1914. if (kobj_path == NULL)
  1915. continue;
  1916. seq_printf(s, "\n %-50s ", kobj_path);
  1917. rtpm_status_str(s, pm_data->dev);
  1918. kfree(kobj_path);
  1919. }
  1920. seq_puts(s, "\n");
  1921. exit:
  1922. mutex_unlock(&genpd->lock);
  1923. return 0;
  1924. }
  1925. static int pm_genpd_summary_show(struct seq_file *s, void *data)
  1926. {
  1927. struct generic_pm_domain *genpd;
  1928. int ret = 0;
  1929. seq_puts(s, " domain status slaves\n");
  1930. seq_puts(s, " /device runtime status\n");
  1931. seq_puts(s, "----------------------------------------------------------------------\n");
  1932. ret = mutex_lock_interruptible(&gpd_list_lock);
  1933. if (ret)
  1934. return -ERESTARTSYS;
  1935. list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
  1936. ret = pm_genpd_summary_one(s, genpd);
  1937. if (ret)
  1938. break;
  1939. }
  1940. mutex_unlock(&gpd_list_lock);
  1941. return ret;
  1942. }
  1943. static int pm_genpd_summary_open(struct inode *inode, struct file *file)
  1944. {
  1945. return single_open(file, pm_genpd_summary_show, NULL);
  1946. }
  1947. static const struct file_operations pm_genpd_summary_fops = {
  1948. .open = pm_genpd_summary_open,
  1949. .read = seq_read,
  1950. .llseek = seq_lseek,
  1951. .release = single_release,
  1952. };
  1953. static int __init pm_genpd_debug_init(void)
  1954. {
  1955. struct dentry *d;
  1956. pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
  1957. if (!pm_genpd_debugfs_dir)
  1958. return -ENOMEM;
  1959. d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
  1960. pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
  1961. if (!d)
  1962. return -ENOMEM;
  1963. return 0;
  1964. }
  1965. late_initcall(pm_genpd_debug_init);
  1966. static void __exit pm_genpd_debug_exit(void)
  1967. {
  1968. debugfs_remove_recursive(pm_genpd_debugfs_dir);
  1969. }
  1970. __exitcall(pm_genpd_debug_exit);
  1971. #endif /* CONFIG_PM_ADVANCED_DEBUG */