mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support
@ 2026-11-19 15:36 Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Ahmed Azeem @ 2026-11-19 15:36 UTC (permalink / raw)
  To: linux-arm-kernel, Sudeep Holla
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Mark Rutland,
	Lorenzo Pieralisi, linux-api, linux-doc, linux-kernel, Divin Raj,
	Bertrand Marquis, Ahmed Azeem

Hi all,

This series proposes an arm64 driver for the Arm Platform Fault
Detection
Interface (PFDI), providing a common Linux interface for privileged
userspace to discover and invoke firmware diagnostics.

We are posting this series as an RFC to seek community feedback on both
the proposed Linux interface and the underlying firmware specification
before the specification is finalized. This series and its validation
use
the PFDI 1.0 ALP0 specification [1]. The current public revision is PFDI
1.0 BET0 [2]. Feedback on the software integration will help identify
any
changes needed to align an upstream implementation with the evolving
specification.

PFDI defines firmware calls for discovering a PE's test engine and test
parts, executing online tests, retrieving out-of-reset test results,
checking
firmware integrity, and injecting responses to validate error handling
[1].
Trusted Firmware-A documents an implementation of these services [3].

A practical use case is a safety-critical or high-availability platform
that uses manufacturer-provided software tests in firmware to detect
faults at boot and during operation. These tests can complement hardware
error-detection mechanisms by targeting failure modes that those
mechanisms do not cover. Firmware performs the boot-time tests, Linux
software can retrieve their results and request online tests at
intervals
chosen by the platform's diagnostic application. The diagnostic coverage
depends on the tests implemented by the platform.

The Linux use case is a privileged diagnostic application that needs to
discover the tests available on a CPU, run a selected range, and
retrieve
the outcome, including a failing test identifier where firmware provides
one. A common driver and documented userspace ABI would allow that
application to use the same interface across platforms implementing
PFDI,
while keeping the test implementation in platform firmware.

Kernel mediation is needed to issue the SMC calls and coordinate them
with
CPU placement and lifetime. PFDI operations are PE-local [1], so
executing
a request on the intended CPU is part of the interface's correctness.
The proposed driver uses a worker bound to each target CPU and exposes
/dev/cpu/<cpu>/pfdi. Requests and results are carried through typed
ioctls;
device nodes default to mode 0600, and each CPU device permits one open
owner at a time.

The driver leaves the choice of test ranges, execution frequency, and
response to a reported fault to userspace. It adds no periodic test loop
or automatic recovery policy. The intended upstream benefit is a shared
firmware access mechanism that platform diagnostic tools can build on,
without embedding their deployment-specific policy in the kernel.

The four patches provide:

1. A UAPI header, ioctl allocation, ABI documentation, and MAINTAINERS
   entry.
2. The firmware driver, per-CPU devices, and query operations.
3. Test execution, out-of-reset result retrieval, and error injection.
4. Worker serialization and CPU hotplug lifetime handling.

Error injection is included to exercise the software path that consumes
PFDI responses. The proposed ABI also returns the firmware's 64-bit
failing
test identifier for test execution and out-of-reset result queries.

Arm's public reference software documentation describes firmware, Linux
driver, and userspace components for a PFDI stack [4]. Its default
reference
test backend is a placeholder, so this provides integration context
rather
than evidence of hardware diagnostic coverage.

Validation of this revision:

Runtime validation was performed on Arm Zena CSS using its reference
PFDI
firmware and userspace stack, exercising service startup, per-core
diagnostic execution, out-of-reset result queries, CLI operations,
monitoring, and forced-error handling. As the default reference firmware
test backend is an integration placeholder rather than a production test
library, this validates the end-to-end userspace, Linux, SMC, and
firmware
paths rather than hardware diagnostic coverage.

I would appreciate feedback on the specification's suitability for Linux
integration, the proposed per-CPU ioctl ABI, the worker and hotplug
model,
and whether this belongs under drivers/firmware. In particular, is this
the right scope for a common PFDI interface, and should error injection
be exposed separately from normal diagnostic operations?

[1] Arm Platform Fault Detection Interface Specification, document
110468,
    version 1.0 ALP0:
    https://documentation-service.arm.com/static/68593b17961937560be90fbe

[2] Arm Platform Fault Detection Interface Specification, document
110468,
    version 1.0 BET0:
    https://support.arm.com/documentation/110468/1-0bet0/?lang=en

[3] Trusted Firmware-A PFDI implementation documentation:
    https://tf-a.docs.trustedfirmware.org/en/latest/components/pfdi.html

[4] Arm Zena CSS PFDI design documentation:
    https://arm-zena-css.docs.arm.com/en/latest/design/platform_fault_detection_interface.html

To: linux-arm-kernel@lists.infradead.org
To: Sudeep Holla <sudeep.holla@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Lorenzo Pieralisi <lpieralisi@kernel.org>
Cc: linux-api@vger.kernel.org
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: Divin Raj <divin.raj@arm.com>
Cc: Bertrand Marquis <Bertrand.Marquis@arm.com>

Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
---
Divin Raj (4):
      uapi: firmware: arm: Add PFDI userspace ABI
      firmware: arm: Add PFDI misc driver skeleton
      firmware: arm: Add PFDI run, result, and error injection support
      firmware: arm: Harden PFDI worker and hotplug handling

 Documentation/ABI/testing/dev-cpu-pfdi             | 103 +++
 Documentation/userspace-api/ioctl/ioctl-number.rst |   1 +
 MAINTAINERS                                        |   8 +
 drivers/firmware/Kconfig                           |  14 +
 drivers/firmware/Makefile                          |   1 +
 drivers/firmware/arm_pfdi.c                        | 948 +++++++++++++++++++++
 include/uapi/linux/pfdi.h                          |  89 ++
 7 files changed, 1164 insertions(+)
---
base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
change-id: 20261119-topics-ahmaze01-upstream-pfdi-rfc-534c8594d266

Best regards,
-- 
Ahmed Azeem <ahmed.azeem@arm.com>


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

