diff mbox series

[v12,2/6] iommu/tegra: Add tegra_dev_iommu_get_stream_id() helper

Message ID 20221202152644.29450-3-thierry.reding@gmail.com (mailing list archive)
State New, archived
Headers show
Series mmc: sdhci: Add Tegra234 support | expand

Commit Message

Thierry Reding Dec. 2, 2022, 3:26 p.m. UTC
From: Thierry Reding <treding@nvidia.com>

Access to the internals of struct iommu_fwspec by non-IOMMU drivers is
discouraged. Many drivers for Tegra SoCs, however, need access to their
IOMMU stream IDs so that they can be programmed into various hardware
registers.

Formalize this access into a common helper to make it easier to audit
and maintain.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 include/linux/iommu.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

Comments

Robin Murphy Dec. 5, 2022, 7:03 p.m. UTC | #1
On 2022-12-02 15:26, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> Access to the internals of struct iommu_fwspec by non-IOMMU drivers is
> discouraged. Many drivers for Tegra SoCs, however, need access to their
> IOMMU stream IDs so that they can be programmed into various hardware
> registers.
> 
> Formalize this access into a common helper to make it easier to audit
> and maintain.

In many ways I'd be happier if this could go somewhere in 
include/soc/tegra/ rather than the generic IOMMU API header, but I see 
the argument for keeping fwspec usage close to home as well, so no 
overwhelming preference one way or the other. Mostly I'd just like to 
get something landed so that all those other drivers can be cleaned up 
as well, and I reckon this counts as good enough.

Unless Joerg has a stronger opinion,

Acked-by: Robin Murphy <robin.murphy@arm.com>

> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>   include/linux/iommu.h | 21 +++++++++++++++++++++
>   1 file changed, 21 insertions(+)
> 
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index dc5e6f5c14fc..f0d72acad7db 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -1186,4 +1186,25 @@ static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
>   }
>   #endif /* CONFIG_IOMMU_SVA */
>   
> +/*
> + * Newer generations of Tegra SoCs require devices' stream IDs to be directly programmed into
> + * some registers. These are always paired with a Tegra SMMU or ARM SMMU, for which the contents
> + * of the struct iommu_fwspec are known. Use this helper to formalize access to these internals.
> + */
> +#define TEGRA_STREAM_ID_BYPASS 0x7f
> +
> +static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream_id)
> +{
> +#ifdef CONFIG_IOMMU_API
> +	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
> +
> +	if (fwspec && fwspec->num_ids == 1) {
> +		*stream_id = fwspec->ids[0] & 0xffff;
> +		return true;
> +	}
> +#endif
> +
> +	return false;
> +}
> +
>   #endif /* __LINUX_IOMMU_H */
Thierry Reding Dec. 6, 2022, 4:49 p.m. UTC | #2
On Mon, Dec 05, 2022 at 07:03:09PM +0000, Robin Murphy wrote:
> On 2022-12-02 15:26, Thierry Reding wrote:
> > From: Thierry Reding <treding@nvidia.com>
> > 
> > Access to the internals of struct iommu_fwspec by non-IOMMU drivers is
> > discouraged. Many drivers for Tegra SoCs, however, need access to their
> > IOMMU stream IDs so that they can be programmed into various hardware
> > registers.
> > 
> > Formalize this access into a common helper to make it easier to audit
> > and maintain.
> 
> In many ways I'd be happier if this could go somewhere in include/soc/tegra/
> rather than the generic IOMMU API header, but I see the argument for keeping
> fwspec usage close to home as well, so no overwhelming preference one way or
> the other. Mostly I'd just like to get something landed so that all those
> other drivers can be cleaned up as well, and I reckon this counts as good
> enough.

Yeah, given Joerg's comment about potentially hiding struct iommu_fwspec
better from others at some point in the future, I figured it'd be better
to have this in a more central location.

> Unless Joerg has a stronger opinion,
> 
> Acked-by: Robin Murphy <robin.murphy@arm.com>

Thanks,
Thierry
diff mbox series

Patch

diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index dc5e6f5c14fc..f0d72acad7db 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -1186,4 +1186,25 @@  static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
 }
 #endif /* CONFIG_IOMMU_SVA */
 
+/*
+ * Newer generations of Tegra SoCs require devices' stream IDs to be directly programmed into
+ * some registers. These are always paired with a Tegra SMMU or ARM SMMU, for which the contents
+ * of the struct iommu_fwspec are known. Use this helper to formalize access to these internals.
+ */
+#define TEGRA_STREAM_ID_BYPASS 0x7f
+
+static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream_id)
+{
+#ifdef CONFIG_IOMMU_API
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+
+	if (fwspec && fwspec->num_ids == 1) {
+		*stream_id = fwspec->ids[0] & 0xffff;
+		return true;
+	}
+#endif
+
+	return false;
+}
+
 #endif /* __LINUX_IOMMU_H */