acpi-cpufreq.c 20 KB

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