/* * SPR has different layout for Psys Domain PowerLimit registers. * There are 17 bits of PL1 and PL2 instead of 15 bits. * The Enable bits and TimeWindow bits are also shifted as a result.
*/ #define PSYS_POWER_LIMIT1_MASK 0x1FFFF #define PSYS_POWER_LIMIT1_ENABLE BIT(17)
staticint get_pl_lock_prim(struct rapl_domain *rd, int pl)
{ if (rd->rp->priv->type == RAPL_IF_TPMI) { if (pl == POWER_LIMIT1) return PL1_LOCK; if (pl == POWER_LIMIT2) return PL2_LOCK; if (pl == POWER_LIMIT4) return PL4_LOCK;
}
/* MSR/MMIO Interface doesn't have Lock bit for PL4 */ if (pl == POWER_LIMIT4) return -EINVAL;
/* * Power Limit register that supports two power limits has a different * bit position for the Lock bit.
*/ if (rd->rp->priv->limits[rd->id] & BIT(POWER_LIMIT2)) return FW_HIGH_LOCK; return FW_LOCK;
}
staticint get_pl_prim(struct rapl_domain *rd, int pl, enum pl_prims prim)
{ switch (pl) { case POWER_LIMIT1: if (prim == PL_ENABLE) return PL1_ENABLE; if (prim == PL_CLAMP && rd->rp->priv->type != RAPL_IF_TPMI) return PL1_CLAMP; if (prim == PL_LIMIT) return POWER_LIMIT1; if (prim == PL_TIME_WINDOW) return TIME_WINDOW1; if (prim == PL_MAX_POWER) return THERMAL_SPEC_POWER; if (prim == PL_LOCK) return get_pl_lock_prim(rd, pl); return -EINVAL; case POWER_LIMIT2: if (prim == PL_ENABLE) return PL2_ENABLE; if (prim == PL_CLAMP && rd->rp->priv->type != RAPL_IF_TPMI) return PL2_CLAMP; if (prim == PL_LIMIT) return POWER_LIMIT2; if (prim == PL_TIME_WINDOW) return TIME_WINDOW2; if (prim == PL_MAX_POWER) return MAX_POWER; if (prim == PL_LOCK) return get_pl_lock_prim(rd, pl); return -EINVAL; case POWER_LIMIT4: if (prim == PL_LIMIT) return POWER_LIMIT4; if (prim == PL_ENABLE) return PL4_ENABLE; /* PL4 would be around two times PL2, use same prim as PL2. */ if (prim == PL_MAX_POWER) return MAX_POWER; if (prim == PL_LOCK) return get_pl_lock_prim(rd, pl); return -EINVAL; default: return -EINVAL;
}
}
/* per domain data. used to describe individual knobs such that access function * can be consolidated into one instead of many inline functions.
*/ struct rapl_primitive_info { constchar *name;
u64 mask; int shift; enum rapl_domain_reg_id id; enum unit_type unit;
u32 flag;
};
#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
.name = #p, \
.mask = m, \
.shift = s, \
.id = i, \
.unit = u, \
.flag = f \
}
/* prevent CPU hotplug, make sure the RAPL domain does not go * away while reading the counter.
*/
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
*energy_raw = energy_now;
cpus_read_unlock();
/* package zone is the last zone of a package, we can free * memory here since all children has been unregistered.
*/ if (rd->id == RAPL_DOMAIN_PACKAGE) {
kfree(rd);
rp->domains = NULL;
}
return 0;
}
staticint find_nr_power_limit(struct rapl_domain *rd)
{ int i, nr_pl = 0;
for (i = 0; i < NR_POWER_LIMITS; i++) { if (is_pl_valid(rd, i))
nr_pl++;
}
/* * Constraint index used by powercap can be different than power limit (PL) * index in that some PLs maybe missing due to non-existent MSRs. So we * need to convert here by finding the valid PLs only (name populated).
*/ staticint contraint_to_pl(struct rapl_domain *rd, int cid)
{ int i, j;
for (i = POWER_LIMIT1, j = 0; i < NR_POWER_LIMITS; i++) { if (is_pl_valid(rd, i) && j++ == cid) {
pr_debug("%s: index %d\n", __func__, i); return i;
}
}
pr_err("Cannot find matching power limit for constraint %d\n", cid);
return -EINVAL;
}
staticint set_power_limit(struct powercap_zone *power_zone, int cid,
u64 power_limit)
{ struct rapl_domain *rd; struct rapl_package *rp; int ret = 0; int id;
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid);
rp = rd->rp;
ret = rapl_write_pl_data(rd, id, PL_LIMIT, power_limit); if (!ret)
package_power_limit_irq_save(rp);
cpus_read_unlock(); return ret;
}
staticint get_current_power_limit(struct powercap_zone *power_zone, int cid,
u64 *data)
{ struct rapl_domain *rd;
u64 val; int ret = 0; int id;
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid);
ret = rapl_read_pl_data(rd, id, PL_LIMIT, true, &val); if (!ret)
*data = val;
cpus_read_unlock();
return ret;
}
staticint set_time_window(struct powercap_zone *power_zone, int cid,
u64 window)
{ struct rapl_domain *rd; int ret = 0; int id;
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid);
ret = rapl_write_pl_data(rd, id, PL_TIME_WINDOW, window);
cpus_read_unlock(); return ret;
}
staticint get_time_window(struct powercap_zone *power_zone, int cid,
u64 *data)
{ struct rapl_domain *rd;
u64 val; int ret = 0; int id;
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid);
ret = rapl_read_pl_data(rd, id, PL_TIME_WINDOW, true, &val); if (!ret)
*data = val;
cpus_read_unlock();
return ret;
}
staticconstchar *get_constraint_name(struct powercap_zone *power_zone, int cid)
{ struct rapl_domain *rd; int id;
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid); if (id >= 0) return rd->rpl[id].name;
return NULL;
}
staticint get_max_power(struct powercap_zone *power_zone, int cid, u64 *data)
{ struct rapl_domain *rd;
u64 val; int ret = 0; int id;
cpus_read_lock();
rd = power_zone_to_rapl_domain(power_zone);
id = contraint_to_pl(rd, cid);
ret = rapl_read_pl_data(rd, id, PL_MAX_POWER, true, &val); if (!ret)
*data = val;
/* As a generalization rule, PL4 would be around two times PL2. */ if (id == POWER_LIMIT4)
*data = *data * 2;
/* Return the id used for read_raw/write_raw callback */ staticint get_rid(struct rapl_package *rp)
{ return rp->lead_cpu >= 0 ? rp->lead_cpu : rp->id;
}
/* called after domain detection and package level data are set */ staticvoid rapl_init_domains(struct rapl_package *rp)
{ enum rapl_domain_type i; enum rapl_domain_reg_id j; struct rapl_domain *rd = rp->domains;
for (i = 0; i < RAPL_DOMAIN_MAX; i++) { unsignedint mask = rp->domain_map & (1 << i); int t;
switch (type) { case POWER_UNIT:
units = rd->power_unit; break; case ENERGY_UNIT:
scale = ENERGY_UNIT_SCALE;
units = rd->energy_unit; break; case TIME_UNIT: return defaults->compute_time_window(rd, value, to_raw); case ARBITRARY_UNIT: default: return value;
}
if (to_raw) return div64_u64(value, units) * scale;
switch (prim) { case POWER_LIMIT1: return PSYS_POWER_LIMIT1; case POWER_LIMIT2: return PSYS_POWER_LIMIT2; case PL1_ENABLE: return PSYS_PL1_ENABLE; case PL2_ENABLE: return PSYS_PL2_ENABLE; case TIME_WINDOW1: return PSYS_TIME_WINDOW1; case TIME_WINDOW2: return PSYS_TIME_WINDOW2; default: return prim;
}
}
/* Read primitive data based on its related struct rapl_primitive_info. * if xlate flag is set, return translated data based on data units, i.e. * time, energy, and power. * RAPL MSRs are non-architectual and are laid out not consistently across * domains. Here we use primitive info to allow writing consolidated access * functions. * For a given primitive, it is processed by MSR mask and shift. Unit conversion * is pre-assigned based on RAPL unit MSRs read at init time. * 63-------------------------- 31--------------------------- 0 * | xxxxx (mask) | * | |<- shift ----------------| * 63-------------------------- 31--------------------------- 0
*/ staticint rapl_read_data_raw(struct rapl_domain *rd, enum rapl_primitives prim, bool xlate, u64 *data)
{
u64 value; enum rapl_primitives prim_fixed = prim_fixups(rd, prim); struct rapl_primitive_info *rpi = get_rpi(rd->rp, prim_fixed); struct reg_action ra;
if (!rpi || !rpi->name || rpi->flag & RAPL_PRIMITIVE_DUMMY) return -EINVAL;
ra.reg = rd->regs[rpi->id]; if (!ra.reg.val) return -EINVAL;
/* non-hardware data are collected by the polling thread */ if (rpi->flag & RAPL_PRIMITIVE_DERIVED) {
*data = rd->rdd.primitives[prim]; return 0;
}
ra.mask = rpi->mask;
if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra)) {
pr_debug("failed to read reg 0x%llx for %s:%s\n", ra.reg.val, rd->rp->name, rd->name); return -EIO;
}
staticint rapl_write_pl_data(struct rapl_domain *rd, int pl, enum pl_prims pl_prim, unsignedlonglong value)
{ enum rapl_primitives prim = get_pl_prim(rd, pl, pl_prim);
if (!is_pl_valid(rd, pl)) return -EINVAL;
if (rd->rpl[pl].locked) {
pr_debug("%s:%s:%s locked by BIOS\n", rd->rp->name, rd->name, pl_names[pl]); return -EACCES;
}
return rapl_write_data_raw(rd, prim, value);
} /* * Raw RAPL data stored in MSRs are in certain scales. We need to * convert them into standard units based on the units reported in * the RAPL unit MSRs. This is specific to CPUs as the method to * calculate units differ on different CPUs. * We convert the units to below format based on CPUs. * i.e. * energy unit: picoJoules : Represented in picoJoules by default * power unit : microWatts : Represented in milliWatts by default * time unit : microseconds: Represented in seconds by default
*/ staticint rapl_check_unit_core(struct rapl_domain *rd)
{ struct reg_action ra;
u32 value;
ra.reg = rd->regs[RAPL_DOMAIN_REG_UNIT];
ra.mask = ~0; if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra)) {
pr_err("Failed to read power unit REG 0x%llx on %s:%s, exit.\n",
ra.reg.val, rd->rp->name, rd->name); return -ENODEV;
}
/* save the state of PLN irq mask bit before disabling it */
rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h); if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
}
l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
}
/* REVISIT: * When package power limit is set artificially low by RAPL, LVT * thermal interrupt for package power limit should be ignored * since we are not really exceeding the real limit. The intention * is to avoid excessive interrupts while we are trying to save power. * A useful feature might be routing the package_power_limit interrupt * to userspace via eventfd. once we have a usecase, this is simple * to do by adding an atomic notifier.
*/
staticvoid package_power_limit_irq_save(struct rapl_package *rp)
{ if (rp->lead_cpu < 0) return;
if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) return;
/* * Restore per package power limit interrupt enable state. Called from cpu * hotplug code on package removal.
*/ staticvoid package_power_limit_irq_restore(struct rapl_package *rp)
{
u32 l, h;
if (rp->lead_cpu < 0) return;
if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) return;
/* irq enable state not saved, nothing to restore */ if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) return;
staticvoid set_floor_freq_default(struct rapl_domain *rd, bool mode)
{ int i;
/* always enable clamp such that p-state can go below OS requested * range. power capping priority over guranteed frequency.
*/
rapl_write_pl_data(rd, POWER_LIMIT1, PL_CLAMP, mode);
for (i = POWER_LIMIT2; i < NR_POWER_LIMITS; i++) {
rapl_write_pl_data(rd, i, PL_ENABLE, mode);
rapl_write_pl_data(rd, i, PL_CLAMP, mode);
}
}
static u64 rapl_compute_time_window_atom(struct rapl_domain *rd, u64 value, bool to_raw)
{ /* * Atom time unit encoding is straight forward val * time_unit, * where time_unit is default to 1 sec. Never 0.
*/ if (!to_raw) return (value) ? value * rd->time_unit : rd->time_unit;
value = div64_u64(value, rd->time_unit);
return value;
}
/* TPMI Unit register has different layout */ #define TPMI_POWER_UNIT_OFFSET POWER_UNIT_OFFSET #define TPMI_POWER_UNIT_MASK POWER_UNIT_MASK #define TPMI_ENERGY_UNIT_OFFSET 0x06 #define TPMI_ENERGY_UNIT_MASK 0x7C0 #define TPMI_TIME_UNIT_OFFSET 0x0C #define TPMI_TIME_UNIT_MASK 0xF000
/* Update the domain data of the new package */
rapl_update_domain_data(rp);
/* first we register package domain as the parent zone */ for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { if (rd->id == RAPL_DOMAIN_PACKAGE) {
nr_pl = find_nr_power_limit(rd);
pr_debug("register package domain %s\n", rp->name);
power_zone = powercap_register_zone(&rd->power_zone,
rp->priv->control_type, rp->name,
NULL, &zone_ops[rd->id], nr_pl,
&constraint_ops); if (IS_ERR(power_zone)) {
pr_debug("failed to register power zone %s\n",
rp->name); return PTR_ERR(power_zone);
} /* track parent zone in per package/socket data */
rp->power_zone = power_zone; /* done, only one package domain per socket */ break;
}
} if (!power_zone) {
pr_err("no package domain found, unknown topology!\n"); return -ENODEV;
} /* now register domains as children of the socket/package */ for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { struct powercap_zone *parent = rp->power_zone;
if (rd->id == RAPL_DOMAIN_PACKAGE) continue; if (rd->id == RAPL_DOMAIN_PLATFORM)
parent = NULL; /* number of power limits per domain varies */
nr_pl = find_nr_power_limit(rd);
power_zone = powercap_register_zone(&rd->power_zone,
rp->priv->control_type,
rd->name, parent,
&zone_ops[rd->id], nr_pl,
&constraint_ops);
if (IS_ERR(power_zone)) {
pr_debug("failed to register power_zone, %s:%s\n",
rp->name, rd->name);
ret = PTR_ERR(power_zone); goto err_cleanup;
}
} return 0;
err_cleanup: /* * Clean up previously initialized domains within the package if we * failed after the first domain setup.
*/ while (--rd >= rp->domains) {
pr_debug("unregister %s domain %s\n", rp->name, rd->name);
powercap_unregister_zone(rp->priv->control_type,
&rd->power_zone);
}
switch (domain) { case RAPL_DOMAIN_PACKAGE: case RAPL_DOMAIN_PP0: case RAPL_DOMAIN_PP1: case RAPL_DOMAIN_DRAM: case RAPL_DOMAIN_PLATFORM:
ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS]; break; default:
pr_err("invalid domain id %d\n", domain); return -EINVAL;
} /* make sure domain counters are available and contains non-zero * values, otherwise skip it.
*/
ra.mask = ENERGY_STATUS_MASK; if (rp->priv->read_raw(get_rid(rp), &ra) || !ra.value) return -ENODEV;
return 0;
}
/* * Get per domain energy/power/time unit. * RAPL Interfaces without per domain unit register will use the package * scope unit register to set per domain units.
*/ staticint rapl_get_domain_unit(struct rapl_domain *rd)
{ struct rapl_defaults *defaults = get_defaults(rd->rp); int ret;
if (!rd->regs[RAPL_DOMAIN_REG_UNIT].val) { if (!rd->rp->priv->reg_unit.val) {
pr_err("No valid Unit register found\n"); return -ENODEV;
}
rd->regs[RAPL_DOMAIN_REG_UNIT] = rd->rp->priv->reg_unit;
}
if (!defaults->check_unit) {
pr_err("missing .check_unit() callback\n"); return -ENODEV;
}
ret = defaults->check_unit(rd); if (ret) return ret;
/* * Check if power limits are available. Two cases when they are not available: * 1. Locked by BIOS, in this case we still provide read-only access so that * users can see what limit is set by the BIOS. * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not * exist at all. In this case, we do not show the constraints in powercap. * * Called after domains are detected and initialized.
*/ staticvoid rapl_detect_powerlimit(struct rapl_domain *rd)
{
u64 val64; int i;
for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) { if (!rapl_read_pl_data(rd, i, PL_LOCK, false, &val64)) { if (val64) {
rd->rpl[i].locked = true;
pr_info("%s:%s:%s locked by BIOS\n",
rd->rp->name, rd->name, pl_names[i]);
}
}
if (rapl_read_pl_data(rd, i, PL_LIMIT, false, &val64))
rd->rpl[i].name = NULL;
}
}
/* Detect active and valid domains for the given CPU, caller must * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
*/ staticint rapl_detect_domains(struct rapl_package *rp)
{ struct rapl_domain *rd; int i;
for (i = 0; i < RAPL_DOMAIN_MAX; i++) { /* use physical package id to read counters */ if (!rapl_check_domain(i, rp)) {
rp->domain_map |= 1 << i;
pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
}
}
rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX); if (!rp->nr_domains) {
pr_debug("no valid rapl domains found in %s\n", rp->name); return -ENODEV;
}
pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
rp->domains = kcalloc(rp->nr_domains, sizeof(struct rapl_domain),
GFP_KERNEL); if (!rp->domains) return -ENOMEM;
/* * Support for RAPL PMU * * Register a PMU if any of the registered RAPL Packages have the requirement * of exposing its energy counters via Perf PMU. * * PMU Name: * power * * Events: * Name Event id RAPL Domain * energy_cores 0x01 RAPL_DOMAIN_PP0 * energy_pkg 0x02 RAPL_DOMAIN_PACKAGE * energy_ram 0x03 RAPL_DOMAIN_DRAM * energy_gpu 0x04 RAPL_DOMAIN_PP1 * energy_psys 0x05 RAPL_DOMAIN_PLATFORM * * Unit: * Joules * * Scale: * 2.3283064365386962890625e-10 * The same RAPL domain in different RAPL Packages may have different * energy units. Use 2.3283064365386962890625e-10 (2^-32) Joules as * the fixed unit for all energy counters, and covert each hardware * counter increase to N times of PMU event counter increases. * * This is fully compatible with the current MSR RAPL PMU. This means that * userspace programs like turbostat can use the same code to handle RAPL Perf * PMU, no matter what RAPL Interface driver (MSR/TPMI, etc) is running * underlying on the platform. * * Note that RAPL Packages can be probed/removed dynamically, and the events * supported by each TPMI RAPL device can be different. Thus the RAPL PMU * support is done on demand, which means * 1. PMU is registered only if it is needed by a RAPL Package. PMU events for * unsupported counters are not exposed. * 2. PMU is unregistered and registered when a new RAPL Package is probed and * supports new counters that are not supported by current PMU. * 3. PMU is unregistered when all registered RAPL Packages don't need PMU.
*/
struct rapl_pmu { struct pmu pmu; /* Perf PMU structure */
u64 timer_ms; /* Maximum expiration time to avoid counter overflow */ unsignedlong domain_map; /* Events supported by current registered PMU */ bool registered; /* Whether the PMU has been registered or not */
};
staticstruct rapl_pmu rapl_pmu;
/* PMU helpers */
staticint get_pmu_cpu(struct rapl_package *rp)
{ int cpu;
if (!rp->has_pmu) return nr_cpu_ids;
/* Only TPMI RAPL is supported for now */ if (rp->priv->type != RAPL_IF_TPMI) return nr_cpu_ids;
/* TPMI RAPL uses any CPU in the package for PMU */
for_each_online_cpu(cpu) if (topology_physical_package_id(cpu) == rp->id) return cpu;
return nr_cpu_ids;
}
staticbool is_rp_pmu_cpu(struct rapl_package *rp, int cpu)
{ if (!rp->has_pmu) returnfalse;
/* Only TPMI RAPL is supported for now */ if (rp->priv->type != RAPL_IF_TPMI) returnfalse;
/* TPMI RAPL uses any CPU in the package for PMU */ return topology_physical_package_id(cpu) == rp->id;
}
/* * Follow the generic code to drain hwc->prev_count. * The loop is not expected to run for multiple times.
*/
prev_raw_count = local64_read(&hwc->prev_count); do {
new_raw_count = event_read_counter(event);
} while (!local64_try_cmpxchg(&hwc->prev_count,
&prev_raw_count, new_raw_count));
/* * Now we have the new raw value and have updated the prev * timestamp already. We can now calculate the elapsed delta * (event-)time and add that to the generic event.
*/
delta = new_raw_count - prev_raw_count;
/* * Scale delta to smallest unit (2^-32) * users must then scale back: count * 1/(1e9*2^32) to get Joules * or use ldexp(count, -32). * Watts = Joules/Time delta
*/
sdelta = delta * data->scale[event->hw.flags];
/* Mark event as deactivated and stopped */ if (!(hwc->state & PERF_HES_STOPPED)) {
WARN_ON_ONCE(data->n_active <= 0); if (--data->n_active == 0)
hrtimer_cancel(&data->hrtimer);
/* Check if update of sw counter is necessary */ if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { /* * Drain the remaining delta count out of a event * that we are disabling:
*/
rapl_event_update(event);
hwc->state |= PERF_HES_UPTODATE;
}
/* Only look at RAPL events */ if (event->attr.type != event->pmu->type) return -ENOENT;
/* Check for supported events only */ if (!cfg || cfg >= PERF_RAPL_MAX) return -EINVAL;
if (event->cpu < 0) return -EINVAL;
/* Find out which Package the event belongs to */
list_for_each_entry(pos, &rapl_packages, plist) { if (is_rp_pmu_cpu(pos, event->cpu)) {
rp = pos; break;
}
} if (!rp) return -ENODEV;
/* Find out which RAPL Domain the event belongs to */
domain = event_to_domain[cfg];
event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
event->pmu_private = rp; /* Which package */
event->hw.flags = domain; /* Which domain */
event->hw.idx = -1; /* Find out the index in rp->domains[] to get domain pointer */ for (idx = 0; idx < rp->nr_domains; idx++) { if (rp->domains[idx].id == domain) {
event->hw.idx = idx; break;
}
}
/* * There are no default events, but we need to create "events" group (with * empty attrs) before updating it with detected events.
*/ staticstruct attribute *attrs_empty[] = {
NULL,
};
static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
{ struct rapl_package *rp;
cpumask_var_t cpu_mask; int cpu; int ret;
if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL)) return -ENOMEM;
cpus_read_lock();
cpumask_clear(cpu_mask);
/* Choose a cpu for each RAPL Package */
list_for_each_entry(rp, &rapl_packages, plist) {
cpu = get_pmu_cpu(rp); if (cpu < nr_cpu_ids)
cpumask_set_cpu(cpu, cpu_mask);
}
cpus_read_unlock();
ret = cpumap_print_to_pagebuf(true, buf, cpu_mask);
staticint rapl_pmu_update(struct rapl_package *rp)
{ int ret = 0;
/* Return if PMU already covers all events supported by current RAPL Package */ if (rapl_pmu.registered && !(rp->domain_map & (~rapl_pmu.domain_map))) goto end;
/* Unregister previous registered PMU */ if (rapl_pmu.registered)
perf_pmu_unregister(&rapl_pmu.pmu);
int rapl_package_add_pmu(struct rapl_package *rp)
{ struct rapl_package_pmu_data *data = &rp->pmu_data; int idx;
if (rp->has_pmu) return -EEXIST;
guard(cpus_read_lock)();
for (idx = 0; idx < rp->nr_domains; idx++) { struct rapl_domain *rd = &rp->domains[idx]; int domain = rd->id;
u64 val;
if (!test_bit(domain, &rp->domain_map)) continue;
/* * The RAPL PMU granularity is 2^-32 Joules * data->scale[]: times of 2^-32 Joules for each ENERGY COUNTER increase
*/
val = rd->energy_unit * (1ULL << 32);
do_div(val, ENERGY_UNIT_SCALE * 1000000);
data->scale[domain] = val;
if (!rapl_pmu.timer_ms) { struct rapl_primitive_info *rpi = get_rpi(rp, ENERGY_COUNTER);
/* * Calculate the timer rate: * Use reference of 200W for scaling the timeout to avoid counter * overflows. * * max_count = rpi->mask >> rpi->shift + 1 * max_energy_pj = max_count * rd->energy_unit * max_time_sec = (max_energy_pj / 1000000000) / 200w * * rapl_pmu.timer_ms = max_time_sec * 1000 / 2
*/
val = (rpi->mask >> rpi->shift) + 1;
val *= rd->energy_unit;
do_div(val, 1000000 * 200 * 2);
rapl_pmu.timer_ms = val;
pr_debug("%llu ms overflow timer\n", rapl_pmu.timer_ms);
}
/* * RAPL Package energy counter scope: * 1. AMD/HYGON platforms use per-PKG package energy counter * 2. For Intel platforms * 2.1 CLX-AP platform has per-DIE package energy counter * 2.2 Other platforms that uses MSR RAPL are single die systems so the * package energy counter can be considered as per-PKG/per-DIE, * here it is considered as per-DIE. * 2.3 New platforms that use TPMI RAPL doesn't care about the * scope because they are not MSR/CPU based.
*/ #define rapl_msrs_are_pkg_scope() \
(boot_cpu_data.x86_vendor == X86_VENDOR_AMD || \
boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
/* caller to ensure CPU hotplug lock is held */ struct rapl_package *rapl_find_package_domain_cpuslocked(int id, struct rapl_if_priv *priv, bool id_is_cpu)
{ struct rapl_package *rp; int uid;
if (id_is_cpu) {
uid = rapl_msrs_are_pkg_scope() ?
topology_physical_package_id(id) : topology_logical_die_id(id); if (uid < 0) {
pr_err("topology_logical_(package/die)_id() returned a negative value"); return NULL;
}
} else
uid = id;
/* called from CPU hotplug notifier, hotplug lock held */ struct rapl_package *rapl_add_package_cpuslocked(int id, struct rapl_if_priv *priv, bool id_is_cpu)
{ struct rapl_package *rp; int ret;
rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL); if (!rp) return ERR_PTR(-ENOMEM);
rp->priv = priv;
ret = rapl_config(rp); if (ret) goto err_free_package;
/* check if the package contains valid domains */ if (rapl_detect_domains(rp)) {
ret = -ENODEV; goto err_free_package;
}
ret = rapl_package_register_powercap(rp); if (!ret) {
INIT_LIST_HEAD(&rp->plist);
list_add(&rp->plist, &rapl_packages); return rp;
}
cpus_read_lock();
list_for_each_entry(rp, &rapl_packages, plist) { if (!rp->power_zone) continue;
rd = power_zone_to_rapl_domain(rp->power_zone); for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) if (rd->rpl[i].last_power_limit)
rapl_write_pl_data(rd, i, PL_LIMIT,
--> --------------------
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.