| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
|
| 3 |
/* |
| 4 |
* linux/drivers/cpufreq/cpufreq_userspace.c |
| 5 |
* |
| 6 |
* Copyright (C) 2001 Russell King |
| 7 |
* (C) 2002 - 2004 Dominik Brodowski <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 |
|
| 12 |
#include <linux/cpufreq.h> |
| 13 |
#include <linux/init.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/mutex.h> |
| 16 |
#include <linux/slab.h> |
| 17 |
|
| 18 |
struct userspace_policy { |
| 19 |
unsigned int is_managed; |
| 20 |
unsigned int setspeed; |
| 21 |
struct mutex mutex; |
| 22 |
}; |
| 23 |
|
| 24 |
/** |
| 25 |
* cpufreq_set - set the CPU frequency |
| 26 |
* @policy: pointer to policy struct where freq is being set |
| 27 |
* @freq: target frequency in kHz |
| 28 |
* |
| 29 |
* Sets the CPU frequency to freq. |
| 30 |
*/ |
| 31 |
static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
| 32 |
{ |
| 33 |
int ret = -EINVAL; |
| 34 |
struct userspace_policy *userspace = policy->governor_data; |
| 35 |
|
| 36 |
pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); |
| 37 |
|
| 38 |
mutex_lock(&userspace->mutex); |
| 39 |
if (!userspace->is_managed) |
| 40 |
goto err; |
| 41 |
|
| 42 |
userspace->setspeed = freq; |
| 43 |
|
| 44 |
ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
| 45 |
err: |
| 46 |
mutex_unlock(&userspace->mutex); |
| 47 |
return ret; |
| 48 |
} |
| 49 |
|
| 50 |
static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
| 51 |
{ |
| 52 |
struct userspace_policy *userspace = policy->governor_data; |
| 53 |
|
| 54 |
return sprintf(buf, "%u\n", userspace->setspeed); |
| 55 |
} |
| 56 |
|
| 57 |
static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy) |
| 58 |
{ |
| 59 |
struct userspace_policy *userspace; |
| 60 |
|
| 61 |
userspace = kzalloc_obj(*userspace); |
| 62 |
if (!userspace) |
| 63 |
return -ENOMEM; |
| 64 |
|
| 65 |
mutex_init(&userspace->mutex); |
| 66 |
|
| 67 |
policy->governor_data = userspace; |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* Any routine that writes to the policy struct will hold the "rwsem" of |
| 73 |
* policy struct that means it is free to free "governor_data" here. |
| 74 |
*/ |
| 75 |
static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy) |
| 76 |
{ |
| 77 |
kfree(policy->governor_data); |
| 78 |
policy->governor_data = NULL; |
| 79 |
} |
| 80 |
|
| 81 |
static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy) |
| 82 |
{ |
| 83 |
struct userspace_policy *userspace = policy->governor_data; |
| 84 |
|
| 85 |
BUG_ON(!policy->cur); |
| 86 |
pr_debug("started managing cpu %u\n", policy->cpu); |
| 87 |
|
| 88 |
mutex_lock(&userspace->mutex); |
| 89 |
userspace->is_managed = 1; |
| 90 |
userspace->setspeed = policy->cur; |
| 91 |
mutex_unlock(&userspace->mutex); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy) |
| 96 |
{ |
| 97 |
struct userspace_policy *userspace = policy->governor_data; |
| 98 |
|
| 99 |
pr_debug("managing cpu %u stopped\n", policy->cpu); |
| 100 |
|
| 101 |
mutex_lock(&userspace->mutex); |
| 102 |
userspace->is_managed = 0; |
| 103 |
userspace->setspeed = 0; |
| 104 |
mutex_unlock(&userspace->mutex); |
| 105 |
} |
| 106 |
|
| 107 |
static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy) |
| 108 |
{ |
| 109 |
struct userspace_policy *userspace = policy->governor_data; |
| 110 |
|
| 111 |
mutex_lock(&userspace->mutex); |
| 112 |
|
| 113 |
pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n", |
| 114 |
policy->cpu, policy->min, policy->max, policy->cur, userspace->setspeed); |
| 115 |
|
| 116 |
if (policy->max < userspace->setspeed) |
| 117 |
__cpufreq_driver_target(policy, policy->max, |
| 118 |
CPUFREQ_RELATION_H); |
| 119 |
else if (policy->min > userspace->setspeed) |
| 120 |
__cpufreq_driver_target(policy, policy->min, |
| 121 |
CPUFREQ_RELATION_L); |
| 122 |
else |
| 123 |
__cpufreq_driver_target(policy, userspace->setspeed, |
| 124 |
CPUFREQ_RELATION_L); |
| 125 |
|
| 126 |
mutex_unlock(&userspace->mutex); |
| 127 |
} |
| 128 |
|
| 129 |
static struct cpufreq_governor cpufreq_gov_userspace = { |
| 130 |
.name = "userspace", |
| 131 |
.init = cpufreq_userspace_policy_init, |
| 132 |
.exit = cpufreq_userspace_policy_exit, |
| 133 |
.start = cpufreq_userspace_policy_start, |
| 134 |
.stop = cpufreq_userspace_policy_stop, |
| 135 |
.limits = cpufreq_userspace_policy_limits, |
| 136 |
.store_setspeed = cpufreq_set, |
| 137 |
.show_setspeed = show_speed, |
| 138 |
.owner = THIS_MODULE, |
| 139 |
.flags = CPUFREQ_GOV_STRICT_TARGET, |
| 140 |
}; |
| 141 |
|
| 142 |
MODULE_AUTHOR("Dominik Brodowski <[email protected]>, " |
| 143 |
"Russell King <[email protected]>"); |
| 144 |
MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 145 |
MODULE_LICENSE("GPL"); |
| 146 |
|
| 147 |
#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
| 148 |
struct cpufreq_governor *cpufreq_default_governor(void) |
| 149 |
{ |
| 150 |
return &cpufreq_gov_userspace; |
| 151 |
} |
| 152 |
#endif |
| 153 |
|
| 154 |
cpufreq_governor_init(cpufreq_gov_userspace); |
| 155 |
cpufreq_governor_exit(cpufreq_gov_userspace); |