cpufreq_userspace.c revision 3fc54d37ab64733448faf0185e19a80f070eb9e3
1/* 2 * linux/drivers/cpufreq/cpufreq_userspace.c 3 * 4 * Copyright (C) 2001 Russell King 5 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 */ 12 13#include <linux/config.h> 14#include <linux/kernel.h> 15#include <linux/module.h> 16#include <linux/smp.h> 17#include <linux/init.h> 18#include <linux/spinlock.h> 19#include <linux/interrupt.h> 20#include <linux/cpufreq.h> 21#include <linux/types.h> 22#include <linux/fs.h> 23#include <linux/sysfs.h> 24#include <linux/mutex.h> 25 26#include <asm/uaccess.h> 27 28 29/** 30 * A few values needed by the userspace governor 31 */ 32static unsigned int cpu_max_freq[NR_CPUS]; 33static unsigned int cpu_min_freq[NR_CPUS]; 34static unsigned int cpu_cur_freq[NR_CPUS]; /* current CPU freq */ 35static unsigned int cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */ 36static unsigned int cpu_is_managed[NR_CPUS]; 37static struct cpufreq_policy current_policy[NR_CPUS]; 38 39static DEFINE_MUTEX (userspace_mutex); 40 41#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg) 42 43/* keep track of frequency transitions */ 44static int 45userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val, 46 void *data) 47{ 48 struct cpufreq_freqs *freq = data; 49 50 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq->cpu, freq->new); 51 cpu_cur_freq[freq->cpu] = freq->new; 52 53 return 0; 54} 55 56static struct notifier_block userspace_cpufreq_notifier_block = { 57 .notifier_call = userspace_cpufreq_notifier 58}; 59 60 61/** 62 * cpufreq_set - set the CPU frequency 63 * @freq: target frequency in kHz 64 * @cpu: CPU for which the frequency is to be set 65 * 66 * Sets the CPU frequency to freq. 67 */ 68static int cpufreq_set(unsigned int freq, unsigned int cpu) 69{ 70 int ret = -EINVAL; 71 72 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", cpu, freq); 73 74 mutex_lock(&userspace_mutex); 75 if (!cpu_is_managed[cpu]) 76 goto err; 77 78 cpu_set_freq[cpu] = freq; 79 80 if (freq < cpu_min_freq[cpu]) 81 freq = cpu_min_freq[cpu]; 82 if (freq > cpu_max_freq[cpu]) 83 freq = cpu_max_freq[cpu]; 84 85 /* 86 * We're safe from concurrent calls to ->target() here 87 * as we hold the userspace_mutex lock. If we were calling 88 * cpufreq_driver_target, a deadlock situation might occur: 89 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock) 90 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex) 91 */ 92 ret = __cpufreq_driver_target(¤t_policy[cpu], freq, 93 CPUFREQ_RELATION_L); 94 95 err: 96 mutex_unlock(&userspace_mutex); 97 return ret; 98} 99 100 101/************************** sysfs interface ************************/ 102static ssize_t show_speed (struct cpufreq_policy *policy, char *buf) 103{ 104 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]); 105} 106 107static ssize_t 108store_speed (struct cpufreq_policy *policy, const char *buf, size_t count) 109{ 110 unsigned int freq = 0; 111 unsigned int ret; 112 113 ret = sscanf (buf, "%u", &freq); 114 if (ret != 1) 115 return -EINVAL; 116 117 cpufreq_set(freq, policy->cpu); 118 119 return count; 120} 121 122static struct freq_attr freq_attr_scaling_setspeed = 123{ 124 .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE }, 125 .show = show_speed, 126 .store = store_speed, 127}; 128 129static int cpufreq_governor_userspace(struct cpufreq_policy *policy, 130 unsigned int event) 131{ 132 unsigned int cpu = policy->cpu; 133 switch (event) { 134 case CPUFREQ_GOV_START: 135 if (!cpu_online(cpu)) 136 return -EINVAL; 137 BUG_ON(!policy->cur); 138 mutex_lock(&userspace_mutex); 139 cpu_is_managed[cpu] = 1; 140 cpu_min_freq[cpu] = policy->min; 141 cpu_max_freq[cpu] = policy->max; 142 cpu_cur_freq[cpu] = policy->cur; 143 cpu_set_freq[cpu] = policy->cur; 144 sysfs_create_file (&policy->kobj, &freq_attr_scaling_setspeed.attr); 145 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy)); 146 dprintk("managing cpu %u started (%u - %u kHz, currently %u kHz)\n", cpu, cpu_min_freq[cpu], cpu_max_freq[cpu], cpu_cur_freq[cpu]); 147 mutex_unlock(&userspace_mutex); 148 break; 149 case CPUFREQ_GOV_STOP: 150 mutex_lock(&userspace_mutex); 151 cpu_is_managed[cpu] = 0; 152 cpu_min_freq[cpu] = 0; 153 cpu_max_freq[cpu] = 0; 154 cpu_set_freq[cpu] = 0; 155 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr); 156 dprintk("managing cpu %u stopped\n", cpu); 157 mutex_unlock(&userspace_mutex); 158 break; 159 case CPUFREQ_GOV_LIMITS: 160 mutex_lock(&userspace_mutex); 161 cpu_min_freq[cpu] = policy->min; 162 cpu_max_freq[cpu] = policy->max; 163 dprintk("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n", cpu, cpu_min_freq[cpu], cpu_max_freq[cpu], cpu_cur_freq[cpu], cpu_set_freq[cpu]); 164 if (policy->max < cpu_set_freq[cpu]) { 165 __cpufreq_driver_target(¤t_policy[cpu], policy->max, 166 CPUFREQ_RELATION_H); 167 } else if (policy->min > cpu_set_freq[cpu]) { 168 __cpufreq_driver_target(¤t_policy[cpu], policy->min, 169 CPUFREQ_RELATION_L); 170 } else { 171 __cpufreq_driver_target(¤t_policy[cpu], cpu_set_freq[cpu], 172 CPUFREQ_RELATION_L); 173 } 174 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy)); 175 mutex_unlock(&userspace_mutex); 176 break; 177 } 178 return 0; 179} 180 181 182struct cpufreq_governor cpufreq_gov_userspace = { 183 .name = "userspace", 184 .governor = cpufreq_governor_userspace, 185 .owner = THIS_MODULE, 186}; 187EXPORT_SYMBOL(cpufreq_gov_userspace); 188 189static int __init cpufreq_gov_userspace_init(void) 190{ 191 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); 192 return cpufreq_register_governor(&cpufreq_gov_userspace); 193} 194 195 196static void __exit cpufreq_gov_userspace_exit(void) 197{ 198 cpufreq_unregister_governor(&cpufreq_gov_userspace); 199 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); 200} 201 202 203MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>"); 204MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'"); 205MODULE_LICENSE ("GPL"); 206 207fs_initcall(cpufreq_gov_userspace_init); 208module_exit(cpufreq_gov_userspace_exit); 209