powernow-k6.c 8.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  This file was based upon code in Powertweak Linux (http://powertweak.sf.net)
3 4
 *  (C) 2000-2003  Dave Jones, Arjan van de Ven, Janne Pänkälä,
 *                 Dominik Brodowski.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
 */

#include <linux/kernel.h>
12
#include <linux/module.h>
L
Linus Torvalds 已提交
13 14 15
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/ioport.h>
16 17
#include <linux/timex.h>
#include <linux/io.h>
L
Linus Torvalds 已提交
18

19
#include <asm/cpu_device_id.h>
20 21
#include <asm/msr.h>

22 23
#define POWERNOW_IOPORT 0xfff0          /* it doesn't matter where, as long
					   as it is unused */
L
Linus Torvalds 已提交
24

25
#define PFX "powernow-k6: "
L
Linus Torvalds 已提交
26 27 28
static unsigned int                     busfreq;   /* FSB, in 10 kHz */
static unsigned int                     max_multiplier;

29 30 31 32 33 34 35 36
static unsigned int			param_busfreq = 0;
static unsigned int			param_max_multiplier = 0;

module_param_named(max_multiplier, param_max_multiplier, uint, S_IRUGO);
MODULE_PARM_DESC(max_multiplier, "Maximum multiplier (allowed values: 20 30 35 40 45 50 55 60)");

module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
L
Linus Torvalds 已提交
37 38 39

/* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
static struct cpufreq_frequency_table clock_ratio[] = {
40 41
	{60,  /* 110 -> 6.0x */ 0},
	{55,  /* 011 -> 5.5x */ 0},
L
Linus Torvalds 已提交
42
	{50,  /* 001 -> 5.0x */ 0},
43
	{45,  /* 000 -> 4.5x */ 0},
L
Linus Torvalds 已提交
44 45
	{40,  /* 010 -> 4.0x */ 0},
	{35,  /* 111 -> 3.5x */ 0},
46 47
	{30,  /* 101 -> 3.0x */ 0},
	{20,  /* 100 -> 2.0x */ 0},
L
Linus Torvalds 已提交
48 49 50
	{0, CPUFREQ_TABLE_END}
};

51 52 53
static const u8 index_to_register[8] = { 6, 3, 1, 0, 2, 7, 5, 4 };
static const u8 register_to_index[8] = { 3, 2, 4, 1, 7, 6, 0, 5 };

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static const struct {
	unsigned freq;
	unsigned mult;
} usual_frequency_table[] = {
	{ 400000, 40 },	// 100   * 4
	{ 450000, 45 }, // 100   * 4.5
	{ 475000, 50 }, //  95   * 5
	{ 500000, 50 }, // 100   * 5
	{ 506250, 45 }, // 112.5 * 4.5
	{ 533500, 55 }, //  97   * 5.5
	{ 550000, 55 }, // 100   * 5.5
	{ 562500, 50 }, // 112.5 * 5
	{ 570000, 60 }, //  95   * 6
	{ 600000, 60 }, // 100   * 6
	{ 618750, 55 }, // 112.5 * 5.5
	{ 660000, 55 }, // 120   * 5.5
	{ 675000, 60 }, // 112.5 * 6
	{ 720000, 60 }, // 120   * 6
};

#define FREQ_RANGE		3000
L
Linus Torvalds 已提交
75 76 77 78

/**
 * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
 *
79
 * Returns the current setting of the frequency multiplier. Core clock
L
Linus Torvalds 已提交
80 81 82 83
 * speed is frequency of the Front-Side Bus multiplied with this value.
 */
