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