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 F r e q C o r e |
| 7 | |
| 8 | |
| 9 | Dominik Brodowski <linux@brodo.de> |
| 10 | David Kimdon <dwhedon@debian.org> |
| 11 | |
| 12 | |
| 13 | |
| 14 | Clock scaling allows you to change the clock speed of the CPUs on the |
| 15 | fly. This is a nice method to save battery power, because the lower |
| 16 | the clock speed, the less power the CPU consumes. |
| 17 | |
| 18 | |
| 19 | Contents: |
| 20 | --------- |
| 21 | 1. CPUFreq core and interfaces |
| 22 | 2. CPUFreq notifiers |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 23 | 3. CPUFreq Table Generation with Operating Performance Point (OPP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | 1. General Information |
| 26 | ======================= |
| 27 | |
Dominik Brodowski | eff0df6 | 2006-10-02 19:26:47 -0400 | [diff] [blame] | 28 | The CPUFreq core code is located in drivers/cpufreq/cpufreq.c. This |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | cpufreq code offers a standardized interface for the CPUFreq |
| 30 | architecture drivers (those pieces of code that do actual |
| 31 | frequency transitions), as well as to "notifiers". These are device |
| 32 | drivers or other part of the kernel that need to be informed of |
| 33 | policy changes (ex. thermal modules like ACPI) or of all |
| 34 | frequency changes (ex. timing code) or even need to force certain |
| 35 | speed limits (like LCD drivers on ARM architecture). Additionally, the |
| 36 | kernel "constant" loops_per_jiffy is updated on frequency changes |
| 37 | here. |
| 38 | |
| 39 | Reference counting is done by cpufreq_get_cpu and cpufreq_put_cpu, |
| 40 | which make sure that the cpufreq processor driver is correctly |
| 41 | registered with the core, and will not be unloaded until |
| 42 | cpufreq_put_cpu is called. |
| 43 | |
| 44 | 2. CPUFreq notifiers |
| 45 | ==================== |
| 46 | |
| 47 | CPUFreq notifiers conform to the standard kernel notifier interface. |
| 48 | See linux/include/linux/notifier.h for details on notifiers. |
| 49 | |
| 50 | There are two different CPUFreq notifiers - policy notifiers and |
| 51 | transition notifiers. |
| 52 | |
| 53 | |
| 54 | 2.1 CPUFreq policy notifiers |
| 55 | ---------------------------- |
| 56 | |
| 57 | These are notified when a new policy is intended to be set. Each |
Viresh Kumar | 6bfb7c7 | 2015-08-03 08:36:14 +0530 | [diff] [blame] | 58 | CPUFreq policy notifier is called twice for a policy transition: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | 1.) During CPUFREQ_ADJUST all CPUFreq notifiers may change the limit if |
| 61 | they see a need for this - may it be thermal considerations or |
| 62 | hardware limitations. |
| 63 | |
Viresh Kumar | 6bfb7c7 | 2015-08-03 08:36:14 +0530 | [diff] [blame] | 64 | 2.) And during CPUFREQ_NOTIFY all notifiers are informed of the new policy |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | - if two hardware drivers failed to agree on a new policy before this |
| 66 | stage, the incompatible hardware shall be shut down, and the user |
| 67 | informed of this. |
| 68 | |
| 69 | The phase is specified in the second argument to the notifier. |
| 70 | |
| 71 | The third argument, a void *pointer, points to a struct cpufreq_policy |
| 72 | consisting of five values: cpu, min, max, policy and max_cpu_freq. min |
| 73 | and max are the lower and upper frequencies (in kHz) of the new |
| 74 | policy, policy the new policy, cpu the number of the affected CPU; and |
| 75 | max_cpu_freq the maximum supported CPU frequency. This value is given |
| 76 | for informational purposes only. |
| 77 | |
| 78 | |
| 79 | 2.2 CPUFreq transition notifiers |
| 80 | -------------------------------- |
| 81 | |
| 82 | These are notified twice when the CPUfreq driver switches the CPU core |
| 83 | frequency and this change has any external implications. |
| 84 | |
| 85 | The second argument specifies the phase - CPUFREQ_PRECHANGE or |
| 86 | CPUFREQ_POSTCHANGE. |
| 87 | |
| 88 | The third argument is a struct cpufreq_freqs with the following |
| 89 | values: |
| 90 | cpu - number of the affected CPU |
| 91 | old - old frequency |
| 92 | new - new frequency |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 93 | |
| 94 | 3. CPUFreq Table Generation with Operating Performance Point (OPP) |
| 95 | ================================================================== |
| 96 | For details about OPP, see Documentation/power/opp.txt |
| 97 | |
| 98 | dev_pm_opp_init_cpufreq_table - cpufreq framework typically is initialized with |
Viresh Kumar | 64bf55a | 2016-05-31 16:50:23 +0530 | [diff] [blame^] | 99 | cpufreq_table_validate_and_show() which is provided with the list of |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 100 | frequencies that are available for operation. This function provides |
| 101 | a ready to use conversion routine to translate the OPP layer's internal |
| 102 | information about the available frequencies into a format readily |
| 103 | providable to cpufreq. |
| 104 | |
| 105 | WARNING: Do not use this function in interrupt context. |
| 106 | |
| 107 | Example: |
| 108 | soc_pm_init() |
| 109 | { |
| 110 | /* Do things */ |
| 111 | r = dev_pm_opp_init_cpufreq_table(dev, &freq_table); |
| 112 | if (!r) |
Viresh Kumar | 64bf55a | 2016-05-31 16:50:23 +0530 | [diff] [blame^] | 113 | cpufreq_table_validate_and_show(policy, freq_table); |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 114 | /* Do other things */ |
| 115 | } |
| 116 | |
| 117 | NOTE: This function is available only if CONFIG_CPU_FREQ is enabled in |
| 118 | addition to CONFIG_PM_OPP. |
| 119 | |
| 120 | dev_pm_opp_free_cpufreq_table - Free up the table allocated by dev_pm_opp_init_cpufreq_table |