speedstep-centrino.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  23. #include <linux/acpi.h>
  24. #include <linux/dmi.h>
  25. #include <acpi/processor.h>
  26. #endif
  27. #include <asm/msr.h>
  28. #include <asm/processor.h>
  29. #include <asm/cpufeature.h>
  30. #define PFX "speedstep-centrino: "
  31. #define MAINTAINER "cpufreq@lists.linux.org.uk"
  32. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-centrino", msg)
  33. struct cpu_id
  34. {
  35. __u8 x86; /* CPU family */
  36. __u8 x86_model; /* model */
  37. __u8 x86_mask; /* stepping */
  38. };
  39. enum {
  40. CPU_BANIAS,
  41. CPU_DOTHAN_A1,
  42. CPU_DOTHAN_A2,
  43. CPU_DOTHAN_B0,
  44. CPU_MP4HT_D0,
  45. CPU_MP4HT_E0,
  46. };
  47. static const struct cpu_id cpu_ids[] = {
  48. [CPU_BANIAS] = { 6, 9, 5 },
  49. [CPU_DOTHAN_A1] = { 6, 13, 1 },
  50. [CPU_DOTHAN_A2] = { 6, 13, 2 },
  51. [CPU_DOTHAN_B0] = { 6, 13, 6 },
  52. [CPU_MP4HT_D0] = {15, 3, 4 },
  53. [CPU_MP4HT_E0] = {15, 4, 1 },
  54. };
  55. #define N_IDS ARRAY_SIZE(cpu_ids)
  56. struct cpu_model
  57. {
  58. const struct cpu_id *cpu_id;
  59. const char *model_name;
  60. unsigned max_freq; /* max clock in kHz */
  61. struct cpufreq_frequency_table *op_points; /* clock/voltage pairs */
  62. };
  63. static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c, const struct cpu_id *x);
  64. /* Operating points for current CPU */
  65. static struct cpu_model *centrino_model[NR_CPUS];
  66. static const struct cpu_id *centrino_cpu[NR_CPUS];
  67. static struct cpufreq_driver centrino_driver;
  68. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE
  69. /* Computes the correct form for IA32_PERF_CTL MSR for a particular
  70. frequency/voltage operating point; frequency in MHz, volts in mV.
  71. This is stored as "index" in the structure. */
  72. #define OP(mhz, mv) \
  73. { \
  74. .frequency = (mhz) * 1000, \
  75. .index = (((mhz)/100) << 8) | ((mv - 700) / 16) \
  76. }
  77. /*
  78. * These voltage tables were derived from the Intel Pentium M
  79. * datasheet, document 25261202.pdf, Table 5. I have verified they
  80. * are consistent with my IBM ThinkPad X31, which has a 1.3GHz Pentium
  81. * M.
  82. */
  83. /* Ultra Low Voltage Intel Pentium M processor 900MHz (Banias) */
  84. static struct cpufreq_frequency_table banias_900[] =
  85. {
  86. OP(600, 844),
  87. OP(800, 988),
  88. OP(900, 1004),
  89. { .frequency = CPUFREQ_TABLE_END }
  90. };
  91. /* Ultra Low Voltage Intel Pentium M processor 1000MHz (Banias) */
  92. static struct cpufreq_frequency_table banias_1000[] =
  93. {
  94. OP(600, 844),
  95. OP(800, 972),
  96. OP(900, 988),
  97. OP(1000, 1004),
  98. { .frequency = CPUFREQ_TABLE_END }
  99. };
  100. /* Low Voltage Intel Pentium M processor 1.10GHz (Banias) */
  101. static struct cpufreq_frequency_table banias_1100[] =
  102. {
  103. OP( 600, 956),
  104. OP( 800, 1020),
  105. OP( 900, 1100),
  106. OP(1000, 1164),
  107. OP(1100, 1180),
  108. { .frequency = CPUFREQ_TABLE_END }
  109. };
  110. /* Low Voltage Intel Pentium M processor 1.20GHz (Banias) */
  111. static struct cpufreq_frequency_table banias_1200[] =
  112. {
  113. OP( 600, 956),
  114. OP( 800, 1004),
  115. OP( 900, 1020),
  116. OP(1000, 1100),
  117. OP(1100, 1164),
  118. OP(1200, 1180),
  119. { .frequency = CPUFREQ_TABLE_END }
  120. };
  121. /* Intel Pentium M processor 1.30GHz (Banias) */
  122. static struct cpufreq_frequency_table banias_1300[] =
  123. {
  124. OP( 600, 956),
  125. OP( 800, 1260),
  126. OP(1000, 1292),
  127. OP(1200, 1356),
  128. OP(1300, 1388),
  129. { .frequency = CPUFREQ_TABLE_END }
  130. };
  131. /* Intel Pentium M processor 1.40GHz (Banias) */
  132. static struct cpufreq_frequency_table banias_1400[] =
  133. {
  134. OP( 600, 956),
  135. OP( 800, 1180),
  136. OP(1000, 1308),
  137. OP(1200, 1436),
  138. OP(1400, 1484),
  139. { .frequency = CPUFREQ_TABLE_END }
  140. };
  141. /* Intel Pentium M processor 1.50GHz (Banias) */
  142. static struct cpufreq_frequency_table banias_1500[] =
  143. {
  144. OP( 600, 956),
  145. OP( 800, 1116),
  146. OP(1000, 1228),
  147. OP(1200, 1356),
  148. OP(1400, 1452),
  149. OP(1500, 1484),
  150. { .frequency = CPUFREQ_TABLE_END }
  151. };
  152. /* Intel Pentium M processor 1.60GHz (Banias) */
  153. static struct cpufreq_frequency_table banias_1600[] =
  154. {
  155. OP( 600, 956),
  156. OP( 800, 1036),
  157. OP(1000, 1164),
  158. OP(1200, 1276),
  159. OP(1400, 1420),
  160. OP(1600, 1484),
  161. { .frequency = CPUFREQ_TABLE_END }
  162. };
  163. /* Intel Pentium M processor 1.70GHz (Banias) */
  164. static struct cpufreq_frequency_table banias_1700[] =
  165. {
  166. OP( 600, 956),
  167. OP( 800, 1004),
  168. OP(1000, 1116),
  169. OP(1200, 1228),
  170. OP(1400, 1308),
  171. OP(1700, 1484),
  172. { .frequency = CPUFREQ_TABLE_END }
  173. };
  174. #undef OP
  175. #define _BANIAS(cpuid, max, name) \
  176. { .cpu_id = cpuid, \
  177. .model_name = "Intel(R) Pentium(R) M processor " name "MHz", \
  178. .max_freq = (max)*1000, \
  179. .op_points = banias_##max, \
  180. }
  181. #define BANIAS(max) _BANIAS(&cpu_ids[CPU_BANIAS], max, #max)
  182. /* CPU models, their operating frequency range, and freq/voltage
  183. operating points */
  184. static struct cpu_model models[] =
  185. {
  186. _BANIAS(&cpu_ids[CPU_BANIAS], 900, " 900"),
  187. BANIAS(1000),
  188. BANIAS(1100),
  189. BANIAS(1200),
  190. BANIAS(1300),
  191. BANIAS(1400),
  192. BANIAS(1500),
  193. BANIAS(1600),
  194. BANIAS(1700),
  195. /* NULL model_name is a wildcard */
  196. { &cpu_ids[CPU_DOTHAN_A1], NULL, 0, NULL },
  197. { &cpu_ids[CPU_DOTHAN_A2], NULL, 0, NULL },
  198. { &cpu_ids[CPU_DOTHAN_B0], NULL, 0, NULL },
  199. { &cpu_ids[CPU_MP4HT_D0], NULL, 0, NULL },
  200. { &cpu_ids[CPU_MP4HT_E0], NULL, 0, NULL },
  201. { NULL, }
  202. };
  203. #undef _BANIAS
  204. #undef BANIAS
  205. static int centrino_cpu_init_table(struct cpufreq_policy *policy)
  206. {
  207. struct cpuinfo_x86 *cpu = &cpu_data[policy->cpu];
  208. struct cpu_model *model;
  209. for(model = models; model->cpu_id != NULL; model++)
  210. if (centrino_verify_cpu_id(cpu, model->cpu_id) &&
  211. (model->model_name == NULL ||
  212. strcmp(cpu->x86_model_id, model->model_name) == 0))
  213. break;
  214. if (model->cpu_id == NULL) {
  215. /* No match at all */
  216. dprintk("no support for CPU model \"%s\": "
  217. "send /proc/cpuinfo to " MAINTAINER "\n",
  218. cpu->x86_model_id);
  219. return -ENOENT;
  220. }
  221. if (model->op_points == NULL) {
  222. /* Matched a non-match */
  223. dprintk("no table support for CPU model \"%s\"\n",
  224. cpu->x86_model_id);
  225. #ifndef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  226. dprintk("try compiling with CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI enabled\n");
  227. #endif
  228. return -ENOENT;
  229. }
  230. centrino_model[policy->cpu] = model;
  231. dprintk("found \"%s\": max frequency: %dkHz\n",
  232. model->model_name, model->max_freq);
  233. return 0;
  234. }
  235. #else
  236. static inline int centrino_cpu_init_table(struct cpufreq_policy *policy) { return -ENODEV; }
  237. #endif /* CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE */
  238. static int centrino_verify_cpu_id(const struct cpuinfo_x86 *c, 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 ((centrino_cpu[cpu] == &cpu_ids[CPU_BANIAS]) ||
  256. (centrino_cpu[cpu] == &cpu_ids[CPU_DOTHAN_A1]) ||
  257. (centrino_cpu[cpu] == &cpu_ids[CPU_DOTHAN_B0])) {
  258. msr = (msr >> 8) & 0xff;
  259. return msr * 100000;
  260. }
  261. if ((!centrino_model[cpu]) || (!centrino_model[cpu]->op_points))
  262. return 0;
  263. msr &= 0xffff;
  264. for (i=0;centrino_model[cpu]->op_points[i].frequency != CPUFREQ_TABLE_END; i++) {
  265. if (msr == centrino_model[cpu]->op_points[i].index)
  266. return centrino_model[cpu]->op_points[i].frequency;
  267. }
  268. if (failsafe)
  269. return centrino_model[cpu]->op_points[i-1].frequency;
  270. else
  271. return 0;
  272. }
  273. /* Return the current CPU frequency in kHz */
  274. static unsigned int get_cur_freq(unsigned int cpu)
  275. {
  276. unsigned l, h;
  277. unsigned clock_freq;
  278. cpumask_t saved_mask;
  279. saved_mask = current->cpus_allowed;
  280. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  281. if (smp_processor_id() != cpu)
  282. return 0;
  283. rdmsr(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(MSR_IA32_PERF_CTL, l, h);
  293. clock_freq = extract_clock(l, cpu, 1);
  294. }
  295. set_cpus_allowed(current, saved_mask);
  296. return clock_freq;
  297. }
  298. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  299. static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
  300. /*
  301. * centrino_cpu_early_init_acpi - Do the preregistering with ACPI P-States
  302. * library
  303. *
  304. * Before doing the actual init, we need to do _PSD related setup whenever
  305. * supported by the BIOS. These are handled by this early_init routine.
  306. */
  307. static int centrino_cpu_early_init_acpi(void)
  308. {
  309. unsigned int i, j;
  310. struct acpi_processor_performance *data;
  311. for_each_possible_cpu(i) {
  312. data = kzalloc(sizeof(struct acpi_processor_performance),
  313. GFP_KERNEL);
  314. if (!data) {
  315. for_each_possible_cpu(j) {
  316. kfree(acpi_perf_data[j]);
  317. acpi_perf_data[j] = NULL;
  318. }
  319. return (-ENOMEM);
  320. }
  321. acpi_perf_data[i] = data;
  322. }
  323. acpi_processor_preregister_performance(acpi_perf_data);
  324. return 0;
  325. }
  326. #ifdef CONFIG_SMP
  327. /*
  328. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  329. * or do it in BIOS firmware and won't inform about it to OS. If not
  330. * detected, this has a side effect of making CPU run at a different speed
  331. * than OS intended it to run at. Detect it and handle it cleanly.
  332. */
  333. static int bios_with_sw_any_bug;
  334. static int sw_any_bug_found(struct dmi_system_id *d)
  335. {
  336. bios_with_sw_any_bug = 1;
  337. return 0;
  338. }
  339. static struct dmi_system_id sw_any_bug_dmi_table[] = {
  340. {
  341. .callback = sw_any_bug_found,
  342. .ident = "Supermicro Server X6DLP",
  343. .matches = {
  344. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  345. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  346. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  347. },
  348. },
  349. { }
  350. };
  351. #endif
  352. /*
  353. * centrino_cpu_init_acpi - register with ACPI P-States library
  354. *
  355. * Register with the ACPI P-States library (part of drivers/acpi/processor.c)
  356. * in order to determine correct frequency and voltage pairings by reading
  357. * the _PSS of the ACPI DSDT or SSDT tables.
  358. */
  359. static int centrino_cpu_init_acpi(struct cpufreq_policy *policy)
  360. {
  361. unsigned long cur_freq;
  362. int result = 0, i;
  363. unsigned int cpu = policy->cpu;
  364. struct acpi_processor_performance *p;
  365. p = acpi_perf_data[cpu];
  366. /* register with ACPI core */
  367. if (acpi_processor_register_performance(p, cpu)) {
  368. dprintk(PFX "obtaining ACPI data failed\n");
  369. return -EIO;
  370. }
  371. policy->shared_type = p->shared_type;
  372. /*
  373. * Will let policy->cpus know about dependency only when software
  374. * coordination is required.
  375. */
  376. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  377. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  378. policy->cpus = p->shared_cpu_map;
  379. }
  380. #ifdef CONFIG_SMP
  381. dmi_check_system(sw_any_bug_dmi_table);
  382. if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
  383. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  384. policy->cpus = cpu_core_map[cpu];
  385. }
  386. #endif
  387. /* verify the acpi_data */
  388. if (p->state_count <= 1) {
  389. dprintk("No P-States\n");
  390. result = -ENODEV;
  391. goto err_unreg;
  392. }
  393. if ((p->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  394. (p->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  395. dprintk("Invalid control/status registers (%x - %x)\n",
  396. p->control_register.space_id, p->status_register.space_id);
  397. result = -EIO;
  398. goto err_unreg;
  399. }
  400. for (i=0; i<p->state_count; i++) {
  401. if (p->states[i].control != p->states[i].status) {
  402. dprintk("Different control (%llu) and status values (%llu)\n",
  403. p->states[i].control, p->states[i].status);
  404. result = -EINVAL;
  405. goto err_unreg;
  406. }
  407. if (!p->states[i].core_frequency) {
  408. dprintk("Zero core frequency for state %u\n", i);
  409. result = -EINVAL;
  410. goto err_unreg;
  411. }
  412. if (p->states[i].core_frequency > p->states[0].core_frequency) {
  413. dprintk("P%u has larger frequency (%llu) than P0 (%llu), skipping\n", i,
  414. p->states[i].core_frequency, p->states[0].core_frequency);
  415. p->states[i].core_frequency = 0;
  416. continue;
  417. }
  418. }
  419. centrino_model[cpu] = kzalloc(sizeof(struct cpu_model), GFP_KERNEL);
  420. if (!centrino_model[cpu]) {
  421. result = -ENOMEM;
  422. goto err_unreg;
  423. }
  424. centrino_model[cpu]->model_name=NULL;
  425. centrino_model[cpu]->max_freq = p->states[0].core_frequency * 1000;
  426. centrino_model[cpu]->op_points = kmalloc(sizeof(struct cpufreq_frequency_table) *
  427. (p->state_count + 1), GFP_KERNEL);
  428. if (!centrino_model[cpu]->op_points) {
  429. result = -ENOMEM;
  430. goto err_kfree;
  431. }
  432. for (i=0; i<p->state_count; i++) {
  433. centrino_model[cpu]->op_points[i].index = p->states[i].control;
  434. centrino_model[cpu]->op_points[i].frequency = p->states[i].core_frequency * 1000;
  435. dprintk("adding state %i with frequency %u and control value %04x\n",
  436. i, centrino_model[cpu]->op_points[i].frequency, centrino_model[cpu]->op_points[i].index);
  437. }
  438. centrino_model[cpu]->op_points[p->state_count].frequency = CPUFREQ_TABLE_END;
  439. cur_freq = get_cur_freq(cpu);
  440. for (i=0; i<p->state_count; i++) {
  441. if (!p->states[i].core_frequency) {
  442. dprintk("skipping state %u\n", i);
  443. centrino_model[cpu]->op_points[i].frequency = CPUFREQ_ENTRY_INVALID;
  444. continue;
  445. }
  446. if (extract_clock(centrino_model[cpu]->op_points[i].index, cpu, 0) !=
  447. (centrino_model[cpu]->op_points[i].frequency)) {
  448. dprintk("Invalid encoded frequency (%u vs. %u)\n",
  449. extract_clock(centrino_model[cpu]->op_points[i].index, cpu, 0),
  450. centrino_model[cpu]->op_points[i].frequency);
  451. result = -EINVAL;
  452. goto err_kfree_all;
  453. }
  454. if (cur_freq == centrino_model[cpu]->op_points[i].frequency)
  455. p->state = i;
  456. }
  457. /* notify BIOS that we exist */
  458. acpi_processor_notify_smm(THIS_MODULE);
  459. printk("speedstep-centrino with X86_SPEEDSTEP_CENTRINO_ACPI"
  460. "config is deprecated.\n "
  461. "Use X86_ACPI_CPUFREQ (acpi-cpufreq instead.\n" );
  462. return 0;
  463. err_kfree_all:
  464. kfree(centrino_model[cpu]->op_points);
  465. err_kfree:
  466. kfree(centrino_model[cpu]);
  467. err_unreg:
  468. acpi_processor_unregister_performance(p, cpu);
  469. dprintk(PFX "invalid ACPI data\n");
  470. return (result);
  471. }
  472. #else
  473. static inline int centrino_cpu_init_acpi(struct cpufreq_policy *policy) { return -ENODEV; }
  474. static inline int centrino_cpu_early_init_acpi(void) { return 0; }
  475. #endif
  476. static int centrino_cpu_init(struct cpufreq_policy *policy)
  477. {
  478. struct cpuinfo_x86 *cpu = &cpu_data[policy->cpu];
  479. unsigned freq;
  480. unsigned l, h;
  481. int ret;
  482. int i;
  483. /* Only Intel makes Enhanced Speedstep-capable CPUs */
  484. if (cpu->x86_vendor != X86_VENDOR_INTEL || !cpu_has(cpu, X86_FEATURE_EST))
  485. return -ENODEV;
  486. if (cpu_has(cpu, X86_FEATURE_CONSTANT_TSC))
  487. centrino_driver.flags |= CPUFREQ_CONST_LOOPS;
  488. if (centrino_cpu_init_acpi(policy)) {
  489. if (policy->cpu != 0)
  490. return -ENODEV;
  491. for (i = 0; i < N_IDS; i++)
  492. if (centrino_verify_cpu_id(cpu, &cpu_ids[i]))
  493. break;
  494. if (i != N_IDS)
  495. centrino_cpu[policy->cpu] = &cpu_ids[i];
  496. if (!centrino_cpu[policy->cpu]) {
  497. dprintk("found unsupported CPU with "
  498. "Enhanced SpeedStep: send /proc/cpuinfo to "
  499. MAINTAINER "\n");
  500. return -ENODEV;
  501. }
  502. if (centrino_cpu_init_table(policy)) {
  503. return -ENODEV;
  504. }
  505. }
  506. /* Check to see if Enhanced SpeedStep is enabled, and try to
  507. enable it if not. */
  508. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  509. if (!(l & (1<<16))) {
  510. l |= (1<<16);
  511. dprintk("trying to enable Enhanced SpeedStep (%x)\n", l);
  512. wrmsr(MSR_IA32_MISC_ENABLE, l, h);
  513. /* check to see if it stuck */
  514. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  515. if (!(l & (1<<16))) {
  516. printk(KERN_INFO PFX "couldn't enable Enhanced SpeedStep\n");
  517. return -ENODEV;
  518. }
  519. }
  520. freq = get_cur_freq(policy->cpu);
  521. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  522. policy->cpuinfo.transition_latency = 10000; /* 10uS transition latency */
  523. policy->cur = freq;
  524. dprintk("centrino_cpu_init: cur=%dkHz\n", policy->cur);
  525. ret = cpufreq_frequency_table_cpuinfo(policy, centrino_model[policy->cpu]->op_points);
  526. if (ret)
  527. return (ret);
  528. cpufreq_frequency_table_get_attr(centrino_model[policy->cpu]->op_points, policy->cpu);
  529. return 0;
  530. }
  531. static int centrino_cpu_exit(struct cpufreq_policy *policy)
  532. {
  533. unsigned int cpu = policy->cpu;
  534. if (!centrino_model[cpu])
  535. return -ENODEV;
  536. cpufreq_frequency_table_put_attr(cpu);
  537. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  538. if (!centrino_model[cpu]->model_name) {
  539. static struct acpi_processor_performance *p;
  540. if (acpi_perf_data[cpu]) {
  541. p = acpi_perf_data[cpu];
  542. dprintk("unregistering and freeing ACPI data\n");
  543. acpi_processor_unregister_performance(p, cpu);
  544. kfree(centrino_model[cpu]->op_points);
  545. kfree(centrino_model[cpu]);
  546. }
  547. }
  548. #endif
  549. centrino_model[cpu] = NULL;
  550. return 0;
  551. }
  552. /**
  553. * centrino_verify - verifies a new CPUFreq policy
  554. * @policy: new policy
  555. *
  556. * Limit must be within this model's frequency range at least one
  557. * border included.
  558. */
  559. static int centrino_verify (struct cpufreq_policy *policy)
  560. {
  561. return cpufreq_frequency_table_verify(policy, centrino_model[policy->cpu]->op_points);
  562. }
  563. /**
  564. * centrino_setpolicy - set a new CPUFreq policy
  565. * @policy: new policy
  566. * @target_freq: the target frequency
  567. * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  568. *
  569. * Sets a new CPUFreq policy.
  570. */
  571. static int centrino_target (struct cpufreq_policy *policy,
  572. unsigned int target_freq,
  573. unsigned int relation)
  574. {
  575. unsigned int newstate = 0;
  576. unsigned int msr, oldmsr = 0, h = 0, cpu = policy->cpu;
  577. struct cpufreq_freqs freqs;
  578. cpumask_t online_policy_cpus;
  579. cpumask_t saved_mask;
  580. cpumask_t set_mask;
  581. cpumask_t covered_cpus;
  582. int retval = 0;
  583. unsigned int j, k, first_cpu, tmp;
  584. if (unlikely(centrino_model[cpu] == NULL))
  585. return -ENODEV;
  586. if (unlikely(cpufreq_frequency_table_target(policy,
  587. centrino_model[cpu]->op_points,
  588. target_freq,
  589. relation,
  590. &newstate))) {
  591. return -EINVAL;
  592. }
  593. #ifdef CONFIG_HOTPLUG_CPU
  594. /* cpufreq holds the hotplug lock, so we are safe from here on */
  595. cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
  596. #else
  597. online_policy_cpus = policy->cpus;
  598. #endif
  599. saved_mask = current->cpus_allowed;
  600. first_cpu = 1;
  601. cpus_clear(covered_cpus);
  602. for_each_cpu_mask(j, online_policy_cpus) {
  603. /*
  604. * Support for SMP systems.
  605. * Make sure we are running on CPU that wants to change freq
  606. */
  607. cpus_clear(set_mask);
  608. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  609. cpus_or(set_mask, set_mask, online_policy_cpus);
  610. else
  611. cpu_set(j, set_mask);
  612. set_cpus_allowed(current, set_mask);
  613. if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
  614. dprintk("couldn't limit to CPUs in this domain\n");
  615. retval = -EAGAIN;
  616. if (first_cpu) {
  617. /* We haven't started the transition yet. */
  618. goto migrate_end;
  619. }
  620. break;
  621. }
  622. msr = centrino_model[cpu]->op_points[newstate].index;
  623. if (first_cpu) {
  624. rdmsr(MSR_IA32_PERF_CTL, oldmsr, h);
  625. if (msr == (oldmsr & 0xffff)) {
  626. dprintk("no change needed - msr was and needs "
  627. "to be %x\n", oldmsr);
  628. retval = 0;
  629. goto migrate_end;
  630. }
  631. freqs.old = extract_clock(oldmsr, cpu, 0);
  632. freqs.new = extract_clock(msr, cpu, 0);
  633. dprintk("target=%dkHz old=%d new=%d msr=%04x\n",
  634. target_freq, freqs.old, freqs.new, msr);
  635. for_each_cpu_mask(k, online_policy_cpus) {
  636. freqs.cpu = k;
  637. cpufreq_notify_transition(&freqs,
  638. CPUFREQ_PRECHANGE);
  639. }
  640. first_cpu = 0;
  641. /* all but 16 LSB are reserved, treat them with care */
  642. oldmsr &= ~0xffff;
  643. msr &= 0xffff;
  644. oldmsr |= msr;
  645. }
  646. wrmsr(MSR_IA32_PERF_CTL, oldmsr, h);
  647. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  648. break;
  649. cpu_set(j, covered_cpus);
  650. }
  651. for_each_cpu_mask(k, online_policy_cpus) {
  652. freqs.cpu = k;
  653. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  654. }
  655. if (unlikely(retval)) {
  656. /*
  657. * We have failed halfway through the frequency change.
  658. * We have sent callbacks to policy->cpus and
  659. * MSRs have already been written on coverd_cpus.
  660. * Best effort undo..
  661. */
  662. if (!cpus_empty(covered_cpus)) {
  663. for_each_cpu_mask(j, covered_cpus) {
  664. set_cpus_allowed(current, cpumask_of_cpu(j));
  665. wrmsr(MSR_IA32_PERF_CTL, oldmsr, h);
  666. }
  667. }
  668. tmp = freqs.new;
  669. freqs.new = freqs.old;
  670. freqs.old = tmp;
  671. for_each_cpu_mask(j, online_policy_cpus) {
  672. freqs.cpu = j;
  673. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  674. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  675. }
  676. }
  677. migrate_end:
  678. set_cpus_allowed(current, saved_mask);
  679. return 0;
  680. }
  681. static struct freq_attr* centrino_attr[] = {
  682. &cpufreq_freq_attr_scaling_available_freqs,
  683. NULL,
  684. };
  685. static struct cpufreq_driver centrino_driver = {
  686. .name = "centrino", /* should be speedstep-centrino,
  687. but there's a 16 char limit */
  688. .init = centrino_cpu_init,
  689. .exit = centrino_cpu_exit,
  690. .verify = centrino_verify,
  691. .target = centrino_target,
  692. .get = get_cur_freq,
  693. .attr = centrino_attr,
  694. .owner = THIS_MODULE,
  695. };
  696. /**
  697. * centrino_init - initializes the Enhanced SpeedStep CPUFreq driver
  698. *
  699. * Initializes the Enhanced SpeedStep support. Returns -ENODEV on
  700. * unsupported devices, -ENOENT if there's no voltage table for this
  701. * particular CPU model, -EINVAL on problems during initiatization,
  702. * and zero on success.
  703. *
  704. * This is quite picky. Not only does the CPU have to advertise the
  705. * "est" flag in the cpuid capability flags, we look for a specific
  706. * CPU model and stepping, and we need to have the exact model name in
  707. * our voltage tables. That is, be paranoid about not releasing
  708. * someone's valuable magic smoke.
  709. */
  710. static int __init centrino_init(void)
  711. {
  712. struct cpuinfo_x86 *cpu = cpu_data;
  713. if (!cpu_has(cpu, X86_FEATURE_EST))
  714. return -ENODEV;
  715. centrino_cpu_early_init_acpi();
  716. return cpufreq_register_driver(&centrino_driver);
  717. }
  718. static void __exit centrino_exit(void)
  719. {
  720. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  721. unsigned int j;
  722. #endif
  723. cpufreq_unregister_driver(&centrino_driver);
  724. #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
  725. for_each_possible_cpu(j) {
  726. kfree(acpi_perf_data[j]);
  727. acpi_perf_data[j] = NULL;
  728. }
  729. #endif
  730. }
  731. MODULE_AUTHOR ("Jeremy Fitzhardinge <jeremy@goop.org>");
  732. MODULE_DESCRIPTION ("Enhanced SpeedStep driver for Intel Pentium M processors.");
  733. MODULE_LICENSE ("GPL");
  734. late_initcall(centrino_init);
  735. module_exit(centrino_exit);