From fb8a3ae29b1f9de12adf11f1461b4764b6c8b8ae Mon Sep 17 00:00:00 2001 From: Bowen Wang Date: Tue, 8 Oct 2024 21:20:47 +0800 Subject: [PATCH 1/2] virtio.h: add new feature bit VIRTIO_F_ANY_LAYOUT Follow the virtio spec, this feature bit indicates that the device accepts arbitrary descriptor layouts. Signed-off-by: Bowen Wang --- lib/include/openamp/virtio.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/include/openamp/virtio.h b/lib/include/openamp/virtio.h index 0e366314..c3b03952 100644 --- a/lib/include/openamp/virtio.h +++ b/lib/include/openamp/virtio.h @@ -118,6 +118,12 @@ struct virtio_device_id { */ #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24) +/* + * This feature indicates that the device accepts arbitrary + * descriptor layouts. + */ +#define VIRTIO_F_ANY_LAYOUT (1 << 27) + /* * The guest should never negotiate this feature; it * is used to detect faulty drivers. From 1b4cb95fe95c8c11406e7695270870f2daac92ca Mon Sep 17 00:00:00 2001 From: Bowen Wang Date: Tue, 8 Oct 2024 21:24:53 +0800 Subject: [PATCH 2/2] virtio.h: add new api virtio_has_feature() virtio_has_feature() can be easily used to check if the virtio device support a specific feature. And assgin feature to vdev->feature for virtio device role when get features, so the virtio device side can use virtio_has_featrue() to check weather the virtio device support a feature. Signed-off-by: Bowen Wang --- lib/include/openamp/virtio.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/include/openamp/virtio.h b/lib/include/openamp/virtio.h index c3b03952..b9f6328d 100644 --- a/lib/include/openamp/virtio.h +++ b/lib/include/openamp/virtio.h @@ -449,6 +449,9 @@ static inline int virtio_get_features(struct virtio_device *vdev, return -ENXIO; *features = vdev->func->get_features(vdev); + if (VIRTIO_ROLE_IS_DEVICE(vdev)) + vdev->features = *features; + return 0; } @@ -565,6 +568,28 @@ static inline int virtio_free_buf(struct virtio_device *vdev, void *buf) return 0; } +/** + * @brief Check if the virtio device support a specific feature. + * + * @param vdev Pointer to device structure. + * @param feature_bit Feature bit to check. + * + * @return true if the feature is supported, otherwise false. + */ +static inline bool virtio_has_feature(struct virtio_device *vdev, + unsigned int feature_bit) +{ + uint32_t features; + + if (!vdev) + return false; + + if (!vdev->features) + virtio_get_features(vdev, &features); + + return (vdev->features & (1UL << feature_bit)) != 0; +} + #if defined __cplusplus } #endif