ppc_cbe_cpufreq.c 4.6 KB
Newer Older
1 2 3
/*
 * cpufreq driver for the cell processor
 *
4
 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005-2007
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Author: Christian Krafft <krafft@de.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/cpufreq.h>
24
#include <linux/module.h>
25 26
#include <linux/of_platform.h>

27
#include <asm/machdep.h>
28
#include <asm/prom.h>
29
#include <asm/cell-regs.h>
30 31

#include "ppc_cbe_cpufreq.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

static DEFINE_MUTEX(cbe_switch_mutex);


/* the CBE supports an 8 step frequency scaling */
static struct cpufreq_frequency_table cbe_freqs[] = {
	{1,	0},
	{2,	0},
	{3,	0},
	{4,	0},
	{5,	0},
	{6,	0},
	{8,	0},
	{10,	0},
	{0,	CPUFREQ_TABLE_END},
};

/*
 * hardware specific functions
 */

53
static int set_pmode(unsigned int cpu, unsigned int slow_mode)
54
{
55
	int rc;
56

57
	if (cbe_cpufreq_has_pmi)
58
		rc = cbe_cpufreq_set_pmode_pmi(cpu, slow_mode);
59
	else
60
		rc = cbe_cpufreq_set_pmode(cpu, slow_mode);
61

62
	pr_debug("register contains slow mode %d\n", cbe_cpufreq_get_pmode(cpu));
63 64

	return rc;
65 66
}

67 68 69 70
/*
 * cpufreq functions
 */

71
static int cbe_cpufreq_cpu_init(struct cpufreq_policy *policy)
72
{
73 74
	const u32 *max_freqp;
	u32 max_freq;
75 76 77 78 79
	int i, cur_pmode;
	struct device_node *cpu;

	cpu = of_get_cpu_node(policy->cpu, NULL);

80
	if (!cpu)
81 82 83 84
		return -ENODEV;

	pr_debug("init cpufreq on CPU %d\n", policy->cpu);

85 86 87 88 89 90 91 92 93
	/*
	 * Let's check we can actually get to the CELL regs
	 */
	if (!cbe_get_cpu_pmd_regs(policy->cpu) ||
	    !cbe_get_cpu_mic_tm_regs(policy->cpu)) {
		pr_info("invalid CBE regs pointers for cpufreq\n");
		return -EINVAL;
	}

94
	max_freqp = of_get_property(cpu, "clock-frequency", NULL);
95

96 97
	of_node_put(cpu);

98
	if (!max_freqp)
99 100
		return -EINVAL;

101
	/* we need the freq in kHz */
102
	max_freq = *max_freqp / 1000;
103

104
	pr_debug("max clock-frequency is at %u kHz\n", max_freq);
105 106
	pr_debug("initializing frequency table\n");

107
	/* initialize frequency table */
108
	for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
109
		cbe_freqs[i].frequency = max_freq / cbe_freqs[i].driver_data;
110 111 112
		pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
	}

113 114
	/* if DEBUG is enabled set_pmode() measures the latency
	 * of a transition */
115 116
	policy->cpuinfo.transition_latency = 25000;

117
	cur_pmode = cbe_cpufreq_get_pmode(policy->cpu);
118 119 120 121 122
	pr_debug("current pmode is at %d\n",cur_pmode);

	policy->cur = cbe_freqs[cur_pmode].frequency;

#ifdef CONFIG_SMP
123
	cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
124 125
#endif

126 127
	/* this ensures that policy->cpuinfo_min
	 * and policy->cpuinfo_max are set correctly */
128
	return cpufreq_table_validate_and_show(policy, cbe_freqs);
129 130
}

131 132 133
static int cbe_cpufreq_target(struct cpufreq_policy *policy,
			      unsigned int target_freq,
			      unsigned int relation)
134 135 136
{
	int rc;
	struct cpufreq_freqs freqs;
137
	unsigned int cbe_pmode_new;
138 139 140 141 142 143 144 145 146 147

	cpufreq_frequency_table_target(policy,
				       cbe_freqs,
				       target_freq,
				       relation,
				       &cbe_pmode_new);

	freqs.old = policy->cur;
	freqs.new = cbe_freqs[cbe_pmode_new].frequency;

148
	mutex_lock(&cbe_switch_mutex);
149
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
150

151 152
	pr_debug("setting frequency for cpu %d to %d kHz, " \
		 "1/%d of max frequency\n",
153 154
		 policy->cpu,
		 cbe_freqs[cbe_pmode_new].frequency,
155
		 cbe_freqs[cbe_pmode_new].driver_data);
156 157

	rc = set_pmode(policy->cpu, cbe_pmode_new);
158

159
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
160 161 162 163 164 165
	mutex_unlock(&cbe_switch_mutex);

	return rc;
}

static struct cpufreq_driver cbe_cpufreq_driver = {
166
	.verify		= cpufreq_generic_frequency_table_verify,
167 168
	.target		= cbe_cpufreq_target,
	.init		= cbe_cpufreq_cpu_init,
169
	.exit		= cpufreq_generic_exit,
170 171 172 173 174 175 176 177 178 179
	.name		= "cbe-cpufreq",
	.flags		= CPUFREQ_CONST_LOOPS,
};

/*
 * module init and destoy
 */

static int __init cbe_cpufreq_init(void)
{
180 181
	if (!machine_is(cell))
		return -ENODEV;
182

183 184 185 186 187 188 189 190 191 192 193 194 195
	return cpufreq_register_driver(&cbe_cpufreq_driver);
}

static void __exit cbe_cpufreq_exit(void)
{
	cpufreq_unregister_driver(&cbe_cpufreq_driver);
}

module_init(cbe_cpufreq_init);
module_exit(cbe_cpufreq_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");