diff mbox series

[03/10] powerpc/hv_gpci: Add sysfs file inside hv_gpci device to show processor config information

Message ID 20230609114001.169235-4-kjain@linux.ibm.com (mailing list archive)
State Changes Requested
Headers show
Series Add sysfs interface files to hv_gpci device to expose system information | expand

Commit Message

Kajol Jain June 9, 2023, 11:39 a.m. UTC
The hcall H_GET_PERF_COUNTER_INFO with counter request value as
PROCESSOR_CONFIG(0X90), can be used to get the system
processor configuration information. To expose the system
processor config information, patch adds sysfs file called
"processor_config" to the "/sys/devices/hv_gpci/interface/"
of hv_gpci pmu driver.

Add macro for PROCESSOR_CONFIG counter request value in hv-gpci.h file.
Also add a new function called "sysinfo_device_attr_create",
which will create and add required device attribute to the
interface_attrs array.

The processor_config sysfs file is only available for power10
and above platforms. Add a new macro called
INTERFACE_PROCESSOR_CONFIG_ATTR, which points to the index of
NULL placefolder, for processor_config attribute in the interface_attrs
array. Also add macro INTERFACE_NULL_ATTR which points to index of NULL
attribute in interface_attrs array.

Signed-off-by: Kajol Jain <kjain@linux.ibm.com>
---
 arch/powerpc/perf/hv-gpci.c | 110 +++++++++++++++++++++++++++++++++---
 arch/powerpc/perf/hv-gpci.h |   5 +-
 2 files changed, 107 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/arch/powerpc/perf/hv-gpci.c b/arch/powerpc/perf/hv-gpci.c
index bca24725699e..c9fe74373e5f 100644
--- a/arch/powerpc/perf/hv-gpci.c
+++ b/arch/powerpc/perf/hv-gpci.c
@@ -237,6 +237,72 @@  static ssize_t processor_bus_topology_show(struct device *dev, struct device_att
 	return ret;
 }
 
+static ssize_t processor_config_show(struct device *dev, struct device_attribute *attr,
+					char *buf)
+{
+	struct hv_gpci_request_buffer *arg;
+	unsigned long ret;
+	size_t n = 0;
+
+	arg = (void *)get_cpu_var(hv_gpci_reqb);
+	memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
+
+	/*
+	 * Pass the counter request value 0x90 corresponds to request
+	 * type 'Processor_config', to retrieve
+	 * the system processor information.
+	 * starting_index value implies the starting hardware
+	 * processor index.
+	 */
+	ret = systeminfo_gpci_request(PROCESSOR_CONFIG, 0, 0, buf, &n, arg);
+
+	if (!ret)
+		return n;
+
+	if (ret != H_PARAMETER)
+		goto out;
+
+	/*
+	 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL', which
+	 * implies that buffer can't accommodate all information, and a partial buffer
+	 * returned. To handle that, we need to take subsequent requests
+	 * with next starting index to retrieve additional (missing) data.
+	 * Below loop do subsequent hcalls with next starting index and add it
+	 * to buffer util we get all the information.
+	 */
+	while (ret == H_PARAMETER) {
+		int returned_values = be16_to_cpu(arg->params.returned_values);
+		int elementsize = be16_to_cpu(arg->params.cv_element_size);
+		int last_element = (returned_values - 1) * elementsize;
+
+		/*
+		 * Since the starting index is part of counter_value
+		 * buffer elements, use the starting index value in the last
+		 * element and add 1 to subsequent hcalls.
+		 */
+		u32 starting_index = arg->bytes[last_element + 3] +
+				(arg->bytes[last_element + 2] << 8) +
+				(arg->bytes[last_element + 1] << 16) +
+				(arg->bytes[last_element] << 24) + 1;
+
+		memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
+
+		ret = systeminfo_gpci_request(PROCESSOR_CONFIG, starting_index, 0, buf, &n, arg);
+
+		if (!ret)
+			return n;
+
+		if (ret != H_PARAMETER)
+			goto out;
+	}
+
+	return n;
+
+out:
+	put_cpu_var(hv_gpci_reqb);
+	return ret;
+}
+
 static DEVICE_ATTR_RO(kernel_version);
 static DEVICE_ATTR_RO(cpumask);
 
@@ -258,6 +324,11 @@  static struct attribute *interface_attrs[] = {
 	 * attribute, set in init function if applicable.
 	 */
 	NULL,
+	/*
+	 * This NULL is a placeholder for the processor_config
+	 * attribute, set in init function if applicable.
+	 */
+	NULL,
 	NULL,
 };
 
@@ -463,21 +534,46 @@  static int hv_gpci_cpu_hotplug_init(void)
 			  ppc_hv_gpci_cpu_offline);
 }
 
-static void add_sysinfo_interface_files(void)
+static void sysinfo_device_attr_create(int sysinfo_interface_group_index)
 {
-	struct device_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL);
+	struct device_attribute *attr;
 
+	if (sysinfo_interface_group_index < INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR ||
+			sysinfo_interface_group_index >= INTERFACE_NULL_ATTR) {
+		pr_info("Wrong interface group index for system information\n");
+		return;
+	}
+
+	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
 	if (!attr) {
-		pr_info("Memory allocation failed for processor_bug_topology interface file\n");
+		pr_info("Mem allocation failed, sysinfo index:%d\n", sysinfo_interface_group_index);
 		return;
 	}
 
-	/* Add processor_bus_topology attribute in the interface_attrs attribute array */
 	sysfs_attr_init(&attr->attr);
-	attr->attr.name = "processor_bug_topology";
+
+	switch (sysinfo_interface_group_index) {
+	case INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR:
+		attr->attr.name = "processor_bug_topology";
+		attr->show = processor_bus_topology_show;
+	break;
+	case INTERFACE_PROCESSOR_CONFIG_ATTR:
+		attr->attr.name = "processor_config";
+		attr->show = processor_config_show;
+	break;
+	}
+
 	attr->attr.mode = 0444;
-	attr->show = processor_bus_topology_show;
-	interface_attrs[INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR] = &attr->attr;
+	interface_attrs[sysinfo_interface_group_index] = &attr->attr;
+}
+
+static void add_sysinfo_interface_files(void)
+{
+	/* Add processor_bus_topology attribute in the interface_attrs attribute array */
+	sysinfo_device_attr_create(INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR);
+
+	/* Add processor_config attribute in the interface_attrs attribute array */
+	sysinfo_device_attr_create(INTERFACE_PROCESSOR_CONFIG_ATTR);
 }
 
 static int hv_gpci_init(void)
diff --git a/arch/powerpc/perf/hv-gpci.h b/arch/powerpc/perf/hv-gpci.h
index ff76bc010823..0d324a7c684b 100644
--- a/arch/powerpc/perf/hv-gpci.h
+++ b/arch/powerpc/perf/hv-gpci.h
@@ -24,10 +24,13 @@  enum {
 };
 
 /* Counter request value to retrieve system information */
-#define PROCESSOR_BUS_TOPOLOGY 0XD0
+#define PROCESSOR_BUS_TOPOLOGY	0XD0
+#define PROCESSOR_CONFIG	0X90
 
 /* Interface attribute array index to store system information */
 #define INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR	6
+#define INTERFACE_PROCESSOR_CONFIG_ATTR		7
+#define INTERFACE_NULL_ATTR			8
 
 #define REQUEST_FILE "../hv-gpci-requests.h"
 #define NAME_LOWER hv_gpci