Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp496305lqb; Wed, 29 May 2024 01:50:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXMgYjttGawO1/4sUIzrHXrSqfy4LLJ9ubHaa/K/wNEeabbHy8uOKjiBB9zdXhXv0RjHnGNQHOY/aQPQd7D50EpMMFRrM09/77mK+bS1A== X-Google-Smtp-Source: AGHT+IFQTmpEEkpBphfeo7e5j0KbTUJ6i9NMrNrwdK9krKjQb/Bd3DlzdoUfSyiDOn+/dJpzdTjL X-Received: by 2002:a17:90b:30c9:b0:2bd:f3dc:62d0 with SMTP id 98e67ed59e1d1-2bf5f74efb3mr12837338a91.37.1716972622891; Wed, 29 May 2024 01:50:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716972622; cv=pass; d=google.com; s=arc-20160816; b=V9FurqIYHPVhSCbb8RA3vBc6tXUEAj0TVplc782J8zJ+9tvJGWzURw+CMyyYbJdycw hGVZ7xHvUTtnLeXJ8nHupRqFmEKYMQhQ19AhUV0ZmVwJT8n6SV3PUUSOfxAEU28DfyCt esRKMicVPbsrbgNPJxGrLQb0V0g5u1y/l3ceNxy01YwcwRojG5ygYDtJJVa16RZttjtS UtxGVKOkA1NhjCmFmsAn43reyO+J5Xa282LxuHXWWSB2VV+fhDxKnZMySXJQcGExDxac ZeisXhmEPnsiqRV40vrhxUkRluo+u+6l2ANyAMvS+UvDT1e1u8nHtt06jMFHse18idyJ DwNQ== 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=u7VIfotmvhCnFPKh0R845OAh004BwcT1TGiZ1dQEGzo=; b=xwdvH5qja13ZY5mVnUZd5TuR+E3UOJwzkJHxiXJDsm6sjQoVdTKNYTxIlJlxOdnHtL OepVwTWWBmZmaqQzkrsnkuqsAwHr3ffsHTZXml8InNVOB7PqRzQzScLDK+x3iKx4NqdZ aAgs3AFa+a/6H2/dUi+HKbUVGNUJ2BYzznMSiRVFy9yvnC8cBhC+Iryi9JsjCSeNkC+Q PuMaW29LmBb4V4Du0rEEnF5Npv4MYo/JExOgrSiyl1Pa63vggN5L9hBwAPKjnzL95xHL 78PtfaewNQwVDqLxXM7axepoTxrd94eODspK33FiK87K38jQf0FwBLsrNlfF10rcQuj4 ZoQQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cPb1tEpP; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-193465-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193465-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bf5fe59e6asi9706533a91.141.2024.05.29.01.50.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 01:50:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-193465-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cPb1tEpP; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-193465-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193465-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B04FF28C431 for ; Wed, 29 May 2024 05:40:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1840915B96C; Wed, 29 May 2024 05:36:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="cPb1tEpP" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (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 0C5BF16A36D for ; Wed, 29 May 2024 05:36:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716960988; cv=none; b=nZ/lz/ks2W2JvNjjSb3r6/g5qXYx+LSayojnvdwjIWF0nL3yzwsJNOEIDLbY6h9ohbkPW4WN7UBQBuN2g3TqRh9CQ9D82r85T9KXgZQzp9AFGaoOccDrfptYzo+oyg1Tu8hKJW9rKHPByiikf+cFdU9qkwxfMiaIn9FbUmnAFC8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716960988; c=relaxed/simple; bh=kAf7SPBLso5zp49wUYubqs0YKzd/lC+tt+i0+MOxu64=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jd86ZU6mlXlpTkXmqTm2M8hXaoNmLLSXtvRmNjbNr8NiX/JZIzSPZn5mlJ1bmvUbIOkG5KQZNkz5Mr6QWRAF76B3lWNS4YdDsR8XsYXFPHqLioBPBCy5HwfoMLEOR4imMA2FVQvlIbVk9Gh7ZAYehrf1pphzwFOFgP1VdIDd8iU= 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=cPb1tEpP; arc=none smtp.client-ip=192.198.163.14 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=1716960987; x=1748496987; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kAf7SPBLso5zp49wUYubqs0YKzd/lC+tt+i0+MOxu64=; b=cPb1tEpPZcXN2opL0krE7oHdFJVjBHYvf8Opyhfe2vta2Dlz5K6w87az wLnSIC/o2IwU4bU1qCl4K0ACweiOOjuhvnz/D6I256lJkQ0OMIBER0M9M EyzpsLCycwjqORZntf3YMkHDEWlzcGeGv4L2u5yZ7uswENy/4UkHjztfQ S/jDOHPo/+5uP7pGwbiP5IpSujhUqmSiaNE+9uvujc0MywSkykEdLqZTR n6UjRqWtLsHoBQMuloLhLs1hl0/gP9Yqxs5qlqLTmUDHDkttlFf3Or1Md h/LYqasIL8nZNJj2vfyWyOc5qManwLmJxt85HPr8jbvLuJBI2U4oAwNoB w==; X-CSE-ConnectionGUID: mJDRITGyQi2GWrdD2WVXSg== X-CSE-MsgGUID: NBnqpYlhQvOAkwYbWABSFw== X-IronPort-AV: E=McAfee;i="6600,9927,11085"; a="13569053" X-IronPort-AV: E=Sophos;i="6.08,197,1712646000"; d="scan'208";a="13569053" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2024 22:36:12 -0700 X-CSE-ConnectionGUID: cCZToLUCTQGJ9JPHPmk0WA== X-CSE-MsgGUID: 3EA9i3ihQEyIhlN5IVrqow== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,197,1712646000"; d="scan'208";a="35257754" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by fmviesa007.fm.intel.com with ESMTP; 28 May 2024 22:36:07 -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 , iommu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH 15/20] iommu/vt-d: Add helper to allocate paging domain Date: Wed, 29 May 2024 13:32:45 +0800 Message-Id: <20240529053250.91284-16-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529053250.91284-1-baolu.lu@linux.intel.com> References: <20240529053250.91284-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