2 * Copyright (c) 2007, Intel Corporation.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
7 * Jiang Yunhong <yunhong.jiang@intel.com>
9 * This file implements direct PCI assignment to a HVM guest
14 #include "xen_backend.h"
16 #include "apic-msidef.h"
19 #define XEN_PT_AUTO_ASSIGN -1
21 /* shift count for gflags */
22 #define XEN_PT_GFLAGS_SHIFT_DEST_ID 0
23 #define XEN_PT_GFLAGS_SHIFT_RH 8
24 #define XEN_PT_GFLAGS_SHIFT_DM 9
25 #define XEN_PT_GFLAGSSHIFT_DELIV_MODE 12
26 #define XEN_PT_GFLAGSSHIFT_TRG_MODE 15
33 static inline uint8_t msi_vector(uint32_t data
)
35 return (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
38 static inline uint8_t msi_dest_id(uint32_t addr
)
40 return (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
43 static inline uint32_t msi_ext_dest_id(uint32_t addr_hi
)
45 return addr_hi
& 0xffffff00;
48 static uint32_t msi_gflags(uint32_t data
, uint64_t addr
)
51 int rh
, dm
, dest_id
, deliv_mode
, trig_mode
;
53 rh
= (addr
>> MSI_ADDR_REDIRECTION_SHIFT
) & 0x1;
54 dm
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
55 dest_id
= msi_dest_id(addr
);
56 deliv_mode
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
57 trig_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
59 result
= dest_id
| (rh
<< XEN_PT_GFLAGS_SHIFT_RH
)
60 | (dm
<< XEN_PT_GFLAGS_SHIFT_DM
)
61 | (deliv_mode
<< XEN_PT_GFLAGSSHIFT_DELIV_MODE
)
62 | (trig_mode
<< XEN_PT_GFLAGSSHIFT_TRG_MODE
);
67 static inline uint64_t msi_addr64(XenPTMSI
*msi
)
69 return (uint64_t)msi
->addr_hi
<< 32 | msi
->addr_lo
;
72 static int msi_msix_enable(XenPCIPassthroughState
*s
,
83 xen_host_pci_get_word(&s
->real_device
, address
, &val
);
89 xen_host_pci_set_word(&s
->real_device
, address
, val
);
93 static int msi_msix_setup(XenPCIPassthroughState
*s
,
101 uint8_t gvec
= msi_vector(data
);
104 assert((!is_msix
&& msix_entry
== 0) || is_msix
);
107 /* if gvec is 0, the guest is asking for a particular pirq that
108 * is passed as dest_id */
109 *ppirq
= msi_ext_dest_id(addr
>> 32) | msi_dest_id(addr
);
111 /* this probably identifies an misconfiguration of the guest,
112 * try the emulated path */
113 *ppirq
= XEN_PT_UNASSIGNED_PIRQ
;
115 XEN_PT_LOG(&s
->dev
, "requested pirq %d for MSI%s"
116 " (vec: %#x, entry: %#x)\n",
117 *ppirq
, is_msix
? "-X" : "", gvec
, msix_entry
);
122 uint64_t table_base
= 0;
125 table_base
= s
->msix
->table_base
;
128 rc
= xc_physdev_map_pirq_msi(xen_xc
, xen_domid
, XEN_PT_AUTO_ASSIGN
,
129 ppirq
, PCI_DEVFN(s
->real_device
.dev
,
130 s
->real_device
.func
),
132 msix_entry
, table_base
);
135 "Mapping of MSI%s (rc: %i, vec: %#x, entry %#x)\n",
136 is_msix
? "-X" : "", rc
, gvec
, msix_entry
);
143 static int msi_msix_update(XenPCIPassthroughState
*s
,
151 PCIDevice
*d
= &s
->dev
;
152 uint8_t gvec
= msi_vector(data
);
153 uint32_t gflags
= msi_gflags(data
, addr
);
155 uint64_t table_addr
= 0;
157 XEN_PT_LOG(d
, "Updating MSI%s with pirq %d gvec %#x gflags %#x"
159 is_msix
? "-X" : "", pirq
, gvec
, gflags
, msix_entry
);
162 table_addr
= s
->msix
->mmio_base_addr
;
165 rc
= xc_domain_update_msi_irq(xen_xc
, xen_domid
, gvec
,
166 pirq
, gflags
, table_addr
);
169 XEN_PT_ERR(d
, "Updating of MSI%s failed. (rc: %d)\n",
170 is_msix
? "-X" : "", rc
);
172 if (xc_physdev_unmap_pirq(xen_xc
, xen_domid
, *old_pirq
)) {
173 XEN_PT_ERR(d
, "Unmapping of MSI%s pirq %d failed.\n",
174 is_msix
? "-X" : "", *old_pirq
);
176 *old_pirq
= XEN_PT_UNASSIGNED_PIRQ
;
181 static int msi_msix_disable(XenPCIPassthroughState
*s
,
188 PCIDevice
*d
= &s
->dev
;
189 uint8_t gvec
= msi_vector(data
);
190 uint32_t gflags
= msi_gflags(data
, addr
);
193 if (pirq
== XEN_PT_UNASSIGNED_PIRQ
) {
198 XEN_PT_LOG(d
, "Unbind MSI%s with pirq %d, gvec %#x\n",
199 is_msix
? "-X" : "", pirq
, gvec
);
200 rc
= xc_domain_unbind_msi_irq(xen_xc
, xen_domid
, gvec
, pirq
, gflags
);
202 XEN_PT_ERR(d
, "Unbinding of MSI%s failed. (pirq: %d, gvec: %#x)\n",
203 is_msix
? "-X" : "", pirq
, gvec
);
208 XEN_PT_LOG(d
, "Unmap MSI%s pirq %d\n", is_msix
? "-X" : "", pirq
);
209 rc
= xc_physdev_unmap_pirq(xen_xc
, xen_domid
, pirq
);
211 XEN_PT_ERR(d
, "Unmapping of MSI%s pirq %d failed. (rc: %i)\n",
212 is_msix
? "-X" : "", pirq
, rc
);
220 * MSI virtualization functions
223 int xen_pt_msi_set_enable(XenPCIPassthroughState
*s
, bool enable
)
225 XEN_PT_LOG(&s
->dev
, "%s MSI.\n", enable
? "enabling" : "disabling");
231 return msi_msix_enable(s
, s
->msi
->ctrl_offset
, PCI_MSI_FLAGS_ENABLE
,
235 /* setup physical msi, but don't enable it */
236 int xen_pt_msi_setup(XenPCIPassthroughState
*s
)
238 int pirq
= XEN_PT_UNASSIGNED_PIRQ
;
240 XenPTMSI
*msi
= s
->msi
;
242 if (msi
->initialized
) {
244 "Setup physical MSI when it has been properly initialized.\n");
248 rc
= msi_msix_setup(s
, msi_addr64(msi
), msi
->data
, &pirq
, false, 0, true);
254 XEN_PT_ERR(&s
->dev
, "Invalid pirq number: %d.\n", pirq
);
259 XEN_PT_LOG(&s
->dev
, "MSI mapped with pirq %d.\n", pirq
);
264 int xen_pt_msi_update(XenPCIPassthroughState
*s
)
266 XenPTMSI
*msi
= s
->msi
;
267 return msi_msix_update(s
, msi_addr64(msi
), msi
->data
, msi
->pirq
,
268 false, 0, &msi
->pirq
);
271 void xen_pt_msi_disable(XenPCIPassthroughState
*s
)
273 XenPTMSI
*msi
= s
->msi
;
279 xen_pt_msi_set_enable(s
, false);
281 msi_msix_disable(s
, msi_addr64(msi
), msi
->data
, msi
->pirq
, false,
287 msi
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
291 * MSI-X virtualization functions
294 static int msix_set_enable(XenPCIPassthroughState
*s
, bool enabled
)
296 XEN_PT_LOG(&s
->dev
, "%s MSI-X.\n", enabled
? "enabling" : "disabling");
302 return msi_msix_enable(s
, s
->msix
->ctrl_offset
, PCI_MSIX_FLAGS_ENABLE
,
306 static int xen_pt_msix_update_one(XenPCIPassthroughState
*s
, int entry_nr
)
308 XenPTMSIXEntry
*entry
= NULL
;
312 if (entry_nr
< 0 || entry_nr
>= s
->msix
->total_entries
) {
316 entry
= &s
->msix
->msix_entry
[entry_nr
];
318 if (!entry
->updated
) {
324 rc
= msi_msix_setup(s
, entry
->addr
, entry
->data
, &pirq
, true, entry_nr
,
325 entry
->pirq
== XEN_PT_UNASSIGNED_PIRQ
);
329 if (entry
->pirq
== XEN_PT_UNASSIGNED_PIRQ
) {
333 rc
= msi_msix_update(s
, entry
->addr
, entry
->data
, pirq
, true,
334 entry_nr
, &entry
->pirq
);
337 entry
->updated
= false;
343 int xen_pt_msix_update(XenPCIPassthroughState
*s
)
345 XenPTMSIX
*msix
= s
->msix
;
348 for (i
= 0; i
< msix
->total_entries
; i
++) {
349 xen_pt_msix_update_one(s
, i
);
355 void xen_pt_msix_disable(XenPCIPassthroughState
*s
)
359 msix_set_enable(s
, false);
361 for (i
= 0; i
< s
->msix
->total_entries
; i
++) {
362 XenPTMSIXEntry
*entry
= &s
->msix
->msix_entry
[i
];
364 msi_msix_disable(s
, entry
->addr
, entry
->data
, entry
->pirq
, true, true);
366 /* clear MSI-X info */
367 entry
->pirq
= XEN_PT_UNASSIGNED_PIRQ
;
368 entry
->updated
= false;
372 int xen_pt_msix_update_remap(XenPCIPassthroughState
*s
, int bar_index
)
374 XenPTMSIXEntry
*entry
;
377 if (!(s
->msix
&& s
->msix
->bar_index
== bar_index
)) {
381 for (i
= 0; i
< s
->msix
->total_entries
; i
++) {
382 entry
= &s
->msix
->msix_entry
[i
];
383 if (entry
->pirq
!= XEN_PT_UNASSIGNED_PIRQ
) {
384 ret
= xc_domain_unbind_pt_irq(xen_xc
, xen_domid
, entry
->pirq
,
385 PT_IRQ_TYPE_MSI
, 0, 0, 0, 0);
387 XEN_PT_ERR(&s
->dev
, "unbind MSI-X entry %d failed\n",
390 entry
->updated
= true;
393 return xen_pt_msix_update(s
);
396 static uint32_t get_entry_value(XenPTMSIXEntry
*e
, int offset
)
399 case PCI_MSIX_ENTRY_LOWER_ADDR
:
400 return e
->addr
& UINT32_MAX
;
401 case PCI_MSIX_ENTRY_UPPER_ADDR
:
402 return e
->addr
>> 32;
403 case PCI_MSIX_ENTRY_DATA
:
405 case PCI_MSIX_ENTRY_VECTOR_CTRL
:
406 return e
->vector_ctrl
;
412 static void set_entry_value(XenPTMSIXEntry
*e
, int offset
, uint32_t val
)
415 case PCI_MSIX_ENTRY_LOWER_ADDR
:
416 e
->addr
= (e
->addr
& ((uint64_t)UINT32_MAX
<< 32)) | val
;
418 case PCI_MSIX_ENTRY_UPPER_ADDR
:
419 e
->addr
= (uint64_t)val
<< 32 | (e
->addr
& UINT32_MAX
);
421 case PCI_MSIX_ENTRY_DATA
:
424 case PCI_MSIX_ENTRY_VECTOR_CTRL
:
425 e
->vector_ctrl
= val
;
430 static void pci_msix_write(void *opaque
, hwaddr addr
,
431 uint64_t val
, unsigned size
)
433 XenPCIPassthroughState
*s
= opaque
;
434 XenPTMSIX
*msix
= s
->msix
;
435 XenPTMSIXEntry
*entry
;
436 int entry_nr
, offset
;
438 entry_nr
= addr
/ PCI_MSIX_ENTRY_SIZE
;
439 if (entry_nr
< 0 || entry_nr
>= msix
->total_entries
) {
440 XEN_PT_ERR(&s
->dev
, "asked MSI-X entry '%i' invalid!\n", entry_nr
);
443 entry
= &msix
->msix_entry
[entry_nr
];
444 offset
= addr
% PCI_MSIX_ENTRY_SIZE
;
446 if (offset
!= PCI_MSIX_ENTRY_VECTOR_CTRL
) {
447 const volatile uint32_t *vec_ctrl
;
449 if (get_entry_value(entry
, offset
) == val
) {
454 * If Xen intercepts the mask bit access, entry->vec_ctrl may not be
455 * up-to-date. Read from hardware directly.
457 vec_ctrl
= s
->msix
->phys_iomem_base
+ entry_nr
* PCI_MSIX_ENTRY_SIZE
458 + PCI_MSIX_ENTRY_VECTOR_CTRL
;
460 if (msix
->enabled
&& !(*vec_ctrl
& PCI_MSIX_ENTRY_CTRL_MASKBIT
)) {
461 XEN_PT_ERR(&s
->dev
, "Can't update msix entry %d since MSI-X is"
462 " already enabled.\n", entry_nr
);
466 entry
->updated
= true;
469 set_entry_value(entry
, offset
, val
);
471 if (offset
== PCI_MSIX_ENTRY_VECTOR_CTRL
) {
472 if (msix
->enabled
&& !(val
& PCI_MSIX_ENTRY_CTRL_MASKBIT
)) {
473 xen_pt_msix_update_one(s
, entry_nr
);
478 static uint64_t pci_msix_read(void *opaque
, hwaddr addr
,
481 XenPCIPassthroughState
*s
= opaque
;
482 XenPTMSIX
*msix
= s
->msix
;
483 int entry_nr
, offset
;
485 entry_nr
= addr
/ PCI_MSIX_ENTRY_SIZE
;
487 XEN_PT_ERR(&s
->dev
, "asked MSI-X entry '%i' invalid!\n", entry_nr
);
491 offset
= addr
% PCI_MSIX_ENTRY_SIZE
;
493 if (addr
< msix
->total_entries
* PCI_MSIX_ENTRY_SIZE
) {
494 return get_entry_value(&msix
->msix_entry
[entry_nr
], offset
);
496 /* Pending Bit Array (PBA) */
497 return *(uint32_t *)(msix
->phys_iomem_base
+ addr
);
501 static const MemoryRegionOps pci_msix_ops
= {
502 .read
= pci_msix_read
,
503 .write
= pci_msix_write
,
504 .endianness
= DEVICE_NATIVE_ENDIAN
,
506 .min_access_size
= 4,
507 .max_access_size
= 4,
512 int xen_pt_msix_init(XenPCIPassthroughState
*s
, uint32_t base
)
515 uint16_t control
= 0;
516 uint32_t table_off
= 0;
517 int i
, total_entries
, bar_index
;
518 XenHostPCIDevice
*hd
= &s
->real_device
;
519 PCIDevice
*d
= &s
->dev
;
521 XenPTMSIX
*msix
= NULL
;
524 rc
= xen_host_pci_get_byte(hd
, base
+ PCI_CAP_LIST_ID
, &id
);
529 if (id
!= PCI_CAP_ID_MSIX
) {
530 XEN_PT_ERR(d
, "Invalid id %#x base %#x\n", id
, base
);
534 xen_host_pci_get_word(hd
, base
+ PCI_MSIX_FLAGS
, &control
);
535 total_entries
= control
& PCI_MSIX_FLAGS_QSIZE
;
538 s
->msix
= g_malloc0(sizeof (XenPTMSIX
)
539 + total_entries
* sizeof (XenPTMSIXEntry
));
542 msix
->total_entries
= total_entries
;
543 for (i
= 0; i
< total_entries
; i
++) {
544 msix
->msix_entry
[i
].pirq
= XEN_PT_UNASSIGNED_PIRQ
;
547 memory_region_init_io(&msix
->mmio
, &pci_msix_ops
, s
, "xen-pci-pt-msix",
548 (total_entries
* PCI_MSIX_ENTRY_SIZE
552 xen_host_pci_get_long(hd
, base
+ PCI_MSIX_TABLE
, &table_off
);
553 bar_index
= msix
->bar_index
= table_off
& PCI_MSIX_FLAGS_BIRMASK
;
554 table_off
= table_off
& ~PCI_MSIX_FLAGS_BIRMASK
;
555 msix
->table_base
= s
->real_device
.io_regions
[bar_index
].base_addr
;
556 XEN_PT_LOG(d
, "get MSI-X table BAR base 0x%"PRIx64
"\n", msix
->table_base
);
558 fd
= open("/dev/mem", O_RDWR
);
561 XEN_PT_ERR(d
, "Can't open /dev/mem: %s\n", strerror(errno
));
564 XEN_PT_LOG(d
, "table_off = %#x, total_entries = %d\n",
565 table_off
, total_entries
);
566 msix
->table_offset_adjust
= table_off
& 0x0fff;
567 msix
->phys_iomem_base
=
569 total_entries
* PCI_MSIX_ENTRY_SIZE
+ msix
->table_offset_adjust
,
571 MAP_SHARED
| MAP_LOCKED
,
573 msix
->table_base
+ table_off
- msix
->table_offset_adjust
);
575 if (msix
->phys_iomem_base
== MAP_FAILED
) {
577 XEN_PT_ERR(d
, "Can't map physical MSI-X table: %s\n", strerror(errno
));
580 msix
->phys_iomem_base
= (char *)msix
->phys_iomem_base
581 + msix
->table_offset_adjust
;
583 XEN_PT_LOG(d
, "mapping physical MSI-X table to %p\n",
584 msix
->phys_iomem_base
);
586 memory_region_add_subregion_overlap(&s
->bar
[bar_index
], table_off
,
588 2); /* Priority: pci default + 1 */
593 memory_region_destroy(&msix
->mmio
);
599 void xen_pt_msix_delete(XenPCIPassthroughState
*s
)
601 XenPTMSIX
*msix
= s
->msix
;
607 /* unmap the MSI-X memory mapped register area */
608 if (msix
->phys_iomem_base
) {
609 XEN_PT_LOG(&s
->dev
, "unmapping physical MSI-X table from %p\n",
610 msix
->phys_iomem_base
);
611 munmap(msix
->phys_iomem_base
, msix
->total_entries
* PCI_MSIX_ENTRY_SIZE
612 + msix
->table_offset_adjust
);
615 memory_region_del_subregion(&s
->bar
[msix
->bar_index
], &msix
->mmio
);
616 memory_region_destroy(&msix
->mmio
);