From c17bba369fc13ffcd4fdea9684ccef58710eb699 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Mon, 11 Mar 2024 15:05:16 -0500 Subject: [PATCH] lib: Use VIRTIO_{DRIVER,DEVICE}_SUPPORT to improve readability Currently compiler defines are defined when support for driver or device is the only support being built. This is a negative define, it surrounds the code to not be built and we use ifndef. This is confusing. It also leaves ifndefs all throughout the code-base. Instead, define a macro that is set to 1 if support is enabled. Use this inline in if statements where possible. Any sane compiler will optimize away the code in the branch when support is not enabled just the same as when using the preprocessor so we keep the same binary size. Signed-off-by: Andrew Davis --- cmake/options.cmake | 8 ++- lib/include/openamp/virtio.h | 10 --- lib/remoteproc/remoteproc.c | 14 ++-- lib/remoteproc/remoteproc_virtio.c | 22 +++--- lib/rpmsg/rpmsg_virtio.c | 110 ++++++++--------------------- lib/virtio/virtio.c | 4 +- lib/virtio/virtqueue.c | 68 ++++-------------- 7 files changed, 68 insertions(+), 168 deletions(-) diff --git a/cmake/options.cmake b/cmake/options.cmake index 35a3333fd..984f51bb6 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -71,11 +71,15 @@ if (WITH_VIRTIO_SLAVE) endif (WITH_VIRTIO_SLAVE) if (NOT WITH_VIRTIO_DRIVER AND NOT WITH_VIRTIO_MASTER) - add_definitions(-DVIRTIO_DEVICE_ONLY) + add_definitions(-DVIRTIO_DRIVER_SUPPORT=0) +else (NOT WITH_VIRTIO_DRIVER AND NOT WITH_VIRTIO_MASTER) + add_definitions(-DVIRTIO_DRIVER_SUPPORT=1) endif (NOT WITH_VIRTIO_DRIVER AND NOT WITH_VIRTIO_MASTER) if (NOT WITH_VIRTIO_DEVICE AND NOT WITH_VIRTIO_SLAVE) - add_definitions(-DVIRTIO_DRIVER_ONLY) + add_definitions(-DVIRTIO_DEVICE_SUPPORT=0) +else (NOT WITH_VIRTIO_DEVICE AND NOT WITH_VIRTIO_SLAVE) + add_definitions(-DVIRTIO_DEVICE_SUPPORT=1) endif (NOT WITH_VIRTIO_DEVICE AND NOT WITH_VIRTIO_SLAVE) option (WITH_VIRTIO_MMIO_DRV "Build with virtio mmio driver support enabled" OFF) diff --git a/lib/include/openamp/virtio.h b/lib/include/openamp/virtio.h index 2829ab7ca..8f8f577f5 100644 --- a/lib/include/openamp/virtio.h +++ b/lib/include/openamp/virtio.h @@ -85,16 +85,6 @@ __deprecated static inline int deprecated_virtio_dev_slave(void) return VIRTIO_DEV_DEVICE; } -#ifdef VIRTIO_MASTER_ONLY -#define VIRTIO_DRIVER_ONLY -#warning "VIRTIO_MASTER_ONLY is deprecated, please use VIRTIO_DRIVER_ONLY" -#endif - -#ifdef VIRTIO_SLAVE_ONLY -#define VIRTIO_DEVICE_ONLY -#warning "VIRTIO_SLAVE_ONLY is deprecated, please use VIRTIO_DEVICE_ONLY" -#endif - /** @brief Virtio device identifier. */ struct virtio_device_id { /** Virtio subsystem device ID. */ diff --git a/lib/remoteproc/remoteproc.c b/lib/remoteproc/remoteproc.c index a0df0bb1a..4062c6d43 100644 --- a/lib/remoteproc/remoteproc.c +++ b/lib/remoteproc/remoteproc.c @@ -926,13 +926,15 @@ remoteproc_create_virtio(struct remoteproc *rproc, unsigned int num_vrings, i; struct metal_list *node; -#ifdef VIRTIO_DRIVER_ONLY - role = (role != VIRTIO_DEV_DRIVER) ? 0xFFFFFFFFUL : role; -#endif + if (role == VIRTIO_DEV_DRIVER && !VIRTIO_DRIVER_SUPPORT) { + metal_log(METAL_LOG_ERROR, "VirtIO role is set to Driver, but Driver support is not enabled\n"); + return NULL; + } -#ifdef VIRTIO_DEVICE_ONLY - role = (role != VIRTIO_DEV_DEVICE) ? 0xFFFFFFFFUL : role; -#endif + if (role == VIRTIO_DEV_DEVICE && !VIRTIO_DEVICE_SUPPORT) { + metal_log(METAL_LOG_ERROR, "VirtIO role is set to Device, but Device support is not enabled\n"); + return NULL; + } if (!rproc || (role != VIRTIO_DEV_DEVICE && role != VIRTIO_DEV_DRIVER)) return NULL; diff --git a/lib/remoteproc/remoteproc_virtio.c b/lib/remoteproc/remoteproc_virtio.c index 5a2da82b3..8939dd654 100644 --- a/lib/remoteproc/remoteproc_virtio.c +++ b/lib/remoteproc/remoteproc_virtio.c @@ -55,14 +55,13 @@ static int rproc_virtio_create_virtqueue(struct virtio_device *vdev, if (!vring_info->vq) return ERROR_NO_MEM; -#ifndef VIRTIO_DEVICE_ONLY - if (vdev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER) { size_t offset = metal_io_virt_to_offset(vring_info->io, vring_alloc->vaddr); size_t size = vring_size(vring_alloc->num_descs, vring_alloc->align); metal_io_block_set(vring_info->io, offset, 0, size); } -#endif + ret = virtqueue_create(vdev, idx, name, vring_alloc, callback, vdev->func->notify, vring_info->vq); if (ret) @@ -144,7 +143,7 @@ static unsigned char rproc_virtio_get_status(struct virtio_device *vdev) return status; } -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT static void rproc_virtio_set_status(struct virtio_device *vdev, unsigned char status) { @@ -199,7 +198,7 @@ static uint32_t rproc_virtio_get_features(struct virtio_device *vdev) return dfeatures & gfeatures; } -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT static void rproc_virtio_set_features(struct virtio_device *vdev, uint32_t features) { @@ -249,7 +248,7 @@ static void rproc_virtio_read_config(struct virtio_device *vdev, } } -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT static void rproc_virtio_write_config(struct virtio_device *vdev, uint32_t offset, void *src, int length) { @@ -288,7 +287,7 @@ static const struct virtio_dispatch remoteproc_virtio_dispatch_funcs = { .read_config = rproc_virtio_read_config, .notify = rproc_virtio_virtqueue_notify, .wait_notified = rproc_virtio_wait_notified, -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT /* * We suppose here that the vdev is in a shared memory so that can * be access only by one core: the host. In this case salve core has @@ -343,13 +342,11 @@ rproc_virtio_create_vdev(unsigned int role, unsigned int notifyid, vdev->vrings_num = num_vrings; vdev->func = &remoteproc_virtio_dispatch_funcs; -#ifndef VIRTIO_DEVICE_ONLY - if (role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && role == VIRTIO_DEV_DRIVER) { uint32_t dfeatures = rproc_virtio_get_dfeatures(vdev); /* Assume the virtio driver support all remote features */ rproc_virtio_negotiate_features(vdev, dfeatures); } -#endif return &rpvdev->vdev; err: @@ -417,15 +414,14 @@ void rproc_virtio_wait_remote_ready(struct virtio_device *vdev) { uint8_t status; -#ifndef VIRTIO_DEVICE_ONLY /* * No status available for remote. As virtio driver has not to wait * remote action, we can return. Behavior should be updated * in future if a remote status is added. */ - if (vdev->role == VIRTIO_DEV_DRIVER) + if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER) return; -#endif + while (1) { status = rproc_virtio_get_status(vdev); if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK) diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index 66623e54b..4695025a6 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -58,7 +58,7 @@ struct vbuff_reclaimer_t { }; /* Default configuration */ -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT #define RPMSG_VIRTIO_DEFAULT_CONFIG \ (&(const struct rpmsg_virtio_config) { \ .h2r_buf_size = RPMSG_BUFFER_SIZE, \ @@ -69,7 +69,7 @@ struct vbuff_reclaimer_t { #define RPMSG_VIRTIO_DEFAULT_CONFIG NULL #endif -#ifndef VIRTIO_DEVICE_ONLY +#if VIRTIO_DRIVER_SUPPORT metal_weak void * rpmsg_virtio_shm_pool_get_buffer(struct rpmsg_virtio_shm_pool *shpool, size_t size) @@ -83,7 +83,7 @@ rpmsg_virtio_shm_pool_get_buffer(struct rpmsg_virtio_shm_pool *shpool, return buffer; } -#endif /*!VIRTIO_DEVICE_ONLY*/ +#endif /*VIRTIO_DRIVER_SUPPORT*/ void rpmsg_virtio_init_shm_pool(struct rpmsg_virtio_shm_pool *shpool, void *shb, size_t size) @@ -114,8 +114,7 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev, BUFFER_INVALIDATE(buffer, len); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { struct virtqueue_buf vqbuf; (void)idx; @@ -125,15 +124,12 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev, ret = virtqueue_add_buffer(rvdev->rvq, &vqbuf, 0, 1, buffer); RPMSG_ASSERT(ret == VQUEUE_SUCCESS, "add buffer failed\r\n"); } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { (void)buffer; ret = virtqueue_add_consumed_buffer(rvdev->rvq, idx, len); RPMSG_ASSERT(ret == VQUEUE_SUCCESS, "add consumed buffer failed\r\n"); } -#endif /*VIRTIO_DRIVER_ONLY*/ } /** @@ -156,8 +152,7 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev, BUFFER_FLUSH(buffer, len); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { struct virtqueue_buf vqbuf; (void)idx; @@ -166,14 +161,12 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev, vqbuf.len = len; return virtqueue_add_buffer(rvdev->svq, &vqbuf, 1, 0, buffer); } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { (void)buffer; return virtqueue_add_consumed_buffer(rvdev->svq, idx, len); } -#endif /*!VIRTIO_DRIVER_ONLY*/ + return 0; } @@ -204,16 +197,11 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev, data = r_desc; *idx = r_desc->idx; -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) *len = rvdev->config.h2r_buf_size; -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) *len = virtqueue_get_buffer_length(rvdev->svq, *idx); -#endif /*!VIRTIO_DRIVER_ONLY*/ -#ifndef VIRTIO_DEVICE_ONLY - } else if (role == RPMSG_HOST) { + } else if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { data = virtqueue_get_buffer(rvdev->svq, len, idx); if (!data && rvdev->svq->vq_free_cnt) { data = rpmsg_virtio_shm_pool_get_buffer(rvdev->shpool, @@ -221,11 +209,8 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev, *len = rvdev->config.h2r_buf_size; *idx = 0; } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - } else if (role == RPMSG_REMOTE) { + } else if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { data = virtqueue_get_available_buffer(rvdev->svq, idx, len); -#endif /*!VIRTIO_DRIVER_ONLY*/ } return data; @@ -248,18 +233,14 @@ static void *rpmsg_virtio_get_rx_buffer(struct rpmsg_virtio_device *rvdev, unsigned int role = rpmsg_virtio_get_role(rvdev); void *data = NULL; -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { data = virtqueue_get_buffer(rvdev->rvq, len, idx); } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { data = virtqueue_get_available_buffer(rvdev->rvq, idx, len); } -#endif /*!VIRTIO_DRIVER_ONLY*/ /* Invalidate the buffer before returning it */ if (data) @@ -268,7 +249,6 @@ static void *rpmsg_virtio_get_rx_buffer(struct rpmsg_virtio_device *rvdev, return data; } -#ifndef VIRTIO_DRIVER_ONLY /* * check if the remote is ready to start RPMsg communication */ @@ -289,7 +269,6 @@ static int rpmsg_virtio_wait_remote_ready(struct rpmsg_virtio_device *rvdev) metal_cpu_yield(); } } -#endif /*!VIRTIO_DRIVER_ONLY*/ /** * @internal @@ -455,11 +434,9 @@ static int rpmsg_virtio_send_offchannel_nocopy(struct rpmsg_device *rdev, metal_mutex_acquire(&rdev->lock); -#ifndef VIRTIO_DEVICE_ONLY - if (rpmsg_virtio_get_role(rvdev) == RPMSG_HOST) + if (VIRTIO_DRIVER_SUPPORT && rpmsg_virtio_get_role(rvdev) == RPMSG_HOST) buff_len = rvdev->config.h2r_buf_size; else -#endif /*!VIRTIO_DEVICE_ONLY*/ buff_len = virtqueue_get_buffer_length(rvdev->svq, idx); /* Enqueue buffer on virtqueue. */ @@ -717,18 +694,15 @@ int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev) rvdev = (struct rpmsg_virtio_device *)rdev; role = rpmsg_virtio_get_role(rvdev); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { /* * If device role is host then buffers are provided by us, * so just provide the macro. */ size = rvdev->config.h2r_buf_size - sizeof(struct rpmsg_hdr); } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { /* * If other core is host then buffers are provided by it, * so get the buffer size from the virtqueue. @@ -736,7 +710,6 @@ int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev) size = (int)virtqueue_get_desc_size(rvdev->svq) - sizeof(struct rpmsg_hdr); } -#endif /*!VIRTIO_DRIVER_ONLY*/ if (size <= 0) size = RPMSG_ERR_NO_BUFF; @@ -759,18 +732,15 @@ int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev) rvdev = (struct rpmsg_virtio_device *)rdev; role = rpmsg_virtio_get_role(rvdev); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { /* * If device role is host then buffers are provided by us, * so just provide the macro. */ size = rvdev->config.r2h_buf_size - sizeof(struct rpmsg_hdr); } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { /* * If other core is host then buffers are provided by it, * so get the buffer size from the virtqueue. @@ -778,7 +748,6 @@ int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev) size = (int)virtqueue_get_desc_size(rvdev->rvq) - sizeof(struct rpmsg_hdr); } -#endif /*!VIRTIO_DRIVER_ONLY*/ if (size <= 0) size = RPMSG_ERR_NO_BUFF; @@ -828,8 +797,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, rdev->ops.release_tx_buffer = rpmsg_virtio_release_tx_buffer; role = rpmsg_virtio_get_role(rvdev); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { /* * The virtio configuration contains only options applicable to * a virtio driver, implying rpmsg host role. @@ -839,23 +807,16 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, } rvdev->config = *config; } -#else /*!VIRTIO_DEVICE_ONLY*/ - /* Ignore passed config in the virtio-device-only configuration. */ - (void)config; -#endif /*!VIRTIO_DEVICE_ONLY*/ - -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { /* wait synchro with the host */ rpmsg_virtio_wait_remote_ready(rvdev); } -#endif /*!VIRTIO_DRIVER_ONLY*/ + vdev->features = rpmsg_virtio_get_features(rvdev); rdev->support_ns = !!(vdev->features & (1 << VIRTIO_RPMSG_F_NS)); -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { /* * Since device is RPMSG Remote so we need to manage the * shared buffers. Create shared memory pool to handle buffers. @@ -871,17 +832,14 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, callback[0] = rpmsg_virtio_rx_callback; callback[1] = rpmsg_virtio_tx_callback; } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - (void)shpool; - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { vq_names[0] = "tx_vq"; vq_names[1] = "rx_vq"; callback[0] = rpmsg_virtio_tx_callback; callback[1] = rpmsg_virtio_rx_callback; } -#endif /*!VIRTIO_DRIVER_ONLY*/ + rvdev->shbuf_io = shm_io; metal_list_init(&rvdev->reclaimer); @@ -892,19 +850,15 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, return status; /* Create virtqueue success, assign back the virtqueue */ -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { rvdev->rvq = vdev->vrings_info[0].vq; rvdev->svq = vdev->vrings_info[1].vq; } -#endif /*!VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (role == RPMSG_REMOTE) { + if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) { rvdev->rvq = vdev->vrings_info[1].vq; rvdev->svq = vdev->vrings_info[0].vq; } -#endif /*!VIRTIO_DRIVER_ONLY*/ /* * Suppress "tx-complete" interrupts @@ -920,8 +874,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, vq->shm_io = shm_io; } -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) { + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) { struct virtqueue_buf vqbuf; unsigned int idx; void *buffer; @@ -952,7 +905,6 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, } } } -#endif /*!VIRTIO_DEVICE_ONLY*/ /* Initialize channels and endpoints list */ metal_list_init(&rdev->endpoints); @@ -967,18 +919,14 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, rpmsg_virtio_ns_callback, NULL); } -#ifndef VIRTIO_DEVICE_ONLY - if (role == RPMSG_HOST) + if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) rpmsg_virtio_set_status(rvdev, VIRTIO_CONFIG_STATUS_DRIVER_OK); -#endif /*!VIRTIO_DEVICE_ONLY*/ return RPMSG_SUCCESS; -#ifndef VIRTIO_DEVICE_ONLY err: rpmsg_virtio_delete_virtqueues(rvdev); return status; -#endif /*!VIRTIO_DEVICE_ONLY*/ } void rpmsg_deinit_vdev(struct rpmsg_virtio_device *rvdev) diff --git a/lib/virtio/virtio.c b/lib/virtio/virtio.c index 794212a23..aa90bb272 100644 --- a/lib/virtio/virtio.c +++ b/lib/virtio/virtio.c @@ -120,8 +120,7 @@ int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags, vring_info = &vdev->vrings_info[i]; vring_alloc = &vring_info->info; -#ifndef VIRTIO_DEVICE_ONLY - if (vdev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER) { size_t offset; struct metal_io_region *io = vring_info->io; @@ -131,7 +130,6 @@ int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags, vring_size(vring_alloc->num_descs, vring_alloc->align)); } -#endif ret = virtqueue_create(vdev, i, names[i], vring_alloc, callbacks[i], vdev->func->notify, vring_info->vq); diff --git a/lib/virtio/virtqueue.c b/lib/virtio/virtqueue.c index 2544ee360..b43418d73 100644 --- a/lib/virtio/virtqueue.c +++ b/lib/virtio/virtqueue.c @@ -21,12 +21,8 @@ static int vq_ring_enable_interrupt(struct virtqueue *, uint16_t); static void vq_ring_free_chain(struct virtqueue *, uint16_t); static int vq_ring_must_notify(struct virtqueue *vq); static void vq_ring_notify(struct virtqueue *vq); -#ifndef VIRTIO_DEVICE_ONLY static int virtqueue_nused(struct virtqueue *vq); -#endif -#ifndef VIRTIO_DRIVER_ONLY static int virtqueue_navail(struct virtqueue *vq); -#endif /* Default implementation of P2V based on libmetal */ static inline void *virtqueue_phys_to_virt(struct virtqueue *vq, @@ -283,37 +279,29 @@ void virtqueue_disable_cb(struct virtqueue *vq) VQUEUE_BUSY(vq); if (vq->vq_dev->features & VIRTIO_RING_F_EVENT_IDX) { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx - vq->vq_nentries - 1; VRING_FLUSH(&vring_used_event(&vq->vq_ring), sizeof(vring_used_event(&vq->vq_ring))); } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { vring_avail_event(&vq->vq_ring) = vq->vq_available_idx - vq->vq_nentries - 1; VRING_FLUSH(&vring_avail_event(&vq->vq_ring), sizeof(vring_avail_event(&vq->vq_ring))); } -#endif /*VIRTIO_DRIVER_ONLY*/ } else { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { vq->vq_ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; VRING_FLUSH(&vq->vq_ring.avail->flags, sizeof(vq->vq_ring.avail->flags)); } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { vq->vq_ring.used->flags |= VRING_USED_F_NO_NOTIFY; VRING_FLUSH(&vq->vq_ring.used->flags, sizeof(vq->vq_ring.used->flags)); } -#endif /*VIRTIO_DRIVER_ONLY*/ } VQUEUE_IDLE(vq); @@ -498,15 +486,13 @@ static void vq_ring_init(struct virtqueue *vq, void *ring_mem, int alignment) vring_init(vr, size, ring_mem, alignment); -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { int i; for (i = 0; i < size - 1; i++) vr->desc[i].next = i + 1; vr->desc[i].next = VQ_RING_DESC_CHAIN_END; } -#endif /*VIRTIO_DEVICE_ONLY*/ } /* @@ -557,37 +543,29 @@ static int vq_ring_enable_interrupt(struct virtqueue *vq, uint16_t ndesc) * what's already been consumed. */ if (vq->vq_dev->features & VIRTIO_RING_F_EVENT_IDX) { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx + ndesc; VRING_FLUSH(&vring_used_event(&vq->vq_ring), sizeof(vring_used_event(&vq->vq_ring))); } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { vring_avail_event(&vq->vq_ring) = vq->vq_available_idx + ndesc; VRING_FLUSH(&vring_avail_event(&vq->vq_ring), sizeof(vring_avail_event(&vq->vq_ring))); } -#endif /*VIRTIO_DRIVER_ONLY*/ } else { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; VRING_FLUSH(&vq->vq_ring.avail->flags, sizeof(vq->vq_ring.avail->flags)); } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { vq->vq_ring.used->flags &= ~VRING_USED_F_NO_NOTIFY; VRING_FLUSH(&vq->vq_ring.used->flags, sizeof(vq->vq_ring.used->flags)); } -#endif /*VIRTIO_DRIVER_ONLY*/ } atomic_thread_fence(memory_order_seq_cst); @@ -597,20 +575,16 @@ static int vq_ring_enable_interrupt(struct virtqueue *vq, uint16_t ndesc) * since we last checked. Let our caller know so it processes the new * entries. */ -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { if (virtqueue_nused(vq) > ndesc) { return 1; } } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { if (virtqueue_navail(vq) > ndesc) { return 1; } } -#endif /*VIRTIO_DRIVER_ONLY*/ return 0; } @@ -637,8 +611,7 @@ static int vq_ring_must_notify(struct virtqueue *vq) uint16_t new_idx, prev_idx, event_idx; if (vq->vq_dev->features & VIRTIO_RING_F_EVENT_IDX) { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { /* CACHE: no need to invalidate avail */ new_idx = vq->vq_ring.avail->idx; prev_idx = new_idx - vq->vq_queued_cnt; @@ -648,9 +621,7 @@ static int vq_ring_must_notify(struct virtqueue *vq) return vring_need_event(event_idx, new_idx, prev_idx) != 0; } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { /* CACHE: no need to invalidate used */ new_idx = vq->vq_ring.used->idx; prev_idx = new_idx - vq->vq_queued_cnt; @@ -660,24 +631,19 @@ static int vq_ring_must_notify(struct virtqueue *vq) return vring_need_event(event_idx, new_idx, prev_idx) != 0; } -#endif /*VIRTIO_DRIVER_ONLY*/ } else { -#ifndef VIRTIO_DEVICE_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DRIVER) { + if (VIRTIO_DRIVER_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DRIVER) { VRING_INVALIDATE(&vq->vq_ring.used->flags, sizeof(vq->vq_ring.used->flags)); return (vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY) == 0; } -#endif /*VIRTIO_DEVICE_ONLY*/ -#ifndef VIRTIO_DRIVER_ONLY - if (vq->vq_dev->role == VIRTIO_DEV_DEVICE) { + if (VIRTIO_DEVICE_SUPPORT && vq->vq_dev->role == VIRTIO_DEV_DEVICE) { VRING_INVALIDATE(&vq->vq_ring.avail->flags, sizeof(vq->vq_ring.avail->flags)); return (vq->vq_ring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) == 0; } -#endif /*VIRTIO_DRIVER_ONLY*/ } return 0; @@ -699,7 +665,6 @@ static void vq_ring_notify(struct virtqueue *vq) * virtqueue_nused * */ -#ifndef VIRTIO_DEVICE_ONLY static int virtqueue_nused(struct virtqueue *vq) { uint16_t used_idx, nused; @@ -713,14 +678,12 @@ static int virtqueue_nused(struct virtqueue *vq) return nused; } -#endif /*VIRTIO_DEVICE_ONLY*/ /* * * virtqueue_navail * */ -#ifndef VIRTIO_DRIVER_ONLY static int virtqueue_navail(struct virtqueue *vq) { uint16_t avail_idx, navail; @@ -735,4 +698,3 @@ static int virtqueue_navail(struct virtqueue *vq) return navail; } -#endif /*VIRTIO_DRIVER_ONLY*/