4 #include "hw/xen/xen_common.h"
5 #include "hw/pci/pci.h"
6 #include "xen-host-pci-device.h"
7 #include "qom/object.h"
9 bool xen_igd_gfx_pt_enabled(void);
10 void xen_igd_gfx_pt_set(bool value
, Error
**errp
);
12 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...) G_GNUC_PRINTF(2, 3);
14 #define XEN_PT_ERR(d, _f, _a...) xen_pt_log(d, "%s: Error: "_f, __func__, ##_a)
16 #ifdef XEN_PT_LOGGING_ENABLED
17 # define XEN_PT_LOG(d, _f, _a...) xen_pt_log(d, "%s: " _f, __func__, ##_a)
18 # define XEN_PT_WARN(d, _f, _a...) \
19 xen_pt_log(d, "%s: Warning: "_f, __func__, ##_a)
21 # define XEN_PT_LOG(d, _f, _a...)
22 # define XEN_PT_WARN(d, _f, _a...)
25 #ifdef XEN_PT_DEBUG_PCI_CONFIG_ACCESS
26 # define XEN_PT_LOG_CONFIG(d, addr, val, len) \
27 xen_pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \
28 __func__, addr, val, len)
30 # define XEN_PT_LOG_CONFIG(d, addr, val, len)
35 #define XEN_PFN(x) ((x) >> XC_PAGE_SHIFT)
37 typedef const struct XenPTRegInfo XenPTRegInfo
;
38 typedef struct XenPTReg XenPTReg
;
41 #define TYPE_XEN_PT_DEVICE "xen-pci-passthrough"
42 OBJECT_DECLARE_SIMPLE_TYPE(XenPCIPassthroughState
, XEN_PT_DEVICE
)
44 uint32_t igd_read_opregion(XenPCIPassthroughState
*s
);
45 void igd_write_opregion(XenPCIPassthroughState
*s
, uint32_t val
);
47 /* function type for config reg */
48 typedef int (*xen_pt_conf_reg_init
)
49 (XenPCIPassthroughState
*, XenPTRegInfo
*, uint32_t real_offset
,
51 typedef int (*xen_pt_conf_dword_write
)
52 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
53 uint32_t *val
, uint32_t dev_value
, uint32_t valid_mask
);
54 typedef int (*xen_pt_conf_word_write
)
55 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
56 uint16_t *val
, uint16_t dev_value
, uint16_t valid_mask
);
57 typedef int (*xen_pt_conf_byte_write
)
58 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
59 uint8_t *val
, uint8_t dev_value
, uint8_t valid_mask
);
60 typedef int (*xen_pt_conf_dword_read
)
61 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
62 uint32_t *val
, uint32_t valid_mask
);
63 typedef int (*xen_pt_conf_word_read
)
64 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
65 uint16_t *val
, uint16_t valid_mask
);
66 typedef int (*xen_pt_conf_byte_read
)
67 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
68 uint8_t *val
, uint8_t valid_mask
);
70 #define XEN_PT_BAR_ALLF 0xFFFFFFFF
71 #define XEN_PT_BAR_UNMAPPED (-1)
73 #define XEN_PCI_CAP_MAX 48
75 #define XEN_PCI_INTEL_OPREGION 0xfc
78 XEN_PT_GRP_TYPE_HARDWIRED
= 0, /* 0 Hardwired reg group */
79 XEN_PT_GRP_TYPE_EMU
, /* emul reg group */
80 } XenPTRegisterGroupType
;
83 XEN_PT_BAR_FLAG_MEM
= 0, /* Memory type BAR */
84 XEN_PT_BAR_FLAG_IO
, /* I/O type BAR */
85 XEN_PT_BAR_FLAG_UPPER
, /* upper 64bit BAR */
86 XEN_PT_BAR_FLAG_UNUSED
, /* unused BAR */
90 typedef struct XenPTRegion
{
92 XenPTBarFlag bar_flag
;
93 /* Translation of the emulated address */
101 /* XenPTRegInfo declaration
102 * - only for emulated register (either a part or whole bit).
103 * - for passthrough register that need special behavior (like interacting with
104 * other component), set emu_mask to all 0 and specify r/w func properly.
105 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
108 /* emulated register information */
109 struct XenPTRegInfo
{
113 /* reg reserved field mask (ON:reserved, OFF:defined) */
115 /* reg read only field mask (ON:RO/ROS, OFF:other) */
117 /* reg read/write-1-clear field mask (ON:RW1C/RW1CS, OFF:other) */
119 /* reg emulate field mask (ON:emu, OFF:passthrough) */
121 xen_pt_conf_reg_init init
;
122 /* read/write function pointer
123 * for double_word/word/byte size */
126 xen_pt_conf_dword_write write
;
127 xen_pt_conf_dword_read read
;
130 xen_pt_conf_word_write write
;
131 xen_pt_conf_word_read read
;
134 xen_pt_conf_byte_write write
;
135 xen_pt_conf_byte_read read
;
140 /* emulated register management */
142 QLIST_ENTRY(XenPTReg
) entries
;
148 } ptr
; /* pointer to dev.config. */
151 typedef const struct XenPTRegGroupInfo XenPTRegGroupInfo
;
153 /* emul reg group size initialize method */
154 typedef int (*xen_pt_reg_size_init_fn
)
155 (XenPCIPassthroughState
*, XenPTRegGroupInfo
*,
156 uint32_t base_offset
, uint8_t *size
);
158 /* emulated register group information */
159 struct XenPTRegGroupInfo
{
161 XenPTRegisterGroupType grp_type
;
163 xen_pt_reg_size_init_fn size_init
;
164 XenPTRegInfo
*emu_regs
;
167 /* emul register group management table */
168 typedef struct XenPTRegGroup
{
169 QLIST_ENTRY(XenPTRegGroup
) entries
;
170 XenPTRegGroupInfo
*reg_grp
;
171 uint32_t base_offset
;
173 QLIST_HEAD(, XenPTReg
) reg_tbl_list
;
177 #define XEN_PT_UNASSIGNED_PIRQ (-1)
178 typedef struct XenPTMSI
{
180 uint32_t addr_lo
; /* guest message address */
181 uint32_t addr_hi
; /* guest message upper address */
182 uint16_t data
; /* guest message data */
183 uint32_t ctrl_offset
; /* saved control offset */
184 uint32_t mask
; /* guest mask bits */
185 int pirq
; /* guest pirq corresponding */
186 bool initialized
; /* when guest MSI is initialized */
187 bool mapped
; /* when pirq is mapped */
190 typedef struct XenPTMSIXEntry
{
195 bool updated
; /* indicate whether MSI ADDR or DATA is updated */
197 typedef struct XenPTMSIX
{
198 uint32_t ctrl_offset
;
204 uint32_t table_offset_adjust
; /* page align mmap */
205 uint64_t mmio_base_addr
;
207 void *phys_iomem_base
;
208 XenPTMSIXEntry msix_entry
[];
211 struct XenPCIPassthroughState
{
214 PCIHostDeviceAddress hostaddr
;
217 bool permissive_warned
;
218 XenHostPCIDevice real_device
;
219 XenPTRegion bases
[PCI_NUM_REGIONS
]; /* Access regions */
220 QLIST_HEAD(, XenPTRegGroup
) reg_grps
;
222 uint32_t machine_irq
;
227 MemoryRegion bar
[PCI_NUM_REGIONS
- 1];
230 MemoryListener memory_listener
;
231 MemoryListener io_listener
;
235 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
);
236 void xen_pt_config_delete(XenPCIPassthroughState
*s
);
237 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
);
238 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
);
239 int xen_pt_bar_offset_to_index(uint32_t offset
);
241 static inline pcibus_t
xen_pt_get_emul_size(XenPTBarFlag flag
, pcibus_t r_size
)
243 /* align resource size (memory type only) */
244 if (flag
== XEN_PT_BAR_FLAG_MEM
) {
245 return (r_size
+ XC_PAGE_SIZE
- 1) & XC_PAGE_MASK
;
252 /* The PCI Local Bus Specification, Rev. 3.0,
253 * Section 6.2.4 Miscellaneous Registers, pp 223
254 * outlines 5 valid values for the interrupt pin (intx).
255 * 0: For devices (or device functions) that don't use an interrupt in
261 * Xen uses the following 4 values for intx
267 * Observing that these list of values are not the same, xen_pt_pci_read_intx()
268 * uses the following mapping from hw to xen values.
269 * This seems to reflect the current usage within Xen.
271 * PCI hardware | Xen | Notes
272 * ----------------+-----+----------------------------------------------------
273 * 0 | 0 | No interrupt
278 * any other value | 0 | This should never happen, log error message
281 static inline uint8_t xen_pt_pci_read_intx(XenPCIPassthroughState
*s
)
284 xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &v
);
288 static inline uint8_t xen_pt_pci_intx(XenPCIPassthroughState
*s
)
290 uint8_t r_val
= xen_pt_pci_read_intx(s
);
292 XEN_PT_LOG(&s
->dev
, "intx=%i\n", r_val
);
293 if (r_val
< 1 || r_val
> 4) {
294 XEN_PT_LOG(&s
->dev
, "Interrupt pin read from hardware is out of range:"
295 " value=%i, acceptable range is 1 - 4\n", r_val
);
298 /* Note that if s.real_device.config_fd is closed we make 0xff. */
306 int xen_pt_msi_setup(XenPCIPassthroughState
*s
);
307 int xen_pt_msi_update(XenPCIPassthroughState
*d
);
308 void xen_pt_msi_disable(XenPCIPassthroughState
*s
);
310 int xen_pt_msix_init(XenPCIPassthroughState
*s
, uint32_t base
);
311 void xen_pt_msix_delete(XenPCIPassthroughState
*s
);
312 void xen_pt_msix_unmap(XenPCIPassthroughState
*s
);
313 int xen_pt_msix_update(XenPCIPassthroughState
*s
);
314 int xen_pt_msix_update_remap(XenPCIPassthroughState
*s
, int bar_index
);
315 void xen_pt_msix_disable(XenPCIPassthroughState
*s
);
317 static inline bool xen_pt_has_msix_mapping(XenPCIPassthroughState
*s
, int bar
)
319 return s
->msix
&& s
->msix
->bar_index
== bar
;
322 extern void *pci_assign_dev_load_option_rom(PCIDevice
*dev
,
325 unsigned int bus
, unsigned int slot
,
326 unsigned int function
);
327 static inline bool is_igd_vga_passthrough(XenHostPCIDevice
*dev
)
329 return (xen_igd_gfx_pt_enabled()
330 && ((dev
->class_code
>> 0x8) == PCI_CLASS_DISPLAY_VGA
));
332 int xen_pt_register_vga_regions(XenHostPCIDevice
*dev
);
333 int xen_pt_unregister_vga_regions(XenHostPCIDevice
*dev
);
334 void xen_pt_setup_vga(XenPCIPassthroughState
*s
, XenHostPCIDevice
*dev
,
336 #endif /* XEN_PT_H */