acpi-cpufreq.c 21 KB

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