All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] MIPS: Expose prid and globalnumber to sysfs
@ 2022-08-11 10:12 Jiaxun Yang
  2022-08-11 10:22 ` Greg KH
  2022-08-12  2:21 ` Florian Fainelli
  0 siblings, 2 replies; 4+ messages in thread
From: Jiaxun Yang @ 2022-08-11 10:12 UTC (permalink / raw)
  To: linux-mips; +Cc: linux-kernel, tsbogend, linux-api, Jiaxun Yang

Some application would like to know precise model and rev of processor
to do errata workaround or optimization.

Expose them in sysfs as:
/sys/devices/system/cpu/cpuX/regs/identification/prid
/sys/devices/system/cpu/cpuX/regs/identification/globalnumber

Reusing AArch64 CPU registers directory.

Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
v2: Drop static qualifier for kobj (gregkh)
---
 .../ABI/testing/sysfs-devices-system-cpu      | 11 +++
 arch/mips/kernel/topology.c                   | 96 +++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu
index 5bf61881f012..adf855e7bb9b 100644
--- a/Documentation/ABI/testing/sysfs-devices-system-cpu
+++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
@@ -512,6 +512,17 @@ Description:	information about CPUs heterogeneity.
 
 		cpu_capacity: capacity of cpuX.
 
+What:		/sys/devices/system/cpu/cpuX/regs/
+		/sys/devices/system/cpu/cpuX/regs/identification/
+		/sys/devices/system/cpu/cpuX/regs/identification/prid
+		/sys/devices/system/cpu/cpuX/regs/identification/globalnumber
+Date:		Augest 2022
+Contact:	Linux MIPS Kernel Mailing list <linux-mips@vger.kernel.org>
+Description:	MIPS CPU registers
+
+		'identification' directory exposes the Processor ID and Global Number
+		registers for identifying model and revision of the CPU.
+
 What:		/sys/devices/system/cpu/vulnerabilities
 		/sys/devices/system/cpu/vulnerabilities/meltdown
 		/sys/devices/system/cpu/vulnerabilities/spectre_v1
diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
index 9429d85a4703..0e3730f3c00f 100644
--- a/arch/mips/kernel/topology.c
+++ b/arch/mips/kernel/topology.c
@@ -5,6 +5,8 @@
 #include <linux/node.h>
 #include <linux/nodemask.h>
 #include <linux/percpu.h>
+#include <linux/seq_file.h>
+#include <linux/smp.h>
 
 static DEFINE_PER_CPU(struct cpu, cpu_devices);
 
@@ -26,3 +28,97 @@ static int __init topology_init(void)
 }
 
 subsys_initcall(topology_init);
+
+static struct kobj_type cpuregs_kobj_type = {
+	.sysfs_ops = &kobj_sysfs_ops,
+};
+
+struct cpureg {
+	struct kobject kobj;
+	struct cpuinfo_mips *info;
+};
+DEFINE_PER_CPU(struct cpureg, cpuregs);
+
+#define kobj_to_cpureg(kobj)	container_of(kobj, struct cpureg, kobj)
+#define CPUREGS_ATTR_RO(_name, _field)						\
+	static ssize_t _name##_show(struct kobject *kobj,			\
+			struct kobj_attribute *attr, char *buf)			\
+	{									\
+		struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info;		\
+										\
+		return sprintf(buf, "0x%08x\n", info->_field);	\
+	}									\
+	static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
+
+CPUREGS_ATTR_RO(prid, processor_id);
+CPUREGS_ATTR_RO(globalnumber, globalnumber);
+
+static struct attribute *cpuregs_id_attrs[] = {
+	&cpuregs_attr_prid.attr,
+	&cpuregs_attr_globalnumber.attr,
+	NULL
+};
+
+static const struct attribute_group cpuregs_attr_group = {
+	.attrs = cpuregs_id_attrs,
+	.name = "identification"
+};
+
+static int cpuregs_cpu_online(unsigned int cpu)
+{
+	int rc;
+	struct device *dev;
+	struct cpureg *reg = &per_cpu(cpuregs, cpu);
+
+	dev = get_cpu_device(cpu);
+	if (!dev) {
+		rc = -ENODEV;
+		goto out;
+	}
+	rc = kobject_add(&reg->kobj, &dev->kobj, "regs");
+	if (rc)
+		goto out;
+	rc = sysfs_create_group(&reg->kobj, &cpuregs_attr_group);
+	if (rc)
+		kobject_del(&reg->kobj);
+out:
+	return rc;
+}
+
+static int cpuregs_cpu_offline(unsigned int cpu)
+{
+	struct device *dev;
+	struct cpureg *reg = &per_cpu(cpuregs, cpu);
+
+	dev = get_cpu_device(cpu);
+	if (!dev)
+		return -ENODEV;
+	if (reg->kobj.parent) {
+		sysfs_remove_group(&reg->kobj, &cpuregs_attr_group);
+		kobject_del(&reg->kobj);
+	}
+
+	return 0;
+}
+
+static int __init cpuinfo_regs_init(void)
+{
+	int cpu, ret;
+
+	for_each_possible_cpu(cpu) {
+		struct cpureg *reg = &per_cpu(cpuregs, cpu);
+
+		reg->info = &cpu_data[cpu];
+		kobject_init(&reg->kobj, &cpuregs_kobj_type);
+	}
+
+	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mips/topology:online",
+				cpuregs_cpu_online, cpuregs_cpu_offline);
+	if (ret < 0) {
+		pr_err("cpuinfo: failed to register hotplug callbacks.\n");
+		return ret;
+	}
+	return 0;
+}
+
+device_initcall(cpuinfo_regs_init);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] MIPS: Expose prid and globalnumber to sysfs
  2022-08-11 10:12 [PATCH v2] MIPS: Expose prid and globalnumber to sysfs Jiaxun Yang
