Skip to contentSkip to conversation

drivers: hwmon: Add On-Chip Controller drive

PATCH Superseded [email protected] 4 participants 12 messages Jan 26, 2017 – Jan 30, 2017

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover drivers: hwmon: Add On-Chip Controller drive

From: "Edward A. James" <eajames-r/[email protected]>

This patchset adds a hwmon driver to support the OCC (On-Chip Controller) on the IBM POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The OCC is an embedded processor that provides real time power and thermal monitoring.

The driver provides an interface on a BMC to poll OCC sensor data, set user power caps, and perform some basic OCC error handling. It interfaces with userspace through hwmon.

The driver is currently functional only for the OCC on POWER8 chips. Communicating with the POWER9 OCC requries FSI support.

Edward A. James (6):
hwmon: Add core On-Chip Controller support for POWER CPUs
hwmon: occ: Add sysfs interface
hwmon: occ: Add I2C transport implementation for SCOM operations
hwmon: occ: Add callbacks for parsing P8 OCC datastructures
hwmon: occ: Add hwmon implementation for the P8 OCC
hwmon: occ: Add callbacks for parsing P9 OCC datastructures

 Documentation/devicetree/bindings/hwmon/occ.txt |  13 +
 Documentation/hwmon/occ                         | 114 ++++++
 MAINTAINERS                                     |   7 +
 drivers/hwmon/Kconfig                           |   2 +
 drivers/hwmon/Makefile                          |   1 +
 drivers/hwmon/occ/Kconfig                       |  29 ++
 drivers/hwmon/occ/Makefile                      |   2 +
 drivers/hwmon/occ/occ.c                         | 479 ++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h                         |  80 ++++
 drivers/hwmon/occ/occ_p8.c                      | 247 ++++++++++++
 drivers/hwmon/occ/occ_p8.h                      |  30 ++
 drivers/hwmon/occ/occ_p9.c                      | 308 +++++++++++++++
 drivers/hwmon/occ/occ_p9.h                      |  30 ++
 drivers/hwmon/occ/occ_scom_i2c.c                |  77 ++++
 drivers/hwmon/occ/occ_scom_i2c.h                |  26 ++
 drivers/hwmon/occ/occ_sysfs.c                   | 259 +++++++++++++
 drivers/hwmon/occ/occ_sysfs.h                   |  30 ++
 drivers/hwmon/occ/p8_occ_i2c.c                  | 104 +++++
 drivers/hwmon/occ/scom.h                        |  47 +++
 19 files changed, 1885 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/occ.txt
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
 create mode 100644 drivers/hwmon/occ/occ_p9.c
 create mode 100644 drivers/hwmon/occ/occ_p9.h
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
 create mode 100644 drivers/hwmon/occ/scom.h
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v4 1/6 hwmon: Add core On-Chip Controller support for POWER CPUs

From: "Edward A. James" <eajames-r/[email protected]>

Add core support for polling the OCC for it's sensor data and parsing that data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 479 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  80 ++++++++
 drivers/hwmon/occ/scom.h   |  47 +++++
 9 files changed, 672 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +40 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9 processor
+(see the BMC - Host Communications section).
+
+Author: Eddie James <eajames-r/[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
+
+This driver provides an interface on a BMC to poll OCC sensor data, set user
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data: power,
+temperature, frequency, and "caps," which indicate limits and thresholds used
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over I2C bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically of 8
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over FSI bus
+and SBE engine. Once again, SCOM operations are required. This driver will
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:	git git://linuxtv.org/media_tree.git
 S:	Maintained
 F:	drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:	Eddie James <eajames-r/[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/hwmon/occ
+F:	drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:	Kyungmin Park <[email protected]>
 L:	[email protected]
drivers/hwmon/Kconfig +2 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
 	  This driver can also be built as a module. If so, the module
 	  will be called nsa320-hwmon.
 
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)	+= xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)	+= occ/
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+	bool "PPC On-Chip Controller"
+	help
+	  If you say yes here you get support to monitor Power CPU
+	  sensors via the On-Chip Controller (OCC).
+
+	  Generally this is used by management controllers such as a BMC
+	  on an OpenPower system.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +479 -0

479 lines not on this page · raw message

drivers/hwmon/occ/occ.h new file +80 -0

80 lines not on this page · raw message

drivers/hwmon/occ/scom.h new file +47 -0

47 lines not on this page · raw message

Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

PATCH v4 2/6 hwmon: occ: Add sysfs interface

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 259 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  30 +++++
 4 files changed, 352 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +62 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many of the
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be used.
+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom sensor.
+
+Below are detailed the names and meaning of each sensor file for both types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+	all processors:
+		in<x>_input - frequency value
+		in<x>_label - sensor id
+temperature:
+	POWER8:
+		temp<x>_input - temperature value
+		temp<x>_label - sensor id
+	POWER9 (in addition to above):
+		temp<x>_type - FRU type
+
+power:
+	POWER8:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average - accumulator
+		power<x>_average_interval - update tag (number of samples in
+			accumulator)
+	POWER9:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average_min - accumulator[0]
+		power<x>_average_max - accumulator[1] (64 bits total)
+		power<x>_average_interval - update tag
+		power<x>_reset_history - (function_id | (apss_channel << 8)
+
+caps:
+	POWER8:
+		power<x>_cap - current powercap
+		power<x>_cap_max - max powercap
+		power<x>_cap_min - min powercap
+		power<x>_max - normal powercap
+		power<x>_alarm - user powercap, r/w
+	POWER9:
+		power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be multiple
+OCCs present on the system, these two files are only present for the "master"
+OCC.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
 BMC - Host Communications
 -------------------------
 
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
drivers/hwmon/occ/occ_sysfs.c new file +259 -0

259 lines not on this page · raw message

drivers/hwmon/occ/occ_sysfs.h new file +30 -0
new file mode 100644
index 0000000..462f9d2
@@ -0,0 +1,30 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes for the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+struct occ;
+struct device;
+struct occ_sysfs;
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  const u32 *sensor_hwmon_configs,
+				  const char *name);
+#endif /* __OCC_SYSFS_H__ */
Signature · 1 line
1.9.1
#8 lore raw

PATCH v4 3/6 hwmon: occ: Add I2C transport implementation for SCOM operations

In reply to [email protected]

From: "Edward A. James" <eajames-r/[email protected]>

Add functions to send SCOM operations over I2C bus. The BMC can communicate with the Power8 host processor over I2C, but needs to use SCOM operations in order to access the OCC register space.

---
 drivers/hwmon/occ/occ_scom_i2c.c | 77 ++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_scom_i2c.h | 26 ++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
