3 * linux/drivers/cpufreq/cpufreq_userspace.c
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/cpufreq.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
22 static DEFINE_PER_CPU(unsigned int, cpu_is_managed
);
23 static DEFINE_MUTEX(userspace_mutex
);
26 * cpufreq_set - set the CPU frequency
27 * @policy: pointer to policy struct where freq is being set
28 * @freq: target frequency in kHz
30 * Sets the CPU frequency to freq.
32 static int cpufreq_set(struct cpufreq_policy
*policy
, unsigned int freq
)
35 unsigned int *setspeed
= policy
->governor_data
;
37 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy
->cpu
, freq
);
39 mutex_lock(&userspace_mutex
);
40 if (!per_cpu(cpu_is_managed
, policy
->cpu
))
46 * We're safe from concurrent calls to ->target() here
47 * as we hold the userspace_mutex lock. If we were calling
48 * cpufreq_driver_target, a deadlock situation might occur:
49 * A: cpufreq_set (lock userspace_mutex) ->
50 * cpufreq_driver_target(lock policy->lock)
51 * B: cpufreq_set_policy(lock policy->lock) ->
52 * __cpufreq_governor ->
53 * cpufreq_governor_userspace (lock userspace_mutex)
55 ret
= __cpufreq_driver_target(policy
, freq
, CPUFREQ_RELATION_L
);
58 mutex_unlock(&userspace_mutex
);
62 static ssize_t
show_speed(struct cpufreq_policy
*policy
, char *buf
)
64 return sprintf(buf
, "%u\n", policy
->cur
);
67 static int cpufreq_userspace_policy_init(struct cpufreq_policy
*policy
)
69 unsigned int *setspeed
;
71 setspeed
= kzalloc(sizeof(*setspeed
), GFP_KERNEL
);
75 policy
->governor_data
= setspeed
;
79 static int cpufreq_governor_userspace(struct cpufreq_policy
*policy
,
82 unsigned int *setspeed
= policy
->governor_data
;
83 unsigned int cpu
= policy
->cpu
;
86 if (event
== CPUFREQ_GOV_POLICY_INIT
)
87 return cpufreq_userspace_policy_init(policy
);
93 case CPUFREQ_GOV_POLICY_EXIT
:
94 mutex_lock(&userspace_mutex
);
95 policy
->governor_data
= NULL
;
97 mutex_unlock(&userspace_mutex
);
99 case CPUFREQ_GOV_START
:
100 BUG_ON(!policy
->cur
);
101 pr_debug("started managing cpu %u\n", cpu
);
103 mutex_lock(&userspace_mutex
);
104 per_cpu(cpu_is_managed
, cpu
) = 1;
105 *setspeed
= policy
->cur
;
106 mutex_unlock(&userspace_mutex
);
108 case CPUFREQ_GOV_STOP
:
109 pr_debug("managing cpu %u stopped\n", cpu
);
111 mutex_lock(&userspace_mutex
);
112 per_cpu(cpu_is_managed
, cpu
) = 0;
114 mutex_unlock(&userspace_mutex
);
116 case CPUFREQ_GOV_LIMITS
:
117 mutex_lock(&userspace_mutex
);
118 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
119 cpu
, policy
->min
, policy
->max
, policy
->cur
, *setspeed
);
121 if (policy
->max
< *setspeed
)
122 __cpufreq_driver_target(policy
, policy
->max
,
124 else if (policy
->min
> *setspeed
)
125 __cpufreq_driver_target(policy
, policy
->min
,
128 __cpufreq_driver_target(policy
, *setspeed
,
130 mutex_unlock(&userspace_mutex
);
136 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
139 struct cpufreq_governor cpufreq_gov_userspace
= {
141 .governor
= cpufreq_governor_userspace
,
142 .store_setspeed
= cpufreq_set
,
143 .show_setspeed
= show_speed
,
144 .owner
= THIS_MODULE
,
147 static int __init
cpufreq_gov_userspace_init(void)
149 return cpufreq_register_governor(&cpufreq_gov_userspace
);
152 static void __exit
cpufreq_gov_userspace_exit(void)
154 cpufreq_unregister_governor(&cpufreq_gov_userspace
);
157 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
158 "Russell King <rmk@arm.linux.org.uk>");
159 MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
160 MODULE_LICENSE("GPL");
162 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
163 fs_initcall(cpufreq_gov_userspace_init
);
165 module_init(cpufreq_gov_userspace_init
);
167 module_exit(cpufreq_gov_userspace_exit
);