]>
Commit | Line | Data |
---|---|---|
c3ebd3ba JK |
1 | /* |
2 | * Copyright (c) 2007, Neocleus Corporation. | |
3 | * | |
4 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
5 | * the COPYING file in the top-level directory. | |
6 | * | |
7 | * | |
8 | * Assign a PCI device from the host to a guest VM. | |
9 | * | |
10 | * This implementation uses the classic device assignment interface of KVM | |
11 | * and is only available on x86 hosts. It is expected to be obsoleted by VFIO | |
12 | * based device assignment. | |
13 | * | |
14 | * Adapted for KVM (qemu-kvm) by Qumranet. QEMU version was based on qemu-kvm | |
15 | * revision 4144fe9d48. See its repository for the history. | |
16 | * | |
17 | * Copyright (c) 2007, Neocleus, Alex Novik ([email protected]) | |
18 | * Copyright (c) 2007, Neocleus, Guy Zana ([email protected]) | |
19 | * Copyright (C) 2008, Qumranet, Amit Shah ([email protected]) | |
20 | * Copyright (C) 2008, Red Hat, Amit Shah ([email protected]) | |
21 | * Copyright (C) 2008, IBM, Muli Ben-Yehuda ([email protected]) | |
22 | */ | |
a9c94277 | 23 | |
b6a0aa05 | 24 | #include "qemu/osdep.h" |
a9c94277 | 25 | #include <linux/kvm.h> |
da34e65c | 26 | #include "qapi/error.h" |
c3ebd3ba | 27 | #include "hw/hw.h" |
0d09e41a | 28 | #include "hw/i386/pc.h" |
1de7afc9 | 29 | #include "qemu/error-report.h" |
28ecbaee | 30 | #include "ui/console.h" |
c3ebd3ba | 31 | #include "hw/loader.h" |
83c9089e | 32 | #include "monitor/monitor.h" |
1de7afc9 | 33 | #include "qemu/range.h" |
9c17d615 | 34 | #include "sysemu/sysemu.h" |
a2cb15b0 MT |
35 | #include "hw/pci/pci.h" |
36 | #include "hw/pci/msi.h" | |
c3ebd3ba | 37 | #include "kvm_i386.h" |
bcd7461e | 38 | #include "hw/pci/pci-assign.h" |
c3ebd3ba | 39 | |
c3ebd3ba JK |
40 | /* From linux/ioport.h */ |
41 | #define IORESOURCE_IO 0x00000100 /* Resource type */ | |
42 | #define IORESOURCE_MEM 0x00000200 | |
43 | #define IORESOURCE_IRQ 0x00000400 | |
44 | #define IORESOURCE_DMA 0x00000800 | |
45 | #define IORESOURCE_PREFETCH 0x00002000 /* No side effects */ | |
0a2a59d3 | 46 | #define IORESOURCE_MEM_64 0x00100000 |
c3ebd3ba | 47 | |
c3ebd3ba JK |
48 | typedef struct PCIRegion { |
49 | int type; /* Memory or port I/O */ | |
50 | int valid; | |
51 | uint64_t base_addr; | |
52 | uint64_t size; /* size of the region */ | |
53 | int resource_fd; | |
54 | } PCIRegion; | |
55 | ||
56 | typedef struct PCIDevRegions { | |
57 | uint8_t bus, dev, func; /* Bus inside domain, device and function */ | |
58 | int irq; /* IRQ number */ | |
59 | uint16_t region_number; /* number of active regions */ | |
60 | ||
61 | /* Port I/O or MMIO Regions */ | |
62 | PCIRegion regions[PCI_NUM_REGIONS - 1]; | |
63 | int config_fd; | |
64 | } PCIDevRegions; | |
65 | ||
66 | typedef struct AssignedDevRegion { | |
67 | MemoryRegion container; | |
68 | MemoryRegion real_iomem; | |
69 | union { | |
70 | uint8_t *r_virtbase; /* mmapped access address for memory regions */ | |
71 | uint32_t r_baseport; /* the base guest port for I/O regions */ | |
72 | } u; | |
73 | pcibus_t e_size; /* emulated size of region in bytes */ | |
74 | pcibus_t r_size; /* real size of region in bytes */ | |
75 | PCIRegion *region; | |
76 | } AssignedDevRegion; | |
77 | ||
78 | #define ASSIGNED_DEVICE_PREFER_MSI_BIT 0 | |
79 | #define ASSIGNED_DEVICE_SHARE_INTX_BIT 1 | |
80 | ||
81 | #define ASSIGNED_DEVICE_PREFER_MSI_MASK (1 << ASSIGNED_DEVICE_PREFER_MSI_BIT) | |
82 | #define ASSIGNED_DEVICE_SHARE_INTX_MASK (1 << ASSIGNED_DEVICE_SHARE_INTX_BIT) | |
83 | ||
84 | typedef struct MSIXTableEntry { | |
85 | uint32_t addr_lo; | |
86 | uint32_t addr_hi; | |
87 | uint32_t data; | |
88 | uint32_t ctrl; | |
89 | } MSIXTableEntry; | |
90 | ||
91 | typedef enum AssignedIRQType { | |
92 | ASSIGNED_IRQ_NONE = 0, | |
93 | ASSIGNED_IRQ_INTX_HOST_INTX, | |
94 | ASSIGNED_IRQ_INTX_HOST_MSI, | |
95 | ASSIGNED_IRQ_MSI, | |
96 | ASSIGNED_IRQ_MSIX | |
97 | } AssignedIRQType; | |
98 | ||
99 | typedef struct AssignedDevice { | |
100 | PCIDevice dev; | |
101 | PCIHostDeviceAddress host; | |
102 | uint32_t dev_id; | |
103 | uint32_t features; | |
104 | int intpin; | |
105 | AssignedDevRegion v_addrs[PCI_NUM_REGIONS - 1]; | |
106 | PCIDevRegions real_device; | |
107 | PCIINTxRoute intx_route; | |
108 | AssignedIRQType assigned_irq_type; | |
109 | struct { | |
110 | #define ASSIGNED_DEVICE_CAP_MSI (1 << 0) | |
111 | #define ASSIGNED_DEVICE_CAP_MSIX (1 << 1) | |
112 | uint32_t available; | |
113 | #define ASSIGNED_DEVICE_MSI_ENABLED (1 << 0) | |
114 | #define ASSIGNED_DEVICE_MSIX_ENABLED (1 << 1) | |
115 | #define ASSIGNED_DEVICE_MSIX_MASKED (1 << 2) | |
116 | uint32_t state; | |
117 | } cap; | |
118 | uint8_t emulate_config_read[PCI_CONFIG_SPACE_SIZE]; | |
119 | uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE]; | |
120 | int msi_virq_nr; | |
121 | int *msi_virq; | |
122 | MSIXTableEntry *msix_table; | |
a8170e5e | 123 | hwaddr msix_table_addr; |
aa1dd39c | 124 | uint16_t msix_table_size; |
c3ebd3ba JK |
125 | uint16_t msix_max; |
126 | MemoryRegion mmio; | |
127 | char *configfd_name; | |
128 | int32_t bootindex; | |
129 | } AssignedDevice; | |
130 | ||
1ea6305a GA |
131 | #define TYPE_PCI_ASSIGN "kvm-pci-assign" |
132 | #define PCI_ASSIGN(obj) OBJECT_CHECK(AssignedDevice, (obj), TYPE_PCI_ASSIGN) | |
133 | ||
c3ebd3ba JK |
134 | static void assigned_dev_update_irq_routing(PCIDevice *dev); |
135 | ||
136 | static void assigned_dev_load_option_rom(AssignedDevice *dev); | |
137 | ||
138 | static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev); | |
139 | ||
140 | static uint64_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region, | |
a8170e5e | 141 | hwaddr addr, int size, |
c3ebd3ba JK |
142 | uint64_t *data) |
143 | { | |
144 | uint64_t val = 0; | |
145 | int fd = dev_region->region->resource_fd; | |
146 | ||
3a902bab JK |
147 | if (data) { |
148 | DEBUG("pwrite data=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx | |
149 | ", addr="TARGET_FMT_plx"\n", *data, size, addr, addr); | |
150 | if (pwrite(fd, data, size, addr) != size) { | |
151 | error_report("%s - pwrite failed %s", __func__, strerror(errno)); | |
c3ebd3ba JK |
152 | } |
153 | } else { | |
3a902bab JK |
154 | if (pread(fd, &val, size, addr) != size) { |
155 | error_report("%s - pread failed %s", __func__, strerror(errno)); | |
156 | val = (1UL << (size * 8)) - 1; | |
c3ebd3ba | 157 | } |
3a902bab JK |
158 | DEBUG("pread val=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx |
159 | ", addr=" TARGET_FMT_plx "\n", val, size, addr, addr); | |
c3ebd3ba JK |
160 | } |
161 | return val; | |
162 | } | |
163 | ||
a8170e5e | 164 | static void assigned_dev_ioport_write(void *opaque, hwaddr addr, |
c3ebd3ba JK |
165 | uint64_t data, unsigned size) |
166 | { | |
167 | assigned_dev_ioport_rw(opaque, addr, size, &data); | |
168 | } | |
169 | ||
170 | static uint64_t assigned_dev_ioport_read(void *opaque, | |
a8170e5e | 171 | hwaddr addr, unsigned size) |
c3ebd3ba JK |
172 | { |
173 | return assigned_dev_ioport_rw(opaque, addr, size, NULL); | |
174 | } | |
175 | ||
a8170e5e | 176 | static uint32_t slow_bar_readb(void *opaque, hwaddr addr) |
c3ebd3ba JK |
177 | { |
178 | AssignedDevRegion *d = opaque; | |
179 | uint8_t *in = d->u.r_virtbase + addr; | |
180 | uint32_t r; | |
181 | ||
182 | r = *in; | |
bd50cbaa | 183 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r); |
c3ebd3ba JK |
184 | |
185 | return r; | |
186 | } | |
187 | ||
a8170e5e | 188 | static uint32_t slow_bar_readw(void *opaque, hwaddr addr) |
c3ebd3ba JK |
189 | { |
190 | AssignedDevRegion *d = opaque; | |
191 | uint16_t *in = (uint16_t *)(d->u.r_virtbase + addr); | |
192 | uint32_t r; | |
193 | ||
194 | r = *in; | |
bd50cbaa | 195 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r); |
c3ebd3ba JK |
196 | |
197 | return r; | |
198 | } | |
199 | ||
a8170e5e | 200 | static uint32_t slow_bar_readl(void *opaque, hwaddr addr) |
c3ebd3ba JK |
201 | { |
202 | AssignedDevRegion *d = opaque; | |
203 | uint32_t *in = (uint32_t *)(d->u.r_virtbase + addr); | |
204 | uint32_t r; | |
205 | ||
206 | r = *in; | |
bd50cbaa | 207 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r); |
c3ebd3ba JK |
208 | |
209 | return r; | |
210 | } | |
211 | ||
a8170e5e | 212 | static void slow_bar_writeb(void *opaque, hwaddr addr, uint32_t val) |
c3ebd3ba JK |
213 | { |
214 | AssignedDevRegion *d = opaque; | |
215 | uint8_t *out = d->u.r_virtbase + addr; | |
216 | ||
bd50cbaa | 217 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%02x\n", addr, val); |
c3ebd3ba JK |
218 | *out = val; |
219 | } | |
220 | ||
a8170e5e | 221 | static void slow_bar_writew(void *opaque, hwaddr addr, uint32_t val) |
c3ebd3ba JK |
222 | { |
223 | AssignedDevRegion *d = opaque; | |
224 | uint16_t *out = (uint16_t *)(d->u.r_virtbase + addr); | |
225 | ||
bd50cbaa | 226 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr, val); |
c3ebd3ba JK |
227 | *out = val; |
228 | } | |
229 | ||
a8170e5e | 230 | static void slow_bar_writel(void *opaque, hwaddr addr, uint32_t val) |
c3ebd3ba JK |
231 | { |
232 | AssignedDevRegion *d = opaque; | |
233 | uint32_t *out = (uint32_t *)(d->u.r_virtbase + addr); | |
234 | ||
bd50cbaa | 235 | DEBUG("addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, val); |
c3ebd3ba JK |
236 | *out = val; |
237 | } | |
238 | ||
239 | static const MemoryRegionOps slow_bar_ops = { | |
240 | .old_mmio = { | |
241 | .read = { slow_bar_readb, slow_bar_readw, slow_bar_readl, }, | |
242 | .write = { slow_bar_writeb, slow_bar_writew, slow_bar_writel, }, | |
243 | }, | |
244 | .endianness = DEVICE_NATIVE_ENDIAN, | |
245 | }; | |
246 | ||
247 | static void assigned_dev_iomem_setup(PCIDevice *pci_dev, int region_num, | |
248 | pcibus_t e_size) | |
249 | { | |
1ea6305a | 250 | AssignedDevice *r_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
251 | AssignedDevRegion *region = &r_dev->v_addrs[region_num]; |
252 | PCIRegion *real_region = &r_dev->real_device.regions[region_num]; | |
253 | ||
254 | if (e_size > 0) { | |
1437c94b PB |
255 | memory_region_init(®ion->container, OBJECT(pci_dev), |
256 | "assigned-dev-container", e_size); | |
c3ebd3ba JK |
257 | memory_region_add_subregion(®ion->container, 0, ®ion->real_iomem); |
258 | ||
259 | /* deal with MSI-X MMIO page */ | |
260 | if (real_region->base_addr <= r_dev->msix_table_addr && | |
261 | real_region->base_addr + real_region->size > | |
262 | r_dev->msix_table_addr) { | |
263 | uint64_t offset = r_dev->msix_table_addr - real_region->base_addr; | |
264 | ||
265 | memory_region_add_subregion_overlap(®ion->container, | |
266 | offset, | |
267 | &r_dev->mmio, | |
268 | 1); | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | static const MemoryRegionOps assigned_dev_ioport_ops = { | |
274 | .read = assigned_dev_ioport_read, | |
275 | .write = assigned_dev_ioport_write, | |
276 | .endianness = DEVICE_NATIVE_ENDIAN, | |
277 | }; | |
278 | ||
279 | static void assigned_dev_ioport_setup(PCIDevice *pci_dev, int region_num, | |
280 | pcibus_t size) | |
281 | { | |
1ea6305a | 282 | AssignedDevice *r_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
283 | AssignedDevRegion *region = &r_dev->v_addrs[region_num]; |
284 | ||
285 | region->e_size = size; | |
1437c94b PB |
286 | memory_region_init(®ion->container, OBJECT(pci_dev), |
287 | "assigned-dev-container", size); | |
288 | memory_region_init_io(®ion->real_iomem, OBJECT(pci_dev), | |
289 | &assigned_dev_ioport_ops, r_dev->v_addrs + region_num, | |
c3ebd3ba JK |
290 | "assigned-dev-iomem", size); |
291 | memory_region_add_subregion(®ion->container, 0, ®ion->real_iomem); | |
292 | } | |
293 | ||
294 | static uint32_t assigned_dev_pci_read(PCIDevice *d, int pos, int len) | |
295 | { | |
1ea6305a | 296 | AssignedDevice *pci_dev = PCI_ASSIGN(d); |
c3ebd3ba JK |
297 | uint32_t val; |
298 | ssize_t ret; | |
299 | int fd = pci_dev->real_device.config_fd; | |
300 | ||
301 | again: | |
302 | ret = pread(fd, &val, len, pos); | |
303 | if (ret != len) { | |
304 | if ((ret < 0) && (errno == EINTR || errno == EAGAIN)) { | |
305 | goto again; | |
306 | } | |
307 | ||
308 | hw_error("pci read failed, ret = %zd errno = %d\n", ret, errno); | |
309 | } | |
310 | ||
311 | return val; | |
312 | } | |
313 | ||
314 | static uint8_t assigned_dev_pci_read_byte(PCIDevice *d, int pos) | |
315 | { | |
316 | return (uint8_t)assigned_dev_pci_read(d, pos, 1); | |
317 | } | |
318 | ||
319 | static void assigned_dev_pci_write(PCIDevice *d, int pos, uint32_t val, int len) | |
320 | { | |
1ea6305a | 321 | AssignedDevice *pci_dev = PCI_ASSIGN(d); |
c3ebd3ba JK |
322 | ssize_t ret; |
323 | int fd = pci_dev->real_device.config_fd; | |
324 | ||
325 | again: | |
326 | ret = pwrite(fd, &val, len, pos); | |
327 | if (ret != len) { | |
328 | if ((ret < 0) && (errno == EINTR || errno == EAGAIN)) { | |
329 | goto again; | |
330 | } | |
331 | ||
332 | hw_error("pci write failed, ret = %zd errno = %d\n", ret, errno); | |
333 | } | |
334 | } | |
335 | ||
336 | static void assigned_dev_emulate_config_read(AssignedDevice *dev, | |
337 | uint32_t offset, uint32_t len) | |
338 | { | |
339 | memset(dev->emulate_config_read + offset, 0xff, len); | |
340 | } | |
341 | ||
342 | static void assigned_dev_direct_config_read(AssignedDevice *dev, | |
343 | uint32_t offset, uint32_t len) | |
344 | { | |
345 | memset(dev->emulate_config_read + offset, 0, len); | |
346 | } | |
347 | ||
348 | static void assigned_dev_direct_config_write(AssignedDevice *dev, | |
349 | uint32_t offset, uint32_t len) | |
350 | { | |
351 | memset(dev->emulate_config_write + offset, 0, len); | |
352 | } | |
353 | ||
354 | static uint8_t pci_find_cap_offset(PCIDevice *d, uint8_t cap, uint8_t start) | |
355 | { | |
356 | int id; | |
357 | int max_cap = 48; | |
358 | int pos = start ? start : PCI_CAPABILITY_LIST; | |
359 | int status; | |
360 | ||
361 | status = assigned_dev_pci_read_byte(d, PCI_STATUS); | |
362 | if ((status & PCI_STATUS_CAP_LIST) == 0) { | |
363 | return 0; | |
364 | } | |
365 | ||
366 | while (max_cap--) { | |
367 | pos = assigned_dev_pci_read_byte(d, pos); | |
368 | if (pos < 0x40) { | |
369 | break; | |
370 | } | |
371 | ||
372 | pos &= ~3; | |
373 | id = assigned_dev_pci_read_byte(d, pos + PCI_CAP_LIST_ID); | |
374 | ||
375 | if (id == 0xff) { | |
376 | break; | |
377 | } | |
378 | if (id == cap) { | |
379 | return pos; | |
380 | } | |
381 | ||
382 | pos += PCI_CAP_LIST_NEXT; | |
383 | } | |
384 | return 0; | |
385 | } | |
386 | ||
7d9cb533 LE |
387 | static void assigned_dev_register_regions(PCIRegion *io_regions, |
388 | unsigned long regions_num, | |
389 | AssignedDevice *pci_dev, | |
390 | Error **errp) | |
c3ebd3ba JK |
391 | { |
392 | uint32_t i; | |
393 | PCIRegion *cur_region = io_regions; | |
394 | ||
395 | for (i = 0; i < regions_num; i++, cur_region++) { | |
396 | if (!cur_region->valid) { | |
397 | continue; | |
398 | } | |
399 | ||
400 | /* handle memory io regions */ | |
401 | if (cur_region->type & IORESOURCE_MEM) { | |
0a2a59d3 XH |
402 | int t = PCI_BASE_ADDRESS_SPACE_MEMORY; |
403 | if (cur_region->type & IORESOURCE_PREFETCH) { | |
404 | t |= PCI_BASE_ADDRESS_MEM_PREFETCH; | |
405 | } | |
406 | if (cur_region->type & IORESOURCE_MEM_64) { | |
407 | t |= PCI_BASE_ADDRESS_MEM_TYPE_64; | |
408 | } | |
c3ebd3ba JK |
409 | |
410 | /* map physical memory */ | |
411 | pci_dev->v_addrs[i].u.r_virtbase = mmap(NULL, cur_region->size, | |
412 | PROT_WRITE | PROT_READ, | |
413 | MAP_SHARED, | |
414 | cur_region->resource_fd, | |
415 | (off_t)0); | |
416 | ||
417 | if (pci_dev->v_addrs[i].u.r_virtbase == MAP_FAILED) { | |
418 | pci_dev->v_addrs[i].u.r_virtbase = NULL; | |
7d9cb533 LE |
419 | error_setg_errno(errp, errno, "Couldn't mmap 0x%" PRIx64 "!", |
420 | cur_region->base_addr); | |
421 | return; | |
c3ebd3ba JK |
422 | } |
423 | ||
424 | pci_dev->v_addrs[i].r_size = cur_region->size; | |
425 | pci_dev->v_addrs[i].e_size = 0; | |
426 | ||
427 | /* add offset */ | |
428 | pci_dev->v_addrs[i].u.r_virtbase += | |
429 | (cur_region->base_addr & 0xFFF); | |
430 | ||
431 | if (cur_region->size & 0xFFF) { | |
432 | error_report("PCI region %d at address 0x%" PRIx64 " has " | |
433 | "size 0x%" PRIx64 ", which is not a multiple of " | |
434 | "4K. You might experience some performance hit " | |
435 | "due to that.", | |
436 | i, cur_region->base_addr, cur_region->size); | |
437 | memory_region_init_io(&pci_dev->v_addrs[i].real_iomem, | |
1437c94b PB |
438 | OBJECT(pci_dev), &slow_bar_ops, |
439 | &pci_dev->v_addrs[i], | |
c3ebd3ba JK |
440 | "assigned-dev-slow-bar", |
441 | cur_region->size); | |
442 | } else { | |
443 | void *virtbase = pci_dev->v_addrs[i].u.r_virtbase; | |
444 | char name[32]; | |
445 | snprintf(name, sizeof(name), "%s.bar%d", | |
446 | object_get_typename(OBJECT(pci_dev)), i); | |
447 | memory_region_init_ram_ptr(&pci_dev->v_addrs[i].real_iomem, | |
1437c94b PB |
448 | OBJECT(pci_dev), name, |
449 | cur_region->size, virtbase); | |
c3ebd3ba JK |
450 | vmstate_register_ram(&pci_dev->v_addrs[i].real_iomem, |
451 | &pci_dev->dev.qdev); | |
452 | } | |
453 | ||
454 | assigned_dev_iomem_setup(&pci_dev->dev, i, cur_region->size); | |
455 | pci_register_bar((PCIDevice *) pci_dev, i, t, | |
456 | &pci_dev->v_addrs[i].container); | |
457 | continue; | |
458 | } else { | |
459 | /* handle port io regions */ | |
460 | uint32_t val; | |
461 | int ret; | |
462 | ||
463 | /* Test kernel support for ioport resource read/write. Old | |
464 | * kernels return EIO. New kernels only allow 1/2/4 byte reads | |
465 | * so should return EINVAL for a 3 byte read */ | |
466 | ret = pread(pci_dev->v_addrs[i].region->resource_fd, &val, 3, 0); | |
467 | if (ret >= 0) { | |
468 | error_report("Unexpected return from I/O port read: %d", ret); | |
469 | abort(); | |
470 | } else if (errno != EINVAL) { | |
471 | error_report("Kernel doesn't support ioport resource " | |
472 | "access, hiding this region."); | |
473 | close(pci_dev->v_addrs[i].region->resource_fd); | |
474 | cur_region->valid = 0; | |
475 | continue; | |
476 | } | |
477 | ||
478 | pci_dev->v_addrs[i].u.r_baseport = cur_region->base_addr; | |
479 | pci_dev->v_addrs[i].r_size = cur_region->size; | |
480 | pci_dev->v_addrs[i].e_size = 0; | |
481 | ||
482 | assigned_dev_ioport_setup(&pci_dev->dev, i, cur_region->size); | |
483 | pci_register_bar((PCIDevice *) pci_dev, i, | |
484 | PCI_BASE_ADDRESS_SPACE_IO, | |
485 | &pci_dev->v_addrs[i].container); | |
486 | } | |
487 | } | |
488 | ||
489 | /* success */ | |
c3ebd3ba JK |
490 | } |
491 | ||
bcdcf75d LE |
492 | static void get_real_id(const char *devpath, const char *idname, uint16_t *val, |
493 | Error **errp) | |
c3ebd3ba JK |
494 | { |
495 | FILE *f; | |
496 | char name[128]; | |
497 | long id; | |
498 | ||
499 | snprintf(name, sizeof(name), "%s%s", devpath, idname); | |
500 | f = fopen(name, "r"); | |
501 | if (f == NULL) { | |
bcdcf75d LE |
502 | error_setg_file_open(errp, errno, name); |
503 | return; | |
c3ebd3ba JK |
504 | } |
505 | if (fscanf(f, "%li\n", &id) == 1) { | |
506 | *val = id; | |
507 | } else { | |
bcdcf75d | 508 | error_setg(errp, "Failed to parse contents of '%s'", name); |
c3ebd3ba JK |
509 | } |
510 | fclose(f); | |
c3ebd3ba JK |
511 | } |
512 | ||
bcdcf75d LE |
513 | static void get_real_vendor_id(const char *devpath, uint16_t *val, |
514 | Error **errp) | |
c3ebd3ba | 515 | { |
bcdcf75d | 516 | get_real_id(devpath, "vendor", val, errp); |
c3ebd3ba JK |
517 | } |
518 | ||
bcdcf75d LE |
519 | static void get_real_device_id(const char *devpath, uint16_t *val, |
520 | Error **errp) | |
c3ebd3ba | 521 | { |
bcdcf75d | 522 | get_real_id(devpath, "device", val, errp); |
c3ebd3ba JK |
523 | } |
524 | ||
5b877045 | 525 | static void get_real_device(AssignedDevice *pci_dev, Error **errp) |
c3ebd3ba JK |
526 | { |
527 | char dir[128], name[128]; | |
bcdcf75d | 528 | int fd, r = 0; |
c3ebd3ba JK |
529 | FILE *f; |
530 | uint64_t start, end, size, flags; | |
531 | uint16_t id; | |
532 | PCIRegion *rp; | |
533 | PCIDevRegions *dev = &pci_dev->real_device; | |
cf10a5b1 | 534 | Error *local_err = NULL; |
c3ebd3ba JK |
535 | |
536 | dev->region_number = 0; | |
537 | ||
538 | snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/", | |
867c47cb WY |
539 | pci_dev->host.domain, pci_dev->host.bus, |
540 | pci_dev->host.slot, pci_dev->host.function); | |
c3ebd3ba JK |
541 | |
542 | snprintf(name, sizeof(name), "%sconfig", dir); | |
543 | ||
544 | if (pci_dev->configfd_name && *pci_dev->configfd_name) { | |
1677f4c6 MA |
545 | dev->config_fd = monitor_fd_param(cur_mon, pci_dev->configfd_name, |
546 | &local_err); | |
cf10a5b1 | 547 | if (local_err) { |
5b877045 LE |
548 | error_propagate(errp, local_err); |
549 | return; | |
c3ebd3ba JK |
550 | } |
551 | } else { | |
552 | dev->config_fd = open(name, O_RDWR); | |
553 | ||
554 | if (dev->config_fd == -1) { | |
5b877045 LE |
555 | error_setg_file_open(errp, errno, name); |
556 | return; | |
c3ebd3ba JK |
557 | } |
558 | } | |
559 | again: | |
560 | r = read(dev->config_fd, pci_dev->dev.config, | |
561 | pci_config_size(&pci_dev->dev)); | |
562 | if (r < 0) { | |
563 | if (errno == EINTR || errno == EAGAIN) { | |
564 | goto again; | |
565 | } | |
5b877045 LE |
566 | error_setg_errno(errp, errno, "read(\"%s\")", |
567 | (pci_dev->configfd_name && *pci_dev->configfd_name) ? | |
568 | pci_dev->configfd_name : name); | |
569 | return; | |
c3ebd3ba JK |
570 | } |
571 | ||
572 | /* Restore or clear multifunction, this is always controlled by qemu */ | |
573 | if (pci_dev->dev.cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { | |
574 | pci_dev->dev.config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; | |
575 | } else { | |
576 | pci_dev->dev.config[PCI_HEADER_TYPE] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION; | |
577 | } | |
578 | ||
579 | /* Clear host resource mapping info. If we choose not to register a | |
580 | * BAR, such as might be the case with the option ROM, we can get | |
581 | * confusing, unwritable, residual addresses from the host here. */ | |
582 | memset(&pci_dev->dev.config[PCI_BASE_ADDRESS_0], 0, 24); | |
583 | memset(&pci_dev->dev.config[PCI_ROM_ADDRESS], 0, 4); | |
584 | ||
585 | snprintf(name, sizeof(name), "%sresource", dir); | |
586 | ||
587 | f = fopen(name, "r"); | |
588 | if (f == NULL) { | |
5b877045 LE |
589 | error_setg_file_open(errp, errno, name); |
590 | return; | |
c3ebd3ba JK |
591 | } |
592 | ||
593 | for (r = 0; r < PCI_ROM_SLOT; r++) { | |
594 | if (fscanf(f, "%" SCNi64 " %" SCNi64 " %" SCNi64 "\n", | |
595 | &start, &end, &flags) != 3) { | |
596 | break; | |
597 | } | |
598 | ||
599 | rp = dev->regions + r; | |
600 | rp->valid = 0; | |
601 | rp->resource_fd = -1; | |
602 | size = end - start + 1; | |
0a2a59d3 XH |
603 | flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |
604 | | IORESOURCE_MEM_64; | |
c3ebd3ba JK |
605 | if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0) { |
606 | continue; | |
607 | } | |
608 | if (flags & IORESOURCE_MEM) { | |
609 | flags &= ~IORESOURCE_IO; | |
610 | } else { | |
611 | flags &= ~IORESOURCE_PREFETCH; | |
612 | } | |
613 | snprintf(name, sizeof(name), "%sresource%d", dir, r); | |
614 | fd = open(name, O_RDWR); | |
615 | if (fd == -1) { | |
616 | continue; | |
617 | } | |
618 | rp->resource_fd = fd; | |
619 | ||
620 | rp->type = flags; | |
621 | rp->valid = 1; | |
622 | rp->base_addr = start; | |
623 | rp->size = size; | |
624 | pci_dev->v_addrs[r].region = rp; | |
625 | DEBUG("region %d size %" PRIu64 " start 0x%" PRIx64 | |
626 | " type %d resource_fd %d\n", | |
627 | r, rp->size, start, rp->type, rp->resource_fd); | |
628 | } | |
629 | ||
630 | fclose(f); | |
631 | ||
632 | /* read and fill vendor ID */ | |
bcdcf75d LE |
633 | get_real_vendor_id(dir, &id, &local_err); |
634 | if (local_err) { | |
5b877045 LE |
635 | error_propagate(errp, local_err); |
636 | return; | |
c3ebd3ba JK |
637 | } |
638 | pci_dev->dev.config[0] = id & 0xff; | |
639 | pci_dev->dev.config[1] = (id & 0xff00) >> 8; | |
640 | ||
641 | /* read and fill device ID */ | |
bcdcf75d LE |
642 | get_real_device_id(dir, &id, &local_err); |
643 | if (local_err) { | |
5b877045 LE |
644 | error_propagate(errp, local_err); |
645 | return; | |
c3ebd3ba JK |
646 | } |
647 | pci_dev->dev.config[2] = id & 0xff; | |
648 | pci_dev->dev.config[3] = (id & 0xff00) >> 8; | |
649 | ||
650 | pci_word_test_and_clear_mask(pci_dev->emulate_config_write + PCI_COMMAND, | |
651 | PCI_COMMAND_MASTER | PCI_COMMAND_INTX_DISABLE); | |
652 | ||
653 | dev->region_number = r; | |
c3ebd3ba JK |
654 | } |
655 | ||
656 | static void free_msi_virqs(AssignedDevice *dev) | |
657 | { | |
658 | int i; | |
659 | ||
660 | for (i = 0; i < dev->msi_virq_nr; i++) { | |
661 | if (dev->msi_virq[i] >= 0) { | |
662 | kvm_irqchip_release_virq(kvm_state, dev->msi_virq[i]); | |
663 | dev->msi_virq[i] = -1; | |
664 | } | |
665 | } | |
666 | g_free(dev->msi_virq); | |
667 | dev->msi_virq = NULL; | |
668 | dev->msi_virq_nr = 0; | |
669 | } | |
670 | ||
671 | static void free_assigned_device(AssignedDevice *dev) | |
672 | { | |
673 | int i; | |
674 | ||
675 | if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) { | |
676 | assigned_dev_unregister_msix_mmio(dev); | |
677 | } | |
678 | for (i = 0; i < dev->real_device.region_number; i++) { | |
679 | PCIRegion *pci_region = &dev->real_device.regions[i]; | |
680 | AssignedDevRegion *region = &dev->v_addrs[i]; | |
681 | ||
682 | if (!pci_region->valid) { | |
683 | continue; | |
684 | } | |
685 | if (pci_region->type & IORESOURCE_IO) { | |
686 | if (region->u.r_baseport) { | |
687 | memory_region_del_subregion(®ion->container, | |
688 | ®ion->real_iomem); | |
c3ebd3ba JK |
689 | } |
690 | } else if (pci_region->type & IORESOURCE_MEM) { | |
691 | if (region->u.r_virtbase) { | |
692 | memory_region_del_subregion(®ion->container, | |
693 | ®ion->real_iomem); | |
694 | ||
695 | /* Remove MSI-X table subregion */ | |
696 | if (pci_region->base_addr <= dev->msix_table_addr && | |
697 | pci_region->base_addr + pci_region->size > | |
698 | dev->msix_table_addr) { | |
699 | memory_region_del_subregion(®ion->container, | |
700 | &dev->mmio); | |
701 | } | |
c3ebd3ba JK |
702 | if (munmap(region->u.r_virtbase, |
703 | (pci_region->size + 0xFFF) & 0xFFFFF000)) { | |
704 | error_report("Failed to unmap assigned device region: %s", | |
705 | strerror(errno)); | |
706 | } | |
707 | } | |
708 | } | |
709 | if (pci_region->resource_fd >= 0) { | |
710 | close(pci_region->resource_fd); | |
711 | } | |
712 | } | |
713 | ||
714 | if (dev->real_device.config_fd >= 0) { | |
715 | close(dev->real_device.config_fd); | |
716 | } | |
717 | ||
718 | free_msi_virqs(dev); | |
719 | } | |
720 | ||
4951013f LE |
721 | /* This function tries to determine the cause of the PCI assignment failure. It |
722 | * always returns the cause as a dynamically allocated, human readable string. | |
723 | * If the function fails to determine the cause for any internal reason, then | |
724 | * the returned string will state that fact. | |
725 | */ | |
726 | static char *assign_failed_examine(const AssignedDevice *dev) | |
c3ebd3ba JK |
727 | { |
728 | char name[PATH_MAX], dir[PATH_MAX], driver[PATH_MAX] = {}, *ns; | |
729 | uint16_t vendor_id, device_id; | |
730 | int r; | |
bcdcf75d | 731 | Error *local_err = NULL; |
c3ebd3ba JK |
732 | |
733 | snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/", | |
734 | dev->host.domain, dev->host.bus, dev->host.slot, | |
735 | dev->host.function); | |
736 | ||
737 | snprintf(name, sizeof(name), "%sdriver", dir); | |
738 | ||
739 | r = readlink(name, driver, sizeof(driver)); | |
740 | if ((r <= 0) || r >= sizeof(driver)) { | |
741 | goto fail; | |
742 | } | |
743 | ||
82d07945 | 744 | driver[r] = 0; |
c3ebd3ba JK |
745 | ns = strrchr(driver, '/'); |
746 | if (!ns) { | |
747 | goto fail; | |
748 | } | |
749 | ||
750 | ns++; | |
751 | ||
bcdcf75d LE |
752 | if ((get_real_vendor_id(dir, &vendor_id, &local_err), local_err) || |
753 | (get_real_device_id(dir, &device_id, &local_err), local_err)) { | |
754 | /* We're already analyzing an assignment error, so we suppress this | |
755 | * one just like the others above. | |
756 | */ | |
757 | error_free(local_err); | |
c3ebd3ba JK |
758 | goto fail; |
759 | } | |
760 | ||
4951013f LE |
761 | return g_strdup_printf( |
762 | "*** The driver '%s' is occupying your device %04x:%02x:%02x.%x.\n" | |
25a666d2 CR |
763 | "***\n" |
764 | "*** You can try the following commands to free it:\n" | |
765 | "***\n" | |
766 | "*** $ echo \"%04x %04x\" > /sys/bus/pci/drivers/pci-stub/new_id\n" | |
767 | "*** $ echo \"%04x:%02x:%02x.%x\" > /sys/bus/pci/drivers/%s/unbind\n" | |
768 | "*** $ echo \"%04x:%02x:%02x.%x\" > /sys/bus/pci/drivers/" | |
769 | "pci-stub/bind\n" | |
770 | "*** $ echo \"%04x %04x\" > /sys/bus/pci/drivers/pci-stub/remove_id\n" | |
c3d2d68a | 771 | "***\n", |
25a666d2 CR |
772 | ns, dev->host.domain, dev->host.bus, dev->host.slot, |
773 | dev->host.function, vendor_id, device_id, | |
774 | dev->host.domain, dev->host.bus, dev->host.slot, dev->host.function, | |
775 | ns, dev->host.domain, dev->host.bus, dev->host.slot, | |
776 | dev->host.function, vendor_id, device_id); | |
c3ebd3ba | 777 | |
c3ebd3ba | 778 | fail: |
c3d2d68a | 779 | return g_strdup("Couldn't find out why.\n"); |
c3ebd3ba JK |
780 | } |
781 | ||
6877cff0 | 782 | static void assign_device(AssignedDevice *dev, Error **errp) |
c3ebd3ba JK |
783 | { |
784 | uint32_t flags = KVM_DEV_ASSIGN_ENABLE_IOMMU; | |
785 | int r; | |
786 | ||
787 | /* Only pass non-zero PCI segment to capable module */ | |
788 | if (!kvm_check_extension(kvm_state, KVM_CAP_PCI_SEGMENT) && | |
789 | dev->host.domain) { | |
6877cff0 LE |
790 | error_setg(errp, "Can't assign device inside non-zero PCI segment " |
791 | "as this KVM module doesn't support it."); | |
792 | return; | |
c3ebd3ba JK |
793 | } |
794 | ||
795 | if (!kvm_check_extension(kvm_state, KVM_CAP_IOMMU)) { | |
6877cff0 LE |
796 | error_setg(errp, "No IOMMU found. Unable to assign device \"%s\"", |
797 | dev->dev.qdev.id); | |
798 | return; | |
c3ebd3ba JK |
799 | } |
800 | ||
801 | if (dev->features & ASSIGNED_DEVICE_SHARE_INTX_MASK && | |
802 | kvm_has_intx_set_mask()) { | |
803 | flags |= KVM_DEV_ASSIGN_PCI_2_3; | |
804 | } | |
805 | ||
806 | r = kvm_device_pci_assign(kvm_state, &dev->host, flags, &dev->dev_id); | |
807 | if (r < 0) { | |
c3ebd3ba | 808 | switch (r) { |
4951013f LE |
809 | case -EBUSY: { |
810 | char *cause; | |
811 | ||
812 | cause = assign_failed_examine(dev); | |
c3d2d68a MA |
813 | error_setg_errno(errp, -r, "Failed to assign device \"%s\"", |
814 | dev->dev.qdev.id); | |
815 | error_append_hint(errp, "%s", cause); | |
4951013f | 816 | g_free(cause); |
c3ebd3ba | 817 | break; |
4951013f | 818 | } |
c3ebd3ba | 819 | default: |
6877cff0 LE |
820 | error_setg_errno(errp, -r, "Failed to assign device \"%s\"", |
821 | dev->dev.qdev.id); | |
c3ebd3ba JK |
822 | break; |
823 | } | |
824 | } | |
c3ebd3ba JK |
825 | } |
826 | ||
665f119f | 827 | static void verify_irqchip_in_kernel(Error **errp) |
c3ebd3ba JK |
828 | { |
829 | if (kvm_irqchip_in_kernel()) { | |
665f119f | 830 | return; |
c3ebd3ba | 831 | } |
665f119f | 832 | error_setg(errp, "pci-assign requires KVM with in-kernel irqchip enabled"); |
c3ebd3ba JK |
833 | } |
834 | ||
ef47827a | 835 | static int assign_intx(AssignedDevice *dev, Error **errp) |
c3ebd3ba JK |
836 | { |
837 | AssignedIRQType new_type; | |
838 | PCIINTxRoute intx_route; | |
839 | bool intx_host_msi; | |
840 | int r; | |
665f119f | 841 | Error *local_err = NULL; |
c3ebd3ba JK |
842 | |
843 | /* Interrupt PIN 0 means don't use INTx */ | |
844 | if (assigned_dev_pci_read_byte(&dev->dev, PCI_INTERRUPT_PIN) == 0) { | |
845 | pci_device_set_intx_routing_notifier(&dev->dev, NULL); | |
846 | return 0; | |
847 | } | |
848 | ||
665f119f LE |
849 | verify_irqchip_in_kernel(&local_err); |
850 | if (local_err) { | |
ef47827a | 851 | error_propagate(errp, local_err); |
c3ebd3ba JK |
852 | return -ENOTSUP; |
853 | } | |
854 | ||
855 | pci_device_set_intx_routing_notifier(&dev->dev, | |
856 | assigned_dev_update_irq_routing); | |
857 | ||
858 | intx_route = pci_device_route_intx_to_irq(&dev->dev, dev->intpin); | |
859 | assert(intx_route.mode != PCI_INTX_INVERTED); | |
860 | ||
4774d7b2 | 861 | if (!pci_intx_route_changed(&dev->intx_route, &intx_route)) { |
c3ebd3ba JK |
862 | return 0; |
863 | } | |
864 | ||
865 | switch (dev->assigned_irq_type) { | |
866 | case ASSIGNED_IRQ_INTX_HOST_INTX: | |
867 | case ASSIGNED_IRQ_INTX_HOST_MSI: | |
868 | intx_host_msi = dev->assigned_irq_type == ASSIGNED_IRQ_INTX_HOST_MSI; | |
869 | r = kvm_device_intx_deassign(kvm_state, dev->dev_id, intx_host_msi); | |
870 | break; | |
871 | case ASSIGNED_IRQ_MSI: | |
872 | r = kvm_device_msi_deassign(kvm_state, dev->dev_id); | |
873 | break; | |
874 | case ASSIGNED_IRQ_MSIX: | |
875 | r = kvm_device_msix_deassign(kvm_state, dev->dev_id); | |
876 | break; | |
877 | default: | |
878 | r = 0; | |
879 | break; | |
880 | } | |
881 | if (r) { | |
882 | perror("assign_intx: deassignment of previous interrupt failed"); | |
883 | } | |
884 | dev->assigned_irq_type = ASSIGNED_IRQ_NONE; | |
885 | ||
886 | if (intx_route.mode == PCI_INTX_DISABLED) { | |
887 | dev->intx_route = intx_route; | |
888 | return 0; | |
889 | } | |
890 | ||
891 | retry: | |
892 | if (dev->features & ASSIGNED_DEVICE_PREFER_MSI_MASK && | |
893 | dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) { | |
894 | intx_host_msi = true; | |
895 | new_type = ASSIGNED_IRQ_INTX_HOST_MSI; | |
896 | } else { | |
897 | intx_host_msi = false; | |
898 | new_type = ASSIGNED_IRQ_INTX_HOST_INTX; | |
899 | } | |
900 | ||
901 | r = kvm_device_intx_assign(kvm_state, dev->dev_id, intx_host_msi, | |
902 | intx_route.irq); | |
903 | if (r < 0) { | |
904 | if (r == -EIO && !(dev->features & ASSIGNED_DEVICE_PREFER_MSI_MASK) && | |
905 | dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) { | |
906 | /* Retry with host-side MSI. There might be an IRQ conflict and | |
907 | * either the kernel or the device doesn't support sharing. */ | |
908 | error_report("Host-side INTx sharing not supported, " | |
474c2134 MA |
909 | "using MSI instead"); |
910 | error_printf("Some devices do not work properly in this mode.\n"); | |
c3ebd3ba JK |
911 | dev->features |= ASSIGNED_DEVICE_PREFER_MSI_MASK; |
912 | goto retry; | |
913 | } | |
c3d2d68a | 914 | error_setg_errno(errp, -r, "Failed to assign irq for \"%s\"", |
ef47827a | 915 | dev->dev.qdev.id); |
c3d2d68a MA |
916 | error_append_hint(errp, "Perhaps you are assigning a device " |
917 | "that shares an IRQ with another device?\n"); | |
c3ebd3ba JK |
918 | return r; |
919 | } | |
920 | ||
921 | dev->intx_route = intx_route; | |
922 | dev->assigned_irq_type = new_type; | |
923 | return r; | |
924 | } | |
925 | ||
926 | static void deassign_device(AssignedDevice *dev) | |
927 | { | |
928 | int r; | |
929 | ||
930 | r = kvm_device_pci_deassign(kvm_state, dev->dev_id); | |
931 | assert(r == 0); | |
932 | } | |
933 | ||
934 | /* The pci config space got updated. Check if irq numbers have changed | |
935 | * for our devices | |
936 | */ | |
937 | static void assigned_dev_update_irq_routing(PCIDevice *dev) | |
938 | { | |
1ea6305a | 939 | AssignedDevice *assigned_dev = PCI_ASSIGN(dev); |
c3ebd3ba JK |
940 | Error *err = NULL; |
941 | int r; | |
942 | ||
ef47827a | 943 | r = assign_intx(assigned_dev, &err); |
c3ebd3ba | 944 | if (r < 0) { |
565f65d2 | 945 | error_report_err(err); |
ef47827a | 946 | err = NULL; |
c3ebd3ba JK |
947 | qdev_unplug(&dev->qdev, &err); |
948 | assert(!err); | |
949 | } | |
950 | } | |
951 | ||
952 | static void assigned_dev_update_msi(PCIDevice *pci_dev) | |
953 | { | |
1ea6305a | 954 | AssignedDevice *assigned_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
955 | uint8_t ctrl_byte = pci_get_byte(pci_dev->config + pci_dev->msi_cap + |
956 | PCI_MSI_FLAGS); | |
957 | int r; | |
958 | ||
959 | /* Some guests gratuitously disable MSI even if they're not using it, | |
960 | * try to catch this by only deassigning irqs if the guest is using | |
961 | * MSI or intends to start. */ | |
962 | if (assigned_dev->assigned_irq_type == ASSIGNED_IRQ_MSI || | |
963 | (ctrl_byte & PCI_MSI_FLAGS_ENABLE)) { | |
964 | r = kvm_device_msi_deassign(kvm_state, assigned_dev->dev_id); | |
965 | /* -ENXIO means no assigned irq */ | |
966 | if (r && r != -ENXIO) { | |
967 | perror("assigned_dev_update_msi: deassign irq"); | |
968 | } | |
969 | ||
970 | free_msi_virqs(assigned_dev); | |
971 | ||
972 | assigned_dev->assigned_irq_type = ASSIGNED_IRQ_NONE; | |
973 | pci_device_set_intx_routing_notifier(pci_dev, NULL); | |
974 | } | |
975 | ||
976 | if (ctrl_byte & PCI_MSI_FLAGS_ENABLE) { | |
c3ebd3ba JK |
977 | int virq; |
978 | ||
d1f6af6a | 979 | virq = kvm_irqchip_add_msi_route(kvm_state, 0, pci_dev); |
c3ebd3ba JK |
980 | if (virq < 0) { |
981 | perror("assigned_dev_update_msi: kvm_irqchip_add_msi_route"); | |
982 | return; | |
983 | } | |
984 | ||
985 | assigned_dev->msi_virq = g_malloc(sizeof(*assigned_dev->msi_virq)); | |
986 | assigned_dev->msi_virq_nr = 1; | |
987 | assigned_dev->msi_virq[0] = virq; | |
988 | if (kvm_device_msi_assign(kvm_state, assigned_dev->dev_id, virq) < 0) { | |
989 | perror("assigned_dev_update_msi: kvm_device_msi_assign"); | |
990 | } | |
991 | ||
992 | assigned_dev->intx_route.mode = PCI_INTX_DISABLED; | |
993 | assigned_dev->intx_route.irq = -1; | |
994 | assigned_dev->assigned_irq_type = ASSIGNED_IRQ_MSI; | |
995 | } else { | |
ef47827a LE |
996 | Error *local_err = NULL; |
997 | ||
998 | assign_intx(assigned_dev, &local_err); | |
999 | if (local_err) { | |
565f65d2 | 1000 | error_report_err(local_err); |
ef47827a | 1001 | } |
c3ebd3ba JK |
1002 | } |
1003 | } | |
1004 | ||
3459f01b AW |
1005 | static void assigned_dev_update_msi_msg(PCIDevice *pci_dev) |
1006 | { | |
1ea6305a | 1007 | AssignedDevice *assigned_dev = PCI_ASSIGN(pci_dev); |
3459f01b AW |
1008 | uint8_t ctrl_byte = pci_get_byte(pci_dev->config + pci_dev->msi_cap + |
1009 | PCI_MSI_FLAGS); | |
1010 | ||
1011 | if (assigned_dev->assigned_irq_type != ASSIGNED_IRQ_MSI || | |
1012 | !(ctrl_byte & PCI_MSI_FLAGS_ENABLE)) { | |
1013 | return; | |
1014 | } | |
1015 | ||
1016 | kvm_irqchip_update_msi_route(kvm_state, assigned_dev->msi_virq[0], | |
dc9f06ca | 1017 | msi_get_message(pci_dev, 0), pci_dev); |
3f1fea0f | 1018 | kvm_irqchip_commit_routes(kvm_state); |
3459f01b AW |
1019 | } |
1020 | ||
c3ebd3ba JK |
1021 | static bool assigned_dev_msix_masked(MSIXTableEntry *entry) |
1022 | { | |
1023 | return (entry->ctrl & cpu_to_le32(0x1)) != 0; | |
1024 | } | |
1025 | ||
feb9a2ab AW |
1026 | /* |
1027 | * When MSI-X is first enabled the vector table typically has all the | |
1028 | * vectors masked, so we can't use that as the obvious test to figure out | |
1029 | * how many vectors to initially enable. Instead we look at the data field | |
1030 | * because this is what worked for pci-assign for a long time. This makes | |
1031 | * sure the physical MSI-X state tracks the guest's view, which is important | |
1032 | * for some VF/PF and PF/fw communication channels. | |
1033 | */ | |
1034 | static bool assigned_dev_msix_skipped(MSIXTableEntry *entry) | |
1035 | { | |
1036 | return !entry->data; | |
1037 | } | |
1038 | ||
c3ebd3ba JK |
1039 | static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev) |
1040 | { | |
1ea6305a | 1041 | AssignedDevice *adev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1042 | uint16_t entries_nr = 0; |
1043 | int i, r = 0; | |
1044 | MSIXTableEntry *entry = adev->msix_table; | |
c3ebd3ba JK |
1045 | |
1046 | /* Get the usable entry number for allocating */ | |
1047 | for (i = 0; i < adev->msix_max; i++, entry++) { | |
feb9a2ab | 1048 | if (assigned_dev_msix_skipped(entry)) { |
c3ebd3ba JK |
1049 | continue; |
1050 | } | |
1051 | entries_nr++; | |
1052 | } | |
1053 | ||
1054 | DEBUG("MSI-X entries: %d\n", entries_nr); | |
1055 | ||
1056 | /* It's valid to enable MSI-X with all entries masked */ | |
1057 | if (!entries_nr) { | |
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | r = kvm_device_msix_init_vectors(kvm_state, adev->dev_id, entries_nr); | |
1062 | if (r != 0) { | |
1063 | error_report("fail to set MSI-X entry number for MSIX! %s", | |
1064 | strerror(-r)); | |
1065 | return r; | |
1066 | } | |
1067 | ||
1068 | free_msi_virqs(adev); | |
1069 | ||
1070 | adev->msi_virq_nr = adev->msix_max; | |
1071 | adev->msi_virq = g_malloc(adev->msix_max * sizeof(*adev->msi_virq)); | |
1072 | ||
1073 | entry = adev->msix_table; | |
1074 | for (i = 0; i < adev->msix_max; i++, entry++) { | |
1075 | adev->msi_virq[i] = -1; | |
1076 | ||
feb9a2ab | 1077 | if (assigned_dev_msix_skipped(entry)) { |
c3ebd3ba JK |
1078 | continue; |
1079 | } | |
1080 | ||
d1f6af6a | 1081 | r = kvm_irqchip_add_msi_route(kvm_state, i, pci_dev); |
c3ebd3ba JK |
1082 | if (r < 0) { |
1083 | return r; | |
1084 | } | |
1085 | adev->msi_virq[i] = r; | |
1086 | ||
1087 | DEBUG("MSI-X vector %d, gsi %d, addr %08x_%08x, data %08x\n", i, | |
1088 | r, entry->addr_hi, entry->addr_lo, entry->data); | |
1089 | ||
1090 | r = kvm_device_msix_set_vector(kvm_state, adev->dev_id, i, | |
1091 | adev->msi_virq[i]); | |
1092 | if (r) { | |
1093 | error_report("fail to set MSI-X entry! %s", strerror(-r)); | |
1094 | break; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | return r; | |
1099 | } | |
1100 | ||
1101 | static void assigned_dev_update_msix(PCIDevice *pci_dev) | |
1102 | { | |
1ea6305a | 1103 | AssignedDevice *assigned_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1104 | uint16_t ctrl_word = pci_get_word(pci_dev->config + pci_dev->msix_cap + |
1105 | PCI_MSIX_FLAGS); | |
1106 | int r; | |
1107 | ||
1108 | /* Some guests gratuitously disable MSIX even if they're not using it, | |
1109 | * try to catch this by only deassigning irqs if the guest is using | |
1110 | * MSIX or intends to start. */ | |
1111 | if ((assigned_dev->assigned_irq_type == ASSIGNED_IRQ_MSIX) || | |
1112 | (ctrl_word & PCI_MSIX_FLAGS_ENABLE)) { | |
1113 | r = kvm_device_msix_deassign(kvm_state, assigned_dev->dev_id); | |
1114 | /* -ENXIO means no assigned irq */ | |
1115 | if (r && r != -ENXIO) { | |
1116 | perror("assigned_dev_update_msix: deassign irq"); | |
1117 | } | |
1118 | ||
1119 | free_msi_virqs(assigned_dev); | |
1120 | ||
1121 | assigned_dev->assigned_irq_type = ASSIGNED_IRQ_NONE; | |
1122 | pci_device_set_intx_routing_notifier(pci_dev, NULL); | |
1123 | } | |
1124 | ||
1125 | if (ctrl_word & PCI_MSIX_FLAGS_ENABLE) { | |
1126 | if (assigned_dev_update_msix_mmio(pci_dev) < 0) { | |
1127 | perror("assigned_dev_update_msix_mmio"); | |
1128 | return; | |
1129 | } | |
1130 | ||
1131 | if (assigned_dev->msi_virq_nr > 0) { | |
1132 | if (kvm_device_msix_assign(kvm_state, assigned_dev->dev_id) < 0) { | |
1133 | perror("assigned_dev_enable_msix: assign irq"); | |
1134 | return; | |
1135 | } | |
1136 | } | |
1137 | assigned_dev->intx_route.mode = PCI_INTX_DISABLED; | |
1138 | assigned_dev->intx_route.irq = -1; | |
1139 | assigned_dev->assigned_irq_type = ASSIGNED_IRQ_MSIX; | |
1140 | } else { | |
ef47827a LE |
1141 | Error *local_err = NULL; |
1142 | ||
1143 | assign_intx(assigned_dev, &local_err); | |
1144 | if (local_err) { | |
565f65d2 | 1145 | error_report_err(local_err); |
ef47827a | 1146 | } |
c3ebd3ba JK |
1147 | } |
1148 | } | |
1149 | ||
1150 | static uint32_t assigned_dev_pci_read_config(PCIDevice *pci_dev, | |
1151 | uint32_t address, int len) | |
1152 | { | |
1ea6305a | 1153 | AssignedDevice *assigned_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1154 | uint32_t virt_val = pci_default_read_config(pci_dev, address, len); |
1155 | uint32_t real_val, emulate_mask, full_emulation_mask; | |
1156 | ||
1157 | emulate_mask = 0; | |
1158 | memcpy(&emulate_mask, assigned_dev->emulate_config_read + address, len); | |
1159 | emulate_mask = le32_to_cpu(emulate_mask); | |
1160 | ||
1161 | full_emulation_mask = 0xffffffff >> (32 - len * 8); | |
1162 | ||
1163 | if (emulate_mask != full_emulation_mask) { | |
1164 | real_val = assigned_dev_pci_read(pci_dev, address, len); | |
1165 | return (virt_val & emulate_mask) | (real_val & ~emulate_mask); | |
1166 | } else { | |
1167 | return virt_val; | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | static void assigned_dev_pci_write_config(PCIDevice *pci_dev, uint32_t address, | |
1172 | uint32_t val, int len) | |
1173 | { | |
1ea6305a | 1174 | AssignedDevice *assigned_dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1175 | uint16_t old_cmd = pci_get_word(pci_dev->config + PCI_COMMAND); |
1176 | uint32_t emulate_mask, full_emulation_mask; | |
1177 | int ret; | |
1178 | ||
1179 | pci_default_write_config(pci_dev, address, val, len); | |
1180 | ||
1181 | if (kvm_has_intx_set_mask() && | |
1182 | range_covers_byte(address, len, PCI_COMMAND + 1)) { | |
1183 | bool intx_masked = (pci_get_word(pci_dev->config + PCI_COMMAND) & | |
1184 | PCI_COMMAND_INTX_DISABLE); | |
1185 | ||
1186 | if (intx_masked != !!(old_cmd & PCI_COMMAND_INTX_DISABLE)) { | |
1187 | ret = kvm_device_intx_set_mask(kvm_state, assigned_dev->dev_id, | |
1188 | intx_masked); | |
1189 | if (ret) { | |
1190 | perror("assigned_dev_pci_write_config: set intx mask"); | |
1191 | } | |
1192 | } | |
1193 | } | |
1194 | if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) { | |
1195 | if (range_covers_byte(address, len, | |
1196 | pci_dev->msi_cap + PCI_MSI_FLAGS)) { | |
1197 | assigned_dev_update_msi(pci_dev); | |
3459f01b AW |
1198 | } else if (ranges_overlap(address, len, /* 32bit MSI only */ |
1199 | pci_dev->msi_cap + PCI_MSI_ADDRESS_LO, 6)) { | |
1200 | assigned_dev_update_msi_msg(pci_dev); | |
c3ebd3ba JK |
1201 | } |
1202 | } | |
1203 | if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) { | |
1204 | if (range_covers_byte(address, len, | |
1205 | pci_dev->msix_cap + PCI_MSIX_FLAGS + 1)) { | |
1206 | assigned_dev_update_msix(pci_dev); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | emulate_mask = 0; | |
1211 | memcpy(&emulate_mask, assigned_dev->emulate_config_write + address, len); | |
1212 | emulate_mask = le32_to_cpu(emulate_mask); | |
1213 | ||
1214 | full_emulation_mask = 0xffffffff >> (32 - len * 8); | |
1215 | ||
1216 | if (emulate_mask != full_emulation_mask) { | |
1217 | if (emulate_mask) { | |
1218 | val &= ~emulate_mask; | |
1219 | val |= assigned_dev_pci_read(pci_dev, address, len) & emulate_mask; | |
1220 | } | |
1221 | assigned_dev_pci_write(pci_dev, address, val, len); | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | static void assigned_dev_setup_cap_read(AssignedDevice *dev, uint32_t offset, | |
1226 | uint32_t len) | |
1227 | { | |
1228 | assigned_dev_direct_config_read(dev, offset, len); | |
1229 | assigned_dev_emulate_config_read(dev, offset + PCI_CAP_LIST_NEXT, 1); | |
1230 | } | |
1231 | ||
64135217 | 1232 | static int assigned_device_pci_cap_init(PCIDevice *pci_dev, Error **errp) |
c3ebd3ba | 1233 | { |
1ea6305a | 1234 | AssignedDevice *dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1235 | PCIRegion *pci_region = dev->real_device.regions; |
1236 | int ret, pos; | |
665f119f | 1237 | Error *local_err = NULL; |
c3ebd3ba JK |
1238 | |
1239 | /* Clear initial capabilities pointer and status copied from hw */ | |
1240 | pci_set_byte(pci_dev->config + PCI_CAPABILITY_LIST, 0); | |
1241 | pci_set_word(pci_dev->config + PCI_STATUS, | |
1242 | pci_get_word(pci_dev->config + PCI_STATUS) & | |
1243 | ~PCI_STATUS_CAP_LIST); | |
1244 | ||
1245 | /* Expose MSI capability | |
1246 | * MSI capability is the 1st capability in capability config */ | |
1247 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_MSI, 0); | |
1248 | if (pos != 0 && kvm_check_extension(kvm_state, KVM_CAP_ASSIGN_DEV_IRQ)) { | |
665f119f LE |
1249 | verify_irqchip_in_kernel(&local_err); |
1250 | if (local_err) { | |
64135217 | 1251 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1252 | return -ENOTSUP; |
1253 | } | |
64e184e2 | 1254 | dev->dev.cap_present |= QEMU_PCI_CAP_MSI; |
c3ebd3ba JK |
1255 | dev->cap.available |= ASSIGNED_DEVICE_CAP_MSI; |
1256 | /* Only 32-bit/no-mask currently supported */ | |
42ee4194 LE |
1257 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_MSI, pos, 10, |
1258 | &local_err); | |
c3ebd3ba | 1259 | if (ret < 0) { |
64135217 | 1260 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1261 | return ret; |
1262 | } | |
1263 | pci_dev->msi_cap = pos; | |
1264 | ||
1265 | pci_set_word(pci_dev->config + pos + PCI_MSI_FLAGS, | |
1266 | pci_get_word(pci_dev->config + pos + PCI_MSI_FLAGS) & | |
1267 | PCI_MSI_FLAGS_QMASK); | |
1268 | pci_set_long(pci_dev->config + pos + PCI_MSI_ADDRESS_LO, 0); | |
1269 | pci_set_word(pci_dev->config + pos + PCI_MSI_DATA_32, 0); | |
1270 | ||
1271 | /* Set writable fields */ | |
1272 | pci_set_word(pci_dev->wmask + pos + PCI_MSI_FLAGS, | |
1273 | PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE); | |
1274 | pci_set_long(pci_dev->wmask + pos + PCI_MSI_ADDRESS_LO, 0xfffffffc); | |
1275 | pci_set_word(pci_dev->wmask + pos + PCI_MSI_DATA_32, 0xffff); | |
1276 | } | |
1277 | /* Expose MSI-X capability */ | |
1278 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_MSIX, 0); | |
1279 | if (pos != 0 && kvm_device_msix_supported(kvm_state)) { | |
1280 | int bar_nr; | |
1281 | uint32_t msix_table_entry; | |
639973a4 | 1282 | uint16_t msix_max; |
c3ebd3ba | 1283 | |
665f119f LE |
1284 | verify_irqchip_in_kernel(&local_err); |
1285 | if (local_err) { | |
64135217 | 1286 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1287 | return -ENOTSUP; |
1288 | } | |
64e184e2 | 1289 | dev->dev.cap_present |= QEMU_PCI_CAP_MSIX; |
c3ebd3ba | 1290 | dev->cap.available |= ASSIGNED_DEVICE_CAP_MSIX; |
42ee4194 LE |
1291 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_MSIX, pos, 12, |
1292 | &local_err); | |
c3ebd3ba | 1293 | if (ret < 0) { |
64135217 | 1294 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1295 | return ret; |
1296 | } | |
1297 | pci_dev->msix_cap = pos; | |
1298 | ||
639973a4 MT |
1299 | msix_max = (pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS) & |
1300 | PCI_MSIX_FLAGS_QSIZE) + 1; | |
1301 | msix_max = MIN(msix_max, KVM_MAX_MSIX_PER_DEV); | |
1302 | pci_set_word(pci_dev->config + pos + PCI_MSIX_FLAGS, msix_max - 1); | |
c3ebd3ba JK |
1303 | |
1304 | /* Only enable and function mask bits are writable */ | |
1305 | pci_set_word(pci_dev->wmask + pos + PCI_MSIX_FLAGS, | |
1306 | PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL); | |
1307 | ||
1308 | msix_table_entry = pci_get_long(pci_dev->config + pos + PCI_MSIX_TABLE); | |
1309 | bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK; | |
1310 | msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK; | |
1311 | dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry; | |
aa1dd39c | 1312 | dev->msix_table_size = msix_max * sizeof(MSIXTableEntry); |
639973a4 | 1313 | dev->msix_max = msix_max; |
c3ebd3ba JK |
1314 | } |
1315 | ||
1316 | /* Minimal PM support, nothing writable, device appears to NAK changes */ | |
1317 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_PM, 0); | |
1318 | if (pos) { | |
1319 | uint16_t pmc; | |
1320 | ||
42ee4194 LE |
1321 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_PM, pos, PCI_PM_SIZEOF, |
1322 | &local_err); | |
c3ebd3ba | 1323 | if (ret < 0) { |
64135217 | 1324 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1325 | return ret; |
1326 | } | |
1327 | ||
1328 | assigned_dev_setup_cap_read(dev, pos, PCI_PM_SIZEOF); | |
1329 | ||
1330 | pmc = pci_get_word(pci_dev->config + pos + PCI_CAP_FLAGS); | |
1331 | pmc &= (PCI_PM_CAP_VER_MASK | PCI_PM_CAP_DSI); | |
1332 | pci_set_word(pci_dev->config + pos + PCI_CAP_FLAGS, pmc); | |
1333 | ||
1334 | /* assign_device will bring the device up to D0, so we don't need | |
1335 | * to worry about doing that ourselves here. */ | |
1336 | pci_set_word(pci_dev->config + pos + PCI_PM_CTRL, | |
1337 | PCI_PM_CTRL_NO_SOFT_RESET); | |
1338 | ||
1339 | pci_set_byte(pci_dev->config + pos + PCI_PM_PPB_EXTENSIONS, 0); | |
1340 | pci_set_byte(pci_dev->config + pos + PCI_PM_DATA_REGISTER, 0); | |
1341 | } | |
1342 | ||
1343 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0); | |
1344 | if (pos) { | |
1345 | uint8_t version, size = 0; | |
1346 | uint16_t type, devctl, lnksta; | |
1347 | uint32_t devcap, lnkcap; | |
1348 | ||
1349 | version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS); | |
1350 | version &= PCI_EXP_FLAGS_VERS; | |
1351 | if (version == 1) { | |
1352 | size = 0x14; | |
1353 | } else if (version == 2) { | |
1354 | /* | |
1355 | * Check for non-std size, accept reduced size to 0x34, | |
1356 | * which is what bcm5761 implemented, violating the | |
1357 | * PCIe v3.0 spec that regs should exist and be read as 0, | |
1358 | * not optionally provided and shorten the struct size. | |
1359 | */ | |
1360 | size = MIN(0x3c, PCI_CONFIG_SPACE_SIZE - pos); | |
1361 | if (size < 0x34) { | |
64135217 LE |
1362 | error_setg(errp, "Invalid size PCIe cap-id 0x%x", |
1363 | PCI_CAP_ID_EXP); | |
c3ebd3ba JK |
1364 | return -EINVAL; |
1365 | } else if (size != 0x3c) { | |
1366 | error_report("WARNING, %s: PCIe cap-id 0x%x has " | |
1367 | "non-standard size 0x%x; std size should be 0x3c", | |
1368 | __func__, PCI_CAP_ID_EXP, size); | |
1369 | } | |
1370 | } else if (version == 0) { | |
1371 | uint16_t vid, did; | |
1372 | vid = pci_get_word(pci_dev->config + PCI_VENDOR_ID); | |
1373 | did = pci_get_word(pci_dev->config + PCI_DEVICE_ID); | |
1374 | if (vid == PCI_VENDOR_ID_INTEL && did == 0x10ed) { | |
1375 | /* | |
1376 | * quirk for Intel 82599 VF with invalid PCIe capability | |
1377 | * version, should really be version 2 (same as PF) | |
1378 | */ | |
1379 | size = 0x3c; | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | if (size == 0) { | |
64135217 LE |
1384 | error_setg(errp, "Unsupported PCI express capability version %d", |
1385 | version); | |
c3ebd3ba JK |
1386 | return -EINVAL; |
1387 | } | |
1388 | ||
42ee4194 LE |
1389 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_EXP, pos, size, |
1390 | &local_err); | |
c3ebd3ba | 1391 | if (ret < 0) { |
64135217 | 1392 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1393 | return ret; |
1394 | } | |
1395 | ||
1396 | assigned_dev_setup_cap_read(dev, pos, size); | |
1397 | ||
1398 | type = pci_get_word(pci_dev->config + pos + PCI_EXP_FLAGS); | |
1399 | type = (type & PCI_EXP_FLAGS_TYPE) >> 4; | |
1400 | if (type != PCI_EXP_TYPE_ENDPOINT && | |
1401 | type != PCI_EXP_TYPE_LEG_END && type != PCI_EXP_TYPE_RC_END) { | |
64135217 LE |
1402 | error_setg(errp, "Device assignment only supports endpoint " |
1403 | "assignment, device type %d", type); | |
c3ebd3ba JK |
1404 | return -EINVAL; |
1405 | } | |
1406 | ||
1407 | /* capabilities, pass existing read-only copy | |
1408 | * PCI_EXP_FLAGS_IRQ: updated by hardware, should be direct read */ | |
1409 | ||
1410 | /* device capabilities: hide FLR */ | |
1411 | devcap = pci_get_long(pci_dev->config + pos + PCI_EXP_DEVCAP); | |
1412 | devcap &= ~PCI_EXP_DEVCAP_FLR; | |
1413 | pci_set_long(pci_dev->config + pos + PCI_EXP_DEVCAP, devcap); | |
1414 | ||
1415 | /* device control: clear all error reporting enable bits, leaving | |
1416 | * only a few host values. Note, these are | |
1417 | * all writable, but not passed to hw. | |
1418 | */ | |
1419 | devctl = pci_get_word(pci_dev->config + pos + PCI_EXP_DEVCTL); | |
1420 | devctl = (devctl & (PCI_EXP_DEVCTL_READRQ | PCI_EXP_DEVCTL_PAYLOAD)) | | |
1421 | PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN; | |
1422 | pci_set_word(pci_dev->config + pos + PCI_EXP_DEVCTL, devctl); | |
1423 | devctl = PCI_EXP_DEVCTL_BCR_FLR | PCI_EXP_DEVCTL_AUX_PME; | |
1424 | pci_set_word(pci_dev->wmask + pos + PCI_EXP_DEVCTL, ~devctl); | |
1425 | ||
1426 | /* Clear device status */ | |
1427 | pci_set_word(pci_dev->config + pos + PCI_EXP_DEVSTA, 0); | |
1428 | ||
1429 | /* Link capabilities, expose links and latencues, clear reporting */ | |
1430 | lnkcap = pci_get_long(pci_dev->config + pos + PCI_EXP_LNKCAP); | |
1431 | lnkcap &= (PCI_EXP_LNKCAP_SLS | PCI_EXP_LNKCAP_MLW | | |
1432 | PCI_EXP_LNKCAP_ASPMS | PCI_EXP_LNKCAP_L0SEL | | |
1433 | PCI_EXP_LNKCAP_L1EL); | |
1434 | pci_set_long(pci_dev->config + pos + PCI_EXP_LNKCAP, lnkcap); | |
1435 | ||
1436 | /* Link control, pass existing read-only copy. Should be writable? */ | |
1437 | ||
1438 | /* Link status, only expose current speed and width */ | |
1439 | lnksta = pci_get_word(pci_dev->config + pos + PCI_EXP_LNKSTA); | |
1440 | lnksta &= (PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW); | |
1441 | pci_set_word(pci_dev->config + pos + PCI_EXP_LNKSTA, lnksta); | |
1442 | ||
1443 | if (version >= 2) { | |
1444 | /* Slot capabilities, control, status - not needed for endpoints */ | |
1445 | pci_set_long(pci_dev->config + pos + PCI_EXP_SLTCAP, 0); | |
1446 | pci_set_word(pci_dev->config + pos + PCI_EXP_SLTCTL, 0); | |
1447 | pci_set_word(pci_dev->config + pos + PCI_EXP_SLTSTA, 0); | |
1448 | ||
1449 | /* Root control, capabilities, status - not needed for endpoints */ | |
1450 | pci_set_word(pci_dev->config + pos + PCI_EXP_RTCTL, 0); | |
1451 | pci_set_word(pci_dev->config + pos + PCI_EXP_RTCAP, 0); | |
1452 | pci_set_long(pci_dev->config + pos + PCI_EXP_RTSTA, 0); | |
1453 | ||
1454 | /* Device capabilities/control 2, pass existing read-only copy */ | |
1455 | /* Link control 2, pass existing read-only copy */ | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_PCIX, 0); | |
1460 | if (pos) { | |
1461 | uint16_t cmd; | |
1462 | uint32_t status; | |
1463 | ||
1464 | /* Only expose the minimum, 8 byte capability */ | |
42ee4194 LE |
1465 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_PCIX, pos, 8, |
1466 | &local_err); | |
c3ebd3ba | 1467 | if (ret < 0) { |
64135217 | 1468 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1469 | return ret; |
1470 | } | |
1471 | ||
1472 | assigned_dev_setup_cap_read(dev, pos, 8); | |
1473 | ||
1474 | /* Command register, clear upper bits, including extended modes */ | |
1475 | cmd = pci_get_word(pci_dev->config + pos + PCI_X_CMD); | |
1476 | cmd &= (PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO | PCI_X_CMD_MAX_READ | | |
1477 | PCI_X_CMD_MAX_SPLIT); | |
1478 | pci_set_word(pci_dev->config + pos + PCI_X_CMD, cmd); | |
1479 | ||
1480 | /* Status register, update with emulated PCI bus location, clear | |
1481 | * error bits, leave the rest. */ | |
1482 | status = pci_get_long(pci_dev->config + pos + PCI_X_STATUS); | |
1483 | status &= ~(PCI_X_STATUS_BUS | PCI_X_STATUS_DEVFN); | |
4a94b3aa | 1484 | status |= pci_get_bdf(pci_dev); |
c3ebd3ba JK |
1485 | status &= ~(PCI_X_STATUS_SPL_DISC | PCI_X_STATUS_UNX_SPL | |
1486 | PCI_X_STATUS_SPL_ERR); | |
1487 | pci_set_long(pci_dev->config + pos + PCI_X_STATUS, status); | |
1488 | } | |
1489 | ||
1490 | pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_VPD, 0); | |
1491 | if (pos) { | |
1492 | /* Direct R/W passthrough */ | |
42ee4194 LE |
1493 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_VPD, pos, 8, |
1494 | &local_err); | |
c3ebd3ba | 1495 | if (ret < 0) { |
64135217 | 1496 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1497 | return ret; |
1498 | } | |
1499 | ||
1500 | assigned_dev_setup_cap_read(dev, pos, 8); | |
1501 | ||
1502 | /* direct write for cap content */ | |
1503 | assigned_dev_direct_config_write(dev, pos + 2, 6); | |
1504 | } | |
1505 | ||
1506 | /* Devices can have multiple vendor capabilities, get them all */ | |
1507 | for (pos = 0; (pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_VNDR, pos)); | |
1508 | pos += PCI_CAP_LIST_NEXT) { | |
1509 | uint8_t len = pci_get_byte(pci_dev->config + pos + PCI_CAP_FLAGS); | |
1510 | /* Direct R/W passthrough */ | |
42ee4194 LE |
1511 | ret = pci_add_capability2(pci_dev, PCI_CAP_ID_VNDR, pos, len, |
1512 | &local_err); | |
c3ebd3ba | 1513 | if (ret < 0) { |
64135217 | 1514 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1515 | return ret; |
1516 | } | |
1517 | ||
1518 | assigned_dev_setup_cap_read(dev, pos, len); | |
1519 | ||
1520 | /* direct write for cap content */ | |
1521 | assigned_dev_direct_config_write(dev, pos + 2, len - 2); | |
1522 | } | |
1523 | ||
1524 | /* If real and virtual capability list status bits differ, virtualize the | |
1525 | * access. */ | |
1526 | if ((pci_get_word(pci_dev->config + PCI_STATUS) & PCI_STATUS_CAP_LIST) != | |
1527 | (assigned_dev_pci_read_byte(pci_dev, PCI_STATUS) & | |
1528 | PCI_STATUS_CAP_LIST)) { | |
1529 | dev->emulate_config_read[PCI_STATUS] |= PCI_STATUS_CAP_LIST; | |
1530 | } | |
1531 | ||
1532 | return 0; | |
1533 | } | |
1534 | ||
1535 | static uint64_t | |
a8170e5e | 1536 | assigned_dev_msix_mmio_read(void *opaque, hwaddr addr, |
c3ebd3ba JK |
1537 | unsigned size) |
1538 | { | |
1539 | AssignedDevice *adev = opaque; | |
1540 | uint64_t val; | |
1541 | ||
1542 | memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size); | |
1543 | ||
1544 | return val; | |
1545 | } | |
1546 | ||
a8170e5e | 1547 | static void assigned_dev_msix_mmio_write(void *opaque, hwaddr addr, |
c3ebd3ba JK |
1548 | uint64_t val, unsigned size) |
1549 | { | |
1550 | AssignedDevice *adev = opaque; | |
1551 | PCIDevice *pdev = &adev->dev; | |
1552 | uint16_t ctrl; | |
1553 | MSIXTableEntry orig; | |
1554 | int i = addr >> 4; | |
1555 | ||
1556 | if (i >= adev->msix_max) { | |
1557 | return; /* Drop write */ | |
1558 | } | |
1559 | ||
1560 | ctrl = pci_get_word(pdev->config + pdev->msix_cap + PCI_MSIX_FLAGS); | |
1561 | ||
1562 | DEBUG("write to MSI-X table offset 0x%lx, val 0x%lx\n", addr, val); | |
1563 | ||
1564 | if (ctrl & PCI_MSIX_FLAGS_ENABLE) { | |
1565 | orig = adev->msix_table[i]; | |
1566 | } | |
1567 | ||
1568 | memcpy((uint8_t *)adev->msix_table + addr, &val, size); | |
1569 | ||
1570 | if (ctrl & PCI_MSIX_FLAGS_ENABLE) { | |
1571 | MSIXTableEntry *entry = &adev->msix_table[i]; | |
1572 | ||
1573 | if (!assigned_dev_msix_masked(&orig) && | |
1574 | assigned_dev_msix_masked(entry)) { | |
1575 | /* | |
1576 | * Vector masked, disable it | |
1577 | * | |
1578 | * XXX It's not clear if we can or should actually attempt | |
1579 | * to mask or disable the interrupt. KVM doesn't have | |
1580 | * support for pending bits and kvm_assign_set_msix_entry | |
1581 | * doesn't modify the device hardware mask. Interrupts | |
1582 | * while masked are simply not injected to the guest, so | |
1583 | * are lost. Can we get away with always injecting an | |
1584 | * interrupt on unmask? | |
1585 | */ | |
1586 | } else if (assigned_dev_msix_masked(&orig) && | |
1587 | !assigned_dev_msix_masked(entry)) { | |
1588 | /* Vector unmasked */ | |
1589 | if (i >= adev->msi_virq_nr || adev->msi_virq[i] < 0) { | |
1590 | /* Previously unassigned vector, start from scratch */ | |
1591 | assigned_dev_update_msix(pdev); | |
1592 | return; | |
1593 | } else { | |
1594 | /* Update an existing, previously masked vector */ | |
1595 | MSIMessage msg; | |
1596 | int ret; | |
1597 | ||
1598 | msg.address = entry->addr_lo | | |
1599 | ((uint64_t)entry->addr_hi << 32); | |
1600 | msg.data = entry->data; | |
1601 | ||
1602 | ret = kvm_irqchip_update_msi_route(kvm_state, | |
dc9f06ca PF |
1603 | adev->msi_virq[i], msg, |
1604 | pdev); | |
c3ebd3ba JK |
1605 | if (ret) { |
1606 | error_report("Error updating irq routing entry (%d)", ret); | |
1607 | } | |
3f1fea0f | 1608 | kvm_irqchip_commit_routes(kvm_state); |
c3ebd3ba JK |
1609 | } |
1610 | } | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | static const MemoryRegionOps assigned_dev_msix_mmio_ops = { | |
1615 | .read = assigned_dev_msix_mmio_read, | |
1616 | .write = assigned_dev_msix_mmio_write, | |
1617 | .endianness = DEVICE_NATIVE_ENDIAN, | |
1618 | .valid = { | |
1619 | .min_access_size = 4, | |
1620 | .max_access_size = 8, | |
1621 | }, | |
1622 | .impl = { | |
1623 | .min_access_size = 4, | |
1624 | .max_access_size = 8, | |
1625 | }, | |
1626 | }; | |
1627 | ||
1628 | static void assigned_dev_msix_reset(AssignedDevice *dev) | |
1629 | { | |
1630 | MSIXTableEntry *entry; | |
1631 | int i; | |
1632 | ||
1633 | if (!dev->msix_table) { | |
1634 | return; | |
1635 | } | |
1636 | ||
aa1dd39c | 1637 | memset(dev->msix_table, 0, dev->msix_table_size); |
c3ebd3ba JK |
1638 | |
1639 | for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) { | |
1640 | entry->ctrl = cpu_to_le32(0x1); /* Masked */ | |
1641 | } | |
1642 | } | |
1643 | ||
7a98593b | 1644 | static void assigned_dev_register_msix_mmio(AssignedDevice *dev, Error **errp) |
c3ebd3ba | 1645 | { |
aa1dd39c IY |
1646 | dev->msix_table = mmap(NULL, dev->msix_table_size, PROT_READ | PROT_WRITE, |
1647 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); | |
c3ebd3ba | 1648 | if (dev->msix_table == MAP_FAILED) { |
7a98593b | 1649 | error_setg_errno(errp, errno, "failed to allocate msix_table"); |
ee11f7a8 | 1650 | dev->msix_table = NULL; |
7a98593b | 1651 | return; |
c3ebd3ba | 1652 | } |
64e184e2 | 1653 | dev->dev.msix_table = (uint8_t *)dev->msix_table; |
c3ebd3ba JK |
1654 | |
1655 | assigned_dev_msix_reset(dev); | |
1656 | ||
1437c94b | 1657 | memory_region_init_io(&dev->mmio, OBJECT(dev), &assigned_dev_msix_mmio_ops, |
aa1dd39c | 1658 | dev, "assigned-dev-msix", dev->msix_table_size); |
c3ebd3ba JK |
1659 | } |
1660 | ||
1661 | static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev) | |
1662 | { | |
1663 | if (!dev->msix_table) { | |
1664 | return; | |
1665 | } | |
1666 | ||
aa1dd39c | 1667 | if (munmap(dev->msix_table, dev->msix_table_size) == -1) { |
c3ebd3ba JK |
1668 | error_report("error unmapping msix_table! %s", strerror(errno)); |
1669 | } | |
1670 | dev->msix_table = NULL; | |
64e184e2 | 1671 | dev->dev.msix_table = NULL; |
c3ebd3ba JK |
1672 | } |
1673 | ||
1674 | static const VMStateDescription vmstate_assigned_device = { | |
1675 | .name = "pci-assign", | |
1676 | .unmigratable = 1, | |
1677 | }; | |
1678 | ||
1679 | static void reset_assigned_device(DeviceState *dev) | |
1680 | { | |
1ea6305a GA |
1681 | PCIDevice *pci_dev = PCI_DEVICE(dev); |
1682 | AssignedDevice *adev = PCI_ASSIGN(pci_dev); | |
c3ebd3ba JK |
1683 | char reset_file[64]; |
1684 | const char reset[] = "1"; | |
1685 | int fd, ret; | |
1686 | ||
1687 | /* | |
1688 | * If a guest is reset without being shutdown, MSI/MSI-X can still | |
1689 | * be running. We want to return the device to a known state on | |
1690 | * reset, so disable those here. We especially do not want MSI-X | |
1691 | * enabled since it lives in MMIO space, which is about to get | |
1692 | * disabled. | |
1693 | */ | |
1694 | if (adev->assigned_irq_type == ASSIGNED_IRQ_MSIX) { | |
1695 | uint16_t ctrl = pci_get_word(pci_dev->config + | |
1696 | pci_dev->msix_cap + PCI_MSIX_FLAGS); | |
1697 | ||
1698 | pci_set_word(pci_dev->config + pci_dev->msix_cap + PCI_MSIX_FLAGS, | |
1699 | ctrl & ~PCI_MSIX_FLAGS_ENABLE); | |
1700 | assigned_dev_update_msix(pci_dev); | |
1701 | } else if (adev->assigned_irq_type == ASSIGNED_IRQ_MSI) { | |
1702 | uint8_t ctrl = pci_get_byte(pci_dev->config + | |
1703 | pci_dev->msi_cap + PCI_MSI_FLAGS); | |
1704 | ||
1705 | pci_set_byte(pci_dev->config + pci_dev->msi_cap + PCI_MSI_FLAGS, | |
1706 | ctrl & ~PCI_MSI_FLAGS_ENABLE); | |
1707 | assigned_dev_update_msi(pci_dev); | |
1708 | } | |
1709 | ||
1710 | snprintf(reset_file, sizeof(reset_file), | |
1711 | "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/reset", | |
1712 | adev->host.domain, adev->host.bus, adev->host.slot, | |
1713 | adev->host.function); | |
1714 | ||
1715 | /* | |
1716 | * Issue a device reset via pci-sysfs. Note that we use write(2) here | |
1717 | * and ignore the return value because some kernels have a bug that | |
1718 | * returns 0 rather than bytes written on success, sending us into an | |
1719 | * infinite retry loop using other write mechanisms. | |
1720 | */ | |
1721 | fd = open(reset_file, O_WRONLY); | |
1722 | if (fd != -1) { | |
1723 | ret = write(fd, reset, strlen(reset)); | |
1724 | (void)ret; | |
1725 | close(fd); | |
1726 | } | |
1727 | ||
1728 | /* | |
1729 | * When a 0 is written to the bus master register, the device is logically | |
1730 | * disconnected from the PCI bus. This avoids further DMA transfers. | |
1731 | */ | |
1732 | assigned_dev_pci_write_config(pci_dev, PCI_COMMAND, 0, 1); | |
1733 | } | |
1734 | ||
d313858d | 1735 | static void assigned_realize(struct PCIDevice *pci_dev, Error **errp) |
c3ebd3ba | 1736 | { |
1ea6305a | 1737 | AssignedDevice *dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1738 | uint8_t e_intx; |
1739 | int r; | |
5b877045 | 1740 | Error *local_err = NULL; |
c3ebd3ba JK |
1741 | |
1742 | if (!kvm_enabled()) { | |
636713ba LE |
1743 | error_setg(&local_err, "pci-assign requires KVM support"); |
1744 | goto exit_with_error; | |
c3ebd3ba JK |
1745 | } |
1746 | ||
1747 | if (!dev->host.domain && !dev->host.bus && !dev->host.slot && | |
1748 | !dev->host.function) { | |
636713ba LE |
1749 | error_setg(&local_err, "no host device specified"); |
1750 | goto exit_with_error; | |
c3ebd3ba JK |
1751 | } |
1752 | ||
1753 | /* | |
1754 | * Set up basic config space access control. Will be further refined during | |
1755 | * device initialization. | |
1756 | */ | |
1757 | assigned_dev_emulate_config_read(dev, 0, PCI_CONFIG_SPACE_SIZE); | |
1758 | assigned_dev_direct_config_read(dev, PCI_STATUS, 2); | |
1759 | assigned_dev_direct_config_read(dev, PCI_REVISION_ID, 1); | |
1760 | assigned_dev_direct_config_read(dev, PCI_CLASS_PROG, 3); | |
1761 | assigned_dev_direct_config_read(dev, PCI_CACHE_LINE_SIZE, 1); | |
1762 | assigned_dev_direct_config_read(dev, PCI_LATENCY_TIMER, 1); | |
1763 | assigned_dev_direct_config_read(dev, PCI_BIST, 1); | |
1764 | assigned_dev_direct_config_read(dev, PCI_CARDBUS_CIS, 4); | |
1765 | assigned_dev_direct_config_read(dev, PCI_SUBSYSTEM_VENDOR_ID, 2); | |
1766 | assigned_dev_direct_config_read(dev, PCI_SUBSYSTEM_ID, 2); | |
1767 | assigned_dev_direct_config_read(dev, PCI_CAPABILITY_LIST + 1, 7); | |
1768 | assigned_dev_direct_config_read(dev, PCI_MIN_GNT, 1); | |
1769 | assigned_dev_direct_config_read(dev, PCI_MAX_LAT, 1); | |
1770 | memcpy(dev->emulate_config_write, dev->emulate_config_read, | |
1771 | sizeof(dev->emulate_config_read)); | |
1772 | ||
5b877045 LE |
1773 | get_real_device(dev, &local_err); |
1774 | if (local_err) { | |
c3ebd3ba JK |
1775 | goto out; |
1776 | } | |
1777 | ||
64135217 | 1778 | if (assigned_device_pci_cap_init(pci_dev, &local_err) < 0) { |
c3ebd3ba JK |
1779 | goto out; |
1780 | } | |
1781 | ||
1782 | /* intercept MSI-X entry page in the MMIO */ | |
1783 | if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) { | |
7a98593b LE |
1784 | assigned_dev_register_msix_mmio(dev, &local_err); |
1785 | if (local_err) { | |
c3ebd3ba JK |
1786 | goto out; |
1787 | } | |
1788 | } | |
1789 | ||
1790 | /* handle real device's MMIO/PIO BARs */ | |
7d9cb533 LE |
1791 | assigned_dev_register_regions(dev->real_device.regions, |
1792 | dev->real_device.region_number, dev, | |
1793 | &local_err); | |
1794 | if (local_err) { | |
c3ebd3ba JK |
1795 | goto out; |
1796 | } | |
1797 | ||
1798 | /* handle interrupt routing */ | |
1799 | e_intx = dev->dev.config[PCI_INTERRUPT_PIN] - 1; | |
1800 | dev->intpin = e_intx; | |
1801 | dev->intx_route.mode = PCI_INTX_DISABLED; | |
1802 | dev->intx_route.irq = -1; | |
1803 | ||
1804 | /* assign device to guest */ | |
6877cff0 LE |
1805 | assign_device(dev, &local_err); |
1806 | if (local_err) { | |
c3ebd3ba JK |
1807 | goto out; |
1808 | } | |
1809 | ||
1810 | /* assign legacy INTx to the device */ | |
ef47827a | 1811 | r = assign_intx(dev, &local_err); |
c3ebd3ba JK |
1812 | if (r < 0) { |
1813 | goto assigned_out; | |
1814 | } | |
1815 | ||
1816 | assigned_dev_load_option_rom(dev); | |
1817 | ||
d313858d | 1818 | return; |
c3ebd3ba JK |
1819 | |
1820 | assigned_out: | |
1821 | deassign_device(dev); | |
636713ba | 1822 | |
c3ebd3ba JK |
1823 | out: |
1824 | free_assigned_device(dev); | |
636713ba LE |
1825 | |
1826 | exit_with_error: | |
1827 | assert(local_err); | |
d313858d | 1828 | error_propagate(errp, local_err); |
c3ebd3ba JK |
1829 | } |
1830 | ||
1831 | static void assigned_exitfn(struct PCIDevice *pci_dev) | |
1832 | { | |
1ea6305a | 1833 | AssignedDevice *dev = PCI_ASSIGN(pci_dev); |
c3ebd3ba JK |
1834 | |
1835 | deassign_device(dev); | |
1836 | free_assigned_device(dev); | |
1837 | } | |
1838 | ||
7f6014af GA |
1839 | static void assigned_dev_instance_init(Object *obj) |
1840 | { | |
1841 | PCIDevice *pci_dev = PCI_DEVICE(obj); | |
1ea6305a | 1842 | AssignedDevice *d = PCI_ASSIGN(pci_dev); |
7f6014af GA |
1843 | |
1844 | device_add_bootindex_property(obj, &d->bootindex, | |
1845 | "bootindex", NULL, | |
1846 | &pci_dev->qdev, NULL); | |
1847 | } | |
1848 | ||
c3ebd3ba JK |
1849 | static Property assigned_dev_properties[] = { |
1850 | DEFINE_PROP_PCI_HOST_DEVADDR("host", AssignedDevice, host), | |
1851 | DEFINE_PROP_BIT("prefer_msi", AssignedDevice, features, | |
1852 | ASSIGNED_DEVICE_PREFER_MSI_BIT, false), | |
1853 | DEFINE_PROP_BIT("share_intx", AssignedDevice, features, | |
1854 | ASSIGNED_DEVICE_SHARE_INTX_BIT, true), | |
c3ebd3ba JK |
1855 | DEFINE_PROP_STRING("configfd", AssignedDevice, configfd_name), |
1856 | DEFINE_PROP_END_OF_LIST(), | |
1857 | }; | |
1858 | ||
1859 | static void assign_class_init(ObjectClass *klass, void *data) | |
1860 | { | |
1861 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1862 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1863 | ||
d313858d | 1864 | k->realize = assigned_realize; |
c3ebd3ba JK |
1865 | k->exit = assigned_exitfn; |
1866 | k->config_read = assigned_dev_pci_read_config; | |
1867 | k->config_write = assigned_dev_pci_write_config; | |
1868 | dc->props = assigned_dev_properties; | |
1869 | dc->vmsd = &vmstate_assigned_device; | |
1870 | dc->reset = reset_assigned_device; | |
125ee0ed | 1871 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
c3ebd3ba JK |
1872 | dc->desc = "KVM-based PCI passthrough"; |
1873 | } | |
1874 | ||
1875 | static const TypeInfo assign_info = { | |
1ea6305a | 1876 | .name = TYPE_PCI_ASSIGN, |
c3ebd3ba JK |
1877 | .parent = TYPE_PCI_DEVICE, |
1878 | .instance_size = sizeof(AssignedDevice), | |
1879 | .class_init = assign_class_init, | |
7f6014af | 1880 | .instance_init = assigned_dev_instance_init, |
c3ebd3ba JK |
1881 | }; |
1882 | ||
1883 | static void assign_register_types(void) | |
1884 | { | |
1885 | type_register_static(&assign_info); | |
1886 | } | |
1887 | ||
1888 | type_init(assign_register_types) | |
1889 | ||
c3ebd3ba JK |
1890 | static void assigned_dev_load_option_rom(AssignedDevice *dev) |
1891 | { | |
bcd7461e | 1892 | int size = 0; |
c3ebd3ba | 1893 | |
bcd7461e TC |
1894 | pci_assign_dev_load_option_rom(&dev->dev, OBJECT(dev), &size, |
1895 | dev->host.domain, dev->host.bus, | |
1896 | dev->host.slot, dev->host.function); | |
c3ebd3ba | 1897 | } |