14471a34fSViresh Kumar /* 24471a34fSViresh Kumar * drivers/cpufreq/cpufreq_governor.h 34471a34fSViresh Kumar * 44471a34fSViresh Kumar * Header file for CPUFreq governors common code 54471a34fSViresh Kumar * 64471a34fSViresh Kumar * Copyright (C) 2001 Russell King 74471a34fSViresh Kumar * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. 84471a34fSViresh Kumar * (C) 2003 Jun Nakajima <jun.nakajima@intel.com> 94471a34fSViresh Kumar * (C) 2009 Alexander Clouter <alex@digriz.org.uk> 104471a34fSViresh Kumar * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org> 114471a34fSViresh Kumar * 124471a34fSViresh Kumar * This program is free software; you can redistribute it and/or modify 134471a34fSViresh Kumar * it under the terms of the GNU General Public License version 2 as 144471a34fSViresh Kumar * published by the Free Software Foundation. 154471a34fSViresh Kumar */ 164471a34fSViresh Kumar 17beb0ff39SBorislav Petkov #ifndef _CPUFREQ_GOVERNOR_H 18beb0ff39SBorislav Petkov #define _CPUFREQ_GOVERNOR_H 194471a34fSViresh Kumar 204471a34fSViresh Kumar #include <linux/cpufreq.h> 215ff0a268SViresh Kumar #include <linux/kernel_stat.h> 225ff0a268SViresh Kumar #include <linux/module.h> 234471a34fSViresh Kumar #include <linux/mutex.h> 244471a34fSViresh Kumar 254471a34fSViresh Kumar /* 264471a34fSViresh Kumar * The polling frequency depends on the capability of the processor. Default 274471a34fSViresh Kumar * polling frequency is 1000 times the transition latency of the processor. The 28c4afc410SStratos Karafotis * governor will work on any processor with transition latency <= 10ms, using 294471a34fSViresh Kumar * appropriate sampling rate. 304471a34fSViresh Kumar * 31c4afc410SStratos Karafotis * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL) 32c4afc410SStratos Karafotis * this governor will not work. All times here are in us (micro seconds). 334471a34fSViresh Kumar */ 344471a34fSViresh Kumar #define MIN_SAMPLING_RATE_RATIO (2) 354471a34fSViresh Kumar #define LATENCY_MULTIPLIER (1000) 3698104ee2SViresh Kumar #define MIN_LATENCY_MULTIPLIER (20) 374471a34fSViresh Kumar #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) 384471a34fSViresh Kumar 394471a34fSViresh Kumar /* Ondemand Sampling types */ 404471a34fSViresh Kumar enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE}; 414471a34fSViresh Kumar 424d5dcc42SViresh Kumar /* 434d5dcc42SViresh Kumar * Macro for creating governors sysfs routines 444d5dcc42SViresh Kumar * 454d5dcc42SViresh Kumar * - gov_sys: One governor instance per whole system 464d5dcc42SViresh Kumar * - gov_pol: One governor instance per policy 474d5dcc42SViresh Kumar */ 484d5dcc42SViresh Kumar 494d5dcc42SViresh Kumar /* Create attributes */ 504d5dcc42SViresh Kumar #define gov_sys_attr_ro(_name) \ 514d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys = \ 524d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_sys, NULL) 534d5dcc42SViresh Kumar 544d5dcc42SViresh Kumar #define gov_sys_attr_rw(_name) \ 554d5dcc42SViresh Kumar static struct global_attr _name##_gov_sys = \ 564d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys) 574d5dcc42SViresh Kumar 584d5dcc42SViresh Kumar #define gov_pol_attr_ro(_name) \ 594d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol = \ 604d5dcc42SViresh Kumar __ATTR(_name, 0444, show_##_name##_gov_pol, NULL) 614d5dcc42SViresh Kumar 624d5dcc42SViresh Kumar #define gov_pol_attr_rw(_name) \ 634d5dcc42SViresh Kumar static struct freq_attr _name##_gov_pol = \ 644d5dcc42SViresh Kumar __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol) 654d5dcc42SViresh Kumar 664d5dcc42SViresh Kumar #define gov_sys_pol_attr_rw(_name) \ 674d5dcc42SViresh Kumar gov_sys_attr_rw(_name); \ 684d5dcc42SViresh Kumar gov_pol_attr_rw(_name) 694d5dcc42SViresh Kumar 704d5dcc42SViresh Kumar #define gov_sys_pol_attr_ro(_name) \ 714d5dcc42SViresh Kumar gov_sys_attr_ro(_name); \ 724d5dcc42SViresh Kumar gov_pol_attr_ro(_name) 734d5dcc42SViresh Kumar 744d5dcc42SViresh Kumar /* Create show/store routines */ 754d5dcc42SViresh Kumar #define show_one(_gov, file_name) \ 764d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_sys \ 774471a34fSViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf) \ 784471a34fSViresh Kumar { \ 794d5dcc42SViresh Kumar struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \ 804d5dcc42SViresh Kumar return sprintf(buf, "%u\n", tuners->file_name); \ 814d5dcc42SViresh Kumar } \ 824d5dcc42SViresh Kumar \ 834d5dcc42SViresh Kumar static ssize_t show_##file_name##_gov_pol \ 844d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf) \ 854d5dcc42SViresh Kumar { \ 864d5dcc42SViresh Kumar struct dbs_data *dbs_data = policy->governor_data; \ 874d5dcc42SViresh Kumar struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \ 884d5dcc42SViresh Kumar return sprintf(buf, "%u\n", tuners->file_name); \ 894471a34fSViresh Kumar } 904471a34fSViresh Kumar 914d5dcc42SViresh Kumar #define store_one(_gov, file_name) \ 924d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_sys \ 934d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \ 944d5dcc42SViresh Kumar { \ 954d5dcc42SViresh Kumar struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \ 964d5dcc42SViresh Kumar return store_##file_name(dbs_data, buf, count); \ 974d5dcc42SViresh Kumar } \ 984d5dcc42SViresh Kumar \ 994d5dcc42SViresh Kumar static ssize_t store_##file_name##_gov_pol \ 1004d5dcc42SViresh Kumar (struct cpufreq_policy *policy, const char *buf, size_t count) \ 1014d5dcc42SViresh Kumar { \ 1024d5dcc42SViresh Kumar struct dbs_data *dbs_data = policy->governor_data; \ 1034d5dcc42SViresh Kumar return store_##file_name(dbs_data, buf, count); \ 1044d5dcc42SViresh Kumar } 1054d5dcc42SViresh Kumar 1064d5dcc42SViresh Kumar #define show_store_one(_gov, file_name) \ 1074d5dcc42SViresh Kumar show_one(_gov, file_name); \ 1084d5dcc42SViresh Kumar store_one(_gov, file_name) 1094d5dcc42SViresh Kumar 1104d5dcc42SViresh Kumar /* create helper routines */ 1114471a34fSViresh Kumar #define define_get_cpu_dbs_routines(_dbs_info) \ 1124471a34fSViresh Kumar static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu) \ 1134471a34fSViresh Kumar { \ 1144471a34fSViresh Kumar return &per_cpu(_dbs_info, cpu).cdbs; \ 1154471a34fSViresh Kumar } \ 1164471a34fSViresh Kumar \ 1174471a34fSViresh Kumar static void *get_cpu_dbs_info_s(int cpu) \ 1184471a34fSViresh Kumar { \ 1194471a34fSViresh Kumar return &per_cpu(_dbs_info, cpu); \ 1204471a34fSViresh Kumar } 1214471a34fSViresh Kumar 1224471a34fSViresh Kumar /* 1234471a34fSViresh Kumar * Abbreviations: 1244471a34fSViresh Kumar * dbs: used as a shortform for demand based switching It helps to keep variable 1254471a34fSViresh Kumar * names smaller, simpler 1264471a34fSViresh Kumar * cdbs: common dbs 127e5dde92cSNamhyung Kim * od_*: On-demand governor 1284471a34fSViresh Kumar * cs_*: Conservative governor 1294471a34fSViresh Kumar */ 1304471a34fSViresh Kumar 1314471a34fSViresh Kumar /* Per cpu structures */ 1324471a34fSViresh Kumar struct cpu_dbs_common_info { 1334471a34fSViresh Kumar int cpu; 1341e7586a1SViresh Kumar u64 prev_cpu_idle; 1351e7586a1SViresh Kumar u64 prev_cpu_wall; 1361e7586a1SViresh Kumar u64 prev_cpu_nice; 13718b46abdSSrivatsa S. Bhat /* 138c8ae481bSViresh Kumar * Used to keep track of load in the previous interval. However, when 139c8ae481bSViresh Kumar * explicitly set to zero, it is used as a flag to ensure that we copy 140c8ae481bSViresh Kumar * the previous load to the current interval only once, upon the first 141c8ae481bSViresh Kumar * wake-up from idle. 14218b46abdSSrivatsa S. Bhat */ 143c8ae481bSViresh Kumar unsigned int prev_load; 1444471a34fSViresh Kumar struct cpufreq_policy *cur_policy; 145386d46e6SViresh Kumar struct delayed_work dwork; 1464471a34fSViresh Kumar /* 1474471a34fSViresh Kumar * percpu mutex that serializes governor limit change with gov_dbs_timer 1484471a34fSViresh Kumar * invocation. We do not want gov_dbs_timer to run when user is changing 1494471a34fSViresh Kumar * the governor or limits. 1504471a34fSViresh Kumar */ 1514471a34fSViresh Kumar struct mutex timer_mutex; 152da53d61eSFabio Baltieri ktime_t time_stamp; 1534471a34fSViresh Kumar }; 1544471a34fSViresh Kumar 1554471a34fSViresh Kumar struct od_cpu_dbs_info_s { 1564471a34fSViresh Kumar struct cpu_dbs_common_info cdbs; 1574471a34fSViresh Kumar struct cpufreq_frequency_table *freq_table; 1584471a34fSViresh Kumar unsigned int freq_lo; 1594471a34fSViresh Kumar unsigned int freq_lo_jiffies; 1604471a34fSViresh Kumar unsigned int freq_hi_jiffies; 1614471a34fSViresh Kumar unsigned int rate_mult; 1624471a34fSViresh Kumar unsigned int sample_type:1; 1634471a34fSViresh Kumar }; 1644471a34fSViresh Kumar 1654471a34fSViresh Kumar struct cs_cpu_dbs_info_s { 1664471a34fSViresh Kumar struct cpu_dbs_common_info cdbs; 1674471a34fSViresh Kumar unsigned int down_skip; 1684471a34fSViresh Kumar unsigned int requested_freq; 1694471a34fSViresh Kumar unsigned int enable:1; 1704471a34fSViresh Kumar }; 1714471a34fSViresh Kumar 172c4afc410SStratos Karafotis /* Per policy Governors sysfs tunables */ 1734471a34fSViresh Kumar struct od_dbs_tuners { 1746c4640c3SViresh Kumar unsigned int ignore_nice_load; 1754471a34fSViresh Kumar unsigned int sampling_rate; 1764471a34fSViresh Kumar unsigned int sampling_down_factor; 1774471a34fSViresh Kumar unsigned int up_threshold; 1784471a34fSViresh Kumar unsigned int powersave_bias; 1794471a34fSViresh Kumar unsigned int io_is_busy; 1804471a34fSViresh Kumar }; 1814471a34fSViresh Kumar 1824471a34fSViresh Kumar struct cs_dbs_tuners { 1836c4640c3SViresh Kumar unsigned int ignore_nice_load; 1844471a34fSViresh Kumar unsigned int sampling_rate; 1854471a34fSViresh Kumar unsigned int sampling_down_factor; 1864471a34fSViresh Kumar unsigned int up_threshold; 1874471a34fSViresh Kumar unsigned int down_threshold; 1884471a34fSViresh Kumar unsigned int freq_step; 1894471a34fSViresh Kumar }; 1904471a34fSViresh Kumar 191c4afc410SStratos Karafotis /* Common Governor data across policies */ 1924d5dcc42SViresh Kumar struct dbs_data; 1934d5dcc42SViresh Kumar struct common_dbs_data { 1944471a34fSViresh Kumar /* Common across governors */ 1954471a34fSViresh Kumar #define GOV_ONDEMAND 0 1964471a34fSViresh Kumar #define GOV_CONSERVATIVE 1 1974471a34fSViresh Kumar int governor; 1984d5dcc42SViresh Kumar struct attribute_group *attr_group_gov_sys; /* one governor - system */ 1994d5dcc42SViresh Kumar struct attribute_group *attr_group_gov_pol; /* one governor - policy */ 2004471a34fSViresh Kumar 2010b981e70SViresh Kumar /* 2020b981e70SViresh Kumar * Common data for platforms that don't set 2030b981e70SViresh Kumar * CPUFREQ_HAVE_GOVERNOR_PER_POLICY 2040b981e70SViresh Kumar */ 2054d5dcc42SViresh Kumar struct dbs_data *gdbs_data; 2064471a34fSViresh Kumar 2074471a34fSViresh Kumar struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu); 2084471a34fSViresh Kumar void *(*get_cpu_dbs_info_s)(int cpu); 2094471a34fSViresh Kumar void (*gov_dbs_timer)(struct work_struct *work); 2104471a34fSViresh Kumar void (*gov_check_cpu)(int cpu, unsigned int load); 2118e0484d2SViresh Kumar int (*init)(struct dbs_data *dbs_data, bool notify); 2128e0484d2SViresh Kumar void (*exit)(struct dbs_data *dbs_data, bool notify); 2134471a34fSViresh Kumar 2144471a34fSViresh Kumar /* Governor specific ops, see below */ 2154471a34fSViresh Kumar void *gov_ops; 216732b6d61SViresh Kumar 217732b6d61SViresh Kumar /* 218732b6d61SViresh Kumar * Protects governor's data (struct dbs_data and struct common_dbs_data) 219732b6d61SViresh Kumar */ 220732b6d61SViresh Kumar struct mutex mutex; 2214471a34fSViresh Kumar }; 2224471a34fSViresh Kumar 223c4afc410SStratos Karafotis /* Governor Per policy data */ 2244d5dcc42SViresh Kumar struct dbs_data { 2254d5dcc42SViresh Kumar struct common_dbs_data *cdata; 2264d5dcc42SViresh Kumar unsigned int min_sampling_rate; 227a97c98adSViresh Kumar int usage_count; 2284d5dcc42SViresh Kumar void *tuners; 2294d5dcc42SViresh Kumar }; 2304d5dcc42SViresh Kumar 2314471a34fSViresh Kumar /* Governor specific ops, will be passed to dbs_data->gov_ops */ 2324471a34fSViresh Kumar struct od_ops { 2334471a34fSViresh Kumar void (*powersave_bias_init_cpu)(int cpu); 2344471a34fSViresh Kumar unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy, 2354471a34fSViresh Kumar unsigned int freq_next, unsigned int relation); 2363a3e9e06SViresh Kumar void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq); 2374471a34fSViresh Kumar }; 2384471a34fSViresh Kumar 2394471a34fSViresh Kumar static inline int delay_for_sampling_rate(unsigned int sampling_rate) 2404471a34fSViresh Kumar { 2414471a34fSViresh Kumar int delay = usecs_to_jiffies(sampling_rate); 2424471a34fSViresh Kumar 2434471a34fSViresh Kumar /* We want all CPUs to do sampling nearly on same jiffy */ 2444471a34fSViresh Kumar if (num_online_cpus() > 1) 2454471a34fSViresh Kumar delay -= jiffies % delay; 2464471a34fSViresh Kumar 2474471a34fSViresh Kumar return delay; 2484471a34fSViresh Kumar } 2494471a34fSViresh Kumar 2504d5dcc42SViresh Kumar #define declare_show_sampling_rate_min(_gov) \ 2514d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_sys \ 2524d5dcc42SViresh Kumar (struct kobject *kobj, struct attribute *attr, char *buf) \ 2534d5dcc42SViresh Kumar { \ 2544d5dcc42SViresh Kumar struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \ 2554d5dcc42SViresh Kumar return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ 2564d5dcc42SViresh Kumar } \ 2574d5dcc42SViresh Kumar \ 2584d5dcc42SViresh Kumar static ssize_t show_sampling_rate_min_gov_pol \ 2594d5dcc42SViresh Kumar (struct cpufreq_policy *policy, char *buf) \ 2604d5dcc42SViresh Kumar { \ 2614d5dcc42SViresh Kumar struct dbs_data *dbs_data = policy->governor_data; \ 2624d5dcc42SViresh Kumar return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ 2634d5dcc42SViresh Kumar } 2644d5dcc42SViresh Kumar 2656f1e4efdSJane Li extern struct mutex cpufreq_governor_lock; 2666f1e4efdSJane Li 2674471a34fSViresh Kumar void dbs_check_cpu(struct dbs_data *dbs_data, int cpu); 2684447266bSViresh Kumar bool need_load_eval(struct cpu_dbs_common_info *cdbs, 2694447266bSViresh Kumar unsigned int sampling_rate); 2704d5dcc42SViresh Kumar int cpufreq_governor_dbs(struct cpufreq_policy *policy, 2714d5dcc42SViresh Kumar struct common_dbs_data *cdata, unsigned int event); 272031299b3SViresh Kumar void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy, 273031299b3SViresh Kumar unsigned int delay, bool all_cpus); 274fb30809eSJacob Shin void od_register_powersave_bias_handler(unsigned int (*f) 275fb30809eSJacob Shin (struct cpufreq_policy *, unsigned int, unsigned int), 276fb30809eSJacob Shin unsigned int powersave_bias); 277fb30809eSJacob Shin void od_unregister_powersave_bias_handler(void); 278beb0ff39SBorislav Petkov #endif /* _CPUFREQ_GOVERNOR_H */ 279