acpi-cpufreq.c 24 KB

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