Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * processor_thermal_device.c
0004  * Copyright (c) 2014, Intel Corporation.
0005  */
0006 #include <linux/acpi.h>
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/pci.h>
0010 #include <linux/thermal.h>
0011 #include "int340x_thermal_zone.h"
0012 #include "processor_thermal_device.h"
0013 #include "../intel_soc_dts_iosf.h"
0014 
0015 #define DRV_NAME "proc_thermal"
0016 
0017 #define POWER_LIMIT_SHOW(index, suffix) \
0018 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
0019                     struct device_attribute *attr, \
0020                     char *buf) \
0021 { \
0022     struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
0023     \
0024     return sprintf(buf, "%lu\n",\
0025     (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
0026 }
0027 
0028 POWER_LIMIT_SHOW(0, min_uw)
0029 POWER_LIMIT_SHOW(0, max_uw)
0030 POWER_LIMIT_SHOW(0, step_uw)
0031 POWER_LIMIT_SHOW(0, tmin_us)
0032 POWER_LIMIT_SHOW(0, tmax_us)
0033 
0034 POWER_LIMIT_SHOW(1, min_uw)
0035 POWER_LIMIT_SHOW(1, max_uw)
0036 POWER_LIMIT_SHOW(1, step_uw)
0037 POWER_LIMIT_SHOW(1, tmin_us)
0038 POWER_LIMIT_SHOW(1, tmax_us)
0039 
0040 static DEVICE_ATTR_RO(power_limit_0_min_uw);
0041 static DEVICE_ATTR_RO(power_limit_0_max_uw);
0042 static DEVICE_ATTR_RO(power_limit_0_step_uw);
0043 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
0044 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
0045 
0046 static DEVICE_ATTR_RO(power_limit_1_min_uw);
0047 static DEVICE_ATTR_RO(power_limit_1_max_uw);
0048 static DEVICE_ATTR_RO(power_limit_1_step_uw);
0049 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
0050 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
0051 
0052 static struct attribute *power_limit_attrs[] = {
0053     &dev_attr_power_limit_0_min_uw.attr,
0054     &dev_attr_power_limit_1_min_uw.attr,
0055     &dev_attr_power_limit_0_max_uw.attr,
0056     &dev_attr_power_limit_1_max_uw.attr,
0057     &dev_attr_power_limit_0_step_uw.attr,
0058     &dev_attr_power_limit_1_step_uw.attr,
0059     &dev_attr_power_limit_0_tmin_us.attr,
0060     &dev_attr_power_limit_1_tmin_us.attr,
0061     &dev_attr_power_limit_0_tmax_us.attr,
0062     &dev_attr_power_limit_1_tmax_us.attr,
0063     NULL
0064 };
0065 
0066 static const struct attribute_group power_limit_attribute_group = {
0067     .attrs = power_limit_attrs,
0068     .name = "power_limits"
0069 };
0070 
0071 static int tcc_get_offset(void)
0072 {
0073     u64 val;
0074     int err;
0075 
0076     err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
0077     if (err)
0078         return err;
0079 
0080     return (val >> 24) & 0x3f;
0081 }
0082 
0083 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
0084                           struct device_attribute *attr,
0085                           char *buf)
0086 {
0087     int tcc;
0088 
0089     tcc = tcc_get_offset();
0090     if (tcc < 0)
0091         return tcc;
0092 
0093     return sprintf(buf, "%d\n", tcc);
0094 }
0095 
0096 static int tcc_offset_update(unsigned int tcc)
0097 {
0098     u64 val;
0099     int err;
0100 
0101     if (tcc > 63)
0102         return -EINVAL;
0103 
0104     err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
0105     if (err)
0106         return err;
0107 
0108     if (val & BIT(31))
0109         return -EPERM;
0110 
0111     val &= ~GENMASK_ULL(29, 24);
0112     val |= (tcc & 0x3f) << 24;
0113 
0114     err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
0115     if (err)
0116         return err;
0117 
0118     return 0;
0119 }
0120 
0121 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
0122                 struct device_attribute *attr, const char *buf,
0123                 size_t count)
0124 {
0125     unsigned int tcc;
0126     u64 val;
0127     int err;
0128 
0129     err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
0130     if (err)
0131         return err;
0132 
0133     if (!(val & BIT(30)))
0134         return -EACCES;
0135 
0136     if (kstrtouint(buf, 0, &tcc))
0137         return -EINVAL;
0138 
0139     err = tcc_offset_update(tcc);
0140     if (err)
0141         return err;
0142 
0143     return count;
0144 }
0145 
0146 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
0147 
0148 static int stored_tjmax; /* since it is fixed, we can have local storage */
0149 
0150 static int get_tjmax(void)
0151 {
0152     u32 eax, edx;
0153     u32 val;
0154     int err;
0155 
0156     err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
0157     if (err)
0158         return err;
0159 
0160     val = (eax >> 16) & 0xff;
0161     if (val)
0162         return val;
0163 
0164     return -EINVAL;
0165 }
0166 
0167 static int read_temp_msr(int *temp)
0168 {
0169     int cpu;
0170     u32 eax, edx;
0171     int err;
0172     unsigned long curr_temp_off = 0;
0173 
0174     *temp = 0;
0175 
0176     for_each_online_cpu(cpu) {
0177         err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
0178                     &edx);
0179         if (err)
0180             goto err_ret;
0181         else {
0182             if (eax & 0x80000000) {
0183                 curr_temp_off = (eax >> 16) & 0x7f;
0184                 if (!*temp || curr_temp_off < *temp)
0185                     *temp = curr_temp_off;
0186             } else {
0187                 err = -EINVAL;
0188                 goto err_ret;
0189             }
0190         }
0191     }
0192 
0193     return 0;
0194 err_ret:
0195     return err;
0196 }
0197 
0198 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
0199                      int *temp)
0200 {
0201     int ret;
0202 
0203     ret = read_temp_msr(temp);
0204     if (!ret)
0205         *temp = (stored_tjmax - *temp) * 1000;
0206 
0207     return ret;
0208 }
0209 
0210 static struct thermal_zone_device_ops proc_thermal_local_ops = {
0211     .get_temp       = proc_thermal_get_zone_temp,
0212 };
0213 
0214 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
0215 {
0216     int i;
0217     acpi_status status;
0218     struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
0219     union acpi_object *elements, *ppcc;
0220     union acpi_object *p;
0221     int ret = 0;
0222 
0223     status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
0224                       NULL, &buf);
0225     if (ACPI_FAILURE(status))
0226         return -ENODEV;
0227 
0228     p = buf.pointer;
0229     if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
0230         dev_err(proc_priv->dev, "Invalid PPCC data\n");
0231         ret = -EFAULT;
0232         goto free_buffer;
0233     }
0234 
0235     if (!p->package.count) {
0236         dev_err(proc_priv->dev, "Invalid PPCC package size\n");
0237         ret = -EFAULT;
0238         goto free_buffer;
0239     }
0240 
0241     for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
0242         elements = &(p->package.elements[i+1]);
0243         if (elements->type != ACPI_TYPE_PACKAGE ||
0244             elements->package.count != 6) {
0245             ret = -EFAULT;
0246             goto free_buffer;
0247         }
0248         ppcc = elements->package.elements;
0249         proc_priv->power_limits[i].index = ppcc[0].integer.value;
0250         proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
0251         proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
0252         proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
0253         proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
0254         proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
0255     }
0256 
0257 free_buffer:
0258     kfree(buf.pointer);
0259 
0260     return ret;
0261 }
0262 
0263 #define PROC_POWER_CAPABILITY_CHANGED   0x83
0264 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
0265 {
0266     struct proc_thermal_device *proc_priv = data;
0267 
0268     if (!proc_priv)
0269         return;
0270 
0271     switch (event) {
0272     case PROC_POWER_CAPABILITY_CHANGED:
0273         proc_thermal_read_ppcc(proc_priv);
0274         int340x_thermal_zone_device_update(proc_priv->int340x_zone,
0275                 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
0276         break;
0277     default:
0278         dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
0279         break;
0280     }
0281 }
0282 
0283 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
0284 {
0285     struct acpi_device *adev;
0286     acpi_status status;
0287     unsigned long long tmp;
0288     struct thermal_zone_device_ops *ops = NULL;
0289     int ret;
0290 
0291     adev = ACPI_COMPANION(dev);
0292     if (!adev)
0293         return -ENODEV;
0294 
0295     proc_priv->dev = dev;
0296     proc_priv->adev = adev;
0297 
0298     ret = proc_thermal_read_ppcc(proc_priv);
0299     if (ret)
0300         return ret;
0301 
0302     status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
0303     if (ACPI_FAILURE(status)) {
0304         /* there is no _TMP method, add local method */
0305         stored_tjmax = get_tjmax();
0306         if (stored_tjmax > 0)
0307             ops = &proc_thermal_local_ops;
0308     }
0309 
0310     proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
0311     if (IS_ERR(proc_priv->int340x_zone)) {
0312         return PTR_ERR(proc_priv->int340x_zone);
0313     } else
0314         ret = 0;
0315 
0316     ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
0317                       proc_thermal_notify,
0318                       (void *)proc_priv);
0319     if (ret)
0320         goto remove_zone;
0321 
0322     ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
0323     if (ret)
0324         goto remove_notify;
0325 
0326     ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
0327     if (ret) {
0328         sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
0329         goto remove_notify;
0330     }
0331 
0332     return 0;
0333 
0334 remove_notify:
0335     acpi_remove_notify_handler(adev->handle,
0336                     ACPI_DEVICE_NOTIFY, proc_thermal_notify);
0337 remove_zone:
0338     int340x_thermal_zone_remove(proc_priv->int340x_zone);
0339 
0340     return ret;
0341 }
0342 EXPORT_SYMBOL_GPL(proc_thermal_add);
0343 
0344 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
0345 {
0346     acpi_remove_notify_handler(proc_priv->adev->handle,
0347                    ACPI_DEVICE_NOTIFY, proc_thermal_notify);
0348     int340x_thermal_zone_remove(proc_priv->int340x_zone);
0349     sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
0350     sysfs_remove_group(&proc_priv->dev->kobj,
0351                &power_limit_attribute_group);
0352 }
0353 EXPORT_SYMBOL_GPL(proc_thermal_remove);
0354 
0355 static int tcc_offset_save = -1;
0356 
0357 int proc_thermal_suspend(struct device *dev)
0358 {
0359     tcc_offset_save = tcc_get_offset();
0360     if (tcc_offset_save < 0)
0361         dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
0362 
0363     return 0;
0364 }
0365 EXPORT_SYMBOL_GPL(proc_thermal_suspend);
0366 
0367 int proc_thermal_resume(struct device *dev)
0368 {
0369     struct proc_thermal_device *proc_dev;
0370 
0371     proc_dev = dev_get_drvdata(dev);
0372     proc_thermal_read_ppcc(proc_dev);
0373 
0374     /* Do not update if saving failed */
0375     if (tcc_offset_save >= 0)
0376         tcc_offset_update(tcc_offset_save);
0377 
0378     return 0;
0379 }
0380 EXPORT_SYMBOL_GPL(proc_thermal_resume);
0381 
0382 #define MCHBAR 0
0383 
0384 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
0385 {
0386     int ret;
0387 
0388     ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
0389     if (ret) {
0390         dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
0391         return -ENOMEM;
0392     }
0393 
0394     proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
0395 
0396     return 0;
0397 }
0398 
0399 int proc_thermal_mmio_add(struct pci_dev *pdev,
0400               struct proc_thermal_device *proc_priv,
0401               kernel_ulong_t feature_mask)
0402 {
0403     int ret;
0404 
0405     proc_priv->mmio_feature_mask = feature_mask;
0406 
0407     if (feature_mask) {
0408         ret = proc_thermal_set_mmio_base(pdev, proc_priv);
0409         if (ret)
0410             return ret;
0411     }
0412 
0413     if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
0414         ret = proc_thermal_rapl_add(pdev, proc_priv);
0415         if (ret) {
0416             dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
0417             return ret;
0418         }
0419     }
0420 
0421     if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
0422         feature_mask & PROC_THERMAL_FEATURE_DVFS) {
0423         ret = proc_thermal_rfim_add(pdev, proc_priv);
0424         if (ret) {
0425             dev_err(&pdev->dev, "failed to add RFIM interface\n");
0426             goto err_rem_rapl;
0427         }
0428     }
0429 
0430     if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
0431         ret = proc_thermal_mbox_add(pdev, proc_priv);
0432         if (ret) {
0433             dev_err(&pdev->dev, "failed to add MBOX interface\n");
0434             goto err_rem_rfim;
0435         }
0436     }
0437 
0438     return 0;
0439 
0440 err_rem_rfim:
0441     proc_thermal_rfim_remove(pdev);
0442 err_rem_rapl:
0443     proc_thermal_rapl_remove();
0444 
0445     return ret;
0446 }
0447 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
0448 
0449 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
0450 {
0451     if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
0452         proc_thermal_rapl_remove();
0453 
0454     if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
0455         proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
0456         proc_thermal_rfim_remove(pdev);
0457 
0458     if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
0459         proc_thermal_mbox_remove(pdev);
0460 }
0461 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
0462 
0463 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
0464 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
0465 MODULE_LICENSE("GPL v2");