]>
Commit | Line | Data |
---|---|---|
967f97fa AL |
1 | /* |
2 | * Virtio Support | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #ifndef _QEMU_VIRTIO_H | |
15 | #define _QEMU_VIRTIO_H | |
16 | ||
967f97fa AL |
17 | #include "hw.h" |
18 | #include "pci.h" | |
19 | ||
20 | /* from Linux's linux/virtio_config.h */ | |
21 | ||
22 | /* Status byte for guest to report progress, and synchronize features. */ | |
23 | /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */ | |
24 | #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 | |
25 | /* We have found a driver for the device. */ | |
26 | #define VIRTIO_CONFIG_S_DRIVER 2 | |
27 | /* Driver has used its parts of the config, and is happy */ | |
28 | #define VIRTIO_CONFIG_S_DRIVER_OK 4 | |
29 | /* We've given up on this device. */ | |
30 | #define VIRTIO_CONFIG_S_FAILED 0x80 | |
31 | ||
32 | /* We notify when the ring is completely used, even if the guest is supressing | |
33 | * callbacks */ | |
34 | #define VIRTIO_F_NOTIFY_ON_EMPTY 24 | |
8eca6b1b AL |
35 | /* A guest should never accept this. It implies negotiation is broken. */ |
36 | #define VIRTIO_F_BAD_FEATURE 30 | |
967f97fa AL |
37 | |
38 | /* from Linux's linux/virtio_ring.h */ | |
39 | ||
40 | /* This marks a buffer as continuing via the next field. */ | |
41 | #define VRING_DESC_F_NEXT 1 | |
42 | /* This marks a buffer as write-only (otherwise read-only). */ | |
43 | #define VRING_DESC_F_WRITE 2 | |
44 | ||
45 | /* This means don't notify other side when buffer added. */ | |
46 | #define VRING_USED_F_NO_NOTIFY 1 | |
47 | /* This means don't interrupt guest when buffer consumed. */ | |
48 | #define VRING_AVAIL_F_NO_INTERRUPT 1 | |
49 | ||
50 | struct VirtQueue; | |
51 | ||
f46f15bc AL |
52 | static inline target_phys_addr_t vring_align(target_phys_addr_t addr, |
53 | unsigned long align) | |
54 | { | |
55 | return (addr + align - 1) & ~(align - 1); | |
56 | } | |
57 | ||
967f97fa AL |
58 | typedef struct VirtQueue VirtQueue; |
59 | typedef struct VirtIODevice VirtIODevice; | |
60 | ||
61 | #define VIRTQUEUE_MAX_SIZE 1024 | |
62 | ||
63 | typedef struct VirtQueueElement | |
64 | { | |
65 | unsigned int index; | |
66 | unsigned int out_num; | |
67 | unsigned int in_num; | |
68 | target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE]; | |
69 | struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; | |
70 | struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; | |
71 | } VirtQueueElement; | |
72 | ||
73 | #define VIRTIO_PCI_QUEUE_MAX 16 | |
74 | ||
75 | struct VirtIODevice | |
76 | { | |
77 | PCIDevice pci_dev; | |
78 | const char *name; | |
79 | uint32_t addr; | |
80 | uint8_t status; | |
81 | uint8_t isr; | |
82 | uint16_t queue_sel; | |
83 | uint32_t features; | |
84 | size_t config_len; | |
85 | void *config; | |
86 | uint32_t (*get_features)(VirtIODevice *vdev); | |
8eca6b1b | 87 | uint32_t (*bad_features)(VirtIODevice *vdev); |
967f97fa AL |
88 | void (*set_features)(VirtIODevice *vdev, uint32_t val); |
89 | void (*get_config)(VirtIODevice *vdev, uint8_t *config); | |
90 | void (*set_config)(VirtIODevice *vdev, const uint8_t *config); | |
91 | void (*reset)(VirtIODevice *vdev); | |
92 | VirtQueue *vq; | |
93 | }; | |
94 | ||
95 | VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name, | |
96 | uint16_t vendor, uint16_t device, | |
97 | uint16_t subvendor, uint16_t subdevice, | |
173a543b BS |
98 | uint16_t class_code, uint8_t pif, |
99 | size_t config_size, size_t struct_size); | |
967f97fa AL |
100 | |
101 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, | |
102 | void (*handle_output)(VirtIODevice *, | |
103 | VirtQueue *)); | |
104 | ||
105 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, | |
106 | unsigned int len); | |
107 | void virtqueue_flush(VirtQueue *vq, unsigned int count); | |
108 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, | |
109 | unsigned int len, unsigned int idx); | |
110 | ||
111 | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem); | |
112 | int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes); | |
113 | ||
114 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); | |
115 | ||
116 | void virtio_save(VirtIODevice *vdev, QEMUFile *f); | |
117 | ||
118 | void virtio_load(VirtIODevice *vdev, QEMUFile *f); | |
119 | ||
120 | void virtio_notify_config(VirtIODevice *vdev); | |
121 | ||
122 | void virtio_queue_set_notification(VirtQueue *vq, int enable); | |
123 | ||
124 | int virtio_queue_ready(VirtQueue *vq); | |
125 | ||
126 | int virtio_queue_empty(VirtQueue *vq); | |
127 | ||
128 | #endif |