]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
drivers: introduce Secure Monitor uclass
authorAlexey Romanov <avromanov@salutedevices.com>
Thu, 21 Sep 2023 08:13:34 +0000 (11:13 +0300)
committerNeil Armstrong <neil.armstrong@linaro.org>
Sun, 15 Oct 2023 10:23:48 +0000 (12:23 +0200)
At the moment, we don't have a common API for working with
SM, only the smc_call() function. This approach is not generic
and difficult to configure and maintain.

This patch adds UCLASS_SM with the generic API:

- sm_call()
- sm_call_write()
- sm_call_read()

These functions operate with struct pt_regs, which describes
Secure Monitor arguments.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Link: https://lore.kernel.org/r/20230921081346.22157-2-avromanov@salutedevices.com
Signed-off-by: Neil Armstrong <neil.armstrong@linaro.org>
drivers/Kconfig
drivers/Makefile
drivers/sm/Kconfig [new file with mode: 0644]
drivers/sm/Makefile [new file with mode: 0644]
drivers/sm/sm-uclass.c [new file with mode: 0644]
include/dm/uclass-id.h
include/sm-uclass.h [new file with mode: 0644]
include/sm.h [new file with mode: 0644]

index a25f6ae02fdfdc84cc4a8722cfa6984e00c077c1..a073230c26dd6a94d2ab46fd8e68278e30b51352 100644 (file)
@@ -118,6 +118,8 @@ source "drivers/scsi/Kconfig"
 
 source "drivers/serial/Kconfig"
 
+source "drivers/sm/Kconfig"
+
 source "drivers/smem/Kconfig"
 
 source "drivers/sound/Kconfig"
index efc2a4afb24e5f78c887efdd6b2d1a0031c28a6a..74f940a57d34481b537d56257c8386a284d6553b 100644 (file)
@@ -35,6 +35,7 @@ obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/
 obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/
 obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/
 obj-$(CONFIG_$(SPL_)SYSINFO) += sysinfo/
+obj-$(CONFIG_$(SPL_TPL_)SM) += sm/
 obj-$(CONFIG_$(SPL_TPL_)TPM) += tpm/
 obj-$(CONFIG_$(SPL_)NVME) += nvme/
 obj-$(CONFIG_XEN) += xen/
