Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3479000rdb; Wed, 13 Sep 2023 13:21:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF33lfVGk/uaVLNqrzskOCUqQapTxj5X7765Fm5injX2cOOJ5xdH5fBkOoNHYU/Vjwi2Bm9 X-Received: by 2002:a17:90a:4489:b0:269:32d2:5ac4 with SMTP id t9-20020a17090a448900b0026932d25ac4mr3382548pjg.25.1694636460799; Wed, 13 Sep 2023 13:21:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694636460; cv=none; d=google.com; s=arc-20160816; b=JYItbGnLdbPW9J6SGCriCKJT+IZvQMPSuqUJQsU466JDBIuTpbEAj2MLmNOl8fP2gD 65L8yruoWOhL21j8YrJyBqLecbkD0HTdvpAk96/IkkqS9TN+GuwbUYH5YZa5gJXSbYI1 cdOai4ucVwJofkhoyAFa3HCKFfXYPoA4oJk62G0/j05DbyzwatZX0d+S1oPY3hSSY00V gNVs2AganD3DvcMGWTE3dTlOwY0S2ptJK+s/455Ack6yqi5YMwApPqMGZ2NxWeehuFn9 iKTHDvyAy4AwqdUJ4ljTXmVZoZafEqEntA4x9gUoQjrZwb3cctFytAk4gTZ1YKa+JN11 G+xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=jgEPnLGLJcXoGfyM35uUO4lOr8c7NM9/OB5yNPNl/ek=; fh=PeYSvzdLo5n6Z8/ydJZJsEGcugVuPxjIfJmpTLESSeE=; b=zMVe4OQYs9MNnsI3gjyDcwDokDwONN90WGz7pdDK1gUcSDP5EcqJPOIu89Qjm49E1J McDt+P/lH12Q2Ol93yaXzX3eJ/mU6OpzAxN6sQGHApwwwlcmPV7eFw73MzeRBXvc6ujS +4CwgO5F3FbWpWi3WsfR59qjQxvPzR3583z7gd+r5QZXdbvMkAQPhxsEUdhkmcOx5rHO E4Y4A6mXAu9GxG1GxCZuq0beXziHsmdym/Cxtyx2u4QTiMiBvsHv4YOYqRKM7Tv3On56 GvVVuAa/z/InyQt2GGHnG+1gi3u/W90YtEgIfWh7PMvZjiXnMIWGrKWH/rvdZiyq4j2d 7gTQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id lx2-20020a17090b4b0200b002681fea6d14si2457036pjb.79.2023.09.13.13.21.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 13:21:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 734E082878DC; Wed, 13 Sep 2023 13:20:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232173AbjIMUU3 (ORCPT + 99 others); Wed, 13 Sep 2023 16:20:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229527AbjIMUU2 (ORCPT ); Wed, 13 Sep 2023 16:20:28 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 012641BC6 for ; Wed, 13 Sep 2023 13:20:23 -0700 (PDT) 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 9776D1FB; Wed, 13 Sep 2023 13:21:00 -0700 (PDT) Received: from [10.57.93.239] (unknown [10.57.93.239]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E20A63F738; Wed, 13 Sep 2023 13:20:21 -0700 (PDT) Message-ID: <157d5041-96ac-6bad-9137-19a78fbf3591@arm.com> Date: Wed, 13 Sep 2023 21:20:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH] iommu/sun50i: Convert to map_pages/unmap_pages Content-Language: en-GB To: Jernej Skrabec , joro@8bytes.org, will@kernel.org Cc: wens@csie.org, samuel@sholland.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org References: <20230912181941.2971036-1-jernej.skrabec@gmail.com> From: Robin Murphy In-Reply-To: <20230912181941.2971036-1-jernej.skrabec@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 13 Sep 2023 13:20:29 -0700 (PDT) X-Spam-Status: No, score=-2.2 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email On 2023-09-12 19:19, Jernej Skrabec wrote: > Convert driver to use map_pages and unmap_pages. Since functions operate > on page table, extend them to be able to operate on whole table, not > just one entry. > > Signed-off-by: Jernej Skrabec > --- > Hi! > > I'm not sure if it makes sense to check validity of page table entry when > unmaping pages makes sense. What do you think? As things currently stand it's largely a matter of opinion - some drivers consider that unmapping something which hasn't been mapped is nonsensical, and almost always represents the caller having gone wrong, thus report it as an error; others take the view that as long as they can achieve the requested end result, they're not going to think too hard about how they got there. The same arguments apply to whether you'd quietly replace an existing PTE or not when mapping, so I'd say the main thing is to at least be self-consistent one way or the other. Cheers, Robin. > > Best regards, > Jernej > > drivers/iommu/sun50i-iommu.c | 55 +++++++++++++++++++++--------------- > 1 file changed, 33 insertions(+), 22 deletions(-) > > diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c > index 74c5cb93e900..be6102730a56 100644 > --- a/drivers/iommu/sun50i-iommu.c > +++ b/drivers/iommu/sun50i-iommu.c > @@ -589,11 +589,12 @@ static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain, > } > > static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, > - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) > + phys_addr_t paddr, size_t pgsize, size_t pgcount, > + int prot, gfp_t gfp, size_t *mapped) > { > struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); > struct sun50i_iommu *iommu = sun50i_domain->iommu; > - u32 pte_index; > + u32 pte_index, count, i; > u32 *page_table, *pte_addr; > int ret = 0; > > @@ -604,45 +605,55 @@ static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, > } > > pte_index = sun50i_iova_get_pte_index(iova); > - pte_addr = &page_table[pte_index]; > - if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) { > - phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr); > - dev_err(iommu->dev, > - "iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n", > - &iova, &page_phys, &paddr, prot); > - ret = -EBUSY; > - goto out; > + count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index); > + for (i = 0; i < count; i++) { > + pte_addr = &page_table[pte_index + i]; > + if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) { > + phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr); > + > + dev_err(iommu->dev, > + "iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n", > + &iova, &page_phys, &paddr, prot); > + ret = -EBUSY; > + goto out; > + } > + *pte_addr = sun50i_mk_pte(paddr, prot); > + paddr += SPAGE_SIZE; > } > > - *pte_addr = sun50i_mk_pte(paddr, prot); > - sun50i_table_flush(sun50i_domain, pte_addr, 1); > + sun50i_table_flush(sun50i_domain, &page_table[pte_index], i); > + *mapped = i * SPAGE_SIZE; > > out: > return ret; > } > > static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova, > - size_t size, struct iommu_iotlb_gather *gather) > + size_t pgsize, size_t pgcount, > + struct iommu_iotlb_gather *gather) > { > struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain); > + u32 dte, count, i, pte_index; > phys_addr_t pt_phys; > u32 *pte_addr; > - u32 dte; > > dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)]; > if (!sun50i_dte_is_pt_valid(dte)) > return 0; > > pt_phys = sun50i_dte_get_pt_address(dte); > - pte_addr = (u32 *)phys_to_virt(pt_phys) + sun50i_iova_get_pte_index(iova); > + pte_index = sun50i_iova_get_pte_index(iova); > + pte_addr = (u32 *)phys_to_virt(pt_phys) + pte_index; > > - if (!sun50i_pte_is_page_valid(*pte_addr)) > - return 0; > + count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index); > + for (i = 0; i < count; i++) > + if (!sun50i_pte_is_page_valid(pte_addr[i])) > + break; > > - memset(pte_addr, 0, sizeof(*pte_addr)); > - sun50i_table_flush(sun50i_domain, pte_addr, 1); > + memset(pte_addr, 0, sizeof(*pte_addr) * i); > + sun50i_table_flush(sun50i_domain, pte_addr, i); > > - return SZ_4K; > + return i * SPAGE_SIZE; > } > > static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain, > @@ -838,8 +849,8 @@ static const struct iommu_ops sun50i_iommu_ops = { > .iotlb_sync_map = sun50i_iommu_iotlb_sync_map, > .iotlb_sync = sun50i_iommu_iotlb_sync, > .iova_to_phys = sun50i_iommu_iova_to_phys, > - .map = sun50i_iommu_map, > - .unmap = sun50i_iommu_unmap, > + .map_pages = sun50i_iommu_map, > + .unmap_pages = sun50i_iommu_unmap, > .free = sun50i_iommu_domain_free, > } > };