Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * processor_throttling.c - Throttling submodule of the ACPI processor driver
0004  *
0005  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
0006  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
0007  *  Copyright (C) 2004       Dominik Brodowski <linux@brodo.de>
0008  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
0009  *                      - Added processor hotplug support
0010  */
0011 
0012 #define pr_fmt(fmt) "ACPI: " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 #include <linux/init.h>
0018 #include <linux/sched.h>
0019 #include <linux/cpufreq.h>
0020 #include <linux/acpi.h>
0021 #include <acpi/processor.h>
0022 #include <asm/io.h>
0023 #include <linux/uaccess.h>
0024 
0025 /* ignore_tpc:
0026  *  0 -> acpi processor driver doesn't ignore _TPC values
0027  *  1 -> acpi processor driver ignores _TPC values
0028  */
0029 static int ignore_tpc;
0030 module_param(ignore_tpc, int, 0644);
0031 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
0032 
0033 struct throttling_tstate {
0034     unsigned int cpu;       /* cpu nr */
0035     int target_state;       /* target T-state */
0036 };
0037 
0038 struct acpi_processor_throttling_arg {
0039     struct acpi_processor *pr;
0040     int target_state;
0041     bool force;
0042 };
0043 
0044 #define THROTTLING_PRECHANGE       (1)
0045 #define THROTTLING_POSTCHANGE      (2)
0046 
0047 static int acpi_processor_get_throttling(struct acpi_processor *pr);
0048 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
0049                        int state, bool force, bool direct);
0050 
0051 static int acpi_processor_update_tsd_coord(void)
0052 {
0053     int count, count_target;
0054     int retval = 0;
0055     unsigned int i, j;
0056     cpumask_var_t covered_cpus;
0057     struct acpi_processor *pr, *match_pr;
0058     struct acpi_tsd_package *pdomain, *match_pdomain;
0059     struct acpi_processor_throttling *pthrottling, *match_pthrottling;
0060 
0061     if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
0062         return -ENOMEM;
0063 
0064     /*
0065      * Now that we have _TSD data from all CPUs, lets setup T-state
0066      * coordination between all CPUs.
0067      */
0068     for_each_possible_cpu(i) {
0069         pr = per_cpu(processors, i);
0070         if (!pr)
0071             continue;
0072 
0073         /* Basic validity check for domain info */
0074         pthrottling = &(pr->throttling);
0075 
0076         /*
0077          * If tsd package for one cpu is invalid, the coordination
0078          * among all CPUs is thought as invalid.
0079          * Maybe it is ugly.
0080          */
0081         if (!pthrottling->tsd_valid_flag) {
0082             retval = -EINVAL;
0083             break;
0084         }
0085     }
0086     if (retval)
0087         goto err_ret;
0088 
0089     for_each_possible_cpu(i) {
0090         pr = per_cpu(processors, i);
0091         if (!pr)
0092             continue;
0093 
0094         if (cpumask_test_cpu(i, covered_cpus))
0095             continue;
0096         pthrottling = &pr->throttling;
0097 
0098         pdomain = &(pthrottling->domain_info);
0099         cpumask_set_cpu(i, pthrottling->shared_cpu_map);
0100         cpumask_set_cpu(i, covered_cpus);
0101         /*
0102          * If the number of processor in the TSD domain is 1, it is
0103          * unnecessary to parse the coordination for this CPU.
0104          */
0105         if (pdomain->num_processors <= 1)
0106             continue;
0107 
0108         /* Validate the Domain info */
0109         count_target = pdomain->num_processors;
0110         count = 1;
0111 
0112         for_each_possible_cpu(j) {
0113             if (i == j)
0114                 continue;
0115 
0116             match_pr = per_cpu(processors, j);
0117             if (!match_pr)
0118                 continue;
0119 
0120             match_pthrottling = &(match_pr->throttling);
0121             match_pdomain = &(match_pthrottling->domain_info);
0122             if (match_pdomain->domain != pdomain->domain)
0123                 continue;
0124 
0125             /* Here i and j are in the same domain.
0126              * If two TSD packages have the same domain, they
0127              * should have the same num_porcessors and
0128              * coordination type. Otherwise it will be regarded
0129              * as illegal.
0130              */
0131             if (match_pdomain->num_processors != count_target) {
0132                 retval = -EINVAL;
0133                 goto err_ret;
0134             }
0135 
0136             if (pdomain->coord_type != match_pdomain->coord_type) {
0137                 retval = -EINVAL;
0138                 goto err_ret;
0139             }
0140 
0141             cpumask_set_cpu(j, covered_cpus);
0142             cpumask_set_cpu(j, pthrottling->shared_cpu_map);
0143             count++;
0144         }
0145         for_each_possible_cpu(j) {
0146             if (i == j)
0147                 continue;
0148 
0149             match_pr = per_cpu(processors, j);
0150             if (!match_pr)
0151                 continue;
0152 
0153             match_pthrottling = &(match_pr->throttling);
0154             match_pdomain = &(match_pthrottling->domain_info);
0155             if (match_pdomain->domain != pdomain->domain)
0156                 continue;
0157 
0158             /*
0159              * If some CPUS have the same domain, they
0160              * will have the same shared_cpu_map.
0161              */
0162             cpumask_copy(match_pthrottling->shared_cpu_map,
0163                      pthrottling->shared_cpu_map);
0164         }
0165     }
0166 
0167 err_ret:
0168     free_cpumask_var(covered_cpus);
0169 
0170     for_each_possible_cpu(i) {
0171         pr = per_cpu(processors, i);
0172         if (!pr)
0173             continue;
0174 
0175         /*
0176          * Assume no coordination on any error parsing domain info.
0177          * The coordination type will be forced as SW_ALL.
0178          */
0179         if (retval) {
0180             pthrottling = &(pr->throttling);
0181             cpumask_clear(pthrottling->shared_cpu_map);
0182             cpumask_set_cpu(i, pthrottling->shared_cpu_map);
0183             pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
0184         }
0185     }
0186 
0187     return retval;
0188 }
0189 
0190 /*
0191  * Update the T-state coordination after the _TSD
0192  * data for all cpus is obtained.
0193  */
0194 void acpi_processor_throttling_init(void)
0195 {
0196     if (acpi_processor_update_tsd_coord())
0197         pr_debug("Assume no T-state coordination\n");
0198 }
0199 
0200 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
0201 {
0202     struct throttling_tstate *p_tstate = data;
0203     struct acpi_processor *pr;
0204     unsigned int cpu;
0205     int target_state;
0206     struct acpi_processor_limit *p_limit;
0207     struct acpi_processor_throttling *p_throttling;
0208 
0209     cpu = p_tstate->cpu;
0210     pr = per_cpu(processors, cpu);
0211     if (!pr) {
0212         pr_debug("Invalid pr pointer\n");
0213         return 0;
0214     }
0215     if (!pr->flags.throttling) {
0216         acpi_handle_debug(pr->handle,
0217                   "Throttling control unsupported on CPU %d\n",
0218                   cpu);
0219         return 0;
0220     }
0221     target_state = p_tstate->target_state;
0222     p_throttling = &(pr->throttling);
0223     switch (event) {
0224     case THROTTLING_PRECHANGE:
0225         /*
0226          * Prechange event is used to choose one proper t-state,
0227          * which meets the limits of thermal, user and _TPC.
0228          */
0229         p_limit = &pr->limit;
0230         if (p_limit->thermal.tx > target_state)
0231             target_state = p_limit->thermal.tx;
0232         if (p_limit->user.tx > target_state)
0233             target_state = p_limit->user.tx;
0234         if (pr->throttling_platform_limit > target_state)
0235             target_state = pr->throttling_platform_limit;
0236         if (target_state >= p_throttling->state_count) {
0237             pr_warn("Exceed the limit of T-state \n");
0238             target_state = p_throttling->state_count - 1;
0239         }
0240         p_tstate->target_state = target_state;
0241         acpi_handle_debug(pr->handle,
0242                   "PreChange Event: target T-state of CPU %d is T%d\n",
0243                   cpu, target_state);
0244         break;
0245     case THROTTLING_POSTCHANGE:
0246         /*
0247          * Postchange event is only used to update the
0248          * T-state flag of acpi_processor_throttling.
0249          */
0250         p_throttling->state = target_state;
0251         acpi_handle_debug(pr->handle,
0252                   "PostChange Event: CPU %d is switched to T%d\n",
0253                   cpu, target_state);
0254         break;
0255     default:
0256         pr_warn("Unsupported Throttling notifier event\n");
0257         break;
0258     }
0259 
0260     return 0;
0261 }
0262 
0263 /*
0264  * _TPC - Throttling Present Capabilities
0265  */
0266 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
0267 {
0268     acpi_status status = 0;
0269     unsigned long long tpc = 0;
0270 
0271     if (!pr)
0272         return -EINVAL;
0273 
0274     if (ignore_tpc)
0275         goto end;
0276 
0277     status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
0278     if (ACPI_FAILURE(status)) {
0279         if (status != AE_NOT_FOUND)
0280             acpi_evaluation_failure_warn(pr->handle, "_TPC", status);
0281 
0282         return -ENODEV;
0283     }
0284 
0285 end:
0286     pr->throttling_platform_limit = (int)tpc;
0287     return 0;
0288 }
0289 
0290 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
0291 {
0292     int result = 0;
0293     int throttling_limit;
0294     int current_state;
0295     struct acpi_processor_limit *limit;
0296     int target_state;
0297 
0298     if (ignore_tpc)
0299         return 0;
0300 
0301     result = acpi_processor_get_platform_limit(pr);
0302     if (result) {
0303         /* Throttling Limit is unsupported */
0304         return result;
0305     }
0306 
0307     throttling_limit = pr->throttling_platform_limit;
0308     if (throttling_limit >= pr->throttling.state_count) {
0309         /* Uncorrect Throttling Limit */
0310         return -EINVAL;
0311     }
0312 
0313     current_state = pr->throttling.state;
0314     if (current_state > throttling_limit) {
0315         /*
0316          * The current state can meet the requirement of
0317          * _TPC limit. But it is reasonable that OSPM changes
0318          * t-states from high to low for better performance.
0319          * Of course the limit condition of thermal
0320          * and user should be considered.
0321          */
0322         limit = &pr->limit;
0323         target_state = throttling_limit;
0324         if (limit->thermal.tx > target_state)
0325             target_state = limit->thermal.tx;
0326         if (limit->user.tx > target_state)
0327             target_state = limit->user.tx;
0328     } else if (current_state == throttling_limit) {
0329         /*
0330          * Unnecessary to change the throttling state
0331          */
0332         return 0;
0333     } else {
0334         /*
0335          * If the current state is lower than the limit of _TPC, it
0336          * will be forced to switch to the throttling state defined
0337          * by throttling_platfor_limit.
0338          * Because the previous state meets with the limit condition
0339          * of thermal and user, it is unnecessary to check it again.
0340          */
0341         target_state = throttling_limit;
0342     }
0343     return acpi_processor_set_throttling(pr, target_state, false);
0344 }
0345 
0346 /*
0347  * This function is used to reevaluate whether the T-state is valid
0348  * after one CPU is onlined/offlined.
0349  * It is noted that it won't reevaluate the following properties for
0350  * the T-state.
0351  *  1. Control method.
0352  *  2. the number of supported T-state
0353  *  3. TSD domain
0354  */
0355 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
0356                     bool is_dead)
0357 {
0358     int result = 0;
0359 
0360     if (is_dead) {
0361         /* When one CPU is offline, the T-state throttling
0362          * will be invalidated.
0363          */
0364         pr->flags.throttling = 0;
0365         return;
0366     }
0367     /* the following is to recheck whether the T-state is valid for
0368      * the online CPU
0369      */
0370     if (!pr->throttling.state_count) {
0371         /* If the number of T-state is invalid, it is
0372          * invalidated.
0373          */
0374         pr->flags.throttling = 0;
0375         return;
0376     }
0377     pr->flags.throttling = 1;
0378 
0379     /* Disable throttling (if enabled).  We'll let subsequent
0380      * policy (e.g.thermal) decide to lower performance if it
0381      * so chooses, but for now we'll crank up the speed.
0382      */
0383 
0384     result = acpi_processor_get_throttling(pr);
0385     if (result)
0386         goto end;
0387 
0388     if (pr->throttling.state) {
0389         result = acpi_processor_set_throttling(pr, 0, false);
0390         if (result)
0391             goto end;
0392     }
0393 
0394 end:
0395     if (result)
0396         pr->flags.throttling = 0;
0397 }
0398 /*
0399  * _PTC - Processor Throttling Control (and status) register location
0400  */
0401 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
0402 {
0403     int result = 0;
0404     acpi_status status = 0;
0405     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0406     union acpi_object *ptc = NULL;
0407     union acpi_object obj;
0408     struct acpi_processor_throttling *throttling;
0409 
0410     status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
0411     if (ACPI_FAILURE(status)) {
0412         if (status != AE_NOT_FOUND)
0413             acpi_evaluation_failure_warn(pr->handle, "_PTC", status);
0414 
0415         return -ENODEV;
0416     }
0417 
0418     ptc = (union acpi_object *)buffer.pointer;
0419     if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
0420         || (ptc->package.count != 2)) {
0421         pr_err("Invalid _PTC data\n");
0422         result = -EFAULT;
0423         goto end;
0424     }
0425 
0426     /*
0427      * control_register
0428      */
0429 
0430     obj = ptc->package.elements[0];
0431 
0432     if ((obj.type != ACPI_TYPE_BUFFER)
0433         || (obj.buffer.length < sizeof(struct acpi_ptc_register))
0434         || (obj.buffer.pointer == NULL)) {
0435         pr_err("Invalid _PTC data (control_register)\n");
0436         result = -EFAULT;
0437         goto end;
0438     }
0439     memcpy(&pr->throttling.control_register, obj.buffer.pointer,
0440            sizeof(struct acpi_ptc_register));
0441 
0442     /*
0443      * status_register
0444      */
0445 
0446     obj = ptc->package.elements[1];
0447 
0448     if ((obj.type != ACPI_TYPE_BUFFER)
0449         || (obj.buffer.length < sizeof(struct acpi_ptc_register))
0450         || (obj.buffer.pointer == NULL)) {
0451         pr_err("Invalid _PTC data (status_register)\n");
0452         result = -EFAULT;
0453         goto end;
0454     }
0455 
0456     memcpy(&pr->throttling.status_register, obj.buffer.pointer,
0457            sizeof(struct acpi_ptc_register));
0458 
0459     throttling = &pr->throttling;
0460 
0461     if ((throttling->control_register.bit_width +
0462         throttling->control_register.bit_offset) > 32) {
0463         pr_err("Invalid _PTC control register\n");
0464         result = -EFAULT;
0465         goto end;
0466     }
0467 
0468     if ((throttling->status_register.bit_width +
0469         throttling->status_register.bit_offset) > 32) {
0470         pr_err("Invalid _PTC status register\n");
0471         result = -EFAULT;
0472         goto end;
0473     }
0474 
0475 end:
0476     kfree(buffer.pointer);
0477 
0478     return result;
0479 }
0480 
0481 /*
0482  * _TSS - Throttling Supported States
0483  */
0484 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
0485 {
0486     int result = 0;
0487     acpi_status status = AE_OK;
0488     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0489     struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
0490     struct acpi_buffer state = { 0, NULL };
0491     union acpi_object *tss = NULL;
0492     int i;
0493 
0494     status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
0495     if (ACPI_FAILURE(status)) {
0496         if (status != AE_NOT_FOUND)
0497             acpi_evaluation_failure_warn(pr->handle, "_TSS", status);
0498 
0499         return -ENODEV;
0500     }
0501 
0502     tss = buffer.pointer;
0503     if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
0504         pr_err("Invalid _TSS data\n");
0505         result = -EFAULT;
0506         goto end;
0507     }
0508 
0509     acpi_handle_debug(pr->handle, "Found %d throttling states\n",
0510               tss->package.count);
0511 
0512     pr->throttling.state_count = tss->package.count;
0513     pr->throttling.states_tss =
0514         kmalloc_array(tss->package.count,
0515               sizeof(struct acpi_processor_tx_tss),
0516               GFP_KERNEL);
0517     if (!pr->throttling.states_tss) {
0518         result = -ENOMEM;
0519         goto end;
0520     }
0521 
0522     for (i = 0; i < pr->throttling.state_count; i++) {
0523 
0524         struct acpi_processor_tx_tss *tx =
0525             (struct acpi_processor_tx_tss *)&(pr->throttling.
0526                               states_tss[i]);
0527 
0528         state.length = sizeof(struct acpi_processor_tx_tss);
0529         state.pointer = tx;
0530 
0531         acpi_handle_debug(pr->handle, "Extracting state %d\n", i);
0532 
0533         status = acpi_extract_package(&(tss->package.elements[i]),
0534                           &format, &state);
0535         if (ACPI_FAILURE(status)) {
0536             acpi_handle_warn(pr->handle, "Invalid _TSS data: %s\n",
0537                      acpi_format_exception(status));
0538             result = -EFAULT;
0539             kfree(pr->throttling.states_tss);
0540             goto end;
0541         }
0542 
0543         if (!tx->freqpercentage) {
0544             pr_err("Invalid _TSS data: freq is zero\n");
0545             result = -EFAULT;
0546             kfree(pr->throttling.states_tss);
0547             goto end;
0548         }
0549     }
0550 
0551 end:
0552     kfree(buffer.pointer);
0553 
0554     return result;
0555 }
0556 
0557 /*
0558  * _TSD - T-State Dependencies
0559  */
0560 static int acpi_processor_get_tsd(struct acpi_processor *pr)
0561 {
0562     int result = 0;
0563     acpi_status status = AE_OK;
0564     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0565     struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
0566     struct acpi_buffer state = { 0, NULL };
0567     union acpi_object *tsd = NULL;
0568     struct acpi_tsd_package *pdomain;
0569     struct acpi_processor_throttling *pthrottling;
0570 
0571     pthrottling = &pr->throttling;
0572     pthrottling->tsd_valid_flag = 0;
0573 
0574     status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
0575     if (ACPI_FAILURE(status)) {
0576         if (status != AE_NOT_FOUND)
0577             acpi_evaluation_failure_warn(pr->handle, "_TSD", status);
0578 
0579         return -ENODEV;
0580     }
0581 
0582     tsd = buffer.pointer;
0583     if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
0584         pr_err("Invalid _TSD data\n");
0585         result = -EFAULT;
0586         goto end;
0587     }
0588 
0589     if (tsd->package.count != 1) {
0590         pr_err("Invalid _TSD data\n");
0591         result = -EFAULT;
0592         goto end;
0593     }
0594 
0595     pdomain = &(pr->throttling.domain_info);
0596 
0597     state.length = sizeof(struct acpi_tsd_package);
0598     state.pointer = pdomain;
0599 
0600     status = acpi_extract_package(&(tsd->package.elements[0]),
0601                       &format, &state);
0602     if (ACPI_FAILURE(status)) {
0603         pr_err("Invalid _TSD data\n");
0604         result = -EFAULT;
0605         goto end;
0606     }
0607 
0608     if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
0609         pr_err("Unknown _TSD:num_entries\n");
0610         result = -EFAULT;
0611         goto end;
0612     }
0613 
0614     if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
0615         pr_err("Unknown _TSD:revision\n");
0616         result = -EFAULT;
0617         goto end;
0618     }
0619 
0620     pthrottling = &pr->throttling;
0621     pthrottling->tsd_valid_flag = 1;
0622     pthrottling->shared_type = pdomain->coord_type;
0623     cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
0624     /*
0625      * If the coordination type is not defined in ACPI spec,
0626      * the tsd_valid_flag will be clear and coordination type
0627      * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
0628      */
0629     if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
0630         pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
0631         pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
0632         pthrottling->tsd_valid_flag = 0;
0633         pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
0634     }
0635 
0636 end:
0637     kfree(buffer.pointer);
0638     return result;
0639 }
0640 
0641 /* --------------------------------------------------------------------------
0642                               Throttling Control
0643    -------------------------------------------------------------------------- */
0644 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
0645 {
0646     int state = 0;
0647     u32 value = 0;
0648     u32 duty_mask = 0;
0649     u32 duty_value = 0;
0650 
0651     if (!pr)
0652         return -EINVAL;
0653 
0654     if (!pr->flags.throttling)
0655         return -ENODEV;
0656 
0657     /*
0658      * We don't care about error returns - we just try to mark
0659      * these reserved so that nobody else is confused into thinking
0660      * that this region might be unused..
0661      *
0662      * (In particular, allocating the IO range for Cardbus)
0663      */
0664     request_region(pr->throttling.address, 6, "ACPI CPU throttle");
0665 
0666     pr->throttling.state = 0;
0667 
0668     duty_mask = pr->throttling.state_count - 1;
0669 
0670     duty_mask <<= pr->throttling.duty_offset;
0671 
0672     local_irq_disable();
0673 
0674     value = inl(pr->throttling.address);
0675 
0676     /*
0677      * Compute the current throttling state when throttling is enabled
0678      * (bit 4 is on).
0679      */
0680     if (value & 0x10) {
0681         duty_value = value & duty_mask;
0682         duty_value >>= pr->throttling.duty_offset;
0683 
0684         if (duty_value)
0685             state = pr->throttling.state_count - duty_value;
0686     }
0687 
0688     pr->throttling.state = state;
0689 
0690     local_irq_enable();
0691 
0692     acpi_handle_debug(pr->handle,
0693               "Throttling state is T%d (%d%% throttling applied)\n",
0694               state, pr->throttling.states[state].performance);
0695 
0696     return 0;
0697 }
0698 
0699 #ifdef CONFIG_X86
0700 static int acpi_throttling_rdmsr(u64 *value)
0701 {
0702     u64 msr_high, msr_low;
0703     u64 msr = 0;
0704     int ret = -1;
0705 
0706     if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
0707         !this_cpu_has(X86_FEATURE_ACPI)) {
0708         pr_err("HARDWARE addr space,NOT supported yet\n");
0709     } else {
0710         msr_low = 0;
0711         msr_high = 0;
0712         rdmsr_safe(MSR_IA32_THERM_CONTROL,
0713             (u32 *)&msr_low, (u32 *) &msr_high);
0714         msr = (msr_high << 32) | msr_low;
0715         *value = (u64) msr;
0716         ret = 0;
0717     }
0718     return ret;
0719 }
0720 
0721 static int acpi_throttling_wrmsr(u64 value)
0722 {
0723     int ret = -1;
0724     u64 msr;
0725 
0726     if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
0727         !this_cpu_has(X86_FEATURE_ACPI)) {
0728         pr_err("HARDWARE addr space,NOT supported yet\n");
0729     } else {
0730         msr = value;
0731         wrmsr_safe(MSR_IA32_THERM_CONTROL,
0732             msr & 0xffffffff, msr >> 32);
0733         ret = 0;
0734     }
0735     return ret;
0736 }
0737 #else
0738 static int acpi_throttling_rdmsr(u64 *value)
0739 {
0740     pr_err("HARDWARE addr space,NOT supported yet\n");
0741     return -1;
0742 }
0743 
0744 static int acpi_throttling_wrmsr(u64 value)
0745 {
0746     pr_err("HARDWARE addr space,NOT supported yet\n");
0747     return -1;
0748 }
0749 #endif
0750 
0751 static int acpi_read_throttling_status(struct acpi_processor *pr,
0752                     u64 *value)
0753 {
0754     u32 bit_width, bit_offset;
0755     u32 ptc_value;
0756     u64 ptc_mask;
0757     struct acpi_processor_throttling *throttling;
0758     int ret = -1;
0759 
0760     throttling = &pr->throttling;
0761     switch (throttling->status_register.space_id) {
0762     case ACPI_ADR_SPACE_SYSTEM_IO:
0763         bit_width = throttling->status_register.bit_width;
0764         bit_offset = throttling->status_register.bit_offset;
0765 
0766         acpi_os_read_port((acpi_io_address) throttling->status_register.
0767                   address, &ptc_value,
0768                   (u32) (bit_width + bit_offset));
0769         ptc_mask = (1 << bit_width) - 1;
0770         *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
0771         ret = 0;
0772         break;
0773     case ACPI_ADR_SPACE_FIXED_HARDWARE:
0774         ret = acpi_throttling_rdmsr(value);
0775         break;
0776     default:
0777         pr_err("Unknown addr space %d\n",
0778                (u32) (throttling->status_register.space_id));
0779     }
0780     return ret;
0781 }
0782 
0783 static int acpi_write_throttling_state(struct acpi_processor *pr,
0784                 u64 value)
0785 {
0786     u32 bit_width, bit_offset;
0787     u64 ptc_value;
0788     u64 ptc_mask;
0789     struct acpi_processor_throttling *throttling;
0790     int ret = -1;
0791 
0792     throttling = &pr->throttling;
0793     switch (throttling->control_register.space_id) {
0794     case ACPI_ADR_SPACE_SYSTEM_IO:
0795         bit_width = throttling->control_register.bit_width;
0796         bit_offset = throttling->control_register.bit_offset;
0797         ptc_mask = (1 << bit_width) - 1;
0798         ptc_value = value & ptc_mask;
0799 
0800         acpi_os_write_port((acpi_io_address) throttling->
0801                     control_register.address,
0802                     (u32) (ptc_value << bit_offset),
0803                     (u32) (bit_width + bit_offset));
0804         ret = 0;
0805         break;
0806     case ACPI_ADR_SPACE_FIXED_HARDWARE:
0807         ret = acpi_throttling_wrmsr(value);
0808         break;
0809     default:
0810         pr_err("Unknown addr space %d\n",
0811                (u32) (throttling->control_register.space_id));
0812     }
0813     return ret;
0814 }
0815 
0816 static int acpi_get_throttling_state(struct acpi_processor *pr,
0817                 u64 value)
0818 {
0819     int i;
0820 
0821     for (i = 0; i < pr->throttling.state_count; i++) {
0822         struct acpi_processor_tx_tss *tx =
0823             (struct acpi_processor_tx_tss *)&(pr->throttling.
0824                               states_tss[i]);
0825         if (tx->control == value)
0826             return i;
0827     }
0828     return -1;
0829 }
0830 
0831 static int acpi_get_throttling_value(struct acpi_processor *pr,
0832             int state, u64 *value)
0833 {
0834     int ret = -1;
0835 
0836     if (state >= 0 && state <= pr->throttling.state_count) {
0837         struct acpi_processor_tx_tss *tx =
0838             (struct acpi_processor_tx_tss *)&(pr->throttling.
0839                               states_tss[state]);
0840         *value = tx->control;
0841         ret = 0;
0842     }
0843     return ret;
0844 }
0845 
0846 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
0847 {
0848     int state = 0;
0849     int ret;
0850     u64 value;
0851 
0852     if (!pr)
0853         return -EINVAL;
0854 
0855     if (!pr->flags.throttling)
0856         return -ENODEV;
0857 
0858     pr->throttling.state = 0;
0859 
0860     value = 0;
0861     ret = acpi_read_throttling_status(pr, &value);
0862     if (ret >= 0) {
0863         state = acpi_get_throttling_state(pr, value);
0864         if (state == -1) {
0865             acpi_handle_debug(pr->handle,
0866                       "Invalid throttling state, reset\n");
0867             state = 0;
0868             ret = __acpi_processor_set_throttling(pr, state, true,
0869                                   true);
0870             if (ret)
0871                 return ret;
0872         }
0873         pr->throttling.state = state;
0874     }
0875 
0876     return 0;
0877 }
0878 
0879 static long __acpi_processor_get_throttling(void *data)
0880 {
0881     struct acpi_processor *pr = data;
0882 
0883     return pr->throttling.acpi_processor_get_throttling(pr);
0884 }
0885 
0886 static int acpi_processor_get_throttling(struct acpi_processor *pr)
0887 {
0888     if (!pr)
0889         return -EINVAL;
0890 
0891     if (!pr->flags.throttling)
0892         return -ENODEV;
0893 
0894     /*
0895      * This is either called from the CPU hotplug callback of
0896      * processor_driver or via the ACPI probe function. In the latter
0897      * case the CPU is not guaranteed to be online. Both call sites are
0898      * protected against CPU hotplug.
0899      */
0900     if (!cpu_online(pr->id))
0901         return -ENODEV;
0902 
0903     return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false);
0904 }
0905 
0906 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
0907 {
0908     int i, step;
0909 
0910     if (!pr->throttling.address) {
0911         acpi_handle_debug(pr->handle, "No throttling register\n");
0912         return -EINVAL;
0913     } else if (!pr->throttling.duty_width) {
0914         acpi_handle_debug(pr->handle, "No throttling states\n");
0915         return -EINVAL;
0916     }
0917     /* TBD: Support duty_cycle values that span bit 4. */
0918     else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
0919         pr_warn("duty_cycle spans bit 4\n");
0920         return -EINVAL;
0921     }
0922 
0923     pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
0924 
0925     /*
0926      * Compute state values. Note that throttling displays a linear power
0927      * performance relationship (at 50% performance the CPU will consume
0928      * 50% power).  Values are in 1/10th of a percent to preserve accuracy.
0929      */
0930 
0931     step = (1000 / pr->throttling.state_count);
0932 
0933     for (i = 0; i < pr->throttling.state_count; i++) {
0934         pr->throttling.states[i].performance = 1000 - step * i;
0935         pr->throttling.states[i].power = 1000 - step * i;
0936     }
0937     return 0;
0938 }
0939 
0940 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
0941                           int state, bool force)
0942 {
0943     u32 value = 0;
0944     u32 duty_mask = 0;
0945     u32 duty_value = 0;
0946 
0947     if (!pr)
0948         return -EINVAL;
0949 
0950     if ((state < 0) || (state > (pr->throttling.state_count - 1)))
0951         return -EINVAL;
0952 
0953     if (!pr->flags.throttling)
0954         return -ENODEV;
0955 
0956     if (!force && (state == pr->throttling.state))
0957         return 0;
0958 
0959     if (state < pr->throttling_platform_limit)
0960         return -EPERM;
0961     /*
0962      * Calculate the duty_value and duty_mask.
0963      */
0964     if (state) {
0965         duty_value = pr->throttling.state_count - state;
0966 
0967         duty_value <<= pr->throttling.duty_offset;
0968 
0969         /* Used to clear all duty_value bits */
0970         duty_mask = pr->throttling.state_count - 1;
0971 
0972         duty_mask <<= acpi_gbl_FADT.duty_offset;
0973         duty_mask = ~duty_mask;
0974     }
0975 
0976     local_irq_disable();
0977 
0978     /*
0979      * Disable throttling by writing a 0 to bit 4.  Note that we must
0980      * turn it off before you can change the duty_value.
0981      */
0982     value = inl(pr->throttling.address);
0983     if (value & 0x10) {
0984         value &= 0xFFFFFFEF;
0985         outl(value, pr->throttling.address);
0986     }
0987 
0988     /*
0989      * Write the new duty_value and then enable throttling.  Note
0990      * that a state value of 0 leaves throttling disabled.
0991      */
0992     if (state) {
0993         value &= duty_mask;
0994         value |= duty_value;
0995         outl(value, pr->throttling.address);
0996 
0997         value |= 0x00000010;
0998         outl(value, pr->throttling.address);
0999     }
1000 
1001     pr->throttling.state = state;
1002 
1003     local_irq_enable();
1004 
1005     acpi_handle_debug(pr->handle,
1006               "Throttling state set to T%d (%d%%)\n", state,
1007               (pr->throttling.states[state].performance ? pr->
1008                throttling.states[state].performance / 10 : 0));
1009 
1010     return 0;
1011 }
1012 
1013 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1014                          int state, bool force)
1015 {
1016     int ret;
1017     u64 value;
1018 
1019     if (!pr)
1020         return -EINVAL;
1021 
1022     if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1023         return -EINVAL;
1024 
1025     if (!pr->flags.throttling)
1026         return -ENODEV;
1027 
1028     if (!force && (state == pr->throttling.state))
1029         return 0;
1030 
1031     if (state < pr->throttling_platform_limit)
1032         return -EPERM;
1033 
1034     value = 0;
1035     ret = acpi_get_throttling_value(pr, state, &value);
1036     if (ret >= 0) {
1037         acpi_write_throttling_state(pr, value);
1038         pr->throttling.state = state;
1039     }
1040 
1041     return 0;
1042 }
1043 
1044 static long acpi_processor_throttling_fn(void *data)
1045 {
1046     struct acpi_processor_throttling_arg *arg = data;
1047     struct acpi_processor *pr = arg->pr;
1048 
1049     return pr->throttling.acpi_processor_set_throttling(pr,
1050             arg->target_state, arg->force);
1051 }
1052 
1053 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
1054                        int state, bool force, bool direct)
1055 {
1056     int ret = 0;
1057     unsigned int i;
1058     struct acpi_processor *match_pr;
1059     struct acpi_processor_throttling *p_throttling;
1060     struct acpi_processor_throttling_arg arg;
1061     struct throttling_tstate t_state;
1062 
1063     if (!pr)
1064         return -EINVAL;
1065 
1066     if (!pr->flags.throttling)
1067         return -ENODEV;
1068 
1069     if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1070         return -EINVAL;
1071 
1072     if (cpu_is_offline(pr->id)) {
1073         /*
1074          * the cpu pointed by pr->id is offline. Unnecessary to change
1075          * the throttling state any more.
1076          */
1077         return -ENODEV;
1078     }
1079 
1080     t_state.target_state = state;
1081     p_throttling = &(pr->throttling);
1082 
1083     /*
1084      * The throttling notifier will be called for every
1085      * affected cpu in order to get one proper T-state.
1086      * The notifier event is THROTTLING_PRECHANGE.
1087      */
1088     for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1089         t_state.cpu = i;
1090         acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1091                             &t_state);
1092     }
1093     /*
1094      * The function of acpi_processor_set_throttling will be called
1095      * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1096      * it is necessary to call it for every affected cpu. Otherwise
1097      * it can be called only for the cpu pointed by pr.
1098      */
1099     if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1100         arg.pr = pr;
1101         arg.target_state = state;
1102         arg.force = force;
1103         ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, &arg,
1104                   direct);
1105     } else {
1106         /*
1107          * When the T-state coordination is SW_ALL or HW_ALL,
1108          * it is necessary to set T-state for every affected
1109          * cpus.
1110          */
1111         for_each_cpu_and(i, cpu_online_mask,
1112             p_throttling->shared_cpu_map) {
1113             match_pr = per_cpu(processors, i);
1114             /*
1115              * If the pointer is invalid, we will report the
1116              * error message and continue.
1117              */
1118             if (!match_pr) {
1119                 acpi_handle_debug(pr->handle,
1120                     "Invalid Pointer for CPU %d\n", i);
1121                 continue;
1122             }
1123             /*
1124              * If the throttling control is unsupported on CPU i,
1125              * we will report the error message and continue.
1126              */
1127             if (!match_pr->flags.throttling) {
1128                 acpi_handle_debug(pr->handle,
1129                     "Throttling Control unsupported on CPU %d\n", i);
1130                 continue;
1131             }
1132 
1133             arg.pr = match_pr;
1134             arg.target_state = state;
1135             arg.force = force;
1136             ret = call_on_cpu(pr->id, acpi_processor_throttling_fn,
1137                       &arg, direct);
1138         }
1139     }
1140     /*
1141      * After the set_throttling is called, the
1142      * throttling notifier is called for every
1143      * affected cpu to update the T-states.
1144      * The notifier event is THROTTLING_POSTCHANGE
1145      */
1146     for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1147         t_state.cpu = i;
1148         acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1149                             &t_state);
1150     }
1151 
1152     return ret;
1153 }
1154 
1155 int acpi_processor_set_throttling(struct acpi_processor *pr, int state,
1156                   bool force)
1157 {
1158     return __acpi_processor_set_throttling(pr, state, force, false);
1159 }
1160 
1161 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1162 {
1163     int result = 0;
1164     struct acpi_processor_throttling *pthrottling;
1165 
1166     acpi_handle_debug(pr->handle,
1167               "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1168               pr->throttling.address,
1169               pr->throttling.duty_offset,
1170               pr->throttling.duty_width);
1171 
1172     /*
1173      * Evaluate _PTC, _TSS and _TPC
1174      * They must all be present or none of them can be used.
1175      */
1176     if (acpi_processor_get_throttling_control(pr) ||
1177         acpi_processor_get_throttling_states(pr) ||
1178         acpi_processor_get_platform_limit(pr)) {
1179         pr->throttling.acpi_processor_get_throttling =
1180             &acpi_processor_get_throttling_fadt;
1181         pr->throttling.acpi_processor_set_throttling =
1182             &acpi_processor_set_throttling_fadt;
1183         if (acpi_processor_get_fadt_info(pr))
1184             return 0;
1185     } else {
1186         pr->throttling.acpi_processor_get_throttling =
1187             &acpi_processor_get_throttling_ptc;
1188         pr->throttling.acpi_processor_set_throttling =
1189             &acpi_processor_set_throttling_ptc;
1190     }
1191 
1192     /*
1193      * If TSD package for one CPU can't be parsed successfully, it means
1194      * that this CPU will have no coordination with other CPUs.
1195      */
1196     if (acpi_processor_get_tsd(pr)) {
1197         pthrottling = &pr->throttling;
1198         pthrottling->tsd_valid_flag = 0;
1199         cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1200         pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1201     }
1202 
1203     /*
1204      * PIIX4 Errata: We don't support throttling on the original PIIX4.
1205      * This shouldn't be an issue as few (if any) mobile systems ever
1206      * used this part.
1207      */
1208     if (errata.piix4.throttle) {
1209         acpi_handle_debug(pr->handle,
1210                   "Throttling not supported on PIIX4 A- or B-step\n");
1211         return 0;
1212     }
1213 
1214     acpi_handle_debug(pr->handle, "Found %d throttling states\n",
1215               pr->throttling.state_count);
1216 
1217     pr->flags.throttling = 1;
1218 
1219     /*
1220      * Disable throttling (if enabled).  We'll let subsequent policy (e.g.
1221      * thermal) decide to lower performance if it so chooses, but for now
1222      * we'll crank up the speed.
1223      */
1224 
1225     result = acpi_processor_get_throttling(pr);
1226     if (result)
1227         goto end;
1228 
1229     if (pr->throttling.state) {
1230         acpi_handle_debug(pr->handle,
1231                   "Disabling throttling (was T%d)\n",
1232                   pr->throttling.state);
1233         result = acpi_processor_set_throttling(pr, 0, false);
1234         if (result)
1235             goto end;
1236     }
1237 
1238 end:
1239     if (result)
1240         pr->flags.throttling = 0;
1241 
1242     return result;
1243 }
1244