Thermal: cpu cooling fix
Subsystems (3)
Zhang Rui
reviewercover Thermal: cpu cooling fix
the per_cpu cpufreq_frequency_table contains all the frequency entries which are reported by platform code.
But this table is not clean enough because it may contain invalid, duplicate entries, and it may be in either descending or asceding order.
converting between cpu frequency and cooling state using this table in cpufreq cooling code is painful as each cooling state of cpu cooling device must have a valid and unique frequency.
thus cpu cooling table is introduced in this patch set. CPU cooling table is a table that
1. each entry represents a cpu cooling state,
aka, a different cpu frequency.
2. no invalid entry in this table
3. no duplicate entry in this table
4. the frequency of all the entries are in descending order, aka, table[0] equals maximum frequency and cooling state 0.
And this is the table we are using inside cpufreq cooling generic code.
Besides, all the cpufreq cooling users, e.g. exynos, should touch neither per_cpu cpufreq_frequency_table nor the cpu cooling table. it should invoke cpufreq cooling API to do cooling_state/frequency transition.
I just run build test here. It would be great that some of you can test it on exynos.
any comments?
thanks,
rui
Zhang Rui
reviewerPATCH 1/5 Thermal: fix a wrong comment
"level" parameter of get_cpu_frequency equals cooling state of cpu cooling device, and it starts from 0.
Fix the misleading comment.
--- drivers/thermal/cpu_cooling.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/thermal/cpu_cooling.c +2 -2
@@ -118,8 +118,8 @@ static int is_cpufreq_valid(int cpu)/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched. -* @level: level of frequency of the CPU -* e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc +* @level: level of frequency, equals cooling state of cpu cooling device +* e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc */ static unsigned int get_cpu_frequency (unsigned int cpu,unsigned long level) {
Signature · 1 line
1.7.9.5
Zhang Rui
reviewerPATCH 2/5 Thermal: Introduce cpu cooling table
In reply to Zhang RuiCPU cooling table is a cleaned cpufreq_frequency_table that
1. each entry represents a cooling state,
aka, each entry has a different frequency.
2. does not have invalid entry.
3. does not have duplicate entry.
4. the frequencies of all the entries are in descending order.
we should use this table inside thermal layer and thermal drivers.
--- drivers/thermal/cpu_cooling.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
drivers/thermal/cpu_cooling.c +63 -0
@@ -115,6 +115,69 @@ static int is_cpufreq_valid(int cpu)return !cpufreq_get_policy (&policy, cpu); } +/* +* get the cleaned cpufreq_frequency_table that +* 1) does not have invalid entries +* 2) does not have duplicate entries +* 3) the frequency of the entries are in descending order +*/ +static struct cpufreq_frequency_table * +get_cpu_cooling_table (unsigned int cpu) +{ +int i =0 ; +int level; +struct cpufreq_frequency_table *old, *new; +unsigned int freq =CPUFREQ_ENTRY_INVALID ; +int descend =-1 ; + + old =cpufreq_frequency_get_table (cpu); +if (!old) +return ERR_PTR (-EINVAL ); + +while (old[i].frequency !=CPUFREQ_TABLE_END ) + i++; + + i++;/* one more entry for CPUFREQ_TABLE_END */ + + new =kzalloc (i *sizeof (struct cpufreq_frequency_table ),GFP_KERNEL ); +if (!new) +return ERR_PTR (-ENOMEM ); + +for (i =0 , level =0 ; old[i].frequency !=CPUFREQ_TABLE_END ; i++) { +/* ignore invalid entry */ +if (old[i].frequency ==CPUFREQ_ENTRY_INVALID ) +continue ; + +/* ignore duplicate entry */ +if (freq == old[i].frequency) +continue ; + +/* found an valid entry */ + new[level].frequency = old[i].frequency; + +if (freq !=CPUFREQ_ENTRY_INVALID && descend ==-1 ) + descend = !!(freq > old[i].frequency); + +/* freq always equals the last valid frequency */ + freq = new[level].frequency; + + level++; + } + new[level].frequency =CPUFREQ_ENTRY_INVALID ; + +/* convert to decending if in ascending order */ +if (!descend) { +for (i =0 ; i < (level - i -1 ); i++) { +int j = level - i -1 ; + freq = new[i].frequency; + new[i].frequency = new[j].frequency; + new[j].frequency = freq; + } + } + +return new; +} +/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched.
Signature · 1 line
1.7.9.5
Zhang Rui
reviewerPATCH 3/5 Thermal: Get cpu frequency via cpu cooling table
In reply to Zhang Rui--- drivers/thermal/cpu_cooling.c | 45 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 37 deletions(-)
drivers/thermal/cpu_cooling.c +8 -37
@@ -186,46 +186,17 @@ get_cpu_cooling_table(unsigned int cpu)*/ static unsigned int get_cpu_frequency (unsigned int cpu,unsigned long level) { -int ret =0 , i =0 ; -unsigned long level_index; -bool descend = false; -struct cpufreq_frequency_table *table = -cpufreq_frequency_get_table (cpu); -if (!table) -return ret; - -while (table[i].frequency !=CPUFREQ_TABLE_END ) { -if (table[i].frequency ==CPUFREQ_ENTRY_INVALID ) -continue ; - -/*check if table in ascending or descending order*/ -if ((table[i +1 ].frequency !=CPUFREQ_TABLE_END ) && - (table[i +1 ].frequency < table[i].frequency) - && !descend) { - descend = true; - } +struct cpufreq_frequency_table *table; +unsigned int freq; -/*return if level matched and table in descending order*/ -if (descend && i == level) -return table[i].frequency; - i++; - } - i--; + table =get_cpu_cooling_table (cpu); +if (IS_ERR (table)) +return -EINVAL ; -if (level > i || descend) -return ret; - level_index = i - level; + freq = table[level].frequency; -/*Scan the table in reverse order and match the level*/ -while (i >=0 ) { -if (table[i].frequency ==CPUFREQ_ENTRY_INVALID ) -continue ; -/*return if level matched*/ -if (i == level_index) -return table[i].frequency; - i--; - } -return ret; +kfree (table); +return 0 ; }/**
Signature · 1 line
1.7.9.5
Zhang Rui
reviewerPATCH 4/5 Thermal: Get max cpu cooling states via cpu cooling table
In reply to Zhang Rui--- drivers/thermal/cpu_cooling.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-)
drivers/thermal/cpu_cooling.c +10 -14
@@ -277,26 +277,22 @@ static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, {struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;struct cpumask *maskPtr = &cpufreq_device->allowed_cpus; -unsigned int cpu;struct cpufreq_frequency_table *table; -unsigned long count =0 ; +unsigned int cpu;int i =0 ; cpu =cpumask_any (maskPtr); - table =cpufreq_frequency_get_table (cpu); -if (!table) { - *state =0 ; -return 0 ; - } + table =get_cpu_cooling_table (cpu); +if (IS_ERR (table)) +return -EINVAL ; -for (i =0 ; (table[i].frequency !=CPUFREQ_TABLE_END ); i++) { -if (table[i].frequency ==CPUFREQ_ENTRY_INVALID ) -continue ; - count++; - } +while (table[i].frequency !=CPUFREQ_TABLE_END ) + i++; + +kfree (table); -if (count >0 ) { - *state = --count; +if (i >0 ) { + *state = --i;return 0 ; }
Signature · 1 line
1.7.9.5
Zhang Rui
reviewerPATCH 5/5 Thermal: Get cooling state via cpu cooling table
In reply to Zhang Rui--- drivers/thermal/cpu_cooling.c | 22 ++++++++++++++++++++++ drivers/thermal/exynos_thermal.c | 24 ++---------------------- include/linux/cpu_cooling.h | 6 ++++++ include/linux/thermal.h | 4 +++- 4 files changed, 33 insertions(+), 23 deletions(-)
drivers/thermal/cpu_cooling.c +22 -0
@@ -178,6 +178,28 @@ get_cpu_cooling_table(unsigned int cpu)return new; } +unsigned long cpufreq_cooling_get_level (unsigned int cpu, +unsigned int freq) +{ +struct cpufreq_frequency_table *table; +int i; +unsigned long level =THERMAL_COOLING_STATE_INVALID ; + + table =get_cpu_cooling_table (cpu); +if (IS_ERR (table)) +return -EINVAL ; + +for (i =0 ; table[i].frequency !=CPUFREQ_TABLE_END ; i++) +if (freq == table[i].frequency) { + level = (unsigned long )i; +break ; + } + +kfree (table); +return level; +} +EXPORT_SYMBOL (cpufreq_cooling_get_level); +/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched.
drivers/thermal/exynos_thermal.c +2 -22
@@ -239,26 +239,6 @@ static int exynos_get_crit_temp(struct thermal_zone_device *thermal,return ret; } -static int exynos_get_frequency_level (unsigned int cpu,unsigned int freq) -{ -int i =0 , ret = -EINVAL ; -struct cpufreq_frequency_table *table =NULL ; -#ifdef CONFIG_CPU_FREQ - table =cpufreq_frequency_get_table (cpu); -#endif -if (!table) -return ret; - -while (table[i].frequency !=CPUFREQ_TABLE_END ) { -if (table[i].frequency ==CPUFREQ_ENTRY_INVALID ) -continue ; -if (table[i].frequency == freq) -return i; - i++; - } -return ret; -} -/* Bind callback functions for thermal zone */ static int exynos_bind (struct thermal_zone_device *thermal,struct thermal_cooling_device *cdev)
@@ -285,8 +265,8 @@ static int exynos_bind(struct thermal_zone_device *thermal,/* Bind the thermal zone to the cpufreq cooling device */ for (i =0 ; i < tab_size; i++) { clip_data = (struct freq_clip_table *)&(tab_ptr[i]); - level =exynos_get_frequency_level (0 , clip_data->freq_clip_max); -if (level <0 ) + level =cpufreq_cooling_get_level (0 , clip_data->freq_clip_max); +if (level ==THERMAL_COOLING_STATE_INVALID )return 0 ;switch (GET_ZONE (i)) {case MONITOR_ZONE :
include/linux/cpu_cooling.h +6 -0
@@ -42,6 +42,7 @@ struct thermal_cooling_device *cpufreq_cooling_register(* @cdev: thermal cooling device pointer. */ void cpufreq_cooling_unregister (struct thermal_cooling_device *cdev); +unsigned long cpufreq_cooling_get_level (unsigned int ,unsigned int );#else /* !CONFIG_CPU_THERMAL */ static inline struct thermal_cooling_device *cpufreq_cooling_register (const struct cpumask *clip_cpus)
@@ -53,6 +54,11 @@ static inline void cpufreq_cooling_unregister( {return ; } +static inline unsigned long cpufreq_cooling_get_level ( +unsigned int cpu,unsigned int freq) +{ +return THERMAL_COOLING_STATE_INVALID ; +}#endif /* CONFIG_CPU_THERMAL */ #endif /* __CPU_COOLING_H__ */
include/linux/thermal.h +3 -1
@@ -33,8 +33,10 @@#define THERMAL_MAX_TRIPS 12#define THERMAL_NAME_LENGTH 20 +#define THERMAL_COOLING_STATE_INVALID -1UL +/* No upper/lower limit requirement */ -#define THERMAL_NO_LIMIT -1UL +#define THERMAL_NO_LIMIT THERMAL_COOLING_STATE_INVALID/* Unit conversion macros */ #define KELVIN_TO_CELSIUS (t) (long)(((long)t-2732 >= 0) ? \
Signature · 1 line
1.7.9.5
Re: [PATCH 2/5] Thermal: Introduce cpu cooling table
In reply to Zhang RuiQuoted 7 lines · Zhang Rui
CPU cooling table is a cleaned
cpufreq_frequency_tablethat
1. each entry represents a cooling state,
aka, each entry has a different frequency.
2. does not have invalid entry.
3. does not have duplicate entry.
4. the frequencies of all the entries are in descending order.
This is a good idea, but we should consider more details, one tough issue is invalid frequencies:
1. some freq is invalid while freq table is creating, but after that the invalid freq becomes valid, we will miss cooling states.
2. vice versa, all the freq is valid while freq table is creating, but become invalid later, thus some cooling state isn't valid either, should we consider such case?
In fact, these should be considered even if this patch isn't sent out.
(We are assuming that all the CPU cores run at same freq and should be adjust freq simultaneously, if no affinity between CPU cores, it will be more complicated -- this seems not proper to be discussed in this patch)
Quoted 84 lines
we should use this table inside thermal layer and thermal drivers.
Signed-off-by Zhang Rui <[email protected]>--- drivers/thermal/cpu_cooling.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)drivers/thermal/cpu_cooling.c +63
-1@@ -115,6 +115,69 @@ static int is_cpufreq_valid(int cpu)return !cpufreq_get_policy (&policy, cpu); } +/* +* get the cleaned cpufreq_frequency_table that +* 1) does not have invalid entries +* 2) does not have duplicate entries +* 3) the frequency of the entries are in descending order +*/ +static struct cpufreq_frequency_table * +get_cpu_cooling_table (unsigned int cpu) +{ +int i =0 ; +int level; +struct cpufreq_frequency_table *old, *new; +unsigned int freq =CPUFREQ_ENTRY_INVALID ; +int descend =-1 ; + + old =cpufreq_frequency_get_table (cpu); +if (!old) +return ERR_PTR (-EINVAL ); + +while (old[i].frequency !=CPUFREQ_TABLE_END ) + i++; + + i++;/* one more entry for CPUFREQ_TABLE_END */ + + new =kzalloc (i *sizeof (struct cpufreq_frequency_table ),GFP_KERNEL ); +if (!new) +return ERR_PTR (-ENOMEM ); + +for (i =0 , level =0 ; old[i].frequency !=CPUFREQ_TABLE_END ; i++) { +/* ignore invalid entry */ +if (old[i].frequency ==CPUFREQ_ENTRY_INVALID ) +continue ; + +/* ignore duplicate entry */ +if (freq == old[i].frequency) +continue ; + +/* found an valid entry */ + new[level].frequency = old[i].frequency; + +if (freq !=CPUFREQ_ENTRY_INVALID && descend ==-1 ) + descend = !!(freq > old[i].frequency); + +/* freq always equals the last valid frequency */ + freq = new[level].frequency; + + level++; + } + new[level].frequency =CPUFREQ_ENTRY_INVALID ; + +/* convert to decending if in ascending order */ +if (!descend) { +for (i =0 ; i < (level - i -1 ); i++) { +int j = level - i -1 ; + freq = new[i].frequency; + new[i].frequency = new[j].frequency; + new[j].frequency = freq; + } + } + +return new; +} +/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched. -- 1.7.9.5
Zhang Rui
reviewerRe: [PATCH 2/5] Thermal: Introduce cpu cooling table
Quoted 8 lines · Hongbo Zhang
Quoted 7 lines · Zhang Rui
CPU cooling table is a cleaned
cpufreq_frequency_tablethat
1. each entry represents a cooling state,
aka, each entry has a different frequency.
2. does not have invalid entry.
3. does not have duplicate entry.
4. the frequencies of all the entries are in descending order.
Quoted 4 lines
This is a good idea, but we should consider more details, one tough issue is invalid frequencies:
1. some freq is invalid while freq table is creating, but after that the invalid freq becomes valid, we will miss cooling states.
when will the invalid freq becomes valid?
for ACPI platforms, the number of valid cpu frequency should not change at runtime, but BIOS will indicate that only several of them can be used dynamically.
I'm not sure if this may behave differently on other platforms. if it is true, the max_state and cur_state may change from time to time. and we should cache current frequency rather than cur_state in cpu cooling code.
Quoted 11 lines
2. vice versa, all the freq is valid while freq table is creating, but become invalid later, thus some cooling state isn't valid either, should we consider such case?
In fact, these should be considered even if this patch isn't sent out.
(We are assuming that all the CPU cores run at same freq and should be adjust freq simultaneously, if no affinity between CPU cores, it will be more complicated -- this seems not proper to be discussed in this patch)
Agreed.
But I'll try to fix the duplicate/invalid entries and ascending order issue in this patch set.
thanks,
rui
Quoted 84 lines
Quoted 84 lines
we should use this table inside thermal layer and thermal drivers.
Signed-off-by Zhang Rui <[email protected]>--- drivers/thermal/cpu_cooling.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)drivers/thermal/cpu_cooling.c +63
-1@@ -115,6 +115,69 @@ static int is_cpufreq_valid(int cpu)return !cpufreq_get_policy (&policy, cpu); } +/* +* get the cleaned cpufreq_frequency_table that +* 1) does not have invalid entries +* 2) does not have duplicate entries +* 3) the frequency of the entries are in descending order +*/ +static struct cpufreq_frequency_table * +get_cpu_cooling_table (unsigned int cpu) +{ +int i =0 ; +int level; +struct cpufreq_frequency_table *old, *new; +unsigned int freq =CPUFREQ_ENTRY_INVALID ; +int descend =-1 ; + + old =cpufreq_frequency_get_table (cpu); +if (!old) +return ERR_PTR (-EINVAL ); + +while (old[i].frequency !=CPUFREQ_TABLE_END ) + i++; + + i++;/* one more entry for CPUFREQ_TABLE_END */ + + new =kzalloc (i *sizeof (struct cpufreq_frequency_table ),GFP_KERNEL ); +if (!new) +return ERR_PTR (-ENOMEM ); + +for (i =0 , level =0 ; old[i].frequency !=CPUFREQ_TABLE_END ; i++) { +/* ignore invalid entry */ +if (old[i].frequency ==CPUFREQ_ENTRY_INVALID ) +continue ; + +/* ignore duplicate entry */ +if (freq == old[i].frequency) +continue ; + +/* found an valid entry */ + new[level].frequency = old[i].frequency; + +if (freq !=CPUFREQ_ENTRY_INVALID && descend ==-1 ) + descend = !!(freq > old[i].frequency); + +/* freq always equals the last valid frequency */ + freq = new[level].frequency; + + level++; + } + new[level].frequency =CPUFREQ_ENTRY_INVALID ; + +/* convert to decending if in ascending order */ +if (!descend) { +for (i =0 ; i < (level - i -1 ); i++) { +int j = level - i -1 ; + freq = new[i].frequency; + new[i].frequency = new[j].frequency; + new[j].frequency = freq; + } + } + +return new; +} +/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched. -- 1.7.9.5
Re: [PATCH 2/5] Thermal: Introduce cpu cooling table
Quoted 14 lines · Zhang Rui
Quoted 8 lines · Hongbo Zhang
Quoted 7 lines · Zhang Rui
CPU cooling table is a cleaned
cpufreq_frequency_tablethat
1. each entry represents a cooling state,
aka, each entry has a different frequency.
2. does not have invalid entry.
3. does not have duplicate entry.
4. the frequencies of all the entries are in descending order.Quoted 4 lines
This is a good idea, but we should consider more details, one tough issue is invalid frequencies:
1. some freq is invalid while freq table is creating, but after that the invalid freq becomes valid, we will miss cooling states.
Quoted 9 lines
when will the invalid freq becomes valid?
for ACPI platforms, the number of valid cpu frequency should not change at runtime, but BIOS will indicate that only several of them can be used dynamically.
I'm not sure if this may behave differently on other platforms. if it is true, themax_stateandcur_statemay change from time to time. and we should cache current frequency rather thancur_statein cpu cooling code.
Honestly, I am not expert of cpufreq, just know that we can set scaling_max_freq and scaling_min_freq under /sys/devices/system/cpu/cpu*/cpufreq/, so the cpufreq scaling range can be changed.
Correct me if I am wrong.
Quoted 104 lines
Quoted 11 lines
2. vice versa, all the freq is valid while freq table is creating, but become invalid later, thus some cooling state isn't valid either, should we consider such case?
In fact, these should be considered even if this patch isn't sent out.
(We are assuming that all the CPU cores run at same freq and should be adjust freq simultaneously, if no affinity between CPU cores, it will be more complicated -- this seems not proper to be discussed in this patch)
Agreed.
But I'll try to fix the duplicate/invalid entries and ascending order issue in this patch set.thanks,
ruiQuoted 84 lines
Quoted 84 lines
we should use this table inside thermal layer and thermal drivers.
Signed-off-by Zhang Rui <[email protected]>--- drivers/thermal/cpu_cooling.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)drivers/thermal/cpu_cooling.c +63
-1@@ -115,6 +115,69 @@ static int is_cpufreq_valid(int cpu)return !cpufreq_get_policy (&policy, cpu); } +/* +* get the cleaned cpufreq_frequency_table that +* 1) does not have invalid entries +* 2) does not have duplicate entries +* 3) the frequency of the entries are in descending order +*/ +static struct cpufreq_frequency_table * +get_cpu_cooling_table (unsigned int cpu) +{ +int i =0 ; +int level; +struct cpufreq_frequency_table *old, *new; +unsigned int freq =CPUFREQ_ENTRY_INVALID ; +int descend =-1 ; + + old =cpufreq_frequency_get_table (cpu); +if (!old) +return ERR_PTR (-EINVAL ); + +while (old[i].frequency !=CPUFREQ_TABLE_END ) + i++; + + i++;/* one more entry for CPUFREQ_TABLE_END */ + + new =kzalloc (i *sizeof (struct cpufreq_frequency_table ),GFP_KERNEL ); +if (!new) +return ERR_PTR (-ENOMEM ); + +for (i =0 , level =0 ; old[i].frequency !=CPUFREQ_TABLE_END ; i++) { +/* ignore invalid entry */ +if (old[i].frequency ==CPUFREQ_ENTRY_INVALID ) +continue ; + +/* ignore duplicate entry */ +if (freq == old[i].frequency) +continue ; + +/* found an valid entry */ + new[level].frequency = old[i].frequency; + +if (freq !=CPUFREQ_ENTRY_INVALID && descend ==-1 ) + descend = !!(freq > old[i].frequency); + +/* freq always equals the last valid frequency */ + freq = new[level].frequency; + + level++; + } + new[level].frequency =CPUFREQ_ENTRY_INVALID ; + +/* convert to decending if in ascending order */ +if (!descend) { +for (i =0 ; i < (level - i -1 ); i++) { +int j = level - i -1 ; + freq = new[i].frequency; + new[i].frequency = new[j].frequency; + new[j].frequency = freq; + } + } + +return new; +} +/** * get_cpu_frequency - get the absolute value of frequency from level. * @cpu: cpu for which frequency is fetched. -- 1.7.9.5