blob: 004b26f75289aec46d9d37faa512e418fab2c3a6 [file] [log] [blame]
/*
* Copyright 2020-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_device.h"
#include "ethosu_buffer.h"
#include "ethosu_core_rpmsg.h"
#include "ethosu_capabilities.h"
#include "ethosu_inference.h"
#include "ethosu_cancel_inference.h"
#include "ethosu_network.h"
#include "ethosu_network_info.h"
#include "uapi/ethosu.h"
#include <linux/dma-mapping.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/of_reserved_mem.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
/****************************************************************************
* Defines
****************************************************************************/
/****************************************************************************
* Functions
****************************************************************************/
/* Incoming messages */
static int ethosu_handle_msg(struct rpmsg_device *rpdev,
void *data,
int len,
void *priv,
u32 src)
{
struct ethosu_device *edev = dev_get_drvdata(&rpdev->dev);
struct device *dev = &rpdev->dev;
struct ethosu_core_rpmsg *rpmsg = data;
int length = len - sizeof(rpmsg->header);
int ret;
dev_info(dev,
"Msg: magic=0x%08x, type=%u, msg_id=%llu",
rpmsg->header.magic, rpmsg->header.type, rpmsg->header.msg_id);
switch (rpmsg->header.type) {
case ETHOSU_CORE_MSG_ERR:
if (length != sizeof(rpmsg->error)) {
dev_warn(dev,
"Msg: Error message of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->error));
ret = -EBADMSG;
break;
}
rpmsg->error.msg[sizeof(rpmsg->error.msg) - 1] = '\0';
dev_warn(dev, "Msg: Error. type=%u, msg=\"%s\"",
rpmsg->error.type, rpmsg->error.msg);
ret = -EBADMSG;
break;
case ETHOSU_CORE_MSG_PING:
dev_info(dev, "Msg: Ping");
ret = ethosu_mailbox_pong(&edev->mailbox);
break;
case ETHOSU_CORE_MSG_PONG:
dev_info(dev, "Msg: Pong");
break;
case ETHOSU_CORE_MSG_INFERENCE_RSP:
if (length != sizeof(rpmsg->inf_rsp)) {
dev_warn(dev,
"Msg: Inference response of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->inf_rsp));
ret = -EBADMSG;
break;
}
dev_info(dev,
"Msg: Inference response. ofm_count=%u, status=%u",
rpmsg->inf_rsp.ofm_count, rpmsg->inf_rsp.status);
ethosu_inference_rsp(edev, rpmsg->header.msg_id,
&rpmsg->inf_rsp);
break;
case ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP:
if (length != sizeof(rpmsg->cancel_rsp)) {
dev_warn(dev,
"Msg: Cancel Inference response of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->cancel_rsp));
ret = -EBADMSG;
break;
}
dev_info(dev,
"Msg: Cancel Inference response. status=%u",
rpmsg->cancel_rsp.status);
ethosu_cancel_inference_rsp(edev, rpmsg->header.msg_id,
&rpmsg->cancel_rsp);
break;
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,
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);
}
break;
case ETHOSU_CORE_MSG_CAPABILITIES_RSP:
if (length != sizeof(rpmsg->cap_rsp)) {
dev_warn(dev,
"Msg: Capabilities response of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->cap_rsp));
ret = -EBADMSG;
break;
}
dev_info(dev,
"Msg: Capabilities response vs%hhu v%hhu.%hhu p%hhu av%hhu.%hhu.%hhu dv%hhu.%hhu.%hhu mcc%hhu csv%hhu cd%hhu",
rpmsg->cap_rsp.version_status,
rpmsg->cap_rsp.version_major,
rpmsg->cap_rsp.version_minor,
rpmsg->cap_rsp.product_major,
rpmsg->cap_rsp.arch_major_rev,
rpmsg->cap_rsp.arch_minor_rev,
rpmsg->cap_rsp.arch_patch_rev,
rpmsg->cap_rsp.driver_major_rev,
rpmsg->cap_rsp.driver_minor_rev,
rpmsg->cap_rsp.driver_patch_rev,
rpmsg->cap_rsp.macs_per_cc,
rpmsg->cap_rsp.cmd_stream_version,
rpmsg->cap_rsp.custom_dma);
ethosu_capability_rsp(edev, rpmsg->header.msg_id,
&rpmsg->cap_rsp);
break;
case ETHOSU_CORE_MSG_NETWORK_INFO_RSP:
if (length != sizeof(rpmsg->net_info_rsp)) {
dev_warn(dev,
"Msg: Network info response of incorrect size. size=%u, expected=%zu", length,
sizeof(rpmsg->net_info_rsp));
ret = -EBADMSG;
break;
}
dev_info(dev,
"Msg: Network info response. status=%u",
rpmsg->net_info_rsp.status);
ethosu_network_info_rsp(edev, rpmsg->header.msg_id,
&rpmsg->net_info_rsp);
break;
default:
/* This should not happen due to version checks */
dev_warn(dev, "Msg: Protocol error. type=%u",
rpmsg->header.type);
ret = -EPROTO;
break;
}
return ret;
}
static int ethosu_open(struct inode *inode,
struct file *file)
{
struct ethosu_device *edev =
container_of(inode->i_cdev, struct ethosu_device, cdev);
file->private_data = edev;
dev_info(edev->dev, "Device open. file=0x%pK", file);
return nonseekable_open(inode, file);
}
static long ethosu_ioctl(struct file *file,
unsigned int cmd,
unsigned long arg)
{
struct ethosu_device *edev = file->private_data;
void __user *udata = (void __user *)arg;
int ret = -EINVAL;
ret = mutex_lock_interruptible(&edev->mutex);
if (ret)
return ret;
dev_info(edev->dev, "Device ioctl. file=0x%pK, cmd=0x%x, arg=0x%lx",
file, cmd, arg);
switch (cmd) {
case ETHOSU_IOCTL_VERSION_REQ:
dev_info(edev->dev, "Device ioctl: Send version request");
ret = ethosu_mailbox_version_request(&edev->mailbox);
break;
case ETHOSU_IOCTL_CAPABILITIES_REQ: {
struct ethosu_uapi_device_capabilities uapi;
dev_info(edev->dev,
"Device ioctl: Send capabilities request");
ret = ethosu_capabilities_request(edev, &uapi);
if (ret)
break;
ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
break;
}
case ETHOSU_IOCTL_PING: {
dev_info(edev->dev, "Device ioctl: Send ping");
ret = ethosu_mailbox_ping(&edev->mailbox);
break;
}
case ETHOSU_IOCTL_BUFFER_CREATE: {
struct ethosu_uapi_buffer_create uapi;
if (copy_from_user(&uapi, udata, sizeof(uapi)))
break;
dev_info(edev->dev,
"Device ioctl: Buffer create. capacity=%u",
uapi.capacity);
ret = ethosu_buffer_create(edev, uapi.capacity);
break;
}
case ETHOSU_IOCTL_NETWORK_CREATE: {
struct ethosu_uapi_network_create uapi;
if (copy_from_user(&uapi, udata, sizeof(uapi)))
break;
dev_info(edev->dev,
"Device ioctl: Network create. type=%u, fd/index=%u",
uapi.type, uapi.fd);
ret = ethosu_network_create(edev, &uapi);
break;
}
default: {
dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
cmd, arg);
break;
}
}
mutex_unlock(&edev->mutex);
return ret;
}
static struct rpmsg_endpoint *ethosu_create_ept(struct rpmsg_device *rpdev)
{
struct device *dev = &rpdev->dev;
struct rpmsg_channel_info info;
struct rpmsg_endpoint *ept;
/* Create rpmsg endpoint */
strncpy(info.name, rpdev->id.name, sizeof(info.name));
info.src = 0;
info.dst = rpdev->dst;
dev_info(dev, "Creating rpmsg endpoint. name=%s, src=%u, dst=%u",
info.name, info.src, info.dst);
ept = rpmsg_create_ept(rpdev, ethosu_handle_msg, NULL, info);
if (!ept) {
dev_err(&rpdev->dev, "Failed to create endpoint");
return ERR_PTR(-EINVAL);
}
return ept;
}
int ethosu_dev_init(struct ethosu_device *edev,
struct rpmsg_device *rpdev,
struct class *class,
dev_t devt)
{
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = &ethosu_open,
.unlocked_ioctl = &ethosu_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = &ethosu_ioctl,
#endif
};
struct device *sysdev;
int ret;
edev->dev = &rpdev->dev;
edev->rpdev = rpdev;
edev->class = class;
edev->devt = devt;
mutex_init(&edev->mutex);
edev->ept = ethosu_create_ept(rpdev);
if (IS_ERR(edev->ept))
return PTR_ERR(edev->ept);
ret = ethosu_mailbox_init(&edev->mailbox, edev->dev, edev->ept);
if (ret)
return ret;
cdev_init(&edev->cdev, &fops);
edev->cdev.owner = THIS_MODULE;
ret = cdev_add(&edev->cdev, edev->devt, 1);
if (ret) {
dev_err(edev->dev, "Failed to add character device.");
goto deinit_mailbox;
}
sysdev = device_create(edev->class, NULL, edev->devt, edev,
"ethosu%d", MINOR(edev->devt));
if (IS_ERR(sysdev)) {
dev_err(edev->dev, "Failed to create device.");
ret = PTR_ERR(sysdev);
goto del_cdev;
}
dev_info(edev->dev,
"Created Arm Ethos-U device. name=%s, major=%d, minor=%d",
dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
ethosu_mailbox_ping(&edev->mailbox);
return 0;
del_cdev:
cdev_del(&edev->cdev);
deinit_mailbox:
ethosu_mailbox_deinit(&edev->mailbox);
return ret;
}
void ethosu_dev_deinit(struct ethosu_device *edev)
{
dev_info(edev->dev, "%s\n", __FUNCTION__);
ethosu_mailbox_deinit(&edev->mailbox);
rpmsg_destroy_ept(edev->ept);
device_destroy(edev->class, edev->cdev.dev);
cdev_del(&edev->cdev);
}