Linux Audio

Check our new training course

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