drivers/hwmon/occ/occ_scom_i2c.c new file +77 -0
new file mode 100644
index 0000000..74bd6ff
@@ -0,0 +1,77 @@
+/*
+ * occ_scom_i2c.c - hwmon OCC driver
+ *
+ * This file contains the functions for performing SCOM operations over I2C bus
+ * to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include "occ_scom_i2c.h"
+#include "scom.h"
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+	ssize_t rc;
+	u64 buf;
+	struct i2c_client *client = bus;
+	struct i2c_msg msgs[2];
+
+	msgs[0].addr = client->addr;
+	msgs[0].flags = client->flags & I2C_M_TEN;
+	msgs[0].len = sizeof(u32);
+	msgs[0].buf = (char *)&address;
+
+	msgs[1].addr = client->addr;
+	msgs[1].flags = client->flags & I2C_M_TEN;
+	msgs[1].flags |= I2C_M_RD;
+	msgs[1].len = sizeof(u64);
+	msgs[1].buf = (char *)&buf;
+
+	rc = i2c_transfer(client->adapter, msgs, 2);
+	if (rc < 0)
+		return rc;
+
+	*data = be64_to_cpu(buf);
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);
+
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+	u32 buf[3];
+	ssize_t rc;
+	struct i2c_client *client = bus;
+
+	/* send raw data, user can handle endian */
+	buf[0] = address;
+	buf[1] = data1;
+	buf[2] = data0;
+
+	rc = i2c_master_send(client, (const char *)buf, sizeof(u32) * 3);
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u32) * 3)
+		return -EIO;
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_putscom);
+
+MODULE_AUTHOR("Eddie James <eajames-r/[email protected]>");
+MODULE_DESCRIPTION("I2C OCC SCOM transport");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_scom_i2c.h new file +26 -0
new file mode 100644
index 0000000..945739c
@@ -0,0 +1,26 @@
+/*
+ * occ_scom_i2c.h - hwmon OCC driver
+ *
+ * This file contains function protoypes for peforming SCOM operations over I2C
+ * bus to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SCOM_I2C_H__
+#define __OCC_SCOM_I2C_H__
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data);
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1);
+
+#endif /* __OCC_SCOM_I2C_H__ */
Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

PATCH v4 4/6 hwmon: occ: Add callbacks for parsing P8 OCC datastructures

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC on the POWER8 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   9 ++
 drivers/hwmon/occ/occ_p8.c | 247 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p8.h |  30 ++++++
 3 files changed, 286 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
Documentation/hwmon/occ +9 -0
index d0bdf06..143951e 100644
@@ -25,6 +25,15 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+The format for the POWER8 OCC sensor data can be found in the P8 OCC
+specification:
+github.com/open-power/docs/blob/master/occ/OCC_OpenPwr_FW_Interfaces.pdf
+This document provides the details of the OCC sensors: power, frequency,
+temperature, and caps. These sensor formats are specific to the POWER8 OCC. A
+number of data structures, such as command format, response headers, and the
+like, are also defined in this specification, and are common to both POWER8 and
+POWER9 OCCs.
+
 sysfs Entries
 -------------
 
drivers/hwmon/occ/occ_p8.c new file +247 -0

247 lines not on this page · raw message

drivers/hwmon/occ/occ_p8.h new file +30 -0
new file mode 100644
index 0000000..3fe6417
@@ -0,0 +1,30 @@
+/*
+ * occ_p8.h - OCC hwmon driver
+ *
+ * This file contains Power8-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P8_H__
+#define __OCC_P8_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p8_get_sensor_hwmon_configs(void);
+struct occ *p8_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P8_H__ */
Signature · 1 line
1.9.1
#10 lore raw

PATCH v4 5/6 hwmon: occ: Add hwmon implementation for the P8 OCC

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add code to tie the hwmon sysfs code and the POWER8 OCC code together, as well as probe the entire driver from the I2C bus. I2C is the communication method between the BMC and the P8 OCC.

---
 Documentation/devicetree/bindings/hwmon/occ.txt |  13 +++
 drivers/hwmon/occ/Kconfig                       |  14 ++++
 drivers/hwmon/occ/Makefile                      |   1 +
 drivers/hwmon/occ/p8_occ_i2c.c                  | 104 ++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/occ.txt
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
Documentation/devicetree/bindings/hwmon/occ.txt new file +13 -0
new file mode 100644
index 0000000..b0d2b36
@@ -0,0 +1,13 @@
+HWMON I2C driver for IBM POWER CPU OCC (On Chip Controller)
+
+Required properties:
+ - compatible: must be "ibm,p8-occ-i2c"
+ - reg: physical address
+
+Example:
+i2c3: i2c-bus@100 {
+	occ@50 {
+		compatible = "ibm,p8-occ-i2c";
+		reg = <0x50>;
+	};
+};
drivers/hwmon/occ/Kconfig +14 -0
index cdb64a7..3a5188f 100644
@@ -13,3 +13,17 @@ menuconfig SENSORS_PPC_OCC
 
 	  This driver can also be built as a module. If so, the module
 	  will be called occ.
+
+if SENSORS_PPC_OCC
+
+config SENSORS_PPC_OCC_P8_I2C
+	tristate "POWER8 OCC hwmon support"
+	depends on I2C
+	help
+	 Provide a hwmon sysfs interface for the POWER8 On-Chip Controller,
+	 exposing temperature, frequency and power measurements.
+
+	 This driver can also be built as a module. If so, the module will be
+	 called p8-occ-i2c.
+
+endif
drivers/hwmon/occ/Makefile +1 -0
index a6881f9..9294b58 100644
@@ -1 +1,2 @@
 obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
+obj-$(CONFIG_SENSORS_PPC_OCC_P8_I2C) += occ_scom_i2c.o occ_p8.o p8_occ_i2c.o
drivers/hwmon/occ/p8_occ_i2c.c new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.9.1
#11 lore raw

PATCH v4 6/6 hwmon: occ: Add callbacks for parsing P9 OCC datastructures

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC on the POWER9 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   3 +
 drivers/hwmon/occ/occ_p9.c | 308 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p9.h |  30 +++++
 3 files changed, 341 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p9.c
 create mode 100644 drivers/hwmon/occ/occ_p9.h
Documentation/hwmon/occ +3 -0
index 143951e..6cea853 100644
@@ -34,6 +34,9 @@ number of data structures, such as command format, response headers, and the
 like, are also defined in this specification, and are common to both POWER8 and
 POWER9 OCCs.
 
+There is currently no public P9 OCC specification, and the data structures
+defined in the POWER9 OCC driver are subject to change.
+
 sysfs Entries
 -------------
 
drivers/hwmon/occ/occ_p9.c new file +308 -0

308 lines not on this page · raw message

drivers/hwmon/occ/occ_p9.h new file +30 -0
new file mode 100644
index 0000000..18ca16a
@@ -0,0 +1,30 @@
+/*
+ * occ_p9.h - OCC hwmon driver
+ *
+ * This file contains Power9-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P9_H__
+#define __OCC_P9_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p9_get_sensor_hwmon_configs(void);
+struct occ *p9_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P9_H__ */
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH linux v4 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

In reply to [email protected]
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Edward,

[auto build test ERROR on hwmon/hwmon-next]
[also build test ERROR on v4.10-rc5 next-20170125]
[cannot apply to linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/eajames-ibm-gmail-com/drivers-hwmon-Add-On-Chip-Controller-drive/20170127-073116
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: alpha-allyesconfig (attached as .config)
compiler: alpha-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=alpha

All error/warnings (new ones prefixed by >>):

In file included from include/linux/linkage.h:6:0,
from include/linux/kernel.h:6,
from include/linux/unaligned/packed_struct.h:4, from include/linux/unaligned/le_struct.h:4, from arch/alpha/include/asm/unaligned.h:4, from drivers/hwmon/occ/occ.c:19:

>> drivers/hwmon/occ/occ.c:433:15: error: 'occ_get_sensor_value' undeclared here (not in a function)
    EXPORT_SYMBOL(occ_get_sensor_value);
                  ^
   include/linux/export.h:58:16: note: in definition of macro '___EXPORT_SYMBOL'
     extern typeof(sym) sym;      \
                   ^~~
>> drivers/hwmon/occ/occ.c:433:1: note: in expansion of macro 'EXPORT_SYMBOL'
    EXPORT_SYMBOL(occ_get_sensor_value);
    ^~~~~~~~~~~~~

vim +/occ_get_sensor_value +433 drivers/hwmon/occ/occ.c

427
428 int occ_get_sensor_field(struct occ *occ, int sensor_type, int sensor_num,
429 u32 hwmon, long *val)
430 {
431 return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon, val);
432 }

 > 433	EXPORT_SYMBOL(occ_get_sensor_value);

