From patchwork Tue Sep 12 18:19:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jernej_=C5=A0krabec?= X-Patchwork-Id: 13382026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79C70EE3F09 for ; Tue, 12 Sep 2023 18:20:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=91bxcGZIsHtam4c7DAvzB7D6OF4cCQRlz4qNpLvnJDE=; b=ISEzV7tkHQ8pPb JdChqNLJhPKg7KJtsuxM47VJloFpPHoADYLPbt2a/zlY7faQeZOdESpPld5wWjYFiGXitFTqXoS7v QduC3BcTqlzn9r+gfQWsc04oQysz0VDazvdEoOIcGmD6P+YBCVVRabaxs7aS/A9GArz7ZKzsy6fp2 5hPLcs37CqEV8Ahr4H4QAsElVN5a1tm9Em0vjCD+hZDSKWHVBz5KsFnJ2BAjyDiTsUxcXle/nUJDp thu+yUIn88E4m40QDH9TfZ87rZ7YPxoNn8ug6sM1KtaPSKORAtCe4N3sgCk7ruCAli4E6X+KnbTM8 47XJ5S+iOk5lAxrb4QAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg7zP-003yr8-1r; Tue, 12 Sep 2023 18:19:55 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg7zM-003yot-0w for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 18:19:53 +0000 Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-529fb04a234so7868949a12.3 for ; Tue, 12 Sep 2023 11:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694542788; x=1695147588; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=bbddqXGl2kQi0jX8ldjOAbABA41f9hA36rjyeoF2Ckc=; b=O07P7mqY30f26fU51gk5SjzvycEOBjV3Dls7B8KsSfFzTazlFjp45Hh3C+wGhOlBuZ VeGgYZ54UT/Aq3sNGfLZX90Kr6XE7HOhkJEk+o52nhN3QyVblT1fWCWP3mrG4gWcYf5n KkV0a2CYcYaHRAw8LUB1uJ7LqLz1yTKc3AsJv0ptHx0eJ6he0n8IgXo0Uw3xsYUyl6tp CjY7ekliNDAViZLS2GYQzm2ELOeBaLoElOLY4TQQ2m1eDt6eZ/sAUlk3RLsIGSRmpIV4 CykCty+7N17UJuSC+helj9II57YjktnS5ECu7LAPyFLOE/lA5fGH9jDoj5KgL1kXJoXn v/4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694542788; x=1695147588; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bbddqXGl2kQi0jX8ldjOAbABA41f9hA36rjyeoF2Ckc=; b=hogcOg+jlplGjwWDVtAqNkjVpBQ4cj1rlCmcpysgM+98+RBCTwKAXb84mxJbsiFWzU 7dyMCXEtD8mTCMmd8hzc6f3vINZZNZyDnhrP8M0wGMNjoOC8gmJDZlfSwSjnqnDXQeMd ewdA6ocmzyBcOXyZwTeWzTErdeKis6X0QYGA6fxPLR9xM4oMmj+YnZvgBDNVL1BM8Nt4 l74KUzdLlaDjp94mIJLERkVf/WTfyNgQ/QNzlwOHHRXTNhMtRzLkhGkpJRXZfYXVXVKb z9aLmsBJUvU7iUlXBdj9NFrf66LRphRewGIrXKu/ZPW4KSauqdRx2x4H9HXURMXGxc2u Uj+w== X-Gm-Message-State: AOJu0YyVZTSCVcqS2QHc1QLWLZrwBUwo6rcr1yrJe9dhx7QXQlo/JG3y mvvFQCa3ALam75gd9C2i4bY= X-Google-Smtp-Source: AGHT+IHe6uQndf+PwueAd+rgdFKpjlWerIStLg26ivu1DPHpgMAmREj657fDN1DRaa6taSHmufTMUQ== X-Received: by 2002:a17:907:7604:b0:9ad:9225:cee1 with SMTP id jx4-20020a170907760400b009ad9225cee1mr63479ejc.62.1694542787302; Tue, 12 Sep 2023 11:19:47 -0700 (PDT) Received: from localhost.localdomain (82-149-12-148.dynamic.telemach.net. [82.149.12.148]) by smtp.gmail.com with ESMTPSA id w5-20020a17090652c500b0099bd453357esm7093966ejn.41.2023.09.12.11.19.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 11:19:46 -0700 (PDT) From: Jernej Skrabec To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com 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, Jernej Skrabec Subject: [PATCH] iommu/sun50i: Convert to map_pages/unmap_pages Date: Tue, 12 Sep 2023 20:19:41 +0200 Message-ID: <20230912181941.2971036-1-jernej.skrabec@gmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230912_111952_330994_3E57A62A X-CRM114-Status: GOOD ( 19.31 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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? 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, } };