* [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI
  2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
@ 2026-11-19 15:36 ` Ahmed Azeem
  2026-11-19 21:35   ` Jonathan Cameron
  2026-11-19 15:36 ` [PATCH RFC 2/4] firmware: arm: Add PFDI misc driver skeleton Ahmed Azeem
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Ahmed Azeem @ 2026-11-19 15:36 UTC (permalink / raw)
  To: linux-arm-kernel, Sudeep Holla
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Mark Rutland,
	Lorenzo Pieralisi, linux-api, linux-doc, linux-kernel, Divin Raj,
	Bertrand Marquis, Ahmed Azeem

From: Divin Raj <divin.raj@arm.com>

Define the userspace ABI for the Arm Platform Fault Detection
Interface (PFDI).

Subsequent commits introduce a driver that exposes a per-CPU misc
device. The ioctl definitions, data structures, and return codes
must therefore be shared with userspace. Add these definitions to
a UAPI header and document the ABI for /dev/cpu/<cpu>/pfdi.

Placing the interface under include/uapi ensures availability via
headers_install and clearly identifies it as a userspace contract.

Link: https://developer.arm.com/documentation/110468/1-0alp0

Signed-off-by: Divin Raj <divin.raj@arm.com>
Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
---
 Documentation/ABI/testing/dev-cpu-pfdi             | 103 +++++++++++++++++++++
 Documentation/userspace-api/ioctl/ioctl-number.rst |   1 +
 MAINTAINERS                                        |   7 ++
 include/uapi/linux/pfdi.h                          |  89 ++++++++++++++++++
 4 files changed, 200 insertions(+)

diff --git a/Documentation/ABI/testing/dev-cpu-pfdi b/Documentation/ABI/testing/dev-cpu-pfdi
new file mode 100644
index 000000000000..e07ed6baa7f8
--- /dev/null
+++ b/Documentation/ABI/testing/dev-cpu-pfdi
@@ -0,0 +1,103 @@
+What:		/dev/cpu/<cpu>/pfdi
+Date:		May 2026
+KernelVersion:	6.18
+Contact:	Linux ARM Kernel Mailing list <linux-arm-kernel@lists.infradead.org>
+Reference:	Arm Platform Fault Detection Interface, document 110468,
+		version 1.0 ALP0
+Description:	Per-CPU Arm Platform Fault Detection Interface (PFDI) misc
+		device nodes. Each node targets the CPU encoded in its
+		path and exposes the PFDI service implemented by firmware.
+		PFDI uses the Arm SMCCC SMC64 calling convention and is
+		PE-local. Userspace is therefore expected to bind itself to
+		the target CPU before opening or issuing ioctls on the
+		matching device node. Each node is single-open: only one
+		userspace owner at a time may hold /dev/cpu/<cpu>/pfdi for a
+		given CPU. Additional open attempts fail with -EBUSY while the
+		device is already open.
+
+		The ABI is defined in <linux/pfdi.h>. The following ioctls
+		are supported:
+
+		* PFDI_IOCTL_VERSION
+		  Read the PFDI version for the target CPU. On success the
+		  ioctl returns 0 and copies a 32-bit version value to
+		  userspace with the following layout:
+		  Bits[30:16] PFDI major version
+		  Bits[15:0] PFDI minor version
+
+		* PFDI_IOCTL_FEATURES
+		  Query whether a PFDI function ID is supported on the
+		  target CPU. Userspace passes a 32-bit PFDI function ID.
+		  The ioctl returns 0 when the function is supported,
+		  and -EOPNOTSUPP when it is not supported on the target
+		  CPU, reserved by the specification, or otherwise not
+		  recognized by firmware.
+
+		* PFDI_IOCTL_PE_TEST_ID
+		  Read the test engine identifier for the target CPU.
+		  On success the ioctl returns 0 and copies a 64-bit value to
+		  userspace with the following layout:
+		  Bits[63:32] reserved and zero
+		  Bits[31:24] vendor ID
+		  Bits[23:20] reserved and zero
+		  Bits[19:16] vendor-defined implementation ID
+		  Bits[15:8] major version
+		  Bits[7:0] minor version
+
+		* PFDI_IOCTL_PE_TEST_PART_COUNT
+		  Read the number of available test parts for the target
+		  CPU. On success the ioctl returns 0 and copies a signed
+		  64-bit count to userspace. The smallest schedulable unit is
+		  a test part, and valid test part indices are in the range 0
+		  to count - 1.
+
+		* PFDI_IOCTL_PE_TEST_RUN
+		  Run a requested inclusive test part range on the target
+		  CPU. struct pfdi_test_run.range.start and .end are signed
+		  64-bit values.
+		  Passing start = -1 and end = -1 requests execution of all
+		  available test parts on the target CPU. Any mixed use of
+		  -1, reversed ranges, out-of-range indices, or non-zero
+		  reserved arguments is rejected with -EINVAL, either by the
+		  kernel before the request is issued or by firmware.
+		  If firmware reports PFDI_RET_FAULT_FOUND, the ioctl returns
+		  -EFAULT and copies a signed 64-bit failing test part
+		  identifier to struct pfdi_test_run.ft_id. If the failing
+		  part cannot be identified, ft_id is set to
+		  PFDI_RET_UNKNOWN.
+
+		* PFDI_IOCTL_PE_TEST_RESULT
+		  Fetch the stored out-of-reset test result for the target CPU.
+		  If firmware reports PFDI_RET_FAULT_FOUND, the ioctl returns
+		  -EFAULT and copies a signed 64-bit failing test part
+		  identifier to userspace. If the failing part cannot be
+		  identified, the copied value is PFDI_RET_UNKNOWN. If no
+		  power-on tests were executed on the target CPU, the ioctl
+		  returns -EAGAIN.
+
+		* PFDI_IOCTL_FW_CHECK
+		  Ask firmware to execute platform firmware resident software
+		  integrity checks. The ioctl returns 0 when no fault is
+		  detected. It returns -EFAULT when firmware reports a fault.
+		  Platforms that do not implement firmware integrity checks
+		  still report success.
+
+		* PFDI_IOCTL_FORCE_ERROR
+		  Schedule injection of a specific PFDI return value for the
+		  next invocation of a selected PFDI function on the same
+		  CPU. The requested function ID must be a supported PFDI
+		  function IDs. Only one pending injected error is permitted
+		  per CPU and function, and a new request replaces any older
+		  undelivered request for the same pair. When the injection
+		  is delivered, firmware returns the injected value in x0 and
+		  zeros x1 to x4.
+
+		These device nodes reflect the PFDI 1.0 ALP0 specification.
+		PFDI state is not persistent across reset or firmware update;
+		userspace must rediscover capabilities and any test state
+		after such events.
+
+		These nodes are intended for platform bring-up and validation.
+		Access should therefore be restricted to privileged software.
+
+Users:		Platform validation and manufacturing test tools
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index dbe462040999..aa5a6cf20d3c 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -170,6 +170,7 @@ Code  Seq#    Include File                                             Comments
 'I'   all    linux/isdn.h                                              conflict!
 'I'   00-0F  drivers/isdn/divert/isdn_divert.h                         conflict!
 'I'   40-4F  linux/mISDNif.h                                           conflict!
+'J'   01-08  linux/pfdi.h                                              Arm Platform Fault Detection Interface
 'K'   all    linux/kd.h
 'L'   00-1F  linux/loop.h                                              conflict!
 'L'   10-1F  drivers/scsi/mpt3sas/mpt3sas_ctl.h                        conflict!
diff --git a/MAINTAINERS b/MAINTAINERS
index d738990161bd..de9c296d1de1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14012,6 +14012,13 @@ F:	drivers/iommu/iova.c
 F:	include/linux/iommu-dma.h
 F:	include/linux/iova.h
 
+Platform Fault Detection Interface - ARM PFDI
+M:	Ahmed Azeem <ahmed.azeem@arm.com>
+L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
+S:	Maintained
+F:	Documentation/ABI/testing/dev-cpu-pfdi
+F:	include/uapi/linux/pfdi.h
+
 IOMMU SUBSYSTEM
 M:	Joerg Roedel (AMD) <joro@8bytes.org>
 M:	Will Deacon <will@kernel.org>
diff --git a/include/uapi/linux/pfdi.h b/include/uapi/linux/pfdi.h
new file mode 100644
index 000000000000..e3b7fe19daf4
--- /dev/null
+++ b/include/uapi/linux/pfdi.h
@@ -0,0 +1,89 @@
+/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
+/*
+ * Arm Platform Fault Detection Interface (PFDI) userspace ABI
+ *
+ * Copyright (C) 2026 Arm Ltd.
+ */
+
+#ifndef _UAPI_LINUX_PFDI_H
+#define _UAPI_LINUX_PFDI_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/*
+ * SMC function IDs for PFDI v1.0.
+ * These follow the SMCCC fast-call, 64-bit, Standard Secure Service encoding.
+ */
+#define PFDI_FN_BASE			0xC40002D0U
+#define PFDI_FN(n)			(PFDI_FN_BASE + (n))
+
+#define PFDI_FN_VERSION			PFDI_FN(0x00)
+#define PFDI_FN_FEATURES		PFDI_FN(0x01)
+#define PFDI_FN_PE_TEST_ID		PFDI_FN(0x02)
+#define PFDI_FN_PE_TEST_PART_COUNT	PFDI_FN(0x03)
+#define PFDI_FN_PE_TEST_RUN		PFDI_FN(0x04)
+#define PFDI_FN_PE_TEST_RESULT		PFDI_FN(0x05)
+#define PFDI_FN_FW_CHECK		PFDI_FN(0x06)
+#define PFDI_FN_FORCE_ERROR		PFDI_FN(0x07)
+
+#define PFDI_VERSION_MAJOR_SHIFT	16
+#define PFDI_VERSION_MAJOR_MASK		0x7FFFU
+#define PFDI_VERSION_MINOR_MASK		0xFFFFU
+
+#define PFDI_VERSION_MAJOR(ver) \
+	(((ver) >> PFDI_VERSION_MAJOR_SHIFT) & PFDI_VERSION_MAJOR_MASK)
+#define PFDI_VERSION_MINOR(ver)		((ver) & PFDI_VERSION_MINOR_MASK)
+#define PFDI_VERSION(maj, min) \
+	((((maj) & PFDI_VERSION_MAJOR_MASK) << PFDI_VERSION_MAJOR_SHIFT) | \
+	 ((min) & PFDI_VERSION_MINOR_MASK))
+
+/* PFDI return values */
+#define PFDI_RET_TEST_COUNT_ZERO	(-8)
+#define PFDI_RET_UNKNOWN		(-7)
+#define PFDI_RET_NOT_RUN		(-6)
+#define PFDI_RET_ERROR			(-5)
+#define PFDI_RET_FAULT_FOUND		(-4)
+#define PFDI_RET_INVALID_PARAM		(-3)
+#ifndef SMCCC_RET_NOT_SUPPORTED
+#define SMCCC_RET_NOT_SUPPORTED		(-1)
+#endif
+#ifndef SMCCC_RET_SUCCESS
+#define SMCCC_RET_SUCCESS		0
+#endif
+
+struct pfdi_test_range {
+	__s64 start;
+	__s64 end;
+};
+
+struct pfdi_test_run {
+	struct pfdi_test_range range;
+	__s64 ft_id;
+};
+
+struct pfdi_force_error {
+	__u32 smc_fid;
+	/* Keep the ioctl layout identical for native and compat userspace. */
+	__u32 reserved;
+	__s64 error_id;
+};
+
+#define PFDI_IOCTL_VERSION \
+	_IOR('J', 1, __u32)
+#define PFDI_IOCTL_FEATURES \
+	_IOW('J', 2, __u32)
+#define PFDI_IOCTL_PE_TEST_ID \
+	_IOR('J', 3, __u64)
+#define PFDI_IOCTL_PE_TEST_PART_COUNT \
+	_IOR('J', 4, __s64)
+#define PFDI_IOCTL_PE_TEST_RUN \
+	_IOWR('J', 5, struct pfdi_test_run)
+#define PFDI_IOCTL_PE_TEST_RESULT \
+	_IOR('J', 6, __s64)
+#define PFDI_IOCTL_FW_CHECK \
+	_IO('J', 7)
+#define PFDI_IOCTL_FORCE_ERROR \
+	_IOW('J', 8, struct pfdi_force_error)
+
+#endif /* _UAPI_LINUX_PFDI_H */

-- 
2.43.0


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

* [PATCH RFC 2/4] firmware: arm: Add PFDI misc driver skeleton
  2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
@ 2026-11-19 15:36 ` Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 4/4] firmware: arm: Harden PFDI worker and hotplug handling Ahmed Azeem
  3 siblings, 0 replies; 6+ messages in thread
From: Ahmed Azeem @ 2026-11-19 15:36 UTC (permalink / raw)
  To: linux-arm-kernel, Sudeep Holla
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Mark Rutland,
	Lorenzo Pieralisi, linux-api, linux-doc, linux-kernel, Divin Raj,
	Bertrand Marquis, Ahmed Azeem

From: Divin Raj <divin.raj@arm.com>

Add the initial in-kernel driver for the Arm Platform Fault Detection
Interface (PFDI).

Hook the driver into drivers/firmware, probe the firmware interface via
SMCCC, and create per-CPU misc devices at /dev/cpu/<cpu>/pfdi. Implement
the read-only query operations used to retrieve the firmware version,
test library identifier, test count, and firmware health check support.

Limit this initial change to probe and query functionality to keep the
core driver structure easy to review before introducing stateful
execution and error injection support.

Signed-off-by: Divin Raj <divin.raj@arm.com>
Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
---
 MAINTAINERS                 |   1 +
 drivers/firmware/Kconfig    |  14 ++
 drivers/firmware/Makefile   |   1 +
 drivers/firmware/arm_pfdi.c | 489 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 505 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index de9c296d1de1..41101524f3c8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14017,6 +14017,7 @@ M:	Ahmed Azeem <ahmed.azeem@arm.com>
 L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
 S:	Maintained
 F:	Documentation/ABI/testing/dev-cpu-pfdi
+F:	drivers/firmware/arm_pfdi.c
 F:	include/uapi/linux/pfdi.h
 
 IOMMU SUBSYSTEM
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index 43e2e2a64594..f8f40027e70a 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -60,6 +60,20 @@ config ARM_SDEI_NMI
 
 	  If unsure, say N.
 
