Skip to content

Commit 6f74e9d

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 8 bytes), otherwise the default size(512 bytes) will be used. Signed-off-by: anchao <anchao@pinecone.net>
1 parent a8d09b8 commit 6f74e9d

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

lib/include/openamp/rpmsg_virtio.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ extern "C" {
2828

2929
/* The feature bitmap for virtio rpmsg */
3030
#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+
METAL_PACKED_BEGIN
34+
struct virtio_rpmsg_config {
35+
/* The tx/rx individual buffer size(if VIRTIO_RPMSG_F_BUFSZ) */
36+
uint32_t txbuf_size;
37+
uint32_t rxbuf_size;
38+
uint32_t reserved[14]; /* Reserve for the future use */
39+
/* Put the customize config here */
40+
} METAL_PACKED_END;
3141

3242
/**
3343
* struct rpmsg_virtio_shm_pool - shared memory pool used for rpmsg buffers
@@ -44,6 +54,7 @@ struct rpmsg_virtio_shm_pool {
4454
/**
4555
* struct rpmsg_virtio_device - representation of a rpmsg device based on virtio
4656
* @rdev: rpmsg device, first property in the struct
57+
* @config: rpmsg config information
4758
* @vdev: pointer to the virtio device
4859
* @rvq: pointer to receive virtqueue
4960
* @svq: pointer to send virtqueue
@@ -52,6 +63,7 @@ struct rpmsg_virtio_shm_pool {
5263
*/
5364
struct rpmsg_virtio_device {
5465
struct rpmsg_device rdev;
66+
struct virtio_rpmsg_config config;
5567
struct virtio_device *vdev;
5668
struct virtqueue *rvq;
5769
struct virtqueue *svq;
@@ -85,6 +97,13 @@ rpmsg_virtio_get_features(struct rpmsg_virtio_device *rvdev)
8597
return rvdev->vdev->func->get_features(rvdev->vdev);
8698
}
8799

100+
static inline void
101+
rpmsg_virtio_read_config(struct rpmsg_virtio_device *rvdev,
102+
uint32_t offset, void *dst, int length)
103+
{
104+
rvdev->vdev->func->read_config(rvdev->vdev, offset, dst, length);
105+
}
106+
88107
static inline int
89108
rpmsg_virtio_create_virtqueues(struct rpmsg_virtio_device *rvdev,
90109
int flags, unsigned int nvqs,

lib/rpmsg/rpmsg_virtio.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
145145
data = virtqueue_get_buffer(rvdev->svq, len, idx);
146146
if (data == NULL) {
147147
data = rpmsg_virtio_shm_pool_get_buffer(rvdev->shpool,
148-
RPMSG_BUFFER_SIZE);
149-
*len = RPMSG_BUFFER_SIZE;
148+
rvdev->config.rxbuf_size);
149+
*len = rvdev->config.rxbuf_size;
150150
}
151151
}
152152
#endif /*!VIRTIO_SLAVE_ONLY*/
@@ -240,7 +240,7 @@ static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev)
240240
* If device role is Master then buffers are provided by us,
241241
* so just provide the macro.
242242
*/
243-
length = RPMSG_BUFFER_SIZE - sizeof(struct rpmsg_hdr);
243+
length = rvdev->config.rxbuf_size - sizeof(struct rpmsg_hdr);
244244
}
245245
#endif /*!VIRTIO_SLAVE_ONLY*/
246246

@@ -528,7 +528,10 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
528528
rvdev->vdev = vdev;
529529
rdev->ns_bind_cb = ns_bind_cb;
530530
vdev->priv = rvdev;
531+
rvdev->config.txbuf_size = RPMSG_BUFFER_SIZE;
532+
rvdev->config.rxbuf_size = RPMSG_BUFFER_SIZE;
531533
rdev->ops.send_offchannel_raw = rpmsg_virtio_send_offchannel_raw;
534+
532535
role = rpmsg_virtio_get_role(rvdev);
533536

534537
#ifndef VIRTIO_MASTER_ONLY
@@ -540,6 +543,13 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
540543
vdev->features = rpmsg_virtio_get_features(rvdev);
541544
rdev->support_ns = !!(vdev->features & (1 << VIRTIO_RPMSG_F_NS));
542545

546+
if (vdev->features & (1 << VIRTIO_RPMSG_F_BUFSZ)) {
547+
rpmsg_virtio_read_config(rvdev,
548+
0,
549+
&rvdev->config,
550+
sizeof(rvdev->config));
551+
}
552+
543553
#ifndef VIRTIO_SLAVE_ONLY
544554
if (role == RPMSG_MASTER) {
545555
/*
@@ -600,11 +610,11 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
600610
unsigned int idx;
601611
void *buffer;
602612

603-
vqbuf.len = RPMSG_BUFFER_SIZE;
613+
vqbuf.len = rvdev->config.txbuf_size;
604614
for (idx = 0; idx < rvdev->rvq->vq_nentries; idx++) {
605615
/* Initialize TX virtqueue buffers for remote device */
606616
buffer = rpmsg_virtio_shm_pool_get_buffer(shpool,
607-
RPMSG_BUFFER_SIZE);
617+
rvdev->config.txbuf_size);
608618

609619
if (!buffer) {
610620
return RPMSG_ERR_NO_BUFF;
@@ -615,7 +625,7 @@ int rpmsg_init_vdev(struct rpmsg_virtio_device *rvdev,
615625
metal_io_block_set(shm_io,
616626
metal_io_virt_to_offset(shm_io,
617627
buffer),
618-
0x00, RPMSG_BUFFER_SIZE);
628+
0x00, rvdev->config.txbuf_size);
619629
status =
620630
virtqueue_add_buffer(rvdev->rvq, &vqbuf, 0, 1,
621631
buffer);

0 commit comments

Comments
 (0)