blob: 35aac82ee8eb340e726e696a98ca34a156d76607 [file] [log] [blame]
Rudolf Marek29fa06c2006-08-28 14:40:17 +02001/*
2 * k8temp.c - Linux kernel module for hardware monitoring
3 *
Jean Delvare7188cc62006-12-12 18:18:30 +01004 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
Rudolf Marek29fa06c2006-08-28 14:40:17 +02005 *
6 * Inspired from the w83785 and amd756 drivers.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24#include <linux/module.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/pci.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
33#include <linux/mutex.h>
Andreas Herrmannbb9a35f2009-01-15 22:27:46 +010034#include <asm/processor.h>
Rudolf Marek29fa06c2006-08-28 14:40:17 +020035
36#define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
37#define REG_TEMP 0xe4
38#define SEL_PLACE 0x40
39#define SEL_CORE 0x04
40
41struct k8temp_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070042 struct device *hwmon_dev;
Rudolf Marek29fa06c2006-08-28 14:40:17 +020043 struct mutex update_lock;
44 const char *name;
45 char valid; /* zero until following fields are valid */
46 unsigned long last_updated; /* in jiffies */
47
48 /* registers values */
Frans Meulenbroeks93092a62012-01-10 23:01:39 +010049 u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */
Rudolf Marek29fa06c2006-08-28 14:40:17 +020050 u32 temp[2][2]; /* core, place */
Andreas Herrmanna2e066b2009-01-15 22:27:47 +010051 u8 swap_core_select; /* meaning of SEL_CORE is inverted */
Andreas Herrmann76ff08d2009-01-15 22:27:47 +010052 u32 temp_offset;
Rudolf Marek29fa06c2006-08-28 14:40:17 +020053};
54
55static struct k8temp_data *k8temp_update_device(struct device *dev)
56{
57 struct k8temp_data *data = dev_get_drvdata(dev);
58 struct pci_dev *pdev = to_pci_dev(dev);
59 u8 tmp;
60
61 mutex_lock(&data->update_lock);
62
63 if (!data->valid
64 || time_after(jiffies, data->last_updated + HZ)) {
65 pci_read_config_byte(pdev, REG_TEMP, &tmp);
Frans Meulenbroeks93092a62012-01-10 23:01:39 +010066 tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
Rudolf Marek29fa06c2006-08-28 14:40:17 +020067 pci_write_config_byte(pdev, REG_TEMP, tmp);
68 pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
69
70 if (data->sensorsp & SEL_PLACE) {
71 tmp |= SEL_PLACE; /* Select sensor 1, core0 */
72 pci_write_config_byte(pdev, REG_TEMP, tmp);
73 pci_read_config_dword(pdev, REG_TEMP,
74 &data->temp[0][1]);
75 }
76
77 if (data->sensorsp & SEL_CORE) {
78 tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
79 tmp |= SEL_CORE;
80 pci_write_config_byte(pdev, REG_TEMP, tmp);
81 pci_read_config_dword(pdev, REG_TEMP,
82 &data->temp[1][0]);
83
84 if (data->sensorsp & SEL_PLACE) {
Frans Meulenbroeks93092a62012-01-10 23:01:39 +010085 tmp |= SEL_PLACE; /* Select sensor 1, core1 */
Rudolf Marek29fa06c2006-08-28 14:40:17 +020086 pci_write_config_byte(pdev, REG_TEMP, tmp);
87 pci_read_config_dword(pdev, REG_TEMP,
88 &data->temp[1][1]);
89 }
90 }
91
92 data->last_updated = jiffies;
93 data->valid = 1;
94 }
95
96 mutex_unlock(&data->update_lock);
97 return data;
98}
99
100/*
101 * Sysfs stuff
102 */
103
104static ssize_t show_name(struct device *dev, struct device_attribute
105 *devattr, char *buf)
106{
107 struct k8temp_data *data = dev_get_drvdata(dev);
108
109 return sprintf(buf, "%s\n", data->name);
110}
111
112
113static ssize_t show_temp(struct device *dev,
114 struct device_attribute *devattr, char *buf)
115{
116 struct sensor_device_attribute_2 *attr =
117 to_sensor_dev_attr_2(devattr);
118 int core = attr->nr;
119 int place = attr->index;
Andreas Herrmann76ff08d2009-01-15 22:27:47 +0100120 int temp;
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200121 struct k8temp_data *data = k8temp_update_device(dev);
122
Jean Delvarecd4de212010-06-20 09:22:32 +0200123 if (data->swap_core_select && (data->sensorsp & SEL_CORE))
Andreas Herrmanna2e066b2009-01-15 22:27:47 +0100124 core = core ? 0 : 1;
125
Andreas Herrmann76ff08d2009-01-15 22:27:47 +0100126 temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
127
128 return sprintf(buf, "%d\n", temp);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200129}
130
131/* core, place */
132
133static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
134static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
135static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
136static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
137static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
138
Frans Meulenbroeks600151b2012-01-05 19:50:17 +0100139static DEFINE_PCI_DEVICE_TABLE(k8temp_ids) = {
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200140 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
141 { 0 },
142};
143
Jean Delvareb17ebc92006-08-28 14:41:03 +0200144MODULE_DEVICE_TABLE(pci, k8temp_ids);
145
Andreas Herrmanna05e93f2010-08-25 15:42:12 +0200146static int __devinit is_rev_g_desktop(u8 model)
147{
148 u32 brandidx;
149
150 if (model < 0x69)
151 return 0;
152
153 if (model == 0xc1 || model == 0x6c || model == 0x7c)
154 return 0;
155
156 /*
157 * Differentiate between AM2 and ASB1.
158 * See "Constructing the processor Name String" in "Revision
159 * Guide for AMD NPT Family 0Fh Processors" (33610).
160 */
161 brandidx = cpuid_ebx(0x80000001);
162 brandidx = (brandidx >> 9) & 0x1f;
163
164 /* Single core */
165 if ((model == 0x6f || model == 0x7f) &&
166 (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
167 return 0;
168
169 /* Dual core */
170 if (model == 0x6b &&
171 (brandidx == 0xb || brandidx == 0xc))
172 return 0;
173
174 return 1;
175}
176
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200177static int __devinit k8temp_probe(struct pci_dev *pdev,
178 const struct pci_device_id *id)
179{
180 int err;
181 u8 scfg;
182 u32 temp;
Andreas Herrmannbb9a35f2009-01-15 22:27:46 +0100183 u8 model, stepping;
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200184 struct k8temp_data *data;
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200185
Frans Meulenbroeks93092a62012-01-10 23:01:39 +0100186 data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL);
187 if (!data) {
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200188 err = -ENOMEM;
189 goto exit;
190 }
191
Andreas Herrmannbb9a35f2009-01-15 22:27:46 +0100192 model = boot_cpu_data.x86_model;
193 stepping = boot_cpu_data.x86_mask;
194
Andreas Herrmann628b4502010-10-28 20:31:42 +0200195 /* feature available since SH-C0, exclude older revisions */
196 if (((model == 4) && (stepping == 0)) ||
197 ((model == 5) && (stepping <= 1))) {
198 err = -ENODEV;
199 goto exit_free;
Andreas Herrmannbb9a35f2009-01-15 22:27:46 +0100200 }
201
Andreas Herrmann628b4502010-10-28 20:31:42 +0200202 /*
203 * AMD NPT family 0fh, i.e. RevF and RevG:
204 * meaning of SEL_CORE bit is inverted
205 */
206 if (model >= 0x40) {
207 data->swap_core_select = 1;
208 dev_warn(&pdev->dev, "Temperature readouts might be wrong - "
209 "check erratum #141\n");
210 }
211
212 /*
213 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
214 * additional offset, otherwise reported temperature is below
215 * ambient temperature
216 */
217 if (is_rev_g_desktop(model))
218 data->temp_offset = 21000;
219
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200220 pci_read_config_byte(pdev, REG_TEMP, &scfg);
Frans Meulenbroeks93092a62012-01-10 23:01:39 +0100221 scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200222 pci_write_config_byte(pdev, REG_TEMP, scfg);
223 pci_read_config_byte(pdev, REG_TEMP, &scfg);
224
225 if (scfg & (SEL_PLACE | SEL_CORE)) {
226 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
227 err = -ENODEV;
228 goto exit_free;
229 }
230
231 scfg |= (SEL_PLACE | SEL_CORE);
232 pci_write_config_byte(pdev, REG_TEMP, scfg);
233
234 /* now we know if we can change core and/or sensor */
235 pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
236
237 if (data->sensorsp & SEL_PLACE) {
238 scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
239 pci_write_config_byte(pdev, REG_TEMP, scfg);
240 pci_read_config_dword(pdev, REG_TEMP, &temp);
241 scfg |= SEL_CORE; /* prepare for next selection */
Frans Meulenbroeks93092a62012-01-10 23:01:39 +0100242 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200243 data->sensorsp &= ~SEL_PLACE;
244 }
245
246 if (data->sensorsp & SEL_CORE) {
247 scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
248 pci_write_config_byte(pdev, REG_TEMP, scfg);
249 pci_read_config_dword(pdev, REG_TEMP, &temp);
Frans Meulenbroeks93092a62012-01-10 23:01:39 +0100250 if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200251 data->sensorsp &= ~SEL_CORE;
252 }
253
254 data->name = "k8temp";
255 mutex_init(&data->update_lock);
Jean Delvare95de3b22011-05-25 20:43:31 +0200256 pci_set_drvdata(pdev, data);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200257
258 /* Register sysfs hooks */
259 err = device_create_file(&pdev->dev,
260 &sensor_dev_attr_temp1_input.dev_attr);
261 if (err)
262 goto exit_remove;
263
264 /* sensor can be changed and reports something */
265 if (data->sensorsp & SEL_PLACE) {
266 err = device_create_file(&pdev->dev,
267 &sensor_dev_attr_temp2_input.dev_attr);
268 if (err)
269 goto exit_remove;
270 }
271
272 /* core can be changed and reports something */
273 if (data->sensorsp & SEL_CORE) {
274 err = device_create_file(&pdev->dev,
275 &sensor_dev_attr_temp3_input.dev_attr);
276 if (err)
277 goto exit_remove;
Julia Lawalldf149d02010-08-14 21:08:47 +0200278 if (data->sensorsp & SEL_PLACE) {
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200279 err = device_create_file(&pdev->dev,
280 &sensor_dev_attr_temp4_input.
281 dev_attr);
282 if (err)
283 goto exit_remove;
Julia Lawalldf149d02010-08-14 21:08:47 +0200284 }
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200285 }
286
287 err = device_create_file(&pdev->dev, &dev_attr_name);
288 if (err)
289 goto exit_remove;
290
Tony Jones1beeffe2007-08-20 13:46:20 -0700291 data->hwmon_dev = hwmon_device_register(&pdev->dev);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200292
Tony Jones1beeffe2007-08-20 13:46:20 -0700293 if (IS_ERR(data->hwmon_dev)) {
294 err = PTR_ERR(data->hwmon_dev);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200295 goto exit_remove;
296 }
297
298 return 0;
299
300exit_remove:
301 device_remove_file(&pdev->dev,
302 &sensor_dev_attr_temp1_input.dev_attr);
303 device_remove_file(&pdev->dev,
304 &sensor_dev_attr_temp2_input.dev_attr);
305 device_remove_file(&pdev->dev,
306 &sensor_dev_attr_temp3_input.dev_attr);
307 device_remove_file(&pdev->dev,
308 &sensor_dev_attr_temp4_input.dev_attr);
309 device_remove_file(&pdev->dev, &dev_attr_name);
310exit_free:
Jean Delvare95de3b22011-05-25 20:43:31 +0200311 pci_set_drvdata(pdev, NULL);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200312 kfree(data);
313exit:
314 return err;
315}
316
317static void __devexit k8temp_remove(struct pci_dev *pdev)
318{
Jean Delvare95de3b22011-05-25 20:43:31 +0200319 struct k8temp_data *data = pci_get_drvdata(pdev);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200320
Tony Jones1beeffe2007-08-20 13:46:20 -0700321 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200322 device_remove_file(&pdev->dev,
323 &sensor_dev_attr_temp1_input.dev_attr);
324 device_remove_file(&pdev->dev,
325 &sensor_dev_attr_temp2_input.dev_attr);
326 device_remove_file(&pdev->dev,
327 &sensor_dev_attr_temp3_input.dev_attr);
328 device_remove_file(&pdev->dev,
329 &sensor_dev_attr_temp4_input.dev_attr);
330 device_remove_file(&pdev->dev, &dev_attr_name);
Jean Delvare95de3b22011-05-25 20:43:31 +0200331 pci_set_drvdata(pdev, NULL);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200332 kfree(data);
333}
334
335static struct pci_driver k8temp_driver = {
336 .name = "k8temp",
337 .id_table = k8temp_ids,
338 .probe = k8temp_probe,
339 .remove = __devexit_p(k8temp_remove),
340};
341
Axel Linf71f5a52012-04-02 21:25:46 -0400342module_pci_driver(k8temp_driver);
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200343
Jean Delvare7188cc62006-12-12 18:18:30 +0100344MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
Rudolf Marek29fa06c2006-08-28 14:40:17 +0200345MODULE_DESCRIPTION("AMD K8 core temperature monitor");
346MODULE_LICENSE("GPL");