Path: blob/master/drivers/cpufreq/speedstep-centrino.c
15111 views
/*1* cpufreq driver for Enhanced SpeedStep, as found in Intel's Pentium2* M (part of the Centrino chipset).3*4* Since the original Pentium M, most new Intel CPUs support Enhanced5* SpeedStep.6*7* Despite the "SpeedStep" in the name, this is almost entirely unlike8* traditional SpeedStep.9*10* Modelled on speedstep.c11*12* Copyright (C) 2003 Jeremy Fitzhardinge <[email protected]>13*/1415#include <linux/kernel.h>16#include <linux/module.h>17#include <linux/init.h>18#include <linux/cpufreq.h>19#include <linux/sched.h> /* current */20#include <linux/delay.h>21#include <linux/compiler.h>22#include <linux/gfp.h>2324#include <asm/msr.h>25#include <asm/processor.h>26#include <asm/cpufeature.h>2728#define PFX "speedstep-centrino: "29#define MAINTAINER "[email protected]"3031#define INTEL_MSR_RANGE (0xffff)3233struct cpu_id34{35__u8 x86; /* CPU family */36__u8 x86_model; /* model */37__u8 x86_mask; /* stepping */38};3940enum {41CPU_BANIAS,42CPU_DOTHAN_A1,43CPU_DOTHAN_A2,44CPU_DOTHAN_B0,45CPU_MP4HT_D0,46CPU_MP4HT_E0,47};4849static const struct cpu_id cpu_ids[] = {50[CPU_BANIAS] = { 6, 9, 5 },51[CPU_DOTHAN_A1] = { 6, 13, 1 },52[CPU_DOTHAN_A2] = { 6, 13, 2 },53[CPU_DOTHAN_B0] = { 6, 13, 6 },54[CPU_MP4HT_D0] = {15, 3, 4 },55[CPU_MP4HT_E0] = {15, 4, 1 },56};57#define N_IDS ARRAY_SIZE(cpu_ids)5859struct cpu_model60{61const struct cpu_id *cpu_id;62const char *model_name;63unsigned max_freq; /* max clock in kHz */6465struct cpufreq_frequency_table *op_points; /* clock/voltage pairs */66};67static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,68const struct cpu_id *x);6970/* Operating points for current CPU */71static DEFINE_PER_CPU(struct cpu_model *, centrino_model);72static DEFINE_PER_CPU(const struct cpu_id *, centrino_cpu);7374static struct cpufreq_driver centrino_driver;7576#ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE7778/* Computes the correct form for IA32_PERF_CTL MSR for a particular79frequency/voltage operating point; frequency in MHz, volts in mV.80This is stored as "index" in the structure. */81#define OP(mhz, mv) \82{ \83.frequency = (mhz) * 1000, \84.index = (((mhz)/100) << 8) | ((mv - 700) / 16) \85}8687/*88* These voltage tables were derived from the Intel Pentium M89* datasheet, document 25261202.pdf, Table 5. I have verified they90* are consistent with my IBM ThinkPad X31, which has a 1.3GHz Pentium91* M.92*/9394/* Ultra Low Voltage Intel Pentium M processor 900MHz (Banias) */95static struct cpufreq_frequency_table banias_900[] =96{97OP(600, 844),98OP(800, 988),99OP(900, 1004),100{ .frequency = CPUFREQ_TABLE_END }101};102103/* Ultra Low Voltage Intel Pentium M processor 1000MHz (Banias) */104static struct cpufreq_frequency_table banias_1000[] =105{106OP(600, 844),107OP(800, 972),108OP(900, 988),109OP(1000, 1004),110{ .frequency = CPUFREQ_TABLE_END }111};112113/* Low Voltage Intel Pentium M processor 1.10GHz (Banias) */114static struct cpufreq_frequency_table banias_1100[] =115{116OP( 600, 956),117OP( 800, 1020),118OP( 900, 1100),119OP(1000, 1164),120OP(1100, 1180),121{ .frequency = CPUFREQ_TABLE_END }122};123124125/* Low Voltage Intel Pentium M processor 1.20GHz (Banias) */126static struct cpufreq_frequency_table banias_1200[] =127{128OP( 600, 956),129OP( 800, 1004),130OP( 900, 1020),131OP(1000, 1100),132OP(1100, 1164),133OP(1200, 1180),134{ .frequency = CPUFREQ_TABLE_END }135};136137/* Intel Pentium M processor 1.30GHz (Banias) */138static struct cpufreq_frequency_table banias_1300[] =139{140OP( 600, 956),141OP( 800, 1260),142OP(1000, 1292),143OP(1200, 1356),144OP(1300, 1388),145{ .frequency = CPUFREQ_TABLE_END }146};147148/* Intel Pentium M processor 1.40GHz (Banias) */149static struct cpufreq_frequency_table banias_1400[] =150{151OP( 600, 956),152OP( 800, 1180),153OP(1000, 1308),154OP(1200, 1436),155OP(1400, 1484),156{ .frequency = CPUFREQ_TABLE_END }157};158159/* Intel Pentium M processor 1.50GHz (Banias) */160static struct cpufreq_frequency_table banias_1500[] =161{162OP( 600, 956),163OP( 800, 1116),164OP(1000, 1228),165OP(1200, 1356),166OP(1400, 1452),167OP(1500, 1484),168{ .frequency = CPUFREQ_TABLE_END }169};170171/* Intel Pentium M processor 1.60GHz (Banias) */172static struct cpufreq_frequency_table banias_1600[] =173{174OP( 600, 956),175OP( 800, 1036),176OP(1000, 1164),177OP(1200, 1276),178OP(1400, 1420),179OP(1600, 1484),180{ .frequency = CPUFREQ_TABLE_END }181};182183/* Intel Pentium M processor 1.70GHz (Banias) */184static struct cpufreq_frequency_table banias_1700[] =185{186OP( 600, 956),187OP( 800, 1004),188OP(1000, 1116),189OP(1200, 1228),190OP(1400, 1308),191OP(1700, 1484),192{ .frequency = CPUFREQ_TABLE_END }193};194#undef OP195196#define _BANIAS(cpuid, max, name) \197{ .cpu_id = cpuid, \198.model_name = "Intel(R) Pentium(R) M processor " name "MHz", \199.max_freq = (max)*1000, \200.op_points = banias_##max, \201}202#define BANIAS(max) _BANIAS(&cpu_ids[CPU_BANIAS], max, #max)203204/* CPU models, their operating frequency range, and freq/voltage205operating points */206static struct cpu_model models[] =207{208_BANIAS(&cpu_ids[CPU_BANIAS], 900, " 900"),209BANIAS(1000),210BANIAS(1100),211BANIAS(1200),212BANIAS(1300),213BANIAS(1400),214BANIAS(1500),215BANIAS(1600),216BANIAS(1700),217218/* NULL model_name is a wildcard */219{ &cpu_ids[CPU_DOTHAN_A1], NULL, 0, NULL },220{ &cpu_ids[CPU_DOTHAN_A2], NULL, 0, NULL },221{ &cpu_ids[CPU_DOTHAN_B0], NULL, 0, NULL },222{ &cpu_ids[CPU_MP4HT_D0], NULL, 0, NULL },223{ &cpu_ids[CPU_MP4HT_E0], NULL, 0, NULL },224225{ NULL, }226};227#undef _BANIAS228#undef BANIAS229230static int centrino_cpu_init_table(struct cpufreq_policy *policy)231{232struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);233struct cpu_model *model;234235for(model = models; model->cpu_id != NULL; model++)236if (centrino_verify_cpu_id(cpu, model->cpu_id) &&237(model->model_name == NULL ||238strcmp(cpu->x86_model_id, model->model_name) == 0))239break;240241if (model->cpu_id == NULL) {242/* No match at all */243pr_debug("no support for CPU model \"%s\": "244"send /proc/cpuinfo to " MAINTAINER "\n",245cpu->x86_model_id);246return -ENOENT;247}248249if (model->op_points == NULL) {250/* Matched a non-match */251pr_debug("no table support for CPU model \"%s\"\n",252cpu->x86_model_id);253pr_debug("try using the acpi-cpufreq driver\n");254return -ENOENT;255}256257per_cpu(centrino_model, policy->cpu) = model;258259pr_debug("found \"%s\": max frequency: %dkHz\n",260model->model_name, model->max_freq);261262return 0;263}264265#else266static inline int centrino_cpu_init_table(struct cpufreq_policy *policy)267{268return -ENODEV;269}270#endif /* CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE */271272static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,273const struct cpu_id *x)274{275if ((c->x86 == x->x86) &&276(c->x86_model == x->x86_model) &&277(c->x86_mask == x->x86_mask))278return 1;279return 0;280}281282/* To be called only after centrino_model is initialized */283static unsigned extract_clock(unsigned msr, unsigned int cpu, int failsafe)284{285int i;286287/*288* Extract clock in kHz from PERF_CTL value289* for centrino, as some DSDTs are buggy.290* Ideally, this can be done using the acpi_data structure.291*/292if ((per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_BANIAS]) ||293(per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_A1]) ||294(per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_B0])) {295msr = (msr >> 8) & 0xff;296return msr * 100000;297}298299if ((!per_cpu(centrino_model, cpu)) ||300(!per_cpu(centrino_model, cpu)->op_points))301return 0;302303msr &= 0xffff;304for (i = 0;305per_cpu(centrino_model, cpu)->op_points[i].frequency306!= CPUFREQ_TABLE_END;307i++) {308if (msr == per_cpu(centrino_model, cpu)->op_points[i].index)309return per_cpu(centrino_model, cpu)->310op_points[i].frequency;311}312if (failsafe)313return per_cpu(centrino_model, cpu)->op_points[i-1].frequency;314else315return 0;316}317318/* Return the current CPU frequency in kHz */319static unsigned int get_cur_freq(unsigned int cpu)320{321unsigned l, h;322unsigned clock_freq;323324rdmsr_on_cpu(cpu, MSR_IA32_PERF_STATUS, &l, &h);325clock_freq = extract_clock(l, cpu, 0);326327if (unlikely(clock_freq == 0)) {328/*329* On some CPUs, we can see transient MSR values (which are330* not present in _PSS), while CPU is doing some automatic331* P-state transition (like TM2). Get the last freq set332* in PERF_CTL.333*/334rdmsr_on_cpu(cpu, MSR_IA32_PERF_CTL, &l, &h);335clock_freq = extract_clock(l, cpu, 1);336}337return clock_freq;338}339340341static int centrino_cpu_init(struct cpufreq_policy *policy)342{343struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);344unsigned freq;345unsigned l, h;346int ret;347int i;348349/* Only Intel makes Enhanced Speedstep-capable CPUs */350if (cpu->x86_vendor != X86_VENDOR_INTEL ||351!cpu_has(cpu, X86_FEATURE_EST))352return -ENODEV;353354if (cpu_has(cpu, X86_FEATURE_CONSTANT_TSC))355centrino_driver.flags |= CPUFREQ_CONST_LOOPS;356357if (policy->cpu != 0)358return -ENODEV;359360for (i = 0; i < N_IDS; i++)361if (centrino_verify_cpu_id(cpu, &cpu_ids[i]))362break;363364if (i != N_IDS)365per_cpu(centrino_cpu, policy->cpu) = &cpu_ids[i];366367if (!per_cpu(centrino_cpu, policy->cpu)) {368pr_debug("found unsupported CPU with "369"Enhanced SpeedStep: send /proc/cpuinfo to "370MAINTAINER "\n");371return -ENODEV;372}373374if (centrino_cpu_init_table(policy)) {375return -ENODEV;376}377378/* Check to see if Enhanced SpeedStep is enabled, and try to379enable it if not. */380rdmsr(MSR_IA32_MISC_ENABLE, l, h);381382if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {383l |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;384pr_debug("trying to enable Enhanced SpeedStep (%x)\n", l);385wrmsr(MSR_IA32_MISC_ENABLE, l, h);386387/* check to see if it stuck */388rdmsr(MSR_IA32_MISC_ENABLE, l, h);389if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {390printk(KERN_INFO PFX391"couldn't enable Enhanced SpeedStep\n");392return -ENODEV;393}394}395396freq = get_cur_freq(policy->cpu);397policy->cpuinfo.transition_latency = 10000;398/* 10uS transition latency */399policy->cur = freq;400401pr_debug("centrino_cpu_init: cur=%dkHz\n", policy->cur);402403ret = cpufreq_frequency_table_cpuinfo(policy,404per_cpu(centrino_model, policy->cpu)->op_points);405if (ret)406return (ret);407408cpufreq_frequency_table_get_attr(409per_cpu(centrino_model, policy->cpu)->op_points, policy->cpu);410411return 0;412}413414static int centrino_cpu_exit(struct cpufreq_policy *policy)415{416unsigned int cpu = policy->cpu;417418if (!per_cpu(centrino_model, cpu))419return -ENODEV;420421cpufreq_frequency_table_put_attr(cpu);422423per_cpu(centrino_model, cpu) = NULL;424425return 0;426}427428/**429* centrino_verify - verifies a new CPUFreq policy430* @policy: new policy431*432* Limit must be within this model's frequency range at least one433* border included.434*/435static int centrino_verify (struct cpufreq_policy *policy)436{437return cpufreq_frequency_table_verify(policy,438per_cpu(centrino_model, policy->cpu)->op_points);439}440441/**442* centrino_setpolicy - set a new CPUFreq policy443* @policy: new policy444* @target_freq: the target frequency445* @relation: how that frequency relates to achieved frequency446* (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)447*448* Sets a new CPUFreq policy.449*/450static int centrino_target (struct cpufreq_policy *policy,451unsigned int target_freq,452unsigned int relation)453{454unsigned int newstate = 0;455unsigned int msr, oldmsr = 0, h = 0, cpu = policy->cpu;456struct cpufreq_freqs freqs;457int retval = 0;458unsigned int j, k, first_cpu, tmp;459cpumask_var_t covered_cpus;460461if (unlikely(!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL)))462return -ENOMEM;463464if (unlikely(per_cpu(centrino_model, cpu) == NULL)) {465retval = -ENODEV;466goto out;467}468469if (unlikely(cpufreq_frequency_table_target(policy,470per_cpu(centrino_model, cpu)->op_points,471target_freq,472relation,473&newstate))) {474retval = -EINVAL;475goto out;476}477478first_cpu = 1;479for_each_cpu(j, policy->cpus) {480int good_cpu;481482/* cpufreq holds the hotplug lock, so we are safe here */483if (!cpu_online(j))484continue;485486/*487* Support for SMP systems.488* Make sure we are running on CPU that wants to change freq489*/490if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)491good_cpu = cpumask_any_and(policy->cpus,492cpu_online_mask);493else494good_cpu = j;495496if (good_cpu >= nr_cpu_ids) {497pr_debug("couldn't limit to CPUs in this domain\n");498retval = -EAGAIN;499if (first_cpu) {500/* We haven't started the transition yet. */501goto out;502}503break;504}505506msr = per_cpu(centrino_model, cpu)->op_points[newstate].index;507508if (first_cpu) {509rdmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, &oldmsr, &h);510if (msr == (oldmsr & 0xffff)) {511pr_debug("no change needed - msr was and needs "512"to be %x\n", oldmsr);513retval = 0;514goto out;515}516517freqs.old = extract_clock(oldmsr, cpu, 0);518freqs.new = extract_clock(msr, cpu, 0);519520pr_debug("target=%dkHz old=%d new=%d msr=%04x\n",521target_freq, freqs.old, freqs.new, msr);522523for_each_cpu(k, policy->cpus) {524if (!cpu_online(k))525continue;526freqs.cpu = k;527cpufreq_notify_transition(&freqs,528CPUFREQ_PRECHANGE);529}530531first_cpu = 0;532/* all but 16 LSB are reserved, treat them with care */533oldmsr &= ~0xffff;534msr &= 0xffff;535oldmsr |= msr;536}537538wrmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, oldmsr, h);539if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)540break;541542cpumask_set_cpu(j, covered_cpus);543}544545for_each_cpu(k, policy->cpus) {546if (!cpu_online(k))547continue;548freqs.cpu = k;549cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);550}551552if (unlikely(retval)) {553/*554* We have failed halfway through the frequency change.555* We have sent callbacks to policy->cpus and556* MSRs have already been written on coverd_cpus.557* Best effort undo..558*/559560for_each_cpu(j, covered_cpus)561wrmsr_on_cpu(j, MSR_IA32_PERF_CTL, oldmsr, h);562563tmp = freqs.new;564freqs.new = freqs.old;565freqs.old = tmp;566for_each_cpu(j, policy->cpus) {567if (!cpu_online(j))568continue;569cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);570cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);571}572}573retval = 0;574575out:576free_cpumask_var(covered_cpus);577return retval;578}579580static struct freq_attr* centrino_attr[] = {581&cpufreq_freq_attr_scaling_available_freqs,582NULL,583};584585static struct cpufreq_driver centrino_driver = {586.name = "centrino", /* should be speedstep-centrino,587but there's a 16 char limit */588.init = centrino_cpu_init,589.exit = centrino_cpu_exit,590.verify = centrino_verify,591.target = centrino_target,592.get = get_cur_freq,593.attr = centrino_attr,594.owner = THIS_MODULE,595};596597598/**599* centrino_init - initializes the Enhanced SpeedStep CPUFreq driver600*601* Initializes the Enhanced SpeedStep support. Returns -ENODEV on602* unsupported devices, -ENOENT if there's no voltage table for this603* particular CPU model, -EINVAL on problems during initiatization,604* and zero on success.605*606* This is quite picky. Not only does the CPU have to advertise the607* "est" flag in the cpuid capability flags, we look for a specific608* CPU model and stepping, and we need to have the exact model name in609* our voltage tables. That is, be paranoid about not releasing610* someone's valuable magic smoke.611*/612static int __init centrino_init(void)613{614struct cpuinfo_x86 *cpu = &cpu_data(0);615616if (!cpu_has(cpu, X86_FEATURE_EST))617return -ENODEV;618619return cpufreq_register_driver(¢rino_driver);620}621622static void __exit centrino_exit(void)623{624cpufreq_unregister_driver(¢rino_driver);625}626627MODULE_AUTHOR ("Jeremy Fitzhardinge <[email protected]>");628MODULE_DESCRIPTION ("Enhanced SpeedStep driver for Intel Pentium M processors.");629MODULE_LICENSE ("GPL");630631late_initcall(centrino_init);632module_exit(centrino_exit);633634635