acpi-cpufreq.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 <linux/slab.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. #include "mperf.h"
  45. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  46. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  47. MODULE_LICENSE("GPL");
  48. #define PFX "acpi-cpufreq: "
  49. enum {
  50. UNDEFINED_CAPABLE = 0,
  51. SYSTEM_INTEL_MSR_CAPABLE,
  52. SYSTEM_AMD_MSR_CAPABLE,
  53. SYSTEM_IO_CAPABLE,
  54. };
  55. #define INTEL_MSR_RANGE (0xffff)
  56. #define AMD_MSR_RANGE (0x7)
  57. #define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
  58. struct acpi_cpufreq_data {
  59. struct acpi_processor_performance *acpi_data;
  60. struct cpufreq_frequency_table *freq_table;
  61. unsigned int resume;
  62. unsigned int cpu_feature;
  63. };
  64. static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
  65. /* acpi_perf_data is a pointer to percpu data. */
  66. static struct acpi_processor_performance __percpu *acpi_perf_data;
  67. static struct cpufreq_driver acpi_cpufreq_driver;
  68. static unsigned int acpi_pstate_strict;
  69. static bool boost_enabled, boost_supported;
  70. static struct msr __percpu *msrs;
  71. static bool boost_state(unsigned int cpu)
  72. {
  73. u32 lo, hi;
  74. u64 msr;
  75. switch (boot_cpu_data.x86_vendor) {
  76. case X86_VENDOR_INTEL:
  77. rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
  78. msr = lo | ((u64)hi << 32);
  79. return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
  80. case X86_VENDOR_AMD:
  81. rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
  82. msr = lo | ((u64)hi << 32);
  83. return !(msr & MSR_K7_HWCR_CPB_DIS);
  84. }
  85. return false;
  86. }
  87. static void boost_set_msrs(bool enable, const struct cpumask *cpumask)
  88. {
  89. u32 cpu;
  90. u32 msr_addr;
  91. u64 msr_mask;
  92. switch (boot_cpu_data.x86_vendor) {
  93. case X86_VENDOR_INTEL:
  94. msr_addr = MSR_IA32_MISC_ENABLE;
  95. msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
  96. break;
  97. case X86_VENDOR_AMD:
  98. msr_addr = MSR_K7_HWCR;
  99. msr_mask = MSR_K7_HWCR_CPB_DIS;
  100. break;
  101. default:
  102. return;
  103. }
  104. rdmsr_on_cpus(cpumask, msr_addr, msrs);
  105. for_each_cpu(cpu, cpumask) {
  106. struct msr *reg = per_cpu_ptr(msrs, cpu);
  107. if (enable)
  108. reg->q &= ~msr_mask;
  109. else
  110. reg->q |= msr_mask;
  111. }
  112. wrmsr_on_cpus(cpumask, msr_addr, msrs);
  113. }
  114. static ssize_t _store_boost(const char *buf, size_t count)
  115. {
  116. int ret;
  117. unsigned long val = 0;
  118. if (!boost_supported)
  119. return -EINVAL;
  120. ret = kstrtoul(buf, 10, &val);
  121. if (ret || (val > 1))
  122. return -EINVAL;
  123. if ((val && boost_enabled) || (!val && !boost_enabled))
  124. return count;
  125. get_online_cpus();
  126. boost_set_msrs(val, cpu_online_mask);
  127. put_online_cpus();
  128. boost_enabled = val;
  129. pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
  130. return count;
  131. }
  132. static ssize_t store_global_boost(struct kobject *kobj, struct attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. return _store_boost(buf, count);
  136. }
  137. static ssize_t show_global_boost(struct kobject *kobj,
  138. struct attribute *attr, char *buf)
  139. {
  140. return sprintf(buf, "%u\n", boost_enabled);
  141. }
  142. static struct global_attr global_boost = __ATTR(boost, 0644,
  143. show_global_boost,
  144. store_global_boost);
  145. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  146. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  147. size_t count)
  148. {
  149. return _store_boost(buf, count);
  150. }
  151. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  152. {
  153. return sprintf(buf, "%u\n", boost_enabled);
  154. }
  155. static struct freq_attr cpb = __ATTR(cpb, 0644, show_cpb, store_cpb);
  156. #endif
  157. static int check_est_cpu(unsigned int cpuid)
  158. {
  159. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  160. return cpu_has(cpu, X86_FEATURE_EST);
  161. }
  162. static int check_amd_hwpstate_cpu(unsigned int cpuid)
  163. {
  164. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  165. return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
  166. }
  167. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  168. {
  169. struct acpi_processor_performance *perf;
  170. int i;
  171. perf = data->acpi_data;
  172. for (i = 0; i < perf->state_count; i++) {
  173. if (value == perf->states[i].status)
  174. return data->freq_table[i].frequency;
  175. }
  176. return 0;
  177. }
  178. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  179. {
  180. int i;
  181. struct acpi_processor_performance *perf;
  182. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  183. msr &= AMD_MSR_RANGE;
  184. else
  185. msr &= INTEL_MSR_RANGE;
  186. perf = data->acpi_data;
  187. for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  188. if (msr == perf->states[data->freq_table[i].index].status)
  189. return data->freq_table[i].frequency;
  190. }
  191. return data->freq_table[0].frequency;
  192. }
  193. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  194. {
  195. switch (data->cpu_feature) {
  196. case SYSTEM_INTEL_MSR_CAPABLE:
  197. case SYSTEM_AMD_MSR_CAPABLE:
  198. return extract_msr(val, data);
  199. case SYSTEM_IO_CAPABLE:
  200. return extract_io(val, data);
  201. default:
  202. return 0;
  203. }
  204. }
  205. struct msr_addr {
  206. u32 reg;
  207. };
  208. struct io_addr {
  209. u16 port;
  210. u8 bit_width;
  211. };
  212. struct drv_cmd {
  213. unsigned int type;
  214. const struct cpumask *mask;
  215. union {
  216. struct msr_addr msr;
  217. struct io_addr io;
  218. } addr;
  219. u32 val;
  220. };
  221. /* Called via smp_call_function_single(), on the target CPU */
  222. static void do_drv_read(void *_cmd)
  223. {
  224. struct drv_cmd *cmd = _cmd;
  225. u32 h;
  226. switch (cmd->type) {
  227. case SYSTEM_INTEL_MSR_CAPABLE:
  228. case SYSTEM_AMD_MSR_CAPABLE:
  229. rdmsr(cmd->addr.msr.reg, cmd->val, h);
  230. break;
  231. case SYSTEM_IO_CAPABLE:
  232. acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
  233. &cmd->val,
  234. (u32)cmd->addr.io.bit_width);
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. /* Called via smp_call_function_many(), on the target CPUs */
  241. static void do_drv_write(void *_cmd)
  242. {
  243. struct drv_cmd *cmd = _cmd;
  244. u32 lo, hi;
  245. switch (cmd->type) {
  246. case SYSTEM_INTEL_MSR_CAPABLE:
  247. rdmsr(cmd->addr.msr.reg, lo, hi);
  248. lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
  249. wrmsr(cmd->addr.msr.reg, lo, hi);
  250. break;
  251. case SYSTEM_AMD_MSR_CAPABLE:
  252. wrmsr(cmd->addr.msr.reg, cmd->val, 0);
  253. break;
  254. case SYSTEM_IO_CAPABLE:
  255. acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
  256. cmd->val,
  257. (u32)cmd->addr.io.bit_width);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. static void drv_read(struct drv_cmd *cmd)
  264. {
  265. int err;
  266. cmd->val = 0;
  267. err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
  268. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  269. }
  270. static void drv_write(struct drv_cmd *cmd)
  271. {
  272. int this_cpu;
  273. this_cpu = get_cpu();
  274. if (cpumask_test_cpu(this_cpu, cmd->mask))
  275. do_drv_write(cmd);
  276. smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
  277. put_cpu();
  278. }
  279. static u32 get_cur_val(const struct cpumask *mask)
  280. {
  281. struct acpi_processor_performance *perf;
  282. struct drv_cmd cmd;
  283. if (unlikely(cpumask_empty(mask)))
  284. return 0;
  285. switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
  286. case SYSTEM_INTEL_MSR_CAPABLE:
  287. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  288. cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
  289. break;
  290. case SYSTEM_AMD_MSR_CAPABLE:
  291. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  292. cmd.addr.msr.reg = MSR_AMD_PERF_STATUS;
  293. break;
  294. case SYSTEM_IO_CAPABLE:
  295. cmd.type = SYSTEM_IO_CAPABLE;
  296. perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
  297. cmd.addr.io.port = perf->control_register.address;
  298. cmd.addr.io.bit_width = perf->control_register.bit_width;
  299. break;
  300. default:
  301. return 0;
  302. }
  303. cmd.mask = mask;
  304. drv_read(&cmd);
  305. pr_debug("get_cur_val = %u\n", cmd.val);
  306. return cmd.val;
  307. }
  308. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  309. {
  310. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
  311. unsigned int freq;
  312. unsigned int cached_freq;
  313. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  314. if (unlikely(data == NULL ||
  315. data->acpi_data == NULL || data->freq_table == NULL)) {
  316. return 0;
  317. }
  318. cached_freq = data->freq_table[data->acpi_data->state].frequency;
  319. freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
  320. if (freq != cached_freq) {
  321. /*
  322. * The dreaded BIOS frequency change behind our back.
  323. * Force set the frequency on next target call.
  324. */
  325. data->resume = 1;
  326. }
  327. pr_debug("cur freq = %u\n", freq);
  328. return freq;
  329. }
  330. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  331. struct acpi_cpufreq_data *data)
  332. {
  333. unsigned int cur_freq;
  334. unsigned int i;
  335. for (i = 0; i < 100; i++) {
  336. cur_freq = extract_freq(get_cur_val(mask), data);
  337. if (cur_freq == freq)
  338. return 1;
  339. udelay(10);
  340. }
  341. return 0;
  342. }
  343. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  344. unsigned int target_freq, unsigned int relation)
  345. {
  346. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  347. struct acpi_processor_performance *perf;
  348. struct cpufreq_freqs freqs;
  349. struct drv_cmd cmd;
  350. unsigned int next_state = 0; /* Index into freq_table */
  351. unsigned int next_perf_state = 0; /* Index into perf table */
  352. unsigned int i;
  353. int result = 0;
  354. pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
  355. if (unlikely(data == NULL ||
  356. data->acpi_data == NULL || data->freq_table == NULL)) {
  357. return -ENODEV;
  358. }
  359. perf = data->acpi_data;
  360. result = cpufreq_frequency_table_target(policy,
  361. data->freq_table,
  362. target_freq,
  363. relation, &next_state);
  364. if (unlikely(result)) {
  365. result = -ENODEV;
  366. goto out;
  367. }
  368. next_perf_state = data->freq_table[next_state].index;
  369. if (perf->state == next_perf_state) {
  370. if (unlikely(data->resume)) {
  371. pr_debug("Called after resume, resetting to P%d\n",
  372. next_perf_state);
  373. data->resume = 0;
  374. } else {
  375. pr_debug("Already at target state (P%d)\n",
  376. next_perf_state);
  377. goto out;
  378. }
  379. }
  380. switch (data->cpu_feature) {
  381. case SYSTEM_INTEL_MSR_CAPABLE:
  382. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  383. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  384. cmd.val = (u32) perf->states[next_perf_state].control;
  385. break;
  386. case SYSTEM_AMD_MSR_CAPABLE:
  387. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  388. cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
  389. cmd.val = (u32) perf->states[next_perf_state].control;
  390. break;
  391. case SYSTEM_IO_CAPABLE:
  392. cmd.type = SYSTEM_IO_CAPABLE;
  393. cmd.addr.io.port = perf->control_register.address;
  394. cmd.addr.io.bit_width = perf->control_register.bit_width;
  395. cmd.val = (u32) perf->states[next_perf_state].control;
  396. break;
  397. default:
  398. result = -ENODEV;
  399. goto out;
  400. }
  401. /* cpufreq holds the hotplug lock, so we are safe from here on */
  402. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  403. cmd.mask = policy->cpus;
  404. else
  405. cmd.mask = cpumask_of(policy->cpu);
  406. freqs.old = perf->states[perf->state].core_frequency * 1000;
  407. freqs.new = data->freq_table[next_state].frequency;
  408. for_each_cpu(i, policy->cpus) {
  409. freqs.cpu = i;
  410. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  411. }
  412. drv_write(&cmd);
  413. if (acpi_pstate_strict) {
  414. if (!check_freqs(cmd.mask, freqs.new, data)) {
  415. pr_debug("acpi_cpufreq_target failed (%d)\n",
  416. policy->cpu);
  417. result = -EAGAIN;
  418. goto out;
  419. }
  420. }
  421. for_each_cpu(i, policy->cpus) {
  422. freqs.cpu = i;
  423. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  424. }
  425. perf->state = next_perf_state;
  426. out:
  427. return result;
  428. }
  429. static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
  430. {
  431. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  432. pr_debug("acpi_cpufreq_verify\n");
  433. return cpufreq_frequency_table_verify(policy, data->freq_table);
  434. }
  435. static unsigned long
  436. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  437. {
  438. struct acpi_processor_performance *perf = data->acpi_data;
  439. if (cpu_khz) {
  440. /* search the closest match to cpu_khz */
  441. unsigned int i;
  442. unsigned long freq;
  443. unsigned long freqn = perf->states[0].core_frequency * 1000;
  444. for (i = 0; i < (perf->state_count-1); i++) {
  445. freq = freqn;
  446. freqn = perf->states[i+1].core_frequency * 1000;
  447. if ((2 * cpu_khz) > (freqn + freq)) {
  448. perf->state = i;
  449. return freq;
  450. }
  451. }
  452. perf->state = perf->state_count-1;
  453. return freqn;
  454. } else {
  455. /* assume CPU is at P0... */
  456. perf->state = 0;
  457. return perf->states[0].core_frequency * 1000;
  458. }
  459. }
  460. static void free_acpi_perf_data(void)
  461. {
  462. unsigned int i;
  463. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  464. for_each_possible_cpu(i)
  465. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  466. ->shared_cpu_map);
  467. free_percpu(acpi_perf_data);
  468. }
  469. static int boost_notify(struct notifier_block *nb, unsigned long action,
  470. void *hcpu)
  471. {
  472. unsigned cpu = (long)hcpu;
  473. const struct cpumask *cpumask;
  474. cpumask = get_cpu_mask(cpu);
  475. /*
  476. * Clear the boost-disable bit on the CPU_DOWN path so that
  477. * this cpu cannot block the remaining ones from boosting. On
  478. * the CPU_UP path we simply keep the boost-disable flag in
  479. * sync with the current global state.
  480. */
  481. switch (action) {
  482. case CPU_UP_PREPARE:
  483. case CPU_UP_PREPARE_FROZEN:
  484. boost_set_msrs(boost_enabled, cpumask);
  485. break;
  486. case CPU_DOWN_PREPARE:
  487. case CPU_DOWN_PREPARE_FROZEN:
  488. boost_set_msrs(1, cpumask);
  489. break;
  490. default:
  491. break;
  492. }
  493. return NOTIFY_OK;
  494. }
  495. static struct notifier_block boost_nb = {
  496. .notifier_call = boost_notify,
  497. };
  498. /*
  499. * acpi_cpufreq_early_init - initialize ACPI P-States library
  500. *
  501. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  502. * in order to determine correct frequency and voltage pairings. We can
  503. * do _PDC and _PSD and find out the processor dependency for the
  504. * actual init that will happen later...
  505. */
  506. static int __init acpi_cpufreq_early_init(void)
  507. {
  508. unsigned int i;
  509. pr_debug("acpi_cpufreq_early_init\n");
  510. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  511. if (!acpi_perf_data) {
  512. pr_debug("Memory allocation error for acpi_perf_data.\n");
  513. return -ENOMEM;
  514. }
  515. for_each_possible_cpu(i) {
  516. if (!zalloc_cpumask_var_node(
  517. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  518. GFP_KERNEL, cpu_to_node(i))) {
  519. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  520. free_acpi_perf_data();
  521. return -ENOMEM;
  522. }
  523. }
  524. /* Do initialization in ACPI core */
  525. acpi_processor_preregister_performance(acpi_perf_data);
  526. return 0;
  527. }
  528. #ifdef CONFIG_SMP
  529. /*
  530. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  531. * or do it in BIOS firmware and won't inform about it to OS. If not
  532. * detected, this has a side effect of making CPU run at a different speed
  533. * than OS intended it to run at. Detect it and handle it cleanly.
  534. */
  535. static int bios_with_sw_any_bug;
  536. static int sw_any_bug_found(const struct dmi_system_id *d)
  537. {
  538. bios_with_sw_any_bug = 1;
  539. return 0;
  540. }
  541. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  542. {
  543. .callback = sw_any_bug_found,
  544. .ident = "Supermicro Server X6DLP",
  545. .matches = {
  546. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  547. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  548. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  549. },
  550. },
  551. { }
  552. };
  553. static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
  554. {
  555. /* Intel Xeon Processor 7100 Series Specification Update
  556. * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
  557. * AL30: A Machine Check Exception (MCE) Occurring during an
  558. * Enhanced Intel SpeedStep Technology Ratio Change May Cause
  559. * Both Processor Cores to Lock Up. */
  560. if (c->x86_vendor == X86_VENDOR_INTEL) {
  561. if ((c->x86 == 15) &&
  562. (c->x86_model == 6) &&
  563. (c->x86_mask == 8)) {
  564. printk(KERN_INFO "acpi-cpufreq: Intel(R) "
  565. "Xeon(R) 7100 Errata AL30, processors may "
  566. "lock up on frequency changes: disabling "
  567. "acpi-cpufreq.\n");
  568. return -ENODEV;
  569. }
  570. }
  571. return 0;
  572. }
  573. #endif
  574. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  575. {
  576. unsigned int i;
  577. unsigned int valid_states = 0;
  578. unsigned int cpu = policy->cpu;
  579. struct acpi_cpufreq_data *data;
  580. unsigned int result = 0;
  581. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  582. struct acpi_processor_performance *perf;
  583. #ifdef CONFIG_SMP
  584. static int blacklisted;
  585. #endif
  586. pr_debug("acpi_cpufreq_cpu_init\n");
  587. #ifdef CONFIG_SMP
  588. if (blacklisted)
  589. return blacklisted;
  590. blacklisted = acpi_cpufreq_blacklist(c);
  591. if (blacklisted)
  592. return blacklisted;
  593. #endif
  594. data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
  595. if (!data)
  596. return -ENOMEM;
  597. data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
  598. per_cpu(acfreq_data, cpu) = data;
  599. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  600. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  601. result = acpi_processor_register_performance(data->acpi_data, cpu);
  602. if (result)
  603. goto err_free;
  604. perf = data->acpi_data;
  605. policy->shared_type = perf->shared_type;
  606. /*
  607. * Will let policy->cpus know about dependency only when software
  608. * coordination is required.
  609. */
  610. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  611. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  612. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  613. }
  614. cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
  615. #ifdef CONFIG_SMP
  616. dmi_check_system(sw_any_bug_dmi_table);
  617. if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
  618. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  619. cpumask_copy(policy->cpus, cpu_core_mask(cpu));
  620. }
  621. if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
  622. cpumask_clear(policy->cpus);
  623. cpumask_set_cpu(cpu, policy->cpus);
  624. cpumask_copy(policy->related_cpus, cpu_sibling_mask(cpu));
  625. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  626. pr_info_once(PFX "overriding BIOS provided _PSD data\n");
  627. }
  628. #endif
  629. /* capability check */
  630. if (perf->state_count <= 1) {
  631. pr_debug("No P-States\n");
  632. result = -ENODEV;
  633. goto err_unreg;
  634. }
  635. if (perf->control_register.space_id != perf->status_register.space_id) {
  636. result = -ENODEV;
  637. goto err_unreg;
  638. }
  639. switch (perf->control_register.space_id) {
  640. case ACPI_ADR_SPACE_SYSTEM_IO:
  641. pr_debug("SYSTEM IO addr space\n");
  642. data->cpu_feature = SYSTEM_IO_CAPABLE;
  643. break;
  644. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  645. pr_debug("HARDWARE addr space\n");
  646. if (check_est_cpu(cpu)) {
  647. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  648. break;
  649. }
  650. if (check_amd_hwpstate_cpu(cpu)) {
  651. data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
  652. break;
  653. }
  654. result = -ENODEV;
  655. goto err_unreg;
  656. default:
  657. pr_debug("Unknown addr space %d\n",
  658. (u32) (perf->control_register.space_id));
  659. result = -ENODEV;
  660. goto err_unreg;
  661. }
  662. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  663. (perf->state_count+1), GFP_KERNEL);
  664. if (!data->freq_table) {
  665. result = -ENOMEM;
  666. goto err_unreg;
  667. }
  668. /* detect transition latency */
  669. policy->cpuinfo.transition_latency = 0;
  670. for (i = 0; i < perf->state_count; i++) {
  671. if ((perf->states[i].transition_latency * 1000) >
  672. policy->cpuinfo.transition_latency)
  673. policy->cpuinfo.transition_latency =
  674. perf->states[i].transition_latency * 1000;
  675. }
  676. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  677. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  678. policy->cpuinfo.transition_latency > 20 * 1000) {
  679. policy->cpuinfo.transition_latency = 20 * 1000;
  680. printk_once(KERN_INFO
  681. "P-state transition latency capped at 20 uS\n");
  682. }
  683. /* table init */
  684. for (i = 0; i < perf->state_count; i++) {
  685. if (i > 0 && perf->states[i].core_frequency >=
  686. data->freq_table[valid_states-1].frequency / 1000)
  687. continue;
  688. data->freq_table[valid_states].index = i;
  689. data->freq_table[valid_states].frequency =
  690. perf->states[i].core_frequency * 1000;
  691. valid_states++;
  692. }
  693. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  694. perf->state = 0;
  695. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  696. if (result)
  697. goto err_freqfree;
  698. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  699. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  700. switch (perf->control_register.space_id) {
  701. case ACPI_ADR_SPACE_SYSTEM_IO:
  702. /* Current speed is unknown and not detectable by IO port */
  703. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  704. break;
  705. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  706. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  707. policy->cur = get_cur_freq_on_cpu(cpu);
  708. break;
  709. default:
  710. break;
  711. }
  712. /* notify BIOS that we exist */
  713. acpi_processor_notify_smm(THIS_MODULE);
  714. /* Check for APERF/MPERF support in hardware */
  715. if (boot_cpu_has(X86_FEATURE_APERFMPERF))
  716. acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
  717. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  718. for (i = 0; i < perf->state_count; i++)
  719. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  720. (i == perf->state ? '*' : ' '), i,
  721. (u32) perf->states[i].core_frequency,
  722. (u32) perf->states[i].power,
  723. (u32) perf->states[i].transition_latency);
  724. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  725. /*
  726. * the first call to ->target() should result in us actually
  727. * writing something to the appropriate registers.
  728. */
  729. data->resume = 1;
  730. return result;
  731. err_freqfree:
  732. kfree(data->freq_table);
  733. err_unreg:
  734. acpi_processor_unregister_performance(perf, cpu);
  735. err_free:
  736. kfree(data);
  737. per_cpu(acfreq_data, cpu) = NULL;
  738. return result;
  739. }
  740. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  741. {
  742. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  743. pr_debug("acpi_cpufreq_cpu_exit\n");
  744. if (data) {
  745. cpufreq_frequency_table_put_attr(policy->cpu);
  746. per_cpu(acfreq_data, policy->cpu) = NULL;
  747. acpi_processor_unregister_performance(data->acpi_data,
  748. policy->cpu);
  749. kfree(data->freq_table);
  750. kfree(data);
  751. }
  752. return 0;
  753. }
  754. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  755. {
  756. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  757. pr_debug("acpi_cpufreq_resume\n");
  758. data->resume = 1;
  759. return 0;
  760. }
  761. static struct freq_attr *acpi_cpufreq_attr[] = {
  762. &cpufreq_freq_attr_scaling_available_freqs,
  763. NULL, /* this is a placeholder for cpb, do not remove */
  764. NULL,
  765. };
  766. static struct cpufreq_driver acpi_cpufreq_driver = {
  767. .verify = acpi_cpufreq_verify,
  768. .target = acpi_cpufreq_target,
  769. .bios_limit = acpi_processor_get_bios_limit,
  770. .init = acpi_cpufreq_cpu_init,
  771. .exit = acpi_cpufreq_cpu_exit,
  772. .resume = acpi_cpufreq_resume,
  773. .name = "acpi-cpufreq",
  774. .owner = THIS_MODULE,
  775. .attr = acpi_cpufreq_attr,
  776. };
  777. static void __init acpi_cpufreq_boost_init(void)
  778. {
  779. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  780. msrs = msrs_alloc();
  781. if (!msrs)
  782. return;
  783. boost_supported = true;
  784. boost_enabled = boost_state(0);
  785. get_online_cpus();
  786. /* Force all MSRs to the same value */
  787. boost_set_msrs(boost_enabled, cpu_online_mask);
  788. register_cpu_notifier(&boost_nb);
  789. put_online_cpus();
  790. } else
  791. global_boost.attr.mode = 0444;
  792. /* We create the boost file in any case, though for systems without
  793. * hardware support it will be read-only and hardwired to return 0.
  794. */
  795. if (sysfs_create_file(cpufreq_global_kobject, &(global_boost.attr)))
  796. pr_warn(PFX "could not register global boost sysfs file\n");
  797. else
  798. pr_debug("registered global boost sysfs file\n");
  799. }
  800. static void __exit acpi_cpufreq_boost_exit(void)
  801. {
  802. sysfs_remove_file(cpufreq_global_kobject, &(global_boost.attr));
  803. if (msrs) {
  804. unregister_cpu_notifier(&boost_nb);
  805. msrs_free(msrs);
  806. msrs = NULL;
  807. }
  808. }
  809. static int __init acpi_cpufreq_init(void)
  810. {
  811. int ret;
  812. if (acpi_disabled)
  813. return 0;
  814. pr_debug("acpi_cpufreq_init\n");
  815. ret = acpi_cpufreq_early_init();
  816. if (ret)
  817. return ret;
  818. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  819. /* this is a sysfs file with a strange name and an even stranger
  820. * semantic - per CPU instantiation, but system global effect.
  821. * Lets enable it only on AMD CPUs for compatibility reasons and
  822. * only if configured. This is considered legacy code, which
  823. * will probably be removed at some point in the future.
  824. */
  825. if (check_amd_hwpstate_cpu(0)) {
  826. struct freq_attr **iter;
  827. pr_debug("adding sysfs entry for cpb\n");
  828. for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
  829. ;
  830. /* make sure there is a terminator behind it */
  831. if (iter[1] == NULL)
  832. *iter = &cpb;
  833. }
  834. #endif
  835. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  836. if (ret)
  837. free_acpi_perf_data();
  838. else
  839. acpi_cpufreq_boost_init();
  840. return ret;
  841. }
  842. static void __exit acpi_cpufreq_exit(void)
  843. {
  844. pr_debug("acpi_cpufreq_exit\n");
  845. acpi_cpufreq_boost_exit();
  846. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  847. free_acpi_perf_data();
  848. }
  849. module_param(acpi_pstate_strict, uint, 0644);
  850. MODULE_PARM_DESC(acpi_pstate_strict,
  851. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  852. "performed during frequency changes.");
  853. late_initcall(acpi_cpufreq_init);
  854. module_exit(acpi_cpufreq_exit);
  855. MODULE_ALIAS("acpi");