linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components
@ 2024-01-24 12:21 Oleksij Rempel
  2024-01-24 12:21 ` [PATCH v2 1/8] power: Extend power_on_reason.h for upcoming PSCRR framework Oleksij Rempel
                   ` (7 more replies)
  0 siblings, 8 replies; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:21 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

changes v2:
- rename the framework from PSCR to PSCRR (last R is for Recorder)
- extend power on reason header and use it to show detected reason on
  system start and in sysfs.
- remove "unknow" reason
- rebase on top of v6.8-rc1
- yaml fixes
- zero reason state on boot

Hello all,

This patch series introduces the Power State Change Reasons (PSCR)
tracking framework and its related components into the kernel. The PSCR
framework is designed for systems where traditional methods of storing
power state change reasons, like PMICs or watchdogs, are inadequate. It
provides a structured way to store reasons for system shutdowns and
reboots, such as under-voltage or software-triggered events, in
non-volatile hardware storage.

These changes are critical for systems requiring detailed postmortem
analysis and where immediate power-down scenarios limit traditional
storage options. The framework also assists bootloaders and early-stage
system components in making informed recovery decisions.

Oleksij Rempel (8):
  power: Extend power_on_reason.h for upcoming PSCRR framework
  dt-bindings: power: reset: add generic PSCRR binding trackers
  power: reset: Introduce PSCR Recording Framework for Non-Volatile
    Storage
  dt-bindings: power: reset: add bindings for NVMEM hardware storing
    PSCR Data
  nvmem: provide consumer access to cell size metrics
  power: reset: add PSCR NVMEM Driver for Recording Power State Change
    Reasons
  regulator: set Power State Change Reason before
    hw_protection_shutdown()
  thermal: core: Record PSCR before hw_protection_shutdown()

 .../bindings/power/reset/pscrr-nvmem.yaml     |  53 +++
 .../bindings/power/reset/pscrr.yaml           |  44 +++
 drivers/nvmem/core.c                          |  25 ++
 drivers/power/reset/Kconfig                   |  30 ++
 drivers/power/reset/Makefile                  |   2 +
 drivers/power/reset/pscrr-nvmem.c             | 121 ++++++
 drivers/power/reset/pscrr.c                   | 353 ++++++++++++++++++
 drivers/regulator/core.c                      |   6 +
 drivers/thermal/thermal_core.c                |   3 +
 include/linux/nvmem-consumer.h                |   7 +
 include/linux/power/power_on_reason.h         |   3 +
 include/linux/pscrr.h                         |  73 ++++
 12 files changed, 720 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr.yaml
 create mode 100644 drivers/power/reset/pscrr-nvmem.c
 create mode 100644 drivers/power/reset/pscrr.c
 create mode 100644 include/linux/pscrr.h

-- 
2.39.2


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

