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 <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"
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
, ...)
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
);
83 static int xen_pt_pci_config_access_check(PCIDevice
*d
, uint32_t addr
, int len
)
85 /* check offset range */
87 XEN_PT_ERR(d
, "Failed to access register with offset exceeding 0xFF. "
88 "(addr: 0x%02x, len: %d)\n", addr
, len
);
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
);
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
);
109 int xen_pt_bar_offset_to_index(uint32_t offset
)
113 /* check Exp ROM BAR */
114 if (offset
== PCI_ROM_ADDRESS
) {
118 /* calculate BAR index */
119 index
= (offset
- PCI_BASE_ADDRESS_0
) >> 2;
120 if (index
>= PCI_NUM_REGIONS
) {
127 static uint32_t xen_pt_pci_read_config(PCIDevice
*d
, uint32_t addr
, int len
)
129 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
131 XenPTRegGroup
*reg_grp_entry
= NULL
;
132 XenPTReg
*reg_entry
= NULL
;
135 uint32_t find_addr
= addr
;
137 if (xen_pt_pci_config_access_check(d
, addr
, len
)) {
141 /* find register group entry */
142 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
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 */
152 /* read I/O device register value */
153 rc
= xen_host_pci_get_block(&s
->real_device
, addr
, (uint8_t *)&val
, len
);
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
) {
165 /* adjust the read value to appropriate CFC-CFF window */
166 val
<<= (addr
& 3) << 3;
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
);
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 */
186 rc
= reg
->u
.b
.read(s
, reg_entry
, ptr_val
, valid_mask
);
191 rc
= reg
->u
.w
.read(s
, reg_entry
,
192 (uint16_t *)ptr_val
, valid_mask
);
196 if (reg
->u
.dw
.read
) {
197 rc
= reg
->u
.dw
.read(s
, reg_entry
,
198 (uint32_t *)ptr_val
, valid_mask
);
204 xen_shutdown_fatal_error("Internal error: Invalid read "
205 "emulation. (%s, rc: %d)\n",
210 /* calculate next address to find */
211 emul_len
-= reg
->size
;
213 find_addr
= real_offset
+ reg
->size
;
216 /* nothing to do with passthrough type register,
217 * continue to find next byte */
223 /* need to shift back before returning them to pci bus emulator */
224 val
>>= ((addr
& 3) << 3);
227 XEN_PT_LOG_CONFIG(d
, addr
, val
, len
);
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
);
236 XenPTRegGroup
*reg_grp_entry
= NULL
;
238 uint32_t read_val
= 0, wb_mask
;
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
)) {
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)) {
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",
267 /* find register group entry */
268 reg_grp_entry
= xen_pt_find_reg_grp(s
, addr
);
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
);
279 rc
= xen_host_pci_get_block(&s
->real_device
, addr
,
280 (uint8_t *)&read_val
, len
);
282 XEN_PT_ERR(d
, "pci_read_block failed. return value: %d.\n", rc
);
283 memset(&read_val
, 0xff, len
);
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
) {
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;
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
);
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 */
330 if (reg
->u
.b
.write
) {
331 rc
= reg
->u
.b
.write(s
, reg_entry
, ptr_val
,
332 read_val
>> ((real_offset
& 3) << 3),
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)),
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)),
353 xen_shutdown_fatal_error("Internal error: Invalid write"
354 " emulation. (%s, rc: %d)\n",
359 /* calculate next address to find */
360 emul_len
-= reg
->size
;
362 find_addr
= real_offset
+ reg
->size
;
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
382 /* need to shift back before passing them to xen_host_pci_set_block. */
383 val
>>= (addr
& 3) << 3;
385 memory_region_transaction_commit();
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)) {
405 } while (wb_mask
& 0xff);
406 rc
= xen_host_pci_set_block(&s
->real_device
, addr
+ index
,
407 (uint8_t *)&val
+ index
, len
);
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
,
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",
427 static void xen_pt_bar_write(void *o
, hwaddr addr
, uint64_t val
,
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",
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
)
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
];
452 if (r
->base_addr
== 0 || r
->size
== 0) {
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
;
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
)) {
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
,
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
);
512 static int xen_pt_bar_from_region(XenPCIPassthroughState
*s
, MemoryRegion
*mr
)
516 for (i
= 0; i
< PCI_NUM_REGIONS
- 1; i
++) {
517 if (mr
== &s
->bar
[i
]) {
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
532 * The return value indicates if the region is overlappsed */
533 struct CheckBarArgs
{
534 XenPCIPassthroughState
*s
;
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
;
547 if (d
->devfn
== s
->dev
.devfn
) {
551 /* xxx: This ignores bridges. */
552 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
553 const PCIIORegion
*r
= &d
->io_regions
[i
];
558 if ((type
& PCI_BASE_ADDRESS_SPACE_IO
)
559 != (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
)) {
563 if (ranges_overlap(arg
->addr
, arg
->size
, r
->addr
, r
->size
)) {
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
);
574 static void xen_pt_region_update(XenPCIPassthroughState
*s
,
575 MemoryRegionSection
*sec
, bool adding
)
577 PCIDevice
*d
= &s
->dev
;
578 MemoryRegion
*mr
= sec
->mr
;
581 int op
= adding
? DPCI_ADD_MAPPING
: DPCI_REMOVE_MAPPING
;
582 struct CheckBarArgs args
= {
584 .addr
= sec
->offset_within_address_space
,
585 .size
= int128_get64(sec
->size
),
589 bar
= xen_pt_bar_from_region(s
, mr
);
590 if (bar
== -1 && (!s
->msix
|| &s
->msix
->mmio
!= mr
)) {
594 if (s
->msix
&& &s
->msix
->mmio
== mr
) {
596 s
->msix
->mmio_base_addr
= sec
->offset_within_address_space
;
597 rc
= xen_pt_msix_update_remap(s
, s
->msix
->bar_index
);
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
);
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
,
620 XEN_PT_ERR(d
, "%s ioport mapping failed! (err: %i)\n",
621 adding
? "create new" : "remove old", errno
);
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),
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
,
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
,
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
,
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
,
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
,
682 static const MemoryListener xen_pt_io_listener
= {
683 .region_add
= xen_pt_io_region_add
,
684 .region_del
= xen_pt_io_region_del
,
689 xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState
*s
,
690 XenHostPCIDevice
*dev
)
693 PCIDevice
*d
= &s
->dev
;
695 gpu_dev_id
= dev
->device_id
;
696 igd_passthrough_isa_bridge_create(d
->bus
, gpu_dev_id
);
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
;
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
,
713 PCI_SLOT(s
->dev
.devfn
),
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. */
726 xen_pt_msi_disable(s
);
729 xen_pt_msix_disable(s
);
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
);
739 XEN_PT_ERR(d
, "unmapping of interrupt %i failed. (err: %d)"
740 " But bravely continuing on..\n",
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
);
763 static int xen_pt_initfn(PCIDevice
*d
)
765 XenPCIPassthroughState
*s
= XEN_PT_DEVICE(d
);
767 uint8_t machine_irq
= 0, scratch
;
769 int pirq
= XEN_PT_UNASSIGNED_PIRQ
;
771 /* register real device */
772 XEN_PT_LOG(d
, "Assigning real physical device %02x:%02x.%d"
774 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
,
777 rc
= xen_host_pci_device_get(&s
->real_device
,
778 s
->hostaddr
.domain
, s
->hostaddr
.bus
,
779 s
->hostaddr
.slot
, s
->hostaddr
.function
);
781 XEN_PT_ERR(d
, "Failed to \"open\" the real pci device. rc: %i\n", rc
);
785 s
->is_virtfn
= s
->real_device
.is_virtfn
;
787 XEN_PT_LOG(d
, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n",
788 s
->real_device
.domain
, s
->real_device
.bus
,
789 s
->real_device
.dev
, s
->real_device
.func
);
792 /* Initialize virtualized PCI configuration (Extended 256 Bytes) */
793 memset(d
->config
, 0, PCI_CONFIG_SPACE_SIZE
);
795 s
->memory_listener
= xen_pt_memory_listener
;
796 s
->io_listener
= xen_pt_io_listener
;
798 /* Setup VGA bios for passthrough GFX */
799 if ((s
->real_device
.domain
== 0) && (s
->real_device
.bus
== 0) &&
800 (s
->real_device
.dev
== 2) && (s
->real_device
.func
== 0)) {
801 if (!is_igd_vga_passthrough(&s
->real_device
)) {
802 XEN_PT_ERR(d
, "Need to enable igd-passthru if you're trying"
803 " to passthrough IGD GFX.\n");
804 xen_host_pci_device_put(&s
->real_device
);
808 if (xen_pt_setup_vga(s
, &s
->real_device
) < 0) {
809 XEN_PT_ERR(d
, "Setup VGA BIOS of passthrough GFX failed!\n");
810 xen_host_pci_device_put(&s
->real_device
);
814 /* Register ISA bridge for passthrough GFX. */
815 xen_igd_passthrough_isa_bridge_create(s
, &s
->real_device
);
818 /* Handle real device's MMIO/PIO BARs */
819 xen_pt_register_regions(s
, &cmd
);
821 /* reinitialize each config register to be emulated */
822 rc
= xen_pt_config_init(s
);
824 XEN_PT_ERR(d
, "PCI Config space initialisation failed.\n");
829 rc
= xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &scratch
);
831 XEN_PT_ERR(d
, "Failed to read PCI_INTERRUPT_PIN! (rc:%d)\n", rc
);
835 XEN_PT_LOG(d
, "no pin interrupt\n");
839 machine_irq
= s
->real_device
.irq
;
840 rc
= xc_physdev_map_pirq(xen_xc
, xen_domid
, machine_irq
, &pirq
);
843 XEN_PT_ERR(d
, "Mapping machine irq %u to pirq %i failed, (err: %d)\n",
844 machine_irq
, pirq
, errno
);
846 /* Disable PCI intx assertion (turn on bit10 of devctl) */
847 cmd
|= PCI_COMMAND_INTX_DISABLE
;
852 s
->machine_irq
= pirq
;
853 xen_pt_mapped_machine_irq
[machine_irq
]++;
856 /* bind machine_irq to device */
857 if (machine_irq
!= 0) {
858 uint8_t e_intx
= xen_pt_pci_intx(s
);
860 rc
= xc_domain_bind_pt_pci_irq(xen_xc
, xen_domid
, machine_irq
,
865 XEN_PT_ERR(d
, "Binding of interrupt %i failed! (err: %d)\n",
868 /* Disable PCI intx assertion (turn on bit10 of devctl) */
869 cmd
|= PCI_COMMAND_INTX_DISABLE
;
870 xen_pt_mapped_machine_irq
[machine_irq
]--;
872 if (xen_pt_mapped_machine_irq
[machine_irq
] == 0) {
873 if (xc_physdev_unmap_pirq(xen_xc
, xen_domid
, machine_irq
)) {
874 XEN_PT_ERR(d
, "Unmapping of machine interrupt %i failed!"
875 " (err: %d)\n", machine_irq
, errno
);
886 rc
= xen_host_pci_get_word(&s
->real_device
, PCI_COMMAND
, &val
);
888 XEN_PT_ERR(d
, "Failed to read PCI_COMMAND! (rc: %d)\n", rc
);
892 rc
= xen_host_pci_set_word(&s
->real_device
, PCI_COMMAND
, val
);
894 XEN_PT_ERR(d
, "Failed to write PCI_COMMAND val=0x%x!(rc: %d)\n",
901 memory_listener_register(&s
->memory_listener
, &s
->dev
.bus_master_as
);
902 memory_listener_register(&s
->io_listener
, &address_space_io
);
903 s
->listener_set
= true;
905 "Real physical device %02x:%02x.%d registered successfully!\n",
906 s
->hostaddr
.bus
, s
->hostaddr
.slot
, s
->hostaddr
.function
);
916 static void xen_pt_unregister_device(PCIDevice
*d
)
921 static Property xen_pci_passthrough_properties
[] = {
922 DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState
, hostaddr
),
923 DEFINE_PROP_BOOL("permissive", XenPCIPassthroughState
, permissive
, false),
924 DEFINE_PROP_END_OF_LIST(),
927 static void xen_pci_passthrough_class_init(ObjectClass
*klass
, void *data
)
929 DeviceClass
*dc
= DEVICE_CLASS(klass
);
930 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
932 k
->init
= xen_pt_initfn
;
933 k
->exit
= xen_pt_unregister_device
;
934 k
->config_read
= xen_pt_pci_read_config
;
935 k
->config_write
= xen_pt_pci_write_config
;
936 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
937 dc
->desc
= "Assign an host PCI device with Xen";
938 dc
->props
= xen_pci_passthrough_properties
;
941 static const TypeInfo xen_pci_passthrough_info
= {
942 .name
= TYPE_XEN_PT_DEVICE
,
943 .parent
= TYPE_PCI_DEVICE
,
944 .instance_size
= sizeof(XenPCIPassthroughState
),
945 .class_init
= xen_pci_passthrough_class_init
,
948 static void xen_pci_passthrough_register_types(void)
950 type_register_static(&xen_pci_passthrough_info
);
953 type_init(xen_pci_passthrough_register_types
)