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 /* EPP interfaces require the full 0-255 preference range. */ 2085 if (i - 2 == ENERGY_PERF && gas_t->bit_width < 8 && 2086 !IS_NULL_REG(gas_t)) { 2087 pr_debug("CPU%d: EPP register width %u is not supported\n", 2088 pr->id, gas_t->bit_width); 2089 ret = -EOPNOTSUPP; 2090 goto out_free; 2091 } 2092 2093 /* 2094 * The PCC Subspace index is encoded inside 2095 * the CPC table entries. The same PCC index 2096 * will be used for all the PCC entries, 2097 * so extract it only once. 2098 */ 2099 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2100 /* These registers have no specified 32-bit upper bound. */ 2101 bool wide_write = i - 2 == PERF_LIMITED || 2102 i - 2 == ENABLE || 2103 i - 2 == AUTO_SEL_ENABLE; 2104 bool write_width_supported = gas_t->bit_width == 8 || 2105 gas_t->bit_width == 16 || 2106 gas_t->bit_width == 32 || 2107 gas_t->bit_width == 64; 2108 bool unsupported; 2109 2110 unsupported = !gas_t->bit_width || 2111 gas_t->bit_width > 64 || 2112 gas_t->bit_offset || 2113 gas_t->bit_width % 8 || 2114 (cpc_reg_is_writable(i - 2) && 2115 (!write_width_supported || 2116 (!wide_write && gas_t->bit_width > 32))); 2117 if (unsupported) { 2118 if (!cpc_retain_pcc_status(cpc_ptr, i - 2)) 2119 unsupported_regs |= BIT(i - 2); 2120 continue; 2121 } 2122 2123 if (pcc_subspace_id < 0) { 2124 pcc_subspace_id = gas_t->access_width; 2125 } else if (pcc_subspace_id != gas_t->access_width) { 2126 pr_debug("Mismatched PCC ids in _CPC for CPU:%d\n", 2127 pr->id); 2128 ret = -EINVAL; 2129 goto out_free; 2130 } 2131 2132 if (!pcc_data_ref) { 2133 err = pcc_data_alloc(pcc_subspace_id); 2134 if (err) { 2135 ret = err; 2136 goto out_free; 2137 } 2138 pcc_data_ref = true; 2139 } 2140 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) { 2141 if (!IS_NULL_REG(gas_t)) { 2142 void __iomem *addr; 2143 size_t access_width; 2144 2145 err = cpc_validate_sysmem_reg(cpc_ptr, gas_t, 2146 i - 2); 2147 if (err) { 2148 unsupported_regs |= BIT(i - 2); 2149 continue; 2150 } 2151 if (!cpc_is_readable(&cpc_ptr->cpc_regs[i - 2]) && 2152 !cpc_is_writable(&cpc_ptr->cpc_regs[i - 2])) 2153 continue; 2154 2155 if (!osc_cpc_flexible_adr_space_confirmed) { 2156 pr_debug("Flexible address space capability not supported\n"); 2157 ret = -EOPNOTSUPP; 2158 if (!cpc_supported_by_cpu()) 2159 goto out_free; 2160 ret = -EINVAL; 2161 } 2162 2163 access_width = cpc_reg_access_width(gas_t); 2164 access_width /= 8; 2165 addr = ioremap(gas_t->address, access_width); 2166 if (!addr) { 2167 ret = -ENOMEM; 2168 goto out_free; 2169 } 2170 cpc_ptr->cpc_regs[i - 2].sys_mem_vaddr = addr; 2171 } 2172 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2173 u64 access_size; 2174 const char *reason = "uses unsupported SystemIO geometry"; 2175 unsigned int access_width; 2176 bool partial = false; 2177 bool unsupported; 2178 2179 access_width = cpc_reg_access_width(gas_t); 2180 unsupported = !IS_ENABLED(CONFIG_HAS_IOPORT); 2181 if (unsupported) 2182 reason = "requires unavailable SystemIO support"; 2183 else 2184 unsupported = access_width != 8 && 2185 access_width != 16 && 2186 access_width != 32; 2187 if (!unsupported) { 2188 access_size = access_width / 8; 2189 unsupported = !gas_t->bit_width || 2190 gas_t->bit_width > access_width || 2191 gas_t->bit_offset >= access_width || 2192 gas_t->bit_width > access_width - 2193 gas_t->bit_offset; 2194 partial = gas_t->bit_offset || 2195 gas_t->bit_width != access_width; 2196 } 2197 if (!unsupported) { 2198 unsupported = (cpc_reg_is_writable(i - 2) && 2199 i - 2 != PERF_LIMITED && 2200 cpc_is_writable(&cpc_ptr->cpc_regs[i - 2]) && 2201 partial) || 2202 !cpc_reg_access_aligned(gas_t, 2203 access_size) || 2204 gas_t->address > 2205 U16_MAX - (access_size - 1); 2206 } 2207 if (unsupported) { 2208 if (cpc_retain_sysio_status(cpc_ptr, i - 2)) 2209 continue; 2210 pr_debug("CPU%d: _CPC register %u %s\n", 2211 pr->id, i - 2, reason); 2212 unsupported_regs |= BIT(i - 2); 2213 continue; 2214 } 2215 if (i - 2 == PERF_LIMITED && partial) { 2216 pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n", 2217 cpc_ptr->cpu_id); 2218 cpc_ptr->cpc_regs[i - 2].cpc_entry.write_unsupported = true; 2219 } 2220 if (!osc_cpc_flexible_adr_space_confirmed) { 2221 pr_debug("Flexible address space capability not supported\n"); 2222 ret = -EOPNOTSUPP; 2223 if (!cpc_supported_by_cpu()) 2224 goto out_free; 2225 ret = -EINVAL; 2226 } 2227 } else { 2228 if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) { 2229 /* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */ 2230 pr_debug("Unsupported register type (%d) in _CPC\n", 2231 gas_t->space_id); 2232 ret = -EOPNOTSUPP; 2233 goto out_free; 2234 } 2235 } 2236 } else if (cpc_obj->type == ACPI_TYPE_PACKAGE && (i - 2) == RESOURCE_PRIORITY) { 2237 /* 2238 * ACPI 6.6, s8.4.6.1.2.7 defines Resource Priority as a 2239 * Package of Resource Priority Register Descriptor sub-packages. 2240 * Parsing the full structure is not yet supported. 2241 * Mark the register as unsupported for now. 2242 */ 2243 pr_debug("CPU:%d Resource Priority not supported\n", pr->id); 2244 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER; 2245 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = 0; 2246 } else { 2247 pr_debug("Invalid entry type (%d) in _CPC for CPU:%d\n", 2248 i, pr->id); 2249 goto out_free; 2250 } 2251 } 2252 2253 per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_data_ref ? 2254 pcc_subspace_id : -1; 2255 2256 ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs); 2257 if (ret) 2258 goto out_free; 2259 unsupported_regs = 0; 2260 2261 ret = cpc_validate_required_controls(cpc_ptr); 2262 if (ret) 2263 goto out_free; 2264 2265 /* 2266 * Initialize the remaining cpc_regs as unsupported. 2267 * Example: In case FW exposes CPPC v2, the below loop will initialize 2268 * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported 2269 */ 2270 for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) { 2271 cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER; 2272 cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0; 2273 } 2274 2275 2276 cpc_mark_rmw_lock_users(cpc_ptr); 2277 raw_spin_lock_init(&cpc_ptr->rmw_lock); 2278 2279 /* Parse PSD data for this CPU */ 2280 ret = acpi_get_psd(cpc_ptr, handle); 2281 if (ret) 2282 goto out_free; 2283 2284 ret = cpc_register_sysmem_desc(cpc_ptr); 2285 if (ret) 2286 goto out_free; 2287 2288 /* Register PCC channel once for all PCC subspace ID. */ 2289 if (pcc_data_ref) { 2290 ret = register_pcc_channel(pcc_subspace_id); 2291 if (ret) { 2292 pr_err("Failed to find PCC channel for subspace %d\n", 2293 pcc_subspace_id); 2294 goto out_free; 2295 } 2296 2297 cpc_validate_pcc_bounds(cpc_ptr, pcc_subspace_id, 2298 pcc_data[pcc_subspace_id], 2299 &unsupported_regs); 2300 2301 ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs); 2302 if (ret) 2303 goto out_free; 2304 2305 /* A range-only status entry needs the channel only for bounds. */ 2306 if (!cpc_pcc_access_needed(cpc_ptr)) { 2307 pcc_data_put(pcc_subspace_id); 2308 pcc_data_ref = false; 2309 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2310 } 2311 } 2312 2313 ret = cpc_validate_bound_controls(cpc_ptr); 2314 if (ret) 2315 goto out_free; 2316 2317 ret = cpc_register_non_mmio_desc(cpc_ptr); 2318 if (ret) 2319 goto out_free; 2320 2321 /* Everything looks okay */ 2322 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id); 2323 2324 /* Add per logical CPU nodes for reading its feedback counters. */ 2325 cpu_dev = get_cpu_device(pr->id); 2326 if (!cpu_dev) { 2327 ret = -EINVAL; 2328 goto out_free; 2329 } 2330 2331 /* Plug PSD data into this CPU's CPC descriptor. */ 2332 cpc_set_desc(pr->id, cpc_ptr); 2333 2334 ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj, 2335 "acpi_cppc"); 2336 if (ret) { 2337 cpc_set_desc(pr->id, NULL); 2338 cpc_unregister_non_mmio_desc(cpc_ptr); 2339 cpc_unregister_sysmem_desc(cpc_ptr); 2340 kobject_put(&cpc_ptr->kobj); 2341 goto out_pcc_put; 2342 } 2343 2344 kfree(output.pointer); 2345 return 0; 2346 2347 out_free: 2348 cppc_free_desc(cpc_ptr); 2349 2350 out_pcc_put: 2351 if (pcc_data_ref) 2352 pcc_data_put(pcc_subspace_id); 2353 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2354 2355 out_buf_free: 2356 if (cpc_present) 2357 pr_err("CPU%d: failed to initialize _CPC: %d\n", pr->id, ret); 2358 kfree(output.pointer); 2359 return ret; 2360 } 2361 EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe); 2362 2363 /** 2364 * acpi_cppc_processor_exit - Cleanup CPC structs. 2365 * @pr: Ptr to acpi_processor containing this CPU's logical ID. 2366 * 2367 * Return: Void 2368 */ 2369 void acpi_cppc_processor_exit(struct acpi_processor *pr) 2370 { 2371 struct cpc_desc *cpc_ptr; 2372 int pcc_ss_id; 2373 2374 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id); 2375 if (!cpc_ptr) { 2376 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2377 return; 2378 } 2379 2380 pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id); 2381 cpc_set_desc(pr->id, NULL); 2382 kobject_del(&cpc_ptr->kobj); 2383 cpc_unregister_non_mmio_desc(cpc_ptr); 2384 cpc_unregister_sysmem_desc(cpc_ptr); 2385 2386 pcc_data_put(pcc_ss_id); 2387 per_cpu(cpu_pcc_subspace_idx, pr->id) = -1; 2388 2389 kobject_put(&cpc_ptr->kobj); 2390 } 2391 EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit); 2392 2393 /** 2394 * cpc_read_ffh() - Read FFH register 2395 * @cpunum: CPU number to read 2396 * @reg: cppc register information 2397 * @val: place holder for return value 2398 * 2399 * Read bit_width bits from a specified address and bit_offset 2400 * 2401 * Return: 0 for success and error code 2402 */ 2403 int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val) 2404 { 2405 return -ENOTSUPP; 2406 } 2407 2408 /** 2409 * cpc_read_ffh_fb_ctrs() - Read FFH feedback counters together 2410 * @cpunum: Target CPU 2411 * @reg1: first CPPC register information 2412 * @val1: place holder for first return value 2413 * @reg2: second CPPC register information 2414 * @val2: place holder for second return value 2415 * 2416 * Return: 0 on success, error code otherwise 2417 */ 2418 int __weak cpc_read_ffh_fb_ctrs(int cpunum, struct cpc_reg *reg1, 2419 u64 *val1, struct cpc_reg *reg2, u64 *val2) 2420 { 2421 return -EOPNOTSUPP; 2422 } 2423 2424 /** 2425 * cpc_write_ffh() - Write FFH register 2426 * @cpunum: CPU number to write 2427 * @reg: cppc register information 2428 * @val: value to write 2429 * 2430 * Write value of bit_width bits to a specified address and bit_offset 2431 * 2432 * Return: 0 for success and error code 2433 */ 2434 int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val) 2435 { 2436 return -ENOTSUPP; 2437 } 2438 2439 /* 2440 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be 2441 * as fast as possible. We have already mapped the PCC subspace during init, so 2442 * we can directly write to it. 2443 */ 2444 2445 static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) 2446 { 2447 void __iomem *vaddr = NULL; 2448 unsigned long flags; 2449 u8 buf[sizeof(*val)]; 2450 unsigned int i; 2451 int size; 2452 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2453 struct cpc_reg *reg = ®_res->cpc_entry.reg; 2454 2455 if (!cpc_is_readable(reg_res)) 2456 return -EOPNOTSUPP; 2457 2458 if (reg_res->type == ACPI_TYPE_INTEGER) { 2459 *val = reg_res->cpc_entry.int_value; 2460 return 0; 2461 } 2462 2463 *val = 0; 2464 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 2465 return cpc_read_ffh(cpu, reg, val); 2466 2467 size = GET_BIT_WIDTH(reg); 2468 2469 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2470 u32 val_u32; 2471 acpi_status status; 2472 2473 if (!IS_ENABLED(CONFIG_HAS_IOPORT)) 2474 return -EOPNOTSUPP; 2475 2476 status = acpi_os_read_port((acpi_io_address)reg->address, 2477 &val_u32, size); 2478 if (ACPI_FAILURE(status)) { 2479 pr_debug("Error: Failed to read SystemIO port %llx\n", 2480 reg->address); 2481 return -EFAULT; 2482 } 2483 2484 *val = MASK_VAL_READ(reg, val_u32); 2485 return 0; 2486 } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2487 if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id]) 2488 return -ENODEV; 2489 2490 /* 2491 * For registers in PCC space, the register size is determined 2492 * by the bit width field; the access size is used to indicate 2493 * the PCC subspace id. 2494 */ 2495 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 2496 size = reg->bit_width / 8; 2497 if (!size || size > sizeof(buf) || reg->bit_width % 8) 2498 return -EFAULT; 2499 2500 raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags); 2501 memcpy_fromio(buf, vaddr, size); 2502 raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock, 2503 flags); 2504 2505 *val = 0; 2506 for (i = 0; i < size; i++) 2507 *val |= (u64)buf[i] << (i * 8); 2508 return 0; 2509 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 2510 vaddr = reg_res->sys_mem_vaddr; 2511 else 2512 return acpi_os_read_memory((acpi_physical_address)reg->address, 2513 val, size); 2514 2515 switch (size) { 2516 case 8: 2517 *val = readb_relaxed(vaddr); 2518 break; 2519 case 16: 2520 *val = readw_relaxed(vaddr); 2521 break; 2522 case 32: 2523 *val = readl_relaxed(vaddr); 2524 break; 2525 case 64: 2526 *val = readq_relaxed(vaddr); 2527 break; 2528 default: 2529 pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n", 2530 size, reg->address); 2531 return -EFAULT; 2532 } 2533 2534 *val = MASK_VAL_READ(reg, *val); 2535 2536 return 0; 2537 } 2538 2539 static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val) 2540 { 2541 int ret_val = 0; 2542 int size; 2543 u64 prev_val; 2544 void __iomem *vaddr = NULL; 2545 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2546 struct cpc_reg *reg; 2547 struct cpc_desc *cpc_desc; 2548 unsigned long flags; 2549 u8 buf[sizeof(val)]; 2550 unsigned int i; 2551 bool locked = false; 2552 2553 if (!cpc_is_writable(reg_res)) 2554 return -EOPNOTSUPP; 2555 2556 reg = ®_res->cpc_entry.reg; 2557 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 2558 return cpc_write_ffh(cpu, reg, val); 2559 2560 size = GET_BIT_WIDTH(reg); 2561 2562 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { 2563 acpi_status status; 2564 2565 if (!IS_ENABLED(CONFIG_HAS_IOPORT)) 2566 return -EOPNOTSUPP; 2567 2568 status = acpi_os_write_port((acpi_io_address)reg->address, 2569 (u32)val, size); 2570 if (ACPI_FAILURE(status)) { 2571 pr_debug("Error: Failed to write SystemIO port %llx\n", 2572 reg->address); 2573 return -EFAULT; 2574 } 2575 2576 return 0; 2577 } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 2578 if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id]) 2579 return -ENODEV; 2580 2581 /* 2582 * For registers in PCC space, the register size is determined 2583 * by the bit width field; the access size is used to indicate 2584 * the PCC subspace id. 2585 */ 2586 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 2587 size = reg->bit_width / 8; 2588 if (!size || size > sizeof(buf) || reg->bit_width % 8) 2589 return -EFAULT; 2590 2591 for (i = 0; i < size; i++) 2592 buf[i] = val >> (i * 8); 2593 2594 raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags); 2595 memcpy_toio(vaddr, buf, size); 2596 /* Publish every payload byte before another CPU can ring the doorbell. */ 2597 wmb(); 2598 raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock, 2599 flags); 2600 return 0; 2601 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 2602 vaddr = reg_res->sys_mem_vaddr; 2603 else 2604 return acpi_os_write_memory((acpi_physical_address)reg->address, 2605 val, size); 2606 2607 /* Partial fields and local overlaps use the descriptor lock. */ 2608 locked = reg_res->cpc_entry.use_rmw_lock; 2609 if (locked) { 2610 cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2611 if (!cpc_desc) { 2612 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2613 return -ENODEV; 2614 } 2615 raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags); 2616 } 2617 2618 if (reg->bit_offset || reg->bit_width != size) { 2619 /* 2620 * MASK_VAL_WRITE() discards the field's old bits, so undefined 2621 * readback from a write-only field is not propagated. 2622 */ 2623 switch (size) { 2624 case 8: 2625 prev_val = readb_relaxed(vaddr); 2626 break; 2627 case 16: 2628 prev_val = readw_relaxed(vaddr); 2629 break; 2630 case 32: 2631 prev_val = readl_relaxed(vaddr); 2632 break; 2633 case 64: 2634 prev_val = readq_relaxed(vaddr); 2635 break; 2636 default: 2637 if (locked) 2638 raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, 2639 flags); 2640 return -EFAULT; 2641 } 2642 val = MASK_VAL_WRITE(reg, prev_val, val); 2643 } 2644 2645 switch (size) { 2646 case 8: 2647 writeb_relaxed(val, vaddr); 2648 break; 2649 case 16: 2650 writew_relaxed(val, vaddr); 2651 break; 2652 case 32: 2653 writel_relaxed(val, vaddr); 2654 break; 2655 case 64: 2656 writeq_relaxed(val, vaddr); 2657 break; 2658 default: 2659 pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n", 2660 size, reg->address); 2661 ret_val = -EFAULT; 2662 break; 2663 } 2664 2665 if (locked) { 2666 if (!ret_val) 2667 mmiowb_set_pending(); 2668 raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags); 2669 } 2670 2671 return ret_val; 2672 } 2673 2674 static int cppc_get_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 *val) 2675 { 2676 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2677 struct cppc_pcc_data *pcc_ss_data = NULL; 2678 int ret; 2679 2680 if (pcc_ss_id < 0) { 2681 pr_debug("Invalid pcc_ss_id\n"); 2682 return -ENODEV; 2683 } 2684 2685 pcc_ss_data = pcc_data[pcc_ss_id]; 2686 2687 down_write(&pcc_ss_data->pcc_lock); 2688 2689 if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0) 2690 ret = cpc_read(cpu, reg, val); 2691 else 2692 ret = -EIO; 2693 2694 up_write(&pcc_ss_data->pcc_lock); 2695 2696 return ret; 2697 } 2698 2699 static int cppc_get_reg_val(int cpu, enum cppc_regs reg_idx, u64 *val) 2700 { 2701 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2702 struct cpc_register_resource *reg; 2703 2704 if (val == NULL) 2705 return -EINVAL; 2706 2707 if (!cpc_desc) { 2708 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2709 return -ENODEV; 2710 } 2711 if (cpc_reg_is_write_only(cpc_desc, reg_idx)) 2712 return -EOPNOTSUPP; 2713 2714 reg = &cpc_desc->cpc_regs[reg_idx]; 2715 2716 /* 2717 * Desired and Performance Limited may be disabled despite not being 2718 * generally optional. 2719 */ 2720 if ((reg->type == ACPI_TYPE_INTEGER && 2721 (IS_OPTIONAL_CPC_REG(reg_idx) || reg_idx == DESIRED_PERF || 2722 reg_idx == PERF_LIMITED) && 2723 !reg->cpc_entry.int_value) || (reg->type != ACPI_TYPE_INTEGER && 2724 IS_NULL_REG(®->cpc_entry.reg))) { 2725 pr_debug("CPC register is not supported\n"); 2726 return -EOPNOTSUPP; 2727 } 2728 if (!cpc_is_readable(reg)) 2729 return -EOPNOTSUPP; 2730 2731 if (CPC_IN_PCC(reg)) 2732 return cppc_get_reg_val_in_pcc(cpu, reg, val); 2733 2734 return cpc_read(cpu, reg, val); 2735 } 2736 2737 static int cppc_set_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 val) 2738 { 2739 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 2740 struct cppc_pcc_data *pcc_ss_data; 2741 int ret; 2742 2743 if (pcc_ss_id < 0) { 2744 pr_debug("Invalid pcc_ss_id\n"); 2745 return -ENODEV; 2746 } 2747 pcc_ss_data = pcc_data[pcc_ss_id]; 2748 if (!pcc_ss_data) 2749 return -ENODEV; 2750 2751 down_write(&pcc_ss_data->pcc_lock); 2752 2753 ret = check_pcc_chan(pcc_ss_id, false); 2754 if (ret) 2755 goto out; 2756 2757 ret = cpc_write(cpu, reg, val); 2758 if (ret) 2759 goto out; 2760 2761 /* after writing CPC, transfer the ownership of PCC to platform */ 2762 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 2763 2764 out: 2765 if (ret) 2766 cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret); 2767 up_write(&pcc_ss_data->pcc_lock); 2768 2769 return ret; 2770 } 2771 2772 static int cppc_set_reg_val(int cpu, enum cppc_regs reg_idx, u64 val) 2773 { 2774 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 2775 struct cpc_register_resource *reg; 2776 2777 if (!cpc_desc) { 2778 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 2779 return -ENODEV; 2780 } 2781 2782 reg = &cpc_desc->cpc_regs[reg_idx]; 2783 2784 /* Integer 1 describes autonomous selection that is always enabled. */ 2785 if (reg_idx == AUTO_SEL_ENABLE && reg->type == ACPI_TYPE_INTEGER && 2786 reg->cpc_entry.int_value == 1) 2787 return val == 1 ? 0 : -EOPNOTSUPP; 2788 2789 /* if a register is writeable, it must be a buffer and not null */ 2790 if (!cpc_is_writable(reg)) { 2791 pr_debug("CPC register is not supported\n"); 2792 return -EOPNOTSUPP; 2793 } 2794 2795 if (CPC_IN_PCC(reg)) 2796 return cppc_set_reg_val_in_pcc(cpu, reg, val); 2797 2798 return cpc_write(cpu, reg, val); 2799 } 2800 2801 /** 2802 * cppc_get_desired_perf - Get the desired performance register value. 2803 * @cpunum: CPU from which to get desired performance. 2804 * @desired_perf: Return address. 2805 * 2806 * Return: 0 for success, -EOPNOTSUPP for _CPC revision 4 or later, and a 2807 * negative errno otherwise. 2808 */ 2809 int cppc_get_desired_perf(int cpunum, u64 *desired_perf) 2810 { 2811 return cppc_get_reg_val(cpunum, DESIRED_PERF, desired_perf); 2812 } 2813 EXPORT_SYMBOL_GPL(cppc_get_desired_perf); 2814 2815 /** 2816 * cppc_get_nominal_perf - Get the nominal performance register value. 2817 * @cpunum: CPU from which to get nominal performance. 2818 * @nominal_perf: Return address. 2819 * 2820 * Return: 0 for success, -EIO otherwise. 2821 */ 2822 int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf) 2823 { 2824 return cppc_get_reg_val(cpunum, NOMINAL_PERF, nominal_perf); 2825 } 2826 2827 /** 2828 * cppc_get_highest_perf - Get the highest performance register value. 2829 * @cpunum: CPU from which to get highest performance. 2830 * @highest_perf: Return address. 2831 * 2832 * Return: 0 for success, -EIO otherwise. 2833 */ 2834 int cppc_get_highest_perf(int cpunum, u64 *highest_perf) 2835 { 2836 return cppc_get_reg_val(cpunum, HIGHEST_PERF, highest_perf); 2837 } 2838 EXPORT_SYMBOL_GPL(cppc_get_highest_perf); 2839 2840 /** 2841 * cppc_get_epp_perf - Get the epp register value. 2842 * @cpunum: CPU from which to get epp preference value. 2843 * @epp_perf: Return address. 2844 * 2845 * Return: 0 for success, -EIO otherwise. 2846 */ 2847 int cppc_get_epp_perf(int cpunum, u64 *epp_perf) 2848 { 2849 return cppc_get_reg_val(cpunum, ENERGY_PERF, epp_perf); 2850 } 2851 EXPORT_SYMBOL_GPL(cppc_get_epp_perf); 2852 2853 /** 2854 * cppc_get_perf_caps - Get a CPU's performance capabilities. 2855 * @cpunum: CPU from which to get capabilities info. 2856 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h 2857 * 2858 * Return: 0 for success with perf_caps populated else -ERRNO. 2859 */ 2860 int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps) 2861 { 2862 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 2863 struct cpc_register_resource *highest_reg, *lowest_reg, 2864 *lowest_non_linear_reg, *nominal_reg, *reference_reg, 2865 *guaranteed_reg, *low_freq_reg = NULL, *nom_freq_reg = NULL; 2866 u64 high, low, guaranteed = 0, nom, ref, min_nonlinear, 2867 low_f = 0, nom_f = 0; 2868 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 2869 struct cppc_pcc_data *pcc_ss_data = NULL; 2870 int ret = 0, regs_in_pcc = 0; 2871 2872 if (!cpc_desc) { 2873 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 2874 return -ENODEV; 2875 } 2876 2877 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF]; 2878 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF]; 2879 lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF]; 2880 nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF]; 2881 reference_reg = &cpc_desc->cpc_regs[REFERENCE_PERF]; 2882 low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ]; 2883 nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ]; 2884 guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF]; 2885 2886 /* Are any of the regs PCC ?*/ 2887 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) || 2888 CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) || 2889 (CPC_SUPPORTED(reference_reg) && CPC_IN_PCC(reference_reg)) || 2890 CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg) || 2891 CPC_IN_PCC(guaranteed_reg)) { 2892 if (pcc_ss_id < 0) { 2893 pr_debug("Invalid pcc_ss_id\n"); 2894 return -ENODEV; 2895 } 2896 pcc_ss_data = pcc_data[pcc_ss_id]; 2897 regs_in_pcc = 1; 2898 down_write(&pcc_ss_data->pcc_lock); 2899 /* Ring doorbell once to update PCC subspace */ 2900 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 2901 ret = -EIO; 2902 goto out_err; 2903 } 2904 } 2905 2906 ret = cpc_read(cpunum, highest_reg, &high); 2907 if (ret) 2908 goto out_err; 2909 perf_caps->highest_perf = high; 2910 2911 ret = cpc_read(cpunum, lowest_reg, &low); 2912 if (ret) 2913 goto out_err; 2914 perf_caps->lowest_perf = low; 2915 2916 ret = cpc_read(cpunum, nominal_reg, &nom); 2917 if (ret) 2918 goto out_err; 2919 perf_caps->nominal_perf = nom; 2920 2921 /* 2922 * If reference perf register is not supported then we should 2923 * use the nominal perf value 2924 */ 2925 if (CPC_SUPPORTED(reference_reg)) { 2926 ret = cpc_read(cpunum, reference_reg, &ref); 2927 if (ret) 2928 goto out_err; 2929 } else { 2930 ref = nom; 2931 } 2932 perf_caps->reference_perf = ref; 2933 2934 if (guaranteed_reg->type != ACPI_TYPE_BUFFER || 2935 IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) { 2936 perf_caps->guaranteed_perf = 0; 2937 } else { 2938 ret = cpc_read(cpunum, guaranteed_reg, &guaranteed); 2939 if (ret) 2940 goto out_err; 2941 perf_caps->guaranteed_perf = guaranteed; 2942 } 2943 2944 ret = cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear); 2945 if (ret) 2946 goto out_err; 2947 perf_caps->lowest_nonlinear_perf = min_nonlinear; 2948 2949 if (!high || !low || !nom || !ref || !min_nonlinear || 2950 high > U32_MAX || low > U32_MAX || guaranteed > U32_MAX || 2951 nom > U32_MAX || ref > U32_MAX || min_nonlinear > U32_MAX || 2952 high < min_nonlinear || min_nonlinear < low || 2953 (CPC_SUPPORTED(guaranteed_reg) && 2954 (guaranteed < low || guaranteed > high))) { 2955 ret = -EFAULT; 2956 goto out_err; 2957 } 2958 2959 /* Read optional lowest and nominal frequencies if present */ 2960 if (CPC_SUPPORTED(low_freq_reg)) { 2961 ret = cpc_read(cpunum, low_freq_reg, &low_f); 2962 if (ret) 2963 goto out_err; 2964 } 2965 2966 if (CPC_SUPPORTED(nom_freq_reg)) { 2967 ret = cpc_read(cpunum, nom_freq_reg, &nom_f); 2968 if (ret) 2969 goto out_err; 2970 } 2971 /* Require ordered anchors and a nonzero slope when frequencies differ. */ 2972 if (low_f > U32_MAX || nom_f > U32_MAX || 2973 (low_f && nom_f && 2974 (nom_f < low_f || nom < low || 2975 (nom_f != low_f && nom == low)))) { 2976 ret = -EFAULT; 2977 goto out_err; 2978 } 2979 2980 perf_caps->lowest_freq = low_f; 2981 perf_caps->nominal_freq = nom_f; 2982 2983 2984 out_err: 2985 if (regs_in_pcc) 2986 up_write(&pcc_ss_data->pcc_lock); 2987 return ret; 2988 } 2989 EXPORT_SYMBOL_GPL(cppc_get_perf_caps); 2990 2991 /** 2992 * cppc_perf_ctrs_in_pcc_cpu - Check if any perf counters of a CPU are in PCC. 2993 * @cpu: CPU on which to check perf counters. 2994 * 2995 * Return: true if any of the counters are in PCC regions, false otherwise 2996 */ 2997 bool cppc_perf_ctrs_in_pcc_cpu(unsigned int cpu) 2998 { 2999 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3000 3001 if (!cpc_desc) 3002 return false; 3003 3004 return CPC_IN_PCC(&cpc_desc->cpc_regs[DELIVERED_CTR]) || 3005 CPC_IN_PCC(&cpc_desc->cpc_regs[REFERENCE_CTR]) || 3006 CPC_IN_PCC(&cpc_desc->cpc_regs[CTR_WRAP_TIME]); 3007 } 3008 EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc_cpu); 3009 3010 static int cppc_read_fb_ctrs(int cpunum, 3011 struct cpc_register_resource *delivered_reg, 3012 struct cpc_register_resource *reference_reg, 3013 u64 *delivered, u64 *reference) 3014 { 3015 int ret; 3016 3017 /* 3018 * For FFH feedback counters, try a paired read first to reduce 3019 * sampling skew between delivered and reference counters. Fall 3020 * back to the existing per-register reads if unsupported. 3021 */ 3022 if (CPC_IN_FFH(delivered_reg) && CPC_IN_FFH(reference_reg)) { 3023 ret = cpc_read_ffh_fb_ctrs(cpunum, 3024 &delivered_reg->cpc_entry.reg, delivered, 3025 &reference_reg->cpc_entry.reg, reference); 3026 if (ret != -EOPNOTSUPP) 3027 return ret; 3028 } 3029 3030 ret = cpc_read(cpunum, delivered_reg, delivered); 3031 if (ret) 3032 return ret; 3033 3034 return cpc_read(cpunum, reference_reg, reference); 3035 } 3036 3037 /** 3038 * cppc_perf_ctrs_in_pcc - Check if any perf counters are in a PCC region. 3039 * 3040 * CPPC has flexibility about how CPU performance counters are accessed. 3041 * One of the choices is PCC regions, which can have a high access latency. This 3042 * routine allows callers of cppc_get_perf_ctrs() to know this ahead of time. 3043 * 3044 * Return: true if any of the counters are in PCC regions, false otherwise 3045 */ 3046 bool cppc_perf_ctrs_in_pcc(void) 3047 { 3048 int cpu; 3049 3050 for_each_online_cpu(cpu) { 3051 if (cppc_perf_ctrs_in_pcc_cpu(cpu)) 3052 return true; 3053 } 3054 3055 return false; 3056 } 3057 EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc); 3058 3059 /** 3060 * cppc_get_perf_ctrs - Read a CPU's performance feedback counters. 3061 * @cpunum: CPU from which to read counters. 3062 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h 3063 * 3064 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO. 3065 */ 3066 int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs) 3067 { 3068 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 3069 struct cpc_register_resource *delivered_reg, *reference_reg, 3070 *ctr_wrap_reg; 3071 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 3072 struct cppc_pcc_data *pcc_ss_data = NULL; 3073 u64 delivered, reference, ctr_wrap_time; 3074 int ret = 0, regs_in_pcc = 0; 3075 3076 if (!cpc_desc) { 3077 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 3078 return -ENODEV; 3079 } 3080 3081 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR]; 3082 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR]; 3083 ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME]; 3084 3085 /* Are any of the regs PCC ?*/ 3086 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) || 3087 CPC_IN_PCC(ctr_wrap_reg)) { 3088 if (pcc_ss_id < 0) { 3089 pr_debug("Invalid pcc_ss_id\n"); 3090 return -ENODEV; 3091 } 3092 pcc_ss_data = pcc_data[pcc_ss_id]; 3093 down_write(&pcc_ss_data->pcc_lock); 3094 regs_in_pcc = 1; 3095 /* Ring doorbell once to update PCC subspace */ 3096 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 3097 ret = -EIO; 3098 goto out_err; 3099 } 3100 } 3101 3102 ret = cppc_read_fb_ctrs(cpunum, delivered_reg, reference_reg, 3103 &delivered, &reference); 3104 if (ret) 3105 goto out_err; 3106 3107 /* 3108 * Per spec, if ctr_wrap_time optional register is unsupported, then the 3109 * performance counters are assumed to never wrap during the lifetime of 3110 * platform 3111 */ 3112 ctr_wrap_time = (u64)(~((u64)0)); 3113 if (CPC_SUPPORTED(ctr_wrap_reg)) { 3114 ret = cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time); 3115 if (ret) 3116 goto out_err; 3117 } 3118 3119 if (!delivered || !reference) { 3120 ret = -EFAULT; 3121 goto out_err; 3122 } 3123 3124 perf_fb_ctrs->delivered = delivered; 3125 perf_fb_ctrs->reference = reference; 3126 perf_fb_ctrs->wraparound_time = ctr_wrap_time; 3127 out_err: 3128 if (regs_in_pcc) 3129 up_write(&pcc_ss_data->pcc_lock); 3130 return ret; 3131 } 3132 EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs); 3133 3134 /* 3135 * Set Energy Performance Preference Register value through 3136 * Performance Controls Interface 3137 */ 3138 int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable) 3139 { 3140 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3141 struct cpc_register_resource *epp_set_reg; 3142 struct cpc_register_resource *auto_sel_reg; 3143 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3144 struct cppc_pcc_data *pcc_ss_data = NULL; 3145 bool auto_sel_pcc; 3146 bool auto_sel_non_pcc; 3147 bool epp_pcc; 3148 bool epp_non_pcc; 3149 int ret; 3150 3151 if (!cpc_desc) { 3152 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3153 return -ENODEV; 3154 } 3155 3156 auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3157 epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF]; 3158 if (!enable && auto_sel_reg->type == ACPI_TYPE_INTEGER && 3159 auto_sel_reg->cpc_entry.int_value == 1) 3160 return -EOPNOTSUPP; 3161 3162 auto_sel_pcc = cpc_is_writable(auto_sel_reg) && 3163 CPC_IN_PCC(auto_sel_reg); 3164 epp_pcc = cpc_is_writable(epp_set_reg) && CPC_IN_PCC(epp_set_reg); 3165 3166 auto_sel_non_pcc = cpc_is_writable(auto_sel_reg) && !auto_sel_pcc; 3167 epp_non_pcc = cpc_is_writable(epp_set_reg) && !epp_pcc; 3168 3169 /* Complete fallible non-PCC writes before staging PCC data. */ 3170 if (auto_sel_non_pcc) { 3171 ret = cpc_write(cpu, auto_sel_reg, enable); 3172 if (ret) 3173 return ret; 3174 } 3175 if (epp_non_pcc) { 3176 ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf); 3177 if (ret) 3178 return ret; 3179 } 3180 3181 if (epp_pcc || auto_sel_pcc) { 3182 if (pcc_ss_id < 0) { 3183 pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu); 3184 return -ENODEV; 3185 } 3186 3187 pcc_ss_data = pcc_data[pcc_ss_id]; 3188 if (!pcc_ss_data) 3189 return -ENODEV; 3190 3191 down_write(&pcc_ss_data->pcc_lock); 3192 3193 ret = check_pcc_chan(pcc_ss_id, false); 3194 if (ret) 3195 goto out_unlock; 3196 3197 if (auto_sel_pcc) { 3198 ret = cpc_write(cpu, auto_sel_reg, enable); 3199 if (ret) 3200 goto out_unlock; 3201 } 3202 3203 if (epp_pcc) { 3204 ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf); 3205 if (ret) 3206 goto out_unlock; 3207 } 3208 3209 /* after writing CPC, transfer the ownership of PCC to platform */ 3210 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3211 3212 out_unlock: 3213 if (ret) 3214 cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret); 3215 up_write(&pcc_ss_data->pcc_lock); 3216 } else if (epp_non_pcc || auto_sel_non_pcc) { 3217 ret = 0; 3218 } else { 3219 ret = -EOPNOTSUPP; 3220 pr_debug("No writable EPP controls for CPU:%d\n", cpu); 3221 } 3222 3223 return ret; 3224 } 3225 EXPORT_SYMBOL_GPL(cppc_set_epp_perf); 3226 3227 /** 3228 * cppc_set_epp() - Write the EPP register. 3229 * @cpu: CPU on which to write register. 3230 * @epp_val: Value to write to the EPP register. 3231 */ 3232 int cppc_set_epp(int cpu, u64 epp_val) 3233 { 3234 if (epp_val > CPPC_EPP_ENERGY_EFFICIENCY_PREF) 3235 return -EINVAL; 3236 3237 return cppc_set_reg_val(cpu, ENERGY_PERF, epp_val); 3238 } 3239 EXPORT_SYMBOL_GPL(cppc_set_epp); 3240 3241 /** 3242 * cppc_get_auto_act_window() - Read autonomous activity window register. 3243 * @cpu: CPU from which to read register. 3244 * @auto_act_window: Return address. 3245 * 3246 * According to ACPI 6.5, s8.4.6.1.6, the value read from the autonomous 3247 * activity window register consists of two parts: a 7 bits value indicate 3248 * significand and a 3 bits value indicate exponent. 3249 */ 3250 int cppc_get_auto_act_window(int cpu, u64 *auto_act_window) 3251 { 3252 unsigned int exp; 3253 u64 val, sig; 3254 int ret; 3255 3256 if (auto_act_window == NULL) 3257 return -EINVAL; 3258 3259 ret = cppc_get_reg_val(cpu, AUTO_ACT_WINDOW, &val); 3260 if (ret) 3261 return ret; 3262 3263 sig = val & CPPC_AUTO_ACT_WINDOW_MAX_SIG; 3264 exp = (val >> CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) & CPPC_AUTO_ACT_WINDOW_MAX_EXP; 3265 *auto_act_window = sig * int_pow(10, exp); 3266 3267 return 0; 3268 } 3269 EXPORT_SYMBOL_GPL(cppc_get_auto_act_window); 3270 3271 /** 3272 * cppc_set_auto_act_window() - Write autonomous activity window register. 3273 * @cpu: CPU on which to write register. 3274 * @auto_act_window: usec value to write to the autonomous activity window register. 3275 * 3276 * According to ACPI 6.5, s8.4.6.1.6, the value to write to the autonomous 3277 * activity window register consists of two parts: a 7 bits value indicate 3278 * significand and a 3 bits value indicate exponent. 3279 */ 3280 int cppc_set_auto_act_window(int cpu, u64 auto_act_window) 3281 { 3282 /* The max value to store is 1270000000 */ 3283 u64 max_val = CPPC_AUTO_ACT_WINDOW_MAX_SIG * int_pow(10, CPPC_AUTO_ACT_WINDOW_MAX_EXP); 3284 int exp = 0; 3285 u64 val; 3286 3287 if (auto_act_window > max_val) 3288 return -EINVAL; 3289 3290 /* 3291 * The max significand is 127, when auto_act_window is larger than 3292 * 129, discard the precision of the last digit and increase the 3293 * exponent by 1. 3294 */ 3295 while (auto_act_window > CPPC_AUTO_ACT_WINDOW_SIG_CARRY_THRESH) { 3296 auto_act_window /= 10; 3297 exp += 1; 3298 } 3299 3300 /* For 128 and 129, cut it to 127. */ 3301 if (auto_act_window > CPPC_AUTO_ACT_WINDOW_MAX_SIG) 3302 auto_act_window = CPPC_AUTO_ACT_WINDOW_MAX_SIG; 3303 3304 val = (exp << CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) + auto_act_window; 3305 3306 return cppc_set_reg_val(cpu, AUTO_ACT_WINDOW, val); 3307 } 3308 EXPORT_SYMBOL_GPL(cppc_set_auto_act_window); 3309 3310 /** 3311 * cppc_get_auto_sel() - Read autonomous selection register. 3312 * @cpu: CPU from which to read register. 3313 * @enable: Return address. 3314 */ 3315 int cppc_get_auto_sel(int cpu, bool *enable) 3316 { 3317 u64 auto_sel; 3318 int ret; 3319 3320 if (enable == NULL) 3321 return -EINVAL; 3322 3323 ret = cppc_get_reg_val(cpu, AUTO_SEL_ENABLE, &auto_sel); 3324 if (ret) 3325 return ret; 3326 3327 *enable = (bool)auto_sel; 3328 3329 return 0; 3330 } 3331 EXPORT_SYMBOL_GPL(cppc_get_auto_sel); 3332 3333 /** 3334 * cppc_auto_sel_is_immutable - Check for always-enabled autonomous selection. 3335 * @cpu: CPU whose _CPC descriptor to check. 3336 * 3337 * Context: Process context. 3338 * Return: true for Integer 1, false for a register or an absent descriptor. 3339 */ 3340 bool cppc_auto_sel_is_immutable(int cpu) 3341 { 3342 struct cpc_desc *cpc_desc; 3343 struct cpc_register_resource *reg; 3344 3345 guard(mutex)(&cpc_desc_lock); 3346 cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3347 if (!cpc_desc) 3348 return false; 3349 3350 reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3351 return reg->type == ACPI_TYPE_INTEGER && reg->cpc_entry.int_value == 1; 3352 } 3353 EXPORT_SYMBOL_GPL(cppc_auto_sel_is_immutable); 3354 3355 /** 3356 * cppc_set_auto_sel - Write autonomous selection register. 3357 * @cpu : CPU to which to write register. 3358 * @enable : the desired value of autonomous selection resiter to be updated. 3359 */ 3360 int cppc_set_auto_sel(int cpu, bool enable) 3361 { 3362 return cppc_set_reg_val(cpu, AUTO_SEL_ENABLE, enable); 3363 } 3364 EXPORT_SYMBOL_GPL(cppc_set_auto_sel); 3365 3366 /** 3367 * cppc_set_enable - Set to enable CPPC on the processor by writing the 3368 * Continuous Performance Control package EnableRegister field. 3369 * @cpu: CPU for which to enable CPPC register. 3370 * @enable: 0 - disable, 1 - enable CPPC feature on the processor. 3371 * 3372 * Return: 0 for success, -ERRNO or -EIO otherwise. 3373 */ 3374 int cppc_set_enable(int cpu, bool enable) 3375 { 3376 return cppc_set_reg_val(cpu, ENABLE, enable); 3377 } 3378 EXPORT_SYMBOL_GPL(cppc_set_enable); 3379 3380 /** 3381 * cppc_get_perf - Get a CPU's performance controls. 3382 * @cpu: CPU for which to get performance controls. 3383 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h 3384 * 3385 * Desired Performance is not read and is returned as 0. 3386 * 3387 * Return: 0 for success with perf_ctrls, -ERRNO otherwise. 3388 */ 3389 int cppc_get_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) 3390 { 3391 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3392 struct cpc_register_resource *min_perf_reg, *max_perf_reg, 3393 *energy_perf_reg, *auto_sel_reg; 3394 u64 min = 0, max = 0, energy_perf = 0, auto_sel = 0; 3395 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3396 struct cppc_pcc_data *pcc_ss_data = NULL; 3397 int ret = 0, regs_in_pcc = 0; 3398 3399 if (!cpc_desc) { 3400 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3401 return -ENODEV; 3402 } 3403 3404 if (!perf_ctrls) { 3405 pr_debug("Invalid perf_ctrls pointer\n"); 3406 return -EINVAL; 3407 } 3408 3409 min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF]; 3410 max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF]; 3411 energy_perf_reg = &cpc_desc->cpc_regs[ENERGY_PERF]; 3412 auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE]; 3413 perf_ctrls->min_perf_valid = false; 3414 3415 /* Are any of the regs PCC ?*/ 3416 if (CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg) || 3417 CPC_IN_PCC(energy_perf_reg) || 3418 CPC_IN_PCC(auto_sel_reg)) { 3419 if (pcc_ss_id < 0) { 3420 pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu); 3421 return -ENODEV; 3422 } 3423 pcc_ss_data = pcc_data[pcc_ss_id]; 3424 regs_in_pcc = 1; 3425 down_write(&pcc_ss_data->pcc_lock); 3426 /* Ring doorbell once to update PCC subspace */ 3427 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 3428 ret = -EIO; 3429 goto out_err; 3430 } 3431 } 3432 3433 /* Read optional elements if present */ 3434 if (CPC_SUPPORTED(max_perf_reg)) { 3435 ret = cpc_read(cpu, max_perf_reg, &max); 3436 if (ret) 3437 goto out_err; 3438 if (max > U32_MAX) { 3439 ret = -EFAULT; 3440 goto out_err; 3441 } 3442 } 3443 perf_ctrls->max_perf = max; 3444 3445 if (CPC_SUPPORTED(min_perf_reg)) { 3446 ret = cpc_read(cpu, min_perf_reg, &min); 3447 if (ret) 3448 goto out_err; 3449 if (min > U32_MAX) { 3450 ret = -EFAULT; 3451 goto out_err; 3452 } 3453 perf_ctrls->min_perf_valid = true; 3454 } 3455 perf_ctrls->min_perf = min; 3456 3457 perf_ctrls->desired_perf = 0; 3458 3459 if (CPC_SUPPORTED(energy_perf_reg)) { 3460 ret = cpc_read(cpu, energy_perf_reg, &energy_perf); 3461 if (ret) 3462 goto out_err; 3463 } 3464 perf_ctrls->energy_perf = energy_perf; 3465 3466 if (CPC_SUPPORTED(auto_sel_reg)) { 3467 ret = cpc_read(cpu, auto_sel_reg, &auto_sel); 3468 if (ret) 3469 goto out_err; 3470 } 3471 perf_ctrls->auto_sel = (bool)auto_sel; 3472 3473 out_err: 3474 if (regs_in_pcc) 3475 up_write(&pcc_ss_data->pcc_lock); 3476 return ret; 3477 } 3478 EXPORT_SYMBOL_GPL(cppc_get_perf); 3479 3480 /** 3481 * cppc_set_perf - Set a CPU's performance controls. 3482 * @cpu: CPU for which to set performance controls. 3483 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h 3484 * 3485 * Return: 0 for success, -ERRNO otherwise. 3486 */ 3487 int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) 3488 { 3489 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 3490 struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg; 3491 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 3492 struct cppc_pcc_data *pcc_ss_data = NULL; 3493 bool desired_update, min_update, max_update; 3494 bool desired_pcc, min_pcc, max_pcc, pcc_update; 3495 bool pcc_layout, direct_layout, mixed_layout; 3496 int ret = 0; 3497 3498 if (!cpc_desc) { 3499 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 3500 return -ENODEV; 3501 } 3502 3503 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 3504 min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF]; 3505 max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF]; 3506 desired_update = cpc_is_writable(desired_reg); 3507 min_update = cpc_is_writable(min_perf_reg) && 3508 (perf_ctrls->min_perf || perf_ctrls->min_perf_valid); 3509 max_update = cpc_is_writable(max_perf_reg) && 3510 perf_ctrls->max_perf; 3511 desired_pcc = desired_update && CPC_IN_PCC(desired_reg); 3512 min_pcc = min_update && CPC_IN_PCC(min_perf_reg); 3513 max_pcc = max_update && CPC_IN_PCC(max_perf_reg); 3514 pcc_update = desired_pcc || min_pcc || max_pcc; 3515 pcc_layout = (cpc_is_writable(desired_reg) && CPC_IN_PCC(desired_reg)) || 3516 (cpc_is_writable(min_perf_reg) && CPC_IN_PCC(min_perf_reg)) || 3517 (cpc_is_writable(max_perf_reg) && CPC_IN_PCC(max_perf_reg)); 3518 direct_layout = (cpc_is_writable(desired_reg) && 3519 !CPC_IN_PCC(desired_reg)) || 3520 (cpc_is_writable(min_perf_reg) && 3521 !CPC_IN_PCC(min_perf_reg)) || 3522 (cpc_is_writable(max_perf_reg) && 3523 !CPC_IN_PCC(max_perf_reg)); 3524 mixed_layout = pcc_layout && direct_layout; 3525 3526 if (mixed_layout || pcc_update) { 3527 if (pcc_ss_id < 0) { 3528 pr_debug("Invalid pcc_ss_id\n"); 3529 return -ENODEV; 3530 } 3531 pcc_ss_data = pcc_data[pcc_ss_id]; 3532 if (!pcc_ss_data) 3533 return -ENODEV; 3534 } 3535 3536 /* 3537 * A mixed layout cannot batch fallible direct writes safely: another 3538 * CPU's staged PCC values may no longer match if a direct write fails. 3539 * Serialize the complete mixed transaction and drain an older batch 3540 * before changing a direct control. 3541 */ 3542 if (mixed_layout) { 3543 down_write(&pcc_ss_data->pcc_lock); 3544 if (pcc_ss_data->pending_pcc_write_cmd) { 3545 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3546 if (ret) 3547 goto out_mixed_unlock; 3548 } 3549 3550 if (pcc_ss_data->platform_owns_pcc) { 3551 ret = check_pcc_chan(pcc_ss_id, false); 3552 if (ret) 3553 goto out_mixed_unlock; 3554 } 3555 3556 if (desired_update && !desired_pcc) { 3557 ret = cpc_write(cpu, desired_reg, 3558 perf_ctrls->desired_perf); 3559 if (ret) 3560 goto out_mixed_unlock; 3561 } 3562 if (min_update && !min_pcc) { 3563 ret = cpc_write(cpu, min_perf_reg, 3564 perf_ctrls->min_perf); 3565 if (ret) 3566 goto out_mixed_unlock; 3567 } 3568 if (max_update && !max_pcc) { 3569 ret = cpc_write(cpu, max_perf_reg, 3570 perf_ctrls->max_perf); 3571 if (ret) 3572 goto out_mixed_unlock; 3573 } 3574 3575 if (desired_pcc) { 3576 ret = cpc_write(cpu, desired_reg, 3577 perf_ctrls->desired_perf); 3578 if (ret) 3579 goto out_mixed_unlock; 3580 } 3581 if (min_pcc) { 3582 ret = cpc_write(cpu, min_perf_reg, 3583 perf_ctrls->min_perf); 3584 if (ret) 3585 goto out_mixed_unlock; 3586 } 3587 if (max_pcc) { 3588 ret = cpc_write(cpu, max_perf_reg, 3589 perf_ctrls->max_perf); 3590 if (ret) 3591 goto out_mixed_unlock; 3592 } 3593 3594 if (pcc_update) { 3595 WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true); 3596 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; 3597 cpc_desc->write_cmd_status = 0; 3598 ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3599 } 3600 3601 out_mixed_unlock: 3602 up_write(&pcc_ss_data->pcc_lock); 3603 return ret; 3604 } 3605 3606 /* A request without PCC updates has no payload to coordinate. */ 3607 if (!pcc_update) { 3608 if (desired_update) { 3609 ret = cpc_write(cpu, desired_reg, 3610 perf_ctrls->desired_perf); 3611 if (ret) 3612 return ret; 3613 } 3614 if (min_update) { 3615 ret = cpc_write(cpu, min_perf_reg, 3616 perf_ctrls->min_perf); 3617 if (ret) 3618 return ret; 3619 } 3620 if (max_update) 3621 ret = cpc_write(cpu, max_perf_reg, 3622 perf_ctrls->max_perf); 3623 return ret; 3624 } 3625 3626 down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */ 3627 if (pcc_ss_data->platform_owns_pcc) { 3628 ret = check_pcc_chan(pcc_ss_id, false); 3629 if (ret) 3630 goto out_pcc_read_unlock; 3631 } 3632 3633 /* 3634 * This is Phase-I where we want to write to CPC registers 3635 * -> We want all CPUs to be able to execute this phase in parallel 3636 * 3637 * Since read_lock can be acquired by multiple CPUs simultaneously we 3638 * achieve that goal here. 3639 */ 3640 if (desired_pcc) { 3641 ret = cpc_write(cpu, desired_reg, perf_ctrls->desired_perf); 3642 if (ret) 3643 goto out_pcc_read_unlock; 3644 } 3645 3646 if (min_pcc) { 3647 ret = cpc_write(cpu, min_perf_reg, perf_ctrls->min_perf); 3648 if (ret) 3649 goto out_pcc_read_unlock; 3650 } 3651 if (max_pcc) { 3652 ret = cpc_write(cpu, max_perf_reg, perf_ctrls->max_perf); 3653 if (ret) 3654 goto out_pcc_read_unlock; 3655 } 3656 3657 /* Block a PCC read until the staged payload has been submitted. */ 3658 WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true); 3659 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; 3660 cpc_desc->write_cmd_status = 0; 3661 up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */ 3662 /* 3663 * This is Phase-II where we transfer the ownership of PCC to Platform 3664 * 3665 * Short Summary: Basically if we think of a group of cppc_set_perf 3666 * requests that happened in short overlapping interval. The last CPU to 3667 * come out of Phase-I will enter Phase-II and ring the doorbell. 3668 * 3669 * We have the following requirements for Phase-II: 3670 * 1. We want to execute Phase-II only when there are no CPUs 3671 * currently executing in Phase-I 3672 * 2. Once we start Phase-II we want to avoid all other CPUs from 3673 * entering Phase-I. 3674 * 3. We want only one CPU among all those who went through Phase-I 3675 * to run phase-II 3676 * 3677 * If write_trylock fails to get the lock and doesn't transfer the 3678 * PCC ownership to the platform, then one of the following will be TRUE 3679 * 1. There is at-least one CPU in Phase-I which will later execute 3680 * write_trylock, so the CPUs in Phase-I will be responsible for 3681 * executing the Phase-II. 3682 * 2. Some other CPU has beaten this CPU to successfully execute the 3683 * write_trylock and has already acquired the write_lock. We know for a 3684 * fact it (other CPU acquiring the write_lock) couldn't have happened 3685 * before this CPU's Phase-I as we held the read_lock. 3686 * 3. Some other CPU executing pcc CMD_READ has stolen the 3687 * down_write, in which case, send_pcc_cmd will check for pending 3688 * CMD_WRITE commands by checking the pending_pcc_write_cmd. 3689 * So this CPU can be certain that its request will be delivered 3690 * So in all cases, this CPU knows that its request will be delivered 3691 * by another CPU and can return 3692 * 3693 * After getting the down_write we still need to check for 3694 * pending_pcc_write_cmd to take care of the following scenario 3695 * The thread running this code could be scheduled out between 3696 * Phase-I and Phase-II. Before it is scheduled back on, another CPU 3697 * could have delivered the request to Platform by triggering the 3698 * doorbell and transferred the ownership of PCC to platform. So this 3699 * avoids triggering an unnecessary doorbell and more importantly before 3700 * triggering the doorbell it makes sure that the PCC channel ownership 3701 * is still with OSPM. 3702 * pending_pcc_write_cmd can also be cleared by a different CPU, if 3703 * there was a pcc CMD_READ waiting on down_write and it steals the lock 3704 * before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this 3705 * case during a CMD_READ and if there are pending writes it delivers 3706 * the write command before servicing the read command 3707 */ 3708 if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */ 3709 /* Update only if there are pending write commands */ 3710 if (pcc_ss_data->pending_pcc_write_cmd) 3711 send_pcc_cmd(pcc_ss_id, CMD_WRITE); 3712 up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */ 3713 } else { 3714 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */ 3715 wait_event(pcc_ss_data->pcc_write_wait_q, 3716 cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt); 3717 } 3718 3719 /* send_pcc_cmd updates the status in case of failure */ 3720 return cpc_desc->write_cmd_status; 3721 3722 out_pcc_read_unlock: 3723 up_read(&pcc_ss_data->pcc_lock); 3724 return ret; 3725 } 3726 EXPORT_SYMBOL_GPL(cppc_set_perf); 3727 3728 /** 3729 * cppc_get_perf_limited - Get the Performance Limited register value. 3730 * @cpu: CPU from which to get Performance Limited register. 3731 * @perf_limited: Pointer to store the Performance Limited value. 3732 * 3733 * The returned value contains sticky status bits indicating platform-imposed 3734 * performance limitations. 3735 * 3736 * Return: 0 for success, -EIO on failure, -EOPNOTSUPP if not supported. 3737 */ 3738 int cppc_get_perf_limited(int cpu, u64 *perf_limited) 3739 { 3740 return cppc_get_reg_val(cpu, PERF_LIMITED, perf_limited); 3741 } 3742 EXPORT_SYMBOL_GPL(cppc_get_perf_limited); 3743 3744 /** 3745 * cppc_set_perf_limited() - Clear bits in the Performance Limited register. 3746 * @cpu: CPU on which to write register. 3747 * @bits_to_clear: Zero for no-op or CPPC_PERF_LIMITED_MASK to clear both bits. 3748 * 3749 * The Performance Limited register contains two sticky bits set by platform: 3750 * - Bit 0 (Desired_Excursion): Set when delivered performance is constrained 3751 * below desired performance. Not used when Autonomous Selection is enabled. 3752 * - Bit 1 (Minimum_Excursion): Set when delivered performance is constrained 3753 * below minimum performance. 3754 * 3755 * These bits are sticky and remain set until OSPM explicitly clears them. 3756 * Selective clears are unsupported because they require an interlocked RMW. 3757 * 3758 * Return: 0 for success, -EINVAL for invalid bits, -EIO on register 3759 * access failure, -EOPNOTSUPP if not supported. 3760 */ 3761 int cppc_set_perf_limited(int cpu, u64 bits_to_clear) 3762 { 3763 /* Only bits 0 and 1 are valid */ 3764 if (bits_to_clear & ~(u64)CPPC_PERF_LIMITED_MASK) 3765 return -EINVAL; 3766 3767 if (!bits_to_clear) 3768 return 0; 3769 3770 /* 3771 * Writing zero clears both bits without depending on how a platform 3772 * treats written ones. ACPI does not define the effect of writing one, 3773 * so a selective clear cannot be implemented without an interlocked RMW. 3774 */ 3775 if (bits_to_clear != CPPC_PERF_LIMITED_MASK) 3776 return -EOPNOTSUPP; 3777 3778 return cppc_set_reg_val(cpu, PERF_LIMITED, 0); 3779 } 3780 EXPORT_SYMBOL_GPL(cppc_set_perf_limited); 3781 3782 /** 3783 * cppc_get_transition_latency - returns frequency transition latency in ns 3784 * @cpu_num: CPU number for per_cpu(). 3785 * 3786 * ACPI CPPC does not explicitly specify how a platform can specify the 3787 * transition latency for performance change requests. The closest we have 3788 * is the timing information from the PCCT tables which provides the info 3789 * on the number and frequency of PCC commands the platform can handle. 3790 * 3791 * If desired_reg is in the SystemMemory or SystemIo ACPI address space, 3792 * then assume there is no latency. 3793 */ 3794 int cppc_get_transition_latency(int cpu_num) 3795 { 3796 /* 3797 * Expected transition latency is based on the PCCT timing values 3798 * Below are definition from ACPI spec: 3799 * pcc_nominal- Expected latency to process a command, in microseconds 3800 * pcc_mpar - The maximum number of periodic requests that the subspace 3801 * channel can support, reported in commands per minute. 0 3802 * indicates no limitation. 3803 * pcc_mrtt - The minimum amount of time that OSPM must wait after the 3804 * completion of a command before issuing the next command, 3805 * in microseconds. 3806 */ 3807 struct cpc_desc *cpc_desc; 3808 struct cpc_register_resource *desired_reg; 3809 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num); 3810 struct cppc_pcc_data *pcc_ss_data; 3811 int latency_ns = 0; 3812 3813 cpc_desc = per_cpu(cpc_desc_ptr, cpu_num); 3814 if (!cpc_desc) 3815 return -ENODATA; 3816 3817 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 3818 if (!cpc_is_writable(desired_reg)) 3819 return -ENODATA; 3820 3821 if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg)) 3822 return 0; 3823 3824 if (!CPC_IN_PCC(desired_reg) || pcc_ss_id < 0) 3825 return -ENODATA; 3826 3827 pcc_ss_data = pcc_data[pcc_ss_id]; 3828 if (pcc_ss_data->pcc_mpar) 3829 latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar); 3830 3831 latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_nominal * 1000); 3832 latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_mrtt * 1000); 3833 3834 return latency_ns; 3835 } 3836 EXPORT_SYMBOL_GPL(cppc_get_transition_latency); 3837 3838 /* Minimum struct length needed for the DMI processor entry we want */ 3839 #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 3840 3841 /* Offset in the DMI processor structure for the max frequency */ 3842 #define DMI_PROCESSOR_MAX_SPEED 0x14 3843 3844 /* Callback function used to retrieve the max frequency from DMI */ 3845 static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private) 3846 { 3847 const u8 *dmi_data = (const u8 *)dm; 3848 u16 *mhz = (u16 *)private; 3849 3850 if (dm->type == DMI_ENTRY_PROCESSOR && 3851 dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) { 3852 u16 val = (u16)get_unaligned((const u16 *) 3853 (dmi_data + DMI_PROCESSOR_MAX_SPEED)); 3854 *mhz = umax(val, *mhz); 3855 } 3856 } 3857 3858 /* Look up the max frequency in DMI */ 3859 u64 cppc_get_dmi_max_khz(void) 3860 { 3861 u16 mhz = 0; 3862 3863 dmi_walk(cppc_find_dmi_mhz, &mhz); 3864 3865 /* 3866 * Real stupid fallback value, just in case there is no 3867 * actual value set. 3868 */ 3869 mhz = mhz ? mhz : 1; 3870 3871 return KHZ_PER_MHZ * mhz; 3872 } 3873 EXPORT_SYMBOL_GPL(cppc_get_dmi_max_khz); 3874 3875 /* 3876 * If CPPC lowest_freq and nominal_freq registers are exposed then we can 3877 * use them to convert perf to freq and vice versa. The conversion is 3878 * extrapolated as an affine function passing by the 2 points: 3879 * - (Low perf, Low freq) 3880 * - (Nominal perf, Nominal freq) 3881 */ 3882 unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf) 3883 { 3884 s64 retval, offset = 0; 3885 static u64 max_khz; 3886 u64 mul, div; 3887 3888 if (caps->lowest_freq && caps->nominal_freq) { 3889 /* Avoid special case when nominal_freq is equal to lowest_freq */ 3890 if (caps->lowest_freq == caps->nominal_freq) { 3891 mul = caps->nominal_freq; 3892 div = caps->nominal_perf; 3893 } else { 3894 mul = caps->nominal_freq - caps->lowest_freq; 3895 div = caps->nominal_perf - caps->lowest_perf; 3896 } 3897 mul *= KHZ_PER_MHZ; 3898 offset = caps->nominal_freq * KHZ_PER_MHZ - 3899 div64_u64(caps->nominal_perf * mul, div); 3900 } else { 3901 if (!max_khz) 3902 max_khz = cppc_get_dmi_max_khz(); 3903 mul = max_khz; 3904 div = caps->highest_perf; 3905 } 3906 3907 retval = offset + div64_u64(perf * mul, div); 3908 if (retval >= 0) 3909 return retval; 3910 return 0; 3911 } 3912 EXPORT_SYMBOL_GPL(cppc_perf_to_khz); 3913 3914 unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq) 3915 { 3916 s64 retval, offset = 0; 3917 static u64 max_khz; 3918 u64 mul, div; 3919 3920 if (caps->lowest_freq && caps->nominal_freq) { 3921 /* Avoid special case when nominal_freq is equal to lowest_freq */ 3922 if (caps->lowest_freq == caps->nominal_freq) { 3923 mul = caps->nominal_perf; 3924 div = caps->nominal_freq; 3925 } else { 3926 mul = caps->nominal_perf - caps->lowest_perf; 3927 div = caps->nominal_freq - caps->lowest_freq; 3928 } 3929 /* 3930 * We don't need to convert to kHz for computing offset and can 3931 * directly use nominal_freq and lowest_freq as the div64_u64 3932 * will remove the frequency unit. 3933 */ 3934 offset = caps->nominal_perf - 3935 div64_u64(caps->nominal_freq * mul, div); 3936 /* But we need it for computing the perf level. */ 3937 div *= KHZ_PER_MHZ; 3938 } else { 3939 if (!max_khz) 3940 max_khz = cppc_get_dmi_max_khz(); 3941 mul = caps->highest_perf; 3942 div = max_khz; 3943 } 3944 3945 retval = offset + div64_u64(freq * mul, div); 3946 if (retval >= 0) 3947 return retval; 3948 return 0; 3949 } 3950 EXPORT_SYMBOL_GPL(cppc_khz_to_perf); 3951