Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp799519pxb; Mon, 16 Aug 2021 18:42:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJws3CZ7edknxDiVHbXU9yuF6BouLADq12Y/mJ70KohWfjT/9Hrq6XaeQEvkIpbYhqJ3jv5R X-Received: by 2002:a92:de46:: with SMTP id e6mr643359ilr.273.1629164520482; Mon, 16 Aug 2021 18:42:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629164520; cv=none; d=google.com; s=arc-20160816; b=pPNJUa+Ydrgi7C2SCWckBnrjpWGXMJU9g1IFeZcRhDRhRd5qRaH+WmjTZ0kbdDBya0 AvAONh3d3nOzqRAxYm+SfaafAVvo0JEancnwtLK7ML1VqTcSIcoWjiBBayyyRfoxAd2/ /zvlfkkNzc10E6o+xgLsb8eZpAvQzRZIFdTnPgp6nOH1iapNtfGebssHbhD/a2G02Nnx rzxlWWZeU3KpoFQ/qtmn1WaW+gU+gpP9bbdpMjwqV++vebYt+AyGp5LDvcQ52Nw/WC6/ alRe149hoe3mzGMc41m2WPd0xkJPwadH0I7i+E4oHVxw/YZa/ui3G45F4jZNwgviW/zu J0Ig== 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=XmLa4Egl3RbFTsKEXycierEXGblZ1yb/jiV30ninE4M=; b=hbPsduV6W95LrXaJNdAmiaQW3GYx8fMViW7RkakvXOx1pQwHKQ91JJJS97tEX8WoHv HGhvmK0IPTNv+Erb5WfO/Tx1xS1uQ2Ys5SUTRi5b5a0zfkc/cwvYOz2q+kypd2UnX1zr 6wlulBqn91XN7FswzmpCjsX2aD0SNUFcAu65UD5nOifVdhnTESo9YanukCvFSCPWrV/K QcCEPU9cahT3Up9rZrLfBYMbI9naWo3On/hC5ku3GcwKlseLDHiTRd+n2ZlyN1f2Ii7+ EHseTORIMyq2Llye1P507XUHWEDOyz27B6i8zK7yr/iJ/ukLAYxHVKw5hfm0cEYIyn+d 469w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lTDp8WDV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s2si590551ilv.0.2021.08.16.18.41.49; Mon, 16 Aug 2021 18:42:00 -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=@chromium.org header.s=google header.b=lTDp8WDV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236561AbhHQBkI (ORCPT + 99 others); Mon, 16 Aug 2021 21:40:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236345AbhHQBkC (ORCPT ); Mon, 16 Aug 2021 21:40:02 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FDB4C06179A for ; Mon, 16 Aug 2021 18:39:30 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id n13-20020a17090a4e0d00b0017946980d8dso2102241pjh.5 for ; Mon, 16 Aug 2021 18:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XmLa4Egl3RbFTsKEXycierEXGblZ1yb/jiV30ninE4M=; b=lTDp8WDVyXsFHl09LsAoHKc2Z1SB9SzRnrxlTirBJb6Ym4k+5OfHh9EXZhMBxBI6sT pHsBVeywg3B28Gz7N4T52B2eg2SN4DaXyKRw+u26EiTlCUa9uNthg7CV4zqKirnYRy1m xeVl0I3lFTQA+vNQ+9OQnf0rzNJnaE35zT7Zs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XmLa4Egl3RbFTsKEXycierEXGblZ1yb/jiV30ninE4M=; b=fLSdji9BdiKd4ArAveeYy2lekh/3us05fJOIgXdQbFwlvalKnTQnp2Sfq5wBVd6vji GgR0AcaJs9TqRD2MwgiEMmp2c6scmeD6cDAv3er5WhQZjTnPsO7DjdlvmuMu6bI6ydeX gVyML2fS5LxjFi1KUdWrQwzlSmKNTrgVCx8a5qpK4PdoRBBUoLhBKH/5EQunzPikQsGV ORPd7Hf3GK109vPDpQnL+t6uVL6aHw41Gyj5vW6Y9vGjUAC1iNEttEYrG+OHlqZW2MFg QcSPJT9Hu4rwIr4DCLACiCMiJUFwNLu2tpvvYTSAvNSwFulCW/tYxQ4ihYwKnDf5rce6 2K7w== X-Gm-Message-State: AOAM5306r/Yr+A2r6oWNUX5TkVqaWwsSPyr1t4i/RaentF/d0hfFLZx1 FSzKaF+84fvIT+wB8+k+NjcmMw== X-Received: by 2002:a63:4245:: with SMTP id p66mr979599pga.402.1629164369872; Mon, 16 Aug 2021 18:39:29 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:6864:767c:2ff8:749d]) by smtp.gmail.com with UTF8SMTPSA id y23sm468518pgf.38.2021.08.16.18.39.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Aug 2021 18:39:29 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: Robin Murphy , Christoph Hellwig Cc: Joerg Roedel , Will Deacon , Lu Baolu , Tom Murphy , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v6 4/7] dma-iommu: fold _swiotlb helpers into callers Date: Tue, 17 Aug 2021 10:38:49 +0900 Message-Id: <20210817013852.3222824-5-stevensd@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210817013852.3222824-1-stevensd@google.com> References: <20210817013852.3222824-1-stevensd@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Stevens Fold the _swiotlb helper functions into the respective _page functions, since recent fixes have moved all logic from the _page functions to the _swiotlb functions. Signed-off-by: David Stevens Reviewed-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 135 +++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 76 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 5dd2c517dbf5..8152efada8b2 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -493,26 +493,6 @@ static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr, iommu_dma_free_iova(cookie, dma_addr, size, iotlb_gather.freelist); } -static void __iommu_dma_unmap_swiotlb(struct device *dev, dma_addr_t dma_addr, - size_t size, enum dma_data_direction dir, - unsigned long attrs) -{ - struct iommu_domain *domain = iommu_get_dma_domain(dev); - phys_addr_t phys; - - phys = iommu_iova_to_phys(domain, dma_addr); - if (WARN_ON(!phys)) - return; - - if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev)) - arch_sync_dma_for_cpu(phys, size, dir); - - __iommu_dma_unmap(dev, dma_addr, size); - - if (unlikely(is_swiotlb_buffer(phys))) - swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); -} - static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, size_t size, int prot, u64 dma_mask) { @@ -539,55 +519,6 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, return iova + iova_off; } -static dma_addr_t __iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys, - size_t org_size, dma_addr_t dma_mask, bool coherent, - enum dma_data_direction dir, unsigned long attrs) -{ - int prot = dma_info_to_prot(dir, coherent, attrs); - struct iommu_domain *domain = iommu_get_dma_domain(dev); - struct iommu_dma_cookie *cookie = domain->iova_cookie; - struct iova_domain *iovad = &cookie->iovad; - size_t aligned_size = org_size; - void *padding_start; - size_t padding_size; - dma_addr_t iova; - - /* - * If both the physical buffer start address and size are - * page aligned, we don't need to use a bounce page. - */ - if (IS_ENABLED(CONFIG_SWIOTLB) && dev_is_untrusted(dev) && - iova_offset(iovad, phys | org_size)) { - aligned_size = iova_align(iovad, org_size); - phys = swiotlb_tbl_map_single(dev, phys, org_size, - aligned_size, dir, attrs); - - if (phys == DMA_MAPPING_ERROR) - return DMA_MAPPING_ERROR; - - /* Cleanup the padding area. */ - padding_start = phys_to_virt(phys); - padding_size = aligned_size; - - if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && - (dir == DMA_TO_DEVICE || - dir == DMA_BIDIRECTIONAL)) { - padding_start += org_size; - padding_size -= org_size; - } - - memset(padding_start, 0, padding_size); - } - - if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) - arch_sync_dma_for_device(phys, org_size, dir); - - iova = __iommu_dma_map(dev, phys, aligned_size, prot, dma_mask); - if (iova == DMA_MAPPING_ERROR && is_swiotlb_buffer(phys)) - swiotlb_tbl_unmap_single(dev, phys, org_size, dir, attrs); - return iova; -} - static void __iommu_dma_free_pages(struct page **pages, int count) { while (count--) @@ -848,15 +779,68 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, { phys_addr_t phys = page_to_phys(page) + offset; bool coherent = dev_is_dma_coherent(dev); + int prot = dma_info_to_prot(dir, coherent, attrs); + struct iommu_domain *domain = iommu_get_dma_domain(dev); + struct iommu_dma_cookie *cookie = domain->iova_cookie; + struct iova_domain *iovad = &cookie->iovad; + size_t aligned_size = size; + dma_addr_t iova, dma_mask = dma_get_mask(dev); + + /* + * If both the physical buffer start address and size are + * page aligned, we don't need to use a bounce page. + */ + if (IS_ENABLED(CONFIG_SWIOTLB) && dev_is_untrusted(dev) && + iova_offset(iovad, phys | size)) { + void *padding_start; + size_t padding_size; + + aligned_size = iova_align(iovad, size); + phys = swiotlb_tbl_map_single(dev, phys, size, + aligned_size, dir, attrs); + + if (phys == DMA_MAPPING_ERROR) + return DMA_MAPPING_ERROR; - return __iommu_dma_map_swiotlb(dev, phys, size, dma_get_mask(dev), - coherent, dir, attrs); + /* Cleanup the padding area. */ + padding_start = phys_to_virt(phys); + padding_size = aligned_size; + + if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && + (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) { + padding_start += size; + padding_size -= size; + } + + memset(padding_start, 0, padding_size); + } + + if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) + arch_sync_dma_for_device(phys, size, dir); + + iova = __iommu_dma_map(dev, phys, aligned_size, prot, dma_mask); + if (iova == DMA_MAPPING_ERROR && is_swiotlb_buffer(phys)) + swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); + return iova; } static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction dir, unsigned long attrs) { - __iommu_dma_unmap_swiotlb(dev, dma_handle, size, dir, attrs); + struct iommu_domain *domain = iommu_get_dma_domain(dev); + phys_addr_t phys; + + phys = iommu_iova_to_phys(domain, dma_handle); + if (WARN_ON(!phys)) + return; + + if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev)) + arch_sync_dma_for_cpu(phys, size, dir); + + __iommu_dma_unmap(dev, dma_handle, size); + + if (unlikely(is_swiotlb_buffer(phys))) + swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); } /* @@ -941,7 +925,7 @@ static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *s int i; for_each_sg(sg, s, nents, i) - __iommu_dma_unmap_swiotlb(dev, sg_dma_address(s), + iommu_dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); } @@ -952,9 +936,8 @@ static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg, int i; for_each_sg(sg, s, nents, i) { - sg_dma_address(s) = __iommu_dma_map_swiotlb(dev, sg_phys(s), - s->length, dma_get_mask(dev), - dev_is_dma_coherent(dev), dir, attrs); + sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s), + s->offset, s->length, dir, attrs); if (sg_dma_address(s) == DMA_MAPPING_ERROR) goto out_unmap; sg_dma_len(s) = s->length; -- 2.33.0.rc1.237.g0d66db33f3-goog