Skip to contentSkip to conversation

Add cpufreq driver for Tegra194

PATCH Superseded Sumit Gupta 3 participants 12 messages Jun 21, 2020 – Jun 23, 2020

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add cpufreq driver for Tegra194

The patch series adds cpufreq driver for Tegra194 SOC.

v2[2] -> v3
- Set same policy for all cpus in a cluster[Viresh].
- Add compatible string for CPU Complex under cpus node[Thierry].
- Add reference to bpmp node under cpus node[Thierry].
- Bind cpufreq driver to CPU Complex compatible string[Thierry].
- Remove patch to get bpmp data as now using cpus node to get that[Thierry].

v1[1] -> v2:
- Remove cpufreq_lock mutex from tegra194_cpufreq_set_target [Viresh].
- Remove CPUFREQ_ASYNC_NOTIFICATION flag [Viresh].
- Remove redundant _begin|end() call from tegra194_cpufreq_set_target.
- Rename opp_table to freq_table [Viresh].

Sumit Gupta (4):
dt-bindings: arm: Add t194 ccplex compatible and bpmp property
arm64: tegra: Add t194 ccplex compatible and bpmp property
cpufreq: Add Tegra194 cpufreq driver
arm64: defconfig: Enable CONFIG_ARM_TEGRA194_CPUFREQ

 Documentation/devicetree/bindings/arm/cpus.yaml |   9 +
 arch/arm64/boot/dts/nvidia/tegra194.dtsi        |   2 +
 arch/arm64/configs/defconfig                    |   1 +
 drivers/cpufreq/Kconfig.arm                     |   6 +
 drivers/cpufreq/Makefile                        |   1 +
 drivers/cpufreq/tegra194-cpufreq.c              | 403 ++++++++++++++++++++++++
 6 files changed, 422 insertions(+)
 create mode 100644 drivers/cpufreq/tegra194-cpufreq.c

[1] https://marc.info/?t=157539452300001&r=1&w=2
[2] https://marc.info/?l=linux-tegra&m=158602857106213&w=2

Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 1/4 dt-bindings: arm: Add t194 ccplex compatible and bpmp property

To do frequency scaling on all CPUs within T194 CPU Complex, we need to query BPMP for data on valid operating points. Document a compatible string under 'cpus' node to represent the CPU Complex for binding drivers like cpufreq which don't have their node or CPU Complex node to bind to. Also, document a property to point to the BPMP device that can be queried for all CPUs.

---
 Documentation/devicetree/bindings/arm/cpus.yaml | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/arm/cpus.yaml +9 -0
index a018147..737b55e 100644
@@ -162,6 +162,7 @@ properties:
       - nvidia,tegra132-denver
       - nvidia,tegra186-denver
       - nvidia,tegra194-carmel
+      - nvidia,tegra194-ccplex
       - qcom,krait
       - qcom,kryo
       - qcom,kryo260
@@ -255,6 +256,14 @@ properties:
 
       where voltage is in V, frequency is in MHz.
 
+  nvidia,bpmp:
+    $ref: '/schemas/types.yaml#/definitions/phandle'
+    descrption: |
+      Specifies the bpmp node that needs to be queried to get
+      operating point data for all CPUs.
+
+      Optional for NVIDIA Tegra194 Carmel CPUs
+
   power-domains:
     $ref: '/schemas/types.yaml#/definitions/phandle-array'
     description:
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 1/4] dt-bindings: arm: Add t194 ccplex compatible and bpmp property

Quoted 6 lines · Sumit Gupta

To do frequency scaling on all CPUs within T194 CPU Complex, we need to query BPMP for data on valid operating points. Document a compatible string under 'cpus' node to represent the CPU Complex for binding drivers like cpufreq which don't have their node or CPU Complex node to bind to. Also, document a property to point to the BPMP device that can be queried for all CPUs.

