Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Aug 2023 07:41:51 GMT
From:      Corvin =?utf-8?Q?K=C3=B6hne?= <corvink@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: b260e413b4b7 - stable/13 - bhyve: read OpRegion address and size for GVT-d
Message-ID:  <202308180741.37I7fp9S066881@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by corvink:

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

commit b260e413b4b7053e4c7a4521e4e9823bb01515be
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-10 11:38:02 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-08-18 07:32:07 +0000

    bhyve: read OpRegion address and size for GVT-d
    
    The OpRegion provides some configuration bits and ACPI methods used by
    some Intel drivers. The guest needs access to it. In the first step,
    we're reading it's address and size.
    
    Reviewed by:            jhb
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40040
    
    (cherry picked from commit 6952b9d25e18fd3a4bf0d1bb7d6b44ff6edf6737)
---
 usr.sbin/bhyve/pci_gvt-d.c | 67 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/usr.sbin/bhyve/pci_gvt-d.c b/usr.sbin/bhyve/pci_gvt-d.c
index 3109bc06be82..fd3b48c3e5b7 100644
--- a/usr.sbin/bhyve/pci_gvt-d.c
+++ b/usr.sbin/bhyve/pci_gvt-d.c
@@ -6,12 +6,16 @@
  */
 
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <sys/sysctl.h>
 
 #include <dev/pci/pcireg.h>
 
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "e820.h"
 #include "pci_gvt-d-opregion.h"
@@ -21,14 +25,20 @@
 #define MB (1024 * KB)
 #define GB (1024 * MB)
 
+#ifndef _PATH_MEM
+#define _PATH_MEM "/dev/mem"
+#endif
+
 #define PCI_VENDOR_INTEL 0x8086
 
-#define PCIR_BDSM 0x5C /* Base of Data Stolen Memory register */
+#define PCIR_BDSM 0x5C	   /* Base of Data Stolen Memory register */
+#define PCIR_ASLS_CTL 0xFC /* Opregion start address register */
 
 #define PCIM_BDSM_GSM_ALIGNMENT \
 	0x00100000 /* Graphics Stolen Memory is 1 MB aligned */
 
 #define GVT_D_MAP_GSM 0
+#define GVT_D_MAP_OPREGION 1
 
 static int
 gvt_d_probe(struct pci_devinst *const pi)
@@ -167,6 +177,56 @@ gvt_d_setup_gsm(struct pci_devinst *const pi)
 	    passthru_cfgwrite_emulate));
 }
 
+static int
+gvt_d_setup_opregion(struct pci_devinst *const pi)
+{
+	struct passthru_softc *sc;
+	struct passthru_mmio_mapping *opregion;
+	struct igd_opregion_header *header;
+	uint64_t asls;
+	int memfd;
+
+	sc = pi->pi_arg;
+
+	memfd = open(_PATH_MEM, O_RDONLY, 0);
+	if (memfd < 0) {
+		warn("%s: Failed to open %s", __func__, _PATH_MEM);
+		return (-1);
+	}
+
+	opregion = passthru_get_mmio(sc, GVT_D_MAP_OPREGION);
+	if (opregion == NULL) {
+		warnx("%s: Unable to access opregion", __func__);
+		close(memfd);
+		return (-1);
+	}
+
+	asls = read_config(passthru_get_sel(sc), PCIR_ASLS_CTL, 4);
+
+	header = mmap(NULL, sizeof(*header), PROT_READ, MAP_SHARED, memfd,
+	    asls);
+	if (header == MAP_FAILED) {
+		warn("%s: Unable to map OpRegion header", __func__);
+		close(memfd);
+		return (-1);
+	}
+	if (memcmp(header->sign, IGD_OPREGION_HEADER_SIGN,
+	    sizeof(header->sign)) != 0) {
+		warnx("%s: Invalid OpRegion signature", __func__);
+		munmap(header, sizeof(*header));
+		close(memfd);
+		return (-1);
+	}
+
+	opregion->hpa = asls;
+	opregion->len = header->size * KB;
+	munmap(header, sizeof(header));
+
+	close(memfd);
+
+	return (0);
+}
+
 static int
 gvt_d_init(struct pci_devinst *const pi, nvlist_t *const nvl __unused)
 {
@@ -177,6 +237,11 @@ gvt_d_init(struct pci_devinst *const pi, nvlist_t *const nvl __unused)
 		goto done;
 	}
 
+	if ((error = gvt_d_setup_opregion(pi)) != 0) {
+		warnx("%s: Unable to setup OpRegion", __func__);
+		goto done;
+	}
+
 done:
 	return (error);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202308180741.37I7fp9S066881>