pm_domain.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <rjw@sisk.pl>, 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_power_state {
  87. s64 power_off_latency_ns;
  88. s64 power_on_latency_ns;
  89. s64 residency_ns;
  90. u64 usage;
  91. u64 rejected;
  92. struct fwnode_handle *fwnode;
  93. ktime_t idle_time;
  94. void *data;
  95. };
  96. struct genpd_lock_ops;
  97. struct dev_pm_opp;
  98. struct opp_table;
  99. struct generic_pm_domain {
  100. struct device dev;
  101. struct dev_pm_domain domain; /* PM domain operations */
  102. struct list_head gpd_list_node; /* Node in the global PM domains list */
  103. struct list_head parent_links; /* Links with PM domain as a parent */
  104. struct list_head child_links; /* Links with PM domain as a child */
  105. struct list_head dev_list; /* List of devices */
  106. struct dev_power_governor *gov;
  107. struct work_struct power_off_work;
  108. struct fwnode_handle *provider; /* Identity of the domain provider */
  109. bool has_provider;
  110. const char *name;
  111. atomic_t sd_count; /* Number of subdomains with power "on" */
  112. enum gpd_status status; /* Current state of the domain */
  113. unsigned int device_count; /* Number of devices */
  114. unsigned int suspended_count; /* System suspend device counter */
  115. unsigned int prepared_count; /* Suspend counter of prepared devices */
  116. unsigned int performance_state; /* Aggregated max performance state */
  117. cpumask_var_t cpus; /* A cpumask of the attached CPUs */
  118. int (*power_off)(struct generic_pm_domain *domain);
  119. int (*power_on)(struct generic_pm_domain *domain);
  120. struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
  121. struct opp_table *opp_table; /* OPP table of the genpd */
  122. unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
  123. struct dev_pm_opp *opp);
  124. int (*set_performance_state)(struct generic_pm_domain *genpd,
  125. unsigned int state);
  126. struct gpd_dev_ops dev_ops;
  127. s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
  128. ktime_t next_wakeup; /* Maintained by the domain governor */
  129. bool max_off_time_changed;
  130. bool cached_power_down_ok;
  131. bool cached_power_down_state_idx;
  132. int (*attach_dev)(struct generic_pm_domain *domain,
  133. struct device *dev);
  134. void (*detach_dev)(struct generic_pm_domain *domain,
  135. struct device *dev);
  136. unsigned int flags; /* Bit field of configs for genpd */
  137. struct genpd_power_state *states;
  138. void (*free_states)(struct genpd_power_state *states,
  139. unsigned int state_count);
  140. unsigned int state_count; /* number of states */
  141. unsigned int state_idx; /* state that genpd will go to when off */
  142. ktime_t on_time;
  143. ktime_t accounting_time;
  144. const struct genpd_lock_ops *lock_ops;
  145. union {
  146. struct mutex mlock;
  147. struct {
  148. spinlock_t slock;
  149. unsigned long lock_flags;
  150. };
  151. };
  152. };
  153. static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
  154. {
  155. return container_of(pd, struct generic_pm_domain, domain);
  156. }
  157. struct gpd_link {
  158. struct generic_pm_domain *parent;
  159. struct list_head parent_node;
  160. struct generic_pm_domain *child;
  161. struct list_head child_node;
  162. /* Sub-domain's per-master domain performance state */
  163. unsigned int performance_state;
  164. unsigned int prev_performance_state;
  165. };
  166. struct gpd_timing_data {
  167. s64 suspend_latency_ns;
  168. s64 resume_latency_ns;
  169. s64 effective_constraint_ns;
  170. bool constraint_changed;
  171. bool cached_suspend_ok;
  172. };
  173. struct pm_domain_data {
  174. struct list_head list_node;
  175. struct device *dev;
  176. };
  177. struct generic_pm_domain_data {
  178. struct pm_domain_data base;
  179. struct gpd_timing_data td;
  180. struct notifier_block nb;
  181. struct notifier_block *power_nb;
  182. int cpu;
  183. unsigned int performance_state;
  184. ktime_t next_wakeup;
  185. void *data;
  186. };
  187. #ifdef CONFIG_PM_GENERIC_DOMAINS
  188. static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
  189. {
  190. return container_of(pdd, struct generic_pm_domain_data, base);
  191. }
  192. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  193. {
  194. return to_gpd_data(dev->power.subsys_data->domain_data);
  195. }
  196. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
  197. int pm_genpd_remove_device(struct device *dev);
  198. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  199. struct generic_pm_domain *subdomain);
  200. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  201. struct generic_pm_domain *subdomain);
  202. int pm_genpd_init(struct generic_pm_domain *genpd,
  203. struct dev_power_governor *gov, bool is_off);
  204. int pm_genpd_remove(struct generic_pm_domain *genpd);
  205. int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
  206. int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
  207. int dev_pm_genpd_remove_notifier(struct device *dev);
  208. void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next);
  209. extern struct dev_power_governor simple_qos_governor;
  210. extern struct dev_power_governor pm_domain_always_on_gov;
  211. #ifdef CONFIG_CPU_IDLE
  212. extern struct dev_power_governor pm_domain_cpu_gov;
  213. #endif
  214. #else
  215. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  216. {
  217. return ERR_PTR(-ENOSYS);
  218. }
  219. static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
  220. struct device *dev)
  221. {
  222. return -ENOSYS;
  223. }
  224. static inline int pm_genpd_remove_device(struct device *dev)
  225. {
  226. return -ENOSYS;
  227. }
  228. static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  229. struct generic_pm_domain *subdomain)
  230. {
  231. return -ENOSYS;
  232. }
  233. static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  234. struct generic_pm_domain *subdomain)
  235. {
  236. return -ENOSYS;
  237. }
  238. static inline int pm_genpd_init(struct generic_pm_domain *genpd,
  239. struct dev_power_governor *gov, bool is_off)
  240. {
  241. return -ENOSYS;
  242. }
  243. static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
  244. {
  245. return -ENOTSUPP;
  246. }
  247. static inline int dev_pm_genpd_set_performance_state(struct device *dev,
  248. unsigned int state)
  249. {
  250. return -ENOTSUPP;
  251. }
  252. static inline int dev_pm_genpd_add_notifier(struct device *dev,
  253. struct notifier_block *nb)
  254. {
  255. return -ENOTSUPP;
  256. }
  257. static inline int dev_pm_genpd_remove_notifier(struct device *dev)
  258. {
  259. return -ENOTSUPP;
  260. }
  261. static inline void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
  262. { }
  263. #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
  264. #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
  265. #endif
  266. #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
  267. void dev_pm_genpd_suspend(struct device *dev);
  268. void dev_pm_genpd_resume(struct device *dev);
  269. #else
  270. static inline void dev_pm_genpd_suspend(struct device *dev) {}
  271. static inline void dev_pm_genpd_resume(struct device *dev) {}
  272. #endif
  273. /* OF PM domain providers */
  274. struct of_device_id;
  275. typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
  276. void *data);
  277. struct genpd_onecell_data {
  278. struct generic_pm_domain **domains;
  279. unsigned int num_domains;
  280. genpd_xlate_t xlate;
  281. };
  282. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  283. int of_genpd_add_provider_simple(struct device_node *np,
  284. struct generic_pm_domain *genpd);
  285. int of_genpd_add_provider_onecell(struct device_node *np,
  286. struct genpd_onecell_data *data);
  287. void of_genpd_del_provider(struct device_node *np);
  288. int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
  289. int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  290. struct of_phandle_args *subdomain_spec);
  291. int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
  292. struct of_phandle_args *subdomain_spec);
  293. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
  294. int of_genpd_parse_idle_states(struct device_node *dn,
  295. struct genpd_power_state **states, int *n);
  296. unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
  297. struct dev_pm_opp *opp);
  298. int genpd_dev_pm_attach(struct device *dev);
  299. struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  300. unsigned int index);
  301. struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  302. const char *name);
  303. #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
  304. static inline int of_genpd_add_provider_simple(struct device_node *np,
  305. struct generic_pm_domain *genpd)
  306. {
  307. return -ENOTSUPP;
  308. }
  309. static inline int of_genpd_add_provider_onecell(struct device_node *np,
  310. struct genpd_onecell_data *data)
  311. {
  312. return -ENOTSUPP;
  313. }
  314. static inline void of_genpd_del_provider(struct device_node *np) {}
  315. static inline int of_genpd_add_device(struct of_phandle_args *args,
  316. struct device *dev)
  317. {
  318. return -ENODEV;
  319. }
  320. static inline int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
  321. struct of_phandle_args *subdomain_spec)
  322. {
  323. return -ENODEV;
  324. }
  325. static inline int of_genpd_remove_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_parse_idle_states(struct device_node *dn,
  331. struct genpd_power_state **states, int *n)
  332. {
  333. return -ENODEV;
  334. }
  335. static inline unsigned int
  336. pm_genpd_opp_to_performance_state(struct device *genpd_dev,
  337. struct dev_pm_opp *opp)
  338. {
  339. return 0;
  340. }
  341. static inline int genpd_dev_pm_attach(struct device *dev)
  342. {
  343. return 0;
  344. }
  345. static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  346. unsigned int index)
  347. {
  348. return NULL;
  349. }
  350. static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  351. const char *name)
  352. {
  353. return NULL;
  354. }
  355. static inline
  356. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
  357. {
  358. return ERR_PTR(-ENOTSUPP);
  359. }
  360. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  361. #ifdef CONFIG_PM
  362. int dev_pm_domain_attach(struct device *dev, bool power_on);
  363. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  364. unsigned int index);
  365. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  366. const char *name);
  367. void dev_pm_domain_detach(struct device *dev, bool power_off);
  368. int dev_pm_domain_start(struct device *dev);
  369. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
  370. #else
  371. static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
  372. {
  373. return 0;
  374. }
  375. static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
  376. unsigned int index)
  377. {
  378. return NULL;
  379. }
  380. static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
  381. const char *name)
  382. {
  383. return NULL;
  384. }
  385. static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
  386. static inline int dev_pm_domain_start(struct device *dev)
  387. {
  388. return 0;
  389. }
  390. static inline void dev_pm_domain_set(struct device *dev,
  391. struct dev_pm_domain *pd) {}
  392. #endif
  393. #endif /* _LINUX_PM_DOMAIN_H */