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