xref: /linux/drivers/hwmon/hwmon-vid.c (revision ad736c1a)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2303760b4SJean Delvare /*
315872212SFrank Myhr  * hwmon-vid.c - VID/VRM/VRD voltage conversions
415872212SFrank Myhr  *
515872212SFrank Myhr  * Copyright (c) 2004 Rudolf Marek <r.marek@assembler.cz>
615872212SFrank Myhr  *
715872212SFrank Myhr  * Partly imported from i2c-vid.h of the lm_sensors project
815872212SFrank Myhr  * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
915872212SFrank Myhr  * With assistance from Trent Piepho <xyzzy@speakeasy.org>
10303760b4SJean Delvare  */
11303760b4SJean Delvare 
121f923c7aSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
131f923c7aSJoe Perches 
14303760b4SJean Delvare #include <linux/module.h>
15303760b4SJean Delvare #include <linux/kernel.h>
16303760b4SJean Delvare #include <linux/hwmon-vid.h>
17303760b4SJean Delvare 
18d0f28270SJean Delvare /*
1915872212SFrank Myhr  * Common code for decoding VID pins.
2015872212SFrank Myhr  *
2115872212SFrank Myhr  * References:
2215872212SFrank Myhr  *
2315872212SFrank Myhr  * For VRM 8.4 to 9.1, "VRM x.y DC-DC Converter Design Guidelines",
2415872212SFrank Myhr  * available at http://developer.intel.com/.
2515872212SFrank Myhr  *
2615872212SFrank Myhr  * For VRD 10.0 and up, "VRD x.y Design Guide",
2715872212SFrank Myhr  * available at http://developer.intel.com/.
2815872212SFrank Myhr  *
29cebd7709SJean Delvare  * AMD Athlon 64 and AMD Opteron Processors, AMD Publication 26094,
30631dd1a8SJustin P. Mattock  * http://support.amd.com/us/Processor_TechDocs/26094.PDF
31cebd7709SJean Delvare  * Table 74. VID Code Voltages
32cebd7709SJean Delvare  * This corresponds to an arbitrary VRM code of 24 in the functions below.
33cebd7709SJean Delvare  * These CPU models (K8 revision <= E) have 5 VID pins. See also:
34cebd7709SJean Delvare  * Revision Guide for AMD Athlon 64 and AMD Opteron Processors, AMD Publication 25759,
35cebd7709SJean Delvare  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25759.pdf
36cebd7709SJean Delvare  *
37cebd7709SJean Delvare  * AMD NPT Family 0Fh Processors, AMD Publication 32559,
38116d0486SFrank Myhr  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/32559.pdf
39116d0486SFrank Myhr  * Table 71. VID Code Voltages
40cebd7709SJean Delvare  * This corresponds to an arbitrary VRM code of 25 in the functions below.
41cebd7709SJean Delvare  * These CPU models (K8 revision >= F) have 6 VID pins. See also:
42cebd7709SJean Delvare  * Revision Guide for AMD NPT Family 0Fh Processors, AMD Publication 33610,
43cebd7709SJean Delvare  * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
4415872212SFrank Myhr  *
4515872212SFrank Myhr  * The 17 specification is in fact Intel Mobile Voltage Positioning -
4615872212SFrank Myhr  * (IMVP-II). You can find more information in the datasheet of Max1718
4715872212SFrank Myhr  * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2452
4815872212SFrank Myhr  *
4915872212SFrank Myhr  * The 13 specification corresponds to the Intel Pentium M series. There
5015872212SFrank Myhr  * doesn't seem to be any named specification for these. The conversion
5115872212SFrank Myhr  * tables are detailed directly in the various Pentium M datasheets:
52*ad736c1aSAlexander A. Klimov  * https://www.intel.com/design/intarch/pentiumm/docs_pentiumm.htm
5315872212SFrank Myhr  *
5415872212SFrank Myhr  * The 14 specification corresponds to Intel Core series. There
5515872212SFrank Myhr  * doesn't seem to be any named specification for these. The conversion
5615872212SFrank Myhr  * tables are detailed directly in the various Pentium Core datasheets:
57*ad736c1aSAlexander A. Klimov  * https://www.intel.com/design/mobile/datashts/309221.htm
5815872212SFrank Myhr  *
5915872212SFrank Myhr  * The 110 (VRM 11) specification corresponds to Intel Conroe based series.
60*ad736c1aSAlexander A. Klimov  * https://www.intel.com/design/processor/applnots/313214.htm
61d0f28270SJean Delvare  */
62d0f28270SJean Delvare 
6315872212SFrank Myhr /*
6415872212SFrank Myhr  * vrm is the VRM/VRD document version multiplied by 10.
6515872212SFrank Myhr  * val is the 4-bit or more VID code.
6615872212SFrank Myhr  * Returned value is in mV to avoid floating point in the kernel.
6715872212SFrank Myhr  * Some VID have some bits in uV scale, this is rounded to mV.
6815872212SFrank Myhr  */
69734a12a3SRudolf Marek int vid_from_reg(int val, u8 vrm)
70d0f28270SJean Delvare {
71d0f28270SJean Delvare 	int vid;
72d0f28270SJean Delvare 
73d0f28270SJean Delvare 	switch (vrm) {
74d0f28270SJean Delvare 
75d0f28270SJean Delvare 	case 100:		/* VRD 10.0 */
766af586dcSRudolf Marek 		/* compute in uV, round to mV */
77177d165dSRudolf Marek 		val &= 0x3f;
78d0f28270SJean Delvare 		if ((val & 0x1f) == 0x1f)
79d0f28270SJean Delvare 			return 0;
80d0f28270SJean Delvare 		if ((val & 0x1f) <= 0x09 || val == 0x0a)
816af586dcSRudolf Marek 			vid = 1087500 - (val & 0x1f) * 25000;
82d0f28270SJean Delvare 		else
836af586dcSRudolf Marek 			vid = 1862500 - (val & 0x1f) * 25000;
84d0f28270SJean Delvare 		if (val & 0x20)
856af586dcSRudolf Marek 			vid -= 12500;
867fe83ad8SFrans Meulenbroeks 		return (vid + 500) / 1000;
87d0f28270SJean Delvare 
886af586dcSRudolf Marek 	case 110:		/* Intel Conroe */
896af586dcSRudolf Marek 				/* compute in uV, round to mV */
906af586dcSRudolf Marek 		val &= 0xff;
919fab2d8bSJean Delvare 		if (val < 0x02 || val > 0xb2)
926af586dcSRudolf Marek 			return 0;
937fe83ad8SFrans Meulenbroeks 		return (1600000 - (val - 2) * 6250 + 500) / 1000;
94116d0486SFrank Myhr 
95cebd7709SJean Delvare 	case 24:		/* Athlon64 & Opteron */
96cebd7709SJean Delvare 		val &= 0x1f;
97cebd7709SJean Delvare 		if (val == 0x1f)
98cebd7709SJean Delvare 			return 0;
99cebd7709SJean Delvare 				/* fall through */
100cebd7709SJean Delvare 	case 25:		/* AMD NPT 0Fh */
101116d0486SFrank Myhr 		val &= 0x3f;
102116d0486SFrank Myhr 		return (val < 32) ? 1550 - 25 * val
103116d0486SFrank Myhr 			: 775 - (25 * (val - 31)) / 2;
104d0f28270SJean Delvare 
105c8ecd27dSJean Delvare 	case 26:		/* AMD family 10h to 15h, serial VID */
106c8ecd27dSJean Delvare 		val &= 0x7f;
107c8ecd27dSJean Delvare 		if (val >= 0x7c)
108c8ecd27dSJean Delvare 			return 0;
109c8ecd27dSJean Delvare 		return DIV_ROUND_CLOSEST(15500 - 125 * val, 10);
110c8ecd27dSJean Delvare 
111d0f28270SJean Delvare 	case 91:		/* VRM 9.1 */
112d0f28270SJean Delvare 	case 90:		/* VRM 9.0 */
113177d165dSRudolf Marek 		val &= 0x1f;
1147fe83ad8SFrans Meulenbroeks 		return val == 0x1f ? 0 :
1157fe83ad8SFrans Meulenbroeks 				     1850 - val * 25;
116d0f28270SJean Delvare 
117d0f28270SJean Delvare 	case 85:		/* VRM 8.5 */
118177d165dSRudolf Marek 		val &= 0x1f;
1197fe83ad8SFrans Meulenbroeks 		return (val & 0x10  ? 25 : 0) +
120d0f28270SJean Delvare 		       ((val & 0x0f) > 0x04 ? 2050 : 1250) -
1217fe83ad8SFrans Meulenbroeks 		       ((val & 0x0f) * 50);
122d0f28270SJean Delvare 
123d0f28270SJean Delvare 	case 84:		/* VRM 8.4 */
124d0f28270SJean Delvare 		val &= 0x0f;
125d0f28270SJean Delvare 				/* fall through */
126734a12a3SRudolf Marek 	case 82:		/* VRM 8.2 */
127177d165dSRudolf Marek 		val &= 0x1f;
1287fe83ad8SFrans Meulenbroeks 		return val == 0x1f ? 0 :
129d0f28270SJean Delvare 		       val & 0x10  ? 5100 - (val) * 100 :
1307fe83ad8SFrans Meulenbroeks 				     2050 - (val) * 50;
131734a12a3SRudolf Marek 	case 17:		/* Intel IMVP-II */
132177d165dSRudolf Marek 		val &= 0x1f;
1337fe83ad8SFrans Meulenbroeks 		return val & 0x10 ? 975 - (val & 0xF) * 25 :
1347fe83ad8SFrans Meulenbroeks 				    1750 - val * 50;
1354c537fb2SJean Delvare 	case 13:
1360a88f4b5SJean Delvare 	case 131:
137177d165dSRudolf Marek 		val &= 0x3f;
1380a88f4b5SJean Delvare 		/* Exception for Eden ULV 500 MHz */
1390a88f4b5SJean Delvare 		if (vrm == 131 && val == 0x3f)
1400a88f4b5SJean Delvare 			val++;
1417fe83ad8SFrans Meulenbroeks 		return 1708 - val * 16;
1426af586dcSRudolf Marek 	case 14:		/* Intel Core */
1436af586dcSRudolf Marek 				/* compute in uV, round to mV */
1446af586dcSRudolf Marek 		val &= 0x7f;
1457fe83ad8SFrans Meulenbroeks 		return val > 0x77 ? 0 : (1500000 - (val * 12500) + 500) / 1000;
146734a12a3SRudolf Marek 	default:		/* report 0 for unknown */
14745f2acc4SJean Delvare 		if (vrm)
1481f923c7aSJoe Perches 			pr_warn("Requested unsupported VRM version (%u)\n",
1491f923c7aSJoe Perches 				(unsigned int)vrm);
150734a12a3SRudolf Marek 		return 0;
151d0f28270SJean Delvare 	}
152d0f28270SJean Delvare }
153f352df65SGuenter Roeck EXPORT_SYMBOL(vid_from_reg);
154d0f28270SJean Delvare 
155d0f28270SJean Delvare /*
15615872212SFrank Myhr  * After this point is the code to automatically determine which
15715872212SFrank Myhr  * VRM/VRD specification should be used depending on the CPU.
158d0f28270SJean Delvare  */
159d0f28270SJean Delvare 
160303760b4SJean Delvare struct vrm_model {
161303760b4SJean Delvare 	u8 vendor;
1623230f704SGuenter Roeck 	u8 family;
1633230f704SGuenter Roeck 	u8 model_from;
1643230f704SGuenter Roeck 	u8 model_to;
1653230f704SGuenter Roeck 	u8 stepping_to;
166734a12a3SRudolf Marek 	u8 vrm_type;
167303760b4SJean Delvare };
168303760b4SJean Delvare 
169303760b4SJean Delvare #define ANY 0xFF
170303760b4SJean Delvare 
171303760b4SJean Delvare #ifdef CONFIG_X86
172303760b4SJean Delvare 
173cebd7709SJean Delvare /*
1743230f704SGuenter Roeck  * The stepping_to parameter is highest acceptable stepping for current line.
175cebd7709SJean Delvare  * The model match must be exact for 4-bit values. For model values 0x10
176cebd7709SJean Delvare  * and above (extended model), all models below the parameter will match.
177cebd7709SJean Delvare  */
178734a12a3SRudolf Marek 
179303760b4SJean Delvare static struct vrm_model vrm_models[] = {
1803230f704SGuenter Roeck 	{X86_VENDOR_AMD, 0x6, 0x0, ANY, ANY, 90},	/* Athlon Duron etc */
1813230f704SGuenter Roeck 	{X86_VENDOR_AMD, 0xF, 0x0, 0x3F, ANY, 24},	/* Athlon 64, Opteron */
18286d566e5SGuenter Roeck 	/*
18386d566e5SGuenter Roeck 	 * In theory, all NPT family 0Fh processors have 6 VID pins and should
18486d566e5SGuenter Roeck 	 * thus use vrm 25, however in practice not all mainboards route the
18586d566e5SGuenter Roeck 	 * 6th VID pin because it is never needed. So we use the 5 VID pin
18686d566e5SGuenter Roeck 	 * variant (vrm 24) for the models which exist today.
18786d566e5SGuenter Roeck 	 */
1883230f704SGuenter Roeck 	{X86_VENDOR_AMD, 0xF, 0x40, 0x7F, ANY, 24},	/* NPT family 0Fh */
1893230f704SGuenter Roeck 	{X86_VENDOR_AMD, 0xF, 0x80, ANY, ANY, 25},	/* future fam. 0Fh */
1903230f704SGuenter Roeck 	{X86_VENDOR_AMD, 0x10, 0x0, ANY, ANY, 25},	/* NPT family 10h */
191c8ecd27dSJean Delvare 	{X86_VENDOR_AMD, 0x11, 0x0, ANY, ANY, 26},	/* family 11h */
192c8ecd27dSJean Delvare 	{X86_VENDOR_AMD, 0x12, 0x0, ANY, ANY, 26},	/* family 12h */
193c8ecd27dSJean Delvare 	{X86_VENDOR_AMD, 0x14, 0x0, ANY, ANY, 26},	/* family 14h */
194c8ecd27dSJean Delvare 	{X86_VENDOR_AMD, 0x15, 0x0, ANY, ANY, 26},	/* family 15h */
1955bed13f5SJean Delvare 
1963230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0x0, 0x6, ANY, 82},	/* Pentium Pro,
1973230f704SGuenter Roeck 							 * Pentium II, Xeon,
1983230f704SGuenter Roeck 							 * Mobile Pentium,
1993230f704SGuenter Roeck 							 * Celeron */
2003230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0x7, 0x7, ANY, 84},	/* Pentium III, Xeon */
2013230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0x8, 0x8, ANY, 82},	/* Pentium III, Xeon */
2023230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0x9, 0x9, ANY, 13},	/* Pentium M (130 nm) */
2033230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0xA, 0xA, ANY, 82},	/* Pentium III Xeon */
2043230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0xB, 0xB, ANY, 85},	/* Tualatin */
2053230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0xD, 0xD, ANY, 13},	/* Pentium M (90 nm) */
2063230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0xE, 0xE, ANY, 14},	/* Intel Core (65 nm) */
2073230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0x6, 0xF, ANY, ANY, 110},	/* Intel Conroe and
2083230f704SGuenter Roeck 							 * later */
2093230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0xF, 0x0, 0x0, ANY, 90},	/* P4 */
2103230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0xF, 0x1, 0x1, ANY, 90},	/* P4 Willamette */
2113230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0xF, 0x2, 0x2, ANY, 90},	/* P4 Northwood */
2123230f704SGuenter Roeck 	{X86_VENDOR_INTEL, 0xF, 0x3, ANY, ANY, 100},	/* Prescott and above
2133230f704SGuenter Roeck 							 * assume VRD 10 */
2145bed13f5SJean Delvare 
2153230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0x7, 0x7, ANY, 85},	/* Eden ESP/Ezra */
2163230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0x8, 0x8, 0x7, 85},	/* Ezra T */
2173230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0x9, 0x9, 0x7, 85},	/* Nehemiah */
2183230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0x9, 0x9, ANY, 17},	/* C3-M, Eden-N */
2193230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0xA, 0xA, 0x7, 0},	/* No information */
2203230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0xA, 0xA, ANY, 13},	/* C7-M, C7,
2213230f704SGuenter Roeck 							 * Eden (Esther) */
2223230f704SGuenter Roeck 	{X86_VENDOR_CENTAUR, 0x6, 0xD, 0xD, ANY, 134},	/* C7-D, C7-M, C7,
2233230f704SGuenter Roeck 							 * Eden (Esther) */
224303760b4SJean Delvare };
225303760b4SJean Delvare 
2260a88f4b5SJean Delvare /*
2270a88f4b5SJean Delvare  * Special case for VIA model D: there are two different possible
2280a88f4b5SJean Delvare  * VID tables, so we have to figure out first, which one must be
2290a88f4b5SJean Delvare  * used. This resolves temporary drm value 134 to 14 (Intel Core
2300a88f4b5SJean Delvare  * 7-bit VID), 13 (Pentium M 6-bit VID) or 131 (Pentium M 6-bit VID
2310a88f4b5SJean Delvare  * + quirk for Eden ULV 500 MHz).
2320a88f4b5SJean Delvare  * Note: something similar might be needed for model A, I'm not sure.
2330a88f4b5SJean Delvare  */
2340a88f4b5SJean Delvare static u8 get_via_model_d_vrm(void)
2350a88f4b5SJean Delvare {
23698128de3SGuenter Roeck 	unsigned int vid, brand, __maybe_unused dummy;
2370a88f4b5SJean Delvare 	static const char *brands[4] = {
2380a88f4b5SJean Delvare 		"C7-M", "C7", "Eden", "C7-D"
2390a88f4b5SJean Delvare 	};
2400a88f4b5SJean Delvare 
2410a88f4b5SJean Delvare 	rdmsr(0x198, dummy, vid);
2420a88f4b5SJean Delvare 	vid &= 0xff;
2430a88f4b5SJean Delvare 
2440a88f4b5SJean Delvare 	rdmsr(0x1154, brand, dummy);
2450a88f4b5SJean Delvare 	brand = ((brand >> 4) ^ (brand >> 2)) & 0x03;
2460a88f4b5SJean Delvare 
2470a88f4b5SJean Delvare 	if (vid > 0x3f) {
2480a88f4b5SJean Delvare 		pr_info("Using %d-bit VID table for VIA %s CPU\n",
2490a88f4b5SJean Delvare 			7, brands[brand]);
2500a88f4b5SJean Delvare 		return 14;
2510a88f4b5SJean Delvare 	} else {
2520a88f4b5SJean Delvare 		pr_info("Using %d-bit VID table for VIA %s CPU\n",
2530a88f4b5SJean Delvare 			6, brands[brand]);
2540a88f4b5SJean Delvare 		/* Enable quirk for Eden */
2550a88f4b5SJean Delvare 		return brand == 2 ? 131 : 13;
2560a88f4b5SJean Delvare 	}
2570a88f4b5SJean Delvare }
2580a88f4b5SJean Delvare 
2593230f704SGuenter Roeck static u8 find_vrm(u8 family, u8 model, u8 stepping, u8 vendor)
260303760b4SJean Delvare {
2613230f704SGuenter Roeck 	int i;
262303760b4SJean Delvare 
2633230f704SGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(vrm_models); i++) {
2643230f704SGuenter Roeck 		if (vendor == vrm_models[i].vendor &&
2653230f704SGuenter Roeck 		    family == vrm_models[i].family &&
2663230f704SGuenter Roeck 		    model >= vrm_models[i].model_from &&
2673230f704SGuenter Roeck 		    model <= vrm_models[i].model_to &&
2683230f704SGuenter Roeck 		    stepping <= vrm_models[i].stepping_to)
269303760b4SJean Delvare 			return vrm_models[i].vrm_type;
270303760b4SJean Delvare 	}
271303760b4SJean Delvare 
272303760b4SJean Delvare 	return 0;
273303760b4SJean Delvare }
274303760b4SJean Delvare 
275734a12a3SRudolf Marek u8 vid_which_vrm(void)
276303760b4SJean Delvare {
27792cb7612SMike Travis 	struct cpuinfo_x86 *c = &cpu_data(0);
2783230f704SGuenter Roeck 	u8 vrm_ret;
279303760b4SJean Delvare 
280da97a5a3SJean Delvare 	if (c->x86 < 6)		/* Any CPU with family lower than 6 */
2813230f704SGuenter Roeck 		return 0;	/* doesn't have VID */
282da97a5a3SJean Delvare 
283b399151cSJia Zhang 	vrm_ret = find_vrm(c->x86, c->x86_model, c->x86_stepping, c->x86_vendor);
2840a88f4b5SJean Delvare 	if (vrm_ret == 134)
2850a88f4b5SJean Delvare 		vrm_ret = get_via_model_d_vrm();
286303760b4SJean Delvare 	if (vrm_ret == 0)
2871f923c7aSJoe Perches 		pr_info("Unknown VRM version of your x86 CPU\n");
288303760b4SJean Delvare 	return vrm_ret;
289303760b4SJean Delvare }
290303760b4SJean Delvare 
291734a12a3SRudolf Marek /* and now for something completely different for the non-x86 world */
292303760b4SJean Delvare #else
293734a12a3SRudolf Marek u8 vid_which_vrm(void)
294303760b4SJean Delvare {
2951f923c7aSJoe Perches 	pr_info("Unknown VRM version of your CPU\n");
296303760b4SJean Delvare 	return 0;
297303760b4SJean Delvare }
298303760b4SJean Delvare #endif
299303760b4SJean Delvare EXPORT_SYMBOL(vid_which_vrm);
300303760b4SJean Delvare 
3017188cc66SJean Delvare MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
302303760b4SJean Delvare 
303303760b4SJean Delvare MODULE_DESCRIPTION("hwmon-vid driver");
304303760b4SJean Delvare MODULE_LICENSE("GPL");
305