Linux Audio

Check our new training course

Loading...
v6.8
  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/pci.h>
 14#include <linux/hwmon.h>
 
 15#include <linux/err.h>
 16#include <linux/mutex.h>
 17#include <asm/processor.h>
 18
 19#define TEMP_FROM_REG(val)	(((((val) >> 16) & 0xff) - 49) * 1000)
 20#define REG_TEMP	0xe4
 21#define SEL_PLACE	0x40
 22#define SEL_CORE	0x04
 23
 24struct k8temp_data {
 
 25	struct mutex update_lock;
 
 
 
 26
 27	/* registers values */
 28	u8 sensorsp;		/* sensor presence bits - SEL_CORE, SEL_PLACE */
 
 29	u8 swap_core_select;    /* meaning of SEL_CORE is inverted */
 30	u32 temp_offset;
 31};
 32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 33static const struct pci_device_id k8temp_ids[] = {
 34	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
 35	{ 0 },
 36};
 
 37MODULE_DEVICE_TABLE(pci, k8temp_ids);
 38
 39static int is_rev_g_desktop(u8 model)
 40{
 41	u32 brandidx;
 42
 43	if (model < 0x69)
 44		return 0;
 45
 46	if (model == 0xc1 || model == 0x6c || model == 0x7c)
 47		return 0;
 48
 49	/*
 50	 * Differentiate between AM2 and ASB1.
 51	 * See "Constructing the processor Name String" in "Revision
 52	 * Guide for AMD NPT Family 0Fh Processors" (33610).
 53	 */
 54	brandidx = cpuid_ebx(0x80000001);
 55	brandidx = (brandidx >> 9) & 0x1f;
 56
 57	/* Single core */
 58	if ((model == 0x6f || model == 0x7f) &&
 59	    (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
 60		return 0;
 61
 62	/* Dual core */
 63	if (model == 0x6b &&
 64	    (brandidx == 0xb || brandidx == 0xc))
 65		return 0;
 66
 67	return 1;
 68}
 69
 70static umode_t
 71k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
 72		  u32 attr, int channel)
 73{
 74	const struct k8temp_data *data = drvdata;
 75
 76	if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
 77		return 0;
 78
 79	if ((channel & 2) && !(data->sensorsp & SEL_CORE))
 80		return 0;
 81
 82	return 0444;
 83}
 84
 85static int
 86k8temp_read(struct device *dev, enum hwmon_sensor_types type,
 87	    u32 attr, int channel, long *val)
 88{
 89	struct k8temp_data *data = dev_get_drvdata(dev);
 90	struct pci_dev *pdev = to_pci_dev(dev->parent);
 91	int core, place;
 92	u32 temp;
 93	u8 tmp;
 94
 95	core = (channel >> 1) & 1;
 96	place = channel & 1;
 97
 98	core ^= data->swap_core_select;
 99
100	mutex_lock(&data->update_lock);
101	pci_read_config_byte(pdev, REG_TEMP, &tmp);
102	tmp &= ~(SEL_PLACE | SEL_CORE);
103	if (core)
104		tmp |= SEL_CORE;
105	if (place)
106		tmp |= SEL_PLACE;
107	pci_write_config_byte(pdev, REG_TEMP, tmp);
108	pci_read_config_dword(pdev, REG_TEMP, &temp);
109	mutex_unlock(&data->update_lock);
110
111	*val = TEMP_FROM_REG(temp) + data->temp_offset;
112
113	return 0;
114}
115
116static const struct hwmon_ops k8temp_ops = {
117	.is_visible = k8temp_is_visible,
118	.read = k8temp_read,
119};
120
121static const struct hwmon_channel_info * const k8temp_info[] = {
122	HWMON_CHANNEL_INFO(temp,
123		HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
124	NULL
125};
126
127static const struct hwmon_chip_info k8temp_chip_info = {
128	.ops = &k8temp_ops,
129	.info = k8temp_info,
130};
131
132static int k8temp_probe(struct pci_dev *pdev,
133				  const struct pci_device_id *id)
134{
 
135	u8 scfg;
136	u32 temp;
137	u8 model, stepping;
138	struct k8temp_data *data;
139	struct device *hwmon_dev;
140
141	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
142	if (!data)
143		return -ENOMEM;
 
144
145	model = boot_cpu_data.x86_model;
146	stepping = boot_cpu_data.x86_stepping;
147
148	/* feature available since SH-C0, exclude older revisions */
149	if ((model == 4 && stepping == 0) ||
150	    (model == 5 && stepping <= 1))
151		return -ENODEV;
 
 
152
153	/*
154	 * AMD NPT family 0fh, i.e. RevF and RevG:
155	 * meaning of SEL_CORE bit is inverted
156	 */
157	if (model >= 0x40) {
158		data->swap_core_select = 1;
159		dev_warn(&pdev->dev,
160			 "Temperature readouts might be wrong - check erratum #141\n");
161	}
162
163	/*
164	 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
165	 * additional offset, otherwise reported temperature is below
166	 * ambient temperature
167	 */
168	if (is_rev_g_desktop(model))
169		data->temp_offset = 21000;
170
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);
175
176	if (scfg & (SEL_PLACE | SEL_CORE)) {
177		dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
178		return -ENODEV;
 
179	}
180
181	scfg |= (SEL_PLACE | SEL_CORE);
182	pci_write_config_byte(pdev, REG_TEMP, scfg);
183
184	/* now we know if we can change core and/or sensor */
185	pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
186
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;
194	}
195
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;
202	}
203
 
