[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH RFC server 08/11] vfio-user: handle PCI BAR accesses
From: |
Jagannathan Raman |
Subject: |
[PATCH RFC server 08/11] vfio-user: handle PCI BAR accesses |
Date: |
Mon, 19 Jul 2021 16:00:10 -0400 |
Determine the BARs used by the PCI device and register handlers to
manage the access to the same.
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
---
hw/remote/vfio-user-obj.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++
hw/remote/trace-events | 2 +
2 files changed, 97 insertions(+)
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index d158a7f..9853feb 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -212,6 +212,99 @@ static int dma_unregister(vfu_ctx_t *vfu_ctx,
vfu_dma_info_t *info)
return 0;
}
+static ssize_t vfu_object_bar_rw(PCIDevice *pci_dev, hwaddr addr, size_t count,
+ char * const buf, const bool is_write,
+ uint8_t type)
+{
+ AddressSpace *as = NULL;
+ MemTxResult res;
+
+ if (type == PCI_BASE_ADDRESS_SPACE_MEMORY) {
+ as = pci_device_iommu_address_space(pci_dev);
+ } else {
+ as = &address_space_io;
+ }
+
+ trace_vfu_bar_rw_enter(is_write ? "Write" : "Read", (uint64_t)addr);
+
+ res = address_space_rw(as, addr, MEMTXATTRS_UNSPECIFIED, (void *)buf,
+ (hwaddr)count, is_write);
+ if (res != MEMTX_OK) {
+ warn_report("vfu: failed to %s 0x%"PRIx64"",
+ is_write ? "write to" : "read from",
+ addr);
+ return -1;
+ }
+
+ trace_vfu_bar_rw_exit(is_write ? "Write" : "Read", (uint64_t)addr);
+
+ return count;
+}
+
+/**
+ * VFU_OBJECT_BAR_HANDLER - macro for defining handlers for PCI BARs.
+ *
+ * To create handler for BAR number 2, VFU_OBJECT_BAR_HANDLER(2) would
+ * define vfu_object_bar2_handler
+ */
+#define VFU_OBJECT_BAR_HANDLER(BAR_NO)
\
+ static ssize_t vfu_object_bar##BAR_NO##_handler(vfu_ctx_t *vfu_ctx,
\
+ char * const buf, size_t count,
\
+ loff_t offset, const bool is_write)
\
+ {
\
+ VfuObject *o = vfu_get_private(vfu_ctx);
\
+ hwaddr addr = (hwaddr)(pci_get_long(o->pci_dev->config +
\
+ PCI_BASE_ADDRESS_0 +
\
+ (4 * BAR_NO)) + offset);
\
+
\
+ return vfu_object_bar_rw(o->pci_dev, addr, count, buf, is_write,
\
+ o->pci_dev->io_regions[BAR_NO].type);
\
+ }
\
+
+VFU_OBJECT_BAR_HANDLER(0)
+VFU_OBJECT_BAR_HANDLER(1)
+VFU_OBJECT_BAR_HANDLER(2)
+VFU_OBJECT_BAR_HANDLER(3)
+VFU_OBJECT_BAR_HANDLER(4)
+VFU_OBJECT_BAR_HANDLER(5)
+
+static vfu_region_access_cb_t *vfu_object_bar_handlers[PCI_NUM_REGIONS] = {
+ &vfu_object_bar0_handler,
+ &vfu_object_bar1_handler,
+ &vfu_object_bar2_handler,
+ &vfu_object_bar3_handler,
+ &vfu_object_bar4_handler,
+ &vfu_object_bar5_handler,
+};
+
+/**
+ * vfu_object_register_bars - Identify active BAR regions of pdev and setup
+ * callbacks to handle read/write accesses
+ */
+static void vfu_object_register_bars(vfu_ctx_t *vfu_ctx, PCIDevice *pdev)
+{
+ uint32_t orig_val, new_val;
+ int i, size;
+
+ for (i = 0; i < PCI_NUM_REGIONS; i++) {
+ orig_val = pci_default_read_config(pdev,
+ PCI_BASE_ADDRESS_0 + (4 * i), 4);
+ new_val = 0xffffffff;
+ pci_default_write_config(pdev,
+ PCI_BASE_ADDRESS_0 + (4 * i), new_val, 4);
+ new_val = pci_default_read_config(pdev,
+ PCI_BASE_ADDRESS_0 + (4 * i), 4);
+ size = (~(new_val & 0xFFFFFFF0)) + 1;
+ pci_default_write_config(pdev, PCI_BASE_ADDRESS_0 + (4 * i),
+ orig_val, 4);
+ if (size) {
+ vfu_setup_region(vfu_ctx, VFU_PCI_DEV_BAR0_REGION_IDX + i, size,
+ vfu_object_bar_handlers[i], VFU_REGION_FLAG_RW,
+ NULL, 0, -1, 0);
+ }
+ }
+}
+
static void vfu_object_machine_done(Notifier *notifier, void *data)
{
VfuObject *o = container_of(notifier, VfuObject, machine_done);
@@ -266,6 +359,8 @@ static void vfu_object_machine_done(Notifier *notifier,
void *data)
return;
}
+ vfu_object_register_bars(o->vfu_ctx, o->pci_dev);
+
qemu_thread_create(&o->vfu_ctx_thread, "VFU ctx runner",
vfu_object_ctx_run,
o, QEMU_THREAD_JOINABLE);
}
diff --git a/hw/remote/trace-events b/hw/remote/trace-events
index f945c7e..f3f65e2 100644
--- a/hw/remote/trace-events
+++ b/hw/remote/trace-events
@@ -9,3 +9,5 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%u ->
0x%x"
vfu_cfg_write(uint32_t offset, uint32_t val) "vfu: cfg: 0x%u <- 0x%x"
vfu_dma_register(uint64_t gpa, size_t len) "vfu: registering GPA 0x%"PRIx64",
%zu bytes"
vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64""
+vfu_bar_rw_enter(const char *op, uint64_t addr) "vfu: %s request for BAR
address 0x%"PRIx64""
+vfu_bar_rw_exit(const char *op, uint64_t addr) "vfu: Finished %s of BAR
address 0x%"PRIx64""
--
1.8.3.1
- Re: [PATCH RFC 12/19] vfio-user: probe remote device's BARs, (continued)
- [PATCH RFC 15/19] vfio-user: vfio user device realize, Elena Ufimtseva, 2021/07/19
- [PATCH RFC 13/19] vfio-user: respond to remote DMA read/write requests, Elena Ufimtseva, 2021/07/19
- [PATCH RFC 18/19] vfio-user: migration support, Elena Ufimtseva, 2021/07/19
- [PATCH RFC 17/19] vfio-user: probe remote device ROM BAR, Elena Ufimtseva, 2021/07/19
- [PATCH RFC 19/19] vfio-user: add migration cli options and version negotiation, Elena Ufimtseva, 2021/07/19
- [PATCH RFC 16/19] vfio-user: pci reset, Elena Ufimtseva, 2021/07/19
- [PATCH RFC server 00/11] vfio-user server in QEMU, Jagannathan Raman, 2021/07/19
- [PATCH RFC server 07/11] vfio-user: handle DMA mappings, Jagannathan Raman, 2021/07/19
- [PATCH RFC server 08/11] vfio-user: handle PCI BAR accesses,
Jagannathan Raman <=
- [PATCH RFC server 10/11] vfio-user: register handlers to facilitate migration, Jagannathan Raman, 2021/07/19
- [PATCH RFC server 09/11] vfio-user: handle device interrupts, Jagannathan Raman, 2021/07/19
- [PATCH RFC server 11/11] vfio-user: acceptance test, Jagannathan Raman, 2021/07/19
- [PATCH RFC server 01/11] vfio-user: build library, Jagannathan Raman, 2021/07/19