Linux-Devicetree Archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components
@ 2024-01-19 13:25 Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
                   ` (7 more replies)
  0 siblings, 8 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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

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 (7):
  dt-bindings: power: reset: add generic PSCR binding trackers
  power: reset: Introduce PSCR Tracking 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 Storing Power State Change
    Reasons
  regulator: set Power State Change Reason before
    hw_protection_shutdown()
  thermal: core: set Power State Change Reason before
    hw_protection_shutdown()

 .../bindings/power/reset/pscr-nvmem.yaml      |  54 ++++
 .../devicetree/bindings/power/reset/pscr.yaml |  51 ++++
 drivers/nvmem/core.c                          |  25 ++
 drivers/power/reset/Kconfig                   |  30 ++
 drivers/power/reset/Makefile                  |   2 +
 drivers/power/reset/pscr-nvmem.c              | 100 +++++++
 drivers/power/reset/pscr.c                    | 259 ++++++++++++++++++
 drivers/regulator/core.c                      |   6 +
 drivers/thermal/thermal_core.c                |   2 +
 include/linux/nvmem-consumer.h                |   7 +
 include/linux/pscr.h                          |  40 +++
 11 files changed, 576 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscr.yaml
 create mode 100644 drivers/power/reset/pscr-nvmem.c
 create mode 100644 drivers/power/reset/pscr.c
 create mode 100644 include/linux/pscr.h

-- 
2.39.2


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

* [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 14:50   ` Rob Herring
  2024-01-19 17:28   ` Rob Herring
  2024-01-19 13:25 ` [RFC PATCH v1 2/7] power: reset: Introduce PSCR Tracking Framework for Non-Volatile Storage Oleksij Rempel
                   ` (6 subsequent siblings)
  7 siblings, 2 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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 (PSCR) subsystem

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

diff --git a/Documentation/devicetree/bindings/power/reset/pscr.yaml b/Documentation/devicetree/bindings/power/reset/pscr.yaml
new file mode 100644
index 000000000000..1ce973f3473c
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/reset/pscr.yaml
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/state-change/pscr.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 responsable 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:
+  $nodename:
+    pattern: "^pscr(@.*|-([0-9]|[1-9][0-9]+))?$"
+
+  pscr-unknown:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: Value to indicate an unknown reason for the power state change.
+
+  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 unknow, 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] 18+ messages in thread

* [RFC PATCH v1 2/7] power: reset: Introduce PSCR Tracking Framework for Non-Volatile Storage
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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 (PSCR) tracking
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/pscr.c   | 259 +++++++++++++++++++++++++++++++++++
 include/linux/pscr.h         |  40 ++++++
 4 files changed, 319 insertions(+)
 create mode 100644 drivers/power/reset/pscr.c
 create mode 100644 include/linux/pscr.h

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index fece990af4a7..da76e84302b9 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 PSCR
+    bool "Power State Change Reasons (PSCR) Tracking Framework"
+    help
+      Enables the Power State Change Reasons (PSCR) tracking 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..d9d744302c68 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_PSCR) += pscr.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/pscr.c b/drivers/power/reset/pscr.c
new file mode 100644
index 000000000000..7506ecbe1aad
--- /dev/null
+++ b/drivers/power/reset/pscr.c
@@ -0,0 +1,259 @@
+// 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/reboot.h>
+#include <linux/pscr.h>
+
+#define PREFIX "pscr-"
+
+struct reason_info {
+	enum power_state_change_reason pscr;
+	u32 magic;
+	struct list_head list;
+};
+
+enum power_state_change_reason system_pscr;
+
+struct pscr_map {
+	const char *reason;
+	enum power_state_change_reason pscr;
+};
+
+struct pscr_map pscr_map_table[] = {
+	{ "unknown", PSCR_UNKNOWN },
+	{ "under-voltage", PSCR_UNDER_VOLTAGE },
+	{ "over-current", PSCR_OVER_CURRENT },
+	{ "regulator-failure", PSCR_REGULATOR_FAILURE },
+	{ "over-temperature", PSCR_OVERTEMPERATURE },
+};
+
+static int find_reason_by_string(const char *reason)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(pscr_map_table); i++) {
+		if (!strcmp(reason, pscr_map_table[i].reason))
+			return pscr_map_table[i].pscr;
+	}
+
+	return -ENOENT;
+}
+
+/**
+ * set_power_state_change_reason() - Set the system's power state change reason
+ * @reason: 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 power_state_change_reason reason)
+{
+	system_pscr = reason;
+}
+EXPORT_SYMBOL_GPL(set_power_state_change_reason);
+
+static unsigned int get_pscr_magic(struct pscr_driver *pscr_drv,
+					  const char *cmd)
+{
+	struct reason_info *info;
+
+	list_for_each_entry(info, &pscr_drv->head, list) {
+		if (info->pscr == system_pscr)
+			return info->magic;
+	}
+
+	return 0;
+}
+
+static int pscr_notify(struct notifier_block *this,
+			      unsigned long reason, void *cmd)
+{
+	struct pscr_driver *pscr_drv = container_of(this, struct pscr_driver,
+						    reboot_notifier);
+	unsigned int magic;
+
+	magic = get_pscr_magic(pscr_drv, cmd);
+	if (magic)
+		pscr_drv->write(pscr_drv, magic);
+
+	return NOTIFY_DONE;
+}
+
+/**
+ * pscr_process_property() - Process a power state change reason property
+ * @pscr_drv: Pointer to the pscr_driver 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 pscr_process_property(struct pscr_driver *pscr_drv,
+				 struct property *prop)
+{
+	struct device *dev = pscr_drv->dev;
+	size_t len = strlen(PREFIX);
+	struct reason_info *info;
+	int ret;
+
+	if (strncmp(prop->name, PREFIX, len))
+		return 0;
+
+	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	ret = of_property_read_u32(dev->of_node, prop->name, &info->magic);
+	if (ret) {
+		dev_err(dev, "Can't read magic number for %s: %pe\n",
+			prop->name, ERR_PTR(ret));
+		devm_kfree(dev, info);
+		return 0;
+	}
+
+	if (!info->magic) {
+		dev_err(dev, "%s with magic number == 0\n", prop->name);
+		devm_kfree(dev, info);
+		return 0;
+	}
+
+	info->pscr = find_reason_by_string(prop->name + len);
+	if (info->pscr < 0) {
+		dev_err(dev, "unsupported reason name(%s): %pe\n",
+				prop->name, ERR_PTR(info->pscr));
+		devm_kfree(dev, info);
+		return 0;
+	}
+
+	if (info->magic > pscr_drv->max_magic)
+		pscr_drv->max_magic = info->magic;
+
+	dev_dbg(dev, "registering reason = %s, magic = %d, pscr = %d\n",
+		prop->name, info->magic, info->pscr);
+	list_add_tail(&info->list, &pscr_drv->head);
+
+	return 0;
+}
+
+/*
+ * pscr_register() - Register the pscr driver and initialize power state change
+ *                   reasons
+ * @pscr_drv: Pointer to the pscr_driver 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 pscr_register(struct pscr_driver *pscr_drv)
+{
+	struct device_node *np = pscr_drv->dev->of_node;
+	struct property *prop;
+
+	INIT_LIST_HEAD(&pscr_drv->head);
+
+	for_each_property_of_node(np, prop) {
+		int ret = pscr_process_property(pscr_drv, prop);
+		if (ret)
+			return ret;
+	}
+
+	pscr_drv->reboot_notifier.notifier_call = pscr_notify;
+	register_reboot_notifier(&pscr_drv->reboot_notifier);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pscr_register);
+
+/*
+ * pscr_unregister() - Unregister the pscr driver's reboot notifier
+ * @pscr_drv: Pointer to the pscr_driver structure
+ *
+ * This function unregisters the reboot notifier for the pscr driver.
+ */
+void pscr_unregister(struct pscr_driver *pscr_drv)
+{
+	unregister_reboot_notifier(&pscr_drv->reboot_notifier);
+}
+EXPORT_SYMBOL_GPL(pscr_unregister);
+
+static void devm_pscr_release(struct device *dev, void *res)
+{
+	pscr_unregister(*(struct pscr_driver **)res);
+}
+
+/**
+ * devm_pscr_register - Register a device-managed PSCR driver
+ * @dev: Device to associate the PSCR driver with
+ * @pscr_drv: 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_pscr_register(struct device *dev,
+			      struct pscr_driver *pscr_drv)
+{
+	struct pscr_driver **dr;
+	int rc;
+
+	dr = devres_alloc(devm_pscr_release, sizeof(*dr), GFP_KERNEL);
+	if (!dr)
+		return -ENOMEM;
+
+	rc = pscr_register(pscr_drv);
+	if (rc) {
+		devres_free(dr);
+		return rc;
+	}
+
+	*dr = pscr_drv;
+	devres_add(dev, dr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_pscr_register);
+
+static int devm_pscr_match(struct device *dev, void *res, void *data)
+{
+	struct pscr_driver **p = res;
+
+	if (WARN_ON(!p || !*p))
+		return 0;
+
+	return *p == data;
+}
+
+/**
+ * devm_pscr_unregister - Unregister a managed PSCR driver
+ * @dev: Device associated with the PSCR driver
+ * @pscr_drv: 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_pscr_unregister(struct device *dev,
+				 struct pscr_driver *pscr_drv)
+{
+	WARN_ON(devres_release(dev,
+			       devm_pscr_release,
+			       devm_pscr_match, pscr_drv));
+}
+EXPORT_SYMBOL_GPL(devm_pscr_unregister);
+
+MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
+MODULE_DESCRIPTION("Power State Change Reason (PSCR) tracking framework");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/pscr.h b/include/linux/pscr.h
new file mode 100644
index 000000000000..bf66dd69d96d
--- /dev/null
+++ b/include/linux/pscr.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PSCR_H__
+#define __PSCR_H__
+
+enum power_state_change_reason {
+	PSCR_UNKNOWN,
+	PSCR_UNDER_VOLTAGE,
+	PSCR_OVER_CURRENT,
+	PSCR_REGULATOR_FAILURE,
+	PSCR_OVERTEMPERATURE,
+};
+
+struct pscr_driver {
+	struct device *dev;
+	struct list_head head;
+	int (*write)(struct pscr_driver *pscr_drv, u32 magic);
+	struct notifier_block reboot_notifier;
+	u32 max_magic;
+};
+
+int pscr_register(struct pscr_driver *pscr_drv);
+void pscr_unregister(struct pscr_driver *pscr_drv);
+int devm_pscr_register(struct device *dev,
+		       struct pscr_driver *pscr_drv);
+void devm_pscr_unregister(struct device *dev,
+			  struct pscr_driver *pscr_drv);
+
+
+#if IS_ENABLED(CONFIG_PSCR)
+
+void set_power_state_change_reason(enum power_state_change_reason reason);
+
+#else
+
+static inline void set_power_state_change_reason(enum power_state_change_reason reason)
+{
+}
+#endif
+
+#endif
-- 
2.39.2


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

* [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 2/7] power: reset: Introduce PSCR Tracking Framework for Non-Volatile Storage Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 14:50   ` Rob Herring
  2024-01-19 13:25 ` [RFC PATCH v1 4/7] nvmem: provide consumer access to cell size metrics Oleksij Rempel
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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/pscr-nvmem.yaml      | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml

diff --git a/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml b/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml
new file mode 100644
index 000000000000..0642b470af41
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/state-change/pscr-nvmem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Generic NVMEM Power State Change Reason storage
+
+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: pse-controller.yaml#
+
+properties:
+  compatible:
+    const: pscr-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-unknown:
+  pscr-under-voltage:
+  pscr-over-current:
+  pscr-over-temperature:
+
+required:
+  - compatible
+  - nvmem-cells
+  - nvmem-cell-names
+
+additionalProperties: false
+
+examples:
+  - |
+    power-state-change-reason {
+      compatible = "pscr-nvmem";
+      nvmem-cells = <&pscr_cell>;
+      nvmem-cell-names = "pscr";
+      pscr-unknown = <1>;
+      pscr-under-voltage = <2>;
+      pscr-over-temperature = <3>;
+    };
+...
-- 
2.39.2


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

* [RFC PATCH v1 4/7] nvmem: provide consumer access to cell size metrics
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (2 preceding siblings ...)
  2024-01-19 13:25 ` [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 5/7] power: reset: add PSCR NVMEM Driver for Storing Power State Change Reasons Oleksij Rempel
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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 bf42b7e826db..6c5b785b804b 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1753,6 +1753,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 6ec4b9743e25..a174b05514b4 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);
@@ -127,6 +128,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] 18+ messages in thread

