5820fb9a4521a042894fcd382c5b8a62697edbf9
[powerpc.git] / arch / powerpc / platforms / cell / cbe_cpufreq.c
1 /*
2  * cpufreq driver for the cell processor
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
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/timer.h>
25
26 #include <asm/hw_irq.h>
27 #include <asm/io.h>
28 #include <asm/machdep.h>
29 #include <asm/processor.h>
30 #include <asm/prom.h>
31 #include <asm/time.h>
32 #include <asm/pmi.h>
33 #include <asm/of_platform.h>
34
35 #include "cbe_regs.h"
36
37 static DEFINE_MUTEX(cbe_switch_mutex);
38
39
40 /* the CBE supports an 8 step frequency scaling */
41 static struct cpufreq_frequency_table cbe_freqs[] = {
42         {1,     0},
43         {2,     0},
44         {3,     0},
45         {4,     0},
46         {5,     0},
47         {6,     0},
48         {8,     0},
49         {10,    0},
50         {0,     CPUFREQ_TABLE_END},
51 };
52
53 /* to write to MIC register */
54 static u64 MIC_Slow_Fast_Timer_table[] = {
55         [0 ... 7] = 0x007fc00000000000ull,
56 };
57
58 /* more values for the MIC */
59 static u64 MIC_Slow_Next_Timer_table[] = {
60         0x0000240000000000ull,
61         0x0000268000000000ull,
62         0x000029C000000000ull,
63         0x00002D0000000000ull,
64         0x0000300000000000ull,
65         0x0000334000000000ull,
66         0x000039C000000000ull,
67         0x00003FC000000000ull,
68 };
69
70 static unsigned int pmi_frequency_limit = 0;
71
72 /*
73  * hardware specific functions
74  */
75
76 static bool cbe_cpufreq_has_pmi;
77
78 #ifdef CONFIG_PPC_PMI
79 static int set_pmode_pmi(int cpu, unsigned int pmode)
80 {
81         int ret;
82         pmi_message_t pmi_msg;
83 #ifdef DEBUG
84         long time;
85 #endif
86
87         pmi_msg.type = PMI_TYPE_FREQ_CHANGE;
88         pmi_msg.data1 = cbe_cpu_to_node(cpu);
89         pmi_msg.data2 = pmode;
90
91 #ifdef DEBUG
92         time = jiffies;
93 #endif
94
95         pmi_send_message(pmi_msg);
96         ret = pmi_msg.data2;
97
98         pr_debug("PMI returned slow mode %d\n", ret);
99
100 #ifdef DEBUG
101         time = jiffies - time; /* actual cycles (not cpu cycles!) */
102         time = jiffies_to_msecs(time);
103         pr_debug("had to wait %lu ms for a transition using PMI.\n", time);
104 #endif
105         return ret;
106 }
107 #endif
108
109 static int get_pmode(int cpu)
110 {
111         int ret;
112         struct cbe_pmd_regs __iomem *pmd_regs;
113
114         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
115         ret = in_be64(&pmd_regs->pmsr) & 0x07;
116
117         return ret;
118 }
119
120 static int set_pmode_reg(int cpu, unsigned int pmode)
121 {
122         struct cbe_pmd_regs __iomem *pmd_regs;
123         struct cbe_mic_tm_regs __iomem *mic_tm_regs;
124         u64 flags;
125         u64 value;
126 #ifdef DEBUG
127         long time;
128 #endif
129
130         local_irq_save(flags);
131
132         mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
133         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
134
135 #ifdef DEBUG
136         time = jiffies;
137 #endif
138         out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
139         out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
140
141         out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
142         out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
143
144         value = in_be64(&pmd_regs->pmcr);
145         /* set bits to zero */
146         value &= 0xFFFFFFFFFFFFFFF8ull;
147         /* set bits to next pmode */
148         value |= pmode;
149
150         out_be64(&pmd_regs->pmcr, value);
151
152 #ifdef DEBUG
153         /* wait until new pmode appears in status register */
154         value = in_be64(&pmd_regs->pmsr) & 0x07;
155         while(value != pmode) {
156                 cpu_relax();
157                 value = in_be64(&pmd_regs->pmsr) & 0x07;
158         }
159
160         time = jiffies - time;
161         time = jiffies_to_msecs(time);
162         pr_debug("had to wait %lu ms for a transition using " \
163                  "the pervasive unit.\n", time);
164 #endif
165         local_irq_restore(flags);
166
167         return 0;
168 }
169
170 static int set_pmode(int cpu, unsigned int slow_mode)
171 {
172 #ifdef CONFIG_PPC_PMI
173         if (cbe_cpufreq_has_pmi)
174                 return set_pmode_pmi(cpu, slow_mode);
175 #endif
176         return set_pmode_reg(cpu, slow_mode);
177 }
178
179 static void cbe_cpufreq_handle_pmi(pmi_message_t pmi_msg)
180 {
181         u8 cpu;
182         u8 cbe_pmode_new;
183
184         BUG_ON(pmi_msg.type != PMI_TYPE_FREQ_CHANGE);
185
186         cpu = cbe_node_to_cpu(pmi_msg.data1);
187         cbe_pmode_new = pmi_msg.data2;
188
189         pmi_frequency_limit = cbe_freqs[cbe_pmode_new].frequency;
190
191         pr_debug("cbe_handle_pmi: max freq=%d\n", pmi_frequency_limit);
192 }
193
194 static int pmi_notifier(struct notifier_block *nb,
195                                        unsigned long event, void *data)
196 {
197         struct cpufreq_policy *policy = data;
198
199         if (event != CPUFREQ_INCOMPATIBLE)
200                 return 0;
201
202         cpufreq_verify_within_limits(policy, 0, pmi_frequency_limit);
203         return 0;
204 }
205
206 static struct notifier_block pmi_notifier_block = {
207         .notifier_call = pmi_notifier,
208 };
209
210 static struct pmi_handler cbe_pmi_handler = {
211         .type                   = PMI_TYPE_FREQ_CHANGE,
212         .handle_pmi_message     = cbe_cpufreq_handle_pmi,
213 };
214
215
216 /*
217  * cpufreq functions
218  */
219
220 static int cbe_cpufreq_cpu_init(struct cpufreq_policy *policy)
221 {
222         const u32 *max_freqp;
223         u32 max_freq;
224         int i, cur_pmode;
225         struct device_node *cpu;
226
227         cpu = of_get_cpu_node(policy->cpu, NULL);
228
229         if (!cpu)
230                 return -ENODEV;
231
232         pr_debug("init cpufreq on CPU %d\n", policy->cpu);
233
234         max_freqp = of_get_property(cpu, "clock-frequency", NULL);
235
236         if (!max_freqp)
237                 return -EINVAL;
238
239         /* we need the freq in kHz */
240         max_freq = *max_freqp / 1000;
241
242         pr_debug("max clock-frequency is at %u kHz\n", max_freq);
243         pr_debug("initializing frequency table\n");
244
245         /* initialize frequency table */
246         for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
247                 cbe_freqs[i].frequency = max_freq / cbe_freqs[i].index;
248                 pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
249         }
250
251         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
252         /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
253         policy->cpuinfo.transition_latency = 25000;
254
255         cur_pmode = get_pmode(policy->cpu);
256         pr_debug("current pmode is at %d\n",cur_pmode);
257
258         policy->cur = cbe_freqs[cur_pmode].frequency;
259
260 #ifdef CONFIG_SMP
261         policy->cpus = cpu_sibling_map[policy->cpu];
262 #endif
263
264         cpufreq_frequency_table_get_attr(cbe_freqs, policy->cpu);
265
266         if (cbe_cpufreq_has_pmi) {
267                 /* frequency might get limited later, initialize limit with max_freq */
268                 pmi_frequency_limit = max_freq;
269                 cpufreq_register_notifier(&pmi_notifier_block, CPUFREQ_POLICY_NOTIFIER);
270         }
271
272         /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
273         return cpufreq_frequency_table_cpuinfo(policy, cbe_freqs);
274 }
275
276 static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
277 {
278         if (cbe_cpufreq_has_pmi)
279                 cpufreq_unregister_notifier(&pmi_notifier_block, CPUFREQ_POLICY_NOTIFIER);
280
281         cpufreq_frequency_table_put_attr(policy->cpu);
282         return 0;
283 }
284
285 static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
286 {
287         return cpufreq_frequency_table_verify(policy, cbe_freqs);
288 }
289
290
291 static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
292                             unsigned int relation)
293 {
294         int rc;
295         struct cpufreq_freqs freqs;
296         int cbe_pmode_new;
297
298         cpufreq_frequency_table_target(policy,
299                                        cbe_freqs,
300                                        target_freq,
301                                        relation,
302                                        &cbe_pmode_new);
303
304         freqs.old = policy->cur;
305         freqs.new = cbe_freqs[cbe_pmode_new].frequency;
306         freqs.cpu = policy->cpu;
307
308         mutex_lock(&cbe_switch_mutex);
309         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
310
311         pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
312                  policy->cpu,
313                  cbe_freqs[cbe_pmode_new].frequency,
314                  cbe_freqs[cbe_pmode_new].index);
315
316         rc = set_pmode(policy->cpu, cbe_pmode_new);
317         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
318         mutex_unlock(&cbe_switch_mutex);
319
320         return rc;
321 }
322
323 static struct cpufreq_driver cbe_cpufreq_driver = {
324         .verify         = cbe_cpufreq_verify,
325         .target         = cbe_cpufreq_target,
326         .init           = cbe_cpufreq_cpu_init,
327         .exit           = cbe_cpufreq_cpu_exit,
328         .name           = "cbe-cpufreq",
329         .owner          = THIS_MODULE,
330         .flags          = CPUFREQ_CONST_LOOPS,
331 };
332
333 /*
334  * module init and destoy
335  */
336
337 static int __init cbe_cpufreq_init(void)
338 {
339         if (!machine_is(cell))
340                 return -ENODEV;
341
342         cbe_cpufreq_has_pmi = pmi_register_handler(&cbe_pmi_handler) == 0;
343
344         return cpufreq_register_driver(&cbe_cpufreq_driver);
345 }
346
347 static void __exit cbe_cpufreq_exit(void)
348 {
349         cpufreq_unregister_driver(&cbe_cpufreq_driver);
350
351         if (cbe_cpufreq_has_pmi)
352                 pmi_unregister_handler(&cbe_pmi_handler);
353 }
354
355 module_init(cbe_cpufreq_init);
356 module_exit(cbe_cpufreq_exit);
357
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");