cpufreq_governor.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <venkatesh.pallipadi@intel.com>.
  9. * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
  10. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  11. * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
  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. void *tuners;
  36. unsigned int ignore_nice_load;
  37. unsigned int sampling_rate;
  38. unsigned int sampling_down_factor;
  39. unsigned int up_threshold;
  40. unsigned int io_is_busy;
  41. };
  42. static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
  43. {
  44. return container_of(attr_set, struct dbs_data, attr_set);
  45. }
  46. #define gov_show_one(_gov, file_name) \
  47. static ssize_t show_##file_name \
  48. (struct gov_attr_set *attr_set, char *buf) \
  49. { \
  50. struct dbs_data *dbs_data = to_dbs_data(attr_set); \
  51. struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
  52. return sprintf(buf, "%u\n", tuners->file_name); \
  53. }
  54. #define gov_show_one_common(file_name) \
  55. static ssize_t show_##file_name \
  56. (struct gov_attr_set *attr_set, char *buf) \
  57. { \
  58. struct dbs_data *dbs_data = to_dbs_data(attr_set); \
  59. return sprintf(buf, "%u\n", dbs_data->file_name); \
  60. }
  61. #define gov_attr_ro(_name) \
  62. static struct governor_attr _name = \
  63. __ATTR(_name, 0444, show_##_name, NULL)
  64. #define gov_attr_rw(_name) \
  65. static struct governor_attr _name = \
  66. __ATTR(_name, 0644, show_##_name, store_##_name)
  67. /* Common to all CPUs of a policy */
  68. struct policy_dbs_info {
  69. struct cpufreq_policy *policy;
  70. /*
  71. * Per policy mutex that serializes load evaluation from limit-change
  72. * and work-handler.
  73. */
  74. struct mutex update_mutex;
  75. u64 last_sample_time;
  76. s64 sample_delay_ns;
  77. atomic_t work_count;
  78. struct irq_work irq_work;
  79. struct work_struct work;
  80. /* dbs_data may be shared between multiple policy objects */
  81. struct dbs_data *dbs_data;
  82. struct list_head list;
  83. /* Multiplier for increasing sample delay temporarily. */
  84. unsigned int rate_mult;
  85. unsigned int idle_periods; /* For conservative */
  86. /* Status indicators */
  87. bool is_shared; /* This object is used by multiple CPUs */
  88. bool work_in_progress; /* Work is being queued up or in progress */
  89. };
  90. static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
  91. unsigned int delay_us)
  92. {
  93. policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
  94. }
  95. /* Per cpu structures */
  96. struct cpu_dbs_info {
  97. u64 prev_cpu_idle;
  98. u64 prev_update_time;
  99. u64 prev_cpu_nice;
  100. /*
  101. * Used to keep track of load in the previous interval. However, when
  102. * explicitly set to zero, it is used as a flag to ensure that we copy
  103. * the previous load to the current interval only once, upon the first
  104. * wake-up from idle.
  105. */
  106. unsigned int prev_load;
  107. struct update_util_data update_util;
  108. struct policy_dbs_info *policy_dbs;
  109. };
  110. /* Common Governor data across policies */
  111. struct dbs_governor {
  112. struct cpufreq_governor gov;
  113. struct kobj_type kobj_type;
  114. /*
  115. * Common data for platforms that don't set
  116. * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
  117. */
  118. struct dbs_data *gdbs_data;
  119. unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
  120. struct policy_dbs_info *(*alloc)(void);
  121. void (*free)(struct policy_dbs_info *policy_dbs);
  122. int (*init)(struct dbs_data *dbs_data);
  123. void (*exit)(struct dbs_data *dbs_data);
  124. void (*start)(struct cpufreq_policy *policy);
  125. };
  126. static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
  127. {
  128. return container_of(policy->governor, struct dbs_governor, gov);
  129. }
  130. /* Governor callback routines */
  131. int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
  132. void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
  133. int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
  134. void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
  135. void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
  136. #define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \
  137. { \
  138. .name = _name_, \
  139. .flags = CPUFREQ_GOV_DYNAMIC_SWITCHING, \
  140. .owner = THIS_MODULE, \
  141. .init = cpufreq_dbs_governor_init, \
  142. .exit = cpufreq_dbs_governor_exit, \
  143. .start = cpufreq_dbs_governor_start, \
  144. .stop = cpufreq_dbs_governor_stop, \
  145. .limits = cpufreq_dbs_governor_limits, \
  146. }
  147. /* Governor specific operations */
  148. struct od_ops {
  149. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  150. unsigned int freq_next, unsigned int relation);
  151. };
  152. unsigned int dbs_update(struct cpufreq_policy *policy);
  153. void od_register_powersave_bias_handler(unsigned int (*f)
  154. (struct cpufreq_policy *, unsigned int, unsigned int),
  155. unsigned int powersave_bias);
  156. void od_unregister_powersave_bias_handler(void);
  157. ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf,
  158. size_t count);
  159. void gov_update_cpu_data(struct dbs_data *dbs_data);
  160. #endif /* _CPUFREQ_GOVERNOR_H */