xref: /linux-6.15/drivers/cpufreq/cppc_cpufreq.c (revision 2b8e6b58)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25477fb3bSAshwin Chaugule /*
35477fb3bSAshwin Chaugule  * CPPC (Collaborative Processor Performance Control) driver for
45477fb3bSAshwin Chaugule  * interfacing with the CPUfreq layer and governors. See
55477fb3bSAshwin Chaugule  * cppc_acpi.c for CPPC specific methods.
65477fb3bSAshwin Chaugule  *
75477fb3bSAshwin Chaugule  * (C) Copyright 2014, 2015 Linaro Ltd.
85477fb3bSAshwin Chaugule  * Author: Ashwin Chaugule <[email protected]>
95477fb3bSAshwin Chaugule  */
105477fb3bSAshwin Chaugule 
115477fb3bSAshwin Chaugule #define pr_fmt(fmt)	"CPPC Cpufreq:"	fmt
125477fb3bSAshwin Chaugule 
131eb5dde6SViresh Kumar #include <linux/arch_topology.h>
145477fb3bSAshwin Chaugule #include <linux/kernel.h>
155477fb3bSAshwin Chaugule #include <linux/module.h>
165477fb3bSAshwin Chaugule #include <linux/delay.h>
175477fb3bSAshwin Chaugule #include <linux/cpu.h>
185477fb3bSAshwin Chaugule #include <linux/cpufreq.h>
191eb5dde6SViresh Kumar #include <linux/irq_work.h>
201eb5dde6SViresh Kumar #include <linux/kthread.h>
213d41386dSGeorge Cherian #include <linux/time.h>
225477fb3bSAshwin Chaugule #include <linux/vmalloc.h>
231eb5dde6SViresh Kumar #include <uapi/linux/sched/types.h>
245477fb3bSAshwin Chaugule 
255f60d5f6SAl Viro #include <linux/unaligned.h>
26ad38677dSAl Stone 
275477fb3bSAshwin Chaugule #include <acpi/cppc_acpi.h>
285477fb3bSAshwin Chaugule 
295477fb3bSAshwin Chaugule /*
30a28b2bfcSIonela Voinescu  * This list contains information parsed from per CPU ACPI _CPC and _PSD
31a28b2bfcSIonela Voinescu  * structures: e.g. the highest and lowest supported performance, capabilities,
32a28b2bfcSIonela Voinescu  * desired performance, level requested etc. Depending on the share_type, not
33a28b2bfcSIonela Voinescu  * all CPUs will have an entry in the list.
345477fb3bSAshwin Chaugule  */
35a28b2bfcSIonela Voinescu static LIST_HEAD(cpu_data_list);
36a28b2bfcSIonela Voinescu 
37a3f083e0SZheng Bin static struct cpufreq_driver cppc_cpufreq_driver;
38a3f083e0SZheng Bin 
39ea1829d4SJie Zhan #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE
40ae2df912SJeremy Linton static enum {
41ae2df912SJeremy Linton 	FIE_UNSET = -1,
42ae2df912SJeremy Linton 	FIE_ENABLED,
43ae2df912SJeremy Linton 	FIE_DISABLED
44ae2df912SJeremy Linton } fie_disabled = FIE_UNSET;
45ae2df912SJeremy Linton 
46ae2df912SJeremy Linton module_param(fie_disabled, int, 0444);
47ae2df912SJeremy Linton MODULE_PARM_DESC(fie_disabled, "Disable Frequency Invariance Engine (FIE)");
481eb5dde6SViresh Kumar 
491eb5dde6SViresh Kumar /* Frequency invariance support */
501eb5dde6SViresh Kumar struct cppc_freq_invariance {
511eb5dde6SViresh Kumar 	int cpu;
521eb5dde6SViresh Kumar 	struct irq_work irq_work;
531eb5dde6SViresh Kumar 	struct kthread_work work;
541eb5dde6SViresh Kumar 	struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
551eb5dde6SViresh Kumar 	struct cppc_cpudata *cpu_data;
561eb5dde6SViresh Kumar };
571eb5dde6SViresh Kumar 
581eb5dde6SViresh Kumar static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
591eb5dde6SViresh Kumar static struct kthread_worker *kworker_fie;
601eb5dde6SViresh Kumar 
611eb5dde6SViresh Kumar static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
621eb5dde6SViresh Kumar 				 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
631eb5dde6SViresh Kumar 				 struct cppc_perf_fb_ctrs *fb_ctrs_t1);
641eb5dde6SViresh Kumar 
651eb5dde6SViresh Kumar /**
661eb5dde6SViresh Kumar  * cppc_scale_freq_workfn - CPPC arch_freq_scale updater for frequency invariance
671eb5dde6SViresh Kumar  * @work: The work item.
681eb5dde6SViresh Kumar  *
691eb5dde6SViresh Kumar  * The CPPC driver register itself with the topology core to provide its own
701eb5dde6SViresh Kumar  * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which
711eb5dde6SViresh Kumar  * gets called by the scheduler on every tick.
721eb5dde6SViresh Kumar  *
731eb5dde6SViresh Kumar  * Note that the arch specific counters have higher priority than CPPC counters,
741eb5dde6SViresh Kumar  * if available, though the CPPC driver doesn't need to have any special
751eb5dde6SViresh Kumar  * handling for that.
761eb5dde6SViresh Kumar  *
771eb5dde6SViresh Kumar  * On an invocation of cppc_scale_freq_tick(), we schedule an irq work (since we
781eb5dde6SViresh Kumar  * reach here from hard-irq context), which then schedules a normal work item
791eb5dde6SViresh Kumar  * and cppc_scale_freq_workfn() updates the per_cpu arch_freq_scale variable
801eb5dde6SViresh Kumar  * based on the counter updates since the last tick.
811eb5dde6SViresh Kumar  */
cppc_scale_freq_workfn(struct kthread_work * work)821eb5dde6SViresh Kumar static void cppc_scale_freq_workfn(struct kthread_work *work)
831eb5dde6SViresh Kumar {
841eb5dde6SViresh Kumar 	struct cppc_freq_invariance *cppc_fi;
851eb5dde6SViresh Kumar 	struct cppc_perf_fb_ctrs fb_ctrs = {0};
861eb5dde6SViresh Kumar 	struct cppc_cpudata *cpu_data;
871eb5dde6SViresh Kumar 	unsigned long local_freq_scale;
881eb5dde6SViresh Kumar 	u64 perf;
891eb5dde6SViresh Kumar 
901eb5dde6SViresh Kumar 	cppc_fi = container_of(work, struct cppc_freq_invariance, work);
911eb5dde6SViresh Kumar 	cpu_data = cppc_fi->cpu_data;
921eb5dde6SViresh Kumar 
931eb5dde6SViresh Kumar 	if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) {
941eb5dde6SViresh Kumar 		pr_warn("%s: failed to read perf counters\n", __func__);
951eb5dde6SViresh Kumar 		return;
961eb5dde6SViresh Kumar 	}
971eb5dde6SViresh Kumar 
981eb5dde6SViresh Kumar 	perf = cppc_perf_from_fbctrs(cpu_data, &cppc_fi->prev_perf_fb_ctrs,
991eb5dde6SViresh Kumar 				     &fb_ctrs);
100c4719563SJie Zhan 	if (!perf)
101c4719563SJie Zhan 		return;
102c4719563SJie Zhan 
1031eb5dde6SViresh Kumar 	cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
1041eb5dde6SViresh Kumar 
1051eb5dde6SViresh Kumar 	perf <<= SCHED_CAPACITY_SHIFT;
1061eb5dde6SViresh Kumar 	local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf);
1071eb5dde6SViresh Kumar 
1081eb5dde6SViresh Kumar 	/* This can happen due to counter's overflow */
1091eb5dde6SViresh Kumar 	if (unlikely(local_freq_scale > 1024))
1101eb5dde6SViresh Kumar 		local_freq_scale = 1024;
1111eb5dde6SViresh Kumar 
1121eb5dde6SViresh Kumar 	per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale;
1131eb5dde6SViresh Kumar }
1141eb5dde6SViresh Kumar 
cppc_irq_work(struct irq_work * irq_work)1151eb5dde6SViresh Kumar static void cppc_irq_work(struct irq_work *irq_work)
1161eb5dde6SViresh Kumar {
1171eb5dde6SViresh Kumar 	struct cppc_freq_invariance *cppc_fi;
1181eb5dde6SViresh Kumar 
1191eb5dde6SViresh Kumar 	cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
1201eb5dde6SViresh Kumar 	kthread_queue_work(kworker_fie, &cppc_fi->work);
1211eb5dde6SViresh Kumar }
1221eb5dde6SViresh Kumar 
cppc_scale_freq_tick(void)1231eb5dde6SViresh Kumar static void cppc_scale_freq_tick(void)
1241eb5dde6SViresh Kumar {
1251eb5dde6SViresh Kumar 	struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id());
1261eb5dde6SViresh Kumar 
1271eb5dde6SViresh Kumar 	/*
1281eb5dde6SViresh Kumar 	 * cppc_get_perf_ctrs() can potentially sleep, call that from the right
1291eb5dde6SViresh Kumar 	 * context.
1301eb5dde6SViresh Kumar 	 */
1311eb5dde6SViresh Kumar 	irq_work_queue(&cppc_fi->irq_work);
1321eb5dde6SViresh Kumar }
1331eb5dde6SViresh Kumar 
1341eb5dde6SViresh Kumar static struct scale_freq_data cppc_sftd = {
1351eb5dde6SViresh Kumar 	.source = SCALE_FREQ_SOURCE_CPPC,
1361eb5dde6SViresh Kumar 	.set_freq_scale = cppc_scale_freq_tick,
1371eb5dde6SViresh Kumar };
1381eb5dde6SViresh Kumar 
cppc_cpufreq_cpu_fie_init(struct cpufreq_policy * policy)1391eb5dde6SViresh Kumar static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
1401eb5dde6SViresh Kumar {
1411eb5dde6SViresh Kumar 	struct cppc_freq_invariance *cppc_fi;
1421eb5dde6SViresh Kumar 	int cpu, ret;
1431eb5dde6SViresh Kumar 
144ae2df912SJeremy Linton 	if (fie_disabled)
1451eb5dde6SViresh Kumar 		return;
1461eb5dde6SViresh Kumar 
1471eb5dde6SViresh Kumar 	for_each_cpu(cpu, policy->cpus) {
1481eb5dde6SViresh Kumar 		cppc_fi = &per_cpu(cppc_freq_inv, cpu);
1491eb5dde6SViresh Kumar 		cppc_fi->cpu = cpu;
1501eb5dde6SViresh Kumar 		cppc_fi->cpu_data = policy->driver_data;
1511eb5dde6SViresh Kumar 		kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn);
1521eb5dde6SViresh Kumar 		init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
1531eb5dde6SViresh Kumar 
1541eb5dde6SViresh Kumar 		ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
1551eb5dde6SViresh Kumar 		if (ret) {
1561eb5dde6SViresh Kumar 			pr_warn("%s: failed to read perf counters for cpu:%d: %d\n",
1571eb5dde6SViresh Kumar 				__func__, cpu, ret);
1581eb5dde6SViresh Kumar 
1591eb5dde6SViresh Kumar 			/*
1601eb5dde6SViresh Kumar 			 * Don't abort if the CPU was offline while the driver
1611eb5dde6SViresh Kumar 			 * was getting registered.
1621eb5dde6SViresh Kumar 			 */
1631eb5dde6SViresh Kumar 			if (cpu_online(cpu))
1641eb5dde6SViresh Kumar 				return;
1651eb5dde6SViresh Kumar 		}
1661eb5dde6SViresh Kumar 	}
1671eb5dde6SViresh Kumar 
1681eb5dde6SViresh Kumar 	/* Register for freq-invariance */
1691eb5dde6SViresh Kumar 	topology_set_scale_freq_source(&cppc_sftd, policy->cpus);
1701eb5dde6SViresh Kumar }
1711eb5dde6SViresh Kumar 
1721eb5dde6SViresh Kumar /*
1731eb5dde6SViresh Kumar  * We free all the resources on policy's removal and not on CPU removal as the
1741eb5dde6SViresh Kumar  * irq-work are per-cpu and the hotplug core takes care of flushing the pending
1751eb5dde6SViresh Kumar  * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work
1761eb5dde6SViresh Kumar  * fires on another CPU after the concerned CPU is removed, it won't harm.
1771eb5dde6SViresh Kumar  *
1781eb5dde6SViresh Kumar  * We just need to make sure to remove them all on policy->exit().
1791eb5dde6SViresh Kumar  */
cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy * policy)1801eb5dde6SViresh Kumar static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
1811eb5dde6SViresh Kumar {
1821eb5dde6SViresh Kumar 	struct cppc_freq_invariance *cppc_fi;
1831eb5dde6SViresh Kumar 	int cpu;
1841eb5dde6SViresh Kumar 
185ae2df912SJeremy Linton 	if (fie_disabled)
1861eb5dde6SViresh Kumar 		return;
1871eb5dde6SViresh Kumar 
1881eb5dde6SViresh Kumar 	/* policy->cpus will be empty here, use related_cpus instead */
1891eb5dde6SViresh Kumar 	topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
1901eb5dde6SViresh Kumar 
1911eb5dde6SViresh Kumar 	for_each_cpu(cpu, policy->related_cpus) {
1921eb5dde6SViresh Kumar 		cppc_fi = &per_cpu(cppc_freq_inv, cpu);
1931eb5dde6SViresh Kumar 		irq_work_sync(&cppc_fi->irq_work);
1941eb5dde6SViresh Kumar 		kthread_cancel_work_sync(&cppc_fi->work);
1951eb5dde6SViresh Kumar 	}
1961eb5dde6SViresh Kumar }
1971eb5dde6SViresh Kumar 
cppc_freq_invariance_init(void)1981eb5dde6SViresh Kumar static void __init cppc_freq_invariance_init(void)
1991eb5dde6SViresh Kumar {
2001eb5dde6SViresh Kumar 	struct sched_attr attr = {
2011eb5dde6SViresh Kumar 		.size		= sizeof(struct sched_attr),
2021eb5dde6SViresh Kumar 		.sched_policy	= SCHED_DEADLINE,
2031eb5dde6SViresh Kumar 		.sched_nice	= 0,
2041eb5dde6SViresh Kumar 		.sched_priority	= 0,
2051eb5dde6SViresh Kumar 		/*
2061eb5dde6SViresh Kumar 		 * Fake (unused) bandwidth; workaround to "fix"
2071eb5dde6SViresh Kumar 		 * priority inheritance.
2081eb5dde6SViresh Kumar 		 */
2094eb71e3bSChristian Loehle 		.sched_runtime	= NSEC_PER_MSEC,
2104eb71e3bSChristian Loehle 		.sched_deadline = 10 * NSEC_PER_MSEC,
2114eb71e3bSChristian Loehle 		.sched_period	= 10 * NSEC_PER_MSEC,
2121eb5dde6SViresh Kumar 	};
2131eb5dde6SViresh Kumar 	int ret;
2141eb5dde6SViresh Kumar 
215ae2df912SJeremy Linton 	if (fie_disabled != FIE_ENABLED && fie_disabled != FIE_DISABLED) {
216ae2df912SJeremy Linton 		fie_disabled = FIE_ENABLED;
217ae2df912SJeremy Linton 		if (cppc_perf_ctrs_in_pcc()) {
218ae2df912SJeremy Linton 			pr_info("FIE not enabled on systems with registers in PCC\n");
219ae2df912SJeremy Linton 			fie_disabled = FIE_DISABLED;
220ae2df912SJeremy Linton 		}
221ae2df912SJeremy Linton 	}
222ae2df912SJeremy Linton 
223ae2df912SJeremy Linton 	if (fie_disabled)
2241eb5dde6SViresh Kumar 		return;
2251eb5dde6SViresh Kumar 
226b04e317bSFrederic Weisbecker 	kworker_fie = kthread_run_worker(0, "cppc_fie");
227e613d8cfSLiao Chang 	if (IS_ERR(kworker_fie)) {
228e613d8cfSLiao Chang 		pr_warn("%s: failed to create kworker_fie: %ld\n", __func__,
229e613d8cfSLiao Chang 			PTR_ERR(kworker_fie));
230e613d8cfSLiao Chang 		fie_disabled = FIE_DISABLED;
2311eb5dde6SViresh Kumar 		return;
232e613d8cfSLiao Chang 	}
2331eb5dde6SViresh Kumar 
2341eb5dde6SViresh Kumar 	ret = sched_setattr_nocheck(kworker_fie->task, &attr);
2351eb5dde6SViresh Kumar 	if (ret) {
2361eb5dde6SViresh Kumar 		pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__,
2371eb5dde6SViresh Kumar 			ret);
2381eb5dde6SViresh Kumar 		kthread_destroy_worker(kworker_fie);
239e613d8cfSLiao Chang 		fie_disabled = FIE_DISABLED;
2401eb5dde6SViresh Kumar 	}
2411eb5dde6SViresh Kumar }
2421eb5dde6SViresh Kumar 
cppc_freq_invariance_exit(void)2431eb5dde6SViresh Kumar static void cppc_freq_invariance_exit(void)
2441eb5dde6SViresh Kumar {
245ae2df912SJeremy Linton 	if (fie_disabled)
2461eb5dde6SViresh Kumar 		return;
2471eb5dde6SViresh Kumar 
2481eb5dde6SViresh Kumar 	kthread_destroy_worker(kworker_fie);
2491eb5dde6SViresh Kumar }
2501eb5dde6SViresh Kumar 
2511eb5dde6SViresh Kumar #else
cppc_cpufreq_cpu_fie_init(struct cpufreq_policy * policy)2521eb5dde6SViresh Kumar static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)
2531eb5dde6SViresh Kumar {
2541eb5dde6SViresh Kumar }
2551eb5dde6SViresh Kumar 
cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy * policy)2561eb5dde6SViresh Kumar static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
2571eb5dde6SViresh Kumar {
2581eb5dde6SViresh Kumar }
2591eb5dde6SViresh Kumar 
cppc_freq_invariance_init(void)2601eb5dde6SViresh Kumar static inline void cppc_freq_invariance_init(void)
2611eb5dde6SViresh Kumar {
2621eb5dde6SViresh Kumar }
2631eb5dde6SViresh Kumar 
cppc_freq_invariance_exit(void)2641eb5dde6SViresh Kumar static inline void cppc_freq_invariance_exit(void)
2651eb5dde6SViresh Kumar {
2661eb5dde6SViresh Kumar }
2671eb5dde6SViresh Kumar #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */
2681eb5dde6SViresh Kumar 
cppc_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)2695477fb3bSAshwin Chaugule static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
2705477fb3bSAshwin Chaugule 				   unsigned int target_freq,
2715477fb3bSAshwin Chaugule 				   unsigned int relation)
2725477fb3bSAshwin Chaugule {
273a28b2bfcSIonela Voinescu 	struct cppc_cpudata *cpu_data = policy->driver_data;
274d2641a5cSIonela Voinescu 	unsigned int cpu = policy->cpu;
2755477fb3bSAshwin Chaugule 	struct cpufreq_freqs freqs;
2765477fb3bSAshwin Chaugule 	int ret = 0;
2775477fb3bSAshwin Chaugule 
27890e4ed6bSRiwen Lu 	cpu_data->perf_ctrls.desired_perf =
27990e4ed6bSRiwen Lu 			cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
2805477fb3bSAshwin Chaugule 	freqs.old = policy->cur;
2815477fb3bSAshwin Chaugule 	freqs.new = target_freq;
2825477fb3bSAshwin Chaugule 
2835477fb3bSAshwin Chaugule 	cpufreq_freq_transition_begin(policy, &freqs);
284d2641a5cSIonela Voinescu 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
2855477fb3bSAshwin Chaugule 	cpufreq_freq_transition_end(policy, &freqs, ret != 0);
2865477fb3bSAshwin Chaugule 
2875477fb3bSAshwin Chaugule 	if (ret)
2885477fb3bSAshwin Chaugule 		pr_debug("Failed to set target on CPU:%d. ret:%d\n",
289d2641a5cSIonela Voinescu 			 cpu, ret);
2905477fb3bSAshwin Chaugule 
2915477fb3bSAshwin Chaugule 	return ret;
2925477fb3bSAshwin Chaugule }
2935477fb3bSAshwin Chaugule 
cppc_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)2943cc30dd0SPierre Gondois static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy,
2953cc30dd0SPierre Gondois 					      unsigned int target_freq)
2963cc30dd0SPierre Gondois {
2973cc30dd0SPierre Gondois 	struct cppc_cpudata *cpu_data = policy->driver_data;
2983cc30dd0SPierre Gondois 	unsigned int cpu = policy->cpu;
2993cc30dd0SPierre Gondois 	u32 desired_perf;
3003cc30dd0SPierre Gondois 	int ret;
3013cc30dd0SPierre Gondois 
30250b813b1SVincent Guittot 	desired_perf = cppc_khz_to_perf(&cpu_data->perf_caps, target_freq);
3033cc30dd0SPierre Gondois 	cpu_data->perf_ctrls.desired_perf = desired_perf;
3043cc30dd0SPierre Gondois 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
3053cc30dd0SPierre Gondois 
3063cc30dd0SPierre Gondois 	if (ret) {
3073cc30dd0SPierre Gondois 		pr_debug("Failed to set target on CPU:%d. ret:%d\n",
3083cc30dd0SPierre Gondois 			 cpu, ret);
3093cc30dd0SPierre Gondois 		return 0;
3103cc30dd0SPierre Gondois 	}
3113cc30dd0SPierre Gondois 
3123cc30dd0SPierre Gondois 	return target_freq;
3133cc30dd0SPierre Gondois }
3143cc30dd0SPierre Gondois 
cppc_verify_policy(struct cpufreq_policy_data * policy)3151e4f63aeSRafael J. Wysocki static int cppc_verify_policy(struct cpufreq_policy_data *policy)
3165477fb3bSAshwin Chaugule {
3175477fb3bSAshwin Chaugule 	cpufreq_verify_within_cpu_limits(policy);
3185477fb3bSAshwin Chaugule 	return 0;
3195477fb3bSAshwin Chaugule }
3205477fb3bSAshwin Chaugule 
321d4f3388aSPrashanth Prakash /*
322d4f3388aSPrashanth Prakash  * The PCC subspace describes the rate at which platform can accept commands
323d4f3388aSPrashanth Prakash  * on the shared PCC channel (including READs which do not count towards freq
32463087265SIonela Voinescu  * transition requests), so ideally we need to use the PCC values as a fallback
325d4f3388aSPrashanth Prakash  * if we don't have a platform specific transition_delay_us
326d4f3388aSPrashanth Prakash  */
327d4f3388aSPrashanth Prakash #ifdef CONFIG_ARM64
328d4f3388aSPrashanth Prakash #include <asm/cputype.h>
329d4f3388aSPrashanth Prakash 
cppc_cpufreq_get_transition_delay_us(unsigned int cpu)33048ad8dc9SIonela Voinescu static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
331d4f3388aSPrashanth Prakash {
332d4f3388aSPrashanth Prakash 	unsigned long implementor = read_cpuid_implementor();
333d4f3388aSPrashanth Prakash 	unsigned long part_num = read_cpuid_part_number();
334d4f3388aSPrashanth Prakash 
335d4f3388aSPrashanth Prakash 	switch (implementor) {
336d4f3388aSPrashanth Prakash 	case ARM_CPU_IMP_QCOM:
337d4f3388aSPrashanth Prakash 		switch (part_num) {
338d4f3388aSPrashanth Prakash 		case QCOM_CPU_PART_FALKOR_V1:
339d4f3388aSPrashanth Prakash 		case QCOM_CPU_PART_FALKOR:
3402b53d1bdSTom Saeger 			return 10000;
341d4f3388aSPrashanth Prakash 		}
342d4f3388aSPrashanth Prakash 	}
3432b53d1bdSTom Saeger 	return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
344d4f3388aSPrashanth Prakash }
345da436345SPierre Gondois #else
cppc_cpufreq_get_transition_delay_us(unsigned int cpu)346da436345SPierre Gondois static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu)
347da436345SPierre Gondois {
348da436345SPierre Gondois 	return cppc_get_transition_latency(cpu) / NSEC_PER_USEC;
349da436345SPierre Gondois }
350da436345SPierre Gondois #endif
351da436345SPierre Gondois 
352da436345SPierre Gondois #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL)
353d4f3388aSPrashanth Prakash 
354d3c3db41SPierre Gondois static DEFINE_PER_CPU(unsigned int, efficiency_class);
355740fcdc2SPierre Gondois static void cppc_cpufreq_register_em(struct cpufreq_policy *policy);
356740fcdc2SPierre Gondois 
357740fcdc2SPierre Gondois /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */
358740fcdc2SPierre Gondois #define CPPC_EM_CAP_STEP	(20)
359740fcdc2SPierre Gondois /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
360740fcdc2SPierre Gondois #define CPPC_EM_COST_STEP	(1)
361740fcdc2SPierre Gondois /* Add a cost gap correspnding to the energy of 4 CPUs. */
362740fcdc2SPierre Gondois #define CPPC_EM_COST_GAP	(4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
363740fcdc2SPierre Gondois 				/ CPPC_EM_CAP_STEP)
364740fcdc2SPierre Gondois 
get_perf_level_count(struct cpufreq_policy * policy)365740fcdc2SPierre Gondois static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
366740fcdc2SPierre Gondois {
367740fcdc2SPierre Gondois 	struct cppc_perf_caps *perf_caps;
368740fcdc2SPierre Gondois 	unsigned int min_cap, max_cap;
369740fcdc2SPierre Gondois 	struct cppc_cpudata *cpu_data;
370740fcdc2SPierre Gondois 	int cpu = policy->cpu;
371740fcdc2SPierre Gondois 
372740fcdc2SPierre Gondois 	cpu_data = policy->driver_data;
373740fcdc2SPierre Gondois 	perf_caps = &cpu_data->perf_caps;
374740fcdc2SPierre Gondois 	max_cap = arch_scale_cpu_capacity(cpu);
375f5f94b9cSPierre Gondois 	min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
376f5f94b9cSPierre Gondois 			  perf_caps->highest_perf);
377740fcdc2SPierre Gondois 	if ((min_cap == 0) || (max_cap < min_cap))
378740fcdc2SPierre Gondois 		return 0;
379740fcdc2SPierre Gondois 	return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
380740fcdc2SPierre Gondois }
381740fcdc2SPierre Gondois 
382740fcdc2SPierre Gondois /*
383740fcdc2SPierre Gondois  * The cost is defined as:
384740fcdc2SPierre Gondois  *   cost = power * max_frequency / frequency
385740fcdc2SPierre Gondois  */
compute_cost(int cpu,int step)386740fcdc2SPierre Gondois static inline unsigned long compute_cost(int cpu, int step)
387740fcdc2SPierre Gondois {
388740fcdc2SPierre Gondois 	return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) +
389740fcdc2SPierre Gondois 			step * CPPC_EM_COST_STEP;
390740fcdc2SPierre Gondois }
391740fcdc2SPierre Gondois 
cppc_get_cpu_power(struct device * cpu_dev,unsigned long * power,unsigned long * KHz)392740fcdc2SPierre Gondois static int cppc_get_cpu_power(struct device *cpu_dev,
393740fcdc2SPierre Gondois 		unsigned long *power, unsigned long *KHz)
394740fcdc2SPierre Gondois {
395740fcdc2SPierre Gondois 	unsigned long perf_step, perf_prev, perf, perf_check;
396740fcdc2SPierre Gondois 	unsigned int min_step, max_step, step, step_check;
397740fcdc2SPierre Gondois 	unsigned long prev_freq = *KHz;
398740fcdc2SPierre Gondois 	unsigned int min_cap, max_cap;
399740fcdc2SPierre Gondois 	struct cpufreq_policy *policy;
400740fcdc2SPierre Gondois 
401740fcdc2SPierre Gondois 	struct cppc_perf_caps *perf_caps;
402740fcdc2SPierre Gondois 	struct cppc_cpudata *cpu_data;
403740fcdc2SPierre Gondois 
404740fcdc2SPierre Gondois 	policy = cpufreq_cpu_get_raw(cpu_dev->id);
405a78e7207SJinjie Ruan 	if (!policy)
406b51eb087SJinjie Ruan 		return -EINVAL;
407a78e7207SJinjie Ruan 
408740fcdc2SPierre Gondois 	cpu_data = policy->driver_data;
409740fcdc2SPierre Gondois 	perf_caps = &cpu_data->perf_caps;
410740fcdc2SPierre Gondois 	max_cap = arch_scale_cpu_capacity(cpu_dev->id);
411f5f94b9cSPierre Gondois 	min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf,
412740fcdc2SPierre Gondois 			  perf_caps->highest_perf);
413f5f94b9cSPierre Gondois 	perf_step = div_u64((u64)CPPC_EM_CAP_STEP * perf_caps->highest_perf,
414f5f94b9cSPierre Gondois 			    max_cap);
415740fcdc2SPierre Gondois 	min_step = min_cap / CPPC_EM_CAP_STEP;
416740fcdc2SPierre Gondois 	max_step = max_cap / CPPC_EM_CAP_STEP;
417740fcdc2SPierre Gondois 
41850b813b1SVincent Guittot 	perf_prev = cppc_khz_to_perf(perf_caps, *KHz);
419740fcdc2SPierre Gondois 	step = perf_prev / perf_step;
420740fcdc2SPierre Gondois 
421740fcdc2SPierre Gondois 	if (step > max_step)
422740fcdc2SPierre Gondois 		return -EINVAL;
423740fcdc2SPierre Gondois 
424740fcdc2SPierre Gondois 	if (min_step == max_step) {
425740fcdc2SPierre Gondois 		step = max_step;
426740fcdc2SPierre Gondois 		perf = perf_caps->highest_perf;
427740fcdc2SPierre Gondois 	} else if (step < min_step) {
428740fcdc2SPierre Gondois 		step = min_step;
429740fcdc2SPierre Gondois 		perf = perf_caps->lowest_perf;
430740fcdc2SPierre Gondois 	} else {
431740fcdc2SPierre Gondois 		step++;
432740fcdc2SPierre Gondois 		if (step == max_step)
433740fcdc2SPierre Gondois 			perf = perf_caps->highest_perf;
434740fcdc2SPierre Gondois 		else
435740fcdc2SPierre Gondois 			perf = step * perf_step;
436740fcdc2SPierre Gondois 	}
437740fcdc2SPierre Gondois 
43850b813b1SVincent Guittot 	*KHz = cppc_perf_to_khz(perf_caps, perf);
43950b813b1SVincent Guittot 	perf_check = cppc_khz_to_perf(perf_caps, *KHz);
440740fcdc2SPierre Gondois 	step_check = perf_check / perf_step;
441740fcdc2SPierre Gondois 
442740fcdc2SPierre Gondois 	/*
443740fcdc2SPierre Gondois 	 * To avoid bad integer approximation, check that new frequency value
444740fcdc2SPierre Gondois 	 * increased and that the new frequency will be converted to the
445740fcdc2SPierre Gondois 	 * desired step value.
446740fcdc2SPierre Gondois 	 */
447740fcdc2SPierre Gondois 	while ((*KHz == prev_freq) || (step_check != step)) {
448740fcdc2SPierre Gondois 		perf++;
44950b813b1SVincent Guittot 		*KHz = cppc_perf_to_khz(perf_caps, perf);
45050b813b1SVincent Guittot 		perf_check = cppc_khz_to_perf(perf_caps, *KHz);
451740fcdc2SPierre Gondois 		step_check = perf_check / perf_step;
452740fcdc2SPierre Gondois 	}
453740fcdc2SPierre Gondois 
454740fcdc2SPierre Gondois 	/*
455740fcdc2SPierre Gondois 	 * With an artificial EM, only the cost value is used. Still the power
456740fcdc2SPierre Gondois 	 * is populated such as 0 < power < EM_MAX_POWER. This allows to add
457740fcdc2SPierre Gondois 	 * more sense to the artificial performance states.
458740fcdc2SPierre Gondois 	 */
459740fcdc2SPierre Gondois 	*power = compute_cost(cpu_dev->id, step);
460740fcdc2SPierre Gondois 
461740fcdc2SPierre Gondois 	return 0;
462740fcdc2SPierre Gondois }
463740fcdc2SPierre Gondois 
cppc_get_cpu_cost(struct device * cpu_dev,unsigned long KHz,unsigned long * cost)464740fcdc2SPierre Gondois static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
465740fcdc2SPierre Gondois 		unsigned long *cost)
466740fcdc2SPierre Gondois {
467740fcdc2SPierre Gondois 	unsigned long perf_step, perf_prev;
468740fcdc2SPierre Gondois 	struct cppc_perf_caps *perf_caps;
469740fcdc2SPierre Gondois 	struct cpufreq_policy *policy;
470740fcdc2SPierre Gondois 	struct cppc_cpudata *cpu_data;
471740fcdc2SPierre Gondois 	unsigned int max_cap;
472740fcdc2SPierre Gondois 	int step;
473740fcdc2SPierre Gondois 
474740fcdc2SPierre Gondois 	policy = cpufreq_cpu_get_raw(cpu_dev->id);
4751a1374bbSJinjie Ruan 	if (!policy)
476be392aa8SJinjie Ruan 		return -EINVAL;
4771a1374bbSJinjie Ruan 
478740fcdc2SPierre Gondois 	cpu_data = policy->driver_data;
479740fcdc2SPierre Gondois 	perf_caps = &cpu_data->perf_caps;
480740fcdc2SPierre Gondois 	max_cap = arch_scale_cpu_capacity(cpu_dev->id);
481740fcdc2SPierre Gondois 
48250b813b1SVincent Guittot 	perf_prev = cppc_khz_to_perf(perf_caps, KHz);
483740fcdc2SPierre Gondois 	perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap;
484740fcdc2SPierre Gondois 	step = perf_prev / perf_step;
485740fcdc2SPierre Gondois 
486740fcdc2SPierre Gondois 	*cost = compute_cost(cpu_dev->id, step);
487740fcdc2SPierre Gondois 
488740fcdc2SPierre Gondois 	return 0;
489740fcdc2SPierre Gondois }
490d3c3db41SPierre Gondois 
populate_efficiency_class(void)491d3c3db41SPierre Gondois static int populate_efficiency_class(void)
492d3c3db41SPierre Gondois {
493d3c3db41SPierre Gondois 	struct acpi_madt_generic_interrupt *gicc;
494d3c3db41SPierre Gondois 	DECLARE_BITMAP(used_classes, 256) = {};
495d3c3db41SPierre Gondois 	int class, cpu, index;
496d3c3db41SPierre Gondois 
497d3c3db41SPierre Gondois 	for_each_possible_cpu(cpu) {
498d3c3db41SPierre Gondois 		gicc = acpi_cpu_get_madt_gicc(cpu);
499d3c3db41SPierre Gondois 		class = gicc->efficiency_class;
500d3c3db41SPierre Gondois 		bitmap_set(used_classes, class, 1);
501d3c3db41SPierre Gondois 	}
502d3c3db41SPierre Gondois 
503d3c3db41SPierre Gondois 	if (bitmap_weight(used_classes, 256) <= 1) {
504d3c3db41SPierre Gondois 		pr_debug("Efficiency classes are all equal (=%d). "
505d3c3db41SPierre Gondois 			"No EM registered", class);
506d3c3db41SPierre Gondois 		return -EINVAL;
507d3c3db41SPierre Gondois 	}
508d3c3db41SPierre Gondois 
509d3c3db41SPierre Gondois 	/*
510d3c3db41SPierre Gondois 	 * Squeeze efficiency class values on [0:#efficiency_class-1].
511d3c3db41SPierre Gondois 	 * Values are per spec in [0:255].
512d3c3db41SPierre Gondois 	 */
513d3c3db41SPierre Gondois 	index = 0;
514d3c3db41SPierre Gondois 	for_each_set_bit(class, used_classes, 256) {
515d3c3db41SPierre Gondois 		for_each_possible_cpu(cpu) {
516d3c3db41SPierre Gondois 			gicc = acpi_cpu_get_madt_gicc(cpu);
517d3c3db41SPierre Gondois 			if (gicc->efficiency_class == class)
518d3c3db41SPierre Gondois 				per_cpu(efficiency_class, cpu) = index;
519d3c3db41SPierre Gondois 		}
520d3c3db41SPierre Gondois 		index++;
521d3c3db41SPierre Gondois 	}
522740fcdc2SPierre Gondois 	cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em;
523d3c3db41SPierre Gondois 
524d3c3db41SPierre Gondois 	return 0;
525d3c3db41SPierre Gondois }
526d3c3db41SPierre Gondois 
cppc_cpufreq_register_em(struct cpufreq_policy * policy)527740fcdc2SPierre Gondois static void cppc_cpufreq_register_em(struct cpufreq_policy *policy)
528740fcdc2SPierre Gondois {
529740fcdc2SPierre Gondois 	struct cppc_cpudata *cpu_data;
530740fcdc2SPierre Gondois 	struct em_data_callback em_cb =
531740fcdc2SPierre Gondois 		EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost);
532740fcdc2SPierre Gondois 
533740fcdc2SPierre Gondois 	cpu_data = policy->driver_data;
534740fcdc2SPierre Gondois 	em_dev_register_perf_domain(get_cpu_device(policy->cpu),
535740fcdc2SPierre Gondois 			get_perf_level_count(policy), &em_cb,
536740fcdc2SPierre Gondois 			cpu_data->shared_cpu_map, 0);
537740fcdc2SPierre Gondois }
538740fcdc2SPierre Gondois 
539d4f3388aSPrashanth Prakash #else
populate_efficiency_class(void)540d3c3db41SPierre Gondois static int populate_efficiency_class(void)
541d3c3db41SPierre Gondois {
542d3c3db41SPierre Gondois 	return 0;
543d3c3db41SPierre Gondois }
544d4f3388aSPrashanth Prakash #endif
545d4f3388aSPrashanth Prakash 
cppc_cpufreq_get_cpu_data(unsigned int cpu)546a28b2bfcSIonela Voinescu static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu)
5475477fb3bSAshwin Chaugule {
548a28b2bfcSIonela Voinescu 	struct cppc_cpudata *cpu_data;
549a28b2bfcSIonela Voinescu 	int ret;
5505477fb3bSAshwin Chaugule 
551a28b2bfcSIonela Voinescu 	cpu_data = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
552a28b2bfcSIonela Voinescu 	if (!cpu_data)
553a28b2bfcSIonela Voinescu 		goto out;
5545477fb3bSAshwin Chaugule 
555a28b2bfcSIonela Voinescu 	if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL))
556a28b2bfcSIonela Voinescu 		goto free_cpu;
557a28b2bfcSIonela Voinescu 
558a28b2bfcSIonela Voinescu 	ret = acpi_get_psd_map(cpu, cpu_data);
5595477fb3bSAshwin Chaugule 	if (ret) {
560a28b2bfcSIonela Voinescu 		pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret);
561a28b2bfcSIonela Voinescu 		goto free_mask;
562a28b2bfcSIonela Voinescu 	}
563a28b2bfcSIonela Voinescu 
564a28b2bfcSIonela Voinescu 	ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps);
565a28b2bfcSIonela Voinescu 	if (ret) {
566a28b2bfcSIonela Voinescu 		pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret);
567a28b2bfcSIonela Voinescu 		goto free_mask;
5685477fb3bSAshwin Chaugule 	}
5695477fb3bSAshwin Chaugule 
570a28b2bfcSIonela Voinescu 	list_add(&cpu_data->node, &cpu_data_list);
571a28b2bfcSIonela Voinescu 
572a28b2bfcSIonela Voinescu 	return cpu_data;
573a28b2bfcSIonela Voinescu 
574a28b2bfcSIonela Voinescu free_mask:
575a28b2bfcSIonela Voinescu 	free_cpumask_var(cpu_data->shared_cpu_map);
576a28b2bfcSIonela Voinescu free_cpu:
577a28b2bfcSIonela Voinescu 	kfree(cpu_data);
578a28b2bfcSIonela Voinescu out:
579a28b2bfcSIonela Voinescu 	return NULL;
580a28b2bfcSIonela Voinescu }
581a28b2bfcSIonela Voinescu 
cppc_cpufreq_put_cpu_data(struct cpufreq_policy * policy)582fe2535a4SViresh Kumar static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy)
583fe2535a4SViresh Kumar {
584fe2535a4SViresh Kumar 	struct cppc_cpudata *cpu_data = policy->driver_data;
585fe2535a4SViresh Kumar 
586fe2535a4SViresh Kumar 	list_del(&cpu_data->node);
587fe2535a4SViresh Kumar 	free_cpumask_var(cpu_data->shared_cpu_map);
588fe2535a4SViresh Kumar 	kfree(cpu_data);
589fe2535a4SViresh Kumar 	policy->driver_data = NULL;
590fe2535a4SViresh Kumar }
591fe2535a4SViresh Kumar 
cppc_cpufreq_cpu_init(struct cpufreq_policy * policy)592a28b2bfcSIonela Voinescu static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
593a28b2bfcSIonela Voinescu {
594a28b2bfcSIonela Voinescu 	unsigned int cpu = policy->cpu;
595a28b2bfcSIonela Voinescu 	struct cppc_cpudata *cpu_data;
596a28b2bfcSIonela Voinescu 	struct cppc_perf_caps *caps;
597a28b2bfcSIonela Voinescu 	int ret;
598a28b2bfcSIonela Voinescu 
599a28b2bfcSIonela Voinescu 	cpu_data = cppc_cpufreq_get_cpu_data(cpu);
600a28b2bfcSIonela Voinescu 	if (!cpu_data) {
601a28b2bfcSIonela Voinescu 		pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu);
602a28b2bfcSIonela Voinescu 		return -ENODEV;
603a28b2bfcSIonela Voinescu 	}
604a28b2bfcSIonela Voinescu 	caps = &cpu_data->perf_caps;
605a28b2bfcSIonela Voinescu 	policy->driver_data = cpu_data;
606ad38677dSAl Stone 
60773808d0fSPrakash, Prashanth 	/*
60873808d0fSPrakash, Prashanth 	 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see
60973808d0fSPrakash, Prashanth 	 * Section 8.4.7.1.1.5 of ACPI 6.1 spec)
61073808d0fSPrakash, Prashanth 	 */
61150b813b1SVincent Guittot 	policy->min = cppc_perf_to_khz(caps, caps->lowest_nonlinear_perf);
61203d8b4e7SLifeng Zheng 	policy->max = cppc_perf_to_khz(caps, policy->boost_enabled ?
61303d8b4e7SLifeng Zheng 						caps->highest_perf : caps->nominal_perf);
61473808d0fSPrakash, Prashanth 
61573808d0fSPrakash, Prashanth 	/*
61673808d0fSPrakash, Prashanth 	 * Set cpuinfo.min_freq to Lowest to make the full range of performance
61773808d0fSPrakash, Prashanth 	 * available if userspace wants to use any perf between lowest & lowest
61873808d0fSPrakash, Prashanth 	 * nonlinear perf
61973808d0fSPrakash, Prashanth 	 */
62050b813b1SVincent Guittot 	policy->cpuinfo.min_freq = cppc_perf_to_khz(caps, caps->lowest_perf);
62103d8b4e7SLifeng Zheng 	policy->cpuinfo.max_freq = policy->max;
62273808d0fSPrakash, Prashanth 
62348ad8dc9SIonela Voinescu 	policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu);
62448ad8dc9SIonela Voinescu 	policy->shared_type = cpu_data->shared_type;
6255477fb3bSAshwin Chaugule 
626bf76bb20SIonela Voinescu 	switch (policy->shared_type) {
627bf76bb20SIonela Voinescu 	case CPUFREQ_SHARED_TYPE_HW:
628bf76bb20SIonela Voinescu 	case CPUFREQ_SHARED_TYPE_NONE:
629bf76bb20SIonela Voinescu 		/* Nothing to be done - we'll have a policy for each CPU */
630bf76bb20SIonela Voinescu 		break;
631bf76bb20SIonela Voinescu 	case CPUFREQ_SHARED_TYPE_ANY:
632a28b2bfcSIonela Voinescu 		/*
633a28b2bfcSIonela Voinescu 		 * All CPUs in the domain will share a policy and all cpufreq
634a28b2bfcSIonela Voinescu 		 * operations will use a single cppc_cpudata structure stored
635a28b2bfcSIonela Voinescu 		 * in policy->driver_data.
636a28b2bfcSIonela Voinescu 		 */
63748ad8dc9SIonela Voinescu 		cpumask_copy(policy->cpus, cpu_data->shared_cpu_map);
638bf76bb20SIonela Voinescu 		break;
639bf76bb20SIonela Voinescu 	default:
640bf76bb20SIonela Voinescu 		pr_debug("Unsupported CPU co-ord type: %d\n",
641bf76bb20SIonela Voinescu 			 policy->shared_type);
642fe2535a4SViresh Kumar 		ret = -EFAULT;
643fe2535a4SViresh Kumar 		goto out;
6445477fb3bSAshwin Chaugule 	}
6455477fb3bSAshwin Chaugule 
6463cc30dd0SPierre Gondois 	policy->fast_switch_possible = cppc_allow_fast_switch();
6472d41dc23SPierre Gondois 	policy->dvfs_possible_from_any_cpu = true;
6483cc30dd0SPierre Gondois 
64954e74df5SXiongfeng Wang 	/*
65054e74df5SXiongfeng Wang 	 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost
65154e74df5SXiongfeng Wang 	 * is supported.
65254e74df5SXiongfeng Wang 	 */
653bb025fb6SIonela Voinescu 	if (caps->highest_perf > caps->nominal_perf)
654a3f48fb2SViresh Kumar 		policy->boost_supported = true;
65554e74df5SXiongfeng Wang 
6565477fb3bSAshwin Chaugule 	/* Set policy->cur to max now. The governors will adjust later. */
65750b813b1SVincent Guittot 	policy->cur = cppc_perf_to_khz(caps, caps->highest_perf);
658bb025fb6SIonela Voinescu 	cpu_data->perf_ctrls.desired_perf =  caps->highest_perf;
6595477fb3bSAshwin Chaugule 
66048ad8dc9SIonela Voinescu 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
661fe2535a4SViresh Kumar 	if (ret) {
6625477fb3bSAshwin Chaugule 		pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
663bb025fb6SIonela Voinescu 			 caps->highest_perf, cpu, ret);
664fe2535a4SViresh Kumar 		goto out;
665fe2535a4SViresh Kumar 	}
6665477fb3bSAshwin Chaugule 
6671eb5dde6SViresh Kumar 	cppc_cpufreq_cpu_fie_init(policy);
668fe2535a4SViresh Kumar 	return 0;
669fe2535a4SViresh Kumar 
670fe2535a4SViresh Kumar out:
671fe2535a4SViresh Kumar 	cppc_cpufreq_put_cpu_data(policy);
6725477fb3bSAshwin Chaugule 	return ret;
6735477fb3bSAshwin Chaugule }
6745477fb3bSAshwin Chaugule 
cppc_cpufreq_cpu_exit(struct cpufreq_policy * policy)675b4b1ddc9SLizhe static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
6769357a380SViresh Kumar {
6779357a380SViresh Kumar 	struct cppc_cpudata *cpu_data = policy->driver_data;
6789357a380SViresh Kumar 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
6799357a380SViresh Kumar 	unsigned int cpu = policy->cpu;
6809357a380SViresh Kumar 	int ret;
6819357a380SViresh Kumar 
6821eb5dde6SViresh Kumar 	cppc_cpufreq_cpu_fie_exit(policy);
6831eb5dde6SViresh Kumar 
6849357a380SViresh Kumar 	cpu_data->perf_ctrls.desired_perf = caps->lowest_perf;
6859357a380SViresh Kumar 
6869357a380SViresh Kumar 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
6879357a380SViresh Kumar 	if (ret)
6889357a380SViresh Kumar 		pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
6899357a380SViresh Kumar 			 caps->lowest_perf, cpu, ret);
6909357a380SViresh Kumar 
691fe2535a4SViresh Kumar 	cppc_cpufreq_put_cpu_data(policy);
6929357a380SViresh Kumar }
6939357a380SViresh Kumar 
get_delta(u64 t1,u64 t0)69433477d84SGeorge Cherian static inline u64 get_delta(u64 t1, u64 t0)
69533477d84SGeorge Cherian {
69633477d84SGeorge Cherian 	if (t1 > t0 || t0 > ~(u32)0)
69733477d84SGeorge Cherian 		return t1 - t0;
69833477d84SGeorge Cherian 
69933477d84SGeorge Cherian 	return (u32)t1 - (u32)t0;
70033477d84SGeorge Cherian }
70133477d84SGeorge Cherian 
cppc_perf_from_fbctrs(struct cppc_cpudata * cpu_data,struct cppc_perf_fb_ctrs * fb_ctrs_t0,struct cppc_perf_fb_ctrs * fb_ctrs_t1)7021eb5dde6SViresh Kumar static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
703eead1840SViresh Kumar 				 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
704eead1840SViresh Kumar 				 struct cppc_perf_fb_ctrs *fb_ctrs_t1)
70533477d84SGeorge Cherian {
70633477d84SGeorge Cherian 	u64 delta_reference, delta_delivered;
7071eb5dde6SViresh Kumar 	u64 reference_perf;
70833477d84SGeorge Cherian 
709eead1840SViresh Kumar 	reference_perf = fb_ctrs_t0->reference_perf;
71033477d84SGeorge Cherian 
711eead1840SViresh Kumar 	delta_reference = get_delta(fb_ctrs_t1->reference,
712eead1840SViresh Kumar 				    fb_ctrs_t0->reference);
713eead1840SViresh Kumar 	delta_delivered = get_delta(fb_ctrs_t1->delivered,
714eead1840SViresh Kumar 				    fb_ctrs_t0->delivered);
71533477d84SGeorge Cherian 
716c4719563SJie Zhan 	/*
717c4719563SJie Zhan 	 * Avoid divide-by zero and unchanged feedback counters.
718c4719563SJie Zhan 	 * Leave it for callers to handle.
719c4719563SJie Zhan 	 */
7201eb5dde6SViresh Kumar 	if (!delta_reference || !delta_delivered)
721c4719563SJie Zhan 		return 0;
72233477d84SGeorge Cherian 
7231eb5dde6SViresh Kumar 	return (reference_perf * delta_delivered) / delta_reference;
72433477d84SGeorge Cherian }
72533477d84SGeorge Cherian 
cppc_get_perf_ctrs_sample(int cpu,struct cppc_perf_fb_ctrs * fb_ctrs_t0,struct cppc_perf_fb_ctrs * fb_ctrs_t1)726c4719563SJie Zhan static int cppc_get_perf_ctrs_sample(int cpu,
727c4719563SJie Zhan 				     struct cppc_perf_fb_ctrs *fb_ctrs_t0,
728c4719563SJie Zhan 				     struct cppc_perf_fb_ctrs *fb_ctrs_t1)
729c4719563SJie Zhan {
730c4719563SJie Zhan 	int ret;
731c4719563SJie Zhan 
732c4719563SJie Zhan 	ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0);
733c4719563SJie Zhan 	if (ret)
734c4719563SJie Zhan 		return ret;
735c4719563SJie Zhan 
736c4719563SJie Zhan 	udelay(2); /* 2usec delay between sampling */
737c4719563SJie Zhan 
738c4719563SJie Zhan 	return cppc_get_perf_ctrs(cpu, fb_ctrs_t1);
739c4719563SJie Zhan }
740c4719563SJie Zhan 
cppc_cpufreq_get_rate(unsigned int cpu)74148ad8dc9SIonela Voinescu static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
74233477d84SGeorge Cherian {
74333477d84SGeorge Cherian 	struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
744a28b2bfcSIonela Voinescu 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
745cf7de258SAleksandr Mishin 	struct cppc_cpudata *cpu_data;
7461eb5dde6SViresh Kumar 	u64 delivered_perf;
74733477d84SGeorge Cherian 	int ret;
74833477d84SGeorge Cherian 
749cf7de258SAleksandr Mishin 	if (!policy)
750*2b8e6b58SMarc Zyngier 		return 0;
751cf7de258SAleksandr Mishin 
752cf7de258SAleksandr Mishin 	cpu_data = policy->driver_data;
753cf7de258SAleksandr Mishin 
754a28b2bfcSIonela Voinescu 	cpufreq_cpu_put(policy);
755a28b2bfcSIonela Voinescu 
756c4719563SJie Zhan 	ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1);
757c4719563SJie Zhan 	if (ret) {
758c4719563SJie Zhan 		if (ret == -EFAULT)
759c4719563SJie Zhan 			/* Any of the associated CPPC regs is 0. */
760c4719563SJie Zhan 			goto out_invalid_counters;
761c4719563SJie Zhan 		else
7626a4fec4fSLiao Chang 			return 0;
763c4719563SJie Zhan 	}
76433477d84SGeorge Cherian 
7651eb5dde6SViresh Kumar 	delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
7661eb5dde6SViresh Kumar 					       &fb_ctrs_t1);
767c4719563SJie Zhan 	if (!delivered_perf)
768c4719563SJie Zhan 		goto out_invalid_counters;
769c4719563SJie Zhan 
770c4719563SJie Zhan 	return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
771c4719563SJie Zhan 
772c4719563SJie Zhan out_invalid_counters:
773c4719563SJie Zhan 	/*
774c4719563SJie Zhan 	 * Feedback counters could be unchanged or 0 when a cpu enters a
775c4719563SJie Zhan 	 * low-power idle state, e.g. clock-gated or power-gated.
776c4719563SJie Zhan 	 * Use desired perf for reflecting frequency.  Get the latest register
777c4719563SJie Zhan 	 * value first as some platforms may update the actual delivered perf
778c4719563SJie Zhan 	 * there; if failed, resort to the cached desired perf.
779c4719563SJie Zhan 	 */
780c4719563SJie Zhan 	if (cppc_get_desired_perf(cpu, &delivered_perf))
781c4719563SJie Zhan 		delivered_perf = cpu_data->perf_ctrls.desired_perf;
7821eb5dde6SViresh Kumar 
78350b813b1SVincent Guittot 	return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
78433477d84SGeorge Cherian }
78533477d84SGeorge Cherian 
cppc_cpufreq_set_boost(struct cpufreq_policy * policy,int state)78654e74df5SXiongfeng Wang static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
78754e74df5SXiongfeng Wang {
788a28b2bfcSIonela Voinescu 	struct cppc_cpudata *cpu_data = policy->driver_data;
789bb025fb6SIonela Voinescu 	struct cppc_perf_caps *caps = &cpu_data->perf_caps;
79054e74df5SXiongfeng Wang 	int ret;
79154e74df5SXiongfeng Wang 
79254e74df5SXiongfeng Wang 	if (state)
79350b813b1SVincent Guittot 		policy->max = cppc_perf_to_khz(caps, caps->highest_perf);
79454e74df5SXiongfeng Wang 	else
79550b813b1SVincent Guittot 		policy->max = cppc_perf_to_khz(caps, caps->nominal_perf);
79654e74df5SXiongfeng Wang 	policy->cpuinfo.max_freq = policy->max;
79754e74df5SXiongfeng Wang 
79854e74df5SXiongfeng Wang 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
79954e74df5SXiongfeng Wang 	if (ret < 0)
80054e74df5SXiongfeng Wang 		return ret;
80154e74df5SXiongfeng Wang 
80254e74df5SXiongfeng Wang 	return 0;
80354e74df5SXiongfeng Wang }
80454e74df5SXiongfeng Wang 
show_freqdomain_cpus(struct cpufreq_policy * policy,char * buf)805cfdc589fSIonela Voinescu static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
806cfdc589fSIonela Voinescu {
807a28b2bfcSIonela Voinescu 	struct cppc_cpudata *cpu_data = policy->driver_data;
808cfdc589fSIonela Voinescu 
809a28b2bfcSIonela Voinescu 	return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf);
810cfdc589fSIonela Voinescu }
811cfdc589fSIonela Voinescu cpufreq_freq_attr_ro(freqdomain_cpus);
812cfdc589fSIonela Voinescu 
813cfdc589fSIonela Voinescu static struct freq_attr *cppc_cpufreq_attr[] = {
814cfdc589fSIonela Voinescu 	&freqdomain_cpus,
815cfdc589fSIonela Voinescu 	NULL,
816cfdc589fSIonela Voinescu };
817cfdc589fSIonela Voinescu 
8185477fb3bSAshwin Chaugule static struct cpufreq_driver cppc_cpufreq_driver = {
8195477fb3bSAshwin Chaugule 	.flags = CPUFREQ_CONST_LOOPS,
8205477fb3bSAshwin Chaugule 	.verify = cppc_verify_policy,
8215477fb3bSAshwin Chaugule 	.target = cppc_cpufreq_set_target,
82233477d84SGeorge Cherian 	.get = cppc_cpufreq_get_rate,
8233cc30dd0SPierre Gondois 	.fast_switch = cppc_cpufreq_fast_switch,
8245477fb3bSAshwin Chaugule 	.init = cppc_cpufreq_cpu_init,
8259357a380SViresh Kumar 	.exit = cppc_cpufreq_cpu_exit,
82654e74df5SXiongfeng Wang 	.set_boost = cppc_cpufreq_set_boost,
827cfdc589fSIonela Voinescu 	.attr = cppc_cpufreq_attr,
8285477fb3bSAshwin Chaugule 	.name = "cppc_cpufreq",
8295477fb3bSAshwin Chaugule };
8305477fb3bSAshwin Chaugule 
cppc_cpufreq_init(void)8315477fb3bSAshwin Chaugule static int __init cppc_cpufreq_init(void)
8325477fb3bSAshwin Chaugule {
8331eb5dde6SViresh Kumar 	int ret;
8341eb5dde6SViresh Kumar 
835a2a9d185SPerry Yuan 	if (!acpi_cpc_valid())
8365477fb3bSAshwin Chaugule 		return -ENODEV;
8375477fb3bSAshwin Chaugule 
8381eb5dde6SViresh Kumar 	cppc_freq_invariance_init();
839d3c3db41SPierre Gondois 	populate_efficiency_class();
8406c8d750fSXiongfeng Wang 
8411eb5dde6SViresh Kumar 	ret = cpufreq_register_driver(&cppc_cpufreq_driver);
8421eb5dde6SViresh Kumar 	if (ret)
8431eb5dde6SViresh Kumar 		cppc_freq_invariance_exit();
8441eb5dde6SViresh Kumar 
8451eb5dde6SViresh Kumar 	return ret;
84655b55abcSChunyu Hu }
8475477fb3bSAshwin Chaugule 
free_cpu_data(void)848a28b2bfcSIonela Voinescu static inline void free_cpu_data(void)
849a28b2bfcSIonela Voinescu {
850a28b2bfcSIonela Voinescu 	struct cppc_cpudata *iter, *tmp;
851a28b2bfcSIonela Voinescu 
852a28b2bfcSIonela Voinescu 	list_for_each_entry_safe(iter, tmp, &cpu_data_list, node) {
853a28b2bfcSIonela Voinescu 		free_cpumask_var(iter->shared_cpu_map);
854a28b2bfcSIonela Voinescu 		list_del(&iter->node);
855a28b2bfcSIonela Voinescu 		kfree(iter);
856a28b2bfcSIonela Voinescu 	}
857a28b2bfcSIonela Voinescu 
8585477fb3bSAshwin Chaugule }
8595477fb3bSAshwin Chaugule 
cppc_cpufreq_exit(void)860a29a1e76SAshwin Chaugule static void __exit cppc_cpufreq_exit(void)
861a29a1e76SAshwin Chaugule {
862a29a1e76SAshwin Chaugule 	cpufreq_unregister_driver(&cppc_cpufreq_driver);
8631eb5dde6SViresh Kumar 	cppc_freq_invariance_exit();
864a29a1e76SAshwin Chaugule 
865a28b2bfcSIonela Voinescu 	free_cpu_data();
866a29a1e76SAshwin Chaugule }
867a29a1e76SAshwin Chaugule 
868a29a1e76SAshwin Chaugule module_exit(cppc_cpufreq_exit);
869a29a1e76SAshwin Chaugule MODULE_AUTHOR("Ashwin Chaugule");
870a29a1e76SAshwin Chaugule MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
871a29a1e76SAshwin Chaugule MODULE_LICENSE("GPL");
872a29a1e76SAshwin Chaugule 
8735477fb3bSAshwin Chaugule late_initcall(cppc_cpufreq_init);
874974f8649SPrakash, Prashanth 
8758ff3c226SNathan Chancellor static const struct acpi_device_id cppc_acpi_ids[] __used = {
876974f8649SPrakash, Prashanth 	{ACPI_PROCESSOR_DEVICE_HID, },
877974f8649SPrakash, Prashanth 	{}
878974f8649SPrakash, Prashanth };
879974f8649SPrakash, Prashanth 
880974f8649SPrakash, Prashanth MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids);
881