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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 21 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 22 | static DEFINE_MUTEX(userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 24 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * cpufreq_set - set the CPU frequency |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 26 | * @policy: pointer to policy struct where freq is being set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * @freq: target frequency in kHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * |
| 29 | * Sets the CPU frequency to freq. |
| 30 | */ |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 31 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | int ret = -EINVAL; |
| 34 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 35 | 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] | 36 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 37 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 38 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | goto err; |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
| 53 | unsigned int event) |
| 54 | { |
| 55 | unsigned int cpu = policy->cpu; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 56 | int rc = 0; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | switch (event) { |
| 59 | case CPUFREQ_GOV_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | BUG_ON(!policy->cur); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 61 | pr_debug("started managing cpu %u\n", cpu); |
| 62 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 63 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 64 | per_cpu(cpu_is_managed, cpu) = 1; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 65 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | break; |
| 67 | case CPUFREQ_GOV_STOP: |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 68 | pr_debug("managing cpu %u stopped\n", cpu); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 69 | |
| 70 | mutex_lock(&userspace_mutex); |
| 71 | per_cpu(cpu_is_managed, cpu) = 0; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 72 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | break; |
| 74 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 75 | mutex_lock(&userspace_mutex); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 76 | pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n", |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 77 | cpu, policy->min, policy->max, |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 78 | policy->cur); |
| 79 | |
| 80 | if (policy->max < policy->cur) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 81 | __cpufreq_driver_target(policy, policy->max, |
| 82 | CPUFREQ_RELATION_H); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame] | 83 | else if (policy->min > policy->cur) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 84 | __cpufreq_driver_target(policy, policy->min, |
| 85 | CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 86 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | break; |
| 88 | } |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 89 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Sven Wegener | c4d14bc | 2008-09-20 16:50:08 +0200 | [diff] [blame] | 92 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
| 93 | static |
| 94 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct cpufreq_governor cpufreq_gov_userspace = { |
| 96 | .name = "userspace", |
| 97 | .governor = cpufreq_governor_userspace, |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 98 | .store_setspeed = cpufreq_set, |
| 99 | .show_setspeed = show_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .owner = THIS_MODULE, |
| 101 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | static int __init cpufreq_gov_userspace_init(void) |
| 104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static void __exit cpufreq_gov_userspace_exit(void) |
| 109 | { |
| 110 | cpufreq_unregister_governor(&cpufreq_gov_userspace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 113 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
| 114 | "Russell King <rmk@arm.linux.org.uk>"); |
| 115 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 116 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 118 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | fs_initcall(cpufreq_gov_userspace_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 120 | #else |
| 121 | module_init(cpufreq_gov_userspace_init); |
| 122 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | module_exit(cpufreq_gov_userspace_exit); |