434
435 void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks)
436 {

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

Re: [PATCH linux v4 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

In reply to [email protected]
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Edward,

[auto build test ERROR on hwmon/hwmon-next]
[also build test ERROR on v4.10-rc5 next-20170125]
[cannot apply to linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/eajames-ibm-gmail-com/drivers-hwmon-Add-On-Chip-Controller-drive/20170127-073116
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: openrisc-allmodconfig (attached as .config)
compiler: or32-linux-gcc (GCC) 4.5.1-or32-1.0rc1

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=openrisc

All errors (new ones prefixed by >>):

drivers/hwmon/occ/occ.c:433:1: error: 'occ_get_sensor_value' undeclared here (not in a function)

>> drivers/hwmon/occ/occ.c:433:1: error: type defaults to 'int' in declaration of 'occ_get_sensor_value'

vim +433 drivers/hwmon/occ/occ.c

427
428 int occ_get_sensor_field(struct occ *occ, int sensor_type, int sensor_num,
429 u32 hwmon, long *val)
430 {
431 return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon, val);
432 }

 > 433	EXPORT_SYMBOL(occ_get_sensor_value);

434
435 void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks)
436 {

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

maintainer
#4 lore raw

Re: [PATCH linux v4 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

In reply to [email protected]
Quoted 380 lines · On 01/26/2017 03:19 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add core support for polling the OCC for it's sensor data and parsing that data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 479 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  80 ++++++++
 drivers/hwmon/occ/scom.h   |  47 +++++
 9 files changed, 672 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +40 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9 processor
+(see the BMC - Host Communications section).
+
+Author: Eddie James <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
+
+This driver provides an interface on a BMC to poll OCC sensor data, set user
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data: power,
+temperature, frequency, and "caps," which indicate limits and thresholds used
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over I2C bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically of 8
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over FSI bus
+and SBE engine. Once again, SCOM operations are required. This driver will
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:	git git://linuxtv.org/media_tree.git
 S:	Maintained
 F:	drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:	Eddie James <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/hwmon/occ
+F:	drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:	Kyungmin Park <[email protected]>
 L:	[email protected]
drivers/hwmon/Kconfig +2 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
 	  This driver can also be built as a module. If so, the module
 	  will be called nsa320-hwmon.
 
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)	+= xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)	+= occ/
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+	bool "PPC On-Chip Controller"
+	help
+	  If you say yes here you get support to monitor Power CPU
+	  sensors via the On-Chip Controller (OCC).
+
+	  Generally this is used by management controllers such as a BMC
+	  on an OpenPower system.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +232 -0
new file mode 100644
index 0000000..3b233d8
@@ -0,0 +1,479 @@
+/*
+ * occ.c - OCC hwmon driver
+ *
+ * This file contains the methods and data structures for the OCC hwmon driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+
+#define OCC_DATA_MAX		4096
+#define OCC_DATA_MIN		40
+#define OCC_BMC_TIMEOUT_MS	20000
+
+/* To generate attn to OCC */
+#define ATTN_DATA		0x0006B035
+
+/* For BMC to read/write SRAM */
+#define OCB_ADDRESS		0x0006B070
+#define OCB_DATA		0x0006B075
+#define OCB_STATUS_CONTROL_AND	0x0006B072
+#define OCB_STATUS_CONTROL_OR	0x0006B073
+
+/* To init OCB */
+#define OCB_AND_INIT0		0xFBFFFFFF
+#define OCB_AND_INIT1		0xFFFFFFFF
+#define OCB_OR_INIT0		0x08000000
+#define OCB_OR_INIT1		0x00000000
+
+/* To generate attention on OCC */
+#define ATTN0			0x01010000
+#define ATTN1			0x00000000
+
+/* OCC return status */
+#define RESP_RETURN_CMD_IN_PRG	0xFF
+#define RESP_RETURN_SUCCESS	0
+#define RESP_RETURN_CMD_INVAL	0x11
+#define RESP_RETURN_CMD_LEN	0x12
+#define RESP_RETURN_DATA_INVAL	0x13
+#define RESP_RETURN_CHKSUM	0x14
+#define RESP_RETURN_OCC_ERR	0x15
+#define RESP_RETURN_STATE	0x16
+
+/* time interval to retry on "command in progress" return status */
+#define CMD_IN_PRG_INT_MS	100
+#define CMD_IN_PRG_RETRIES	(OCC_BMC_TIMEOUT_MS / CMD_IN_PRG_INT_MS)
+
+/* OCC command definitions */
+#define OCC_POLL		0
+#define OCC_SET_USER_POWR_CAP	0x22
+
+/* OCC poll command data */
+#define OCC_POLL_STAT_SENSOR	0x10
+
+/* OCC response data offsets */
+#define RESP_RETURN_STATUS		2
+#define RESP_DATA_LENGTH		3
+#define RESP_HEADER_OFFSET		5
+#define SENSOR_STR_OFFSET		37
+#define NUM_SENSOR_BLOCKS_OFFSET	43
+#define SENSOR_BLOCK_OFFSET		45
+
+/* occ_poll_header
+ * structure to match the raw occ poll response data
+ */
+struct occ_poll_header {
+	u8 status;
+	u8 ext_status;
+	u8 occs_present;
+	u8 config;
+	u8 occ_state;
+	u8 mode;
+	u8 ips_status;
+	u8 error_log_id;
+	u32 error_log_addr_start;
+	u16 error_log_length;
+	u8 reserved2;
+	u8 reserved3;
+	u8 occ_code_level[16];
+	u8 sensor_eye_catcher[6];
+	u8 num_sensor_blocks;
+	u8 sensor_data_version;
+} __attribute__((packed, aligned(4)));
+
+struct occ_response {
+	struct occ_poll_header header;
+	struct occ_blocks data;
+};
+
+struct occ {
+	struct device *dev;
+	void *bus;
+	struct occ_bus_ops bus_ops;
+	struct occ_ops ops;
+	struct occ_config config;
+	unsigned long update_interval;
+	unsigned long last_updated;
+	struct mutex update_lock;
+	struct occ_response response;
+	bool valid;
+	u8 *raw_data;
+};
+
+static int occ_check_sensor(struct occ *driver, u8 sensor_length,
+			    u8 num_sensors, enum sensor_type t, int b)
+{
+	struct occ_response *resp = &driver->response;
+	struct sensor_data_block *block = &resp->data.blocks[b];
+
+	if (block->sensors) {
+		/* empty block, release old data */
+		if (num_sensors == 0 || sensor_length == 0) {
+			devm_kfree(driver->dev, block->sensors);
+			block->sensors = NULL;
+			return -ENODATA;
+		}
+
+		/* different num sensors or length, re-alloc */
+		if (num_sensors != block->header.num_sensors ||
+		    sensor_length != block->header.sensor_length)
+			devm_kfree(driver->dev, block->sensors);
+		else
+			return 0;
+	}
+
+	/* each sensor type is a different size */
+	block->sensors = driver->ops.alloc_sensor(t, num_sensors);
+	if (!block->sensors)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static int parse_occ_response(struct occ *driver, u16 num_bytes)
+{
+	int b;
+	int s;
+	int rc;
+	unsigned int offset = SENSOR_BLOCK_OFFSET;
+	int sensor_type;
+	u8 num_sensor_blocks;
+	struct sensor_data_block_header *block;
+	struct device *dev = driver->dev;
+	u8 *data = driver->raw_data;
+	struct occ_response *resp = &driver->response;
+
+	/* check if the data is valid */
+	if (strncmp(&data[SENSOR_STR_OFFSET], "SENSOR", 6) != 0) {
+		dev_err(dev, "no SENSOR string in response\n");
+		rc = -ENODATA;
+		goto err;
+	}
+
+	num_sensor_blocks = data[NUM_SENSOR_BLOCKS_OFFSET];
+	if (num_sensor_blocks == 0) {
+		dev_warn(dev, "no sensor blocks available\n");
+		rc = -ENODATA;
+		goto err;
+	}
+
+	memcpy(&resp->header, &data[RESP_HEADER_OFFSET],
+	       sizeof(struct occ_poll_header));
+
+	/* data length starts at actual data */
+	num_bytes += RESP_HEADER_OFFSET;
+
+	/* translate fields > 1 byte */
+	resp->header.error_log_addr_start =
+		be32_to_cpu(resp->header.error_log_addr_start);
+	resp->header.error_log_length =
+		be16_to_cpu(resp->header.error_log_length);
+
+	for (b = 0; b < num_sensor_blocks && b < MAX_OCC_SENSOR_TYPE; b++) {
+		if (offset + sizeof(struct sensor_data_block_header) >
+		    num_bytes) {
+			dev_warn(dev, "exceeded data length\n");
+			goto err;
+		}
+
+		block = (struct sensor_data_block_header *)&data[offset];
+		offset += sizeof(struct sensor_data_block_header);
+
+		if (strncmp(block->sensor_type, "FREQ", 4) == 0)
+			sensor_type = FREQ;
+		else if (strncmp(block->sensor_type, "TEMP", 4) == 0)
+			sensor_type = TEMP;
+		else if (strncmp(block->sensor_type, "POWR", 4) == 0)
+			sensor_type = POWER;
+		else if (strncmp(block->sensor_type, "CAPS", 4) == 0)
+			sensor_type = CAPS;
+		else {
+			dev_warn(dev, "sensor type not supported %.4s\n",
+				block->sensor_type);
+			continue;
+		}
+
+		rc = occ_check_sensor(driver, block->sensor_length,
+				      block->num_sensors, sensor_type, b);
+		if (rc == -ENOMEM)
+			goto err;
+		else if (rc)
+			continue;
+
+		resp->data.sensor_block_id[sensor_type] = b;
+		/* copy block data over to response pointer */
+		resp->data.blocks[b].header = *block;
+		for (s = 0; s < block->num_sensors; s++) {
+			if (offset + block->sensor_length > num_bytes) {
+				dev_warn(dev, "exceeded data length\n");
+				goto err;
+			}
+

drivers/hwmon/occ/occ.c line 242

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+			driver->ops.parse_sensor(data,
+						 resp->data.blocks[b].sensors,
+						 sensor_type, offset, s);
+			offset += block->sensor_length;
+		}
+	}
+
+	return 0;
+err:
+	return rc;

Please no unnecessary gotos. The code jumping here can return directly.

Quoted 131 lines
drivers/hwmon/occ/occ.c new file +131 -0
+}
+
+static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16 length,
+		       const u8 *data, u8 *resp)
+{
+	u32 cmd1, cmd2 = 0;
+	u16 checksum = 0;
+	bool retry = false;
+	int i, rc, tries = 0;
+
+	cmd1 = (seq << 24) | (type << 16) | length;
+	memcpy(&cmd2, data, length);
+	cmd2 <<= ((4 - length) * 8);
+
+	/* checksum: sum of every bytes of cmd1, cmd2 */
+	for (i = 0; i < 4; i++) {
+		checksum += (cmd1 >> (i * 8)) & 0xFF;
+		checksum += (cmd2 >> (i * 8)) & 0xFF;
+	}
+
+	cmd2 |= checksum << ((2 - length) * 8);
+
+	/* Init OCB */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_OR,
+				     OCB_OR_INIT0, OCB_OR_INIT1);
+	if (rc)
+		goto err;
+
+	rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_AND,
+				     OCB_AND_INIT0, OCB_AND_INIT1);
+	if (rc)
+		goto err;
+
+	/* Send command, 2nd half of the 64-bit addr is unused (write 0) */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+				     driver->config.command_addr, 0);
+	if (rc)
+		goto err;
+
+	rc = driver->bus_ops.putscom(driver->bus, OCB_DATA, cmd1, cmd2);
+	if (rc)
+		goto err;
+
+	/* Trigger attention */
+	rc = driver->bus_ops.putscom(driver->bus, ATTN_DATA, ATTN0, ATTN1);
+	if (rc)
+		goto err;
+
+	/* Get response data */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+				     driver->config.response_addr, 0);
+	if (rc)
+		goto err;
+
+	do {
+		if (retry) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			schedule_timeout(msecs_to_jiffies(CMD_IN_PRG_INT_MS));
+		}
+
+		rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+					     (u64 *)resp);
+		if (rc)
+			goto err;
+
+		/* retry if we get "command in progress" return status */
+		retry = resp[RESP_RETURN_STATUS] == RESP_RETURN_CMD_IN_PRG &&
+			tries++ < CMD_IN_PRG_RETRIES;
+	} while (retry);
+
+	/* check the occ response */
+	switch (resp[RESP_RETURN_STATUS]) {
+	case RESP_RETURN_CMD_IN_PRG:
+		rc = -EALREADY;
+		break;
+	case RESP_RETURN_SUCCESS:
+		rc = 0;
+		break;
+	case RESP_RETURN_CMD_INVAL:
+	case RESP_RETURN_CMD_LEN:
+	case RESP_RETURN_DATA_INVAL:
+	case RESP_RETURN_CHKSUM:
+		rc = -EINVAL;
+		break;
+	case RESP_RETURN_OCC_ERR:
+		rc = -EREMOTE;
+		break;
+	default:
+		rc = -EFAULT;
+	}
+
+	if (rc < 0)
+		dev_warn(driver->dev, "occ bad response:%d\n",
+			 resp[RESP_RETURN_STATUS]);
+
+	return rc;
+
+err:
+	dev_err(driver->dev, "scom op failed rc:%d\n", rc);
+	return rc;
+}
+
+static int occ_get_all(struct occ *driver)
+{
+	int i = 0, rc;
+	u8 *occ_data = driver->raw_data;
+	u16 num_bytes;
+	const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
+	struct device *dev = driver->dev;
+
+	memset(occ_data, 0, OCC_DATA_MAX);
+
+	rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data, occ_data);
+	if (rc)
+		goto out;
+
+	num_bytes = get_unaligned((u16 *)&occ_data[RESP_DATA_LENGTH]);
+	num_bytes = be16_to_cpu(num_bytes);
+
+	if (num_bytes > OCC_DATA_MAX || num_bytes < OCC_DATA_MIN) {
+		dev_err(dev, "bad OCC data length:%d\n", num_bytes);
+		rc = -EINVAL;
+		goto out;
+	}
+
+	/* read remaining data, 8 byte scoms at a time */
+	for (i = 8; i < num_bytes + 8; i += 8) {
+		rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+					     (u64 *)&occ_data[i]);
+		if (rc) {
+			dev_err(dev, "getscom op failed rc:%d\n", rc);

drivers/hwmon/occ/occ.c line 383

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+			goto out;
+		}
+	}
+
+	/* don't need more sanity checks; buffer is alloc'd for max response
+	 * size so we just check for valid data in parse_occ_response
+	 */
+	rc = parse_occ_response(driver, num_bytes);
+
+out:

