blob: 50a4d784658009112d1d160e8b3bc8d9d9f2de0d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thomas Renningerc0672862006-01-27 16:15:26 +01002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003/*
4 * linux/drivers/cpufreq/cpufreq_userspace.c
5 *
6 * Copyright (C) 2001 Russell King
7 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Viresh Kumardb701152012-10-23 01:29:03 +020010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/cpufreq.h>
Viresh Kumard1922f02013-06-05 11:47:38 +053013#include <linux/init.h>
14#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080015#include <linux/mutex.h>
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Mike Travisb38868a2008-07-18 18:11:32 -070018static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Dave Jones1bceb8d2009-01-18 01:51:46 -050019static DEFINE_MUTEX(userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Dave Jones32ee8c32006-02-28 00:43:23 -050021/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070023 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
26 * Sets the CPU frequency to freq.
27 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070028static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 int ret = -EINVAL;
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070031 unsigned int *setspeed = policy->governor_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020033 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080035 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070036 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 goto err;
38
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070039 *setspeed = freq;
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
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070052static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy)
53{
54 unsigned int *setspeed;
55
56 setspeed = kzalloc(sizeof(*setspeed), GFP_KERNEL);
57 if (!setspeed)
58 return -ENOMEM;
59
60 policy->governor_data = setspeed;
61 return 0;
62}
63
Rafael J. Wysockie7888922016-06-02 23:24:15 +020064static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy)
65{
66 mutex_lock(&userspace_mutex);
67 kfree(policy->governor_data);
68 policy->governor_data = NULL;
69 mutex_unlock(&userspace_mutex);
70}
71
72static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070074 unsigned int *setspeed = policy->governor_data;
Jeff Garzik914f7c32006-10-20 14:31:00 -070075
Rafael J. Wysockie7888922016-06-02 23:24:15 +020076 BUG_ON(!policy->cur);
77 pr_debug("started managing cpu %u\n", policy->cpu);
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070078
Rafael J. Wysockie7888922016-06-02 23:24:15 +020079 mutex_lock(&userspace_mutex);
80 per_cpu(cpu_is_managed, policy->cpu) = 1;
81 *setspeed = policy->cur;
82 mutex_unlock(&userspace_mutex);
83 return 0;
84}
Sai Gurrappadie43e94c2016-04-29 14:44:37 -070085
Rafael J. Wysockie7888922016-06-02 23:24:15 +020086static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy)
87{
88 unsigned int *setspeed = policy->governor_data;
Viresh Kumard1922f02013-06-05 11:47:38 +053089
Rafael J. Wysockie7888922016-06-02 23:24:15 +020090 pr_debug("managing cpu %u stopped\n", policy->cpu);
Viresh Kumard1922f02013-06-05 11:47:38 +053091
Rafael J. Wysockie7888922016-06-02 23:24:15 +020092 mutex_lock(&userspace_mutex);
93 per_cpu(cpu_is_managed, policy->cpu) = 0;
94 *setspeed = 0;
95 mutex_unlock(&userspace_mutex);
96}
Viresh Kumard1922f02013-06-05 11:47:38 +053097
Rafael J. Wysockie7888922016-06-02 23:24:15 +020098static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy)
99{
100 unsigned int *setspeed = policy->governor_data;
101
102 mutex_lock(&userspace_mutex);
103
104 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
105 policy->cpu, policy->min, policy->max, policy->cur, *setspeed);
106
107 if (policy->max < *setspeed)
108 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
109 else if (policy->min > *setspeed)
110 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
111 else
112 __cpufreq_driver_target(policy, *setspeed, CPUFREQ_RELATION_L);
113
114 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100117static struct cpufreq_governor cpufreq_gov_userspace = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .name = "userspace",
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200119 .init = cpufreq_userspace_policy_init,
120 .exit = cpufreq_userspace_policy_exit,
121 .start = cpufreq_userspace_policy_start,
122 .stop = cpufreq_userspace_policy_stop,
123 .limits = cpufreq_userspace_policy_limits,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700124 .store_setspeed = cpufreq_set,
125 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .owner = THIS_MODULE,
127};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dave Jones1bceb8d2009-01-18 01:51:46 -0500129MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
130 "Russell King <rmk@arm.linux.org.uk>");
131MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
132MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Johannes Weiner69157192008-01-17 15:21:08 -0800134#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100135struct cpufreq_governor *cpufreq_default_governor(void)
136{
137 return &cpufreq_gov_userspace;
138}
Johannes Weiner69157192008-01-17 15:21:08 -0800139#endif
Quentin Perret10dd8572020-06-29 13:54:59 +0530140
141cpufreq_governor_init(cpufreq_gov_userspace);
142cpufreq_governor_exit(cpufreq_gov_userspace);