You shouldn't be putting how linux is going to use this information and entries shouldn't be made just so cpufreq can bind to a driver.

Though I see that this is a real hardware register which you can use to interact with the firmware ? And so it makes sense to have it, maybe in different form though.

I will let Rob explain what would be the right way of doing this though.

Quoted 35 lines
---
 Documentation/devicetree/bindings/arm/cpus.yaml | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/arm/cpus.yaml +9 -0
index a018147..737b55e 100644
@@ -162,6 +162,7 @@ properties:
       - nvidia,tegra132-denver
       - nvidia,tegra186-denver
       - nvidia,tegra194-carmel
+      - nvidia,tegra194-ccplex
       - qcom,krait
       - qcom,kryo
       - qcom,kryo260
@@ -255,6 +256,14 @@ properties:
 
       where voltage is in V, frequency is in MHz.
 
+  nvidia,bpmp:
+    $ref: '/schemas/types.yaml#/definitions/phandle'
+    descrption: |
+      Specifies the bpmp node that needs to be queried to get
+      operating point data for all CPUs.
+
+      Optional for NVIDIA Tegra194 Carmel CPUs
+
   power-domains:
     $ref: '/schemas/types.yaml#/definitions/phandle-array'
     description:
Signature · 1 line
2.7.4
Signature · 1 line
viresh
#3 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 1/4] dt-bindings: arm: Add t194 ccplex compatible and bpmp property

Quoted 11 lines · Viresh Kumar

External email: Use caution opening links or attachments

Quoted 6 lines · Sumit Gupta

To do frequency scaling on all CPUs within T194 CPU Complex, we need to query BPMP for data on valid operating points. Document a compatible string under 'cpus' node to represent the CPU Complex for binding drivers like cpufreq which don't have their node or CPU Complex node to bind to. Also, document a property to point to the BPMP device that can be queried for all CPUs.

Quoted 7 lines

You shouldn't be putting how linux is going to use this information and entries shouldn't be made just so cpufreq can bind to a driver.

Though I see that this is a real hardware register which you can use to interact with the firmware ? And so it makes sense to have it, maybe in different form though.

CPUFREQ driver doesn't communicate directly with BPMP firmware. It uses BPMP node's reference to call api's exported by BPMP driver which communicates with BPMP firmware.

Quoted 2 lines

I will let Rob explain what would be the right way of doing this though.

This is already discussed by Thierry with Rob.
Please refer https://marc.info/?l=linux-arm-kernel&m=158999171528418&w=2

Quoted 39 lines
Quoted 34 lines
---
  Documentation/devicetree/bindings/arm/cpus.yaml | 9 +++++++++
  1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/arm/cpus.yaml +9 -1
index a018147..737b55e 100644
@@ -162,6 +162,7 @@ properties:
        - nvidia,tegra132-denver
        - nvidia,tegra186-denver
        - nvidia,tegra194-carmel
+      - nvidia,tegra194-ccplex
        - qcom,krait
        - qcom,kryo
        - qcom,kryo260
@@ -255,6 +256,14 @@ properties:
 
        where voltage is in V, frequency is in MHz.
 
+  nvidia,bpmp:
+    $ref: '/schemas/types.yaml#/definitions/phandle'
+    descrption: |
+      Specifies the bpmp node that needs to be queried to get
+      operating point data for all CPUs.
+
+      Optional for NVIDIA Tegra194 Carmel CPUs
+
    power-domains:
      $ref: '/schemas/types.yaml#/definitions/phandle-array'
      description:
--
Quoted 1 line

2.7.4

--
viresh

#4 lore raw

PATCH v3 2/4 arm64: tegra: Add t194 ccplex compatible and bpmp property

In reply to Sumit Gupta

On Tegra194, data on valid operating points for the CPUs needs to be queried from BPMP. In T194, there is no node representing CPU complex. So, add compatible string to the 'cpus' node instead of using dummy node to bind cpufreq driver. Also, add reference to the BPMP instance for the CPU complex.

