3 * Copyright (c) 2015, 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
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <asm/iosf_mbi.h>
22 #include "intel_soc_dts_iosf.h"
24 #define SOC_DTS_OFFSET_ENABLE 0xB0
25 #define SOC_DTS_OFFSET_TEMP 0xB1
27 #define SOC_DTS_OFFSET_PTPS 0xB2
28 #define SOC_DTS_OFFSET_PTTS 0xB3
29 #define SOC_DTS_OFFSET_PTTSS 0xB4
30 #define SOC_DTS_OFFSET_PTMC 0x80
31 #define SOC_DTS_TE_AUX0 0xB5
32 #define SOC_DTS_TE_AUX1 0xB6
34 #define SOC_DTS_AUX0_ENABLE_BIT BIT(0)
35 #define SOC_DTS_AUX1_ENABLE_BIT BIT(1)
36 #define SOC_DTS_CPU_MODULE0_ENABLE_BIT BIT(16)
37 #define SOC_DTS_CPU_MODULE1_ENABLE_BIT BIT(17)
38 #define SOC_DTS_TE_SCI_ENABLE BIT(9)
39 #define SOC_DTS_TE_SMI_ENABLE BIT(10)
40 #define SOC_DTS_TE_MSI_ENABLE BIT(11)
41 #define SOC_DTS_TE_APICA_ENABLE BIT(14)
42 #define SOC_DTS_PTMC_APIC_DEASSERT_BIT BIT(4)
44 /* DTS encoding for TJ MAX temperature */
45 #define SOC_DTS_TJMAX_ENCODING 0x7F
47 /* Only 2 out of 4 is allowed for OSPM */
48 #define SOC_MAX_DTS_TRIPS 2
50 /* Mask for two trips in status bits */
51 #define SOC_DTS_TRIP_MASK 0x03
54 #define SOC_MAX_DTS_SENSORS 2
56 static int get_tj_max(u32
*tj_max
)
62 err
= rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET
, &eax
, &edx
);
66 val
= (eax
>> 16) & 0xff;
82 static int sys_get_trip_temp(struct thermal_zone_device
*tzd
, int trip
,
87 struct intel_soc_dts_sensor_entry
*dts
;
88 struct intel_soc_dts_sensors
*sensors
;
91 sensors
= dts
->sensors
;
92 mutex_lock(&sensors
->dts_update_lock
);
93 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
94 SOC_DTS_OFFSET_PTPS
, &out
);
95 mutex_unlock(&sensors
->dts_update_lock
);
99 out
= (out
>> (trip
* 8)) & SOC_DTS_TJMAX_ENCODING
;
103 *temp
= sensors
->tj_max
- out
* 1000;
108 static int update_trip_temp(struct intel_soc_dts_sensor_entry
*dts
,
109 int thres_index
, unsigned long temp
,
110 enum thermal_trip_type trip_type
)
119 u32 int_enable_bit
= SOC_DTS_TE_APICA_ENABLE
;
120 struct intel_soc_dts_sensors
*sensors
= dts
->sensors
;
122 if (sensors
->intr_type
== INTEL_SOC_DTS_INTERRUPT_MSI
)
123 int_enable_bit
|= SOC_DTS_TE_MSI_ENABLE
;
125 temp_out
= (sensors
->tj_max
- temp
) / 1000;
127 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
128 SOC_DTS_OFFSET_PTPS
, &store_ptps
);
132 out
= (store_ptps
& ~(0xFF << (thres_index
* 8)));
133 out
|= (temp_out
& 0xFF) << (thres_index
* 8);
134 status
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
135 SOC_DTS_OFFSET_PTPS
, out
);
139 pr_debug("update_trip_temp PTPS = %x\n", out
);
140 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
141 SOC_DTS_OFFSET_PTMC
, &out
);
143 goto err_restore_ptps
;
147 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
148 SOC_DTS_TE_AUX0
+ thres_index
,
151 goto err_restore_ptmc
;
153 store_te_out
= te_out
;
154 /* Enable for CPU module 0 and module 1 */
155 out
|= (SOC_DTS_CPU_MODULE0_ENABLE_BIT
|
156 SOC_DTS_CPU_MODULE1_ENABLE_BIT
);
159 out
|= SOC_DTS_AUX1_ENABLE_BIT
;
161 out
|= SOC_DTS_AUX0_ENABLE_BIT
;
162 te_out
|= int_enable_bit
;
165 out
&= ~SOC_DTS_AUX1_ENABLE_BIT
;
167 out
&= ~SOC_DTS_AUX0_ENABLE_BIT
;
168 te_out
&= ~int_enable_bit
;
170 status
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
171 SOC_DTS_OFFSET_PTMC
, out
);
173 goto err_restore_te_out
;
175 status
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
176 SOC_DTS_TE_AUX0
+ thres_index
,
179 goto err_restore_te_out
;
181 dts
->trip_types
[thres_index
] = trip_type
;
185 iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
186 SOC_DTS_OFFSET_PTMC
, store_te_out
);
188 iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
189 SOC_DTS_OFFSET_PTMC
, store_ptmc
);
191 iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
192 SOC_DTS_OFFSET_PTPS
, store_ptps
);
193 /* Nothing we can do if restore fails */
198 static int sys_set_trip_temp(struct thermal_zone_device
*tzd
, int trip
,
201 struct intel_soc_dts_sensor_entry
*dts
= tzd
->devdata
;
202 struct intel_soc_dts_sensors
*sensors
= dts
->sensors
;
205 if (temp
> sensors
->tj_max
)
208 mutex_lock(&sensors
->dts_update_lock
);
209 status
= update_trip_temp(tzd
->devdata
, trip
, temp
,
210 dts
->trip_types
[trip
]);
211 mutex_unlock(&sensors
->dts_update_lock
);
216 static int sys_get_trip_type(struct thermal_zone_device
*tzd
,
217 int trip
, enum thermal_trip_type
*type
)
219 struct intel_soc_dts_sensor_entry
*dts
;
223 *type
= dts
->trip_types
[trip
];
228 static int sys_get_curr_temp(struct thermal_zone_device
*tzd
,
233 struct intel_soc_dts_sensor_entry
*dts
;
234 struct intel_soc_dts_sensors
*sensors
;
237 sensors
= dts
->sensors
;
238 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
239 SOC_DTS_OFFSET_TEMP
, &out
);
243 out
= (out
& dts
->temp_mask
) >> dts
->temp_shift
;
244 out
-= SOC_DTS_TJMAX_ENCODING
;
245 *temp
= sensors
->tj_max
- out
* 1000;
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
,
257 static int soc_dts_enable(int id
)
262 ret
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
263 SOC_DTS_OFFSET_ENABLE
, &out
);
267 if (!(out
& BIT(id
))) {
269 ret
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
270 SOC_DTS_OFFSET_ENABLE
, out
);
278 static void remove_dts_thermal_zone(struct intel_soc_dts_sensor_entry
*dts
)
281 iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
282 SOC_DTS_OFFSET_ENABLE
, dts
->store_status
);
283 thermal_zone_device_unregister(dts
->tzone
);
287 static int add_dts_thermal_zone(int id
, struct intel_soc_dts_sensor_entry
*dts
,
288 bool notification_support
, int trip_cnt
,
289 int read_only_trip_cnt
)
298 /* Store status to restor on exit */
299 ret
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
300 SOC_DTS_OFFSET_ENABLE
,
306 dts
->temp_mask
= 0x00FF << (id
* 8);
307 dts
->temp_shift
= id
* 8;
308 if (notification_support
) {
309 trip_count
= min(SOC_MAX_DTS_TRIPS
, trip_cnt
);
310 trip_mask
= BIT(trip_count
- read_only_trip_cnt
) - 1;
313 /* Check if the writable trip we provide is not used by BIOS */
314 ret
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
315 SOC_DTS_OFFSET_PTPS
, &store_ptps
);
319 for (i
= 0; i
< trip_count
; ++i
) {
320 if (trip_mask
& BIT(i
))
321 if (store_ptps
& (0xff << (i
* 8)))
322 trip_mask
&= ~BIT(i
);
325 dts
->trip_mask
= trip_mask
;
326 dts
->trip_count
= trip_count
;
327 snprintf(name
, sizeof(name
), "soc_dts%d", id
);
328 dts
->tzone
= thermal_zone_device_register(name
,
333 if (IS_ERR(dts
->tzone
)) {
334 ret
= PTR_ERR(dts
->tzone
);
338 ret
= soc_dts_enable(id
);
344 thermal_zone_device_unregister(dts
->tzone
);
349 int intel_soc_dts_iosf_add_read_only_critical_trip(
350 struct intel_soc_dts_sensors
*sensors
, int critical_offset
)
354 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
) {
355 for (j
= 0; j
< sensors
->soc_dts
[i
].trip_count
; ++j
) {
356 if (!(sensors
->soc_dts
[i
].trip_mask
& BIT(j
))) {
357 return update_trip_temp(&sensors
->soc_dts
[i
], j
,
358 sensors
->tj_max
- critical_offset
,
359 THERMAL_TRIP_CRITICAL
);
366 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_add_read_only_critical_trip
);
368 void intel_soc_dts_iosf_interrupt_handler(struct intel_soc_dts_sensors
*sensors
)
375 spin_lock_irqsave(&sensors
->intr_notify_lock
, flags
);
377 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
378 SOC_DTS_OFFSET_PTMC
, &ptmc_out
);
379 ptmc_out
|= SOC_DTS_PTMC_APIC_DEASSERT_BIT
;
380 status
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
381 SOC_DTS_OFFSET_PTMC
, ptmc_out
);
383 status
= iosf_mbi_read(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_READ
,
384 SOC_DTS_OFFSET_PTTSS
, &sticky_out
);
385 pr_debug("status %d PTTSS %x\n", status
, sticky_out
);
386 if (sticky_out
& SOC_DTS_TRIP_MASK
) {
388 /* reset sticky bit */
389 status
= iosf_mbi_write(BT_MBI_UNIT_PMC
, BT_MBI_BUNIT_WRITE
,
390 SOC_DTS_OFFSET_PTTSS
, sticky_out
);
391 spin_unlock_irqrestore(&sensors
->intr_notify_lock
, flags
);
393 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
) {
394 pr_debug("TZD update for zone %d\n", i
);
395 thermal_zone_device_update(sensors
->soc_dts
[i
].tzone
);
398 spin_unlock_irqrestore(&sensors
->intr_notify_lock
, flags
);
400 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_interrupt_handler
);
402 struct intel_soc_dts_sensors
*intel_soc_dts_iosf_init(
403 enum intel_soc_dts_interrupt_type intr_type
, int trip_count
,
404 int read_only_trip_count
)
406 struct intel_soc_dts_sensors
*sensors
;
412 if (!iosf_mbi_available())
413 return ERR_PTR(-ENODEV
);
415 if (!trip_count
|| read_only_trip_count
> trip_count
)
416 return ERR_PTR(-EINVAL
);
418 if (get_tj_max(&tj_max
))
419 return ERR_PTR(-EINVAL
);
421 sensors
= kzalloc(sizeof(*sensors
), GFP_KERNEL
);
423 return ERR_PTR(-ENOMEM
);
425 spin_lock_init(&sensors
->intr_notify_lock
);
426 mutex_init(&sensors
->dts_update_lock
);
427 sensors
->intr_type
= intr_type
;
428 sensors
->tj_max
= tj_max
;
429 if (intr_type
== INTEL_SOC_DTS_INTERRUPT_NONE
)
430 notification
= false;
433 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
) {
434 sensors
->soc_dts
[i
].sensors
= sensors
;
435 ret
= add_dts_thermal_zone(i
, &sensors
->soc_dts
[i
],
436 notification
, trip_count
,
437 read_only_trip_count
);
442 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
) {
443 ret
= update_trip_temp(&sensors
->soc_dts
[i
], 0, 0,
444 THERMAL_TRIP_PASSIVE
);
446 goto err_remove_zone
;
448 ret
= update_trip_temp(&sensors
->soc_dts
[i
], 1, 0,
449 THERMAL_TRIP_PASSIVE
);
451 goto err_remove_zone
;
456 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
)
457 remove_dts_thermal_zone(&sensors
->soc_dts
[i
]);
463 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_init
);
465 void intel_soc_dts_iosf_exit(struct intel_soc_dts_sensors
*sensors
)
469 for (i
= 0; i
< SOC_MAX_DTS_SENSORS
; ++i
) {
470 update_trip_temp(&sensors
->soc_dts
[i
], 0, 0, 0);
471 update_trip_temp(&sensors
->soc_dts
[i
], 1, 0, 0);
472 remove_dts_thermal_zone(&sensors
->soc_dts
[i
]);
476 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_exit
);
478 MODULE_LICENSE("GPL v2");