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