Skip to content

Commit

Permalink
lib: Add and use IS_ENABLED() macro
Browse files Browse the repository at this point in the history
Checks if this symbol is defined and set to equal 1. Used to make it
more clear that a preprocessor level check is intended.

Signed-off-by: Andrew Davis <afd@ti.com>
  • Loading branch information
glneo committed Apr 3, 2024
1 parent fe69c94 commit 3b609d4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
2 changes: 2 additions & 0 deletions lib/include/openamp/virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ __deprecated static inline int deprecated_virtio_dev_slave(void)
#warning "VIRTIO_DEVICE_ONLY is deprecated, please use VIRTIO_DRIVER_SUPPORT=0"
#endif

#define IS_ENABLED(option) (option == 1)

/** @brief Virtio device identifier. */
struct virtio_device_id {
/** Virtio subsystem device ID. */
Expand Down
4 changes: 2 additions & 2 deletions lib/remoteproc/remoteproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,12 +916,12 @@ remoteproc_create_virtio(struct remoteproc *rproc,
unsigned int num_vrings, i;
struct metal_list *node;

#if !VIRTIO_DRIVER_SUPPORT
#if !IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
if (role == VIRTIO_DEV_DRIVER)
return NULL;
#endif

#if !VIRTIO_DEVICE_SUPPORT
#if !IS_ENABLED(VIRTIO_DEVICE_SUPPORT)
if (role == VIRTIO_DEV_DEVICE)
return NULL;
#endif
Expand Down
14 changes: 7 additions & 7 deletions lib/remoteproc/remoteproc_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static int rproc_virtio_create_virtqueue(struct virtio_device *vdev,
if (!vring_info->vq)
return ERROR_NO_MEM;

if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER) {
if (IS_ENABLED(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);

Expand Down Expand Up @@ -128,7 +128,7 @@ static unsigned char rproc_virtio_get_status(struct virtio_device *vdev)
return status;
}

#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
static void rproc_virtio_set_status(struct virtio_device *vdev,
unsigned char status)
{
Expand Down Expand Up @@ -183,7 +183,7 @@ static uint32_t rproc_virtio_get_features(struct virtio_device *vdev)
return dfeatures & gfeatures;
}

#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
static void rproc_virtio_set_features(struct virtio_device *vdev,
uint32_t features)
{
Expand Down Expand Up @@ -233,7 +233,7 @@ static void rproc_virtio_read_config(struct virtio_device *vdev,
}
}

#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
static void rproc_virtio_write_config(struct virtio_device *vdev,
uint32_t offset, void *src, int length)
{
Expand Down Expand Up @@ -271,7 +271,7 @@ static const struct virtio_dispatch remoteproc_virtio_dispatch_funcs = {
.get_features = rproc_virtio_get_features,
.read_config = rproc_virtio_read_config,
.notify = rproc_virtio_virtqueue_notify,
#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(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
Expand Down Expand Up @@ -326,7 +326,7 @@ rproc_virtio_create_vdev(unsigned int role, unsigned int notifyid,
vdev->vrings_num = num_vrings;
vdev->func = &remoteproc_virtio_dispatch_funcs;

#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
if (role == VIRTIO_DEV_DRIVER) {
uint32_t dfeatures = rproc_virtio_get_dfeatures(vdev);
/* Assume the virtio driver support all remote features */
Expand Down Expand Up @@ -405,7 +405,7 @@ void rproc_virtio_wait_remote_ready(struct virtio_device *vdev)
* remote action, we can return. Behavior should be updated
* in future if a remote status is added.
*/
if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER)
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && vdev->role == VIRTIO_DEV_DRIVER)
return;

while (1) {
Expand Down
52 changes: 26 additions & 26 deletions lib/rpmsg/rpmsg_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct vbuff_reclaimer_t {
};

/* Default configuration */
#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
#define RPMSG_VIRTIO_DEFAULT_CONFIG \
(&(const struct rpmsg_virtio_config) { \
.h2r_buf_size = RPMSG_BUFFER_SIZE, \
Expand All @@ -69,7 +69,7 @@ struct vbuff_reclaimer_t {
#define RPMSG_VIRTIO_DEFAULT_CONFIG NULL
#endif

#if VIRTIO_DRIVER_SUPPORT
#if IS_ENABLED(VIRTIO_DRIVER_SUPPORT)
metal_weak void *
rpmsg_virtio_shm_pool_get_buffer(struct rpmsg_virtio_shm_pool *shpool,
size_t size)
Expand All @@ -83,7 +83,7 @@ rpmsg_virtio_shm_pool_get_buffer(struct rpmsg_virtio_shm_pool *shpool,

return buffer;
}
#endif /*VIRTIO_DRIVER_SUPPORT*/
#endif

void rpmsg_virtio_init_shm_pool(struct rpmsg_virtio_shm_pool *shpool,
void *shb, size_t size)
Expand Down Expand Up @@ -114,7 +114,7 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev,

BUFFER_INVALIDATE(buffer, len);

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
struct virtqueue_buf vqbuf;

(void)idx;
Expand All @@ -125,7 +125,7 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev,
RPMSG_ASSERT(ret == VQUEUE_SUCCESS, "add buffer failed\r\n");
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(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");
Expand All @@ -152,7 +152,7 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev,

BUFFER_FLUSH(buffer, len);

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
struct virtqueue_buf vqbuf;
(void)idx;

Expand All @@ -162,7 +162,7 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev,
return virtqueue_add_buffer(rvdev->svq, &vqbuf, 1, 0, buffer);
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
(void)buffer;
return virtqueue_add_consumed_buffer(rvdev->svq, idx, len);
}
Expand Down Expand Up @@ -197,19 +197,19 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
data = r_desc;
*idx = r_desc->idx;

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST)
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST)
*len = rvdev->config.h2r_buf_size;
if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE)
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE)
*len = virtqueue_get_buffer_length(rvdev->svq, *idx);
} else if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
} else if (IS_ENABLED(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,
rvdev->config.h2r_buf_size);
*len = rvdev->config.h2r_buf_size;
*idx = 0;
}
} else if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
} else if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
data = virtqueue_get_available_buffer(rvdev->svq, idx, len);
}

