Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * k8temp.c - Linux kernel module for hardware monitoring
4 *
5 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
6 *
7 * Inspired from the w83785 and amd756 drivers.
8 */
9
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>
20
21#define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
22#define REG_TEMP 0xe4
23#define SEL_PLACE 0x40
24#define SEL_CORE 0x04
25
26struct k8temp_data {
27 struct device *hwmon_dev;
28 struct mutex update_lock;
29 const char *name;
30 char valid; /* zero until following fields are valid */
31 unsigned long last_updated; /* in jiffies */
32
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 */
37 u32 temp_offset;
38};
39
40static struct k8temp_data *k8temp_update_device(struct device *dev)
41{
42 struct k8temp_data *data = dev_get_drvdata(dev);
43 struct pci_dev *pdev = to_pci_dev(dev);
44 u8 tmp;
45
46 mutex_lock(&data->update_lock);
47
48 if (!data->valid
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]);
54
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,
59 &data->temp[0][1]);
60 }
61
62 if (data->sensorsp & SEL_CORE) {
63 tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
64 tmp |= SEL_CORE;
65 pci_write_config_byte(pdev, REG_TEMP, tmp);
66 pci_read_config_dword(pdev, REG_TEMP,
67 &data->temp[1][0]);
68
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,
73 &data->temp[1][1]);
74 }
75 }
76
77 data->last_updated = jiffies;
78 data->valid = 1;
79 }
80
81 mutex_unlock(&data->update_lock);
82 return data;
83}
84
85/*
86 * Sysfs stuff
87 */
88
89static ssize_t name_show(struct device *dev, struct device_attribute
90 *devattr, char *buf)
91{
92 struct k8temp_data *data = dev_get_drvdata(dev);
93
94 return sprintf(buf, "%s\n", data->name);
95}
96
97
98static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
99 char *buf)
100{
101 struct sensor_device_attribute_2 *attr =
102 to_sensor_dev_attr_2(devattr);
103 int core = attr->nr;
104 int place = attr->index;
105 int temp;
106 struct k8temp_data *data = k8temp_update_device(dev);
107
108 if (data->swap_core_select && (data->sensorsp & SEL_CORE))
109 core = core ? 0 : 1;
110
111 temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
112
113 return sprintf(buf, "%d\n", temp);
114}
115
116/* core, place */
117
118static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
119static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
120static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
121static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
122static DEVICE_ATTR_RO(name);
123
124static const struct pci_device_id k8temp_ids[] = {
125 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
126 { 0 },
127};
128
129MODULE_DEVICE_TABLE(pci, k8temp_ids);
130
131static int is_rev_g_desktop(u8 model)
132{
133 u32 brandidx;
134
135 if (model < 0x69)
136 return 0;
137
138 if (model == 0xc1 || model == 0x6c || model == 0x7c)
139 return 0;
140
141 /*
142 * Differentiate between AM2 and ASB1.
143 * See "Constructing the processor Name String" in "Revision
144 * Guide for AMD NPT Family 0Fh Processors" (33610).
145 */
146 brandidx = cpuid_ebx(0x80000001);
147 brandidx = (brandidx >> 9) & 0x1f;
148
149 /* Single core */
150 if ((model == 0x6f || model == 0x7f) &&
151 (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
152 return 0;
153
154 /* Dual core */
155 if (model == 0x6b &&
156 (brandidx == 0xb || brandidx == 0xc))
157 return 0;
158
159 return 1;
160}
161
162static int k8temp_probe(struct pci_dev *pdev,
163 const struct pci_device_id *id)
164{
165 int err;
166 u8 scfg;
167 u32 temp;
168 u8 model, stepping;
169 struct k8temp_data *data;
170
171 data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
172 if (!data)
173 return -ENOMEM;
174
175 model = boot_cpu_data.x86_model;
176 stepping = boot_cpu_data.x86_stepping;
177
178 /* feature available since SH-C0, exclude older revisions */
179 if ((model == 4 && stepping == 0) ||
180 (model == 5 && stepping <= 1))
181 return -ENODEV;
182
183 /*
184 * AMD NPT family 0fh, i.e. RevF and RevG:
185 * meaning of SEL_CORE bit is inverted
186 */
187 if (model >= 0x40) {
188 data->swap_core_select = 1;
189 dev_warn(&pdev->dev,
190 "Temperature readouts might be wrong - check erratum #141\n");
191 }
192
193 /*
194 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
195 * additional offset, otherwise reported temperature is below
196 * ambient temperature
197 */
198 if (is_rev_g_desktop(model))
199 data->temp_offset = 21000;
200
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);
205
206 if (scfg & (SEL_PLACE | SEL_CORE)) {
207 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
208 return -ENODEV;
209 }
210
211 scfg |= (SEL_PLACE | SEL_CORE);
212 pci_write_config_byte(pdev, REG_TEMP, scfg);
213
214 /* now we know if we can change core and/or sensor */
215 pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
216
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;
224 }
225
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;
232 }
233
234 data->name = "k8temp";
235 mutex_init(&data->update_lock);
236 pci_set_drvdata(pdev, data);
237
238 /* Register sysfs hooks */
239 err = device_create_file(&pdev->dev,
240 &sensor_dev_attr_temp1_input.dev_attr);
241 if (err)
242 goto exit_remove;
243
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);
248 if (err)
249 goto exit_remove;
250 }
251
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);
256 if (err)
257 goto exit_remove;
258 if (data->sensorsp & SEL_PLACE) {
259 err = device_create_file(&pdev->dev,
260 &sensor_dev_attr_temp4_input.
261 dev_attr);
262 if (err)
263 goto exit_remove;
264 }
265 }
266
267 err = device_create_file(&pdev->dev, &dev_attr_name);
268 if (err)
269 goto exit_remove;
270
271 data->hwmon_dev = hwmon_device_register(&pdev->dev);
272
273 if (IS_ERR(data->hwmon_dev)) {
274 err = PTR_ERR(data->hwmon_dev);
275 goto exit_remove;
276 }
277
278 return 0;
279
280exit_remove:
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);
290 return err;
291}
292
293static void k8temp_remove(struct pci_dev *pdev)
294{
295 struct k8temp_data *data = pci_get_drvdata(pdev);
296
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);
307}
308
309static struct pci_driver k8temp_driver = {
310 .name = "k8temp",
311 .id_table = k8temp_ids,
312 .probe = k8temp_probe,
313 .remove = k8temp_remove,
314};
315
316module_pci_driver(k8temp_driver);
317
318MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
319MODULE_DESCRIPTION("AMD K8 core temperature monitor");
320MODULE_LICENSE("GPL");