Skip to contentSkip to conversation

Add Altera SoCFPGA Crypto Service (FCS) driver

PATCH Applied [email protected] 2 participants 5 messages Nov 5 – Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v9 cover Add Altera SoCFPGA Crypto Service (FCS) driver

From: Hang Suan Wang <[email protected]>

This series adds Linux support for one of the Altera SoCFPGA Crypto Service (FCS): Secure Data Object Service (SDOS). SDOS is an Altera-proprietary service that seals and unseals arbitrary application data. FCS is a runtime cryptographic interface provided by the Secure Device Manager (SDM).

The SDM is the hardware security controller in Altera SoCFPGA devices. Host software reaches it through the existing stratix10-svc mailbox, which uses Arm Trusted Firmware SIP SMC calls. This series does not expose the rest of FCS (RNG, AES, SHA, key management, and so on).

SDOS protects data at rest. The SDM encrypts and decrypts using a key derived from a device-unique SDOS root key that never leaves the secure boundary. The host supplies plaintext (or an authenticated object) and never handles raw key material.

SDOS requests use the stratix10-svc asynchronous SIP SMC path. Source and destination buffers are allocated from the service-layer memory pool, and their physical addresses and SMMU IOVAs are passed to the SDM.

The caller opens one SDM crypto session with FCS_IOC_OPEN_SESSION and closes it with FCS_IOC_CLOSE_SESSION. SDOS commands use that session and do not open or close it. The session belongs to the file that opened it:
another file cannot use it or close it. Closing that file, including when the process exits, closes the session in the driver's release path. The SDM allows one session at a time. Each command takes the device lock, so only one request is in flight, and the kernel gives each command a new context ID. That ID is not part of the user ABI.

This is the session model later FCS commands will use, so a key imported in a future series stays alive until the caller closes the session. Those commands are not in this series. The ioctls are usable on their own. This series does not add a daemon in the kernel or require one in userspace.

The obvious question is why this is not a trusted-keys backend or a crypto API transform. There are four reasons this feature cannot fit:

- No key handle. The key is derived inside the SDM from a device-unique root key and is never set, imported or named by the host, so there is nothing for setkey() to bind to.

- Object format. The SDM emits a firmware-defined container: a 40-byte header, the payload, and a 48-byte HMAC, with the owner ID carried inside the header. That is not a generic AEAD output.

- Size. SDOS payloads are 32 to 32,672 bytes. Trusted keys wrap key material, which is far smaller, so the keyring model does not fit a data-at-rest service.

- Status reporting. SDM statuses 0x102 and 0x103 mean "decryption succeeded, but the key is outdated or should be reprovisioned". They are successes carrying a warning, and the warning has to reach the caller together with valid plaintext. Neither keyctl nor AF_ALG carries a firmware status alongside a successful result.

The series is organized as follows:
- Patch 1 (prerequisite) enlarges the stratix10-svc SMC argument array so the asynchronous FCS SDOS command can pass its full set of parameters.

- Patch 2 (prerequisite) extends the stratix10-svc service layer with the FCS command codes and matching SIP SMC function IDs, adds the Agilex 5 (intel,agilex5-svc) match, and registers a "stratix10-fcs" platform device that an FCS client driver binds to.

- Patch 3 adds the FCS driver. It is a misc device (/dev/socfpga-fcs). FCS_IOC_OPEN_SESSION and FCS_IOC_CLOSE_SESSION manage the SDM crypto session. FCS_IOC_SDOS encrypts or decrypts on that session.

