2 * x86_pkg_temp_thermal driver
3 * Copyright (c) 2013, Intel Corporation.
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.
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
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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #include <linux/thermal.h>
31 #include <linux/debugfs.h>
32 #include <asm/cpu_device_id.h>
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
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.");
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.
56 #define MAX_NUMBER_OF_TRIPS 2
57 /* Limit number of package temp zones */
58 #define MAX_PKG_TEMP_ZONE_IDS 256
60 struct phy_dev_entry
{
61 struct list_head list
;
66 u32 start_pkg_therm_low
;
67 u32 start_pkg_therm_high
;
68 struct thermal_zone_device
*tzone
;
71 /* List maintaining number of package instances */
72 static LIST_HEAD(phy_dev_list
);
73 static DEFINE_MUTEX(phy_dev_list_mutex
);
75 /* Interrupt to work function schedule queue */
76 static DEFINE_PER_CPU(struct delayed_work
, pkg_temp_thermal_threshold_work
);
78 /* To track if the work is already scheduled on a package */
79 static u8
*pkg_work_scheduled
;
81 /* Spin lock to prevent races with pkg_work_scheduled */
82 static spinlock_t pkg_work_lock
;
83 static u16 max_phy_id
;
85 /* Debug counters to show using debugfs */
86 static struct dentry
*debugfs
;
87 static unsigned int pkg_interrupt_cnt
;
88 static unsigned int pkg_work_cnt
;
90 static int pkg_temp_debugfs_init(void)
94 debugfs
= debugfs_create_dir("pkg_temp_thermal", NULL
);
98 d
= debugfs_create_u32("pkg_thres_interrupt", S_IRUGO
, debugfs
,
99 (u32
*)&pkg_interrupt_cnt
);
103 d
= debugfs_create_u32("pkg_thres_work", S_IRUGO
, debugfs
,
104 (u32
*)&pkg_work_cnt
);
111 debugfs_remove_recursive(debugfs
);
115 static struct phy_dev_entry
116 *pkg_temp_thermal_get_phy_entry(unsigned int cpu
)
118 u16 phys_proc_id
= topology_physical_package_id(cpu
);
119 struct phy_dev_entry
*phy_ptr
;
121 mutex_lock(&phy_dev_list_mutex
);
123 list_for_each_entry(phy_ptr
, &phy_dev_list
, list
)
124 if (phy_ptr
->phys_proc_id
== phys_proc_id
) {
125 mutex_unlock(&phy_dev_list_mutex
);
129 mutex_unlock(&phy_dev_list_mutex
);
135 * tj-max is is interesting because threshold is set relative to this
138 static int get_tj_max(int cpu
, u32
*tj_max
)
144 err
= rdmsr_safe_on_cpu(cpu
, MSR_IA32_TEMPERATURE_TARGET
, &eax
, &edx
);
148 val
= (eax
>> 16) & 0xff;
150 *tj_max
= val
* 1000;
163 static int sys_get_curr_temp(struct thermal_zone_device
*tzd
, unsigned long *temp
)
166 struct phy_dev_entry
*phy_dev_entry
;
168 phy_dev_entry
= tzd
->devdata
;
169 rdmsr_on_cpu(phy_dev_entry
->first_cpu
, MSR_IA32_PACKAGE_THERM_STATUS
,
171 if (eax
& 0x80000000) {
172 *temp
= phy_dev_entry
->tj_max
-
173 ((eax
>> 16) & 0x7f) * 1000;
174 pr_debug("sys_get_curr_temp %ld\n", *temp
);
181 static int sys_get_trip_temp(struct thermal_zone_device
*tzd
,
182 int trip
, unsigned long *temp
)
185 struct phy_dev_entry
*phy_dev_entry
;
187 unsigned long thres_reg_value
;
190 if (trip
>= MAX_NUMBER_OF_TRIPS
)
193 phy_dev_entry
= tzd
->devdata
;
196 mask
= THERM_MASK_THRESHOLD1
;
197 shift
= THERM_SHIFT_THRESHOLD1
;
199 mask
= THERM_MASK_THRESHOLD0
;
200 shift
= THERM_SHIFT_THRESHOLD0
;
203 ret
= rdmsr_on_cpu(phy_dev_entry
->first_cpu
,
204 MSR_IA32_PACKAGE_THERM_INTERRUPT
, &eax
, &edx
);
208 thres_reg_value
= (eax
& mask
) >> shift
;
210 *temp
= phy_dev_entry
->tj_max
- thres_reg_value
* 1000;
213 pr_debug("sys_get_trip_temp %ld\n", *temp
);
218 static int sys_set_trip_temp(struct thermal_zone_device
*tzd
, int trip
,
222 struct phy_dev_entry
*phy_dev_entry
;
223 u32 mask
, shift
, intr
;
226 phy_dev_entry
= tzd
->devdata
;
228 if (trip
>= MAX_NUMBER_OF_TRIPS
|| temp
>= phy_dev_entry
->tj_max
)
231 ret
= rdmsr_on_cpu(phy_dev_entry
->first_cpu
,
232 MSR_IA32_PACKAGE_THERM_INTERRUPT
,
238 mask
= THERM_MASK_THRESHOLD1
;
239 shift
= THERM_SHIFT_THRESHOLD1
;
240 intr
= THERM_INT_THRESHOLD1_ENABLE
;
242 mask
= THERM_MASK_THRESHOLD0
;
243 shift
= THERM_SHIFT_THRESHOLD0
;
244 intr
= THERM_INT_THRESHOLD0_ENABLE
;
248 * When users space sets a trip temperature == 0, which is indication
249 * that, it is no longer interested in receiving notifications.
254 l
|= (phy_dev_entry
->tj_max
- temp
)/1000 << shift
;
258 return wrmsr_on_cpu(phy_dev_entry
->first_cpu
,
259 MSR_IA32_PACKAGE_THERM_INTERRUPT
,
263 static int sys_get_trip_type(struct thermal_zone_device
*thermal
,
264 int trip
, enum thermal_trip_type
*type
)
267 *type
= THERMAL_TRIP_PASSIVE
;
272 /* Thermal zone callback registry */
273 static struct thermal_zone_device_ops tzone_ops
= {
274 .get_temp
= sys_get_curr_temp
,
275 .get_trip_temp
= sys_get_trip_temp
,
276 .get_trip_type
= sys_get_trip_type
,
277 .set_trip_temp
= sys_set_trip_temp
,
280 static bool pkg_temp_thermal_platform_thermal_rate_control(void)
285 /* Enable threshold interrupt on local package/cpu */
286 static inline void enable_pkg_thres_interrupt(void)
291 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
292 /* only enable/disable if it had valid threshold value */
293 thres_0
= (l
& THERM_MASK_THRESHOLD0
) >> THERM_SHIFT_THRESHOLD0
;
294 thres_1
= (l
& THERM_MASK_THRESHOLD1
) >> THERM_SHIFT_THRESHOLD1
;
296 l
|= THERM_INT_THRESHOLD0_ENABLE
;
298 l
|= THERM_INT_THRESHOLD1_ENABLE
;
299 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
302 /* Disable threshold interrupt on local package/cpu */
303 static inline void disable_pkg_thres_interrupt(void)
306 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
, l
, h
);
307 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT
,
308 l
& (~THERM_INT_THRESHOLD0_ENABLE
) &
309 (~THERM_INT_THRESHOLD1_ENABLE
), h
);
312 static void pkg_temp_thermal_threshold_work_fn(struct work_struct
*work
)
315 int cpu
= smp_processor_id();
316 int phy_id
= topology_physical_package_id(cpu
);
317 struct phy_dev_entry
*phdev
= pkg_temp_thermal_get_phy_entry(cpu
);
324 spin_lock_irqsave(&pkg_work_lock
, flags
);
326 if (unlikely(phy_id
> max_phy_id
)) {
327 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
330 pkg_work_scheduled
[phy_id
] = 0;
331 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
333 enable_pkg_thres_interrupt();
334 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS
, msr_val
);
335 if (msr_val
& THERM_LOG_THRESHOLD0
) {
336 wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS
,
337 msr_val
& ~THERM_LOG_THRESHOLD0
);
340 if (msr_val
& THERM_LOG_THRESHOLD1
) {
341 wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS
,
342 msr_val
& ~THERM_LOG_THRESHOLD1
);
346 pr_debug("thermal_zone_device_update\n");
347 thermal_zone_device_update(phdev
->tzone
);
351 static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val
)
354 int cpu
= smp_processor_id();
355 int phy_id
= topology_physical_package_id(cpu
);
358 * When a package is in interrupted state, all CPU's in that package
359 * are in the same interrupt state. So scheduling on any one CPU in
360 * the package is enough and simply return for others.
362 spin_lock_irqsave(&pkg_work_lock
, flags
);
364 if (unlikely(phy_id
> max_phy_id
) || unlikely(!pkg_work_scheduled
) ||
365 pkg_work_scheduled
[phy_id
]) {
366 disable_pkg_thres_interrupt();
367 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
370 pkg_work_scheduled
[phy_id
] = 1;
371 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
373 disable_pkg_thres_interrupt();
374 schedule_delayed_work_on(cpu
,
375 &per_cpu(pkg_temp_thermal_threshold_work
, cpu
),
376 msecs_to_jiffies(notify_delay_ms
));
380 static int find_siblings_cpu(int cpu
)
383 int id
= topology_physical_package_id(cpu
);
385 for_each_online_cpu(i
)
386 if (i
!= cpu
&& topology_physical_package_id(i
) == id
)
392 static int pkg_temp_thermal_device_add(unsigned int cpu
)
396 struct phy_dev_entry
*phy_dev_entry
;
399 u32 eax
, ebx
, ecx
, edx
;
403 cpuid(6, &eax
, &ebx
, &ecx
, &edx
);
404 thres_count
= ebx
& 0x07;
408 if (topology_physical_package_id(cpu
) > MAX_PKG_TEMP_ZONE_IDS
)
411 thres_count
= clamp_val(thres_count
, 0, MAX_NUMBER_OF_TRIPS
);
413 err
= get_tj_max(cpu
, &tj_max
);
417 mutex_lock(&phy_dev_list_mutex
);
419 phy_dev_entry
= kzalloc(sizeof(*phy_dev_entry
), GFP_KERNEL
);
420 if (!phy_dev_entry
) {
425 spin_lock_irqsave(&pkg_work_lock
, flags
);
426 if (topology_physical_package_id(cpu
) > max_phy_id
)
427 max_phy_id
= topology_physical_package_id(cpu
);
428 temp
= krealloc(pkg_work_scheduled
,
429 (max_phy_id
+1) * sizeof(u8
), GFP_ATOMIC
);
431 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
435 pkg_work_scheduled
= temp
;
436 pkg_work_scheduled
[topology_physical_package_id(cpu
)] = 0;
437 spin_unlock_irqrestore(&pkg_work_lock
, flags
);
439 phy_dev_entry
->phys_proc_id
= topology_physical_package_id(cpu
);
440 phy_dev_entry
->first_cpu
= cpu
;
441 phy_dev_entry
->tj_max
= tj_max
;
442 phy_dev_entry
->ref_cnt
= 1;
443 snprintf(buffer
, sizeof(buffer
), "pkg-temp-%d\n",
444 phy_dev_entry
->phys_proc_id
);
445 phy_dev_entry
->tzone
= thermal_zone_device_register(buffer
,
447 (thres_count
== MAX_NUMBER_OF_TRIPS
) ?
449 phy_dev_entry
, &tzone_ops
, NULL
, 0, 0);
450 if (IS_ERR(phy_dev_entry
->tzone
)) {
451 err
= PTR_ERR(phy_dev_entry
->tzone
);
454 /* Store MSR value for package thermal interrupt, to restore at exit */
455 rdmsr_on_cpu(cpu
, MSR_IA32_PACKAGE_THERM_INTERRUPT
,
456 &phy_dev_entry
->start_pkg_therm_low
,
457 &phy_dev_entry
->start_pkg_therm_high
);
459 list_add_tail(&phy_dev_entry
->list
, &phy_dev_list
);
460 pr_debug("pkg_temp_thermal_device_add :phy_id %d cpu %d\n",
461 phy_dev_entry
->phys_proc_id
, cpu
);
463 mutex_unlock(&phy_dev_list_mutex
);
468 kfree(phy_dev_entry
);
470 mutex_unlock(&phy_dev_list_mutex
);
476 static int pkg_temp_thermal_device_remove(unsigned int cpu
)
478 struct phy_dev_entry
*n
;
479 u16 phys_proc_id
= topology_physical_package_id(cpu
);
480 struct phy_dev_entry
*phdev
=
481 pkg_temp_thermal_get_phy_entry(cpu
);
486 mutex_lock(&phy_dev_list_mutex
);
487 /* If we are loosing the first cpu for this package, we need change */
488 if (phdev
->first_cpu
== cpu
) {
489 phdev
->first_cpu
= find_siblings_cpu(cpu
);
490 pr_debug("thermal_device_remove: first cpu switched %d\n",
494 * It is possible that no siblings left as this was the last cpu
495 * going offline. We don't need to worry about this assignment
496 * as the phydev entry will be removed in this case and
497 * thermal zone is removed.
500 pr_debug("thermal_device_remove: pkg: %d cpu %d ref_cnt %d\n",
501 phys_proc_id
, cpu
, phdev
->ref_cnt
);
503 list_for_each_entry_safe(phdev
, n
, &phy_dev_list
, list
) {
504 if (phdev
->phys_proc_id
== phys_proc_id
) {
505 thermal_zone_device_unregister(phdev
->tzone
);
506 list_del(&phdev
->list
);
511 mutex_unlock(&phy_dev_list_mutex
);
516 static int get_core_online(unsigned int cpu
)
518 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
519 struct phy_dev_entry
*phdev
= pkg_temp_thermal_get_phy_entry(cpu
);
521 /* Check if there is already an instance for this package */
523 if (!cpu_has(c
, X86_FEATURE_DTHERM
) ||
524 !cpu_has(c
, X86_FEATURE_PTS
))
526 if (pkg_temp_thermal_device_add(cpu
))
529 mutex_lock(&phy_dev_list_mutex
);
531 pr_debug("get_core_online: cpu %d ref_cnt %d\n",
532 cpu
, phdev
->ref_cnt
);
533 mutex_unlock(&phy_dev_list_mutex
);
535 INIT_DELAYED_WORK(&per_cpu(pkg_temp_thermal_threshold_work
, cpu
),
536 pkg_temp_thermal_threshold_work_fn
);
538 pr_debug("get_core_online: cpu %d successful\n", cpu
);
543 static void put_core_offline(unsigned int cpu
)
545 if (!pkg_temp_thermal_device_remove(cpu
))
546 cancel_delayed_work_sync(
547 &per_cpu(pkg_temp_thermal_threshold_work
, cpu
));
549 pr_debug("put_core_offline: cpu %d\n", cpu
);
552 static int pkg_temp_thermal_cpu_callback(struct notifier_block
*nfb
,
553 unsigned long action
, void *hcpu
)
555 unsigned int cpu
= (unsigned long) hcpu
;
559 case CPU_DOWN_FAILED
:
560 get_core_online(cpu
);
562 case CPU_DOWN_PREPARE
:
563 put_core_offline(cpu
);
569 static struct notifier_block pkg_temp_thermal_notifier __refdata
= {
570 .notifier_call
= pkg_temp_thermal_cpu_callback
,
573 static const struct x86_cpu_id __initconst pkg_temp_thermal_ids
[] = {
574 { X86_VENDOR_INTEL
, X86_FAMILY_ANY
, X86_MODEL_ANY
, X86_FEATURE_PTS
},
577 MODULE_DEVICE_TABLE(x86cpu
, pkg_temp_thermal_ids
);
579 static int __init
pkg_temp_thermal_init(void)
583 if (!x86_match_cpu(pkg_temp_thermal_ids
))
586 spin_lock_init(&pkg_work_lock
);
587 platform_thermal_package_notify
=
588 pkg_temp_thermal_platform_thermal_notify
;
589 platform_thermal_package_rate_control
=
590 pkg_temp_thermal_platform_thermal_rate_control
;
593 for_each_online_cpu(i
)
594 if (get_core_online(i
))
596 register_hotcpu_notifier(&pkg_temp_thermal_notifier
);
599 pkg_temp_debugfs_init(); /* Don't care if fails */
604 for_each_online_cpu(i
)
607 kfree(pkg_work_scheduled
);
608 platform_thermal_package_notify
= NULL
;
609 platform_thermal_package_rate_control
= NULL
;
614 static void __exit
pkg_temp_thermal_exit(void)
616 struct phy_dev_entry
*phdev
, *n
;
620 unregister_hotcpu_notifier(&pkg_temp_thermal_notifier
);
621 mutex_lock(&phy_dev_list_mutex
);
622 list_for_each_entry_safe(phdev
, n
, &phy_dev_list
, list
) {
623 /* Retore old MSR value for package thermal interrupt */
624 wrmsr_on_cpu(phdev
->first_cpu
,
625 MSR_IA32_PACKAGE_THERM_INTERRUPT
,
626 phdev
->start_pkg_therm_low
,
627 phdev
->start_pkg_therm_high
);
628 thermal_zone_device_unregister(phdev
->tzone
);
629 list_del(&phdev
->list
);
632 mutex_unlock(&phy_dev_list_mutex
);
633 platform_thermal_package_notify
= NULL
;
634 platform_thermal_package_rate_control
= NULL
;
635 for_each_online_cpu(i
)
636 cancel_delayed_work_sync(
637 &per_cpu(pkg_temp_thermal_threshold_work
, i
));
640 kfree(pkg_work_scheduled
);
642 debugfs_remove_recursive(debugfs
);
645 module_init(pkg_temp_thermal_init
)
646 module_exit(pkg_temp_thermal_exit
)
648 MODULE_DESCRIPTION("X86 PKG TEMP Thermal Driver");
649 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
650 MODULE_LICENSE("GPL v2");