---
 arch/arm64/boot/dts/nvidia/tegra194.dtsi | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/boot/dts/nvidia/tegra194.dtsi +2 -0
index 7c9511a..0abf287 100644
@@ -1764,6 +1764,8 @@
 	};
 
 	cpus {
+		compatible = "nvidia,tegra194-ccplex";
+		nvidia,bpmp = <&bpmp>;
 		#address-cells = <1>;
 		#size-cells = <0>;
 
Signature · 1 line
2.7.4
#5 lore raw

PATCH v3 3/4 cpufreq: Add Tegra194 cpufreq driver

In reply to Sumit Gupta

Add support for CPU frequency scaling on Tegra194. The frequency of each core can be adjusted by writing a clock divisor value to a MSR on the core. The range of valid divisors is queried from the BPMP.

---
 drivers/cpufreq/Kconfig.arm        |   6 +
 drivers/cpufreq/Makefile           |   1 +
 drivers/cpufreq/tegra194-cpufreq.c | 403 +++++++++++++++++++++++++++++++++++++
 3 files changed, 410 insertions(+)
 create mode 100644 drivers/cpufreq/tegra194-cpufreq.c
drivers/cpufreq/Kconfig.arm +6 -0
index 15c1a12..f3d8f09 100644
@@ -314,6 +314,12 @@ config ARM_TEGRA186_CPUFREQ
 	help
 	  This adds the CPUFreq driver support for Tegra186 SOCs.
 
+config ARM_TEGRA194_CPUFREQ
+	tristate "Tegra194 CPUFreq support"
+	depends on ARCH_TEGRA && TEGRA_BPMP
+	help
+	  This adds CPU frequency driver support for Tegra194 SOCs.
+
 config ARM_TI_CPUFREQ
 	bool "Texas Instruments CPUFreq support"
 	depends on ARCH_OMAP2PLUS
drivers/cpufreq/Makefile +1 -0
index f6670c4..66b5563 100644
@@ -83,6 +83,7 @@ obj-$(CONFIG_ARM_TANGO_CPUFREQ)		+= tango-cpufreq.o
 obj-$(CONFIG_ARM_TEGRA20_CPUFREQ)	+= tegra20-cpufreq.o
 obj-$(CONFIG_ARM_TEGRA124_CPUFREQ)	+= tegra124-cpufreq.o
 obj-$(CONFIG_ARM_TEGRA186_CPUFREQ)	+= tegra186-cpufreq.o
+obj-$(CONFIG_ARM_TEGRA194_CPUFREQ)	+= tegra194-cpufreq.o
 obj-$(CONFIG_ARM_TI_CPUFREQ)		+= ti-cpufreq.o
 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ)	+= vexpress-spc-cpufreq.o
 
drivers/cpufreq/tegra194-cpufreq.c new file +403 -0

403 lines not on this page · raw message

Signature · 1 line
2.7.4
#6 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 3/4] cpufreq: Add Tegra194 cpufreq driver

Thank you for the patch! Perhaps something to improve:

Hi Sumit,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on pm/linux-next]
[also build test WARNING on robh/for-next arm64/for-next/core v5.8-rc2 next-20200621] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sumit-Gupta/Add-cpufreq-driver-for-Tegra194/20200622-053622
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: arm64-allyesconfig (attached as .config)
compiler: aarch64-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/cpufreq/tegra194-cpufreq.c:86:5: warning: no previous prototype for 'map_freq_to_ndiv' [-Wmissing-prototypes]

86 | u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)
| ^~~~~~~~~~~~~~~~

vim +/map_freq_to_ndiv +86 drivers/cpufreq/tegra194-cpufreq.c

    85	
  > 86	u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)
    87	{
    88		return DIV_ROUND_UP(freq * nltbl->pdiv * nltbl->mdiv,
    89				    nltbl->ref_clk_hz / KHZ);
    90	}
    91	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 3/4] cpufreq: Add Tegra194 cpufreq driver

