]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
drivers: introduce Meson Secure Monitor driver
authorAlexey Romanov <avromanov@salutedevices.com>
Thu, 21 Sep 2023 08:13:39 +0000 (11:13 +0300)
committerNeil Armstrong <neil.armstrong@linaro.org>
Sun, 15 Oct 2023 10:23:48 +0000 (12:23 +0200)
This patch adds an implementation of the Meson Secure Monitor
driver based on UCLASS_SM.

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

index 7d5d05320c0f86d464c5a02fde53b23f0e9736d6..16b17fdd96d1065d6241a1f8418c522700b122d6 100644 (file)
@@ -161,6 +161,7 @@ F:  drivers/net/phy/meson-gxl.c
 F:     drivers/adc/meson-saradc.c
 F:     drivers/phy/meson*
 F:     drivers/mmc/meson_gx_mmc.c
+F:     drivers/sm/meson-sm.c
 F:     drivers/spi/meson_spifc.c
 F:     drivers/pinctrl/meson/
 F:     drivers/power/domain/meson-gx-pwrc-vpu.c
index 6cc6d55578eb8c4b8f04cc49c3af5743ffd09f47..f0987275d2654f9d4b1294c940d6a90c1decc9e9 100644 (file)
@@ -1,2 +1,9 @@
 config SM
        bool "Enable Secure Monitor driver support"
+
+config MESON_SM
+       bool "Amlogic Secure Monitor driver"
+       select SM
+       default n
+       help
+         Say y here to enable the Amlogic secure monitor driver.
index af5f475c2b27ca3dba1c0a075201f48c3bd67314..da81ee898abfed0a802ee77cc51a2bc87e86bf1e 100644 (file)
@@ -2,3 +2,4 @@
 
 obj-y += sm-uclass.o
 obj-$(CONFIG_SANDBOX) += sandbox-sm.o
