1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * intel_soc_dts_iosf.c 4 * Copyright (c) 2015, Intel Corporation. 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/bitops.h> 10 #include <linux/intel_tcc.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/interrupt.h> 14 #include <asm/iosf_mbi.h> 15 #include "intel_soc_dts_iosf.h" 16 17 #define SOC_DTS_OFFSET_ENABLE 0xB0 18 #define SOC_DTS_OFFSET_TEMP 0xB1 19 20 #define SOC_DTS_OFFSET_PTPS 0xB2 21 #define SOC_DTS_OFFSET_PTTS 0xB3 22 #define SOC_DTS_OFFSET_PTTSS 0xB4 23 #define SOC_DTS_OFFSET_PTMC 0x80 24 #define SOC_DTS_TE_AUX0 0xB5 25 #define SOC_DTS_TE_AUX1 0xB6 26 27 #define SOC_DTS_AUX0_ENABLE_BIT BIT(0) 28 #define SOC_DTS_AUX1_ENABLE_BIT BIT(1) 29 #define SOC_DTS_CPU_MODULE0_ENABLE_BIT BIT(16) 30 #define SOC_DTS_CPU_MODULE1_ENABLE_BIT BIT(17) 31 #define SOC_DTS_TE_SCI_ENABLE BIT(9) 32 #define SOC_DTS_TE_SMI_ENABLE BIT(10) 33 #define SOC_DTS_TE_MSI_ENABLE BIT(11) 34 #define SOC_DTS_TE_APICA_ENABLE BIT(14) 35 #define SOC_DTS_PTMC_APIC_DEASSERT_BIT BIT(4) 36 37 /* DTS encoding for TJ MAX temperature */ 38 #define SOC_DTS_TJMAX_ENCODING 0x7F 39 40 /* Mask for two trips in status bits */ 41 #define SOC_DTS_TRIP_MASK 0x03 42 43 static int update_trip_temp(struct intel_soc_dts_sensors *sensors, 44 int thres_index, int temp) 45 { 46 int status; 47 u32 temp_out; 48 u32 out; 49 unsigned long update_ptps; 50 u32 store_ptps; 51 u32 store_ptmc; 52 u32 store_te_out; 53 u32 te_out; 54 u32 int_enable_bit = SOC_DTS_TE_APICA_ENABLE; 55 56 if (sensors->intr_type == INTEL_SOC_DTS_INTERRUPT_MSI) 57 int_enable_bit |= SOC_DTS_TE_MSI_ENABLE; 58 59 temp_out = (sensors->tj_max - temp) / 1000; 60 61 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 62 SOC_DTS_OFFSET_PTPS, &store_ptps); 63 if (status) 64 return status; 65 66 update_ptps = store_ptps; 67 bitmap_set_value8(&update_ptps, temp_out & 0xFF, thres_index * 8); 68 out = update_ptps; 69 70 status = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 71 SOC_DTS_OFFSET_PTPS, out); 72 if (status) 73 return status; 74 75 pr_debug("update_trip_temp PTPS = %x\n", out); 76 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 77 SOC_DTS_OFFSET_PTMC, &out); 78 if (status) 79 goto err_restore_ptps; 80 81 store_ptmc = out; 82 83 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 84 SOC_DTS_TE_AUX0 + thres_index, 85 &te_out); 86 if (status) 87 goto err_restore_ptmc; 88 89 store_te_out = te_out; 90 /* Enable for CPU module 0 and module 1 */ 91 out |= (SOC_DTS_CPU_MODULE0_ENABLE_BIT | 92 SOC_DTS_CPU_MODULE1_ENABLE_BIT); 93 if (temp) { 94 if (thres_index) 95 out |= SOC_DTS_AUX1_ENABLE_BIT; 96 else 97 out |= SOC_DTS_AUX0_ENABLE_BIT; 98 te_out |= int_enable_bit; 99 } else { 100 if (thres_index) 101 out &= ~SOC_DTS_AUX1_ENABLE_BIT; 102 else 103 out &= ~SOC_DTS_AUX0_ENABLE_BIT; 104 te_out &= ~int_enable_bit; 105 } 106 status = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 107 SOC_DTS_OFFSET_PTMC, out); 108 if (status) 109 goto err_restore_te_out; 110 111 status = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 112 SOC_DTS_TE_AUX0 + thres_index, 113 te_out); 114 if (status) 115 goto err_restore_te_out; 116 117 return 0; 118 err_restore_te_out: 119 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 120 SOC_DTS_OFFSET_PTMC, store_te_out); 121 err_restore_ptmc: 122 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 123 SOC_DTS_OFFSET_PTMC, store_ptmc); 124 err_restore_ptps: 125 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 126 SOC_DTS_OFFSET_PTPS, store_ptps); 127 /* Nothing we can do if restore fails */ 128 129 return status; 130 } 131 132 static int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip, 133 int temp) 134 { 135 struct intel_soc_dts_sensor_entry *dts = thermal_zone_device_priv(tzd); 136 struct intel_soc_dts_sensors *sensors = dts->sensors; 137 int status; 138 139 if (temp > sensors->tj_max) 140 return -EINVAL; 141 142 mutex_lock(&sensors->dts_update_lock); 143 status = update_trip_temp(sensors, trip, temp); 144 mutex_unlock(&sensors->dts_update_lock); 145 146 return status; 147 } 148 149 static int sys_get_curr_temp(struct thermal_zone_device *tzd, 150 int *temp) 151 { 152 int status; 153 u32 out; 154 struct intel_soc_dts_sensor_entry *dts = thermal_zone_device_priv(tzd); 155 struct intel_soc_dts_sensors *sensors; 156 unsigned long raw; 157 158 sensors = dts->sensors; 159 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 160 SOC_DTS_OFFSET_TEMP, &out); 161 if (status) 162 return status; 163 164 raw = out; 165 out = bitmap_get_value8(&raw, dts->id * 8) - SOC_DTS_TJMAX_ENCODING; 166 *temp = sensors->tj_max - out * 1000; 167 168 return 0; 169 } 170 171 static const struct thermal_zone_device_ops tzone_ops = { 172 .get_temp = sys_get_curr_temp, 173 .set_trip_temp = sys_set_trip_temp, 174 }; 175 176 static int soc_dts_enable(int id) 177 { 178 u32 out; 179 int ret; 180 181 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 182 SOC_DTS_OFFSET_ENABLE, &out); 183 if (ret) 184 return ret; 185 186 if (!(out & BIT(id))) { 187 out |= BIT(id); 188 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 189 SOC_DTS_OFFSET_ENABLE, out); 190 if (ret) 191 return ret; 192 } 193 194 return ret; 195 } 196 197 static void remove_dts_thermal_zone(struct intel_soc_dts_sensor_entry *dts) 198 { 199 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 200 SOC_DTS_OFFSET_ENABLE, dts->store_status); 201 thermal_zone_device_unregister(dts->tzone); 202 } 203 204 static int add_dts_thermal_zone(int id, struct intel_soc_dts_sensor_entry *dts, 205 struct thermal_trip *trips) 206 { 207 char name[10]; 208 u32 store_ptps; 209 int ret; 210 211 /* Store status to restor on exit */ 212 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 213 SOC_DTS_OFFSET_ENABLE, &dts->store_status); 214 if (ret) 215 goto err_ret; 216 217 dts->id = id; 218 219 /* Check if the writable trip we provide is not used by BIOS */ 220 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 221 SOC_DTS_OFFSET_PTPS, &store_ptps); 222 if (!ret) { 223 int i; 224 225 for (i = 0; i <= 1; i++) { 226 if (store_ptps & (0xFFU << i * 8)) 227 trips[i].flags &= ~THERMAL_TRIP_FLAG_RW_TEMP; 228 } 229 } 230 snprintf(name, sizeof(name), "soc_dts%d", id); 231 dts->tzone = thermal_zone_device_register_with_trips(name, trips, 232 SOC_MAX_DTS_TRIPS, 233 dts, &tzone_ops, 234 NULL, 0, 0); 235 if (IS_ERR(dts->tzone)) { 236 ret = PTR_ERR(dts->tzone); 237 goto err_ret; 238 } 239 ret = thermal_zone_device_enable(dts->tzone); 240 if (ret) 241 goto err_enable; 242 243 ret = soc_dts_enable(id); 244 if (ret) 245 goto err_enable; 246 247 return 0; 248 err_enable: 249 thermal_zone_device_unregister(dts->tzone); 250 err_ret: 251 return ret; 252 } 253 254 void intel_soc_dts_iosf_interrupt_handler(struct intel_soc_dts_sensors *sensors) 255 { 256 u32 sticky_out; 257 int status; 258 u32 ptmc_out; 259 unsigned long flags; 260 261 spin_lock_irqsave(&sensors->intr_notify_lock, flags); 262 263 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 264 SOC_DTS_OFFSET_PTMC, &ptmc_out); 265 ptmc_out |= SOC_DTS_PTMC_APIC_DEASSERT_BIT; 266 status = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 267 SOC_DTS_OFFSET_PTMC, ptmc_out); 268 269 status = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, 270 SOC_DTS_OFFSET_PTTSS, &sticky_out); 271 pr_debug("status %d PTTSS %x\n", status, sticky_out); 272 if (sticky_out & SOC_DTS_TRIP_MASK) { 273 int i; 274 /* reset sticky bit */ 275 status = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, 276 SOC_DTS_OFFSET_PTTSS, sticky_out); 277 spin_unlock_irqrestore(&sensors->intr_notify_lock, flags); 278 279 for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) { 280 pr_debug("TZD update for zone %d\n", i); 281 thermal_zone_device_update(sensors->soc_dts[i].tzone, 282 THERMAL_EVENT_UNSPECIFIED); 283 } 284 } else 285 spin_unlock_irqrestore(&sensors->intr_notify_lock, flags); 286 } 287 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_interrupt_handler); 288 289 static void dts_trips_reset(struct intel_soc_dts_sensors *sensors, int dts_index) 290 { 291 update_trip_temp(sensors, 0, 0); 292 update_trip_temp(sensors, 1, 0); 293 } 294 295 static void set_trip(struct thermal_trip *trip, enum thermal_trip_type type, 296 u8 flags, int temp) 297 { 298 trip->type = type; 299 trip->flags = flags; 300 trip->temperature = temp; 301 } 302 303 struct intel_soc_dts_sensors * 304 intel_soc_dts_iosf_init(enum intel_soc_dts_interrupt_type intr_type, 305 bool critical_trip, int crit_offset) 306 { 307 struct thermal_trip trips[SOC_MAX_DTS_SENSORS][SOC_MAX_DTS_TRIPS] = { 0 }; 308 struct intel_soc_dts_sensors *sensors; 309 int tj_max; 310 int ret; 311 int i; 312 313 if (!iosf_mbi_available()) 314 return ERR_PTR(-ENODEV); 315 316 tj_max = intel_tcc_get_tjmax(-1); 317 if (tj_max < 0) 318 return ERR_PTR(tj_max); 319 320 sensors = kzalloc(sizeof(*sensors), GFP_KERNEL); 321 if (!sensors) 322 return ERR_PTR(-ENOMEM); 323 324 spin_lock_init(&sensors->intr_notify_lock); 325 mutex_init(&sensors->dts_update_lock); 326 sensors->intr_type = intr_type; 327 sensors->tj_max = tj_max * 1000; 328 329 for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) { 330 int temp; 331 332 sensors->soc_dts[i].sensors = sensors; 333 334 set_trip(&trips[i][0], THERMAL_TRIP_PASSIVE, 335 THERMAL_TRIP_FLAG_RW_TEMP, 0); 336 337 ret = update_trip_temp(sensors, 0, 0); 338 if (ret) 339 goto err_reset_trips; 340 341 if (critical_trip) { 342 temp = sensors->tj_max - crit_offset; 343 set_trip(&trips[i][1], THERMAL_TRIP_CRITICAL, 0, temp); 344 } else { 345 set_trip(&trips[i][1], THERMAL_TRIP_PASSIVE, 346 THERMAL_TRIP_FLAG_RW_TEMP, 0); 347 temp = 0; 348 } 349 350 ret = update_trip_temp(sensors, 1, temp); 351 if (ret) 352 goto err_reset_trips; 353 } 354 355 for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) { 356 ret = add_dts_thermal_zone(i, &sensors->soc_dts[i], trips[i]); 357 if (ret) 358 goto err_remove_zone; 359 } 360 361 return sensors; 362 363 err_remove_zone: 364 for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) 365 remove_dts_thermal_zone(&sensors->soc_dts[i]); 366 367 err_reset_trips: 368 for (i = 0; i < SOC_MAX_DTS_SENSORS; i++) 369 dts_trips_reset(sensors, i); 370 371 kfree(sensors); 372 return ERR_PTR(ret); 373 } 374 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_init); 375 376 void intel_soc_dts_iosf_exit(struct intel_soc_dts_sensors *sensors) 377 { 378 int i; 379 380 for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) { 381 remove_dts_thermal_zone(&sensors->soc_dts[i]); 382 dts_trips_reset(sensors, i); 383 } 384 kfree(sensors); 385 } 386 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_exit); 387 388 MODULE_IMPORT_NS(INTEL_TCC); 389 MODULE_LICENSE("GPL v2"); 390 MODULE_DESCRIPTION("SoC DTS driver using side band interface"); 391