Received: by 2002:a05:6a10:c7d3:0:0:0:0 with SMTP id h19csp1394231pxy; Sun, 15 Aug 2021 20:01:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJN96pmrAi7lo5M9mhG+vkMt845ly9yjxfJ3FhHNuJpIDpR2lDE+lCDYGupJkaH7NJBVUc X-Received: by 2002:a02:334d:: with SMTP id k13mr13441261jak.11.1629082913227; Sun, 15 Aug 2021 20:01:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629082913; cv=none; d=google.com; s=arc-20160816; b=Xluz4nnr58xPHO01HoGxh4piC9l6xwm0ju8WRjG6RzLvC52fP8M/glIC6nHHuumqNi B1e+50nrdkNkRJuvex+GuNE1srVQ8lXHoACxI7oNUa55L6a/d0DemTpw3Ix/6cOonFqS J4KCX2VsXoVIT5Gw3vsPsLthexJ2Q3U2JKvqVMXIRmgyCc7SZRYSwO74XKPW8djhrAlk 86sxgdCKC8XaPJHX+D/zjdSsFL/ot9GORm5a+B79D/jbI8xrh6hJ7vs3/o0U876Jls2f /oKlTLQgdhVNpIqpJ3wxRd8WPmisWNBEURYlJRC+SJwyEDoUsBNwgbEYrEFdgzOkqhOE q5gQ== 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=5As/V4f+0am36FOl0SodjRRhXjFKVt0DU7SUbqZvWtw=; b=T3hAT6ShcVTM+MjSR93dU6xb3+jwwhmfyj91RKZiNS8H/Q7/tfuughrn/U5zkjh/S0 Ird/6cVXPwGkvUjN5F6mjnfeMvfIC0co5PsEmYkAVnfudoYvGJm5EdyKChQEilDYOW82 VtIimRkgoZA9O9YxU335dXFkbzMfxNDXG/vnQA28eAiQ/A7r0ahqyl2ELfqzaNvbcLdr Ck9K+3evPGQUgUvFEl68L6jpZT0FsD3asKTvjwUhTMR4iySAT7LCus37n1BHMvbxlh8R 7WjccCFy9nYdUkQw1kFN439UyknU1JpHHCeWMdBl52OzKIx8PKYQV4Wii5VP3Aqd6UcA 3iNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IaZIFfxD; 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 n11si11151828ilt.25.2021.08.15.20.01.42; Sun, 15 Aug 2021 20:01:53 -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=IaZIFfxD; 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 S232667AbhHPDAI (ORCPT + 99 others); Sun, 15 Aug 2021 23:00:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231789AbhHPDAI (ORCPT ); Sun, 15 Aug 2021 23:00:08 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A717BC061764 for ; Sun, 15 Aug 2021 19:59:37 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d17so19157971plr.12 for ; Sun, 15 Aug 2021 19:59:37 -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=5As/V4f+0am36FOl0SodjRRhXjFKVt0DU7SUbqZvWtw=; b=IaZIFfxDzmXMKgfAzeBEh/e9caVHk1x+4fnIviZP/wjeof5ZoT6hS221lgrRdMUbPU yv67EwgzDsVdjQNbt9mOKFpC/zvGXnPyQRDHPIt4J7QcGiLdCgj46gO/Y2ELJRJiyHrf adUij3HkMMWus94bEeRwXvfw6XuRGHwo6+DYU= 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=5As/V4f+0am36FOl0SodjRRhXjFKVt0DU7SUbqZvWtw=; b=PpqH3nJo0LS88jFGQn94QrDcIcnDzIXmAnsxybJxbpQCxFXQeOTzfGJT9ipao3vAPg S9I0kAJzY1XogOn1CfrGhgDxluuOTlaraV6ypVHVBE/C2VBxySbY/g9VikvuyKKrB1gj Ha3dF45eKFlUDjzlUQrEL793MRAUD+0IBfLo9QmIZUgEhEg8LbWt86EG6WHnrOmQs0fh hbIKxdSBSM0r32Y2GRvx+1Aixsn3YZ//3zNFYPbYtNbVdmNGbXBw9ymKooEJvZ1ugTz+ FfPlJ3sRrU048OGkEDB5uvns0GyB+Hnb9KB9x9uNl6x0TZUWBS6Zs+9J+FZbxjQ9rgl+ IdAw== X-Gm-Message-State: AOAM533iphLy/50u4Lb2cE/CThgdPw+Irm/SHHGL+moArgD42S+0Ki0B SjLLXR2ZIw9oZrAjvV4+qOYDNg== X-Received: by 2002:a05:6a00:1796:b029:3c4:25d6:9ee1 with SMTP id s22-20020a056a001796b02903c425d69ee1mr14141512pfg.21.1629082777241; Sun, 15 Aug 2021 19:59:37 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:17b8:f07a:2a52:317a]) by smtp.gmail.com with UTF8SMTPSA id y23sm9658650pfb.130.2021.08.15.19.59.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Aug 2021 19:59:37 -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 v5 4/7] dma-iommu: fold _swiotlb helpers into callers Date: Mon, 16 Aug 2021 11:57:52 +0900 Message-Id: <20210816025755.2906695-5-stevensd@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210816025755.2906695-1-stevensd@google.com> References: <20210816025755.2906695-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 --- drivers/iommu/dma-iommu.c | 136 +++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 76 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 5dd2c517dbf5..f7da4934f7e6 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,69 @@ 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; - return __iommu_dma_map_swiotlb(dev, phys, size, dma_get_mask(dev), - coherent, dir, attrs); + 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; + + /* 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 +926,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 +937,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