Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp338250lqp; Mon, 10 Jun 2024 06:00:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUM41OhBW8gt+g6kVUBP2PDNiuKLD6qlpgQQlKHQ2eTQE9j5J9Hl+LK45AvwP8vZOg2+YA99N26cJXkwOQ5QvaUHfSFcREQTgYsolAAGg== X-Google-Smtp-Source: AGHT+IECcYFc2AopMJgiUm1/7vr+LGD6bJWakk8k7yWrSn6peWEZf1SBLizHLrNpNUh7/FO7ibZq X-Received: by 2002:a17:90a:c87:b0:2bd:7e38:798e with SMTP id 98e67ed59e1d1-2c2bcc0ba89mr7679512a91.28.1718024403364; Mon, 10 Jun 2024 06:00:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718024403; cv=pass; d=google.com; s=arc-20160816; b=IZh8/+ucl+4HJTRHJ539DqrNLnKFprHX72JHK0f3EDMhB7Yb0dUhgNHFc8yMtsP+Cr KzOFXkhE/hHEe244cEOYqxLAZbhYrsdrjKf0LeNT3paa3QI6nPjKtcKxVTvnBKpeddFc 9yFrzLYGFJp0PH/KRvJhU5aGOOIxwKWMBO9CLrkLydRHT6VF7CtojcK8TZj3XjxDFw2U 61nxnJ0Ik4uwBRfv0hI0nYCSgk07faNA8kGxlAYfOijPlL6qRN3YsUwS1ISJihOGlr2P yIXZNbgijWk1eLSBIk5NEOyVriFf+iGfFR+5ilfX3a9ow4n4zmd8BB9qrrqB4xSWbsZy W7Rg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=fr40brAswpmGmhMFrh6KcZXgHsqtbEnyu9zzTU7pYWQ=; fh=oMfgzkEw/ooUXqvARCgNaSZrpwGw5y27+DEhhPidNdU=; b=O1K56Qf5peu0EthynzB3DhIaZd0gd8PAksd7iiwt2saE4Y5mzUmY6raIEwHnwEyowq CQeLSPqvBBvYkxCpak17Fn9ldVO47knXtUtsPA3V9hp30OKf3wm9eBu9xpYqxSUuojhM JXehHZSNja4tkBLJDaXlMaijhjFVF8NKV+3rux3c/l2XEK4WveGGmEZ6xSwCiHKQH0sV 3IqgEoFsGUgBnUi1GmnEuMOGC0T+VQsTejnCqHCMhbDeqVFsCzxmVwG+O24aeiPoGqXt y1DAuwU+HfSS+LhwE8d28COdjnNWN2I6vRQvx6/hKPzF3zKTH1niYj0t/5jmKuVbrpMD 3mJg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Uq1ealpY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-207906-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207906-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c2d113a288si4909690a91.150.2024.06.10.06.00.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 06:00:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-207906-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Uq1ealpY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-207906-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207906-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 31D4DB249FC for ; Mon, 10 Jun 2024 09:04:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0E4D879DDB; Mon, 10 Jun 2024 08:59:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Uq1ealpY" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB6BF12F397 for ; Mon, 10 Jun 2024 08:59:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718009976; cv=none; b=N22kIc0XMKvwQ9Wsw/u9HbdTvCdjG4ORKqxrOAX2PfCXaRYdOP6MpI/rgUuEvqkJShuWI5DsdiMOVsP11qrIuWE9DyRbyvHODv+9lLM8POUqhNuIsmPE8SVff7LxJ4gVyjJEKW4r0vHBA9MmkEHoCifVV62t0Uz0RrWkgqsPXbI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718009976; c=relaxed/simple; bh=kAf7SPBLso5zp49wUYubqs0YKzd/lC+tt+i0+MOxu64=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mjrYeoJTQZA39YnvhirzUpwhmOnLbkj53iXiidupRL9IaiHc0O8oOPs7ZYOJcLJQpU4Bzr1aA4+jBxp70txhtj8uZH/SJRmshzf/EU0Jj/x0PDheiWyUZ0blYLvBpB57ArsXJamhjR3FTWZ5hqfZnZ3iUJSWfjb87fL5w/ClttA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Uq1ealpY; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718009974; x=1749545974; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kAf7SPBLso5zp49wUYubqs0YKzd/lC+tt+i0+MOxu64=; b=Uq1ealpY4p14ZSL7xW/ZJSu5oMw2bkJy+4y5B7rZSU2KCvMWBLysszFS 2A5tq4lBAwzYR8zUDAGiirNmQxHkUyHUMeX75r/KivKQyT7v0G+1niwH3 kzBnsZ6uidEv77gRspEkt7KUjFuNAjDJ3WiA3OtVeIgo0XQKFsyATUHSU wxMJv6QOwxNGDGGe+o6JhScDjv/lFvZaWXwUpsGPxj5BP5bpDoDCT3rZg 3YHELKRNiOvuZYA6+y1yJ9H0GDqp06UGmqQDToVatgL++ChrOQ4mqaUbF uYlV0XtFYfJ7f4t/XeLmFXYlNwMJyWYAkXGHSNM2UHbKb1GzTsSakXQXm w==; X-CSE-ConnectionGUID: EtOWuowtRBKyEbMMOTP4Ug== X-CSE-MsgGUID: Qcjl1ZJuSGG0E615LVv1fQ== X-IronPort-AV: E=McAfee;i="6600,9927,11098"; a="17581898" X-IronPort-AV: E=Sophos;i="6.08,227,1712646000"; d="scan'208";a="17581898" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jun 2024 01:59:34 -0700 X-CSE-ConnectionGUID: IZUNZ8cdRjO1AyWUbBMniA== X-CSE-MsgGUID: oS6yPYCwQEWB97mfMcpzCQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,227,1712646000"; d="scan'208";a="43432851" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by fmviesa003.fm.intel.com with ESMTP; 10 Jun 2024 01:59:30 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian Cc: Yi Liu , David Airlie , Daniel Vetter , Kalle Valo , Bjorn Andersson , Mathieu Poirier , Alex Williamson , mst@redhat.com, Jason Wang , Thierry Reding , Jonathan Hunter , Mikko Perttunen , Jeff Johnson , ath10k@lists.infradead.org, ath11k@lists.infradead.org, iommu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 15/21] iommu/vt-d: Add helper to allocate paging domain Date: Mon, 10 Jun 2024 16:55:49 +0800 Message-Id: <20240610085555.88197-16-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240610085555.88197-1-baolu.lu@linux.intel.com> References: <20240610085555.88197-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The domain_alloc_user operation is currently implemented by allocating a paging domain using iommu_domain_alloc(). This is because it needs to fully initialize the domain before return. Add a helper to do this to avoid using iommu_domain_alloc(). Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 87 +++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 2e9811bf2a4e..ccde5f5972e4 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -3633,6 +3633,79 @@ static struct iommu_domain blocking_domain = { } }; +static int iommu_superpage_capability(struct intel_iommu *iommu, bool first_stage) +{ + if (!intel_iommu_superpage) + return 0; + + if (first_stage) + return cap_fl1gp_support(iommu->cap) ? 2 : 1; + + return fls(cap_super_page_val(iommu->cap)); +} + +static struct dmar_domain *paging_domain_alloc(struct device *dev, bool first_stage) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + struct dmar_domain *domain; + int addr_width; + + domain = kzalloc(sizeof(*domain), GFP_KERNEL); + if (!domain) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&domain->devices); + INIT_LIST_HEAD(&domain->dev_pasids); + INIT_LIST_HEAD(&domain->cache_tags); + spin_lock_init(&domain->lock); + spin_lock_init(&domain->cache_lock); + xa_init(&domain->iommu_array); + + domain->nid = dev_to_node(dev); + domain->has_iotlb_device = info->ats_enabled; + domain->use_first_level = first_stage; + + /* calculate the address width */ + addr_width = agaw_to_width(iommu->agaw); + if (addr_width > cap_mgaw(iommu->cap)) + addr_width = cap_mgaw(iommu->cap); + domain->gaw = addr_width; + domain->agaw = iommu->agaw; + domain->max_addr = __DOMAIN_MAX_ADDR(addr_width); + + /* iommu memory access coherency */ + domain->iommu_coherency = iommu_paging_structure_coherency(iommu); + + /* pagesize bitmap */ + domain->domain.pgsize_bitmap = SZ_4K; + domain->iommu_superpage = iommu_superpage_capability(iommu, first_stage); + domain->domain.pgsize_bitmap |= domain_super_pgsize_bitmap(domain); + + /* + * IOVA aperture: First-level translation restricts the input-address + * to a canonical address (i.e., address bits 63:N have the same value + * as address bit [N-1], where N is 48-bits with 4-level paging and + * 57-bits with 5-level paging). Hence, skip bit [N-1]. + */ + domain->domain.geometry.force_aperture = true; + domain->domain.geometry.aperture_start = 0; + if (first_stage) + domain->domain.geometry.aperture_end = __DOMAIN_MAX_ADDR(domain->gaw - 1); + else + domain->domain.geometry.aperture_end = __DOMAIN_MAX_ADDR(domain->gaw); + + /* always allocate the top pgd */ + domain->pgd = iommu_alloc_page_node(domain->nid, GFP_KERNEL); + if (!domain->pgd) { + kfree(domain); + return ERR_PTR(-ENOMEM); + } + domain_flush_cache(domain, domain->pgd, PAGE_SIZE); + + return domain; +} + static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) { struct dmar_domain *dmar_domain; @@ -3695,15 +3768,11 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags, if (user_data || (dirty_tracking && !ssads_supported(iommu))) return ERR_PTR(-EOPNOTSUPP); - /* - * domain_alloc_user op needs to fully initialize a domain before - * return, so uses iommu_domain_alloc() here for simple. - */ - domain = iommu_domain_alloc(dev->bus); - if (!domain) - return ERR_PTR(-ENOMEM); - - dmar_domain = to_dmar_domain(domain); + /* Do not use first stage for user domain translation. */ + dmar_domain = paging_domain_alloc(dev, false); + if (IS_ERR(dmar_domain)) + return ERR_CAST(dmar_domain); + domain = &dmar_domain->domain; if (nested_parent) { dmar_domain->nested_parent = true; -- 2.34.1