From nobody Mon May 16 12:50:10 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 548C31AE7FDE; Mon, 16 May 2022 12:50:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4L1zcM1rWRz4nMX; Mon, 16 May 2022 12:50:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652705411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=As7YWPIM9i20tWDD+spjirioMJOHYyS6QpdloE38y/w=; b=L5//FJHtdOIGOBE80+LXzb/YN9ImdP5Vi0tsEXvgZMy4HtSBQem3zJxbFrpv4smKVBOb6L 9KNnUuZ+N7XZFG9wTpz3dslwrlsx1gWNn7DNhlmIXgqSduZu1SiGUW8ZHn6MSLMn/voFwt K3sDv/n06+kcgHj53DdE4B8WJORw0JNELlLDpYS0UcMnvQbWxu3jnAWl6HH4ukLdoXr9Qx l1xYwUcdS6hdtJupGrThVWVO3kLcPOEKi9xQ2uWRa9F7if6xGJYeNQ+nO6y1lfpWw9R0fv S6ofeoVqAhxXOtvDCPrUsNsndDipktE4ynknBbvROLuTysLXe7TXl99My5N59g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1C195202C7; Mon, 16 May 2022 12:50:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24GCoAEn092459; Mon, 16 May 2022 12:50:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24GCoAt5092455; Mon, 16 May 2022 12:50:10 GMT (envelope-from git) Date: Mon, 16 May 2022 12:50:10 GMT Message-Id: <202205161250.24GCoAt5092455@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Emmanuel Vadot Subject: git: 7c6d3eb00932 - stable/13 - bhyve: export funcs for read/write pci config List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: manu X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7c6d3eb00932e79296664ed561f51cf618af03fd Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652705411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=As7YWPIM9i20tWDD+spjirioMJOHYyS6QpdloE38y/w=; b=NonsIoHcjAdIpNBtRSSOxK4MCnYpFC2au9kDtCN2oFSUKMwDscZSuax+z0MvvSPpf4UaPt RPltOs0NsaIdj4Tqpkq8A9pnmOJXeeS0nN69kB8FIGBgNfCOSPr3byRK0tqg+Bd3RrcRCR OjVspvT/342SklbfGl2gpoJiHxBm/HgW4KoEpg+s6L8/C5je7qC8aGP/Dvb+wZacvCP+Xc cqCqK24hamIPBBQY/Lqqq8IOUd/nOeAE3/d82skuN2ZKrks7FfSrfnPNT+OaQuGEyWz3rl aJUQqkqfPR++bsccHzcwFegM66rutJkjLXzOYnKjuFbDqp7R6vpf7jvsQ1v4Gg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1652705411; a=rsa-sha256; cv=none; b=MK/W7rYEJrP3tpTiaMFTHhqHmiFwax0O0ikVVFZDNZey2a9zCD14XaHXSNVdW8Lg8CMHhc vH9Z41cr6gpOJb2lbSsIu3DKRUoI5jvim57Ykb1fp/9A6xJBwYN3W8YNnXHpvKuKZDOR6n bVBgznHNquc2sMFAz2uMdd/Pd/GbmMfF7hPKgBU/8AicE82JsE8r9tTLqv4gUEtHkO8VdF YFcq1Ivoa6rus5mGpYlbqxayk88nILB9OQEiB2EDaVzAGbHT/1v6gQFyR7p3cgPTPHAv8E PCxwkhD3KWRjvM9hf0ReU2Nk9Fdx2UV2m228YBRT2LFaUUfHSfzyH8vRkHtQ+w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by manu: URL: https://cgit.FreeBSD.org/src/commit/?id=7c6d3eb00932e79296664ed561f51cf618af03fd commit 7c6d3eb00932e79296664ed561f51cf618af03fd Author: Corvin Köhne AuthorDate: 2022-03-10 10:26:19 +0000 Commit: Emmanuel Vadot CommitDate: 2022-05-16 12:41:14 +0000 bhyve: export funcs for read/write pci config Export functions for reading and writing the pci config space from passthru device to be used by other devices. This is required for lpc devices to set their vendor/device ids to their physical values. Otherwise, GPU passthrough for integrated Intel GPUs won't work properly. Differential Revision: https://reviews.freebsd.org/D33769 Reviewed by: markj Sponsored by: Beckhoff Automation GmbH & Co. KG MFC after: 1 month (cherry picked from commit 563fd2240e13cfb53f6e5d50e9d1dc738b7c441e) --- usr.sbin/bhyve/pci_passthru.c | 62 +++++++++++++++++++++++++------------------ usr.sbin/bhyve/pci_passthru.h | 15 +++++++++++ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c index 586a2da8aa05..3fe446f42eee 100644 --- a/usr.sbin/bhyve/pci_passthru.c +++ b/usr.sbin/bhyve/pci_passthru.c @@ -61,12 +61,11 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include "config.h" #include "debug.h" -#include "pci_emul.h" #include "mem.h" +#include "pci_passthru.h" #ifndef _PATH_DEVPCI #define _PATH_DEVPCI "/dev/pci" @@ -115,9 +114,36 @@ msi_caplen(int msgctrl) return (len); } -static uint32_t +static int +pcifd_init() { + pcifd = open(_PATH_DEVPCI, O_RDWR, 0); + if (pcifd < 0) { + warn("failed to open %s", _PATH_DEVPCI); + return (1); + } + +#ifndef WITHOUT_CAPSICUM + cap_rights_t pcifd_rights; + cap_rights_init(&pcifd_rights, CAP_IOCTL, CAP_READ, CAP_WRITE); + if (caph_rights_limit(pcifd, &pcifd_rights) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); + + const cap_ioctl_t pcifd_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR, + PCIOCBARIO, PCIOCBARMMAP }; + if (caph_ioctls_limit(pcifd, pcifd_ioctls, nitems(pcifd_ioctls)) == -1) + errx(EX_OSERR, "Unable to apply rights for sandbox"); +#endif + + return (0); +} + +uint32_t read_config(const struct pcisel *sel, long reg, int width) { + if (pcifd < 0 && pcifd_init()) { + return (0); + } + struct pci_io pi; bzero(&pi, sizeof(pi)); @@ -131,9 +157,13 @@ read_config(const struct pcisel *sel, long reg, int width) return (pi.pi_data); } -static void +void write_config(const struct pcisel *sel, long reg, int width, uint32_t data) { + if (pcifd < 0 && pcifd_init()) { + return; + } + struct pci_io pi; bzero(&pi, sizeof(pi)); @@ -645,40 +675,20 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) int bus, slot, func, error, memflags; struct passthru_softc *sc; const char *value; -#ifndef WITHOUT_CAPSICUM - cap_rights_t rights; - cap_ioctl_t pci_ioctls[] = - { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR, PCIOCBARIO, PCIOCBARMMAP }; -#endif sc = NULL; error = 1; -#ifndef WITHOUT_CAPSICUM - cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE); -#endif - memflags = vm_get_memflags(ctx); if (!(memflags & VM_MEM_F_WIRED)) { warnx("passthru requires guest memory to be wired"); return (error); } - if (pcifd < 0) { - pcifd = open(_PATH_DEVPCI, O_RDWR, 0); - if (pcifd < 0) { - warn("failed to open %s", _PATH_DEVPCI); - return (error); - } + if (pcifd < 0 && pcifd_init()) { + return (error); } -#ifndef WITHOUT_CAPSICUM - if (caph_rights_limit(pcifd, &rights) == -1) - errx(EX_OSERR, "Unable to apply rights for sandbox"); - if (caph_ioctls_limit(pcifd, pci_ioctls, nitems(pci_ioctls)) == -1) - errx(EX_OSERR, "Unable to apply rights for sandbox"); -#endif - #define GET_INT_CONFIG(var, name) do { \ value = get_config_value_node(nvl, name); \ if (value == NULL) { \ diff --git a/usr.sbin/bhyve/pci_passthru.h b/usr.sbin/bhyve/pci_passthru.h new file mode 100644 index 000000000000..bdc8dc3cd0e2 --- /dev/null +++ b/usr.sbin/bhyve/pci_passthru.h @@ -0,0 +1,15 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Beckhoff Automation GmbH & Co. KG + * Author: Corvin Köhne + */ + +#pragma once + +#include + +#include "pci_emul.h" + +uint32_t read_config(const struct pcisel *sel, long reg, int width); +void write_config(const struct pcisel *sel, long reg, int width, uint32_t data);