Hi,

Thank you for the report.

Quoted 15 lines

Hi Sumit,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on pm/linux-next]
[also build test WARNING on robh/for-next arm64/for-next/core v5.8-rc2 next-20200621] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Sumit-Gupta/Add-cpufreq-driver-for-Tegra194/20200622-053622
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: arm64-allyesconfig (attached as .config)
compiler: aarch64-linux-gcc (GCC) 9.3.0
Quoted 8 lines

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

Sure.

Quoted 9 lines

All warnings (new ones prefixed by >>, old ones prefixed by <<):

Quoted 1 line
Quoted 1 line

drivers/cpufreq/tegra194-cpufreq.c:86:5: warning: no previous prototype for 'map_freq_to_ndiv' [-Wmissing-prototypes]

86 | u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)
| ^~~~~~~~~~~~~~~~

vim +/map_freq_to_ndiv +86 drivers/cpufreq/tegra194-cpufreq.c

Quoted 8 lines
     85
Quoted 1 line

86 u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)

     87  {
     88          return DIV_ROUND_UP(freq * nltbl->pdiv * nltbl->mdiv,
     89                              nltbl->ref_clk_hz / KHZ);
     90  }
     91

will take care of this in next version.

Quoted 4 lines

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#8 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 3/4] cpufreq: Add Tegra194 cpufreq driver

In reply to Sumit Gupta

drivers/cpufreq/tegra194-cpufreq.c line 3

Quoted 9 lines · Sumit Gupta
drivers/cpufreq/tegra194-cpufreq.c new file +3 -0
new file mode 100644
index 0000000..8de8000
@@ -0,0 +1,403 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved
                    2020
Quoted 19 lines
drivers/cpufreq/tegra194-cpufreq.c new file +19 -0
+ */
+
+#include <linux/cpu.h>
+#include <linux/cpufreq.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#include <asm/smp_plat.h>
+
+#include <soc/tegra/bpmp.h>
+#include <soc/tegra/bpmp-abi.h>
+
+#define KHZ                     1000
+#define REF_CLK_MHZ             408 /* 408 MHz */

drivers/cpufreq/tegra194-cpufreq.c line 32

Quoted 10 lines
drivers/cpufreq/tegra194-cpufreq.c new file +10 -0
+#define US_DELAY                500
+#define US_DELAY_MIN            2
+#define CPUFREQ_TBL_STEP_HZ     (50 * KHZ * KHZ)
+#define MAX_CNT                 ~0U
+
+/* cpufreq transisition latency */
+#define TEGRA_CPUFREQ_TRANSITION_LATENCY (300 * 1000) /* unit in nanoseconds */
+
+#define LOOP_FOR_EACH_CPU_OF_CLUSTER(cl) for (cpu = (cl * 2); \
+					cpu < ((cl + 1) * 2); cpu++)

Both latency and this loop are used only once in the code, maybe just open code it. Also you should have passed cpu as a parameter to the macro, even if it works fine without it, for better readability.

drivers/cpufreq/tegra194-cpufreq.c line 34

Quoted 2 lines
drivers/cpufreq/tegra194-cpufreq.c new file +2 -0
+
+u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)

Unused routine

Quoted 22 lines
drivers/cpufreq/tegra194-cpufreq.c new file +21 -0
+{
+	return DIV_ROUND_UP(freq * nltbl->pdiv * nltbl->mdiv,
+			    nltbl->ref_clk_hz / KHZ);
+}
 
