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-legacy-backend.h"
21 #define XEN_PT_MERGE_VALUE(value, data, val_mask) \
22 (((value) & (val_mask)) | ((data) & ~(val_mask)))
24 #define XEN_PT_INVALID_REG 0xFFFFFFFF /* invalid register value */
28 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
29 uint32_t real_offset
, uint32_t *data
);
34 /* A return value of 1 means the capability should NOT be exposed to guest. */
35 static int xen_pt_hide_dev_cap(const XenHostPCIDevice
*d
, uint8_t grp_id
)
39 /* The PCI Express Capability Structure of the VF of Intel 82599 10GbE
40 * Controller looks trivial, e.g., the PCI Express Capabilities
41 * Register is 0. We should not try to expose it to guest.
43 * The datasheet is available at
44 * http://download.intel.com/design/network/datashts/82599_datasheet.pdf
46 * See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the
47 * PCI Express Capability Structure of the VF of Intel 82599 10GbE
48 * Controller looks trivial, e.g., the PCI Express Capabilities
49 * Register is 0, so the Capability Version is 0 and
50 * xen_pt_pcie_size_init() would fail.
52 if (d
->vendor_id
== PCI_VENDOR_ID_INTEL
&&
53 d
->device_id
== PCI_DEVICE_ID_INTEL_82599_SFP_VF
) {
61 /* find emulate register group entry */
62 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
)
64 XenPTRegGroup
*entry
= NULL
;
66 /* find register group entry */
67 QLIST_FOREACH(entry
, &s
->reg_grps
, entries
) {
69 if ((entry
->base_offset
<= address
)
70 && ((entry
->base_offset
+ entry
->size
) > address
)) {
75 /* group entry not found */
79 /* find emulate register entry */
80 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
)
82 XenPTReg
*reg_entry
= NULL
;
83 XenPTRegInfo
*reg
= NULL
;
84 uint32_t real_offset
= 0;
86 /* find register entry */
87 QLIST_FOREACH(reg_entry
, ®_grp
->reg_tbl_list
, entries
) {
89 real_offset
= reg_grp
->base_offset
+ reg
->offset
;
91 if ((real_offset
<= address
)
92 && ((real_offset
+ reg
->size
) > address
)) {
100 static uint32_t get_throughable_mask(const XenPCIPassthroughState
*s
,
101 XenPTRegInfo
*reg
, uint32_t valid_mask
)
103 uint32_t throughable_mask
= ~(reg
->emu_mask
| reg
->ro_mask
);
105 if (!s
->permissive
) {
106 throughable_mask
&= ~reg
->res_mask
;
109 return throughable_mask
& valid_mask
;
113 * general register functions
116 /* register initialization function */
118 static int xen_pt_common_reg_init(XenPCIPassthroughState
*s
,
119 XenPTRegInfo
*reg
, uint32_t real_offset
,
122 *data
= reg
->init_val
;
126 /* Read register functions */
128 static int xen_pt_byte_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
129 uint8_t *value
, uint8_t valid_mask
)
131 XenPTRegInfo
*reg
= cfg_entry
->reg
;
132 uint8_t valid_emu_mask
= 0;
133 uint8_t *data
= cfg_entry
->ptr
.byte
;
135 /* emulate byte register */
136 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
137 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
141 static int xen_pt_word_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
142 uint16_t *value
, uint16_t valid_mask
)
144 XenPTRegInfo
*reg
= cfg_entry
->reg
;
145 uint16_t valid_emu_mask
= 0;
146 uint16_t *data
= cfg_entry
->ptr
.half_word
;
148 /* emulate word register */
149 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
150 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
154 static int xen_pt_long_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
155 uint32_t *value
, uint32_t valid_mask
)
157 XenPTRegInfo
*reg
= cfg_entry
->reg
;
158 uint32_t valid_emu_mask
= 0;
159 uint32_t *data
= cfg_entry
->ptr
.word
;
161 /* emulate long register */
162 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
163 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
168 /* Write register functions */
170 static int xen_pt_byte_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
171 uint8_t *val
, uint8_t dev_value
,
174 XenPTRegInfo
*reg
= cfg_entry
->reg
;
175 uint8_t writable_mask
= 0;
176 uint8_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
177 uint8_t *data
= cfg_entry
->ptr
.byte
;
179 /* modify emulate register */
180 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
181 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
183 /* create value for writing to I/O device register */
184 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
189 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
190 uint16_t *val
, uint16_t dev_value
,
193 XenPTRegInfo
*reg
= cfg_entry
->reg
;
194 uint16_t writable_mask
= 0;
195 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
196 uint16_t *data
= cfg_entry
->ptr
.half_word
;
198 /* modify emulate register */
199 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
200 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
202 /* create value for writing to I/O device register */
203 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
208 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
209 uint32_t *val
, uint32_t dev_value
,
212 XenPTRegInfo
*reg
= cfg_entry
->reg
;
213 uint32_t writable_mask
= 0;
214 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
215 uint32_t *data
= cfg_entry
->ptr
.word
;
217 /* modify emulate register */
218 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
219 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
221 /* create value for writing to I/O device register */
222 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
229 /* XenPTRegInfo declaration
230 * - only for emulated register (either a part or whole bit).
231 * - for passthrough register that need special behavior (like interacting with
232 * other component), set emu_mask to all 0 and specify r/w func properly.
233 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
236 /********************
240 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
241 XenPTRegInfo
*reg
, uint32_t real_offset
,
244 *data
= s
->real_device
.vendor_id
;
247 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
248 XenPTRegInfo
*reg
, uint32_t real_offset
,
251 *data
= s
->real_device
.device_id
;
254 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
255 XenPTRegInfo
*reg
, uint32_t real_offset
,
258 XenPTRegGroup
*reg_grp_entry
= NULL
;
259 XenPTReg
*reg_entry
= NULL
;
260 uint32_t reg_field
= 0;
262 /* find Header register group */
263 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
265 /* find Capabilities Pointer register */
266 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
268 /* check Capabilities Pointer register */
269 if (*reg_entry
->ptr
.half_word
) {
270 reg_field
|= PCI_STATUS_CAP_LIST
;
272 reg_field
&= ~PCI_STATUS_CAP_LIST
;
275 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
276 " for Capabilities Pointer register."
277 " (%s)\n", __func__
);
281 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
282 " for Header. (%s)\n", __func__
);
289 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
290 XenPTRegInfo
*reg
, uint32_t real_offset
,
293 /* read PCI_HEADER_TYPE */
294 *data
= reg
->init_val
| 0x80;
298 /* initialize Interrupt Pin register */
299 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
300 XenPTRegInfo
*reg
, uint32_t real_offset
,
303 if (s
->real_device
.irq
) {
304 *data
= xen_pt_pci_read_intx(s
);
309 /* Command register */
310 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
311 uint16_t *val
, uint16_t dev_value
,
314 XenPTRegInfo
*reg
= cfg_entry
->reg
;
315 uint16_t writable_mask
= 0;
316 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
317 uint16_t *data
= cfg_entry
->ptr
.half_word
;
319 /* modify emulate register */
320 writable_mask
= ~reg
->ro_mask
& valid_mask
;
321 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
323 /* create value for writing to I/O device register */
324 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
325 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
327 if (s
->machine_irq
) {
328 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
332 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
338 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
339 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
340 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
341 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
343 static bool is_64bit_bar(PCIIORegion
*r
)
345 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
348 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
350 if (is_64bit_bar(r
)) {
352 size64
= (r
+ 1)->size
;
360 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
363 PCIDevice
*d
= PCI_DEVICE(s
);
364 XenPTRegion
*region
= NULL
;
367 /* check 64bit BAR */
368 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
369 int type
= s
->real_device
.io_regions
[index
- 1].type
;
371 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
372 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
373 region
= &s
->bases
[index
- 1];
374 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
375 return XEN_PT_BAR_FLAG_UPPER
;
380 /* check unused BAR */
381 r
= &d
->io_regions
[index
];
382 if (!xen_pt_get_bar_size(r
)) {
383 return XEN_PT_BAR_FLAG_UNUSED
;
387 if (index
== PCI_ROM_SLOT
) {
388 return XEN_PT_BAR_FLAG_MEM
;
391 /* check BAR I/O indicator */
392 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
393 return XEN_PT_BAR_FLAG_IO
;
395 return XEN_PT_BAR_FLAG_MEM
;
399 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
401 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
402 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
404 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
408 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
409 uint32_t real_offset
, uint32_t *data
)
411 uint32_t reg_field
= 0;
414 index
= xen_pt_bar_offset_to_index(reg
->offset
);
415 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
416 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
421 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
422 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
423 reg_field
= XEN_PT_INVALID_REG
;
429 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
430 uint32_t *value
, uint32_t valid_mask
)
432 XenPTRegInfo
*reg
= cfg_entry
->reg
;
433 uint32_t valid_emu_mask
= 0;
434 uint32_t bar_emu_mask
= 0;
438 index
= xen_pt_bar_offset_to_index(reg
->offset
);
439 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
440 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
444 /* use fixed-up value from kernel sysfs */
445 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
447 /* set emulate mask depend on BAR flag */
448 switch (s
->bases
[index
].bar_flag
) {
449 case XEN_PT_BAR_FLAG_MEM
:
450 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
452 case XEN_PT_BAR_FLAG_IO
:
453 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
455 case XEN_PT_BAR_FLAG_UPPER
:
456 bar_emu_mask
= XEN_PT_BAR_ALLF
;
463 valid_emu_mask
= bar_emu_mask
& valid_mask
;
464 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
468 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
469 uint32_t *val
, uint32_t dev_value
,
472 XenPTRegInfo
*reg
= cfg_entry
->reg
;
473 XenPTRegion
*base
= NULL
;
474 PCIDevice
*d
= PCI_DEVICE(s
);
475 const PCIIORegion
*r
;
476 uint32_t writable_mask
= 0;
477 uint32_t bar_emu_mask
= 0;
478 uint32_t bar_ro_mask
= 0;
481 uint32_t *data
= cfg_entry
->ptr
.word
;
483 index
= xen_pt_bar_offset_to_index(reg
->offset
);
484 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
485 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
489 r
= &d
->io_regions
[index
];
490 base
= &s
->bases
[index
];
491 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
493 /* set emulate mask and read-only mask values depend on the BAR flag */
494 switch (s
->bases
[index
].bar_flag
) {
495 case XEN_PT_BAR_FLAG_MEM
:
496 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
498 /* low 32 bits mask for 64 bit bars */
499 bar_ro_mask
= XEN_PT_BAR_ALLF
;
501 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
504 case XEN_PT_BAR_FLAG_IO
:
505 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
506 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
508 case XEN_PT_BAR_FLAG_UPPER
:
510 r_size
= d
->io_regions
[index
- 1].size
>> 32;
511 bar_emu_mask
= XEN_PT_BAR_ALLF
;
512 bar_ro_mask
= r_size
? r_size
- 1 : 0;
518 /* modify emulate register */
519 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
520 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
522 /* check whether we need to update the virtual region address or not */
523 switch (s
->bases
[index
].bar_flag
) {
524 case XEN_PT_BAR_FLAG_UPPER
:
525 case XEN_PT_BAR_FLAG_MEM
:
528 case XEN_PT_BAR_FLAG_IO
:
535 /* create value for writing to I/O device register */
536 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
541 /* write Exp ROM BAR */
542 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
543 XenPTReg
*cfg_entry
, uint32_t *val
,
544 uint32_t dev_value
, uint32_t valid_mask
)
546 XenPTRegInfo
*reg
= cfg_entry
->reg
;
547 XenPTRegion
*base
= NULL
;
548 PCIDevice
*d
= PCI_DEVICE(s
);
549 uint32_t writable_mask
= 0;
550 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
552 uint32_t bar_ro_mask
= 0;
553 uint32_t *data
= cfg_entry
->ptr
.word
;
555 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
556 base
= &s
->bases
[PCI_ROM_SLOT
];
557 /* align memory type resource size */
558 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
560 /* set emulate mask and read-only mask */
561 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
563 /* modify emulate register */
564 writable_mask
= ~bar_ro_mask
& valid_mask
;
565 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
567 /* create value for writing to I/O device register */
568 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
573 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
575 uint32_t *value
, uint32_t valid_mask
)
577 *value
= igd_read_opregion(s
);
581 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
582 XenPTReg
*cfg_entry
, uint32_t *value
,
583 uint32_t dev_value
, uint32_t valid_mask
)
585 igd_write_opregion(s
, *value
);
589 /* Header Type0 reg static information table */
590 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
593 .offset
= PCI_VENDOR_ID
,
598 .init
= xen_pt_vendor_reg_init
,
599 .u
.w
.read
= xen_pt_word_reg_read
,
600 .u
.w
.write
= xen_pt_word_reg_write
,
604 .offset
= PCI_DEVICE_ID
,
609 .init
= xen_pt_device_reg_init
,
610 .u
.w
.read
= xen_pt_word_reg_read
,
611 .u
.w
.write
= xen_pt_word_reg_write
,
615 .offset
= PCI_COMMAND
,
620 .init
= xen_pt_common_reg_init
,
621 .u
.w
.read
= xen_pt_word_reg_read
,
622 .u
.w
.write
= xen_pt_cmd_reg_write
,
624 /* Capabilities Pointer reg */
626 .offset
= PCI_CAPABILITY_LIST
,
631 .init
= xen_pt_ptr_reg_init
,
632 .u
.b
.read
= xen_pt_byte_reg_read
,
633 .u
.b
.write
= xen_pt_byte_reg_write
,
636 /* use emulated Cap Ptr value to initialize,
637 * so need to be declared after Cap Ptr reg
640 .offset
= PCI_STATUS
,
647 .init
= xen_pt_status_reg_init
,
648 .u
.w
.read
= xen_pt_word_reg_read
,
649 .u
.w
.write
= xen_pt_word_reg_write
,
651 /* Cache Line Size reg */
653 .offset
= PCI_CACHE_LINE_SIZE
,
658 .init
= xen_pt_common_reg_init
,
659 .u
.b
.read
= xen_pt_byte_reg_read
,
660 .u
.b
.write
= xen_pt_byte_reg_write
,
662 /* Latency Timer reg */
664 .offset
= PCI_LATENCY_TIMER
,
669 .init
= xen_pt_common_reg_init
,
670 .u
.b
.read
= xen_pt_byte_reg_read
,
671 .u
.b
.write
= xen_pt_byte_reg_write
,
673 /* Header Type reg */
675 .offset
= PCI_HEADER_TYPE
,
680 .init
= xen_pt_header_type_reg_init
,
681 .u
.b
.read
= xen_pt_byte_reg_read
,
682 .u
.b
.write
= xen_pt_byte_reg_write
,
684 /* Interrupt Line reg */
686 .offset
= PCI_INTERRUPT_LINE
,
691 .init
= xen_pt_common_reg_init
,
692 .u
.b
.read
= xen_pt_byte_reg_read
,
693 .u
.b
.write
= xen_pt_byte_reg_write
,
695 /* Interrupt Pin reg */
697 .offset
= PCI_INTERRUPT_PIN
,
702 .init
= xen_pt_irqpin_reg_init
,
703 .u
.b
.read
= xen_pt_byte_reg_read
,
704 .u
.b
.write
= xen_pt_byte_reg_write
,
707 /* mask of BAR need to be decided later, depends on IO/MEM type */
709 .offset
= PCI_BASE_ADDRESS_0
,
711 .init_val
= 0x00000000,
712 .init
= xen_pt_bar_reg_init
,
713 .u
.dw
.read
= xen_pt_bar_reg_read
,
714 .u
.dw
.write
= xen_pt_bar_reg_write
,
718 .offset
= PCI_BASE_ADDRESS_1
,
720 .init_val
= 0x00000000,
721 .init
= xen_pt_bar_reg_init
,
722 .u
.dw
.read
= xen_pt_bar_reg_read
,
723 .u
.dw
.write
= xen_pt_bar_reg_write
,
727 .offset
= PCI_BASE_ADDRESS_2
,
729 .init_val
= 0x00000000,
730 .init
= xen_pt_bar_reg_init
,
731 .u
.dw
.read
= xen_pt_bar_reg_read
,
732 .u
.dw
.write
= xen_pt_bar_reg_write
,
736 .offset
= PCI_BASE_ADDRESS_3
,
738 .init_val
= 0x00000000,
739 .init
= xen_pt_bar_reg_init
,
740 .u
.dw
.read
= xen_pt_bar_reg_read
,
741 .u
.dw
.write
= xen_pt_bar_reg_write
,
745 .offset
= PCI_BASE_ADDRESS_4
,
747 .init_val
= 0x00000000,
748 .init
= xen_pt_bar_reg_init
,
749 .u
.dw
.read
= xen_pt_bar_reg_read
,
750 .u
.dw
.write
= xen_pt_bar_reg_write
,
754 .offset
= PCI_BASE_ADDRESS_5
,
756 .init_val
= 0x00000000,
757 .init
= xen_pt_bar_reg_init
,
758 .u
.dw
.read
= xen_pt_bar_reg_read
,
759 .u
.dw
.write
= xen_pt_bar_reg_write
,
761 /* Expansion ROM BAR reg */
763 .offset
= PCI_ROM_ADDRESS
,
765 .init_val
= 0x00000000,
766 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
767 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
768 .init
= xen_pt_bar_reg_init
,
769 .u
.dw
.read
= xen_pt_long_reg_read
,
770 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
778 /*********************************
779 * Vital Product Data Capability
782 /* Vital Product Data Capability Structure reg static information table */
783 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
785 .offset
= PCI_CAP_LIST_NEXT
,
790 .init
= xen_pt_ptr_reg_init
,
791 .u
.b
.read
= xen_pt_byte_reg_read
,
792 .u
.b
.write
= xen_pt_byte_reg_write
,
795 .offset
= PCI_VPD_ADDR
,
799 .init
= xen_pt_common_reg_init
,
800 .u
.w
.read
= xen_pt_word_reg_read
,
801 .u
.w
.write
= xen_pt_word_reg_write
,
809 /**************************************
810 * Vendor Specific Capability
813 /* Vendor Specific Capability Structure reg static information table */
814 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
816 .offset
= PCI_CAP_LIST_NEXT
,
821 .init
= xen_pt_ptr_reg_init
,
822 .u
.b
.read
= xen_pt_byte_reg_read
,
823 .u
.b
.write
= xen_pt_byte_reg_write
,
831 /*****************************
832 * PCI Express Capability
835 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
839 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
842 return flag
& PCI_EXP_FLAGS_VERS
;
845 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
849 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
852 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
855 /* initialize Link Control register */
856 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
857 XenPTRegInfo
*reg
, uint32_t real_offset
,
860 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
861 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
863 /* no need to initialize in case of Root Complex Integrated Endpoint
866 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
867 *data
= XEN_PT_INVALID_REG
;
870 *data
= reg
->init_val
;
873 /* initialize Device Control 2 register */
874 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
875 XenPTRegInfo
*reg
, uint32_t real_offset
,
878 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
880 /* no need to initialize in case of cap_ver 1.x */
882 *data
= XEN_PT_INVALID_REG
;
885 *data
= reg
->init_val
;
888 /* initialize Link Control 2 register */
889 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
890 XenPTRegInfo
*reg
, uint32_t real_offset
,
893 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
894 uint32_t reg_field
= 0;
896 /* no need to initialize in case of cap_ver 1.x */
898 reg_field
= XEN_PT_INVALID_REG
;
900 /* set Supported Link Speed */
903 rc
= xen_host_pci_get_byte(&s
->real_device
,
904 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
909 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
916 /* PCI Express Capability Structure reg static information table */
917 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
918 /* Next Pointer reg */
920 .offset
= PCI_CAP_LIST_NEXT
,
925 .init
= xen_pt_ptr_reg_init
,
926 .u
.b
.read
= xen_pt_byte_reg_read
,
927 .u
.b
.write
= xen_pt_byte_reg_write
,
929 /* Device Capabilities reg */
931 .offset
= PCI_EXP_DEVCAP
,
933 .init_val
= 0x00000000,
934 .ro_mask
= 0xFFFFFFFF,
935 .emu_mask
= 0x10000000,
936 .init
= xen_pt_common_reg_init
,
937 .u
.dw
.read
= xen_pt_long_reg_read
,
938 .u
.dw
.write
= xen_pt_long_reg_write
,
940 /* Device Control reg */
942 .offset
= PCI_EXP_DEVCTL
,
947 .init
= xen_pt_common_reg_init
,
948 .u
.w
.read
= xen_pt_word_reg_read
,
949 .u
.w
.write
= xen_pt_word_reg_write
,
951 /* Device Status reg */
953 .offset
= PCI_EXP_DEVSTA
,
958 .init
= xen_pt_common_reg_init
,
959 .u
.w
.read
= xen_pt_word_reg_read
,
960 .u
.w
.write
= xen_pt_word_reg_write
,
962 /* Link Control reg */
964 .offset
= PCI_EXP_LNKCTL
,
969 .init
= xen_pt_linkctrl_reg_init
,
970 .u
.w
.read
= xen_pt_word_reg_read
,
971 .u
.w
.write
= xen_pt_word_reg_write
,
973 /* Link Status reg */
975 .offset
= PCI_EXP_LNKSTA
,
979 .init
= xen_pt_common_reg_init
,
980 .u
.w
.read
= xen_pt_word_reg_read
,
981 .u
.w
.write
= xen_pt_word_reg_write
,
983 /* Device Control 2 reg */
990 .init
= xen_pt_devctrl2_reg_init
,
991 .u
.w
.read
= xen_pt_word_reg_read
,
992 .u
.w
.write
= xen_pt_word_reg_write
,
994 /* Link Control 2 reg */
1001 .init
= xen_pt_linkctrl2_reg_init
,
1002 .u
.w
.read
= xen_pt_word_reg_read
,
1003 .u
.w
.write
= xen_pt_word_reg_write
,
1011 /*********************************
1012 * Power Management Capability
1015 /* Power Management Capability reg static information table */
1016 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1017 /* Next Pointer reg */
1019 .offset
= PCI_CAP_LIST_NEXT
,
1024 .init
= xen_pt_ptr_reg_init
,
1025 .u
.b
.read
= xen_pt_byte_reg_read
,
1026 .u
.b
.write
= xen_pt_byte_reg_write
,
1028 /* Power Management Capabilities reg */
1030 .offset
= PCI_CAP_FLAGS
,
1035 .init
= xen_pt_common_reg_init
,
1036 .u
.w
.read
= xen_pt_word_reg_read
,
1037 .u
.w
.write
= xen_pt_word_reg_write
,
1039 /* PCI Power Management Control/Status reg */
1041 .offset
= PCI_PM_CTRL
,
1046 .rw1c_mask
= 0x8000,
1048 .init
= xen_pt_common_reg_init
,
1049 .u
.w
.read
= xen_pt_word_reg_read
,
1050 .u
.w
.write
= xen_pt_word_reg_write
,
1058 /********************************
1063 #define xen_pt_msi_check_type(offset, flags, what) \
1064 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1065 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1067 /* Message Control register */
1068 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1069 XenPTRegInfo
*reg
, uint32_t real_offset
,
1072 XenPTMSI
*msi
= s
->msi
;
1076 /* use I/O device register's value as initial value */
1077 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1081 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1082 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1083 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1084 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1086 msi
->flags
|= reg_field
;
1087 msi
->ctrl_offset
= real_offset
;
1088 msi
->initialized
= false;
1089 msi
->mapped
= false;
1091 *data
= reg
->init_val
;
1094 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1095 XenPTReg
*cfg_entry
, uint16_t *val
,
1096 uint16_t dev_value
, uint16_t valid_mask
)
1098 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1099 XenPTMSI
*msi
= s
->msi
;
1100 uint16_t writable_mask
= 0;
1101 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1102 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1104 /* Currently no support for multi-vector */
1105 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1106 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1109 /* modify emulate register */
1110 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1111 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1112 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1114 /* create value for writing to I/O device register */
1115 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1118 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1119 /* setup MSI pirq for the first time */
1120 if (!msi
->initialized
) {
1121 /* Init physical one */
1122 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1123 if (xen_pt_msi_setup(s
)) {
1124 /* We do not broadcast the error to the framework code, so
1125 * that MSI errors are contained in MSI emulation code and
1126 * QEMU can go on running.
1127 * Guest MSI would be actually not working.
1129 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1130 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1133 if (xen_pt_msi_update(s
)) {
1134 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1135 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1138 msi
->initialized
= true;
1141 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1142 } else if (msi
->mapped
) {
1143 xen_pt_msi_disable(s
);
1149 /* initialize Message Upper Address register */
1150 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1151 XenPTRegInfo
*reg
, uint32_t real_offset
,
1154 /* no need to initialize in case of 32 bit type */
1155 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1156 *data
= XEN_PT_INVALID_REG
;
1158 *data
= reg
->init_val
;
1163 /* this function will be called twice (for 32 bit and 64 bit type) */
1164 /* initialize Message Data register */
1165 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1166 XenPTRegInfo
*reg
, uint32_t real_offset
,
1169 uint32_t flags
= s
->msi
->flags
;
1170 uint32_t offset
= reg
->offset
;
1172 /* check the offset whether matches the type or not */
1173 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1174 *data
= reg
->init_val
;
1176 *data
= XEN_PT_INVALID_REG
;
1181 /* this function will be called twice (for 32 bit and 64 bit type) */
1182 /* initialize Mask register */
1183 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1184 XenPTRegInfo
*reg
, uint32_t real_offset
,
1187 uint32_t flags
= s
->msi
->flags
;
1189 /* check the offset whether matches the type or not */
1190 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1191 *data
= XEN_PT_INVALID_REG
;
1192 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1193 *data
= reg
->init_val
;
1195 *data
= XEN_PT_INVALID_REG
;
1200 /* this function will be called twice (for 32 bit and 64 bit type) */
1201 /* initialize Pending register */
1202 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1203 XenPTRegInfo
*reg
, uint32_t real_offset
,
1206 uint32_t flags
= s
->msi
->flags
;
1208 /* check the offset whether matches the type or not */
1209 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1210 *data
= XEN_PT_INVALID_REG
;
1211 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1212 *data
= reg
->init_val
;
1214 *data
= XEN_PT_INVALID_REG
;
1219 /* write Message Address register */
1220 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1221 XenPTReg
*cfg_entry
, uint32_t *val
,
1222 uint32_t dev_value
, uint32_t valid_mask
)
1224 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1225 uint32_t writable_mask
= 0;
1226 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1227 uint32_t *data
= cfg_entry
->ptr
.word
;
1229 /* modify emulate register */
1230 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1231 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1232 s
->msi
->addr_lo
= *data
;
1234 /* create value for writing to I/O device register */
1235 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1238 if (*data
!= old_addr
) {
1239 if (s
->msi
->mapped
) {
1240 xen_pt_msi_update(s
);
1246 /* write Message Upper Address register */
1247 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1248 XenPTReg
*cfg_entry
, uint32_t *val
,
1249 uint32_t dev_value
, uint32_t valid_mask
)
1251 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1252 uint32_t writable_mask
= 0;
1253 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1254 uint32_t *data
= cfg_entry
->ptr
.word
;
1256 /* check whether the type is 64 bit or not */
1257 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1259 "Can't write to the upper address without 64 bit support\n");
1263 /* modify emulate register */
1264 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1265 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1266 /* update the msi_info too */
1267 s
->msi
->addr_hi
= *data
;
1269 /* create value for writing to I/O device register */
1270 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1273 if (*data
!= old_addr
) {
1274 if (s
->msi
->mapped
) {
1275 xen_pt_msi_update(s
);
1283 /* this function will be called twice (for 32 bit and 64 bit type) */
1284 /* write Message Data register */
1285 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1286 XenPTReg
*cfg_entry
, uint16_t *val
,
1287 uint16_t dev_value
, uint16_t valid_mask
)
1289 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1290 XenPTMSI
*msi
= s
->msi
;
1291 uint16_t writable_mask
= 0;
1292 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1293 uint32_t offset
= reg
->offset
;
1294 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1296 /* check the offset whether matches the type or not */
1297 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1298 /* exit I/O emulator */
1299 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1303 /* modify emulate register */
1304 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1305 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1306 /* update the msi_info too */
1309 /* create value for writing to I/O device register */
1310 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1313 if (*data
!= old_data
) {
1315 xen_pt_msi_update(s
);
1322 static int xen_pt_mask_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
1323 uint32_t *val
, uint32_t dev_value
,
1324 uint32_t valid_mask
)
1328 rc
= xen_pt_long_reg_write(s
, cfg_entry
, val
, dev_value
, valid_mask
);
1333 s
->msi
->mask
= *val
;
1338 /* MSI Capability Structure reg static information table */
1339 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1340 /* Next Pointer reg */
1342 .offset
= PCI_CAP_LIST_NEXT
,
1347 .init
= xen_pt_ptr_reg_init
,
1348 .u
.b
.read
= xen_pt_byte_reg_read
,
1349 .u
.b
.write
= xen_pt_byte_reg_write
,
1351 /* Message Control reg */
1353 .offset
= PCI_MSI_FLAGS
,
1359 .init
= xen_pt_msgctrl_reg_init
,
1360 .u
.w
.read
= xen_pt_word_reg_read
,
1361 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1363 /* Message Address reg */
1365 .offset
= PCI_MSI_ADDRESS_LO
,
1367 .init_val
= 0x00000000,
1368 .ro_mask
= 0x00000003,
1369 .emu_mask
= 0xFFFFFFFF,
1370 .init
= xen_pt_common_reg_init
,
1371 .u
.dw
.read
= xen_pt_long_reg_read
,
1372 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1374 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1376 .offset
= PCI_MSI_ADDRESS_HI
,
1378 .init_val
= 0x00000000,
1379 .ro_mask
= 0x00000000,
1380 .emu_mask
= 0xFFFFFFFF,
1381 .init
= xen_pt_msgaddr64_reg_init
,
1382 .u
.dw
.read
= xen_pt_long_reg_read
,
1383 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1385 /* Message Data reg (16 bits of data for 32-bit devices) */
1387 .offset
= PCI_MSI_DATA_32
,
1392 .init
= xen_pt_msgdata_reg_init
,
1393 .u
.w
.read
= xen_pt_word_reg_read
,
1394 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1396 /* Message Data reg (16 bits of data for 64-bit devices) */
1398 .offset
= PCI_MSI_DATA_64
,
1403 .init
= xen_pt_msgdata_reg_init
,
1404 .u
.w
.read
= xen_pt_word_reg_read
,
1405 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1407 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1409 .offset
= PCI_MSI_MASK_32
,
1411 .init_val
= 0x00000000,
1412 .ro_mask
= 0xFFFFFFFF,
1413 .emu_mask
= 0xFFFFFFFF,
1414 .init
= xen_pt_mask_reg_init
,
1415 .u
.dw
.read
= xen_pt_long_reg_read
,
1416 .u
.dw
.write
= xen_pt_mask_reg_write
,
1418 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1420 .offset
= PCI_MSI_MASK_64
,
1422 .init_val
= 0x00000000,
1423 .ro_mask
= 0xFFFFFFFF,
1424 .emu_mask
= 0xFFFFFFFF,
1425 .init
= xen_pt_mask_reg_init
,
1426 .u
.dw
.read
= xen_pt_long_reg_read
,
1427 .u
.dw
.write
= xen_pt_mask_reg_write
,
1429 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1431 .offset
= PCI_MSI_MASK_32
+ 4,
1433 .init_val
= 0x00000000,
1434 .ro_mask
= 0xFFFFFFFF,
1435 .emu_mask
= 0x00000000,
1436 .init
= xen_pt_pending_reg_init
,
1437 .u
.dw
.read
= xen_pt_long_reg_read
,
1438 .u
.dw
.write
= xen_pt_long_reg_write
,
1440 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1442 .offset
= PCI_MSI_MASK_64
+ 4,
1444 .init_val
= 0x00000000,
1445 .ro_mask
= 0xFFFFFFFF,
1446 .emu_mask
= 0x00000000,
1447 .init
= xen_pt_pending_reg_init
,
1448 .u
.dw
.read
= xen_pt_long_reg_read
,
1449 .u
.dw
.write
= xen_pt_long_reg_write
,
1457 /**************************************
1461 /* Message Control register for MSI-X */
1462 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1463 XenPTRegInfo
*reg
, uint32_t real_offset
,
1469 /* use I/O device register's value as initial value */
1470 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1474 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1475 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1476 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1477 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1480 s
->msix
->ctrl_offset
= real_offset
;
1482 *data
= reg
->init_val
;
1485 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1486 XenPTReg
*cfg_entry
, uint16_t *val
,
1487 uint16_t dev_value
, uint16_t valid_mask
)
1489 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1490 uint16_t writable_mask
= 0;
1491 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1492 int debug_msix_enabled_old
;
1493 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1495 /* modify emulate register */
1496 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1497 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1499 /* create value for writing to I/O device register */
1500 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1503 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1504 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1505 xen_pt_msix_update(s
);
1506 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1507 xen_pt_msix_disable(s
);
1510 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1512 debug_msix_enabled_old
= s
->msix
->enabled
;
1513 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1514 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1515 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1516 s
->msix
->enabled
? "enable" : "disable");
1522 /* MSI-X Capability Structure reg static information table */
1523 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1524 /* Next Pointer reg */
1526 .offset
= PCI_CAP_LIST_NEXT
,
1531 .init
= xen_pt_ptr_reg_init
,
1532 .u
.b
.read
= xen_pt_byte_reg_read
,
1533 .u
.b
.write
= xen_pt_byte_reg_write
,
1535 /* Message Control reg */
1537 .offset
= PCI_MSI_FLAGS
,
1543 .init
= xen_pt_msixctrl_reg_init
,
1544 .u
.w
.read
= xen_pt_word_reg_read
,
1545 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1552 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1553 /* Intel IGFX OpRegion reg */
1558 .emu_mask
= 0xFFFFFFFF,
1559 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1560 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1567 /****************************
1571 /* capability structure register group size functions */
1573 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1574 const XenPTRegGroupInfo
*grp_reg
,
1575 uint32_t base_offset
, uint8_t *size
)
1577 *size
= grp_reg
->grp_size
;
1580 /* get Vendor Specific Capability Structure register group size */
1581 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1582 const XenPTRegGroupInfo
*grp_reg
,
1583 uint32_t base_offset
, uint8_t *size
)
1585 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1587 /* get PCI Express Capability Structure register group size */
1588 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1589 const XenPTRegGroupInfo
*grp_reg
,
1590 uint32_t base_offset
, uint8_t *size
)
1592 PCIDevice
*d
= PCI_DEVICE(s
);
1593 uint8_t version
= get_capability_version(s
, base_offset
);
1594 uint8_t type
= get_device_type(s
, base_offset
);
1595 uint8_t pcie_size
= 0;
1598 /* calculate size depend on capability version and device/port type */
1599 /* in case of PCI Express Base Specification Rev 1.x */
1601 /* The PCI Express Capabilities, Device Capabilities, and Device
1602 * Status/Control registers are required for all PCI Express devices.
1603 * The Link Capabilities and Link Status/Control are required for all
1604 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1605 * are not required to implement registers other than those listed
1606 * above and terminate the capability structure.
1609 case PCI_EXP_TYPE_ENDPOINT
:
1610 case PCI_EXP_TYPE_LEG_END
:
1613 case PCI_EXP_TYPE_RC_END
:
1617 /* only EndPoint passthrough is supported */
1618 case PCI_EXP_TYPE_ROOT_PORT
:
1619 case PCI_EXP_TYPE_UPSTREAM
:
1620 case PCI_EXP_TYPE_DOWNSTREAM
:
1621 case PCI_EXP_TYPE_PCI_BRIDGE
:
1622 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1623 case PCI_EXP_TYPE_RC_EC
:
1625 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1629 /* in case of PCI Express Base Specification Rev 2.0 */
1630 else if (version
== 2) {
1632 case PCI_EXP_TYPE_ENDPOINT
:
1633 case PCI_EXP_TYPE_LEG_END
:
1634 case PCI_EXP_TYPE_RC_END
:
1635 /* For Functions that do not implement the registers,
1636 * these spaces must be hardwired to 0b.
1640 /* only EndPoint passthrough is supported */
1641 case PCI_EXP_TYPE_ROOT_PORT
:
1642 case PCI_EXP_TYPE_UPSTREAM
:
1643 case PCI_EXP_TYPE_DOWNSTREAM
:
1644 case PCI_EXP_TYPE_PCI_BRIDGE
:
1645 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1646 case PCI_EXP_TYPE_RC_EC
:
1648 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1652 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1659 /* get MSI Capability Structure register group size */
1660 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1661 const XenPTRegGroupInfo
*grp_reg
,
1662 uint32_t base_offset
, uint8_t *size
)
1664 uint16_t msg_ctrl
= 0;
1665 uint8_t msi_size
= 0xa;
1668 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1673 /* check if 64-bit address is capable of per-vector masking */
1674 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1677 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1681 s
->msi
= g_new0(XenPTMSI
, 1);
1682 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1687 /* get MSI-X Capability Structure register group size */
1688 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1689 const XenPTRegGroupInfo
*grp_reg
,
1690 uint32_t base_offset
, uint8_t *size
)
1694 rc
= xen_pt_msix_init(s
, base_offset
);
1697 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1701 *size
= grp_reg
->grp_size
;
1706 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1707 /* Header Type0 reg group */
1710 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1712 .size_init
= xen_pt_reg_grp_size_init
,
1713 .emu_regs
= xen_pt_emu_reg_header0
,
1715 /* PCI PowerManagement Capability reg group */
1717 .grp_id
= PCI_CAP_ID_PM
,
1718 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1719 .grp_size
= PCI_PM_SIZEOF
,
1720 .size_init
= xen_pt_reg_grp_size_init
,
1721 .emu_regs
= xen_pt_emu_reg_pm
,
1723 /* AGP Capability Structure reg group */
1725 .grp_id
= PCI_CAP_ID_AGP
,
1726 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1728 .size_init
= xen_pt_reg_grp_size_init
,
1730 /* Vital Product Data Capability Structure reg group */
1732 .grp_id
= PCI_CAP_ID_VPD
,
1733 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1735 .size_init
= xen_pt_reg_grp_size_init
,
1736 .emu_regs
= xen_pt_emu_reg_vpd
,
1738 /* Slot Identification reg group */
1740 .grp_id
= PCI_CAP_ID_SLOTID
,
1741 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1743 .size_init
= xen_pt_reg_grp_size_init
,
1745 /* MSI Capability Structure reg group */
1747 .grp_id
= PCI_CAP_ID_MSI
,
1748 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1750 .size_init
= xen_pt_msi_size_init
,
1751 .emu_regs
= xen_pt_emu_reg_msi
,
1753 /* PCI-X Capabilities List Item reg group */
1755 .grp_id
= PCI_CAP_ID_PCIX
,
1756 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1758 .size_init
= xen_pt_reg_grp_size_init
,
1760 /* Vendor Specific Capability Structure reg group */
1762 .grp_id
= PCI_CAP_ID_VNDR
,
1763 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1765 .size_init
= xen_pt_vendor_size_init
,
1766 .emu_regs
= xen_pt_emu_reg_vendor
,
1768 /* SHPC Capability List Item reg group */
1770 .grp_id
= PCI_CAP_ID_SHPC
,
1771 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1773 .size_init
= xen_pt_reg_grp_size_init
,
1775 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1777 .grp_id
= PCI_CAP_ID_SSVID
,
1778 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1780 .size_init
= xen_pt_reg_grp_size_init
,
1782 /* AGP 8x Capability Structure reg group */
1784 .grp_id
= PCI_CAP_ID_AGP3
,
1785 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1787 .size_init
= xen_pt_reg_grp_size_init
,
1789 /* PCI Express Capability Structure reg group */
1791 .grp_id
= PCI_CAP_ID_EXP
,
1792 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1794 .size_init
= xen_pt_pcie_size_init
,
1795 .emu_regs
= xen_pt_emu_reg_pcie
,
1797 /* MSI-X Capability Structure reg group */
1799 .grp_id
= PCI_CAP_ID_MSIX
,
1800 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1802 .size_init
= xen_pt_msix_size_init
,
1803 .emu_regs
= xen_pt_emu_reg_msix
,
1805 /* Intel IGD Opregion group */
1807 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1808 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1810 .size_init
= xen_pt_reg_grp_size_init
,
1811 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1818 /* initialize Capabilities Pointer or Next Pointer register */
1819 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1820 XenPTRegInfo
*reg
, uint32_t real_offset
,
1827 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1831 /* find capability offset */
1833 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1834 if (xen_pt_hide_dev_cap(&s
->real_device
,
1835 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1839 rc
= xen_host_pci_get_byte(&s
->real_device
,
1840 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1842 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1843 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1846 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1847 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1850 /* ignore the 0 hardwired capability, find next one */
1855 /* next capability */
1856 rc
= xen_host_pci_get_byte(&s
->real_device
,
1857 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1873 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1876 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1877 uint8_t pos
= PCI_CAPABILITY_LIST
;
1880 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1883 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1888 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1891 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1896 if (xen_host_pci_get_byte(&s
->real_device
,
1897 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1908 pos
+= PCI_CAP_LIST_NEXT
;
1913 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1914 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1917 XenPTReg
*reg_entry
;
1921 reg_entry
= g_new0(XenPTReg
, 1);
1922 reg_entry
->reg
= reg
;
1925 uint32_t host_mask
, size_mask
;
1926 unsigned int offset
;
1929 /* initialize emulate register */
1930 rc
= reg
->init(s
, reg_entry
->reg
,
1931 reg_grp
->base_offset
+ reg
->offset
, &data
);
1934 error_setg(errp
, "Init emulate register fail");
1937 if (data
== XEN_PT_INVALID_REG
) {
1938 /* free unused BAR register entry */
1942 /* Sync up the data to dev.config */
1943 offset
= reg_grp
->base_offset
+ reg
->offset
;
1944 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1946 switch (reg
->size
) {
1947 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1949 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1951 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1956 /* Serious issues when we cannot read the host values! */
1958 error_setg(errp
, "Cannot read host values");
1961 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1962 * contain the emulated view of the guest - therefore we flip the mask
1963 * to mask out the host values (which dev.config initially has) . */
1964 host_mask
= size_mask
& ~reg
->emu_mask
;
1966 if ((data
& host_mask
) != (val
& host_mask
)) {
1969 /* Mask out host (including past size). */
1970 new_val
= val
& host_mask
;
1971 /* Merge emulated ones (excluding the non-emulated ones). */
1972 new_val
|= data
& host_mask
;
1973 /* Leave intact host and emulated values past the size - even though
1974 * we do not care as we write per reg->size granularity, but for the
1975 * logging below lets have the proper value. */
1976 new_val
|= ((val
| data
)) & ~size_mask
;
1977 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1978 offset
, data
, val
, new_val
);
1983 if (val
& ~size_mask
) {
1984 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1985 " register size (%d)", offset
, val
, reg
->size
);
1989 /* This could be just pci_set_long as we don't modify the bits
1990 * past reg->size, but in case this routine is run in parallel or the
1991 * init value is larger, we do not want to over-write registers. */
1992 switch (reg
->size
) {
1993 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1995 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1997 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
2001 /* set register value pointer to the data. */
2002 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
2005 /* list add register entry */
2006 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
2009 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
2014 QLIST_INIT(&s
->reg_grps
);
2016 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
2017 uint32_t reg_grp_offset
= 0;
2018 XenPTRegGroup
*reg_grp_entry
= NULL
;
2020 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2021 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2022 if (xen_pt_hide_dev_cap(&s
->real_device
,
2023 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2027 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2029 if (!reg_grp_offset
) {
2035 * By default we will trap up to 0x40 in the cfg space.
2036 * If an intel device is pass through we need to trap 0xfc,
2037 * therefore the size should be 0xff.
2039 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2040 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2043 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2044 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2045 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2047 reg_grp_entry
->base_offset
= reg_grp_offset
;
2048 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2049 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2050 /* get register group size */
2051 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2053 ®_grp_entry
->size
);
2055 error_setg(&err
, "Failed to initialize %d/%zu, type = 0x%x,"
2056 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2057 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2058 error_propagate(errp
, err
);
2059 xen_pt_config_delete(s
);
2064 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2065 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2067 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2069 /* initialize capability register */
2070 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2071 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, &err
);
2073 error_append_hint(&err
, "Failed to init register %d"
2074 " offsets 0x%x in grp_type = 0x%x (%d/%zu)", j
,
2075 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2076 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2077 error_propagate(errp
, err
);
2078 xen_pt_config_delete(s
);
2087 /* delete all emulate register */
2088 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2090 struct XenPTRegGroup
*reg_group
, *next_grp
;
2091 struct XenPTReg
*reg
, *next_reg
;
2093 /* free MSI/MSI-X info table */
2095 xen_pt_msix_unmap(s
);
2099 /* free all register group entry */
2100 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2101 /* free all register entry */
2102 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2103 QLIST_REMOVE(reg
, entries
);
2107 QLIST_REMOVE(reg_group
, entries
);