Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp998353rwr; Thu, 27 Apr 2023 10:50:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5v5J8d7hyiuO6Yw63x62Yu4Stxq+TMmrZA0G2CNhVcXnigV04zQR0muPRqEpoweWc8Zd7A X-Received: by 2002:a05:6a20:441b:b0:f2:4c39:8028 with SMTP id ce27-20020a056a20441b00b000f24c398028mr3518594pzb.21.1682617800351; Thu, 27 Apr 2023 10:50:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682617800; cv=none; d=google.com; s=arc-20160816; b=NifQgR8oKqG+rrD7EqaPeTrNNWrYLS6uEQi0jHUxU4kOGnoTLONQR1sNzFE9PxO28t +pAXIJI31ZYA21tbxKAS3OgXHVQOyTvp8b6oOYK5qklqhywjs8qRyjDHm6eLJwqP07IP j8aM8dv6x/jUBpziMZ46JMMgP+gmfpMVLRCNluMFSZEdK7aiULlNth29SDFPzjCMcT8h I4mYAxZS1REhFhxY1cFDAyWoyaNhriBFqSplgwxIu9HXK0KXhWNdrkEWDm521pky2NNE MeEfdvlo6h0NrFTIiqnkPzbwRTBLxGhGbpXRkndfBG859CvbUYr2b1q4kvwtmXAo3vou X2Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=siExPDwRqpNUg9gcNtnHOrJYSVkLyb9jnu4QU/J/BPw=; b=DHwmdunFSPGwj/NQcB+Y7ipLL8FHRikrwYt41lR0273gT47OEF0FdR1DtdT4ZexEao OMeDmF7CWqTrZhzRXEu2SXvlhBowyDtoNjKXXQx8tuioYq3mkTvR03DeB2PpISP8bTOU 6S4bfmxJ6rTsdiIJQEeiBLyCKhLIqPkTl5lMMFpb8cUoRUkh9kamTvzLAAe79pYzCd4a Jvu2eKDevIWfVHR/fGve4iM/pUTUWBZ98jJdoFQg0n2IQnTRQVzSWesqQ+9tQbyvsl2G nILbmsmbL2E6vUS4pZGNJeniDRKILALOO/Dv9GQJjHSOUn9kkXZRGReCV/8RSpae1yAZ xsmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LTb2mQ9B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e65-20020a636944000000b005139e210d10si19541134pgc.587.2023.04.27.10.49.47; Thu, 27 Apr 2023 10:50:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LTb2mQ9B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244467AbjD0RqJ (ORCPT + 99 others); Thu, 27 Apr 2023 13:46:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244243AbjD0Rpq (ORCPT ); Thu, 27 Apr 2023 13:45:46 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A882469A; Thu, 27 Apr 2023 10:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682617543; x=1714153543; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=b47cHt2vFYeXlFrXhgvZHs8+gCR1tr502DezdYmRDRI=; b=LTb2mQ9BPw3FkJsviFfKH6Hr5ER/NiZIEQdZCJJEDV/R5W73GuG6NXtr aQbWaRlYkJI+pPyfpTQh2oZTVnd7mRrCXwUDdD5S+/1o8p0vixA86dQYF Er6e+Rb9X2EYuwnNKrOtYY4C1nMEOUiaCSemLonpNbVqQZDh8cCeK6sFq JcW6yOEqIkglEtlhcGrKc2e/fiofys72UdAmC1+50bEqCWjttLRbU8YLo CFkdbjnbOJhwd0sY7VnOs1W0rRHezBVqSpbV2GPbpzyosYoQ092yVdl+b IfvlP/TmbcvEmVNnB53iPbxkjJKHKIjiAr5zemHYDhSjCXs1kIZX09ORC w==; X-IronPort-AV: E=McAfee;i="6600,9927,10693"; a="350398745" X-IronPort-AV: E=Sophos;i="5.99,232,1677571200"; d="scan'208";a="350398745" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2023 10:45:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10693"; a="1024219723" X-IronPort-AV: E=Sophos;i="5.99,232,1677571200"; d="scan'208";a="1024219723" Received: from srinivas-otcpl-7600.jf.intel.com (HELO jacob-builder.jf.intel.com) ([10.54.39.106]) by fmsmga005.fm.intel.com with ESMTP; 27 Apr 2023 10:45:23 -0700 From: Jacob Pan To: LKML , iommu@lists.linux.dev, "Robin Murphy" , Jason Gunthorpe , "Lu Baolu" , Joerg Roedel , dmaengine@vger.kernel.org, vkoul@kernel.org Cc: "Will Deacon" , David Woodhouse , Raj Ashok , "Tian, Kevin" , Yi Liu , "Yu, Fenghua" , Dave Jiang , Tony Luck , "Zanussi, Tom" , narayan.ranganathan@intel.com, Jacob Pan Subject: [PATCH v5 6/7] iommu/vt-d: Implement set_dev_pasid domain op Date: Thu, 27 Apr 2023 10:49:36 -0700 Message-Id: <20230427174937.471668-7-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230427174937.471668-1-jacob.jun.pan@linux.intel.com> References: <20230427174937.471668-1-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Devices that use ENQCMDS to submit work on buffers mapped by DMA API must attach a PASID to the default domain of the device. In preparation for this use case, this patch implements set_dev_pasid() for the default_domain_ops. If the device context has not been set up prior to this call, this will set up the device context in addition to PASID attachment. Signed-off-by: Jacob Pan --- drivers/iommu/intel/iommu.c | 92 ++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 388453a7415e..f9d6c31cdc8e 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -278,6 +278,8 @@ static LIST_HEAD(dmar_satc_units); list_for_each_entry(rmrr, &dmar_rmrr_units, list) static void device_block_translation(struct device *dev); +static void intel_iommu_detach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid); static void intel_iommu_domain_free(struct iommu_domain *domain); int dmar_disabled = !IS_ENABLED(CONFIG_INTEL_IOMMU_DEFAULT_ON); @@ -4091,8 +4093,7 @@ static void device_block_translation(struct device *dev) iommu_disable_pci_caps(info); if (!dev_is_real_dma_subdevice(dev)) { if (sm_supported(iommu)) - intel_pasid_tear_down_entry(iommu, dev, - IOMMU_DEF_RID_PASID, false); + intel_iommu_detach_device_pasid(&info->domain->domain, dev, IOMMU_DEF_RID_PASID); else domain_context_clear(info); } @@ -4761,28 +4762,86 @@ static void intel_iommu_iotlb_sync_map(struct iommu_domain *domain, __mapping_notify_one(info->iommu, dmar_domain, pfn, pages); } -static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) +static void intel_iommu_detach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) { - struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); - struct iommu_domain *domain; + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + struct device_pasid_info *i, *dev_pasid = NULL; + struct intel_iommu *iommu = info->iommu; + unsigned long flags; - /* Domain type specific cleanup: */ - domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0); - if (domain) { - switch (domain->type) { - case IOMMU_DOMAIN_SVA: - intel_svm_remove_dev_pasid(dev, pasid); - break; - default: - /* should never reach here */ - WARN_ON(1); + spin_lock_irqsave(&dmar_domain->lock, flags); + list_for_each_entry(i, &dmar_domain->dev_pasids, link_domain) { + if (i->dev == dev && i->pasid == pasid) { + list_del(&i->link_domain); + dev_pasid = i; break; } } + spin_unlock_irqrestore(&dmar_domain->lock, flags); + if (WARN_ON(!dev_pasid)) + return; + + /* PASID entry already cleared during SVA unbind */ + if (domain->type != IOMMU_DOMAIN_SVA) + intel_pasid_tear_down_entry(iommu, dev, pasid, false); + + kfree(dev_pasid); +} + +static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain; + struct iommu_domain *domain; + + domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0); + dmar_domain = to_dmar_domain(domain); + + /* + * SVA Domain type specific cleanup: Not ideal but not until we have + * IOPF capable domain specific ops, we need this special case. + */ + if (domain->type == IOMMU_DOMAIN_SVA) + return intel_svm_remove_dev_pasid(dev, pasid); - intel_pasid_tear_down_entry(iommu, dev, pasid, false); + intel_iommu_detach_device_pasid(domain, dev, pasid); + domain_detach_iommu(dmar_domain, info->iommu); } +static int intel_iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + struct intel_iommu *iommu = info->iommu; + int ret; + + if (!pasid_supported(iommu)) + return -ENODEV; + + ret = prepare_domain_attach_device(domain, dev); + if (ret) + return ret; + + /* + * Most likely the device context has already been set up, will only + * take a domain ID reference. Otherwise, device context will be set + * up here. + * The upper layer APIs make no assumption about the ordering between + * device attachment and the PASID attachment. + */ + ret = dmar_domain_attach_device(to_dmar_domain(domain), dev); + if (ret) { + dev_err(dev, "Attach device failed\n"); + return ret; + } + return dmar_domain_attach_device_pasid(dmar_domain, iommu, dev, pasid); +} + + + const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, .domain_alloc = intel_iommu_domain_alloc, @@ -4802,6 +4861,7 @@ const struct iommu_ops intel_iommu_ops = { #endif .default_domain_ops = &(const struct iommu_domain_ops) { .attach_dev = intel_iommu_attach_device, + .set_dev_pasid = intel_iommu_attach_device_pasid, .map_pages = intel_iommu_map_pages, .unmap_pages = intel_iommu_unmap_pages, .iotlb_sync_map = intel_iommu_iotlb_sync_map, -- 2.25.1