Add Error **errp for xen_pt_setup_vga()
[qemu/ar7.git] / hw / xen / xen_pt.c
blob07bfcecb639f0496998243fcf86f96c2360fc00f
1 /*
2 * Copyright (c) 2007, Neocleus Corporation.
3 * Copyright (c) 2007, Intel Corporation.
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
8 * Alex Novik <alex@neocleus.com>
9 * Allen Kay <allen.m.kay@intel.com>
10 * Guy Zana <guy@neocleus.com>
12 * This file implements direct PCI assignment to a HVM guest
16 * Interrupt Disable policy:
18 * INTx interrupt:
19 * Initialize(register_real_device)
20 * Map INTx(xc_physdev_map_pirq):
21 * <fail>
22 * - Set real Interrupt Disable bit to '1'.
23 * - Set machine_irq and assigned_device->machine_irq to '0'.
24 * * Don't bind INTx.
26 * Bind INTx(xc_domain_bind_pt_pci_irq):
27 * <fail>
28 * - Set real Interrupt Disable bit to '1'.
29 * - Unmap INTx.
30 * - Decrement xen_pt_mapped_machine_irq[machine_irq]
31 * - Set assigned_device->machine_irq to '0'.
33 * Write to Interrupt Disable bit by guest software(xen_pt_cmd_reg_write)
34 * Write '0'
35 * - Set real bit to '0' if assigned_device->machine_irq isn't '0'.
37 * Write '1'
38 * - Set real bit to '1'.
40 * MSI interrupt:
41 * Initialize MSI register(xen_pt_msi_setup, xen_pt_msi_update)
42 * Bind MSI(xc_domain_update_msi_irq)
43 * <fail>
44 * - Unmap MSI.
45 * - Set dev->msi->pirq to '-1'.
47 * MSI-X interrupt:
48 * Initialize MSI-X register(xen_pt_msix_update_one)
49 * Bind MSI-X(xc_domain_update_msi_irq)
50 * <fail>
51 * - Unmap MSI-X.
52 * - Set entry->pirq to '-1'.
55 #include <sys/ioctl.h>
57 #include "hw/pci/pci.h"
58 #include "hw/xen/xen.h"
59 #include "hw/i386/pc.h"
60 #include "hw/xen/xen_backend.h"
61 #include "xen_pt.h"
62 #include "qemu/range.h"
63 #include "exec/address-spaces.h"
65 #define XEN_PT_NR_IRQS (256)
66 static uint8_t xen_pt_mapped_machine_irq[XEN_PT_NR_IRQS] = {0};
68 void xen_pt_log(const PCIDevice *d, const char *f, ...)
70 va_list ap;
72 va_start(ap, f);
73 if (d) {
74 fprintf(stderr, "[%02x:%02x.%d] ", pci_bus_num(d->bus),
75 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
77 vfprintf(stderr, f, ap);
78 va_end(ap);
81 /* Config Space */
83 static int xen_pt_pci_config_access_check(PCIDevice *d, uint32_t addr, int len)
85 /* check offset range */
86 if (addr >= 0xFF) {
87 XEN_PT_ERR(d, "Failed to access register with offset exceeding 0xFF. "
88 "(addr: 0x%02x, len: %d)\n", addr, len);
89 return -1;
92 /* check read size */
93 if ((len != 1) && (len != 2) && (len != 4)) {
94 XEN_PT_ERR(d, "Failed to access register with invalid access length. "
95 "(addr: 0x%02x, len: %d)\n", addr, len);
96 return -1;
99 /* check offset alignment */
100 if (addr & (len - 1)) {
101 XEN_PT_ERR(d, "Failed to access register with invalid access size "
102 "alignment. (addr: 0x%02x, len: %d)\n", addr, len);
103 return -1;
106 return 0;
109 int xen_pt_bar_offset_to_index(uint32_t offset)
111 int index = 0;
113 /* check Exp ROM BAR */
114 if (offset == PCI_ROM_ADDRESS) {
115 return PCI_ROM_SLOT;
118 /* calculate BAR index */
119 index = (offset - PCI_BASE_ADDRESS_0) >> 2;
120 if (index >= PCI_NUM_REGIONS) {
121 return -1;
124 return index;
127 static uint32_t xen_pt_pci_read_config(PCIDevice *d, uint32_t addr, int len)
129 XenPCIPassthroughState *s = XEN_PT_DEVICE(d);
130 uint32_t val = 0;
131 XenPTRegGroup *reg_grp_entry = NULL;
132 XenPTReg *reg_entry = NULL;
133 int rc = 0;
134 int emul_len = 0;
135 uint32_t find_addr = addr;
137 if (xen_pt_pci_config_access_check(d, addr, len)) {
138 goto exit;
141 /* find register group entry */
142 reg_grp_entry = xen_pt_find_reg_grp(s, addr);
143 if (reg_grp_entry) {
144 /* check 0-Hardwired register group */
145 if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) {
146 /* no need to emulate, just return 0 */
147 val = 0;
148 goto exit;
152 /* read I/O device register value */
153 rc = xen_host_pci_get_block(&s->real_device, addr, (uint8_t *)&val, len);
154 if (rc < 0) {
155 XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc);
156 memset(&val, 0xff, len);
159 /* just return the I/O device register value for
160 * passthrough type register group */
161 if (reg_grp_entry == NULL) {
162 goto exit;
165 /* adjust the read value to appropriate CFC-CFF window */
166 val <<= (addr & 3) << 3;
167 emul_len = len;
169 /* loop around the guest requested size */
170 while (emul_len > 0) {
171 /* find register entry to be emulated */
172 reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr);
173 if (reg_entry) {
174 XenPTRegInfo *reg = reg_entry->reg;
175 uint32_t real_offset = reg_grp_entry->base_offset + reg->offset;
176 uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3);
177 uint8_t *ptr_val = NULL;
179 valid_mask <<= (find_addr - real_offset) << 3;
180 ptr_val = (uint8_t *)&val + (real_offset & 3);
182 /* do emulation based on register size */
183 switch (reg->size) {
184 case 1:
185 if (reg->u.b.read) {
186 rc = reg->u.b.read(s, reg_entry, ptr_val, valid_mask);
188 break;
189 case 2:
190 if (reg->u.w.read) {
191 rc = reg->u.w.read(s, reg_entry,
192 (uint16_t *)ptr_val, valid_mask);
194 break;
195 case 4:
196 if (reg->u.dw.read) {
197 rc = reg->u.dw.read(s, reg_entry,
198 (uint32_t *)ptr_val, valid_mask);
200 break;
203 if (rc < 0) {
204 xen_shutdown_fatal_error("Internal error: Invalid read "
205 "emulation. (%s, rc: %d)\n",
206 __func__, rc);
207 return 0;
210 /* calculate next address to find */
211 emul_len -= reg->size;
212 if (emul_len > 0) {
213 find_addr = real_offset + reg->size;
215 } else {
216 /* nothing to do with passthrough type register,
217 * continue to find next byte */
218 emul_len--;
219 find_addr++;
223 /* need to shift back before returning them to pci bus emulator */
224 val >>= ((addr & 3) << 3);
226 exit:
227 XEN_PT_LOG_CONFIG(d, addr, val, len);
228 return val;
231 static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
232 uint32_t val, int len)
234 XenPCIPassthroughState *s = XEN_PT_DEVICE(d);
235 int index = 0;
236 XenPTRegGroup *reg_grp_entry = NULL;
237 int rc = 0;
238 uint32_t read_val = 0, wb_mask;
239 int emul_len = 0;
240 XenPTReg *reg_entry = NULL;
241 uint32_t find_addr = addr;
242 XenPTRegInfo *reg = NULL;
243 bool wp_flag = false;
245 if (xen_pt_pci_config_access_check(d, addr, len)) {
246 return;
249 XEN_PT_LOG_CONFIG(d, addr, val, len);
251 /* check unused BAR register */
252 index = xen_pt_bar_offset_to_index(addr);
253 if ((index >= 0) && (val != 0)) {
254 uint32_t chk = val;
256 if (index == PCI_ROM_SLOT)
257 chk |= (uint32_t)~PCI_ROM_ADDRESS_MASK;
259 if ((chk != XEN_PT_BAR_ALLF) &&
260 (s->bases[index].bar_flag == XEN_PT_BAR_FLAG_UNUSED)) {
261 XEN_PT_WARN(d, "Guest attempt to set address to unused "
262 "Base Address Register. (addr: 0x%02x, len: %d)\n",
263 addr, len);
267 /* find register group entry */
268 reg_grp_entry = xen_pt_find_reg_grp(s, addr);
269 if (reg_grp_entry) {
270 /* check 0-Hardwired register group */
271 if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) {
272 /* ignore silently */
273 XEN_PT_WARN(d, "Access to 0-Hardwired register. "
274 "(addr: 0x%02x, len: %d)\n", addr, len);
275 return;
279 rc = xen_host_pci_get_block(&s->real_device, addr,
280 (uint8_t *)&read_val, len);
281 if (rc < 0) {
282 XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc);
283 memset(&read_val, 0xff, len);
284 wb_mask = 0;
285 } else {
286 wb_mask = 0xFFFFFFFF >> ((4 - len) << 3);
289 /* pass directly to the real device for passthrough type register group */
290 if (reg_grp_entry == NULL) {
291 if (!s->permissive) {
292 wb_mask = 0;
293 wp_flag = true;
295 goto out;
298 memory_region_transaction_begin();
299 pci_default_write_config(d, addr, val, len);
301 /* adjust the read and write value to appropriate CFC-CFF window */
302 read_val <<= (addr & 3) << 3;
303 val <<= (addr & 3) << 3;
304 emul_len = len;
306 /* loop around the guest requested size */
307 while (emul_len > 0) {
308 /* find register entry to be emulated */
309 reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr);
310 if (reg_entry) {
311 reg = reg_entry->reg;
312 uint32_t real_offset = reg_grp_entry->base_offset + reg->offset;
313 uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3);
314 uint8_t *ptr_val = NULL;
315 uint32_t wp_mask = reg->emu_mask | reg->ro_mask;
317 valid_mask <<= (find_addr - real_offset) << 3;
318 ptr_val = (uint8_t *)&val + (real_offset & 3);
319 if (!s->permissive) {
320 wp_mask |= reg->res_mask;
322 if (wp_mask == (0xFFFFFFFF >> ((4 - reg->size) << 3))) {
323 wb_mask &= ~((wp_mask >> ((find_addr - real_offset) << 3))
324 << ((len - emul_len) << 3));
327 /* do emulation based on register size */
328 switch (reg->size) {
329 case 1:
330 if (reg->u.b.write) {
331 rc = reg->u.b.write(s, reg_entry, ptr_val,
332 read_val >> ((real_offset & 3) << 3),
333 valid_mask);
335 break;
336 case 2:
337 if (reg->u.w.write) {
338 rc = reg->u.w.write(s, reg_entry, (uint16_t *)ptr_val,
339 (read_val >> ((real_offset & 3) << 3)),
340 valid_mask);
342 break;
343 case 4:
344 if (reg->u.dw.write) {
345 rc = reg->u.dw.write(s, reg_entry, (uint32_t *)ptr_val,
346 (read_val >> ((real_offset & 3) << 3)),
347 valid_mask);
349 break;
352 if (rc < 0) {
353 xen_shutdown_fatal_error("Internal error: Invalid write"
354 " emulation. (%s, rc: %d)\n",
355 __func__, rc);
356 return;
359 /* calculate next address to find */
360 emul_len -= reg->size;
361 if (emul_len > 0) {
362 find_addr = real_offset + reg->size;
364 } else {
365 /* nothing to do with passthrough type register,
366 * continue to find next byte */
367 if (!s->permissive) {
368 wb_mask &= ~(0xff << ((len - emul_len) << 3));
369 /* Unused BARs will make it here, but we don't want to issue
370 * warnings for writes to them (bogus writes get dealt with
371 * above).
373 if (index < 0) {
374 wp_flag = true;
377 emul_len--;
378 find_addr++;
382 /* need to shift back before passing them to xen_host_pci_set_block. */
383 val >>= (addr & 3) << 3;
385 memory_region_transaction_commit();
387 out:
388 if (wp_flag && !s->permissive_warned) {
389 s->permissive_warned = true;
390 xen_pt_log(d, "Write-back to unknown field 0x%02x (partially) inhibited (0x%0*x)\n",
391 addr, len * 2, wb_mask);
392 xen_pt_log(d, "If the device doesn't work, try enabling permissive mode\n");
393 xen_pt_log(d, "(unsafe) and if it helps report the problem to xen-devel\n");
395 for (index = 0; wb_mask; index += len) {
396 /* unknown regs are passed through */
397 while (!(wb_mask & 0xff)) {
398 index++;
399 wb_mask >>= 8;
401 len = 0;
402 do {
403 len++;
404 wb_mask >>= 8;
405 } while (wb_mask & 0xff);
406 rc = xen_host_pci_set_block(&s->real_device, addr + index,
407 (uint8_t *)&val + index, len);
409 if (rc < 0) {
410 XEN_PT_ERR(d, "xen_host_pci_set_block failed. return value: %d.\n", rc);
415 /* register regions */
417 static uint64_t xen_pt_bar_read(void *o, hwaddr addr,
418 unsigned size)
420 PCIDevice *d = o;
421 /* if this function is called, that probably means that there is a
422 * misconfiguration of the IOMMU. */
423 XEN_PT_ERR(d, "Should not read BAR through QEMU. @0x"TARGET_FMT_plx"\n",
424 addr);
425 return 0;
427 static void xen_pt_bar_write(void *o, hwaddr addr, uint64_t val,
428 unsigned size)
430 PCIDevice *d = o;
431 /* Same comment as xen_pt_bar_read function */
432 XEN_PT_ERR(d, "Should not write BAR through QEMU. @0x"TARGET_FMT_plx"\n",
433 addr);
436 static const MemoryRegionOps ops = {
437 .endianness = DEVICE_NATIVE_ENDIAN,
438 .read = xen_pt_bar_read,
439 .write = xen_pt_bar_write,
442 static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)
444 int i = 0;
445 XenHostPCIDevice *d = &s->real_device;
447 /* Register PIO/MMIO BARs */
448 for (i = 0; i < PCI_ROM_SLOT; i++) {
449 XenHostPCIIORegion *r = &d->io_regions[i];
450 uint8_t type;
452 if (r->base_addr == 0 || r->size == 0) {
453 continue;
456 s->bases[i].access.u = r->base_addr;
458 if (r->type & XEN_HOST_PCI_REGION_TYPE_IO) {
459 type = PCI_BASE_ADDRESS_SPACE_IO;
460 *cmd |= PCI_COMMAND_IO;
461 } else {
462 type = PCI_BASE_ADDRESS_SPACE_MEMORY;
463 if (r->type & XEN_HOST_PCI_REGION_TYPE_PREFETCH) {
464 type |= PCI_BASE_ADDRESS_MEM_PREFETCH;
466 if (r->type & XEN_HOST_PCI_REGION_TYPE_MEM_64) {
467 type |= PCI_BASE_ADDRESS_MEM_TYPE_64;
469 *cmd |= PCI_COMMAND_MEMORY;
472 memory_region_init_io(&s->bar[i], OBJECT(s), &ops, &s->dev,
473 "xen-pci-pt-bar", r->size);
474 pci_register_bar(&s->dev, i, type, &s->bar[i]);
476 XEN_PT_LOG(&s->dev, "IO region %i registered (size=0x%08"PRIx64
477 " base_addr=0x%08"PRIx64" type: %#x)\n",
478 i, r->size, r->base_addr, type);
481 /* Register expansion ROM address */
482 if (d->rom.base_addr && d->rom.size) {
483 uint32_t bar_data = 0;
485 /* Re-set BAR reported by OS, otherwise ROM can't be read. */
486 if (xen_host_pci_get_long(d, PCI_ROM_ADDRESS, &bar_data)) {
487 return 0;
489 if ((bar_data & PCI_ROM_ADDRESS_MASK) == 0) {
490 bar_data |= d->rom.base_addr & PCI_ROM_ADDRESS_MASK;
491 xen_host_pci_set_long(d, PCI_ROM_ADDRESS, bar_data);
494 s->bases[PCI_ROM_SLOT].access.maddr = d->rom.base_addr;
496 memory_region_init_io(&s->rom, OBJECT(s), &ops, &s->dev,
497 "xen-pci-pt-rom", d->rom.size);
498 pci_register_bar(&s->dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_MEM_PREFETCH,
499 &s->rom);
501 XEN_PT_LOG(&s->dev, "Expansion ROM registered (size=0x%08"PRIx64
502 " base_addr=0x%08"PRIx64")\n",
503 d->rom.size, d->rom.base_addr);
506 xen_pt_register_vga_regions(d);
507 return 0;
510 /* region mapping */
512 static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
514 int i = 0;
516 for (i = 0; i < PCI_NUM_REGIONS - 1; i++) {
517 if (mr == &s->bar[i]) {
518 return i;
521 if (mr == &s->rom) {
522 return PCI_ROM_SLOT;
524 return -1;
528 * This function checks if an io_region overlaps an io_region from another
529 * device. The io_region to check is provided with (addr, size and type)
530 * A callback can be provided and will be called for every region that is
531 * overlapped.
532 * The return value indicates if the region is overlappsed */
533 struct CheckBarArgs {
534 XenPCIPassthroughState *s;
535 pcibus_t addr;
536 pcibus_t size;
537 uint8_t type;
538 bool rc;
540 static void xen_pt_check_bar_overlap(PCIBus *bus, PCIDevice *d, void *opaque)
542 struct CheckBarArgs *arg = opaque;
543 XenPCIPassthroughState *s = arg->s;
544 uint8_t type = arg->type;
545 int i;
547 if (d->devfn == s->dev.devfn) {
548 return;
551 /* xxx: This ignores bridges. */
552 for (i = 0; i < PCI_NUM_REGIONS; i++) {
553 const PCIIORegion *r = &d->io_regions[i];
555 if (!r->size) {
556 continue;
558 if ((type & PCI_BASE_ADDRESS_SPACE_IO)
559 != (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
560 continue;
563 if (ranges_overlap(arg->addr, arg->size, r->addr, r->size)) {
564 XEN_PT_WARN(&s->dev,
565 "Overlapped to device [%02x:%02x.%d] Region: %i"
566 " (addr: %#"FMT_PCIBUS", len: %#"FMT_PCIBUS")\n",
567 pci_bus_num(bus), PCI_SLOT(d->devfn),
568 PCI_FUNC(d->devfn), i, r->addr, r->size);
569 arg->rc = true;
574 static void xen_pt_region_update(XenPCIPassthroughState *s,
575 MemoryRegionSection *sec, bool adding)
577 PCIDevice *d = &s->dev;
578 MemoryRegion *mr = sec->mr;
579 int bar = -1;
580 int rc;
581 int op = adding ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING;
582 struct CheckBarArgs args = {
583 .s = s,
584 .addr = sec->offset_within_address_space,
585 .size = int128_get64(sec->size),
586 .rc = false,
589 bar = xen_pt_bar_from_region(s, mr);
590 if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
591 return;
594 if (s->msix && &s->msix->mmio == mr) {
595 if (adding) {
596 s->msix->mmio_base_addr = sec->offset_within_address_space;
597 rc = xen_pt_msix_update_remap(s, s->msix->bar_index);
599 return;
602 args.type = d->io_regions[bar].type;
603 pci_for_each_device(d->bus, pci_bus_num(d->bus),
604 xen_pt_check_bar_overlap, &args);
605 if (args.rc) {
606 XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS
607 ", len: %#"FMT_PCIBUS") is overlapped.\n",
608 bar, sec->offset_within_address_space,
609 int128_get64(sec->size));
612 if (d->io_regions[bar].type & PCI_BASE_ADDRESS_SPACE_IO) {
613 uint32_t guest_port = sec->offset_within_address_space;
614 uint32_t machine_port = s->bases[bar].access.pio_base;
615 uint32_t size = int128_get64(sec->size);
616 rc = xc_domain_ioport_mapping(xen_xc, xen_domid,
617 guest_port, machine_port, size,
618 op);
619 if (rc) {
620 XEN_PT_ERR(d, "%s ioport mapping failed! (err: %i)\n",
621 adding ? "create new" : "remove old", errno);
623 } else {
624 pcibus_t guest_addr = sec->offset_within_address_space;
625 pcibus_t machine_addr = s->bases[bar].access.maddr
626 + sec->offset_within_region;
627 pcibus_t size = int128_get64(sec->size);
628 rc = xc_domain_memory_mapping(xen_xc, xen_domid,
629 XEN_PFN(guest_addr + XC_PAGE_SIZE - 1),
630 XEN_PFN(machine_addr + XC_PAGE_SIZE - 1),
631 XEN_PFN(size + XC_PAGE_SIZE - 1),
632 op);
633 if (rc) {
634 XEN_PT_ERR(d, "%s mem mapping failed! (err: %i)\n",
635 adding ? "create new" : "remove old", errno);
640 static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
642 XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
643 memory_listener);
645 memory_region_ref(sec->mr);
646 xen_pt_region_update(s, sec, true);
649 static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
651 XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
652 memory_listener);
654 xen_pt_region_update(s, sec, false);
655 memory_region_unref(sec->mr);
658 static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
660 XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
661 io_listener);
663 memory_region_ref(sec->mr);
664 xen_pt_region_update(s, sec, true);
667 static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
669 XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
670 io_listener);
672 xen_pt_region_update(s, sec, false);
673 memory_region_unref(sec->mr);
676 static const MemoryListener xen_pt_memory_listener = {
677 .region_add = xen_pt_region_add,
678 .region_del = xen_pt_region_del,
679 .priority = 10,
682 static const MemoryListener xen_pt_io_listener = {
683 .region_add = xen_pt_io_region_add,
684 .region_del = xen_pt_io_region_del,
685 .priority = 10,
688 static void
689 xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState *s,
690 XenHostPCIDevice *dev)
692 uint16_t gpu_dev_id;
693 PCIDevice *d = &s->dev;
695 gpu_dev_id = dev->device_id;
696 igd_passthrough_isa_bridge_create(d->bus, gpu_dev_id);
699 /* destroy. */
700 static void xen_pt_destroy(PCIDevice *d) {
702 XenPCIPassthroughState *s = XEN_PT_DEVICE(d);
703 XenHostPCIDevice *host_dev = &s->real_device;
704 uint8_t machine_irq = s->machine_irq;
705 uint8_t intx;
706 int rc;
708 if (machine_irq && !xen_host_pci_device_closed(&s->real_device)) {
709 intx = xen_pt_pci_intx(s);
710 rc = xc_domain_unbind_pt_irq(xen_xc, xen_domid, machine_irq,
711 PT_IRQ_TYPE_PCI,
712 pci_bus_num(d->bus),
713 PCI_SLOT(s->dev.devfn),
714 intx,
715 0 /* isa_irq */);
716 if (rc < 0) {
717 XEN_PT_ERR(d, "unbinding of interrupt INT%c failed."
718 " (machine irq: %i, err: %d)"
719 " But bravely continuing on..\n",
720 'a' + intx, machine_irq, errno);
724 /* N.B. xen_pt_config_delete takes care of freeing them. */
725 if (s->msi) {
726 xen_pt_msi_disable(s);
728 if (s->msix) {
729 xen_pt_msix_disable(s);
732 if (machine_irq) {
733 xen_pt_mapped_machine_irq[machine_irq]--;
735 if (xen_pt_mapped_machine_irq[machine_irq] == 0) {
736 rc = xc_physdev_unmap_pirq(xen_xc, xen_domid, machine_irq);
738 if (rc < 0) {
739 XEN_PT_ERR(d, "unmapping of interrupt %i failed. (err: %d)"
740 " But bravely continuing on..\n",
741 machine_irq, errno);
744 s->machine_irq = 0;
747 /* delete all emulated config registers */
748 xen_pt_config_delete(s);
750 xen_pt_unregister_vga_regions(host_dev);
752 if (s->listener_set) {
753 memory_listener_unregister(&s->memory_listener);
754 memory_listener_unregister(&s->io_listener);
755 s->listener_set = false;
757 if (!xen_host_pci_device_closed(&s->real_device)) {
758 xen_host_pci_device_put(&s->real_device);
761 /* init */
763 static int xen_pt_initfn(PCIDevice *d)
765 XenPCIPassthroughState *s = XEN_PT_DEVICE(d);
766 int rc = 0;
767 uint8_t machine_irq = 0, scratch;
768 uint16_t cmd = 0;
769 int pirq = XEN_PT_UNASSIGNED_PIRQ;
770 Error *err = NULL;
772 /* register real device */
773 XEN_PT_LOG(d, "Assigning real physical device %02x:%02x.%d"
774 " to devfn %#x\n",
775 s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function,
776 s->dev.devfn);
778 xen_host_pci_device_get(&s->real_device,
779 s->hostaddr.domain, s->hostaddr.bus,
780 s->hostaddr.slot, s->hostaddr.function,
781 &err);
782 if (err) {
783 error_append_hint(&err, "Failed to \"open\" the real pci device");
784 error_report_err(err);
785 return -1;
788 s->is_virtfn = s->real_device.is_virtfn;
789 if (s->is_virtfn) {
790 XEN_PT_LOG(d, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n",
791 s->real_device.domain, s->real_device.bus,
792 s->real_device.dev, s->real_device.func);
795 /* Initialize virtualized PCI configuration (Extended 256 Bytes) */
796 memset(d->config, 0, PCI_CONFIG_SPACE_SIZE);
798 s->memory_listener = xen_pt_memory_listener;
799 s->io_listener = xen_pt_io_listener;
801 /* Setup VGA bios for passthrough GFX */
802 if ((s->real_device.domain == 0) && (s->real_device.bus == 0) &&
803 (s->real_device.dev == 2) && (s->real_device.func == 0)) {
804 if (!is_igd_vga_passthrough(&s->real_device)) {
805 XEN_PT_ERR(d, "Need to enable igd-passthru if you're trying"
806 " to passthrough IGD GFX.\n");
807 xen_host_pci_device_put(&s->real_device);
808 return -1;
811 xen_pt_setup_vga(s, &s->real_device, &err);
812 if (err) {
813 error_append_hint(&err, "Setup VGA BIOS of passthrough"
814 " GFX failed");
815 error_report_err(err);
816 xen_host_pci_device_put(&s->real_device);
817 return -1;
820 /* Register ISA bridge for passthrough GFX. */
821 xen_igd_passthrough_isa_bridge_create(s, &s->real_device);
824 /* Handle real device's MMIO/PIO BARs */
825 xen_pt_register_regions(s, &cmd);
827 /* reinitialize each config register to be emulated */
828 rc = xen_pt_config_init(s);
829 if (rc) {
830 XEN_PT_ERR(d, "PCI Config space initialisation failed.\n");
831 goto err_out;
834 /* Bind interrupt */
835 rc = xen_host_pci_get_byte(&s->real_device, PCI_INTERRUPT_PIN, &scratch);
836 if (rc) {
837 XEN_PT_ERR(d, "Failed to read PCI_INTERRUPT_PIN! (rc:%d)\n", rc);
838 goto err_out;
840 if (!scratch) {
841 XEN_PT_LOG(d, "no pin interrupt\n");
842 goto out;
845 machine_irq = s->real_device.irq;
846 rc = xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq);
848 if (rc < 0) {
849 XEN_PT_ERR(d, "Mapping machine irq %u to pirq %i failed, (err: %d)\n",
850 machine_irq, pirq, errno);
852 /* Disable PCI intx assertion (turn on bit10 of devctl) */
853 cmd |= PCI_COMMAND_INTX_DISABLE;
854 machine_irq = 0;
855 s->machine_irq = 0;
856 } else {
857 machine_irq = pirq;
858 s->machine_irq = pirq;
859 xen_pt_mapped_machine_irq[machine_irq]++;
862 /* bind machine_irq to device */
863 if (machine_irq != 0) {
864 uint8_t e_intx = xen_pt_pci_intx(s);
866 rc = xc_domain_bind_pt_pci_irq(xen_xc, xen_domid, machine_irq,
867 pci_bus_num(d->bus),
868 PCI_SLOT(d->devfn),
869 e_intx);
870 if (rc < 0) {
871 XEN_PT_ERR(d, "Binding of interrupt %i failed! (err: %d)\n",
872 e_intx, errno);
874 /* Disable PCI intx assertion (turn on bit10 of devctl) */
875 cmd |= PCI_COMMAND_INTX_DISABLE;
876 xen_pt_mapped_machine_irq[machine_irq]--;
878 if (xen_pt_mapped_machine_irq[machine_irq] == 0) {
879 if (xc_physdev_unmap_pirq(xen_xc, xen_domid, machine_irq)) {
880 XEN_PT_ERR(d, "Unmapping of machine interrupt %i failed!"
881 " (err: %d)\n", machine_irq, errno);
884 s->machine_irq = 0;
888 out:
889 if (cmd) {
890 uint16_t val;
892 rc = xen_host_pci_get_word(&s->real_device, PCI_COMMAND, &val);
893 if (rc) {
894 XEN_PT_ERR(d, "Failed to read PCI_COMMAND! (rc: %d)\n", rc);
895 goto err_out;
896 } else {
897 val |= cmd;
898 rc = xen_host_pci_set_word(&s->real_device, PCI_COMMAND, val);
899 if (rc) {
900 XEN_PT_ERR(d, "Failed to write PCI_COMMAND val=0x%x!(rc: %d)\n",
901 val, rc);
902 goto err_out;
907 memory_listener_register(&s->memory_listener, &s->dev.bus_master_as);
908 memory_listener_register(&s->io_listener, &address_space_io);
909 s->listener_set = true;
910 XEN_PT_LOG(d,
911 "Real physical device %02x:%02x.%d registered successfully!\n",
912 s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function);
914 return 0;
916 err_out:
917 xen_pt_destroy(d);
918 assert(rc);
919 return rc;
922 static void xen_pt_unregister_device(PCIDevice *d)
924 xen_pt_destroy(d);
927 static Property xen_pci_passthrough_properties[] = {
928 DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState, hostaddr),
929 DEFINE_PROP_BOOL("permissive", XenPCIPassthroughState, permissive, false),
930 DEFINE_PROP_END_OF_LIST(),
933 static void xen_pci_passthrough_class_init(ObjectClass *klass, void *data)
935 DeviceClass *dc = DEVICE_CLASS(klass);
936 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
938 k->init = xen_pt_initfn;
939 k->exit = xen_pt_unregister_device;
940 k->config_read = xen_pt_pci_read_config;
941 k->config_write = xen_pt_pci_write_config;
942 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
943 dc->desc = "Assign an host PCI device with Xen";
944 dc->props = xen_pci_passthrough_properties;
947 static void xen_pci_passthrough_finalize(Object *obj)
949 XenPCIPassthroughState *s = XEN_PT_DEVICE(obj);
951 xen_pt_msix_delete(s);
954 static const TypeInfo xen_pci_passthrough_info = {
955 .name = TYPE_XEN_PT_DEVICE,
956 .parent = TYPE_PCI_DEVICE,
957 .instance_size = sizeof(XenPCIPassthroughState),
958 .instance_finalize = xen_pci_passthrough_finalize,
959 .class_init = xen_pci_passthrough_class_init,
962 static void xen_pci_passthrough_register_types(void)
964 type_register_static(&xen_pci_passthrough_info);
967 type_init(xen_pci_passthrough_register_types)