Path: blob/master/drivers/acpi/processor_throttling.c
15109 views
/*1* processor_throttling.c - Throttling submodule of the ACPI processor driver2*3* Copyright (C) 2001, 2002 Andy Grover <[email protected]>4* Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>5* Copyright (C) 2004 Dominik Brodowski <[email protected]>6* Copyright (C) 2004 Anil S Keshavamurthy <[email protected]>7* - Added processor hotplug support8*9* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2 of the License, or (at14* your option) any later version.15*16* This program is distributed in the hope that it will be useful, but17* WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU19* General Public License for more details.20*21* You should have received a copy of the GNU General Public License along22* with this program; if not, write to the Free Software Foundation, Inc.,23* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.24*25* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~26*/2728#include <linux/kernel.h>29#include <linux/module.h>30#include <linux/slab.h>31#include <linux/init.h>32#include <linux/sched.h>33#include <linux/cpufreq.h>3435#include <asm/io.h>36#include <asm/uaccess.h>3738#include <acpi/acpi_bus.h>39#include <acpi/acpi_drivers.h>40#include <acpi/processor.h>4142#define PREFIX "ACPI: "4344#define ACPI_PROCESSOR_CLASS "processor"45#define _COMPONENT ACPI_PROCESSOR_COMPONENT46ACPI_MODULE_NAME("processor_throttling");4748/* ignore_tpc:49* 0 -> acpi processor driver doesn't ignore _TPC values50* 1 -> acpi processor driver ignores _TPC values51*/52static int ignore_tpc;53module_param(ignore_tpc, int, 0644);54MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");5556struct throttling_tstate {57unsigned int cpu; /* cpu nr */58int target_state; /* target T-state */59};6061#define THROTTLING_PRECHANGE (1)62#define THROTTLING_POSTCHANGE (2)6364static int acpi_processor_get_throttling(struct acpi_processor *pr);65int acpi_processor_set_throttling(struct acpi_processor *pr,66int state, bool force);6768static int acpi_processor_update_tsd_coord(void)69{70int count, count_target;71int retval = 0;72unsigned int i, j;73cpumask_var_t covered_cpus;74struct acpi_processor *pr, *match_pr;75struct acpi_tsd_package *pdomain, *match_pdomain;76struct acpi_processor_throttling *pthrottling, *match_pthrottling;7778if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))79return -ENOMEM;8081/*82* Now that we have _TSD data from all CPUs, lets setup T-state83* coordination between all CPUs.84*/85for_each_possible_cpu(i) {86pr = per_cpu(processors, i);87if (!pr)88continue;8990/* Basic validity check for domain info */91pthrottling = &(pr->throttling);9293/*94* If tsd package for one cpu is invalid, the coordination95* among all CPUs is thought as invalid.96* Maybe it is ugly.97*/98if (!pthrottling->tsd_valid_flag) {99retval = -EINVAL;100break;101}102}103if (retval)104goto err_ret;105106for_each_possible_cpu(i) {107pr = per_cpu(processors, i);108if (!pr)109continue;110111if (cpumask_test_cpu(i, covered_cpus))112continue;113pthrottling = &pr->throttling;114115pdomain = &(pthrottling->domain_info);116cpumask_set_cpu(i, pthrottling->shared_cpu_map);117cpumask_set_cpu(i, covered_cpus);118/*119* If the number of processor in the TSD domain is 1, it is120* unnecessary to parse the coordination for this CPU.121*/122if (pdomain->num_processors <= 1)123continue;124125/* Validate the Domain info */126count_target = pdomain->num_processors;127count = 1;128129for_each_possible_cpu(j) {130if (i == j)131continue;132133match_pr = per_cpu(processors, j);134if (!match_pr)135continue;136137match_pthrottling = &(match_pr->throttling);138match_pdomain = &(match_pthrottling->domain_info);139if (match_pdomain->domain != pdomain->domain)140continue;141142/* Here i and j are in the same domain.143* If two TSD packages have the same domain, they144* should have the same num_porcessors and145* coordination type. Otherwise it will be regarded146* as illegal.147*/148if (match_pdomain->num_processors != count_target) {149retval = -EINVAL;150goto err_ret;151}152153if (pdomain->coord_type != match_pdomain->coord_type) {154retval = -EINVAL;155goto err_ret;156}157158cpumask_set_cpu(j, covered_cpus);159cpumask_set_cpu(j, pthrottling->shared_cpu_map);160count++;161}162for_each_possible_cpu(j) {163if (i == j)164continue;165166match_pr = per_cpu(processors, j);167if (!match_pr)168continue;169170match_pthrottling = &(match_pr->throttling);171match_pdomain = &(match_pthrottling->domain_info);172if (match_pdomain->domain != pdomain->domain)173continue;174175/*176* If some CPUS have the same domain, they177* will have the same shared_cpu_map.178*/179cpumask_copy(match_pthrottling->shared_cpu_map,180pthrottling->shared_cpu_map);181}182}183184err_ret:185free_cpumask_var(covered_cpus);186187for_each_possible_cpu(i) {188pr = per_cpu(processors, i);189if (!pr)190continue;191192/*193* Assume no coordination on any error parsing domain info.194* The coordination type will be forced as SW_ALL.195*/196if (retval) {197pthrottling = &(pr->throttling);198cpumask_clear(pthrottling->shared_cpu_map);199cpumask_set_cpu(i, pthrottling->shared_cpu_map);200pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;201}202}203204return retval;205}206207/*208* Update the T-state coordination after the _TSD209* data for all cpus is obtained.210*/211void acpi_processor_throttling_init(void)212{213if (acpi_processor_update_tsd_coord())214ACPI_DEBUG_PRINT((ACPI_DB_INFO,215"Assume no T-state coordination\n"));216217return;218}219220static int acpi_processor_throttling_notifier(unsigned long event, void *data)221{222struct throttling_tstate *p_tstate = data;223struct acpi_processor *pr;224unsigned int cpu ;225int target_state;226struct acpi_processor_limit *p_limit;227struct acpi_processor_throttling *p_throttling;228229cpu = p_tstate->cpu;230pr = per_cpu(processors, cpu);231if (!pr) {232ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));233return 0;234}235if (!pr->flags.throttling) {236ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "237"unsupported on CPU %d\n", cpu));238return 0;239}240target_state = p_tstate->target_state;241p_throttling = &(pr->throttling);242switch (event) {243case THROTTLING_PRECHANGE:244/*245* Prechange event is used to choose one proper t-state,246* which meets the limits of thermal, user and _TPC.247*/248p_limit = &pr->limit;249if (p_limit->thermal.tx > target_state)250target_state = p_limit->thermal.tx;251if (p_limit->user.tx > target_state)252target_state = p_limit->user.tx;253if (pr->throttling_platform_limit > target_state)254target_state = pr->throttling_platform_limit;255if (target_state >= p_throttling->state_count) {256printk(KERN_WARNING257"Exceed the limit of T-state \n");258target_state = p_throttling->state_count - 1;259}260p_tstate->target_state = target_state;261ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"262"target T-state of CPU %d is T%d\n",263cpu, target_state));264break;265case THROTTLING_POSTCHANGE:266/*267* Postchange event is only used to update the268* T-state flag of acpi_processor_throttling.269*/270p_throttling->state = target_state;271ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"272"CPU %d is switched to T%d\n",273cpu, target_state));274break;275default:276printk(KERN_WARNING277"Unsupported Throttling notifier event\n");278break;279}280281return 0;282}283284/*285* _TPC - Throttling Present Capabilities286*/287static int acpi_processor_get_platform_limit(struct acpi_processor *pr)288{289acpi_status status = 0;290unsigned long long tpc = 0;291292if (!pr)293return -EINVAL;294295if (ignore_tpc)296goto end;297298status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);299if (ACPI_FAILURE(status)) {300if (status != AE_NOT_FOUND) {301ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));302}303return -ENODEV;304}305306end:307pr->throttling_platform_limit = (int)tpc;308return 0;309}310311int acpi_processor_tstate_has_changed(struct acpi_processor *pr)312{313int result = 0;314int throttling_limit;315int current_state;316struct acpi_processor_limit *limit;317int target_state;318319if (ignore_tpc)320return 0;321322result = acpi_processor_get_platform_limit(pr);323if (result) {324/* Throttling Limit is unsupported */325return result;326}327328throttling_limit = pr->throttling_platform_limit;329if (throttling_limit >= pr->throttling.state_count) {330/* Uncorrect Throttling Limit */331return -EINVAL;332}333334current_state = pr->throttling.state;335if (current_state > throttling_limit) {336/*337* The current state can meet the requirement of338* _TPC limit. But it is reasonable that OSPM changes339* t-states from high to low for better performance.340* Of course the limit condition of thermal341* and user should be considered.342*/343limit = &pr->limit;344target_state = throttling_limit;345if (limit->thermal.tx > target_state)346target_state = limit->thermal.tx;347if (limit->user.tx > target_state)348target_state = limit->user.tx;349} else if (current_state == throttling_limit) {350/*351* Unnecessary to change the throttling state352*/353return 0;354} else {355/*356* If the current state is lower than the limit of _TPC, it357* will be forced to switch to the throttling state defined358* by throttling_platfor_limit.359* Because the previous state meets with the limit condition360* of thermal and user, it is unnecessary to check it again.361*/362target_state = throttling_limit;363}364return acpi_processor_set_throttling(pr, target_state, false);365}366367/*368* This function is used to reevaluate whether the T-state is valid369* after one CPU is onlined/offlined.370* It is noted that it won't reevaluate the following properties for371* the T-state.372* 1. Control method.373* 2. the number of supported T-state374* 3. TSD domain375*/376void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,377unsigned long action)378{379int result = 0;380381if (action == CPU_DEAD) {382/* When one CPU is offline, the T-state throttling383* will be invalidated.384*/385pr->flags.throttling = 0;386return;387}388/* the following is to recheck whether the T-state is valid for389* the online CPU390*/391if (!pr->throttling.state_count) {392/* If the number of T-state is invalid, it is393* invalidated.394*/395pr->flags.throttling = 0;396return;397}398pr->flags.throttling = 1;399400/* Disable throttling (if enabled). We'll let subsequent401* policy (e.g.thermal) decide to lower performance if it402* so chooses, but for now we'll crank up the speed.403*/404405result = acpi_processor_get_throttling(pr);406if (result)407goto end;408409if (pr->throttling.state) {410result = acpi_processor_set_throttling(pr, 0, false);411if (result)412goto end;413}414415end:416if (result)417pr->flags.throttling = 0;418}419/*420* _PTC - Processor Throttling Control (and status) register location421*/422static int acpi_processor_get_throttling_control(struct acpi_processor *pr)423{424int result = 0;425acpi_status status = 0;426struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };427union acpi_object *ptc = NULL;428union acpi_object obj = { 0 };429struct acpi_processor_throttling *throttling;430431status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);432if (ACPI_FAILURE(status)) {433if (status != AE_NOT_FOUND) {434ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));435}436return -ENODEV;437}438439ptc = (union acpi_object *)buffer.pointer;440if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)441|| (ptc->package.count != 2)) {442printk(KERN_ERR PREFIX "Invalid _PTC data\n");443result = -EFAULT;444goto end;445}446447/*448* control_register449*/450451obj = ptc->package.elements[0];452453if ((obj.type != ACPI_TYPE_BUFFER)454|| (obj.buffer.length < sizeof(struct acpi_ptc_register))455|| (obj.buffer.pointer == NULL)) {456printk(KERN_ERR PREFIX457"Invalid _PTC data (control_register)\n");458result = -EFAULT;459goto end;460}461memcpy(&pr->throttling.control_register, obj.buffer.pointer,462sizeof(struct acpi_ptc_register));463464/*465* status_register466*/467468obj = ptc->package.elements[1];469470if ((obj.type != ACPI_TYPE_BUFFER)471|| (obj.buffer.length < sizeof(struct acpi_ptc_register))472|| (obj.buffer.pointer == NULL)) {473printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");474result = -EFAULT;475goto end;476}477478memcpy(&pr->throttling.status_register, obj.buffer.pointer,479sizeof(struct acpi_ptc_register));480481throttling = &pr->throttling;482483if ((throttling->control_register.bit_width +484throttling->control_register.bit_offset) > 32) {485printk(KERN_ERR PREFIX "Invalid _PTC control register\n");486result = -EFAULT;487goto end;488}489490if ((throttling->status_register.bit_width +491throttling->status_register.bit_offset) > 32) {492printk(KERN_ERR PREFIX "Invalid _PTC status register\n");493result = -EFAULT;494goto end;495}496497end:498kfree(buffer.pointer);499500return result;501}502503/*504* _TSS - Throttling Supported States505*/506static int acpi_processor_get_throttling_states(struct acpi_processor *pr)507{508int result = 0;509acpi_status status = AE_OK;510struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };511struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };512struct acpi_buffer state = { 0, NULL };513union acpi_object *tss = NULL;514int i;515516status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);517if (ACPI_FAILURE(status)) {518if (status != AE_NOT_FOUND) {519ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));520}521return -ENODEV;522}523524tss = buffer.pointer;525if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {526printk(KERN_ERR PREFIX "Invalid _TSS data\n");527result = -EFAULT;528goto end;529}530531ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",532tss->package.count));533534pr->throttling.state_count = tss->package.count;535pr->throttling.states_tss =536kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,537GFP_KERNEL);538if (!pr->throttling.states_tss) {539result = -ENOMEM;540goto end;541}542543for (i = 0; i < pr->throttling.state_count; i++) {544545struct acpi_processor_tx_tss *tx =546(struct acpi_processor_tx_tss *)&(pr->throttling.547states_tss[i]);548549state.length = sizeof(struct acpi_processor_tx_tss);550state.pointer = tx;551552ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));553554status = acpi_extract_package(&(tss->package.elements[i]),555&format, &state);556if (ACPI_FAILURE(status)) {557ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));558result = -EFAULT;559kfree(pr->throttling.states_tss);560goto end;561}562563if (!tx->freqpercentage) {564printk(KERN_ERR PREFIX565"Invalid _TSS data: freq is zero\n");566result = -EFAULT;567kfree(pr->throttling.states_tss);568goto end;569}570}571572end:573kfree(buffer.pointer);574575return result;576}577578/*579* _TSD - T-State Dependencies580*/581static int acpi_processor_get_tsd(struct acpi_processor *pr)582{583int result = 0;584acpi_status status = AE_OK;585struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };586struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };587struct acpi_buffer state = { 0, NULL };588union acpi_object *tsd = NULL;589struct acpi_tsd_package *pdomain;590struct acpi_processor_throttling *pthrottling;591592pthrottling = &pr->throttling;593pthrottling->tsd_valid_flag = 0;594595status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);596if (ACPI_FAILURE(status)) {597if (status != AE_NOT_FOUND) {598ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));599}600return -ENODEV;601}602603tsd = buffer.pointer;604if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {605printk(KERN_ERR PREFIX "Invalid _TSD data\n");606result = -EFAULT;607goto end;608}609610if (tsd->package.count != 1) {611printk(KERN_ERR PREFIX "Invalid _TSD data\n");612result = -EFAULT;613goto end;614}615616pdomain = &(pr->throttling.domain_info);617618state.length = sizeof(struct acpi_tsd_package);619state.pointer = pdomain;620621status = acpi_extract_package(&(tsd->package.elements[0]),622&format, &state);623if (ACPI_FAILURE(status)) {624printk(KERN_ERR PREFIX "Invalid _TSD data\n");625result = -EFAULT;626goto end;627}628629if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {630printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");631result = -EFAULT;632goto end;633}634635if (pdomain->revision != ACPI_TSD_REV0_REVISION) {636printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");637result = -EFAULT;638goto end;639}640641pthrottling = &pr->throttling;642pthrottling->tsd_valid_flag = 1;643pthrottling->shared_type = pdomain->coord_type;644cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);645/*646* If the coordination type is not defined in ACPI spec,647* the tsd_valid_flag will be clear and coordination type648* will be forecd as DOMAIN_COORD_TYPE_SW_ALL.649*/650if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&651pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&652pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {653pthrottling->tsd_valid_flag = 0;654pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;655}656657end:658kfree(buffer.pointer);659return result;660}661662/* --------------------------------------------------------------------------663Throttling Control664-------------------------------------------------------------------------- */665static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)666{667int state = 0;668u32 value = 0;669u32 duty_mask = 0;670u32 duty_value = 0;671672if (!pr)673return -EINVAL;674675if (!pr->flags.throttling)676return -ENODEV;677678pr->throttling.state = 0;679680duty_mask = pr->throttling.state_count - 1;681682duty_mask <<= pr->throttling.duty_offset;683684local_irq_disable();685686value = inl(pr->throttling.address);687688/*689* Compute the current throttling state when throttling is enabled690* (bit 4 is on).691*/692if (value & 0x10) {693duty_value = value & duty_mask;694duty_value >>= pr->throttling.duty_offset;695696if (duty_value)697state = pr->throttling.state_count - duty_value;698}699700pr->throttling.state = state;701702local_irq_enable();703704ACPI_DEBUG_PRINT((ACPI_DB_INFO,705"Throttling state is T%d (%d%% throttling applied)\n",706state, pr->throttling.states[state].performance));707708return 0;709}710711#ifdef CONFIG_X86712static int acpi_throttling_rdmsr(u64 *value)713{714u64 msr_high, msr_low;715u64 msr = 0;716int ret = -1;717718if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||719!this_cpu_has(X86_FEATURE_ACPI)) {720printk(KERN_ERR PREFIX721"HARDWARE addr space,NOT supported yet\n");722} else {723msr_low = 0;724msr_high = 0;725rdmsr_safe(MSR_IA32_THERM_CONTROL,726(u32 *)&msr_low , (u32 *) &msr_high);727msr = (msr_high << 32) | msr_low;728*value = (u64) msr;729ret = 0;730}731return ret;732}733734static int acpi_throttling_wrmsr(u64 value)735{736int ret = -1;737u64 msr;738739if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||740!this_cpu_has(X86_FEATURE_ACPI)) {741printk(KERN_ERR PREFIX742"HARDWARE addr space,NOT supported yet\n");743} else {744msr = value;745wrmsr_safe(MSR_IA32_THERM_CONTROL,746msr & 0xffffffff, msr >> 32);747ret = 0;748}749return ret;750}751#else752static int acpi_throttling_rdmsr(u64 *value)753{754printk(KERN_ERR PREFIX755"HARDWARE addr space,NOT supported yet\n");756return -1;757}758759static int acpi_throttling_wrmsr(u64 value)760{761printk(KERN_ERR PREFIX762"HARDWARE addr space,NOT supported yet\n");763return -1;764}765#endif766767static int acpi_read_throttling_status(struct acpi_processor *pr,768u64 *value)769{770u32 bit_width, bit_offset;771u64 ptc_value;772u64 ptc_mask;773struct acpi_processor_throttling *throttling;774int ret = -1;775776throttling = &pr->throttling;777switch (throttling->status_register.space_id) {778case ACPI_ADR_SPACE_SYSTEM_IO:779ptc_value = 0;780bit_width = throttling->status_register.bit_width;781bit_offset = throttling->status_register.bit_offset;782783acpi_os_read_port((acpi_io_address) throttling->status_register.784address, (u32 *) &ptc_value,785(u32) (bit_width + bit_offset));786ptc_mask = (1 << bit_width) - 1;787*value = (u64) ((ptc_value >> bit_offset) & ptc_mask);788ret = 0;789break;790case ACPI_ADR_SPACE_FIXED_HARDWARE:791ret = acpi_throttling_rdmsr(value);792break;793default:794printk(KERN_ERR PREFIX "Unknown addr space %d\n",795(u32) (throttling->status_register.space_id));796}797return ret;798}799800static int acpi_write_throttling_state(struct acpi_processor *pr,801u64 value)802{803u32 bit_width, bit_offset;804u64 ptc_value;805u64 ptc_mask;806struct acpi_processor_throttling *throttling;807int ret = -1;808809throttling = &pr->throttling;810switch (throttling->control_register.space_id) {811case ACPI_ADR_SPACE_SYSTEM_IO:812bit_width = throttling->control_register.bit_width;813bit_offset = throttling->control_register.bit_offset;814ptc_mask = (1 << bit_width) - 1;815ptc_value = value & ptc_mask;816817acpi_os_write_port((acpi_io_address) throttling->818control_register.address,819(u32) (ptc_value << bit_offset),820(u32) (bit_width + bit_offset));821ret = 0;822break;823case ACPI_ADR_SPACE_FIXED_HARDWARE:824ret = acpi_throttling_wrmsr(value);825break;826default:827printk(KERN_ERR PREFIX "Unknown addr space %d\n",828(u32) (throttling->control_register.space_id));829}830return ret;831}832833static int acpi_get_throttling_state(struct acpi_processor *pr,834u64 value)835{836int i;837838for (i = 0; i < pr->throttling.state_count; i++) {839struct acpi_processor_tx_tss *tx =840(struct acpi_processor_tx_tss *)&(pr->throttling.841states_tss[i]);842if (tx->control == value)843return i;844}845return -1;846}847848static int acpi_get_throttling_value(struct acpi_processor *pr,849int state, u64 *value)850{851int ret = -1;852853if (state >= 0 && state <= pr->throttling.state_count) {854struct acpi_processor_tx_tss *tx =855(struct acpi_processor_tx_tss *)&(pr->throttling.856states_tss[state]);857*value = tx->control;858ret = 0;859}860return ret;861}862863static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)864{865int state = 0;866int ret;867u64 value;868869if (!pr)870return -EINVAL;871872if (!pr->flags.throttling)873return -ENODEV;874875pr->throttling.state = 0;876877value = 0;878ret = acpi_read_throttling_status(pr, &value);879if (ret >= 0) {880state = acpi_get_throttling_state(pr, value);881if (state == -1) {882ACPI_DEBUG_PRINT((ACPI_DB_INFO,883"Invalid throttling state, reset\n"));884state = 0;885ret = acpi_processor_set_throttling(pr, state, true);886if (ret)887return ret;888}889pr->throttling.state = state;890}891892return 0;893}894895static int acpi_processor_get_throttling(struct acpi_processor *pr)896{897cpumask_var_t saved_mask;898int ret;899900if (!pr)901return -EINVAL;902903if (!pr->flags.throttling)904return -ENODEV;905906if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))907return -ENOMEM;908909/*910* Migrate task to the cpu pointed by pr.911*/912cpumask_copy(saved_mask, ¤t->cpus_allowed);913/* FIXME: use work_on_cpu() */914if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {915/* Can't migrate to the target pr->id CPU. Exit */916free_cpumask_var(saved_mask);917return -ENODEV;918}919ret = pr->throttling.acpi_processor_get_throttling(pr);920/* restore the previous state */921set_cpus_allowed_ptr(current, saved_mask);922free_cpumask_var(saved_mask);923924return ret;925}926927static int acpi_processor_get_fadt_info(struct acpi_processor *pr)928{929int i, step;930931if (!pr->throttling.address) {932ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));933return -EINVAL;934} else if (!pr->throttling.duty_width) {935ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));936return -EINVAL;937}938/* TBD: Support duty_cycle values that span bit 4. */939else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {940printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");941return -EINVAL;942}943944pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;945946/*947* Compute state values. Note that throttling displays a linear power948* performance relationship (at 50% performance the CPU will consume949* 50% power). Values are in 1/10th of a percent to preserve accuracy.950*/951952step = (1000 / pr->throttling.state_count);953954for (i = 0; i < pr->throttling.state_count; i++) {955pr->throttling.states[i].performance = 1000 - step * i;956pr->throttling.states[i].power = 1000 - step * i;957}958return 0;959}960961static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,962int state, bool force)963{964u32 value = 0;965u32 duty_mask = 0;966u32 duty_value = 0;967968if (!pr)969return -EINVAL;970971if ((state < 0) || (state > (pr->throttling.state_count - 1)))972return -EINVAL;973974if (!pr->flags.throttling)975return -ENODEV;976977if (!force && (state == pr->throttling.state))978return 0;979980if (state < pr->throttling_platform_limit)981return -EPERM;982/*983* Calculate the duty_value and duty_mask.984*/985if (state) {986duty_value = pr->throttling.state_count - state;987988duty_value <<= pr->throttling.duty_offset;989990/* Used to clear all duty_value bits */991duty_mask = pr->throttling.state_count - 1;992993duty_mask <<= acpi_gbl_FADT.duty_offset;994duty_mask = ~duty_mask;995}996997local_irq_disable();998999/*1000* Disable throttling by writing a 0 to bit 4. Note that we must1001* turn it off before you can change the duty_value.1002*/1003value = inl(pr->throttling.address);1004if (value & 0x10) {1005value &= 0xFFFFFFEF;1006outl(value, pr->throttling.address);1007}10081009/*1010* Write the new duty_value and then enable throttling. Note1011* that a state value of 0 leaves throttling disabled.1012*/1013if (state) {1014value &= duty_mask;1015value |= duty_value;1016outl(value, pr->throttling.address);10171018value |= 0x00000010;1019outl(value, pr->throttling.address);1020}10211022pr->throttling.state = state;10231024local_irq_enable();10251026ACPI_DEBUG_PRINT((ACPI_DB_INFO,1027"Throttling state set to T%d (%d%%)\n", state,1028(pr->throttling.states[state].performance ? pr->1029throttling.states[state].performance / 10 : 0)));10301031return 0;1032}10331034static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,1035int state, bool force)1036{1037int ret;1038u64 value;10391040if (!pr)1041return -EINVAL;10421043if ((state < 0) || (state > (pr->throttling.state_count - 1)))1044return -EINVAL;10451046if (!pr->flags.throttling)1047return -ENODEV;10481049if (!force && (state == pr->throttling.state))1050return 0;10511052if (state < pr->throttling_platform_limit)1053return -EPERM;10541055value = 0;1056ret = acpi_get_throttling_value(pr, state, &value);1057if (ret >= 0) {1058acpi_write_throttling_state(pr, value);1059pr->throttling.state = state;1060}10611062return 0;1063}10641065int acpi_processor_set_throttling(struct acpi_processor *pr,1066int state, bool force)1067{1068cpumask_var_t saved_mask;1069int ret = 0;1070unsigned int i;1071struct acpi_processor *match_pr;1072struct acpi_processor_throttling *p_throttling;1073struct throttling_tstate t_state;1074cpumask_var_t online_throttling_cpus;10751076if (!pr)1077return -EINVAL;10781079if (!pr->flags.throttling)1080return -ENODEV;10811082if ((state < 0) || (state > (pr->throttling.state_count - 1)))1083return -EINVAL;10841085if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))1086return -ENOMEM;10871088if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) {1089free_cpumask_var(saved_mask);1090return -ENOMEM;1091}10921093if (cpu_is_offline(pr->id)) {1094/*1095* the cpu pointed by pr->id is offline. Unnecessary to change1096* the throttling state any more.1097*/1098return -ENODEV;1099}11001101cpumask_copy(saved_mask, ¤t->cpus_allowed);1102t_state.target_state = state;1103p_throttling = &(pr->throttling);1104cpumask_and(online_throttling_cpus, cpu_online_mask,1105p_throttling->shared_cpu_map);1106/*1107* The throttling notifier will be called for every1108* affected cpu in order to get one proper T-state.1109* The notifier event is THROTTLING_PRECHANGE.1110*/1111for_each_cpu(i, online_throttling_cpus) {1112t_state.cpu = i;1113acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,1114&t_state);1115}1116/*1117* The function of acpi_processor_set_throttling will be called1118* to switch T-state. If the coordination type is SW_ALL or HW_ALL,1119* it is necessary to call it for every affected cpu. Otherwise1120* it can be called only for the cpu pointed by pr.1121*/1122if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {1123/* FIXME: use work_on_cpu() */1124if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {1125/* Can't migrate to the pr->id CPU. Exit */1126ret = -ENODEV;1127goto exit;1128}1129ret = p_throttling->acpi_processor_set_throttling(pr,1130t_state.target_state, force);1131} else {1132/*1133* When the T-state coordination is SW_ALL or HW_ALL,1134* it is necessary to set T-state for every affected1135* cpus.1136*/1137for_each_cpu(i, online_throttling_cpus) {1138match_pr = per_cpu(processors, i);1139/*1140* If the pointer is invalid, we will report the1141* error message and continue.1142*/1143if (!match_pr) {1144ACPI_DEBUG_PRINT((ACPI_DB_INFO,1145"Invalid Pointer for CPU %d\n", i));1146continue;1147}1148/*1149* If the throttling control is unsupported on CPU i,1150* we will report the error message and continue.1151*/1152if (!match_pr->flags.throttling) {1153ACPI_DEBUG_PRINT((ACPI_DB_INFO,1154"Throttling Control is unsupported "1155"on CPU %d\n", i));1156continue;1157}1158t_state.cpu = i;1159/* FIXME: use work_on_cpu() */1160if (set_cpus_allowed_ptr(current, cpumask_of(i)))1161continue;1162ret = match_pr->throttling.1163acpi_processor_set_throttling(1164match_pr, t_state.target_state, force);1165}1166}1167/*1168* After the set_throttling is called, the1169* throttling notifier is called for every1170* affected cpu to update the T-states.1171* The notifier event is THROTTLING_POSTCHANGE1172*/1173for_each_cpu(i, online_throttling_cpus) {1174t_state.cpu = i;1175acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,1176&t_state);1177}1178/* restore the previous state */1179/* FIXME: use work_on_cpu() */1180set_cpus_allowed_ptr(current, saved_mask);1181exit:1182free_cpumask_var(online_throttling_cpus);1183free_cpumask_var(saved_mask);1184return ret;1185}11861187int acpi_processor_get_throttling_info(struct acpi_processor *pr)1188{1189int result = 0;1190struct acpi_processor_throttling *pthrottling;11911192ACPI_DEBUG_PRINT((ACPI_DB_INFO,1193"pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",1194pr->throttling.address,1195pr->throttling.duty_offset,1196pr->throttling.duty_width));11971198/*1199* Evaluate _PTC, _TSS and _TPC1200* They must all be present or none of them can be used.1201*/1202if (acpi_processor_get_throttling_control(pr) ||1203acpi_processor_get_throttling_states(pr) ||1204acpi_processor_get_platform_limit(pr))1205{1206pr->throttling.acpi_processor_get_throttling =1207&acpi_processor_get_throttling_fadt;1208pr->throttling.acpi_processor_set_throttling =1209&acpi_processor_set_throttling_fadt;1210if (acpi_processor_get_fadt_info(pr))1211return 0;1212} else {1213pr->throttling.acpi_processor_get_throttling =1214&acpi_processor_get_throttling_ptc;1215pr->throttling.acpi_processor_set_throttling =1216&acpi_processor_set_throttling_ptc;1217}12181219/*1220* If TSD package for one CPU can't be parsed successfully, it means1221* that this CPU will have no coordination with other CPUs.1222*/1223if (acpi_processor_get_tsd(pr)) {1224pthrottling = &pr->throttling;1225pthrottling->tsd_valid_flag = 0;1226cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);1227pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;1228}12291230/*1231* PIIX4 Errata: We don't support throttling on the original PIIX4.1232* This shouldn't be an issue as few (if any) mobile systems ever1233* used this part.1234*/1235if (errata.piix4.throttle) {1236ACPI_DEBUG_PRINT((ACPI_DB_INFO,1237"Throttling not supported on PIIX4 A- or B-step\n"));1238return 0;1239}12401241ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",1242pr->throttling.state_count));12431244pr->flags.throttling = 1;12451246/*1247* Disable throttling (if enabled). We'll let subsequent policy (e.g.1248* thermal) decide to lower performance if it so chooses, but for now1249* we'll crank up the speed.1250*/12511252result = acpi_processor_get_throttling(pr);1253if (result)1254goto end;12551256if (pr->throttling.state) {1257ACPI_DEBUG_PRINT((ACPI_DB_INFO,1258"Disabling throttling (was T%d)\n",1259pr->throttling.state));1260result = acpi_processor_set_throttling(pr, 0, false);1261if (result)1262goto end;1263}12641265end:1266if (result)1267pr->flags.throttling = 0;12681269return result;1270}1271127212731274