Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 1 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Kumar | db70115 | 2012-10-23 01:29:03 +0200 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/cpufreq.h> |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 22 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 23 | static DEFINE_MUTEX(userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 25 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * cpufreq_set - set the CPU frequency |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 27 | * @policy: pointer to policy struct where freq is being set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * @freq: target frequency in kHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * |
| 30 | * Sets the CPU frequency to freq. |
| 31 | */ |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 32 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | int ret = -EINVAL; |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 35 | unsigned int *setspeed = policy->governor_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 37 | 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] | 38 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 39 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 40 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | goto err; |
| 42 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 43 | *setspeed = freq; |
| 44 | |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 45 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | err: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 47 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return ret; |
| 49 | } |
| 50 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 51 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 53 | return sprintf(buf, "%u\n", policy->cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 56 | static 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. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 68 | static 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 | |
| 76 | static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 78 | unsigned int *setspeed = policy->governor_data; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 79 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 80 | BUG_ON(!policy->cur); |
| 81 | pr_debug("started managing cpu %u\n", policy->cpu); |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 82 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 83 | 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 Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame] | 89 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 90 | static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy) |
| 91 | { |
| 92 | unsigned int *setspeed = policy->governor_data; |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 93 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 94 | pr_debug("managing cpu %u stopped\n", policy->cpu); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 95 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 96 | mutex_lock(&userspace_mutex); |
| 97 | per_cpu(cpu_is_managed, policy->cpu) = 0; |
| 98 | *setspeed = 0; |
| 99 | mutex_unlock(&userspace_mutex); |
| 100 | } |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 101 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 102 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 121 | static struct cpufreq_governor cpufreq_gov_userspace = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .name = "userspace", |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 123 | .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 Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 128 | .store_setspeed = cpufreq_set, |
| 129 | .show_setspeed = show_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .owner = THIS_MODULE, |
| 131 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | static int __init cpufreq_gov_userspace_init(void) |
| 134 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static void __exit cpufreq_gov_userspace_exit(void) |
| 139 | { |
| 140 | cpufreq_unregister_governor(&cpufreq_gov_userspace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 143 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
| 144 | "Russell King <rmk@arm.linux.org.uk>"); |
| 145 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 146 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 148 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 149 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 150 | { |
| 151 | return &cpufreq_gov_userspace; |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | fs_initcall(cpufreq_gov_userspace_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 155 | #else |
| 156 | module_init(cpufreq_gov_userspace_init); |
| 157 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | module_exit(cpufreq_gov_userspace_exit); |