Skip to content

Commit a54d6a4

Browse files
aescolarrlubos
authored andcommitted
[nrf fromtree] ipc: icmsg & icbmsg: Add support for POSIX arch targets
Add support in this IPC backends for POSIX arch targets in general, and ensure the nrf5340bsim defines the buffer which will be used. Signed-off-by: Alberto Escolar Piedras <[email protected]> (cherry picked from commit b5b91b3)
1 parent 4e43269 commit a54d6a4

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

include/zephyr/ipc/pbuf.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ extern "C" {
3535
*/
3636
#define _PBUF_MIN_DATA_LEN ROUND_UP(PBUF_PACKET_LEN_SZ + 1 + _PBUF_IDX_SIZE, _PBUF_IDX_SIZE)
3737

38+
#if defined(CONFIG_ARCH_POSIX)
39+
/* For the native simulated boards we need to modify some pointers at init */
40+
#define PBUF_MAYBE_CONST
41+
#else
42+
#define PBUF_MAYBE_CONST const
43+
#endif
44+
3845
/** @brief Control block of packet buffer.
3946
*
4047
* The structure contains configuration data.
@@ -87,9 +94,9 @@ struct pbuf_data {
8794
* written in a way to protect the data from being corrupted.
8895
*/
8996
struct pbuf {
90-
const struct pbuf_cfg *const cfg; /* Configuration of the
91-
* buffer.
92-
*/
97+
PBUF_MAYBE_CONST struct pbuf_cfg *const cfg; /* Configuration of the
98+
* buffer.
99+
*/
93100
struct pbuf_data data; /* Data used to read and write
94101
* to the buffer
95102
*/
@@ -144,8 +151,7 @@ struct pbuf {
144151
"Misaligned memory."); \
145152
BUILD_ASSERT(size >= (MAX(dcache_align, _PBUF_IDX_SIZE) + _PBUF_IDX_SIZE + \
146153
_PBUF_MIN_DATA_LEN), "Insufficient size."); \
147-
\
148-
static const struct pbuf_cfg cfg_##name = \
154+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg_##name = \
149155
PBUF_CFG_INIT(mem_addr, size, dcache_align); \
150156
static struct pbuf name = { \
151157
.cfg = &cfg_##name, \

subsys/ipc/ipc_service/backends/ipc_icbmsg.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
#include <zephyr/ipc/ipc_service_backend.h>
8888
#include <zephyr/cache.h>
8989

90+
#if defined(CONFIG_ARCH_POSIX)
91+
#include <soc.h>
92+
#define MAYBE_CONST
93+
#else
94+
#define MAYBE_CONST const
95+
#endif
96+
9097
LOG_MODULE_REGISTER(ipc_icbmsg,
9198
CONFIG_IPC_SERVICE_BACKEND_ICBMSG_LOG_LEVEL);
9299

@@ -1238,12 +1245,17 @@ static int release_rx_buffer(const struct device *instance, void *token, void *d
12381245
*/
12391246
static int backend_init(const struct device *instance)
12401247
{
1241-
const struct icbmsg_config *conf = instance->config;
1248+
MAYBE_CONST struct icbmsg_config *conf = (struct icbmsg_config *)instance->config;
12421249
struct backend_data *dev_data = instance->data;
12431250
#ifdef CONFIG_MULTITHREADING
12441251
static K_THREAD_STACK_DEFINE(ep_bound_work_q_stack, EP_BOUND_WORK_Q_STACK_SIZE);
12451252
static bool is_work_q_started;
12461253

1254+
#if defined(CONFIG_ARCH_POSIX)
1255+
native_emb_addr_remap((void **)&conf->tx.blocks_ptr);
1256+
native_emb_addr_remap((void **)&conf->rx.blocks_ptr);
1257+
#endif
1258+
12471259
if (!is_work_q_started) {
12481260
k_work_queue_init(&ep_bound_work_q);
12491261
k_work_queue_start(&ep_bound_work_q, ep_bound_work_q_stack,
@@ -1401,7 +1413,7 @@ const static struct ipc_service_backend backend_ops = {
14011413
.rx_pb = &rx_icbmsg_pb_##i, \
14021414
} \
14031415
}; \
1404-
static const struct icbmsg_config backend_config_##i = \
1416+
static MAYBE_CONST struct icbmsg_config backend_config_##i = \
14051417
{ \
14061418
.control_config = { \
14071419
.mbox_tx = MBOX_DT_SPEC_INST_GET(i, tx), \

subsys/ipc/ipc_service/lib/pbuf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <zephyr/ipc/pbuf.h>
1212
#include <zephyr/sys/byteorder.h>
1313

14+
#if defined(CONFIG_ARCH_POSIX)
15+
#include <soc.h>
16+
#endif
17+
1418
/* Helper funciton for getting numer of bytes being written to the bufer. */
1519
static uint32_t idx_occupied(uint32_t len, uint32_t wr_idx, uint32_t rd_idx)
1620
{
@@ -54,11 +58,23 @@ static int validate_cfg(const struct pbuf_cfg *cfg)
5458
return 0;
5559
}
5660

61+
#if defined(CONFIG_ARCH_POSIX)
62+
void pbuf_native_addr_remap(struct pbuf *pb)
63+
{
64+
native_emb_addr_remap((void **)&pb->cfg->rd_idx_loc);
65+
native_emb_addr_remap((void **)&pb->cfg->wr_idx_loc);
66+
native_emb_addr_remap((void **)&pb->cfg->data_loc);
67+
}
68+
#endif
69+
5770
int pbuf_tx_init(struct pbuf *pb)
5871
{
5972
if (validate_cfg(pb->cfg) != 0) {
6073
return -EINVAL;
6174
}
75+
#if defined(CONFIG_ARCH_POSIX)
76+
pbuf_native_addr_remap(pb);
77+
#endif
6278

6379
/* Initialize local copy of indexes. */
6480
pb->data.wr_idx = 0;
@@ -82,6 +98,10 @@ int pbuf_rx_init(struct pbuf *pb)
8298
if (validate_cfg(pb->cfg) != 0) {
8399
return -EINVAL;
84100
}
101+
#if defined(CONFIG_ARCH_POSIX)
102+
pbuf_native_addr_remap(pb);
103+
#endif
104+
85105
/* Initialize local copy of indexes. */
86106
pb->data.wr_idx = 0;
87107
pb->data.rd_idx = 0;

tests/subsys/ipc/pbuf/src/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ZTEST(test_pbuf, test_rw)
4848
* order to avoid clang complains about memory_area not being constant
4949
* expression.
5050
*/
51-
static const struct pbuf_cfg cfg = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0);
51+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0);
5252

5353
static struct pbuf pb = {
5454
.cfg = &cfg,
@@ -115,9 +115,9 @@ ZTEST(test_pbuf, test_retcodes)
115115
* order to avoid clang complains about memory_area not being constant
116116
* expression.
117117
*/
118-
static const struct pbuf_cfg cfg0 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 32);
119-
static const struct pbuf_cfg cfg1 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0);
120-
static const struct pbuf_cfg cfg2 = PBUF_CFG_INIT(memory_area, 20, 4);
118+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg0 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 32);
119+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg1 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0);
120+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg2 = PBUF_CFG_INIT(memory_area, 20, 4);
121121

122122
static struct pbuf pb0 = {
123123
.cfg = &cfg0,
@@ -268,7 +268,7 @@ ZTEST(test_pbuf, test_stress)
268268
* order to avoid clang complains about buffer not being constant
269269
* expression.
270270
*/
271-
static const struct pbuf_cfg cfg = PBUF_CFG_INIT(buffer, MEM_AREA_SZ, 4);
271+
static PBUF_MAYBE_CONST struct pbuf_cfg cfg = PBUF_CFG_INIT(buffer, MEM_AREA_SZ, 4);
272272

273273
static struct pbuf pb = {
274274
.cfg = &cfg,

0 commit comments

Comments
 (0)