2019-11-02 18:30:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/rptun/rptun.c
|
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-11-02 18:30:33 +01:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-02 18:30:33 +01:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-11-02 18:30:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2019-11-03 14:28:17 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
2023-07-24 17:27:23 +02:00
|
|
|
#include <stdbool.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2019-11-03 14:28:17 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
#include <nuttx/arch.h>
|
2021-12-24 09:33:07 +01:00
|
|
|
#include <nuttx/board.h>
|
2019-11-02 18:30:33 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/kthread.h>
|
2022-05-29 17:59:14 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2022-02-09 16:08:22 +01:00
|
|
|
#include <nuttx/power/pm.h>
|
2024-01-10 07:20:15 +01:00
|
|
|
#include <nuttx/rptun/rptun.h>
|
|
|
|
#include <nuttx/semaphore.h>
|
2021-12-28 13:13:28 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2019-11-02 18:30:33 +01:00
|
|
|
#include <metal/utilities.h>
|
2023-12-12 03:53:45 +01:00
|
|
|
#include <openamp/remoteproc_loader.h>
|
|
|
|
#include <openamp/remoteproc_virtio.h>
|
2024-01-10 07:20:15 +01:00
|
|
|
#include <rpmsg/rpmsg_internal.h>
|
2022-02-16 08:23:49 +01:00
|
|
|
|
2019-11-03 14:28:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
#ifndef ALIGN_UP
|
2022-02-09 16:08:22 +01:00
|
|
|
# define ALIGN_UP(s, a) (((s) + (a) - 1) & ~((a) - 1))
|
2019-11-02 18:30:33 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-09 16:08:22 +01:00
|
|
|
#define RPTUNIOC_NONE 0
|
2021-07-15 09:47:42 +02:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct rptun_priv_s
|
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
struct rpmsg_s rpmsg;
|
|
|
|
struct rpmsg_virtio_device rvdev;
|
2019-11-02 18:30:33 +01:00
|
|
|
FAR struct rptun_dev_s *dev;
|
|
|
|
struct remoteproc rproc;
|
2022-07-21 16:26:51 +02:00
|
|
|
struct rpmsg_virtio_shm_pool pool[2];
|
2022-04-24 16:51:10 +02:00
|
|
|
sem_t semtx;
|
|
|
|
sem_t semrx;
|
2022-03-14 10:23:02 +01:00
|
|
|
pid_t tid;
|
2022-02-09 16:08:22 +01:00
|
|
|
#ifdef CONFIG_RPTUN_PM
|
|
|
|
bool stay;
|
|
|
|
#endif
|
2019-11-02 18:30:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rptun_store_s
|
|
|
|
{
|
2019-11-03 14:33:04 +01:00
|
|
|
struct file file;
|
|
|
|
FAR char *buf;
|
2019-11-02 18:30:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct remoteproc *rptun_init(FAR struct remoteproc *rproc,
|
2022-01-25 07:16:49 +01:00
|
|
|
FAR const struct remoteproc_ops *ops,
|
2019-11-02 18:30:33 +01:00
|
|
|
FAR void *arg);
|
|
|
|
static void rptun_remove(FAR struct remoteproc *rproc);
|
2020-05-21 06:24:56 +02:00
|
|
|
static int rptun_config(struct remoteproc *rproc, void *data);
|
2019-11-02 18:30:33 +01:00
|
|
|
static int rptun_start(FAR struct remoteproc *rproc);
|
|
|
|
static int rptun_stop(FAR struct remoteproc *rproc);
|
|
|
|
static int rptun_notify(FAR struct remoteproc *rproc, uint32_t id);
|
2020-11-12 14:00:49 +01:00
|
|
|
static FAR struct remoteproc_mem *
|
|
|
|
rptun_get_mem(FAR struct remoteproc *rproc,
|
|
|
|
FAR const char *name,
|
|
|
|
metal_phys_addr_t pa,
|
|
|
|
metal_phys_addr_t da,
|
|
|
|
FAR void *va, size_t size,
|
|
|
|
FAR struct remoteproc_mem *buf);
|
2022-07-21 16:26:51 +02:00
|
|
|
static int rptun_notify_wait(FAR struct remoteproc *rproc, uint32_t id);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
static int rptun_dev_start(FAR struct remoteproc *rproc);
|
2023-07-24 17:27:23 +02:00
|
|
|
static int rptun_dev_stop(FAR struct remoteproc *rproc, bool stop_ns);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2021-09-02 08:47:31 +02:00
|
|
|
#ifdef CONFIG_RPTUN_LOADER
|
2019-11-02 18:30:33 +01:00
|
|
|
static int rptun_store_open(FAR void *store_, FAR const char *path,
|
|
|
|
FAR const void **img_data);
|
|
|
|
static void rptun_store_close(FAR void *store_);
|
|
|
|
static int rptun_store_load(FAR void *store_, size_t offset,
|
|
|
|
size_t size, FAR const void **data,
|
|
|
|
metal_phys_addr_t pa,
|
|
|
|
FAR struct metal_io_region *io,
|
|
|
|
char is_blocking);
|
2021-09-02 08:47:31 +02:00
|
|
|
#endif
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
static metal_phys_addr_t rptun_pa_to_da(FAR struct rptun_dev_s *dev,
|
|
|
|
metal_phys_addr_t pa);
|
|
|
|
static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
|
|
|
|
metal_phys_addr_t da);
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
static int rptun_wait(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
|
|
|
|
static int rptun_post(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem);
|
2023-12-28 09:44:49 +01:00
|
|
|
static int rptun_ioctl(FAR struct rpmsg_s *rpmsg, int cmd,
|
|
|
|
unsigned long arg);
|
2024-01-11 08:23:19 +01:00
|
|
|
static void rptun_panic_(FAR struct rpmsg_s *rpmsg);
|
|
|
|
static void rptun_dump(FAR struct rpmsg_s *rpmsg);
|
2024-06-19 13:54:54 +02:00
|
|
|
static FAR const char *rptun_get_local_cpuname(FAR struct rpmsg_s *rpmsg);
|
2024-01-02 08:54:44 +01:00
|
|
|
static FAR const char *rptun_get_cpuname(FAR struct rpmsg_s *rpmsg);
|
|
|
|
static int rptun_get_tx_buffer_size(FAR struct rpmsg_s *rpmsg);
|
|
|
|
static int rptun_get_rx_buffer_size(FAR struct rpmsg_s *rpmsg);
|
2023-12-12 03:53:45 +01:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-25 07:16:49 +01:00
|
|
|
static const struct remoteproc_ops g_rptun_ops =
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
2022-07-21 16:26:51 +02:00
|
|
|
.init = rptun_init,
|
|
|
|
.remove = rptun_remove,
|
|
|
|
.config = rptun_config,
|
|
|
|
.start = rptun_start,
|
|
|
|
.stop = rptun_stop,
|
|
|
|
.notify = rptun_notify,
|
|
|
|
.get_mem = rptun_get_mem,
|
|
|
|
.notify_wait = rptun_notify_wait,
|
2019-11-02 18:30:33 +01:00
|
|
|
};
|
|
|
|
|
2021-09-02 08:47:31 +02:00
|
|
|
#ifdef CONFIG_RPTUN_LOADER
|
2023-12-12 03:53:45 +01:00
|
|
|
static const struct image_store_ops g_rptun_store_ops =
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
|
|
|
.open = rptun_store_open,
|
|
|
|
.close = rptun_store_close,
|
|
|
|
.load = rptun_store_load,
|
|
|
|
.features = SUPPORT_SEEK,
|
|
|
|
};
|
2021-09-02 08:47:31 +02:00
|
|
|
#endif
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
static const struct rpmsg_ops_s g_rptun_rpmsg_ops =
|
|
|
|
{
|
|
|
|
rptun_wait,
|
|
|
|
rptun_post,
|
2023-12-28 09:44:49 +01:00
|
|
|
rptun_ioctl,
|
2024-01-11 08:23:19 +01:00
|
|
|
rptun_panic_,
|
|
|
|
rptun_dump,
|
2024-06-19 13:54:54 +02:00
|
|
|
rptun_get_local_cpuname,
|
2024-01-02 08:54:44 +01:00
|
|
|
rptun_get_cpuname,
|
|
|
|
rptun_get_tx_buffer_size,
|
|
|
|
rptun_get_rx_buffer_size,
|
2023-12-12 03:53:45 +01:00
|
|
|
};
|
2022-06-06 05:52:23 +02:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-01-10 07:20:15 +01:00
|
|
|
static int rptun_buffer_nused(FAR struct rpmsg_virtio_device *rvdev, bool rx)
|
|
|
|
{
|
|
|
|
FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
|
|
|
|
uint16_t nused = vq->vq_ring.avail->idx - vq->vq_ring.used->idx;
|
|
|
|
|
|
|
|
if ((rpmsg_virtio_get_role(rvdev) == RPMSG_HOST) ^ rx)
|
|
|
|
{
|
|
|
|
return nused;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return vq->vq_nentries - nused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rptun_wakeup_tx(FAR struct rptun_priv_s *priv)
|
|
|
|
{
|
|
|
|
int semcount;
|
|
|
|
|
|
|
|
nxsem_get_value(&priv->semtx, &semcount);
|
|
|
|
while (semcount++ < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(&priv->semtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:08:22 +01:00
|
|
|
#ifdef CONFIG_RPTUN_PM
|
|
|
|
static inline void rptun_pm_action(FAR struct rptun_priv_s *priv,
|
|
|
|
bool stay)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
if (stay && !priv->stay)
|
|
|
|
{
|
2022-04-24 16:51:10 +02:00
|
|
|
pm_stay(PM_IDLE_DOMAIN, PM_IDLE);
|
2022-02-09 16:08:22 +01:00
|
|
|
priv->stay = true;
|
|
|
|
}
|
|
|
|
|
2022-02-16 08:23:49 +01:00
|
|
|
if (!stay && priv->stay && !rptun_buffer_nused(&priv->rvdev, false))
|
2022-02-09 16:08:22 +01:00
|
|
|
{
|
2022-04-24 16:51:10 +02:00
|
|
|
pm_relax(PM_IDLE_DOMAIN, PM_IDLE);
|
2022-02-09 16:08:22 +01:00
|
|
|
priv->stay = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# define rptun_pm_action(priv, stay)
|
|
|
|
#endif
|
|
|
|
|
2023-08-31 13:43:45 +02:00
|
|
|
static void rptun_start_worker(FAR void *arg)
|
2021-12-28 13:13:28 +01:00
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = arg;
|
|
|
|
|
2023-08-31 13:43:45 +02:00
|
|
|
if (priv->rproc.state == RPROC_OFFLINE)
|
2021-12-28 13:13:28 +01:00
|
|
|
{
|
2023-08-31 13:43:45 +02:00
|
|
|
rptun_dev_start(&priv->rproc);
|
2021-12-28 13:13:28 +01:00
|
|
|
}
|
2023-08-31 13:43:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rptun_worker(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = arg;
|
2021-12-28 13:13:28 +01:00
|
|
|
|
|
|
|
remoteproc_get_notification(&priv->rproc, RPTUN_NOTIFY_ALL);
|
2022-01-18 08:51:24 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
static int rptun_thread(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv;
|
|
|
|
|
2023-07-25 14:08:53 +02:00
|
|
|
priv = (FAR struct rptun_priv_s *)((uintptr_t)strtoul(argv[2], NULL, 16));
|
2023-02-01 11:05:58 +01:00
|
|
|
priv->tid = nxsched_gettid();
|
2021-11-23 13:12:55 +01:00
|
|
|
|
2023-08-31 13:43:45 +02:00
|
|
|
if (RPTUN_IS_AUTOSTART(priv->dev))
|
|
|
|
{
|
|
|
|
rptun_start_worker(priv);
|
|
|
|
}
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2022-04-24 16:51:10 +02:00
|
|
|
nxsem_wait_uninterruptible(&priv->semrx);
|
2021-12-28 13:13:28 +01:00
|
|
|
rptun_worker(priv);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:51:10 +02:00
|
|
|
static void rptun_wakeup_rx(FAR struct rptun_priv_s *priv)
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
2022-11-04 04:44:25 +01:00
|
|
|
int semcount;
|
|
|
|
|
|
|
|
nxsem_get_value(&priv->semrx, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(&priv->semrx);
|
|
|
|
}
|
2022-01-18 08:51:24 +01:00
|
|
|
}
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-01-18 08:51:24 +01:00
|
|
|
static bool rptun_is_recursive(FAR struct rptun_priv_s *priv)
|
|
|
|
{
|
2023-02-01 11:05:58 +01:00
|
|
|
return nxsched_gettid() == priv->tid;
|
2021-07-15 09:47:42 +02:00
|
|
|
}
|
2021-06-17 12:03:07 +02:00
|
|
|
|
2021-07-15 09:47:42 +02:00
|
|
|
static int rptun_callback(FAR void *arg, uint32_t vqid)
|
|
|
|
{
|
2022-04-24 16:51:10 +02:00
|
|
|
FAR struct rptun_priv_s *priv = arg;
|
|
|
|
FAR struct rpmsg_virtio_device *rvdev = &priv->rvdev;
|
|
|
|
FAR struct virtio_device *vdev = rvdev->vdev;
|
|
|
|
FAR struct virtqueue *svq = rvdev->svq;
|
|
|
|
FAR struct virtqueue *rvq = rvdev->rvq;
|
|
|
|
|
|
|
|
if (vqid == RPTUN_NOTIFY_ALL ||
|
|
|
|
vqid == vdev->vrings_info[rvq->vq_queue_index].notifyid)
|
|
|
|
{
|
2022-04-27 17:45:04 +02:00
|
|
|
if (rptun_buffer_nused(&priv->rvdev, true))
|
|
|
|
{
|
|
|
|
rptun_wakeup_rx(priv);
|
|
|
|
}
|
2022-04-24 16:51:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vqid == RPTUN_NOTIFY_ALL ||
|
|
|
|
vqid == vdev->vrings_info[svq->vq_queue_index].notifyid)
|
|
|
|
{
|
|
|
|
rptun_wakeup_tx(priv);
|
|
|
|
rptun_pm_action(priv, false);
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:03:07 +02:00
|
|
|
return OK;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static FAR struct remoteproc *rptun_init(FAR struct remoteproc *rproc,
|
2022-01-25 07:16:49 +01:00
|
|
|
FAR const struct remoteproc_ops *ops,
|
2019-11-02 18:30:33 +01:00
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
rproc->ops = ops;
|
|
|
|
rproc->priv = arg;
|
|
|
|
|
|
|
|
return rproc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rptun_remove(FAR struct remoteproc *rproc)
|
|
|
|
{
|
|
|
|
rproc->priv = NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-21 06:24:56 +02:00
|
|
|
static int rptun_config(struct remoteproc *rproc, void *data)
|
|
|
|
{
|
|
|
|
struct rptun_priv_s *priv = rproc->priv;
|
|
|
|
|
|
|
|
if (RPTUN_IS_MASTER(priv->dev))
|
|
|
|
{
|
|
|
|
return RPTUN_CONFIG(priv->dev, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
static int rptun_start(FAR struct remoteproc *rproc)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
|
|
|
|
|
|
|
if (RPTUN_IS_MASTER(priv->dev))
|
|
|
|
{
|
|
|
|
return RPTUN_START(priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rptun_stop(FAR struct remoteproc *rproc)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
|
|
|
|
|
|
|
if (RPTUN_IS_MASTER(priv->dev))
|
|
|
|
{
|
|
|
|
return RPTUN_STOP(priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rptun_notify(FAR struct remoteproc *rproc, uint32_t id)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
2022-02-09 16:08:22 +01:00
|
|
|
FAR struct rpmsg_virtio_device *rvdev = &priv->rvdev;
|
|
|
|
FAR struct virtqueue *vq = rvdev->svq;
|
|
|
|
|
|
|
|
if (rvdev->vdev && vq &&
|
|
|
|
rvdev->vdev->vrings_info[vq->vq_queue_index].notifyid == id)
|
|
|
|
{
|
|
|
|
rptun_pm_action(priv, true);
|
|
|
|
}
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-04-24 16:51:10 +02:00
|
|
|
RPTUN_NOTIFY(priv->dev, id);
|
2019-11-02 18:30:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:00:49 +01:00
|
|
|
static FAR struct remoteproc_mem *
|
|
|
|
rptun_get_mem(FAR struct remoteproc *rproc,
|
|
|
|
FAR const char *name,
|
|
|
|
metal_phys_addr_t pa,
|
|
|
|
metal_phys_addr_t da,
|
|
|
|
FAR void *va, size_t size,
|
|
|
|
FAR struct remoteproc_mem *buf)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
|
|
|
|
|
|
|
metal_list_init(&buf->node);
|
2022-05-07 09:35:05 +02:00
|
|
|
strlcpy(buf->name, name ? name : "", RPROC_MAX_NAME_LEN);
|
2020-11-12 14:00:49 +01:00
|
|
|
buf->io = metal_io_get_region();
|
|
|
|
buf->size = size;
|
|
|
|
|
|
|
|
if (pa != METAL_BAD_PHYS)
|
|
|
|
{
|
|
|
|
buf->pa = pa;
|
|
|
|
buf->da = rptun_pa_to_da(priv->dev, pa);
|
|
|
|
}
|
|
|
|
else if (da != METAL_BAD_PHYS)
|
|
|
|
{
|
|
|
|
buf->pa = rptun_da_to_pa(priv->dev, da);
|
|
|
|
buf->da = da;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->pa = metal_io_virt_to_phys(buf->io, va);
|
|
|
|
buf->da = rptun_pa_to_da(priv->dev, buf->pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->pa == METAL_BAD_PHYS || buf->da == METAL_BAD_PHYS)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:26:51 +02:00
|
|
|
static int rptun_notify_wait(FAR struct remoteproc *rproc, uint32_t id)
|
2021-11-23 13:12:55 +01:00
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
2022-01-18 08:51:24 +01:00
|
|
|
|
|
|
|
if (!rptun_is_recursive(priv))
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait to wakeup */
|
|
|
|
|
2022-04-24 16:51:10 +02:00
|
|
|
nxsem_wait(&priv->semtx);
|
2022-01-18 08:51:24 +01:00
|
|
|
rptun_worker(priv);
|
|
|
|
|
|
|
|
return 0;
|
2021-11-23 13:12:55 +01:00
|
|
|
}
|
|
|
|
|
2024-01-10 07:20:15 +01:00
|
|
|
static void rptun_dump_buffer(FAR struct rpmsg_virtio_device *rvdev,
|
|
|
|
bool rx)
|
|
|
|
{
|
|
|
|
FAR struct virtqueue *vq = rx ? rvdev->rvq : rvdev->svq;
|
|
|
|
FAR void *addr;
|
|
|
|
int desc_idx;
|
|
|
|
int num;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num = rptun_buffer_nused(rvdev, rx);
|
|
|
|
metal_log(METAL_LOG_EMERGENCY,
|
|
|
|
" %s buffer, total %d, pending %d\n",
|
|
|
|
rx ? "RX" : "TX", vq->vq_nentries, num);
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
if ((rpmsg_virtio_get_role(rvdev) == RPMSG_HOST) ^ rx)
|
|
|
|
{
|
|
|
|
desc_idx = (vq->vq_ring.used->idx + i) & (vq->vq_nentries - 1);
|
|
|
|
desc_idx = vq->vq_ring.avail->ring[desc_idx];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
desc_idx = (vq->vq_ring.avail->idx + i) & (vq->vq_nentries - 1);
|
|
|
|
desc_idx = vq->vq_ring.used->ring[desc_idx].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = metal_io_phys_to_virt(vq->shm_io,
|
|
|
|
vq->vq_ring.desc[desc_idx].addr);
|
|
|
|
if (addr)
|
|
|
|
{
|
|
|
|
FAR struct rpmsg_hdr *hdr = addr;
|
|
|
|
FAR struct rpmsg_endpoint *ept;
|
|
|
|
|
|
|
|
ept = rpmsg_get_ept_from_addr(&rvdev->rdev,
|
|
|
|
rx ? hdr->dst : hdr->src);
|
|
|
|
if (ept)
|
|
|
|
{
|
|
|
|
metal_log(METAL_LOG_EMERGENCY,
|
|
|
|
" %s buffer %p hold by %s\n",
|
|
|
|
rx ? "RX" : "TX", hdr, ept->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
static int rptun_wait(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem)
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
int ret;
|
2022-06-06 10:38:11 +02:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
if (!rptun_is_recursive(priv))
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
return nxsem_wait_uninterruptible(sem);
|
|
|
|
}
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ret = nxsem_trywait(sem);
|
|
|
|
if (ret >= 0)
|
2022-06-06 10:38:11 +02:00
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
break;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
nxsem_wait(&priv->semtx);
|
|
|
|
rptun_worker(priv);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
2022-06-06 10:38:11 +02:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
return ret;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
static int rptun_post(FAR struct rpmsg_s *rpmsg, FAR sem_t *sem)
|
2021-07-20 11:43:46 +02:00
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
int semcount;
|
|
|
|
int ret;
|
2021-07-20 11:43:46 +02:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
nxsem_get_value(sem, &semcount);
|
|
|
|
ret = nxsem_post(sem);
|
2021-07-20 11:43:46 +02:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
if (priv && semcount >= 0)
|
2021-07-20 11:43:46 +02:00
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
rptun_wakeup_tx(priv);
|
2021-07-20 11:43:46 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
return ret;
|
2021-07-20 11:43:46 +02:00
|
|
|
}
|
|
|
|
|
2023-12-28 09:44:49 +01:00
|
|
|
static int rptun_ioctl(FAR struct rpmsg_s *rpmsg, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2024-01-11 08:23:19 +01:00
|
|
|
case RPTUNIOC_START:
|
2023-12-28 09:44:49 +01:00
|
|
|
if (priv->rproc.state == RPROC_OFFLINE)
|
|
|
|
{
|
|
|
|
ret = rptun_dev_start(&priv->rproc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = rptun_dev_stop(&priv->rproc, false);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = rptun_dev_start(&priv->rproc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2024-01-11 08:23:19 +01:00
|
|
|
case RPTUNIOC_STOP:
|
2023-12-28 09:44:49 +01:00
|
|
|
ret = rptun_dev_stop(&priv->rproc, true);
|
|
|
|
break;
|
2024-01-11 08:23:19 +01:00
|
|
|
case RPTUNIOC_RESET:
|
2023-12-28 09:44:49 +01:00
|
|
|
RPTUN_RESET(priv->dev, arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-01-11 08:23:19 +01:00
|
|
|
static void rptun_panic_(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
|
|
|
|
RPTUN_PANIC(priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rptun_dump(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
FAR struct rpmsg_virtio_device *rvdev = &priv->rvdev;
|
|
|
|
FAR struct rpmsg_device *rdev = rpmsg->rdev;
|
|
|
|
FAR struct rpmsg_endpoint *ept;
|
|
|
|
FAR struct metal_list *node;
|
|
|
|
bool needlock = true;
|
|
|
|
|
|
|
|
if (!rvdev->vdev)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up_interrupt_context() || sched_idletask() ||
|
|
|
|
nxmutex_is_hold(&rdev->lock))
|
|
|
|
{
|
|
|
|
needlock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needlock)
|
|
|
|
{
|
|
|
|
metal_mutex_acquire(&rdev->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
metal_log(METAL_LOG_EMERGENCY,
|
|
|
|
"Dump rpmsg info between cpu (master: %s)%s <==> %s:\n",
|
|
|
|
rpmsg_virtio_get_role(rvdev) == RPMSG_HOST ? "yes" : "no",
|
|
|
|
CONFIG_RPMSG_LOCAL_CPUNAME, rpmsg_get_cpuname(rdev));
|
|
|
|
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, "rpmsg vq RX:\n");
|
|
|
|
virtqueue_dump(rvdev->rvq);
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, "rpmsg vq TX:\n");
|
|
|
|
virtqueue_dump(rvdev->svq);
|
|
|
|
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, " rpmsg ept list:\n");
|
|
|
|
|
|
|
|
metal_list_for_each(&rdev->endpoints, node)
|
|
|
|
{
|
|
|
|
ept = metal_container_of(node, struct rpmsg_endpoint, node);
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, " ept %s\n", ept->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, " rpmsg buffer list:\n");
|
|
|
|
|
|
|
|
rptun_dump_buffer(rvdev, true);
|
|
|
|
rptun_dump_buffer(rvdev, false);
|
|
|
|
|
|
|
|
if (needlock)
|
|
|
|
{
|
|
|
|
metal_mutex_release(&rdev->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_RPTUN_PM
|
|
|
|
metal_log(METAL_LOG_EMERGENCY, "rptun headrx %d\n", priv->headrx);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-06-19 13:54:54 +02:00
|
|
|
static FAR const char *rptun_get_local_cpuname(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
|
|
|
|
return RPTUN_GET_LOCAL_CPUNAME(priv->dev);
|
|
|
|
}
|
|
|
|
|
2024-01-02 08:54:44 +01:00
|
|
|
static FAR const char *rptun_get_cpuname(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = (FAR struct rptun_priv_s *)rpmsg;
|
|
|
|
|
|
|
|
return RPTUN_GET_CPUNAME(priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rptun_get_tx_buffer_size(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
return rpmsg_virtio_get_buffer_size(rpmsg->rdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rptun_get_rx_buffer_size(FAR struct rpmsg_s *rpmsg)
|
|
|
|
{
|
|
|
|
return rpmsg_virtio_get_rx_buffer_size(rpmsg->rdev);
|
|
|
|
}
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
static int rptun_dev_start(FAR struct remoteproc *rproc)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
|
|
|
FAR struct virtio_device *vdev;
|
|
|
|
FAR struct rptun_rsc_s *rsc;
|
|
|
|
unsigned int role = RPMSG_REMOTE;
|
|
|
|
int ret;
|
|
|
|
|
2022-06-06 05:52:23 +02:00
|
|
|
ret = remoteproc_config(rproc, NULL);
|
2019-11-11 16:09:40 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:47:31 +02:00
|
|
|
#ifdef CONFIG_RPTUN_LOADER
|
2019-11-02 18:30:33 +01:00
|
|
|
if (RPTUN_GET_FIRMWARE(priv->dev))
|
|
|
|
{
|
|
|
|
struct rptun_store_s store =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = remoteproc_load(rproc, RPTUN_GET_FIRMWARE(priv->dev),
|
2023-12-12 03:53:45 +01:00
|
|
|
&store, &g_rptun_store_ops, NULL);
|
2019-11-02 18:30:33 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsc = rproc->rsc_table;
|
|
|
|
}
|
|
|
|
else
|
2021-09-02 08:47:31 +02:00
|
|
|
#endif
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
|
|
|
rsc = RPTUN_GET_RESOURCE(priv->dev);
|
|
|
|
if (!rsc)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = remoteproc_set_rsc_table(rproc, (struct resource_table *)rsc,
|
|
|
|
sizeof(struct rptun_rsc_s));
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update resource table on MASTER side */
|
|
|
|
|
|
|
|
if (RPTUN_IS_MASTER(priv->dev))
|
|
|
|
{
|
|
|
|
uint32_t tbsz;
|
|
|
|
uint32_t v0sz;
|
|
|
|
uint32_t v1sz;
|
|
|
|
uint32_t shbufsz;
|
|
|
|
metal_phys_addr_t da0;
|
|
|
|
metal_phys_addr_t da1;
|
|
|
|
uint32_t align0;
|
|
|
|
uint32_t align1;
|
|
|
|
FAR void *va0;
|
|
|
|
FAR void *va1;
|
|
|
|
FAR void *shbuf;
|
2020-11-12 14:00:49 +01:00
|
|
|
FAR struct metal_io_region *io;
|
|
|
|
metal_phys_addr_t pa0;
|
|
|
|
metal_phys_addr_t pa1;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2021-12-08 05:20:35 +01:00
|
|
|
align0 = rsc->rpmsg_vring0.align;
|
|
|
|
align1 = rsc->rpmsg_vring1.align;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2019-11-11 16:11:17 +01:00
|
|
|
v0sz = ALIGN_UP(vring_size(rsc->rpmsg_vring0.num, align0), align0);
|
|
|
|
v1sz = ALIGN_UP(vring_size(rsc->rpmsg_vring1.num, align1), align1);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2024-02-06 03:20:56 +01:00
|
|
|
if (rsc->rpmsg_vring0.da == 0 ||
|
|
|
|
rsc->rpmsg_vring0.da == FW_RSC_U32_ADDR_ANY ||
|
|
|
|
rsc->rpmsg_vring1.da == 0 ||
|
|
|
|
rsc->rpmsg_vring1.da == FW_RSC_U32_ADDR_ANY)
|
2022-02-07 13:28:25 +01:00
|
|
|
{
|
|
|
|
tbsz = ALIGN_UP(sizeof(struct rptun_rsc_s), MAX(align0, align1));
|
2020-11-12 14:00:49 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
va0 = (FAR char *)rsc + tbsz;
|
|
|
|
va1 = (FAR char *)rsc + tbsz + v0sz;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
io = metal_io_get_region();
|
|
|
|
pa0 = metal_io_virt_to_phys(io, va0);
|
|
|
|
pa1 = metal_io_virt_to_phys(io, va1);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
da0 = da1 = METAL_BAD_PHYS;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
remoteproc_mmap(rproc, &pa0, &da0, v0sz, 0, NULL);
|
|
|
|
remoteproc_mmap(rproc, &pa1, &da1, v1sz, 0, NULL);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
rsc->rpmsg_vring0.da = da0;
|
|
|
|
rsc->rpmsg_vring1.da = da1;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-02-07 13:28:25 +01:00
|
|
|
shbuf = (FAR char *)rsc + tbsz + v0sz + v1sz;
|
2022-07-21 16:26:51 +02:00
|
|
|
shbufsz = rsc->config.r2h_buf_size * rsc->rpmsg_vring0.num +
|
|
|
|
rsc->config.h2r_buf_size * rsc->rpmsg_vring1.num;
|
2022-02-07 13:28:25 +01:00
|
|
|
|
2022-07-21 16:26:51 +02:00
|
|
|
rpmsg_virtio_init_shm_pool(priv->pool, shbuf, shbufsz);
|
2022-02-07 13:28:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
da0 = rsc->rpmsg_vring0.da;
|
|
|
|
shbuf = (FAR char *)remoteproc_mmap(rproc, NULL, &da0,
|
|
|
|
v0sz, 0, NULL) + v0sz;
|
2022-07-21 16:26:51 +02:00
|
|
|
shbufsz = rsc->config.r2h_buf_size * rsc->rpmsg_vring0.num;
|
|
|
|
rpmsg_virtio_init_shm_pool(&priv->pool[0], shbuf, shbufsz);
|
2022-02-07 13:28:25 +01:00
|
|
|
|
|
|
|
da1 = rsc->rpmsg_vring1.da;
|
|
|
|
shbuf = (FAR char *)remoteproc_mmap(rproc, NULL, &da1,
|
|
|
|
v1sz, 0, NULL) + v1sz;
|
2022-07-21 16:26:51 +02:00
|
|
|
shbufsz = rsc->config.h2r_buf_size * rsc->rpmsg_vring1.num;
|
|
|
|
rpmsg_virtio_init_shm_pool(&priv->pool[1], shbuf, shbufsz);
|
2022-02-07 13:28:25 +01:00
|
|
|
}
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-07-21 16:26:51 +02:00
|
|
|
role = RPMSG_HOST;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remote proc create */
|
|
|
|
|
|
|
|
vdev = remoteproc_create_virtio(rproc, 0, role, NULL);
|
|
|
|
if (!vdev)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:26:51 +02:00
|
|
|
if (priv->pool[1].base)
|
2022-02-07 13:28:25 +01:00
|
|
|
{
|
2022-07-21 16:26:51 +02:00
|
|
|
struct rpmsg_virtio_config config =
|
|
|
|
{
|
|
|
|
RPMSG_BUFFER_SIZE,
|
|
|
|
RPMSG_BUFFER_SIZE,
|
|
|
|
true,
|
|
|
|
};
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
ret = rpmsg_init_vdev_with_config(&priv->rvdev, vdev, rpmsg_ns_bind,
|
2022-07-21 16:26:51 +02:00
|
|
|
metal_io_get_region(),
|
|
|
|
priv->pool,
|
|
|
|
&config);
|
2022-02-07 13:28:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-12 03:53:45 +01:00
|
|
|
ret = rpmsg_init_vdev(&priv->rvdev, vdev, rpmsg_ns_bind,
|
2022-07-21 16:26:51 +02:00
|
|
|
metal_io_get_region(), priv->pool);
|
2022-02-07 13:28:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
remoteproc_remove_virtio(rproc, vdev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
priv->rvdev.rdev.ns_unbind_cb = rpmsg_ns_unbind;
|
2021-07-20 11:43:46 +02:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/* Remote proc start */
|
|
|
|
|
|
|
|
ret = remoteproc_start(rproc);
|
|
|
|
if (ret)
|
|
|
|
{
|
2023-08-31 13:43:45 +02:00
|
|
|
rpmsg_deinit_vdev(&priv->rvdev);
|
2019-11-02 18:30:33 +01:00
|
|
|
remoteproc_remove_virtio(rproc, vdev);
|
2023-08-31 13:43:45 +02:00
|
|
|
remoteproc_shutdown(rproc);
|
2019-11-02 18:30:33 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 08:51:24 +01:00
|
|
|
/* Register callback to mbox for receiving remote message */
|
|
|
|
|
|
|
|
RPTUN_REGISTER_CALLBACK(priv->dev, rptun_callback, priv);
|
2023-08-31 13:43:45 +02:00
|
|
|
rptun_wakeup_rx(priv);
|
2022-01-18 08:51:24 +01:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/* Broadcast device_created to all registers */
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
rpmsg_device_created(&priv->rpmsg);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
/* Open tx buffer return callback */
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2022-03-01 12:11:40 +01:00
|
|
|
virtqueue_enable_cb(priv->rvdev.svq);
|
2022-02-16 08:23:49 +01:00
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:27:23 +02:00
|
|
|
static int rptun_dev_stop(FAR struct remoteproc *rproc, bool stop_ns)
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv = rproc->priv;
|
2023-07-24 17:27:23 +02:00
|
|
|
FAR struct rpmsg_device *rdev = &priv->rvdev.rdev;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
drivers/rptun: check the status before stop remote proc
Race condition if the remote proc is stoped during initialization phase
| #0 0x0249f959 in rpmsg_destroy_ept (ept=0xffffffc0) at open-amp/lib/rpmsg/rpmsg.c:376
| #1 0x024a938c in rpmsg_deinit_vdev (rvdev=0xf2303a48) at open-amp/lib/rpmsg/rpmsg_virtio.c:971
| #2 0x02117e33 in rptun_dev_stop (rproc=0xf2303a04, stop_ns=true) at rptun/rptun.c:891
| #3 0x021181d8 in rptun_do_ioctl (priv=0xf2303a00, cmd=11010, arg=0) at rptun/rptun.c:922
| #4 0x02119722 in rptun_ioctl_foreach (cpuname=0x0, cmd=11010, value=0) at rptun/rptun.c:1086
| #5 0x0211b9df in rptun_poweroff (cpuname=0x0) at rptun/rptun.c:1378
| #6 0x02053aa6 in board_power_off (status=0) at sim/sim_head.c:206
| #7 0x0253d65c in boardctl (cmd=65283, arg=0) at boardctl.c:400
| #8 0x021eb497 in cmd_poweroff (vtbl=0xef606280, argc=1, argv=0xef9b73e0) at nsh_syscmds.c:356
| #9 0x021cdb4d in nsh_command (vtbl=0xef606280, argc=1, argv=0xef9b73e0) at nsh_command.c:1164
| #10 0x021baa72 in nsh_execute (vtbl=0xef606280, argc=1, argv=0xef9b73e0, redirfile=0x0, oflags=0) at nsh_parse.c:845
| #11 0x021c6b0a in nsh_parse_command (vtbl=0xef606280, cmdline=0xef606708 "poweroff") at nsh_parse.c:2744
| #12 0x021c7166 in nsh_parse (vtbl=0xef606280, cmdline=0xef606708 "poweroff") at nsh_parse.c:2828
| #13 0x0221fa2f in nsh_session (pstate=0xef606280, login=1, argc=1, argv=0xef7a7860) at nsh_session.c:245
| #14 0x021f8c04 in nsh_consolemain (argc=1, argv=0xef7a7860) at nsh_consolemain.c:75
| #15 0x021b77eb in nsh_main (argc=1, argv=0xef7a7860) at nsh_main.c:74
| #16 0x02166ddf in nxtask_startup (entrypt=0x21b76ca <nsh_main>, argc=1, argv=0xef7a7860) at sched/task_startup.c:70
| #17 0x020b363c in nxtask_start () at task/task_start.c:134
Signed-off-by: chao an <anchao@xiaomi.com>
2023-10-31 09:44:50 +01:00
|
|
|
if (priv->rproc.state == RPROC_OFFLINE)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (priv->rproc.state == RPROC_CONFIGURED ||
|
|
|
|
priv->rproc.state == RPROC_READY)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:27:23 +02:00
|
|
|
rdev->support_ns = stop_ns;
|
|
|
|
|
2019-11-02 18:30:33 +01:00
|
|
|
/* Unregister callback from mbox */
|
|
|
|
|
|
|
|
RPTUN_UNREGISTER_CALLBACK(priv->dev);
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
rpmsg_device_destory(&priv->rpmsg);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
/* Remote proc remove */
|
|
|
|
|
2022-01-14 16:25:49 +01:00
|
|
|
rpmsg_deinit_vdev(&priv->rvdev);
|
2023-07-04 11:24:06 +02:00
|
|
|
remoteproc_remove_virtio(rproc, priv->rvdev.vdev);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-07-24 17:27:23 +02:00
|
|
|
/* Remote proc stop and shutdown */
|
|
|
|
|
|
|
|
remoteproc_shutdown(rproc);
|
|
|
|
|
drivers/rptun: check the status before stop remote proc
Race condition if the remote proc is stoped during initialization phase
| #0 0x0249f959 in rpmsg_destroy_ept (ept=0xffffffc0) at open-amp/lib/rpmsg/rpmsg.c:376
| #1 0x024a938c in rpmsg_deinit_vdev (rvdev=0xf2303a48) at open-amp/lib/rpmsg/rpmsg_virtio.c:971
| #2 0x02117e33 in rptun_dev_stop (rproc=0xf2303a04, stop_ns=true) at rptun/rptun.c:891
| #3 0x021181d8 in rptun_do_ioctl (priv=0xf2303a00, cmd=11010, arg=0) at rptun/rptun.c:922
| #4 0x02119722 in rptun_ioctl_foreach (cpuname=0x0, cmd=11010, value=0) at rptun/rptun.c:1086
| #5 0x0211b9df in rptun_poweroff (cpuname=0x0) at rptun/rptun.c:1378
| #6 0x02053aa6 in board_power_off (status=0) at sim/sim_head.c:206
| #7 0x0253d65c in boardctl (cmd=65283, arg=0) at boardctl.c:400
| #8 0x021eb497 in cmd_poweroff (vtbl=0xef606280, argc=1, argv=0xef9b73e0) at nsh_syscmds.c:356
| #9 0x021cdb4d in nsh_command (vtbl=0xef606280, argc=1, argv=0xef9b73e0) at nsh_command.c:1164
| #10 0x021baa72 in nsh_execute (vtbl=0xef606280, argc=1, argv=0xef9b73e0, redirfile=0x0, oflags=0) at nsh_parse.c:845
| #11 0x021c6b0a in nsh_parse_command (vtbl=0xef606280, cmdline=0xef606708 "poweroff") at nsh_parse.c:2744
| #12 0x021c7166 in nsh_parse (vtbl=0xef606280, cmdline=0xef606708 "poweroff") at nsh_parse.c:2828
| #13 0x0221fa2f in nsh_session (pstate=0xef606280, login=1, argc=1, argv=0xef7a7860) at nsh_session.c:245
| #14 0x021f8c04 in nsh_consolemain (argc=1, argv=0xef7a7860) at nsh_consolemain.c:75
| #15 0x021b77eb in nsh_main (argc=1, argv=0xef7a7860) at nsh_main.c:74
| #16 0x02166ddf in nxtask_startup (entrypt=0x21b76ca <nsh_main>, argc=1, argv=0xef7a7860) at sched/task_startup.c:70
| #17 0x020b363c in nxtask_start () at task/task_start.c:134
Signed-off-by: chao an <anchao@xiaomi.com>
2023-10-31 09:44:50 +01:00
|
|
|
return OK;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2021-09-02 08:47:31 +02:00
|
|
|
#ifdef CONFIG_RPTUN_LOADER
|
2020-04-27 03:10:58 +02:00
|
|
|
static int rptun_store_open(FAR void *store_,
|
|
|
|
FAR const char *path,
|
2019-11-02 18:30:33 +01:00
|
|
|
FAR const void **img_data)
|
|
|
|
{
|
|
|
|
FAR struct rptun_store_s *store = store_;
|
|
|
|
int len = 0x100;
|
2019-11-03 14:33:04 +01:00
|
|
|
int ret;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-09-13 15:08:13 +02:00
|
|
|
ret = file_open(&store->file, path, O_RDONLY | O_CLOEXEC);
|
2019-11-03 14:33:04 +01:00
|
|
|
if (ret < 0)
|
2019-11-02 18:30:33 +01:00
|
|
|
{
|
2019-11-03 14:33:04 +01:00
|
|
|
return ret;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
store->buf = kmm_malloc(len);
|
|
|
|
if (!store->buf)
|
|
|
|
{
|
2019-11-03 14:33:04 +01:00
|
|
|
file_close(&store->file);
|
2019-11-02 18:30:33 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*img_data = store->buf;
|
|
|
|
|
2019-11-03 14:33:04 +01:00
|
|
|
return file_read(&store->file, store->buf, len);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rptun_store_close(FAR void *store_)
|
|
|
|
{
|
|
|
|
FAR struct rptun_store_s *store = store_;
|
|
|
|
|
|
|
|
kmm_free(store->buf);
|
2019-11-03 14:33:04 +01:00
|
|
|
file_close(&store->file);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rptun_store_load(FAR void *store_, size_t offset,
|
|
|
|
size_t size, FAR const void **data,
|
|
|
|
metal_phys_addr_t pa,
|
|
|
|
FAR struct metal_io_region *io,
|
|
|
|
char is_blocking)
|
|
|
|
{
|
|
|
|
FAR struct rptun_store_s *store = store_;
|
|
|
|
FAR char *tmp;
|
|
|
|
|
|
|
|
if (pa == METAL_BAD_PHYS)
|
|
|
|
{
|
|
|
|
tmp = kmm_realloc(store->buf, size);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
store->buf = tmp;
|
|
|
|
*data = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = metal_io_phys_to_virt(io, pa);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 14:33:04 +01:00
|
|
|
file_seek(&store->file, offset, SEEK_SET);
|
|
|
|
return file_read(&store->file, tmp, size);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
2021-09-02 08:47:31 +02:00
|
|
|
#endif
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
static metal_phys_addr_t rptun_pa_to_da(FAR struct rptun_dev_s *dev,
|
|
|
|
metal_phys_addr_t pa)
|
|
|
|
{
|
|
|
|
FAR const struct rptun_addrenv_s *addrenv;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
addrenv = RPTUN_GET_ADDRENV(dev);
|
|
|
|
if (!addrenv)
|
|
|
|
{
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
|
|
|
{
|
|
|
|
if (pa - addrenv[i].pa < addrenv[i].size)
|
|
|
|
{
|
|
|
|
return addrenv[i].da + (pa - addrenv[i].pa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
|
|
|
|
metal_phys_addr_t da)
|
|
|
|
{
|
|
|
|
FAR const struct rptun_addrenv_s *addrenv;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
addrenv = RPTUN_GET_ADDRENV(dev);
|
|
|
|
if (!addrenv)
|
|
|
|
{
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
|
|
|
{
|
|
|
|
if (da - addrenv[i].da < addrenv[i].size)
|
|
|
|
{
|
|
|
|
return addrenv[i].pa + (da - addrenv[i].da);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
2023-12-12 03:53:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2019-11-02 18:30:33 +01:00
|
|
|
|
|
|
|
int rptun_initialize(FAR struct rptun_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct rptun_priv_s *priv;
|
2019-11-10 15:45:14 +01:00
|
|
|
FAR char *argv[3];
|
2024-03-05 12:17:33 +01:00
|
|
|
char arg1[32];
|
2019-11-10 15:45:14 +01:00
|
|
|
char name[32];
|
2019-11-02 18:30:33 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv = kmm_zalloc(sizeof(struct rptun_priv_s));
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2024-03-05 12:17:33 +01:00
|
|
|
return -ENOMEM;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2021-07-15 09:47:42 +02:00
|
|
|
priv->dev = dev;
|
2024-03-05 12:17:33 +01:00
|
|
|
nxsem_init(&priv->semtx, 0, 0);
|
|
|
|
nxsem_init(&priv->semrx, 0, 0);
|
2021-07-15 09:47:42 +02:00
|
|
|
|
2021-12-28 13:13:28 +01:00
|
|
|
remoteproc_init(&priv->rproc, &g_rptun_ops, priv);
|
2021-07-15 09:47:42 +02:00
|
|
|
|
rptun: fix compile-time warning
Fix this compile-time warning:
rptun/rptun.c:956:22: warning: '%lx' directive output may be truncated writing between 1 and 16 bytes into a region of size 14 [-Wformat-truncation=]
956 | snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
| ^~~~~
rptun/rptun.c:956:25: note: format string is defined here
956 | snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
rptun/rptun.c:956:22: note: directive argument in the range [1, 18446744073709551615]
956 | snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
| ^~~~~
rptun/rptun.c:956:3: note: 'snprintf' output between 4 and 19 bytes into a destination of size 16
956 | snprintf(arg1, 16, "0x%" PRIxPTR, (uintptr_t)priv);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Eero Nurkkala <eero.nurkkala@offcode.fi>
2022-03-25 11:50:18 +01:00
|
|
|
snprintf(name, sizeof(name), "/dev/rptun/%s", RPTUN_GET_CPUNAME(dev));
|
2023-12-28 09:44:49 +01:00
|
|
|
ret = rpmsg_register(name, &priv->rpmsg, &g_rptun_rpmsg_ops);
|
2021-07-15 09:47:42 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err_driver;
|
|
|
|
}
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2024-03-05 12:17:33 +01:00
|
|
|
snprintf(arg1, sizeof(arg1), "%p", priv);
|
|
|
|
argv[0] = (FAR char *)RPTUN_GET_CPUNAME(dev);
|
2019-11-10 15:45:14 +01:00
|
|
|
argv[1] = arg1;
|
|
|
|
argv[2] = NULL;
|
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
ret = kthread_create("rptun", CONFIG_RPTUN_PRIORITY,
|
|
|
|
CONFIG_RPTUN_STACKSIZE, rptun_thread, argv);
|
2019-11-02 18:30:33 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-12-28 09:44:49 +01:00
|
|
|
goto err_thread;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 11:09:40 +01:00
|
|
|
/* Add priv to list */
|
|
|
|
|
2021-07-15 09:47:42 +02:00
|
|
|
return OK;
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2023-12-28 09:44:49 +01:00
|
|
|
err_thread:
|
|
|
|
nxsem_destroy(&priv->semtx);
|
|
|
|
nxsem_destroy(&priv->semrx);
|
|
|
|
rpmsg_unregister(name, &priv->rpmsg);
|
|
|
|
|
2021-07-15 09:47:42 +02:00
|
|
|
err_driver:
|
2024-03-05 12:17:33 +01:00
|
|
|
nxsem_destroy(&priv->semtx);
|
|
|
|
nxsem_destroy(&priv->semrx);
|
2021-07-15 09:47:42 +02:00
|
|
|
kmm_free(priv);
|
2019-11-02 18:30:33 +01:00
|
|
|
|
2021-07-15 09:47:42 +02:00
|
|
|
return ret;
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int rptun_boot(FAR const char *cpuname)
|
|
|
|
{
|
2024-01-11 08:23:19 +01:00
|
|
|
return rpmsg_ioctl(cpuname, RPTUNIOC_START, 0);
|
2019-11-02 18:30:33 +01:00
|
|
|
}
|
2021-12-20 08:52:00 +01:00
|
|
|
|
2023-08-31 13:43:45 +02:00
|
|
|
int rptun_poweroff(FAR const char *cpuname)
|
|
|
|
{
|
2024-01-11 08:23:19 +01:00
|
|
|
return rpmsg_ioctl(cpuname, RPTUNIOC_STOP, 0);
|
2023-08-31 13:43:45 +02:00
|
|
|
}
|
|
|
|
|
2021-12-24 09:33:07 +01:00
|
|
|
int rptun_reset(FAR const char *cpuname, int value)
|
2021-12-20 08:52:00 +01:00
|
|
|
{
|
2024-01-11 08:23:19 +01:00
|
|
|
return rpmsg_ioctl(cpuname, RPTUNIOC_RESET, value);
|
2021-12-20 08:52:00 +01:00
|
|
|
}
|