* [RFC PATCH v1 5/7] power: reset: add PSCR NVMEM Driver for Storing Power State Change Reasons
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (3 preceding siblings ...)
  2024-01-19 13:25 ` [RFC PATCH v1 4/7] nvmem: provide consumer access to cell size metrics Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 13:25 ` [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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 (PSCR) 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/pscr-nvmem.c | 100 +++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 drivers/power/reset/pscr-nvmem.c

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index da76e84302b9..a110dff599af 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -324,3 +324,14 @@ menuconfig PSCR
       timeouts.
 
       If unsure, say N.
+
+if PSCR
+
+config PSCR_NVMEM
+	tristate "Generic NVMEM-based Power State Change Reason Tracking"
+	depends on OF
+	help
+	  Enabling this option adds support for storing power state change
+	  reasons in a NVMEM cell.
+
+endif
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index d9d744302c68..e7985d81ac8a 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_PSCR) += pscr.o
+obj-$(CONFIG_PSCR_NVMEM) += pscr-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/pscr-nvmem.c b/drivers/power/reset/pscr-nvmem.c
new file mode 100644
index 000000000000..74048341b7d7
--- /dev/null
+++ b/drivers/power/reset/pscr-nvmem.c
@@ -0,0 +1,100 @@
+// 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/pscr.h>
+
+struct pscr_nvmem {
+	struct pscr_driver pscr_drv;
+	struct nvmem_cell *cell;
+	size_t max_magic_bytes;
+};
+
+static int pscr_nvmem_write(struct pscr_driver *pscr_drv, u32 magic)
+{
+	struct pscr_nvmem *priv = container_of(pscr_drv, struct pscr_nvmem,
+					       pscr_drv);
+	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(pscr_drv->dev, "update reason bits failed: %pe\n",
+			ERR_PTR(ret));
+
+	return ret;
+}
+
+static int pscr_nvmem_probe(struct platform_device *pdev)
+{
+	const char *pscr = "pscr";
+	struct pscr_nvmem *priv;
+	size_t bytes, bits, magic_bits;
+	int ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->pscr_drv.dev = &pdev->dev;
+	priv->pscr_drv.write = pscr_nvmem_write;
+
+	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_pscr_register(&pdev->dev, &priv->pscr_drv);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "failed to register pscr driver\n");
+
+	magic_bits = fls(priv->pscr_drv.max_magic);
+	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 ret;
+}
+
+static const struct of_device_id pscr_nvmem_of_match[] = {
+	{ .compatible = "pscr-nvmem" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, pscr_nvmem_of_match);
+
+static struct platform_driver pscr_nvmem_driver = {
+	.probe = pscr_nvmem_probe,
+	.driver = {
+		.name = "pscr-nvmem",
+		.of_match_table = pscr_nvmem_of_match,
+	},
+};
+module_platform_driver(pscr_nvmem_driver);
+
+MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
+MODULE_DESCRIPTION("NVMEM Driver for Power State Change Reason Tracking");
+MODULE_LICENSE("GPL v2");
-- 
2.39.2


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

* [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (4 preceding siblings ...)
  2024-01-19 13:25 ` [RFC PATCH v1 5/7] power: reset: add PSCR NVMEM Driver for Storing Power State Change Reasons Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 14:07   ` Mark Brown
  2024-01-19 13:25 ` [RFC PATCH v1 7/7] thermal: core: " Oleksij Rempel
  2024-01-19 23:19 ` [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Sebastian Reichel
  7 siblings, 1 reply; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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

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

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

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index a6cb84af989e..d8a45c89f14a 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/pscr.h>
 #include <linux/reboot.h>
 #include <linux/regmap.h>
 #include <linux/regulator/of_regulator.h>
@@ -5074,6 +5075,7 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);
 static void regulator_handle_critical(struct regulator_dev *rdev,
 				      unsigned long event)
 {
+	enum power_state_change_reason pscr;
 	const char *reason = NULL;
 
 	if (!rdev->constraints->system_critical)
@@ -5082,17 +5084,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] 18+ messages in thread

* [RFC PATCH v1 7/7] thermal: core: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (5 preceding siblings ...)
  2024-01-19 13:25 ` [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
@ 2024-01-19 13:25 ` Oleksij Rempel
  2024-01-19 18:34   ` Rafael J. Wysocki
  2024-01-19 23:19 ` [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Sebastian Reichel
  7 siblings, 1 reply; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 13:25 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

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

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

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 9c17d35ccbbd..5ee3a59d7a0e 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/pscr.h>
 #include <linux/reboot.h>
 #include <linux/string.h>
 #include <linux/of.h>
@@ -325,6 +326,7 @@ void thermal_zone_device_critical(struct thermal_zone_device *tz)
 	dev_emerg(&tz->device, "%s: critical temperature reached, "
 		  "shutting down\n", tz->type);
 
+	set_power_state_change_reason(PSCR_OVERTEMPERATURE);
 	hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
 }
 EXPORT_SYMBOL(thermal_zone_device_critical);
-- 
2.39.2


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

* Re: [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 13:25 ` [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
@ 2024-01-19 14:07   ` Mark Brown
  0 siblings, 0 replies; 18+ messages in thread
From: Mark Brown @ 2024-01-19 14:07 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Srinivas Kandagatla, kernel, linux-kernel, Liam Girdwood,
	Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	linux-pm, devicetree, Søren Andersen

[-- Attachment #1: Type: text/plain, Size: 189 bytes --]

On Fri, Jan 19, 2024 at 02:25:20PM +0100, Oleksij Rempel wrote:

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

Reviewed-by: Mark Brown <broonie@kernel.org>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers
  2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
@ 2024-01-19 14:50   ` Rob Herring
  2024-01-19 17:28   ` Rob Herring
  1 sibling, 0 replies; 18+ messages in thread
From: Rob Herring @ 2024-01-19 14:50 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: devicetree, kernel, Rafael J. Wysocki, Søren Andersen,
	Sebastian Reichel, Daniel Lezcano, Liam Girdwood, linux-pm,
	Lukasz Luba, Conor Dooley, Zhang Rui, Rob Herring,
	Srinivas Kandagatla, Krzysztof Kozlowski, Mark Brown,
	linux-kernel


On Fri, 19 Jan 2024 14:25:15 +0100, Oleksij Rempel wrote:
> Add binding for Power State Change Reason (PSCR) subsystem
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  .../devicetree/bindings/power/reset/pscr.yaml | 51 +++++++++++++++++++
>  1 file changed, 51 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/reset/pscr.yaml
> 

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr.yaml: $id: Cannot determine base path from $id, relative path/filename doesn't match actual path or filename
 	 $id: http://devicetree.org/schemas/power/state-change/pscr.yaml
 	file: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/20240119132521.3609945-2-o.rempel@pengutronix.de

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


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

* Re: [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data
  2024-01-19 13:25 ` [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
@ 2024-01-19 14:50   ` Rob Herring
  0 siblings, 0 replies; 18+ messages in thread
From: Rob Herring @ 2024-01-19 14:50 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: Krzysztof Kozlowski, Mark Brown, kernel, Rafael J. Wysocki,
	devicetree, Søren Andersen, Conor Dooley, Lukasz Luba,
	linux-pm, Liam Girdwood, Zhang Rui, Srinivas Kandagatla,
	Daniel Lezcano, Sebastian Reichel, linux-kernel, Rob Herring


On Fri, 19 Jan 2024 14:25:17 +0100, Oleksij Rempel wrote:
> 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/pscr-nvmem.yaml      | 54 +++++++++++++++++++
>  1 file changed, 54 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml
> 

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml:32:16: [error] empty value in block mapping (empty-values)
./Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml:33:22: [error] empty value in block mapping (empty-values)
./Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml:34:21: [error] empty value in block mapping (empty-values)
./Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml:35:25: [error] empty value in block mapping (empty-values)

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-unknown: None is not of type 'object', 'boolean'
	from schema $id: http://json-schema.org/draft-07/schema#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-over-current: None is not of type 'object', 'boolean'
	from schema $id: http://json-schema.org/draft-07/schema#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-under-voltage: None is not of type 'object', 'boolean'
	from schema $id: http://json-schema.org/draft-07/schema#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-over-temperature: None is not of type 'object', 'boolean'
	from schema $id: http://json-schema.org/draft-07/schema#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-unknown: None is not of type 'object', 'boolean'
	from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-over-current: None is not of type 'object', 'boolean'
	from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-under-voltage: None is not of type 'object', 'boolean'
	from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: properties:pscr-over-temperature: None is not of type 'object', 'boolean'
	from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: $id: Cannot determine base path from $id, relative path/filename doesn't match actual path or filename
 	 $id: http://devicetree.org/schemas/power/state-change/pscr-nvmem.yaml
 	file: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/power/reset/pscr-nvmem.yaml: ignoring, error in schema: properties: pscr-under-voltage
Documentation/devicetree/bindings/power/reset/pscr-nvmem.example.dtb: /example-0/power-state-change-reason: failed to match any schema with compatible: ['pscr-nvmem']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/20240119132521.3609945-4-o.rempel@pengutronix.de

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


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

* Re: [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers
  2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
  2024-01-19 14:50   ` Rob Herring
@ 2024-01-19 17:28   ` Rob Herring
  2024-01-19 17:54     ` Oleksij Rempel
  1 sibling, 1 reply; 18+ messages in thread
From: Rob Herring @ 2024-01-19 17:28 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: Sebastian Reichel, 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 Fri, Jan 19, 2024 at 02:25:15PM +0100, Oleksij Rempel wrote:
> Add binding for Power State Change Reason (PSCR) subsystem

Why? 

How is this different from the reboot reason binding?

> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  .../devicetree/bindings/power/reset/pscr.yaml | 51 +++++++++++++++++++
>  1 file changed, 51 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/reset/pscr.yaml
> 
> diff --git a/Documentation/devicetree/bindings/power/reset/pscr.yaml b/Documentation/devicetree/bindings/power/reset/pscr.yaml
> new file mode 100644
> index 000000000000..1ce973f3473c
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/reset/pscr.yaml
> @@ -0,0 +1,51 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/power/state-change/pscr.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 responsable to store reasons for power state

responsible

> +  changes such as reboot and power-off. Reasons like unknown, under voltage,
> +  and over temperature are captured for diagnostic or automatic recovery
> +  purposes.
> +
> +properties:
> +  $nodename:
> +    pattern: "^pscr(@.*|-([0-9]|[1-9][0-9]+))?$"

Drop. This could be used in any random device.

> +
> +  pscr-unknown:
> +    $ref: /schemas/types.yaml#/definitions/uint32
> +    description: Value to indicate an unknown reason for the power state change.

What's an undocumented value? It would be unknown too, so just drop this 
property.

> +
> +  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 unknow, 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	[flat|nested] 18+ messages in thread

* Re: [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers
  2024-01-19 17:28   ` Rob Herring
@ 2024-01-19 17:54     ` Oleksij Rempel
  0 siblings, 0 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 17:54 UTC (permalink / raw
  To: Rob Herring
  Cc: Sebastian Reichel, 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 Fri, Jan 19, 2024 at 11:28:38AM -0600, Rob Herring wrote:
> On Fri, Jan 19, 2024 at 02:25:15PM +0100, Oleksij Rempel wrote:
> > Add binding for Power State Change Reason (PSCR) subsystem
> 
> Why? 
> 
> How is this different from the reboot reason binding?

I was not able to find "reboot reason", you probably refer to "reboot
mode".

Reboot Mode:
 Purpose: Dictates how the system should reboot (e.g., normal, recovery,
   bootloader).
 Usage: Provides instructions for the next boot mode.
 Scenario: Utilized in planned reboots or software-triggered reset scenarios.

PSCR (Power State Change Reasons):
 Purpose: Logs the reason behind a power state change (e.g., voltage drop,
   over-temperature).
 Usage: Used for rapid logging and post-event analysis, potentially informing
   automatic decision-making in subsequent boots.
 Scenario: Critical in abrupt power-down situations where immediate, detailed
   decision-making is not possible.

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] 18+ messages in thread

* Re: [RFC PATCH v1 7/7] thermal: core: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 13:25 ` [RFC PATCH v1 7/7] thermal: core: " Oleksij Rempel
@ 2024-01-19 18:34   ` Rafael J. Wysocki
  2024-01-19 19:34     ` Oleksij Rempel
  0 siblings, 1 reply; 18+ messages in thread
From: Rafael J. Wysocki @ 2024-01-19 18:34 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 Fri, Jan 19, 2024 at 2:25 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote:
>
> Store the state change reason to some black box for later investigation.

Seriously?

What black box, where, how this is useful and who is going to use it,
pretty please.

> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  drivers/thermal/thermal_core.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 9c17d35ccbbd..5ee3a59d7a0e 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/pscr.h>
>  #include <linux/reboot.h>
>  #include <linux/string.h>
>  #include <linux/of.h>
> @@ -325,6 +326,7 @@ void thermal_zone_device_critical(struct thermal_zone_device *tz)
>         dev_emerg(&tz->device, "%s: critical temperature reached, "
>                   "shutting down\n", tz->type);
>
> +       set_power_state_change_reason(PSCR_OVERTEMPERATURE);
>         hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
>  }
>  EXPORT_SYMBOL(thermal_zone_device_critical);
> --
> 2.39.2
>

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

* Re: [RFC PATCH v1 7/7] thermal: core: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 18:34   ` Rafael J. Wysocki
@ 2024-01-19 19:34     ` Oleksij Rempel
  2024-01-19 20:11       ` Rafael J. Wysocki
  0 siblings, 1 reply; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-19 19:34 UTC (permalink / raw
  To: Rafael J. Wysocki
  Cc: devicetree, Conor Dooley, Daniel Lezcano, Krzysztof Kozlowski,
	Liam Girdwood, Mark Brown, Sebastian Reichel, linux-kernel,
	Søren Andersen, Rob Herring, Srinivas Kandagatla, kernel,
	linux-pm, Zhang Rui, Lukasz Luba

On Fri, Jan 19, 2024 at 07:34:26PM +0100, Rafael J. Wysocki wrote:
> On Fri, Jan 19, 2024 at 2:25 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote:
> >
> > Store the state change reason to some black box for later investigation.
> 
> Seriously?
> 
> What black box, where, how this is useful and who is going to use it,
> pretty please.

The 'black box' refers to a non-volatile memory (NVMEM) cell used by the
Power State Change Reasons (PSCR) framework. This cell stores reasons
for sudden power state changes, like voltage drops or over-temperature
events. This data is invaluable for post-mortem analysis to understand
system failures or abrupt shutdowns. It's particularly useful for
systems where PMICs or watchdogs cannot record such events. The data can
inform recovery routines in the bootloader or early kernel stages during
subsequent boots, enhancing system reliability and aiding in debugging
and diagnostics.

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] 18+ messages in thread

* Re: [RFC PATCH v1 7/7] thermal: core: set Power State Change Reason before hw_protection_shutdown()
  2024-01-19 19:34     ` Oleksij Rempel
@ 2024-01-19 20:11       ` Rafael J. Wysocki
  0 siblings, 0 replies; 18+ messages in thread
From: Rafael J. Wysocki @ 2024-01-19 20:11 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: Rafael J. Wysocki, devicetree, Conor Dooley, Daniel Lezcano,
	Krzysztof Kozlowski, Liam Girdwood, Mark Brown, Sebastian Reichel,
	linux-kernel, Søren Andersen, Rob Herring,
	Srinivas Kandagatla, kernel, linux-pm, Zhang Rui, Lukasz Luba

On Fri, Jan 19, 2024 at 8:34 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote:
>
> On Fri, Jan 19, 2024 at 07:34:26PM +0100, Rafael J. Wysocki wrote:
> > On Fri, Jan 19, 2024 at 2:25 PM Oleksij Rempel <o.rempel@pengutronix.de> wrote:
> > >
> > > Store the state change reason to some black box for later investigation.
> >
> > Seriously?
> >
> > What black box, where, how this is useful and who is going to use it,
> > pretty please.
>
> The 'black box' refers to a non-volatile memory (NVMEM) cell used by the
> Power State Change Reasons (PSCR) framework. This cell stores reasons
> for sudden power state changes, like voltage drops or over-temperature
> events. This data is invaluable for post-mortem analysis to understand
> system failures or abrupt shutdowns. It's particularly useful for
> systems where PMICs or watchdogs cannot record such events. The data can
> inform recovery routines in the bootloader or early kernel stages during
> subsequent boots, enhancing system reliability and aiding in debugging
> and diagnostics.

OK, so please add all of the above to the patch changelog.

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

* Re: [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components
  2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
                   ` (6 preceding siblings ...)
  2024-01-19 13:25 ` [RFC PATCH v1 7/7] thermal: core: " Oleksij Rempel
@ 2024-01-19 23:19 ` Sebastian Reichel
  2024-01-21  6:56   ` Oleksij Rempel
  7 siblings, 1 reply; 18+ messages in thread
From: Sebastian Reichel @ 2024-01-19 23:19 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: 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

[-- Attachment #1: Type: text/plain, Size: 1578 bytes --]

Hi,

On Fri, Jan 19, 2024 at 02:25:14PM +0100, Oleksij Rempel wrote:
> 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.

A couple of things come to my mind:

1. Do we need the DT based reason-string-to-integer mapping? Can we
   just use a fixed mapping instead? It simplifies the binding a
   lot. With that the generic part could be dropped completely.

2. I would expect the infrastructure to read and clear the reason
   during boot. If e.g. a watchdog triggers a reset you will otherwise
   get an incorrect value.

3. The reason is only stored, but not used? We have a sysfs ABI to
   expose the reboot reason to userspace since half a year ago, see
   d40befed9a58 (power: reset: at91-reset: add sysfs interface to
   the power on reason).

4. Available options should be synced with the list in
   include/linux/power/power_on_reason.h

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components
  2024-01-19 23:19 ` [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Sebastian Reichel
@ 2024-01-21  6:56   ` Oleksij Rempel
  0 siblings, 0 replies; 18+ messages in thread
From: Oleksij Rempel @ 2024-01-21  6:56 UTC (permalink / raw
  To: Sebastian Reichel
  Cc: 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

Hi,

On Sat, Jan 20, 2024 at 12:19:09AM +0100, Sebastian Reichel wrote:
> Hi,
> 
> On Fri, Jan 19, 2024 at 02:25:14PM +0100, Oleksij Rempel wrote:
> > 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.
> 
> A couple of things come to my mind:
> 
> 1. Do we need the DT based reason-string-to-integer mapping? Can we
>    just use a fixed mapping instead? It simplifies the binding a
>    lot. With that the generic part could be dropped completely.

The project I'm working is using RTC for storage. The RTC device in
question provides 8 bits, 3 bits are assigned for PSCR.
Currently all reasons provided in this patch set would fit int to 3 bits,
but soon or later it may expand.

> 2. I would expect the infrastructure to read and clear the reason
>    during boot. If e.g. a watchdog triggers a reset you will otherwise
>    get an incorrect value.

Hm.. good point. I'll set a value on the boot that there is currently no
attempt to shutdown at all. PSCR works only for software assisted
shutdown/reboot. It should extend, not replace PMIC or watchdog detected
reasons.

> 3. The reason is only stored, but not used? We have a sysfs ABI to
>    expose the reboot reason to userspace since half a year ago, see
>    d40befed9a58 (power: reset: at91-reset: add sysfs interface to
>    the power on reason).

ACK. I'll add sysfs support.
For my use case, the user is the bootloader. The is one of reasons why
DT is used for mappings, this is the stable ABI between this systems.

> 4. Available options should be synced with the list in
>    include/linux/power/power_on_reason.h

ACK

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] 18+ messages in thread

end of thread, other threads:[~2024-01-21  6:56 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-19 13:25 [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Oleksij Rempel
2024-01-19 13:25 ` [RFC PATCH v1 1/7] dt-bindings: power: reset: add generic PSCR binding trackers Oleksij Rempel
2024-01-19 14:50   ` Rob Herring
2024-01-19 17:28   ` Rob Herring
2024-01-19 17:54     ` Oleksij Rempel
2024-01-19 13:25 ` [RFC PATCH v1 2/7] power: reset: Introduce PSCR Tracking Framework for Non-Volatile Storage Oleksij Rempel
2024-01-19 13:25 ` [RFC PATCH v1 3/7] dt-bindings: power: reset: add bindings for NVMEM hardware storing PSCR Data Oleksij Rempel
2024-01-19 14:50   ` Rob Herring
2024-01-19 13:25 ` [RFC PATCH v1 4/7] nvmem: provide consumer access to cell size metrics Oleksij Rempel
2024-01-19 13:25 ` [RFC PATCH v1 5/7] power: reset: add PSCR NVMEM Driver for Storing Power State Change Reasons Oleksij Rempel
2024-01-19 13:25 ` [RFC PATCH v1 6/7] regulator: set Power State Change Reason before hw_protection_shutdown() Oleksij Rempel
2024-01-19 14:07   ` Mark Brown
2024-01-19 13:25 ` [RFC PATCH v1 7/7] thermal: core: " Oleksij Rempel
2024-01-19 18:34   ` Rafael J. Wysocki
2024-01-19 19:34     ` Oleksij Rempel
2024-01-19 20:11       ` Rafael J. Wysocki
2024-01-19 23:19 ` [RFC PATCH v1 0/7] Introduction of PSCR Framework and Related Components Sebastian Reichel
2024-01-21  6:56   ` 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 read-only IMAP folder(s) and NNTP newsgroup(s).