Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * k8temp.c - Linux kernel module for hardware monitoring |
| 4 | * |
Jean Delvare | 7188cc6 | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 5 | * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 6 | * |
| 7 | * Inspired from the w83785 and amd756 drivers. |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 13 | #include <linux/pci.h> |
| 14 | #include <linux/hwmon.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/mutex.h> |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 17 | #include <asm/processor.h> |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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 | |
| 24 | struct k8temp_data { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 25 | struct mutex update_lock; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 26 | |
| 27 | /* registers values */ |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 28 | u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */ |
Andreas Herrmann | a2e066b | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 29 | u8 swap_core_select; /* meaning of SEL_CORE is inverted */ |
Andreas Herrmann | 76ff08d | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 30 | u32 temp_offset; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
Jingoo Han | cd9bb05 | 2013-12-03 07:10:29 +0000 | [diff] [blame] | 33 | static const struct pci_device_id k8temp_ids[] = { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 34 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) }, |
| 35 | { 0 }, |
| 36 | }; |
Jean Delvare | b17ebc9 | 2006-08-28 14:41:03 +0200 | [diff] [blame] | 37 | MODULE_DEVICE_TABLE(pci, k8temp_ids); |
| 38 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 39 | static int is_rev_g_desktop(u8 model) |
Andreas Herrmann | a05e93f | 2010-08-25 15:42:12 +0200 | [diff] [blame] | 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 | |
Robert Karszniewicz | 3b07a70 | 2019-07-21 14:00:51 +0200 | [diff] [blame] | 70 | static umode_t |
| 71 | k8temp_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 | |
| 85 | static int |
| 86 | k8temp_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 | |
| 116 | static const struct hwmon_ops k8temp_ops = { |
| 117 | .is_visible = k8temp_is_visible, |
| 118 | .read = k8temp_read, |
| 119 | }; |
| 120 | |
| 121 | static const struct hwmon_channel_info *k8temp_info[] = { |
| 122 | HWMON_CHANNEL_INFO(temp, |
| 123 | HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT), |
| 124 | NULL |
| 125 | }; |
| 126 | |
| 127 | static const struct hwmon_chip_info k8temp_chip_info = { |
| 128 | .ops = &k8temp_ops, |
| 129 | .info = k8temp_info, |
| 130 | }; |
| 131 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 132 | static int k8temp_probe(struct pci_dev *pdev, |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 133 | const struct pci_device_id *id) |
| 134 | { |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 135 | u8 scfg; |
| 136 | u32 temp; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 137 | u8 model, stepping; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 138 | struct k8temp_data *data; |
Robert Karszniewicz | 3b07a70 | 2019-07-21 14:00:51 +0200 | [diff] [blame] | 139 | struct device *hwmon_dev; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 140 | |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 141 | data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL); |
| 142 | if (!data) |
| 143 | return -ENOMEM; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 144 | |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 145 | model = boot_cpu_data.x86_model; |
Jia Zhang | b399151 | 2018-01-01 09:52:10 +0800 | [diff] [blame] | 146 | stepping = boot_cpu_data.x86_stepping; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 147 | |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 148 | /* feature available since SH-C0, exclude older revisions */ |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 149 | if ((model == 4 && stepping == 0) || |
| 150 | (model == 5 && stepping <= 1)) |
| 151 | return -ENODEV; |
Andreas Herrmann | bb9a35f | 2009-01-15 22:27:46 +0100 | [diff] [blame] | 152 | |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 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; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 159 | dev_warn(&pdev->dev, |
| 160 | "Temperature readouts might be wrong - check erratum #141\n"); |
Andreas Herrmann | 628b450 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 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 | |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 171 | pci_read_config_byte(pdev, REG_TEMP, &scfg); |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 172 | scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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"); |
Guenter Roeck | a0d44cb | 2012-06-02 12:04:06 -0700 | [diff] [blame] | 178 | return -ENODEV; |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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 */ |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 192 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 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); |
Frans Meulenbroeks | 93092a6 | 2012-01-10 23:01:39 +0100 | [diff] [blame] | 200 | if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */ |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 201 | data->sensorsp &= ~SEL_CORE; |
| 202 | } |
| 203 | |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 204 | mutex_init(&data->update_lock); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 205 | |
Robert Karszniewicz | 3b07a70 | 2019-07-21 14:00:51 +0200 | [diff] [blame] | 206 | hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, |
| 207 | "k8temp", |
| 208 | data, |
| 209 | &k8temp_chip_info, |
| 210 | NULL); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 211 | |
Robert Karszniewicz | 3b07a70 | 2019-07-21 14:00:51 +0200 | [diff] [blame] | 212 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static struct pci_driver k8temp_driver = { |
| 216 | .name = "k8temp", |
| 217 | .id_table = k8temp_ids, |
| 218 | .probe = k8temp_probe, |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 219 | }; |
| 220 | |
Axel Lin | f71f5a5 | 2012-04-02 21:25:46 -0400 | [diff] [blame] | 221 | module_pci_driver(k8temp_driver); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 222 | |
Jean Delvare | 7188cc6 | 2006-12-12 18:18:30 +0100 | [diff] [blame] | 223 | MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>"); |
Rudolf Marek | 29fa06c | 2006-08-28 14:40:17 +0200 | [diff] [blame] | 224 | MODULE_DESCRIPTION("AMD K8 core temperature monitor"); |
| 225 | MODULE_LICENSE("GPL"); |