Commit | Line | Data |
---|---|---|
c0672860 | 1 | |
1da177e4 LT |
2 | /* |
3 | * linux/drivers/cpufreq/cpufreq_userspace.c | |
4 | * | |
5 | * Copyright (C) 2001 Russell King | |
6 | * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | */ | |
13 | ||
db701151 VK |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | ||
1da177e4 | 16 | #include <linux/cpufreq.h> |
d1922f02 VK |
17 | #include <linux/init.h> |
18 | #include <linux/module.h> | |
3fc54d37 | 19 | #include <linux/mutex.h> |
1da177e4 | 20 | |
b38868aa | 21 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); |
1bceb8d1 | 22 | static DEFINE_MUTEX(userspace_mutex); |
1da177e4 | 23 | |
32ee8c3e | 24 | /** |
1da177e4 | 25 | * cpufreq_set - set the CPU frequency |
9e76988e | 26 | * @policy: pointer to policy struct where freq is being set |
1da177e4 | 27 | * @freq: target frequency in kHz |
1da177e4 LT |
28 | * |
29 | * Sets the CPU frequency to freq. | |
30 | */ | |
9e76988e | 31 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
1da177e4 LT |
32 | { |
33 | int ret = -EINVAL; | |
34 | ||
2d06d8c4 | 35 | pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); |
1da177e4 | 36 | |
3fc54d37 | 37 | mutex_lock(&userspace_mutex); |
b38868aa | 38 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
1da177e4 LT |
39 | goto err; |
40 | ||
1da177e4 LT |
41 | /* |
42 | * We're safe from concurrent calls to ->target() here | |
3fc54d37 | 43 | * as we hold the userspace_mutex lock. If we were calling |
1da177e4 | 44 | * cpufreq_driver_target, a deadlock situation might occur: |
1bceb8d1 DJ |
45 | * A: cpufreq_set (lock userspace_mutex) -> |
46 | * cpufreq_driver_target(lock policy->lock) | |
47 | * B: cpufreq_set_policy(lock policy->lock) -> | |
48 | * __cpufreq_governor -> | |
49 | * cpufreq_governor_userspace (lock userspace_mutex) | |
1da177e4 | 50 | */ |
c0672860 | 51 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
1da177e4 LT |
52 | |
53 | err: | |
3fc54d37 | 54 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
55 | return ret; |
56 | } | |
57 | ||
9e76988e | 58 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 59 | { |
d1922f02 | 60 | return sprintf(buf, "%u\n", policy->cur); |
1da177e4 LT |
61 | } |
62 | ||
1da177e4 LT |
63 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
64 | unsigned int event) | |
65 | { | |
66 | unsigned int cpu = policy->cpu; | |
914f7c31 JG |
67 | int rc = 0; |
68 | ||
1da177e4 LT |
69 | switch (event) { |
70 | case CPUFREQ_GOV_START: | |
1da177e4 | 71 | BUG_ON(!policy->cur); |
d1922f02 | 72 | pr_debug("started managing cpu %u\n", cpu); |
c7f652e0 | 73 | |
d1922f02 | 74 | mutex_lock(&userspace_mutex); |
b38868aa | 75 | per_cpu(cpu_is_managed, cpu) = 1; |
3fc54d37 | 76 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
77 | break; |
78 | case CPUFREQ_GOV_STOP: | |
d1922f02 | 79 | pr_debug("managing cpu %u stopped\n", cpu); |
c7f652e0 | 80 | |
d1922f02 | 81 | mutex_lock(&userspace_mutex); |
b38868aa | 82 | per_cpu(cpu_is_managed, cpu) = 0; |
3fc54d37 | 83 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
84 | break; |
85 | case CPUFREQ_GOV_LIMITS: | |
3fc54d37 | 86 | mutex_lock(&userspace_mutex); |
d1922f02 | 87 | pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n", |
c0672860 | 88 | cpu, policy->min, policy->max, |
d1922f02 VK |
89 | policy->cur); |
90 | ||
91 | if (policy->max < policy->cur) | |
c0672860 TR |
92 | __cpufreq_driver_target(policy, policy->max, |
93 | CPUFREQ_RELATION_H); | |
d1922f02 | 94 | else if (policy->min > policy->cur) |
c0672860 TR |
95 | __cpufreq_driver_target(policy, policy->min, |
96 | CPUFREQ_RELATION_L); | |
3fc54d37 | 97 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
98 | break; |
99 | } | |
914f7c31 | 100 | return rc; |
1da177e4 LT |
101 | } |
102 | ||
c4d14bc0 SW |
103 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
104 | static | |
105 | #endif | |
1da177e4 LT |
106 | struct cpufreq_governor cpufreq_gov_userspace = { |
107 | .name = "userspace", | |
108 | .governor = cpufreq_governor_userspace, | |
9e76988e VP |
109 | .store_setspeed = cpufreq_set, |
110 | .show_setspeed = show_speed, | |
1da177e4 LT |
111 | .owner = THIS_MODULE, |
112 | }; | |
1da177e4 LT |
113 | |
114 | static int __init cpufreq_gov_userspace_init(void) | |
115 | { | |
1da177e4 LT |
116 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
117 | } | |
118 | ||
1da177e4 LT |
119 | static void __exit cpufreq_gov_userspace_exit(void) |
120 | { | |
121 | cpufreq_unregister_governor(&cpufreq_gov_userspace); | |
1da177e4 LT |
122 | } |
123 | ||
1bceb8d1 DJ |
124 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
125 | "Russell King <rmk@arm.linux.org.uk>"); | |
126 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); | |
127 | MODULE_LICENSE("GPL"); | |
1da177e4 | 128 | |
6915719b | 129 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
1da177e4 | 130 | fs_initcall(cpufreq_gov_userspace_init); |
6915719b JW |
131 | #else |
132 | module_init(cpufreq_gov_userspace_init); | |
133 | #endif | |
1da177e4 | 134 | module_exit(cpufreq_gov_userspace_exit); |