domain.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/pm_domain.h>
  13. #include <linux/pm_qos.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/suspend.h>
  18. #include <linux/export.h>
  19. #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
  20. ({ \
  21. type (*__routine)(struct device *__d); \
  22. type __ret = (type)0; \
  23. \
  24. __routine = genpd->dev_ops.callback; \
  25. if (__routine) { \
  26. __ret = __routine(dev); \
  27. } else { \
  28. __routine = dev_gpd_data(dev)->ops.callback; \
  29. if (__routine) \
  30. __ret = __routine(dev); \
  31. } \
  32. __ret; \
  33. })
  34. #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
  35. ({ \
  36. ktime_t __start = ktime_get(); \
  37. type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
  38. s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
  39. struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
  40. if (!__retval && __elapsed > __td->field) { \
  41. __td->field = __elapsed; \
  42. dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
  43. __elapsed); \
  44. genpd->max_off_time_changed = true; \
  45. __td->constraint_changed = true; \
  46. } \
  47. __retval; \
  48. })
  49. static LIST_HEAD(gpd_list);
  50. static DEFINE_MUTEX(gpd_list_lock);
  51. #ifdef CONFIG_PM
  52. struct generic_pm_domain *dev_to_genpd(struct device *dev)
  53. {
  54. if (IS_ERR_OR_NULL(dev->pm_domain))
  55. return ERR_PTR(-EINVAL);
  56. return pd_to_genpd(dev->pm_domain);
  57. }
  58. static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
  59. {
  60. return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
  61. stop_latency_ns, "stop");
  62. }
  63. static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
  64. {
  65. return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
  66. start_latency_ns, "start");
  67. }
  68. static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
  69. {
  70. return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
  71. save_state_latency_ns, "state save");
  72. }
  73. static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
  74. {
  75. return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
  76. restore_state_latency_ns,
  77. "state restore");
  78. }
  79. static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  80. {
  81. bool ret = false;
  82. if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  83. ret = !!atomic_dec_and_test(&genpd->sd_count);
  84. return ret;
  85. }
  86. static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
  87. {
  88. atomic_inc(&genpd->sd_count);
  89. smp_mb__after_atomic_inc();
  90. }
  91. static void genpd_acquire_lock(struct generic_pm_domain *genpd)
  92. {
  93. DEFINE_WAIT(wait);
  94. mutex_lock(&genpd->lock);
  95. /*
  96. * Wait for the domain to transition into either the active,
  97. * or the power off state.
  98. */
  99. for (;;) {
  100. prepare_to_wait(&genpd->status_wait_queue, &wait,
  101. TASK_UNINTERRUPTIBLE);
  102. if (genpd->status == GPD_STATE_ACTIVE
  103. || genpd->status == GPD_STATE_POWER_OFF)
  104. break;
  105. mutex_unlock(&genpd->lock);
  106. schedule();
  107. mutex_lock(&genpd->lock);
  108. }
  109. finish_wait(&genpd->status_wait_queue, &wait);
  110. }
  111. static void genpd_release_lock(struct generic_pm_domain *genpd)
  112. {
  113. mutex_unlock(&genpd->lock);
  114. }
  115. static void genpd_set_active(struct generic_pm_domain *genpd)
  116. {
  117. if (genpd->resume_count == 0)
  118. genpd->status = GPD_STATE_ACTIVE;
  119. }
  120. /**
  121. * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
  122. * @genpd: PM domain to power up.
  123. *
  124. * Restore power to @genpd and all of its masters so that it is possible to
  125. * resume a device belonging to it.
  126. */
  127. int __pm_genpd_poweron(struct generic_pm_domain *genpd)
  128. __releases(&genpd->lock) __acquires(&genpd->lock)
  129. {
  130. struct gpd_link *link;
  131. DEFINE_WAIT(wait);
  132. int ret = 0;
  133. /* If the domain's master is being waited for, we have to wait too. */
  134. for (;;) {
  135. prepare_to_wait(&genpd->status_wait_queue, &wait,
  136. TASK_UNINTERRUPTIBLE);
  137. if (genpd->status != GPD_STATE_WAIT_MASTER)
  138. break;
  139. mutex_unlock(&genpd->lock);
  140. schedule();
  141. mutex_lock(&genpd->lock);
  142. }
  143. finish_wait(&genpd->status_wait_queue, &wait);
  144. if (genpd->status == GPD_STATE_ACTIVE
  145. || (genpd->prepared_count > 0 && genpd->suspend_power_off))
  146. return 0;
  147. if (genpd->status != GPD_STATE_POWER_OFF) {
  148. genpd_set_active(genpd);
  149. return 0;
  150. }
  151. /*
  152. * The list is guaranteed not to change while the loop below is being
  153. * executed, unless one of the masters' .power_on() callbacks fiddles
  154. * with it.
  155. */
  156. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  157. genpd_sd_counter_inc(link->master);
  158. genpd->status = GPD_STATE_WAIT_MASTER;
  159. mutex_unlock(&genpd->lock);
  160. ret = pm_genpd_poweron(link->master);
  161. mutex_lock(&genpd->lock);
  162. /*
  163. * The "wait for parent" status is guaranteed not to change
  164. * while the master is powering on.
  165. */
  166. genpd->status = GPD_STATE_POWER_OFF;
  167. wake_up_all(&genpd->status_wait_queue);
  168. if (ret) {
  169. genpd_sd_counter_dec(link->master);
  170. goto err;
  171. }
  172. }
  173. if (genpd->power_on) {
  174. ktime_t time_start = ktime_get();
  175. s64 elapsed_ns;
  176. ret = genpd->power_on(genpd);
  177. if (ret)
  178. goto err;
  179. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  180. if (elapsed_ns > genpd->power_on_latency_ns) {
  181. genpd->power_on_latency_ns = elapsed_ns;
  182. genpd->max_off_time_changed = true;
  183. if (genpd->name)
  184. pr_warning("%s: Power-on latency exceeded, "
  185. "new value %lld ns\n", genpd->name,
  186. elapsed_ns);
  187. }
  188. }
  189. genpd_set_active(genpd);
  190. return 0;
  191. err:
  192. list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
  193. genpd_sd_counter_dec(link->master);
  194. return ret;
  195. }
  196. /**
  197. * pm_genpd_poweron - Restore power to a given PM domain and its masters.
  198. * @genpd: PM domain to power up.
  199. */
  200. int pm_genpd_poweron(struct generic_pm_domain *genpd)
  201. {
  202. int ret;
  203. mutex_lock(&genpd->lock);
  204. ret = __pm_genpd_poweron(genpd);
  205. mutex_unlock(&genpd->lock);
  206. return ret;
  207. }
  208. #endif /* CONFIG_PM */
  209. #ifdef CONFIG_PM_RUNTIME
  210. static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  211. unsigned long val, void *ptr)
  212. {
  213. struct generic_pm_domain_data *gpd_data;
  214. struct device *dev;
  215. gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
  216. mutex_lock(&gpd_data->lock);
  217. dev = gpd_data->base.dev;
  218. if (!dev) {
  219. mutex_unlock(&gpd_data->lock);
  220. return NOTIFY_DONE;
  221. }
  222. mutex_unlock(&gpd_data->lock);
  223. for (;;) {
  224. struct generic_pm_domain *genpd;
  225. struct pm_domain_data *pdd;
  226. spin_lock_irq(&dev->power.lock);
  227. pdd = dev->power.subsys_data ?
  228. dev->power.subsys_data->domain_data : NULL;
  229. if (pdd) {
  230. to_gpd_data(pdd)->td.constraint_changed = true;
  231. genpd = dev_to_genpd(dev);
  232. } else {
  233. genpd = ERR_PTR(-ENODATA);
  234. }
  235. spin_unlock_irq(&dev->power.lock);
  236. if (!IS_ERR(genpd)) {
  237. mutex_lock(&genpd->lock);
  238. genpd->max_off_time_changed = true;
  239. mutex_unlock(&genpd->lock);
  240. }
  241. dev = dev->parent;
  242. if (!dev || dev->power.ignore_children)
  243. break;
  244. }
  245. return NOTIFY_DONE;
  246. }
  247. /**
  248. * __pm_genpd_save_device - Save the pre-suspend state of a device.
  249. * @pdd: Domain data of the device to save the state of.
  250. * @genpd: PM domain the device belongs to.
  251. */
  252. static int __pm_genpd_save_device(struct pm_domain_data *pdd,
  253. struct generic_pm_domain *genpd)
  254. __releases(&genpd->lock) __acquires(&genpd->lock)
  255. {
  256. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  257. struct device *dev = pdd->dev;
  258. int ret = 0;
  259. if (gpd_data->need_restore)
  260. return 0;
  261. mutex_unlock(&genpd->lock);
  262. genpd_start_dev(genpd, dev);
  263. ret = genpd_save_dev(genpd, dev);
  264. genpd_stop_dev(genpd, dev);
  265. mutex_lock(&genpd->lock);
  266. if (!ret)
  267. gpd_data->need_restore = true;
  268. return ret;
  269. }
  270. /**
  271. * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
  272. * @pdd: Domain data of the device to restore the state of.
  273. * @genpd: PM domain the device belongs to.
  274. */
  275. static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
  276. struct generic_pm_domain *genpd)
  277. __releases(&genpd->lock) __acquires(&genpd->lock)
  278. {
  279. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  280. struct device *dev = pdd->dev;
  281. if (!gpd_data->need_restore)
  282. return;
  283. mutex_unlock(&genpd->lock);
  284. genpd_start_dev(genpd, dev);
  285. genpd_restore_dev(genpd, dev);
  286. genpd_stop_dev(genpd, dev);
  287. mutex_lock(&genpd->lock);
  288. gpd_data->need_restore = false;
  289. }
  290. /**
  291. * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
  292. * @genpd: PM domain to check.
  293. *
  294. * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
  295. * a "power off" operation, which means that a "power on" has occured in the
  296. * meantime, or if its resume_count field is different from zero, which means
  297. * that one of its devices has been resumed in the meantime.
  298. */
  299. static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
  300. {
  301. return genpd->status == GPD_STATE_WAIT_MASTER
  302. || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
  303. }
  304. /**
  305. * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
  306. * @genpd: PM domait to power off.
  307. *
  308. * Queue up the execution of pm_genpd_poweroff() unless it's already been done
  309. * before.
  310. */
  311. void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  312. {
  313. if (!work_pending(&genpd->power_off_work))
  314. queue_work(pm_wq, &genpd->power_off_work);
  315. }
  316. /**
  317. * pm_genpd_poweroff - Remove power from a given PM domain.
  318. * @genpd: PM domain to power down.
  319. *
  320. * If all of the @genpd's devices have been suspended and all of its subdomains
  321. * have been powered down, run the runtime suspend callbacks provided by all of
  322. * the @genpd's devices' drivers and remove power from @genpd.
  323. */
  324. static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
  325. __releases(&genpd->lock) __acquires(&genpd->lock)
  326. {
  327. struct pm_domain_data *pdd;
  328. struct gpd_link *link;
  329. unsigned int not_suspended;
  330. int ret = 0;
  331. start:
  332. /*
  333. * Do not try to power off the domain in the following situations:
  334. * (1) The domain is already in the "power off" state.
  335. * (2) The domain is waiting for its master to power up.
  336. * (3) One of the domain's devices is being resumed right now.
  337. * (4) System suspend is in progress.
  338. */
  339. if (genpd->status == GPD_STATE_POWER_OFF
  340. || genpd->status == GPD_STATE_WAIT_MASTER
  341. || genpd->resume_count > 0 || genpd->prepared_count > 0)
  342. return 0;
  343. if (atomic_read(&genpd->sd_count) > 0)
  344. return -EBUSY;
  345. not_suspended = 0;
  346. list_for_each_entry(pdd, &genpd->dev_list, list_node)
  347. if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
  348. || pdd->dev->power.irq_safe || to_gpd_data(pdd)->always_on))
  349. not_suspended++;
  350. if (not_suspended > genpd->in_progress)
  351. return -EBUSY;
  352. if (genpd->poweroff_task) {
  353. /*
  354. * Another instance of pm_genpd_poweroff() is executing
  355. * callbacks, so tell it to start over and return.
  356. */
  357. genpd->status = GPD_STATE_REPEAT;
  358. return 0;
  359. }
  360. if (genpd->gov && genpd->gov->power_down_ok) {
  361. if (!genpd->gov->power_down_ok(&genpd->domain))
  362. return -EAGAIN;
  363. }
  364. genpd->status = GPD_STATE_BUSY;
  365. genpd->poweroff_task = current;
  366. list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
  367. ret = atomic_read(&genpd->sd_count) == 0 ?
  368. __pm_genpd_save_device(pdd, genpd) : -EBUSY;
  369. if (genpd_abort_poweroff(genpd))
  370. goto out;
  371. if (ret) {
  372. genpd_set_active(genpd);
  373. goto out;
  374. }
  375. if (genpd->status == GPD_STATE_REPEAT) {
  376. genpd->poweroff_task = NULL;
  377. goto start;
  378. }
  379. }
  380. if (genpd->power_off) {
  381. ktime_t time_start;
  382. s64 elapsed_ns;
  383. if (atomic_read(&genpd->sd_count) > 0) {
  384. ret = -EBUSY;
  385. goto out;
  386. }
  387. time_start = ktime_get();
  388. /*
  389. * If sd_count > 0 at this point, one of the subdomains hasn't
  390. * managed to call pm_genpd_poweron() for the master yet after
  391. * incrementing it. In that case pm_genpd_poweron() will wait
  392. * for us to drop the lock, so we can call .power_off() and let
  393. * the pm_genpd_poweron() restore power for us (this shouldn't
  394. * happen very often).
  395. */
  396. ret = genpd->power_off(genpd);
  397. if (ret == -EBUSY) {
  398. genpd_set_active(genpd);
  399. goto out;
  400. }
  401. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  402. if (elapsed_ns > genpd->power_off_latency_ns) {
  403. genpd->power_off_latency_ns = elapsed_ns;
  404. genpd->max_off_time_changed = true;
  405. if (genpd->name)
  406. pr_warning("%s: Power-off latency exceeded, "
  407. "new value %lld ns\n", genpd->name,
  408. elapsed_ns);
  409. }
  410. }
  411. genpd->status = GPD_STATE_POWER_OFF;
  412. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  413. genpd_sd_counter_dec(link->master);
  414. genpd_queue_power_off_work(link->master);
  415. }
  416. out:
  417. genpd->poweroff_task = NULL;
  418. wake_up_all(&genpd->status_wait_queue);
  419. return ret;
  420. }
  421. /**
  422. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  423. * @work: Work structure used for scheduling the execution of this function.
  424. */
  425. static void genpd_power_off_work_fn(struct work_struct *work)
  426. {
  427. struct generic_pm_domain *genpd;
  428. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  429. genpd_acquire_lock(genpd);
  430. pm_genpd_poweroff(genpd);
  431. genpd_release_lock(genpd);
  432. }
  433. /**
  434. * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  435. * @dev: Device to suspend.
  436. *
  437. * Carry out a runtime suspend of a device under the assumption that its
  438. * pm_domain field points to the domain member of an object of type
  439. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  440. */
  441. static int pm_genpd_runtime_suspend(struct device *dev)
  442. {
  443. struct generic_pm_domain *genpd;
  444. bool (*stop_ok)(struct device *__dev);
  445. int ret;
  446. dev_dbg(dev, "%s()\n", __func__);
  447. genpd = dev_to_genpd(dev);
  448. if (IS_ERR(genpd))
  449. return -EINVAL;
  450. might_sleep_if(!genpd->dev_irq_safe);
  451. if (dev_gpd_data(dev)->always_on)
  452. return -EBUSY;
  453. stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
  454. if (stop_ok && !stop_ok(dev))
  455. return -EBUSY;
  456. ret = genpd_stop_dev(genpd, dev);
  457. if (ret)
  458. return ret;
  459. /*
  460. * If power.irq_safe is set, this routine will be run with interrupts
  461. * off, so it can't use mutexes.
  462. */
  463. if (dev->power.irq_safe)
  464. return 0;
  465. mutex_lock(&genpd->lock);
  466. genpd->in_progress++;
  467. pm_genpd_poweroff(genpd);
  468. genpd->in_progress--;
  469. mutex_unlock(&genpd->lock);
  470. return 0;
  471. }
  472. /**
  473. * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  474. * @dev: Device to resume.
  475. *
  476. * Carry out a runtime resume of a device under the assumption that its
  477. * pm_domain field points to the domain member of an object of type
  478. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  479. */
  480. static int pm_genpd_runtime_resume(struct device *dev)
  481. {
  482. struct generic_pm_domain *genpd;
  483. DEFINE_WAIT(wait);
  484. int ret;
  485. dev_dbg(dev, "%s()\n", __func__);
  486. genpd = dev_to_genpd(dev);
  487. if (IS_ERR(genpd))
  488. return -EINVAL;
  489. might_sleep_if(!genpd->dev_irq_safe);
  490. /* If power.irq_safe, the PM domain is never powered off. */
  491. if (dev->power.irq_safe)
  492. goto out;
  493. mutex_lock(&genpd->lock);
  494. ret = __pm_genpd_poweron(genpd);
  495. if (ret) {
  496. mutex_unlock(&genpd->lock);
  497. return ret;
  498. }
  499. genpd->status = GPD_STATE_BUSY;
  500. genpd->resume_count++;
  501. for (;;) {
  502. prepare_to_wait(&genpd->status_wait_queue, &wait,
  503. TASK_UNINTERRUPTIBLE);
  504. /*
  505. * If current is the powering off task, we have been called
  506. * reentrantly from one of the device callbacks, so we should
  507. * not wait.
  508. */
  509. if (!genpd->poweroff_task || genpd->poweroff_task == current)
  510. break;
  511. mutex_unlock(&genpd->lock);
  512. schedule();
  513. mutex_lock(&genpd->lock);
  514. }
  515. finish_wait(&genpd->status_wait_queue, &wait);
  516. __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
  517. genpd->resume_count--;
  518. genpd_set_active(genpd);
  519. wake_up_all(&genpd->status_wait_queue);
  520. mutex_unlock(&genpd->lock);
  521. out:
  522. genpd_start_dev(genpd, dev);
  523. return 0;
  524. }
  525. /**
  526. * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
  527. */
  528. void pm_genpd_poweroff_unused(void)
  529. {
  530. struct generic_pm_domain *genpd;
  531. mutex_lock(&gpd_list_lock);
  532. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  533. genpd_queue_power_off_work(genpd);
  534. mutex_unlock(&gpd_list_lock);
  535. }
  536. #else
  537. static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  538. unsigned long val, void *ptr)
  539. {
  540. return NOTIFY_DONE;
  541. }
  542. static inline void genpd_power_off_work_fn(struct work_struct *work) {}
  543. #define pm_genpd_runtime_suspend NULL
  544. #define pm_genpd_runtime_resume NULL
  545. #endif /* CONFIG_PM_RUNTIME */
  546. #ifdef CONFIG_PM_SLEEP
  547. static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
  548. struct device *dev)
  549. {
  550. return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
  551. }
  552. static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
  553. {
  554. return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
  555. }
  556. static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
  557. {
  558. return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
  559. }
  560. static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
  561. {
  562. return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
  563. }
  564. static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
  565. {
  566. return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
  567. }
  568. static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
  569. {
  570. return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
  571. }
  572. static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
  573. {
  574. return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
  575. }
  576. static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
  577. {
  578. return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
  579. }
  580. static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
  581. {
  582. return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
  583. }
  584. /**
  585. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
  586. * @genpd: PM domain to power off, if possible.
  587. *
  588. * Check if the given PM domain can be powered off (during system suspend or
  589. * hibernation) and do that if so. Also, in that case propagate to its masters.
  590. *
  591. * This function is only called in "noirq" stages of system power transitions,
  592. * so it need not acquire locks (all of the "noirq" callbacks are executed
  593. * sequentially, so it is guaranteed that it will never run twice in parallel).
  594. */
  595. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
  596. {
  597. struct gpd_link *link;
  598. if (genpd->status == GPD_STATE_POWER_OFF)
  599. return;
  600. if (genpd->suspended_count != genpd->device_count
  601. || atomic_read(&genpd->sd_count) > 0)
  602. return;
  603. if (genpd->power_off)
  604. genpd->power_off(genpd);
  605. genpd->status = GPD_STATE_POWER_OFF;
  606. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  607. genpd_sd_counter_dec(link->master);
  608. pm_genpd_sync_poweroff(link->master);
  609. }
  610. }
  611. /**
  612. * resume_needed - Check whether to resume a device before system suspend.
  613. * @dev: Device to check.
  614. * @genpd: PM domain the device belongs to.
  615. *
  616. * There are two cases in which a device that can wake up the system from sleep
  617. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  618. * to wake up the system and it has to remain active for this purpose while the
  619. * system is in the sleep state and (2) if the device is not enabled to wake up
  620. * the system from sleep states and it generally doesn't generate wakeup signals
  621. * by itself (those signals are generated on its behalf by other parts of the
  622. * system). In the latter case it may be necessary to reconfigure the device's
  623. * wakeup settings during system suspend, because it may have been set up to
  624. * signal remote wakeup from the system's working state as needed by runtime PM.
  625. * Return 'true' in either of the above cases.
  626. */
  627. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  628. {
  629. bool active_wakeup;
  630. if (!device_can_wakeup(dev))
  631. return false;
  632. active_wakeup = genpd_dev_active_wakeup(genpd, dev);
  633. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  634. }
  635. /**
  636. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  637. * @dev: Device to start the transition of.
  638. *
  639. * Start a power transition of a device (during a system-wide power transition)
  640. * under the assumption that its pm_domain field points to the domain member of
  641. * an object of type struct generic_pm_domain representing a PM domain
  642. * consisting of I/O devices.
  643. */
  644. static int pm_genpd_prepare(struct device *dev)
  645. {
  646. struct generic_pm_domain *genpd;
  647. int ret;
  648. dev_dbg(dev, "%s()\n", __func__);
  649. genpd = dev_to_genpd(dev);
  650. if (IS_ERR(genpd))
  651. return -EINVAL;
  652. /*
  653. * If a wakeup request is pending for the device, it should be woken up
  654. * at this point and a system wakeup event should be reported if it's
  655. * set up to wake up the system from sleep states.
  656. */
  657. pm_runtime_get_noresume(dev);
  658. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  659. pm_wakeup_event(dev, 0);
  660. if (pm_wakeup_pending()) {
  661. pm_runtime_put_sync(dev);
  662. return -EBUSY;
  663. }
  664. if (resume_needed(dev, genpd))
  665. pm_runtime_resume(dev);
  666. genpd_acquire_lock(genpd);
  667. if (genpd->prepared_count++ == 0) {
  668. genpd->suspended_count = 0;
  669. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  670. }
  671. genpd_release_lock(genpd);
  672. if (genpd->suspend_power_off) {
  673. pm_runtime_put_noidle(dev);
  674. return 0;
  675. }
  676. /*
  677. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  678. * so pm_genpd_poweron() will return immediately, but if the device
  679. * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
  680. * to make it operational.
  681. */
  682. pm_runtime_resume(dev);
  683. __pm_runtime_disable(dev, false);
  684. ret = pm_generic_prepare(dev);
  685. if (ret) {
  686. mutex_lock(&genpd->lock);
  687. if (--genpd->prepared_count == 0)
  688. genpd->suspend_power_off = false;
  689. mutex_unlock(&genpd->lock);
  690. pm_runtime_enable(dev);
  691. }
  692. pm_runtime_put_sync(dev);
  693. return ret;
  694. }
  695. /**
  696. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  697. * @dev: Device to suspend.
  698. *
  699. * Suspend a device under the assumption that its pm_domain field points to the
  700. * domain member of an object of type struct generic_pm_domain representing
  701. * a PM domain consisting of I/O devices.
  702. */
  703. static int pm_genpd_suspend(struct device *dev)
  704. {
  705. struct generic_pm_domain *genpd;
  706. dev_dbg(dev, "%s()\n", __func__);
  707. genpd = dev_to_genpd(dev);
  708. if (IS_ERR(genpd))
  709. return -EINVAL;
  710. return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
  711. }
  712. /**
  713. * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
  714. * @dev: Device to suspend.
  715. *
  716. * Carry out a late suspend of a device under the assumption that its
  717. * pm_domain field points to the domain member of an object of type
  718. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  719. */
  720. static int pm_genpd_suspend_late(struct device *dev)
  721. {
  722. struct generic_pm_domain *genpd;
  723. dev_dbg(dev, "%s()\n", __func__);
  724. genpd = dev_to_genpd(dev);
  725. if (IS_ERR(genpd))
  726. return -EINVAL;
  727. return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
  728. }
  729. /**
  730. * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  731. * @dev: Device to suspend.
  732. *
  733. * Stop the device and remove power from the domain if all devices in it have
  734. * been stopped.
  735. */
  736. static int pm_genpd_suspend_noirq(struct device *dev)
  737. {
  738. struct generic_pm_domain *genpd;
  739. dev_dbg(dev, "%s()\n", __func__);
  740. genpd = dev_to_genpd(dev);
  741. if (IS_ERR(genpd))
  742. return -EINVAL;
  743. if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
  744. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  745. return 0;
  746. genpd_stop_dev(genpd, dev);
  747. /*
  748. * Since all of the "noirq" callbacks are executed sequentially, it is
  749. * guaranteed that this function will never run twice in parallel for
  750. * the same PM domain, so it is not necessary to use locking here.
  751. */
  752. genpd->suspended_count++;
  753. pm_genpd_sync_poweroff(genpd);
  754. return 0;
  755. }
  756. /**
  757. * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  758. * @dev: Device to resume.
  759. *
  760. * Restore power to the device's PM domain, if necessary, and start the device.
  761. */
  762. static int pm_genpd_resume_noirq(struct device *dev)
  763. {
  764. struct generic_pm_domain *genpd;
  765. dev_dbg(dev, "%s()\n", __func__);
  766. genpd = dev_to_genpd(dev);
  767. if (IS_ERR(genpd))
  768. return -EINVAL;
  769. if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
  770. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  771. return 0;
  772. /*
  773. * Since all of the "noirq" callbacks are executed sequentially, it is
  774. * guaranteed that this function will never run twice in parallel for
  775. * the same PM domain, so it is not necessary to use locking here.
  776. */
  777. pm_genpd_poweron(genpd);
  778. genpd->suspended_count--;
  779. return genpd_start_dev(genpd, dev);
  780. }
  781. /**
  782. * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
  783. * @dev: Device to resume.
  784. *
  785. * Carry out an early resume of a device under the assumption that its
  786. * pm_domain field points to the domain member of an object of type
  787. * struct generic_pm_domain representing a power domain consisting of I/O
  788. * devices.
  789. */
  790. static int pm_genpd_resume_early(struct device *dev)
  791. {
  792. struct generic_pm_domain *genpd;
  793. dev_dbg(dev, "%s()\n", __func__);
  794. genpd = dev_to_genpd(dev);
  795. if (IS_ERR(genpd))
  796. return -EINVAL;
  797. return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
  798. }
  799. /**
  800. * pm_genpd_resume - Resume of device in an I/O PM domain.
  801. * @dev: Device to resume.
  802. *
  803. * Resume a device under the assumption that its pm_domain field points to the
  804. * domain member of an object of type struct generic_pm_domain representing
  805. * a power domain consisting of I/O devices.
  806. */
  807. static int pm_genpd_resume(struct device *dev)
  808. {
  809. struct generic_pm_domain *genpd;
  810. dev_dbg(dev, "%s()\n", __func__);
  811. genpd = dev_to_genpd(dev);
  812. if (IS_ERR(genpd))
  813. return -EINVAL;
  814. return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
  815. }
  816. /**
  817. * pm_genpd_freeze - Freezing a device in an I/O PM domain.
  818. * @dev: Device to freeze.
  819. *
  820. * Freeze a device under the assumption that its pm_domain field points to the
  821. * domain member of an object of type struct generic_pm_domain representing
  822. * a power domain consisting of I/O devices.
  823. */
  824. static int pm_genpd_freeze(struct device *dev)
  825. {
  826. struct generic_pm_domain *genpd;
  827. dev_dbg(dev, "%s()\n", __func__);
  828. genpd = dev_to_genpd(dev);
  829. if (IS_ERR(genpd))
  830. return -EINVAL;
  831. return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
  832. }
  833. /**
  834. * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
  835. * @dev: Device to freeze.
  836. *
  837. * Carry out a late freeze of a device under the assumption that its
  838. * pm_domain field points to the domain member of an object of type
  839. * struct generic_pm_domain representing a power domain consisting of I/O
  840. * devices.
  841. */
  842. static int pm_genpd_freeze_late(struct device *dev)
  843. {
  844. struct generic_pm_domain *genpd;
  845. dev_dbg(dev, "%s()\n", __func__);
  846. genpd = dev_to_genpd(dev);
  847. if (IS_ERR(genpd))
  848. return -EINVAL;
  849. return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
  850. }
  851. /**
  852. * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  853. * @dev: Device to freeze.
  854. *
  855. * Carry out a late freeze of a device under the assumption that its
  856. * pm_domain field points to the domain member of an object of type
  857. * struct generic_pm_domain representing a power domain consisting of I/O
  858. * devices.
  859. */
  860. static int pm_genpd_freeze_noirq(struct device *dev)
  861. {
  862. struct generic_pm_domain *genpd;
  863. dev_dbg(dev, "%s()\n", __func__);
  864. genpd = dev_to_genpd(dev);
  865. if (IS_ERR(genpd))
  866. return -EINVAL;
  867. return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
  868. 0 : genpd_stop_dev(genpd, dev);
  869. }
  870. /**
  871. * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  872. * @dev: Device to thaw.
  873. *
  874. * Start the device, unless power has been removed from the domain already
  875. * before the system transition.
  876. */
  877. static int pm_genpd_thaw_noirq(struct device *dev)
  878. {
  879. struct generic_pm_domain *genpd;
  880. dev_dbg(dev, "%s()\n", __func__);
  881. genpd = dev_to_genpd(dev);
  882. if (IS_ERR(genpd))
  883. return -EINVAL;
  884. return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
  885. 0 : genpd_start_dev(genpd, dev);
  886. }
  887. /**
  888. * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
  889. * @dev: Device to thaw.
  890. *
  891. * Carry out an early thaw of a device under the assumption that its
  892. * pm_domain field points to the domain member of an object of type
  893. * struct generic_pm_domain representing a power domain consisting of I/O
  894. * devices.
  895. */
  896. static int pm_genpd_thaw_early(struct device *dev)
  897. {
  898. struct generic_pm_domain *genpd;
  899. dev_dbg(dev, "%s()\n", __func__);
  900. genpd = dev_to_genpd(dev);
  901. if (IS_ERR(genpd))
  902. return -EINVAL;
  903. return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
  904. }
  905. /**
  906. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  907. * @dev: Device to thaw.
  908. *
  909. * Thaw a device under the assumption that its pm_domain field points to the
  910. * domain member of an object of type struct generic_pm_domain representing
  911. * a power domain consisting of I/O devices.
  912. */
  913. static int pm_genpd_thaw(struct device *dev)
  914. {
  915. struct generic_pm_domain *genpd;
  916. dev_dbg(dev, "%s()\n", __func__);
  917. genpd = dev_to_genpd(dev);
  918. if (IS_ERR(genpd))
  919. return -EINVAL;
  920. return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
  921. }
  922. /**
  923. * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  924. * @dev: Device to resume.
  925. *
  926. * Make sure the domain will be in the same power state as before the
  927. * hibernation the system is resuming from and start the device if necessary.
  928. */
  929. static int pm_genpd_restore_noirq(struct device *dev)
  930. {
  931. struct generic_pm_domain *genpd;
  932. dev_dbg(dev, "%s()\n", __func__);
  933. genpd = dev_to_genpd(dev);
  934. if (IS_ERR(genpd))
  935. return -EINVAL;
  936. /*
  937. * Since all of the "noirq" callbacks are executed sequentially, it is
  938. * guaranteed that this function will never run twice in parallel for
  939. * the same PM domain, so it is not necessary to use locking here.
  940. *
  941. * At this point suspended_count == 0 means we are being run for the
  942. * first time for the given domain in the present cycle.
  943. */
  944. if (genpd->suspended_count++ == 0) {
  945. /*
  946. * The boot kernel might put the domain into arbitrary state,
  947. * so make it appear as powered off to pm_genpd_poweron(), so
  948. * that it tries to power it on in case it was really off.
  949. */
  950. genpd->status = GPD_STATE_POWER_OFF;
  951. if (genpd->suspend_power_off) {
  952. /*
  953. * If the domain was off before the hibernation, make
  954. * sure it will be off going forward.
  955. */
  956. if (genpd->power_off)
  957. genpd->power_off(genpd);
  958. return 0;
  959. }
  960. }
  961. if (genpd->suspend_power_off)
  962. return 0;
  963. pm_genpd_poweron(genpd);
  964. return dev_gpd_data(dev)->always_on ? 0 : genpd_start_dev(genpd, dev);
  965. }
  966. /**
  967. * pm_genpd_complete - Complete power transition of a device in a power domain.
  968. * @dev: Device to complete the transition of.
  969. *
  970. * Complete a power transition of a device (during a system-wide power
  971. * transition) under the assumption that its pm_domain field points to the
  972. * domain member of an object of type struct generic_pm_domain representing
  973. * a power domain consisting of I/O devices.
  974. */
  975. static void pm_genpd_complete(struct device *dev)
  976. {
  977. struct generic_pm_domain *genpd;
  978. bool run_complete;
  979. dev_dbg(dev, "%s()\n", __func__);
  980. genpd = dev_to_genpd(dev);
  981. if (IS_ERR(genpd))
  982. return;
  983. mutex_lock(&genpd->lock);
  984. run_complete = !genpd->suspend_power_off;
  985. if (--genpd->prepared_count == 0)
  986. genpd->suspend_power_off = false;
  987. mutex_unlock(&genpd->lock);
  988. if (run_complete) {
  989. pm_generic_complete(dev);
  990. pm_runtime_set_active(dev);
  991. pm_runtime_enable(dev);
  992. pm_runtime_idle(dev);
  993. }
  994. }
  995. #else
  996. #define pm_genpd_prepare NULL
  997. #define pm_genpd_suspend NULL
  998. #define pm_genpd_suspend_late NULL
  999. #define pm_genpd_suspend_noirq NULL
  1000. #define pm_genpd_resume_early NULL
  1001. #define pm_genpd_resume_noirq NULL
  1002. #define pm_genpd_resume NULL
  1003. #define pm_genpd_freeze NULL
  1004. #define pm_genpd_freeze_late NULL
  1005. #define pm_genpd_freeze_noirq NULL
  1006. #define pm_genpd_thaw_early NULL
  1007. #define pm_genpd_thaw_noirq NULL
  1008. #define pm_genpd_thaw NULL
  1009. #define pm_genpd_restore_noirq NULL
  1010. #define pm_genpd_complete NULL
  1011. #endif /* CONFIG_PM_SLEEP */
  1012. /**
  1013. * __pm_genpd_add_device - Add a device to an I/O PM domain.
  1014. * @genpd: PM domain to add the device to.
  1015. * @dev: Device to be added.
  1016. * @td: Set of PM QoS timing parameters to attach to the device.
  1017. */
  1018. int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
  1019. struct gpd_timing_data *td)
  1020. {
  1021. struct generic_pm_domain_data *gpd_data;
  1022. struct pm_domain_data *pdd;
  1023. int ret = 0;
  1024. dev_dbg(dev, "%s()\n", __func__);
  1025. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
  1026. return -EINVAL;
  1027. gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
  1028. if (!gpd_data)
  1029. return -ENOMEM;
  1030. mutex_init(&gpd_data->lock);
  1031. gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
  1032. dev_pm_qos_add_notifier(dev, &gpd_data->nb);
  1033. genpd_acquire_lock(genpd);
  1034. if (genpd->prepared_count > 0) {
  1035. ret = -EAGAIN;
  1036. goto out;
  1037. }
  1038. list_for_each_entry(pdd, &genpd->dev_list, list_node)
  1039. if (pdd->dev == dev) {
  1040. ret = -EINVAL;
  1041. goto out;
  1042. }
  1043. genpd->device_count++;
  1044. genpd->max_off_time_changed = true;
  1045. dev_pm_get_subsys_data(dev);
  1046. mutex_lock(&gpd_data->lock);
  1047. spin_lock_irq(&dev->power.lock);
  1048. dev->pm_domain = &genpd->domain;
  1049. dev->power.subsys_data->domain_data = &gpd_data->base;
  1050. gpd_data->base.dev = dev;
  1051. list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
  1052. gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
  1053. if (td)
  1054. gpd_data->td = *td;
  1055. gpd_data->td.constraint_changed = true;
  1056. gpd_data->td.effective_constraint_ns = -1;
  1057. spin_unlock_irq(&dev->power.lock);
  1058. mutex_unlock(&gpd_data->lock);
  1059. genpd_release_lock(genpd);
  1060. return 0;
  1061. out:
  1062. genpd_release_lock(genpd);
  1063. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1064. kfree(gpd_data);
  1065. return ret;
  1066. }
  1067. /**
  1068. * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
  1069. * @genpd_node: Device tree node pointer representing a PM domain to which the
  1070. * the device is added to.
  1071. * @dev: Device to be added.
  1072. * @td: Set of PM QoS timing parameters to attach to the device.
  1073. */
  1074. int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
  1075. struct gpd_timing_data *td)
  1076. {
  1077. struct generic_pm_domain *genpd = NULL, *gpd;
  1078. dev_dbg(dev, "%s()\n", __func__);
  1079. if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
  1080. return -EINVAL;
  1081. mutex_lock(&gpd_list_lock);
  1082. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  1083. if (gpd->of_node == genpd_node) {
  1084. genpd = gpd;
  1085. break;
  1086. }
  1087. }
  1088. mutex_unlock(&gpd_list_lock);
  1089. if (!genpd)
  1090. return -EINVAL;
  1091. return __pm_genpd_add_device(genpd, dev, td);
  1092. }
  1093. /**
  1094. * pm_genpd_remove_device - Remove a device from an I/O PM domain.
  1095. * @genpd: PM domain to remove the device from.
  1096. * @dev: Device to be removed.
  1097. */
  1098. int pm_genpd_remove_device(struct generic_pm_domain *genpd,
  1099. struct device *dev)
  1100. {
  1101. struct generic_pm_domain_data *gpd_data;
  1102. struct pm_domain_data *pdd;
  1103. int ret = 0;
  1104. dev_dbg(dev, "%s()\n", __func__);
  1105. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
  1106. || IS_ERR_OR_NULL(dev->pm_domain)
  1107. || pd_to_genpd(dev->pm_domain) != genpd)
  1108. return -EINVAL;
  1109. genpd_acquire_lock(genpd);
  1110. if (genpd->prepared_count > 0) {
  1111. ret = -EAGAIN;
  1112. goto out;
  1113. }
  1114. genpd->device_count--;
  1115. genpd->max_off_time_changed = true;
  1116. spin_lock_irq(&dev->power.lock);
  1117. dev->pm_domain = NULL;
  1118. pdd = dev->power.subsys_data->domain_data;
  1119. list_del_init(&pdd->list_node);
  1120. dev->power.subsys_data->domain_data = NULL;
  1121. spin_unlock_irq(&dev->power.lock);
  1122. gpd_data = to_gpd_data(pdd);
  1123. mutex_lock(&gpd_data->lock);
  1124. pdd->dev = NULL;
  1125. mutex_unlock(&gpd_data->lock);
  1126. genpd_release_lock(genpd);
  1127. dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
  1128. kfree(gpd_data);
  1129. dev_pm_put_subsys_data(dev);
  1130. return 0;
  1131. out:
  1132. genpd_release_lock(genpd);
  1133. return ret;
  1134. }
  1135. /**
  1136. * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device.
  1137. * @dev: Device to set/unset the flag for.
  1138. * @val: The new value of the device's "always on" flag.
  1139. */
  1140. void pm_genpd_dev_always_on(struct device *dev, bool val)
  1141. {
  1142. struct pm_subsys_data *psd;
  1143. unsigned long flags;
  1144. spin_lock_irqsave(&dev->power.lock, flags);
  1145. psd = dev_to_psd(dev);
  1146. if (psd && psd->domain_data)
  1147. to_gpd_data(psd->domain_data)->always_on = val;
  1148. spin_unlock_irqrestore(&dev->power.lock, flags);
  1149. }
  1150. EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on);
  1151. /**
  1152. * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
  1153. * @dev: Device to set/unset the flag for.
  1154. * @val: The new value of the device's "need restore" flag.
  1155. */
  1156. void pm_genpd_dev_need_restore(struct device *dev, bool val)
  1157. {
  1158. struct pm_subsys_data *psd;
  1159. unsigned long flags;
  1160. spin_lock_irqsave(&dev->power.lock, flags);
  1161. psd = dev_to_psd(dev);
  1162. if (psd && psd->domain_data)
  1163. to_gpd_data(psd->domain_data)->need_restore = val;
  1164. spin_unlock_irqrestore(&dev->power.lock, flags);
  1165. }
  1166. EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
  1167. /**
  1168. * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
  1169. * @genpd: Master PM domain to add the subdomain to.
  1170. * @subdomain: Subdomain to be added.
  1171. */
  1172. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  1173. struct generic_pm_domain *subdomain)
  1174. {
  1175. struct gpd_link *link;
  1176. int ret = 0;
  1177. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1178. return -EINVAL;
  1179. start:
  1180. genpd_acquire_lock(genpd);
  1181. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1182. if (subdomain->status != GPD_STATE_POWER_OFF
  1183. && subdomain->status != GPD_STATE_ACTIVE) {
  1184. mutex_unlock(&subdomain->lock);
  1185. genpd_release_lock(genpd);
  1186. goto start;
  1187. }
  1188. if (genpd->status == GPD_STATE_POWER_OFF
  1189. && subdomain->status != GPD_STATE_POWER_OFF) {
  1190. ret = -EINVAL;
  1191. goto out;
  1192. }
  1193. list_for_each_entry(link, &genpd->master_links, master_node) {
  1194. if (link->slave == subdomain && link->master == genpd) {
  1195. ret = -EINVAL;
  1196. goto out;
  1197. }
  1198. }
  1199. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1200. if (!link) {
  1201. ret = -ENOMEM;
  1202. goto out;
  1203. }
  1204. link->master = genpd;
  1205. list_add_tail(&link->master_node, &genpd->master_links);
  1206. link->slave = subdomain;
  1207. list_add_tail(&link->slave_node, &subdomain->slave_links);
  1208. if (subdomain->status != GPD_STATE_POWER_OFF)
  1209. genpd_sd_counter_inc(genpd);
  1210. out:
  1211. mutex_unlock(&subdomain->lock);
  1212. genpd_release_lock(genpd);
  1213. return ret;
  1214. }
  1215. /**
  1216. * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
  1217. * @genpd: Master PM domain to remove the subdomain from.
  1218. * @subdomain: Subdomain to be removed.
  1219. */
  1220. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  1221. struct generic_pm_domain *subdomain)
  1222. {
  1223. struct gpd_link *link;
  1224. int ret = -EINVAL;
  1225. if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
  1226. return -EINVAL;
  1227. start:
  1228. genpd_acquire_lock(genpd);
  1229. list_for_each_entry(link, &genpd->master_links, master_node) {
  1230. if (link->slave != subdomain)
  1231. continue;
  1232. mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
  1233. if (subdomain->status != GPD_STATE_POWER_OFF
  1234. && subdomain->status != GPD_STATE_ACTIVE) {
  1235. mutex_unlock(&subdomain->lock);
  1236. genpd_release_lock(genpd);
  1237. goto start;
  1238. }
  1239. list_del(&link->master_node);
  1240. list_del(&link->slave_node);
  1241. kfree(link);
  1242. if (subdomain->status != GPD_STATE_POWER_OFF)
  1243. genpd_sd_counter_dec(genpd);
  1244. mutex_unlock(&subdomain->lock);
  1245. ret = 0;
  1246. break;
  1247. }
  1248. genpd_release_lock(genpd);
  1249. return ret;
  1250. }
  1251. /**
  1252. * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
  1253. * @dev: Device to add the callbacks to.
  1254. * @ops: Set of callbacks to add.
  1255. * @td: Timing data to add to the device along with the callbacks (optional).
  1256. */
  1257. int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
  1258. struct gpd_timing_data *td)
  1259. {
  1260. struct pm_domain_data *pdd;
  1261. int ret = 0;
  1262. if (!(dev && dev->power.subsys_data && ops))
  1263. return -EINVAL;
  1264. pm_runtime_disable(dev);
  1265. device_pm_lock();
  1266. pdd = dev->power.subsys_data->domain_data;
  1267. if (pdd) {
  1268. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  1269. gpd_data->ops = *ops;
  1270. if (td)
  1271. gpd_data->td = *td;
  1272. } else {
  1273. ret = -EINVAL;
  1274. }
  1275. device_pm_unlock();
  1276. pm_runtime_enable(dev);
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
  1280. /**
  1281. * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
  1282. * @dev: Device to remove the callbacks from.
  1283. * @clear_td: If set, clear the device's timing data too.
  1284. */
  1285. int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
  1286. {
  1287. struct pm_domain_data *pdd;
  1288. int ret = 0;
  1289. if (!(dev && dev->power.subsys_data))
  1290. return -EINVAL;
  1291. pm_runtime_disable(dev);
  1292. device_pm_lock();
  1293. pdd = dev->power.subsys_data->domain_data;
  1294. if (pdd) {
  1295. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  1296. gpd_data->ops = (struct gpd_dev_ops){ 0 };
  1297. if (clear_td)
  1298. gpd_data->td = (struct gpd_timing_data){ 0 };
  1299. } else {
  1300. ret = -EINVAL;
  1301. }
  1302. device_pm_unlock();
  1303. pm_runtime_enable(dev);
  1304. return ret;
  1305. }
  1306. EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
  1307. /* Default device callbacks for generic PM domains. */
  1308. /**
  1309. * pm_genpd_default_save_state - Default "save device state" for PM domians.
  1310. * @dev: Device to handle.
  1311. */
  1312. static int pm_genpd_default_save_state(struct device *dev)
  1313. {
  1314. int (*cb)(struct device *__dev);
  1315. struct device_driver *drv = dev->driver;
  1316. cb = dev_gpd_data(dev)->ops.save_state;
  1317. if (cb)
  1318. return cb(dev);
  1319. if (drv && drv->pm && drv->pm->runtime_suspend)
  1320. return drv->pm->runtime_suspend(dev);
  1321. return 0;
  1322. }
  1323. /**
  1324. * pm_genpd_default_restore_state - Default PM domians "restore device state".
  1325. * @dev: Device to handle.
  1326. */
  1327. static int pm_genpd_default_restore_state(struct device *dev)
  1328. {
  1329. int (*cb)(struct device *__dev);
  1330. struct device_driver *drv = dev->driver;
  1331. cb = dev_gpd_data(dev)->ops.restore_state;
  1332. if (cb)
  1333. return cb(dev);
  1334. if (drv && drv->pm && drv->pm->runtime_resume)
  1335. return drv->pm->runtime_resume(dev);
  1336. return 0;
  1337. }
  1338. #ifdef CONFIG_PM_SLEEP
  1339. /**
  1340. * pm_genpd_default_suspend - Default "device suspend" for PM domians.
  1341. * @dev: Device to handle.
  1342. */
  1343. static int pm_genpd_default_suspend(struct device *dev)
  1344. {
  1345. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
  1346. return cb ? cb(dev) : pm_generic_suspend(dev);
  1347. }
  1348. /**
  1349. * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
  1350. * @dev: Device to handle.
  1351. */
  1352. static int pm_genpd_default_suspend_late(struct device *dev)
  1353. {
  1354. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
  1355. return cb ? cb(dev) : pm_generic_suspend_late(dev);
  1356. }
  1357. /**
  1358. * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
  1359. * @dev: Device to handle.
  1360. */
  1361. static int pm_genpd_default_resume_early(struct device *dev)
  1362. {
  1363. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
  1364. return cb ? cb(dev) : pm_generic_resume_early(dev);
  1365. }
  1366. /**
  1367. * pm_genpd_default_resume - Default "device resume" for PM domians.
  1368. * @dev: Device to handle.
  1369. */
  1370. static int pm_genpd_default_resume(struct device *dev)
  1371. {
  1372. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
  1373. return cb ? cb(dev) : pm_generic_resume(dev);
  1374. }
  1375. /**
  1376. * pm_genpd_default_freeze - Default "device freeze" for PM domians.
  1377. * @dev: Device to handle.
  1378. */
  1379. static int pm_genpd_default_freeze(struct device *dev)
  1380. {
  1381. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
  1382. return cb ? cb(dev) : pm_generic_freeze(dev);
  1383. }
  1384. /**
  1385. * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
  1386. * @dev: Device to handle.
  1387. */
  1388. static int pm_genpd_default_freeze_late(struct device *dev)
  1389. {
  1390. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
  1391. return cb ? cb(dev) : pm_generic_freeze_late(dev);
  1392. }
  1393. /**
  1394. * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
  1395. * @dev: Device to handle.
  1396. */
  1397. static int pm_genpd_default_thaw_early(struct device *dev)
  1398. {
  1399. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
  1400. return cb ? cb(dev) : pm_generic_thaw_early(dev);
  1401. }
  1402. /**
  1403. * pm_genpd_default_thaw - Default "device thaw" for PM domians.
  1404. * @dev: Device to handle.
  1405. */
  1406. static int pm_genpd_default_thaw(struct device *dev)
  1407. {
  1408. int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
  1409. return cb ? cb(dev) : pm_generic_thaw(dev);
  1410. }
  1411. #else /* !CONFIG_PM_SLEEP */
  1412. #define pm_genpd_default_suspend NULL
  1413. #define pm_genpd_default_suspend_late NULL
  1414. #define pm_genpd_default_resume_early NULL
  1415. #define pm_genpd_default_resume NULL
  1416. #define pm_genpd_default_freeze NULL
  1417. #define pm_genpd_default_freeze_late NULL
  1418. #define pm_genpd_default_thaw_early NULL
  1419. #define pm_genpd_default_thaw NULL
  1420. #endif /* !CONFIG_PM_SLEEP */
  1421. /**
  1422. * pm_genpd_init - Initialize a generic I/O PM domain object.
  1423. * @genpd: PM domain object to initialize.
  1424. * @gov: PM domain governor to associate with the domain (may be NULL).
  1425. * @is_off: Initial value of the domain's power_is_off field.
  1426. */
  1427. void pm_genpd_init(struct generic_pm_domain *genpd,
  1428. struct dev_power_governor *gov, bool is_off)
  1429. {
  1430. if (IS_ERR_OR_NULL(genpd))
  1431. return;
  1432. INIT_LIST_HEAD(&genpd->master_links);
  1433. INIT_LIST_HEAD(&genpd->slave_links);
  1434. INIT_LIST_HEAD(&genpd->dev_list);
  1435. mutex_init(&genpd->lock);
  1436. genpd->gov = gov;
  1437. INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
  1438. genpd->in_progress = 0;
  1439. atomic_set(&genpd->sd_count, 0);
  1440. genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
  1441. init_waitqueue_head(&genpd->status_wait_queue);
  1442. genpd->poweroff_task = NULL;
  1443. genpd->resume_count = 0;
  1444. genpd->device_count = 0;
  1445. genpd->max_off_time_ns = -1;
  1446. genpd->max_off_time_changed = true;
  1447. genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
  1448. genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
  1449. genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
  1450. genpd->domain.ops.prepare = pm_genpd_prepare;
  1451. genpd->domain.ops.suspend = pm_genpd_suspend;
  1452. genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
  1453. genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
  1454. genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
  1455. genpd->domain.ops.resume_early = pm_genpd_resume_early;
  1456. genpd->domain.ops.resume = pm_genpd_resume;
  1457. genpd->domain.ops.freeze = pm_genpd_freeze;
  1458. genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
  1459. genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
  1460. genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
  1461. genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
  1462. genpd->domain.ops.thaw = pm_genpd_thaw;
  1463. genpd->domain.ops.poweroff = pm_genpd_suspend;
  1464. genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
  1465. genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
  1466. genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
  1467. genpd->domain.ops.restore_early = pm_genpd_resume_early;
  1468. genpd->domain.ops.restore = pm_genpd_resume;
  1469. genpd->domain.ops.complete = pm_genpd_complete;
  1470. genpd->dev_ops.save_state = pm_genpd_default_save_state;
  1471. genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
  1472. genpd->dev_ops.suspend = pm_genpd_default_suspend;
  1473. genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
  1474. genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
  1475. genpd->dev_ops.resume = pm_genpd_default_resume;
  1476. genpd->dev_ops.freeze = pm_genpd_default_freeze;
  1477. genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
  1478. genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
  1479. genpd->dev_ops.thaw = pm_genpd_default_thaw;
  1480. mutex_lock(&gpd_list_lock);
  1481. list_add(&genpd->gpd_list_node, &gpd_list);
  1482. mutex_unlock(&gpd_list_lock);
  1483. }