acpi-cpufreq.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. 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. 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 struct msr __percpu *msrs;
  75. static bool boost_state(unsigned int cpu)
  76. {
  77. u32 lo, hi;
  78. u64 msr;
  79. switch (boot_cpu_data.x86_vendor) {
  80. case X86_VENDOR_INTEL:
  81. rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
  82. msr = lo | ((u64)hi << 32);
  83. return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
  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 void boost_set_msrs(bool enable, const struct cpumask *cpumask)
  92. {
  93. u32 cpu;
  94. u32 msr_addr;
  95. u64 msr_mask;
  96. switch (boot_cpu_data.x86_vendor) {
  97. case X86_VENDOR_INTEL:
  98. msr_addr = MSR_IA32_MISC_ENABLE;
  99. msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
  100. break;
  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;
  107. }
  108. rdmsr_on_cpus(cpumask, msr_addr, msrs);
  109. for_each_cpu(cpu, cpumask) {
  110. struct msr *reg = per_cpu_ptr(msrs, cpu);
  111. if (enable)
  112. reg->q &= ~msr_mask;
  113. else
  114. reg->q |= msr_mask;
  115. }
  116. wrmsr_on_cpus(cpumask, msr_addr, msrs);
  117. }
  118. static int set_boost(int val)
  119. {
  120. get_online_cpus();
  121. boost_set_msrs(val, cpu_online_mask);
  122. put_online_cpus();
  123. pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
  124. return 0;
  125. }
  126. static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
  127. {
  128. struct acpi_cpufreq_data *data = policy->driver_data;
  129. if (unlikely(!data))
  130. return -ENODEV;
  131. return cpufreq_show_cpus(data->freqdomain_cpus, buf);
  132. }
  133. cpufreq_freq_attr_ro(freqdomain_cpus);
  134. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  135. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  136. size_t count)
  137. {
  138. int ret;
  139. unsigned int val = 0;
  140. if (!acpi_cpufreq_driver.set_boost)
  141. return -EINVAL;
  142. ret = kstrtouint(buf, 10, &val);
  143. if (ret || val > 1)
  144. return -EINVAL;
  145. set_boost(val);
  146. return count;
  147. }
  148. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  149. {
  150. return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
  151. }
  152. cpufreq_freq_attr_rw(cpb);
  153. #endif
  154. static int check_est_cpu(unsigned int cpuid)
  155. {
  156. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  157. return cpu_has(cpu, X86_FEATURE_EST);
  158. }
  159. static int check_amd_hwpstate_cpu(unsigned int cpuid)
  160. {
  161. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  162. return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
  163. }
  164. static unsigned extract_io(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. static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used)
  202. {
  203. u32 val, dummy;
  204. rdmsr(MSR_IA32_PERF_CTL, val, dummy);
  205. return val;
  206. }
  207. static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val)
  208. {
  209. u32 lo, hi;
  210. rdmsr(MSR_IA32_PERF_CTL, lo, hi);
  211. lo = (lo & ~INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE);
  212. wrmsr(MSR_IA32_PERF_CTL, lo, hi);
  213. }
  214. static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used)
  215. {
  216. u32 val, dummy;
  217. rdmsr(MSR_AMD_PERF_CTL, val, dummy);
  218. return val;
  219. }
  220. static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val)
  221. {
  222. wrmsr(MSR_AMD_PERF_CTL, val, 0);
  223. }
  224. static u32 cpu_freq_read_io(struct acpi_pct_register *reg)
  225. {
  226. u32 val;
  227. acpi_os_read_port(reg->address, &val, reg->bit_width);
  228. return val;
  229. }
  230. static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val)
  231. {
  232. acpi_os_write_port(reg->address, val, reg->bit_width);
  233. }
  234. struct drv_cmd {
  235. struct acpi_pct_register *reg;
  236. u32 val;
  237. union {
  238. void (*write)(struct acpi_pct_register *reg, u32 val);
  239. u32 (*read)(struct acpi_pct_register *reg);
  240. } func;
  241. };
  242. /* Called via smp_call_function_single(), on the target CPU */
  243. static void do_drv_read(void *_cmd)
  244. {
  245. struct drv_cmd *cmd = _cmd;
  246. cmd->val = cmd->func.read(cmd->reg);
  247. }
  248. static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)
  249. {
  250. struct acpi_processor_performance *perf = to_perf_data(data);
  251. struct drv_cmd cmd = {
  252. .reg = &perf->control_register,
  253. .func.read = data->cpu_freq_read,
  254. };
  255. int err;
  256. err = smp_call_function_any(mask, do_drv_read, &cmd, 1);
  257. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  258. return cmd.val;
  259. }
  260. /* Called via smp_call_function_many(), on the target CPUs */
  261. static void do_drv_write(void *_cmd)
  262. {
  263. struct drv_cmd *cmd = _cmd;
  264. cmd->func.write(cmd->reg, cmd->val);
  265. }
  266. static void drv_write(struct acpi_cpufreq_data *data,
  267. const struct cpumask *mask, u32 val)
  268. {
  269. struct acpi_processor_performance *perf = to_perf_data(data);
  270. struct drv_cmd cmd = {
  271. .reg = &perf->control_register,
  272. .val = val,
  273. .func.write = data->cpu_freq_write,
  274. };
  275. int this_cpu;
  276. this_cpu = get_cpu();
  277. if (cpumask_test_cpu(this_cpu, mask))
  278. do_drv_write(&cmd);
  279. smp_call_function_many(mask, do_drv_write, &cmd, 1);
  280. put_cpu();
  281. }
  282. static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
  283. {
  284. u32 val;
  285. if (unlikely(cpumask_empty(mask)))
  286. return 0;
  287. val = drv_read(data, mask);
  288. pr_debug("get_cur_val = %u\n", val);
  289. return val;
  290. }
  291. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  292. {
  293. struct acpi_cpufreq_data *data;
  294. struct cpufreq_policy *policy;
  295. unsigned int freq;
  296. unsigned int cached_freq;
  297. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  298. policy = cpufreq_cpu_get_raw(cpu);
  299. if (unlikely(!policy))
  300. return 0;
  301. data = policy->driver_data;
  302. if (unlikely(!data || !data->freq_table))
  303. return 0;
  304. cached_freq = data->freq_table[to_perf_data(data)->state].frequency;
  305. freq = extract_freq(get_cur_val(cpumask_of(cpu), data), data);
  306. if (freq != cached_freq) {
  307. /*
  308. * The dreaded BIOS frequency change behind our back.
  309. * Force set the frequency on next target call.
  310. */
  311. data->resume = 1;
  312. }
  313. pr_debug("cur freq = %u\n", freq);
  314. return freq;
  315. }
  316. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  317. struct acpi_cpufreq_data *data)
  318. {
  319. unsigned int cur_freq;
  320. unsigned int i;
  321. for (i = 0; i < 100; i++) {
  322. cur_freq = extract_freq(get_cur_val(mask, data), data);
  323. if (cur_freq == freq)
  324. return 1;
  325. udelay(10);
  326. }
  327. return 0;
  328. }
  329. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  330. unsigned int index)
  331. {
  332. struct acpi_cpufreq_data *data = policy->driver_data;
  333. struct acpi_processor_performance *perf;
  334. const struct cpumask *mask;
  335. unsigned int next_perf_state = 0; /* Index into perf table */
  336. int result = 0;
  337. if (unlikely(data == NULL || data->freq_table == NULL)) {
  338. return -ENODEV;
  339. }
  340. perf = to_perf_data(data);
  341. next_perf_state = data->freq_table[index].driver_data;
  342. if (perf->state == next_perf_state) {
  343. if (unlikely(data->resume)) {
  344. pr_debug("Called after resume, resetting to P%d\n",
  345. next_perf_state);
  346. data->resume = 0;
  347. } else {
  348. pr_debug("Already at target state (P%d)\n",
  349. next_perf_state);
  350. return 0;
  351. }
  352. }
  353. /*
  354. * The core won't allow CPUs to go away until the governor has been
  355. * stopped, so we can rely on the stability of policy->cpus.
  356. */
  357. mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ?
  358. cpumask_of(policy->cpu) : policy->cpus;
  359. drv_write(data, mask, perf->states[next_perf_state].control);
  360. if (acpi_pstate_strict) {
  361. if (!check_freqs(mask, data->freq_table[index].frequency,
  362. data)) {
  363. pr_debug("acpi_cpufreq_target failed (%d)\n",
  364. policy->cpu);
  365. result = -EAGAIN;
  366. }
  367. }
  368. if (!result)
  369. perf->state = next_perf_state;
  370. return result;
  371. }
  372. unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
  373. unsigned int target_freq)
  374. {
  375. struct acpi_cpufreq_data *data = policy->driver_data;
  376. struct acpi_processor_performance *perf;
  377. struct cpufreq_frequency_table *entry;
  378. unsigned int next_perf_state, next_freq, freq;
  379. /*
  380. * Find the closest frequency above target_freq.
  381. *
  382. * The table is sorted in the reverse order with respect to the
  383. * frequency and all of the entries are valid (see the initialization).
  384. */
  385. entry = data->freq_table;
  386. do {
  387. entry++;
  388. freq = entry->frequency;
  389. } while (freq >= target_freq && freq != CPUFREQ_TABLE_END);
  390. entry--;
  391. next_freq = entry->frequency;
  392. next_perf_state = entry->driver_data;
  393. perf = to_perf_data(data);
  394. if (perf->state == next_perf_state) {
  395. if (unlikely(data->resume))
  396. data->resume = 0;
  397. else
  398. return next_freq;
  399. }
  400. data->cpu_freq_write(&perf->control_register,
  401. perf->states[next_perf_state].control);
  402. perf->state = next_perf_state;
  403. return next_freq;
  404. }
  405. static unsigned long
  406. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  407. {
  408. struct acpi_processor_performance *perf;
  409. perf = to_perf_data(data);
  410. if (cpu_khz) {
  411. /* search the closest match to cpu_khz */
  412. unsigned int i;
  413. unsigned long freq;
  414. unsigned long freqn = perf->states[0].core_frequency * 1000;
  415. for (i = 0; i < (perf->state_count-1); i++) {
  416. freq = freqn;
  417. freqn = perf->states[i+1].core_frequency * 1000;
  418. if ((2 * cpu_khz) > (freqn + freq)) {
  419. perf->state = i;
  420. return freq;
  421. }
  422. }
  423. perf->state = perf->state_count-1;
  424. return freqn;
  425. } else {
  426. /* assume CPU is at P0... */
  427. perf->state = 0;
  428. return perf->states[0].core_frequency * 1000;
  429. }
  430. }
  431. static void free_acpi_perf_data(void)
  432. {
  433. unsigned int i;
  434. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  435. for_each_possible_cpu(i)
  436. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  437. ->shared_cpu_map);
  438. free_percpu(acpi_perf_data);
  439. }
  440. static int boost_notify(struct notifier_block *nb, unsigned long action,
  441. void *hcpu)
  442. {
  443. unsigned cpu = (long)hcpu;
  444. const struct cpumask *cpumask;
  445. cpumask = get_cpu_mask(cpu);
  446. /*
  447. * Clear the boost-disable bit on the CPU_DOWN path so that
  448. * this cpu cannot block the remaining ones from boosting. On
  449. * the CPU_UP path we simply keep the boost-disable flag in
  450. * sync with the current global state.
  451. */
  452. switch (action) {
  453. case CPU_DOWN_FAILED:
  454. case CPU_DOWN_FAILED_FROZEN:
  455. case CPU_ONLINE:
  456. case CPU_ONLINE_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. pr_info("Intel(R) Xeon(R) 7100 Errata AL30, processors may lock up on frequency changes: disabling acpi-cpufreq\n");
  538. return -ENODEV;
  539. }
  540. }
  541. return 0;
  542. }
  543. #endif
  544. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  545. {
  546. unsigned int i;
  547. unsigned int valid_states = 0;
  548. unsigned int cpu = policy->cpu;
  549. struct acpi_cpufreq_data *data;
  550. unsigned int result = 0;
  551. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  552. struct acpi_processor_performance *perf;
  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. data->freq_table = kzalloc(sizeof(*data->freq_table) *
  650. (perf->state_count+1), GFP_KERNEL);
  651. if (!data->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. data->freq_table[valid_states-1].frequency / 1000)
  673. continue;
  674. data->freq_table[valid_states].driver_data = i;
  675. data->freq_table[valid_states].frequency =
  676. perf->states[i].core_frequency * 1000;
  677. valid_states++;
  678. }
  679. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  680. perf->state = 0;
  681. result = cpufreq_table_validate_and_show(policy, data->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(data->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. if (data) {
  735. policy->fast_switch_possible = false;
  736. policy->driver_data = NULL;
  737. acpi_processor_unregister_performance(data->acpi_perf_cpu);
  738. free_cpumask_var(data->freqdomain_cpus);
  739. kfree(data->freq_table);
  740. kfree(data);
  741. }
  742. return 0;
  743. }
  744. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  745. {
  746. struct acpi_cpufreq_data *data = policy->driver_data;
  747. pr_debug("acpi_cpufreq_resume\n");
  748. data->resume = 1;
  749. return 0;
  750. }
  751. static struct freq_attr *acpi_cpufreq_attr[] = {
  752. &cpufreq_freq_attr_scaling_available_freqs,
  753. &freqdomain_cpus,
  754. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  755. &cpb,
  756. #endif
  757. NULL,
  758. };
  759. static struct cpufreq_driver acpi_cpufreq_driver = {
  760. .verify = cpufreq_generic_frequency_table_verify,
  761. .target_index = acpi_cpufreq_target,
  762. .fast_switch = acpi_cpufreq_fast_switch,
  763. .bios_limit = acpi_processor_get_bios_limit,
  764. .init = acpi_cpufreq_cpu_init,
  765. .exit = acpi_cpufreq_cpu_exit,
  766. .resume = acpi_cpufreq_resume,
  767. .name = "acpi-cpufreq",
  768. .attr = acpi_cpufreq_attr,
  769. };
  770. static void __init acpi_cpufreq_boost_init(void)
  771. {
  772. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  773. msrs = msrs_alloc();
  774. if (!msrs)
  775. return;
  776. acpi_cpufreq_driver.set_boost = set_boost;
  777. acpi_cpufreq_driver.boost_enabled = boost_state(0);
  778. cpu_notifier_register_begin();
  779. /* Force all MSRs to the same value */
  780. boost_set_msrs(acpi_cpufreq_driver.boost_enabled,
  781. cpu_online_mask);
  782. __register_cpu_notifier(&boost_nb);
  783. cpu_notifier_register_done();
  784. }
  785. }
  786. static void acpi_cpufreq_boost_exit(void)
  787. {
  788. if (msrs) {
  789. unregister_cpu_notifier(&boost_nb);
  790. msrs_free(msrs);
  791. msrs = NULL;
  792. }
  793. }
  794. static int __init acpi_cpufreq_init(void)
  795. {
  796. int ret;
  797. if (acpi_disabled)
  798. return -ENODEV;
  799. /* don't keep reloading if cpufreq_driver exists */
  800. if (cpufreq_get_current_driver())
  801. return -EEXIST;
  802. pr_debug("acpi_cpufreq_init\n");
  803. ret = acpi_cpufreq_early_init();
  804. if (ret)
  805. return ret;
  806. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  807. /* this is a sysfs file with a strange name and an even stranger
  808. * semantic - per CPU instantiation, but system global effect.
  809. * Lets enable it only on AMD CPUs for compatibility reasons and
  810. * only if configured. This is considered legacy code, which
  811. * will probably be removed at some point in the future.
  812. */
  813. if (!check_amd_hwpstate_cpu(0)) {
  814. struct freq_attr **attr;
  815. pr_debug("CPB unsupported, do not expose it\n");
  816. for (attr = acpi_cpufreq_attr; *attr; attr++)
  817. if (*attr == &cpb) {
  818. *attr = NULL;
  819. break;
  820. }
  821. }
  822. #endif
  823. acpi_cpufreq_boost_init();
  824. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  825. if (ret) {
  826. free_acpi_perf_data();
  827. acpi_cpufreq_boost_exit();
  828. }
  829. return ret;
  830. }
  831. static void __exit acpi_cpufreq_exit(void)
  832. {
  833. pr_debug("acpi_cpufreq_exit\n");
  834. acpi_cpufreq_boost_exit();
  835. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  836. free_acpi_perf_data();
  837. }
  838. module_param(acpi_pstate_strict, uint, 0644);
  839. MODULE_PARM_DESC(acpi_pstate_strict,
  840. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  841. "performed during frequency changes.");
  842. late_initcall(acpi_cpufreq_init);
  843. module_exit(acpi_cpufreq_exit);
  844. static const struct x86_cpu_id acpi_cpufreq_ids[] = {
  845. X86_FEATURE_MATCH(X86_FEATURE_ACPI),
  846. X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
  847. {}
  848. };
  849. MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
  850. static const struct acpi_device_id processor_device_ids[] = {
  851. {ACPI_PROCESSOR_OBJECT_HID, },
  852. {ACPI_PROCESSOR_DEVICE_HID, },
  853. {},
  854. };
  855. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  856. MODULE_ALIAS("acpi");