+config ARM_PFDI
+	tristate "Arm Platform Fault Detection Interface (PFDI)"
+	depends on ARM64 && HAVE_ARM_SMCCC
+	help
+	  Expose the Arm Platform Fault Detection Interface (PFDI)
+	  implemented by platform firmware through per-CPU misc devices.
+
+	  This creates /dev/cpu/<cpu>/pfdi device nodes that allow
+	  privileged userspace to query PFDI test metadata and trigger the
+	  firmware health check.
+
+	  Say M or Y here if your platform firmware implements PFDI and you
+	  need Linux userspace access to it.
+
 config EDD
 	tristate "BIOS Enhanced Disk Drive calls determine boot disk"
 	depends on X86
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index 50b338313152..6dd53e2426bd 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -2,6 +2,7 @@
 #
 # Makefile for the linux kernel.
 #
+obj-$(CONFIG_ARM_PFDI)		+= arm_pfdi.o
 obj-$(CONFIG_ARM_SCPI_PROTOCOL)	+= arm_scpi.o
 obj-$(CONFIG_ARM_SDE_INTERFACE)	+= arm_sdei.o
 obj-$(CONFIG_ARM_SDEI_NMI)	+= arm_sdei_nmi.o
diff --git a/drivers/firmware/arm_pfdi.c b/drivers/firmware/arm_pfdi.c
new file mode 100644
index 000000000000..8c6d315b0572
--- /dev/null
+++ b/drivers/firmware/arm_pfdi.c
@@ -0,0 +1,489 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (C) 2026 Arm Ltd.
+
+#define pr_fmt(fmt) "arm-pfdi: " fmt
+
+#include <linux/arm-smccc.h>
+#include <linux/completion.h>
+#include <linux/compat.h>
+#include <linux/cpuhotplug.h>
+#include <linux/cpu.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/kthread.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/pfdi.h>
+#include <linux/semaphore.h>
+#include <linux/slab.h>
+#include <linux/smp.h>
+#include <linux/uaccess.h>
+
+struct pfdi_per_cpu_info {
+	/* Ensures a single userspace owner per CPU while the node is open. */
+	struct semaphore open_sem;
+};
+
+struct pfdi_dev_data {
+	unsigned int cpu_id;
+	char name[16];
+	char nodename[24];
+};
+
+struct pfdi_cpu_worker {
+	struct kthread_work work;
+	struct completion done;
+	u32 feature_fid;
+	/* Protects worker lifetime and in-flight work state. */
+	struct mutex worker_lock;
+	struct arm_smccc_res smc_ret;
+	int result;
+};
+
+static const u32 pfdi_supported_fids[] = {
+	PFDI_FN_VERSION,
+	PFDI_FN_FEATURES,
+	PFDI_FN_PE_TEST_ID,
+	PFDI_FN_PE_TEST_PART_COUNT,
+	PFDI_FN_PE_TEST_RUN,
+	PFDI_FN_PE_TEST_RESULT,
+	PFDI_FN_FW_CHECK,
+	PFDI_FN_FORCE_ERROR,
+};
+
+static DEFINE_PER_CPU(struct kthread_worker *, pfdi_worker);
+static DEFINE_PER_CPU(struct miscdevice, pfdi_misc_device);
+static DEFINE_PER_CPU(struct pfdi_dev_data, pfdi_device_data);
+static DEFINE_PER_CPU(struct pfdi_cpu_worker, pfdi_worker_ctx);
+static DEFINE_PER_CPU(struct pfdi_per_cpu_info, pfdi_cpu_info);
+
+static enum cpuhp_state pfdi_cpuhp_state;
+
+static int pfdi_to_linux_errno(int err)
+{
+	switch (err) {
+	case SMCCC_RET_SUCCESS:
+		return 0;
+	case SMCCC_RET_NOT_SUPPORTED:
+		return -EOPNOTSUPP;
+	case PFDI_RET_INVALID_PARAM:
+		return -EINVAL;
+	case PFDI_RET_NOT_RUN:
+	case PFDI_RET_TEST_COUNT_ZERO:
+		return -EAGAIN;
+	case PFDI_RET_ERROR:
+	case PFDI_RET_UNKNOWN:
+		return -EIO;
+	case PFDI_RET_FAULT_FOUND:
+		return -EFAULT;
+	default:
+		return -EINVAL;
+	}
+}
+
+static struct arm_smccc_res pfdi_smc_call(u32 smc_fid, u64 x1, u64 x2, u64 x3,
+					  u64 x4)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(smc_fid, x1, x2, x3, x4, 0, 0, 0, &res);
+	return res;
+}
+
+static int pfdi_smc_feature(u32 smc_fid)
+{
+	return (int)pfdi_smc_call(PFDI_FN_FEATURES, smc_fid, 0, 0, 0).a0;
+}
+
+static int pfdi_validate_cpu_features(void)
+{
+	unsigned int i;
+	int ret;
+	struct arm_smccc_res smc_ret;
+
+	smc_ret = pfdi_smc_call(PFDI_FN_VERSION, 0, 0, 0, 0);
+	if ((int)smc_ret.a0 < SMCCC_RET_SUCCESS)
+		return pfdi_to_linux_errno((int)smc_ret.a0);
+
+	if (PFDI_VERSION_MAJOR(smc_ret.a0) != 1)
+		return -EOPNOTSUPP;
+
+	for (i = 0; i < ARRAY_SIZE(pfdi_supported_fids); i++) {
+		ret = pfdi_smc_feature(pfdi_supported_fids[i]);
+		if (ret < 0)
+			return pfdi_to_linux_errno(ret);
+	}
+
+	return 0;
+}
+
+static int pfdi_thread_worker(unsigned int cpu,
+			      void (*handler)(struct kthread_work *work),
+			      unsigned long timeout)
+{
+	int ret = 0;
+	struct kthread_worker *worker;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	mutex_lock(&worker_ctx->worker_lock);
+	worker = per_cpu(pfdi_worker, cpu);
+	if (!worker) {
+		mutex_unlock(&worker_ctx->worker_lock);
+		return -ENODEV;
+	}
+
+	reinit_completion(&worker_ctx->done);
+	kthread_init_work(&worker_ctx->work, handler);
+	kthread_queue_work(worker, &worker_ctx->work);
+
+	if (!wait_for_completion_timeout(&worker_ctx->done, timeout)) {
+		ret = -ETIME;
+		kthread_cancel_work_sync(&worker_ctx->work);
+	}
+
+	mutex_unlock(&worker_ctx->worker_lock);
+	return ret;
+}
+
+static void pfdi_pe_get_test_id_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_ID, 0, 0, 0, 0);
+	complete(&worker_ctx->done);
+}
+
+static void pfdi_pe_get_version_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_VERSION, 0, 0, 0, 0);
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_get_version(unsigned int cpu, u32 *version)
+{
+	s64 ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_get_version_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	ret = (int)worker_ctx->smc_ret.a0;
+	if (ret >= 0)
+		*version = (u32)ret;
+
+	if (ret >= 0)
+		return 0;
+
+	return pfdi_to_linux_errno((int)ret);
+}
+
+static void pfdi_pe_get_features_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->result = pfdi_smc_call(PFDI_FN_FEATURES,
+					   worker_ctx->feature_fid,
+					   0, 0, 0).a0;
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_get_features(unsigned int cpu, u32 fid)
+{
+	int ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	worker_ctx->feature_fid = fid;
+	ret = pfdi_thread_worker(cpu, pfdi_pe_get_features_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	return pfdi_to_linux_errno(worker_ctx->result);
+}
+
+static int pfdi_pe_get_test_id(unsigned int cpu, u64 *lib_info)
+{
+	int ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_get_test_id_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	ret = (int)worker_ctx->smc_ret.a0;
+	if (!ret)
+		*lib_info = worker_ctx->smc_ret.a1;
+
+	return pfdi_to_linux_errno(ret);
+}
+
+static void pfdi_pe_get_test_count_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_PART_COUNT,
+					    0, 0, 0, 0);
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_get_test_count(unsigned int cpu, s64 *count)
+{
+	s64 ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_get_test_count_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	ret = (s64)worker_ctx->smc_ret.a0;
+	if (ret >= 0) {
+		*count = ret;
+		return 0;
+	}
+
+	return pfdi_to_linux_errno((int)ret);
+}
+
+static void pfdi_pe_fw_check_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->result = pfdi_smc_call(PFDI_FN_FW_CHECK,
+					   0, 0, 0, 0).a0;
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_fw_check(unsigned int cpu)
+{
+	int ret;
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_fw_check_fn, MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	return pfdi_to_linux_errno(per_cpu(pfdi_worker_ctx, cpu).result);
+}
+
+static int arm_pfdi_open(struct inode *inode, struct file *file)
+{
+	struct miscdevice *misc_dev = file->private_data;
+	struct pfdi_dev_data *data;
+	unsigned int cpu;
+
+	if (!misc_dev || !misc_dev->this_device)
+		return -ENODEV;
+
+	data = dev_get_drvdata(misc_dev->this_device);
+	if (!data)
+		return -ENODEV;
+
+	cpu = get_cpu();
+	if (data->cpu_id != cpu) {
+		put_cpu();
+		return -EPERM;
+	}
+
+	if (down_trylock(&per_cpu(pfdi_cpu_info, cpu).open_sem)) {
+		put_cpu();
+		return -EBUSY;
+	}
+
+	put_cpu();
+	return 0;
+}
+
+static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
+			   unsigned long arg)
+{
+	int err = 0;
+	u32 feature_fid;
+	u32 version;
+	s64 count;
+	u64 lib_info;
+	struct miscdevice *misc_dev = file->private_data;
+	struct pfdi_dev_data *device_data;
+	unsigned int cpu;
+
+	if (!misc_dev || !misc_dev->this_device)
+		return -ENODEV;
+
+	device_data = dev_get_drvdata(misc_dev->this_device);
+	if (!device_data)
+		return -ENODEV;
+
+	cpu = device_data->cpu_id;
+
+	switch (cmd) {
+	case PFDI_IOCTL_VERSION:
+		err = pfdi_pe_get_version(cpu, &version);
+		if (!err && copy_to_user((void __user *)arg, &version,
+					 sizeof(version)))
+			err = -EFAULT;
+		break;
+	case PFDI_IOCTL_FEATURES:
+		if (copy_from_user(&feature_fid, (void __user *)arg,
+				   sizeof(feature_fid)))
+			return -EFAULT;
+
+		err = pfdi_pe_get_features(cpu, feature_fid);
+		break;
+	case PFDI_IOCTL_PE_TEST_ID:
+		err = pfdi_pe_get_test_id(cpu, &lib_info);
+		if (!err && copy_to_user((void __user *)arg, &lib_info,
+					 sizeof(lib_info)))
+			err = -EFAULT;
+		break;
+	case PFDI_IOCTL_PE_TEST_PART_COUNT:
+		err = pfdi_pe_get_test_count(cpu, &count);
+		if (!err && copy_to_user((void __user *)arg, &count,
+					 sizeof(count)))
+			err = -EFAULT;
+		break;
+	case PFDI_IOCTL_FW_CHECK:
+		err = pfdi_pe_fw_check(cpu);
+		break;
+	default:
+		err = -ENOTTY;
+		break;
+	}
+
+	return err;
+}
+
+static int arm_pfdi_release(struct inode *inode, struct file *file)
+{
+	struct miscdevice *misc_dev = file->private_data;
+	struct pfdi_dev_data *data;
+	unsigned int cpu;
+
+	if (!misc_dev || !misc_dev->this_device)
+		return 0;
+
+	data = dev_get_drvdata(misc_dev->this_device);
+	cpu = data ? data->cpu_id : raw_smp_processor_id();
+	up(&per_cpu(pfdi_cpu_info, cpu).open_sem);
+
+	return 0;
+}
+
+static const struct file_operations arm_pfdi_fops = {
+	.owner		= THIS_MODULE,
+	.open		= arm_pfdi_open,
+	.release	= arm_pfdi_release,
+	.unlocked_ioctl	= arm_pfdi_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl	= compat_ptr_ioctl,
+#endif
+};
+
+static void arm_pfdi_device_init(struct miscdevice *misc_dev,
+				 struct pfdi_dev_data *dev_data, unsigned int cpu)
+{
+	misc_dev->minor = MISC_DYNAMIC_MINOR;
+	dev_data->cpu_id = cpu;
+	snprintf(dev_data->name, sizeof(dev_data->name), "pfdi%u", cpu);
+	snprintf(dev_data->nodename, sizeof(dev_data->nodename),
+		 "cpu/%u/pfdi", cpu);
+	misc_dev->name = dev_data->name;
+	misc_dev->nodename = dev_data->nodename;
+	misc_dev->mode = 0600;
+	misc_dev->fops = &arm_pfdi_fops;
+}
+
+static int arm_pfdi_device_create(unsigned int cpu)
+{
+	int ret;
+	struct miscdevice *misc_dev = &per_cpu(pfdi_misc_device, cpu);
+	struct pfdi_dev_data *dev_data = &per_cpu(pfdi_device_data, cpu);
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	memset(misc_dev, 0, sizeof(*misc_dev));
+	arm_pfdi_device_init(misc_dev, dev_data, cpu);
+
+	sema_init(&per_cpu(pfdi_cpu_info, cpu).open_sem, 1);
+	mutex_init(&worker_ctx->worker_lock);
+	init_completion(&worker_ctx->done);
+	per_cpu(pfdi_worker, cpu) =
+		kthread_create_worker_on_cpu(cpu, 0, "pfdi/%u");
+	if (IS_ERR(per_cpu(pfdi_worker, cpu))) {
+		ret = PTR_ERR(per_cpu(pfdi_worker, cpu));
+		per_cpu(pfdi_worker, cpu) = NULL;
+		return ret;
+	}
+
+	ret = misc_register(misc_dev);
+	if (ret)
+		goto destroy_worker;
+
+	if (!misc_dev->this_device) {
+		ret = -ENODEV;
+		goto deregister_misc;
+	}
+
+	dev_set_drvdata(misc_dev->this_device, dev_data);
+
+	return 0;
+
+deregister_misc:
+	misc_deregister(misc_dev);
+destroy_worker:
+	kthread_destroy_worker(per_cpu(pfdi_worker, cpu));
+	per_cpu(pfdi_worker, cpu) = NULL;
+	return ret;
+}
+
+static int arm_pfdi_device_destroy(unsigned int cpu)
+{
+	struct miscdevice *misc_dev = &per_cpu(pfdi_misc_device, cpu);
+
+	if (per_cpu(pfdi_worker, cpu)) {
+		kthread_flush_worker(per_cpu(pfdi_worker, cpu));
+		kthread_destroy_worker(per_cpu(pfdi_worker, cpu));
+		per_cpu(pfdi_worker, cpu) = NULL;
+	}
+
+	dev_set_drvdata(misc_dev->this_device, NULL);
+	misc_deregister(misc_dev);
+	return 0;
+}
+
+static int __init arm_pfdi_init(void)
+{
+	int ret;
+
+	ret = pfdi_validate_cpu_features();
+	if (ret)
+		return ret;
+
+	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "firmware/arm-pfdi:online",
+				arm_pfdi_device_create, arm_pfdi_device_destroy);
+	if (ret < 0)
+		return ret;
+
+	pfdi_cpuhp_state = ret;
+	return 0;
+}
+
+static void __exit arm_pfdi_exit(void)
+{
+	if (pfdi_cpuhp_state)
+		cpuhp_remove_state(pfdi_cpuhp_state);
+}
+
+module_init(arm_pfdi_init);
+module_exit(arm_pfdi_exit);
+
+MODULE_DESCRIPTION("Arm Platform Fault Detection Interface driver");
+MODULE_AUTHOR("Divin Raj <divin.raj@arm.com>");
+MODULE_LICENSE("GPL");

