blob: 88301e53f0856bf44f90f9f0d6ac5c56808d12f0 [file] [log] [blame]
Christian Krafft880e7102008-07-16 05:51:43 +10001/*
2 * spu aware cpufreq governor for the cell processor
3 *
4 * © Copyright IBM Corporation 2006-2008
5 *
6 * Author: Christian Krafft <krafft@de.ibm.com>
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 as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/cpufreq.h>
24#include <linux/sched.h>
Paul Gortmaker7dfe2932011-05-27 13:23:32 -040025#include <linux/module.h>
Christian Krafft880e7102008-07-16 05:51:43 +100026#include <linux/timer.h>
27#include <linux/workqueue.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Christian Krafft880e7102008-07-16 05:51:43 +100029#include <asm/machdep.h>
30#include <asm/spu.h>
31
32#define POLL_TIME 100000 /* in µs */
33#define EXP 753 /* exp(-1) in fixed-point */
34
35struct spu_gov_info_struct {
36 unsigned long busy_spus; /* fixed-point */
37 struct cpufreq_policy *policy;
38 struct delayed_work work;
39 unsigned int poll_int; /* µs */
40};
41static DEFINE_PER_CPU(struct spu_gov_info_struct, spu_gov_info);
42
Christian Krafft880e7102008-07-16 05:51:43 +100043static int calc_freq(struct spu_gov_info_struct *info)
44{
45 int cpu;
46 int busy_spus;
47
48 cpu = info->policy->cpu;
49 busy_spus = atomic_read(&cbe_spu_info[cpu_to_node(cpu)].busy_spus);
50
51 CALC_LOAD(info->busy_spus, EXP, busy_spus * FIXED_1);
52 pr_debug("cpu %d: busy_spus=%d, info->busy_spus=%ld\n",
53 cpu, busy_spus, info->busy_spus);
54
55 return info->policy->max * info->busy_spus / FIXED_1;
56}
57
58static void spu_gov_work(struct work_struct *work)
59{
60 struct spu_gov_info_struct *info;
61 int delay;
62 unsigned long target_freq;
63
64 info = container_of(work, struct spu_gov_info_struct, work.work);
65
66 /* after cancel_delayed_work_sync we unset info->policy */
67 BUG_ON(info->policy == NULL);
68
69 target_freq = calc_freq(info);
70 __cpufreq_driver_target(info->policy, target_freq, CPUFREQ_RELATION_H);
71
72 delay = usecs_to_jiffies(info->poll_int);
Tejun Heob18ae082011-01-03 03:49:25 +000073 schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
Christian Krafft880e7102008-07-16 05:51:43 +100074}
75
76static void spu_gov_init_work(struct spu_gov_info_struct *info)
77{
78 int delay = usecs_to_jiffies(info->poll_int);
Tejun Heo203b42f2012-08-21 13:18:23 -070079 INIT_DEFERRABLE_WORK(&info->work, spu_gov_work);
Tejun Heob18ae082011-01-03 03:49:25 +000080 schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
Christian Krafft880e7102008-07-16 05:51:43 +100081}
82
83static void spu_gov_cancel_work(struct spu_gov_info_struct *info)
84{
85 cancel_delayed_work_sync(&info->work);
86}
87
Rafael J. Wysockie7888922016-06-02 23:24:15 +020088static int spu_gov_start(struct cpufreq_policy *policy)
Christian Krafft880e7102008-07-16 05:51:43 +100089{
90 unsigned int cpu = policy->cpu;
Rafael J. Wysockie7888922016-06-02 23:24:15 +020091 struct spu_gov_info_struct *info = &per_cpu(spu_gov_info, cpu);
92 struct spu_gov_info_struct *affected_info;
Christian Krafft880e7102008-07-16 05:51:43 +100093 int i;
Christian Krafft880e7102008-07-16 05:51:43 +100094
Rafael J. Wysockie7888922016-06-02 23:24:15 +020095 if (!cpu_online(cpu)) {
96 printk(KERN_ERR "cpu %d is not online\n", cpu);
97 return -EINVAL;
Christian Krafft880e7102008-07-16 05:51:43 +100098 }
99
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200100 if (!policy->cur) {
101 printk(KERN_ERR "no cpu specified in policy\n");
102 return -EINVAL;
103 }
104
105 /* initialize spu_gov_info for all affected cpus */
106 for_each_cpu(i, policy->cpus) {
107 affected_info = &per_cpu(spu_gov_info, i);
108 affected_info->policy = policy;
109 }
110
111 info->poll_int = POLL_TIME;
112
113 /* setup timer */
114 spu_gov_init_work(info);
115
116 return 0;
117}
118
119static void spu_gov_stop(struct cpufreq_policy *policy)
120{
121 unsigned int cpu = policy->cpu;
122 struct spu_gov_info_struct *info = &per_cpu(spu_gov_info, cpu);
123 int i;
124
125 /* cancel timer */
126 spu_gov_cancel_work(info);
127
128 /* clean spu_gov_info for all affected cpus */
129 for_each_cpu (i, policy->cpus) {
130 info = &per_cpu(spu_gov_info, i);
131 info->policy = NULL;
132 }
Christian Krafft880e7102008-07-16 05:51:43 +1000133}
134
135static struct cpufreq_governor spu_governor = {
136 .name = "spudemand",
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200137 .start = spu_gov_start,
138 .stop = spu_gov_stop,
Christian Krafft880e7102008-07-16 05:51:43 +1000139 .owner = THIS_MODULE,
140};
141
142/*
143 * module init and destoy
144 */
145
146static int __init spu_gov_init(void)
147{
148 int ret;
149
Christian Krafft880e7102008-07-16 05:51:43 +1000150 ret = cpufreq_register_governor(&spu_governor);
Tejun Heob18ae082011-01-03 03:49:25 +0000151 if (ret)
Christian Krafft880e7102008-07-16 05:51:43 +1000152 printk(KERN_ERR "registration of governor failed\n");
Christian Krafft880e7102008-07-16 05:51:43 +1000153 return ret;
154}
155
156static void __exit spu_gov_exit(void)
157{
158 cpufreq_unregister_governor(&spu_governor);
Christian Krafft880e7102008-07-16 05:51:43 +1000159}
160
161
162module_init(spu_gov_init);
163module_exit(spu_gov_exit);
164
165MODULE_LICENSE("GPL");
166MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
167