pm_domain.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * pm_domain.h - Definitions and headers related to device power domains.
  4. *
  5. * Copyright (C) 2011 Rafael J. Wysocki <[email protected]>, Renesas Electronics Corp.
  6. */
  7. #ifndef _LINUX_PM_DOMAIN_H
  8. #define _LINUX_PM_DOMAIN_H
  9. #include <linux/device.h>
  10. #include <linux/ktime.h>
  11. #include <linux/mutex.h>
  12. #include <linux/pm.h>
  13. #include <linux/err.h>
  14. #include <linux/of.h>
  15. #include <linux/notifier.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/cpumask.h>
  18. /*
  19. * Flags to control the behaviour of a genpd.
  20. *
  21. * These flags may be set in the struct generic_pm_domain's flags field by a
  22. * genpd backend driver. The flags must be set before it calls pm_genpd_init(),
  23. * which initializes a genpd.
  24. *
  25. * GENPD_FLAG_PM_CLK: Instructs genpd to use the PM clk framework,
  26. * while powering on/off attached devices.
  27. *
  28. * GENPD_FLAG_IRQ_SAFE: This informs genpd that its backend callbacks,
  29. * ->power_on|off(), doesn't sleep. Hence, these
  30. * can be invoked from within atomic context, which
  31. * enables genpd to power on/off the PM domain,
  32. * even when pm_runtime_is_irq_safe() returns true,
  33. * for any of its attached devices. Note that, a
  34. * genpd having this flag set, requires its
  35. * masterdomains to also have it set.
  36. *
  37. * GENPD_FLAG_ALWAYS_ON: Instructs genpd to always keep the PM domain
  38. * powered on.
  39. *
  40. * GENPD_FLAG_ACTIVE_WAKEUP: Instructs genpd to keep the PM domain powered
  41. * on, in case any of its attached devices is used
  42. * in the wakeup path to serve system wakeups.
  43. *
  44. * GENPD_FLAG_CPU_DOMAIN: Instructs genpd that it should expect to get
  45. * devices attached, which may belong to CPUs or
  46. * possibly have subdomains with CPUs attached.
  47. * This flag enables the genpd backend driver to
  48. * deploy idle power management support for CPUs
  49. * and groups of CPUs. Note that, the backend
  50. * driver must then comply with the so called,
  51. * last-man-standing algorithm, for the CPUs in the
  52. * PM domain.
  53. *
  54. * GENPD_FLAG_RPM_ALWAYS_ON: Instructs genpd to always keep the PM domain
  55. * powered on except for system suspend.
  56. *
  57. * GENPD_FLAG_MIN_RESIDENCY: Enable the genpd governor to consider its
  58. * components' next wakeup when determining the
  59. * optimal idle state.
  60. */
  61. #define GENPD_FLAG_PM_CLK (1U << 0)
  62. #define GENPD_FLAG_IRQ_SAFE (1U << 1)
  63. #define GENPD_FLAG_ALWAYS_ON (1U << 2)
  64. #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3)
  65. #define GENPD_FLAG_CPU_DOMAIN (1U << 4)
  66. #define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
  67. #define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
  68. enum gpd_status {
  69. GENPD_STATE_ON = 0, /* PM domain is on */
  70. GENPD_STATE_OFF, /* PM domain is off */
  71. };
  72. enum genpd_notication {
  73. GENPD_NOTIFY_PRE_OFF = 0,
  74. GENPD_NOTIFY_OFF,
  75. GENPD_NOTIFY_PRE_ON,
  76. GENPD_NOTIFY_ON,
  77. };
  78. struct dev_power_governor {
  79. bool (*power_down_ok)(struct dev_pm_domain *domain);
  80. bool (*suspend_ok)(struct device *dev);
  81. };
  82. struct gpd_dev_ops {
  83. int (*start)(struct device *dev);
  84. int (*stop)(struct device *dev);
  85. };
  86. struct genpd_governor_data {
  87. s64 max_off_time_ns;
  88. bool max_off_time_changed;
  89. ktime_t next_wakeup;
  90. bool cached_power_down_ok;
  91. bool cached_power_down_state_idx;
  92. };
  93. struct genpd_power_state {
  94. s64 power_off_latency_ns;
  95. s64 power_on_latency_ns;
  96. s64 residency_ns;
  97. u64 usage;
  98. u64 rejected;
  99. struct fwnode_handle *fwnode;
  100. u64 idle_time;
  101. void *data;
  102. };
  103. struct genpd_lock_ops;
  104. struct dev_pm_opp;
  105. struct opp_table;
  106. struct generic_pm_domain {
  107. struct device dev;
  108. struct dev_pm_domain domain; /* PM domain operations */
  109. struct list_head gpd_list_node; /* Node in the global PM domains list */
  110. struct list_head parent_links; /* Links with PM domain as a parent */
  111. struct list_head child_links; /* Links with PM domain as a child */
  112. struct list_head dev_list; /* List of devices */
  113. struct dev_power_governor *gov;
  114. struct genpd_governor_data *gd; /* Data used by a genpd governor. */
  115. struct work_struct power_off_work;
  116. struct fwnode_handle *provider; /* Identity of the domain provider */
  117. bool has_provider;
  118. const char *name;
  119. atomic_t sd_count; /* Number of subdomains with power "on" */
  120. enum gpd_status status; /* Current state of the domain */
  121. unsigned int device_count; /* Number of devices */
  122. unsigned int suspended_count; /* System suspend device counter */
  123. unsigned int prepared_count; /* Suspend counter of prepared devices */
  124. unsigned int performance_state; /* Aggregated max performance state */
  125. cpumask_var_t cpus; /* A cpumask of the attached CPUs */
  126. int (*power_off)(struct generic_pm_domain *domain);
  127. int (*power_on)(struct generic_pm_domain *domain);
  128. struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
  129. struct opp_table *opp_table; /* OPP table of the genpd */
  130. unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
  131. struct dev_pm_opp *opp);
  132. int (*set_performance_state)(struct generic_pm_domain *genpd,
  133. unsigned int state);
  134. struct gpd_dev_ops dev_ops;
  135. int (*attach_dev)(struct generic_pm_domain *domain,
  136. struct device *dev);
  137. void (*detach_dev)(struct generic_pm_domain *domain,
  138. struct device *dev);
  139. unsigned int flags; /* Bit field of configs for genpd */
  140. struct genpd_power_state *states;
  141. void (*free_states)(struct genpd_power_state *states,
  142. unsigned int state_count);
  143. unsigned int state_count; /* number of states */
  144. unsigned int state_idx; /* state that genpd will go to when off */
  145. u64 on_time;
  146. u64 accounting_time;
  147. const struct genpd_lock_ops *lock_ops;
  148. union {
  149. struct mutex mlock;
  150. struct {
  151. spinlock_t slock;
  152. unsigned long lock_flags;
  153. };
  154. };
  155. };
  156. static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
  157. {
  158. return container_of(pd, struct generic_pm_domain, domain);
  159. }
  160. struct gpd_link {
  161. struct generic_pm_domain *parent;
  162. struct list_head parent_node;
  163. struct generic_pm_domain *child;
  164. struct list_head child_node;
  165. /* Sub-domain's per-master domain performance state */
  166. unsigned int performance_state;
  167. unsigned int prev_performance_state;
  168. };
  169. struct gpd_timing_data {
  170. s64 suspend_latency_ns;
  171. s64 resume_latency_ns;
  172. s64 effective_constraint_ns;
  173. ktime_t next_wakeup;
  174. bool constraint_changed;
  175. bool cached_suspend_ok;
  176. };
  177. struct pm_domain_data {
  178. struct list_head list_node;
  179. struct device *dev;
  180. };
  181. struct generic_pm_domain_data {
  182. struct pm_domain_data base;
  183. struct gpd_timing_data *td;
  184. struct notifier_block nb;
  185. struct notifier_block *power_nb;
  186. int cpu;
  187. unsigned int performance_state;
  188. unsigned int default_pstate;
  189. unsigned int rpm_pstate;
  190. void *data;
  191. };
  192. #ifdef CONFIG_PM_GENERIC_DOMAINS
  193. static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
  194. {
  195. return container_of(pdd, struct generic_pm_domain_data, base);
  196. }
  197. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  198. {
  199. return to_gpd_data(dev->power.subsys_data->domain_data);
  200. }
  201. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
  202. int pm_genpd_remove_device(struct device *dev);
  203. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  204. struct generic_pm_domain *subdomain);
  205. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  206. struct generic_pm_domain *subdomain);
  207. int pm_genpd_init(struct generic_pm_domain *genpd,
  208. struct dev_power_governor *gov, bool is_off);
  209. int pm_genpd_remove(struct generic_pm_domain *genpd);
  210. int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
  211. int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
  212. int dev_pm_genpd_remove_notifier(struct device *dev);
  213. void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
  214. extern struct dev_power_governor simple_qos_governor;
  215. extern struct dev_power_governor pm_domain_always_on_gov;
  216. #ifdef CONFIG_CPU_IDLE
  217. extern struct dev_power_governor pm_domain_cpu_gov;
  218. #endif
  219. #else
  220. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  221. {
  222. return ERR_PTR(-ENOSYS);
  223. }
  224. static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
  225. struct device *dev)
  226. {
  227. return -ENOSYS;
  228. }
  229. static inline int pm_genpd_remove_device(struct device *dev)
  230. {
  231. return -ENOSYS;
  232. }
  233. static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  234. struct generic_pm_domain *subdomain)
  235. {
  236. return -ENOSYS;
  237. }
  238. static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  239. struct generic_pm_domain *subdomain)
  240. {
  241. return -ENOSYS;
  242. }
  243. static inline int pm_genpd_init(struct generic_pm_domain *genpd,
  244. struct dev_power_governor *gov, bool is_off)
  245. {
  246. return -ENOSYS;
  247. }
  248. static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
  249. {
  250. return -EOPNOTSUPP;
  251. }
  252. static inline int dev_pm_genpd_set_performance_state(struct device *dev,
  253. unsigned int state)
  254. {
  255. return -EOPNOTSUPP;
  256. }
  257. static inline int dev_pm_genpd_add_notifier(struct device *dev,
  258. struct notifier_block *nb)
  259. {
  260. return -EOPNOTSUPP;
  261. }
  262. static inline int dev_pm_genpd_remove_notifier(struct device *dev)
  263. {
  264. return -EOPNOTSUPP;
  265. }
  266. static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
  267. { }
  268. #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
  269. #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
  270. #endif
  271. #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
  272. void dev_pm_genpd_suspend(struct device *dev);
  273. void dev_pm_genpd_resume(struct device *dev);
  274. #else
  275. static inline void dev_pm_genpd_suspend(struct device *dev) {}
  276. static inline void dev_pm_genpd_resume(struct device *dev) {}
  277. #endif
  278. /* OF PM domain providers */
  279. struct of_device_id;
  280. typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
  281. void *data);
  282. struct genpd_onecell_data {
  283. struct generic_pm_domain **domains;
  284. unsigned int num_domains;
  285. genpd_xlate_t xlate;
  286. };
  287. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  288. int of_genpd_add_provider_simple(struct device_node *np,
  289. struct generic_pm_domain *genpd);
  290. int of_genpd_add_provider_onecell(struct device_node *np,
  291. struct genpd_onecell_data *data);
  292. void of_genpd_del_provider(struct device_node *np);
  293. int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
  294. int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  295. struct of_phandle_args *subdomain_spec);
  296. int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
  297. struct of_phandle_args *subdomain_spec);
  298. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
  299. int of_genpd_parse_idle_states(struct device_node *dn,
  300. struct genpd_power_state **states, int *n);
  301. unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
  302. struct dev_pm_opp *opp);
  303. int genpd_dev_pm_attach(struct device *dev);
  304. struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  305. unsigned int index);
  306. struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  307. const char *name);
  308. #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
  309. static inline int of_genpd_add_provider_simple(struct device_node *np,
  310. struct generic_pm_domain *genpd)
  311. {
  312. return -EOPNOTSUPP;
  313. }
  314. static inline int of_genpd_add_provider_onecell(struct device_node *np,
  315. struct genpd_onecell_data *data)
  316. {
  317. return -EOPNOTSUPP;
  318. }
  319. static inline void of_genpd_del_provider(struct device_node *np) {}
  320. static inline int of_genpd_add_device(struct of_phandle_args *args,
  321. struct device *dev)
  322. {
  323. return -ENODEV;
  324. }
  325. static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  326. struct of_phandle_args *subdomain_spec)
  327. {
  328. return -ENODEV;
  329. }
  330. static inline int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
  331. struct of_phandle_args *subdomain_spec)
  332. {
  333. return -ENODEV;
  334. }
  335. static inline int of_genpd_parse_idle_states(struct device_node *dn,
  336. struct genpd_power_state **states, int *n)
  337. {
  338. return -ENODEV;
  339. }
  340. static inline unsigned int
  341. pm_genpd_opp_to_performance_state(struct device *genpd_dev,
  342. struct dev_pm_opp *opp)
  343. {
  344. return 0;
  345. }
  346. static inline int genpd_dev_pm_attach(struct device *dev)
  347. {
  348. return 0;
  349. }
  350. static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  351. unsigned int index)
  352. {
  353. return NULL;
  354. }
  355. static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  356. const char *name)
  357. {
  358. return NULL;
  359. }
  360. static inline
  361. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
  362. {
  363. return ERR_PTR(-EOPNOTSUPP);
  364. }
  365. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  366. #ifdef CONFIG_PM
  367. int dev_pm_domain_attach(struct device *dev, bool power_on);
  368. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  369. unsigned int index);
  370. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  371. const char *name);
  372. void dev_pm_domain_detach(struct device *dev, bool power_off);
  373. int dev_pm_domain_start(struct device *dev);
  374. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
  375. #else
  376. static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
  377. {
  378. return 0;
  379. }
  380. static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
  381. unsigned int index)
  382. {
  383. return NULL;
  384. }
  385. static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
  386. const char *name)
  387. {
  388. return NULL;
  389. }
  390. static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
  391. static inline int dev_pm_domain_start(struct device *dev)
  392. {
  393. return 0;
  394. }
  395. static inline void dev_pm_domain_set(struct device *dev,
  396. struct dev_pm_domain *pd) {}
  397. #endif
  398. #endif /* _LINUX_PM_DOMAIN_H */