root / hw / ivshmem.c @ 7e0a9247
History | View | Annotate | Download (22.5 kB)
1 | 6cbf4c8c | Cam Macdonell | /*
|
---|---|---|---|
2 | 6cbf4c8c | Cam Macdonell | * Inter-VM Shared Memory PCI device.
|
3 | 6cbf4c8c | Cam Macdonell | *
|
4 | 6cbf4c8c | Cam Macdonell | * Author:
|
5 | 6cbf4c8c | Cam Macdonell | * Cam Macdonell <cam@cs.ualberta.ca>
|
6 | 6cbf4c8c | Cam Macdonell | *
|
7 | 6cbf4c8c | Cam Macdonell | * Based On: cirrus_vga.c
|
8 | 6cbf4c8c | Cam Macdonell | * Copyright (c) 2004 Fabrice Bellard
|
9 | 6cbf4c8c | Cam Macdonell | * Copyright (c) 2004 Makoto Suzuki (suzu)
|
10 | 6cbf4c8c | Cam Macdonell | *
|
11 | 6cbf4c8c | Cam Macdonell | * and rtl8139.c
|
12 | 6cbf4c8c | Cam Macdonell | * Copyright (c) 2006 Igor Kovalenko
|
13 | 6cbf4c8c | Cam Macdonell | *
|
14 | 6cbf4c8c | Cam Macdonell | * This code is licensed under the GNU GPL v2.
|
15 | 6b620ca3 | Paolo Bonzini | *
|
16 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
17 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
18 | 6cbf4c8c | Cam Macdonell | */
|
19 | 6cbf4c8c | Cam Macdonell | #include "hw.h" |
20 | 6cbf4c8c | Cam Macdonell | #include "pc.h" |
21 | 6cbf4c8c | Cam Macdonell | #include "pci.h" |
22 | 6cbf4c8c | Cam Macdonell | #include "msix.h" |
23 | 6cbf4c8c | Cam Macdonell | #include "kvm.h" |
24 | 38e0735e | Anthony Liguori | #include "migration.h" |
25 | 38e0735e | Anthony Liguori | #include "qerror.h" |
26 | 563027cc | Paolo Bonzini | #include "event_notifier.h" |
27 | 6cbf4c8c | Cam Macdonell | |
28 | 6cbf4c8c | Cam Macdonell | #include <sys/mman.h> |
29 | 6cbf4c8c | Cam Macdonell | #include <sys/types.h> |
30 | 6cbf4c8c | Cam Macdonell | |
31 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_IOEVENTFD 0 |
32 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_MSI 1 |
33 | 6cbf4c8c | Cam Macdonell | |
34 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_PEER 0 |
35 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_MASTER 1 |
36 | 6cbf4c8c | Cam Macdonell | |
37 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_REG_BAR_SIZE 0x100 |
38 | 6cbf4c8c | Cam Macdonell | |
39 | 6cbf4c8c | Cam Macdonell | //#define DEBUG_IVSHMEM
|
40 | 6cbf4c8c | Cam Macdonell | #ifdef DEBUG_IVSHMEM
|
41 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_DPRINTF(fmt, ...) \
|
42 | 6cbf4c8c | Cam Macdonell | do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0) |
43 | 6cbf4c8c | Cam Macdonell | #else
|
44 | 6cbf4c8c | Cam Macdonell | #define IVSHMEM_DPRINTF(fmt, ...)
|
45 | 6cbf4c8c | Cam Macdonell | #endif
|
46 | 6cbf4c8c | Cam Macdonell | |
47 | 6cbf4c8c | Cam Macdonell | typedef struct Peer { |
48 | 6cbf4c8c | Cam Macdonell | int nb_eventfds;
|
49 | 563027cc | Paolo Bonzini | EventNotifier *eventfds; |
50 | 6cbf4c8c | Cam Macdonell | } Peer; |
51 | 6cbf4c8c | Cam Macdonell | |
52 | 6cbf4c8c | Cam Macdonell | typedef struct EventfdEntry { |
53 | 6cbf4c8c | Cam Macdonell | PCIDevice *pdev; |
54 | 6cbf4c8c | Cam Macdonell | int vector;
|
55 | 6cbf4c8c | Cam Macdonell | } EventfdEntry; |
56 | 6cbf4c8c | Cam Macdonell | |
57 | 6cbf4c8c | Cam Macdonell | typedef struct IVShmemState { |
58 | 6cbf4c8c | Cam Macdonell | PCIDevice dev; |
59 | 6cbf4c8c | Cam Macdonell | uint32_t intrmask; |
60 | 6cbf4c8c | Cam Macdonell | uint32_t intrstatus; |
61 | 6cbf4c8c | Cam Macdonell | uint32_t doorbell; |
62 | 6cbf4c8c | Cam Macdonell | |
63 | 6cbf4c8c | Cam Macdonell | CharDriverState **eventfd_chr; |
64 | 6cbf4c8c | Cam Macdonell | CharDriverState *server_chr; |
65 | cb06608e | Avi Kivity | MemoryRegion ivshmem_mmio; |
66 | 6cbf4c8c | Cam Macdonell | |
67 | cb06608e | Avi Kivity | /* We might need to register the BAR before we actually have the memory.
|
68 | cb06608e | Avi Kivity | * So prepare a container MemoryRegion for the BAR immediately and
|
69 | cb06608e | Avi Kivity | * add a subregion when we have the memory.
|
70 | cb06608e | Avi Kivity | */
|
71 | cb06608e | Avi Kivity | MemoryRegion bar; |
72 | cb06608e | Avi Kivity | MemoryRegion ivshmem; |
73 | 6cbf4c8c | Cam Macdonell | uint64_t ivshmem_size; /* size of shared memory region */
|
74 | c08ba66f | Gerd Hoffmann | uint32_t ivshmem_attr; |
75 | c08ba66f | Gerd Hoffmann | uint32_t ivshmem_64bit; |
76 | 6cbf4c8c | Cam Macdonell | int shm_fd; /* shared memory file descriptor */ |
77 | 6cbf4c8c | Cam Macdonell | |
78 | 6cbf4c8c | Cam Macdonell | Peer *peers; |
79 | 6cbf4c8c | Cam Macdonell | int nb_peers; /* how many guests we have space for */ |
80 | 6cbf4c8c | Cam Macdonell | int max_peer; /* maximum numbered peer */ |
81 | 6cbf4c8c | Cam Macdonell | |
82 | 6cbf4c8c | Cam Macdonell | int vm_id;
|
83 | 6cbf4c8c | Cam Macdonell | uint32_t vectors; |
84 | 6cbf4c8c | Cam Macdonell | uint32_t features; |
85 | 6cbf4c8c | Cam Macdonell | EventfdEntry *eventfd_table; |
86 | 6cbf4c8c | Cam Macdonell | |
87 | 38e0735e | Anthony Liguori | Error *migration_blocker; |
88 | 38e0735e | Anthony Liguori | |
89 | 6cbf4c8c | Cam Macdonell | char * shmobj;
|
90 | 6cbf4c8c | Cam Macdonell | char * sizearg;
|
91 | 6cbf4c8c | Cam Macdonell | char * role;
|
92 | 6cbf4c8c | Cam Macdonell | int role_val; /* scalar to avoid multiple string comparisons */ |
93 | 6cbf4c8c | Cam Macdonell | } IVShmemState; |
94 | 6cbf4c8c | Cam Macdonell | |
95 | 6cbf4c8c | Cam Macdonell | /* registers for the Inter-VM shared memory device */
|
96 | 6cbf4c8c | Cam Macdonell | enum ivshmem_registers {
|
97 | 6cbf4c8c | Cam Macdonell | INTRMASK = 0,
|
98 | 6cbf4c8c | Cam Macdonell | INTRSTATUS = 4,
|
99 | 6cbf4c8c | Cam Macdonell | IVPOSITION = 8,
|
100 | 6cbf4c8c | Cam Macdonell | DOORBELL = 12,
|
101 | 6cbf4c8c | Cam Macdonell | }; |
102 | 6cbf4c8c | Cam Macdonell | |
103 | 6cbf4c8c | Cam Macdonell | static inline uint32_t ivshmem_has_feature(IVShmemState *ivs, |
104 | 6cbf4c8c | Cam Macdonell | unsigned int feature) { |
105 | 6cbf4c8c | Cam Macdonell | return (ivs->features & (1 << feature)); |
106 | 6cbf4c8c | Cam Macdonell | } |
107 | 6cbf4c8c | Cam Macdonell | |
108 | 6cbf4c8c | Cam Macdonell | static inline bool is_power_of_two(uint64_t x) { |
109 | 6cbf4c8c | Cam Macdonell | return (x & (x - 1)) == 0; |
110 | 6cbf4c8c | Cam Macdonell | } |
111 | 6cbf4c8c | Cam Macdonell | |
112 | 6cbf4c8c | Cam Macdonell | /* accessing registers - based on rtl8139 */
|
113 | 6cbf4c8c | Cam Macdonell | static void ivshmem_update_irq(IVShmemState *s, int val) |
114 | 6cbf4c8c | Cam Macdonell | { |
115 | 6cbf4c8c | Cam Macdonell | int isr;
|
116 | 6cbf4c8c | Cam Macdonell | isr = (s->intrstatus & s->intrmask) & 0xffffffff;
|
117 | 6cbf4c8c | Cam Macdonell | |
118 | 6cbf4c8c | Cam Macdonell | /* don't print ISR resets */
|
119 | 6cbf4c8c | Cam Macdonell | if (isr) {
|
120 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
|
121 | 6cbf4c8c | Cam Macdonell | isr ? 1 : 0, s->intrstatus, s->intrmask); |
122 | 6cbf4c8c | Cam Macdonell | } |
123 | 6cbf4c8c | Cam Macdonell | |
124 | 6cbf4c8c | Cam Macdonell | qemu_set_irq(s->dev.irq[0], (isr != 0)); |
125 | 6cbf4c8c | Cam Macdonell | } |
126 | 6cbf4c8c | Cam Macdonell | |
127 | 6cbf4c8c | Cam Macdonell | static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val) |
128 | 6cbf4c8c | Cam Macdonell | { |
129 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
|
130 | 6cbf4c8c | Cam Macdonell | |
131 | 6cbf4c8c | Cam Macdonell | s->intrmask = val; |
132 | 6cbf4c8c | Cam Macdonell | |
133 | 6cbf4c8c | Cam Macdonell | ivshmem_update_irq(s, val); |
134 | 6cbf4c8c | Cam Macdonell | } |
135 | 6cbf4c8c | Cam Macdonell | |
136 | 6cbf4c8c | Cam Macdonell | static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
|
137 | 6cbf4c8c | Cam Macdonell | { |
138 | 6cbf4c8c | Cam Macdonell | uint32_t ret = s->intrmask; |
139 | 6cbf4c8c | Cam Macdonell | |
140 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
|
141 | 6cbf4c8c | Cam Macdonell | |
142 | 6cbf4c8c | Cam Macdonell | return ret;
|
143 | 6cbf4c8c | Cam Macdonell | } |
144 | 6cbf4c8c | Cam Macdonell | |
145 | 6cbf4c8c | Cam Macdonell | static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val) |
146 | 6cbf4c8c | Cam Macdonell | { |
147 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
|
148 | 6cbf4c8c | Cam Macdonell | |
149 | 6cbf4c8c | Cam Macdonell | s->intrstatus = val; |
150 | 6cbf4c8c | Cam Macdonell | |
151 | 6cbf4c8c | Cam Macdonell | ivshmem_update_irq(s, val); |
152 | 6cbf4c8c | Cam Macdonell | } |
153 | 6cbf4c8c | Cam Macdonell | |
154 | 6cbf4c8c | Cam Macdonell | static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
|
155 | 6cbf4c8c | Cam Macdonell | { |
156 | 6cbf4c8c | Cam Macdonell | uint32_t ret = s->intrstatus; |
157 | 6cbf4c8c | Cam Macdonell | |
158 | 6cbf4c8c | Cam Macdonell | /* reading ISR clears all interrupts */
|
159 | 6cbf4c8c | Cam Macdonell | s->intrstatus = 0;
|
160 | 6cbf4c8c | Cam Macdonell | |
161 | 6cbf4c8c | Cam Macdonell | ivshmem_update_irq(s, 0);
|
162 | 6cbf4c8c | Cam Macdonell | |
163 | 6cbf4c8c | Cam Macdonell | return ret;
|
164 | 6cbf4c8c | Cam Macdonell | } |
165 | 6cbf4c8c | Cam Macdonell | |
166 | a8170e5e | Avi Kivity | static void ivshmem_io_write(void *opaque, hwaddr addr, |
167 | cb06608e | Avi Kivity | uint64_t val, unsigned size)
|
168 | 6cbf4c8c | Cam Macdonell | { |
169 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = opaque; |
170 | 6cbf4c8c | Cam Macdonell | |
171 | 6cbf4c8c | Cam Macdonell | uint16_t dest = val >> 16;
|
172 | 6cbf4c8c | Cam Macdonell | uint16_t vector = val & 0xff;
|
173 | 6cbf4c8c | Cam Macdonell | |
174 | 6cbf4c8c | Cam Macdonell | addr &= 0xfc;
|
175 | 6cbf4c8c | Cam Macdonell | |
176 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr); |
177 | 6cbf4c8c | Cam Macdonell | switch (addr)
|
178 | 6cbf4c8c | Cam Macdonell | { |
179 | 6cbf4c8c | Cam Macdonell | case INTRMASK:
|
180 | 6cbf4c8c | Cam Macdonell | ivshmem_IntrMask_write(s, val); |
181 | 6cbf4c8c | Cam Macdonell | break;
|
182 | 6cbf4c8c | Cam Macdonell | |
183 | 6cbf4c8c | Cam Macdonell | case INTRSTATUS:
|
184 | 6cbf4c8c | Cam Macdonell | ivshmem_IntrStatus_write(s, val); |
185 | 6cbf4c8c | Cam Macdonell | break;
|
186 | 6cbf4c8c | Cam Macdonell | |
187 | 6cbf4c8c | Cam Macdonell | case DOORBELL:
|
188 | 6cbf4c8c | Cam Macdonell | /* check that dest VM ID is reasonable */
|
189 | 1b27d7a1 | Jes Sorensen | if (dest > s->max_peer) {
|
190 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
|
191 | 6cbf4c8c | Cam Macdonell | break;
|
192 | 6cbf4c8c | Cam Macdonell | } |
193 | 6cbf4c8c | Cam Macdonell | |
194 | 6cbf4c8c | Cam Macdonell | /* check doorbell range */
|
195 | 1b27d7a1 | Jes Sorensen | if (vector < s->peers[dest].nb_eventfds) {
|
196 | 563027cc | Paolo Bonzini | IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
|
197 | 563027cc | Paolo Bonzini | event_notifier_set(&s->peers[dest].eventfds[vector]); |
198 | 6cbf4c8c | Cam Macdonell | } |
199 | 6cbf4c8c | Cam Macdonell | break;
|
200 | 6cbf4c8c | Cam Macdonell | default:
|
201 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
|
202 | 6cbf4c8c | Cam Macdonell | } |
203 | 6cbf4c8c | Cam Macdonell | } |
204 | 6cbf4c8c | Cam Macdonell | |
205 | a8170e5e | Avi Kivity | static uint64_t ivshmem_io_read(void *opaque, hwaddr addr, |
206 | cb06608e | Avi Kivity | unsigned size)
|
207 | 6cbf4c8c | Cam Macdonell | { |
208 | 6cbf4c8c | Cam Macdonell | |
209 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = opaque; |
210 | 6cbf4c8c | Cam Macdonell | uint32_t ret; |
211 | 6cbf4c8c | Cam Macdonell | |
212 | 6cbf4c8c | Cam Macdonell | switch (addr)
|
213 | 6cbf4c8c | Cam Macdonell | { |
214 | 6cbf4c8c | Cam Macdonell | case INTRMASK:
|
215 | 6cbf4c8c | Cam Macdonell | ret = ivshmem_IntrMask_read(s); |
216 | 6cbf4c8c | Cam Macdonell | break;
|
217 | 6cbf4c8c | Cam Macdonell | |
218 | 6cbf4c8c | Cam Macdonell | case INTRSTATUS:
|
219 | 6cbf4c8c | Cam Macdonell | ret = ivshmem_IntrStatus_read(s); |
220 | 6cbf4c8c | Cam Macdonell | break;
|
221 | 6cbf4c8c | Cam Macdonell | |
222 | 6cbf4c8c | Cam Macdonell | case IVPOSITION:
|
223 | 6cbf4c8c | Cam Macdonell | /* return my VM ID if the memory is mapped */
|
224 | 6cbf4c8c | Cam Macdonell | if (s->shm_fd > 0) { |
225 | 6cbf4c8c | Cam Macdonell | ret = s->vm_id; |
226 | 6cbf4c8c | Cam Macdonell | } else {
|
227 | 6cbf4c8c | Cam Macdonell | ret = -1;
|
228 | 6cbf4c8c | Cam Macdonell | } |
229 | 6cbf4c8c | Cam Macdonell | break;
|
230 | 6cbf4c8c | Cam Macdonell | |
231 | 6cbf4c8c | Cam Macdonell | default:
|
232 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr); |
233 | 6cbf4c8c | Cam Macdonell | ret = 0;
|
234 | 6cbf4c8c | Cam Macdonell | } |
235 | 6cbf4c8c | Cam Macdonell | |
236 | 6cbf4c8c | Cam Macdonell | return ret;
|
237 | 6cbf4c8c | Cam Macdonell | } |
238 | 6cbf4c8c | Cam Macdonell | |
239 | cb06608e | Avi Kivity | static const MemoryRegionOps ivshmem_mmio_ops = { |
240 | cb06608e | Avi Kivity | .read = ivshmem_io_read, |
241 | cb06608e | Avi Kivity | .write = ivshmem_io_write, |
242 | cb06608e | Avi Kivity | .endianness = DEVICE_NATIVE_ENDIAN, |
243 | cb06608e | Avi Kivity | .impl = { |
244 | cb06608e | Avi Kivity | .min_access_size = 4,
|
245 | cb06608e | Avi Kivity | .max_access_size = 4,
|
246 | cb06608e | Avi Kivity | }, |
247 | 6cbf4c8c | Cam Macdonell | }; |
248 | 6cbf4c8c | Cam Macdonell | |
249 | 6cbf4c8c | Cam Macdonell | static void ivshmem_receive(void *opaque, const uint8_t *buf, int size) |
250 | 6cbf4c8c | Cam Macdonell | { |
251 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = opaque; |
252 | 6cbf4c8c | Cam Macdonell | |
253 | 6cbf4c8c | Cam Macdonell | ivshmem_IntrStatus_write(s, *buf); |
254 | 6cbf4c8c | Cam Macdonell | |
255 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
|
256 | 6cbf4c8c | Cam Macdonell | } |
257 | 6cbf4c8c | Cam Macdonell | |
258 | 6cbf4c8c | Cam Macdonell | static int ivshmem_can_receive(void * opaque) |
259 | 6cbf4c8c | Cam Macdonell | { |
260 | 6cbf4c8c | Cam Macdonell | return 8; |
261 | 6cbf4c8c | Cam Macdonell | } |
262 | 6cbf4c8c | Cam Macdonell | |
263 | 6cbf4c8c | Cam Macdonell | static void ivshmem_event(void *opaque, int event) |
264 | 6cbf4c8c | Cam Macdonell | { |
265 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
|
266 | 6cbf4c8c | Cam Macdonell | } |
267 | 6cbf4c8c | Cam Macdonell | |
268 | 6cbf4c8c | Cam Macdonell | static void fake_irqfd(void *opaque, const uint8_t *buf, int size) { |
269 | 6cbf4c8c | Cam Macdonell | |
270 | 6cbf4c8c | Cam Macdonell | EventfdEntry *entry = opaque; |
271 | 6cbf4c8c | Cam Macdonell | PCIDevice *pdev = entry->pdev; |
272 | 6cbf4c8c | Cam Macdonell | |
273 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
|
274 | 6cbf4c8c | Cam Macdonell | msix_notify(pdev, entry->vector); |
275 | 6cbf4c8c | Cam Macdonell | } |
276 | 6cbf4c8c | Cam Macdonell | |
277 | 563027cc | Paolo Bonzini | static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n, |
278 | 563027cc | Paolo Bonzini | int vector)
|
279 | 6cbf4c8c | Cam Macdonell | { |
280 | 6cbf4c8c | Cam Macdonell | /* create a event character device based on the passed eventfd */
|
281 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = opaque; |
282 | 6cbf4c8c | Cam Macdonell | CharDriverState * chr; |
283 | 563027cc | Paolo Bonzini | int eventfd = event_notifier_get_fd(n);
|
284 | 6cbf4c8c | Cam Macdonell | |
285 | 6cbf4c8c | Cam Macdonell | chr = qemu_chr_open_eventfd(eventfd); |
286 | 6cbf4c8c | Cam Macdonell | |
287 | 6cbf4c8c | Cam Macdonell | if (chr == NULL) { |
288 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
|
289 | 6cbf4c8c | Cam Macdonell | exit(-1);
|
290 | 6cbf4c8c | Cam Macdonell | } |
291 | 6cbf4c8c | Cam Macdonell | |
292 | 6cbf4c8c | Cam Macdonell | /* if MSI is supported we need multiple interrupts */
|
293 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
|
294 | 6cbf4c8c | Cam Macdonell | s->eventfd_table[vector].pdev = &s->dev; |
295 | 6cbf4c8c | Cam Macdonell | s->eventfd_table[vector].vector = vector; |
296 | 6cbf4c8c | Cam Macdonell | |
297 | 6cbf4c8c | Cam Macdonell | qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd, |
298 | 6cbf4c8c | Cam Macdonell | ivshmem_event, &s->eventfd_table[vector]); |
299 | 6cbf4c8c | Cam Macdonell | } else {
|
300 | 6cbf4c8c | Cam Macdonell | qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive, |
301 | 6cbf4c8c | Cam Macdonell | ivshmem_event, s); |
302 | 6cbf4c8c | Cam Macdonell | } |
303 | 6cbf4c8c | Cam Macdonell | |
304 | 6cbf4c8c | Cam Macdonell | return chr;
|
305 | 6cbf4c8c | Cam Macdonell | |
306 | 6cbf4c8c | Cam Macdonell | } |
307 | 6cbf4c8c | Cam Macdonell | |
308 | 6cbf4c8c | Cam Macdonell | static int check_shm_size(IVShmemState *s, int fd) { |
309 | 6cbf4c8c | Cam Macdonell | /* check that the guest isn't going to try and map more memory than the
|
310 | 6cbf4c8c | Cam Macdonell | * the object has allocated return -1 to indicate error */
|
311 | 6cbf4c8c | Cam Macdonell | |
312 | 6cbf4c8c | Cam Macdonell | struct stat buf;
|
313 | 6cbf4c8c | Cam Macdonell | |
314 | 6cbf4c8c | Cam Macdonell | fstat(fd, &buf); |
315 | 6cbf4c8c | Cam Macdonell | |
316 | 6cbf4c8c | Cam Macdonell | if (s->ivshmem_size > buf.st_size) {
|
317 | ad0a4ac1 | Avi Kivity | fprintf(stderr, |
318 | ad0a4ac1 | Avi Kivity | "IVSHMEM ERROR: Requested memory size greater"
|
319 | ad0a4ac1 | Avi Kivity | " than shared object size (%" PRIu64 " > %" PRIu64")\n", |
320 | ad0a4ac1 | Avi Kivity | s->ivshmem_size, (uint64_t)buf.st_size); |
321 | 6cbf4c8c | Cam Macdonell | return -1; |
322 | 6cbf4c8c | Cam Macdonell | } else {
|
323 | 6cbf4c8c | Cam Macdonell | return 0; |
324 | 6cbf4c8c | Cam Macdonell | } |
325 | 6cbf4c8c | Cam Macdonell | } |
326 | 6cbf4c8c | Cam Macdonell | |
327 | 6cbf4c8c | Cam Macdonell | /* create the shared memory BAR when we are not using the server, so we can
|
328 | 6cbf4c8c | Cam Macdonell | * create the BAR and map the memory immediately */
|
329 | 6cbf4c8c | Cam Macdonell | static void create_shared_memory_BAR(IVShmemState *s, int fd) { |
330 | 6cbf4c8c | Cam Macdonell | |
331 | 6cbf4c8c | Cam Macdonell | void * ptr;
|
332 | 6cbf4c8c | Cam Macdonell | |
333 | 6cbf4c8c | Cam Macdonell | s->shm_fd = fd; |
334 | 6cbf4c8c | Cam Macdonell | |
335 | 6cbf4c8c | Cam Macdonell | ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
336 | 6cbf4c8c | Cam Macdonell | |
337 | c5705a77 | Avi Kivity | memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
|
338 | cb06608e | Avi Kivity | s->ivshmem_size, ptr); |
339 | c5705a77 | Avi Kivity | vmstate_register_ram(&s->ivshmem, &s->dev.qdev); |
340 | cb06608e | Avi Kivity | memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
|
341 | 6cbf4c8c | Cam Macdonell | |
342 | 6cbf4c8c | Cam Macdonell | /* region for shared memory */
|
343 | c08ba66f | Gerd Hoffmann | pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
|
344 | 6cbf4c8c | Cam Macdonell | } |
345 | 6cbf4c8c | Cam Macdonell | |
346 | 563027cc | Paolo Bonzini | static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i) |
347 | 563027cc | Paolo Bonzini | { |
348 | 563027cc | Paolo Bonzini | memory_region_add_eventfd(&s->ivshmem_mmio, |
349 | 563027cc | Paolo Bonzini | DOORBELL, |
350 | 563027cc | Paolo Bonzini | 4,
|
351 | 563027cc | Paolo Bonzini | true,
|
352 | 563027cc | Paolo Bonzini | (posn << 16) | i,
|
353 | 753d5e14 | Paolo Bonzini | &s->peers[posn].eventfds[i]); |
354 | 563027cc | Paolo Bonzini | } |
355 | 563027cc | Paolo Bonzini | |
356 | 563027cc | Paolo Bonzini | static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i) |
357 | 563027cc | Paolo Bonzini | { |
358 | 563027cc | Paolo Bonzini | memory_region_del_eventfd(&s->ivshmem_mmio, |
359 | 563027cc | Paolo Bonzini | DOORBELL, |
360 | 563027cc | Paolo Bonzini | 4,
|
361 | 563027cc | Paolo Bonzini | true,
|
362 | 563027cc | Paolo Bonzini | (posn << 16) | i,
|
363 | 753d5e14 | Paolo Bonzini | &s->peers[posn].eventfds[i]); |
364 | 563027cc | Paolo Bonzini | } |
365 | 563027cc | Paolo Bonzini | |
366 | 6cbf4c8c | Cam Macdonell | static void close_guest_eventfds(IVShmemState *s, int posn) |
367 | 6cbf4c8c | Cam Macdonell | { |
368 | 6cbf4c8c | Cam Macdonell | int i, guest_curr_max;
|
369 | 6cbf4c8c | Cam Macdonell | |
370 | 98609cd8 | Paolo Bonzini | if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
|
371 | 98609cd8 | Paolo Bonzini | return;
|
372 | 98609cd8 | Paolo Bonzini | } |
373 | 98609cd8 | Paolo Bonzini | |
374 | 6cbf4c8c | Cam Macdonell | guest_curr_max = s->peers[posn].nb_eventfds; |
375 | 6cbf4c8c | Cam Macdonell | |
376 | b6a1f3a5 | Paolo Bonzini | memory_region_transaction_begin(); |
377 | 6cbf4c8c | Cam Macdonell | for (i = 0; i < guest_curr_max; i++) { |
378 | 563027cc | Paolo Bonzini | ivshmem_del_eventfd(s, posn, i); |
379 | b6a1f3a5 | Paolo Bonzini | } |
380 | b6a1f3a5 | Paolo Bonzini | memory_region_transaction_commit(); |
381 | b6a1f3a5 | Paolo Bonzini | for (i = 0; i < guest_curr_max; i++) { |
382 | 563027cc | Paolo Bonzini | event_notifier_cleanup(&s->peers[posn].eventfds[i]); |
383 | 6cbf4c8c | Cam Macdonell | } |
384 | 6cbf4c8c | Cam Macdonell | |
385 | 7267c094 | Anthony Liguori | g_free(s->peers[posn].eventfds); |
386 | 6cbf4c8c | Cam Macdonell | s->peers[posn].nb_eventfds = 0;
|
387 | 6cbf4c8c | Cam Macdonell | } |
388 | 6cbf4c8c | Cam Macdonell | |
389 | 6cbf4c8c | Cam Macdonell | /* this function increase the dynamic storage need to store data about other
|
390 | 6cbf4c8c | Cam Macdonell | * guests */
|
391 | 6cbf4c8c | Cam Macdonell | static void increase_dynamic_storage(IVShmemState *s, int new_min_size) { |
392 | 6cbf4c8c | Cam Macdonell | |
393 | 6cbf4c8c | Cam Macdonell | int j, old_nb_alloc;
|
394 | 6cbf4c8c | Cam Macdonell | |
395 | 6cbf4c8c | Cam Macdonell | old_nb_alloc = s->nb_peers; |
396 | 6cbf4c8c | Cam Macdonell | |
397 | 6cbf4c8c | Cam Macdonell | while (new_min_size >= s->nb_peers)
|
398 | 6cbf4c8c | Cam Macdonell | s->nb_peers = s->nb_peers * 2;
|
399 | 6cbf4c8c | Cam Macdonell | |
400 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
|
401 | 7267c094 | Anthony Liguori | s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
|
402 | 6cbf4c8c | Cam Macdonell | |
403 | 6cbf4c8c | Cam Macdonell | /* zero out new pointers */
|
404 | 6cbf4c8c | Cam Macdonell | for (j = old_nb_alloc; j < s->nb_peers; j++) {
|
405 | 6cbf4c8c | Cam Macdonell | s->peers[j].eventfds = NULL;
|
406 | 6cbf4c8c | Cam Macdonell | s->peers[j].nb_eventfds = 0;
|
407 | 6cbf4c8c | Cam Macdonell | } |
408 | 6cbf4c8c | Cam Macdonell | } |
409 | 6cbf4c8c | Cam Macdonell | |
410 | 6cbf4c8c | Cam Macdonell | static void ivshmem_read(void *opaque, const uint8_t * buf, int flags) |
411 | 6cbf4c8c | Cam Macdonell | { |
412 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = opaque; |
413 | 6cbf4c8c | Cam Macdonell | int incoming_fd, tmp_fd;
|
414 | 6cbf4c8c | Cam Macdonell | int guest_max_eventfd;
|
415 | 6cbf4c8c | Cam Macdonell | long incoming_posn;
|
416 | 6cbf4c8c | Cam Macdonell | |
417 | 6cbf4c8c | Cam Macdonell | memcpy(&incoming_posn, buf, sizeof(long)); |
418 | 6cbf4c8c | Cam Macdonell | /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
|
419 | 74c0d6f0 | Anthony Liguori | tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr); |
420 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
|
421 | 6cbf4c8c | Cam Macdonell | |
422 | 6cbf4c8c | Cam Macdonell | /* make sure we have enough space for this guest */
|
423 | 6cbf4c8c | Cam Macdonell | if (incoming_posn >= s->nb_peers) {
|
424 | 6cbf4c8c | Cam Macdonell | increase_dynamic_storage(s, incoming_posn); |
425 | 6cbf4c8c | Cam Macdonell | } |
426 | 6cbf4c8c | Cam Macdonell | |
427 | 6cbf4c8c | Cam Macdonell | if (tmp_fd == -1) { |
428 | 6cbf4c8c | Cam Macdonell | /* if posn is positive and unseen before then this is our posn*/
|
429 | 6cbf4c8c | Cam Macdonell | if ((incoming_posn >= 0) && |
430 | 6cbf4c8c | Cam Macdonell | (s->peers[incoming_posn].eventfds == NULL)) {
|
431 | 6cbf4c8c | Cam Macdonell | /* receive our posn */
|
432 | 6cbf4c8c | Cam Macdonell | s->vm_id = incoming_posn; |
433 | 6cbf4c8c | Cam Macdonell | return;
|
434 | 6cbf4c8c | Cam Macdonell | } else {
|
435 | 6cbf4c8c | Cam Macdonell | /* otherwise an fd == -1 means an existing guest has gone away */
|
436 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
|
437 | 6cbf4c8c | Cam Macdonell | close_guest_eventfds(s, incoming_posn); |
438 | 6cbf4c8c | Cam Macdonell | return;
|
439 | 6cbf4c8c | Cam Macdonell | } |
440 | 6cbf4c8c | Cam Macdonell | } |
441 | 6cbf4c8c | Cam Macdonell | |
442 | 6cbf4c8c | Cam Macdonell | /* because of the implementation of get_msgfd, we need a dup */
|
443 | 6cbf4c8c | Cam Macdonell | incoming_fd = dup(tmp_fd); |
444 | 6cbf4c8c | Cam Macdonell | |
445 | 6cbf4c8c | Cam Macdonell | if (incoming_fd == -1) { |
446 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "could not allocate file descriptor %s\n",
|
447 | 6cbf4c8c | Cam Macdonell | strerror(errno)); |
448 | 6cbf4c8c | Cam Macdonell | return;
|
449 | 6cbf4c8c | Cam Macdonell | } |
450 | 6cbf4c8c | Cam Macdonell | |
451 | 6cbf4c8c | Cam Macdonell | /* if the position is -1, then it's shared memory region fd */
|
452 | 6cbf4c8c | Cam Macdonell | if (incoming_posn == -1) { |
453 | 6cbf4c8c | Cam Macdonell | |
454 | 6cbf4c8c | Cam Macdonell | void * map_ptr;
|
455 | 6cbf4c8c | Cam Macdonell | |
456 | 6cbf4c8c | Cam Macdonell | s->max_peer = 0;
|
457 | 6cbf4c8c | Cam Macdonell | |
458 | 6cbf4c8c | Cam Macdonell | if (check_shm_size(s, incoming_fd) == -1) { |
459 | 6cbf4c8c | Cam Macdonell | exit(-1);
|
460 | 6cbf4c8c | Cam Macdonell | } |
461 | 6cbf4c8c | Cam Macdonell | |
462 | 6cbf4c8c | Cam Macdonell | /* mmap the region and map into the BAR2 */
|
463 | 6cbf4c8c | Cam Macdonell | map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
|
464 | 6cbf4c8c | Cam Macdonell | incoming_fd, 0);
|
465 | c5705a77 | Avi Kivity | memory_region_init_ram_ptr(&s->ivshmem, |
466 | cb06608e | Avi Kivity | "ivshmem.bar2", s->ivshmem_size, map_ptr);
|
467 | c5705a77 | Avi Kivity | vmstate_register_ram(&s->ivshmem, &s->dev.qdev); |
468 | 6cbf4c8c | Cam Macdonell | |
469 | cb06608e | Avi Kivity | IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n", |
470 | 6cbf4c8c | Cam Macdonell | s->ivshmem_offset, s->ivshmem_size); |
471 | 6cbf4c8c | Cam Macdonell | |
472 | cb06608e | Avi Kivity | memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
|
473 | 6cbf4c8c | Cam Macdonell | |
474 | 6cbf4c8c | Cam Macdonell | /* only store the fd if it is successfully mapped */
|
475 | 6cbf4c8c | Cam Macdonell | s->shm_fd = incoming_fd; |
476 | 6cbf4c8c | Cam Macdonell | |
477 | 6cbf4c8c | Cam Macdonell | return;
|
478 | 6cbf4c8c | Cam Macdonell | } |
479 | 6cbf4c8c | Cam Macdonell | |
480 | 6cbf4c8c | Cam Macdonell | /* each guest has an array of eventfds, and we keep track of how many
|
481 | 6cbf4c8c | Cam Macdonell | * guests for each VM */
|
482 | 6cbf4c8c | Cam Macdonell | guest_max_eventfd = s->peers[incoming_posn].nb_eventfds; |
483 | 6cbf4c8c | Cam Macdonell | |
484 | 6cbf4c8c | Cam Macdonell | if (guest_max_eventfd == 0) { |
485 | 6cbf4c8c | Cam Macdonell | /* one eventfd per MSI vector */
|
486 | 563027cc | Paolo Bonzini | s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors); |
487 | 6cbf4c8c | Cam Macdonell | } |
488 | 6cbf4c8c | Cam Macdonell | |
489 | 6cbf4c8c | Cam Macdonell | /* this is an eventfd for a particular guest VM */
|
490 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
|
491 | 6cbf4c8c | Cam Macdonell | guest_max_eventfd, incoming_fd); |
492 | 563027cc | Paolo Bonzini | event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd], |
493 | 563027cc | Paolo Bonzini | incoming_fd); |
494 | 6cbf4c8c | Cam Macdonell | |
495 | 6cbf4c8c | Cam Macdonell | /* increment count for particular guest */
|
496 | 6cbf4c8c | Cam Macdonell | s->peers[incoming_posn].nb_eventfds++; |
497 | 6cbf4c8c | Cam Macdonell | |
498 | 6cbf4c8c | Cam Macdonell | /* keep track of the maximum VM ID */
|
499 | 6cbf4c8c | Cam Macdonell | if (incoming_posn > s->max_peer) {
|
500 | 6cbf4c8c | Cam Macdonell | s->max_peer = incoming_posn; |
501 | 6cbf4c8c | Cam Macdonell | } |
502 | 6cbf4c8c | Cam Macdonell | |
503 | 6cbf4c8c | Cam Macdonell | if (incoming_posn == s->vm_id) {
|
504 | 6cbf4c8c | Cam Macdonell | s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s, |
505 | 563027cc | Paolo Bonzini | &s->peers[s->vm_id].eventfds[guest_max_eventfd], |
506 | 6cbf4c8c | Cam Macdonell | guest_max_eventfd); |
507 | 6cbf4c8c | Cam Macdonell | } |
508 | 6cbf4c8c | Cam Macdonell | |
509 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
|
510 | 563027cc | Paolo Bonzini | ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd); |
511 | 6cbf4c8c | Cam Macdonell | } |
512 | 6cbf4c8c | Cam Macdonell | } |
513 | 6cbf4c8c | Cam Macdonell | |
514 | 4490c711 | Michael S. Tsirkin | /* Select the MSI-X vectors used by device.
|
515 | 4490c711 | Michael S. Tsirkin | * ivshmem maps events to vectors statically, so
|
516 | 4490c711 | Michael S. Tsirkin | * we just enable all vectors on init and after reset. */
|
517 | 4490c711 | Michael S. Tsirkin | static void ivshmem_use_msix(IVShmemState * s) |
518 | 4490c711 | Michael S. Tsirkin | { |
519 | 4490c711 | Michael S. Tsirkin | int i;
|
520 | 4490c711 | Michael S. Tsirkin | |
521 | 4490c711 | Michael S. Tsirkin | if (!msix_present(&s->dev)) {
|
522 | 4490c711 | Michael S. Tsirkin | return;
|
523 | 4490c711 | Michael S. Tsirkin | } |
524 | 4490c711 | Michael S. Tsirkin | |
525 | 4490c711 | Michael S. Tsirkin | for (i = 0; i < s->vectors; i++) { |
526 | 4490c711 | Michael S. Tsirkin | msix_vector_use(&s->dev, i); |
527 | 4490c711 | Michael S. Tsirkin | } |
528 | 4490c711 | Michael S. Tsirkin | } |
529 | 4490c711 | Michael S. Tsirkin | |
530 | 6cbf4c8c | Cam Macdonell | static void ivshmem_reset(DeviceState *d) |
531 | 6cbf4c8c | Cam Macdonell | { |
532 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d); |
533 | 6cbf4c8c | Cam Macdonell | |
534 | 6cbf4c8c | Cam Macdonell | s->intrstatus = 0;
|
535 | 4490c711 | Michael S. Tsirkin | ivshmem_use_msix(s); |
536 | 6cbf4c8c | Cam Macdonell | } |
537 | 6cbf4c8c | Cam Macdonell | |
538 | 6cbf4c8c | Cam Macdonell | static uint64_t ivshmem_get_size(IVShmemState * s) {
|
539 | 6cbf4c8c | Cam Macdonell | |
540 | 6cbf4c8c | Cam Macdonell | uint64_t value; |
541 | 6cbf4c8c | Cam Macdonell | char *ptr;
|
542 | 6cbf4c8c | Cam Macdonell | |
543 | 6cbf4c8c | Cam Macdonell | value = strtoull(s->sizearg, &ptr, 10);
|
544 | 6cbf4c8c | Cam Macdonell | switch (*ptr) {
|
545 | 6cbf4c8c | Cam Macdonell | case 0: case 'M': case 'm': |
546 | 6cbf4c8c | Cam Macdonell | value <<= 20;
|
547 | 6cbf4c8c | Cam Macdonell | break;
|
548 | 6cbf4c8c | Cam Macdonell | case 'G': case 'g': |
549 | 6cbf4c8c | Cam Macdonell | value <<= 30;
|
550 | 6cbf4c8c | Cam Macdonell | break;
|
551 | 6cbf4c8c | Cam Macdonell | default:
|
552 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
|
553 | 6cbf4c8c | Cam Macdonell | exit(1);
|
554 | 6cbf4c8c | Cam Macdonell | } |
555 | 6cbf4c8c | Cam Macdonell | |
556 | 6cbf4c8c | Cam Macdonell | /* BARs must be a power of 2 */
|
557 | 6cbf4c8c | Cam Macdonell | if (!is_power_of_two(value)) {
|
558 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: size must be power of 2\n");
|
559 | 6cbf4c8c | Cam Macdonell | exit(1);
|
560 | 6cbf4c8c | Cam Macdonell | } |
561 | 6cbf4c8c | Cam Macdonell | |
562 | 6cbf4c8c | Cam Macdonell | return value;
|
563 | 6cbf4c8c | Cam Macdonell | } |
564 | 6cbf4c8c | Cam Macdonell | |
565 | 4490c711 | Michael S. Tsirkin | static void ivshmem_setup_msi(IVShmemState * s) |
566 | 4490c711 | Michael S. Tsirkin | { |
567 | 1116b539 | Alex Williamson | if (msix_init_exclusive_bar(&s->dev, s->vectors, 1)) { |
568 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("msix initialization failed\n");
|
569 | 6cbf4c8c | Cam Macdonell | exit(1);
|
570 | 6cbf4c8c | Cam Macdonell | } |
571 | 6cbf4c8c | Cam Macdonell | |
572 | 1116b539 | Alex Williamson | IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
|
573 | 1116b539 | Alex Williamson | |
574 | 5cbdb3a3 | Stefan Weil | /* allocate QEMU char devices for receiving interrupts */
|
575 | 7267c094 | Anthony Liguori | s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
|
576 | 4490c711 | Michael S. Tsirkin | |
577 | 4490c711 | Michael S. Tsirkin | ivshmem_use_msix(s); |
578 | 6cbf4c8c | Cam Macdonell | } |
579 | 6cbf4c8c | Cam Macdonell | |
580 | 6cbf4c8c | Cam Macdonell | static void ivshmem_save(QEMUFile* f, void *opaque) |
581 | 6cbf4c8c | Cam Macdonell | { |
582 | 6cbf4c8c | Cam Macdonell | IVShmemState *proxy = opaque; |
583 | 6cbf4c8c | Cam Macdonell | |
584 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("ivshmem_save\n");
|
585 | 6cbf4c8c | Cam Macdonell | pci_device_save(&proxy->dev, f); |
586 | 6cbf4c8c | Cam Macdonell | |
587 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
|
588 | 6cbf4c8c | Cam Macdonell | msix_save(&proxy->dev, f); |
589 | 6cbf4c8c | Cam Macdonell | } else {
|
590 | 6cbf4c8c | Cam Macdonell | qemu_put_be32(f, proxy->intrstatus); |
591 | 6cbf4c8c | Cam Macdonell | qemu_put_be32(f, proxy->intrmask); |
592 | 6cbf4c8c | Cam Macdonell | } |
593 | 6cbf4c8c | Cam Macdonell | |
594 | 6cbf4c8c | Cam Macdonell | } |
595 | 6cbf4c8c | Cam Macdonell | |
596 | 6cbf4c8c | Cam Macdonell | static int ivshmem_load(QEMUFile* f, void *opaque, int version_id) |
597 | 6cbf4c8c | Cam Macdonell | { |
598 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("ivshmem_load\n");
|
599 | 6cbf4c8c | Cam Macdonell | |
600 | 6cbf4c8c | Cam Macdonell | IVShmemState *proxy = opaque; |
601 | 4490c711 | Michael S. Tsirkin | int ret;
|
602 | 6cbf4c8c | Cam Macdonell | |
603 | 6cbf4c8c | Cam Macdonell | if (version_id > 0) { |
604 | 6cbf4c8c | Cam Macdonell | return -EINVAL;
|
605 | 6cbf4c8c | Cam Macdonell | } |
606 | 6cbf4c8c | Cam Macdonell | |
607 | 6cbf4c8c | Cam Macdonell | if (proxy->role_val == IVSHMEM_PEER) {
|
608 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
|
609 | 6cbf4c8c | Cam Macdonell | return -EINVAL;
|
610 | 6cbf4c8c | Cam Macdonell | } |
611 | 6cbf4c8c | Cam Macdonell | |
612 | 6cbf4c8c | Cam Macdonell | ret = pci_device_load(&proxy->dev, f); |
613 | 6cbf4c8c | Cam Macdonell | if (ret) {
|
614 | 6cbf4c8c | Cam Macdonell | return ret;
|
615 | 6cbf4c8c | Cam Macdonell | } |
616 | 6cbf4c8c | Cam Macdonell | |
617 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
|
618 | 6cbf4c8c | Cam Macdonell | msix_load(&proxy->dev, f); |
619 | 4490c711 | Michael S. Tsirkin | ivshmem_use_msix(proxy); |
620 | 6cbf4c8c | Cam Macdonell | } else {
|
621 | 6cbf4c8c | Cam Macdonell | proxy->intrstatus = qemu_get_be32(f); |
622 | 6cbf4c8c | Cam Macdonell | proxy->intrmask = qemu_get_be32(f); |
623 | 6cbf4c8c | Cam Macdonell | } |
624 | 6cbf4c8c | Cam Macdonell | |
625 | 6cbf4c8c | Cam Macdonell | return 0; |
626 | 6cbf4c8c | Cam Macdonell | } |
627 | 6cbf4c8c | Cam Macdonell | |
628 | 4490c711 | Michael S. Tsirkin | static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address, |
629 | 4490c711 | Michael S. Tsirkin | uint32_t val, int len)
|
630 | 4490c711 | Michael S. Tsirkin | { |
631 | 4490c711 | Michael S. Tsirkin | pci_default_write_config(pci_dev, address, val, len); |
632 | 4490c711 | Michael S. Tsirkin | msix_write_config(pci_dev, address, val, len); |
633 | 4490c711 | Michael S. Tsirkin | } |
634 | 4490c711 | Michael S. Tsirkin | |
635 | 6cbf4c8c | Cam Macdonell | static int pci_ivshmem_init(PCIDevice *dev) |
636 | 6cbf4c8c | Cam Macdonell | { |
637 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev); |
638 | 6cbf4c8c | Cam Macdonell | uint8_t *pci_conf; |
639 | 6cbf4c8c | Cam Macdonell | |
640 | 6cbf4c8c | Cam Macdonell | if (s->sizearg == NULL) |
641 | 6cbf4c8c | Cam Macdonell | s->ivshmem_size = 4 << 20; /* 4 MB default */ |
642 | 6cbf4c8c | Cam Macdonell | else {
|
643 | 6cbf4c8c | Cam Macdonell | s->ivshmem_size = ivshmem_get_size(s); |
644 | 6cbf4c8c | Cam Macdonell | } |
645 | 6cbf4c8c | Cam Macdonell | |
646 | 6cbf4c8c | Cam Macdonell | register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load, |
647 | 6cbf4c8c | Cam Macdonell | dev); |
648 | 6cbf4c8c | Cam Macdonell | |
649 | 6cbf4c8c | Cam Macdonell | /* IRQFD requires MSI */
|
650 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
|
651 | 6cbf4c8c | Cam Macdonell | !ivshmem_has_feature(s, IVSHMEM_MSI)) { |
652 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
|
653 | 6cbf4c8c | Cam Macdonell | exit(1);
|
654 | 6cbf4c8c | Cam Macdonell | } |
655 | 6cbf4c8c | Cam Macdonell | |
656 | 6cbf4c8c | Cam Macdonell | /* check that role is reasonable */
|
657 | 6cbf4c8c | Cam Macdonell | if (s->role) {
|
658 | 6cbf4c8c | Cam Macdonell | if (strncmp(s->role, "peer", 5) == 0) { |
659 | 6cbf4c8c | Cam Macdonell | s->role_val = IVSHMEM_PEER; |
660 | 6cbf4c8c | Cam Macdonell | } else if (strncmp(s->role, "master", 7) == 0) { |
661 | 6cbf4c8c | Cam Macdonell | s->role_val = IVSHMEM_MASTER; |
662 | 6cbf4c8c | Cam Macdonell | } else {
|
663 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
|
664 | 6cbf4c8c | Cam Macdonell | exit(1);
|
665 | 6cbf4c8c | Cam Macdonell | } |
666 | 6cbf4c8c | Cam Macdonell | } else {
|
667 | 6cbf4c8c | Cam Macdonell | s->role_val = IVSHMEM_MASTER; /* default */
|
668 | 6cbf4c8c | Cam Macdonell | } |
669 | 6cbf4c8c | Cam Macdonell | |
670 | 6cbf4c8c | Cam Macdonell | if (s->role_val == IVSHMEM_PEER) {
|
671 | c3594ed7 | Alberto Garcia | error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION, |
672 | c3594ed7 | Alberto Garcia | "peer mode", "ivshmem"); |
673 | 38e0735e | Anthony Liguori | migrate_add_blocker(s->migration_blocker); |
674 | 6cbf4c8c | Cam Macdonell | } |
675 | 6cbf4c8c | Cam Macdonell | |
676 | 6cbf4c8c | Cam Macdonell | pci_conf = s->dev.config; |
677 | 6cbf4c8c | Cam Macdonell | pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY; |
678 | 6cbf4c8c | Cam Macdonell | |
679 | 6cbf4c8c | Cam Macdonell | pci_config_set_interrupt_pin(pci_conf, 1);
|
680 | 6cbf4c8c | Cam Macdonell | |
681 | 6cbf4c8c | Cam Macdonell | s->shm_fd = 0;
|
682 | 6cbf4c8c | Cam Macdonell | |
683 | cb06608e | Avi Kivity | memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s, |
684 | cb06608e | Avi Kivity | "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
|
685 | cb06608e | Avi Kivity | |
686 | 6cbf4c8c | Cam Macdonell | /* region for registers*/
|
687 | e824b2cc | Avi Kivity | pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
|
688 | e824b2cc | Avi Kivity | &s->ivshmem_mmio); |
689 | cb06608e | Avi Kivity | |
690 | cb06608e | Avi Kivity | memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
|
691 | c08ba66f | Gerd Hoffmann | s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY | |
692 | c08ba66f | Gerd Hoffmann | PCI_BASE_ADDRESS_MEM_PREFETCH; |
693 | c08ba66f | Gerd Hoffmann | if (s->ivshmem_64bit) {
|
694 | c08ba66f | Gerd Hoffmann | s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64; |
695 | c08ba66f | Gerd Hoffmann | } |
696 | 6cbf4c8c | Cam Macdonell | |
697 | 6cbf4c8c | Cam Macdonell | if ((s->server_chr != NULL) && |
698 | 6cbf4c8c | Cam Macdonell | (strncmp(s->server_chr->filename, "unix:", 5) == 0)) { |
699 | 6cbf4c8c | Cam Macdonell | /* if we get a UNIX socket as the parameter we will talk
|
700 | 6cbf4c8c | Cam Macdonell | * to the ivshmem server to receive the memory region */
|
701 | 6cbf4c8c | Cam Macdonell | |
702 | 6cbf4c8c | Cam Macdonell | if (s->shmobj != NULL) { |
703 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "WARNING: do not specify both 'chardev' "
|
704 | 6cbf4c8c | Cam Macdonell | "and 'shm' with ivshmem\n");
|
705 | 6cbf4c8c | Cam Macdonell | } |
706 | 6cbf4c8c | Cam Macdonell | |
707 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
|
708 | 6cbf4c8c | Cam Macdonell | s->server_chr->filename); |
709 | 6cbf4c8c | Cam Macdonell | |
710 | 6cbf4c8c | Cam Macdonell | if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
|
711 | 6cbf4c8c | Cam Macdonell | ivshmem_setup_msi(s); |
712 | 6cbf4c8c | Cam Macdonell | } |
713 | 6cbf4c8c | Cam Macdonell | |
714 | 6cbf4c8c | Cam Macdonell | /* we allocate enough space for 16 guests and grow as needed */
|
715 | 6cbf4c8c | Cam Macdonell | s->nb_peers = 16;
|
716 | 6cbf4c8c | Cam Macdonell | s->vm_id = -1;
|
717 | 6cbf4c8c | Cam Macdonell | |
718 | 6cbf4c8c | Cam Macdonell | /* allocate/initialize space for interrupt handling */
|
719 | 7267c094 | Anthony Liguori | s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
|
720 | 6cbf4c8c | Cam Macdonell | |
721 | c08ba66f | Gerd Hoffmann | pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
|
722 | 6cbf4c8c | Cam Macdonell | |
723 | 7267c094 | Anthony Liguori | s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
|
724 | 6cbf4c8c | Cam Macdonell | |
725 | 6cbf4c8c | Cam Macdonell | qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read, |
726 | 6cbf4c8c | Cam Macdonell | ivshmem_event, s); |
727 | 6cbf4c8c | Cam Macdonell | } else {
|
728 | 6cbf4c8c | Cam Macdonell | /* just map the file immediately, we're not using a server */
|
729 | 6cbf4c8c | Cam Macdonell | int fd;
|
730 | 6cbf4c8c | Cam Macdonell | |
731 | 6cbf4c8c | Cam Macdonell | if (s->shmobj == NULL) { |
732 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
|
733 | 6cbf4c8c | Cam Macdonell | } |
734 | 6cbf4c8c | Cam Macdonell | |
735 | 6cbf4c8c | Cam Macdonell | IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
|
736 | 6cbf4c8c | Cam Macdonell | |
737 | 6cbf4c8c | Cam Macdonell | /* try opening with O_EXCL and if it succeeds zero the memory
|
738 | 6cbf4c8c | Cam Macdonell | * by truncating to 0 */
|
739 | 6cbf4c8c | Cam Macdonell | if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
|
740 | 6cbf4c8c | Cam Macdonell | S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
|
741 | 6cbf4c8c | Cam Macdonell | /* truncate file to length PCI device's memory */
|
742 | 6cbf4c8c | Cam Macdonell | if (ftruncate(fd, s->ivshmem_size) != 0) { |
743 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: could not truncate shared file\n");
|
744 | 6cbf4c8c | Cam Macdonell | } |
745 | 6cbf4c8c | Cam Macdonell | |
746 | 6cbf4c8c | Cam Macdonell | } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR, |
747 | 6cbf4c8c | Cam Macdonell | S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
|
748 | 6cbf4c8c | Cam Macdonell | fprintf(stderr, "ivshmem: could not open shared file\n");
|
749 | 6cbf4c8c | Cam Macdonell | exit(-1);
|
750 | 6cbf4c8c | Cam Macdonell | |
751 | 6cbf4c8c | Cam Macdonell | } |
752 | 6cbf4c8c | Cam Macdonell | |
753 | 6cbf4c8c | Cam Macdonell | if (check_shm_size(s, fd) == -1) { |
754 | 6cbf4c8c | Cam Macdonell | exit(-1);
|
755 | 6cbf4c8c | Cam Macdonell | } |
756 | 6cbf4c8c | Cam Macdonell | |
757 | 6cbf4c8c | Cam Macdonell | create_shared_memory_BAR(s, fd); |
758 | 6cbf4c8c | Cam Macdonell | |
759 | 6cbf4c8c | Cam Macdonell | } |
760 | 6cbf4c8c | Cam Macdonell | |
761 | 4490c711 | Michael S. Tsirkin | s->dev.config_write = ivshmem_write_config; |
762 | 4490c711 | Michael S. Tsirkin | |
763 | 6cbf4c8c | Cam Macdonell | return 0; |
764 | 6cbf4c8c | Cam Macdonell | } |
765 | 6cbf4c8c | Cam Macdonell | |
766 | f90c2bcd | Alex Williamson | static void pci_ivshmem_uninit(PCIDevice *dev) |
767 | 6cbf4c8c | Cam Macdonell | { |
768 | 6cbf4c8c | Cam Macdonell | IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev); |
769 | 6cbf4c8c | Cam Macdonell | |
770 | 38e0735e | Anthony Liguori | if (s->migration_blocker) {
|
771 | 38e0735e | Anthony Liguori | migrate_del_blocker(s->migration_blocker); |
772 | 38e0735e | Anthony Liguori | error_free(s->migration_blocker); |
773 | 38e0735e | Anthony Liguori | } |
774 | 38e0735e | Anthony Liguori | |
775 | cb06608e | Avi Kivity | memory_region_destroy(&s->ivshmem_mmio); |
776 | cb06608e | Avi Kivity | memory_region_del_subregion(&s->bar, &s->ivshmem); |
777 | c5705a77 | Avi Kivity | vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev); |
778 | cb06608e | Avi Kivity | memory_region_destroy(&s->ivshmem); |
779 | cb06608e | Avi Kivity | memory_region_destroy(&s->bar); |
780 | 6cbf4c8c | Cam Macdonell | unregister_savevm(&dev->qdev, "ivshmem", s);
|
781 | 6cbf4c8c | Cam Macdonell | } |
782 | 6cbf4c8c | Cam Macdonell | |
783 | 40021f08 | Anthony Liguori | static Property ivshmem_properties[] = {
|
784 | 40021f08 | Anthony Liguori | DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
|
785 | 40021f08 | Anthony Liguori | DEFINE_PROP_STRING("size", IVShmemState, sizearg),
|
786 | 40021f08 | Anthony Liguori | DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1), |
787 | 40021f08 | Anthony Liguori | DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false), |
788 | 40021f08 | Anthony Liguori | DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true), |
789 | 40021f08 | Anthony Liguori | DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
|
790 | 40021f08 | Anthony Liguori | DEFINE_PROP_STRING("role", IVShmemState, role),
|
791 | c08ba66f | Gerd Hoffmann | DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1), |
792 | 40021f08 | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
793 | 40021f08 | Anthony Liguori | }; |
794 | 40021f08 | Anthony Liguori | |
795 | 40021f08 | Anthony Liguori | static void ivshmem_class_init(ObjectClass *klass, void *data) |
796 | 40021f08 | Anthony Liguori | { |
797 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
798 | 40021f08 | Anthony Liguori | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
799 | 40021f08 | Anthony Liguori | |
800 | 40021f08 | Anthony Liguori | k->init = pci_ivshmem_init; |
801 | 40021f08 | Anthony Liguori | k->exit = pci_ivshmem_uninit; |
802 | 40021f08 | Anthony Liguori | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
803 | 40021f08 | Anthony Liguori | k->device_id = 0x1110;
|
804 | 40021f08 | Anthony Liguori | k->class_id = PCI_CLASS_MEMORY_RAM; |
805 | 39bffca2 | Anthony Liguori | dc->reset = ivshmem_reset; |
806 | 39bffca2 | Anthony Liguori | dc->props = ivshmem_properties; |
807 | 40021f08 | Anthony Liguori | } |
808 | 40021f08 | Anthony Liguori | |
809 | 39bffca2 | Anthony Liguori | static TypeInfo ivshmem_info = {
|
810 | 39bffca2 | Anthony Liguori | .name = "ivshmem",
|
811 | 39bffca2 | Anthony Liguori | .parent = TYPE_PCI_DEVICE, |
812 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(IVShmemState),
|
813 | 39bffca2 | Anthony Liguori | .class_init = ivshmem_class_init, |
814 | 6cbf4c8c | Cam Macdonell | }; |
815 | 6cbf4c8c | Cam Macdonell | |
816 | 83f7d43a | Andreas Färber | static void ivshmem_register_types(void) |
817 | 6cbf4c8c | Cam Macdonell | { |
818 | 39bffca2 | Anthony Liguori | type_register_static(&ivshmem_info); |
819 | 6cbf4c8c | Cam Macdonell | } |
820 | 6cbf4c8c | Cam Macdonell | |
821 | 83f7d43a | Andreas Färber | type_init(ivshmem_register_types) |