domain.c 61 KB

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