cpufreq_governor.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_GOVERNER_H
  17. #define _CPUFREQ_GOVERNER_H
  18. #include <linux/cpufreq.h>
  19. #include <linux/kobject.h>
  20. #include <linux/mutex.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/sysfs.h>
  23. /*
  24. * The polling frequency depends on the capability of the processor. Default
  25. * polling frequency is 1000 times the transition latency of the processor. The
  26. * governor will work on any processor with transition latency <= 10mS, using
  27. * appropriate sampling rate.
  28. *
  29. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  30. * this governor will not work. All times here are in uS.
  31. */
  32. #define MIN_SAMPLING_RATE_RATIO (2)
  33. #define LATENCY_MULTIPLIER (1000)
  34. #define MIN_LATENCY_MULTIPLIER (100)
  35. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  36. /* Ondemand Sampling types */
  37. enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
  38. /* Macro creating sysfs show routines */
  39. #define show_one(_gov, file_name, object) \
  40. static ssize_t show_##file_name \
  41. (struct kobject *kobj, struct attribute *attr, char *buf) \
  42. { \
  43. return sprintf(buf, "%u\n", _gov##_tuners.object); \
  44. }
  45. #define define_get_cpu_dbs_routines(_dbs_info) \
  46. static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu) \
  47. { \
  48. return &per_cpu(_dbs_info, cpu).cdbs; \
  49. } \
  50. \
  51. static void *get_cpu_dbs_info_s(int cpu) \
  52. { \
  53. return &per_cpu(_dbs_info, cpu); \
  54. }
  55. /*
  56. * Abbreviations:
  57. * dbs: used as a shortform for demand based switching It helps to keep variable
  58. * names smaller, simpler
  59. * cdbs: common dbs
  60. * od_*: On-demand governor
  61. * cs_*: Conservative governor
  62. */
  63. /* Per cpu structures */
  64. struct cpu_dbs_common_info {
  65. int cpu;
  66. u64 prev_cpu_idle;
  67. u64 prev_cpu_wall;
  68. u64 prev_cpu_nice;
  69. struct cpufreq_policy *cur_policy;
  70. struct delayed_work work;
  71. /*
  72. * percpu mutex that serializes governor limit change with gov_dbs_timer
  73. * invocation. We do not want gov_dbs_timer to run when user is changing
  74. * the governor or limits.
  75. */
  76. struct mutex timer_mutex;
  77. ktime_t time_stamp;
  78. };
  79. struct od_cpu_dbs_info_s {
  80. struct cpu_dbs_common_info cdbs;
  81. u64 prev_cpu_iowait;
  82. struct cpufreq_frequency_table *freq_table;
  83. unsigned int freq_lo;
  84. unsigned int freq_lo_jiffies;
  85. unsigned int freq_hi_jiffies;
  86. unsigned int rate_mult;
  87. unsigned int sample_type:1;
  88. };
  89. struct cs_cpu_dbs_info_s {
  90. struct cpu_dbs_common_info cdbs;
  91. unsigned int down_skip;
  92. unsigned int requested_freq;
  93. unsigned int enable:1;
  94. };
  95. /* Governers sysfs tunables */
  96. struct od_dbs_tuners {
  97. unsigned int ignore_nice;
  98. unsigned int sampling_rate;
  99. unsigned int sampling_down_factor;
  100. unsigned int up_threshold;
  101. unsigned int adj_up_threshold;
  102. unsigned int powersave_bias;
  103. unsigned int io_is_busy;
  104. };
  105. struct cs_dbs_tuners {
  106. unsigned int ignore_nice;
  107. unsigned int sampling_rate;
  108. unsigned int sampling_down_factor;
  109. unsigned int up_threshold;
  110. unsigned int down_threshold;
  111. unsigned int freq_step;
  112. };
  113. /* Per Governer data */
  114. struct dbs_data {
  115. /* Common across governors */
  116. #define GOV_ONDEMAND 0
  117. #define GOV_CONSERVATIVE 1
  118. int governor;
  119. unsigned int min_sampling_rate;
  120. struct attribute_group *attr_group;
  121. void *tuners;
  122. /* dbs_mutex protects dbs_enable in governor start/stop */
  123. struct mutex mutex;
  124. struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu);
  125. void *(*get_cpu_dbs_info_s)(int cpu);
  126. void (*gov_dbs_timer)(struct work_struct *work);
  127. void (*gov_check_cpu)(int cpu, unsigned int load);
  128. /* Governor specific ops, see below */
  129. void *gov_ops;
  130. };
  131. /* Governor specific ops, will be passed to dbs_data->gov_ops */
  132. struct od_ops {
  133. int (*io_busy)(void);
  134. void (*powersave_bias_init_cpu)(int cpu);
  135. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  136. unsigned int freq_next, unsigned int relation);
  137. void (*freq_increase)(struct cpufreq_policy *p, unsigned int freq);
  138. };
  139. struct cs_ops {
  140. struct notifier_block *notifier_block;
  141. };
  142. static inline int delay_for_sampling_rate(unsigned int sampling_rate)
  143. {
  144. int delay = usecs_to_jiffies(sampling_rate);
  145. /* We want all CPUs to do sampling nearly on same jiffy */
  146. if (num_online_cpus() > 1)
  147. delay -= jiffies % delay;
  148. return delay;
  149. }
  150. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall);
  151. void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
  152. bool need_load_eval(struct cpu_dbs_common_info *cdbs,
  153. unsigned int sampling_rate);
  154. int cpufreq_governor_dbs(struct dbs_data *dbs_data,
  155. struct cpufreq_policy *policy, unsigned int event);
  156. #endif /* _CPUFREQ_GOVERNER_H */