Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp817281pxj; Thu, 13 May 2021 18:32:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKJo4frdHXyfSpuiNXbJFXLcVFPDbcTXPdezbmHEZRzJmU0eM4wLcI/+7tzVxKoxk4edau X-Received: by 2002:a5e:a704:: with SMTP id b4mr33782935iod.35.1620955939072; Thu, 13 May 2021 18:32:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620955939; cv=none; d=google.com; s=arc-20160816; b=PRQ6sJdXTAHB+tbUFrnRF5NkowjLPbsrnNXiSFXdUsaZOpW4ysfny6A3fx5exGpM6u S6FHkd9RW2lBVO7ED3eE45k3eY3sBLJHurQwXRx13W20gNa0CpmEIDU/JzoEMAEJcMkw QUN+mrqJeKwN9DPirOI4HiUn3+LLedRPWM720NfTF5buKb7kKkT1/baf1XlYgwYQom3V Bl71PLwZblqHz1qcJzAKqEixJ1ix8hEIOUzdWhLXfd971AKgp01Tk6krSJdkld+OMGI7 TIFKutDE3K2C7klgywmKuOzulbICfHDayaqRbGJdEnFBkdATOKfvuCIIbmI0npAEmVKN j7Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:cc:to:from:dkim-signature; bh=IVvn5tn7hJz/M3nGGEc6QXjEojbGjBWgvoIwjwl6ECU=; b=vjVsXbVUjmjodxKIYNWeq0bptF8u9eBqFtFHZ4STbaRewBzZggayvbx4XTj8yJ4qP0 gGx7NLh1Ki494uJWoOuD2AqEN+cajk5L87OoNgUZxHZKXlVoyIIuy5OLvKiVSqVSDVDZ 3zajwNSl+cBvK3SAcY8ae0X6bmfediJEAy+ohJ4Uq0Kf3Tv86YqyNvow1xkj+d7ZiPcz VKgzWHYcaXBP54A7t7N6ah9VhBckRx736OfHoZZoHTM1xCKOji6UJ/ZzHMCWQ3a8O2rM I3mLDGK8UGWFIdxBnsT4Nh0+qgAVx5xuAeFUPipUqq63Dtw6QO3g1Kpu9YhY8lKS/24w TPjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=sH+LekVg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si5230392ioq.28.2021.05.13.18.32.06; Thu, 13 May 2021 18:32:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=sH+LekVg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233796AbhEMWeD (ORCPT + 99 others); Thu, 13 May 2021 18:34:03 -0400 Received: from ale.deltatee.com ([204.191.154.188]:59138 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233656AbhEMWdc (ORCPT ); Thu, 13 May 2021 18:33:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=deltatee.com; s=20200525; h=Subject:MIME-Version:References:In-Reply-To: Message-Id:Date:Cc:To:From:content-disposition; bh=IVvn5tn7hJz/M3nGGEc6QXjEojbGjBWgvoIwjwl6ECU=; b=sH+LekVgC3pEDVBU0MY59rGlk4 iog/TLuHrNUSQiugp4e5CPKUzOkW1JGBQZtpY3xnGc0jxKkfvfWcSh1yhSE30LQmRLHAjq5jjnuWm RPu9VlQ8tNRgYaZS1q2VL9ZZo1GPUzKy0aTdVRGmsiPOEaXEAArBeEp3VJ+gsVccwGtCb/1MIH9cY GQDFoExu8Qrc44xn5wMp3fakbTCB7zyQBpVQMi3jmlxmYScAKPMvT7MtTZmB7J87q4+aG6GD2di1k T3MAIH/bWExzTkgvKrTeJvnVg2Adk59FPNv2AKzgtuE0LVC0UgaIo6mwn14zFU6VC9e6ttfvFL479 ERVzYpuQ==; Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lhJsS-0000nD-Tc; Thu, 13 May 2021 16:32:22 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.92) (envelope-from ) id 1lhJsH-0001TB-Up; Thu, 13 May 2021 16:32:09 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Stephen Bates , Christoph Hellwig , Dan Williams , Jason Gunthorpe , =?UTF-8?q?Christian=20K=C3=B6nig?= , John Hubbard , Don Dutile , Matthew Wilcox , Daniel Vetter , Jakowski Andrzej , Minturn Dave B , Jason Ekstrand , Dave Hansen , Xiong Jianxin , Bjorn Helgaas , Ira Weiny , Robin Murphy , Logan Gunthorpe Date: Thu, 13 May 2021 16:31:56 -0600 Message-Id: <20210513223203.5542-16-logang@deltatee.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210513223203.5542-1-logang@deltatee.com> References: <20210513223203.5542-1-logang@deltatee.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, sbates@raithlin.com, hch@lst.de, jgg@ziepe.ca, christian.koenig@amd.com, jhubbard@nvidia.com, ddutile@redhat.com, willy@infradead.org, daniel.vetter@ffwll.ch, jason@jlekstrand.net, dave.hansen@linux.intel.com, helgaas@kernel.org, dan.j.williams@intel.com, andrzej.jakowski@intel.com, dave.b.minturn@intel.com, jianxin.xiong@intel.com, ira.weiny@intel.com, robin.murphy@arm.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-6.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, MYRULES_NO_TEXT autolearn=no autolearn_force=no version=3.4.2 Subject: [PATCH v2 15/22] dma-direct: Support PCI P2PDMA pages in dma-direct map_sg X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add PCI P2PDMA support for dma_direct_map_sg() so that it can map PCI P2PDMA pages directly without a hack in the callers. This allows for heterogeneous SGLs that contain both P2PDMA and regular pages. A P2PDMA page may have three possible outcomes when being mapped: 1) If the data path between the two devices doesn't go through the root port, then it should be mapped with a PCI bus address 2) If the data path goes through the host bridge, it should be mapped normally, as though it were a CPU physical address 3) It is not possible for the two devices to communicate and thus the mapping operation should fail (and it will return -EREMOTEIO). SGL segments that contain PCI bus addresses are marked with sg_dma_mark_pci_p2pdma() and are ignored when unmapped. Signed-off-by: Logan Gunthorpe --- kernel/dma/direct.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 803ee9321170..567dac942e89 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "direct.h" /* @@ -381,29 +382,60 @@ void dma_direct_sync_sg_for_cpu(struct device *dev, arch_sync_dma_for_cpu_all(); } +/* + * Unmaps segments, except for ones marked as pci_p2pdma which do not + * require any further action as they contain a bus address. + */ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) { struct scatterlist *sg; int i; - for_each_sg(sgl, sg, nents, i) - dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, - attrs); + for_each_sg(sgl, sg, nents, i) { + if (sg_is_dma_pci_p2pdma(sg)) { + sg_dma_unmark_pci_p2pdma(sg); + } else { + dma_direct_unmap_page(dev, sg->dma_address, + sg_dma_len(sg), dir, attrs); + } + } } #endif int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) { - int i; + struct pci_p2pdma_map_state p2pdma_state = {}; + enum pci_p2pdma_map_type map; struct scatterlist *sg; + int i, ret; for_each_sg(sgl, sg, nents, i) { + if (is_pci_p2pdma_page(sg_page(sg))) { + map = pci_p2pdma_map_segment(&p2pdma_state, dev, sg); + switch (map) { + case PCI_P2PDMA_MAP_BUS_ADDR: + continue; + case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: + /* + * Mapping through host bridge should be + * mapped normally, thus we do nothing + * and continue below. + */ + break; + default: + ret = -EREMOTEIO; + goto out_unmap; + } + } + sg->dma_address = dma_direct_map_page(dev, sg_page(sg), sg->offset, sg->length, dir, attrs); - if (sg->dma_address == DMA_MAPPING_ERROR) + if (sg->dma_address == DMA_MAPPING_ERROR) { + ret = -EINVAL; goto out_unmap; + } sg_dma_len(sg) = sg->length; } @@ -411,7 +443,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, out_unmap: dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); - return -EINVAL; + return ret; } dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, -- 2.20.1