Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp576685pxb; Wed, 13 Jan 2021 10:30:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJy0j66EplbKaC+Ww8VLCO9JBT5NE1vNL6vcjFZRQVChUsu5RNs1GmIDp4Y2yw8t37uIE6P0 X-Received: by 2002:a17:907:20f1:: with SMTP id rh17mr2503083ejb.147.1610562640872; Wed, 13 Jan 2021 10:30:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610562640; cv=none; d=google.com; s=arc-20160816; b=KP81ymPSetI6mEHOFggdcCpy8wMjT7bHo+7p6zi/pDaXqULvVzC1ntPAV0rH50MSUP Ihap4jpVd67vPLcCQNHbU6XxSkVqIKNEP5qxmCg/c0ylvia0zj0lqiJ2W9TGTf8o0q58 zsiH8KPGBQqUnJdcepVBNy9AVaqBTnHlTaDoW1u/39CPYq0+YdQdOdHAegW/jXIyPtaB dqMgpWqkGRpOiVi2sj9K0/dT5zCBu6A8b/0oicvVZuk0XGZUdO6CPcAU+lLT35rQsLPs n7Q4xvnth3fMGfTgQ00JQV5uWsz7V4X0mXj73vEq3W0EjR3ETI+1FTsQbR6FxEdCLbDq RziA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=OkPVwnh+jEjQa+TUG0YxQu7dg/UEIeUv8rmEggcxZqU=; b=K7oVOMYMLrvt06+byR8rZhluOVCpu84BbVzyUPDOJOrSJmc79OrbfQUt2vcy9MttDP tND4LTV3A+Rqxv1bAkA2ru26ELW9hJPAPer4dzUCHjpkhq1Z3raTVsg+2w1DI9Y62f84 fc8GrIRT7a+osVzfb+2bT+JBHbnWdZhesj2gQE5fBakQhiJxpZGAdyDicXY7xT7+l+2Q qtD2r2vMSy83/NMhEjCOVc4W0OqSgvHDYimgI6CL9gqzitn4wRpiGQ1VUITQnzglr0cx SXJZ2Km0e5qL3iF4d74TpXuh1ml3QLaHjxbcvbiAAXSKlyl3u3ZNUtZQ+97IHB9dbDf0 hbxw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s18si1392091eji.645.2021.01.13.10.30.17; Wed, 13 Jan 2021 10:30:40 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728387AbhAMS2Z (ORCPT + 99 others); Wed, 13 Jan 2021 13:28:25 -0500 Received: from foss.arm.com ([217.140.110.172]:40292 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727984AbhAMS2Y (ORCPT ); Wed, 13 Jan 2021 13:28:24 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 938921FB; Wed, 13 Jan 2021 10:27:38 -0800 (PST) Received: from [10.57.56.43] (unknown [10.57.56.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 992EC3F66E; Wed, 13 Jan 2021 10:27:09 -0800 (PST) Subject: Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support. To: Christoph Hellwig , Claire Chang Cc: robh+dt@kernel.org, mpe@ellerman.id.au, benh@kernel.crashing.org, paulus@samba.org, joro@8bytes.org, will@kernel.org, frowand.list@gmail.com, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, m.szyprowski@samsung.com, grant.likely@arm.com, xypron.glpk@gmx.de, treding@nvidia.com, mingo@kernel.org, bauerman@linux.ibm.com, peterz@infradead.org, gregkh@linuxfoundation.org, saravanak@google.com, rafael.j.wysocki@intel.com, heikki.krogerus@linux.intel.com, andriy.shevchenko@linux.intel.com, rdunlap@infradead.org, dan.j.williams@intel.com, bgolaszewski@baylibre.com, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, iommu@lists.linux-foundation.org, xen-devel@lists.xenproject.org, tfiga@chromium.org, drinkcat@chromium.org References: <20210106034124.30560-1-tientzu@chromium.org> <20210106034124.30560-5-tientzu@chromium.org> <20210113124847.GC1383@lst.de> From: Robin Murphy Message-ID: <82bb75bc-11e6-ac94-9d24-7c896e3aae98@arm.com> Date: Wed, 13 Jan 2021 18:27:08 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20210113124847.GC1383@lst.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-01-13 12:48, Christoph Hellwig wrote: >> +#ifdef CONFIG_SWIOTLB >> + if (unlikely(dev->dma_io_tlb_mem)) >> + return swiotlb_alloc(dev, size, dma_handle, attrs); >> +#endif > > Another place where the dma_io_tlb_mem is useful to avoid the ifdef. > >> -phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, >> - size_t mapping_size, size_t alloc_size, >> - enum dma_data_direction dir, unsigned long attrs) >> +static int swiotlb_tbl_find_free_region(struct device *hwdev, >> + dma_addr_t tbl_dma_addr, >> + size_t alloc_size, >> + unsigned long attrs) > >> +static void swiotlb_tbl_release_region(struct device *hwdev, int index, >> + size_t size) > > This refactoring should be another prep patch. > > >> +void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, >> + unsigned long attrs) > > I'd rather have the names convey there are for the per-device bounce > buffer in some form. > >> + struct io_tlb_mem *mem = dev->dma_io_tlb_mem; > > While we're at it I wonder if the io_tlb is something we could change > while we're at it. Maybe replace io_tlb_mem with struct swiotlb > and rename the field in struct device to dev_swiotlb? > >> + int index; >> + void *vaddr; >> + phys_addr_t tlb_addr; >> + >> + size = PAGE_ALIGN(size); >> + index = swiotlb_tbl_find_free_region(dev, mem->start, size, attrs); >> + if (index < 0) >> + return NULL; >> + >> + tlb_addr = mem->start + (index << IO_TLB_SHIFT); >> + *dma_handle = phys_to_dma_unencrypted(dev, tlb_addr); >> + >> + if (!dev_is_dma_coherent(dev)) { >> + unsigned long pfn = PFN_DOWN(tlb_addr); >> + >> + /* remove any dirty cache lines on the kernel alias */ >> + arch_dma_prep_coherent(pfn_to_page(pfn), size); > > Can we hook in somewhat lower level in the dma-direct code so that all > the remapping in dma-direct can be reused instead of duplicated? That > also becomes important if we want to use non-remapping uncached support, > e.g. on mips or x86, or the direct changing of the attributes that Will > planned to look into for arm64. Indeed, AFAICS this ought to boil down to a direct equivalent of __dma_direct_alloc_pages() - other than the address there should be no conceptual difference between pages from the restricted pool and those from the regular page allocator, so this probably deserves to be plumbed in as an alternative to that. Robin.