Same as above - this label is unnecessary.

drivers/hwmon/occ/occ.c line 393

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+	return rc;
+}
+
+int occ_update_device(struct occ *driver)
+{
+	int rc = 0;
+
+	mutex_lock(&driver->update_lock);
+
+	if (time_after(jiffies, driver->last_updated + driver->update_interval)

Can you make update_interval a constant ?

Quoted 15 lines
drivers/hwmon/occ/occ.c new file +15 -0
+	    || !driver->valid) {
+		driver->valid = true;
+
+		rc = occ_get_all(driver);
+		if (rc)
+			driver->valid = false;
+
+		driver->last_updated = jiffies;
+	}
+
+	mutex_unlock(&driver->update_lock);
+
+	return rc;
+}
+EXPORT_SYMBOL(occ_update_device);

drivers/hwmon/occ/occ.c line 418

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+
+void *occ_get_sensor(struct occ *driver, int sensor_type)
+{
+	int rc;
+	int type_id;
+
+	/* occ_update_device locks the update lock */
+	rc = occ_update_device(driver);
+	if (rc)
+		return NULL;

NULL or ERR_PTR() ?

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+
+	type_id = driver->response.data.sensor_block_id[sensor_type];
+	if (type_id == -1)
+		return NULL;
+

drivers/hwmon/occ/occ.c line 433

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+	return driver->response.data.blocks[type_id].sensors;
+}
+EXPORT_SYMBOL(occ_get_sensor);
+
+int occ_get_sensor_field(struct occ *occ, int sensor_type, int sensor_num,
+			 u32 hwmon, long *val)
+{
+	return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon, val);
+}
+EXPORT_SYMBOL(occ_get_sensor_value);

