Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp3320776pxb; Sun, 29 Aug 2021 22:03:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz6n69Mj7oddATpT6QC9V/qgFe3he58QoVXBaRptDYZdw7g9GWEDVo5jnbrp5QBY70CTWOh X-Received: by 2002:aa7:dd48:: with SMTP id o8mr21935333edw.44.1630299834031; Sun, 29 Aug 2021 22:03:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630299834; cv=none; d=google.com; s=arc-20160816; b=dQnMGPx79XBJKvzNsEPFD7voHSeCy+c0LZPN7UZDHrPGKcPK3/WW4mHUC338viuaVO jTA9Z4GF9Lrql253ZnFB6ty8eKbzGxUQG8Pt2C0FOy1bmhjM7qmbUOj04Gwcruk/4ZP4 KP7mXPmwuhgw9Pk0aaNpzF+wRommAs9K67x+ckaq0xItT+vmC6tzhmwkpIW8kKN+4oJG BmwdjfYgQxf236Ep6jk/CSPxa9XgYIdur86vaG818896CPMj3g8jo1lKK+6eadtfsD9K kBBnhVJiPaIoAy5ydXQwISHoAp3VVVDYVFJhSyo06fZQP4Ni/XP3o1QIvtPdeyn+0MyN 0g3A== 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=/UcjlJnfTknwOZgerznLWkcH+3yPNZsmu2We65xRgN4=; b=k7qOPEXv0O/v4rEvulaeGZckS/AqySkd353W2ozwIjHqsscz6xJvCTuIIsCrWP3cpJ ZPD7GTMbO349rpIjWzsTSYHk/Jaw7rlVxzbKEWXnZ5RurN0WxXlAyg1P7WCEeIsI+X0J k0PkFZ4FurxkQZmEzAIcW8ZOrfNBaf1fiDprY5unO40tmKbzaVs/rS7p95SxIFaxlSOL oi5FedzEufZKsTWL6XAT9Cb58F6P/GYCzgv9gaYXqCzZIz02DudPW/tq4OTXEd8v5DRh ppnJ1HzrvIgrhsYhYkjw/K3D7v1clALaG9JrRwuGqklBfIs5Y8XJnjqLVlwlCeRpNXht Yjhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nJCQRxQG; 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 cs9si12345673ejc.233.2021.08.29.22.03.31; Sun, 29 Aug 2021 22:03:54 -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=nJCQRxQG; 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 S232700AbhH3FBo (ORCPT + 99 others); Mon, 30 Aug 2021 01:01:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232012AbhH3FBU (ORCPT ); Mon, 30 Aug 2021 01:01:20 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 737AAC061760 for ; Sun, 29 Aug 2021 22:00:27 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id s29so6396427pfw.5 for ; Sun, 29 Aug 2021 22:00:27 -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=/UcjlJnfTknwOZgerznLWkcH+3yPNZsmu2We65xRgN4=; b=nJCQRxQG0B2Oc65GqMuYHSJowcmVNngYqphTK/JV92PZjbh3Vokk5EojJTBpYGOj+1 YmgkJfX4q4RDXcPetejjRRobXhUmVOeuL84dCsWah4tcw7mpWiaMDyF8AYQhO0++H8+c 1RZ+59LTnfTG0TU2zxGDUkLu4Snk8RiPqgG60= 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=/UcjlJnfTknwOZgerznLWkcH+3yPNZsmu2We65xRgN4=; b=RyqTk7yvtpsJkirjZ0dhDCoCUDLN0HjZu7k+821K9XRj7RDSosdk+PDnwFNBLOot96 /zXJN3QmwxkUHAYk+NToHP2S3ROJPokDEZbK5umeRzqKNpKmHN1J0znNLz5PDY6LF04w 2nZD94goAfcZZhgXHcBn4wyZl4FBTjh+a5AYKdbn4XgVwaUOlav0v+vz0WB42DguYG3K UR1LQ6aym5zBB54u3vDv8b9G7XLzH99T1wbIKutZgy554UTc9aJQbHtL42bXtRoOoW89 ZlyvRzKiPbMNx8mAFuyh1f7YVbhTK30kCLHtPt5jFlREAaGRh+TM6cY0yzoPkJuErvn1 16+g== X-Gm-Message-State: AOAM532Vk5MJAGlcsTagZC+NbwmG8yTA2cSL/JsVlL03xdJl2UiDeq1e TfP3XGsLcyYzXhPwKVGJGIbwhw== X-Received: by 2002:a63:fe47:: with SMTP id x7mr17562605pgj.34.1630299626866; Sun, 29 Aug 2021 22:00:26 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:28a5:1b9f:af1d:5542]) by smtp.gmail.com with UTF8SMTPSA id x4sm12892490pjq.20.2021.08.29.22.00.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Aug 2021 22:00:26 -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 , Rajat Jain , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v7 4/7] dma-iommu: fold _swiotlb helpers into callers Date: Mon, 30 Aug 2021 13:59:22 +0900 Message-Id: <20210830045925.4163412-5-stevensd@google.com> X-Mailer: git-send-email 2.33.0.259.gc128427fd7-goog In-Reply-To: <20210830045925.4163412-1-stevensd@google.com> References: <20210830045925.4163412-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 Reviewed-by: Robin Murphy --- 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 abc528ed653c..95bfa57be488 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -510,26 +510,6 @@ static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr, iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather); } -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) { @@ -556,55 +536,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--) @@ -865,15 +796,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); } /* @@ -958,7 +942,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); } @@ -969,9 +953,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.259.gc128427fd7-goog