blob: 7b726f00f1834d1cd4aa544090215ad9562e4c08 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +11002#include <linux/types.h>
3#include <linux/errno.h>
4#include <linux/kernel.h>
5#include <linux/delay.h>
Viresh Kumardce2e3a2019-07-23 11:44:03 +05306#include <linux/pm_qos.h>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +11007#include <linux/slab.h>
8#include <linux/init.h>
9#include <linux/wait.h>
Viresh Kumardce2e3a2019-07-23 11:44:03 +053010#include <linux/cpu.h>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110011#include <linux/cpufreq.h>
12
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +110013#include <asm/prom.h>
14
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110015#include "windfarm.h"
16
17#define VERSION "0.3"
18
19static int clamped;
20static struct wf_control *clamp_control;
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020021static struct freq_qos_request qos_req;
Viresh Kumardce2e3a2019-07-23 11:44:03 +053022static unsigned int min_freq, max_freq;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110023
24static int clamp_set(struct wf_control *ct, s32 value)
25{
Viresh Kumardce2e3a2019-07-23 11:44:03 +053026 unsigned int freq;
27
28 if (value) {
29 freq = min_freq;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110030 printk(KERN_INFO "windfarm: Clamping CPU frequency to "
31 "minimum !\n");
Viresh Kumardce2e3a2019-07-23 11:44:03 +053032 } else {
33 freq = max_freq;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110034 printk(KERN_INFO "windfarm: CPU frequency unclamped !\n");
Viresh Kumardce2e3a2019-07-23 11:44:03 +053035 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110036 clamped = value;
Viresh Kumardce2e3a2019-07-23 11:44:03 +053037
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020038 return freq_qos_update_request(&qos_req, freq);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110039}
40
41static int clamp_get(struct wf_control *ct, s32 *value)
42{
43 *value = clamped;
44 return 0;
45}
46
47static s32 clamp_min(struct wf_control *ct)
48{
49 return 0;
50}
51
52static s32 clamp_max(struct wf_control *ct)
53{
54 return 1;
55}
56
Bhumika Goyal1ad35f62017-08-11 23:08:45 +053057static const struct wf_control_ops clamp_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110058 .set_value = clamp_set,
59 .get_value = clamp_get,
60 .get_min = clamp_min,
61 .get_max = clamp_max,
62 .owner = THIS_MODULE,
63};
64
65static int __init wf_cpufreq_clamp_init(void)
66{
Viresh Kumardce2e3a2019-07-23 11:44:03 +053067 struct cpufreq_policy *policy;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110068 struct wf_control *clamp;
Viresh Kumardce2e3a2019-07-23 11:44:03 +053069 struct device *dev;
70 int ret;
71
72 policy = cpufreq_cpu_get(0);
73 if (!policy) {
74 pr_warn("%s: cpufreq policy not found cpu0\n", __func__);
75 return -EPROBE_DEFER;
76 }
77
78 min_freq = policy->cpuinfo.min_freq;
79 max_freq = policy->cpuinfo.max_freq;
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020080
81 ret = freq_qos_add_request(&policy->constraints, &qos_req, FREQ_QOS_MAX,
82 max_freq);
83
Viresh Kumardce2e3a2019-07-23 11:44:03 +053084 cpufreq_cpu_put(policy);
85
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020086 if (ret < 0) {
87 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
88 ret);
89 return ret;
90 }
91
Viresh Kumardce2e3a2019-07-23 11:44:03 +053092 dev = get_cpu_device(0);
93 if (unlikely(!dev)) {
94 pr_warn("%s: No cpu device for cpu0\n", __func__);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +020095 ret = -ENODEV;
96 goto fail;
Viresh Kumardce2e3a2019-07-23 11:44:03 +053097 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110098
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110099 clamp = kmalloc(sizeof(struct wf_control), GFP_KERNEL);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200100 if (clamp == NULL) {
101 ret = -ENOMEM;
102 goto fail;
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530103 }
104
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100105 clamp->ops = &clamp_ops;
106 clamp->name = "cpufreq-clamp";
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530107 ret = wf_register_control(clamp);
108 if (ret)
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200109 goto free;
110
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100111 clamp_control = clamp;
112 return 0;
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530113
114 free:
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100115 kfree(clamp);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200116 fail:
117 freq_qos_remove_request(&qos_req);
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530118 return ret;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100119}
120
121static void __exit wf_cpufreq_clamp_exit(void)
122{
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530123 if (clamp_control) {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100124 wf_unregister_control(clamp_control);
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200125 freq_qos_remove_request(&qos_req);
Viresh Kumardce2e3a2019-07-23 11:44:03 +0530126 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100127}
128
129
130module_init(wf_cpufreq_clamp_init);
131module_exit(wf_cpufreq_clamp_exit);
132
133MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
134MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control");
135MODULE_LICENSE("GPL");
136