blob: 03078090b5f76e15d263f3a035f575f9e7901342 [file] [log] [blame]
Thomas Renningerc0672862006-01-27 16:15:26 +01001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Viresh Kumardb701152012-10-23 01:29:03 +020014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/cpufreq.h>
Viresh Kumard1922f02013-06-05 11:47:38 +053017#include <linux/init.h>
18#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Mike Travisb38868a2008-07-18 18:11:32 -070021static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Dave Jones1bceb8d2009-01-18 01:51:46 -050022static DEFINE_MUTEX(userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Dave Jones32ee8c32006-02-28 00:43:23 -050024/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070026 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
29 * Sets the CPU frequency to freq.
30 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070031static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 int ret = -EINVAL;
34
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020035 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080037 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070038 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 goto err;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /*
42 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080043 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * cpufreq_driver_target, a deadlock situation might occur:
Dave Jones1bceb8d2009-01-18 01:51:46 -050045 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Thomas Renningerc0672862006-01-27 16:15:26 +010051 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080054 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return ret;
56}
57
Venki Pallipadi9e769882007-10-26 10:18:21 -070058static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Viresh Kumard1922f02013-06-05 11:47:38 +053060 return sprintf(buf, "%u\n", policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
64 unsigned int event)
65{
66 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -070067 int rc = 0;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 switch (event) {
70 case CPUFREQ_GOV_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 BUG_ON(!policy->cur);
Viresh Kumard1922f02013-06-05 11:47:38 +053072 pr_debug("started managing cpu %u\n", cpu);
73
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080074 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070075 per_cpu(cpu_is_managed, cpu) = 1;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080076 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 break;
78 case CPUFREQ_GOV_STOP:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020079 pr_debug("managing cpu %u stopped\n", cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053080
81 mutex_lock(&userspace_mutex);
82 per_cpu(cpu_is_managed, cpu) = 0;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080083 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080086 mutex_lock(&userspace_mutex);
Viresh Kumard1922f02013-06-05 11:47:38 +053087 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n",
Thomas Renningerc0672862006-01-27 16:15:26 +010088 cpu, policy->min, policy->max,
Viresh Kumard1922f02013-06-05 11:47:38 +053089 policy->cur);
90
91 if (policy->max < policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010092 __cpufreq_driver_target(policy, policy->max,
93 CPUFREQ_RELATION_H);
Viresh Kumard1922f02013-06-05 11:47:38 +053094 else if (policy->min > policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010095 __cpufreq_driver_target(policy, policy->min,
96 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080097 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700100 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200103#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
104static
105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct cpufreq_governor cpufreq_gov_userspace = {
107 .name = "userspace",
108 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700109 .store_setspeed = cpufreq_set,
110 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .owner = THIS_MODULE,
112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static int __init cpufreq_gov_userspace_init(void)
115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return cpufreq_register_governor(&cpufreq_gov_userspace);
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void __exit cpufreq_gov_userspace_exit(void)
120{
121 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Dave Jones1bceb8d2009-01-18 01:51:46 -0500124MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
125 "Russell King <rmk@arm.linux.org.uk>");
126MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
127MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Johannes Weiner69157192008-01-17 15:21:08 -0800129#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800131#else
132module_init(cpufreq_gov_userspace_init);
133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134module_exit(cpufreq_gov_userspace_exit);