blob: 123fb006810d5da028c926b3a68da2b324095500 [file] [log] [blame]
Thomas Gleixner4f190482019-05-27 08:55:14 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Dave Jonesd5e80b42014-12-19 11:20:43 -05003 * (C) 2001-2004 Dave Jones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * (C) 2002 Padraig Brady. <padraig@antefacto.com>
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based upon datasheets & sample CPUs kindly provided by VIA.
7 *
8 * VIA have currently 3 different versions of Longhaul.
9 * Version 1 (Longhaul) uses the BCR2 MSR at 0x1147.
10 * It is present only in Samuel 1 (C5A), Samuel 2 (C5B) stepping 0.
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +010011 * Version 2 of longhaul is backward compatible with v1, but adds
12 * LONGHAUL MSR for purpose of both frequency and voltage scaling.
13 * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C).
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Version 3 of longhaul got renamed to Powersaver and redesigned
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +010015 * to use only the POWERSAVER MSR at 0x110a.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
17 * It's pretty much the same feature wise to longhaul v2, though
18 * there is provision for scaling FSB too, but this doesn't work
19 * too well in practice so we don't even try to use this.
20 *
21 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
22 */
23
Joe Perches1c5864e2016-04-05 13:28:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/cpufreq.h>
Rafa³ Bilski179da8e2006-08-08 19:12:20 +020031#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/string.h>
Rafał Bilski73e107d2007-05-28 21:56:19 +020034#include <linux/delay.h>
Dave Jonesac617bd2009-01-17 23:29:53 -050035#include <linux/timex.h>
36#include <linux/io.h>
37#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/msr.h>
Andi Kleenfa8031a2012-01-26 00:09:12 +010040#include <asm/cpu_device_id.h>
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020041#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "longhaul.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define TYPE_LONGHAUL_V1 1
46#define TYPE_LONGHAUL_V2 2
47#define TYPE_POWERSAVER 3
48
49#define CPU_SAMUEL 1
50#define CPU_SAMUEL2 2
51#define CPU_EZRA 3
52#define CPU_EZRA_T 4
53#define CPU_NEHEMIAH 5
Rafa³ Bilski980342a2007-01-31 23:42:47 +010054#define CPU_NEHEMIAH_C 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafa³ Bilski264166e2006-12-24 14:04:23 +010056/* Flags */
57#define USE_ACPI_C3 (1 << 1)
58#define USE_NORTHBRIDGE (1 << 2)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int cpu_model;
Dave Jonesac617bd2009-01-17 23:29:53 -050061static unsigned int numscales = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static unsigned int fsb;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020063
Dave Jonesbd5ab262007-02-22 19:11:16 -050064static const struct mV_pos *vrm_mV_table;
65static const unsigned char *mV_vrm_table;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020066
67static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int minmult, maxmult;
69static int can_scale_voltage;
Dave Jonesac617bd2009-01-17 23:29:53 -050070static struct acpi_processor *pr;
71static struct acpi_processor_cx *cx;
Rafał Bilski275bc6b2007-06-05 22:08:50 +020072static u32 acpi_regs_addr;
Rafa³ Bilski264166e2006-12-24 14:04:23 +010073static u8 longhaul_flags;
Rafał Bilski73e107d2007-05-28 21:56:19 +020074static unsigned int longhaul_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020077static int scale_voltage;
Rafał Bilski905497c2007-07-08 21:51:26 +020078static int disable_acpi_c3;
Rafal Bilski52a26382007-10-07 00:24:32 -070079static int revid_errata;
Rafał Bilskib5811bc2012-12-15 00:45:02 +010080static int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Clock ratios multiplied by 10 */
Dave Jonesac617bd2009-01-17 23:29:53 -050083static int mults[32];
84static int eblcr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int longhaul_version;
86static struct cpufreq_frequency_table *longhaul_table;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static char speedbuffer[8];
89
90static char *print_speed(int speed)
91{
Dave Jonese2aa8732006-05-30 17:37:15 -040092 if (speed < 1000) {
Dave Jonesac617bd2009-01-17 23:29:53 -050093 snprintf(speedbuffer, sizeof(speedbuffer), "%dMHz", speed);
Dave Jonese2aa8732006-05-30 17:37:15 -040094 return speedbuffer;
95 }
96
97 if (speed%1000 == 0)
98 snprintf(speedbuffer, sizeof(speedbuffer),
99 "%dGHz", speed/1000);
100 else
101 snprintf(speedbuffer, sizeof(speedbuffer),
102 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return speedbuffer;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107
108static unsigned int calc_speed(int mult)
109{
110 int khz;
111 khz = (mult/10)*fsb;
112 if (mult%10)
113 khz += fsb/2;
114 khz *= 1000;
115 return khz;
116}
117
118
119static int longhaul_get_cpu_mult(void)
120{
Dave Jonesac617bd2009-01-17 23:29:53 -0500121 unsigned long invalue = 0, lo, hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Dave Jonesac617bd2009-01-17 23:29:53 -0500123 rdmsr(MSR_IA32_EBL_CR_POWERON, lo, hi);
124 invalue = (lo & (1<<22|1<<23|1<<24|1<<25))>>22;
125 if (longhaul_version == TYPE_LONGHAUL_V2 ||
126 longhaul_version == TYPE_POWERSAVER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (lo & (1<<27))
Dave Jonesac617bd2009-01-17 23:29:53 -0500128 invalue += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500130 return eblcr[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200133/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dave Jonesac617bd2009-01-17 23:29:53 -0500135static void do_longhaul1(unsigned int mults_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200137 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200139 rdmsrl(MSR_VIA_BCR2, bcr2.val);
140 /* Enable software clock multiplier */
141 bcr2.bits.ESOFTBF = 1;
Dave Jonesac617bd2009-01-17 23:29:53 -0500142 bcr2.bits.CLOCKMUL = mults_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200144 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700145 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200146 /* Change frequency on next halt or sleep */
147 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200148 /* Invoke transition */
149 ACPI_FLUSH_CPU_CACHE();
150 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200152 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700153 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200154 rdmsrl(MSR_VIA_BCR2, bcr2.val);
155 bcr2.bits.ESOFTBF = 0;
156 wrmsrl(MSR_VIA_BCR2, bcr2.val);
157}
Dave Jones3be6a482005-05-31 19:03:51 -0700158
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200159/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700160
Dave Jonesac617bd2009-01-17 23:29:53 -0500161static void do_powersaver(int cx_address, unsigned int mults_index,
Rafał Bilski73e107d2007-05-28 21:56:19 +0200162 unsigned int dir)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200163{
164 union msr_longhaul longhaul;
165 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700166
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200167 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100168 /* Setup new frequency */
Rafal Bilski52a26382007-10-07 00:24:32 -0700169 if (!revid_errata)
170 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
171 else
172 longhaul.bits.RevisionKey = 0;
Dave Jonesac617bd2009-01-17 23:29:53 -0500173 longhaul.bits.SoftBusRatio = mults_index & 0xf;
174 longhaul.bits.SoftBusRatio4 = (mults_index & 0x10) >> 4;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100175 /* Setup new voltage */
176 if (can_scale_voltage)
Dave Jonesac617bd2009-01-17 23:29:53 -0500177 longhaul.bits.SoftVID = (mults_index >> 8) & 0x1f;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200178 /* Sync to timer tick */
179 safe_halt();
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100180 /* Raise voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200181 if (can_scale_voltage && dir) {
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100182 longhaul.bits.EnableSoftVID = 1;
183 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
184 /* Change voltage */
185 if (!cx_address) {
186 ACPI_FLUSH_CPU_CACHE();
187 halt();
188 } else {
189 ACPI_FLUSH_CPU_CACHE();
190 /* Invoke C3 */
191 inb(cx_address);
192 /* Dummy op - must do something useless after P_LVL3
193 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500194 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100195 }
196 longhaul.bits.EnableSoftVID = 0;
197 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100198 }
199
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200200 /* Change frequency on next halt or sleep */
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100201 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200202 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100203 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200204 ACPI_FLUSH_CPU_CACHE();
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200205 halt();
206 } else {
207 ACPI_FLUSH_CPU_CACHE();
208 /* Invoke C3 */
209 inb(cx_address);
210 /* Dummy op - must do something useless after P_LVL3 read */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300211 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200212 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200213 /* Disable bus ratio bit */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200214 longhaul.bits.EnableSoftBusRatio = 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200215 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100216
217 /* Reduce voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200218 if (can_scale_voltage && !dir) {
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100219 longhaul.bits.EnableSoftVID = 1;
220 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
221 /* Change voltage */
222 if (!cx_address) {
223 ACPI_FLUSH_CPU_CACHE();
224 halt();
225 } else {
226 ACPI_FLUSH_CPU_CACHE();
227 /* Invoke C3 */
228 inb(cx_address);
229 /* Dummy op - must do something useless after P_LVL3
230 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500231 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100232 }
233 longhaul.bits.EnableSoftVID = 0;
234 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/**
239 * longhaul_set_cpu_frequency()
Dave Jonesac617bd2009-01-17 23:29:53 -0500240 * @mults_index : bitpattern of the new multiplier.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
242 * Sets a new clock ratio.
243 */
244
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530245static int longhaul_setstate(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530246 unsigned int table_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Dave Jonesac617bd2009-01-17 23:29:53 -0500248 unsigned int mults_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int speed, mult;
250 struct cpufreq_freqs freqs;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200251 unsigned long flags;
252 unsigned int pic1_mask, pic2_mask;
Rafał Bilski689eba72007-06-07 22:31:24 +0200253 u16 bm_status = 0;
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200254 u32 bm_timeout = 1000;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200255 unsigned int dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Viresh Kumar50701582013-03-30 16:25:15 +0530257 mults_index = longhaul_table[table_index].driver_data;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200258 /* Safety precautions */
Dave Jonesac617bd2009-01-17 23:29:53 -0500259 mult = mults[mults_index & 0x1f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (mult == -1)
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530261 return -EINVAL;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 speed = calc_speed(mult);
264 if ((speed > highest_speed) || (speed < lowest_speed))
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530265 return -EINVAL;
266
Rafał Bilski73e107d2007-05-28 21:56:19 +0200267 /* Voltage transition before frequency transition? */
268 if (can_scale_voltage && longhaul_index < table_index)
269 dir = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 freqs.old = calc_speed(longhaul_get_cpu_mult());
272 freqs.new = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200274 pr_debug("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 fsb, mult/10, mult%10, print_speed(speed/1000));
Rafal Bilski52a26382007-10-07 00:24:32 -0700276retry_loop:
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200277 preempt_disable();
278 local_irq_save(flags);
279
280 pic2_mask = inb(0xA1);
281 pic1_mask = inb(0x21); /* works on C3. save mask. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500282 outb(0xFF, 0xA1); /* Overkill */
283 outb(0xFE, 0x21); /* TMR0 only */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200284
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200285 /* Wait while PCI bus is busy. */
Rafał Bilski689eba72007-06-07 22:31:24 +0200286 if (acpi_regs_addr && (longhaul_flags & USE_NORTHBRIDGE
287 || ((pr != NULL) && pr->flags.bm_control))) {
288 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200289 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200290 while (bm_status && bm_timeout) {
Rafał Bilski689eba72007-06-07 22:31:24 +0200291 outw(1 << 4, acpi_regs_addr);
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200292 bm_timeout--;
Rafał Bilski689eba72007-06-07 22:31:24 +0200293 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200294 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200295 }
296 }
297
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100298 if (longhaul_flags & USE_NORTHBRIDGE) {
299 /* Disable AGP and PCI arbiters */
300 outb(3, 0x22);
301 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200302 /* Disable bus master arbitration */
Lin Mingfb318cb2009-03-18 09:09:01 +0800303 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (longhaul_version) {
306
307 /*
308 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
309 * Software controlled multipliers only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
311 case TYPE_LONGHAUL_V1:
Dave Jonesac617bd2009-01-17 23:29:53 -0500312 do_longhaul1(mults_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314
315 /*
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100316 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5B] and Ezra [C5C]
317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Nehemiah can do FSB scaling too, but this has never been proven
320 * to work in practice.
321 */
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100322 case TYPE_LONGHAUL_V2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100324 if (longhaul_flags & USE_ACPI_C3) {
325 /* Don't allow wakeup */
Lin Mingfb318cb2009-03-18 09:09:01 +0800326 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Dave Jonesac617bd2009-01-17 23:29:53 -0500327 do_powersaver(cx->address, mults_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100328 } else {
Dave Jonesac617bd2009-01-17 23:29:53 -0500329 do_powersaver(0, mults_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332 }
333
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100334 if (longhaul_flags & USE_NORTHBRIDGE) {
335 /* Enable arbiters */
336 outb(0, 0x22);
337 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200338 /* Enable bus master arbitration */
Lin Mingfb318cb2009-03-18 09:09:01 +0800339 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200340 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500341 outb(pic2_mask, 0xA1); /* restore mask */
342 outb(pic1_mask, 0x21);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200343
344 local_irq_restore(flags);
345 preempt_enable();
346
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100347 freqs.new = calc_speed(longhaul_get_cpu_mult());
Rafal Bilski52a26382007-10-07 00:24:32 -0700348 /* Check if requested frequency is set. */
349 if (unlikely(freqs.new != speed)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700350 pr_info("Failed to set requested frequency!\n");
Rafal Bilski52a26382007-10-07 00:24:32 -0700351 /* Revision ID = 1 but processor is expecting revision key
352 * equal to 0. Jumpers at the bottom of processor will change
353 * multiplier and FSB, but will not change bits in Longhaul
354 * MSR nor enable voltage scaling. */
355 if (!revid_errata) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700356 pr_info("Enabling \"Ignore Revision ID\" option\n");
Rafal Bilski52a26382007-10-07 00:24:32 -0700357 revid_errata = 1;
358 msleep(200);
359 goto retry_loop;
360 }
361 /* Why ACPI C3 sometimes doesn't work is a mystery for me.
362 * But it does happen. Processor is entering ACPI C3 state,
363 * but it doesn't change frequency. I tried poking various
364 * bits in northbridge registers, but without success. */
365 if (longhaul_flags & USE_ACPI_C3) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700366 pr_info("Disabling ACPI C3 support\n");
Rafal Bilski52a26382007-10-07 00:24:32 -0700367 longhaul_flags &= ~USE_ACPI_C3;
368 if (revid_errata) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700369 pr_info("Disabling \"Ignore Revision ID\" option\n");
Rafal Bilski52a26382007-10-07 00:24:32 -0700370 revid_errata = 0;
371 }
372 msleep(200);
373 goto retry_loop;
374 }
375 /* This shouldn't happen. Longhaul ver. 2 was reported not
376 * working on processors without voltage scaling, but with
377 * RevID = 1. RevID errata will make things right. Just
378 * to be 100% sure. */
379 if (longhaul_version == TYPE_LONGHAUL_V2) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700380 pr_info("Switching to Longhaul ver. 1\n");
Rafal Bilski52a26382007-10-07 00:24:32 -0700381 longhaul_version = TYPE_LONGHAUL_V1;
382 msleep(200);
383 goto retry_loop;
384 }
385 }
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200386
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530387 if (!bm_timeout) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700388 pr_info("Warning: Timeout while waiting for idle PCI bus\n");
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530389 return -EBUSY;
390 }
391
392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
396 * Centaur decided to make life a little more tricky.
397 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
398 * Samuel2 and above have to try and guess what the FSB is.
399 * We do this by assuming we booted at maximum multiplier, and interpolate
400 * between that value multiplied by possible FSBs and cpu_mhz which
401 * was calculated at boot time. Really ugly, but no other way to do this.
402 */
403
404#define ROUNDING 0xf
405
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100406static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100408 int speed = cpu_khz / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int i;
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100410 int speeds[] = { 666, 1000, 1333, 2000 };
411 int f_max, f_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100413 for (i = 0; i < 4; i++) {
414 f_max = ((speeds[i] * mult) + 50) / 100;
415 f_max += (ROUNDING / 2);
416 f_min = f_max - ROUNDING;
417 if ((speed <= f_max) && (speed >= f_min))
418 return speeds[i] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 return 0;
421}
422
423
Paul Gortmaker27609842013-06-19 13:54:04 -0400424static int longhaul_get_ranges(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Rafał Bilski73e107d2007-05-28 21:56:19 +0200426 unsigned int i, j, k = 0;
427 unsigned int ratio;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100428 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100430 /* Get current frequency */
431 mult = longhaul_get_cpu_mult();
432 if (mult == -1) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700433 pr_info("Invalid (reserved) multiplier!\n");
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100434 return -EINVAL;
435 }
436 fsb = guess_fsb(mult);
437 if (fsb == 0) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700438 pr_info("Invalid (reserved) FSB!\n");
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100439 return -EINVAL;
440 }
441 /* Get max multiplier - as we always did.
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300442 * Longhaul MSR is useful only when voltage scaling is enabled.
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100443 * C3 is booting at max anyway. */
444 maxmult = mult;
445 /* Get min multiplier */
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100446 switch (cpu_model) {
447 case CPU_NEHEMIAH:
448 minmult = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100450 case CPU_NEHEMIAH_C:
451 minmult = 40;
452 break;
453 default:
454 minmult = 30;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200458 pr_debug("MinMult:%d.%dx MaxMult:%d.%dx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 minmult/10, minmult%10, maxmult/10, maxmult%10);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 highest_speed = calc_speed(maxmult);
462 lowest_speed = calc_speed(minmult);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200463 pr_debug("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300464 print_speed(lowest_speed/1000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 print_speed(highest_speed/1000));
466
467 if (lowest_speed == highest_speed) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700468 pr_info("highestspeed == lowest, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470 }
471 if (lowest_speed > highest_speed) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700472 pr_info("nonsense! lowest (%d > %d) !\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 lowest_speed, highest_speed);
474 return -EINVAL;
475 }
476
Kees Cook6396bb22018-06-12 14:03:40 -0700477 longhaul_table = kcalloc(numscales + 1, sizeof(*longhaul_table),
478 GFP_KERNEL);
Dave Jonesac617bd2009-01-17 23:29:53 -0500479 if (!longhaul_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -ENOMEM;
481
Rafał Bilski73e107d2007-05-28 21:56:19 +0200482 for (j = 0; j < numscales; j++) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500483 ratio = mults[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (ratio == -1)
485 continue;
486 if (ratio > maxmult || ratio < minmult)
487 continue;
488 longhaul_table[k].frequency = calc_speed(ratio);
Viresh Kumar50701582013-03-30 16:25:15 +0530489 longhaul_table[k].driver_data = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 k++;
491 }
Rafał Bilski73e107d2007-05-28 21:56:19 +0200492 if (k <= 1) {
493 kfree(longhaul_table);
494 return -ENODEV;
495 }
496 /* Sort */
497 for (j = 0; j < k - 1; j++) {
498 unsigned int min_f, min_i;
499 min_f = longhaul_table[j].frequency;
500 min_i = j;
501 for (i = j + 1; i < k; i++) {
502 if (longhaul_table[i].frequency < min_f) {
503 min_f = longhaul_table[i].frequency;
504 min_i = i;
505 }
506 }
507 if (min_i != j) {
Dave Jones91420222009-02-04 15:28:54 -0500508 swap(longhaul_table[j].frequency,
509 longhaul_table[min_i].frequency);
Viresh Kumar50701582013-03-30 16:25:15 +0530510 swap(longhaul_table[j].driver_data,
511 longhaul_table[min_i].driver_data);
Rafał Bilski73e107d2007-05-28 21:56:19 +0200512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Rafał Bilski73e107d2007-05-28 21:56:19 +0200515 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
516
517 /* Find index we are running on */
518 for (j = 0; j < k; j++) {
Viresh Kumar50701582013-03-30 16:25:15 +0530519 if (mults[longhaul_table[j].driver_data & 0x1f] == mult) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200520 longhaul_index = j;
521 break;
522 }
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
525}
526
527
Paul Gortmaker27609842013-06-19 13:54:04 -0400528static void longhaul_setup_voltagescaling(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300530 struct cpufreq_frequency_table *freq_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 union msr_longhaul longhaul;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200532 struct mV_pos minvid, maxvid, vid;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200533 unsigned int j, speed, pos, kHz_step, numvscales;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100534 int min_vid_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200536 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
537 if (!(longhaul.bits.RevisionID & 1)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700538 pr_info("Voltage scaling not supported by CPU\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200542 if (!longhaul.bits.VRMRev) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700543 pr_info("VRM 8.5\n");
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200544 vrm_mV_table = &vrm85_mV[0];
545 mV_vrm_table = &mV_vrm85[0];
546 } else {
Joe Perches1c5864e2016-04-05 13:28:25 -0700547 pr_info("Mobile VRM\n");
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100548 if (cpu_model < CPU_NEHEMIAH)
549 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200550 vrm_mV_table = &mobilevrm_mV[0];
551 mV_vrm_table = &mV_mobilevrm[0];
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200554 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
555 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200556
557 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700558 pr_info("Bogus values Min:%d.%03d Max:%d.%03d - Voltage scaling disabled\n",
Joe Perchesb49c22a2016-04-05 13:28:24 -0700559 minvid.mV/1000, minvid.mV%1000,
560 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return;
562 }
563
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200564 if (minvid.mV == maxvid.mV) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700565 pr_info("Claims to support voltage scaling but min & max are both %d.%03d - Voltage scaling disabled\n",
Joe Perchesb49c22a2016-04-05 13:28:24 -0700566 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return;
568 }
569
Dave Jonesac617bd2009-01-17 23:29:53 -0500570 /* How many voltage steps*/
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100571 numvscales = maxvid.pos - minvid.pos + 1;
Joe Perches1c5864e2016-04-05 13:28:25 -0700572 pr_info("Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200573 maxvid.mV/1000, maxvid.mV%1000,
574 minvid.mV/1000, minvid.mV%1000,
575 numvscales);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100576
577 /* Calculate max frequency at min voltage */
578 j = longhaul.bits.MinMHzBR;
579 if (longhaul.bits.MinMHzBR4)
580 j += 16;
Dave Jonesac617bd2009-01-17 23:29:53 -0500581 min_vid_speed = eblcr[j];
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100582 if (min_vid_speed == -1)
583 return;
584 switch (longhaul.bits.MinMHzFSB) {
585 case 0:
586 min_vid_speed *= 13333;
587 break;
588 case 1:
589 min_vid_speed *= 10000;
590 break;
591 case 3:
592 min_vid_speed *= 6666;
593 break;
594 default:
595 return;
596 break;
597 }
598 if (min_vid_speed >= highest_speed)
599 return;
600 /* Calculate kHz for one voltage step */
601 kHz_step = (highest_speed - min_vid_speed) / numvscales;
602
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100603 cpufreq_for_each_entry_idx(freq_pos, longhaul_table, j) {
Stratos Karafotis041526f2014-04-25 23:15:38 +0300604 speed = freq_pos->frequency;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100605 if (speed > min_vid_speed)
606 pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
607 else
608 pos = minvid.pos;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300609 freq_pos->driver_data |= mV_vrm_table[pos] << 8;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200610 vid = vrm_mV_table[mV_vrm_table[pos]];
Joe Perches1c5864e2016-04-05 13:28:25 -0700611 pr_info("f: %d kHz, index: %d, vid: %d mV\n",
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100612 speed, j, vid.mV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 can_scale_voltage = 1;
Joe Perches1c5864e2016-04-05 13:28:25 -0700616 pr_info("Voltage scaling enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int longhaul_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530621 unsigned int table_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Rafał Bilski73e107d2007-05-28 21:56:19 +0200623 unsigned int i;
624 unsigned int dir = 0;
625 u8 vid, current_vid;
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530626 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Rafał Bilski73e107d2007-05-28 21:56:19 +0200628 if (!can_scale_voltage)
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530629 retval = longhaul_setstate(policy, table_index);
Rafał Bilski73e107d2007-05-28 21:56:19 +0200630 else {
631 /* On test system voltage transitions exceeding single
632 * step up or down were turning motherboard off. Both
633 * "ondemand" and "userspace" are unsafe. C7 is doing
634 * this in hardware, C3 is old and we need to do this
635 * in software. */
636 i = longhaul_index;
Viresh Kumar50701582013-03-30 16:25:15 +0530637 current_vid = (longhaul_table[longhaul_index].driver_data >> 8);
Dave Jonesac617bd2009-01-17 23:29:53 -0500638 current_vid &= 0x1f;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200639 if (table_index > longhaul_index)
640 dir = 1;
641 while (i != table_index) {
Viresh Kumar50701582013-03-30 16:25:15 +0530642 vid = (longhaul_table[i].driver_data >> 8) & 0x1f;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200643 if (vid != current_vid) {
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530644 retval = longhaul_setstate(policy, i);
Rafał Bilski73e107d2007-05-28 21:56:19 +0200645 current_vid = vid;
646 msleep(200);
647 }
648 if (dir)
649 i++;
650 else
651 i--;
652 }
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530653 retval = longhaul_setstate(policy, table_index);
Rafał Bilski73e107d2007-05-28 21:56:19 +0200654 }
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530655
Rafał Bilski73e107d2007-05-28 21:56:19 +0200656 longhaul_index = table_index;
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530657 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660
661static unsigned int longhaul_get(unsigned int cpu)
662{
663 if (cpu)
664 return 0;
665 return calc_speed(longhaul_get_cpu_mult());
666}
667
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200668static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
669 u32 nesting_level,
670 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200671{
672 struct acpi_device *d;
673
Dave Jonesac617bd2009-01-17 23:29:53 -0500674 if (acpi_bus_get_device(obj_handle, &d))
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200675 return 0;
Dave Jonesac617bd2009-01-17 23:29:53 -0500676
Jan Engelhardtade1af72008-01-30 13:33:23 +0100677 *return_value = acpi_driver_data(d);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200678 return 1;
679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200681/* VIA don't support PM2 reg, but have something similar */
682static int enable_arbiter_disable(void)
683{
684 struct pci_dev *dev;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200685 int status = 1;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200686 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200687 u8 pci_cmd;
688
689 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200690 reg = 0x78;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100691 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0,
692 NULL);
Linus Torvalds4d5709a2007-10-12 15:42:01 -0700693 /* Find PM133/VT8605 host bridge */
694 if (dev == NULL)
695 dev = pci_get_device(PCI_VENDOR_ID_VIA,
696 PCI_DEVICE_ID_VIA_8605_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200697 /* Find CLE266 host bridge */
698 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200699 reg = 0x76;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100700 dev = pci_get_device(PCI_VENDOR_ID_VIA,
701 PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100702 /* Find CN400 V-Link host bridge */
703 if (dev == NULL)
Rafał Bilskifb48e152007-03-02 20:12:27 +0100704 dev = pci_get_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200705 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200706 if (dev != NULL) {
707 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200708 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100709 if (!(pci_cmd & 1<<7)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200710 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200711 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100712 pci_read_config_byte(dev, reg, &pci_cmd);
713 if (!(pci_cmd & 1<<7)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700714 pr_err("Can't enable access to port 0x22\n");
Rafał Bilskifb48e152007-03-02 20:12:27 +0100715 status = 0;
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100716 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200717 }
Rafał Bilskifb48e152007-03-02 20:12:27 +0100718 pci_dev_put(dev);
719 return status;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200720 }
721 return 0;
722}
723
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200724static int longhaul_setup_southbridge(void)
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100725{
726 struct pci_dev *dev;
727 u8 pci_cmd;
728
729 /* Find VT8235 southbridge */
Rafał Bilskifb48e152007-03-02 20:12:27 +0100730 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
Rafał Bilski920dd0f2007-05-17 22:35:29 +0200731 if (dev == NULL)
Dave Jonesac617bd2009-01-17 23:29:53 -0500732 /* Find VT8237 southbridge */
Rafał Bilski920dd0f2007-05-17 22:35:29 +0200733 dev = pci_get_device(PCI_VENDOR_ID_VIA,
734 PCI_DEVICE_ID_VIA_8237, NULL);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100735 if (dev != NULL) {
736 /* Set transition time to max */
737 pci_read_config_byte(dev, 0xec, &pci_cmd);
738 pci_cmd &= ~(1 << 2);
739 pci_write_config_byte(dev, 0xec, pci_cmd);
740 pci_read_config_byte(dev, 0xe4, &pci_cmd);
741 pci_cmd &= ~(1 << 7);
742 pci_write_config_byte(dev, 0xe4, pci_cmd);
743 pci_read_config_byte(dev, 0xe5, &pci_cmd);
744 pci_cmd |= 1 << 7;
745 pci_write_config_byte(dev, 0xe5, pci_cmd);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200746 /* Get address of ACPI registers block*/
747 pci_read_config_byte(dev, 0x81, &pci_cmd);
748 if (pci_cmd & 1 << 7) {
749 pci_read_config_dword(dev, 0x88, &acpi_regs_addr);
750 acpi_regs_addr &= 0xff00;
Joe Perches1c5864e2016-04-05 13:28:25 -0700751 pr_info("ACPI I/O at 0x%x\n", acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200752 }
753
Rafał Bilskifb48e152007-03-02 20:12:27 +0100754 pci_dev_put(dev);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100755 return 1;
756 }
757 return 0;
758}
759
Paul Gortmaker27609842013-06-19 13:54:04 -0400760static int longhaul_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Mike Travis92cb7612007-10-19 20:35:04 +0200762 struct cpuinfo_x86 *c = &cpu_data(0);
Dave Jonesac617bd2009-01-17 23:29:53 -0500763 char *cpuname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int ret;
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100765 u32 lo, hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200767 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 switch (c->x86_model) {
769 case 6:
770 cpu_model = CPU_SAMUEL;
771 cpuname = "C3 'Samuel' [C5A]";
772 longhaul_version = TYPE_LONGHAUL_V1;
Dave Jonesac617bd2009-01-17 23:29:53 -0500773 memcpy(mults, samuel1_mults, sizeof(samuel1_mults));
774 memcpy(eblcr, samuel1_eblcr, sizeof(samuel1_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776
777 case 7:
Jia Zhangb3991512018-01-01 09:52:10 +0800778 switch (c->x86_stepping) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 case 0:
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100780 longhaul_version = TYPE_LONGHAUL_V1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 cpu_model = CPU_SAMUEL2;
782 cpuname = "C3 'Samuel 2' [C5B]";
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100783 /* Note, this is not a typo, early Samuel2's had
784 * Samuel1 ratios. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500785 memcpy(mults, samuel1_mults, sizeof(samuel1_mults));
786 memcpy(eblcr, samuel2_eblcr, sizeof(samuel2_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
788 case 1 ... 15:
Krzysztof Heltf7f3cad2009-10-24 17:25:38 +0200789 longhaul_version = TYPE_LONGHAUL_V2;
Jia Zhangb3991512018-01-01 09:52:10 +0800790 if (c->x86_stepping < 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 cpu_model = CPU_SAMUEL2;
792 cpuname = "C3 'Samuel 2' [C5B]";
793 } else {
794 cpu_model = CPU_EZRA;
795 cpuname = "C3 'Ezra' [C5C]";
796 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500797 memcpy(mults, ezra_mults, sizeof(ezra_mults));
798 memcpy(eblcr, ezra_eblcr, sizeof(ezra_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800 }
801 break;
802
803 case 8:
804 cpu_model = CPU_EZRA_T;
805 cpuname = "C3 'Ezra-T' [C5M]";
806 longhaul_version = TYPE_POWERSAVER;
Dave Jonesac617bd2009-01-17 23:29:53 -0500807 numscales = 32;
808 memcpy(mults, ezrat_mults, sizeof(ezrat_mults));
809 memcpy(eblcr, ezrat_eblcr, sizeof(ezrat_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811
812 case 9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 longhaul_version = TYPE_POWERSAVER;
Rafa³ Bilski0d44b2b2007-01-31 23:50:49 +0100814 numscales = 32;
Dave Jonesac617bd2009-01-17 23:29:53 -0500815 memcpy(mults, nehemiah_mults, sizeof(nehemiah_mults));
816 memcpy(eblcr, nehemiah_eblcr, sizeof(nehemiah_eblcr));
Jia Zhangb3991512018-01-01 09:52:10 +0800817 switch (c->x86_stepping) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case 0 ... 1:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100819 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100820 cpuname = "C3 'Nehemiah A' [C5XLOE]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 case 2 ... 4:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100823 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100824 cpuname = "C3 'Nehemiah B' [C5XLOH]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
826 case 5 ... 15:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100827 cpu_model = CPU_NEHEMIAH_C;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100828 cpuname = "C3 'Nehemiah C' [C5P]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830 }
831 break;
832
833 default:
834 cpuname = "Unknown";
835 break;
836 }
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100837 /* Check Longhaul ver. 2 */
838 if (longhaul_version == TYPE_LONGHAUL_V2) {
839 rdmsr(MSR_VIA_LONGHAUL, lo, hi);
840 if (lo == 0 && hi == 0)
841 /* Looks like MSR isn't present */
842 longhaul_version = TYPE_LONGHAUL_V1;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Joe Perches1c5864e2016-04-05 13:28:25 -0700845 pr_info("VIA %s CPU detected. ", cpuname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 switch (longhaul_version) {
847 case TYPE_LONGHAUL_V1:
848 case TYPE_LONGHAUL_V2:
Joe Perchesb49c22a2016-04-05 13:28:24 -0700849 pr_cont("Longhaul v%d supported\n", longhaul_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851 case TYPE_POWERSAVER:
Joe Perchesb49c22a2016-04-05 13:28:24 -0700852 pr_cont("Powersaver supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 break;
Yangtao Li28143352019-02-16 10:55:27 -0500854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100856 /* Doesn't hurt */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200857 longhaul_setup_southbridge();
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100858
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200859 /* Find ACPI data for processor */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100860 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800861 ACPI_UINT32_MAX, &longhaul_walk_callback, NULL,
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100862 NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200863
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100864 /* Check ACPI support for C3 state */
Dave Jones7ab77e02007-04-20 15:58:00 -0400865 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200866 cx = &pr->power.states[ACPI_STATE_C3];
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200867 if (cx->address > 0 && cx->latency <= 1000)
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100868 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200869 }
Rafał Bilski905497c2007-07-08 21:51:26 +0200870 /* Disable if it isn't working */
871 if (disable_acpi_c3)
872 longhaul_flags &= ~USE_ACPI_C3;
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100873 /* Check if northbridge is friendly */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200874 if (enable_arbiter_disable())
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100875 longhaul_flags |= USE_NORTHBRIDGE;
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200876
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100877 /* Check ACPI support for bus master arbiter disable */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200878 if (!(longhaul_flags & USE_ACPI_C3
879 || longhaul_flags & USE_NORTHBRIDGE)
880 && ((pr == NULL) || !(pr->flags.bm_control))) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700881 pr_err("No ACPI support: Unsupported northbridge\n");
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100882 return -ENODEV;
883 }
884
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100885 if (longhaul_flags & USE_NORTHBRIDGE)
Joe Perches1c5864e2016-04-05 13:28:25 -0700886 pr_info("Using northbridge support\n");
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200887 if (longhaul_flags & USE_ACPI_C3)
Joe Perches1c5864e2016-04-05 13:28:25 -0700888 pr_info("Using ACPI support\n");
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ret = longhaul_get_ranges();
891 if (ret != 0)
892 return ret;
893
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100894 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 longhaul_setup_voltagescaling();
896
Viresh Kumar1d0d0642017-12-07 15:15:19 +0530897 policy->transition_delay_us = 200000; /* usec */
Viresh Kumar2c656162018-02-26 10:38:54 +0530898 policy->freq_table = longhaul_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Viresh Kumar2c656162018-02-26 10:38:54 +0530900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Linus Torvalds221dee22007-02-26 14:55:48 -0800903static struct cpufreq_driver longhaul_driver = {
Viresh Kumar3a4d0342013-10-03 20:28:10 +0530904 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530905 .target_index = longhaul_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .get = longhaul_get,
907 .init = longhaul_cpu_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .name = "longhaul",
Viresh Kumar3a4d0342013-10-03 20:28:10 +0530909 .attr = cpufreq_generic_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910};
911
Andi Kleenfa8031a2012-01-26 00:09:12 +0100912static const struct x86_cpu_id longhaul_id[] = {
Thomas Gleixnerb11d77f2020-03-24 14:51:51 +0100913 X86_MATCH_VENDOR_FAM(CENTAUR, 6, NULL),
Andi Kleenfa8031a2012-01-26 00:09:12 +0100914 {}
915};
916MODULE_DEVICE_TABLE(x86cpu, longhaul_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918static int __init longhaul_init(void)
919{
Mike Travis92cb7612007-10-19 20:35:04 +0200920 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Andi Kleenfa8031a2012-01-26 00:09:12 +0100922 if (!x86_match_cpu(longhaul_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENODEV;
924
Rafał Bilskib5811bc2012-12-15 00:45:02 +0100925 if (!enable) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700926 pr_err("Option \"enable\" not set - Aborting\n");
Rafał Bilskib5811bc2012-12-15 00:45:02 +0100927 return -ENODEV;
928 }
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200929#ifdef CONFIG_SMP
930 if (num_online_cpus() > 1) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700931 pr_err("More than 1 CPU detected, longhaul disabled\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500932 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200933 }
934#endif
935#ifdef CONFIG_X86_IO_APIC
Borislav Petkov93984fb2016-04-04 22:25:00 +0200936 if (boot_cpu_has(X86_FEATURE_APIC)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700937 pr_err("APIC detected. Longhaul is currently broken in this configuration.\n");
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200938 return -ENODEV;
939 }
940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 switch (c->x86_model) {
942 case 6 ... 9:
943 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500944 case 10:
Joe Perches1c5864e2016-04-05 13:28:25 -0700945 pr_err("Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 default:
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800947 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 return -ENODEV;
951}
952
953
954static void __exit longhaul_exit(void)
955{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530956 struct cpufreq_policy *policy = cpufreq_cpu_get(0);
Dave Jones8eebf1a2006-05-30 17:40:16 -0400957 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Dave Jonesac617bd2009-01-17 23:29:53 -0500959 for (i = 0; i < numscales; i++) {
960 if (mults[i] == maxmult) {
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530961 struct cpufreq_freqs freqs;
962
963 freqs.old = policy->cur;
964 freqs.new = longhaul_table[i].frequency;
965 freqs.flags = 0;
966
967 cpufreq_freq_transition_begin(policy, &freqs);
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530968 longhaul_setstate(policy, i);
Srivatsa S. Bhat7aa05572014-04-29 00:24:09 +0530969 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 }
972 }
973
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530974 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 cpufreq_unregister_driver(&longhaul_driver);
976 kfree(longhaul_table);
977}
978
Rafal Bilski52a26382007-10-07 00:24:32 -0700979/* Even if BIOS is exporting ACPI C3 state, and it is used
980 * with success when CPU is idle, this state doesn't
981 * trigger frequency transition in some cases. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500982module_param(disable_acpi_c3, int, 0644);
Rafał Bilski905497c2007-07-08 21:51:26 +0200983MODULE_PARM_DESC(disable_acpi_c3, "Don't use ACPI C3 support");
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300984/* Change CPU voltage with frequency. Very useful to save
Rafal Bilski52a26382007-10-07 00:24:32 -0700985 * power, but most VIA C3 processors aren't supporting it. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500986module_param(scale_voltage, int, 0644);
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200987MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Rafal Bilski52a26382007-10-07 00:24:32 -0700988/* Force revision key to 0 for processors which doesn't
989 * support voltage scaling, but are introducing itself as
990 * such. */
991module_param(revid_errata, int, 0644);
992MODULE_PARM_DESC(revid_errata, "Ignore CPU Revision ID");
Rafał Bilskib5811bc2012-12-15 00:45:02 +0100993/* By default driver is disabled to prevent incompatible
994 * system freeze. */
995module_param(enable, int, 0644);
996MODULE_PARM_DESC(enable, "Enable driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Dave Jonesd5e80b42014-12-19 11:20:43 -0500998MODULE_AUTHOR("Dave Jones");
Dave Jonesac617bd2009-01-17 23:29:53 -0500999MODULE_DESCRIPTION("Longhaul driver for VIA Cyrix processors.");
1000MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +02001002late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003module_exit(longhaul_exit);