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> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/pci.h> |
| 29 | #include <linux/hwmon.h> |
| 30 | #include <linux/hwmon-sysfs.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/mutex.h> |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 33 | #include <asm/processor.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 34 | |
| 35 | #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000) |
| 36 | #define REG_TEMP 0xe4 |
| 37 | #define SEL_PLACE 0x40 |
| 38 | #define SEL_CORE 0x04 |
| 39 | |
| 40 | struct k8temp_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 41 | struct device *hwmon_dev; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 42 | struct mutex update_lock; |
| 43 | const char *name; |
| 44 | char valid; /* zero until following fields are valid */ |
| 45 | unsigned long last_updated; /* in jiffies */ |
| 46 | |
| 47 | /* registers values */ |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 48 | u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 49 | u32 temp[2][2]; /* core, place */ |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 50 | u8 swap_core_select; /* meaning of SEL_CORE is inverted */ |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 51 | u32 temp_offset; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static struct k8temp_data *k8temp_update_device(struct device *dev) |
| 55 | { |
| 56 | struct k8temp_data *data = dev_get_drvdata(dev); |
| 57 | struct pci_dev *pdev = to_pci_dev(dev); |
| 58 | u8 tmp; |
| 59 | |
| 60 | mutex_lock(&data->update_lock); |
| 61 | |
| 62 | if (!data->valid |
| 63 | || time_after(jiffies, data->last_updated + HZ)) { |
| 64 | pci_read_config_byte(pdev, REG_TEMP, &tmp); |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 65 | tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 66 | pci_write_config_byte(pdev, REG_TEMP, tmp); |
| 67 | pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]); |
| 68 | |
| 69 | if (data->sensorsp & SEL_PLACE) { |
| 70 | tmp |= SEL_PLACE; /* Select sensor 1, core0 */ |
| 71 | pci_write_config_byte(pdev, REG_TEMP, tmp); |
| 72 | pci_read_config_dword(pdev, REG_TEMP, |
| 73 | &data->temp[0][1]); |
| 74 | } |
| 75 | |
| 76 | if (data->sensorsp & SEL_CORE) { |
| 77 | tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */ |
| 78 | tmp |= SEL_CORE; |
| 79 | pci_write_config_byte(pdev, REG_TEMP, tmp); |
| 80 | pci_read_config_dword(pdev, REG_TEMP, |
| 81 | &data->temp[1][0]); |
| 82 | |
| 83 | if (data->sensorsp & SEL_PLACE) { |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 84 | tmp |= SEL_PLACE; /* Select sensor 1, core1 */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 85 | pci_write_config_byte(pdev, REG_TEMP, tmp); |
| 86 | pci_read_config_dword(pdev, REG_TEMP, |
| 87 | &data->temp[1][1]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | data->last_updated = jiffies; |
| 92 | data->valid = 1; |
| 93 | } |
| 94 | |
| 95 | mutex_unlock(&data->update_lock); |
| 96 | return data; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Sysfs stuff |
| 101 | */ |
| 102 | |
| 103 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 104 | *devattr, char *buf) |
| 105 | { |
| 106 | struct k8temp_data *data = dev_get_drvdata(dev); |
| 107 | |
| 108 | return sprintf(buf, "%s\n", data->name); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static ssize_t show_temp(struct device *dev, |
| 113 | struct device_attribute *devattr, char *buf) |
| 114 | { |
| 115 | struct sensor_device_attribute_2 *attr = |
| 116 | to_sensor_dev_attr_2(devattr); |
| 117 | int core = attr->nr; |
| 118 | int place = attr->index; |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 119 | int temp; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 120 | struct k8temp_data *data = k8temp_update_device(dev); |
| 121 | |
Jean Delvare | cd4de21 | 2010-06-20 09:22:32 +0200 | [diff] [blame] | 122 | if (data->swap_core_select && (data->sensorsp & SEL_CORE)) |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 123 | core = core ? 0 : 1; |
| 124 | |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 125 | temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset; |
| 126 | |
| 127 | return sprintf(buf, "%d\n", temp); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* core, place */ |
| 131 | |
| 132 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0); |
| 133 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1); |
| 134 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0); |
| 135 | static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1); |
| 136 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 137 | |
Jingoo Han | cd9bb05 | 2013-12-03 07:10:29 +0000 | [diff] [blame] | 138 | static const struct pci_device_id k8temp_ids[] = { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 139 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) }, |
| 140 | { 0 }, |
| 141 | }; |
| 142 | |
Jean Delvare | b17ebc9 | 2006-08-28 14:41:03 +0200 | [diff] [blame] | 143 | MODULE_DEVICE_TABLE(pci, k8temp_ids); |
| 144 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 145 | static int is_rev_g_desktop(u8 model) |
Andreas Herrmann | a05e93f | 2010-08-25 15:42:12 +0200 | [diff] [blame] | 146 | { |
| 147 | u32 brandidx; |
| 148 | |
| 149 | if (model < 0x69) |
| 150 | return 0; |
| 151 | |
| 152 | if (model == 0xc1 || model == 0x6c || model == 0x7c) |
| 153 | return 0; |
| 154 | |
| 155 | /* |
| 156 | * Differentiate between AM2 and ASB1. |
| 157 | * See "Constructing the processor Name String" in "Revision |
| 158 | * Guide for AMD NPT Family 0Fh Processors" (33610). |
| 159 | */ |
| 160 | brandidx = cpuid_ebx(0x80000001); |
| 161 | brandidx = (brandidx >> 9) & 0x1f; |
| 162 | |
| 163 | /* Single core */ |
| 164 | if ((model == 0x6f || model == 0x7f) && |
| 165 | (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc)) |
| 166 | return 0; |
| 167 | |
| 168 | /* Dual core */ |
| 169 | if (model == 0x6b && |
| 170 | (brandidx == 0xb || brandidx == 0xc)) |
| 171 | return 0; |
| 172 | |
| 173 | return 1; |
| 174 | } |
| 175 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 176 | static int k8temp_probe(struct pci_dev *pdev, |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 177 | const struct pci_device_id *id) |
| 178 | { |
| 179 | int err; |
| 180 | u8 scfg; |
| 181 | u32 temp; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 182 | u8 model, stepping; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 183 | struct k8temp_data *data; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 184 | |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 185 | data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL); |
| 186 | if (!data) |
| 187 | return -ENOMEM; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 188 | |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 189 | model = boot_cpu_data.x86_model; |
| 190 | stepping = boot_cpu_data.x86_mask; |
| 191 | |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 192 | /* feature available since SH-C0, exclude older revisions */ |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 193 | if ((model == 4 && stepping == 0) || |
| 194 | (model == 5 && stepping <= 1)) |
| 195 | return -ENODEV; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 196 | |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 197 | /* |
| 198 | * AMD NPT family 0fh, i.e. RevF and RevG: |
| 199 | * meaning of SEL_CORE bit is inverted |
| 200 | */ |
| 201 | if (model >= 0x40) { |
| 202 | data->swap_core_select = 1; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 203 | dev_warn(&pdev->dev, |
| 204 | "Temperature readouts might be wrong - check erratum #141\n"); |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need |
| 209 | * additional offset, otherwise reported temperature is below |
| 210 | * ambient temperature |
| 211 | */ |
| 212 | if (is_rev_g_desktop(model)) |
| 213 | data->temp_offset = 21000; |
| 214 | |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 215 | pci_read_config_byte(pdev, REG_TEMP, &scfg); |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 216 | scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 217 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 218 | pci_read_config_byte(pdev, REG_TEMP, &scfg); |
| 219 | |
| 220 | if (scfg & (SEL_PLACE | SEL_CORE)) { |
| 221 | dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n"); |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 222 | return -ENODEV; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | scfg |= (SEL_PLACE | SEL_CORE); |
| 226 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 227 | |
| 228 | /* now we know if we can change core and/or sensor */ |
| 229 | pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp); |
| 230 | |
| 231 | if (data->sensorsp & SEL_PLACE) { |
| 232 | scfg &= ~SEL_CORE; /* Select sensor 1, core0 */ |
| 233 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 234 | pci_read_config_dword(pdev, REG_TEMP, &temp); |
| 235 | scfg |= SEL_CORE; /* prepare for next selection */ |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 236 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 237 | data->sensorsp &= ~SEL_PLACE; |
| 238 | } |
| 239 | |
| 240 | if (data->sensorsp & SEL_CORE) { |
| 241 | scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */ |
| 242 | pci_write_config_byte(pdev, REG_TEMP, scfg); |
| 243 | pci_read_config_dword(pdev, REG_TEMP, &temp); |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 244 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 245 | data->sensorsp &= ~SEL_CORE; |
| 246 | } |
| 247 | |
| 248 | data->name = "k8temp"; |
| 249 | mutex_init(&data->update_lock); |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 250 | pci_set_drvdata(pdev, data); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 251 | |
| 252 | /* Register sysfs hooks */ |
| 253 | err = device_create_file(&pdev->dev, |
| 254 | &sensor_dev_attr_temp1_input.dev_attr); |
| 255 | if (err) |
| 256 | goto exit_remove; |
| 257 | |
| 258 | /* sensor can be changed and reports something */ |
| 259 | if (data->sensorsp & SEL_PLACE) { |
| 260 | err = device_create_file(&pdev->dev, |
| 261 | &sensor_dev_attr_temp2_input.dev_attr); |
| 262 | if (err) |
| 263 | goto exit_remove; |
| 264 | } |
| 265 | |
| 266 | /* core can be changed and reports something */ |
| 267 | if (data->sensorsp & SEL_CORE) { |
| 268 | err = device_create_file(&pdev->dev, |
| 269 | &sensor_dev_attr_temp3_input.dev_attr); |
| 270 | if (err) |
| 271 | goto exit_remove; |
Julia Lawall | df149d0 | 2010-08-14 21:08:47 +0200 | [diff] [blame] | 272 | if (data->sensorsp & SEL_PLACE) { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 273 | err = device_create_file(&pdev->dev, |
| 274 | &sensor_dev_attr_temp4_input. |
| 275 | dev_attr); |
| 276 | if (err) |
| 277 | goto exit_remove; |
Julia Lawall | df149d0 | 2010-08-14 21:08:47 +0200 | [diff] [blame] | 278 | } |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | err = device_create_file(&pdev->dev, &dev_attr_name); |
| 282 | if (err) |
| 283 | goto exit_remove; |
| 284 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 285 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 286 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 287 | if (IS_ERR(data->hwmon_dev)) { |
| 288 | err = PTR_ERR(data->hwmon_dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 289 | goto exit_remove; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | exit_remove: |
| 295 | device_remove_file(&pdev->dev, |
| 296 | &sensor_dev_attr_temp1_input.dev_attr); |
| 297 | device_remove_file(&pdev->dev, |
| 298 | &sensor_dev_attr_temp2_input.dev_attr); |
| 299 | device_remove_file(&pdev->dev, |
| 300 | &sensor_dev_attr_temp3_input.dev_attr); |
| 301 | device_remove_file(&pdev->dev, |
| 302 | &sensor_dev_attr_temp4_input.dev_attr); |
| 303 | device_remove_file(&pdev->dev, &dev_attr_name); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 307 | static void k8temp_remove(struct pci_dev *pdev) |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 308 | { |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 309 | struct k8temp_data *data = pci_get_drvdata(pdev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 310 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 311 | hwmon_device_unregister(data->hwmon_dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 312 | device_remove_file(&pdev->dev, |
| 313 | &sensor_dev_attr_temp1_input.dev_attr); |
| 314 | device_remove_file(&pdev->dev, |
| 315 | &sensor_dev_attr_temp2_input.dev_attr); |
| 316 | device_remove_file(&pdev->dev, |
| 317 | &sensor_dev_attr_temp3_input.dev_attr); |
| 318 | device_remove_file(&pdev->dev, |
| 319 | &sensor_dev_attr_temp4_input.dev_attr); |
| 320 | device_remove_file(&pdev->dev, &dev_attr_name); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static struct pci_driver k8temp_driver = { |
| 324 | .name = "k8temp", |
| 325 | .id_table = k8temp_ids, |
| 326 | .probe = k8temp_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 327 | .remove = k8temp_remove, |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 328 | }; |
| 329 | |
Axel Lin | f71f5a5 | 2012-04-02 21:25:46 -0400 | [diff] [blame] | 330 | module_pci_driver(k8temp_driver); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 331 | |
Jean Delvare | 7188cc6 | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 332 | MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>"); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 333 | MODULE_DESCRIPTION("AMD K8 core temperature monitor"); |
| 334 | MODULE_LICENSE("GPL"); |