static int powernow_k6_get_cpu_multiplier(void)
{
84
	unsigned long invalue = 0;
85
	u32 msrval;
86

87 88
	local_irq_disable();

L
Linus Torvalds 已提交
89 90
	msrval = POWERNOW_IOPORT + 0x1;
	wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
91
	invalue = inl(POWERNOW_IOPORT + 0x8);
L
Linus Torvalds 已提交
92 93 94
	msrval = POWERNOW_IOPORT + 0x0;
	wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */

95 96
	local_irq_enable();

97
	return clock_ratio[register_to_index[(invalue >> 5)&7]].driver_data;
L
Linus Torvalds 已提交
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
{
	unsigned long outvalue, invalue;
	unsigned long msrval;
	unsigned long cr0;

	/* we now need to transform best_i to the BVC format, see AMD#23446 */

	/*
	 * The processor doesn't respond to inquiry cycles while changing the
	 * frequency, so we must disable cache.
	 */
	local_irq_disable();
	cr0 = read_cr0();
	write_cr0(cr0 | X86_CR0_CD);
	wbinvd();

117
	outvalue = (1<<12) | (1<<10) | (1<<9) | (index_to_register[best_i]<<5);
118 119 120 121 122 123 124 125 126 127 128 129 130

	msrval = POWERNOW_IOPORT + 0x1;
	wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
	invalue = inl(POWERNOW_IOPORT + 0x8);
	invalue = invalue & 0x1f;
	outvalue = outvalue | invalue;
	outl(outvalue, (POWERNOW_IOPORT + 0x8));
	msrval = POWERNOW_IOPORT + 0x0;
	wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */

	write_cr0(cr0);
	local_irq_enable();
}
L
Linus Torvalds 已提交
131 132

/**
133
 * powernow_k6_target - set the PowerNow! multiplier
L
Linus Torvalds 已提交
134 135 136 137
 * @best_i: clock_ratio[best_i] is the target multiplier
 *
 *   Tries to change the PowerNow! multiplier
 */
138
static int powernow_k6_target(struct cpufreq_policy *policy,
139
		unsigned int best_i)
L
Linus Torvalds 已提交
140
{
141
	struct cpufreq_freqs freqs;
L
Linus Torvalds 已提交
142

143
	if (clock_ratio[best_i].driver_data > max_multiplier) {
144
		printk(KERN_ERR PFX "invalid target frequency\n");
145
		return -EINVAL;
L
Linus Torvalds 已提交
146 147 148
	}

	freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
149
	freqs.new = busfreq * clock_ratio[best_i].driver_data;
150

151
	cpufreq_freq_transition_begin(policy, &freqs);
L
Linus Torvalds 已提交
152

153
	powernow_k6_set_cpu_multiplier(best_i);
L
Linus Torvalds 已提交
154

155
	cpufreq_freq_transition_end(policy, &freqs, 0);
L
Linus Torvalds 已提交
156 157 158 159 160 161

	return 0;
}

static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
{
162
	unsigned int i, f;
163
	unsigned khz;
L
Linus Torvalds 已提交
164 165 166 167

	if (policy->cpu != 0)
		return -ENODEV;

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	max_multiplier = 0;
	khz = cpu_khz;
	for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
		if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
		    khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
			khz = usual_frequency_table[i].freq;
			max_multiplier = usual_frequency_table[i].mult;
			break;
		}
	}
	if (param_max_multiplier) {
		for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
			if (clock_ratio[i].driver_data == param_max_multiplier) {
				max_multiplier = param_max_multiplier;
				goto have_max_multiplier;
			}
		}
		printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
		return -EINVAL;
	}

	if (!max_multiplier) {
		printk(KERN_WARNING "powernow-k6: unknown frequency %u, cannot determine current multiplier\n", khz);
		printk(KERN_WARNING "powernow-k6: use module parameters max_multiplier and bus_frequency\n");
		return -EOPNOTSUPP;
	}

have_max_multiplier:
	param_max_multiplier = max_multiplier;

	if (param_busfreq) {
		if (param_busfreq >= 50000 && param_busfreq <= 150000) {
			busfreq = param_busfreq / 10;
			goto have_busfreq;
		}
		printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
		return -EINVAL;
	}

	busfreq = khz / max_multiplier;
have_busfreq:
	param_busfreq = busfreq * 10;
L
Linus Torvalds 已提交
210 211

	/* table init */
212
	for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
213
		f = clock_ratio[i].driver_data;
214
		if (f > max_multiplier)
L
Linus Torvalds 已提交
215 216
			clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
		else
217
			clock_ratio[i].frequency = busfreq * f;
L
Linus Torvalds 已提交
218 219 220
	}

	/* cpuinfo and default policy values */
221
	policy->cpuinfo.transition_latency = 500000;
L
Linus Torvalds 已提交
222

223
	return cpufreq_table_validate_and_show(policy, clock_ratio);
L
Linus Torvalds 已提交
224 225 226 227 228 229
}


static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
{
	unsigned int i;
230 231
	for (i = 0; i < 8; i++) {
		if (i == max_multiplier)
232
			powernow_k6_target(policy, i);
L
Linus Torvalds 已提交
233
	}
234
	return 0;
L
Linus Torvalds 已提交
235 236 237 238
}

static unsigned int powernow_k6_get(unsigned int cpu)
{
239 240 241
	unsigned int ret;
	ret = (busfreq * powernow_k6_get_cpu_multiplier());
	return ret;
L
Linus Torvalds 已提交
242 243
}

244
static struct cpufreq_driver powernow_k6_driver = {
245
	.verify		= cpufreq_generic_frequency_table_verify,
246
	.target_index	= powernow_k6_target,
L
Linus Torvalds 已提交
247 248 249 250
	.init		= powernow_k6_cpu_init,
	.exit		= powernow_k6_cpu_exit,
	.get		= powernow_k6_get,
	.name		= "powernow-k6",
251
	.attr		= cpufreq_generic_attr,
L
Linus Torvalds 已提交
252 253
};

254 255 256 257 258
static const struct x86_cpu_id powernow_k6_ids[] = {
	{ X86_VENDOR_AMD, 5, 12 },
	{ X86_VENDOR_AMD, 5, 13 },
	{}
};
259
MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268

/**
 * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
 *
 *   Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
 * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
 * on success.
 */
static int __init powernow_k6_init(void)
269
{
270
	if (!x86_match_cpu(powernow_k6_ids))
L
Linus Torvalds 已提交
271 272 273
		return -ENODEV;

	if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
274
		printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
L
Linus Torvalds 已提交
275 276 277 278
		return -EIO;
	}

	if (cpufreq_register_driver(&powernow_k6_driver)) {
279
		release_region(POWERNOW_IOPORT, 16);
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		return -EINVAL;
	}

	return 0;
}


/**
 * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
 *
 *   Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
 */
static void __exit powernow_k6_exit(void)
{
	cpufreq_unregister_driver(&powernow_k6_driver);
295
	release_region(POWERNOW_IOPORT, 16);
L
Linus Torvalds 已提交
296 297 298
}


299 300
MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
		"Dominik Brodowski <linux@brodo.de>");
301 302
MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
303 304 305

module_init(powernow_k6_init);
module_exit(powernow_k6_exit);