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 "libqos/virtio.h"
13 #include "libqos/virtio-mmio.h"
14 #include "libqos/malloc.h"
15 #include "libqos/malloc-generic.h"
16 #include "standard-headers/linux/virtio_ring.h"
18 static uint8_t qvirtio_mmio_config_readb(QVirtioDevice
*d
, uint64_t off
)
20 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
21 return readb(dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
24 static uint16_t qvirtio_mmio_config_readw(QVirtioDevice
*d
, uint64_t off
)
26 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
27 return readw(dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
30 static uint32_t qvirtio_mmio_config_readl(QVirtioDevice
*d
, uint64_t off
)
32 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
33 return readl(dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
36 static uint64_t qvirtio_mmio_config_readq(QVirtioDevice
*d
, uint64_t off
)
38 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
39 return readq(dev
->addr
+ QVIRTIO_MMIO_DEVICE_SPECIFIC
+ off
);
42 static uint32_t qvirtio_mmio_get_features(QVirtioDevice
*d
)
44 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
45 writel(dev
->addr
+ QVIRTIO_MMIO_HOST_FEATURES_SEL
, 0);
46 return readl(dev
->addr
+ QVIRTIO_MMIO_HOST_FEATURES
);
49 static void qvirtio_mmio_set_features(QVirtioDevice
*d
, uint32_t features
)
51 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
52 dev
->features
= features
;
53 writel(dev
->addr
+ QVIRTIO_MMIO_GUEST_FEATURES_SEL
, 0);
54 writel(dev
->addr
+ QVIRTIO_MMIO_GUEST_FEATURES
, features
);
57 static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice
*d
)
59 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
63 static uint8_t qvirtio_mmio_get_status(QVirtioDevice
*d
)
65 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
66 return (uint8_t)readl(dev
->addr
+ QVIRTIO_MMIO_DEVICE_STATUS
);
69 static void qvirtio_mmio_set_status(QVirtioDevice
*d
, uint8_t status
)
71 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
72 writel(dev
->addr
+ QVIRTIO_MMIO_DEVICE_STATUS
, (uint32_t)status
);
75 static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice
*d
, QVirtQueue
*vq
)
77 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
80 isr
= readl(dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_STATUS
) & 1;
82 writel(dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_ACK
, 1);
89 static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice
*d
)
91 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
94 isr
= readl(dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_STATUS
) & 2;
96 writel(dev
->addr
+ QVIRTIO_MMIO_INTERRUPT_ACK
, 2);
103 static void qvirtio_mmio_queue_select(QVirtioDevice
*d
, uint16_t index
)
105 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
106 writel(dev
->addr
+ QVIRTIO_MMIO_QUEUE_SEL
, (uint32_t)index
);
108 g_assert_cmphex(readl(dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
), ==, 0);
111 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice
*d
)
113 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
114 return (uint16_t)readl(dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM_MAX
);
117 static void qvirtio_mmio_set_queue_address(QVirtioDevice
*d
, uint32_t pfn
)
119 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
120 writel(dev
->addr
+ QVIRTIO_MMIO_QUEUE_PFN
, pfn
);
123 static QVirtQueue
*qvirtio_mmio_virtqueue_setup(QVirtioDevice
*d
,
124 QGuestAllocator
*alloc
, uint16_t index
)
126 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
130 vq
= g_malloc0(sizeof(*vq
));
131 qvirtio_mmio_queue_select(d
, index
);
132 writel(dev
->addr
+ QVIRTIO_MMIO_QUEUE_ALIGN
, dev
->page_size
);
135 vq
->size
= qvirtio_mmio_get_queue_size(d
);
137 vq
->num_free
= vq
->size
;
138 vq
->align
= dev
->page_size
;
139 vq
->indirect
= (dev
->features
& (1u << VIRTIO_RING_F_INDIRECT_DESC
)) != 0;
140 vq
->event
= (dev
->features
& (1u << VIRTIO_RING_F_EVENT_IDX
)) != 0;
142 writel(dev
->addr
+ QVIRTIO_MMIO_QUEUE_NUM
, vq
->size
);
144 /* Check different than 0 */
145 g_assert_cmpint(vq
->size
, !=, 0);
147 /* Check power of 2 */
148 g_assert_cmpint(vq
->size
& (vq
->size
- 1), ==, 0);
150 addr
= guest_alloc(alloc
, qvring_size(vq
->size
, dev
->page_size
));
151 qvring_init(alloc
, vq
, addr
);
152 qvirtio_mmio_set_queue_address(d
, vq
->desc
/ dev
->page_size
);
157 static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue
*vq
,
158 QGuestAllocator
*alloc
)
160 guest_free(alloc
, vq
->desc
);
164 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice
*d
, QVirtQueue
*vq
)
166 QVirtioMMIODevice
*dev
= (QVirtioMMIODevice
*)d
;
167 writel(dev
->addr
+ QVIRTIO_MMIO_QUEUE_NOTIFY
, vq
->index
);
170 const QVirtioBus qvirtio_mmio
= {
171 .config_readb
= qvirtio_mmio_config_readb
,
172 .config_readw
= qvirtio_mmio_config_readw
,
173 .config_readl
= qvirtio_mmio_config_readl
,
174 .config_readq
= qvirtio_mmio_config_readq
,
175 .get_features
= qvirtio_mmio_get_features
,
176 .set_features
= qvirtio_mmio_set_features
,
177 .get_guest_features
= qvirtio_mmio_get_guest_features
,
178 .get_status
= qvirtio_mmio_get_status
,
179 .set_status
= qvirtio_mmio_set_status
,
180 .get_queue_isr_status
= qvirtio_mmio_get_queue_isr_status
,
181 .get_config_isr_status
= qvirtio_mmio_get_config_isr_status
,
182 .queue_select
= qvirtio_mmio_queue_select
,
183 .get_queue_size
= qvirtio_mmio_get_queue_size
,
184 .set_queue_address
= qvirtio_mmio_set_queue_address
,
185 .virtqueue_setup
= qvirtio_mmio_virtqueue_setup
,
186 .virtqueue_cleanup
= qvirtio_mmio_virtqueue_cleanup
,
187 .virtqueue_kick
= qvirtio_mmio_virtqueue_kick
,
190 QVirtioMMIODevice
*qvirtio_mmio_init_device(uint64_t addr
, uint32_t page_size
)
192 QVirtioMMIODevice
*dev
;
194 dev
= g_malloc0(sizeof(*dev
));
196 magic
= readl(addr
+ QVIRTIO_MMIO_MAGIC_VALUE
);
197 g_assert(magic
== ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
200 dev
->page_size
= page_size
;
201 dev
->vdev
.device_type
= readl(addr
+ QVIRTIO_MMIO_DEVICE_ID
);
202 dev
->vdev
.bus
= &qvirtio_mmio
;
204 writel(addr
+ QVIRTIO_MMIO_GUEST_PAGE_SIZE
, page_size
);