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 202dd3e724SRafael J. Wysocki #include <linux/atomic.h> 219be4fd2cSRafael J. Wysocki #include <linux/irq_work.h> 224471a34fSViresh Kumar #include <linux/cpufreq.h> 235ff0a268SViresh Kumar #include <linux/kernel_stat.h> 245ff0a268SViresh Kumar #include <linux/module.h> 254471a34fSViresh Kumar #include <linux/mutex.h> 264471a34fSViresh Kumar 274471a34fSViresh Kumar /* 284471a34fSViresh Kumar * The polling frequency depends on the capability of the processor. Default 294471a34fSViresh Kumar * polling frequency is 1000 times the transition latency of the processor. The 30c4afc410SStratos Karafotis * governor will work on any processor with transition latency <= 10ms, using 314471a34fSViresh Kumar * appropriate sampling rate. 324471a34fSViresh Kumar * 33c4afc410SStratos Karafotis * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL) 34c4afc410SStratos Karafotis * this governor will not work. All times here are in us (micro seconds). 354471a34fSViresh Kumar */ 364471a34fSViresh Kumar #define MIN_SAMPLING_RATE_RATIO (2) 374471a34fSViresh Kumar #define LATENCY_MULTIPLIER (1000) 3898104ee2SViresh Kumar #define MIN_LATENCY_MULTIPLIER (20) 394471a34fSViresh Kumar #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) 404471a34fSViresh Kumar 414471a34fSViresh Kumar /* Ondemand Sampling types */ 424471a34fSViresh Kumar enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE}; 434471a34fSViresh Kumar 444d5dcc42SViresh Kumar /* create helper routines */ 454471a34fSViresh Kumar #define define_get_cpu_dbs_routines(_dbs_info) \ 46875b8508SViresh Kumar static struct cpu_dbs_info *get_cpu_cdbs(int cpu) \ 474471a34fSViresh Kumar { \ 484471a34fSViresh Kumar return &per_cpu(_dbs_info, cpu).cdbs; \ 494471a34fSViresh Kumar } 504471a34fSViresh Kumar 514471a34fSViresh Kumar /* 524471a34fSViresh Kumar * Abbreviations: 534471a34fSViresh Kumar * dbs: used as a shortform for demand based switching It helps to keep variable 544471a34fSViresh Kumar * names smaller, simpler 554471a34fSViresh Kumar * cdbs: common dbs 56e5dde92cSNamhyung Kim * od_*: On-demand governor 574471a34fSViresh Kumar * cs_*: Conservative governor 584471a34fSViresh Kumar */ 594471a34fSViresh Kumar 60bc505475SRafael J. Wysocki /* Governor demand based switching data (per-policy or global). */ 61bc505475SRafael J. Wysocki struct dbs_data { 62bc505475SRafael J. Wysocki int usage_count; 63bc505475SRafael J. Wysocki void *tuners; 64ff4b1789SViresh Kumar unsigned int min_sampling_rate; 65ff4b1789SViresh Kumar unsigned int ignore_nice_load; 66ff4b1789SViresh Kumar unsigned int sampling_rate; 67ff4b1789SViresh Kumar unsigned int sampling_down_factor; 68ff4b1789SViresh Kumar unsigned int up_threshold; 698847e038SRafael J. Wysocki unsigned int io_is_busy; 70c4435630SViresh Kumar 71c4435630SViresh Kumar struct kobject kobj; 72c54df071SViresh Kumar struct list_head policy_dbs_list; 73c54df071SViresh Kumar /* 74c54df071SViresh Kumar * Protect concurrent updates to governor tunables from sysfs, 75c54df071SViresh Kumar * policy_dbs_list and usage_count. 76c54df071SViresh Kumar */ 77c4435630SViresh Kumar struct mutex mutex; 78bc505475SRafael J. Wysocki }; 79bc505475SRafael J. Wysocki 80c4435630SViresh Kumar /* Governor's specific attributes */ 81c4435630SViresh Kumar struct dbs_data; 82c4435630SViresh Kumar struct governor_attr { 83c4435630SViresh Kumar struct attribute attr; 84c4435630SViresh Kumar ssize_t (*show)(struct dbs_data *dbs_data, char *buf); 85c4435630SViresh Kumar ssize_t (*store)(struct dbs_data *dbs_data, const char *buf, 86c4435630SViresh Kumar size_t count); 87c4435630SViresh Kumar }; 88c4435630SViresh Kumar 89c4435630SViresh Kumar #define gov_show_one(_gov, file_name) \ 90c4435630SViresh Kumar static ssize_t show_##file_name \ 91c4435630SViresh Kumar (struct dbs_data *dbs_data, char *buf) \ 92c4435630SViresh Kumar { \ 93c4435630SViresh Kumar struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \ 94c4435630SViresh Kumar return sprintf(buf, "%u\n", tuners->file_name); \ 95c4435630SViresh Kumar } 96c4435630SViresh Kumar 97c4435630SViresh Kumar #define gov_show_one_common(file_name) \ 98c4435630SViresh Kumar static ssize_t show_##file_name \ 99c4435630SViresh Kumar (struct dbs_data *dbs_data, char *buf) \ 100c4435630SViresh Kumar { \ 101c4435630SViresh Kumar return sprintf(buf, "%u\n", dbs_data->file_name); \ 102c4435630SViresh Kumar } 103c4435630SViresh Kumar 104c4435630SViresh Kumar #define gov_attr_ro(_name) \ 105c4435630SViresh Kumar static struct governor_attr _name = \ 106c4435630SViresh Kumar __ATTR(_name, 0444, show_##_name, NULL) 107c4435630SViresh Kumar 108c4435630SViresh Kumar #define gov_attr_rw(_name) \ 109c4435630SViresh Kumar static struct governor_attr _name = \ 110c4435630SViresh Kumar __ATTR(_name, 0644, show_##_name, store_##_name) 111c4435630SViresh Kumar 11244152cb8SViresh Kumar /* Common to all CPUs of a policy */ 113e40e7b25SRafael J. Wysocki struct policy_dbs_info { 11444152cb8SViresh Kumar struct cpufreq_policy *policy; 11544152cb8SViresh Kumar /* 11670f43e5eSViresh Kumar * Per policy mutex that serializes load evaluation from limit-change 11770f43e5eSViresh Kumar * and work-handler. 11844152cb8SViresh Kumar */ 11944152cb8SViresh Kumar struct mutex timer_mutex; 12070f43e5eSViresh Kumar 1219be4fd2cSRafael J. Wysocki u64 last_sample_time; 1229be4fd2cSRafael J. Wysocki s64 sample_delay_ns; 123686cc637SRafael J. Wysocki atomic_t work_count; 1249be4fd2cSRafael J. Wysocki struct irq_work irq_work; 12570f43e5eSViresh Kumar struct work_struct work; 126bc505475SRafael J. Wysocki /* dbs_data may be shared between multiple policy objects */ 127bc505475SRafael J. Wysocki struct dbs_data *dbs_data; 128c54df071SViresh Kumar struct list_head list; 12957dc3bcdSRafael J. Wysocki /* Multiplier for increasing sample delay temporarily. */ 13057dc3bcdSRafael J. Wysocki unsigned int rate_mult; 131e4db2813SRafael J. Wysocki /* Status indicators */ 132e4db2813SRafael J. Wysocki bool is_shared; /* This object is used by multiple CPUs */ 133e4db2813SRafael J. Wysocki bool work_in_progress; /* Work is being queued up or in progress */ 13444152cb8SViresh Kumar }; 13544152cb8SViresh Kumar 136e40e7b25SRafael J. Wysocki static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs, 1379be4fd2cSRafael J. Wysocki unsigned int delay_us) 1389be4fd2cSRafael J. Wysocki { 139e40e7b25SRafael J. Wysocki policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC; 1409be4fd2cSRafael J. Wysocki } 1419be4fd2cSRafael J. Wysocki 1424471a34fSViresh Kumar /* Per cpu structures */ 143875b8508SViresh Kumar struct cpu_dbs_info { 1441e7586a1SViresh Kumar u64 prev_cpu_idle; 1451e7586a1SViresh Kumar u64 prev_cpu_wall; 1461e7586a1SViresh Kumar u64 prev_cpu_nice; 14718b46abdSSrivatsa S. Bhat /* 148c8ae481bSViresh Kumar * Used to keep track of load in the previous interval. However, when 149c8ae481bSViresh Kumar * explicitly set to zero, it is used as a flag to ensure that we copy 150c8ae481bSViresh Kumar * the previous load to the current interval only once, upon the first 151c8ae481bSViresh Kumar * wake-up from idle. 15218b46abdSSrivatsa S. Bhat */ 153c8ae481bSViresh Kumar unsigned int prev_load; 1549be4fd2cSRafael J. Wysocki struct update_util_data update_util; 155e40e7b25SRafael J. Wysocki struct policy_dbs_info *policy_dbs; 1564471a34fSViresh Kumar }; 1574471a34fSViresh Kumar 1584471a34fSViresh Kumar struct od_cpu_dbs_info_s { 159875b8508SViresh Kumar struct cpu_dbs_info cdbs; 1604471a34fSViresh Kumar }; 1614471a34fSViresh Kumar 1624471a34fSViresh Kumar struct cs_cpu_dbs_info_s { 163875b8508SViresh Kumar struct cpu_dbs_info cdbs; 1644471a34fSViresh Kumar }; 1654471a34fSViresh Kumar 166c4afc410SStratos Karafotis /* Per policy Governors sysfs tunables */ 1674471a34fSViresh Kumar struct od_dbs_tuners { 1684471a34fSViresh Kumar unsigned int powersave_bias; 1694471a34fSViresh Kumar }; 1704471a34fSViresh Kumar 1714471a34fSViresh Kumar struct cs_dbs_tuners { 1724471a34fSViresh Kumar unsigned int down_threshold; 1734471a34fSViresh Kumar unsigned int freq_step; 1744471a34fSViresh Kumar }; 1754471a34fSViresh Kumar 176c4afc410SStratos Karafotis /* Common Governor data across policies */ 1777bdad34dSRafael J. Wysocki struct dbs_governor { 178af926185SRafael J. Wysocki struct cpufreq_governor gov; 179c4435630SViresh Kumar struct kobj_type kobj_type; 1804471a34fSViresh Kumar 1810b981e70SViresh Kumar /* 1820b981e70SViresh Kumar * Common data for platforms that don't set 1830b981e70SViresh Kumar * CPUFREQ_HAVE_GOVERNOR_PER_POLICY 1840b981e70SViresh Kumar */ 1854d5dcc42SViresh Kumar struct dbs_data *gdbs_data; 1864471a34fSViresh Kumar 187875b8508SViresh Kumar struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu); 1889be4fd2cSRafael J. Wysocki unsigned int (*gov_dbs_timer)(struct cpufreq_policy *policy); 1897d5a9956SRafael J. Wysocki struct policy_dbs_info *(*alloc)(void); 1907d5a9956SRafael J. Wysocki void (*free)(struct policy_dbs_info *policy_dbs); 1918e0484d2SViresh Kumar int (*init)(struct dbs_data *dbs_data, bool notify); 1928e0484d2SViresh Kumar void (*exit)(struct dbs_data *dbs_data, bool notify); 193702c9e54SRafael J. Wysocki void (*start)(struct cpufreq_policy *policy); 1944471a34fSViresh Kumar }; 1954471a34fSViresh Kumar 196ea59ee0dSRafael J. Wysocki static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy) 197ea59ee0dSRafael J. Wysocki { 198ea59ee0dSRafael J. Wysocki return container_of(policy->governor, struct dbs_governor, gov); 199ea59ee0dSRafael J. Wysocki } 200ea59ee0dSRafael J. Wysocki 2018434dadbSRafael J. Wysocki /* Governor specific operations */ 2024471a34fSViresh Kumar struct od_ops { 2034471a34fSViresh Kumar unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy, 2044471a34fSViresh Kumar unsigned int freq_next, unsigned int relation); 2054471a34fSViresh Kumar }; 2064471a34fSViresh Kumar 2072bb8d94fSRafael J. Wysocki extern struct mutex dbs_data_mutex; 2084cccf755SRafael J. Wysocki unsigned int dbs_update(struct cpufreq_policy *policy); 209906a6e5aSRafael J. Wysocki int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event); 210fb30809eSJacob Shin void od_register_powersave_bias_handler(unsigned int (*f) 211fb30809eSJacob Shin (struct cpufreq_policy *, unsigned int, unsigned int), 212fb30809eSJacob Shin unsigned int powersave_bias); 213fb30809eSJacob Shin void od_unregister_powersave_bias_handler(void); 214aded387bSViresh Kumar ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, 215aded387bSViresh Kumar size_t count); 216a33cce1cSRafael J. Wysocki void gov_update_cpu_data(struct dbs_governor *gov, struct dbs_data *dbs_data); 217beb0ff39SBorislav Petkov #endif /* _CPUFREQ_GOVERNOR_H */ 218