Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3169328rdb; Wed, 13 Sep 2023 04:28:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG31ZlF+adDFzpw1D4vmDvBAevuYPJeDOUVM6/2xtVgymyYw3Ay2B5YQa3aTiPzG7OwjKbZ X-Received: by 2002:a17:90a:c8a:b0:25c:1ad3:a4a1 with SMTP id v10-20020a17090a0c8a00b0025c1ad3a4a1mr1777549pja.1.1694604527585; Wed, 13 Sep 2023 04:28:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694604527; cv=none; d=google.com; s=arc-20160816; b=nZZiIDbE+ZKOTdS39DUCVvg1j/IhqQOHWSckjC+/bE+iyMRj6Ofd8oka2XbIaUgNkN jIIBgWeWOoceC1yIClxytUzQtfTyrmVmOXpM0u3/pPSODxajpBJ/xBIQ9MMIgaQVetH/ poryvmudGsHtBVhfbv5bUknqLGdZOMHppTAu96GNf8gvI74GgQuAD11PfEE4kkrUWjMl ph/Ad7Lq/N1M4pk+ylp+xs/hCMfrq8YBNbiI5f4vi4s7baY1DD1ewAwH6BlGH8HM08Fm BWK5koBq9YFbqReZnqc3lS9kZ67B25NL+j7puhSAf9Ezr87mFKOH+w1FMYqbsA6/WNip 9cWQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=bbddqXGl2kQi0jX8ldjOAbABA41f9hA36rjyeoF2Ckc=; fh=t/2RkfiSAGLcRrVqyISOk7LLXlu2+/6U3QffRFjtv2o=; b=BmFOt9CMe+wrXD44h+Ac92LuCEaCnum1aJhkYV/THxQTzG7eq7kVhib1fLNGywBaa3 uxiGxWl8qw3Gfbv5+A9IfT+vSItcE+0+k8OhbjCc14HgauAH0DqhMNq3HoW69eBMSKa0 JG8u+pAZWZqnRXe7UDunatKRcARRTfVfjiNlCy57ajY9kZNdmrKvjFSPV6OUbKHTbgkC gF/ZObXEfjKO2aLsbXIR2q9JuHL72tKGzmKgMR6Ap7fnsocZ8jg0fznoK0Sb83F6WpN0 VXAVfHHPKPuyPw7YQkTjHxYTceXfvvCuZVDhCEOTk+xKG4KNCK5j82jX0S/kzYtkXYDm CCyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=siRw+UCU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id m3-20020a17090b068300b00262ca5c4c12si1291568pjz.178.2023.09.13.04.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 04:28:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=siRw+UCU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D0B718026DD4; Tue, 12 Sep 2023 11:19:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237211AbjILSTy (ORCPT + 99 others); Tue, 12 Sep 2023 14:19:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236903AbjILSTx (ORCPT ); Tue, 12 Sep 2023 14:19:53 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CAAF10D3 for ; Tue, 12 Sep 2023 11:19:49 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-52a40cf952dso7875350a12.2 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=vger.kernel.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=siRw+UCUfsFICG23C3+zif8zfTLgJMRilTIeNxTZ8xWI/4RfdzqPVYLvCBABLtBC6B 6NMi1ZdbF/aNMdmJRHW5eJE3c2ax9m/QssQFI2BLBHfK4B3Tcp7WF6FTbAHy3i+WBwSt sxCfb4I7idpQxZJsxK+qtUCnv6gEOCI908npF84XsqT6nYputw+ycRP/pEC++ege7JhH zcpxU8lXllBOdg3RxZxszngCFuxTXnTF3lbkLcQDdwdfLHOWXDaci/Jy9CvnGshCJTjR EvnlQml3fpBPQT1mLz9ClRYYoIomdwSsUAgQpp5Ntc/b+iofJP+gxzxXscck8+Kbb30P 14Rg== 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=Ft1DGAbR1BEzX7lsuprjFbYTbimR9oe0BHDHtwCHq2pIih401OK/wV+j3k6Kd96RS+ gVBNTvsULt2cAveGAvtPALvz4ZXnm93mkOs5LXAQ3cch4L/y0tmAwlB3HbVKQdqlIR95 wOSKVN9BuY6cecQSB4kjulcJYx07pcCArTae5zZzktfRaozae5L5qvQPnQF1Z271gjJH TIR7md2tFAmYJGIMR3GDfp6ThXA8pkp88vzP+jcv9smU5kL6CtNci54FqXd/ZkEl422O 1ZFhEzGZNHOKjJWmG3tg5SdCOxEjh3jeErg4tKEFGMKs1y8zCzm1/zp1dY2CHmXiuTv/ LByQ== X-Gm-Message-State: AOJu0YwKz9ooPfM1lZrm0e0F0L81ZR8aTrwVTcJlLT9NX7XSgjnSNfbd HE+0vzoOQIjBTQmuhY75doI= 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 Content-Transfer-Encoding: 8bit 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 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 11:19:53 -0700 (PDT) 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, } }; -- 2.42.0