-- 
2.43.0


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

* [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support
  2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 2/4] firmware: arm: Add PFDI misc driver skeleton Ahmed Azeem
@ 2026-11-19 15:36 ` Ahmed Azeem
  2026-11-19 15:36 ` [PATCH RFC 4/4] firmware: arm: Harden PFDI worker and hotplug handling Ahmed Azeem
  3 siblings, 0 replies; 6+ messages in thread
From: Ahmed Azeem @ 2026-11-19 15:36 UTC (permalink / raw)
  To: linux-arm-kernel, Sudeep Holla
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Mark Rutland,
	Lorenzo Pieralisi, linux-api, linux-doc, linux-kernel, Divin Raj,
	Bertrand Marquis, Ahmed Azeem

From: Divin Raj <divin.raj@arm.com>

Extend the PFDI driver with ioctls to execute tests and manage results.

Add support for PFDI_IOCTL_PE_TEST_RUN, PFDI_IOCTL_PE_TEST_RESULT, and
PFDI_IOCTL_FORCE_ERROR, along with per-CPU state to serialize test
execution. When firmware reports PFDI_RET_FAULT_FOUND, return the
failing test identifier to userspace.

Signed-off-by: Divin Raj <divin.raj@arm.com>
Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
---
 drivers/firmware/arm_pfdi.c | 225 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 225 insertions(+)

diff --git a/drivers/firmware/arm_pfdi.c b/drivers/firmware/arm_pfdi.c
index 8c6d315b0572..22a69b12dbc8 100644
--- a/drivers/firmware/arm_pfdi.c
+++ b/drivers/firmware/arm_pfdi.c
@@ -18,10 +18,16 @@
 #include <linux/slab.h>
 #include <linux/smp.h>
 #include <linux/uaccess.h>
+#include <linux/wait.h>
 
 struct pfdi_per_cpu_info {
+	atomic_t run_inprogress;
 	/* Ensures a single userspace owner per CPU while the node is open. */
 	struct semaphore open_sem;
+	/* Serializes per-CPU run state and device availability. */
+	struct mutex state_lock;
+	struct pfdi_test_range ongoing_run_range;
+	wait_queue_head_t run_wait;
 };
 
 struct pfdi_dev_data {
@@ -36,6 +42,8 @@ struct pfdi_cpu_worker {
 	u32 feature_fid;
 	/* Protects worker lifetime and in-flight work state. */
 	struct mutex worker_lock;
+	struct pfdi_force_error error_info;
+	struct pfdi_test_run test_run;
 	struct arm_smccc_res smc_ret;
 	int result;
 };
@@ -59,6 +67,10 @@ static DEFINE_PER_CPU(struct pfdi_per_cpu_info, pfdi_cpu_info);
 
 static enum cpuhp_state pfdi_cpuhp_state;
 
+static unsigned int pfdi_wait_timeout_ms = 60;
+module_param_named(wait_timeout_ms, pfdi_wait_timeout_ms, uint, 0444);
+MODULE_PARM_DESC(wait_timeout_ms,
+		 "Maximum time in ms to wait for an in-progress PFDI run");
 static int pfdi_to_linux_errno(int err)
 {
 	switch (err) {
@@ -95,6 +107,18 @@ static int pfdi_smc_feature(u32 smc_fid)
 	return (int)pfdi_smc_call(PFDI_FN_FEATURES, smc_fid, 0, 0, 0).a0;
 }
 
+static bool pfdi_fid_supported(u32 smc_fid)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(pfdi_supported_fids); i++) {
+		if (pfdi_supported_fids[i] == smc_fid)
+			return true;
+	}
+
+	return false;
+}
+
 static int pfdi_validate_cpu_features(void)
 {
 	unsigned int i;
@@ -275,6 +299,172 @@ static int pfdi_pe_fw_check(unsigned int cpu)
 	return pfdi_to_linux_errno(per_cpu(pfdi_worker_ctx, cpu).result);
 }
 
