2 * cpufreq driver for the cell processor
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Christian Krafft <krafft@de.ibm.com>
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)
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.
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.
23 #include <linux/cpufreq.h>
24 #include <linux/timer.h>
26 #include <asm/hw_irq.h>
28 #include <asm/machdep.h>
29 #include <asm/processor.h>
33 #include <asm/of_platform.h>
37 static DEFINE_MUTEX(cbe_switch_mutex
);
40 /* the CBE supports an 8 step frequency scaling */
41 static struct cpufreq_frequency_table cbe_freqs
[] = {
50 {0, CPUFREQ_TABLE_END
},
53 /* to write to MIC register */
54 static u64 MIC_Slow_Fast_Timer_table
[] = {
55 [0 ... 7] = 0x007fc00000000000ull
,
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
,
70 static unsigned int pmi_frequency_limit
= 0;
73 * hardware specific functions
76 static bool cbe_cpufreq_has_pmi
;
79 static int set_pmode_pmi(int cpu
, unsigned int pmode
)
82 pmi_message_t pmi_msg
;
87 pmi_msg
.type
= PMI_TYPE_FREQ_CHANGE
;
88 pmi_msg
.data1
= cbe_cpu_to_node(cpu
);
89 pmi_msg
.data2
= pmode
;
92 time
= (u64
) get_cycles();
95 pmi_send_message(pmi_msg
);
98 pr_debug("PMI returned slow mode %d\n", ret
);
101 time
= (u64
) get_cycles() - time
; /* actual cycles (not cpu cycles!) */
102 time
= 1000000000 * time
/ CLOCK_TICK_RATE
; /* time in ns (10^-9) */
103 pr_debug("had to wait %lu ns for a transition\n", time
);
109 static int get_pmode(int cpu
)
112 struct cbe_pmd_regs __iomem
*pmd_regs
;
114 pmd_regs
= cbe_get_cpu_pmd_regs(cpu
);
115 ret
= in_be64(&pmd_regs
->pmsr
) & 0x07;
120 static int set_pmode_reg(int cpu
, unsigned int pmode
)
122 struct cbe_pmd_regs __iomem
*pmd_regs
;
123 struct cbe_mic_tm_regs __iomem
*mic_tm_regs
;
127 local_irq_save(flags
);
129 mic_tm_regs
= cbe_get_cpu_mic_tm_regs(cpu
);
130 pmd_regs
= cbe_get_cpu_pmd_regs(cpu
);
132 pr_debug("pm register is mapped at %p\n", &pmd_regs
->pmcr
);
133 pr_debug("mic register is mapped at %p\n", &mic_tm_regs
->slow_fast_timer_0
);
135 out_be64(&mic_tm_regs
->slow_fast_timer_0
, MIC_Slow_Fast_Timer_table
[pmode
]);
136 out_be64(&mic_tm_regs
->slow_fast_timer_1
, MIC_Slow_Fast_Timer_table
[pmode
]);
138 out_be64(&mic_tm_regs
->slow_next_timer_0
, MIC_Slow_Next_Timer_table
[pmode
]);
139 out_be64(&mic_tm_regs
->slow_next_timer_1
, MIC_Slow_Next_Timer_table
[pmode
]);
141 value
= in_be64(&pmd_regs
->pmcr
);
142 /* set bits to zero */
143 value
&= 0xFFFFFFFFFFFFFFF8ull
;
144 /* set bits to next pmode */
147 out_be64(&pmd_regs
->pmcr
, value
);
149 /* wait until new pmode appears in status register */
150 value
= in_be64(&pmd_regs
->pmsr
) & 0x07;
151 while(value
!= pmode
) {
153 value
= in_be64(&pmd_regs
->pmsr
) & 0x07;
156 local_irq_restore(flags
);
161 static int set_pmode(int cpu
, unsigned int slow_mode
)
163 #ifdef CONFIG_PPC_PMI
164 if (cbe_cpufreq_has_pmi
)
165 return set_pmode_pmi(cpu
, slow_mode
);
167 return set_pmode_reg(cpu
, slow_mode
);
170 static void cbe_cpufreq_handle_pmi(pmi_message_t pmi_msg
)
175 BUG_ON(pmi_msg
.type
!= PMI_TYPE_FREQ_CHANGE
);
177 cpu
= cbe_node_to_cpu(pmi_msg
.data1
);
178 cbe_pmode_new
= pmi_msg
.data2
;
180 pmi_frequency_limit
= cbe_freqs
[cbe_pmode_new
].frequency
;
182 pr_debug("cbe_handle_pmi: max freq=%d\n", pmi_frequency_limit
);
185 static int pmi_notifier(struct notifier_block
*nb
,
186 unsigned long event
, void *data
)
188 struct cpufreq_policy
*policy
= data
;
190 if (event
!= CPUFREQ_INCOMPATIBLE
)
193 cpufreq_verify_within_limits(policy
, 0, pmi_frequency_limit
);
197 static struct notifier_block pmi_notifier_block
= {
198 .notifier_call
= pmi_notifier
,
201 static struct pmi_handler cbe_pmi_handler
= {
202 .type
= PMI_TYPE_FREQ_CHANGE
,
203 .handle_pmi_message
= cbe_cpufreq_handle_pmi
,
211 static int cbe_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
213 const u32
*max_freqp
;
216 struct device_node
*cpu
;
218 cpu
= of_get_cpu_node(policy
->cpu
, NULL
);
223 pr_debug("init cpufreq on CPU %d\n", policy
->cpu
);
225 max_freqp
= of_get_property(cpu
, "clock-frequency", NULL
);
230 /* we need the freq in kHz */
231 max_freq
= *max_freqp
/ 1000;
233 pr_debug("max clock-frequency is at %u kHz\n", max_freq
);
234 pr_debug("initializing frequency table\n");
236 /* initialize frequency table */
237 for (i
=0; cbe_freqs
[i
].frequency
!=CPUFREQ_TABLE_END
; i
++) {
238 cbe_freqs
[i
].frequency
= max_freq
/ cbe_freqs
[i
].index
;
239 pr_debug("%d: %d\n", i
, cbe_freqs
[i
].frequency
);
242 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
243 /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
244 policy
->cpuinfo
.transition_latency
= 25000;
246 cur_pmode
= get_pmode(policy
->cpu
);
247 pr_debug("current pmode is at %d\n",cur_pmode
);
249 policy
->cur
= cbe_freqs
[cur_pmode
].frequency
;
252 policy
->cpus
= cpu_sibling_map
[policy
->cpu
];
255 cpufreq_frequency_table_get_attr(cbe_freqs
, policy
->cpu
);
257 if (cbe_cpufreq_has_pmi
) {
258 /* frequency might get limited later, initialize limit with max_freq */
259 pmi_frequency_limit
= max_freq
;
260 cpufreq_register_notifier(&pmi_notifier_block
, CPUFREQ_POLICY_NOTIFIER
);
263 /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
264 return cpufreq_frequency_table_cpuinfo(policy
, cbe_freqs
);
267 static int cbe_cpufreq_cpu_exit(struct cpufreq_policy
*policy
)
269 if (cbe_cpufreq_has_pmi
)
270 cpufreq_unregister_notifier(&pmi_notifier_block
, CPUFREQ_POLICY_NOTIFIER
);
272 cpufreq_frequency_table_put_attr(policy
->cpu
);
276 static int cbe_cpufreq_verify(struct cpufreq_policy
*policy
)
278 return cpufreq_frequency_table_verify(policy
, cbe_freqs
);
282 static int cbe_cpufreq_target(struct cpufreq_policy
*policy
, unsigned int target_freq
,
283 unsigned int relation
)
286 struct cpufreq_freqs freqs
;
289 cpufreq_frequency_table_target(policy
,
295 freqs
.old
= policy
->cur
;
296 freqs
.new = cbe_freqs
[cbe_pmode_new
].frequency
;
297 freqs
.cpu
= policy
->cpu
;
299 mutex_lock(&cbe_switch_mutex
);
300 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
302 pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
304 cbe_freqs
[cbe_pmode_new
].frequency
,
305 cbe_freqs
[cbe_pmode_new
].index
);
307 rc
= set_pmode(policy
->cpu
, cbe_pmode_new
);
308 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
309 mutex_unlock(&cbe_switch_mutex
);
314 static struct cpufreq_driver cbe_cpufreq_driver
= {
315 .verify
= cbe_cpufreq_verify
,
316 .target
= cbe_cpufreq_target
,
317 .init
= cbe_cpufreq_cpu_init
,
318 .exit
= cbe_cpufreq_cpu_exit
,
319 .name
= "cbe-cpufreq",
320 .owner
= THIS_MODULE
,
321 .flags
= CPUFREQ_CONST_LOOPS
,
325 * module init and destoy
328 static int __init
cbe_cpufreq_init(void)
330 if (!machine_is(cell
))
333 cbe_cpufreq_has_pmi
= pmi_register_handler(&cbe_pmi_handler
) == 0;
335 return cpufreq_register_driver(&cbe_cpufreq_driver
);
338 static void __exit
cbe_cpufreq_exit(void)
340 cpufreq_unregister_driver(&cbe_cpufreq_driver
);
342 if (cbe_cpufreq_has_pmi
)
343 pmi_unregister_handler(&cbe_pmi_handler
);
346 module_init(cbe_cpufreq_init
);
347 module_exit(cbe_cpufreq_exit
);
349 MODULE_LICENSE("GPL");
350 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");