1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * processor_thermal_device.c 4 * Copyright (c) 2014, Intel Corporation. 5 */ 6 #include <linux/acpi.h> 7 #include <linux/intel_tcc.h> 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 #include <linux/thermal.h> 12 #include <asm/msr.h> 13 #include "int340x_thermal_zone.h" 14 #include "processor_thermal_device.h" 15 #include "../intel_soc_dts_iosf.h" 16 17 #define DRV_NAME "proc_thermal" 18 19 #define POWER_LIMIT_SHOW(index, suffix) \ 20 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \ 21 struct device_attribute *attr, \ 22 char *buf) \ 23 { \ 24 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \ 25 \ 26 return sprintf(buf, "%lu\n",\ 27 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \ 28 } 29 30 static ssize_t power_floor_status_show(struct device *dev, 31 struct device_attribute *attr, 32 char *buf) 33 { 34 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); 35 int ret; 36 37 ret = proc_thermal_read_power_floor_status(proc_dev); 38 39 return sysfs_emit(buf, "%d\n", ret); 40 } 41 42 static ssize_t power_floor_enable_show(struct device *dev, 43 struct device_attribute *attr, 44 char *buf) 45 { 46 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); 47 bool ret; 48 49 ret = proc_thermal_power_floor_get_state(proc_dev); 50 51 return sysfs_emit(buf, "%d\n", ret); 52 } 53 54 static ssize_t power_floor_enable_store(struct device *dev, 55 struct device_attribute *attr, 56 const char *buf, size_t count) 57 { 58 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); 59 u8 state; 60 int ret; 61 62 if (kstrtou8(buf, 0, &state)) 63 return -EINVAL; 64 65 ret = proc_thermal_power_floor_set_state(proc_dev, !!state); 66 if (ret) 67 return ret; 68 69 return count; 70 } 71 72 POWER_LIMIT_SHOW(0, min_uw) 73 POWER_LIMIT_SHOW(0, max_uw) 74 POWER_LIMIT_SHOW(0, step_uw) 75 POWER_LIMIT_SHOW(0, tmin_us) 76 POWER_LIMIT_SHOW(0, tmax_us) 77 78 POWER_LIMIT_SHOW(1, min_uw) 79 POWER_LIMIT_SHOW(1, max_uw) 80 POWER_LIMIT_SHOW(1, step_uw) 81 POWER_LIMIT_SHOW(1, tmin_us) 82 POWER_LIMIT_SHOW(1, tmax_us) 83 84 static DEVICE_ATTR_RO(power_limit_0_min_uw); 85 static DEVICE_ATTR_RO(power_limit_0_max_uw); 86 static DEVICE_ATTR_RO(power_limit_0_step_uw); 87 static DEVICE_ATTR_RO(power_limit_0_tmin_us); 88 static DEVICE_ATTR_RO(power_limit_0_tmax_us); 89 90 static DEVICE_ATTR_RO(power_limit_1_min_uw); 91 static DEVICE_ATTR_RO(power_limit_1_max_uw); 92 static DEVICE_ATTR_RO(power_limit_1_step_uw); 93 static DEVICE_ATTR_RO(power_limit_1_tmin_us); 94 static DEVICE_ATTR_RO(power_limit_1_tmax_us); 95 96 static DEVICE_ATTR_RO(power_floor_status); 97 static DEVICE_ATTR_RW(power_floor_enable); 98 99 static struct attribute *power_limit_attrs[] = { 100 &dev_attr_power_limit_0_min_uw.attr, 101 &dev_attr_power_limit_1_min_uw.attr, 102 &dev_attr_power_limit_0_max_uw.attr, 103 &dev_attr_power_limit_1_max_uw.attr, 104 &dev_attr_power_limit_0_step_uw.attr, 105 &dev_attr_power_limit_1_step_uw.attr, 106 &dev_attr_power_limit_0_tmin_us.attr, 107 &dev_attr_power_limit_1_tmin_us.attr, 108 &dev_attr_power_limit_0_tmax_us.attr, 109 &dev_attr_power_limit_1_tmax_us.attr, 110 &dev_attr_power_floor_status.attr, 111 &dev_attr_power_floor_enable.attr, 112 NULL 113 }; 114 115 static umode_t power_limit_attr_visible(struct kobject *kobj, struct attribute *attr, int unused) 116 { 117 struct device *dev = kobj_to_dev(kobj); 118 struct proc_thermal_device *proc_dev; 119 120 if (attr != &dev_attr_power_floor_status.attr && attr != &dev_attr_power_floor_enable.attr) 121 return attr->mode; 122 123 proc_dev = dev_get_drvdata(dev); 124 if (!proc_dev || !(proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR)) 125 return 0; 126 127 return attr->mode; 128 } 129 130 static const struct attribute_group power_limit_attribute_group = { 131 .attrs = power_limit_attrs, 132 .name = "power_limits", 133 .is_visible = power_limit_attr_visible, 134 }; 135 136 static ssize_t tcc_offset_degree_celsius_show(struct device *dev, 137 struct device_attribute *attr, 138 char *buf) 139 { 140 int offset; 141 142 offset = intel_tcc_get_offset(-1); 143 if (offset < 0) 144 return offset; 145 146 return sprintf(buf, "%d\n", offset); 147 } 148 149 static ssize_t tcc_offset_degree_celsius_store(struct device *dev, 150 struct device_attribute *attr, const char *buf, 151 size_t count) 152 { 153 unsigned int tcc; 154 u64 val; 155 int err; 156 157 err = rdmsrq_safe(MSR_PLATFORM_INFO, &val); 158 if (err) 159 return err; 160 161 if (!(val & BIT(30))) 162 return -EACCES; 163 164 if (kstrtouint(buf, 0, &tcc)) 165 return -EINVAL; 166 167 err = intel_tcc_set_offset(-1, tcc); 168 if (err) 169 return err; 170 171 return count; 172 } 173 174 static DEVICE_ATTR_RW(tcc_offset_degree_celsius); 175 176 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone, 177 int *temp) 178 { 179 int cpu; 180 int curr_temp, ret; 181 182 *temp = 0; 183 184 for_each_online_cpu(cpu) { 185 ret = intel_tcc_get_temp(cpu, &curr_temp, false); 186 if (ret < 0) 187 return ret; 188 if (!*temp || curr_temp > *temp) 189 *temp = curr_temp; 190 } 191 192 *temp *= 1000; 193 194 return 0; 195 } 196 197 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv) 198 { 199 int i; 200 acpi_status status; 201 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 202 union acpi_object *elements, *ppcc; 203 union acpi_object *p; 204 int ret = 0; 205 206 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC", 207 NULL, &buf); 208 if (ACPI_FAILURE(status)) 209 return -ENODEV; 210 211 p = buf.pointer; 212 if (!p || (p->type != ACPI_TYPE_PACKAGE)) { 213 dev_err(proc_priv->dev, "Invalid PPCC data\n"); 214 ret = -EFAULT; 215 goto free_buffer; 216 } 217 218 if (!p->package.count) { 219 dev_err(proc_priv->dev, "Invalid PPCC package size\n"); 220 ret = -EFAULT; 221 goto free_buffer; 222 } 223 224 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) { 225 elements = &(p->package.elements[i+1]); 226 if (elements->type != ACPI_TYPE_PACKAGE || 227 elements->package.count != 6) { 228 ret = -EFAULT; 229 goto free_buffer; 230 } 231 ppcc = elements->package.elements; 232 proc_priv->power_limits[i].index = ppcc[0].integer.value; 233 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value; 234 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value; 235 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value; 236 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value; 237 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value; 238 } 239 240 free_buffer: 241 kfree(buf.pointer); 242 243 return ret; 244 } 245 246 #define PROC_POWER_CAPABILITY_CHANGED 0x83 247 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data) 248 { 249 struct proc_thermal_device *proc_priv = data; 250 251 if (!proc_priv) 252 return; 253 254 switch (event) { 255 case PROC_POWER_CAPABILITY_CHANGED: 256 proc_thermal_read_ppcc(proc_priv); 257 int340x_thermal_zone_device_update(proc_priv->int340x_zone, 258 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED); 259 break; 260 default: 261 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event); 262 break; 263 } 264 } 265 266 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv) 267 { 268 struct acpi_device *adev; 269 acpi_status status; 270 unsigned long long tmp; 271 int (*get_temp) (struct thermal_zone_device *, int *) = NULL; 272 int ret; 273 274 adev = ACPI_COMPANION(dev); 275 if (!adev) 276 return -ENODEV; 277 278 proc_priv->dev = dev; 279 proc_priv->adev = adev; 280 281 ret = proc_thermal_read_ppcc(proc_priv); 282 if (ret) 283 return ret; 284 285 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp); 286 if (ACPI_FAILURE(status)) { 287 /* there is no _TMP method, add local method */ 288 if (intel_tcc_get_tjmax(-1) > 0) 289 get_temp = proc_thermal_get_zone_temp; 290 } 291 292 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, get_temp); 293 if (IS_ERR(proc_priv->int340x_zone)) { 294 return PTR_ERR(proc_priv->int340x_zone); 295 } else 296 ret = 0; 297 298 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 299 proc_thermal_notify, 300 (void *)proc_priv); 301 if (ret) 302 goto remove_zone; 303 304 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr); 305 if (ret) 306 goto remove_notify; 307 308 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group); 309 if (ret) { 310 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr); 311 goto remove_notify; 312 } 313 314 return 0; 315 316 remove_notify: 317 acpi_remove_notify_handler(adev->handle, 318 ACPI_DEVICE_NOTIFY, proc_thermal_notify); 319 remove_zone: 320 int340x_thermal_zone_remove(proc_priv->int340x_zone); 321 322 return ret; 323 } 324 EXPORT_SYMBOL_GPL(proc_thermal_add); 325 326 void proc_thermal_remove(struct proc_thermal_device *proc_priv) 327 { 328 acpi_remove_notify_handler(proc_priv->adev->handle, 329 ACPI_DEVICE_NOTIFY, proc_thermal_notify); 330 int340x_thermal_zone_remove(proc_priv->int340x_zone); 331 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr); 332 sysfs_remove_group(&proc_priv->dev->kobj, 333 &power_limit_attribute_group); 334 } 335 EXPORT_SYMBOL_GPL(proc_thermal_remove); 336 337 static int tcc_offset_save = -1; 338 339 int proc_thermal_suspend(struct device *dev) 340 { 341 tcc_offset_save = intel_tcc_get_offset(-1); 342 if (tcc_offset_save < 0) 343 dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save); 344 345 return 0; 346 } 347 EXPORT_SYMBOL_GPL(proc_thermal_suspend); 348 349 int proc_thermal_resume(struct device *dev) 350 { 351 struct proc_thermal_device *proc_dev; 352 353 proc_dev = dev_get_drvdata(dev); 354 proc_thermal_read_ppcc(proc_dev); 355 356 /* Do not update if saving failed */ 357 if (tcc_offset_save >= 0) 358 intel_tcc_set_offset(-1, tcc_offset_save); 359 360 return 0; 361 } 362 EXPORT_SYMBOL_GPL(proc_thermal_resume); 363 364 #define MCHBAR 0 365 366 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv) 367 { 368 int ret; 369 370 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME); 371 if (ret) { 372 dev_err(&pdev->dev, "cannot reserve PCI memory region\n"); 373 return -ENOMEM; 374 } 375 376 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR]; 377 378 return 0; 379 } 380 381 int proc_thermal_mmio_add(struct pci_dev *pdev, 382 struct proc_thermal_device *proc_priv, 383 kernel_ulong_t feature_mask) 384 { 385 int ret; 386 387 proc_priv->mmio_feature_mask = feature_mask; 388 389 if (feature_mask) { 390 ret = proc_thermal_set_mmio_base(pdev, proc_priv); 391 if (ret) 392 return ret; 393 } 394 395 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) { 396 ret = proc_thermal_rapl_add(pdev, proc_priv); 397 if (ret) { 398 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n"); 399 return ret; 400 } 401 } 402 403 if (feature_mask & PROC_THERMAL_FEATURE_PTC) { 404 ret = proc_thermal_ptc_add(pdev, proc_priv); 405 if (ret) { 406 dev_err(&pdev->dev, "failed to add PTC MMIO interface\n"); 407 goto err_rem_rapl; 408 } 409 } 410 411 if (feature_mask & PROC_THERMAL_FEATURE_FIVR || 412 feature_mask & PROC_THERMAL_FEATURE_DVFS || 413 feature_mask & PROC_THERMAL_FEATURE_DLVR) { 414 ret = proc_thermal_rfim_add(pdev, proc_priv); 415 if (ret) { 416 dev_err(&pdev->dev, "failed to add RFIM interface\n"); 417 goto err_rem_ptc; 418 } 419 } 420 421 if (feature_mask & PROC_THERMAL_FEATURE_WT_REQ) { 422 ret = proc_thermal_wt_req_add(pdev, proc_priv); 423 if (ret) { 424 dev_err(&pdev->dev, "failed to add MBOX interface\n"); 425 goto err_rem_rfim; 426 } 427 } else if (feature_mask & PROC_THERMAL_FEATURE_WT_HINT) { 428 ret = proc_thermal_wt_hint_add(pdev, proc_priv); 429 if (ret) { 430 dev_err(&pdev->dev, "failed to add WT Hint\n"); 431 goto err_rem_rfim; 432 } 433 } 434 435 return 0; 436 437 err_rem_rfim: 438 proc_thermal_rfim_remove(pdev); 439 err_rem_ptc: 440 proc_thermal_ptc_remove(pdev); 441 err_rem_rapl: 442 proc_thermal_rapl_remove(); 443 444 return ret; 445 } 446 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add); 447 448 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv) 449 { 450 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL) 451 proc_thermal_rapl_remove(); 452 453 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_PTC) 454 proc_thermal_ptc_remove(pdev); 455 456 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR || 457 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS || 458 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DLVR) 459 proc_thermal_rfim_remove(pdev); 460 461 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR) 462 proc_thermal_power_floor_set_state(proc_priv, false); 463 464 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_REQ) 465 proc_thermal_wt_req_remove(pdev); 466 else if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_HINT) 467 proc_thermal_wt_hint_remove(pdev); 468 } 469 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove); 470 471 MODULE_IMPORT_NS("INTEL_TCC"); 472 MODULE_IMPORT_NS("INT340X_THERMAL"); 473 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 474 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver"); 475 MODULE_LICENSE("GPL v2"); 476