+static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
+{
+	struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
+	int cl = get_cpu_cluster(policy->cpu);
+	u32 cpu;
+
+	if (cl >= data->num_clusters)
+		return -EINVAL;
+
+	policy->cur = tegra194_fast_get_speed(policy->cpu); /* boot freq */
+
+	/* set same policy for all cpus in a cluster */
+	LOOP_FOR_EACH_CPU_OF_CLUSTER(cl)
+		cpumask_set_cpu(cpu, policy->cpus);
+
+	policy->freq_table = data->tables[cl];
+	policy->cpuinfo.transition_latency = TEGRA_CPUFREQ_TRANSITION_LATENCY;

drivers/cpufreq/tegra194-cpufreq.c line 235

Quoted 10 lines
drivers/cpufreq/tegra194-cpufreq.c new file +9 -0
+
+	return 0;
+}
 
+static int tegra194_cpufreq_set_target(struct cpufreq_policy *policy,
+				       unsigned int index)
+{
+	struct cpufreq_frequency_table *tbl = policy->freq_table + index;
+
+	on_each_cpu_mask(policy->cpus, set_cpu_ndiv, tbl, true);

I am still a bit confused. While setting the frequency you are calling this routine for each CPU of the policy (cluster). Does that mean that CPUs within a cluster can actually run at different frequencies at any given point of time ?

If cpufreq terms, a cpufreq policy represents a group of CPUs that change frequency together, i.e. they share the clk line. If all CPUs in your system can do DVFS separately, then you must have policy per CPU, instead of cluster.

drivers/cpufreq/tegra194-cpufreq.c line 253

Quoted 3 lines
drivers/cpufreq/tegra194-cpufreq.c new file +3 -0
+static void tegra194_cpufreq_free_resources(void)
+{
+	flush_workqueue(read_counters_wq);

Why is this required exactly? I see that you add the work request and immediately flush it, then why would you need to do this separately ?

Quoted 16 lines
drivers/cpufreq/tegra194-cpufreq.c new file +16 -0
+	destroy_workqueue(read_counters_wq);
+}
+
+static struct cpufreq_frequency_table *
+init_freq_table(struct platform_device *pdev, struct tegra_bpmp *bpmp,
+		unsigned int cluster_id)
+{
+	struct cpufreq_frequency_table *freq_table;
+	struct mrq_cpu_ndiv_limits_response resp;
+	unsigned int num_freqs, ndiv, delta_ndiv;
+	struct mrq_cpu_ndiv_limits_request req;
+	struct tegra_bpmp_message msg;
+	u16 freq_table_step_size;
+	int err, index;
+
+	memset(&req, 0, sizeof(req));

drivers/cpufreq/tegra194-cpufreq.c line 279

Quoted 10 lines
drivers/cpufreq/tegra194-cpufreq.c new file +10 -0
+	req.cluster_id = cluster_id;
+
+	memset(&msg, 0, sizeof(msg));
+	msg.mrq = MRQ_CPU_NDIV_LIMITS;
+	msg.tx.data = &req;
+	msg.tx.size = sizeof(req);
+	msg.rx.data = &resp;
+	msg.rx.size = sizeof(resp);
+
+	err = tegra_bpmp_transfer(bpmp, &msg);

So the firmware can actually return different frequency tables for the clusters, right ? Else you could have received the table only once and used it for all the CPUs.

Quoted 41 lines
drivers/cpufreq/tegra194-cpufreq.c new file +41 -0
+	if (err)
+		return ERR_PTR(err);
+
+	/*
+	 * Make sure frequency table step is a multiple of mdiv to match
+	 * vhint table granularity.
+	 */
+	freq_table_step_size = resp.mdiv *
+			DIV_ROUND_UP(CPUFREQ_TBL_STEP_HZ, resp.ref_clk_hz);
+
+	dev_dbg(&pdev->dev, "cluster %d: frequency table step size: %d\n",
+		cluster_id, freq_table_step_size);
+
+	delta_ndiv = resp.ndiv_max - resp.ndiv_min;
+
+	if (unlikely(delta_ndiv == 0))
+		num_freqs = 1;
+	else
+		/* We store both ndiv_min and ndiv_max hence the +1 */
+		num_freqs = delta_ndiv / freq_table_step_size + 1;
+
+	num_freqs += (delta_ndiv % freq_table_step_size) ? 1 : 0;
+
+	freq_table = devm_kcalloc(&pdev->dev, num_freqs + 1,
+				  sizeof(*freq_table), GFP_KERNEL);
+	if (!freq_table)
+		return ERR_PTR(-ENOMEM);
+
+	for (index = 0, ndiv = resp.ndiv_min;
+			ndiv < resp.ndiv_max;
+			index++, ndiv += freq_table_step_size) {
+		freq_table[index].driver_data = ndiv;
+		freq_table[index].frequency = map_ndiv_to_freq(&resp, ndiv);
+	}
+
+	freq_table[index].driver_data = resp.ndiv_max;
+	freq_table[index++].frequency = map_ndiv_to_freq(&resp, resp.ndiv_max);
+	freq_table[index].frequency = CPUFREQ_TABLE_END;
+
+	return freq_table;
+}
Signature · 1 line
viresh
#9 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 3/4] cpufreq: Add Tegra194 cpufreq driver

Hi Viresh,

Thank you for the review. please find my reply inline.

Quoted 33 lines

drivers/cpufreq/tegra194-cpufreq.c line 3

Quoted 5 lines

+++ b/drivers/cpufreq/tegra194-cpufreq.c

drivers/cpufreq/tegra194-cpufreq.c new file +3 -0
@@ -0,0 +1,403 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved
                     2020
Quoted 25 lines
drivers/cpufreq/tegra194-cpufreq.c new file +25 -0
+ */
+
+#include <linux/cpu.h>
+#include <linux/cpufreq.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#include <asm/smp_plat.h>
+
+#include <soc/tegra/bpmp.h>
+#include <soc/tegra/bpmp-abi.h>
+
+#define KHZ                     1000
+#define REF_CLK_MHZ             408 /* 408 MHz */
+#define US_DELAY                500
+#define US_DELAY_MIN            2
+#define CPUFREQ_TBL_STEP_HZ     (50 * KHZ * KHZ)
+#define MAX_CNT                 ~0U
+
+/* cpufreq transisition latency */
Quoted 9 lines

drivers/cpufreq/tegra194-cpufreq.c line 32

Quoted 4 lines
drivers/cpufreq/tegra194-cpufreq.c new file +4 -0
+#define TEGRA_CPUFREQ_TRANSITION_LATENCY (300 * 1000) /* unit in nanoseconds */
+
+#define LOOP_FOR_EACH_CPU_OF_CLUSTER(cl) for (cpu = (cl * 2); \
+                                     cpu < ((cl + 1) * 2); cpu++)

