Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  drivers/cpufreq/cpufreq_stats.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) struct cpufreq_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	unsigned int total_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	unsigned long long last_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned int max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	unsigned int state_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	unsigned int last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	u64 *time_in_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned int *freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	unsigned int *trans_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	/* Deferred reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int reset_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	unsigned long long reset_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void cpufreq_stats_update(struct cpufreq_stats *stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 				 unsigned long long time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long long cur_time = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	stats->time_in_state[stats->last_index] += cur_time - time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	stats->last_time = cur_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned int count = stats->max_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	memset(stats->time_in_state, 0, count * sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	memset(stats->trans_table, 0, count * count * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	stats->last_time = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	stats->total_trans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* Adjust for the time elapsed since reset was requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	WRITE_ONCE(stats->reset_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * Prevent the reset_time read from being reordered before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * reset_pending accesses in cpufreq_stats_record_transition().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (READ_ONCE(stats->reset_pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return sprintf(buf, "%d\n", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return sprintf(buf, "%u\n", stats->total_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) cpufreq_freq_attr_ro(total_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	bool pending = READ_ONCE(stats->reset_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned long long time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (i = 0; i < stats->state_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			if (i == stats->last_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				 * Prevent the reset_time read from occurring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				 * before the reset_pending read above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				time = get_jiffies_64() - READ_ONCE(stats->reset_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			time = stats->time_in_state[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			if (i == stats->last_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				time += get_jiffies_64() - stats->last_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			       jiffies_64_to_clock_t(time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) cpufreq_freq_attr_ro(time_in_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* We don't care what is written to the attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			   size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * Defer resetting of stats to cpufreq_stats_record_transition() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * avoid races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	WRITE_ONCE(stats->reset_time, get_jiffies_64());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * The memory barrier below is to prevent the readers of reset_time from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * seeing a stale or partially updated value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	WRITE_ONCE(stats->reset_pending, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) cpufreq_freq_attr_wo(reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	bool pending = READ_ONCE(stats->reset_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int i, j, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	len += scnprintf(buf + len, PAGE_SIZE - len, "   From  :    To\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	len += scnprintf(buf + len, PAGE_SIZE - len, "         : ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	for (i = 0; i < stats->state_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				stats->freq_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	for (i = 0; i < stats->state_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		len += scnprintf(buf + len, PAGE_SIZE - len, "%9u: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				stats->freq_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		for (j = 0; j < stats->state_num; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			if (pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				count = stats->trans_table[i * stats->max_state + j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		if (len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (len >= PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) cpufreq_freq_attr_ro(trans_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static struct attribute *default_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	&total_trans.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	&time_in_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	&reset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	&trans_table.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static const struct attribute_group stats_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.attrs = default_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.name = "stats"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	for (index = 0; index < stats->max_state; index++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (stats->freq_table[index] == freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void cpufreq_stats_free_table(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/* Already freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	pr_debug("%s: Free stats table\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	sysfs_remove_group(&policy->kobj, &stats_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	kfree(stats->time_in_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	kfree(stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	policy->stats = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) void cpufreq_stats_create_table(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	unsigned int i = 0, count = 0, ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct cpufreq_stats *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	unsigned int alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct cpufreq_frequency_table *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	count = cpufreq_table_count_valid_entries(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* stats already initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (policy->stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	stats = kzalloc(sizeof(*stats), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (!stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	alloc_size = count * sizeof(int) + count * sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	alloc_size += count * count * sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* Allocate memory for time_in_state/freq_table/trans_table in one go */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (!stats->time_in_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		goto free_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	stats->freq_table = (unsigned int *)(stats->time_in_state + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	stats->trans_table = stats->freq_table + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	stats->max_state = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* Find valid-unique entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (freq_table_get_index(stats, pos->frequency) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			stats->freq_table[i++] = pos->frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	stats->state_num = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	stats->last_time = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	stats->last_index = freq_table_get_index(stats, policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	policy->stats = stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* We failed, release resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	policy->stats = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	kfree(stats->time_in_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) free_stat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	kfree(stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				     unsigned int new_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct cpufreq_stats *stats = policy->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int old_index, new_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (unlikely(!stats))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (unlikely(READ_ONCE(stats->reset_pending)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		cpufreq_stats_reset_table(stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	old_index = stats->last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	new_index = freq_table_get_index(stats, new_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/* We can't do stats->time_in_state[-1]= .. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (unlikely(old_index == -1 || new_index == -1 || old_index == new_index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	cpufreq_stats_update(stats, stats->last_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	stats->last_index = new_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	stats->trans_table[old_index * stats->max_state + new_index]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	stats->total_trans++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }