aperfmperf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * x86 APERF/MPERF KHz calculation for
  4. * /sys/.../cpufreq/scaling_cur_freq
  5. *
  6. * Copyright (C) 2017 Intel Corp.
  7. * Author: Len Brown <len.brown@intel.com>
  8. */
  9. #include <linux/cpufreq.h>
  10. #include <linux/delay.h>
  11. #include <linux/ktime.h>
  12. #include <linux/math64.h>
  13. #include <linux/percpu.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/sched/isolation.h>
  16. #include <linux/sched/topology.h>
  17. #include <linux/smp.h>
  18. #include <linux/syscore_ops.h>
  19. #include <asm/cpu.h>
  20. #include <asm/cpu_device_id.h>
  21. #include <asm/intel-family.h>
  22. #include "cpu.h"
  23. struct aperfmperf {
  24. seqcount_t seq;
  25. unsigned long last_update;
  26. u64 acnt;
  27. u64 mcnt;
  28. u64 aperf;
  29. u64 mperf;
  30. };
  31. static DEFINE_PER_CPU_SHARED_ALIGNED(struct aperfmperf, cpu_samples) = {
  32. .seq = SEQCNT_ZERO(cpu_samples.seq)
  33. };
  34. static void init_counter_refs(void)
  35. {
  36. u64 aperf, mperf;
  37. rdmsrl(MSR_IA32_APERF, aperf);
  38. rdmsrl(MSR_IA32_MPERF, mperf);
  39. this_cpu_write(cpu_samples.aperf, aperf);
  40. this_cpu_write(cpu_samples.mperf, mperf);
  41. }
  42. #if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
  43. /*
  44. * APERF/MPERF frequency ratio computation.
  45. *
  46. * The scheduler wants to do frequency invariant accounting and needs a <1
  47. * ratio to account for the 'current' frequency, corresponding to
  48. * freq_curr / freq_max.
  49. *
  50. * Since the frequency freq_curr on x86 is controlled by micro-controller and
  51. * our P-state setting is little more than a request/hint, we need to observe
  52. * the effective frequency 'BusyMHz', i.e. the average frequency over a time
  53. * interval after discarding idle time. This is given by:
  54. *
  55. * BusyMHz = delta_APERF / delta_MPERF * freq_base
  56. *
  57. * where freq_base is the max non-turbo P-state.
  58. *
  59. * The freq_max term has to be set to a somewhat arbitrary value, because we
  60. * can't know which turbo states will be available at a given point in time:
  61. * it all depends on the thermal headroom of the entire package. We set it to
  62. * the turbo level with 4 cores active.
  63. *
  64. * Benchmarks show that's a good compromise between the 1C turbo ratio
  65. * (freq_curr/freq_max would rarely reach 1) and something close to freq_base,
  66. * which would ignore the entire turbo range (a conspicuous part, making
  67. * freq_curr/freq_max always maxed out).
  68. *
  69. * An exception to the heuristic above is the Atom uarch, where we choose the
  70. * highest turbo level for freq_max since Atom's are generally oriented towards
  71. * power efficiency.
  72. *
  73. * Setting freq_max to anything less than the 1C turbo ratio makes the ratio
  74. * freq_curr / freq_max to eventually grow >1, in which case we clip it to 1.
  75. */
  76. DEFINE_STATIC_KEY_FALSE(arch_scale_freq_key);
  77. static u64 arch_turbo_freq_ratio = SCHED_CAPACITY_SCALE;
  78. static u64 arch_max_freq_ratio = SCHED_CAPACITY_SCALE;
  79. void arch_set_max_freq_ratio(bool turbo_disabled)
  80. {
  81. arch_max_freq_ratio = turbo_disabled ? SCHED_CAPACITY_SCALE :
  82. arch_turbo_freq_ratio;
  83. }
  84. EXPORT_SYMBOL_GPL(arch_set_max_freq_ratio);
  85. static bool __init turbo_disabled(void)
  86. {
  87. u64 misc_en;
  88. int err;
  89. err = rdmsrl_safe(MSR_IA32_MISC_ENABLE, &misc_en);
  90. if (err)
  91. return false;
  92. return (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
  93. }
  94. static bool __init slv_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq)
  95. {
  96. int err;
  97. err = rdmsrl_safe(MSR_ATOM_CORE_RATIOS, base_freq);
  98. if (err)
  99. return false;
  100. err = rdmsrl_safe(MSR_ATOM_CORE_TURBO_RATIOS, turbo_freq);
  101. if (err)
  102. return false;
  103. *base_freq = (*base_freq >> 16) & 0x3F; /* max P state */
  104. *turbo_freq = *turbo_freq & 0x3F; /* 1C turbo */
  105. return true;
  106. }
  107. #define X86_MATCH(vfm) \
  108. X86_MATCH_VFM_FEATURE(vfm, X86_FEATURE_APERFMPERF, NULL)
  109. static const struct x86_cpu_id has_knl_turbo_ratio_limits[] __initconst = {
  110. X86_MATCH(INTEL_XEON_PHI_KNL),
  111. X86_MATCH(INTEL_XEON_PHI_KNM),
  112. {}
  113. };
  114. static const struct x86_cpu_id has_skx_turbo_ratio_limits[] __initconst = {
  115. X86_MATCH(INTEL_SKYLAKE_X),
  116. {}
  117. };
  118. static const struct x86_cpu_id has_glm_turbo_ratio_limits[] __initconst = {
  119. X86_MATCH(INTEL_ATOM_GOLDMONT),
  120. X86_MATCH(INTEL_ATOM_GOLDMONT_D),
  121. X86_MATCH(INTEL_ATOM_GOLDMONT_PLUS),
  122. {}
  123. };
  124. static bool __init knl_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq,
  125. int num_delta_fratio)
  126. {
  127. int fratio, delta_fratio, found;
  128. int err, i;
  129. u64 msr;
  130. err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
  131. if (err)
  132. return false;
  133. *base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
  134. err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &msr);
  135. if (err)
  136. return false;
  137. fratio = (msr >> 8) & 0xFF;
  138. i = 16;
  139. found = 0;
  140. do {
  141. if (found >= num_delta_fratio) {
  142. *turbo_freq = fratio;
  143. return true;
  144. }
  145. delta_fratio = (msr >> (i + 5)) & 0x7;
  146. if (delta_fratio) {
  147. found += 1;
  148. fratio -= delta_fratio;
  149. }
  150. i += 8;
  151. } while (i < 64);
  152. return true;
  153. }
  154. static bool __init skx_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq, int size)
  155. {
  156. u64 ratios, counts;
  157. u32 group_size;
  158. int err, i;
  159. err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
  160. if (err)
  161. return false;
  162. *base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
  163. err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &ratios);
  164. if (err)
  165. return false;
  166. err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT1, &counts);
  167. if (err)
  168. return false;
  169. for (i = 0; i < 64; i += 8) {
  170. group_size = (counts >> i) & 0xFF;
  171. if (group_size >= size) {
  172. *turbo_freq = (ratios >> i) & 0xFF;
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. static bool __init core_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq)
  179. {
  180. u64 msr;
  181. int err;
  182. err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
  183. if (err)
  184. return false;
  185. err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &msr);
  186. if (err)
  187. return false;
  188. *base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
  189. *turbo_freq = (msr >> 24) & 0xFF; /* 4C turbo */
  190. /* The CPU may have less than 4 cores */
  191. if (!*turbo_freq)
  192. *turbo_freq = msr & 0xFF; /* 1C turbo */
  193. return true;
  194. }
  195. static bool __init intel_set_max_freq_ratio(void)
  196. {
  197. u64 base_freq, turbo_freq;
  198. u64 turbo_ratio;
  199. if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
  200. goto out;
  201. if (x86_match_cpu(has_glm_turbo_ratio_limits) &&
  202. skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
  203. goto out;
  204. if (x86_match_cpu(has_knl_turbo_ratio_limits) &&
  205. knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
  206. goto out;
  207. if (x86_match_cpu(has_skx_turbo_ratio_limits) &&
  208. skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4))
  209. goto out;
  210. if (core_set_max_freq_ratio(&base_freq, &turbo_freq))
  211. goto out;
  212. return false;
  213. out:
  214. /*
  215. * Some hypervisors advertise X86_FEATURE_APERFMPERF
  216. * but then fill all MSR's with zeroes.
  217. * Some CPUs have turbo boost but don't declare any turbo ratio
  218. * in MSR_TURBO_RATIO_LIMIT.
  219. */
  220. if (!base_freq || !turbo_freq) {
  221. pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
  222. return false;
  223. }
  224. turbo_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE, base_freq);
  225. if (!turbo_ratio) {
  226. pr_debug("Non-zero turbo and base frequencies led to a 0 ratio.\n");
  227. return false;
  228. }
  229. arch_turbo_freq_ratio = turbo_ratio;
  230. arch_set_max_freq_ratio(turbo_disabled());
  231. return true;
  232. }
  233. #ifdef CONFIG_PM_SLEEP
  234. static struct syscore_ops freq_invariance_syscore_ops = {
  235. .resume = init_counter_refs,
  236. };
  237. static void register_freq_invariance_syscore_ops(void)
  238. {
  239. register_syscore_ops(&freq_invariance_syscore_ops);
  240. }
  241. #else
  242. static inline void register_freq_invariance_syscore_ops(void) {}
  243. #endif
  244. static void freq_invariance_enable(void)
  245. {
  246. if (static_branch_unlikely(&arch_scale_freq_key)) {
  247. WARN_ON_ONCE(1);
  248. return;
  249. }
  250. static_branch_enable_cpuslocked(&arch_scale_freq_key);
  251. register_freq_invariance_syscore_ops();
  252. pr_info("Estimated ratio of average max frequency by base frequency (times 1024): %llu\n", arch_max_freq_ratio);
  253. }
  254. void freq_invariance_set_perf_ratio(u64 ratio, bool turbo_disabled)
  255. {
  256. arch_turbo_freq_ratio = ratio;
  257. arch_set_max_freq_ratio(turbo_disabled);
  258. freq_invariance_enable();
  259. }
  260. static void __init bp_init_freq_invariance(void)
  261. {
  262. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  263. return;
  264. if (intel_set_max_freq_ratio()) {
  265. guard(cpus_read_lock)();
  266. freq_invariance_enable();
  267. }
  268. }
  269. static void disable_freq_invariance_workfn(struct work_struct *work)
  270. {
  271. int cpu;
  272. static_branch_disable(&arch_scale_freq_key);
  273. /*
  274. * Set arch_freq_scale to a default value on all cpus
  275. * This negates the effect of scaling
  276. */
  277. for_each_possible_cpu(cpu)
  278. per_cpu(arch_freq_scale, cpu) = SCHED_CAPACITY_SCALE;
  279. }
  280. static DECLARE_WORK(disable_freq_invariance_work,
  281. disable_freq_invariance_workfn);
  282. DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
  283. EXPORT_PER_CPU_SYMBOL_GPL(arch_freq_scale);
  284. static DEFINE_STATIC_KEY_FALSE(arch_hybrid_cap_scale_key);
  285. struct arch_hybrid_cpu_scale {
  286. unsigned long capacity;
  287. unsigned long freq_ratio;
  288. };
  289. static struct arch_hybrid_cpu_scale __percpu *arch_cpu_scale;
  290. /**
  291. * arch_enable_hybrid_capacity_scale() - Enable hybrid CPU capacity scaling
  292. *
  293. * Allocate memory for per-CPU data used by hybrid CPU capacity scaling,
  294. * initialize it and set the static key controlling its code paths.
  295. *
  296. * Must be called before arch_set_cpu_capacity().
  297. */
  298. bool arch_enable_hybrid_capacity_scale(void)
  299. {
  300. int cpu;
  301. if (static_branch_unlikely(&arch_hybrid_cap_scale_key)) {
  302. WARN_ONCE(1, "Hybrid CPU capacity scaling already enabled");
  303. return true;
  304. }
  305. arch_cpu_scale = alloc_percpu(struct arch_hybrid_cpu_scale);
  306. if (!arch_cpu_scale)
  307. return false;
  308. for_each_possible_cpu(cpu) {
  309. per_cpu_ptr(arch_cpu_scale, cpu)->capacity = SCHED_CAPACITY_SCALE;
  310. per_cpu_ptr(arch_cpu_scale, cpu)->freq_ratio = arch_max_freq_ratio;
  311. }
  312. static_branch_enable(&arch_hybrid_cap_scale_key);
  313. pr_info("Hybrid CPU capacity scaling enabled\n");
  314. return true;
  315. }
  316. /**
  317. * arch_set_cpu_capacity() - Set scale-invariance parameters for a CPU
  318. * @cpu: Target CPU.
  319. * @cap: Capacity of @cpu at its maximum frequency, relative to @max_cap.
  320. * @max_cap: System-wide maximum CPU capacity.
  321. * @cap_freq: Frequency of @cpu corresponding to @cap.
  322. * @base_freq: Frequency of @cpu at which MPERF counts.
  323. *
  324. * The units in which @cap and @max_cap are expressed do not matter, so long
  325. * as they are consistent, because the former is effectively divided by the
  326. * latter. Analogously for @cap_freq and @base_freq.
  327. *
  328. * After calling this function for all CPUs, call arch_rebuild_sched_domains()
  329. * to let the scheduler know that capacity-aware scheduling can be used going
  330. * forward.
  331. */
  332. void arch_set_cpu_capacity(int cpu, unsigned long cap, unsigned long max_cap,
  333. unsigned long cap_freq, unsigned long base_freq)
  334. {
  335. if (static_branch_likely(&arch_hybrid_cap_scale_key)) {
  336. WRITE_ONCE(per_cpu_ptr(arch_cpu_scale, cpu)->capacity,
  337. div_u64(cap << SCHED_CAPACITY_SHIFT, max_cap));
  338. WRITE_ONCE(per_cpu_ptr(arch_cpu_scale, cpu)->freq_ratio,
  339. div_u64(cap_freq << SCHED_CAPACITY_SHIFT, base_freq));
  340. } else {
  341. WARN_ONCE(1, "Hybrid CPU capacity scaling not enabled");
  342. }
  343. }
  344. unsigned long arch_scale_cpu_capacity(int cpu)
  345. {
  346. if (static_branch_unlikely(&arch_hybrid_cap_scale_key))
  347. return READ_ONCE(per_cpu_ptr(arch_cpu_scale, cpu)->capacity);
  348. return SCHED_CAPACITY_SCALE;
  349. }
  350. EXPORT_SYMBOL_GPL(arch_scale_cpu_capacity);
  351. static void scale_freq_tick(u64 acnt, u64 mcnt)
  352. {
  353. u64 freq_scale, freq_ratio;
  354. if (!arch_scale_freq_invariant())
  355. return;
  356. if (check_shl_overflow(acnt, 2*SCHED_CAPACITY_SHIFT, &acnt))
  357. goto error;
  358. if (static_branch_unlikely(&arch_hybrid_cap_scale_key))
  359. freq_ratio = READ_ONCE(this_cpu_ptr(arch_cpu_scale)->freq_ratio);
  360. else
  361. freq_ratio = arch_max_freq_ratio;
  362. if (check_mul_overflow(mcnt, freq_ratio, &mcnt) || !mcnt)
  363. goto error;
  364. freq_scale = div64_u64(acnt, mcnt);
  365. if (!freq_scale)
  366. goto error;
  367. if (freq_scale > SCHED_CAPACITY_SCALE)
  368. freq_scale = SCHED_CAPACITY_SCALE;
  369. this_cpu_write(arch_freq_scale, freq_scale);
  370. return;
  371. error:
  372. pr_warn("Scheduler frequency invariance went wobbly, disabling!\n");
  373. schedule_work(&disable_freq_invariance_work);
  374. }
  375. #else
  376. static inline void bp_init_freq_invariance(void) { }
  377. static inline void scale_freq_tick(u64 acnt, u64 mcnt) { }
  378. #endif /* CONFIG_X86_64 && CONFIG_SMP */
  379. void arch_scale_freq_tick(void)
  380. {
  381. struct aperfmperf *s = this_cpu_ptr(&cpu_samples);
  382. u64 acnt, mcnt, aperf, mperf;
  383. if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
  384. return;
  385. rdmsrl(MSR_IA32_APERF, aperf);
  386. rdmsrl(MSR_IA32_MPERF, mperf);
  387. acnt = aperf - s->aperf;
  388. mcnt = mperf - s->mperf;
  389. s->aperf = aperf;
  390. s->mperf = mperf;
  391. raw_write_seqcount_begin(&s->seq);
  392. s->last_update = jiffies;
  393. s->acnt = acnt;
  394. s->mcnt = mcnt;
  395. raw_write_seqcount_end(&s->seq);
  396. scale_freq_tick(acnt, mcnt);
  397. }
  398. /*
  399. * Discard samples older than the define maximum sample age of 20ms. There
  400. * is no point in sending IPIs in such a case. If the scheduler tick was
  401. * not running then the CPU is either idle or isolated.
  402. */
  403. #define MAX_SAMPLE_AGE ((unsigned long)HZ / 50)
  404. unsigned int arch_freq_get_on_cpu(int cpu)
  405. {
  406. struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu);
  407. unsigned int seq, freq;
  408. unsigned long last;
  409. u64 acnt, mcnt;
  410. if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
  411. goto fallback;
  412. do {
  413. seq = raw_read_seqcount_begin(&s->seq);
  414. last = s->last_update;
  415. acnt = s->acnt;
  416. mcnt = s->mcnt;
  417. } while (read_seqcount_retry(&s->seq, seq));
  418. /*
  419. * Bail on invalid count and when the last update was too long ago,
  420. * which covers idle and NOHZ full CPUs.
  421. */
  422. if (!mcnt || (jiffies - last) > MAX_SAMPLE_AGE)
  423. goto fallback;
  424. return div64_u64((cpu_khz * acnt), mcnt);
  425. fallback:
  426. freq = cpufreq_quick_get(cpu);
  427. return freq ? freq : cpu_khz;
  428. }
  429. static int __init bp_init_aperfmperf(void)
  430. {
  431. if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
  432. return 0;
  433. init_counter_refs();
  434. bp_init_freq_invariance();
  435. return 0;
  436. }
  437. early_initcall(bp_init_aperfmperf);
  438. void ap_init_aperfmperf(void)
  439. {
  440. if (cpu_feature_enabled(X86_FEATURE_APERFMPERF))
  441. init_counter_refs();
  442. }