acpi-cpufreq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/acpi.h>
  36. #include <acpi/processor.h>
  37. #include <asm/io.h>
  38. #include <asm/msr.h>
  39. #include <asm/processor.h>
  40. #include <asm/cpufeature.h>
  41. #include <asm/delay.h>
  42. #include <asm/uaccess.h>
  43. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
  44. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  45. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  46. MODULE_LICENSE("GPL");
  47. enum {
  48. UNDEFINED_CAPABLE = 0,
  49. SYSTEM_INTEL_MSR_CAPABLE,
  50. SYSTEM_IO_CAPABLE,
  51. };
  52. #define INTEL_MSR_RANGE (0xffff)
  53. #define CPUID_6_ECX_APERFMPERF_CAPABILITY (0x1)
  54. struct acpi_cpufreq_data {
  55. struct acpi_processor_performance *acpi_data;
  56. struct cpufreq_frequency_table *freq_table;
  57. unsigned int max_freq;
  58. unsigned int resume;
  59. unsigned int cpu_feature;
  60. };
  61. static DEFINE_PER_CPU(struct acpi_cpufreq_data *, drv_data);
  62. /* acpi_perf_data is a pointer to percpu data. */
  63. static struct acpi_processor_performance *acpi_perf_data;
  64. static struct cpufreq_driver acpi_cpufreq_driver;
  65. static unsigned int acpi_pstate_strict;
  66. static int check_est_cpu(unsigned int cpuid)
  67. {
  68. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  69. if (cpu->x86_vendor != X86_VENDOR_INTEL ||
  70. !cpu_has(cpu, X86_FEATURE_EST))
  71. return 0;
  72. return 1;
  73. }
  74. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  75. {
  76. struct acpi_processor_performance *perf;
  77. int i;
  78. perf = data->acpi_data;
  79. for (i=0; i<perf->state_count; i++) {
  80. if (value == perf->states[i].status)
  81. return data->freq_table[i].frequency;
  82. }
  83. return 0;
  84. }
  85. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  86. {
  87. int i;
  88. struct acpi_processor_performance *perf;
  89. msr &= INTEL_MSR_RANGE;
  90. perf = data->acpi_data;
  91. for (i=0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  92. if (msr == perf->states[data->freq_table[i].index].status)
  93. return data->freq_table[i].frequency;
  94. }
  95. return data->freq_table[0].frequency;
  96. }
  97. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  98. {
  99. switch (data->cpu_feature) {
  100. case SYSTEM_INTEL_MSR_CAPABLE:
  101. return extract_msr(val, data);
  102. case SYSTEM_IO_CAPABLE:
  103. return extract_io(val, data);
  104. default:
  105. return 0;
  106. }
  107. }
  108. struct msr_addr {
  109. u32 reg;
  110. };
  111. struct io_addr {
  112. u16 port;
  113. u8 bit_width;
  114. };
  115. typedef union {
  116. struct msr_addr msr;
  117. struct io_addr io;
  118. } drv_addr_union;
  119. struct drv_cmd {
  120. unsigned int type;
  121. cpumask_t mask;
  122. drv_addr_union addr;
  123. u32 val;
  124. };
  125. static void do_drv_read(struct drv_cmd *cmd)
  126. {
  127. u32 h;
  128. switch (cmd->type) {
  129. case SYSTEM_INTEL_MSR_CAPABLE:
  130. rdmsr(cmd->addr.msr.reg, cmd->val, h);
  131. break;
  132. case SYSTEM_IO_CAPABLE:
  133. acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
  134. &cmd->val,
  135. (u32)cmd->addr.io.bit_width);
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. static void do_drv_write(struct drv_cmd *cmd)
  142. {
  143. u32 lo, hi;
  144. switch (cmd->type) {
  145. case SYSTEM_INTEL_MSR_CAPABLE:
  146. rdmsr(cmd->addr.msr.reg, lo, hi);
  147. lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
  148. wrmsr(cmd->addr.msr.reg, lo, hi);
  149. break;
  150. case SYSTEM_IO_CAPABLE:
  151. acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
  152. cmd->val,
  153. (u32)cmd->addr.io.bit_width);
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. static void drv_read(struct drv_cmd *cmd)
  160. {
  161. cpumask_t saved_mask = current->cpus_allowed;
  162. cmd->val = 0;
  163. set_cpus_allowed_ptr(current, &cmd->mask);
  164. do_drv_read(cmd);
  165. set_cpus_allowed_ptr(current, &saved_mask);
  166. }
  167. static void drv_write(struct drv_cmd *cmd)
  168. {
  169. cpumask_t saved_mask = current->cpus_allowed;
  170. cpumask_of_cpu_ptr_declare(cpu_mask);
  171. unsigned int i;
  172. for_each_cpu_mask_nr(i, cmd->mask) {
  173. cpumask_of_cpu_ptr_next(cpu_mask, i);
  174. set_cpus_allowed_ptr(current, cpu_mask);
  175. do_drv_write(cmd);
  176. }
  177. set_cpus_allowed_ptr(current, &saved_mask);
  178. return;
  179. }
  180. static u32 get_cur_val(const cpumask_t *mask)
  181. {
  182. struct acpi_processor_performance *perf;
  183. struct drv_cmd cmd;
  184. if (unlikely(cpus_empty(*mask)))
  185. return 0;
  186. switch (per_cpu(drv_data, first_cpu(*mask))->cpu_feature) {
  187. case SYSTEM_INTEL_MSR_CAPABLE:
  188. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  189. cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
  190. break;
  191. case SYSTEM_IO_CAPABLE:
  192. cmd.type = SYSTEM_IO_CAPABLE;
  193. perf = per_cpu(drv_data, first_cpu(*mask))->acpi_data;
  194. cmd.addr.io.port = perf->control_register.address;
  195. cmd.addr.io.bit_width = perf->control_register.bit_width;
  196. break;
  197. default:
  198. return 0;
  199. }
  200. cmd.mask = *mask;
  201. drv_read(&cmd);
  202. dprintk("get_cur_val = %u\n", cmd.val);
  203. return cmd.val;
  204. }
  205. /*
  206. * Return the measured active (C0) frequency on this CPU since last call
  207. * to this function.
  208. * Input: cpu number
  209. * Return: Average CPU frequency in terms of max frequency (zero on error)
  210. *
  211. * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
  212. * over a period of time, while CPU is in C0 state.
  213. * IA32_MPERF counts at the rate of max advertised frequency
  214. * IA32_APERF counts at the rate of actual CPU frequency
  215. * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
  216. * no meaning should be associated with absolute values of these MSRs.
  217. */
  218. static unsigned int get_measured_perf(unsigned int cpu)
  219. {
  220. union {
  221. struct {
  222. u32 lo;
  223. u32 hi;
  224. } split;
  225. u64 whole;
  226. } aperf_cur, mperf_cur;
  227. cpumask_t saved_mask;
  228. cpumask_of_cpu_ptr(cpu_mask, cpu);
  229. unsigned int perf_percent;
  230. unsigned int retval;
  231. saved_mask = current->cpus_allowed;
  232. set_cpus_allowed_ptr(current, cpu_mask);
  233. if (get_cpu() != cpu) {
  234. /* We were not able to run on requested processor */
  235. put_cpu();
  236. return 0;
  237. }
  238. rdmsr(MSR_IA32_APERF, aperf_cur.split.lo, aperf_cur.split.hi);
  239. rdmsr(MSR_IA32_MPERF, mperf_cur.split.lo, mperf_cur.split.hi);
  240. wrmsr(MSR_IA32_APERF, 0,0);
  241. wrmsr(MSR_IA32_MPERF, 0,0);
  242. #ifdef __i386__
  243. /*
  244. * We dont want to do 64 bit divide with 32 bit kernel
  245. * Get an approximate value. Return failure in case we cannot get
  246. * an approximate value.
  247. */
  248. if (unlikely(aperf_cur.split.hi || mperf_cur.split.hi)) {
  249. int shift_count;
  250. u32 h;
  251. h = max_t(u32, aperf_cur.split.hi, mperf_cur.split.hi);
  252. shift_count = fls(h);
  253. aperf_cur.whole >>= shift_count;
  254. mperf_cur.whole >>= shift_count;
  255. }
  256. if (((unsigned long)(-1) / 100) < aperf_cur.split.lo) {
  257. int shift_count = 7;
  258. aperf_cur.split.lo >>= shift_count;
  259. mperf_cur.split.lo >>= shift_count;
  260. }
  261. if (aperf_cur.split.lo && mperf_cur.split.lo)
  262. perf_percent = (aperf_cur.split.lo * 100) / mperf_cur.split.lo;
  263. else
  264. perf_percent = 0;
  265. #else
  266. if (unlikely(((unsigned long)(-1) / 100) < aperf_cur.whole)) {
  267. int shift_count = 7;
  268. aperf_cur.whole >>= shift_count;
  269. mperf_cur.whole >>= shift_count;
  270. }
  271. if (aperf_cur.whole && mperf_cur.whole)
  272. perf_percent = (aperf_cur.whole * 100) / mperf_cur.whole;
  273. else
  274. perf_percent = 0;
  275. #endif
  276. retval = per_cpu(drv_data, cpu)->max_freq * perf_percent / 100;
  277. put_cpu();
  278. set_cpus_allowed_ptr(current, &saved_mask);
  279. dprintk("cpu %d: performance percent %d\n", cpu, perf_percent);
  280. return retval;
  281. }
  282. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  283. {
  284. cpumask_of_cpu_ptr(cpu_mask, cpu);
  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(cpu_mask), 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 cpumask_t *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. cpumask_t online_policy_cpus;
  325. struct drv_cmd cmd;
  326. unsigned int next_state = 0; /* Index into freq_table */
  327. unsigned int next_perf_state = 0; /* Index into perf table */
  328. unsigned int i;
  329. int result = 0;
  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. return -ENODEV;
  342. #ifdef CONFIG_HOTPLUG_CPU
  343. /* cpufreq holds the hotplug lock, so we are safe from here on */
  344. cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
  345. #else
  346. online_policy_cpus = policy->cpus;
  347. #endif
  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. return 0;
  358. }
  359. }
  360. switch (data->cpu_feature) {
  361. case SYSTEM_INTEL_MSR_CAPABLE:
  362. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  363. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  364. cmd.val = (u32) perf->states[next_perf_state].control;
  365. break;
  366. case SYSTEM_IO_CAPABLE:
  367. cmd.type = SYSTEM_IO_CAPABLE;
  368. cmd.addr.io.port = perf->control_register.address;
  369. cmd.addr.io.bit_width = perf->control_register.bit_width;
  370. cmd.val = (u32) perf->states[next_perf_state].control;
  371. break;
  372. default:
  373. return -ENODEV;
  374. }
  375. cpus_clear(cmd.mask);
  376. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  377. cmd.mask = online_policy_cpus;
  378. else
  379. cpu_set(policy->cpu, cmd.mask);
  380. freqs.old = perf->states[perf->state].core_frequency * 1000;
  381. freqs.new = data->freq_table[next_state].frequency;
  382. for_each_cpu_mask_nr(i, cmd.mask) {
  383. freqs.cpu = i;
  384. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  385. }
  386. drv_write(&cmd);
  387. if (acpi_pstate_strict) {
  388. if (!check_freqs(&cmd.mask, freqs.new, data)) {
  389. dprintk("acpi_cpufreq_target failed (%d)\n",
  390. policy->cpu);
  391. return -EAGAIN;
  392. }
  393. }
  394. for_each_cpu_mask_nr(i, cmd.mask) {
  395. freqs.cpu = i;
  396. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  397. }
  398. perf->state = next_perf_state;
  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. /*
  433. * acpi_cpufreq_early_init - initialize ACPI P-States library
  434. *
  435. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  436. * in order to determine correct frequency and voltage pairings. We can
  437. * do _PDC and _PSD and find out the processor dependency for the
  438. * actual init that will happen later...
  439. */
  440. static int __init acpi_cpufreq_early_init(void)
  441. {
  442. dprintk("acpi_cpufreq_early_init\n");
  443. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  444. if (!acpi_perf_data) {
  445. dprintk("Memory allocation error for acpi_perf_data.\n");
  446. return -ENOMEM;
  447. }
  448. /* Do initialization in ACPI core */
  449. acpi_processor_preregister_performance(acpi_perf_data);
  450. return 0;
  451. }
  452. #ifdef CONFIG_SMP
  453. /*
  454. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  455. * or do it in BIOS firmware and won't inform about it to OS. If not
  456. * detected, this has a side effect of making CPU run at a different speed
  457. * than OS intended it to run at. Detect it and handle it cleanly.
  458. */
  459. static int bios_with_sw_any_bug;
  460. static int sw_any_bug_found(const struct dmi_system_id *d)
  461. {
  462. bios_with_sw_any_bug = 1;
  463. return 0;
  464. }
  465. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  466. {
  467. .callback = sw_any_bug_found,
  468. .ident = "Supermicro Server X6DLP",
  469. .matches = {
  470. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  471. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  472. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  473. },
  474. },
  475. { }
  476. };
  477. #endif
  478. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  479. {
  480. unsigned int i;
  481. unsigned int valid_states = 0;
  482. unsigned int cpu = policy->cpu;
  483. struct acpi_cpufreq_data *data;
  484. unsigned int result = 0;
  485. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  486. struct acpi_processor_performance *perf;
  487. dprintk("acpi_cpufreq_cpu_init\n");
  488. data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
  489. if (!data)
  490. return -ENOMEM;
  491. data->acpi_data = percpu_ptr(acpi_perf_data, cpu);
  492. per_cpu(drv_data, cpu) = data;
  493. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  494. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  495. result = acpi_processor_register_performance(data->acpi_data, cpu);
  496. if (result)
  497. goto err_free;
  498. perf = data->acpi_data;
  499. policy->shared_type = perf->shared_type;
  500. /*
  501. * Will let policy->cpus know about dependency only when software
  502. * coordination is required.
  503. */
  504. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  505. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  506. policy->cpus = perf->shared_cpu_map;
  507. }
  508. policy->related_cpus = perf->shared_cpu_map;
  509. #ifdef CONFIG_SMP
  510. dmi_check_system(sw_any_bug_dmi_table);
  511. if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
  512. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  513. policy->cpus = per_cpu(cpu_core_map, cpu);
  514. }
  515. #endif
  516. /* capability check */
  517. if (perf->state_count <= 1) {
  518. dprintk("No P-States\n");
  519. result = -ENODEV;
  520. goto err_unreg;
  521. }
  522. if (perf->control_register.space_id != perf->status_register.space_id) {
  523. result = -ENODEV;
  524. goto err_unreg;
  525. }
  526. switch (perf->control_register.space_id) {
  527. case ACPI_ADR_SPACE_SYSTEM_IO:
  528. dprintk("SYSTEM IO addr space\n");
  529. data->cpu_feature = SYSTEM_IO_CAPABLE;
  530. break;
  531. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  532. dprintk("HARDWARE addr space\n");
  533. if (!check_est_cpu(cpu)) {
  534. result = -ENODEV;
  535. goto err_unreg;
  536. }
  537. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  538. break;
  539. default:
  540. dprintk("Unknown addr space %d\n",
  541. (u32) (perf->control_register.space_id));
  542. result = -ENODEV;
  543. goto err_unreg;
  544. }
  545. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  546. (perf->state_count+1), GFP_KERNEL);
  547. if (!data->freq_table) {
  548. result = -ENOMEM;
  549. goto err_unreg;
  550. }
  551. /* detect transition latency */
  552. policy->cpuinfo.transition_latency = 0;
  553. for (i=0; i<perf->state_count; i++) {
  554. if ((perf->states[i].transition_latency * 1000) >
  555. policy->cpuinfo.transition_latency)
  556. policy->cpuinfo.transition_latency =
  557. perf->states[i].transition_latency * 1000;
  558. }
  559. data->max_freq = perf->states[0].core_frequency * 1000;
  560. /* table init */
  561. for (i=0; i<perf->state_count; i++) {
  562. if (i>0 && perf->states[i].core_frequency >=
  563. data->freq_table[valid_states-1].frequency / 1000)
  564. continue;
  565. data->freq_table[valid_states].index = i;
  566. data->freq_table[valid_states].frequency =
  567. perf->states[i].core_frequency * 1000;
  568. valid_states++;
  569. }
  570. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  571. perf->state = 0;
  572. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  573. if (result)
  574. goto err_freqfree;
  575. switch (perf->control_register.space_id) {
  576. case ACPI_ADR_SPACE_SYSTEM_IO:
  577. /* Current speed is unknown and not detectable by IO port */
  578. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  579. break;
  580. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  581. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  582. policy->cur = get_cur_freq_on_cpu(cpu);
  583. break;
  584. default:
  585. break;
  586. }
  587. /* notify BIOS that we exist */
  588. acpi_processor_notify_smm(THIS_MODULE);
  589. /* Check for APERF/MPERF support in hardware */
  590. if (c->x86_vendor == X86_VENDOR_INTEL && c->cpuid_level >= 6) {
  591. unsigned int ecx;
  592. ecx = cpuid_ecx(6);
  593. if (ecx & CPUID_6_ECX_APERFMPERF_CAPABILITY)
  594. acpi_cpufreq_driver.getavg = get_measured_perf;
  595. }
  596. dprintk("CPU%u - ACPI performance management activated.\n", cpu);
  597. for (i = 0; i < perf->state_count; i++)
  598. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  599. (i == perf->state ? '*' : ' '), i,
  600. (u32) perf->states[i].core_frequency,
  601. (u32) perf->states[i].power,
  602. (u32) perf->states[i].transition_latency);
  603. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  604. /*
  605. * the first call to ->target() should result in us actually
  606. * writing something to the appropriate registers.
  607. */
  608. data->resume = 1;
  609. return result;
  610. err_freqfree:
  611. kfree(data->freq_table);
  612. err_unreg:
  613. acpi_processor_unregister_performance(perf, cpu);
  614. err_free:
  615. kfree(data);
  616. per_cpu(drv_data, cpu) = NULL;
  617. return result;
  618. }
  619. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  620. {
  621. struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
  622. dprintk("acpi_cpufreq_cpu_exit\n");
  623. if (data) {
  624. cpufreq_frequency_table_put_attr(policy->cpu);
  625. per_cpu(drv_data, policy->cpu) = NULL;
  626. acpi_processor_unregister_performance(data->acpi_data,
  627. policy->cpu);
  628. kfree(data);
  629. }
  630. return 0;
  631. }
  632. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  633. {
  634. struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
  635. dprintk("acpi_cpufreq_resume\n");
  636. data->resume = 1;
  637. return 0;
  638. }
  639. static struct freq_attr *acpi_cpufreq_attr[] = {
  640. &cpufreq_freq_attr_scaling_available_freqs,
  641. NULL,
  642. };
  643. static struct cpufreq_driver acpi_cpufreq_driver = {
  644. .verify = acpi_cpufreq_verify,
  645. .target = acpi_cpufreq_target,
  646. .init = acpi_cpufreq_cpu_init,
  647. .exit = acpi_cpufreq_cpu_exit,
  648. .resume = acpi_cpufreq_resume,
  649. .name = "acpi-cpufreq",
  650. .owner = THIS_MODULE,
  651. .attr = acpi_cpufreq_attr,
  652. };
  653. static int __init acpi_cpufreq_init(void)
  654. {
  655. int ret;
  656. dprintk("acpi_cpufreq_init\n");
  657. ret = acpi_cpufreq_early_init();
  658. if (ret)
  659. return ret;
  660. return cpufreq_register_driver(&acpi_cpufreq_driver);
  661. }
  662. static void __exit acpi_cpufreq_exit(void)
  663. {
  664. dprintk("acpi_cpufreq_exit\n");
  665. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  666. free_percpu(acpi_perf_data);
  667. return;
  668. }
  669. module_param(acpi_pstate_strict, uint, 0644);
  670. MODULE_PARM_DESC(acpi_pstate_strict,
  671. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  672. "performed during frequency changes.");
  673. late_initcall(acpi_cpufreq_init);
  674. module_exit(acpi_cpufreq_exit);
  675. MODULE_ALIAS("acpi");