Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | CPU frequency and voltage scaling code in the Linux(TM) kernel |
| 2 | |
| 3 | |
| 4 | L i n u x C P U F r e q |
| 5 | |
| 6 | C P U D r i v e r s |
| 7 | |
| 8 | - information for developers - |
| 9 | |
| 10 | |
| 11 | Dominik Brodowski <linux@brodo.de> |
| 12 | |
| 13 | |
| 14 | |
| 15 | Clock scaling allows you to change the clock speed of the CPUs on the |
| 16 | fly. This is a nice method to save battery power, because the lower |
| 17 | the clock speed, the less power the CPU consumes. |
| 18 | |
| 19 | |
| 20 | Contents: |
| 21 | --------- |
| 22 | 1. What To Do? |
| 23 | 1.1 Initialization |
| 24 | 1.2 Per-CPU Initialization |
| 25 | 1.3 verify |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 26 | 1.4 target/target_index or setpolicy? |
| 27 | 1.5 target/target_index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 1.6 setpolicy |
| 29 | 2. Frequency Table Helpers |
| 30 | |
| 31 | |
| 32 | |
| 33 | 1. What To Do? |
| 34 | ============== |
| 35 | |
| 36 | So, you just got a brand-new CPU / chipset with datasheets and want to |
| 37 | add cpufreq support for this CPU / chipset? Great. Here are some hints |
| 38 | on what is necessary: |
| 39 | |
| 40 | |
| 41 | 1.1 Initialization |
| 42 | ------------------ |
| 43 | |
| 44 | First of all, in an __initcall level 7 (module_init()) or later |
| 45 | function check whether this kernel runs on the right CPU and the right |
| 46 | chipset. If so, register a struct cpufreq_driver with the CPUfreq core |
| 47 | using cpufreq_register_driver() |
| 48 | |
| 49 | What shall this struct cpufreq_driver contain? |
| 50 | |
| 51 | cpufreq_driver.name - The name of this driver. |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | cpufreq_driver.init - A pointer to the per-CPU initialization |
| 54 | function. |
| 55 | |
| 56 | cpufreq_driver.verify - A pointer to a "verification" function. |
| 57 | |
| 58 | cpufreq_driver.setpolicy _or_ |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 59 | cpufreq_driver.target/ |
| 60 | target_index - See below on the differences. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | And optionally |
| 63 | |
Dirk Brandewie | 367dc4a | 2014-03-19 08:45:53 -0700 | [diff] [blame] | 64 | cpufreq_driver.exit - A pointer to a per-CPU cleanup |
| 65 | function called during CPU_POST_DEAD |
| 66 | phase of cpu hotplug process. |
| 67 | |
| 68 | cpufreq_driver.stop_cpu - A pointer to a per-CPU stop function |
| 69 | called during CPU_DOWN_PREPARE phase of |
| 70 | cpu hotplug process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | cpufreq_driver.resume - A pointer to a per-CPU resume function |
| 73 | which is called with interrupts disabled |
| 74 | and _before_ the pre-suspend frequency |
| 75 | and/or policy is restored by a call to |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 76 | ->target/target_index or ->setpolicy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | cpufreq_driver.attr - A pointer to a NULL-terminated list of |
| 79 | "struct freq_attr" which allow to |
| 80 | export values to sysfs. |
| 81 | |
| 82 | |
| 83 | 1.2 Per-CPU Initialization |
| 84 | -------------------------- |
| 85 | |
| 86 | Whenever a new CPU is registered with the device model, or after the |
| 87 | cpufreq driver registers itself, the per-CPU initialization function |
| 88 | cpufreq_driver.init is called. It takes a struct cpufreq_policy |
| 89 | *policy as argument. What to do now? |
| 90 | |
| 91 | If necessary, activate the CPUfreq support on your CPU. |
| 92 | |
| 93 | Then, the driver must fill in the following values: |
| 94 | |
| 95 | policy->cpuinfo.min_freq _and_ |
| 96 | policy->cpuinfo.max_freq - the minimum and maximum frequency |
| 97 | (in kHz) which is supported by |
| 98 | this CPU |
| 99 | policy->cpuinfo.transition_latency the time it takes on this CPU to |
Mark Brown | bbe237a | 2009-11-12 16:06:45 +0000 | [diff] [blame] | 100 | switch between two frequencies in |
| 101 | nanoseconds (if appropriate, else |
| 102 | specify CPUFREQ_ETERNAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | policy->cur The current operating frequency of |
| 105 | this CPU (if appropriate) |
| 106 | policy->min, |
| 107 | policy->max, |
| 108 | policy->policy and, if necessary, |
| 109 | policy->governor must contain the "default policy" for |
| 110 | this CPU. A few moments later, |
| 111 | cpufreq_driver.verify and either |
| 112 | cpufreq_driver.setpolicy or |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 113 | cpufreq_driver.target/target_index is called |
| 114 | with these values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Viresh Kumar | eb2f50f | 2013-04-01 12:57:48 +0000 | [diff] [blame] | 116 | For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the |
| 117 | frequency table helpers might be helpful. See the section 2 for more information |
| 118 | on them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Viresh Kumar | 951fc5f | 2013-01-31 02:03:53 +0000 | [diff] [blame] | 120 | SMP systems normally have same clock source for a group of cpus. For these the |
| 121 | .init() would be called only once for the first online cpu. Here the .init() |
| 122 | routine must initialize policy->cpus with mask of all possible cpus (Online + |
| 123 | Offline) that share the clock. Then the core would copy this mask onto |
| 124 | policy->related_cpus and will reset policy->cpus to carry only online cpus. |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | 1.3 verify |
| 128 | ------------ |
| 129 | |
| 130 | When the user decides a new policy (consisting of |
| 131 | "policy,governor,min,max") shall be set, this policy must be validated |
| 132 | so that incompatible values can be corrected. For verifying these |
| 133 | values, a frequency table helper and/or the |
| 134 | cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned |
| 135 | int min_freq, unsigned int max_freq) function might be helpful. See |
| 136 | section 2 for details on frequency table helpers. |
| 137 | |
| 138 | You need to make sure that at least one valid frequency (or operating |
| 139 | range) is within policy->min and policy->max. If necessary, increase |
| 140 | policy->max first, and only if this is no solution, decrease policy->min. |
| 141 | |
| 142 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 143 | 1.4 target/target_index or setpolicy? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | ---------------------------- |
| 145 | |
| 146 | Most cpufreq drivers or even most cpu frequency scaling algorithms |
| 147 | only allow the CPU to be set to one frequency. For these, you use the |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 148 | ->target/target_index call. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | Some cpufreq-capable processors switch the frequency between certain |
| 151 | limits on their own. These shall use the ->setpolicy call |
| 152 | |
| 153 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 154 | 1.4. target/target_index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | ------------- |
| 156 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 157 | The target_index call has two arguments: struct cpufreq_policy *policy, |
| 158 | and unsigned int index (into the exposed frequency table). |
| 159 | |
| 160 | The CPUfreq driver must set the new frequency when called here. The |
| 161 | actual frequency must be determined by freq_table[index].frequency. |
| 162 | |
| 163 | Deprecated: |
| 164 | ---------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | The target call has three arguments: struct cpufreq_policy *policy, |
| 166 | unsigned int target_frequency, unsigned int relation. |
| 167 | |
| 168 | The CPUfreq driver must set the new frequency when called here. The |
| 169 | actual frequency must be determined using the following rules: |
| 170 | |
| 171 | - keep close to "target_freq" |
| 172 | - policy->min <= new_freq <= policy->max (THIS MUST BE VALID!!!) |
| 173 | - if relation==CPUFREQ_REL_L, try to select a new_freq higher than or equal |
| 174 | target_freq. ("L for lowest, but no lower than") |
| 175 | - if relation==CPUFREQ_REL_H, try to select a new_freq lower than or equal |
| 176 | target_freq. ("H for highest, but no higher than") |
| 177 | |
Chumbalkar Nagananda | 51555c0 | 2009-05-21 23:29:48 +0000 | [diff] [blame] | 178 | Here again the frequency table helper might assist you - see section 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | for details. |
| 180 | |
| 181 | |
| 182 | 1.5 setpolicy |
| 183 | --------------- |
| 184 | |
| 185 | The setpolicy call only takes a struct cpufreq_policy *policy as |
| 186 | argument. You need to set the lower limit of the in-processor or |
| 187 | in-chipset dynamic frequency switching to policy->min, the upper limit |
| 188 | to policy->max, and -if supported- select a performance-oriented |
| 189 | setting when policy->policy is CPUFREQ_POLICY_PERFORMANCE, and a |
| 190 | powersaving-oriented setting when CPUFREQ_POLICY_POWERSAVE. Also check |
Wanlong Gao | 25eb650 | 2011-06-13 17:53:53 +0800 | [diff] [blame] | 191 | the reference implementation in drivers/cpufreq/longrun.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | |
| 194 | |
| 195 | 2. Frequency Table Helpers |
| 196 | ========================== |
| 197 | |
| 198 | As most cpufreq processors only allow for being set to a few specific |
| 199 | frequencies, a "frequency table" with some functions might assist in |
| 200 | some work of the processor driver. Such a "frequency table" consists |
Viresh Kumar | 3a7818e | 2013-04-01 12:57:42 +0000 | [diff] [blame] | 201 | of an array of struct cpufreq_frequency_table entries, with any value in |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 202 | "driver_data" you want to use, and the corresponding frequency in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | "frequency". At the end of the table, you need to add a |
Viresh Kumar | 3a7818e | 2013-04-01 12:57:42 +0000 | [diff] [blame] | 204 | cpufreq_frequency_table entry with frequency set to CPUFREQ_TABLE_END. And |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if you want to skip one entry in the table, set the frequency to |
| 206 | CPUFREQ_ENTRY_INVALID. The entries don't need to be in ascending |
| 207 | order. |
| 208 | |
| 209 | By calling cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
| 210 | struct cpufreq_frequency_table *table); |
| 211 | the cpuinfo.min_freq and cpuinfo.max_freq values are detected, and |
| 212 | policy->min and policy->max are set to the same values. This is |
| 213 | helpful for the per-CPU initialization stage. |
| 214 | |
| 215 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, |
| 216 | struct cpufreq_frequency_table *table); |
| 217 | assures that at least one valid frequency is within policy->min and |
| 218 | policy->max, and all other criteria are met. This is helpful for the |
| 219 | ->verify call. |
| 220 | |
| 221 | int cpufreq_frequency_table_target(struct cpufreq_policy *policy, |
| 222 | struct cpufreq_frequency_table *table, |
| 223 | unsigned int target_freq, |
| 224 | unsigned int relation, |
| 225 | unsigned int *index); |
| 226 | |
| 227 | is the corresponding frequency table helper for the ->target |
| 228 | stage. Just pass the values to this function, and the unsigned int |
| 229 | index returns the number of the frequency table entry which contains |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 230 | the frequency the CPU shall be set to. |