Ad 0day found, something got messed up here. Please fix and resubmit.

Quoted 186 lines
drivers/hwmon/occ/occ.c new file +46 -0
+
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks)
+{
+	*blocks = &occ->response.data;
+}
+EXPORT_SYMBOL(occ_get_response_blocks);
+
+int occ_set_user_powercap(struct occ *occ, u16 cap)
+{
+	u8 resp[8];
+
+	cap = cpu_to_be16(cap);
+
+	return occ_send_cmd(occ, 0, OCC_SET_USER_POWR_CAP, 2, (const u8 *)&cap,
+			    resp);
+}
+EXPORT_SYMBOL(occ_set_user_powercap);
+
+struct occ *occ_start(struct device *dev, void *bus,
+		      struct occ_bus_ops *bus_ops, const struct occ_ops *ops,
+		      const struct occ_config *config)
+{
+	struct occ *driver = devm_kzalloc(dev, sizeof(struct occ), GFP_KERNEL);
+
+	if (!driver)
+		return ERR_PTR(-ENOMEM);
+
+	driver->dev = dev;
+	driver->bus = bus;
+	driver->bus_ops = *bus_ops;
+	driver->ops = *ops;
+	driver->config = *config;
+	driver->raw_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);
+	if (!driver->raw_data)
+		return ERR_PTR(-ENOMEM);
+
+	driver->update_interval = HZ;
+	mutex_init(&driver->update_lock);
+
+	return driver;
+}
+EXPORT_SYMBOL(occ_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC hwmon core driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ.h new file +80 -0
new file mode 100644
index 0000000..41f11b2
@@ -0,0 +1,80 @@
+/*
+ * occ.h - hwmon OCC driver
+ *
+ * This file contains data structures and function prototypes for common access
+ * between different bus protocols and host systems.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_H__
+#define __OCC_H__
+
+#include "scom.h"
+
+struct device;
+struct occ;
+
+/* sensor_data_block_header
+ * structure to match the raw occ sensor block header
+ */
+struct sensor_data_block_header {
+	u8 sensor_type[4];
+	u8 reserved0;
+	u8 sensor_format;
+	u8 sensor_length;
+	u8 num_sensors;
+} __attribute__((packed, aligned(4)));
+
+struct sensor_data_block {
+	struct sensor_data_block_header header;
+	void *sensors;
+};
+
+enum sensor_type {
+	FREQ = 0,
+	TEMP,
+	POWER,
+	CAPS,
+	MAX_OCC_SENSOR_TYPE
+};
+
+struct occ_ops {
+	void (*parse_sensor)(u8 *data, void *sensor, int sensor_type, int off,
+			     int snum);
+	void *(*alloc_sensor)(int sensor_type, int num_sensors);
+	int (*get_sensor)(struct occ *driver, int sensor_type, int sensor_num,
+			  u32 hwmon, long *val);
+};
+
+struct occ_config {
+	u32 command_addr;
+	u32 response_addr;
+};
+
+struct occ_blocks {
+	int sensor_block_id[MAX_OCC_SENSOR_TYPE];
+	struct sensor_data_block blocks[MAX_OCC_SENSOR_TYPE];
+};
+
+struct occ *occ_start(struct device *dev, void *bus,
+		      struct occ_bus_ops *bus_ops, const struct occ_ops *ops,
+		      const struct occ_config *config);
+void *occ_get_sensor(struct occ *occ, int sensor_type);
+int occ_get_sensor_field(struct occ *occ, int sensor_type, int sensor_num,
+			 u32 hwmon, long *val);
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks);
+int occ_update_device(struct occ *driver);
+int occ_set_user_powercap(struct occ *occ, u16 cap);
+
+#endif /* __OCC_H__ */
drivers/hwmon/occ/scom.h new file +47 -0
new file mode 100644
index 0000000..b0691f3
@@ -0,0 +1,47 @@
+/*
+ * scom.h - hwmon OCC driver
+ *
+ * This file contains data structures for scom operations to the OCC
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __SCOM_H__
+#define __SCOM_H__
+
+/*
+ * occ_bus_ops - represent the low-level transfer methods to communicate with
+ * the OCC.
+ *
+ * getscom - OCC scom read
+ * @bus: handle to slave device
+ * @address: address
+ * @data: where to store data read from slave; buffer size must be at least
+ * eight bytes.
+ *
+ * Returns 0 on success or a negative errno on error
+ *
+ * putscom - OCC scom write
+ * @bus: handle to slave device
+ * @address: address
+ * @data0: first data word to write
+ * @data1: second data word to write
+ *
+ * Returns 0 on success or a negative errno on error
+ */
+struct occ_bus_ops {
+	int (*getscom)(void *bus, u32 address, u64 *data);
+	int (*putscom)(void *bus, u32 address, u32 data0, u32 data1);
+};
+
+#endif /* __SCOM_H__ */
maintainer
#6 lore raw

Re: [PATCH linux v4 2/6] hwmon: occ: Add sysfs interface

