2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-11-07 13:08:17 +08:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/delay.h>
|
2019-07-23 14:14:03 +08:00
|
|
|
#include <linux/pm_qos.h>
|
2005-11-07 13:08:17 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/wait.h>
|
2019-07-23 14:14:03 +08:00
|
|
|
#include <linux/cpu.h>
|
2005-11-07 13:08:17 +08:00
|
|
|
#include <linux/cpufreq.h>
|
|
|
|
|
2006-03-03 14:03:21 +08:00
|
|
|
#include <asm/prom.h>
|
|
|
|
|
2005-11-07 13:08:17 +08:00
|
|
|
#include "windfarm.h"
|
|
|
|
|
|
|
|
#define VERSION "0.3"
|
|
|
|
|
|
|
|
static int clamped;
|
|
|
|
static struct wf_control *clamp_control;
|
2019-07-23 14:14:03 +08:00
|
|
|
static struct dev_pm_qos_request qos_req;
|
|
|
|
static unsigned int min_freq, max_freq;
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
static int clamp_set(struct wf_control *ct, s32 value)
|
|
|
|
{
|
2019-07-23 14:14:03 +08:00
|
|
|
unsigned int freq;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
freq = min_freq;
|
2005-11-07 13:08:17 +08:00
|
|
|
printk(KERN_INFO "windfarm: Clamping CPU frequency to "
|
|
|
|
"minimum !\n");
|
2019-07-23 14:14:03 +08:00
|
|
|
} else {
|
|
|
|
freq = max_freq;
|
2005-11-07 13:08:17 +08:00
|
|
|
printk(KERN_INFO "windfarm: CPU frequency unclamped !\n");
|
2019-07-23 14:14:03 +08:00
|
|
|
}
|
2005-11-07 13:08:17 +08:00
|
|
|
clamped = value;
|
2019-07-23 14:14:03 +08:00
|
|
|
|
|
|
|
return dev_pm_qos_update_request(&qos_req, freq);
|
2005-11-07 13:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int clamp_get(struct wf_control *ct, s32 *value)
|
|
|
|
{
|
|
|
|
*value = clamped;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 clamp_min(struct wf_control *ct)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 clamp_max(struct wf_control *ct)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-12 01:38:45 +08:00
|
|
|
static const struct wf_control_ops clamp_ops = {
|
2005-11-07 13:08:17 +08:00
|
|
|
.set_value = clamp_set,
|
|
|
|
.get_value = clamp_get,
|
|
|
|
.get_min = clamp_min,
|
|
|
|
.get_max = clamp_max,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init wf_cpufreq_clamp_init(void)
|
|
|
|
{
|
2019-07-23 14:14:03 +08:00
|
|
|
struct cpufreq_policy *policy;
|
2005-11-07 13:08:17 +08:00
|
|
|
struct wf_control *clamp;
|
2019-07-23 14:14:03 +08:00
|
|
|
struct device *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
policy = cpufreq_cpu_get(0);
|
|
|
|
if (!policy) {
|
|
|
|
pr_warn("%s: cpufreq policy not found cpu0\n", __func__);
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
min_freq = policy->cpuinfo.min_freq;
|
|
|
|
max_freq = policy->cpuinfo.max_freq;
|
|
|
|
cpufreq_cpu_put(policy);
|
|
|
|
|
|
|
|
dev = get_cpu_device(0);
|
|
|
|
if (unlikely(!dev)) {
|
|
|
|
pr_warn("%s: No cpu device for cpu0\n", __func__);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
clamp = kmalloc(sizeof(struct wf_control), GFP_KERNEL);
|
|
|
|
if (clamp == NULL)
|
|
|
|
return -ENOMEM;
|
2019-07-23 14:14:03 +08:00
|
|
|
|
|
|
|
ret = dev_pm_qos_add_request(dev, &qos_req, DEV_PM_QOS_MAX_FREQUENCY,
|
|
|
|
max_freq);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
|
|
|
|
ret);
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
2005-11-07 13:08:17 +08:00
|
|
|
clamp->ops = &clamp_ops;
|
|
|
|
clamp->name = "cpufreq-clamp";
|
2019-07-23 14:14:03 +08:00
|
|
|
ret = wf_register_control(clamp);
|
|
|
|
if (ret)
|
2005-11-07 13:08:17 +08:00
|
|
|
goto fail;
|
|
|
|
clamp_control = clamp;
|
|
|
|
return 0;
|
|
|
|
fail:
|
2019-07-23 14:14:03 +08:00
|
|
|
dev_pm_qos_remove_request(&qos_req);
|
|
|
|
|
|
|
|
free:
|
2005-11-07 13:08:17 +08:00
|
|
|
kfree(clamp);
|
2019-07-23 14:14:03 +08:00
|
|
|
return ret;
|
2005-11-07 13:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit wf_cpufreq_clamp_exit(void)
|
|
|
|
{
|
2019-07-23 14:14:03 +08:00
|
|
|
if (clamp_control) {
|
2005-11-07 13:08:17 +08:00
|
|
|
wf_unregister_control(clamp_control);
|
2019-07-23 14:14:03 +08:00
|
|
|
dev_pm_qos_remove_request(&qos_req);
|
|
|
|
}
|
2005-11-07 13:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module_init(wf_cpufreq_clamp_init);
|
|
|
|
module_exit(wf_cpufreq_clamp_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
|
|
|
|
MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|