]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/patches/linux-2.6.20-hwmon-coretemp.patch
Fix vnstat directory creation
[people/pmueller/ipfire-2.x.git] / src / patches / linux-2.6.20-hwmon-coretemp.patch
CommitLineData
c138eec8
AF
1Index: linux-2.6.20-rc4/drivers/hwmon/coretemp.c
2===================================================================
3--- /dev/null 1970-01-01 00:00:00.000000000 +0000
4+++ linux-2.6.20-rc4/drivers/hwmon/coretemp.c 2007-01-14 21:49:52.764871760 +0100
5@@ -0,0 +1,472 @@
6+/*
7+ * coretemp.c - Linux kernel module for hardware monitoring
8+ *
9+ * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
10+ *
11+ * Inspired from many hwmon drivers
12+ *
13+ * This program is free software; you can redistribute it and/or modify
14+ * it under the terms of the GNU General Public License as published by
15+ * the Free Software Foundation; either version 2 of the License, or
16+ * (at your option) any later version.
17+ *
18+ * This program is distributed in the hope that it will be useful,
19+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+ * GNU General Public License for more details.
22+ *
23+ * You should have received a copy of the GNU General Public License
24+ * along with this program; if not, write to the Free Software
25+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26+ * 02110-1301 USA.
27+ */
28+
29+#include <linux/module.h>
30+#include <linux/delay.h>
31+#include <linux/init.h>
32+#include <linux/slab.h>
33+#include <linux/jiffies.h>
34+#include <linux/hwmon.h>
35+#include <linux/sysfs.h>
36+#include <linux/hwmon-sysfs.h>
37+#include <linux/err.h>
38+#include <linux/mutex.h>
39+#include <linux/list.h>
40+#include <linux/platform_device.h>
41+#include <linux/cpu.h>
42+#include <asm/msr.h>
43+#include <asm/processor.h>
44+
45+#define DRVNAME "coretemp"
46+
47+/*
48+ Following part ripped from the msr.c. It should be merged with generic MSR
49+ infrastructure (once ready)
50+*/
51+
52+static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
53+{
54+ int err;
55+
56+ err = rdmsr_safe(reg, eax, edx);
57+ if (err)
58+ err = -EIO;
59+ return err;
60+}
61+
62+#ifdef CONFIG_SMP
63+
64+struct msr_command {
65+ int cpu;
66+ int err;
67+ u32 reg;
68+ u32 data[2];
69+};
70+
71+static void msr_smp_rdmsr(void *cmd_block)
72+{
73+ struct msr_command *cmd = (struct msr_command *)cmd_block;
74+
75+ if (cmd->cpu == smp_processor_id())
76+ cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
77+}
78+
79+static inline int msr_read(int cpu, u32 reg, u32 * eax, u32 * edx)
80+{
81+ struct msr_command cmd;
82+ int ret;
83+
84+ preempt_disable();
85+ if (cpu == smp_processor_id()) {
86+ ret = rdmsr_eio(reg, eax, edx);
87+ } else {
88+ cmd.cpu = cpu;
89+ cmd.reg = reg;
90+
91+ smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
92+
93+ *eax = cmd.data[0];
94+ *edx = cmd.data[1];
95+
96+ ret = cmd.err;
97+ }
98+ preempt_enable();
99+ return ret;
100+}
101+
102+#else /* ! CONFIG_SMP */
103+
104+static inline int msr_read(int cpu, u32 reg, u32 *eax, u32 *edx)
105+{
106+ return rdmsr_eio(reg, eax, edx);
107+}
108+
109+#endif /* ! CONFIG_SMP */
110+
111+/* cut here */
112+
113+typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_LABEL, SHOW_NAME } SHOW;
114+
115+/*
116+ * Functions declaration
117+ */
118+
119+static struct coretemp_data *coretemp_update_device(struct device *dev);
120+
121+struct coretemp_data {
122+ struct class_device *class_dev;
123+ struct mutex update_lock;
124+ const char *name;
125+ u32 id;
126+ char valid; /* zero until following fields are valid */
127+ unsigned long last_updated; /* in jiffies */
128+ int temp;
129+ int tjmax;
130+ /* registers values */
131+ u32 therm_status;
132+};
133+
134+static struct coretemp_data *coretemp_update_device(struct device *dev);
135+
136+/*
137+ * Sysfs stuff
138+ */
139+
140+static ssize_t show_name(struct device *dev, struct device_attribute
141+ *devattr, char *buf)
142+{
143+ int ret;
144+ struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
145+ struct coretemp_data *data = dev_get_drvdata(dev);
146+
147+ if (attr->index == SHOW_NAME)
148+ ret = sprintf(buf, "%s\n", data->name);
149+ else /* show label */
150+ ret = sprintf(buf, "Core %d\n", data->id);
151+ return ret;
152+}
153+
154+static ssize_t show_alarm(struct device *dev, struct device_attribute
155+ *devattr, char *buf)
156+{
157+ struct coretemp_data *data = coretemp_update_device(dev);
158+ /* read the Out-of-spec log, never clear */
159+ return sprintf(buf, "%d\n", (data->therm_status >> 5) & 1);
160+}
161+
162+static ssize_t show_temp(struct device *dev,
163+ struct device_attribute *devattr, char *buf)
164+{
165+ struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166+ struct coretemp_data *data = coretemp_update_device(dev);
167+ return sprintf(buf, "%d\n",
168+ attr->index ==
169+ SHOW_TEMP ? data->temp : data->tjmax);
170+}
171+
172+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
173+ SHOW_TEMP);
174+static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
175+ SHOW_TJMAX);
176+static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
177+static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
178+static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
179+
180+static struct attribute *coretemp_attributes[] = {
181+ &sensor_dev_attr_name.dev_attr.attr,
182+ &sensor_dev_attr_temp1_label.dev_attr.attr,
183+ &dev_attr_temp1_crit_alarm.attr,
184+ &sensor_dev_attr_temp1_input.dev_attr.attr,
185+ &sensor_dev_attr_temp1_crit.dev_attr.attr,
186+ NULL
187+};
188+
189+static const struct attribute_group coretemp_group = {
190+ .attrs = coretemp_attributes,
191+};
192+
193+static struct coretemp_data *coretemp_update_device(struct device *dev)
194+{
195+ struct coretemp_data *data = dev_get_drvdata(dev);
196+
197+ mutex_lock(&data->update_lock);
198+
199+ if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
200+ u32 eax, edx;
201+
202+ data->valid = 0;
203+ msr_read(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
204+ data->therm_status = eax;
205+
206+ /* update only if data has been valid */
207+ if (eax & 0x80000000) {
208+ data->temp = data->tjmax - (((data->therm_status >> 16)
209+ & 0x7f) * 1000);
210+ data->valid = 1;
211+ }
212+ data->last_updated = jiffies;
213+ }
214+
215+ mutex_unlock(&data->update_lock);
216+ return data;
217+}
218+
219+static int __devinit coretemp_probe(struct platform_device *pdev)
220+{
221+ struct coretemp_data *data;
222+ struct cpuinfo_x86 *c = &(cpu_data)[pdev->id];
223+ int err;
224+ u32 eax, edx;
225+
226+ if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
227+ err = -ENOMEM;
228+ dev_err(&pdev->dev, "Out of memory\n");
229+ goto exit;
230+ }
231+
232+ data->id = pdev->id;
233+ data->name = "coretemp";
234+ mutex_init(&data->update_lock);
235+ /* Tjmax default is 100C */
236+ data->tjmax = 100000;
237+
238+ /* Some processors have Tjmax 85 following magic should detect it
239+ Intel won't disclose the information without signed NDA, but
240+ individuals cannot sign it. Catch(ed) 22.
241+ */
242+
243+ if (((c->x86_model == 0xf) && (c->x86_mask > 3 )) ||
244+ (c->x86_model == 0xe)) {
245+
246+ err = msr_read(data->id, 0xee, &eax, &edx);
247+ if (err) {
248+ dev_warn(&pdev->dev,
249+ "Unable to access MSR 0xEE, Tjmax left at %d\n",
250+ data->tjmax);
251+ } else if (eax & 0x40000000) {
252+ data->tjmax = 85000;
253+ }
254+ }
255+
256+ /* test if we can access the THERM_STATUS MSR */
257+ err = msr_read(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
258+
259+ if (err) {
260+ dev_err(&pdev->dev,
261+ "Unable to access THERM_STATUS MSR, giving up\n");
262+ goto exit_free;
263+ }
264+ platform_set_drvdata(pdev, data);
265+
266+ if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
267+ goto exit_free;
268+
269+ data->class_dev = hwmon_device_register(&pdev->dev);
270+ if (IS_ERR(data->class_dev)) {
271+ err = PTR_ERR(data->class_dev);
272+ dev_err(&pdev->dev, "Class registration failed (%d)\n",
273+ err);
274+ goto exit_class;
275+ }
276+
277+ dev_warn(&pdev->dev, "This driver uses undocumented features of Core "
278+ "CPU (Intel will not disclose the information to "
279+ "individuals). Temperature might be wrong!\n");
280+ return 0;
281+
282+exit_class:
283+ sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
284+exit_free:
285+ kfree(data);
286+exit:
287+ return err;
288+}
289+
290+static int __devexit coretemp_remove(struct platform_device *pdev)
291+{
292+ struct coretemp_data *data = platform_get_drvdata(pdev);
293+
294+ hwmon_device_unregister(data->class_dev);
295+ sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
296+ platform_set_drvdata(pdev, NULL);
297+ kfree(data);
298+ return 0;
299+}
300+
301+static struct platform_driver coretemp_driver = {
302+ .driver = {
303+ .owner = THIS_MODULE,
304+ .name = DRVNAME,
305+ },
306+ .probe = coretemp_probe,
307+ .remove = __devexit_p(coretemp_remove),
308+};
309+
310+struct pdev_entry {
311+ struct list_head list;
312+ struct platform_device *pdev;
313+ unsigned int cpu;
314+};
315+
316+static LIST_HEAD(pdev_list);
317+static DEFINE_MUTEX(pdev_list_mutex);
318+
319+static int __cpuinit coretemp_devices_add(unsigned int cpu)
320+{
321+ int err;
322+ struct platform_device *pdev;
323+ struct pdev_entry *pdev_entry;
324+
325+ pdev = platform_device_alloc(DRVNAME, cpu);
326+ if (!pdev) {
327+ err = -ENOMEM;
328+ printk(KERN_ERR DRVNAME ": Device allocation failed\n");
329+ goto exit;
330+
331+ }
332+
333+ pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
334+
335+ if (!pdev_entry) {
336+ err = -ENOMEM;
337+ goto exit_device_put;
338+ }
339+
340+ err = platform_device_add(pdev);
341+
342+ if (err) {
343+ printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
344+ err);
345+ goto exit_device_free;
346+ }
347+
348+ pdev_entry->pdev = pdev;
349+ pdev_entry->cpu = cpu;
350+ mutex_lock(&pdev_list_mutex);
351+ list_add_tail(&pdev_entry->list, &pdev_list);
352+ mutex_unlock(&pdev_list_mutex);
353+
354+ return 0;
355+
356+exit_device_free:
357+ kfree(pdev_entry);
358+exit_device_put:
359+ platform_device_put(pdev);
360+exit:
361+ return err;
362+}
363+
364+#ifdef CONFIG_HOTPLUG_CPU
365+void coretemp_devices_remove(unsigned int cpu)
366+{
367+ struct pdev_entry *p, *n;
368+ mutex_lock(&pdev_list_mutex);
369+ list_for_each_entry_safe(p, n, &pdev_list, list) {
370+ if (p->cpu == cpu) {
371+ platform_device_unregister(p->pdev);
372+ list_del(&p->list);
373+ kfree(p);
374+ }
375+ }
376+ mutex_unlock(&pdev_list_mutex);
377+}
378+
379+static int coretemp_cpu_callback(struct notifier_block *nfb,
380+ unsigned long action, void *hcpu)
381+{
382+ unsigned int cpu = (unsigned long) hcpu;
383+
384+ switch (action) {
385+ case CPU_ONLINE:
386+ coretemp_devices_add(cpu);
387+ break;
388+ case CPU_DEAD:
389+ coretemp_devices_remove(cpu);
390+ break;
391+ }
392+ return NOTIFY_OK;
393+}
394+
395+static struct notifier_block __cpuinitdata coretemp_cpu_notifier = {
396+ .notifier_call = coretemp_cpu_callback,
397+};
398+#endif /* !CONFIG_HOTPLUG_CPU */
399+
400+static int __init coretemp_init(void)
401+{
402+ int i, err = -ENODEV;
403+ struct pdev_entry *p, *n;
404+
405+ /* quick check if we run Intel */
406+ if (cpu_data[0].x86_vendor != X86_VENDOR_INTEL)
407+ goto exit;
408+
409+ err = platform_driver_register(&coretemp_driver);
410+ if (err)
411+ goto exit;
412+
413+ for_each_online_cpu(i) {
414+ struct cpuinfo_x86 *c = &(cpu_data)[i];
415+
416+ /* check if family 6, models e, f */
417+ if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
418+ !((c->x86_model == 0xe) || (c->x86_model == 0xf))) {
419+
420+ /* supported CPU not found, but report the unknown
421+ family 6 CPU */
422+ if ((c->x86 == 0x6) && (c->x86_model > 0xf))
423+ printk(KERN_WARNING DRVNAME ": Unknown CPU, please"
424+ " report to the lm-sensors@lm-sensors.org\n");
425+ continue;
426+ }
427+
428+ err = coretemp_devices_add(i);
429+ if (err)
430+ goto exit_driver;
431+ }
432+ if (list_empty(&pdev_list)) {
433+ err = -ENODEV;
434+ goto exit_driver_unreg;
435+ }
436+
437+#ifdef CONFIG_HOTPLUG_CPU
438+ register_hotcpu_notifier(&coretemp_cpu_notifier);
439+#endif
440+ return 0;
441+
442+exit_driver:
443+ mutex_lock(&pdev_list_mutex);
444+ list_for_each_entry_safe(p, n, &pdev_list, list) {
445+ platform_device_unregister(p->pdev);
446+ list_del(&p->list);
447+ kfree(p);
448+ }
449+ mutex_unlock(&pdev_list_mutex);
450+exit_driver_unreg:
451+ platform_driver_unregister(&coretemp_driver);
452+exit:
453+ return err;
454+}
455+
456+static void __exit coretemp_exit(void)
457+{
458+ struct pdev_entry *p, *n;
459+#ifdef CONFIG_HOTPLUG_CPU
460+ unregister_hotcpu_notifier(&coretemp_cpu_notifier);
461+#endif
462+ mutex_lock(&pdev_list_mutex);
463+ list_for_each_entry_safe(p, n, &pdev_list, list) {
464+ platform_device_unregister(p->pdev);
465+ list_del(&p->list);
466+ kfree(p);
467+ }
468+ mutex_unlock(&pdev_list_mutex);
469+ platform_driver_unregister(&coretemp_driver);
470+}
471+
472+MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
473+MODULE_DESCRIPTION("Intel Core temperature monitor");
474+MODULE_LICENSE("GPL");
475+
476+module_init(coretemp_init)
477+module_exit(coretemp_exit)
478Index: linux-2.6.20-rc4/drivers/hwmon/Makefile
479===================================================================
480--- linux-2.6.20-rc4.orig/drivers/hwmon/Makefile 2007-01-07 06:45:51.000000000 +0100
481+++ linux-2.6.20-rc4/drivers/hwmon/Makefile 2007-01-14 21:29:41.899868496 +0100
482@@ -21,6 +21,7 @@
483 obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o
484 obj-$(CONFIG_SENSORS_AMS) += ams/
485 obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o
486+obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o
487 obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
488 obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
489 obj-$(CONFIG_SENSORS_FSCHER) += fscher.o
490Index: linux-2.6.20-rc4/drivers/hwmon/Kconfig
491===================================================================
492--- linux-2.6.20-rc4.orig/drivers/hwmon/Kconfig 2007-01-07 06:45:51.000000000 +0100
493+++ linux-2.6.20-rc4/drivers/hwmon/Kconfig 2007-01-14 21:40:33.336991782 +0100
494@@ -156,6 +156,14 @@
495 This driver can also be built as a module. If so, the module
496 will be called atxp1.
497
498+config SENSORS_CORETEMP
499+ tristate "Intel Core (2) Duo/Solo temperature sensor"
500+ depends on HWMON && X86 && EXPERIMENTAL
501+ help
502+ If you say yes here you get support for the temperature
503+ sensor inside your CPU. Supported all are all known variants
504+ of Intel Core family.
505+
506 config SENSORS_DS1621
507 tristate "Dallas Semiconductor DS1621 and DS1625"
508 depends on HWMON && I2C
509--- /dev/null 1970-01-01 00:00:00.000000000 +0000
510+++ linux-2.6.20-rc4/Documentation/hwmon/coretemp 2007-01-14 22:01:58.766244221 +0100
511@@ -0,0 +1,37 @@
512+Kernel driver coretemp
513+======================
514+
515+Supported chips:
516+ * All Intel Core family
517+ Prefix: 'coretemp'
518+ Addresses scanned: CPUID (family 0x6, models 0xe, 0xf)
519+ Datasheet: Intel 64 and IA-32 Architectures Software Developer's Manual
520+ Volume 3A: System Programming Guide
521+
522+Author: Rudolf Marek
523+Contact: Rudolf Marek <r.marek@assembler.cz>
524+
525+Description
526+-----------
527+
528+This driver permits reading temperature sensor embedded inside Intel Core CPU.
529+Temperature is measured in degrees Celsius and measurement resolution is
530+1 degree C. Valid temperatures are from 0 to TjMax degrees C, because
531+the actual temperature is in fact a delta from TjMax.
532+
533+Temperature known as TjMax is the maximum junction temperature of processor.
534+Intel defines this temperature as 85C or 100C. At this temperature, protection
535+mechanism will perform actions to forcibly cool down the processor. Alarm
536+may be raised, if the temperature grows enough (more than TjMax) to trigger
537+the Out-Of-Spec bit. Following table summarizes the exported sysfs files:
538+
539+temp1_input - Core temperature (in milidegrees of Celsius).
540+temp1_crit - Maximum junction temperature (in milidegrees of Celsius).
541+temp1_crit_alarm - Set when Out-of-spec bit is set, never clears.
542+ Correct CPU operation is no longer guaranteed.
543+temp1_label - Contains string with the "Core X", where X is processor
544+ number.
545+
546+The TjMax temperature is set to 85C if undocumented model specific register
547+(UMSR) 0xee has bit 30 set. If not the TjMax is 100C as documented in processor
548+datasheet. Intel will not disclose this information to individuals.
549