domain.c 60 KB

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