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