204	mutex_init(&data->update_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
206	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
207							 "k8temp",
208							 data,
209							 &k8temp_chip_info,
210							 NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
212	return PTR_ERR_OR_ZERO(hwmon_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213}
214
215static struct pci_driver k8temp_driver = {
216	.name = "k8temp",
217	.id_table = k8temp_ids,
218	.probe = k8temp_probe,
 
219};
220
221module_pci_driver(k8temp_driver);
 
 
 
 
 
 
 
 
222
223MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
224MODULE_DESCRIPTION("AMD K8 core temperature monitor");
225MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 * k8temp.c - Linux kernel module for hardware monitoring
  3 *
  4 * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
  5 *
  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>
 34#include <asm/processor.h>
 35
 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 {
 42	struct device *hwmon_dev;
 43	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 */
 49	u8 sensorsp;		/* sensor presence bits - SEL_CORE & SEL_PLACE */
 50	u32 temp[2][2];		/* core, place */
 51	u8 swap_core_select;    /* meaning of SEL_CORE is inverted */
 52	u32 temp_offset;
 53};
 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);
 66		tmp &= ~(SEL_PLACE | SEL_CORE);		/* Select sensor 0, core0 */
 67		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) {
 85				tmp |= SEL_PLACE;	/* Select sensor 1, core1 */
 86				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;
120	int temp;
121	struct k8temp_data *data = k8temp_update_device(dev);
122
123	if (data->swap_core_select && (data->sensorsp & SEL_CORE))
124		core = core ? 0 : 1;
125
126	temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
127
128	return sprintf(buf, "%d\n", temp);
129}
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
139static const struct pci_device_id k8temp_ids[] = {
140	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
141	{ 0 },
142};
143
144MODULE_DEVICE_TABLE(pci, k8temp_ids);
145
146static 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
177static 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;
183	u8 model, stepping;
184	struct k8temp_data *data;
 
