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/iopoll.h> 38 #include <linux/ktime.h> 39 #include <linux/rwsem.h> 40 #include <linux/wait.h> 41 #include <linux/topology.h> 42 43 #include <acpi/cppc_acpi.h> 44 45 struct cppc_pcc_data { 46 struct pcc_mbox_chan *pcc_channel; 47 void __iomem *pcc_comm_addr; 48 bool pcc_channel_acquired; 49 unsigned int deadline_us; 50 unsigned int pcc_mpar, pcc_mrtt, pcc_nominal; 51 52 bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */ 53 bool platform_owns_pcc; /* Ownership of PCC subspace */ 54 unsigned int pcc_write_cnt; /* Running count of PCC write commands */ 55 56 /* 57 * Lock to provide controlled access to the PCC channel. 58 * 59 * For performance critical usecases(currently cppc_set_perf) 60 * We need to take read_lock and check if channel belongs to OSPM 61 * before reading or writing to PCC subspace 62 * We need to take write_lock before transferring the channel 63 * ownership to the platform via a Doorbell 64 * This allows us to batch a number of CPPC requests if they happen 65 * to originate in about the same time 66 * 67 * For non-performance critical usecases(init) 68 * Take write_lock for all purposes which gives exclusive access 69 */ 70 struct rw_semaphore pcc_lock; 71 72 /* Wait queue for CPUs whose requests were batched */ 73 wait_queue_head_t pcc_write_wait_q; 74 ktime_t last_cmd_cmpl_time; 75 ktime_t last_mpar_reset; 76 int mpar_count; 77 int refcount; 78 }; 79 80 /* Array to represent the PCC channel per subspace ID */ 81 static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES]; 82 /* The cpu_pcc_subspace_idx contains per CPU subspace ID */ 83 static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx); 84 85 /* 86 * The cpc_desc structure contains the ACPI register details 87 * as described in the per CPU _CPC tables. The details 88 * include the type of register (e.g. PCC, System IO, FFH etc.) 89 * and destination addresses which lets us READ/WRITE CPU performance 90 * information using the appropriate I/O methods. 91 */ 92 static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr); 93 94 /* pcc mapped address + header size + offset within PCC subspace */ 95 #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_comm_addr + \ 96 0x8 + (offs)) 97 98 /* Check if a CPC register is in PCC */ 99 #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \ 100 (cpc)->cpc_entry.reg.space_id == \ 101 ACPI_ADR_SPACE_PLATFORM_COMM) 102 103 /* Evaluates to True if reg is a NULL register descriptor */ 104 #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \ 105 (reg)->address == 0 && \ 106 (reg)->bit_width == 0 && \ 107 (reg)->bit_offset == 0 && \ 108 (reg)->access_width == 0) 109 110 /* Evaluates to True if an optional cpc field is supported */ 111 #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \ 112 !!(cpc)->cpc_entry.int_value : \ 113 !IS_NULL_REG(&(cpc)->cpc_entry.reg)) 114 /* 115 * Arbitrary Retries in case the remote processor is slow to respond 116 * to PCC commands. Keeping it high enough to cover emulators where 117 * the processors run painfully slow. 118 */ 119 #define NUM_RETRIES 500ULL 120 121 #define define_one_cppc_ro(_name) \ 122 static struct kobj_attribute _name = \ 123 __ATTR(_name, 0444, show_##_name, NULL) 124 125 #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj) 126 127 #define show_cppc_data(access_fn, struct_name, member_name) \ 128 static ssize_t show_##member_name(struct kobject *kobj, \ 129 struct kobj_attribute *attr, char *buf) \ 130 { \ 131 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \ 132 struct struct_name st_name = {0}; \ 133 int ret; \ 134 \ 135 ret = access_fn(cpc_ptr->cpu_id, &st_name); \ 136 if (ret) \ 137 return ret; \ 138 \ 139 return scnprintf(buf, PAGE_SIZE, "%llu\n", \ 140 (u64)st_name.member_name); \ 141 } \ 142 define_one_cppc_ro(member_name) 143 144 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf); 145 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf); 146 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf); 147 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf); 148 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq); 149 show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq); 150 151 show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf); 152 show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time); 153 154 static ssize_t show_feedback_ctrs(struct kobject *kobj, 155 struct kobj_attribute *attr, char *buf) 156 { 157 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); 158 struct cppc_perf_fb_ctrs fb_ctrs = {0}; 159 int ret; 160 161 ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs); 162 if (ret) 163 return ret; 164 165 return scnprintf(buf, PAGE_SIZE, "ref:%llu del:%llu\n", 166 fb_ctrs.reference, fb_ctrs.delivered); 167 } 168 define_one_cppc_ro(feedback_ctrs); 169 170 static struct attribute *cppc_attrs[] = { 171 &feedback_ctrs.attr, 172 &reference_perf.attr, 173 &wraparound_time.attr, 174 &highest_perf.attr, 175 &lowest_perf.attr, 176 &lowest_nonlinear_perf.attr, 177 &nominal_perf.attr, 178 &nominal_freq.attr, 179 &lowest_freq.attr, 180 NULL 181 }; 182 183 static struct kobj_type cppc_ktype = { 184 .sysfs_ops = &kobj_sysfs_ops, 185 .default_attrs = cppc_attrs, 186 }; 187 188 static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit) 189 { 190 int ret, status; 191 struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id]; 192 struct acpi_pcct_shared_memory __iomem *generic_comm_base = 193 pcc_ss_data->pcc_comm_addr; 194 195 if (!pcc_ss_data->platform_owns_pcc) 196 return 0; 197 198 /* 199 * Poll PCC status register every 3us(delay_us) for maximum of 200 * deadline_us(timeout_us) until PCC command complete bit is set(cond) 201 */ 202 ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status, 203 status & PCC_CMD_COMPLETE_MASK, 3, 204 pcc_ss_data->deadline_us); 205 206 if (likely(!ret)) { 207 pcc_ss_data->platform_owns_pcc = false; 208 if (chk_err_bit && (status & PCC_ERROR_MASK)) 209 ret = -EIO; 210 } 211 212 if (unlikely(ret)) 213 pr_err("PCC check channel failed for ss: %d. ret=%d\n", 214 pcc_ss_id, ret); 215 216 return ret; 217 } 218 219 /* 220 * This function transfers the ownership of the PCC to the platform 221 * So it must be called while holding write_lock(pcc_lock) 222 */ 223 static int send_pcc_cmd(int pcc_ss_id, u16 cmd) 224 { 225 int ret = -EIO, i; 226 struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id]; 227 struct acpi_pcct_shared_memory __iomem *generic_comm_base = 228 pcc_ss_data->pcc_comm_addr; 229 unsigned int time_delta; 230 231 /* 232 * For CMD_WRITE we know for a fact the caller should have checked 233 * the channel before writing to PCC space 234 */ 235 if (cmd == CMD_READ) { 236 /* 237 * If there are pending cpc_writes, then we stole the channel 238 * before write completion, so first send a WRITE command to 239 * platform 240 */ 241 if (pcc_ss_data->pending_pcc_write_cmd) 242 send_pcc_cmd(pcc_ss_id, CMD_WRITE); 243 244 ret = check_pcc_chan(pcc_ss_id, false); 245 if (ret) 246 goto end; 247 } else /* CMD_WRITE */ 248 pcc_ss_data->pending_pcc_write_cmd = FALSE; 249 250 /* 251 * Handle the Minimum Request Turnaround Time(MRTT) 252 * "The minimum amount of time that OSPM must wait after the completion 253 * of a command before issuing the next command, in microseconds" 254 */ 255 if (pcc_ss_data->pcc_mrtt) { 256 time_delta = ktime_us_delta(ktime_get(), 257 pcc_ss_data->last_cmd_cmpl_time); 258 if (pcc_ss_data->pcc_mrtt > time_delta) 259 udelay(pcc_ss_data->pcc_mrtt - time_delta); 260 } 261 262 /* 263 * Handle the non-zero Maximum Periodic Access Rate(MPAR) 264 * "The maximum number of periodic requests that the subspace channel can 265 * support, reported in commands per minute. 0 indicates no limitation." 266 * 267 * This parameter should be ideally zero or large enough so that it can 268 * handle maximum number of requests that all the cores in the system can 269 * collectively generate. If it is not, we will follow the spec and just 270 * not send the request to the platform after hitting the MPAR limit in 271 * any 60s window 272 */ 273 if (pcc_ss_data->pcc_mpar) { 274 if (pcc_ss_data->mpar_count == 0) { 275 time_delta = ktime_ms_delta(ktime_get(), 276 pcc_ss_data->last_mpar_reset); 277 if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) { 278 pr_debug("PCC cmd for subspace %d not sent due to MPAR limit", 279 pcc_ss_id); 280 ret = -EIO; 281 goto end; 282 } 283 pcc_ss_data->last_mpar_reset = ktime_get(); 284 pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar; 285 } 286 pcc_ss_data->mpar_count--; 287 } 288 289 /* Write to the shared comm region. */ 290 writew_relaxed(cmd, &generic_comm_base->command); 291 292 /* Flip CMD COMPLETE bit */ 293 writew_relaxed(0, &generic_comm_base->status); 294 295 pcc_ss_data->platform_owns_pcc = true; 296 297 /* Ring doorbell */ 298 ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd); 299 if (ret < 0) { 300 pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n", 301 pcc_ss_id, cmd, ret); 302 goto end; 303 } 304 305 /* wait for completion and check for PCC errro bit */ 306 ret = check_pcc_chan(pcc_ss_id, true); 307 308 if (pcc_ss_data->pcc_mrtt) 309 pcc_ss_data->last_cmd_cmpl_time = ktime_get(); 310 311 if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq) 312 mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret); 313 else 314 mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret); 315 316 end: 317 if (cmd == CMD_WRITE) { 318 if (unlikely(ret)) { 319 for_each_possible_cpu(i) { 320 struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i); 321 322 if (!desc) 323 continue; 324 325 if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt) 326 desc->write_cmd_status = ret; 327 } 328 } 329 pcc_ss_data->pcc_write_cnt++; 330 wake_up_all(&pcc_ss_data->pcc_write_wait_q); 331 } 332 333 return ret; 334 } 335 336 static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret) 337 { 338 if (ret < 0) 339 pr_debug("TX did not complete: CMD sent:%x, ret:%d\n", 340 *(u16 *)msg, ret); 341 else 342 pr_debug("TX completed. CMD sent:%x, ret:%d\n", 343 *(u16 *)msg, ret); 344 } 345 346 static struct mbox_client cppc_mbox_cl = { 347 .tx_done = cppc_chan_tx_done, 348 .knows_txdone = true, 349 }; 350 351 static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle) 352 { 353 int result = -EFAULT; 354 acpi_status status = AE_OK; 355 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 356 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"}; 357 struct acpi_buffer state = {0, NULL}; 358 union acpi_object *psd = NULL; 359 struct acpi_psd_package *pdomain; 360 361 status = acpi_evaluate_object_typed(handle, "_PSD", NULL, 362 &buffer, ACPI_TYPE_PACKAGE); 363 if (status == AE_NOT_FOUND) /* _PSD is optional */ 364 return 0; 365 if (ACPI_FAILURE(status)) 366 return -ENODEV; 367 368 psd = buffer.pointer; 369 if (!psd || psd->package.count != 1) { 370 pr_debug("Invalid _PSD data\n"); 371 goto end; 372 } 373 374 pdomain = &(cpc_ptr->domain_info); 375 376 state.length = sizeof(struct acpi_psd_package); 377 state.pointer = pdomain; 378 379 status = acpi_extract_package(&(psd->package.elements[0]), 380 &format, &state); 381 if (ACPI_FAILURE(status)) { 382 pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id); 383 goto end; 384 } 385 386 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) { 387 pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id); 388 goto end; 389 } 390 391 if (pdomain->revision != ACPI_PSD_REV0_REVISION) { 392 pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id); 393 goto end; 394 } 395 396 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL && 397 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY && 398 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) { 399 pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id); 400 goto end; 401 } 402 403 result = 0; 404 end: 405 kfree(buffer.pointer); 406 return result; 407 } 408 409 bool acpi_cpc_valid(void) 410 { 411 struct cpc_desc *cpc_ptr; 412 int cpu; 413 414 for_each_possible_cpu(cpu) { 415 cpc_ptr = per_cpu(cpc_desc_ptr, cpu); 416 if (!cpc_ptr) 417 return false; 418 } 419 420 return true; 421 } 422 EXPORT_SYMBOL_GPL(acpi_cpc_valid); 423 424 /** 425 * acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu 426 * @cpu: Find all CPUs that share a domain with cpu. 427 * @cpu_data: Pointer to CPU specific CPPC data including PSD info. 428 * 429 * Return: 0 for success or negative value for err. 430 */ 431 int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data) 432 { 433 struct cpc_desc *cpc_ptr, *match_cpc_ptr; 434 struct acpi_psd_package *match_pdomain; 435 struct acpi_psd_package *pdomain; 436 int count_target, i; 437 438 /* 439 * Now that we have _PSD data from all CPUs, let's setup P-state 440 * domain info. 441 */ 442 cpc_ptr = per_cpu(cpc_desc_ptr, cpu); 443 if (!cpc_ptr) 444 return -EFAULT; 445 446 pdomain = &(cpc_ptr->domain_info); 447 cpumask_set_cpu(cpu, cpu_data->shared_cpu_map); 448 if (pdomain->num_processors <= 1) 449 return 0; 450 451 /* Validate the Domain info */ 452 count_target = pdomain->num_processors; 453 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL) 454 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL; 455 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL) 456 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW; 457 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY) 458 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY; 459 460 for_each_possible_cpu(i) { 461 if (i == cpu) 462 continue; 463 464 match_cpc_ptr = per_cpu(cpc_desc_ptr, i); 465 if (!match_cpc_ptr) 466 goto err_fault; 467 468 match_pdomain = &(match_cpc_ptr->domain_info); 469 if (match_pdomain->domain != pdomain->domain) 470 continue; 471 472 /* Here i and cpu are in the same domain */ 473 if (match_pdomain->num_processors != count_target) 474 goto err_fault; 475 476 if (pdomain->coord_type != match_pdomain->coord_type) 477 goto err_fault; 478 479 cpumask_set_cpu(i, cpu_data->shared_cpu_map); 480 } 481 482 return 0; 483 484 err_fault: 485 /* Assume no coordination on any error parsing domain info */ 486 cpumask_clear(cpu_data->shared_cpu_map); 487 cpumask_set_cpu(cpu, cpu_data->shared_cpu_map); 488 cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE; 489 490 return -EFAULT; 491 } 492 EXPORT_SYMBOL_GPL(acpi_get_psd_map); 493 494 static int register_pcc_channel(int pcc_ss_idx) 495 { 496 struct pcc_mbox_chan *pcc_chan; 497 u64 usecs_lat; 498 499 if (pcc_ss_idx >= 0) { 500 pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx); 501 502 if (IS_ERR(pcc_chan)) { 503 pr_err("Failed to find PCC channel for subspace %d\n", 504 pcc_ss_idx); 505 return -ENODEV; 506 } 507 508 pcc_data[pcc_ss_idx]->pcc_channel = pcc_chan; 509 /* 510 * cppc_ss->latency is just a Nominal value. In reality 511 * the remote processor could be much slower to reply. 512 * So add an arbitrary amount of wait on top of Nominal. 513 */ 514 usecs_lat = NUM_RETRIES * pcc_chan->latency; 515 pcc_data[pcc_ss_idx]->deadline_us = usecs_lat; 516 pcc_data[pcc_ss_idx]->pcc_mrtt = pcc_chan->min_turnaround_time; 517 pcc_data[pcc_ss_idx]->pcc_mpar = pcc_chan->max_access_rate; 518 pcc_data[pcc_ss_idx]->pcc_nominal = pcc_chan->latency; 519 520 pcc_data[pcc_ss_idx]->pcc_comm_addr = 521 acpi_os_ioremap(pcc_chan->shmem_base_addr, 522 pcc_chan->shmem_size); 523 if (!pcc_data[pcc_ss_idx]->pcc_comm_addr) { 524 pr_err("Failed to ioremap PCC comm region mem for %d\n", 525 pcc_ss_idx); 526 return -ENOMEM; 527 } 528 529 /* Set flag so that we don't come here for each CPU. */ 530 pcc_data[pcc_ss_idx]->pcc_channel_acquired = true; 531 } 532 533 return 0; 534 } 535 536 /** 537 * cpc_ffh_supported() - check if FFH reading supported 538 * 539 * Check if the architecture has support for functional fixed hardware 540 * read/write capability. 541 * 542 * Return: true for supported, false for not supported 543 */ 544 bool __weak cpc_ffh_supported(void) 545 { 546 return false; 547 } 548 549 /** 550 * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace 551 * 552 * Check and allocate the cppc_pcc_data memory. 553 * In some processor configurations it is possible that same subspace 554 * is shared between multiple CPUs. This is seen especially in CPUs 555 * with hardware multi-threading support. 556 * 557 * Return: 0 for success, errno for failure 558 */ 559 static int pcc_data_alloc(int pcc_ss_id) 560 { 561 if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES) 562 return -EINVAL; 563 564 if (pcc_data[pcc_ss_id]) { 565 pcc_data[pcc_ss_id]->refcount++; 566 } else { 567 pcc_data[pcc_ss_id] = kzalloc(sizeof(struct cppc_pcc_data), 568 GFP_KERNEL); 569 if (!pcc_data[pcc_ss_id]) 570 return -ENOMEM; 571 pcc_data[pcc_ss_id]->refcount++; 572 } 573 574 return 0; 575 } 576 577 /* Check if CPPC revision + num_ent combination is supported */ 578 static bool is_cppc_supported(int revision, int num_ent) 579 { 580 int expected_num_ent; 581 582 switch (revision) { 583 case CPPC_V2_REV: 584 expected_num_ent = CPPC_V2_NUM_ENT; 585 break; 586 case CPPC_V3_REV: 587 expected_num_ent = CPPC_V3_NUM_ENT; 588 break; 589 default: 590 pr_debug("Firmware exports unsupported CPPC revision: %d\n", 591 revision); 592 return false; 593 } 594 595 if (expected_num_ent != num_ent) { 596 pr_debug("Firmware exports %d entries. Expected: %d for CPPC rev:%d\n", 597 num_ent, expected_num_ent, revision); 598 return false; 599 } 600 601 return true; 602 } 603 604 /* 605 * An example CPC table looks like the following. 606 * 607 * Name(_CPC, Package() 608 * { 609 * 17, 610 * NumEntries 611 * 1, 612 * // Revision 613 * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)}, 614 * // Highest Performance 615 * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)}, 616 * // Nominal Performance 617 * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)}, 618 * // Lowest Nonlinear Performance 619 * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)}, 620 * // Lowest Performance 621 * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)}, 622 * // Guaranteed Performance Register 623 * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)}, 624 * // Desired Performance Register 625 * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)}, 626 * .. 627 * .. 628 * .. 629 * 630 * } 631 * Each Register() encodes how to access that specific register. 632 * e.g. a sample PCC entry has the following encoding: 633 * 634 * Register ( 635 * PCC, 636 * AddressSpaceKeyword 637 * 8, 638 * //RegisterBitWidth 639 * 8, 640 * //RegisterBitOffset 641 * 0x30, 642 * //RegisterAddress 643 * 9 644 * //AccessSize (subspace ID) 645 * 0 646 * ) 647 * } 648 */ 649 650 #ifndef init_freq_invariance_cppc 651 static inline void init_freq_invariance_cppc(void) { } 652 #endif 653 654 /** 655 * acpi_cppc_processor_probe - Search for per CPU _CPC objects. 656 * @pr: Ptr to acpi_processor containing this CPU's logical ID. 657 * 658 * Return: 0 for success or negative value for err. 659 */ 660 int acpi_cppc_processor_probe(struct acpi_processor *pr) 661 { 662 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 663 union acpi_object *out_obj, *cpc_obj; 664 struct cpc_desc *cpc_ptr; 665 struct cpc_reg *gas_t; 666 struct device *cpu_dev; 667 acpi_handle handle = pr->handle; 668 unsigned int num_ent, i, cpc_rev; 669 int pcc_subspace_id = -1; 670 acpi_status status; 671 int ret = -EFAULT; 672 673 /* Parse the ACPI _CPC table for this CPU. */ 674 status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output, 675 ACPI_TYPE_PACKAGE); 676 if (ACPI_FAILURE(status)) { 677 ret = -ENODEV; 678 goto out_buf_free; 679 } 680 681 out_obj = (union acpi_object *) output.pointer; 682 683 cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL); 684 if (!cpc_ptr) { 685 ret = -ENOMEM; 686 goto out_buf_free; 687 } 688 689 /* First entry is NumEntries. */ 690 cpc_obj = &out_obj->package.elements[0]; 691 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 692 num_ent = cpc_obj->integer.value; 693 } else { 694 pr_debug("Unexpected entry type(%d) for NumEntries\n", 695 cpc_obj->type); 696 goto out_free; 697 } 698 cpc_ptr->num_entries = num_ent; 699 700 /* Second entry should be revision. */ 701 cpc_obj = &out_obj->package.elements[1]; 702 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 703 cpc_rev = cpc_obj->integer.value; 704 } else { 705 pr_debug("Unexpected entry type(%d) for Revision\n", 706 cpc_obj->type); 707 goto out_free; 708 } 709 cpc_ptr->version = cpc_rev; 710 711 if (!is_cppc_supported(cpc_rev, num_ent)) 712 goto out_free; 713 714 /* Iterate through remaining entries in _CPC */ 715 for (i = 2; i < num_ent; i++) { 716 cpc_obj = &out_obj->package.elements[i]; 717 718 if (cpc_obj->type == ACPI_TYPE_INTEGER) { 719 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER; 720 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value; 721 } else if (cpc_obj->type == ACPI_TYPE_BUFFER) { 722 gas_t = (struct cpc_reg *) 723 cpc_obj->buffer.pointer; 724 725 /* 726 * The PCC Subspace index is encoded inside 727 * the CPC table entries. The same PCC index 728 * will be used for all the PCC entries, 729 * so extract it only once. 730 */ 731 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { 732 if (pcc_subspace_id < 0) { 733 pcc_subspace_id = gas_t->access_width; 734 if (pcc_data_alloc(pcc_subspace_id)) 735 goto out_free; 736 } else if (pcc_subspace_id != gas_t->access_width) { 737 pr_debug("Mismatched PCC ids.\n"); 738 goto out_free; 739 } 740 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) { 741 if (gas_t->address) { 742 void __iomem *addr; 743 744 addr = ioremap(gas_t->address, gas_t->bit_width/8); 745 if (!addr) 746 goto out_free; 747 cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr; 748 } 749 } else { 750 if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) { 751 /* Support only PCC ,SYS MEM and FFH type regs */ 752 pr_debug("Unsupported register type: %d\n", gas_t->space_id); 753 goto out_free; 754 } 755 } 756 757 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER; 758 memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t)); 759 } else { 760 pr_debug("Err in entry:%d in CPC table of CPU:%d\n", i, pr->id); 761 goto out_free; 762 } 763 } 764 per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id; 765 766 /* 767 * Initialize the remaining cpc_regs as unsupported. 768 * Example: In case FW exposes CPPC v2, the below loop will initialize 769 * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported 770 */ 771 for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) { 772 cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER; 773 cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0; 774 } 775 776 777 /* Store CPU Logical ID */ 778 cpc_ptr->cpu_id = pr->id; 779 780 /* Parse PSD data for this CPU */ 781 ret = acpi_get_psd(cpc_ptr, handle); 782 if (ret) 783 goto out_free; 784 785 /* Register PCC channel once for all PCC subspace ID. */ 786 if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) { 787 ret = register_pcc_channel(pcc_subspace_id); 788 if (ret) 789 goto out_free; 790 791 init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock); 792 init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q); 793 } 794 795 /* Everything looks okay */ 796 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id); 797 798 /* Add per logical CPU nodes for reading its feedback counters. */ 799 cpu_dev = get_cpu_device(pr->id); 800 if (!cpu_dev) { 801 ret = -EINVAL; 802 goto out_free; 803 } 804 805 /* Plug PSD data into this CPU's CPC descriptor. */ 806 per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr; 807 808 ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj, 809 "acpi_cppc"); 810 if (ret) { 811 per_cpu(cpc_desc_ptr, pr->id) = NULL; 812 kobject_put(&cpc_ptr->kobj); 813 goto out_free; 814 } 815 816 init_freq_invariance_cppc(); 817 818 kfree(output.pointer); 819 return 0; 820 821 out_free: 822 /* Free all the mapped sys mem areas for this CPU */ 823 for (i = 2; i < cpc_ptr->num_entries; i++) { 824 void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr; 825 826 if (addr) 827 iounmap(addr); 828 } 829 kfree(cpc_ptr); 830 831 out_buf_free: 832 kfree(output.pointer); 833 return ret; 834 } 835 EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe); 836 837 /** 838 * acpi_cppc_processor_exit - Cleanup CPC structs. 839 * @pr: Ptr to acpi_processor containing this CPU's logical ID. 840 * 841 * Return: Void 842 */ 843 void acpi_cppc_processor_exit(struct acpi_processor *pr) 844 { 845 struct cpc_desc *cpc_ptr; 846 unsigned int i; 847 void __iomem *addr; 848 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id); 849 850 if (pcc_ss_id >= 0 && pcc_data[pcc_ss_id]) { 851 if (pcc_data[pcc_ss_id]->pcc_channel_acquired) { 852 pcc_data[pcc_ss_id]->refcount--; 853 if (!pcc_data[pcc_ss_id]->refcount) { 854 pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel); 855 kfree(pcc_data[pcc_ss_id]); 856 pcc_data[pcc_ss_id] = NULL; 857 } 858 } 859 } 860 861 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id); 862 if (!cpc_ptr) 863 return; 864 865 /* Free all the mapped sys mem areas for this CPU */ 866 for (i = 2; i < cpc_ptr->num_entries; i++) { 867 addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr; 868 if (addr) 869 iounmap(addr); 870 } 871 872 kobject_put(&cpc_ptr->kobj); 873 kfree(cpc_ptr); 874 } 875 EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit); 876 877 /** 878 * cpc_read_ffh() - Read FFH register 879 * @cpunum: CPU number to read 880 * @reg: cppc register information 881 * @val: place holder for return value 882 * 883 * Read bit_width bits from a specified address and bit_offset 884 * 885 * Return: 0 for success and error code 886 */ 887 int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val) 888 { 889 return -ENOTSUPP; 890 } 891 892 /** 893 * cpc_write_ffh() - Write FFH register 894 * @cpunum: CPU number to write 895 * @reg: cppc register information 896 * @val: value to write 897 * 898 * Write value of bit_width bits to a specified address and bit_offset 899 * 900 * Return: 0 for success and error code 901 */ 902 int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val) 903 { 904 return -ENOTSUPP; 905 } 906 907 /* 908 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be 909 * as fast as possible. We have already mapped the PCC subspace during init, so 910 * we can directly write to it. 911 */ 912 913 static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) 914 { 915 int ret_val = 0; 916 void __iomem *vaddr = NULL; 917 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 918 struct cpc_reg *reg = ®_res->cpc_entry.reg; 919 920 if (reg_res->type == ACPI_TYPE_INTEGER) { 921 *val = reg_res->cpc_entry.int_value; 922 return ret_val; 923 } 924 925 *val = 0; 926 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) 927 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 928 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 929 vaddr = reg_res->sys_mem_vaddr; 930 else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 931 return cpc_read_ffh(cpu, reg, val); 932 else 933 return acpi_os_read_memory((acpi_physical_address)reg->address, 934 val, reg->bit_width); 935 936 switch (reg->bit_width) { 937 case 8: 938 *val = readb_relaxed(vaddr); 939 break; 940 case 16: 941 *val = readw_relaxed(vaddr); 942 break; 943 case 32: 944 *val = readl_relaxed(vaddr); 945 break; 946 case 64: 947 *val = readq_relaxed(vaddr); 948 break; 949 default: 950 pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n", 951 reg->bit_width, pcc_ss_id); 952 ret_val = -EFAULT; 953 } 954 955 return ret_val; 956 } 957 958 static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val) 959 { 960 int ret_val = 0; 961 void __iomem *vaddr = NULL; 962 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 963 struct cpc_reg *reg = ®_res->cpc_entry.reg; 964 965 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) 966 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id); 967 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) 968 vaddr = reg_res->sys_mem_vaddr; 969 else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) 970 return cpc_write_ffh(cpu, reg, val); 971 else 972 return acpi_os_write_memory((acpi_physical_address)reg->address, 973 val, reg->bit_width); 974 975 switch (reg->bit_width) { 976 case 8: 977 writeb_relaxed(val, vaddr); 978 break; 979 case 16: 980 writew_relaxed(val, vaddr); 981 break; 982 case 32: 983 writel_relaxed(val, vaddr); 984 break; 985 case 64: 986 writeq_relaxed(val, vaddr); 987 break; 988 default: 989 pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n", 990 reg->bit_width, pcc_ss_id); 991 ret_val = -EFAULT; 992 break; 993 } 994 995 return ret_val; 996 } 997 998 static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf) 999 { 1000 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 1001 struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx]; 1002 1003 if (CPC_IN_PCC(reg)) { 1004 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 1005 struct cppc_pcc_data *pcc_ss_data = NULL; 1006 int ret = 0; 1007 1008 if (pcc_ss_id < 0) 1009 return -EIO; 1010 1011 pcc_ss_data = pcc_data[pcc_ss_id]; 1012 1013 down_write(&pcc_ss_data->pcc_lock); 1014 1015 if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0) 1016 cpc_read(cpunum, reg, perf); 1017 else 1018 ret = -EIO; 1019 1020 up_write(&pcc_ss_data->pcc_lock); 1021 1022 return ret; 1023 } 1024 1025 cpc_read(cpunum, reg, perf); 1026 1027 return 0; 1028 } 1029 1030 /** 1031 * cppc_get_desired_perf - Get the desired performance register value. 1032 * @cpunum: CPU from which to get desired performance. 1033 * @desired_perf: Return address. 1034 * 1035 * Return: 0 for success, -EIO otherwise. 1036 */ 1037 int cppc_get_desired_perf(int cpunum, u64 *desired_perf) 1038 { 1039 return cppc_get_perf(cpunum, DESIRED_PERF, desired_perf); 1040 } 1041 EXPORT_SYMBOL_GPL(cppc_get_desired_perf); 1042 1043 /** 1044 * cppc_get_nominal_perf - Get the nominal performance register value. 1045 * @cpunum: CPU from which to get nominal performance. 1046 * @nominal_perf: Return address. 1047 * 1048 * Return: 0 for success, -EIO otherwise. 1049 */ 1050 int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf) 1051 { 1052 return cppc_get_perf(cpunum, NOMINAL_PERF, nominal_perf); 1053 } 1054 1055 /** 1056 * cppc_get_perf_caps - Get a CPU's performance capabilities. 1057 * @cpunum: CPU from which to get capabilities info. 1058 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h 1059 * 1060 * Return: 0 for success with perf_caps populated else -ERRNO. 1061 */ 1062 int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps) 1063 { 1064 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 1065 struct cpc_register_resource *highest_reg, *lowest_reg, 1066 *lowest_non_linear_reg, *nominal_reg, *guaranteed_reg, 1067 *low_freq_reg = NULL, *nom_freq_reg = NULL; 1068 u64 high, low, guaranteed, nom, min_nonlinear, low_f = 0, nom_f = 0; 1069 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 1070 struct cppc_pcc_data *pcc_ss_data = NULL; 1071 int ret = 0, regs_in_pcc = 0; 1072 1073 if (!cpc_desc) { 1074 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 1075 return -ENODEV; 1076 } 1077 1078 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF]; 1079 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF]; 1080 lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF]; 1081 nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF]; 1082 low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ]; 1083 nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ]; 1084 guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF]; 1085 1086 /* Are any of the regs PCC ?*/ 1087 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) || 1088 CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) || 1089 CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg)) { 1090 if (pcc_ss_id < 0) { 1091 pr_debug("Invalid pcc_ss_id\n"); 1092 return -ENODEV; 1093 } 1094 pcc_ss_data = pcc_data[pcc_ss_id]; 1095 regs_in_pcc = 1; 1096 down_write(&pcc_ss_data->pcc_lock); 1097 /* Ring doorbell once to update PCC subspace */ 1098 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 1099 ret = -EIO; 1100 goto out_err; 1101 } 1102 } 1103 1104 cpc_read(cpunum, highest_reg, &high); 1105 perf_caps->highest_perf = high; 1106 1107 cpc_read(cpunum, lowest_reg, &low); 1108 perf_caps->lowest_perf = low; 1109 1110 cpc_read(cpunum, nominal_reg, &nom); 1111 perf_caps->nominal_perf = nom; 1112 1113 if (guaranteed_reg->type != ACPI_TYPE_BUFFER || 1114 IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) { 1115 perf_caps->guaranteed_perf = 0; 1116 } else { 1117 cpc_read(cpunum, guaranteed_reg, &guaranteed); 1118 perf_caps->guaranteed_perf = guaranteed; 1119 } 1120 1121 cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear); 1122 perf_caps->lowest_nonlinear_perf = min_nonlinear; 1123 1124 if (!high || !low || !nom || !min_nonlinear) 1125 ret = -EFAULT; 1126 1127 /* Read optional lowest and nominal frequencies if present */ 1128 if (CPC_SUPPORTED(low_freq_reg)) 1129 cpc_read(cpunum, low_freq_reg, &low_f); 1130 1131 if (CPC_SUPPORTED(nom_freq_reg)) 1132 cpc_read(cpunum, nom_freq_reg, &nom_f); 1133 1134 perf_caps->lowest_freq = low_f; 1135 perf_caps->nominal_freq = nom_f; 1136 1137 1138 out_err: 1139 if (regs_in_pcc) 1140 up_write(&pcc_ss_data->pcc_lock); 1141 return ret; 1142 } 1143 EXPORT_SYMBOL_GPL(cppc_get_perf_caps); 1144 1145 /** 1146 * cppc_get_perf_ctrs - Read a CPU's performance feedback counters. 1147 * @cpunum: CPU from which to read counters. 1148 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h 1149 * 1150 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO. 1151 */ 1152 int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs) 1153 { 1154 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum); 1155 struct cpc_register_resource *delivered_reg, *reference_reg, 1156 *ref_perf_reg, *ctr_wrap_reg; 1157 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum); 1158 struct cppc_pcc_data *pcc_ss_data = NULL; 1159 u64 delivered, reference, ref_perf, ctr_wrap_time; 1160 int ret = 0, regs_in_pcc = 0; 1161 1162 if (!cpc_desc) { 1163 pr_debug("No CPC descriptor for CPU:%d\n", cpunum); 1164 return -ENODEV; 1165 } 1166 1167 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR]; 1168 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR]; 1169 ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF]; 1170 ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME]; 1171 1172 /* 1173 * If reference perf register is not supported then we should 1174 * use the nominal perf value 1175 */ 1176 if (!CPC_SUPPORTED(ref_perf_reg)) 1177 ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF]; 1178 1179 /* Are any of the regs PCC ?*/ 1180 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) || 1181 CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) { 1182 if (pcc_ss_id < 0) { 1183 pr_debug("Invalid pcc_ss_id\n"); 1184 return -ENODEV; 1185 } 1186 pcc_ss_data = pcc_data[pcc_ss_id]; 1187 down_write(&pcc_ss_data->pcc_lock); 1188 regs_in_pcc = 1; 1189 /* Ring doorbell once to update PCC subspace */ 1190 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) { 1191 ret = -EIO; 1192 goto out_err; 1193 } 1194 } 1195 1196 cpc_read(cpunum, delivered_reg, &delivered); 1197 cpc_read(cpunum, reference_reg, &reference); 1198 cpc_read(cpunum, ref_perf_reg, &ref_perf); 1199 1200 /* 1201 * Per spec, if ctr_wrap_time optional register is unsupported, then the 1202 * performance counters are assumed to never wrap during the lifetime of 1203 * platform 1204 */ 1205 ctr_wrap_time = (u64)(~((u64)0)); 1206 if (CPC_SUPPORTED(ctr_wrap_reg)) 1207 cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time); 1208 1209 if (!delivered || !reference || !ref_perf) { 1210 ret = -EFAULT; 1211 goto out_err; 1212 } 1213 1214 perf_fb_ctrs->delivered = delivered; 1215 perf_fb_ctrs->reference = reference; 1216 perf_fb_ctrs->reference_perf = ref_perf; 1217 perf_fb_ctrs->wraparound_time = ctr_wrap_time; 1218 out_err: 1219 if (regs_in_pcc) 1220 up_write(&pcc_ss_data->pcc_lock); 1221 return ret; 1222 } 1223 EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs); 1224 1225 /** 1226 * cppc_set_perf - Set a CPU's performance controls. 1227 * @cpu: CPU for which to set performance controls. 1228 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h 1229 * 1230 * Return: 0 for success, -ERRNO otherwise. 1231 */ 1232 int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) 1233 { 1234 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu); 1235 struct cpc_register_resource *desired_reg; 1236 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu); 1237 struct cppc_pcc_data *pcc_ss_data = NULL; 1238 int ret = 0; 1239 1240 if (!cpc_desc) { 1241 pr_debug("No CPC descriptor for CPU:%d\n", cpu); 1242 return -ENODEV; 1243 } 1244 1245 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 1246 1247 /* 1248 * This is Phase-I where we want to write to CPC registers 1249 * -> We want all CPUs to be able to execute this phase in parallel 1250 * 1251 * Since read_lock can be acquired by multiple CPUs simultaneously we 1252 * achieve that goal here 1253 */ 1254 if (CPC_IN_PCC(desired_reg)) { 1255 if (pcc_ss_id < 0) { 1256 pr_debug("Invalid pcc_ss_id\n"); 1257 return -ENODEV; 1258 } 1259 pcc_ss_data = pcc_data[pcc_ss_id]; 1260 down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */ 1261 if (pcc_ss_data->platform_owns_pcc) { 1262 ret = check_pcc_chan(pcc_ss_id, false); 1263 if (ret) { 1264 up_read(&pcc_ss_data->pcc_lock); 1265 return ret; 1266 } 1267 } 1268 /* 1269 * Update the pending_write to make sure a PCC CMD_READ will not 1270 * arrive and steal the channel during the switch to write lock 1271 */ 1272 pcc_ss_data->pending_pcc_write_cmd = true; 1273 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; 1274 cpc_desc->write_cmd_status = 0; 1275 } 1276 1277 /* 1278 * Skip writing MIN/MAX until Linux knows how to come up with 1279 * useful values. 1280 */ 1281 cpc_write(cpu, desired_reg, perf_ctrls->desired_perf); 1282 1283 if (CPC_IN_PCC(desired_reg)) 1284 up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */ 1285 /* 1286 * This is Phase-II where we transfer the ownership of PCC to Platform 1287 * 1288 * Short Summary: Basically if we think of a group of cppc_set_perf 1289 * requests that happened in short overlapping interval. The last CPU to 1290 * come out of Phase-I will enter Phase-II and ring the doorbell. 1291 * 1292 * We have the following requirements for Phase-II: 1293 * 1. We want to execute Phase-II only when there are no CPUs 1294 * currently executing in Phase-I 1295 * 2. Once we start Phase-II we want to avoid all other CPUs from 1296 * entering Phase-I. 1297 * 3. We want only one CPU among all those who went through Phase-I 1298 * to run phase-II 1299 * 1300 * If write_trylock fails to get the lock and doesn't transfer the 1301 * PCC ownership to the platform, then one of the following will be TRUE 1302 * 1. There is at-least one CPU in Phase-I which will later execute 1303 * write_trylock, so the CPUs in Phase-I will be responsible for 1304 * executing the Phase-II. 1305 * 2. Some other CPU has beaten this CPU to successfully execute the 1306 * write_trylock and has already acquired the write_lock. We know for a 1307 * fact it (other CPU acquiring the write_lock) couldn't have happened 1308 * before this CPU's Phase-I as we held the read_lock. 1309 * 3. Some other CPU executing pcc CMD_READ has stolen the 1310 * down_write, in which case, send_pcc_cmd will check for pending 1311 * CMD_WRITE commands by checking the pending_pcc_write_cmd. 1312 * So this CPU can be certain that its request will be delivered 1313 * So in all cases, this CPU knows that its request will be delivered 1314 * by another CPU and can return 1315 * 1316 * After getting the down_write we still need to check for 1317 * pending_pcc_write_cmd to take care of the following scenario 1318 * The thread running this code could be scheduled out between 1319 * Phase-I and Phase-II. Before it is scheduled back on, another CPU 1320 * could have delivered the request to Platform by triggering the 1321 * doorbell and transferred the ownership of PCC to platform. So this 1322 * avoids triggering an unnecessary doorbell and more importantly before 1323 * triggering the doorbell it makes sure that the PCC channel ownership 1324 * is still with OSPM. 1325 * pending_pcc_write_cmd can also be cleared by a different CPU, if 1326 * there was a pcc CMD_READ waiting on down_write and it steals the lock 1327 * before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this 1328 * case during a CMD_READ and if there are pending writes it delivers 1329 * the write command before servicing the read command 1330 */ 1331 if (CPC_IN_PCC(desired_reg)) { 1332 if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */ 1333 /* Update only if there are pending write commands */ 1334 if (pcc_ss_data->pending_pcc_write_cmd) 1335 send_pcc_cmd(pcc_ss_id, CMD_WRITE); 1336 up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */ 1337 } else 1338 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */ 1339 wait_event(pcc_ss_data->pcc_write_wait_q, 1340 cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt); 1341 1342 /* send_pcc_cmd updates the status in case of failure */ 1343 ret = cpc_desc->write_cmd_status; 1344 } 1345 return ret; 1346 } 1347 EXPORT_SYMBOL_GPL(cppc_set_perf); 1348 1349 /** 1350 * cppc_get_transition_latency - returns frequency transition latency in ns 1351 * 1352 * ACPI CPPC does not explicitly specify how a platform can specify the 1353 * transition latency for performance change requests. The closest we have 1354 * is the timing information from the PCCT tables which provides the info 1355 * on the number and frequency of PCC commands the platform can handle. 1356 */ 1357 unsigned int cppc_get_transition_latency(int cpu_num) 1358 { 1359 /* 1360 * Expected transition latency is based on the PCCT timing values 1361 * Below are definition from ACPI spec: 1362 * pcc_nominal- Expected latency to process a command, in microseconds 1363 * pcc_mpar - The maximum number of periodic requests that the subspace 1364 * channel can support, reported in commands per minute. 0 1365 * indicates no limitation. 1366 * pcc_mrtt - The minimum amount of time that OSPM must wait after the 1367 * completion of a command before issuing the next command, 1368 * in microseconds. 1369 */ 1370 unsigned int latency_ns = 0; 1371 struct cpc_desc *cpc_desc; 1372 struct cpc_register_resource *desired_reg; 1373 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num); 1374 struct cppc_pcc_data *pcc_ss_data; 1375 1376 cpc_desc = per_cpu(cpc_desc_ptr, cpu_num); 1377 if (!cpc_desc) 1378 return CPUFREQ_ETERNAL; 1379 1380 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF]; 1381 if (!CPC_IN_PCC(desired_reg)) 1382 return CPUFREQ_ETERNAL; 1383 1384 if (pcc_ss_id < 0) 1385 return CPUFREQ_ETERNAL; 1386 1387 pcc_ss_data = pcc_data[pcc_ss_id]; 1388 if (pcc_ss_data->pcc_mpar) 1389 latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar); 1390 1391 latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000); 1392 latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000); 1393 1394 return latency_ns; 1395 } 1396 EXPORT_SYMBOL_GPL(cppc_get_transition_latency); 1397