blob: 4dbf1db16aca0e5d29d44b002a6d4ad0cae6d139 [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
Thomas Renningerc0672862006-01-27 16:15:26 +010041 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080043 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return ret;
45}
46
Venki Pallipadi9e769882007-10-26 10:18:21 -070047static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Viresh Kumard1922f02013-06-05 11:47:38 +053049 return sprintf(buf, "%u\n", policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
53 unsigned int event)
54{
55 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -070056 int rc = 0;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 switch (event) {
59 case CPUFREQ_GOV_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 BUG_ON(!policy->cur);
Viresh Kumard1922f02013-06-05 11:47:38 +053061 pr_debug("started managing cpu %u\n", cpu);
62
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080063 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070064 per_cpu(cpu_is_managed, cpu) = 1;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080065 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67 case CPUFREQ_GOV_STOP:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020068 pr_debug("managing cpu %u stopped\n", cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053069
70 mutex_lock(&userspace_mutex);
71 per_cpu(cpu_is_managed, cpu) = 0;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080072 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
74 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080075 mutex_lock(&userspace_mutex);
Viresh Kumard1922f02013-06-05 11:47:38 +053076 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n",
Thomas Renningerc0672862006-01-27 16:15:26 +010077 cpu, policy->min, policy->max,
Viresh Kumard1922f02013-06-05 11:47:38 +053078 policy->cur);
79
80 if (policy->max < policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010081 __cpufreq_driver_target(policy, policy->max,
82 CPUFREQ_RELATION_H);
Viresh Kumard1922f02013-06-05 11:47:38 +053083 else if (policy->min > policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010084 __cpufreq_driver_target(policy, policy->min,
85 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080086 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88 }
Jeff Garzik914f7c32006-10-20 14:31:00 -070089 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Sven Wegenerc4d14bc2008-09-20 16:50:08 +020092#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
93static
94#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct cpufreq_governor cpufreq_gov_userspace = {
96 .name = "userspace",
97 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -070098 .store_setspeed = cpufreq_set,
99 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .owner = THIS_MODULE,
101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static int __init cpufreq_gov_userspace_init(void)
104{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return cpufreq_register_governor(&cpufreq_gov_userspace);
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void __exit cpufreq_gov_userspace_exit(void)
109{
110 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Dave Jones1bceb8d2009-01-18 01:51:46 -0500113MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
114 "Russell King <rmk@arm.linux.org.uk>");
115MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
116MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Johannes Weiner69157192008-01-17 15:21:08 -0800118#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800120#else
121module_init(cpufreq_gov_userspace_init);
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123module_exit(cpufreq_gov_userspace_exit);