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:
19 * Initialize(register_real_device)
20 * Map INTx(xc_physdev_map_pirq):
22 * - Set real Interrupt Disable bit to '1'.
23 * - Set machine_irq and assigned_device->machine_irq to '0'.
26 * Bind INTx(xc_domain_bind_pt_pci_irq):
28 * - Set real Interrupt Disable bit to '1'.
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)
35 * - Set real bit to '0' if assigned_device->machine_irq isn't '0'.
38 * - Set real bit to '1'.
41 * Initialize MSI register(xen_pt_msi_setup, xen_pt_msi_update)
42 * Bind MSI(xc_domain_update_msi_irq)
45 * - Set dev->msi->pirq to '-1'.
48 * Initialize MSI-X register(xen_pt_msix_update_one)
49 * Bind MSI-X(xc_domain_update_msi_irq)
52 * - Set entry->pirq to '-1'.
55 #include "qemu/osdep.h"
56 #include "qapi/error.h"
57 #include <sys/ioctl.h>
59 #include "hw/pci/pci.h"
60 #include "hw/qdev-properties.h"
61 #include "hw/xen/xen.h"
62 #include "hw/i386/pc.h"
63 #include "hw/xen/xen-legacy-backend.h"
65 #include "qemu/range.h"
66 #include "exec/address-spaces.h"
68 #define XEN_PT_NR_IRQS (256)
69 static uint8_t xen_pt_mapped_machine_irq
[XEN_PT_NR_IRQS
] = {0};
71 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...)
77 fprintf(stderr
, "[%02x:%02x.%d] ", pci_dev_bus_num(d
),
78 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
));
80 vfprintf(stderr
, f
, ap
);
86 static int xen_pt_pci_config_access_check(PCIDevice
*d
, uint32_t addr
, int len
)
88 /* check offset range */
90 XEN_PT_ERR(d
, "Failed to access register with offset exceeding 0xFF. "
91 "(addr: 0x%02x, len: %d)\n", addr
, len
);
96 if ((len
!= 1) && (len
!= 2) && (len
!= 4)) {
97 XEN_PT_ERR(d
, "Failed to access register with invalid access length. "
98 "(addr: 0x%02x, len: %d)\n", addr
, len
);
102 /* check offset alignment */
103 if (addr
& (len
- 1)) {
104 XEN_PT_ERR(d
, "Failed to access register with invalid access size "
105 "alignment. (addr: 0x%02x, len: %d)\n", addr
, len
);
112 int xen_pt_bar_offset_to_index(uint32_t offset
)
116 /* check Exp ROM BAR */
117 if (offset
== PCI_ROM_ADDRESS
) {
121 /* calculate BAR index */
122 index
= (offset
- PCI_BASE_ADDRESS_0
) >> 2;
123 if (index
>= PCI_NUM_REGIONS
) {
130 static uint32_t xen_pt_pci_read_config(PCIDevice
*d
, uint32_t addr
, int len
)
132 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
134 XenPTRegGroup
*reg_grp_entry
= NULL
;
135 XenPTReg
*reg_entry
= NULL
;
138 uint32_t find_addr
= addr
;
140 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
144 /* find register group entry */
145 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
147 /* check 0-Hardwired register group */
148 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
149 /* no need to emulate, just return 0 */
155 /* read I/O device register value */
156 rc
= xen_host_pci_get_block(&s
->real_device
, addr
, (uint8_t *)&val
, len
);
158 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
159 memset(&val
, 0xff, len
);
162 /* just return the I/O device register value for
163 * passthrough type register group */
164 if (reg_grp_entry
== NULL
) {
168 /* adjust the read value to appropriate CFC-CFF window */
169 val
<<= (addr
& 3) << 3;
172 /* loop around the guest requested size */
173 while (emul_len
> 0) {
174 /* find register entry to be emulated */
175 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
177 XenPTRegInfo
*reg
= reg_entry
->reg
;
178 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
179 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
180 uint8_t *ptr_val
= NULL
;
182 valid_mask
<<= (find_addr
- real_offset
) << 3;
183 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
185 /* do emulation based on register size */
189 rc
= reg
->u
.b
.read(s
, reg_entry
, ptr_val
, valid_mask
);
194 rc
= reg
->u
.w
.read(s
, reg_entry
,
195 (uint16_t *)ptr_val
, valid_mask
);
199 if (reg
->u
.dw
.read
) {
200 rc
= reg
->u
.dw
.read(s
, reg_entry
,
201 (uint32_t *)ptr_val
, valid_mask
);
207 xen_shutdown_fatal_error("Internal error: Invalid read "
208 "emulation. (%s, rc: %d)\n",
213 /* calculate next address to find */
214 emul_len
-= reg
->size
;
216 find_addr
= real_offset
+ reg
->size
;
219 /* nothing to do with passthrough type register,
220 * continue to find next byte */
226 /* need to shift back before returning them to pci bus emulator */
227 val
>>= ((addr
& 3) << 3);
230 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
234 static void xen_pt_pci_write_config(PCIDevice
*d
, uint32_t addr
,
235 uint32_t val
, int len
)
237 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
239 XenPTRegGroup
*reg_grp_entry
= NULL
;
241 uint32_t read_val
= 0, wb_mask
;
243 XenPTReg
*reg_entry
= NULL
;
244 uint32_t find_addr
= addr
;
245 XenPTRegInfo
*reg
= NULL
;
246 bool wp_flag
= false;
248 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
252 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
254 /* check unused BAR register */
255 index
= xen_pt_bar_offset_to_index(addr
);
256 if ((index
>= 0) && (val
!= 0)) {
259 if (index
== PCI_ROM_SLOT
)
260 chk
|= (uint32_t)~PCI_ROM_ADDRESS_MASK
;
262 if ((chk
!= XEN_PT_BAR_ALLF
) &&
263 (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
)) {
264 XEN_PT_WARN(d
, "Guest attempt to set address to unused "
265 "Base Address Register. (addr: 0x%02x, len: %d)\n",
270 /* find register group entry */
271 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
273 /* check 0-Hardwired register group */
274 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
275 /* ignore silently */
276 XEN_PT_WARN(d
, "Access to 0-Hardwired register. "
277 "(addr: 0x%02x, len: %d)\n", addr
, len
);
282 rc
= xen_host_pci_get_block(&s
->real_device
, addr
,
283 (uint8_t *)&read_val
, len
);
285 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
286 memset(&read_val
, 0xff, len
);
289 wb_mask
= 0xFFFFFFFF >> ((4 - len
) << 3);
292 /* pass directly to the real device for passthrough type register group */
293 if (reg_grp_entry
== NULL
) {
294 if (!s
->permissive
) {
301 memory_region_transaction_begin();
302 pci_default_write_config(d
, addr
, val
, len
);
304 /* adjust the read and write value to appropriate CFC-CFF window */
305 read_val
<<= (addr
& 3) << 3;
306 val
<<= (addr
& 3) << 3;
309 /* loop around the guest requested size */
310 while (emul_len
> 0) {
311 /* find register entry to be emulated */
312 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
314 reg
= reg_entry
->reg
;
315 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
316 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
317 uint8_t *ptr_val
= NULL
;
318 uint32_t wp_mask
= reg
->emu_mask
| reg
->ro_mask
;
320 valid_mask
<<= (find_addr
- real_offset
) << 3;
321 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
322 if (!s
->permissive
) {
323 wp_mask
|= reg
->res_mask
;
325 if (wp_mask
== (0xFFFFFFFF >> ((4 - reg
->size
) << 3))) {
326 wb_mask
&= ~((wp_mask
>> ((find_addr
- real_offset
) << 3))
327 << ((len
- emul_len
) << 3));
330 /* do emulation based on register size */
333 if (reg
->u
.b
.write
) {
334 rc
= reg
->u
.b
.write(s
, reg_entry
, ptr_val
,
335 read_val
>> ((real_offset
& 3) << 3),
340 if (reg
->u
.w
.write
) {
341 rc
= reg
->u
.w
.write(s
, reg_entry
, (uint16_t *)ptr_val
,
342 (read_val
>> ((real_offset
& 3) << 3)),
347 if (reg
->u
.dw
.write
) {
348 rc
= reg
->u
.dw
.write(s
, reg_entry
, (uint32_t *)ptr_val
,
349 (read_val
>> ((real_offset
& 3) << 3)),
356 xen_shutdown_fatal_error("Internal error: Invalid write"
357 " emulation. (%s, rc: %d)\n",
362 /* calculate next address to find */
363 emul_len
-= reg
->size
;
365 find_addr
= real_offset
+ reg
->size
;
368 /* nothing to do with passthrough type register,
369 * continue to find next byte */
370 if (!s
->permissive
) {
371 wb_mask
&= ~(0xff << ((len
- emul_len
) << 3));
372 /* Unused BARs will make it here, but we don't want to issue
373 * warnings for writes to them (bogus writes get dealt with
385 /* need to shift back before passing them to xen_host_pci_set_block. */
386 val
>>= (addr
& 3) << 3;
388 memory_region_transaction_commit();
391 if (wp_flag
&& !s
->permissive_warned
) {
392 s
->permissive_warned
= true;
393 xen_pt_log(d
, "Write-back to unknown field 0x%02x (partially) inhibited (0x%0*x)\n",
394 addr
, len
* 2, wb_mask
);
395 xen_pt_log(d
, "If the device doesn't work, try enabling permissive mode\n");
396 xen_pt_log(d
, "(unsafe) and if it helps report the problem to xen-devel\n");
398 for (index
= 0; wb_mask
; index
+= len
) {
399 /* unknown regs are passed through */
400 while (!(wb_mask
& 0xff)) {
408 } while (wb_mask
& 0xff);
409 rc
= xen_host_pci_set_block(&s
->real_device
, addr
+ index
,
410 (uint8_t *)&val
+ index
, len
);
413 XEN_PT_ERR(d
, "xen_host_pci_set_block failed. return value: %d.\n", rc
);
418 /* register regions */
420 static uint64_t xen_pt_bar_read(void *o
, hwaddr addr
,
424 /* if this function is called, that probably means that there is a
425 * misconfiguration of the IOMMU. */
426 XEN_PT_ERR(d
, "Should not read BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
430 static void xen_pt_bar_write(void *o
, hwaddr addr
, uint64_t val
,
434 /* Same comment as xen_pt_bar_read function */
435 XEN_PT_ERR(d
, "Should not write BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
439 static const MemoryRegionOps ops
= {
440 .endianness
= DEVICE_NATIVE_ENDIAN
,
441 .read
= xen_pt_bar_read
,
442 .write
= xen_pt_bar_write
,
445 static int xen_pt_register_regions(XenPCIPassthroughState
*s
, uint16_t *cmd
)
448 XenHostPCIDevice
*d
= &s
->real_device
;
450 /* Register PIO/MMIO BARs */
451 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
452 XenHostPCIIORegion
*r
= &d
->io_regions
[i
];
455 if (r
->base_addr
== 0 || r
->size
== 0) {
459 s
->bases
[i
].access
.u
= r
->base_addr
;
461 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
462 type
= PCI_BASE_ADDRESS_SPACE_IO
;
463 *cmd
|= PCI_COMMAND_IO
;
465 type
= PCI_BASE_ADDRESS_SPACE_MEMORY
;
466 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_PREFETCH
) {
467 type
|= PCI_BASE_ADDRESS_MEM_PREFETCH
;
469 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
) {
470 type
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
472 *cmd
|= PCI_COMMAND_MEMORY
;
475 memory_region_init_io(&s
->bar
[i
], OBJECT(s
), &ops
, &s
->dev
,
476 "xen-pci-pt-bar", r
->size
);
477 pci_register_bar(&s
->dev
, i
, type
, &s
->bar
[i
]);
479 XEN_PT_LOG(&s
->dev
, "IO region %i registered (size=0x%08"PRIx64
480 " base_addr=0x%08"PRIx64
" type: %#x)\n",
481 i
, r
->size
, r
->base_addr
, type
);
484 /* Register expansion ROM address */
485 if (d
->rom
.base_addr
&& d
->rom
.size
) {
486 uint32_t bar_data
= 0;
488 /* Re-set BAR reported by OS, otherwise ROM can't be read. */
489 if (xen_host_pci_get_long(d
, PCI_ROM_ADDRESS
, &bar_data
)) {
492 if ((bar_data
& PCI_ROM_ADDRESS_MASK
) == 0) {
493 bar_data
|= d
->rom
.base_addr
& PCI_ROM_ADDRESS_MASK
;
494 xen_host_pci_set_long(d
, PCI_ROM_ADDRESS
, bar_data
);
497 s
->bases
[PCI_ROM_SLOT
].access
.maddr
= d
->rom
.base_addr
;
499 memory_region_init_io(&s
->rom
, OBJECT(s
), &ops
, &s
->dev
,
500 "xen-pci-pt-rom", d
->rom
.size
);
501 pci_register_bar(&s
->dev
, PCI_ROM_SLOT
, PCI_BASE_ADDRESS_MEM_PREFETCH
,
504 XEN_PT_LOG(&s
->dev
, "Expansion ROM registered (size=0x%08"PRIx64
505 " base_addr=0x%08"PRIx64
")\n",
506 d
->rom
.size
, d
->rom
.base_addr
);
509 xen_pt_register_vga_regions(d
);
515 static int xen_pt_bar_from_region(XenPCIPassthroughState
*s
, MemoryRegion
*mr
)
519 for (i
= 0; i
< PCI_NUM_REGIONS
- 1; i
++) {
520 if (mr
== &s
->bar
[i
]) {
531 * This function checks if an io_region overlaps an io_region from another
532 * device. The io_region to check is provided with (addr, size and type)
533 * A callback can be provided and will be called for every region that is
535 * The return value indicates if the region is overlappsed */
536 struct CheckBarArgs
{
537 XenPCIPassthroughState
*s
;
543 static void xen_pt_check_bar_overlap(PCIBus
*bus
, PCIDevice
*d
, void *opaque
)
545 struct CheckBarArgs
*arg
= opaque
;
546 XenPCIPassthroughState
*s
= arg
->s
;
547 uint8_t type
= arg
->type
;
550 if (d
->devfn
== s
->dev
.devfn
) {
554 /* xxx: This ignores bridges. */
555 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
556 const PCIIORegion
*r
= &d
->io_regions
[i
];
561 if ((type
& PCI_BASE_ADDRESS_SPACE_IO
)
562 != (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
)) {
566 if (ranges_overlap(arg
->addr
, arg
->size
, r
->addr
, r
->size
)) {
568 "Overlapped to device [%02x:%02x.%d] Region: %i"
569 " (addr: %#"FMT_PCIBUS
", len: %#"FMT_PCIBUS
")\n",
570 pci_bus_num(bus
), PCI_SLOT(d
->devfn
),
571 PCI_FUNC(d
->devfn
), i
, r
->addr
, r
->size
);
577 static void xen_pt_region_update(XenPCIPassthroughState
*s
,
578 MemoryRegionSection
*sec
, bool adding
)
580 PCIDevice
*d
= &s
->dev
;
581 MemoryRegion
*mr
= sec
->mr
;
584 int op
= adding
? DPCI_ADD_MAPPING
: DPCI_REMOVE_MAPPING
;
585 struct CheckBarArgs args
= {
587 .addr
= sec
->offset_within_address_space
,
588 .size
= int128_get64(sec
->size
),
592 bar
= xen_pt_bar_from_region(s
, mr
);
593 if (bar
== -1 && (!s
->msix
|| &s
->msix
->mmio
!= mr
)) {
597 if (s
->msix
&& &s
->msix
->mmio
== mr
) {
599 s
->msix
->mmio_base_addr
= sec
->offset_within_address_space
;
600 rc
= xen_pt_msix_update_remap(s
, s
->msix
->bar_index
);
605 args
.type
= d
->io_regions
[bar
].type
;
606 pci_for_each_device(pci_get_bus(d
), pci_dev_bus_num(d
),
607 xen_pt_check_bar_overlap
, &args
);
609 XEN_PT_WARN(d
, "Region: %d (addr: %#"FMT_PCIBUS
610 ", len: %#"FMT_PCIBUS
") is overlapped.\n",
611 bar
, sec
->offset_within_address_space
,
612 int128_get64(sec
->size
));
615 if (d
->io_regions
[bar
].type
& PCI_BASE_ADDRESS_SPACE_IO
) {
616 uint32_t guest_port
= sec
->offset_within_address_space
;
617 uint32_t machine_port
= s
->bases
[bar
].access
.pio_base
;
618 uint32_t size
= int128_get64(sec
->size
);
619 rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
620 guest_port
, machine_port
, size
,
623 XEN_PT_ERR(d
, "%s ioport mapping failed! (err: %i)\n",
624 adding
? "create new" : "remove old", errno
);
627 pcibus_t guest_addr
= sec
->offset_within_address_space
;
628 pcibus_t machine_addr
= s
->bases
[bar
].access
.maddr
629 + sec
->offset_within_region
;
630 pcibus_t size
= int128_get64(sec
->size
);
631 rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
632 XEN_PFN(guest_addr
+ XC_PAGE_SIZE
- 1),
633 XEN_PFN(machine_addr
+ XC_PAGE_SIZE
- 1),
634 XEN_PFN(size
+ XC_PAGE_SIZE
- 1),
637 XEN_PT_ERR(d
, "%s mem mapping failed! (err: %i)\n",
638 adding
? "create new" : "remove old", errno
);
643 static void xen_pt_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
645 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
648 memory_region_ref(sec
->mr
);
649 xen_pt_region_update(s
, sec
, true);
652 static void xen_pt_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
654 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
657 xen_pt_region_update(s
, sec
, false);
658 memory_region_unref(sec
->mr
);
661 static void xen_pt_io_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
663 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
666 memory_region_ref(sec
->mr
);
667 xen_pt_region_update(s
, sec
, true);
670 static void xen_pt_io_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
672 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
675 xen_pt_region_update(s
, sec
, false);
676 memory_region_unref(sec
->mr
);
679 static const MemoryListener xen_pt_memory_listener
= {
680 .region_add
= xen_pt_region_add
,
681 .region_del
= xen_pt_region_del
,
685 static const MemoryListener xen_pt_io_listener
= {
686 .region_add
= xen_pt_io_region_add
,
687 .region_del
= xen_pt_io_region_del
,
692 xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState
*s
,
693 XenHostPCIDevice
*dev
)
696 PCIDevice
*d
= &s
->dev
;
698 gpu_dev_id
= dev
->device_id
;
699 igd_passthrough_isa_bridge_create(pci_get_bus(d
), gpu_dev_id
);
703 static void xen_pt_destroy(PCIDevice
*d
) {
705 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
706 XenHostPCIDevice
*host_dev
= &s
->real_device
;
707 uint8_t machine_irq
= s
->machine_irq
;
711 if (machine_irq
&& !xen_host_pci_device_closed(&s
->real_device
)) {
712 intx
= xen_pt_pci_intx(s
);
713 rc
= xc_domain_unbind_pt_irq(xen_xc
, xen_domid
, machine_irq
,
716 PCI_SLOT(s
->dev
.devfn
),
720 XEN_PT_ERR(d
, "unbinding of interrupt INT%c failed."
721 " (machine irq: %i, err: %d)"
722 " But bravely continuing on..\n",
723 'a' + intx
, machine_irq
, errno
);
727 /* N.B. xen_pt_config_delete takes care of freeing them. */
729 xen_pt_msi_disable(s
);
732 xen_pt_msix_disable(s
);
736 xen_pt_mapped_machine_irq
[machine_irq
]--;
738 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
739 rc
= xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
);
742 XEN_PT_ERR(d
, "unmapping of interrupt %i failed. (err: %d)"
743 " But bravely continuing on..\n",
750 /* delete all emulated config registers */
751 xen_pt_config_delete(s
);
753 xen_pt_unregister_vga_regions(host_dev
);
755 if (s
->listener_set
) {
756 memory_listener_unregister(&s
->memory_listener
);
757 memory_listener_unregister(&s
->io_listener
);
758 s
->listener_set
= false;
760 if (!xen_host_pci_device_closed(&s
->real_device
)) {
761 xen_host_pci_device_put(&s
->real_device
);
766 static void xen_pt_realize(PCIDevice
*d
, Error
**errp
)
768 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
770 uint8_t machine_irq
= 0, scratch
;
772 int pirq
= XEN_PT_UNASSIGNED_PIRQ
;
775 /* register real device */
776 XEN_PT_LOG(d
, "Assigning real physical device %02x:%02x.%d"
778 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
,
781 xen_host_pci_device_get(&s
->real_device
,
782 s
->hostaddr
.domain
, s
->hostaddr
.bus
,
783 s
->hostaddr
.slot
, s
->hostaddr
.function
,
786 error_append_hint(&err
, "Failed to \"open\" the real pci device");
787 error_propagate(errp
, err
);
791 s
->is_virtfn
= s
->real_device
.is_virtfn
;
793 XEN_PT_LOG(d
, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n",
794 s
->real_device
.domain
, s
->real_device
.bus
,
795 s
->real_device
.dev
, s
->real_device
.func
);
798 /* Initialize virtualized PCI configuration (Extended 256 Bytes) */
799 memset(d
->config
, 0, PCI_CONFIG_SPACE_SIZE
);
801 s
->memory_listener
= xen_pt_memory_listener
;
802 s
->io_listener
= xen_pt_io_listener
;
804 /* Setup VGA bios for passthrough GFX */
805 if ((s
->real_device
.domain
== 0) && (s
->real_device
.bus
== 0) &&
806 (s
->real_device
.dev
== 2) && (s
->real_device
.func
== 0)) {
807 if (!is_igd_vga_passthrough(&s
->real_device
)) {
808 error_setg(errp
, "Need to enable igd-passthru if you're trying"
809 " to passthrough IGD GFX");
810 xen_host_pci_device_put(&s
->real_device
);
814 xen_pt_setup_vga(s
, &s
->real_device
, &err
);
816 error_append_hint(&err
, "Setup VGA BIOS of passthrough"
818 error_propagate(errp
, err
);
819 xen_host_pci_device_put(&s
->real_device
);
823 /* Register ISA bridge for passthrough GFX. */
824 xen_igd_passthrough_isa_bridge_create(s
, &s
->real_device
);
827 /* Handle real device's MMIO/PIO BARs */
828 xen_pt_register_regions(s
, &cmd
);
830 /* reinitialize each config register to be emulated */
831 xen_pt_config_init(s
, &err
);
833 error_append_hint(&err
, "PCI Config space initialisation failed");
834 error_propagate(errp
, err
);
840 rc
= xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &scratch
);
842 error_setg_errno(errp
, errno
, "Failed to read PCI_INTERRUPT_PIN");
846 XEN_PT_LOG(d
, "no pin interrupt\n");
850 machine_irq
= s
->real_device
.irq
;
851 if (machine_irq
== 0) {
852 XEN_PT_LOG(d
, "machine irq is 0\n");
853 cmd
|= PCI_COMMAND_INTX_DISABLE
;
857 rc
= xc_physdev_map_pirq(xen_xc
, xen_domid
, machine_irq
, &pirq
);
859 error_setg_errno(errp
, errno
, "Mapping machine irq %u to"
860 " pirq %i failed", machine_irq
, pirq
);
862 /* Disable PCI intx assertion (turn on bit10 of devctl) */
863 cmd
|= PCI_COMMAND_INTX_DISABLE
;
868 s
->machine_irq
= pirq
;
869 xen_pt_mapped_machine_irq
[machine_irq
]++;
872 /* bind machine_irq to device */
873 if (machine_irq
!= 0) {
874 uint8_t e_intx
= xen_pt_pci_intx(s
);
876 rc
= xc_domain_bind_pt_pci_irq(xen_xc
, xen_domid
, machine_irq
,
881 error_setg_errno(errp
, errno
, "Binding of interrupt %u failed",
884 /* Disable PCI intx assertion (turn on bit10 of devctl) */
885 cmd
|= PCI_COMMAND_INTX_DISABLE
;
886 xen_pt_mapped_machine_irq
[machine_irq
]--;
888 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
889 if (xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
)) {
890 error_setg_errno(errp
, errno
, "Unmapping of machine"
891 " interrupt %u failed", machine_irq
);
902 rc
= xen_host_pci_get_word(&s
->real_device
, PCI_COMMAND
, &val
);
904 error_setg_errno(errp
, errno
, "Failed to read PCI_COMMAND");
908 rc
= xen_host_pci_set_word(&s
->real_device
, PCI_COMMAND
, val
);
910 error_setg_errno(errp
, errno
, "Failed to write PCI_COMMAND"
917 memory_listener_register(&s
->memory_listener
, &address_space_memory
);
918 memory_listener_register(&s
->io_listener
, &address_space_io
);
919 s
->listener_set
= true;
921 "Real physical device %02x:%02x.%d registered successfully\n",
922 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
);
927 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
928 object_unparent(OBJECT(&s
->bar
[i
]));
930 object_unparent(OBJECT(&s
->rom
));
936 static void xen_pt_unregister_device(PCIDevice
*d
)
941 static Property xen_pci_passthrough_properties
[] = {
942 DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState
, hostaddr
),
943 DEFINE_PROP_BOOL("permissive", XenPCIPassthroughState
, permissive
, false),
944 DEFINE_PROP_END_OF_LIST(),
947 static void xen_pci_passthrough_instance_init(Object
*obj
)
949 /* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
950 * line, therefore, no need to wait to realize like other devices */
951 PCI_DEVICE(obj
)->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
954 static void xen_pci_passthrough_class_init(ObjectClass
*klass
, void *data
)
956 DeviceClass
*dc
= DEVICE_CLASS(klass
);
957 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
959 k
->realize
= xen_pt_realize
;
960 k
->exit
= xen_pt_unregister_device
;
961 k
->config_read
= xen_pt_pci_read_config
;
962 k
->config_write
= xen_pt_pci_write_config
;
963 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
964 dc
->desc
= "Assign an host PCI device with Xen";
965 dc
->props
= xen_pci_passthrough_properties
;
968 static void xen_pci_passthrough_finalize(Object
*obj
)
970 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(obj
);
972 xen_pt_msix_delete(s
);
975 static const TypeInfo xen_pci_passthrough_info
= {
976 .name
= TYPE_XEN_PT_DEVICE
,
977 .parent
= TYPE_PCI_DEVICE
,
978 .instance_size
= sizeof(XenPCIPassthroughState
),
979 .instance_finalize
= xen_pci_passthrough_finalize
,
980 .class_init
= xen_pci_passthrough_class_init
,
981 .instance_init
= xen_pci_passthrough_instance_init
,
982 .interfaces
= (InterfaceInfo
[]) {
983 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
984 { INTERFACE_PCIE_DEVICE
},
989 static void xen_pci_passthrough_register_types(void)
991 type_register_static(&xen_pci_passthrough_info
);
994 type_init(xen_pci_passthrough_register_types
)