domain.c 61 KB

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