acpi-cpufreq.c 25 KB

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