cppc_cpufreq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPPC (Collaborative Processor Performance Control) driver for
  4. * interfacing with the CPUfreq layer and governors. See
  5. * cppc_acpi.c for CPPC specific methods.
  6. *
  7. * (C) Copyright 2014, 2015 Linaro Ltd.
  8. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  9. */
  10. #define pr_fmt(fmt) "CPPC Cpufreq:" fmt
  11. #include <linux/arch_topology.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/delay.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/irq_work.h>
  18. #include <linux/kthread.h>
  19. #include <linux/time.h>
  20. #include <linux/vmalloc.h>
  21. #include <uapi/linux/sched/types.h>
  22. #include <linux/unaligned.h>
  23. #include <acpi/cppc_acpi.h>
  24. static struct cpufreq_driver cppc_cpufreq_driver;
  25. #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
  26. static enum {
  27. FIE_UNSET = -1,
  28. FIE_ENABLED,
  29. FIE_DISABLED
  30. } fie_disabled = FIE_UNSET;
  31. module_param(fie_disabled, int, 0444);
  32. MODULE_PARM_DESC(fie_disabled, "Disable Frequency Invariance Engine (FIE)");
  33. /* Frequency invariance support */
  34. struct cppc_freq_invariance {
  35. int cpu;
  36. struct irq_work irq_work;
  37. struct kthread_work work;
  38. struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
  39. struct cppc_cpudata *cpu_data;
  40. };
  41. static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
  42. static struct kthread_worker *kworker_fie;
  43. static int cppc_perf_from_fbctrs(struct cppc_perf_fb_ctrs *fb_ctrs_t0,
  44. struct cppc_perf_fb_ctrs *fb_ctrs_t1);
  45. /**
  46. * __cppc_scale_freq_tick - CPPC arch_freq_scale updater for frequency invariance
  47. * @cppc_fi: per-cpu CPPC FIE data.
  48. *
  49. * The CPPC driver registers itself with the topology core to provide its own
  50. * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
  51. * gets called by the scheduler on every tick.
  52. *
  53. * Note that the arch specific counters have higher priority than CPPC counters,
  54. * if available, though the CPPC driver doesn't need to have any special
  55. * handling for that.
  56. */
  57. static void __cppc_scale_freq_tick(struct cppc_freq_invariance *cppc_fi)
  58. {
  59. struct cppc_perf_fb_ctrs fb_ctrs = {0};
  60. struct cppc_cpudata *cpu_data;
  61. unsigned long local_freq_scale;
  62. u64 perf;
  63. cpu_data = cppc_fi->cpu_data;
  64. if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) {
  65. pr_warn("%s: failed to read perf counters\n", __func__);
  66. return;
  67. }
  68. perf = cppc_perf_from_fbctrs(&cppc_fi->prev_perf_fb_ctrs, &fb_ctrs);
  69. if (!perf)
  70. return;
  71. cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
  72. perf <<= SCHED_CAPACITY_SHIFT;
  73. local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
  74. /* This can happen due to counter's overflow */
  75. if (unlikely(local_freq_scale > 1024))
  76. local_freq_scale = 1024;
  77. per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
  78. }
  79. static void cppc_scale_freq_tick(void)
  80. {
  81. __cppc_scale_freq_tick(&per_cpu(cppc_freq_inv, smp_processor_id()));
  82. }
  83. static struct scale_freq_data cppc_sftd = {
  84. .source = SCALE_FREQ_SOURCE_CPPC,
  85. .set_freq_scale = cppc_scale_freq_tick,
  86. };
  87. static void cppc_scale_freq_workfn(struct kthread_work *work)
  88. {
  89. struct cppc_freq_invariance *cppc_fi;
  90. cppc_fi = container_of(work, struct cppc_freq_invariance, work);
  91. __cppc_scale_freq_tick(cppc_fi);
  92. }
  93. static void cppc_irq_work(struct irq_work *irq_work)
  94. {
  95. struct cppc_freq_invariance *cppc_fi;
  96. cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
  97. kthread_queue_work(kworker_fie, &cppc_fi->work);
  98. }
  99. /*
  100. * Reading perf counters may sleep if the CPC regs are in PCC. Thus, we
  101. * schedule an irq work in scale_freq_tick (since we reach here from hard-irq
  102. * context), which then schedules a normal work item cppc_scale_freq_workfn()
  103. * that updates the per_cpu arch_freq_scale variable based on the counter
  104. * updates since the last tick.
  105. */
  106. static void cppc_scale_freq_tick_pcc(void)
  107. {
  108. struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
  109. /*
  110. * cppc_get_perf_ctrs() can potentially sleep, call that from the right
  111. * context.
  112. */
  113. irq_work_queue(&cppc_fi->irq_work);
  114. }
  115. static struct scale_freq_data cppc_sftd_pcc = {
  116. .source = SCALE_FREQ_SOURCE_CPPC,
  117. .set_freq_scale = cppc_scale_freq_tick_pcc,
  118. };
  119. static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
  120. {
  121. struct scale_freq_data *sftd = &cppc_sftd;
  122. struct cppc_freq_invariance *cppc_fi;
  123. int cpu, ret;
  124. if (fie_disabled)
  125. return;
  126. for_each_cpu(cpu, policy->cpus) {
  127. cppc_fi = &per_cpu(cppc_freq_inv, cpu);
  128. cppc_fi->cpu = cpu;
  129. cppc_fi->cpu_data = policy->driver_data;
  130. if (cppc_perf_ctrs_in_pcc_cpu(cpu)) {
  131. kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
  132. init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
  133. sftd = &cppc_sftd_pcc;
  134. }
  135. ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
  136. /*
  137. * Don't abort as the CPU was offline while the driver was
  138. * getting registered.
  139. */
  140. if (ret && cpu_online(cpu)) {
  141. pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
  142. __func__, cpu, ret);
  143. return;
  144. }
  145. }
  146. /* Register for freq-invariance */
  147. topology_set_scale_freq_source(sftd, policy->cpus);
  148. }
  149. /*
  150. * We free all the resources on policy's removal and not on CPU removal as the
  151. * irq-work are per-cpu and the hotplug core takes care of flushing the pending
  152. * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
  153. * fires on another CPU after the concerned CPU is removed, it won't harm.
  154. *
  155. * We just need to make sure to remove them all on policy->exit().
  156. */
  157. static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
  158. {
  159. struct cppc_freq_invariance *cppc_fi;
  160. int cpu;
  161. if (fie_disabled)
  162. return;
  163. /* policy->cpus will be empty here, use related_cpus instead */
  164. topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
  165. for_each_cpu(cpu, policy->related_cpus) {
  166. if (!cppc_perf_ctrs_in_pcc_cpu(cpu))
  167. continue;
  168. cppc_fi = &per_cpu(cppc_freq_inv, cpu);
  169. irq_work_sync(&cppc_fi->irq_work);
  170. kthread_cancel_work_sync(&cppc_fi->work);
  171. }
  172. }
  173. static void cppc_fie_kworker_init(void)
  174. {
  175. struct sched_attr attr = {
  176. .size = sizeof(struct sched_attr),
  177. .sched_policy = SCHED_DEADLINE,
  178. .sched_nice = 0,
  179. .sched_priority = 0,
  180. /*
  181. * Fake (unused) bandwidth; workaround to "fix"
  182. * priority inheritance.
  183. */
  184. .sched_runtime = NSEC_PER_MSEC,
  185. .sched_deadline = 10 * NSEC_PER_MSEC,
  186. .sched_period = 10 * NSEC_PER_MSEC,
  187. };
  188. int ret;
  189. kworker_fie = kthread_run_worker(0, "cppc_fie");
  190. if (IS_ERR(kworker_fie)) {
  191. pr_warn("%s: failed to create kworker_fie: %ld\n", __func__,
  192. PTR_ERR(kworker_fie));
  193. fie_disabled = FIE_DISABLED;
  194. kworker_fie = NULL;
  195. return;
  196. }
  197. ret = sched_setattr_nocheck(kworker_fie->task, &attr);
  198. if (ret) {
  199. pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
  200. ret);
  201. kthread_destroy_worker(kworker_fie);
  202. fie_disabled = FIE_DISABLED;
  203. kworker_fie = NULL;
  204. }
  205. }
  206. static void __init cppc_freq_invariance_init(void)
  207. {
  208. bool perf_ctrs_in_pcc = cppc_perf_ctrs_in_pcc();
  209. if (fie_disabled == FIE_UNSET) {
  210. if (perf_ctrs_in_pcc) {
  211. pr_info("FIE not enabled on systems with registers in PCC\n");
  212. fie_disabled = FIE_DISABLED;
  213. } else {
  214. fie_disabled = FIE_ENABLED;
  215. }
  216. }
  217. if (fie_disabled || !perf_ctrs_in_pcc)
  218. return;
  219. cppc_fie_kworker_init();
  220. }
  221. static void cppc_freq_invariance_exit(void)
  222. {
  223. if (kworker_fie)
  224. kthread_destroy_worker(kworker_fie);
  225. }
  226. #else
  227. static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
  228. {
  229. }
  230. static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
  231. {
  232. }
  233. static inline void cppc_freq_invariance_init(void)
  234. {
  235. }
  236. static inline void cppc_freq_invariance_exit(void)
  237. {
  238. }
  239. #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
  240. static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
  241. unsigned int target_freq,
  242. unsigned int relation)
  243. {
  244. struct cppc_cpudata *cpu_data = policy->driver_data;
  245. unsigned int cpu = policy->cpu;
  246. struct cpufreq_freqs freqs;
  247. int ret = 0;
  248. cpu_data->perf_ctrls.desired_perf =
  249. cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
  250. freqs.old = policy->cur;
  251. freqs.new = target_freq;
  252. cpufreq_freq_transition_begin(policy, &freqs);
  253. ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
  254. cpufreq_freq_transition_end(policy, &freqs, ret != 0);
  255. if (ret)
  256. pr_debug("Failed to set target on CPU:%d. ret:%d\n",
  257. cpu, ret);
  258. return ret;
  259. }
  260. static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy,
  261. unsigned int target_freq)
  262. {
  263. struct cppc_cpudata *cpu_data = policy->driver_data;
  264. unsigned int cpu = policy->cpu;
  265. u32 desired_perf;
  266. int ret;
  267. desired_perf = cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
  268. cpu_data->perf_ctrls.desired_perf = desired_perf;
  269. ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
  270. if (ret) {
  271. pr_debug("Failed to set target on CPU:%d. ret:%d\n",
  272. cpu, ret);
  273. return 0;
  274. }
  275. return target_freq;
  276. }
  277. static int cppc_verify_policy(struct cpufreq_policy_data *policy)
  278. {
  279. cpufreq_verify_within_cpu_limits(policy);
  280. return 0;
  281. }
  282. static unsigned int __cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
  283. {
  284. int transition_latency_ns = cppc_get_transition_latency(cpu);
  285. if (transition_latency_ns < 0)
  286. return CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS / NSEC_PER_USEC;
  287. return transition_latency_ns / NSEC_PER_USEC;
  288. }
  289. /*
  290. * The PCC subspace describes the rate at which platform can accept commands
  291. * on the shared PCC channel (including READs which do not count towards freq
  292. * transition requests), so ideally we need to use the PCC values as a fallback
  293. * if we don't have a platform specific transition_delay_us
  294. */
  295. #ifdef CONFIG_ARM64
  296. #include <asm/cputype.h>
  297. static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
  298. {
  299. unsigned long implementor = read_cpuid_implementor();
  300. unsigned long part_num = read_cpuid_part_number();
  301. switch (implementor) {
  302. case ARM_CPU_IMP_QCOM:
  303. switch (part_num) {
  304. case QCOM_CPU_PART_FALKOR_V1:
  305. case QCOM_CPU_PART_FALKOR:
  306. return 10000;
  307. }
  308. }
  309. return __cppc_cpufreq_get_transition_delay_us(cpu);
  310. }
  311. #else
  312. static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
  313. {
  314. return __cppc_cpufreq_get_transition_delay_us(cpu);
  315. }
  316. #endif
  317. #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
  318. static DEFINE_PER_CPU(unsigned int, efficiency_class);
  319. /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
  320. #define CPPC_EM_CAP_STEP (20)
  321. /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
  322. #define CPPC_EM_COST_STEP (1)
  323. /* Add a cost gap correspnding to the energy of 4 CPUs. */
  324. #define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
  325. / CPPC_EM_CAP_STEP)
  326. static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
  327. {
  328. struct cppc_perf_caps *perf_caps;
  329. unsigned int min_cap, max_cap;
  330. struct cppc_cpudata *cpu_data;
  331. int cpu = policy->cpu;
  332. cpu_data = policy->driver_data;
  333. perf_caps = &cpu_data->perf_caps;
  334. max_cap = arch_scale_cpu_capacity(cpu);
  335. min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
  336. perf_caps->highest_perf);
  337. if ((min_cap == 0) || (max_cap < min_cap))
  338. return 0;
  339. return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
  340. }
  341. /*
  342. * The cost is defined as:
  343. * cost = power * max_frequency / frequency
  344. */
  345. static inline unsigned long compute_cost(int cpu, int step)
  346. {
  347. return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) +
  348. step * CPPC_EM_COST_STEP;
  349. }
  350. static int cppc_get_cpu_power(struct device *cpu_dev,
  351. unsigned long *power, unsigned long *KHz)
  352. {
  353. unsigned long perf_step, perf_prev, perf, perf_check;
  354. unsigned int min_step, max_step, step, step_check;
  355. unsigned long prev_freq = *KHz;
  356. unsigned int min_cap, max_cap;
  357. struct cpufreq_policy *policy;
  358. struct cppc_perf_caps *perf_caps;
  359. struct cppc_cpudata *cpu_data;
  360. policy = cpufreq_cpu_get_raw(cpu_dev->id);
  361. if (!policy)
  362. return -EINVAL;
  363. cpu_data = policy->driver_data;
  364. perf_caps = &cpu_data->perf_caps;
  365. max_cap = arch_scale_cpu_capacity(cpu_dev->id);
  366. min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
  367. perf_caps->highest_perf);
  368. perf_step = div_u64((u64)CPPC_EM_CAP_STEP * perf_caps->highest_perf,
  369. max_cap);
  370. min_step = min_cap / CPPC_EM_CAP_STEP;
  371. max_step = max_cap / CPPC_EM_CAP_STEP;
  372. perf_prev = cppc_khz_to_perf(perf_caps, *KHz);
  373. step = perf_prev / perf_step;
  374. if (step > max_step)
  375. return -EINVAL;
  376. if (min_step == max_step) {
  377. step = max_step;
  378. perf = perf_caps->highest_perf;
  379. } else if (step < min_step) {
  380. step = min_step;
  381. perf = perf_caps->lowest_perf;
  382. } else {
  383. step++;
  384. if (step == max_step)
  385. perf = perf_caps->highest_perf;
  386. else
  387. perf = step * perf_step;
  388. }
  389. *KHz = cppc_perf_to_khz(perf_caps, perf);
  390. perf_check = cppc_khz_to_perf(perf_caps, *KHz);
  391. step_check = perf_check / perf_step;
  392. /*
  393. * To avoid bad integer approximation, check that new frequency value
  394. * increased and that the new frequency will be converted to the
  395. * desired step value.
  396. */
  397. while ((*KHz == prev_freq) || (step_check != step)) {
  398. perf++;
  399. *KHz = cppc_perf_to_khz(perf_caps, perf);
  400. perf_check = cppc_khz_to_perf(perf_caps, *KHz);
  401. step_check = perf_check / perf_step;
  402. }
  403. /*
  404. * With an artificial EM, only the cost value is used. Still the power
  405. * is populated such as 0 < power < EM_MAX_POWER. This allows to add
  406. * more sense to the artificial performance states.
  407. */
  408. *power = compute_cost(cpu_dev->id, step);
  409. return 0;
  410. }
  411. static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
  412. unsigned long *cost)
  413. {
  414. unsigned long perf_step, perf_prev;
  415. struct cppc_perf_caps *perf_caps;
  416. struct cpufreq_policy *policy;
  417. struct cppc_cpudata *cpu_data;
  418. unsigned int max_cap;
  419. int step;
  420. policy = cpufreq_cpu_get_raw(cpu_dev->id);
  421. if (!policy)
  422. return -EINVAL;
  423. cpu_data = policy->driver_data;
  424. perf_caps = &cpu_data->perf_caps;
  425. max_cap = arch_scale_cpu_capacity(cpu_dev->id);
  426. perf_prev = cppc_khz_to_perf(perf_caps, KHz);
  427. perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
  428. step = perf_prev / perf_step;
  429. *cost = compute_cost(cpu_dev->id, step);
  430. return 0;
  431. }
  432. static void cppc_cpufreq_register_em(struct cpufreq_policy *policy)
  433. {
  434. struct cppc_cpudata *cpu_data;
  435. struct em_data_callback em_cb =
  436. EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost);
  437. cpu_data = policy->driver_data;
  438. em_dev_register_perf_domain(get_cpu_device(policy->cpu),
  439. get_perf_level_count(policy), &em_cb,
  440. cpu_data->shared_cpu_map, 0);
  441. }
  442. static void populate_efficiency_class(void)
  443. {
  444. struct acpi_madt_generic_interrupt *gicc;
  445. DECLARE_BITMAP(used_classes, 256) = {};
  446. int class, cpu, index;
  447. for_each_possible_cpu(cpu) {
  448. gicc = acpi_cpu_get_madt_gicc(cpu);
  449. class = gicc->efficiency_class;
  450. bitmap_set(used_classes, class, 1);
  451. }
  452. if (bitmap_weight(used_classes, 256) <= 1) {
  453. pr_debug("Efficiency classes are all equal (=%d). "
  454. "No EM registered", class);
  455. return;
  456. }
  457. /*
  458. * Squeeze efficiency class values on [0:#efficiency_class-1].
  459. * Values are per spec in [0:255].
  460. */
  461. index = 0;
  462. for_each_set_bit(class, used_classes, 256) {
  463. for_each_possible_cpu(cpu) {
  464. gicc = acpi_cpu_get_madt_gicc(cpu);
  465. if (gicc->efficiency_class == class)
  466. per_cpu(efficiency_class, cpu) = index;
  467. }
  468. index++;
  469. }
  470. cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em;
  471. }
  472. #else
  473. static void populate_efficiency_class(void)
  474. {
  475. }
  476. #endif
  477. static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
  478. {
  479. struct cppc_cpudata *cpu_data;
  480. int ret;
  481. cpu_data = kzalloc_obj(struct cppc_cpudata);
  482. if (!cpu_data)
  483. goto out;
  484. if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
  485. goto free_cpu;
  486. ret = acpi_get_psd_map(cpu, cpu_data);
  487. if (ret) {
  488. pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
  489. goto free_mask;
  490. }
  491. ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
  492. if (ret) {
  493. pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
  494. goto free_mask;
  495. }
  496. return cpu_data;
  497. free_mask:
  498. free_cpumask_var(cpu_data->shared_cpu_map);
  499. free_cpu:
  500. kfree(cpu_data);
  501. out:
  502. return NULL;
  503. }
  504. static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
  505. {
  506. struct cppc_cpudata *cpu_data = policy->driver_data;
  507. free_cpumask_var(cpu_data->shared_cpu_map);
  508. kfree(cpu_data);
  509. policy->driver_data = NULL;
  510. }
  511. static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
  512. {
  513. unsigned int cpu = policy->cpu;
  514. struct cppc_cpudata *cpu_data;
  515. struct cppc_perf_caps *caps;
  516. int ret;
  517. cpu_data = cppc_cpufreq_get_cpu_data(cpu);
  518. if (!cpu_data) {
  519. pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
  520. return -ENODEV;
  521. }
  522. caps = &cpu_data->perf_caps;
  523. policy->driver_data = cpu_data;
  524. /*
  525. * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
  526. * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
  527. */
  528. policy->min = cppc_perf_to_khz(caps, caps->lowest_nonlinear_perf);
  529. policy->max = cppc_perf_to_khz(caps, policy->boost_enabled ?
  530. caps->highest_perf : caps->nominal_perf);
  531. /*
  532. * Set cpuinfo.min_freq to Lowest to make the full range of performance
  533. * available if userspace wants to use any perf between lowest & lowest
  534. * nonlinear perf
  535. */
  536. policy->cpuinfo.min_freq = cppc_perf_to_khz(caps, caps->lowest_perf);
  537. policy->cpuinfo.max_freq = policy->max;
  538. policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
  539. policy->shared_type = cpu_data->shared_type;
  540. switch (policy->shared_type) {
  541. case CPUFREQ_SHARED_TYPE_HW:
  542. case CPUFREQ_SHARED_TYPE_NONE:
  543. /* Nothing to be done - we'll have a policy for each CPU */
  544. break;
  545. case CPUFREQ_SHARED_TYPE_ANY:
  546. /*
  547. * All CPUs in the domain will share a policy and all cpufreq
  548. * operations will use a single cppc_cpudata structure stored
  549. * in policy->driver_data.
  550. */
  551. cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
  552. break;
  553. default:
  554. pr_debug("Unsupported CPU co-ord type: %d\n",
  555. policy->shared_type);
  556. ret = -EFAULT;
  557. goto out;
  558. }
  559. policy->fast_switch_possible = cppc_allow_fast_switch();
  560. policy->dvfs_possible_from_any_cpu = true;
  561. /*
  562. * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
  563. * is supported.
  564. */
  565. if (caps->highest_perf > caps->nominal_perf)
  566. policy->boost_supported = true;
  567. /* Set policy->cur to max now. The governors will adjust later. */
  568. policy->cur = cppc_perf_to_khz(caps, caps->highest_perf);
  569. cpu_data->perf_ctrls.desired_perf = caps->highest_perf;
  570. ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
  571. if (ret) {
  572. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  573. caps->highest_perf, cpu, ret);
  574. goto out;
  575. }
  576. cppc_cpufreq_cpu_fie_init(policy);
  577. return 0;
  578. out:
  579. cppc_cpufreq_put_cpu_data(policy);
  580. return ret;
  581. }
  582. static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  583. {
  584. struct cppc_cpudata *cpu_data = policy->driver_data;
  585. struct cppc_perf_caps *caps = &cpu_data->perf_caps;
  586. unsigned int cpu = policy->cpu;
  587. int ret;
  588. cppc_cpufreq_cpu_fie_exit(policy);
  589. cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
  590. ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
  591. if (ret)
  592. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  593. caps->lowest_perf, cpu, ret);
  594. cppc_cpufreq_put_cpu_data(policy);
  595. }
  596. static inline u64 get_delta(u64 t1, u64 t0)
  597. {
  598. if (t1 > t0 || t0 > ~(u32)0)
  599. return t1 - t0;
  600. return (u32)t1 - (u32)t0;
  601. }
  602. static int cppc_perf_from_fbctrs(struct cppc_perf_fb_ctrs *fb_ctrs_t0,
  603. struct cppc_perf_fb_ctrs *fb_ctrs_t1)
  604. {
  605. u64 delta_reference, delta_delivered;
  606. u64 reference_perf;
  607. reference_perf = fb_ctrs_t0->reference_perf;
  608. delta_reference = get_delta(fb_ctrs_t1->reference,
  609. fb_ctrs_t0->reference);
  610. delta_delivered = get_delta(fb_ctrs_t1->delivered,
  611. fb_ctrs_t0->delivered);
  612. /*
  613. * Avoid divide-by zero and unchanged feedback counters.
  614. * Leave it for callers to handle.
  615. */
  616. if (!delta_reference || !delta_delivered)
  617. return 0;
  618. return (reference_perf * delta_delivered) / delta_reference;
  619. }
  620. static int cppc_get_perf_ctrs_sample(int cpu,
  621. struct cppc_perf_fb_ctrs *fb_ctrs_t0,
  622. struct cppc_perf_fb_ctrs *fb_ctrs_t1)
  623. {
  624. int ret;
  625. ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0);
  626. if (ret)
  627. return ret;
  628. udelay(2); /* 2usec delay between sampling */
  629. return cppc_get_perf_ctrs(cpu, fb_ctrs_t1);
  630. }
  631. static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
  632. {
  633. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  634. struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
  635. struct cppc_cpudata *cpu_data;
  636. u64 delivered_perf;
  637. int ret;
  638. if (!policy)
  639. return 0;
  640. cpu_data = policy->driver_data;
  641. ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1);
  642. if (ret) {
  643. if (ret == -EFAULT)
  644. /* Any of the associated CPPC regs is 0. */
  645. goto out_invalid_counters;
  646. else
  647. return 0;
  648. }
  649. delivered_perf = cppc_perf_from_fbctrs(&fb_ctrs_t0, &fb_ctrs_t1);
  650. if (!delivered_perf)
  651. goto out_invalid_counters;
  652. return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
  653. out_invalid_counters:
  654. /*
  655. * Feedback counters could be unchanged or 0 when a cpu enters a
  656. * low-power idle state, e.g. clock-gated or power-gated.
  657. * Use desired perf for reflecting frequency. Get the latest register
  658. * value first as some platforms may update the actual delivered perf
  659. * there; if failed, resort to the cached desired perf.
  660. */
  661. if (cppc_get_desired_perf(cpu, &delivered_perf))
  662. delivered_perf = cpu_data->perf_ctrls.desired_perf;
  663. return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
  664. }
  665. static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
  666. {
  667. struct cppc_cpudata *cpu_data = policy->driver_data;
  668. struct cppc_perf_caps *caps = &cpu_data->perf_caps;
  669. int ret;
  670. if (state)
  671. policy->max = cppc_perf_to_khz(caps, caps->highest_perf);
  672. else
  673. policy->max = cppc_perf_to_khz(caps, caps->nominal_perf);
  674. policy->cpuinfo.max_freq = policy->max;
  675. ret = freq_qos_update_request(policy->max_freq_req, policy->max);
  676. if (ret < 0)
  677. return ret;
  678. return 0;
  679. }
  680. static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
  681. {
  682. struct cppc_cpudata *cpu_data = policy->driver_data;
  683. return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
  684. }
  685. static ssize_t show_auto_select(struct cpufreq_policy *policy, char *buf)
  686. {
  687. bool val;
  688. int ret;
  689. ret = cppc_get_auto_sel(policy->cpu, &val);
  690. /* show "<unsupported>" when this register is not supported by cpc */
  691. if (ret == -EOPNOTSUPP)
  692. return sysfs_emit(buf, "<unsupported>\n");
  693. if (ret)
  694. return ret;
  695. return sysfs_emit(buf, "%d\n", val);
  696. }
  697. static ssize_t store_auto_select(struct cpufreq_policy *policy,
  698. const char *buf, size_t count)
  699. {
  700. bool val;
  701. int ret;
  702. ret = kstrtobool(buf, &val);
  703. if (ret)
  704. return ret;
  705. ret = cppc_set_auto_sel(policy->cpu, val);
  706. if (ret)
  707. return ret;
  708. return count;
  709. }
  710. static ssize_t cppc_cpufreq_sysfs_show_u64(unsigned int cpu,
  711. int (*get_func)(int, u64 *),
  712. char *buf)
  713. {
  714. u64 val;
  715. int ret = get_func((int)cpu, &val);
  716. if (ret == -EOPNOTSUPP)
  717. return sysfs_emit(buf, "<unsupported>\n");
  718. if (ret)
  719. return ret;
  720. return sysfs_emit(buf, "%llu\n", val);
  721. }
  722. static ssize_t cppc_cpufreq_sysfs_store_u64(unsigned int cpu,
  723. int (*set_func)(int, u64),
  724. const char *buf, size_t count)
  725. {
  726. u64 val;
  727. int ret;
  728. ret = kstrtou64(buf, 0, &val);
  729. if (ret)
  730. return ret;
  731. ret = set_func((int)cpu, val);
  732. return ret ? ret : count;
  733. }
  734. #define CPPC_CPUFREQ_ATTR_RW_U64(_name, _get_func, _set_func) \
  735. static ssize_t show_##_name(struct cpufreq_policy *policy, char *buf) \
  736. { \
  737. return cppc_cpufreq_sysfs_show_u64(policy->cpu, _get_func, buf);\
  738. } \
  739. static ssize_t store_##_name(struct cpufreq_policy *policy, \
  740. const char *buf, size_t count) \
  741. { \
  742. return cppc_cpufreq_sysfs_store_u64(policy->cpu, _set_func, \
  743. buf, count); \
  744. }
  745. CPPC_CPUFREQ_ATTR_RW_U64(auto_act_window, cppc_get_auto_act_window,
  746. cppc_set_auto_act_window)
  747. CPPC_CPUFREQ_ATTR_RW_U64(energy_performance_preference_val,
  748. cppc_get_epp_perf, cppc_set_epp)
  749. cpufreq_freq_attr_ro(freqdomain_cpus);
  750. cpufreq_freq_attr_rw(auto_select);
  751. cpufreq_freq_attr_rw(auto_act_window);
  752. cpufreq_freq_attr_rw(energy_performance_preference_val);
  753. static struct freq_attr *cppc_cpufreq_attr[] = {
  754. &freqdomain_cpus,
  755. &auto_select,
  756. &auto_act_window,
  757. &energy_performance_preference_val,
  758. NULL,
  759. };
  760. static struct cpufreq_driver cppc_cpufreq_driver = {
  761. .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
  762. .verify = cppc_verify_policy,
  763. .target = cppc_cpufreq_set_target,
  764. .get = cppc_cpufreq_get_rate,
  765. .fast_switch = cppc_cpufreq_fast_switch,
  766. .init = cppc_cpufreq_cpu_init,
  767. .exit = cppc_cpufreq_cpu_exit,
  768. .set_boost = cppc_cpufreq_set_boost,
  769. .attr = cppc_cpufreq_attr,
  770. .name = "cppc_cpufreq",
  771. };
  772. static int __init cppc_cpufreq_init(void)
  773. {
  774. int ret;
  775. if (!acpi_cpc_valid())
  776. return -ENODEV;
  777. cppc_freq_invariance_init();
  778. populate_efficiency_class();
  779. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  780. if (ret)
  781. cppc_freq_invariance_exit();
  782. return ret;
  783. }
  784. static void __exit cppc_cpufreq_exit(void)
  785. {
  786. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  787. cppc_freq_invariance_exit();
  788. }
  789. module_exit(cppc_cpufreq_exit);
  790. MODULE_AUTHOR("Ashwin Chaugule");
  791. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  792. MODULE_LICENSE("GPL");
  793. late_initcall(cppc_cpufreq_init);
  794. static const struct acpi_device_id cppc_acpi_ids[] __used = {
  795. {ACPI_PROCESSOR_DEVICE_HID, },
  796. {}
  797. };
  798. MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);