1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * k8temp.c - Linux kernel module for hardware monitoring
5 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
7 * Inspired from the w83785 and amd756 drivers.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/pci.h>
14 #include <linux/hwmon.h>
15 #include <linux/err.h>
16 #include <linux/mutex.h>
17 #include <asm/processor.h>
19 #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
21 #define SEL_PLACE 0x40
25 struct mutex update_lock
;
27 /* registers values */
28 u8 sensorsp
; /* sensor presence bits - SEL_CORE, SEL_PLACE */
29 u8 swap_core_select
; /* meaning of SEL_CORE is inverted */
33 static const struct pci_device_id k8temp_ids
[] = {
34 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_K8_NB_MISC
) },
37 MODULE_DEVICE_TABLE(pci
, k8temp_ids
);
39 static int is_rev_g_desktop(u8 model
)
46 if (model
== 0xc1 || model
== 0x6c || model
== 0x7c)
50 * Differentiate between AM2 and ASB1.
51 * See "Constructing the processor Name String" in "Revision
52 * Guide for AMD NPT Family 0Fh Processors" (33610).
54 brandidx
= cpuid_ebx(0x80000001);
55 brandidx
= (brandidx
>> 9) & 0x1f;
58 if ((model
== 0x6f || model
== 0x7f) &&
59 (brandidx
== 0x7 || brandidx
== 0x9 || brandidx
== 0xc))
64 (brandidx
== 0xb || brandidx
== 0xc))
71 k8temp_is_visible(const void *drvdata
, enum hwmon_sensor_types type
,
72 u32 attr
, int channel
)
74 const struct k8temp_data
*data
= drvdata
;
76 if ((channel
& 1) && !(data
->sensorsp
& SEL_PLACE
))
79 if ((channel
& 2) && !(data
->sensorsp
& SEL_CORE
))
86 k8temp_read(struct device
*dev
, enum hwmon_sensor_types type
,
87 u32 attr
, int channel
, long *val
)
89 struct k8temp_data
*data
= dev_get_drvdata(dev
);
90 struct pci_dev
*pdev
= to_pci_dev(dev
->parent
);
95 core
= (channel
>> 1) & 1;
98 core
^= data
->swap_core_select
;
100 mutex_lock(&data
->update_lock
);
101 pci_read_config_byte(pdev
, REG_TEMP
, &tmp
);
102 tmp
&= ~(SEL_PLACE
| SEL_CORE
);
107 pci_write_config_byte(pdev
, REG_TEMP
, tmp
);
108 pci_read_config_dword(pdev
, REG_TEMP
, &temp
);
109 mutex_unlock(&data
->update_lock
);
111 *val
= TEMP_FROM_REG(temp
) + data
->temp_offset
;
116 static const struct hwmon_ops k8temp_ops
= {
117 .is_visible
= k8temp_is_visible
,
121 static const struct hwmon_channel_info
*k8temp_info
[] = {
122 HWMON_CHANNEL_INFO(temp
,
123 HWMON_T_INPUT
, HWMON_T_INPUT
, HWMON_T_INPUT
, HWMON_T_INPUT
),
127 static const struct hwmon_chip_info k8temp_chip_info
= {
132 static int k8temp_probe(struct pci_dev
*pdev
,
133 const struct pci_device_id
*id
)
138 struct k8temp_data
*data
;
139 struct device
*hwmon_dev
;
141 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct k8temp_data
), GFP_KERNEL
);
145 model
= boot_cpu_data
.x86_model
;
146 stepping
= boot_cpu_data
.x86_stepping
;
148 /* feature available since SH-C0, exclude older revisions */
149 if ((model
== 4 && stepping
== 0) ||
150 (model
== 5 && stepping
<= 1))
154 * AMD NPT family 0fh, i.e. RevF and RevG:
155 * meaning of SEL_CORE bit is inverted
158 data
->swap_core_select
= 1;
160 "Temperature readouts might be wrong - check erratum #141\n");
164 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
165 * additional offset, otherwise reported temperature is below
166 * ambient temperature
168 if (is_rev_g_desktop(model
))
169 data
->temp_offset
= 21000;
171 pci_read_config_byte(pdev
, REG_TEMP
, &scfg
);
172 scfg
&= ~(SEL_PLACE
| SEL_CORE
); /* Select sensor 0, core0 */
173 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
174 pci_read_config_byte(pdev
, REG_TEMP
, &scfg
);
176 if (scfg
& (SEL_PLACE
| SEL_CORE
)) {
177 dev_err(&pdev
->dev
, "Configuration bit(s) stuck at 1!\n");
181 scfg
|= (SEL_PLACE
| SEL_CORE
);
182 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
184 /* now we know if we can change core and/or sensor */
185 pci_read_config_byte(pdev
, REG_TEMP
, &data
->sensorsp
);
187 if (data
->sensorsp
& SEL_PLACE
) {
188 scfg
&= ~SEL_CORE
; /* Select sensor 1, core0 */
189 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
190 pci_read_config_dword(pdev
, REG_TEMP
, &temp
);
191 scfg
|= SEL_CORE
; /* prepare for next selection */
192 if (!((temp
>> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
193 data
->sensorsp
&= ~SEL_PLACE
;
196 if (data
->sensorsp
& SEL_CORE
) {
197 scfg
&= ~SEL_PLACE
; /* Select sensor 0, core1 */
198 pci_write_config_byte(pdev
, REG_TEMP
, scfg
);
199 pci_read_config_dword(pdev
, REG_TEMP
, &temp
);
200 if (!((temp
>> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
201 data
->sensorsp
&= ~SEL_CORE
;
204 mutex_init(&data
->update_lock
);
206 hwmon_dev
= devm_hwmon_device_register_with_info(&pdev
->dev
,
212 return PTR_ERR_OR_ZERO(hwmon_dev
);
215 static struct pci_driver k8temp_driver
= {
217 .id_table
= k8temp_ids
,
218 .probe
= k8temp_probe
,
221 module_pci_driver(k8temp_driver
);
223 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
224 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
225 MODULE_LICENSE("GPL");