acpi-cpufreq.c 20 KB

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