Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | /* |
| 4 | * linux/drivers/cpufreq/cpufreq_userspace.c |
| 5 | * |
| 6 | * Copyright (C) 2001 Russell King |
| 7 | * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Viresh Kumar | db70115 | 2012-10-23 01:29:03 +0200 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/cpufreq.h> |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 18 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 19 | static DEFINE_MUTEX(userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 21 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * cpufreq_set - set the CPU frequency |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 23 | * @policy: pointer to policy struct where freq is being set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * @freq: target frequency in kHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * |
| 26 | * Sets the CPU frequency to freq. |
| 27 | */ |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 28 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
| 30 | int ret = -EINVAL; |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 31 | unsigned int *setspeed = policy->governor_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 33 | pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 35 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 36 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | goto err; |
| 38 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 39 | *setspeed = freq; |
| 40 | |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 41 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | err: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 43 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return ret; |
| 45 | } |
| 46 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 47 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 49 | return sprintf(buf, "%u\n", policy->cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 52 | static 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. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 64 | static 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 | |
| 72 | static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 74 | unsigned int *setspeed = policy->governor_data; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 75 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 76 | BUG_ON(!policy->cur); |
| 77 | pr_debug("started managing cpu %u\n", policy->cpu); |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 78 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 79 | 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 Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 85 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 86 | static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy) |
| 87 | { |
| 88 | unsigned int *setspeed = policy->governor_data; |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 89 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 90 | pr_debug("managing cpu %u stopped\n", policy->cpu); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 91 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 92 | mutex_lock(&userspace_mutex); |
| 93 | per_cpu(cpu_is_managed, policy->cpu) = 0; |
| 94 | *setspeed = 0; |
| 95 | mutex_unlock(&userspace_mutex); |
| 96 | } |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 97 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 98 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 117 | static struct cpufreq_governor cpufreq_gov_userspace = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | .name = "userspace", |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 119 | .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 Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 124 | .store_setspeed = cpufreq_set, |
| 125 | .show_setspeed = show_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .owner = THIS_MODULE, |
| 127 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 129 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
| 130 | "Russell King <rmk@arm.linux.org.uk>"); |
| 131 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 132 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 134 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 135 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 136 | { |
| 137 | return &cpufreq_gov_userspace; |
| 138 | } |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 139 | #endif |
Quentin Perret | 10dd857 | 2020-06-29 13:54:59 +0530 | [diff] [blame] | 140 | |
| 141 | cpufreq_governor_init(cpufreq_gov_userspace); |
| 142 | cpufreq_governor_exit(cpufreq_gov_userspace); |