acpi-cpufreq.c 25 KB

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