blob: 5dc77b7a7594131299f5d113fc25f0c335809b88 [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
58
Venki Pallipadi9e769882007-10-26 10:18:21 -070059static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Viresh Kumard1922f02013-06-05 11:47:38 +053061 return sprintf(buf, "%u\n", policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
65 unsigned int event)
66{
67 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -070068 int rc = 0;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 switch (event) {
71 case CPUFREQ_GOV_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 BUG_ON(!policy->cur);
Viresh Kumard1922f02013-06-05 11:47:38 +053073 pr_debug("started managing cpu %u\n", cpu);
74
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080075 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070076 per_cpu(cpu_is_managed, cpu) = 1;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080077 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case CPUFREQ_GOV_STOP:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020080 pr_debug("managing cpu %u stopped\n", cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053081
82 mutex_lock(&userspace_mutex);
83 per_cpu(cpu_is_managed, cpu) = 0;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080084 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080087 mutex_lock(&userspace_mutex);
Viresh Kumard1922f02013-06-05 11:47:38 +053088 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n",
Thomas Renningerc0672862006-01-27 16:15:26 +010089 cpu, policy->min, policy->max,
Viresh Kumard1922f02013-06-05 11:47:38 +053090 policy->cur);
91
92 if (policy->max < policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010093 __cpufreq_driver_target(policy, policy->max,
94 CPUFREQ_RELATION_H);
Viresh Kumard1922f02013-06-05 11:47:38 +053095 else if (policy->min > policy->cur)
Thomas Renningerc0672862006-01-27 16:15:26 +010096 __cpufreq_driver_target(policy, policy->min,
97 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080098 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 break;
100 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700101 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200105#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
106static
107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct cpufreq_governor cpufreq_gov_userspace = {
109 .name = "userspace",
110 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700111 .store_setspeed = cpufreq_set,
112 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .owner = THIS_MODULE,
114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static int __init cpufreq_gov_userspace_init(void)
117{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return cpufreq_register_governor(&cpufreq_gov_userspace);
119}
120
121
122static void __exit cpufreq_gov_userspace_exit(void)
123{
124 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127
Dave Jones1bceb8d2009-01-18 01:51:46 -0500128MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
129 "Russell King <rmk@arm.linux.org.uk>");
130MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
131MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Johannes Weiner69157192008-01-17 15:21:08 -0800133#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800135#else
136module_init(cpufreq_gov_userspace_init);
137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138module_exit(cpufreq_gov_userspace_exit);