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
= PCI_DEVICE(s
);
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
= PCI_DEVICE(s
);
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
:
508 r_size
= d
->io_regions
[index
- 1].size
>> 32;
509 bar_emu_mask
= XEN_PT_BAR_ALLF
;
510 bar_ro_mask
= r_size
? r_size
- 1 : 0;
516 /* modify emulate register */
517 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
518 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
520 /* check whether we need to update the virtual region address or not */
521 switch (s
->bases
[index
].bar_flag
) {
522 case XEN_PT_BAR_FLAG_UPPER
:
523 case XEN_PT_BAR_FLAG_MEM
:
526 case XEN_PT_BAR_FLAG_IO
:
533 /* create value for writing to I/O device register */
534 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
539 /* write Exp ROM BAR */
540 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
541 XenPTReg
*cfg_entry
, uint32_t *val
,
542 uint32_t dev_value
, uint32_t valid_mask
)
544 XenPTRegInfo
*reg
= cfg_entry
->reg
;
545 XenPTRegion
*base
= NULL
;
546 PCIDevice
*d
= PCI_DEVICE(s
);
547 uint32_t writable_mask
= 0;
548 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
550 uint32_t bar_ro_mask
= 0;
551 uint32_t *data
= cfg_entry
->ptr
.word
;
553 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
554 base
= &s
->bases
[PCI_ROM_SLOT
];
555 /* align memory type resource size */
556 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
558 /* set emulate mask and read-only mask */
559 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
561 /* modify emulate register */
562 writable_mask
= ~bar_ro_mask
& valid_mask
;
563 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
565 /* create value for writing to I/O device register */
566 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
571 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
573 uint32_t *value
, uint32_t valid_mask
)
575 *value
= igd_read_opregion(s
);
579 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
580 XenPTReg
*cfg_entry
, uint32_t *value
,
581 uint32_t dev_value
, uint32_t valid_mask
)
583 igd_write_opregion(s
, *value
);
587 /* Header Type0 reg static information table */
588 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
591 .offset
= PCI_VENDOR_ID
,
596 .init
= xen_pt_vendor_reg_init
,
597 .u
.w
.read
= xen_pt_word_reg_read
,
598 .u
.w
.write
= xen_pt_word_reg_write
,
602 .offset
= PCI_DEVICE_ID
,
607 .init
= xen_pt_device_reg_init
,
608 .u
.w
.read
= xen_pt_word_reg_read
,
609 .u
.w
.write
= xen_pt_word_reg_write
,
613 .offset
= PCI_COMMAND
,
618 .init
= xen_pt_common_reg_init
,
619 .u
.w
.read
= xen_pt_word_reg_read
,
620 .u
.w
.write
= xen_pt_cmd_reg_write
,
622 /* Capabilities Pointer reg */
624 .offset
= PCI_CAPABILITY_LIST
,
629 .init
= xen_pt_ptr_reg_init
,
630 .u
.b
.read
= xen_pt_byte_reg_read
,
631 .u
.b
.write
= xen_pt_byte_reg_write
,
634 /* use emulated Cap Ptr value to initialize,
635 * so need to be declared after Cap Ptr reg
638 .offset
= PCI_STATUS
,
645 .init
= xen_pt_status_reg_init
,
646 .u
.w
.read
= xen_pt_word_reg_read
,
647 .u
.w
.write
= xen_pt_word_reg_write
,
649 /* Cache Line Size reg */
651 .offset
= PCI_CACHE_LINE_SIZE
,
656 .init
= xen_pt_common_reg_init
,
657 .u
.b
.read
= xen_pt_byte_reg_read
,
658 .u
.b
.write
= xen_pt_byte_reg_write
,
660 /* Latency Timer reg */
662 .offset
= PCI_LATENCY_TIMER
,
667 .init
= xen_pt_common_reg_init
,
668 .u
.b
.read
= xen_pt_byte_reg_read
,
669 .u
.b
.write
= xen_pt_byte_reg_write
,
671 /* Header Type reg */
673 .offset
= PCI_HEADER_TYPE
,
678 .init
= xen_pt_header_type_reg_init
,
679 .u
.b
.read
= xen_pt_byte_reg_read
,
680 .u
.b
.write
= xen_pt_byte_reg_write
,
682 /* Interrupt Line reg */
684 .offset
= PCI_INTERRUPT_LINE
,
689 .init
= xen_pt_common_reg_init
,
690 .u
.b
.read
= xen_pt_byte_reg_read
,
691 .u
.b
.write
= xen_pt_byte_reg_write
,
693 /* Interrupt Pin reg */
695 .offset
= PCI_INTERRUPT_PIN
,
700 .init
= xen_pt_irqpin_reg_init
,
701 .u
.b
.read
= xen_pt_byte_reg_read
,
702 .u
.b
.write
= xen_pt_byte_reg_write
,
705 /* mask of BAR need to be decided later, depends on IO/MEM type */
707 .offset
= PCI_BASE_ADDRESS_0
,
709 .init_val
= 0x00000000,
710 .init
= xen_pt_bar_reg_init
,
711 .u
.dw
.read
= xen_pt_bar_reg_read
,
712 .u
.dw
.write
= xen_pt_bar_reg_write
,
716 .offset
= PCI_BASE_ADDRESS_1
,
718 .init_val
= 0x00000000,
719 .init
= xen_pt_bar_reg_init
,
720 .u
.dw
.read
= xen_pt_bar_reg_read
,
721 .u
.dw
.write
= xen_pt_bar_reg_write
,
725 .offset
= PCI_BASE_ADDRESS_2
,
727 .init_val
= 0x00000000,
728 .init
= xen_pt_bar_reg_init
,
729 .u
.dw
.read
= xen_pt_bar_reg_read
,
730 .u
.dw
.write
= xen_pt_bar_reg_write
,
734 .offset
= PCI_BASE_ADDRESS_3
,
736 .init_val
= 0x00000000,
737 .init
= xen_pt_bar_reg_init
,
738 .u
.dw
.read
= xen_pt_bar_reg_read
,
739 .u
.dw
.write
= xen_pt_bar_reg_write
,
743 .offset
= PCI_BASE_ADDRESS_4
,
745 .init_val
= 0x00000000,
746 .init
= xen_pt_bar_reg_init
,
747 .u
.dw
.read
= xen_pt_bar_reg_read
,
748 .u
.dw
.write
= xen_pt_bar_reg_write
,
752 .offset
= PCI_BASE_ADDRESS_5
,
754 .init_val
= 0x00000000,
755 .init
= xen_pt_bar_reg_init
,
756 .u
.dw
.read
= xen_pt_bar_reg_read
,
757 .u
.dw
.write
= xen_pt_bar_reg_write
,
759 /* Expansion ROM BAR reg */
761 .offset
= PCI_ROM_ADDRESS
,
763 .init_val
= 0x00000000,
764 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
765 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
766 .init
= xen_pt_bar_reg_init
,
767 .u
.dw
.read
= xen_pt_long_reg_read
,
768 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
776 /*********************************
777 * Vital Product Data Capability
780 /* Vital Product Data Capability Structure reg static information table */
781 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
783 .offset
= PCI_CAP_LIST_NEXT
,
788 .init
= xen_pt_ptr_reg_init
,
789 .u
.b
.read
= xen_pt_byte_reg_read
,
790 .u
.b
.write
= xen_pt_byte_reg_write
,
793 .offset
= PCI_VPD_ADDR
,
797 .init
= xen_pt_common_reg_init
,
798 .u
.w
.read
= xen_pt_word_reg_read
,
799 .u
.w
.write
= xen_pt_word_reg_write
,
807 /**************************************
808 * Vendor Specific Capability
811 /* Vendor Specific Capability Structure reg static information table */
812 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
814 .offset
= PCI_CAP_LIST_NEXT
,
819 .init
= xen_pt_ptr_reg_init
,
820 .u
.b
.read
= xen_pt_byte_reg_read
,
821 .u
.b
.write
= xen_pt_byte_reg_write
,
829 /*****************************
830 * PCI Express Capability
833 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
837 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
840 return flag
& PCI_EXP_FLAGS_VERS
;
843 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
847 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
850 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
853 /* initialize Link Control register */
854 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
855 XenPTRegInfo
*reg
, uint32_t real_offset
,
858 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
859 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
861 /* no need to initialize in case of Root Complex Integrated Endpoint
864 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
865 *data
= XEN_PT_INVALID_REG
;
868 *data
= reg
->init_val
;
871 /* initialize Device Control 2 register */
872 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
873 XenPTRegInfo
*reg
, uint32_t real_offset
,
876 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
878 /* no need to initialize in case of cap_ver 1.x */
880 *data
= XEN_PT_INVALID_REG
;
883 *data
= reg
->init_val
;
886 /* initialize Link Control 2 register */
887 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
888 XenPTRegInfo
*reg
, uint32_t real_offset
,
891 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
892 uint32_t reg_field
= 0;
894 /* no need to initialize in case of cap_ver 1.x */
896 reg_field
= XEN_PT_INVALID_REG
;
898 /* set Supported Link Speed */
901 rc
= xen_host_pci_get_byte(&s
->real_device
,
902 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
907 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
914 /* PCI Express Capability Structure reg static information table */
915 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
916 /* Next Pointer reg */
918 .offset
= PCI_CAP_LIST_NEXT
,
923 .init
= xen_pt_ptr_reg_init
,
924 .u
.b
.read
= xen_pt_byte_reg_read
,
925 .u
.b
.write
= xen_pt_byte_reg_write
,
927 /* Device Capabilities reg */
929 .offset
= PCI_EXP_DEVCAP
,
931 .init_val
= 0x00000000,
932 .ro_mask
= 0xFFFFFFFF,
933 .emu_mask
= 0x10000000,
934 .init
= xen_pt_common_reg_init
,
935 .u
.dw
.read
= xen_pt_long_reg_read
,
936 .u
.dw
.write
= xen_pt_long_reg_write
,
938 /* Device Control reg */
940 .offset
= PCI_EXP_DEVCTL
,
945 .init
= xen_pt_common_reg_init
,
946 .u
.w
.read
= xen_pt_word_reg_read
,
947 .u
.w
.write
= xen_pt_word_reg_write
,
949 /* Device Status reg */
951 .offset
= PCI_EXP_DEVSTA
,
956 .init
= xen_pt_common_reg_init
,
957 .u
.w
.read
= xen_pt_word_reg_read
,
958 .u
.w
.write
= xen_pt_word_reg_write
,
960 /* Link Control reg */
962 .offset
= PCI_EXP_LNKCTL
,
967 .init
= xen_pt_linkctrl_reg_init
,
968 .u
.w
.read
= xen_pt_word_reg_read
,
969 .u
.w
.write
= xen_pt_word_reg_write
,
971 /* Link Status reg */
973 .offset
= PCI_EXP_LNKSTA
,
977 .init
= xen_pt_common_reg_init
,
978 .u
.w
.read
= xen_pt_word_reg_read
,
979 .u
.w
.write
= xen_pt_word_reg_write
,
981 /* Device Control 2 reg */
988 .init
= xen_pt_devctrl2_reg_init
,
989 .u
.w
.read
= xen_pt_word_reg_read
,
990 .u
.w
.write
= xen_pt_word_reg_write
,
992 /* Link Control 2 reg */
999 .init
= xen_pt_linkctrl2_reg_init
,
1000 .u
.w
.read
= xen_pt_word_reg_read
,
1001 .u
.w
.write
= xen_pt_word_reg_write
,
1009 /*********************************
1010 * Power Management Capability
1013 /* Power Management Capability reg static information table */
1014 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1015 /* Next Pointer reg */
1017 .offset
= PCI_CAP_LIST_NEXT
,
1022 .init
= xen_pt_ptr_reg_init
,
1023 .u
.b
.read
= xen_pt_byte_reg_read
,
1024 .u
.b
.write
= xen_pt_byte_reg_write
,
1026 /* Power Management Capabilities reg */
1028 .offset
= PCI_CAP_FLAGS
,
1033 .init
= xen_pt_common_reg_init
,
1034 .u
.w
.read
= xen_pt_word_reg_read
,
1035 .u
.w
.write
= xen_pt_word_reg_write
,
1037 /* PCI Power Management Control/Status reg */
1039 .offset
= PCI_PM_CTRL
,
1044 .rw1c_mask
= 0x8000,
1046 .init
= xen_pt_common_reg_init
,
1047 .u
.w
.read
= xen_pt_word_reg_read
,
1048 .u
.w
.write
= xen_pt_word_reg_write
,
1056 /********************************
1061 #define xen_pt_msi_check_type(offset, flags, what) \
1062 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1063 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1065 /* Message Control register */
1066 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1067 XenPTRegInfo
*reg
, uint32_t real_offset
,
1070 XenPTMSI
*msi
= s
->msi
;
1074 /* use I/O device register's value as initial value */
1075 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1079 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1080 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1081 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1082 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1084 msi
->flags
|= reg_field
;
1085 msi
->ctrl_offset
= real_offset
;
1086 msi
->initialized
= false;
1087 msi
->mapped
= false;
1089 *data
= reg
->init_val
;
1092 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1093 XenPTReg
*cfg_entry
, uint16_t *val
,
1094 uint16_t dev_value
, uint16_t valid_mask
)
1096 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1097 XenPTMSI
*msi
= s
->msi
;
1098 uint16_t writable_mask
= 0;
1099 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1100 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1102 /* Currently no support for multi-vector */
1103 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1104 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1107 /* modify emulate register */
1108 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1109 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1110 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1112 /* create value for writing to I/O device register */
1113 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1116 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1117 /* setup MSI pirq for the first time */
1118 if (!msi
->initialized
) {
1119 /* Init physical one */
1120 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1121 if (xen_pt_msi_setup(s
)) {
1122 /* We do not broadcast the error to the framework code, so
1123 * that MSI errors are contained in MSI emulation code and
1124 * QEMU can go on running.
1125 * Guest MSI would be actually not working.
1127 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1128 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1131 if (xen_pt_msi_update(s
)) {
1132 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1133 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1136 msi
->initialized
= true;
1139 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1140 } else if (msi
->mapped
) {
1141 xen_pt_msi_disable(s
);
1147 /* initialize Message Upper Address register */
1148 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1149 XenPTRegInfo
*reg
, uint32_t real_offset
,
1152 /* no need to initialize in case of 32 bit type */
1153 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1154 *data
= XEN_PT_INVALID_REG
;
1156 *data
= reg
->init_val
;
1161 /* this function will be called twice (for 32 bit and 64 bit type) */
1162 /* initialize Message Data register */
1163 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1164 XenPTRegInfo
*reg
, uint32_t real_offset
,
1167 uint32_t flags
= s
->msi
->flags
;
1168 uint32_t offset
= reg
->offset
;
1170 /* check the offset whether matches the type or not */
1171 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1172 *data
= reg
->init_val
;
1174 *data
= XEN_PT_INVALID_REG
;
1179 /* this function will be called twice (for 32 bit and 64 bit type) */
1180 /* initialize Mask register */
1181 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1182 XenPTRegInfo
*reg
, uint32_t real_offset
,
1185 uint32_t flags
= s
->msi
->flags
;
1187 /* check the offset whether matches the type or not */
1188 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1189 *data
= XEN_PT_INVALID_REG
;
1190 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1191 *data
= reg
->init_val
;
1193 *data
= XEN_PT_INVALID_REG
;
1198 /* this function will be called twice (for 32 bit and 64 bit type) */
1199 /* initialize Pending register */
1200 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1201 XenPTRegInfo
*reg
, uint32_t real_offset
,
1204 uint32_t flags
= s
->msi
->flags
;
1206 /* check the offset whether matches the type or not */
1207 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1208 *data
= XEN_PT_INVALID_REG
;
1209 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1210 *data
= reg
->init_val
;
1212 *data
= XEN_PT_INVALID_REG
;
1217 /* write Message Address register */
1218 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1219 XenPTReg
*cfg_entry
, uint32_t *val
,
1220 uint32_t dev_value
, uint32_t valid_mask
)
1222 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1223 uint32_t writable_mask
= 0;
1224 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1225 uint32_t *data
= cfg_entry
->ptr
.word
;
1227 /* modify emulate register */
1228 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1229 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1230 s
->msi
->addr_lo
= *data
;
1232 /* create value for writing to I/O device register */
1233 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1236 if (*data
!= old_addr
) {
1237 if (s
->msi
->mapped
) {
1238 xen_pt_msi_update(s
);
1244 /* write Message Upper Address register */
1245 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1246 XenPTReg
*cfg_entry
, uint32_t *val
,
1247 uint32_t dev_value
, uint32_t valid_mask
)
1249 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1250 uint32_t writable_mask
= 0;
1251 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1252 uint32_t *data
= cfg_entry
->ptr
.word
;
1254 /* check whether the type is 64 bit or not */
1255 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1257 "Can't write to the upper address without 64 bit support\n");
1261 /* modify emulate register */
1262 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1263 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1264 /* update the msi_info too */
1265 s
->msi
->addr_hi
= *data
;
1267 /* create value for writing to I/O device register */
1268 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1271 if (*data
!= old_addr
) {
1272 if (s
->msi
->mapped
) {
1273 xen_pt_msi_update(s
);
1281 /* this function will be called twice (for 32 bit and 64 bit type) */
1282 /* write Message Data register */
1283 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1284 XenPTReg
*cfg_entry
, uint16_t *val
,
1285 uint16_t dev_value
, uint16_t valid_mask
)
1287 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1288 XenPTMSI
*msi
= s
->msi
;
1289 uint16_t writable_mask
= 0;
1290 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1291 uint32_t offset
= reg
->offset
;
1292 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1294 /* check the offset whether matches the type or not */
1295 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1296 /* exit I/O emulator */
1297 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1301 /* modify emulate register */
1302 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1303 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1304 /* update the msi_info too */
1307 /* create value for writing to I/O device register */
1308 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1311 if (*data
!= old_data
) {
1313 xen_pt_msi_update(s
);
1320 static int xen_pt_mask_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
1321 uint32_t *val
, uint32_t dev_value
,
1322 uint32_t valid_mask
)
1326 rc
= xen_pt_long_reg_write(s
, cfg_entry
, val
, dev_value
, valid_mask
);
1331 s
->msi
->mask
= *val
;
1336 /* MSI Capability Structure reg static information table */
1337 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1338 /* Next Pointer reg */
1340 .offset
= PCI_CAP_LIST_NEXT
,
1345 .init
= xen_pt_ptr_reg_init
,
1346 .u
.b
.read
= xen_pt_byte_reg_read
,
1347 .u
.b
.write
= xen_pt_byte_reg_write
,
1349 /* Message Control reg */
1351 .offset
= PCI_MSI_FLAGS
,
1357 .init
= xen_pt_msgctrl_reg_init
,
1358 .u
.w
.read
= xen_pt_word_reg_read
,
1359 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1361 /* Message Address reg */
1363 .offset
= PCI_MSI_ADDRESS_LO
,
1365 .init_val
= 0x00000000,
1366 .ro_mask
= 0x00000003,
1367 .emu_mask
= 0xFFFFFFFF,
1368 .init
= xen_pt_common_reg_init
,
1369 .u
.dw
.read
= xen_pt_long_reg_read
,
1370 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1372 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1374 .offset
= PCI_MSI_ADDRESS_HI
,
1376 .init_val
= 0x00000000,
1377 .ro_mask
= 0x00000000,
1378 .emu_mask
= 0xFFFFFFFF,
1379 .init
= xen_pt_msgaddr64_reg_init
,
1380 .u
.dw
.read
= xen_pt_long_reg_read
,
1381 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1383 /* Message Data reg (16 bits of data for 32-bit devices) */
1385 .offset
= PCI_MSI_DATA_32
,
1390 .init
= xen_pt_msgdata_reg_init
,
1391 .u
.w
.read
= xen_pt_word_reg_read
,
1392 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1394 /* Message Data reg (16 bits of data for 64-bit devices) */
1396 .offset
= PCI_MSI_DATA_64
,
1401 .init
= xen_pt_msgdata_reg_init
,
1402 .u
.w
.read
= xen_pt_word_reg_read
,
1403 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1405 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1407 .offset
= PCI_MSI_MASK_32
,
1409 .init_val
= 0x00000000,
1410 .ro_mask
= 0xFFFFFFFF,
1411 .emu_mask
= 0xFFFFFFFF,
1412 .init
= xen_pt_mask_reg_init
,
1413 .u
.dw
.read
= xen_pt_long_reg_read
,
1414 .u
.dw
.write
= xen_pt_mask_reg_write
,
1416 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1418 .offset
= PCI_MSI_MASK_64
,
1420 .init_val
= 0x00000000,
1421 .ro_mask
= 0xFFFFFFFF,
1422 .emu_mask
= 0xFFFFFFFF,
1423 .init
= xen_pt_mask_reg_init
,
1424 .u
.dw
.read
= xen_pt_long_reg_read
,
1425 .u
.dw
.write
= xen_pt_mask_reg_write
,
1427 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1429 .offset
= PCI_MSI_MASK_32
+ 4,
1431 .init_val
= 0x00000000,
1432 .ro_mask
= 0xFFFFFFFF,
1433 .emu_mask
= 0x00000000,
1434 .init
= xen_pt_pending_reg_init
,
1435 .u
.dw
.read
= xen_pt_long_reg_read
,
1436 .u
.dw
.write
= xen_pt_long_reg_write
,
1438 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1440 .offset
= PCI_MSI_MASK_64
+ 4,
1442 .init_val
= 0x00000000,
1443 .ro_mask
= 0xFFFFFFFF,
1444 .emu_mask
= 0x00000000,
1445 .init
= xen_pt_pending_reg_init
,
1446 .u
.dw
.read
= xen_pt_long_reg_read
,
1447 .u
.dw
.write
= xen_pt_long_reg_write
,
1455 /**************************************
1459 /* Message Control register for MSI-X */
1460 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1461 XenPTRegInfo
*reg
, uint32_t real_offset
,
1467 /* use I/O device register's value as initial value */
1468 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1472 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1473 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1474 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1475 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1478 s
->msix
->ctrl_offset
= real_offset
;
1480 *data
= reg
->init_val
;
1483 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1484 XenPTReg
*cfg_entry
, uint16_t *val
,
1485 uint16_t dev_value
, uint16_t valid_mask
)
1487 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1488 uint16_t writable_mask
= 0;
1489 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1490 int debug_msix_enabled_old
;
1491 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1493 /* modify emulate register */
1494 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1495 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1497 /* create value for writing to I/O device register */
1498 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1501 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1502 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1503 xen_pt_msix_update(s
);
1504 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1505 xen_pt_msix_disable(s
);
1508 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1510 debug_msix_enabled_old
= s
->msix
->enabled
;
1511 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1512 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1513 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1514 s
->msix
->enabled
? "enable" : "disable");
1520 /* MSI-X Capability Structure reg static information table */
1521 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1522 /* Next Pointer reg */
1524 .offset
= PCI_CAP_LIST_NEXT
,
1529 .init
= xen_pt_ptr_reg_init
,
1530 .u
.b
.read
= xen_pt_byte_reg_read
,
1531 .u
.b
.write
= xen_pt_byte_reg_write
,
1533 /* Message Control reg */
1535 .offset
= PCI_MSI_FLAGS
,
1541 .init
= xen_pt_msixctrl_reg_init
,
1542 .u
.w
.read
= xen_pt_word_reg_read
,
1543 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1550 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1551 /* Intel IGFX OpRegion reg */
1556 .emu_mask
= 0xFFFFFFFF,
1557 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1558 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1565 /****************************
1569 /* capability structure register group size functions */
1571 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1572 const XenPTRegGroupInfo
*grp_reg
,
1573 uint32_t base_offset
, uint8_t *size
)
1575 *size
= grp_reg
->grp_size
;
1578 /* get Vendor Specific Capability Structure register group size */
1579 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1580 const XenPTRegGroupInfo
*grp_reg
,
1581 uint32_t base_offset
, uint8_t *size
)
1583 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1585 /* get PCI Express Capability Structure register group size */
1586 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1587 const XenPTRegGroupInfo
*grp_reg
,
1588 uint32_t base_offset
, uint8_t *size
)
1590 PCIDevice
*d
= PCI_DEVICE(s
);
1591 uint8_t version
= get_capability_version(s
, base_offset
);
1592 uint8_t type
= get_device_type(s
, base_offset
);
1593 uint8_t pcie_size
= 0;
1596 /* calculate size depend on capability version and device/port type */
1597 /* in case of PCI Express Base Specification Rev 1.x */
1599 /* The PCI Express Capabilities, Device Capabilities, and Device
1600 * Status/Control registers are required for all PCI Express devices.
1601 * The Link Capabilities and Link Status/Control are required for all
1602 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1603 * are not required to implement registers other than those listed
1604 * above and terminate the capability structure.
1607 case PCI_EXP_TYPE_ENDPOINT
:
1608 case PCI_EXP_TYPE_LEG_END
:
1611 case PCI_EXP_TYPE_RC_END
:
1615 /* only EndPoint passthrough is supported */
1616 case PCI_EXP_TYPE_ROOT_PORT
:
1617 case PCI_EXP_TYPE_UPSTREAM
:
1618 case PCI_EXP_TYPE_DOWNSTREAM
:
1619 case PCI_EXP_TYPE_PCI_BRIDGE
:
1620 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1621 case PCI_EXP_TYPE_RC_EC
:
1623 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1627 /* in case of PCI Express Base Specification Rev 2.0 */
1628 else if (version
== 2) {
1630 case PCI_EXP_TYPE_ENDPOINT
:
1631 case PCI_EXP_TYPE_LEG_END
:
1632 case PCI_EXP_TYPE_RC_END
:
1633 /* For Functions that do not implement the registers,
1634 * these spaces must be hardwired to 0b.
1638 /* only EndPoint passthrough is supported */
1639 case PCI_EXP_TYPE_ROOT_PORT
:
1640 case PCI_EXP_TYPE_UPSTREAM
:
1641 case PCI_EXP_TYPE_DOWNSTREAM
:
1642 case PCI_EXP_TYPE_PCI_BRIDGE
:
1643 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1644 case PCI_EXP_TYPE_RC_EC
:
1646 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1650 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1657 /* get MSI Capability Structure register group size */
1658 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1659 const XenPTRegGroupInfo
*grp_reg
,
1660 uint32_t base_offset
, uint8_t *size
)
1662 uint16_t msg_ctrl
= 0;
1663 uint8_t msi_size
= 0xa;
1666 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1671 /* check if 64-bit address is capable of per-vector masking */
1672 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1675 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1679 s
->msi
= g_new0(XenPTMSI
, 1);
1680 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1685 /* get MSI-X Capability Structure register group size */
1686 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1687 const XenPTRegGroupInfo
*grp_reg
,
1688 uint32_t base_offset
, uint8_t *size
)
1692 rc
= xen_pt_msix_init(s
, base_offset
);
1695 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1699 *size
= grp_reg
->grp_size
;
1704 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1705 /* Header Type0 reg group */
1708 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1710 .size_init
= xen_pt_reg_grp_size_init
,
1711 .emu_regs
= xen_pt_emu_reg_header0
,
1713 /* PCI PowerManagement Capability reg group */
1715 .grp_id
= PCI_CAP_ID_PM
,
1716 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1717 .grp_size
= PCI_PM_SIZEOF
,
1718 .size_init
= xen_pt_reg_grp_size_init
,
1719 .emu_regs
= xen_pt_emu_reg_pm
,
1721 /* AGP Capability Structure reg group */
1723 .grp_id
= PCI_CAP_ID_AGP
,
1724 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1726 .size_init
= xen_pt_reg_grp_size_init
,
1728 /* Vital Product Data Capability Structure reg group */
1730 .grp_id
= PCI_CAP_ID_VPD
,
1731 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1733 .size_init
= xen_pt_reg_grp_size_init
,
1734 .emu_regs
= xen_pt_emu_reg_vpd
,
1736 /* Slot Identification reg group */
1738 .grp_id
= PCI_CAP_ID_SLOTID
,
1739 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1741 .size_init
= xen_pt_reg_grp_size_init
,
1743 /* MSI Capability Structure reg group */
1745 .grp_id
= PCI_CAP_ID_MSI
,
1746 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1748 .size_init
= xen_pt_msi_size_init
,
1749 .emu_regs
= xen_pt_emu_reg_msi
,
1751 /* PCI-X Capabilities List Item reg group */
1753 .grp_id
= PCI_CAP_ID_PCIX
,
1754 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1756 .size_init
= xen_pt_reg_grp_size_init
,
1758 /* Vendor Specific Capability Structure reg group */
1760 .grp_id
= PCI_CAP_ID_VNDR
,
1761 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1763 .size_init
= xen_pt_vendor_size_init
,
1764 .emu_regs
= xen_pt_emu_reg_vendor
,
1766 /* SHPC Capability List Item reg group */
1768 .grp_id
= PCI_CAP_ID_SHPC
,
1769 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1771 .size_init
= xen_pt_reg_grp_size_init
,
1773 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1775 .grp_id
= PCI_CAP_ID_SSVID
,
1776 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1778 .size_init
= xen_pt_reg_grp_size_init
,
1780 /* AGP 8x Capability Structure reg group */
1782 .grp_id
= PCI_CAP_ID_AGP3
,
1783 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1785 .size_init
= xen_pt_reg_grp_size_init
,
1787 /* PCI Express Capability Structure reg group */
1789 .grp_id
= PCI_CAP_ID_EXP
,
1790 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1792 .size_init
= xen_pt_pcie_size_init
,
1793 .emu_regs
= xen_pt_emu_reg_pcie
,
1795 /* MSI-X Capability Structure reg group */
1797 .grp_id
= PCI_CAP_ID_MSIX
,
1798 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1800 .size_init
= xen_pt_msix_size_init
,
1801 .emu_regs
= xen_pt_emu_reg_msix
,
1803 /* Intel IGD Opregion group */
1805 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1806 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1808 .size_init
= xen_pt_reg_grp_size_init
,
1809 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1816 /* initialize Capabilities Pointer or Next Pointer register */
1817 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1818 XenPTRegInfo
*reg
, uint32_t real_offset
,
1825 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1829 /* find capability offset */
1831 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1832 if (xen_pt_hide_dev_cap(&s
->real_device
,
1833 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1837 rc
= xen_host_pci_get_byte(&s
->real_device
,
1838 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1840 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1841 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1844 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1845 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1848 /* ignore the 0 hardwired capability, find next one */
1853 /* next capability */
1854 rc
= xen_host_pci_get_byte(&s
->real_device
,
1855 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1871 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1874 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1875 uint8_t pos
= PCI_CAPABILITY_LIST
;
1878 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1881 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1886 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1889 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1894 if (xen_host_pci_get_byte(&s
->real_device
,
1895 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1906 pos
+= PCI_CAP_LIST_NEXT
;
1911 static void xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1912 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
,
1915 XenPTReg
*reg_entry
;
1919 reg_entry
= g_new0(XenPTReg
, 1);
1920 reg_entry
->reg
= reg
;
1923 uint32_t host_mask
, size_mask
;
1924 unsigned int offset
;
1927 /* initialize emulate register */
1928 rc
= reg
->init(s
, reg_entry
->reg
,
1929 reg_grp
->base_offset
+ reg
->offset
, &data
);
1932 error_setg(errp
, "Init emulate register fail");
1935 if (data
== XEN_PT_INVALID_REG
) {
1936 /* free unused BAR register entry */
1940 /* Sync up the data to dev.config */
1941 offset
= reg_grp
->base_offset
+ reg
->offset
;
1942 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1944 switch (reg
->size
) {
1945 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1947 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1949 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1954 /* Serious issues when we cannot read the host values! */
1956 error_setg(errp
, "Cannot read host values");
1959 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1960 * contain the emulated view of the guest - therefore we flip the mask
1961 * to mask out the host values (which dev.config initially has) . */
1962 host_mask
= size_mask
& ~reg
->emu_mask
;
1964 if ((data
& host_mask
) != (val
& host_mask
)) {
1967 /* Mask out host (including past size). */
1968 new_val
= val
& host_mask
;
1969 /* Merge emulated ones (excluding the non-emulated ones). */
1970 new_val
|= data
& host_mask
;
1971 /* Leave intact host and emulated values past the size - even though
1972 * we do not care as we write per reg->size granularity, but for the
1973 * logging below lets have the proper value. */
1974 new_val
|= ((val
| data
)) & ~size_mask
;
1975 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1976 offset
, data
, val
, new_val
);
1981 if (val
& ~size_mask
) {
1982 error_setg(errp
, "Offset 0x%04x:0x%04x expands past"
1983 " register size (%d)", offset
, val
, reg
->size
);
1987 /* This could be just pci_set_long as we don't modify the bits
1988 * past reg->size, but in case this routine is run in parallel or the
1989 * init value is larger, we do not want to over-write registers. */
1990 switch (reg
->size
) {
1991 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1993 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1995 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1999 /* set register value pointer to the data. */
2000 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
2003 /* list add register entry */
2004 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
2007 void xen_pt_config_init(XenPCIPassthroughState
*s
, Error
**errp
)
2012 QLIST_INIT(&s
->reg_grps
);
2014 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
2015 uint32_t reg_grp_offset
= 0;
2016 XenPTRegGroup
*reg_grp_entry
= NULL
;
2018 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2019 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2020 if (xen_pt_hide_dev_cap(&s
->real_device
,
2021 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2025 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2027 if (!reg_grp_offset
) {
2033 * By default we will trap up to 0x40 in the cfg space.
2034 * If an intel device is pass through we need to trap 0xfc,
2035 * therefore the size should be 0xff.
2037 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2038 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2041 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2042 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2043 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2045 reg_grp_entry
->base_offset
= reg_grp_offset
;
2046 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2047 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2048 /* get register group size */
2049 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2051 ®_grp_entry
->size
);
2053 error_setg(&err
, "Failed to initialize %d/%zu, type = 0x%x,"
2054 " rc: %d", i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2055 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2056 error_propagate(errp
, err
);
2057 xen_pt_config_delete(s
);
2062 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2063 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2065 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2067 /* initialize capability register */
2068 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2069 xen_pt_config_reg_init(s
, reg_grp_entry
, regs
, &err
);
2071 error_append_hint(&err
, "Failed to init register %d"
2072 " offsets 0x%x in grp_type = 0x%x (%d/%zu)", j
,
2073 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2074 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
));
2075 error_propagate(errp
, err
);
2076 xen_pt_config_delete(s
);
2085 /* delete all emulate register */
2086 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2088 struct XenPTRegGroup
*reg_group
, *next_grp
;
2089 struct XenPTReg
*reg
, *next_reg
;
2091 /* free MSI/MSI-X info table */
2093 xen_pt_msix_unmap(s
);
2097 /* free all register group entry */
2098 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2099 /* free all register entry */
2100 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2101 QLIST_REMOVE(reg
, entries
);
2105 QLIST_REMOVE(reg_group
, entries
);