2 * libqos virtio MMIO driver
4 * Copyright (c) 2014 Marc MarĂ
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.
10 #include "qemu/osdep.h"
12 #include "qemu/module.h"
13 #include "libqos/virtio.h"
14 #include "libqos/virtio-mmio.h"
15 #include "libqos/malloc.h"
16 #include "libqos/qgraph.h"
17 #include "standard-headers/linux/virtio_ring.h"
19 static uint8_t qvirtio_mmio_config_readb(QVirtioDevice
*d
, uint64_t off
)
21 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
22 return qtest_readb(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
25 static uint16_t qvirtio_mmio_config_readw(QVirtioDevice
*d
, uint64_t off
)
27 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
28 return qtest_readw(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
31 static uint32_t qvirtio_mmio_config_readl(QVirtioDevice
*d
, uint64_t off
)
33 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
34 return qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
37 static uint64_t qvirtio_mmio_config_readq(QVirtioDevice
*d
, uint64_t off
)
39 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
40 return qtest_readq(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
43 static uint32_t qvirtio_mmio_get_features(QVirtioDevice
*d
)
45 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
46 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_HOST_FEATURES_SEL
, 0);
47 return qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_HOST_FEATURES
);
50 static void qvirtio_mmio_set_features(QVirtioDevice
*d
, uint32_t features
)
52 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
53 dev
->features
= features
;
54 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_GUEST_FEATURES_SEL
, 0);
55 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_GUEST_FEATURES
, features
);
58 static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice
*d
)
60 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
64 static uint8_t qvirtio_mmio_get_status(QVirtioDevice
*d
)
66 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
67 return (uint8_t)qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_STATUS
);
70 static void qvirtio_mmio_set_status(QVirtioDevice
*d
, uint8_t status
)
72 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
73 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_DEVICE_STATUS
, (uint32_t)status
);
76 static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice
*d
, QVirtQueue
*vq
)
78 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
81 isr
= qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_STATUS
) & 1;
83 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_ACK
, 1);
90 static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice
*d
)
92 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
95 isr
= qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_STATUS
) & 2;
97 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_ACK
, 2);
104 static void qvirtio_mmio_wait_config_isr_status(QVirtioDevice
*d
,
107 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
108 gint64 start_time
= g_get_monotonic_time();
111 g_assert(g_get_monotonic_time() - start_time
<= timeout_us
);
112 qtest_clock_step(dev
->qts
, 100);
113 } while (!qvirtio_mmio_get_config_isr_status(d
));
116 static void qvirtio_mmio_queue_select(QVirtioDevice
*d
, uint16_t index
)
118 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
119 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_SEL
, (uint32_t)index
);
121 g_assert_cmphex(qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
), ==, 0);
124 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice
*d
)
126 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
127 return (uint16_t)qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM_MAX
);
130 static void qvirtio_mmio_set_queue_address(QVirtioDevice
*d
, uint32_t pfn
)
132 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
133 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
, pfn
);
136 static QVirtQueue
*qvirtio_mmio_virtqueue_setup(QVirtioDevice
*d
,
137 QGuestAllocator
*alloc
, uint16_t index
)
139 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
143 vq
= g_malloc0(sizeof(*vq
));
144 qvirtio_mmio_queue_select(d
, index
);
145 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_ALIGN
, dev
->page_size
);
148 vq
->size
= qvirtio_mmio_get_queue_size(d
);
150 vq
->num_free
= vq
->size
;
151 vq
->align
= dev
->page_size
;
152 vq
->indirect
= (dev
->features
& (1u << VIRTIO_RING_F_INDIRECT_DESC
)) != 0;
153 vq
->event
= (dev
->features
& (1u << VIRTIO_RING_F_EVENT_IDX
)) != 0;
155 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM
, vq
->size
);
157 /* Check different than 0 */
158 g_assert_cmpint(vq
->size
, !=, 0);
160 /* Check power of 2 */
161 g_assert_cmpint(vq
->size
& (vq
->size
- 1), ==, 0);
163 addr
= guest_alloc(alloc
, qvring_size(vq
->size
, dev
->page_size
));
164 qvring_init(dev
->qts
, alloc
, vq
, addr
);
165 qvirtio_mmio_set_queue_address(d
, vq
->desc
/ dev
->page_size
);
170 static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue
*vq
,
171 QGuestAllocator
*alloc
)
173 guest_free(alloc
, vq
->desc
);
177 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice
*d
, QVirtQueue
*vq
)
179 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
180 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NOTIFY
, vq
->index
);
183 const QVirtioBus qvirtio_mmio
= {
184 .config_readb
= qvirtio_mmio_config_readb
,
185 .config_readw
= qvirtio_mmio_config_readw
,
186 .config_readl
= qvirtio_mmio_config_readl
,
187 .config_readq
= qvirtio_mmio_config_readq
,
188 .get_features
= qvirtio_mmio_get_features
,
189 .set_features
= qvirtio_mmio_set_features
,
190 .get_guest_features
= qvirtio_mmio_get_guest_features
,
191 .get_status
= qvirtio_mmio_get_status
,
192 .set_status
= qvirtio_mmio_set_status
,
193 .get_queue_isr_status
= qvirtio_mmio_get_queue_isr_status
,
194 .wait_config_isr_status
= qvirtio_mmio_wait_config_isr_status
,
195 .queue_select
= qvirtio_mmio_queue_select
,
196 .get_queue_size
= qvirtio_mmio_get_queue_size
,
197 .set_queue_address
= qvirtio_mmio_set_queue_address
,
198 .virtqueue_setup
= qvirtio_mmio_virtqueue_setup
,
199 .virtqueue_cleanup
= qvirtio_mmio_virtqueue_cleanup
,
200 .virtqueue_kick
= qvirtio_mmio_virtqueue_kick
,
203 static void *qvirtio_mmio_get_driver(void *obj
, const char *interface
)
205 QVirtioMMIODevice
*virtio_mmio
= obj
;
206 if (!g_strcmp0(interface
, "virtio-bus")) {
207 return &virtio_mmio
->vdev
;
209 fprintf(stderr
, "%s not present in virtio-mmio\n", interface
);
210 g_assert_not_reached();
213 static void qvirtio_mmio_start_hw(QOSGraphObject
*obj
)
215 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*) obj
;
216 qvirtio_start_device(&dev
->vdev
);
219 void qvirtio_mmio_init_device(QVirtioMMIODevice
*dev
, QTestState
*qts
,
220 uint64_t addr
, uint32_t page_size
)
223 magic
= qtest_readl(qts
, addr
+ QVIRTIO_MMIO_MAGIC_VALUE
);
224 g_assert(magic
== ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
228 dev
->page_size
= page_size
;
229 dev
->vdev
.device_type
= qtest_readl(qts
, addr
+ QVIRTIO_MMIO_DEVICE_ID
);
230 dev
->vdev
.bus
= &qvirtio_mmio
;
232 qtest_writel(qts
, addr
+ QVIRTIO_MMIO_GUEST_PAGE_SIZE
, page_size
);
234 dev
->obj
.get_driver
= qvirtio_mmio_get_driver
;
235 dev
->obj
.start_hw
= qvirtio_mmio_start_hw
;
238 static void virtio_mmio_register_nodes(void)
240 qos_node_create_driver("virtio-mmio", NULL
);
241 qos_node_produces("virtio-mmio", "virtio-bus");
244 libqos_init(virtio_mmio_register_nodes
);