1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * processor_throttling.c - Throttling submodule of the ACPI processor driver 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de> 8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> 9 * - Added processor hotplug support 10 */ 11 12 #define pr_fmt(fmt) "ACPI: " fmt 13 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/init.h> 18 #include <linux/sched.h> 19 #include <linux/cpufreq.h> 20 #include <linux/acpi.h> 21 #include <linux/uaccess.h> 22 #include <acpi/processor.h> 23 #include <asm/io.h> 24 #ifdef CONFIG_X86 25 #include <asm/msr.h> 26 #endif 27 28 /* ignore_tpc: 29 * 0 -> acpi processor driver doesn't ignore _TPC values 30 * 1 -> acpi processor driver ignores _TPC values 31 */ 32 static int ignore_tpc; 33 module_param(ignore_tpc, int, 0644); 34 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support"); 35 36 struct throttling_tstate { 37 unsigned int cpu; /* cpu nr */ 38 int target_state; /* target T-state */ 39 }; 40 41 struct acpi_processor_throttling_arg { 42 struct acpi_processor *pr; 43 int target_state; 44 bool force; 45 }; 46 47 #define THROTTLING_PRECHANGE (1) 48 #define THROTTLING_POSTCHANGE (2) 49 50 static int acpi_processor_get_throttling(struct acpi_processor *pr); 51 static int __acpi_processor_set_throttling(struct acpi_processor *pr, 52 int state, bool force, bool direct); 53 54 static int acpi_processor_update_tsd_coord(void) 55 { 56 int count_target; 57 int retval = 0; 58 unsigned int i, j; 59 cpumask_var_t covered_cpus; 60 struct acpi_processor *pr, *match_pr; 61 struct acpi_tsd_package *pdomain, *match_pdomain; 62 struct acpi_processor_throttling *pthrottling, *match_pthrottling; 63 64 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL)) 65 return -ENOMEM; 66 67 /* 68 * Now that we have _TSD data from all CPUs, lets setup T-state 69 * coordination between all CPUs. 70 */ 71 for_each_possible_cpu(i) { 72 pr = per_cpu(processors, i); 73 if (!pr) 74 continue; 75 76 /* Basic validity check for domain info */ 77 pthrottling = &(pr->throttling); 78 79 /* 80 * If tsd package for one cpu is invalid, the coordination 81 * among all CPUs is thought as invalid. 82 * Maybe it is ugly. 83 */ 84 if (!pthrottling->tsd_valid_flag) { 85 retval = -EINVAL; 86 break; 87 } 88 } 89 if (retval) 90 goto err_ret; 91 92 for_each_possible_cpu(i) { 93 pr = per_cpu(processors, i); 94 if (!pr) 95 continue; 96 97 if (cpumask_test_cpu(i, covered_cpus)) 98 continue; 99 pthrottling = &pr->throttling; 100 101 pdomain = &(pthrottling->domain_info); 102 cpumask_set_cpu(i, pthrottling->shared_cpu_map); 103 cpumask_set_cpu(i, covered_cpus); 104 /* 105 * If the number of processor in the TSD domain is 1, it is 106 * unnecessary to parse the coordination for this CPU. 107 */ 108 if (pdomain->num_processors <= 1) 109 continue; 110 111 /* Validate the Domain info */ 112 count_target = pdomain->num_processors; 113 114 for_each_possible_cpu(j) { 115 if (i == j) 116 continue; 117 118 match_pr = per_cpu(processors, j); 119 if (!match_pr) 120 continue; 121 122 match_pthrottling = &(match_pr->throttling); 123 match_pdomain = &(match_pthrottling->domain_info); 124 if (match_pdomain->domain != pdomain->domain) 125 continue; 126 127 /* Here i and j are in the same domain. 128 * If two TSD packages have the same domain, they 129 * should have the same num_porcessors and 130 * coordination type. Otherwise it will be regarded 131 * as illegal. 132 */ 133 if (match_pdomain->num_processors != count_target) { 134 retval = -EINVAL; 135 goto err_ret; 136 } 137 138 if (pdomain->coord_type != match_pdomain->coord_type) { 139 retval = -EINVAL; 140 goto err_ret; 141 } 142 143 cpumask_set_cpu(j, covered_cpus); 144 cpumask_set_cpu(j, pthrottling->shared_cpu_map); 145 } 146 for_each_possible_cpu(j) { 147 if (i == j) 148 continue; 149 150 match_pr = per_cpu(processors, j); 151 if (!match_pr) 152 continue; 153 154 match_pthrottling = &(match_pr->throttling); 155 match_pdomain = &(match_pthrottling->domain_info); 156 if (match_pdomain->domain != pdomain->domain) 157 continue; 158 159 /* 160 * If some CPUS have the same domain, they 161 * will have the same shared_cpu_map. 162 */ 163 cpumask_copy(match_pthrottling->shared_cpu_map, 164 pthrottling->shared_cpu_map); 165 } 166 } 167 168 err_ret: 169 free_cpumask_var(covered_cpus); 170 171 for_each_possible_cpu(i) { 172 pr = per_cpu(processors, i); 173 if (!pr) 174 continue; 175 176 /* 177 * Assume no coordination on any error parsing domain info. 178 * The coordination type will be forced as SW_ALL. 179 */ 180 if (retval) { 181 pthrottling = &(pr->throttling); 182 cpumask_clear(pthrottling->shared_cpu_map); 183 cpumask_set_cpu(i, pthrottling->shared_cpu_map); 184 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; 185 } 186 } 187 188 return retval; 189 } 190 191 /* 192 * Update the T-state coordination after the _TSD 193 * data for all cpus is obtained. 194 */ 195 void acpi_processor_throttling_init(void) 196 { 197 if (acpi_processor_update_tsd_coord()) 198 pr_debug("Assume no T-state coordination\n"); 199 } 200 201 static int acpi_processor_throttling_notifier(unsigned long event, void *data) 202 { 203 struct throttling_tstate *p_tstate = data; 204 struct acpi_processor *pr; 205 unsigned int cpu; 206 int target_state; 207 struct acpi_processor_limit *p_limit; 208 struct acpi_processor_throttling *p_throttling; 209 210 cpu = p_tstate->cpu; 211 pr = per_cpu(processors, cpu); 212 if (!pr) { 213 pr_debug("Invalid pr pointer\n"); 214 return 0; 215 } 216 if (!pr->flags.throttling) { 217 acpi_handle_debug(pr->handle, 218 "Throttling control unsupported on CPU %d\n", 219 cpu); 220 return 0; 221 } 222 target_state = p_tstate->target_state; 223 p_throttling = &(pr->throttling); 224 switch (event) { 225 case THROTTLING_PRECHANGE: 226 /* 227 * Prechange event is used to choose one proper t-state, 228 * which meets the limits of thermal, user and _TPC. 229 */ 230 p_limit = &pr->limit; 231 if (p_limit->thermal.tx > target_state) 232 target_state = p_limit->thermal.tx; 233 if (p_limit->user.tx > target_state) 234 target_state = p_limit->user.tx; 235 if (pr->throttling_platform_limit > target_state) 236 target_state = pr->throttling_platform_limit; 237 if (target_state >= p_throttling->state_count) { 238 pr_warn("Exceed the limit of T-state\n"); 239 target_state = p_throttling->state_count - 1; 240 } 241 p_tstate->target_state = target_state; 242 acpi_handle_debug(pr->handle, 243 "PreChange Event: target T-state of CPU %d is T%d\n", 244 cpu, target_state); 245 break; 246 case THROTTLING_POSTCHANGE: 247 /* 248 * Postchange event is only used to update the 249 * T-state flag of acpi_processor_throttling. 250 */ 251 p_throttling->state = target_state; 252 acpi_handle_debug(pr->handle, 253 "PostChange Event: CPU %d is switched to T%d\n", 254 cpu, target_state); 255 break; 256 default: 257 pr_warn("Unsupported Throttling notifier event\n"); 258 break; 259 } 260 261 return 0; 262 } 263 264 /* 265 * _TPC - Throttling Present Capabilities 266 */ 267 static int acpi_processor_get_platform_limit(struct acpi_processor *pr) 268 { 269 acpi_status status = 0; 270 unsigned long long tpc = 0; 271 272 if (!pr) 273 return -EINVAL; 274 275 if (ignore_tpc) 276 goto end; 277 278 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc); 279 if (ACPI_FAILURE(status)) { 280 if (status != AE_NOT_FOUND) 281 acpi_evaluation_failure_warn(pr->handle, "_TPC", status); 282 283 return -ENODEV; 284 } 285 286 end: 287 pr->throttling_platform_limit = (int)tpc; 288 return 0; 289 } 290 291 int acpi_processor_tstate_has_changed(struct acpi_processor *pr) 292 { 293 int result = 0; 294 int throttling_limit; 295 int current_state; 296 struct acpi_processor_limit *limit; 297 int target_state; 298 299 if (ignore_tpc) 300 return 0; 301 302 result = acpi_processor_get_platform_limit(pr); 303 if (result) { 304 /* Throttling Limit is unsupported */ 305 return result; 306 } 307 308 throttling_limit = pr->throttling_platform_limit; 309 if (throttling_limit >= pr->throttling.state_count) { 310 /* Uncorrect Throttling Limit */ 311 return -EINVAL; 312 } 313 314 current_state = pr->throttling.state; 315 if (current_state > throttling_limit) { 316 /* 317 * The current state can meet the requirement of 318 * _TPC limit. But it is reasonable that OSPM changes 319 * t-states from high to low for better performance. 320 * Of course the limit condition of thermal 321 * and user should be considered. 322 */ 323 limit = &pr->limit; 324 target_state = throttling_limit; 325 if (limit->thermal.tx > target_state) 326 target_state = limit->thermal.tx; 327 if (limit->user.tx > target_state) 328 target_state = limit->user.tx; 329 } else if (current_state == throttling_limit) { 330 /* 331 * Unnecessary to change the throttling state 332 */ 333 return 0; 334 } else { 335 /* 336 * If the current state is lower than the limit of _TPC, it 337 * will be forced to switch to the throttling state defined 338 * by throttling_platfor_limit. 339 * Because the previous state meets with the limit condition 340 * of thermal and user, it is unnecessary to check it again. 341 */ 342 target_state = throttling_limit; 343 } 344 return acpi_processor_set_throttling(pr, target_state, false); 345 } 346 347 /* 348 * This function is used to reevaluate whether the T-state is valid 349 * after one CPU is onlined/offlined. 350 * It is noted that it won't reevaluate the following properties for 351 * the T-state. 352 * 1. Control method. 353 * 2. the number of supported T-state 354 * 3. TSD domain 355 */ 356 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr, 357 bool is_dead) 358 { 359 int result = 0; 360 361 if (is_dead) { 362 /* When one CPU is offline, the T-state throttling 363 * will be invalidated. 364 */ 365 pr->flags.throttling = 0; 366 return; 367 } 368 /* the following is to recheck whether the T-state is valid for 369 * the online CPU 370 */ 371 if (!pr->throttling.state_count) { 372 /* If the number of T-state is invalid, it is 373 * invalidated. 374 */ 375 pr->flags.throttling = 0; 376 return; 377 } 378 pr->flags.throttling = 1; 379 380 /* Disable throttling (if enabled). We'll let subsequent 381 * policy (e.g.thermal) decide to lower performance if it 382 * so chooses, but for now we'll crank up the speed. 383 */ 384 385 result = acpi_processor_get_throttling(pr); 386 if (result) 387 goto end; 388 389 if (pr->throttling.state) { 390 result = acpi_processor_set_throttling(pr, 0, false); 391 if (result) 392 goto end; 393 } 394 395 end: 396 if (result) 397 pr->flags.throttling = 0; 398 } 399 /* 400 * _PTC - Processor Throttling Control (and status) register location 401 */ 402 static int acpi_processor_get_throttling_control(struct acpi_processor *pr) 403 { 404 int result = 0; 405 acpi_status status = 0; 406 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 407 union acpi_object *ptc = NULL; 408 union acpi_object obj; 409 struct acpi_processor_throttling *throttling; 410 411 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer); 412 if (ACPI_FAILURE(status)) { 413 if (status != AE_NOT_FOUND) 414 acpi_evaluation_failure_warn(pr->handle, "_PTC", status); 415 416 return -ENODEV; 417 } 418 419 ptc = (union acpi_object *)buffer.pointer; 420 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE) 421 || (ptc->package.count != 2)) { 422 pr_err("Invalid _PTC data\n"); 423 result = -EFAULT; 424 goto end; 425 } 426 427 /* 428 * control_register 429 */ 430 431 obj = ptc->package.elements[0]; 432 433 if ((obj.type != ACPI_TYPE_BUFFER) 434 || (obj.buffer.length < sizeof(struct acpi_ptc_register)) 435 || (obj.buffer.pointer == NULL)) { 436 pr_err("Invalid _PTC data (control_register)\n"); 437 result = -EFAULT; 438 goto end; 439 } 440 memcpy(&pr->throttling.control_register, obj.buffer.pointer, 441 sizeof(struct acpi_ptc_register)); 442 443 /* 444 * status_register 445 */ 446 447 obj = ptc->package.elements[1]; 448 449 if ((obj.type != ACPI_TYPE_BUFFER) 450 || (obj.buffer.length < sizeof(struct acpi_ptc_register)) 451 || (obj.buffer.pointer == NULL)) { 452 pr_err("Invalid _PTC data (status_register)\n"); 453 result = -EFAULT; 454 goto end; 455 } 456 457 memcpy(&pr->throttling.status_register, obj.buffer.pointer, 458 sizeof(struct acpi_ptc_register)); 459 460 throttling = &pr->throttling; 461 462 if ((throttling->control_register.bit_width + 463 throttling->control_register.bit_offset) > 32) { 464 pr_err("Invalid _PTC control register\n"); 465 result = -EFAULT; 466 goto end; 467 } 468 469 if ((throttling->status_register.bit_width + 470 throttling->status_register.bit_offset) > 32) { 471 pr_err("Invalid _PTC status register\n"); 472 result = -EFAULT; 473 goto end; 474 } 475 476 end: 477 kfree(buffer.pointer); 478 479 return result; 480 } 481 482 /* 483 * _TSS - Throttling Supported States 484 */ 485 static int acpi_processor_get_throttling_states(struct acpi_processor *pr) 486 { 487 int result = 0; 488 acpi_status status = AE_OK; 489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 490 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" }; 491 struct acpi_buffer state = { 0, NULL }; 492 union acpi_object *tss = NULL; 493 int i; 494 495 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer); 496 if (ACPI_FAILURE(status)) { 497 if (status != AE_NOT_FOUND) 498 acpi_evaluation_failure_warn(pr->handle, "_TSS", status); 499 500 return -ENODEV; 501 } 502 503 tss = buffer.pointer; 504 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) { 505 pr_err("Invalid _TSS data\n"); 506 result = -EFAULT; 507 goto end; 508 } 509 510 acpi_handle_debug(pr->handle, "Found %d throttling states\n", 511 tss->package.count); 512 513 pr->throttling.state_count = tss->package.count; 514 pr->throttling.states_tss = 515 kmalloc_objs(struct acpi_processor_tx_tss, tss->package.count); 516 if (!pr->throttling.states_tss) { 517 result = -ENOMEM; 518 goto end; 519 } 520 521 for (i = 0; i < pr->throttling.state_count; i++) { 522 523 struct acpi_processor_tx_tss *tx = 524 (struct acpi_processor_tx_tss *)&(pr->throttling. 525 states_tss[i]); 526 527 state.length = sizeof(struct acpi_processor_tx_tss); 528 state.pointer = tx; 529 530 acpi_handle_debug(pr->handle, "Extracting state %d\n", i); 531 532 status = acpi_extract_package(&(tss->package.elements[i]), 533 &format, &state); 534 if (ACPI_FAILURE(status)) { 535 acpi_handle_warn(pr->handle, "Invalid _TSS data: %s\n", 536 acpi_format_exception(status)); 537 result = -EFAULT; 538 kfree(pr->throttling.states_tss); 539 goto end; 540 } 541 542 if (!tx->freqpercentage) { 543 pr_err("Invalid _TSS data: freq is zero\n"); 544 result = -EFAULT; 545 kfree(pr->throttling.states_tss); 546 goto end; 547 } 548 } 549 550 end: 551 kfree(buffer.pointer); 552 553 return result; 554 } 555 556 /* 557 * _TSD - T-State Dependencies 558 */ 559 static int acpi_processor_get_tsd(struct acpi_processor *pr) 560 { 561 int result = 0; 562 acpi_status status = AE_OK; 563 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 564 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" }; 565 struct acpi_buffer state = { 0, NULL }; 566 union acpi_object *tsd = NULL; 567 struct acpi_tsd_package *pdomain; 568 struct acpi_processor_throttling *pthrottling; 569 570 pthrottling = &pr->throttling; 571 pthrottling->tsd_valid_flag = 0; 572 573 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer); 574 if (ACPI_FAILURE(status)) { 575 if (status != AE_NOT_FOUND) 576 acpi_evaluation_failure_warn(pr->handle, "_TSD", status); 577 578 return -ENODEV; 579 } 580 581 tsd = buffer.pointer; 582 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) { 583 pr_err("Invalid _TSD data\n"); 584 result = -EFAULT; 585 goto end; 586 } 587 588 if (tsd->package.count != 1) { 589 pr_err("Invalid _TSD data\n"); 590 result = -EFAULT; 591 goto end; 592 } 593 594 pdomain = &(pr->throttling.domain_info); 595 596 state.length = sizeof(struct acpi_tsd_package); 597 state.pointer = pdomain; 598 599 status = acpi_extract_package(&(tsd->package.elements[0]), 600 &format, &state); 601 if (ACPI_FAILURE(status)) { 602 pr_err("Invalid _TSD data\n"); 603 result = -EFAULT; 604 goto end; 605 } 606 607 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) { 608 pr_err("Unknown _TSD:num_entries\n"); 609 result = -EFAULT; 610 goto end; 611 } 612 613 if (pdomain->revision != ACPI_TSD_REV0_REVISION) { 614 pr_err("Unknown _TSD:revision\n"); 615 result = -EFAULT; 616 goto end; 617 } 618 619 pthrottling = &pr->throttling; 620 pthrottling->tsd_valid_flag = 1; 621 pthrottling->shared_type = pdomain->coord_type; 622 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map); 623 /* 624 * If the coordination type is not defined in ACPI spec, 625 * the tsd_valid_flag will be clear and coordination type 626 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL. 627 */ 628 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL && 629 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY && 630 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) { 631 pthrottling->tsd_valid_flag = 0; 632 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; 633 } 634 635 end: 636 kfree(buffer.pointer); 637 return result; 638 } 639 640 /* -------------------------------------------------------------------------- 641 Throttling Control 642 -------------------------------------------------------------------------- */ 643 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr) 644 { 645 int state = 0; 646 u32 value = 0; 647 u32 duty_mask = 0; 648 u32 duty_value = 0; 649 650 if (!pr) 651 return -EINVAL; 652 653 if (!pr->flags.throttling) 654 return -ENODEV; 655 656 /* 657 * We don't care about error returns - we just try to mark 658 * these reserved so that nobody else is confused into thinking 659 * that this region might be unused.. 660 * 661 * (In particular, allocating the IO range for Cardbus) 662 */ 663 request_region(pr->throttling.address, 6, "ACPI CPU throttle"); 664 665 pr->throttling.state = 0; 666 667 duty_mask = pr->throttling.state_count - 1; 668 669 duty_mask <<= pr->throttling.duty_offset; 670 671 local_irq_disable(); 672 673 value = inl(pr->throttling.address); 674 675 /* 676 * Compute the current throttling state when throttling is enabled 677 * (bit 4 is on). 678 */ 679 if (value & 0x10) { 680 duty_value = value & duty_mask; 681 duty_value >>= pr->throttling.duty_offset; 682 683 if (duty_value) 684 state = pr->throttling.state_count - duty_value; 685 } 686 687 pr->throttling.state = state; 688 689 local_irq_enable(); 690 691 acpi_handle_debug(pr->handle, 692 "Throttling state is T%d (%d%% throttling applied)\n", 693 state, pr->throttling.states[state].performance); 694 695 return 0; 696 } 697 698 #ifdef CONFIG_X86 699 static int acpi_throttling_rdmsr(u64 *value) 700 { 701 u64 msr_high, msr_low; 702 u64 msr = 0; 703 int ret = -1; 704 705 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) || 706 !this_cpu_has(X86_FEATURE_ACPI)) { 707 pr_err("HARDWARE addr space,NOT supported yet\n"); 708 } else { 709 msr_low = 0; 710 msr_high = 0; 711 rdmsr_safe(MSR_IA32_THERM_CONTROL, 712 (u32 *)&msr_low, (u32 *) &msr_high); 713 msr = (msr_high << 32) | msr_low; 714 *value = (u64) msr; 715 ret = 0; 716 } 717 return ret; 718 } 719 720 static int acpi_throttling_wrmsr(u64 value) 721 { 722 int ret = -1; 723 u64 msr; 724 725 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) || 726 !this_cpu_has(X86_FEATURE_ACPI)) { 727 pr_err("HARDWARE addr space,NOT supported yet\n"); 728 } else { 729 msr = value; 730 wrmsr_safe(MSR_IA32_THERM_CONTROL, 731 msr & 0xffffffff, msr >> 32); 732 ret = 0; 733 } 734 return ret; 735 } 736 #else 737 static int acpi_throttling_rdmsr(u64 *value) 738 { 739 pr_err("HARDWARE addr space,NOT supported yet\n"); 740 return -1; 741 } 742 743 static int acpi_throttling_wrmsr(u64 value) 744 { 745 pr_err("HARDWARE addr space,NOT supported yet\n"); 746 return -1; 747 } 748 #endif 749 750 static int acpi_read_throttling_status(struct acpi_processor *pr, 751 u64 *value) 752 { 753 u32 bit_width, bit_offset; 754 u32 ptc_value; 755 u64 ptc_mask; 756 struct acpi_processor_throttling *throttling; 757 int ret = -1; 758 759 throttling = &pr->throttling; 760 switch (throttling->status_register.space_id) { 761 case ACPI_ADR_SPACE_SYSTEM_IO: 762 bit_width = throttling->status_register.bit_width; 763 bit_offset = throttling->status_register.bit_offset; 764 765 acpi_os_read_port((acpi_io_address) throttling->status_register. 766 address, &ptc_value, 767 (u32) (bit_width + bit_offset)); 768 ptc_mask = (1 << bit_width) - 1; 769 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask); 770 ret = 0; 771 break; 772 case ACPI_ADR_SPACE_FIXED_HARDWARE: 773 ret = acpi_throttling_rdmsr(value); 774 break; 775 default: 776 pr_err("Unknown addr space %d\n", 777 (u32) (throttling->status_register.space_id)); 778 } 779 return ret; 780 } 781 782 static int acpi_write_throttling_state(struct acpi_processor *pr, 783 u64 value) 784 { 785 u32 bit_width, bit_offset; 786 u64 ptc_value; 787 u64 ptc_mask; 788 struct acpi_processor_throttling *throttling; 789 int ret = -1; 790 791 throttling = &pr->throttling; 792 switch (throttling->control_register.space_id) { 793 case ACPI_ADR_SPACE_SYSTEM_IO: 794 bit_width = throttling->control_register.bit_width; 795 bit_offset = throttling->control_register.bit_offset; 796 ptc_mask = (1 << bit_width) - 1; 797 ptc_value = value & ptc_mask; 798 799 acpi_os_write_port((acpi_io_address) throttling-> 800 control_register.address, 801 (u32) (ptc_value << bit_offset), 802 (u32) (bit_width + bit_offset)); 803 ret = 0; 804 break; 805 case ACPI_ADR_SPACE_FIXED_HARDWARE: 806 ret = acpi_throttling_wrmsr(value); 807 break; 808 default: 809 pr_err("Unknown addr space %d\n", 810 (u32) (throttling->control_register.space_id)); 811 } 812 return ret; 813 } 814 815 static int acpi_get_throttling_state(struct acpi_processor *pr, 816 u64 value) 817 { 818 int i; 819 820 for (i = 0; i < pr->throttling.state_count; i++) { 821 struct acpi_processor_tx_tss *tx = 822 (struct acpi_processor_tx_tss *)&(pr->throttling. 823 states_tss[i]); 824 if (tx->control == value) 825 return i; 826 } 827 return -1; 828 } 829 830 static int acpi_get_throttling_value(struct acpi_processor *pr, 831 int state, u64 *value) 832 { 833 int ret = -1; 834 835 if (state >= 0 && state <= pr->throttling.state_count) { 836 struct acpi_processor_tx_tss *tx = 837 (struct acpi_processor_tx_tss *)&(pr->throttling. 838 states_tss[state]); 839 *value = tx->control; 840 ret = 0; 841 } 842 return ret; 843 } 844 845 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr) 846 { 847 int state = 0; 848 int ret; 849 u64 value; 850 851 if (!pr) 852 return -EINVAL; 853 854 if (!pr->flags.throttling) 855 return -ENODEV; 856 857 pr->throttling.state = 0; 858 859 value = 0; 860 ret = acpi_read_throttling_status(pr, &value); 861 if (ret >= 0) { 862 state = acpi_get_throttling_state(pr, value); 863 if (state == -1) { 864 acpi_handle_debug(pr->handle, 865 "Invalid throttling state, reset\n"); 866 state = 0; 867 ret = __acpi_processor_set_throttling(pr, state, true, 868 true); 869 if (ret) 870 return ret; 871 } 872 pr->throttling.state = state; 873 } 874 875 return 0; 876 } 877 878 static long __acpi_processor_get_throttling(void *data) 879 { 880 struct acpi_processor *pr = data; 881 882 return pr->throttling.acpi_processor_get_throttling(pr); 883 } 884 885 static int acpi_processor_get_throttling(struct acpi_processor *pr) 886 { 887 if (!pr) 888 return -EINVAL; 889 890 if (!pr->flags.throttling) 891 return -ENODEV; 892 893 /* 894 * This is either called from the CPU hotplug callback of 895 * processor_driver or via the ACPI probe function. In the latter 896 * case the CPU is not guaranteed to be online. Both call sites are 897 * protected against CPU hotplug. 898 */ 899 if (!cpu_online(pr->id)) 900 return -ENODEV; 901 902 return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false); 903 } 904 905 static int acpi_processor_get_fadt_info(struct acpi_processor *pr) 906 { 907 int i, step; 908 909 if (!pr->throttling.address) { 910 acpi_handle_debug(pr->handle, "No throttling register\n"); 911 return -EINVAL; 912 } else if (!pr->throttling.duty_width) { 913 acpi_handle_debug(pr->handle, "No throttling states\n"); 914 return -EINVAL; 915 } 916 /* TBD: Support duty_cycle values that span bit 4. */ 917 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) { 918 pr_warn("duty_cycle spans bit 4\n"); 919 return -EINVAL; 920 } 921 922 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width; 923 924 /* 925 * Compute state values. Note that throttling displays a linear power 926 * performance relationship (at 50% performance the CPU will consume 927 * 50% power). Values are in 1/10th of a percent to preserve accuracy. 928 */ 929 930 step = (1000 / pr->throttling.state_count); 931 932 for (i = 0; i < pr->throttling.state_count; i++) { 933 pr->throttling.states[i].performance = 1000 - step * i; 934 pr->throttling.states[i].power = 1000 - step * i; 935 } 936 return 0; 937 } 938 939 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr, 940 int state, bool force) 941 { 942 u32 value = 0; 943 u32 duty_mask = 0; 944 u32 duty_value = 0; 945 946 if (!pr) 947 return -EINVAL; 948 949 if ((state < 0) || (state > (pr->throttling.state_count - 1))) 950 return -EINVAL; 951 952 if (!pr->flags.throttling) 953 return -ENODEV; 954 955 if (!force && (state == pr->throttling.state)) 956 return 0; 957 958 if (state < pr->throttling_platform_limit) 959 return -EPERM; 960 /* 961 * Calculate the duty_value and duty_mask. 962 */ 963 if (state) { 964 duty_value = pr->throttling.state_count - state; 965 966 duty_value <<= pr->throttling.duty_offset; 967 968 /* Used to clear all duty_value bits */ 969 duty_mask = pr->throttling.state_count - 1; 970 971 duty_mask <<= acpi_gbl_FADT.duty_offset; 972 duty_mask = ~duty_mask; 973 } 974 975 local_irq_disable(); 976 977 /* 978 * Disable throttling by writing a 0 to bit 4. Note that we must 979 * turn it off before you can change the duty_value. 980 */ 981 value = inl(pr->throttling.address); 982 if (value & 0x10) { 983 value &= 0xFFFFFFEF; 984 outl(value, pr->throttling.address); 985 } 986 987 /* 988 * Write the new duty_value and then enable throttling. Note 989 * that a state value of 0 leaves throttling disabled. 990 */ 991 if (state) { 992 value &= duty_mask; 993 value |= duty_value; 994 outl(value, pr->throttling.address); 995 996 value |= 0x00000010; 997 outl(value, pr->throttling.address); 998 } 999 1000 pr->throttling.state = state; 1001 1002 local_irq_enable(); 1003 1004 acpi_handle_debug(pr->handle, 1005 "Throttling state set to T%d (%d%%)\n", state, 1006 (pr->throttling.states[state].performance ? pr-> 1007 throttling.states[state].performance / 10 : 0)); 1008 1009 return 0; 1010 } 1011 1012 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr, 1013 int state, bool force) 1014 { 1015 int ret; 1016 u64 value; 1017 1018 if (!pr) 1019 return -EINVAL; 1020 1021 if ((state < 0) || (state > (pr->throttling.state_count - 1))) 1022 return -EINVAL; 1023 1024 if (!pr->flags.throttling) 1025 return -ENODEV; 1026 1027 if (!force && (state == pr->throttling.state)) 1028 return 0; 1029 1030 if (state < pr->throttling_platform_limit) 1031 return -EPERM; 1032 1033 value = 0; 1034 ret = acpi_get_throttling_value(pr, state, &value); 1035 if (ret >= 0) { 1036 acpi_write_throttling_state(pr, value); 1037 pr->throttling.state = state; 1038 } 1039 1040 return 0; 1041 } 1042 1043 static long acpi_processor_throttling_fn(void *data) 1044 { 1045 struct acpi_processor_throttling_arg *arg = data; 1046 struct acpi_processor *pr = arg->pr; 1047 1048 return pr->throttling.acpi_processor_set_throttling(pr, 1049 arg->target_state, arg->force); 1050 } 1051 1052 static int __acpi_processor_set_throttling(struct acpi_processor *pr, 1053 int state, bool force, bool direct) 1054 { 1055 int ret = 0; 1056 unsigned int i; 1057 struct acpi_processor *match_pr; 1058 struct acpi_processor_throttling *p_throttling; 1059 struct acpi_processor_throttling_arg arg; 1060 struct throttling_tstate t_state; 1061 1062 if (!pr) 1063 return -EINVAL; 1064 1065 if (!pr->flags.throttling) 1066 return -ENODEV; 1067 1068 if ((state < 0) || (state > (pr->throttling.state_count - 1))) 1069 return -EINVAL; 1070 1071 if (cpu_is_offline(pr->id)) { 1072 /* 1073 * the cpu pointed by pr->id is offline. Unnecessary to change 1074 * the throttling state any more. 1075 */ 1076 return -ENODEV; 1077 } 1078 1079 t_state.target_state = state; 1080 p_throttling = &(pr->throttling); 1081 1082 /* 1083 * The throttling notifier will be called for every 1084 * affected cpu in order to get one proper T-state. 1085 * The notifier event is THROTTLING_PRECHANGE. 1086 */ 1087 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) { 1088 t_state.cpu = i; 1089 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE, 1090 &t_state); 1091 } 1092 /* 1093 * The function of acpi_processor_set_throttling will be called 1094 * to switch T-state. If the coordination type is SW_ALL or HW_ALL, 1095 * it is necessary to call it for every affected cpu. Otherwise 1096 * it can be called only for the cpu pointed by pr. 1097 */ 1098 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) { 1099 arg.pr = pr; 1100 arg.target_state = state; 1101 arg.force = force; 1102 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, &arg, 1103 direct); 1104 } else { 1105 /* 1106 * When the T-state coordination is SW_ALL or HW_ALL, 1107 * it is necessary to set T-state for every affected 1108 * cpus. 1109 */ 1110 for_each_cpu_and(i, cpu_online_mask, 1111 p_throttling->shared_cpu_map) { 1112 match_pr = per_cpu(processors, i); 1113 /* 1114 * If the pointer is invalid, we will report the 1115 * error message and continue. 1116 */ 1117 if (!match_pr) { 1118 acpi_handle_debug(pr->handle, 1119 "Invalid Pointer for CPU %d\n", i); 1120 continue; 1121 } 1122 /* 1123 * If the throttling control is unsupported on CPU i, 1124 * we will report the error message and continue. 1125 */ 1126 if (!match_pr->flags.throttling) { 1127 acpi_handle_debug(pr->handle, 1128 "Throttling Control unsupported on CPU %d\n", i); 1129 continue; 1130 } 1131 1132 arg.pr = match_pr; 1133 arg.target_state = state; 1134 arg.force = force; 1135 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, 1136 &arg, direct); 1137 } 1138 } 1139 /* 1140 * After the set_throttling is called, the 1141 * throttling notifier is called for every 1142 * affected cpu to update the T-states. 1143 * The notifier event is THROTTLING_POSTCHANGE 1144 */ 1145 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) { 1146 t_state.cpu = i; 1147 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE, 1148 &t_state); 1149 } 1150 1151 return ret; 1152 } 1153 1154 int acpi_processor_set_throttling(struct acpi_processor *pr, int state, 1155 bool force) 1156 { 1157 return __acpi_processor_set_throttling(pr, state, force, false); 1158 } 1159 1160 int acpi_processor_get_throttling_info(struct acpi_processor *pr) 1161 { 1162 int result = 0; 1163 struct acpi_processor_throttling *pthrottling; 1164 1165 acpi_handle_debug(pr->handle, 1166 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n", 1167 pr->throttling.address, 1168 pr->throttling.duty_offset, 1169 pr->throttling.duty_width); 1170 1171 /* 1172 * Evaluate _PTC, _TSS and _TPC 1173 * They must all be present or none of them can be used. 1174 */ 1175 if (acpi_processor_get_throttling_control(pr) || 1176 acpi_processor_get_throttling_states(pr) || 1177 acpi_processor_get_platform_limit(pr)) { 1178 pr->throttling.acpi_processor_get_throttling = 1179 &acpi_processor_get_throttling_fadt; 1180 pr->throttling.acpi_processor_set_throttling = 1181 &acpi_processor_set_throttling_fadt; 1182 if (acpi_processor_get_fadt_info(pr)) 1183 return 0; 1184 } else { 1185 pr->throttling.acpi_processor_get_throttling = 1186 &acpi_processor_get_throttling_ptc; 1187 pr->throttling.acpi_processor_set_throttling = 1188 &acpi_processor_set_throttling_ptc; 1189 } 1190 1191 /* 1192 * If TSD package for one CPU can't be parsed successfully, it means 1193 * that this CPU will have no coordination with other CPUs. 1194 */ 1195 if (acpi_processor_get_tsd(pr)) { 1196 pthrottling = &pr->throttling; 1197 pthrottling->tsd_valid_flag = 0; 1198 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map); 1199 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL; 1200 } 1201 1202 /* 1203 * PIIX4 Errata: We don't support throttling on the original PIIX4. 1204 * This shouldn't be an issue as few (if any) mobile systems ever 1205 * used this part. 1206 */ 1207 if (errata.piix4.throttle) { 1208 acpi_handle_debug(pr->handle, 1209 "Throttling not supported on PIIX4 A- or B-step\n"); 1210 return 0; 1211 } 1212 1213 acpi_handle_debug(pr->handle, "Found %d throttling states\n", 1214 pr->throttling.state_count); 1215 1216 pr->flags.throttling = 1; 1217 1218 /* 1219 * Disable throttling (if enabled). We'll let subsequent policy (e.g. 1220 * thermal) decide to lower performance if it so chooses, but for now 1221 * we'll crank up the speed. 1222 */ 1223 1224 result = acpi_processor_get_throttling(pr); 1225 if (result) 1226 goto end; 1227 1228 if (pr->throttling.state) { 1229 acpi_handle_debug(pr->handle, 1230 "Disabling throttling (was T%d)\n", 1231 pr->throttling.state); 1232 result = acpi_processor_set_throttling(pr, 0, false); 1233 if (result) 1234 goto end; 1235 } 1236 1237 end: 1238 if (result) 1239 pr->flags.throttling = 0; 1240 1241 return result; 1242 } 1243 1244