@ 2022-08-11 10:22 ` Greg KH
  2022-08-12  2:21 ` Florian Fainelli
  1 sibling, 0 replies; 4+ messages in thread
From: Greg KH @ 2022-08-11 10:22 UTC (permalink / raw)
  To: Jiaxun Yang; +Cc: linux-mips, linux-kernel, tsbogend, linux-api

On Thu, Aug 11, 2022 at 10:12:36AM +0000, Jiaxun Yang wrote:
> Some application would like to know precise model and rev of processor
> to do errata workaround or optimization.
> 
> Expose them in sysfs as:
> /sys/devices/system/cpu/cpuX/regs/identification/prid
> /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
> 
> Reusing AArch64 CPU registers directory.
> 
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v2: Drop static qualifier for kobj (gregkh)
> ---
>  .../ABI/testing/sysfs-devices-system-cpu      | 11 +++
>  arch/mips/kernel/topology.c                   | 96 +++++++++++++++++++
>  2 files changed, 107 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu
> index 5bf61881f012..adf855e7bb9b 100644
> --- a/Documentation/ABI/testing/sysfs-devices-system-cpu
> +++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
> @@ -512,6 +512,17 @@ Description:	information about CPUs heterogeneity.
>  
>  		cpu_capacity: capacity of cpuX.
>  
> +What:		/sys/devices/system/cpu/cpuX/regs/
> +		/sys/devices/system/cpu/cpuX/regs/identification/
> +		/sys/devices/system/cpu/cpuX/regs/identification/prid
> +		/sys/devices/system/cpu/cpuX/regs/identification/globalnumber
> +Date:		Augest 2022
> +Contact:	Linux MIPS Kernel Mailing list <linux-mips@vger.kernel.org>
> +Description:	MIPS CPU registers
> +
> +		'identification' directory exposes the Processor ID and Global Number
> +		registers for identifying model and revision of the CPU.

You do not document that this is only for MIPS processors.

Actually, why not do this for all chip types?  Why is this only for MIPS
chips?

> +
>  What:		/sys/devices/system/cpu/vulnerabilities
>  		/sys/devices/system/cpu/vulnerabilities/meltdown
>  		/sys/devices/system/cpu/vulnerabilities/spectre_v1
> diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
> index 9429d85a4703..0e3730f3c00f 100644
> --- a/arch/mips/kernel/topology.c
> +++ b/arch/mips/kernel/topology.c
> @@ -5,6 +5,8 @@
>  #include <linux/node.h>
>  #include <linux/nodemask.h>
>  #include <linux/percpu.h>
> +#include <linux/seq_file.h>
> +#include <linux/smp.h>
>  
>  static DEFINE_PER_CPU(struct cpu, cpu_devices);
>  
> @@ -26,3 +28,97 @@ static int __init topology_init(void)
>  }
>  
>  subsys_initcall(topology_init);
> +
> +static struct kobj_type cpuregs_kobj_type = {
> +	.sysfs_ops = &kobj_sysfs_ops,
> +};
> +
> +struct cpureg {
> +	struct kobject kobj;
> +	struct cpuinfo_mips *info;
> +};
> +DEFINE_PER_CPU(struct cpureg, cpuregs);