Expand All @@ -233,11 +233,11 @@ static void *rpmsg_virtio_get_rx_buffer(struct rpmsg_virtio_device *rvdev,
unsigned int role = rpmsg_virtio_get_role(rvdev);
void *data = NULL;

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
data = virtqueue_get_buffer(rvdev->rvq, len, idx);
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
data =
virtqueue_get_available_buffer(rvdev->rvq, idx, len);
}
Expand Down Expand Up @@ -452,7 +452,7 @@ static int rpmsg_virtio_send_offchannel_nocopy(struct rpmsg_device *rdev,

metal_mutex_acquire(&rdev->lock);

if (VIRTIO_DRIVER_SUPPORT && rpmsg_virtio_get_role(rvdev) == RPMSG_HOST)
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && rpmsg_virtio_get_role(rvdev) == RPMSG_HOST)
buff_len = rvdev->config.h2r_buf_size;
else
buff_len = virtqueue_get_buffer_length(rvdev->svq, idx);
Expand Down Expand Up @@ -712,15 +712,15 @@ int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev)
rvdev = (struct rpmsg_virtio_device *)rdev;
role = rpmsg_virtio_get_role(rvdev);

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(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);
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(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.
Expand Down Expand Up @@ -750,15 +750,15 @@ int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev)
rvdev = (struct rpmsg_virtio_device *)rdev;
role = rpmsg_virtio_get_role(rvdev);

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(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);
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(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.
Expand Down Expand Up @@ -816,7 +816,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);

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
/*
* The virtio configuration contains only options applicable to
* a virtio driver, implying rpmsg host role.
Expand All @@ -827,7 +827,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
rvdev->config = *config;
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
/* wait synchro with the host */
status = rpmsg_virtio_wait_remote_ready(rvdev);
if (status)
Expand All @@ -839,7 +839,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
return status;
rdev->support_ns = !!(features & (1 << VIRTIO_RPMSG_F_NS));

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(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.
Expand All @@ -856,7 +856,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
callback[1] = rpmsg_virtio_tx_callback;
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
vq_names[0] = "tx_vq";
vq_names[1] = "rx_vq";
callback[0] = rpmsg_virtio_tx_callback;
Expand All @@ -873,12 +873,12 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
return status;

/* Create virtqueue success, assign back the virtqueue */
if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
rvdev->rvq = vdev->vrings_info[0].vq;
rvdev->svq = vdev->vrings_info[1].vq;
}

if (VIRTIO_DEVICE_SUPPORT && role == RPMSG_REMOTE) {
if (IS_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == RPMSG_REMOTE) {
rvdev->rvq = vdev->vrings_info[1].vq;
rvdev->svq = vdev->vrings_info[0].vq;
}
Expand All @@ -897,7 +897,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
vq->shm_io = shm_io;
}

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
struct virtqueue_buf vqbuf;
unsigned int idx;
void *buffer;
Expand Down Expand Up @@ -942,7 +942,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
rpmsg_virtio_ns_callback, NULL);
}

if (VIRTIO_DRIVER_SUPPORT && role == RPMSG_HOST) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == RPMSG_HOST) {
status = virtio_set_status(rvdev->vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
if (status)
goto err;
Expand Down
2 changes: 1 addition & 1 deletion lib/virtio/virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags,
vring_info = &vdev->vrings_info[i];

vring_alloc = &vring_info->info;
if (VIRTIO_DRIVER_SUPPORT && vdev->role == VIRTIO_DEV_DRIVER) {
if (IS_ENABLED(VIRTIO_DRIVER_SUPPORT) && vdev->role == VIRTIO_DEV_DRIVER) {
size_t offset;
struct metal_io_region *io = vring_info->io;

Expand Down
Loading

0 comments on commit 3b609d4

Please sign in to comment.