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/xen/xen.h"
61 #include "hw/i386/pc.h"
62 #include "hw/xen/xen-legacy-backend.h"
64 #include "qemu/range.h"
65 #include "exec/address-spaces.h"
67 #define XEN_PT_NR_IRQS (256)
68 static uint8_t xen_pt_mapped_machine_irq
[XEN_PT_NR_IRQS
] = {0};
70 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...)
76 fprintf(stderr
, "[%02x:%02x.%d] ", pci_dev_bus_num(d
),
77 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
));
79 vfprintf(stderr
, f
, ap
);
85 static int xen_pt_pci_config_access_check(PCIDevice
*d
, uint32_t addr
, int len
)
87 /* check offset range */
89 XEN_PT_ERR(d
, "Failed to access register with offset exceeding 0xFF. "
90 "(addr: 0x%02x, len: %d)\n", addr
, len
);
95 if ((len
!= 1) && (len
!= 2) && (len
!= 4)) {
96 XEN_PT_ERR(d
, "Failed to access register with invalid access length. "
97 "(addr: 0x%02x, len: %d)\n", addr
, len
);
101 /* check offset alignment */
102 if (addr
& (len
- 1)) {
103 XEN_PT_ERR(d
, "Failed to access register with invalid access size "
104 "alignment. (addr: 0x%02x, len: %d)\n", addr
, len
);
111 int xen_pt_bar_offset_to_index(uint32_t offset
)
115 /* check Exp ROM BAR */
116 if (offset
== PCI_ROM_ADDRESS
) {
120 /* calculate BAR index */
121 index
= (offset
- PCI_BASE_ADDRESS_0
) >> 2;
122 if (index
>= PCI_NUM_REGIONS
) {
129 static uint32_t xen_pt_pci_read_config(PCIDevice
*d
, uint32_t addr
, int len
)
131 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
133 XenPTRegGroup
*reg_grp_entry
= NULL
;
134 XenPTReg
*reg_entry
= NULL
;
137 uint32_t find_addr
= addr
;
139 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
143 /* find register group entry */
144 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
146 /* check 0-Hardwired register group */
147 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
148 /* no need to emulate, just return 0 */
154 /* read I/O device register value */
155 rc
= xen_host_pci_get_block(&s
->real_device
, addr
, (uint8_t *)&val
, len
);
157 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
158 memset(&val
, 0xff, len
);
161 /* just return the I/O device register value for
162 * passthrough type register group */
163 if (reg_grp_entry
== NULL
) {
167 /* adjust the read value to appropriate CFC-CFF window */
168 val
<<= (addr
& 3) << 3;
171 /* loop around the guest requested size */
172 while (emul_len
> 0) {
173 /* find register entry to be emulated */
174 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
176 XenPTRegInfo
*reg
= reg_entry
->reg
;
177 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
178 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
179 uint8_t *ptr_val
= NULL
;
181 valid_mask
<<= (find_addr
- real_offset
) << 3;
182 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
184 /* do emulation based on register size */
188 rc
= reg
->u
.b
.read(s
, reg_entry
, ptr_val
, valid_mask
);
193 rc
= reg
->u
.w
.read(s
, reg_entry
,
194 (uint16_t *)ptr_val
, valid_mask
);
198 if (reg
->u
.dw
.read
) {
199 rc
= reg
->u
.dw
.read(s
, reg_entry
,
200 (uint32_t *)ptr_val
, valid_mask
);
206 xen_shutdown_fatal_error("Internal error: Invalid read "
207 "emulation. (%s, rc: %d)\n",
212 /* calculate next address to find */
213 emul_len
-= reg
->size
;
215 find_addr
= real_offset
+ reg
->size
;
218 /* nothing to do with passthrough type register,
219 * continue to find next byte */
225 /* need to shift back before returning them to pci bus emulator */
226 val
>>= ((addr
& 3) << 3);
229 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
233 static void xen_pt_pci_write_config(PCIDevice
*d
, uint32_t addr
,
234 uint32_t val
, int len
)
236 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
238 XenPTRegGroup
*reg_grp_entry
= NULL
;
240 uint32_t read_val
= 0, wb_mask
;
242 XenPTReg
*reg_entry
= NULL
;
243 uint32_t find_addr
= addr
;
244 XenPTRegInfo
*reg
= NULL
;
245 bool wp_flag
= false;
247 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
251 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
253 /* check unused BAR register */
254 index
= xen_pt_bar_offset_to_index(addr
);
255 if ((index
>= 0) && (val
!= 0)) {
258 if (index
== PCI_ROM_SLOT
)
259 chk
|= (uint32_t)~PCI_ROM_ADDRESS_MASK
;
261 if ((chk
!= XEN_PT_BAR_ALLF
) &&
262 (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
)) {
263 XEN_PT_WARN(d
, "Guest attempt to set address to unused "
264 "Base Address Register. (addr: 0x%02x, len: %d)\n",
269 /* find register group entry */
270 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
272 /* check 0-Hardwired register group */
273 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
274 /* ignore silently */
275 XEN_PT_WARN(d
, "Access to 0-Hardwired register. "
276 "(addr: 0x%02x, len: %d)\n", addr
, len
);
281 rc
= xen_host_pci_get_block(&s
->real_device
, addr
,
282 (uint8_t *)&read_val
, len
);
284 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
285 memset(&read_val
, 0xff, len
);
288 wb_mask
= 0xFFFFFFFF >> ((4 - len
) << 3);
291 /* pass directly to the real device for passthrough type register group */
292 if (reg_grp_entry
== NULL
) {
293 if (!s
->permissive
) {
300 memory_region_transaction_begin();
301 pci_default_write_config(d
, addr
, val
, len
);
303 /* adjust the read and write value to appropriate CFC-CFF window */
304 read_val
<<= (addr
& 3) << 3;
305 val
<<= (addr
& 3) << 3;
308 /* loop around the guest requested size */
309 while (emul_len
> 0) {
310 /* find register entry to be emulated */
311 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
313 reg
= reg_entry
->reg
;
314 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
315 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
316 uint8_t *ptr_val
= NULL
;
317 uint32_t wp_mask
= reg
->emu_mask
| reg
->ro_mask
;
319 valid_mask
<<= (find_addr
- real_offset
) << 3;
320 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
321 if (!s
->permissive
) {
322 wp_mask
|= reg
->res_mask
;
324 if (wp_mask
== (0xFFFFFFFF >> ((4 - reg
->size
) << 3))) {
325 wb_mask
&= ~((wp_mask
>> ((find_addr
- real_offset
) << 3))
326 << ((len
- emul_len
) << 3));
329 /* do emulation based on register size */
332 if (reg
->u
.b
.write
) {
333 rc
= reg
->u
.b
.write(s
, reg_entry
, ptr_val
,
334 read_val
>> ((real_offset
& 3) << 3),
339 if (reg
->u
.w
.write
) {
340 rc
= reg
->u
.w
.write(s
, reg_entry
, (uint16_t *)ptr_val
,
341 (read_val
>> ((real_offset
& 3) << 3)),
346 if (reg
->u
.dw
.write
) {
347 rc
= reg
->u
.dw
.write(s
, reg_entry
, (uint32_t *)ptr_val
,
348 (read_val
>> ((real_offset
& 3) << 3)),
355 xen_shutdown_fatal_error("Internal error: Invalid write"
356 " emulation. (%s, rc: %d)\n",
361 /* calculate next address to find */
362 emul_len
-= reg
->size
;
364 find_addr
= real_offset
+ reg
->size
;
367 /* nothing to do with passthrough type register,
368 * continue to find next byte */
369 if (!s
->permissive
) {
370 wb_mask
&= ~(0xff << ((len
- emul_len
) << 3));
371 /* Unused BARs will make it here, but we don't want to issue
372 * warnings for writes to them (bogus writes get dealt with
384 /* need to shift back before passing them to xen_host_pci_set_block. */
385 val
>>= (addr
& 3) << 3;
387 memory_region_transaction_commit();
390 if (wp_flag
&& !s
->permissive_warned
) {
391 s
->permissive_warned
= true;
392 xen_pt_log(d
, "Write-back to unknown field 0x%02x (partially) inhibited (0x%0*x)\n",
393 addr
, len
* 2, wb_mask
);
394 xen_pt_log(d
, "If the device doesn't work, try enabling permissive mode\n");
395 xen_pt_log(d
, "(unsafe) and if it helps report the problem to xen-devel\n");
397 for (index
= 0; wb_mask
; index
+= len
) {
398 /* unknown regs are passed through */
399 while (!(wb_mask
& 0xff)) {
407 } while (wb_mask
& 0xff);
408 rc
= xen_host_pci_set_block(&s
->real_device
, addr
+ index
,
409 (uint8_t *)&val
+ index
, len
);
412 XEN_PT_ERR(d
, "xen_host_pci_set_block failed. return value: %d.\n", rc
);
417 /* register regions */
419 static uint64_t xen_pt_bar_read(void *o
, hwaddr addr
,
423 /* if this function is called, that probably means that there is a
424 * misconfiguration of the IOMMU. */
425 XEN_PT_ERR(d
, "Should not read BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
429 static void xen_pt_bar_write(void *o
, hwaddr addr
, uint64_t val
,
433 /* Same comment as xen_pt_bar_read function */
434 XEN_PT_ERR(d
, "Should not write BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
438 static const MemoryRegionOps ops
= {
439 .endianness
= DEVICE_NATIVE_ENDIAN
,
440 .read
= xen_pt_bar_read
,
441 .write
= xen_pt_bar_write
,
444 static int xen_pt_register_regions(XenPCIPassthroughState
*s
, uint16_t *cmd
)
447 XenHostPCIDevice
*d
= &s
->real_device
;
449 /* Register PIO/MMIO BARs */
450 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
451 XenHostPCIIORegion
*r
= &d
->io_regions
[i
];
454 if (r
->base_addr
== 0 || r
->size
== 0) {
458 s
->bases
[i
].access
.u
= r
->base_addr
;
460 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
461 type
= PCI_BASE_ADDRESS_SPACE_IO
;
462 *cmd
|= PCI_COMMAND_IO
;
464 type
= PCI_BASE_ADDRESS_SPACE_MEMORY
;
465 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_PREFETCH
) {
466 type
|= PCI_BASE_ADDRESS_MEM_PREFETCH
;
468 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
) {
469 type
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
471 *cmd
|= PCI_COMMAND_MEMORY
;
474 memory_region_init_io(&s
->bar
[i
], OBJECT(s
), &ops
, &s
->dev
,
475 "xen-pci-pt-bar", r
->size
);
476 pci_register_bar(&s
->dev
, i
, type
, &s
->bar
[i
]);
478 XEN_PT_LOG(&s
->dev
, "IO region %i registered (size=0x%08"PRIx64
479 " base_addr=0x%08"PRIx64
" type: %#x)\n",
480 i
, r
->size
, r
->base_addr
, type
);
483 /* Register expansion ROM address */
484 if (d
->rom
.base_addr
&& d
->rom
.size
) {
485 uint32_t bar_data
= 0;
487 /* Re-set BAR reported by OS, otherwise ROM can't be read. */
488 if (xen_host_pci_get_long(d
, PCI_ROM_ADDRESS
, &bar_data
)) {
491 if ((bar_data
& PCI_ROM_ADDRESS_MASK
) == 0) {
492 bar_data
|= d
->rom
.base_addr
& PCI_ROM_ADDRESS_MASK
;
493 xen_host_pci_set_long(d
, PCI_ROM_ADDRESS
, bar_data
);
496 s
->bases
[PCI_ROM_SLOT
].access
.maddr
= d
->rom
.base_addr
;
498 memory_region_init_io(&s
->rom
, OBJECT(s
), &ops
, &s
->dev
,
499 "xen-pci-pt-rom", d
->rom
.size
);
500 pci_register_bar(&s
->dev
, PCI_ROM_SLOT
, PCI_BASE_ADDRESS_MEM_PREFETCH
,
503 XEN_PT_LOG(&s
->dev
, "Expansion ROM registered (size=0x%08"PRIx64
504 " base_addr=0x%08"PRIx64
")\n",
505 d
->rom
.size
, d
->rom
.base_addr
);
508 xen_pt_register_vga_regions(d
);
514 static int xen_pt_bar_from_region(XenPCIPassthroughState
*s
, MemoryRegion
*mr
)
518 for (i
= 0; i
< PCI_NUM_REGIONS
- 1; i
++) {
519 if (mr
== &s
->bar
[i
]) {
530 * This function checks if an io_region overlaps an io_region from another
531 * device. The io_region to check is provided with (addr, size and type)
532 * A callback can be provided and will be called for every region that is
534 * The return value indicates if the region is overlappsed */
535 struct CheckBarArgs
{
536 XenPCIPassthroughState
*s
;
542 static void xen_pt_check_bar_overlap(PCIBus
*bus
, PCIDevice
*d
, void *opaque
)
544 struct CheckBarArgs
*arg
= opaque
;
545 XenPCIPassthroughState
*s
= arg
->s
;
546 uint8_t type
= arg
->type
;
549 if (d
->devfn
== s
->dev
.devfn
) {
553 /* xxx: This ignores bridges. */
554 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
555 const PCIIORegion
*r
= &d
->io_regions
[i
];
560 if ((type
& PCI_BASE_ADDRESS_SPACE_IO
)
561 != (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
)) {
565 if (ranges_overlap(arg
->addr
, arg
->size
, r
->addr
, r
->size
)) {
567 "Overlapped to device [%02x:%02x.%d] Region: %i"
568 " (addr: %#"FMT_PCIBUS
", len: %#"FMT_PCIBUS
")\n",
569 pci_bus_num(bus
), PCI_SLOT(d
->devfn
),
570 PCI_FUNC(d
->devfn
), i
, r
->addr
, r
->size
);
576 static void xen_pt_region_update(XenPCIPassthroughState
*s
,
577 MemoryRegionSection
*sec
, bool adding
)
579 PCIDevice
*d
= &s
->dev
;
580 MemoryRegion
*mr
= sec
->mr
;
583 int op
= adding
? DPCI_ADD_MAPPING
: DPCI_REMOVE_MAPPING
;
584 struct CheckBarArgs args
= {
586 .addr
= sec
->offset_within_address_space
,
587 .size
= int128_get64(sec
->size
),
591 bar
= xen_pt_bar_from_region(s
, mr
);
592 if (bar
== -1 && (!s
->msix
|| &s
->msix
->mmio
!= mr
)) {
596 if (s
->msix
&& &s
->msix
->mmio
== mr
) {
598 s
->msix
->mmio_base_addr
= sec
->offset_within_address_space
;
599 rc
= xen_pt_msix_update_remap(s
, s
->msix
->bar_index
);
604 args
.type
= d
->io_regions
[bar
].type
;
605 pci_for_each_device(pci_get_bus(d
), pci_dev_bus_num(d
),
606 xen_pt_check_bar_overlap
, &args
);
608 XEN_PT_WARN(d
, "Region: %d (addr: %#"FMT_PCIBUS
609 ", len: %#"FMT_PCIBUS
") is overlapped.\n",
610 bar
, sec
->offset_within_address_space
,
611 int128_get64(sec
->size
));
614 if (d
->io_regions
[bar
].type
& PCI_BASE_ADDRESS_SPACE_IO
) {
615 uint32_t guest_port
= sec
->offset_within_address_space
;
616 uint32_t machine_port
= s
->bases
[bar
].access
.pio_base
;
617 uint32_t size
= int128_get64(sec
->size
);
618 rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
619 guest_port
, machine_port
, size
,
622 XEN_PT_ERR(d
, "%s ioport mapping failed! (err: %i)\n",
623 adding
? "create new" : "remove old", errno
);
626 pcibus_t guest_addr
= sec
->offset_within_address_space
;
627 pcibus_t machine_addr
= s
->bases
[bar
].access
.maddr
628 + sec
->offset_within_region
;
629 pcibus_t size
= int128_get64(sec
->size
);
630 rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
631 XEN_PFN(guest_addr
+ XC_PAGE_SIZE
- 1),
632 XEN_PFN(machine_addr
+ XC_PAGE_SIZE
- 1),
633 XEN_PFN(size
+ XC_PAGE_SIZE
- 1),
636 XEN_PT_ERR(d
, "%s mem mapping failed! (err: %i)\n",
637 adding
? "create new" : "remove old", errno
);
642 static void xen_pt_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
644 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
647 memory_region_ref(sec
->mr
);
648 xen_pt_region_update(s
, sec
, true);
651 static void xen_pt_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
653 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
656 xen_pt_region_update(s
, sec
, false);
657 memory_region_unref(sec
->mr
);
660 static void xen_pt_io_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
662 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
665 memory_region_ref(sec
->mr
);
666 xen_pt_region_update(s
, sec
, true);
669 static void xen_pt_io_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
671 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
674 xen_pt_region_update(s
, sec
, false);
675 memory_region_unref(sec
->mr
);
678 static const MemoryListener xen_pt_memory_listener
= {
679 .region_add
= xen_pt_region_add
,
680 .region_del
= xen_pt_region_del
,
684 static const MemoryListener xen_pt_io_listener
= {
685 .region_add
= xen_pt_io_region_add
,
686 .region_del
= xen_pt_io_region_del
,
691 xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState
*s
,
692 XenHostPCIDevice
*dev
)
695 PCIDevice
*d
= &s
->dev
;
697 gpu_dev_id
= dev
->device_id
;
698 igd_passthrough_isa_bridge_create(pci_get_bus(d
), gpu_dev_id
);
702 static void xen_pt_destroy(PCIDevice
*d
) {
704 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
705 XenHostPCIDevice
*host_dev
= &s
->real_device
;
706 uint8_t machine_irq
= s
->machine_irq
;
710 if (machine_irq
&& !xen_host_pci_device_closed(&s
->real_device
)) {
711 intx
= xen_pt_pci_intx(s
);
712 rc
= xc_domain_unbind_pt_irq(xen_xc
, xen_domid
, machine_irq
,
715 PCI_SLOT(s
->dev
.devfn
),
719 XEN_PT_ERR(d
, "unbinding of interrupt INT%c failed."
720 " (machine irq: %i, err: %d)"
721 " But bravely continuing on..\n",
722 'a' + intx
, machine_irq
, errno
);
726 /* N.B. xen_pt_config_delete takes care of freeing them. */
728 xen_pt_msi_disable(s
);
731 xen_pt_msix_disable(s
);
735 xen_pt_mapped_machine_irq
[machine_irq
]--;
737 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
738 rc
= xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
);
741 XEN_PT_ERR(d
, "unmapping of interrupt %i failed. (err: %d)"
742 " But bravely continuing on..\n",
749 /* delete all emulated config registers */
750 xen_pt_config_delete(s
);
752 xen_pt_unregister_vga_regions(host_dev
);
754 if (s
->listener_set
) {
755 memory_listener_unregister(&s
->memory_listener
);
756 memory_listener_unregister(&s
->io_listener
);
757 s
->listener_set
= false;
759 if (!xen_host_pci_device_closed(&s
->real_device
)) {
760 xen_host_pci_device_put(&s
->real_device
);
765 static void xen_pt_realize(PCIDevice
*d
, Error
**errp
)
767 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
769 uint8_t machine_irq
= 0, scratch
;
771 int pirq
= XEN_PT_UNASSIGNED_PIRQ
;
774 /* register real device */
775 XEN_PT_LOG(d
, "Assigning real physical device %02x:%02x.%d"
777 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
,
780 xen_host_pci_device_get(&s
->real_device
,
781 s
->hostaddr
.domain
, s
->hostaddr
.bus
,
782 s
->hostaddr
.slot
, s
->hostaddr
.function
,
785 error_append_hint(&err
, "Failed to \"open\" the real pci device");
786 error_propagate(errp
, err
);
790 s
->is_virtfn
= s
->real_device
.is_virtfn
;
792 XEN_PT_LOG(d
, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n",
793 s
->real_device
.domain
, s
->real_device
.bus
,
794 s
->real_device
.dev
, s
->real_device
.func
);
797 /* Initialize virtualized PCI configuration (Extended 256 Bytes) */
798 memset(d
->config
, 0, PCI_CONFIG_SPACE_SIZE
);
800 s
->memory_listener
= xen_pt_memory_listener
;
801 s
->io_listener
= xen_pt_io_listener
;
803 /* Setup VGA bios for passthrough GFX */
804 if ((s
->real_device
.domain
== 0) && (s
->real_device
.bus
== 0) &&
805 (s
->real_device
.dev
== 2) && (s
->real_device
.func
== 0)) {
806 if (!is_igd_vga_passthrough(&s
->real_device
)) {
807 error_setg(errp
, "Need to enable igd-passthru if you're trying"
808 " to passthrough IGD GFX");
809 xen_host_pci_device_put(&s
->real_device
);
813 xen_pt_setup_vga(s
, &s
->real_device
, &err
);
815 error_append_hint(&err
, "Setup VGA BIOS of passthrough"
817 error_propagate(errp
, err
);
818 xen_host_pci_device_put(&s
->real_device
);
822 /* Register ISA bridge for passthrough GFX. */
823 xen_igd_passthrough_isa_bridge_create(s
, &s
->real_device
);
826 /* Handle real device's MMIO/PIO BARs */
827 xen_pt_register_regions(s
, &cmd
);
829 /* reinitialize each config register to be emulated */
830 xen_pt_config_init(s
, &err
);
832 error_append_hint(&err
, "PCI Config space initialisation failed");
833 error_propagate(errp
, err
);
839 rc
= xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &scratch
);
841 error_setg_errno(errp
, errno
, "Failed to read PCI_INTERRUPT_PIN");
845 XEN_PT_LOG(d
, "no pin interrupt\n");
849 machine_irq
= s
->real_device
.irq
;
850 if (machine_irq
== 0) {
851 XEN_PT_LOG(d
, "machine irq is 0\n");
852 cmd
|= PCI_COMMAND_INTX_DISABLE
;
856 rc
= xc_physdev_map_pirq(xen_xc
, xen_domid
, machine_irq
, &pirq
);
858 error_setg_errno(errp
, errno
, "Mapping machine irq %u to"
859 " pirq %i failed", machine_irq
, pirq
);
861 /* Disable PCI intx assertion (turn on bit10 of devctl) */
862 cmd
|= PCI_COMMAND_INTX_DISABLE
;
867 s
->machine_irq
= pirq
;
868 xen_pt_mapped_machine_irq
[machine_irq
]++;
871 /* bind machine_irq to device */
872 if (machine_irq
!= 0) {
873 uint8_t e_intx
= xen_pt_pci_intx(s
);
875 rc
= xc_domain_bind_pt_pci_irq(xen_xc
, xen_domid
, machine_irq
,
880 error_setg_errno(errp
, errno
, "Binding of interrupt %u failed",
883 /* Disable PCI intx assertion (turn on bit10 of devctl) */
884 cmd
|= PCI_COMMAND_INTX_DISABLE
;
885 xen_pt_mapped_machine_irq
[machine_irq
]--;
887 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
888 if (xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
)) {
889 error_setg_errno(errp
, errno
, "Unmapping of machine"
890 " interrupt %u failed", machine_irq
);
901 rc
= xen_host_pci_get_word(&s
->real_device
, PCI_COMMAND
, &val
);
903 error_setg_errno(errp
, errno
, "Failed to read PCI_COMMAND");
907 rc
= xen_host_pci_set_word(&s
->real_device
, PCI_COMMAND
, val
);
909 error_setg_errno(errp
, errno
, "Failed to write PCI_COMMAND"
916 memory_listener_register(&s
->memory_listener
, &address_space_memory
);
917 memory_listener_register(&s
->io_listener
, &address_space_io
);
918 s
->listener_set
= true;
920 "Real physical device %02x:%02x.%d registered successfully\n",
921 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
);
926 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
927 object_unparent(OBJECT(&s
->bar
[i
]));
929 object_unparent(OBJECT(&s
->rom
));
935 static void xen_pt_unregister_device(PCIDevice
*d
)
940 static Property xen_pci_passthrough_properties
[] = {
941 DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState
, hostaddr
),
942 DEFINE_PROP_BOOL("permissive", XenPCIPassthroughState
, permissive
, false),
943 DEFINE_PROP_END_OF_LIST(),
946 static void xen_pci_passthrough_instance_init(Object
*obj
)
948 /* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
949 * line, therefore, no need to wait to realize like other devices */
950 PCI_DEVICE(obj
)->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
953 static void xen_pci_passthrough_class_init(ObjectClass
*klass
, void *data
)
955 DeviceClass
*dc
= DEVICE_CLASS(klass
);
956 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
958 k
->realize
= xen_pt_realize
;
959 k
->exit
= xen_pt_unregister_device
;
960 k
->config_read
= xen_pt_pci_read_config
;
961 k
->config_write
= xen_pt_pci_write_config
;
962 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
963 dc
->desc
= "Assign an host PCI device with Xen";
964 dc
->props
= xen_pci_passthrough_properties
;
967 static void xen_pci_passthrough_finalize(Object
*obj
)
969 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(obj
);
971 xen_pt_msix_delete(s
);
974 static const TypeInfo xen_pci_passthrough_info
= {
975 .name
= TYPE_XEN_PT_DEVICE
,
976 .parent
= TYPE_PCI_DEVICE
,
977 .instance_size
= sizeof(XenPCIPassthroughState
),
978 .instance_finalize
= xen_pci_passthrough_finalize
,
979 .class_init
= xen_pci_passthrough_class_init
,
980 .instance_init
= xen_pci_passthrough_instance_init
,
981 .interfaces
= (InterfaceInfo
[]) {
982 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
983 { INTERFACE_PCIE_DEVICE
},
988 static void xen_pci_passthrough_register_types(void)
990 type_register_static(&xen_pci_passthrough_info
);
993 type_init(xen_pci_passthrough_register_types
)