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
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu/timer.h"
18 #include "hw/xen/xen_pt.h"
19 #include "hw/xen/xen_igd.h"
20 #include "hw/xen/xen-legacy-backend.h"
22 #define XEN_PT_MERGE_VALUE(value, data, val_mask) \
23 (((value) & (val_mask)) | ((data) & ~(val_mask)))
25 #define XEN_PT_INVALID_REG 0xFFFFFFFF /* invalid register value */
29 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
30 uint32_t real_offset
, uint32_t *data
);
35 /* A return value of 1 means the capability should NOT be exposed to guest. */
36 static int xen_pt_hide_dev_cap(const XenHostPCIDevice
*d
, uint8_t grp_id
)
40 /* The PCI Express Capability Structure of the VF of Intel 82599 10GbE
41 * Controller looks trivial, e.g., the PCI Express Capabilities
42 * Register is 0. We should not try to expose it to guest.
44 * The datasheet is available at
45 * http://download.intel.com/design/network/datashts/82599_datasheet.pdf
47 * See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the
48 * PCI Express Capability Structure of the VF of Intel 82599 10GbE
49 * Controller looks trivial, e.g., the PCI Express Capabilities
50 * Register is 0, so the Capability Version is 0 and
51 * xen_pt_pcie_size_init() would fail.
53 if (d
->vendor_id
== PCI_VENDOR_ID_INTEL
&&
54 d
->device_id
== PCI_DEVICE_ID_INTEL_82599_SFP_VF
) {
62 /* find emulate register group entry */
63 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
)
65 XenPTRegGroup
*entry
= NULL
;
67 /* find register group entry */
68 QLIST_FOREACH(entry
, &s
->reg_grps
, entries
) {
70 if ((entry
->base_offset
<= address
)
71 && ((entry
->base_offset
+ entry
->size
) > address
)) {
76 /* group entry not found */
80 /* find emulate register entry */
81 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
)
83 XenPTReg
*reg_entry
= NULL
;
84 XenPTRegInfo
*reg
= NULL
;
85 uint32_t real_offset
= 0;
87 /* find register entry */
88 QLIST_FOREACH(reg_entry
, ®_grp
->reg_tbl_list
, entries
) {
90 real_offset
= reg_grp
->base_offset
+ reg
->offset
;
92 if ((real_offset
<= address
)
93 && ((real_offset
+ reg
->size
) > address
)) {
101 static uint32_t get_throughable_mask(const XenPCIPassthroughState
*s
,
102 XenPTRegInfo
*reg
, uint32_t valid_mask
)
104 uint32_t throughable_mask
= ~(reg
->emu_mask
| reg
->ro_mask
);
106 if (!s
->permissive
) {
107 throughable_mask
&= ~reg
->res_mask
;
110 return throughable_mask
& valid_mask
;
114 * general register functions
117 /* register initialization function */
119 static int xen_pt_common_reg_init(XenPCIPassthroughState
*s
,
120 XenPTRegInfo
*reg
, uint32_t real_offset
,
123 *data
= reg
->init_val
;
127 /* Read register functions */
129 static int xen_pt_byte_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
130 uint8_t *value
, uint8_t valid_mask
)
132 XenPTRegInfo
*reg
= cfg_entry
->reg
;
133 uint8_t valid_emu_mask
= 0;
134 uint8_t *data
= cfg_entry
->ptr
.byte
;
136 /* emulate byte register */
137 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
138 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
142 static int xen_pt_word_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
143 uint16_t *value
, uint16_t valid_mask
)
145 XenPTRegInfo
*reg
= cfg_entry
->reg
;
146 uint16_t valid_emu_mask
= 0;
147 uint16_t *data
= cfg_entry
->ptr
.half_word
;
149 /* emulate word register */
150 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
151 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
155 static int xen_pt_long_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
156 uint32_t *value
, uint32_t valid_mask
)
158 XenPTRegInfo
*reg
= cfg_entry
->reg
;
159 uint32_t valid_emu_mask
= 0;
160 uint32_t *data
= cfg_entry
->ptr
.word
;
162 /* emulate long register */
163 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
164 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
169 /* Write register functions */
171 static int xen_pt_byte_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
172 uint8_t *val
, uint8_t dev_value
,
175 XenPTRegInfo
*reg
= cfg_entry
->reg
;
176 uint8_t writable_mask
= 0;
177 uint8_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
178 uint8_t *data
= cfg_entry
->ptr
.byte
;
180 /* modify emulate register */
181 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
182 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
184 /* create value for writing to I/O device register */
185 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
190 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
191 uint16_t *val
, uint16_t dev_value
,
194 XenPTRegInfo
*reg
= cfg_entry
->reg
;
195 uint16_t writable_mask
= 0;
196 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
197 uint16_t *data
= cfg_entry
->ptr
.half_word
;
199 /* modify emulate register */
200 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
201 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
203 /* create value for writing to I/O device register */
204 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
209 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
210 uint32_t *val
, uint32_t dev_value
,
213 XenPTRegInfo
*reg
= cfg_entry
->reg
;
214 uint32_t writable_mask
= 0;
215 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
216 uint32_t *data
= cfg_entry
->ptr
.word
;
218 /* modify emulate register */
219 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
220 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
222 /* create value for writing to I/O device register */
223 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
230 /* XenPTRegInfo declaration
231 * - only for emulated register (either a part or whole bit).
232 * - for passthrough register that need special behavior (like interacting with
233 * other component), set emu_mask to all 0 and specify r/w func properly.
234 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
237 /********************
241 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
242 XenPTRegInfo
*reg
, uint32_t real_offset
,
245 *data
= s
->real_device
.vendor_id
;
248 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
249 XenPTRegInfo
*reg
, uint32_t real_offset
,
252 *data
= s
->real_device
.device_id
;
255 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
256 XenPTRegInfo
*reg
, uint32_t real_offset
,
259 XenPTRegGroup
*reg_grp_entry
= NULL
;
260 XenPTReg
*reg_entry
= NULL
;
261 uint32_t reg_field
= 0;
263 /* find Header register group */
264 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
266 /* find Capabilities Pointer register */
267 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
269 /* check Capabilities Pointer register */
270 if (*reg_entry
->ptr
.half_word
) {
271 reg_field
|= PCI_STATUS_CAP_LIST
;
273 reg_field
&= ~PCI_STATUS_CAP_LIST
;
276 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
277 " for Capabilities Pointer register."
278 " (%s)\n", __func__
);
282 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
283 " for Header. (%s)\n", __func__
);
290 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
291 XenPTRegInfo
*reg
, uint32_t real_offset
,
294 /* read PCI_HEADER_TYPE */
295 *data
= reg
->init_val
;
296 if ((PCI_DEVICE(s
)->cap_present
& QEMU_PCI_CAP_MULTIFUNCTION
)) {
297 *data
|= PCI_HEADER_TYPE_MULTI_FUNCTION
;
302 /* initialize Interrupt Pin register */
303 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
304 XenPTRegInfo
*reg
, uint32_t real_offset
,
307 if (s
->real_device
.irq
) {
308 *data
= xen_pt_pci_read_intx(s
);
313 /* Command register */
314 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
315 uint16_t *val
, uint16_t dev_value
,
318 XenPTRegInfo
*reg
= cfg_entry
->reg
;
319 uint16_t writable_mask
= 0;
320 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
321 uint16_t *data
= cfg_entry
->ptr
.half_word
;
323 /* modify emulate register */
324 writable_mask
= ~reg
->ro_mask
& valid_mask
;
325 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
327 /* create value for writing to I/O device register */
328 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
329 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
331 if (s
->machine_irq
) {
332 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
336 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
342 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
343 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
344 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
345 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
347 static bool is_64bit_bar(PCIIORegion
*r
)
349 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
352 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
354 if (is_64bit_bar(r
)) {
356 size64
= (r
+ 1)->size
;
364 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
367 PCIDevice
*d
= PCI_DEVICE(s
);
368 XenPTRegion
*region
= NULL
;
371 /* check 64bit BAR */
372 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
373 int type
= s
->real_device
.io_regions
[index
- 1].type
;
375 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
376 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
377 region
= &s
->bases
[index
- 1];
378 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
379 return XEN_PT_BAR_FLAG_UPPER
;
384 /* check unused BAR */
385 r
= &d
->io_regions
[index
];
386 if (!xen_pt_get_bar_size(r
)) {
387 return XEN_PT_BAR_FLAG_UNUSED
;
391 if (index
== PCI_ROM_SLOT
) {
392 return XEN_PT_BAR_FLAG_MEM
;
395 /* check BAR I/O indicator */
396 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
397 return XEN_PT_BAR_FLAG_IO
;
399 return XEN_PT_BAR_FLAG_MEM
;
403 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
405 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
406 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
408 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
412 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
413 uint32_t real_offset
, uint32_t *data
)
415 uint32_t reg_field
= 0;
418 index
= xen_pt_bar_offset_to_index(reg
->offset
);
419 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
420 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
425 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
426 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
427 reg_field
= XEN_PT_INVALID_REG
;
433 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
434 uint32_t *value
, uint32_t valid_mask
)
436 XenPTRegInfo
*reg
= cfg_entry
->reg
;
437 uint32_t valid_emu_mask
= 0;
438 uint32_t bar_emu_mask
= 0;
442 index
= xen_pt_bar_offset_to_index(reg
->offset
);
443 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
444 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
448 /* use fixed-up value from kernel sysfs */
449 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
451 /* set emulate mask depend on BAR flag */
452 switch (s
->bases
[index
].bar_flag
) {
453 case XEN_PT_BAR_FLAG_MEM
:
454 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
456 case XEN_PT_BAR_FLAG_IO
:
457 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
459 case XEN_PT_BAR_FLAG_UPPER
:
460 bar_emu_mask
= XEN_PT_BAR_ALLF
;
467 valid_emu_mask
= bar_emu_mask
& valid_mask
;
468 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
472 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
473 uint32_t *val
, uint32_t dev_value
,
476 XenPTRegInfo
*reg
= cfg_entry
->reg
;
477 XenPTRegion
*base
= NULL
;
478 PCIDevice
*d
= PCI_DEVICE(s
);
479 const PCIIORegion
*r
;
480 uint32_t writable_mask
= 0;
481 uint32_t bar_emu_mask
= 0;
482 uint32_t bar_ro_mask
= 0;
485 uint32_t *data
= cfg_entry
->ptr
.word
;
487 index
= xen_pt_bar_offset_to_index(reg
->offset
);
488 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
489 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
493 r
= &d
->io_regions
[index
];
494 base
= &s
->bases
[index
];
495 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
497 /* set emulate mask and read-only mask values depend on the BAR flag */
498 switch (s
->bases
[index
].bar_flag
) {
499 case XEN_PT_BAR_FLAG_MEM
:
500 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
502 /* low 32 bits mask for 64 bit bars */
503 bar_ro_mask
= XEN_PT_BAR_ALLF
;
505 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
508 case XEN_PT_BAR_FLAG_IO
:
509 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
510 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
512 case XEN_PT_BAR_FLAG_UPPER
:
514 r_size
= d
->io_regions
[index
- 1].size
>> 32;
515 bar_emu_mask
= XEN_PT_BAR_ALLF
;
516 bar_ro_mask
= r_size
? r_size
- 1 : 0;
522 /* modify emulate register */
523 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
524 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
526 /* check whether we need to update the virtual region address or not */
527 switch (s
->bases
[index
].bar_flag
) {
528 case XEN_PT_BAR_FLAG_UPPER
:
529 case XEN_PT_BAR_FLAG_MEM
:
532 case XEN_PT_BAR_FLAG_IO
:
539 /* create value for writing to I/O device register */
540 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
545 /* write Exp ROM BAR */
546 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
547 XenPTReg
*cfg_entry
, uint32_t *val
,
548 uint32_t dev_value
, uint32_t valid_mask
)
550 XenPTRegInfo
*reg
= cfg_entry
->reg
;
551 XenPTRegion
*base
= NULL
;
552 PCIDevice
*d
= PCI_DEVICE(s
);
553 uint32_t writable_mask
= 0;
554 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
556 uint32_t bar_ro_mask
= 0;
557 uint32_t *data
= cfg_entry
->ptr
.word
;
559 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
560 base
= &s
->bases
[PCI_ROM_SLOT
];
561 /* align memory type resource size */
562 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
564 /* set emulate mask and read-only mask */
565 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
567 /* modify emulate register */
568 writable_mask
= ~bar_ro_mask
& valid_mask
;
569 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
571 /* create value for writing to I/O device register */
572 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
577 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
579 uint32_t *value
, uint32_t valid_mask
)
581 *value
= igd_read_opregion(s
);
585 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
586 XenPTReg
*cfg_entry
, uint32_t *value
,
587 uint32_t dev_value
, uint32_t valid_mask
)
589 igd_write_opregion(s
, *value
);
593 /* Header Type0 reg static information table */
594 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
597 .offset
= PCI_VENDOR_ID
,
602 .init
= xen_pt_vendor_reg_init
,
603 .u
.w
.read
= xen_pt_word_reg_read
,
604 .u
.w
.write
= xen_pt_word_reg_write
,
608 .offset
= PCI_DEVICE_ID
,
613 .init
= xen_pt_device_reg_init
,
614 .u
.w
.read
= xen_pt_word_reg_read
,
615 .u
.w
.write
= xen_pt_word_reg_write
,
619 .offset
= PCI_COMMAND
,
624 .init
= xen_pt_common_reg_init
,
625 .u
.w
.read
= xen_pt_word_reg_read
,
626 .u
.w
.write
= xen_pt_cmd_reg_write
,
628 /* Capabilities Pointer reg */
630 .offset
= PCI_CAPABILITY_LIST
,
635 .init
= xen_pt_ptr_reg_init
,
636 .u
.b
.read
= xen_pt_byte_reg_read
,
637 .u
.b
.write
= xen_pt_byte_reg_write
,
640 /* use emulated Cap Ptr value to initialize,
641 * so need to be declared after Cap Ptr reg
644 .offset
= PCI_STATUS
,
651 .init
= xen_pt_status_reg_init
,
652 .u
.w
.read
= xen_pt_word_reg_read
,
653 .u
.w
.write
= xen_pt_word_reg_write
,
655 /* Cache Line Size reg */
657 .offset
= PCI_CACHE_LINE_SIZE
,
662 .init
= xen_pt_common_reg_init
,
663 .u
.b
.read
= xen_pt_byte_reg_read
,
664 .u
.b
.write
= xen_pt_byte_reg_write
,
666 /* Latency Timer reg */
668 .offset
= PCI_LATENCY_TIMER
,
673 .init
= xen_pt_common_reg_init
,
674 .u
.b
.read
= xen_pt_byte_reg_read
,
675 .u
.b
.write
= xen_pt_byte_reg_write
,
677 /* Header Type reg */
679 .offset
= PCI_HEADER_TYPE
,
683 .emu_mask
= PCI_HEADER_TYPE_MULTI_FUNCTION
,
684 .init
= xen_pt_header_type_reg_init
,
685 .u
.b
.read
= xen_pt_byte_reg_read
,
686 .u
.b
.write
= xen_pt_byte_reg_write
,
688 /* Interrupt Line reg */
690 .offset
= PCI_INTERRUPT_LINE
,
695 .init
= xen_pt_common_reg_init
,
696 .u
.b
.read
= xen_pt_byte_reg_read
,
697 .u
.b
.write
= xen_pt_byte_reg_write
,
699 /* Interrupt Pin reg */
701 .offset
= PCI_INTERRUPT_PIN
,
706 .init
= xen_pt_irqpin_reg_init
,
707 .u
.b
.read
= xen_pt_byte_reg_read
,
708 .u
.b
.write
= xen_pt_byte_reg_write
,
711 /* mask of BAR need to be decided later, depends on IO/MEM type */
713 .offset
= PCI_BASE_ADDRESS_0
,
715 .init_val
= 0x00000000,
716 .init
= xen_pt_bar_reg_init
,
717 .u
.dw
.read
= xen_pt_bar_reg_read
,
718 .u
.dw
.write
= xen_pt_bar_reg_write
,
722 .offset
= PCI_BASE_ADDRESS_1
,
724 .init_val
= 0x00000000,
725 .init
= xen_pt_bar_reg_init
,
726 .u
.dw
.read
= xen_pt_bar_reg_read
,
727 .u
.dw
.write
= xen_pt_bar_reg_write
,
731 .offset
= PCI_BASE_ADDRESS_2
,
733 .init_val
= 0x00000000,
734 .init
= xen_pt_bar_reg_init
,
735 .u
.dw
.read
= xen_pt_bar_reg_read
,
736 .u
.dw
.write
= xen_pt_bar_reg_write
,
740 .offset
= PCI_BASE_ADDRESS_3
,
742 .init_val
= 0x00000000,
743 .init
= xen_pt_bar_reg_init
,
744 .u
.dw
.read
= xen_pt_bar_reg_read
,
745 .u
.dw
.write
= xen_pt_bar_reg_write
,
749 .offset
= PCI_BASE_ADDRESS_4
,
751 .init_val
= 0x00000000,
752 .init
= xen_pt_bar_reg_init
,
753 .u
.dw
.read
= xen_pt_bar_reg_read
,
754 .u
.dw
.write
= xen_pt_bar_reg_write
,
758 .offset
= PCI_BASE_ADDRESS_5
,
760 .init_val
= 0x00000000,
761 .init
= xen_pt_bar_reg_init
,
762 .u
.dw
.read
= xen_pt_bar_reg_read
,
763 .u
.dw
.write
= xen_pt_bar_reg_write
,
765 /* Expansion ROM BAR reg */
767 .offset
= PCI_ROM_ADDRESS
,
769 .init_val
= 0x00000000,
770 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
771 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
772 .init
= xen_pt_bar_reg_init
,
773 .u
.dw
.read
= xen_pt_long_reg_read
,
774 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
782 /*********************************
783 * Vital Product Data Capability
786 /* Vital Product Data Capability Structure reg static information table */
787 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
789 .offset
= PCI_CAP_LIST_NEXT
,
794 .init
= xen_pt_ptr_reg_init
,
795 .u
.b
.read
= xen_pt_byte_reg_read
,
796 .u
.b
.write
= xen_pt_byte_reg_write
,
799 .offset
= PCI_VPD_ADDR
,
803 .init
= xen_pt_common_reg_init
,
804 .u
.w
.read
= xen_pt_word_reg_read
,
805 .u
.w
.write
= xen_pt_word_reg_write
,
813 /**************************************
814 * Vendor Specific Capability
817 /* Vendor Specific Capability Structure reg static information table */
818 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
820 .offset
= PCI_CAP_LIST_NEXT
,
825 .init
= xen_pt_ptr_reg_init
,
826 .u
.b
.read
= xen_pt_byte_reg_read
,
827 .u
.b
.write
= xen_pt_byte_reg_write
,
835 /*****************************
836 * PCI Express Capability
839 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
843 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
846 return flag
& PCI_EXP_FLAGS_VERS
;
849 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
853 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
856 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
859 /* initialize Link Control register */
860 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
861 XenPTRegInfo
*reg
, uint32_t real_offset
,
864 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
865 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
867 /* no need to initialize in case of Root Complex Integrated Endpoint
870 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
871 *data
= XEN_PT_INVALID_REG
;
874 *data
= reg
->init_val
;
877 /* initialize Device Control 2 register */
878 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
879 XenPTRegInfo
*reg
, uint32_t real_offset
,
882 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
884 /* no need to initialize in case of cap_ver 1.x */
886 *data
= XEN_PT_INVALID_REG
;
889 *data
= reg
->init_val
;
892 /* initialize Link Control 2 register */
893 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
894 XenPTRegInfo
*reg
, uint32_t real_offset
,
897 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
898 uint32_t reg_field
= 0;
900 /* no need to initialize in case of cap_ver 1.x */
902 reg_field
= XEN_PT_INVALID_REG
;
904 /* set Supported Link Speed */
907 rc
= xen_host_pci_get_byte(&s
->real_device
,
908 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
913 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
920 /* PCI Express Capability Structure reg static information table */
921 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
922 /* Next Pointer reg */
924 .offset
= PCI_CAP_LIST_NEXT
,
929 .init
= xen_pt_ptr_reg_init
,
930 .u
.b
.read
= xen_pt_byte_reg_read
,
931 .u
.b
.write
= xen_pt_byte_reg_write
,
933 /* Device Capabilities reg */
935 .offset
= PCI_EXP_DEVCAP
,
937 .init_val
= 0x00000000,
938 .ro_mask
= 0xFFFFFFFF,
939 .emu_mask
= 0x10000000,
940 .init
= xen_pt_common_reg_init
,
941 .u
.dw
.read
= xen_pt_long_reg_read
,
942 .u
.dw
.write
= xen_pt_long_reg_write
,
944 /* Device Control reg */
946 .offset
= PCI_EXP_DEVCTL
,
951 .init
= xen_pt_common_reg_init
,
952 .u
.w
.read
= xen_pt_word_reg_read
,
953 .u
.w
.write
= xen_pt_word_reg_write
,
955 /* Device Status reg */
957 .offset
= PCI_EXP_DEVSTA
,
962 .init
= xen_pt_common_reg_init
,
963 .u
.w
.read
= xen_pt_word_reg_read
,
964 .u
.w
.write
= xen_pt_word_reg_write
,
966 /* Link Control reg */
968 .offset
= PCI_EXP_LNKCTL
,
973 .init
= xen_pt_linkctrl_reg_init
,
974 .u
.w
.read
= xen_pt_word_reg_read
,
975 .u
.w
.write
= xen_pt_word_reg_write
,
977 /* Link Status reg */
979 .offset
= PCI_EXP_LNKSTA
,
983 .init
= xen_pt_common_reg_init
,
984 .u
.w
.read
= xen_pt_word_reg_read
,
985 .u
.w
.write
= xen_pt_word_reg_write
,
987 /* Device Control 2 reg */
994 .init
= xen_pt_devctrl2_reg_init
,
995 .u
.w
.read
= xen_pt_word_reg_read
,
996 .u
.w
.write
= xen_pt_word_reg_write
,
998 /* Link Control 2 reg */
1005 .init
= xen_pt_linkctrl2_reg_init
,
1006 .u
.w
.read
= xen_pt_word_reg_read
,
1007 .u
.w
.write
= xen_pt_word_reg_write
,
1015 /*********************************
1016 * Power Management Capability
1019 /* Power Management Capability reg static information table */
1020 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1021 /* Next Pointer reg */
1023 .offset
= PCI_CAP_LIST_NEXT
,
1028 .init
= xen_pt_ptr_reg_init
,
1029 .u
.b
.read
= xen_pt_byte_reg_read
,
1030 .u
.b
.write
= xen_pt_byte_reg_write
,
1032 /* Power Management Capabilities reg */
1034 .offset
= PCI_CAP_FLAGS
,
1039 .init
= xen_pt_common_reg_init
,
1040 .u
.w
.read
= xen_pt_word_reg_read
,
1041 .u
.w
.write
= xen_pt_word_reg_write
,
1043 /* PCI Power Management Control/Status reg */
1045 .offset
= PCI_PM_CTRL
,
1050 .rw1c_mask
= 0x8000,
1052 .init
= xen_pt_common_reg_init
,
1053 .u
.w
.read
= xen_pt_word_reg_read
,
1054 .u
.w
.write
= xen_pt_word_reg_write
,
1062 /********************************
1067 #define xen_pt_msi_check_type(offset, flags, what) \
1068 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1069 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1071 /* Message Control register */
1072 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1073 XenPTRegInfo
*reg
, uint32_t real_offset
,
1076 XenPTMSI
*msi
= s
->msi
;
1080 /* use I/O device register's value as initial value */
1081 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1085 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1086 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1087 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1088 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1090 msi
->flags
|= reg_field
;
1091 msi
->ctrl_offset
= real_offset
;
1092 msi
->initialized
= false;
1093 msi
->mapped
= false;
1095 *data
= reg
->init_val
;
1098 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1099 XenPTReg
*cfg_entry
, uint16_t *val
,
1100 uint16_t dev_value
, uint16_t valid_mask
)
1102 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1103 XenPTMSI
*msi
= s
->msi
;
1104 uint16_t writable_mask
= 0;
1105 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1106 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1108 /* Currently no support for multi-vector */
1109 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1110 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1113 /* modify emulate register */
1114 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1115 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1116 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1118 /* create value for writing to I/O device register */
1119 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1122 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1123 /* setup MSI pirq for the first time */
1124 if (!msi
->initialized
) {
1125 /* Init physical one */
1126 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1127 if (xen_pt_msi_setup(s
)) {
1128 /* We do not broadcast the error to the framework code, so
1129 * that MSI errors are contained in MSI emulation code and
1130 * QEMU can go on running.
1131 * Guest MSI would be actually not working.
1133 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1134 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1137 if (xen_pt_msi_update(s
)) {
1138 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1139 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1142 msi
->initialized
= true;
1145 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1146 } else if (msi
->mapped
) {
1147 xen_pt_msi_disable(s
);
1153 /* initialize Message Upper Address register */
1154 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1155 XenPTRegInfo
*reg
, uint32_t real_offset
,
1158 /* no need to initialize in case of 32 bit type */
1159 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1160 *data
= XEN_PT_INVALID_REG
;
1162 *data
= reg
->init_val
;
1167 /* this function will be called twice (for 32 bit and 64 bit type) */
1168 /* initialize Message Data register */
1169 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1170 XenPTRegInfo
*reg
, uint32_t real_offset
,
1173 uint32_t flags
= s
->msi
->flags
;
1174 uint32_t offset
= reg
->offset
;
1176 /* check the offset whether matches the type or not */
1177 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1178 *data
= reg
->init_val
;
1180 *data
= XEN_PT_INVALID_REG
;
1185 /* this function will be called twice (for 32 bit and 64 bit type) */
1186 /* initialize Mask register */
1187 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1188 XenPTRegInfo
*reg
, uint32_t real_offset
,
1191 uint32_t flags
= s
->msi
->flags
;
1193 /* check the offset whether matches the type or not */
1194 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1195 *data
= XEN_PT_INVALID_REG
;
1196 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1197 *data
= reg
->init_val
;
1199 *data
= XEN_PT_INVALID_REG
;
1204 /* this function will be called twice (for 32 bit and 64 bit type) */
1205 /* initialize Pending register */
1206 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1207 XenPTRegInfo
*reg
, uint32_t real_offset
,
1210 uint32_t flags
= s
->msi
->flags
;
1212 /* check the offset whether matches the type or not */
1213 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1214 *data
= XEN_PT_INVALID_REG
;
1215 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1216 *data
= reg
->init_val
;
1218 *data
= XEN_PT_INVALID_REG
;
1223 /* write Message Address register */
1224 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1225 XenPTReg
*cfg_entry
, uint32_t *val
,
1226 uint32_t dev_value
, uint32_t valid_mask
)
1228 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1229 uint32_t writable_mask
= 0;
1230 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1231 uint32_t *data
= cfg_entry
->ptr
.word
;
1233 /* modify emulate register */
1234 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1235 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1236 s
->msi
->addr_lo
= *data
;
1238 /* create value for writing to I/O device register */
1239 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1242 if (*data
!= old_addr
) {
1243 if (s
->msi
->mapped
) {
1244 xen_pt_msi_update(s
);
1250 /* write Message Upper Address register */
1251 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1252 XenPTReg
*cfg_entry
, uint32_t *val
,
1253 uint32_t dev_value
, uint32_t valid_mask
)
1255 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1256 uint32_t writable_mask
= 0;
1257 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1258 uint32_t *data
= cfg_entry
->ptr
.word
;
1260 /* check whether the type is 64 bit or not */
1261 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1263 "Can't write to the upper address without 64 bit support\n");
1267 /* modify emulate register */
1268 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1269 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1270 /* update the msi_info too */
1271 s
->msi
->addr_hi
= *data
;
1273 /* create value for writing to I/O device register */
1274 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1277 if (*data
!= old_addr
) {
1278 if (s
->msi
->mapped
) {
1279 xen_pt_msi_update(s
);
1287 /* this function will be called twice (for 32 bit and 64 bit type) */
1288 /* write Message Data register */
1289 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1290 XenPTReg
*cfg_entry
, uint16_t *val
,
1291 uint16_t dev_value
, uint16_t valid_mask
)
1293 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1294 XenPTMSI
*msi
= s
->msi
;
1295 uint16_t writable_mask
= 0;
1296 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1297 uint32_t offset
= reg
->offset
;
1298 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1300 /* check the offset whether matches the type or not */
1301 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1302 /* exit I/O emulator */
1303 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1307 /* modify emulate register */
1308 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1309 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1310 /* update the msi_info too */
1313 /* create value for writing to I/O device register */
1314 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1317 if (*data
!= old_data
) {
1319 xen_pt_msi_update(s
);
1326 static int xen_pt_mask_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
1327 uint32_t *val
, uint32_t dev_value
,
1328 uint32_t valid_mask
)
1332 rc
= xen_pt_long_reg_write(s
, cfg_entry
, val
, dev_value
, valid_mask
);
1337 s
->msi
->mask
= *val
;
1342 /* MSI Capability Structure reg static information table */
1343 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1344 /* Next Pointer reg */
1346 .offset
= PCI_CAP_LIST_NEXT
,
1351 .init
= xen_pt_ptr_reg_init
,
1352 .u
.b
.read
= xen_pt_byte_reg_read
,
1353 .u
.b
.write
= xen_pt_byte_reg_write
,
1355 /* Message Control reg */
1357 .offset
= PCI_MSI_FLAGS
,
1363 .init
= xen_pt_msgctrl_reg_init
,
1364 .u
.w
.read
= xen_pt_word_reg_read
,
1365 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1367 /* Message Address reg */
1369 .offset
= PCI_MSI_ADDRESS_LO
,
1371 .init_val
= 0x00000000,
1372 .ro_mask
= 0x00000003,
1373 .emu_mask
= 0xFFFFFFFF,
1374 .init
= xen_pt_common_reg_init
,
1375 .u
.dw
.read
= xen_pt_long_reg_read
,
1376 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1378 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1380 .offset
= PCI_MSI_ADDRESS_HI
,
1382 .init_val
= 0x00000000,
1383 .ro_mask
= 0x00000000,
1384 .emu_mask
= 0xFFFFFFFF,
1385 .init
= xen_pt_msgaddr64_reg_init
,
1386 .u
.dw
.read
= xen_pt_long_reg_read
,
1387 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1389 /* Message Data reg (16 bits of data for 32-bit devices) */
1391 .offset
= PCI_MSI_DATA_32
,
1396 .init
= xen_pt_msgdata_reg_init
,
1397 .u
.w
.read
= xen_pt_word_reg_read
,
1398 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1400 /* Message Data reg (16 bits of data for 64-bit devices) */
1402 .offset
= PCI_MSI_DATA_64
,
1407 .init
= xen_pt_msgdata_reg_init
,
1408 .u
.w
.read
= xen_pt_word_reg_read
,
1409 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1411 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1413 .offset
= PCI_MSI_MASK_32
,
1415 .init_val
= 0x00000000,
1416 .ro_mask
= 0xFFFFFFFF,
1417 .emu_mask
= 0xFFFFFFFF,
1418 .init
= xen_pt_mask_reg_init
,
1419 .u
.dw
.read
= xen_pt_long_reg_read
,
1420 .u
.dw
.write
= xen_pt_mask_reg_write
,
1422 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1424 .offset
= PCI_MSI_MASK_64
,
1426 .init_val
= 0x00000000,
1427 .ro_mask
= 0xFFFFFFFF,
1428 .emu_mask
= 0xFFFFFFFF,
1429 .init
= xen_pt_mask_reg_init
,
1430 .u
.dw
.read
= xen_pt_long_reg_read
,
1431 .u
.dw
.write
= xen_pt_mask_reg_write
,
1433 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1435 .offset
= PCI_MSI_MASK_32
+ 4,
1437 .init_val
= 0x00000000,
1438 .ro_mask
= 0xFFFFFFFF,
1439 .emu_mask
= 0x00000000,
1440 .init
= xen_pt_pending_reg_init
,
1441 .u
.dw
.read
= xen_pt_long_reg_read
,
1442 .u
.dw
.write
= xen_pt_long_reg_write
,
1444 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1446 .offset
= PCI_MSI_MASK_64
+ 4,
1448 .init_val
= 0x00000000,
1449 .ro_mask
= 0xFFFFFFFF,
1450 .emu_mask
= 0x00000000,
1451 .init
= xen_pt_pending_reg_init
,
1452 .u
.dw
.read
= xen_pt_long_reg_read
,
1453 .u
.dw
.write
= xen_pt_long_reg_write
,
1461 /**************************************
1465 /* Message Control register for MSI-X */
1466 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1467 XenPTRegInfo
*reg
, uint32_t real_offset
,
1473 /* use I/O device register's value as initial value */
1474 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1478 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1479 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1480 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1481 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1484 s
->msix
->ctrl_offset
= real_offset
;
1486 *data
= reg
->init_val
;
1489 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1490 XenPTReg
*cfg_entry
, uint16_t *val
,
1491 uint16_t dev_value
, uint16_t valid_mask
)
1493 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1494 uint16_t writable_mask
= 0;
1495 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1496 int debug_msix_enabled_old
;
1497 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1499 /* modify emulate register */
1500 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1501 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1503 /* create value for writing to I/O device register */
1504 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1507 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1508 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1509 xen_pt_msix_update(s
);
1510 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1511 xen_pt_msix_disable(s
);
1514 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1516 debug_msix_enabled_old
= s
->msix
->enabled
;
1517 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1518 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1519 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1520 s
->msix
->enabled
? "enable" : "disable");
1526 /* MSI-X Capability Structure reg static information table */
1527 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1528 /* Next Pointer reg */
1530 .offset
= PCI_CAP_LIST_NEXT
,
1535 .init
= xen_pt_ptr_reg_init
,
1536 .u
.b
.read
= xen_pt_byte_reg_read
,
1537 .u
.b
.write
= xen_pt_byte_reg_write
,
1539 /* Message Control reg */
1541 .offset
= PCI_MSI_FLAGS
,
1547 .init
= xen_pt_msixctrl_reg_init
,
1548 .u
.w
.read
= xen_pt_word_reg_read
,
1549 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1556 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1557 /* Intel IGFX OpRegion reg */
1562 .emu_mask
= 0xFFFFFFFF,
1563 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1564 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1571 /****************************
1575 /* capability structure register group size functions */
1577 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1578 const XenPTRegGroupInfo
*grp_reg
,
1579 uint32_t base_offset
, uint8_t *size
)
1581 *size
= grp_reg
->grp_size
;
1584 /* get Vendor Specific Capability Structure register group size */
1585 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1586 const XenPTRegGroupInfo
*grp_reg
,
1587 uint32_t base_offset
, uint8_t *size
)
1589 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1591 /* get PCI Express Capability Structure register group size */
1592 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1593 const XenPTRegGroupInfo
*grp_reg
,
1594 uint32_t base_offset
, uint8_t *size
)
1596 PCIDevice
*d
= PCI_DEVICE(s
);
1597 uint8_t version
= get_capability_version(s
, base_offset
);
1598 uint8_t type
= get_device_type(s
, base_offset
);
1599 uint8_t pcie_size
= 0;
1602 /* calculate size depend on capability version and device/port type */
1603 /* in case of PCI Express Base Specification Rev 1.x */
1605 /* The PCI Express Capabilities, Device Capabilities, and Device
1606 * Status/Control registers are required for all PCI Express devices.
1607 * The Link Capabilities and Link Status/Control are required for all
1608 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1609 * are not required to implement registers other than those listed
1610 * above and terminate the capability structure.
1613 case PCI_EXP_TYPE_ENDPOINT
:
1614 case PCI_EXP_TYPE_LEG_END
:
1617 case PCI_EXP_TYPE_RC_END
:
1621 /* only EndPoint passthrough is supported */
1622 case PCI_EXP_TYPE_ROOT_PORT
:
1623 case PCI_EXP_TYPE_UPSTREAM
:
1624 case PCI_EXP_TYPE_DOWNSTREAM
:
1625 case PCI_EXP_TYPE_PCI_BRIDGE
:
1626 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1627 case PCI_EXP_TYPE_RC_EC
:
1629 XEN_PT_ERR(d
, "Unsupported device/port type 0x%x.\n", type
);
1633 /* in case of PCI Express Base Specification Rev 2.0 */
1634 else if (version
== 2) {
1636 case PCI_EXP_TYPE_ENDPOINT
:
1637 case PCI_EXP_TYPE_LEG_END
:
1638 case PCI_EXP_TYPE_RC_END
:
1639 /* For Functions that do not implement the registers,
1640 * these spaces must be hardwired to 0b.
1644 /* only EndPoint passthrough is supported */
1645 case PCI_EXP_TYPE_ROOT_PORT
:
1646 case PCI_EXP_TYPE_UPSTREAM
:
1647 case PCI_EXP_TYPE_DOWNSTREAM
:
1648 case PCI_EXP_TYPE_PCI_BRIDGE
:
1649 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1650 case PCI_EXP_TYPE_RC_EC
:
1652 XEN_PT_ERR(d
, "Unsupported device/port type 0x%x.\n", type
);
1656 XEN_PT_ERR(d
, "Unsupported capability version 0x%x.\n", version
);
1663 /* get MSI Capability Structure register group size */
1664 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1665 const XenPTRegGroupInfo
*grp_reg
,
1666 uint32_t base_offset
, uint8_t *size
)
1668 uint16_t msg_ctrl
= 0;
1669 uint8_t msi_size
= 0xa;
1672 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1677 /* check if 64-bit address is capable of per-vector masking */
1678 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1681 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1685 s
->msi
= g_new0(XenPTMSI
, 1);
1686 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1691 /* get MSI-X Capability Structure register group size */
1692 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1693 const XenPTRegGroupInfo
*grp_reg
,
1694 uint32_t base_offset
, uint8_t *size
)
1698 rc
= xen_pt_msix_init(s
, base_offset
);
1701 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1705 *size
= grp_reg
->grp_size
;
1710 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1711 /* Header Type0 reg group */
1714 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1716 .size_init
= xen_pt_reg_grp_size_init
,
1717 .emu_regs
= xen_pt_emu_reg_header0
,
1719 /* PCI PowerManagement Capability reg group */
1721 .grp_id
= PCI_CAP_ID_PM
,
1722 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1723 .grp_size
= PCI_PM_SIZEOF
,
1724 .size_init
= xen_pt_reg_grp_size_init
,
1725 .emu_regs
= xen_pt_emu_reg_pm
,
1727 /* AGP Capability Structure reg group */
1729 .grp_id
= PCI_CAP_ID_AGP
,
1730 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1732 .size_init
= xen_pt_reg_grp_size_init
,
1734 /* Vital Product Data Capability Structure reg group */
1736 .grp_id
= PCI_CAP_ID_VPD
,
1737 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1739 .size_init
= xen_pt_reg_grp_size_init
,
1740 .emu_regs
= xen_pt_emu_reg_vpd
,
1742 /* Slot Identification reg group */
1744 .grp_id
= PCI_CAP_ID_SLOTID
,
1745 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1747 .size_init
= xen_pt_reg_grp_size_init
,
1749 /* MSI Capability Structure reg group */
1751 .grp_id
= PCI_CAP_ID_MSI
,
1752 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1754 .size_init
= xen_pt_msi_size_init
,
1755 .emu_regs
= xen_pt_emu_reg_msi
,
1757 /* PCI-X Capabilities List Item reg group */
1759 .grp_id
= PCI_CAP_ID_PCIX
,
1760 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1762 .size_init
= xen_pt_reg_grp_size_init
,
1764 /* Vendor Specific Capability Structure reg group */
1766 .grp_id
= PCI_CAP_ID_VNDR
,
1767 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1769 .size_init
= xen_pt_vendor_size_init
,
1770 .emu_regs
= xen_pt_emu_reg_vendor
,
1772 /* SHPC Capability List Item reg group */
1774 .grp_id
= PCI_CAP_ID_SHPC
,
1775 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1777 .size_init
= xen_pt_reg_grp_size_init
,
1779 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1781 .grp_id
= PCI_CAP_ID_SSVID
,
1782 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1784 .size_init
= xen_pt_reg_grp_size_init
,
1786 /* AGP 8x Capability Structure reg group */
1788 .grp_id
= PCI_CAP_ID_AGP3
,
1789 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1791 .size_init
= xen_pt_reg_grp_size_init
,
1793 /* PCI Express Capability Structure reg group */
1795 .grp_id
= PCI_CAP_ID_EXP
,
1796 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1798 .size_init
= xen_pt_pcie_size_init
,
1799 .emu_regs
= xen_pt_emu_reg_pcie
,
1801 /* MSI-X Capability Structure reg group */
1803 .grp_id
= PCI_CAP_ID_MSIX
,
1804 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1806 .size_init
= xen_pt_msix_size_init
,
1807 .emu_regs
= xen_pt_emu_reg_msix
,
1809 /* Intel IGD Opregion group */
1811 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1812 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1814 .size_init
= xen_pt_reg_grp_size_init
,
1815 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1822 /* initialize Capabilities Pointer or Next Pointer register */
1823 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1824 XenPTRegInfo
*reg
, uint32_t real_offset
,
1831 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1835 /* find capability offset */
1837 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1838 if (xen_pt_hide_dev_cap(&s
->real_device
,
1839 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1843 rc
= xen_host_pci_get_byte(&s
->real_device
,
1844 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1846 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1847 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1850 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1851 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1854 /* ignore the 0 hardwired capability, find next one */
1859 /* next capability */
1860 rc
= xen_host_pci_get_byte(&s
->real_device
,
1861 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1877 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1880 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1881 uint8_t pos
= PCI_CAPABILITY_LIST
;
1884 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1887 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1892 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1895 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1900 if (xen_host_pci_get_byte(&s
->real_device
,
1901 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1912 pos
+= PCI_CAP_LIST_NEXT
;
1917 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1918 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1921 XenPTReg
*reg_entry
;
1925 reg_entry
= g_new0(XenPTReg
, 1);
1926 reg_entry
->reg
= reg
;
1929 uint32_t host_mask
, size_mask
;
1930 unsigned int offset
;
1933 /* initialize emulate register */
1934 rc
= reg
->init(s
, reg_entry
->reg
,
1935 reg_grp
->base_offset
+ reg
->offset
, &data
);
1938 error_setg(errp
, "Init emulate register fail");
1941 if (data
== XEN_PT_INVALID_REG
) {
1942 /* free unused BAR register entry */
1946 /* Sync up the data to dev.config */
1947 offset
= reg_grp
->base_offset
+ reg
->offset
;
1948 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1950 switch (reg
->size
) {
1951 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1953 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1955 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1960 /* Serious issues when we cannot read the host values! */
1962 error_setg(errp
, "Cannot read host values");
1965 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1966 * contain the emulated view of the guest - therefore we flip the mask
1967 * to mask out the host values (which dev.config initially has) . */
1968 host_mask
= size_mask
& ~reg
->emu_mask
;
1970 if ((data
& host_mask
) != (val
& host_mask
)) {
1973 * Merge the emulated bits (data) with the host bits (val)
1974 * and mask out the bits past size to enable restoration
1975 * of the proper value for logging below.
1977 new_val
= XEN_PT_MERGE_VALUE(val
, data
, host_mask
) & size_mask
;
1978 /* Leave intact host and emulated values past the size - even though
1979 * we do not care as we write per reg->size granularity, but for the
1980 * logging below lets have the proper value. */
1981 new_val
|= ((val
| data
)) & ~size_mask
;
1982 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1983 offset
, data
, val
, new_val
);
1988 if (val
& ~size_mask
) {
1989 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1990 " register size (%d)", offset
, val
, reg
->size
);
1994 /* This could be just pci_set_long as we don't modify the bits
1995 * past reg->size, but in case this routine is run in parallel or the
1996 * init value is larger, we do not want to over-write registers. */
1997 switch (reg
->size
) {
1998 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
2000 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
2002 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
2006 /* set register value pointer to the data. */
2007 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
2010 /* list add register entry */
2011 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
2014 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
2019 QLIST_INIT(&s
->reg_grps
);
2021 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
2022 uint32_t reg_grp_offset
= 0;
2023 XenPTRegGroup
*reg_grp_entry
= NULL
;
2025 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2026 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2027 if (xen_pt_hide_dev_cap(&s
->real_device
,
2028 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2032 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2034 if (!reg_grp_offset
) {
2039 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2040 if (!is_igd_vga_passthrough(&s
->real_device
) ||
2041 s
->real_device
.vendor_id
!= PCI_VENDOR_ID_INTEL
) {
2045 * By default we will trap up to 0x40 in the cfg space.
2046 * If an intel device is pass through we need to trap 0xfc,
2047 * therefore the size should be 0xff.
2049 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2052 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2053 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2054 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2056 reg_grp_entry
->base_offset
= reg_grp_offset
;
2057 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2058 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2059 /* get register group size */
2060 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2062 ®_grp_entry
->size
);
2064 error_setg(errp
, "Failed to initialize %d/%zu, type = 0x%x,"
2065 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2066 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2067 xen_pt_config_delete(s
);
2072 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2073 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2075 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2077 /* initialize capability register */
2078 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2079 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, errp
);
2081 error_append_hint(errp
, "Failed to init register %d"
2082 " offsets 0x%x in grp_type = 0x%x (%d/%zu)",
2085 xen_pt_emu_reg_grps
[i
].grp_type
,
2086 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2087 xen_pt_config_delete(s
);
2096 /* delete all emulate register */
2097 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2099 struct XenPTRegGroup
*reg_group
, *next_grp
;
2100 struct XenPTReg
*reg
, *next_reg
;
2102 /* free MSI/MSI-X info table */
2104 xen_pt_msix_unmap(s
);
2108 /* free all register group entry */
2109 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2110 /* free all register entry */
2111 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2112 QLIST_REMOVE(reg
, entries
);
2116 QLIST_REMOVE(reg_group
, entries
);