1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020 - 2022, NVIDIA CORPORATION. All rights reserved
7 #include <linux/cpufreq.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/units.h>
16 #include <asm/smp_plat.h>
18 #include <soc/tegra/bpmp.h>
19 #include <soc/tegra/bpmp-abi.h>
22 #define REF_CLK_MHZ 408 /* 408 MHz */
23 #define CPUFREQ_TBL_STEP_HZ (50 * KHZ * KHZ)
26 #define MAX_DELTA_KHZ 115200
28 #define NDIV_MASK 0x1FF
30 #define CORE_OFFSET(cpu) (cpu * 8)
31 #define CMU_CLKS_BASE 0x2000
32 #define SCRATCH_FREQ_CORE_REG(data, cpu) (data->regs + CMU_CLKS_BASE + CORE_OFFSET(cpu))
34 #define MMCRAB_CLUSTER_BASE(cl) (0x30000 + (cl * 0x10000))
35 #define CLUSTER_ACTMON_BASE(data, cl) \
36 (data->regs + (MMCRAB_CLUSTER_BASE(cl) + data->soc->actmon_cntr_base))
37 #define CORE_ACTMON_CNTR_REG(data, cl, cpu) (CLUSTER_ACTMON_BASE(data, cl) + CORE_OFFSET(cpu))
39 /* cpufreq transisition latency */
40 #define TEGRA_CPUFREQ_TRANSITION_LATENCY (300 * 1000) /* unit in nanoseconds */
42 struct tegra_cpu_data
{
45 void __iomem
*freq_core_reg
;
48 struct tegra_cpu_ctr
{
50 u32 coreclk_cnt
, last_coreclk_cnt
;
51 u32 refclk_cnt
, last_refclk_cnt
;
54 struct read_counters_work
{
55 struct work_struct work
;
56 struct tegra_cpu_ctr c
;
59 struct tegra_cpufreq_ops
{
60 void (*read_counters
)(struct tegra_cpu_ctr
*c
);
61 void (*set_cpu_ndiv
)(struct cpufreq_policy
*policy
, u64 ndiv
);
62 void (*get_cpu_cluster_id
)(u32 cpu
, u32
*cpuid
, u32
*clusterid
);
63 int (*get_cpu_ndiv
)(u32 cpu
, u32 cpuid
, u32 clusterid
, u64
*ndiv
);
66 struct tegra_cpufreq_soc
{
67 struct tegra_cpufreq_ops
*ops
;
68 int maxcpus_per_cluster
;
69 unsigned int num_clusters
;
70 phys_addr_t actmon_cntr_base
;
74 struct tegra194_cpufreq_data
{
76 struct cpufreq_frequency_table
**bpmp_luts
;
77 const struct tegra_cpufreq_soc
*soc
;
78 bool icc_dram_bw_scaling
;
79 struct tegra_cpu_data
*cpu_data
;
82 static struct workqueue_struct
*read_counters_wq
;
84 static int tegra_cpufreq_set_bw(struct cpufreq_policy
*policy
, unsigned long freq_khz
)
86 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
87 struct dev_pm_opp
*opp
;
91 dev
= get_cpu_device(policy
->cpu
);
95 opp
= dev_pm_opp_find_freq_exact(dev
, freq_khz
* KHZ
, true);
99 ret
= dev_pm_opp_set_opp(dev
, opp
);
101 data
->icc_dram_bw_scaling
= false;
107 static void tegra_get_cpu_mpidr(void *mpidr
)
109 *((u64
*)mpidr
) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK
;
112 static void tegra234_get_cpu_cluster_id(u32 cpu
, u32
*cpuid
, u32
*clusterid
)
116 smp_call_function_single(cpu
, tegra_get_cpu_mpidr
, &mpidr
, true);
119 *cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
121 *clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 2);
124 static int tegra234_get_cpu_ndiv(u32 cpu
, u32 cpuid
, u32 clusterid
, u64
*ndiv
)
126 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
128 *ndiv
= readl(data
->cpu_data
[cpu
].freq_core_reg
) & NDIV_MASK
;
133 static void tegra234_set_cpu_ndiv(struct cpufreq_policy
*policy
, u64 ndiv
)
135 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
138 for_each_cpu(cpu
, policy
->cpus
)
139 writel(ndiv
, data
->cpu_data
[cpu
].freq_core_reg
);
143 * This register provides access to two counter values with a single
144 * 64-bit read. The counter values are used to determine the average
145 * actual frequency a core has run at over a period of time.
146 * [63:32] PLLP counter: Counts at fixed frequency (408 MHz)
147 * [31:0] Core clock counter: Counts on every core clock cycle
149 static void tegra234_read_counters(struct tegra_cpu_ctr
*c
)
151 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
152 void __iomem
*actmon_reg
;
157 actmon_reg
= CORE_ACTMON_CNTR_REG(data
, data
->cpu_data
[c
->cpu
].clusterid
,
158 data
->cpu_data
[c
->cpu
].cpuid
);
160 val
= readq(actmon_reg
);
161 c
->last_refclk_cnt
= upper_32_bits(val
);
162 c
->last_coreclk_cnt
= lower_32_bits(val
);
165 * The sampling window is based on the minimum number of reference
166 * clock cycles which is known to give a stable value of CPU frequency.
169 val
= readq(actmon_reg
);
170 c
->refclk_cnt
= upper_32_bits(val
);
171 c
->coreclk_cnt
= lower_32_bits(val
);
172 if (c
->refclk_cnt
< c
->last_refclk_cnt
)
173 delta_refcnt
= c
->refclk_cnt
+ (MAX_CNT
- c
->last_refclk_cnt
);
175 delta_refcnt
= c
->refclk_cnt
- c
->last_refclk_cnt
;
176 if (++cnt
>= 0xFFFF) {
177 pr_warn("cpufreq: problem with refclk on cpu:%d, delta_refcnt:%u, cnt:%d\n",
178 c
->cpu
, delta_refcnt
, cnt
);
181 } while (delta_refcnt
< data
->soc
->refclk_delta_min
);
184 static struct tegra_cpufreq_ops tegra234_cpufreq_ops
= {
185 .read_counters
= tegra234_read_counters
,
186 .get_cpu_cluster_id
= tegra234_get_cpu_cluster_id
,
187 .get_cpu_ndiv
= tegra234_get_cpu_ndiv
,
188 .set_cpu_ndiv
= tegra234_set_cpu_ndiv
,
191 static const struct tegra_cpufreq_soc tegra234_cpufreq_soc
= {
192 .ops
= &tegra234_cpufreq_ops
,
193 .actmon_cntr_base
= 0x9000,
194 .maxcpus_per_cluster
= 4,
196 .refclk_delta_min
= 16000,
199 static const struct tegra_cpufreq_soc tegra239_cpufreq_soc
= {
200 .ops
= &tegra234_cpufreq_ops
,
201 .actmon_cntr_base
= 0x4000,
202 .maxcpus_per_cluster
= 8,
204 .refclk_delta_min
= 16000,
207 static void tegra194_get_cpu_cluster_id(u32 cpu
, u32
*cpuid
, u32
*clusterid
)
211 smp_call_function_single(cpu
, tegra_get_cpu_mpidr
, &mpidr
, true);
214 *cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
216 *clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
220 * Read per-core Read-only system register NVFREQ_FEEDBACK_EL1.
221 * The register provides frequency feedback information to
222 * determine the average actual frequency a core has run at over
224 * [31:0] PLLP counter: Counts at fixed frequency (408 MHz)
225 * [63:32] Core clock counter: counts on every core clock cycle
226 * where the core is architecturally clocking
228 static u64
read_freq_feedback(void)
232 asm volatile("mrs %0, s3_0_c15_c0_5" : "=r" (val
) : );
237 static inline u32
map_ndiv_to_freq(struct mrq_cpu_ndiv_limits_response
240 return nltbl
->ref_clk_hz
/ KHZ
* ndiv
/ (nltbl
->pdiv
* nltbl
->mdiv
);
243 static void tegra194_read_counters(struct tegra_cpu_ctr
*c
)
245 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
250 val
= read_freq_feedback();
251 c
->last_refclk_cnt
= lower_32_bits(val
);
252 c
->last_coreclk_cnt
= upper_32_bits(val
);
255 * The sampling window is based on the minimum number of reference
256 * clock cycles which is known to give a stable value of CPU frequency.
259 val
= read_freq_feedback();
260 c
->refclk_cnt
= lower_32_bits(val
);
261 c
->coreclk_cnt
= upper_32_bits(val
);
262 if (c
->refclk_cnt
< c
->last_refclk_cnt
)
263 delta_refcnt
= c
->refclk_cnt
+ (MAX_CNT
- c
->last_refclk_cnt
);
265 delta_refcnt
= c
->refclk_cnt
- c
->last_refclk_cnt
;
266 if (++cnt
>= 0xFFFF) {
267 pr_warn("cpufreq: problem with refclk on cpu:%d, delta_refcnt:%u, cnt:%d\n",
268 c
->cpu
, delta_refcnt
, cnt
);
271 } while (delta_refcnt
< data
->soc
->refclk_delta_min
);
274 static void tegra_read_counters(struct work_struct
*work
)
276 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
277 struct read_counters_work
*read_counters_work
;
278 struct tegra_cpu_ctr
*c
;
281 * ref_clk_counter(32 bit counter) runs on constant clk,
283 * It will take = 2 ^ 32 / 408 MHz to overflow ref clk counter
284 * = 10526880 usec = 10.527 sec to overflow
286 * Like wise core_clk_counter(32 bit counter) runs on core clock.
287 * It's synchronized to crab_clk (cpu_crab_clk) which runs at
288 * freq of cluster. Assuming max cluster clock ~2000MHz,
289 * It will take = 2 ^ 32 / 2000 MHz to overflow core clk counter
290 * = ~2.147 sec to overflow
292 read_counters_work
= container_of(work
, struct read_counters_work
,
294 c
= &read_counters_work
->c
;
296 data
->soc
->ops
->read_counters(c
);
300 * Return instantaneous cpu speed
301 * Instantaneous freq is calculated as -
302 * -Takes sample on every query of getting the freq.
303 * - Read core and ref clock counters;
305 * - Read above cycle counters again
306 * - Calculates freq by subtracting current and previous counters
307 * divided by the delay time or eqv. of ref_clk_counter in delta time
308 * - Return Kcycles/second, freq in KHz
310 * delta time period = x sec
311 * = delta ref_clk_counter / (408 * 10^6) sec
312 * freq in Hz = cycles/sec
313 * = (delta cycles / x sec
314 * = (delta cycles * 408 * 10^6) / delta ref_clk_counter
315 * in KHz = (delta cycles * 408 * 10^3) / delta ref_clk_counter
317 * @cpu - logical cpu whose freq to be updated
318 * Returns freq in KHz on success, 0 if cpu is offline
320 static unsigned int tegra194_calculate_speed(u32 cpu
)
322 struct read_counters_work read_counters_work
;
323 struct tegra_cpu_ctr c
;
329 * Reconstruct cpu frequency over an observation/sampling window.
330 * Using workqueue to keep interrupts enabled during the interval.
332 read_counters_work
.c
.cpu
= cpu
;
333 INIT_WORK_ONSTACK(&read_counters_work
.work
, tegra_read_counters
);
334 queue_work_on(cpu
, read_counters_wq
, &read_counters_work
.work
);
335 flush_work(&read_counters_work
.work
);
336 c
= read_counters_work
.c
;
338 if (c
.coreclk_cnt
< c
.last_coreclk_cnt
)
339 delta_ccnt
= c
.coreclk_cnt
+ (MAX_CNT
- c
.last_coreclk_cnt
);
341 delta_ccnt
= c
.coreclk_cnt
- c
.last_coreclk_cnt
;
345 /* ref clock is 32 bits */
346 if (c
.refclk_cnt
< c
.last_refclk_cnt
)
347 delta_refcnt
= c
.refclk_cnt
+ (MAX_CNT
- c
.last_refclk_cnt
);
349 delta_refcnt
= c
.refclk_cnt
- c
.last_refclk_cnt
;
351 pr_debug("cpufreq: %d is idle, delta_refcnt: 0\n", cpu
);
354 rate_mhz
= ((unsigned long)(delta_ccnt
* REF_CLK_MHZ
)) / delta_refcnt
;
356 return (rate_mhz
* KHZ
); /* in KHz */
359 static void tegra194_get_cpu_ndiv_sysreg(void *ndiv
)
363 asm volatile("mrs %0, s3_0_c15_c0_4" : "=r" (ndiv_val
) : );
365 *(u64
*)ndiv
= ndiv_val
;
368 static int tegra194_get_cpu_ndiv(u32 cpu
, u32 cpuid
, u32 clusterid
, u64
*ndiv
)
370 return smp_call_function_single(cpu
, tegra194_get_cpu_ndiv_sysreg
, &ndiv
, true);
373 static void tegra194_set_cpu_ndiv_sysreg(void *data
)
375 u64 ndiv_val
= *(u64
*)data
;
377 asm volatile("msr s3_0_c15_c0_4, %0" : : "r" (ndiv_val
));
380 static void tegra194_set_cpu_ndiv(struct cpufreq_policy
*policy
, u64 ndiv
)
382 on_each_cpu_mask(policy
->cpus
, tegra194_set_cpu_ndiv_sysreg
, &ndiv
, true);
385 static unsigned int tegra194_get_speed(u32 cpu
)
387 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
388 u32 clusterid
= data
->cpu_data
[cpu
].clusterid
;
389 struct cpufreq_frequency_table
*pos
;
394 /* reconstruct actual cpu freq using counters */
395 rate
= tegra194_calculate_speed(cpu
);
397 /* get last written ndiv value */
398 ret
= data
->soc
->ops
->get_cpu_ndiv(cpu
, data
->cpu_data
[cpu
].cpuid
, clusterid
, &ndiv
);
399 if (WARN_ON_ONCE(ret
))
403 * If the reconstructed frequency has acceptable delta from
404 * the last written value, then return freq corresponding
405 * to the last written ndiv value from freq_table. This is
406 * done to return consistent value.
408 cpufreq_for_each_valid_entry(pos
, data
->bpmp_luts
[clusterid
]) {
409 if (pos
->driver_data
!= ndiv
)
412 if (abs(pos
->frequency
- rate
) > MAX_DELTA_KHZ
) {
413 pr_warn("cpufreq: cpu%d,cur:%u,set:%u,delta:%d,set ndiv:%llu\n",
414 cpu
, rate
, pos
->frequency
, abs(rate
- pos
->frequency
), ndiv
);
416 rate
= pos
->frequency
;
423 static int tegra_cpufreq_init_cpufreq_table(struct cpufreq_policy
*policy
,
424 struct cpufreq_frequency_table
*bpmp_lut
,
425 struct cpufreq_frequency_table
**opp_table
)
427 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
428 struct cpufreq_frequency_table
*freq_table
= NULL
;
429 struct cpufreq_frequency_table
*pos
;
430 struct device
*cpu_dev
;
431 struct dev_pm_opp
*opp
;
436 cpu_dev
= get_cpu_device(policy
->cpu
);
438 pr_err("%s: failed to get cpu%d device\n", __func__
, policy
->cpu
);
442 /* Initialize OPP table mentioned in operating-points-v2 property in DT */
443 ret
= dev_pm_opp_of_add_table_indexed(cpu_dev
, 0);
445 max_opps
= dev_pm_opp_get_opp_count(cpu_dev
);
447 dev_err(cpu_dev
, "Failed to add OPPs\n");
451 /* Disable all opps and cross-validate against LUT later */
452 for (rate
= 0; ; rate
++) {
453 opp
= dev_pm_opp_find_freq_ceil(cpu_dev
, &rate
);
458 dev_pm_opp_disable(cpu_dev
, rate
);
461 dev_err(cpu_dev
, "Invalid or empty opp table in device tree\n");
462 data
->icc_dram_bw_scaling
= false;
466 freq_table
= kcalloc((max_opps
+ 1), sizeof(*freq_table
), GFP_KERNEL
);
471 * Cross check the frequencies from BPMP-FW LUT against the OPP's present in DT.
472 * Enable only those DT OPP's which are present in LUT also.
474 cpufreq_for_each_valid_entry(pos
, bpmp_lut
) {
475 opp
= dev_pm_opp_find_freq_exact(cpu_dev
, pos
->frequency
* KHZ
, false);
481 ret
= dev_pm_opp_enable(cpu_dev
, pos
->frequency
* KHZ
);
485 freq_table
[j
].driver_data
= pos
->driver_data
;
486 freq_table
[j
].frequency
= pos
->frequency
;
490 freq_table
[j
].driver_data
= pos
->driver_data
;
491 freq_table
[j
].frequency
= CPUFREQ_TABLE_END
;
493 *opp_table
= &freq_table
[0];
495 dev_pm_opp_set_sharing_cpus(cpu_dev
, policy
->cpus
);
500 static int tegra194_cpufreq_init(struct cpufreq_policy
*policy
)
502 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
503 int maxcpus_per_cluster
= data
->soc
->maxcpus_per_cluster
;
504 u32 clusterid
= data
->cpu_data
[policy
->cpu
].clusterid
;
505 struct cpufreq_frequency_table
*freq_table
;
506 struct cpufreq_frequency_table
*bpmp_lut
;
510 if (clusterid
>= data
->soc
->num_clusters
|| !data
->bpmp_luts
[clusterid
])
513 start_cpu
= rounddown(policy
->cpu
, maxcpus_per_cluster
);
514 /* set same policy for all cpus in a cluster */
515 for (cpu
= start_cpu
; cpu
< (start_cpu
+ maxcpus_per_cluster
); cpu
++) {
516 if (cpu_possible(cpu
))
517 cpumask_set_cpu(cpu
, policy
->cpus
);
519 policy
->cpuinfo
.transition_latency
= TEGRA_CPUFREQ_TRANSITION_LATENCY
;
521 bpmp_lut
= data
->bpmp_luts
[clusterid
];
523 if (data
->icc_dram_bw_scaling
) {
524 ret
= tegra_cpufreq_init_cpufreq_table(policy
, bpmp_lut
, &freq_table
);
526 policy
->freq_table
= freq_table
;
531 data
->icc_dram_bw_scaling
= false;
532 policy
->freq_table
= bpmp_lut
;
533 pr_info("OPP tables missing from DT, EMC frequency scaling disabled\n");
538 static int tegra194_cpufreq_online(struct cpufreq_policy
*policy
)
540 /* We did light-weight tear down earlier, nothing to do here */
544 static int tegra194_cpufreq_offline(struct cpufreq_policy
*policy
)
547 * Preserve policy->driver_data and don't free resources on light-weight
554 static void tegra194_cpufreq_exit(struct cpufreq_policy
*policy
)
556 struct device
*cpu_dev
= get_cpu_device(policy
->cpu
);
558 dev_pm_opp_remove_all_dynamic(cpu_dev
);
559 dev_pm_opp_of_cpumask_remove_table(policy
->related_cpus
);
562 static int tegra194_cpufreq_set_target(struct cpufreq_policy
*policy
,
565 struct cpufreq_frequency_table
*tbl
= policy
->freq_table
+ index
;
566 struct tegra194_cpufreq_data
*data
= cpufreq_get_driver_data();
569 * Each core writes frequency in per core register. Then both cores
570 * in a cluster run at same frequency which is the maximum frequency
571 * request out of the values requested by both cores in that cluster.
573 data
->soc
->ops
->set_cpu_ndiv(policy
, (u64
)tbl
->driver_data
);
575 if (data
->icc_dram_bw_scaling
)
576 tegra_cpufreq_set_bw(policy
, tbl
->frequency
);
581 static struct cpufreq_driver tegra194_cpufreq_driver
= {
583 .flags
= CPUFREQ_CONST_LOOPS
| CPUFREQ_NEED_INITIAL_FREQ_CHECK
|
584 CPUFREQ_IS_COOLING_DEV
,
585 .verify
= cpufreq_generic_frequency_table_verify
,
586 .target_index
= tegra194_cpufreq_set_target
,
587 .get
= tegra194_get_speed
,
588 .init
= tegra194_cpufreq_init
,
589 .exit
= tegra194_cpufreq_exit
,
590 .online
= tegra194_cpufreq_online
,
591 .offline
= tegra194_cpufreq_offline
,
592 .attr
= cpufreq_generic_attr
,
595 static struct tegra_cpufreq_ops tegra194_cpufreq_ops
= {
596 .read_counters
= tegra194_read_counters
,
597 .get_cpu_cluster_id
= tegra194_get_cpu_cluster_id
,
598 .get_cpu_ndiv
= tegra194_get_cpu_ndiv
,
599 .set_cpu_ndiv
= tegra194_set_cpu_ndiv
,
602 static const struct tegra_cpufreq_soc tegra194_cpufreq_soc
= {
603 .ops
= &tegra194_cpufreq_ops
,
604 .maxcpus_per_cluster
= 2,
606 .refclk_delta_min
= 16000,
609 static void tegra194_cpufreq_free_resources(void)
611 destroy_workqueue(read_counters_wq
);
614 static struct cpufreq_frequency_table
*
615 tegra_cpufreq_bpmp_read_lut(struct platform_device
*pdev
, struct tegra_bpmp
*bpmp
,
616 unsigned int cluster_id
)
618 struct cpufreq_frequency_table
*freq_table
;
619 struct mrq_cpu_ndiv_limits_response resp
;
620 unsigned int num_freqs
, ndiv
, delta_ndiv
;
621 struct mrq_cpu_ndiv_limits_request req
;
622 struct tegra_bpmp_message msg
;
623 u16 freq_table_step_size
;
626 memset(&req
, 0, sizeof(req
));
627 req
.cluster_id
= cluster_id
;
629 memset(&msg
, 0, sizeof(msg
));
630 msg
.mrq
= MRQ_CPU_NDIV_LIMITS
;
632 msg
.tx
.size
= sizeof(req
);
634 msg
.rx
.size
= sizeof(resp
);
636 err
= tegra_bpmp_transfer(bpmp
, &msg
);
639 if (msg
.rx
.ret
== -BPMP_EINVAL
) {
640 /* Cluster not available */
644 return ERR_PTR(-EINVAL
);
647 * Make sure frequency table step is a multiple of mdiv to match
648 * vhint table granularity.
650 freq_table_step_size
= resp
.mdiv
*
651 DIV_ROUND_UP(CPUFREQ_TBL_STEP_HZ
, resp
.ref_clk_hz
);
653 dev_dbg(&pdev
->dev
, "cluster %d: frequency table step size: %d\n",
654 cluster_id
, freq_table_step_size
);
656 delta_ndiv
= resp
.ndiv_max
- resp
.ndiv_min
;
658 if (unlikely(delta_ndiv
== 0)) {
661 /* We store both ndiv_min and ndiv_max hence the +1 */
662 num_freqs
= delta_ndiv
/ freq_table_step_size
+ 1;
665 num_freqs
+= (delta_ndiv
% freq_table_step_size
) ? 1 : 0;
667 freq_table
= devm_kcalloc(&pdev
->dev
, num_freqs
+ 1,
668 sizeof(*freq_table
), GFP_KERNEL
);
670 return ERR_PTR(-ENOMEM
);
672 for (index
= 0, ndiv
= resp
.ndiv_min
;
673 ndiv
< resp
.ndiv_max
;
674 index
++, ndiv
+= freq_table_step_size
) {
675 freq_table
[index
].driver_data
= ndiv
;
676 freq_table
[index
].frequency
= map_ndiv_to_freq(&resp
, ndiv
);
679 freq_table
[index
].driver_data
= resp
.ndiv_max
;
680 freq_table
[index
++].frequency
= map_ndiv_to_freq(&resp
, resp
.ndiv_max
);
681 freq_table
[index
].frequency
= CPUFREQ_TABLE_END
;
686 static int tegra194_cpufreq_store_physids(unsigned int cpu
, struct tegra194_cpufreq_data
*data
)
688 int num_cpus
= data
->soc
->maxcpus_per_cluster
* data
->soc
->num_clusters
;
689 u32 cpuid
, clusterid
;
692 if (cpu
> (num_cpus
- 1)) {
693 pr_err("cpufreq: wrong num of cpus or clusters in soc data\n");
697 data
->soc
->ops
->get_cpu_cluster_id(cpu
, &cpuid
, &clusterid
);
699 mpidr_id
= (clusterid
* data
->soc
->maxcpus_per_cluster
) + cpuid
;
701 data
->cpu_data
[cpu
].cpuid
= cpuid
;
702 data
->cpu_data
[cpu
].clusterid
= clusterid
;
703 data
->cpu_data
[cpu
].freq_core_reg
= SCRATCH_FREQ_CORE_REG(data
, mpidr_id
);
708 static int tegra194_cpufreq_probe(struct platform_device
*pdev
)
710 const struct tegra_cpufreq_soc
*soc
;
711 struct tegra194_cpufreq_data
*data
;
712 struct tegra_bpmp
*bpmp
;
713 struct device
*cpu_dev
;
717 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
721 soc
= of_device_get_match_data(&pdev
->dev
);
723 if (soc
->ops
&& soc
->maxcpus_per_cluster
&& soc
->num_clusters
&& soc
->refclk_delta_min
) {
726 dev_err(&pdev
->dev
, "soc data missing\n");
730 data
->bpmp_luts
= devm_kcalloc(&pdev
->dev
, data
->soc
->num_clusters
,
731 sizeof(*data
->bpmp_luts
), GFP_KERNEL
);
732 if (!data
->bpmp_luts
)
735 if (soc
->actmon_cntr_base
) {
736 /* mmio registers are used for frequency request and re-construction */
737 data
->regs
= devm_platform_ioremap_resource(pdev
, 0);
738 if (IS_ERR(data
->regs
))
739 return PTR_ERR(data
->regs
);
742 data
->cpu_data
= devm_kcalloc(&pdev
->dev
, data
->soc
->num_clusters
*
743 data
->soc
->maxcpus_per_cluster
,
744 sizeof(*data
->cpu_data
), GFP_KERNEL
);
748 platform_set_drvdata(pdev
, data
);
750 bpmp
= tegra_bpmp_get(&pdev
->dev
);
752 return PTR_ERR(bpmp
);
754 read_counters_wq
= alloc_workqueue("read_counters_wq", __WQ_LEGACY
, 1);
755 if (!read_counters_wq
) {
756 dev_err(&pdev
->dev
, "fail to create_workqueue\n");
761 for (i
= 0; i
< data
->soc
->num_clusters
; i
++) {
762 data
->bpmp_luts
[i
] = tegra_cpufreq_bpmp_read_lut(pdev
, bpmp
, i
);
763 if (IS_ERR(data
->bpmp_luts
[i
])) {
764 err
= PTR_ERR(data
->bpmp_luts
[i
]);
769 for_each_possible_cpu(cpu
) {
770 err
= tegra194_cpufreq_store_physids(cpu
, data
);
775 tegra194_cpufreq_driver
.driver_data
= data
;
777 /* Check for optional OPPv2 and interconnect paths on CPU0 to enable ICC scaling */
778 cpu_dev
= get_cpu_device(0);
784 if (dev_pm_opp_of_get_opp_desc_node(cpu_dev
)) {
785 err
= dev_pm_opp_of_find_icc_paths(cpu_dev
, NULL
);
787 data
->icc_dram_bw_scaling
= true;
790 err
= cpufreq_register_driver(&tegra194_cpufreq_driver
);
795 tegra194_cpufreq_free_resources();
797 tegra_bpmp_put(bpmp
);
801 static void tegra194_cpufreq_remove(struct platform_device
*pdev
)
803 cpufreq_unregister_driver(&tegra194_cpufreq_driver
);
804 tegra194_cpufreq_free_resources();
807 static const struct of_device_id tegra194_cpufreq_of_match
[] = {
808 { .compatible
= "nvidia,tegra194-ccplex", .data
= &tegra194_cpufreq_soc
},
809 { .compatible
= "nvidia,tegra234-ccplex-cluster", .data
= &tegra234_cpufreq_soc
},
810 { .compatible
= "nvidia,tegra239-ccplex-cluster", .data
= &tegra239_cpufreq_soc
},
813 MODULE_DEVICE_TABLE(of
, tegra194_cpufreq_of_match
);
815 static struct platform_driver tegra194_ccplex_driver
= {
817 .name
= "tegra194-cpufreq",
818 .of_match_table
= tegra194_cpufreq_of_match
,
820 .probe
= tegra194_cpufreq_probe
,
821 .remove_new
= tegra194_cpufreq_remove
,
823 module_platform_driver(tegra194_ccplex_driver
);
825 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
826 MODULE_AUTHOR("Sumit Gupta <sumitg@nvidia.com>");
827 MODULE_DESCRIPTION("NVIDIA Tegra194 cpufreq driver");
828 MODULE_LICENSE("GPL v2");