You still have static kobjects here, nothing changed from your previous
submission :(


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] MIPS: Expose prid and globalnumber to sysfs
  2022-08-11 10:12 [PATCH v2] MIPS: Expose prid and globalnumber to sysfs Jiaxun Yang
  2022-08-11 10:22 ` Greg KH
@ 2022-08-12  2:21 ` Florian Fainelli
       [not found]   ` <2ED0B1C9-07AB-4DE3-BC85-F490FA94785F@flygoat.com>
  1 sibling, 1 reply; 4+ messages in thread
From: Florian Fainelli @ 2022-08-12  2:21 UTC (permalink / raw)
  To: Jiaxun Yang, linux-mips; +Cc: linux-kernel, tsbogend, linux-api



On 8/11/2022 3:12 AM, Jiaxun Yang wrote:
> Some application would like to know precise model and rev of processor
> to do errata workaround or optimization.
> 
> Expose them in sysfs as:
> /sys/devices/system/cpu/cpuX/regs/identification/prid
> /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
> 
> Reusing AArch64 CPU registers directory.
> 
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v2: Drop static qualifier for kobj (gregkh)
> ---
>   .../ABI/testing/sysfs-devices-system-cpu      | 11 +++
>   arch/mips/kernel/topology.c                   | 96 +++++++++++++++++++
>   2 files changed, 107 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu
> index 5bf61881f012..adf855e7bb9b 100644
> --- a/Documentation/ABI/testing/sysfs-devices-system-cpu
> +++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
> @@ -512,6 +512,17 @@ Description:	information about CPUs heterogeneity.
>   
>   		cpu_capacity: capacity of cpuX.
>   
> +What:		/sys/devices/system/cpu/cpuX/regs/
> +		/sys/devices/system/cpu/cpuX/regs/identification/
> +		/sys/devices/system/cpu/cpuX/regs/identification/prid
> +		/sys/devices/system/cpu/cpuX/regs/identification/globalnumber
> +Date:		Augest 2022

typo: August

> +Contact:	Linux MIPS Kernel Mailing list <linux-mips@vger.kernel.org>
> +Description:	MIPS CPU registers
> +
> +		'identification' directory exposes the Processor ID and Global Number
> +		registers for identifying model and revision of the CPU.
> +
>   What:		/sys/devices/system/cpu/vulnerabilities
>   		/sys/devices/system/cpu/vulnerabilities/meltdown
>   		/sys/devices/system/cpu/vulnerabilities/spectre_v1
> diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
> index 9429d85a4703..0e3730f3c00f 100644
> --- a/arch/mips/kernel/topology.c
> +++ b/arch/mips/kernel/topology.c
> @@ -5,6 +5,8 @@
>   #include <linux/node.h>
>   #include <linux/nodemask.h>
>   #include <linux/percpu.h>
> +#include <linux/seq_file.h>
> +#include <linux/smp.h>
>   
>   static DEFINE_PER_CPU(struct cpu, cpu_devices);
>   
> @@ -26,3 +28,97 @@ static int __init topology_init(void)
>   }
>   
>   subsys_initcall(topology_init);
> +
> +static struct kobj_type cpuregs_kobj_type = {
> +	.sysfs_ops = &kobj_sysfs_ops,
> +};
> +
> +struct cpureg {
> +	struct kobject kobj;
> +	struct cpuinfo_mips *info;
> +};
> +DEFINE_PER_CPU(struct cpureg, cpuregs);
> +
> +#define kobj_to_cpureg(kobj)	container_of(kobj, struct cpureg, kobj)
> +#define CPUREGS_ATTR_RO(_name, _field)						\
> +	static ssize_t _name##_show(struct kobject *kobj,			\
> +			struct kobj_attribute *attr, char *buf)			\
> +	{									\
> +		struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info;		\
> +										\
> +		return sprintf(buf, "0x%08x\n", info->_field);	\

Would not you be able to simplify this greatly with just:

	struct cpuinfo_mips *info = current_cpu_data;

and not have to associate that with struct with the kobject?
-- 
Florian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] MIPS: Expose prid and globalnumber to sysfs
       [not found]   ` <2ED0B1C9-07AB-4DE3-BC85-F490FA94785F@flygoat.com>
