Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * drivers/cpufreq/cpufreq_governor.h
0004  *
0005  * Header file for CPUFreq governors common code
0006  *
0007  * Copyright    (C) 2001 Russell King
0008  *      (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0009  *      (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
0010  *      (C) 2009 Alexander Clouter <alex@digriz.org.uk>
0011  *      (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
0012  */
0013 
0014 #ifndef _CPUFREQ_GOVERNOR_H
0015 #define _CPUFREQ_GOVERNOR_H
0016 
0017 #include <linux/atomic.h>
0018 #include <linux/irq_work.h>
0019 #include <linux/cpufreq.h>
0020 #include <linux/sched/cpufreq.h>
0021 #include <linux/kernel_stat.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 
0025 /* Ondemand Sampling types */
0026 enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
0027 
0028 /*
0029  * Abbreviations:
0030  * dbs: used as a shortform for demand based switching It helps to keep variable
0031  *  names smaller, simpler
0032  * cdbs: common dbs
0033  * od_*: On-demand governor
0034  * cs_*: Conservative governor
0035  */
0036 
0037 /* Governor demand based switching data (per-policy or global). */
0038 struct dbs_data {
0039     struct gov_attr_set attr_set;
0040     struct dbs_governor *gov;
0041     void *tuners;
0042     unsigned int ignore_nice_load;
0043     unsigned int sampling_rate;
0044     unsigned int sampling_down_factor;
0045     unsigned int up_threshold;
0046     unsigned int io_is_busy;
0047 };
0048 
0049 static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set)
0050 {
0051     return container_of(attr_set, struct dbs_data, attr_set);
0052 }
0053 
0054 #define gov_show_one(_gov, file_name)                   \
0055 static ssize_t file_name##_show                     \
0056 (struct gov_attr_set *attr_set, char *buf)              \
0057 {                                   \
0058     struct dbs_data *dbs_data = to_dbs_data(attr_set);      \
0059     struct _gov##_dbs_tuners *tuners = dbs_data->tuners;        \
0060     return sprintf(buf, "%u\n", tuners->file_name);         \
0061 }
0062 
0063 #define gov_show_one_common(file_name)                  \
0064 static ssize_t file_name##_show                     \
0065 (struct gov_attr_set *attr_set, char *buf)              \
0066 {                                   \
0067     struct dbs_data *dbs_data = to_dbs_data(attr_set);      \
0068     return sprintf(buf, "%u\n", dbs_data->file_name);       \
0069 }
0070 
0071 #define gov_attr_ro(_name)                      \
0072 static struct governor_attr _name = __ATTR_RO(_name)
0073 
0074 #define gov_attr_rw(_name)                      \
0075 static struct governor_attr _name = __ATTR_RW(_name)
0076 
0077 /* Common to all CPUs of a policy */
0078 struct policy_dbs_info {
0079     struct cpufreq_policy *policy;
0080     /*
0081      * Per policy mutex that serializes load evaluation from limit-change
0082      * and work-handler.
0083      */
0084     struct mutex update_mutex;
0085 
0086     u64 last_sample_time;
0087     s64 sample_delay_ns;
0088     atomic_t work_count;
0089     struct irq_work irq_work;
0090     struct work_struct work;
0091     /* dbs_data may be shared between multiple policy objects */
0092     struct dbs_data *dbs_data;
0093     struct list_head list;
0094     /* Multiplier for increasing sample delay temporarily. */
0095     unsigned int rate_mult;
0096     unsigned int idle_periods;  /* For conservative */
0097     /* Status indicators */
0098     bool is_shared;     /* This object is used by multiple CPUs */
0099     bool work_in_progress;  /* Work is being queued up or in progress */
0100 };
0101 
0102 static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs,
0103                        unsigned int delay_us)
0104 {
0105     policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC;
0106 }
0107 
0108 /* Per cpu structures */
0109 struct cpu_dbs_info {
0110     u64 prev_cpu_idle;
0111     u64 prev_update_time;
0112     u64 prev_cpu_nice;
0113     /*
0114      * Used to keep track of load in the previous interval. However, when
0115      * explicitly set to zero, it is used as a flag to ensure that we copy
0116      * the previous load to the current interval only once, upon the first
0117      * wake-up from idle.
0118      */
0119     unsigned int prev_load;
0120     struct update_util_data update_util;
0121     struct policy_dbs_info *policy_dbs;
0122 };
0123 
0124 /* Common Governor data across policies */
0125 struct dbs_governor {
0126     struct cpufreq_governor gov;
0127     struct kobj_type kobj_type;
0128 
0129     /*
0130      * Common data for platforms that don't set
0131      * CPUFREQ_HAVE_GOVERNOR_PER_POLICY
0132      */
0133     struct dbs_data *gdbs_data;
0134 
0135     unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy);
0136     struct policy_dbs_info *(*alloc)(void);
0137     void (*free)(struct policy_dbs_info *policy_dbs);
0138     int (*init)(struct dbs_data *dbs_data);
0139     void (*exit)(struct dbs_data *dbs_data);
0140     void (*start)(struct cpufreq_policy *policy);
0141 };
0142 
0143 static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy)
0144 {
0145     return container_of(policy->governor, struct dbs_governor, gov);
0146 }
0147 
0148 /* Governor callback routines */
0149 int cpufreq_dbs_governor_init(struct cpufreq_policy *policy);
0150 void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy);
0151 int cpufreq_dbs_governor_start(struct cpufreq_policy *policy);
0152 void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy);
0153 void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy);
0154 
0155 #define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_)            \
0156     {                               \
0157         .name = _name_,                     \
0158         .flags = CPUFREQ_GOV_DYNAMIC_SWITCHING,         \
0159         .owner = THIS_MODULE,                   \
0160         .init = cpufreq_dbs_governor_init,          \
0161         .exit = cpufreq_dbs_governor_exit,          \
0162         .start = cpufreq_dbs_governor_start,            \
0163         .stop = cpufreq_dbs_governor_stop,          \
0164         .limits = cpufreq_dbs_governor_limits,          \
0165     }
0166 
0167 /* Governor specific operations */
0168 struct od_ops {
0169     unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
0170             unsigned int freq_next, unsigned int relation);
0171 };
0172 
0173 unsigned int dbs_update(struct cpufreq_policy *policy);
0174 void od_register_powersave_bias_handler(unsigned int (*f)
0175         (struct cpufreq_policy *, unsigned int, unsigned int),
0176         unsigned int powersave_bias);
0177 void od_unregister_powersave_bias_handler(void);
0178 ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf,
0179                 size_t count);
0180 void gov_update_cpu_data(struct dbs_data *dbs_data);
0181 #endif /* _CPUFREQ_GOVERNOR_H */