cpufreq_governor.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/kernel_stat.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. /*
  25. * The polling frequency depends on the capability of the processor. Default
  26. * polling frequency is 1000 times the transition latency of the processor. The
  27. * governor will work on any processor with transition latency <= 10ms, using
  28. * appropriate sampling rate.
  29. *
  30. * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
  31. * this governor will not work. All times here are in us (micro seconds).
  32. */
  33. #define MIN_SAMPLING_RATE_RATIO (2)
  34. #define LATENCY_MULTIPLIER (1000)
  35. #define MIN_LATENCY_MULTIPLIER (20)
  36. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  37. /* Ondemand Sampling types */
  38. enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
  39. /*
  40. * Abbreviations:
  41. * dbs: used as a shortform for demand based switching It helps to keep variable
  42. * names smaller, simpler
  43. * cdbs: common dbs
  44. * od_*: On-demand governor
  45. * cs_*: Conservative governor
  46. */
  47. /* Governor demand based switching data (per-policy or global). */
  48. struct dbs_data {
  49. int usage_count;
  50. void *tuners;
  51. unsigned int min_sampling_rate;
  52. unsigned int ignore_nice_load;
  53. unsigned int sampling_rate;
  54. unsigned int sampling_down_factor;
  55. unsigned int up_threshold;
  56. unsigned int io_is_busy;
  57. struct kobject kobj;
  58. struct list_head policy_dbs_list;
  59. /*
  60. * Protect concurrent updates to governor tunables from sysfs,
  61. * policy_dbs_list and usage_count.
  62. */
  63. struct mutex mutex;
  64. };
  65. /* Governor's specific attributes */
  66. struct dbs_data;
  67. struct governor_attr {
  68. struct attribute attr;
  69. ssize_t (*show)(struct dbs_data *dbs_data, char *buf);
  70. ssize_t (*store)(struct dbs_data *dbs_data, const char *buf,
  71. size_t count);
  72. };
  73. #define gov_show_one(_gov, file_name) \
  74. static ssize_t show_##file_name \
  75. (struct dbs_data *dbs_data, char *buf) \
  76. { \
  77. struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
  78. return sprintf(buf, "%u\n", tuners->file_name); \
  79. }
  80. #define gov_show_one_common(file_name) \
  81. static ssize_t show_##file_name \
  82. (struct dbs_data *dbs_data, char *buf) \
  83. { \
  84. return sprintf(buf, "%u\n", dbs_data->file_name); \
  85. }
  86. #define gov_attr_ro(_name) \
  87. static struct governor_attr _name = \
  88. __ATTR(_name, 0444, show_##_name, NULL)
  89. #define gov_attr_rw(_name) \
  90. static struct governor_attr _name = \
  91. __ATTR(_name, 0644, show_##_name, store_##_name)
  92. /* Common to all CPUs of a policy */
  93. struct policy_dbs_info {
  94. struct cpufreq_policy *policy;
  95. /*
  96. * Per policy mutex that serializes load evaluation from limit-change
  97. * and work-handler.
  98. */
  99. struct mutex timer_mutex;
  100. u64 last_sample_time;
  101. s64 sample_delay_ns;
  102. atomic_t work_count;
  103. struct irq_work irq_work;
  104. struct work_struct work;
  105. /* dbs_data may be shared between multiple policy objects */
  106. struct dbs_data *dbs_data;
  107. struct list_head list;
  108. /* Multiplier for increasing sample delay temporarily. */
  109. unsigned int rate_mult;
  110. /* Status indicators */
  111. bool is_shared; /* This object is used by multiple CPUs */
  112. bool work_in_progress; /* Work is being queued up or in progress */
  113. };
  114. static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
  115. unsigned int delay_us)
  116. {
  117. policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
  118. }
  119. /* Per cpu structures */
  120. struct cpu_dbs_info {
  121. u64 prev_cpu_idle;
  122. u64 prev_cpu_wall;
  123. u64 prev_cpu_nice;
  124. /*
  125. * Used to keep track of load in the previous interval. However, when
  126. * explicitly set to zero, it is used as a flag to ensure that we copy
  127. * the previous load to the current interval only once, upon the first
  128. * wake-up from idle.
  129. */
  130. unsigned int prev_load;
  131. struct update_util_data update_util;
  132. struct policy_dbs_info *policy_dbs;
  133. };
  134. /* Common Governor data across policies */
  135. struct dbs_governor {
  136. struct cpufreq_governor gov;
  137. struct kobj_type kobj_type;
  138. /*
  139. * Common data for platforms that don't set
  140. * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
  141. */
  142. struct dbs_data *gdbs_data;
  143. unsigned int (*gov_dbs_timer)(struct cpufreq_policy *policy);
  144. struct policy_dbs_info *(*alloc)(void);
  145. void (*free)(struct policy_dbs_info *policy_dbs);
  146. int (*init)(struct dbs_data *dbs_data, bool notify);
  147. void (*exit)(struct dbs_data *dbs_data, bool notify);
  148. void (*start)(struct cpufreq_policy *policy);
  149. };
  150. static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
  151. {
  152. return container_of(policy->governor, struct dbs_governor, gov);
  153. }
  154. /* Governor specific operations */
  155. struct od_ops {
  156. unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
  157. unsigned int freq_next, unsigned int relation);
  158. };
  159. unsigned int dbs_update(struct cpufreq_policy *policy);
  160. int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event);
  161. void od_register_powersave_bias_handler(unsigned int (*f)
  162. (struct cpufreq_policy *, unsigned int, unsigned int),
  163. unsigned int powersave_bias);
  164. void od_unregister_powersave_bias_handler(void);
  165. ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
  166. size_t count);
  167. void gov_update_cpu_data(struct dbs_data *dbs_data);
  168. #endif /* _CPUFREQ_GOVERNOR_H */