blob: bd897e3e134de055e5120cc0fe63bc48a70b21c2 [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>
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Mike Travisb38868a2008-07-18 18:11:32 -070022static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Dave Jones1bceb8d2009-01-18 01:51:46 -050023static DEFINE_MUTEX(userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Dave Jones32ee8c32006-02-28 00:43:23 -050025/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070027 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 * Sets the CPU frequency to freq.
31 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070032static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int ret = -EINVAL;
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070035 unsigned int *setspeed = policy->governor_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020037 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080039 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070040 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 goto err;
42
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070043 *setspeed = freq;
44
Thomas Renningerc0672862006-01-27 16:15:26 +010045 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080047 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return ret;
49}
50
Venki Pallipadi9e769882007-10-26 10:18:21 -070051static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Viresh Kumard1922f02013-06-05 11:47:38 +053053 return sprintf(buf, "%u\n", policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070056static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy)
57{
58 unsigned int *setspeed;
59
60 setspeed = kzalloc(sizeof(*setspeed), GFP_KERNEL);
61 if (!setspeed)
62 return -ENOMEM;
63
64 policy->governor_data = setspeed;
65 return 0;
66}
67
Rafael J. Wysockie7888922016-06-02 23:24:15 +020068static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy)
69{
70 mutex_lock(&userspace_mutex);
71 kfree(policy->governor_data);
72 policy->governor_data = NULL;
73 mutex_unlock(&userspace_mutex);
74}
75
76static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070078 unsigned int *setspeed = policy->governor_data;
Jeff Garzik914f7c32006-10-20 14:31:00 -070079
Rafael J. Wysockie7888922016-06-02 23:24:15 +020080 BUG_ON(!policy->cur);
81 pr_debug("started managing cpu %u\n", policy->cpu);
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070082
Rafael J. Wysockie7888922016-06-02 23:24:15 +020083 mutex_lock(&userspace_mutex);
84 per_cpu(cpu_is_managed, policy->cpu) = 1;
85 *setspeed = policy->cur;
86 mutex_unlock(&userspace_mutex);
87 return 0;
88}
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070089
Rafael J. Wysockie7888922016-06-02 23:24:15 +020090static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy)
91{
92 unsigned int *setspeed = policy->governor_data;
Viresh Kumard1922f02013-06-05 11:47:38 +053093
Rafael J. Wysockie7888922016-06-02 23:24:15 +020094 pr_debug("managing cpu %u stopped\n", policy->cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053095
Rafael J. Wysockie7888922016-06-02 23:24:15 +020096 mutex_lock(&userspace_mutex);
97 per_cpu(cpu_is_managed, policy->cpu) = 0;
98 *setspeed = 0;
99 mutex_unlock(&userspace_mutex);
100}
Viresh Kumard1922f02013-06-05 11:47:38 +0530101
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200102static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy)
103{
104 unsigned int *setspeed = policy->governor_data;
105
106 mutex_lock(&userspace_mutex);
107
108 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
109 policy->cpu, policy->min, policy->max, policy->cur, *setspeed);
110
111 if (policy->max < *setspeed)
112 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
113 else if (policy->min > *setspeed)
114 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
115 else
116 __cpufreq_driver_target(policy, *setspeed, CPUFREQ_RELATION_L);
117
118 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100121static struct cpufreq_governor cpufreq_gov_userspace = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .name = "userspace",
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200123 .init = cpufreq_userspace_policy_init,
124 .exit = cpufreq_userspace_policy_exit,
125 .start = cpufreq_userspace_policy_start,
126 .stop = cpufreq_userspace_policy_stop,
127 .limits = cpufreq_userspace_policy_limits,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700128 .store_setspeed = cpufreq_set,
129 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .owner = THIS_MODULE,
131};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static int __init cpufreq_gov_userspace_init(void)
134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return cpufreq_register_governor(&cpufreq_gov_userspace);
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static void __exit cpufreq_gov_userspace_exit(void)
139{
140 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Dave Jones1bceb8d2009-01-18 01:51:46 -0500143MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
144 "Russell King <rmk@arm.linux.org.uk>");
145MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
146MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Johannes Weiner69157192008-01-17 15:21:08 -0800148#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100149struct cpufreq_governor *cpufreq_default_governor(void)
150{
151 return &cpufreq_gov_userspace;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800155#else
156module_init(cpufreq_gov_userspace_init);
157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158module_exit(cpufreq_gov_userspace_exit);