// SPDX-License-Identifier: GPL-2.0-only /* * coretemp.c - Linux kernel module for hardware monitoring * * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz> * * Inspired from many hwmon drivers
*/
/* * force_tjmax only matters when TjMax can't be read from the CPU itself. * When set, it replaces the driver's suboptimal heuristic.
*/ staticint force_tjmax;
module_param_named(tjmax, force_tjmax, int, 0444);
MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
#define NUM_REAL_CORES 512 /* Number of Real cores per cpu */ #define CORETEMP_NAME_LENGTH 28 /* String Length of attrs */
enum coretemp_attr_index {
ATTR_LABEL,
ATTR_CRIT_ALARM,
ATTR_TEMP,
ATTR_TJMAX,
ATTR_TTARGET,
MAX_CORE_ATTRS = ATTR_TJMAX + 1, /* Maximum no of basic attrs */
TOTAL_ATTRS = ATTR_TTARGET + 1 /* Maximum no of possible attrs */
};
#ifdef CONFIG_SMP #define for_each_sibling(i, cpu) \
for_each_cpu(i, topology_sibling_cpumask(cpu)) #else #define for_each_sibling(i, cpu) for (i = 0; false; ) #endif
/* * Per-Core Temperature Data * @tjmax: The static tjmax value when tjmax cannot be retrieved from * IA32_TEMPERATURE_TARGET MSR. * @last_updated: The time when the current temperature value was updated * earlier (in jiffies). * @cpu_core_id: The CPU Core from which temperature values should be read * This value is passed as "id" field to rdmsr/wrmsr functions. * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS, * from where the temperature values should be read. * @attr_size: Total number of pre-core attrs displayed in the sysfs.
*/ struct temp_data { int temp; int tjmax; unsignedlong last_updated; unsignedint cpu; int index;
u32 cpu_core_id;
u32 status_reg; int attr_size; struct device_attribute sd_attrs[TOTAL_ATTRS]; char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH]; struct attribute *attrs[TOTAL_ATTRS + 1]; struct attribute_group attr_group; struct mutex update_lock;
};
/* Platform Data per Physical CPU */ struct platform_data { struct device *hwmon_dev;
u16 pkg_id; int nr_cores; struct ida ida; struct cpumask cpumask; struct temp_data *pkg_data; struct temp_data **core_data; struct device_attribute name_attr;
};
struct tjmax_pci { unsignedint device; int tjmax;
};
staticint adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
{ /* The 100C is default for both mobile and non mobile CPUs */
int tjmax = 100000; int tjmax_ee = 85000; int usemsr_ee = 1; int err;
u32 eax, edx; int i;
u16 devfn = PCI_DEVFN(0, 0); struct pci_dev *host_bridge = pci_get_domain_bus_and_slot(0, 0, devfn);
/* * Explicit tjmax table entries override heuristics. * First try PCI host bridge IDs, followed by model ID strings * and model/stepping information.
*/ if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL) { for (i = 0; i < ARRAY_SIZE(tjmax_pci_table); i++) { if (host_bridge->device == tjmax_pci_table[i].device) {
pci_dev_put(host_bridge); return tjmax_pci_table[i].tjmax;
}
}
}
pci_dev_put(host_bridge);
for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) { if (strstr(c->x86_model_id, tjmax_table[i].id)) return tjmax_table[i].tjmax;
}
for (i = 0; i < ARRAY_SIZE(tjmax_model_table); i++) { conststruct tjmax_model *tm = &tjmax_model_table[i]; if (c->x86_model == tm->model &&
(tm->mask == ANY || c->x86_stepping == tm->mask)) return tm->tjmax;
}
if (c->x86_model > 0xe && usemsr_ee) {
u8 platform_id;
/* * Now we can detect the mobile CPU using Intel provided table * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
*/
err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx); if (err) {
dev_warn(dev, "Unable to access MSR 0x17, assuming desktop" " CPU\n");
usemsr_ee = 0;
} elseif (c->x86_model < 0x17 && !(eax & 0x10000000)) { /* * Trust bit 28 up to Penryn, I could not find any * documentation on that; if you happen to know * someone at Intel please ask
*/
usemsr_ee = 0;
} else { /* Platform ID bits 52:50 (EDX starts at bit 32) */
platform_id = (edx >> 18) & 0x7;
/* * Mobile Penryn CPU seems to be platform ID 7 or 5 * (guesswork)
*/ if (c->x86_model == 0x17 &&
(platform_id == 5 || platform_id == 7)) { /* * If MSR EE bit is set, set it to 90 degrees C, * otherwise 105 degrees C
*/
tjmax_ee = 90000;
tjmax = 105000;
}
}
}
if (usemsr_ee) {
err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx); if (err) {
dev_warn(dev, "Unable to access MSR 0xEE, for Tjmax, left" " at default\n");
} elseif (eax & 0x40000000) {
tjmax = tjmax_ee;
}
} elseif (tjmax == 100000) { /* * If we don't use msr EE it means we are desktop CPU * (with exeception of Atom)
*/
dev_warn(dev, "Using relative temperature scale!\n");
}
return tjmax;
}
staticbool cpu_has_tjmax(struct cpuinfo_x86 *c)
{
u8 model = c->x86_model;
return model > 0xe &&
model != 0x1c &&
model != 0x26 &&
model != 0x27 &&
model != 0x35 &&
model != 0x36;
}
/* use static tjmax once it is set */ if (tdata->tjmax) return tdata->tjmax;
/* * A new feature of current Intel(R) processors, the * IA32_TEMPERATURE_TARGET contains the TjMax value
*/
err = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); if (err) { if (cpu_has_tjmax(c))
dev_warn(dev, "Unable to read TjMax from CPU %u\n", tdata->cpu);
} else {
val = (eax >> 16) & 0xff; if (val) return val * 1000;
}
if (force_tjmax) {
dev_notice(dev, "TjMax forced to %d degrees C by user\n",
force_tjmax);
tdata->tjmax = force_tjmax * 1000;
} else { /* * An assumption is made for early CPUs and unreadable MSR. * NOTE: the calculated value may not be correct.
*/
tdata->tjmax = adjust_tjmax(c, tdata->cpu, dev);
} return tdata->tjmax;
}
/* * ttarget is valid only if tjmax can be retrieved from * MSR_IA32_TEMPERATURE_TARGET
*/ if (tdata->tjmax) return -ENODEV;
ret = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); if (ret) return ret;
tjmax = (eax >> 16) & 0xff;
/* Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET. */
ttarget_offset = (eax >> 8) & 0xff;
return (tjmax - ttarget_offset) * 1000;
}
/* Keep track of how many zone pointers we allocated in init() */ staticint max_zones __read_mostly; /* Array of zone pointers. Serialized by cpu hotplug lock */ staticstruct platform_device **zone_devices;
tjmax = get_tjmax(tdata, dev); /* Check whether the time interval has elapsed */ if (time_after(jiffies, tdata->last_updated + HZ)) {
rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx); /* * Ignore the valid bit. In all observed cases the register * value is either low or zero if the valid bit is 0. * Return it instead of reporting an error which doesn't * really help at all.
*/
tdata->temp = tjmax - ((eax >> 16) & 0xff) * 1000;
tdata->last_updated = jiffies;
}
for (i = 0; i < tdata->attr_size; i++) { /* * We map the attr number to core id of the CPU * The attr number is always core id + 2 * The Pkgtemp will always show up as temp1_*, if available
*/ int attr_no = is_pkg_temp_data(tdata) ? 1 : tdata->cpu_core_id + 2;
/* * Check if we have problem with errata AE18 of Core processors: * Readings might stop update when processor visited too deep sleep, * fixed for stepping D0 (6EC).
*/ if (c->x86_model == 0xe && c->x86_stepping < 0xc && c->microcode < 0x39) {
pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n"); return -ENODEV;
} return 0;
}
staticstruct platform_device *coretemp_get_pdev(unsignedint cpu)
{ int id = topology_logical_die_id(cpu);
if (id >= 0 && id < max_zones) return zone_devices[id]; return NULL;
}
if (!pdata->core_data) { /* * TODO: * The information of actual possible cores in a package is broken for now. * Will replace hardcoded NUM_REAL_CORES with actual per package core count * when this information becomes available.
*/
pdata->nr_cores = NUM_REAL_CORES;
pdata->core_data = kcalloc(pdata->nr_cores, sizeof(struct temp_data *),
GFP_KERNEL); if (!pdata->core_data) return NULL;
}
tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL); if (!tdata) return NULL;
if (pkg_flag) {
pdata->pkg_data = tdata; /* Use tdata->index as indicator of package temp data */
tdata->index = -1;
} else {
tdata->index = ida_alloc_max(&pdata->ida, pdata->nr_cores - 1, GFP_KERNEL); if (tdata->index < 0) {
kfree(tdata); return NULL;
}
pdata->core_data[tdata->index] = tdata;
}
if (!housekeeping_cpu(cpu, HK_TYPE_MISC)) return 0;
tdata = init_temp_data(pdata, cpu, pkg_flag); if (!tdata) return -ENOMEM;
/* Test if we can access the status register */
err = rdmsr_safe_on_cpu(cpu, tdata->status_reg, &eax, &edx); if (err) goto err;
/* Make sure tdata->tjmax is a valid indicator for dynamic/static tjmax */
get_tjmax(tdata, &pdev->dev);
/* * The target temperature is available on older CPUs but not in the * MSR_IA32_TEMPERATURE_TARGET register. Atoms don't have the register * at all.
*/ if (c->x86_model > 0xe && c->x86_model != 0x1c) if (get_ttarget(tdata, &pdev->dev) >= 0)
tdata->attr_size++;
staticvoid
coretemp_add_core(struct platform_device *pdev, unsignedint cpu, int pkg_flag)
{ if (create_core_data(pdev, cpu, pkg_flag))
dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
}
staticvoid coretemp_remove_core(struct platform_data *pdata, struct temp_data *tdata)
{ /* if we errored on add then this is already gone */ if (!tdata) return;
/* Remove the sysfs attributes */
sysfs_remove_group(&pdata->hwmon_dev->kobj, &tdata->attr_group);
/* * Don't execute this on resume as the offline callback did * not get executed on suspend.
*/ if (cpuhp_tasks_frozen) return 0;
/* * CPUID.06H.EAX[0] indicates whether the CPU has thermal * sensors. We check this bit only, all the early CPUs * without thermal sensors will be filtered out.
*/ if (!cpu_has(c, X86_FEATURE_DTHERM)) return -ENODEV;
pdata = platform_get_drvdata(pdev); if (!pdata->hwmon_dev) { struct device *hwmon;
/* Check the microcode version of the CPU */ if (chk_ucode_version(cpu)) return -EINVAL;
/* * Alright, we have DTS support. * We are bringing the _first_ core in this pkg * online. So, initialize per-pkg data structures and * then bring this core online.
*/
hwmon = hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
pdata, NULL); if (IS_ERR(hwmon)) return PTR_ERR(hwmon);
pdata->hwmon_dev = hwmon;
/* * Check whether pkgtemp support is available. * If so, add interfaces for pkgtemp.
*/ if (cpu_has(c, X86_FEATURE_PTS))
coretemp_add_core(pdev, cpu, 1);
}
/* * Check whether a thread sibling is already online. If not add the * interface for this CPU core.
*/ if (!cpumask_intersects(&pdata->cpumask, topology_sibling_cpumask(cpu)))
coretemp_add_core(pdev, cpu, 0);
/* No need to tear down any interfaces for suspend */ if (cpuhp_tasks_frozen) return 0;
/* If the physical CPU device does not exist, just return */
pd = platform_get_drvdata(pdev); if (!pd->hwmon_dev) return 0;
tdata = get_temp_data(pd, cpu);
cpumask_clear_cpu(cpu, &pd->cpumask);
/* * If this is the last thread sibling, remove the CPU core * interface, If there is still a sibling online, transfer the * target cpu of that core interface to it.
*/
target = cpumask_any_and(&pd->cpumask, topology_sibling_cpumask(cpu)); if (target >= nr_cpu_ids) {
coretemp_remove_core(pd, tdata);
} elseif (tdata && tdata->cpu == cpu) {
mutex_lock(&tdata->update_lock);
tdata->cpu = target;
mutex_unlock(&tdata->update_lock);
}
/* * If all cores in this pkg are offline, remove the interface.
*/
tdata = get_temp_data(pd, -1); if (cpumask_empty(&pd->cpumask)) { if (tdata)
coretemp_remove_core(pd, tdata);
hwmon_device_unregister(pd->hwmon_dev);
pd->hwmon_dev = NULL; return 0;
}
/* * Check whether this core is the target for the package * interface. We need to assign it to some other cpu.
*/ if (tdata && tdata->cpu == cpu) {
target = cpumask_first(&pd->cpumask);
mutex_lock(&tdata->update_lock);
tdata->cpu = target;
mutex_unlock(&tdata->update_lock);
} return 0;
} staticconststruct x86_cpu_id __initconst coretemp_ids[] = {
X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_DTHERM, NULL),
{}
};
MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
staticenum cpuhp_state coretemp_hp_online;
staticint __init coretemp_init(void)
{ int i, err;
/* * CPUID.06H.EAX[0] indicates whether the CPU has thermal * sensors. We check this bit only, all the early CPUs * without thermal sensors will be filtered out.
*/ if (!x86_match_cpu(coretemp_ids)) return -ENODEV;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.