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 "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
)) {
100 * general register functions
103 /* register initialization function */
105 static int xen_pt_common_reg_init(XenPCIPassthroughState
*s
,
106 XenPTRegInfo
*reg
, uint32_t real_offset
,
109 *data
= reg
->init_val
;
113 /* Read register functions */
115 static int xen_pt_byte_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
116 uint8_t *value
, uint8_t valid_mask
)
118 XenPTRegInfo
*reg
= cfg_entry
->reg
;
119 uint8_t valid_emu_mask
= 0;
121 /* emulate byte register */
122 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
123 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
127 static int xen_pt_word_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
128 uint16_t *value
, uint16_t valid_mask
)
130 XenPTRegInfo
*reg
= cfg_entry
->reg
;
131 uint16_t valid_emu_mask
= 0;
133 /* emulate word register */
134 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
135 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
139 static int xen_pt_long_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
140 uint32_t *value
, uint32_t valid_mask
)
142 XenPTRegInfo
*reg
= cfg_entry
->reg
;
143 uint32_t valid_emu_mask
= 0;
145 /* emulate long register */
146 valid_emu_mask
= reg
->emu_mask
& valid_mask
;
147 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
152 /* Write register functions */
154 static int xen_pt_byte_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
155 uint8_t *val
, uint8_t dev_value
,
158 XenPTRegInfo
*reg
= cfg_entry
->reg
;
159 uint8_t writable_mask
= 0;
160 uint8_t throughable_mask
= 0;
162 /* modify emulate register */
163 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
164 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
166 /* create value for writing to I/O device register */
167 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
168 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
172 static int xen_pt_word_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
173 uint16_t *val
, uint16_t dev_value
,
176 XenPTRegInfo
*reg
= cfg_entry
->reg
;
177 uint16_t writable_mask
= 0;
178 uint16_t throughable_mask
= 0;
180 /* modify emulate register */
181 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
182 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
184 /* create value for writing to I/O device register */
185 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
186 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
190 static int xen_pt_long_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
191 uint32_t *val
, uint32_t dev_value
,
194 XenPTRegInfo
*reg
= cfg_entry
->reg
;
195 uint32_t writable_mask
= 0;
196 uint32_t throughable_mask
= 0;
198 /* modify emulate register */
199 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
200 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
202 /* create value for writing to I/O device register */
203 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
204 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
210 /* XenPTRegInfo declaration
211 * - only for emulated register (either a part or whole bit).
212 * - for passthrough register that need special behavior (like interacting with
213 * other component), set emu_mask to all 0 and specify r/w func properly.
214 * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
217 /********************
221 static int xen_pt_vendor_reg_init(XenPCIPassthroughState
*s
,
222 XenPTRegInfo
*reg
, uint32_t real_offset
,
225 *data
= s
->real_device
.vendor_id
;
228 static int xen_pt_device_reg_init(XenPCIPassthroughState
*s
,
229 XenPTRegInfo
*reg
, uint32_t real_offset
,
232 *data
= s
->real_device
.device_id
;
235 static int xen_pt_status_reg_init(XenPCIPassthroughState
*s
,
236 XenPTRegInfo
*reg
, uint32_t real_offset
,
239 XenPTRegGroup
*reg_grp_entry
= NULL
;
240 XenPTReg
*reg_entry
= NULL
;
241 uint32_t reg_field
= 0;
243 /* find Header register group */
244 reg_grp_entry
= xen_pt_find_reg_grp(s
, PCI_CAPABILITY_LIST
);
246 /* find Capabilities Pointer register */
247 reg_entry
= xen_pt_find_reg(reg_grp_entry
, PCI_CAPABILITY_LIST
);
249 /* check Capabilities Pointer register */
250 if (reg_entry
->data
) {
251 reg_field
|= PCI_STATUS_CAP_LIST
;
253 reg_field
&= ~PCI_STATUS_CAP_LIST
;
256 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTReg*"
257 " for Capabilities Pointer register."
258 " (%s)\n", __func__
);
262 xen_shutdown_fatal_error("Internal error: Couldn't find XenPTRegGroup"
263 " for Header. (%s)\n", __func__
);
270 static int xen_pt_header_type_reg_init(XenPCIPassthroughState
*s
,
271 XenPTRegInfo
*reg
, uint32_t real_offset
,
274 /* read PCI_HEADER_TYPE */
275 *data
= reg
->init_val
| 0x80;
279 /* initialize Interrupt Pin register */
280 static int xen_pt_irqpin_reg_init(XenPCIPassthroughState
*s
,
281 XenPTRegInfo
*reg
, uint32_t real_offset
,
284 *data
= xen_pt_pci_read_intx(s
);
288 /* Command register */
289 static int xen_pt_cmd_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
290 uint16_t *value
, uint16_t valid_mask
)
292 XenPTRegInfo
*reg
= cfg_entry
->reg
;
293 uint16_t valid_emu_mask
= 0;
294 uint16_t emu_mask
= reg
->emu_mask
;
297 emu_mask
|= PCI_COMMAND_MEMORY
;
300 /* emulate word register */
301 valid_emu_mask
= emu_mask
& valid_mask
;
302 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
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
= 0;
313 uint16_t emu_mask
= reg
->emu_mask
;
316 emu_mask
|= PCI_COMMAND_MEMORY
;
319 /* modify emulate register */
320 writable_mask
= ~reg
->ro_mask
& valid_mask
;
321 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
323 /* create value for writing to I/O device register */
324 throughable_mask
= ~emu_mask
& valid_mask
;
326 if (*val
& PCI_COMMAND_INTX_DISABLE
) {
327 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
329 if (s
->machine_irq
) {
330 throughable_mask
|= PCI_COMMAND_INTX_DISABLE
;
334 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
340 #define XEN_PT_BAR_MEM_RO_MASK 0x0000000F /* BAR ReadOnly mask(Memory) */
341 #define XEN_PT_BAR_MEM_EMU_MASK 0xFFFFFFF0 /* BAR emul mask(Memory) */
342 #define XEN_PT_BAR_IO_RO_MASK 0x00000003 /* BAR ReadOnly mask(I/O) */
343 #define XEN_PT_BAR_IO_EMU_MASK 0xFFFFFFFC /* BAR emul mask(I/O) */
345 static XenPTBarFlag
xen_pt_bar_reg_parse(XenPCIPassthroughState
*s
,
348 PCIDevice
*d
= &s
->dev
;
349 XenPTRegion
*region
= NULL
;
353 /* check 64bit BAR */
354 index
= xen_pt_bar_offset_to_index(reg
->offset
);
355 if ((0 < index
) && (index
< PCI_ROM_SLOT
)) {
356 int type
= s
->real_device
.io_regions
[index
- 1].type
;
358 if ((type
& XEN_HOST_PCI_REGION_TYPE_MEM
)
359 && (type
& XEN_HOST_PCI_REGION_TYPE_MEM_64
)) {
360 region
= &s
->bases
[index
- 1];
361 if (region
->bar_flag
!= XEN_PT_BAR_FLAG_UPPER
) {
362 return XEN_PT_BAR_FLAG_UPPER
;
367 /* check unused BAR */
368 r
= &d
->io_regions
[index
];
370 return XEN_PT_BAR_FLAG_UNUSED
;
374 if (index
== PCI_ROM_SLOT
) {
375 return XEN_PT_BAR_FLAG_MEM
;
378 /* check BAR I/O indicator */
379 if (s
->real_device
.io_regions
[index
].type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
380 return XEN_PT_BAR_FLAG_IO
;
382 return XEN_PT_BAR_FLAG_MEM
;
386 static inline uint32_t base_address_with_flags(XenHostPCIIORegion
*hr
)
388 if (hr
->type
& XEN_HOST_PCI_REGION_TYPE_IO
) {
389 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_IO_MASK
);
391 return hr
->base_addr
| (hr
->bus_flags
& ~PCI_BASE_ADDRESS_MEM_MASK
);
395 static int xen_pt_bar_reg_init(XenPCIPassthroughState
*s
, XenPTRegInfo
*reg
,
396 uint32_t real_offset
, uint32_t *data
)
398 uint32_t reg_field
= 0;
401 index
= xen_pt_bar_offset_to_index(reg
->offset
);
402 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
403 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
408 s
->bases
[index
].bar_flag
= xen_pt_bar_reg_parse(s
, reg
);
409 if (s
->bases
[index
].bar_flag
== XEN_PT_BAR_FLAG_UNUSED
) {
410 reg_field
= XEN_PT_INVALID_REG
;
416 static int xen_pt_bar_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
417 uint32_t *value
, uint32_t valid_mask
)
419 XenPTRegInfo
*reg
= cfg_entry
->reg
;
420 uint32_t valid_emu_mask
= 0;
421 uint32_t bar_emu_mask
= 0;
425 index
= xen_pt_bar_offset_to_index(reg
->offset
);
426 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
427 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid BAR index [%d].\n", index
);
431 /* use fixed-up value from kernel sysfs */
432 *value
= base_address_with_flags(&s
->real_device
.io_regions
[index
]);
434 /* set emulate mask depend on BAR flag */
435 switch (s
->bases
[index
].bar_flag
) {
436 case XEN_PT_BAR_FLAG_MEM
:
437 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
439 case XEN_PT_BAR_FLAG_IO
:
440 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
442 case XEN_PT_BAR_FLAG_UPPER
:
443 bar_emu_mask
= XEN_PT_BAR_ALLF
;
450 valid_emu_mask
= bar_emu_mask
& valid_mask
;
451 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
455 static int xen_pt_bar_reg_write(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
456 uint32_t *val
, uint32_t dev_value
,
459 XenPTRegInfo
*reg
= cfg_entry
->reg
;
460 XenPTRegion
*base
= NULL
;
461 PCIDevice
*d
= &s
->dev
;
462 const PCIIORegion
*r
;
463 uint32_t writable_mask
= 0;
464 uint32_t throughable_mask
= 0;
465 uint32_t bar_emu_mask
= 0;
466 uint32_t bar_ro_mask
= 0;
470 index
= xen_pt_bar_offset_to_index(reg
->offset
);
471 if (index
< 0 || index
>= PCI_NUM_REGIONS
) {
472 XEN_PT_ERR(d
, "Internal error: Invalid BAR index [%d].\n", index
);
476 r
= &d
->io_regions
[index
];
477 base
= &s
->bases
[index
];
478 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r
->size
);
480 /* set emulate mask and read-only mask values depend on the BAR flag */
481 switch (s
->bases
[index
].bar_flag
) {
482 case XEN_PT_BAR_FLAG_MEM
:
483 bar_emu_mask
= XEN_PT_BAR_MEM_EMU_MASK
;
484 bar_ro_mask
= XEN_PT_BAR_MEM_RO_MASK
| (r_size
- 1);
486 case XEN_PT_BAR_FLAG_IO
:
487 bar_emu_mask
= XEN_PT_BAR_IO_EMU_MASK
;
488 bar_ro_mask
= XEN_PT_BAR_IO_RO_MASK
| (r_size
- 1);
490 case XEN_PT_BAR_FLAG_UPPER
:
491 bar_emu_mask
= XEN_PT_BAR_ALLF
;
492 bar_ro_mask
= 0; /* all upper 32bit are R/W */
498 /* modify emulate register */
499 writable_mask
= bar_emu_mask
& ~bar_ro_mask
& valid_mask
;
500 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
502 /* check whether we need to update the virtual region address or not */
503 switch (s
->bases
[index
].bar_flag
) {
504 case XEN_PT_BAR_FLAG_MEM
:
507 case XEN_PT_BAR_FLAG_IO
:
510 case XEN_PT_BAR_FLAG_UPPER
:
511 if (cfg_entry
->data
) {
512 if (cfg_entry
->data
!= (XEN_PT_BAR_ALLF
& ~bar_ro_mask
)) {
513 XEN_PT_WARN(d
, "Guest attempt to set high MMIO Base Address. "
515 "(offset: 0x%02x, high address: 0x%08x)\n",
516 reg
->offset
, cfg_entry
->data
);
524 /* create value for writing to I/O device register */
525 throughable_mask
= ~bar_emu_mask
& valid_mask
;
526 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
531 /* write Exp ROM BAR */
532 static int xen_pt_exp_rom_bar_reg_write(XenPCIPassthroughState
*s
,
533 XenPTReg
*cfg_entry
, uint32_t *val
,
534 uint32_t dev_value
, uint32_t valid_mask
)
536 XenPTRegInfo
*reg
= cfg_entry
->reg
;
537 XenPTRegion
*base
= NULL
;
538 PCIDevice
*d
= (PCIDevice
*)&s
->dev
;
539 uint32_t writable_mask
= 0;
540 uint32_t throughable_mask
= 0;
542 uint32_t bar_emu_mask
= 0;
543 uint32_t bar_ro_mask
= 0;
545 r_size
= d
->io_regions
[PCI_ROM_SLOT
].size
;
546 base
= &s
->bases
[PCI_ROM_SLOT
];
547 /* align memory type resource size */
548 r_size
= xen_pt_get_emul_size(base
->bar_flag
, r_size
);
550 /* set emulate mask and read-only mask */
551 bar_emu_mask
= reg
->emu_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 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
558 /* create value for writing to I/O device register */
559 throughable_mask
= ~bar_emu_mask
& valid_mask
;
560 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
565 /* Header Type0 reg static infomation table */
566 static XenPTRegInfo xen_pt_emu_reg_header0
[] = {
569 .offset
= PCI_VENDOR_ID
,
574 .init
= xen_pt_vendor_reg_init
,
575 .u
.w
.read
= xen_pt_word_reg_read
,
576 .u
.w
.write
= xen_pt_word_reg_write
,
580 .offset
= PCI_DEVICE_ID
,
585 .init
= xen_pt_device_reg_init
,
586 .u
.w
.read
= xen_pt_word_reg_read
,
587 .u
.w
.write
= xen_pt_word_reg_write
,
591 .offset
= PCI_COMMAND
,
596 .init
= xen_pt_common_reg_init
,
597 .u
.w
.read
= xen_pt_cmd_reg_read
,
598 .u
.w
.write
= xen_pt_cmd_reg_write
,
600 /* Capabilities Pointer reg */
602 .offset
= PCI_CAPABILITY_LIST
,
607 .init
= xen_pt_ptr_reg_init
,
608 .u
.b
.read
= xen_pt_byte_reg_read
,
609 .u
.b
.write
= xen_pt_byte_reg_write
,
612 /* use emulated Cap Ptr value to initialize,
613 * so need to be declared after Cap Ptr reg
616 .offset
= PCI_STATUS
,
621 .init
= xen_pt_status_reg_init
,
622 .u
.w
.read
= xen_pt_word_reg_read
,
623 .u
.w
.write
= xen_pt_word_reg_write
,
625 /* Cache Line Size reg */
627 .offset
= PCI_CACHE_LINE_SIZE
,
632 .init
= xen_pt_common_reg_init
,
633 .u
.b
.read
= xen_pt_byte_reg_read
,
634 .u
.b
.write
= xen_pt_byte_reg_write
,
636 /* Latency Timer reg */
638 .offset
= PCI_LATENCY_TIMER
,
643 .init
= xen_pt_common_reg_init
,
644 .u
.b
.read
= xen_pt_byte_reg_read
,
645 .u
.b
.write
= xen_pt_byte_reg_write
,
647 /* Header Type reg */
649 .offset
= PCI_HEADER_TYPE
,
654 .init
= xen_pt_header_type_reg_init
,
655 .u
.b
.read
= xen_pt_byte_reg_read
,
656 .u
.b
.write
= xen_pt_byte_reg_write
,
658 /* Interrupt Line reg */
660 .offset
= PCI_INTERRUPT_LINE
,
665 .init
= xen_pt_common_reg_init
,
666 .u
.b
.read
= xen_pt_byte_reg_read
,
667 .u
.b
.write
= xen_pt_byte_reg_write
,
669 /* Interrupt Pin reg */
671 .offset
= PCI_INTERRUPT_PIN
,
676 .init
= xen_pt_irqpin_reg_init
,
677 .u
.b
.read
= xen_pt_byte_reg_read
,
678 .u
.b
.write
= xen_pt_byte_reg_write
,
681 /* mask of BAR need to be decided later, depends on IO/MEM type */
683 .offset
= PCI_BASE_ADDRESS_0
,
685 .init_val
= 0x00000000,
686 .init
= xen_pt_bar_reg_init
,
687 .u
.dw
.read
= xen_pt_bar_reg_read
,
688 .u
.dw
.write
= xen_pt_bar_reg_write
,
692 .offset
= PCI_BASE_ADDRESS_1
,
694 .init_val
= 0x00000000,
695 .init
= xen_pt_bar_reg_init
,
696 .u
.dw
.read
= xen_pt_bar_reg_read
,
697 .u
.dw
.write
= xen_pt_bar_reg_write
,
701 .offset
= PCI_BASE_ADDRESS_2
,
703 .init_val
= 0x00000000,
704 .init
= xen_pt_bar_reg_init
,
705 .u
.dw
.read
= xen_pt_bar_reg_read
,
706 .u
.dw
.write
= xen_pt_bar_reg_write
,
710 .offset
= PCI_BASE_ADDRESS_3
,
712 .init_val
= 0x00000000,
713 .init
= xen_pt_bar_reg_init
,
714 .u
.dw
.read
= xen_pt_bar_reg_read
,
715 .u
.dw
.write
= xen_pt_bar_reg_write
,
719 .offset
= PCI_BASE_ADDRESS_4
,
721 .init_val
= 0x00000000,
722 .init
= xen_pt_bar_reg_init
,
723 .u
.dw
.read
= xen_pt_bar_reg_read
,
724 .u
.dw
.write
= xen_pt_bar_reg_write
,
728 .offset
= PCI_BASE_ADDRESS_5
,
730 .init_val
= 0x00000000,
731 .init
= xen_pt_bar_reg_init
,
732 .u
.dw
.read
= xen_pt_bar_reg_read
,
733 .u
.dw
.write
= xen_pt_bar_reg_write
,
735 /* Expansion ROM BAR reg */
737 .offset
= PCI_ROM_ADDRESS
,
739 .init_val
= 0x00000000,
740 .ro_mask
= 0x000007FE,
741 .emu_mask
= 0xFFFFF800,
742 .init
= xen_pt_bar_reg_init
,
743 .u
.dw
.read
= xen_pt_long_reg_read
,
744 .u
.dw
.write
= xen_pt_exp_rom_bar_reg_write
,
752 /*********************************
753 * Vital Product Data Capability
756 /* Vital Product Data Capability Structure reg static infomation table */
757 static XenPTRegInfo xen_pt_emu_reg_vpd
[] = {
759 .offset
= PCI_CAP_LIST_NEXT
,
764 .init
= xen_pt_ptr_reg_init
,
765 .u
.b
.read
= xen_pt_byte_reg_read
,
766 .u
.b
.write
= xen_pt_byte_reg_write
,
774 /**************************************
775 * Vendor Specific Capability
778 /* Vendor Specific Capability Structure reg static infomation table */
779 static XenPTRegInfo xen_pt_emu_reg_vendor
[] = {
781 .offset
= PCI_CAP_LIST_NEXT
,
786 .init
= xen_pt_ptr_reg_init
,
787 .u
.b
.read
= xen_pt_byte_reg_read
,
788 .u
.b
.write
= xen_pt_byte_reg_write
,
796 /*****************************
797 * PCI Express Capability
800 static inline uint8_t get_capability_version(XenPCIPassthroughState
*s
,
803 uint8_t flags
= pci_get_byte(s
->dev
.config
+ offset
+ PCI_EXP_FLAGS
);
804 return flags
& PCI_EXP_FLAGS_VERS
;
807 static inline uint8_t get_device_type(XenPCIPassthroughState
*s
,
810 uint8_t flags
= pci_get_byte(s
->dev
.config
+ offset
+ PCI_EXP_FLAGS
);
811 return (flags
& PCI_EXP_FLAGS_TYPE
) >> 4;
814 /* initialize Link Control register */
815 static int xen_pt_linkctrl_reg_init(XenPCIPassthroughState
*s
,
816 XenPTRegInfo
*reg
, uint32_t real_offset
,
819 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
820 uint8_t dev_type
= get_device_type(s
, real_offset
- reg
->offset
);
822 /* no need to initialize in case of Root Complex Integrated Endpoint
825 if ((dev_type
== PCI_EXP_TYPE_RC_END
) && (cap_ver
== 1)) {
826 *data
= XEN_PT_INVALID_REG
;
829 *data
= reg
->init_val
;
832 /* initialize Device Control 2 register */
833 static int xen_pt_devctrl2_reg_init(XenPCIPassthroughState
*s
,
834 XenPTRegInfo
*reg
, uint32_t real_offset
,
837 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
839 /* no need to initialize in case of cap_ver 1.x */
841 *data
= XEN_PT_INVALID_REG
;
844 *data
= reg
->init_val
;
847 /* initialize Link Control 2 register */
848 static int xen_pt_linkctrl2_reg_init(XenPCIPassthroughState
*s
,
849 XenPTRegInfo
*reg
, uint32_t real_offset
,
852 uint8_t cap_ver
= get_capability_version(s
, real_offset
- reg
->offset
);
853 uint32_t reg_field
= 0;
855 /* no need to initialize in case of cap_ver 1.x */
857 reg_field
= XEN_PT_INVALID_REG
;
859 /* set Supported Link Speed */
860 uint8_t lnkcap
= pci_get_byte(s
->dev
.config
+ real_offset
- reg
->offset
862 reg_field
|= PCI_EXP_LNKCAP_SLS
& lnkcap
;
869 /* PCI Express Capability Structure reg static infomation table */
870 static XenPTRegInfo xen_pt_emu_reg_pcie
[] = {
871 /* Next Pointer reg */
873 .offset
= PCI_CAP_LIST_NEXT
,
878 .init
= xen_pt_ptr_reg_init
,
879 .u
.b
.read
= xen_pt_byte_reg_read
,
880 .u
.b
.write
= xen_pt_byte_reg_write
,
882 /* Device Capabilities reg */
884 .offset
= PCI_EXP_DEVCAP
,
886 .init_val
= 0x00000000,
887 .ro_mask
= 0x1FFCFFFF,
888 .emu_mask
= 0x10000000,
889 .init
= xen_pt_common_reg_init
,
890 .u
.dw
.read
= xen_pt_long_reg_read
,
891 .u
.dw
.write
= xen_pt_long_reg_write
,
893 /* Device Control reg */
895 .offset
= PCI_EXP_DEVCTL
,
900 .init
= xen_pt_common_reg_init
,
901 .u
.w
.read
= xen_pt_word_reg_read
,
902 .u
.w
.write
= xen_pt_word_reg_write
,
904 /* Link Control reg */
906 .offset
= PCI_EXP_LNKCTL
,
911 .init
= xen_pt_linkctrl_reg_init
,
912 .u
.w
.read
= xen_pt_word_reg_read
,
913 .u
.w
.write
= xen_pt_word_reg_write
,
915 /* Device Control 2 reg */
922 .init
= xen_pt_devctrl2_reg_init
,
923 .u
.w
.read
= xen_pt_word_reg_read
,
924 .u
.w
.write
= xen_pt_word_reg_write
,
926 /* Link Control 2 reg */
933 .init
= xen_pt_linkctrl2_reg_init
,
934 .u
.w
.read
= xen_pt_word_reg_read
,
935 .u
.w
.write
= xen_pt_word_reg_write
,
943 /*********************************
944 * Power Management Capability
947 /* read Power Management Control/Status register */
948 static int xen_pt_pmcsr_reg_read(XenPCIPassthroughState
*s
, XenPTReg
*cfg_entry
,
949 uint16_t *value
, uint16_t valid_mask
)
951 XenPTRegInfo
*reg
= cfg_entry
->reg
;
952 uint16_t valid_emu_mask
= reg
->emu_mask
;
954 valid_emu_mask
|= PCI_PM_CTRL_STATE_MASK
| PCI_PM_CTRL_NO_SOFT_RESET
;
956 valid_emu_mask
= valid_emu_mask
& valid_mask
;
957 *value
= XEN_PT_MERGE_VALUE(*value
, cfg_entry
->data
, ~valid_emu_mask
);
961 /* write Power Management Control/Status register */
962 static int xen_pt_pmcsr_reg_write(XenPCIPassthroughState
*s
,
963 XenPTReg
*cfg_entry
, uint16_t *val
,
964 uint16_t dev_value
, uint16_t valid_mask
)
966 XenPTRegInfo
*reg
= cfg_entry
->reg
;
967 uint16_t emu_mask
= reg
->emu_mask
;
968 uint16_t writable_mask
= 0;
969 uint16_t throughable_mask
= 0;
971 emu_mask
|= PCI_PM_CTRL_STATE_MASK
| PCI_PM_CTRL_NO_SOFT_RESET
;
973 /* modify emulate register */
974 writable_mask
= emu_mask
& ~reg
->ro_mask
& valid_mask
;
975 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
977 /* create value for writing to I/O device register */
978 throughable_mask
= ~emu_mask
& valid_mask
;
979 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
984 /* Power Management Capability reg static infomation table */
985 static XenPTRegInfo xen_pt_emu_reg_pm
[] = {
986 /* Next Pointer reg */
988 .offset
= PCI_CAP_LIST_NEXT
,
993 .init
= xen_pt_ptr_reg_init
,
994 .u
.b
.read
= xen_pt_byte_reg_read
,
995 .u
.b
.write
= xen_pt_byte_reg_write
,
997 /* Power Management Capabilities reg */
999 .offset
= PCI_CAP_FLAGS
,
1004 .init
= xen_pt_common_reg_init
,
1005 .u
.w
.read
= xen_pt_word_reg_read
,
1006 .u
.w
.write
= xen_pt_word_reg_write
,
1008 /* PCI Power Management Control/Status reg */
1010 .offset
= PCI_PM_CTRL
,
1015 .init
= xen_pt_common_reg_init
,
1016 .u
.w
.read
= xen_pt_pmcsr_reg_read
,
1017 .u
.w
.write
= xen_pt_pmcsr_reg_write
,
1025 /********************************
1030 static bool xen_pt_msgdata_check_type(uint32_t offset
, uint16_t flags
)
1032 /* check the offset whether matches the type or not */
1033 bool is_32
= (offset
== PCI_MSI_DATA_32
) && !(flags
& PCI_MSI_FLAGS_64BIT
);
1034 bool is_64
= (offset
== PCI_MSI_DATA_64
) && (flags
& PCI_MSI_FLAGS_64BIT
);
1035 return is_32
|| is_64
;
1038 /* Message Control register */
1039 static int xen_pt_msgctrl_reg_init(XenPCIPassthroughState
*s
,
1040 XenPTRegInfo
*reg
, uint32_t real_offset
,
1043 PCIDevice
*d
= &s
->dev
;
1044 XenPTMSI
*msi
= s
->msi
;
1045 uint16_t reg_field
= 0;
1047 /* use I/O device register's value as initial value */
1048 reg_field
= pci_get_word(d
->config
+ real_offset
);
1050 if (reg_field
& PCI_MSI_FLAGS_ENABLE
) {
1051 XEN_PT_LOG(&s
->dev
, "MSI already enabled, disabling it first\n");
1052 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1053 reg_field
& ~PCI_MSI_FLAGS_ENABLE
);
1055 msi
->flags
|= reg_field
;
1056 msi
->ctrl_offset
= real_offset
;
1057 msi
->initialized
= false;
1058 msi
->mapped
= false;
1060 *data
= reg
->init_val
;
1063 static int xen_pt_msgctrl_reg_write(XenPCIPassthroughState
*s
,
1064 XenPTReg
*cfg_entry
, uint16_t *val
,
1065 uint16_t dev_value
, uint16_t valid_mask
)
1067 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1068 XenPTMSI
*msi
= s
->msi
;
1069 uint16_t writable_mask
= 0;
1070 uint16_t throughable_mask
= 0;
1073 /* Currently no support for multi-vector */
1074 if (*val
& PCI_MSI_FLAGS_QSIZE
) {
1075 XEN_PT_WARN(&s
->dev
, "Tries to set more than 1 vector ctrl %x\n", *val
);
1078 /* modify emulate register */
1079 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1080 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
1081 msi
->flags
|= cfg_entry
->data
& ~PCI_MSI_FLAGS_ENABLE
;
1083 /* create value for writing to I/O device register */
1085 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
1086 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1089 if (raw_val
& PCI_MSI_FLAGS_ENABLE
) {
1090 /* setup MSI pirq for the first time */
1091 if (!msi
->initialized
) {
1092 /* Init physical one */
1093 XEN_PT_LOG(&s
->dev
, "setup MSI\n");
1094 if (xen_pt_msi_setup(s
)) {
1095 /* We do not broadcast the error to the framework code, so
1096 * that MSI errors are contained in MSI emulation code and
1097 * QEMU can go on running.
1098 * Guest MSI would be actually not working.
1100 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1101 XEN_PT_WARN(&s
->dev
, "Can not map MSI.\n");
1104 if (xen_pt_msi_update(s
)) {
1105 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1106 XEN_PT_WARN(&s
->dev
, "Can not bind MSI\n");
1109 msi
->initialized
= true;
1112 msi
->flags
|= PCI_MSI_FLAGS_ENABLE
;
1114 msi
->flags
&= ~PCI_MSI_FLAGS_ENABLE
;
1117 /* pass through MSI_ENABLE bit */
1118 *val
&= ~PCI_MSI_FLAGS_ENABLE
;
1119 *val
|= raw_val
& PCI_MSI_FLAGS_ENABLE
;
1124 /* initialize Message Upper Address register */
1125 static int xen_pt_msgaddr64_reg_init(XenPCIPassthroughState
*s
,
1126 XenPTRegInfo
*reg
, uint32_t real_offset
,
1129 /* no need to initialize in case of 32 bit type */
1130 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1131 *data
= XEN_PT_INVALID_REG
;
1133 *data
= reg
->init_val
;
1138 /* this function will be called twice (for 32 bit and 64 bit type) */
1139 /* initialize Message Data register */
1140 static int xen_pt_msgdata_reg_init(XenPCIPassthroughState
*s
,
1141 XenPTRegInfo
*reg
, uint32_t real_offset
,
1144 uint32_t flags
= s
->msi
->flags
;
1145 uint32_t offset
= reg
->offset
;
1147 /* check the offset whether matches the type or not */
1148 if (xen_pt_msgdata_check_type(offset
, flags
)) {
1149 *data
= reg
->init_val
;
1151 *data
= XEN_PT_INVALID_REG
;
1156 /* write Message Address register */
1157 static int xen_pt_msgaddr32_reg_write(XenPCIPassthroughState
*s
,
1158 XenPTReg
*cfg_entry
, uint32_t *val
,
1159 uint32_t dev_value
, uint32_t valid_mask
)
1161 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1162 uint32_t writable_mask
= 0;
1163 uint32_t throughable_mask
= 0;
1164 uint32_t old_addr
= cfg_entry
->data
;
1166 /* modify emulate register */
1167 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1168 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
1169 s
->msi
->addr_lo
= cfg_entry
->data
;
1171 /* create value for writing to I/O device register */
1172 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
1173 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1176 if (cfg_entry
->data
!= old_addr
) {
1177 if (s
->msi
->mapped
) {
1178 xen_pt_msi_update(s
);
1184 /* write Message Upper Address register */
1185 static int xen_pt_msgaddr64_reg_write(XenPCIPassthroughState
*s
,
1186 XenPTReg
*cfg_entry
, uint32_t *val
,
1187 uint32_t dev_value
, uint32_t valid_mask
)
1189 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1190 uint32_t writable_mask
= 0;
1191 uint32_t throughable_mask
= 0;
1192 uint32_t old_addr
= cfg_entry
->data
;
1194 /* check whether the type is 64 bit or not */
1195 if (!(s
->msi
->flags
& PCI_MSI_FLAGS_64BIT
)) {
1197 "Can't write to the upper address without 64 bit support\n");
1201 /* modify emulate register */
1202 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1203 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
1204 /* update the msi_info too */
1205 s
->msi
->addr_hi
= cfg_entry
->data
;
1207 /* create value for writing to I/O device register */
1208 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
1209 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1212 if (cfg_entry
->data
!= old_addr
) {
1213 if (s
->msi
->mapped
) {
1214 xen_pt_msi_update(s
);
1222 /* this function will be called twice (for 32 bit and 64 bit type) */
1223 /* write Message Data register */
1224 static int xen_pt_msgdata_reg_write(XenPCIPassthroughState
*s
,
1225 XenPTReg
*cfg_entry
, uint16_t *val
,
1226 uint16_t dev_value
, uint16_t valid_mask
)
1228 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1229 XenPTMSI
*msi
= s
->msi
;
1230 uint16_t writable_mask
= 0;
1231 uint16_t throughable_mask
= 0;
1232 uint16_t old_data
= cfg_entry
->data
;
1233 uint32_t offset
= reg
->offset
;
1235 /* check the offset whether matches the type or not */
1236 if (!xen_pt_msgdata_check_type(offset
, msi
->flags
)) {
1237 /* exit I/O emulator */
1238 XEN_PT_ERR(&s
->dev
, "the offset does not match the 32/64 bit type!\n");
1242 /* modify emulate register */
1243 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1244 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
1245 /* update the msi_info too */
1246 msi
->data
= cfg_entry
->data
;
1248 /* create value for writing to I/O device register */
1249 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
1250 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1253 if (cfg_entry
->data
!= old_data
) {
1255 xen_pt_msi_update(s
);
1262 /* MSI Capability Structure reg static infomation table */
1263 static XenPTRegInfo xen_pt_emu_reg_msi
[] = {
1264 /* Next Pointer reg */
1266 .offset
= PCI_CAP_LIST_NEXT
,
1271 .init
= xen_pt_ptr_reg_init
,
1272 .u
.b
.read
= xen_pt_byte_reg_read
,
1273 .u
.b
.write
= xen_pt_byte_reg_write
,
1275 /* Message Control reg */
1277 .offset
= PCI_MSI_FLAGS
,
1282 .init
= xen_pt_msgctrl_reg_init
,
1283 .u
.w
.read
= xen_pt_word_reg_read
,
1284 .u
.w
.write
= xen_pt_msgctrl_reg_write
,
1286 /* Message Address reg */
1288 .offset
= PCI_MSI_ADDRESS_LO
,
1290 .init_val
= 0x00000000,
1291 .ro_mask
= 0x00000003,
1292 .emu_mask
= 0xFFFFFFFF,
1294 .init
= xen_pt_common_reg_init
,
1295 .u
.dw
.read
= xen_pt_long_reg_read
,
1296 .u
.dw
.write
= xen_pt_msgaddr32_reg_write
,
1298 /* Message Upper Address reg (if PCI_MSI_FLAGS_64BIT set) */
1300 .offset
= PCI_MSI_ADDRESS_HI
,
1302 .init_val
= 0x00000000,
1303 .ro_mask
= 0x00000000,
1304 .emu_mask
= 0xFFFFFFFF,
1306 .init
= xen_pt_msgaddr64_reg_init
,
1307 .u
.dw
.read
= xen_pt_long_reg_read
,
1308 .u
.dw
.write
= xen_pt_msgaddr64_reg_write
,
1310 /* Message Data reg (16 bits of data for 32-bit devices) */
1312 .offset
= PCI_MSI_DATA_32
,
1318 .init
= xen_pt_msgdata_reg_init
,
1319 .u
.w
.read
= xen_pt_word_reg_read
,
1320 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1322 /* Message Data reg (16 bits of data for 64-bit devices) */
1324 .offset
= PCI_MSI_DATA_64
,
1330 .init
= xen_pt_msgdata_reg_init
,
1331 .u
.w
.read
= xen_pt_word_reg_read
,
1332 .u
.w
.write
= xen_pt_msgdata_reg_write
,
1340 /**************************************
1344 /* Message Control register for MSI-X */
1345 static int xen_pt_msixctrl_reg_init(XenPCIPassthroughState
*s
,
1346 XenPTRegInfo
*reg
, uint32_t real_offset
,
1349 PCIDevice
*d
= &s
->dev
;
1350 uint16_t reg_field
= 0;
1352 /* use I/O device register's value as initial value */
1353 reg_field
= pci_get_word(d
->config
+ real_offset
);
1355 if (reg_field
& PCI_MSIX_FLAGS_ENABLE
) {
1356 XEN_PT_LOG(d
, "MSIX already enabled, disabling it first\n");
1357 xen_host_pci_set_word(&s
->real_device
, real_offset
,
1358 reg_field
& ~PCI_MSIX_FLAGS_ENABLE
);
1361 s
->msix
->ctrl_offset
= real_offset
;
1363 *data
= reg
->init_val
;
1366 static int xen_pt_msixctrl_reg_write(XenPCIPassthroughState
*s
,
1367 XenPTReg
*cfg_entry
, uint16_t *val
,
1368 uint16_t dev_value
, uint16_t valid_mask
)
1370 XenPTRegInfo
*reg
= cfg_entry
->reg
;
1371 uint16_t writable_mask
= 0;
1372 uint16_t throughable_mask
= 0;
1373 int debug_msix_enabled_old
;
1375 /* modify emulate register */
1376 writable_mask
= reg
->emu_mask
& ~reg
->ro_mask
& valid_mask
;
1377 cfg_entry
->data
= XEN_PT_MERGE_VALUE(*val
, cfg_entry
->data
, writable_mask
);
1379 /* create value for writing to I/O device register */
1380 throughable_mask
= ~reg
->emu_mask
& valid_mask
;
1381 *val
= XEN_PT_MERGE_VALUE(*val
, dev_value
, throughable_mask
);
1384 if ((*val
& PCI_MSIX_FLAGS_ENABLE
)
1385 && !(*val
& PCI_MSIX_FLAGS_MASKALL
)) {
1386 xen_pt_msix_update(s
);
1389 debug_msix_enabled_old
= s
->msix
->enabled
;
1390 s
->msix
->enabled
= !!(*val
& PCI_MSIX_FLAGS_ENABLE
);
1391 if (s
->msix
->enabled
!= debug_msix_enabled_old
) {
1392 XEN_PT_LOG(&s
->dev
, "%s MSI-X\n",
1393 s
->msix
->enabled
? "enable" : "disable");
1399 /* MSI-X Capability Structure reg static infomation table */
1400 static XenPTRegInfo xen_pt_emu_reg_msix
[] = {
1401 /* Next Pointer reg */
1403 .offset
= PCI_CAP_LIST_NEXT
,
1408 .init
= xen_pt_ptr_reg_init
,
1409 .u
.b
.read
= xen_pt_byte_reg_read
,
1410 .u
.b
.write
= xen_pt_byte_reg_write
,
1412 /* Message Control reg */
1414 .offset
= PCI_MSI_FLAGS
,
1419 .init
= xen_pt_msixctrl_reg_init
,
1420 .u
.w
.read
= xen_pt_word_reg_read
,
1421 .u
.w
.write
= xen_pt_msixctrl_reg_write
,
1429 /****************************
1433 /* capability structure register group size functions */
1435 static int xen_pt_reg_grp_size_init(XenPCIPassthroughState
*s
,
1436 const XenPTRegGroupInfo
*grp_reg
,
1437 uint32_t base_offset
, uint8_t *size
)
1439 *size
= grp_reg
->grp_size
;
1442 /* get Vendor Specific Capability Structure register group size */
1443 static int xen_pt_vendor_size_init(XenPCIPassthroughState
*s
,
1444 const XenPTRegGroupInfo
*grp_reg
,
1445 uint32_t base_offset
, uint8_t *size
)
1447 *size
= pci_get_byte(s
->dev
.config
+ base_offset
+ 0x02);
1450 /* get PCI Express Capability Structure register group size */
1451 static int xen_pt_pcie_size_init(XenPCIPassthroughState
*s
,
1452 const XenPTRegGroupInfo
*grp_reg
,
1453 uint32_t base_offset
, uint8_t *size
)
1455 PCIDevice
*d
= &s
->dev
;
1456 uint8_t version
= get_capability_version(s
, base_offset
);
1457 uint8_t type
= get_device_type(s
, base_offset
);
1458 uint8_t pcie_size
= 0;
1461 /* calculate size depend on capability version and device/port type */
1462 /* in case of PCI Express Base Specification Rev 1.x */
1464 /* The PCI Express Capabilities, Device Capabilities, and Device
1465 * Status/Control registers are required for all PCI Express devices.
1466 * The Link Capabilities and Link Status/Control are required for all
1467 * Endpoints that are not Root Complex Integrated Endpoints. Endpoints
1468 * are not required to implement registers other than those listed
1469 * above and terminate the capability structure.
1472 case PCI_EXP_TYPE_ENDPOINT
:
1473 case PCI_EXP_TYPE_LEG_END
:
1476 case PCI_EXP_TYPE_RC_END
:
1480 /* only EndPoint passthrough is supported */
1481 case PCI_EXP_TYPE_ROOT_PORT
:
1482 case PCI_EXP_TYPE_UPSTREAM
:
1483 case PCI_EXP_TYPE_DOWNSTREAM
:
1484 case PCI_EXP_TYPE_PCI_BRIDGE
:
1485 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1486 case PCI_EXP_TYPE_RC_EC
:
1488 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1492 /* in case of PCI Express Base Specification Rev 2.0 */
1493 else if (version
== 2) {
1495 case PCI_EXP_TYPE_ENDPOINT
:
1496 case PCI_EXP_TYPE_LEG_END
:
1497 case PCI_EXP_TYPE_RC_END
:
1498 /* For Functions that do not implement the registers,
1499 * these spaces must be hardwired to 0b.
1503 /* only EndPoint passthrough is supported */
1504 case PCI_EXP_TYPE_ROOT_PORT
:
1505 case PCI_EXP_TYPE_UPSTREAM
:
1506 case PCI_EXP_TYPE_DOWNSTREAM
:
1507 case PCI_EXP_TYPE_PCI_BRIDGE
:
1508 case PCI_EXP_TYPE_PCIE_BRIDGE
:
1509 case PCI_EXP_TYPE_RC_EC
:
1511 XEN_PT_ERR(d
, "Unsupported device/port type %#x.\n", type
);
1515 XEN_PT_ERR(d
, "Unsupported capability version %#x.\n", version
);
1522 /* get MSI Capability Structure register group size */
1523 static int xen_pt_msi_size_init(XenPCIPassthroughState
*s
,
1524 const XenPTRegGroupInfo
*grp_reg
,
1525 uint32_t base_offset
, uint8_t *size
)
1527 PCIDevice
*d
= &s
->dev
;
1528 uint16_t msg_ctrl
= 0;
1529 uint8_t msi_size
= 0xa;
1531 msg_ctrl
= pci_get_word(d
->config
+ (base_offset
+ PCI_MSI_FLAGS
));
1533 /* check if 64-bit address is capable of per-vector masking */
1534 if (msg_ctrl
& PCI_MSI_FLAGS_64BIT
) {
1537 if (msg_ctrl
& PCI_MSI_FLAGS_MASKBIT
) {
1541 s
->msi
= g_new0(XenPTMSI
, 1);
1542 s
->msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
1547 /* get MSI-X Capability Structure register group size */
1548 static int xen_pt_msix_size_init(XenPCIPassthroughState
*s
,
1549 const XenPTRegGroupInfo
*grp_reg
,
1550 uint32_t base_offset
, uint8_t *size
)
1554 rc
= xen_pt_msix_init(s
, base_offset
);
1557 XEN_PT_ERR(&s
->dev
, "Internal error: Invalid xen_pt_msix_init.\n");
1561 *size
= grp_reg
->grp_size
;
1566 static const XenPTRegGroupInfo xen_pt_emu_reg_grps
[] = {
1567 /* Header Type0 reg group */
1570 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1572 .size_init
= xen_pt_reg_grp_size_init
,
1573 .emu_regs
= xen_pt_emu_reg_header0
,
1575 /* PCI PowerManagement Capability reg group */
1577 .grp_id
= PCI_CAP_ID_PM
,
1578 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1579 .grp_size
= PCI_PM_SIZEOF
,
1580 .size_init
= xen_pt_reg_grp_size_init
,
1581 .emu_regs
= xen_pt_emu_reg_pm
,
1583 /* AGP Capability Structure reg group */
1585 .grp_id
= PCI_CAP_ID_AGP
,
1586 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1588 .size_init
= xen_pt_reg_grp_size_init
,
1590 /* Vital Product Data Capability Structure reg group */
1592 .grp_id
= PCI_CAP_ID_VPD
,
1593 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1595 .size_init
= xen_pt_reg_grp_size_init
,
1596 .emu_regs
= xen_pt_emu_reg_vpd
,
1598 /* Slot Identification reg group */
1600 .grp_id
= PCI_CAP_ID_SLOTID
,
1601 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1603 .size_init
= xen_pt_reg_grp_size_init
,
1605 /* MSI Capability Structure reg group */
1607 .grp_id
= PCI_CAP_ID_MSI
,
1608 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1610 .size_init
= xen_pt_msi_size_init
,
1611 .emu_regs
= xen_pt_emu_reg_msi
,
1613 /* PCI-X Capabilities List Item reg group */
1615 .grp_id
= PCI_CAP_ID_PCIX
,
1616 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1618 .size_init
= xen_pt_reg_grp_size_init
,
1620 /* Vendor Specific Capability Structure reg group */
1622 .grp_id
= PCI_CAP_ID_VNDR
,
1623 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1625 .size_init
= xen_pt_vendor_size_init
,
1626 .emu_regs
= xen_pt_emu_reg_vendor
,
1628 /* SHPC Capability List Item reg group */
1630 .grp_id
= PCI_CAP_ID_SHPC
,
1631 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1633 .size_init
= xen_pt_reg_grp_size_init
,
1635 /* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */
1637 .grp_id
= PCI_CAP_ID_SSVID
,
1638 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1640 .size_init
= xen_pt_reg_grp_size_init
,
1642 /* AGP 8x Capability Structure reg group */
1644 .grp_id
= PCI_CAP_ID_AGP3
,
1645 .grp_type
= XEN_PT_GRP_TYPE_HARDWIRED
,
1647 .size_init
= xen_pt_reg_grp_size_init
,
1649 /* PCI Express Capability Structure reg group */
1651 .grp_id
= PCI_CAP_ID_EXP
,
1652 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1654 .size_init
= xen_pt_pcie_size_init
,
1655 .emu_regs
= xen_pt_emu_reg_pcie
,
1657 /* MSI-X Capability Structure reg group */
1659 .grp_id
= PCI_CAP_ID_MSIX
,
1660 .grp_type
= XEN_PT_GRP_TYPE_EMU
,
1662 .size_init
= xen_pt_msix_size_init
,
1663 .emu_regs
= xen_pt_emu_reg_msix
,
1670 /* initialize Capabilities Pointer or Next Pointer register */
1671 static int xen_pt_ptr_reg_init(XenPCIPassthroughState
*s
,
1672 XenPTRegInfo
*reg
, uint32_t real_offset
,
1676 uint8_t *config
= s
->dev
.config
;
1677 uint32_t reg_field
= pci_get_byte(config
+ real_offset
);
1680 /* find capability offset */
1682 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1683 if (xen_pt_hide_dev_cap(&s
->real_device
,
1684 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1688 cap_id
= pci_get_byte(config
+ reg_field
+ PCI_CAP_LIST_ID
);
1689 if (xen_pt_emu_reg_grps
[i
].grp_id
== cap_id
) {
1690 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1693 /* ignore the 0 hardwired capability, find next one */
1698 /* next capability */
1699 reg_field
= pci_get_byte(config
+ reg_field
+ PCI_CAP_LIST_NEXT
);
1712 static uint8_t find_cap_offset(XenPCIPassthroughState
*s
, uint8_t cap
)
1715 unsigned max_cap
= PCI_CAP_MAX
;
1716 uint8_t pos
= PCI_CAPABILITY_LIST
;
1719 if (xen_host_pci_get_byte(&s
->real_device
, PCI_STATUS
, &status
)) {
1722 if ((status
& PCI_STATUS_CAP_LIST
) == 0) {
1727 if (xen_host_pci_get_byte(&s
->real_device
, pos
, &pos
)) {
1730 if (pos
< PCI_CONFIG_HEADER_SIZE
) {
1735 if (xen_host_pci_get_byte(&s
->real_device
,
1736 pos
+ PCI_CAP_LIST_ID
, &id
)) {
1747 pos
+= PCI_CAP_LIST_NEXT
;
1752 static int xen_pt_config_reg_init(XenPCIPassthroughState
*s
,
1753 XenPTRegGroup
*reg_grp
, XenPTRegInfo
*reg
)
1755 XenPTReg
*reg_entry
;
1759 reg_entry
= g_new0(XenPTReg
, 1);
1760 reg_entry
->reg
= reg
;
1763 /* initialize emulate register */
1764 rc
= reg
->init(s
, reg_entry
->reg
,
1765 reg_grp
->base_offset
+ reg
->offset
, &data
);
1770 if (data
== XEN_PT_INVALID_REG
) {
1771 /* free unused BAR register entry */
1775 /* set register value */
1776 reg_entry
->data
= data
;
1778 /* list add register entry */
1779 QLIST_INSERT_HEAD(®_grp
->reg_tbl_list
, reg_entry
, entries
);
1784 int xen_pt_config_init(XenPCIPassthroughState
*s
)
1788 QLIST_INIT(&s
->reg_grps
);
1790 for (i
= 0; xen_pt_emu_reg_grps
[i
].grp_size
!= 0; i
++) {
1791 uint32_t reg_grp_offset
= 0;
1792 XenPTRegGroup
*reg_grp_entry
= NULL
;
1794 if (xen_pt_emu_reg_grps
[i
].grp_id
!= 0xFF) {
1795 if (xen_pt_hide_dev_cap(&s
->real_device
,
1796 xen_pt_emu_reg_grps
[i
].grp_id
)) {
1800 reg_grp_offset
= find_cap_offset(s
, xen_pt_emu_reg_grps
[i
].grp_id
);
1802 if (!reg_grp_offset
) {
1807 reg_grp_entry
= g_new0(XenPTRegGroup
, 1);
1808 QLIST_INIT(®_grp_entry
->reg_tbl_list
);
1809 QLIST_INSERT_HEAD(&s
->reg_grps
, reg_grp_entry
, entries
);
1811 reg_grp_entry
->base_offset
= reg_grp_offset
;
1812 reg_grp_entry
->reg_grp
= xen_pt_emu_reg_grps
+ i
;
1813 if (xen_pt_emu_reg_grps
[i
].size_init
) {
1814 /* get register group size */
1815 rc
= xen_pt_emu_reg_grps
[i
].size_init(s
, reg_grp_entry
->reg_grp
,
1817 ®_grp_entry
->size
);
1819 xen_pt_config_delete(s
);
1824 if (xen_pt_emu_reg_grps
[i
].grp_type
== XEN_PT_GRP_TYPE_EMU
) {
1825 if (xen_pt_emu_reg_grps
[i
].emu_regs
) {
1827 XenPTRegInfo
*regs
= xen_pt_emu_reg_grps
[i
].emu_regs
;
1828 /* initialize capability register */
1829 for (j
= 0; regs
->size
!= 0; j
++, regs
++) {
1830 /* initialize capability register */
1831 rc
= xen_pt_config_reg_init(s
, reg_grp_entry
, regs
);
1833 xen_pt_config_delete(s
);
1844 /* delete all emulate register */
1845 void xen_pt_config_delete(XenPCIPassthroughState
*s
)
1847 struct XenPTRegGroup
*reg_group
, *next_grp
;
1848 struct XenPTReg
*reg
, *next_reg
;
1850 /* free MSI/MSI-X info table */
1852 xen_pt_msix_delete(s
);
1858 /* free all register group entry */
1859 QLIST_FOREACH_SAFE(reg_group
, &s
->reg_grps
, entries
, next_grp
) {
1860 /* free all register entry */
1861 QLIST_FOREACH_SAFE(reg
, ®_group
->reg_tbl_list
, entries
, next_reg
) {
1862 QLIST_REMOVE(reg
, entries
);
1866 QLIST_REMOVE(reg_group
, entries
);