1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * k8temp.c - Linux kernel module for hardware monitoring
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/jiffies.h>
14 #include <linux/pci.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <asm/processor.h>
21 #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
23 #define SEL_PLACE 0x40
27 struct device *hwmon_dev;
28 struct mutex update_lock;
30 char valid; /* zero until following fields are valid */
31 unsigned long last_updated; /* in jiffies */
33 /* registers values */
34 u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */
35 u32 temp[2][2]; /* core, place */
36 u8 swap_core_select; /* meaning of SEL_CORE is inverted */
40 static struct k8temp_data *k8temp_update_device(struct device *dev)
42 struct k8temp_data *data = dev_get_drvdata(dev);
43 struct pci_dev *pdev = to_pci_dev(dev);
46 mutex_lock(&data->update_lock);
49 || time_after(jiffies, data->last_updated + HZ)) {
50 pci_read_config_byte(pdev, REG_TEMP, &tmp);
51 tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
52 pci_write_config_byte(pdev, REG_TEMP, tmp);
53 pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
55 if (data->sensorsp & SEL_PLACE) {
56 tmp |= SEL_PLACE; /* Select sensor 1, core0 */
57 pci_write_config_byte(pdev, REG_TEMP, tmp);
58 pci_read_config_dword(pdev, REG_TEMP,
62 if (data->sensorsp & SEL_CORE) {
63 tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
65 pci_write_config_byte(pdev, REG_TEMP, tmp);
66 pci_read_config_dword(pdev, REG_TEMP,
69 if (data->sensorsp & SEL_PLACE) {
70 tmp |= SEL_PLACE; /* Select sensor 1, core1 */
71 pci_write_config_byte(pdev, REG_TEMP, tmp);
72 pci_read_config_dword(pdev, REG_TEMP,
77 data->last_updated = jiffies;
81 mutex_unlock(&data->update_lock);
89 static ssize_t name_show(struct device *dev, struct device_attribute
92 struct k8temp_data *data = dev_get_drvdata(dev);
94 return sprintf(buf, "%s\n", data->name);
98 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
101 struct sensor_device_attribute_2 *attr =
102 to_sensor_dev_attr_2(devattr);
104 int place = attr->index;
106 struct k8temp_data *data = k8temp_update_device(dev);
108 if (data->swap_core_select && (data->sensorsp & SEL_CORE))
111 temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
113 return sprintf(buf, "%d\n", temp);
118 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
119 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
120 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
121 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
122 static DEVICE_ATTR_RO(name);
124 static const struct pci_device_id k8temp_ids[] = {
125 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
129 MODULE_DEVICE_TABLE(pci, k8temp_ids);
131 static int is_rev_g_desktop(u8 model)
138 if (model == 0xc1 || model == 0x6c || model == 0x7c)
142 * Differentiate between AM2 and ASB1.
143 * See "Constructing the processor Name String" in "Revision
144 * Guide for AMD NPT Family 0Fh Processors" (33610).
146 brandidx = cpuid_ebx(0x80000001);
147 brandidx = (brandidx >> 9) & 0x1f;
150 if ((model == 0x6f || model == 0x7f) &&
151 (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
156 (brandidx == 0xb || brandidx == 0xc))
162 static int k8temp_probe(struct pci_dev *pdev,
163 const struct pci_device_id *id)
169 struct k8temp_data *data;
171 data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
175 model = boot_cpu_data.x86_model;
176 stepping = boot_cpu_data.x86_stepping;
178 /* feature available since SH-C0, exclude older revisions */
179 if ((model == 4 && stepping == 0) ||
180 (model == 5 && stepping <= 1))
184 * AMD NPT family 0fh, i.e. RevF and RevG:
185 * meaning of SEL_CORE bit is inverted
188 data->swap_core_select = 1;
190 "Temperature readouts might be wrong - check erratum #141\n");
194 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
195 * additional offset, otherwise reported temperature is below
196 * ambient temperature
198 if (is_rev_g_desktop(model))
199 data->temp_offset = 21000;
201 pci_read_config_byte(pdev, REG_TEMP, &scfg);
202 scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
203 pci_write_config_byte(pdev, REG_TEMP, scfg);
204 pci_read_config_byte(pdev, REG_TEMP, &scfg);
206 if (scfg & (SEL_PLACE | SEL_CORE)) {
207 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
211 scfg |= (SEL_PLACE | SEL_CORE);
212 pci_write_config_byte(pdev, REG_TEMP, scfg);
214 /* now we know if we can change core and/or sensor */
215 pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
217 if (data->sensorsp & SEL_PLACE) {
218 scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
219 pci_write_config_byte(pdev, REG_TEMP, scfg);
220 pci_read_config_dword(pdev, REG_TEMP, &temp);
221 scfg |= SEL_CORE; /* prepare for next selection */
222 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
223 data->sensorsp &= ~SEL_PLACE;
226 if (data->sensorsp & SEL_CORE) {
227 scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
228 pci_write_config_byte(pdev, REG_TEMP, scfg);
229 pci_read_config_dword(pdev, REG_TEMP, &temp);
230 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
231 data->sensorsp &= ~SEL_CORE;
234 data->name = "k8temp";
235 mutex_init(&data->update_lock);
236 pci_set_drvdata(pdev, data);
238 /* Register sysfs hooks */
239 err = device_create_file(&pdev->dev,
240 &sensor_dev_attr_temp1_input.dev_attr);
244 /* sensor can be changed and reports something */
245 if (data->sensorsp & SEL_PLACE) {
246 err = device_create_file(&pdev->dev,
247 &sensor_dev_attr_temp2_input.dev_attr);
252 /* core can be changed and reports something */
253 if (data->sensorsp & SEL_CORE) {
254 err = device_create_file(&pdev->dev,
255 &sensor_dev_attr_temp3_input.dev_attr);
258 if (data->sensorsp & SEL_PLACE) {
259 err = device_create_file(&pdev->dev,
260 &sensor_dev_attr_temp4_input.
267 err = device_create_file(&pdev->dev, &dev_attr_name);
271 data->hwmon_dev = hwmon_device_register(&pdev->dev);
273 if (IS_ERR(data->hwmon_dev)) {
274 err = PTR_ERR(data->hwmon_dev);
281 device_remove_file(&pdev->dev,
282 &sensor_dev_attr_temp1_input.dev_attr);
283 device_remove_file(&pdev->dev,
284 &sensor_dev_attr_temp2_input.dev_attr);
285 device_remove_file(&pdev->dev,
286 &sensor_dev_attr_temp3_input.dev_attr);
287 device_remove_file(&pdev->dev,
288 &sensor_dev_attr_temp4_input.dev_attr);
289 device_remove_file(&pdev->dev, &dev_attr_name);
293 static void k8temp_remove(struct pci_dev *pdev)
295 struct k8temp_data *data = pci_get_drvdata(pdev);
297 hwmon_device_unregister(data->hwmon_dev);
298 device_remove_file(&pdev->dev,
299 &sensor_dev_attr_temp1_input.dev_attr);
300 device_remove_file(&pdev->dev,
301 &sensor_dev_attr_temp2_input.dev_attr);
302 device_remove_file(&pdev->dev,
303 &sensor_dev_attr_temp3_input.dev_attr);
304 device_remove_file(&pdev->dev,
305 &sensor_dev_attr_temp4_input.dev_attr);
306 device_remove_file(&pdev->dev, &dev_attr_name);
309 static struct pci_driver k8temp_driver = {
311 .id_table = k8temp_ids,
312 .probe = k8temp_probe,
313 .remove = k8temp_remove,
316 module_pci_driver(k8temp_driver);
319 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
320 MODULE_LICENSE("GPL");