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/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.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 */
24 #define CPUFREQ_TBL_STEP_HZ (50 * KHZ * KHZ)
27 #define NDIV_MASK 0x1FF
29 #define CORE_OFFSET(cpu) (cpu * 8)
30 #define CMU_CLKS_BASE 0x2000
31 #define SCRATCH_FREQ_CORE_REG(data, cpu) (data->regs + CMU_CLKS_BASE + CORE_OFFSET(cpu))
33 #define MMCRAB_CLUSTER_BASE(cl) (0x30000 + (cl * 0x10000))
34 #define CLUSTER_ACTMON_BASE(data, cl) \
35 (data->regs + (MMCRAB_CLUSTER_BASE(cl) + data->soc->actmon_cntr_base))
36 #define CORE_ACTMON_CNTR_REG(data, cl, cpu) (CLUSTER_ACTMON_BASE(data, cl) + CORE_OFFSET(cpu))
38 /* cpufreq transisition latency */
39 #define TEGRA_CPUFREQ_TRANSITION_LATENCY (300 * 1000) /* unit in nanoseconds */
49 struct tegra_cpu_ctr {
51 u32 coreclk_cnt, last_coreclk_cnt;
52 u32 refclk_cnt, last_refclk_cnt;
55 struct read_counters_work {
56 struct work_struct work;
57 struct tegra_cpu_ctr c;
60 struct tegra_cpufreq_ops {
61 void (*read_counters)(struct tegra_cpu_ctr *c);
62 void (*set_cpu_ndiv)(struct cpufreq_policy *policy, u64 ndiv);
63 void (*get_cpu_cluster_id)(u32 cpu, u32 *cpuid, u32 *clusterid);
64 int (*get_cpu_ndiv)(u32 cpu, u32 cpuid, u32 clusterid, u64 *ndiv);
67 struct tegra_cpufreq_soc {
68 struct tegra_cpufreq_ops *ops;
69 int maxcpus_per_cluster;
70 phys_addr_t actmon_cntr_base;
73 struct tegra194_cpufreq_data {
76 struct cpufreq_frequency_table **tables;
77 const struct tegra_cpufreq_soc *soc;
80 static struct workqueue_struct *read_counters_wq;
82 static void tegra_get_cpu_mpidr(void *mpidr)
84 *((u64 *)mpidr) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
87 static void tegra234_get_cpu_cluster_id(u32 cpu, u32 *cpuid, u32 *clusterid)
91 smp_call_function_single(cpu, tegra_get_cpu_mpidr, &mpidr, true);
94 *cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
96 *clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 2);
99 static int tegra234_get_cpu_ndiv(u32 cpu, u32 cpuid, u32 clusterid, u64 *ndiv)
101 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
102 void __iomem *freq_core_reg;
105 /* use physical id to get address of per core frequency register */
106 mpidr_id = (clusterid * data->soc->maxcpus_per_cluster) + cpuid;
107 freq_core_reg = SCRATCH_FREQ_CORE_REG(data, mpidr_id);
109 *ndiv = readl(freq_core_reg) & NDIV_MASK;
114 static void tegra234_set_cpu_ndiv(struct cpufreq_policy *policy, u64 ndiv)
116 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
117 void __iomem *freq_core_reg;
118 u32 cpu, cpuid, clusterid;
121 for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
122 data->soc->ops->get_cpu_cluster_id(cpu, &cpuid, &clusterid);
124 /* use physical id to get address of per core frequency register */
125 mpidr_id = (clusterid * data->soc->maxcpus_per_cluster) + cpuid;
126 freq_core_reg = SCRATCH_FREQ_CORE_REG(data, mpidr_id);
128 writel(ndiv, freq_core_reg);
133 * This register provides access to two counter values with a single
134 * 64-bit read. The counter values are used to determine the average
135 * actual frequency a core has run at over a period of time.
136 * [63:32] PLLP counter: Counts at fixed frequency (408 MHz)
137 * [31:0] Core clock counter: Counts on every core clock cycle
139 static void tegra234_read_counters(struct tegra_cpu_ctr *c)
141 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
142 void __iomem *actmon_reg;
143 u32 cpuid, clusterid;
146 data->soc->ops->get_cpu_cluster_id(c->cpu, &cpuid, &clusterid);
147 actmon_reg = CORE_ACTMON_CNTR_REG(data, clusterid, cpuid);
149 val = readq(actmon_reg);
150 c->last_refclk_cnt = upper_32_bits(val);
151 c->last_coreclk_cnt = lower_32_bits(val);
153 val = readq(actmon_reg);
154 c->refclk_cnt = upper_32_bits(val);
155 c->coreclk_cnt = lower_32_bits(val);
158 static struct tegra_cpufreq_ops tegra234_cpufreq_ops = {
159 .read_counters = tegra234_read_counters,
160 .get_cpu_cluster_id = tegra234_get_cpu_cluster_id,
161 .get_cpu_ndiv = tegra234_get_cpu_ndiv,
162 .set_cpu_ndiv = tegra234_set_cpu_ndiv,
165 const struct tegra_cpufreq_soc tegra234_cpufreq_soc = {
166 .ops = &tegra234_cpufreq_ops,
167 .actmon_cntr_base = 0x9000,
168 .maxcpus_per_cluster = 4,
171 static void tegra194_get_cpu_cluster_id(u32 cpu, u32 *cpuid, u32 *clusterid)
175 smp_call_function_single(cpu, tegra_get_cpu_mpidr, &mpidr, true);
178 *cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
180 *clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
184 * Read per-core Read-only system register NVFREQ_FEEDBACK_EL1.
185 * The register provides frequency feedback information to
186 * determine the average actual frequency a core has run at over
188 * [31:0] PLLP counter: Counts at fixed frequency (408 MHz)
189 * [63:32] Core clock counter: counts on every core clock cycle
190 * where the core is architecturally clocking
192 static u64 read_freq_feedback(void)
196 asm volatile("mrs %0, s3_0_c15_c0_5" : "=r" (val) : );
201 static inline u32 map_ndiv_to_freq(struct mrq_cpu_ndiv_limits_response
204 return nltbl->ref_clk_hz / KHZ * ndiv / (nltbl->pdiv * nltbl->mdiv);
207 static void tegra194_read_counters(struct tegra_cpu_ctr *c)
211 val = read_freq_feedback();
212 c->last_refclk_cnt = lower_32_bits(val);
213 c->last_coreclk_cnt = upper_32_bits(val);
215 val = read_freq_feedback();
216 c->refclk_cnt = lower_32_bits(val);
217 c->coreclk_cnt = upper_32_bits(val);
220 static void tegra_read_counters(struct work_struct *work)
222 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
223 struct read_counters_work *read_counters_work;
224 struct tegra_cpu_ctr *c;
227 * ref_clk_counter(32 bit counter) runs on constant clk,
229 * It will take = 2 ^ 32 / 408 MHz to overflow ref clk counter
230 * = 10526880 usec = 10.527 sec to overflow
232 * Like wise core_clk_counter(32 bit counter) runs on core clock.
233 * It's synchronized to crab_clk (cpu_crab_clk) which runs at
234 * freq of cluster. Assuming max cluster clock ~2000MHz,
235 * It will take = 2 ^ 32 / 2000 MHz to overflow core clk counter
236 * = ~2.147 sec to overflow
238 read_counters_work = container_of(work, struct read_counters_work,
240 c = &read_counters_work->c;
242 data->soc->ops->read_counters(c);
246 * Return instantaneous cpu speed
247 * Instantaneous freq is calculated as -
248 * -Takes sample on every query of getting the freq.
249 * - Read core and ref clock counters;
251 * - Read above cycle counters again
252 * - Calculates freq by subtracting current and previous counters
253 * divided by the delay time or eqv. of ref_clk_counter in delta time
254 * - Return Kcycles/second, freq in KHz
256 * delta time period = x sec
257 * = delta ref_clk_counter / (408 * 10^6) sec
258 * freq in Hz = cycles/sec
259 * = (delta cycles / x sec
260 * = (delta cycles * 408 * 10^6) / delta ref_clk_counter
261 * in KHz = (delta cycles * 408 * 10^3) / delta ref_clk_counter
263 * @cpu - logical cpu whose freq to be updated
264 * Returns freq in KHz on success, 0 if cpu is offline
266 static unsigned int tegra194_calculate_speed(u32 cpu)
268 struct read_counters_work read_counters_work;
269 struct tegra_cpu_ctr c;
275 * udelay() is required to reconstruct cpu frequency over an
276 * observation window. Using workqueue to call udelay() with
277 * interrupts enabled.
279 read_counters_work.c.cpu = cpu;
280 INIT_WORK_ONSTACK(&read_counters_work.work, tegra_read_counters);
281 queue_work_on(cpu, read_counters_wq, &read_counters_work.work);
282 flush_work(&read_counters_work.work);
283 c = read_counters_work.c;
285 if (c.coreclk_cnt < c.last_coreclk_cnt)
286 delta_ccnt = c.coreclk_cnt + (MAX_CNT - c.last_coreclk_cnt);
288 delta_ccnt = c.coreclk_cnt - c.last_coreclk_cnt;
292 /* ref clock is 32 bits */
293 if (c.refclk_cnt < c.last_refclk_cnt)
294 delta_refcnt = c.refclk_cnt + (MAX_CNT - c.last_refclk_cnt);
296 delta_refcnt = c.refclk_cnt - c.last_refclk_cnt;
298 pr_debug("cpufreq: %d is idle, delta_refcnt: 0\n", cpu);
301 rate_mhz = ((unsigned long)(delta_ccnt * REF_CLK_MHZ)) / delta_refcnt;
303 return (rate_mhz * KHZ); /* in KHz */
306 static void tegra194_get_cpu_ndiv_sysreg(void *ndiv)
310 asm volatile("mrs %0, s3_0_c15_c0_4" : "=r" (ndiv_val) : );
312 *(u64 *)ndiv = ndiv_val;
315 static int tegra194_get_cpu_ndiv(u32 cpu, u32 cpuid, u32 clusterid, u64 *ndiv)
319 ret = smp_call_function_single(cpu, tegra194_get_cpu_ndiv_sysreg, &ndiv, true);
324 static void tegra194_set_cpu_ndiv_sysreg(void *data)
326 u64 ndiv_val = *(u64 *)data;
328 asm volatile("msr s3_0_c15_c0_4, %0" : : "r" (ndiv_val));
331 static void tegra194_set_cpu_ndiv(struct cpufreq_policy *policy, u64 ndiv)
333 on_each_cpu_mask(policy->cpus, tegra194_set_cpu_ndiv_sysreg, &ndiv, true);
336 static unsigned int tegra194_get_speed(u32 cpu)
338 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
339 struct cpufreq_frequency_table *pos;
340 u32 cpuid, clusterid;
345 data->soc->ops->get_cpu_cluster_id(cpu, &cpuid, &clusterid);
347 /* reconstruct actual cpu freq using counters */
348 rate = tegra194_calculate_speed(cpu);
350 /* get last written ndiv value */
351 ret = data->soc->ops->get_cpu_ndiv(cpu, cpuid, clusterid, &ndiv);
352 if (WARN_ON_ONCE(ret))
356 * If the reconstructed frequency has acceptable delta from
357 * the last written value, then return freq corresponding
358 * to the last written ndiv value from freq_table. This is
359 * done to return consistent value.
361 cpufreq_for_each_valid_entry(pos, data->tables[clusterid]) {
362 if (pos->driver_data != ndiv)
365 if (abs(pos->frequency - rate) > 115200) {
366 pr_warn("cpufreq: cpu%d,cur:%u,set:%u,set ndiv:%llu\n",
367 cpu, rate, pos->frequency, ndiv);
369 rate = pos->frequency;
376 static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
378 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
379 int maxcpus_per_cluster = data->soc->maxcpus_per_cluster;
383 data->soc->ops->get_cpu_cluster_id(policy->cpu, NULL, &clusterid);
385 if (clusterid >= data->num_clusters || !data->tables[clusterid])
388 start_cpu = rounddown(policy->cpu, maxcpus_per_cluster);
389 /* set same policy for all cpus in a cluster */
390 for (cpu = start_cpu; cpu < (start_cpu + maxcpus_per_cluster); cpu++) {
391 if (cpu_possible(cpu))
392 cpumask_set_cpu(cpu, policy->cpus);
394 policy->freq_table = data->tables[clusterid];
395 policy->cpuinfo.transition_latency = TEGRA_CPUFREQ_TRANSITION_LATENCY;
400 static int tegra194_cpufreq_set_target(struct cpufreq_policy *policy,
403 struct cpufreq_frequency_table *tbl = policy->freq_table + index;
404 struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
407 * Each core writes frequency in per core register. Then both cores
408 * in a cluster run at same frequency which is the maximum frequency
409 * request out of the values requested by both cores in that cluster.
411 data->soc->ops->set_cpu_ndiv(policy, (u64)tbl->driver_data);
416 static struct cpufreq_driver tegra194_cpufreq_driver = {
418 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
419 .verify = cpufreq_generic_frequency_table_verify,
420 .target_index = tegra194_cpufreq_set_target,
421 .get = tegra194_get_speed,
422 .init = tegra194_cpufreq_init,
423 .attr = cpufreq_generic_attr,
426 static struct tegra_cpufreq_ops tegra194_cpufreq_ops = {
427 .read_counters = tegra194_read_counters,
428 .get_cpu_cluster_id = tegra194_get_cpu_cluster_id,
429 .get_cpu_ndiv = tegra194_get_cpu_ndiv,
430 .set_cpu_ndiv = tegra194_set_cpu_ndiv,
433 const struct tegra_cpufreq_soc tegra194_cpufreq_soc = {
434 .ops = &tegra194_cpufreq_ops,
435 .maxcpus_per_cluster = 2,
438 static void tegra194_cpufreq_free_resources(void)
440 destroy_workqueue(read_counters_wq);
443 static struct cpufreq_frequency_table *
444 init_freq_table(struct platform_device *pdev, struct tegra_bpmp *bpmp,
445 unsigned int cluster_id)
447 struct cpufreq_frequency_table *freq_table;
448 struct mrq_cpu_ndiv_limits_response resp;
449 unsigned int num_freqs, ndiv, delta_ndiv;
450 struct mrq_cpu_ndiv_limits_request req;
451 struct tegra_bpmp_message msg;
452 u16 freq_table_step_size;
455 memset(&req, 0, sizeof(req));
456 req.cluster_id = cluster_id;
458 memset(&msg, 0, sizeof(msg));
459 msg.mrq = MRQ_CPU_NDIV_LIMITS;
461 msg.tx.size = sizeof(req);
463 msg.rx.size = sizeof(resp);
465 err = tegra_bpmp_transfer(bpmp, &msg);
468 if (msg.rx.ret == -BPMP_EINVAL) {
469 /* Cluster not available */
473 return ERR_PTR(-EINVAL);
476 * Make sure frequency table step is a multiple of mdiv to match
477 * vhint table granularity.
479 freq_table_step_size = resp.mdiv *
480 DIV_ROUND_UP(CPUFREQ_TBL_STEP_HZ, resp.ref_clk_hz);
482 dev_dbg(&pdev->dev, "cluster %d: frequency table step size: %d\n",
483 cluster_id, freq_table_step_size);
485 delta_ndiv = resp.ndiv_max - resp.ndiv_min;
487 if (unlikely(delta_ndiv == 0)) {
490 /* We store both ndiv_min and ndiv_max hence the +1 */
491 num_freqs = delta_ndiv / freq_table_step_size + 1;
494 num_freqs += (delta_ndiv % freq_table_step_size) ? 1 : 0;
496 freq_table = devm_kcalloc(&pdev->dev, num_freqs + 1,
497 sizeof(*freq_table), GFP_KERNEL);
499 return ERR_PTR(-ENOMEM);
501 for (index = 0, ndiv = resp.ndiv_min;
502 ndiv < resp.ndiv_max;
503 index++, ndiv += freq_table_step_size) {
504 freq_table[index].driver_data = ndiv;
505 freq_table[index].frequency = map_ndiv_to_freq(&resp, ndiv);
508 freq_table[index].driver_data = resp.ndiv_max;
509 freq_table[index++].frequency = map_ndiv_to_freq(&resp, resp.ndiv_max);
510 freq_table[index].frequency = CPUFREQ_TABLE_END;
515 static int tegra194_cpufreq_probe(struct platform_device *pdev)
517 const struct tegra_cpufreq_soc *soc;
518 struct tegra194_cpufreq_data *data;
519 struct tegra_bpmp *bpmp;
522 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
526 soc = of_device_get_match_data(&pdev->dev);
528 if (soc->ops && soc->maxcpus_per_cluster) {
531 dev_err(&pdev->dev, "soc data missing\n");
535 data->num_clusters = MAX_CLUSTERS;
536 data->tables = devm_kcalloc(&pdev->dev, data->num_clusters,
537 sizeof(*data->tables), GFP_KERNEL);
541 if (soc->actmon_cntr_base) {
542 /* mmio registers are used for frequency request and re-construction */
543 data->regs = devm_platform_ioremap_resource(pdev, 0);
544 if (IS_ERR(data->regs))
545 return PTR_ERR(data->regs);
548 platform_set_drvdata(pdev, data);
550 bpmp = tegra_bpmp_get(&pdev->dev);
552 return PTR_ERR(bpmp);
554 read_counters_wq = alloc_workqueue("read_counters_wq", __WQ_LEGACY, 1);
555 if (!read_counters_wq) {
556 dev_err(&pdev->dev, "fail to create_workqueue\n");
561 for (i = 0; i < data->num_clusters; i++) {
562 data->tables[i] = init_freq_table(pdev, bpmp, i);
563 if (IS_ERR(data->tables[i])) {
564 err = PTR_ERR(data->tables[i]);
569 tegra194_cpufreq_driver.driver_data = data;
571 err = cpufreq_register_driver(&tegra194_cpufreq_driver);
576 tegra194_cpufreq_free_resources();
578 tegra_bpmp_put(bpmp);
582 static int tegra194_cpufreq_remove(struct platform_device *pdev)
584 cpufreq_unregister_driver(&tegra194_cpufreq_driver);
585 tegra194_cpufreq_free_resources();
590 static const struct of_device_id tegra194_cpufreq_of_match[] = {
591 { .compatible = "nvidia,tegra194-ccplex", .data = &tegra194_cpufreq_soc },
592 { .compatible = "nvidia,tegra234-ccplex-cluster", .data = &tegra234_cpufreq_soc },
596 static struct platform_driver tegra194_ccplex_driver = {
598 .name = "tegra194-cpufreq",
599 .of_match_table = tegra194_cpufreq_of_match,
601 .probe = tegra194_cpufreq_probe,
602 .remove = tegra194_cpufreq_remove,
604 module_platform_driver(tegra194_ccplex_driver);
606 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
607 MODULE_AUTHOR("Sumit Gupta <sumitg@nvidia.com>");
608 MODULE_DESCRIPTION("NVIDIA Tegra194 cpufreq driver");
609 MODULE_LICENSE("GPL v2");