From patchwork Thu Jul 27 05:48:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 126669 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp886271vqo; Wed, 26 Jul 2023 23:19:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlH1XNIL4h6qdLVqvyurCCSSLC9COMDT+xcyHPeoyltwrmm1/HT/dRizJWBPs7RJdph4XA31 X-Received: by 2002:a05:6358:52c3:b0:134:ece4:1e74 with SMTP id z3-20020a05635852c300b00134ece41e74mr1982495rwz.21.1690438754174; Wed, 26 Jul 2023 23:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690438754; cv=none; d=google.com; s=arc-20160816; b=bBdhmFpFYoFbanYdF0rOBs58dOa2rMH2rbTS35zFiIxuWgvXdHbmwz+bTESskz4Uj0 RNag8WZhEt8U5WqGi3xFyX88+ee64fT/wlQDxCQX0baIQk+2gHZ0LrA3d3NPBzyJMR6D 7e1rkwtwlL21mWj2fv10KnPMNdrUy95v5xSx+HHJk5nzxNMY4d3uVlFaUyw9oNl/1jjq p7i/kx3KzjjMdTHLy00JMUFSBTz/aK2JL0ud8wS+eTml2mgjpRpFpRorVOU/lRUSJnTr uvKBhCDf/gV3Cj5VQgz4kvQP1Jh/mQ7W8/BmPeX/FSRkZN+IHyZkaJoZ/fYXSBqoiWY1 7dlQ== 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=lYfO4bCj6wpb4T1tf964ym3c4i9iCRnBtlgGfXYTbyc=; fh=5v0QJkujOnxKSzhmeWiMX/0q9X35NwMz+SRMkKa4moc=; b=yaBade0gqcOwiVIbx0fEkRftmnI5vY7fzzMyhHRU8LuR6cfHUVhkjuqj1LZofu2MjD fimhjQ2EchB1Awf0o0M1mzlyGLvQ79V5jgU0L2416vPGlO2UsKCaViNB6dA3RGXw6sYM 1aon0uAXD7VO+Vj6TePpdVLkaRMRMNgiwxoc5E24mPDO1njUzTSBIFoFOfoXVXvpiUkS 9OwuMDFMH7y9U2kln2d1EwXUHJ8LNUQx/YMxstC60z7dN3xV0YdY5IHVOfwHiWK6duem CUBV3kBc+5TJZTP03eCyXyBjyAzPAGEO/9Zidseny+gXxpla15YmZ7dW+PuvalpXcWLT 4G0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HxCaC5UZ; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o13-20020a63920d000000b005633d1402a2si743619pgd.440.2023.07.26.23.19.00; Wed, 26 Jul 2023 23:19:14 -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=HxCaC5UZ; 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 S232697AbjG0Fxj (ORCPT + 99 others); Thu, 27 Jul 2023 01:53:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232864AbjG0FxJ (ORCPT ); Thu, 27 Jul 2023 01:53:09 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 592D11FF3; Wed, 26 Jul 2023 22:52:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690437149; x=1721973149; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OrB4GkbjupQVUOggSlcnw0nzLKoABWATL0bGy51+mmY=; b=HxCaC5UZCk1rRgB51xpd3oBKzAaZi67G4QYdh/LM6fE0aahU8Ff0eseS O2nhl7pOp+XmecixV3fzC6dhknvSkUCjIIyR4BSJoQiZinqTLnNJ1pOmU MVhUZn1qGAvxJyWi0OeHRbdqMkuo6n4/nvEOF0RBk/TgaCDjU4pEMAfR3 IFEUq2g7YavncVXH28K+aym2lwvcpPGnSC5cuKfW2APLTpV+cK3yWRqw6 79/GTzMcavG0ETWKXc29cR+Q7BjJ5cr0KWLNVNvZhRHM39g/YpvCISdfd 5/j7oMfDSoZhY1/452vGHbmvEt3nO9wDqSvGWtVuN4rS7/coOtKGAQAzB Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="399152663" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="399152663" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2023 22:51:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10783"; a="840585335" X-IronPort-AV: E=Sophos;i="6.01,234,1684825200"; d="scan'208";a="840585335" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmsmga002.fm.intel.com with ESMTP; 26 Jul 2023 22:51:24 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen Cc: Yi Liu , Jacob Pan , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 12/12] iommu: Add helper to set iopf handler for domain Date: Thu, 27 Jul 2023 13:48:37 +0800 Message-Id: <20230727054837.147050-13-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230727054837.147050-1-baolu.lu@linux.intel.com> References: <20230727054837.147050-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772553506698377445 X-GMAIL-MSGID: 1772553506698377445 To avoid open code everywhere. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 11 ++++++++++- drivers/iommu/iommu.c | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index d725500344a4..4e0b75befefc 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -132,6 +132,7 @@ struct iommu_page_response { typedef int (*iommu_fault_handler_t)(struct iommu_domain *, struct device *, unsigned long, int, void *); +typedef int (*iommu_iopf_handler_t)(struct iopf_group *group); struct iommu_domain_geometry { dma_addr_t aperture_start; /* First address that can be mapped */ @@ -181,7 +182,7 @@ struct iommu_domain { unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; - int (*iopf_handler)(struct iopf_group *group); + iommu_iopf_handler_t iopf_handler; void *fault_data; union { struct { @@ -581,6 +582,8 @@ extern ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova); extern void iommu_set_fault_handler(struct iommu_domain *domain, iommu_fault_handler_t handler, void *token); +void iommu_domain_set_iopf_handler(struct iommu_domain *domain, + iommu_iopf_handler_t handler, void *data); extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); @@ -904,6 +907,12 @@ static inline void iommu_set_fault_handler(struct iommu_domain *domain, { } +static inline void iommu_domain_set_iopf_handler(struct iommu_domain *domain, + iommu_iopf_handler_t handler, + void *data) +{ +} + static inline void iommu_get_resv_regions(struct device *dev, struct list_head *list) { diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 9d964d16d0ad..2254b0ef91fb 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1839,6 +1839,23 @@ void iommu_set_fault_handler(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); +/** + * iommu_domain_set_iopf_handler() - set io page fault handler for a domain + * @domain: iommu domain + * @handler: fault handler + * @data: user data, will be passed back to the fault handler + * + * This function should be used by iommu domain users which want to be notified + * whenever an IOMMU I/O page fault happens. + */ +void iommu_domain_set_iopf_handler(struct iommu_domain *domain, + iommu_iopf_handler_t handler, void *data) +{ + domain->iopf_handler = handler; + domain->fault_data = data; +} +EXPORT_SYMBOL_GPL(iommu_domain_set_iopf_handler); + static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, unsigned type) { @@ -3328,8 +3345,7 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, domain->type = IOMMU_DOMAIN_SVA; mmgrab(mm); domain->mm = mm; - domain->iopf_handler = iommu_sva_handle_iopf_group; - domain->fault_data = mm; + iommu_domain_set_iopf_handler(domain, iommu_sva_handle_iopf_group, mm); return domain; }