Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * x86_pkg_temp_thermal driver |
| 3 | * Copyright (c) 2013, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc. |
| 16 | * |
| 17 | */ |
| 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/param.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/cpu.h> |
| 27 | #include <linux/smp.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/pm.h> |
| 30 | #include <linux/thermal.h> |
| 31 | #include <linux/debugfs.h> |
| 32 | #include <asm/cpu_device_id.h> |
| 33 | #include <asm/mce.h> |
| 34 | |
| 35 | /* |
| 36 | * Rate control delay: Idea is to introduce denounce effect |
| 37 | * This should be long enough to avoid reduce events, when |
| 38 | * threshold is set to a temperature, which is constantly |
| 39 | * violated, but at the short enough to take any action. |
| 40 | * The action can be remove threshold or change it to next |
| 41 | * interesting setting. Based on experiments, in around |
| 42 | * every 5 seconds under load will give us a significant |
| 43 | * temperature change. |
| 44 | */ |
| 45 | #define PKG_TEMP_THERMAL_NOTIFY_DELAY 5000 |
| 46 | static int notify_delay_ms = PKG_TEMP_THERMAL_NOTIFY_DELAY; |
| 47 | module_param(notify_delay_ms, int, 0644); |
| 48 | MODULE_PARM_DESC(notify_delay_ms, |
| 49 | "User space notification delay in milli seconds."); |
| 50 | |
| 51 | /* Number of trip points in thermal zone. Currently it can't |
| 52 | * be more than 2. MSR can allow setting and getting notifications |
| 53 | * for only 2 thresholds. This define enforces this, if there |
| 54 | * is some wrong values returned by cpuid for number of thresholds. |
| 55 | */ |
| 56 | #define MAX_NUMBER_OF_TRIPS 2 |
Srinivas Pandruvada | 94e791f | 2013-07-15 15:38:52 -0700 | [diff] [blame] | 57 | /* Limit number of package temp zones */ |
| 58 | #define MAX_PKG_TEMP_ZONE_IDS 256 |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 59 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 60 | struct pkg_device { |
| 61 | struct list_head list; |
| 62 | u16 phys_proc_id; |
| 63 | u16 cpu; |
Thomas Gleixner | 64ca738 | 2016-11-22 17:57:12 +0000 | [diff] [blame^] | 64 | bool work_scheduled; |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 65 | u32 tj_max; |
| 66 | u32 msr_pkg_therm_low; |
| 67 | u32 msr_pkg_therm_high; |
| 68 | struct thermal_zone_device *tzone; |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 69 | struct cpumask cpumask; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Javi Merino | 4abe602 | 2015-03-03 15:30:50 +0000 | [diff] [blame] | 72 | static struct thermal_zone_params pkg_temp_tz_params = { |
Jean Delvare | 7978688 | 2014-03-02 15:33:35 +0100 | [diff] [blame] | 73 | .no_hwmon = true, |
| 74 | }; |
| 75 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 76 | /* List maintaining number of package instances */ |
| 77 | static LIST_HEAD(phy_dev_list); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 78 | /* Serializes interrupt notification, work and hotplug */ |
| 79 | static DEFINE_SPINLOCK(pkg_temp_lock); |
| 80 | /* Protects zone operation in the work function against hotplug removal */ |
| 81 | static DEFINE_MUTEX(thermal_zone_mutex); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 82 | |
| 83 | /* Interrupt to work function schedule queue */ |
| 84 | static DEFINE_PER_CPU(struct delayed_work, pkg_temp_thermal_threshold_work); |
| 85 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 86 | /* Debug counters to show using debugfs */ |
| 87 | static struct dentry *debugfs; |
| 88 | static unsigned int pkg_interrupt_cnt; |
| 89 | static unsigned int pkg_work_cnt; |
| 90 | |
| 91 | static int pkg_temp_debugfs_init(void) |
| 92 | { |
| 93 | struct dentry *d; |
| 94 | |
| 95 | debugfs = debugfs_create_dir("pkg_temp_thermal", NULL); |
| 96 | if (!debugfs) |
| 97 | return -ENOENT; |
| 98 | |
| 99 | d = debugfs_create_u32("pkg_thres_interrupt", S_IRUGO, debugfs, |
| 100 | (u32 *)&pkg_interrupt_cnt); |
| 101 | if (!d) |
| 102 | goto err_out; |
| 103 | |
| 104 | d = debugfs_create_u32("pkg_thres_work", S_IRUGO, debugfs, |
| 105 | (u32 *)&pkg_work_cnt); |
| 106 | if (!d) |
| 107 | goto err_out; |
| 108 | |
| 109 | return 0; |
| 110 | |
| 111 | err_out: |
| 112 | debugfs_remove_recursive(debugfs); |
| 113 | return -ENOENT; |
| 114 | } |
| 115 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 116 | /* |
| 117 | * Protection: |
| 118 | * |
| 119 | * - cpu hotplug: Read serialized by cpu hotplug lock |
| 120 | * Write must hold pkg_temp_lock |
| 121 | * |
| 122 | * - Other callsites: Must hold pkg_temp_lock |
| 123 | */ |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 124 | static struct pkg_device *pkg_temp_thermal_get_dev(unsigned int cpu) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 125 | { |
| 126 | u16 phys_proc_id = topology_physical_package_id(cpu); |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 127 | struct pkg_device *pkgdev; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 128 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 129 | list_for_each_entry(pkgdev, &phy_dev_list, list) { |
| 130 | if (pkgdev->phys_proc_id == phys_proc_id) |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 131 | return pkgdev; |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 132 | } |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * tj-max is is interesting because threshold is set relative to this |
| 138 | * temperature. |
| 139 | */ |
| 140 | static int get_tj_max(int cpu, u32 *tj_max) |
| 141 | { |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 142 | u32 eax, edx, val; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 143 | int err; |
| 144 | |
| 145 | err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); |
| 146 | if (err) |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 147 | return err; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 148 | |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 149 | val = (eax >> 16) & 0xff; |
| 150 | *tj_max = val * 1000; |
| 151 | |
| 152 | return val ? 0 : -EINVAL; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 155 | static int sys_get_curr_temp(struct thermal_zone_device *tzd, int *temp) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 156 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 157 | struct pkg_device *pkgdev = tzd->devdata; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 158 | u32 eax, edx; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 159 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 160 | rdmsr_on_cpu(pkgdev->cpu, MSR_IA32_PACKAGE_THERM_STATUS, &eax, &edx); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 161 | if (eax & 0x80000000) { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 162 | *temp = pkgdev->tj_max - ((eax >> 16) & 0x7f) * 1000; |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 163 | pr_debug("sys_get_curr_temp %d\n", *temp); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | } |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | static int sys_get_trip_temp(struct thermal_zone_device *tzd, |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 170 | int trip, int *temp) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 171 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 172 | struct pkg_device *pkgdev = tzd->devdata; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 173 | unsigned long thres_reg_value; |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 174 | u32 mask, shift, eax, edx; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 175 | int ret; |
| 176 | |
| 177 | if (trip >= MAX_NUMBER_OF_TRIPS) |
| 178 | return -EINVAL; |
| 179 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 180 | if (trip) { |
| 181 | mask = THERM_MASK_THRESHOLD1; |
| 182 | shift = THERM_SHIFT_THRESHOLD1; |
| 183 | } else { |
| 184 | mask = THERM_MASK_THRESHOLD0; |
| 185 | shift = THERM_SHIFT_THRESHOLD0; |
| 186 | } |
| 187 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 188 | ret = rdmsr_on_cpu(pkgdev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, |
Thomas Gleixner | b6badbe | 2016-11-22 17:57:07 +0000 | [diff] [blame] | 189 | &eax, &edx); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 190 | if (ret < 0) |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 191 | return ret; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 192 | |
| 193 | thres_reg_value = (eax & mask) >> shift; |
| 194 | if (thres_reg_value) |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 195 | *temp = pkgdev->tj_max - thres_reg_value * 1000; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 196 | else |
| 197 | *temp = 0; |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 198 | pr_debug("sys_get_trip_temp %d\n", *temp); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 203 | static int |
| 204 | sys_set_trip_temp(struct thermal_zone_device *tzd, int trip, int temp) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 205 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 206 | struct pkg_device *pkgdev = tzd->devdata; |
| 207 | u32 l, h, mask, shift, intr; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 208 | int ret; |
| 209 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 210 | if (trip >= MAX_NUMBER_OF_TRIPS || temp >= pkgdev->tj_max) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 213 | ret = rdmsr_on_cpu(pkgdev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, |
Thomas Gleixner | b6badbe | 2016-11-22 17:57:07 +0000 | [diff] [blame] | 214 | &l, &h); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 215 | if (ret < 0) |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 216 | return ret; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 217 | |
| 218 | if (trip) { |
| 219 | mask = THERM_MASK_THRESHOLD1; |
| 220 | shift = THERM_SHIFT_THRESHOLD1; |
| 221 | intr = THERM_INT_THRESHOLD1_ENABLE; |
| 222 | } else { |
| 223 | mask = THERM_MASK_THRESHOLD0; |
| 224 | shift = THERM_SHIFT_THRESHOLD0; |
| 225 | intr = THERM_INT_THRESHOLD0_ENABLE; |
| 226 | } |
| 227 | l &= ~mask; |
| 228 | /* |
| 229 | * When users space sets a trip temperature == 0, which is indication |
| 230 | * that, it is no longer interested in receiving notifications. |
| 231 | */ |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 232 | if (!temp) { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 233 | l &= ~intr; |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 234 | } else { |
| 235 | l |= (pkgdev->tj_max - temp)/1000 << shift; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 236 | l |= intr; |
| 237 | } |
| 238 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 239 | return wrmsr_on_cpu(pkgdev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 242 | static int sys_get_trip_type(struct thermal_zone_device *thermal, int trip, |
| 243 | enum thermal_trip_type *type) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 244 | { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 245 | *type = THERMAL_TRIP_PASSIVE; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* Thermal zone callback registry */ |
| 250 | static struct thermal_zone_device_ops tzone_ops = { |
| 251 | .get_temp = sys_get_curr_temp, |
| 252 | .get_trip_temp = sys_get_trip_temp, |
| 253 | .get_trip_type = sys_get_trip_type, |
| 254 | .set_trip_temp = sys_set_trip_temp, |
| 255 | }; |
| 256 | |
Thomas Gleixner | 09a674c | 2016-11-22 17:57:06 +0000 | [diff] [blame] | 257 | static bool pkg_thermal_rate_control(void) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 258 | { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /* Enable threshold interrupt on local package/cpu */ |
| 263 | static inline void enable_pkg_thres_interrupt(void) |
| 264 | { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 265 | u8 thres_0, thres_1; |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 266 | u32 l, h; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 267 | |
| 268 | rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
| 269 | /* only enable/disable if it had valid threshold value */ |
| 270 | thres_0 = (l & THERM_MASK_THRESHOLD0) >> THERM_SHIFT_THRESHOLD0; |
| 271 | thres_1 = (l & THERM_MASK_THRESHOLD1) >> THERM_SHIFT_THRESHOLD1; |
| 272 | if (thres_0) |
| 273 | l |= THERM_INT_THRESHOLD0_ENABLE; |
| 274 | if (thres_1) |
| 275 | l |= THERM_INT_THRESHOLD1_ENABLE; |
| 276 | wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
| 277 | } |
| 278 | |
| 279 | /* Disable threshold interrupt on local package/cpu */ |
| 280 | static inline void disable_pkg_thres_interrupt(void) |
| 281 | { |
| 282 | u32 l, h; |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 283 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 284 | rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 285 | |
| 286 | l &= ~(THERM_INT_THRESHOLD0_ENABLE | THERM_INT_THRESHOLD1_ENABLE); |
| 287 | wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work) |
| 291 | { |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 292 | struct thermal_zone_device *tzone = NULL; |
Thomas Gleixner | 64ca738 | 2016-11-22 17:57:12 +0000 | [diff] [blame^] | 293 | int cpu = smp_processor_id(); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 294 | struct pkg_device *pkgdev; |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 295 | u64 msr_val, wr_val; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 296 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 297 | mutex_lock(&thermal_zone_mutex); |
| 298 | spin_lock_irq(&pkg_temp_lock); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 299 | ++pkg_work_cnt; |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 300 | |
| 301 | pkgdev = pkg_temp_thermal_get_dev(cpu); |
| 302 | if (!pkgdev) { |
| 303 | spin_unlock_irq(&pkg_temp_lock); |
| 304 | mutex_unlock(&thermal_zone_mutex); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 305 | return; |
| 306 | } |
Thomas Gleixner | 64ca738 | 2016-11-22 17:57:12 +0000 | [diff] [blame^] | 307 | pkgdev->work_scheduled = false; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 308 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 309 | rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val); |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 310 | wr_val = msr_val & ~(THERM_LOG_THRESHOLD0 | THERM_LOG_THRESHOLD1); |
| 311 | if (wr_val != msr_val) { |
| 312 | wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS, wr_val); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 313 | tzone = pkgdev->tzone; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 314 | } |
Thomas Gleixner | 768bd13 | 2016-11-22 17:57:04 +0000 | [diff] [blame] | 315 | |
| 316 | enable_pkg_thres_interrupt(); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 317 | spin_unlock_irq(&pkg_temp_lock); |
Thomas Gleixner | 768bd13 | 2016-11-22 17:57:04 +0000 | [diff] [blame] | 318 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 319 | /* |
| 320 | * If tzone is not NULL, then thermal_zone_mutex will prevent the |
| 321 | * concurrent removal in the cpu offline callback. |
| 322 | */ |
| 323 | if (tzone) |
| 324 | thermal_zone_device_update(tzone, THERMAL_EVENT_UNSPECIFIED); |
| 325 | |
| 326 | mutex_unlock(&thermal_zone_mutex); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 329 | static int pkg_thermal_notify(u64 msr_val) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 330 | { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 331 | int cpu = smp_processor_id(); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 332 | struct pkg_device *pkgdev; |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 333 | unsigned long flags; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 334 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 335 | spin_lock_irqsave(&pkg_temp_lock, flags); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 336 | ++pkg_interrupt_cnt; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 337 | |
| 338 | disable_pkg_thres_interrupt(); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 339 | |
| 340 | /* Work is per package, so scheduling it once is enough. */ |
| 341 | pkgdev = pkg_temp_thermal_get_dev(cpu); |
Thomas Gleixner | 64ca738 | 2016-11-22 17:57:12 +0000 | [diff] [blame^] | 342 | if (pkgdev && !pkgdev->work_scheduled) { |
| 343 | pkgdev->work_scheduled = true; |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 344 | schedule_delayed_work_on(cpu, |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 345 | &per_cpu(pkg_temp_thermal_threshold_work, cpu), |
| 346 | msecs_to_jiffies(notify_delay_ms)); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | spin_unlock_irqrestore(&pkg_temp_lock, flags); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 353 | static int pkg_temp_thermal_device_add(unsigned int cpu) |
| 354 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 355 | u32 tj_max, eax, ebx, ecx, edx; |
| 356 | struct pkg_device *pkgdev; |
| 357 | int thres_count, err; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 358 | |
| 359 | cpuid(6, &eax, &ebx, &ecx, &edx); |
| 360 | thres_count = ebx & 0x07; |
| 361 | if (!thres_count) |
| 362 | return -ENODEV; |
| 363 | |
Srinivas Pandruvada | 94e791f | 2013-07-15 15:38:52 -0700 | [diff] [blame] | 364 | if (topology_physical_package_id(cpu) > MAX_PKG_TEMP_ZONE_IDS) |
| 365 | return -ENODEV; |
| 366 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 367 | thres_count = clamp_val(thres_count, 0, MAX_NUMBER_OF_TRIPS); |
| 368 | |
| 369 | err = get_tj_max(cpu, &tj_max); |
| 370 | if (err) |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 371 | return err; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 372 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 373 | pkgdev = kzalloc(sizeof(*pkgdev), GFP_KERNEL); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 374 | if (!pkgdev) |
| 375 | return -ENOMEM; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 376 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 377 | pkgdev->phys_proc_id = topology_physical_package_id(cpu); |
| 378 | pkgdev->cpu = cpu; |
| 379 | pkgdev->tj_max = tj_max; |
| 380 | pkgdev->tzone = thermal_zone_device_register("x86_pkg_temp", |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 381 | thres_count, |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 382 | (thres_count == MAX_NUMBER_OF_TRIPS) ? 0x03 : 0x01, |
| 383 | pkgdev, &tzone_ops, &pkg_temp_tz_params, 0, 0); |
| 384 | if (IS_ERR(pkgdev->tzone)) { |
| 385 | err = PTR_ERR(pkgdev->tzone); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 386 | kfree(pkgdev); |
| 387 | return err; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 388 | } |
| 389 | /* Store MSR value for package thermal interrupt, to restore at exit */ |
| 390 | rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 391 | &pkgdev->msr_pkg_therm_low, |
| 392 | &pkgdev->msr_pkg_therm_high); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 393 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 394 | cpumask_set_cpu(cpu, &pkgdev->cpumask); |
| 395 | spin_lock_irq(&pkg_temp_lock); |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 396 | list_add_tail(&pkgdev->list, &phy_dev_list); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 397 | spin_unlock_irq(&pkg_temp_lock); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 398 | return 0; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 401 | static void put_core_offline(unsigned int cpu) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 402 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 403 | struct pkg_device *pkgdev = pkg_temp_thermal_get_dev(cpu); |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 404 | bool lastcpu; |
Thomas Gleixner | b6badbe | 2016-11-22 17:57:07 +0000 | [diff] [blame] | 405 | int target; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 406 | |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 407 | if (!pkgdev) |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 408 | return; |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 409 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 410 | target = cpumask_any_but(&pkgdev->cpumask, cpu); |
| 411 | cpumask_clear_cpu(cpu, &pkgdev->cpumask); |
| 412 | lastcpu = target >= nr_cpu_ids; |
| 413 | /* |
| 414 | * Remove the sysfs files, if this is the last cpu in the package |
| 415 | * before doing further cleanups. |
| 416 | */ |
| 417 | if (lastcpu) { |
| 418 | struct thermal_zone_device *tzone = pkgdev->tzone; |
Thomas Gleixner | 21a3d3d | 2016-11-22 17:57:06 +0000 | [diff] [blame] | 419 | |
Thomas Gleixner | 89baa56 | 2016-11-22 17:57:05 +0000 | [diff] [blame] | 420 | /* |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 421 | * We must protect against a work function calling |
| 422 | * thermal_zone_update, after/while unregister. We null out |
| 423 | * the pointer under the zone mutex, so the worker function |
| 424 | * won't try to call. |
Thomas Gleixner | 89baa56 | 2016-11-22 17:57:05 +0000 | [diff] [blame] | 425 | */ |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 426 | mutex_lock(&thermal_zone_mutex); |
| 427 | pkgdev->tzone = NULL; |
| 428 | mutex_unlock(&thermal_zone_mutex); |
| 429 | |
| 430 | thermal_zone_device_unregister(tzone); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * If this is the last CPU in the package, restore the interrupt |
| 435 | * MSR and remove the package reference from the array. |
| 436 | */ |
| 437 | if (lastcpu) { |
| 438 | /* Protect against work and interrupts */ |
| 439 | spin_lock_irq(&pkg_temp_lock); |
| 440 | list_del(&pkgdev->list); |
| 441 | /* |
| 442 | * After this point nothing touches the MSR anymore. We |
| 443 | * must drop the lock to make the cross cpu call. This goes |
| 444 | * away once we move that code to the hotplug state machine. |
| 445 | */ |
| 446 | spin_unlock_irq(&pkg_temp_lock); |
Thomas Gleixner | 89baa56 | 2016-11-22 17:57:05 +0000 | [diff] [blame] | 447 | wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 448 | pkgdev->msr_pkg_therm_low, |
| 449 | pkgdev->msr_pkg_therm_high); |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 450 | kfree(pkgdev); |
Thomas Gleixner | 89baa56 | 2016-11-22 17:57:05 +0000 | [diff] [blame] | 451 | } |
Thomas Gleixner | b6badbe | 2016-11-22 17:57:07 +0000 | [diff] [blame] | 452 | |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 453 | /* |
| 454 | * Note, this is broken when work was really scheduled on the |
| 455 | * outgoing cpu because this will leave the work_scheduled flag set |
| 456 | * and the thermal interrupts disabled. Will be fixed in the next |
| 457 | * step as there is no way to fix it in a sane way with the per cpu |
| 458 | * work nonsense. |
| 459 | */ |
| 460 | cancel_delayed_work_sync(&per_cpu(pkg_temp_thermal_threshold_work, cpu)); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static int get_core_online(unsigned int cpu) |
| 464 | { |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 465 | struct pkg_device *pkgdev = pkg_temp_thermal_get_dev(cpu); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 466 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 467 | |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 468 | /* Paranoia check */ |
| 469 | if (!cpu_has(c, X86_FEATURE_DTHERM) || !cpu_has(c, X86_FEATURE_PTS)) |
| 470 | return -ENODEV; |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 471 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 472 | INIT_DELAYED_WORK(&per_cpu(pkg_temp_thermal_threshold_work, cpu), |
Thomas Gleixner | 3883a64 | 2016-11-22 17:57:08 +0000 | [diff] [blame] | 473 | pkg_temp_thermal_threshold_work_fn); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 474 | |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 475 | /* If the package exists, nothing to do */ |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 476 | if (pkgdev) { |
| 477 | cpumask_set_cpu(cpu, &pkgdev->cpumask); |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 478 | return 0; |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 479 | } |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 480 | return pkg_temp_thermal_device_add(cpu); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 483 | static int pkg_temp_thermal_cpu_callback(struct notifier_block *nfb, |
| 484 | unsigned long action, void *hcpu) |
| 485 | { |
| 486 | unsigned int cpu = (unsigned long) hcpu; |
| 487 | |
Richard Cochran | 5af897e | 2016-03-18 22:26:09 +0100 | [diff] [blame] | 488 | switch (action & ~CPU_TASKS_FROZEN) { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 489 | case CPU_ONLINE: |
| 490 | case CPU_DOWN_FAILED: |
| 491 | get_core_online(cpu); |
| 492 | break; |
| 493 | case CPU_DOWN_PREPARE: |
| 494 | put_core_offline(cpu); |
| 495 | break; |
| 496 | } |
| 497 | return NOTIFY_OK; |
| 498 | } |
| 499 | |
| 500 | static struct notifier_block pkg_temp_thermal_notifier __refdata = { |
| 501 | .notifier_call = pkg_temp_thermal_cpu_callback, |
| 502 | }; |
| 503 | |
| 504 | static const struct x86_cpu_id __initconst pkg_temp_thermal_ids[] = { |
Srinivas Pandruvada | f3ed0a1 | 2013-07-11 09:50:30 -0700 | [diff] [blame] | 505 | { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_PTS }, |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 506 | {} |
| 507 | }; |
| 508 | MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids); |
| 509 | |
| 510 | static int __init pkg_temp_thermal_init(void) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | if (!x86_match_cpu(pkg_temp_thermal_ids)) |
| 515 | return -ENODEV; |
| 516 | |
Srivatsa S. Bhat | cf0485a | 2014-03-11 02:10:54 +0530 | [diff] [blame] | 517 | cpu_notifier_register_begin(); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 518 | for_each_online_cpu(i) |
| 519 | if (get_core_online(i)) |
| 520 | goto err_ret; |
Srivatsa S. Bhat | cf0485a | 2014-03-11 02:10:54 +0530 | [diff] [blame] | 521 | __register_hotcpu_notifier(&pkg_temp_thermal_notifier); |
| 522 | cpu_notifier_register_done(); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 523 | |
Thomas Gleixner | 09a674c | 2016-11-22 17:57:06 +0000 | [diff] [blame] | 524 | platform_thermal_package_notify = pkg_thermal_notify; |
| 525 | platform_thermal_package_rate_control = pkg_thermal_rate_control; |
| 526 | |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 527 | /* Don't care if it fails */ |
| 528 | pkg_temp_debugfs_init(); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 529 | return 0; |
| 530 | |
| 531 | err_ret: |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 532 | for_each_online_cpu(i) |
| 533 | put_core_offline(i); |
Srivatsa S. Bhat | cf0485a | 2014-03-11 02:10:54 +0530 | [diff] [blame] | 534 | cpu_notifier_register_done(); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 535 | return -ENODEV; |
| 536 | } |
Thomas Gleixner | 8079a4b | 2016-11-22 17:57:09 +0000 | [diff] [blame] | 537 | module_init(pkg_temp_thermal_init) |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 538 | |
| 539 | static void __exit pkg_temp_thermal_exit(void) |
| 540 | { |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 541 | int i; |
| 542 | |
Thomas Gleixner | 09a674c | 2016-11-22 17:57:06 +0000 | [diff] [blame] | 543 | platform_thermal_package_notify = NULL; |
| 544 | platform_thermal_package_rate_control = NULL; |
| 545 | |
Srivatsa S. Bhat | cf0485a | 2014-03-11 02:10:54 +0530 | [diff] [blame] | 546 | cpu_notifier_register_begin(); |
| 547 | __unregister_hotcpu_notifier(&pkg_temp_thermal_notifier); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 548 | for_each_online_cpu(i) |
Thomas Gleixner | ab47bd9 | 2016-11-22 17:57:10 +0000 | [diff] [blame] | 549 | put_core_offline(i); |
Srivatsa S. Bhat | cf0485a | 2014-03-11 02:10:54 +0530 | [diff] [blame] | 550 | cpu_notifier_register_done(); |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 551 | |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 552 | debugfs_remove_recursive(debugfs); |
| 553 | } |
Srinivas Pandruvada | f1a18a1 | 2013-05-17 23:42:02 +0000 | [diff] [blame] | 554 | module_exit(pkg_temp_thermal_exit) |
| 555 | |
| 556 | MODULE_DESCRIPTION("X86 PKG TEMP Thermal Driver"); |
| 557 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 558 | MODULE_LICENSE("GPL v2"); |