acpi-cpufreq.c 25 KB

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