@ 2022-08-12 16:43     ` Florian Fainelli
  0 siblings, 0 replies; 4+ messages in thread
From: Florian Fainelli @ 2022-08-12 16:43 UTC (permalink / raw)
  To: Jiaxun Yang; +Cc: linux-mips, linux-kernel, tsbogend, linux-api

On 8/12/22 03:29, Jiaxun Yang wrote:
> 
> 
>> 2022年8月12日 03:21,Florian Fainelli <f.fainelli@gmail.com 
>> <mailto:f.fainelli@gmail.com>> 写道:
>>
>>
>>
>> On 8/11/2022 3:12 AM, Jiaxun Yang wrote:
>>> Some application would like to know precise model and rev of processor
>>> to do errata workaround or optimization.
>>> Expose them in sysfs as:
>>> /sys/devices/system/cpu/cpuX/regs/identification/prid
>>> /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
>>> Reusing AArch64 CPU registers directory.
>>> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com 
>>> <mailto:jiaxun.yang@flygoat.com>>
>>> ---
>>> v2: Drop static qualifier for kobj (gregkh)
>>> ---
>>>  .../ABI/testing/sysfs-devices-system-cpu      | 11 +++
>>>  arch/mips/kernel/topology.c                   | 96 +++++++++++++++++++
>>>  2 files changed, 107 insertions(+)
>>> diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu 
>>> b/Documentation/ABI/testing/sysfs-devices-system-cpu
>>> index 5bf61881f012..adf855e7bb9b 100644
>>> --- a/Documentation/ABI/testing/sysfs-devices-system-cpu
>>> +++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
>>> @@ -512,6 +512,17 @@ Description:information about CPUs heterogeneity.
>>> cpu_capacity: capacity of cpuX.
>>>  +What:/sys/devices/system/cpu/cpuX/regs/
>>> +/sys/devices/system/cpu/cpuX/regs/identification/
>>> +/sys/devices/system/cpu/cpuX/regs/identification/prid
>>> +/sys/devices/system/cpu/cpuX/regs/identification/globalnumber
>>> +Date:Augest 2022
>>
>> typo: August
> 
> Thanks, good catch :-)
> 
>>
>>> +Contact:Linux MIPS Kernel Mailing list <linux-mips@vger.kernel.org 
>>> <mailto:linux-mips@vger.kernel.org>>
>>> +Description:MIPS CPU registers
>>> +
>>> +'identification' directory exposes the Processor ID and Global Number
>>> +registers for identifying model and revision of the CPU.
>>> +
>>>  What:/sys/devices/system/cpu/vulnerabilities
>>> /sys/devices/system/cpu/vulnerabilities/meltdown
>>> /sys/devices/system/cpu/vulnerabilities/spectre_v1
>>> diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
>>> index 9429d85a4703..0e3730f3c00f 100644
>>> --- a/arch/mips/kernel/topology.c
>>> +++ b/arch/mips/kernel/topology.c
>>> @@ -5,6 +5,8 @@
>>>  #include <linux/node.h>
>>>  #include <linux/nodemask.h>
>>>  #include <linux/percpu.h>
>>> +#include <linux/seq_file.h>
>>> +#include <linux/smp.h>
>>>    static DEFINE_PER_CPU(struct cpu, cpu_devices);
>>>  @@ -26,3 +28,97 @@ static int __init topology_init(void)
>>>  }
>>>    subsys_initcall(topology_init);
>>> +
>>> +static struct kobj_type cpuregs_kobj_type = {
>>> +.sysfs_ops = &kobj_sysfs_ops,
>>> +};
>>> +
>>> +struct cpureg {
>>> +struct kobject kobj;
>>> +struct cpuinfo_mips *info;
>>> +};
>>> +DEFINE_PER_CPU(struct cpureg, cpuregs);
>>> +
>>> +#define kobj_to_cpureg(kobj)container_of(kobj, struct cpureg, kobj)
>>> +#define CPUREGS_ATTR_RO(_name, _field)\
>>> +static ssize_t _name##_show(struct kobject *kobj,\
>>> +struct kobj_attribute *attr, char *buf)\
>>> +{\
>>> +struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info;\
>>> +\
>>> +return sprintf(buf, "0x%08x\n", info->_field);\
>>
>> Would not you be able to simplify this greatly with just:
>>
>> struct cpuinfo_mips *info = current_cpu_data;
> 
> Because some fields in PRID and globalnumber are not consistent between 
> cores
> so it needs to be per CPU.

Yes my bad, I was thinking that you could simplify things by always 
getting the registers of the CPU you are running on, but you need to 
create those attributes for each CPU in the system and ensure that they 
do resolve to the actual cpuinfo_mips of said CPU, such that when you do:

cat /sys/device/system/cpu/cpu*/regs/prid

we do return the actual information of that CPU number, not the one from 
the CPU we are on.

Thanks!
-- 
Florian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-12 16:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-11 10:12 [PATCH v2] MIPS: Expose prid and globalnumber to sysfs Jiaxun Yang
2022-08-11 10:22 ` Greg KH
2022-08-12  2:21 ` Florian Fainelli
     [not found]   ` <2ED0B1C9-07AB-4DE3-BC85-F490FA94785F@flygoat.com>
2022-08-12 16:43     ` Florian Fainelli

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.