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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * We're safe from concurrent calls to ->target() here |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 43 | * as we hold the userspace_mutex lock. If we were calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * cpufreq_driver_target, a deadlock situation might occur: |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 45 | * A: cpufreq_set (lock userspace_mutex) -> |
| 46 | * cpufreq_driver_target(lock policy->lock) |
| 47 | * B: cpufreq_set_policy(lock policy->lock) -> |
| 48 | * __cpufreq_governor -> |
| 49 | * cpufreq_governor_userspace (lock userspace_mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 51 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | err: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 54 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 59 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 61 | return sprintf(buf, "%u\n", policy->cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
| 65 | unsigned int event) |
| 66 | { |
| 67 | unsigned int cpu = policy->cpu; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 68 | int rc = 0; |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | switch (event) { |
| 71 | case CPUFREQ_GOV_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | BUG_ON(!policy->cur); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 73 | pr_debug("started managing cpu %u\n", cpu); |
| 74 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 75 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 76 | per_cpu(cpu_is_managed, cpu) = 1; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 77 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | case CPUFREQ_GOV_STOP: |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 80 | pr_debug("managing cpu %u stopped\n", cpu); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 81 | |
| 82 | mutex_lock(&userspace_mutex); |
| 83 | per_cpu(cpu_is_managed, cpu) = 0; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 84 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | break; |
| 86 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 87 | mutex_lock(&userspace_mutex); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 88 | 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] | 89 | cpu, policy->min, policy->max, |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 90 | policy->cur); |
| 91 | |
| 92 | if (policy->max < policy->cur) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 93 | __cpufreq_driver_target(policy, policy->max, |
| 94 | CPUFREQ_RELATION_H); |
Viresh Kumar | d1922f0 | 2013-06-05 11:47:38 +0530 | [diff] [blame^] | 95 | else if (policy->min > policy->cur) |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 96 | __cpufreq_driver_target(policy, policy->min, |
| 97 | CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 98 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | break; |
| 100 | } |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 101 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Sven Wegener | c4d14bc | 2008-09-20 16:50:08 +0200 | [diff] [blame] | 105 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
| 106 | static |
| 107 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | struct cpufreq_governor cpufreq_gov_userspace = { |
| 109 | .name = "userspace", |
| 110 | .governor = cpufreq_governor_userspace, |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 111 | .store_setspeed = cpufreq_set, |
| 112 | .show_setspeed = show_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | .owner = THIS_MODULE, |
| 114 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static int __init cpufreq_gov_userspace_init(void) |
| 117 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void __exit cpufreq_gov_userspace_exit(void) |
| 123 | { |
| 124 | cpufreq_unregister_governor(&cpufreq_gov_userspace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 128 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
| 129 | "Russell King <rmk@arm.linux.org.uk>"); |
| 130 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 131 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 133 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | fs_initcall(cpufreq_gov_userspace_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 135 | #else |
| 136 | module_init(cpufreq_gov_userspace_init); |
| 137 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | module_exit(cpufreq_gov_userspace_exit); |