1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers. 4 * 5 * (C) Copyright 2014, 2015 Linaro Ltd. 6 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org> 7 * 8 * CPPC describes a few methods for controlling CPU performance using 9 * information from a per CPU table called CPC. This table is described in 10 * the ACPI v5.0+ specification. The table consists of a list of 11 * registers which may be memory mapped or hardware registers and also may 12 * include some static integer values. 13 * 14 * CPU performance is on an abstract continuous scale as against a discretized 15 * P-state scale which is tied to CPU frequency only. In brief, the basic 16 * operation involves: 17 * 18 * - OS makes a CPU performance request. (Can provide min and max bounds) 19 * 20 * - Platform (such as BMC) is free to optimize request within requested bounds 21 * depending on power/thermal budgets etc. 22 * 23 * - Platform conveys its decision back to OS 24 * 25 * The communication between OS and platform occurs through another medium 26 * called (PCC) Platform Communication Channel. This is a generic mailbox like 27 * mechanism which includes doorbell semantics to indicate register updates. 28 * See drivers/mailbox/pcc.c for details on PCC. 29 * 30 * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and 31 * above specifications. 32 */ 33 34 #define pr_fmt(fmt) "ACPI CPPC: " fmt 35 36 #include <linux/delay.h> 37 #include <linux/interval_tree_generic.h> 38 #include <linux/iopoll.h> 39 #include <linux/ktime.h> 40 #include <linux/list.h> 41 #include <linux/mutex.h> 42 #include <linux/rbtree.h> 43 #include <linux/rwsem.h> 44 #include <linux/wait.h> 45 #include <linux/topology.h> 46 #include <linux/dmi.h> 47 #include <linux/units.h> 48 #include <linux/unaligned.h> 49 50 #include <acpi/cppc_acpi.h> 51 52 struct cppc_pcc_data { 53 struct pcc_mbox_chan *pcc_channel; 54 bool pcc_channel_acquired; 55 unsigned int deadline_us; 56 unsigned int pcc_mpar, pcc_mrtt, pcc_nominal; 57 58 bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */ 59 bool platform_owns_pcc; /* Ownership of PCC subspace */ 60 unsigned int pcc_write_cnt; /* Running count of PCC write commands */ 61 62 /* 63 * Lock to provide controlled access to the PCC channel. 64 * 65 * For performance critical usecases(currently cppc_set_perf) 66 * We need to take read_lock and check if channel belongs to OSPM 67 * before reading or writing to PCC subspace 68 * We need to take write_lock before transferring the channel 69 * ownership to the platform via a Doorbell 70 * This allows us to batch a number of CPPC requests if they happen 71 * to originate in about the same time 72 * 73 * For non-performance critical usecases(init) 74 * Take write_lock for all purposes which gives exclusive access 75 */ 76 struct rw_semaphore pcc_lock; 77 /* Serialize byte-oriented accesses to aliased PCC payload fields. */ 78 raw_spinlock_t payload_lock; 79 80 /* Wait queue for CPUs whose requests were batched */ 81 wait_queue_head_t pcc_write_wait_q; 82 ktime_t last_cmd_cmpl_time; 83 ktime_t last_mpar_reset; 84 int mpar_count; 85 int refcount; 86 }; 87 88 /* Array to represent the PCC channel per subspace ID */ 89 static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES]; 90 static DEFINE_MUTEX(pcc_data_lock); 91 /* The cpu_pcc_subspace_idx contains per CPU subspace ID */ 92 static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx); 93 94 /* 95 * The cpc_desc structure contains the ACPI register details 96 * as described in the per CPU _CPC tables. The details 97 * include the type of register (e.g. PCC, System IO, FFH etc.) 98 * and destination addresses which lets us READ/WRITE CPU performance 99 * information using the appropriate I/O methods. 100 */ 101 static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr); 102 103 /* Protect immutable capability queries against descriptor removal. */ 104 static DEFINE_MUTEX(cpc_desc_lock); 105 106 static void cpc_set_desc(unsigned int cpu, struct cpc_desc *desc) 107 { 108 guard(mutex)(&cpc_desc_lock); 109 per_cpu(cpc_desc_ptr, cpu) = desc; 110 } 111 112 struct cpc_sysmem_node { 113 struct rb_node rb; 114 u64 subtree_last; 115 u64 start; 116 u64 last; 117 struct cpc_desc *desc; 118 unsigned int reg_idx; 119 struct list_head aliases; 120 struct list_head alias_node; 121 struct cpc_sysmem_node *alias_of; 122 bool registered; 123 }; 124 125 struct cpc_non_mmio_node { 126 struct rb_node rb; 127 u64 subtree_last; 128 u64 start; 129 u64 last; 130 struct cpc_desc *desc; 131 unsigned int reg_idx; 132 u8 space_id; 133 u8 pcc_ss_id; 134 bool registered; 135 }; 136 137 #define CPC_SYSMEM_START(node) ((node)->start) 138 #define CPC_SYSMEM_LAST(node) ((node)->last) 139 140 INTERVAL_TREE_DEFINE(struct cpc_sysmem_node, rb, u64, subtree_last, 141 CPC_SYSMEM_START, CPC_SYSMEM_LAST, static inline, 142 cpc_sysmem_itree) 143 144 static struct rb_root_cached cpc_sysmem_tree = RB_ROOT_CACHED; 145 static DEFINE_MUTEX(cpc_sysmem_lock); 146 147 #define CPC_NON_MMIO_START(node) ((node)->start) 148 #define CPC_NON_MMIO_LAST(node) ((node)->last) 149 150 INTERVAL_TREE_DEFINE(struct cpc_non_mmio_node, rb, u64, subtree_last, 151 CPC_NON_MMIO_START, CPC_NON_MMIO_LAST, static inline, 152 cpc_non_mmio_itree) 153 154 static struct rb_root_cached cpc_pcc_trees[MAX_PCC_SUBSPACES]; 155 static struct rb_root_cached cpc_sysio_tree = RB_ROOT_CACHED; 156 static DEFINE_MUTEX(cpc_non_mmio_lock); 157 158 static struct cpc_sysmem_node *cpc_sysmem_first(u64 start, u64 last) 159 { 160 return cpc_sysmem_itree_iter_first(&cpc_sysmem_tree, start, last); 161 } 162 163 static struct cpc_sysmem_node *cpc_sysmem_next(struct cpc_sysmem_node *node, 164 u64 start, u64 last) 165 { 166 return cpc_sysmem_itree_iter_next(node, start, last); 167 } 168 169 #define CPC_PCC_HEADER_SIZE 0x8 170 171 /* pcc mapped address + header size + offset within PCC subspace */ 172 #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_channel->shmem + \ 173 CPC_PCC_HEADER_SIZE + (offs)) 174 175 /* Check if a CPC register is in PCC */ 176 #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \ 177 (cpc)->cpc_entry.reg.space_id == \ 178 ACPI_ADR_SPACE_PLATFORM_COMM) 179 180 /* Check if a CPC register is in FFH */ 181 #define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \ 182 (cpc)->cpc_entry.reg.space_id == \ 183 ACPI_ADR_SPACE_FIXED_HARDWARE) 184 185 /* Check if a CPC register is in SystemMemory */ 186 #define CPC_IN_SYSTEM_MEMORY(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \ 187 (cpc)->cpc_entry.reg.space_id == \ 188 ACPI_ADR_SPACE_SYSTEM_MEMORY) 189 190 /* Check if a CPC register is in SystemIo */ 191 #define CPC_IN_SYSTEM_IO(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \ 192 (cpc)->cpc_entry.reg.space_id == \ 193 ACPI_ADR_SPACE_SYSTEM_IO) 194 195 /* Evaluates to True if reg is a NULL register descriptor */ 196 #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \ 197 (reg)->address == 0 && \ 198 (reg)->bit_width == 0 && \ 199 (reg)->bit_offset == 0 && \ 200 (reg)->access_width == 0) 201 202 /* Evaluates to True if an optional cpc field is supported */ 203 #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \ 204 !!(cpc)->cpc_entry.int_value : \ 205 !IS_NULL_REG(&(cpc)->cpc_entry.reg)) 206 207 static bool cpc_is_writable(const struct cpc_register_resource *cpc) 208 { 209 return cpc->type == ACPI_TYPE_BUFFER && 210 !IS_NULL_REG(&cpc->cpc_entry.reg) && 211 !cpc->cpc_entry.write_unsupported; 212 } 213 214 static bool cpc_is_readable(const struct cpc_register_resource *cpc) 215 { 216 return cpc->type != ACPI_TYPE_BUFFER || 217 !cpc->cpc_entry.read_unsupported; 218 } 219 220 static bool cpc_entry_present(const struct cpc_register_resource *cpc) 221 { 222 if (cpc->type == ACPI_TYPE_INTEGER) 223 return true; 224 225 return cpc->type == ACPI_TYPE_BUFFER && 226 !IS_NULL_REG(&cpc->cpc_entry.reg); 227 } 228 229 /* 230 * Each bit indicates the optionality of the register in per-cpu 231 * cpc_regs[] with the corresponding index. 0 means mandatory and 1 232 * means optional. 233 */ 234 #define REG_OPTIONAL (0x7FC7D0) 235 236 /* 237 * Use the index of the register in per-cpu cpc_regs[] to check if 238 * it's an optional one. 239 */ 240 #define IS_OPTIONAL_CPC_REG(reg_idx) (REG_OPTIONAL & (1U << (reg_idx))) 241 242 static bool cpc_integer_entry_valid(unsigned int reg_idx, u64 value, 243 bool *legacy_null) 244 { 245 *legacy_null = false; 246 247 switch (reg_idx) { 248 case HIGHEST_PERF: 249 case NOMINAL_PERF: 250 case LOW_NON_LINEAR_PERF: 251 case LOWEST_PERF: 252 case REFERENCE_PERF: 253 case LOWEST_FREQ: 254 case NOMINAL_FREQ: 255 return value <= U32_MAX; 256 case CTR_WRAP_TIME: 257 /* AML Integers and the kernel interface are both 64-bit. */ 258 return true; 259 case AUTO_SEL_ENABLE: 260 return value <= 1; 261 case DESIRED_PERF: 262 /* Validated against Autonomous Selection after parsing. */ 263 *legacy_null = value == 0; 264 return *legacy_null; 265 default: 266 /* Tolerate legacy Integer 0 placeholders for absent options. */ 267 *legacy_null = value == 0 && IS_OPTIONAL_CPC_REG(reg_idx); 268 return *legacy_null; 269 } 270 } 271 272 /* 273 * Arbitrary Retries in case the remote processor is slow to respond 274 * to PCC commands. Keeping it high enough to cover emulators where 275 * the processors run painfully slow. 276 */ 277 #define NUM_RETRIES 500ULL 278 279 #define CPC_GENERIC_REGISTER_DESCRIPTOR 0x82 280 #define CPC_GENERIC_REGISTER_LENGTH (sizeof(struct cpc_reg) - 3) 281 282 #define define_one_cppc_ro(_name) \ 283 static struct kobj_attribute _name = \ 284 __ATTR(_name, 0444, show_##_name, NULL) 285 286 #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj) 287 288 #define show_cppc_data(access_fn, struct_name, member_name) \ 289 static ssize_t show_##member_name(struct kobject *kobj, \ 290 struct kobj_attribute *attr, char *buf) \ 291 { \ 292 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \ 293 struct struct_name st_name = {0}; \ 294 int ret; \ 295 \ 296 ret = access_fn(cpc_ptr->cpu_id, &st_name); \ 297 if (ret) \ 298 return ret; \ 299 \ 300 return sysfs_emit(buf, "%llu\n", \ 301 (u64)st_name.member_name); \ 302 } \ 303 define_one_cppc_ro(member_name) 304 305 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf); 306 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf); 307 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf); 308 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, reference_perf); 309 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf); 310 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, guaranteed_perf); 311 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq); 312 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq); 313 314 show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time); 315 316 /* 317 * PCC reuses the access_width field as the subspace id, so only decode access 318 * size for non-PCC registers. Otherwise, use the bit_width. 319 */ 320 #define GET_BIT_WIDTH(reg) (((reg)->access_width && \ 321 (reg)->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) ? \ 322 (8 << ((reg)->access_width - 1)) : (reg)->bit_width) 323 324 /* Shift and apply the mask for CPC reads/writes */ 325 #define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \ 326 GENMASK(((reg)->bit_width) - 1, 0)) 327 #define MASK_VAL_WRITE(reg, prev_val, val) \ 328 ((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \ 329 ((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \ 330 331 static unsigned int cpc_reg_access_width(const struct cpc_reg *reg) 332 { 333 if (reg->access_width > 4) 334 return 0; 335 336 if (reg->access_width) 337 return 8U << (reg->access_width - 1); 338 339 return reg->bit_width; 340 } 341 342 enum cpc_platform_quirk { 343 CPC_QUIRK_PERF_LIMITED_OWNS_UNIT = BIT(0), 344 }; 345 346 static const struct acpi_platform_list cpc_platform_quirk_list[] = { 347 { 348 .oem_id = "NVIDIA", 349 .oem_table_id = "T41", 350 .table = ACPI_SIG_DSDT, 351 .pred = all_versions, 352 .reason = "Performance Limited owns its access unit", 353 .data = CPC_QUIRK_PERF_LIMITED_OWNS_UNIT, 354 }, 355 { } 356 }; 357 358 static DEFINE_MUTEX(cpc_platform_quirk_lock); 359 static bool cpc_platform_quirks_initialized; 360 static u32 cpc_platform_quirks; 361 362 static int cpc_get_platform_quirks(u32 *quirks) 363 { 364 int idx, ret = 0; 365 366 mutex_lock(&cpc_platform_quirk_lock); 367 if (!cpc_platform_quirks_initialized) { 368 idx = acpi_match_platform_list(cpc_platform_quirk_list); 369 if (idx < 0 && idx != -ENODEV) { 370 ret = idx; 371 goto out; 372 } 373 if (idx >= 0) 374 cpc_platform_quirks = cpc_platform_quirk_list[idx].data; 375 cpc_platform_quirks_initialized = true; 376 } 377 *quirks = cpc_platform_quirks; 378 out: 379 mutex_unlock(&cpc_platform_quirk_lock); 380 381 return ret; 382 } 383 384 static void cpc_apply_platform_quirks(struct cpc_reg *reg, 385 unsigned int reg_idx, u32 quirks) 386 { 387 unsigned int access_width; 388 389 if (!(quirks & CPC_QUIRK_PERF_LIMITED_OWNS_UNIT) || 390 reg_idx != PERF_LIMITED || 391 reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY || 392 reg->bit_width != 2 || reg->bit_offset) 393 return; 394 395 access_width = cpc_reg_access_width(reg); 396 if (access_width != 32) 397 return; 398 399 reg->bit_width = access_width; 400 pr_info_once("firmware quirk: Performance Limited owns its access unit, using Bit Width %u\n", 401 access_width); 402 } 403 404 static u64 cpc_sysmem_access_size(const struct cpc_register_resource *reg) 405 { 406 unsigned int width = cpc_reg_access_width(®->cpc_entry.reg); 407 408 if (width != 8 && width != 16 && width != 32 && width != 64) 409 return 0; 410 411 return width / 8; 412 } 413 414 static u64 cpc_sysmem_field_size(const struct cpc_reg *gas) 415 { 416 return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8); 417 } 418 419 static u64 cpc_sysmem_claim_size(const struct cpc_register_resource *reg) 420 { 421 const struct cpc_reg *gas = ®->cpc_entry.reg; 422 u64 access_size = cpc_sysmem_access_size(reg); 423 424 if (!gas->bit_width) 425 return access_size; 426 427 return max(access_size, cpc_sysmem_field_size(gas)); 428 } 429 430 static bool cpc_reg_access_aligned(const struct cpc_reg *reg, u64 access_size) 431 { 432 /* x86 MMIO and port-I/O accessors support unaligned addresses. */ 433 return IS_ENABLED(CONFIG_X86) || IS_ALIGNED(reg->address, access_size); 434 } 435 436 static bool cpc_sysmem_access_units_overlap(const struct cpc_register_resource *a, 437 const struct cpc_register_resource *b) 438 { 439 const struct cpc_reg *a_gas = &a->cpc_entry.reg; 440 const struct cpc_reg *b_gas = &b->cpc_entry.reg; 441 u64 a_size = cpc_sysmem_claim_size(a); 442 u64 b_size = cpc_sysmem_claim_size(b); 443 444 /* Keep the conservative locking path for malformed access widths. */ 445 if (!a_size || !b_size) 446 return true; 447 448 if (a_gas->address < b_gas->address) 449 return b_gas->address - a_gas->address < a_size; 450 451 return a_gas->address - b_gas->address < b_size; 452 } 453 454 static bool cpc_reg_is_writable(unsigned int reg_idx) 455 { 456 /* Only controls written by this driver can be competing writers. */ 457 switch (reg_idx) { 458 case DESIRED_PERF: 459 case MIN_PERF: 460 case MAX_PERF: 461 case PERF_LIMITED: 462 case ENABLE: 463 case AUTO_SEL_ENABLE: 464 case AUTO_ACT_WINDOW: 465 case ENERGY_PERF: 466 return true; 467 default: 468 return false; 469 } 470 } 471 472 static bool cpc_reg_is_write_only(const struct cpc_desc *cpc_desc, 473 unsigned int reg_idx) 474 { 475 return cpc_desc->version >= CPPC_V4_REV && 476 (reg_idx == DESIRED_PERF || reg_idx == OSPM_NOMINAL_PERF); 477 } 478 479 static void cpc_disable_reg(struct cpc_desc *cpc_desc, unsigned int reg_idx) 480 { 481 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 482 483 reg->type = ACPI_TYPE_INTEGER; 484 reg->cpc_entry.int_value = 0; 485 } 486 487 static bool cpc_optional_writer_can_be_disabled(unsigned int reg_idx) 488 { 489 if (!IS_OPTIONAL_CPC_REG(reg_idx) || !cpc_reg_is_writable(reg_idx) || 490 reg_idx == MIN_PERF || reg_idx == MAX_PERF || reg_idx == ENABLE || 491 reg_idx == AUTO_SEL_ENABLE) 492 return false; 493 494 return true; 495 } 496 497 static bool cpc_sysmem_reg_needs_rmw(const struct cpc_register_resource *reg) 498 { 499 const struct cpc_reg *gas = ®->cpc_entry.reg; 500 u64 access_size = cpc_sysmem_access_size(reg); 501 502 return gas->bit_offset || gas->bit_width != access_size * 8; 503 } 504 505 static int cpc_validate_sysmem_reg(struct cpc_desc *cpc_desc, 506 const struct cpc_reg *gas, 507 unsigned int reg_idx) 508 { 509 unsigned int access_width = cpc_reg_access_width(gas); 510 u64 access_size; 511 512 if (access_width != 8 && access_width != 16 && 513 access_width != 32 && access_width != 64) 514 goto invalid; 515 516 if (!gas->bit_width || gas->bit_width > access_width || 517 gas->bit_offset >= access_width || 518 gas->bit_width > access_width - gas->bit_offset) 519 goto invalid; 520 521 access_size = access_width / 8; 522 if (!gas->address || gas->address > U64_MAX - (access_size - 1)) 523 goto invalid; 524 if (!cpc_reg_access_aligned(gas, access_size)) 525 goto invalid; 526 527 if (reg_idx == PERF_LIMITED) { 528 if (access_width == 64 && !IS_ENABLED(CONFIG_64BIT)) { 529 pr_warn_once("CPU%d: Performance Limited register cannot be accessed atomically; keeping its range reserved\n", 530 cpc_desc->cpu_id); 531 cpc_desc->cpc_regs[reg_idx].cpc_entry.read_unsupported = true; 532 cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true; 533 return 0; 534 } 535 536 if (gas->bit_offset || gas->bit_width != access_width) { 537 pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n", 538 cpc_desc->cpu_id); 539 cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true; 540 } 541 } 542 543 return 0; 544 545 invalid: 546 access_size = 0; 547 if (access_width == 8 || access_width == 16 || 548 access_width == 32 || access_width == 64) 549 access_size = access_width / 8; 550 if (gas->bit_width) 551 access_size = max(access_size, cpc_sysmem_field_size(gas)); 552 if ((cpc_reg_is_write_only(cpc_desc, reg_idx) || 553 reg_idx == PERF_LIMITED) && gas->address && access_size && 554 gas->address <= U64_MAX - (access_size - 1)) { 555 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 556 557 if (reg_idx == PERF_LIMITED) 558 pr_warn_once("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n", 559 cpc_desc->cpu_id, cpc_desc->version, reg_idx); 560 else 561 pr_warn("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n", 562 cpc_desc->cpu_id, cpc_desc->version, reg_idx); 563 reg->cpc_entry.read_unsupported = true; 564 reg->cpc_entry.write_unsupported = true; 565 return 0; 566 } 567 568 pr_debug("CPU:%d invalid SystemMemory GAS for _CPC register %u\n", 569 cpc_desc->cpu_id, reg_idx); 570 return -EINVAL; 571 } 572 573 static bool cpc_immutable_autonomous(const struct cpc_desc *cpc_desc) 574 { 575 const struct cpc_register_resource *reg; 576 577 reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 578 return osc_sb_cppc2_support_acked && reg->type == ACPI_TYPE_INTEGER && 579 reg->cpc_entry.int_value == 1; 580 } 581 582 static bool cpc_retain_pcc_status(struct cpc_desc *cpc_desc, 583 unsigned int reg_idx); 584 585 static int cpc_resolve_unsupported(struct cpc_desc *cpc_desc, 586 u32 unsupported) 587 { 588 unsigned int i; 589 u32 bounds = BIT(MIN_PERF) | BIT(MAX_PERF); 590 bool min_unusable, max_unusable; 591 592 if (unsupported & bounds) { 593 min_unusable = (unsupported & BIT(MIN_PERF)) || 594 !cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]); 595 max_unusable = (unsupported & BIT(MAX_PERF)) || 596 !cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]); 597 if (min_unusable && max_unusable) { 598 pr_warn("CPU%d: ignoring inaccessible Minimum and Maximum Performance registers\n", 599 cpc_desc->cpu_id); 600 cpc_disable_reg(cpc_desc, MIN_PERF); 601 cpc_disable_reg(cpc_desc, MAX_PERF); 602 unsupported &= ~bounds; 603 } 604 } 605 606 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 607 if (!(unsupported & BIT(i))) 608 continue; 609 610 /* CPPC control does not depend on Performance Limited status. */ 611 if (i == PERF_LIMITED) { 612 if (CPC_IN_PCC(&cpc_desc->cpc_regs[i]) && 613 cpc_retain_pcc_status(cpc_desc, i)) 614 continue; 615 616 pr_warn_once("CPU%d: ignoring inaccessible Performance Limited register\n", 617 cpc_desc->cpu_id); 618 cpc_disable_reg(cpc_desc, i); 619 continue; 620 } 621 622 if (i == DESIRED_PERF && cpc_immutable_autonomous(cpc_desc)) { 623 pr_warn("CPU%d: ignoring inaccessible Desired Performance register in autonomous mode\n", 624 cpc_desc->cpu_id); 625 cpc_disable_reg(cpc_desc, i); 626 continue; 627 } 628 629 /* 630 * A present Enable or Autonomous Selection control must remain 631 * usable. Disabling the latter could leave autonomous selection 632 * enabled while OSPM believes that it has disabled it. 633 */ 634 if (i == ENABLE || 635 (i == AUTO_SEL_ENABLE && cpc_entry_present(&cpc_desc->cpc_regs[i])) || 636 i == MIN_PERF || i == MAX_PERF || 637 !IS_OPTIONAL_CPC_REG(i)) { 638 pr_err("CPU%d: cannot access _CPC register %u\n", 639 cpc_desc->cpu_id, i); 640 return -EINVAL; 641 } 642 643 pr_warn("CPU%d: ignoring inaccessible optional _CPC register %u\n", 644 cpc_desc->cpu_id, i); 645 cpc_disable_reg(cpc_desc, i); 646 } 647 648 return 0; 649 } 650 651 static int cpc_validate_required_controls(struct cpc_desc *cpc_desc) 652 { 653 unsigned int i; 654 655 /* 656 * Performance Limited is required by the specification, but tolerate a 657 * NULL descriptor used by firmware which cannot report limiting events. 658 * CPPC control does not depend on this status. 659 */ 660 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 661 if (i != DESIRED_PERF && i != PERF_LIMITED && 662 !IS_OPTIONAL_CPC_REG(i) && 663 !cpc_entry_present(&cpc_desc->cpc_regs[i])) { 664 pr_debug("CPU:%d lacks mandatory _CPC register %u\n", 665 cpc_desc->cpu_id, i); 666 return -EINVAL; 667 } 668 } 669 670 /* Desired may be absent only for immutable autonomous operation. */ 671 if (!cpc_is_writable(&cpc_desc->cpc_regs[DESIRED_PERF]) && 672 !cpc_immutable_autonomous(cpc_desc)) { 673 pr_debug("CPU:%d lacks a writable Desired Performance register\n", 674 cpc_desc->cpu_id); 675 return -EINVAL; 676 } 677 678 return 0; 679 } 680 681 static int cpc_validate_bound_controls(struct cpc_desc *cpc_desc) 682 { 683 bool have_min, have_max; 684 685 have_min = cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]); 686 have_max = cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]); 687 if (have_min != have_max) { 688 pr_err("CPU%d: _CPC must provide both Minimum and Maximum Performance or neither\n", 689 cpc_desc->cpu_id); 690 return -EINVAL; 691 } 692 693 return 0; 694 } 695 696 static bool 697 cpc_retain_pcc_status(struct cpc_desc *cpc_desc, unsigned int reg_idx) 698 { 699 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 700 const struct cpc_reg *gas = ®->cpc_entry.reg; 701 u64 size; 702 703 if (reg_idx != PERF_LIMITED || !gas->bit_width) 704 return false; 705 706 size = DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8); 707 if (!size || gas->address > U64_MAX - (size - 1)) 708 return false; 709 710 reg->cpc_entry.read_unsupported = true; 711 reg->cpc_entry.write_unsupported = true; 712 pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its PCC range reserved\n", 713 cpc_desc->cpu_id); 714 return true; 715 } 716 717 static bool 718 cpc_retain_sysio_status(struct cpc_desc *cpc_desc, unsigned int reg_idx) 719 { 720 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 721 const struct cpc_reg *gas = ®->cpc_entry.reg; 722 723 if (reg_idx != PERF_LIMITED || !gas->bit_width) 724 return false; 725 726 /* Retain any in-range portion for overlap validation only. */ 727 if (gas->address > U16_MAX) 728 return false; 729 730 pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its SystemIO range reserved\n", 731 cpc_desc->cpu_id); 732 reg->cpc_entry.read_unsupported = true; 733 reg->cpc_entry.write_unsupported = true; 734 return true; 735 } 736 737 static u64 cpc_non_mmio_access_size(const struct cpc_register_resource *reg) 738 { 739 const struct cpc_reg *gas = ®->cpc_entry.reg; 740 741 if (gas->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) 742 return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8); 743 744 return max((u64)cpc_reg_access_width(gas) / 8, 745 DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8)); 746 } 747 748 static void cpc_validate_pcc_bounds(struct cpc_desc *cpc_desc, 749 int pcc_ss_id, struct cppc_pcc_data *data, 750 u32 *unsupported) 751 { 752 u64 shmem_size = data->pcc_channel->shmem_size; 753 unsigned int i; 754 755 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 756 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 757 struct cpc_reg *gas; 758 u64 access_size; 759 760 if ((*unsupported & BIT(i)) || !CPC_SUPPORTED(reg) || 761 !CPC_IN_PCC(reg)) 762 continue; 763 764 gas = ®->cpc_entry.reg; 765 if (gas->access_width != pcc_ss_id) 766 continue; 767 access_size = cpc_non_mmio_access_size(reg); 768 if (shmem_size >= CPC_PCC_HEADER_SIZE && 769 gas->address <= shmem_size - CPC_PCC_HEADER_SIZE && 770 access_size <= shmem_size - CPC_PCC_HEADER_SIZE - gas->address) 771 continue; 772 773 pr_debug("CPU%d: _CPC register %u exceeds the PCC shared region\n", 774 cpc_desc->cpu_id, i); 775 *unsupported |= BIT(i); 776 } 777 } 778 779 static bool cpc_pcc_access_needed(const struct cpc_desc *cpc_desc) 780 { 781 unsigned int i; 782 783 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 784 const struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 785 786 if (CPC_SUPPORTED(reg) && CPC_IN_PCC(reg) && 787 (cpc_is_readable(reg) || cpc_is_writable(reg))) 788 return true; 789 } 790 791 return false; 792 } 793 794 static bool cpc_non_mmio_overlap_conflicts(u8 space_id, bool a_writable, 795 bool b_writable, bool a_write_only, 796 bool b_write_only) 797 { 798 /* Only a write-only control can use a separate read-side port alias. */ 799 if (space_id == ACPI_ADR_SPACE_SYSTEM_IO && a_writable != b_writable) 800 return a_writable ? !a_write_only : !b_write_only; 801 802 return a_writable || b_writable; 803 } 804 805 static bool 806 cpc_sysio_perf_limited_conflicts(unsigned int a_idx, bool a_writable, 807 unsigned int b_idx, bool b_writable) 808 { 809 return (a_idx == PERF_LIMITED && b_writable) || 810 (b_idx == PERF_LIMITED && a_writable); 811 } 812 813 static struct rb_root_cached *cpc_non_mmio_tree(u8 space_id, u8 pcc_ss_id) 814 { 815 if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM) 816 return &cpc_pcc_trees[pcc_ss_id]; 817 if (space_id == ACPI_ADR_SPACE_SYSTEM_IO) 818 return &cpc_sysio_tree; 819 return NULL; 820 } 821 822 static bool cpc_same_non_mmio_register(const struct cpc_non_mmio_node *a, 823 const struct cpc_non_mmio_node *b) 824 { 825 const struct cpc_reg *a_gas = 826 &a->desc->cpc_regs[a->reg_idx].cpc_entry.reg; 827 const struct cpc_reg *b_gas = 828 &b->desc->cpc_regs[b->reg_idx].cpc_entry.reg; 829 830 return a->space_id == b->space_id && a->pcc_ss_id == b->pcc_ss_id && 831 a->reg_idx == b->reg_idx && a->start == b->start && 832 a->last == b->last && a_gas->bit_offset == b_gas->bit_offset && 833 a_gas->bit_width == b_gas->bit_width && 834 (a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM || 835 cpc_reg_access_width(a_gas) == cpc_reg_access_width(b_gas)); 836 } 837 838 static int cpc_validate_non_mmio_pair(const struct cpc_non_mmio_node *a, 839 const struct cpc_non_mmio_node *b) 840 { 841 const struct cpc_register_resource *a_reg; 842 const struct cpc_register_resource *b_reg; 843 bool a_writable, b_writable; 844 const char *name; 845 846 a_reg = &a->desc->cpc_regs[a->reg_idx]; 847 b_reg = &b->desc->cpc_regs[b->reg_idx]; 848 a_writable = cpc_reg_is_writable(a->reg_idx) && cpc_is_writable(a_reg); 849 b_writable = cpc_reg_is_writable(b->reg_idx) && cpc_is_writable(b_reg); 850 851 if (!cpc_non_mmio_overlap_conflicts(a->space_id, a_writable, 852 b_writable, 853 cpc_reg_is_write_only(a->desc, a->reg_idx), 854 cpc_reg_is_write_only(b->desc, b->reg_idx)) && 855 !(a->space_id == ACPI_ADR_SPACE_SYSTEM_IO && 856 cpc_sysio_perf_limited_conflicts(a->reg_idx, a_writable, 857 b->reg_idx, b_writable))) 858 return 0; 859 860 if (cpc_same_non_mmio_register(a, b)) 861 return 0; 862 863 name = a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM ? 864 "PCC" : "SystemIO"; 865 pr_err("CPU%d: %s _CPC register %u conflicts with CPU%d register %u\n", 866 a->desc->cpu_id, name, a->reg_idx, b->desc->cpu_id, 867 b->reg_idx); 868 return -EINVAL; 869 } 870 871 static void cpc_unregister_non_mmio_desc_locked(struct cpc_desc *cpc_desc) 872 { 873 unsigned int i; 874 875 if (!cpc_desc->non_mmio_nodes) 876 return; 877 878 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 879 struct cpc_non_mmio_node *node = &cpc_desc->non_mmio_nodes[i]; 880 struct rb_root_cached *tree; 881 882 if (!node->registered) 883 continue; 884 885 tree = cpc_non_mmio_tree(node->space_id, node->pcc_ss_id); 886 cpc_non_mmio_itree_remove(node, tree); 887 } 888 889 kfree(cpc_desc->non_mmio_nodes); 890 cpc_desc->non_mmio_nodes = NULL; 891 } 892 893 static int cpc_register_non_mmio_desc(struct cpc_desc *cpc_desc) 894 { 895 unsigned int nr_regs = cpc_desc->num_entries - 2; 896 unsigned int i; 897 int ret = 0; 898 bool found = false; 899 900 for (i = 0; i < nr_regs; i++) { 901 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 902 u8 space_id; 903 904 if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER) 905 continue; 906 space_id = reg->cpc_entry.reg.space_id; 907 if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM || 908 space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 909 found = true; 910 break; 911 } 912 } 913 if (!found) 914 return 0; 915 916 cpc_desc->non_mmio_nodes = kzalloc_objs(*cpc_desc->non_mmio_nodes, 917 nr_regs); 918 if (!cpc_desc->non_mmio_nodes) 919 return -ENOMEM; 920 921 mutex_lock(&cpc_non_mmio_lock); 922 923 for (i = 0; i < nr_regs; i++) { 924 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 925 struct cpc_non_mmio_node *match, *node; 926 struct rb_root_cached *tree; 927 u8 space_id; 928 u64 size; 929 930 if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER) 931 continue; 932 933 space_id = reg->cpc_entry.reg.space_id; 934 if (space_id != ACPI_ADR_SPACE_PLATFORM_COMM && 935 space_id != ACPI_ADR_SPACE_SYSTEM_IO) 936 continue; 937 938 node = &cpc_desc->non_mmio_nodes[i]; 939 size = cpc_non_mmio_access_size(reg); 940 node->start = reg->cpc_entry.reg.address; 941 node->last = node->start + size - 1; 942 node->desc = cpc_desc; 943 node->reg_idx = i; 944 node->space_id = space_id; 945 node->pcc_ss_id = space_id == ACPI_ADR_SPACE_PLATFORM_COMM ? 946 reg->cpc_entry.reg.access_width : 0; 947 tree = cpc_non_mmio_tree(space_id, node->pcc_ss_id); 948 949 match = cpc_non_mmio_itree_iter_first(tree, node->start, 950 node->last); 951 while (match) { 952 ret = cpc_validate_non_mmio_pair(node, match); 953 if (ret) 954 goto out_unregister; 955 956 match = cpc_non_mmio_itree_iter_next(match, node->start, 957 node->last); 958 } 959 cpc_non_mmio_itree_insert(node, tree); 960 node->registered = true; 961 } 962 963 mutex_unlock(&cpc_non_mmio_lock); 964 return 0; 965 966 out_unregister: 967 cpc_unregister_non_mmio_desc_locked(cpc_desc); 968 mutex_unlock(&cpc_non_mmio_lock); 969 return ret; 970 } 971 972 static void cpc_unregister_non_mmio_desc(struct cpc_desc *cpc_desc) 973 { 974 if (!cpc_desc->non_mmio_nodes) 975 return; 976 977 mutex_lock(&cpc_non_mmio_lock); 978 cpc_unregister_non_mmio_desc_locked(cpc_desc); 979 mutex_unlock(&cpc_non_mmio_lock); 980 } 981 982 static void cpc_mark_rmw_lock_users(struct cpc_desc *cpc_desc) 983 { 984 int i; 985 986 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 987 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 988 989 if (CPC_SUPPORTED(reg) && CPC_IN_SYSTEM_MEMORY(reg) && 990 cpc_is_writable(reg)) 991 reg->cpc_entry.use_rmw_lock = 992 cpc_sysmem_reg_needs_rmw(reg); 993 } 994 } 995 996 struct cpc_bit_position { 997 u64 byte; 998 u8 bit; 999 }; 1000 1001 static bool cpc_bit_position_before(const struct cpc_bit_position *a, 1002 const struct cpc_bit_position *b) 1003 { 1004 return a->byte < b->byte || (a->byte == b->byte && a->bit < b->bit); 1005 } 1006 1007 static bool cpc_sysmem_fields_overlap(const struct cpc_register_resource *a, 1008 const struct cpc_register_resource *b) 1009 { 1010 const struct cpc_reg *a_gas = &a->cpc_entry.reg; 1011 const struct cpc_reg *b_gas = &b->cpc_entry.reg; 1012 unsigned int a_last_bit = a_gas->bit_offset + a_gas->bit_width - 1; 1013 unsigned int b_last_bit = b_gas->bit_offset + b_gas->bit_width - 1; 1014 struct cpc_bit_position a_start = { 1015 .byte = a_gas->address + a_gas->bit_offset / 8, 1016 .bit = a_gas->bit_offset % 8, 1017 }; 1018 struct cpc_bit_position a_end = { 1019 .byte = a_gas->address + a_last_bit / 8, 1020 .bit = a_last_bit % 8, 1021 }; 1022 struct cpc_bit_position b_start = { 1023 .byte = b_gas->address + b_gas->bit_offset / 8, 1024 .bit = b_gas->bit_offset % 8, 1025 }; 1026 struct cpc_bit_position b_end = { 1027 .byte = b_gas->address + b_last_bit / 8, 1028 .bit = b_last_bit % 8, 1029 }; 1030 1031 return !cpc_bit_position_before(&a_end, &b_start) && 1032 !cpc_bit_position_before(&b_end, &a_start); 1033 } 1034 1035 static bool cpc_sysmem_access_overlaps_field(const struct cpc_register_resource *access, 1036 const struct cpc_register_resource *field) 1037 { 1038 const struct cpc_reg *access_gas = &access->cpc_entry.reg; 1039 const struct cpc_reg *field_gas = &field->cpc_entry.reg; 1040 u64 access_last; 1041 u64 field_start; 1042 u64 field_last; 1043 1044 if (!field_gas->bit_width) 1045 return cpc_sysmem_access_units_overlap(access, field); 1046 1047 access_last = access_gas->address + 1048 cpc_sysmem_access_size(access) - 1; 1049 field_start = field_gas->address + field_gas->bit_offset / 8; 1050 field_last = field_gas->address + 1051 (field_gas->bit_offset + field_gas->bit_width - 1) / 8; 1052 1053 return access_gas->address <= field_last && field_start <= access_last; 1054 } 1055 1056 static bool cpc_same_sysmem_register(unsigned int a_idx, 1057 const struct cpc_register_resource *a, 1058 unsigned int b_idx, 1059 const struct cpc_register_resource *b) 1060 { 1061 const struct cpc_reg *a_gas = &a->cpc_entry.reg; 1062 const struct cpc_reg *b_gas = &b->cpc_entry.reg; 1063 1064 return a_idx == b_idx && 1065 a_gas->address == b_gas->address && 1066 a_gas->bit_width == b_gas->bit_width && 1067 a_gas->bit_offset == b_gas->bit_offset && 1068 cpc_reg_access_width(a_gas) == cpc_reg_access_width(b_gas); 1069 } 1070 1071 static int cpc_validate_sysmem_pair(const struct cpc_desc *a_desc, 1072 unsigned int a_idx, 1073 const struct cpc_desc *b_desc, 1074 unsigned int b_idx) 1075 { 1076 const struct cpc_register_resource *a = &a_desc->cpc_regs[a_idx]; 1077 const struct cpc_register_resource *b = &b_desc->cpc_regs[b_idx]; 1078 bool a_write_only, b_write_only; 1079 bool a_writable, b_writable; 1080 bool fields_overlap; 1081 1082 /* The overlap helper includes each descriptor's conservative claim. */ 1083 if (!CPC_SUPPORTED(a) || !CPC_IN_SYSTEM_MEMORY(a) || 1084 !CPC_SUPPORTED(b) || !CPC_IN_SYSTEM_MEMORY(b) || 1085 !cpc_sysmem_access_units_overlap(a, b)) 1086 return 0; 1087 1088 a_write_only = cpc_reg_is_write_only(a_desc, a_idx); 1089 b_write_only = cpc_reg_is_write_only(b_desc, b_idx); 1090 fields_overlap = !a->cpc_entry.reg.bit_width || 1091 !b->cpc_entry.reg.bit_width || 1092 cpc_sysmem_fields_overlap(a, b); 1093 /* A readable field must not expose another field's undefined bits. */ 1094 if (a_write_only != b_write_only && 1095 cpc_is_readable(a_write_only ? b : a) && 1096 fields_overlap) 1097 goto conflict; 1098 1099 a_writable = cpc_reg_is_writable(a_idx) && cpc_is_writable(a); 1100 b_writable = cpc_reg_is_writable(b_idx) && cpc_is_writable(b); 1101 if (!a_writable && !b_writable) 1102 return 0; 1103 1104 if (cpc_same_sysmem_register(a_idx, a, b_idx, b)) { 1105 u64 access_size = cpc_sysmem_access_size(a); 1106 1107 /* 1108 * Exact partial aliases update the same field and retain 1109 * last-writer-wins semantics when the complete access is one native 1110 * transaction. A 64-bit MMIO write may be split on 32-bit kernels, 1111 * and an unaligned x86 access is not guaranteed to be one device 1112 * transaction. 1113 */ 1114 if (!a_writable || 1115 (IS_ALIGNED(a->cpc_entry.reg.address, access_size) && 1116 (access_size < sizeof(u64) || 1117 IS_ENABLED(CONFIG_64BIT)))) 1118 return 0; 1119 goto conflict; 1120 } 1121 1122 /* 1123 * The platform may set Performance Limited asynchronously. A write to 1124 * another field in the same access unit could write back stale status 1125 * bits, which an OSPM lock cannot prevent. 1126 */ 1127 if ((a_idx == PERF_LIMITED && b_writable) || 1128 (b_idx == PERF_LIMITED && a_writable)) 1129 goto conflict; 1130 1131 /* A full-width writer must not overwrite another logical field. */ 1132 if (fields_overlap && 1133 ((a_writable && b_writable) || 1134 (a_writable && !cpc_sysmem_reg_needs_rmw(a)) || 1135 (b_writable && !cpc_sysmem_reg_needs_rmw(b)))) 1136 goto conflict; 1137 1138 /* Different descriptors do not share their partial-write locks. */ 1139 if (a_desc != b_desc && a_writable && b_writable) 1140 goto conflict; 1141 1142 /* 1143 * RMW of either writer preserves the other field. If that field is 1144 * write-only, its readback is undefined and cannot safely be replayed. 1145 */ 1146 if ((a_write_only && b_writable && 1147 cpc_sysmem_reg_needs_rmw(b) && 1148 cpc_sysmem_access_overlaps_field(b, a)) || 1149 (b_write_only && a_writable && 1150 cpc_sysmem_reg_needs_rmw(a) && 1151 cpc_sysmem_access_overlaps_field(a, b))) 1152 goto conflict; 1153 1154 return 0; 1155 1156 conflict: 1157 pr_err("CPU%d: SystemMemory _CPC register %u conflicts with CPU%d register %u\n", 1158 a_desc->cpu_id, a_idx, b_desc->cpu_id, b_idx); 1159 return -EINVAL; 1160 } 1161 1162 static bool cpc_disable_new_sysmem_writer(struct cpc_desc *cpc_desc, 1163 unsigned int reg_idx, 1164 const struct cpc_sysmem_node *node) 1165 { 1166 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 1167 struct cpc_sysmem_node *match; 1168 unsigned int status_cpu = 0; 1169 bool found = false; 1170 1171 if (!cpc_optional_writer_can_be_disabled(reg_idx) || 1172 !cpc_is_writable(reg)) 1173 return false; 1174 1175 match = cpc_sysmem_first(node->start, node->last); 1176 while (match) { 1177 const struct cpc_register_resource *status; 1178 1179 if (match->reg_idx == PERF_LIMITED) { 1180 status = &match->desc->cpc_regs[PERF_LIMITED]; 1181 if (!status->cpc_entry.reg.bit_width || 1182 cpc_sysmem_fields_overlap(reg, status)) 1183 return false; 1184 status_cpu = match->desc->cpu_id; 1185 found = true; 1186 } 1187 1188 match = cpc_sysmem_next(match, node->start, node->last); 1189 } 1190 if (!found) 1191 return false; 1192 1193 pr_warn_once("CPU%d: ignoring optional _CPC register %u sharing CPU%d Performance Limited access unit\n", 1194 cpc_desc->cpu_id, reg_idx, status_cpu); 1195 cpc_disable_reg(cpc_desc, reg_idx); 1196 return true; 1197 } 1198 1199 static void cpc_unregister_sysmem_desc_locked(struct cpc_desc *cpc_desc) 1200 { 1201 unsigned int i; 1202 1203 if (!cpc_desc->sysmem_nodes) 1204 return; 1205 1206 for (i = 0; i < cpc_desc->num_entries - 2; i++) { 1207 struct cpc_sysmem_node *node = &cpc_desc->sysmem_nodes[i]; 1208 struct cpc_sysmem_node *alias, *child; 1209 1210 if (node->alias_of) { 1211 list_del(&node->alias_node); 1212 continue; 1213 } 1214 if (!node->registered) 1215 continue; 1216 1217 cpc_sysmem_itree_remove(node, &cpc_sysmem_tree); 1218 node->registered = false; 1219 if (list_empty(&node->aliases)) 1220 continue; 1221 1222 /* Keep one representative for aliases owned by live descriptors. */ 1223 alias = list_first_entry(&node->aliases, 1224 struct cpc_sysmem_node, alias_node); 1225 list_del_init(&alias->alias_node); 1226 alias->alias_of = NULL; 1227 alias->registered = true; 1228 list_splice_init(&node->aliases, &alias->aliases); 1229 list_for_each_entry(child, &alias->aliases, alias_node) 1230 child->alias_of = alias; 1231 cpc_sysmem_itree_insert(alias, &cpc_sysmem_tree); 1232 } 1233 1234 kfree(cpc_desc->sysmem_nodes); 1235 cpc_desc->sysmem_nodes = NULL; 1236 } 1237 1238 static int cpc_register_sysmem_desc(struct cpc_desc *cpc_desc) 1239 { 1240 unsigned int nr_regs = cpc_desc->num_entries - 2; 1241 unsigned int i; 1242 int ret = 0; 1243 bool found = false; 1244 1245 for (i = 0; i < nr_regs; i++) { 1246 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 1247 1248 if (CPC_SUPPORTED(reg) && CPC_IN_SYSTEM_MEMORY(reg)) { 1249 found = true; 1250 break; 1251 } 1252 } 1253 if (!found) 1254 return 0; 1255 1256 cpc_desc->sysmem_nodes = kzalloc_objs(*cpc_desc->sysmem_nodes, nr_regs); 1257 if (!cpc_desc->sysmem_nodes) 1258 return -ENOMEM; 1259 1260 mutex_lock(&cpc_sysmem_lock); 1261 1262 for (i = 0; i < nr_regs; i++) { 1263 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i]; 1264 struct cpc_sysmem_node *alias = NULL, *match, *node; 1265 u64 size; 1266 1267 if (!CPC_SUPPORTED(reg) || !CPC_IN_SYSTEM_MEMORY(reg)) 1268 continue; 1269 1270 node = &cpc_desc->sysmem_nodes[i]; 1271 size = cpc_sysmem_claim_size(reg); 1272 node->start = reg->cpc_entry.reg.address; 1273 node->last = node->start + size - 1; 1274 node->desc = cpc_desc; 1275 node->reg_idx = i; 1276 INIT_LIST_HEAD(&node->aliases); 1277 INIT_LIST_HEAD(&node->alias_node); 1278 1279 /* Performance Limited precedes every optional writer we may disable. */ 1280 if (cpc_disable_new_sysmem_writer(cpc_desc, i, node)) 1281 continue; 1282 1283 match = cpc_sysmem_first(node->start, node->last); 1284 while (match) { 1285 struct cpc_register_resource *match_reg; 1286 1287 match_reg = &match->desc->cpc_regs[match->reg_idx]; 1288 ret = cpc_validate_sysmem_pair(cpc_desc, i, match->desc, 1289 match->reg_idx); 1290 if (ret) 1291 goto out_unregister; 1292 if (cpc_desc == match->desc) { 1293 reg->cpc_entry.use_rmw_lock = true; 1294 match_reg->cpc_entry.use_rmw_lock = true; 1295 } 1296 if (cpc_same_sysmem_register(i, reg, match->reg_idx, match_reg)) 1297 alias = match; 1298 1299 match = cpc_sysmem_next(match, node->start, node->last); 1300 } 1301 if (alias) { 1302 node->alias_of = alias; 1303 list_add_tail(&node->alias_node, &alias->aliases); 1304 continue; 1305 } 1306 1307 cpc_sysmem_itree_insert(node, &cpc_sysmem_tree); 1308 node->registered = true; 1309 } 1310 1311 mutex_unlock(&cpc_sysmem_lock); 1312 return 0; 1313 1314 out_unregister: 1315 cpc_unregister_sysmem_desc_locked(cpc_desc); 1316 mutex_unlock(&cpc_sysmem_lock); 1317 return ret; 1318 } 1319 1320 static void cpc_unregister_sysmem_desc(struct cpc_desc *cpc_desc) 1321 { 1322 if (!cpc_desc->sysmem_nodes) 1323 return; 1324 1325 mutex_lock(&cpc_sysmem_lock); 1326 cpc_unregister_sysmem_desc_locked(cpc_desc); 1327 mutex_unlock(&cpc_sysmem_lock); 1328 } 1329 1330 static ssize_t show_feedback_ctrs(struct kobject *kobj, 1331 struct kobj_attribute *attr, char *buf) 1332 { 1333 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); 1334 struct cppc_perf_fb_ctrs fb_ctrs = {0}; 1335 int ret; 1336 1337 ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs); 1338 if (ret) 1339 return ret; 1340 1341 return sysfs_emit(buf, "ref:%llu del:%llu\n", 1342 fb_ctrs.reference, fb_ctrs.delivered); 1343 } 1344 define_one_cppc_ro(feedback_ctrs); 1345 1346 static struct attribute *cppc_attrs[] = { 1347 &feedback_ctrs.attr, 1348 &reference_perf.attr, 1349 &wraparound_time.attr, 1350 &highest_perf.attr, 1351 &lowest_perf.attr, 1352 &lowest_nonlinear_perf.attr, 1353 &guaranteed_perf.attr, 1354 &nominal_perf.attr, 1355 &nominal_freq.attr, 1356 &lowest_freq.attr, 1357 NULL 1358 }; 1359 ATTRIBUTE_GROUPS(cppc); 1360 1361 static void cppc_free_desc(struct cpc_desc *cpc_ptr) 1362 { 1363 unsigned int i; 1364 1365 cpc_unregister_non_mmio_desc(cpc_ptr); 1366 cpc_unregister_sysmem_desc(cpc_ptr); 1367 1368 for (i = 2; i < cpc_ptr->num_entries; i++) { 1369 void __iomem *addr = cpc_ptr->cpc_regs[i - 2].sys_mem_vaddr; 1370 1371 if (addr) 1372 iounmap(addr); 1373 } 1374 1375 kfree(cpc_ptr); 1376 } 1377 1378 static void cppc_kobj_release(struct kobject *kobj) 1379 { 1380 cppc_free_desc(to_cpc_desc(kobj)); 1381 } 1382 1383 static const struct kobj_type cppc_ktype = { 1384 .release = cppc_kobj_release, 1385 .sysfs_ops = &kobj_sysfs_ops, 1386 .default_groups = cppc_groups, 1387 }; 1388 1389 static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit) 1390 { 1391 int ret, status; 1392 struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id]; 1393 struct acpi_pcct_shared_memory __iomem *generic_comm_base = 1394 pcc_ss_data->pcc_channel->shmem; 1395 1396 if (!pcc_ss_data->platform_owns_pcc) 1397 return 0; 1398 1399 /* 1400 * Poll PCC status register every 3us(delay_us) for maximum of 1401 * deadline_us(timeout_us) until PCC command complete bit is set(cond) 1402 */ 1403 ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status, 1404 status & PCC_CMD_COMPLETE_MASK, 3, 1405 pcc_ss_data->deadline_us); 1406 1407 if (likely(!ret)) { 1408 /* Order completion status before reading the returned payload. */ 1409 rmb(); 1410 pcc_ss_data->platform_owns_pcc = false; 1411 if (chk_err_bit && (status & PCC_ERROR_MASK)) 1412 ret = -EIO; 1413 } 1414 1415 if (unlikely(ret)) 1416 pr_err("PCC check channel failed for ss: %d. ret=%d\n", 1417 pcc_ss_id, ret); 1418 1419 return ret; 1420 } 1421 1422 static void cppc_complete_pcc_write(int pcc_ss_id, 1423 struct cppc_pcc_data *pcc_ss_data, int ret) 1424 { 1425 int i; 1426 1427 if (unlikely(ret)) { 1428 for_each_possible_cpu(i) { 1429 struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i); 1430 1431 if (!desc || 1432 per_cpu(cpu_pcc_subspace_idx, i) != pcc_ss_id) 1433 continue; 1434 1435 if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt) 1436 desc->write_cmd_status = ret; 1437 } 1438 } 1439 1440 pcc_ss_data->pcc_write_cnt++; 1441 wake_up_all(&pcc_ss_data->pcc_write_wait_q); 1442 } 1443 1444 /* The caller must hold pcc_lock for write. */ 1445 static void cppc_abort_pending_pcc_write(int pcc_ss_id, 1446 struct cppc_pcc_data *pcc_ss_data, 1447 int ret) 1448 { 1449 if (!pcc_ss_data->pending_pcc_write_cmd) 1450 return; 1451 1452 pcc_ss_data->pending_pcc_write_cmd = false; 1453 cppc_complete_pcc_write(pcc_ss_id, pcc_ss_data, ret); 1454 } 1455 1456 /* 1457 * This function transfers the ownership of the PCC to the platform 1458 * So it must be called while holding write_lock(pcc_lock) 1459 */ 1460 static int send_pcc_cmd(int pcc_ss_id, u16 cmd) 1461 { 1462 int ret = -EIO; 1463 struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id]; 1464 struct acpi_pcct_shared_memory __iomem *generic_comm_base = 1465 pcc_ss_data->pcc_channel->shmem; 1466 unsigned int time_delta; 1467 1468 /* 1469 * For CMD_WRITE we know for a fact the caller should have checked 1470 * the channel before writing to PCC space 1471 */ 1472 if (cmd == CMD_READ) { 1473 /* 1474 * If there are pending cpc_writes, then we stole the channel 1475 * before write completion, so first send a WRITE command to 1476 * platform 1477 */ 1478 if (pcc_ss_data->pending_pcc_write_cmd) 1479 send_pcc_cmd(pcc_ss_id, CMD_WRITE); 1480 1481 ret = check_pcc_chan(pcc_ss_id, false); 1482 if (ret) 1483 goto end; 1484 } else /* CMD_WRITE */ 1485 pcc_ss_data->pending_pcc_write_cmd = FALSE; 1486 1487 /* 1488 * Handle the Minimum Request Turnaround Time(MRTT) 1489 * "The minimum amount of time that OSPM must wait after the completion 1490 * of a command before issuing the next command, in microseconds" 1491 */ 1492 if (pcc_ss_data->pcc_mrtt) { 1493 time_delta = ktime_us_delta(ktime_get(), 1494 pcc_ss_data->last_cmd_cmpl_time); 1495 if (pcc_ss_data->pcc_mrtt > time_delta) 1496 udelay(pcc_ss_data->pcc_mrtt - time_delta); 1497 } 1498 1499 /* 1500 * Handle the non-zero Maximum Periodic Access Rate(MPAR) 1501 * "The maximum number of periodic requests that the subspace channel can 1502 * support, reported in commands per minute. 0 indicates no limitation." 1503 * 1504 * This parameter should be ideally zero or large enough so that it can 1505 * handle maximum number of requests that all the cores in the system can 1506 * collectively generate. If it is not, we will follow the spec and just 1507 * not send the request to the platform after hitting the MPAR limit in 1508 * any 60s window 1509 */ 1510 if (pcc_ss_data->pcc_mpar) { 1511 if (pcc_ss_data->mpar_count == 0) { 1512 time_delta = ktime_ms_delta(ktime_get(), 1513 pcc_ss_data->last_mpar_reset); 1514 if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) { 1515 pr_debug("PCC cmd for subspace %d not sent due to MPAR limit", 1516 pcc_ss_id); 1517 ret = -EIO; 1518 goto end; 1519 } 1520 pcc_ss_data->last_mpar_reset = ktime_get(); 1521 pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar; 1522 } 1523 pcc_ss_data->mpar_count--; 1524 } 1525 1526 /* Write to the shared comm region. */ 1527 writew_relaxed(cmd, &generic_comm_base->command); 1528 1529 /* Flip CMD COMPLETE bit */ 1530 writew_relaxed(0, &generic_comm_base->status); 1531 1532 pcc_ss_data->platform_owns_pcc = true; 1533 1534 /* Ring doorbell */ 1535 ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd); 1536 if (ret < 0) { 1537 pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n", 1538 pcc_ss_id, cmd, ret); 1539 goto end; 1540 } 1541 1542 /* wait for completion and check for PCC error bit */ 1543 ret = check_pcc_chan(pcc_ss_id, true); 1544 1545 if (pcc_ss_data->pcc_mrtt) 1546 pcc_ss_data->last_cmd_cmpl_time = ktime_get(); 1547 1548 if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq) 1549 mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret); 1550 else 1551 mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret); 1552 1553 end: 1554 if (cmd == CMD_WRITE) 1555 cppc_complete_pcc_write(pcc_ss_id, pcc_ss_data, ret); 1556 1557 return ret; 1558 } 1559 1560 static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret) 1561 { 1562 if (ret < 0) 1563 pr_debug("TX did not complete: CMD sent:%x, ret:%d\n", 1564 *(u16 *)msg, ret); 1565 else 1566 pr_debug("TX completed. CMD sent:%x, ret:%d\n", 1567 *(u16 *)msg, ret); 1568 } 1569 1570 static struct mbox_client cppc_mbox_cl = { 1571 .tx_done = cppc_chan_tx_done, 1572 .knows_txdone = true, 1573 }; 1574 1575 static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle) 1576 { 1577 int result = -EFAULT; 1578 acpi_status status = AE_OK; 1579 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 1580 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"}; 1581 struct acpi_buffer state = {0, NULL}; 1582 union acpi_object *psd = NULL; 1583 struct acpi_psd_package *pdomain; 1584 1585 status = acpi_evaluate_object_typed(handle, "_PSD", NULL, 1586 &buffer, ACPI_TYPE_PACKAGE); 1587 if (status == AE_NOT_FOUND) /* _PSD is optional */ 1588 return 0; 1589 if (ACPI_FAILURE(status)) 1590 return -ENODEV; 1591 1592 psd = buffer.pointer; 1593 if (!psd || psd->package.count != 1) { 1594 pr_debug("Invalid _PSD data\n"); 1595 goto end; 1596 } 1597 1598 pdomain = &(cpc_ptr->domain_info); 1599 1600 state.length = sizeof(struct acpi_psd_package); 1601 state.pointer = pdomain; 1602 1603 status = acpi_extract_package(&(psd->package.elements[0]), 1604 &format, &state); 1605 if (ACPI_FAILURE(status)) { 1606 pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id); 1607 goto end; 1608 } 1609 1610 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) { 1611 pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id); 1612 goto end; 1613 } 1614 1615 if (pdomain->revision != ACPI_PSD_REV0_REVISION) { 1616 pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id); 1617 goto end; 1618 } 1619 1620 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL && 1621 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY && 1622 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) { 1623 pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id); 1624 goto end; 1625 } 1626 1627 result = 0; 1628 end: 1629 kfree(buffer.pointer); 1630 return result; 1631 } 1632 1633 bool acpi_cpc_valid(void) 1634 { 1635 struct cpc_desc *cpc_ptr; 1636 int cpu; 1637 1638 if (acpi_disabled) 1639 return false; 1640 1641 for_each_online_cpu(cpu) { 1642 cpc_ptr = per_cpu(cpc_desc_ptr, cpu); 1643 if (!cpc_ptr) 1644 return false; 1645 } 1646 1647 return true; 1648 } 1649 EXPORT_SYMBOL_GPL(acpi_cpc_valid); 1650 1651 bool cppc_allow_fast_switch(const struct cpumask *cpus) 1652 { 1653 struct cpc_register_resource *desired_reg, *min_reg, *max_reg; 1654 struct cpc_desc *cpc_ptr; 1655 int cpu; 1656 1657 for_each_cpu(cpu, cpus) { 1658 cpc_ptr = per_cpu(cpc_desc_ptr, cpu); 1659 if (!cpc_ptr) 1660 return false; 1661 desired_reg = &cpc_ptr->cpc_regs[DESIRED_PERF]; 1662 min_reg = &cpc_ptr->cpc_regs[MIN_PERF]; 1663 max_reg = &cpc_ptr->cpc_regs[MAX_PERF]; 1664 1665 if (!cpc_is_writable(desired_reg) || 1666 (!CPC_IN_SYSTEM_MEMORY(desired_reg) && 1667 !CPC_IN_SYSTEM_IO(desired_reg)) || 1668 (CPC_SUPPORTED(min_reg) && 1669 !CPC_IN_SYSTEM_MEMORY(min_reg) && 1670 !CPC_IN_SYSTEM_IO(min_reg)) || 1671 (CPC_SUPPORTED(max_reg) && 1672 !CPC_IN_SYSTEM_MEMORY(max_reg) && 1673 !CPC_IN_SYSTEM_IO(max_reg))) 1674 return false; 1675 } 1676 1677 return true; 1678 } 1679 EXPORT_SYMBOL_GPL(cppc_allow_fast_switch); 1680 1681 /** 1682 * acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu 1683 * @cpu: Find all CPUs that share a domain with cpu. 1684 * @cpu_data: Pointer to CPU specific CPPC data including PSD info. 1685 * 1686 * Return: 0 for success or negative value for err. 1687 */ 1688 int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data) 1689 { 1690 struct cpc_desc *cpc_ptr, *match_cpc_ptr; 1691 struct acpi_psd_package *match_pdomain; 1692 struct acpi_psd_package *pdomain; 1693 int count_target, i; 1694 1695 /* 1696 * Now that we have _PSD data from all CPUs, let's setup P-state 1697 * domain info. 1698 */ 1699 cpc_ptr = per_cpu(cpc_desc_ptr, cpu); 1700 if (!cpc_ptr) 1701 return -EFAULT; 1702 1703 pdomain = &(cpc_ptr->domain_info); 1704 cpumask_set_cpu(cpu, cpu_data->shared_cpu_map); 1705 if (pdomain->num_processors <= 1) 1706 return 0; 1707 1708 /* Validate the Domain info */ 1709 count_target = pdomain->num_processors; 1710 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL) 1711 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL; 1712 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL) 1713 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW; 1714 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY) 1715 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY; 1716 1717 for_each_possible_cpu(i) { 1718 if (i == cpu) 1719 continue; 1720 1721 match_cpc_ptr = per_cpu(cpc_desc_ptr, i); 1722 if (!match_cpc_ptr) 1723 continue; 1724 1725 match_pdomain = &(match_cpc_ptr->domain_info); 1726 if (match_pdomain->domain != pdomain->domain) 1727 continue; 1728 1729 /* Here i and cpu are in the same domain */ 1730 if (match_pdomain->num_processors != count_target) 1731 goto err_fault; 1732 1733 if (pdomain->coord_type != match_pdomain->coord_type) 1734 goto err_fault; 1735 1736 cpumask_set_cpu(i, cpu_data->shared_cpu_map); 1737 } 1738 1739 return 0; 1740 1741 err_fault: 1742 /* Assume no coordination on any error parsing domain info */ 1743 cpumask_clear(cpu_data->shared_cpu_map); 1744 cpumask_set_cpu(cpu, cpu_data->shared_cpu_map); 1745 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE; 1746 1747 return -EFAULT; 1748 } 1749 EXPORT_SYMBOL_GPL(acpi_get_psd_map); 1750 1751 static int register_pcc_channel(int pcc_ss_idx) 1752 { 1753 struct cppc_pcc_data *data; 1754 struct pcc_mbox_chan *pcc_chan; 1755 u64 usecs_lat; 1756 int ret = 0; 1757 1758 if (pcc_ss_idx < 0 || pcc_ss_idx >= MAX_PCC_SUBSPACES) 1759 return -EINVAL; 1760 1761 mutex_lock(&pcc_data_lock); 1762 data = pcc_data[pcc_ss_idx]; 1763 if (!data) { 1764 ret = -ENODEV; 1765 goto out_unlock; 1766 } 1767 if (data->pcc_channel_acquired) 1768 goto out_unlock; 1769 1770 pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx); 1771 if (IS_ERR(pcc_chan)) { 1772 ret = -ENODEV; 1773 goto out_unlock; 1774 } 1775 1776 data->pcc_channel = pcc_chan; 1777 /* 1778 * cppc_ss->latency is just a Nominal value. In reality 1779 * the remote processor could be much slower to reply. 1780 * So add an arbitrary amount of wait on top of Nominal. 1781 */ 1782 usecs_lat = NUM_RETRIES * pcc_chan->latency; 1783 data->deadline_us = usecs_lat; 1784 data->pcc_mrtt = pcc_chan->min_turnaround_time; 1785 data->pcc_mpar = pcc_chan->max_access_rate; 1786 data->pcc_nominal = pcc_chan->latency; 1787 init_rwsem(&data->pcc_lock); 1788 init_waitqueue_head(&data->pcc_write_wait_q); 1789 1790 /* Reuse this channel when another CPU references the same subspace. */ 1791 data->pcc_channel_acquired = true; 1792 1793 out_unlock: 1794 mutex_unlock(&pcc_data_lock); 1795 return ret; 1796 } 1797 1798 /** 1799 * cpc_ffh_supported() - check if FFH reading supported 1800 * 1801 * Check if the architecture has support for functional fixed hardware 1802 * read/write capability. 1803 * 1804 * Return: true for supported, false for not supported 1805 */ 1806 bool __weak cpc_ffh_supported(void) 1807 { 1808 return false; 1809 } 1810 1811 /** 1812 * cpc_supported_by_cpu() - check if CPPC is supported by CPU 1813 * 1814 * Check if the architectural support for CPPC is present even 1815 * if the _OSC hasn't prescribed it 1816 * 1817 * Return: true for supported, false for not supported 1818 */ 1819 bool __weak cpc_supported_by_cpu(void) 1820 { 1821 return false; 1822 } 1823 1824 /** 1825 * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace 1826 * @pcc_ss_id: PCC Subspace index as in the PCC client ACPI package. 1827 * 1828 * Check and allocate the cppc_pcc_data memory. 1829 * In some processor configurations it is possible that same subspace 1830 * is shared between multiple CPUs. This is seen especially in CPUs 1831 * with hardware multi-threading support. 1832 * 1833 * Return: 0 for success, errno for failure 1834 */ 1835 static int pcc_data_alloc(int pcc_ss_id) 1836 { 1837 struct cppc_pcc_data *data; 1838 int ret = 0; 1839 1840 if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES) 1841 return -EINVAL; 1842 1843 mutex_lock(&pcc_data_lock); 1844 data = pcc_data[pcc_ss_id]; 1845 if (!data) { 1846 data = kzalloc_obj(struct cppc_pcc_data); 1847 if (!data) { 1848 ret = -ENOMEM; 1849 goto out_unlock; 1850 } 1851 raw_spin_lock_init(&data->payload_lock); 1852 pcc_data[pcc_ss_id] = data; 1853 } 1854 data->refcount++; 1855 1856 out_unlock: 1857 mutex_unlock(&pcc_data_lock); 1858 return ret; 1859 } 1860 1861 static void pcc_data_put(int pcc_ss_id) 1862 { 1863 struct cppc_pcc_data *data; 1864 1865 if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES) 1866 return; 1867 1868 mutex_lock(&pcc_data_lock); 1869 data = pcc_data[pcc_ss_id]; 1870 if (!data || --data->refcount) 1871 goto out_unlock; 1872 1873 pcc_data[pcc_ss_id] = NULL; 1874 if (data->pcc_channel_acquired) 1875 pcc_mbox_free_channel(data->pcc_channel); 1876 1877 kfree(data); 1878 1879 out_unlock: 1880 mutex_unlock(&pcc_data_lock); 1881 } 1882 1883 /* 1884 * An example CPC table looks like the following. 1885 * 1886 * Name (_CPC, Package() { 1887 * 17, // NumEntries 1888 * 1, // Revision 1889 * ResourceTemplate() {Register(PCC, 32, 0, 0x120, 2)}, // Highest Performance 1890 * ResourceTemplate() {Register(PCC, 32, 0, 0x124, 2)}, // Nominal Performance 1891 * ResourceTemplate() {Register(PCC, 32, 0, 0x128, 2)}, // Lowest Nonlinear Performance 1892 * ResourceTemplate() {Register(PCC, 32, 0, 0x12C, 2)}, // Lowest Performance 1893 * ResourceTemplate() {Register(PCC, 32, 0, 0x130, 2)}, // Guaranteed Performance Register 1894 * ResourceTemplate() {Register(PCC, 32, 0, 0x110, 2)}, // Desired Performance Register 1895 * ResourceTemplate() {Register(SystemMemory, 0, 0, 0, 0)}, 1896 * ... 1897 * ... 1898 * ... 1899 * } 1900 * Each Register() encodes how to access that specific register. 1901 * e.g. a sample PCC entry has the following encoding: 1902 * 1903 * Register ( 1904 * PCC, // AddressSpaceKeyword 1905 * 8, // RegisterBitWidth 1906 * 8, // RegisterBitOffset 1907 * 0x30, // RegisterAddress 1908 * 9, // AccessSize (subspace ID) 1909 * ) 1910 */ 1911 1912 /** 1913 * acpi_cppc_processor_probe - Search for per CPU _CPC objects. 1914 * @pr: Ptr to acpi_processor containing this CPU's logical ID. 1915 * 1916 * Return: 0 for success or negative value for err. 1917 */ 1918 int acpi_cppc_processor_probe(struct acpi_processor *pr) 1919 { 1920 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 1921 union acpi_object *out_obj, *cpc_obj; 1922 struct cpc_desc *cpc_ptr; 1923 struct cpc_reg *gas_t; 1924 struct device *cpu_dev; 1925 acpi_handle handle = pr->handle; 1926 unsigned int num_ent, i, cpc_rev; 1927 u32 unsupported_regs = 0; 1928 u32 platform_quirks; 1929 int pcc_subspace_id = -1; 1930 bool pcc_data_ref = false; 1931 bool cpc_present = false; 1932 acpi_status status; 1933 int ret = -EINVAL; 1934 int err; 1935 1936 if (per_cpu(cpc_desc_ptr, pr->id)) 1937 return 0; 1938 ret = cpc_get_platform_quirks(&platform_quirks); 1939 if (ret) { 1940 pr_err("CPU%d: failed to match CPPC platform quirks: %d\n", 1941 pr->id, ret); 1942 return ret; 1943 } 1944 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 1945 1946 if (!osc_sb_cppc2_support_acked) { 1947 pr_debug("CPPC v2 _OSC not acked\n"); 1948 if (!cpc_supported_by_cpu()) { 1949 pr_debug("CPPC is not supported by the CPU\n"); 1950 return -ENODEV; 1951 } 1952 } 1953 1954 /* Parse the ACPI _CPC table for this CPU. */ 1955 status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output, 1956 ACPI_TYPE_PACKAGE); 1957 if (ACPI_FAILURE(status)) { 1958 ret = -ENODEV; 1959 goto out_buf_free; 1960 } 1961 cpc_present = true; 1962 ret = -EINVAL; 1963 1964 out_obj = (union acpi_object *) output.pointer; 1965 if (out_obj->package.count < 2) { 1966 pr_debug("Unexpected _CPC package count (%u) for CPU:%d\n", 1967 out_obj->package.count, pr->id); 1968 goto out_buf_free; 1969 } 1970 1971 cpc_ptr = kzalloc_obj(struct cpc_desc); 1972 if (!cpc_ptr) { 1973 ret = -ENOMEM; 1974 goto out_buf_free; 1975 } 1976 cpc_ptr->cpu_id = pr->id; 1977 1978 /* First entry is NumEntries. */ 1979 cpc_obj = &out_obj->package.elements[0]; 1980 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 1981 if (cpc_obj->integer.value < 2 || 1982 cpc_obj->integer.value > out_obj->package.count) { 1983 pr_debug("Invalid _CPC NumEntries (%llu) for package count (%u) on CPU:%d\n", 1984 cpc_obj->integer.value, out_obj->package.count, 1985 pr->id); 1986 goto out_free; 1987 } 1988 1989 num_ent = cpc_obj->integer.value; 1990 } else { 1991 pr_debug("Unexpected _CPC NumEntries entry type (%d) for CPU:%d\n", 1992 cpc_obj->type, pr->id); 1993 goto out_free; 1994 } 1995 1996 /* Second entry should be revision. */ 1997 cpc_obj = &out_obj->package.elements[1]; 1998 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 1999 if (cpc_obj->integer.value > U8_MAX) { 2000 pr_debug("Invalid _CPC Revision (%llu) for CPU:%d\n", 2001 cpc_obj->integer.value, pr->id); 2002 ret = -EINVAL; 2003 goto out_free; 2004 } 2005 cpc_rev = cpc_obj->integer.value; 2006 } else { 2007 pr_debug("Unexpected _CPC Revision entry type (%d) for CPU:%d\n", 2008 cpc_obj->type, pr->id); 2009 goto out_free; 2010 } 2011 2012 if (cpc_rev < CPPC_V2_REV) { 2013 pr_debug("Unsupported _CPC Revision (%d) for CPU:%d\n", cpc_rev, 2014 pr->id); 2015 goto out_free; 2016 } 2017 2018 /* 2019 * Disregard _CPC if the number of entries in the return package is not 2020 * as expected, but support future revisions being proper supersets of 2021 * the v4 and only causing more entries to be returned by _CPC. 2022 */ 2023 if ((cpc_rev == CPPC_V2_REV && num_ent != CPPC_V2_NUM_ENT) || 2024 (cpc_rev == CPPC_V3_REV && num_ent != CPPC_V3_NUM_ENT) || 2025 (cpc_rev == CPPC_V4_REV && num_ent != CPPC_V4_NUM_ENT) || 2026 (cpc_rev > CPPC_V4_REV && num_ent <= CPPC_V4_NUM_ENT)) { 2027 pr_debug("Unexpected number of _CPC return package entries (%d) for CPU:%d\n", 2028 num_ent, pr->id); 2029 goto out_free; 2030 } 2031 if (cpc_rev > CPPC_V4_REV) { 2032 num_ent = CPPC_V4_NUM_ENT; 2033 cpc_rev = CPPC_V4_REV; 2034 } 2035 2036 cpc_ptr->num_entries = num_ent; 2037 cpc_ptr->version = cpc_rev; 2038 2039 /* Iterate through remaining entries in _CPC */ 2040 for (i = 2; i < num_ent; i++) { 2041 cpc_obj = &out_obj->package.elements[i]; 2042 2043 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 2044 bool legacy_null; 2045 2046 if (!cpc_integer_entry_valid(i - 2, 2047 cpc_obj->integer.value, 2048 &legacy_null)) { 2049 pr_debug("Invalid Integer _CPC register %u for CPU:%d\n", 2050 i - 2, pr->id); 2051 ret = -EINVAL; 2052 goto out_free; 2053 } 2054 if (legacy_null) 2055 pr_warn_once(FW_BUG "_CPC register %u uses Integer 0 for an absent Buffer\n", 2056 i - 2); 2057 cpc_ptr->cpc_regs[i - 2].type = ACPI_TYPE_INTEGER; 2058 cpc_ptr->cpc_regs[i - 2].cpc_entry.int_value = cpc_obj->integer.value; 2059 } else if (cpc_obj->type == ACPI_TYPE_BUFFER) { 2060 if (cpc_obj->buffer.length < sizeof(*gas_t)) { 2061 pr_debug("Invalid register descriptor for CPU:%d\n", 2062 pr->id); 2063 ret = -EINVAL; 2064 goto out_free; 2065 } 2066 2067 gas_t = (struct cpc_reg *) 2068 cpc_obj->buffer.pointer; 2069 if (gas_t->descriptor != CPC_GENERIC_REGISTER_DESCRIPTOR || 2070 gas_t->length != CPC_GENERIC_REGISTER_LENGTH) { 2071 pr_debug("Invalid register resource for CPU:%d\n", 2072 pr->id); 2073 ret = -EINVAL; 2074 goto out_free; 2075 } 2076 2077 cpc_ptr->cpc_regs[i - 2].type = ACPI_TYPE_BUFFER; 2078 memcpy(&cpc_ptr->cpc_regs[i - 2].cpc_entry.reg, gas_t, 2079 sizeof(*gas_t)); 2080 gas_t = &cpc_ptr->cpc_regs[i - 2].cpc_entry.reg; 2081 cpc_apply_platform_quirks(gas_t, i - 2, 2082 platform_quirks); 2083 2084 /* 2085 * The PCC Subspace index is encoded inside 2086 * the CPC table entries. The same PCC index 2087 * will be used for all the PCC entries, 2088 * so extract it only once. 2089 */ 2090 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2091 /* These registers have no specified 32-bit upper bound. */ 2092 bool wide_write = i - 2 == PERF_LIMITED || 2093 i - 2 == ENABLE || 2094 i - 2 == AUTO_SEL_ENABLE; 2095 bool write_width_supported = gas_t->bit_width == 8 || 2096 gas_t->bit_width == 16 || 2097 gas_t->bit_width == 32 || 2098 gas_t->bit_width == 64; 2099 bool unsupported; 2100 2101 unsupported = !gas_t->bit_width || 2102 gas_t->bit_width > 64 || 2103 gas_t->bit_offset || 2104 gas_t->bit_width % 8 || 2105 (cpc_reg_is_writable(i - 2) && 2106 (!write_width_supported || 2107 (!wide_write && gas_t->bit_width > 32))); 2108 if (unsupported) { 2109 if (!cpc_retain_pcc_status(cpc_ptr, i - 2)) 2110 unsupported_regs |= BIT(i - 2); 2111 continue; 2112 } 2113 2114 if (pcc_subspace_id < 0) { 2115 pcc_subspace_id = gas_t->access_width; 2116 } else if (pcc_subspace_id != gas_t->access_width) { 2117 pr_debug("Mismatched PCC ids in _CPC for CPU:%d\n", 2118 pr->id); 2119 ret = -EINVAL; 2120 goto out_free; 2121 } 2122 2123 if (!pcc_data_ref) { 2124 err = pcc_data_alloc(pcc_subspace_id); 2125 if (err) { 2126 ret = err; 2127 goto out_free; 2128 } 2129 pcc_data_ref = true; 2130 } 2131 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) { 2132 if (!IS_NULL_REG(gas_t)) { 2133 void __iomem *addr; 2134 size_t access_width; 2135 2136 err = cpc_validate_sysmem_reg(cpc_ptr, gas_t, 2137 i - 2); 2138 if (err) { 2139 unsupported_regs |= BIT(i - 2); 2140 continue; 2141 } 2142 if (!cpc_is_readable(&cpc_ptr->cpc_regs[i - 2]) && 2143 !cpc_is_writable(&cpc_ptr->cpc_regs[i - 2])) 2144 continue; 2145 2146 if (!osc_cpc_flexible_adr_space_confirmed) { 2147 pr_debug("Flexible address space capability not supported\n"); 2148 ret = -EOPNOTSUPP; 2149 if (!cpc_supported_by_cpu()) 2150 goto out_free; 2151 ret = -EINVAL; 2152 } 2153 2154 access_width = cpc_reg_access_width(gas_t); 2155 access_width /= 8; 2156 addr = ioremap(gas_t->address, access_width); 2157 if (!addr) { 2158 ret = -ENOMEM; 2159 goto out_free; 2160 } 2161 cpc_ptr->cpc_regs[i - 2].sys_mem_vaddr = addr; 2162 } 2163 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2164 u64 access_size; 2165 const char *reason = "uses unsupported SystemIO geometry"; 2166 unsigned int access_width; 2167 bool partial = false; 2168 bool unsupported; 2169 2170 access_width = cpc_reg_access_width(gas_t); 2171 unsupported = !IS_ENABLED(CONFIG_HAS_IOPORT); 2172 if (unsupported) 2173 reason = "requires unavailable SystemIO support"; 2174 else 2175 unsupported = access_width != 8 && 2176 access_width != 16 && 2177 access_width != 32; 2178 if (!unsupported) { 2179 access_size = access_width / 8; 2180 unsupported = !gas_t->bit_width || 2181 gas_t->bit_width > access_width || 2182 gas_t->bit_offset >= access_width || 2183 gas_t->bit_width > access_width - 2184 gas_t->bit_offset; 2185 partial = gas_t->bit_offset || 2186 gas_t->bit_width != access_width; 2187 } 2188 if (!unsupported) { 2189 unsupported = (cpc_reg_is_writable(i - 2) && 2190 i - 2 != PERF_LIMITED && 2191 cpc_is_writable(&cpc_ptr->cpc_regs[i - 2]) && 2192 partial) || 2193 !cpc_reg_access_aligned(gas_t, 2194 access_size) || 2195 gas_t->address > 2196 U16_MAX - (access_size - 1); 2197 } 2198 if (unsupported) { 2199 if (cpc_retain_sysio_status(cpc_ptr, i - 2)) 2200 continue; 2201 pr_debug("CPU%d: _CPC register %u %s\n", 2202 pr->id, i - 2, reason); 2203 unsupported_regs |= BIT(i - 2); 2204 continue; 2205 } 2206 if (i - 2 == PERF_LIMITED && partial) { 2207 pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n", 2208 cpc_ptr->cpu_id); 2209 cpc_ptr->cpc_regs[i - 2].cpc_entry.write_unsupported = true; 2210 } 2211 if (!osc_cpc_flexible_adr_space_confirmed) { 2212 pr_debug("Flexible address space capability not supported\n"); 2213 ret = -EOPNOTSUPP; 2214 if (!cpc_supported_by_cpu()) 2215 goto out_free; 2216 ret = -EINVAL; 2217 } 2218 } else { 2219 if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) { 2220 /* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */ 2221 pr_debug("Unsupported register type (%d) in _CPC\n", 2222 gas_t->space_id); 2223 ret = -EOPNOTSUPP; 2224 goto out_free; 2225 } 2226 } 2227 } else if (cpc_obj->type == ACPI_TYPE_PACKAGE && (i - 2) == RESOURCE_PRIORITY) { 2228 /* 2229 * ACPI 6.6, s8.4.6.1.2.7 defines Resource Priority as a 2230 * Package of Resource Priority Register Descriptor sub-packages. 2231 * Parsing the full structure is not yet supported. 2232 * Mark the register as unsupported for now. 2233 */ 2234 pr_debug("CPU:%d Resource Priority not supported\n", pr->id); 2235 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER; 2236 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = 0; 2237 } else { 2238 pr_debug("Invalid entry type (%d) in _CPC for CPU:%d\n", 2239 i, pr->id); 2240 goto out_free; 2241 } 2242 } 2243 2244 per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_data_ref ? 2245 pcc_subspace_id : -1; 2246 2247 ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs); 2248 if (ret) 2249 goto out_free; 2250 unsupported_regs = 0; 2251 2252 ret = cpc_validate_required_controls(cpc_ptr); 2253 if (ret) 2254 goto out_free; 2255 2256 /* 2257 * Initialize the remaining cpc_regs as unsupported. 2258 * Example: In case FW exposes CPPC v2, the below loop will initialize 2259 * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported 2260 */ 2261 for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) { 2262 cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER; 2263 cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0; 2264 } 2265 2266 2267 cpc_mark_rmw_lock_users(cpc_ptr); 2268 raw_spin_lock_init(&cpc_ptr->rmw_lock); 2269 2270 /* Parse PSD data for this CPU */ 2271 ret = acpi_get_psd(cpc_ptr, handle); 2272 if (ret) 2273 goto out_free; 2274 2275 ret = cpc_register_sysmem_desc(cpc_ptr); 2276 if (ret) 2277 goto out_free; 2278 2279 /* Register PCC channel once for all PCC subspace ID. */ 2280 if (pcc_data_ref) { 2281 ret = register_pcc_channel(pcc_subspace_id); 2282 if (ret) { 2283 pr_err("Failed to find PCC channel for subspace %d\n", 2284 pcc_subspace_id); 2285 goto out_free; 2286 } 2287 2288 cpc_validate_pcc_bounds(cpc_ptr, pcc_subspace_id, 2289 pcc_data[pcc_subspace_id], 2290 &unsupported_regs); 2291 2292 ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs); 2293 if (ret) 2294 goto out_free; 2295 2296 /* A range-only status entry needs the channel only for bounds. */ 2297 if (!cpc_pcc_access_needed(cpc_ptr)) { 2298 pcc_data_put(pcc_subspace_id); 2299 pcc_data_ref = false; 2300 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2301 } 2302 } 2303 2304 ret = cpc_validate_bound_controls(cpc_ptr); 2305 if (ret) 2306 goto out_free; 2307 2308 ret = cpc_register_non_mmio_desc(cpc_ptr); 2309 if (ret) 2310 goto out_free; 2311 2312 /* Everything looks okay */ 2313 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id); 2314 2315 /* Add per logical CPU nodes for reading its feedback counters. */ 2316 cpu_dev = get_cpu_device(pr->id); 2317 if (!cpu_dev) { 2318 ret = -EINVAL; 2319 goto out_free; 2320 } 2321 2322 /* Plug PSD data into this CPU's CPC descriptor. */ 2323 cpc_set_desc(pr->id, cpc_ptr); 2324 2325 ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj, 2326 "acpi_cppc"); 2327 if (ret) { 2328 cpc_set_desc(pr->id, NULL); 2329 cpc_unregister_non_mmio_desc(cpc_ptr); 2330 cpc_unregister_sysmem_desc(cpc_ptr); 2331 kobject_put(&cpc_ptr->kobj); 2332 goto out_pcc_put; 2333 } 2334 2335 kfree(output.pointer); 2336 return 0; 2337 2338 out_free: 2339 cppc_free_desc(cpc_ptr); 2340 2341 out_pcc_put: 2342 if (pcc_data_ref) 2343 pcc_data_put(pcc_subspace_id); 2344 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2345 2346 out_buf_free: 2347 if (cpc_present) 2348 pr_err("CPU%d: failed to initialize _CPC: %d\n", pr->id, ret); 2349 kfree(output.pointer); 2350 return ret; 2351 } 2352 EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe); 2353 2354 /** 2355 * acpi_cppc_processor_exit - Cleanup CPC structs. 2356 * @pr: Ptr to acpi_processor containing this CPU's logical ID. 2357 * 2358 * Return: Void 2359 */ 2360 void acpi_cppc_processor_exit(struct acpi_processor *pr) 2361 { 2362 struct cpc_desc *cpc_ptr; 2363 int pcc_ss_id; 2364 2365 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id); 2366 if (!cpc_ptr) { 2367 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2368 return; 2369 } 2370 2371 pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id); 2372 cpc_set_desc(pr->id, NULL); 2373 kobject_del(&cpc_ptr->kobj); 2374 cpc_unregister_non_mmio_desc(cpc_ptr); 2375 cpc_unregister_sysmem_desc(cpc_ptr); 2376 2377 pcc_data_put(pcc_ss_id); 2378 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2379 2380 kobject_put(&cpc_ptr->kobj); 2381 } 2382 EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit); 2383 2384 /** 2385 * cpc_read_ffh() - Read FFH register 2386 * @cpunum: CPU number to read 2387 * @reg: cppc register information 2388 * @val: place holder for return value 2389 * 2390 * Read bit_width bits from a specified address and bit_offset 2391 * 2392 * Return: 0 for success and error code 2393 */ 2394 int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val) 2395 { 2396 return -ENOTSUPP; 2397 } 2398 2399 /** 2400 * cpc_read_ffh_fb_ctrs() - Read FFH feedback counters together 2401 * @cpunum: Target CPU 2402 * @reg1: first CPPC register information 2403 * @val1: place holder for first return value 2404 * @reg2: second CPPC register information 2405 * @val2: place holder for second return value 2406 * 2407 * Return: 0 on success, error code otherwise 2408 */ 2409 int __weak cpc_read_ffh_fb_ctrs(int cpunum, struct cpc_reg *reg1, 2410 u64 *val1, struct cpc_reg *reg2, u64 *val2) 2411 { 2412 return -EOPNOTSUPP; 2413 } 2414 2415 /** 2416 * cpc_write_ffh() - Write FFH register 2417 * @cpunum: CPU number to write 2418 * @reg: cppc register information 2419 * @val: value to write 2420 * 2421 * Write value of bit_width bits to a specified address and bit_offset 2422 * 2423 * Return: 0 for success and error code 2424 */ 2425 int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val) 2426 { 2427 return -ENOTSUPP; 2428 } 2429 2430 /* 2431 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be 2432 * as fast as possible. We have already mapped the PCC subspace during init, so 2433 * we can directly write to it. 2434 */ 2435 2436 static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) 2437 { 2438 void __iomem *vaddr = NULL; 2439 unsigned long flags; 2440 u8 buf[sizeof(*val)]; 2441 unsigned int i; 2442 int size; 2443 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2444 struct cpc_reg *reg = ®_res->cpc_entry.reg; 2445 2446 if (!cpc_is_readable(reg_res)) 2447 return -EOPNOTSUPP; 2448 2449 if (reg_res->type == ACPI_TYPE_INTEGER) { 2450 *val = reg_res->cpc_entry.int_value; 2451 return 0; 2452 } 2453 2454 *val = 0; 2455 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 2456 return cpc_read_ffh(cpu, reg, val); 2457 2458 size = GET_BIT_WIDTH(reg); 2459 2460 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2461 u32 val_u32; 2462 acpi_status status; 2463 2464 if (!IS_ENABLED(CONFIG_HAS_IOPORT)) 2465 return -EOPNOTSUPP; 2466 2467 status = acpi_os_read_port((acpi_io_address)reg->address, 2468 &val_u32, size); 2469 if (ACPI_FAILURE(status)) { 2470 pr_debug("Error: Failed to read SystemIO port %llx\n", 2471 reg->address); 2472 return -EFAULT; 2473 } 2474 2475 *val = MASK_VAL_READ(reg, val_u32); 2476 return 0; 2477 } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2478 if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id]) 2479 return -ENODEV; 2480 2481 /* 2482 * For registers in PCC space, the register size is determined 2483 * by the bit width field; the access size is used to indicate 2484 * the PCC subspace id. 2485 */ 2486 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 2487 size = reg->bit_width / 8; 2488 if (!size || size > sizeof(buf) || reg->bit_width % 8) 2489 return -EFAULT; 2490 2491 raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags); 2492 memcpy_fromio(buf, vaddr, size); 2493 raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock, 2494 flags); 2495 2496 *val = 0; 2497 for (i = 0; i < size; i++) 2498 *val |= (u64)buf[i] << (i * 8); 2499 return 0; 2500 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 2501 vaddr = reg_res->sys_mem_vaddr; 2502 else 2503 return acpi_os_read_memory((acpi_physical_address)reg->address, 2504 val, size); 2505 2506 switch (size) { 2507 case 8: 2508 *val = readb_relaxed(vaddr); 2509 break; 2510 case 16: 2511 *val = readw_relaxed(vaddr); 2512 break; 2513 case 32: 2514 *val = readl_relaxed(vaddr); 2515 break; 2516 case 64: 2517 *val = readq_relaxed(vaddr); 2518 break; 2519 default: 2520 pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n", 2521 size, reg->address); 2522 return -EFAULT; 2523 } 2524 2525 *val = MASK_VAL_READ(reg, *val); 2526 2527 return 0; 2528 } 2529 2530 static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val) 2531 { 2532 int ret_val = 0; 2533 int size; 2534 u64 prev_val; 2535 void __iomem *vaddr = NULL; 2536 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2537 struct cpc_reg *reg; 2538 struct cpc_desc *cpc_desc; 2539 unsigned long flags; 2540 u8 buf[sizeof(val)]; 2541 unsigned int i; 2542 bool locked = false; 2543 2544 if (!cpc_is_writable(reg_res)) 2545 return -EOPNOTSUPP; 2546 2547 reg = ®_res->cpc_entry.reg; 2548 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 2549 return cpc_write_ffh(cpu, reg, val); 2550 2551 size = GET_BIT_WIDTH(reg); 2552 2553 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2554 acpi_status status; 2555 2556 if (!IS_ENABLED(CONFIG_HAS_IOPORT)) 2557 return -EOPNOTSUPP; 2558 2559 status = acpi_os_write_port((acpi_io_address)reg->address, 2560 (u32)val, size); 2561 if (ACPI_FAILURE(status)) { 2562 pr_debug("Error: Failed to write SystemIO port %llx\n", 2563 reg->address); 2564 return -EFAULT; 2565 } 2566 2567 return 0; 2568 } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2569 if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id]) 2570 return -ENODEV; 2571 2572 /* 2573 * For registers in PCC space, the register size is determined 2574 * by the bit width field; the access size is used to indicate 2575 * the PCC subspace id. 2576 */ 2577 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 2578 size = reg->bit_width / 8; 2579 if (!size || size > sizeof(buf) || reg->bit_width % 8) 2580 return -EFAULT; 2581 2582 for (i = 0; i < size; i++) 2583 buf[i] = val >> (i * 8); 2584 2585 raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags); 2586 memcpy_toio(vaddr, buf, size); 2587 /* Publish every payload byte before another CPU can ring the doorbell. */ 2588 wmb(); 2589 raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock, 2590 flags); 2591 return 0; 2592 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 2593 vaddr = reg_res->sys_mem_vaddr; 2594 else 2595 return acpi_os_write_memory((acpi_physical_address)reg->address, 2596 val, size); 2597 2598 /* Partial fields and local overlaps use the descriptor lock. */ 2599 locked = reg_res->cpc_entry.use_rmw_lock; 2600 if (locked) { 2601 cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2602 if (!cpc_desc) { 2603 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2604 return -ENODEV; 2605 } 2606 raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags); 2607 } 2608 2609 if (reg->bit_offset || reg->bit_width != size) { 2610 /* 2611 * MASK_VAL_WRITE() discards the field's old bits, so undefined 2612 * readback from a write-only field is not propagated. 2613 */ 2614 switch (size) { 2615 case 8: 2616 prev_val = readb_relaxed(vaddr); 2617 break; 2618 case 16: 2619 prev_val = readw_relaxed(vaddr); 2620 break; 2621 case 32: 2622 prev_val = readl_relaxed(vaddr); 2623 break; 2624 case 64: 2625 prev_val = readq_relaxed(vaddr); 2626 break; 2627 default: 2628 if (locked) 2629 raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, 2630 flags); 2631 return -EFAULT; 2632 } 2633 val = MASK_VAL_WRITE(reg, prev_val, val); 2634 } 2635 2636 switch (size) { 2637 case 8: 2638 writeb_relaxed(val, vaddr); 2639 break; 2640 case 16: 2641 writew_relaxed(val, vaddr); 2642 break; 2643 case 32: 2644 writel_relaxed(val, vaddr); 2645 break; 2646 case 64: 2647 writeq_relaxed(val, vaddr); 2648 break; 2649 default: 2650 pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n", 2651 size, reg->address); 2652 ret_val = -EFAULT; 2653 break; 2654 } 2655 2656 if (locked) { 2657 if (!ret_val) 2658 mmiowb_set_pending(); 2659 raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags); 2660 } 2661 2662 return ret_val; 2663 } 2664 2665 static int cppc_get_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 *val) 2666 { 2667 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2668 struct cppc_pcc_data *pcc_ss_data = NULL; 2669 int ret; 2670 2671 if (pcc_ss_id < 0) { 2672 pr_debug("Invalid pcc_ss_id\n"); 2673 return -ENODEV; 2674 } 2675 2676 pcc_ss_data = pcc_data[pcc_ss_id]; 2677 2678 down_write(&pcc_ss_data->pcc_lock); 2679 2680 if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0) 2681 ret = cpc_read(cpu, reg, val); 2682 else 2683 ret = -EIO; 2684 2685 up_write(&pcc_ss_data->pcc_lock); 2686 2687 return ret; 2688 } 2689 2690 static int cppc_get_reg_val(int cpu, enum cppc_regs reg_idx, u64 *val) 2691 { 2692 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2693 struct cpc_register_resource *reg; 2694 2695 if (val == NULL) 2696 return -EINVAL; 2697 2698 if (!cpc_desc) { 2699 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2700 return -ENODEV; 2701 } 2702 if (cpc_reg_is_write_only(cpc_desc, reg_idx)) 2703 return -EOPNOTSUPP; 2704 2705 reg = &cpc_desc->cpc_regs[reg_idx]; 2706 2707 /* 2708 * Desired and Performance Limited may be disabled despite not being 2709 * generally optional. 2710 */ 2711 if ((reg->type == ACPI_TYPE_INTEGER && 2712 (IS_OPTIONAL_CPC_REG(reg_idx) || reg_idx == DESIRED_PERF || 2713 reg_idx == PERF_LIMITED) && 2714 !reg->cpc_entry.int_value) || (reg->type != ACPI_TYPE_INTEGER && 2715 IS_NULL_REG(®->cpc_entry.reg))) { 2716 pr_debug("CPC register is not supported\n"); 2717 return -EOPNOTSUPP; 2718 } 2719 if (!cpc_is_readable(reg)) 2720 return -EOPNOTSUPP; 2721 2722 if (CPC_IN_PCC(reg)) 2723 return cppc_get_reg_val_in_pcc(cpu, reg, val); 2724 2725 return cpc_read(cpu, reg, val); 2726 } 2727 2728 static int cppc_set_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 val) 2729 { 2730 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2731 struct cppc_pcc_data *pcc_ss_data; 2732 int ret; 2733 2734 if (pcc_ss_id < 0) { 2735 pr_debug("Invalid pcc_ss_id\n"); 2736 return -ENODEV; 2737 } 2738 pcc_ss_data = pcc_data[pcc_ss_id]; 2739 if (!pcc_ss_data) 2740 return -ENODEV; 2741 2742 down_write(&pcc_ss_data->pcc_lock); 2743 2744 ret = check_pcc_chan(pcc_ss_id, false); 2745 if (ret) 2746 goto out; 2747 2748 ret = cpc_write(cpu, reg, val); 2749 if (ret) 2750 goto out; 2751 2752 /* after writing CPC, transfer the ownership of PCC to platform */ 2753 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 2754 2755 out: 2756 if (ret) 2757 cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret); 2758 up_write(&pcc_ss_data->pcc_lock); 2759 2760 return ret; 2761 } 2762 2763 static int cppc_set_reg_val(int cpu, enum cppc_regs reg_idx, u64 val) 2764 { 2765 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2766 struct cpc_register_resource *reg; 2767 2768 if (!cpc_desc) { 2769 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2770 return -ENODEV; 2771 } 2772 2773 reg = &cpc_desc->cpc_regs[reg_idx]; 2774 2775 /* Integer 1 describes autonomous selection that is always enabled. */ 2776 if (reg_idx == AUTO_SEL_ENABLE && reg->type == ACPI_TYPE_INTEGER && 2777 reg->cpc_entry.int_value == 1) 2778 return val == 1 ? 0 : -EOPNOTSUPP; 2779 2780 /* if a register is writeable, it must be a buffer and not null */ 2781 if (!cpc_is_writable(reg)) { 2782 pr_debug("CPC register is not supported\n"); 2783 return -EOPNOTSUPP; 2784 } 2785 2786 if (CPC_IN_PCC(reg)) 2787 return cppc_set_reg_val_in_pcc(cpu, reg, val); 2788 2789 return cpc_write(cpu, reg, val); 2790 } 2791 2792 /** 2793 * cppc_get_desired_perf - Get the desired performance register value. 2794 * @cpunum: CPU from which to get desired performance. 2795 * @desired_perf: Return address. 2796 * 2797 * Return: 0 for success, -EOPNOTSUPP for _CPC revision 4 or later, and a 2798 * negative errno otherwise. 2799 */ 2800 int cppc_get_desired_perf(int cpunum, u64 *desired_perf) 2801 { 2802 return cppc_get_reg_val(cpunum, DESIRED_PERF, desired_perf); 2803 } 2804 EXPORT_SYMBOL_GPL(cppc_get_desired_perf); 2805 2806 /** 2807 * cppc_get_nominal_perf - Get the nominal performance register value. 2808 * @cpunum: CPU from which to get nominal performance. 2809 * @nominal_perf: Return address. 2810 * 2811 * Return: 0 for success, -EIO otherwise. 2812 */ 2813 int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf) 2814 { 2815 return cppc_get_reg_val(cpunum, NOMINAL_PERF, nominal_perf); 2816 } 2817 2818 /** 2819 * cppc_get_highest_perf - Get the highest performance register value. 2820 * @cpunum: CPU from which to get highest performance. 2821 * @highest_perf: Return address. 2822 * 2823 * Return: 0 for success, -EIO otherwise. 2824 */ 2825 int cppc_get_highest_perf(int cpunum, u64 *highest_perf) 2826 { 2827 return cppc_get_reg_val(cpunum, HIGHEST_PERF, highest_perf); 2828 } 2829 EXPORT_SYMBOL_GPL(cppc_get_highest_perf); 2830 2831 /** 2832 * cppc_get_epp_perf - Get the epp register value. 2833 * @cpunum: CPU from which to get epp preference value. 2834 * @epp_perf: Return address. 2835 * 2836 * Return: 0 for success, -EIO otherwise. 2837 */ 2838 int cppc_get_epp_perf(int cpunum, u64 *epp_perf) 2839 { 2840 return cppc_get_reg_val(cpunum, ENERGY_PERF, epp_perf); 2841 } 2842 EXPORT_SYMBOL_GPL(cppc_get_epp_perf); 2843 2844 /** 2845 * cppc_get_perf_caps - Get a CPU's performance capabilities. 2846 * @cpunum: CPU from which to get capabilities info. 2847 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h 2848 * 2849 * Return: 0 for success with perf_caps populated else -ERRNO. 2850 */ 2851 int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps) 2852 { 2853 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 2854 struct cpc_register_resource *highest_reg, *lowest_reg, 2855 *lowest_non_linear_reg, *nominal_reg, *reference_reg, 2856 *guaranteed_reg, *low_freq_reg = NULL, *nom_freq_reg = NULL; 2857 u64 high, low, guaranteed = 0, nom, ref, min_nonlinear, 2858 low_f = 0, nom_f = 0; 2859 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 2860 struct cppc_pcc_data *pcc_ss_data = NULL; 2861 int ret = 0, regs_in_pcc = 0; 2862 2863 if (!cpc_desc) { 2864 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 2865 return -ENODEV; 2866 } 2867 2868 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF]; 2869 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF]; 2870 lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF]; 2871 nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF]; 2872 reference_reg = &cpc_desc->cpc_regs[REFERENCE_PERF]; 2873 low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ]; 2874 nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ]; 2875 guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF]; 2876 2877 /* Are any of the regs PCC ?*/ 2878 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) || 2879 CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) || 2880 (CPC_SUPPORTED(reference_reg) && CPC_IN_PCC(reference_reg)) || 2881 CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg) || 2882 CPC_IN_PCC(guaranteed_reg)) { 2883 if (pcc_ss_id < 0) { 2884 pr_debug("Invalid pcc_ss_id\n"); 2885 return -ENODEV; 2886 } 2887 pcc_ss_data = pcc_data[pcc_ss_id]; 2888 regs_in_pcc = 1; 2889 down_write(&pcc_ss_data->pcc_lock); 2890 /* Ring doorbell once to update PCC subspace */ 2891 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 2892 ret = -EIO; 2893 goto out_err; 2894 } 2895 } 2896 2897 ret = cpc_read(cpunum, highest_reg, &high); 2898 if (ret) 2899 goto out_err; 2900 perf_caps->highest_perf = high; 2901 2902 ret = cpc_read(cpunum, lowest_reg, &low); 2903 if (ret) 2904 goto out_err; 2905 perf_caps->lowest_perf = low; 2906 2907 ret = cpc_read(cpunum, nominal_reg, &nom); 2908 if (ret) 2909 goto out_err; 2910 perf_caps->nominal_perf = nom; 2911 2912 /* 2913 * If reference perf register is not supported then we should 2914 * use the nominal perf value 2915 */ 2916 if (CPC_SUPPORTED(reference_reg)) { 2917 ret = cpc_read(cpunum, reference_reg, &ref); 2918 if (ret) 2919 goto out_err; 2920 } else { 2921 ref = nom; 2922 } 2923 perf_caps->reference_perf = ref; 2924 2925 if (guaranteed_reg->type != ACPI_TYPE_BUFFER || 2926 IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) { 2927 perf_caps->guaranteed_perf = 0; 2928 } else { 2929 ret = cpc_read(cpunum, guaranteed_reg, &guaranteed); 2930 if (ret) 2931 goto out_err; 2932 perf_caps->guaranteed_perf = guaranteed; 2933 } 2934 2935 ret = cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear); 2936 if (ret) 2937 goto out_err; 2938 perf_caps->lowest_nonlinear_perf = min_nonlinear; 2939 2940 if (!high || !low || !nom || !ref || !min_nonlinear || 2941 high > U32_MAX || low > U32_MAX || guaranteed > U32_MAX || 2942 nom > U32_MAX || ref > U32_MAX || min_nonlinear > U32_MAX || 2943 high < nom || nom < min_nonlinear || min_nonlinear < low || 2944 (CPC_SUPPORTED(guaranteed_reg) && 2945 (guaranteed < low || guaranteed > nom))) { 2946 ret = -EFAULT; 2947 goto out_err; 2948 } 2949 2950 /* Read optional lowest and nominal frequencies if present */ 2951 if (CPC_SUPPORTED(low_freq_reg)) { 2952 ret = cpc_read(cpunum, low_freq_reg, &low_f); 2953 if (ret) 2954 goto out_err; 2955 } 2956 2957 if (CPC_SUPPORTED(nom_freq_reg)) { 2958 ret = cpc_read(cpunum, nom_freq_reg, &nom_f); 2959 if (ret) 2960 goto out_err; 2961 } 2962 /* Require ordered anchors and a nonzero slope when frequencies differ. */ 2963 if (low_f > U32_MAX || nom_f > U32_MAX || 2964 (low_f && nom_f && 2965 (nom_f < low_f || nom < low || 2966 (nom_f != low_f && nom == low)))) { 2967 ret = -EFAULT; 2968 goto out_err; 2969 } 2970 2971 perf_caps->lowest_freq = low_f; 2972 perf_caps->nominal_freq = nom_f; 2973 2974 2975 out_err: 2976 if (regs_in_pcc) 2977 up_write(&pcc_ss_data->pcc_lock); 2978 return ret; 2979 } 2980 EXPORT_SYMBOL_GPL(cppc_get_perf_caps); 2981 2982 /** 2983 * cppc_perf_ctrs_in_pcc_cpu - Check if any perf counters of a CPU are in PCC. 2984 * @cpu: CPU on which to check perf counters. 2985 * 2986 * Return: true if any of the counters are in PCC regions, false otherwise 2987 */ 2988 bool cppc_perf_ctrs_in_pcc_cpu(unsigned int cpu) 2989 { 2990 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2991 2992 if (!cpc_desc) 2993 return false; 2994 2995 return CPC_IN_PCC(&cpc_desc->cpc_regs[DELIVERED_CTR]) || 2996 CPC_IN_PCC(&cpc_desc->cpc_regs[REFERENCE_CTR]) || 2997 CPC_IN_PCC(&cpc_desc->cpc_regs[CTR_WRAP_TIME]); 2998 } 2999 EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc_cpu); 3000 3001 static int cppc_read_fb_ctrs(int cpunum, 3002 struct cpc_register_resource *delivered_reg, 3003 struct cpc_register_resource *reference_reg, 3004 u64 *delivered, u64 *reference) 3005 { 3006 int ret; 3007 3008 /* 3009 * For FFH feedback counters, try a paired read first to reduce 3010 * sampling skew between delivered and reference counters. Fall 3011 * back to the existing per-register reads if unsupported. 3012 */ 3013 if (CPC_IN_FFH(delivered_reg) && CPC_IN_FFH(reference_reg)) { 3014 ret = cpc_read_ffh_fb_ctrs(cpunum, 3015 &delivered_reg->cpc_entry.reg, delivered, 3016 &reference_reg->cpc_entry.reg, reference); 3017 if (ret != -EOPNOTSUPP) 3018 return ret; 3019 } 3020 3021 ret = cpc_read(cpunum, delivered_reg, delivered); 3022 if (ret) 3023 return ret; 3024 3025 return cpc_read(cpunum, reference_reg, reference); 3026 } 3027 3028 /** 3029 * cppc_perf_ctrs_in_pcc - Check if any perf counters are in a PCC region. 3030 * 3031 * CPPC has flexibility about how CPU performance counters are accessed. 3032 * One of the choices is PCC regions, which can have a high access latency. This 3033 * routine allows callers of cppc_get_perf_ctrs() to know this ahead of time. 3034 * 3035 * Return: true if any of the counters are in PCC regions, false otherwise 3036 */ 3037 bool cppc_perf_ctrs_in_pcc(void) 3038 { 3039 int cpu; 3040 3041 for_each_online_cpu(cpu) { 3042 if (cppc_perf_ctrs_in_pcc_cpu(cpu)) 3043 return true; 3044 } 3045 3046 return false; 3047 } 3048 EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc); 3049 3050 /** 3051 * cppc_get_perf_ctrs - Read a CPU's performance feedback counters. 3052 * @cpunum: CPU from which to read counters. 3053 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h 3054 * 3055 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO. 3056 */ 3057 int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs) 3058 { 3059 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 3060 struct cpc_register_resource *delivered_reg, *reference_reg, 3061 *ctr_wrap_reg; 3062 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 3063 struct cppc_pcc_data *pcc_ss_data = NULL; 3064 u64 delivered, reference, ctr_wrap_time; 3065 int ret = 0, regs_in_pcc = 0; 3066 3067 if (!cpc_desc) { 3068 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 3069 return -ENODEV; 3070 } 3071 3072 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR]; 3073 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR]; 3074 ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME]; 3075 3076 /* Are any of the regs PCC ?*/ 3077 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) || 3078 CPC_IN_PCC(ctr_wrap_reg)) { 3079 if (pcc_ss_id < 0) { 3080 pr_debug("Invalid pcc_ss_id\n"); 3081 return -ENODEV; 3082 } 3083 pcc_ss_data = pcc_data[pcc_ss_id]; 3084 down_write(&pcc_ss_data->pcc_lock); 3085 regs_in_pcc = 1; 3086 /* Ring doorbell once to update PCC subspace */ 3087 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 3088 ret = -EIO; 3089 goto out_err; 3090 } 3091 } 3092 3093 ret = cppc_read_fb_ctrs(cpunum, delivered_reg, reference_reg, 3094 &delivered, &reference); 3095 if (ret) 3096 goto out_err; 3097 3098 /* 3099 * Per spec, if ctr_wrap_time optional register is unsupported, then the 3100 * performance counters are assumed to never wrap during the lifetime of 3101 * platform 3102 */ 3103 ctr_wrap_time = (u64)(~((u64)0)); 3104 if (CPC_SUPPORTED(ctr_wrap_reg)) { 3105 ret = cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time); 3106 if (ret) 3107 goto out_err; 3108 } 3109 3110 if (!delivered || !reference) { 3111 ret = -EFAULT; 3112 goto out_err; 3113 } 3114 3115 perf_fb_ctrs->delivered = delivered; 3116 perf_fb_ctrs->reference = reference; 3117 perf_fb_ctrs->wraparound_time = ctr_wrap_time; 3118 out_err: 3119 if (regs_in_pcc) 3120 up_write(&pcc_ss_data->pcc_lock); 3121 return ret; 3122 } 3123 EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs); 3124 3125 /* 3126 * Set Energy Performance Preference Register value through 3127 * Performance Controls Interface 3128 */ 3129 int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable) 3130 { 3131 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3132 struct cpc_register_resource *epp_set_reg; 3133 struct cpc_register_resource *auto_sel_reg; 3134 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3135 struct cppc_pcc_data *pcc_ss_data = NULL; 3136 bool auto_sel_pcc; 3137 bool auto_sel_non_pcc; 3138 bool epp_pcc; 3139 bool epp_non_pcc; 3140 int ret; 3141 3142 if (!cpc_desc) { 3143 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3144 return -ENODEV; 3145 } 3146 3147 auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3148 epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF]; 3149 if (!enable && auto_sel_reg->type == ACPI_TYPE_INTEGER && 3150 auto_sel_reg->cpc_entry.int_value == 1) 3151 return -EOPNOTSUPP; 3152 3153 auto_sel_pcc = cpc_is_writable(auto_sel_reg) && 3154 CPC_IN_PCC(auto_sel_reg); 3155 epp_pcc = cpc_is_writable(epp_set_reg) && CPC_IN_PCC(epp_set_reg); 3156 3157 auto_sel_non_pcc = cpc_is_writable(auto_sel_reg) && !auto_sel_pcc; 3158 epp_non_pcc = cpc_is_writable(epp_set_reg) && !epp_pcc; 3159 3160 /* Complete fallible non-PCC writes before staging PCC data. */ 3161 if (auto_sel_non_pcc) { 3162 ret = cpc_write(cpu, auto_sel_reg, enable); 3163 if (ret) 3164 return ret; 3165 } 3166 if (epp_non_pcc) { 3167 ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf); 3168 if (ret) 3169 return ret; 3170 } 3171 3172 if (epp_pcc || auto_sel_pcc) { 3173 if (pcc_ss_id < 0) { 3174 pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu); 3175 return -ENODEV; 3176 } 3177 3178 pcc_ss_data = pcc_data[pcc_ss_id]; 3179 if (!pcc_ss_data) 3180 return -ENODEV; 3181 3182 down_write(&pcc_ss_data->pcc_lock); 3183 3184 ret = check_pcc_chan(pcc_ss_id, false); 3185 if (ret) 3186 goto out_unlock; 3187 3188 if (auto_sel_pcc) { 3189 ret = cpc_write(cpu, auto_sel_reg, enable); 3190 if (ret) 3191 goto out_unlock; 3192 } 3193 3194 if (epp_pcc) { 3195 ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf); 3196 if (ret) 3197 goto out_unlock; 3198 } 3199 3200 /* after writing CPC, transfer the ownership of PCC to platform */ 3201 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3202 3203 out_unlock: 3204 if (ret) 3205 cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret); 3206 up_write(&pcc_ss_data->pcc_lock); 3207 } else if (epp_non_pcc || auto_sel_non_pcc) { 3208 ret = 0; 3209 } else { 3210 ret = -EOPNOTSUPP; 3211 pr_debug("No writable EPP controls for CPU:%d\n", cpu); 3212 } 3213 3214 return ret; 3215 } 3216 EXPORT_SYMBOL_GPL(cppc_set_epp_perf); 3217 3218 /** 3219 * cppc_set_epp() - Write the EPP register. 3220 * @cpu: CPU on which to write register. 3221 * @epp_val: Value to write to the EPP register. 3222 */ 3223 int cppc_set_epp(int cpu, u64 epp_val) 3224 { 3225 if (epp_val > CPPC_EPP_ENERGY_EFFICIENCY_PREF) 3226 return -EINVAL; 3227 3228 return cppc_set_reg_val(cpu, ENERGY_PERF, epp_val); 3229 } 3230 EXPORT_SYMBOL_GPL(cppc_set_epp); 3231 3232 /** 3233 * cppc_get_auto_act_window() - Read autonomous activity window register. 3234 * @cpu: CPU from which to read register. 3235 * @auto_act_window: Return address. 3236 * 3237 * According to ACPI 6.5, s8.4.6.1.6, the value read from the autonomous 3238 * activity window register consists of two parts: a 7 bits value indicate 3239 * significand and a 3 bits value indicate exponent. 3240 */ 3241 int cppc_get_auto_act_window(int cpu, u64 *auto_act_window) 3242 { 3243 unsigned int exp; 3244 u64 val, sig; 3245 int ret; 3246 3247 if (auto_act_window == NULL) 3248 return -EINVAL; 3249 3250 ret = cppc_get_reg_val(cpu, AUTO_ACT_WINDOW, &val); 3251 if (ret) 3252 return ret; 3253 3254 sig = val & CPPC_AUTO_ACT_WINDOW_MAX_SIG; 3255 exp = (val >> CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) & CPPC_AUTO_ACT_WINDOW_MAX_EXP; 3256 *auto_act_window = sig * int_pow(10, exp); 3257 3258 return 0; 3259 } 3260 EXPORT_SYMBOL_GPL(cppc_get_auto_act_window); 3261 3262 /** 3263 * cppc_set_auto_act_window() - Write autonomous activity window register. 3264 * @cpu: CPU on which to write register. 3265 * @auto_act_window: usec value to write to the autonomous activity window register. 3266 * 3267 * According to ACPI 6.5, s8.4.6.1.6, the value to write to the autonomous 3268 * activity window register consists of two parts: a 7 bits value indicate 3269 * significand and a 3 bits value indicate exponent. 3270 */ 3271 int cppc_set_auto_act_window(int cpu, u64 auto_act_window) 3272 { 3273 /* The max value to store is 1270000000 */ 3274 u64 max_val = CPPC_AUTO_ACT_WINDOW_MAX_SIG * int_pow(10, CPPC_AUTO_ACT_WINDOW_MAX_EXP); 3275 int exp = 0; 3276 u64 val; 3277 3278 if (auto_act_window > max_val) 3279 return -EINVAL; 3280 3281 /* 3282 * The max significand is 127, when auto_act_window is larger than 3283 * 129, discard the precision of the last digit and increase the 3284 * exponent by 1. 3285 */ 3286 while (auto_act_window > CPPC_AUTO_ACT_WINDOW_SIG_CARRY_THRESH) { 3287 auto_act_window /= 10; 3288 exp += 1; 3289 } 3290 3291 /* For 128 and 129, cut it to 127. */ 3292 if (auto_act_window > CPPC_AUTO_ACT_WINDOW_MAX_SIG) 3293 auto_act_window = CPPC_AUTO_ACT_WINDOW_MAX_SIG; 3294 3295 val = (exp << CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) + auto_act_window; 3296 3297 return cppc_set_reg_val(cpu, AUTO_ACT_WINDOW, val); 3298 } 3299 EXPORT_SYMBOL_GPL(cppc_set_auto_act_window); 3300 3301 /** 3302 * cppc_get_auto_sel() - Read autonomous selection register. 3303 * @cpu: CPU from which to read register. 3304 * @enable: Return address. 3305 */ 3306 int cppc_get_auto_sel(int cpu, bool *enable) 3307 { 3308 u64 auto_sel; 3309 int ret; 3310 3311 if (enable == NULL) 3312 return -EINVAL; 3313 3314 ret = cppc_get_reg_val(cpu, AUTO_SEL_ENABLE, &auto_sel); 3315 if (ret) 3316 return ret; 3317 3318 *enable = (bool)auto_sel; 3319 3320 return 0; 3321 } 3322 EXPORT_SYMBOL_GPL(cppc_get_auto_sel); 3323 3324 /** 3325 * cppc_auto_sel_is_immutable - Check for always-enabled autonomous selection. 3326 * @cpu: CPU whose _CPC descriptor to check. 3327 * 3328 * Context: Process context. 3329 * Return: true for Integer 1, false for a register or an absent descriptor. 3330 */ 3331 bool cppc_auto_sel_is_immutable(int cpu) 3332 { 3333 struct cpc_desc *cpc_desc; 3334 struct cpc_register_resource *reg; 3335 3336 guard(mutex)(&cpc_desc_lock); 3337 cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3338 if (!cpc_desc) 3339 return false; 3340 3341 reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3342 return reg->type == ACPI_TYPE_INTEGER && reg->cpc_entry.int_value == 1; 3343 } 3344 EXPORT_SYMBOL_GPL(cppc_auto_sel_is_immutable); 3345 3346 /** 3347 * cppc_set_auto_sel - Write autonomous selection register. 3348 * @cpu : CPU to which to write register. 3349 * @enable : the desired value of autonomous selection resiter to be updated. 3350 */ 3351 int cppc_set_auto_sel(int cpu, bool enable) 3352 { 3353 return cppc_set_reg_val(cpu, AUTO_SEL_ENABLE, enable); 3354 } 3355 EXPORT_SYMBOL_GPL(cppc_set_auto_sel); 3356 3357 /** 3358 * cppc_set_enable - Set to enable CPPC on the processor by writing the 3359 * Continuous Performance Control package EnableRegister field. 3360 * @cpu: CPU for which to enable CPPC register. 3361 * @enable: 0 - disable, 1 - enable CPPC feature on the processor. 3362 * 3363 * Return: 0 for success, -ERRNO or -EIO otherwise. 3364 */ 3365 int cppc_set_enable(int cpu, bool enable) 3366 { 3367 return cppc_set_reg_val(cpu, ENABLE, enable); 3368 } 3369 EXPORT_SYMBOL_GPL(cppc_set_enable); 3370 3371 /** 3372 * cppc_get_perf - Get a CPU's performance controls. 3373 * @cpu: CPU for which to get performance controls. 3374 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h 3375 * 3376 * Desired Performance is not read and is returned as 0. 3377 * 3378 * Return: 0 for success with perf_ctrls, -ERRNO otherwise. 3379 */ 3380 int cppc_get_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) 3381 { 3382 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3383 struct cpc_register_resource *min_perf_reg, *max_perf_reg, 3384 *energy_perf_reg, *auto_sel_reg; 3385 u64 min = 0, max = 0, energy_perf = 0, auto_sel = 0; 3386 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3387 struct cppc_pcc_data *pcc_ss_data = NULL; 3388 int ret = 0, regs_in_pcc = 0; 3389 3390 if (!cpc_desc) { 3391 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3392 return -ENODEV; 3393 } 3394 3395 if (!perf_ctrls) { 3396 pr_debug("Invalid perf_ctrls pointer\n"); 3397 return -EINVAL; 3398 } 3399 3400 min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF]; 3401 max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF]; 3402 energy_perf_reg = &cpc_desc->cpc_regs[ENERGY_PERF]; 3403 auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3404 perf_ctrls->min_perf_valid = false; 3405 3406 /* Are any of the regs PCC ?*/ 3407 if (CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg) || 3408 CPC_IN_PCC(energy_perf_reg) || 3409 CPC_IN_PCC(auto_sel_reg)) { 3410 if (pcc_ss_id < 0) { 3411 pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu); 3412 return -ENODEV; 3413 } 3414 pcc_ss_data = pcc_data[pcc_ss_id]; 3415 regs_in_pcc = 1; 3416 down_write(&pcc_ss_data->pcc_lock); 3417 /* Ring doorbell once to update PCC subspace */ 3418 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 3419 ret = -EIO; 3420 goto out_err; 3421 } 3422 } 3423 3424 /* Read optional elements if present */ 3425 if (CPC_SUPPORTED(max_perf_reg)) { 3426 ret = cpc_read(cpu, max_perf_reg, &max); 3427 if (ret) 3428 goto out_err; 3429 if (max > U32_MAX) { 3430 ret = -EFAULT; 3431 goto out_err; 3432 } 3433 } 3434 perf_ctrls->max_perf = max; 3435 3436 if (CPC_SUPPORTED(min_perf_reg)) { 3437 ret = cpc_read(cpu, min_perf_reg, &min); 3438 if (ret) 3439 goto out_err; 3440 if (min > U32_MAX) { 3441 ret = -EFAULT; 3442 goto out_err; 3443 } 3444 perf_ctrls->min_perf_valid = true; 3445 } 3446 perf_ctrls->min_perf = min; 3447 3448 perf_ctrls->desired_perf = 0; 3449 3450 if (CPC_SUPPORTED(energy_perf_reg)) { 3451 ret = cpc_read(cpu, energy_perf_reg, &energy_perf); 3452 if (ret) 3453 goto out_err; 3454 } 3455 perf_ctrls->energy_perf = energy_perf; 3456 3457 if (CPC_SUPPORTED(auto_sel_reg)) { 3458 ret = cpc_read(cpu, auto_sel_reg, &auto_sel); 3459 if (ret) 3460 goto out_err; 3461 } 3462 perf_ctrls->auto_sel = (bool)auto_sel; 3463 3464 out_err: 3465 if (regs_in_pcc) 3466 up_write(&pcc_ss_data->pcc_lock); 3467 return ret; 3468 } 3469 EXPORT_SYMBOL_GPL(cppc_get_perf); 3470 3471 /** 3472 * cppc_set_perf - Set a CPU's performance controls. 3473 * @cpu: CPU for which to set performance controls. 3474 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h 3475 * 3476 * Return: 0 for success, -ERRNO otherwise. 3477 */ 3478 int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) 3479 { 3480 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3481 struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg; 3482 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3483 struct cppc_pcc_data *pcc_ss_data = NULL; 3484 bool desired_update, min_update, max_update; 3485 bool desired_pcc, min_pcc, max_pcc, pcc_update; 3486 bool pcc_layout, direct_layout, mixed_layout; 3487 int ret = 0; 3488 3489 if (!cpc_desc) { 3490 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3491 return -ENODEV; 3492 } 3493 3494 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 3495 min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF]; 3496 max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF]; 3497 desired_update = cpc_is_writable(desired_reg); 3498 min_update = cpc_is_writable(min_perf_reg) && 3499 (perf_ctrls->min_perf || perf_ctrls->min_perf_valid); 3500 max_update = cpc_is_writable(max_perf_reg) && 3501 perf_ctrls->max_perf; 3502 desired_pcc = desired_update && CPC_IN_PCC(desired_reg); 3503 min_pcc = min_update && CPC_IN_PCC(min_perf_reg); 3504 max_pcc = max_update && CPC_IN_PCC(max_perf_reg); 3505 pcc_update = desired_pcc || min_pcc || max_pcc; 3506 pcc_layout = (cpc_is_writable(desired_reg) && CPC_IN_PCC(desired_reg)) || 3507 (cpc_is_writable(min_perf_reg) && CPC_IN_PCC(min_perf_reg)) || 3508 (cpc_is_writable(max_perf_reg) && CPC_IN_PCC(max_perf_reg)); 3509 direct_layout = (cpc_is_writable(desired_reg) && 3510 !CPC_IN_PCC(desired_reg)) || 3511 (cpc_is_writable(min_perf_reg) && 3512 !CPC_IN_PCC(min_perf_reg)) || 3513 (cpc_is_writable(max_perf_reg) && 3514 !CPC_IN_PCC(max_perf_reg)); 3515 mixed_layout = pcc_layout && direct_layout; 3516 3517 if (mixed_layout || pcc_update) { 3518 if (pcc_ss_id < 0) { 3519 pr_debug("Invalid pcc_ss_id\n"); 3520 return -ENODEV; 3521 } 3522 pcc_ss_data = pcc_data[pcc_ss_id]; 3523 if (!pcc_ss_data) 3524 return -ENODEV; 3525 } 3526 3527 /* 3528 * A mixed layout cannot batch fallible direct writes safely: another 3529 * CPU's staged PCC values may no longer match if a direct write fails. 3530 * Serialize the complete mixed transaction and drain an older batch 3531 * before changing a direct control. 3532 */ 3533 if (mixed_layout) { 3534 down_write(&pcc_ss_data->pcc_lock); 3535 if (pcc_ss_data->pending_pcc_write_cmd) { 3536 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3537 if (ret) 3538 goto out_mixed_unlock; 3539 } 3540 3541 if (pcc_ss_data->platform_owns_pcc) { 3542 ret = check_pcc_chan(pcc_ss_id, false); 3543 if (ret) 3544 goto out_mixed_unlock; 3545 } 3546 3547 if (desired_update && !desired_pcc) { 3548 ret = cpc_write(cpu, desired_reg, 3549 perf_ctrls->desired_perf); 3550 if (ret) 3551 goto out_mixed_unlock; 3552 } 3553 if (min_update && !min_pcc) { 3554 ret = cpc_write(cpu, min_perf_reg, 3555 perf_ctrls->min_perf); 3556 if (ret) 3557 goto out_mixed_unlock; 3558 } 3559 if (max_update && !max_pcc) { 3560 ret = cpc_write(cpu, max_perf_reg, 3561 perf_ctrls->max_perf); 3562 if (ret) 3563 goto out_mixed_unlock; 3564 } 3565 3566 if (desired_pcc) { 3567 ret = cpc_write(cpu, desired_reg, 3568 perf_ctrls->desired_perf); 3569 if (ret) 3570 goto out_mixed_unlock; 3571 } 3572 if (min_pcc) { 3573 ret = cpc_write(cpu, min_perf_reg, 3574 perf_ctrls->min_perf); 3575 if (ret) 3576 goto out_mixed_unlock; 3577 } 3578 if (max_pcc) { 3579 ret = cpc_write(cpu, max_perf_reg, 3580 perf_ctrls->max_perf); 3581 if (ret) 3582 goto out_mixed_unlock; 3583 } 3584 3585 if (pcc_update) { 3586 WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true); 3587 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; 3588 cpc_desc->write_cmd_status = 0; 3589 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3590 } 3591 3592 out_mixed_unlock: 3593 up_write(&pcc_ss_data->pcc_lock); 3594 return ret; 3595 } 3596 3597 /* A request without PCC updates has no payload to coordinate. */ 3598 if (!pcc_update) { 3599 if (desired_update) { 3600 ret = cpc_write(cpu, desired_reg, 3601 perf_ctrls->desired_perf); 3602 if (ret) 3603 return ret; 3604 } 3605 if (min_update) { 3606 ret = cpc_write(cpu, min_perf_reg, 3607 perf_ctrls->min_perf); 3608 if (ret) 3609 return ret; 3610 } 3611 if (max_update) 3612 ret = cpc_write(cpu, max_perf_reg, 3613 perf_ctrls->max_perf); 3614 return ret; 3615 } 3616 3617 down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */ 3618 if (pcc_ss_data->platform_owns_pcc) { 3619 ret = check_pcc_chan(pcc_ss_id, false); 3620 if (ret) 3621 goto out_pcc_read_unlock; 3622 } 3623 3624 /* 3625 * This is Phase-I where we want to write to CPC registers 3626 * -> We want all CPUs to be able to execute this phase in parallel 3627 * 3628 * Since read_lock can be acquired by multiple CPUs simultaneously we 3629 * achieve that goal here. 3630 */ 3631 if (desired_pcc) { 3632 ret = cpc_write(cpu, desired_reg, perf_ctrls->desired_perf); 3633 if (ret) 3634 goto out_pcc_read_unlock; 3635 } 3636 3637 if (min_pcc) { 3638 ret = cpc_write(cpu, min_perf_reg, perf_ctrls->min_perf); 3639 if (ret) 3640 goto out_pcc_read_unlock; 3641 } 3642 if (max_pcc) { 3643 ret = cpc_write(cpu, max_perf_reg, perf_ctrls->max_perf); 3644 if (ret) 3645 goto out_pcc_read_unlock; 3646 } 3647 3648 /* Block a PCC read until the staged payload has been submitted. */ 3649 WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true); 3650 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; 3651 cpc_desc->write_cmd_status = 0; 3652 up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */ 3653 /* 3654 * This is Phase-II where we transfer the ownership of PCC to Platform 3655 * 3656 * Short Summary: Basically if we think of a group of cppc_set_perf 3657 * requests that happened in short overlapping interval. The last CPU to 3658 * come out of Phase-I will enter Phase-II and ring the doorbell. 3659 * 3660 * We have the following requirements for Phase-II: 3661 * 1. We want to execute Phase-II only when there are no CPUs 3662 * currently executing in Phase-I 3663 * 2. Once we start Phase-II we want to avoid all other CPUs from 3664 * entering Phase-I. 3665 * 3. We want only one CPU among all those who went through Phase-I 3666 * to run phase-II 3667 * 3668 * If write_trylock fails to get the lock and doesn't transfer the 3669 * PCC ownership to the platform, then one of the following will be TRUE 3670 * 1. There is at-least one CPU in Phase-I which will later execute 3671 * write_trylock, so the CPUs in Phase-I will be responsible for 3672 * executing the Phase-II. 3673 * 2. Some other CPU has beaten this CPU to successfully execute the 3674 * write_trylock and has already acquired the write_lock. We know for a 3675 * fact it (other CPU acquiring the write_lock) couldn't have happened 3676 * before this CPU's Phase-I as we held the read_lock. 3677 * 3. Some other CPU executing pcc CMD_READ has stolen the 3678 * down_write, in which case, send_pcc_cmd will check for pending 3679 * CMD_WRITE commands by checking the pending_pcc_write_cmd. 3680 * So this CPU can be certain that its request will be delivered 3681 * So in all cases, this CPU knows that its request will be delivered 3682 * by another CPU and can return 3683 * 3684 * After getting the down_write we still need to check for 3685 * pending_pcc_write_cmd to take care of the following scenario 3686 * The thread running this code could be scheduled out between 3687 * Phase-I and Phase-II. Before it is scheduled back on, another CPU 3688 * could have delivered the request to Platform by triggering the 3689 * doorbell and transferred the ownership of PCC to platform. So this 3690 * avoids triggering an unnecessary doorbell and more importantly before 3691 * triggering the doorbell it makes sure that the PCC channel ownership 3692 * is still with OSPM. 3693 * pending_pcc_write_cmd can also be cleared by a different CPU, if 3694 * there was a pcc CMD_READ waiting on down_write and it steals the lock 3695 * before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this 3696 * case during a CMD_READ and if there are pending writes it delivers 3697 * the write command before servicing the read command 3698 */ 3699 if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */ 3700 /* Update only if there are pending write commands */ 3701 if (pcc_ss_data->pending_pcc_write_cmd) 3702 send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3703 up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */ 3704 } else { 3705 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */ 3706 wait_event(pcc_ss_data->pcc_write_wait_q, 3707 cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt); 3708 } 3709 3710 /* send_pcc_cmd updates the status in case of failure */ 3711 return cpc_desc->write_cmd_status; 3712 3713 out_pcc_read_unlock: 3714 up_read(&pcc_ss_data->pcc_lock); 3715 return ret; 3716 } 3717 EXPORT_SYMBOL_GPL(cppc_set_perf); 3718 3719 /** 3720 * cppc_get_perf_limited - Get the Performance Limited register value. 3721 * @cpu: CPU from which to get Performance Limited register. 3722 * @perf_limited: Pointer to store the Performance Limited value. 3723 * 3724 * The returned value contains sticky status bits indicating platform-imposed 3725 * performance limitations. 3726 * 3727 * Return: 0 for success, -EIO on failure, -EOPNOTSUPP if not supported. 3728 */ 3729 int cppc_get_perf_limited(int cpu, u64 *perf_limited) 3730 { 3731 return cppc_get_reg_val(cpu, PERF_LIMITED, perf_limited); 3732 } 3733 EXPORT_SYMBOL_GPL(cppc_get_perf_limited); 3734 3735 /** 3736 * cppc_set_perf_limited() - Clear bits in the Performance Limited register. 3737 * @cpu: CPU on which to write register. 3738 * @bits_to_clear: Zero for no-op or CPPC_PERF_LIMITED_MASK to clear both bits. 3739 * 3740 * The Performance Limited register contains two sticky bits set by platform: 3741 * - Bit 0 (Desired_Excursion): Set when delivered performance is constrained 3742 * below desired performance. Not used when Autonomous Selection is enabled. 3743 * - Bit 1 (Minimum_Excursion): Set when delivered performance is constrained 3744 * below minimum performance. 3745 * 3746 * These bits are sticky and remain set until OSPM explicitly clears them. 3747 * Selective clears are unsupported because they require an interlocked RMW. 3748 * 3749 * Return: 0 for success, -EINVAL for invalid bits, -EIO on register 3750 * access failure, -EOPNOTSUPP if not supported. 3751 */ 3752 int cppc_set_perf_limited(int cpu, u64 bits_to_clear) 3753 { 3754 /* Only bits 0 and 1 are valid */ 3755 if (bits_to_clear & ~(u64)CPPC_PERF_LIMITED_MASK) 3756 return -EINVAL; 3757 3758 if (!bits_to_clear) 3759 return 0; 3760 3761 /* 3762 * Writing zero clears both bits without depending on how a platform 3763 * treats written ones. ACPI does not define the effect of writing one, 3764 * so a selective clear cannot be implemented without an interlocked RMW. 3765 */ 3766 if (bits_to_clear != CPPC_PERF_LIMITED_MASK) 3767 return -EOPNOTSUPP; 3768 3769 return cppc_set_reg_val(cpu, PERF_LIMITED, 0); 3770 } 3771 EXPORT_SYMBOL_GPL(cppc_set_perf_limited); 3772 3773 /** 3774 * cppc_get_transition_latency - returns frequency transition latency in ns 3775 * @cpu_num: CPU number for per_cpu(). 3776 * 3777 * ACPI CPPC does not explicitly specify how a platform can specify the 3778 * transition latency for performance change requests. The closest we have 3779 * is the timing information from the PCCT tables which provides the info 3780 * on the number and frequency of PCC commands the platform can handle. 3781 * 3782 * If desired_reg is in the SystemMemory or SystemIo ACPI address space, 3783 * then assume there is no latency. 3784 */ 3785 int cppc_get_transition_latency(int cpu_num) 3786 { 3787 /* 3788 * Expected transition latency is based on the PCCT timing values 3789 * Below are definition from ACPI spec: 3790 * pcc_nominal- Expected latency to process a command, in microseconds 3791 * pcc_mpar - The maximum number of periodic requests that the subspace 3792 * channel can support, reported in commands per minute. 0 3793 * indicates no limitation. 3794 * pcc_mrtt - The minimum amount of time that OSPM must wait after the 3795 * completion of a command before issuing the next command, 3796 * in microseconds. 3797 */ 3798 struct cpc_desc *cpc_desc; 3799 struct cpc_register_resource *desired_reg; 3800 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num); 3801 struct cppc_pcc_data *pcc_ss_data; 3802 int latency_ns = 0; 3803 3804 cpc_desc = per_cpu(cpc_desc_ptr, cpu_num); 3805 if (!cpc_desc) 3806 return -ENODATA; 3807 3808 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 3809 if (!cpc_is_writable(desired_reg)) 3810 return -ENODATA; 3811 3812 if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg)) 3813 return 0; 3814 3815 if (!CPC_IN_PCC(desired_reg) || pcc_ss_id < 0) 3816 return -ENODATA; 3817 3818 pcc_ss_data = pcc_data[pcc_ss_id]; 3819 if (pcc_ss_data->pcc_mpar) 3820 latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar); 3821 3822 latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_nominal * 1000); 3823 latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_mrtt * 1000); 3824 3825 return latency_ns; 3826 } 3827 EXPORT_SYMBOL_GPL(cppc_get_transition_latency); 3828 3829 /* Minimum struct length needed for the DMI processor entry we want */ 3830 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 3831 3832 /* Offset in the DMI processor structure for the max frequency */ 3833 #define DMI_PROCESSOR_MAX_SPEED 0x14 3834 3835 /* Callback function used to retrieve the max frequency from DMI */ 3836 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private) 3837 { 3838 const u8 *dmi_data = (const u8 *)dm; 3839 u16 *mhz = (u16 *)private; 3840 3841 if (dm->type == DMI_ENTRY_PROCESSOR && 3842 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) { 3843 u16 val = (u16)get_unaligned((const u16 *) 3844 (dmi_data + DMI_PROCESSOR_MAX_SPEED)); 3845 *mhz = umax(val, *mhz); 3846 } 3847 } 3848 3849 /* Look up the max frequency in DMI */ 3850 u64 cppc_get_dmi_max_khz(void) 3851 { 3852 u16 mhz = 0; 3853 3854 dmi_walk(cppc_find_dmi_mhz, &mhz); 3855 3856 /* 3857 * Real stupid fallback value, just in case there is no 3858 * actual value set. 3859 */ 3860 mhz = mhz ? mhz : 1; 3861 3862 return KHZ_PER_MHZ * mhz; 3863 } 3864 EXPORT_SYMBOL_GPL(cppc_get_dmi_max_khz); 3865 3866 /* 3867 * If CPPC lowest_freq and nominal_freq registers are exposed then we can 3868 * use them to convert perf to freq and vice versa. The conversion is 3869 * extrapolated as an affine function passing by the 2 points: 3870 * - (Low perf, Low freq) 3871 * - (Nominal perf, Nominal freq) 3872 */ 3873 unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf) 3874 { 3875 s64 retval, offset = 0; 3876 static u64 max_khz; 3877 u64 mul, div; 3878 3879 if (caps->lowest_freq && caps->nominal_freq) { 3880 /* Avoid special case when nominal_freq is equal to lowest_freq */ 3881 if (caps->lowest_freq == caps->nominal_freq) { 3882 mul = caps->nominal_freq; 3883 div = caps->nominal_perf; 3884 } else { 3885 mul = caps->nominal_freq - caps->lowest_freq; 3886 div = caps->nominal_perf - caps->lowest_perf; 3887 } 3888 mul *= KHZ_PER_MHZ; 3889 offset = caps->nominal_freq * KHZ_PER_MHZ - 3890 div64_u64(caps->nominal_perf * mul, div); 3891 } else { 3892 if (!max_khz) 3893 max_khz = cppc_get_dmi_max_khz(); 3894 mul = max_khz; 3895 div = caps->highest_perf; 3896 } 3897 3898 retval = offset + div64_u64(perf * mul, div); 3899 if (retval >= 0) 3900 return retval; 3901 return 0; 3902 } 3903 EXPORT_SYMBOL_GPL(cppc_perf_to_khz); 3904 3905 unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq) 3906 { 3907 s64 retval, offset = 0; 3908 static u64 max_khz; 3909 u64 mul, div; 3910 3911 if (caps->lowest_freq && caps->nominal_freq) { 3912 /* Avoid special case when nominal_freq is equal to lowest_freq */ 3913 if (caps->lowest_freq == caps->nominal_freq) { 3914 mul = caps->nominal_perf; 3915 div = caps->nominal_freq; 3916 } else { 3917 mul = caps->nominal_perf - caps->lowest_perf; 3918 div = caps->nominal_freq - caps->lowest_freq; 3919 } 3920 /* 3921 * We don't need to convert to kHz for computing offset and can 3922 * directly use nominal_freq and lowest_freq as the div64_u64 3923 * will remove the frequency unit. 3924 */ 3925 offset = caps->nominal_perf - 3926 div64_u64(caps->nominal_freq * mul, div); 3927 /* But we need it for computing the perf level. */ 3928 div *= KHZ_PER_MHZ; 3929 } else { 3930 if (!max_khz) 3931 max_khz = cppc_get_dmi_max_khz(); 3932 mul = caps->highest_perf; 3933 div = max_khz; 3934 } 3935 3936 retval = offset + div64_u64(freq * mul, div); 3937 if (retval >= 0) 3938 return retval; 3939 return 0; 3940 } 3941 EXPORT_SYMBOL_GPL(cppc_khz_to_perf); 3942