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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  linux/drivers/cpufreq/cpufreq_userspace.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (C)  2001 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *            (C)  2002 - 2004 Dominik Brodowski <linux@brodo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static DEFINE_MUTEX(userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * cpufreq_set - set the CPU frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @policy: pointer to policy struct where freq is being set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @freq: target frequency in kHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Sets the CPU frequency to freq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned int *setspeed = policy->governor_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	mutex_lock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (!per_cpu(cpu_is_managed, policy->cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	*setspeed = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	mutex_unlock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return sprintf(buf, "%u\n", policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	unsigned int *setspeed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	setspeed = kzalloc(sizeof(*setspeed), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	policy->governor_data = setspeed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	mutex_lock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	kfree(policy->governor_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	policy->governor_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	mutex_unlock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	unsigned int *setspeed = policy->governor_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	BUG_ON(!policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	pr_debug("started managing cpu %u\n", policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	mutex_lock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	per_cpu(cpu_is_managed, policy->cpu) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!*setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		*setspeed = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	mutex_unlock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	pr_debug("managing cpu %u stopped\n", policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	mutex_lock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	per_cpu(cpu_is_managed, policy->cpu) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	mutex_unlock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	unsigned int *setspeed = policy->governor_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mutex_lock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		 policy->cpu, policy->min, policy->max, policy->cur, *setspeed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (policy->max < *setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		__cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	else if (policy->min > *setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		__cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		__cpufreq_driver_target(policy, *setspeed, CPUFREQ_RELATION_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_unlock(&userspace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static struct cpufreq_governor cpufreq_gov_userspace = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.name		= "userspace",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	.init		= cpufreq_userspace_policy_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	.exit		= cpufreq_userspace_policy_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.start		= cpufreq_userspace_policy_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	.stop		= cpufreq_userspace_policy_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	.limits		= cpufreq_userspace_policy_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	.store_setspeed	= cpufreq_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	.show_setspeed	= show_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		"Russell King <rmk@arm.linux.org.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct cpufreq_governor *cpufreq_default_governor(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return &cpufreq_gov_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) cpufreq_governor_init(cpufreq_gov_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) cpufreq_governor_exit(cpufreq_gov_userspace);