acpi-cpufreq.c 19 KB

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