4 * Copyright (c) 2011 Linaro Limited
7 * Peter Maydell <peter.maydell@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "hw/sysbus.h"
23 #include "hw/virtio/virtio.h"
24 #include "qemu/host-utils.h"
25 #include "hw/virtio/virtio-bus.h"
27 /* #define DEBUG_VIRTIO_MMIO */
29 #ifdef DEBUG_VIRTIO_MMIO
31 #define DPRINTF(fmt, ...) \
32 do { printf("virtio_mmio: " fmt , ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) do {} while (0)
39 #define TYPE_VIRTIO_MMIO_BUS "virtio-mmio-bus"
40 #define VIRTIO_MMIO_BUS(obj) \
41 OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_MMIO_BUS)
42 #define VIRTIO_MMIO_BUS_GET_CLASS(obj) \
43 OBJECT_GET_CLASS(VirtioBusClass, (obj), TYPE_VIRTIO_MMIO_BUS)
44 #define VIRTIO_MMIO_BUS_CLASS(klass) \
45 OBJECT_CLASS_CHECK(VirtioBusClass, (klass), TYPE_VIRTIO_MMIO_BUS)
48 #define TYPE_VIRTIO_MMIO "virtio-mmio"
49 #define VIRTIO_MMIO(obj) \
50 OBJECT_CHECK(VirtIOMMIOProxy, (obj), TYPE_VIRTIO_MMIO)
52 /* Memory mapped register offsets */
53 #define VIRTIO_MMIO_MAGIC 0x0
54 #define VIRTIO_MMIO_VERSION 0x4
55 #define VIRTIO_MMIO_DEVICEID 0x8
56 #define VIRTIO_MMIO_VENDORID 0xc
57 #define VIRTIO_MMIO_HOSTFEATURES 0x10
58 #define VIRTIO_MMIO_HOSTFEATURESSEL 0x14
59 #define VIRTIO_MMIO_GUESTFEATURES 0x20
60 #define VIRTIO_MMIO_GUESTFEATURESSEL 0x24
61 #define VIRTIO_MMIO_GUESTPAGESIZE 0x28
62 #define VIRTIO_MMIO_QUEUESEL 0x30
63 #define VIRTIO_MMIO_QUEUENUMMAX 0x34
64 #define VIRTIO_MMIO_QUEUENUM 0x38
65 #define VIRTIO_MMIO_QUEUEALIGN 0x3c
66 #define VIRTIO_MMIO_QUEUEPFN 0x40
67 #define VIRTIO_MMIO_QUEUENOTIFY 0x50
68 #define VIRTIO_MMIO_INTERRUPTSTATUS 0x60
69 #define VIRTIO_MMIO_INTERRUPTACK 0x64
70 #define VIRTIO_MMIO_STATUS 0x70
71 /* Device specific config space starts here */
72 #define VIRTIO_MMIO_CONFIG 0x100
74 #define VIRT_MAGIC 0x74726976 /* 'virt' */
75 #define VIRT_VERSION 1
76 #define VIRT_VENDOR 0x554D4551 /* 'QEMU' */
80 SysBusDevice parent_obj
;
83 /* Guest accessible state needing migration and reset */
84 uint32_t host_features_sel
;
85 uint32_t guest_features_sel
;
86 uint32_t guest_page_shift
;
91 static uint64_t virtio_mmio_read(void *opaque
, hwaddr offset
, unsigned size
)
93 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
94 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
96 DPRINTF("virtio_mmio_read offset 0x%x\n", (int)offset
);
99 /* If no backend is present, we treat most registers as
100 * read-as-zero, except for the magic number, version and
101 * vendor ID. This is not strictly sanctioned by the virtio
102 * spec, but it allows us to provide transports with no backend
103 * plugged in which don't confuse Linux's virtio code: the
104 * probe won't complain about the bad magic number, but the
105 * device ID of zero means no backend will claim it.
108 case VIRTIO_MMIO_MAGIC
:
110 case VIRTIO_MMIO_VERSION
:
112 case VIRTIO_MMIO_VENDORID
:
119 if (offset
>= VIRTIO_MMIO_CONFIG
) {
120 offset
-= VIRTIO_MMIO_CONFIG
;
123 return virtio_config_readb(vdev
, offset
);
125 return virtio_config_readw(vdev
, offset
);
127 return virtio_config_readl(vdev
, offset
);
133 DPRINTF("wrong size access to register!\n");
137 case VIRTIO_MMIO_MAGIC
:
139 case VIRTIO_MMIO_VERSION
:
141 case VIRTIO_MMIO_DEVICEID
:
142 return vdev
->device_id
;
143 case VIRTIO_MMIO_VENDORID
:
145 case VIRTIO_MMIO_HOSTFEATURES
:
146 if (proxy
->host_features_sel
) {
149 return vdev
->host_features
;
150 case VIRTIO_MMIO_QUEUENUMMAX
:
151 if (!virtio_queue_get_num(vdev
, vdev
->queue_sel
)) {
154 return VIRTQUEUE_MAX_SIZE
;
155 case VIRTIO_MMIO_QUEUEPFN
:
156 return virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
157 >> proxy
->guest_page_shift
;
158 case VIRTIO_MMIO_INTERRUPTSTATUS
:
160 case VIRTIO_MMIO_STATUS
:
162 case VIRTIO_MMIO_HOSTFEATURESSEL
:
163 case VIRTIO_MMIO_GUESTFEATURES
:
164 case VIRTIO_MMIO_GUESTFEATURESSEL
:
165 case VIRTIO_MMIO_GUESTPAGESIZE
:
166 case VIRTIO_MMIO_QUEUESEL
:
167 case VIRTIO_MMIO_QUEUENUM
:
168 case VIRTIO_MMIO_QUEUEALIGN
:
169 case VIRTIO_MMIO_QUEUENOTIFY
:
170 case VIRTIO_MMIO_INTERRUPTACK
:
171 DPRINTF("read of write-only register\n");
174 DPRINTF("bad register offset\n");
180 static void virtio_mmio_write(void *opaque
, hwaddr offset
, uint64_t value
,
183 VirtIOMMIOProxy
*proxy
= (VirtIOMMIOProxy
*)opaque
;
184 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
186 DPRINTF("virtio_mmio_write offset 0x%x value 0x%" PRIx64
"\n",
190 /* If no backend is present, we just make all registers
191 * write-ignored. This allows us to provide transports with
192 * no backend plugged in.
197 if (offset
>= VIRTIO_MMIO_CONFIG
) {
198 offset
-= VIRTIO_MMIO_CONFIG
;
201 virtio_config_writeb(vdev
, offset
, value
);
204 virtio_config_writew(vdev
, offset
, value
);
207 virtio_config_writel(vdev
, offset
, value
);
215 DPRINTF("wrong size access to register!\n");
219 case VIRTIO_MMIO_HOSTFEATURESSEL
:
220 proxy
->host_features_sel
= value
;
222 case VIRTIO_MMIO_GUESTFEATURES
:
223 if (!proxy
->guest_features_sel
) {
224 virtio_set_features(vdev
, value
);
227 case VIRTIO_MMIO_GUESTFEATURESSEL
:
228 proxy
->guest_features_sel
= value
;
230 case VIRTIO_MMIO_GUESTPAGESIZE
:
231 proxy
->guest_page_shift
= ctz32(value
);
232 if (proxy
->guest_page_shift
> 31) {
233 proxy
->guest_page_shift
= 0;
235 DPRINTF("guest page size %" PRIx64
" shift %d\n", value
,
236 proxy
->guest_page_shift
);
238 case VIRTIO_MMIO_QUEUESEL
:
239 if (value
< VIRTIO_PCI_QUEUE_MAX
) {
240 vdev
->queue_sel
= value
;
243 case VIRTIO_MMIO_QUEUENUM
:
244 DPRINTF("mmio_queue write %d max %d\n", (int)value
, VIRTQUEUE_MAX_SIZE
);
245 virtio_queue_set_num(vdev
, vdev
->queue_sel
, value
);
247 case VIRTIO_MMIO_QUEUEALIGN
:
248 virtio_queue_set_align(vdev
, vdev
->queue_sel
, value
);
250 case VIRTIO_MMIO_QUEUEPFN
:
254 virtio_queue_set_addr(vdev
, vdev
->queue_sel
,
255 value
<< proxy
->guest_page_shift
);
258 case VIRTIO_MMIO_QUEUENOTIFY
:
259 if (value
< VIRTIO_PCI_QUEUE_MAX
) {
260 virtio_queue_notify(vdev
, value
);
263 case VIRTIO_MMIO_INTERRUPTACK
:
265 virtio_update_irq(vdev
);
267 case VIRTIO_MMIO_STATUS
:
268 virtio_set_status(vdev
, value
& 0xff);
269 if (vdev
->status
== 0) {
273 case VIRTIO_MMIO_MAGIC
:
274 case VIRTIO_MMIO_VERSION
:
275 case VIRTIO_MMIO_DEVICEID
:
276 case VIRTIO_MMIO_VENDORID
:
277 case VIRTIO_MMIO_HOSTFEATURES
:
278 case VIRTIO_MMIO_QUEUENUMMAX
:
279 case VIRTIO_MMIO_INTERRUPTSTATUS
:
280 DPRINTF("write to readonly register\n");
284 DPRINTF("bad register offset\n");
288 static const MemoryRegionOps virtio_mem_ops
= {
289 .read
= virtio_mmio_read
,
290 .write
= virtio_mmio_write
,
291 .endianness
= DEVICE_NATIVE_ENDIAN
,
294 static void virtio_mmio_update_irq(DeviceState
*opaque
, uint16_t vector
)
296 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
297 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
303 level
= (vdev
->isr
!= 0);
304 DPRINTF("virtio_mmio setting IRQ %d\n", level
);
305 qemu_set_irq(proxy
->irq
, level
);
308 static int virtio_mmio_load_config(DeviceState
*opaque
, QEMUFile
*f
)
310 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
312 proxy
->host_features_sel
= qemu_get_be32(f
);
313 proxy
->guest_features_sel
= qemu_get_be32(f
);
314 proxy
->guest_page_shift
= qemu_get_be32(f
);
318 static void virtio_mmio_save_config(DeviceState
*opaque
, QEMUFile
*f
)
320 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
322 qemu_put_be32(f
, proxy
->host_features_sel
);
323 qemu_put_be32(f
, proxy
->guest_features_sel
);
324 qemu_put_be32(f
, proxy
->guest_page_shift
);
327 static void virtio_mmio_reset(DeviceState
*d
)
329 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
331 virtio_bus_reset(&proxy
->bus
);
332 proxy
->host_features_sel
= 0;
333 proxy
->guest_features_sel
= 0;
334 proxy
->guest_page_shift
= 0;
337 /* virtio-mmio device */
339 /* This is called by virtio-bus just after the device is plugged. */
340 static void virtio_mmio_device_plugged(DeviceState
*opaque
)
342 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(opaque
);
343 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
345 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_NOTIFY_ON_EMPTY
);
348 static void virtio_mmio_realizefn(DeviceState
*d
, Error
**errp
)
350 VirtIOMMIOProxy
*proxy
= VIRTIO_MMIO(d
);
351 SysBusDevice
*sbd
= SYS_BUS_DEVICE(d
);
353 qbus_create_inplace(&proxy
->bus
, sizeof(proxy
->bus
), TYPE_VIRTIO_MMIO_BUS
,
355 sysbus_init_irq(sbd
, &proxy
->irq
);
356 memory_region_init_io(&proxy
->iomem
, OBJECT(d
), &virtio_mem_ops
, proxy
,
357 TYPE_VIRTIO_MMIO
, 0x200);
358 sysbus_init_mmio(sbd
, &proxy
->iomem
);
361 static void virtio_mmio_class_init(ObjectClass
*klass
, void *data
)
363 DeviceClass
*dc
= DEVICE_CLASS(klass
);
365 dc
->realize
= virtio_mmio_realizefn
;
366 dc
->reset
= virtio_mmio_reset
;
367 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
370 static const TypeInfo virtio_mmio_info
= {
371 .name
= TYPE_VIRTIO_MMIO
,
372 .parent
= TYPE_SYS_BUS_DEVICE
,
373 .instance_size
= sizeof(VirtIOMMIOProxy
),
374 .class_init
= virtio_mmio_class_init
,
377 /* virtio-mmio-bus. */
379 static void virtio_mmio_bus_class_init(ObjectClass
*klass
, void *data
)
381 BusClass
*bus_class
= BUS_CLASS(klass
);
382 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
384 k
->notify
= virtio_mmio_update_irq
;
385 k
->save_config
= virtio_mmio_save_config
;
386 k
->load_config
= virtio_mmio_load_config
;
387 k
->device_plugged
= virtio_mmio_device_plugged
;
388 k
->has_variable_vring_alignment
= true;
389 bus_class
->max_dev
= 1;
392 static const TypeInfo virtio_mmio_bus_info
= {
393 .name
= TYPE_VIRTIO_MMIO_BUS
,
394 .parent
= TYPE_VIRTIO_BUS
,
395 .instance_size
= sizeof(VirtioBusState
),
396 .class_init
= virtio_mmio_bus_class_init
,
399 static void virtio_mmio_register_types(void)
401 type_register_static(&virtio_mmio_bus_info
);
402 type_register_static(&virtio_mmio_info
);
405 type_init(virtio_mmio_register_types
)