pm_domain.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * pm_domain.h - Definitions and headers 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. #ifndef _LINUX_PM_DOMAIN_H
  9. #define _LINUX_PM_DOMAIN_H
  10. #include <linux/device.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. /* Defines used for the flags field in the struct generic_pm_domain */
  18. #define GENPD_FLAG_PM_CLK (1U << 0) /* PM domain uses PM clk */
  19. #define GENPD_FLAG_IRQ_SAFE (1U << 1) /* PM domain operates in atomic */
  20. #define GENPD_FLAG_ALWAYS_ON (1U << 2) /* PM domain is always powered on */
  21. #define GENPD_FLAG_ACTIVE_WAKEUP (1U << 3) /* Keep devices active if wakeup */
  22. enum gpd_status {
  23. GPD_STATE_ACTIVE = 0, /* PM domain is active */
  24. GPD_STATE_POWER_OFF, /* PM domain is off */
  25. };
  26. struct dev_power_governor {
  27. bool (*power_down_ok)(struct dev_pm_domain *domain);
  28. bool (*suspend_ok)(struct device *dev);
  29. };
  30. struct gpd_dev_ops {
  31. int (*start)(struct device *dev);
  32. int (*stop)(struct device *dev);
  33. };
  34. struct genpd_power_state {
  35. s64 power_off_latency_ns;
  36. s64 power_on_latency_ns;
  37. s64 residency_ns;
  38. struct fwnode_handle *fwnode;
  39. ktime_t idle_time;
  40. };
  41. struct genpd_lock_ops;
  42. struct dev_pm_opp;
  43. struct generic_pm_domain {
  44. struct device dev;
  45. struct dev_pm_domain domain; /* PM domain operations */
  46. struct list_head gpd_list_node; /* Node in the global PM domains list */
  47. struct list_head master_links; /* Links with PM domain as a master */
  48. struct list_head slave_links; /* Links with PM domain as a slave */
  49. struct list_head dev_list; /* List of devices */
  50. struct dev_power_governor *gov;
  51. struct work_struct power_off_work;
  52. struct fwnode_handle *provider; /* Identity of the domain provider */
  53. bool has_provider;
  54. const char *name;
  55. atomic_t sd_count; /* Number of subdomains with power "on" */
  56. enum gpd_status status; /* Current state of the domain */
  57. unsigned int device_count; /* Number of devices */
  58. unsigned int suspended_count; /* System suspend device counter */
  59. unsigned int prepared_count; /* Suspend counter of prepared devices */
  60. unsigned int performance_state; /* Aggregated max performance state */
  61. int (*power_off)(struct generic_pm_domain *domain);
  62. int (*power_on)(struct generic_pm_domain *domain);
  63. unsigned int (*opp_to_performance_state)(struct generic_pm_domain *genpd,
  64. struct dev_pm_opp *opp);
  65. int (*set_performance_state)(struct generic_pm_domain *genpd,
  66. unsigned int state);
  67. struct gpd_dev_ops dev_ops;
  68. s64 max_off_time_ns; /* Maximum allowed "suspended" time. */
  69. bool max_off_time_changed;
  70. bool cached_power_down_ok;
  71. int (*attach_dev)(struct generic_pm_domain *domain,
  72. struct device *dev);
  73. void (*detach_dev)(struct generic_pm_domain *domain,
  74. struct device *dev);
  75. unsigned int flags; /* Bit field of configs for genpd */
  76. struct genpd_power_state *states;
  77. unsigned int state_count; /* number of states */
  78. unsigned int state_idx; /* state that genpd will go to when off */
  79. void *free; /* Free the state that was allocated for default */
  80. ktime_t on_time;
  81. ktime_t accounting_time;
  82. const struct genpd_lock_ops *lock_ops;
  83. union {
  84. struct mutex mlock;
  85. struct {
  86. spinlock_t slock;
  87. unsigned long lock_flags;
  88. };
  89. };
  90. };
  91. static inline struct generic_pm_domain *pd_to_genpd(struct dev_pm_domain *pd)
  92. {
  93. return container_of(pd, struct generic_pm_domain, domain);
  94. }
  95. struct gpd_link {
  96. struct generic_pm_domain *master;
  97. struct list_head master_node;
  98. struct generic_pm_domain *slave;
  99. struct list_head slave_node;
  100. };
  101. struct gpd_timing_data {
  102. s64 suspend_latency_ns;
  103. s64 resume_latency_ns;
  104. s64 effective_constraint_ns;
  105. bool constraint_changed;
  106. bool cached_suspend_ok;
  107. };
  108. struct pm_domain_data {
  109. struct list_head list_node;
  110. struct device *dev;
  111. };
  112. struct generic_pm_domain_data {
  113. struct pm_domain_data base;
  114. struct gpd_timing_data td;
  115. struct notifier_block nb;
  116. unsigned int performance_state;
  117. void *data;
  118. };
  119. #ifdef CONFIG_PM_GENERIC_DOMAINS
  120. static inline struct generic_pm_domain_data *to_gpd_data(struct pm_domain_data *pdd)
  121. {
  122. return container_of(pdd, struct generic_pm_domain_data, base);
  123. }
  124. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  125. {
  126. return to_gpd_data(dev->power.subsys_data->domain_data);
  127. }
  128. int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev);
  129. int pm_genpd_remove_device(struct device *dev);
  130. int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  131. struct generic_pm_domain *new_subdomain);
  132. int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  133. struct generic_pm_domain *target);
  134. int pm_genpd_init(struct generic_pm_domain *genpd,
  135. struct dev_power_governor *gov, bool is_off);
  136. int pm_genpd_remove(struct generic_pm_domain *genpd);
  137. int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
  138. extern struct dev_power_governor simple_qos_governor;
  139. extern struct dev_power_governor pm_domain_always_on_gov;
  140. #else
  141. static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)
  142. {
  143. return ERR_PTR(-ENOSYS);
  144. }
  145. static inline int pm_genpd_add_device(struct generic_pm_domain *genpd,
  146. struct device *dev)
  147. {
  148. return -ENOSYS;
  149. }
  150. static inline int pm_genpd_remove_device(struct device *dev)
  151. {
  152. return -ENOSYS;
  153. }
  154. static inline int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
  155. struct generic_pm_domain *new_sd)
  156. {
  157. return -ENOSYS;
  158. }
  159. static inline int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
  160. struct generic_pm_domain *target)
  161. {
  162. return -ENOSYS;
  163. }
  164. static inline int pm_genpd_init(struct generic_pm_domain *genpd,
  165. struct dev_power_governor *gov, bool is_off)
  166. {
  167. return -ENOSYS;
  168. }
  169. static inline int pm_genpd_remove(struct generic_pm_domain *genpd)
  170. {
  171. return -ENOTSUPP;
  172. }
  173. static inline int dev_pm_genpd_set_performance_state(struct device *dev,
  174. unsigned int state)
  175. {
  176. return -ENOTSUPP;
  177. }
  178. #define simple_qos_governor (*(struct dev_power_governor *)(NULL))
  179. #define pm_domain_always_on_gov (*(struct dev_power_governor *)(NULL))
  180. #endif
  181. #ifdef CONFIG_PM_GENERIC_DOMAINS_SLEEP
  182. void pm_genpd_syscore_poweroff(struct device *dev);
  183. void pm_genpd_syscore_poweron(struct device *dev);
  184. #else
  185. static inline void pm_genpd_syscore_poweroff(struct device *dev) {}
  186. static inline void pm_genpd_syscore_poweron(struct device *dev) {}
  187. #endif
  188. /* OF PM domain providers */
  189. struct of_device_id;
  190. typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
  191. void *data);
  192. struct genpd_onecell_data {
  193. struct generic_pm_domain **domains;
  194. unsigned int num_domains;
  195. genpd_xlate_t xlate;
  196. };
  197. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  198. int of_genpd_add_provider_simple(struct device_node *np,
  199. struct generic_pm_domain *genpd);
  200. int of_genpd_add_provider_onecell(struct device_node *np,
  201. struct genpd_onecell_data *data);
  202. void of_genpd_del_provider(struct device_node *np);
  203. int of_genpd_add_device(struct of_phandle_args *args, struct device *dev);
  204. int of_genpd_add_subdomain(struct of_phandle_args *parent,
  205. struct of_phandle_args *new_subdomain);
  206. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
  207. int of_genpd_parse_idle_states(struct device_node *dn,
  208. struct genpd_power_state **states, int *n);
  209. unsigned int of_genpd_opp_to_performance_state(struct device *dev,
  210. struct device_node *np);
  211. int genpd_dev_pm_attach(struct device *dev);
  212. struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  213. unsigned int index);
  214. struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  215. char *name);
  216. #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
  217. static inline int of_genpd_add_provider_simple(struct device_node *np,
  218. struct generic_pm_domain *genpd)
  219. {
  220. return -ENOTSUPP;
  221. }
  222. static inline int of_genpd_add_provider_onecell(struct device_node *np,
  223. struct genpd_onecell_data *data)
  224. {
  225. return -ENOTSUPP;
  226. }
  227. static inline void of_genpd_del_provider(struct device_node *np) {}
  228. static inline int of_genpd_add_device(struct of_phandle_args *args,
  229. struct device *dev)
  230. {
  231. return -ENODEV;
  232. }
  233. static inline int of_genpd_add_subdomain(struct of_phandle_args *parent,
  234. struct of_phandle_args *new_subdomain)
  235. {
  236. return -ENODEV;
  237. }
  238. static inline int of_genpd_parse_idle_states(struct device_node *dn,
  239. struct genpd_power_state **states, int *n)
  240. {
  241. return -ENODEV;
  242. }
  243. static inline unsigned int
  244. of_genpd_opp_to_performance_state(struct device *dev,
  245. struct device_node *np)
  246. {
  247. return 0;
  248. }
  249. static inline int genpd_dev_pm_attach(struct device *dev)
  250. {
  251. return 0;
  252. }
  253. static inline struct device *genpd_dev_pm_attach_by_id(struct device *dev,
  254. unsigned int index)
  255. {
  256. return NULL;
  257. }
  258. static inline struct device *genpd_dev_pm_attach_by_name(struct device *dev,
  259. char *name)
  260. {
  261. return NULL;
  262. }
  263. static inline
  264. struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
  265. {
  266. return ERR_PTR(-ENOTSUPP);
  267. }
  268. #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
  269. #ifdef CONFIG_PM
  270. int dev_pm_domain_attach(struct device *dev, bool power_on);
  271. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  272. unsigned int index);
  273. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  274. char *name);
  275. void dev_pm_domain_detach(struct device *dev, bool power_off);
  276. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd);
  277. #else
  278. static inline int dev_pm_domain_attach(struct device *dev, bool power_on)
  279. {
  280. return 0;
  281. }
  282. static inline struct device *dev_pm_domain_attach_by_id(struct device *dev,
  283. unsigned int index)
  284. {
  285. return NULL;
  286. }
  287. static inline struct device *dev_pm_domain_attach_by_name(struct device *dev,
  288. char *name)
  289. {
  290. return NULL;
  291. }
  292. static inline void dev_pm_domain_detach(struct device *dev, bool power_off) {}
  293. static inline void dev_pm_domain_set(struct device *dev,
  294. struct dev_pm_domain *pd) {}
  295. #endif
  296. #endif /* _LINUX_PM_DOMAIN_H */