cpufreq_governor.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * drivers/cpufreq/cpufreq_governor.h
  4. *
  5. * Header file for CPUFreq governors common code
  6. *
  7. * Copyright (C) 2001 Russell King
  8. * (C) 2003 Venkatesh Pallipadi <[email protected]>.
  9. * (C) 2003 Jun Nakajima <[email protected]>
  10. * (C) 2009 Alexander Clouter <[email protected]>
  11. * (c) 2012 Viresh Kumar <[email protected]>
  12. */
  13. #ifndef _CPUFREQ_GOVERNOR_H
  14. #define _CPUFREQ_GOVERNOR_H
  15. #include <linux/atomic.h>
  16. #include <linux/irq_work.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/sched/cpufreq.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. /* Ondemand Sampling types */
  23. enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
  24. /*
  25. * Abbreviations:
  26. * dbs: used as a shortform for demand based switching It helps to keep variable
  27. * names smaller, simpler
  28. * cdbs: common dbs
  29. * od_*: On-demand governor
  30. * cs_*: Conservative governor
  31. */
  32. /* Governor demand based switching data (per-policy or global). */
  33. struct dbs_data {
  34. struct gov_attr_set attr_set;
  35. struct dbs_governor *gov;
  36. void *tuners;
  37. unsigned int ignore_nice_load;
  38. unsigned int sampling_rate;
  39. unsigned int sampling_down_factor;
  40. unsigned int up_threshold;
  41. unsigned int io_is_busy;
  42. };
  43. static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
  44. {
  45. return container_of(attr_set, struct dbs_data, attr_set);
  46. }
  47. #define gov_show_one(_gov, file_name) \
  48. static ssize_t file_name##_show \
  49. (struct gov_attr_set *attr_set, char *buf) \
  50. { \
  51. struct dbs_data *dbs_data = to_dbs_data(attr_set); \
  52. struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
  53. return sprintf(buf, "%u\n", tuners->file_name); \
  54. }
  55. #define gov_show_one_common(file_name) \
  56. static ssize_t file_name##_show \
  57. (struct gov_attr_set *attr_set, char *buf) \
  58. { \
  59. struct dbs_data *dbs_data = to_dbs_data(attr_set); \
  60. return sprintf(buf, "%u\n", dbs_data->file_name); \
  61. }
  62. #define gov_attr_ro(_name) \
  63. static struct governor_attr _name = __ATTR_RO(_name)
  64. #define gov_attr_rw(_name) \
  65. static struct governor_attr _name = __ATTR_RW(_name)
  66. /* Common to all CPUs of a policy */
  67. struct policy_dbs_info {
  68. struct cpufreq_policy *policy;
  69. /*
  70. * Per policy mutex that serializes load evaluation from limit-change
  71. * and work-handler.
  72. */
  73. struct mutex update_mutex;
  74. u64 last_sample_time;
  75. s64 sample_delay_ns;
  76. atomic_t work_count;
  77. struct irq_work irq_work;
  78. struct work_struct work;
  79. /* dbs_data may be shared between multiple policy objects */
  80. struct dbs_data *dbs_data;
  81. struct list_head list;
  82. /* Multiplier for increasing sample delay temporarily. */
  83. unsigned int rate_mult;
  84. unsigned int idle_periods; /* For conservative */
  85. /* Status indicators */
  86. bool is_shared; /* This object is used by multiple CPUs */
  87. bool work_in_progress; /* Work is being queued up or in progress */
  88. };
  89. static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
  90. unsigned int delay_us)
  91. {
  92. policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
  93. }
  94. /* Per cpu structures */
  95. struct cpu_dbs_info {
  96. u64 prev_cpu_idle;
  97. u64 prev_update_time;
  98. u64 prev_cpu_nice;
  99. /*
  100. * Used to keep track of load in the previous interval. However, when
  101. * explicitly set to zero, it is used as a flag to ensure that we copy
  102. * the previous load to the current interval only once, upon the first
  103. * wake-up from idle.
  104. */
  105. unsigned int prev_load;
  106. struct update_util_data update_util;
  107. struct policy_dbs_info *policy_dbs;
  108. };
  109. /* Common Governor data across policies */
  110. struct dbs_governor {
  111. struct cpufreq_governor gov;
  112. struct kobj_type kobj_type;
  113. /*
  114. * Common data for platforms that don't set
  115. * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
  116. */
  117. struct dbs_data *gdbs_data;
  118. unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
  119. struct policy_dbs_info *(*alloc)(void);
  120. void (*free)(struct policy_dbs_info *policy_dbs);
  121. int (*init)(struct dbs_data *dbs_data);
  122. void (*exit)(struct dbs_data *dbs_data);
  123. void (*start)(struct cpufreq_policy *policy);
  124. };
  125. static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
  126. {
  127. return container_of(policy->governor, struct dbs_governor, gov);
  128. }
  129. /* Governor callback routines */
  130. int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
  131. void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
  132. int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
  133. void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
  134. void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
  135. #define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
  136. { \
  137. .name = _name_, \
  138. .flags = CPUFREQ_GOV_DYNAMIC_SWITCHING, \
  139. .owner = THIS_MODULE, \
  140. .init = cpufreq_dbs_governor_init, \
  141. .exit = cpufreq_dbs_governor_exit, \
  142. .start = cpufreq_dbs_governor_start, \
  143. .stop = cpufreq_dbs_governor_stop, \
  144. .limits = cpufreq_dbs_governor_limits, \
  145. }
  146. /* Governor specific operations */
  147. struct od_ops {
  148. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  149. unsigned int freq_next, unsigned int relation);
  150. };
  151. unsigned int dbs_update(struct cpufreq_policy *policy);
  152. void od_register_powersave_bias_handler(unsigned int (*f)
  153. (struct cpufreq_policy *, unsigned int, unsigned int),
  154. unsigned int powersave_bias);
  155. void od_unregister_powersave_bias_handler(void);
  156. ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf,
  157. size_t count);
  158. void gov_update_cpu_data(struct dbs_data *dbs_data);
  159. #endif /* _CPUFREQ_GOVERNOR_H */