From nobody Thu Nov 14 06:58:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B573FC636D4 for ; Tue, 14 Feb 2023 03:12:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230451AbjBNDMi (ORCPT ); Mon, 13 Feb 2023 22:12:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230259AbjBNDMf (ORCPT ); Mon, 13 Feb 2023 22:12:35 -0500 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFE0219688; Mon, 13 Feb 2023 19:12:22 -0800 (PST) X-UUID: 61dff98cac1511ed945fc101203acc17-20230214 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=zWET5nPAG15bJ/2TfLTPXB14Wk3COEmdilceNirVKAE=; b=nm+sOKFO+NA9GuHrf4gseyD2R5Set4vKLsotTGXGI7RJ1HhauhM0WIXTaQmZq7Pv8cNCWEbKOlCOQc2THgh2sSbf0zPFtxTJ5+1arhmrOULBtcNOcSrC6ESiqjy/xILL3fu7Aa0DIhFv0CSSlnvzdLJzrJdG6MpUBz6Zqxpe8LY=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.19,REQID:bbb6fba2-38b9-4fdb-b5ff-9b88f8d52b8c,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:885ddb2,CLOUDID:07ca5bf8-ff42-4fb0-b929-626456a83c14,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-UUID: 61dff98cac1511ed945fc101203acc17-20230214 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 822963398; Tue, 14 Feb 2023 11:12:15 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.792.3; Tue, 14 Feb 2023 11:12:14 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.792.15 via Frontend Transport; Tue, 14 Feb 2023 11:12:13 +0800 From: Yong Wu To: Joerg Roedel , Matthias Brugger , Mauro Carvalho Chehab , Rob Herring CC: Will Deacon , Robin Murphy , Krzysztof Kozlowski , Hans Verkuil , , , , , , , , "AngeloGioacchino Del Regno" , , , , Yunfei Dong , kyrie wu , , , , Yong Wu Subject: [PATCH v3 04/11] iommu/mediatek: Get regionid from larb/port id Date: Tue, 14 Feb 2023 11:11:07 +0800 Message-ID: <20230214031114.926-5-yong.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230214031114.926-1-yong.wu@mediatek.com> References: <20230214031114.926-1-yong.wu@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After commit f1ad5338a4d5 ("of: Fix "dma-ranges" handling for bus controllers"), the dma-ranges is not allowed for dts leaf node. but we still would like to separate to different masters into different iova regions. Thus we have to separate it by the HW larbid and portid. For example, larb1/2 are in region2 and larb3 is in region3. The problem is that some ports inside a larb are in region4 while some ports inside this larb are in region5. Therefore I define a "iova_region_larb_msk" to help record the information for each a port. Take a example for a larb: [1] =3D ~0: means all ports in this larb are in region1; [2] =3D BIT(3) | BIT(4): means port3/4 in this larb are region2; [3] =3D ~(BIT(3) | BIT(4)): means all the other ports except port3/4 in this larb are region3. This method also avoids the users forget/abuse the iova regions. Signed-off-by: Yong Wu Reviewed-by: AngeloGioacchino Del Regno --- drivers/iommu/mtk_iommu.c | 47 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index ab53edcb221f..7e2cb3b8cac8 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -212,6 +211,11 @@ struct mtk_iommu_plat_data { struct { unsigned int iova_region_nr; const struct mtk_iommu_iova_region *iova_region; + /* + * Indicate the correspondence between larbs/ports and regions. + * The index is same with iova_region. + */ + const u32 (*iova_region_larb_msk)[MTK_LARB_NR_MAX]; }; =20 /* @@ -529,30 +533,33 @@ static unsigned int mtk_iommu_get_bank_id(struct devi= ce *dev, static int mtk_iommu_get_iova_region_id(struct device *dev, const struct mtk_iommu_plat_data *plat_data) { - const struct mtk_iommu_iova_region *rgn =3D plat_data->iova_region; - const struct bus_dma_region *dma_rgn =3D dev->dma_range_map; - int i, candidate =3D -1; - dma_addr_t dma_end; + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); + unsigned int portidmsk =3D 0, larbid; + const u32 *rgn_larb_msk; + int i, region_id =3D -1; =20 - if (!dma_rgn || plat_data->iova_region_nr =3D=3D 1) + if (plat_data->iova_region_nr =3D=3D 1) return 0; =20 - dma_end =3D dma_rgn->dma_start + dma_rgn->size - 1; - for (i =3D 0; i < plat_data->iova_region_nr; i++, rgn++) { - /* Best fit. */ - if (dma_rgn->dma_start =3D=3D rgn->iova_base && - dma_end =3D=3D rgn->iova_base + rgn->size - 1) - return i; - /* ok if it is inside this region. */ - if (dma_rgn->dma_start >=3D rgn->iova_base && - dma_end < rgn->iova_base + rgn->size) - candidate =3D i; + larbid =3D MTK_M4U_TO_LARB(fwspec->ids[0]); + for (i =3D 0; i < fwspec->num_ids; i++) + portidmsk |=3D BIT(MTK_M4U_TO_PORT(fwspec->ids[i])); + + for (i =3D 0; i < plat_data->iova_region_nr; i++) { + rgn_larb_msk =3D plat_data->iova_region_larb_msk[i]; + if (!rgn_larb_msk) + continue; + + if ((rgn_larb_msk[larbid] & portidmsk) =3D=3D portidmsk) { + region_id =3D i; + break; + } } =20 - if (candidate >=3D 0) - return candidate; - dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n", - &dma_rgn->dma_start, dma_rgn->size); + if (region_id >=3D 0) + return region_id; + dev_err(dev, "Can NOT find the region for larb(%d-%x).\n", + larbid, portidmsk); return -EINVAL; } =20 --=20 2.18.0