/* * Some Intel Ibex Peak based platforms support so-called "intelligent * power sharing", which allows the CPU and GPU to cooperate to maximize * performance within a given TDP (thermal design point). This driver * performs the coordination between the CPU and GPU, monitors thermal and * power statistics in the platform, and initializes power monitoring * hardware. It also provides a few tunables to control behavior. Its * primary purpose is to safely allow CPU and GPU turbo modes to be enabled * by tracking power and thermal budget; secondarily it can boost turbo * performance by allocating more power or thermal budget to the CPU or GPU * based on available headroom and activity. * * The basic algorithm is driven by a 5s moving average of temperature. If * thermal headroom is available, the CPU and/or GPU power clamps may be * adjusted upwards. If we hit the thermal ceiling or a thermal trigger, * we scale back the clamp. Aside from trigger events (when we're critically * close or over our TDP) we don't adjust the clamps more than once every * five seconds. * * The thermal device (device 31, function 6) has a set of registers that * are updated by the ME firmware. The ME should also take the clamp values * written to those registers and write them to the CPU, but we currently * bypass that functionality and write the CPU MSR directly. * * UNSUPPORTED: * - dual MCP configs * * TODO: * - handle CPU hotplug * - provide turbo enable/disable api * * Related documents: * - CDI 403777, 403778 - Auburndale EDS vol 1 & 2 * - CDI 401376 - Ibex Peak EDS * - ref 26037, 26641 - IPS BIOS spec * - ref 26489 - Nehalem BIOS writer's guide * - ref 26921 - Ibex Peak BIOS Specification
*/
/* For initial average collection */ staticconstint IPS_SAMPLE_PERIOD = 200; /* ms */ staticconstint IPS_SAMPLE_WINDOW = 5000; /* 5s moving window of samples */ #define IPS_SAMPLE_COUNT (IPS_SAMPLE_WINDOW / IPS_SAMPLE_PERIOD)
/* Per-SKU limits */ struct ips_mcp_limits { int mcp_power_limit; /* mW units */ int core_power_limit; int mch_power_limit; int core_temp_limit; /* degrees C */ int mch_temp_limit;
};
/* Max temps are -10 degrees C to avoid PROCHOT# */
/* Average CPU core temps (all averages in .01 degrees C for precision) */
u16 ctv1_avg_temp;
u16 ctv2_avg_temp; /* GMCH average */
u16 mch_avg_temp; /* Average for the CPU (both cores?) */
u16 mcp_avg_temp; /* Average power consumption (in mW) */
u32 cpu_avg_power;
u32 mch_avg_power;
/* Optional MCH interfaces for if i915 is in use */ unsignedlong (*read_mch_val)(void); bool (*gpu_raise)(void); bool (*gpu_lower)(void); bool (*gpu_busy)(void); bool (*gpu_turbo_disable)(void);
/* For restoration at unload */
u64 orig_turbo_limit;
u64 orig_turbo_ratios;
};
/** * ips_cpu_busy - is CPU busy? * @ips: IPS driver struct * * Check CPU for load to see whether we should increase its thermal budget. * * RETURNS: * True if the CPU could use more power, false otherwise.
*/ staticbool ips_cpu_busy(struct ips_driver *ips)
{ if ((avenrun[0] >> FSHIFT) > 1) returntrue;
returnfalse;
}
/** * ips_cpu_raise - raise CPU power clamp * @ips: IPS driver struct * * Raise the CPU power clamp by %IPS_CPU_STEP, in accordance with TDP for * this platform. * * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR upwards (as * long as we haven't hit the TDP limit for the SKU).
*/ staticvoid ips_cpu_raise(struct ips_driver *ips)
{
u64 turbo_override;
u16 cur_tdp_limit, new_tdp_limit;
/** * ips_cpu_lower - lower CPU power clamp * @ips: IPS driver struct * * Lower CPU power clamp b %IPS_CPU_STEP if possible. * * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR down, going * as low as the platform limits will allow (though we could go lower there * wouldn't be much point).
*/ staticvoid ips_cpu_lower(struct ips_driver *ips)
{
u64 turbo_override;
u16 cur_limit, new_limit;
/** * do_enable_cpu_turbo - internal turbo enable function * @data: unused * * Internal function for actually updating MSRs. When we enable/disable * turbo, we need to do it on each CPU; this function is the one called * by on_each_cpu() when needed.
*/ staticvoid do_enable_cpu_turbo(void *data)
{
u64 perf_ctl;
/** * ips_enable_cpu_turbo - enable turbo mode on all CPUs * @ips: IPS driver struct * * Enable turbo mode by clearing the disable bit in IA32_PERF_CTL on * all logical threads.
*/ staticvoid ips_enable_cpu_turbo(struct ips_driver *ips)
{ /* Already on, no need to mess with MSRs */ if (ips->__cpu_turbo_on) return;
if (ips->turbo_toggle_allowed)
on_each_cpu(do_enable_cpu_turbo, ips, 1);
ips->__cpu_turbo_on = true;
}
/** * do_disable_cpu_turbo - internal turbo disable function * @data: unused * * Internal function for actually updating MSRs. When we enable/disable * turbo, we need to do it on each CPU; this function is the one called * by on_each_cpu() when needed.
*/ staticvoid do_disable_cpu_turbo(void *data)
{
u64 perf_ctl;
/** * ips_disable_cpu_turbo - disable turbo mode on all CPUs * @ips: IPS driver struct * * Disable turbo mode by setting the disable bit in IA32_PERF_CTL on * all logical threads.
*/ staticvoid ips_disable_cpu_turbo(struct ips_driver *ips)
{ /* Already off, leave it */ if (!ips->__cpu_turbo_on) return;
if (ips->turbo_toggle_allowed)
on_each_cpu(do_disable_cpu_turbo, ips, 1);
ips->__cpu_turbo_on = false;
}
/** * ips_gpu_busy - is GPU busy? * @ips: IPS driver struct * * Check GPU for load to see whether we should increase its thermal budget. * We need to call into the i915 driver in this case. * * RETURNS: * True if the GPU could use more power, false otherwise.
*/ staticbool ips_gpu_busy(struct ips_driver *ips)
{ if (!ips_gpu_turbo_enabled(ips)) returnfalse;
return ips->gpu_busy();
}
/** * ips_gpu_raise - raise GPU power clamp * @ips: IPS driver struct * * Raise the GPU frequency/power if possible. We need to call into the * i915 driver in this case.
*/ staticvoid ips_gpu_raise(struct ips_driver *ips)
{ if (!ips_gpu_turbo_enabled(ips)) return;
if (!ips->gpu_raise())
ips->gpu_turbo_enabled = false;
return;
}
/** * ips_gpu_lower - lower GPU power clamp * @ips: IPS driver struct * * Lower GPU frequency/power if possible. Need to call i915.
*/ staticvoid ips_gpu_lower(struct ips_driver *ips)
{ if (!ips_gpu_turbo_enabled(ips)) return;
if (!ips->gpu_lower())
ips->gpu_turbo_enabled = false;
return;
}
/** * ips_enable_gpu_turbo - notify the gfx driver turbo is available * @ips: IPS driver struct * * Call into the graphics driver indicating that it can safely use * turbo mode.
*/ staticvoid ips_enable_gpu_turbo(struct ips_driver *ips)
{ if (ips->__gpu_turbo_on) return;
ips->__gpu_turbo_on = true;
}
/** * ips_disable_gpu_turbo - notify the gfx driver to disable turbo mode * @ips: IPS driver struct * * Request that the graphics driver disable turbo mode.
*/ staticvoid ips_disable_gpu_turbo(struct ips_driver *ips)
{ /* Avoid calling i915 if turbo is already disabled */ if (!ips->__gpu_turbo_on) return;
if (!ips->gpu_turbo_disable())
dev_err(ips->dev, "failed to disable graphics turbo\n"); else
ips->__gpu_turbo_on = false;
}
/** * mcp_exceeded - check whether we're outside our thermal & power limits * @ips: IPS driver struct * * Check whether the MCP is over its thermal or power budget. * * Returns: %true if the temp or power has exceeded its maximum, else %false
*/ staticbool mcp_exceeded(struct ips_driver *ips)
{ unsignedlong flags; bool ret = false;
u32 temp_limit;
u32 avg_power;
/** * cpu_exceeded - check whether a CPU core is outside its limits * @ips: IPS driver struct * @cpu: CPU number to check * * Check a given CPU's average temp or power is over its limit. * * Returns: %true if the temp or power has exceeded its maximum, else %false
*/ staticbool cpu_exceeded(struct ips_driver *ips, int cpu)
{ unsignedlong flags; int avg; bool ret = false;
spin_lock_irqsave(&ips->turbo_status_lock, flags);
avg = cpu ? ips->ctv2_avg_temp : ips->ctv1_avg_temp; if (avg > (ips->limits->core_temp_limit * 100))
ret = true; if (ips->cpu_avg_power > ips->core_power_limit * 100)
ret = true;
spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
if (ret)
dev_info(ips->dev, "CPU power or thermal limit exceeded\n");
return ret;
}
/** * mch_exceeded - check whether the GPU is over budget * @ips: IPS driver struct * * Check the MCH temp & power against their maximums. * * Returns: %true if the temp or power has exceeded its maximum, else %false
*/ staticbool mch_exceeded(struct ips_driver *ips)
{ unsignedlong flags; bool ret = false;
spin_lock_irqsave(&ips->turbo_status_lock, flags); if (ips->mch_avg_temp > (ips->limits->mch_temp_limit * 100))
ret = true; if (ips->mch_avg_power > ips->mch_power_limit)
ret = true;
spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
return ret;
}
/** * verify_limits - verify BIOS provided limits * @ips: IPS structure * * BIOS can optionally provide non-default limits for power and temp. Check * them here and use the defaults if the BIOS values are not provided or * are otherwise unusable.
*/ staticvoid verify_limits(struct ips_driver *ips)
{ if (ips->mcp_power_limit < ips->limits->mcp_power_limit ||
ips->mcp_power_limit > 35000)
ips->mcp_power_limit = ips->limits->mcp_power_limit;
/** * update_turbo_limits - get various limits & settings from regs * @ips: IPS driver struct * * Update the IPS power & temp limits, along with turbo enable flags, * based on latest register contents. * * Used at init time and for runtime BIOS support, which requires polling * the regs for updates (as a result of AC->DC transition for example). * * LOCKING: * Caller must hold turbo_status_lock (outside of init)
*/ staticvoid update_turbo_limits(struct ips_driver *ips)
{
u32 hts = thm_readl(THM_HTS);
ips->cpu_turbo_enabled = !(hts & HTS_PCTD_DIS); /* * Disable turbo for now, until we can figure out why the power figures * are wrong
*/
ips->cpu_turbo_enabled = false;
if (ips->gpu_busy)
ips->gpu_turbo_enabled = !(hts & HTS_GTD_DIS);
verify_limits(ips); /* Ignore BIOS CPU vs GPU pref */
}
/** * ips_adjust - adjust power clamp based on thermal state * @data: ips driver structure * * Wake up every 5s or so and check whether we should adjust the power clamp. * Check CPU and GPU load to determine which needs adjustment. There are * several things to consider here: * - do we need to adjust up or down? * - is CPU busy? * - is GPU busy? * - is CPU in turbo? * - is GPU in turbo? * - is CPU or GPU preferred? (CPU is default) * * So, given the above, we do the following: * - up (TDP available) * - CPU not busy, GPU not busy - nothing * - CPU busy, GPU not busy - adjust CPU up * - CPU not busy, GPU busy - adjust GPU up * - CPU busy, GPU busy - adjust preferred unit up, taking headroom from * non-preferred unit if necessary * - down (at TDP limit) * - adjust both CPU and GPU down if possible * * |cpu+ gpu+ cpu+gpu- cpu-gpu+ cpu-gpu- * cpu < gpu < |cpu+gpu+ cpu+ gpu+ nothing * cpu < gpu >= |cpu+gpu-(mcp<) cpu+gpu-(mcp<) gpu- gpu- * cpu >= gpu < |cpu-gpu+(mcp<) cpu- cpu-gpu+(mcp<) cpu- * cpu >= gpu >=|cpu-gpu- cpu-gpu- cpu-gpu- cpu-gpu- * * Returns: %0
*/ staticint ips_adjust(void *data)
{ struct ips_driver *ips = data; unsignedlong flags;
/* * Adjust CPU and GPU clamps every 5s if needed. Doing it more * often isn't recommended due to ME interaction.
*/ do { bool cpu_busy = ips_cpu_busy(ips); bool gpu_busy = ips_gpu_busy(ips);
spin_lock_irqsave(&ips->turbo_status_lock, flags); if (ips->poll_turbo_status)
update_turbo_limits(ips);
spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
/* Update turbo status if necessary */ if (ips->cpu_turbo_enabled)
ips_enable_cpu_turbo(ips); else
ips_disable_cpu_turbo(ips);
if (ips->gpu_turbo_enabled)
ips_enable_gpu_turbo(ips); else
ips_disable_gpu_turbo(ips);
/* We're outside our comfort zone, crank them down */ if (mcp_exceeded(ips)) {
ips_cpu_lower(ips);
ips_gpu_lower(ips); goto sleep;
}
if (!cpu_exceeded(ips, 0) && cpu_busy)
ips_cpu_raise(ips); else
ips_cpu_lower(ips);
if (!mch_exceeded(ips) && gpu_busy)
ips_gpu_raise(ips); else
ips_gpu_lower(ips);
sleep:
schedule_timeout_interruptible(msecs_to_jiffies(IPS_ADJUST_PERIOD));
} while (!kthread_should_stop());
dev_dbg(ips->dev, "ips-adjust thread stopped\n");
return 0;
}
/* * Helpers for reading out temp/power values and calculating their * averages for the decision making and monitoring functions.
*/
static u16 calc_avg_temp(struct ips_driver *ips, u16 *array)
{
u64 total = 0; int i;
u16 avg;
for (i = 0; i < IPS_SAMPLE_COUNT; i++)
total += (u64)(array[i] * 100);
/** * ips_monitor - temp/power monitoring thread * @data: ips driver structure * * This is the main function for the IPS driver. It monitors power and * temperature in the MCP and adjusts CPU and GPU power clamps accordingly. * * We keep a 5s moving average of power consumption and temperature. Using * that data, along with CPU vs GPU preference, we adjust the power clamps * up or down. * * Returns: %0 on success or -errno on error
*/ staticint ips_monitor(void *data)
{ struct ips_driver *ips = data; unsignedlong seqno_timestamp, expire, last_msecs, last_sample_period; int i;
u32 *cpu_samples, *mchp_samples, old_cpu_power;
u16 *mcp_samples, *ctv1_samples, *ctv2_samples, *mch_samples;
u8 cur_seqno, last_seqno;
/* Start the adjustment thread now that we have data */
wake_up_process(ips->adjust);
/* * Ok, now we have an initial avg. From here on out, we track the * running avg using a decaying average calculation. This allows * us to reduce the sample frequency if the CPU and GPU are idle.
*/
old_cpu_power = thm_readl(THM_CEC);
schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
last_sample_period = IPS_SAMPLE_PERIOD;
timer_setup(&ips->timer, monitor_timeout, TIMER_DEFERRABLE); do {
u32 cpu_val, mch_val;
u16 val;
if (ips->second_cpu) { /* Processor 1 */
val = read_ctv(ips, 1);
ips->ctv2_avg_temp =
update_average_temp(ips->ctv2_avg_temp, val);
}
/* MCH */
val = read_mgtv(ips);
ips->mch_avg_temp = update_average_temp(ips->mch_avg_temp, val); /* Power */ if (ips->read_mch_val) {
mch_val = ips->read_mch_val();
ips->mch_avg_power =
update_average_power(ips->mch_avg_power,
mch_val);
}
/* * Make sure ME is updating thermal regs. * Note: * If it's been more than a second since the last update, * the ME is probably hung.
*/
cur_seqno = (thm_readl(THM_ITV) & ITV_ME_SEQNO_MASK) >>
ITV_ME_SEQNO_SHIFT; if (cur_seqno == last_seqno &&
time_after(jiffies, seqno_timestamp + HZ)) {
dev_warn(ips->dev, "ME failed to update for more than 1s, likely hung\n");
} else {
seqno_timestamp = get_jiffies_64();
last_seqno = cur_seqno;
}
/* Calculate actual sample period for power averaging */
last_sample_period = jiffies_to_msecs(jiffies) - last_msecs; if (!last_sample_period)
last_sample_period = 1;
} while (!kthread_should_stop());
/** * ips_irq_handler - handle temperature triggers and other IPS events * @irq: irq number * @arg: unused * * Handle temperature limit trigger events, generally by lowering the clamps. * If we're at a critical limit, we clamp back to the lowest possible value * to prevent emergency shutdown. * * Returns: IRQ_NONE or IRQ_HANDLED
*/ static irqreturn_t ips_irq_handler(int irq, void *arg)
{ struct ips_driver *ips = arg;
u8 tses = thm_readb(THM_TSES);
u8 tes = thm_readb(THM_TES);
/** * ips_detect_cpu - detect whether CPU supports IPS * @ips: IPS driver struct * * Walk our list and see if we're on a supported CPU. If we find one, * return the limits for it. * * Returns: the &ips_mcp_limits struct that matches the boot CPU or %NULL
*/ staticstruct ips_mcp_limits *ips_detect_cpu(struct ips_driver *ips)
{
u64 turbo_power, misc_en; struct ips_mcp_limits *limits = NULL;
u16 tdp;
if (!(boot_cpu_data.x86_vfm == INTEL_WESTMERE)) {
dev_info(ips->dev, "Non-IPS CPU detected.\n"); return NULL;
}
rdmsrq(IA32_MISC_ENABLE, misc_en); /* * If the turbo enable bit isn't set, we shouldn't try to enable/disable * turbo manually or we'll get an illegal MSR access, even though * turbo will still be available.
*/ if (misc_en & IA32_MISC_TURBO_EN)
ips->turbo_toggle_allowed = true; else
ips->turbo_toggle_allowed = false;
/* Sanity check TDP against CPU */ if (limits->core_power_limit != (tdp / 8) * 1000) {
dev_info(ips->dev, "CPU TDP doesn't match expected value (found %d, expected %d)\n",
tdp / 8, limits->core_power_limit / 1000);
limits->core_power_limit = (tdp / 8) * 1000;
}
return limits;
}
/** * ips_get_i915_syms - try to get GPU control methods from i915 driver * @ips: IPS driver * * The i915 driver exports several interfaces to allow the IPS driver to * monitor and control graphics turbo mode. If we can find them, we can * enable graphics turbo, otherwise we must disable it to avoid exceeding * thermal and power limits in the MCP. * * Returns: %true if the required symbols are found, else %false
*/ staticbool ips_get_i915_syms(struct ips_driver *ips)
{
ips->read_mch_val = symbol_get(i915_read_mch_val); if (!ips->read_mch_val) goto out_err;
ips->gpu_raise = symbol_get(i915_gpu_raise); if (!ips->gpu_raise) goto out_put_mch;
ips->gpu_lower = symbol_get(i915_gpu_lower); if (!ips->gpu_lower) goto out_put_raise;
ips->gpu_busy = symbol_get(i915_gpu_busy); if (!ips->gpu_busy) goto out_put_lower;
ips->gpu_turbo_disable = symbol_get(i915_gpu_turbo_disable); if (!ips->gpu_turbo_disable) goto out_put_busy;
void
ips_link_to_i915_driver(void)
{ /* We can't cleanly get at the various ips_driver structs from * this caller (the i915 driver), so just set a flag saying * that it's time to try getting the symbols again.
*/
late_i915_load = true;
}
EXPORT_SYMBOL_GPL(ips_link_to_i915_driver);
ips->limits = ips_detect_cpu(ips); if (!ips->limits) {
dev_info(&dev->dev, "IPS not supported on this CPU\n"); return -ENXIO;
}
ret = pcim_enable_device(dev); if (ret) {
dev_err(&dev->dev, "can't enable PCI device, aborting\n"); return ret;
}
ret = pcim_iomap_regions(dev, 1 << 0, pci_name(dev)); if (ret) {
dev_err(&dev->dev, "failed to map thermal regs, aborting\n"); return ret;
}
ips->regmap = pcim_iomap_table(dev)[0];
pci_set_drvdata(dev, ips);
tse = thm_readb(THM_TSE); if (tse != TSE_EN) {
dev_err(&dev->dev, "thermal device not enabled (0x%02x), aborting\n", tse); return -ENXIO;
}
trc = thm_readw(THM_TRC);
trc_required_mask = TRC_CORE1_EN | TRC_CORE_PWR | TRC_MCH_EN; if ((trc & trc_required_mask) != trc_required_mask) {
dev_err(&dev->dev, "thermal reporting for required devices not enabled, aborting\n"); return -ENXIO;
}
if (trc & TRC_CORE2_EN)
ips->second_cpu = true;
update_turbo_limits(ips);
dev_dbg(&dev->dev, "max cpu power clamp: %dW\n",
ips->mcp_power_limit / 10);
dev_dbg(&dev->dev, "max core power clamp: %dW\n",
ips->core_power_limit / 10); /* BIOS may update limits at runtime */ if (thm_readl(THM_PSC) & PSP_PBRT)
ips->poll_turbo_status = true;
if (!ips_get_i915_syms(ips)) {
dev_info(&dev->dev, "failed to get i915 symbols, graphics turbo disabled until i915 loads\n");
ips->gpu_turbo_enabled = false;
} else {
dev_dbg(&dev->dev, "graphics turbo enabled\n");
ips->gpu_turbo_enabled = true;
}
/* * Check PLATFORM_INFO MSR to make sure this chip is * turbo capable.
*/
rdmsrq(PLATFORM_INFO, platform_info); if (!(platform_info & PLATFORM_TDP)) {
dev_err(&dev->dev, "platform indicates TDP override unavailable, aborting\n"); return -ENODEV;
}
/* * IRQ handler for ME interaction * Note: don't use MSI here as the PCH has bugs.
*/
ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_INTX); if (ret < 0) return ret;
ips->irq = pci_irq_vector(dev, 0);
ret = request_irq(ips->irq, ips_irq_handler, IRQF_SHARED, "ips", ips); if (ret) {
dev_err(&dev->dev, "request irq failed, aborting\n"); return ret;
}
/* Create thermal adjust thread */
ips->adjust = kthread_create(ips_adjust, ips, "ips-adjust"); if (IS_ERR(ips->adjust)) {
dev_err(&dev->dev, "failed to create thermal adjust thread, aborting\n");
ret = -ENOMEM; goto error_free_irq;
}
/* * Set up the work queue and monitor thread. The monitor thread * will wake up ips_adjust thread.
*/
ips->monitor = kthread_run(ips_monitor, ips, "ips-monitor"); if (IS_ERR(ips->monitor)) {
dev_err(&dev->dev, "failed to create thermal monitor thread, aborting\n");
ret = -ENOMEM; goto error_thread_cleanup;
}
/* Release i915 driver */ if (ips->read_mch_val)
symbol_put(i915_read_mch_val); if (ips->gpu_raise)
symbol_put(i915_gpu_raise); if (ips->gpu_lower)
symbol_put(i915_gpu_lower); if (ips->gpu_busy)
symbol_put(i915_gpu_busy); if (ips->gpu_turbo_disable)
symbol_put(i915_gpu_turbo_disable);
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.