acpi-cpufreq.c 25 KB

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