Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * k8temp.c - Linux kernel module for hardware monitoring |
| 3 | * |
Jean Delvare | 7188cc6 | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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> |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 34 | #include <asm/processor.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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 | |
| 41 | struct k8temp_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 42 | struct device *hwmon_dev; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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 */ |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 51 | u8 swap_core_select; /* meaning of SEL_CORE is inverted */ |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 52 | u32 temp_offset; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static 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 | |
| 104 | static 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 | |
| 113 | static 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 Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 120 | int temp; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 121 | struct k8temp_data *data = k8temp_update_device(dev); |
| 122 | |
Jean Delvare | cd4de21 | 2010-06-20 09:22:32 +0200 | [diff] [blame] | 123 | if (data->swap_core_select && (data->sensorsp & SEL_CORE)) |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 124 | core = core ? 0 : 1; |
| 125 | |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 126 | temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset; |
| 127 | |
| 128 | return sprintf(buf, "%d\n", temp); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* core, place */ |
| 132 | |
| 133 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0); |
| 134 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1); |
| 135 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0); |
| 136 | static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1); |
| 137 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 138 | |
Márton Németh | 3dd3a15 | 2010-01-10 20:52:35 +0100 | [diff] [blame] | 139 | static const struct pci_device_id k8temp_ids[] = { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 140 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) }, |
| 141 | { 0 }, |
| 142 | }; |
| 143 | |
Jean Delvare | b17ebc9 | 2006-08-28 14:41:03 +0200 | [diff] [blame] | 144 | MODULE_DEVICE_TABLE(pci, k8temp_ids); |
| 145 | |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 146 | static int __devinit k8temp_probe(struct pci_dev *pdev, |
| 147 | const struct pci_device_id *id) |
| 148 | { |
| 149 | int err; |
| 150 | u8 scfg; |
| 151 | u32 temp; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 152 | u8 model, stepping; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 153 | struct k8temp_data *data; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 154 | |
| 155 | if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) { |
| 156 | err = -ENOMEM; |
| 157 | goto exit; |
| 158 | } |
| 159 | |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 160 | model = boot_cpu_data.x86_model; |
| 161 | stepping = boot_cpu_data.x86_mask; |
| 162 | |
| 163 | switch (boot_cpu_data.x86) { |
| 164 | case 0xf: |
| 165 | /* feature available since SH-C0, exclude older revisions */ |
| 166 | if (((model == 4) && (stepping == 0)) || |
| 167 | ((model == 5) && (stepping <= 1))) { |
| 168 | err = -ENODEV; |
| 169 | goto exit_free; |
| 170 | } |
| 171 | |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 172 | /* |
| 173 | * AMD NPT family 0fh, i.e. RevF and RevG: |
| 174 | * meaning of SEL_CORE bit is inverted |
| 175 | */ |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 176 | if (model >= 0x40) { |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 177 | data->swap_core_select = 1; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 178 | dev_warn(&pdev->dev, "Temperature readouts might be " |
| 179 | "wrong - check erratum #141\n"); |
| 180 | } |
| 181 | |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 182 | if ((model >= 0x69) && |
Andreas Herrmann | d535bad | 2010-07-09 16:22:47 +0200 | [diff] [blame] | 183 | !(model == 0xc1 || model == 0x6c || model == 0x7c || |
| 184 | model == 0x6b || model == 0x6f || model == 0x7f)) { |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 185 | /* |
Andreas Herrmann | d535bad | 2010-07-09 16:22:47 +0200 | [diff] [blame] | 186 | * RevG desktop CPUs (i.e. no socket S1G1 or |
| 187 | * ASB1 parts) need additional offset, |
| 188 | * otherwise reported temperature is below |
| 189 | * ambient temperature |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 190 | */ |
| 191 | data->temp_offset = 21000; |
| 192 | } |
| 193 | |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 197 | pci_read_config_byte(pdev, REG_TEMP, &scfg); |
| 198 | scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */ |
| 199 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 200 | pci_read_config_byte(pdev, REG_TEMP, &scfg); |
| 201 | |
| 202 | if (scfg & (SEL_PLACE | SEL_CORE)) { |
| 203 | dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n"); |
| 204 | err = -ENODEV; |
| 205 | goto exit_free; |
| 206 | } |
| 207 | |
| 208 | scfg |= (SEL_PLACE | SEL_CORE); |
| 209 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 210 | |
| 211 | /* now we know if we can change core and/or sensor */ |
| 212 | pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp); |
| 213 | |
| 214 | if (data->sensorsp & SEL_PLACE) { |
| 215 | scfg &= ~SEL_CORE; /* Select sensor 1, core0 */ |
| 216 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 217 | pci_read_config_dword(pdev, REG_TEMP, &temp); |
| 218 | scfg |= SEL_CORE; /* prepare for next selection */ |
| 219 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */ |
| 220 | data->sensorsp &= ~SEL_PLACE; |
| 221 | } |
| 222 | |
| 223 | if (data->sensorsp & SEL_CORE) { |
| 224 | scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */ |
| 225 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 226 | pci_read_config_dword(pdev, REG_TEMP, &temp); |
| 227 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */ |
| 228 | data->sensorsp &= ~SEL_CORE; |
| 229 | } |
| 230 | |
| 231 | data->name = "k8temp"; |
| 232 | mutex_init(&data->update_lock); |
| 233 | dev_set_drvdata(&pdev->dev, data); |
| 234 | |
| 235 | /* Register sysfs hooks */ |
| 236 | err = device_create_file(&pdev->dev, |
| 237 | &sensor_dev_attr_temp1_input.dev_attr); |
| 238 | if (err) |
| 239 | goto exit_remove; |
| 240 | |
| 241 | /* sensor can be changed and reports something */ |
| 242 | if (data->sensorsp & SEL_PLACE) { |
| 243 | err = device_create_file(&pdev->dev, |
| 244 | &sensor_dev_attr_temp2_input.dev_attr); |
| 245 | if (err) |
| 246 | goto exit_remove; |
| 247 | } |
| 248 | |
| 249 | /* core can be changed and reports something */ |
| 250 | if (data->sensorsp & SEL_CORE) { |
| 251 | err = device_create_file(&pdev->dev, |
| 252 | &sensor_dev_attr_temp3_input.dev_attr); |
| 253 | if (err) |
| 254 | goto exit_remove; |
Julia Lawall | df149d0 | 2010-08-14 21:08:47 +0200 | [diff] [blame] | 255 | if (data->sensorsp & SEL_PLACE) { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 256 | err = device_create_file(&pdev->dev, |
| 257 | &sensor_dev_attr_temp4_input. |
| 258 | dev_attr); |
| 259 | if (err) |
| 260 | goto exit_remove; |
Julia Lawall | df149d0 | 2010-08-14 21:08:47 +0200 | [diff] [blame] | 261 | } |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | err = device_create_file(&pdev->dev, &dev_attr_name); |
| 265 | if (err) |
| 266 | goto exit_remove; |
| 267 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 268 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 269 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 270 | if (IS_ERR(data->hwmon_dev)) { |
| 271 | err = PTR_ERR(data->hwmon_dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 272 | goto exit_remove; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | |
| 277 | exit_remove: |
| 278 | device_remove_file(&pdev->dev, |
| 279 | &sensor_dev_attr_temp1_input.dev_attr); |
| 280 | device_remove_file(&pdev->dev, |
| 281 | &sensor_dev_attr_temp2_input.dev_attr); |
| 282 | device_remove_file(&pdev->dev, |
| 283 | &sensor_dev_attr_temp3_input.dev_attr); |
| 284 | device_remove_file(&pdev->dev, |
| 285 | &sensor_dev_attr_temp4_input.dev_attr); |
| 286 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 287 | exit_free: |
| 288 | dev_set_drvdata(&pdev->dev, NULL); |
| 289 | kfree(data); |
| 290 | exit: |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | static void __devexit k8temp_remove(struct pci_dev *pdev) |
| 295 | { |
| 296 | struct k8temp_data *data = dev_get_drvdata(&pdev->dev); |
| 297 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 298 | hwmon_device_unregister(data->hwmon_dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 299 | device_remove_file(&pdev->dev, |
| 300 | &sensor_dev_attr_temp1_input.dev_attr); |
| 301 | device_remove_file(&pdev->dev, |
| 302 | &sensor_dev_attr_temp2_input.dev_attr); |
| 303 | device_remove_file(&pdev->dev, |
| 304 | &sensor_dev_attr_temp3_input.dev_attr); |
| 305 | device_remove_file(&pdev->dev, |
| 306 | &sensor_dev_attr_temp4_input.dev_attr); |
| 307 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 308 | dev_set_drvdata(&pdev->dev, NULL); |
| 309 | kfree(data); |
| 310 | } |
| 311 | |
| 312 | static struct pci_driver k8temp_driver = { |
| 313 | .name = "k8temp", |
| 314 | .id_table = k8temp_ids, |
| 315 | .probe = k8temp_probe, |
| 316 | .remove = __devexit_p(k8temp_remove), |
| 317 | }; |
| 318 | |
| 319 | static int __init k8temp_init(void) |
| 320 | { |
| 321 | return pci_register_driver(&k8temp_driver); |
| 322 | } |
| 323 | |
| 324 | static void __exit k8temp_exit(void) |
| 325 | { |
| 326 | pci_unregister_driver(&k8temp_driver); |
| 327 | } |
| 328 | |
Jean Delvare | 7188cc6 | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 329 | MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>"); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 330 | MODULE_DESCRIPTION("AMD K8 core temperature monitor"); |
| 331 | MODULE_LICENSE("GPL"); |
| 332 | |
| 333 | module_init(k8temp_init) |
| 334 | module_exit(k8temp_exit) |