[RFC PATCH v3 33/37] kvx: Add support for cpuinfo

ysionneau@kalrayinc.com posted 37 patches 1 year, 1 month ago
[RFC PATCH v3 33/37] kvx: Add support for cpuinfo
Posted by ysionneau@kalrayinc.com 1 year, 1 month ago
From: Yann Sionneau <ysionneau@kalrayinc.com>

Add support for cpuinfo on kvx arch.

Co-developed-by: Clement Leger <clement@clement-leger.fr>
Signed-off-by: Clement Leger <clement@clement-leger.fr>
Co-developed-by: Guillaume Thouvenin <thouveng@gmail.com>
Signed-off-by: Guillaume Thouvenin <thouveng@gmail.com>
Co-developed-by: Julian Vetter <jvetter@kalrayinc.com>
Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
Signed-off-by: Jules Maselbas <jmaselbas@zdiv.net>
Signed-off-by: Yann Sionneau <ysionneau@kalrayinc.com>
---

Notes:
V1 -> V2: No changes
V2 -> V3:
- add missing function declaration for setup_cpuinfo()
- replace printk(KERN_WARNING... with pr_warn(...
---
 arch/kvx/include/asm/cpuinfo.h |  7 +++
 arch/kvx/kernel/cpuinfo.c      | 95 ++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 arch/kvx/include/asm/cpuinfo.h
 create mode 100644 arch/kvx/kernel/cpuinfo.c

diff --git a/arch/kvx/include/asm/cpuinfo.h b/arch/kvx/include/asm/cpuinfo.h
new file mode 100644
index 0000000000000..ace9b85fbafaf
--- /dev/null
+++ b/arch/kvx/include/asm/cpuinfo.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __ASM_KVX_CPUINFO_H
+#define __ASM_KVX_CPUINFO_H
+
+extern void setup_cpuinfo(void);
+
+#endif /* __ASM_KVX_CPUINFO_H */
diff --git a/arch/kvx/kernel/cpuinfo.c b/arch/kvx/kernel/cpuinfo.c
new file mode 100644
index 0000000000000..6d46fd2a1bd93
--- /dev/null
+++ b/arch/kvx/kernel/cpuinfo.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2017-2023 Kalray Inc.
+ * Author(s): Clement Leger
+ *            Guillaume Thouvenin
+ */
+
+#include <linux/seq_file.h>
+#include <linux/delay.h>
+#include <linux/clk.h>
+#include <linux/cpu.h>
+#include <linux/of.h>
+
+unsigned long elf_hwcap __read_mostly;
+
+static int show_cpuinfo(struct seq_file *m, void *v)
+{
+	int cpu_num = *(unsigned int *)v;
+	struct cpuinfo_kvx *n = per_cpu_ptr(&cpu_info, cpu_num);
+
+	seq_printf(m, "processor\t: %d\nvendor_id\t: Kalray\n", cpu_num);
+
+	seq_printf(m,
+		   "copro enabled\t: %s\n"
+		   "arch revision\t: %d\n"
+		   "uarch revision\t: %d\n",
+		   n->copro_enable ? "yes" : "no",
+		   n->arch_rev,
+		   n->uarch_rev);
+
+	seq_printf(m,
+		   "bogomips\t: %lu.%02lu\n"
+		   "cpu MHz\t\t: %llu.%03llu\n\n",
+		   (loops_per_jiffy * HZ) / 500000,
+		   ((loops_per_jiffy * HZ) / 5000) % 100,
+		   n->freq / 1000000, (n->freq / 10000) % 100);
+
+	return 0;
+}
+
+static void *c_start(struct seq_file *m, loff_t *pos)
+{
+	if (*pos == 0)
+		*pos = cpumask_first(cpu_online_mask);
+	if (*pos >= num_online_cpus())
+		return NULL;
+
+	return pos;
+}
+
+static void *c_next(struct seq_file *m, void *v, loff_t *pos)
+{
+	*pos = cpumask_next(*pos, cpu_online_mask);
+
+	return c_start(m, pos);
+}
+
+static void c_stop(struct seq_file *m, void *v)
+{
+}
+
+const struct seq_operations cpuinfo_op = {
+	.start = c_start,
+	.next = c_next,
+	.stop = c_stop,
+	.show = show_cpuinfo,
+};
+
+static int __init setup_cpuinfo(void)
+{
+	int cpu;
+	struct clk *clk;
+	unsigned long cpu_freq = 1000000000;
+	struct device_node *node = of_get_cpu_node(0, NULL);
+
+	clk = of_clk_get(node, 0);
+	if (IS_ERR(clk)) {
+		pr_warn("Device tree missing CPU 'clock' parameter. Assuming frequency is 1GHZ");
+		goto setup_cpu_freq;
+	}
+
+	cpu_freq = clk_get_rate(clk);
+
+	clk_put(clk);
+
+setup_cpu_freq:
+	of_node_put(node);
+
+	for_each_possible_cpu(cpu)
+		per_cpu_ptr(&cpu_info, cpu)->freq = cpu_freq;
+
+	return 0;
+}
+
+late_initcall(setup_cpuinfo);
-- 
2.45.2
Re: [RFC PATCH v3 33/37] kvx: Add support for cpuinfo
Posted by Krzysztof Kozlowski 1 year, 1 month ago
On 22/07/2024 11:41, ysionneau@kalrayinc.com wrote:
> From: Yann Sionneau <ysionneau@kalrayinc.com>
> 
> Add support for cpuinfo on kvx arch.
> 
> Co-developed-by: Clement Leger <clement@clement-leger.fr>
> Signed-off-by: Clement Leger <clement@clement-leger.fr>
> Co-developed-by: Guillaume Thouvenin <thouveng@gmail.com>
> Signed-off-by: Guillaume Thouvenin <thouveng@gmail.com>
> Co-developed-by: Julian Vetter <jvetter@kalrayinc.com>
> Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
> Signed-off-by: Jules Maselbas <jmaselbas@zdiv.net>
> Signed-off-by: Yann Sionneau <ysionneau@kalrayinc.com>
> ---
> 
> Notes:
> V1 -> V2: No changes
> V2 -> V3:
> - add missing function declaration for setup_cpuinfo()
> - replace printk(KERN_WARNING... with pr_warn(...
> ---
>  arch/kvx/include/asm/cpuinfo.h |  7 +++
>  arch/kvx/kernel/cpuinfo.c      | 95 ++++++++++++++++++++++++++++++++++
>  2 files changed, 102 insertions(+)
>  create mode 100644 arch/kvx/include/asm/cpuinfo.h
>  create mode 100644 arch/kvx/kernel/cpuinfo.c
> 
> diff --git a/arch/kvx/include/asm/cpuinfo.h b/arch/kvx/include/asm/cpuinfo.h
> new file mode 100644
> index 0000000000000..ace9b85fbafaf
> --- /dev/null
> +++ b/arch/kvx/include/asm/cpuinfo.h
> @@ -0,0 +1,7 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +#ifndef __ASM_KVX_CPUINFO_H
> +#define __ASM_KVX_CPUINFO_H
> +
> +extern void setup_cpuinfo(void);
> +
> +#endif /* __ASM_KVX_CPUINFO_H */
> diff --git a/arch/kvx/kernel/cpuinfo.c b/arch/kvx/kernel/cpuinfo.c
> new file mode 100644
> index 0000000000000..6d46fd2a1bd93
> --- /dev/null
> +++ b/arch/kvx/kernel/cpuinfo.c
> @@ -0,0 +1,95 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2017-2023 Kalray Inc.
> + * Author(s): Clement Leger
> + *            Guillaume Thouvenin
> + */
> +
> +#include <linux/seq_file.h>
> +#include <linux/delay.h>
> +#include <linux/clk.h>
> +#include <linux/cpu.h>
> +#include <linux/of.h>
> +
> +unsigned long elf_hwcap __read_mostly;
> +
> +static int show_cpuinfo(struct seq_file *m, void *v)
> +{
> +	int cpu_num = *(unsigned int *)v;
> +	struct cpuinfo_kvx *n = per_cpu_ptr(&cpu_info, cpu_num);
> +
> +	seq_printf(m, "processor\t: %d\nvendor_id\t: Kalray\n", cpu_num);
> +
> +	seq_printf(m,
> +		   "copro enabled\t: %s\n"
> +		   "arch revision\t: %d\n"
> +		   "uarch revision\t: %d\n",
> +		   n->copro_enable ? "yes" : "no",
> +		   n->arch_rev,
> +		   n->uarch_rev);
> +
> +	seq_printf(m,
> +		   "bogomips\t: %lu.%02lu\n"
> +		   "cpu MHz\t\t: %llu.%03llu\n\n",
> +		   (loops_per_jiffy * HZ) / 500000,
> +		   ((loops_per_jiffy * HZ) / 5000) % 100,
> +		   n->freq / 1000000, (n->freq / 10000) % 100);
> +
> +	return 0;
> +}
> +
> +static void *c_start(struct seq_file *m, loff_t *pos)
> +{
> +	if (*pos == 0)
> +		*pos = cpumask_first(cpu_online_mask);
> +	if (*pos >= num_online_cpus())
> +		return NULL;
> +
> +	return pos;
> +}
> +
> +static void *c_next(struct seq_file *m, void *v, loff_t *pos)
> +{
> +	*pos = cpumask_next(*pos, cpu_online_mask);
> +
> +	return c_start(m, pos);
> +}
> +
> +static void c_stop(struct seq_file *m, void *v)
> +{
> +}
> +
> +const struct seq_operations cpuinfo_op = {

Where is this used?

> +	.start = c_start,
> +	.next = c_next,
> +	.stop = c_stop,
> +	.show = show_cpuinfo,
> +};
> +

Best regards,
Krzysztof
Re: [RFC PATCH v3 33/37] kvx: Add support for cpuinfo
Posted by Yann Sionneau 1 year ago
Hello Krzysztof

On 22/07/2024 14:35, Krzysztof Kozlowski wrote:
> On 22/07/2024 11:41, ysionneau@kalrayinc.com wrote:
>> From: Yann Sionneau <ysionneau@kalrayinc.com>
>>
>> Add support for cpuinfo on kvx arch.
[...]
>> +
>> +static void c_stop(struct seq_file *m, void *v)
>> +{
>> +}
>> +
>> +const struct seq_operations cpuinfo_op = {
> Where is this used?

This is used by `fs/proc/cpuinfo.c`: https://elixir.bootlin.com/linux/v6.10.6/source/fs/proc/cpuinfo.c#L12

I think I should add a #ifdef CONFIG_PROC_FS around this code, or even better use obj-$(CONFIG_PROC_FS) in the Makefile

>
>> +	.start = c_start,
>> +	.next = c_next,
>> +	.stop = c_stop,
>> +	.show = show_cpuinfo,
>> +};
>> +

-- 

Yann