diff --git a/drivers/sm/Kconfig b/drivers/sm/Kconfig
new file mode 100644 (file)
index 0000000..6cc6d55
--- /dev/null
@@ -0,0 +1,2 @@
+config SM
+       bool "Enable Secure Monitor driver support"
diff --git a/drivers/sm/Makefile b/drivers/sm/Makefile
new file mode 100644 (file)
index 0000000..9f4683b
--- /dev/null
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-y += sm-uclass.o
diff --git a/drivers/sm/sm-uclass.c b/drivers/sm/sm-uclass.c
new file mode 100644 (file)
index 0000000..6a8b702
--- /dev/null
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2023 SberDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.com>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <sm-uclass.h>
+
+static const struct sm_ops *get_sm_ops(struct udevice *dev)
+{
+       return (const struct sm_ops *)dev->driver->ops;
+}
+
+int sm_call(struct udevice *dev, u32 cmd, s32 *ret, struct pt_regs *args)
+{
+       const struct sm_ops *ops = get_sm_ops(dev);
+
+       if (ops->sm_call)
+               return ops->sm_call(dev, cmd, ret, args);
+
+       return -ENOSYS;
+}
+
+int sm_call_read(struct udevice *dev, void *buffer, size_t size,
+                u32 cmd, struct pt_regs *args)
+{
+       const struct sm_ops *ops = get_sm_ops(dev);
+
+       if (ops->sm_call_read)
+               return ops->sm_call_read(dev, buffer, size, cmd,
+                                        args);
+
+       return -ENOSYS;
+}
+
+int sm_call_write(struct udevice *dev, void *buffer, size_t size,
+                  u32 cmd, struct pt_regs *args)
+{
+       const struct sm_ops *ops = get_sm_ops(dev);
+
+       if (ops->sm_call_write)
+               return ops->sm_call_write(dev, buffer, size, cmd,
+                                         args);
+
+       return -ENOSYS;
+}
+
+UCLASS_DRIVER(sm) = {
+       .name           = "sm",
+       .id             = UCLASS_SM,
+};
index 0432c95c9edc19f25daa520da941ed9bc0ee60da..d2c5288c8bbd34b845a3ebf21db9d8be66046b7a 100644 (file)
@@ -89,6 +89,7 @@ enum uclass_id {
        UCLASS_MDIO,            /* MDIO bus */
        UCLASS_MDIO_MUX,        /* MDIO MUX/switch */
        UCLASS_MEMORY,          /* Memory Controller device */
+       UCLASS_SM,              /* Secure Monitor driver */
        UCLASS_MISC,            /* Miscellaneous device */
        UCLASS_MMC,             /* SD / MMC card or chip */
        UCLASS_MOD_EXP,         /* RSA Mod Exp device */
diff --git a/include/sm-uclass.h b/include/sm-uclass.h
new file mode 100644 (file)
index 0000000..c114484
--- /dev/null
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2023 SberDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.com>
+ */
+
+#ifndef __SM_UCLASS_H__
+#define __SM_UCLASS_H__
+
+#include <asm/types.h>
+#include <asm/ptrace.h>
+
+struct udevice;
+
+/**
+ * struct sm_ops - The functions that a SM driver must implement.
+ *
+ * @sm_call: Request a secure monitor call with specified command.
+ *
+ * @sm_call_read: Request a secure monitor call and retrieve data
+ * from secure-monitor (depends on specified command).
+ *
+ * @sm_call_write: Request a secure monitor call and send data
+ * to secure-monitor (depends on specified command).
+ *
+ * The individual methods are described more fully below.
+ */
+struct sm_ops {
+       /**
+        * sm_call - generic SMC call to the secure-monitor
+        *
+        * @dev:        Pointer to UCLASS_SM device
+        * @cmd_index:  Index of the SMC function ID
+        * @smc_ret:    Returned value from secure world
+        * @args:       SMC arguments
+        *
+        * @return:     0 on success, a negative value on error
+        */
+       int (*sm_call)(struct udevice *dev, u32 cmd, s32 *smc_ret,
+                      struct pt_regs *args);
+
+       /**
+        * sm_call_write - send data to secure-monitor
+        *
+        * @dev:        Pointer to UCLASS_SM device
+        * @buffer:     Buffer containing data to send
+        * @size:       Size of the buffer
+        * @cmd:        Index of the SMC function ID
+        * @args:       SMC arguments
+        *
+        * @return:     size of sent data on success, a negative value on error
+        */
+       int (*sm_call_write)(struct udevice *dev, void *buffer,
+                            size_t size, u32 cmd, struct pt_regs *args);
+
+       /**
+        * sm_call_read - retrieve data from secure-monitor
+        *
+        * @dev:        Pointer to UCLASS_SM device
+        * @buffer:     Buffer to store the retrieved data
+        * @size:       Size of the buffer
+        * @cmd:        Index of the SMC function ID
+        * @args:       SMC arguments
+        *
+        * @return:     size of read data on success, a negative value on error
+        */
+       int (*sm_call_read)(struct udevice *dev, void *buffer,
+                           size_t size, u32 cmd, struct pt_regs *args);
+};
+
+#endif /* __SM_UCLASS_H__ */
diff --git a/include/sm.h b/include/sm.h
new file mode 100644 (file)
index 0000000..afa9c89
--- /dev/null
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2023 SberDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.ru>
+ */
+
+#ifndef __SM_H__
+#define __SM_H__
+
+/*
+ * NOTE: UCLASS_SM is designed with the idea that
+ * each driver should convert @cmd to some raw
+ * value, which is known only for driver, and set this
+ * value to the first element of the @args->regs array.
+ * Therefore, it is necessary to pass the remaining
+ * arguments starting at index = 1. Anyway, driver
+ * implementation may vary, so, please, check the specific
+ * implementation of the driver you are using.
+ */
+
+#include <asm/types.h>
+#include <asm/ptrace.h>
+
+struct udevice;
+
+/**
+ * sm_call - generic SMC call to the secure-monitor
+ *
+ * @dev:       Pointer to UCLASS_SM device
+ * @cmd_index: Index of the SMC function ID
+ * @smc_ret:   Returned value from secure world
+ * @args:      SMC arguments
+ *
+ * @return:    0 on success, a negative value on error
+ */
+int sm_call(struct udevice *dev, u32 cmd, s32 *ret, struct pt_regs *args);
+
+/**
+ * sm_call_read - retrieve data from secure-monitor
+ *
+ * @dev:       Pointer to UCLASS_MESON_SM device
+ * @buffer:    Buffer to store the retrieved data
+ * @size:      Size of the buffer
+ * @cmd:       Index of the SMC function ID
+ * @args:      SMC arguments
+ *
+ * @return:    size of read data on success, a negative value on error
+ */
+int sm_call_read(struct udevice *dev, void *buffer, size_t size,
+                u32 cmd, struct pt_regs *args);
+
+/**
+ * sm_call_write - send data to secure-monitor
+ *
+ * @dev:       Pointer to UCLASS_SM device
+ * @buffer:    Buffer containing data to send
+ * @size:      Size of the buffer
+ * @cmd:       Index of the SMC function ID
+ * @args:      SMC arguments
+ *
+ * @return:    size of sent data on success, a negative value on error
+ */
+int sm_call_write(struct udevice *dev, void *buffer, size_t size,
+                 u32 cmd, struct pt_regs *args);
+
+#endif /* __SM_H__ */