acpi-cpufreq.c 21 KB

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