aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikael Olsson <mikael.olsson@arm.com>2023-08-03 12:41:48 +0200
committerMikael Olsson <mikael.olsson@arm.com>2023-08-09 15:09:38 +0200
commit7c843dc763a175269e810510af57b658ae81c529 (patch)
treee43f8f848e642abb6d448a9ba6399007fbf81bc7
parentfab31ebedaa0d2a3026ccb2de453d33780aed7c6 (diff)
downloadethos-u-linux-driver-stack-7c843dc763a175269e810510af57b658ae81c529.tar.gz
Add protocol version check during probing
To ensure that the Linux kernel NPU driver and the firmware used are compatible, the NPU driver will now request and verify the protocol version when setting up the device during the probing. Change-Id: I13bca96bf6f98ca90dc79e11ae637c269c2a5eec Signed-off-by: Mikael Olsson <mikael.olsson@arm.com>
-rw-r--r--kernel/Kbuild3
-rw-r--r--kernel/ethosu_device.c29
-rw-r--r--kernel/ethosu_mailbox.c12
-rw-r--r--kernel/ethosu_mailbox.h5
-rw-r--r--kernel/ethosu_version.c149
-rw-r--r--kernel/ethosu_version.h60
6 files changed, 236 insertions, 22 deletions
diff --git a/kernel/Kbuild b/kernel/Kbuild
index 867d6aa..4b76b0d 100644
--- a/kernel/Kbuild
+++ b/kernel/Kbuild
@@ -28,4 +28,5 @@ ethosu-objs := ethosu_driver.o \
ethosu_network.o \
ethosu_network_info.o \
ethosu_capabilities.o \
- ethosu_cancel_inference.o
+ ethosu_cancel_inference.o \
+ ethosu_version.o
diff --git a/kernel/ethosu_device.c b/kernel/ethosu_device.c
index 32d04ae..0df6c07 100644
--- a/kernel/ethosu_device.c
+++ b/kernel/ethosu_device.c
@@ -31,6 +31,7 @@
#include "ethosu_cancel_inference.h"
#include "ethosu_network.h"
#include "ethosu_network_info.h"
+#include "ethosu_version.h"
#include "uapi/ethosu.h"
#include <linux/dma-mapping.h>
@@ -145,26 +146,18 @@ static int ethosu_handle_rpmsg(struct rpmsg_device *rpdev,
case ETHOSU_CORE_MSG_VERSION_RSP:
if (length != sizeof(rpmsg->version_rsp)) {
dev_warn(dev,
- "Msg: Version response of incorrect size. size=%u, expected=%zu", length,
+ "Msg: Protocol version response of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->version_rsp));
ret = -EBADMSG;
break;
}
- dev_info(dev, "Msg: Version response v%u.%u.%u",
- rpmsg->version_rsp.major, rpmsg->version_rsp.minor,
- rpmsg->version_rsp.patch);
-
- /* Check major and minor version match, else return error */
- if (rpmsg->version_rsp.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
- rpmsg->version_rsp.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
- dev_warn(dev, "Msg: Version mismatch detected! ");
- dev_warn(dev, "Local version: v%u.%u.%u",
- ETHOSU_CORE_MSG_VERSION_MAJOR,
- ETHOSU_CORE_MSG_VERSION_MINOR,
- ETHOSU_CORE_MSG_VERSION_PATCH);
- }
+ dev_dbg(dev, "Msg: Protocol version response %u.%u.%u",
+ rpmsg->version_rsp.major, rpmsg->version_rsp.minor,
+ rpmsg->version_rsp.patch);
+ ethosu_version_rsp(&edev->mailbox, rpmsg->header.msg_id,
+ &rpmsg->version_rsp);
break;
case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
if (length != sizeof(rpmsg->cap_rsp)) {
@@ -457,7 +450,13 @@ int ethosu_dev_init(struct rpmsg_device *rpdev,
if (ret)
goto free_rpmsg_ept;
- ethosu_mailbox_ping(&edev->mailbox);
+ device_lock(dev);
+ ret = ethosu_version_check_request(dev, &edev->mailbox);
+ device_unlock(dev);
+ if (ret) {
+ dev_err(dev, "Protocol version check failed: %d", ret);
+ goto deinit_mailbox;
+ }
device_lock(dev);
ret = ethosu_capabilities_request(dev, &edev->mailbox,
diff --git a/kernel/ethosu_mailbox.c b/kernel/ethosu_mailbox.c
index 5b105d8..4f7f5b7 100644
--- a/kernel/ethosu_mailbox.c
+++ b/kernel/ethosu_mailbox.c
@@ -134,15 +134,19 @@ int ethosu_mailbox_pong(struct ethosu_mailbox *mbox)
return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
}
-int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox)
+int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox,
+ struct ethosu_mailbox_msg *msg)
{
struct ethosu_core_rpmsg rpmsg = {
- .header = {
- .magic = ETHOSU_CORE_MSG_MAGIC,
- .type = ETHOSU_CORE_MSG_VERSION_REQ,
+ .header = {
+ .magic = ETHOSU_CORE_MSG_MAGIC,
+ .type = ETHOSU_CORE_MSG_VERSION_REQ,
+ .msg_id = msg->id
}
};
+ msg->type = rpmsg.header.type;
+
return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
}
diff --git a/kernel/ethosu_mailbox.h b/kernel/ethosu_mailbox.h
index ea4409f..a3e2c14 100644
--- a/kernel/ethosu_mailbox.h
+++ b/kernel/ethosu_mailbox.h
@@ -140,11 +140,12 @@ int ethosu_mailbox_ping(struct ethosu_mailbox *mbox);
int ethosu_mailbox_pong(struct ethosu_mailbox *mbox);
/**
- * ethosu_mailbox_version_response - Send version request
+ * ethosu_mailbox_version_request() - Send protocol version request
*
* Return: 0 on succes, else error code
*/
-int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox);
+int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox,
+ struct ethosu_mailbox_msg *msg);
/**
* ethosu_mailbox_capabilities_request() - Send capabilities request
diff --git a/kernel/ethosu_version.c b/kernel/ethosu_version.c
new file mode 100644
index 0000000..1a92dc2
--- /dev/null
+++ b/kernel/ethosu_version.c
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2023 Arm Limited and/or its affiliates
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms
+ * of such GNU licence.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you can access it online at
+ * http://www.gnu.org/licenses/gpl-2.0.html.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+/****************************************************************************
+ * Includes
+ ****************************************************************************/
+
+#include "ethosu_version.h"
+
+#include "ethosu_core_rpmsg.h"
+
+#include <linux/errno.h>
+
+/****************************************************************************
+ * Defines
+ ****************************************************************************/
+
+#define VERSION_RESP_TIMEOUT_MS 2000
+
+/****************************************************************************
+ * Functions
+ ****************************************************************************/
+
+static void ethosu_version_fail(struct ethosu_mailbox_msg *msg)
+{
+ struct ethosu_version *version =
+ container_of(msg, typeof(*version), msg);
+
+ if (completion_done(&version->done))
+ return;
+
+ version->errno = -EFAULT;
+ complete(&version->done);
+}
+
+void ethosu_version_rsp(struct ethosu_mailbox *mailbox,
+ int msg_id,
+ struct ethosu_core_msg_version_rsp *rsp)
+{
+ struct device *dev = mailbox->dev;
+ struct ethosu_mailbox_msg *msg;
+ struct ethosu_version *version;
+
+ msg = ethosu_mailbox_find(mailbox, msg_id,
+ ETHOSU_CORE_MSG_VERSION_REQ);
+ if (IS_ERR(msg)) {
+ dev_warn(dev,
+ "Id for version msg not found. Id=0x%0x: %ld\n",
+ msg_id, PTR_ERR(msg));
+
+ return;
+ }
+
+ version = container_of(msg, typeof(*version), msg);
+
+ if (completion_done(&version->done))
+ return;
+
+ if (rsp->major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
+ rsp->minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
+ dev_warn(dev,
+ "Msg: Protocol version mismatch. Expected %u.%u.X but got %u.%u.%u",
+ ETHOSU_CORE_MSG_VERSION_MAJOR,
+ ETHOSU_CORE_MSG_VERSION_MINOR,
+ rsp->major, rsp->minor, rsp->patch);
+ version->errno = -EPROTO;
+ } else {
+ version->errno = 0;
+ }
+
+ complete(&version->done);
+}
+
+int ethosu_version_check_request(struct device *dev,
+ struct ethosu_mailbox *mailbox)
+{
+ struct ethosu_version *version;
+ int ret;
+ int timeout;
+
+ version = devm_kzalloc(dev, sizeof(*version), GFP_KERNEL);
+ if (!version)
+ return -ENOMEM;
+
+ version->dev = dev;
+ init_completion(&version->done);
+ version->msg.fail = ethosu_version_fail;
+
+ ret = ethosu_mailbox_register(mailbox, &version->msg);
+ if (ret < 0)
+ goto free_version;
+
+ dev_dbg(dev, "Protocol version request created. Id=0x%x, handle=%pK\n",
+ version->msg.id, version);
+
+ ret = ethosu_mailbox_version_request(mailbox, &version->msg);
+ if (ret)
+ goto deregister;
+
+ /* Unlock the mutex to not block other messages while waiting */
+ device_unlock(dev);
+
+ /* Wait for version response */
+ timeout = wait_for_completion_timeout(&version->done,
+ msecs_to_jiffies(
+ VERSION_RESP_TIMEOUT_MS));
+
+ /* Take back the mutex before resuming to do anything */
+ device_lock(dev);
+
+ if (0 == timeout) {
+ dev_warn(dev, "Protocol version response timeout");
+ ret = -ETIME;
+ goto deregister;
+ }
+
+ if (version->errno) {
+ ret = version->errno;
+ goto deregister;
+ }
+
+deregister:
+ ethosu_mailbox_deregister(mailbox, &version->msg);
+
+free_version:
+ dev_dbg(dev, "Protocol version destroy. Id=0x%x, handle=%pK\n",
+ version->msg.id,
+ version);
+ devm_kfree(dev, version);
+
+ return ret;
+}
diff --git a/kernel/ethosu_version.h b/kernel/ethosu_version.h
new file mode 100644
index 0000000..c5c02ad
--- /dev/null
+++ b/kernel/ethosu_version.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2023 Arm Limited and/or its affiliates
+ *
+ * This program is free software and is provided to you under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation, and any use by you of this program is subject to the terms
+ * of such GNU licence.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you can access it online at
+ * http://www.gnu.org/licenses/gpl-2.0.html.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#ifndef ETHOSU_VERSION_H
+#define ETHOSU_VERSION_H
+
+/****************************************************************************
+ * Includes
+ ****************************************************************************/
+
+#include "ethosu_mailbox.h"
+
+#include <linux/types.h>
+#include <linux/completion.h>
+
+/****************************************************************************
+ * Types
+ ****************************************************************************/
+
+struct ethosu_core_msg_version_rsp;
+
+/**
+ * struct ethosu_version - Protocol version internal struct
+ */
+struct ethosu_version {
+ struct device *dev;
+ struct completion done;
+ struct ethosu_mailbox_msg msg;
+ int errno;
+};
+
+/****************************************************************************
+ * Functions
+ ****************************************************************************/
+
+void ethosu_version_rsp(struct ethosu_mailbox *mailbox,
+ int msg_id,
+ struct ethosu_core_msg_version_rsp *rsp);
+
+int ethosu_version_check_request(struct device *dev,
+ struct ethosu_mailbox *mailbox);
+
+#endif /* ETHOSU_VERSION_H */