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/qdev-properties-system.h"
62 #include "hw/xen/xen.h"
63 #include "hw/i386/pc.h"
64 #include "hw/xen/xen-legacy-backend.h"
66 #include "qemu/range.h"
68 static bool has_igd_gfx_passthru
;
70 bool xen_igd_gfx_pt_enabled(void)
72 return has_igd_gfx_passthru
;
75 void xen_igd_gfx_pt_set(bool value
, Error
**errp
)
77 has_igd_gfx_passthru
= value
;
80 #define XEN_PT_NR_IRQS (256)
81 static uint8_t xen_pt_mapped_machine_irq
[XEN_PT_NR_IRQS
] = {0};
83 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...)
89 fprintf(stderr
, "[%02x:%02x.%d] ", pci_dev_bus_num(d
),
90 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
));
92 vfprintf(stderr
, f
, ap
);
98 static int xen_pt_pci_config_access_check(PCIDevice
*d
, uint32_t addr
, int len
)
100 /* check offset range */
102 XEN_PT_ERR(d
, "Failed to access register with offset exceeding 0xFF. "
103 "(addr: 0x%02x, len: %d)\n", addr
, len
);
107 /* check read size */
108 if ((len
!= 1) && (len
!= 2) && (len
!= 4)) {
109 XEN_PT_ERR(d
, "Failed to access register with invalid access length. "
110 "(addr: 0x%02x, len: %d)\n", addr
, len
);
114 /* check offset alignment */
115 if (addr
& (len
- 1)) {
116 XEN_PT_ERR(d
, "Failed to access register with invalid access size "
117 "alignment. (addr: 0x%02x, len: %d)\n", addr
, len
);
124 int xen_pt_bar_offset_to_index(uint32_t offset
)
128 /* check Exp ROM BAR */
129 if (offset
== PCI_ROM_ADDRESS
) {
133 /* calculate BAR index */
134 index
= (offset
- PCI_BASE_ADDRESS_0
) >> 2;
135 if (index
>= PCI_NUM_REGIONS
) {
142 static uint32_t xen_pt_pci_read_config(PCIDevice
*d
, uint32_t addr
, int len
)
144 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
146 XenPTRegGroup
*reg_grp_entry
= NULL
;
147 XenPTReg
*reg_entry
= NULL
;
150 uint32_t find_addr
= addr
;
152 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
156 /* find register group entry */
157 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
159 /* check 0-Hardwired register group */
160 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
161 /* no need to emulate, just return 0 */
167 /* read I/O device register value */
168 rc
= xen_host_pci_get_block(&s
->real_device
, addr
, (uint8_t *)&val
, len
);
170 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
171 memset(&val
, 0xff, len
);
174 /* just return the I/O device register value for
175 * passthrough type register group */
176 if (reg_grp_entry
== NULL
) {
180 /* adjust the read value to appropriate CFC-CFF window */
181 val
<<= (addr
& 3) << 3;
184 /* loop around the guest requested size */
185 while (emul_len
> 0) {
186 /* find register entry to be emulated */
187 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
189 XenPTRegInfo
*reg
= reg_entry
->reg
;
190 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
191 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
192 uint8_t *ptr_val
= NULL
;
194 valid_mask
<<= (find_addr
- real_offset
) << 3;
195 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
197 /* do emulation based on register size */
201 rc
= reg
->u
.b
.read(s
, reg_entry
, ptr_val
, valid_mask
);
206 rc
= reg
->u
.w
.read(s
, reg_entry
,
207 (uint16_t *)ptr_val
, valid_mask
);
211 if (reg
->u
.dw
.read
) {
212 rc
= reg
->u
.dw
.read(s
, reg_entry
,
213 (uint32_t *)ptr_val
, valid_mask
);
219 xen_shutdown_fatal_error("Internal error: Invalid read "
220 "emulation. (%s, rc: %d)\n",
225 /* calculate next address to find */
226 emul_len
-= reg
->size
;
228 find_addr
= real_offset
+ reg
->size
;
231 /* nothing to do with passthrough type register,
232 * continue to find next byte */
238 /* need to shift back before returning them to pci bus emulator */
239 val
>>= ((addr
& 3) << 3);
242 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
246 static void xen_pt_pci_write_config(PCIDevice
*d
, uint32_t addr
,
247 uint32_t val
, int len
)
249 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
251 XenPTRegGroup
*reg_grp_entry
= NULL
;
253 uint32_t read_val
= 0, wb_mask
;
255 XenPTReg
*reg_entry
= NULL
;
256 uint32_t find_addr
= addr
;
257 XenPTRegInfo
*reg
= NULL
;
258 bool wp_flag
= false;
260 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
264 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
266 /* check unused BAR register */
267 index
= xen_pt_bar_offset_to_index(addr
);
268 if ((index
>= 0) && (val
!= 0)) {
271 if (index
== PCI_ROM_SLOT
)
272 chk
|= (uint32_t)~PCI_ROM_ADDRESS_MASK
;
274 if ((chk
!= XEN_PT_BAR_ALLF
) &&
275 (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
)) {
276 XEN_PT_WARN(d
, "Guest attempt to set address to unused "
277 "Base Address Register. (addr: 0x%02x, len: %d)\n",
282 /* find register group entry */
283 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
285 /* check 0-Hardwired register group */
286 if (reg_grp_entry
->reg_grp
->grp_type
== XEN_PT_GRP_TYPE_HARDWIRED
) {
287 /* ignore silently */
288 XEN_PT_WARN(d
, "Access to 0-Hardwired register. "
289 "(addr: 0x%02x, len: %d)\n", addr
, len
);
294 rc
= xen_host_pci_get_block(&s
->real_device
, addr
,
295 (uint8_t *)&read_val
, len
);
297 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
298 memset(&read_val
, 0xff, len
);
301 wb_mask
= 0xFFFFFFFF >> ((4 - len
) << 3);
304 /* pass directly to the real device for passthrough type register group */
305 if (reg_grp_entry
== NULL
) {
306 if (!s
->permissive
) {
313 memory_region_transaction_begin();
314 pci_default_write_config(d
, addr
, val
, len
);
316 /* adjust the read and write value to appropriate CFC-CFF window */
317 read_val
<<= (addr
& 3) << 3;
318 val
<<= (addr
& 3) << 3;
321 /* loop around the guest requested size */
322 while (emul_len
> 0) {
323 /* find register entry to be emulated */
324 reg_entry
= xen_pt_find_reg(reg_grp_entry
, find_addr
);
326 reg
= reg_entry
->reg
;
327 uint32_t real_offset
= reg_grp_entry
->base_offset
+ reg
->offset
;
328 uint32_t valid_mask
= 0xFFFFFFFF >> ((4 - emul_len
) << 3);
329 uint8_t *ptr_val
= NULL
;
330 uint32_t wp_mask
= reg
->emu_mask
| reg
->ro_mask
;
332 valid_mask
<<= (find_addr
- real_offset
) << 3;
333 ptr_val
= (uint8_t *)&val
+ (real_offset
& 3);
334 if (!s
->permissive
) {
335 wp_mask
|= reg
->res_mask
;
337 if (wp_mask
== (0xFFFFFFFF >> ((4 - reg
->size
) << 3))) {
338 wb_mask
&= ~((wp_mask
>> ((find_addr
- real_offset
) << 3))
339 << ((len
- emul_len
) << 3));
342 /* do emulation based on register size */
345 if (reg
->u
.b
.write
) {
346 rc
= reg
->u
.b
.write(s
, reg_entry
, ptr_val
,
347 read_val
>> ((real_offset
& 3) << 3),
352 if (reg
->u
.w
.write
) {
353 rc
= reg
->u
.w
.write(s
, reg_entry
, (uint16_t *)ptr_val
,
354 (read_val
>> ((real_offset
& 3) << 3)),
359 if (reg
->u
.dw
.write
) {
360 rc
= reg
->u
.dw
.write(s
, reg_entry
, (uint32_t *)ptr_val
,
361 (read_val
>> ((real_offset
& 3) << 3)),
368 xen_shutdown_fatal_error("Internal error: Invalid write"
369 " emulation. (%s, rc: %d)\n",
374 /* calculate next address to find */
375 emul_len
-= reg
->size
;
377 find_addr
= real_offset
+ reg
->size
;
380 /* nothing to do with passthrough type register,
381 * continue to find next byte */
382 if (!s
->permissive
) {
383 wb_mask
&= ~(0xff << ((len
- emul_len
) << 3));
384 /* Unused BARs will make it here, but we don't want to issue
385 * warnings for writes to them (bogus writes get dealt with
397 /* need to shift back before passing them to xen_host_pci_set_block. */
398 val
>>= (addr
& 3) << 3;
400 memory_region_transaction_commit();
403 if (wp_flag
&& !s
->permissive_warned
) {
404 s
->permissive_warned
= true;
405 xen_pt_log(d
, "Write-back to unknown field 0x%02x (partially) inhibited (0x%0*x)\n",
406 addr
, len
* 2, wb_mask
);
407 xen_pt_log(d
, "If the device doesn't work, try enabling permissive mode\n");
408 xen_pt_log(d
, "(unsafe) and if it helps report the problem to xen-devel\n");
410 for (index
= 0; wb_mask
; index
+= len
) {
411 /* unknown regs are passed through */
412 while (!(wb_mask
& 0xff)) {
420 } while (wb_mask
& 0xff);
421 rc
= xen_host_pci_set_block(&s
->real_device
, addr
+ index
,
422 (uint8_t *)&val
+ index
, len
);
425 XEN_PT_ERR(d
, "xen_host_pci_set_block failed. return value: %d.\n", rc
);
430 /* register regions */
432 static uint64_t xen_pt_bar_read(void *o
, hwaddr addr
,
436 /* if this function is called, that probably means that there is a
437 * misconfiguration of the IOMMU. */
438 XEN_PT_ERR(d
, "Should not read BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
442 static void xen_pt_bar_write(void *o
, hwaddr addr
, uint64_t val
,
446 /* Same comment as xen_pt_bar_read function */
447 XEN_PT_ERR(d
, "Should not write BAR through QEMU. @0x"TARGET_FMT_plx
"\n",
451 static const MemoryRegionOps ops
= {
452 .endianness
= DEVICE_NATIVE_ENDIAN
,
453 .read
= xen_pt_bar_read
,
454 .write
= xen_pt_bar_write
,
457 static int xen_pt_register_regions(XenPCIPassthroughState
*s
, uint16_t *cmd
)
460 XenHostPCIDevice
*d
= &s
->real_device
;
462 /* Register PIO/MMIO BARs */
463 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
464 XenHostPCIIORegion
*r
= &d
->io_regions
[i
];
467 if (r
->base_addr
== 0 || r
->size
== 0) {
471 s
->bases
[i
].access
.u
= r
->base_addr
;
473 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
474 type
= PCI_BASE_ADDRESS_SPACE_IO
;
475 *cmd
|= PCI_COMMAND_IO
;
477 type
= PCI_BASE_ADDRESS_SPACE_MEMORY
;
478 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_PREFETCH
) {
479 type
|= PCI_BASE_ADDRESS_MEM_PREFETCH
;
481 if (r
->type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
) {
482 type
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
484 *cmd
|= PCI_COMMAND_MEMORY
;
487 memory_region_init_io(&s
->bar
[i
], OBJECT(s
), &ops
, &s
->dev
,
488 "xen-pci-pt-bar", r
->size
);
489 pci_register_bar(&s
->dev
, i
, type
, &s
->bar
[i
]);
491 XEN_PT_LOG(&s
->dev
, "IO region %i registered (size=0x%08"PRIx64
492 " base_addr=0x%08"PRIx64
" type: 0x%x)\n",
493 i
, r
->size
, r
->base_addr
, type
);
496 /* Register expansion ROM address */
497 if (d
->rom
.base_addr
&& d
->rom
.size
) {
498 uint32_t bar_data
= 0;
500 /* Re-set BAR reported by OS, otherwise ROM can't be read. */
501 if (xen_host_pci_get_long(d
, PCI_ROM_ADDRESS
, &bar_data
)) {
504 if ((bar_data
& PCI_ROM_ADDRESS_MASK
) == 0) {
505 bar_data
|= d
->rom
.base_addr
& PCI_ROM_ADDRESS_MASK
;
506 xen_host_pci_set_long(d
, PCI_ROM_ADDRESS
, bar_data
);
509 s
->bases
[PCI_ROM_SLOT
].access
.maddr
= d
->rom
.base_addr
;
511 memory_region_init_io(&s
->rom
, OBJECT(s
), &ops
, &s
->dev
,
512 "xen-pci-pt-rom", d
->rom
.size
);
513 pci_register_bar(&s
->dev
, PCI_ROM_SLOT
, PCI_BASE_ADDRESS_MEM_PREFETCH
,
516 XEN_PT_LOG(&s
->dev
, "Expansion ROM registered (size=0x%08"PRIx64
517 " base_addr=0x%08"PRIx64
")\n",
518 d
->rom
.size
, d
->rom
.base_addr
);
521 xen_pt_register_vga_regions(d
);
527 static int xen_pt_bar_from_region(XenPCIPassthroughState
*s
, MemoryRegion
*mr
)
531 for (i
= 0; i
< PCI_NUM_REGIONS
- 1; i
++) {
532 if (mr
== &s
->bar
[i
]) {
543 * This function checks if an io_region overlaps an io_region from another
544 * device. The io_region to check is provided with (addr, size and type)
545 * A callback can be provided and will be called for every region that is
547 * The return value indicates if the region is overlappsed */
548 struct CheckBarArgs
{
549 XenPCIPassthroughState
*s
;
555 static void xen_pt_check_bar_overlap(PCIBus
*bus
, PCIDevice
*d
, void *opaque
)
557 struct CheckBarArgs
*arg
= opaque
;
558 XenPCIPassthroughState
*s
= arg
->s
;
559 uint8_t type
= arg
->type
;
562 if (d
->devfn
== s
->dev
.devfn
) {
566 /* xxx: This ignores bridges. */
567 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
568 const PCIIORegion
*r
= &d
->io_regions
[i
];
573 if ((type
& PCI_BASE_ADDRESS_SPACE_IO
)
574 != (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
)) {
578 if (ranges_overlap(arg
->addr
, arg
->size
, r
->addr
, r
->size
)) {
580 "Overlapped to device [%02x:%02x.%d] Region: %i"
581 " (addr: 0x%"FMT_PCIBUS
", len: 0x%"FMT_PCIBUS
")\n",
582 pci_bus_num(bus
), PCI_SLOT(d
->devfn
),
583 PCI_FUNC(d
->devfn
), i
, r
->addr
, r
->size
);
589 static void xen_pt_region_update(XenPCIPassthroughState
*s
,
590 MemoryRegionSection
*sec
, bool adding
)
592 PCIDevice
*d
= &s
->dev
;
593 MemoryRegion
*mr
= sec
->mr
;
596 int op
= adding
? DPCI_ADD_MAPPING
: DPCI_REMOVE_MAPPING
;
597 struct CheckBarArgs args
= {
599 .addr
= sec
->offset_within_address_space
,
600 .size
= int128_get64(sec
->size
),
604 bar
= xen_pt_bar_from_region(s
, mr
);
605 if (bar
== -1 && (!s
->msix
|| &s
->msix
->mmio
!= mr
)) {
609 if (s
->msix
&& &s
->msix
->mmio
== mr
) {
611 s
->msix
->mmio_base_addr
= sec
->offset_within_address_space
;
612 rc
= xen_pt_msix_update_remap(s
, s
->msix
->bar_index
);
617 args
.type
= d
->io_regions
[bar
].type
;
618 pci_for_each_device(pci_get_bus(d
), pci_dev_bus_num(d
),
619 xen_pt_check_bar_overlap
, &args
);
621 XEN_PT_WARN(d
, "Region: %d (addr: 0x%"FMT_PCIBUS
622 ", len: 0x%"FMT_PCIBUS
") is overlapped.\n",
623 bar
, sec
->offset_within_address_space
,
624 int128_get64(sec
->size
));
627 if (d
->io_regions
[bar
].type
& PCI_BASE_ADDRESS_SPACE_IO
) {
628 uint32_t guest_port
= sec
->offset_within_address_space
;
629 uint32_t machine_port
= s
->bases
[bar
].access
.pio_base
;
630 uint32_t size
= int128_get64(sec
->size
);
631 rc
= xc_domain_ioport_mapping(xen_xc
, xen_domid
,
632 guest_port
, machine_port
, size
,
635 XEN_PT_ERR(d
, "%s ioport mapping failed! (err: %i)\n",
636 adding
? "create new" : "remove old", errno
);
639 pcibus_t guest_addr
= sec
->offset_within_address_space
;
640 pcibus_t machine_addr
= s
->bases
[bar
].access
.maddr
641 + sec
->offset_within_region
;
642 pcibus_t size
= int128_get64(sec
->size
);
643 rc
= xc_domain_memory_mapping(xen_xc
, xen_domid
,
644 XEN_PFN(guest_addr
+ XC_PAGE_SIZE
- 1),
645 XEN_PFN(machine_addr
+ XC_PAGE_SIZE
- 1),
646 XEN_PFN(size
+ XC_PAGE_SIZE
- 1),
649 XEN_PT_ERR(d
, "%s mem mapping failed! (err: %i)\n",
650 adding
? "create new" : "remove old", errno
);
655 static void xen_pt_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
657 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
660 memory_region_ref(sec
->mr
);
661 xen_pt_region_update(s
, sec
, true);
664 static void xen_pt_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
666 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
669 xen_pt_region_update(s
, sec
, false);
670 memory_region_unref(sec
->mr
);
673 static void xen_pt_io_region_add(MemoryListener
*l
, MemoryRegionSection
*sec
)
675 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
678 memory_region_ref(sec
->mr
);
679 xen_pt_region_update(s
, sec
, true);
682 static void xen_pt_io_region_del(MemoryListener
*l
, MemoryRegionSection
*sec
)
684 XenPCIPassthroughState
*s
= container_of(l
, XenPCIPassthroughState
,
687 xen_pt_region_update(s
, sec
, false);
688 memory_region_unref(sec
->mr
);
691 static const MemoryListener xen_pt_memory_listener
= {
692 .region_add
= xen_pt_region_add
,
693 .region_del
= xen_pt_region_del
,
697 static const MemoryListener xen_pt_io_listener
= {
698 .region_add
= xen_pt_io_region_add
,
699 .region_del
= xen_pt_io_region_del
,
704 xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState
*s
,
705 XenHostPCIDevice
*dev
)
708 PCIDevice
*d
= &s
->dev
;
710 gpu_dev_id
= dev
->device_id
;
711 igd_passthrough_isa_bridge_create(pci_get_bus(d
), gpu_dev_id
);
715 static void xen_pt_destroy(PCIDevice
*d
) {
717 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
718 XenHostPCIDevice
*host_dev
= &s
->real_device
;
719 uint8_t machine_irq
= s
->machine_irq
;
723 if (machine_irq
&& !xen_host_pci_device_closed(&s
->real_device
)) {
724 intx
= xen_pt_pci_intx(s
);
725 rc
= xc_domain_unbind_pt_irq(xen_xc
, xen_domid
, machine_irq
,
728 PCI_SLOT(s
->dev
.devfn
),
732 XEN_PT_ERR(d
, "unbinding of interrupt INT%c failed."
733 " (machine irq: %i, err: %d)"
734 " But bravely continuing on..\n",
735 'a' + intx
, machine_irq
, errno
);
739 /* N.B. xen_pt_config_delete takes care of freeing them. */
741 xen_pt_msi_disable(s
);
744 xen_pt_msix_disable(s
);
748 xen_pt_mapped_machine_irq
[machine_irq
]--;
750 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
751 rc
= xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
);
754 XEN_PT_ERR(d
, "unmapping of interrupt %i failed. (err: %d)"
755 " But bravely continuing on..\n",
762 /* delete all emulated config registers */
763 xen_pt_config_delete(s
);
765 xen_pt_unregister_vga_regions(host_dev
);
767 if (s
->listener_set
) {
768 memory_listener_unregister(&s
->memory_listener
);
769 memory_listener_unregister(&s
->io_listener
);
770 s
->listener_set
= false;
772 if (!xen_host_pci_device_closed(&s
->real_device
)) {
773 xen_host_pci_device_put(&s
->real_device
);
778 static void xen_pt_realize(PCIDevice
*d
, Error
**errp
)
781 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
783 uint8_t machine_irq
= 0, scratch
;
785 int pirq
= XEN_PT_UNASSIGNED_PIRQ
;
787 /* register real device */
788 XEN_PT_LOG(d
, "Assigning real physical device %02x:%02x.%d"
790 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
,
793 xen_host_pci_device_get(&s
->real_device
,
794 s
->hostaddr
.domain
, s
->hostaddr
.bus
,
795 s
->hostaddr
.slot
, s
->hostaddr
.function
,
798 error_append_hint(errp
, "Failed to \"open\" the real pci device");
802 s
->is_virtfn
= s
->real_device
.is_virtfn
;
804 XEN_PT_LOG(d
, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n",
805 s
->real_device
.domain
, s
->real_device
.bus
,
806 s
->real_device
.dev
, s
->real_device
.func
);
809 /* Initialize virtualized PCI configuration (Extended 256 Bytes) */
810 memset(d
->config
, 0, PCI_CONFIG_SPACE_SIZE
);
812 s
->memory_listener
= xen_pt_memory_listener
;
813 s
->io_listener
= xen_pt_io_listener
;
815 /* Setup VGA bios for passthrough GFX */
816 if ((s
->real_device
.domain
== 0) && (s
->real_device
.bus
== 0) &&
817 (s
->real_device
.dev
== 2) && (s
->real_device
.func
== 0)) {
818 if (!is_igd_vga_passthrough(&s
->real_device
)) {
819 error_setg(errp
, "Need to enable igd-passthru if you're trying"
820 " to passthrough IGD GFX");
821 xen_host_pci_device_put(&s
->real_device
);
825 xen_pt_setup_vga(s
, &s
->real_device
, errp
);
827 error_append_hint(errp
, "Setup VGA BIOS of passthrough"
829 xen_host_pci_device_put(&s
->real_device
);
833 /* Register ISA bridge for passthrough GFX. */
834 xen_igd_passthrough_isa_bridge_create(s
, &s
->real_device
);
837 /* Handle real device's MMIO/PIO BARs */
838 xen_pt_register_regions(s
, &cmd
);
840 /* reinitialize each config register to be emulated */
841 xen_pt_config_init(s
, errp
);
843 error_append_hint(errp
, "PCI Config space initialisation failed");
849 rc
= xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &scratch
);
851 error_setg_errno(errp
, errno
, "Failed to read PCI_INTERRUPT_PIN");
855 XEN_PT_LOG(d
, "no pin interrupt\n");
859 machine_irq
= s
->real_device
.irq
;
860 if (machine_irq
== 0) {
861 XEN_PT_LOG(d
, "machine irq is 0\n");
862 cmd
|= PCI_COMMAND_INTX_DISABLE
;
866 rc
= xc_physdev_map_pirq(xen_xc
, xen_domid
, machine_irq
, &pirq
);
868 XEN_PT_ERR(d
, "Mapping machine irq %u to pirq %i failed, (err: %d)\n",
869 machine_irq
, pirq
, errno
);
871 /* Disable PCI intx assertion (turn on bit10 of devctl) */
872 cmd
|= PCI_COMMAND_INTX_DISABLE
;
877 s
->machine_irq
= pirq
;
878 xen_pt_mapped_machine_irq
[machine_irq
]++;
881 /* bind machine_irq to device */
882 if (machine_irq
!= 0) {
883 uint8_t e_intx
= xen_pt_pci_intx(s
);
885 rc
= xc_domain_bind_pt_pci_irq(xen_xc
, xen_domid
, machine_irq
,
890 XEN_PT_ERR(d
, "Binding of interrupt %i failed! (err: %d)\n",
893 /* Disable PCI intx assertion (turn on bit10 of devctl) */
894 cmd
|= PCI_COMMAND_INTX_DISABLE
;
895 xen_pt_mapped_machine_irq
[machine_irq
]--;
897 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
898 if (xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
)) {
899 XEN_PT_ERR(d
, "Unmapping of machine interrupt %i failed!"
900 " (err: %d)\n", machine_irq
, errno
);
911 rc
= xen_host_pci_get_word(&s
->real_device
, PCI_COMMAND
, &val
);
913 error_setg_errno(errp
, errno
, "Failed to read PCI_COMMAND");
917 rc
= xen_host_pci_set_word(&s
->real_device
, PCI_COMMAND
, val
);
919 error_setg_errno(errp
, errno
, "Failed to write PCI_COMMAND"
926 memory_listener_register(&s
->memory_listener
, &address_space_memory
);
927 memory_listener_register(&s
->io_listener
, &address_space_io
);
928 s
->listener_set
= true;
930 "Real physical device %02x:%02x.%d registered successfully\n",
931 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
);
936 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
937 object_unparent(OBJECT(&s
->bar
[i
]));
939 object_unparent(OBJECT(&s
->rom
));
945 static void xen_pt_unregister_device(PCIDevice
*d
)
950 static Property xen_pci_passthrough_properties
[] = {
951 DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState
, hostaddr
),
952 DEFINE_PROP_BOOL("permissive", XenPCIPassthroughState
, permissive
, false),
953 DEFINE_PROP_END_OF_LIST(),
956 static void xen_pci_passthrough_instance_init(Object
*obj
)
958 /* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
959 * line, therefore, no need to wait to realize like other devices */
960 PCI_DEVICE(obj
)->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
963 static void xen_pci_passthrough_class_init(ObjectClass
*klass
, void *data
)
965 DeviceClass
*dc
= DEVICE_CLASS(klass
);
966 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
968 k
->realize
= xen_pt_realize
;
969 k
->exit
= xen_pt_unregister_device
;
970 k
->config_read
= xen_pt_pci_read_config
;
971 k
->config_write
= xen_pt_pci_write_config
;
972 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
973 dc
->desc
= "Assign an host PCI device with Xen";
974 device_class_set_props(dc
, xen_pci_passthrough_properties
);
977 static void xen_pci_passthrough_finalize(Object
*obj
)
979 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(obj
);
981 xen_pt_msix_delete(s
);
984 static const TypeInfo xen_pci_passthrough_info
= {
985 .name
= TYPE_XEN_PT_DEVICE
,
986 .parent
= TYPE_PCI_DEVICE
,
987 .instance_size
= sizeof(XenPCIPassthroughState
),
988 .instance_finalize
= xen_pci_passthrough_finalize
,
989 .class_init
= xen_pci_passthrough_class_init
,
990 .instance_init
= xen_pci_passthrough_instance_init
,
991 .interfaces
= (InterfaceInfo
[]) {
992 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
993 { INTERFACE_PCIE_DEVICE
},
998 static void xen_pci_passthrough_register_types(void)
1000 type_register_static(&xen_pci_passthrough_info
);
1003 type_init(xen_pci_passthrough_register_types
)