xref: /openbmc/linux/drivers/cpufreq/cpufreq_userspace.c (revision db7011516cbfc3d867b77721f77258d36cfbf705)
1c0672860SThomas Renninger 
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq_userspace.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C)  2001 Russell King
61da177e4SLinus Torvalds  *            (C)  2002 - 2004 Dominik Brodowski <linux@brodo.de>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
101da177e4SLinus Torvalds  * published by the Free Software Foundation.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
14*db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15*db701151SViresh Kumar 
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/smp.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/spinlock.h>
211da177e4SLinus Torvalds #include <linux/interrupt.h>
221da177e4SLinus Torvalds #include <linux/cpufreq.h>
23153d7f3fSArjan van de Ven #include <linux/cpu.h>
241da177e4SLinus Torvalds #include <linux/types.h>
251da177e4SLinus Torvalds #include <linux/fs.h>
261da177e4SLinus Torvalds #include <linux/sysfs.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /**
301da177e4SLinus Torvalds  * A few values needed by the userspace governor
311da177e4SLinus Torvalds  */
32b38868aaSMike Travis static DEFINE_PER_CPU(unsigned int, cpu_max_freq);
33b38868aaSMike Travis static DEFINE_PER_CPU(unsigned int, cpu_min_freq);
34b38868aaSMike Travis static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */
35b38868aaSMike Travis static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by
36b38868aaSMike Travis 							userspace */
37b38868aaSMike Travis static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
381da177e4SLinus Torvalds 
393fc54d37Sakpm@osdl.org static DEFINE_MUTEX(userspace_mutex);
40c7f652e0SVenki Pallipadi static int cpus_using_userspace_governor;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* keep track of frequency transitions */
431da177e4SLinus Torvalds static int
441da177e4SLinus Torvalds userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
451da177e4SLinus Torvalds 	void *data)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	struct cpufreq_freqs *freq = data;
481da177e4SLinus Torvalds 
49b38868aaSMike Travis 	if (!per_cpu(cpu_is_managed, freq->cpu))
50c7f652e0SVenki Pallipadi 		return 0;
51c7f652e0SVenki Pallipadi 
52226dd019SAfzal Mohammed 	if (val == CPUFREQ_POSTCHANGE) {
532d06d8c4SDominik Brodowski 		pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n",
54c7f652e0SVenki Pallipadi 				freq->cpu, freq->new);
55b38868aaSMike Travis 		per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
56226dd019SAfzal Mohammed 	}
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	return 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static struct notifier_block userspace_cpufreq_notifier_block = {
621da177e4SLinus Torvalds 	.notifier_call  = userspace_cpufreq_notifier
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /**
671da177e4SLinus Torvalds  * cpufreq_set - set the CPU frequency
689e76988eSVenki Pallipadi  * @policy: pointer to policy struct where freq is being set
691da177e4SLinus Torvalds  * @freq: target frequency in kHz
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * Sets the CPU frequency to freq.
721da177e4SLinus Torvalds  */
739e76988eSVenki Pallipadi static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	int ret = -EINVAL;
761da177e4SLinus Torvalds 
772d06d8c4SDominik Brodowski 	pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
781da177e4SLinus Torvalds 
793fc54d37Sakpm@osdl.org 	mutex_lock(&userspace_mutex);
80b38868aaSMike Travis 	if (!per_cpu(cpu_is_managed, policy->cpu))
811da177e4SLinus Torvalds 		goto err;
821da177e4SLinus Torvalds 
83b38868aaSMike Travis 	per_cpu(cpu_set_freq, policy->cpu) = freq;
841da177e4SLinus Torvalds 
85b38868aaSMike Travis 	if (freq < per_cpu(cpu_min_freq, policy->cpu))
86b38868aaSMike Travis 		freq = per_cpu(cpu_min_freq, policy->cpu);
87b38868aaSMike Travis 	if (freq > per_cpu(cpu_max_freq, policy->cpu))
88b38868aaSMike Travis 		freq = per_cpu(cpu_max_freq, policy->cpu);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/*
911da177e4SLinus Torvalds 	 * We're safe from concurrent calls to ->target() here
923fc54d37Sakpm@osdl.org 	 * as we hold the userspace_mutex lock. If we were calling
931da177e4SLinus Torvalds 	 * cpufreq_driver_target, a deadlock situation might occur:
941bceb8d1SDave Jones 	 * A: cpufreq_set (lock userspace_mutex) ->
951bceb8d1SDave Jones 	 *      cpufreq_driver_target(lock policy->lock)
961bceb8d1SDave Jones 	 * B: cpufreq_set_policy(lock policy->lock) ->
971bceb8d1SDave Jones 	 *      __cpufreq_governor ->
981bceb8d1SDave Jones 	 *         cpufreq_governor_userspace (lock userspace_mutex)
991da177e4SLinus Torvalds 	 */
100c0672860SThomas Renninger 	ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds  err:
1033fc54d37Sakpm@osdl.org 	mutex_unlock(&userspace_mutex);
1041da177e4SLinus Torvalds 	return ret;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
1091da177e4SLinus Torvalds {
110b38868aaSMike Travis 	return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
1141da177e4SLinus Torvalds 				   unsigned int event)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	unsigned int cpu = policy->cpu;
117914f7c31SJeff Garzik 	int rc = 0;
118914f7c31SJeff Garzik 
1191da177e4SLinus Torvalds 	switch (event) {
1201da177e4SLinus Torvalds 	case CPUFREQ_GOV_START:
1211da177e4SLinus Torvalds 		if (!cpu_online(cpu))
1221da177e4SLinus Torvalds 			return -EINVAL;
1231da177e4SLinus Torvalds 		BUG_ON(!policy->cur);
1243fc54d37Sakpm@osdl.org 		mutex_lock(&userspace_mutex);
125914f7c31SJeff Garzik 
126c7f652e0SVenki Pallipadi 		if (cpus_using_userspace_governor == 0) {
127c7f652e0SVenki Pallipadi 			cpufreq_register_notifier(
128c7f652e0SVenki Pallipadi 					&userspace_cpufreq_notifier_block,
129c7f652e0SVenki Pallipadi 					CPUFREQ_TRANSITION_NOTIFIER);
130c7f652e0SVenki Pallipadi 		}
131c7f652e0SVenki Pallipadi 		cpus_using_userspace_governor++;
132c7f652e0SVenki Pallipadi 
133b38868aaSMike Travis 		per_cpu(cpu_is_managed, cpu) = 1;
134b38868aaSMike Travis 		per_cpu(cpu_min_freq, cpu) = policy->min;
135b38868aaSMike Travis 		per_cpu(cpu_max_freq, cpu) = policy->max;
136b38868aaSMike Travis 		per_cpu(cpu_cur_freq, cpu) = policy->cur;
137b38868aaSMike Travis 		per_cpu(cpu_set_freq, cpu) = policy->cur;
1382d06d8c4SDominik Brodowski 		pr_debug("managing cpu %u started "
139b38868aaSMike Travis 			"(%u - %u kHz, currently %u kHz)\n",
140b38868aaSMike Travis 				cpu,
141b38868aaSMike Travis 				per_cpu(cpu_min_freq, cpu),
142b38868aaSMike Travis 				per_cpu(cpu_max_freq, cpu),
143b38868aaSMike Travis 				per_cpu(cpu_cur_freq, cpu));
1449e76988eSVenki Pallipadi 
1453fc54d37Sakpm@osdl.org 		mutex_unlock(&userspace_mutex);
1461da177e4SLinus Torvalds 		break;
1471da177e4SLinus Torvalds 	case CPUFREQ_GOV_STOP:
1483fc54d37Sakpm@osdl.org 		mutex_lock(&userspace_mutex);
149c7f652e0SVenki Pallipadi 		cpus_using_userspace_governor--;
150c7f652e0SVenki Pallipadi 		if (cpus_using_userspace_governor == 0) {
151c7f652e0SVenki Pallipadi 			cpufreq_unregister_notifier(
152c7f652e0SVenki Pallipadi 					&userspace_cpufreq_notifier_block,
153c7f652e0SVenki Pallipadi 					CPUFREQ_TRANSITION_NOTIFIER);
154c7f652e0SVenki Pallipadi 		}
155c7f652e0SVenki Pallipadi 
156b38868aaSMike Travis 		per_cpu(cpu_is_managed, cpu) = 0;
157b38868aaSMike Travis 		per_cpu(cpu_min_freq, cpu) = 0;
158b38868aaSMike Travis 		per_cpu(cpu_max_freq, cpu) = 0;
159b38868aaSMike Travis 		per_cpu(cpu_set_freq, cpu) = 0;
1602d06d8c4SDominik Brodowski 		pr_debug("managing cpu %u stopped\n", cpu);
1613fc54d37Sakpm@osdl.org 		mutex_unlock(&userspace_mutex);
1621da177e4SLinus Torvalds 		break;
1631da177e4SLinus Torvalds 	case CPUFREQ_GOV_LIMITS:
1643fc54d37Sakpm@osdl.org 		mutex_lock(&userspace_mutex);
1652d06d8c4SDominik Brodowski 		pr_debug("limit event for cpu %u: %u - %u kHz, "
166c0672860SThomas Renninger 			"currently %u kHz, last set to %u kHz\n",
167c0672860SThomas Renninger 			cpu, policy->min, policy->max,
168b38868aaSMike Travis 			per_cpu(cpu_cur_freq, cpu),
169b38868aaSMike Travis 			per_cpu(cpu_set_freq, cpu));
170b38868aaSMike Travis 		if (policy->max < per_cpu(cpu_set_freq, cpu)) {
171c0672860SThomas Renninger 			__cpufreq_driver_target(policy, policy->max,
1721da177e4SLinus Torvalds 						CPUFREQ_RELATION_H);
173b38868aaSMike Travis 		} else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
174c0672860SThomas Renninger 			__cpufreq_driver_target(policy, policy->min,
1751da177e4SLinus Torvalds 						CPUFREQ_RELATION_L);
176b38868aaSMike Travis 		} else {
177b38868aaSMike Travis 			__cpufreq_driver_target(policy,
178b38868aaSMike Travis 						per_cpu(cpu_set_freq, cpu),
179c0672860SThomas Renninger 						CPUFREQ_RELATION_L);
180c0672860SThomas Renninger 		}
181b38868aaSMike Travis 		per_cpu(cpu_min_freq, cpu) = policy->min;
182b38868aaSMike Travis 		per_cpu(cpu_max_freq, cpu) = policy->max;
183b38868aaSMike Travis 		per_cpu(cpu_cur_freq, cpu) = policy->cur;
1843fc54d37Sakpm@osdl.org 		mutex_unlock(&userspace_mutex);
1851da177e4SLinus Torvalds 		break;
1861da177e4SLinus Torvalds 	}
187914f7c31SJeff Garzik 	return rc;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 
191c4d14bc0SSven Wegener #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
192c4d14bc0SSven Wegener static
193c4d14bc0SSven Wegener #endif
1941da177e4SLinus Torvalds struct cpufreq_governor cpufreq_gov_userspace = {
1951da177e4SLinus Torvalds 	.name		= "userspace",
1961da177e4SLinus Torvalds 	.governor	= cpufreq_governor_userspace,
1979e76988eSVenki Pallipadi 	.store_setspeed	= cpufreq_set,
1989e76988eSVenki Pallipadi 	.show_setspeed	= show_speed,
1991da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static int __init cpufreq_gov_userspace_init(void)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	return cpufreq_register_governor(&cpufreq_gov_userspace);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static void __exit cpufreq_gov_userspace_exit(void)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	cpufreq_unregister_governor(&cpufreq_gov_userspace);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 
2141bceb8d1SDave Jones MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
2151bceb8d1SDave Jones 		"Russell King <rmk@arm.linux.org.uk>");
2161da177e4SLinus Torvalds MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
2171da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2181da177e4SLinus Torvalds 
2196915719bSJohannes Weiner #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
2201da177e4SLinus Torvalds fs_initcall(cpufreq_gov_userspace_init);
2216915719bSJohannes Weiner #else
2226915719bSJohannes Weiner module_init(cpufreq_gov_userspace_init);
2236915719bSJohannes Weiner #endif
2241da177e4SLinus Torvalds module_exit(cpufreq_gov_userspace_exit);
225