speedstep-centrino.c 21 KB

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