Skip to content

Commit d8e49b1

Browse files
anchaoxiaoxiang781216
authored andcommitted
Negotiate individual buffer size dynamically
If slave support VIRTIO_RPMSG_F_BUFSZ(0x04) feature, master determine the buffer size from config space(first 4 bytes), otherwise the default size(512 bytes) will be used. Signed-off-by: anchao <anchao@pinecone.net>
1 parent d5240e5 commit d8e49b1

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

lib/include/openamp/rpmsg_virtio.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ extern "C" {
2727
#endif
2828

2929
/* The feature bitmap for virtio rpmsg */
30-
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
30+
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
31+
#define VIRTIO_RPMSG_F_BUFSZ 2 /* RP supports get buffer size from config space */
32+
33+
/* The config space offset for virtio rpmsg */
34+
#define VIRTIO_RPMSG_BUFSZ_OFF 0 /* buffer size(4B) */
3135

3236
struct rpmsg_virtio_shm_pool;
3337
/**
@@ -52,6 +56,7 @@ struct rpmsg_virtio_shm_pool {
5256
* @shbuf_io: pointer to the shared buffer I/O region
5357
* @shpool: pointer to the shared buffers pool
5458
* @endpoints: list of endpoints.
59+
* @shbuf_size: size of each rpmsg buffer
5560
*/
5661
struct rpmsg_virtio_device {
5762
struct rpmsg_device rdev;
@@ -60,6 +65,7 @@ struct rpmsg_virtio_device {
6065
struct virtqueue *svq;
6166
struct metal_io_region *shbuf_io;
6267
struct rpmsg_virtio_shm_pool *shpool;
68+
uint32_t shbuf_size;
6369
};
6470

6571
#define RPMSG_REMOTE VIRTIO_DEV_SLAVE
@@ -87,6 +93,13 @@ static inline uint32_t
8793
return rvdev->vdev->func->get_features(rvdev->vdev);
8894
}
8995

96+
static inline void
97+
rpmsg_virtio_read_config(struct rpmsg_virtio_device *rvdev,
98+
uint32_t offset, void *dst, int length)
99+
{
100+
rvdev->vdev->func->read_config(rvdev->vdev, offset, dst, length);
101+
}
102+
90103
static inline int
91104
rpmsg_virtio_create_virtqueues(struct rpmsg_virtio_device *rvdev,
92105
int flags, unsigned int nvqs,

lib/rpmsg/rpmsg_virtio.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
151151
data = virtqueue_get_buffer(rvdev->svq, (uint32_t *)len, idx);
152152
if (data == NULL) {
153153
data = rpmsg_virtio_shm_pool_get_buffer(rvdev->shpool,
154-
RPMSG_BUFFER_SIZE);
155-
*len = RPMSG_BUFFER_SIZE;
154+
rvdev->shbuf_size);
155+
*len = rvdev->shbuf_size;
156156
}
157157
}
158158
#endif /*!VIRTIO_SLAVE_ONLY*/
@@ -258,7 +258,7 @@ static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev)
258258
* If device role is Remote then buffers are provided by us
259259
* (RPMSG Master), so just provide the macro.
260260
*/
261-
length = RPMSG_BUFFER_SIZE - sizeof(struct rpmsg_hdr);
261+
length = rvdev->shbuf_size - sizeof(struct rpmsg_hdr);
262262
}
263263
#endif /*!VIRTIO_SLAVE_ONLY*/
264264

@@ -545,6 +545,15 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
545545
rdev->ns_bind_cb = ns_bind_cb;
546546
vdev->priv = rvdev;
547547
rdev->ops.send_offchannel_raw = rpmsg_virtio_send_offchannel_raw;
548+
549+
if (vdev->features & (1 << VIRTIO_RPMSG_F_BUFSZ)) {
550+
rpmsg_virtio_read_config(rvdev,
551+
VIRTIO_RPMSG_BUFSZ_OFF,
552+
&rvdev->shbuf_size,
553+
sizeof(rvdev->shbuf_size));
554+
} else
555+
rvdev->shbuf_size = RPMSG_BUFFER_SIZE;
556+
548557
role = rpmsg_virtio_get_role(rvdev);
549558

550559
#ifndef VIRTIO_SLAVE_ONLY
@@ -608,11 +617,11 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
608617
unsigned int idx;
609618
void *buffer;
610619

611-
vqbuf.len = RPMSG_BUFFER_SIZE;
620+
vqbuf.len = rvdev->shbuf_size;
612621
for (idx = 0; idx < rvdev->rvq->vq_nentries; idx++) {
613622
/* Initialize TX virtqueue buffers for remote device */
614623
buffer = rpmsg_virtio_shm_pool_get_buffer(shpool,
615-
RPMSG_BUFFER_SIZE);
624+
rvdev->shbuf_size);
616625

617626
if (!buffer) {
618627
return RPMSG_ERR_NO_BUFF;
@@ -623,7 +632,7 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
623632
metal_io_block_set(shm_io,
624633
metal_io_virt_to_offset(shm_io,
625634
buffer),
626-
0x00, RPMSG_BUFFER_SIZE);
635+
0x00, rvdev->shbuf_size);
627636
status =
628637
virtqueue_add_buffer(rvdev->rvq, &vqbuf, 0, 1,
629638
buffer);

0 commit comments

Comments
 (0)