* [PATCH v2 1/8] power: Extend power_on_reason.h for upcoming PSCRR framework
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
@ 2024-01-24 12:21 ` Oleksij Rempel
  2024-01-24 12:21 ` [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers Oleksij Rempel
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:21 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Prepare for the introduction of the Power State Change Reason Recorder
(PSCRR)  framework by expanding the power_on_reason.h header. This
extension includes new power-on reasons:
- POWER_ON_REASON_OVER_CURRENT for over-current conditions.
- POWER_ON_REASON_REGULATOR_FAILURE for regulator failures.
- POWER_ON_REASON_OVERTEMPERATURE for over temperature situations.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 include/linux/power/power_on_reason.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/linux/power/power_on_reason.h b/include/linux/power/power_on_reason.h
index 95a1ec0c403c..f2446cece277 100644
--- a/include/linux/power/power_on_reason.h
+++ b/include/linux/power/power_on_reason.h
@@ -15,5 +15,8 @@
 #define POWER_ON_REASON_XTAL_FAIL "crystal oscillator failure"
 #define POWER_ON_REASON_BROWN_OUT "brown-out reset"
 #define POWER_ON_REASON_UNKNOWN "unknown reason"
+#define POWER_ON_REASON_OVER_CURRENT "over current"
+#define POWER_ON_REASON_REGULATOR_FAILURE "regulator failure"
+#define POWER_ON_REASON_OVERTEMPERATURE "overtemperature"
 
 #endif /* POWER_ON_REASON_H */
-- 
2.39.2


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

* [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
  2024-01-24 12:21 ` [PATCH v2 1/8] power: Extend power_on_reason.h for upcoming PSCRR framework Oleksij Rempel
@ 2024-01-24 12:21 ` Oleksij Rempel
  2024-01-25 10:54   ` Krzysztof Kozlowski
  2024-01-24 12:21 ` [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage Oleksij Rempel
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:21 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Add binding for Power State Change Reason Recording (PSCRR) subsystem

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 .../bindings/power/reset/pscrr.yaml           | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr.yaml

diff --git a/Documentation/devicetree/bindings/power/reset/pscrr.yaml b/Documentation/devicetree/bindings/power/reset/pscrr.yaml
new file mode 100644
index 000000000000..c8738b4930fe
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/reset/pscrr.yaml
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/reset/pscrr.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Power State Change Reason (PSCR)
+
+maintainers:
+  - Oleksij Rempel <o.rempel@pengutronix.de>
+
+description: Binding for devices responsible to store reasons for power state
+  changes such as reboot and power-off. Reasons like unknown, under voltage,
+  and over temperature are captured for diagnostic or automatic recovery
+  purposes.
+
+properties:
+  pscr-under-voltage:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Value to indicate an under-voltage condition of a system critical
+      regulator as the reason for the power state change.
+
+  pscr-over-current:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Value to indicate an over-current condition of a system ctitical regulator
+      as the reason for the power state change.
+
+  pscr-regulator-failure:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Value to indicate an unknown, system ctitical regulator related failure
+      as the reason for the power state change.
+
+  pscr-over-temperature:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Value to indicate a system critical over-temperature condition as the
+      reason for the power state change.
+
+additionalProperties: true
+
+...
-- 
2.39.2


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

* [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
  2024-01-24 12:21 ` [PATCH v2 1/8] power: Extend power_on_reason.h for upcoming PSCRR framework Oleksij Rempel
  2024-01-24 12:21 ` [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers Oleksij Rempel
@ 2024-01-24 12:21 ` Oleksij Rempel
  2024-01-28  9:05   ` kernel test robot
  2024-01-24 12:22 ` [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:21 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

This commit introduces the Power State Change Reasons Recording (PSCRR)
framework into the kernel. The framework is vital for systems where
PMICs or watchdogs cannot provide information on power state changes. It
stores reasons for system shutdowns and reboots, like under-voltage or
software-triggered events, in non-volatile hardware storage. This
approach is essential for postmortem analysis in scenarios where
traditional storage methods (block devices, RAM) are not feasible. The
framework aids bootloaders and early-stage system components in recovery
decision-making, although it does not cover resets caused by hardware
issues like system freezes or watchdog timeouts.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/power/reset/Kconfig  |  19 ++
 drivers/power/reset/Makefile |   1 +
 drivers/power/reset/pscrr.c  | 353 +++++++++++++++++++++++++++++++++++
 include/linux/pscrr.h        |  73 ++++++++
 4 files changed, 446 insertions(+)
 create mode 100644 drivers/power/reset/pscrr.c
 create mode 100644 include/linux/pscrr.h

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index fece990af4a7..c6ce7e647048 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -305,3 +305,22 @@ config POWER_MLXBF
 	  This driver supports reset or low power mode handling for Mellanox BlueField.
 
 endif
+
+menuconfig PSCRR
+    bool "Power State Change Reasons (PSCR) Recording Framework"
+    help
+      Enables the Power State Change Reasons (PSCR) Recording framework.
+
+      This framework is designed to store reasons for system shutdowns or
+      reboots,  like under voltage or software-triggered events, in non-volatile
+      hardware storage. It is particularly useful for postmortem analysis, where
+      traditional storage methods (like block devices or RAM) are not feasible
+      due to immediate power-down requirements or insufficient power to retain
+      data.
+
+      This is useful for bootloaders or other early-stage system components to
+      make recovery decisions based on the last known system state. Note that it
+      does not cover hardware-induced resets like system freezes or watchdog
+      timeouts.
+
+      If unsure, say N.
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index a95d1bd275d1..e618c34a30f9 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_POWER_RESET_KEYSTONE) += keystone-reset.o
 obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_RMOBILE) += rmobile-reset.o
+obj-$(CONFIG_PSCRR) += pscrr.o
 obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o
diff --git a/drivers/power/reset/pscrr.c b/drivers/power/reset/pscrr.c
new file mode 100644
index 000000000000..651fc210878d
--- /dev/null
+++ b/drivers/power/reset/pscrr.c
@@ -0,0 +1,353 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2016, Fuzhou Rockchip Electronics Co., Ltd
+// Copyright (c) 2024 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
+/*
+ * Based on drivers/power/reset/reboot-mode.c
+ * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
+ */
+
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/power/power_on_reason.h>
+#include <linux/pscrr.h>
+#include <linux/reboot.h>
+
+#define PREFIX "pscr-"
+
+enum pscr system_pscr;
+
+/*
+ * struct pscr_to_magic_entry - Entry for mapping PSCR to magic values.
+ *
+ * This structure represents a single entry in a list that maps Power State
+ * Change Reason (PSCR) values to corresponding magic values. Each entry
+ * associates a specific PSCR with a unique magic value, facilitating easy
+ * translation between the two.
+ *
+ * @pscr: The PSCR value.
+ * @magic: The corresponding magic value.
+ * @list: List head for chaining multiple such entries.
+ */
+struct pscr_to_magic_entry {
+	enum pscr pscr;
+	u32 magic;
+	struct list_head list;
+};
+
+/*
+ * struct pscr_map - Maps device tree property names to PSCR values.
+ *
+ * @dt_prop_name: Device tree property name without the "pscr-" prefix.
+ * @pscr: The corresponding PSCR enum value for the given property name.
+ */
+struct pscr_map {
+	const char *dt_prop_name;
+	enum pscr pscr;
+};
+
+/*
+ * struct pscr_map - Maps shortened DT property names to PSCR values.
+ *
+ * This structure maps device tree property names, with the "pscr-" prefix
+ * omitted, to their corresponding Power State Change Reason (PSCR) values.
+ */
+struct pscr_map pscr_map_table[] = {
+	{ "under-voltage", PSCR_UNDER_VOLTAGE },
+	{ "over-current", PSCR_OVER_CURRENT },
+	{ "regulator-failure", PSCR_REGULATOR_FAILURE },
+	{ "over-temperature", PSCR_OVERTEMPERATURE },
+};
+
+/*
+ * pscr_find_from_dt_name - Finds the PSCR value for a given DT property name.
+ *
+ * @dt_prop_name: The device tree property name, without the "pscr-" prefix, to
+ * look up.
+ * Returns the corresponding PSCR value or -ENOENT if not found.
+ */
+static int find_pscr_by_string(const char *dt_prop_name)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(pscr_map_table); i++) {
+		if (!strcmp(dt_prop_name, pscr_map_table[i].dt_prop_name))
+			return pscr_map_table[i].pscr;
+	}
+
+	return -ENOENT;
+}
+
+static enum pscr get_pscr_by_magic(struct pscrr_device *pscrr_dev, u32 magic)
+{
+	struct pscr_to_magic_entry *map_entry;
+
+	list_for_each_entry(map_entry, &pscrr_dev->pscr_map_list, list) {
+		if (map_entry->magic == magic)
+			return map_entry->pscr;
+	}
+
+	return 0;
+}
+
+static u32 get_magic_by_pscr(struct pscrr_device *pscrr_dev, enum pscr pscr)
+{
+	struct pscr_to_magic_entry *map_entry;
+
+	list_for_each_entry(map_entry, &pscrr_dev->pscr_map_list, list) {
+		if (map_entry->pscr == pscr)
+			return map_entry->magic;
+	}
+
+	return 0;
+}
+
+/**
+ * set_power_state_change_reason() - Set the system's power state change reason
+ * @pscr: The enum value representing the power state change reason
+ *
+ * This function sets the system's power state change reason based on the
+ * provided enum value.
+ */
+void set_power_state_change_reason(enum pscr pscr)
+{
+	system_pscr = pscr;
+}
+EXPORT_SYMBOL_GPL(set_power_state_change_reason);
+
+static const char *pscr_to_por_string(enum pscr pscr)
+{
+	switch (pscr) {
+	case PSCR_UNDER_VOLTAGE:
+		return POWER_ON_REASON_BROWN_OUT;
+	case PSCR_OVER_CURRENT:
+		return POWER_ON_REASON_OVER_CURRENT;
+	case PSCR_REGULATOR_FAILURE:
+		return POWER_ON_REASON_REGULATOR_FAILURE;
+	case PSCR_OVERTEMPERATURE:
+		return POWER_ON_REASON_OVERTEMPERATURE;
+	default:
+	}
+
+	return POWER_ON_REASON_UNKNOWN;
+}
+
+static ssize_t power_state_change_reason_show(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct pscrr_device *pscrr_dev = dev_get_drvdata(dev);
+
+	return sprintf(buf, "%s\n", pscr_to_por_string(pscrr_dev->last_pscr));
+}
+static DEVICE_ATTR_RO(power_state_change_reason);
+
+int handle_last_pscr(struct pscrr_device *pscrr_dev)
+{
+	u32 magic;
+	int ret;
+
+	ret = pscrr_dev->read(pscrr_dev, &magic);
+	if (ret)
+		return ret;
+
+	pscrr_dev->last_pscr = get_pscr_by_magic(pscrr_dev, magic);
+
+	dev_info(pscrr_dev->dev, "Last recorded power state change reason: %s\n",
+		 pscr_to_por_string(pscrr_dev->last_pscr));
+
+	ret = pscrr_dev->write(pscrr_dev, 0);
+	if (ret)
+		dev_err(pscrr_dev->dev, "Failed to clear power state change reason\n");
+
+	return ret;
+}
+
+static int pscrr_notify(struct notifier_block *this, unsigned long x, void *c)
+{
+	struct pscrr_device *pscrr_dev = container_of(this, struct pscrr_device,
+						      reboot_notifier);
+	u32 magic;
+
+	magic = get_magic_by_pscr(pscrr_dev, system_pscr);
+	pscrr_dev->write(pscrr_dev, magic);
+
+	return NOTIFY_DONE;
+}
+
+/**
+ * pscrr_process_property() - Process a power state change reason property
+ * @pscrr_dev: Pointer to the pscrr_device structure
+ * @prop: Pointer to the property structure to be processed
+ *
+ * This function processes a device tree property representing a power state
+ * change reason and initializes the relevant data structures.
+ *
+ * Returns: 0 on success, -ENOMEM on memory allocation failure.
+ */
+static int pscrr_process_property(struct pscrr_device *pscrr_dev,
+				  struct property *prop)
+{
+	struct pscr_to_magic_entry *map_entry;
+	struct device *dev = pscrr_dev->dev;
+	size_t len = strlen(PREFIX);
+	int ret;
+
+	if (strncmp(prop->name, PREFIX, len))
+		return 0;
+
+	map_entry = devm_kzalloc(dev, sizeof(*map_entry), GFP_KERNEL);
+	if (!map_entry)
+		return -ENOMEM;
+
+	ret = of_property_read_u32(dev->of_node, prop->name, &map_entry->magic);
+	if (ret) {
+		dev_err(dev, "Can't read magic number for %s: %pe\n",
+			prop->name, ERR_PTR(ret));
+		devm_kfree(dev, map_entry);
+		return 0;
+	}
+
+	if (!map_entry->magic) {
+		dev_err(dev, "%s with magic number == 0\n", prop->name);
+		devm_kfree(dev, map_entry);
+		return 0;
+	}
+
+	map_entry->pscr = find_pscr_by_string(prop->name + len);
+	if (map_entry->pscr < 0) {
+		dev_err(dev, "unsupported reason name(%s): %pe\n",
+			prop->name, ERR_PTR(map_entry->pscr));
+		devm_kfree(dev, map_entry);
+		return 0;
+	}
+
+	if (map_entry->magic > pscrr_dev->max_magic_val)
+		pscrr_dev->max_magic_val = map_entry->magic;
+
+	dev_dbg(dev, "registering reason = %s, magic = %d, pscr = %d\n",
+		prop->name, map_entry->magic, map_entry->pscr);
+	list_add_tail(&map_entry->list, &pscrr_dev->pscr_map_list);
+
+	return 0;
+}
+
+/*
+ * pscrr_register() - Register the pscr driver and initialize power state change
+ *                   reasons
+ * @pscrr_dev: Pointer to the pscrr_device structure
+ *
+ * This function registers the pscr driver and initializes power state change
+ * reasons based on device tree properties.
+ *
+ * Returns: 0 on success, -ENOMEM on memory allocation failure
+ */
+int pscrr_register(struct pscrr_device *pscrr_dev)
+{
+	struct device_node *np = pscrr_dev->dev->of_node;
+	struct property *prop;
+	int ret;
+
+	INIT_LIST_HEAD(&pscrr_dev->pscr_map_list);
+
+	for_each_property_of_node(np, prop) {
+		ret = pscrr_process_property(pscrr_dev, prop);
+		if (ret)
+			return ret;
+	}
+
+	pscrr_dev->reboot_notifier.notifier_call = pscrr_notify;
+	register_reboot_notifier(&pscrr_dev->reboot_notifier);
+
+	dev_set_drvdata(pscrr_dev->dev, pscrr_dev);
+
+	ret = device_create_file(pscrr_dev->dev, &dev_attr_power_state_change_reason);
+	if (ret)
+		dev_err(pscrr_dev->dev, "Could not create sysfs entry\n");
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(pscrr_register);
+
+/*
+ * pscrr_unregister() - Unregister the pscr driver's reboot notifier
+ * @pscrr_dev: Pointer to the pscrr_device structure
+ *
+ * This function unregisters the reboot notifier for the pscr driver.
+ */
+void pscrr_unregister(struct pscrr_device *pscrr_dev)
+{
+	unregister_reboot_notifier(&pscrr_dev->reboot_notifier);
+}
+EXPORT_SYMBOL_GPL(pscrr_unregister);
+
+static void devm_pscrr_release(struct device *dev, void *res)
+{
+	pscrr_unregister(*(struct pscrr_device **)res);
+}
+
+/**
+ * devm_pscrr_register - Register a device-managed PSCR driver
+ * @dev: Device to associate the PSCR driver with
+ * @pscrr_dev: Pointer to the PSCR driver to be registered
+ *
+ * Registers a Power State Change Reason (PSCR) driver as a device-managed
+ * resource.
+ *
+ * Returns: 0 on successful registration or a negative error code on failure.
+ */
+int devm_pscrr_register(struct device *dev, struct pscrr_device *pscrr_dev)
+{
+	struct pscrr_device **dr;
+	int rc;
+
+	dr = devres_alloc(devm_pscrr_release, sizeof(*dr), GFP_KERNEL);
+	if (!dr)
+		return -ENOMEM;
+
+	rc = pscrr_register(pscrr_dev);
+	if (rc) {
+		devres_free(dr);
+		return rc;
+	}
+
+	*dr = pscrr_dev;
+	devres_add(dev, dr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_pscrr_register);
+
+static int devm_pscrr_match(struct device *dev, void *res, void *data)
+{
+	struct pscrr_device **p = res;
+
+	if (WARN_ON(!p || !*p))
+		return 0;
+
+	return *p == data;
+}
+
+/**
+ * devm_pscrr_unregister - Unregister a managed PSCR driver
+ * @dev: Device associated with the PSCR driver
+ * @pscrr_dev: Pointer to the PSCR driver to unregister
+ *
+ * Unregisters a device-managed Power State Change Reason (PSCR) driver.
+ * It handles the cleanup and release of resources associated with the PSCR
+ * driver which was previously registered.
+ */
+void devm_pscrr_unregister(struct device *dev,
+			   struct pscrr_device *pscrr_dev)
+{
+	WARN_ON(devres_release(dev,
+			       devm_pscrr_release,
+			       devm_pscrr_match, pscrr_dev));
+}
+EXPORT_SYMBOL_GPL(devm_pscrr_unregister);
+
+MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
+MODULE_DESCRIPTION("Power State Change Reason (PSCR) Recording framework");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/pscrr.h b/include/linux/pscrr.h
new file mode 100644
index 000000000000..2de9d9bc9aeb
--- /dev/null
+++ b/include/linux/pscrr.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PSCRR_H__
+#define __PSCRR_H__
+
+/*
+ * enum pscr - Enumerates reasons for power state changes.
+ *
+ * This enum lists the various reasons why a power state change might
+ * occur in a system. Each value represents a specific condition that
+ * could trigger a change in power state, such as shutdown or reboot.
+ *
+ * PSCR_UNKNOWN: Represents an unknown or unspecified reason.
+ * PSCR_UNDER_VOLTAGE: Indicates a power state change due to under-voltage.
+ * PSCR_OVER_CURRENT: Indicates a power state change due to over-current.
+ * PSCR_REGULATOR_FAILURE: Indicates a failure in a voltage regulator.
+ * PSCR_OVERTEMPERATURE: Indicates an over-temperature condition.
+ */
+enum pscr {
+	PSCR_UNKNOWN,
+	PSCR_UNDER_VOLTAGE,
+	PSCR_OVER_CURRENT,
+	PSCR_REGULATOR_FAILURE,
+	PSCR_OVERTEMPERATURE,
+};
+
+/*
+ * struct pscrr_device - Manages a Power State Change Reason Recorder device.
+ *
+ * This structure is utilized for controlling a device responsible for
+ * recording reasons for power state changes (PSCR). It includes mechanisms
+ * for mapping PSCR values to specific magic codes, storing these mappings,
+ * and recovering the last PSCR value from storage during system start-up.
+ *
+ * @dev: Device structure pointer.
+ * @pscr_map_list: List head for structs holding PSCR to magic code mappings.
+ * @write: Function pointer to write a new mapped PSCR value.
+ * @read: Function pointer to read the current mapped PSCR value.
+ * @reboot_notifier: Notifier block for recording PSCR at reboot.
+ * @max_magic_val: Maximum permissible magic code, used for verifying storage
+ *                 capacity and mapping integrity.
+ * @last_pscr: Last PSCR value recovered from storage at system start,
+ *             representing the reason for the last system power cycle.
+ */
+struct pscrr_device {
+	struct device *dev;
+	struct list_head pscr_map_list;
+	int (*write)(struct pscrr_device *pscrr_dev, u32 magic);
+	int (*read)(struct pscrr_device *pscrr_dev, u32 *magic);
+	struct notifier_block reboot_notifier;
+	u32 max_magic_val;
+	enum pscr last_pscr;
+};
+
+int pscrr_register(struct pscrr_device *pscrr_dev);
+void pscrr_unregister(struct pscrr_device *pscrr_dev);
+int devm_pscrr_register(struct device *dev,
+			struct pscrr_device *pscrr_dev);
+void devm_pscrr_unregister(struct device *dev,
+			   struct pscrr_device *pscrr_dev);
+int handle_last_pscr(struct pscrr_device *pscrr_dev);
+
+#if IS_ENABLED(CONFIG_PSCRR)
+
+void set_power_state_change_reason(enum pscr pscr);
+
+#else
+
+static inline void set_power_state_change_reason(enum pscr pscr)
+{
+}
+#endif
+
+#endif
-- 
2.39.2


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

* [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (2 preceding siblings ...)
  2024-01-24 12:21 ` [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage Oleksij Rempel
@ 2024-01-24 12:22 ` Oleksij Rempel
  2024-01-25 10:57   ` Krzysztof Kozlowski
  2024-01-24 12:22 ` [PATCH v2 5/8] nvmem: provide consumer access to cell size metrics Oleksij Rempel
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:22 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Add device tree bindings that describe hardware implementations of
Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
(PSCR).

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 .../bindings/power/reset/pscrr-nvmem.yaml     | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml

diff --git a/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml b/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
new file mode 100644
index 000000000000..779920dea283
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/reset/pscrr-nvmem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Generic NVMEM Power State Change Reason Recorder
+
+maintainers:
+  - Oleksij Rempel <o.rempel@pengutronix.de>
+
+description: This binding describes the Non-Volatile Memory (NVMEM) hardware
+  that stores Power State Change Reasons (PSCR).
+
+allOf:
+  - $ref: pscrr.yaml#
+
+properties:
+  compatible:
+    const: pscrr-nvmem
+
+  nvmem-cells:
+    description: |
+      A phandle pointing to the nvmem-cells node where the power state change
+      reasons are stored.
+    maxItems: 1
+
+  nvmem-cell-names:
+    items:
+      - const: pscr
+
+  pscr-under-voltage: true
+  pscr-over-current: true
+  pscr-regulator-failure: true
+  pscr-over-temperature: true
+
+required:
+  - compatible
+  - nvmem-cells
+  - nvmem-cell-names
+
+additionalProperties: false
+
+examples:
+  - |
+    power-state-change-reason {
+      compatible = "pscrr-nvmem";
+      nvmem-cells = <&pscr_cell>;
+      nvmem-cell-names = "pscr";
+      pscr-under-voltage = <1>;
+      pscr-over-temperature = <2>;
+    };
+...
-- 
2.39.2


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

* [PATCH v2 5/8] nvmem: provide consumer access to cell size metrics
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (3 preceding siblings ...)
  2024-01-24 12:22 ` [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
@ 2024-01-24 12:22 ` Oleksij Rempel
  2024-01-24 12:22 ` [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons Oleksij Rempel
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:22 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Add nvmem_cell_get_size() function to provide access to cell size
metrics. In some cases we may get cell size less as consumer would
expect it. So, nvmem_cell_write() would fail with incorrect buffer size.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/nvmem/core.c           | 25 +++++++++++++++++++++++++
 include/linux/nvmem-consumer.h |  7 +++++++
 2 files changed, 32 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 980123fb4dde..a21e5649fcda 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1790,6 +1790,31 @@ int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
 
 EXPORT_SYMBOL_GPL(nvmem_cell_write);
 
+/**
+ * nvmem_cell_get_size() - Get the size of a given nvmem cell
+ * @cell: nvmem cell to be queried.
+ * @bytes: Pointer to store the size of the cell in bytes. Can be NULL.
+ * @bits: Pointer to store the size of the cell in bits. Can be NULL.
+ *
+ * Return: 0 on success or negative on failure.
+ */
+int nvmem_cell_get_size(struct nvmem_cell *cell, size_t *bytes, size_t *bits)
+{
+	struct nvmem_cell_entry *entry = cell->entry;
+
+	if (!entry->nvmem)
+		return -EINVAL;
+
+	if (bytes)
+		*bytes = entry->bytes;
+
+	if (bits)
+		*bits = entry->nbits;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_get_size);
+
 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
 				  void *val, size_t count)
 {
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index 34c0e58dfa26..bcb0e17e415d 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -56,6 +56,7 @@ void nvmem_cell_put(struct nvmem_cell *cell);
 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
+int nvmem_cell_get_size(struct nvmem_cell *cell, size_t *bytes, size_t *bits);
 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val);
 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val);
 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val);
@@ -128,6 +129,12 @@ static inline int nvmem_cell_write(struct nvmem_cell *cell,
 	return -EOPNOTSUPP;
 }
 
+static inline int nvmem_cell_get_size(struct nvmem_cell *cell, size_t *bytes,
+				      size_t *bits)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline int nvmem_cell_read_u8(struct device *dev,
 				     const char *cell_id, u8 *val)
 {
-- 
2.39.2


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

* [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (4 preceding siblings ...)
  2024-01-24 12:22 ` [PATCH v2 5/8] nvmem: provide consumer access to cell size metrics Oleksij Rempel
@ 2024-01-24 12:22 ` Oleksij Rempel
  2024-01-28  6:38   ` kernel test robot
  2024-01-24 12:22 ` [PATCH v2 7/8] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
  2024-01-24 12:22 ` [PATCH v2 8/8] thermal: core: Record PSCR " Oleksij Rempel
  7 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:22 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

This driver utilizes the Power State Change Reasons Recording (PSCRR)
framework to store specific power state change information, such as
shutdown or reboot reasons, into a designated non-volatile memory
(NVMEM) cell.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/power/reset/Kconfig       |  11 +++
 drivers/power/reset/Makefile      |   1 +
 drivers/power/reset/pscrr-nvmem.c | 121 ++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 drivers/power/reset/pscrr-nvmem.c

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index c6ce7e647048..8e50e495ef98 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -324,3 +324,14 @@ menuconfig PSCRR
       timeouts.
 
       If unsure, say N.
+
+if PSCRR
+
+config PSCRR_NVMEM
+	tristate "Generic NVMEM-based Power State Change Reason Recorder"
+	depends on OF
+	help
+	  Enabling this option adds support for recording power state change
+	  reasons in a NVMEM cell.
+
+endif
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index e618c34a30f9..3860dbbacb23 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_RMOBILE) += rmobile-reset.o
 obj-$(CONFIG_PSCRR) += pscrr.o
+obj-$(CONFIG_PSCRR_NVMEM) += pscrr-nvmem.o
 obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o
diff --git a/drivers/power/reset/pscrr-nvmem.c b/drivers/power/reset/pscrr-nvmem.c
new file mode 100644
index 000000000000..fe9053b78b79
--- /dev/null
+++ b/drivers/power/reset/pscrr-nvmem.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) Vaisala Oyj. All rights reserved.
+// Copyright (c) 2024 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
+/*
+ * Based on drivers/power/reset/nvmem-reboot-mode.c
+ * Copyright (c) Vaisala Oyj. All rights reserved.
+ */
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pscrr.h>
+
+struct pscrr_nvmem {
+	struct pscrr_device pscrr_dev;
+	struct nvmem_cell *cell;
+	size_t max_magic_bytes;
+};
+
+static int pscrr_nvmem_write(struct pscrr_device *pscrr_dev, u32 magic)
+{
+	struct pscrr_nvmem *priv = container_of(pscrr_dev, struct pscrr_nvmem,
+						pscrr_dev);
+	size_t size = min(priv->max_magic_bytes, sizeof(magic));
+	int ret;
+
+	ret = nvmem_cell_write(priv->cell, &magic, size);
+	if (ret < 0) {
+		dev_err(pscrr_dev->dev, "update reason bits failed: %pe\n",
+			ERR_PTR(ret));
+		return ret;
+	}
+
+	return 0;
+}
+
+static int pscrr_nvmem_read(struct pscrr_device *pscrr_dev, u32 *magic)
+{
+	struct pscrr_nvmem *priv = container_of(pscrr_dev, struct pscrr_nvmem,
+						pscrr_dev);
+	size_t len;
+	void *buf;
+
+	buf = nvmem_cell_read(priv->cell, &len);
+	if (IS_ERR(buf))
+		return PTR_ERR(buf);
+
+	*magic = 0;
+	memcpy(magic, buf, min(len, sizeof(*magic)));
+	kfree(buf);
+
+	return 0;
+}
+
+static int pscrr_nvmem_probe(struct platform_device *pdev)
+{
+	size_t bytes, bits, magic_bits;
+	struct pscrr_nvmem *priv;
+	const char *pscr = "pscr";
+	int ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->pscrr_dev.dev = &pdev->dev;
+	priv->pscrr_dev.write = pscrr_nvmem_write;
+	priv->pscrr_dev.read = pscrr_nvmem_read;
+
+	priv->cell = devm_nvmem_cell_get(&pdev->dev, pscr);
+	if (IS_ERR(priv->cell))
+		return dev_err_probe(&pdev->dev, PTR_ERR(priv->cell),
+				     "failed to get the nvmem %s cell\n", pscr);
+
+	ret = nvmem_cell_get_size(priv->cell, &bytes, &bits);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "failed to get the nvmem %s size\n",
+				     pscr);
+
+	if (!bytes || bytes > sizeof(u32) || bits > 32)
+		return dev_err_probe(&pdev->dev, -EINVAL, "invalid nvmem %s size. bytes: %zu, bits: %zu\n",
+				     pscr, bytes, bits);
+
+	ret = devm_pscrr_register(&pdev->dev, &priv->pscrr_dev);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "failed to register pscr driver\n");
+
+	magic_bits = fls(priv->pscrr_dev.max_magic_val);
+	priv->max_magic_bytes = DIV_ROUND_UP(magic_bits, 8);
+
+	if (!bits)
+		bits = bytes * 8;
+
+	if (magic_bits > bits)
+		return dev_err_probe(&pdev->dev, -EINVAL, "provided magic can't fit into nvmem %s. bytes: %zu, bits: %zu, magic_bits: %zu\n",
+				     pscr, bytes, bits, magic_bits);
+
+	return handle_last_pscr(&priv->pscrr_dev);
+}
+
+static const struct of_device_id pscrr_nvmem_of_match[] = {
+	{ .compatible = "pscrr-nvmem" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, pscrr_nvmem_of_match);
+
+static struct platform_driver pscrr_nvmem_driver = {
+	.probe = pscrr_nvmem_probe,
+	.driver = {
+		.name = "pscrr-nvmem",
+		.of_match_table = pscrr_nvmem_of_match,
+	},
+};
+module_platform_driver(pscrr_nvmem_driver);
+
+MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
+MODULE_DESCRIPTION("NVMEM Driver for Power State Change Reason Recording");
+MODULE_LICENSE("GPL");
-- 
2.39.2


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

* [PATCH v2 7/8] regulator: set Power State Change Reason before hw_protection_shutdown()
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (5 preceding siblings ...)
  2024-01-24 12:22 ` [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons Oleksij Rempel
@ 2024-01-24 12:22 ` Oleksij Rempel
  2024-01-24 12:22 ` [PATCH v2 8/8] thermal: core: Record PSCR " Oleksij Rempel
  7 siblings, 0 replies; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:22 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, Mark Brown, kernel, linux-kernel, Liam Girdwood,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Store the state change reason to some black box, for later
investigation.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Reviewed-by: Mark Brown <broonie@kernel.org>
---
 drivers/regulator/core.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index a968dabb48f5..a811a5ff2273 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -19,6 +19,7 @@
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
+#include <linux/pscrr.h>
 #include <linux/reboot.h>
 #include <linux/regmap.h>
 #include <linux/regulator/of_regulator.h>
@@ -5095,6 +5096,7 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);
 static void regulator_handle_critical(struct regulator_dev *rdev,
 				      unsigned long event)
 {
+	enum pscr pscr;
 	const char *reason = NULL;
 
 	if (!rdev->constraints->system_critical)
@@ -5103,17 +5105,21 @@ static void regulator_handle_critical(struct regulator_dev *rdev,
 	switch (event) {
 	case REGULATOR_EVENT_UNDER_VOLTAGE:
 		reason = "System critical regulator: voltage drop detected";
+		pscr = PSCR_UNDER_VOLTAGE;
 		break;
 	case REGULATOR_EVENT_OVER_CURRENT:
 		reason = "System critical regulator: over-current detected";
+		pscr = PSCR_OVER_CURRENT;
 		break;
 	case REGULATOR_EVENT_FAIL:
 		reason = "System critical regulator: unknown error";
+		pscr = PSCR_REGULATOR_FAILURE;
 	}
 
 	if (!reason)
 		return;
 
+	set_power_state_change_reason(pscr);
 	hw_protection_shutdown(reason,
 			       rdev->constraints->uv_less_critical_window_ms);
 }
-- 
2.39.2


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

* [PATCH v2 8/8] thermal: core: Record PSCR before hw_protection_shutdown()
  2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (6 preceding siblings ...)
  2024-01-24 12:22 ` [PATCH v2 7/8] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
@ 2024-01-24 12:22 ` Oleksij Rempel
  7 siblings, 0 replies; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-24 12:22 UTC (permalink / raw)
  To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla
  Cc: Oleksij Rempel, kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

Enhance the thermal core to record the Power State Change Reason (PSCR)
prior to invoking hw_protection_shutdown(). This change integrates the
PSCR framework with the thermal subsystem, ensuring that reasons for
power state changes, such as overtemperature events, are stored in a
dedicated non-volatile memory (NVMEM) cell.

This 'black box' recording is crucial for post-mortem analysis, enabling
a deeper understanding of system failures and abrupt shutdowns,
especially in scenarios where PMICs or watchdog timers are incapable of
logging such events.  The recorded data can be utilized during system
recovery routines in the bootloader or early kernel stages of subsequent
boots, significantly enhancing system diagnostics, reliability, and
debugging capabilities.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/thermal/thermal_core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index dfaa6341694a..0511d82351c5 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -16,6 +16,7 @@
 #include <linux/kdev_t.h>
 #include <linux/idr.h>
 #include <linux/thermal.h>
+#include <linux/pscrr.h>
 #include <linux/reboot.h>
 #include <linux/string.h>
 #include <linux/of.h>
@@ -329,6 +330,8 @@ static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdo
 
 	dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
 
+	set_power_state_change_reason(PSCR_OVERTEMPERATURE);
+
 	if (shutdown)
 		hw_protection_shutdown(msg, poweroff_delay_ms);
 	else
-- 
2.39.2


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

* Re: [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers
  2024-01-24 12:21 ` [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers Oleksij Rempel
@ 2024-01-25 10:54   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 17+ messages in thread
From: Krzysztof Kozlowski @ 2024-01-25 10:54 UTC (permalink / raw)
  To: Oleksij Rempel, Sebastian Reichel, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Srinivas Kandagatla
  Cc: kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

On 24/01/2024 13:21, Oleksij Rempel wrote:
> Add binding for Power State Change Reason Recording (PSCRR) subsystem
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  .../bindings/power/reset/pscrr.yaml           | 44 +++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr.yaml
> 
> diff --git a/Documentation/devicetree/bindings/power/reset/pscrr.yaml b/Documentation/devicetree/bindings/power/reset/pscrr.yaml
> new file mode 100644
> index 000000000000..c8738b4930fe
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/reset/pscrr.yaml
> @@ -0,0 +1,44 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/power/reset/pscrr.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Power State Change Reason (PSCR)

Missing final R?

> +
> +maintainers:
> +  - Oleksij Rempel <o.rempel@pengutronix.de>
> +
> +description: Binding for devices responsible to store reasons for power state

Line break after description:

> +  changes such as reboot and power-off. Reasons like unknown, under voltage,
> +  and over temperature are captured for diagnostic or automatic recovery
> +  purposes.
> +
> +properties:
> +  pscr-under-voltage:
> +    $ref: /schemas/types.yaml#/definitions/uint32
> +    description: |
> +      Value to indicate an under-voltage condition of a system critical
> +      regulator as the reason for the power state change.

I don't understand how it is supposed to work... unless you wrote
binding for drivers. For drivers it would make sense, but that's another
problem: binding is not for drivers.

You also did not present here DTS with any actual device doing this. You
just added a driver...

Best regards,
Krzysztof


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

* Re: [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-24 12:22 ` [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
@ 2024-01-25 10:57   ` Krzysztof Kozlowski
  2024-01-25 17:11     ` Oleksij Rempel
  0 siblings, 1 reply; 17+ messages in thread
From: Krzysztof Kozlowski @ 2024-01-25 10:57 UTC (permalink / raw)
  To: Oleksij Rempel, Sebastian Reichel, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Srinivas Kandagatla
  Cc: kernel, linux-kernel, Liam Girdwood, Mark Brown,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

On 24/01/2024 13:22, Oleksij Rempel wrote:
> Add device tree bindings that describe hardware implementations of
> Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
> (PSCR).

A nit, subject: drop second/last, redundant "bindings for". The
"dt-bindings" prefix is already stating that these are bindings.
See also:
https://elixir.bootlin.com/linux/v6.7-rc8/source/Documentation/devicetree/bindings/submitting-patches.rst#L18

> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  .../bindings/power/reset/pscrr-nvmem.yaml     | 53 +++++++++++++++++++
>  1 file changed, 53 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
> 
> diff --git a/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml b/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
> new file mode 100644
> index 000000000000..779920dea283
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/reset/pscrr-nvmem.yaml
> @@ -0,0 +1,53 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/power/reset/pscrr-nvmem.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Generic NVMEM Power State Change Reason Recorder
> +
> +maintainers:
> +  - Oleksij Rempel <o.rempel@pengutronix.de>
> +
> +description: This binding describes the Non-Volatile Memory (NVMEM) hardware

Same comment and also: describe the hardware, not the binding. s/This
binding describes/something useful/

> +  that stores Power State Change Reasons (PSCR).
> +
> +allOf:
> +  - $ref: pscrr.yaml#
> +
> +properties:
> +  compatible:
> +    const: pscrr-nvmem
> +

So that's a driver :/. Maybe Rob will like it, but it's a no from me.
Please come up with something really suiting DEVICES, not DRIVERS.

> +  nvmem-cells:
> +    description: |

Do not need '|' unless you need to preserve formatting.

> +      A phandle pointing to the nvmem-cells node where the power state change
> +      reasons are stored.
> +    maxItems: 1
> +


Best regards,
Krzysztof


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

* Re: [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-25 10:57   ` Krzysztof Kozlowski
@ 2024-01-25 17:11     ` Oleksij Rempel
  2024-01-26  9:03       ` Krzysztof Kozlowski
  0 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-25 17:11 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla, kernel, linux-kernel,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Daniel Lezcano,
	Zhang Rui, Lukasz Luba, linux-pm, devicetree,
	Søren Andersen

On Thu, Jan 25, 2024 at 11:57:18AM +0100, Krzysztof Kozlowski wrote:
> On 24/01/2024 13:22, Oleksij Rempel wrote:
> > Add device tree bindings that describe hardware implementations of
> > Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
> > (PSCR).
> > +  that stores Power State Change Reasons (PSCR).
> > +
> > +allOf:
> > +  - $ref: pscrr.yaml#
> > +
> > +properties:
> > +  compatible:
> > +    const: pscrr-nvmem
> > +
> 
> So that's a driver :/. Maybe Rob will like it, but it's a no from me.
> Please come up with something really suiting DEVICES, not DRIVERS.

If I understand your distinction between 'DEVICES' and 'DRIVERS'
correctly, 'DEVICES' in the device tree context are meant to represent
physical hardware components, while 'DRIVERS' refer to software
abstractions of these components. However, there are numerous device
tree instances, like software-based implementations for SPI, I2C, or
GPIO, which could also be interpreted as 'DRIVERS' in the context of
your email. Similarly, the binding for PSCRR represents functionality not
fully implemented in hardware but supported by the hardware component of
NVMEM, akin to how ramoops or other functionalities are represented.

If I'm misunderstanding your distinction between 'DEVICES' and
'DRIVERS', please clarify with an example of how a proper binding should
be implemented for a case like this.

Regards,
Oleksij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

* Re: [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-25 17:11     ` Oleksij Rempel
@ 2024-01-26  9:03       ` Krzysztof Kozlowski
  2024-01-26 16:51         ` Oleksij Rempel
  0 siblings, 1 reply; 17+ messages in thread
From: Krzysztof Kozlowski @ 2024-01-26  9:03 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Srinivas Kandagatla, kernel, linux-kernel,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Daniel Lezcano,
	Zhang Rui, Lukasz Luba, linux-pm, devicetree,
	Søren Andersen

On 25/01/2024 18:11, Oleksij Rempel wrote:
> On Thu, Jan 25, 2024 at 11:57:18AM +0100, Krzysztof Kozlowski wrote:
>> On 24/01/2024 13:22, Oleksij Rempel wrote:
>>> Add device tree bindings that describe hardware implementations of
>>> Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
>>> (PSCR).
>>> +  that stores Power State Change Reasons (PSCR).
>>> +
>>> +allOf:
>>> +  - $ref: pscrr.yaml#
>>> +
>>> +properties:
>>> +  compatible:
>>> +    const: pscrr-nvmem
>>> +
>>
>> So that's a driver :/. Maybe Rob will like it, but it's a no from me.
>> Please come up with something really suiting DEVICES, not DRIVERS.
> 
> If I understand your distinction between 'DEVICES' and 'DRIVERS'
> correctly, 'DEVICES' in the device tree context are meant to represent
> physical hardware components, while 'DRIVERS' refer to software

Yes.

> abstractions of these components. However, there are numerous device
> tree instances, like software-based implementations for SPI, I2C, or
> GPIO, which could also be interpreted as 'DRIVERS' in the context of

True. Yet they are still for physical interfaces. There is no DTS having
some virtual I2C for a board which does not have I2C.

> your email. Similarly, the binding for PSCRR represents functionality not
> fully implemented in hardware but supported by the hardware component of
> NVMEM, akin to how ramoops or other functionalities are represented.

You don't need a binding for your case. Instantiate it whatever you wish
- modprobe for example - and configure through approved kernel
interfaces - sysfs for example.

Best regards,
Krzysztof


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

* Re: [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-26  9:03       ` Krzysztof Kozlowski
@ 2024-01-26 16:51         ` Oleksij Rempel
  2024-01-29  7:43           ` Krzysztof Kozlowski
  0 siblings, 1 reply; 17+ messages in thread
From: Oleksij Rempel @ 2024-01-26 16:51 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: devicetree, Conor Dooley, Daniel Lezcano, Krzysztof Kozlowski,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Sebastian Reichel,
	linux-kernel, Søren Andersen, Rob Herring,
	Srinivas Kandagatla, kernel, linux-pm, Zhang Rui, Lukasz Luba

On Fri, Jan 26, 2024 at 10:03:51AM +0100, Krzysztof Kozlowski wrote:
> On 25/01/2024 18:11, Oleksij Rempel wrote:
> > On Thu, Jan 25, 2024 at 11:57:18AM +0100, Krzysztof Kozlowski wrote:
> >> On 24/01/2024 13:22, Oleksij Rempel wrote:
> >>> Add device tree bindings that describe hardware implementations of
> >>> Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
> >>> (PSCR).
> >>> +  that stores Power State Change Reasons (PSCR).
> >>> +
> >>> +allOf:
> >>> +  - $ref: pscrr.yaml#
> >>> +
> >>> +properties:
> >>> +  compatible:
> >>> +    const: pscrr-nvmem
> >>> +
> >>
> >> So that's a driver :/. Maybe Rob will like it, but it's a no from me.
> >> Please come up with something really suiting DEVICES, not DRIVERS.
> > 
> > If I understand your distinction between 'DEVICES' and 'DRIVERS'
> > correctly, 'DEVICES' in the device tree context are meant to represent
> > physical hardware components, while 'DRIVERS' refer to software
> 
> Yes.
> 
> > abstractions of these components. However, there are numerous device
> > tree instances, like software-based implementations for SPI, I2C, or
> > GPIO, which could also be interpreted as 'DRIVERS' in the context of
> 
> True. Yet they are still for physical interfaces. There is no DTS having
> some virtual I2C for a board which does not have I2C.
> 
> > your email. Similarly, the binding for PSCRR represents functionality not
> > fully implemented in hardware but supported by the hardware component of
> > NVMEM, akin to how ramoops or other functionalities are represented.
> 
> You don't need a binding for your case. Instantiate it whatever you wish
> - modprobe for example - and configure through approved kernel
> interfaces - sysfs for example.

About using sysfs for the NVMEM cell, it won't work for my needs because
I have to know about reboot events before the filesystem is ready. So,
I'm thinking of using a boot parameter for the kernel. It would look
like this: pscrr-nvmem.nvmem_cell_alias=nvmem-cell0. This way, I can set
up the NVMEM cell right at the system's start. I'll need to use stable
NVMEM cell names for this. Is it ok to introduce NVMEM cell aliases in
the devicetree?

Best Regards,
Oleksij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

* Re: [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons
  2024-01-24 12:22 ` [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons Oleksij Rempel
@ 2024-01-28  6:38   ` kernel test robot
  0 siblings, 0 replies; 17+ messages in thread
From: kernel test robot @ 2024-01-28  6:38 UTC (permalink / raw)
  To: Oleksij Rempel, Sebastian Reichel, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Srinivas Kandagatla
  Cc: llvm, oe-kbuild-all, Oleksij Rempel, kernel, linux-kernel,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Daniel Lezcano,
	Zhang Rui, Lukasz Luba, linux-pm, devicetree,
	Søren Andersen

Hi Oleksij,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on broonie-regulator/for-next rafael-pm/thermal linus/master v6.8-rc1 next-20240125]
[cannot apply to sre-power-supply/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oleksij-Rempel/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20240124-202833
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20240124122204.730370-7-o.rempel%40pengutronix.de
patch subject: [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20240128/202401281442.zKlRfljS-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240128/202401281442.zKlRfljS-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202401281442.zKlRfljS-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/power/reset/pscrr-nvmem.c:53:2: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
      53 |         kfree(buf);
         |         ^
   1 error generated.


vim +/kfree +53 drivers/power/reset/pscrr-nvmem.c

    39	
    40	static int pscrr_nvmem_read(struct pscrr_device *pscrr_dev, u32 *magic)
    41	{
    42		struct pscrr_nvmem *priv = container_of(pscrr_dev, struct pscrr_nvmem,
    43							pscrr_dev);
    44		size_t len;
    45		void *buf;
    46	
    47		buf = nvmem_cell_read(priv->cell, &len);
    48		if (IS_ERR(buf))
    49			return PTR_ERR(buf);
    50	
    51		*magic = 0;
    52		memcpy(magic, buf, min(len, sizeof(*magic)));
  > 53		kfree(buf);
    54	
    55		return 0;
    56	}
    57	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
  2024-01-24 12:21 ` [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage Oleksij Rempel
@ 2024-01-28  9:05   ` kernel test robot
  0 siblings, 0 replies; 17+ messages in thread
From: kernel test robot @ 2024-01-28  9:05 UTC (permalink / raw)
  To: Oleksij Rempel, Sebastian Reichel, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Srinivas Kandagatla
  Cc: llvm, oe-kbuild-all, Oleksij Rempel, kernel, linux-kernel,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Daniel Lezcano,
	Zhang Rui, Lukasz Luba, linux-pm, devicetree,
	Søren Andersen

Hi Oleksij,

kernel test robot noticed the following build warnings:

[auto build test WARNING on robh/for-next]
[also build test WARNING on broonie-regulator/for-next rafael-pm/thermal linus/master v6.8-rc1 next-20240125]
[cannot apply to sre-power-supply/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oleksij-Rempel/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20240124-202833
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20240124122204.730370-4-o.rempel%40pengutronix.de
patch subject: [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20240128/202401281628.Z3E5872D-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240128/202401281628.Z3E5872D-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202401281628.Z3E5872D-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/power/reset/pscrr.c:132:2: warning: label at end of compound statement is a C2x extension [-Wc2x-extensions]
     132 |         }
         |         ^
   1 warning generated.


vim +132 drivers/power/reset/pscrr.c

   119	
   120	static const char *pscr_to_por_string(enum pscr pscr)
   121	{
   122		switch (pscr) {
   123		case PSCR_UNDER_VOLTAGE:
   124			return POWER_ON_REASON_BROWN_OUT;
   125		case PSCR_OVER_CURRENT:
   126			return POWER_ON_REASON_OVER_CURRENT;
   127		case PSCR_REGULATOR_FAILURE:
   128			return POWER_ON_REASON_REGULATOR_FAILURE;
   129		case PSCR_OVERTEMPERATURE:
   130			return POWER_ON_REASON_OVERTEMPERATURE;
   131		default:
 > 132		}
   133	
   134		return POWER_ON_REASON_UNKNOWN;
   135	}
   136	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-26 16:51         ` Oleksij Rempel
@ 2024-01-29  7:43           ` Krzysztof Kozlowski
  0 siblings, 0 replies; 17+ messages in thread
From: Krzysztof Kozlowski @ 2024-01-29  7:43 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: devicetree, Conor Dooley, Daniel Lezcano, Krzysztof Kozlowski,
	Liam Girdwood, Mark Brown, Rafael J. Wysocki, Sebastian Reichel,
	linux-kernel, Søren Andersen, Rob Herring,
	Srinivas Kandagatla, kernel, linux-pm, Zhang Rui, Lukasz Luba

On 26/01/2024 17:51, Oleksij Rempel wrote:
> On Fri, Jan 26, 2024 at 10:03:51AM +0100, Krzysztof Kozlowski wrote:
>> On 25/01/2024 18:11, Oleksij Rempel wrote:
>>> On Thu, Jan 25, 2024 at 11:57:18AM +0100, Krzysztof Kozlowski wrote:
>>>> On 24/01/2024 13:22, Oleksij Rempel wrote:
>>>>> Add device tree bindings that describe hardware implementations of
>>>>> Non-Volatile Memory (NVMEM) used for storing Power State Change Reasons
>>>>> (PSCR).
>>>>> +  that stores Power State Change Reasons (PSCR).
>>>>> +
>>>>> +allOf:
>>>>> +  - $ref: pscrr.yaml#
>>>>> +
>>>>> +properties:
>>>>> +  compatible:
>>>>> +    const: pscrr-nvmem
>>>>> +
>>>>
>>>> So that's a driver :/. Maybe Rob will like it, but it's a no from me.
>>>> Please come up with something really suiting DEVICES, not DRIVERS.
>>>
>>> If I understand your distinction between 'DEVICES' and 'DRIVERS'
>>> correctly, 'DEVICES' in the device tree context are meant to represent
>>> physical hardware components, while 'DRIVERS' refer to software
>>
>> Yes.
>>
>>> abstractions of these components. However, there are numerous device
>>> tree instances, like software-based implementations for SPI, I2C, or
>>> GPIO, which could also be interpreted as 'DRIVERS' in the context of
>>
>> True. Yet they are still for physical interfaces. There is no DTS having
>> some virtual I2C for a board which does not have I2C.
>>
>>> your email. Similarly, the binding for PSCRR represents functionality not
>>> fully implemented in hardware but supported by the hardware component of
>>> NVMEM, akin to how ramoops or other functionalities are represented.
>>
>> You don't need a binding for your case. Instantiate it whatever you wish
>> - modprobe for example - and configure through approved kernel
>> interfaces - sysfs for example.
> 
> About using sysfs for the NVMEM cell, it won't work for my needs because
> I have to know about reboot events before the filesystem is ready. So,

initrd can configure it before mounting filesystem.

> I'm thinking of using a boot parameter for the kernel. It would look
> like this: pscrr-nvmem.nvmem_cell_alias=nvmem-cell0. This way, I can set
> up the NVMEM cell right at the system's start. I'll need to use stable
> NVMEM cell names for this. Is it ok to introduce NVMEM cell aliases in
> the devicetree?

In my opinion no, because the point of Devicetree is not to solve your
system init problems. You add pure software node which should not be in
your DTS for many reasons: it is not a hardware description and it is a
 software policy enforced on all users of DTS without actually
consulting them. Also, this solution ignores ACPI systems. Developing a
proper interface would work on ACPI as well.

Best regards,
Krzysztof


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

end of thread, other threads:[~2024-01-29  7:43 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-24 12:21 [RFC PATCH v1 0/8] Introduction of PSCR Framework and Related Components Oleksij Rempel
2024-01-24 12:21 ` [PATCH v2 1/8] power: Extend power_on_reason.h for upcoming PSCRR framework Oleksij Rempel
2024-01-24 12:21 ` [PATCH v2 2/8] dt-bindings: power: reset: add generic PSCRR binding trackers Oleksij Rempel
2024-01-25 10:54   ` Krzysztof Kozlowski
2024-01-24 12:21 ` [PATCH v2 3/8] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage Oleksij Rempel
2024-01-28  9:05   ` kernel test robot
2024-01-24 12:22 ` [PATCH v2 4/8] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
2024-01-25 10:57   ` Krzysztof Kozlowski
2024-01-25 17:11     ` Oleksij Rempel
2024-01-26  9:03       ` Krzysztof Kozlowski
2024-01-26 16:51         ` Oleksij Rempel
2024-01-29  7:43           ` Krzysztof Kozlowski
2024-01-24 12:22 ` [PATCH v2 5/8] nvmem: provide consumer access to cell size metrics Oleksij Rempel
2024-01-24 12:22 ` [PATCH v2 6/8] power: reset: add PSCR NVMEM Driver for Recording Power State Change Reasons Oleksij Rempel
2024-01-28  6:38   ` kernel test robot
2024-01-24 12:22 ` [PATCH v2 7/8] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
2024-01-24 12:22 ` [PATCH v2 8/8] thermal: core: Record PSCR " Oleksij Rempel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).