From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0010D30BF66 for ; Tue, 2 Dec 2025 23:03:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716623; cv=none; b=Fzf8qi5PNa55kJOdDz2uEjGvcfq4zW1BnkmniNvfVFCMfxXKfd0Q8mkwSz5FwJzYjmMGlNYrG8H8DdFxnEu2zlwmxQ9kpCqogxb6UXjlHgPOXkHwPYyVZAi8iZw4pGTXA+GSlj+LWkyEGklEuzmUotj0sYdJrJIVQI34q7O7emA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716623; c=relaxed/simple; bh=5GD10ZU0KhW6zeLHK/hyK2hQghVTgOyKdURXNa1nQY0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DskgWKt5BhVTiO2aWi94HhGNBU6ohtswybmWPs62swIspnKL+A7aQVvouIjbRVSIyZRmw2zPgmfAhv8SvOCd0CaDQbCTWWPncu2QR4Cbq2V89e/1dc2s1P/PAiObqNRtSq1sPZjjiO5SuZbgPEYTGmCZhK3lVf5tajVH0VSy4qE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=i1xDRJ9O; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i1xDRJ9O" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34374bfbcccso5366480a91.0 for ; Tue, 02 Dec 2025 15:03:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716621; x=1765321421; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+fRG4Lrj/9WUyhWaNYVWHNxqEVSERVnybunCSAx6MXk=; b=i1xDRJ9ONg4/FpisOmPv4efDcanr7PczNXLkMpE/Uvn8OV9KlDxCkU6iwlJb2o8j6m a7mviwUZg+5hf40gy9AUznYO6lqWH7yjYQNONCWp3JUGxOTE9PH8Cv+Hs8nEN0GKjHIg WpAjb0YxDfM/GmiheY0aHDivoq5QloZthWbtVFhvCtq2WVg/4rRZRfEHId9IEA3fo4EF 5fPlljtKcw4tr+ts1cwc3cKmIkgDyoWd7BkTX7i6iGgq/PYWoBt3fG9RSYJrpnV73CC0 2bw6OUpTuuHgaJpsiBZGsZhgV2bKV9VkJuvhlNRErRTW0gi/P8wkSxIFFQDqNWmfWvOn J8kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716621; x=1765321421; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+fRG4Lrj/9WUyhWaNYVWHNxqEVSERVnybunCSAx6MXk=; b=mTNi/FCzdPUWlEZmoQRYrLpA9gXSf8BqDK6oMWTvPgJdbMXx0Csa17sR+bRDytusPB oo0C2ln3S2UjtpNyitMbY6v6A6TrKkym7DVRGl4njWCKOF3w+2kHF/4ozt+ntpJqe26b dSTcktaasiStUS5mov/sE5vnO02aI9cnUHK88z1L7H8kT4jSuf/4SWwpuFM0AXVg1RTP 6cZFT6Zi/UNYNHkrejxbfGjH3BpmNhMgS1D9UT/aY24f7pfrC2tyVZqgyA1yeG2sZR2q MxJWchc3QptVJI1qD9SoTZI6bBfLAhCuuT+mGXt8XNpjWuPqLh1IEkp8XHLRGav96WHX ZwwQ== X-Forwarded-Encrypted: i=1; AJvYcCVv0araZy2ZipHfXHchLnXSfXNpqxc0N63g3CTeI9yGEp2TzKUi1tgw2+alL3LdKYtAn5PXx4RK8visxgs=@vger.kernel.org X-Gm-Message-State: AOJu0YyKfcrqNaeWvDY9Md5M61ueh2W+w1FRux2vzDDgWTFMR1zDVGGB 7uZmuQkwRAx9IN1ksY57uDbule0GZ2M/1f36M++q6OhO9X0jEEBwyvmXM3kornhUvGonvb/GOkZ wEvC0JNgqJEnI6Q== X-Google-Smtp-Source: AGHT+IGsuqCnXTY/yfM/J9XhXwgiuCWsZNUKQ4WZHM9VOE70Vn+/1b9na61r8+ZiPT2RVcwfHONXe0xUL01LIQ== X-Received: from pjbnw12.prod.google.com ([2002:a17:90b:254c:b0:341:8b2e:afe9]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:51cf:b0:340:dcd4:f851 with SMTP id 98e67ed59e1d1-349125cfb65mr391052a91.3.1764716621289; Tue, 02 Dec 2025 15:03:41 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:54 +0000 In-Reply-To: <20251202230303.1017519-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-25-skhawaja@google.com> Subject: [RFC PATCH v2 24/32] iommu/vt-d: restore state of the preserved IOMMU From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" During boot fetch the preserved state of IOMMU unit and if found then restore the state. Reuse the root_table that was preserved in the previous kernel. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 30 ++++++++++++++++++++++++------ drivers/iommu/intel/iommu.h | 2 ++ drivers/iommu/intel/liveupdate.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 84fef81ecf4d..888351f91918 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -224,12 +224,12 @@ static void clear_translation_pre_enabled(struct intel_iommu *iommu) iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED; } -static void init_translation_status(struct intel_iommu *iommu) +static void init_translation_status(struct intel_iommu *iommu, bool restoring) { u32 gsts; gsts = readl(iommu->reg + DMAR_GSTS_REG); - if (gsts & DMA_GSTS_TES) + if (!restoring && (gsts & DMA_GSTS_TES)) iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED; } @@ -672,10 +672,18 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id, #endif /* iommu handling */ -static int iommu_alloc_root_entry(struct intel_iommu *iommu) +static int iommu_alloc_root_entry(struct intel_iommu *iommu, struct iommu_ser *restored_state) { struct root_entry *root; +#if CONFIG_LIVEUPDATE + if (restored_state) { + intel_iommu_liveupdate_restore_root_table(iommu, restored_state); + /* Should not be needed since the entries are already cleaned in last kernel. */ + __iommu_flush_cache(iommu, iommu->root_entry, ROOT_SIZE); + return 0; + } +#endif root = iommu_alloc_pages_node_sz(iommu->node, GFP_ATOMIC, SZ_4K); if (!root) { pr_err("Allocating root entry for %s failed\n", @@ -1616,6 +1624,7 @@ static int copy_translation_tables(struct intel_iommu *iommu) static int __init init_dmars(void) { + struct iommu_ser *iommu_ser = NULL; struct dmar_drhd_unit *drhd; struct intel_iommu *iommu; int ret; @@ -1638,8 +1647,12 @@ static int __init init_dmars(void) intel_pasid_max_id); } +#if IS_ENABLED(CONFIG_LIVEUPDATE) + iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL); +#endif + intel_iommu_init_qi(iommu); - init_translation_status(iommu); + init_translation_status(iommu, !!iommu_ser); if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { iommu_disable_translation(iommu); @@ -1653,7 +1666,7 @@ static int __init init_dmars(void) * we could share the same root & context tables * among all IOMMU's. Need to Split it later. */ - ret = iommu_alloc_root_entry(iommu); + ret = iommu_alloc_root_entry(iommu, iommu_ser); if (ret) goto free_iommu; @@ -2112,6 +2125,7 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg) static int intel_iommu_add(struct dmar_drhd_unit *dmaru) { struct intel_iommu *iommu = dmaru->iommu; + struct iommu_ser *iommu_ser = NULL; int ret; /* @@ -2120,7 +2134,11 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru) if (iommu->gcmd & DMA_GCMD_TE) iommu_disable_translation(iommu); - ret = iommu_alloc_root_entry(iommu); +#if IS_ENABLED(CONFIG_LIVEUPDATE) + iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL); +#endif + + ret = iommu_alloc_root_entry(iommu, iommu_ser); if (ret) goto out; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 1eb60ce1300f..b0c56e27f167 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_se void intel_iommu_unpreserve_device(struct device *dev, struct device_ser *device_ser); int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser); void intel_iommu_unpreserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser); +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, + struct iommu_ser *iommu_ser); bool intel_iommu_liveupdate_clear_context_entries(struct intel_iommu *iommu); #endif diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c index 3f8c7f15bc36..140887187084 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -73,6 +73,36 @@ static int preserve_iommu_context(struct intel_iommu *iommu) return ret; } +static void restore_iommu_context(struct intel_iommu *iommu) +{ + struct context_entry *context; + int i; + + for (i = 0; i < ROOT_ENTRY_NR; i++) { + context = iommu_context_addr(iommu, i, 0, 0); + if (context) + BUG_ON(!kho_restore_folio(virt_to_phys(context))); + + if (!sm_supported(iommu)) + continue; + + context = iommu_context_addr(iommu, i, 0x80, 0); + if (context) + BUG_ON(!kho_restore_folio(virt_to_phys(context))); + } +} + +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, + struct iommu_ser *iommu_ser) +{ + BUG_ON(!kho_restore_folio(iommu_ser->intel.root_table)); + iommu->root_entry = __va(iommu_ser->intel.root_table); + + restore_iommu_context(iommu); + pr_info("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n", + iommu->reg_phys, iommu_ser->intel.root_table); +} + int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_ser) { struct device_domain_info *info = dev_iommu_priv_get(dev); -- 2.52.0.158.g65b55ccf14-goog