git: 93cf93179c50 - main - bhyve: add helper for passthru specific mmio ranges

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Fri, 12 May 2023 07:47:46 UTC
The branch main has been updated by corvink:

URL: https://cgit.FreeBSD.org/src/commit/?id=93cf93179c505cebbcdbeef196d523d36890ed4a

commit 93cf93179c505cebbcdbeef196d523d36890ed4a
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-10 10:22:33 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-05-12 07:29:44 +0000

    bhyve: add helper for passthru specific mmio ranges
    
    Intel GPUs have two special memory regions. They are called Graphics
    Stolen Memory and OpRegion. bhyve has to emulate both of them. In order
    to keep track of those special regions, add generic mmio ranges to the
    passthru emulation.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40036
---
 usr.sbin/bhyve/pci_passthru.c | 12 ++++++++++++
 usr.sbin/bhyve/pci_passthru.h | 10 ++++++++++
 2 files changed, 22 insertions(+)

diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c
index 054b284a2f27..0b7a0d362593 100644
--- a/usr.sbin/bhyve/pci_passthru.c
+++ b/usr.sbin/bhyve/pci_passthru.c
@@ -78,6 +78,8 @@ __FBSDID("$FreeBSD$");
 #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
 #define MSIX_CAPLEN 12
 
+#define PASSTHRU_MMIO_MAX 2
+
 static int pcifd = -1;
 
 struct passthru_softc {
@@ -94,6 +96,7 @@ struct passthru_softc {
 	} psc_msix;
 	struct pcisel psc_sel;
 
+	struct passthru_mmio_mapping psc_mmio_map[PASSTHRU_MMIO_MAX];
 	cfgread_handler psc_pcir_rhandler[PCI_REGMAX + 1];
 	cfgwrite_handler psc_pcir_whandler[PCI_REGMAX + 1];
 };
@@ -660,6 +663,15 @@ done:
 	return (error);
 }
 
+struct passthru_mmio_mapping *
+passthru_get_mmio(struct passthru_softc *sc, int num)
+{
+	assert(sc != NULL);
+	assert(num < PASSTHRU_MMIO_MAX);
+
+	return (&sc->psc_mmio_map[num]);
+}
+
 struct pcisel *
 passthru_get_sel(struct passthru_softc *sc)
 {
diff --git a/usr.sbin/bhyve/pci_passthru.h b/usr.sbin/bhyve/pci_passthru.h
index 79578a8496c4..1ca10fa343e4 100644
--- a/usr.sbin/bhyve/pci_passthru.h
+++ b/usr.sbin/bhyve/pci_passthru.h
@@ -11,6 +11,14 @@
 
 #include "pci_emul.h"
 
+struct passthru_mmio_mapping {
+	vm_paddr_t gpa; /* guest physical address */
+	void *gva;	/* guest virtual address */
+	vm_paddr_t hpa; /* host physical address */
+	void *hva;	/* guest virtual address */
+	vm_paddr_t len;
+};
+
 struct passthru_softc;
 
 typedef int (*cfgread_handler)(struct passthru_softc *sc,
@@ -24,6 +32,8 @@ int passthru_cfgread_emulate(struct passthru_softc *sc, struct pci_devinst *pi,
     int coff, int bytes, uint32_t *rv);
 int passthru_cfgwrite_emulate(struct passthru_softc *sc, struct pci_devinst *pi,
     int coff, int bytes, uint32_t val);
+struct passthru_mmio_mapping *passthru_get_mmio(struct passthru_softc *sc,
+    int num);
 struct pcisel *passthru_get_sel(struct passthru_softc *sc);
 int set_pcir_handler(struct passthru_softc *sc, int reg, int len,
     cfgread_handler rhandler, cfgwrite_handler whandler);