acpi-cpufreq.c 26 KB

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