acpi-cpufreq.c 19 KB

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