acpi-cpufreq.c 20 KB

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