Message ID | 20230314184659.176473-1-manivannan.sadhasivam@linaro.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1943124wrd; Tue, 14 Mar 2023 12:26:39 -0700 (PDT) X-Google-Smtp-Source: AK7set9dXDx4JNYAN1YLqRwkHD7wSPYPfvfBvlkH3ReI4a9HBuI5DVrdFuOsinK4MHJNl6EK3yxM X-Received: by 2002:a05:6a20:4f10:b0:d3:d236:f5b7 with SMTP id gi16-20020a056a204f1000b000d3d236f5b7mr9295828pzb.26.1678821999043; Tue, 14 Mar 2023 12:26:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678821999; cv=none; d=google.com; s=arc-20160816; b=iz2bE4G83aVeocKPJBuaUY7yDRX6sE1ICfJEudoK8jpZtHAGZZZC6Ngsitoz1d1rwk sJCG4QUU1mbPqY8Dc4Ssu7VTZoSJVvJ2Tr+Bw1Qh5Od5BXZdxyjyGjUokp79heZQATGz Yu+CmUH67s5boQoWvcHzGngQpjI6cPnTPmCZR18StH+hHhLUz0w3ACMcLMMX7e99eaUN XE7DkYQ866yl3YjGAeNzbgjpyfh4vT2RY3cXpxNatcVSWV4lvx2g3ROzZguFaTUZLpNw HXXwwEj2dbcKJGWzA5P1Sn4eSDjbVKPJXgGtmw4KyHhZVIu0CBh3WHNwwU4o+9/xBZ2p 8i+Q== 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=fpiDPV6205M5h8bqVFYHaaAk5mlWpnrzsDJlxhDj4wc=; b=ngPfWnKYLlWojE38z3SqnSThIT0m0i0gi6mOA5CiM0lvck6BKE1WdC7Bn8G29nG4TP Jzgja7mpVqGTOpNL+4dl0DUxoRkogr+AlkzAdC6jod/NCByCxHWOvSv3b4Dj4Rv7m3gO Jb93zuWZQKoTw+5a6WeNSFiNMuxyIqFFT7cxcA6mv60iGqMOBYEz8l0OJ8iXdZ1PAfi3 Fs+YAwN2r5yzAYsXOCCjC1C/G8f1E7pEdclLvmmDqmWSndgpoE5BgYZmHVs1biPz/nE7 PFKFMQq3pFzHytUhWfPlutKIKX2Mk2Ut9SJAvJtxenvgNClvSNL0prflgnsFPZQ+weCI Pz5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oCIgU7JM; 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=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c72-20020a621c4b000000b005a8e403a325si2903775pfc.10.2023.03.14.12.26.26; Tue, 14 Mar 2023 12:26:39 -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=@linaro.org header.s=google header.b=oCIgU7JM; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230499AbjCNSrl (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Tue, 14 Mar 2023 14:47:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230174AbjCNSri (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 14 Mar 2023 14:47:38 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC0FB79D6 for <linux-kernel@vger.kernel.org>; Tue, 14 Mar 2023 11:47:12 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id j3-20020a17090adc8300b0023d09aea4a6so6974177pjv.5 for <linux-kernel@vger.kernel.org>; Tue, 14 Mar 2023 11:47:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1678819629; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=fpiDPV6205M5h8bqVFYHaaAk5mlWpnrzsDJlxhDj4wc=; b=oCIgU7JMjQpLn46jQ77R/mhDV9eGuDUoffPrfCIqkdC/ewgl5MGXUwcsQII6Gx9YQx 1G2aCGdEggBu5LZZxUKbLtU6tIBpQT7TKAS6FbudToJyon/o5E6tblEVKXQ6zZNiNw4u wtcdB/WWAoX6wBx17h79kepvv/FYYAwksbUyWssAvwPhrl24fQusChPo3SgosrkFM0YK BESclnIzMp/KTA/i6qzeyAmYmWtvlYrnu6+Av+SWD2R3GQiw1lgI2IQzKejxzm/mJl0G Dc1oFfaDbYImcm2DqALJNuCs9rs+jeH1IW/k8aNaPHlpSHWf+NsWcpub1OclVMsjPwLv HcTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678819629; 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=fpiDPV6205M5h8bqVFYHaaAk5mlWpnrzsDJlxhDj4wc=; b=yS4PUtxWvYiNRHauroDU0snoXQuA43lxDlq2biAhI5J4ERDF/RYLADF3+ds06wPf0+ apKNNfxZxKDYRmESbN4iyBr4d1UIYTKlAey/ti0h9oByBMqdTDeZFrIlusb4neZBlwja 9bJSDDr3EzQV0beKhQqJ/Yop8+Wg5uGajPlazSKaBnayoS1ST5e34qCsVC1irP31UWQB nPdEKss1FOPNcQycVWLmDN0qcGrQbxGoiiLpDXQp8y/Pfdr/uNl3CE9tfIdmfMibJPqN zzqW9gUOJt40SUTf4+XEVC7S7/XGo8sldOfjj2bkVTdzfgG2tcu2rnMrhnzPfzcDDxQp Vwmw== X-Gm-Message-State: AO0yUKV7BB6Ctdh7AatU7JLl1Hppy/73gka47E2VbN6QpdiWImZrXwJd wRZJro7zh8ar2y/L3g6ukdQF X-Received: by 2002:a17:902:740c:b0:19f:2dff:2199 with SMTP id g12-20020a170902740c00b0019f2dff2199mr10106839pll.68.1678819628793; Tue, 14 Mar 2023 11:47:08 -0700 (PDT) Received: from localhost.localdomain ([117.217.182.35]) by smtp.gmail.com with ESMTPSA id kz11-20020a170902f9cb00b001a0667822c8sm1920782plb.94.2023.03.14.11.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 11:47:06 -0700 (PDT) From: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> To: will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, andersson@kernel.org, johan+linaro@kernel.org, steev@kali.org, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> Subject: [PATCH v3] iommu/arm-smmu-qcom: Rework the logic finding the bypass quirk Date: Wed, 15 Mar 2023 00:16:59 +0530 Message-Id: <20230314184659.176473-1-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760372456408104164?= X-GMAIL-MSGID: =?utf-8?q?1760372456408104164?= |
Series |
[v3] iommu/arm-smmu-qcom: Rework the logic finding the bypass quirk
|
|
Commit Message
Manivannan Sadhasivam
March 14, 2023, 6:46 p.m. UTC
The logic used to find the quirky firmware that intercepts the writes to S2CR register to replace bypass type streams with a fault, and ignore the fault type, is not working with the firmware on newer SoCs like SC8280XP. The current logic uses the last stream mapping group (num_mapping_groups - 1) as an index for finding quirky firmware. But on SC8280XP, NUMSMRG reports a value of 162 due to emulation and the logic is not working for stream mapping groups > 128. (Note that the ARM SMMU architecture specification defines NUMSMRG in the range of 0-127). So the current logic that checks the (162-1)th S2CR entry fails to detect the quirky firmware on these devices and SMMU triggers invalid context fault for bypass streams. To fix this issue, let's limit "num_mapping_groups" to 128 as per ARM SMMU spec and rework the logic to find the first non-valid (free) stream mapping register group (SMR) and use that index to access S2CR for detecting the bypass quirk. If no free groups are available, then just skip the quirk detection. While at it, let's move the quirk detection logic to a separate function and change the local variable name from last_s2cr to free_s2cr. Reviewed-by: Bjorn Andersson <andersson@kernel.org> Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> --- Changes in v3: * Limited num_mapping_groups to 128 as per ARM SMMU spec and removed the check for 128 groups in qcom_smmu_bypass_quirk() * Reworded the commit message accordingly Changes in v2: * Limited the check to 128 groups as per ARM SMMU spec's NUMSMRG range * Moved the quirk handling to its own function * Collected review tag from Bjorn drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c | 52 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 8 deletions(-)
Comments
On Wed, Mar 15, 2023 at 12:16:59AM +0530, Manivannan Sadhasivam wrote: > The logic used to find the quirky firmware that intercepts the writes to > S2CR register to replace bypass type streams with a fault, and ignore the > fault type, is not working with the firmware on newer SoCs like SC8280XP. > > The current logic uses the last stream mapping group (num_mapping_groups > - 1) as an index for finding quirky firmware. But on SC8280XP, NUMSMRG > reports a value of 162 due to emulation and the logic is not working for > stream mapping groups > 128. (Note that the ARM SMMU architecture > specification defines NUMSMRG in the range of 0-127). > > So the current logic that checks the (162-1)th S2CR entry fails to detect > the quirky firmware on these devices and SMMU triggers invalid context > fault for bypass streams. > > To fix this issue, let's limit "num_mapping_groups" to 128 as per ARM SMMU > spec and rework the logic to find the first non-valid (free) stream mapping > register group (SMR) and use that index to access S2CR for detecting the > bypass quirk. If no free groups are available, then just skip the quirk > detection. > > While at it, let's move the quirk detection logic to a separate function > and change the local variable name from last_s2cr to free_s2cr. > > Reviewed-by: Bjorn Andersson <andersson@kernel.org> > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> > --- > > Changes in v3: > > * Limited num_mapping_groups to 128 as per ARM SMMU spec and removed the > check for 128 groups in qcom_smmu_bypass_quirk() > * Reworded the commit message accordingly > > Changes in v2: > > * Limited the check to 128 groups as per ARM SMMU spec's NUMSMRG range > * Moved the quirk handling to its own function > * Collected review tag from Bjorn > +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) > +{ > + u32 smr; > + int i; > + > + /* > + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture > + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the > + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And > + * those groups don't exhibit the same behavior as the architecture supported ones. > + */ Please fix your editor so that it wraps lines at 80 columns, which is still the preferred (soft) limit. > + if (smmu->num_mapping_groups > 128) { > + dev_warn(smmu->dev, "\tLimiting the stream matching groups to 128\n"); dev_notice() should do since there's nothing a user can do about this. > + smmu->num_mapping_groups = 128; > + } So this hunk is really all that is needed to make the current quirk detection work on sc8280xp. Why not simply stick with the current logic and use the last group until there is a need for anything more? Also, should this not be done in arm_smmu_device_cfg_probe() as I suggested earlier (e.g. to avoid allocating resources for the groups that will never be used)? > + > + qcom_smmu_bypass_quirk(smmu); > > for (i = 0; i < smmu->num_mapping_groups; i++) { > smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i)); Johan
On Wed, Mar 15, 2023 at 08:37:32AM +0100, Johan Hovold wrote: > On Wed, Mar 15, 2023 at 12:16:59AM +0530, Manivannan Sadhasivam wrote: > > The logic used to find the quirky firmware that intercepts the writes to > > S2CR register to replace bypass type streams with a fault, and ignore the > > fault type, is not working with the firmware on newer SoCs like SC8280XP. > > > > The current logic uses the last stream mapping group (num_mapping_groups > > - 1) as an index for finding quirky firmware. But on SC8280XP, NUMSMRG > > reports a value of 162 due to emulation and the logic is not working for > > stream mapping groups > 128. (Note that the ARM SMMU architecture > > specification defines NUMSMRG in the range of 0-127). > > > > So the current logic that checks the (162-1)th S2CR entry fails to detect > > the quirky firmware on these devices and SMMU triggers invalid context > > fault for bypass streams. > > > > To fix this issue, let's limit "num_mapping_groups" to 128 as per ARM SMMU > > spec and rework the logic to find the first non-valid (free) stream mapping > > register group (SMR) and use that index to access S2CR for detecting the > > bypass quirk. If no free groups are available, then just skip the quirk > > detection. > > > > While at it, let's move the quirk detection logic to a separate function > > and change the local variable name from last_s2cr to free_s2cr. > > > > Reviewed-by: Bjorn Andersson <andersson@kernel.org> > > Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> > > --- > > > > Changes in v3: > > > > * Limited num_mapping_groups to 128 as per ARM SMMU spec and removed the > > check for 128 groups in qcom_smmu_bypass_quirk() > > * Reworded the commit message accordingly > > > > Changes in v2: > > > > * Limited the check to 128 groups as per ARM SMMU spec's NUMSMRG range > > * Moved the quirk handling to its own function > > * Collected review tag from Bjorn > > > +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) > > +{ > > + u32 smr; > > + int i; > > + > > + /* > > + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture > > + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the > > + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And > > + * those groups don't exhibit the same behavior as the architecture supported ones. > > + */ > > Please fix your editor so that it wraps lines at 80 columns, which is > still the preferred (soft) limit. > If exceeding 80 columns end up making the comment more readable (fewer lines), then why should we limit ourselves? > > + if (smmu->num_mapping_groups > 128) { > > + dev_warn(smmu->dev, "\tLimiting the stream matching groups to 128\n"); > > dev_notice() should do since there's nothing a user can do about this. > Ok. > > + smmu->num_mapping_groups = 128; > > + } > > So this hunk is really all that is needed to make the current quirk > detection work on sc8280xp. Why not simply stick with the current logic > and use the last group until there is a need for anything more? > No! What if the bootloader had set up mapping for 128 groups? In that case we'll overwrite the last group. It is still required to find the valid group and use it for quirk detection. If no group is available, we'll skip it. > Also, should this not be done in arm_smmu_device_cfg_probe() as I > suggested earlier (e.g. to avoid allocating resources for the groups > that will never be used)? > I just went with Robin's suggestion on adding this check to cfg_probe() hook. But I don't have any strong preference either. Thanks, Mani > > + > > + qcom_smmu_bypass_quirk(smmu); > > > > for (i = 0; i < smmu->num_mapping_groups; i++) { > > smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i)); > > Johan
On Wed, Mar 15, 2023 at 01:29:58PM +0530, Manivannan Sadhasivam wrote: > On Wed, Mar 15, 2023 at 08:37:32AM +0100, Johan Hovold wrote: > > > +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) > > > +{ > > > + u32 smr; > > > + int i; > > > + > > > + /* > > > + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture > > > + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the > > > + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And > > > + * those groups don't exhibit the same behavior as the architecture supported ones. > > > + */ > > > > Please fix your editor so that it wraps lines at 80 columns, which is > > still the preferred (soft) limit. > > > > If exceeding 80 columns end up making the comment more readable (fewer lines), > then why should we limit ourselves? Exceeding 80 column for comments does generally not improve readability. That part of the coding standard has do to with not adding excessive line breaks to *code*, where it can sometimes impact readability. > > > + if (smmu->num_mapping_groups > 128) { > > > + dev_warn(smmu->dev, "\tLimiting the stream matching groups to 128\n"); > > > > dev_notice() should do since there's nothing a user can do about this. > > > > Ok. > > > > + smmu->num_mapping_groups = 128; > > > + } > > > > So this hunk is really all that is needed to make the current quirk > > detection work on sc8280xp. Why not simply stick with the current logic > > and use the last group until there is a need for anything more? > > > > No! What if the bootloader had set up mapping for 128 groups? In that case > we'll overwrite the last group. It is still required to find the valid group > and use it for quirk detection. If no group is available, we'll skip it. Yes, but that's also entirely hypothetical (and could perhaps also be handled by adding a warning for now). If you want to rework the quirk handling for this you should at least do so in a separate patch as it is arguably a separate change from fixing the current quirk detection for newer SoCs by capping the number of groups (a minimal fix that could be backported). Johan
On Wed, Mar 15, 2023 at 09:38:42AM +0100, Johan Hovold wrote: > On Wed, Mar 15, 2023 at 01:29:58PM +0530, Manivannan Sadhasivam wrote: > > On Wed, Mar 15, 2023 at 08:37:32AM +0100, Johan Hovold wrote: > > > > > +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) > > > > +{ > > > > + u32 smr; > > > > + int i; > > > > + > > > > + /* > > > > + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture > > > > + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the > > > > + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And > > > > + * those groups don't exhibit the same behavior as the architecture supported ones. > > > > + */ > > > > > > Please fix your editor so that it wraps lines at 80 columns, which is > > > still the preferred (soft) limit. > > > > > > > If exceeding 80 columns end up making the comment more readable (fewer lines), > > then why should we limit ourselves? > > Exceeding 80 column for comments does generally not improve readability. > It all depends on the perspective/preference... But I can limit to 80 columns here. > That part of the coding standard has do to with not adding excessive > line breaks to *code*, where it can sometimes impact readability. > > > > > + if (smmu->num_mapping_groups > 128) { > > > > + dev_warn(smmu->dev, "\tLimiting the stream matching groups to 128\n"); > > > > > > dev_notice() should do since there's nothing a user can do about this. > > > > > > > Ok. > > > > > > + smmu->num_mapping_groups = 128; > > > > + } > > > > > > So this hunk is really all that is needed to make the current quirk > > > detection work on sc8280xp. Why not simply stick with the current logic > > > and use the last group until there is a need for anything more? > > > > > > > No! What if the bootloader had set up mapping for 128 groups? In that case > > we'll overwrite the last group. It is still required to find the valid group > > and use it for quirk detection. If no group is available, we'll skip it. > > Yes, but that's also entirely hypothetical (and could perhaps also be > handled by adding a warning for now). > > If you want to rework the quirk handling for this you should at least do > so in a separate patch as it is arguably a separate change from fixing > the current quirk detection for newer SoCs by capping the number of > groups (a minimal fix that could be backported). > Using a separate patch makes sense to me. Will do that in v4. Thanks, Mani > Johan
On Wed, Mar 15, 2023 at 07:07:30PM +0530, Manivannan Sadhasivam wrote: > On Wed, Mar 15, 2023 at 09:38:42AM +0100, Johan Hovold wrote: > > On Wed, Mar 15, 2023 at 01:29:58PM +0530, Manivannan Sadhasivam wrote: > > > On Wed, Mar 15, 2023 at 08:37:32AM +0100, Johan Hovold wrote: > > > > > > > +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) > > > > > +{ > > > > > + u32 smr; > > > > > + int i; > > > > > + > > > > > + /* > > > > > + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture > > > > > + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the > > > > > + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And > > > > > + * those groups don't exhibit the same behavior as the architecture supported ones. > > > > > + */ > > > > > > > > Please fix your editor so that it wraps lines at 80 columns, which is > > > > still the preferred (soft) limit. > > > > > > > > > > If exceeding 80 columns end up making the comment more readable (fewer lines), > > > then why should we limit ourselves? > > > > Exceeding 80 column for comments does generally not improve readability. > > > > It all depends on the perspective/preference... But I can limit to 80 columns > here. No, this is not about preference. The soft limit is 80 columns and a valid reason for breaking that for comments would be, for example, if you have a long URL or something which does not fit within that width (and not just personal preference). Johan
diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c index d1b296b95c86..301396b29024 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c @@ -266,25 +266,42 @@ static int qcom_smmu_init_context(struct arm_smmu_domain *smmu_domain, return 0; } -static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) +static void qcom_smmu_bypass_quirk(struct arm_smmu_device *smmu) { - unsigned int last_s2cr = ARM_SMMU_GR0_S2CR(smmu->num_mapping_groups - 1); struct qcom_smmu *qsmmu = to_qcom_smmu(smmu); - u32 reg; - u32 smr; + u32 free_s2cr; + u32 reg, smr; int i; + /* + * Find the first non-valid (free) stream mapping register group and + * use that index to access S2CR for detecting the bypass quirk. + */ + for (i = 0; i < smmu->num_mapping_groups; i++) { + smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i)); + + if (!FIELD_GET(ARM_SMMU_SMR_VALID, smr)) + break; + } + + /* If no free stream mapping register group is available, skip the check */ + if (i == smmu->num_mapping_groups) + return; + + free_s2cr = ARM_SMMU_GR0_S2CR(i); + /* * With some firmware versions writes to S2CR of type FAULT are * ignored, and writing BYPASS will end up written as FAULT in the - * register. Perform a write to S2CR to detect if this is the case and - * if so reserve a context bank to emulate bypass streams. + * register. Perform a write to the first free S2CR to detect if + * this is the case and if so reserve a context bank to emulate + * bypass streams. */ reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, S2CR_TYPE_BYPASS) | FIELD_PREP(ARM_SMMU_S2CR_CBNDX, 0xff) | FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, S2CR_PRIVCFG_DEFAULT); - arm_smmu_gr0_write(smmu, last_s2cr, reg); - reg = arm_smmu_gr0_read(smmu, last_s2cr); + arm_smmu_gr0_write(smmu, free_s2cr, reg); + reg = arm_smmu_gr0_read(smmu, free_s2cr); if (FIELD_GET(ARM_SMMU_S2CR_TYPE, reg) != S2CR_TYPE_BYPASS) { qsmmu->bypass_quirk = true; qsmmu->bypass_cbndx = smmu->num_context_banks - 1; @@ -296,6 +313,25 @@ static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, CBAR_TYPE_S1_TRANS_S2_BYPASS); arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(qsmmu->bypass_cbndx), reg); } +} + +static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu) +{ + u32 smr; + int i; + + /* + * Limit the number of stream matching groups to 128 as the ARM SMMU architecture + * specification defines NUMSMRG (Number of Stream Mapping Register Groups) in the + * range of 0-127, but some Qcom platforms emulate more stream mapping groups. And + * those groups don't exhibit the same behavior as the architecture supported ones. + */ + if (smmu->num_mapping_groups > 128) { + dev_warn(smmu->dev, "\tLimiting the stream matching groups to 128\n"); + smmu->num_mapping_groups = 128; + } + + qcom_smmu_bypass_quirk(smmu); for (i = 0; i < smmu->num_mapping_groups; i++) { smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));