]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/virtio/virtio-mmio.c
Include hw/irq.h a lot less
[thirdparty/qemu.git] / hw / virtio / virtio-mmio.c
CommitLineData
4b52530b
PM
1/*
2 * Virtio MMIO bindings
3 *
4 * Copyright (c) 2011 Linaro Limited
5 *
6 * Author:
7 * Peter Maydell <peter.maydell@linaro.org>
8 *
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.
12 *
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.
17 *
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/>.
20 */
21
9b8bfe21 22#include "qemu/osdep.h"
7e71da7f 23#include "standard-headers/linux/virtio_mmio.h"
64552b6b 24#include "hw/irq.h"
4b52530b
PM
25#include "hw/sysbus.h"
26#include "hw/virtio/virtio.h"
ca77ee28 27#include "migration/qemu-file-types.h"
4b52530b 28#include "qemu/host-utils.h"
0b8fa32f 29#include "qemu/module.h"
434027ba 30#include "sysemu/kvm.h"
4b52530b 31#include "hw/virtio/virtio-bus.h"
434027ba 32#include "qemu/error-report.h"
da1804d1
BL
33#include "qemu/log.h"
34#include "trace.h"
4b52530b
PM
35
36/* QOM macros */
37/* virtio-mmio-bus */
38#define TYPE_VIRTIO_MMIO_BUS "virtio-mmio-bus"
39#define VIRTIO_MMIO_BUS(obj) \
40 OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_MMIO_BUS)
41#define VIRTIO_MMIO_BUS_GET_CLASS(obj) \
42 OBJECT_GET_CLASS(VirtioBusClass, (obj), TYPE_VIRTIO_MMIO_BUS)
43#define VIRTIO_MMIO_BUS_CLASS(klass) \
44 OBJECT_CLASS_CHECK(VirtioBusClass, (klass), TYPE_VIRTIO_MMIO_BUS)
45
46/* virtio-mmio */
47#define TYPE_VIRTIO_MMIO "virtio-mmio"
48#define VIRTIO_MMIO(obj) \
49 OBJECT_CHECK(VirtIOMMIOProxy, (obj), TYPE_VIRTIO_MMIO)
50
4b52530b
PM
51#define VIRT_MAGIC 0x74726976 /* 'virt' */
52#define VIRT_VERSION 1
53#define VIRT_VENDOR 0x554D4551 /* 'QEMU' */
54
55typedef struct {
56 /* Generic */
57 SysBusDevice parent_obj;
58 MemoryRegion iomem;
59 qemu_irq irq;
4b52530b
PM
60 /* Guest accessible state needing migration and reset */
61 uint32_t host_features_sel;
62 uint32_t guest_features_sel;
63 uint32_t guest_page_shift;
64 /* virtio-bus */
65 VirtioBusState bus;
f58b39d2 66 bool format_transport_address;
4b52530b
PM
67} VirtIOMMIOProxy;
68
8e93cef1 69static bool virtio_mmio_ioeventfd_enabled(DeviceState *d)
c0971bcb 70{
8e93cef1 71 return kvm_eventfds_enabled();
434027ba
YSP
72}
73
c0971bcb
CH
74static int virtio_mmio_ioeventfd_assign(DeviceState *d,
75 EventNotifier *notifier,
76 int n, bool assign)
434027ba 77{
c0971bcb 78 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
434027ba 79
c0971bcb 80 if (assign) {
7e71da7f 81 memory_region_add_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
c0971bcb
CH
82 true, n, notifier);
83 } else {
7e71da7f 84 memory_region_del_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
c0971bcb 85 true, n, notifier);
434027ba 86 }
c0971bcb
CH
87 return 0;
88}
434027ba 89
c0971bcb
CH
90static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy *proxy)
91{
92 virtio_bus_start_ioeventfd(&proxy->bus);
93}
434027ba 94
c0971bcb
CH
95static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy *proxy)
96{
97 virtio_bus_stop_ioeventfd(&proxy->bus);
434027ba
YSP
98}
99
4b52530b
PM
100static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
101{
102 VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
06d3dff0 103 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
4b52530b 104
da1804d1 105 trace_virtio_mmio_read(offset);
4b52530b
PM
106
107 if (!vdev) {
108 /* If no backend is present, we treat most registers as
109 * read-as-zero, except for the magic number, version and
110 * vendor ID. This is not strictly sanctioned by the virtio
111 * spec, but it allows us to provide transports with no backend
112 * plugged in which don't confuse Linux's virtio code: the
113 * probe won't complain about the bad magic number, but the
114 * device ID of zero means no backend will claim it.
115 */
116 switch (offset) {
7e71da7f 117 case VIRTIO_MMIO_MAGIC_VALUE:
4b52530b
PM
118 return VIRT_MAGIC;
119 case VIRTIO_MMIO_VERSION:
120 return VIRT_VERSION;
7e71da7f 121 case VIRTIO_MMIO_VENDOR_ID:
4b52530b
PM
122 return VIRT_VENDOR;
123 default:
124 return 0;
125 }
126 }
127
128 if (offset >= VIRTIO_MMIO_CONFIG) {
129 offset -= VIRTIO_MMIO_CONFIG;
130 switch (size) {
131 case 1:
132 return virtio_config_readb(vdev, offset);
133 case 2:
134 return virtio_config_readw(vdev, offset);
135 case 4:
136 return virtio_config_readl(vdev, offset);
137 default:
138 abort();
139 }
140 }
141 if (size != 4) {
da1804d1
BL
142 qemu_log_mask(LOG_GUEST_ERROR,
143 "%s: wrong size access to register!\n",
144 __func__);
4b52530b
PM
145 return 0;
146 }
147 switch (offset) {
7e71da7f 148 case VIRTIO_MMIO_MAGIC_VALUE:
4b52530b
PM
149 return VIRT_MAGIC;
150 case VIRTIO_MMIO_VERSION:
151 return VIRT_VERSION;
7e71da7f 152 case VIRTIO_MMIO_DEVICE_ID:
4b52530b 153 return vdev->device_id;
7e71da7f 154 case VIRTIO_MMIO_VENDOR_ID:
4b52530b 155 return VIRT_VENDOR;
7e71da7f 156 case VIRTIO_MMIO_DEVICE_FEATURES:
4b52530b
PM
157 if (proxy->host_features_sel) {
158 return 0;
159 }
6b8f1020 160 return vdev->host_features;
7e71da7f 161 case VIRTIO_MMIO_QUEUE_NUM_MAX:
f7b803b3
PM
162 if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {
163 return 0;
164 }
4b52530b 165 return VIRTQUEUE_MAX_SIZE;
7e71da7f 166 case VIRTIO_MMIO_QUEUE_PFN:
4b52530b
PM
167 return virtio_queue_get_addr(vdev, vdev->queue_sel)
168 >> proxy->guest_page_shift;
7e71da7f 169 case VIRTIO_MMIO_INTERRUPT_STATUS:
0687c37c 170 return atomic_read(&vdev->isr);
4b52530b
PM
171 case VIRTIO_MMIO_STATUS:
172 return vdev->status;
7e71da7f
MT
173 case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
174 case VIRTIO_MMIO_DRIVER_FEATURES:
175 case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
176 case VIRTIO_MMIO_GUEST_PAGE_SIZE:
177 case VIRTIO_MMIO_QUEUE_SEL:
178 case VIRTIO_MMIO_QUEUE_NUM:
179 case VIRTIO_MMIO_QUEUE_ALIGN:
180 case VIRTIO_MMIO_QUEUE_NOTIFY:
181 case VIRTIO_MMIO_INTERRUPT_ACK:
da1804d1
BL
182 qemu_log_mask(LOG_GUEST_ERROR,
183 "%s: read of write-only register\n",
184 __func__);
4b52530b
PM
185 return 0;
186 default:
da1804d1 187 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad register offset\n", __func__);
4b52530b
PM
188 return 0;
189 }
190 return 0;
191}
192
193static void virtio_mmio_write(void *opaque, hwaddr offset, uint64_t value,
194 unsigned size)
195{
196 VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
06d3dff0 197 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
4b52530b 198
da1804d1 199 trace_virtio_mmio_write_offset(offset, value);
4b52530b
PM
200
201 if (!vdev) {
202 /* If no backend is present, we just make all registers
203 * write-ignored. This allows us to provide transports with
204 * no backend plugged in.
205 */
206 return;
207 }
208
209 if (offset >= VIRTIO_MMIO_CONFIG) {
210 offset -= VIRTIO_MMIO_CONFIG;
211 switch (size) {
212 case 1:
213 virtio_config_writeb(vdev, offset, value);
214 break;
215 case 2:
216 virtio_config_writew(vdev, offset, value);
217 break;
218 case 4:
219 virtio_config_writel(vdev, offset, value);
220 break;
221 default:
222 abort();
223 }
224 return;
225 }
226 if (size != 4) {
da1804d1
BL
227 qemu_log_mask(LOG_GUEST_ERROR,
228 "%s: wrong size access to register!\n",
229 __func__);
4b52530b
PM
230 return;
231 }
232 switch (offset) {
7e71da7f 233 case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
4b52530b
PM
234 proxy->host_features_sel = value;
235 break;
7e71da7f 236 case VIRTIO_MMIO_DRIVER_FEATURES:
4b52530b
PM
237 if (!proxy->guest_features_sel) {
238 virtio_set_features(vdev, value);
239 }
240 break;
7e71da7f 241 case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
4b52530b
PM
242 proxy->guest_features_sel = value;
243 break;
7e71da7f 244 case VIRTIO_MMIO_GUEST_PAGE_SIZE:
4b52530b
PM
245 proxy->guest_page_shift = ctz32(value);
246 if (proxy->guest_page_shift > 31) {
247 proxy->guest_page_shift = 0;
248 }
da1804d1 249 trace_virtio_mmio_guest_page(value, proxy->guest_page_shift);
4b52530b 250 break;
7e71da7f 251 case VIRTIO_MMIO_QUEUE_SEL:
87b3bd1c 252 if (value < VIRTIO_QUEUE_MAX) {
4b52530b
PM
253 vdev->queue_sel = value;
254 }
255 break;
7e71da7f 256 case VIRTIO_MMIO_QUEUE_NUM:
da1804d1 257 trace_virtio_mmio_queue_write(value, VIRTQUEUE_MAX_SIZE);
4b52530b 258 virtio_queue_set_num(vdev, vdev->queue_sel, value);
ab223c95
CH
259 /* Note: only call this function for legacy devices */
260 virtio_queue_update_rings(vdev, vdev->queue_sel);
4b52530b 261 break;
7e71da7f 262 case VIRTIO_MMIO_QUEUE_ALIGN:
ab223c95 263 /* Note: this is only valid for legacy devices */
4b52530b
PM
264 virtio_queue_set_align(vdev, vdev->queue_sel, value);
265 break;
7e71da7f 266 case VIRTIO_MMIO_QUEUE_PFN:
4b52530b
PM
267 if (value == 0) {
268 virtio_reset(vdev);
269 } else {
270 virtio_queue_set_addr(vdev, vdev->queue_sel,
271 value << proxy->guest_page_shift);
272 }
273 break;
7e71da7f 274 case VIRTIO_MMIO_QUEUE_NOTIFY:
87b3bd1c 275 if (value < VIRTIO_QUEUE_MAX) {
4b52530b
PM
276 virtio_queue_notify(vdev, value);
277 }
278 break;
7e71da7f 279 case VIRTIO_MMIO_INTERRUPT_ACK:
0687c37c 280 atomic_and(&vdev->isr, ~value);
4b52530b
PM
281 virtio_update_irq(vdev);
282 break;
283 case VIRTIO_MMIO_STATUS:
434027ba
YSP
284 if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) {
285 virtio_mmio_stop_ioeventfd(proxy);
286 }
287
4b52530b 288 virtio_set_status(vdev, value & 0xff);
434027ba
YSP
289
290 if (value & VIRTIO_CONFIG_S_DRIVER_OK) {
291 virtio_mmio_start_ioeventfd(proxy);
292 }
293
4b52530b
PM
294 if (vdev->status == 0) {
295 virtio_reset(vdev);
296 }
297 break;
7e71da7f 298 case VIRTIO_MMIO_MAGIC_VALUE:
4b52530b 299 case VIRTIO_MMIO_VERSION:
7e71da7f
MT
300 case VIRTIO_MMIO_DEVICE_ID:
301 case VIRTIO_MMIO_VENDOR_ID:
302 case VIRTIO_MMIO_DEVICE_FEATURES:
303 case VIRTIO_MMIO_QUEUE_NUM_MAX:
304 case VIRTIO_MMIO_INTERRUPT_STATUS:
da1804d1
BL
305 qemu_log_mask(LOG_GUEST_ERROR,
306 "%s: write to readonly register\n",
307 __func__);
4b52530b
PM
308 break;
309
310 default:
da1804d1 311 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad register offset\n", __func__);
4b52530b
PM
312 }
313}
314
315static const MemoryRegionOps virtio_mem_ops = {
316 .read = virtio_mmio_read,
317 .write = virtio_mmio_write,
318 .endianness = DEVICE_NATIVE_ENDIAN,
319};
320
321static void virtio_mmio_update_irq(DeviceState *opaque, uint16_t vector)
322{
323 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
06d3dff0 324 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
4b52530b
PM
325 int level;
326
06d3dff0 327 if (!vdev) {
4b52530b
PM
328 return;
329 }
0687c37c 330 level = (atomic_read(&vdev->isr) != 0);
da1804d1 331 trace_virtio_mmio_setting_irq(level);
4b52530b
PM
332 qemu_set_irq(proxy->irq, level);
333}
334
4b52530b
PM
335static int virtio_mmio_load_config(DeviceState *opaque, QEMUFile *f)
336{
337 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
338
339 proxy->host_features_sel = qemu_get_be32(f);
340 proxy->guest_features_sel = qemu_get_be32(f);
341 proxy->guest_page_shift = qemu_get_be32(f);
342 return 0;
343}
344
345static void virtio_mmio_save_config(DeviceState *opaque, QEMUFile *f)
346{
347 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
348
349 qemu_put_be32(f, proxy->host_features_sel);
350 qemu_put_be32(f, proxy->guest_features_sel);
351 qemu_put_be32(f, proxy->guest_page_shift);
352}
353
354static void virtio_mmio_reset(DeviceState *d)
355{
356 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
357
434027ba 358 virtio_mmio_stop_ioeventfd(proxy);
4b52530b
PM
359 virtio_bus_reset(&proxy->bus);
360 proxy->host_features_sel = 0;
361 proxy->guest_features_sel = 0;
362 proxy->guest_page_shift = 0;
363}
364
434027ba
YSP
365static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
366 bool with_irqfd)
367{
368 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
369 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
370 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
371 VirtQueue *vq = virtio_get_queue(vdev, n);
372 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
373
374 if (assign) {
375 int r = event_notifier_init(notifier, 0);
376 if (r < 0) {
377 return r;
378 }
379 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
380 } else {
381 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
382 event_notifier_cleanup(notifier);
383 }
384
2858bc68 385 if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
434027ba
YSP
386 vdc->guest_notifier_mask(vdev, n, !assign);
387 }
388
389 return 0;
390}
391
392static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
393 bool assign)
394{
395 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
396 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
397 /* TODO: need to check if kvm-arm supports irqfd */
398 bool with_irqfd = false;
399 int r, n;
400
401 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
402
403 for (n = 0; n < nvqs; n++) {
404 if (!virtio_queue_get_num(vdev, n)) {
405 break;
406 }
407
408 r = virtio_mmio_set_guest_notifier(d, n, assign, with_irqfd);
409 if (r < 0) {
410 goto assign_error;
411 }
412 }
413
414 return 0;
415
416assign_error:
417 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
418 assert(assign);
419 while (--n >= 0) {
420 virtio_mmio_set_guest_notifier(d, n, !assign, false);
421 }
422 return r;
423}
424
4b52530b
PM
425/* virtio-mmio device */
426
f58b39d2
LE
427static Property virtio_mmio_properties[] = {
428 DEFINE_PROP_BOOL("format_transport_address", VirtIOMMIOProxy,
429 format_transport_address, true),
430 DEFINE_PROP_END_OF_LIST(),
431};
432
4b52530b
PM
433static void virtio_mmio_realizefn(DeviceState *d, Error **errp)
434{
435 VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
436 SysBusDevice *sbd = SYS_BUS_DEVICE(d);
437
2f4f6035
IM
438 qbus_create_inplace(&proxy->bus, sizeof(proxy->bus), TYPE_VIRTIO_MMIO_BUS,
439 d, NULL);
4b52530b
PM
440 sysbus_init_irq(sbd, &proxy->irq);
441 memory_region_init_io(&proxy->iomem, OBJECT(d), &virtio_mem_ops, proxy,
442 TYPE_VIRTIO_MMIO, 0x200);
443 sysbus_init_mmio(sbd, &proxy->iomem);
444}
445
446static void virtio_mmio_class_init(ObjectClass *klass, void *data)
447{
448 DeviceClass *dc = DEVICE_CLASS(klass);
449
450 dc->realize = virtio_mmio_realizefn;
451 dc->reset = virtio_mmio_reset;
125ee0ed 452 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
f58b39d2 453 dc->props = virtio_mmio_properties;
4b52530b
PM
454}
455
456static const TypeInfo virtio_mmio_info = {
457 .name = TYPE_VIRTIO_MMIO,
458 .parent = TYPE_SYS_BUS_DEVICE,
459 .instance_size = sizeof(VirtIOMMIOProxy),
460 .class_init = virtio_mmio_class_init,
461};
462
463/* virtio-mmio-bus. */
464
f58b39d2
LE
465static char *virtio_mmio_bus_get_dev_path(DeviceState *dev)
466{
467 BusState *virtio_mmio_bus;
468 VirtIOMMIOProxy *virtio_mmio_proxy;
469 char *proxy_path;
470 SysBusDevice *proxy_sbd;
471 char *path;
472
473 virtio_mmio_bus = qdev_get_parent_bus(dev);
474 virtio_mmio_proxy = VIRTIO_MMIO(virtio_mmio_bus->parent);
475 proxy_path = qdev_get_dev_path(DEVICE(virtio_mmio_proxy));
476
477 /*
478 * If @format_transport_address is false, then we just perform the same as
479 * virtio_bus_get_dev_path(): we delegate the address formatting for the
480 * device on the virtio-mmio bus to the bus that the virtio-mmio proxy
481 * (i.e., the device that implements the virtio-mmio bus) resides on. In
482 * this case the base address of the virtio-mmio transport will be
483 * invisible.
484 */
485 if (!virtio_mmio_proxy->format_transport_address) {
486 return proxy_path;
487 }
488
489 /* Otherwise, we append the base address of the transport. */
490 proxy_sbd = SYS_BUS_DEVICE(virtio_mmio_proxy);
491 assert(proxy_sbd->num_mmio == 1);
492 assert(proxy_sbd->mmio[0].memory == &virtio_mmio_proxy->iomem);
493
494 if (proxy_path) {
495 path = g_strdup_printf("%s/virtio-mmio@" TARGET_FMT_plx, proxy_path,
496 proxy_sbd->mmio[0].addr);
497 } else {
498 path = g_strdup_printf("virtio-mmio@" TARGET_FMT_plx,
499 proxy_sbd->mmio[0].addr);
500 }
501 g_free(proxy_path);
502 return path;
503}
504
4b52530b
PM
505static void virtio_mmio_bus_class_init(ObjectClass *klass, void *data)
506{
507 BusClass *bus_class = BUS_CLASS(klass);
508 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
509
510 k->notify = virtio_mmio_update_irq;
511 k->save_config = virtio_mmio_save_config;
512 k->load_config = virtio_mmio_load_config;
434027ba 513 k->set_guest_notifiers = virtio_mmio_set_guest_notifiers;
8e93cef1 514 k->ioeventfd_enabled = virtio_mmio_ioeventfd_enabled;
c0971bcb 515 k->ioeventfd_assign = virtio_mmio_ioeventfd_assign;
4b52530b
PM
516 k->has_variable_vring_alignment = true;
517 bus_class->max_dev = 1;
f58b39d2 518 bus_class->get_dev_path = virtio_mmio_bus_get_dev_path;
4b52530b
PM
519}
520
521static const TypeInfo virtio_mmio_bus_info = {
522 .name = TYPE_VIRTIO_MMIO_BUS,
523 .parent = TYPE_VIRTIO_BUS,
524 .instance_size = sizeof(VirtioBusState),
525 .class_init = virtio_mmio_bus_class_init,
526};
527
528static void virtio_mmio_register_types(void)
529{
530 type_register_static(&virtio_mmio_bus_info);
531 type_register_static(&virtio_mmio_info);
532}
533
534type_init(virtio_mmio_register_types)