speedstep-centrino.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * cpufreq driver for Enhanced SpeedStep, as found in Intel's Pentium
  3. * M (part of the Centrino chipset).
  4. *
  5. * Since the original Pentium M, most new Intel CPUs support Enhanced
  6. * SpeedStep.
  7. *
  8. * Despite the "SpeedStep" in the name, this is almost entirely unlike
  9. * traditional SpeedStep.
  10. *
  11. * Modelled on speedstep.c
  12. *
  13. * Copyright (C) 2003 Jeremy Fitzhardinge <jeremy@goop.org>
  14. */
  15. #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>
  23. #include <asm/msr.h>
  24. #include <asm/processor.h>
  25. #include <asm/cpufeature.h>
  26. #include <asm/cpu_device_id.h>
  27. #define PFX "speedstep-centrino: "
  28. #define MAINTAINER "linux-pm@vger.kernel.org"
  29. #define INTEL_MSR_RANGE (0xffff)
  30. struct cpu_id
  31. {
  32. __u8 x86; /* CPU family */
  33. __u8 x86_model; /* model */
  34. __u8 x86_mask; /* stepping */
  35. };
  36. enum {
  37. CPU_BANIAS,
  38. CPU_DOTHAN_A1,
  39. CPU_DOTHAN_A2,
  40. CPU_DOTHAN_B0,
  41. CPU_MP4HT_D0,
  42. CPU_MP4HT_E0,
  43. };
  44. static const struct cpu_id cpu_ids[] = {
  45. [CPU_BANIAS] = { 6, 9, 5 },
  46. [CPU_DOTHAN_A1] = { 6, 13, 1 },
  47. [CPU_DOTHAN_A2] = { 6, 13, 2 },
  48. [CPU_DOTHAN_B0] = { 6, 13, 6 },
  49. [CPU_MP4HT_D0] = {15, 3, 4 },
  50. [CPU_MP4HT_E0] = {15, 4, 1 },
  51. };
  52. #define N_IDS ARRAY_SIZE(cpu_ids)
  53. struct cpu_model
  54. {
  55. const struct cpu_id *cpu_id;
  56. const char *model_name;
  57. unsigned max_freq; /* max clock in kHz */
  58. struct cpufreq_frequency_table *op_points; /* clock/voltage pairs */
  59. };
  60. static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,
  61. const struct cpu_id *x);
  62. /* Operating points for current CPU */
  63. static DEFINE_PER_CPU(struct cpu_model *, centrino_model);
  64. static DEFINE_PER_CPU(const struct cpu_id *, centrino_cpu);
  65. static struct cpufreq_driver centrino_driver;
  66. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE
  67. /* Computes the correct form for IA32_PERF_CTL MSR for a particular
  68. frequency/voltage operating point; frequency in MHz, volts in mV.
  69. This is stored as "driver_data" in the structure. */
  70. #define OP(mhz, mv) \
  71. { \
  72. .frequency = (mhz) * 1000, \
  73. .driver_data = (((mhz)/100) << 8) | ((mv - 700) / 16) \
  74. }
  75. /*
  76. * These voltage tables were derived from the Intel Pentium M
  77. * datasheet, document 25261202.pdf, Table 5. I have verified they
  78. * are consistent with my IBM ThinkPad X31, which has a 1.3GHz Pentium
  79. * M.
  80. */
  81. /* Ultra Low Voltage Intel Pentium M processor 900MHz (Banias) */
  82. static struct cpufreq_frequency_table banias_900[] =
  83. {
  84. OP(600, 844),
  85. OP(800, 988),
  86. OP(900, 1004),
  87. { .frequency = CPUFREQ_TABLE_END }
  88. };
  89. /* Ultra Low Voltage Intel Pentium M processor 1000MHz (Banias) */
  90. static struct cpufreq_frequency_table banias_1000[] =
  91. {
  92. OP(600, 844),
  93. OP(800, 972),
  94. OP(900, 988),
  95. OP(1000, 1004),
  96. { .frequency = CPUFREQ_TABLE_END }
  97. };
  98. /* Low Voltage Intel Pentium M processor 1.10GHz (Banias) */
  99. static struct cpufreq_frequency_table banias_1100[] =
  100. {
  101. OP( 600, 956),
  102. OP( 800, 1020),
  103. OP( 900, 1100),
  104. OP(1000, 1164),
  105. OP(1100, 1180),
  106. { .frequency = CPUFREQ_TABLE_END }
  107. };
  108. /* Low Voltage Intel Pentium M processor 1.20GHz (Banias) */
  109. static struct cpufreq_frequency_table banias_1200[] =
  110. {
  111. OP( 600, 956),
  112. OP( 800, 1004),
  113. OP( 900, 1020),
  114. OP(1000, 1100),
  115. OP(1100, 1164),
  116. OP(1200, 1180),
  117. { .frequency = CPUFREQ_TABLE_END }
  118. };
  119. /* Intel Pentium M processor 1.30GHz (Banias) */
  120. static struct cpufreq_frequency_table banias_1300[] =
  121. {
  122. OP( 600, 956),
  123. OP( 800, 1260),
  124. OP(1000, 1292),
  125. OP(1200, 1356),
  126. OP(1300, 1388),
  127. { .frequency = CPUFREQ_TABLE_END }
  128. };
  129. /* Intel Pentium M processor 1.40GHz (Banias) */
  130. static struct cpufreq_frequency_table banias_1400[] =
  131. {
  132. OP( 600, 956),
  133. OP( 800, 1180),
  134. OP(1000, 1308),
  135. OP(1200, 1436),
  136. OP(1400, 1484),
  137. { .frequency = CPUFREQ_TABLE_END }
  138. };
  139. /* Intel Pentium M processor 1.50GHz (Banias) */
  140. static struct cpufreq_frequency_table banias_1500[] =
  141. {
  142. OP( 600, 956),
  143. OP( 800, 1116),
  144. OP(1000, 1228),
  145. OP(1200, 1356),
  146. OP(1400, 1452),
  147. OP(1500, 1484),
  148. { .frequency = CPUFREQ_TABLE_END }
  149. };
  150. /* Intel Pentium M processor 1.60GHz (Banias) */
  151. static struct cpufreq_frequency_table banias_1600[] =
  152. {
  153. OP( 600, 956),
  154. OP( 800, 1036),
  155. OP(1000, 1164),
  156. OP(1200, 1276),
  157. OP(1400, 1420),
  158. OP(1600, 1484),
  159. { .frequency = CPUFREQ_TABLE_END }
  160. };
  161. /* Intel Pentium M processor 1.70GHz (Banias) */
  162. static struct cpufreq_frequency_table banias_1700[] =
  163. {
  164. OP( 600, 956),
  165. OP( 800, 1004),
  166. OP(1000, 1116),
  167. OP(1200, 1228),
  168. OP(1400, 1308),
  169. OP(1700, 1484),
  170. { .frequency = CPUFREQ_TABLE_END }
  171. };
  172. #undef OP
  173. #define _BANIAS(cpuid, max, name) \
  174. { .cpu_id = cpuid, \
  175. .model_name = "Intel(R) Pentium(R) M processor " name "MHz", \
  176. .max_freq = (max)*1000, \
  177. .op_points = banias_##max, \
  178. }
  179. #define BANIAS(max) _BANIAS(&cpu_ids[CPU_BANIAS], max, #max)
  180. /* CPU models, their operating frequency range, and freq/voltage
  181. operating points */
  182. static struct cpu_model models[] =
  183. {
  184. _BANIAS(&cpu_ids[CPU_BANIAS], 900, " 900"),
  185. BANIAS(1000),
  186. BANIAS(1100),
  187. BANIAS(1200),
  188. BANIAS(1300),
  189. BANIAS(1400),
  190. BANIAS(1500),
  191. BANIAS(1600),
  192. BANIAS(1700),
  193. /* NULL model_name is a wildcard */
  194. { &cpu_ids[CPU_DOTHAN_A1], NULL, 0, NULL },
  195. { &cpu_ids[CPU_DOTHAN_A2], NULL, 0, NULL },
  196. { &cpu_ids[CPU_DOTHAN_B0], NULL, 0, NULL },
  197. { &cpu_ids[CPU_MP4HT_D0], NULL, 0, NULL },
  198. { &cpu_ids[CPU_MP4HT_E0], NULL, 0, NULL },
  199. { NULL, }
  200. };
  201. #undef _BANIAS
  202. #undef BANIAS
  203. static int centrino_cpu_init_table(struct cpufreq_policy *policy)
  204. {
  205. struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);
  206. struct cpu_model *model;
  207. for(model = models; model->cpu_id != NULL; model++)
  208. if (centrino_verify_cpu_id(cpu, model->cpu_id) &&
  209. (model->model_name == NULL ||
  210. strcmp(cpu->x86_model_id, model->model_name) == 0))
  211. break;
  212. if (model->cpu_id == NULL) {
  213. /* No match at all */
  214. pr_debug("no support for CPU model \"%s\": "
  215. "send /proc/cpuinfo to " MAINTAINER "\n",
  216. cpu->x86_model_id);
  217. return -ENOENT;
  218. }
  219. if (model->op_points == NULL) {
  220. /* Matched a non-match */
  221. pr_debug("no table support for CPU model \"%s\"\n",
  222. cpu->x86_model_id);
  223. pr_debug("try using the acpi-cpufreq driver\n");
  224. return -ENOENT;
  225. }
  226. per_cpu(centrino_model, policy->cpu) = model;
  227. pr_debug("found \"%s\": max frequency: %dkHz\n",
  228. model->model_name, model->max_freq);
  229. return 0;
  230. }
  231. #else
  232. static inline int centrino_cpu_init_table(struct cpufreq_policy *policy)
  233. {
  234. return -ENODEV;
  235. }
  236. #endif /* CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE */
  237. static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c,
  238. const struct cpu_id *x)
  239. {
  240. if ((c->x86 == x->x86) &&
  241. (c->x86_model == x->x86_model) &&
  242. (c->x86_mask == x->x86_mask))
  243. return 1;
  244. return 0;
  245. }
  246. /* To be called only after centrino_model is initialized */
  247. static unsigned extract_clock(unsigned msr, unsigned int cpu, int failsafe)
  248. {
  249. int i;
  250. /*
  251. * Extract clock in kHz from PERF_CTL value
  252. * for centrino, as some DSDTs are buggy.
  253. * Ideally, this can be done using the acpi_data structure.
  254. */
  255. if ((per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_BANIAS]) ||
  256. (per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_A1]) ||
  257. (per_cpu(centrino_cpu, cpu) == &cpu_ids[CPU_DOTHAN_B0])) {
  258. msr = (msr >> 8) & 0xff;
  259. return msr * 100000;
  260. }
  261. if ((!per_cpu(centrino_model, cpu)) ||
  262. (!per_cpu(centrino_model, cpu)->op_points))
  263. return 0;
  264. msr &= 0xffff;
  265. for (i = 0;
  266. per_cpu(centrino_model, cpu)->op_points[i].frequency
  267. != CPUFREQ_TABLE_END;
  268. i++) {
  269. if (msr == per_cpu(centrino_model, cpu)->op_points[i].driver_data)
  270. return per_cpu(centrino_model, cpu)->
  271. op_points[i].frequency;
  272. }
  273. if (failsafe)
  274. return per_cpu(centrino_model, cpu)->op_points[i-1].frequency;
  275. else
  276. return 0;
  277. }
  278. /* Return the current CPU frequency in kHz */
  279. static unsigned int get_cur_freq(unsigned int cpu)
  280. {
  281. unsigned l, h;
  282. unsigned clock_freq;
  283. rdmsr_on_cpu(cpu, MSR_IA32_PERF_STATUS, &l, &h);
  284. clock_freq = extract_clock(l, cpu, 0);
  285. if (unlikely(clock_freq == 0)) {
  286. /*
  287. * On some CPUs, we can see transient MSR values (which are
  288. * not present in _PSS), while CPU is doing some automatic
  289. * P-state transition (like TM2). Get the last freq set
  290. * in PERF_CTL.
  291. */
  292. rdmsr_on_cpu(cpu, MSR_IA32_PERF_CTL, &l, &h);
  293. clock_freq = extract_clock(l, cpu, 1);
  294. }
  295. return clock_freq;
  296. }
  297. static int centrino_cpu_init(struct cpufreq_policy *policy)
  298. {
  299. struct cpuinfo_x86 *cpu = &cpu_data(policy->cpu);
  300. unsigned l, h;
  301. int i;
  302. /* Only Intel makes Enhanced Speedstep-capable CPUs */
  303. if (cpu->x86_vendor != X86_VENDOR_INTEL ||
  304. !cpu_has(cpu, X86_FEATURE_EST))
  305. return -ENODEV;
  306. if (cpu_has(cpu, X86_FEATURE_CONSTANT_TSC))
  307. centrino_driver.flags |= CPUFREQ_CONST_LOOPS;
  308. if (policy->cpu != 0)
  309. return -ENODEV;
  310. for (i = 0; i < N_IDS; i++)
  311. if (centrino_verify_cpu_id(cpu, &cpu_ids[i]))
  312. break;
  313. if (i != N_IDS)
  314. per_cpu(centrino_cpu, policy->cpu) = &cpu_ids[i];
  315. if (!per_cpu(centrino_cpu, policy->cpu)) {
  316. pr_debug("found unsupported CPU with "
  317. "Enhanced SpeedStep: send /proc/cpuinfo to "
  318. MAINTAINER "\n");
  319. return -ENODEV;
  320. }
  321. if (centrino_cpu_init_table(policy))
  322. return -ENODEV;
  323. /* Check to see if Enhanced SpeedStep is enabled, and try to
  324. enable it if not. */
  325. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  326. if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  327. l |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
  328. pr_debug("trying to enable Enhanced SpeedStep (%x)\n", l);
  329. wrmsr(MSR_IA32_MISC_ENABLE, l, h);
  330. /* check to see if it stuck */
  331. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  332. if (!(l & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  333. printk(KERN_INFO PFX
  334. "couldn't enable Enhanced SpeedStep\n");
  335. return -ENODEV;
  336. }
  337. }
  338. policy->cpuinfo.transition_latency = 10000;
  339. /* 10uS transition latency */
  340. return cpufreq_table_validate_and_show(policy,
  341. per_cpu(centrino_model, policy->cpu)->op_points);
  342. }
  343. static int centrino_cpu_exit(struct cpufreq_policy *policy)
  344. {
  345. unsigned int cpu = policy->cpu;
  346. if (!per_cpu(centrino_model, cpu))
  347. return -ENODEV;
  348. per_cpu(centrino_model, cpu) = NULL;
  349. return 0;
  350. }
  351. /**
  352. * centrino_setpolicy - set a new CPUFreq policy
  353. * @policy: new policy
  354. * @index: index of target frequency
  355. *
  356. * Sets a new CPUFreq policy.
  357. */
  358. static int centrino_target(struct cpufreq_policy *policy, unsigned int index)
  359. {
  360. unsigned int msr, oldmsr = 0, h = 0, cpu = policy->cpu;
  361. int retval = 0;
  362. unsigned int j, first_cpu;
  363. struct cpufreq_frequency_table *op_points;
  364. cpumask_var_t covered_cpus;
  365. if (unlikely(!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL)))
  366. return -ENOMEM;
  367. if (unlikely(per_cpu(centrino_model, cpu) == NULL)) {
  368. retval = -ENODEV;
  369. goto out;
  370. }
  371. first_cpu = 1;
  372. op_points = &per_cpu(centrino_model, cpu)->op_points[index];
  373. for_each_cpu(j, policy->cpus) {
  374. int good_cpu;
  375. /*
  376. * Support for SMP systems.
  377. * Make sure we are running on CPU that wants to change freq
  378. */
  379. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  380. good_cpu = cpumask_any_and(policy->cpus,
  381. cpu_online_mask);
  382. else
  383. good_cpu = j;
  384. if (good_cpu >= nr_cpu_ids) {
  385. pr_debug("couldn't limit to CPUs in this domain\n");
  386. retval = -EAGAIN;
  387. if (first_cpu) {
  388. /* We haven't started the transition yet. */
  389. goto out;
  390. }
  391. break;
  392. }
  393. msr = op_points->driver_data;
  394. if (first_cpu) {
  395. rdmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, &oldmsr, &h);
  396. if (msr == (oldmsr & 0xffff)) {
  397. pr_debug("no change needed - msr was and needs "
  398. "to be %x\n", oldmsr);
  399. retval = 0;
  400. goto out;
  401. }
  402. first_cpu = 0;
  403. /* all but 16 LSB are reserved, treat them with care */
  404. oldmsr &= ~0xffff;
  405. msr &= 0xffff;
  406. oldmsr |= msr;
  407. }
  408. wrmsr_on_cpu(good_cpu, MSR_IA32_PERF_CTL, oldmsr, h);
  409. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  410. break;
  411. cpumask_set_cpu(j, covered_cpus);
  412. }
  413. if (unlikely(retval)) {
  414. /*
  415. * We have failed halfway through the frequency change.
  416. * We have sent callbacks to policy->cpus and
  417. * MSRs have already been written on coverd_cpus.
  418. * Best effort undo..
  419. */
  420. for_each_cpu(j, covered_cpus)
  421. wrmsr_on_cpu(j, MSR_IA32_PERF_CTL, oldmsr, h);
  422. }
  423. retval = 0;
  424. out:
  425. free_cpumask_var(covered_cpus);
  426. return retval;
  427. }
  428. static struct cpufreq_driver centrino_driver = {
  429. .name = "centrino", /* should be speedstep-centrino,
  430. but there's a 16 char limit */
  431. .init = centrino_cpu_init,
  432. .exit = centrino_cpu_exit,
  433. .verify = cpufreq_generic_frequency_table_verify,
  434. .target_index = centrino_target,
  435. .get = get_cur_freq,
  436. .attr = cpufreq_generic_attr,
  437. };
  438. /*
  439. * This doesn't replace the detailed checks above because
  440. * the generic CPU IDs don't have a way to match for steppings
  441. * or ASCII model IDs.
  442. */
  443. static const struct x86_cpu_id centrino_ids[] = {
  444. { X86_VENDOR_INTEL, 6, 9, X86_FEATURE_EST },
  445. { X86_VENDOR_INTEL, 6, 13, X86_FEATURE_EST },
  446. { X86_VENDOR_INTEL, 6, 13, X86_FEATURE_EST },
  447. { X86_VENDOR_INTEL, 6, 13, X86_FEATURE_EST },
  448. { X86_VENDOR_INTEL, 15, 3, X86_FEATURE_EST },
  449. { X86_VENDOR_INTEL, 15, 4, X86_FEATURE_EST },
  450. {}
  451. };
  452. #if 0
  453. /* Autoload or not? Do not for now. */
  454. MODULE_DEVICE_TABLE(x86cpu, centrino_ids);
  455. #endif
  456. /**
  457. * centrino_init - initializes the Enhanced SpeedStep CPUFreq driver
  458. *
  459. * Initializes the Enhanced SpeedStep support. Returns -ENODEV on
  460. * unsupported devices, -ENOENT if there's no voltage table for this
  461. * particular CPU model, -EINVAL on problems during initiatization,
  462. * and zero on success.
  463. *
  464. * This is quite picky. Not only does the CPU have to advertise the
  465. * "est" flag in the cpuid capability flags, we look for a specific
  466. * CPU model and stepping, and we need to have the exact model name in
  467. * our voltage tables. That is, be paranoid about not releasing
  468. * someone's valuable magic smoke.
  469. */
  470. static int __init centrino_init(void)
  471. {
  472. if (!x86_match_cpu(centrino_ids))
  473. return -ENODEV;
  474. return cpufreq_register_driver(&centrino_driver);
  475. }
  476. static void __exit centrino_exit(void)
  477. {
  478. cpufreq_unregister_driver(&centrino_driver);
  479. }
  480. MODULE_AUTHOR ("Jeremy Fitzhardinge <jeremy@goop.org>");
  481. MODULE_DESCRIPTION ("Enhanced SpeedStep driver for Intel Pentium M processors.");
  482. MODULE_LICENSE ("GPL");
  483. late_initcall(centrino_init);
  484. module_exit(centrino_exit);