In reply to [email protected]
Quoted 191 lines · On 01/26/2017 03:19 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 259 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  30 +++++
 4 files changed, 352 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +62 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many of the
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be used.
+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom sensor.
+
+Below are detailed the names and meaning of each sensor file for both types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+	all processors:
+		in<x>_input - frequency value
+		in<x>_label - sensor id
+temperature:
+	POWER8:
+		temp<x>_input - temperature value
+		temp<x>_label - sensor id
+	POWER9 (in addition to above):
+		temp<x>_type - FRU type
+
+power:
+	POWER8:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average - accumulator
+		power<x>_average_interval - update tag (number of samples in
+			accumulator)
+	POWER9:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average_min - accumulator[0]
+		power<x>_average_max - accumulator[1] (64 bits total)
+		power<x>_average_interval - update tag
+		power<x>_reset_history - (function_id | (apss_channel << 8)
+
+caps:
+	POWER8:
+		power<x>_cap - current powercap
+		power<x>_cap_max - max powercap
+		power<x>_cap_min - min powercap
+		power<x>_max - normal powercap
+		power<x>_alarm - user powercap, r/w
+	POWER9:
+		power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be multiple
+OCCs present on the system, these two files are only present for the "master"
+OCC.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
 BMC - Host Communications
 -------------------------
 
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
drivers/hwmon/occ/occ_sysfs.c new file +89 -0
new file mode 100644
index 0000000..ea2aa04
@@ -0,0 +1,259 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for implementing the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL	0x13
+#define OCC_HWMON_NAME_LENGTH	32
+
+struct occ_sysfs {
+	struct device *dev;
+	struct occ *occ;
+
+	char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
+	const u32 *sensor_hwmon_configs;
+	struct hwmon_channel_info **occ_sensors;
+	struct hwmon_chip_info occ_info;
+	u16 user_powercap;
+};
+
+static int occ_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			  u32 attr, int channel, long *val)
+{
+	int rc;
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	struct occ *occ = driver->occ;
+
+	switch (type) {
+	case hwmon_in:
+		rc = occ_get_sensor_field(occ, FREQ, channel, attr, val);
+		break;
+	case hwmon_temp:
+		rc = occ_get_sensor_field(occ, TEMP, channel, attr, val);
+		break;
+	case hwmon_power:
+		rc = occ_get_sensor_field(occ, POWER, channel, attr, val);
+		break;
+	default:
+		rc = -EOPNOTSUPP;
+	}
+
+	return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+				 enum hwmon_sensor_types type, u32 attr,
+				 int channel, char **str)
+{
+	int rc;
+	unsigned long val = 0;
+
+	if (!((type == hwmon_in && attr == hwmon_in_label) ||
+	    (type == hwmon_temp && attr == hwmon_temp_label) ||
+	    (type == hwmon_power && attr == hwmon_power_label)))
+		return -EOPNOTSUPP;
+
+	/* will fetch the "label", the sensor_id */
+	rc = occ_hwmon_read(dev, type, attr, channel, &val);
+	if (rc < 0)
+		return rc;
+
+	rc = snprintf(*str, PAGE_SIZE - 1, "%lu", val);

drivers/hwmon/occ/occ_sysfs.c line 99

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+	if (rc > 0)
+		rc = 0;
+
+	return rc;
+}
+
+static int occ_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
+			   u32 attr, int channel, long val)
+{
+	int rc = 0;

Unnecessary initialization.

drivers/hwmon/occ/occ_sysfs.c line 105

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	if (type == hwmon_power && attr == hwmon_power_alarm) {
+		rc = occ_set_user_powercap(driver->occ, val);
+		if (rc) {
+			if (rc == RESP_RETURN_CMD_INVAL) {

Is the mix of private and public error codes necessary ? Any chance the called code can just return -EINVAL ?

Quoted 17 lines
drivers/hwmon/occ/occ_sysfs.c new file +17 -0
+				dev_err(dev,
+					"set invalid powercap value: %ld\n",
+					val);
+				return -EINVAL;
+			}
+
+			dev_err(dev, "set user powercap failed: 0x:%x\n", rc);
+			return rc;
+		}
+
+		driver->user_powercap = val;
+
+		return rc;
+	}
+
+	return -EOPNOTSUPP;
+}

drivers/hwmon/occ/occ_sysfs.c line 132

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+static umode_t occ_is_visible(const void *data, enum hwmon_sensor_types type,
+			      u32 attr, int channel)
+{
+	const struct occ_sysfs *driver = data;
+
+	switch (type) {
+	case hwmon_chip:
+		if (attr == hwmon_chip_update_interval)
+			return S_IRUGO | S_IWUSR;

It might be better to just use octals directly to avoid the annoying checkpatch warnings.

Quoted 26 lines
drivers/hwmon/occ/occ_sysfs.c new file +26 -0
+		break;
+	case hwmon_in:
+		if (BIT(attr) & driver->sensor_hwmon_configs[0])
+			return S_IRUGO;
+		break;
+	case hwmon_temp:
+		if (BIT(attr) & driver->sensor_hwmon_configs[1])
+			return S_IRUGO;
+		break;
+	case hwmon_power:
+		/* user power limit */
+		if (attr == hwmon_power_alarm)
+			return S_IRUGO | S_IWUSR;
+		else if ((BIT(attr) & driver->sensor_hwmon_configs[2]) ||
+			 (BIT(attr) & driver->sensor_hwmon_configs[3]))
+			return S_IRUGO;
+		break;
+	default:
+		return 0;
+	}
+
+	return 0;
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+	.is_visible = occ_is_visible,

drivers/hwmon/occ/occ_sysfs.c line 168

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+	.read = occ_hwmon_read,
+	.read_string = occ_hwmon_read_string,
+	.write = occ_hwmon_write,
+};
+
+static const enum hwmon_sensor_types occ_sensor_types[MAX_OCC_SENSOR_TYPE] = {
+	hwmon_in,
+	hwmon_temp,
+	hwmon_power,
+	hwmon_power

So you have two sets of power sensors, meaning you'll end up with two sets of hwmon_channel_info describing hwmon_power ? This kind of misuses the API. I hope that doesn't create duplicate attributes; I think it might if the actually supported attributes overlap. Also, this means that the attribute index association gets lost. For example, if the alarm attributes are in the second group, power1_alarm would not necessarily align with, say, power1_max.

Is there any chance to avoid this ?

Quoted 128 lines
drivers/hwmon/occ/occ_sysfs.c new file +91 -0
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  const u32 *sensor_hwmon_configs,
+				  const char *name)
+{
+	int rc, i, j, num_sensors, index = 0, id;
+	char *brk;
+	struct occ_blocks *resp = NULL;
+	u32 *sensor_config;
+	struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+					       GFP_KERNEL);
+	if (!hwmon)
+		return ERR_PTR(-ENOMEM);
+
+	/* need space for null-termination */
+	hwmon->occ_sensors =
+		devm_kzalloc(dev, sizeof(struct hwmon_channel_info *) *
+			     (MAX_OCC_SENSOR_TYPE + 1), GFP_KERNEL);
+	if (!hwmon->occ_sensors)
+		return ERR_PTR(-ENOMEM);
+
+	hwmon->occ = occ;
+	hwmon->sensor_hwmon_configs = sensor_hwmon_configs;
+	hwmon->occ_info.ops = &occ_hwmon_ops;
+	hwmon->occ_info.info =
+		(const struct hwmon_channel_info **)hwmon->occ_sensors;
+
+	occ_get_response_blocks(occ, &resp);
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+		resp->sensor_block_id[i] = -1;
+
+	/* read sensor data from occ */
+	rc = occ_update_device(occ);
+	if (rc)
+		return ERR_PTR(rc);
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+		id = resp->sensor_block_id[i];
+		if (id < 0)
+			continue;
+
+		num_sensors = resp->blocks[id].header.num_sensors;
+		/* need null-termination */
+		sensor_config = devm_kzalloc(dev,
+					     sizeof(u32) * (num_sensors + 1),
+					     GFP_KERNEL);
+		if (!sensor_config)
+			return ERR_PTR(-ENOMEM);
+
+		for (j = 0; j < num_sensors; j++)
+			sensor_config[j] = sensor_hwmon_configs[i];
+
+		hwmon->occ_sensors[index] =
+			devm_kzalloc(dev, sizeof(struct hwmon_channel_info),
+				     GFP_KERNEL);
+		if (!hwmon->occ_sensors[index])
+			return ERR_PTR(-ENOMEM);
+
+		hwmon->occ_sensors[index]->type = occ_sensor_types[i];
+		hwmon->occ_sensors[index]->config = sensor_config;
+		index++;
+	}
+
+	/* search for bad chars */
+	strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+	brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+	while (brk) {
+		*brk = '_';
+		brk = strpbrk(brk,  "-* \t\n");
+	}
+
+	hwmon->dev = devm_hwmon_device_register_with_info(dev,
+							  hwmon->hwmon_name,
+							  hwmon,
+							  &hwmon->occ_info,
+							  NULL);
+	if (IS_ERR(hwmon->dev)) {
+		dev_err(dev, "cannot register hwmon device %s: %ld\n",
+			hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+		return ERR_CAST(hwmon->dev);
+	}
+
+	return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +30 -0
new file mode 100644
index 0000000..462f9d2
@@ -0,0 +1,30 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes for the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+struct occ;
+struct device;
+struct occ_sysfs;
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  const u32 *sensor_hwmon_configs,
+				  const char *name);
+#endif /* __OCC_SYSFS_H__ */
#7 lore raw

