cppc_cpufreq.c 26 KB

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