Skip to content

Commit

Permalink
ipc: backends: Port IcMsg based backends to use pbuf
Browse files Browse the repository at this point in the history
Replace spsc_pbuf with pbuf implementation dedicated to
be used by IcMsg based backends.
The pbuf is written on top of simple read/write semantics
with minimal footprint and code complexity

Signed-off-by: Emil Obalski <Emil.Obalski@nordicsemi.no>
  • Loading branch information
Emil Obalski authored and Emil Obalski committed Oct 31, 2023
1 parent 2f4a443 commit 775e386
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 290 deletions.
4 changes: 4 additions & 0 deletions dts/bindings/ipc/zephyr,ipc-icmsg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ properties:
required: true
type: phandle

cache-line-sz:
description: Size of data cache line. Should be same as for remote.
type: int

mboxes:
description: phandle to the MBOX controller (TX and RX are required)
required: true
Expand Down
19 changes: 5 additions & 14 deletions include/zephyr/ipc/icmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/mbox.h>
#include <zephyr/ipc/ipc_service.h>
#include <zephyr/ipc/pbuf.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/spsc_pbuf.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -33,19 +33,16 @@ enum icmsg_state {
};

struct icmsg_config_t {
uintptr_t tx_shm_addr;
uintptr_t rx_shm_addr;
size_t tx_shm_size;
size_t rx_shm_size;
struct mbox_channel mbox_tx;
struct mbox_channel mbox_rx;
struct pbuf_cfg tx_pb;
struct pbuf_cfg rx_pb;
};

struct icmsg_data_t {
/* Tx/Rx buffers. */
struct spsc_pbuf *tx_ib;
struct spsc_pbuf *rx_ib;
atomic_t tx_buffer_state;
struct pbuf *tx_pb;
struct pbuf *rx_pb;
#ifdef CONFIG_IPC_SERVICE_ICMSG_SHMEM_ACCESS_SYNC
struct k_mutex tx_lock;
#endif
Expand All @@ -59,12 +56,6 @@ struct icmsg_data_t {
struct k_work_delayable notify_work;
struct k_work mbox_work;
atomic_t state;
/* No-copy */
#ifdef CONFIG_IPC_SERVICE_ICMSG_NOCOPY_RX
atomic_t rx_buffer_state;
const void *rx_buffer;
uint16_t rx_len;
#endif
};

/** @brief Open an icmsg instance
Expand Down
26 changes: 19 additions & 7 deletions subsys/ipc/ipc_service/backends/ipc_icmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,29 @@ static int backend_init(const struct device *instance)

#define DEFINE_BACKEND_DEVICE(i) \
static const struct icmsg_config_t backend_config_##i = { \
.tx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
.tx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
.rx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
.rx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
.mbox_tx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), tx), \
.mbox_rx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), rx), \
.tx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
.rx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
}; \
\
BUILD_ASSERT(DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)) > \
sizeof(struct spsc_pbuf)); \
static struct icmsg_data_t backend_data_##i; \
static struct pbuf tx_pb_##i = { \
.cfg = &backend_config_##i.tx_pb, \
}; \
static struct pbuf rx_pb_##i = { \
.cfg = &backend_config_##i.rx_pb, \
}; \
\
static struct icmsg_data_t backend_data_##i = { \
.tx_pb = &tx_pb_##i, \
.rx_pb = &rx_pb_##i, \
}; \
\
DEVICE_DT_INST_DEFINE(i, \
&backend_init, \
Expand Down
32 changes: 25 additions & 7 deletions subsys/ipc/ipc_service/backends/ipc_icmsg_me_follower.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,34 @@ static int backend_init(const struct device *instance)
}

#define DEFINE_BACKEND_DEVICE(i) \
static const struct icmsg_config_t backend_config_##i = \
{ \
.tx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
.tx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
.rx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
.rx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
static const struct icmsg_config_t backend_config_##i = { \
.mbox_tx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), tx), \
.mbox_rx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), rx), \
.tx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
.rx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
}; \
\
static struct pbuf tx_pb_##i = { \
.cfg = &backend_config_##i.tx_pb, \
}; \
static struct pbuf rx_pb_##i = { \
.cfg = &backend_config_##i.rx_pb, \
}; \
\
static struct backend_data_t backend_data_##i = { \
.icmsg_me_data = { \
.icmsg_data = { \
.tx_pb = &tx_pb_##i, \
.rx_pb = &rx_pb_##i, \
} \
} \
}; \
static struct backend_data_t backend_data_##i; \
\
DEVICE_DT_INST_DEFINE(i, \
&backend_init, \
Expand Down
32 changes: 25 additions & 7 deletions subsys/ipc/ipc_service/backends/ipc_icmsg_me_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,34 @@ static int backend_init(const struct device *instance)
}

#define DEFINE_BACKEND_DEVICE(i) \
static const struct icmsg_config_t backend_config_##i = \
{ \
.tx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
.tx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
.rx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
.rx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
static const struct icmsg_config_t backend_config_##i = { \
.mbox_tx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), tx), \
.mbox_rx = MBOX_DT_CHANNEL_GET(DT_DRV_INST(i), rx), \
.tx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, tx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, tx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
.rx_pb = PBUF_CFG_INIT( \
DT_REG_ADDR(DT_INST_PHANDLE(i, rx_region)), \
DT_REG_SIZE(DT_INST_PHANDLE(i, rx_region)), \
DT_INST_PROP_OR(i, cache_line_sz, 0)), \
}; \
\
static struct pbuf tx_pb_##i = { \
.cfg = &backend_config_##i.tx_pb, \
}; \
static struct pbuf rx_pb_##i = { \
.cfg = &backend_config_##i.rx_pb, \
}; \
\
static struct backend_data_t backend_data_##i = { \
.icmsg_me_data = { \
.icmsg_data = { \
.tx_pb = &tx_pb_##i, \
.rx_pb = &rx_pb_##i, \
} \
} \
}; \
static struct backend_data_t backend_data_##i; \
\
DEVICE_DT_INST_DEFINE(i, \
&backend_init, \
Expand Down
3 changes: 1 addition & 2 deletions subsys/ipc/ipc_service/lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ config IPC_SERVICE_STATIC_VRINGS_MEM_ALIGNMENT

menuconfig IPC_SERVICE_ICMSG
bool "icmsg IPC library"
select SPSC_PBUF
select SPSC_PBUF_USE_CACHE
select PBUF
help
Icmsg library

Expand Down
Loading

0 comments on commit 775e386

Please sign in to comment.