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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
| 69 | unsigned int event) |
| 70 | { |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 71 | unsigned int *setspeed = policy->governor_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | unsigned int cpu = policy->cpu; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 73 | int rc = 0; |
| 74 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 75 | if (event == CPUFREQ_GOV_POLICY_INIT) |
| 76 | return cpufreq_userspace_policy_init(policy); |
| 77 | |
| 78 | if (!setspeed) |
| 79 | return -EINVAL; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | switch (event) { |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 82 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | case CPUFREQ_GOV_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | BUG_ON(!policy->cur); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 90 | pr_debug("started managing cpu %u\n", cpu); |
| 91 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 92 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 93 | per_cpu(cpu_is_managed, cpu) = 1; |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 94 | *setspeed = policy->cur; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 95 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | break; |
| 97 | case CPUFREQ_GOV_STOP: |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 98 | pr_debug("managing cpu %u stopped\n", cpu); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 99 | |
| 100 | mutex_lock(&userspace_mutex); |
| 101 | per_cpu(cpu_is_managed, cpu) = 0; |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 102 | *setspeed = 0; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 103 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | break; |
| 105 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 106 | mutex_lock(&userspace_mutex); |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 107 | 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 Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 109 | |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 110 | if (policy->max < *setspeed) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 111 | __cpufreq_driver_target(policy, policy->max, |
| 112 | CPUFREQ_RELATION_H); |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 113 | else if (policy->min > *setspeed) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 114 | __cpufreq_driver_target(policy, policy->min, |
| 115 | CPUFREQ_RELATION_L); |
Sai Gurrappadi | e43e94c | 2016-04-29 14:44:37 -0700 | [diff] [blame^] | 116 | else |
| 117 | __cpufreq_driver_target(policy, *setspeed, |
| 118 | CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 119 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | break; |
| 121 | } |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 122 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 125 | static struct cpufreq_governor cpufreq_gov_userspace = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .name = "userspace", |
| 127 | .governor = cpufreq_governor_userspace, |
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); |