]>
Commit | Line | Data |
---|---|---|
311e666a MM |
1 | /* |
2 | * libqos virtio definitions | |
3 | * | |
4 | * Copyright (c) 2014 Marc Marí | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | */ | |
9 | ||
10 | #ifndef LIBQOS_VIRTIO_H | |
11 | #define LIBQOS_VIRTIO_H | |
12 | ||
bf3c63d2 | 13 | #include "libqos/malloc.h" |
780b11a0 | 14 | #include "standard-headers/linux/virtio_ring.h" |
bf3c63d2 | 15 | |
1053587c | 16 | #define QVIRTIO_F_BAD_FEATURE 0x40000000 |
f294b029 | 17 | |
6b9cdf4c LV |
18 | typedef struct QVirtioBus QVirtioBus; |
19 | ||
311e666a | 20 | typedef struct QVirtioDevice { |
6b9cdf4c | 21 | const QVirtioBus *bus; |
311e666a MM |
22 | /* Device type */ |
23 | uint16_t device_type; | |
583349d1 | 24 | uint64_t features; |
d5006a45 | 25 | bool big_endian; |
311e666a MM |
26 | } QVirtioDevice; |
27 | ||
bf3c63d2 | 28 | typedef struct QVirtQueue { |
780b11a0 SH |
29 | uint64_t desc; /* This points to an array of struct vring_desc */ |
30 | uint64_t avail; /* This points to a struct vring_avail */ | |
afbccba6 | 31 | uint64_t used; /* This points to a struct vring_used */ |
bf3c63d2 MM |
32 | uint16_t index; |
33 | uint32_t size; | |
34 | uint32_t free_head; | |
35 | uint32_t num_free; | |
36 | uint32_t align; | |
e77abbe9 | 37 | uint16_t last_used_idx; |
f294b029 | 38 | bool indirect; |
1053587c | 39 | bool event; |
bf3c63d2 MM |
40 | } QVirtQueue; |
41 | ||
f294b029 | 42 | typedef struct QVRingIndirectDesc { |
780b11a0 | 43 | uint64_t desc; /* This points to an array fo struct vring_desc */ |
f294b029 MM |
44 | uint16_t index; |
45 | uint16_t elem; | |
46 | } QVRingIndirectDesc; | |
47 | ||
6b9cdf4c | 48 | struct QVirtioBus { |
728312b8 MM |
49 | uint8_t (*config_readb)(QVirtioDevice *d, uint64_t addr); |
50 | uint16_t (*config_readw)(QVirtioDevice *d, uint64_t addr); | |
51 | uint32_t (*config_readl)(QVirtioDevice *d, uint64_t addr); | |
52 | uint64_t (*config_readq)(QVirtioDevice *d, uint64_t addr); | |
46e0cf76 | 53 | |
bf3c63d2 MM |
54 | /* Get features of the device */ |
55 | uint32_t (*get_features)(QVirtioDevice *d); | |
56 | ||
f294b029 | 57 | /* Set features of the device */ |
bf3c63d2 MM |
58 | void (*set_features)(QVirtioDevice *d, uint32_t features); |
59 | ||
f294b029 MM |
60 | /* Get features of the guest */ |
61 | uint32_t (*get_guest_features)(QVirtioDevice *d); | |
62 | ||
46e0cf76 MM |
63 | /* Get status of the device */ |
64 | uint8_t (*get_status)(QVirtioDevice *d); | |
65 | ||
66 | /* Set status of the device */ | |
67 | void (*set_status)(QVirtioDevice *d, uint8_t status); | |
bf3c63d2 | 68 | |
58368113 MM |
69 | /* Get the queue ISR status of the device */ |
70 | bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq); | |
71 | ||
72 | /* Get the configuration ISR status of the device */ | |
73 | bool (*get_config_isr_status)(QVirtioDevice *d); | |
bf3c63d2 MM |
74 | |
75 | /* Select a queue to work on */ | |
76 | void (*queue_select)(QVirtioDevice *d, uint16_t index); | |
77 | ||
78 | /* Get the size of the selected queue */ | |
79 | uint16_t (*get_queue_size)(QVirtioDevice *d); | |
80 | ||
81 | /* Set the address of the selected queue */ | |
82 | void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn); | |
83 | ||
84 | /* Setup the virtqueue specified by index */ | |
85 | QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc, | |
86 | uint16_t index); | |
87 | ||
f1d3b991 SH |
88 | /* Free virtqueue resources */ |
89 | void (*virtqueue_cleanup)(QVirtQueue *vq, QGuestAllocator *alloc); | |
90 | ||
bf3c63d2 MM |
91 | /* Notify changes in virtqueue */ |
92 | void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq); | |
6b9cdf4c | 93 | }; |
46e0cf76 | 94 | |
bf3c63d2 MM |
95 | static inline uint32_t qvring_size(uint32_t num, uint32_t align) |
96 | { | |
780b11a0 | 97 | return ((sizeof(struct vring_desc) * num + sizeof(uint16_t) * (3 + num) |
bf3c63d2 | 98 | + align - 1) & ~(align - 1)) |
780b11a0 | 99 | + sizeof(uint16_t) * 3 + sizeof(struct vring_used_elem) * num; |
bf3c63d2 MM |
100 | } |
101 | ||
6b9cdf4c LV |
102 | uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr); |
103 | uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr); | |
104 | uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr); | |
105 | uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr); | |
106 | uint32_t qvirtio_get_features(QVirtioDevice *d); | |
107 | void qvirtio_set_features(QVirtioDevice *d, uint32_t features); | |
d5006a45 | 108 | bool qvirtio_is_big_endian(QVirtioDevice *d); |
6b9cdf4c LV |
109 | |
110 | void qvirtio_reset(QVirtioDevice *d); | |
111 | void qvirtio_set_acknowledge(QVirtioDevice *d); | |
112 | void qvirtio_set_driver(QVirtioDevice *d); | |
113 | void qvirtio_set_driver_ok(QVirtioDevice *d); | |
114 | ||
115 | void qvirtio_wait_queue_isr(QVirtioDevice *d, | |
70556264 | 116 | QVirtQueue *vq, gint64 timeout_us); |
6b9cdf4c | 117 | uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d, |
e8c81b4d SH |
118 | QVirtQueue *vq, |
119 | uint64_t addr, | |
120 | gint64 timeout_us); | |
e77abbe9 SH |
121 | void qvirtio_wait_used_elem(QVirtioDevice *d, |
122 | QVirtQueue *vq, | |
123 | uint32_t desc_idx, | |
be3a6781 | 124 | uint32_t *len, |
e77abbe9 | 125 | gint64 timeout_us); |
6b9cdf4c LV |
126 | void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us); |
127 | QVirtQueue *qvirtqueue_setup(QVirtioDevice *d, | |
128 | QGuestAllocator *alloc, uint16_t index); | |
f1d3b991 SH |
129 | void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq, |
130 | QGuestAllocator *alloc); | |
bf3c63d2 | 131 | |
8b898f59 TH |
132 | void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq, |
133 | uint64_t addr); | |
f294b029 MM |
134 | QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d, |
135 | QGuestAllocator *alloc, uint16_t elem); | |
136 | void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data, | |
137 | uint32_t len, bool write); | |
bf3c63d2 MM |
138 | uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, |
139 | bool next); | |
f294b029 | 140 | uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect); |
6b9cdf4c | 141 | void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head); |
be3a6781 | 142 | bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx, uint32_t *len); |
46e0cf76 | 143 | |
1053587c | 144 | void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx); |
2f84a92e | 145 | |
34c97748 | 146 | void qvirtio_start_device(QVirtioDevice *vdev); |
2f84a92e | 147 | |
311e666a | 148 | #endif |