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 Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/thermal.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 15 | #include <linux/cpufreq.h> |
| 16 | #include <linux/err.h> |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 17 | #include <linux/idr.h> |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 18 | #include <linux/pm_opp.h> |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 19 | #include <linux/pm_qos.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/cpu.h> |
| 22 | #include <linux/cpu_cooling.h> |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 23 | #include <linux/energy_model.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 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 126 | for (i = cpufreq_cdev->max_level - 1; 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 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 131 | return cpufreq_cdev->em->table[i + 1].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 |
| 185 | * @tz: a valid thermal zone device pointer |
| 186 | * @power: pointer in which to store the resulting power |
| 187 | * |
| 188 | * Calculate the current power consumption of the cpus in milliwatts |
| 189 | * and store it in @power. This function should actually calculate |
| 190 | * the requested power, but it's hard to get the frequency that |
| 191 | * cpufreq would have assigned if there were no thermal limits. |
| 192 | * Instead, we calculate the current power on the assumption that the |
| 193 | * immediate future will look like the immediate past. |
| 194 | * |
| 195 | * We use the current frequency and the average load since this |
| 196 | * function was last called. In reality, there could have been |
| 197 | * multiple opps since this function was last called and that affects |
| 198 | * the load calculation. While it's not perfectly accurate, this |
| 199 | * simplification is good enough and works. REVISIT this, as more |
| 200 | * complex code may be needed if experiments show that it's not |
| 201 | * accurate enough. |
| 202 | * |
| 203 | * Return: 0 on success, -E* if getting the static power failed. |
| 204 | */ |
| 205 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, |
| 206 | struct thermal_zone_device *tz, |
| 207 | u32 *power) |
| 208 | { |
| 209 | unsigned long freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 210 | int i = 0, cpu; |
| 211 | u32 total_load = 0; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 212 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 213 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 214 | u32 *load_cpu = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 215 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 216 | freq = cpufreq_quick_get(policy->cpu); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 217 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 218 | if (trace_thermal_power_cpu_get_power_enabled()) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 219 | u32 ncpus = cpumask_weight(policy->related_cpus); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 220 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 221 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 224 | for_each_cpu(cpu, policy->related_cpus) { |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 225 | u32 load; |
| 226 | |
| 227 | if (cpu_online(cpu)) |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 228 | load = get_load(cpufreq_cdev, cpu, i); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 229 | else |
| 230 | load = 0; |
| 231 | |
| 232 | total_load += load; |
Matthias Kaehlcke | bf45ac1 | 2019-05-02 11:32:38 -0700 | [diff] [blame] | 233 | if (load_cpu) |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 234 | load_cpu[i] = load; |
| 235 | |
| 236 | i++; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 239 | cpufreq_cdev->last_load = total_load; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 240 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 241 | *power = get_dynamic_power(cpufreq_cdev, freq); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 242 | |
| 243 | if (load_cpu) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 244 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 245 | load_cpu, i, *power); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 246 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 247 | kfree(load_cpu); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 248 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 249 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * cpufreq_state2power() - convert a cpu cdev state to power consumed |
| 255 | * @cdev: &thermal_cooling_device pointer |
| 256 | * @tz: a valid thermal zone device pointer |
| 257 | * @state: cooling device state to be converted |
| 258 | * @power: pointer in which to store the resulting power |
| 259 | * |
| 260 | * Convert cooling device state @state into power consumption in |
| 261 | * milliwatts assuming 100% load. Store the calculated power in |
| 262 | * @power. |
| 263 | * |
| 264 | * Return: 0 on success, -EINVAL if the cooling device state could not |
| 265 | * be converted into a frequency or other -E* if there was an error |
| 266 | * when calculating the static power. |
| 267 | */ |
| 268 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, |
| 269 | struct thermal_zone_device *tz, |
| 270 | unsigned long state, u32 *power) |
| 271 | { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 272 | unsigned int freq, num_cpus, idx; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 273 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 274 | |
Viresh Kumar | cb1b631 | 2017-04-25 15:57:23 +0530 | [diff] [blame] | 275 | /* Request state should be less than max_level */ |
Daniel Lezcano | 40ea568 | 2020-03-21 20:31:07 +0100 | [diff] [blame^] | 276 | if (state > cpufreq_cdev->max_level) |
Viresh Kumar | cb1b631 | 2017-04-25 15:57:23 +0530 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 279 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 280 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 281 | idx = cpufreq_cdev->max_level - state; |
| 282 | freq = cpufreq_cdev->em->table[idx].frequency; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 283 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 284 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 285 | return 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * cpufreq_power2state() - convert power to a cooling device state |
| 290 | * @cdev: &thermal_cooling_device pointer |
| 291 | * @tz: a valid thermal zone device pointer |
| 292 | * @power: power in milliwatts to be converted |
| 293 | * @state: pointer in which to store the resulting state |
| 294 | * |
| 295 | * Calculate a cooling device state for the cpus described by @cdev |
| 296 | * that would allow them to consume at most @power mW and store it in |
| 297 | * @state. Note that this calculation depends on external factors |
| 298 | * such as the cpu load or the current static power. Calling this |
| 299 | * function with the same power as input can yield different cooling |
| 300 | * device states depending on those external factors. |
| 301 | * |
| 302 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if |
| 303 | * the calculated frequency could not be converted to a valid state. |
| 304 | * The latter should not happen unless the frequencies available to |
| 305 | * cpufreq have changed since the initialization of the cpu cooling |
| 306 | * device. |
| 307 | */ |
| 308 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, |
| 309 | struct thermal_zone_device *tz, u32 power, |
| 310 | unsigned long *state) |
| 311 | { |
Shaokun Zhang | e0fda73 | 2019-02-18 14:22:30 +0800 | [diff] [blame] | 312 | unsigned int target_freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 313 | u32 last_load, normalised_power; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 314 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 315 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 316 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 317 | last_load = cpufreq_cdev->last_load ?: 1; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 318 | normalised_power = (power * 100) / last_load; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 319 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 320 | |
Viresh Kumar | 3e08b2d | 2017-04-25 15:57:12 +0530 | [diff] [blame] | 321 | *state = get_level(cpufreq_cdev, target_freq); |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 322 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
| 323 | power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 326 | |
| 327 | static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev, |
| 328 | struct em_perf_domain *em) { |
| 329 | struct cpufreq_policy *policy; |
| 330 | unsigned int nr_levels; |
| 331 | |
| 332 | if (!em) |
| 333 | return false; |
| 334 | |
| 335 | policy = cpufreq_cdev->policy; |
| 336 | if (!cpumask_equal(policy->related_cpus, to_cpumask(em->cpus))) { |
| 337 | pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n", |
| 338 | cpumask_pr_args(to_cpumask(em->cpus)), |
| 339 | cpumask_pr_args(policy->related_cpus)); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | nr_levels = cpufreq_cdev->max_level + 1; |
| 344 | if (em->nr_cap_states != nr_levels) { |
| 345 | pr_err("The number of cap states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n", |
| 346 | cpumask_pr_args(to_cpumask(em->cpus)), |
| 347 | em->nr_cap_states, nr_levels); |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return true; |
| 352 | } |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 353 | #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */ |
| 354 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 355 | static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
| 356 | unsigned long state) |
| 357 | { |
| 358 | struct cpufreq_policy *policy; |
| 359 | unsigned long idx; |
| 360 | |
| 361 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
| 362 | /* Use the Energy Model table if available */ |
| 363 | if (cpufreq_cdev->em) { |
| 364 | idx = cpufreq_cdev->max_level - state; |
| 365 | return cpufreq_cdev->em->table[idx].frequency; |
| 366 | } |
| 367 | #endif |
| 368 | |
| 369 | /* Otherwise, fallback on the CPUFreq table */ |
| 370 | policy = cpufreq_cdev->policy; |
| 371 | if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) |
| 372 | idx = cpufreq_cdev->max_level - state; |
| 373 | else |
| 374 | idx = state; |
| 375 | |
| 376 | return policy->freq_table[idx].frequency; |
| 377 | } |
| 378 | |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 379 | /* cpufreq cooling device callback functions are defined below */ |
| 380 | |
| 381 | /** |
| 382 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 383 | * @cdev: thermal cooling device pointer. |
| 384 | * @state: fill this variable with the max cooling state. |
| 385 | * |
| 386 | * Callback for the thermal cooling device to return the cpufreq |
| 387 | * max cooling state. |
| 388 | * |
| 389 | * Return: 0 on success, an error code otherwise. |
| 390 | */ |
| 391 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 392 | unsigned long *state) |
| 393 | { |
| 394 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 395 | |
| 396 | *state = cpufreq_cdev->max_level; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 402 | * @cdev: thermal cooling device pointer. |
| 403 | * @state: fill this variable with the current cooling state. |
| 404 | * |
| 405 | * Callback for the thermal cooling device to return the cpufreq |
| 406 | * current cooling state. |
| 407 | * |
| 408 | * Return: 0 on success, an error code otherwise. |
| 409 | */ |
| 410 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 411 | unsigned long *state) |
| 412 | { |
| 413 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
| 414 | |
| 415 | *state = cpufreq_cdev->cpufreq_state; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 422 | * @cdev: thermal cooling device pointer. |
| 423 | * @state: set this variable to the current cooling state. |
| 424 | * |
| 425 | * Callback for the thermal cooling device to change the cpufreq |
| 426 | * current cooling state. |
| 427 | * |
| 428 | * Return: 0 on success, an error code otherwise. |
| 429 | */ |
| 430 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 431 | unsigned long state) |
| 432 | { |
| 433 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Willy Wolff | ff44f67 | 2020-03-21 09:27:40 +0000 | [diff] [blame] | 434 | int ret; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 435 | |
| 436 | /* Request state should be less than max_level */ |
Daniel Lezcano | 40ea568 | 2020-03-21 20:31:07 +0100 | [diff] [blame^] | 437 | if (state > cpufreq_cdev->max_level) |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | |
| 440 | /* Check if the old cooling action is same as new cooling action */ |
| 441 | if (cpufreq_cdev->cpufreq_state == state) |
| 442 | return 0; |
| 443 | |
| 444 | cpufreq_cdev->cpufreq_state = state; |
| 445 | |
Willy Wolff | ff44f67 | 2020-03-21 09:27:40 +0000 | [diff] [blame] | 446 | ret = freq_qos_update_request(&cpufreq_cdev->qos_req, |
| 447 | get_state_freq(cpufreq_cdev, state)); |
| 448 | return ret < 0 ? ret : 0; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 449 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 450 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 451 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 452 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 453 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 454 | .get_max_state = cpufreq_get_max_state, |
| 455 | .get_cur_state = cpufreq_get_cur_state, |
| 456 | .set_cur_state = cpufreq_set_cur_state, |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 457 | }; |
| 458 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 459 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 460 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 461 | * @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] | 462 | * @policy: cpufreq policy |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 463 | * Normally this should be same as cpufreq policy->related_cpus. |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 464 | * @em: Energy Model of the cpufreq policy |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 465 | * |
| 466 | * This interface function registers the cpufreq cooling device with the name |
| 467 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 468 | * cooling devices. It also gives the opportunity to link the cooling device |
| 469 | * 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] | 470 | * |
| 471 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 472 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 473 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 474 | static struct thermal_cooling_device * |
| 475 | __cpufreq_cooling_register(struct device_node *np, |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 476 | struct cpufreq_policy *policy, |
| 477 | struct em_perf_domain *em) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 478 | { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 479 | struct thermal_cooling_device *cdev; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 480 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 481 | char dev_name[THERMAL_NAME_LENGTH]; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 482 | unsigned int i, num_cpus; |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 483 | struct device *dev; |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 484 | int ret; |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 485 | struct thermal_cooling_device_ops *cooling_ops; |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 486 | |
| 487 | dev = get_cpu_device(policy->cpu); |
| 488 | if (unlikely(!dev)) { |
| 489 | pr_warn("No cpu device for cpu %d\n", policy->cpu); |
| 490 | return ERR_PTR(-ENODEV); |
| 491 | } |
| 492 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 493 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 494 | if (IS_ERR_OR_NULL(policy)) { |
Arvind Yadav | b2fd708 | 2017-10-24 13:20:39 +0530 | [diff] [blame] | 495 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 496 | return ERR_PTR(-EINVAL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 497 | } |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 498 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 499 | i = cpufreq_table_count_valid_entries(policy); |
| 500 | if (!i) { |
| 501 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", |
| 502 | __func__); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 503 | return ERR_PTR(-ENODEV); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 504 | } |
| 505 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 506 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 507 | if (!cpufreq_cdev) |
| 508 | return ERR_PTR(-ENOMEM); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 509 | |
Viresh Kumar | b12b651 | 2017-04-25 15:57:16 +0530 | [diff] [blame] | 510 | cpufreq_cdev->policy = policy; |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 511 | num_cpus = cpumask_weight(policy->related_cpus); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 512 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
| 513 | sizeof(*cpufreq_cdev->idle_time), |
| 514 | GFP_KERNEL); |
| 515 | if (!cpufreq_cdev->idle_time) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 516 | cdev = ERR_PTR(-ENOMEM); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 517 | goto free_cdev; |
| 518 | } |
| 519 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 520 | /* max_level is an index, not a counter */ |
| 521 | cpufreq_cdev->max_level = i - 1; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 522 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 523 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
| 524 | if (ret < 0) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 525 | cdev = ERR_PTR(ret); |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 526 | goto free_idle_time; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 527 | } |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 528 | cpufreq_cdev->id = ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 529 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 530 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 531 | cpufreq_cdev->id); |
| 532 | |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 533 | cooling_ops = &cpufreq_cooling_ops; |
| 534 | |
| 535 | #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 536 | if (em_is_sane(cpufreq_cdev, em)) { |
| 537 | cpufreq_cdev->em = em; |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 538 | cooling_ops->get_requested_power = cpufreq_get_requested_power; |
| 539 | cooling_ops->state2power = cpufreq_state2power; |
| 540 | cooling_ops->power2state = cpufreq_power2state; |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 541 | } else |
Quentin Perret | 5a4e5b7 | 2019-10-30 15:14:50 +0000 | [diff] [blame] | 542 | #endif |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 543 | if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) { |
| 544 | pr_err("%s: unsorted frequency tables are not supported\n", |
| 545 | __func__); |
| 546 | cdev = ERR_PTR(-EINVAL); |
| 547 | goto remove_ida; |
| 548 | } |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 549 | |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 550 | ret = freq_qos_add_request(&policy->constraints, |
| 551 | &cpufreq_cdev->qos_req, FREQ_QOS_MAX, |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 552 | get_state_freq(cpufreq_cdev, 0)); |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 553 | if (ret < 0) { |
| 554 | pr_err("%s: Failed to add freq constraint (%d)\n", __func__, |
| 555 | ret); |
| 556 | cdev = ERR_PTR(ret); |
| 557 | goto remove_ida; |
| 558 | } |
| 559 | |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 560 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
| 561 | cooling_ops); |
| 562 | if (IS_ERR(cdev)) |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 563 | goto remove_qos_req; |
Viresh Kumar | 92e615e | 2014-12-04 09:41:51 +0530 | [diff] [blame] | 564 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 565 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 566 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 567 | mutex_unlock(&cooling_list_lock); |
| 568 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 569 | return cdev; |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 570 | |
Viresh Kumar | 5130802 | 2019-07-23 11:44:02 +0530 | [diff] [blame] | 571 | remove_qos_req: |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 572 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 573 | remove_ida: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 574 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 575 | free_idle_time: |
| 576 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 577 | free_cdev: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 578 | kfree(cpufreq_cdev); |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 579 | return cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 580 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 584 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 585 | * |
| 586 | * This interface function registers the cpufreq cooling device with the name |
| 587 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 588 | * cooling devices. |
| 589 | * |
| 590 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 591 | * on failure, it returns a corresponding ERR_PTR(). |
| 592 | */ |
| 593 | struct thermal_cooling_device * |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 594 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 595 | { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 596 | return __cpufreq_cooling_register(NULL, policy, NULL); |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 597 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 598 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 599 | |
| 600 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 601 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 602 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 603 | * |
| 604 | * This interface function registers the cpufreq cooling device with the name |
| 605 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 606 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 607 | * linked to the device tree node provided. |
| 608 | * |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 609 | * Using this function, the cooling device will implement the power |
| 610 | * extensions by using a simple cpu power model. The cpus must have |
| 611 | * registered their OPPs using the OPP library. |
| 612 | * |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 613 | * It also takes into account, if property present in policy CPU node, the |
| 614 | * static power consumed by the cpu. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 615 | * |
| 616 | * Return: a valid struct thermal_cooling_device pointer on success, |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 617 | * and NULL on failure. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 618 | */ |
| 619 | struct thermal_cooling_device * |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 620 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 621 | { |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 622 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
| 623 | struct thermal_cooling_device *cdev = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 624 | |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 625 | if (!np) { |
Daniel Lezcano | 23affa2 | 2019-12-19 23:53:17 +0100 | [diff] [blame] | 626 | pr_err("cpufreq_cooling: OF node not available for cpu%d\n", |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 627 | policy->cpu); |
| 628 | return NULL; |
| 629 | } |
| 630 | |
| 631 | if (of_find_property(np, "#cooling-cells", NULL)) { |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 632 | struct em_perf_domain *em = em_cpu_get(policy->cpu); |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 633 | |
Quentin Perret | a4e893e | 2019-10-30 15:14:51 +0000 | [diff] [blame] | 634 | cdev = __cpufreq_cooling_register(np, policy, em); |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 635 | if (IS_ERR(cdev)) { |
Daniel Lezcano | 23affa2 | 2019-12-19 23:53:17 +0100 | [diff] [blame] | 636 | 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] | 637 | policy->cpu, PTR_ERR(cdev)); |
| 638 | cdev = NULL; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | of_node_put(np); |
| 643 | return cdev; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 644 | } |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 645 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 646 | |
| 647 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 648 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 649 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 650 | * |
| 651 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 652 | */ |
| 653 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 654 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 655 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 656 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 657 | if (!cdev) |
| 658 | return; |
| 659 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 660 | cpufreq_cdev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 661 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 662 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 663 | list_del(&cpufreq_cdev->node); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 664 | mutex_unlock(&cooling_list_lock); |
| 665 | |
Daniel Lezcano | 72554a7 | 2019-04-28 11:51:05 +0200 | [diff] [blame] | 666 | thermal_cooling_device_unregister(cdev); |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 667 | freq_qos_remove_request(&cpufreq_cdev->qos_req); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 668 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 669 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 670 | kfree(cpufreq_cdev); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 671 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 672 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |