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 static int xen_pt_mask_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
1319 uint32_t *val
, uint32_t dev_value
,
1320 uint32_t valid_mask
)
1324 rc
= xen_pt_long_reg_write(s
, cfg_entry
, val
, dev_value
, valid_mask
);
1329 s
->msi
->mask
= *val
;
1334 /* MSI Capability Structure reg static information table */
1335 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1336 /* Next Pointer reg */
1338 .offset
= PCI_CAP_LIST_NEXT
,
1343 .init
= xen_pt_ptr_reg_init
,
1344 .u
.b
.read
= xen_pt_byte_reg_read
,
1345 .u
.b
.write
= xen_pt_byte_reg_write
,
1347 /* Message Control reg */
1349 .offset
= PCI_MSI_FLAGS
,
1355 .init
= xen_pt_msgctrl_reg_init
,
1356 .u
.w
.read
= xen_pt_word_reg_read
,
1357 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1359 /* Message Address reg */
1361 .offset
= PCI_MSI_ADDRESS_LO
,
1363 .init_val
= 0x00000000,
1364 .ro_mask
= 0x00000003,
1365 .emu_mask
= 0xFFFFFFFF,
1366 .init
= xen_pt_common_reg_init
,
1367 .u
.dw
.read
= xen_pt_long_reg_read
,
1368 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1370 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1372 .offset
= PCI_MSI_ADDRESS_HI
,
1374 .init_val
= 0x00000000,
1375 .ro_mask
= 0x00000000,
1376 .emu_mask
= 0xFFFFFFFF,
1377 .init
= xen_pt_msgaddr64_reg_init
,
1378 .u
.dw
.read
= xen_pt_long_reg_read
,
1379 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1381 /* Message Data reg (16 bits of data for 32-bit devices) */
1383 .offset
= PCI_MSI_DATA_32
,
1388 .init
= xen_pt_msgdata_reg_init
,
1389 .u
.w
.read
= xen_pt_word_reg_read
,
1390 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1392 /* Message Data reg (16 bits of data for 64-bit devices) */
1394 .offset
= PCI_MSI_DATA_64
,
1399 .init
= xen_pt_msgdata_reg_init
,
1400 .u
.w
.read
= xen_pt_word_reg_read
,
1401 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1403 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1405 .offset
= PCI_MSI_MASK_32
,
1407 .init_val
= 0x00000000,
1408 .ro_mask
= 0xFFFFFFFF,
1409 .emu_mask
= 0xFFFFFFFF,
1410 .init
= xen_pt_mask_reg_init
,
1411 .u
.dw
.read
= xen_pt_long_reg_read
,
1412 .u
.dw
.write
= xen_pt_mask_reg_write
,
1414 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1416 .offset
= PCI_MSI_MASK_64
,
1418 .init_val
= 0x00000000,
1419 .ro_mask
= 0xFFFFFFFF,
1420 .emu_mask
= 0xFFFFFFFF,
1421 .init
= xen_pt_mask_reg_init
,
1422 .u
.dw
.read
= xen_pt_long_reg_read
,
1423 .u
.dw
.write
= xen_pt_mask_reg_write
,
1425 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1427 .offset
= PCI_MSI_MASK_32
+ 4,
1429 .init_val
= 0x00000000,
1430 .ro_mask
= 0xFFFFFFFF,
1431 .emu_mask
= 0x00000000,
1432 .init
= xen_pt_pending_reg_init
,
1433 .u
.dw
.read
= xen_pt_long_reg_read
,
1434 .u
.dw
.write
= xen_pt_long_reg_write
,
1436 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1438 .offset
= PCI_MSI_MASK_64
+ 4,
1440 .init_val
= 0x00000000,
1441 .ro_mask
= 0xFFFFFFFF,
1442 .emu_mask
= 0x00000000,
1443 .init
= xen_pt_pending_reg_init
,
1444 .u
.dw
.read
= xen_pt_long_reg_read
,
1445 .u
.dw
.write
= xen_pt_long_reg_write
,
1453 /**************************************
1457 /* Message Control register for MSI-X */
1458 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1459 XenPTRegInfo
*reg
, uint32_t real_offset
,
1465 /* use I/O device register's value as initial value */
1466 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1470 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1471 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1472 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1473 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1476 s
->msix
->ctrl_offset
= real_offset
;
1478 *data
= reg
->init_val
;
1481 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1482 XenPTReg
*cfg_entry
, uint16_t *val
,
1483 uint16_t dev_value
, uint16_t valid_mask
)
1485 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1486 uint16_t writable_mask
= 0;
1487 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1488 int debug_msix_enabled_old
;
1489 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1491 /* modify emulate register */
1492 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1493 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1495 /* create value for writing to I/O device register */
1496 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1499 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1500 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1501 xen_pt_msix_update(s
);
1502 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1503 xen_pt_msix_disable(s
);
1506 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1508 debug_msix_enabled_old
= s
->msix
->enabled
;
1509 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1510 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1511 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1512 s
->msix
->enabled
? "enable" : "disable");
1518 /* MSI-X Capability Structure reg static information table */
1519 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1520 /* Next Pointer reg */
1522 .offset
= PCI_CAP_LIST_NEXT
,
1527 .init
= xen_pt_ptr_reg_init
,
1528 .u
.b
.read
= xen_pt_byte_reg_read
,
1529 .u
.b
.write
= xen_pt_byte_reg_write
,
1531 /* Message Control reg */
1533 .offset
= PCI_MSI_FLAGS
,
1539 .init
= xen_pt_msixctrl_reg_init
,
1540 .u
.w
.read
= xen_pt_word_reg_read
,
1541 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1548 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1549 /* Intel IGFX OpRegion reg */
1554 .emu_mask
= 0xFFFFFFFF,
1555 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1556 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1563 /****************************
1567 /* capability structure register group size functions */
1569 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1570 const XenPTRegGroupInfo
*grp_reg
,
1571 uint32_t base_offset
, uint8_t *size
)
1573 *size
= grp_reg
->grp_size
;
1576 /* get Vendor Specific Capability Structure register group size */
1577 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1578 const XenPTRegGroupInfo
*grp_reg
,
1579 uint32_t base_offset
, uint8_t *size
)
1581 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1583 /* get PCI Express Capability Structure register group size */
1584 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1585 const XenPTRegGroupInfo
*grp_reg
,
1586 uint32_t base_offset
, uint8_t *size
)
1588 PCIDevice
*d
= &s
->dev
;
1589 uint8_t version
= get_capability_version(s
, base_offset
);
1590 uint8_t type
= get_device_type(s
, base_offset
);
1591 uint8_t pcie_size
= 0;
1594 /* calculate size depend on capability version and device/port type */
1595 /* in case of PCI Express Base Specification Rev 1.x */
1597 /* The PCI Express Capabilities, Device Capabilities, and Device
1598 * Status/Control registers are required for all PCI Express devices.
1599 * The Link Capabilities and Link Status/Control are required for all
1600 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1601 * are not required to implement registers other than those listed
1602 * above and terminate the capability structure.
1605 case PCI_EXP_TYPE_ENDPOINT
:
1606 case PCI_EXP_TYPE_LEG_END
:
1609 case PCI_EXP_TYPE_RC_END
:
1613 /* only EndPoint passthrough is supported */
1614 case PCI_EXP_TYPE_ROOT_PORT
:
1615 case PCI_EXP_TYPE_UPSTREAM
:
1616 case PCI_EXP_TYPE_DOWNSTREAM
:
1617 case PCI_EXP_TYPE_PCI_BRIDGE
:
1618 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1619 case PCI_EXP_TYPE_RC_EC
:
1621 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1625 /* in case of PCI Express Base Specification Rev 2.0 */
1626 else if (version
== 2) {
1628 case PCI_EXP_TYPE_ENDPOINT
:
1629 case PCI_EXP_TYPE_LEG_END
:
1630 case PCI_EXP_TYPE_RC_END
:
1631 /* For Functions that do not implement the registers,
1632 * these spaces must be hardwired to 0b.
1636 /* only EndPoint passthrough is supported */
1637 case PCI_EXP_TYPE_ROOT_PORT
:
1638 case PCI_EXP_TYPE_UPSTREAM
:
1639 case PCI_EXP_TYPE_DOWNSTREAM
:
1640 case PCI_EXP_TYPE_PCI_BRIDGE
:
1641 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1642 case PCI_EXP_TYPE_RC_EC
:
1644 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1648 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1655 /* get MSI Capability Structure register group size */
1656 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1657 const XenPTRegGroupInfo
*grp_reg
,
1658 uint32_t base_offset
, uint8_t *size
)
1660 uint16_t msg_ctrl
= 0;
1661 uint8_t msi_size
= 0xa;
1664 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1669 /* check if 64-bit address is capable of per-vector masking */
1670 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1673 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1677 s
->msi
= g_new0(XenPTMSI
, 1);
1678 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1683 /* get MSI-X Capability Structure register group size */
1684 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1685 const XenPTRegGroupInfo
*grp_reg
,
1686 uint32_t base_offset
, uint8_t *size
)
1690 rc
= xen_pt_msix_init(s
, base_offset
);
1693 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1697 *size
= grp_reg
->grp_size
;
1702 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1703 /* Header Type0 reg group */
1706 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1708 .size_init
= xen_pt_reg_grp_size_init
,
1709 .emu_regs
= xen_pt_emu_reg_header0
,
1711 /* PCI PowerManagement Capability reg group */
1713 .grp_id
= PCI_CAP_ID_PM
,
1714 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1715 .grp_size
= PCI_PM_SIZEOF
,
1716 .size_init
= xen_pt_reg_grp_size_init
,
1717 .emu_regs
= xen_pt_emu_reg_pm
,
1719 /* AGP Capability Structure reg group */
1721 .grp_id
= PCI_CAP_ID_AGP
,
1722 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1724 .size_init
= xen_pt_reg_grp_size_init
,
1726 /* Vital Product Data Capability Structure reg group */
1728 .grp_id
= PCI_CAP_ID_VPD
,
1729 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1731 .size_init
= xen_pt_reg_grp_size_init
,
1732 .emu_regs
= xen_pt_emu_reg_vpd
,
1734 /* Slot Identification reg group */
1736 .grp_id
= PCI_CAP_ID_SLOTID
,
1737 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1739 .size_init
= xen_pt_reg_grp_size_init
,
1741 /* MSI Capability Structure reg group */
1743 .grp_id
= PCI_CAP_ID_MSI
,
1744 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1746 .size_init
= xen_pt_msi_size_init
,
1747 .emu_regs
= xen_pt_emu_reg_msi
,
1749 /* PCI-X Capabilities List Item reg group */
1751 .grp_id
= PCI_CAP_ID_PCIX
,
1752 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1754 .size_init
= xen_pt_reg_grp_size_init
,
1756 /* Vendor Specific Capability Structure reg group */
1758 .grp_id
= PCI_CAP_ID_VNDR
,
1759 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1761 .size_init
= xen_pt_vendor_size_init
,
1762 .emu_regs
= xen_pt_emu_reg_vendor
,
1764 /* SHPC Capability List Item reg group */
1766 .grp_id
= PCI_CAP_ID_SHPC
,
1767 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1769 .size_init
= xen_pt_reg_grp_size_init
,
1771 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1773 .grp_id
= PCI_CAP_ID_SSVID
,
1774 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1776 .size_init
= xen_pt_reg_grp_size_init
,
1778 /* AGP 8x Capability Structure reg group */
1780 .grp_id
= PCI_CAP_ID_AGP3
,
1781 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1783 .size_init
= xen_pt_reg_grp_size_init
,
1785 /* PCI Express Capability Structure reg group */
1787 .grp_id
= PCI_CAP_ID_EXP
,
1788 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1790 .size_init
= xen_pt_pcie_size_init
,
1791 .emu_regs
= xen_pt_emu_reg_pcie
,
1793 /* MSI-X Capability Structure reg group */
1795 .grp_id
= PCI_CAP_ID_MSIX
,
1796 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1798 .size_init
= xen_pt_msix_size_init
,
1799 .emu_regs
= xen_pt_emu_reg_msix
,
1801 /* Intel IGD Opregion group */
1803 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1804 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1806 .size_init
= xen_pt_reg_grp_size_init
,
1807 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1814 /* initialize Capabilities Pointer or Next Pointer register */
1815 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1816 XenPTRegInfo
*reg
, uint32_t real_offset
,
1823 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1827 /* find capability offset */
1829 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1830 if (xen_pt_hide_dev_cap(&s
->real_device
,
1831 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1835 rc
= xen_host_pci_get_byte(&s
->real_device
,
1836 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1838 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1839 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1842 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1843 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1846 /* ignore the 0 hardwired capability, find next one */
1851 /* next capability */
1852 rc
= xen_host_pci_get_byte(&s
->real_device
,
1853 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1869 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1872 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1873 uint8_t pos
= PCI_CAPABILITY_LIST
;
1876 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1879 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1884 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1887 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1892 if (xen_host_pci_get_byte(&s
->real_device
,
1893 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1904 pos
+= PCI_CAP_LIST_NEXT
;
1909 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1910 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1913 XenPTReg
*reg_entry
;
1917 reg_entry
= g_new0(XenPTReg
, 1);
1918 reg_entry
->reg
= reg
;
1921 uint32_t host_mask
, size_mask
;
1922 unsigned int offset
;
1925 /* initialize emulate register */
1926 rc
= reg
->init(s
, reg_entry
->reg
,
1927 reg_grp
->base_offset
+ reg
->offset
, &data
);
1930 error_setg(errp
, "Init emulate register fail");
1933 if (data
== XEN_PT_INVALID_REG
) {
1934 /* free unused BAR register entry */
1938 /* Sync up the data to dev.config */
1939 offset
= reg_grp
->base_offset
+ reg
->offset
;
1940 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1942 switch (reg
->size
) {
1943 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1945 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1947 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1952 /* Serious issues when we cannot read the host values! */
1954 error_setg(errp
, "Cannot read host values");
1957 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1958 * contain the emulated view of the guest - therefore we flip the mask
1959 * to mask out the host values (which dev.config initially has) . */
1960 host_mask
= size_mask
& ~reg
->emu_mask
;
1962 if ((data
& host_mask
) != (val
& host_mask
)) {
1965 /* Mask out host (including past size). */
1966 new_val
= val
& host_mask
;
1967 /* Merge emulated ones (excluding the non-emulated ones). */
1968 new_val
|= data
& host_mask
;
1969 /* Leave intact host and emulated values past the size - even though
1970 * we do not care as we write per reg->size granularity, but for the
1971 * logging below lets have the proper value. */
1972 new_val
|= ((val
| data
)) & ~size_mask
;
1973 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1974 offset
, data
, val
, new_val
);
1979 if (val
& ~size_mask
) {
1980 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1981 " register size (%d)", offset
, val
, reg
->size
);
1985 /* This could be just pci_set_long as we don't modify the bits
1986 * past reg->size, but in case this routine is run in parallel or the
1987 * init value is larger, we do not want to over-write registers. */
1988 switch (reg
->size
) {
1989 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1991 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1993 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1997 /* set register value pointer to the data. */
1998 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
2001 /* list add register entry */
2002 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
2005 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
2010 QLIST_INIT(&s
->reg_grps
);
2012 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
2013 uint32_t reg_grp_offset
= 0;
2014 XenPTRegGroup
*reg_grp_entry
= NULL
;
2016 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2017 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2018 if (xen_pt_hide_dev_cap(&s
->real_device
,
2019 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2023 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2025 if (!reg_grp_offset
) {
2031 * By default we will trap up to 0x40 in the cfg space.
2032 * If an intel device is pass through we need to trap 0xfc,
2033 * therefore the size should be 0xff.
2035 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2036 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2039 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2040 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2041 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2043 reg_grp_entry
->base_offset
= reg_grp_offset
;
2044 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2045 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2046 /* get register group size */
2047 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2049 ®_grp_entry
->size
);
2051 error_setg(&err
, "Failed to initialize %d/%zu, type = 0x%x,"
2052 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2053 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2054 error_propagate(errp
, err
);
2055 xen_pt_config_delete(s
);
2060 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2061 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2063 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2065 /* initialize capability register */
2066 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2067 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, &err
);
2069 error_append_hint(&err
, "Failed to init register %d"
2070 " offsets 0x%x in grp_type = 0x%x (%d/%zu)", j
,
2071 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2072 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2073 error_propagate(errp
, err
);
2074 xen_pt_config_delete(s
);
2083 /* delete all emulate register */
2084 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2086 struct XenPTRegGroup
*reg_group
, *next_grp
;
2087 struct XenPTReg
*reg
, *next_reg
;
2089 /* free MSI/MSI-X info table */
2091 xen_pt_msix_unmap(s
);
2095 /* free all register group entry */
2096 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2097 /* free all register entry */
2098 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2099 QLIST_REMOVE(reg
, entries
);
2103 QLIST_REMOVE(reg_group
, entries
);