4 #include "qemu-common.h"
5 #include "hw/xen/xen_common.h"
6 #include "hw/pci/pci.h"
7 #include "xen-host-pci-device.h"
9 void xen_pt_log(const PCIDevice
*d
, const char *f
, ...) GCC_FMT_ATTR(2, 3);
11 #define XEN_PT_ERR(d, _f, _a...) xen_pt_log(d, "%s: Error: "_f, __func__, ##_a)
13 #ifdef XEN_PT_LOGGING_ENABLED
14 # define XEN_PT_LOG(d, _f, _a...) xen_pt_log(d, "%s: " _f, __func__, ##_a)
15 # define XEN_PT_WARN(d, _f, _a...) \
16 xen_pt_log(d, "%s: Warning: "_f, __func__, ##_a)
18 # define XEN_PT_LOG(d, _f, _a...)
19 # define XEN_PT_WARN(d, _f, _a...)
22 #ifdef XEN_PT_DEBUG_PCI_CONFIG_ACCESS
23 # define XEN_PT_LOG_CONFIG(d, addr, val, len) \
24 xen_pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \
25 __func__, addr, val, len)
27 # define XEN_PT_LOG_CONFIG(d, addr, val, len)
32 #define XEN_PFN(x) ((x) >> XC_PAGE_SHIFT)
34 typedef const struct XenPTRegInfo XenPTRegInfo
;
35 typedef struct XenPTReg XenPTReg
;
37 typedef struct XenPCIPassthroughState XenPCIPassthroughState
;
39 #define TYPE_XEN_PT_DEVICE "xen-pci-passthrough"
40 #define XEN_PT_DEVICE(obj) \
41 OBJECT_CHECK(XenPCIPassthroughState, (obj), TYPE_XEN_PT_DEVICE)
43 /* function type for config reg */
44 typedef int (*xen_pt_conf_reg_init
)
45 (XenPCIPassthroughState
*, XenPTRegInfo
*, uint32_t real_offset
,
47 typedef int (*xen_pt_conf_dword_write
)
48 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
49 uint32_t *val
, uint32_t dev_value
, uint32_t valid_mask
);
50 typedef int (*xen_pt_conf_word_write
)
51 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
52 uint16_t *val
, uint16_t dev_value
, uint16_t valid_mask
);
53 typedef int (*xen_pt_conf_byte_write
)
54 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
55 uint8_t *val
, uint8_t dev_value
, uint8_t valid_mask
);
56 typedef int (*xen_pt_conf_dword_read
)
57 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
58 uint32_t *val
, uint32_t valid_mask
);
59 typedef int (*xen_pt_conf_word_read
)
60 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
61 uint16_t *val
, uint16_t valid_mask
);
62 typedef int (*xen_pt_conf_byte_read
)
63 (XenPCIPassthroughState
*, XenPTReg
*cfg_entry
,
64 uint8_t *val
, uint8_t valid_mask
);
66 #define XEN_PT_BAR_ALLF 0xFFFFFFFF
67 #define XEN_PT_BAR_UNMAPPED (-1)
69 #define PCI_CAP_MAX 48
73 XEN_PT_GRP_TYPE_HARDWIRED
= 0, /* 0 Hardwired reg group */
74 XEN_PT_GRP_TYPE_EMU
, /* emul reg group */
75 } XenPTRegisterGroupType
;
78 XEN_PT_BAR_FLAG_MEM
= 0, /* Memory type BAR */
79 XEN_PT_BAR_FLAG_IO
, /* I/O type BAR */
80 XEN_PT_BAR_FLAG_UPPER
, /* upper 64bit BAR */
81 XEN_PT_BAR_FLAG_UNUSED
, /* unused BAR */
85 typedef struct XenPTRegion
{
87 XenPTBarFlag bar_flag
;
88 /* Translation of the emulated address */
96 /* XenPTRegInfo declaration
97 * - only for emulated register (either a part or whole bit).
98 * - for passthrough register that need special behavior (like interacting with
99 * other component), set emu_mask to all 0 and specify r/w func properly.
100 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
103 /* emulated register information */
104 struct XenPTRegInfo
{
108 /* reg reserved field mask (ON:reserved, OFF:defined) */
110 /* reg read only field mask (ON:RO/ROS, OFF:other) */
112 /* reg emulate field mask (ON:emu, OFF:passthrough) */
114 xen_pt_conf_reg_init init
;
115 /* read/write function pointer
116 * for double_word/word/byte size */
119 xen_pt_conf_dword_write write
;
120 xen_pt_conf_dword_read read
;
123 xen_pt_conf_word_write write
;
124 xen_pt_conf_word_read read
;
127 xen_pt_conf_byte_write write
;
128 xen_pt_conf_byte_read read
;
133 /* emulated register management */
135 QLIST_ENTRY(XenPTReg
) entries
;
137 uint32_t data
; /* emulated value */
140 typedef const struct XenPTRegGroupInfo XenPTRegGroupInfo
;
142 /* emul reg group size initialize method */
143 typedef int (*xen_pt_reg_size_init_fn
)
144 (XenPCIPassthroughState
*, XenPTRegGroupInfo
*,
145 uint32_t base_offset
, uint8_t *size
);
147 /* emulated register group information */
148 struct XenPTRegGroupInfo
{
150 XenPTRegisterGroupType grp_type
;
152 xen_pt_reg_size_init_fn size_init
;
153 XenPTRegInfo
*emu_regs
;
156 /* emul register group management table */
157 typedef struct XenPTRegGroup
{
158 QLIST_ENTRY(XenPTRegGroup
) entries
;
159 XenPTRegGroupInfo
*reg_grp
;
160 uint32_t base_offset
;
162 QLIST_HEAD(, XenPTReg
) reg_tbl_list
;
166 #define XEN_PT_UNASSIGNED_PIRQ (-1)
167 typedef struct XenPTMSI
{
169 uint32_t addr_lo
; /* guest message address */
170 uint32_t addr_hi
; /* guest message upper address */
171 uint16_t data
; /* guest message data */
172 uint32_t ctrl_offset
; /* saved control offset */
173 int pirq
; /* guest pirq corresponding */
174 bool initialized
; /* when guest MSI is initialized */
175 bool mapped
; /* when pirq is mapped */
178 typedef struct XenPTMSIXEntry
{
182 uint32_t vector_ctrl
;
183 bool updated
; /* indicate whether MSI ADDR or DATA is updated */
184 bool warned
; /* avoid issuing (bogus) warning more than once */
186 typedef struct XenPTMSIX
{
187 uint32_t ctrl_offset
;
192 uint32_t table_offset_adjust
; /* page align mmap */
193 uint64_t mmio_base_addr
;
195 void *phys_iomem_base
;
196 XenPTMSIXEntry msix_entry
[0];
199 struct XenPCIPassthroughState
{
202 PCIHostDeviceAddress hostaddr
;
205 bool permissive_warned
;
206 XenHostPCIDevice real_device
;
207 XenPTRegion bases
[PCI_NUM_REGIONS
]; /* Access regions */
208 QLIST_HEAD(, XenPTRegGroup
) reg_grps
;
210 uint32_t machine_irq
;
215 MemoryRegion bar
[PCI_NUM_REGIONS
- 1];
218 MemoryListener memory_listener
;
219 MemoryListener io_listener
;
222 int xen_pt_config_init(XenPCIPassthroughState
*s
);
223 void xen_pt_config_delete(XenPCIPassthroughState
*s
);
224 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
);
225 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
);
226 int xen_pt_bar_offset_to_index(uint32_t offset
);
228 static inline pcibus_t
xen_pt_get_emul_size(XenPTBarFlag flag
, pcibus_t r_size
)
230 /* align resource size (memory type only) */
231 if (flag
== XEN_PT_BAR_FLAG_MEM
) {
232 return (r_size
+ XC_PAGE_SIZE
- 1) & XC_PAGE_MASK
;
239 /* The PCI Local Bus Specification, Rev. 3.0,
240 * Section 6.2.4 Miscellaneous Registers, pp 223
241 * outlines 5 valid values for the interrupt pin (intx).
242 * 0: For devices (or device functions) that don't use an interrupt in
248 * Xen uses the following 4 values for intx
254 * Observing that these list of values are not the same, xen_pt_pci_read_intx()
255 * uses the following mapping from hw to xen values.
256 * This seems to reflect the current usage within Xen.
258 * PCI hardware | Xen | Notes
259 * ----------------+-----+----------------------------------------------------
260 * 0 | 0 | No interrupt
265 * any other value | 0 | This should never happen, log error message
268 static inline uint8_t xen_pt_pci_read_intx(XenPCIPassthroughState
*s
)
271 xen_host_pci_get_byte(&s
->real_device
, PCI_INTERRUPT_PIN
, &v
);
275 static inline uint8_t xen_pt_pci_intx(XenPCIPassthroughState
*s
)
277 uint8_t r_val
= xen_pt_pci_read_intx(s
);
279 XEN_PT_LOG(&s
->dev
, "intx=%i\n", r_val
);
280 if (r_val
< 1 || r_val
> 4) {
281 XEN_PT_LOG(&s
->dev
, "Interrupt pin read from hardware is out of range:"
282 " value=%i, acceptable range is 1 - 4\n", r_val
);
292 int xen_pt_msi_set_enable(XenPCIPassthroughState
*s
, bool en
);
293 int xen_pt_msi_setup(XenPCIPassthroughState
*s
);
294 int xen_pt_msi_update(XenPCIPassthroughState
*d
);
295 void xen_pt_msi_disable(XenPCIPassthroughState
*s
);
297 int xen_pt_msix_init(XenPCIPassthroughState
*s
, uint32_t base
);
298 void xen_pt_msix_delete(XenPCIPassthroughState
*s
);
299 int xen_pt_msix_update(XenPCIPassthroughState
*s
);
300 int xen_pt_msix_update_remap(XenPCIPassthroughState
*s
, int bar_index
);
301 void xen_pt_msix_disable(XenPCIPassthroughState
*s
);
303 static inline bool xen_pt_has_msix_mapping(XenPCIPassthroughState
*s
, int bar
)
305 return s
->msix
&& s
->msix
->bar_index
== bar
;
308 extern bool has_igd_gfx_passthru
;
309 static inline bool is_igd_vga_passthrough(XenHostPCIDevice
*dev
)
311 return (has_igd_gfx_passthru
312 && ((dev
->class_code
>> 0x8) == PCI_CLASS_DISPLAY_VGA
));
314 int xen_pt_register_vga_regions(XenHostPCIDevice
*dev
);
315 int xen_pt_unregister_vga_regions(XenHostPCIDevice
*dev
);
316 int xen_pt_setup_vga(XenPCIPassthroughState
*s
, XenHostPCIDevice
*dev
);
317 #endif /* !XEN_PT_H */