Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1112358pxb; Fri, 20 Nov 2020 01:10:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwYWtoDgPgLg7GQw2yVlb/OppbPOp+CRHea8yJbgPMnk9TvaacgovQHYlHf7eWvJamutvx X-Received: by 2002:a17:906:6d52:: with SMTP id a18mr31373773ejt.224.1605863404832; Fri, 20 Nov 2020 01:10:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605863404; cv=none; d=google.com; s=arc-20160816; b=SpTmDh5oc9TGA8SsPZUi0Vpo4kmRRl2Q9x3jzu1H37wDOYt+qNh8lBV7f1HI/hv0RH y2oJ7WFQt4KgRAVJKjUazEITu7koNgOrwO2LVK9tx3DZOmUNicCr9hkBM2eoSOZmaSkN UYsy4MRe+IVGuiC4TsuJFwbD4imQXPVZG2c1i9Fjw1hPys6eMczvcVprPcVGWct/YApz tGcoJFKAjNkJUNS4q7Fyz02Rl+4xmE2kUcE/JsrqfyuTP2HRWQYcuVaL6JIl+MusxhuA aHlWO1ohxgbruEyuXUEzeSVQ1kE6wv0xC+lHa+fjkbHNLK88hFVGZpD/IgizSjp6DJR0 TW2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=KWkZDyovtj8VVv7Zhl30LidWzpuHNDjbEQM4cjp8tp8=; b=ukSQ02fyKsXUtB8s/LjW4HS8chSjHS84J6nnIrA9y2NKZo4FmgOaHz1xv++/KvIuzu TcwEu7LcyYADdKAM/M8/m+sV5n3he0mdZzyHl/E4H2HHk/1kf/GGvsF82jL9Y0MQrLrH epFi4sK9na7iJ0PzsMMYsgsXch2iM5KBfjfM9SeszvETyKCOPm5NVx2+kXI06ul5h1hF AcB6JhXwUDF4fumX+DygES1B3c95WoEMsj44PnJ9cO/vhCpsObK4Fbowq1PLRv8MsK5+ hV0mbcCStayP4kmF+SdHQ+fjkjvWzusVP6lYUcMfFzifAnAJMKdReHToc7vbw2ML2SVe ob0A== 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=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i22si1312196ejr.184.2020.11.20.01.09.42; Fri, 20 Nov 2020 01:10:04 -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=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727294AbgKTJHS (ORCPT + 99 others); Fri, 20 Nov 2020 04:07:18 -0500 Received: from mailgw01.mediatek.com ([210.61.82.183]:35188 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726701AbgKTJHR (ORCPT ); Fri, 20 Nov 2020 04:07:17 -0500 X-UUID: d44ab65c4cb644f39c8bfc2208586de6-20201120 X-UUID: d44ab65c4cb644f39c8bfc2208586de6-20201120 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1967541814; Fri, 20 Nov 2020 17:07:12 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs07n1.mediatek.inc (172.21.101.16) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 20 Nov 2020 17:07:11 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 20 Nov 2020 17:07:10 +0800 From: Yong Wu To: Joerg Roedel , Will Deacon , Robin Murphy CC: Matthias Brugger , Krzysztof Kozlowski , Tomasz Figa , , , , , , , , Nicolas Boichat , , Subject: [PATCH] iommu: Improve the performance for direct_mapping Date: Fri, 20 Nov 2020 17:06:28 +0800 Message-ID: <20201120090628.6566-1-yong.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently direct_mapping always use the smallest pgsize which is SZ_4K normally to mapping. This is unnecessary. we could gather the size, and call iommu_map then, iommu_map could decide how to map better with the just right pgsize. From the original comment, we should take care overlap, otherwise, iommu_map may return -EEXIST. In this overlap case, we should map the previous region before overlap firstly. then map the left part. Each a iommu device will call this direct_mapping when its iommu initialize, This patch is effective to improve the boot/initialization time especially while it only needs level 1 mapping. Signed-off-by: Anan Sun Signed-off-by: Yong Wu --- drivers/iommu/iommu.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index df87c8e825f7..854a8fcb928d 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -737,6 +737,7 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group, /* We need to consider overlapping regions for different devices */ list_for_each_entry(entry, &mappings, list) { dma_addr_t start, end, addr; + size_t unmapped_sz = 0; if (domain->ops->apply_resv_region) domain->ops->apply_resv_region(dev, domain, entry); @@ -752,10 +753,25 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group, phys_addr_t phys_addr; phys_addr = iommu_iova_to_phys(domain, addr); - if (phys_addr) + if (phys_addr == 0) { + unmapped_sz += pg_size; /* Gather the size. */ continue; + } - ret = iommu_map(domain, addr, addr, pg_size, entry->prot); + if (unmapped_sz) { + /* Map the region before the overlap. */ + ret = iommu_map(domain, start, start, + unmapped_sz, entry->prot); + if (ret) + goto out; + start += unmapped_sz; + unmapped_sz = 0; + } + start += pg_size; + } + if (unmapped_sz) { + ret = iommu_map(domain, start, start, unmapped_sz, + entry->prot); if (ret) goto out; } -- 2.18.0