From 33408fa7af129f7e76aff680f568046ab0bd1944 Mon Sep 17 00:00:00 2001 From: Kamil Piszczek Date: Fri, 18 Mar 2022 14:46:42 +0100 Subject: [PATCH] ipc: backends: rpmsg: initialize shared memory to zero Added a code for initializing shared memory to zero. This operation normalizes the memory state so that the IPC service is no longer prone to reading status bits from the previous reset session. Signed-off-by: Kamil Piszczek --- subsys/ipc/ipc_service/backends/Kconfig.rpmsg | 9 ++++++++ .../backends/ipc_rpmsg_static_vrings.c | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/subsys/ipc/ipc_service/backends/Kconfig.rpmsg b/subsys/ipc/ipc_service/backends/Kconfig.rpmsg index b5104ca45a7..7bff068fd49 100644 --- a/subsys/ipc/ipc_service/backends/Kconfig.rpmsg +++ b/subsys/ipc/ipc_service/backends/Kconfig.rpmsg @@ -12,4 +12,13 @@ config IPC_SERVICE_BACKEND_RPMSG_WQ_STACK_SIZE prevent notifying service users about received data from the system work queue. Size is the same for all instances. +config IPC_SERVICE_BACKEND_RPMSG_SHMEM_RESET + bool "Reset shared memory state" + help + Some platforms retain the memory content upon reset. This is + problematic because the backend expects a zero-ed memory to be + able to correctly setup instances and endpoints at init time. + When this parameter is set to 'y' the status region of the shared + memory is reset on kernel initialization. + endif # IP_SERVICE_BACKEND_RPMSG diff --git a/subsys/ipc/ipc_service/backends/ipc_rpmsg_static_vrings.c b/subsys/ipc/ipc_service/backends/ipc_rpmsg_static_vrings.c index 0ef7e8bf73d..16f46ddddaf 100644 --- a/subsys/ipc/ipc_service/backends/ipc_rpmsg_static_vrings.c +++ b/subsys/ipc/ipc_service/backends/ipc_rpmsg_static_vrings.c @@ -508,3 +508,24 @@ static int backend_init(const struct device *instance) &backend_ops); DT_INST_FOREACH_STATUS_OKAY(DEFINE_BACKEND_DEVICE) + +#define BACKEND_CONFIG_INIT(n) &backend_config_##n, + +#if defined(CONFIG_IPC_SERVICE_BACKEND_RPMSG_SHMEM_RESET) +static int shared_memory_prepare(const struct device *arg) +{ + static const struct backend_config_t *config[] = { + DT_INST_FOREACH_STATUS_OKAY(BACKEND_CONFIG_INIT) + }; + + for (int i = 0; i < DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT); i++) { + if (config[i]->role == ROLE_HOST) { + memset((void *) config[i]->shm_addr, 0, VDEV_STATUS_SIZE); + } + } + + return 0; +} + +SYS_INIT(shared_memory_prepare, PRE_KERNEL_1, 1); +#endif /* CONFIG_IPC_SERVICE_BACKEND_RPMSG_SHMEM_RESET */