/* * Rate control delay: Idea is to introduce denounce effect * This should be long enough to avoid reduce events, when * threshold is set to a temperature, which is constantly * violated, but at the short enough to take any action. * The action can be remove threshold or change it to next * interesting setting. Based on experiments, in around * every 5 seconds under load will give us a significant * temperature change.
*/ #define PKG_TEMP_THERMAL_NOTIFY_DELAY 5000 staticint notify_delay_ms = PKG_TEMP_THERMAL_NOTIFY_DELAY;
module_param(notify_delay_ms, int, 0644);
MODULE_PARM_DESC(notify_delay_ms, "User space notification delay in milli seconds.");
/* Number of trip points in thermal zone. Currently it can't * be more than 2. MSR can allow setting and getting notifications * for only 2 thresholds. This define enforces this, if there * is some wrong values returned by cpuid for number of thresholds.
*/ #define MAX_NUMBER_OF_TRIPS 2
/* Keep track of how many zone pointers we allocated in init() */ staticint max_id __read_mostly; /* Array of zone pointers */ staticstruct zone_device **zones; /* Serializes interrupt notification, work and hotplug */ static DEFINE_RAW_SPINLOCK(pkg_temp_lock); /* Protects zone operation in the work function against hotplug removal */ static DEFINE_MUTEX(thermal_zone_mutex);
/* The dynamically assigned cpu hotplug state for module_exit() */ staticenum cpuhp_state pkg_thermal_hp_state __read_mostly;
/* Debug counters to show using debugfs */ staticstruct dentry *debugfs; staticunsignedint pkg_interrupt_cnt; staticunsignedint pkg_work_cnt;
/* * Protection: * * - cpu hotplug: Read serialized by cpu hotplug lock * Write must hold pkg_temp_lock * * - Other callsites: Must hold pkg_temp_lock
*/ staticstruct zone_device *pkg_temp_thermal_get_dev(unsignedint cpu)
{ int id = topology_logical_die_id(cpu);
if (id >= 0 && id < max_id) return zones[id]; return NULL;
}
staticint sys_get_curr_temp(struct thermal_zone_device *tzd, int *temp)
{ struct zone_device *zonedev = thermal_zone_device_priv(tzd); int val, ret;
ret = intel_tcc_get_temp(zonedev->cpu, &val, true); if (ret < 0) return ret;
if (trip_index >= MAX_NUMBER_OF_TRIPS || val < 0 || val > 0x7f) return -EINVAL;
ret = rdmsr_on_cpu(zonedev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
&l, &h); if (ret < 0) return ret;
if (trip_index) {
mask = THERM_MASK_THRESHOLD1;
shift = THERM_SHIFT_THRESHOLD1;
intr = THERM_INT_THRESHOLD1_ENABLE;
} else {
mask = THERM_MASK_THRESHOLD0;
shift = THERM_SHIFT_THRESHOLD0;
intr = THERM_INT_THRESHOLD0_ENABLE;
}
l &= ~mask; /* * When users space sets a trip temperature == 0, which is indication * that, it is no longer interested in receiving notifications.
*/ if (!temp) {
l &= ~intr;
} else {
l |= val << shift;
l |= intr;
}
/* Enable threshold interrupt on local package/cpu */ staticinlinevoid enable_pkg_thres_interrupt(void)
{
u8 thres_0, thres_1;
u32 l, h;
rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); /* only enable/disable if it had valid threshold value */
thres_0 = (l & THERM_MASK_THRESHOLD0) >> THERM_SHIFT_THRESHOLD0;
thres_1 = (l & THERM_MASK_THRESHOLD1) >> THERM_SHIFT_THRESHOLD1; if (thres_0)
l |= THERM_INT_THRESHOLD0_ENABLE; if (thres_1)
l |= THERM_INT_THRESHOLD1_ENABLE;
wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
}
/* Disable threshold interrupt on local package/cpu */ staticinlinevoid disable_pkg_thres_interrupt(void)
{
u32 l, h;
rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
l &= ~(THERM_INT_THRESHOLD0_ENABLE | THERM_INT_THRESHOLD1_ENABLE);
wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
}
staticvoid pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
{ struct thermal_zone_device *tzone = NULL; int cpu = smp_processor_id(); struct zone_device *zonedev;
/* * If tzone is not NULL, then thermal_zone_mutex will prevent the * concurrent removal in the cpu offline callback.
*/ if (tzone)
thermal_zone_device_update(tzone, THERMAL_EVENT_UNSPECIFIED);
/* Work is per package, so scheduling it once is enough. */
zonedev = pkg_temp_thermal_get_dev(cpu); if (zonedev && !zonedev->work_scheduled) {
zonedev->work_scheduled = true;
pkg_thermal_schedule_work(zonedev->cpu, &zonedev->work);
}
/* Store MSR value for package thermal interrupt, to restore at exit */
rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, zonedev->msr_pkg_therm_low,
zonedev->msr_pkg_therm_high);
target = cpumask_any_but(&zonedev->cpumask, cpu);
cpumask_clear_cpu(cpu, &zonedev->cpumask);
lastcpu = target >= nr_cpu_ids; /* * Remove the sysfs files, if this is the last cpu in the package * before doing further cleanups.
*/ if (lastcpu) { struct thermal_zone_device *tzone = zonedev->tzone;
/* * We must protect against a work function calling * thermal_zone_update, after/while unregister. We null out * the pointer under the zone mutex, so the worker function * won't try to call.
*/
mutex_lock(&thermal_zone_mutex);
zonedev->tzone = NULL;
mutex_unlock(&thermal_zone_mutex);
thermal_zone_device_unregister(tzone);
}
/* Protect against work and interrupts */
raw_spin_lock_irq(&pkg_temp_lock);
/* * Check whether this cpu was the current target and store the new * one. When we drop the lock, then the interrupt notify function * will see the new target.
*/
was_target = zonedev->cpu == cpu;
zonedev->cpu = target;
/* * If this is the last CPU in the package remove the package * reference from the array and restore the interrupt MSR. When we * drop the lock neither the interrupt notify function nor the * worker will see the package anymore.
*/ if (lastcpu) {
zones[topology_logical_die_id(cpu)] = NULL; /* After this point nothing touches the MSR anymore. */
wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
zonedev->msr_pkg_therm_low, zonedev->msr_pkg_therm_high);
}
/* * Check whether there is work scheduled and whether the work is * targeted at the outgoing CPU.
*/ if (zonedev->work_scheduled && was_target) { /* * To cancel the work we need to drop the lock, otherwise * we might deadlock if the work needs to be flushed.
*/
raw_spin_unlock_irq(&pkg_temp_lock);
cancel_delayed_work_sync(&zonedev->work);
raw_spin_lock_irq(&pkg_temp_lock); /* * If this is not the last cpu in the package and the work * did not run after we dropped the lock above, then we * need to reschedule the work, otherwise the interrupt * stays disabled forever.
*/ if (!lastcpu && zonedev->work_scheduled)
pkg_thermal_schedule_work(target, &zonedev->work);
}
raw_spin_unlock_irq(&pkg_temp_lock);
/* Final cleanup if this is the last cpu */ if (lastcpu)
kfree(zonedev);
/* If the package exists, nothing to do */ if (zonedev) {
cpumask_set_cpu(cpu, &zonedev->cpumask); return 0;
} return pkg_temp_thermal_device_add(cpu);
}
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.