| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Common code for Intel Running Average Power Limit (RAPL) support. |
| 4 |
* Copyright (c) 2019, Intel Corporation. |
| 5 |
*/ |
| 6 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 |
|
| 8 |
#include <linux/bitmap.h> |
| 9 |
#include <linux/cleanup.h> |
| 10 |
#include <linux/cpu.h> |
| 11 |
#include <linux/delay.h> |
| 12 |
#include <linux/device.h> |
| 13 |
#include <linux/intel_rapl.h> |
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/list.h> |
| 16 |
#include <linux/log2.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <linux/nospec.h> |
| 19 |
#include <linux/perf_event.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/powercap.h> |
| 22 |
#include <linux/processor.h> |
| 23 |
#include <linux/slab.h> |
| 24 |
#include <linux/suspend.h> |
| 25 |
#include <linux/sysfs.h> |
| 26 |
#include <linux/types.h> |
| 27 |
|
| 28 |
#include <asm/cpu_device_id.h> |
| 29 |
#include <asm/intel-family.h> |
| 30 |
#include <asm/iosf_mbi.h> |
| 31 |
#include <asm/msr.h> |
| 32 |
|
| 33 |
/* bitmasks for RAPL MSRs, used by primitive access functions */ |
| 34 |
#define ENERGY_STATUS_MASK 0xffffffff |
| 35 |
|
| 36 |
#define POWER_LIMIT1_MASK 0x7FFF |
| 37 |
#define POWER_LIMIT1_ENABLE BIT(15) |
| 38 |
#define POWER_LIMIT1_CLAMP BIT(16) |
| 39 |
|
| 40 |
#define POWER_LIMIT2_MASK (0x7FFFULL<<32) |
| 41 |
#define POWER_LIMIT2_ENABLE BIT_ULL(47) |
| 42 |
#define POWER_LIMIT2_CLAMP BIT_ULL(48) |
| 43 |
#define POWER_HIGH_LOCK BIT_ULL(63) |
| 44 |
#define POWER_LOW_LOCK BIT(31) |
| 45 |
|
| 46 |
#define POWER_LIMIT4_MASK 0x1FFF |
| 47 |
|
| 48 |
#define TIME_WINDOW1_MASK (0x7FULL<<17) |
| 49 |
#define TIME_WINDOW2_MASK (0x7FULL<<49) |
| 50 |
|
| 51 |
#define POWER_UNIT_OFFSET 0 |
| 52 |
#define POWER_UNIT_MASK 0x0F |
| 53 |
|
| 54 |
#define ENERGY_UNIT_OFFSET 0x08 |
| 55 |
#define ENERGY_UNIT_MASK 0x1F00 |
| 56 |
|
| 57 |
#define TIME_UNIT_OFFSET 0x10 |
| 58 |
#define TIME_UNIT_MASK 0xF0000 |
| 59 |
|
| 60 |
#define POWER_INFO_MAX_MASK (0x7fffULL<<32) |
| 61 |
#define POWER_INFO_MIN_MASK (0x7fffULL<<16) |
| 62 |
#define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48) |
| 63 |
#define POWER_INFO_THERMAL_SPEC_MASK 0x7fff |
| 64 |
|
| 65 |
#define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff |
| 66 |
#define PP_POLICY_MASK 0x1F |
| 67 |
|
| 68 |
/* |
| 69 |
* SPR has different layout for Psys Domain PowerLimit registers. |
| 70 |
* There are 17 bits of PL1 and PL2 instead of 15 bits. |
| 71 |
* The Enable bits and TimeWindow bits are also shifted as a result. |
| 72 |
*/ |
| 73 |
#define PSYS_POWER_LIMIT1_MASK 0x1FFFF |
| 74 |
#define PSYS_POWER_LIMIT1_ENABLE BIT(17) |
| 75 |
|
| 76 |
#define PSYS_POWER_LIMIT2_MASK (0x1FFFFULL<<32) |
| 77 |
#define PSYS_POWER_LIMIT2_ENABLE BIT_ULL(49) |
| 78 |
|
| 79 |
#define PSYS_TIME_WINDOW1_MASK (0x7FULL<<19) |
| 80 |
#define PSYS_TIME_WINDOW2_MASK (0x7FULL<<51) |
| 81 |
|
| 82 |
/* bitmasks for RAPL TPMI, used by primitive access functions */ |
| 83 |
#define TPMI_POWER_LIMIT_MASK 0x3FFFF |
| 84 |
#define TPMI_POWER_LIMIT_ENABLE BIT_ULL(62) |
| 85 |
#define TPMI_TIME_WINDOW_MASK (0x7FULL<<18) |
| 86 |
#define TPMI_INFO_SPEC_MASK 0x3FFFF |
| 87 |
#define TPMI_INFO_MIN_MASK (0x3FFFFULL << 18) |
| 88 |
#define TPMI_INFO_MAX_MASK (0x3FFFFULL << 36) |
| 89 |
#define TPMI_INFO_MAX_TIME_WIN_MASK (0x7FULL << 54) |
| 90 |
|
| 91 |
/* Non HW constants */ |
| 92 |
#define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */ |
| 93 |
#define RAPL_PRIMITIVE_DUMMY BIT(2) |
| 94 |
|
| 95 |
#define TIME_WINDOW_MAX_MSEC 40000 |
| 96 |
#define TIME_WINDOW_MIN_MSEC 250 |
| 97 |
#define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */ |
| 98 |
enum unit_type { |
| 99 |
ARBITRARY_UNIT, /* no translation */ |
| 100 |
POWER_UNIT, |
| 101 |
ENERGY_UNIT, |
| 102 |
TIME_UNIT, |
| 103 |
}; |
| 104 |
|
| 105 |
/* per domain data, some are optional */ |
| 106 |
#define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2) |
| 107 |
|
| 108 |
#define DOMAIN_STATE_INACTIVE BIT(0) |
| 109 |
#define DOMAIN_STATE_POWER_LIMIT_SET BIT(1) |
| 110 |
|
| 111 |
static const char *pl_names[NR_POWER_LIMITS] = { |
| 112 |
[POWER_LIMIT1] = "long_term", |
| 113 |
[POWER_LIMIT2] = "short_term", |
| 114 |
[POWER_LIMIT4] = "peak_power", |
| 115 |
}; |
| 116 |
|
| 117 |
enum pl_prims { |
| 118 |
PL_ENABLE, |
| 119 |
PL_CLAMP, |
| 120 |
PL_LIMIT, |
| 121 |
PL_TIME_WINDOW, |
| 122 |
PL_MAX_POWER, |
| 123 |
PL_LOCK, |
| 124 |
}; |
| 125 |
|
| 126 |
static bool is_pl_valid(struct rapl_domain *rd, int pl) |
| 127 |
{ |
| 128 |
if (pl < POWER_LIMIT1 || pl > POWER_LIMIT4) |
| 129 |
return false; |
| 130 |
return rd->rpl[pl].name ? true : false; |
| 131 |
} |
| 132 |
|
| 133 |
static int get_pl_lock_prim(struct rapl_domain *rd, int pl) |
| 134 |
{ |
| 135 |
if (rd->rp->priv->type == RAPL_IF_TPMI) { |
| 136 |
if (pl == POWER_LIMIT1) |
| 137 |
return PL1_LOCK; |
| 138 |
if (pl == POWER_LIMIT2) |
| 139 |
return PL2_LOCK; |
| 140 |
if (pl == POWER_LIMIT4) |
| 141 |
return PL4_LOCK; |
| 142 |
} |
| 143 |
|
| 144 |
/* MSR/MMIO Interface doesn't have Lock bit for PL4 */ |
| 145 |
if (pl == POWER_LIMIT4) |
| 146 |
return -EINVAL; |
| 147 |
|
| 148 |
/* |
| 149 |
* Power Limit register that supports two power limits has a different |
| 150 |
* bit position for the Lock bit. |
| 151 |
*/ |
| 152 |
if (rd->rp->priv->limits[rd->id] & BIT(POWER_LIMIT2)) |
| 153 |
return FW_HIGH_LOCK; |
| 154 |
return FW_LOCK; |
| 155 |
} |
| 156 |
|
| 157 |
static int get_pl_prim(struct rapl_domain *rd, int pl, enum pl_prims prim) |
| 158 |
{ |
| 159 |
switch (pl) { |
| 160 |
case POWER_LIMIT1: |
| 161 |
if (prim == PL_ENABLE) |
| 162 |
return PL1_ENABLE; |
| 163 |
if (prim == PL_CLAMP && rd->rp->priv->type != RAPL_IF_TPMI) |
| 164 |
return PL1_CLAMP; |
| 165 |
if (prim == PL_LIMIT) |
| 166 |
return POWER_LIMIT1; |
| 167 |
if (prim == PL_TIME_WINDOW) |
| 168 |
return TIME_WINDOW1; |
| 169 |
if (prim == PL_MAX_POWER) |
| 170 |
return THERMAL_SPEC_POWER; |
| 171 |
if (prim == PL_LOCK) |
| 172 |
return get_pl_lock_prim(rd, pl); |
| 173 |
return -EINVAL; |
| 174 |
case POWER_LIMIT2: |
| 175 |
if (prim == PL_ENABLE) |
| 176 |
return PL2_ENABLE; |
| 177 |
if (prim == PL_CLAMP && rd->rp->priv->type != RAPL_IF_TPMI) |
| 178 |
return PL2_CLAMP; |
| 179 |
if (prim == PL_LIMIT) |
| 180 |
return POWER_LIMIT2; |
| 181 |
if (prim == PL_TIME_WINDOW) |
| 182 |
return TIME_WINDOW2; |
| 183 |
if (prim == PL_MAX_POWER) |
| 184 |
return MAX_POWER; |
| 185 |
if (prim == PL_LOCK) |
| 186 |
return get_pl_lock_prim(rd, pl); |
| 187 |
return -EINVAL; |
| 188 |
case POWER_LIMIT4: |
| 189 |
if (prim == PL_LIMIT) |
| 190 |
return POWER_LIMIT4; |
| 191 |
if (prim == PL_ENABLE) |
| 192 |
return PL4_ENABLE; |
| 193 |
/* PL4 would be around two times PL2, use same prim as PL2. */ |
| 194 |
if (prim == PL_MAX_POWER) |
| 195 |
return MAX_POWER; |
| 196 |
if (prim == PL_LOCK) |
| 197 |
return get_pl_lock_prim(rd, pl); |
| 198 |
return -EINVAL; |
| 199 |
default: |
| 200 |
return -EINVAL; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#define power_zone_to_rapl_domain(_zone) \ |
| 205 |
container_of(_zone, struct rapl_domain, power_zone) |
| 206 |
|
| 207 |
struct rapl_defaults { |
| 208 |
u8 floor_freq_reg_addr; |
| 209 |
int (*check_unit)(struct rapl_domain *rd); |
| 210 |
void (*set_floor_freq)(struct rapl_domain *rd, bool mode); |
| 211 |
u64 (*compute_time_window)(struct rapl_domain *rd, u64 val, |
| 212 |
bool to_raw); |
| 213 |
unsigned int dram_domain_energy_unit; |
| 214 |
unsigned int psys_domain_energy_unit; |
| 215 |
bool spr_psys_bits; |
| 216 |
}; |
| 217 |
static struct rapl_defaults *defaults_msr; |
| 218 |
static const struct rapl_defaults defaults_tpmi; |
| 219 |
|
| 220 |
static struct rapl_defaults *get_defaults(struct rapl_package *rp) |
| 221 |
{ |
| 222 |
return rp->priv->defaults; |
| 223 |
} |
| 224 |
|
| 225 |
/* Sideband MBI registers */ |
| 226 |
#define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2) |
| 227 |
#define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf) |
| 228 |
|
| 229 |
#define PACKAGE_PLN_INT_SAVED BIT(0) |
| 230 |
#define MAX_PRIM_NAME (32) |
| 231 |
|
| 232 |
/* per domain data. used to describe individual knobs such that access function |
| 233 |
* can be consolidated into one instead of many inline functions. |
| 234 |
*/ |
| 235 |
struct rapl_primitive_info { |
| 236 |
const char *name; |
| 237 |
u64 mask; |
| 238 |
int shift; |
| 239 |
enum rapl_domain_reg_id id; |
| 240 |
enum unit_type unit; |
| 241 |
u32 flag; |
| 242 |
}; |
| 243 |
|
| 244 |
#define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \ |
| 245 |
.name = #p, \ |
| 246 |
.mask = m, \ |
| 247 |
.shift = s, \ |
| 248 |
.id = i, \ |
| 249 |
.unit = u, \ |
| 250 |
.flag = f \ |
| 251 |
} |
| 252 |
|
| 253 |
static void rapl_init_domains(struct rapl_package *rp); |
| 254 |
static int rapl_read_data_raw(struct rapl_domain *rd, |
| 255 |
enum rapl_primitives prim, |
| 256 |
bool xlate, u64 *data, |
| 257 |
bool pmu_ctx); |
| 258 |
static int rapl_write_data_raw(struct rapl_domain *rd, |
| 259 |
enum rapl_primitives prim, |
| 260 |
unsigned long long value); |
| 261 |
static int rapl_read_pl_data(struct rapl_domain *rd, int pl, |
| 262 |
enum pl_prims pl_prim, |
| 263 |
bool xlate, u64 *data); |
| 264 |
static int rapl_write_pl_data(struct rapl_domain *rd, int pl, |
| 265 |
enum pl_prims pl_prim, |
| 266 |
unsigned long long value); |
| 267 |
static u64 rapl_unit_xlate(struct rapl_domain *rd, |
| 268 |
enum unit_type type, u64 value, int to_raw); |
| 269 |
static void package_power_limit_irq_save(struct rapl_package *rp); |
| 270 |
|
| 271 |
static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */ |
| 272 |
|
| 273 |
static const char *const rapl_domain_names[] = { |
| 274 |
"package", |
| 275 |
"core", |
| 276 |
"uncore", |
| 277 |
"dram", |
| 278 |
"psys", |
| 279 |
}; |
| 280 |
|
| 281 |
static int get_energy_counter(struct powercap_zone *power_zone, |
| 282 |
u64 *energy_raw) |
| 283 |
{ |
| 284 |
struct rapl_domain *rd; |
| 285 |
u64 energy_now; |
| 286 |
|
| 287 |
/* prevent CPU hotplug, make sure the RAPL domain does not go |
| 288 |
* away while reading the counter. |
| 289 |
*/ |
| 290 |
cpus_read_lock(); |
| 291 |
rd = power_zone_to_rapl_domain(power_zone); |
| 292 |
|
| 293 |
if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now, false)) { |
| 294 |
*energy_raw = energy_now; |
| 295 |
cpus_read_unlock(); |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |
| 299 |
cpus_read_unlock(); |
| 300 |
|
| 301 |
return -EIO; |
| 302 |
} |
| 303 |
|
| 304 |
static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy) |
| 305 |
{ |
| 306 |
struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev); |
| 307 |
|
| 308 |
*energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0); |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
static int release_zone(struct powercap_zone *power_zone) |
| 313 |
{ |
| 314 |
struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
| 315 |
struct rapl_package *rp = rd->rp; |
| 316 |
|
| 317 |
/* package zone is the last zone of a package, we can free |
| 318 |
* memory here since all children has been unregistered. |
| 319 |
*/ |
| 320 |
if (rd->id == RAPL_DOMAIN_PACKAGE) { |
| 321 |
kfree(rd); |
| 322 |
rp->domains = NULL; |
| 323 |
} |
| 324 |
|
| 325 |
return 0; |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
static int find_nr_power_limit(struct rapl_domain *rd) |
| 330 |
{ |
| 331 |
int i, nr_pl = 0; |
| 332 |
|
| 333 |
for (i = 0; i < NR_POWER_LIMITS; i++) { |
| 334 |
if (is_pl_valid(rd, i)) |
| 335 |
nr_pl++; |
| 336 |
} |
| 337 |
|
| 338 |
return nr_pl; |
| 339 |
} |
| 340 |
|
| 341 |
static int set_domain_enable(struct powercap_zone *power_zone, bool mode) |
| 342 |
{ |
| 343 |
struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
| 344 |
struct rapl_defaults *defaults = get_defaults(rd->rp); |
| 345 |
u64 val; |
| 346 |
int ret; |
| 347 |
|
| 348 |
cpus_read_lock(); |
| 349 |
ret = rapl_write_pl_data(rd, POWER_LIMIT1, PL_ENABLE, mode); |
| 350 |
if (ret) |
| 351 |
goto end; |
| 352 |
|
| 353 |
ret = rapl_read_pl_data(rd, POWER_LIMIT1, PL_ENABLE, false, &val); |
| 354 |
if (ret) |
| 355 |
goto end; |
| 356 |
|
| 357 |
if (mode != val) { |
| 358 |
pr_debug("%s cannot be %s\n", power_zone->name, |
| 359 |
str_enabled_disabled(mode)); |
| 360 |
goto end; |
| 361 |
} |
| 362 |
|
| 363 |
if (defaults->set_floor_freq) |
| 364 |
defaults->set_floor_freq(rd, mode); |
| 365 |
|
| 366 |
end: |
| 367 |
cpus_read_unlock(); |
| 368 |
|
| 369 |
return ret; |
| 370 |
} |
| 371 |
|
| 372 |
static int get_domain_enable(struct powercap_zone *power_zone, bool *mode) |
| 373 |
{ |
| 374 |
struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone); |
| 375 |
u64 val; |
| 376 |
int ret; |
| 377 |
|
| 378 |
if (rd->rpl[POWER_LIMIT1].locked) { |
| 379 |
*mode = false; |
| 380 |
return 0; |
| 381 |
} |
| 382 |
cpus_read_lock(); |
| 383 |
ret = rapl_read_pl_data(rd, POWER_LIMIT1, PL_ENABLE, true, &val); |
| 384 |
if (!ret) |
| 385 |
*mode = val; |
| 386 |
cpus_read_unlock(); |
| 387 |
|
| 388 |
return ret; |
| 389 |
} |
| 390 |
|
| 391 |
/* per RAPL domain ops, in the order of rapl_domain_type */ |
| 392 |
static const struct powercap_zone_ops zone_ops[] = { |
| 393 |
/* RAPL_DOMAIN_PACKAGE */ |
| 394 |
{ |
| 395 |
.get_energy_uj = get_energy_counter, |
| 396 |
.get_max_energy_range_uj = get_max_energy_counter, |
| 397 |
.release = release_zone, |
| 398 |
.set_enable = set_domain_enable, |
| 399 |
.get_enable = get_domain_enable, |
| 400 |
}, |
| 401 |
/* RAPL_DOMAIN_PP0 */ |
| 402 |
{ |
| 403 |
.get_energy_uj = get_energy_counter, |
| 404 |
.get_max_energy_range_uj = get_max_energy_counter, |
| 405 |
.release = release_zone, |
| 406 |
.set_enable = set_domain_enable, |
| 407 |
.get_enable = get_domain_enable, |
| 408 |
}, |
| 409 |
/* RAPL_DOMAIN_PP1 */ |
| 410 |
{ |
| 411 |
.get_energy_uj = get_energy_counter, |
| 412 |
.get_max_energy_range_uj = get_max_energy_counter, |
| 413 |
.release = release_zone, |
| 414 |
.set_enable = set_domain_enable, |
| 415 |
.get_enable = get_domain_enable, |
| 416 |
}, |
| 417 |
/* RAPL_DOMAIN_DRAM */ |
| 418 |
{ |
| 419 |
.get_energy_uj = get_energy_counter, |
| 420 |
.get_max_energy_range_uj = get_max_energy_counter, |
| 421 |
.release = release_zone, |
| 422 |
.set_enable = set_domain_enable, |
| 423 |
.get_enable = get_domain_enable, |
| 424 |
}, |
| 425 |
/* RAPL_DOMAIN_PLATFORM */ |
| 426 |
{ |
| 427 |
.get_energy_uj = get_energy_counter, |
| 428 |
.get_max_energy_range_uj = get_max_energy_counter, |
| 429 |
.release = release_zone, |
| 430 |
.set_enable = set_domain_enable, |
| 431 |
.get_enable = get_domain_enable, |
| 432 |
}, |
| 433 |
}; |
| 434 |
|
| 435 |
/* |
| 436 |
* Constraint index used by powercap can be different than power limit (PL) |
| 437 |
* index in that some PLs maybe missing due to non-existent MSRs. So we |
| 438 |
* need to convert here by finding the valid PLs only (name populated). |
| 439 |
*/ |
| 440 |
static int contraint_to_pl(struct rapl_domain *rd, int cid) |
| 441 |
{ |
| 442 |
int i, j; |
| 443 |
|
| 444 |
for (i = POWER_LIMIT1, j = 0; i < NR_POWER_LIMITS; i++) { |
| 445 |
if (is_pl_valid(rd, i) && j++ == cid) { |
| 446 |
pr_debug("%s: index %d\n", __func__, i); |
| 447 |
return i; |
| 448 |
} |
| 449 |
} |
| 450 |
pr_err("Cannot find matching power limit for constraint %d\n", cid); |
| 451 |
|
| 452 |
return -EINVAL; |
| 453 |
} |
| 454 |
|
| 455 |
static int set_power_limit(struct powercap_zone *power_zone, int cid, |
| 456 |
u64 power_limit) |
| 457 |
{ |
| 458 |
struct rapl_domain *rd; |
| 459 |
struct rapl_package *rp; |
| 460 |
int ret = 0; |
| 461 |
int id; |
| 462 |
|
| 463 |
cpus_read_lock(); |
| 464 |
rd = power_zone_to_rapl_domain(power_zone); |
| 465 |
id = contraint_to_pl(rd, cid); |
| 466 |
rp = rd->rp; |
| 467 |
|
| 468 |
ret = rapl_write_pl_data(rd, id, PL_LIMIT, power_limit); |
| 469 |
if (!ret) |
| 470 |
package_power_limit_irq_save(rp); |
| 471 |
cpus_read_unlock(); |
| 472 |
return ret; |
| 473 |
} |
| 474 |
|
| 475 |
static int get_current_power_limit(struct powercap_zone *power_zone, int cid, |
| 476 |
u64 *data) |
| 477 |
{ |
| 478 |
struct rapl_domain *rd; |
| 479 |
u64 val; |
| 480 |
int ret = 0; |
| 481 |
int id; |
| 482 |
|
| 483 |
cpus_read_lock(); |
| 484 |
rd = power_zone_to_rapl_domain(power_zone); |
| 485 |
id = contraint_to_pl(rd, cid); |
| 486 |
|
| 487 |
ret = rapl_read_pl_data(rd, id, PL_LIMIT, true, &val); |
| 488 |
if (!ret) |
| 489 |
*data = val; |
| 490 |
|
| 491 |
cpus_read_unlock(); |
| 492 |
|
| 493 |
return ret; |
| 494 |
} |
| 495 |
|
| 496 |
static int set_time_window(struct powercap_zone *power_zone, int cid, |
| 497 |
u64 window) |
| 498 |
{ |
| 499 |
struct rapl_domain *rd; |
| 500 |
int ret = 0; |
| 501 |
int id; |
| 502 |
|
| 503 |
cpus_read_lock(); |
| 504 |
rd = power_zone_to_rapl_domain(power_zone); |
| 505 |
id = contraint_to_pl(rd, cid); |
| 506 |
|
| 507 |
ret = rapl_write_pl_data(rd, id, PL_TIME_WINDOW, window); |
| 508 |
|
| 509 |
cpus_read_unlock(); |
| 510 |
return ret; |
| 511 |
} |
| 512 |
|
| 513 |
static int get_time_window(struct powercap_zone *power_zone, int cid, |
| 514 |
u64 *data) |
| 515 |
{ |
| 516 |
struct rapl_domain *rd; |
| 517 |
u64 val; |
| 518 |
int ret = 0; |
| 519 |
int id; |
| 520 |
|
| 521 |
cpus_read_lock(); |
| 522 |
rd = power_zone_to_rapl_domain(power_zone); |
| 523 |
id = contraint_to_pl(rd, cid); |
| 524 |
|
| 525 |
ret = rapl_read_pl_data(rd, id, PL_TIME_WINDOW, true, &val); |
| 526 |
if (!ret) |
| 527 |
*data = val; |
| 528 |
|
| 529 |
cpus_read_unlock(); |
| 530 |
|
| 531 |
return ret; |
| 532 |
} |
| 533 |
|
| 534 |
static const char *get_constraint_name(struct powercap_zone *power_zone, |
| 535 |
int cid) |
| 536 |
{ |
| 537 |
struct rapl_domain *rd; |
| 538 |
int id; |
| 539 |
|
| 540 |
rd = power_zone_to_rapl_domain(power_zone); |
| 541 |
id = contraint_to_pl(rd, cid); |
| 542 |
if (id >= 0) |
| 543 |
return rd->rpl[id].name; |
| 544 |
|
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
static int get_max_power(struct powercap_zone *power_zone, int cid, u64 *data) |
| 549 |
{ |
| 550 |
struct rapl_domain *rd; |
| 551 |
u64 val; |
| 552 |
int ret = 0; |
| 553 |
int id; |
| 554 |
|
| 555 |
cpus_read_lock(); |
| 556 |
rd = power_zone_to_rapl_domain(power_zone); |
| 557 |
id = contraint_to_pl(rd, cid); |
| 558 |
|
| 559 |
ret = rapl_read_pl_data(rd, id, PL_MAX_POWER, true, &val); |
| 560 |
if (!ret) |
| 561 |
*data = val; |
| 562 |
|
| 563 |
/* As a generalization rule, PL4 would be around two times PL2. */ |
| 564 |
if (id == POWER_LIMIT4) |
| 565 |
*data = *data * 2; |
| 566 |
|
| 567 |
cpus_read_unlock(); |
| 568 |
|
| 569 |
return ret; |
| 570 |
} |
| 571 |
|
| 572 |
static const struct powercap_zone_constraint_ops constraint_ops = { |
| 573 |
.set_power_limit_uw = set_power_limit, |
| 574 |
.get_power_limit_uw = get_current_power_limit, |
| 575 |
.set_time_window_us = set_time_window, |
| 576 |
.get_time_window_us = get_time_window, |
| 577 |
.get_max_power_uw = get_max_power, |
| 578 |
.get_name = get_constraint_name, |
| 579 |
}; |
| 580 |
|
| 581 |
/* Return the id used for read_raw/write_raw callback */ |
| 582 |
static int get_rid(struct rapl_package *rp) |
| 583 |
{ |
| 584 |
return rp->lead_cpu >= 0 ? rp->lead_cpu : rp->id; |
| 585 |
} |
| 586 |
|
| 587 |
/* called after domain detection and package level data are set */ |
| 588 |
static void rapl_init_domains(struct rapl_package *rp) |
| 589 |
{ |
| 590 |
enum rapl_domain_type i; |
| 591 |
enum rapl_domain_reg_id j; |
| 592 |
struct rapl_domain *rd = rp->domains; |
| 593 |
|
| 594 |
for (i = 0; i < RAPL_DOMAIN_MAX; i++) { |
| 595 |
unsigned int mask = rp->domain_map & (1 << i); |
| 596 |
int t; |
| 597 |
|
| 598 |
if (!mask) |
| 599 |
continue; |
| 600 |
|
| 601 |
rd->rp = rp; |
| 602 |
|
| 603 |
if (i == RAPL_DOMAIN_PLATFORM && rp->id > 0) { |
| 604 |
snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "psys-%d", |
| 605 |
rp->lead_cpu >= 0 ? topology_physical_package_id(rp->lead_cpu) : |
| 606 |
rp->id); |
| 607 |
} else { |
| 608 |
snprintf(rd->name, RAPL_DOMAIN_NAME_LENGTH, "%s", |
| 609 |
rapl_domain_names[i]); |
| 610 |
} |
| 611 |
|
| 612 |
rd->id = i; |
| 613 |
|
| 614 |
/* PL1 is supported by default */ |
| 615 |
rp->priv->limits[i] |= BIT(POWER_LIMIT1); |
| 616 |
|
| 617 |
for (t = POWER_LIMIT1; t < NR_POWER_LIMITS; t++) { |
| 618 |
if (rp->priv->limits[i] & BIT(t)) |
| 619 |
rd->rpl[t].name = pl_names[t]; |
| 620 |
} |
| 621 |
|
| 622 |
for (j = 0; j < RAPL_DOMAIN_REG_MAX; j++) |
| 623 |
rd->regs[j] = rp->priv->regs[i][j]; |
| 624 |
|
| 625 |
rd++; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type, |
| 630 |
u64 value, int to_raw) |
| 631 |
{ |
| 632 |
u64 units = 1; |
| 633 |
struct rapl_defaults *defaults = get_defaults(rd->rp); |
| 634 |
u64 scale = 1; |
| 635 |
|
| 636 |
switch (type) { |
| 637 |
case POWER_UNIT: |
| 638 |
units = rd->power_unit; |
| 639 |
break; |
| 640 |
case ENERGY_UNIT: |
| 641 |
scale = ENERGY_UNIT_SCALE; |
| 642 |
units = rd->energy_unit; |
| 643 |
break; |
| 644 |
case TIME_UNIT: |
| 645 |
return defaults->compute_time_window(rd, value, to_raw); |
| 646 |
case ARBITRARY_UNIT: |
| 647 |
default: |
| 648 |
return value; |
| 649 |
} |
| 650 |
|
| 651 |
if (to_raw) |
| 652 |
return div64_u64(value, units) * scale; |
| 653 |
|
| 654 |
value *= units; |
| 655 |
|
| 656 |
return div64_u64(value, scale); |
| 657 |
} |
| 658 |
|
| 659 |
/* RAPL primitives for MSR and MMIO I/F */ |
| 660 |
static struct rapl_primitive_info rpi_msr[NR_RAPL_PRIMITIVES] = { |
| 661 |
/* name, mask, shift, msr index, unit divisor */ |
| 662 |
[POWER_LIMIT1] = PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0, |
| 663 |
RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
| 664 |
[POWER_LIMIT2] = PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32, |
| 665 |
RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
| 666 |
[POWER_LIMIT4] = PRIMITIVE_INFO_INIT(POWER_LIMIT4, POWER_LIMIT4_MASK, 0, |
| 667 |
RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0), |
| 668 |
[ENERGY_COUNTER] = PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0, |
| 669 |
RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0), |
| 670 |
[FW_LOCK] = PRIMITIVE_INFO_INIT(FW_LOCK, POWER_LOW_LOCK, 31, |
| 671 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 672 |
[FW_HIGH_LOCK] = PRIMITIVE_INFO_INIT(FW_LOCK, POWER_HIGH_LOCK, 63, |
| 673 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 674 |
[PL1_ENABLE] = PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15, |
| 675 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 676 |
[PL1_CLAMP] = PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16, |
| 677 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 678 |
[PL2_ENABLE] = PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47, |
| 679 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 680 |
[PL2_CLAMP] = PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48, |
| 681 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 682 |
[TIME_WINDOW1] = PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17, |
| 683 |
RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
| 684 |
[TIME_WINDOW2] = PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49, |
| 685 |
RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
| 686 |
[THERMAL_SPEC_POWER] = PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK, |
| 687 |
0, RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 688 |
[MAX_POWER] = PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32, |
| 689 |
RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 690 |
[MIN_POWER] = PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16, |
| 691 |
RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 692 |
[MAX_TIME_WINDOW] = PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48, |
| 693 |
RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0), |
| 694 |
[THROTTLED_TIME] = PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0, |
| 695 |
RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0), |
| 696 |
[PRIORITY_LEVEL] = PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0, |
| 697 |
RAPL_DOMAIN_REG_POLICY, ARBITRARY_UNIT, 0), |
| 698 |
[PSYS_POWER_LIMIT1] = PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT1, PSYS_POWER_LIMIT1_MASK, 0, |
| 699 |
RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
| 700 |
[PSYS_POWER_LIMIT2] = PRIMITIVE_INFO_INIT(PSYS_POWER_LIMIT2, PSYS_POWER_LIMIT2_MASK, 32, |
| 701 |
RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
| 702 |
[PSYS_PL1_ENABLE] = PRIMITIVE_INFO_INIT(PSYS_PL1_ENABLE, PSYS_POWER_LIMIT1_ENABLE, 17, |
| 703 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 704 |
[PSYS_PL2_ENABLE] = PRIMITIVE_INFO_INIT(PSYS_PL2_ENABLE, PSYS_POWER_LIMIT2_ENABLE, 49, |
| 705 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 706 |
[PSYS_TIME_WINDOW1] = PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW1, PSYS_TIME_WINDOW1_MASK, 19, |
| 707 |
RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
| 708 |
[PSYS_TIME_WINDOW2] = PRIMITIVE_INFO_INIT(PSYS_TIME_WINDOW2, PSYS_TIME_WINDOW2_MASK, 51, |
| 709 |
RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
| 710 |
/* non-hardware */ |
| 711 |
[AVERAGE_POWER] = PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT, |
| 712 |
RAPL_PRIMITIVE_DERIVED), |
| 713 |
}; |
| 714 |
|
| 715 |
/* RAPL primitives for TPMI I/F */ |
| 716 |
static struct rapl_primitive_info rpi_tpmi[NR_RAPL_PRIMITIVES] = { |
| 717 |
/* name, mask, shift, msr index, unit divisor */ |
| 718 |
[POWER_LIMIT1] = PRIMITIVE_INFO_INIT(POWER_LIMIT1, TPMI_POWER_LIMIT_MASK, 0, |
| 719 |
RAPL_DOMAIN_REG_LIMIT, POWER_UNIT, 0), |
| 720 |
[POWER_LIMIT2] = PRIMITIVE_INFO_INIT(POWER_LIMIT2, TPMI_POWER_LIMIT_MASK, 0, |
| 721 |
RAPL_DOMAIN_REG_PL2, POWER_UNIT, 0), |
| 722 |
[POWER_LIMIT4] = PRIMITIVE_INFO_INIT(POWER_LIMIT4, TPMI_POWER_LIMIT_MASK, 0, |
| 723 |
RAPL_DOMAIN_REG_PL4, POWER_UNIT, 0), |
| 724 |
[ENERGY_COUNTER] = PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0, |
| 725 |
RAPL_DOMAIN_REG_STATUS, ENERGY_UNIT, 0), |
| 726 |
[PL1_LOCK] = PRIMITIVE_INFO_INIT(PL1_LOCK, POWER_HIGH_LOCK, 63, |
| 727 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 728 |
[PL2_LOCK] = PRIMITIVE_INFO_INIT(PL2_LOCK, POWER_HIGH_LOCK, 63, |
| 729 |
RAPL_DOMAIN_REG_PL2, ARBITRARY_UNIT, 0), |
| 730 |
[PL4_LOCK] = PRIMITIVE_INFO_INIT(PL4_LOCK, POWER_HIGH_LOCK, 63, |
| 731 |
RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0), |
| 732 |
[PL1_ENABLE] = PRIMITIVE_INFO_INIT(PL1_ENABLE, TPMI_POWER_LIMIT_ENABLE, 62, |
| 733 |
RAPL_DOMAIN_REG_LIMIT, ARBITRARY_UNIT, 0), |
| 734 |
[PL2_ENABLE] = PRIMITIVE_INFO_INIT(PL2_ENABLE, TPMI_POWER_LIMIT_ENABLE, 62, |
| 735 |
RAPL_DOMAIN_REG_PL2, ARBITRARY_UNIT, 0), |
| 736 |
[PL4_ENABLE] = PRIMITIVE_INFO_INIT(PL4_ENABLE, TPMI_POWER_LIMIT_ENABLE, 62, |
| 737 |
RAPL_DOMAIN_REG_PL4, ARBITRARY_UNIT, 0), |
| 738 |
[TIME_WINDOW1] = PRIMITIVE_INFO_INIT(TIME_WINDOW1, TPMI_TIME_WINDOW_MASK, 18, |
| 739 |
RAPL_DOMAIN_REG_LIMIT, TIME_UNIT, 0), |
| 740 |
[TIME_WINDOW2] = PRIMITIVE_INFO_INIT(TIME_WINDOW2, TPMI_TIME_WINDOW_MASK, 18, |
| 741 |
RAPL_DOMAIN_REG_PL2, TIME_UNIT, 0), |
| 742 |
[THERMAL_SPEC_POWER] = PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, TPMI_INFO_SPEC_MASK, 0, |
| 743 |
RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 744 |
[MAX_POWER] = PRIMITIVE_INFO_INIT(MAX_POWER, TPMI_INFO_MAX_MASK, 36, |
| 745 |
RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 746 |
[MIN_POWER] = PRIMITIVE_INFO_INIT(MIN_POWER, TPMI_INFO_MIN_MASK, 18, |
| 747 |
RAPL_DOMAIN_REG_INFO, POWER_UNIT, 0), |
| 748 |
[MAX_TIME_WINDOW] = PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, TPMI_INFO_MAX_TIME_WIN_MASK, 54, |
| 749 |
RAPL_DOMAIN_REG_INFO, TIME_UNIT, 0), |
| 750 |
[THROTTLED_TIME] = PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0, |
| 751 |
RAPL_DOMAIN_REG_PERF, TIME_UNIT, 0), |
| 752 |
/* non-hardware */ |
| 753 |
[AVERAGE_POWER] = PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, |
| 754 |
POWER_UNIT, RAPL_PRIMITIVE_DERIVED), |
| 755 |
}; |
| 756 |
|
| 757 |
static struct rapl_primitive_info *get_rpi(struct rapl_package *rp, int prim) |
| 758 |
{ |
| 759 |
struct rapl_primitive_info *rpi = rp->priv->rpi; |
| 760 |
|
| 761 |
if (prim < 0 || prim >= NR_RAPL_PRIMITIVES || !rpi) |
| 762 |
return NULL; |
| 763 |
|
| 764 |
return &rpi[prim]; |
| 765 |
} |
| 766 |
|
| 767 |
static int rapl_config(struct rapl_package *rp) |
| 768 |
{ |
| 769 |
switch (rp->priv->type) { |
| 770 |
/* MMIO I/F shares the same register layout as MSR registers */ |
| 771 |
case RAPL_IF_MMIO: |
| 772 |
case RAPL_IF_MSR: |
| 773 |
rp->priv->defaults = (void *)defaults_msr; |
| 774 |
rp->priv->rpi = (void *)rpi_msr; |
| 775 |
break; |
| 776 |
case RAPL_IF_TPMI: |
| 777 |
rp->priv->defaults = (void *)&defaults_tpmi; |
| 778 |
rp->priv->rpi = (void *)rpi_tpmi; |
| 779 |
break; |
| 780 |
default: |
| 781 |
return -EINVAL; |
| 782 |
} |
| 783 |
|
| 784 |
/* defaults_msr can be NULL on unsupported platforms */ |
| 785 |
if (!rp->priv->defaults || !rp->priv->rpi) |
| 786 |
return -ENODEV; |
| 787 |
|
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static enum rapl_primitives |
| 792 |
prim_fixups(struct rapl_domain *rd, enum rapl_primitives prim) |
| 793 |
{ |
| 794 |
struct rapl_defaults *defaults = get_defaults(rd->rp); |
| 795 |
|
| 796 |
if (!defaults->spr_psys_bits) |
| 797 |
return prim; |
| 798 |
|
| 799 |
if (rd->id != RAPL_DOMAIN_PLATFORM) |
| 800 |
return prim; |
| 801 |
|
| 802 |
switch (prim) { |
| 803 |
case POWER_LIMIT1: |
| 804 |
return PSYS_POWER_LIMIT1; |
| 805 |
case POWER_LIMIT2: |
| 806 |
return PSYS_POWER_LIMIT2; |
| 807 |
case PL1_ENABLE: |
| 808 |
return PSYS_PL1_ENABLE; |
| 809 |
case PL2_ENABLE: |
| 810 |
return PSYS_PL2_ENABLE; |
| 811 |
case TIME_WINDOW1: |
| 812 |
return PSYS_TIME_WINDOW1; |
| 813 |
case TIME_WINDOW2: |
| 814 |
return PSYS_TIME_WINDOW2; |
| 815 |
default: |
| 816 |
return prim; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
/* Read primitive data based on its related struct rapl_primitive_info. |
| 821 |
* if xlate flag is set, return translated data based on data units, i.e. |
| 822 |
* time, energy, and power. |
| 823 |
* RAPL MSRs are non-architectual and are laid out not consistently across |
| 824 |
* domains. Here we use primitive info to allow writing consolidated access |
| 825 |
* functions. |
| 826 |
* For a given primitive, it is processed by MSR mask and shift. Unit conversion |
| 827 |
* is pre-assigned based on RAPL unit MSRs read at init time. |
| 828 |
* 63-------------------------- 31--------------------------- 0 |
| 829 |
* | xxxxx (mask) | |
| 830 |
* | |<- shift ----------------| |
| 831 |
* 63-------------------------- 31--------------------------- 0 |
| 832 |
*/ |
| 833 |
static int rapl_read_data_raw(struct rapl_domain *rd, |
| 834 |
enum rapl_primitives prim, bool xlate, u64 *data, |
| 835 |
bool pmu_ctx) |
| 836 |
{ |
| 837 |
u64 value; |
| 838 |
enum rapl_primitives prim_fixed = prim_fixups(rd, prim); |
| 839 |
struct rapl_primitive_info *rpi = get_rpi(rd->rp, prim_fixed); |
| 840 |
struct reg_action ra; |
| 841 |
|
| 842 |
if (!rpi || !rpi->name || rpi->flag & RAPL_PRIMITIVE_DUMMY) |
| 843 |
return -EINVAL; |
| 844 |
|
| 845 |
ra.reg = rd->regs[rpi->id]; |
| 846 |
if (!ra.reg.val) |
| 847 |
return -EINVAL; |
| 848 |
|
| 849 |
/* non-hardware data are collected by the polling thread */ |
| 850 |
if (rpi->flag & RAPL_PRIMITIVE_DERIVED) { |
| 851 |
*data = rd->rdd.primitives[prim]; |
| 852 |
return 0; |
| 853 |
} |
| 854 |
|
| 855 |
ra.mask = rpi->mask; |
| 856 |
|
| 857 |
if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra, pmu_ctx)) { |
| 858 |
pr_debug("failed to read reg 0x%llx for %s:%s\n", ra.reg.val, rd->rp->name, rd->name); |
| 859 |
return -EIO; |
| 860 |
} |
| 861 |
|
| 862 |
value = ra.value >> rpi->shift; |
| 863 |
|
| 864 |
if (xlate) |
| 865 |
*data = rapl_unit_xlate(rd, rpi->unit, value, 0); |
| 866 |
else |
| 867 |
*data = value; |
| 868 |
|
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
/* Similar use of primitive info in the read counterpart */ |
| 873 |
static int rapl_write_data_raw(struct rapl_domain *rd, |
| 874 |
enum rapl_primitives prim, |
| 875 |
unsigned long long value) |
| 876 |
{ |
| 877 |
enum rapl_primitives prim_fixed = prim_fixups(rd, prim); |
| 878 |
struct rapl_primitive_info *rpi = get_rpi(rd->rp, prim_fixed); |
| 879 |
u64 bits; |
| 880 |
struct reg_action ra; |
| 881 |
int ret; |
| 882 |
|
| 883 |
if (!rpi || !rpi->name || rpi->flag & RAPL_PRIMITIVE_DUMMY) |
| 884 |
return -EINVAL; |
| 885 |
|
| 886 |
bits = rapl_unit_xlate(rd, rpi->unit, value, 1); |
| 887 |
bits <<= rpi->shift; |
| 888 |
bits &= rpi->mask; |
| 889 |
|
| 890 |
memset(&ra, 0, sizeof(ra)); |
| 891 |
|
| 892 |
ra.reg = rd->regs[rpi->id]; |
| 893 |
ra.mask = rpi->mask; |
| 894 |
ra.value = bits; |
| 895 |
|
| 896 |
ret = rd->rp->priv->write_raw(get_rid(rd->rp), &ra); |
| 897 |
|
| 898 |
return ret; |
| 899 |
} |
| 900 |
|
| 901 |
static int rapl_read_pl_data(struct rapl_domain *rd, int pl, |
| 902 |
enum pl_prims pl_prim, bool xlate, u64 *data) |
| 903 |
{ |
| 904 |
enum rapl_primitives prim = get_pl_prim(rd, pl, pl_prim); |
| 905 |
|
| 906 |
if (!is_pl_valid(rd, pl)) |
| 907 |
return -EINVAL; |
| 908 |
|
| 909 |
return rapl_read_data_raw(rd, prim, xlate, data, false); |
| 910 |
} |
| 911 |
|
| 912 |
static int rapl_write_pl_data(struct rapl_domain *rd, int pl, |
| 913 |
enum pl_prims pl_prim, |
| 914 |
unsigned long long value) |
| 915 |
{ |
| 916 |
enum rapl_primitives prim = get_pl_prim(rd, pl, pl_prim); |
| 917 |
|
| 918 |
if (!is_pl_valid(rd, pl)) |
| 919 |
return -EINVAL; |
| 920 |
|
| 921 |
if (rd->rpl[pl].locked) { |
| 922 |
pr_debug("%s:%s:%s locked by BIOS\n", rd->rp->name, rd->name, pl_names[pl]); |
| 923 |
return -EACCES; |
| 924 |
} |
| 925 |
|
| 926 |
return rapl_write_data_raw(rd, prim, value); |
| 927 |
} |
| 928 |
/* |
| 929 |
* Raw RAPL data stored in MSRs are in certain scales. We need to |
| 930 |
* convert them into standard units based on the units reported in |
| 931 |
* the RAPL unit MSRs. This is specific to CPUs as the method to |
| 932 |
* calculate units differ on different CPUs. |
| 933 |
* We convert the units to below format based on CPUs. |
| 934 |
* i.e. |
| 935 |
* energy unit: picoJoules : Represented in picoJoules by default |
| 936 |
* power unit : microWatts : Represented in milliWatts by default |
| 937 |
* time unit : microseconds: Represented in seconds by default |
| 938 |
*/ |
| 939 |
static int rapl_check_unit_core(struct rapl_domain *rd) |
| 940 |
{ |
| 941 |
struct reg_action ra; |
| 942 |
u32 value; |
| 943 |
|
| 944 |
ra.reg = rd->regs[RAPL_DOMAIN_REG_UNIT]; |
| 945 |
ra.mask = ~0; |
| 946 |
if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra, false)) { |
| 947 |
pr_err("Failed to read power unit REG 0x%llx on %s:%s, exit.\n", |
| 948 |
ra.reg.val, rd->rp->name, rd->name); |
| 949 |
return -ENODEV; |
| 950 |
} |
| 951 |
|
| 952 |
value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET; |
| 953 |
rd->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value); |
| 954 |
|
| 955 |
value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET; |
| 956 |
rd->power_unit = 1000000 / (1 << value); |
| 957 |
|
| 958 |
value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET; |
| 959 |
rd->time_unit = 1000000 / (1 << value); |
| 960 |
|
| 961 |
pr_debug("Core CPU %s:%s energy=%dpJ, time=%dus, power=%duW\n", |
| 962 |
rd->rp->name, rd->name, rd->energy_unit, rd->time_unit, rd->power_unit); |
| 963 |
|
| 964 |
return 0; |
| 965 |
} |
| 966 |
|
| 967 |
static int rapl_check_unit_atom(struct rapl_domain *rd) |
| 968 |
{ |
| 969 |
struct reg_action ra; |
| 970 |
u32 value; |
| 971 |
|
| 972 |
ra.reg = rd->regs[RAPL_DOMAIN_REG_UNIT]; |
| 973 |
ra.mask = ~0; |
| 974 |
if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra, false)) { |
| 975 |
pr_err("Failed to read power unit REG 0x%llx on %s:%s, exit.\n", |
| 976 |
ra.reg.val, rd->rp->name, rd->name); |
| 977 |
return -ENODEV; |
| 978 |
} |
| 979 |
|
| 980 |
value = (ra.value & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET; |
| 981 |
rd->energy_unit = ENERGY_UNIT_SCALE * 1 << value; |
| 982 |
|
| 983 |
value = (ra.value & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET; |
| 984 |
rd->power_unit = (1 << value) * 1000; |
| 985 |
|
| 986 |
value = (ra.value & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET; |
| 987 |
rd->time_unit = 1000000 / (1 << value); |
| 988 |
|
| 989 |
pr_debug("Atom %s:%s energy=%dpJ, time=%dus, power=%duW\n", |
| 990 |
rd->rp->name, rd->name, rd->energy_unit, rd->time_unit, rd->power_unit); |
| 991 |
|
| 992 |
return 0; |
| 993 |
} |
| 994 |
|
| 995 |
static void power_limit_irq_save_cpu(void *info) |
| 996 |
{ |
| 997 |
u32 l, h = 0; |
| 998 |
struct rapl_package *rp = (struct rapl_package *)info; |
| 999 |
|
| 1000 |
/* save the state of PLN irq mask bit before disabling it */ |
| 1001 |
rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h); |
| 1002 |
if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) { |
| 1003 |
rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE; |
| 1004 |
rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED; |
| 1005 |
} |
| 1006 |
l &= ~PACKAGE_THERM_INT_PLN_ENABLE; |
| 1007 |
wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* REVISIT: |
| 1011 |
* When package power limit is set artificially low by RAPL, LVT |
| 1012 |
* thermal interrupt for package power limit should be ignored |
| 1013 |
* since we are not really exceeding the real limit. The intention |
| 1014 |
* is to avoid excessive interrupts while we are trying to save power. |
| 1015 |
* A useful feature might be routing the package_power_limit interrupt |
| 1016 |
* to userspace via eventfd. once we have a usecase, this is simple |
| 1017 |
* to do by adding an atomic notifier. |
| 1018 |
*/ |
| 1019 |
|
| 1020 |
static void package_power_limit_irq_save(struct rapl_package *rp) |
| 1021 |
{ |
| 1022 |
if (rp->lead_cpu < 0) |
| 1023 |
return; |
| 1024 |
|
| 1025 |
if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) |
| 1026 |
return; |
| 1027 |
|
| 1028 |
smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* |
| 1032 |
* Restore per package power limit interrupt enable state. Called from cpu |
| 1033 |
* hotplug code on package removal. |
| 1034 |
*/ |
| 1035 |
static void package_power_limit_irq_restore(struct rapl_package *rp) |
| 1036 |
{ |
| 1037 |
u32 l, h; |
| 1038 |
|
| 1039 |
if (rp->lead_cpu < 0) |
| 1040 |
return; |
| 1041 |
|
| 1042 |
if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN)) |
| 1043 |
return; |
| 1044 |
|
| 1045 |
/* irq enable state not saved, nothing to restore */ |
| 1046 |
if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) |
| 1047 |
return; |
| 1048 |
|
| 1049 |
rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h); |
| 1050 |
|
| 1051 |
if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE) |
| 1052 |
l |= PACKAGE_THERM_INT_PLN_ENABLE; |
| 1053 |
else |
| 1054 |
l &= ~PACKAGE_THERM_INT_PLN_ENABLE; |
| 1055 |
|
| 1056 |
wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h); |
| 1057 |
} |
| 1058 |
|
| 1059 |
static void set_floor_freq_default(struct rapl_domain *rd, bool mode) |
| 1060 |
{ |
| 1061 |
int i; |
| 1062 |
|
| 1063 |
/* always enable clamp such that p-state can go below OS requested |
| 1064 |
* range. power capping priority over guranteed frequency. |
| 1065 |
*/ |
| 1066 |
rapl_write_pl_data(rd, POWER_LIMIT1, PL_CLAMP, mode); |
| 1067 |
|
| 1068 |
for (i = POWER_LIMIT2; i < NR_POWER_LIMITS; i++) { |
| 1069 |
rapl_write_pl_data(rd, i, PL_ENABLE, mode); |
| 1070 |
rapl_write_pl_data(rd, i, PL_CLAMP, mode); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
static void set_floor_freq_atom(struct rapl_domain *rd, bool enable) |
| 1075 |
{ |
| 1076 |
static u32 power_ctrl_orig_val; |
| 1077 |
struct rapl_defaults *defaults = get_defaults(rd->rp); |
| 1078 |
u32 mdata; |
| 1079 |
|
| 1080 |
if (!defaults->floor_freq_reg_addr) { |
| 1081 |
pr_err("Invalid floor frequency config register\n"); |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (!power_ctrl_orig_val) |
| 1086 |
iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ, |
| 1087 |
defaults->floor_freq_reg_addr, |
| 1088 |
&power_ctrl_orig_val); |
| 1089 |
mdata = power_ctrl_orig_val; |
| 1090 |
if (enable) { |
| 1091 |
mdata &= ~(0x7f << 8); |
| 1092 |
mdata |= 1 << 8; |
| 1093 |
} |
| 1094 |
iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE, |
| 1095 |
defaults->floor_freq_reg_addr, mdata); |
| 1096 |
} |
| 1097 |
|
| 1098 |
static u64 rapl_compute_time_window_core(struct rapl_domain *rd, u64 value, |
| 1099 |
bool to_raw) |
| 1100 |
{ |
| 1101 |
u64 f, y; /* fraction and exp. used for time unit */ |
| 1102 |
|
| 1103 |
/* |
| 1104 |
* Special processing based on 2^Y*(1+F/4), refer |
| 1105 |
* to Intel Software Developer's manual Vol.3B: CH 14.9.3. |
| 1106 |
*/ |
| 1107 |
if (!to_raw) { |
| 1108 |
f = (value & 0x60) >> 5; |
| 1109 |
y = value & 0x1f; |
| 1110 |
value = (1 << y) * (4 + f) * rd->time_unit / 4; |
| 1111 |
} else { |
| 1112 |
if (value < rd->time_unit) |
| 1113 |
return 0; |
| 1114 |
|
| 1115 |
do_div(value, rd->time_unit); |
| 1116 |
y = ilog2(value); |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* The target hardware field is 7 bits wide, so return all ones |
| 1120 |
* if the exponent is too large. |
| 1121 |
*/ |
| 1122 |
if (y > 0x1f) |
| 1123 |
return 0x7f; |
| 1124 |
|
| 1125 |
f = div64_u64(4 * (value - (1ULL << y)), 1ULL << y); |
| 1126 |
value = (y & 0x1f) | ((f & 0x3) << 5); |
| 1127 |
} |
| 1128 |
return value; |
| 1129 |
} |
| 1130 |
|
| 1131 |
static u64 rapl_compute_time_window_atom(struct rapl_domain *rd, u64 value, |
| 1132 |
bool to_raw) |
| 1133 |
{ |
| 1134 |
/* |
| 1135 |
* Atom time unit encoding is straight forward val * time_unit, |
| 1136 |
* where time_unit is default to 1 sec. Never 0. |
| 1137 |
*/ |
| 1138 |
if (!to_raw) |
| 1139 |
return (value) ? value * rd->time_unit : rd->time_unit; |
| 1140 |
|
| 1141 |
value = div64_u64(value, rd->time_unit); |
| 1142 |
|
| 1143 |
return value; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* TPMI Unit register has different layout */ |
| 1147 |
#define TPMI_POWER_UNIT_OFFSET POWER_UNIT_OFFSET |
| 1148 |
#define TPMI_POWER_UNIT_MASK POWER_UNIT_MASK |
| 1149 |
#define TPMI_ENERGY_UNIT_OFFSET 0x06 |
| 1150 |
#define TPMI_ENERGY_UNIT_MASK 0x7C0 |
| 1151 |
#define TPMI_TIME_UNIT_OFFSET 0x0C |
| 1152 |
#define TPMI_TIME_UNIT_MASK 0xF000 |
| 1153 |
|
| 1154 |
static int rapl_check_unit_tpmi(struct rapl_domain *rd) |
| 1155 |
{ |
| 1156 |
struct reg_action ra; |
| 1157 |
u32 value; |
| 1158 |
|
| 1159 |
ra.reg = rd->regs[RAPL_DOMAIN_REG_UNIT]; |
| 1160 |
ra.mask = ~0; |
| 1161 |
if (rd->rp->priv->read_raw(get_rid(rd->rp), &ra, false)) { |
| 1162 |
pr_err("Failed to read power unit REG 0x%llx on %s:%s, exit.\n", |
| 1163 |
ra.reg.val, rd->rp->name, rd->name); |
| 1164 |
return -ENODEV; |
| 1165 |
} |
| 1166 |
|
| 1167 |
value = (ra.value & TPMI_ENERGY_UNIT_MASK) >> TPMI_ENERGY_UNIT_OFFSET; |
| 1168 |
rd->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value); |
| 1169 |
|
| 1170 |
value = (ra.value & TPMI_POWER_UNIT_MASK) >> TPMI_POWER_UNIT_OFFSET; |
| 1171 |
rd->power_unit = 1000000 / (1 << value); |
| 1172 |
|
| 1173 |
value = (ra.value & TPMI_TIME_UNIT_MASK) >> TPMI_TIME_UNIT_OFFSET; |
| 1174 |
rd->time_unit = 1000000 / (1 << value); |
| 1175 |
|
| 1176 |
pr_debug("Core CPU %s:%s energy=%dpJ, time=%dus, power=%duW\n", |
| 1177 |
rd->rp->name, rd->name, rd->energy_unit, rd->time_unit, rd->power_unit); |
| 1178 |
|
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
|
| 1182 |
static const struct rapl_defaults defaults_tpmi = { |
| 1183 |
.check_unit = rapl_check_unit_tpmi, |
| 1184 |
/* Reuse existing logic, ignore the PL_CLAMP failures and enable all Power Limits */ |
| 1185 |
.set_floor_freq = set_floor_freq_default, |
| 1186 |
.compute_time_window = rapl_compute_time_window_core, |
| 1187 |
}; |
| 1188 |
|
| 1189 |
static const struct rapl_defaults rapl_defaults_core = { |
| 1190 |
.floor_freq_reg_addr = 0, |
| 1191 |
.check_unit = rapl_check_unit_core, |
| 1192 |
.set_floor_freq = set_floor_freq_default, |
| 1193 |
.compute_time_window = rapl_compute_time_window_core, |
| 1194 |
}; |
| 1195 |
|
| 1196 |
static const struct rapl_defaults rapl_defaults_hsw_server = { |
| 1197 |
.check_unit = rapl_check_unit_core, |
| 1198 |
.set_floor_freq = set_floor_freq_default, |
| 1199 |
.compute_time_window = rapl_compute_time_window_core, |
| 1200 |
.dram_domain_energy_unit = 15300, |
| 1201 |
}; |
| 1202 |
|
| 1203 |
static const struct rapl_defaults rapl_defaults_spr_server = { |
| 1204 |
.check_unit = rapl_check_unit_core, |
| 1205 |
.set_floor_freq = set_floor_freq_default, |
| 1206 |
.compute_time_window = rapl_compute_time_window_core, |
| 1207 |
.psys_domain_energy_unit = 1000000000, |
| 1208 |
.spr_psys_bits = true, |
| 1209 |
}; |
| 1210 |
|
| 1211 |
static const struct rapl_defaults rapl_defaults_byt = { |
| 1212 |
.floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT, |
| 1213 |
.check_unit = rapl_check_unit_atom, |
| 1214 |
.set_floor_freq = set_floor_freq_atom, |
| 1215 |
.compute_time_window = rapl_compute_time_window_atom, |
| 1216 |
}; |
| 1217 |
|
| 1218 |
static const struct rapl_defaults rapl_defaults_tng = { |
| 1219 |
.floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG, |
| 1220 |
.check_unit = rapl_check_unit_atom, |
| 1221 |
.set_floor_freq = set_floor_freq_atom, |
| 1222 |
.compute_time_window = rapl_compute_time_window_atom, |
| 1223 |
}; |
| 1224 |
|
| 1225 |
static const struct rapl_defaults rapl_defaults_ann = { |
| 1226 |
.floor_freq_reg_addr = 0, |
| 1227 |
.check_unit = rapl_check_unit_atom, |
| 1228 |
.set_floor_freq = NULL, |
| 1229 |
.compute_time_window = rapl_compute_time_window_atom, |
| 1230 |
}; |
| 1231 |
|
| 1232 |
static const struct rapl_defaults rapl_defaults_cht = { |
| 1233 |
.floor_freq_reg_addr = 0, |
| 1234 |
.check_unit = rapl_check_unit_atom, |
| 1235 |
.set_floor_freq = NULL, |
| 1236 |
.compute_time_window = rapl_compute_time_window_atom, |
| 1237 |
}; |
| 1238 |
|
| 1239 |
static const struct rapl_defaults rapl_defaults_amd = { |
| 1240 |
.check_unit = rapl_check_unit_core, |
| 1241 |
}; |
| 1242 |
|
| 1243 |
static const struct x86_cpu_id rapl_ids[] __initconst = { |
| 1244 |
X86_MATCH_VFM(INTEL_SANDYBRIDGE, &rapl_defaults_core), |
| 1245 |
X86_MATCH_VFM(INTEL_SANDYBRIDGE_X, &rapl_defaults_core), |
| 1246 |
|
| 1247 |
X86_MATCH_VFM(INTEL_IVYBRIDGE, &rapl_defaults_core), |
| 1248 |
X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &rapl_defaults_core), |
| 1249 |
|
| 1250 |
X86_MATCH_VFM(INTEL_HASWELL, &rapl_defaults_core), |
| 1251 |
X86_MATCH_VFM(INTEL_HASWELL_L, &rapl_defaults_core), |
| 1252 |
X86_MATCH_VFM(INTEL_HASWELL_G, &rapl_defaults_core), |
| 1253 |
X86_MATCH_VFM(INTEL_HASWELL_X, &rapl_defaults_hsw_server), |
| 1254 |
|
| 1255 |
X86_MATCH_VFM(INTEL_BROADWELL, &rapl_defaults_core), |
| 1256 |
X86_MATCH_VFM(INTEL_BROADWELL_G, &rapl_defaults_core), |
| 1257 |
X86_MATCH_VFM(INTEL_BROADWELL_D, &rapl_defaults_core), |
| 1258 |
X86_MATCH_VFM(INTEL_BROADWELL_X, &rapl_defaults_hsw_server), |
| 1259 |
|
| 1260 |
X86_MATCH_VFM(INTEL_SKYLAKE, &rapl_defaults_core), |
| 1261 |
X86_MATCH_VFM(INTEL_SKYLAKE_L, &rapl_defaults_core), |
| 1262 |
X86_MATCH_VFM(INTEL_SKYLAKE_X, &rapl_defaults_hsw_server), |
| 1263 |
X86_MATCH_VFM(INTEL_KABYLAKE_L, &rapl_defaults_core), |
| 1264 |
X86_MATCH_VFM(INTEL_KABYLAKE, &rapl_defaults_core), |
| 1265 |
X86_MATCH_VFM(INTEL_CANNONLAKE_L, &rapl_defaults_core), |
| 1266 |
X86_MATCH_VFM(INTEL_ICELAKE_L, &rapl_defaults_core), |
| 1267 |
X86_MATCH_VFM(INTEL_ICELAKE, &rapl_defaults_core), |
| 1268 |
X86_MATCH_VFM(INTEL_ICELAKE_NNPI, &rapl_defaults_core), |
| 1269 |
X86_MATCH_VFM(INTEL_ICELAKE_X, &rapl_defaults_hsw_server), |
| 1270 |
X86_MATCH_VFM(INTEL_ICELAKE_D, &rapl_defaults_hsw_server), |
| 1271 |
X86_MATCH_VFM(INTEL_COMETLAKE_L, &rapl_defaults_core), |
| 1272 |
X86_MATCH_VFM(INTEL_COMETLAKE, &rapl_defaults_core), |
| 1273 |
X86_MATCH_VFM(INTEL_TIGERLAKE_L, &rapl_defaults_core), |
| 1274 |
X86_MATCH_VFM(INTEL_TIGERLAKE, &rapl_defaults_core), |
| 1275 |
X86_MATCH_VFM(INTEL_ROCKETLAKE, &rapl_defaults_core), |
| 1276 |
X86_MATCH_VFM(INTEL_ALDERLAKE, &rapl_defaults_core), |
| 1277 |
X86_MATCH_VFM(INTEL_ALDERLAKE_L, &rapl_defaults_core), |
| 1278 |
X86_MATCH_VFM(INTEL_ATOM_GRACEMONT, &rapl_defaults_core), |
| 1279 |
X86_MATCH_VFM(INTEL_RAPTORLAKE, &rapl_defaults_core), |
| 1280 |
X86_MATCH_VFM(INTEL_RAPTORLAKE_P, &rapl_defaults_core), |
| 1281 |
X86_MATCH_VFM(INTEL_RAPTORLAKE_S, &rapl_defaults_core), |
| 1282 |
X86_MATCH_VFM(INTEL_BARTLETTLAKE, &rapl_defaults_core), |
| 1283 |
X86_MATCH_VFM(INTEL_METEORLAKE, &rapl_defaults_core), |
| 1284 |
X86_MATCH_VFM(INTEL_METEORLAKE_L, &rapl_defaults_core), |
| 1285 |
X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &rapl_defaults_spr_server), |
| 1286 |
X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &rapl_defaults_spr_server), |
| 1287 |
X86_MATCH_VFM(INTEL_LUNARLAKE_M, &rapl_defaults_core), |
| 1288 |
X86_MATCH_VFM(INTEL_PANTHERLAKE_L, &rapl_defaults_core), |
| 1289 |
X86_MATCH_VFM(INTEL_WILDCATLAKE_L, &rapl_defaults_core), |
| 1290 |
X86_MATCH_VFM(INTEL_NOVALAKE, &rapl_defaults_core), |
| 1291 |
X86_MATCH_VFM(INTEL_NOVALAKE_L, &rapl_defaults_core), |
| 1292 |
X86_MATCH_VFM(INTEL_ARROWLAKE_H, &rapl_defaults_core), |
| 1293 |
X86_MATCH_VFM(INTEL_ARROWLAKE, &rapl_defaults_core), |
| 1294 |
X86_MATCH_VFM(INTEL_ARROWLAKE_U, &rapl_defaults_core), |
| 1295 |
X86_MATCH_VFM(INTEL_LAKEFIELD, &rapl_defaults_core), |
| 1296 |
|
| 1297 |
X86_MATCH_VFM(INTEL_ATOM_SILVERMONT, &rapl_defaults_byt), |
| 1298 |
X86_MATCH_VFM(INTEL_ATOM_AIRMONT, &rapl_defaults_cht), |
| 1299 |
X86_MATCH_VFM(INTEL_ATOM_SILVERMONT_MID, &rapl_defaults_tng), |
| 1300 |
X86_MATCH_VFM(INTEL_ATOM_SILVERMONT_MID2,&rapl_defaults_ann), |
| 1301 |
X86_MATCH_VFM(INTEL_ATOM_GOLDMONT, &rapl_defaults_core), |
| 1302 |
X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_PLUS, &rapl_defaults_core), |
| 1303 |
X86_MATCH_VFM(INTEL_ATOM_GOLDMONT_D, &rapl_defaults_core), |
| 1304 |
X86_MATCH_VFM(INTEL_ATOM_TREMONT, &rapl_defaults_core), |
| 1305 |
X86_MATCH_VFM(INTEL_ATOM_TREMONT_D, &rapl_defaults_core), |
| 1306 |
X86_MATCH_VFM(INTEL_ATOM_TREMONT_L, &rapl_defaults_core), |
| 1307 |
|
| 1308 |
X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &rapl_defaults_hsw_server), |
| 1309 |
X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &rapl_defaults_hsw_server), |
| 1310 |
|
| 1311 |
X86_MATCH_VENDOR_FAM(AMD, 0x17, &rapl_defaults_amd), |
| 1312 |
X86_MATCH_VENDOR_FAM(AMD, 0x19, &rapl_defaults_amd), |
| 1313 |
X86_MATCH_VENDOR_FAM(AMD, 0x1A, &rapl_defaults_amd), |
| 1314 |
X86_MATCH_VENDOR_FAM(HYGON, 0x18, &rapl_defaults_amd), |
| 1315 |
{} |
| 1316 |
}; |
| 1317 |
MODULE_DEVICE_TABLE(x86cpu, rapl_ids); |
| 1318 |
|
| 1319 |
/* Read once for all raw primitive data for domains */ |
| 1320 |
static void rapl_update_domain_data(struct rapl_package *rp) |
| 1321 |
{ |
| 1322 |
int dmn, prim; |
| 1323 |
u64 val; |
| 1324 |
|
| 1325 |
for (dmn = 0; dmn < rp->nr_domains; dmn++) { |
| 1326 |
pr_debug("update %s domain %s data\n", rp->name, |
| 1327 |
rp->domains[dmn].name); |
| 1328 |
/* exclude non-raw primitives */ |
| 1329 |
for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) { |
| 1330 |
struct rapl_primitive_info *rpi = get_rpi(rp, prim); |
| 1331 |
|
| 1332 |
if (!rapl_read_data_raw(&rp->domains[dmn], prim, |
| 1333 |
rpi->unit, &val, false)) |
| 1334 |
rp->domains[dmn].rdd.primitives[prim] = val; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
} |
| 1339 |
|
| 1340 |
static int rapl_package_register_powercap(struct rapl_package *rp) |
| 1341 |
{ |
| 1342 |
struct rapl_domain *rd; |
| 1343 |
struct powercap_zone *power_zone = NULL; |
| 1344 |
int nr_pl, ret; |
| 1345 |
|
| 1346 |
/* Update the domain data of the new package */ |
| 1347 |
rapl_update_domain_data(rp); |
| 1348 |
|
| 1349 |
/* first we register package domain as the parent zone */ |
| 1350 |
for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 1351 |
if (rd->id == RAPL_DOMAIN_PACKAGE) { |
| 1352 |
nr_pl = find_nr_power_limit(rd); |
| 1353 |
pr_debug("register package domain %s\n", rp->name); |
| 1354 |
power_zone = powercap_register_zone(&rd->power_zone, |
| 1355 |
rp->priv->control_type, rp->name, |
| 1356 |
NULL, &zone_ops[rd->id], nr_pl, |
| 1357 |
&constraint_ops); |
| 1358 |
if (IS_ERR(power_zone)) { |
| 1359 |
pr_debug("failed to register power zone %s\n", |
| 1360 |
rp->name); |
| 1361 |
return PTR_ERR(power_zone); |
| 1362 |
} |
| 1363 |
/* track parent zone in per package/socket data */ |
| 1364 |
rp->power_zone = power_zone; |
| 1365 |
/* done, only one package domain per socket */ |
| 1366 |
break; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
if (!power_zone) { |
| 1370 |
pr_err("no package domain found, unknown topology!\n"); |
| 1371 |
return -ENODEV; |
| 1372 |
} |
| 1373 |
/* now register domains as children of the socket/package */ |
| 1374 |
for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 1375 |
struct powercap_zone *parent = rp->power_zone; |
| 1376 |
|
| 1377 |
if (rd->id == RAPL_DOMAIN_PACKAGE) |
| 1378 |
continue; |
| 1379 |
if (rd->id == RAPL_DOMAIN_PLATFORM) |
| 1380 |
parent = NULL; |
| 1381 |
/* number of power limits per domain varies */ |
| 1382 |
nr_pl = find_nr_power_limit(rd); |
| 1383 |
power_zone = powercap_register_zone(&rd->power_zone, |
| 1384 |
rp->priv->control_type, |
| 1385 |
rd->name, parent, |
| 1386 |
&zone_ops[rd->id], nr_pl, |
| 1387 |
&constraint_ops); |
| 1388 |
|
| 1389 |
if (IS_ERR(power_zone)) { |
| 1390 |
pr_debug("failed to register power_zone, %s:%s\n", |
| 1391 |
rp->name, rd->name); |
| 1392 |
ret = PTR_ERR(power_zone); |
| 1393 |
goto err_cleanup; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
return 0; |
| 1397 |
|
| 1398 |
err_cleanup: |
| 1399 |
/* |
| 1400 |
* Clean up previously initialized domains within the package if we |
| 1401 |
* failed after the first domain setup. |
| 1402 |
*/ |
| 1403 |
while (--rd >= rp->domains) { |
| 1404 |
pr_debug("unregister %s domain %s\n", rp->name, rd->name); |
| 1405 |
powercap_unregister_zone(rp->priv->control_type, |
| 1406 |
&rd->power_zone); |
| 1407 |
} |
| 1408 |
|
| 1409 |
return ret; |
| 1410 |
} |
| 1411 |
|
| 1412 |
static int rapl_check_domain(int domain, struct rapl_package *rp) |
| 1413 |
{ |
| 1414 |
struct reg_action ra; |
| 1415 |
|
| 1416 |
switch (domain) { |
| 1417 |
case RAPL_DOMAIN_PACKAGE: |
| 1418 |
case RAPL_DOMAIN_PP0: |
| 1419 |
case RAPL_DOMAIN_PP1: |
| 1420 |
case RAPL_DOMAIN_DRAM: |
| 1421 |
case RAPL_DOMAIN_PLATFORM: |
| 1422 |
ra.reg = rp->priv->regs[domain][RAPL_DOMAIN_REG_STATUS]; |
| 1423 |
break; |
| 1424 |
default: |
| 1425 |
pr_err("invalid domain id %d\n", domain); |
| 1426 |
return -EINVAL; |
| 1427 |
} |
| 1428 |
/* make sure domain counters are available and contains non-zero |
| 1429 |
* values, otherwise skip it. |
| 1430 |
*/ |
| 1431 |
|
| 1432 |
ra.mask = ENERGY_STATUS_MASK; |
| 1433 |
if (rp->priv->read_raw(get_rid(rp), &ra, false) || !ra.value) |
| 1434 |
return -ENODEV; |
| 1435 |
|
| 1436 |
return 0; |
| 1437 |
} |
| 1438 |
|
| 1439 |
/* |
| 1440 |
* Get per domain energy/power/time unit. |
| 1441 |
* RAPL Interfaces without per domain unit register will use the package |
| 1442 |
* scope unit register to set per domain units. |
| 1443 |
*/ |
| 1444 |
static int rapl_get_domain_unit(struct rapl_domain *rd) |
| 1445 |
{ |
| 1446 |
struct rapl_defaults *defaults = get_defaults(rd->rp); |
| 1447 |
int ret; |
| 1448 |
|
| 1449 |
if (!rd->regs[RAPL_DOMAIN_REG_UNIT].val) { |
| 1450 |
if (!rd->rp->priv->reg_unit.val) { |
| 1451 |
pr_err("No valid Unit register found\n"); |
| 1452 |
return -ENODEV; |
| 1453 |
} |
| 1454 |
rd->regs[RAPL_DOMAIN_REG_UNIT] = rd->rp->priv->reg_unit; |
| 1455 |
} |
| 1456 |
|
| 1457 |
if (!defaults->check_unit) { |
| 1458 |
pr_err("missing .check_unit() callback\n"); |
| 1459 |
return -ENODEV; |
| 1460 |
} |
| 1461 |
|
| 1462 |
ret = defaults->check_unit(rd); |
| 1463 |
if (ret) |
| 1464 |
return ret; |
| 1465 |
|
| 1466 |
if (rd->id == RAPL_DOMAIN_DRAM && defaults->dram_domain_energy_unit) |
| 1467 |
rd->energy_unit = defaults->dram_domain_energy_unit; |
| 1468 |
if (rd->id == RAPL_DOMAIN_PLATFORM && defaults->psys_domain_energy_unit) |
| 1469 |
rd->energy_unit = defaults->psys_domain_energy_unit; |
| 1470 |
return 0; |
| 1471 |
} |
| 1472 |
|
| 1473 |
/* |
| 1474 |
* Check if power limits are available. Two cases when they are not available: |
| 1475 |
* 1. Locked by BIOS, in this case we still provide read-only access so that |
| 1476 |
* users can see what limit is set by the BIOS. |
| 1477 |
* 2. Some CPUs make some domains monitoring only which means PLx MSRs may not |
| 1478 |
* exist at all. In this case, we do not show the constraints in powercap. |
| 1479 |
* |
| 1480 |
* Called after domains are detected and initialized. |
| 1481 |
*/ |
| 1482 |
static void rapl_detect_powerlimit(struct rapl_domain *rd) |
| 1483 |
{ |
| 1484 |
u64 val64; |
| 1485 |
int i; |
| 1486 |
|
| 1487 |
for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) { |
| 1488 |
if (!rapl_read_pl_data(rd, i, PL_LOCK, false, &val64)) { |
| 1489 |
if (val64) { |
| 1490 |
rd->rpl[i].locked = true; |
| 1491 |
pr_info("%s:%s:%s locked by BIOS\n", |
| 1492 |
rd->rp->name, rd->name, pl_names[i]); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
|
| 1496 |
if (rapl_read_pl_data(rd, i, PL_LIMIT, false, &val64)) |
| 1497 |
rd->rpl[i].name = NULL; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
/* Detect active and valid domains for the given CPU, caller must |
| 1502 |
* ensure the CPU belongs to the targeted package and CPU hotlug is disabled. |
| 1503 |
*/ |
| 1504 |
static int rapl_detect_domains(struct rapl_package *rp) |
| 1505 |
{ |
| 1506 |
struct rapl_domain *rd; |
| 1507 |
int i; |
| 1508 |
|
| 1509 |
for (i = 0; i < RAPL_DOMAIN_MAX; i++) { |
| 1510 |
/* use physical package id to read counters */ |
| 1511 |
if (!rapl_check_domain(i, rp)) { |
| 1512 |
rp->domain_map |= 1 << i; |
| 1513 |
pr_info("Found RAPL domain %s\n", rapl_domain_names[i]); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX); |
| 1517 |
if (!rp->nr_domains) { |
| 1518 |
pr_debug("no valid rapl domains found in %s\n", rp->name); |
| 1519 |
return -ENODEV; |
| 1520 |
} |
| 1521 |
pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name); |
| 1522 |
|
| 1523 |
rp->domains = kzalloc_objs(struct rapl_domain, rp->nr_domains); |
| 1524 |
if (!rp->domains) |
| 1525 |
return -ENOMEM; |
| 1526 |
|
| 1527 |
rapl_init_domains(rp); |
| 1528 |
|
| 1529 |
for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 1530 |
rapl_get_domain_unit(rd); |
| 1531 |
rapl_detect_powerlimit(rd); |
| 1532 |
} |
| 1533 |
|
| 1534 |
return 0; |
| 1535 |
} |
| 1536 |
|
| 1537 |
#ifdef CONFIG_PERF_EVENTS |
| 1538 |
|
| 1539 |
/* |
| 1540 |
* Support for RAPL PMU |
| 1541 |
* |
| 1542 |
* Register a PMU if any of the registered RAPL Packages have the requirement |
| 1543 |
* of exposing its energy counters via Perf PMU. |
| 1544 |
* |
| 1545 |
* PMU Name: |
| 1546 |
* power |
| 1547 |
* |
| 1548 |
* Events: |
| 1549 |
* Name Event id RAPL Domain |
| 1550 |
* energy_cores 0x01 RAPL_DOMAIN_PP0 |
| 1551 |
* energy_pkg 0x02 RAPL_DOMAIN_PACKAGE |
| 1552 |
* energy_ram 0x03 RAPL_DOMAIN_DRAM |
| 1553 |
* energy_gpu 0x04 RAPL_DOMAIN_PP1 |
| 1554 |
* energy_psys 0x05 RAPL_DOMAIN_PLATFORM |
| 1555 |
* |
| 1556 |
* Unit: |
| 1557 |
* Joules |
| 1558 |
* |
| 1559 |
* Scale: |
| 1560 |
* 2.3283064365386962890625e-10 |
| 1561 |
* The same RAPL domain in different RAPL Packages may have different |
| 1562 |
* energy units. Use 2.3283064365386962890625e-10 (2^-32) Joules as |
| 1563 |
* the fixed unit for all energy counters, and covert each hardware |
| 1564 |
* counter increase to N times of PMU event counter increases. |
| 1565 |
* |
| 1566 |
* This is fully compatible with the current MSR RAPL PMU. This means that |
| 1567 |
* userspace programs like turbostat can use the same code to handle RAPL Perf |
| 1568 |
* PMU, no matter what RAPL Interface driver (MSR/TPMI, etc) is running |
| 1569 |
* underlying on the platform. |
| 1570 |
* |
| 1571 |
* Note that RAPL Packages can be probed/removed dynamically, and the events |
| 1572 |
* supported by each TPMI RAPL device can be different. Thus the RAPL PMU |
| 1573 |
* support is done on demand, which means |
| 1574 |
* 1. PMU is registered only if it is needed by a RAPL Package. PMU events for |
| 1575 |
* unsupported counters are not exposed. |
| 1576 |
* 2. PMU is unregistered and registered when a new RAPL Package is probed and |
| 1577 |
* supports new counters that are not supported by current PMU. |
| 1578 |
* 3. PMU is unregistered when all registered RAPL Packages don't need PMU. |
| 1579 |
*/ |
| 1580 |
|
| 1581 |
struct rapl_pmu { |
| 1582 |
struct pmu pmu; /* Perf PMU structure */ |
| 1583 |
u64 timer_ms; /* Maximum expiration time to avoid counter overflow */ |
| 1584 |
unsigned long domain_map; /* Events supported by current registered PMU */ |
| 1585 |
bool registered; /* Whether the PMU has been registered or not */ |
| 1586 |
}; |
| 1587 |
|
| 1588 |
static struct rapl_pmu rapl_pmu; |
| 1589 |
|
| 1590 |
/* PMU helpers */ |
| 1591 |
|
| 1592 |
static void set_pmu_cpumask(struct rapl_package *rp, cpumask_var_t mask) |
| 1593 |
{ |
| 1594 |
int cpu; |
| 1595 |
|
| 1596 |
if (!rp->has_pmu) |
| 1597 |
return; |
| 1598 |
|
| 1599 |
/* Only TPMI & MSR RAPL are supported for now */ |
| 1600 |
if (rp->priv->type != RAPL_IF_TPMI && rp->priv->type != RAPL_IF_MSR) |
| 1601 |
return; |
| 1602 |
|
| 1603 |
/* TPMI/MSR RAPL uses any CPU in the package for PMU */ |
| 1604 |
for_each_online_cpu(cpu) |
| 1605 |
if (topology_physical_package_id(cpu) == rp->id) |
| 1606 |
cpumask_set_cpu(cpu, mask); |
| 1607 |
} |
| 1608 |
|
| 1609 |
static bool is_rp_pmu_cpu(struct rapl_package *rp, int cpu) |
| 1610 |
{ |
| 1611 |
if (!rp->has_pmu) |
| 1612 |
return false; |
| 1613 |
|
| 1614 |
/* Only TPMI & MSR RAPL are supported for now */ |
| 1615 |
if (rp->priv->type != RAPL_IF_TPMI && rp->priv->type != RAPL_IF_MSR) |
| 1616 |
return false; |
| 1617 |
|
| 1618 |
/* TPMI/MSR RAPL uses any CPU in the package for PMU */ |
| 1619 |
return topology_physical_package_id(cpu) == rp->id; |
| 1620 |
} |
| 1621 |
|
| 1622 |
static struct rapl_package_pmu_data *event_to_pmu_data(struct perf_event *event) |
| 1623 |
{ |
| 1624 |
struct rapl_package *rp = event->pmu_private; |
| 1625 |
|
| 1626 |
return &rp->pmu_data; |
| 1627 |
} |
| 1628 |
|
| 1629 |
/* PMU event callbacks */ |
| 1630 |
|
| 1631 |
static u64 event_read_counter(struct perf_event *event) |
| 1632 |
{ |
| 1633 |
struct rapl_package *rp = event->pmu_private; |
| 1634 |
u64 val; |
| 1635 |
int ret; |
| 1636 |
|
| 1637 |
/* Return 0 for unsupported events */ |
| 1638 |
if (event->hw.idx < 0) |
| 1639 |
return 0; |
| 1640 |
|
| 1641 |
ret = rapl_read_data_raw(&rp->domains[event->hw.idx], ENERGY_COUNTER, false, &val, true); |
| 1642 |
|
| 1643 |
/* Return 0 for failed read */ |
| 1644 |
if (ret) |
| 1645 |
return 0; |
| 1646 |
|
| 1647 |
return val; |
| 1648 |
} |
| 1649 |
|
| 1650 |
static void __rapl_pmu_event_start(struct perf_event *event) |
| 1651 |
{ |
| 1652 |
struct rapl_package_pmu_data *data = event_to_pmu_data(event); |
| 1653 |
|
| 1654 |
if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
| 1655 |
return; |
| 1656 |
|
| 1657 |
event->hw.state = 0; |
| 1658 |
|
| 1659 |
list_add_tail(&event->active_entry, &data->active_list); |
| 1660 |
|
| 1661 |
local64_set(&event->hw.prev_count, event_read_counter(event)); |
| 1662 |
if (++data->n_active == 1) |
| 1663 |
hrtimer_start(&data->hrtimer, data->timer_interval, |
| 1664 |
HRTIMER_MODE_REL_PINNED); |
| 1665 |
} |
| 1666 |
|
| 1667 |
static void rapl_pmu_event_start(struct perf_event *event, int mode) |
| 1668 |
{ |
| 1669 |
struct rapl_package_pmu_data *data = event_to_pmu_data(event); |
| 1670 |
unsigned long flags; |
| 1671 |
|
| 1672 |
raw_spin_lock_irqsave(&data->lock, flags); |
| 1673 |
__rapl_pmu_event_start(event); |
| 1674 |
raw_spin_unlock_irqrestore(&data->lock, flags); |
| 1675 |
} |
| 1676 |
|
| 1677 |
static u64 rapl_event_update(struct perf_event *event) |
| 1678 |
{ |
| 1679 |
struct hw_perf_event *hwc = &event->hw; |
| 1680 |
struct rapl_package_pmu_data *data = event_to_pmu_data(event); |
| 1681 |
u64 prev_raw_count, new_raw_count; |
| 1682 |
s64 delta, sdelta; |
| 1683 |
|
| 1684 |
/* |
| 1685 |
* Follow the generic code to drain hwc->prev_count. |
| 1686 |
* The loop is not expected to run for multiple times. |
| 1687 |
*/ |
| 1688 |
prev_raw_count = local64_read(&hwc->prev_count); |
| 1689 |
do { |
| 1690 |
new_raw_count = event_read_counter(event); |
| 1691 |
} while (!local64_try_cmpxchg(&hwc->prev_count, |
| 1692 |
&prev_raw_count, new_raw_count)); |
| 1693 |
|
| 1694 |
|
| 1695 |
/* |
| 1696 |
* Now we have the new raw value and have updated the prev |
| 1697 |
* timestamp already. We can now calculate the elapsed delta |
| 1698 |
* (event-)time and add that to the generic event. |
| 1699 |
*/ |
| 1700 |
delta = new_raw_count - prev_raw_count; |
| 1701 |
|
| 1702 |
/* |
| 1703 |
* Scale delta to smallest unit (2^-32) |
| 1704 |
* users must then scale back: count * 1/(1e9*2^32) to get Joules |
| 1705 |
* or use ldexp(count, -32). |
| 1706 |
* Watts = Joules/Time delta |
| 1707 |
*/ |
| 1708 |
sdelta = delta * data->scale[event->hw.flags]; |
| 1709 |
|
| 1710 |
local64_add(sdelta, &event->count); |
| 1711 |
|
| 1712 |
return new_raw_count; |
| 1713 |
} |
| 1714 |
|
| 1715 |
static void rapl_pmu_event_stop(struct perf_event *event, int mode) |
| 1716 |
{ |
| 1717 |
struct rapl_package_pmu_data *data = event_to_pmu_data(event); |
| 1718 |
struct hw_perf_event *hwc = &event->hw; |
| 1719 |
unsigned long flags; |
| 1720 |
|
| 1721 |
raw_spin_lock_irqsave(&data->lock, flags); |
| 1722 |
|
| 1723 |
/* Mark event as deactivated and stopped */ |
| 1724 |
if (!(hwc->state & PERF_HES_STOPPED)) { |
| 1725 |
WARN_ON_ONCE(data->n_active <= 0); |
| 1726 |
if (--data->n_active == 0) |
| 1727 |
hrtimer_cancel(&data->hrtimer); |
| 1728 |
|
| 1729 |
list_del(&event->active_entry); |
| 1730 |
|
| 1731 |
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); |
| 1732 |
hwc->state |= PERF_HES_STOPPED; |
| 1733 |
} |
| 1734 |
|
| 1735 |
/* Check if update of sw counter is necessary */ |
| 1736 |
if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
| 1737 |
/* |
| 1738 |
* Drain the remaining delta count out of a event |
| 1739 |
* that we are disabling: |
| 1740 |
*/ |
| 1741 |
rapl_event_update(event); |
| 1742 |
hwc->state |= PERF_HES_UPTODATE; |
| 1743 |
} |
| 1744 |
|
| 1745 |
raw_spin_unlock_irqrestore(&data->lock, flags); |
| 1746 |
} |
| 1747 |
|
| 1748 |
static int rapl_pmu_event_add(struct perf_event *event, int mode) |
| 1749 |
{ |
| 1750 |
struct rapl_package_pmu_data *data = event_to_pmu_data(event); |
| 1751 |
struct hw_perf_event *hwc = &event->hw; |
| 1752 |
unsigned long flags; |
| 1753 |
|
| 1754 |
raw_spin_lock_irqsave(&data->lock, flags); |
| 1755 |
|
| 1756 |
hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 1757 |
|
| 1758 |
if (mode & PERF_EF_START) |
| 1759 |
__rapl_pmu_event_start(event); |
| 1760 |
|
| 1761 |
raw_spin_unlock_irqrestore(&data->lock, flags); |
| 1762 |
|
| 1763 |
return 0; |
| 1764 |
} |
| 1765 |
|
| 1766 |
static void rapl_pmu_event_del(struct perf_event *event, int flags) |
| 1767 |
{ |
| 1768 |
rapl_pmu_event_stop(event, PERF_EF_UPDATE); |
| 1769 |
} |
| 1770 |
|
| 1771 |
/* RAPL PMU event ids, same as shown in sysfs */ |
| 1772 |
enum perf_rapl_events { |
| 1773 |
PERF_RAPL_PP0 = 1, /* all cores */ |
| 1774 |
PERF_RAPL_PKG, /* entire package */ |
| 1775 |
PERF_RAPL_RAM, /* DRAM */ |
| 1776 |
PERF_RAPL_PP1, /* gpu */ |
| 1777 |
PERF_RAPL_PSYS, /* psys */ |
| 1778 |
PERF_RAPL_MAX |
| 1779 |
}; |
| 1780 |
#define RAPL_EVENT_MASK GENMASK(7, 0) |
| 1781 |
|
| 1782 |
static const int event_to_domain[PERF_RAPL_MAX] = { |
| 1783 |
[PERF_RAPL_PP0] = RAPL_DOMAIN_PP0, |
| 1784 |
[PERF_RAPL_PKG] = RAPL_DOMAIN_PACKAGE, |
| 1785 |
[PERF_RAPL_RAM] = RAPL_DOMAIN_DRAM, |
| 1786 |
[PERF_RAPL_PP1] = RAPL_DOMAIN_PP1, |
| 1787 |
[PERF_RAPL_PSYS] = RAPL_DOMAIN_PLATFORM, |
| 1788 |
}; |
| 1789 |
|
| 1790 |
static int rapl_pmu_event_init(struct perf_event *event) |
| 1791 |
{ |
| 1792 |
struct rapl_package *pos, *rp = NULL; |
| 1793 |
u64 cfg = event->attr.config & RAPL_EVENT_MASK; |
| 1794 |
int domain, idx; |
| 1795 |
|
| 1796 |
/* Only look at RAPL events */ |
| 1797 |
if (event->attr.type != event->pmu->type) |
| 1798 |
return -ENOENT; |
| 1799 |
|
| 1800 |
/* Check for supported events only */ |
| 1801 |
if (!cfg || cfg >= PERF_RAPL_MAX) |
| 1802 |
return -EINVAL; |
| 1803 |
|
| 1804 |
if (event->cpu < 0) |
| 1805 |
return -EINVAL; |
| 1806 |
|
| 1807 |
/* Find out which Package the event belongs to */ |
| 1808 |
list_for_each_entry(pos, &rapl_packages, plist) { |
| 1809 |
if (is_rp_pmu_cpu(pos, event->cpu)) { |
| 1810 |
rp = pos; |
| 1811 |
break; |
| 1812 |
} |
| 1813 |
} |
| 1814 |
if (!rp) |
| 1815 |
return -ENODEV; |
| 1816 |
|
| 1817 |
/* Find out which RAPL Domain the event belongs to */ |
| 1818 |
domain = event_to_domain[cfg]; |
| 1819 |
|
| 1820 |
event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG; |
| 1821 |
event->pmu_private = rp; /* Which package */ |
| 1822 |
event->hw.flags = domain; /* Which domain */ |
| 1823 |
|
| 1824 |
event->hw.idx = -1; |
| 1825 |
/* Find out the index in rp->domains[] to get domain pointer */ |
| 1826 |
for (idx = 0; idx < rp->nr_domains; idx++) { |
| 1827 |
if (rp->domains[idx].id == domain) { |
| 1828 |
event->hw.idx = idx; |
| 1829 |
break; |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
return 0; |
| 1834 |
} |
| 1835 |
|
| 1836 |
static void rapl_pmu_event_read(struct perf_event *event) |
| 1837 |
{ |
| 1838 |
rapl_event_update(event); |
| 1839 |
} |
| 1840 |
|
| 1841 |
static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer) |
| 1842 |
{ |
| 1843 |
struct rapl_package_pmu_data *data = |
| 1844 |
container_of(hrtimer, struct rapl_package_pmu_data, hrtimer); |
| 1845 |
struct perf_event *event; |
| 1846 |
unsigned long flags; |
| 1847 |
|
| 1848 |
if (!data->n_active) |
| 1849 |
return HRTIMER_NORESTART; |
| 1850 |
|
| 1851 |
raw_spin_lock_irqsave(&data->lock, flags); |
| 1852 |
|
| 1853 |
list_for_each_entry(event, &data->active_list, active_entry) |
| 1854 |
rapl_event_update(event); |
| 1855 |
|
| 1856 |
raw_spin_unlock_irqrestore(&data->lock, flags); |
| 1857 |
|
| 1858 |
hrtimer_forward_now(hrtimer, data->timer_interval); |
| 1859 |
|
| 1860 |
return HRTIMER_RESTART; |
| 1861 |
} |
| 1862 |
|
| 1863 |
/* PMU sysfs attributes */ |
| 1864 |
|
| 1865 |
/* |
| 1866 |
* There are no default events, but we need to create "events" group (with |
| 1867 |
* empty attrs) before updating it with detected events. |
| 1868 |
*/ |
| 1869 |
static struct attribute *attrs_empty[] = { |
| 1870 |
NULL, |
| 1871 |
}; |
| 1872 |
|
| 1873 |
static struct attribute_group pmu_events_group = { |
| 1874 |
.name = "events", |
| 1875 |
.attrs = attrs_empty, |
| 1876 |
}; |
| 1877 |
|
| 1878 |
static ssize_t cpumask_show(struct device *dev, |
| 1879 |
struct device_attribute *attr, char *buf) |
| 1880 |
{ |
| 1881 |
struct rapl_package *rp; |
| 1882 |
cpumask_var_t cpu_mask; |
| 1883 |
int ret; |
| 1884 |
|
| 1885 |
if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL)) |
| 1886 |
return -ENOMEM; |
| 1887 |
|
| 1888 |
cpus_read_lock(); |
| 1889 |
|
| 1890 |
cpumask_clear(cpu_mask); |
| 1891 |
|
| 1892 |
/* Choose a cpu for each RAPL Package */ |
| 1893 |
list_for_each_entry(rp, &rapl_packages, plist) { |
| 1894 |
set_pmu_cpumask(rp, cpu_mask); |
| 1895 |
} |
| 1896 |
cpus_read_unlock(); |
| 1897 |
|
| 1898 |
ret = cpumap_print_to_pagebuf(true, buf, cpu_mask); |
| 1899 |
|
| 1900 |
free_cpumask_var(cpu_mask); |
| 1901 |
|
| 1902 |
return ret; |
| 1903 |
} |
| 1904 |
|
| 1905 |
static DEVICE_ATTR_RO(cpumask); |
| 1906 |
|
| 1907 |
static struct attribute *pmu_cpumask_attrs[] = { |
| 1908 |
&dev_attr_cpumask.attr, |
| 1909 |
NULL |
| 1910 |
}; |
| 1911 |
|
| 1912 |
static struct attribute_group pmu_cpumask_group = { |
| 1913 |
.attrs = pmu_cpumask_attrs, |
| 1914 |
}; |
| 1915 |
|
| 1916 |
PMU_FORMAT_ATTR(event, "config:0-7"); |
| 1917 |
static struct attribute *pmu_format_attr[] = { |
| 1918 |
&format_attr_event.attr, |
| 1919 |
NULL |
| 1920 |
}; |
| 1921 |
|
| 1922 |
static struct attribute_group pmu_format_group = { |
| 1923 |
.name = "format", |
| 1924 |
.attrs = pmu_format_attr, |
| 1925 |
}; |
| 1926 |
|
| 1927 |
static const struct attribute_group *pmu_attr_groups[] = { |
| 1928 |
&pmu_events_group, |
| 1929 |
&pmu_cpumask_group, |
| 1930 |
&pmu_format_group, |
| 1931 |
NULL |
| 1932 |
}; |
| 1933 |
|
| 1934 |
#define RAPL_EVENT_ATTR_STR(_name, v, str) \ |
| 1935 |
static struct perf_pmu_events_attr event_attr_##v = { \ |
| 1936 |
.attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ |
| 1937 |
.event_str = str, \ |
| 1938 |
} |
| 1939 |
|
| 1940 |
RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01"); |
| 1941 |
RAPL_EVENT_ATTR_STR(energy-pkg, rapl_pkg, "event=0x02"); |
| 1942 |
RAPL_EVENT_ATTR_STR(energy-ram, rapl_ram, "event=0x03"); |
| 1943 |
RAPL_EVENT_ATTR_STR(energy-gpu, rapl_gpu, "event=0x04"); |
| 1944 |
RAPL_EVENT_ATTR_STR(energy-psys, rapl_psys, "event=0x05"); |
| 1945 |
|
| 1946 |
RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_unit_cores, "Joules"); |
| 1947 |
RAPL_EVENT_ATTR_STR(energy-pkg.unit, rapl_unit_pkg, "Joules"); |
| 1948 |
RAPL_EVENT_ATTR_STR(energy-ram.unit, rapl_unit_ram, "Joules"); |
| 1949 |
RAPL_EVENT_ATTR_STR(energy-gpu.unit, rapl_unit_gpu, "Joules"); |
| 1950 |
RAPL_EVENT_ATTR_STR(energy-psys.unit, rapl_unit_psys, "Joules"); |
| 1951 |
|
| 1952 |
RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_scale_cores, "2.3283064365386962890625e-10"); |
| 1953 |
RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_scale_pkg, "2.3283064365386962890625e-10"); |
| 1954 |
RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_scale_ram, "2.3283064365386962890625e-10"); |
| 1955 |
RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_scale_gpu, "2.3283064365386962890625e-10"); |
| 1956 |
RAPL_EVENT_ATTR_STR(energy-psys.scale, rapl_scale_psys, "2.3283064365386962890625e-10"); |
| 1957 |
|
| 1958 |
#define RAPL_EVENT_GROUP(_name, domain) \ |
| 1959 |
static struct attribute *pmu_attr_##_name[] = { \ |
| 1960 |
&event_attr_rapl_##_name.attr.attr, \ |
| 1961 |
&event_attr_rapl_unit_##_name.attr.attr, \ |
| 1962 |
&event_attr_rapl_scale_##_name.attr.attr, \ |
| 1963 |
NULL \ |
| 1964 |
}; \ |
| 1965 |
static umode_t is_visible_##_name(struct kobject *kobj, struct attribute *attr, int event) \ |
| 1966 |
{ \ |
| 1967 |
return rapl_pmu.domain_map & BIT(domain) ? attr->mode : 0; \ |
| 1968 |
} \ |
| 1969 |
static struct attribute_group pmu_group_##_name = { \ |
| 1970 |
.name = "events", \ |
| 1971 |
.attrs = pmu_attr_##_name, \ |
| 1972 |
.is_visible = is_visible_##_name, \ |
| 1973 |
} |
| 1974 |
|
| 1975 |
RAPL_EVENT_GROUP(cores, RAPL_DOMAIN_PP0); |
| 1976 |
RAPL_EVENT_GROUP(pkg, RAPL_DOMAIN_PACKAGE); |
| 1977 |
RAPL_EVENT_GROUP(ram, RAPL_DOMAIN_DRAM); |
| 1978 |
RAPL_EVENT_GROUP(gpu, RAPL_DOMAIN_PP1); |
| 1979 |
RAPL_EVENT_GROUP(psys, RAPL_DOMAIN_PLATFORM); |
| 1980 |
|
| 1981 |
static const struct attribute_group *pmu_attr_update[] = { |
| 1982 |
&pmu_group_cores, |
| 1983 |
&pmu_group_pkg, |
| 1984 |
&pmu_group_ram, |
| 1985 |
&pmu_group_gpu, |
| 1986 |
&pmu_group_psys, |
| 1987 |
NULL |
| 1988 |
}; |
| 1989 |
|
| 1990 |
static int rapl_pmu_update(struct rapl_package *rp) |
| 1991 |
{ |
| 1992 |
int ret = 0; |
| 1993 |
|
| 1994 |
/* Return if PMU already covers all events supported by current RAPL Package */ |
| 1995 |
if (rapl_pmu.registered && !(rp->domain_map & (~rapl_pmu.domain_map))) |
| 1996 |
goto end; |
| 1997 |
|
| 1998 |
/* Unregister previous registered PMU */ |
| 1999 |
if (rapl_pmu.registered) |
| 2000 |
perf_pmu_unregister(&rapl_pmu.pmu); |
| 2001 |
|
| 2002 |
rapl_pmu.registered = false; |
| 2003 |
rapl_pmu.domain_map |= rp->domain_map; |
| 2004 |
|
| 2005 |
memset(&rapl_pmu.pmu, 0, sizeof(struct pmu)); |
| 2006 |
rapl_pmu.pmu.attr_groups = pmu_attr_groups; |
| 2007 |
rapl_pmu.pmu.attr_update = pmu_attr_update; |
| 2008 |
rapl_pmu.pmu.task_ctx_nr = perf_invalid_context; |
| 2009 |
rapl_pmu.pmu.event_init = rapl_pmu_event_init; |
| 2010 |
rapl_pmu.pmu.add = rapl_pmu_event_add; |
| 2011 |
rapl_pmu.pmu.del = rapl_pmu_event_del; |
| 2012 |
rapl_pmu.pmu.start = rapl_pmu_event_start; |
| 2013 |
rapl_pmu.pmu.stop = rapl_pmu_event_stop; |
| 2014 |
rapl_pmu.pmu.read = rapl_pmu_event_read; |
| 2015 |
rapl_pmu.pmu.module = THIS_MODULE; |
| 2016 |
rapl_pmu.pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT; |
| 2017 |
ret = perf_pmu_register(&rapl_pmu.pmu, "power", -1); |
| 2018 |
if (ret) { |
| 2019 |
pr_info("Failed to register PMU\n"); |
| 2020 |
return ret; |
| 2021 |
} |
| 2022 |
|
| 2023 |
rapl_pmu.registered = true; |
| 2024 |
end: |
| 2025 |
rp->has_pmu = true; |
| 2026 |
return ret; |
| 2027 |
} |
| 2028 |
|
| 2029 |
int rapl_package_add_pmu_locked(struct rapl_package *rp) |
| 2030 |
{ |
| 2031 |
struct rapl_package_pmu_data *data = &rp->pmu_data; |
| 2032 |
int idx; |
| 2033 |
|
| 2034 |
if (rp->has_pmu) |
| 2035 |
return -EEXIST; |
| 2036 |
|
| 2037 |
for (idx = 0; idx < rp->nr_domains; idx++) { |
| 2038 |
struct rapl_domain *rd = &rp->domains[idx]; |
| 2039 |
int domain = rd->id; |
| 2040 |
u64 val; |
| 2041 |
|
| 2042 |
if (!test_bit(domain, &rp->domain_map)) |
| 2043 |
continue; |
| 2044 |
|
| 2045 |
/* |
| 2046 |
* The RAPL PMU granularity is 2^-32 Joules |
| 2047 |
* data->scale[]: times of 2^-32 Joules for each ENERGY COUNTER increase |
| 2048 |
*/ |
| 2049 |
val = rd->energy_unit * (1ULL << 32); |
| 2050 |
do_div(val, ENERGY_UNIT_SCALE * 1000000); |
| 2051 |
data->scale[domain] = val; |
| 2052 |
|
| 2053 |
if (!rapl_pmu.timer_ms) { |
| 2054 |
struct rapl_primitive_info *rpi = get_rpi(rp, ENERGY_COUNTER); |
| 2055 |
|
| 2056 |
/* |
| 2057 |
* Calculate the timer rate: |
| 2058 |
* Use reference of 200W for scaling the timeout to avoid counter |
| 2059 |
* overflows. |
| 2060 |
* |
| 2061 |
* max_count = rpi->mask >> rpi->shift + 1 |
| 2062 |
* max_energy_pj = max_count * rd->energy_unit |
| 2063 |
* max_time_sec = (max_energy_pj / 1000000000) / 200w |
| 2064 |
* |
| 2065 |
* rapl_pmu.timer_ms = max_time_sec * 1000 / 2 |
| 2066 |
*/ |
| 2067 |
val = (rpi->mask >> rpi->shift) + 1; |
| 2068 |
val *= rd->energy_unit; |
| 2069 |
do_div(val, 1000000 * 200 * 2); |
| 2070 |
rapl_pmu.timer_ms = val; |
| 2071 |
|
| 2072 |
pr_debug("%llu ms overflow timer\n", rapl_pmu.timer_ms); |
| 2073 |
} |
| 2074 |
|
| 2075 |
pr_debug("Domain %s: hw unit %lld * 2^-32 Joules\n", rd->name, data->scale[domain]); |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* Initialize per package PMU data */ |
| 2079 |
raw_spin_lock_init(&data->lock); |
| 2080 |
INIT_LIST_HEAD(&data->active_list); |
| 2081 |
data->timer_interval = ms_to_ktime(rapl_pmu.timer_ms); |
| 2082 |
hrtimer_setup(&data->hrtimer, rapl_hrtimer_handle, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2083 |
|
| 2084 |
return rapl_pmu_update(rp); |
| 2085 |
} |
| 2086 |
EXPORT_SYMBOL_GPL(rapl_package_add_pmu_locked); |
| 2087 |
|
| 2088 |
int rapl_package_add_pmu(struct rapl_package *rp) |
| 2089 |
{ |
| 2090 |
guard(cpus_read_lock)(); |
| 2091 |
|
| 2092 |
return rapl_package_add_pmu_locked(rp); |
| 2093 |
} |
| 2094 |
EXPORT_SYMBOL_GPL(rapl_package_add_pmu); |
| 2095 |
|
| 2096 |
void rapl_package_remove_pmu_locked(struct rapl_package *rp) |
| 2097 |
{ |
| 2098 |
struct rapl_package *pos; |
| 2099 |
|
| 2100 |
if (!rp->has_pmu) |
| 2101 |
return; |
| 2102 |
|
| 2103 |
list_for_each_entry(pos, &rapl_packages, plist) { |
| 2104 |
/* PMU is still needed */ |
| 2105 |
if (pos->has_pmu && pos != rp) |
| 2106 |
return; |
| 2107 |
} |
| 2108 |
|
| 2109 |
perf_pmu_unregister(&rapl_pmu.pmu); |
| 2110 |
memset(&rapl_pmu, 0, sizeof(struct rapl_pmu)); |
| 2111 |
} |
| 2112 |
EXPORT_SYMBOL_GPL(rapl_package_remove_pmu_locked); |
| 2113 |
|
| 2114 |
void rapl_package_remove_pmu(struct rapl_package *rp) |
| 2115 |
{ |
| 2116 |
guard(cpus_read_lock)(); |
| 2117 |
|
| 2118 |
rapl_package_remove_pmu_locked(rp); |
| 2119 |
} |
| 2120 |
EXPORT_SYMBOL_GPL(rapl_package_remove_pmu); |
| 2121 |
#endif |
| 2122 |
|
| 2123 |
/* called from CPU hotplug notifier, hotplug lock held */ |
| 2124 |
void rapl_remove_package_cpuslocked(struct rapl_package *rp) |
| 2125 |
{ |
| 2126 |
struct rapl_domain *rd, *rd_package = NULL; |
| 2127 |
|
| 2128 |
package_power_limit_irq_restore(rp); |
| 2129 |
|
| 2130 |
for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) { |
| 2131 |
int i; |
| 2132 |
|
| 2133 |
for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) { |
| 2134 |
rapl_write_pl_data(rd, i, PL_ENABLE, 0); |
| 2135 |
rapl_write_pl_data(rd, i, PL_CLAMP, 0); |
| 2136 |
} |
| 2137 |
|
| 2138 |
if (rd->id == RAPL_DOMAIN_PACKAGE) { |
| 2139 |
rd_package = rd; |
| 2140 |
continue; |
| 2141 |
} |
| 2142 |
pr_debug("remove package, undo power limit on %s: %s\n", |
| 2143 |
rp->name, rd->name); |
| 2144 |
powercap_unregister_zone(rp->priv->control_type, |
| 2145 |
&rd->power_zone); |
| 2146 |
} |
| 2147 |
/* do parent zone last */ |
| 2148 |
powercap_unregister_zone(rp->priv->control_type, |
| 2149 |
&rd_package->power_zone); |
| 2150 |
list_del(&rp->plist); |
| 2151 |
kfree(rp); |
| 2152 |
} |
| 2153 |
EXPORT_SYMBOL_GPL(rapl_remove_package_cpuslocked); |
| 2154 |
|
| 2155 |
void rapl_remove_package(struct rapl_package *rp) |
| 2156 |
{ |
| 2157 |
guard(cpus_read_lock)(); |
| 2158 |
rapl_remove_package_cpuslocked(rp); |
| 2159 |
} |
| 2160 |
EXPORT_SYMBOL_GPL(rapl_remove_package); |
| 2161 |
|
| 2162 |
/* |
| 2163 |
* RAPL Package energy counter scope: |
| 2164 |
* 1. AMD/HYGON platforms use per-PKG package energy counter |
| 2165 |
* 2. For Intel platforms |
| 2166 |
* 2.1 CLX-AP platform has per-DIE package energy counter |
| 2167 |
* 2.2 Other platforms that uses MSR RAPL are single die systems so the |
| 2168 |
* package energy counter can be considered as per-PKG/per-DIE, |
| 2169 |
* here it is considered as per-DIE. |
| 2170 |
* 2.3 New platforms that use TPMI RAPL doesn't care about the |
| 2171 |
* scope because they are not MSR/CPU based. |
| 2172 |
*/ |
| 2173 |
#define rapl_msrs_are_pkg_scope() \ |
| 2174 |
(boot_cpu_data.x86_vendor == X86_VENDOR_AMD || \ |
| 2175 |
boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) |
| 2176 |
|
| 2177 |
/* caller to ensure CPU hotplug lock is held */ |
| 2178 |
struct rapl_package *rapl_find_package_domain_cpuslocked(int id, struct rapl_if_priv *priv, |
| 2179 |
bool id_is_cpu) |
| 2180 |
{ |
| 2181 |
struct rapl_package *rp; |
| 2182 |
int uid; |
| 2183 |
|
| 2184 |
if (id_is_cpu) { |
| 2185 |
uid = rapl_msrs_are_pkg_scope() ? |
| 2186 |
topology_physical_package_id(id) : topology_logical_die_id(id); |
| 2187 |
if (uid < 0) { |
| 2188 |
pr_err("topology_logical_(package/die)_id() returned a negative value"); |
| 2189 |
return NULL; |
| 2190 |
} |
| 2191 |
} |
| 2192 |
else |
| 2193 |
uid = id; |
| 2194 |
|
| 2195 |
list_for_each_entry(rp, &rapl_packages, plist) { |
| 2196 |
if (rp->id == uid |
| 2197 |
&& rp->priv->control_type == priv->control_type) |
| 2198 |
return rp; |
| 2199 |
} |
| 2200 |
|
| 2201 |
return NULL; |
| 2202 |
} |
| 2203 |
EXPORT_SYMBOL_GPL(rapl_find_package_domain_cpuslocked); |
| 2204 |
|
| 2205 |
struct rapl_package *rapl_find_package_domain(int id, struct rapl_if_priv *priv, bool id_is_cpu) |
| 2206 |
{ |
| 2207 |
guard(cpus_read_lock)(); |
| 2208 |
return rapl_find_package_domain_cpuslocked(id, priv, id_is_cpu); |
| 2209 |
} |
| 2210 |
EXPORT_SYMBOL_GPL(rapl_find_package_domain); |
| 2211 |
|
| 2212 |
/* called from CPU hotplug notifier, hotplug lock held */ |
| 2213 |
struct rapl_package *rapl_add_package_cpuslocked(int id, struct rapl_if_priv *priv, bool id_is_cpu) |
| 2214 |
{ |
| 2215 |
struct rapl_package *rp; |
| 2216 |
int ret; |
| 2217 |
|
| 2218 |
rp = kzalloc_obj(struct rapl_package); |
| 2219 |
if (!rp) |
| 2220 |
return ERR_PTR(-ENOMEM); |
| 2221 |
|
| 2222 |
if (id_is_cpu) { |
| 2223 |
rp->id = rapl_msrs_are_pkg_scope() ? |
| 2224 |
topology_physical_package_id(id) : topology_logical_die_id(id); |
| 2225 |
if ((int)(rp->id) < 0) { |
| 2226 |
pr_err("topology_logical_(package/die)_id() returned a negative value"); |
| 2227 |
return ERR_PTR(-EINVAL); |
| 2228 |
} |
| 2229 |
rp->lead_cpu = id; |
| 2230 |
if (!rapl_msrs_are_pkg_scope() && topology_max_dies_per_package() > 1) |
| 2231 |
snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d-die-%d", |
| 2232 |
topology_physical_package_id(id), topology_die_id(id)); |
| 2233 |
else |
| 2234 |
snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d", |
| 2235 |
topology_physical_package_id(id)); |
| 2236 |
} else { |
| 2237 |
rp->id = id; |
| 2238 |
rp->lead_cpu = -1; |
| 2239 |
snprintf(rp->name, PACKAGE_DOMAIN_NAME_LENGTH, "package-%d", id); |
| 2240 |
} |
| 2241 |
|
| 2242 |
rp->priv = priv; |
| 2243 |
ret = rapl_config(rp); |
| 2244 |
if (ret) |
| 2245 |
goto err_free_package; |
| 2246 |
|
| 2247 |
/* check if the package contains valid domains */ |
| 2248 |
if (rapl_detect_domains(rp)) { |
| 2249 |
ret = -ENODEV; |
| 2250 |
goto err_free_package; |
| 2251 |
} |
| 2252 |
ret = rapl_package_register_powercap(rp); |
| 2253 |
if (!ret) { |
| 2254 |
INIT_LIST_HEAD(&rp->plist); |
| 2255 |
list_add(&rp->plist, &rapl_packages); |
| 2256 |
return rp; |
| 2257 |
} |
| 2258 |
|
| 2259 |
err_free_package: |
| 2260 |
kfree(rp->domains); |
| 2261 |
kfree(rp); |
| 2262 |
return ERR_PTR(ret); |
| 2263 |
} |
| 2264 |
EXPORT_SYMBOL_GPL(rapl_add_package_cpuslocked); |
| 2265 |
|
| 2266 |
struct rapl_package *rapl_add_package(int id, struct rapl_if_priv *priv, bool id_is_cpu) |
| 2267 |
{ |
| 2268 |
guard(cpus_read_lock)(); |
| 2269 |
return rapl_add_package_cpuslocked(id, priv, id_is_cpu); |
| 2270 |
} |
| 2271 |
EXPORT_SYMBOL_GPL(rapl_add_package); |
| 2272 |
|
| 2273 |
static void power_limit_state_save(void) |
| 2274 |
{ |
| 2275 |
struct rapl_package *rp; |
| 2276 |
struct rapl_domain *rd; |
| 2277 |
int ret, i; |
| 2278 |
|
| 2279 |
cpus_read_lock(); |
| 2280 |
list_for_each_entry(rp, &rapl_packages, plist) { |
| 2281 |
if (!rp->power_zone) |
| 2282 |
continue; |
| 2283 |
rd = power_zone_to_rapl_domain(rp->power_zone); |
| 2284 |
for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) { |
| 2285 |
ret = rapl_read_pl_data(rd, i, PL_LIMIT, true, |
| 2286 |
&rd->rpl[i].last_power_limit); |
| 2287 |
if (ret) |
| 2288 |
rd->rpl[i].last_power_limit = 0; |
| 2289 |
} |
| 2290 |
} |
| 2291 |
cpus_read_unlock(); |
| 2292 |
} |
| 2293 |
|
| 2294 |
static void power_limit_state_restore(void) |
| 2295 |
{ |
| 2296 |
struct rapl_package *rp; |
| 2297 |
struct rapl_domain *rd; |
| 2298 |
int i; |
| 2299 |
|
| 2300 |
cpus_read_lock(); |
| 2301 |
list_for_each_entry(rp, &rapl_packages, plist) { |
| 2302 |
if (!rp->power_zone) |
| 2303 |
continue; |
| 2304 |
rd = power_zone_to_rapl_domain(rp->power_zone); |
| 2305 |
for (i = POWER_LIMIT1; i < NR_POWER_LIMITS; i++) |
| 2306 |
if (rd->rpl[i].last_power_limit) |
| 2307 |
rapl_write_pl_data(rd, i, PL_LIMIT, |
| 2308 |
rd->rpl[i].last_power_limit); |
| 2309 |
} |
| 2310 |
cpus_read_unlock(); |
| 2311 |
} |
| 2312 |
|
| 2313 |
static int rapl_pm_callback(struct notifier_block *nb, |
| 2314 |
unsigned long mode, void *_unused) |
| 2315 |
{ |
| 2316 |
switch (mode) { |
| 2317 |
case PM_SUSPEND_PREPARE: |
| 2318 |
power_limit_state_save(); |
| 2319 |
break; |
| 2320 |
case PM_POST_SUSPEND: |
| 2321 |
power_limit_state_restore(); |
| 2322 |
break; |
| 2323 |
} |
| 2324 |
return NOTIFY_OK; |
| 2325 |
} |
| 2326 |
|
| 2327 |
static struct notifier_block rapl_pm_notifier = { |
| 2328 |
.notifier_call = rapl_pm_callback, |
| 2329 |
}; |
| 2330 |
|
| 2331 |
static struct platform_device *rapl_msr_platdev; |
| 2332 |
|
| 2333 |
static int __init rapl_init(void) |
| 2334 |
{ |
| 2335 |
const struct x86_cpu_id *id; |
| 2336 |
int ret; |
| 2337 |
|
| 2338 |
id = x86_match_cpu(rapl_ids); |
| 2339 |
if (id) { |
| 2340 |
defaults_msr = (struct rapl_defaults *)id->driver_data; |
| 2341 |
|
| 2342 |
rapl_msr_platdev = platform_device_alloc("intel_rapl_msr", 0); |
| 2343 |
if (!rapl_msr_platdev) |
| 2344 |
return -ENOMEM; |
| 2345 |
|
| 2346 |
ret = platform_device_add(rapl_msr_platdev); |
| 2347 |
if (ret) { |
| 2348 |
platform_device_put(rapl_msr_platdev); |
| 2349 |
return ret; |
| 2350 |
} |
| 2351 |
} |
| 2352 |
|
| 2353 |
ret = register_pm_notifier(&rapl_pm_notifier); |
| 2354 |
if (ret && rapl_msr_platdev) { |
| 2355 |
platform_device_del(rapl_msr_platdev); |
| 2356 |
platform_device_put(rapl_msr_platdev); |
| 2357 |
} |
| 2358 |
|
| 2359 |
return ret; |
| 2360 |
} |
| 2361 |
|
| 2362 |
static void __exit rapl_exit(void) |
| 2363 |
{ |
| 2364 |
platform_device_unregister(rapl_msr_platdev); |
| 2365 |
unregister_pm_notifier(&rapl_pm_notifier); |
| 2366 |
} |
| 2367 |
|
| 2368 |
fs_initcall(rapl_init); |
| 2369 |
module_exit(rapl_exit); |
| 2370 |
|
| 2371 |
MODULE_DESCRIPTION("Intel Runtime Average Power Limit (RAPL) common code"); |
| 2372 |
MODULE_AUTHOR("Jacob Pan <[email protected]>"); |
| 2373 |
MODULE_LICENSE("GPL v2"); |