+obj-$(CONFIG_MESON_SM) += meson-sm.o
diff --git a/drivers/sm/meson-sm.c b/drivers/sm/meson-sm.c
new file mode 100644 (file)
index 0000000..25adaf4
--- /dev/null
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2023 SberDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.com>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <regmap.h>
+#include <sm.h>
+#include <sm-uclass.h>
+#include <stdlib.h>
+#include <syscon.h>
+#include <asm/ptrace.h>
+#include <asm/system.h>
+#include <meson/sm.h>
+#include <linux/bitfield.h>
+#include <linux/err.h>
+#include <linux/sizes.h>
+
+struct meson_sm_cmd {
+       u32 smc_id;
+};
+
+#define SET_CMD(index, id)     \
+       [index] = {             \
+               .smc_id = (id), \
+       }
+
+struct meson_sm_data {
+       u32 cmd_get_shmem_in;
+       u32 cmd_get_shmem_out;
+       unsigned int shmem_size;
+       struct meson_sm_cmd cmd[];
+};
+
+struct meson_sm_priv {
+       void *sm_shmem_in;
+       void *sm_shmem_out;
+       const struct meson_sm_data *data;
+};
+
+static unsigned long __meson_sm_call(u32 cmd, const struct pt_regs *args)
+{
+       struct pt_regs r = *args;
+
+       r.regs[0] = cmd;
+       smc_call(&r);
+
+       return r.regs[0];
+};
+
+static u32 meson_sm_get_cmd(const struct meson_sm_data *data,
+                           u32 cmd_index)
+{
+       struct meson_sm_cmd cmd;
+
+       if (cmd_index >= MESON_SMC_CMD_COUNT)
+               return 0;
+
+       cmd = data->cmd[cmd_index];
+       return cmd.smc_id;
+}
+
+static int meson_sm_call(struct udevice *dev, u32 cmd_index, s32 *retval,
+                        struct pt_regs *args)
+{
+       struct meson_sm_priv *priv = dev_get_priv(dev);
+       u32 cmd, ret;
+
+       cmd = meson_sm_get_cmd(priv->data, cmd_index);
+       if (!cmd)
+               return -ENOENT;
+
+       ret = __meson_sm_call(cmd, args);
+       if (retval)
+               *retval = ret;
+
+       return 0;
+}
+
+static int meson_sm_call_read(struct udevice *dev, void *buffer, size_t size,
+                             u32 cmd_index, struct pt_regs *args)
+{
+       struct meson_sm_priv *priv = dev_get_priv(dev);
+       s32 nbytes;
+       int ret;
+
+       if (!buffer || size > priv->data->shmem_size)
+               return -EINVAL;
+
+       ret = meson_sm_call(dev, cmd_index, &nbytes, args);
+       if (ret)
+               return ret;
+
+       if (nbytes < 0 || nbytes > size)
+               return -ENOBUFS;
+
+       /* In some cases (for example GET_CHIP_ID command),
+        * SMC doesn't return the number of bytes read, even
+        * though the bytes were actually read into sm_shmem_out.
+        * So this check is needed.
+        */
+       ret = nbytes;
+       if (!nbytes)
+               nbytes = size;
+
+       memcpy(buffer, priv->sm_shmem_out, nbytes);
+
+       return ret;
+}
+
+static int meson_sm_call_write(struct udevice *dev, void *buffer, size_t size,
+                              u32 cmd_index, struct pt_regs *args)
+{
+       struct meson_sm_priv *priv = dev_get_priv(dev);
+       s32 nbytes;
+       int ret;
+
+       if (!buffer || size > priv->data->shmem_size)
+               return -EINVAL;
+
+       memcpy(priv->sm_shmem_in, buffer, size);
+
+       ret = meson_sm_call(dev, cmd_index, &nbytes, args);
+       if (ret)
+               return ret;
+
+       if (nbytes <= 0 || nbytes > size)
+               return -EIO;
+
+       return nbytes;
+}
+
+static int meson_sm_probe(struct udevice *dev)
+{
+       struct meson_sm_priv *priv = dev_get_priv(dev);
+       struct pt_regs regs = { 0 };
+
+       priv->data = (struct meson_sm_data *)dev_get_driver_data(dev);
+       if (!priv->data)
+               return -EINVAL;
+
+       priv->sm_shmem_in =
+               (void *)__meson_sm_call(priv->data->cmd_get_shmem_in, &regs);
+
+       if (!priv->sm_shmem_in)
+               return -ENOMEM;
+
+       priv->sm_shmem_out =
+               (void *)__meson_sm_call(priv->data->cmd_get_shmem_out, &regs);
+
+       if (!priv->sm_shmem_out)
+               return -ENOMEM;
+
+       pr_debug("meson sm driver probed\n"
+                "shmem_in addr: 0x%p, shmem_out addr: 0x%p\n",
+                priv->sm_shmem_in,
+                priv->sm_shmem_out);
+
+       return 0;
+}
+
+static const struct meson_sm_data meson_sm_gxbb_data = {
+       .cmd_get_shmem_in  = 0x82000020,
+       .cmd_get_shmem_out = 0x82000021,
+       .shmem_size = SZ_4K,
+       .cmd = {
+               SET_CMD(MESON_SMC_CMD_EFUSE_READ,  0x82000030),
+               SET_CMD(MESON_SMC_CMD_EFUSE_WRITE, 0x82000031),
+               SET_CMD(MESON_SMC_CMD_CHIP_ID_GET, 0x82000044),
+               SET_CMD(MESON_SMC_CMD_PWRDM_SET,   0x82000093),
+       },
+};
+
+static const struct udevice_id meson_sm_ids[] = {
+       {
+               .compatible = "amlogic,meson-gxbb-sm",
+               .data = (ulong)&meson_sm_gxbb_data,
+       },
+       { }
+};
+
+static const struct sm_ops sm_ops = {
+       .sm_call = meson_sm_call,
+       .sm_call_read = meson_sm_call_read,
+       .sm_call_write = meson_sm_call_write,
+};
+
+U_BOOT_DRIVER(meson_sm) = {
+       .name = "meson_sm",
+       .id = UCLASS_SM,
+       .of_match = meson_sm_ids,
+       .probe = meson_sm_probe,
+       .priv_auto = sizeof(struct meson_sm_priv),
+       .ops = &sm_ops,
+};
diff --git a/include/meson/sm.h b/include/meson/sm.h
new file mode 100644 (file)
index 0000000..fbaab1f
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2023 SberDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.com>
+ */
+
+#ifndef __MESON_SM_CMD_H__
+#define __MESON_SM_CMD_H__
+
+enum meson_smc_cmd {
+       MESON_SMC_CMD_EFUSE_READ,  /* read efuse memory */
+       MESON_SMC_CMD_EFUSE_WRITE, /* write efuse memory */
+       MESON_SMC_CMD_CHIP_ID_GET, /* readh chip unique id */
+       MESON_SMC_CMD_PWRDM_SET,   /* do command at specified power domain */
+       MESON_SMC_CMD_COUNT,
+};
+
+#endif