Re: [PATCH linux v4 2/6] hwmon: occ: Add sysfs interface

Guenter Roeck <[email protected]> wrote on 01/29/2017 01:50:55 PM:

Quoted 24 lines · On 01/26/2017 03:19 PM, [email protected] wrote:
Quoted 11 lines

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 259 +++++++++++++++++++++++++++++

+++++++++++++

Documentation/hwmon/occ Currently, all versions of the OCC support four

Quoted 10 lines
 drivers/hwmon/occ/occ_sysfs.h |  30 +++++
 4 files changed, 352 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +0 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four

types of sensor data: power,

Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 25

Quoted 1 line
Documentation/hwmon/occ +0 -0
 temperature, frequency, and "caps," which indicate limits and

thresholds used

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 6 lines
Documentation/hwmon/occ +4 -0
 internally on the OCC.
 
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to

userspace.

Quoted 2 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 33

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+
+The driver exports a number of sysfs files for each type of sensor.

The

Quoted 5 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 34

Quoted 1 line
Documentation/hwmon/occ +1 -0
+sensor-specific files vary depending on the processor type,

though many of the

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be

used.

Quoted 3 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 38

Quoted 1 line
Documentation/hwmon/occ +1 -0
+Therefore, the frequency sensor on the OCC uses the "input" type

sensor defined

Quoted 1 line
Documentation/hwmon/occ +1 -0
+by the hwmon interface, rather than defining a new type of custom

sensor.

Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 41

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+
+Below are detailed the names and meaning of each sensor file for

both types of

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 42

Quoted 1 line
Documentation/hwmon/occ +1 -0
+processors. All sensors are read-only unless otherwise specified.

<x> indicates

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 43

Quoted 1 line
Documentation/hwmon/occ +1 -0
+the hwmon index. sensor id indicates the unique internal OCC

identifer. Please

Quoted 1 line
Documentation/hwmon/occ +1 -0
+see the POWER OCC specification for details on all these sensor

values.

