2014-09-01 12:07:54 +02:00
|
|
|
/*
|
|
|
|
* libqos virtio definitions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Marc Marí
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LIBQOS_VIRTIO_H
|
|
|
|
#define LIBQOS_VIRTIO_H
|
|
|
|
|
2014-09-01 12:07:56 +02:00
|
|
|
#include "libqos/malloc.h"
|
2016-05-09 14:47:38 +02:00
|
|
|
#include "standard-headers/linux/virtio_ring.h"
|
2014-09-01 12:07:56 +02:00
|
|
|
|
2014-09-01 12:08:00 +02:00
|
|
|
#define QVIRTIO_F_BAD_FEATURE 0x40000000
|
2014-09-01 12:07:57 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
typedef struct QVirtioBus QVirtioBus;
|
|
|
|
|
2014-09-01 12:07:54 +02:00
|
|
|
typedef struct QVirtioDevice {
|
2016-10-17 12:30:21 +02:00
|
|
|
const QVirtioBus *bus;
|
2014-09-01 12:07:54 +02:00
|
|
|
/* Device type */
|
|
|
|
uint16_t device_type;
|
|
|
|
} QVirtioDevice;
|
|
|
|
|
2014-09-01 12:07:56 +02:00
|
|
|
typedef struct QVirtQueue {
|
2016-05-09 14:47:38 +02:00
|
|
|
uint64_t desc; /* This points to an array of struct vring_desc */
|
|
|
|
uint64_t avail; /* This points to a struct vring_avail */
|
2017-06-28 20:47:19 +02:00
|
|
|
uint64_t used; /* This points to a struct vring_used */
|
2014-09-01 12:07:56 +02:00
|
|
|
uint16_t index;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t free_head;
|
|
|
|
uint32_t num_free;
|
|
|
|
uint32_t align;
|
2017-06-28 20:47:20 +02:00
|
|
|
uint16_t last_used_idx;
|
2014-09-01 12:07:57 +02:00
|
|
|
bool indirect;
|
2014-09-01 12:08:00 +02:00
|
|
|
bool event;
|
2014-09-01 12:07:56 +02:00
|
|
|
} QVirtQueue;
|
|
|
|
|
2014-09-01 12:07:57 +02:00
|
|
|
typedef struct QVRingIndirectDesc {
|
2016-05-09 14:47:38 +02:00
|
|
|
uint64_t desc; /* This points to an array fo struct vring_desc */
|
2014-09-01 12:07:57 +02:00
|
|
|
uint16_t index;
|
|
|
|
uint16_t elem;
|
|
|
|
} QVRingIndirectDesc;
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
struct QVirtioBus {
|
2015-02-24 22:21:51 +01:00
|
|
|
uint8_t (*config_readb)(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint16_t (*config_readw)(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint32_t (*config_readl)(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint64_t (*config_readq)(QVirtioDevice *d, uint64_t addr);
|
2014-09-01 12:07:55 +02:00
|
|
|
|
2014-09-01 12:07:56 +02:00
|
|
|
/* Get features of the device */
|
|
|
|
uint32_t (*get_features)(QVirtioDevice *d);
|
|
|
|
|
2014-09-01 12:07:57 +02:00
|
|
|
/* Set features of the device */
|
2014-09-01 12:07:56 +02:00
|
|
|
void (*set_features)(QVirtioDevice *d, uint32_t features);
|
|
|
|
|
2014-09-01 12:07:57 +02:00
|
|
|
/* Get features of the guest */
|
|
|
|
uint32_t (*get_guest_features)(QVirtioDevice *d);
|
|
|
|
|
2014-09-01 12:07:55 +02:00
|
|
|
/* Get status of the device */
|
|
|
|
uint8_t (*get_status)(QVirtioDevice *d);
|
|
|
|
|
|
|
|
/* Set status of the device */
|
|
|
|
void (*set_status)(QVirtioDevice *d, uint8_t status);
|
2014-09-01 12:07:56 +02:00
|
|
|
|
2014-09-01 12:07:59 +02:00
|
|
|
/* Get the queue ISR status of the device */
|
|
|
|
bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq);
|
|
|
|
|
|
|
|
/* Get the configuration ISR status of the device */
|
|
|
|
bool (*get_config_isr_status)(QVirtioDevice *d);
|
2014-09-01 12:07:56 +02:00
|
|
|
|
|
|
|
/* Select a queue to work on */
|
|
|
|
void (*queue_select)(QVirtioDevice *d, uint16_t index);
|
|
|
|
|
|
|
|
/* Get the size of the selected queue */
|
|
|
|
uint16_t (*get_queue_size)(QVirtioDevice *d);
|
|
|
|
|
|
|
|
/* Set the address of the selected queue */
|
|
|
|
void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn);
|
|
|
|
|
|
|
|
/* Setup the virtqueue specified by index */
|
|
|
|
QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc,
|
|
|
|
uint16_t index);
|
|
|
|
|
2016-05-05 17:53:35 +02:00
|
|
|
/* Free virtqueue resources */
|
|
|
|
void (*virtqueue_cleanup)(QVirtQueue *vq, QGuestAllocator *alloc);
|
|
|
|
|
2014-09-01 12:07:56 +02:00
|
|
|
/* Notify changes in virtqueue */
|
|
|
|
void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq);
|
2016-10-17 12:30:21 +02:00
|
|
|
};
|
2014-09-01 12:07:55 +02:00
|
|
|
|
2016-10-17 12:30:22 +02:00
|
|
|
static inline bool qvirtio_is_big_endian(QVirtioDevice *d)
|
|
|
|
{
|
|
|
|
/* FIXME: virtio 1.0 is always little-endian */
|
|
|
|
return qtest_big_endian(global_qtest);
|
|
|
|
}
|
|
|
|
|
2014-09-01 12:07:56 +02:00
|
|
|
static inline uint32_t qvring_size(uint32_t num, uint32_t align)
|
|
|
|
{
|
2016-05-09 14:47:38 +02:00
|
|
|
return ((sizeof(struct vring_desc) * num + sizeof(uint16_t) * (3 + num)
|
2014-09-01 12:07:56 +02:00
|
|
|
+ align - 1) & ~(align - 1))
|
2016-05-09 14:47:38 +02:00
|
|
|
+ sizeof(uint16_t) * 3 + sizeof(struct vring_used_elem) * num;
|
2014-09-01 12:07:56 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr);
|
|
|
|
uint32_t qvirtio_get_features(QVirtioDevice *d);
|
|
|
|
void qvirtio_set_features(QVirtioDevice *d, uint32_t features);
|
|
|
|
|
|
|
|
void qvirtio_reset(QVirtioDevice *d);
|
|
|
|
void qvirtio_set_acknowledge(QVirtioDevice *d);
|
|
|
|
void qvirtio_set_driver(QVirtioDevice *d);
|
|
|
|
void qvirtio_set_driver_ok(QVirtioDevice *d);
|
|
|
|
|
|
|
|
void qvirtio_wait_queue_isr(QVirtioDevice *d,
|
2014-09-29 17:40:12 +02:00
|
|
|
QVirtQueue *vq, gint64 timeout_us);
|
2016-10-17 12:30:21 +02:00
|
|
|
uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d,
|
2014-09-29 17:40:11 +02:00
|
|
|
QVirtQueue *vq,
|
|
|
|
uint64_t addr,
|
|
|
|
gint64 timeout_us);
|
2017-06-28 20:47:20 +02:00
|
|
|
void qvirtio_wait_used_elem(QVirtioDevice *d,
|
|
|
|
QVirtQueue *vq,
|
|
|
|
uint32_t desc_idx,
|
2018-02-01 21:21:28 +01:00
|
|
|
uint32_t *len,
|
2017-06-28 20:47:20 +02:00
|
|
|
gint64 timeout_us);
|
2016-10-17 12:30:21 +02:00
|
|
|
void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us);
|
|
|
|
QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
|
|
|
|
QGuestAllocator *alloc, uint16_t index);
|
2016-05-05 17:53:35 +02:00
|
|
|
void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
|
|
|
|
QGuestAllocator *alloc);
|
2014-09-01 12:07:56 +02:00
|
|
|
|
|
|
|
void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr);
|
2014-09-01 12:07:57 +02:00
|
|
|
QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
|
|
|
|
QGuestAllocator *alloc, uint16_t elem);
|
|
|
|
void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
|
|
|
|
uint32_t len, bool write);
|
2014-09-01 12:07:56 +02:00
|
|
|
uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
|
|
|
|
bool next);
|
2014-09-01 12:07:57 +02:00
|
|
|
uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
|
2016-10-17 12:30:21 +02:00
|
|
|
void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head);
|
2018-02-01 21:21:28 +01:00
|
|
|
bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx, uint32_t *len);
|
2014-09-01 12:07:55 +02:00
|
|
|
|
2014-09-01 12:08:00 +02:00
|
|
|
void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx);
|
2017-08-31 16:40:08 +02:00
|
|
|
|
|
|
|
const char *qvirtio_get_dev_type(void);
|
|
|
|
|
2014-09-01 12:07:54 +02:00
|
|
|
#endif
|