2 * Copyright (c) 2007, Neocleus Corporation.
3 * Copyright (c) 2007, Intel Corporation.
5 * SPDX-License-Identifier: GPL-2.0-only
7 * Alex Novik <alex@neocleus.com>
8 * Allen Kay <allen.m.kay@intel.com>
9 * Guy Zana <guy@neocleus.com>
14 #include "hw/xen/xen_native.h"
15 #include "xen-host-pci-device.h"
16 #include "qom/object.h"
18 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...) G_GNUC_PRINTF(2, 3);
20 #define XEN_PT_ERR(d, _f, _a...) xen_pt_log(d, "%s: Error: "_f, __func__, ##_a)
22 #ifdef XEN_PT_LOGGING_ENABLED
23 # define XEN_PT_LOG(d, _f, _a...) xen_pt_log(d, "%s: " _f, __func__, ##_a)
24 # define XEN_PT_WARN(d, _f, _a...) \
25 xen_pt_log(d, "%s: Warning: "_f, __func__, ##_a)
27 # define XEN_PT_LOG(d, _f, _a...)
28 # define XEN_PT_WARN(d, _f, _a...)
31 #ifdef XEN_PT_DEBUG_PCI_CONFIG_ACCESS
32 # define XEN_PT_LOG_CONFIG(d, addr, val, len) \
33 xen_pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \
34 __func__, addr, val, len)
36 # define XEN_PT_LOG_CONFIG(d, addr, val, len)
41 #define XEN_PFN(x) ((x) >> XC_PAGE_SHIFT)
43 typedef const struct XenPTRegInfo XenPTRegInfo
;
44 typedef struct XenPTReg XenPTReg
;
47 #define TYPE_XEN_PT_DEVICE "xen-pci-passthrough"
48 OBJECT_DECLARE_SIMPLE_TYPE(XenPCIPassthroughState
, XEN_PT_DEVICE
)
50 #define XEN_PT_DEVICE_CLASS(klass) \
51 OBJECT_CLASS_CHECK(XenPTDeviceClass, klass, TYPE_XEN_PT_DEVICE)
52 #define XEN_PT_DEVICE_GET_CLASS(obj) \
53 OBJECT_GET_CLASS(XenPTDeviceClass, obj, TYPE_XEN_PT_DEVICE)
55 typedef void (*XenPTQdevRealize
)(DeviceState
*qdev
, Error
**errp
);
57 typedef struct XenPTDeviceClass
{
58 PCIDeviceClass parent_class
;
59 XenPTQdevRealize pci_qdev_realize
;
62 /* function type for config reg */
63 typedef int (*xen_pt_conf_reg_init
)
64 (XenPCIPassthroughState
*, XenPTRegInfo
*, uint32_t real_offset
,
66 typedef int (*xen_pt_conf_dword_write
)
67 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
68 uint32_t *val
, uint32_t dev_value
, uint32_t valid_mask
);
69 typedef int (*xen_pt_conf_word_write
)
70 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
71 uint16_t *val
, uint16_t dev_value
, uint16_t valid_mask
);
72 typedef int (*xen_pt_conf_byte_write
)
73 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
74 uint8_t *val
, uint8_t dev_value
, uint8_t valid_mask
);
75 typedef int (*xen_pt_conf_dword_read
)
76 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
77 uint32_t *val
, uint32_t valid_mask
);
78 typedef int (*xen_pt_conf_word_read
)
79 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
80 uint16_t *val
, uint16_t valid_mask
);
81 typedef int (*xen_pt_conf_byte_read
)
82 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
83 uint8_t *val
, uint8_t valid_mask
);
85 #define XEN_PT_BAR_ALLF 0xFFFFFFFF
86 #define XEN_PT_BAR_UNMAPPED (-1)
88 #define XEN_PCI_CAP_MAX 48
90 #define XEN_PCI_INTEL_OPREGION 0xfc
92 #define XEN_PCI_IGD_DOMAIN 0
93 #define XEN_PCI_IGD_BUS 0
94 #define XEN_PCI_IGD_DEV 2
95 #define XEN_PCI_IGD_FN 0
96 #define XEN_PCI_IGD_SLOT_MASK \
97 (1UL << PCI_SLOT(PCI_DEVFN(XEN_PCI_IGD_DEV, XEN_PCI_IGD_FN)))
100 XEN_PT_GRP_TYPE_HARDWIRED
= 0, /* 0 Hardwired reg group */
101 XEN_PT_GRP_TYPE_EMU
, /* emul reg group */
102 } XenPTRegisterGroupType
;
105 XEN_PT_BAR_FLAG_MEM
= 0, /* Memory type BAR */
106 XEN_PT_BAR_FLAG_IO
, /* I/O type BAR */
107 XEN_PT_BAR_FLAG_UPPER
, /* upper 64bit BAR */
108 XEN_PT_BAR_FLAG_UNUSED
, /* unused BAR */
112 typedef struct XenPTRegion
{
114 XenPTBarFlag bar_flag
;
115 /* Translation of the emulated address */
123 /* XenPTRegInfo declaration
124 * - only for emulated register (either a part or whole bit).
125 * - for passthrough register that need special behavior (like interacting with
126 * other component), set emu_mask to all 0 and specify r/w func properly.
127 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
130 /* emulated register information */
131 struct XenPTRegInfo
{
135 /* reg reserved field mask (ON:reserved, OFF:defined) */
137 /* reg read only field mask (ON:RO/ROS, OFF:other) */
139 /* reg read/write-1-clear field mask (ON:RW1C/RW1CS, OFF:other) */
141 /* reg emulate field mask (ON:emu, OFF:passthrough) */
143 xen_pt_conf_reg_init init
;
144 /* read/write function pointer
145 * for double_word/word/byte size */
148 xen_pt_conf_dword_write write
;
149 xen_pt_conf_dword_read read
;
152 xen_pt_conf_word_write write
;
153 xen_pt_conf_word_read read
;
156 xen_pt_conf_byte_write write
;
157 xen_pt_conf_byte_read read
;
162 /* emulated register management */
164 QLIST_ENTRY(XenPTReg
) entries
;
170 } ptr
; /* pointer to dev.config. */
173 typedef const struct XenPTRegGroupInfo XenPTRegGroupInfo
;
175 /* emul reg group size initialize method */
176 typedef int (*xen_pt_reg_size_init_fn
)
177 (XenPCIPassthroughState
*, XenPTRegGroupInfo
*,
178 uint32_t base_offset
, uint8_t *size
);
180 /* emulated register group information */
181 struct XenPTRegGroupInfo
{
183 XenPTRegisterGroupType grp_type
;
185 xen_pt_reg_size_init_fn size_init
;
186 XenPTRegInfo
*emu_regs
;
189 /* emul register group management table */
190 typedef struct XenPTRegGroup
{
191 QLIST_ENTRY(XenPTRegGroup
) entries
;
192 XenPTRegGroupInfo
*reg_grp
;
193 uint32_t base_offset
;
195 QLIST_HEAD(, XenPTReg
) reg_tbl_list
;
199 #define XEN_PT_UNASSIGNED_PIRQ (-1)
200 typedef struct XenPTMSI
{
202 uint32_t addr_lo
; /* guest message address */
203 uint32_t addr_hi
; /* guest message upper address */
204 uint16_t data
; /* guest message data */
205 uint32_t ctrl_offset
; /* saved control offset */
206 uint32_t mask
; /* guest mask bits */
207 int pirq
; /* guest pirq corresponding */
208 bool initialized
; /* when guest MSI is initialized */
209 bool mapped
; /* when pirq is mapped */
212 typedef struct XenPTMSIXEntry
{
217 bool updated
; /* indicate whether MSI ADDR or DATA is updated */
219 typedef struct XenPTMSIX
{
220 uint32_t ctrl_offset
;
226 uint32_t table_offset_adjust
; /* page align mmap */
227 uint64_t mmio_base_addr
;
229 void *phys_iomem_base
;
230 XenPTMSIXEntry msix_entry
[];
233 struct XenPCIPassthroughState
{
236 PCIHostDeviceAddress hostaddr
;
239 bool permissive_warned
;
240 XenHostPCIDevice real_device
;
241 XenPTRegion bases
[PCI_NUM_REGIONS
]; /* Access regions */
242 QLIST_HEAD(, XenPTRegGroup
) reg_grps
;
244 uint32_t machine_irq
;
249 MemoryRegion bar
[PCI_NUM_REGIONS
- 1];
252 MemoryListener memory_listener
;
253 MemoryListener io_listener
;
257 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
);
258 void xen_pt_config_delete(XenPCIPassthroughState
*s
);
259 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
);
260 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
);
261 int xen_pt_bar_offset_to_index(uint32_t offset
);
263 static inline pcibus_t
xen_pt_get_emul_size(XenPTBarFlag flag
, pcibus_t r_size
)
265 /* align resource size (memory type only) */
266 if (flag
== XEN_PT_BAR_FLAG_MEM
) {
267 return (r_size
+ XC_PAGE_SIZE
- 1) & XC_PAGE_MASK
;
274 /* The PCI Local Bus Specification, Rev. 3.0,
275 * Section 6.2.4 Miscellaneous Registers, pp 223
276 * outlines 5 valid values for the interrupt pin (intx).
277 * 0: For devices (or device functions) that don't use an interrupt in
283 * Xen uses the following 4 values for intx
289 * Observing that these list of values are not the same, xen_pt_pci_read_intx()
290 * uses the following mapping from hw to xen values.
291 * This seems to reflect the current usage within Xen.
293 * PCI hardware | Xen | Notes
294 * ----------------+-----+----------------------------------------------------
295 * 0 | 0 | No interrupt
300 * any other value | 0 | This should never happen, log error message
303 static inline uint8_t xen_pt_pci_read_intx(XenPCIPassthroughState
*s
)
306 xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &v
);
310 static inline uint8_t xen_pt_pci_intx(XenPCIPassthroughState
*s
)
312 uint8_t r_val
= xen_pt_pci_read_intx(s
);
314 XEN_PT_LOG(&s
->dev
, "intx=%i\n", r_val
);
315 if (r_val
< 1 || r_val
> 4) {
316 XEN_PT_LOG(&s
->dev
, "Interrupt pin read from hardware is out of range:"
317 " value=%i, acceptable range is 1 - 4\n", r_val
);
320 /* Note that if s.real_device.config_fd is closed we make 0xff. */
328 int xen_pt_msi_setup(XenPCIPassthroughState
*s
);
329 int xen_pt_msi_update(XenPCIPassthroughState
*d
);
330 void xen_pt_msi_disable(XenPCIPassthroughState
*s
);
332 int xen_pt_msix_init(XenPCIPassthroughState
*s
, uint32_t base
);
333 void xen_pt_msix_delete(XenPCIPassthroughState
*s
);
334 void xen_pt_msix_unmap(XenPCIPassthroughState
*s
);
335 int xen_pt_msix_update(XenPCIPassthroughState
*s
);
336 int xen_pt_msix_update_remap(XenPCIPassthroughState
*s
, int bar_index
);
337 void xen_pt_msix_disable(XenPCIPassthroughState
*s
);
339 static inline bool xen_pt_has_msix_mapping(XenPCIPassthroughState
*s
, int bar
)
341 return s
->msix
&& s
->msix
->bar_index
== bar
;
344 void *pci_assign_dev_load_option_rom(PCIDevice
*dev
, int *size
,
345 unsigned int domain
, unsigned int bus
,
346 unsigned int slot
, unsigned int function
);
347 int xen_pt_register_vga_regions(XenHostPCIDevice
*dev
);
348 int xen_pt_unregister_vga_regions(XenHostPCIDevice
*dev
);
349 void xen_pt_setup_vga(XenPCIPassthroughState
*s
, XenHostPCIDevice
*dev
,
351 #endif /* XEN_PT_H */