Both latency and this loop are used only once in the code, maybe just open code it. Also you should have passed cpu as a parameter to the macro, even if it works fine without it, for better readability.

Ok, i will open code the loop in next version. For latency value, i feel named macro makes readability better. So, prefer keeping it.

Quoted 5 lines

Review line 34

Quoted 2 lines
(quoted diff) +2 -0
+
+u16 map_freq_to_ndiv(struct mrq_cpu_ndiv_limits_response *nltbl, u32 freq)

Unused routine

Sure, will remove it.

Quoted 37 lines
Quoted 22 lines
drivers/cpufreq/tegra194-cpufreq.c new file +21 -0
+{
+     return DIV_ROUND_UP(freq * nltbl->pdiv * nltbl->mdiv,
+                         nltbl->ref_clk_hz / KHZ);
+}
 
+static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
+{
+     struct tegra194_cpufreq_data *data = cpufreq_get_driver_data();
+     int cl = get_cpu_cluster(policy->cpu);
+     u32 cpu;
+
+     if (cl >= data->num_clusters)
+             return -EINVAL;
+
+     policy->cur = tegra194_fast_get_speed(policy->cpu); /* boot freq */
+
+     /* set same policy for all cpus in a cluster */
+     LOOP_FOR_EACH_CPU_OF_CLUSTER(cl)
+             cpumask_set_cpu(cpu, policy->cpus);
+
+     policy->freq_table = data->tables[cl];
+     policy->cpuinfo.transition_latency = TEGRA_CPUFREQ_TRANSITION_LATENCY;

drivers/cpufreq/tegra194-cpufreq.c line 235

Quoted 10 lines
drivers/cpufreq/tegra194-cpufreq.c new file +9 -0
+
+     return 0;
+}
 
