blob: 9f3dec9a3f36db98407c743a0fc6493476b14bcf [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
69 unsigned int event)
70{
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070071 unsigned int *setspeed = policy->governor_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -070073 int rc = 0;
74
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070075 if (event == CPUFREQ_GOV_POLICY_INIT)
76 return cpufreq_userspace_policy_init(policy);
77
78 if (!setspeed)
79 return -EINVAL;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 switch (event) {
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070082 case CPUFREQ_GOV_POLICY_EXIT:
83 mutex_lock(&userspace_mutex);
84 policy->governor_data = NULL;
85 kfree(setspeed);
86 mutex_unlock(&userspace_mutex);
87 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 case CPUFREQ_GOV_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 BUG_ON(!policy->cur);
Viresh Kumard1922f02013-06-05 11:47:38 +053090 pr_debug("started managing cpu %u\n", cpu);
91
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080092 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070093 per_cpu(cpu_is_managed, cpu) = 1;
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070094 *setspeed = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080095 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 case CPUFREQ_GOV_STOP:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020098 pr_debug("managing cpu %u stopped\n", cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053099
100 mutex_lock(&userspace_mutex);
101 per_cpu(cpu_is_managed, cpu) = 0;
Sai Gurrappadie43e94c2016-04-29 14:44:37 -0700102 *setspeed = 0;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800103 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
105 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800106 mutex_lock(&userspace_mutex);
Sai Gurrappadie43e94c2016-04-29 14:44:37 -0700107 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
108 cpu, policy->min, policy->max, policy->cur, *setspeed);
Viresh Kumard1922f02013-06-05 11:47:38 +0530109
Sai Gurrappadie43e94c2016-04-29 14:44:37 -0700110 if (policy->max < *setspeed)
Thomas Renningerc0672862006-01-27 16:15:26 +0100111 __cpufreq_driver_target(policy, policy->max,
112 CPUFREQ_RELATION_H);
Sai Gurrappadie43e94c2016-04-29 14:44:37 -0700113 else if (policy->min > *setspeed)
Thomas Renningerc0672862006-01-27 16:15:26 +0100114 __cpufreq_driver_target(policy, policy->min,
115 CPUFREQ_RELATION_L);
Sai Gurrappadie43e94c2016-04-29 14:44:37 -0700116 else
117 __cpufreq_driver_target(policy, *setspeed,
118 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800119 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700122 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100125static struct cpufreq_governor cpufreq_gov_userspace = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .name = "userspace",
127 .governor = cpufreq_governor_userspace,
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);