From nobody Mon Oct 20 21:08:14 2025 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 4cr7Lk2vLWz6D7xm; Mon, 20 Oct 2025 21:08:14 +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 "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cr7Lk2JSSz3VCN; Mon, 20 Oct 2025 21:08:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1760994494; 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=NRfO8uaBTuyyoitHe/CxsFvi4cPoD3YG2Z08Ye/rsOY=; b=xnnaO18qjeURMx6jV6JlLv7w43I5T0pTio+bfQKFbtQzyEMIp6x7j83zwdbuU84GmCpcCM Y1SHv407TPYySktRxt1IPXx+uChYia42/LSmF/LMJegJ/45lQQqJs4XLk1UcMUGA7poCRe 4Sna7/h+5PNT21XeNptnwy3PwFXlDbo7aUVEFM3NCTWrD2istN+GUcncwzcItn2w+dDAhd OHL6KmKobZ9nYyZVtWz0OWZac08Yue9Jjb7veiN/gnvVt8jlF+HhhDtHOwfHg7n0L1KlP1 q/YccYX6gEuKsHVmJIDPsPsx0Al3YMDEt2uFNVnNwjVQQqvk8ERdu1lnN1uk0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1760994494; 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=NRfO8uaBTuyyoitHe/CxsFvi4cPoD3YG2Z08Ye/rsOY=; b=I6A6yF5Oh6qJAym9UlfyITEz+5+4GOCiPaUGAzbe/XWEKvzyOS6VncbnxMEMM3xsAHiWiZ fwYw2HTHc19di2stlvMV3kI7qGhn4lXBWD0aKDRj76TqM+wLYC2bK6UwsHZOX41/6FWUg6 5A2jhZop0WvpcSDLAjhoUrZQEqViEsi+BCZdbULFlMkm6e3AaaWLskkrjNBDnvLjls2FwP pEYBZIQ8Gw+sCiuLVvM9ulYTgj7hLmxCsZawwMd1J1X3SsN6izvz2Olhb0Fo8LVbZZyyMX Jwuih3Oxz5oqEkn9o83NGZq2N10hUOX8f4DzhdVy0BohUJT3QGUNmdB6sW+XRA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1760994494; a=rsa-sha256; cv=none; b=mcYI7HHT7K3ZpB5uTOPODsM2L57m7Nsyws7CW7D5e5fF5yPDNkSLH7FkSoN4WTlhG7hbhh yIwgqAZoQ9ojHQa9vKIPD11OmWGs7eyMyV4G8CyQOKPDwyL4P7zDtOKDEIdIYVH/Kppk5I 4/2qClh64mtfGhndDCex2gF9ryLCDj/JjI8f1bNUrmRHfZtTS4aOzOc3sY2GK7xmgDcPio yAhULQJTz1ar8Q+mdkGA4EeNjajZpJI63bte5NWz/D8T9EoKk7whLF/b4s83gLyLRkC95k jtCq5iGKAMAxN45eR6JAZgV+9HifCtKSwhZELhwcHzt0CclzmkBer7DKL1EAkA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4cr7Lk1pQzzTVv; Mon, 20 Oct 2025 21:08:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 59KL8Ena092060; Mon, 20 Oct 2025 21:08:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 59KL8EmS092056; Mon, 20 Oct 2025 21:08:14 GMT (envelope-from git) Date: Mon, 20 Oct 2025 21:08:14 GMT Message-Id: <202510202108.59KL8EmS092056@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Jason A. Harmening" Subject: git: 35170408a7b8 - main - AMD IOMMU: fix per-device IOMMU bypass when IR is enabled 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jah X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 35170408a7b86a879d7e2ffb5ddb70fe75951d89 Auto-Submitted: auto-generated The branch main has been updated by jah: URL: https://cgit.FreeBSD.org/src/commit/?id=35170408a7b86a879d7e2ffb5ddb70fe75951d89 commit 35170408a7b86a879d7e2ffb5ddb70fe75951d89 Author: Jason A. Harmening AuthorDate: 2025-10-20 01:53:08 +0000 Commit: Jason A. Harmening CommitDate: 2025-10-20 21:06:47 +0000 AMD IOMMU: fix per-device IOMMU bypass when IR is enabled When interrupt remapping (IR) is enabled, the device table entry (DTE) for a given device will likely be initialized by amdiommu_ir_find() during MSI configuration. This function directly calls amdiommu_get_ctx_for_dev() with id_mapped=false, which means that any attempt to disable DMA remapping for the device (e.g. by setting hw.busdma.pciD.B.S.F='bounce' in the loader tunables) will effectively be ignored as the paging mode field in the DTE will not be set to 0 as required for identity mapping. This will ultimately produce an unusable device, because busdma will later observe the bounce configuration through iommu_instantiate_ctx() and will employ the non-translated 'bounce' busdma methods for the device, while the DTE remains configured to enable translation. Fix this by tweaking iommu_instantiate_ctx() to always return the relevant per-device context object even if translation is disabled, and adopt it in amdiommu_ir_find() instead of directly calling amdiommu_get_ctx_for_dev(). Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D53209 --- sys/dev/iommu/busdma_iommu.c | 7 +++++-- sys/x86/iommu/amd_intrmap.c | 9 ++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/sys/dev/iommu/busdma_iommu.c b/sys/dev/iommu/busdma_iommu.c index 668ccf056463..82f73d469585 100644 --- a/sys/dev/iommu/busdma_iommu.c +++ b/sys/dev/iommu/busdma_iommu.c @@ -295,7 +295,6 @@ iommu_instantiate_ctx(struct iommu_unit *unit, device_t dev, bool rmrr) } else { iommu_free_ctx_locked(unit, ctx); } - ctx = NULL; } return (ctx); } @@ -303,6 +302,7 @@ iommu_instantiate_ctx(struct iommu_unit *unit, device_t dev, bool rmrr) struct iommu_ctx * iommu_get_dev_ctx(device_t dev) { + struct iommu_ctx *ctx; struct iommu_unit *unit; unit = iommu_find(dev, bootverbose); @@ -313,7 +313,10 @@ iommu_get_dev_ctx(device_t dev) return (NULL); iommu_unit_pre_instantiate_ctx(unit); - return (iommu_instantiate_ctx(unit, dev, false)); + ctx = iommu_instantiate_ctx(unit, dev, false); + if (ctx != NULL && (ctx->flags & IOMMU_CTX_DISABLED) != 0) + ctx = NULL; + return (ctx); } bus_dma_tag_t diff --git a/sys/x86/iommu/amd_intrmap.c b/sys/x86/iommu/amd_intrmap.c index f8900fe0561f..cce4f57ca323 100644 --- a/sys/x86/iommu/amd_intrmap.c +++ b/sys/x86/iommu/amd_intrmap.c @@ -223,9 +223,9 @@ static struct amdiommu_ctx * amdiommu_ir_find(device_t src, uint16_t *ridp, bool *is_iommu) { devclass_t src_class; - device_t requester; struct amdiommu_unit *unit; struct amdiommu_ctx *ctx; + struct iommu_ctx *ioctx; uint32_t edte; uint16_t rid; uint8_t dte; @@ -255,10 +255,9 @@ amdiommu_ir_find(device_t src, uint16_t *ridp, bool *is_iommu) error = amdiommu_find_unit(src, &unit, &rid, &dte, &edte, bootverbose); if (error == 0) { - error = iommu_get_requester(src, &requester, &rid); - MPASS(error == 0); - ctx = amdiommu_get_ctx_for_dev(unit, src, - rid, 0, false /* XXXKIB */, false, dte, edte); + ioctx = iommu_instantiate_ctx(AMD2IOMMU(unit), src, false); + if (ioctx != NULL) + ctx = IOCTX2CTX(ioctx); } } if (ridp != NULL)