+static int tegra194_cpufreq_set_target(struct cpufreq_policy *policy,
+                                    unsigned int index)
+{
+     struct cpufreq_frequency_table *tbl = policy->freq_table + index;
+
+     on_each_cpu_mask(policy->cpus, set_cpu_ndiv, tbl, true);

I am still a bit confused. While setting the frequency you are calling this routine for each CPU of the policy (cluster). Does that mean that CPUs within a cluster can actually run at different frequencies at any given point of time ?

Quoted 4 lines

If cpufreq terms, a cpufreq policy represents a group of CPUs that change frequency together, i.e. they share the clk line. If all CPUs in your system can do DVFS separately, then you must have policy per CPU, instead of cluster.

T194 supports four CPU clusters, each with two cores. Each CPU cluster is capable of running at a specific frequency sourced by respective NAFLL to provide cluster specific clocks. Individual cores within a cluster write freq in per core register. Cluster h/w forwards the max(core0, core1) request to per cluster NAFLL.

Quoted 7 lines

drivers/cpufreq/tegra194-cpufreq.c line 253

Quoted 3 lines
drivers/cpufreq/tegra194-cpufreq.c new file +3 -0
+static void tegra194_cpufreq_free_resources(void)
+{
+     flush_workqueue(read_counters_wq);

Why is this required exactly? I see that you add the work request and immediately flush it, then why would you need to do this separately ?

Ya, will remove flush_workqueue().

Quoted 22 lines
Quoted 22 lines
drivers/cpufreq/tegra194-cpufreq.c new file +22 -0
+     destroy_workqueue(read_counters_wq);
+}
+
+static struct cpufreq_frequency_table *
+init_freq_table(struct platform_device *pdev, struct tegra_bpmp *bpmp,
+             unsigned int cluster_id)
+{
+     struct cpufreq_frequency_table *freq_table;
+     struct mrq_cpu_ndiv_limits_response resp;
+     unsigned int num_freqs, ndiv, delta_ndiv;
+     struct mrq_cpu_ndiv_limits_request req;
+     struct tegra_bpmp_message msg;
+     u16 freq_table_step_size;
+     int err, index;
+
+     memset(&req, 0, sizeof(req));
+     req.cluster_id = cluster_id;
+
+     memset(&msg, 0, sizeof(msg));
+     msg.mrq = MRQ_CPU_NDIV_LIMITS;
+     msg.tx.data = &req;
+     msg.tx.size = sizeof(req);
Quoted 9 lines

drivers/cpufreq/tegra194-cpufreq.c line 279

Quoted 4 lines
drivers/cpufreq/tegra194-cpufreq.c new file +4 -0
+     msg.rx.data = &resp;
+     msg.rx.size = sizeof(resp);
+
+     err = tegra_bpmp_transfer(bpmp, &msg);

So the firmware can actually return different frequency tables for the clusters, right ? Else you could have received the table only once and used it for all the CPUs.

Yes, frequency tables are returned per cluster by BPMP firmware. In T194 SOC, currently same table values are used for all clusters. This might change in future.

Quoted 45 lines
Quoted 31 lines
drivers/cpufreq/tegra194-cpufreq.c new file +31 -0
+     if (err)
+             return ERR_PTR(err);
+
+     /*
+      * Make sure frequency table step is a multiple of mdiv to match
+      * vhint table granularity.
+      */
+     freq_table_step_size = resp.mdiv *
+                     DIV_ROUND_UP(CPUFREQ_TBL_STEP_HZ, resp.ref_clk_hz);
+
+     dev_dbg(&pdev->dev, "cluster %d: frequency table step size: %d\n",
+             cluster_id, freq_table_step_size);
+
+     delta_ndiv = resp.ndiv_max - resp.ndiv_min;
+
+     if (unlikely(delta_ndiv == 0))
+             num_freqs = 1;
+     else
+             /* We store both ndiv_min and ndiv_max hence the +1 */
+             num_freqs = delta_ndiv / freq_table_step_size + 1;
+
+     num_freqs += (delta_ndiv % freq_table_step_size) ? 1 : 0;
+
+     freq_table = devm_kcalloc(&pdev->dev, num_freqs + 1,
+                               sizeof(*freq_table), GFP_KERNEL);
+     if (!freq_table)
+             return ERR_PTR(-ENOMEM);
+
+     for (index = 0, ndiv = resp.ndiv_min;
+                     ndiv < resp.ndiv_max;
+                     index++, ndiv += freq_table_step_size) {

drivers/cpufreq/tegra194-cpufreq.c line 320

Quoted 10 lines
drivers/cpufreq/tegra194-cpufreq.c new file +10 -0
+             freq_table[index].driver_data = ndiv;
+             freq_table[index].frequency = map_ndiv_to_freq(&resp, ndiv);
+     }
+
+     freq_table[index].driver_data = resp.ndiv_max;
+     freq_table[index++].frequency = map_ndiv_to_freq(&resp, resp.ndiv_max);
+     freq_table[index].frequency = CPUFREQ_TABLE_END;
+
+     return freq_table;
+}

--
viresh

maintainer
#10 lore raw

Re: [TEGRA194_CPUFREQ Patch v3 3/4] cpufreq: Add Tegra194 cpufreq driver

Quoted 12 lines · Sumit Gupta

Hi Viresh,

Thank you for the review. please find my reply inline.

Quoted 7 lines

drivers/cpufreq/tegra194-cpufreq.c line 3

Quoted 5 lines

+++ b/drivers/cpufreq/tegra194-cpufreq.c

drivers/cpufreq/tegra194-cpufreq.c new file +3 -0
@@ -0,0 +1,403 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved
                     2020

You missed this ?

Quoted 5 lines

T194 supports four CPU clusters, each with two cores. Each CPU cluster is capable of running at a specific frequency sourced by respective NAFLL to provide cluster specific clocks. Individual cores within a cluster write freq in per core register. Cluster h/w forwards the max(core0, core1) request to per cluster NAFLL.

Okay, this is clear now. Add a comment about this max thing in the target routine to show why you need to do this on all CPUs.

Signature · 1 line
viresh
#11 lore raw

PATCH v3 4/4 arm64: defconfig: Enable CONFIG_ARM_TEGRA194_CPUFREQ

In reply to Sumit Gupta

Enable Tegra194 CPU frequency scaling support by default.

---
 arch/arm64/configs/defconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/configs/defconfig +1 -0
index f9d378d..385bd35 100644
@@ -91,6 +91,7 @@ CONFIG_ARM_QCOM_CPUFREQ_NVMEM=y
 CONFIG_ARM_QCOM_CPUFREQ_HW=y
 CONFIG_ARM_RASPBERRYPI_CPUFREQ=m
 CONFIG_ARM_TEGRA186_CPUFREQ=y
+CONFIG_ARM_TEGRA194_CPUFREQ=y
 CONFIG_QORIQ_CPUFREQ=y
 CONFIG_ARM_SCPI_PROTOCOL=y
 CONFIG_RASPBERRYPI_FIRMWARE=y
Signature · 1 line
2.7.4