linux/drivers/cpufreq/intel_pstate.c
<<
>>
Prefs
   1/*
   2 * cpufreq_snb.c: Native P state management for Intel processors
   3 *
   4 * (C) Copyright 2012 Intel Corporation
   5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License
   9 * as published by the Free Software Foundation; version 2
  10 * of the License.
  11 */
  12
  13#include <linux/kernel.h>
  14#include <linux/kernel_stat.h>
  15#include <linux/module.h>
  16#include <linux/ktime.h>
  17#include <linux/hrtimer.h>
  18#include <linux/tick.h>
  19#include <linux/slab.h>
  20#include <linux/sched.h>
  21#include <linux/list.h>
  22#include <linux/cpu.h>
  23#include <linux/cpufreq.h>
  24#include <linux/sysfs.h>
  25#include <linux/types.h>
  26#include <linux/fs.h>
  27#include <linux/debugfs.h>
  28#include <trace/events/power.h>
  29
  30#include <asm/div64.h>
  31#include <asm/msr.h>
  32#include <asm/cpu_device_id.h>
  33
  34#define SAMPLE_COUNT            3
  35
  36#define FRAC_BITS 8
  37#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
  38#define fp_toint(X) ((X) >> FRAC_BITS)
  39
  40static inline int32_t mul_fp(int32_t x, int32_t y)
  41{
  42        return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
  43}
  44
  45static inline int32_t div_fp(int32_t x, int32_t y)
  46{
  47        return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
  48}
  49
  50struct sample {
  51        int core_pct_busy;
  52        u64 aperf;
  53        u64 mperf;
  54        int freq;
  55};
  56
  57struct pstate_data {
  58        int     current_pstate;
  59        int     min_pstate;
  60        int     max_pstate;
  61        int     turbo_pstate;
  62};
  63
  64struct _pid {
  65        int setpoint;
  66        int32_t integral;
  67        int32_t p_gain;
  68        int32_t i_gain;
  69        int32_t d_gain;
  70        int deadband;
  71        int last_err;
  72};
  73
  74struct cpudata {
  75        int cpu;
  76
  77        char name[64];
  78
  79        struct timer_list timer;
  80
  81        struct pstate_adjust_policy *pstate_policy;
  82        struct pstate_data pstate;
  83        struct _pid pid;
  84        struct _pid idle_pid;
  85
  86        int min_pstate_count;
  87        int idle_mode;
  88
  89        u64     prev_aperf;
  90        u64     prev_mperf;
  91        int     sample_ptr;
  92        struct sample samples[SAMPLE_COUNT];
  93};
  94
  95static struct cpudata **all_cpu_data;
  96struct pstate_adjust_policy {
  97        int sample_rate_ms;
  98        int deadband;
  99        int setpoint;
 100        int p_gain_pct;
 101        int d_gain_pct;
 102        int i_gain_pct;
 103};
 104
 105static struct pstate_adjust_policy default_policy = {
 106        .sample_rate_ms = 10,
 107        .deadband = 0,
 108        .setpoint = 109,
 109        .p_gain_pct = 17,
 110        .d_gain_pct = 0,
 111        .i_gain_pct = 4,
 112};
 113
 114struct perf_limits {
 115        int no_turbo;
 116        int max_perf_pct;
 117        int min_perf_pct;
 118        int32_t max_perf;
 119        int32_t min_perf;
 120        int max_policy_pct;
 121        int max_sysfs_pct;
 122};
 123
 124static struct perf_limits limits = {
 125        .no_turbo = 0,
 126        .max_perf_pct = 100,
 127        .max_perf = int_tofp(1),
 128        .min_perf_pct = 0,
 129        .min_perf = 0,
 130        .max_policy_pct = 100,
 131        .max_sysfs_pct = 100,
 132};
 133
 134static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
 135                        int deadband, int integral) {
 136        pid->setpoint = setpoint;
 137        pid->deadband  = deadband;
 138        pid->integral  = int_tofp(integral);
 139        pid->last_err  = setpoint - busy;
 140}
 141
 142static inline void pid_p_gain_set(struct _pid *pid, int percent)
 143{
 144        pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
 145}
 146
 147static inline void pid_i_gain_set(struct _pid *pid, int percent)
 148{
 149        pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
 150}
 151
 152static inline void pid_d_gain_set(struct _pid *pid, int percent)
 153{
 154
 155        pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
 156}
 157
 158static signed int pid_calc(struct _pid *pid, int busy)
 159{
 160        signed int err, result;
 161        int32_t pterm, dterm, fp_error;
 162        int32_t integral_limit;
 163
 164        err = pid->setpoint - busy;
 165        fp_error = int_tofp(err);
 166
 167        if (abs(err) <= pid->deadband)
 168                return 0;
 169
 170        pterm = mul_fp(pid->p_gain, fp_error);
 171
 172        pid->integral += fp_error;
 173
 174        /* limit the integral term */
 175        integral_limit = int_tofp(30);
 176        if (pid->integral > integral_limit)
 177                pid->integral = integral_limit;
 178        if (pid->integral < -integral_limit)
 179                pid->integral = -integral_limit;
 180
 181        dterm = mul_fp(pid->d_gain, (err - pid->last_err));
 182        pid->last_err = err;
 183
 184        result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
 185
 186        return (signed int)fp_toint(result);
 187}
 188
 189static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
 190{
 191        pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct);
 192        pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct);
 193        pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct);
 194
 195        pid_reset(&cpu->pid,
 196                cpu->pstate_policy->setpoint,
 197                100,
 198                cpu->pstate_policy->deadband,
 199                0);
 200}
 201
 202static inline void intel_pstate_idle_pid_reset(struct cpudata *cpu)
 203{
 204        pid_p_gain_set(&cpu->idle_pid, cpu->pstate_policy->p_gain_pct);
 205        pid_d_gain_set(&cpu->idle_pid, cpu->pstate_policy->d_gain_pct);
 206        pid_i_gain_set(&cpu->idle_pid, cpu->pstate_policy->i_gain_pct);
 207
 208        pid_reset(&cpu->idle_pid,
 209                75,
 210                50,
 211                cpu->pstate_policy->deadband,
 212                0);
 213}
 214
 215static inline void intel_pstate_reset_all_pid(void)
 216{
 217        unsigned int cpu;
 218        for_each_online_cpu(cpu) {
 219                if (all_cpu_data[cpu])
 220                        intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
 221        }
 222}
 223
 224/************************** debugfs begin ************************/
 225static int pid_param_set(void *data, u64 val)
 226{
 227        *(u32 *)data = val;
 228        intel_pstate_reset_all_pid();
 229        return 0;
 230}
 231static int pid_param_get(void *data, u64 *val)
 232{
 233        *val = *(u32 *)data;
 234        return 0;
 235}
 236DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
 237                        pid_param_set, "%llu\n");
 238
 239struct pid_param {
 240        char *name;
 241        void *value;
 242};
 243
 244static struct pid_param pid_files[] = {
 245        {"sample_rate_ms", &default_policy.sample_rate_ms},
 246        {"d_gain_pct", &default_policy.d_gain_pct},
 247        {"i_gain_pct", &default_policy.i_gain_pct},
 248        {"deadband", &default_policy.deadband},
 249        {"setpoint", &default_policy.setpoint},
 250        {"p_gain_pct", &default_policy.p_gain_pct},
 251        {NULL, NULL}
 252};
 253
 254static struct dentry *debugfs_parent;
 255static void intel_pstate_debug_expose_params(void)
 256{
 257        int i = 0;
 258
 259        debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
 260        if (IS_ERR_OR_NULL(debugfs_parent))
 261                return;
 262        while (pid_files[i].name) {
 263                debugfs_create_file(pid_files[i].name, 0660,
 264                                debugfs_parent, pid_files[i].value,
 265                                &fops_pid_param);
 266                i++;
 267        }
 268}
 269
 270/************************** debugfs end ************************/
 271
 272/************************** sysfs begin ************************/
 273#define show_one(file_name, object)                                     \
 274        static ssize_t show_##file_name                                 \
 275        (struct kobject *kobj, struct attribute *attr, char *buf)       \
 276        {                                                               \
 277                return sprintf(buf, "%u\n", limits.object);             \
 278        }
 279
 280static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
 281                                const char *buf, size_t count)
 282{
 283        unsigned int input;
 284        int ret;
 285        ret = sscanf(buf, "%u", &input);
 286        if (ret != 1)
 287                return -EINVAL;
 288        limits.no_turbo = clamp_t(int, input, 0 , 1);
 289
 290        return count;
 291}
 292
 293static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
 294                                const char *buf, size_t count)
 295{
 296        unsigned int input;
 297        int ret;
 298        ret = sscanf(buf, "%u", &input);
 299        if (ret != 1)
 300                return -EINVAL;
 301
 302        limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
 303        limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
 304        limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
 305        return count;
 306}
 307
 308static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
 309                                const char *buf, size_t count)
 310{
 311        unsigned int input;
 312        int ret;
 313        ret = sscanf(buf, "%u", &input);
 314        if (ret != 1)
 315                return -EINVAL;
 316        limits.min_perf_pct = clamp_t(int, input, 0 , 100);
 317        limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
 318
 319        return count;
 320}
 321
 322show_one(no_turbo, no_turbo);
 323show_one(max_perf_pct, max_perf_pct);
 324show_one(min_perf_pct, min_perf_pct);
 325
 326define_one_global_rw(no_turbo);
 327define_one_global_rw(max_perf_pct);
 328define_one_global_rw(min_perf_pct);
 329
 330static struct attribute *intel_pstate_attributes[] = {
 331        &no_turbo.attr,
 332        &max_perf_pct.attr,
 333        &min_perf_pct.attr,
 334        NULL
 335};
 336
 337static struct attribute_group intel_pstate_attr_group = {
 338        .attrs = intel_pstate_attributes,
 339};
 340static struct kobject *intel_pstate_kobject;
 341
 342static void intel_pstate_sysfs_expose_params(void)
 343{
 344        int rc;
 345
 346        intel_pstate_kobject = kobject_create_and_add("intel_pstate",
 347                                                &cpu_subsys.dev_root->kobj);
 348        BUG_ON(!intel_pstate_kobject);
 349        rc = sysfs_create_group(intel_pstate_kobject,
 350                                &intel_pstate_attr_group);
 351        BUG_ON(rc);
 352}
 353
 354/************************** sysfs end ************************/
 355
 356static int intel_pstate_min_pstate(void)
 357{
 358        u64 value;
 359        rdmsrl(MSR_PLATFORM_INFO, value);
 360        return (value >> 40) & 0xFF;
 361}
 362
 363static int intel_pstate_max_pstate(void)
 364{
 365        u64 value;
 366        rdmsrl(MSR_PLATFORM_INFO, value);
 367        return (value >> 8) & 0xFF;
 368}
 369
 370static int intel_pstate_turbo_pstate(void)
 371{
 372        u64 value;
 373        int nont, ret;
 374        rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
 375        nont = intel_pstate_max_pstate();
 376        ret = ((value) & 255);
 377        if (ret <= nont)
 378                ret = nont;
 379        return ret;
 380}
 381
 382static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
 383{
 384        int max_perf = cpu->pstate.turbo_pstate;
 385        int min_perf;
 386        if (limits.no_turbo)
 387                max_perf = cpu->pstate.max_pstate;
 388
 389        max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
 390        *max = clamp_t(int, max_perf,
 391                        cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
 392
 393        min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
 394        *min = clamp_t(int, min_perf,
 395                        cpu->pstate.min_pstate, max_perf);
 396}
 397
 398static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
 399{
 400        int max_perf, min_perf;
 401
 402        intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
 403
 404        pstate = clamp_t(int, pstate, min_perf, max_perf);
 405
 406        if (pstate == cpu->pstate.current_pstate)
 407                return;
 408
 409#ifndef MODULE
 410        trace_cpu_frequency(pstate * 100000, cpu->cpu);
 411#endif
 412        cpu->pstate.current_pstate = pstate;
 413        wrmsrl(MSR_IA32_PERF_CTL, pstate << 8);
 414
 415}
 416
 417static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
 418{
 419        int target;
 420        target = cpu->pstate.current_pstate + steps;
 421
 422        intel_pstate_set_pstate(cpu, target);
 423}
 424
 425static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
 426{
 427        int target;
 428        target = cpu->pstate.current_pstate - steps;
 429        intel_pstate_set_pstate(cpu, target);
 430}
 431
 432static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
 433{
 434        sprintf(cpu->name, "Intel 2nd generation core");
 435
 436        cpu->pstate.min_pstate = intel_pstate_min_pstate();
 437        cpu->pstate.max_pstate = intel_pstate_max_pstate();
 438        cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate();
 439
 440        /*
 441         * goto max pstate so we don't slow up boot if we are built-in if we are
 442         * a module we will take care of it during normal operation
 443         */
 444        intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
 445}
 446
 447static inline void intel_pstate_calc_busy(struct cpudata *cpu,
 448                                        struct sample *sample)
 449{
 450        u64 core_pct;
 451        core_pct = div64_u64(sample->aperf * 100, sample->mperf);
 452        sample->freq = cpu->pstate.max_pstate * core_pct * 1000;
 453
 454        sample->core_pct_busy = core_pct;
 455}
 456
 457static inline void intel_pstate_sample(struct cpudata *cpu)
 458{
 459        u64 aperf, mperf;
 460
 461        rdmsrl(MSR_IA32_APERF, aperf);
 462        rdmsrl(MSR_IA32_MPERF, mperf);
 463        cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
 464        cpu->samples[cpu->sample_ptr].aperf = aperf;
 465        cpu->samples[cpu->sample_ptr].mperf = mperf;
 466        cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
 467        cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
 468
 469        intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
 470
 471        cpu->prev_aperf = aperf;
 472        cpu->prev_mperf = mperf;
 473}
 474
 475static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
 476{
 477        int sample_time, delay;
 478
 479        sample_time = cpu->pstate_policy->sample_rate_ms;
 480        delay = msecs_to_jiffies(sample_time);
 481        mod_timer_pinned(&cpu->timer, jiffies + delay);
 482}
 483
 484static inline void intel_pstate_idle_mode(struct cpudata *cpu)
 485{
 486        cpu->idle_mode = 1;
 487}
 488
 489static inline void intel_pstate_normal_mode(struct cpudata *cpu)
 490{
 491        cpu->idle_mode = 0;
 492}
 493
 494static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu)
 495{
 496        int32_t busy_scaled;
 497        int32_t core_busy, turbo_pstate, current_pstate;
 498
 499        core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy);
 500        turbo_pstate = int_tofp(cpu->pstate.turbo_pstate);
 501        current_pstate = int_tofp(cpu->pstate.current_pstate);
 502        busy_scaled = mul_fp(core_busy, div_fp(turbo_pstate, current_pstate));
 503
 504        return fp_toint(busy_scaled);
 505}
 506
 507static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
 508{
 509        int busy_scaled;
 510        struct _pid *pid;
 511        signed int ctl = 0;
 512        int steps;
 513
 514        pid = &cpu->pid;
 515        busy_scaled = intel_pstate_get_scaled_busy(cpu);
 516
 517        ctl = pid_calc(pid, busy_scaled);
 518
 519        steps = abs(ctl);
 520        if (ctl < 0)
 521                intel_pstate_pstate_increase(cpu, steps);
 522        else
 523                intel_pstate_pstate_decrease(cpu, steps);
 524}
 525
 526static inline void intel_pstate_adjust_idle_pstate(struct cpudata *cpu)
 527{
 528        int busy_scaled;
 529        struct _pid *pid;
 530        int ctl = 0;
 531        int steps;
 532
 533        pid = &cpu->idle_pid;
 534
 535        busy_scaled = intel_pstate_get_scaled_busy(cpu);
 536
 537        ctl = pid_calc(pid, 100 - busy_scaled);
 538
 539        steps = abs(ctl);
 540        if (ctl < 0)
 541                intel_pstate_pstate_decrease(cpu, steps);
 542        else
 543                intel_pstate_pstate_increase(cpu, steps);
 544
 545        if (cpu->pstate.current_pstate == cpu->pstate.min_pstate)
 546                intel_pstate_normal_mode(cpu);
 547}
 548
 549static void intel_pstate_timer_func(unsigned long __data)
 550{
 551        struct cpudata *cpu = (struct cpudata *) __data;
 552
 553        intel_pstate_sample(cpu);
 554        intel_pstate_adjust_busy_pstate(cpu);
 555
 556        if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
 557                cpu->min_pstate_count++;
 558                if (!(cpu->min_pstate_count % 5)) {
 559                        intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
 560                }
 561        } else
 562                cpu->min_pstate_count = 0;
 563
 564        intel_pstate_set_sample_time(cpu);
 565}
 566
 567#define ICPU(model, policy) \
 568        { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy }
 569
 570static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
 571        ICPU(0x2a, default_policy),
 572        ICPU(0x2d, default_policy),
 573        {}
 574};
 575MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
 576
 577static int intel_pstate_init_cpu(unsigned int cpunum)
 578{
 579
 580        const struct x86_cpu_id *id;
 581        struct cpudata *cpu;
 582
 583        id = x86_match_cpu(intel_pstate_cpu_ids);
 584        if (!id)
 585                return -ENODEV;
 586
 587        all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
 588        if (!all_cpu_data[cpunum])
 589                return -ENOMEM;
 590
 591        cpu = all_cpu_data[cpunum];
 592
 593        intel_pstate_get_cpu_pstates(cpu);
 594
 595        cpu->cpu = cpunum;
 596        cpu->pstate_policy =
 597                (struct pstate_adjust_policy *)id->driver_data;
 598        init_timer_deferrable(&cpu->timer);
 599        cpu->timer.function = intel_pstate_timer_func;
 600        cpu->timer.data =
 601                (unsigned long)cpu;
 602        cpu->timer.expires = jiffies + HZ/100;
 603        intel_pstate_busy_pid_reset(cpu);
 604        intel_pstate_idle_pid_reset(cpu);
 605        intel_pstate_sample(cpu);
 606        intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
 607
 608        add_timer_on(&cpu->timer, cpunum);
 609
 610        pr_info("Intel pstate controlling: cpu %d\n", cpunum);
 611
 612        return 0;
 613}
 614
 615static unsigned int intel_pstate_get(unsigned int cpu_num)
 616{
 617        struct sample *sample;
 618        struct cpudata *cpu;
 619
 620        cpu = all_cpu_data[cpu_num];
 621        if (!cpu)
 622                return 0;
 623        sample = &cpu->samples[cpu->sample_ptr];
 624        return sample->freq;
 625}
 626
 627static int intel_pstate_set_policy(struct cpufreq_policy *policy)
 628{
 629        struct cpudata *cpu;
 630        int min, max;
 631
 632        cpu = all_cpu_data[policy->cpu];
 633
 634        if (!policy->cpuinfo.max_freq)
 635                return -ENODEV;
 636
 637        intel_pstate_get_min_max(cpu, &min, &max);
 638
 639        limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
 640        limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
 641        limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
 642
 643        limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
 644        limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
 645        limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
 646        limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
 647
 648        if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
 649                limits.min_perf_pct = 100;
 650                limits.min_perf = int_tofp(1);
 651                limits.max_perf_pct = 100;
 652                limits.max_perf = int_tofp(1);
 653                limits.no_turbo = 0;
 654        }
 655
 656        return 0;
 657}
 658
 659static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
 660{
 661        cpufreq_verify_within_limits(policy,
 662                                policy->cpuinfo.min_freq,
 663                                policy->cpuinfo.max_freq);
 664
 665        if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
 666                (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
 667                return -EINVAL;
 668
 669        return 0;
 670}
 671
 672static int __cpuinit intel_pstate_cpu_exit(struct cpufreq_policy *policy)
 673{
 674        int cpu = policy->cpu;
 675
 676        del_timer(&all_cpu_data[cpu]->timer);
 677        kfree(all_cpu_data[cpu]);
 678        all_cpu_data[cpu] = NULL;
 679        return 0;
 680}
 681
 682static int __cpuinit intel_pstate_cpu_init(struct cpufreq_policy *policy)
 683{
 684        int rc, min_pstate, max_pstate;
 685        struct cpudata *cpu;
 686
 687        rc = intel_pstate_init_cpu(policy->cpu);
 688        if (rc)
 689                return rc;
 690
 691        cpu = all_cpu_data[policy->cpu];
 692
 693        if (!limits.no_turbo &&
 694                limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
 695                policy->policy = CPUFREQ_POLICY_PERFORMANCE;
 696        else
 697                policy->policy = CPUFREQ_POLICY_POWERSAVE;
 698
 699        intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
 700        policy->min = min_pstate * 100000;
 701        policy->max = max_pstate * 100000;
 702
 703        /* cpuinfo and default policy values */
 704        policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
 705        policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
 706        policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
 707        cpumask_set_cpu(policy->cpu, policy->cpus);
 708
 709        return 0;
 710}
 711
 712static struct cpufreq_driver intel_pstate_driver = {
 713        .flags          = CPUFREQ_CONST_LOOPS,
 714        .verify         = intel_pstate_verify_policy,
 715        .setpolicy      = intel_pstate_set_policy,
 716        .get            = intel_pstate_get,
 717        .init           = intel_pstate_cpu_init,
 718        .exit           = intel_pstate_cpu_exit,
 719        .name           = "intel_pstate",
 720        .owner          = THIS_MODULE,
 721};
 722
 723static int __initdata no_load;
 724
 725static int intel_pstate_msrs_not_valid(void)
 726{
 727        /* Check that all the msr's we are using are valid. */
 728        u64 aperf, mperf, tmp;
 729
 730        rdmsrl(MSR_IA32_APERF, aperf);
 731        rdmsrl(MSR_IA32_MPERF, mperf);
 732
 733        if (!intel_pstate_min_pstate() ||
 734                !intel_pstate_max_pstate() ||
 735                !intel_pstate_turbo_pstate())
 736                return -ENODEV;
 737
 738        rdmsrl(MSR_IA32_APERF, tmp);
 739        if (!(tmp - aperf))
 740                return -ENODEV;
 741
 742        rdmsrl(MSR_IA32_MPERF, tmp);
 743        if (!(tmp - mperf))
 744                return -ENODEV;
 745
 746        return 0;
 747}
 748static int __init intel_pstate_init(void)
 749{
 750        int cpu, rc = 0;
 751        const struct x86_cpu_id *id;
 752
 753        if (no_load)
 754                return -ENODEV;
 755
 756        id = x86_match_cpu(intel_pstate_cpu_ids);
 757        if (!id)
 758                return -ENODEV;
 759
 760        if (intel_pstate_msrs_not_valid())
 761                return -ENODEV;
 762
 763        pr_info("Intel P-state driver initializing.\n");
 764
 765        all_cpu_data = vmalloc(sizeof(void *) * num_possible_cpus());
 766        if (!all_cpu_data)
 767                return -ENOMEM;
 768        memset(all_cpu_data, 0, sizeof(void *) * num_possible_cpus());
 769
 770        rc = cpufreq_register_driver(&intel_pstate_driver);
 771        if (rc)
 772                goto out;
 773
 774        intel_pstate_debug_expose_params();
 775        intel_pstate_sysfs_expose_params();
 776        return rc;
 777out:
 778        get_online_cpus();
 779        for_each_online_cpu(cpu) {
 780                if (all_cpu_data[cpu]) {
 781                        del_timer_sync(&all_cpu_data[cpu]->timer);
 782                        kfree(all_cpu_data[cpu]);
 783                }
 784        }
 785
 786        put_online_cpus();
 787        vfree(all_cpu_data);
 788        return -ENODEV;
 789}
 790device_initcall(intel_pstate_init);
 791
 792static int __init intel_pstate_setup(char *str)
 793{
 794        if (!str)
 795                return -EINVAL;
 796
 797        if (!strcmp(str, "disable"))
 798                no_load = 1;
 799        return 0;
 800}
 801early_param("intel_pstate", intel_pstate_setup);
 802
 803MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
 804MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
 805MODULE_LICENSE("GPL");
 806
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.