Quoted 31 lines
Quoted 31 lines
Documentation/hwmon/occ +31 -0
+
+frequency:
+   all processors:
+      in<x>_input - frequency value
+      in<x>_label - sensor id
+temperature:
+   POWER8:
+      temp<x>_input - temperature value
+      temp<x>_label - sensor id
+   POWER9 (in addition to above):
+      temp<x>_type - FRU type
+
+power:
+   POWER8:
+      power<x>_input - power value
+      power<x>_label - sensor id
+      power<x>_average - accumulator
+      power<x>_average_interval - update tag (number of samples in
+         accumulator)
+   POWER9:
+      power<x>_input - power value
+      power<x>_label - sensor id
+      power<x>_average_min - accumulator[0]
+      power<x>_average_max - accumulator[1] (64 bits total)
+      power<x>_average_interval - update tag
+      power<x>_reset_history - (function_id | (apss_channel << 8)
+
+caps:
+   POWER8:
+      power<x>_cap - current powercap
+      power<x>_cap_max - max powercap
Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 8 lines
Documentation/hwmon/occ +8 -0
+      power<x>_cap_min - min powercap
+      power<x>_max - normal powercap
+      power<x>_alarm - user powercap, r/w
+   POWER9:
+      power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be

multiple

Quoted 5 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 84

Quoted 1 line
Documentation/hwmon/occ +1 -0
+OCCs present on the system, these two files are only present for

the "master"

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+OCC.
+   name - read the name of the driver
+   update_interval - read or write the minimum interval for polling

the

Quoted 5 lines
Quoted 5 lines
Documentation/hwmon/occ +2 -0
+      OCC.
+
 BMC - Host Communications
 -------------------------
 
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.c

Quoted 15 lines

drivers/hwmon/occ/occ_sysfs.c line 4

Quoted 9 lines

new file mode 100644
index 0000000..ea2aa04

drivers/hwmon/occ/occ_sysfs.c new file +4 -0
@@ -0,0 +1,259 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for

implementing the OCC

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_sysfs.c line 10

Quoted 1 line
drivers/hwmon/occ/occ_sysfs.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 30 lines
Quoted 30 lines
drivers/hwmon/occ/occ_sysfs.c new file +30 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL   0x13
+#define OCC_HWMON_NAME_LENGTH   32
+
+struct occ_sysfs {
+   struct device *dev;
+   struct occ *occ;
+
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+   char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
+   const u32 *sensor_hwmon_configs;
+   struct hwmon_channel_info **occ_sensors;
+   struct hwmon_chip_info occ_info;
+   u16 user_powercap;
+};
+
+static int occ_hwmon_read(struct device *dev, enum hwmon_sensor_types

type,

Quoted 85 lines
Quoted 38 lines
drivers/hwmon/occ/occ_sysfs.c new file +38 -0
+           u32 attr, int channel, long *val)
+{
+   int rc;
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+   struct occ *occ = driver->occ;
+
+   switch (type) {
+   case hwmon_in:
+      rc = occ_get_sensor_field(occ, FREQ, channel, attr, val);
+      break;
+   case hwmon_temp:
+      rc = occ_get_sensor_field(occ, TEMP, channel, attr, val);
+      break;
+   case hwmon_power:
+      rc = occ_get_sensor_field(occ, POWER, channel, attr, val);
+      break;
+   default:
+      rc = -EOPNOTSUPP;
+   }
+
+   return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+             enum hwmon_sensor_types type, u32 attr,
+             int channel, char **str)
+{
+   int rc;
+   unsigned long val = 0;
+
+   if (!((type == hwmon_in && attr == hwmon_in_label) ||
+       (type == hwmon_temp && attr == hwmon_temp_label) ||
+       (type == hwmon_power && attr == hwmon_power_label)))
+      return -EOPNOTSUPP;
+
+   /* will fetch the "label", the sensor_id */
+   rc = occ_hwmon_read(dev, type, attr, channel, &val);
+   if (rc < 0)

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+      return rc;
+
+   rc = snprintf(*str, PAGE_SIZE - 1, "%lu", val);
+   if (rc > 0)
+      rc = 0;
+
+   return rc;
+}
+
+static int occ_hwmon_write(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/occ/occ_sysfs.c line 99

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+            u32 attr, int channel, long val)
+{
+   int rc = 0;

Unnecessary initialization.

drivers/hwmon/occ/occ_sysfs.c line 105

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   if (type == hwmon_power && attr == hwmon_power_alarm) {
+      rc = occ_set_user_powercap(driver->occ, val);
+      if (rc) {
+         if (rc == RESP_RETURN_CMD_INVAL) {

Is the mix of private and public error codes necessary ? Any chance the called code can just return -EINVAL ?

Quoted 9 lines
drivers/hwmon/occ/occ_sysfs.c new file +9 -0
+            dev_err(dev,
+               "set invalid powercap value: %ld\n",
+               val);
+            return -EINVAL;
+         }
+
+         dev_err(dev, "set user powercap failed: 0x:%x\n", rc);
+         return rc;
+      }

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+      driver->user_powercap = val;
+
+      return rc;
+   }
+
+   return -EOPNOTSUPP;
+}
+
+static umode_t occ_is_visible(const void *data, enum

hwmon_sensor_types type,

Quoted 10 lines

drivers/hwmon/occ/occ_sysfs.c line 132

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               u32 attr, int channel)
+{
+   const struct occ_sysfs *driver = data;
+
+   switch (type) {
+   case hwmon_chip:
+      if (attr == hwmon_chip_update_interval)
+         return S_IRUGO | S_IWUSR;

It might be better to just use octals directly to avoid the

annoyingcheckpatch

Quoted 35 lines

warnings.

Quoted 22 lines
drivers/hwmon/occ/occ_sysfs.c new file +22 -0
+      break;
+   case hwmon_in:
+      if (BIT(attr) & driver->sensor_hwmon_configs[0])
+         return S_IRUGO;
+      break;
+   case hwmon_temp:
+      if (BIT(attr) & driver->sensor_hwmon_configs[1])
+         return S_IRUGO;
+      break;
+   case hwmon_power:
+      /* user power limit */
+      if (attr == hwmon_power_alarm)
+         return S_IRUGO | S_IWUSR;
+      else if ((BIT(attr) & driver->sensor_hwmon_configs[2]) ||
+          (BIT(attr) & driver->sensor_hwmon_configs[3]))
+         return S_IRUGO;
+      break;
+   default:
+      return 0;
+   }
+
+   return 0;

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+   .is_visible = occ_is_visible,
+   .read = occ_hwmon_read,
+   .read_string = occ_hwmon_read_string,
+   .write = occ_hwmon_write,
+};
+
+static const enum hwmon_sensor_types occ_sensor_types

[MAX_OCC_SENSOR_TYPE] = {

Quoted 6 lines

drivers/hwmon/occ/occ_sysfs.c line 168

Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+   hwmon_in,
+   hwmon_temp,
+   hwmon_power,
+   hwmon_power

So you have two sets of power sensors, meaning you'll end up with two

sets

Quoted 1 line

of hwmon_channel_info describing hwmon_power ? This kind of misuses the

API.

Quoted 2 lines

I hope that doesn't create duplicate attributes; I think it might if the actually supported attributes overlap. Also, this means that

theattribute

Quoted 5 lines

index association gets lost. For example, if the alarm attributes are in the second group, power1_alarm would not necessarily align with, say, power1_max.

Is there any chance to avoid this ?

They will overlap if we use the attributes incorrectly. Those are defined in
occ_p8.c or occ_p9.c, as they specific to the OCC version. But the current definitions I have cannot overlap.

The problem is both the "caps" sensor and the "power" sensor coming off the OCC
are relevant to the power hwmon type, so I didn't want to use another type. The
problem is that for each OCC, there may be any number of power sensors, but only
one caps sensor. So I can't combine the two, as I believe that would create all
the hwmon power attributes specified (for both "caps" and "power" sensors) for
each power sensor. However, this does mean the indexing is not an issue, as the
caps sensor doesn't need to correspond to the power sensors at all.

This current set up works pretty well for us, though I agree it is a little confusing. Not sure of any more ideal way to do it. Our sensors just don't line up
well with the currently defined hwmon types.

Thanks for the feedback,
Eddie

Quoted 12 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 12 lines
drivers/hwmon/occ/occ_sysfs.c new file +11 -0
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              const u32 *sensor_hwmon_configs,
+              const char *name)
+{
+   int rc, i, j, num_sensors, index = 0, id;
+   char *brk;
+   struct occ_blocks *resp = NULL;
+   u32 *sensor_config;
+   struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct

occ_sysfs),

Quoted 73 lines
Quoted 73 lines
drivers/hwmon/occ/occ_sysfs.c new file +73 -0
+                      GFP_KERNEL);
+   if (!hwmon)
+      return ERR_PTR(-ENOMEM);
+
+   /* need space for null-termination */
+   hwmon->occ_sensors =
+      devm_kzalloc(dev, sizeof(struct hwmon_channel_info *) *
+              (MAX_OCC_SENSOR_TYPE + 1), GFP_KERNEL);
+   if (!hwmon->occ_sensors)
+      return ERR_PTR(-ENOMEM);
+
+   hwmon->occ = occ;
+   hwmon->sensor_hwmon_configs = sensor_hwmon_configs;
+   hwmon->occ_info.ops = &occ_hwmon_ops;
+   hwmon->occ_info.info =
+      (const struct hwmon_channel_info **)hwmon->occ_sensors;
+
+   occ_get_response_blocks(occ, &resp);
+
+   for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+      resp->sensor_block_id[i] = -1;
+
+   /* read sensor data from occ */
+   rc = occ_update_device(occ);
+   if (rc)
+      return ERR_PTR(rc);
+
+   for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+      id = resp->sensor_block_id[i];
+      if (id < 0)
+         continue;
+
+      num_sensors = resp->blocks[id].header.num_sensors;
+      /* need null-termination */
+      sensor_config = devm_kzalloc(dev,
+                    sizeof(u32) * (num_sensors + 1),
+                    GFP_KERNEL);
+      if (!sensor_config)
+         return ERR_PTR(-ENOMEM);
+
+      for (j = 0; j < num_sensors; j++)
+         sensor_config[j] = sensor_hwmon_configs[i];
+
+      hwmon->occ_sensors[index] =
+         devm_kzalloc(dev, sizeof(struct hwmon_channel_info),
+                 GFP_KERNEL);
+      if (!hwmon->occ_sensors[index])
+         return ERR_PTR(-ENOMEM);
+
+      hwmon->occ_sensors[index]->type = occ_sensor_types[i];
+      hwmon->occ_sensors[index]->config = sensor_config;
+      index++;
+   }
+
+   /* search for bad chars */
+   strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+   brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+   while (brk) {
+      *brk = '_';
+      brk = strpbrk(brk,  "-* \t\n");
+   }
+
+   hwmon->dev = devm_hwmon_device_register_with_info(dev,
+                       hwmon->hwmon_name,
+                       hwmon,
+                       &hwmon->occ_info,
+                       NULL);
+   if (IS_ERR(hwmon->dev)) {
+      dev_err(dev, "cannot register hwmon device %s: %ld\n",
+         hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+      return ERR_CAST(hwmon->dev);
+   }
+
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+   return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.h

Quoted 15 lines

drivers/hwmon/occ/occ_sysfs.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..462f9d2

drivers/hwmon/occ/occ_sysfs.h new file +4 -0
@@ -0,0 +1,30 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes

for the OCC

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_sysfs.h line 10

Quoted 1 line
drivers/hwmon/occ/occ_sysfs.h new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 22 lines
Quoted 21 lines
drivers/hwmon/occ/occ_sysfs.h new file +20 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+struct occ;
+struct device;
+struct occ_sysfs;
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              const u32 *sensor_hwmon_configs,
+              const char *name);
+#endif /* __OCC_SYSFS_H__ */