From patchwork Fri Mar 31 03:31:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Zhang X-Patchwork-Id: 77521 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp295567vqo; Thu, 30 Mar 2023 20:38:59 -0700 (PDT) X-Google-Smtp-Source: AKy350YV55WzsdDv5wxHW+u3FWaP3/5VdaqhQrrWOVPGn/JUWZTyY4Ml7/I+Bnr3nitMMEQj5PkN X-Received: by 2002:a17:906:1501:b0:93e:9362:7607 with SMTP id b1-20020a170906150100b0093e93627607mr22728918ejd.20.1680233938982; Thu, 30 Mar 2023 20:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680233938; cv=none; d=google.com; s=arc-20160816; b=xPwuqlYSL77AphKQuaumlbDrOijbEqIVDfU6z4gdL28b/rUdRa1eMrrsbwSpEj/0Z2 SbGhqBm8F2At24TbLT/MbUdlpXXLiLyDTvTBpP7ek0wvtn9N02UYZL4QMY0r2BF+Oe3z PiSoQ7LM1EcZp1B3P8GVFMA/kUvQGExdpwrSN1v7b8bgebhC3Z4I8pCJFnT4P9P34n+2 JRbfj/0IRl9B5JBPuEs6YBgOcgr9Ogp2FQPw0PmI5uMnVPzAbExvZ/TnZZvIqzow/cAe uKbN9T5hyll0nNn8iTJMcOp+OnU1ST64iwXgZjnoVk2mGAhW1ZPHigMh7KW0+6rOtysw 93Mw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=qtOn0DhjOxthDfL0ZsZgFYX1mwPfvsE7T9xws5Nc+F43apsyEH4CNyR9GUMIeFFkzk 6aCjvj3fywKo0Cy/jg5TRKGECR7yNu56YQIXmTmM1/b8vsTD84jqLIA0KflLYCVXApQj JDLn2kOyzwAat/uBTgW+X8UoVmKEdyuZ7jkeRpn3bzfKq0v83fiwCjHNinAV3IG+BL0L uuhx4N38xGHt4T1qQat65an76Q6flTBWgh0WlKXmWujRvBWP2YUuTaAvWWGI+NH5sZbA EUKUe6hVQLYkypC5sciYrF/MdyMkrkqd5apnn0Qn217XyKCOMXVeRac2Jyb33zy/9w6W rcKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DCa6KQxG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bh1-20020a170906a0c100b00947bac8a3cbsi106417ejb.546.2023.03.30.20.38.34; Thu, 30 Mar 2023 20:38:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DCa6KQxG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbjCaDcE (ORCPT + 99 others); Thu, 30 Mar 2023 23:32:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbjCaDcB (ORCPT ); Thu, 30 Mar 2023 23:32:01 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CC37CA19 for ; Thu, 30 Mar 2023 20:31:58 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id o2so20046945plg.4 for ; Thu, 30 Mar 2023 20:31:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680233517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=DCa6KQxGG6AzMQVouFVFhw+hOgFCcmaF1zjXTSBVZFn3cRWwVJMldaTcDG/bcHarIn wKN527uYemylzJGjuDKerFk0a1H1DxfYo0pWhoAM2XeqMrO/zWP7A4j2eKcsklyP5nYO 6iLv41misbE4Ld549/Mpg0xZ0xvDmWDPHdldwhJ0tKWt8eZteEbQaixaP83kHwVdThhX FNYYiN/01zrYghz6igEjQwEA47ScJQf/uEjLsppkLDXuoWrkLBkMYRStm7guSPHSL0Ve WKhcgrWCVPP4yZOltI4ItQJGBVwY39oGinS+lAAVzuVw6H1dj8B8hbE1qmAghoPRhWgZ aFCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680233517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=zn1kbVSVgFN78jhwNNRxNX66gcmEUayTlUcCKuUPLtMyJqTaFXvgv9uhzE4Iim95vJ fs8/FonSi3GYoryqtF4n7Z+4hp2OmnoCwYzXoj225Tq6mHQCAMZzdm2vjojHGw1oDUea 331qB03VS3dFfxe+XttyA5yq4RyKMDXeeZoatCGJlFHSA80LGXOpwm8b6oVk+UoT/63K ghvgkYT8zmfdMEgnZuLrI1DH0/ZLJAc/CNHSHR5O1zwq9uKKJme3a7ooQv86xeU+fCC1 pjbH42T0GD7S2/FpLyoUZnxKVbKKYrCiwiIX5e0VzfKkHoXzjUrCb7ODhBW2JNrSatOv cSAg== X-Gm-Message-State: AAQBX9fS94BML7gKlXO5N6zENCyheNlKkkw+KRNmJjrwh4VwbXGw/vAP FCxXtb3VJT5o/tT8uNdajfE= X-Received: by 2002:a17:90b:33cc:b0:237:40a5:7cb9 with SMTP id lk12-20020a17090b33cc00b0023740a57cb9mr28369711pjb.5.1680233517522; Thu, 30 Mar 2023 20:31:57 -0700 (PDT) Received: from ubt.spreadtrum.com ([138.113.14.245]) by smtp.gmail.com with ESMTPSA id p4-20020a170902eac400b001a239325f1csm449128pld.100.2023.03.30.20.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:31:57 -0700 (PDT) From: Chunyan Zhang To: Joerg Roedel , Will Deacon , Robin Murphy Cc: iommu@lists.linux.dev, Baolin Wang , Orson Zhai , Chunyan Zhang , Chunyan Zhang , LKML Subject: [RESEND PATCH V3 2/2] iommu: sprd: Add support for reattaching an existing domain Date: Fri, 31 Mar 2023 11:31:24 +0800 Message-Id: <20230331033124.864691-3-zhang.lyra@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230331033124.864691-1-zhang.lyra@gmail.com> References: <20230331033124.864691-1-zhang.lyra@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761852983291462456?= X-GMAIL-MSGID: =?utf-8?q?1761852983291462456?= From: Chunyan Zhang This IOMMU driver should allow a domain to be attached more than once. If IOMMU is reattaching to the same domain which is attached, there's nothing to be done. If reattching to a previously-used domain, do not alloc DMA buffer again which stores address mapping table to avoid memory leak. Signed-off-by: Chunyan Zhang --- drivers/iommu/sprd-iommu.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index 7df1f730c778..3513b2b108bf 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -62,6 +62,7 @@ enum sprd_iommu_version { * @eb: gate clock which controls IOMMU access */ struct sprd_iommu_device { + struct sprd_iommu_domain *dom; enum sprd_iommu_version ver; u32 *prot_page_va; dma_addr_t prot_page_pa; @@ -252,15 +253,27 @@ static int sprd_iommu_attach_device(struct iommu_domain *domain, struct sprd_iommu_domain *dom = to_sprd_domain(domain); size_t pgt_size = sprd_iommu_pgt_size(domain); - if (dom->sdev) - return -EINVAL; + /* The device is attached to this domain */ + if (sdev->dom == dom) + return 0; - dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); - if (!dom->pgt_va) - return -ENOMEM; + /* The first time that domain is attaching to a device */ + if (!dom->pgt_va) { + dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); + if (!dom->pgt_va) + return -ENOMEM; + + dom->sdev = sdev; + } - dom->sdev = sdev; + sdev->dom = dom; + /* + * One sprd IOMMU serves one client device only, disabled it before + * configure mapping table to avoid access conflict in case other + * mapping table is stored in. + */ + sprd_iommu_hw_en(sdev, false); sprd_iommu_first_ppn(dom); sprd_iommu_first_vpn(dom); sprd_iommu_vpn_range(dom);