domain.c 57 KB

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