rpmsg: virtio_rpmsg_bus: Get buffer size from config space

512 bytes isn't always suitable for all the cases, let firmware
maker decide the best value from the resource table. Enabled
by VIRTIO_RPMSG_F_BUFSZ feature bit.

Signed-off-by: Tanmay Shah <tanmay.shah@amd.com>
Acked-by: Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>
Link: https://lore.kernel.org/r/20260828145853.2843486-4-tanmay.shah@amd.com
Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>
This commit is contained in:
Tanmay Shah 2026-08-28 07:58:50 -07:00 committed by Mathieu Poirier
parent 8727c3f3e2
commit ca96ea25e5
2 changed files with 122 additions and 19 deletions

View File

@ -20,6 +20,7 @@
#include <linux/rpmsg.h>
#include <linux/rpmsg/byteorder.h>
#include <linux/rpmsg/ns.h>
#include <linux/rpmsg/virtio_rpmsg.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/sched.h>
@ -39,7 +40,8 @@
* @tx_bufs: kernel address of tx buffers
* @num_rx_buf: total number of rx buffers
* @num_tx_buf: total number of tx buffers
* @buf_size: size of one rx or tx buffer
* @rx_buf_size: size of one rx buffer
* @tx_buf_size: size of one tx buffer
* @last_tx_buf: index of last tx buffer used
* @bufs_dma: dma base addr of the buffers
* @tx_lock: protects svq and tx_bufs, to allow concurrent senders.
@ -59,7 +61,8 @@ struct virtproc_info {
void *rx_bufs, *tx_bufs;
unsigned int num_rx_buf;
unsigned int num_tx_buf;
unsigned int buf_size;
unsigned int rx_buf_size;
unsigned int tx_buf_size;
int last_tx_buf;
dma_addr_t bufs_dma;
struct mutex tx_lock;
@ -68,9 +71,6 @@ struct virtproc_info {
wait_queue_head_t sendq;
};
/* The feature bitmap for virtio rpmsg */
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
/**
* struct rpmsg_hdr - common header for all rpmsg messages
* @src: source address
@ -128,7 +128,7 @@ struct virtio_rpmsg_channel {
* processor.
*/
#define MAX_RPMSG_NUM_BUFS (256)
#define MAX_RPMSG_BUF_SIZE (512)
#define DEFAULT_RPMSG_BUF_SIZE (512)
/*
* Local addresses are dynamically allocated on-demand.
@ -443,7 +443,7 @@ static void *get_a_tx_buf(struct virtproc_info *vrp)
/* either pick the next unused tx buffer */
if (vrp->last_tx_buf < vrp->num_tx_buf)
ret = vrp->tx_bufs + vrp->buf_size * vrp->last_tx_buf++;
ret = vrp->tx_bufs + vrp->tx_buf_size * vrp->last_tx_buf++;
/* or recycle a used one */
else
ret = virtqueue_get_buf(vrp->svq, &len);
@ -513,7 +513,7 @@ static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
* messaging), or to improve the buffer allocator, to support
* variable-length buffer sizes.
*/
if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
if (len > vrp->tx_buf_size - sizeof(struct rpmsg_hdr)) {
dev_err(dev, "message is too big (%d)\n", len);
return -EMSGSIZE;
}
@ -646,7 +646,7 @@ static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept)
struct rpmsg_device *rpdev = ept->rpdev;
struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
return vch->vrp->tx_buf_size - sizeof(struct rpmsg_hdr);
}
static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
@ -672,7 +672,7 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
* We currently use fixed-sized buffers, so trivially sanitize
* the reported payload length.
*/
if (len > vrp->buf_size ||
if (len > vrp->rx_buf_size ||
msg_len > (len - sizeof(struct rpmsg_hdr))) {
dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
return -EINVAL;
@ -705,7 +705,7 @@ static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
dev_warn_ratelimited(dev, "msg received with no recipient\n");
/* publish the real size of the buffer */
rpmsg_sg_init(&sg, msg, vrp->buf_size);
rpmsg_sg_init(&sg, msg, vrp->rx_buf_size);
/* add the buffer back to the remote processor's virtqueue */
err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
@ -809,6 +809,65 @@ static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
device_unregister(&rpdev_ctrl->dev);
}
static int rpmsg_virtio_get_buf_size(struct virtproc_info *vrp)
{
struct virtio_device *vdev;
u8 version;
u16 size;
vdev = vrp->vdev;
/*
* If VIRTIO_RPMSG_F_BUFSZ feature is supported, then configure buf
* size from virtio device config space from the resource table.
* If the feature is not supported, then assign default buf size.
*/
if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_BUFSZ)) {
virtio_cread(vdev, struct virtio_rpmsg_config,
version, &version);
/* for now we support only v1 */
if (version != RPMSG_VDEV_CONFIG_V1) {
dev_err(&vdev->dev,
"unsupported vdev config version %u\n", version);
return -EINVAL;
}
/* size of the config space must match */
virtio_cread(vdev, struct virtio_rpmsg_config,
size, &size);
if (size != sizeof(struct virtio_rpmsg_config)) {
dev_err(&vdev->dev, "invalid size of vdev config %u\n",
size);
return -EINVAL;
}
/* note: tx and rx are defined from remote view */
virtio_cread(vdev, struct virtio_rpmsg_config,
txbuf_size, &vrp->rx_buf_size);
virtio_cread(vdev, struct virtio_rpmsg_config,
rxbuf_size, &vrp->tx_buf_size);
/* The buffers must hold at least the rpmsg header */
if (vrp->rx_buf_size < sizeof(struct rpmsg_hdr) ||
vrp->tx_buf_size < sizeof(struct rpmsg_hdr)) {
dev_err(&vdev->dev,
"bad vdev config: rx buf sz = %u, tx buf sz = %u\n",
vrp->rx_buf_size, vrp->tx_buf_size);
return -EINVAL;
}
dev_dbg(&vdev->dev,
"vdev config: ver=%u, rx sz = 0x%x, tx sz = 0x%x\n",
version, vrp->rx_buf_size, vrp->tx_buf_size);
} else {
vrp->rx_buf_size = DEFAULT_RPMSG_BUF_SIZE;
vrp->tx_buf_size = DEFAULT_RPMSG_BUF_SIZE;
}
return 0;
}
static int rpmsg_probe(struct virtio_device *vdev)
{
struct virtqueue_info vqs_info[] = {
@ -854,9 +913,12 @@ static int rpmsg_probe(struct virtio_device *vdev)
else
vrp->num_tx_buf = MAX_RPMSG_NUM_BUFS;
vrp->buf_size = MAX_RPMSG_BUF_SIZE;
err = rpmsg_virtio_get_buf_size(vrp);
if (err)
goto vqs_del;
total_buf_space = (vrp->num_rx_buf + vrp->num_tx_buf) * vrp->buf_size;
total_buf_space = (vrp->num_rx_buf * vrp->rx_buf_size) +
(vrp->num_tx_buf * vrp->tx_buf_size);
/* allocate coherent memory for the buffers */
bufs_va = dma_alloc_coherent(vdev->dev.parent,
@ -873,15 +935,14 @@ static int rpmsg_probe(struct virtio_device *vdev)
/* first part of the buffers is dedicated for RX */
vrp->rx_bufs = bufs_va;
/* and second part is dedicated for TX */
vrp->tx_bufs = bufs_va + vrp->num_rx_buf * vrp->buf_size;
vrp->tx_bufs = bufs_va + (vrp->num_rx_buf * vrp->rx_buf_size);
/* set up the receive buffers */
for (i = 0; i < vrp->num_rx_buf; i++) {
struct scatterlist sg;
void *cpu_addr = vrp->rx_bufs + i * vrp->buf_size;
void *cpu_addr = vrp->rx_bufs + i * vrp->rx_buf_size;
rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
rpmsg_sg_init(&sg, cpu_addr, vrp->rx_buf_size);
err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
GFP_KERNEL);
@ -964,8 +1025,8 @@ static int rpmsg_remove_device(struct device *dev, void *data)
static void rpmsg_remove(struct virtio_device *vdev)
{
struct virtproc_info *vrp = vdev->priv;
unsigned int num_bufs = vrp->num_rx_buf + vrp->num_tx_buf;
size_t total_buf_space = num_bufs * vrp->buf_size;
size_t total_buf_space = (vrp->num_rx_buf * vrp->rx_buf_size) +
(vrp->num_tx_buf * vrp->tx_buf_size);
int ret;
virtio_reset_device(vdev);
@ -991,6 +1052,7 @@ static struct virtio_device_id id_table[] = {
static unsigned int features[] = {
VIRTIO_RPMSG_F_NS,
VIRTIO_RPMSG_F_BUFSZ,
};
static struct virtio_driver virtio_ipc_driver = {

View File

@ -0,0 +1,41 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) Pinecone Inc. 2019
* Copyright (C) Xiang Xiao <xiaoxiang@pinecone.net>
* Copyright (C) Advanced Micro Devices, Inc. 2026
*/
#ifndef _LINUX_VIRTIO_RPMSG_H
#define _LINUX_VIRTIO_RPMSG_H
#include <linux/types.h>
#include <linux/virtio_types.h>
/* The feature bitmap for virtio rpmsg */
#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
#define VIRTIO_RPMSG_F_BUFSZ 1 /* RP advertises RX/TX buffer sizes from config space */
/* Version of struct virtio_rpmsg_config understood by this driver */
#define RPMSG_VDEV_CONFIG_V1 1
/**
* struct virtio_rpmsg_config - config space for rpmsg virtio device
*
* @version: version of this structure, currently %RPMSG_VDEV_CONFIG_V1.
* @size: size of this structure in bytes.
* @txbuf_size: Tx buf size from remote's view. For Linux this is rx buf size.
* @rxbuf_size: Rx buf size from remote's view. For Linux this is tx buf size.
*
* This is the configuration structure shared by the device and the driver,
* read when %VIRTIO_RPMSG_F_BUFSZ is negotiated. The fields are laid out so
* the structure is naturally 32-bit aligned.
*/
struct virtio_rpmsg_config {
u8 version;
__virtio16 size;
/* The tx/rx individual buffer size (if VIRTIO_RPMSG_F_BUFSZ) */
__virtio32 txbuf_size;
__virtio32 rxbuf_size;
} __packed;
#endif /* _LINUX_VIRTIO_RPMSG_H */