From nobody Wed Dec 31 10:32:50 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEB58C4332F for ; Mon, 6 Nov 2023 07:14:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230517AbjKFHOP (ORCPT ); Mon, 6 Nov 2023 02:14:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230520AbjKFHOL (ORCPT ); Mon, 6 Nov 2023 02:14:11 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF7FA125 for ; Sun, 5 Nov 2023 23:14:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254849; x=1730790849; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AMX3QyiSsHY62hL/1ZifmtDUB3BOUarR8RpIWdZzzu4=; b=FDDD+bquOdN9x9TArfJUiGpReYtymTnUV90mF95hYEZLClkSsEVGfJ7N PF33sL4AvuZ2olIVqSJVdWzNmwPl+0tkNRV4ZWpBVS5sJJ3rQfcQ/R3kE XD4UyKV5PSC22a4W6JzT/iOOi7r9II/JYVS/r3i0OJw8kJoZS1HHjTE0T JPhedcIZa6wJgf0v+wqEnF0MLW/YkcgKq+07dqhtm880kVI8f5FqniFpH 6nKReK6cL8V8/zwUgDVm/sxX81PGBauo+CCZvA8We6UDWopLEK3UaKLZt PegyURF0zzxfvNCDUr0Sn80WH+xOTU1S+Iv0WNgXbdOZqmYRJll4YR9jq w==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759067" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759067" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690901" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690901" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:14:05 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 4/5] iommu/vt-d: Adapt alloc_pgtable interface to be used by others Date: Mon, 6 Nov 2023 02:12:25 -0500 Message-Id: <20231106071226.9656-5-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The generic IO page table framework provides a set of interfaces for invoking IO page table operations. Other entity (e.g., virtio-iommu driver) can use the interface to ask VT-d driver to generate a VT-d format IO page table. This patch adds the support. Signed-off-by: Tina Zhang --- drivers/iommu/intel/iommu.c | 69 +++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 80bd1993861c..d714e780a031 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -5248,17 +5248,80 @@ static phys_addr_t pgtable_iova_to_phys(struct io_p= gtable_ops *ops, return intel_iommu_iova_to_phys(&dmar_domain->domain, iova); } =20 +static void __iommu_calculate_cfg(struct io_pgtable_cfg *cfg) +{ + unsigned long fl_sagaw, sl_sagaw, sagaw; + int agaw, addr_width; + + fl_sagaw =3D BIT(2) | (cap_fl5lp_support(cfg->vtd_cfg.cap_reg) ? BIT(3) := 0); + sl_sagaw =3D cap_sagaw(cfg->vtd_cfg.cap_reg); + sagaw =3D fl_sagaw & sl_sagaw; + + for (agaw =3D width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH); agaw >=3D 0; a= gaw--) { + if (test_bit(agaw, &sagaw)) + break; + } + + addr_width =3D agaw_to_width(agaw); + if (cfg->ias > addr_width) + cfg->ias =3D addr_width; + if (cfg->oas !=3D addr_width) + cfg->oas =3D addr_width; +} + static struct io_pgtable *alloc_pgtable(struct io_pgtable_cfg *cfg, void *= cookie) { - struct dmar_io_pgtable *pgtable =3D io_pgtable_cfg_to_dmar_pgtable(cfg); + struct dmar_io_pgtable *pgtable; + struct dmar_domain *domain; + int adjust_width; + + /* Platform must have nested translation support */ + if (!ecap_nest(cfg->vtd_cfg.ecap_reg)) + return NULL; + + domain =3D kzalloc(sizeof(*domain), GFP_KERNEL); + if (!domain) + return NULL; + + domain->nid =3D NUMA_NO_NODE; + domain->use_first_level =3D true; + domain->has_iotlb_device =3D false; + INIT_LIST_HEAD(&domain->devices); + spin_lock_init(&domain->lock); + xa_init(&domain->iommu_array); + + /* calculate AGAW */ + __iommu_calculate_cfg(cfg); + domain->gaw =3D cfg->ias; + adjust_width =3D guestwidth_to_adjustwidth(domain->gaw); + domain->agaw =3D width_to_agaw(adjust_width); + + domain->iommu_coherency =3D ecap_smpwc(cfg->vtd_cfg.ecap_reg); + domain->force_snooping =3D true; + domain->iommu_superpage =3D cap_fl1gp_support(cfg->vtd_cfg.ecap_reg) ? 2 = : 1; + domain->max_addr =3D 0; + + cfg->coherent_walk =3D domain->iommu_coherency; + + pgtable =3D &domain->dmar_iop; =20 + /* always allocate the top pgd */ + domain->pgd =3D alloc_pgtable_page(domain->nid, GFP_KERNEL); + if (!domain->pgd) + goto out_free_domain; + domain_flush_cache(domain, domain->pgd, PAGE_SIZE); + + cfg->virt.pgd =3D virt_to_phys(domain->pgd); + cfg->tlb =3D &flush_ops; pgtable->iop.ops.map_pages =3D pgtable_map_pages; pgtable->iop.ops.unmap_pages =3D pgtable_unmap_pages; pgtable->iop.ops.iova_to_phys =3D pgtable_iova_to_phys; =20 - cfg->tlb =3D &flush_ops; - return &pgtable->iop; + +out_free_domain: + kfree(domain); + return NULL; } =20 struct io_pgtable_init_fns io_pgtable_intel_iommu_init_fns =3D { --=20 2.39.3