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_queue_select(QVirtioDevice
*d
, uint16_t index
)
106 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
107 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_SEL
, (uint32_t)index
);
109 g_assert_cmphex(qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
), ==, 0);
112 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice
*d
)
114 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
115 return (uint16_t)qtest_readl(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM_MAX
);
118 static void qvirtio_mmio_set_queue_address(QVirtioDevice
*d
, uint32_t pfn
)
120 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
121 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
, pfn
);
124 static QVirtQueue
*qvirtio_mmio_virtqueue_setup(QVirtioDevice
*d
,
125 QGuestAllocator
*alloc
, uint16_t index
)
127 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
131 vq
= g_malloc0(sizeof(*vq
));
132 qvirtio_mmio_queue_select(d
, index
);
133 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_ALIGN
, dev
->page_size
);
136 vq
->size
= qvirtio_mmio_get_queue_size(d
);
138 vq
->num_free
= vq
->size
;
139 vq
->align
= dev
->page_size
;
140 vq
->indirect
= (dev
->features
& (1u << VIRTIO_RING_F_INDIRECT_DESC
)) != 0;
141 vq
->event
= (dev
->features
& (1u << VIRTIO_RING_F_EVENT_IDX
)) != 0;
143 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM
, vq
->size
);
145 /* Check different than 0 */
146 g_assert_cmpint(vq
->size
, !=, 0);
148 /* Check power of 2 */
149 g_assert_cmpint(vq
->size
& (vq
->size
- 1), ==, 0);
151 addr
= guest_alloc(alloc
, qvring_size(vq
->size
, dev
->page_size
));
152 qvring_init(dev
->qts
, alloc
, vq
, addr
);
153 qvirtio_mmio_set_queue_address(d
, vq
->desc
/ dev
->page_size
);
158 static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue
*vq
,
159 QGuestAllocator
*alloc
)
161 guest_free(alloc
, vq
->desc
);
165 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice
*d
, QVirtQueue
*vq
)
167 QVirtioMMIODevice
*dev
= container_of(d
, QVirtioMMIODevice
, vdev
);
168 qtest_writel(dev
->qts
, dev
->addr
+ QVIRTIO_MMIO_QUEUE_NOTIFY
, vq
->index
);
171 const QVirtioBus qvirtio_mmio
= {
172 .config_readb
= qvirtio_mmio_config_readb
,
173 .config_readw
= qvirtio_mmio_config_readw
,
174 .config_readl
= qvirtio_mmio_config_readl
,
175 .config_readq
= qvirtio_mmio_config_readq
,
176 .get_features
= qvirtio_mmio_get_features
,
177 .set_features
= qvirtio_mmio_set_features
,
178 .get_guest_features
= qvirtio_mmio_get_guest_features
,
179 .get_status
= qvirtio_mmio_get_status
,
180 .set_status
= qvirtio_mmio_set_status
,
181 .get_queue_isr_status
= qvirtio_mmio_get_queue_isr_status
,
182 .get_config_isr_status
= qvirtio_mmio_get_config_isr_status
,
183 .queue_select
= qvirtio_mmio_queue_select
,
184 .get_queue_size
= qvirtio_mmio_get_queue_size
,
185 .set_queue_address
= qvirtio_mmio_set_queue_address
,
186 .virtqueue_setup
= qvirtio_mmio_virtqueue_setup
,
187 .virtqueue_cleanup
= qvirtio_mmio_virtqueue_cleanup
,
188 .virtqueue_kick
= qvirtio_mmio_virtqueue_kick
,
191 static void *qvirtio_mmio_get_driver(void *obj
, const char *interface
)
193 QVirtioMMIODevice
*virtio_mmio
= obj
;
194 if (!g_strcmp0(interface
, "virtio-bus")) {
195 return &virtio_mmio
->vdev
;
197 fprintf(stderr
, "%s not present in virtio-mmio\n", interface
);
198 g_assert_not_reached();
201 static void qvirtio_mmio_start_hw(QOSGraphObject
*obj
)
203 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*) obj
;
204 qvirtio_start_device(&dev
->vdev
);
207 void qvirtio_mmio_init_device(QVirtioMMIODevice
*dev
, QTestState
*qts
,
208 uint64_t addr
, uint32_t page_size
)
211 magic
= qtest_readl(qts
, addr
+ QVIRTIO_MMIO_MAGIC_VALUE
);
212 g_assert(magic
== ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
216 dev
->page_size
= page_size
;
217 dev
->vdev
.device_type
= qtest_readl(qts
, addr
+ QVIRTIO_MMIO_DEVICE_ID
);
218 dev
->vdev
.bus
= &qvirtio_mmio
;
220 qtest_writel(qts
, addr
+ QVIRTIO_MMIO_GUEST_PAGE_SIZE
, page_size
);
222 dev
->obj
.get_driver
= qvirtio_mmio_get_driver
;
223 dev
->obj
.start_hw
= qvirtio_mmio_start_hw
;
226 static void virtio_mmio_register_nodes(void)
228 qos_node_create_driver("virtio-mmio", NULL
);
229 qos_node_produces("virtio-mmio", "virtio-bus");
232 libqos_init(virtio_mmio_register_nodes
);