+static void pfdi_pe_get_result_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_RESULT,
+					    0, 0, 0, 0);
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_get_result(unsigned int cpu, s64 *ft_id)
+{
+	int ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_get_result_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	ret = (int)worker_ctx->smc_ret.a0;
+	if (ret == PFDI_RET_FAULT_FOUND)
+		*ft_id = worker_ctx->smc_ret.a1;
+
+	return pfdi_to_linux_errno(ret);
+}
+
+static void pfdi_pe_force_error_fn(struct kthread_work *work)
+{
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	worker_ctx->result = pfdi_smc_call(PFDI_FN_FORCE_ERROR,
+					   worker_ctx->error_info.smc_fid,
+					   worker_ctx->error_info.error_id,
+					   0, 0).a0;
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_force_error(unsigned int cpu,
+			       const struct pfdi_force_error *error_info)
+{
+	int ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	if (!pfdi_fid_supported(error_info->smc_fid))
+		return -EINVAL;
+
+	if (error_info->error_id < PFDI_RET_TEST_COUNT_ZERO ||
+	    error_info->error_id > SMCCC_RET_SUCCESS)
+		return -EINVAL;
+
+	worker_ctx->error_info = *error_info;
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_force_error_fn,
+				 MAX_SCHEDULE_TIMEOUT);
+	if (ret)
+		return ret;
+
+	return pfdi_to_linux_errno(worker_ctx->result);
+}
+
+static bool pfdi_ranges_equal(const struct pfdi_test_range *lhs,
+			      const struct pfdi_test_range *rhs)
+{
+	return lhs->start == rhs->start && lhs->end == rhs->end;
+}
+
+static bool pfdi_run_available(const struct pfdi_per_cpu_info *cpu_info)
+{
+	return !atomic_read(&cpu_info->run_inprogress);
+}
+
+static int pfdi_validate_test_range(unsigned int cpu,
+				    const struct pfdi_test_range *range)
+{
+	s64 count;
+	int ret;
+
+	if (range->start == -1 || range->end == -1)
+		return pfdi_ranges_equal(range,
+					 &(struct pfdi_test_range){ .start = -1,
+								    .end = -1 }) ?
+		       0 : -EINVAL;
+
+	if (range->start < 0 || range->end < 0 || range->start > range->end)
+		return -EINVAL;
+
+	ret = pfdi_pe_get_test_count(cpu, &count);
+	if (ret)
+		return ret;
+
+	if (range->start >= count || range->end >= count)
+		return -EINVAL;
+
+	return 0;
+}
+
+static void pfdi_pe_run_fn(struct kthread_work *work)
+{
+	struct arm_smccc_res smc_ret;
+	struct pfdi_cpu_worker *worker_ctx;
+
+	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
+	smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_RUN,
+				worker_ctx->test_run.range.start,
+				worker_ctx->test_run.range.end, 0, 0);
+
+	if ((int)smc_ret.a0 == PFDI_RET_FAULT_FOUND)
+		worker_ctx->test_run.ft_id = smc_ret.a1;
+
+	worker_ctx->result = (int)smc_ret.a0;
+	complete(&worker_ctx->done);
+}
+
+static int pfdi_pe_run_tests(unsigned int cpu, struct pfdi_test_run *test_run)
+{
+	int ret;
+	unsigned long wait_timeout;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, cpu);
+
+	ret = pfdi_validate_test_range(cpu, &test_run->range);
+	if (ret)
+		return ret;
+
+	wait_timeout = msecs_to_jiffies(pfdi_wait_timeout_ms);
+
+	mutex_lock(&cpu_info->state_lock);
+	while (atomic_read(&cpu_info->run_inprogress)) {
+		if (pfdi_ranges_equal(&test_run->range,
+				      &cpu_info->ongoing_run_range)) {
+			mutex_unlock(&cpu_info->state_lock);
+			return -EBUSY;
+		}
+
+		mutex_unlock(&cpu_info->state_lock);
+			ret = wait_event_interruptible_timeout(cpu_info->run_wait,
+							       pfdi_run_available(cpu_info),
+							       wait_timeout);
+		if (ret <= 0)
+			return ret ? ret : -ETIME;
+
+		mutex_lock(&cpu_info->state_lock);
+	}
+
+	atomic_set(&cpu_info->run_inprogress, 1);
+	cpu_info->ongoing_run_range = test_run->range;
+	worker_ctx->test_run = *test_run;
+	mutex_unlock(&cpu_info->state_lock);
+
+	ret = pfdi_thread_worker(cpu, pfdi_pe_run_fn, wait_timeout);
+
+	mutex_lock(&cpu_info->state_lock);
+	if (worker_ctx->result == PFDI_RET_FAULT_FOUND)
+		test_run->ft_id = worker_ctx->test_run.ft_id;
+	atomic_set(&cpu_info->run_inprogress, 0);
+	wake_up_all(&cpu_info->run_wait);
+	mutex_unlock(&cpu_info->state_lock);
+
+	if (ret)
+		return ret;
+
+	return pfdi_to_linux_errno(worker_ctx->result);
+}
+
 static int arm_pfdi_open(struct inode *inode, struct file *file)
 {
 	struct miscdevice *misc_dev = file->private_data;
@@ -310,9 +500,12 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
 	u32 feature_fid;
 	u32 version;
 	s64 count;
+	s64 ft_id;
 	u64 lib_info;
 	struct miscdevice *misc_dev = file->private_data;
 	struct pfdi_dev_data *device_data;
+	struct pfdi_test_run test_run;
+	struct pfdi_force_error error_info;
 	unsigned int cpu;
 
 	if (!misc_dev || !misc_dev->this_device)
@@ -350,9 +543,35 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
 					 sizeof(count)))
 			err = -EFAULT;
 		break;
+	case PFDI_IOCTL_PE_TEST_RUN:
+		if (copy_from_user(&test_run, (void __user *)arg,
+				   sizeof(test_run)))
+			return -EFAULT;
+
+		err = pfdi_pe_run_tests(cpu, &test_run);
+		if (err == -EFAULT &&
+		    copy_to_user((void __user *)arg, &test_run,
+				 sizeof(test_run)))
+			err = -EFAULT;
+		break;
+	case PFDI_IOCTL_PE_TEST_RESULT:
+		err = pfdi_pe_get_result(cpu, &ft_id);
+		if (err == -EFAULT && copy_to_user((void __user *)arg, &ft_id,
+						   sizeof(ft_id)))
+			err = -EFAULT;
+		break;
 	case PFDI_IOCTL_FW_CHECK:
 		err = pfdi_pe_fw_check(cpu);
 		break;
+	case PFDI_IOCTL_FORCE_ERROR:
+		if (copy_from_user(&error_info, (void __user *)arg,
+				   sizeof(error_info)))
+			return -EFAULT;
+		if (error_info.reserved)
+			return -EINVAL;
+
+		err = pfdi_pe_force_error(cpu, &error_info);
+		break;
 	default:
 		err = -ENOTTY;
 		break;
@@ -413,6 +632,9 @@ static int arm_pfdi_device_create(unsigned int cpu)
 
 	sema_init(&per_cpu(pfdi_cpu_info, cpu).open_sem, 1);
 	mutex_init(&worker_ctx->worker_lock);
+	atomic_set(&per_cpu(pfdi_cpu_info, cpu).run_inprogress, 0);
+	mutex_init(&per_cpu(pfdi_cpu_info, cpu).state_lock);
+	init_waitqueue_head(&per_cpu(pfdi_cpu_info, cpu).run_wait);
 	init_completion(&worker_ctx->done);
 	per_cpu(pfdi_worker, cpu) =
 		kthread_create_worker_on_cpu(cpu, 0, "pfdi/%u");
