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_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 *data
= xen_pt_pci_read_intx(s
);
307 /* Command register */
308 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
309 uint16_t *val
, uint16_t dev_value
,
312 XenPTRegInfo
*reg
= cfg_entry
->reg
;
313 uint16_t writable_mask
= 0;
314 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
315 uint16_t *data
= cfg_entry
->ptr
.half_word
;
317 /* modify emulate register */
318 writable_mask
= ~reg
->ro_mask
& valid_mask
;
319 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
321 /* create value for writing to I/O device register */
322 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
323 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
325 if (s
->machine_irq
) {
326 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
330 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
336 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
337 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
338 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
339 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
341 static bool is_64bit_bar(PCIIORegion
*r
)
343 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
346 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
348 if (is_64bit_bar(r
)) {
350 size64
= (r
+ 1)->size
;
358 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
361 PCIDevice
*d
= &s
->dev
;
362 XenPTRegion
*region
= NULL
;
365 /* check 64bit BAR */
366 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
367 int type
= s
->real_device
.io_regions
[index
- 1].type
;
369 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
370 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
371 region
= &s
->bases
[index
- 1];
372 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
373 return XEN_PT_BAR_FLAG_UPPER
;
378 /* check unused BAR */
379 r
= &d
->io_regions
[index
];
380 if (!xen_pt_get_bar_size(r
)) {
381 return XEN_PT_BAR_FLAG_UNUSED
;
385 if (index
== PCI_ROM_SLOT
) {
386 return XEN_PT_BAR_FLAG_MEM
;
389 /* check BAR I/O indicator */
390 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
391 return XEN_PT_BAR_FLAG_IO
;
393 return XEN_PT_BAR_FLAG_MEM
;
397 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
399 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
400 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
402 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
406 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
407 uint32_t real_offset
, uint32_t *data
)
409 uint32_t reg_field
= 0;
412 index
= xen_pt_bar_offset_to_index(reg
->offset
);
413 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
414 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
419 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
420 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
421 reg_field
= XEN_PT_INVALID_REG
;
427 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
428 uint32_t *value
, uint32_t valid_mask
)
430 XenPTRegInfo
*reg
= cfg_entry
->reg
;
431 uint32_t valid_emu_mask
= 0;
432 uint32_t bar_emu_mask
= 0;
436 index
= xen_pt_bar_offset_to_index(reg
->offset
);
437 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
438 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
442 /* use fixed-up value from kernel sysfs */
443 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
445 /* set emulate mask depend on BAR flag */
446 switch (s
->bases
[index
].bar_flag
) {
447 case XEN_PT_BAR_FLAG_MEM
:
448 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
450 case XEN_PT_BAR_FLAG_IO
:
451 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
453 case XEN_PT_BAR_FLAG_UPPER
:
454 bar_emu_mask
= XEN_PT_BAR_ALLF
;
461 valid_emu_mask
= bar_emu_mask
& valid_mask
;
462 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
466 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
467 uint32_t *val
, uint32_t dev_value
,
470 XenPTRegInfo
*reg
= cfg_entry
->reg
;
471 XenPTRegion
*base
= NULL
;
472 PCIDevice
*d
= &s
->dev
;
473 const PCIIORegion
*r
;
474 uint32_t writable_mask
= 0;
475 uint32_t bar_emu_mask
= 0;
476 uint32_t bar_ro_mask
= 0;
479 uint32_t *data
= cfg_entry
->ptr
.word
;
481 index
= xen_pt_bar_offset_to_index(reg
->offset
);
482 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
483 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
487 r
= &d
->io_regions
[index
];
488 base
= &s
->bases
[index
];
489 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
491 /* set emulate mask and read-only mask values depend on the BAR flag */
492 switch (s
->bases
[index
].bar_flag
) {
493 case XEN_PT_BAR_FLAG_MEM
:
494 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
496 /* low 32 bits mask for 64 bit bars */
497 bar_ro_mask
= XEN_PT_BAR_ALLF
;
499 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
502 case XEN_PT_BAR_FLAG_IO
:
503 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
504 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
506 case XEN_PT_BAR_FLAG_UPPER
:
507 bar_emu_mask
= XEN_PT_BAR_ALLF
;
508 bar_ro_mask
= r_size
? r_size
- 1 : 0;
514 /* modify emulate register */
515 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
516 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
518 /* check whether we need to update the virtual region address or not */
519 switch (s
->bases
[index
].bar_flag
) {
520 case XEN_PT_BAR_FLAG_UPPER
:
521 case XEN_PT_BAR_FLAG_MEM
:
524 case XEN_PT_BAR_FLAG_IO
:
531 /* create value for writing to I/O device register */
532 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
537 /* write Exp ROM BAR */
538 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
539 XenPTReg
*cfg_entry
, uint32_t *val
,
540 uint32_t dev_value
, uint32_t valid_mask
)
542 XenPTRegInfo
*reg
= cfg_entry
->reg
;
543 XenPTRegion
*base
= NULL
;
544 PCIDevice
*d
= (PCIDevice
*)&s
->dev
;
545 uint32_t writable_mask
= 0;
546 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
548 uint32_t bar_ro_mask
= 0;
549 uint32_t *data
= cfg_entry
->ptr
.word
;
551 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
552 base
= &s
->bases
[PCI_ROM_SLOT
];
553 /* align memory type resource size */
554 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
556 /* set emulate mask and read-only mask */
557 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
559 /* modify emulate register */
560 writable_mask
= ~bar_ro_mask
& valid_mask
;
561 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
563 /* create value for writing to I/O device register */
564 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
569 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
571 uint32_t *value
, uint32_t valid_mask
)
573 *value
= igd_read_opregion(s
);
577 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
578 XenPTReg
*cfg_entry
, uint32_t *value
,
579 uint32_t dev_value
, uint32_t valid_mask
)
581 igd_write_opregion(s
, *value
);
585 /* Header Type0 reg static information table */
586 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
589 .offset
= PCI_VENDOR_ID
,
594 .init
= xen_pt_vendor_reg_init
,
595 .u
.w
.read
= xen_pt_word_reg_read
,
596 .u
.w
.write
= xen_pt_word_reg_write
,
600 .offset
= PCI_DEVICE_ID
,
605 .init
= xen_pt_device_reg_init
,
606 .u
.w
.read
= xen_pt_word_reg_read
,
607 .u
.w
.write
= xen_pt_word_reg_write
,
611 .offset
= PCI_COMMAND
,
616 .init
= xen_pt_common_reg_init
,
617 .u
.w
.read
= xen_pt_word_reg_read
,
618 .u
.w
.write
= xen_pt_cmd_reg_write
,
620 /* Capabilities Pointer reg */
622 .offset
= PCI_CAPABILITY_LIST
,
627 .init
= xen_pt_ptr_reg_init
,
628 .u
.b
.read
= xen_pt_byte_reg_read
,
629 .u
.b
.write
= xen_pt_byte_reg_write
,
632 /* use emulated Cap Ptr value to initialize,
633 * so need to be declared after Cap Ptr reg
636 .offset
= PCI_STATUS
,
643 .init
= xen_pt_status_reg_init
,
644 .u
.w
.read
= xen_pt_word_reg_read
,
645 .u
.w
.write
= xen_pt_word_reg_write
,
647 /* Cache Line Size reg */
649 .offset
= PCI_CACHE_LINE_SIZE
,
654 .init
= xen_pt_common_reg_init
,
655 .u
.b
.read
= xen_pt_byte_reg_read
,
656 .u
.b
.write
= xen_pt_byte_reg_write
,
658 /* Latency Timer reg */
660 .offset
= PCI_LATENCY_TIMER
,
665 .init
= xen_pt_common_reg_init
,
666 .u
.b
.read
= xen_pt_byte_reg_read
,
667 .u
.b
.write
= xen_pt_byte_reg_write
,
669 /* Header Type reg */
671 .offset
= PCI_HEADER_TYPE
,
676 .init
= xen_pt_header_type_reg_init
,
677 .u
.b
.read
= xen_pt_byte_reg_read
,
678 .u
.b
.write
= xen_pt_byte_reg_write
,
680 /* Interrupt Line reg */
682 .offset
= PCI_INTERRUPT_LINE
,
687 .init
= xen_pt_common_reg_init
,
688 .u
.b
.read
= xen_pt_byte_reg_read
,
689 .u
.b
.write
= xen_pt_byte_reg_write
,
691 /* Interrupt Pin reg */
693 .offset
= PCI_INTERRUPT_PIN
,
698 .init
= xen_pt_irqpin_reg_init
,
699 .u
.b
.read
= xen_pt_byte_reg_read
,
700 .u
.b
.write
= xen_pt_byte_reg_write
,
703 /* mask of BAR need to be decided later, depends on IO/MEM type */
705 .offset
= PCI_BASE_ADDRESS_0
,
707 .init_val
= 0x00000000,
708 .init
= xen_pt_bar_reg_init
,
709 .u
.dw
.read
= xen_pt_bar_reg_read
,
710 .u
.dw
.write
= xen_pt_bar_reg_write
,
714 .offset
= PCI_BASE_ADDRESS_1
,
716 .init_val
= 0x00000000,
717 .init
= xen_pt_bar_reg_init
,
718 .u
.dw
.read
= xen_pt_bar_reg_read
,
719 .u
.dw
.write
= xen_pt_bar_reg_write
,
723 .offset
= PCI_BASE_ADDRESS_2
,
725 .init_val
= 0x00000000,
726 .init
= xen_pt_bar_reg_init
,
727 .u
.dw
.read
= xen_pt_bar_reg_read
,
728 .u
.dw
.write
= xen_pt_bar_reg_write
,
732 .offset
= PCI_BASE_ADDRESS_3
,
734 .init_val
= 0x00000000,
735 .init
= xen_pt_bar_reg_init
,
736 .u
.dw
.read
= xen_pt_bar_reg_read
,
737 .u
.dw
.write
= xen_pt_bar_reg_write
,
741 .offset
= PCI_BASE_ADDRESS_4
,
743 .init_val
= 0x00000000,
744 .init
= xen_pt_bar_reg_init
,
745 .u
.dw
.read
= xen_pt_bar_reg_read
,
746 .u
.dw
.write
= xen_pt_bar_reg_write
,
750 .offset
= PCI_BASE_ADDRESS_5
,
752 .init_val
= 0x00000000,
753 .init
= xen_pt_bar_reg_init
,
754 .u
.dw
.read
= xen_pt_bar_reg_read
,
755 .u
.dw
.write
= xen_pt_bar_reg_write
,
757 /* Expansion ROM BAR reg */
759 .offset
= PCI_ROM_ADDRESS
,
761 .init_val
= 0x00000000,
762 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
763 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
764 .init
= xen_pt_bar_reg_init
,
765 .u
.dw
.read
= xen_pt_long_reg_read
,
766 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
774 /*********************************
775 * Vital Product Data Capability
778 /* Vital Product Data Capability Structure reg static information table */
779 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
781 .offset
= PCI_CAP_LIST_NEXT
,
786 .init
= xen_pt_ptr_reg_init
,
787 .u
.b
.read
= xen_pt_byte_reg_read
,
788 .u
.b
.write
= xen_pt_byte_reg_write
,
791 .offset
= PCI_VPD_ADDR
,
795 .init
= xen_pt_common_reg_init
,
796 .u
.w
.read
= xen_pt_word_reg_read
,
797 .u
.w
.write
= xen_pt_word_reg_write
,
805 /**************************************
806 * Vendor Specific Capability
809 /* Vendor Specific Capability Structure reg static information table */
810 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
812 .offset
= PCI_CAP_LIST_NEXT
,
817 .init
= xen_pt_ptr_reg_init
,
818 .u
.b
.read
= xen_pt_byte_reg_read
,
819 .u
.b
.write
= xen_pt_byte_reg_write
,
827 /*****************************
828 * PCI Express Capability
831 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
835 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
838 return flag
& PCI_EXP_FLAGS_VERS
;
841 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
845 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
848 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
851 /* initialize Link Control register */
852 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
853 XenPTRegInfo
*reg
, uint32_t real_offset
,
856 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
857 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
859 /* no need to initialize in case of Root Complex Integrated Endpoint
862 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
863 *data
= XEN_PT_INVALID_REG
;
866 *data
= reg
->init_val
;
869 /* initialize Device Control 2 register */
870 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
871 XenPTRegInfo
*reg
, uint32_t real_offset
,
874 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
876 /* no need to initialize in case of cap_ver 1.x */
878 *data
= XEN_PT_INVALID_REG
;
881 *data
= reg
->init_val
;
884 /* initialize Link Control 2 register */
885 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
886 XenPTRegInfo
*reg
, uint32_t real_offset
,
889 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
890 uint32_t reg_field
= 0;
892 /* no need to initialize in case of cap_ver 1.x */
894 reg_field
= XEN_PT_INVALID_REG
;
896 /* set Supported Link Speed */
899 rc
= xen_host_pci_get_byte(&s
->real_device
,
900 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
905 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
912 /* PCI Express Capability Structure reg static information table */
913 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
914 /* Next Pointer reg */
916 .offset
= PCI_CAP_LIST_NEXT
,
921 .init
= xen_pt_ptr_reg_init
,
922 .u
.b
.read
= xen_pt_byte_reg_read
,
923 .u
.b
.write
= xen_pt_byte_reg_write
,
925 /* Device Capabilities reg */
927 .offset
= PCI_EXP_DEVCAP
,
929 .init_val
= 0x00000000,
930 .ro_mask
= 0xFFFFFFFF,
931 .emu_mask
= 0x10000000,
932 .init
= xen_pt_common_reg_init
,
933 .u
.dw
.read
= xen_pt_long_reg_read
,
934 .u
.dw
.write
= xen_pt_long_reg_write
,
936 /* Device Control reg */
938 .offset
= PCI_EXP_DEVCTL
,
943 .init
= xen_pt_common_reg_init
,
944 .u
.w
.read
= xen_pt_word_reg_read
,
945 .u
.w
.write
= xen_pt_word_reg_write
,
947 /* Device Status reg */
949 .offset
= PCI_EXP_DEVSTA
,
954 .init
= xen_pt_common_reg_init
,
955 .u
.w
.read
= xen_pt_word_reg_read
,
956 .u
.w
.write
= xen_pt_word_reg_write
,
958 /* Link Control reg */
960 .offset
= PCI_EXP_LNKCTL
,
965 .init
= xen_pt_linkctrl_reg_init
,
966 .u
.w
.read
= xen_pt_word_reg_read
,
967 .u
.w
.write
= xen_pt_word_reg_write
,
969 /* Link Status reg */
971 .offset
= PCI_EXP_LNKSTA
,
975 .init
= xen_pt_common_reg_init
,
976 .u
.w
.read
= xen_pt_word_reg_read
,
977 .u
.w
.write
= xen_pt_word_reg_write
,
979 /* Device Control 2 reg */
986 .init
= xen_pt_devctrl2_reg_init
,
987 .u
.w
.read
= xen_pt_word_reg_read
,
988 .u
.w
.write
= xen_pt_word_reg_write
,
990 /* Link Control 2 reg */
997 .init
= xen_pt_linkctrl2_reg_init
,
998 .u
.w
.read
= xen_pt_word_reg_read
,
999 .u
.w
.write
= xen_pt_word_reg_write
,
1007 /*********************************
1008 * Power Management Capability
1011 /* Power Management Capability reg static information table */
1012 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1013 /* Next Pointer reg */
1015 .offset
= PCI_CAP_LIST_NEXT
,
1020 .init
= xen_pt_ptr_reg_init
,
1021 .u
.b
.read
= xen_pt_byte_reg_read
,
1022 .u
.b
.write
= xen_pt_byte_reg_write
,
1024 /* Power Management Capabilities reg */
1026 .offset
= PCI_CAP_FLAGS
,
1031 .init
= xen_pt_common_reg_init
,
1032 .u
.w
.read
= xen_pt_word_reg_read
,
1033 .u
.w
.write
= xen_pt_word_reg_write
,
1035 /* PCI Power Management Control/Status reg */
1037 .offset
= PCI_PM_CTRL
,
1042 .rw1c_mask
= 0x8000,
1044 .init
= xen_pt_common_reg_init
,
1045 .u
.w
.read
= xen_pt_word_reg_read
,
1046 .u
.w
.write
= xen_pt_word_reg_write
,
1054 /********************************
1059 #define xen_pt_msi_check_type(offset, flags, what) \
1060 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1061 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1063 /* Message Control register */
1064 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1065 XenPTRegInfo
*reg
, uint32_t real_offset
,
1068 XenPTMSI
*msi
= s
->msi
;
1072 /* use I/O device register's value as initial value */
1073 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1077 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1078 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1079 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1080 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1082 msi
->flags
|= reg_field
;
1083 msi
->ctrl_offset
= real_offset
;
1084 msi
->initialized
= false;
1085 msi
->mapped
= false;
1087 *data
= reg
->init_val
;
1090 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1091 XenPTReg
*cfg_entry
, uint16_t *val
,
1092 uint16_t dev_value
, uint16_t valid_mask
)
1094 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1095 XenPTMSI
*msi
= s
->msi
;
1096 uint16_t writable_mask
= 0;
1097 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1098 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1100 /* Currently no support for multi-vector */
1101 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1102 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1105 /* modify emulate register */
1106 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1107 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1108 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1110 /* create value for writing to I/O device register */
1111 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1114 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1115 /* setup MSI pirq for the first time */
1116 if (!msi
->initialized
) {
1117 /* Init physical one */
1118 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1119 if (xen_pt_msi_setup(s
)) {
1120 /* We do not broadcast the error to the framework code, so
1121 * that MSI errors are contained in MSI emulation code and
1122 * QEMU can go on running.
1123 * Guest MSI would be actually not working.
1125 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1126 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1129 if (xen_pt_msi_update(s
)) {
1130 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1131 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1134 msi
->initialized
= true;
1137 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1138 } else if (msi
->mapped
) {
1139 xen_pt_msi_disable(s
);
1145 /* initialize Message Upper Address register */
1146 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1147 XenPTRegInfo
*reg
, uint32_t real_offset
,
1150 /* no need to initialize in case of 32 bit type */
1151 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1152 *data
= XEN_PT_INVALID_REG
;
1154 *data
= reg
->init_val
;
1159 /* this function will be called twice (for 32 bit and 64 bit type) */
1160 /* initialize Message Data register */
1161 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1162 XenPTRegInfo
*reg
, uint32_t real_offset
,
1165 uint32_t flags
= s
->msi
->flags
;
1166 uint32_t offset
= reg
->offset
;
1168 /* check the offset whether matches the type or not */
1169 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1170 *data
= reg
->init_val
;
1172 *data
= XEN_PT_INVALID_REG
;
1177 /* this function will be called twice (for 32 bit and 64 bit type) */
1178 /* initialize Mask register */
1179 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1180 XenPTRegInfo
*reg
, uint32_t real_offset
,
1183 uint32_t flags
= s
->msi
->flags
;
1185 /* check the offset whether matches the type or not */
1186 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1187 *data
= XEN_PT_INVALID_REG
;
1188 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1189 *data
= reg
->init_val
;
1191 *data
= XEN_PT_INVALID_REG
;
1196 /* this function will be called twice (for 32 bit and 64 bit type) */
1197 /* initialize Pending register */
1198 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1199 XenPTRegInfo
*reg
, uint32_t real_offset
,
1202 uint32_t flags
= s
->msi
->flags
;
1204 /* check the offset whether matches the type or not */
1205 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1206 *data
= XEN_PT_INVALID_REG
;
1207 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1208 *data
= reg
->init_val
;
1210 *data
= XEN_PT_INVALID_REG
;
1215 /* write Message Address register */
1216 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1217 XenPTReg
*cfg_entry
, uint32_t *val
,
1218 uint32_t dev_value
, uint32_t valid_mask
)
1220 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1221 uint32_t writable_mask
= 0;
1222 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1223 uint32_t *data
= cfg_entry
->ptr
.word
;
1225 /* modify emulate register */
1226 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1227 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1228 s
->msi
->addr_lo
= *data
;
1230 /* create value for writing to I/O device register */
1231 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1234 if (*data
!= old_addr
) {
1235 if (s
->msi
->mapped
) {
1236 xen_pt_msi_update(s
);
1242 /* write Message Upper Address register */
1243 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1244 XenPTReg
*cfg_entry
, uint32_t *val
,
1245 uint32_t dev_value
, uint32_t valid_mask
)
1247 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1248 uint32_t writable_mask
= 0;
1249 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1250 uint32_t *data
= cfg_entry
->ptr
.word
;
1252 /* check whether the type is 64 bit or not */
1253 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1255 "Can't write to the upper address without 64 bit support\n");
1259 /* modify emulate register */
1260 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1261 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1262 /* update the msi_info too */
1263 s
->msi
->addr_hi
= *data
;
1265 /* create value for writing to I/O device register */
1266 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1269 if (*data
!= old_addr
) {
1270 if (s
->msi
->mapped
) {
1271 xen_pt_msi_update(s
);
1279 /* this function will be called twice (for 32 bit and 64 bit type) */
1280 /* write Message Data register */
1281 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1282 XenPTReg
*cfg_entry
, uint16_t *val
,
1283 uint16_t dev_value
, uint16_t valid_mask
)
1285 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1286 XenPTMSI
*msi
= s
->msi
;
1287 uint16_t writable_mask
= 0;
1288 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1289 uint32_t offset
= reg
->offset
;
1290 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1292 /* check the offset whether matches the type or not */
1293 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1294 /* exit I/O emulator */
1295 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1299 /* modify emulate register */
1300 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1301 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1302 /* update the msi_info too */
1305 /* create value for writing to I/O device register */
1306 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1309 if (*data
!= old_data
) {
1311 xen_pt_msi_update(s
);
1318 /* MSI Capability Structure reg static information table */
1319 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1320 /* Next Pointer reg */
1322 .offset
= PCI_CAP_LIST_NEXT
,
1327 .init
= xen_pt_ptr_reg_init
,
1328 .u
.b
.read
= xen_pt_byte_reg_read
,
1329 .u
.b
.write
= xen_pt_byte_reg_write
,
1331 /* Message Control reg */
1333 .offset
= PCI_MSI_FLAGS
,
1339 .init
= xen_pt_msgctrl_reg_init
,
1340 .u
.w
.read
= xen_pt_word_reg_read
,
1341 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1343 /* Message Address reg */
1345 .offset
= PCI_MSI_ADDRESS_LO
,
1347 .init_val
= 0x00000000,
1348 .ro_mask
= 0x00000003,
1349 .emu_mask
= 0xFFFFFFFF,
1350 .init
= xen_pt_common_reg_init
,
1351 .u
.dw
.read
= xen_pt_long_reg_read
,
1352 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1354 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1356 .offset
= PCI_MSI_ADDRESS_HI
,
1358 .init_val
= 0x00000000,
1359 .ro_mask
= 0x00000000,
1360 .emu_mask
= 0xFFFFFFFF,
1361 .init
= xen_pt_msgaddr64_reg_init
,
1362 .u
.dw
.read
= xen_pt_long_reg_read
,
1363 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1365 /* Message Data reg (16 bits of data for 32-bit devices) */
1367 .offset
= PCI_MSI_DATA_32
,
1372 .init
= xen_pt_msgdata_reg_init
,
1373 .u
.w
.read
= xen_pt_word_reg_read
,
1374 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1376 /* Message Data reg (16 bits of data for 64-bit devices) */
1378 .offset
= PCI_MSI_DATA_64
,
1383 .init
= xen_pt_msgdata_reg_init
,
1384 .u
.w
.read
= xen_pt_word_reg_read
,
1385 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1387 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1389 .offset
= PCI_MSI_MASK_32
,
1391 .init_val
= 0x00000000,
1392 .ro_mask
= 0xFFFFFFFF,
1393 .emu_mask
= 0xFFFFFFFF,
1394 .init
= xen_pt_mask_reg_init
,
1395 .u
.dw
.read
= xen_pt_long_reg_read
,
1396 .u
.dw
.write
= xen_pt_long_reg_write
,
1398 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1400 .offset
= PCI_MSI_MASK_64
,
1402 .init_val
= 0x00000000,
1403 .ro_mask
= 0xFFFFFFFF,
1404 .emu_mask
= 0xFFFFFFFF,
1405 .init
= xen_pt_mask_reg_init
,
1406 .u
.dw
.read
= xen_pt_long_reg_read
,
1407 .u
.dw
.write
= xen_pt_long_reg_write
,
1409 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1411 .offset
= PCI_MSI_MASK_32
+ 4,
1413 .init_val
= 0x00000000,
1414 .ro_mask
= 0xFFFFFFFF,
1415 .emu_mask
= 0x00000000,
1416 .init
= xen_pt_pending_reg_init
,
1417 .u
.dw
.read
= xen_pt_long_reg_read
,
1418 .u
.dw
.write
= xen_pt_long_reg_write
,
1420 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1422 .offset
= PCI_MSI_MASK_64
+ 4,
1424 .init_val
= 0x00000000,
1425 .ro_mask
= 0xFFFFFFFF,
1426 .emu_mask
= 0x00000000,
1427 .init
= xen_pt_pending_reg_init
,
1428 .u
.dw
.read
= xen_pt_long_reg_read
,
1429 .u
.dw
.write
= xen_pt_long_reg_write
,
1437 /**************************************
1441 /* Message Control register for MSI-X */
1442 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1443 XenPTRegInfo
*reg
, uint32_t real_offset
,
1449 /* use I/O device register's value as initial value */
1450 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1454 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1455 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1456 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1457 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1460 s
->msix
->ctrl_offset
= real_offset
;
1462 *data
= reg
->init_val
;
1465 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1466 XenPTReg
*cfg_entry
, uint16_t *val
,
1467 uint16_t dev_value
, uint16_t valid_mask
)
1469 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1470 uint16_t writable_mask
= 0;
1471 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1472 int debug_msix_enabled_old
;
1473 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1475 /* modify emulate register */
1476 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1477 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1479 /* create value for writing to I/O device register */
1480 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1483 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1484 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1485 xen_pt_msix_update(s
);
1486 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1487 xen_pt_msix_disable(s
);
1490 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1492 debug_msix_enabled_old
= s
->msix
->enabled
;
1493 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1494 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1495 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1496 s
->msix
->enabled
? "enable" : "disable");
1502 /* MSI-X Capability Structure reg static information table */
1503 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1504 /* Next Pointer reg */
1506 .offset
= PCI_CAP_LIST_NEXT
,
1511 .init
= xen_pt_ptr_reg_init
,
1512 .u
.b
.read
= xen_pt_byte_reg_read
,
1513 .u
.b
.write
= xen_pt_byte_reg_write
,
1515 /* Message Control reg */
1517 .offset
= PCI_MSI_FLAGS
,
1523 .init
= xen_pt_msixctrl_reg_init
,
1524 .u
.w
.read
= xen_pt_word_reg_read
,
1525 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1532 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1533 /* Intel IGFX OpRegion reg */
1538 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1539 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1546 /****************************
1550 /* capability structure register group size functions */
1552 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1553 const XenPTRegGroupInfo
*grp_reg
,
1554 uint32_t base_offset
, uint8_t *size
)
1556 *size
= grp_reg
->grp_size
;
1559 /* get Vendor Specific Capability Structure register group size */
1560 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1561 const XenPTRegGroupInfo
*grp_reg
,
1562 uint32_t base_offset
, uint8_t *size
)
1564 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1566 /* get PCI Express Capability Structure register group size */
1567 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1568 const XenPTRegGroupInfo
*grp_reg
,
1569 uint32_t base_offset
, uint8_t *size
)
1571 PCIDevice
*d
= &s
->dev
;
1572 uint8_t version
= get_capability_version(s
, base_offset
);
1573 uint8_t type
= get_device_type(s
, base_offset
);
1574 uint8_t pcie_size
= 0;
1577 /* calculate size depend on capability version and device/port type */
1578 /* in case of PCI Express Base Specification Rev 1.x */
1580 /* The PCI Express Capabilities, Device Capabilities, and Device
1581 * Status/Control registers are required for all PCI Express devices.
1582 * The Link Capabilities and Link Status/Control are required for all
1583 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1584 * are not required to implement registers other than those listed
1585 * above and terminate the capability structure.
1588 case PCI_EXP_TYPE_ENDPOINT
:
1589 case PCI_EXP_TYPE_LEG_END
:
1592 case PCI_EXP_TYPE_RC_END
:
1596 /* only EndPoint passthrough is supported */
1597 case PCI_EXP_TYPE_ROOT_PORT
:
1598 case PCI_EXP_TYPE_UPSTREAM
:
1599 case PCI_EXP_TYPE_DOWNSTREAM
:
1600 case PCI_EXP_TYPE_PCI_BRIDGE
:
1601 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1602 case PCI_EXP_TYPE_RC_EC
:
1604 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1608 /* in case of PCI Express Base Specification Rev 2.0 */
1609 else if (version
== 2) {
1611 case PCI_EXP_TYPE_ENDPOINT
:
1612 case PCI_EXP_TYPE_LEG_END
:
1613 case PCI_EXP_TYPE_RC_END
:
1614 /* For Functions that do not implement the registers,
1615 * these spaces must be hardwired to 0b.
1619 /* only EndPoint passthrough is supported */
1620 case PCI_EXP_TYPE_ROOT_PORT
:
1621 case PCI_EXP_TYPE_UPSTREAM
:
1622 case PCI_EXP_TYPE_DOWNSTREAM
:
1623 case PCI_EXP_TYPE_PCI_BRIDGE
:
1624 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1625 case PCI_EXP_TYPE_RC_EC
:
1627 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1631 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1638 /* get MSI Capability Structure register group size */
1639 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1640 const XenPTRegGroupInfo
*grp_reg
,
1641 uint32_t base_offset
, uint8_t *size
)
1643 uint16_t msg_ctrl
= 0;
1644 uint8_t msi_size
= 0xa;
1647 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1652 /* check if 64-bit address is capable of per-vector masking */
1653 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1656 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1660 s
->msi
= g_new0(XenPTMSI
, 1);
1661 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1666 /* get MSI-X Capability Structure register group size */
1667 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1668 const XenPTRegGroupInfo
*grp_reg
,
1669 uint32_t base_offset
, uint8_t *size
)
1673 rc
= xen_pt_msix_init(s
, base_offset
);
1676 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1680 *size
= grp_reg
->grp_size
;
1685 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1686 /* Header Type0 reg group */
1689 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1691 .size_init
= xen_pt_reg_grp_size_init
,
1692 .emu_regs
= xen_pt_emu_reg_header0
,
1694 /* PCI PowerManagement Capability reg group */
1696 .grp_id
= PCI_CAP_ID_PM
,
1697 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1698 .grp_size
= PCI_PM_SIZEOF
,
1699 .size_init
= xen_pt_reg_grp_size_init
,
1700 .emu_regs
= xen_pt_emu_reg_pm
,
1702 /* AGP Capability Structure reg group */
1704 .grp_id
= PCI_CAP_ID_AGP
,
1705 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1707 .size_init
= xen_pt_reg_grp_size_init
,
1709 /* Vital Product Data Capability Structure reg group */
1711 .grp_id
= PCI_CAP_ID_VPD
,
1712 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1714 .size_init
= xen_pt_reg_grp_size_init
,
1715 .emu_regs
= xen_pt_emu_reg_vpd
,
1717 /* Slot Identification reg group */
1719 .grp_id
= PCI_CAP_ID_SLOTID
,
1720 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1722 .size_init
= xen_pt_reg_grp_size_init
,
1724 /* MSI Capability Structure reg group */
1726 .grp_id
= PCI_CAP_ID_MSI
,
1727 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1729 .size_init
= xen_pt_msi_size_init
,
1730 .emu_regs
= xen_pt_emu_reg_msi
,
1732 /* PCI-X Capabilities List Item reg group */
1734 .grp_id
= PCI_CAP_ID_PCIX
,
1735 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1737 .size_init
= xen_pt_reg_grp_size_init
,
1739 /* Vendor Specific Capability Structure reg group */
1741 .grp_id
= PCI_CAP_ID_VNDR
,
1742 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1744 .size_init
= xen_pt_vendor_size_init
,
1745 .emu_regs
= xen_pt_emu_reg_vendor
,
1747 /* SHPC Capability List Item reg group */
1749 .grp_id
= PCI_CAP_ID_SHPC
,
1750 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1752 .size_init
= xen_pt_reg_grp_size_init
,
1754 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1756 .grp_id
= PCI_CAP_ID_SSVID
,
1757 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1759 .size_init
= xen_pt_reg_grp_size_init
,
1761 /* AGP 8x Capability Structure reg group */
1763 .grp_id
= PCI_CAP_ID_AGP3
,
1764 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1766 .size_init
= xen_pt_reg_grp_size_init
,
1768 /* PCI Express Capability Structure reg group */
1770 .grp_id
= PCI_CAP_ID_EXP
,
1771 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1773 .size_init
= xen_pt_pcie_size_init
,
1774 .emu_regs
= xen_pt_emu_reg_pcie
,
1776 /* MSI-X Capability Structure reg group */
1778 .grp_id
= PCI_CAP_ID_MSIX
,
1779 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1781 .size_init
= xen_pt_msix_size_init
,
1782 .emu_regs
= xen_pt_emu_reg_msix
,
1784 /* Intel IGD Opregion group */
1786 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1787 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1789 .size_init
= xen_pt_reg_grp_size_init
,
1790 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1797 /* initialize Capabilities Pointer or Next Pointer register */
1798 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1799 XenPTRegInfo
*reg
, uint32_t real_offset
,
1806 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1810 /* find capability offset */
1812 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1813 if (xen_pt_hide_dev_cap(&s
->real_device
,
1814 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1818 rc
= xen_host_pci_get_byte(&s
->real_device
,
1819 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1821 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1822 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1825 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1826 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1829 /* ignore the 0 hardwired capability, find next one */
1834 /* next capability */
1835 rc
= xen_host_pci_get_byte(&s
->real_device
,
1836 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1852 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1855 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1856 uint8_t pos
= PCI_CAPABILITY_LIST
;
1859 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1862 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1867 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1870 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1875 if (xen_host_pci_get_byte(&s
->real_device
,
1876 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1887 pos
+= PCI_CAP_LIST_NEXT
;
1892 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1893 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1896 XenPTReg
*reg_entry
;
1900 reg_entry
= g_new0(XenPTReg
, 1);
1901 reg_entry
->reg
= reg
;
1904 uint32_t host_mask
, size_mask
;
1905 unsigned int offset
;
1908 /* initialize emulate register */
1909 rc
= reg
->init(s
, reg_entry
->reg
,
1910 reg_grp
->base_offset
+ reg
->offset
, &data
);
1913 error_setg(errp
, "Init emulate register fail");
1916 if (data
== XEN_PT_INVALID_REG
) {
1917 /* free unused BAR register entry */
1921 /* Sync up the data to dev.config */
1922 offset
= reg_grp
->base_offset
+ reg
->offset
;
1923 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1925 switch (reg
->size
) {
1926 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1928 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1930 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1935 /* Serious issues when we cannot read the host values! */
1937 error_setg(errp
, "Cannot read host values");
1940 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1941 * contain the emulated view of the guest - therefore we flip the mask
1942 * to mask out the host values (which dev.config initially has) . */
1943 host_mask
= size_mask
& ~reg
->emu_mask
;
1945 if ((data
& host_mask
) != (val
& host_mask
)) {
1948 /* Mask out host (including past size). */
1949 new_val
= val
& host_mask
;
1950 /* Merge emulated ones (excluding the non-emulated ones). */
1951 new_val
|= data
& host_mask
;
1952 /* Leave intact host and emulated values past the size - even though
1953 * we do not care as we write per reg->size granularity, but for the
1954 * logging below lets have the proper value. */
1955 new_val
|= ((val
| data
)) & ~size_mask
;
1956 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1957 offset
, data
, val
, new_val
);
1962 if (val
& ~size_mask
) {
1963 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1964 " register size (%d)", offset
, val
, reg
->size
);
1968 /* This could be just pci_set_long as we don't modify the bits
1969 * past reg->size, but in case this routine is run in parallel or the
1970 * init value is larger, we do not want to over-write registers. */
1971 switch (reg
->size
) {
1972 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1974 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1976 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1980 /* set register value pointer to the data. */
1981 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
1984 /* list add register entry */
1985 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
1988 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
1993 QLIST_INIT(&s
->reg_grps
);
1995 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1996 uint32_t reg_grp_offset
= 0;
1997 XenPTRegGroup
*reg_grp_entry
= NULL
;
1999 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2000 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2001 if (xen_pt_hide_dev_cap(&s
->real_device
,
2002 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2006 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2008 if (!reg_grp_offset
) {
2014 * By default we will trap up to 0x40 in the cfg space.
2015 * If an intel device is pass through we need to trap 0xfc,
2016 * therefore the size should be 0xff.
2018 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2019 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2022 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2023 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2024 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2026 reg_grp_entry
->base_offset
= reg_grp_offset
;
2027 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2028 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2029 /* get register group size */
2030 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2032 ®_grp_entry
->size
);
2034 error_setg(&err
, "Failed to initialize %d/%zu, type = 0x%x,"
2035 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2036 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2037 error_propagate(errp
, err
);
2038 xen_pt_config_delete(s
);
2043 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2044 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2046 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2048 /* initialize capability register */
2049 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2050 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, &err
);
2052 error_append_hint(&err
, "Failed to initialize %d/%zu"
2053 " reg 0x%x in grp_type = 0x%x (%d/%zu)",
2054 j
, ARRAY_SIZE(xen_pt_emu_reg_grps
[i
].emu_regs
),
2055 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2056 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2057 error_propagate(errp
, err
);
2058 xen_pt_config_delete(s
);
2067 /* delete all emulate register */
2068 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2070 struct XenPTRegGroup
*reg_group
, *next_grp
;
2071 struct XenPTReg
*reg
, *next_reg
;
2073 /* free MSI/MSI-X info table */
2075 xen_pt_msix_unmap(s
);
2079 /* free all register group entry */
2080 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2081 /* free all register entry */
2082 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2083 QLIST_REMOVE(reg
, entries
);
2087 QLIST_REMOVE(reg_group
, entries
);