185
186	if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
187		err = -ENOMEM;
188		goto exit;
189	}
190
191	model = boot_cpu_data.x86_model;
192	stepping = boot_cpu_data.x86_mask;
193
194	/* feature available since SH-C0, exclude older revisions */
195	if (((model == 4) && (stepping == 0)) ||
196	    ((model == 5) && (stepping <= 1))) {
197		err = -ENODEV;
198		goto exit_free;
199	}
200
201	/*
202	 * AMD NPT family 0fh, i.e. RevF and RevG:
203	 * meaning of SEL_CORE bit is inverted
204	 */
205	if (model >= 0x40) {
206		data->swap_core_select = 1;
207		dev_warn(&pdev->dev, "Temperature readouts might be wrong - "
208			 "check erratum #141\n");
209	}
210
211	/*
212	 * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
213	 * additional offset, otherwise reported temperature is below
214	 * ambient temperature
215	 */
216	if (is_rev_g_desktop(model))
217		data->temp_offset = 21000;
218
219	pci_read_config_byte(pdev, REG_TEMP, &scfg);
220	scfg &= ~(SEL_PLACE | SEL_CORE);		/* Select sensor 0, core0 */
221	pci_write_config_byte(pdev, REG_TEMP, scfg);
222	pci_read_config_byte(pdev, REG_TEMP, &scfg);
223
224	if (scfg & (SEL_PLACE | SEL_CORE)) {
225		dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
226		err = -ENODEV;
227		goto exit_free;
228	}
229
230	scfg |= (SEL_PLACE | SEL_CORE);
231	pci_write_config_byte(pdev, REG_TEMP, scfg);
232
233	/* now we know if we can change core and/or sensor */
234	pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
235
236	if (data->sensorsp & SEL_PLACE) {
237		scfg &= ~SEL_CORE;	/* Select sensor 1, core0 */
238		pci_write_config_byte(pdev, REG_TEMP, scfg);
239		pci_read_config_dword(pdev, REG_TEMP, &temp);
240		scfg |= SEL_CORE;	/* prepare for next selection */
241		if (!((temp >> 16) & 0xff))	/* if temp is 0 -49C is not likely */
242			data->sensorsp &= ~SEL_PLACE;
243	}
244
245	if (data->sensorsp & SEL_CORE) {
246		scfg &= ~SEL_PLACE;	/* Select sensor 0, core1 */
247		pci_write_config_byte(pdev, REG_TEMP, scfg);
248		pci_read_config_dword(pdev, REG_TEMP, &temp);
249		if (!((temp >> 16) & 0xff))	/* if temp is 0 -49C is not likely */
250			data->sensorsp &= ~SEL_CORE;
251	}
252
253	data->name = "k8temp";
254	mutex_init(&data->update_lock);
255	pci_set_drvdata(pdev, data);
256
257	/* Register sysfs hooks */
258	err = device_create_file(&pdev->dev,
259			   &sensor_dev_attr_temp1_input.dev_attr);
260	if (err)
261		goto exit_remove;
262
263	/* sensor can be changed and reports something */
264	if (data->sensorsp & SEL_PLACE) {
265		err = device_create_file(&pdev->dev,
266				   &sensor_dev_attr_temp2_input.dev_attr);
267		if (err)
268			goto exit_remove;
269	}
270
271	/* core can be changed and reports something */
272	if (data->sensorsp & SEL_CORE) {
273		err = device_create_file(&pdev->dev,
274				   &sensor_dev_attr_temp3_input.dev_attr);
275		if (err)
276			goto exit_remove;
277		if (data->sensorsp & SEL_PLACE) {
278			err = device_create_file(&pdev->dev,
279					   &sensor_dev_attr_temp4_input.
280					   dev_attr);
281			if (err)
282				goto exit_remove;
283		}
284	}
285
286	err = device_create_file(&pdev->dev, &dev_attr_name);
287	if (err)
288		goto exit_remove;
289
290	data->hwmon_dev = hwmon_device_register(&pdev->dev);
291
292	if (IS_ERR(data->hwmon_dev)) {
293		err = PTR_ERR(data->hwmon_dev);
294		goto exit_remove;
295	}
296
297	return 0;
298
299exit_remove:
300	device_remove_file(&pdev->dev,
301			   &sensor_dev_attr_temp1_input.dev_attr);
302	device_remove_file(&pdev->dev,
303			   &sensor_dev_attr_temp2_input.dev_attr);
304	device_remove_file(&pdev->dev,
305			   &sensor_dev_attr_temp3_input.dev_attr);
306	device_remove_file(&pdev->dev,
307			   &sensor_dev_attr_temp4_input.dev_attr);
308	device_remove_file(&pdev->dev, &dev_attr_name);
309exit_free:
310	pci_set_drvdata(pdev, NULL);
311	kfree(data);
312exit:
313	return err;
314}
315
316static void __devexit k8temp_remove(struct pci_dev *pdev)
317{
318	struct k8temp_data *data = pci_get_drvdata(pdev);
319
320	hwmon_device_unregister(data->hwmon_dev);
321	device_remove_file(&pdev->dev,
322			   &sensor_dev_attr_temp1_input.dev_attr);
323	device_remove_file(&pdev->dev,
324			   &sensor_dev_attr_temp2_input.dev_attr);
325	device_remove_file(&pdev->dev,
326			   &sensor_dev_attr_temp3_input.dev_attr);
327	device_remove_file(&pdev->dev,
328			   &sensor_dev_attr_temp4_input.dev_attr);
329	device_remove_file(&pdev->dev, &dev_attr_name);
330	pci_set_drvdata(pdev, NULL);
331	kfree(data);
332}
333
334static struct pci_driver k8temp_driver = {
335	.name = "k8temp",
336	.id_table = k8temp_ids,
337	.probe = k8temp_probe,
338	.remove = __devexit_p(k8temp_remove),
339};
340
341static int __init k8temp_init(void)
342{
343	return pci_register_driver(&k8temp_driver);
344}
345
346static void __exit k8temp_exit(void)
347{
348	pci_unregister_driver(&k8temp_driver);
349}
350
351MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
352MODULE_DESCRIPTION("AMD K8 core temperature monitor");
353MODULE_LICENSE("GPL");
354
355module_init(k8temp_init)
356module_exit(k8temp_exit)