@@ -447,6 +669,9 @@ static int arm_pfdi_device_destroy(unsigned int cpu)
 {
 	struct miscdevice *misc_dev = &per_cpu(pfdi_misc_device, cpu);
 
+	if (atomic_read(&per_cpu(pfdi_cpu_info, cpu).run_inprogress) > 0)
+		return -EBUSY;
+
 	if (per_cpu(pfdi_worker, cpu)) {
 		kthread_flush_worker(per_cpu(pfdi_worker, cpu));
 		kthread_destroy_worker(per_cpu(pfdi_worker, cpu));

-- 
2.43.0


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

* [PATCH RFC 4/4] firmware: arm: Harden PFDI worker and hotplug handling
  2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
                   ` (2 preceding siblings ...)
  2026-11-19 15:36 ` [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support Ahmed Azeem
@ 2026-11-19 15:36 ` Ahmed Azeem
  3 siblings, 0 replies; 6+ messages in thread
From: Ahmed Azeem @ 2026-11-19 15:36 UTC (permalink / raw)
  To: linux-arm-kernel, Sudeep Holla
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Mark Rutland,
	Lorenzo Pieralisi, linux-api, linux-doc, linux-kernel, Divin Raj,
	Bertrand Marquis, Ahmed Azeem

From: Divin Raj <divin.raj@arm.com>

Tighten lifetime rules for the per-CPU worker and misc devices.

Do not reuse worker state after a timeout, as the SMC may still be
executing in firmware after wait_for_completion_timeout() expires.
Track in-flight work explicitly, prevent reuse until completion, and
reject teardown while work is active.

Gate open() against CPU teardown, preserve the full 64-bit failing test
identifier returned by firmware, and ensure device removal does not race
with open file descriptors or in-progress requests.

Signed-off-by: Divin Raj <divin.raj@arm.com>
Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
---
 drivers/firmware/arm_pfdi.c | 406 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 320 insertions(+), 86 deletions(-)

diff --git a/drivers/firmware/arm_pfdi.c b/drivers/firmware/arm_pfdi.c
index 22a69b12dbc8..34ccbb7724e6 100644
--- a/drivers/firmware/arm_pfdi.c
+++ b/drivers/firmware/arm_pfdi.c
@@ -24,10 +24,13 @@ struct pfdi_per_cpu_info {
 	atomic_t run_inprogress;
 	/* Ensures a single userspace owner per CPU while the node is open. */
 	struct semaphore open_sem;
+	/* Bumped on each device recreation so stale file contexts can be rejected. */
+	unsigned int generation;
 	/* Serializes per-CPU run state and device availability. */
 	struct mutex state_lock;
 	struct pfdi_test_range ongoing_run_range;
 	wait_queue_head_t run_wait;
+	bool online;
 };
 
 struct pfdi_dev_data {
@@ -36,9 +39,17 @@ struct pfdi_dev_data {
 	char nodename[24];
 };
 
+struct pfdi_file_ctx {
+	unsigned int cpu;
+	/* Snapshot of cpu_info->generation taken when the file was opened. */
+	unsigned int generation;
+};
+
 struct pfdi_cpu_worker {
 	struct kthread_work work;
 	struct completion done;
+	/* Serializes request staging and result extraction around the worker. */
+	struct mutex op_lock;
 	u32 feature_fid;
 	/* Protects worker lifetime and in-flight work state. */
 	struct mutex worker_lock;
@@ -46,6 +57,9 @@ struct pfdi_cpu_worker {
 	struct pfdi_test_run test_run;
 	struct arm_smccc_res smc_ret;
 	int result;
+	bool work_inflight;
+	bool shutting_down;
+	wait_queue_head_t worker_wait;
 };
 
 static const u32 pfdi_supported_fids[] = {
@@ -141,32 +155,80 @@ static int pfdi_validate_cpu_features(void)
 	return 0;
 }
 
-static int pfdi_thread_worker(unsigned int cpu,
-			      void (*handler)(struct kthread_work *work),
-			      unsigned long timeout)
+static int pfdi_reserve_worker(unsigned int cpu, unsigned long timeout)
 {
 	int ret = 0;
 	struct kthread_worker *worker;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
+retry:
 	mutex_lock(&worker_ctx->worker_lock);
-	worker = per_cpu(pfdi_worker, cpu);
-	if (!worker) {
+	worker = READ_ONCE(per_cpu(pfdi_worker, cpu));
+	if (!worker || worker_ctx->shutting_down) {
 		mutex_unlock(&worker_ctx->worker_lock);
 		return -ENODEV;
 	}
 
+	if (worker_ctx->work_inflight) {
+		mutex_unlock(&worker_ctx->worker_lock);
+		ret =
+			wait_event_interruptible_timeout(worker_ctx->worker_wait,
+							 !READ_ONCE(worker_ctx->work_inflight) ||
+							 READ_ONCE(worker_ctx->shutting_down),
+							 timeout);
+		if (ret < 0)
+			return ret;
+		if (!ret)
+			return -ETIME;
+		goto retry;
+	}
+
 	reinit_completion(&worker_ctx->done);
+	WRITE_ONCE(worker_ctx->work_inflight, true);
+	mutex_unlock(&worker_ctx->worker_lock);
+
+	return 0;
+}
+
+static int pfdi_queue_worker(unsigned int cpu,
+			     void (*handler)(struct kthread_work *work),
+			     unsigned long timeout)
+{
+	struct kthread_worker *worker;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+	mutex_lock(&worker_ctx->worker_lock);
+	worker = READ_ONCE(per_cpu(pfdi_worker, cpu));
+	if (!worker || worker_ctx->shutting_down || !worker_ctx->work_inflight) {
+		mutex_unlock(&worker_ctx->worker_lock);
+		return -ENODEV;
+	}
+
 	kthread_init_work(&worker_ctx->work, handler);
 	kthread_queue_work(worker, &worker_ctx->work);
+	mutex_unlock(&worker_ctx->worker_lock);
 
-	if (!wait_for_completion_timeout(&worker_ctx->done, timeout)) {
-		ret = -ETIME;
-		kthread_cancel_work_sync(&worker_ctx->work);
-	}
+	if (!wait_for_completion_timeout(&worker_ctx->done, timeout))
+		return -ETIME;
+
+	return 0;
+}
 
+static void pfdi_complete_worker(struct pfdi_cpu_worker *worker_ctx)
+{
+	/*
+	 * Signal the current request before making the worker available again.
+	 * Otherwise a timed-out caller can release op_lock, a new caller can
+	 * reuse the shared completion, and this completion can satisfy the new
+	 * request before its work has run.
+	 */
+	complete(&worker_ctx->done);
+
+	mutex_lock(&worker_ctx->worker_lock);
+	WRITE_ONCE(worker_ctx->work_inflight, false);
 	mutex_unlock(&worker_ctx->worker_lock);
-	return ret;
+
+	wake_up(&worker_ctx->worker_wait);
 }
 
 static void pfdi_pe_get_test_id_fn(struct kthread_work *work)
@@ -175,7 +237,7 @@ static void pfdi_pe_get_test_id_fn(struct kthread_work *work)
 
 	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
 	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_ID, 0, 0, 0, 0);
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static void pfdi_pe_get_version_fn(struct kthread_work *work)
@@ -184,7 +246,7 @@ static void pfdi_pe_get_version_fn(struct kthread_work *work)
 
 	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
 	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_VERSION, 0, 0, 0, 0);
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_get_version(unsigned int cpu, u32 *version)
@@ -192,14 +254,24 @@ static int pfdi_pe_get_version(unsigned int cpu, u32 *version)
 	s64 ret;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_get_version_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
 
-	ret = (int)worker_ctx->smc_ret.a0;
+	ret = pfdi_queue_worker(cpu, pfdi_pe_get_version_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
+
+	ret = (s64)worker_ctx->smc_ret.a0;
 	if (ret >= 0)
 		*version = (u32)ret;
+	mutex_unlock(&worker_ctx->op_lock);
 
 	if (ret >= 0)
 		return 0;
@@ -215,7 +287,7 @@ static void pfdi_pe_get_features_fn(struct kthread_work *work)
 	worker_ctx->result = pfdi_smc_call(PFDI_FN_FEATURES,
 					   worker_ctx->feature_fid,
 					   0, 0, 0).a0;
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_get_features(unsigned int cpu, u32 fid)
@@ -223,13 +295,25 @@ static int pfdi_pe_get_features(unsigned int cpu, u32 fid)
 	int ret;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
+
 	worker_ctx->feature_fid = fid;
-	ret = pfdi_thread_worker(cpu, pfdi_pe_get_features_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	ret = pfdi_queue_worker(cpu, pfdi_pe_get_features_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
 
-	return pfdi_to_linux_errno(worker_ctx->result);
+	ret = worker_ctx->result;
+	mutex_unlock(&worker_ctx->op_lock);
+
+	return pfdi_to_linux_errno(ret);
 }
 
 static int pfdi_pe_get_test_id(unsigned int cpu, u64 *lib_info)
@@ -237,14 +321,24 @@ static int pfdi_pe_get_test_id(unsigned int cpu, u64 *lib_info)
 	int ret;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_get_test_id_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
+
+	ret = pfdi_queue_worker(cpu, pfdi_pe_get_test_id_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
 
 	ret = (int)worker_ctx->smc_ret.a0;
 	if (!ret)
 		*lib_info = worker_ctx->smc_ret.a1;
+	mutex_unlock(&worker_ctx->op_lock);
 
 	return pfdi_to_linux_errno(ret);
 }
@@ -256,7 +350,7 @@ static void pfdi_pe_get_test_count_fn(struct kthread_work *work)
 	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
 	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_PART_COUNT,
 					    0, 0, 0, 0);
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_get_test_count(unsigned int cpu, s64 *count)
@@ -264,12 +358,22 @@ static int pfdi_pe_get_test_count(unsigned int cpu, s64 *count)
 	s64 ret;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_get_test_count_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
+
+	ret = pfdi_queue_worker(cpu, pfdi_pe_get_test_count_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
 
 	ret = (s64)worker_ctx->smc_ret.a0;
+	mutex_unlock(&worker_ctx->op_lock);
 	if (ret >= 0) {
 		*count = ret;
 		return 0;
@@ -285,18 +389,32 @@ static void pfdi_pe_fw_check_fn(struct kthread_work *work)
 	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
 	worker_ctx->result = pfdi_smc_call(PFDI_FN_FW_CHECK,
 					   0, 0, 0, 0).a0;
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_fw_check(unsigned int cpu)
 {
 	int ret;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_fw_check_fn, MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
+
+	ret = pfdi_queue_worker(cpu, pfdi_pe_fw_check_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
 
-	return pfdi_to_linux_errno(per_cpu(pfdi_worker_ctx, cpu).result);
+	ret = worker_ctx->result;
+	mutex_unlock(&worker_ctx->op_lock);
+
+	return pfdi_to_linux_errno(ret);
 }
 
 static void pfdi_pe_get_result_fn(struct kthread_work *work)
@@ -306,7 +424,7 @@ static void pfdi_pe_get_result_fn(struct kthread_work *work)
 	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
 	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_RESULT,
 					    0, 0, 0, 0);
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_get_result(unsigned int cpu, s64 *ft_id)
@@ -314,14 +432,24 @@ static int pfdi_pe_get_result(unsigned int cpu, s64 *ft_id)
 	int ret;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_get_result_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
+
+	ret = pfdi_queue_worker(cpu, pfdi_pe_get_result_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
 
 	ret = (int)worker_ctx->smc_ret.a0;
 	if (ret == PFDI_RET_FAULT_FOUND)
 		*ft_id = worker_ctx->smc_ret.a1;
+	mutex_unlock(&worker_ctx->op_lock);
 
 	return pfdi_to_linux_errno(ret);
 }
@@ -335,7 +463,7 @@ static void pfdi_pe_force_error_fn(struct kthread_work *work)
 					   worker_ctx->error_info.smc_fid,
 					   worker_ctx->error_info.error_id,
 					   0, 0).a0;
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_force_error(unsigned int cpu,
@@ -351,14 +479,26 @@ static int pfdi_pe_force_error(unsigned int cpu,
 	    error_info->error_id > SMCCC_RET_SUCCESS)
 		return -EINVAL;
 
+	mutex_lock(&worker_ctx->op_lock);
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
+		return ret;
+	}
+
 	worker_ctx->error_info = *error_info;
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_force_error_fn,
-				 MAX_SCHEDULE_TIMEOUT);
-	if (ret)
+	ret = pfdi_queue_worker(cpu, pfdi_pe_force_error_fn,
+				MAX_SCHEDULE_TIMEOUT);
+	if (ret) {
+		mutex_unlock(&worker_ctx->op_lock);
 		return ret;
+	}
 
-	return pfdi_to_linux_errno(worker_ctx->result);
+	ret = worker_ctx->result;
+	mutex_unlock(&worker_ctx->op_lock);
+
+	return pfdi_to_linux_errno(ret);
 }
 
 static bool pfdi_ranges_equal(const struct pfdi_test_range *lhs,
@@ -397,6 +537,18 @@ static int pfdi_validate_test_range(unsigned int cpu,
 	return 0;
 }
 
+static bool pfdi_file_ctx_valid(const struct pfdi_file_ctx *ctx)
+{
+	bool valid;
+	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, ctx->cpu);
+
+	mutex_lock(&cpu_info->state_lock);
+	valid = cpu_info->online && ctx->generation == cpu_info->generation;
+	mutex_unlock(&cpu_info->state_lock);
+
+	return valid;
+}
+
 static void pfdi_pe_run_fn(struct kthread_work *work)
 {
 	struct arm_smccc_res smc_ret;
@@ -411,12 +563,12 @@ static void pfdi_pe_run_fn(struct kthread_work *work)
 		worker_ctx->test_run.ft_id = smc_ret.a1;
 
 	worker_ctx->result = (int)smc_ret.a0;
-	complete(&worker_ctx->done);
+	pfdi_complete_worker(worker_ctx);
 }
 
 static int pfdi_pe_run_tests(unsigned int cpu, struct pfdi_test_run *test_run)
 {
-	int ret;
+	int ret, result;
 	unsigned long wait_timeout;
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, cpu);
@@ -447,14 +599,26 @@ static int pfdi_pe_run_tests(unsigned int cpu, struct pfdi_test_run *test_run)
 
 	atomic_set(&cpu_info->run_inprogress, 1);
 	cpu_info->ongoing_run_range = test_run->range;
-	worker_ctx->test_run = *test_run;
+	mutex_lock(&worker_ctx->op_lock);
 	mutex_unlock(&cpu_info->state_lock);
 
-	ret = pfdi_thread_worker(cpu, pfdi_pe_run_fn, wait_timeout);
+	/*
+	 * wait_timeout_ms limits how long a caller waits behind another run.
+	 * Once this run is selected to execute, wait for the worker to really
+	 * become available and for the firmware operation to complete.
+	 */
+	ret = pfdi_reserve_worker(cpu, MAX_SCHEDULE_TIMEOUT);
+	if (!ret) {
+		worker_ctx->test_run = *test_run;
+		ret = pfdi_queue_worker(cpu, pfdi_pe_run_fn, MAX_SCHEDULE_TIMEOUT);
+	}
 
 	mutex_lock(&cpu_info->state_lock);
-	if (worker_ctx->result == PFDI_RET_FAULT_FOUND)
+	if (!ret && worker_ctx->result == PFDI_RET_FAULT_FOUND)
 		test_run->ft_id = worker_ctx->test_run.ft_id;
+	if (!ret)
+		result = worker_ctx->result;
+	mutex_unlock(&worker_ctx->op_lock);
 	atomic_set(&cpu_info->run_inprogress, 0);
 	wake_up_all(&cpu_info->run_wait);
 	mutex_unlock(&cpu_info->state_lock);
@@ -462,14 +626,16 @@ static int pfdi_pe_run_tests(unsigned int cpu, struct pfdi_test_run *test_run)
 	if (ret)
 		return ret;
 
-	return pfdi_to_linux_errno(worker_ctx->result);
+	return pfdi_to_linux_errno(result);
 }
 
 static int arm_pfdi_open(struct inode *inode, struct file *file)
 {
 	struct miscdevice *misc_dev = file->private_data;
 	struct pfdi_dev_data *data;
-	unsigned int cpu;
+	struct pfdi_per_cpu_info *cpu_info;
+	struct pfdi_file_ctx *ctx;
+	unsigned int cpu, generation;
 
 	if (!misc_dev || !misc_dev->this_device)
 		return -ENODEV;
@@ -478,18 +644,38 @@ static int arm_pfdi_open(struct inode *inode, struct file *file)
 	if (!data)
 		return -ENODEV;
 
+	ctx = kzalloc_obj(*ctx, GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+
 	cpu = get_cpu();
 	if (data->cpu_id != cpu) {
 		put_cpu();
+		kfree(ctx);
 		return -EPERM;
 	}
+	cpu_info = &per_cpu(pfdi_cpu_info, cpu);
+	put_cpu();
 
-	if (down_trylock(&per_cpu(pfdi_cpu_info, cpu).open_sem)) {
-		put_cpu();
+	mutex_lock(&cpu_info->state_lock);
+	if (!cpu_info->online) {
+		mutex_unlock(&cpu_info->state_lock);
+		kfree(ctx);
+		return -ENODEV;
+	}
+
+	if (down_trylock(&cpu_info->open_sem)) {
+		mutex_unlock(&cpu_info->state_lock);
+		kfree(ctx);
 		return -EBUSY;
 	}
+	generation = cpu_info->generation;
 
-	put_cpu();
+	mutex_unlock(&cpu_info->state_lock);
+
+	ctx->cpu = cpu;
+	ctx->generation = generation;
+	file->private_data = ctx;
 	return 0;
 }
 
@@ -502,21 +688,18 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
 	s64 count;
 	s64 ft_id;
 	u64 lib_info;
-	struct miscdevice *misc_dev = file->private_data;
-	struct pfdi_dev_data *device_data;
 	struct pfdi_test_run test_run;
+	struct pfdi_file_ctx *ctx = file->private_data;
 	struct pfdi_force_error error_info;
 	unsigned int cpu;
 
-	if (!misc_dev || !misc_dev->this_device)
+	if (!ctx)
 		return -ENODEV;
 
-	device_data = dev_get_drvdata(misc_dev->this_device);
-	if (!device_data)
+	cpu = ctx->cpu;
+	if (!pfdi_file_ctx_valid(ctx))
 		return -ENODEV;
 
-	cpu = device_data->cpu_id;
-
 	switch (cmd) {
 	case PFDI_IOCTL_VERSION:
 		err = pfdi_pe_get_version(cpu, &version);
@@ -582,16 +765,18 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
 
 static int arm_pfdi_release(struct inode *inode, struct file *file)
 {
-	struct miscdevice *misc_dev = file->private_data;
-	struct pfdi_dev_data *data;
-	unsigned int cpu;
+	struct pfdi_file_ctx *ctx = file->private_data;
+	struct pfdi_per_cpu_info *cpu_info;
 
-	if (!misc_dev || !misc_dev->this_device)
+	if (!ctx)
 		return 0;
 
-	data = dev_get_drvdata(misc_dev->this_device);
-	cpu = data ? data->cpu_id : raw_smp_processor_id();
-	up(&per_cpu(pfdi_cpu_info, cpu).open_sem);
+	cpu_info = &per_cpu(pfdi_cpu_info, ctx->cpu);
+	mutex_lock(&cpu_info->state_lock);
+	if (cpu_info->online && ctx->generation == cpu_info->generation)
+		up(&cpu_info->open_sem);
+	mutex_unlock(&cpu_info->state_lock);
+	kfree(ctx);
 
 	return 0;
 }
@@ -625,22 +810,31 @@ static int arm_pfdi_device_create(unsigned int cpu)
 	int ret;
 	struct miscdevice *misc_dev = &per_cpu(pfdi_misc_device, cpu);
 	struct pfdi_dev_data *dev_data = &per_cpu(pfdi_device_data, cpu);
+	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, cpu);
 	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
 
 	memset(misc_dev, 0, sizeof(*misc_dev));
 	arm_pfdi_device_init(misc_dev, dev_data, cpu);
 
-	sema_init(&per_cpu(pfdi_cpu_info, cpu).open_sem, 1);
-	mutex_init(&worker_ctx->worker_lock);
-	atomic_set(&per_cpu(pfdi_cpu_info, cpu).run_inprogress, 0);
-	mutex_init(&per_cpu(pfdi_cpu_info, cpu).state_lock);
-	init_waitqueue_head(&per_cpu(pfdi_cpu_info, cpu).run_wait);
-	init_completion(&worker_ctx->done);
-	per_cpu(pfdi_worker, cpu) =
-		kthread_create_worker_on_cpu(cpu, 0, "pfdi/%u");
-	if (IS_ERR(per_cpu(pfdi_worker, cpu))) {
-		ret = PTR_ERR(per_cpu(pfdi_worker, cpu));
-		per_cpu(pfdi_worker, cpu) = NULL;
+	ret = pfdi_validate_cpu_features();
+	if (ret)
+		return ret;
+
+	mutex_lock(&cpu_info->state_lock);
+	atomic_set(&cpu_info->run_inprogress, 0);
+	cpu_info->online = false;
+	cpu_info->generation++;
+	mutex_unlock(&cpu_info->state_lock);
+
+	mutex_lock(&worker_ctx->worker_lock);
+	WRITE_ONCE(worker_ctx->work_inflight, false);
+	WRITE_ONCE(worker_ctx->shutting_down, false);
+	mutex_unlock(&worker_ctx->worker_lock);
+	WRITE_ONCE(per_cpu(pfdi_worker, cpu),
+		   kthread_create_worker_on_cpu(cpu, 0, "pfdi/%u"));
+	if (IS_ERR(READ_ONCE(per_cpu(pfdi_worker, cpu)))) {
+		ret = PTR_ERR(READ_ONCE(per_cpu(pfdi_worker, cpu)));
+		WRITE_ONCE(per_cpu(pfdi_worker, cpu), NULL);
 		return ret;
 	}
 
@@ -654,6 +848,9 @@ static int arm_pfdi_device_create(unsigned int cpu)
 	}
 
 	dev_set_drvdata(misc_dev->this_device, dev_data);
+	mutex_lock(&cpu_info->state_lock);
+	cpu_info->online = true;
+	mutex_unlock(&cpu_info->state_lock);
 
 	return 0;
 
@@ -668,28 +865,65 @@ static int arm_pfdi_device_create(unsigned int cpu)
 static int arm_pfdi_device_destroy(unsigned int cpu)
 {
 	struct miscdevice *misc_dev = &per_cpu(pfdi_misc_device, cpu);
+	struct kthread_worker *worker;
+	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, cpu);
+
+	mutex_lock(&cpu_info->state_lock);
+	if (atomic_read(&cpu_info->run_inprogress) > 0) {
+		mutex_unlock(&cpu_info->state_lock);
+		return -EBUSY;
+	}
+	if (down_trylock(&cpu_info->open_sem)) {
+		mutex_unlock(&cpu_info->state_lock);
+		return -EBUSY;
+	}
+	cpu_info->online = false;
+	mutex_unlock(&cpu_info->state_lock);
 
-	if (atomic_read(&per_cpu(pfdi_cpu_info, cpu).run_inprogress) > 0)
+	mutex_lock(&worker_ctx->worker_lock);
+	if (worker_ctx->work_inflight) {
+		mutex_unlock(&worker_ctx->worker_lock);
+		mutex_lock(&cpu_info->state_lock);
+		cpu_info->online = true;
+		mutex_unlock(&cpu_info->state_lock);
+		up(&cpu_info->open_sem);
 		return -EBUSY;
+	}
+	WRITE_ONCE(worker_ctx->shutting_down, true);
+	mutex_unlock(&worker_ctx->worker_lock);
 
-	if (per_cpu(pfdi_worker, cpu)) {
-		kthread_flush_worker(per_cpu(pfdi_worker, cpu));
-		kthread_destroy_worker(per_cpu(pfdi_worker, cpu));
-		per_cpu(pfdi_worker, cpu) = NULL;
+	worker = READ_ONCE(per_cpu(pfdi_worker, cpu));
+	if (worker) {
+		kthread_flush_worker(worker);
+		kthread_destroy_worker(worker);
+		WRITE_ONCE(per_cpu(pfdi_worker, cpu), NULL);
 	}
 
-	dev_set_drvdata(misc_dev->this_device, NULL);
 	misc_deregister(misc_dev);
+	up(&cpu_info->open_sem);
 	return 0;
 }
 
 static int __init arm_pfdi_init(void)
 {
+	unsigned int cpu;
 	int ret;
-
-	ret = pfdi_validate_cpu_features();
-	if (ret)
-		return ret;
+	struct pfdi_cpu_worker *worker_ctx;
+	struct pfdi_per_cpu_info *cpu_info;
+
+	for_each_possible_cpu(cpu) {
+		cpu_info = &per_cpu(pfdi_cpu_info, cpu);
+		worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
+
+		mutex_init(&cpu_info->state_lock);
+		sema_init(&cpu_info->open_sem, 1);
+		init_waitqueue_head(&cpu_info->run_wait);
+		mutex_init(&worker_ctx->op_lock);
+		mutex_init(&worker_ctx->worker_lock);
+		init_completion(&worker_ctx->done);
+		init_waitqueue_head(&worker_ctx->worker_wait);
+	}
 
 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "firmware/arm-pfdi:online",
 				arm_pfdi_device_create, arm_pfdi_device_destroy);

-- 
2.43.0


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

* Re: [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI
  2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
@ 2026-11-19 21:35   ` Jonathan Cameron
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Cameron @ 2026-11-19 21:35 UTC (permalink / raw)
  To: Ahmed Azeem
  Cc: linux-arm-kernel, Sudeep Holla, Jonathan Corbet, Shuah Khan,
	Randy Dunlap, Mark Rutland, Lorenzo Pieralisi, linux-api,
	linux-doc, linux-kernel, Divin Raj, Bertrand Marquis

On Thu, 19 Nov 2026 15:36:03 +0000
Ahmed Azeem <ahmed.azeem@arm.com> wrote:

> From: Divin Raj <divin.raj@arm.com>
> 
> Define the userspace ABI for the Arm Platform Fault Detection
> Interface (PFDI).
> 
> Subsequent commits introduce a driver that exposes a per-CPU misc
> device. The ioctl definitions, data structures, and return codes
> must therefore be shared with userspace. Add these definitions to
> a UAPI header and document the ABI for /dev/cpu/<cpu>/pfdi.
> 
> Placing the interface under include/uapi ensures availability via
> headers_install and clearly identifies it as a userspace contract.
> 

This is very much an initial look as until now I wasn't even aware
a specification for this existed. This isn't the first such
similar system, and there are likely to be more, so definitely 
worth thinking about generalization at least at the level
of userspace interface. Bit tricky to generalize SMC calls if they
aren't wrapped in anything.

> Link: https://developer.arm.com/documentation/110468/1-0alp0
> 

No blank lines in tag blocks.

> Signed-off-by: Divin Raj <divin.raj@arm.com>
> Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
> ---
>  Documentation/ABI/testing/dev-cpu-pfdi             | 103 +++++++++++++++++++++
>  Documentation/userspace-api/ioctl/ioctl-number.rst |   1 +
>  MAINTAINERS                                        |   7 ++
>  include/uapi/linux/pfdi.h                          |  89 ++++++++++++++++++
>  4 files changed, 200 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/dev-cpu-pfdi b/Documentation/ABI/testing/dev-cpu-pfdi
> new file mode 100644
> index 000000000000..e07ed6baa7f8
> --- /dev/null
> +++ b/Documentation/ABI/testing/dev-cpu-pfdi
> @@ -0,0 +1,103 @@
> +What:		/dev/cpu/<cpu>/pfdi
> +Date:		May 2026
> +KernelVersion:	6.18

Time travel.

> +Contact:	Linux ARM Kernel Mailing list <linux-arm-kernel@lists.infradead.org>
> +Reference:	Arm Platform Fault Detection Interface, document 110468,
> +		version 1.0 ALP0
> +Description:	Per-CPU Arm Platform Fault Detection Interface (PFDI) misc
> +		device nodes. Each node targets the CPU encoded in its

Trivial but wrap docs at 80 chars.

> +		path and exposes the PFDI service implemented by firmware.
> +		PFDI uses the Arm SMCCC SMC64 calling convention and is
> +		PE-local. Userspace is therefore expected to bind itself to
> +		the target CPU before opening or issuing ioctls on the
> +		matching device node. Each node is single-open: only one
> +		userspace owner at a time may hold /dev/cpu/<cpu>/pfdi for a
> +		given CPU. Additional open attempts fail with -EBUSY while the
> +		device is already open.
> +
> +		The ABI is defined in <linux/pfdi.h>. The following ioctls
> +		are supported:
> +
> +		* PFDI_IOCTL_VERSION
> +		  Read the PFDI version for the target CPU. On success the
> +		  ioctl returns 0 and copies a 32-bit version value to
> +		  userspace with the following layout:
> +		  Bits[30:16] PFDI major version
> +		  Bits[15:0] PFDI minor version
> +
> +		* PFDI_IOCTL_FEATURES
> +		  Query whether a PFDI function ID is supported on the
> +		  target CPU. Userspace passes a 32-bit PFDI function ID.
> +		  The ioctl returns 0 when the function is supported,
> +		  and -EOPNOTSUPP when it is not supported on the target
> +		  CPU, reserved by the specification, or otherwise not
> +		  recognized by firmware.
> +
> +		* PFDI_IOCTL_PE_TEST_ID
> +		  Read the test engine identifier for the target CPU.
> +		  On success the ioctl returns 0 and copies a 64-bit value to
> +		  userspace with the following layout:
> +		  Bits[63:32] reserved and zero
> +		  Bits[31:24] vendor ID
> +		  Bits[23:20] reserved and zero
> +		  Bits[19:16] vendor-defined implementation ID
> +		  Bits[15:8] major version
> +		  Bits[7:0] minor version
> +
> +		* PFDI_IOCTL_PE_TEST_PART_COUNT
> +		  Read the number of available test parts for the target

The naming 'test_part' is a little confusing given it could be interpreted
as thing to test, rather than part of a test.  I'd consider a rename.

> +		  CPU. On success the ioctl returns 0 and copies a signed
> +		  64-bit count to userspace. The smallest schedulable unit is
> +		  a test part, and valid test part indices are in the range 0
> +		  to count - 1.
> +
> +		* PFDI_IOCTL_PE_TEST_RUN
> +		  Run a requested inclusive test part range on the target
> +		  CPU. struct pfdi_test_run.range.start and .end are signed
> +		  64-bit values.
> +		  Passing start = -1 and end = -1 requests execution of all
> +		  available test parts on the target CPU. Any mixed use of
> +		  -1, reversed ranges, out-of-range indices, or non-zero
> +		  reserved arguments is rejected with -EINVAL, either by the
> +		  kernel before the request is issued or by firmware.
> +		  If firmware reports PFDI_RET_FAULT_FOUND, the ioctl returns
> +		  -EFAULT and copies a signed 64-bit failing test part
> +		  identifier to struct pfdi_test_run.ft_id. If the failing
> +		  part cannot be identified, ft_id is set to
> +		  PFDI_RET_UNKNOWN.
> +
> +		* PFDI_IOCTL_PE_TEST_RESULT
> +		  Fetch the stored out-of-reset test result for the target CPU.
> +		  If firmware reports PFDI_RET_FAULT_FOUND, the ioctl returns
> +		  -EFAULT and copies a signed 64-bit failing test part
> +		  identifier to userspace. If the failing part cannot be
> +		  identified, the copied value is PFDI_RET_UNKNOWN. If no
> +		  power-on tests were executed on the target CPU, the ioctl
> +		  returns -EAGAIN.

I get that the intent is that TEST_RUN is the runtime path and this is
out of boot.  However naming would make people expect this to return
something after a runtime test as well.  Maybe rename?

> +
> +		* PFDI_IOCTL_FW_CHECK
> +		  Ask firmware to execute platform firmware resident software
> +		  integrity checks. The ioctl returns 0 when no fault is
> +		  detected. It returns -EFAULT when firmware reports a fault.
> +		  Platforms that do not implement firmware integrity checks
> +		  still report success.

Why not just make this a TEST_RUN of test 0 and skip having a separate interface?

> +
> +		* PFDI_IOCTL_FORCE_ERROR
> +		  Schedule injection of a specific PFDI return value for the
> +		  next invocation of a selected PFDI function on the same
> +		  CPU. The requested function ID must be a supported PFDI
> +		  function IDs. Only one pending injected error is permitted
> +		  per CPU and function, and a new request replaces any older
> +		  undelivered request for the same pair. When the injection
> +		  is delivered, firmware returns the injected value in x0 and
> +		  zeros x1 to x4.
> +
> +		These device nodes reflect the PFDI 1.0 ALP0 specification.
> +		PFDI state is not persistent across reset or firmware update;
> +		userspace must rediscover capabilities and any test state
> +		after such events.
> +
> +		These nodes are intended for platform bring-up and validation.
> +		Access should therefore be restricted to privileged software.
> +
> +Users:		Platform validation and manufacturing test tools



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

end of thread, other threads:[~2026-11-19 21:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
2026-11-19 21:35   ` Jonathan Cameron
2026-11-19 15:36 ` [PATCH RFC 2/4] firmware: arm: Add PFDI misc driver skeleton Ahmed Azeem
2026-11-19 15:36 ` [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support Ahmed Azeem
2026-11-19 15:36 ` [PATCH RFC 4/4] firmware: arm: Harden PFDI worker and hotplug handling Ahmed Azeem

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®