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/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/suspend.h>
  18. #include <linux/export.h>
  19. #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
  20. ({ \
  21. type (*__routine)(struct device *__d); \
  22. type __ret = (type)0; \
  23. \
  24. __routine = genpd->dev_ops.callback; \
  25. if (__routine) { \
  26. __ret = __routine(dev); \
  27. } \
  28. __ret; \
  29. })
  30. #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
  31. ({ \
  32. ktime_t __start = ktime_get(); \
  33. type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
  34. s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
  35. struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
  36. if (!__retval && __elapsed > __td->field) { \
  37. __td->field = __elapsed; \
  38. dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
  39. __elapsed); \
  40. genpd->max_off_time_changed = true; \
  41. __td->constraint_changed = true; \
  42. } \
  43. __retval; \
  44. })
  45. static LIST_HEAD(gpd_list);
  46. static DEFINE_MUTEX(gpd_list_lock);
  47. static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
  48. {
  49. struct generic_pm_domain *genpd = NULL, *gpd;
  50. if (IS_ERR_OR_NULL(domain_name))
  51. return NULL;
  52. mutex_lock(&gpd_list_lock);
  53. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  54. if (!strcmp(gpd->name, domain_name)) {
  55. genpd = gpd;
  56. break;
  57. }
  58. }
  59. mutex_unlock(&gpd_list_lock);
  60. return genpd;
  61. }
  62. struct generic_pm_domain *dev_to_genpd(struct device *dev)
  63. {
  64. if (IS_ERR_OR_NULL(dev->pm_domain))
  65. return ERR_PTR(-EINVAL);
  66. return pd_to_genpd(dev->pm_domain);
  67. }
  68. static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
  69. {
  70. return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
  71. stop_latency_ns, "stop");
  72. }
  73. static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
  74. {
  75. return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
  76. start_latency_ns, "start");
  77. }
  78. static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  79. {
  80. bool ret = false;
  81. if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
  82. ret = !!atomic_dec_and_test(&genpd->sd_count);
  83. return ret;
  84. }
  85. static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
  86. {
  87. atomic_inc(&genpd->sd_count);
  88. smp_mb__after_atomic();
  89. }
  90. static void genpd_acquire_lock(struct generic_pm_domain *genpd)
  91. {
  92. DEFINE_WAIT(wait);
  93. mutex_lock(&genpd->lock);
  94. /*
  95. * Wait for the domain to transition into either the active,
  96. * or the power off state.
  97. */
  98. for (;;) {
  99. prepare_to_wait(&genpd->status_wait_queue, &wait,
  100. TASK_UNINTERRUPTIBLE);
  101. if (genpd->status == GPD_STATE_ACTIVE
  102. || genpd->status == GPD_STATE_POWER_OFF)
  103. break;
  104. mutex_unlock(&genpd->lock);
  105. schedule();
  106. mutex_lock(&genpd->lock);
  107. }
  108. finish_wait(&genpd->status_wait_queue, &wait);
  109. }
  110. static void genpd_release_lock(struct generic_pm_domain *genpd)
  111. {
  112. mutex_unlock(&genpd->lock);
  113. }
  114. static void genpd_set_active(struct generic_pm_domain *genpd)
  115. {
  116. if (genpd->resume_count == 0)
  117. genpd->status = GPD_STATE_ACTIVE;
  118. }
  119. static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
  120. {
  121. s64 usecs64;
  122. if (!genpd->cpuidle_data)
  123. return;
  124. usecs64 = genpd->power_on_latency_ns;
  125. do_div(usecs64, NSEC_PER_USEC);
  126. usecs64 += genpd->cpuidle_data->saved_exit_latency;
  127. genpd->cpuidle_data->idle_state->exit_latency = usecs64;
  128. }
  129. /**
  130. * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
  131. * @genpd: PM domain to power up.
  132. *
  133. * Restore power to @genpd and all of its masters so that it is possible to
  134. * resume a device belonging to it.
  135. */
  136. static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
  137. __releases(&genpd->lock) __acquires(&genpd->lock)
  138. {
  139. struct gpd_link *link;
  140. DEFINE_WAIT(wait);
  141. int ret = 0;
  142. /* If the domain's master is being waited for, we have to wait too. */
  143. for (;;) {
  144. prepare_to_wait(&genpd->status_wait_queue, &wait,
  145. TASK_UNINTERRUPTIBLE);
  146. if (genpd->status != GPD_STATE_WAIT_MASTER)
  147. break;
  148. mutex_unlock(&genpd->lock);
  149. schedule();
  150. mutex_lock(&genpd->lock);
  151. }
  152. finish_wait(&genpd->status_wait_queue, &wait);
  153. if (genpd->status == GPD_STATE_ACTIVE
  154. || (genpd->prepared_count > 0 && genpd->suspend_power_off))
  155. return 0;
  156. if (genpd->status != GPD_STATE_POWER_OFF) {
  157. genpd_set_active(genpd);
  158. return 0;
  159. }
  160. if (genpd->cpuidle_data) {
  161. cpuidle_pause_and_lock();
  162. genpd->cpuidle_data->idle_state->disabled = true;
  163. cpuidle_resume_and_unlock();
  164. goto out;
  165. }
  166. /*
  167. * The list is guaranteed not to change while the loop below is being
  168. * executed, unless one of the masters' .power_on() callbacks fiddles
  169. * with it.
  170. */
  171. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  172. genpd_sd_counter_inc(link->master);
  173. genpd->status = GPD_STATE_WAIT_MASTER;
  174. mutex_unlock(&genpd->lock);
  175. ret = pm_genpd_poweron(link->master);
  176. mutex_lock(&genpd->lock);
  177. /*
  178. * The "wait for parent" status is guaranteed not to change
  179. * while the master is powering on.
  180. */
  181. genpd->status = GPD_STATE_POWER_OFF;
  182. wake_up_all(&genpd->status_wait_queue);
  183. if (ret) {
  184. genpd_sd_counter_dec(link->master);
  185. goto err;
  186. }
  187. }
  188. if (genpd->power_on) {
  189. ktime_t time_start = ktime_get();
  190. s64 elapsed_ns;
  191. ret = genpd->power_on(genpd);
  192. if (ret)
  193. goto err;
  194. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  195. if (elapsed_ns > genpd->power_on_latency_ns) {
  196. genpd->power_on_latency_ns = elapsed_ns;
  197. genpd->max_off_time_changed = true;
  198. genpd_recalc_cpu_exit_latency(genpd);
  199. if (genpd->name)
  200. pr_warning("%s: Power-on latency exceeded, "
  201. "new value %lld ns\n", genpd->name,
  202. elapsed_ns);
  203. }
  204. }
  205. out:
  206. genpd_set_active(genpd);
  207. return 0;
  208. err:
  209. list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
  210. genpd_sd_counter_dec(link->master);
  211. return ret;
  212. }
  213. /**
  214. * pm_genpd_poweron - Restore power to a given PM domain and its masters.
  215. * @genpd: PM domain to power up.
  216. */
  217. int pm_genpd_poweron(struct generic_pm_domain *genpd)
  218. {
  219. int ret;
  220. mutex_lock(&genpd->lock);
  221. ret = __pm_genpd_poweron(genpd);
  222. mutex_unlock(&genpd->lock);
  223. return ret;
  224. }
  225. /**
  226. * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
  227. * @domain_name: Name of the PM domain to power up.
  228. */
  229. int pm_genpd_name_poweron(const char *domain_name)
  230. {
  231. struct generic_pm_domain *genpd;
  232. genpd = pm_genpd_lookup_name(domain_name);
  233. return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
  234. }
  235. #ifdef CONFIG_PM_RUNTIME
  236. static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
  237. struct device *dev)
  238. {
  239. return GENPD_DEV_CALLBACK(genpd, int, start, dev);
  240. }
  241. static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
  242. {
  243. return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
  244. save_state_latency_ns, "state save");
  245. }
  246. static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
  247. {
  248. return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
  249. restore_state_latency_ns,
  250. "state restore");
  251. }
  252. static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  253. unsigned long val, void *ptr)
  254. {
  255. struct generic_pm_domain_data *gpd_data;
  256. struct device *dev;
  257. gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
  258. mutex_lock(&gpd_data->lock);
  259. dev = gpd_data->base.dev;
  260. if (!dev) {
  261. mutex_unlock(&gpd_data->lock);
  262. return NOTIFY_DONE;
  263. }
  264. mutex_unlock(&gpd_data->lock);
  265. for (;;) {
  266. struct generic_pm_domain *genpd;
  267. struct pm_domain_data *pdd;
  268. spin_lock_irq(&dev->power.lock);
  269. pdd = dev->power.subsys_data ?
  270. dev->power.subsys_data->domain_data : NULL;
  271. if (pdd && pdd->dev) {
  272. to_gpd_data(pdd)->td.constraint_changed = true;
  273. genpd = dev_to_genpd(dev);
  274. } else {
  275. genpd = ERR_PTR(-ENODATA);
  276. }
  277. spin_unlock_irq(&dev->power.lock);
  278. if (!IS_ERR(genpd)) {
  279. mutex_lock(&genpd->lock);
  280. genpd->max_off_time_changed = true;
  281. mutex_unlock(&genpd->lock);
  282. }
  283. dev = dev->parent;
  284. if (!dev || dev->power.ignore_children)
  285. break;
  286. }
  287. return NOTIFY_DONE;
  288. }
  289. /**
  290. * __pm_genpd_save_device - Save the pre-suspend state of a device.
  291. * @pdd: Domain data of the device to save the state of.
  292. * @genpd: PM domain the device belongs to.
  293. */
  294. static int __pm_genpd_save_device(struct pm_domain_data *pdd,
  295. struct generic_pm_domain *genpd)
  296. __releases(&genpd->lock) __acquires(&genpd->lock)
  297. {
  298. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  299. struct device *dev = pdd->dev;
  300. int ret = 0;
  301. if (gpd_data->need_restore > 0)
  302. return 0;
  303. /*
  304. * If the value of the need_restore flag is still unknown at this point,
  305. * we trust that pm_genpd_poweroff() has verified that the device is
  306. * already runtime PM suspended.
  307. */
  308. if (gpd_data->need_restore < 0) {
  309. gpd_data->need_restore = 1;
  310. return 0;
  311. }
  312. mutex_unlock(&genpd->lock);
  313. genpd_start_dev(genpd, dev);
  314. ret = genpd_save_dev(genpd, dev);
  315. genpd_stop_dev(genpd, dev);
  316. mutex_lock(&genpd->lock);
  317. if (!ret)
  318. gpd_data->need_restore = 1;
  319. return ret;
  320. }
  321. /**
  322. * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
  323. * @pdd: Domain data of the device to restore the state of.
  324. * @genpd: PM domain the device belongs to.
  325. */
  326. static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
  327. struct generic_pm_domain *genpd)
  328. __releases(&genpd->lock) __acquires(&genpd->lock)
  329. {
  330. struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
  331. struct device *dev = pdd->dev;
  332. int need_restore = gpd_data->need_restore;
  333. gpd_data->need_restore = 0;
  334. mutex_unlock(&genpd->lock);
  335. genpd_start_dev(genpd, dev);
  336. /*
  337. * Call genpd_restore_dev() for recently added devices too (need_restore
  338. * is negative then).
  339. */
  340. if (need_restore)
  341. genpd_restore_dev(genpd, dev);
  342. mutex_lock(&genpd->lock);
  343. }
  344. /**
  345. * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
  346. * @genpd: PM domain to check.
  347. *
  348. * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
  349. * a "power off" operation, which means that a "power on" has occured in the
  350. * meantime, or if its resume_count field is different from zero, which means
  351. * that one of its devices has been resumed in the meantime.
  352. */
  353. static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
  354. {
  355. return genpd->status == GPD_STATE_WAIT_MASTER
  356. || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
  357. }
  358. /**
  359. * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
  360. * @genpd: PM domait to power off.
  361. *
  362. * Queue up the execution of pm_genpd_poweroff() unless it's already been done
  363. * before.
  364. */
  365. static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
  366. {
  367. queue_work(pm_wq, &genpd->power_off_work);
  368. }
  369. /**
  370. * pm_genpd_poweroff - Remove power from a given PM domain.
  371. * @genpd: PM domain to power down.
  372. *
  373. * If all of the @genpd's devices have been suspended and all of its subdomains
  374. * have been powered down, run the runtime suspend callbacks provided by all of
  375. * the @genpd's devices' drivers and remove power from @genpd.
  376. */
  377. static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
  378. __releases(&genpd->lock) __acquires(&genpd->lock)
  379. {
  380. struct pm_domain_data *pdd;
  381. struct gpd_link *link;
  382. unsigned int not_suspended;
  383. int ret = 0;
  384. start:
  385. /*
  386. * Do not try to power off the domain in the following situations:
  387. * (1) The domain is already in the "power off" state.
  388. * (2) The domain is waiting for its master to power up.
  389. * (3) One of the domain's devices is being resumed right now.
  390. * (4) System suspend is in progress.
  391. */
  392. if (genpd->status == GPD_STATE_POWER_OFF
  393. || genpd->status == GPD_STATE_WAIT_MASTER
  394. || genpd->resume_count > 0 || genpd->prepared_count > 0)
  395. return 0;
  396. if (atomic_read(&genpd->sd_count) > 0)
  397. return -EBUSY;
  398. not_suspended = 0;
  399. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  400. enum pm_qos_flags_status stat;
  401. stat = dev_pm_qos_flags(pdd->dev,
  402. PM_QOS_FLAG_NO_POWER_OFF
  403. | PM_QOS_FLAG_REMOTE_WAKEUP);
  404. if (stat > PM_QOS_FLAGS_NONE)
  405. return -EBUSY;
  406. if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
  407. || pdd->dev->power.irq_safe))
  408. not_suspended++;
  409. }
  410. if (not_suspended > genpd->in_progress)
  411. return -EBUSY;
  412. if (genpd->poweroff_task) {
  413. /*
  414. * Another instance of pm_genpd_poweroff() is executing
  415. * callbacks, so tell it to start over and return.
  416. */
  417. genpd->status = GPD_STATE_REPEAT;
  418. return 0;
  419. }
  420. if (genpd->gov && genpd->gov->power_down_ok) {
  421. if (!genpd->gov->power_down_ok(&genpd->domain))
  422. return -EAGAIN;
  423. }
  424. genpd->status = GPD_STATE_BUSY;
  425. genpd->poweroff_task = current;
  426. list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
  427. ret = atomic_read(&genpd->sd_count) == 0 ?
  428. __pm_genpd_save_device(pdd, genpd) : -EBUSY;
  429. if (genpd_abort_poweroff(genpd))
  430. goto out;
  431. if (ret) {
  432. genpd_set_active(genpd);
  433. goto out;
  434. }
  435. if (genpd->status == GPD_STATE_REPEAT) {
  436. genpd->poweroff_task = NULL;
  437. goto start;
  438. }
  439. }
  440. if (genpd->cpuidle_data) {
  441. /*
  442. * If cpuidle_data is set, cpuidle should turn the domain off
  443. * when the CPU in it is idle. In that case we don't decrement
  444. * the subdomain counts of the master domains, so that power is
  445. * not removed from the current domain prematurely as a result
  446. * of cutting off the masters' power.
  447. */
  448. genpd->status = GPD_STATE_POWER_OFF;
  449. cpuidle_pause_and_lock();
  450. genpd->cpuidle_data->idle_state->disabled = false;
  451. cpuidle_resume_and_unlock();
  452. goto out;
  453. }
  454. if (genpd->power_off) {
  455. ktime_t time_start;
  456. s64 elapsed_ns;
  457. if (atomic_read(&genpd->sd_count) > 0) {
  458. ret = -EBUSY;
  459. goto out;
  460. }
  461. time_start = ktime_get();
  462. /*
  463. * If sd_count > 0 at this point, one of the subdomains hasn't
  464. * managed to call pm_genpd_poweron() for the master yet after
  465. * incrementing it. In that case pm_genpd_poweron() will wait
  466. * for us to drop the lock, so we can call .power_off() and let
  467. * the pm_genpd_poweron() restore power for us (this shouldn't
  468. * happen very often).
  469. */
  470. ret = genpd->power_off(genpd);
  471. if (ret == -EBUSY) {
  472. genpd_set_active(genpd);
  473. goto out;
  474. }
  475. elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
  476. if (elapsed_ns > genpd->power_off_latency_ns) {
  477. genpd->power_off_latency_ns = elapsed_ns;
  478. genpd->max_off_time_changed = true;
  479. if (genpd->name)
  480. pr_warning("%s: Power-off latency exceeded, "
  481. "new value %lld ns\n", genpd->name,
  482. elapsed_ns);
  483. }
  484. }
  485. genpd->status = GPD_STATE_POWER_OFF;
  486. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  487. genpd_sd_counter_dec(link->master);
  488. genpd_queue_power_off_work(link->master);
  489. }
  490. out:
  491. genpd->poweroff_task = NULL;
  492. wake_up_all(&genpd->status_wait_queue);
  493. return ret;
  494. }
  495. /**
  496. * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
  497. * @work: Work structure used for scheduling the execution of this function.
  498. */
  499. static void genpd_power_off_work_fn(struct work_struct *work)
  500. {
  501. struct generic_pm_domain *genpd;
  502. genpd = container_of(work, struct generic_pm_domain, power_off_work);
  503. genpd_acquire_lock(genpd);
  504. pm_genpd_poweroff(genpd);
  505. genpd_release_lock(genpd);
  506. }
  507. /**
  508. * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
  509. * @dev: Device to suspend.
  510. *
  511. * Carry out a runtime suspend of a device under the assumption that its
  512. * pm_domain field points to the domain member of an object of type
  513. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  514. */
  515. static int pm_genpd_runtime_suspend(struct device *dev)
  516. {
  517. struct generic_pm_domain *genpd;
  518. struct generic_pm_domain_data *gpd_data;
  519. bool (*stop_ok)(struct device *__dev);
  520. int ret;
  521. dev_dbg(dev, "%s()\n", __func__);
  522. genpd = dev_to_genpd(dev);
  523. if (IS_ERR(genpd))
  524. return -EINVAL;
  525. stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
  526. if (stop_ok && !stop_ok(dev))
  527. return -EBUSY;
  528. ret = genpd_stop_dev(genpd, dev);
  529. if (ret)
  530. return ret;
  531. /*
  532. * If power.irq_safe is set, this routine will be run with interrupts
  533. * off, so it can't use mutexes.
  534. */
  535. if (dev->power.irq_safe)
  536. return 0;
  537. mutex_lock(&genpd->lock);
  538. /*
  539. * If we have an unknown state of the need_restore flag, it means none
  540. * of the runtime PM callbacks has been invoked yet. Let's update the
  541. * flag to reflect that the current state is active.
  542. */
  543. gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
  544. if (gpd_data->need_restore < 0)
  545. gpd_data->need_restore = 0;
  546. genpd->in_progress++;
  547. pm_genpd_poweroff(genpd);
  548. genpd->in_progress--;
  549. mutex_unlock(&genpd->lock);
  550. return 0;
  551. }
  552. /**
  553. * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
  554. * @dev: Device to resume.
  555. *
  556. * Carry out a runtime resume of a device under the assumption that its
  557. * pm_domain field points to the domain member of an object of type
  558. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  559. */
  560. static int pm_genpd_runtime_resume(struct device *dev)
  561. {
  562. struct generic_pm_domain *genpd;
  563. DEFINE_WAIT(wait);
  564. int ret;
  565. dev_dbg(dev, "%s()\n", __func__);
  566. genpd = dev_to_genpd(dev);
  567. if (IS_ERR(genpd))
  568. return -EINVAL;
  569. /* If power.irq_safe, the PM domain is never powered off. */
  570. if (dev->power.irq_safe)
  571. return genpd_start_dev_no_timing(genpd, dev);
  572. mutex_lock(&genpd->lock);
  573. ret = __pm_genpd_poweron(genpd);
  574. if (ret) {
  575. mutex_unlock(&genpd->lock);
  576. return ret;
  577. }
  578. genpd->status = GPD_STATE_BUSY;
  579. genpd->resume_count++;
  580. for (;;) {
  581. prepare_to_wait(&genpd->status_wait_queue, &wait,
  582. TASK_UNINTERRUPTIBLE);
  583. /*
  584. * If current is the powering off task, we have been called
  585. * reentrantly from one of the device callbacks, so we should
  586. * not wait.
  587. */
  588. if (!genpd->poweroff_task || genpd->poweroff_task == current)
  589. break;
  590. mutex_unlock(&genpd->lock);
  591. schedule();
  592. mutex_lock(&genpd->lock);
  593. }
  594. finish_wait(&genpd->status_wait_queue, &wait);
  595. __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
  596. genpd->resume_count--;
  597. genpd_set_active(genpd);
  598. wake_up_all(&genpd->status_wait_queue);
  599. mutex_unlock(&genpd->lock);
  600. return 0;
  601. }
  602. static bool pd_ignore_unused;
  603. static int __init pd_ignore_unused_setup(char *__unused)
  604. {
  605. pd_ignore_unused = true;
  606. return 1;
  607. }
  608. __setup("pd_ignore_unused", pd_ignore_unused_setup);
  609. /**
  610. * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
  611. */
  612. void pm_genpd_poweroff_unused(void)
  613. {
  614. struct generic_pm_domain *genpd;
  615. if (pd_ignore_unused) {
  616. pr_warn("genpd: Not disabling unused power domains\n");
  617. return;
  618. }
  619. mutex_lock(&gpd_list_lock);
  620. list_for_each_entry(genpd, &gpd_list, gpd_list_node)
  621. genpd_queue_power_off_work(genpd);
  622. mutex_unlock(&gpd_list_lock);
  623. }
  624. static int __init genpd_poweroff_unused(void)
  625. {
  626. pm_genpd_poweroff_unused();
  627. return 0;
  628. }
  629. late_initcall(genpd_poweroff_unused);
  630. #else
  631. static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
  632. unsigned long val, void *ptr)
  633. {
  634. return NOTIFY_DONE;
  635. }
  636. static inline void
  637. genpd_queue_power_off_work(struct generic_pm_domain *genpd) {}
  638. static inline void genpd_power_off_work_fn(struct work_struct *work) {}
  639. #define pm_genpd_runtime_suspend NULL
  640. #define pm_genpd_runtime_resume NULL
  641. #endif /* CONFIG_PM_RUNTIME */
  642. #ifdef CONFIG_PM_SLEEP
  643. /**
  644. * pm_genpd_present - Check if the given PM domain has been initialized.
  645. * @genpd: PM domain to check.
  646. */
  647. static bool pm_genpd_present(struct generic_pm_domain *genpd)
  648. {
  649. struct generic_pm_domain *gpd;
  650. if (IS_ERR_OR_NULL(genpd))
  651. return false;
  652. list_for_each_entry(gpd, &gpd_list, gpd_list_node)
  653. if (gpd == genpd)
  654. return true;
  655. return false;
  656. }
  657. static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
  658. struct device *dev)
  659. {
  660. return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
  661. }
  662. /**
  663. * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
  664. * @genpd: PM domain to power off, if possible.
  665. *
  666. * Check if the given PM domain can be powered off (during system suspend or
  667. * hibernation) and do that if so. Also, in that case propagate to its masters.
  668. *
  669. * This function is only called in "noirq" and "syscore" stages of system power
  670. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  671. * executed sequentially, so it is guaranteed that it will never run twice in
  672. * parallel).
  673. */
  674. static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
  675. {
  676. struct gpd_link *link;
  677. if (genpd->status == GPD_STATE_POWER_OFF)
  678. return;
  679. if (genpd->suspended_count != genpd->device_count
  680. || atomic_read(&genpd->sd_count) > 0)
  681. return;
  682. if (genpd->power_off)
  683. genpd->power_off(genpd);
  684. genpd->status = GPD_STATE_POWER_OFF;
  685. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  686. genpd_sd_counter_dec(link->master);
  687. pm_genpd_sync_poweroff(link->master);
  688. }
  689. }
  690. /**
  691. * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
  692. * @genpd: PM domain to power on.
  693. *
  694. * This function is only called in "noirq" and "syscore" stages of system power
  695. * transitions, so it need not acquire locks (all of the "noirq" callbacks are
  696. * executed sequentially, so it is guaranteed that it will never run twice in
  697. * parallel).
  698. */
  699. static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
  700. {
  701. struct gpd_link *link;
  702. if (genpd->status != GPD_STATE_POWER_OFF)
  703. return;
  704. list_for_each_entry(link, &genpd->slave_links, slave_node) {
  705. pm_genpd_sync_poweron(link->master);
  706. genpd_sd_counter_inc(link->master);
  707. }
  708. if (genpd->power_on)
  709. genpd->power_on(genpd);
  710. genpd->status = GPD_STATE_ACTIVE;
  711. }
  712. /**
  713. * resume_needed - Check whether to resume a device before system suspend.
  714. * @dev: Device to check.
  715. * @genpd: PM domain the device belongs to.
  716. *
  717. * There are two cases in which a device that can wake up the system from sleep
  718. * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
  719. * to wake up the system and it has to remain active for this purpose while the
  720. * system is in the sleep state and (2) if the device is not enabled to wake up
  721. * the system from sleep states and it generally doesn't generate wakeup signals
  722. * by itself (those signals are generated on its behalf by other parts of the
  723. * system). In the latter case it may be necessary to reconfigure the device's
  724. * wakeup settings during system suspend, because it may have been set up to
  725. * signal remote wakeup from the system's working state as needed by runtime PM.
  726. * Return 'true' in either of the above cases.
  727. */
  728. static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
  729. {
  730. bool active_wakeup;
  731. if (!device_can_wakeup(dev))
  732. return false;
  733. active_wakeup = genpd_dev_active_wakeup(genpd, dev);
  734. return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
  735. }
  736. /**
  737. * pm_genpd_prepare - Start power transition of a device in a PM domain.
  738. * @dev: Device to start the transition of.
  739. *
  740. * Start a power transition of a device (during a system-wide power transition)
  741. * under the assumption that its pm_domain field points to the domain member of
  742. * an object of type struct generic_pm_domain representing a PM domain
  743. * consisting of I/O devices.
  744. */
  745. static int pm_genpd_prepare(struct device *dev)
  746. {
  747. struct generic_pm_domain *genpd;
  748. int ret;
  749. dev_dbg(dev, "%s()\n", __func__);
  750. genpd = dev_to_genpd(dev);
  751. if (IS_ERR(genpd))
  752. return -EINVAL;
  753. /*
  754. * If a wakeup request is pending for the device, it should be woken up
  755. * at this point and a system wakeup event should be reported if it's
  756. * set up to wake up the system from sleep states.
  757. */
  758. pm_runtime_get_noresume(dev);
  759. if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
  760. pm_wakeup_event(dev, 0);
  761. if (pm_wakeup_pending()) {
  762. pm_runtime_put(dev);
  763. return -EBUSY;
  764. }
  765. if (resume_needed(dev, genpd))
  766. pm_runtime_resume(dev);
  767. genpd_acquire_lock(genpd);
  768. if (genpd->prepared_count++ == 0) {
  769. genpd->suspended_count = 0;
  770. genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
  771. }
  772. genpd_release_lock(genpd);
  773. if (genpd->suspend_power_off) {
  774. pm_runtime_put_noidle(dev);
  775. return 0;
  776. }
  777. /*
  778. * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
  779. * so pm_genpd_poweron() will return immediately, but if the device
  780. * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
  781. * to make it operational.
  782. */
  783. pm_runtime_resume(dev);
  784. __pm_runtime_disable(dev, false);
  785. ret = pm_generic_prepare(dev);
  786. if (ret) {
  787. mutex_lock(&genpd->lock);
  788. if (--genpd->prepared_count == 0)
  789. genpd->suspend_power_off = false;
  790. mutex_unlock(&genpd->lock);
  791. pm_runtime_enable(dev);
  792. }
  793. pm_runtime_put(dev);
  794. return ret;
  795. }
  796. /**
  797. * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
  798. * @dev: Device to suspend.
  799. *
  800. * Suspend a device under the assumption that its pm_domain field points to the
  801. * domain member of an object of type struct generic_pm_domain representing
  802. * a PM domain consisting of I/O devices.
  803. */
  804. static int pm_genpd_suspend(struct device *dev)
  805. {
  806. struct generic_pm_domain *genpd;
  807. dev_dbg(dev, "%s()\n", __func__);
  808. genpd = dev_to_genpd(dev);
  809. if (IS_ERR(genpd))
  810. return -EINVAL;
  811. return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
  812. }
  813. /**
  814. * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
  815. * @dev: Device to suspend.
  816. *
  817. * Carry out a late suspend of a device under the assumption that its
  818. * pm_domain field points to the domain member of an object of type
  819. * struct generic_pm_domain representing a PM domain consisting of I/O devices.
  820. */
  821. static int pm_genpd_suspend_late(struct device *dev)
  822. {
  823. struct generic_pm_domain *genpd;
  824. dev_dbg(dev, "%s()\n", __func__);
  825. genpd = dev_to_genpd(dev);
  826. if (IS_ERR(genpd))
  827. return -EINVAL;
  828. return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
  829. }
  830. /**
  831. * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
  832. * @dev: Device to suspend.
  833. *
  834. * Stop the device and remove power from the domain if all devices in it have
  835. * been stopped.
  836. */
  837. static int pm_genpd_suspend_noirq(struct device *dev)
  838. {
  839. struct generic_pm_domain *genpd;
  840. dev_dbg(dev, "%s()\n", __func__);
  841. genpd = dev_to_genpd(dev);
  842. if (IS_ERR(genpd))
  843. return -EINVAL;
  844. if (genpd->suspend_power_off
  845. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  846. return 0;
  847. genpd_stop_dev(genpd, dev);
  848. /*
  849. * Since all of the "noirq" callbacks are executed sequentially, it is
  850. * guaranteed that this function will never run twice in parallel for
  851. * the same PM domain, so it is not necessary to use locking here.
  852. */
  853. genpd->suspended_count++;
  854. pm_genpd_sync_poweroff(genpd);
  855. return 0;
  856. }
  857. /**
  858. * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
  859. * @dev: Device to resume.
  860. *
  861. * Restore power to the device's PM domain, if necessary, and start the device.
  862. */
  863. static int pm_genpd_resume_noirq(struct device *dev)
  864. {
  865. struct generic_pm_domain *genpd;
  866. dev_dbg(dev, "%s()\n", __func__);
  867. genpd = dev_to_genpd(dev);
  868. if (IS_ERR(genpd))
  869. return -EINVAL;
  870. if (genpd->suspend_power_off
  871. || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
  872. return 0;
  873. /*
  874. * Since all of the "noirq" callbacks are executed sequentially, it is
  875. * guaranteed that this function will never run twice in parallel for
  876. * the same PM domain, so it is not necessary to use locking here.
  877. */
  878. pm_genpd_sync_poweron(genpd);
  879. genpd->suspended_count--;
  880. return genpd_start_dev(genpd, dev);
  881. }
  882. /**
  883. * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
  884. * @dev: Device to resume.
  885. *
  886. * Carry out an early resume of a device under the assumption that its
  887. * pm_domain field points to the domain member of an object of type
  888. * struct generic_pm_domain representing a power domain consisting of I/O
  889. * devices.
  890. */
  891. static int pm_genpd_resume_early(struct device *dev)
  892. {
  893. struct generic_pm_domain *genpd;
  894. dev_dbg(dev, "%s()\n", __func__);
  895. genpd = dev_to_genpd(dev);
  896. if (IS_ERR(genpd))
  897. return -EINVAL;
  898. return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
  899. }
  900. /**
  901. * pm_genpd_resume - Resume of device in an I/O PM domain.
  902. * @dev: Device to resume.
  903. *
  904. * Resume a device under the assumption that its pm_domain field points to the
  905. * domain member of an object of type struct generic_pm_domain representing
  906. * a power domain consisting of I/O devices.
  907. */
  908. static int pm_genpd_resume(struct device *dev)
  909. {
  910. struct generic_pm_domain *genpd;
  911. dev_dbg(dev, "%s()\n", __func__);
  912. genpd = dev_to_genpd(dev);
  913. if (IS_ERR(genpd))
  914. return -EINVAL;
  915. return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
  916. }
  917. /**
  918. * pm_genpd_freeze - Freezing a device in an I/O PM domain.
  919. * @dev: Device to freeze.
  920. *
  921. * Freeze a device under the assumption that its pm_domain field points to the
  922. * domain member of an object of type struct generic_pm_domain representing
  923. * a power domain consisting of I/O devices.
  924. */
  925. static int pm_genpd_freeze(struct device *dev)
  926. {
  927. struct generic_pm_domain *genpd;
  928. dev_dbg(dev, "%s()\n", __func__);
  929. genpd = dev_to_genpd(dev);
  930. if (IS_ERR(genpd))
  931. return -EINVAL;
  932. return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
  933. }
  934. /**
  935. * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
  936. * @dev: Device to freeze.
  937. *
  938. * Carry out a late freeze of a device under the assumption that its
  939. * pm_domain field points to the domain member of an object of type
  940. * struct generic_pm_domain representing a power domain consisting of I/O
  941. * devices.
  942. */
  943. static int pm_genpd_freeze_late(struct device *dev)
  944. {
  945. struct generic_pm_domain *genpd;
  946. dev_dbg(dev, "%s()\n", __func__);
  947. genpd = dev_to_genpd(dev);
  948. if (IS_ERR(genpd))
  949. return -EINVAL;
  950. return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
  951. }
  952. /**
  953. * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
  954. * @dev: Device to freeze.
  955. *
  956. * Carry out a late freeze of a device under the assumption that its
  957. * pm_domain field points to the domain member of an object of type
  958. * struct generic_pm_domain representing a power domain consisting of I/O
  959. * devices.
  960. */
  961. static int pm_genpd_freeze_noirq(struct device *dev)
  962. {
  963. struct generic_pm_domain *genpd;
  964. dev_dbg(dev, "%s()\n", __func__);
  965. genpd = dev_to_genpd(dev);
  966. if (IS_ERR(genpd))
  967. return -EINVAL;
  968. return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
  969. }
  970. /**
  971. * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
  972. * @dev: Device to thaw.
  973. *
  974. * Start the device, unless power has been removed from the domain already
  975. * before the system transition.
  976. */
  977. static int pm_genpd_thaw_noirq(struct device *dev)
  978. {
  979. struct generic_pm_domain *genpd;
  980. dev_dbg(dev, "%s()\n", __func__);
  981. genpd = dev_to_genpd(dev);
  982. if (IS_ERR(genpd))
  983. return -EINVAL;
  984. return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
  985. }
  986. /**
  987. * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
  988. * @dev: Device to thaw.
  989. *
  990. * Carry out an early thaw of a device under the assumption that its
  991. * pm_domain field points to the domain member of an object of type
  992. * struct generic_pm_domain representing a power domain consisting of I/O
  993. * devices.
  994. */
  995. static int pm_genpd_thaw_early(struct device *dev)
  996. {
  997. struct generic_pm_domain *genpd;
  998. dev_dbg(dev, "%s()\n", __func__);
  999. genpd = dev_to_genpd(dev);
  1000. if (IS_ERR(genpd))
  1001. return -EINVAL;
  1002. return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
  1003. }
  1004. /**
  1005. * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
  1006. * @dev: Device to thaw.
  1007. *
  1008. * Thaw a device under the assumption that its pm_domain field points to the
  1009. * domain member of an object of type struct generic_pm_domain representing
  1010. * a power domain consisting of I/O devices.
  1011. */
  1012. static int pm_genpd_thaw(struct device *dev)
  1013. {
  1014. struct generic_pm_domain *genpd;
  1015. dev_dbg(dev, "%s()\n", __func__);
  1016. genpd = dev_to_genpd(dev);
  1017. if (IS_ERR(genpd))
  1018. return -EINVAL;
  1019. return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
  1020. }
  1021. /**
  1022. * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
  1023. * @dev: Device to resume.
  1024. *
  1025. * Make sure the domain will be in the same power state as before the
  1026. * hibernation the system is resuming from and start the device if necessary.
  1027. */
  1028. static int pm_genpd_restore_noirq(struct device *dev)
  1029. {
  1030. struct generic_pm_domain *genpd;
  1031. dev_dbg(dev, "%s()\n", __func__);
  1032. genpd = dev_to_genpd(dev);
  1033. if (IS_ERR(genpd))
  1034. return -EINVAL;
  1035. /*
  1036. * Since all of the "noirq" callbacks are executed sequentially, it is
  1037. * guaranteed that this function will never run twice in parallel for
  1038. * the same PM domain, so it is not necessary to use locking here.
  1039. *
  1040. * At this point suspended_count == 0 means we are being run for the
  1041. * first time for the given domain in the present cycle.
  1042. */
  1043. if (genpd->suspended_count++ == 0) {
  1044. /*
  1045. * The boot kernel might put the domain into arbitrary state,
  1046. * so make it appear as powered off to pm_genpd_sync_poweron(),
  1047. * so that it tries to power it on in case it was really off.
  1048. */
  1049. genpd->status = GPD_STATE_POWER_OFF;
  1050. if (genpd->suspend_power_off) {
  1051. /*
  1052. * If the domain was off before the hibernation, make
  1053. * sure it will be off going forward.
  1054. */
  1055. if (genpd->power_off)
  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
  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. mutex_lock(&gpd_list_lock);
  1596. list_add(&genpd->gpd_list_node, &gpd_list);
  1597. mutex_unlock(&gpd_list_lock);
  1598. }
  1599. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  1600. /*
  1601. * Device Tree based PM domain providers.
  1602. *
  1603. * The code below implements generic device tree based PM domain providers that
  1604. * bind device tree nodes with generic PM domains registered in the system.
  1605. *
  1606. * Any driver that registers generic PM domains and needs to support binding of
  1607. * devices to these domains is supposed to register a PM domain provider, which
  1608. * maps a PM domain specifier retrieved from the device tree to a PM domain.
  1609. *
  1610. * Two simple mapping functions have been provided for convenience:
  1611. * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
  1612. * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
  1613. * index.
  1614. */
  1615. /**
  1616. * struct of_genpd_provider - PM domain provider registration structure
  1617. * @link: Entry in global list of PM domain providers
  1618. * @node: Pointer to device tree node of PM domain provider
  1619. * @xlate: Provider-specific xlate callback mapping a set of specifier cells
  1620. * into a PM domain.
  1621. * @data: context pointer to be passed into @xlate callback
  1622. */
  1623. struct of_genpd_provider {
  1624. struct list_head link;
  1625. struct device_node *node;
  1626. genpd_xlate_t xlate;
  1627. void *data;
  1628. };
  1629. /* List of registered PM domain providers. */
  1630. static LIST_HEAD(of_genpd_providers);
  1631. /* Mutex to protect the list above. */
  1632. static DEFINE_MUTEX(of_genpd_mutex);
  1633. /**
  1634. * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
  1635. * @genpdspec: OF phandle args to map into a PM domain
  1636. * @data: xlate function private data - pointer to struct generic_pm_domain
  1637. *
  1638. * This is a generic xlate function that can be used to model PM domains that
  1639. * have their own device tree nodes. The private data of xlate function needs
  1640. * to be a valid pointer to struct generic_pm_domain.
  1641. */
  1642. struct generic_pm_domain *__of_genpd_xlate_simple(
  1643. struct of_phandle_args *genpdspec,
  1644. void *data)
  1645. {
  1646. if (genpdspec->args_count != 0)
  1647. return ERR_PTR(-EINVAL);
  1648. return data;
  1649. }
  1650. EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
  1651. /**
  1652. * __of_genpd_xlate_onecell() - Xlate function using a single index.
  1653. * @genpdspec: OF phandle args to map into a PM domain
  1654. * @data: xlate function private data - pointer to struct genpd_onecell_data
  1655. *
  1656. * This is a generic xlate function that can be used to model simple PM domain
  1657. * controllers that have one device tree node and provide multiple PM domains.
  1658. * A single cell is used as an index into an array of PM domains specified in
  1659. * the genpd_onecell_data struct when registering the provider.
  1660. */
  1661. struct generic_pm_domain *__of_genpd_xlate_onecell(
  1662. struct of_phandle_args *genpdspec,
  1663. void *data)
  1664. {
  1665. struct genpd_onecell_data *genpd_data = data;
  1666. unsigned int idx = genpdspec->args[0];
  1667. if (genpdspec->args_count != 1)
  1668. return ERR_PTR(-EINVAL);
  1669. if (idx >= genpd_data->num_domains) {
  1670. pr_err("%s: invalid domain index %u\n", __func__, idx);
  1671. return ERR_PTR(-EINVAL);
  1672. }
  1673. if (!genpd_data->domains[idx])
  1674. return ERR_PTR(-ENOENT);
  1675. return genpd_data->domains[idx];
  1676. }
  1677. EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
  1678. /**
  1679. * __of_genpd_add_provider() - Register a PM domain provider for a node
  1680. * @np: Device node pointer associated with the PM domain provider.
  1681. * @xlate: Callback for decoding PM domain from phandle arguments.
  1682. * @data: Context pointer for @xlate callback.
  1683. */
  1684. int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
  1685. void *data)
  1686. {
  1687. struct of_genpd_provider *cp;
  1688. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  1689. if (!cp)
  1690. return -ENOMEM;
  1691. cp->node = of_node_get(np);
  1692. cp->data = data;
  1693. cp->xlate = xlate;
  1694. mutex_lock(&of_genpd_mutex);
  1695. list_add(&cp->link, &of_genpd_providers);
  1696. mutex_unlock(&of_genpd_mutex);
  1697. pr_debug("Added domain provider from %s\n", np->full_name);
  1698. return 0;
  1699. }
  1700. EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
  1701. /**
  1702. * of_genpd_del_provider() - Remove a previously registered PM domain provider
  1703. * @np: Device node pointer associated with the PM domain provider
  1704. */
  1705. void of_genpd_del_provider(struct device_node *np)
  1706. {
  1707. struct of_genpd_provider *cp;
  1708. mutex_lock(&of_genpd_mutex);
  1709. list_for_each_entry(cp, &of_genpd_providers, link) {
  1710. if (cp->node == np) {
  1711. list_del(&cp->link);
  1712. of_node_put(cp->node);
  1713. kfree(cp);
  1714. break;
  1715. }
  1716. }
  1717. mutex_unlock(&of_genpd_mutex);
  1718. }
  1719. EXPORT_SYMBOL_GPL(of_genpd_del_provider);
  1720. /**
  1721. * of_genpd_get_from_provider() - Look-up PM domain
  1722. * @genpdspec: OF phandle args to use for look-up
  1723. *
  1724. * Looks for a PM domain provider under the node specified by @genpdspec and if
  1725. * found, uses xlate function of the provider to map phandle args to a PM
  1726. * domain.
  1727. *
  1728. * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
  1729. * on failure.
  1730. */
  1731. static struct generic_pm_domain *of_genpd_get_from_provider(
  1732. struct of_phandle_args *genpdspec)
  1733. {
  1734. struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
  1735. struct of_genpd_provider *provider;
  1736. mutex_lock(&of_genpd_mutex);
  1737. /* Check if we have such a provider in our array */
  1738. list_for_each_entry(provider, &of_genpd_providers, link) {
  1739. if (provider->node == genpdspec->np)
  1740. genpd = provider->xlate(genpdspec, provider->data);
  1741. if (!IS_ERR(genpd))
  1742. break;
  1743. }
  1744. mutex_unlock(&of_genpd_mutex);
  1745. return genpd;
  1746. }
  1747. /**
  1748. * genpd_dev_pm_detach - Detach a device from its PM domain.
  1749. * @dev: Device to attach.
  1750. * @power_off: Currently not used
  1751. *
  1752. * Try to locate a corresponding generic PM domain, which the device was
  1753. * attached to previously. If such is found, the device is detached from it.
  1754. */
  1755. static void genpd_dev_pm_detach(struct device *dev, bool power_off)
  1756. {
  1757. struct generic_pm_domain *pd = NULL, *gpd;
  1758. int ret = 0;
  1759. if (!dev->pm_domain)
  1760. return;
  1761. mutex_lock(&gpd_list_lock);
  1762. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  1763. if (&gpd->domain == dev->pm_domain) {
  1764. pd = gpd;
  1765. break;
  1766. }
  1767. }
  1768. mutex_unlock(&gpd_list_lock);
  1769. if (!pd)
  1770. return;
  1771. dev_dbg(dev, "removing from PM domain %s\n", pd->name);
  1772. while (1) {
  1773. ret = pm_genpd_remove_device(pd, dev);
  1774. if (ret != -EAGAIN)
  1775. break;
  1776. cond_resched();
  1777. }
  1778. if (ret < 0) {
  1779. dev_err(dev, "failed to remove from PM domain %s: %d",
  1780. pd->name, ret);
  1781. return;
  1782. }
  1783. /* Check if PM domain can be powered off after removing this device. */
  1784. genpd_queue_power_off_work(pd);
  1785. }
  1786. /**
  1787. * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
  1788. * @dev: Device to attach.
  1789. *
  1790. * Parse device's OF node to find a PM domain specifier. If such is found,
  1791. * attaches the device to retrieved pm_domain ops.
  1792. *
  1793. * Both generic and legacy Samsung-specific DT bindings are supported to keep
  1794. * backwards compatibility with existing DTBs.
  1795. *
  1796. * Returns 0 on successfully attached PM domain or negative error code.
  1797. */
  1798. int genpd_dev_pm_attach(struct device *dev)
  1799. {
  1800. struct of_phandle_args pd_args;
  1801. struct generic_pm_domain *pd;
  1802. int ret;
  1803. if (!dev->of_node)
  1804. return -ENODEV;
  1805. if (dev->pm_domain)
  1806. return -EEXIST;
  1807. ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
  1808. "#power-domain-cells", 0, &pd_args);
  1809. if (ret < 0) {
  1810. if (ret != -ENOENT)
  1811. return ret;
  1812. /*
  1813. * Try legacy Samsung-specific bindings
  1814. * (for backwards compatibility of DT ABI)
  1815. */
  1816. pd_args.args_count = 0;
  1817. pd_args.np = of_parse_phandle(dev->of_node,
  1818. "samsung,power-domain", 0);
  1819. if (!pd_args.np)
  1820. return -ENOENT;
  1821. }
  1822. pd = of_genpd_get_from_provider(&pd_args);
  1823. if (IS_ERR(pd)) {
  1824. dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
  1825. __func__, PTR_ERR(pd));
  1826. of_node_put(dev->of_node);
  1827. return PTR_ERR(pd);
  1828. }
  1829. dev_dbg(dev, "adding to PM domain %s\n", pd->name);
  1830. while (1) {
  1831. ret = pm_genpd_add_device(pd, dev);
  1832. if (ret != -EAGAIN)
  1833. break;
  1834. cond_resched();
  1835. }
  1836. if (ret < 0) {
  1837. dev_err(dev, "failed to add to PM domain %s: %d",
  1838. pd->name, ret);
  1839. of_node_put(dev->of_node);
  1840. return ret;
  1841. }
  1842. dev->pm_domain->detach = genpd_dev_pm_detach;
  1843. return 0;
  1844. }
  1845. EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
  1846. #endif
  1847. /*** debugfs support ***/
  1848. #ifdef CONFIG_PM_ADVANCED_DEBUG
  1849. #include <linux/pm.h>
  1850. #include <linux/device.h>
  1851. #include <linux/debugfs.h>
  1852. #include <linux/seq_file.h>
  1853. #include <linux/init.h>
  1854. #include <linux/kobject.h>
  1855. static struct dentry *pm_genpd_debugfs_dir;
  1856. /*
  1857. * TODO: This function is a slightly modified version of rtpm_status_show
  1858. * from sysfs.c, but dependencies between PM_GENERIC_DOMAINS and PM_RUNTIME
  1859. * are too loose to generalize it.
  1860. */
  1861. #ifdef CONFIG_PM_RUNTIME
  1862. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  1863. {
  1864. static const char * const status_lookup[] = {
  1865. [RPM_ACTIVE] = "active",
  1866. [RPM_RESUMING] = "resuming",
  1867. [RPM_SUSPENDED] = "suspended",
  1868. [RPM_SUSPENDING] = "suspending"
  1869. };
  1870. const char *p = "";
  1871. if (dev->power.runtime_error)
  1872. p = "error";
  1873. else if (dev->power.disable_depth)
  1874. p = "unsupported";
  1875. else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
  1876. p = status_lookup[dev->power.runtime_status];
  1877. else
  1878. WARN_ON(1);
  1879. seq_puts(s, p);
  1880. }
  1881. #else
  1882. static void rtpm_status_str(struct seq_file *s, struct device *dev)
  1883. {
  1884. seq_puts(s, "active");
  1885. }
  1886. #endif
  1887. static int pm_genpd_summary_one(struct seq_file *s,
  1888. struct generic_pm_domain *gpd)
  1889. {
  1890. static const char * const status_lookup[] = {
  1891. [GPD_STATE_ACTIVE] = "on",
  1892. [GPD_STATE_WAIT_MASTER] = "wait-master",
  1893. [GPD_STATE_BUSY] = "busy",
  1894. [GPD_STATE_REPEAT] = "off-in-progress",
  1895. [GPD_STATE_POWER_OFF] = "off"
  1896. };
  1897. struct pm_domain_data *pm_data;
  1898. const char *kobj_path;
  1899. struct gpd_link *link;
  1900. int ret;
  1901. ret = mutex_lock_interruptible(&gpd->lock);
  1902. if (ret)
  1903. return -ERESTARTSYS;
  1904. if (WARN_ON(gpd->status >= ARRAY_SIZE(status_lookup)))
  1905. goto exit;
  1906. seq_printf(s, "%-30s %-15s ", gpd->name, status_lookup[gpd->status]);
  1907. /*
  1908. * Modifications on the list require holding locks on both
  1909. * master and slave, so we are safe.
  1910. * Also gpd->name is immutable.
  1911. */
  1912. list_for_each_entry(link, &gpd->master_links, master_node) {
  1913. seq_printf(s, "%s", link->slave->name);
  1914. if (!list_is_last(&link->master_node, &gpd->master_links))
  1915. seq_puts(s, ", ");
  1916. }
  1917. list_for_each_entry(pm_data, &gpd->dev_list, list_node) {
  1918. kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
  1919. if (kobj_path == NULL)
  1920. continue;
  1921. seq_printf(s, "\n %-50s ", kobj_path);
  1922. rtpm_status_str(s, pm_data->dev);
  1923. kfree(kobj_path);
  1924. }
  1925. seq_puts(s, "\n");
  1926. exit:
  1927. mutex_unlock(&gpd->lock);
  1928. return 0;
  1929. }
  1930. static int pm_genpd_summary_show(struct seq_file *s, void *data)
  1931. {
  1932. struct generic_pm_domain *gpd;
  1933. int ret = 0;
  1934. seq_puts(s, " domain status slaves\n");
  1935. seq_puts(s, " /device runtime status\n");
  1936. seq_puts(s, "----------------------------------------------------------------------\n");
  1937. ret = mutex_lock_interruptible(&gpd_list_lock);
  1938. if (ret)
  1939. return -ERESTARTSYS;
  1940. list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  1941. ret = pm_genpd_summary_one(s, gpd);
  1942. if (ret)
  1943. break;
  1944. }
  1945. mutex_unlock(&gpd_list_lock);
  1946. return ret;
  1947. }
  1948. static int pm_genpd_summary_open(struct inode *inode, struct file *file)
  1949. {
  1950. return single_open(file, pm_genpd_summary_show, NULL);
  1951. }
  1952. static const struct file_operations pm_genpd_summary_fops = {
  1953. .open = pm_genpd_summary_open,
  1954. .read = seq_read,
  1955. .llseek = seq_lseek,
  1956. .release = single_release,
  1957. };
  1958. static int __init pm_genpd_debug_init(void)
  1959. {
  1960. struct dentry *d;
  1961. pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
  1962. if (!pm_genpd_debugfs_dir)
  1963. return -ENOMEM;
  1964. d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
  1965. pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
  1966. if (!d)
  1967. return -ENOMEM;
  1968. return 0;
  1969. }
  1970. late_initcall(pm_genpd_debug_init);
  1971. static void __exit pm_genpd_debug_exit(void)
  1972. {
  1973. debugfs_remove_recursive(pm_genpd_debugfs_dir);
  1974. }
  1975. __exitcall(pm_genpd_debug_exit);
  1976. #endif /* CONFIG_PM_ADVANCED_DEBUG */