acpi-cpufreq.c 21 KB

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