2 * linux/drivers/cpufreq/cpufreq_userspace.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
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.
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>
23 #include <linux/sysfs.h>
25 #include <asm/uaccess.h>
29 * A few values needed by the userspace governor
31 static unsigned int cpu_max_freq
[NR_CPUS
];
32 static unsigned int cpu_min_freq
[NR_CPUS
];
33 static unsigned int cpu_cur_freq
[NR_CPUS
]; /* current CPU freq */
34 static unsigned int cpu_set_freq
[NR_CPUS
]; /* CPU freq desired by userspace */
35 static unsigned int cpu_is_managed
[NR_CPUS
];
36 static struct cpufreq_policy current_policy
[NR_CPUS
];
38 static DECLARE_MUTEX (userspace_sem
);
40 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
42 /* keep track of frequency transitions */
44 userspace_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
47 struct cpufreq_freqs
*freq
= data
;
49 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq
->cpu
, freq
->new);
50 cpu_cur_freq
[freq
->cpu
] = freq
->new;
55 static struct notifier_block userspace_cpufreq_notifier_block
= {
56 .notifier_call
= userspace_cpufreq_notifier
61 * cpufreq_set - set the CPU frequency
62 * @freq: target frequency in kHz
63 * @cpu: CPU for which the frequency is to be set
65 * Sets the CPU frequency to freq.
67 static int cpufreq_set(unsigned int freq
, unsigned int cpu
)
71 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", cpu
, freq
);
74 if (!cpu_is_managed
[cpu
])
77 cpu_set_freq
[cpu
] = freq
;
79 if (freq
< cpu_min_freq
[cpu
])
80 freq
= cpu_min_freq
[cpu
];
81 if (freq
> cpu_max_freq
[cpu
])
82 freq
= cpu_max_freq
[cpu
];
85 * We're safe from concurrent calls to ->target() here
86 * as we hold the userspace_sem lock. If we were calling
87 * cpufreq_driver_target, a deadlock situation might occur:
88 * A: cpufreq_set (lock userspace_sem) -> cpufreq_driver_target(lock policy->lock)
89 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_sem)
91 ret
= __cpufreq_driver_target(¤t_policy
[cpu
], freq
,
100 /************************** sysfs interface ************************/
101 static ssize_t
show_speed (struct cpufreq_policy
*policy
, char *buf
)
103 return sprintf (buf
, "%u\n", cpu_cur_freq
[policy
->cpu
]);
107 store_speed (struct cpufreq_policy
*policy
, const char *buf
, size_t count
)
109 unsigned int freq
= 0;
112 ret
= sscanf (buf
, "%u", &freq
);
116 cpufreq_set(freq
, policy
->cpu
);
121 static struct freq_attr freq_attr_scaling_setspeed
=
123 .attr
= { .name
= "scaling_setspeed", .mode
= 0644, .owner
= THIS_MODULE
},
125 .store
= store_speed
,
128 static int cpufreq_governor_userspace(struct cpufreq_policy
*policy
,
131 unsigned int cpu
= policy
->cpu
;
133 case CPUFREQ_GOV_START
:
134 if (!cpu_online(cpu
))
136 BUG_ON(!policy
->cur
);
137 down(&userspace_sem
);
138 cpu_is_managed
[cpu
] = 1;
139 cpu_min_freq
[cpu
] = policy
->min
;
140 cpu_max_freq
[cpu
] = policy
->max
;
141 cpu_cur_freq
[cpu
] = policy
->cur
;
142 cpu_set_freq
[cpu
] = policy
->cur
;
143 sysfs_create_file (&policy
->kobj
, &freq_attr_scaling_setspeed
.attr
);
144 memcpy (¤t_policy
[cpu
], policy
, sizeof(struct cpufreq_policy
));
145 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
]);
148 case CPUFREQ_GOV_STOP
:
149 down(&userspace_sem
);
150 cpu_is_managed
[cpu
] = 0;
151 cpu_min_freq
[cpu
] = 0;
152 cpu_max_freq
[cpu
] = 0;
153 cpu_set_freq
[cpu
] = 0;
154 sysfs_remove_file (&policy
->kobj
, &freq_attr_scaling_setspeed
.attr
);
155 dprintk("managing cpu %u stopped\n", cpu
);
158 case CPUFREQ_GOV_LIMITS
:
159 down(&userspace_sem
);
160 cpu_min_freq
[cpu
] = policy
->min
;
161 cpu_max_freq
[cpu
] = policy
->max
;
162 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
]);
163 if (policy
->max
< cpu_set_freq
[cpu
]) {
164 __cpufreq_driver_target(¤t_policy
[cpu
], policy
->max
,
166 } else if (policy
->min
> cpu_set_freq
[cpu
]) {
167 __cpufreq_driver_target(¤t_policy
[cpu
], policy
->min
,
170 __cpufreq_driver_target(¤t_policy
[cpu
], cpu_set_freq
[cpu
],
173 memcpy (¤t_policy
[cpu
], policy
, sizeof(struct cpufreq_policy
));
181 struct cpufreq_governor cpufreq_gov_userspace
= {
183 .governor
= cpufreq_governor_userspace
,
184 .owner
= THIS_MODULE
,
186 EXPORT_SYMBOL(cpufreq_gov_userspace
);
188 static int __init
cpufreq_gov_userspace_init(void)
190 cpufreq_register_notifier(&userspace_cpufreq_notifier_block
, CPUFREQ_TRANSITION_NOTIFIER
);
191 return cpufreq_register_governor(&cpufreq_gov_userspace
);
195 static void __exit
cpufreq_gov_userspace_exit(void)
197 cpufreq_unregister_governor(&cpufreq_gov_userspace
);
198 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block
, CPUFREQ_TRANSITION_NOTIFIER
);
202 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
203 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
204 MODULE_LICENSE ("GPL");
206 fs_initcall(cpufreq_gov_userspace_init
);
207 module_exit(cpufreq_gov_userspace_exit
);