cpufreq_governor.h 5.6 KB

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