domain.c 55 KB

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