Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * drivers/cpufreq/cpufreq_governor.h |
| 4 | * |
| 5 | * Header file for CPUFreq governors common code |
| 6 | * |
| 7 | * Copyright (C) 2001 Russell King |
| 8 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. |
| 9 | * (C) 2003 Jun Nakajima <jun.nakajima@intel.com> |
| 10 | * (C) 2009 Alexander Clouter <alex@digriz.org.uk> |
| 11 | * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
Borislav Petkov | beb0ff3 | 2013-04-02 12:26:15 +0000 | [diff] [blame] | 14 | #ifndef _CPUFREQ_GOVERNOR_H |
| 15 | #define _CPUFREQ_GOVERNOR_H |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 16 | |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 18 | #include <linux/irq_work.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 19 | #include <linux/cpufreq.h> |
Ingo Molnar | 55687da | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 20 | #include <linux/sched/cpufreq.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 21 | #include <linux/kernel_stat.h> |
| 22 | #include <linux/module.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 24 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 25 | /* Ondemand Sampling types */ |
| 26 | enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE}; |
| 27 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 28 | /* |
| 29 | * Abbreviations: |
| 30 | * dbs: used as a shortform for demand based switching It helps to keep variable |
| 31 | * names smaller, simpler |
| 32 | * cdbs: common dbs |
Namhyung Kim | e5dde92 | 2013-02-28 05:38:00 +0000 | [diff] [blame] | 33 | * od_*: On-demand governor |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 34 | * cs_*: Conservative governor |
| 35 | */ |
| 36 | |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 37 | /* Governor demand based switching data (per-policy or global). */ |
| 38 | struct dbs_data { |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 39 | struct gov_attr_set attr_set; |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 40 | void *tuners; |
Viresh Kumar | ff4b178 | 2016-02-09 09:01:32 +0530 | [diff] [blame] | 41 | unsigned int ignore_nice_load; |
| 42 | unsigned int sampling_rate; |
| 43 | unsigned int sampling_down_factor; |
| 44 | unsigned int up_threshold; |
Rafael J. Wysocki | 8847e03 | 2016-02-18 02:20:13 +0100 | [diff] [blame] | 45 | unsigned int io_is_busy; |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 48 | static inline struct dbs_data *to_dbs_data(struct gov_attr_set *attr_set) |
| 49 | { |
| 50 | return container_of(attr_set, struct dbs_data, attr_set); |
| 51 | } |
| 52 | |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 53 | #define gov_show_one(_gov, file_name) \ |
| 54 | static ssize_t show_##file_name \ |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 55 | (struct gov_attr_set *attr_set, char *buf) \ |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 56 | { \ |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 57 | struct dbs_data *dbs_data = to_dbs_data(attr_set); \ |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 58 | struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \ |
| 59 | return sprintf(buf, "%u\n", tuners->file_name); \ |
| 60 | } |
| 61 | |
| 62 | #define gov_show_one_common(file_name) \ |
| 63 | static ssize_t show_##file_name \ |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 64 | (struct gov_attr_set *attr_set, char *buf) \ |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 65 | { \ |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 66 | struct dbs_data *dbs_data = to_dbs_data(attr_set); \ |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 67 | return sprintf(buf, "%u\n", dbs_data->file_name); \ |
| 68 | } |
| 69 | |
| 70 | #define gov_attr_ro(_name) \ |
| 71 | static struct governor_attr _name = \ |
| 72 | __ATTR(_name, 0444, show_##_name, NULL) |
| 73 | |
| 74 | #define gov_attr_rw(_name) \ |
| 75 | static struct governor_attr _name = \ |
| 76 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 77 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 78 | /* Common to all CPUs of a policy */ |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 79 | struct policy_dbs_info { |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 80 | struct cpufreq_policy *policy; |
| 81 | /* |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 82 | * Per policy mutex that serializes load evaluation from limit-change |
| 83 | * and work-handler. |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 84 | */ |
Viresh Kumar | 26f0dbc | 2016-11-08 11:06:33 +0530 | [diff] [blame] | 85 | struct mutex update_mutex; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 86 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 87 | u64 last_sample_time; |
| 88 | s64 sample_delay_ns; |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 89 | atomic_t work_count; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 90 | struct irq_work irq_work; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 91 | struct work_struct work; |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 92 | /* dbs_data may be shared between multiple policy objects */ |
| 93 | struct dbs_data *dbs_data; |
Viresh Kumar | c54df07 | 2016-02-10 11:00:25 +0530 | [diff] [blame] | 94 | struct list_head list; |
Rafael J. Wysocki | 57dc3bc | 2016-02-15 02:20:51 +0100 | [diff] [blame] | 95 | /* Multiplier for increasing sample delay temporarily. */ |
| 96 | unsigned int rate_mult; |
Stratos Karafotis | 00bfe05 | 2016-11-16 19:26:29 +0200 | [diff] [blame] | 97 | unsigned int idle_periods; /* For conservative */ |
Rafael J. Wysocki | e4db281 | 2016-02-15 02:13:42 +0100 | [diff] [blame] | 98 | /* Status indicators */ |
| 99 | bool is_shared; /* This object is used by multiple CPUs */ |
| 100 | bool work_in_progress; /* Work is being queued up or in progress */ |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 101 | }; |
| 102 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 103 | static inline void gov_update_sample_delay(struct policy_dbs_info *policy_dbs, |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 104 | unsigned int delay_us) |
| 105 | { |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 106 | policy_dbs->sample_delay_ns = delay_us * NSEC_PER_USEC; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 109 | /* Per cpu structures */ |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 110 | struct cpu_dbs_info { |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 111 | u64 prev_cpu_idle; |
Rafael J. Wysocki | b4f4b4b | 2016-04-28 01:19:03 +0200 | [diff] [blame] | 112 | u64 prev_update_time; |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 113 | u64 prev_cpu_nice; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 114 | /* |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 115 | * Used to keep track of load in the previous interval. However, when |
| 116 | * explicitly set to zero, it is used as a flag to ensure that we copy |
| 117 | * the previous load to the current interval only once, upon the first |
| 118 | * wake-up from idle. |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 119 | */ |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 120 | unsigned int prev_load; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 121 | struct update_util_data update_util; |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 122 | struct policy_dbs_info *policy_dbs; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 125 | /* Common Governor data across policies */ |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 126 | struct dbs_governor { |
Rafael J. Wysocki | af92618 | 2016-02-05 03:16:08 +0100 | [diff] [blame] | 127 | struct cpufreq_governor gov; |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame] | 128 | struct kobj_type kobj_type; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 129 | |
Viresh Kumar | 0b981e7 | 2013-10-02 14:13:18 +0530 | [diff] [blame] | 130 | /* |
| 131 | * Common data for platforms that don't set |
| 132 | * CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
| 133 | */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 134 | struct dbs_data *gdbs_data; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 135 | |
Viresh Kumar | 26f0dbc | 2016-11-08 11:06:33 +0530 | [diff] [blame] | 136 | unsigned int (*gov_dbs_update)(struct cpufreq_policy *policy); |
Rafael J. Wysocki | 7d5a995 | 2016-02-18 18:40:14 +0100 | [diff] [blame] | 137 | struct policy_dbs_info *(*alloc)(void); |
| 138 | void (*free)(struct policy_dbs_info *policy_dbs); |
Rafael J. Wysocki | 9a15fb2 | 2016-05-18 22:59:49 +0200 | [diff] [blame] | 139 | int (*init)(struct dbs_data *dbs_data); |
| 140 | void (*exit)(struct dbs_data *dbs_data); |
Rafael J. Wysocki | 702c9e5 | 2016-02-18 02:21:21 +0100 | [diff] [blame] | 141 | void (*start)(struct cpufreq_policy *policy); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 144 | static inline struct dbs_governor *dbs_governor_of(struct cpufreq_policy *policy) |
| 145 | { |
| 146 | return container_of(policy->governor, struct dbs_governor, gov); |
| 147 | } |
| 148 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 149 | /* Governor callback routines */ |
| 150 | int cpufreq_dbs_governor_init(struct cpufreq_policy *policy); |
| 151 | void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy); |
| 152 | int cpufreq_dbs_governor_start(struct cpufreq_policy *policy); |
| 153 | void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy); |
| 154 | void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy); |
| 155 | |
| 156 | #define CPUFREQ_DBS_GOVERNOR_INITIALIZER(_name_) \ |
| 157 | { \ |
| 158 | .name = _name_, \ |
Viresh Kumar | ed4676e | 2017-07-19 15:42:46 +0530 | [diff] [blame] | 159 | .dynamic_switching = true, \ |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 160 | .owner = THIS_MODULE, \ |
| 161 | .init = cpufreq_dbs_governor_init, \ |
| 162 | .exit = cpufreq_dbs_governor_exit, \ |
| 163 | .start = cpufreq_dbs_governor_start, \ |
| 164 | .stop = cpufreq_dbs_governor_stop, \ |
| 165 | .limits = cpufreq_dbs_governor_limits, \ |
| 166 | } |
| 167 | |
Rafael J. Wysocki | 8434dad | 2016-02-18 02:22:42 +0100 | [diff] [blame] | 168 | /* Governor specific operations */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 169 | struct od_ops { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 170 | unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy, |
| 171 | unsigned int freq_next, unsigned int relation); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Rafael J. Wysocki | 4cccf75 | 2016-02-15 02:19:31 +0100 | [diff] [blame] | 174 | unsigned int dbs_update(struct cpufreq_policy *policy); |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 175 | void od_register_powersave_bias_handler(unsigned int (*f) |
| 176 | (struct cpufreq_policy *, unsigned int, unsigned int), |
| 177 | unsigned int powersave_bias); |
| 178 | void od_unregister_powersave_bias_handler(void); |
Rafael J. Wysocki | 0dd3c1d | 2016-03-22 02:47:51 +0100 | [diff] [blame] | 179 | ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf, |
Viresh Kumar | aded387 | 2016-02-11 17:31:15 +0530 | [diff] [blame] | 180 | size_t count); |
Rafael J. Wysocki | 8c8f77f | 2016-02-21 00:51:27 +0100 | [diff] [blame] | 181 | void gov_update_cpu_data(struct dbs_data *dbs_data); |
Borislav Petkov | beb0ff3 | 2013-04-02 12:26:15 +0000 | [diff] [blame] | 182 | #endif /* _CPUFREQ_GOVERNOR_H */ |