acpi-cpufreq.c 25 KB

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