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/timer.h"
16 #include "hw/xen/xen_backend.h"
19 #define XEN_PT_MERGE_VALUE(value, data, val_mask) \
20 (((value) & (val_mask)) | ((data) & ~(val_mask)))
22 #define XEN_PT_INVALID_REG 0xFFFFFFFF /* invalid register value */
26 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
27 uint32_t real_offset
, uint32_t *data
);
32 /* A return value of 1 means the capability should NOT be exposed to guest. */
33 static int xen_pt_hide_dev_cap(const XenHostPCIDevice
*d
, uint8_t grp_id
)
37 /* The PCI Express Capability Structure of the VF of Intel 82599 10GbE
38 * Controller looks trivial, e.g., the PCI Express Capabilities
39 * Register is 0. We should not try to expose it to guest.
41 * The datasheet is available at
42 * http://download.intel.com/design/network/datashts/82599_datasheet.pdf
44 * See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the
45 * PCI Express Capability Structure of the VF of Intel 82599 10GbE
46 * Controller looks trivial, e.g., the PCI Express Capabilities
47 * Register is 0, so the Capability Version is 0 and
48 * xen_pt_pcie_size_init() would fail.
50 if (d
->vendor_id
== PCI_VENDOR_ID_INTEL
&&
51 d
->device_id
== PCI_DEVICE_ID_INTEL_82599_SFP_VF
) {
59 /* find emulate register group entry */
60 XenPTRegGroup
*xen_pt_find_reg_grp(XenPCIPassthroughState
*s
, uint32_t address
)
62 XenPTRegGroup
*entry
= NULL
;
64 /* find register group entry */
65 QLIST_FOREACH(entry
, &s
->reg_grps
, entries
) {
67 if ((entry
->base_offset
<= address
)
68 && ((entry
->base_offset
+ entry
->size
) > address
)) {
73 /* group entry not found */
77 /* find emulate register entry */
78 XenPTReg
*xen_pt_find_reg(XenPTRegGroup
*reg_grp
, uint32_t address
)
80 XenPTReg
*reg_entry
= NULL
;
81 XenPTRegInfo
*reg
= NULL
;
82 uint32_t real_offset
= 0;
84 /* find register entry */
85 QLIST_FOREACH(reg_entry
, ®_grp
->reg_tbl_list
, entries
) {
87 real_offset
= reg_grp
->base_offset
+ reg
->offset
;
89 if ((real_offset
<= address
)
90 && ((real_offset
+ reg
->size
) > address
)) {
98 static uint32_t get_throughable_mask(const XenPCIPassthroughState
*s
,
99 XenPTRegInfo
*reg
, uint32_t valid_mask
)
101 uint32_t throughable_mask
= ~(reg
->emu_mask
| reg
->ro_mask
);
103 if (!s
->permissive
) {
104 throughable_mask
&= ~reg
->res_mask
;
107 return throughable_mask
& valid_mask
;
111 * general register functions
114 /* register initialization function */
116 static int xen_pt_common_reg_init(XenPCIPassthroughState
*s
,
117 XenPTRegInfo
*reg
, uint32_t real_offset
,
120 *data
= reg
->init_val
;
124 /* Read register functions */
126 static int xen_pt_byte_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
127 uint8_t *value
, uint8_t valid_mask
)
129 XenPTRegInfo
*reg
= cfg_entry
->reg
;
130 uint8_t valid_emu_mask
= 0;
131 uint8_t *data
= cfg_entry
->ptr
.byte
;
133 /* emulate byte register */
134 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
135 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
139 static int xen_pt_word_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
140 uint16_t *value
, uint16_t valid_mask
)
142 XenPTRegInfo
*reg
= cfg_entry
->reg
;
143 uint16_t valid_emu_mask
= 0;
144 uint16_t *data
= cfg_entry
->ptr
.half_word
;
146 /* emulate word register */
147 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
148 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
152 static int xen_pt_long_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
153 uint32_t *value
, uint32_t valid_mask
)
155 XenPTRegInfo
*reg
= cfg_entry
->reg
;
156 uint32_t valid_emu_mask
= 0;
157 uint32_t *data
= cfg_entry
->ptr
.word
;
159 /* emulate long register */
160 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
161 *value
= XEN_PT_MERGE_VALUE(*value
, *data
, ~valid_emu_mask
);
166 /* Write register functions */
168 static int xen_pt_byte_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
169 uint8_t *val
, uint8_t dev_value
,
172 XenPTRegInfo
*reg
= cfg_entry
->reg
;
173 uint8_t writable_mask
= 0;
174 uint8_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
175 uint8_t *data
= cfg_entry
->ptr
.byte
;
177 /* modify emulate register */
178 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
179 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
181 /* create value for writing to I/O device register */
182 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
186 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
187 uint16_t *val
, uint16_t dev_value
,
190 XenPTRegInfo
*reg
= cfg_entry
->reg
;
191 uint16_t writable_mask
= 0;
192 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
193 uint16_t *data
= cfg_entry
->ptr
.half_word
;
195 /* modify emulate register */
196 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
197 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
199 /* create value for writing to I/O device register */
200 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
204 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
205 uint32_t *val
, uint32_t dev_value
,
208 XenPTRegInfo
*reg
= cfg_entry
->reg
;
209 uint32_t writable_mask
= 0;
210 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
211 uint32_t *data
= cfg_entry
->ptr
.word
;
213 /* modify emulate register */
214 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
215 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
217 /* create value for writing to I/O device register */
218 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
224 /* XenPTRegInfo declaration
225 * - only for emulated register (either a part or whole bit).
226 * - for passthrough register that need special behavior (like interacting with
227 * other component), set emu_mask to all 0 and specify r/w func properly.
228 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
231 /********************
235 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
236 XenPTRegInfo
*reg
, uint32_t real_offset
,
239 *data
= s
->real_device
.vendor_id
;
242 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
243 XenPTRegInfo
*reg
, uint32_t real_offset
,
246 *data
= s
->real_device
.device_id
;
249 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
250 XenPTRegInfo
*reg
, uint32_t real_offset
,
253 XenPTRegGroup
*reg_grp_entry
= NULL
;
254 XenPTReg
*reg_entry
= NULL
;
255 uint32_t reg_field
= 0;
257 /* find Header register group */
258 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
260 /* find Capabilities Pointer register */
261 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
263 /* check Capabilities Pointer register */
264 if (*reg_entry
->ptr
.half_word
) {
265 reg_field
|= PCI_STATUS_CAP_LIST
;
267 reg_field
&= ~PCI_STATUS_CAP_LIST
;
270 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
271 " for Capabilities Pointer register."
272 " (%s)\n", __func__
);
276 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
277 " for Header. (%s)\n", __func__
);
284 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
285 XenPTRegInfo
*reg
, uint32_t real_offset
,
288 /* read PCI_HEADER_TYPE */
289 *data
= reg
->init_val
| 0x80;
293 /* initialize Interrupt Pin register */
294 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
295 XenPTRegInfo
*reg
, uint32_t real_offset
,
298 *data
= xen_pt_pci_read_intx(s
);
302 /* Command register */
303 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
304 uint16_t *val
, uint16_t dev_value
,
307 XenPTRegInfo
*reg
= cfg_entry
->reg
;
308 uint16_t writable_mask
= 0;
309 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
310 uint16_t *data
= cfg_entry
->ptr
.half_word
;
312 /* modify emulate register */
313 writable_mask
= ~reg
->ro_mask
& valid_mask
;
314 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
316 /* create value for writing to I/O device register */
317 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
318 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
320 if (s
->machine_irq
) {
321 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
325 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
331 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
332 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
333 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
334 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
336 static bool is_64bit_bar(PCIIORegion
*r
)
338 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
341 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
343 if (is_64bit_bar(r
)) {
345 size64
= (r
+ 1)->size
;
353 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
356 PCIDevice
*d
= &s
->dev
;
357 XenPTRegion
*region
= NULL
;
360 /* check 64bit BAR */
361 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
362 int type
= s
->real_device
.io_regions
[index
- 1].type
;
364 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
365 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
366 region
= &s
->bases
[index
- 1];
367 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
368 return XEN_PT_BAR_FLAG_UPPER
;
373 /* check unused BAR */
374 r
= &d
->io_regions
[index
];
375 if (!xen_pt_get_bar_size(r
)) {
376 return XEN_PT_BAR_FLAG_UNUSED
;
380 if (index
== PCI_ROM_SLOT
) {
381 return XEN_PT_BAR_FLAG_MEM
;
384 /* check BAR I/O indicator */
385 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
386 return XEN_PT_BAR_FLAG_IO
;
388 return XEN_PT_BAR_FLAG_MEM
;
392 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
394 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
395 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
397 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
401 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
402 uint32_t real_offset
, uint32_t *data
)
404 uint32_t reg_field
= 0;
407 index
= xen_pt_bar_offset_to_index(reg
->offset
);
408 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
409 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
414 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
415 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
416 reg_field
= XEN_PT_INVALID_REG
;
422 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
423 uint32_t *value
, uint32_t valid_mask
)
425 XenPTRegInfo
*reg
= cfg_entry
->reg
;
426 uint32_t valid_emu_mask
= 0;
427 uint32_t bar_emu_mask
= 0;
431 index
= xen_pt_bar_offset_to_index(reg
->offset
);
432 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
433 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
437 /* use fixed-up value from kernel sysfs */
438 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
440 /* set emulate mask depend on BAR flag */
441 switch (s
->bases
[index
].bar_flag
) {
442 case XEN_PT_BAR_FLAG_MEM
:
443 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
445 case XEN_PT_BAR_FLAG_IO
:
446 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
448 case XEN_PT_BAR_FLAG_UPPER
:
449 bar_emu_mask
= XEN_PT_BAR_ALLF
;
456 valid_emu_mask
= bar_emu_mask
& valid_mask
;
457 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
461 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
462 uint32_t *val
, uint32_t dev_value
,
465 XenPTRegInfo
*reg
= cfg_entry
->reg
;
466 XenPTRegion
*base
= NULL
;
467 PCIDevice
*d
= &s
->dev
;
468 const PCIIORegion
*r
;
469 uint32_t writable_mask
= 0;
470 uint32_t bar_emu_mask
= 0;
471 uint32_t bar_ro_mask
= 0;
474 uint32_t *data
= cfg_entry
->ptr
.word
;
476 index
= xen_pt_bar_offset_to_index(reg
->offset
);
477 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
478 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
482 r
= &d
->io_regions
[index
];
483 base
= &s
->bases
[index
];
484 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
486 /* set emulate mask and read-only mask values depend on the BAR flag */
487 switch (s
->bases
[index
].bar_flag
) {
488 case XEN_PT_BAR_FLAG_MEM
:
489 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
491 /* low 32 bits mask for 64 bit bars */
492 bar_ro_mask
= XEN_PT_BAR_ALLF
;
494 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
497 case XEN_PT_BAR_FLAG_IO
:
498 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
499 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
501 case XEN_PT_BAR_FLAG_UPPER
:
502 bar_emu_mask
= XEN_PT_BAR_ALLF
;
503 bar_ro_mask
= r_size
? r_size
- 1 : 0;
509 /* modify emulate register */
510 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
511 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
513 /* check whether we need to update the virtual region address or not */
514 switch (s
->bases
[index
].bar_flag
) {
515 case XEN_PT_BAR_FLAG_UPPER
:
516 case XEN_PT_BAR_FLAG_MEM
:
519 case XEN_PT_BAR_FLAG_IO
:
526 /* create value for writing to I/O device register */
527 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
532 /* write Exp ROM BAR */
533 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
534 XenPTReg
*cfg_entry
, uint32_t *val
,
535 uint32_t dev_value
, uint32_t valid_mask
)
537 XenPTRegInfo
*reg
= cfg_entry
->reg
;
538 XenPTRegion
*base
= NULL
;
539 PCIDevice
*d
= (PCIDevice
*)&s
->dev
;
540 uint32_t writable_mask
= 0;
541 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
543 uint32_t bar_ro_mask
= 0;
544 uint32_t *data
= cfg_entry
->ptr
.word
;
546 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
547 base
= &s
->bases
[PCI_ROM_SLOT
];
548 /* align memory type resource size */
549 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
551 /* set emulate mask and read-only mask */
552 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
554 /* modify emulate register */
555 writable_mask
= ~bar_ro_mask
& valid_mask
;
556 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
558 /* create value for writing to I/O device register */
559 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
564 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
566 uint32_t *value
, uint32_t valid_mask
)
568 *value
= igd_read_opregion(s
);
572 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
573 XenPTReg
*cfg_entry
, uint32_t *value
,
574 uint32_t dev_value
, uint32_t valid_mask
)
576 igd_write_opregion(s
, *value
);
580 /* Header Type0 reg static information table */
581 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
584 .offset
= PCI_VENDOR_ID
,
589 .init
= xen_pt_vendor_reg_init
,
590 .u
.w
.read
= xen_pt_word_reg_read
,
591 .u
.w
.write
= xen_pt_word_reg_write
,
595 .offset
= PCI_DEVICE_ID
,
600 .init
= xen_pt_device_reg_init
,
601 .u
.w
.read
= xen_pt_word_reg_read
,
602 .u
.w
.write
= xen_pt_word_reg_write
,
606 .offset
= PCI_COMMAND
,
611 .init
= xen_pt_common_reg_init
,
612 .u
.w
.read
= xen_pt_word_reg_read
,
613 .u
.w
.write
= xen_pt_cmd_reg_write
,
615 /* Capabilities Pointer reg */
617 .offset
= PCI_CAPABILITY_LIST
,
622 .init
= xen_pt_ptr_reg_init
,
623 .u
.b
.read
= xen_pt_byte_reg_read
,
624 .u
.b
.write
= xen_pt_byte_reg_write
,
627 /* use emulated Cap Ptr value to initialize,
628 * so need to be declared after Cap Ptr reg
631 .offset
= PCI_STATUS
,
637 .init
= xen_pt_status_reg_init
,
638 .u
.w
.read
= xen_pt_word_reg_read
,
639 .u
.w
.write
= xen_pt_word_reg_write
,
641 /* Cache Line Size reg */
643 .offset
= PCI_CACHE_LINE_SIZE
,
648 .init
= xen_pt_common_reg_init
,
649 .u
.b
.read
= xen_pt_byte_reg_read
,
650 .u
.b
.write
= xen_pt_byte_reg_write
,
652 /* Latency Timer reg */
654 .offset
= PCI_LATENCY_TIMER
,
659 .init
= xen_pt_common_reg_init
,
660 .u
.b
.read
= xen_pt_byte_reg_read
,
661 .u
.b
.write
= xen_pt_byte_reg_write
,
663 /* Header Type reg */
665 .offset
= PCI_HEADER_TYPE
,
670 .init
= xen_pt_header_type_reg_init
,
671 .u
.b
.read
= xen_pt_byte_reg_read
,
672 .u
.b
.write
= xen_pt_byte_reg_write
,
674 /* Interrupt Line reg */
676 .offset
= PCI_INTERRUPT_LINE
,
681 .init
= xen_pt_common_reg_init
,
682 .u
.b
.read
= xen_pt_byte_reg_read
,
683 .u
.b
.write
= xen_pt_byte_reg_write
,
685 /* Interrupt Pin reg */
687 .offset
= PCI_INTERRUPT_PIN
,
692 .init
= xen_pt_irqpin_reg_init
,
693 .u
.b
.read
= xen_pt_byte_reg_read
,
694 .u
.b
.write
= xen_pt_byte_reg_write
,
697 /* mask of BAR need to be decided later, depends on IO/MEM type */
699 .offset
= PCI_BASE_ADDRESS_0
,
701 .init_val
= 0x00000000,
702 .init
= xen_pt_bar_reg_init
,
703 .u
.dw
.read
= xen_pt_bar_reg_read
,
704 .u
.dw
.write
= xen_pt_bar_reg_write
,
708 .offset
= PCI_BASE_ADDRESS_1
,
710 .init_val
= 0x00000000,
711 .init
= xen_pt_bar_reg_init
,
712 .u
.dw
.read
= xen_pt_bar_reg_read
,
713 .u
.dw
.write
= xen_pt_bar_reg_write
,
717 .offset
= PCI_BASE_ADDRESS_2
,
719 .init_val
= 0x00000000,
720 .init
= xen_pt_bar_reg_init
,
721 .u
.dw
.read
= xen_pt_bar_reg_read
,
722 .u
.dw
.write
= xen_pt_bar_reg_write
,
726 .offset
= PCI_BASE_ADDRESS_3
,
728 .init_val
= 0x00000000,
729 .init
= xen_pt_bar_reg_init
,
730 .u
.dw
.read
= xen_pt_bar_reg_read
,
731 .u
.dw
.write
= xen_pt_bar_reg_write
,
735 .offset
= PCI_BASE_ADDRESS_4
,
737 .init_val
= 0x00000000,
738 .init
= xen_pt_bar_reg_init
,
739 .u
.dw
.read
= xen_pt_bar_reg_read
,
740 .u
.dw
.write
= xen_pt_bar_reg_write
,
744 .offset
= PCI_BASE_ADDRESS_5
,
746 .init_val
= 0x00000000,
747 .init
= xen_pt_bar_reg_init
,
748 .u
.dw
.read
= xen_pt_bar_reg_read
,
749 .u
.dw
.write
= xen_pt_bar_reg_write
,
751 /* Expansion ROM BAR reg */
753 .offset
= PCI_ROM_ADDRESS
,
755 .init_val
= 0x00000000,
756 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
757 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
758 .init
= xen_pt_bar_reg_init
,
759 .u
.dw
.read
= xen_pt_long_reg_read
,
760 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
768 /*********************************
769 * Vital Product Data Capability
772 /* Vital Product Data Capability Structure reg static information table */
773 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
775 .offset
= PCI_CAP_LIST_NEXT
,
780 .init
= xen_pt_ptr_reg_init
,
781 .u
.b
.read
= xen_pt_byte_reg_read
,
782 .u
.b
.write
= xen_pt_byte_reg_write
,
785 .offset
= PCI_VPD_ADDR
,
789 .init
= xen_pt_common_reg_init
,
790 .u
.w
.read
= xen_pt_word_reg_read
,
791 .u
.w
.write
= xen_pt_word_reg_write
,
799 /**************************************
800 * Vendor Specific Capability
803 /* Vendor Specific Capability Structure reg static information table */
804 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
806 .offset
= PCI_CAP_LIST_NEXT
,
811 .init
= xen_pt_ptr_reg_init
,
812 .u
.b
.read
= xen_pt_byte_reg_read
,
813 .u
.b
.write
= xen_pt_byte_reg_write
,
821 /*****************************
822 * PCI Express Capability
825 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
829 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
832 return flag
& PCI_EXP_FLAGS_VERS
;
835 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
839 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
842 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
845 /* initialize Link Control register */
846 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
847 XenPTRegInfo
*reg
, uint32_t real_offset
,
850 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
851 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
853 /* no need to initialize in case of Root Complex Integrated Endpoint
856 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
857 *data
= XEN_PT_INVALID_REG
;
860 *data
= reg
->init_val
;
863 /* initialize Device Control 2 register */
864 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
865 XenPTRegInfo
*reg
, uint32_t real_offset
,
868 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
870 /* no need to initialize in case of cap_ver 1.x */
872 *data
= XEN_PT_INVALID_REG
;
875 *data
= reg
->init_val
;
878 /* initialize Link Control 2 register */
879 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
880 XenPTRegInfo
*reg
, uint32_t real_offset
,
883 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
884 uint32_t reg_field
= 0;
886 /* no need to initialize in case of cap_ver 1.x */
888 reg_field
= XEN_PT_INVALID_REG
;
890 /* set Supported Link Speed */
893 rc
= xen_host_pci_get_byte(&s
->real_device
,
894 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
899 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
906 /* PCI Express Capability Structure reg static information table */
907 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
908 /* Next Pointer reg */
910 .offset
= PCI_CAP_LIST_NEXT
,
915 .init
= xen_pt_ptr_reg_init
,
916 .u
.b
.read
= xen_pt_byte_reg_read
,
917 .u
.b
.write
= xen_pt_byte_reg_write
,
919 /* Device Capabilities reg */
921 .offset
= PCI_EXP_DEVCAP
,
923 .init_val
= 0x00000000,
924 .ro_mask
= 0xFFFFFFFF,
925 .emu_mask
= 0x10000000,
926 .init
= xen_pt_common_reg_init
,
927 .u
.dw
.read
= xen_pt_long_reg_read
,
928 .u
.dw
.write
= xen_pt_long_reg_write
,
930 /* Device Control reg */
932 .offset
= PCI_EXP_DEVCTL
,
937 .init
= xen_pt_common_reg_init
,
938 .u
.w
.read
= xen_pt_word_reg_read
,
939 .u
.w
.write
= xen_pt_word_reg_write
,
941 /* Device Status reg */
943 .offset
= PCI_EXP_DEVSTA
,
947 .init
= xen_pt_common_reg_init
,
948 .u
.w
.read
= xen_pt_word_reg_read
,
949 .u
.w
.write
= xen_pt_word_reg_write
,
951 /* Link Control reg */
953 .offset
= PCI_EXP_LNKCTL
,
958 .init
= xen_pt_linkctrl_reg_init
,
959 .u
.w
.read
= xen_pt_word_reg_read
,
960 .u
.w
.write
= xen_pt_word_reg_write
,
962 /* Link Status reg */
964 .offset
= PCI_EXP_LNKSTA
,
967 .init
= xen_pt_common_reg_init
,
968 .u
.w
.read
= xen_pt_word_reg_read
,
969 .u
.w
.write
= xen_pt_word_reg_write
,
971 /* Device Control 2 reg */
978 .init
= xen_pt_devctrl2_reg_init
,
979 .u
.w
.read
= xen_pt_word_reg_read
,
980 .u
.w
.write
= xen_pt_word_reg_write
,
982 /* Link Control 2 reg */
989 .init
= xen_pt_linkctrl2_reg_init
,
990 .u
.w
.read
= xen_pt_word_reg_read
,
991 .u
.w
.write
= xen_pt_word_reg_write
,
999 /*********************************
1000 * Power Management Capability
1003 /* write Power Management Control/Status register */
1004 static int xen_pt_pmcsr_reg_write(XenPCIPassthroughState
*s
,
1005 XenPTReg
*cfg_entry
, uint16_t *val
,
1006 uint16_t dev_value
, uint16_t valid_mask
)
1008 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1009 uint16_t writable_mask
= 0;
1010 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1011 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1013 /* modify emulate register */
1014 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1015 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1017 /* create value for writing to I/O device register */
1018 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~PCI_PM_CTRL_PME_STATUS
,
1024 /* Power Management Capability reg static information table */
1025 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1026 /* Next Pointer reg */
1028 .offset
= PCI_CAP_LIST_NEXT
,
1033 .init
= xen_pt_ptr_reg_init
,
1034 .u
.b
.read
= xen_pt_byte_reg_read
,
1035 .u
.b
.write
= xen_pt_byte_reg_write
,
1037 /* Power Management Capabilities reg */
1039 .offset
= PCI_CAP_FLAGS
,
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
,
1048 /* PCI Power Management Control/Status reg */
1050 .offset
= PCI_PM_CTRL
,
1056 .init
= xen_pt_common_reg_init
,
1057 .u
.w
.read
= xen_pt_word_reg_read
,
1058 .u
.w
.write
= xen_pt_pmcsr_reg_write
,
1066 /********************************
1071 #define xen_pt_msi_check_type(offset, flags, what) \
1072 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1073 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1075 /* Message Control register */
1076 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1077 XenPTRegInfo
*reg
, uint32_t real_offset
,
1080 XenPTMSI
*msi
= s
->msi
;
1084 /* use I/O device register's value as initial value */
1085 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1089 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1090 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1091 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1092 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1094 msi
->flags
|= reg_field
;
1095 msi
->ctrl_offset
= real_offset
;
1096 msi
->initialized
= false;
1097 msi
->mapped
= false;
1099 *data
= reg
->init_val
;
1102 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1103 XenPTReg
*cfg_entry
, uint16_t *val
,
1104 uint16_t dev_value
, uint16_t valid_mask
)
1106 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1107 XenPTMSI
*msi
= s
->msi
;
1108 uint16_t writable_mask
= 0;
1109 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1110 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1112 /* Currently no support for multi-vector */
1113 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1114 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1117 /* modify emulate register */
1118 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1119 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1120 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1122 /* create value for writing to I/O device register */
1123 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1126 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1127 /* setup MSI pirq for the first time */
1128 if (!msi
->initialized
) {
1129 /* Init physical one */
1130 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1131 if (xen_pt_msi_setup(s
)) {
1132 /* We do not broadcast the error to the framework code, so
1133 * that MSI errors are contained in MSI emulation code and
1134 * QEMU can go on running.
1135 * Guest MSI would be actually not working.
1137 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1138 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1141 if (xen_pt_msi_update(s
)) {
1142 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1143 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1146 msi
->initialized
= true;
1149 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1150 } else if (msi
->mapped
) {
1151 xen_pt_msi_disable(s
);
1157 /* initialize Message Upper Address register */
1158 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1159 XenPTRegInfo
*reg
, uint32_t real_offset
,
1162 /* no need to initialize in case of 32 bit type */
1163 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1164 *data
= XEN_PT_INVALID_REG
;
1166 *data
= reg
->init_val
;
1171 /* this function will be called twice (for 32 bit and 64 bit type) */
1172 /* initialize Message Data register */
1173 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1174 XenPTRegInfo
*reg
, uint32_t real_offset
,
1177 uint32_t flags
= s
->msi
->flags
;
1178 uint32_t offset
= reg
->offset
;
1180 /* check the offset whether matches the type or not */
1181 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1182 *data
= reg
->init_val
;
1184 *data
= XEN_PT_INVALID_REG
;
1189 /* this function will be called twice (for 32 bit and 64 bit type) */
1190 /* initialize Mask register */
1191 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1192 XenPTRegInfo
*reg
, uint32_t real_offset
,
1195 uint32_t flags
= s
->msi
->flags
;
1197 /* check the offset whether matches the type or not */
1198 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1199 *data
= XEN_PT_INVALID_REG
;
1200 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1201 *data
= reg
->init_val
;
1203 *data
= XEN_PT_INVALID_REG
;
1208 /* this function will be called twice (for 32 bit and 64 bit type) */
1209 /* initialize Pending register */
1210 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1211 XenPTRegInfo
*reg
, uint32_t real_offset
,
1214 uint32_t flags
= s
->msi
->flags
;
1216 /* check the offset whether matches the type or not */
1217 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1218 *data
= XEN_PT_INVALID_REG
;
1219 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1220 *data
= reg
->init_val
;
1222 *data
= XEN_PT_INVALID_REG
;
1227 /* write Message Address register */
1228 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1229 XenPTReg
*cfg_entry
, uint32_t *val
,
1230 uint32_t dev_value
, uint32_t valid_mask
)
1232 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1233 uint32_t writable_mask
= 0;
1234 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1235 uint32_t *data
= cfg_entry
->ptr
.word
;
1237 /* modify emulate register */
1238 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1239 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1240 s
->msi
->addr_lo
= *data
;
1242 /* create value for writing to I/O device register */
1243 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1246 if (*data
!= old_addr
) {
1247 if (s
->msi
->mapped
) {
1248 xen_pt_msi_update(s
);
1254 /* write Message Upper Address register */
1255 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1256 XenPTReg
*cfg_entry
, uint32_t *val
,
1257 uint32_t dev_value
, uint32_t valid_mask
)
1259 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1260 uint32_t writable_mask
= 0;
1261 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1262 uint32_t *data
= cfg_entry
->ptr
.word
;
1264 /* check whether the type is 64 bit or not */
1265 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1267 "Can't write to the upper address without 64 bit support\n");
1271 /* modify emulate register */
1272 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1273 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1274 /* update the msi_info too */
1275 s
->msi
->addr_hi
= *data
;
1277 /* create value for writing to I/O device register */
1278 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1281 if (*data
!= old_addr
) {
1282 if (s
->msi
->mapped
) {
1283 xen_pt_msi_update(s
);
1291 /* this function will be called twice (for 32 bit and 64 bit type) */
1292 /* write Message Data register */
1293 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1294 XenPTReg
*cfg_entry
, uint16_t *val
,
1295 uint16_t dev_value
, uint16_t valid_mask
)
1297 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1298 XenPTMSI
*msi
= s
->msi
;
1299 uint16_t writable_mask
= 0;
1300 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1301 uint32_t offset
= reg
->offset
;
1302 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1304 /* check the offset whether matches the type or not */
1305 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1306 /* exit I/O emulator */
1307 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1311 /* modify emulate register */
1312 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1313 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1314 /* update the msi_info too */
1317 /* create value for writing to I/O device register */
1318 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1321 if (*data
!= old_data
) {
1323 xen_pt_msi_update(s
);
1330 /* MSI Capability Structure reg static information table */
1331 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1332 /* Next Pointer reg */
1334 .offset
= PCI_CAP_LIST_NEXT
,
1339 .init
= xen_pt_ptr_reg_init
,
1340 .u
.b
.read
= xen_pt_byte_reg_read
,
1341 .u
.b
.write
= xen_pt_byte_reg_write
,
1343 /* Message Control reg */
1345 .offset
= PCI_MSI_FLAGS
,
1351 .init
= xen_pt_msgctrl_reg_init
,
1352 .u
.w
.read
= xen_pt_word_reg_read
,
1353 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1355 /* Message Address reg */
1357 .offset
= PCI_MSI_ADDRESS_LO
,
1359 .init_val
= 0x00000000,
1360 .ro_mask
= 0x00000003,
1361 .emu_mask
= 0xFFFFFFFF,
1362 .init
= xen_pt_common_reg_init
,
1363 .u
.dw
.read
= xen_pt_long_reg_read
,
1364 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1366 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1368 .offset
= PCI_MSI_ADDRESS_HI
,
1370 .init_val
= 0x00000000,
1371 .ro_mask
= 0x00000000,
1372 .emu_mask
= 0xFFFFFFFF,
1373 .init
= xen_pt_msgaddr64_reg_init
,
1374 .u
.dw
.read
= xen_pt_long_reg_read
,
1375 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1377 /* Message Data reg (16 bits of data for 32-bit devices) */
1379 .offset
= PCI_MSI_DATA_32
,
1384 .init
= xen_pt_msgdata_reg_init
,
1385 .u
.w
.read
= xen_pt_word_reg_read
,
1386 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1388 /* Message Data reg (16 bits of data for 64-bit devices) */
1390 .offset
= PCI_MSI_DATA_64
,
1395 .init
= xen_pt_msgdata_reg_init
,
1396 .u
.w
.read
= xen_pt_word_reg_read
,
1397 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1399 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1401 .offset
= PCI_MSI_MASK_32
,
1403 .init_val
= 0x00000000,
1404 .ro_mask
= 0xFFFFFFFF,
1405 .emu_mask
= 0xFFFFFFFF,
1406 .init
= xen_pt_mask_reg_init
,
1407 .u
.dw
.read
= xen_pt_long_reg_read
,
1408 .u
.dw
.write
= xen_pt_long_reg_write
,
1410 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1412 .offset
= PCI_MSI_MASK_64
,
1414 .init_val
= 0x00000000,
1415 .ro_mask
= 0xFFFFFFFF,
1416 .emu_mask
= 0xFFFFFFFF,
1417 .init
= xen_pt_mask_reg_init
,
1418 .u
.dw
.read
= xen_pt_long_reg_read
,
1419 .u
.dw
.write
= xen_pt_long_reg_write
,
1421 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1423 .offset
= PCI_MSI_MASK_32
+ 4,
1425 .init_val
= 0x00000000,
1426 .ro_mask
= 0xFFFFFFFF,
1427 .emu_mask
= 0x00000000,
1428 .init
= xen_pt_pending_reg_init
,
1429 .u
.dw
.read
= xen_pt_long_reg_read
,
1430 .u
.dw
.write
= xen_pt_long_reg_write
,
1432 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1434 .offset
= PCI_MSI_MASK_64
+ 4,
1436 .init_val
= 0x00000000,
1437 .ro_mask
= 0xFFFFFFFF,
1438 .emu_mask
= 0x00000000,
1439 .init
= xen_pt_pending_reg_init
,
1440 .u
.dw
.read
= xen_pt_long_reg_read
,
1441 .u
.dw
.write
= xen_pt_long_reg_write
,
1449 /**************************************
1453 /* Message Control register for MSI-X */
1454 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1455 XenPTRegInfo
*reg
, uint32_t real_offset
,
1461 /* use I/O device register's value as initial value */
1462 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1466 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1467 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1468 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1469 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1472 s
->msix
->ctrl_offset
= real_offset
;
1474 *data
= reg
->init_val
;
1477 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1478 XenPTReg
*cfg_entry
, uint16_t *val
,
1479 uint16_t dev_value
, uint16_t valid_mask
)
1481 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1482 uint16_t writable_mask
= 0;
1483 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1484 int debug_msix_enabled_old
;
1485 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1487 /* modify emulate register */
1488 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1489 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1491 /* create value for writing to I/O device register */
1492 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1495 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1496 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1497 xen_pt_msix_update(s
);
1498 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1499 xen_pt_msix_disable(s
);
1502 debug_msix_enabled_old
= s
->msix
->enabled
;
1503 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1504 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1505 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1506 s
->msix
->enabled
? "enable" : "disable");
1512 /* MSI-X Capability Structure reg static information table */
1513 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1514 /* Next Pointer reg */
1516 .offset
= PCI_CAP_LIST_NEXT
,
1521 .init
= xen_pt_ptr_reg_init
,
1522 .u
.b
.read
= xen_pt_byte_reg_read
,
1523 .u
.b
.write
= xen_pt_byte_reg_write
,
1525 /* Message Control reg */
1527 .offset
= PCI_MSI_FLAGS
,
1533 .init
= xen_pt_msixctrl_reg_init
,
1534 .u
.w
.read
= xen_pt_word_reg_read
,
1535 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1542 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1543 /* Intel IGFX OpRegion reg */
1548 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1549 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1556 /****************************
1560 /* capability structure register group size functions */
1562 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1563 const XenPTRegGroupInfo
*grp_reg
,
1564 uint32_t base_offset
, uint8_t *size
)
1566 *size
= grp_reg
->grp_size
;
1569 /* get Vendor Specific Capability Structure register group size */
1570 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1571 const XenPTRegGroupInfo
*grp_reg
,
1572 uint32_t base_offset
, uint8_t *size
)
1574 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1576 /* get PCI Express Capability Structure register group size */
1577 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1578 const XenPTRegGroupInfo
*grp_reg
,
1579 uint32_t base_offset
, uint8_t *size
)
1581 PCIDevice
*d
= &s
->dev
;
1582 uint8_t version
= get_capability_version(s
, base_offset
);
1583 uint8_t type
= get_device_type(s
, base_offset
);
1584 uint8_t pcie_size
= 0;
1587 /* calculate size depend on capability version and device/port type */
1588 /* in case of PCI Express Base Specification Rev 1.x */
1590 /* The PCI Express Capabilities, Device Capabilities, and Device
1591 * Status/Control registers are required for all PCI Express devices.
1592 * The Link Capabilities and Link Status/Control are required for all
1593 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1594 * are not required to implement registers other than those listed
1595 * above and terminate the capability structure.
1598 case PCI_EXP_TYPE_ENDPOINT
:
1599 case PCI_EXP_TYPE_LEG_END
:
1602 case PCI_EXP_TYPE_RC_END
:
1606 /* only EndPoint passthrough is supported */
1607 case PCI_EXP_TYPE_ROOT_PORT
:
1608 case PCI_EXP_TYPE_UPSTREAM
:
1609 case PCI_EXP_TYPE_DOWNSTREAM
:
1610 case PCI_EXP_TYPE_PCI_BRIDGE
:
1611 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1612 case PCI_EXP_TYPE_RC_EC
:
1614 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1618 /* in case of PCI Express Base Specification Rev 2.0 */
1619 else if (version
== 2) {
1621 case PCI_EXP_TYPE_ENDPOINT
:
1622 case PCI_EXP_TYPE_LEG_END
:
1623 case PCI_EXP_TYPE_RC_END
:
1624 /* For Functions that do not implement the registers,
1625 * these spaces must be hardwired to 0b.
1629 /* only EndPoint passthrough is supported */
1630 case PCI_EXP_TYPE_ROOT_PORT
:
1631 case PCI_EXP_TYPE_UPSTREAM
:
1632 case PCI_EXP_TYPE_DOWNSTREAM
:
1633 case PCI_EXP_TYPE_PCI_BRIDGE
:
1634 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1635 case PCI_EXP_TYPE_RC_EC
:
1637 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1641 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1648 /* get MSI Capability Structure register group size */
1649 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1650 const XenPTRegGroupInfo
*grp_reg
,
1651 uint32_t base_offset
, uint8_t *size
)
1653 uint16_t msg_ctrl
= 0;
1654 uint8_t msi_size
= 0xa;
1657 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1662 /* check if 64-bit address is capable of per-vector masking */
1663 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1666 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1670 s
->msi
= g_new0(XenPTMSI
, 1);
1671 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1676 /* get MSI-X Capability Structure register group size */
1677 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1678 const XenPTRegGroupInfo
*grp_reg
,
1679 uint32_t base_offset
, uint8_t *size
)
1683 rc
= xen_pt_msix_init(s
, base_offset
);
1686 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1690 *size
= grp_reg
->grp_size
;
1695 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1696 /* Header Type0 reg group */
1699 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1701 .size_init
= xen_pt_reg_grp_size_init
,
1702 .emu_regs
= xen_pt_emu_reg_header0
,
1704 /* PCI PowerManagement Capability reg group */
1706 .grp_id
= PCI_CAP_ID_PM
,
1707 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1708 .grp_size
= PCI_PM_SIZEOF
,
1709 .size_init
= xen_pt_reg_grp_size_init
,
1710 .emu_regs
= xen_pt_emu_reg_pm
,
1712 /* AGP Capability Structure reg group */
1714 .grp_id
= PCI_CAP_ID_AGP
,
1715 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1717 .size_init
= xen_pt_reg_grp_size_init
,
1719 /* Vital Product Data Capability Structure reg group */
1721 .grp_id
= PCI_CAP_ID_VPD
,
1722 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1724 .size_init
= xen_pt_reg_grp_size_init
,
1725 .emu_regs
= xen_pt_emu_reg_vpd
,
1727 /* Slot Identification reg group */
1729 .grp_id
= PCI_CAP_ID_SLOTID
,
1730 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1732 .size_init
= xen_pt_reg_grp_size_init
,
1734 /* MSI Capability Structure reg group */
1736 .grp_id
= PCI_CAP_ID_MSI
,
1737 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1739 .size_init
= xen_pt_msi_size_init
,
1740 .emu_regs
= xen_pt_emu_reg_msi
,
1742 /* PCI-X Capabilities List Item reg group */
1744 .grp_id
= PCI_CAP_ID_PCIX
,
1745 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1747 .size_init
= xen_pt_reg_grp_size_init
,
1749 /* Vendor Specific Capability Structure reg group */
1751 .grp_id
= PCI_CAP_ID_VNDR
,
1752 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1754 .size_init
= xen_pt_vendor_size_init
,
1755 .emu_regs
= xen_pt_emu_reg_vendor
,
1757 /* SHPC Capability List Item reg group */
1759 .grp_id
= PCI_CAP_ID_SHPC
,
1760 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1762 .size_init
= xen_pt_reg_grp_size_init
,
1764 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1766 .grp_id
= PCI_CAP_ID_SSVID
,
1767 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1769 .size_init
= xen_pt_reg_grp_size_init
,
1771 /* AGP 8x Capability Structure reg group */
1773 .grp_id
= PCI_CAP_ID_AGP3
,
1774 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1776 .size_init
= xen_pt_reg_grp_size_init
,
1778 /* PCI Express Capability Structure reg group */
1780 .grp_id
= PCI_CAP_ID_EXP
,
1781 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1783 .size_init
= xen_pt_pcie_size_init
,
1784 .emu_regs
= xen_pt_emu_reg_pcie
,
1786 /* MSI-X Capability Structure reg group */
1788 .grp_id
= PCI_CAP_ID_MSIX
,
1789 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1791 .size_init
= xen_pt_msix_size_init
,
1792 .emu_regs
= xen_pt_emu_reg_msix
,
1794 /* Intel IGD Opregion group */
1796 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1797 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1799 .size_init
= xen_pt_reg_grp_size_init
,
1800 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1807 /* initialize Capabilities Pointer or Next Pointer register */
1808 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1809 XenPTRegInfo
*reg
, uint32_t real_offset
,
1816 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1820 /* find capability offset */
1822 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1823 if (xen_pt_hide_dev_cap(&s
->real_device
,
1824 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1828 rc
= xen_host_pci_get_byte(&s
->real_device
,
1829 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1831 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1832 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1835 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1836 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1839 /* ignore the 0 hardwired capability, find next one */
1844 /* next capability */
1845 rc
= xen_host_pci_get_byte(&s
->real_device
,
1846 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1862 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1865 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1866 uint8_t pos
= PCI_CAPABILITY_LIST
;
1869 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1872 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1877 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1880 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1885 if (xen_host_pci_get_byte(&s
->real_device
,
1886 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1897 pos
+= PCI_CAP_LIST_NEXT
;
1902 static int xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1903 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
)
1905 XenPTReg
*reg_entry
;
1909 reg_entry
= g_new0(XenPTReg
, 1);
1910 reg_entry
->reg
= reg
;
1913 uint32_t host_mask
, size_mask
;
1914 unsigned int offset
;
1917 /* initialize emulate register */
1918 rc
= reg
->init(s
, reg_entry
->reg
,
1919 reg_grp
->base_offset
+ reg
->offset
, &data
);
1924 if (data
== XEN_PT_INVALID_REG
) {
1925 /* free unused BAR register entry */
1929 /* Sync up the data to dev.config */
1930 offset
= reg_grp
->base_offset
+ reg
->offset
;
1931 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1933 switch (reg
->size
) {
1934 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1936 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1938 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1943 /* Serious issues when we cannot read the host values! */
1947 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1948 * contain the emulated view of the guest - therefore we flip the mask
1949 * to mask out the host values (which dev.config initially has) . */
1950 host_mask
= size_mask
& ~reg
->emu_mask
;
1952 if ((data
& host_mask
) != (val
& host_mask
)) {
1955 /* Mask out host (including past size). */
1956 new_val
= val
& host_mask
;
1957 /* Merge emulated ones (excluding the non-emulated ones). */
1958 new_val
|= data
& host_mask
;
1959 /* Leave intact host and emulated values past the size - even though
1960 * we do not care as we write per reg->size granularity, but for the
1961 * logging below lets have the proper value. */
1962 new_val
|= ((val
| data
)) & ~size_mask
;
1963 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1964 offset
, data
, val
, new_val
);
1969 if (val
& ~size_mask
) {
1970 XEN_PT_ERR(&s
->dev
,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
1971 offset
, val
, reg
->size
);
1975 /* This could be just pci_set_long as we don't modify the bits
1976 * past reg->size, but in case this routine is run in parallel or the
1977 * init value is larger, we do not want to over-write registers. */
1978 switch (reg
->size
) {
1979 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1981 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1983 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1987 /* set register value pointer to the data. */
1988 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
1991 /* list add register entry */
1992 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
1997 int xen_pt_config_init(XenPCIPassthroughState
*s
)
2001 QLIST_INIT(&s
->reg_grps
);
2003 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
2004 uint32_t reg_grp_offset
= 0;
2005 XenPTRegGroup
*reg_grp_entry
= NULL
;
2007 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
2008 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
2009 if (xen_pt_hide_dev_cap(&s
->real_device
,
2010 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2014 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2016 if (!reg_grp_offset
) {
2022 * By default we will trap up to 0x40 in the cfg space.
2023 * If an intel device is pass through we need to trap 0xfc,
2024 * therefore the size should be 0xff.
2026 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2027 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2030 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2031 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2032 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2034 reg_grp_entry
->base_offset
= reg_grp_offset
;
2035 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2036 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2037 /* get register group size */
2038 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2040 ®_grp_entry
->size
);
2042 XEN_PT_LOG(&s
->dev
, "Failed to initialize %d/%ld, type=0x%x, rc:%d\n",
2043 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2044 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2045 xen_pt_config_delete(s
);
2050 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2051 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2053 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2054 /* initialize capability register */
2055 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2056 /* initialize capability register */
2057 rc
= xen_pt_config_reg_init(s
, reg_grp_entry
, regs
);
2059 XEN_PT_LOG(&s
->dev
, "Failed to initialize %d/%ld reg 0x%x in grp_type=0x%x (%d/%ld), rc=%d\n",
2060 j
, ARRAY_SIZE(xen_pt_emu_reg_grps
[i
].emu_regs
),
2061 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2062 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
), rc
);
2063 xen_pt_config_delete(s
);
2074 /* delete all emulate register */
2075 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2077 struct XenPTRegGroup
*reg_group
, *next_grp
;
2078 struct XenPTReg
*reg
, *next_reg
;
2080 /* free MSI/MSI-X info table */
2082 xen_pt_msix_unmap(s
);
2086 /* free all register group entry */
2087 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2088 /* free all register entry */
2089 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2090 QLIST_REMOVE(reg
, entries
);
2094 QLIST_REMOVE(reg_group
, entries
);