| 1 | |
| 2 | |
| 3 | * (C) 2002 - 2003 Dominik Brodowski <[email protected]>
|
| 4 | *
|
| 5 | * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/kernel.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/init.h>
|
| 11 | #include <linux/cpufreq.h>
|
| 12 | #include <linux/timex.h>
|
| 13 |
|
| 14 | #include <asm/msr.h>
|
| 15 | #include <asm/processor.h>
|
| 16 | #include <asm/cpu_device_id.h>
|
| 17 | #include <asm/cpuid/api.h>
|
| 18 |
|
| 19 | static struct cpufreq_driver longrun_driver;
|
| 20 |
|
| 21 | |
| 22 | * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz
|
| 23 | * values into per cent values. In TMTA microcode, the following is valid:
|
| 24 | * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
|
| 25 |
|
| 26 | static unsigned int longrun_low_freq, longrun_high_freq;
|
| 27 |
|
| 28 |
|
| 29 | |
| 30 | * longrun_get_policy - get the current LongRun policy
|
| 31 | * @policy: struct cpufreq_policy where current policy is written into
|
| 32 | *
|
| 33 | * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
|
| 34 | * and MSR_TMTA_LONGRUN_CTRL
|
| 35 |
|
| 36 | static void longrun_get_policy(struct cpufreq_policy *policy)
|
| 37 | {
|
| 38 | struct msr msr;
|
| 39 |
|
| 40 | rdmsrq(MSR_TMTA_LONGRUN_FLAGS, msr.q);
|
| 41 | pr_debug("longrun flags are %x - %x\n", msr.l, msr.h);
|
| 42 | if (msr.l & 0x01)
|
| 43 | policy->policy = CPUFREQ_POLICY_PERFORMANCE;
|
| 44 | else
|
| 45 | policy->policy = CPUFREQ_POLICY_POWERSAVE;
|
| 46 |
|
| 47 | rdmsrq(MSR_TMTA_LONGRUN_CTRL, msr.q);
|
| 48 | pr_debug("longrun ctrl is %x - %x\n", msr.l, msr.h);
|
| 49 | msr.l &= 0x0000007F;
|
| 50 | msr.h &= 0x0000007F;
|
| 51 |
|
| 52 | if (longrun_high_freq <= longrun_low_freq) {
|
| 53 |
|
| 54 | policy->min = policy->max = longrun_high_freq;
|
| 55 | } else {
|
| 56 | policy->min = longrun_low_freq + msr.l *
|
| 57 | ((longrun_high_freq - longrun_low_freq) / 100);
|
| 58 | policy->max = longrun_low_freq + msr.h *
|
| 59 | ((longrun_high_freq - longrun_low_freq) / 100);
|
| 60 | }
|
| 61 | policy->cpu = 0;
|
| 62 | }
|
| 63 |
|
| 64 |
|
| 65 | |
| 66 | * longrun_set_policy - sets a new CPUFreq policy
|
| 67 | * @policy: new policy
|
| 68 | *
|
| 69 | * Sets a new CPUFreq policy on LongRun-capable processors. This function
|
| 70 | * has to be called with cpufreq_driver locked.
|
| 71 |
|
| 72 | static int longrun_set_policy(struct cpufreq_policy *policy)
|
| 73 | {
|
| 74 | struct msr msr;
|
| 75 | u32 pctg_lo, pctg_hi;
|
| 76 |
|
| 77 | if (!policy)
|
| 78 | return -EINVAL;
|
| 79 |
|
| 80 | if (longrun_high_freq <= longrun_low_freq) {
|
| 81 |
|
| 82 | pctg_lo = pctg_hi = 100;
|
| 83 | } else {
|
| 84 | pctg_lo = (policy->min - longrun_low_freq) /
|
| 85 | ((longrun_high_freq - longrun_low_freq) / 100);
|
| 86 | pctg_hi = (policy->max - longrun_low_freq) /
|
| 87 | ((longrun_high_freq - longrun_low_freq) / 100);
|
| 88 | }
|
| 89 |
|
| 90 | if (pctg_hi > 100)
|
| 91 | pctg_hi = 100;
|
| 92 | if (pctg_lo > pctg_hi)
|
| 93 | pctg_lo = pctg_hi;
|
| 94 |
|
| 95 |
|
| 96 | rdmsrq(MSR_TMTA_LONGRUN_FLAGS, msr.q);
|
| 97 | msr.l &= 0xFFFFFFFE;
|
| 98 | switch (policy->policy) {
|
| 99 | case CPUFREQ_POLICY_PERFORMANCE:
|
| 100 | msr.l |= 0x00000001;
|
| 101 | break;
|
| 102 | case CPUFREQ_POLICY_POWERSAVE:
|
| 103 | break;
|
| 104 | }
|
| 105 | wrmsrq(MSR_TMTA_LONGRUN_FLAGS, msr.q);
|
| 106 |
|
| 107 |
|
| 108 | rdmsrq(MSR_TMTA_LONGRUN_CTRL, msr.q);
|
| 109 | msr.l &= 0xFFFFFF80;
|
| 110 | msr.h &= 0xFFFFFF80;
|
| 111 | msr.l |= pctg_lo;
|
| 112 | msr.h |= pctg_hi;
|
| 113 | wrmsrq(MSR_TMTA_LONGRUN_CTRL, msr.q);
|
| 114 |
|
| 115 | return 0;
|
| 116 | }
|
| 117 |
|
| 118 |
|
| 119 | |
| 120 | * longrun_verify_poliy - verifies a new CPUFreq policy
|
| 121 | * @policy: the policy to verify
|
| 122 | *
|
| 123 | * Validates a new CPUFreq policy. This function has to be called with
|
| 124 | * cpufreq_driver locked.
|
| 125 |
|
| 126 | static int longrun_verify_policy(struct cpufreq_policy_data *policy)
|
| 127 | {
|
| 128 | if (!policy)
|
| 129 | return -EINVAL;
|
| 130 |
|
| 131 | policy->cpu = 0;
|
| 132 | cpufreq_verify_within_cpu_limits(policy);
|
| 133 |
|
| 134 | return 0;
|
| 135 | }
|
| 136 |
|
| 137 | static unsigned int longrun_get(unsigned int cpu)
|
| 138 | {
|
| 139 | u32 eax, ebx, ecx, edx;
|
| 140 |
|
| 141 | if (cpu)
|
| 142 | return 0;
|
| 143 |
|
| 144 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
|
| 145 | pr_debug("cpuid eax is %u\n", eax);
|
| 146 |
|
| 147 | return eax * 1000;
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | * longrun_determine_freqs - determines the lowest and highest possible core frequency
|
| 152 | * @low_freq: an int to put the lowest frequency into
|
| 153 | * @high_freq: an int to put the highest frequency into
|
| 154 | *
|
| 155 | * Determines the lowest and highest possible core frequencies on this CPU.
|
| 156 | * This is necessary to calculate the performance percentage according to
|
| 157 | * TMTA rules:
|
| 158 | * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
|
| 159 |
|
| 160 | static int longrun_determine_freqs(unsigned int *low_freq,
|
| 161 | unsigned int *high_freq)
|
| 162 | {
|
| 163 | struct msr msr, save;
|
| 164 | u32 eax, ebx, ecx, edx;
|
| 165 | u32 try_hi;
|
| 166 | struct cpuinfo_x86 *c = &cpu_data(0);
|
| 167 |
|
| 168 | if (!low_freq || !high_freq)
|
| 169 | return -EINVAL;
|
| 170 |
|
| 171 | if (cpu_has(c, X86_FEATURE_LRTI)) {
|
| 172 | |
| 173 | * detection is a bit easier:
|
| 174 | * For minimum frequency, read out the maximum
|
| 175 | * level (msr_hi), write that into "currently
|
| 176 | * selected level", and read out the frequency.
|
| 177 | * For maximum frequency, read out level zero.
|
| 178 |
|
| 179 |
|
| 180 | rdmsrq(MSR_TMTA_LRTI_READOUT, msr.q);
|
| 181 | msr.l = msr.h;
|
| 182 | wrmsrq(MSR_TMTA_LRTI_READOUT, msr.q);
|
| 183 | rdmsrq(MSR_TMTA_LRTI_VOLT_MHZ, msr.q);
|
| 184 | *low_freq = msr.l * 1000;
|
| 185 |
|
| 186 |
|
| 187 | msr.l = 0;
|
| 188 | wrmsrq(MSR_TMTA_LRTI_READOUT, msr.q);
|
| 189 | rdmsrq(MSR_TMTA_LRTI_VOLT_MHZ, msr.q);
|
| 190 | *high_freq = msr.l * 1000;
|
| 191 |
|
| 192 | pr_debug("longrun table interface told %u - %u kHz\n",
|
| 193 | *low_freq, *high_freq);
|
| 194 |
|
| 195 | if (*low_freq > *high_freq)
|
| 196 | *low_freq = *high_freq;
|
| 197 | return 0;
|
| 198 | }
|
| 199 |
|
| 200 |
|
| 201 | *high_freq = (cpu_khz / 1000);
|
| 202 | *high_freq = *high_freq * 1000;
|
| 203 | pr_debug("high frequency is %u kHz\n", *high_freq);
|
| 204 |
|
| 205 |
|
| 206 | rdmsrq(MSR_TMTA_LONGRUN_CTRL, msr.q);
|
| 207 | save.l = msr.l & 0x0000007F;
|
| 208 | save.h = msr.h & 0x0000007F;
|
| 209 |
|
| 210 | |
| 211 | * upper limit to make the calculation more accurate.
|
| 212 |
|
| 213 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
|
| 214 | |
| 215 | * on some barrier values
|
| 216 | for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
|
| 217 |
|
| 218 | msr.l &= 0xFFFFFF80;
|
| 219 | msr.h &= 0xFFFFFF80;
|
| 220 | msr.h |= try_hi;
|
| 221 | wrmsrq(MSR_TMTA_LONGRUN_CTRL, msr.q);
|
| 222 |
|
| 223 |
|
| 224 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
|
| 225 |
|
| 226 |
|
| 227 | wrmsrq(MSR_TMTA_LONGRUN_CTRL, save.q);
|
| 228 | }
|
| 229 | pr_debug("percentage is %u %%, freq is %u MHz\n", ecx, eax);
|
| 230 |
|
| 231 | |
| 232 | * eqals
|
| 233 | * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
|
| 234 | *
|
| 235 | * high_freq * perf_pctg is stored tempoarily into "ebx".
|
| 236 |
|
| 237 | ebx = (((cpu_khz / 1000) * ecx) / 100);
|
| 238 |
|
| 239 | if ((ecx > 95) || (ecx == 0) || (eax < ebx))
|
| 240 | return -EIO;
|
| 241 |
|
| 242 | edx = ((eax - ebx) * 100) / (100 - ecx);
|
| 243 | *low_freq = edx * 1000;
|
| 244 |
|
| 245 | pr_debug("low frequency is %u kHz\n", *low_freq);
|
| 246 |
|
| 247 | if (*low_freq > *high_freq)
|
| 248 | *low_freq = *high_freq;
|
| 249 |
|
| 250 | return 0;
|
| 251 | }
|
| 252 |
|
| 253 |
|
| 254 | static int longrun_cpu_init(struct cpufreq_policy *policy)
|
| 255 | {
|
| 256 | int result = 0;
|
| 257 |
|
| 258 |
|
| 259 | if (policy->cpu != 0)
|
| 260 | return -ENODEV;
|
| 261 |
|
| 262 |
|
| 263 | result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
|
| 264 | if (result)
|
| 265 | return result;
|
| 266 |
|
| 267 |
|
| 268 | policy->cpuinfo.min_freq = longrun_low_freq;
|
| 269 | policy->cpuinfo.max_freq = longrun_high_freq;
|
| 270 | longrun_get_policy(policy);
|
| 271 |
|
| 272 | return 0;
|
| 273 | }
|
| 274 |
|
| 275 |
|
| 276 | static struct cpufreq_driver longrun_driver = {
|
| 277 | .flags = CPUFREQ_CONST_LOOPS,
|
| 278 | .verify = longrun_verify_policy,
|
| 279 | .setpolicy = longrun_set_policy,
|
| 280 | .get = longrun_get,
|
| 281 | .init = longrun_cpu_init,
|
| 282 | .name = "longrun",
|
| 283 | };
|
| 284 |
|
| 285 | static const struct x86_cpu_id longrun_ids[] = {
|
| 286 | X86_MATCH_VENDOR_FEATURE(TRANSMETA, X86_FEATURE_LONGRUN, NULL),
|
| 287 | {}
|
| 288 | };
|
| 289 | MODULE_DEVICE_TABLE(x86cpu, longrun_ids);
|
| 290 |
|
| 291 | |
| 292 | * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
|
| 293 | *
|
| 294 | * Initializes the LongRun support.
|
| 295 |
|
| 296 | static int __init longrun_init(void)
|
| 297 | {
|
| 298 | if (!x86_match_cpu(longrun_ids))
|
| 299 | return -ENODEV;
|
| 300 | return cpufreq_register_driver(&longrun_driver);
|
| 301 | }
|
| 302 |
|
| 303 |
|
| 304 | |
| 305 | * longrun_exit - unregisters LongRun support
|
| 306 |
|
| 307 | static void __exit longrun_exit(void)
|
| 308 | {
|
| 309 | cpufreq_unregister_driver(&longrun_driver);
|
| 310 | }
|
| 311 |
|
| 312 |
|
| 313 | MODULE_AUTHOR("Dominik Brodowski <[email protected]>");
|
| 314 | MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
|
| 315 | "Efficeon processors.");
|
| 316 | MODULE_LICENSE("GPL");
|
| 317 |
|
| 318 | module_init(longrun_init);
|
| 319 | module_exit(longrun_exit);
|