Testing:
- Built for arm64 (defconfig + CONFIG_ALTERA_SOCFPGA_FCS=m).
- Tested on an Agilex 5 SoC FPGA board (SDOS root key provisioned). SDOS encrypt/decrypt round-trip; the decrypted output matches the original 64-byte plaintext:

   root@agilex5e:~# hexdump random_payload_bin2.bin
   0007cc0 a5e3 b39e cd9a 86de b3e7 d035 ad6f 6b4a
   0007cd0 1327 9672 25ec 1947 d98e e61e 9b2d 594d
   0007ce0 0474 29a2 fd28 e5a2 4a4b b608 6271 7e3d
   0007cf0 5c61 c2d1 4c7e 7aa6 e347 10b7 9aae 5439
   root@agilex5e:~# ./fcs_client -e
   root@agilex5e:~# ./fcs_client -E -i random_payload_bin2.bin -o
                    random_payload_encrypted_bin2.bin -v
   root@agilex5e:~# ./fcs_client -D -i random_payload_encrypted_bin2.bin -o
                    random_payload_decrypted_bin2.bin -v
   root@agilex5e:~# ./fcs_client -l
   root@agilex5e:~# hexdump -C random_payload_decrypted_bin2.bin
   e3 a5 9e b3 9a cd de 86  e7 b3 35 d0 6f ad 4a 6b  |..........5.o.Jk|
   27 13 72 96 ec 25 47 19  8e d9 1e e6 2d 9b 4d 59  |'.r..%G.....-.MY|
   74 04 a2 29 28 fd a2 e5  4b 4a 08 b6 71 62 3d 7e  |t..)(...KJ..qb=~|
   61 5c d1 c2 7e 4c a6 7a  47 e3 b7 10 ae 9a 39 54  |a\..~L.zG.....9T|

---
Changes since v8:
- Rework the FCS core into a command engine, socfpga_fcs_core_engine(), that takes per-command parameters. The caller supplies a fresh context ID for each command.
- Poll for async completion instead of registering a service-layer callback, fixing a race on the shared completion.
- Fix a NULL dereference in the ATF version callback when the response carries no payload.
- Return -ETIMEDOUT when a request times out; only busy and no-response statuses are retried until the deadline.
- Reject an SDOS op_mode other than 0 or 1 with -EINVAL instead of passing it to the firmware.
- Report the SDM status in error_code even when a request fails.
- Update the ioctl and ABI documentation, including the errno table.

Changes since v7:
- Give the caller the session lifecycle. FCS_IOC_OPEN_SESSION opens one SDM crypto session. FCS_IOC_CLOSE_SESSION closes it. SDOS no longer opens or closes the session, including when a request fails.
- Bind the session to the file that opened it. Another open returns -EBUSY. Use or close from another file returns -EPERM.
- Close the session from the file release path, so process exit still tears it down.
- Generate a new context ID for each command. It remains internal to the kernel.
- Document the session lifecycle, and split EBUSY and EPERM in the ioctl and ABI docs.

Changes since v6:
- Add proper support for the two SDOS address forms required by the SDM:
pass the physical address backing each buffer separately from its SMMU-remapped IOVA.
- Resolve DMA IOVAs through the device IOMMU domain with iommu_iova_to_phys(), and reject physical addresses outside the SDM's 32-bit addressable range.
- Allocate FCS service buffers with DMA_ATTR_FORCE_CONTIGUOUS on the SMMU path so their backing memory is reachable by the SDM; track allocation attributes and use matching dma_free_attrs().
- Mark only the FCS service channel as requiring physically reachable contiguous allocations.
- Rebase onto the current upstream stratix10-svc implementation and update FCS child-device registration and cleanup.
- Convert FCS core logging to device-scoped dev_*() messages.
- Update the MAINTAINERS mailing list and Makefile placement.

Changes since v5:
- Drop the exported fcs_alloc_buf()/fcs_free_buf() helpers. fcs_sdos_crypt() now allocates and releases the service-layer pool buffers itself, both under priv->lock, so a pool buffer can no longer be leaked when the device is unbound while a request is in flight.
- Stage user data through kvmalloc() buffers in fcs_sdos() so copy_from_user()/copy_to_user() run outside priv->lock. A faulting user page can no longer pin the lock and block device removal.
- Read the remaining 32-bit mailbox results with lower_32_bits(): the SDOS payload size and the ATF version error status. Both still cast an arm_smccc unsigned long through a 32-bit pointer.
- Document struct fcs_ioc_sdos and FCS_IOC_SDOS with kernel-doc in include/uapi/misc/socfpga-fcs-crypto.h. The structure layout and the ioctl number are unchanged.
- Add Documentation/misc-devices/socfpga-fcs.rst: the device node, the ioctl, the size ranges per direction, the errno table, and the 0x102/0x103 success-with-warning statuses.
- Explain in this cover letter why SDOS is a dedicated ioctl rather than a trusted-keys backend or a crypto API transform.

Changes since v4:
- Read ATF version words with lower_32_bits() so a 32-bit value in an arm_smccc unsigned long is correct on 64-bit big-endian.
- Keep the SDOS payload size when SDM returns 0x102/0x103 (decrypt OK, outdated key). Those are success-with-warning; still return the plaintext.
- Release the svc channel in driver remove, not on last close, so unbind cannot free parent channel memory while an fd is still open.
- Guard fcs_alloc_buf()/fcs_free_buf() with the lock and a NULL channel check so they do not use the channel after unbind.
- Always call stratix10_svc_done() after the ATF version query, including send failure, so the sync svc kthread is stopped safely.

Changes since v3:
- Rebase onto current linux-upstream; register stratix10-fcs beside the existing socfpga-hwmon child device.
- Drop context_id from the ioctl UAPI; the kernel owns session and context
- Make FCS state per-device and kref-counted so an open fd can outlive driver remove (-ENODEV after unbind).
- Copy user buffers in the front-end; the core uses kernel pointers only.
- Use kzalloc_obj() and shorten comments.

Changes since v2:
socfpga-fcs (front-end):
- Replace the sysfs store interface with an ioctl character device (/dev/socfpga_fcs) using a fixed-width UAPI struct and compat_ptr_ioctl(), fixing the KASAN out-of-bounds read and the 32-bit incompatibility of casting the sysfs buffer as a pointer.
- Add include/uapi/misc/socfpga-fcs-crypto.h and register the ioctl magic in Documentation/userspace-api/ioctl/ioctl-number.rst.
- Add a .release handler so a session is torn down if the owning fd is closed (including on crash).
socfpga-fcs (core):
- Manage the crypto session internally: SDOS opens and closes its own session; reject a concurrent open with -EBUSY. Removes the user-visible open/close-session interface and session UUID, fixing the session-exhaustion/DoS concern.
- Harden the async poll loop: keep polling until the deadline and always call stratix10_svc_async_done(), fixing the teardown use-after-free from the previous -EINPROGRESS early-return and the needless full-timeout block.
- Clear receive_cb only on the synchronous send-error path so a late firmware response cannot deref a NULL callback.
- Set priv = NULL on all fcs_init() error paths so a later probe is not permanently rejected with -EBUSY.
- Read the SDOS owner ID with get_unaligned_le64() for correct little-endian handling on big-endian hosts.
stratix10-svc:
- Enlarge the SMC arguments array so the async SDOS command can pass its full parameter set (through a11).
- In remove(), unregister the child devices before stratix10_svc_async_exit() so async_poll() cannot race a freed handle.

Changes since v1:
stratix10-svc:
- Forward result registers (kaddr1) for OPEN_SESSION and SDOS_DATA_EXT so session ID / output length reach the client.
socfpga-fcs (front-end):
- Publish sysfs via driver.dev_groups on the platform device instead of a raw kobject under /sys/kernel; callbacks now get a struct device.
socfpga-fcs (core):
- Convert completion timeouts with msecs_to_jiffies(); drop TIMEOUT.
- On SDM-busy timeout (-EAGAIN), abort without async_done() (no id reuse) and return -EINPROGRESS.
- SDOS: on in-flight abort, leak s_buf/d_buf instead of freeing to avoid corruption from late firmware DMA.
- Validate the caller UUID early in fcs_sdos_crypt().
- Enforce single instance: -EBUSY in fcs_init() and priv NULL-guard in fcs_acquire_cmd_ctx().
- Drop the always-zero platform attribute and fcs_get_platform().
- Set/clear receive_cb only around the synchronous send.
- Use device-lifetime priv->completion for the async path (was on-stack).
- Use the local ctx snapshot consistently in fcs_sdos_crypt() and fcs_session_close().
- Point sdos.dst_size at priv->sdos_output_size, not a stack variable.
socfpga-fcs.h:
- Drop platform/AGILEX5_PLAT; add sdos_output_size.
---

Hang Suan Wang (3):
firmware: stratix10-svc: increase args array
firmware: stratix10-svc: add FCS crypto-service commands for Agilex 5
firmware: socfpga-fcs: add Altera SoCFPGA FCS driver with SDOS feature

 .../sysfs-devices-platform-stratix10-fcs      |  21 +
 Documentation/misc-devices/index.rst          |   1 +
 Documentation/misc-devices/socfpga-fcs.rst    | 231 +++++
 .../userspace-api/ioctl/ioctl-number.rst      |   2 +
 MAINTAINERS                                   |  11 +
 drivers/firmware/Kconfig                      |  17 +
 drivers/firmware/Makefile                     |   2 +
 drivers/firmware/socfpga-fcs-core.c           | 856 ++++++++++++++++++
 drivers/firmware/socfpga-fcs.c                | 297 ++++++
 drivers/firmware/stratix10-svc.c              | 184 +++-
 include/linux/firmware/intel/socfpga-fcs.h    | 168 ++++
 include/linux/firmware/intel/stratix10-smc.h  |  64 ++
 .../firmware/intel/stratix10-svc-client.h     |  16 +-
 include/uapi/misc/socfpga-fcs-crypto.h        | 136 +++
 14 files changed, 1994 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs
 create mode 100644 Documentation/misc-devices/socfpga-fcs.rst
 create mode 100644 drivers/firmware/socfpga-fcs-core.c
 create mode 100644 drivers/firmware/socfpga-fcs.c
 create mode 100644 include/linux/firmware/intel/socfpga-fcs.h
 create mode 100644 include/uapi/misc/socfpga-fcs-crypto.h
Signature · 1 line
2.43.7
#1 lore raw

PATCH v9 1/3 firmware: stratix10-svc: increase args array

From: Hang Suan Wang <[email protected]>

Increase args array from 3 to 6, for the SDOS encryption to call smc call which is used for args to be passed via registers and not physically mapped buffer.

---
 drivers/firmware/stratix10-svc.c                    | 6 ++++--
 include/linux/firmware/intel/stratix10-svc-client.h | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/firmware/stratix10-svc.c +4 -2
index f7928ef6b6db..8f07b945392a 100644
@@ -219,7 +219,7 @@ struct stratix10_svc_data {
 	size_t size_output;
 	u32 command;
 	u32 flag;
-	u64 arg[3];
+	u64 arg[6];
 };
 
 /**
@@ -2088,7 +2088,9 @@ int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
 	p_data->arg[0] = p_msg->arg[0];
 	p_data->arg[1] = p_msg->arg[1];
 	p_data->arg[2] = p_msg->arg[2];
-	p_data->size = p_msg->payload_length;
+	p_data->arg[3] = p_msg->arg[3];
+	p_data->arg[4] = p_msg->arg[4];
+	p_data->arg[5] = p_msg->arg[5];
 	p_data->chan = chan;
 	pr_debug("%s: %s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n",
 		 __func__,
include/linux/firmware/intel/stratix10-svc-client.h +1 -1
index af13dacdf5ac..9bb46c3cb0f8 100644
@@ -215,7 +215,7 @@ struct stratix10_svc_client_msg {
 	void *payload_output;
 	size_t payload_length_output;
 	enum stratix10_svc_command_code command;
-	u64 arg[3];
+	u64 arg[6];
 };
 
 /**
Signature · 1 line
2.43.7
#2 lore raw

PATCH v9 2/3 firmware: stratix10-svc: add FCS crypto-service commands for Agilex 5

In reply to [email protected]

From: Hang Suan Wang <[email protected]>

The Agilex 5 Secure Device Manager (SDM 1.5) exposes an FPGA Crypto Service (FCS) over the existing SIP SMC mailbox: a session-based interface for crypto primitives such as SDOS (Secure Data Object Service) encrypt/decrypt. The service layer has no command to drive it yet.

Configure stratix10-svc about this interface so an in-kernel FCS client can use it:

- add the client command codes COMMAND_FCS_CRYPTO_OPEN_SESSION, COMMAND_FCS_CRYPTO_CLOSE_SESSION and COMMAND_FCS_SDOS_DATA_EXT (all asynchronous)

- add the matching asynchronous SIP SMC function IDs (INTEL_SIP_SMC_ASYNC_FCS_OPEN_CS_SESSION, INTEL_SIP_SMC_ASYNC_FCS_CLOSE_CS_SESSION and INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT) with their register-usage documentation;

- match "intel,agilex5-svc" and register a "stratix10-fcs" child platform device, mirroring the existing RSU child, so an FCS client driver can bind without a dedicated device-tree node;

The transport is unchanged: Agilex 5 reuses the SIP SMC calling convention and async mailbox ABI the driver already implements, so no new transport mechanism is required.

The command COMMAND_FCS_SDOS_DATA_EXT takes 2 types of buffer which are the physical address of the source and destination, and SMMU remapped address. Like other device in the market, Agilex 5 drives the SDM through an SMMU. The remapped address carry the IOVA returned by the DMA allocator, and the physical slots carry the address backing it, resolved with iommu_iova_to_phys().

The SDM can only reach 32-bit physical addresses, but dma_alloc_coherent() has no way to ask for that directly. Once an IOMMU is involved, it drops the __GFP_DMA and __GFP_DMA32 flags and adds __GFP_HIGHMEM instead. It assumes the IOMMU can map any page, so address range no longer matters to it. The only attribute that skips this behavior is DMA_ATTR_FORCE_CONTIGUOUS, which pulls memory from CMA instead. On arm64, CMA happens to be reserved below the DMA zone limit, so it ends up 32-bit-reachable. The contiguous part is not actually what we need it's just a side effect of using CMA. Apply this attribute only to channels whose commands carry a physical address, and track which attributes each buffer was allocated with so it's freed the same way.

This is a prerequisite for the SoCFPGA FCS driver, the first in-tree consumer of these commands.

---
 drivers/firmware/stratix10-svc.c              | 178 +++++++++++++++++-
 include/linux/firmware/intel/stratix10-smc.h  |  64 +++++++
 .../firmware/intel/stratix10-svc-client.h     |  14 ++
 3 files changed, 247 insertions(+), 9 deletions(-)
drivers/firmware/stratix10-svc.c +169 -9
index 8f07b945392a..15af21203c8f 100644
@@ -72,6 +72,7 @@
 
 /* stratix10 service layer clients */
 #define STRATIX10_RSU				"stratix10-rsu"
+#define STRATIX10_FCS				"stratix10-fcs"
 #define SOCFPGA_HWMON				"socfpga-hwmon"
 
 /* Maximum number of SDM client IDs. */
@@ -145,10 +146,12 @@ struct stratix10_svc_chan;
 /**
  * struct stratix10_svc - svc private data
  * @stratix10_svc_rsu: pointer to stratix10 RSU device
+ * @stratix10_svc_fcs: pointer to stratix10 FCS device
  * @stratix10_svc_hwmon: pointer to stratix10 HWMON device
  */
 struct stratix10_svc {
 	struct platform_device *stratix10_svc_rsu;
+	struct platform_device *stratix10_svc_fcs;
 	struct platform_device *stratix10_svc_hwmon;
 };
 
@@ -181,8 +184,10 @@ struct stratix10_svc_sh_memory {
  *         On the SMMU path this is (IOVA + %SVC_SDM_DMA_ADDR_OFFSET);
  *         on the gen_pool path this equals the raw physical address.
  * @size: size of memory
- * @dma_addr: IOVA returned by dma_alloc_coherent(); used to free the
- *            mapping via dma_free_coherent() on the SMMU path.
+ * @dma_addr: IOVA returned by the DMA allocator; used to release the
+ *            mapping again on the SMMU path.
+ * @attrs: DMA attributes the buffer was allocated with, passed back to
+ *         dma_free_attrs() so that the release matches the allocation.
  * @node: link list head node
  *
  * This struct is used in a list that keeps track of buffers which have
@@ -195,6 +200,7 @@ struct stratix10_svc_data_mem {
 	phys_addr_t paddr;
 	size_t size;
 	dma_addr_t dma_addr;
+	unsigned long attrs;
 	struct list_head node;
 };
 
@@ -306,6 +312,10 @@ struct stratix10_async_ctrl {
  * @svc_fifo_lock: protect access to service message data queue (locking pending fifo)
  * @lock: protect access to the channel
  * @async_chan: reference to asynchronous channel object for this channel
+ * @mem_contiguous: when true, buffers for this channel are allocated so that
+ *                  their physical address is reachable by the SDM, which is
+ *                  required by commands passing a physical address rather
+ *                  than an IOVA.
  *
  * This struct is used by service client to communicate with service layer.
  * Each service client has its own channel created by service controller.
@@ -319,6 +329,7 @@ struct stratix10_svc_chan {
 	spinlock_t svc_fifo_lock;
 	spinlock_t lock;
 	struct stratix10_async_chan *async_chan;
+	bool mem_contiguous;
 };
 
 /**
@@ -336,6 +347,8 @@ struct stratix10_svc_chan {
  * @psci_reboot_nb: reboot notifier for PSCI secondary CPU offlining
  * @use_dma_mem: when true, buffers are allocated via dma_alloc_coherent()
  *               instead of the ATF reserved-memory gen_pool.
+ * @domain: IOMMU domain the SDM-visible buffers are mapped into; used to
+ *          resolve a buffer's IOVA back to the physical address ATF expects.
  * @dma_addr_offset: value added to the DMA address (IOVA) before passing it
  *                   to ATF via SMC. ATF uses this offset to distinguish
  *                   SMMU-mapped buffers from direct physical addresses; it
@@ -361,6 +374,7 @@ struct stratix10_svc_controller {
 	struct stratix10_async_ctrl actrl;
 	struct notifier_block psci_reboot_nb;
 	bool use_dma_mem;
+	struct iommu_domain *domain;
 	unsigned long dma_addr_offset;
 	struct stratix10_svc_chan chans[] __counted_by(num_chans);
 };
@@ -396,6 +410,88 @@ static void *svc_pa_to_va(unsigned long addr)
 	return NULL;
 }
 
+/**
+ * svc_va_to_pa() - translate virtual address to physical address
+ * @ctrl: pointer to service layer controller
+ * @vaddr: buffer returned by stratix10_svc_allocate_memory()
+ *
+ * Provides the physical address arguments of an SMC call. On the SMMU path
+ * ATF expects the address backing the buffer's IOVA here, with the IOVA
+ * itself supplied separately by svc_va_to_iova(). The backing address is
+ * resolved through the device's IOMMU domain, since dma_alloc_coherent()
+ * returns a remapped virtual address that virt_to_phys() cannot translate.
+ * On the gen_pool path the stored address is already a physical address.
+ *
+ * The SDM can only reach a 32-bit physical address, so reject anything
+ * beyond that rather than letting the SDM fail the command later.
+ *
+ * Return: physical address of the buffer, or 0 if the provided virtual
+ * address doesn't exist or is not reachable by the SDM.
+ */
+static unsigned long svc_va_to_pa(struct stratix10_svc_controller *ctrl,
+				  void *vaddr)
+{
+	struct stratix10_svc_data_mem *pmem;
+	phys_addr_t pa;
+
+	if (!vaddr)
+		return 0;
+
+	guard(mutex)(&svc_mem_lock);
+	list_for_each_entry(pmem, &svc_data_mem, node) {
+		if (pmem->vaddr != vaddr)
+			continue;
+
+		if (!ctrl->use_dma_mem)
+			return pmem->paddr;
+
+		pa = iommu_iova_to_phys(ctrl->domain, pmem->dma_addr);
+		if (!pa || pa > U32_MAX) {
+			dev_err(ctrl->dev,
+				"payload %p resolved to %pa, which the SDM cannot reach\n",
+				vaddr, &pa);
+			return 0;
+		}
+
+		return pa;
+	}
+
+	dev_err(ctrl->dev, "payload %p is not a service layer buffer\n", vaddr);
+	return 0;
+}
+
+/**
+ * svc_va_to_iova() - translate virtual address to SMMU remapped address
+ * @ctrl: pointer to service layer controller
+ * @vaddr: buffer returned by stratix10_svc_allocate_memory()
+ *
+ * Provides the SMMU remapped address arguments of an SMC call. Without an
+ * SMMU there is no separate remapped address, and ATF expects these
+ * arguments to repeat the physical address.
+ *
+ * Return: IOVA of the buffer on the SMMU path, its physical address on the
+ * gen_pool path, or 0 if the provided virtual address doesn't exist.
+ */
+static unsigned long svc_va_to_iova(struct stratix10_svc_controller *ctrl,
+				    void *vaddr)
+{
+	struct stratix10_svc_data_mem *pmem;
+
+	if (!vaddr)
+		return 0;
+
+	guard(mutex)(&svc_mem_lock);
+	list_for_each_entry(pmem, &svc_data_mem, node) {
+		if (pmem->vaddr != vaddr)
+			continue;
+
+		return ctrl->use_dma_mem ? pmem->dma_addr : pmem->paddr;
+	}
+
+	dev_err(ctrl->dev, "payload %p is not a service layer buffer\n", vaddr);
+	return 0;
+}
+
 /**
  * svc_thread_cmd_data_claim() - claim back buffer from the secure world
  * @ctrl: pointer to service layer controller
@@ -1492,6 +1588,26 @@ int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg,
 		STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id);
 
 	switch (p_msg->command) {
+	case COMMAND_FCS_CRYPTO_OPEN_SESSION:
+		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_OPEN_CS_SESSION;
+		break;
+	case COMMAND_FCS_CRYPTO_CLOSE_SESSION:
+		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CLOSE_CS_SESSION;
+		args.a2 = p_msg->arg[0];
+		break;
+	case COMMAND_FCS_SDOS_DATA_EXT:
+		args.a0 = INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT;
+		args.a2 = p_msg->arg[0];
+		args.a3 = p_msg->arg[1];
+		args.a4 = p_msg->arg[2];
+		args.a5 = svc_va_to_pa(ctrl, p_msg->payload);
+		args.a6 = p_msg->payload_length;
+		args.a7 = svc_va_to_pa(ctrl, p_msg->payload_output);
+		args.a8 = p_msg->payload_length_output;
+		args.a9 = p_msg->arg[3];
+		args.a10 = svc_va_to_iova(ctrl, p_msg->payload);
+		args.a11 = svc_va_to_iova(ctrl, p_msg->payload_output);
+		break;
 	case COMMAND_RSU_GET_SPT_TABLE:
 		args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_SPT;
 		break;
@@ -1589,8 +1705,13 @@ static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan,
 	data->status = STRATIX10_GET_SDM_STATUS_CODE(handle->res.a1);
 
 	switch (p_msg->command) {
+	case COMMAND_FCS_CRYPTO_CLOSE_SESSION:
 	case COMMAND_RSU_NOTIFY:
 		break;
+	case COMMAND_FCS_CRYPTO_OPEN_SESSION:
+	case COMMAND_FCS_SDOS_DATA_EXT:
+		data->kaddr1 = (void *)&handle->res.a2;
+		break;
 	case COMMAND_RSU_GET_SPT_TABLE:
 		data->kaddr1 = (void *)&handle->res.a2;
 		data->kaddr2 = (void *)&handle->res.a3;
@@ -2157,7 +2278,25 @@ void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
 		return ERR_PTR(-ENOMEM);
 
 	if (ctrl->use_dma_mem) {
-		va = dma_alloc_coherent(ctrl->dev, size, &dma_addr, GFP_KERNEL);
+		pmem->attrs = 0;
+		if (chan->mem_contiguous) {
+			/*
+			 * Some SMC calls take a buffer's physical address and
+			 * its SMMU remapped address in separate arguments, and
+			 * the SDM has to reach the physical one (see
+			 * INTEL_SIP_SMC_ASYNC_FCS_CRYPTION_EXT). The default
+			 * allocator path adds __GFP_HIGHMEM, so route the
+			 * allocation through CMA instead, which arm64 reserves
+			 * below the DMA zone limit.
+			 */
+			if (size <= PAGE_SIZE)
+				size = PAGE_SIZE * 2;
+			pmem->attrs = DMA_ATTR_FORCE_CONTIGUOUS;
+			va = dma_alloc_attrs(ctrl->dev, size, &dma_addr, GFP_KERNEL,
+					     pmem->attrs);
+		} else {
+			va = dma_alloc_coherent(ctrl->dev, size, &dma_addr, GFP_KERNEL);
+		}
 		if (!va) {
 			kfree(pmem);
 			return ERR_PTR(-ENOMEM);
@@ -2211,8 +2350,8 @@ void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
 			continue;
 
 		if (ctrl->use_dma_mem) {
-			dma_free_coherent(ctrl->dev, pmem->size,
-					  pmem->vaddr, pmem->dma_addr);
+			dma_free_attrs(ctrl->dev, pmem->size, pmem->vaddr,
+				       pmem->dma_addr, pmem->attrs);
 		} else {
 			gen_pool_free(ctrl->genpool,
 				      (unsigned long)kaddr, pmem->size);
@@ -2303,8 +2442,8 @@ static void svc_data_mem_cleanup(void *data)
 		dev_warn(ctrl->dev, "leaked svc buffer %p, freeing on unbind\n",
 			 pmem->vaddr);
 		if (ctrl->use_dma_mem) {
-			dma_free_coherent(ctrl->dev, pmem->size,
-					  pmem->vaddr, pmem->dma_addr);
+			dma_free_attrs(ctrl->dev, pmem->size, pmem->vaddr,
+				       pmem->dma_addr, pmem->attrs);
 		} else {
 			gen_pool_free(ctrl->genpool,
 				      (unsigned long)pmem->vaddr, pmem->size);
@@ -2386,6 +2525,8 @@ static int stratix10_svc_drv_probe(struct platform_device *pdev)
 	init_completion(&controller->complete_status);
 
 	if (use_dma_mem) {
+		controller->domain = iommu_get_domain_for_dev(dev);
+
 		ret = devm_add_action_or_reset(dev, svc_data_mem_cleanup, controller);
 		if (ret)
 			goto err_destroy_pool;
@@ -2421,6 +2562,8 @@ static int stratix10_svc_drv_probe(struct platform_device *pdev)
 		controller->chans[i].scl = NULL;
 		controller->chans[i].ctrl = controller;
 		controller->chans[i].name = (char *)chan_names[i];
+		controller->chans[i].mem_contiguous =
+			!strcmp(controller->chans[i].name, SVC_CLIENT_FCS);
 		spin_lock_init(&controller->chans[i].lock);
 		ret = kfifo_alloc(&controller->chans[i].svc_fifo, fifo_size, GFP_KERNEL);
 		if (ret) {
@@ -2448,9 +2591,20 @@ static int stratix10_svc_drv_probe(struct platform_device *pdev)
 		goto err_free_fifos;
 	}
 
+	svc->stratix10_svc_fcs = platform_device_alloc(STRATIX10_FCS, 0);
+	if (!svc->stratix10_svc_fcs) {
+		dev_err(dev, "failed to allocate %s device\n", STRATIX10_FCS);
+		ret = -ENOMEM;
+		goto err_unregister_clients;
+	}
+
 	ret = platform_device_add(svc->stratix10_svc_rsu);
 	if (ret)
-		goto err_put_device;
+		goto err_put_rsu;
+
+	ret = platform_device_add(svc->stratix10_svc_fcs);
+	if (ret)
+		goto err_put_fcs;
 
 	if (IS_ENABLED(CONFIG_SENSORS_ALTERA_SOCFPGA_HWMON)) {
 		svc->stratix10_svc_hwmon =
@@ -2484,9 +2638,13 @@ static int stratix10_svc_drv_probe(struct platform_device *pdev)
 		platform_device_unregister(svc->stratix10_svc_hwmon);
 	if (svc->stratix10_svc_rsu)
 		platform_device_unregister(svc->stratix10_svc_rsu);
+	if (svc->stratix10_svc_fcs)
+		platform_device_unregister(svc->stratix10_svc_fcs);
 	goto err_free_fifos;
-err_put_device:
+err_put_rsu:
 	platform_device_put(svc->stratix10_svc_rsu);
+err_put_fcs:
+	platform_device_put(svc->stratix10_svc_fcs);
 err_free_fifos:
 	/* only remove from list if list_add_tail() was reached */
 	if (!list_empty(&controller->node))
@@ -2513,6 +2671,8 @@ static void stratix10_svc_drv_remove(struct platform_device *pdev)
 	platform_device_unregister(svc->stratix10_svc_rsu);
 	if (svc->stratix10_svc_hwmon)
 		platform_device_unregister(svc->stratix10_svc_hwmon);
+	if (svc->stratix10_svc_fcs)
+		platform_device_unregister(svc->stratix10_svc_fcs);
 
 	psci_cpu_off_teardown(ctrl);
 
include/linux/firmware/intel/stratix10-smc.h +64 -0

70 lines not on this page · raw message

include/linux/firmware/intel/stratix10-svc-client.h +14 -0
index 9bb46c3cb0f8..edd7953d5d08 100644
@@ -122,6 +122,15 @@ struct stratix10_svc_chan;
  * @COMMAND_SMC_SVC_VERSION: Non-mailbox SMC SVC API Version,
  * return status is SVC_STATUS_OK
  *
+ * @COMMAND_FCS_CRYPTO_OPEN_SESSION: open the crypto service session(s),
+ * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
+ *
+ * @COMMAND_FCS_CRYPTO_CLOSE_SESSION: close the crypto service session(s),
+ * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
+ *
+ * @COMMAND_FCS_SDOS_DATA_EXT: extend SDOS data encryption & decryption,
+ * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
+ *
  * @COMMAND_MBOX_SEND_CMD: send generic mailbox command, return status is
  * SVC_STATUS_OK or SVC_STATUS_ERROR
  *
@@ -190,6 +199,11 @@ enum stratix10_svc_command_code {
 	COMMAND_FCS_RANDOM_NUMBER_GEN,
 	/* for general status poll */
 	COMMAND_POLL_SERVICE_STATUS = 40,
+	/* for crypto service */
+	COMMAND_FCS_CRYPTO_OPEN_SESSION = 50,
+	COMMAND_FCS_CRYPTO_CLOSE_SESSION,
+	/* for extended SDOS encrypt/decrypt */
+	COMMAND_FCS_SDOS_DATA_EXT = 82,
 	/* for generic mailbox send command */
 	COMMAND_MBOX_SEND_CMD = 100,
 	/* Non-mailbox SMC Call */
Signature · 1 line
2.43.7
#3 lore raw

PATCH v9 3/3 firmware: socfpga-fcs: add Altera SoCFPGA FCS driver with SDOS feature

In reply to [email protected]

From: Hang Suan Wang <[email protected]>

The Secure Device Manager (SDM) is a hardware security manager in Altera SoCFPGA. It provides the Secure Data Object Store (SDOS), a service for encrypting and decrypting data.

This patch adds the Altera SoCFPGA Crypto Service (FCS) driver, allowing non-secure Linux software to request SDOS operations. Requests pass through the SoCFPGA service layer (svc) to ARM Trusted Firmware (ATF), which forwards permitted requests to the SDM.

The SDOS protects data at rest: the SDM encrypts and decrypts using a key derived from a device-unique SDOS root key plus an SDM-generated IV, so the host never handles raw key material or IVs. It only submits plaintext it already owns and receives authenticated ciphertext objects managed by the SDM. A primary use case is installing operational keys without ever appearing in cleartext.

The driver is a standalone module and describes no hardware of its own. It binds by name to the "stratix10-fcs" platform device registered by stratix10-svc, so no device-tree node is required. SDOS requests go to the SDM through the stratix10-svc asynchronous SIP SMC path using service-layer memory pool buffers that the SDM can reach via physical or SMMU-remapped addresses. Userspace talks to /dev/socfpga-fcs via ioctl and sysfs exposes atf_version.

For encryption the SDM returns a structured object (header, ciphertext, HMAC). For decryption the SDM validates the HMAC and enforces the 64-bit owner ID from the object header so only the creator can decrypt it.

The user opens one SDM crypto session, sends one or more commands such as SDOS, and closes the session when finished. The session stays open across those commands. Each command takes priv->lock, so only one request is in flight at a time. If the file that owns the session is closed, including on process exit, the driver closes the session.

---
 .../sysfs-devices-platform-stratix10-fcs      |  21 +
 Documentation/misc-devices/index.rst          |   1 +
 Documentation/misc-devices/socfpga-fcs.rst    | 231 +++++
 .../userspace-api/ioctl/ioctl-number.rst      |   2 +
 MAINTAINERS                                   |  11 +
 drivers/firmware/Kconfig                      |  17 +
 drivers/firmware/Makefile                     |   2 +
 drivers/firmware/socfpga-fcs-core.c           | 856 ++++++++++++++++++
 drivers/firmware/socfpga-fcs.c                | 297 ++++++
 include/linux/firmware/intel/socfpga-fcs.h    | 168 ++++
 include/uapi/misc/socfpga-fcs-crypto.h        | 136 +++
 11 files changed, 1742 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs
 create mode 100644 Documentation/misc-devices/socfpga-fcs.rst
 create mode 100644 drivers/firmware/socfpga-fcs-core.c
 create mode 100644 drivers/firmware/socfpga-fcs.c
 create mode 100644 include/linux/firmware/intel/socfpga-fcs.h
 create mode 100644 include/uapi/misc/socfpga-fcs-crypto.h
Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs new file +21 -0
new file mode 100644
index 000000000000..1b23db16331e
@@ -0,0 +1,21 @@
+	Intel Stratix10 / Altera Agilex FPGA Crypto Service (FCS) device
+	attributes
+
+What:		/sys/devices/platform/stratix10-fcs.0/atf_version
+Date:		October 2026
+KernelVersion:	7.3
+Contact:	Hang Suan Wang <[email protected]>
+Contact:	Genevieve Chan <[email protected]>
+Description:
+		(RO) Arm Trusted Firmware build version. The driver queries
+		it once through the stratix10-svc service layer while
+		probing, and every read returns that cached result.
+
+		The file contains three unsigned decimal numbers, the major,
+		minor and patch version, separated by dots and terminated by
+		a newline::
+
+			%u.%u.%u
+
+		A read fails with ENODATA when that probe-time query produced
+		no version.
Documentation/misc-devices/index.rst +1 -0
index f911edaecbfa..9cefd8bc15a7 100644
@@ -24,6 +24,7 @@ fit into other categories.
    mrvl_cn10k_dpi
    oxsemi-tornado
    pci-endpoint-test
+   socfpga-fcs
    spear-pcie-gadget
    tps6594-pfsm
    uacce
Documentation/misc-devices/socfpga-fcs.rst new file +231 -0

231 lines not on this page · raw message

Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
index 2fc53093752d..2bf78a38f266 100644
@@ -348,6 +348,8 @@ Code  Seq#    Include File                                             Comments
                                                                        <mailto:[email protected]>
 0xA6  00-0F  uapi/linux/alloc_tag.h                                    Memory allocation profiling
                                                                        <mailto:[email protected]>
+0xA7  00-1F  uapi/misc/socfpga-fcs-crypto.h                            Altera SoCFPGA FCS (Crypto Service)
+                                                                       <mailto:[email protected]>
 0xAA  00-3F  linux/uapi/linux/userfaultfd.h
 0xAB  00-1F  linux/nbd.h
 0xAC  00-1F  linux/raw.h
MAINTAINERS +11 -0
index 3a19da74d00c..e1367d1c4631 100644
@@ -945,6 +945,17 @@ ALPS PS/2 TOUCHPAD DRIVER
 R:	Pali Rohár <[email protected]>
 F:	drivers/input/mouse/alps.*
 
+ALTERA FCS DRIVER
+M:	Hang Suan Wang <[email protected]>
+M:	Genevieve Chan <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/ABI/testing/sysfs-devices-platform-stratix10-fcs
+F:	Documentation/misc-devices/socfpga-fcs.rst
+F:	drivers/firmware/socfpga-fcs*
+F:	include/linux/firmware/intel/socfpga-fcs*
+F:	include/uapi/misc/socfpga-fcs*
+
 ALTERA MAILBOX DRIVER
 M:	Tien Sung Ang <[email protected]>
 S:	Maintained
drivers/firmware/Kconfig +17 -0
index b7cc11e4fbfa..15727855fd5f 100644
@@ -193,6 +193,23 @@ config INTEL_STRATIX10_RSU
 
 	  Say Y here if you want Intel RSU support.
 
+config ALTERA_SOCFPGA_FCS
+	tristate "Altera SoCFPGA Crypto Services (FCS)"
+	depends on INTEL_STRATIX10_SERVICE
+	help
+	  Altera SoCFPGA Crypto Services (FCS) driver gives user space
+	  access to the crypto services of the Secure Device Manager (SDM)
+	  through the Intel Service Layer, with requests forwarded to Arm
+	  Trusted Firmware.
+
+	  The SDM executes or authorizes the requests using device-rooted
+	  security resources. Protected key material stays within the
+	  secure firmware boundary and is never exposed to non-secure host
+	  software.
+
+	  Say Y here to add support for Altera SoCFPGA Crypto Services
+	  (FCS).
+
 config MTK_ADSP_IPC
 	tristate "MTK ADSP IPC Protocol driver"
 	depends on MTK_ADSP_MBOX
drivers/firmware/Makefile +2 -0
index be46f1e1dc77..b489548ef3b8 100644
@@ -2,6 +2,8 @@
 #
 # Makefile for the linux kernel.
 #
+obj-$(CONFIG_ALTERA_SOCFPGA_FCS) += altera-fcs.o
+altera-fcs-y := socfpga-fcs.o socfpga-fcs-core.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
drivers/firmware/socfpga-fcs-core.c new file +856 -0

856 lines not on this page · raw message

drivers/firmware/socfpga-fcs.c new file +297 -0

297 lines not on this page · raw message

include/linux/firmware/intel/socfpga-fcs.h new file +168 -0

168 lines not on this page · raw message

include/uapi/misc/socfpga-fcs-crypto.h new file +136 -0

136 lines not on this page · raw message

Signature · 1 line
2.43.7
maintainer Applied
#4 lore raw
In reply to [email protected]
Quoted 6 lines · On Thu, 05 Nov 2026 09:17:09 -0800, [email protected] wrote:

This series adds Linux support for one of the Altera SoCFPGA Crypto Service (FCS): Secure Data Object Service (SDOS). SDOS is an Altera-proprietary service that seals and unseals arbitrary application data. FCS is a runtime cryptographic interface provided by the Secure Device Manager (SDM).

Quoted 6 lines

The SDM is the hardware security controller in Altera SoCFPGA devices. Host software reaches it through the existing stratix10-svc mailbox, which uses Arm Trusted Firmware SIP SMC calls. This series does not expose the rest of FCS (RNG, AES, SHA, key management, and so on).

[...]

Applied, thanks!

[1/3] firmware: stratix10-svc: increase args array

      commit: 71a5f8548729bcdc37cce69c022bccb34a67c116
[2/3] firmware: stratix10-svc: add FCS crypto-service commands for Agilex 5
      commit: cfafd39ef0ef09b33a06cb9564f210b3a113197d
[3/3] firmware: socfpga-fcs: add Altera SoCFPGA FCS driver with SDOS feature
      commit: c5548ffea1db1b77756145bfbbb7839edf0576ff

Best regards,

Signature · 1 line
Dinh Nguyen <[email protected]>