Daniel Lezcano | 0fac9e2f | 2019-04-28 11:51:04 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 2 | /* |
Daniel Lezcano | 23affa2 | 2019-12-19 23:53:17 +0100 | [diff] [blame] | 3 | * linux/drivers/thermal/cpufreq_cooling.c |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 6 | * |
Daniel Lezcano | 42cd9b0 | 2019-04-28 11:51:03 +0200 | [diff] [blame] | 7 | * Copyright (C) 2012-2018 Linaro Limited. |
| 8 | * |
| 9 | * Authors: Amit Daniel <amit.kachhap@linaro.org> |
| 10 | * Viresh Kumar <viresh.kumar@linaro.org> |
Viresh Kumar | 73904cb | 2014-12-04 09:42:08 +0530 | [diff] [blame] | 11 | * |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 12 | */ |
Amit Kucheria | 5ccb451 | 2020-05-11 17:54:57 +0530 | [diff] [blame] | 13 | #include <linux/cpu.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 14 | #include <linux/cpufreq.h> |
Amit Kucheria | 5ccb451 | 2020-05-11 17:54:57 +0530 | [diff] [blame] | 15 | #include <linux/cpu_cooling.h> |
| 16 | #include <linux/energy_model.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 17 | #include <linux/err.h> |
Amit Kucheria | c65f83c | 2020-05-11 17:54:58 +0530 | [diff] [blame] | 18 | #include <linux/export.h> |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 19 | #include <linux/idr.h> |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 20 | #include <linux/pm_opp.h> |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 21 | #include <linux/pm_qos.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 22 | #include <linux/slab.h> |
Amit Kucheria | 5ccb451 | 2020-05-11 17:54:57 +0530 | [diff] [blame] | 23 | #include <linux/thermal.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 24 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 25 | #include <trace/events/thermal.h> |
| 26 | |
Viresh Kumar | 07d888d | 2014-12-04 09:41:49 +0530 | [diff] [blame] | 27 | /* |
| 28 | * Cooling state <-> CPUFreq frequency |
| 29 | * |
| 30 | * Cooling states are translated to frequencies throughout this driver and this |
| 31 | * is the relation between them. |
| 32 | * |
| 33 | * Highest cooling state corresponds to lowest possible frequency. |
| 34 | * |
| 35 | * i.e. |
| 36 | * level 0 --> 1st Max Freq |
| 37 | * level 1 --> 2nd Max Freq |
| 38 | * ... |
| 39 | */ |
| 40 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 41 | /** |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 42 | * struct time_in_idle - Idle time stats |
| 43 | * @time: previous reading of the absolute time that this cpu was idle |
| 44 | * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() |
| 45 | */ |
| 46 | struct time_in_idle { |
| 47 | u64 time; |
| 48 | u64 timestamp; |
| 49 | }; |
| 50 | |
| 51 | /** |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 52 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 53 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
| 54 | * registered. |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 55 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 56 | * @cpufreq_state: integer value representing the current state of cpufreq |
| 57 | * cooling devices. |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 58 | * @max_level: maximum cooling level. One less than total number of valid |
| 59 | * cpufreq frequencies. |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 60 | * @em: Reference on the Energy Model of the device |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 61 | * @cdev: thermal_cooling_device pointer to keep track of the |
| 62 | * registered cooling device. |
| 63 | * @policy: cpufreq policy. |
Javi Merino | fc4de35 | 2014-12-15 16:55:52 +0000 | [diff] [blame] | 64 | * @node: list_head to link all cpufreq_cooling_device together. |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 65 | * @idle_time: idle time stats |
Amit Kucheria | 7b4e7f0 | 2019-11-20 21:15:11 +0530 | [diff] [blame] | 66 | * @qos_req: PM QoS contraint to apply |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 67 | * |
Viresh Kumar | beca605 | 2014-12-04 09:41:48 +0530 | [diff] [blame] | 68 | * This structure is required for keeping information of each registered |
| 69 | * cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 70 | */ |
| 71 | struct cpufreq_cooling_device { |
| 72 | int id; |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 73 | u32 last_load; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 74 | unsigned int cpufreq_state; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 75 | unsigned int max_level; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 76 | struct em_perf_domain *em; |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 77 | struct cpufreq_policy *policy; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 78 | struct list_head node; |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 79 | struct time_in_idle *idle_time; |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 80 | struct freq_qos_request qos_req; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 81 | }; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 82 | |
Viresh Kumar | fb8ea30 | 2017-04-25 15:57:09 +0530 | [diff] [blame] | 83 | static DEFINE_IDA(cpufreq_ida); |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 84 | static DEFINE_MUTEX(cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 85 | static LIST_HEAD(cpufreq_cdev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 86 | |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 87 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 88 | /** |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 89 | * get_level: Find the level for a particular frequency |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 90 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 91 | * @freq: Frequency |
Eduardo Valentin | 82b9ee4 | 2013-04-17 17:12:00 +0000 | [diff] [blame] | 92 | * |
Viresh Kumar | da27f69 | 2017-04-25 15:57:21 +0530 | [diff] [blame] | 93 | * Return: level corresponding to the frequency. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 94 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 95 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 96 | unsigned int freq) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 97 | { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 98 | int i; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 99 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 100 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
| 101 | if (freq > cpufreq_cdev->em->table[i].frequency) |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 102 | break; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 105 | return cpufreq_cdev->max_level - i - 1; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 108 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 109 | u32 freq) |
| 110 | { |
| 111 | int i; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 112 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 113 | for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { |
| 114 | if (freq > cpufreq_cdev->em->table[i].frequency) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 115 | break; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 116 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 117 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 118 | return cpufreq_cdev->em->table[i + 1].power; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 121 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 122 | u32 power) |
| 123 | { |
| 124 | int i; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 125 | |
Finley Xiao | 371a3bc | 2020-06-19 17:08:25 +0800 | [diff] [blame] | 126 | for (i = cpufreq_cdev->max_level; i >= 0; i--) { |
| 127 | if (power >= cpufreq_cdev->em->table[i].power) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 128 | break; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 129 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 130 | |
Finley Xiao | 371a3bc | 2020-06-19 17:08:25 +0800 | [diff] [blame] | 131 | return cpufreq_cdev->em->table[i].frequency; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * get_load() - get load for a cpu since last updated |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 136 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 137 | * @cpu: cpu number |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 138 | * @cpu_idx: index of the cpu in time_in_idle* |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 139 | * |
| 140 | * Return: The average load of cpu @cpu in percentage since this |
| 141 | * function was last called. |
| 142 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 143 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 144 | int cpu_idx) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 145 | { |
| 146 | u32 load; |
| 147 | u64 now, now_idle, delta_time, delta_idle; |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 148 | struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 149 | |
| 150 | now_idle = get_cpu_idle_time(cpu, &now, 0); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 151 | delta_idle = now_idle - idle_time->time; |
| 152 | delta_time = now - idle_time->timestamp; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 153 | |
| 154 | if (delta_time <= delta_idle) |
| 155 | load = 0; |
| 156 | else |
| 157 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); |
| 158 | |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 159 | idle_time->time = now_idle; |
| 160 | idle_time->timestamp = now; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 161 | |
| 162 | return load; |
| 163 | } |
| 164 | |
| 165 | /** |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 166 | * get_dynamic_power() - calculate the dynamic power |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 167 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 168 | * @freq: current frequency |
| 169 | * |
| 170 | * Return: the dynamic power consumed by the cpus described by |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 171 | * @cpufreq_cdev. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 172 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 173 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 174 | unsigned long freq) |
| 175 | { |
| 176 | u32 raw_cpu_power; |
| 177 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 178 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
| 179 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 180 | } |
| 181 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 182 | /** |
| 183 | * cpufreq_get_requested_power() - get the current power |
| 184 | * @cdev: &thermal_cooling_device pointer |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 185 | * @power: pointer in which to store the resulting power |
| 186 | * |
| 187 | * Calculate the current power consumption of the cpus in milliwatts |
| 188 | * and store it in @power. This function should actually calculate |
| 189 | * the requested power, but it's hard to get the frequency that |
| 190 | * cpufreq would have assigned if there were no thermal limits. |
| 191 | * Instead, we calculate the current power on the assumption that the |
| 192 | * immediate future will look like the immediate past. |
| 193 | * |
| 194 | * We use the current frequency and the average load since this |
| 195 | * function was last called. In reality, there could have been |
| 196 | * multiple opps since this function was last called and that affects |
| 197 | * the load calculation. While it's not perfectly accurate, this |
| 198 | * simplification is good enough and works. REVISIT this, as more |
| 199 | * complex code may be needed if experiments show that it's not |
| 200 | * accurate enough. |
| 201 | * |
| 202 | * Return: 0 on success, -E* if getting the static power failed. |
| 203 | */ |
| 204 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 205 | u32 *power) |
| 206 | { |
| 207 | unsigned long freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 208 | int i = 0, cpu; |
| 209 | u32 total_load = 0; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 210 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 211 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 212 | u32 *load_cpu = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 213 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 214 | freq = cpufreq_quick_get(policy->cpu); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 215 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 216 | if (trace_thermal_power_cpu_get_power_enabled()) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 217 | u32 ncpus = cpumask_weight(policy->related_cpus); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 218 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 219 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 222 | for_each_cpu(cpu, policy->related_cpus) { |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 223 | u32 load; |
| 224 | |
| 225 | if (cpu_online(cpu)) |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 226 | load = get_load(cpufreq_cdev, cpu, i); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 227 | else |
| 228 | load = 0; |
| 229 | |
| 230 | total_load += load; |
Matthias Kaehlcke | bf45ac1 | 2019-05-02 11:32:38 -0700 | [diff] [blame] | 231 | if (load_cpu) |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 232 | load_cpu[i] = load; |
| 233 | |
| 234 | i++; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 237 | cpufreq_cdev->last_load = total_load; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 238 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 239 | *power = get_dynamic_power(cpufreq_cdev, freq); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 240 | |
| 241 | if (load_cpu) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 242 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 243 | load_cpu, i, *power); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 244 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 245 | kfree(load_cpu); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 246 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 247 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * cpufreq_state2power() - convert a cpu cdev state to power consumed |
| 253 | * @cdev: &thermal_cooling_device pointer |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 254 | * @state: cooling device state to be converted |
| 255 | * @power: pointer in which to store the resulting power |
| 256 | * |
| 257 | * Convert cooling device state @state into power consumption in |
| 258 | * milliwatts assuming 100% load. Store the calculated power in |
| 259 | * @power. |
| 260 | * |
| 261 | * Return: 0 on success, -EINVAL if the cooling device state could not |
| 262 | * be converted into a frequency or other -E* if there was an error |
| 263 | * when calculating the static power. |
| 264 | */ |
| 265 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 266 | unsigned long state, u32 *power) |
| 267 | { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 268 | unsigned int freq, num_cpus, idx; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 269 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 270 | |
Viresh Kumar | cb1b631 | 2017-04-25 15:57:23 +0530 | [diff] [blame] | 271 | /* Request state should be less than max_level */ |
Daniel Lezcano | 40ea568 | 2020-03-21 20:31:07 +0100 | [diff] [blame] | 272 | if (state > cpufreq_cdev->max_level) |
Viresh Kumar | cb1b631 | 2017-04-25 15:57:23 +0530 | [diff] [blame] | 273 | return -EINVAL; |
| 274 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 275 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 276 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 277 | idx = cpufreq_cdev->max_level - state; |
| 278 | freq = cpufreq_cdev->em->table[idx].frequency; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 279 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 280 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 281 | return 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * cpufreq_power2state() - convert power to a cooling device state |
| 286 | * @cdev: &thermal_cooling_device pointer |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 287 | * @power: power in milliwatts to be converted |
| 288 | * @state: pointer in which to store the resulting state |
| 289 | * |
| 290 | * Calculate a cooling device state for the cpus described by @cdev |
| 291 | * that would allow them to consume at most @power mW and store it in |
| 292 | * @state. Note that this calculation depends on external factors |
| 293 | * such as the cpu load or the current static power. Calling this |
| 294 | * function with the same power as input can yield different cooling |
| 295 | * device states depending on those external factors. |
| 296 | * |
| 297 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if |
| 298 | * the calculated frequency could not be converted to a valid state. |
| 299 | * The latter should not happen unless the frequencies available to |
| 300 | * cpufreq have changed since the initialization of the cpu cooling |
| 301 | * device. |
| 302 | */ |
| 303 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, |
zhuguangqing | ecd1d2a | 2020-09-14 15:11:01 +0800 | [diff] [blame] | 304 | u32 power, unsigned long *state) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 305 | { |
Shaokun Zhang | e0fda73 | 2019-02-18 14:22:30 +0800 | [diff] [blame] | 306 | unsigned int target_freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 307 | u32 last_load, normalised_power; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 308 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 309 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 310 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 311 | last_load = cpufreq_cdev->last_load ?: 1; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 312 | normalised_power = (power * 100) / last_load; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 313 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 314 | |
Viresh Kumar | 3e08b2d | 2017-04-25 15:57:12 +0530 | [diff] [blame] | 315 | *state = get_level(cpufreq_cdev, target_freq); |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 316 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
| 317 | power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 320 | |
| 321 | static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev, |
| 322 | struct em_perf_domain *em) { |
| 323 | struct cpufreq_policy *policy; |
| 324 | unsigned int nr_levels; |
| 325 | |
| 326 | if (!em) |
| 327 | return false; |
| 328 | |
| 329 | policy = cpufreq_cdev->policy; |
Lukasz Luba | 521b512 | 2020-05-27 10:58:47 +0100 | [diff] [blame] | 330 | if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 331 | pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n", |
Lukasz Luba | 521b512 | 2020-05-27 10:58:47 +0100 | [diff] [blame] | 332 | cpumask_pr_args(em_span_cpus(em)), |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 333 | cpumask_pr_args(policy->related_cpus)); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | nr_levels = cpufreq_cdev->max_level + 1; |
Lukasz Luba | 521b512 | 2020-05-27 10:58:47 +0100 | [diff] [blame] | 338 | if (em_pd_nr_perf_states(em) != nr_levels) { |
| 339 | pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n", |
| 340 | cpumask_pr_args(em_span_cpus(em)), |
| 341 | em_pd_nr_perf_states(em), nr_levels); |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 342 | return false; |
| 343 | } |
| 344 | |
| 345 | return true; |
| 346 | } |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 347 | #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */ |
| 348 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 349 | static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
| 350 | unsigned long state) |
| 351 | { |
| 352 | struct cpufreq_policy *policy; |
| 353 | unsigned long idx; |
| 354 | |
| 355 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
| 356 | /* Use the Energy Model table if available */ |
| 357 | if (cpufreq_cdev->em) { |
| 358 | idx = cpufreq_cdev->max_level - state; |
| 359 | return cpufreq_cdev->em->table[idx].frequency; |
| 360 | } |
| 361 | #endif |
| 362 | |
| 363 | /* Otherwise, fallback on the CPUFreq table */ |
| 364 | policy = cpufreq_cdev->policy; |
| 365 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 366 | idx = cpufreq_cdev->max_level - state; |
| 367 | else |
| 368 | idx = state; |
| 369 | |
| 370 | return policy->freq_table[idx].frequency; |
| 371 | } |
| 372 | |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 373 | /* cpufreq cooling device callback functions are defined below */ |
| 374 | |
| 375 | /** |
| 376 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 377 | * @cdev: thermal cooling device pointer. |
| 378 | * @state: fill this variable with the max cooling state. |
| 379 | * |
| 380 | * Callback for the thermal cooling device to return the cpufreq |
| 381 | * max cooling state. |
| 382 | * |
| 383 | * Return: 0 on success, an error code otherwise. |
| 384 | */ |
| 385 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 386 | unsigned long *state) |
| 387 | { |
| 388 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 389 | |
| 390 | *state = cpufreq_cdev->max_level; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 396 | * @cdev: thermal cooling device pointer. |
| 397 | * @state: fill this variable with the current cooling state. |
| 398 | * |
| 399 | * Callback for the thermal cooling device to return the cpufreq |
| 400 | * current cooling state. |
| 401 | * |
| 402 | * Return: 0 on success, an error code otherwise. |
| 403 | */ |
| 404 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 405 | unsigned long *state) |
| 406 | { |
| 407 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 408 | |
| 409 | *state = cpufreq_cdev->cpufreq_state; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 416 | * @cdev: thermal cooling device pointer. |
| 417 | * @state: set this variable to the current cooling state. |
| 418 | * |
| 419 | * Callback for the thermal cooling device to change the cpufreq |
| 420 | * current cooling state. |
| 421 | * |
| 422 | * Return: 0 on success, an error code otherwise. |
| 423 | */ |
| 424 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 425 | unsigned long state) |
| 426 | { |
| 427 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Thara Gopinath | f12e4f6 | 2020-02-21 19:52:12 -0500 | [diff] [blame] | 428 | struct cpumask *cpus; |
| 429 | unsigned int frequency; |
| 430 | unsigned long max_capacity, capacity; |
| 431 | int ret; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 432 | |
| 433 | /* Request state should be less than max_level */ |
Daniel Lezcano | 40ea568 | 2020-03-21 20:31:07 +0100 | [diff] [blame] | 434 | if (state > cpufreq_cdev->max_level) |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | |
| 437 | /* Check if the old cooling action is same as new cooling action */ |
| 438 | if (cpufreq_cdev->cpufreq_state == state) |
| 439 | return 0; |
| 440 | |
| 441 | cpufreq_cdev->cpufreq_state = state; |
| 442 | |
Thara Gopinath | f12e4f6 | 2020-02-21 19:52:12 -0500 | [diff] [blame] | 443 | frequency = get_state_freq(cpufreq_cdev, state); |
| 444 | |
| 445 | ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency); |
| 446 | |
| 447 | if (ret > 0) { |
| 448 | cpus = cpufreq_cdev->policy->cpus; |
| 449 | max_capacity = arch_scale_cpu_capacity(cpumask_first(cpus)); |
| 450 | capacity = frequency * max_capacity; |
| 451 | capacity /= cpufreq_cdev->policy->cpuinfo.max_freq; |
| 452 | arch_set_thermal_pressure(cpus, max_capacity - capacity); |
Linus Torvalds | 34183dd | 2020-04-07 20:00:16 -0700 | [diff] [blame] | 453 | ret = 0; |
Thara Gopinath | f12e4f6 | 2020-02-21 19:52:12 -0500 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return ret; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 457 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 458 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 459 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 460 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 461 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 462 | .get_max_state = cpufreq_get_max_state, |
| 463 | .get_cur_state = cpufreq_get_cur_state, |
| 464 | .set_cur_state = cpufreq_set_cur_state, |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 465 | }; |
| 466 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 467 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 468 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 469 | * @np: a valid struct device_node to the cooling device device tree node |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 470 | * @policy: cpufreq policy |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 471 | * Normally this should be same as cpufreq policy->related_cpus. |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 472 | * @em: Energy Model of the cpufreq policy |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 473 | * |
| 474 | * This interface function registers the cpufreq cooling device with the name |
| 475 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 476 | * cooling devices. It also gives the opportunity to link the cooling device |
| 477 | * with a device tree node, in order to bind it via the thermal DT code. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 478 | * |
| 479 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 480 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 481 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 482 | static struct thermal_cooling_device * |
| 483 | __cpufreq_cooling_register(struct device_node *np, |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 484 | struct cpufreq_policy *policy, |
| 485 | struct em_perf_domain *em) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 486 | { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 487 | struct thermal_cooling_device *cdev; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 488 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 489 | char dev_name[THERMAL_NAME_LENGTH]; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 490 | unsigned int i, num_cpus; |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 491 | struct device *dev; |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 492 | int ret; |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 493 | struct thermal_cooling_device_ops *cooling_ops; |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 494 | |
| 495 | dev = get_cpu_device(policy->cpu); |
| 496 | if (unlikely(!dev)) { |
| 497 | pr_warn("No cpu device for cpu %d\n", policy->cpu); |
| 498 | return ERR_PTR(-ENODEV); |
| 499 | } |
| 500 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 501 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 502 | if (IS_ERR_OR_NULL(policy)) { |
Arvind Yadav | b2fd708 | 2017-10-24 13:20:39 +0530 | [diff] [blame] | 503 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 504 | return ERR_PTR(-EINVAL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 505 | } |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 506 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 507 | i = cpufreq_table_count_valid_entries(policy); |
| 508 | if (!i) { |
| 509 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", |
| 510 | __func__); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 511 | return ERR_PTR(-ENODEV); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 512 | } |
| 513 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 514 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 515 | if (!cpufreq_cdev) |
| 516 | return ERR_PTR(-ENOMEM); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 517 | |
Viresh Kumar | b12b651 | 2017-04-25 15:57:16 +0530 | [diff] [blame] | 518 | cpufreq_cdev->policy = policy; |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 519 | num_cpus = cpumask_weight(policy->related_cpus); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 520 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
| 521 | sizeof(*cpufreq_cdev->idle_time), |
| 522 | GFP_KERNEL); |
| 523 | if (!cpufreq_cdev->idle_time) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 524 | cdev = ERR_PTR(-ENOMEM); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 525 | goto free_cdev; |
| 526 | } |
| 527 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 528 | /* max_level is an index, not a counter */ |
| 529 | cpufreq_cdev->max_level = i - 1; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 530 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 531 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
| 532 | if (ret < 0) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 533 | cdev = ERR_PTR(ret); |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 534 | goto free_idle_time; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 535 | } |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 536 | cpufreq_cdev->id = ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 537 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 538 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 539 | cpufreq_cdev->id); |
| 540 | |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 541 | cooling_ops = &cpufreq_cooling_ops; |
| 542 | |
| 543 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 544 | if (em_is_sane(cpufreq_cdev, em)) { |
| 545 | cpufreq_cdev->em = em; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 546 | cooling_ops->get_requested_power = cpufreq_get_requested_power; |
| 547 | cooling_ops->state2power = cpufreq_state2power; |
| 548 | cooling_ops->power2state = cpufreq_power2state; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 549 | } else |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 550 | #endif |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 551 | if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) { |
| 552 | pr_err("%s: unsorted frequency tables are not supported\n", |
| 553 | __func__); |
| 554 | cdev = ERR_PTR(-EINVAL); |
| 555 | goto remove_ida; |
| 556 | } |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 557 | |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 558 | ret = freq_qos_add_request(&policy->constraints, |
| 559 | &cpufreq_cdev->qos_req, FREQ_QOS_MAX, |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 560 | get_state_freq(cpufreq_cdev, 0)); |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 561 | if (ret < 0) { |
| 562 | pr_err("%s: Failed to add freq constraint (%d)\n", __func__, |
| 563 | ret); |
| 564 | cdev = ERR_PTR(ret); |
| 565 | goto remove_ida; |
| 566 | } |
| 567 | |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 568 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
| 569 | cooling_ops); |
| 570 | if (IS_ERR(cdev)) |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 571 | goto remove_qos_req; |
Viresh Kumar | 92e615e | 2014-12-04 09:41:51 +0530 | [diff] [blame] | 572 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 573 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 574 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 575 | mutex_unlock(&cooling_list_lock); |
| 576 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 577 | return cdev; |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 578 | |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 579 | remove_qos_req: |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 580 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 581 | remove_ida: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 582 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 583 | free_idle_time: |
| 584 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 585 | free_cdev: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 586 | kfree(cpufreq_cdev); |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 587 | return cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 588 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 592 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 593 | * |
| 594 | * This interface function registers the cpufreq cooling device with the name |
| 595 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 596 | * cooling devices. |
| 597 | * |
| 598 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 599 | * on failure, it returns a corresponding ERR_PTR(). |
| 600 | */ |
| 601 | struct thermal_cooling_device * |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 602 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 603 | { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 604 | return __cpufreq_cooling_register(NULL, policy, NULL); |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 605 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 606 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 607 | |
| 608 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 609 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 610 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 611 | * |
| 612 | * This interface function registers the cpufreq cooling device with the name |
| 613 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 614 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 615 | * linked to the device tree node provided. |
| 616 | * |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 617 | * Using this function, the cooling device will implement the power |
| 618 | * extensions by using a simple cpu power model. The cpus must have |
| 619 | * registered their OPPs using the OPP library. |
| 620 | * |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 621 | * It also takes into account, if property present in policy CPU node, the |
| 622 | * static power consumed by the cpu. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 623 | * |
| 624 | * Return: a valid struct thermal_cooling_device pointer on success, |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 625 | * and NULL on failure. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 626 | */ |
| 627 | struct thermal_cooling_device * |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 628 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 629 | { |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 630 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
| 631 | struct thermal_cooling_device *cdev = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 632 | |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 633 | if (!np) { |
Daniel Lezcano | 23affa2 | 2019-12-19 23:53:17 +0100 | [diff] [blame] | 634 | pr_err("cpufreq_cooling: OF node not available for cpu%d\n", |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 635 | policy->cpu); |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | if (of_find_property(np, "#cooling-cells", NULL)) { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 640 | struct em_perf_domain *em = em_cpu_get(policy->cpu); |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 641 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 642 | cdev = __cpufreq_cooling_register(np, policy, em); |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 643 | if (IS_ERR(cdev)) { |
Daniel Lezcano | 23affa2 | 2019-12-19 23:53:17 +0100 | [diff] [blame] | 644 | pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n", |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 645 | policy->cpu, PTR_ERR(cdev)); |
| 646 | cdev = NULL; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | of_node_put(np); |
| 651 | return cdev; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 652 | } |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 653 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 654 | |
| 655 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 656 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 657 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 658 | * |
| 659 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 660 | */ |
| 661 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 662 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 663 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 664 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 665 | if (!cdev) |
| 666 | return; |
| 667 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 668 | cpufreq_cdev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 669 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 670 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 671 | list_del(&cpufreq_cdev->node); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 672 | mutex_unlock(&cooling_list_lock); |
| 673 | |
Daniel Lezcano | 72554a7 | 2019-04-28 11:51:05 +0200 | [diff] [blame] | 674 | thermal_cooling_device_unregister(cdev); |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 675 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 676 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 677 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 678 | kfree(cpufreq_cdev); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 679 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 680 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |