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
& ~reg
->rw1c_mask
,
187 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
188 uint16_t *val
, uint16_t dev_value
,
191 XenPTRegInfo
*reg
= cfg_entry
->reg
;
192 uint16_t writable_mask
= 0;
193 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
194 uint16_t *data
= cfg_entry
->ptr
.half_word
;
196 /* modify emulate register */
197 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
198 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
200 /* create value for writing to I/O device register */
201 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
206 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
207 uint32_t *val
, uint32_t dev_value
,
210 XenPTRegInfo
*reg
= cfg_entry
->reg
;
211 uint32_t writable_mask
= 0;
212 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
213 uint32_t *data
= cfg_entry
->ptr
.word
;
215 /* modify emulate register */
216 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
217 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
219 /* create value for writing to I/O device register */
220 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
& ~reg
->rw1c_mask
,
227 /* XenPTRegInfo declaration
228 * - only for emulated register (either a part or whole bit).
229 * - for passthrough register that need special behavior (like interacting with
230 * other component), set emu_mask to all 0 and specify r/w func properly.
231 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
234 /********************
238 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
239 XenPTRegInfo
*reg
, uint32_t real_offset
,
242 *data
= s
->real_device
.vendor_id
;
245 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
246 XenPTRegInfo
*reg
, uint32_t real_offset
,
249 *data
= s
->real_device
.device_id
;
252 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
253 XenPTRegInfo
*reg
, uint32_t real_offset
,
256 XenPTRegGroup
*reg_grp_entry
= NULL
;
257 XenPTReg
*reg_entry
= NULL
;
258 uint32_t reg_field
= 0;
260 /* find Header register group */
261 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
263 /* find Capabilities Pointer register */
264 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
266 /* check Capabilities Pointer register */
267 if (*reg_entry
->ptr
.half_word
) {
268 reg_field
|= PCI_STATUS_CAP_LIST
;
270 reg_field
&= ~PCI_STATUS_CAP_LIST
;
273 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
274 " for Capabilities Pointer register."
275 " (%s)\n", __func__
);
279 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
280 " for Header. (%s)\n", __func__
);
287 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
288 XenPTRegInfo
*reg
, uint32_t real_offset
,
291 /* read PCI_HEADER_TYPE */
292 *data
= reg
->init_val
| 0x80;
296 /* initialize Interrupt Pin register */
297 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
298 XenPTRegInfo
*reg
, uint32_t real_offset
,
301 *data
= xen_pt_pci_read_intx(s
);
305 /* Command register */
306 static int xen_pt_cmd_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
307 uint16_t *val
, uint16_t dev_value
,
310 XenPTRegInfo
*reg
= cfg_entry
->reg
;
311 uint16_t writable_mask
= 0;
312 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
313 uint16_t *data
= cfg_entry
->ptr
.half_word
;
315 /* modify emulate register */
316 writable_mask
= ~reg
->ro_mask
& valid_mask
;
317 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
319 /* create value for writing to I/O device register */
320 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
321 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
323 if (s
->machine_irq
) {
324 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
328 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
334 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
335 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
336 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
337 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
339 static bool is_64bit_bar(PCIIORegion
*r
)
341 return !!(r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
344 static uint64_t xen_pt_get_bar_size(PCIIORegion
*r
)
346 if (is_64bit_bar(r
)) {
348 size64
= (r
+ 1)->size
;
356 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
359 PCIDevice
*d
= &s
->dev
;
360 XenPTRegion
*region
= NULL
;
363 /* check 64bit BAR */
364 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
365 int type
= s
->real_device
.io_regions
[index
- 1].type
;
367 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
368 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
369 region
= &s
->bases
[index
- 1];
370 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
371 return XEN_PT_BAR_FLAG_UPPER
;
376 /* check unused BAR */
377 r
= &d
->io_regions
[index
];
378 if (!xen_pt_get_bar_size(r
)) {
379 return XEN_PT_BAR_FLAG_UNUSED
;
383 if (index
== PCI_ROM_SLOT
) {
384 return XEN_PT_BAR_FLAG_MEM
;
387 /* check BAR I/O indicator */
388 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
389 return XEN_PT_BAR_FLAG_IO
;
391 return XEN_PT_BAR_FLAG_MEM
;
395 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
397 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
398 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
400 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
404 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
405 uint32_t real_offset
, uint32_t *data
)
407 uint32_t reg_field
= 0;
410 index
= xen_pt_bar_offset_to_index(reg
->offset
);
411 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
412 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
417 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, index
);
418 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
419 reg_field
= XEN_PT_INVALID_REG
;
425 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
426 uint32_t *value
, uint32_t valid_mask
)
428 XenPTRegInfo
*reg
= cfg_entry
->reg
;
429 uint32_t valid_emu_mask
= 0;
430 uint32_t bar_emu_mask
= 0;
434 index
= xen_pt_bar_offset_to_index(reg
->offset
);
435 if (index
< 0 || index
>= PCI_NUM_REGIONS
- 1) {
436 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
440 /* use fixed-up value from kernel sysfs */
441 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
443 /* set emulate mask depend on BAR flag */
444 switch (s
->bases
[index
].bar_flag
) {
445 case XEN_PT_BAR_FLAG_MEM
:
446 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
448 case XEN_PT_BAR_FLAG_IO
:
449 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
451 case XEN_PT_BAR_FLAG_UPPER
:
452 bar_emu_mask
= XEN_PT_BAR_ALLF
;
459 valid_emu_mask
= bar_emu_mask
& valid_mask
;
460 *value
= XEN_PT_MERGE_VALUE(*value
, *cfg_entry
->ptr
.word
, ~valid_emu_mask
);
464 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
465 uint32_t *val
, uint32_t dev_value
,
468 XenPTRegInfo
*reg
= cfg_entry
->reg
;
469 XenPTRegion
*base
= NULL
;
470 PCIDevice
*d
= &s
->dev
;
471 const PCIIORegion
*r
;
472 uint32_t writable_mask
= 0;
473 uint32_t bar_emu_mask
= 0;
474 uint32_t bar_ro_mask
= 0;
477 uint32_t *data
= cfg_entry
->ptr
.word
;
479 index
= xen_pt_bar_offset_to_index(reg
->offset
);
480 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
481 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
485 r
= &d
->io_regions
[index
];
486 base
= &s
->bases
[index
];
487 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
489 /* set emulate mask and read-only mask values depend on the BAR flag */
490 switch (s
->bases
[index
].bar_flag
) {
491 case XEN_PT_BAR_FLAG_MEM
:
492 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
494 /* low 32 bits mask for 64 bit bars */
495 bar_ro_mask
= XEN_PT_BAR_ALLF
;
497 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
500 case XEN_PT_BAR_FLAG_IO
:
501 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
502 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
504 case XEN_PT_BAR_FLAG_UPPER
:
505 bar_emu_mask
= XEN_PT_BAR_ALLF
;
506 bar_ro_mask
= r_size
? r_size
- 1 : 0;
512 /* modify emulate register */
513 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
514 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
516 /* check whether we need to update the virtual region address or not */
517 switch (s
->bases
[index
].bar_flag
) {
518 case XEN_PT_BAR_FLAG_UPPER
:
519 case XEN_PT_BAR_FLAG_MEM
:
522 case XEN_PT_BAR_FLAG_IO
:
529 /* create value for writing to I/O device register */
530 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
535 /* write Exp ROM BAR */
536 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
537 XenPTReg
*cfg_entry
, uint32_t *val
,
538 uint32_t dev_value
, uint32_t valid_mask
)
540 XenPTRegInfo
*reg
= cfg_entry
->reg
;
541 XenPTRegion
*base
= NULL
;
542 PCIDevice
*d
= (PCIDevice
*)&s
->dev
;
543 uint32_t writable_mask
= 0;
544 uint32_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
546 uint32_t bar_ro_mask
= 0;
547 uint32_t *data
= cfg_entry
->ptr
.word
;
549 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
550 base
= &s
->bases
[PCI_ROM_SLOT
];
551 /* align memory type resource size */
552 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
554 /* set emulate mask and read-only mask */
555 bar_ro_mask
= (reg
->ro_mask
| (r_size
- 1)) & ~PCI_ROM_ADDRESS_ENABLE
;
557 /* modify emulate register */
558 writable_mask
= ~bar_ro_mask
& valid_mask
;
559 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
561 /* create value for writing to I/O device register */
562 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
567 static int xen_pt_intel_opregion_read(XenPCIPassthroughState
*s
,
569 uint32_t *value
, uint32_t valid_mask
)
571 *value
= igd_read_opregion(s
);
575 static int xen_pt_intel_opregion_write(XenPCIPassthroughState
*s
,
576 XenPTReg
*cfg_entry
, uint32_t *value
,
577 uint32_t dev_value
, uint32_t valid_mask
)
579 igd_write_opregion(s
, *value
);
583 /* Header Type0 reg static information table */
584 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
587 .offset
= PCI_VENDOR_ID
,
592 .init
= xen_pt_vendor_reg_init
,
593 .u
.w
.read
= xen_pt_word_reg_read
,
594 .u
.w
.write
= xen_pt_word_reg_write
,
598 .offset
= PCI_DEVICE_ID
,
603 .init
= xen_pt_device_reg_init
,
604 .u
.w
.read
= xen_pt_word_reg_read
,
605 .u
.w
.write
= xen_pt_word_reg_write
,
609 .offset
= PCI_COMMAND
,
614 .init
= xen_pt_common_reg_init
,
615 .u
.w
.read
= xen_pt_word_reg_read
,
616 .u
.w
.write
= xen_pt_cmd_reg_write
,
618 /* Capabilities Pointer reg */
620 .offset
= PCI_CAPABILITY_LIST
,
625 .init
= xen_pt_ptr_reg_init
,
626 .u
.b
.read
= xen_pt_byte_reg_read
,
627 .u
.b
.write
= xen_pt_byte_reg_write
,
630 /* use emulated Cap Ptr value to initialize,
631 * so need to be declared after Cap Ptr reg
634 .offset
= PCI_STATUS
,
641 .init
= xen_pt_status_reg_init
,
642 .u
.w
.read
= xen_pt_word_reg_read
,
643 .u
.w
.write
= xen_pt_word_reg_write
,
645 /* Cache Line Size reg */
647 .offset
= PCI_CACHE_LINE_SIZE
,
652 .init
= xen_pt_common_reg_init
,
653 .u
.b
.read
= xen_pt_byte_reg_read
,
654 .u
.b
.write
= xen_pt_byte_reg_write
,
656 /* Latency Timer reg */
658 .offset
= PCI_LATENCY_TIMER
,
663 .init
= xen_pt_common_reg_init
,
664 .u
.b
.read
= xen_pt_byte_reg_read
,
665 .u
.b
.write
= xen_pt_byte_reg_write
,
667 /* Header Type reg */
669 .offset
= PCI_HEADER_TYPE
,
674 .init
= xen_pt_header_type_reg_init
,
675 .u
.b
.read
= xen_pt_byte_reg_read
,
676 .u
.b
.write
= xen_pt_byte_reg_write
,
678 /* Interrupt Line reg */
680 .offset
= PCI_INTERRUPT_LINE
,
685 .init
= xen_pt_common_reg_init
,
686 .u
.b
.read
= xen_pt_byte_reg_read
,
687 .u
.b
.write
= xen_pt_byte_reg_write
,
689 /* Interrupt Pin reg */
691 .offset
= PCI_INTERRUPT_PIN
,
696 .init
= xen_pt_irqpin_reg_init
,
697 .u
.b
.read
= xen_pt_byte_reg_read
,
698 .u
.b
.write
= xen_pt_byte_reg_write
,
701 /* mask of BAR need to be decided later, depends on IO/MEM type */
703 .offset
= PCI_BASE_ADDRESS_0
,
705 .init_val
= 0x00000000,
706 .init
= xen_pt_bar_reg_init
,
707 .u
.dw
.read
= xen_pt_bar_reg_read
,
708 .u
.dw
.write
= xen_pt_bar_reg_write
,
712 .offset
= PCI_BASE_ADDRESS_1
,
714 .init_val
= 0x00000000,
715 .init
= xen_pt_bar_reg_init
,
716 .u
.dw
.read
= xen_pt_bar_reg_read
,
717 .u
.dw
.write
= xen_pt_bar_reg_write
,
721 .offset
= PCI_BASE_ADDRESS_2
,
723 .init_val
= 0x00000000,
724 .init
= xen_pt_bar_reg_init
,
725 .u
.dw
.read
= xen_pt_bar_reg_read
,
726 .u
.dw
.write
= xen_pt_bar_reg_write
,
730 .offset
= PCI_BASE_ADDRESS_3
,
732 .init_val
= 0x00000000,
733 .init
= xen_pt_bar_reg_init
,
734 .u
.dw
.read
= xen_pt_bar_reg_read
,
735 .u
.dw
.write
= xen_pt_bar_reg_write
,
739 .offset
= PCI_BASE_ADDRESS_4
,
741 .init_val
= 0x00000000,
742 .init
= xen_pt_bar_reg_init
,
743 .u
.dw
.read
= xen_pt_bar_reg_read
,
744 .u
.dw
.write
= xen_pt_bar_reg_write
,
748 .offset
= PCI_BASE_ADDRESS_5
,
750 .init_val
= 0x00000000,
751 .init
= xen_pt_bar_reg_init
,
752 .u
.dw
.read
= xen_pt_bar_reg_read
,
753 .u
.dw
.write
= xen_pt_bar_reg_write
,
755 /* Expansion ROM BAR reg */
757 .offset
= PCI_ROM_ADDRESS
,
759 .init_val
= 0x00000000,
760 .ro_mask
= ~PCI_ROM_ADDRESS_MASK
& ~PCI_ROM_ADDRESS_ENABLE
,
761 .emu_mask
= (uint32_t)PCI_ROM_ADDRESS_MASK
,
762 .init
= xen_pt_bar_reg_init
,
763 .u
.dw
.read
= xen_pt_long_reg_read
,
764 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
772 /*********************************
773 * Vital Product Data Capability
776 /* Vital Product Data Capability Structure reg static information table */
777 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
779 .offset
= PCI_CAP_LIST_NEXT
,
784 .init
= xen_pt_ptr_reg_init
,
785 .u
.b
.read
= xen_pt_byte_reg_read
,
786 .u
.b
.write
= xen_pt_byte_reg_write
,
789 .offset
= PCI_VPD_ADDR
,
793 .init
= xen_pt_common_reg_init
,
794 .u
.w
.read
= xen_pt_word_reg_read
,
795 .u
.w
.write
= xen_pt_word_reg_write
,
803 /**************************************
804 * Vendor Specific Capability
807 /* Vendor Specific Capability Structure reg static information table */
808 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
810 .offset
= PCI_CAP_LIST_NEXT
,
815 .init
= xen_pt_ptr_reg_init
,
816 .u
.b
.read
= xen_pt_byte_reg_read
,
817 .u
.b
.write
= xen_pt_byte_reg_write
,
825 /*****************************
826 * PCI Express Capability
829 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
833 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
836 return flag
& PCI_EXP_FLAGS_VERS
;
839 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
843 if (xen_host_pci_get_byte(&s
->real_device
, offset
+ PCI_EXP_FLAGS
, &flag
)) {
846 return (flag
& PCI_EXP_FLAGS_TYPE
) >> 4;
849 /* initialize Link Control register */
850 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
851 XenPTRegInfo
*reg
, uint32_t real_offset
,
854 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
855 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
857 /* no need to initialize in case of Root Complex Integrated Endpoint
860 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
861 *data
= XEN_PT_INVALID_REG
;
864 *data
= reg
->init_val
;
867 /* initialize Device Control 2 register */
868 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
869 XenPTRegInfo
*reg
, uint32_t real_offset
,
872 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
874 /* no need to initialize in case of cap_ver 1.x */
876 *data
= XEN_PT_INVALID_REG
;
879 *data
= reg
->init_val
;
882 /* initialize Link Control 2 register */
883 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
884 XenPTRegInfo
*reg
, uint32_t real_offset
,
887 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
888 uint32_t reg_field
= 0;
890 /* no need to initialize in case of cap_ver 1.x */
892 reg_field
= XEN_PT_INVALID_REG
;
894 /* set Supported Link Speed */
897 rc
= xen_host_pci_get_byte(&s
->real_device
,
898 real_offset
- reg
->offset
+ PCI_EXP_LNKCAP
,
903 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
910 /* PCI Express Capability Structure reg static information table */
911 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
912 /* Next Pointer reg */
914 .offset
= PCI_CAP_LIST_NEXT
,
919 .init
= xen_pt_ptr_reg_init
,
920 .u
.b
.read
= xen_pt_byte_reg_read
,
921 .u
.b
.write
= xen_pt_byte_reg_write
,
923 /* Device Capabilities reg */
925 .offset
= PCI_EXP_DEVCAP
,
927 .init_val
= 0x00000000,
928 .ro_mask
= 0xFFFFFFFF,
929 .emu_mask
= 0x10000000,
930 .init
= xen_pt_common_reg_init
,
931 .u
.dw
.read
= xen_pt_long_reg_read
,
932 .u
.dw
.write
= xen_pt_long_reg_write
,
934 /* Device Control reg */
936 .offset
= PCI_EXP_DEVCTL
,
941 .init
= xen_pt_common_reg_init
,
942 .u
.w
.read
= xen_pt_word_reg_read
,
943 .u
.w
.write
= xen_pt_word_reg_write
,
945 /* Device Status reg */
947 .offset
= PCI_EXP_DEVSTA
,
952 .init
= xen_pt_common_reg_init
,
953 .u
.w
.read
= xen_pt_word_reg_read
,
954 .u
.w
.write
= xen_pt_word_reg_write
,
956 /* Link Control reg */
958 .offset
= PCI_EXP_LNKCTL
,
963 .init
= xen_pt_linkctrl_reg_init
,
964 .u
.w
.read
= xen_pt_word_reg_read
,
965 .u
.w
.write
= xen_pt_word_reg_write
,
967 /* Link Status reg */
969 .offset
= PCI_EXP_LNKSTA
,
973 .init
= xen_pt_common_reg_init
,
974 .u
.w
.read
= xen_pt_word_reg_read
,
975 .u
.w
.write
= xen_pt_word_reg_write
,
977 /* Device Control 2 reg */
984 .init
= xen_pt_devctrl2_reg_init
,
985 .u
.w
.read
= xen_pt_word_reg_read
,
986 .u
.w
.write
= xen_pt_word_reg_write
,
988 /* Link Control 2 reg */
995 .init
= xen_pt_linkctrl2_reg_init
,
996 .u
.w
.read
= xen_pt_word_reg_read
,
997 .u
.w
.write
= xen_pt_word_reg_write
,
1005 /*********************************
1006 * Power Management Capability
1009 /* Power Management Capability reg static information table */
1010 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
1011 /* Next Pointer reg */
1013 .offset
= PCI_CAP_LIST_NEXT
,
1018 .init
= xen_pt_ptr_reg_init
,
1019 .u
.b
.read
= xen_pt_byte_reg_read
,
1020 .u
.b
.write
= xen_pt_byte_reg_write
,
1022 /* Power Management Capabilities reg */
1024 .offset
= PCI_CAP_FLAGS
,
1029 .init
= xen_pt_common_reg_init
,
1030 .u
.w
.read
= xen_pt_word_reg_read
,
1031 .u
.w
.write
= xen_pt_word_reg_write
,
1033 /* PCI Power Management Control/Status reg */
1035 .offset
= PCI_PM_CTRL
,
1040 .rw1c_mask
= 0x8000,
1042 .init
= xen_pt_common_reg_init
,
1043 .u
.w
.read
= xen_pt_word_reg_read
,
1044 .u
.w
.write
= xen_pt_word_reg_write
,
1052 /********************************
1057 #define xen_pt_msi_check_type(offset, flags, what) \
1058 ((offset) == ((flags) & PCI_MSI_FLAGS_64BIT ? \
1059 PCI_MSI_##what##_64 : PCI_MSI_##what##_32))
1061 /* Message Control register */
1062 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1063 XenPTRegInfo
*reg
, uint32_t real_offset
,
1066 XenPTMSI
*msi
= s
->msi
;
1070 /* use I/O device register's value as initial value */
1071 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1075 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1076 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1077 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1078 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1080 msi
->flags
|= reg_field
;
1081 msi
->ctrl_offset
= real_offset
;
1082 msi
->initialized
= false;
1083 msi
->mapped
= false;
1085 *data
= reg
->init_val
;
1088 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1089 XenPTReg
*cfg_entry
, uint16_t *val
,
1090 uint16_t dev_value
, uint16_t valid_mask
)
1092 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1093 XenPTMSI
*msi
= s
->msi
;
1094 uint16_t writable_mask
= 0;
1095 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1096 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1098 /* Currently no support for multi-vector */
1099 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1100 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1103 /* modify emulate register */
1104 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1105 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1106 msi
->flags
|= *data
& ~PCI_MSI_FLAGS_ENABLE
;
1108 /* create value for writing to I/O device register */
1109 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1112 if (*val
& PCI_MSI_FLAGS_ENABLE
) {
1113 /* setup MSI pirq for the first time */
1114 if (!msi
->initialized
) {
1115 /* Init physical one */
1116 XEN_PT_LOG(&s
->dev
, "setup MSI (register: %x).\n", *val
);
1117 if (xen_pt_msi_setup(s
)) {
1118 /* We do not broadcast the error to the framework code, so
1119 * that MSI errors are contained in MSI emulation code and
1120 * QEMU can go on running.
1121 * Guest MSI would be actually not working.
1123 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1124 XEN_PT_WARN(&s
->dev
, "Can not map MSI (register: %x)!\n", *val
);
1127 if (xen_pt_msi_update(s
)) {
1128 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1129 XEN_PT_WARN(&s
->dev
, "Can not bind MSI (register: %x)!\n", *val
);
1132 msi
->initialized
= true;
1135 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1136 } else if (msi
->mapped
) {
1137 xen_pt_msi_disable(s
);
1143 /* initialize Message Upper Address register */
1144 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1145 XenPTRegInfo
*reg
, uint32_t real_offset
,
1148 /* no need to initialize in case of 32 bit type */
1149 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1150 *data
= XEN_PT_INVALID_REG
;
1152 *data
= reg
->init_val
;
1157 /* this function will be called twice (for 32 bit and 64 bit type) */
1158 /* initialize Message Data register */
1159 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1160 XenPTRegInfo
*reg
, uint32_t real_offset
,
1163 uint32_t flags
= s
->msi
->flags
;
1164 uint32_t offset
= reg
->offset
;
1166 /* check the offset whether matches the type or not */
1167 if (xen_pt_msi_check_type(offset
, flags
, DATA
)) {
1168 *data
= reg
->init_val
;
1170 *data
= XEN_PT_INVALID_REG
;
1175 /* this function will be called twice (for 32 bit and 64 bit type) */
1176 /* initialize Mask register */
1177 static int xen_pt_mask_reg_init(XenPCIPassthroughState
*s
,
1178 XenPTRegInfo
*reg
, uint32_t real_offset
,
1181 uint32_t flags
= s
->msi
->flags
;
1183 /* check the offset whether matches the type or not */
1184 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1185 *data
= XEN_PT_INVALID_REG
;
1186 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, MASK
)) {
1187 *data
= reg
->init_val
;
1189 *data
= XEN_PT_INVALID_REG
;
1194 /* this function will be called twice (for 32 bit and 64 bit type) */
1195 /* initialize Pending register */
1196 static int xen_pt_pending_reg_init(XenPCIPassthroughState
*s
,
1197 XenPTRegInfo
*reg
, uint32_t real_offset
,
1200 uint32_t flags
= s
->msi
->flags
;
1202 /* check the offset whether matches the type or not */
1203 if (!(flags
& PCI_MSI_FLAGS_MASKBIT
)) {
1204 *data
= XEN_PT_INVALID_REG
;
1205 } else if (xen_pt_msi_check_type(reg
->offset
, flags
, PENDING
)) {
1206 *data
= reg
->init_val
;
1208 *data
= XEN_PT_INVALID_REG
;
1213 /* write Message Address register */
1214 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1215 XenPTReg
*cfg_entry
, uint32_t *val
,
1216 uint32_t dev_value
, uint32_t valid_mask
)
1218 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1219 uint32_t writable_mask
= 0;
1220 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1221 uint32_t *data
= cfg_entry
->ptr
.word
;
1223 /* modify emulate register */
1224 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1225 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1226 s
->msi
->addr_lo
= *data
;
1228 /* create value for writing to I/O device register */
1229 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1232 if (*data
!= old_addr
) {
1233 if (s
->msi
->mapped
) {
1234 xen_pt_msi_update(s
);
1240 /* write Message Upper Address register */
1241 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1242 XenPTReg
*cfg_entry
, uint32_t *val
,
1243 uint32_t dev_value
, uint32_t valid_mask
)
1245 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1246 uint32_t writable_mask
= 0;
1247 uint32_t old_addr
= *cfg_entry
->ptr
.word
;
1248 uint32_t *data
= cfg_entry
->ptr
.word
;
1250 /* check whether the type is 64 bit or not */
1251 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1253 "Can't write to the upper address without 64 bit support\n");
1257 /* modify emulate register */
1258 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1259 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1260 /* update the msi_info too */
1261 s
->msi
->addr_hi
= *data
;
1263 /* create value for writing to I/O device register */
1264 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1267 if (*data
!= old_addr
) {
1268 if (s
->msi
->mapped
) {
1269 xen_pt_msi_update(s
);
1277 /* this function will be called twice (for 32 bit and 64 bit type) */
1278 /* write Message Data register */
1279 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1280 XenPTReg
*cfg_entry
, uint16_t *val
,
1281 uint16_t dev_value
, uint16_t valid_mask
)
1283 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1284 XenPTMSI
*msi
= s
->msi
;
1285 uint16_t writable_mask
= 0;
1286 uint16_t old_data
= *cfg_entry
->ptr
.half_word
;
1287 uint32_t offset
= reg
->offset
;
1288 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1290 /* check the offset whether matches the type or not */
1291 if (!xen_pt_msi_check_type(offset
, msi
->flags
, DATA
)) {
1292 /* exit I/O emulator */
1293 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1297 /* modify emulate register */
1298 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1299 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1300 /* update the msi_info too */
1303 /* create value for writing to I/O device register */
1304 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, 0);
1307 if (*data
!= old_data
) {
1309 xen_pt_msi_update(s
);
1316 /* MSI Capability Structure reg static information table */
1317 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1318 /* Next Pointer reg */
1320 .offset
= PCI_CAP_LIST_NEXT
,
1325 .init
= xen_pt_ptr_reg_init
,
1326 .u
.b
.read
= xen_pt_byte_reg_read
,
1327 .u
.b
.write
= xen_pt_byte_reg_write
,
1329 /* Message Control reg */
1331 .offset
= PCI_MSI_FLAGS
,
1337 .init
= xen_pt_msgctrl_reg_init
,
1338 .u
.w
.read
= xen_pt_word_reg_read
,
1339 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1341 /* Message Address reg */
1343 .offset
= PCI_MSI_ADDRESS_LO
,
1345 .init_val
= 0x00000000,
1346 .ro_mask
= 0x00000003,
1347 .emu_mask
= 0xFFFFFFFF,
1348 .init
= xen_pt_common_reg_init
,
1349 .u
.dw
.read
= xen_pt_long_reg_read
,
1350 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1352 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1354 .offset
= PCI_MSI_ADDRESS_HI
,
1356 .init_val
= 0x00000000,
1357 .ro_mask
= 0x00000000,
1358 .emu_mask
= 0xFFFFFFFF,
1359 .init
= xen_pt_msgaddr64_reg_init
,
1360 .u
.dw
.read
= xen_pt_long_reg_read
,
1361 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1363 /* Message Data reg (16 bits of data for 32-bit devices) */
1365 .offset
= PCI_MSI_DATA_32
,
1370 .init
= xen_pt_msgdata_reg_init
,
1371 .u
.w
.read
= xen_pt_word_reg_read
,
1372 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1374 /* Message Data reg (16 bits of data for 64-bit devices) */
1376 .offset
= PCI_MSI_DATA_64
,
1381 .init
= xen_pt_msgdata_reg_init
,
1382 .u
.w
.read
= xen_pt_word_reg_read
,
1383 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1385 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1387 .offset
= PCI_MSI_MASK_32
,
1389 .init_val
= 0x00000000,
1390 .ro_mask
= 0xFFFFFFFF,
1391 .emu_mask
= 0xFFFFFFFF,
1392 .init
= xen_pt_mask_reg_init
,
1393 .u
.dw
.read
= xen_pt_long_reg_read
,
1394 .u
.dw
.write
= xen_pt_long_reg_write
,
1396 /* Mask reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1398 .offset
= PCI_MSI_MASK_64
,
1400 .init_val
= 0x00000000,
1401 .ro_mask
= 0xFFFFFFFF,
1402 .emu_mask
= 0xFFFFFFFF,
1403 .init
= xen_pt_mask_reg_init
,
1404 .u
.dw
.read
= xen_pt_long_reg_read
,
1405 .u
.dw
.write
= xen_pt_long_reg_write
,
1407 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 32-bit devices) */
1409 .offset
= PCI_MSI_MASK_32
+ 4,
1411 .init_val
= 0x00000000,
1412 .ro_mask
= 0xFFFFFFFF,
1413 .emu_mask
= 0x00000000,
1414 .init
= xen_pt_pending_reg_init
,
1415 .u
.dw
.read
= xen_pt_long_reg_read
,
1416 .u
.dw
.write
= xen_pt_long_reg_write
,
1418 /* Pending reg (if PCI_MSI_FLAGS_MASKBIT set, for 64-bit devices) */
1420 .offset
= PCI_MSI_MASK_64
+ 4,
1422 .init_val
= 0x00000000,
1423 .ro_mask
= 0xFFFFFFFF,
1424 .emu_mask
= 0x00000000,
1425 .init
= xen_pt_pending_reg_init
,
1426 .u
.dw
.read
= xen_pt_long_reg_read
,
1427 .u
.dw
.write
= xen_pt_long_reg_write
,
1435 /**************************************
1439 /* Message Control register for MSI-X */
1440 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1441 XenPTRegInfo
*reg
, uint32_t real_offset
,
1447 /* use I/O device register's value as initial value */
1448 rc
= xen_host_pci_get_word(&s
->real_device
, real_offset
, ®_field
);
1452 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1453 XEN_PT_LOG(&s
->dev
, "MSIX already enabled, disabling it first\n");
1454 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1455 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1458 s
->msix
->ctrl_offset
= real_offset
;
1460 *data
= reg
->init_val
;
1463 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1464 XenPTReg
*cfg_entry
, uint16_t *val
,
1465 uint16_t dev_value
, uint16_t valid_mask
)
1467 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1468 uint16_t writable_mask
= 0;
1469 uint16_t throughable_mask
= get_throughable_mask(s
, reg
, valid_mask
);
1470 int debug_msix_enabled_old
;
1471 uint16_t *data
= cfg_entry
->ptr
.half_word
;
1473 /* modify emulate register */
1474 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1475 *data
= XEN_PT_MERGE_VALUE(*val
, *data
, writable_mask
);
1477 /* create value for writing to I/O device register */
1478 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1481 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1482 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1483 xen_pt_msix_update(s
);
1484 } else if (!(*val
& PCI_MSIX_FLAGS_ENABLE
) && s
->msix
->enabled
) {
1485 xen_pt_msix_disable(s
);
1488 s
->msix
->maskall
= *val
& PCI_MSIX_FLAGS_MASKALL
;
1490 debug_msix_enabled_old
= s
->msix
->enabled
;
1491 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1492 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1493 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1494 s
->msix
->enabled
? "enable" : "disable");
1500 /* MSI-X Capability Structure reg static information table */
1501 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1502 /* Next Pointer reg */
1504 .offset
= PCI_CAP_LIST_NEXT
,
1509 .init
= xen_pt_ptr_reg_init
,
1510 .u
.b
.read
= xen_pt_byte_reg_read
,
1511 .u
.b
.write
= xen_pt_byte_reg_write
,
1513 /* Message Control reg */
1515 .offset
= PCI_MSI_FLAGS
,
1521 .init
= xen_pt_msixctrl_reg_init
,
1522 .u
.w
.read
= xen_pt_word_reg_read
,
1523 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1530 static XenPTRegInfo xen_pt_emu_reg_igd_opregion
[] = {
1531 /* Intel IGFX OpRegion reg */
1536 .u
.dw
.read
= xen_pt_intel_opregion_read
,
1537 .u
.dw
.write
= xen_pt_intel_opregion_write
,
1544 /****************************
1548 /* capability structure register group size functions */
1550 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1551 const XenPTRegGroupInfo
*grp_reg
,
1552 uint32_t base_offset
, uint8_t *size
)
1554 *size
= grp_reg
->grp_size
;
1557 /* get Vendor Specific Capability Structure register group size */
1558 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1559 const XenPTRegGroupInfo
*grp_reg
,
1560 uint32_t base_offset
, uint8_t *size
)
1562 return xen_host_pci_get_byte(&s
->real_device
, base_offset
+ 0x02, size
);
1564 /* get PCI Express Capability Structure register group size */
1565 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1566 const XenPTRegGroupInfo
*grp_reg
,
1567 uint32_t base_offset
, uint8_t *size
)
1569 PCIDevice
*d
= &s
->dev
;
1570 uint8_t version
= get_capability_version(s
, base_offset
);
1571 uint8_t type
= get_device_type(s
, base_offset
);
1572 uint8_t pcie_size
= 0;
1575 /* calculate size depend on capability version and device/port type */
1576 /* in case of PCI Express Base Specification Rev 1.x */
1578 /* The PCI Express Capabilities, Device Capabilities, and Device
1579 * Status/Control registers are required for all PCI Express devices.
1580 * The Link Capabilities and Link Status/Control are required for all
1581 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1582 * are not required to implement registers other than those listed
1583 * above and terminate the capability structure.
1586 case PCI_EXP_TYPE_ENDPOINT
:
1587 case PCI_EXP_TYPE_LEG_END
:
1590 case PCI_EXP_TYPE_RC_END
:
1594 /* only EndPoint passthrough is supported */
1595 case PCI_EXP_TYPE_ROOT_PORT
:
1596 case PCI_EXP_TYPE_UPSTREAM
:
1597 case PCI_EXP_TYPE_DOWNSTREAM
:
1598 case PCI_EXP_TYPE_PCI_BRIDGE
:
1599 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1600 case PCI_EXP_TYPE_RC_EC
:
1602 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1606 /* in case of PCI Express Base Specification Rev 2.0 */
1607 else if (version
== 2) {
1609 case PCI_EXP_TYPE_ENDPOINT
:
1610 case PCI_EXP_TYPE_LEG_END
:
1611 case PCI_EXP_TYPE_RC_END
:
1612 /* For Functions that do not implement the registers,
1613 * these spaces must be hardwired to 0b.
1617 /* only EndPoint passthrough is supported */
1618 case PCI_EXP_TYPE_ROOT_PORT
:
1619 case PCI_EXP_TYPE_UPSTREAM
:
1620 case PCI_EXP_TYPE_DOWNSTREAM
:
1621 case PCI_EXP_TYPE_PCI_BRIDGE
:
1622 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1623 case PCI_EXP_TYPE_RC_EC
:
1625 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1629 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1636 /* get MSI Capability Structure register group size */
1637 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1638 const XenPTRegGroupInfo
*grp_reg
,
1639 uint32_t base_offset
, uint8_t *size
)
1641 uint16_t msg_ctrl
= 0;
1642 uint8_t msi_size
= 0xa;
1645 rc
= xen_host_pci_get_word(&s
->real_device
, base_offset
+ PCI_MSI_FLAGS
,
1650 /* check if 64-bit address is capable of per-vector masking */
1651 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1654 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1658 s
->msi
= g_new0(XenPTMSI
, 1);
1659 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1664 /* get MSI-X Capability Structure register group size */
1665 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1666 const XenPTRegGroupInfo
*grp_reg
,
1667 uint32_t base_offset
, uint8_t *size
)
1671 rc
= xen_pt_msix_init(s
, base_offset
);
1674 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1678 *size
= grp_reg
->grp_size
;
1683 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1684 /* Header Type0 reg group */
1687 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1689 .size_init
= xen_pt_reg_grp_size_init
,
1690 .emu_regs
= xen_pt_emu_reg_header0
,
1692 /* PCI PowerManagement Capability reg group */
1694 .grp_id
= PCI_CAP_ID_PM
,
1695 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1696 .grp_size
= PCI_PM_SIZEOF
,
1697 .size_init
= xen_pt_reg_grp_size_init
,
1698 .emu_regs
= xen_pt_emu_reg_pm
,
1700 /* AGP Capability Structure reg group */
1702 .grp_id
= PCI_CAP_ID_AGP
,
1703 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1705 .size_init
= xen_pt_reg_grp_size_init
,
1707 /* Vital Product Data Capability Structure reg group */
1709 .grp_id
= PCI_CAP_ID_VPD
,
1710 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1712 .size_init
= xen_pt_reg_grp_size_init
,
1713 .emu_regs
= xen_pt_emu_reg_vpd
,
1715 /* Slot Identification reg group */
1717 .grp_id
= PCI_CAP_ID_SLOTID
,
1718 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1720 .size_init
= xen_pt_reg_grp_size_init
,
1722 /* MSI Capability Structure reg group */
1724 .grp_id
= PCI_CAP_ID_MSI
,
1725 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1727 .size_init
= xen_pt_msi_size_init
,
1728 .emu_regs
= xen_pt_emu_reg_msi
,
1730 /* PCI-X Capabilities List Item reg group */
1732 .grp_id
= PCI_CAP_ID_PCIX
,
1733 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1735 .size_init
= xen_pt_reg_grp_size_init
,
1737 /* Vendor Specific Capability Structure reg group */
1739 .grp_id
= PCI_CAP_ID_VNDR
,
1740 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1742 .size_init
= xen_pt_vendor_size_init
,
1743 .emu_regs
= xen_pt_emu_reg_vendor
,
1745 /* SHPC Capability List Item reg group */
1747 .grp_id
= PCI_CAP_ID_SHPC
,
1748 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1750 .size_init
= xen_pt_reg_grp_size_init
,
1752 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1754 .grp_id
= PCI_CAP_ID_SSVID
,
1755 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1757 .size_init
= xen_pt_reg_grp_size_init
,
1759 /* AGP 8x Capability Structure reg group */
1761 .grp_id
= PCI_CAP_ID_AGP3
,
1762 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1764 .size_init
= xen_pt_reg_grp_size_init
,
1766 /* PCI Express Capability Structure reg group */
1768 .grp_id
= PCI_CAP_ID_EXP
,
1769 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1771 .size_init
= xen_pt_pcie_size_init
,
1772 .emu_regs
= xen_pt_emu_reg_pcie
,
1774 /* MSI-X Capability Structure reg group */
1776 .grp_id
= PCI_CAP_ID_MSIX
,
1777 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1779 .size_init
= xen_pt_msix_size_init
,
1780 .emu_regs
= xen_pt_emu_reg_msix
,
1782 /* Intel IGD Opregion group */
1784 .grp_id
= XEN_PCI_INTEL_OPREGION
,
1785 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1787 .size_init
= xen_pt_reg_grp_size_init
,
1788 .emu_regs
= xen_pt_emu_reg_igd_opregion
,
1795 /* initialize Capabilities Pointer or Next Pointer register */
1796 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1797 XenPTRegInfo
*reg
, uint32_t real_offset
,
1804 rc
= xen_host_pci_get_byte(&s
->real_device
, real_offset
, ®_field
);
1808 /* find capability offset */
1810 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1811 if (xen_pt_hide_dev_cap(&s
->real_device
,
1812 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1816 rc
= xen_host_pci_get_byte(&s
->real_device
,
1817 reg_field
+ PCI_CAP_LIST_ID
, &cap_id
);
1819 XEN_PT_ERR(&s
->dev
, "Failed to read capability @0x%x (rc:%d)\n",
1820 reg_field
+ PCI_CAP_LIST_ID
, rc
);
1823 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1824 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1827 /* ignore the 0 hardwired capability, find next one */
1832 /* next capability */
1833 rc
= xen_host_pci_get_byte(&s
->real_device
,
1834 reg_field
+ PCI_CAP_LIST_NEXT
, ®_field
);
1850 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1853 unsigned max_cap
= XEN_PCI_CAP_MAX
;
1854 uint8_t pos
= PCI_CAPABILITY_LIST
;
1857 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1860 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1865 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1868 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1873 if (xen_host_pci_get_byte(&s
->real_device
,
1874 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1885 pos
+= PCI_CAP_LIST_NEXT
;
1890 static int xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1891 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
)
1893 XenPTReg
*reg_entry
;
1897 reg_entry
= g_new0(XenPTReg
, 1);
1898 reg_entry
->reg
= reg
;
1901 uint32_t host_mask
, size_mask
;
1902 unsigned int offset
;
1905 /* initialize emulate register */
1906 rc
= reg
->init(s
, reg_entry
->reg
,
1907 reg_grp
->base_offset
+ reg
->offset
, &data
);
1912 if (data
== XEN_PT_INVALID_REG
) {
1913 /* free unused BAR register entry */
1917 /* Sync up the data to dev.config */
1918 offset
= reg_grp
->base_offset
+ reg
->offset
;
1919 size_mask
= 0xFFFFFFFF >> ((4 - reg
->size
) << 3);
1921 switch (reg
->size
) {
1922 case 1: rc
= xen_host_pci_get_byte(&s
->real_device
, offset
, (uint8_t *)&val
);
1924 case 2: rc
= xen_host_pci_get_word(&s
->real_device
, offset
, (uint16_t *)&val
);
1926 case 4: rc
= xen_host_pci_get_long(&s
->real_device
, offset
, &val
);
1931 /* Serious issues when we cannot read the host values! */
1935 /* Set bits in emu_mask are the ones we emulate. The dev.config shall
1936 * contain the emulated view of the guest - therefore we flip the mask
1937 * to mask out the host values (which dev.config initially has) . */
1938 host_mask
= size_mask
& ~reg
->emu_mask
;
1940 if ((data
& host_mask
) != (val
& host_mask
)) {
1943 /* Mask out host (including past size). */
1944 new_val
= val
& host_mask
;
1945 /* Merge emulated ones (excluding the non-emulated ones). */
1946 new_val
|= data
& host_mask
;
1947 /* Leave intact host and emulated values past the size - even though
1948 * we do not care as we write per reg->size granularity, but for the
1949 * logging below lets have the proper value. */
1950 new_val
|= ((val
| data
)) & ~size_mask
;
1951 XEN_PT_LOG(&s
->dev
,"Offset 0x%04x mismatch! Emulated=0x%04x, host=0x%04x, syncing to 0x%04x.\n",
1952 offset
, data
, val
, new_val
);
1957 if (val
& ~size_mask
) {
1958 XEN_PT_ERR(&s
->dev
,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
1959 offset
, val
, reg
->size
);
1963 /* This could be just pci_set_long as we don't modify the bits
1964 * past reg->size, but in case this routine is run in parallel or the
1965 * init value is larger, we do not want to over-write registers. */
1966 switch (reg
->size
) {
1967 case 1: pci_set_byte(s
->dev
.config
+ offset
, (uint8_t)val
);
1969 case 2: pci_set_word(s
->dev
.config
+ offset
, (uint16_t)val
);
1971 case 4: pci_set_long(s
->dev
.config
+ offset
, val
);
1975 /* set register value pointer to the data. */
1976 reg_entry
->ptr
.byte
= s
->dev
.config
+ offset
;
1979 /* list add register entry */
1980 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
1985 int xen_pt_config_init(XenPCIPassthroughState
*s
)
1989 QLIST_INIT(&s
->reg_grps
);
1991 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1992 uint32_t reg_grp_offset
= 0;
1993 XenPTRegGroup
*reg_grp_entry
= NULL
;
1995 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF
1996 && xen_pt_emu_reg_grps
[i
].grp_id
!= XEN_PCI_INTEL_OPREGION
) {
1997 if (xen_pt_hide_dev_cap(&s
->real_device
,
1998 xen_pt_emu_reg_grps
[i
].grp_id
)) {
2002 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
2004 if (!reg_grp_offset
) {
2010 * By default we will trap up to 0x40 in the cfg space.
2011 * If an intel device is pass through we need to trap 0xfc,
2012 * therefore the size should be 0xff.
2014 if (xen_pt_emu_reg_grps
[i
].grp_id
== XEN_PCI_INTEL_OPREGION
) {
2015 reg_grp_offset
= XEN_PCI_INTEL_OPREGION
;
2018 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
2019 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
2020 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
2022 reg_grp_entry
->base_offset
= reg_grp_offset
;
2023 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
2024 if (xen_pt_emu_reg_grps
[i
].size_init
) {
2025 /* get register group size */
2026 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
2028 ®_grp_entry
->size
);
2030 XEN_PT_LOG(&s
->dev
, "Failed to initialize %d/%ld, type=0x%x, rc:%d\n",
2031 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
),
2032 xen_pt_emu_reg_grps
[i
].grp_type
, rc
);
2033 xen_pt_config_delete(s
);
2038 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
2039 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
2041 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
2042 /* initialize capability register */
2043 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
2044 /* initialize capability register */
2045 rc
= xen_pt_config_reg_init(s
, reg_grp_entry
, regs
);
2047 XEN_PT_LOG(&s
->dev
, "Failed to initialize %d/%ld reg 0x%x in grp_type=0x%x (%d/%ld), rc=%d\n",
2048 j
, ARRAY_SIZE(xen_pt_emu_reg_grps
[i
].emu_regs
),
2049 regs
->offset
, xen_pt_emu_reg_grps
[i
].grp_type
,
2050 i
, ARRAY_SIZE(xen_pt_emu_reg_grps
), rc
);
2051 xen_pt_config_delete(s
);
2062 /* delete all emulate register */
2063 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
2065 struct XenPTRegGroup
*reg_group
, *next_grp
;
2066 struct XenPTReg
*reg
, *next_reg
;
2068 /* free MSI/MSI-X info table */
2070 xen_pt_msix_unmap(s
);
2074 /* free all register group entry */
2075 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
2076 /* free all register entry */
2077 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
2078 QLIST_REMOVE(reg
, entries
);
2082 QLIST_REMOVE(reg_group
, entries
);