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 "qemu/timer.h"
17 #include "hw/xen/xen_backend.h"
20 #define XEN_PT_MERGE_VALUE(value, data, val_mask) \
21 (((value) & (val_mask)) | ((data) & ~(val_mask)))
23 #define XEN_PT_INVALID_REG 0xFFFFFFFF /* invalid register value */
27 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
28 uint32_t real_offset
, uint32_t *data
);
33 /* A return value of 1 means the capability should NOT be exposed to guest. */
34 static int xen_pt_hide_dev_cap(const XenHostPCIDevice
*d
, uint8_t grp_id
)
38 /* The PCI Express Capability Structure of the VF of Intel 82599 10GbE
39 * Controller looks trivial, e.g., the PCI Express Capabilities
40 * Register is 0. We should not try to expose it to guest.
42 * The datasheet is available at
43 * http://download.intel.com/design/network/datashts/82599_datasheet.pdf
45 * See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the
46 * PCI Express Capability Structure of the VF of Intel 82599 10GbE
47 * Controller looks trivial, e.g., the PCI Express Capabilities
48 * Register is 0, so the Capability Version is 0 and
49 * xen_pt_pcie_size_init() would fail.
51 if (d
->vendor_id
== PCI_VENDOR_ID_INTEL
&&
52 d
->device_id
== PCI_DEVICE_ID_INTEL_82599_SFP_VF
) {
60 /* find emulate register group entry */
61 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
)
63 XenPTRegGroup
*entry
= NULL
;
65 /* find register group entry */
66 QLIST_FOREACH(entry
, &s
->reg_grps
, entries
) {
68 if ((entry
->base_offset
<= address
)
69 && ((entry
->base_offset
+ entry
->size
) > address
)) {
74 /* group entry not found */
78 /* find emulate register entry */
79 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
)
81 XenPTReg
*reg_entry
= NULL
;
82 XenPTRegInfo
*reg
= NULL
;
83 uint32_t real_offset
= 0;
85 /* find register entry */
86 QLIST_FOREACH(reg_entry
, ®_grp
->reg_tbl_list
, entries
) {
88 real_offset
= reg_grp
->base_offset
+ reg
->offset
;
90 if ((real_offset
<= address
)
91 && ((real_offset
+ reg
->size
) > address
)) {
99 static uint32_t get_throughable_mask(const XenPCIPassthroughState
*s
,
100 XenPTRegInfo
*reg
, uint32_t valid_mask
)
102 uint32_t throughable_mask
= ~(reg
->emu_mask
| reg
->ro_mask
);
104 if (!s
->permissive
) {
105 throughable_mask
&= ~reg
->res_mask
;
108 return throughable_mask
& valid_mask
;
112 * general register functions
115 /* register initialization function */
117 static int xen_pt_common_reg_init(XenPCIPassthroughState
*s
,
118 XenPTRegInfo
*reg
, uint32_t real_offset
,
121 *data
= reg
->init_val
;
125 /* Read register functions */
127 static int xen_pt_byte_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
128 uint8_t *value
, uint8_t valid_mask
)
130 XenPTRegInfo
*reg
= cfg_entry
->reg
;
131 uint8_t valid_emu_mask
= 0;
132 uint8_t *data
= cfg_entry
->ptr
.byte
;
134 /* emulate byte register */
135 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
136 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
140 static int xen_pt_word_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
141 uint16_t *value
, uint16_t valid_mask
)
143 XenPTRegInfo
*reg
= cfg_entry
->reg
;
144 uint16_t valid_emu_mask
= 0;
145 uint16_t *data
= cfg_entry
->ptr
.half_word
;
147 /* emulate word register */
148 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
149 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
153 static int xen_pt_long_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
154 uint32_t *value
, uint32_t valid_mask
)
156 XenPTRegInfo
*reg
= cfg_entry
->reg
;
157 uint32_t valid_emu_mask
= 0;
158 uint32_t *data
= cfg_entry
->ptr
.word
;
160 /* emulate long register */
161 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
162 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
167 /* Write register functions */
169 static int xen_pt_byte_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
170 uint8_t *val
, uint8_t dev_value
,
173 XenPTRegInfo
*reg
= cfg_entry
->reg
;
174 uint8_t writable_mask
= 0;
175 uint8_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
176 uint8_t *data
= cfg_entry
->ptr
.byte
;
178 /* modify emulate register */
179 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
180 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
182 /* create value for writing to I/O device register */
183 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
188 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
189 uint16_t *val
, uint16_t dev_value
,
192 XenPTRegInfo
*reg
= cfg_entry
->reg
;
193 uint16_t writable_mask
= 0;
194 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
195 uint16_t *data
= cfg_entry
->ptr
.half_word
;
197 /* modify emulate register */
198 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
199 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
201 /* create value for writing to I/O device register */
202 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
207 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
208 uint32_t *val
, uint32_t dev_value
,
211 XenPTRegInfo
*reg
= cfg_entry
->reg
;
212 uint32_t writable_mask
= 0;
213 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
214 uint32_t *data
= cfg_entry
->ptr
.word
;
216 /* modify emulate register */
217 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
218 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
220 /* create value for writing to I/O device register */
221 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
228 /* XenPTRegInfo declaration
229 * - only for emulated register (either a part or whole bit).
230 * - for passthrough register that need special behavior (like interacting with
231 * other component), set emu_mask to all 0 and specify r/w func properly.
232 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
235 /********************
239 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
240 XenPTRegInfo
*reg
, uint32_t real_offset
,
243 *data
= s
->real_device
.vendor_id
;
246 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
247 XenPTRegInfo
*reg
, uint32_t real_offset
,
250 *data
= s
->real_device
.device_id
;
253 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
254 XenPTRegInfo
*reg
, uint32_t real_offset
,
257 XenPTRegGroup
*reg_grp_entry
= NULL
;
258 XenPTReg
*reg_entry
= NULL
;
259 uint32_t reg_field
= 0;
261 /* find Header register group */
262 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
264 /* find Capabilities Pointer register */
265 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
267 /* check Capabilities Pointer register */
268 if (*reg_entry
->ptr
.half_word
) {
269 reg_field
|= PCI_STATUS_CAP_LIST
;
271 reg_field
&= ~PCI_STATUS_CAP_LIST
;
274 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
275 " for Capabilities Pointer register."
276 " (%s)\n", __func__
);
280 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
281 " for Header. (%s)\n", __func__
);
288 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
289 XenPTRegInfo
*reg
, uint32_t real_offset
,
292 /* read PCI_HEADER_TYPE */
293 *data
= reg
->init_val
| 0x80;
297 /* initialize Interrupt Pin register */
298 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
299 XenPTRegInfo
*reg
, uint32_t real_offset
,
302 *data
= xen_pt_pci_read_intx(s
);
306 /* Command register */
307 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
308 uint16_t *val
, uint16_t dev_value
,
311 XenPTRegInfo
*reg
= cfg_entry
->reg
;
312 uint16_t writable_mask
= 0;
313 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
314 uint16_t *data
= cfg_entry
->ptr
.half_word
;
316 /* modify emulate register */
317 writable_mask
= ~reg
->ro_mask
& valid_mask
;
318 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
320 /* create value for writing to I/O device register */
321 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
322 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
324 if (s
->machine_irq
) {
325 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
329 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
335 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
336 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
337 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
338 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
340 static bool is_64bit_bar(PCIIORegion
*r
)
342 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
345 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
347 if (is_64bit_bar(r
)) {
349 size64
= (r
+ 1)->size
;
357 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
360 PCIDevice
*d
= &s
->dev
;
361 XenPTRegion
*region
= NULL
;
364 /* check 64bit BAR */
365 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
366 int type
= s
->real_device
.io_regions
[index
- 1].type
;
368 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
369 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
370 region
= &s
->bases
[index
- 1];
371 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
372 return XEN_PT_BAR_FLAG_UPPER
;
377 /* check unused BAR */
378 r
= &d
->io_regions
[index
];
379 if (!xen_pt_get_bar_size(r
)) {
380 return XEN_PT_BAR_FLAG_UNUSED
;
384 if (index
== PCI_ROM_SLOT
) {
385 return XEN_PT_BAR_FLAG_MEM
;
388 /* check BAR I/O indicator */
389 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
390 return XEN_PT_BAR_FLAG_IO
;
392 return XEN_PT_BAR_FLAG_MEM
;
396 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
398 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
399 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
401 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
405 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
406 uint32_t real_offset
, uint32_t *data
)
408 uint32_t reg_field
= 0;
411 index
= xen_pt_bar_offset_to_index(reg
->offset
);
412 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
413 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
418 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
419 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
420 reg_field
= XEN_PT_INVALID_REG
;
426 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
427 uint32_t *value
, uint32_t valid_mask
)
429 XenPTRegInfo
*reg
= cfg_entry
->reg
;
430 uint32_t valid_emu_mask
= 0;
431 uint32_t bar_emu_mask
= 0;
435 index
= xen_pt_bar_offset_to_index(reg
->offset
);
436 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
437 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
441 /* use fixed-up value from kernel sysfs */
442 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
444 /* set emulate mask depend on BAR flag */
445 switch (s
->bases
[index
].bar_flag
) {
446 case XEN_PT_BAR_FLAG_MEM
:
447 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
449 case XEN_PT_BAR_FLAG_IO
:
450 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
452 case XEN_PT_BAR_FLAG_UPPER
:
453 bar_emu_mask
= XEN_PT_BAR_ALLF
;
460 valid_emu_mask
= bar_emu_mask
& valid_mask
;
461 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
465 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
466 uint32_t *val
, uint32_t dev_value
,
469 XenPTRegInfo
*reg
= cfg_entry
->reg
;
470 XenPTRegion
*base
= NULL
;
471 PCIDevice
*d
= &s
->dev
;
472 const PCIIORegion
*r
;
473 uint32_t writable_mask
= 0;
474 uint32_t bar_emu_mask
= 0;
475 uint32_t bar_ro_mask
= 0;
478 uint32_t *data
= cfg_entry
->ptr
.word
;
480 index
= xen_pt_bar_offset_to_index(reg
->offset
);
481 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
482 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
486 r
= &d
->io_regions
[index
];
487 base
= &s
->bases
[index
];
488 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
490 /* set emulate mask and read-only mask values depend on the BAR flag */
491 switch (s
->bases
[index
].bar_flag
) {
492 case XEN_PT_BAR_FLAG_MEM
:
493 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
495 /* low 32 bits mask for 64 bit bars */
496 bar_ro_mask
= XEN_PT_BAR_ALLF
;
498 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
501 case XEN_PT_BAR_FLAG_IO
:
502 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
503 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
505 case XEN_PT_BAR_FLAG_UPPER
:
506 bar_emu_mask
= XEN_PT_BAR_ALLF
;
507 bar_ro_mask
= r_size
? r_size
- 1 : 0;
513 /* modify emulate register */
514 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
515 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
517 /* check whether we need to update the virtual region address or not */
518 switch (s
->bases
[index
].bar_flag
) {
519 case XEN_PT_BAR_FLAG_UPPER
:
520 case XEN_PT_BAR_FLAG_MEM
:
523 case XEN_PT_BAR_FLAG_IO
:
530 /* create value for writing to I/O device register */
531 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
536 /* write Exp ROM BAR */
537 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
538 XenPTReg
*cfg_entry
, uint32_t *val
,
539 uint32_t dev_value
, uint32_t valid_mask
)
541 XenPTRegInfo
*reg
= cfg_entry
->reg
;
542 XenPTRegion
*base
= NULL
;
543 PCIDevice
*d
= (PCIDevice
*)&s
->dev
;
544 uint32_t writable_mask
= 0;
545 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
547 uint32_t bar_ro_mask
= 0;
548 uint32_t *data
= cfg_entry
->ptr
.word
;
550 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
551 base
= &s
->bases
[PCI_ROM_SLOT
];
552 /* align memory type resource size */
553 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
555 /* set emulate mask and read-only mask */
556 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
558 /* modify emulate register */
559 writable_mask
= ~bar_ro_mask
& valid_mask
;
560 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
562 /* create value for writing to I/O device register */
563 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
568 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
570 uint32_t *value
, uint32_t valid_mask
)
572 *value
= igd_read_opregion(s
);
576 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
577 XenPTReg
*cfg_entry
, uint32_t *value
,
578 uint32_t dev_value
, uint32_t valid_mask
)
580 igd_write_opregion(s
, *value
);
584 /* Header Type0 reg static information table */
585 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
588 .offset
= PCI_VENDOR_ID
,
593 .init
= xen_pt_vendor_reg_init
,
594 .u
.w
.read
= xen_pt_word_reg_read
,
595 .u
.w
.write
= xen_pt_word_reg_write
,
599 .offset
= PCI_DEVICE_ID
,
604 .init
= xen_pt_device_reg_init
,
605 .u
.w
.read
= xen_pt_word_reg_read
,
606 .u
.w
.write
= xen_pt_word_reg_write
,
610 .offset
= PCI_COMMAND
,
615 .init
= xen_pt_common_reg_init
,
616 .u
.w
.read
= xen_pt_word_reg_read
,
617 .u
.w
.write
= xen_pt_cmd_reg_write
,
619 /* Capabilities Pointer reg */
621 .offset
= PCI_CAPABILITY_LIST
,
626 .init
= xen_pt_ptr_reg_init
,
627 .u
.b
.read
= xen_pt_byte_reg_read
,
628 .u
.b
.write
= xen_pt_byte_reg_write
,
631 /* use emulated Cap Ptr value to initialize,
632 * so need to be declared after Cap Ptr reg
635 .offset
= PCI_STATUS
,
642 .init
= xen_pt_status_reg_init
,
643 .u
.w
.read
= xen_pt_word_reg_read
,
644 .u
.w
.write
= xen_pt_word_reg_write
,
646 /* Cache Line Size reg */
648 .offset
= PCI_CACHE_LINE_SIZE
,
653 .init
= xen_pt_common_reg_init
,
654 .u
.b
.read
= xen_pt_byte_reg_read
,
655 .u
.b
.write
= xen_pt_byte_reg_write
,
657 /* Latency Timer reg */
659 .offset
= PCI_LATENCY_TIMER
,
664 .init
= xen_pt_common_reg_init
,
665 .u
.b
.read
= xen_pt_byte_reg_read
,
666 .u
.b
.write
= xen_pt_byte_reg_write
,
668 /* Header Type reg */
670 .offset
= PCI_HEADER_TYPE
,
675 .init
= xen_pt_header_type_reg_init
,
676 .u
.b
.read
= xen_pt_byte_reg_read
,
677 .u
.b
.write
= xen_pt_byte_reg_write
,
679 /* Interrupt Line reg */
681 .offset
= PCI_INTERRUPT_LINE
,
686 .init
= xen_pt_common_reg_init
,
687 .u
.b
.read
= xen_pt_byte_reg_read
,
688 .u
.b
.write
= xen_pt_byte_reg_write
,
690 /* Interrupt Pin reg */
692 .offset
= PCI_INTERRUPT_PIN
,
697 .init
= xen_pt_irqpin_reg_init
,
698 .u
.b
.read
= xen_pt_byte_reg_read
,
699 .u
.b
.write
= xen_pt_byte_reg_write
,
702 /* mask of BAR need to be decided later, depends on IO/MEM type */
704 .offset
= PCI_BASE_ADDRESS_0
,
706 .init_val
= 0x00000000,
707 .init
= xen_pt_bar_reg_init
,
708 .u
.dw
.read
= xen_pt_bar_reg_read
,
709 .u
.dw
.write
= xen_pt_bar_reg_write
,
713 .offset
= PCI_BASE_ADDRESS_1
,
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_2
,
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_3
,
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_4
,
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_5
,
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
,
756 /* Expansion ROM BAR reg */
758 .offset
= PCI_ROM_ADDRESS
,
760 .init_val
= 0x00000000,
761 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
762 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
763 .init
= xen_pt_bar_reg_init
,
764 .u
.dw
.read
= xen_pt_long_reg_read
,
765 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
773 /*********************************
774 * Vital Product Data Capability
777 /* Vital Product Data Capability Structure reg static information table */
778 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
780 .offset
= PCI_CAP_LIST_NEXT
,
785 .init
= xen_pt_ptr_reg_init
,
786 .u
.b
.read
= xen_pt_byte_reg_read
,
787 .u
.b
.write
= xen_pt_byte_reg_write
,
790 .offset
= PCI_VPD_ADDR
,
794 .init
= xen_pt_common_reg_init
,
795 .u
.w
.read
= xen_pt_word_reg_read
,
796 .u
.w
.write
= xen_pt_word_reg_write
,
804 /**************************************
805 * Vendor Specific Capability
808 /* Vendor Specific Capability Structure reg static information table */
809 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
811 .offset
= PCI_CAP_LIST_NEXT
,
816 .init
= xen_pt_ptr_reg_init
,
817 .u
.b
.read
= xen_pt_byte_reg_read
,
818 .u
.b
.write
= xen_pt_byte_reg_write
,
826 /*****************************
827 * PCI Express Capability
830 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
834 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
837 return flag
& PCI_EXP_FLAGS_VERS
;
840 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
844 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
847 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
850 /* initialize Link Control register */
851 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
852 XenPTRegInfo
*reg
, uint32_t real_offset
,
855 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
856 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
858 /* no need to initialize in case of Root Complex Integrated Endpoint
861 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
862 *data
= XEN_PT_INVALID_REG
;
865 *data
= reg
->init_val
;
868 /* initialize Device Control 2 register */
869 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
870 XenPTRegInfo
*reg
, uint32_t real_offset
,
873 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
875 /* no need to initialize in case of cap_ver 1.x */
877 *data
= XEN_PT_INVALID_REG
;
880 *data
= reg
->init_val
;
883 /* initialize Link Control 2 register */
884 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
885 XenPTRegInfo
*reg
, uint32_t real_offset
,
888 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
889 uint32_t reg_field
= 0;
891 /* no need to initialize in case of cap_ver 1.x */
893 reg_field
= XEN_PT_INVALID_REG
;
895 /* set Supported Link Speed */
898 rc
= xen_host_pci_get_byte(&s
->real_device
,
899 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
904 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
911 /* PCI Express Capability Structure reg static information table */
912 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
913 /* Next Pointer reg */
915 .offset
= PCI_CAP_LIST_NEXT
,
920 .init
= xen_pt_ptr_reg_init
,
921 .u
.b
.read
= xen_pt_byte_reg_read
,
922 .u
.b
.write
= xen_pt_byte_reg_write
,
924 /* Device Capabilities reg */
926 .offset
= PCI_EXP_DEVCAP
,
928 .init_val
= 0x00000000,
929 .ro_mask
= 0xFFFFFFFF,
930 .emu_mask
= 0x10000000,
931 .init
= xen_pt_common_reg_init
,
932 .u
.dw
.read
= xen_pt_long_reg_read
,
933 .u
.dw
.write
= xen_pt_long_reg_write
,
935 /* Device Control reg */
937 .offset
= PCI_EXP_DEVCTL
,
942 .init
= xen_pt_common_reg_init
,
943 .u
.w
.read
= xen_pt_word_reg_read
,
944 .u
.w
.write
= xen_pt_word_reg_write
,
946 /* Device Status reg */
948 .offset
= PCI_EXP_DEVSTA
,
953 .init
= xen_pt_common_reg_init
,
954 .u
.w
.read
= xen_pt_word_reg_read
,
955 .u
.w
.write
= xen_pt_word_reg_write
,
957 /* Link Control reg */
959 .offset
= PCI_EXP_LNKCTL
,
964 .init
= xen_pt_linkctrl_reg_init
,
965 .u
.w
.read
= xen_pt_word_reg_read
,
966 .u
.w
.write
= xen_pt_word_reg_write
,
968 /* Link Status reg */
970 .offset
= PCI_EXP_LNKSTA
,
974 .init
= xen_pt_common_reg_init
,
975 .u
.w
.read
= xen_pt_word_reg_read
,
976 .u
.w
.write
= xen_pt_word_reg_write
,
978 /* Device Control 2 reg */
985 .init
= xen_pt_devctrl2_reg_init
,
986 .u
.w
.read
= xen_pt_word_reg_read
,
987 .u
.w
.write
= xen_pt_word_reg_write
,
989 /* Link Control 2 reg */
996 .init
= xen_pt_linkctrl2_reg_init
,
997 .u
.w
.read
= xen_pt_word_reg_read
,
998 .u
.w
.write
= xen_pt_word_reg_write
,
1006 /*********************************
1007 * Power Management Capability
1010 /* Power Management Capability reg static information table */
1011 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1012 /* Next Pointer reg */
1014 .offset
= PCI_CAP_LIST_NEXT
,
1019 .init
= xen_pt_ptr_reg_init
,
1020 .u
.b
.read
= xen_pt_byte_reg_read
,
1021 .u
.b
.write
= xen_pt_byte_reg_write
,
1023 /* Power Management Capabilities reg */
1025 .offset
= PCI_CAP_FLAGS
,
1030 .init
= xen_pt_common_reg_init
,
1031 .u
.w
.read
= xen_pt_word_reg_read
,
1032 .u
.w
.write
= xen_pt_word_reg_write
,
1034 /* PCI Power Management Control/Status reg */
1036 .offset
= PCI_PM_CTRL
,
1041 .rw1c_mask
= 0x8000,
1043 .init
= xen_pt_common_reg_init
,
1044 .u
.w
.read
= xen_pt_word_reg_read
,
1045 .u
.w
.write
= xen_pt_word_reg_write
,
1053 /********************************
1058 #define xen_pt_msi_check_type(offset, flags, what) \
1059 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1060 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1062 /* Message Control register */
1063 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1064 XenPTRegInfo
*reg
, uint32_t real_offset
,
1067 XenPTMSI
*msi
= s
->msi
;
1071 /* use I/O device register's value as initial value */
1072 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1076 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1077 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1078 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1079 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1081 msi
->flags
|= reg_field
;
1082 msi
->ctrl_offset
= real_offset
;
1083 msi
->initialized
= false;
1084 msi
->mapped
= false;
1086 *data
= reg
->init_val
;
1089 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1090 XenPTReg
*cfg_entry
, uint16_t *val
,
1091 uint16_t dev_value
, uint16_t valid_mask
)
1093 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1094 XenPTMSI
*msi
= s
->msi
;
1095 uint16_t writable_mask
= 0;
1096 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1097 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1099 /* Currently no support for multi-vector */
1100 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1101 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1104 /* modify emulate register */
1105 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1106 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1107 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1109 /* create value for writing to I/O device register */
1110 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1113 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1114 /* setup MSI pirq for the first time */
1115 if (!msi
->initialized
) {
1116 /* Init physical one */
1117 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1118 if (xen_pt_msi_setup(s
)) {
1119 /* We do not broadcast the error to the framework code, so
1120 * that MSI errors are contained in MSI emulation code and
1121 * QEMU can go on running.
1122 * Guest MSI would be actually not working.
1124 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1125 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1128 if (xen_pt_msi_update(s
)) {
1129 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1130 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1133 msi
->initialized
= true;
1136 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1137 } else if (msi
->mapped
) {
1138 xen_pt_msi_disable(s
);
1144 /* initialize Message Upper Address register */
1145 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1146 XenPTRegInfo
*reg
, uint32_t real_offset
,
1149 /* no need to initialize in case of 32 bit type */
1150 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1151 *data
= XEN_PT_INVALID_REG
;
1153 *data
= reg
->init_val
;
1158 /* this function will be called twice (for 32 bit and 64 bit type) */
1159 /* initialize Message Data register */
1160 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1161 XenPTRegInfo
*reg
, uint32_t real_offset
,
1164 uint32_t flags
= s
->msi
->flags
;
1165 uint32_t offset
= reg
->offset
;
1167 /* check the offset whether matches the type or not */
1168 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1169 *data
= reg
->init_val
;
1171 *data
= XEN_PT_INVALID_REG
;
1176 /* this function will be called twice (for 32 bit and 64 bit type) */
1177 /* initialize Mask register */
1178 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1179 XenPTRegInfo
*reg
, uint32_t real_offset
,
1182 uint32_t flags
= s
->msi
->flags
;
1184 /* check the offset whether matches the type or not */
1185 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1186 *data
= XEN_PT_INVALID_REG
;
1187 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1188 *data
= reg
->init_val
;
1190 *data
= XEN_PT_INVALID_REG
;
1195 /* this function will be called twice (for 32 bit and 64 bit type) */
1196 /* initialize Pending register */
1197 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1198 XenPTRegInfo
*reg
, uint32_t real_offset
,
1201 uint32_t flags
= s
->msi
->flags
;
1203 /* check the offset whether matches the type or not */
1204 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1205 *data
= XEN_PT_INVALID_REG
;
1206 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1207 *data
= reg
->init_val
;
1209 *data
= XEN_PT_INVALID_REG
;
1214 /* write Message Address register */
1215 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1216 XenPTReg
*cfg_entry
, uint32_t *val
,
1217 uint32_t dev_value
, uint32_t valid_mask
)
1219 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1220 uint32_t writable_mask
= 0;
1221 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1222 uint32_t *data
= cfg_entry
->ptr
.word
;
1224 /* modify emulate register */
1225 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1226 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1227 s
->msi
->addr_lo
= *data
;
1229 /* create value for writing to I/O device register */
1230 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1233 if (*data
!= old_addr
) {
1234 if (s
->msi
->mapped
) {
1235 xen_pt_msi_update(s
);
1241 /* write Message Upper Address register */
1242 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1243 XenPTReg
*cfg_entry
, uint32_t *val
,
1244 uint32_t dev_value
, uint32_t valid_mask
)
1246 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1247 uint32_t writable_mask
= 0;
1248 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1249 uint32_t *data
= cfg_entry
->ptr
.word
;
1251 /* check whether the type is 64 bit or not */
1252 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1254 "Can't write to the upper address without 64 bit support\n");
1258 /* modify emulate register */
1259 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1260 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1261 /* update the msi_info too */
1262 s
->msi
->addr_hi
= *data
;
1264 /* create value for writing to I/O device register */
1265 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1268 if (*data
!= old_addr
) {
1269 if (s
->msi
->mapped
) {
1270 xen_pt_msi_update(s
);
1278 /* this function will be called twice (for 32 bit and 64 bit type) */
1279 /* write Message Data register */
1280 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1281 XenPTReg
*cfg_entry
, uint16_t *val
,
1282 uint16_t dev_value
, uint16_t valid_mask
)
1284 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1285 XenPTMSI
*msi
= s
->msi
;
1286 uint16_t writable_mask
= 0;
1287 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1288 uint32_t offset
= reg
->offset
;
1289 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1291 /* check the offset whether matches the type or not */
1292 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1293 /* exit I/O emulator */
1294 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1298 /* modify emulate register */
1299 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1300 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1301 /* update the msi_info too */
1304 /* create value for writing to I/O device register */
1305 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1308 if (*data
!= old_data
) {
1310 xen_pt_msi_update(s
);
1317 /* MSI Capability Structure reg static information table */
1318 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1319 /* Next Pointer reg */
1321 .offset
= PCI_CAP_LIST_NEXT
,
1326 .init
= xen_pt_ptr_reg_init
,
1327 .u
.b
.read
= xen_pt_byte_reg_read
,
1328 .u
.b
.write
= xen_pt_byte_reg_write
,
1330 /* Message Control reg */
1332 .offset
= PCI_MSI_FLAGS
,
1338 .init
= xen_pt_msgctrl_reg_init
,
1339 .u
.w
.read
= xen_pt_word_reg_read
,
1340 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1342 /* Message Address reg */
1344 .offset
= PCI_MSI_ADDRESS_LO
,
1346 .init_val
= 0x00000000,
1347 .ro_mask
= 0x00000003,
1348 .emu_mask
= 0xFFFFFFFF,
1349 .init
= xen_pt_common_reg_init
,
1350 .u
.dw
.read
= xen_pt_long_reg_read
,
1351 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1353 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1355 .offset
= PCI_MSI_ADDRESS_HI
,
1357 .init_val
= 0x00000000,
1358 .ro_mask
= 0x00000000,
1359 .emu_mask
= 0xFFFFFFFF,
1360 .init
= xen_pt_msgaddr64_reg_init
,
1361 .u
.dw
.read
= xen_pt_long_reg_read
,
1362 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1364 /* Message Data reg (16 bits of data for 32-bit devices) */
1366 .offset
= PCI_MSI_DATA_32
,
1371 .init
= xen_pt_msgdata_reg_init
,
1372 .u
.w
.read
= xen_pt_word_reg_read
,
1373 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1375 /* Message Data reg (16 bits of data for 64-bit devices) */
1377 .offset
= PCI_MSI_DATA_64
,
1382 .init
= xen_pt_msgdata_reg_init
,
1383 .u
.w
.read
= xen_pt_word_reg_read
,
1384 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1386 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1388 .offset
= PCI_MSI_MASK_32
,
1390 .init_val
= 0x00000000,
1391 .ro_mask
= 0xFFFFFFFF,
1392 .emu_mask
= 0xFFFFFFFF,
1393 .init
= xen_pt_mask_reg_init
,
1394 .u
.dw
.read
= xen_pt_long_reg_read
,
1395 .u
.dw
.write
= xen_pt_long_reg_write
,
1397 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1399 .offset
= PCI_MSI_MASK_64
,
1401 .init_val
= 0x00000000,
1402 .ro_mask
= 0xFFFFFFFF,
1403 .emu_mask
= 0xFFFFFFFF,
1404 .init
= xen_pt_mask_reg_init
,
1405 .u
.dw
.read
= xen_pt_long_reg_read
,
1406 .u
.dw
.write
= xen_pt_long_reg_write
,
1408 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1410 .offset
= PCI_MSI_MASK_32
+ 4,
1412 .init_val
= 0x00000000,
1413 .ro_mask
= 0xFFFFFFFF,
1414 .emu_mask
= 0x00000000,
1415 .init
= xen_pt_pending_reg_init
,
1416 .u
.dw
.read
= xen_pt_long_reg_read
,
1417 .u
.dw
.write
= xen_pt_long_reg_write
,
1419 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1421 .offset
= PCI_MSI_MASK_64
+ 4,
1423 .init_val
= 0x00000000,
1424 .ro_mask
= 0xFFFFFFFF,
1425 .emu_mask
= 0x00000000,
1426 .init
= xen_pt_pending_reg_init
,
1427 .u
.dw
.read
= xen_pt_long_reg_read
,
1428 .u
.dw
.write
= xen_pt_long_reg_write
,
1436 /**************************************
1440 /* Message Control register for MSI-X */
1441 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1442 XenPTRegInfo
*reg
, uint32_t real_offset
,
1448 /* use I/O device register's value as initial value */
1449 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1453 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1454 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1455 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1456 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1459 s
->msix
->ctrl_offset
= real_offset
;
1461 *data
= reg
->init_val
;
1464 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1465 XenPTReg
*cfg_entry
, uint16_t *val
,
1466 uint16_t dev_value
, uint16_t valid_mask
)
1468 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1469 uint16_t writable_mask
= 0;
1470 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1471 int debug_msix_enabled_old
;
1472 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1474 /* modify emulate register */
1475 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1476 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1478 /* create value for writing to I/O device register */
1479 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1482 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1483 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1484 xen_pt_msix_update(s
);
1485 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1486 xen_pt_msix_disable(s
);
1489 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1491 debug_msix_enabled_old
= s
->msix
->enabled
;
1492 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1493 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1494 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1495 s
->msix
->enabled
? "enable" : "disable");
1501 /* MSI-X Capability Structure reg static information table */
1502 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1503 /* Next Pointer reg */
1505 .offset
= PCI_CAP_LIST_NEXT
,
1510 .init
= xen_pt_ptr_reg_init
,
1511 .u
.b
.read
= xen_pt_byte_reg_read
,
1512 .u
.b
.write
= xen_pt_byte_reg_write
,
1514 /* Message Control reg */
1516 .offset
= PCI_MSI_FLAGS
,
1522 .init
= xen_pt_msixctrl_reg_init
,
1523 .u
.w
.read
= xen_pt_word_reg_read
,
1524 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1531 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1532 /* Intel IGFX OpRegion reg */
1537 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1538 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1545 /****************************
1549 /* capability structure register group size functions */
1551 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1552 const XenPTRegGroupInfo
*grp_reg
,
1553 uint32_t base_offset
, uint8_t *size
)
1555 *size
= grp_reg
->grp_size
;
1558 /* get Vendor Specific Capability Structure register group size */
1559 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1560 const XenPTRegGroupInfo
*grp_reg
,
1561 uint32_t base_offset
, uint8_t *size
)
1563 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1565 /* get PCI Express Capability Structure register group size */
1566 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1567 const XenPTRegGroupInfo
*grp_reg
,
1568 uint32_t base_offset
, uint8_t *size
)
1570 PCIDevice
*d
= &s
->dev
;
1571 uint8_t version
= get_capability_version(s
, base_offset
);
1572 uint8_t type
= get_device_type(s
, base_offset
);
1573 uint8_t pcie_size
= 0;
1576 /* calculate size depend on capability version and device/port type */
1577 /* in case of PCI Express Base Specification Rev 1.x */
1579 /* The PCI Express Capabilities, Device Capabilities, and Device
1580 * Status/Control registers are required for all PCI Express devices.
1581 * The Link Capabilities and Link Status/Control are required for all
1582 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1583 * are not required to implement registers other than those listed
1584 * above and terminate the capability structure.
1587 case PCI_EXP_TYPE_ENDPOINT
:
1588 case PCI_EXP_TYPE_LEG_END
:
1591 case PCI_EXP_TYPE_RC_END
:
1595 /* only EndPoint passthrough is supported */
1596 case PCI_EXP_TYPE_ROOT_PORT
:
1597 case PCI_EXP_TYPE_UPSTREAM
:
1598 case PCI_EXP_TYPE_DOWNSTREAM
:
1599 case PCI_EXP_TYPE_PCI_BRIDGE
:
1600 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1601 case PCI_EXP_TYPE_RC_EC
:
1603 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1607 /* in case of PCI Express Base Specification Rev 2.0 */
1608 else if (version
== 2) {
1610 case PCI_EXP_TYPE_ENDPOINT
:
1611 case PCI_EXP_TYPE_LEG_END
:
1612 case PCI_EXP_TYPE_RC_END
:
1613 /* For Functions that do not implement the registers,
1614 * these spaces must be hardwired to 0b.
1618 /* only EndPoint passthrough is supported */
1619 case PCI_EXP_TYPE_ROOT_PORT
:
1620 case PCI_EXP_TYPE_UPSTREAM
:
1621 case PCI_EXP_TYPE_DOWNSTREAM
:
1622 case PCI_EXP_TYPE_PCI_BRIDGE
:
1623 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1624 case PCI_EXP_TYPE_RC_EC
:
1626 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1630 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1637 /* get MSI Capability Structure register group size */
1638 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1639 const XenPTRegGroupInfo
*grp_reg
,
1640 uint32_t base_offset
, uint8_t *size
)
1642 uint16_t msg_ctrl
= 0;
1643 uint8_t msi_size
= 0xa;
1646 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1651 /* check if 64-bit address is capable of per-vector masking */
1652 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1655 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1659 s
->msi
= g_new0(XenPTMSI
, 1);
1660 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1665 /* get MSI-X Capability Structure register group size */
1666 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1667 const XenPTRegGroupInfo
*grp_reg
,
1668 uint32_t base_offset
, uint8_t *size
)
1672 rc
= xen_pt_msix_init(s
, base_offset
);
1675 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1679 *size
= grp_reg
->grp_size
;
1684 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1685 /* Header Type0 reg group */
1688 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1690 .size_init
= xen_pt_reg_grp_size_init
,
1691 .emu_regs
= xen_pt_emu_reg_header0
,
1693 /* PCI PowerManagement Capability reg group */
1695 .grp_id
= PCI_CAP_ID_PM
,
1696 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1697 .grp_size
= PCI_PM_SIZEOF
,
1698 .size_init
= xen_pt_reg_grp_size_init
,
1699 .emu_regs
= xen_pt_emu_reg_pm
,
1701 /* AGP Capability Structure reg group */
1703 .grp_id
= PCI_CAP_ID_AGP
,
1704 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1706 .size_init
= xen_pt_reg_grp_size_init
,
1708 /* Vital Product Data Capability Structure reg group */
1710 .grp_id
= PCI_CAP_ID_VPD
,
1711 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1713 .size_init
= xen_pt_reg_grp_size_init
,
1714 .emu_regs
= xen_pt_emu_reg_vpd
,
1716 /* Slot Identification reg group */
1718 .grp_id
= PCI_CAP_ID_SLOTID
,
1719 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1721 .size_init
= xen_pt_reg_grp_size_init
,
1723 /* MSI Capability Structure reg group */
1725 .grp_id
= PCI_CAP_ID_MSI
,
1726 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1728 .size_init
= xen_pt_msi_size_init
,
1729 .emu_regs
= xen_pt_emu_reg_msi
,
1731 /* PCI-X Capabilities List Item reg group */
1733 .grp_id
= PCI_CAP_ID_PCIX
,
1734 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1736 .size_init
= xen_pt_reg_grp_size_init
,
1738 /* Vendor Specific Capability Structure reg group */
1740 .grp_id
= PCI_CAP_ID_VNDR
,
1741 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1743 .size_init
= xen_pt_vendor_size_init
,
1744 .emu_regs
= xen_pt_emu_reg_vendor
,
1746 /* SHPC Capability List Item reg group */
1748 .grp_id
= PCI_CAP_ID_SHPC
,
1749 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1751 .size_init
= xen_pt_reg_grp_size_init
,
1753 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1755 .grp_id
= PCI_CAP_ID_SSVID
,
1756 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1758 .size_init
= xen_pt_reg_grp_size_init
,
1760 /* AGP 8x Capability Structure reg group */
1762 .grp_id
= PCI_CAP_ID_AGP3
,
1763 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1765 .size_init
= xen_pt_reg_grp_size_init
,
1767 /* PCI Express Capability Structure reg group */
1769 .grp_id
= PCI_CAP_ID_EXP
,
1770 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1772 .size_init
= xen_pt_pcie_size_init
,
1773 .emu_regs
= xen_pt_emu_reg_pcie
,
1775 /* MSI-X Capability Structure reg group */
1777 .grp_id
= PCI_CAP_ID_MSIX
,
1778 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1780 .size_init
= xen_pt_msix_size_init
,
1781 .emu_regs
= xen_pt_emu_reg_msix
,
1783 /* Intel IGD Opregion group */
1785 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1786 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1788 .size_init
= xen_pt_reg_grp_size_init
,
1789 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1796 /* initialize Capabilities Pointer or Next Pointer register */
1797 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1798 XenPTRegInfo
*reg
, uint32_t real_offset
,
1805 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1809 /* find capability offset */
1811 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1812 if (xen_pt_hide_dev_cap(&s
->real_device
,
1813 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1817 rc
= xen_host_pci_get_byte(&s
->real_device
,
1818 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1820 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1821 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1824 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1825 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1828 /* ignore the 0 hardwired capability, find next one */
1833 /* next capability */
1834 rc
= xen_host_pci_get_byte(&s
->real_device
,
1835 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1851 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1854 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1855 uint8_t pos
= PCI_CAPABILITY_LIST
;
1858 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1861 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1866 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1869 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1874 if (xen_host_pci_get_byte(&s
->real_device
,
1875 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1886 pos
+= PCI_CAP_LIST_NEXT
;
1891 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1892 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1895 XenPTReg
*reg_entry
;
1899 reg_entry
= g_new0(XenPTReg
, 1);
1900 reg_entry
->reg
= reg
;
1903 uint32_t host_mask
, size_mask
;
1904 unsigned int offset
;
1907 /* initialize emulate register */
1908 rc
= reg
->init(s
, reg_entry
->reg
,
1909 reg_grp
->base_offset
+ reg
->offset
, &data
);
1912 error_setg(errp
, "Init emulate register fail");
1915 if (data
== XEN_PT_INVALID_REG
) {
1916 /* free unused BAR register entry */
1920 /* Sync up the data to dev.config */
1921 offset
= reg_grp
->base_offset
+ reg
->offset
;
1922 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1924 switch (reg
->size
) {
1925 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1927 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1929 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1934 /* Serious issues when we cannot read the host values! */
1936 error_setg(errp
, "Cannot read host values");
1939 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1940 * contain the emulated view of the guest - therefore we flip the mask
1941 * to mask out the host values (which dev.config initially has) . */
1942 host_mask
= size_mask
& ~reg
->emu_mask
;
1944 if ((data
& host_mask
) != (val
& host_mask
)) {
1947 /* Mask out host (including past size). */
1948 new_val
= val
& host_mask
;
1949 /* Merge emulated ones (excluding the non-emulated ones). */
1950 new_val
|= data
& host_mask
;
1951 /* Leave intact host and emulated values past the size - even though
1952 * we do not care as we write per reg->size granularity, but for the
1953 * logging below lets have the proper value. */
1954 new_val
|= ((val
| data
)) & ~size_mask
;
1955 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1956 offset
, data
, val
, new_val
);
1961 if (val
& ~size_mask
) {
1962 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1963 " register size (%d)", offset
, val
, reg
->size
);
1967 /* This could be just pci_set_long as we don't modify the bits
1968 * past reg->size, but in case this routine is run in parallel or the
1969 * init value is larger, we do not want to over-write registers. */
1970 switch (reg
->size
) {
1971 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1973 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1975 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1979 /* set register value pointer to the data. */
1980 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
1983 /* list add register entry */
1984 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
1987 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
1992 QLIST_INIT(&s
->reg_grps
);
1994 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1995 uint32_t reg_grp_offset
= 0;
1996 XenPTRegGroup
*reg_grp_entry
= NULL
;
1998 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
1999 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2000 if (xen_pt_hide_dev_cap(&s
->real_device
,
2001 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2005 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2007 if (!reg_grp_offset
) {
2013 * By default we will trap up to 0x40 in the cfg space.
2014 * If an intel device is pass through we need to trap 0xfc,
2015 * therefore the size should be 0xff.
2017 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2018 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2021 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2022 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2023 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2025 reg_grp_entry
->base_offset
= reg_grp_offset
;
2026 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2027 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2028 /* get register group size */
2029 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2031 ®_grp_entry
->size
);
2033 error_setg(&err
, "Failed to initialize %d/%zu, type = 0x%x,"
2034 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2035 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2036 error_propagate(errp
, err
);
2037 xen_pt_config_delete(s
);
2042 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2043 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2045 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2047 /* initialize capability register */
2048 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2049 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, &err
);
2051 error_append_hint(&err
, "Failed to initialize %d/%zu"
2052 " reg 0x%x in grp_type = 0x%x (%d/%zu)",
2053 j
, ARRAY_SIZE(xen_pt_emu_reg_grps
[i
].emu_regs
),
2054 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2055 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2056 error_propagate(errp
, err
);
2057 xen_pt_config_delete(s
);
2066 /* delete all emulate register */
2067 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2069 struct XenPTRegGroup
*reg_group
, *next_grp
;
2070 struct XenPTReg
*reg
, *next_reg
;
2072 /* free MSI/MSI-X info table */
2074 xen_pt_msix_unmap(s
);
2078 /* free all register group entry */
2079 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2080 /* free all register entry */
2081 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2082 QLIST_REMOVE(reg
, entries
);
2086 QLIST_REMOVE(reg_group
, entries
);