acpi-cpufreq.c 21 KB

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