acpi-cpufreq.c 26 KB

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