cpufreq_governor.h 5.6 KB

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