acpi-cpufreq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * acpi-cpufreq.c - ACPI Processor P-States Driver
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  24. *
  25. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/smp.h>
  31. #include <linux/sched.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/compiler.h>
  34. #include <linux/dmi.h>
  35. #include <linux/slab.h>
  36. #include <linux/acpi.h>
  37. #include <linux/io.h>
  38. #include <linux/delay.h>
  39. #include <linux/uaccess.h>
  40. #include <acpi/processor.h>
  41. #include <asm/msr.h>
  42. #include <asm/processor.h>
  43. #include <asm/cpufeature.h>
  44. #include "mperf.h"
  45. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  46. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  47. MODULE_LICENSE("GPL");
  48. enum {
  49. UNDEFINED_CAPABLE = 0,
  50. SYSTEM_INTEL_MSR_CAPABLE,
  51. SYSTEM_IO_CAPABLE,
  52. };
  53. #define INTEL_MSR_RANGE (0xffff)
  54. struct acpi_cpufreq_data {
  55. struct acpi_processor_performance *acpi_data;
  56. struct cpufreq_frequency_table *freq_table;
  57. unsigned int resume;
  58. unsigned int cpu_feature;
  59. };
  60. static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
  61. /* acpi_perf_data is a pointer to percpu data. */
  62. static struct acpi_processor_performance __percpu *acpi_perf_data;
  63. static struct cpufreq_driver acpi_cpufreq_driver;
  64. static unsigned int acpi_pstate_strict;
  65. static int check_est_cpu(unsigned int cpuid)
  66. {
  67. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  68. return cpu_has(cpu, X86_FEATURE_EST);
  69. }
  70. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  71. {
  72. struct acpi_processor_performance *perf;
  73. int i;
  74. perf = data->acpi_data;
  75. for (i = 0; i < perf->state_count; i++) {
  76. if (value == perf->states[i].status)
  77. return data->freq_table[i].frequency;
  78. }
  79. return 0;
  80. }
  81. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  82. {
  83. int i;
  84. struct acpi_processor_performance *perf;
  85. msr &= INTEL_MSR_RANGE;
  86. perf = data->acpi_data;
  87. for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  88. if (msr == perf->states[data->freq_table[i].index].status)
  89. return data->freq_table[i].frequency;
  90. }
  91. return data->freq_table[0].frequency;
  92. }
  93. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  94. {
  95. switch (data->cpu_feature) {
  96. case SYSTEM_INTEL_MSR_CAPABLE:
  97. return extract_msr(val, data);
  98. case SYSTEM_IO_CAPABLE:
  99. return extract_io(val, data);
  100. default:
  101. return 0;
  102. }
  103. }
  104. struct msr_addr {
  105. u32 reg;
  106. };
  107. struct io_addr {
  108. u16 port;
  109. u8 bit_width;
  110. };
  111. struct drv_cmd {
  112. unsigned int type;
  113. const struct cpumask *mask;
  114. union {
  115. struct msr_addr msr;
  116. struct io_addr io;
  117. } addr;
  118. u32 val;
  119. };
  120. /* Called via smp_call_function_single(), on the target CPU */
  121. static void do_drv_read(void *_cmd)
  122. {
  123. struct drv_cmd *cmd = _cmd;
  124. u32 h;
  125. switch (cmd->type) {
  126. case SYSTEM_INTEL_MSR_CAPABLE:
  127. rdmsr(cmd->addr.msr.reg, cmd->val, h);
  128. break;
  129. case SYSTEM_IO_CAPABLE:
  130. acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
  131. &cmd->val,
  132. (u32)cmd->addr.io.bit_width);
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. /* Called via smp_call_function_many(), on the target CPUs */
  139. static void do_drv_write(void *_cmd)
  140. {
  141. struct drv_cmd *cmd = _cmd;
  142. u32 lo, hi;
  143. switch (cmd->type) {
  144. case SYSTEM_INTEL_MSR_CAPABLE:
  145. rdmsr(cmd->addr.msr.reg, lo, hi);
  146. lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
  147. wrmsr(cmd->addr.msr.reg, lo, hi);
  148. break;
  149. case SYSTEM_IO_CAPABLE:
  150. acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
  151. cmd->val,
  152. (u32)cmd->addr.io.bit_width);
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. static void drv_read(struct drv_cmd *cmd)
  159. {
  160. int err;
  161. cmd->val = 0;
  162. err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
  163. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  164. }
  165. static void drv_write(struct drv_cmd *cmd)
  166. {
  167. int this_cpu;
  168. this_cpu = get_cpu();
  169. if (cpumask_test_cpu(this_cpu, cmd->mask))
  170. do_drv_write(cmd);
  171. smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
  172. put_cpu();
  173. }
  174. static u32 get_cur_val(const struct cpumask *mask)
  175. {
  176. struct acpi_processor_performance *perf;
  177. struct drv_cmd cmd;
  178. if (unlikely(cpumask_empty(mask)))
  179. return 0;
  180. switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
  181. case SYSTEM_INTEL_MSR_CAPABLE:
  182. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  183. cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
  184. break;
  185. case SYSTEM_IO_CAPABLE:
  186. cmd.type = SYSTEM_IO_CAPABLE;
  187. perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
  188. cmd.addr.io.port = perf->control_register.address;
  189. cmd.addr.io.bit_width = perf->control_register.bit_width;
  190. break;
  191. default:
  192. return 0;
  193. }
  194. cmd.mask = mask;
  195. drv_read(&cmd);
  196. pr_debug("get_cur_val = %u\n", cmd.val);
  197. return cmd.val;
  198. }
  199. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  200. {
  201. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
  202. unsigned int freq;
  203. unsigned int cached_freq;
  204. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  205. if (unlikely(data == NULL ||
  206. data->acpi_data == NULL || data->freq_table == NULL)) {
  207. return 0;
  208. }
  209. cached_freq = data->freq_table[data->acpi_data->state].frequency;
  210. freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
  211. if (freq != cached_freq) {
  212. /*
  213. * The dreaded BIOS frequency change behind our back.
  214. * Force set the frequency on next target call.
  215. */
  216. data->resume = 1;
  217. }
  218. pr_debug("cur freq = %u\n", freq);
  219. return freq;
  220. }
  221. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  222. struct acpi_cpufreq_data *data)
  223. {
  224. unsigned int cur_freq;
  225. unsigned int i;
  226. for (i = 0; i < 100; i++) {
  227. cur_freq = extract_freq(get_cur_val(mask), data);
  228. if (cur_freq == freq)
  229. return 1;
  230. udelay(10);
  231. }
  232. return 0;
  233. }
  234. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  235. unsigned int target_freq, unsigned int relation)
  236. {
  237. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  238. struct acpi_processor_performance *perf;
  239. struct cpufreq_freqs freqs;
  240. struct drv_cmd cmd;
  241. unsigned int next_state = 0; /* Index into freq_table */
  242. unsigned int next_perf_state = 0; /* Index into perf table */
  243. unsigned int i;
  244. int result = 0;
  245. pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
  246. if (unlikely(data == NULL ||
  247. data->acpi_data == NULL || data->freq_table == NULL)) {
  248. return -ENODEV;
  249. }
  250. perf = data->acpi_data;
  251. result = cpufreq_frequency_table_target(policy,
  252. data->freq_table,
  253. target_freq,
  254. relation, &next_state);
  255. if (unlikely(result)) {
  256. result = -ENODEV;
  257. goto out;
  258. }
  259. next_perf_state = data->freq_table[next_state].index;
  260. if (perf->state == next_perf_state) {
  261. if (unlikely(data->resume)) {
  262. pr_debug("Called after resume, resetting to P%d\n",
  263. next_perf_state);
  264. data->resume = 0;
  265. } else {
  266. pr_debug("Already at target state (P%d)\n",
  267. next_perf_state);
  268. goto out;
  269. }
  270. }
  271. switch (data->cpu_feature) {
  272. case SYSTEM_INTEL_MSR_CAPABLE:
  273. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  274. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  275. cmd.val = (u32) perf->states[next_perf_state].control;
  276. break;
  277. case SYSTEM_IO_CAPABLE:
  278. cmd.type = SYSTEM_IO_CAPABLE;
  279. cmd.addr.io.port = perf->control_register.address;
  280. cmd.addr.io.bit_width = perf->control_register.bit_width;
  281. cmd.val = (u32) perf->states[next_perf_state].control;
  282. break;
  283. default:
  284. result = -ENODEV;
  285. goto out;
  286. }
  287. /* cpufreq holds the hotplug lock, so we are safe from here on */
  288. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  289. cmd.mask = policy->cpus;
  290. else
  291. cmd.mask = cpumask_of(policy->cpu);
  292. freqs.old = perf->states[perf->state].core_frequency * 1000;
  293. freqs.new = data->freq_table[next_state].frequency;
  294. for_each_cpu(i, policy->cpus) {
  295. freqs.cpu = i;
  296. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  297. }
  298. drv_write(&cmd);
  299. if (acpi_pstate_strict) {
  300. if (!check_freqs(cmd.mask, freqs.new, data)) {
  301. pr_debug("acpi_cpufreq_target failed (%d)\n",
  302. policy->cpu);
  303. result = -EAGAIN;
  304. goto out;
  305. }
  306. }
  307. for_each_cpu(i, policy->cpus) {
  308. freqs.cpu = i;
  309. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  310. }
  311. perf->state = next_perf_state;
  312. out:
  313. return result;
  314. }
  315. static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
  316. {
  317. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  318. pr_debug("acpi_cpufreq_verify\n");
  319. return cpufreq_frequency_table_verify(policy, data->freq_table);
  320. }
  321. static unsigned long
  322. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  323. {
  324. struct acpi_processor_performance *perf = data->acpi_data;
  325. if (cpu_khz) {
  326. /* search the closest match to cpu_khz */
  327. unsigned int i;
  328. unsigned long freq;
  329. unsigned long freqn = perf->states[0].core_frequency * 1000;
  330. for (i = 0; i < (perf->state_count-1); i++) {
  331. freq = freqn;
  332. freqn = perf->states[i+1].core_frequency * 1000;
  333. if ((2 * cpu_khz) > (freqn + freq)) {
  334. perf->state = i;
  335. return freq;
  336. }
  337. }
  338. perf->state = perf->state_count-1;
  339. return freqn;
  340. } else {
  341. /* assume CPU is at P0... */
  342. perf->state = 0;
  343. return perf->states[0].core_frequency * 1000;
  344. }
  345. }
  346. static void free_acpi_perf_data(void)
  347. {
  348. unsigned int i;
  349. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  350. for_each_possible_cpu(i)
  351. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  352. ->shared_cpu_map);
  353. free_percpu(acpi_perf_data);
  354. }
  355. /*
  356. * acpi_cpufreq_early_init - initialize ACPI P-States library
  357. *
  358. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  359. * in order to determine correct frequency and voltage pairings. We can
  360. * do _PDC and _PSD and find out the processor dependency for the
  361. * actual init that will happen later...
  362. */
  363. static int __init acpi_cpufreq_early_init(void)
  364. {
  365. unsigned int i;
  366. pr_debug("acpi_cpufreq_early_init\n");
  367. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  368. if (!acpi_perf_data) {
  369. pr_debug("Memory allocation error for acpi_perf_data.\n");
  370. return -ENOMEM;
  371. }
  372. for_each_possible_cpu(i) {
  373. if (!zalloc_cpumask_var_node(
  374. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  375. GFP_KERNEL, cpu_to_node(i))) {
  376. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  377. free_acpi_perf_data();
  378. return -ENOMEM;
  379. }
  380. }
  381. /* Do initialization in ACPI core */
  382. acpi_processor_preregister_performance(acpi_perf_data);
  383. return 0;
  384. }
  385. #ifdef CONFIG_SMP
  386. /*
  387. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  388. * or do it in BIOS firmware and won't inform about it to OS. If not
  389. * detected, this has a side effect of making CPU run at a different speed
  390. * than OS intended it to run at. Detect it and handle it cleanly.
  391. */
  392. static int bios_with_sw_any_bug;
  393. static int sw_any_bug_found(const struct dmi_system_id *d)
  394. {
  395. bios_with_sw_any_bug = 1;
  396. return 0;
  397. }
  398. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  399. {
  400. .callback = sw_any_bug_found,
  401. .ident = "Supermicro Server X6DLP",
  402. .matches = {
  403. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  404. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  405. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  406. },
  407. },
  408. { }
  409. };
  410. static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
  411. {
  412. /* Intel Xeon Processor 7100 Series Specification Update
  413. * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
  414. * AL30: A Machine Check Exception (MCE) Occurring during an
  415. * Enhanced Intel SpeedStep Technology Ratio Change May Cause
  416. * Both Processor Cores to Lock Up. */
  417. if (c->x86_vendor == X86_VENDOR_INTEL) {
  418. if ((c->x86 == 15) &&
  419. (c->x86_model == 6) &&
  420. (c->x86_mask == 8)) {
  421. printk(KERN_INFO "acpi-cpufreq: Intel(R) "
  422. "Xeon(R) 7100 Errata AL30, processors may "
  423. "lock up on frequency changes: disabling "
  424. "acpi-cpufreq.\n");
  425. return -ENODEV;
  426. }
  427. }
  428. return 0;
  429. }
  430. #endif
  431. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  432. {
  433. unsigned int i;
  434. unsigned int valid_states = 0;
  435. unsigned int cpu = policy->cpu;
  436. struct acpi_cpufreq_data *data;
  437. unsigned int result = 0;
  438. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  439. struct acpi_processor_performance *perf;
  440. #ifdef CONFIG_SMP
  441. static int blacklisted;
  442. #endif
  443. pr_debug("acpi_cpufreq_cpu_init\n");
  444. #ifdef CONFIG_SMP
  445. if (blacklisted)
  446. return blacklisted;
  447. blacklisted = acpi_cpufreq_blacklist(c);
  448. if (blacklisted)
  449. return blacklisted;
  450. #endif
  451. data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
  452. if (!data)
  453. return -ENOMEM;
  454. data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
  455. per_cpu(acfreq_data, cpu) = data;
  456. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  457. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  458. result = acpi_processor_register_performance(data->acpi_data, cpu);
  459. if (result)
  460. goto err_free;
  461. perf = data->acpi_data;
  462. policy->shared_type = perf->shared_type;
  463. /*
  464. * Will let policy->cpus know about dependency only when software
  465. * coordination is required.
  466. */
  467. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  468. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  469. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  470. }
  471. cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
  472. #ifdef CONFIG_SMP
  473. dmi_check_system(sw_any_bug_dmi_table);
  474. if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
  475. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  476. cpumask_copy(policy->cpus, cpu_core_mask(cpu));
  477. }
  478. #endif
  479. /* capability check */
  480. if (perf->state_count <= 1) {
  481. pr_debug("No P-States\n");
  482. result = -ENODEV;
  483. goto err_unreg;
  484. }
  485. if (perf->control_register.space_id != perf->status_register.space_id) {
  486. result = -ENODEV;
  487. goto err_unreg;
  488. }
  489. switch (perf->control_register.space_id) {
  490. case ACPI_ADR_SPACE_SYSTEM_IO:
  491. pr_debug("SYSTEM IO addr space\n");
  492. data->cpu_feature = SYSTEM_IO_CAPABLE;
  493. break;
  494. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  495. pr_debug("HARDWARE addr space\n");
  496. if (!check_est_cpu(cpu)) {
  497. result = -ENODEV;
  498. goto err_unreg;
  499. }
  500. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  501. break;
  502. default:
  503. pr_debug("Unknown addr space %d\n",
  504. (u32) (perf->control_register.space_id));
  505. result = -ENODEV;
  506. goto err_unreg;
  507. }
  508. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  509. (perf->state_count+1), GFP_KERNEL);
  510. if (!data->freq_table) {
  511. result = -ENOMEM;
  512. goto err_unreg;
  513. }
  514. /* detect transition latency */
  515. policy->cpuinfo.transition_latency = 0;
  516. for (i = 0; i < perf->state_count; i++) {
  517. if ((perf->states[i].transition_latency * 1000) >
  518. policy->cpuinfo.transition_latency)
  519. policy->cpuinfo.transition_latency =
  520. perf->states[i].transition_latency * 1000;
  521. }
  522. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  523. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  524. policy->cpuinfo.transition_latency > 20 * 1000) {
  525. policy->cpuinfo.transition_latency = 20 * 1000;
  526. printk_once(KERN_INFO
  527. "P-state transition latency capped at 20 uS\n");
  528. }
  529. /* table init */
  530. for (i = 0; i < perf->state_count; i++) {
  531. if (i > 0 && perf->states[i].core_frequency >=
  532. data->freq_table[valid_states-1].frequency / 1000)
  533. continue;
  534. data->freq_table[valid_states].index = i;
  535. data->freq_table[valid_states].frequency =
  536. perf->states[i].core_frequency * 1000;
  537. valid_states++;
  538. }
  539. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  540. perf->state = 0;
  541. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  542. if (result)
  543. goto err_freqfree;
  544. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  545. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  546. switch (perf->control_register.space_id) {
  547. case ACPI_ADR_SPACE_SYSTEM_IO:
  548. /* Current speed is unknown and not detectable by IO port */
  549. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  550. break;
  551. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  552. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  553. policy->cur = get_cur_freq_on_cpu(cpu);
  554. break;
  555. default:
  556. break;
  557. }
  558. /* notify BIOS that we exist */
  559. acpi_processor_notify_smm(THIS_MODULE);
  560. /* Check for APERF/MPERF support in hardware */
  561. if (boot_cpu_has(X86_FEATURE_APERFMPERF))
  562. acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
  563. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  564. for (i = 0; i < perf->state_count; i++)
  565. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  566. (i == perf->state ? '*' : ' '), i,
  567. (u32) perf->states[i].core_frequency,
  568. (u32) perf->states[i].power,
  569. (u32) perf->states[i].transition_latency);
  570. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  571. /*
  572. * the first call to ->target() should result in us actually
  573. * writing something to the appropriate registers.
  574. */
  575. data->resume = 1;
  576. return result;
  577. err_freqfree:
  578. kfree(data->freq_table);
  579. err_unreg:
  580. acpi_processor_unregister_performance(perf, cpu);
  581. err_free:
  582. kfree(data);
  583. per_cpu(acfreq_data, cpu) = NULL;
  584. return result;
  585. }
  586. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  587. {
  588. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  589. pr_debug("acpi_cpufreq_cpu_exit\n");
  590. if (data) {
  591. cpufreq_frequency_table_put_attr(policy->cpu);
  592. per_cpu(acfreq_data, policy->cpu) = NULL;
  593. acpi_processor_unregister_performance(data->acpi_data,
  594. policy->cpu);
  595. kfree(data->freq_table);
  596. kfree(data);
  597. }
  598. return 0;
  599. }
  600. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  601. {
  602. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  603. pr_debug("acpi_cpufreq_resume\n");
  604. data->resume = 1;
  605. return 0;
  606. }
  607. static struct freq_attr *acpi_cpufreq_attr[] = {
  608. &cpufreq_freq_attr_scaling_available_freqs,
  609. NULL,
  610. };
  611. static struct cpufreq_driver acpi_cpufreq_driver = {
  612. .verify = acpi_cpufreq_verify,
  613. .target = acpi_cpufreq_target,
  614. .bios_limit = acpi_processor_get_bios_limit,
  615. .init = acpi_cpufreq_cpu_init,
  616. .exit = acpi_cpufreq_cpu_exit,
  617. .resume = acpi_cpufreq_resume,
  618. .name = "acpi-cpufreq",
  619. .owner = THIS_MODULE,
  620. .attr = acpi_cpufreq_attr,
  621. };
  622. static int __init acpi_cpufreq_init(void)
  623. {
  624. int ret;
  625. if (acpi_disabled)
  626. return 0;
  627. pr_debug("acpi_cpufreq_init\n");
  628. ret = acpi_cpufreq_early_init();
  629. if (ret)
  630. return ret;
  631. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  632. if (ret)
  633. free_acpi_perf_data();
  634. return ret;
  635. }
  636. static void __exit acpi_cpufreq_exit(void)
  637. {
  638. pr_debug("acpi_cpufreq_exit\n");
  639. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  640. free_acpi_perf_data();
  641. }
  642. module_param(acpi_pstate_strict, uint, 0644);
  643. MODULE_PARM_DESC(acpi_pstate_strict,
  644. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  645. "performed during frequency changes.");
  646. late_initcall(acpi_cpufreq_init);
  647. module_exit(acpi_cpufreq_exit);
  648. MODULE_ALIAS("acpi");