Skip to contentSkip to conversation

iommu: dart: Add suspend/resume support

PATCH In review Hector Martin 3 participants 3 messages Dec 5, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: dart: Add suspend/resume support

We need to save/restore the TCR/TTBR registers, since they are lost on power gate.

---
 drivers/iommu/apple-dart.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/iommu/apple-dart.c +50 -0
index 4f4a323be0d0..ed524765ad87 100644
@@ -121,6 +121,9 @@ struct apple_dart {
 
 	struct iommu_group *sid2group[DART_MAX_STREAMS];
 	struct iommu_device iommu;
+
+	u32 save_tcr[DART_MAX_STREAMS];
+	u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR];
 };
 
 /*
@@ -932,6 +935,50 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = {
 	.fmt = APPLE_DART2,
 };
 
+#ifdef CONFIG_PM_SLEEP
+static int apple_dart_suspend(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		dart->save_tcr[sid] = readl_relaxed(dart->regs + DART_TCR(sid));
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			dart->save_ttbr[sid][idx] =
+				readl_relaxed(dart->regs + DART_TTBR(sid, idx));
+	}
+
+	return 0;
+}
+
+static int apple_dart_resume(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+	int ret;
+
+	ret = apple_dart_hw_reset(dart);
+	if (ret) {
+		dev_err(dev, "Failed to reset DART on resume\n");
+		return ret;
+	}
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			writel_relaxed(dart->save_ttbr[sid][idx],
+				       dart->regs + DART_TTBR(sid, idx));
+		writel_relaxed(dart->save_tcr[sid], dart->regs + DART_TCR(sid));
+	}
+
+	return 0;
+}
+
+static const struct dev_pm_ops apple_dart_pm_ops = {
+	.suspend	= apple_dart_suspend,
+	.resume		= apple_dart_resume,
+};
+#endif
+
 static const struct of_device_id apple_dart_of_match[] = {
 	{ .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 },
 	{ .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 },
@@ -944,6 +991,9 @@ static struct platform_driver apple_dart_driver = {
 		.name			= "apple-dart",
 		.of_match_table		= apple_dart_of_match,
 		.suppress_bind_attrs    = true,
+#ifdef CONFIG_PM_SLEEP
+		.pm			= &apple_dart_pm_ops,
+#endif
 	},
 	.probe	= apple_dart_probe,
 	.remove	= apple_dart_remove,
Signature · 1 line
2.35.1
reviewer
#1 lore raw
Quoted 23 lines · Hector Martin

We need to save/restore the TCR/TTBR registers, since they are lost on power gate.

---
  drivers/iommu/apple-dart.c | 50 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
drivers/iommu/apple-dart.c +3 -0
index 4f4a323be0d0..ed524765ad87 100644
@@ -121,6 +121,9 @@ struct apple_dart {
 
  	struct iommu_group *sid2group[DART_MAX_STREAMS];
  	struct iommu_device iommu;
+
+	u32 save_tcr[DART_MAX_STREAMS];
+	u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR];
  };
 
  /*

drivers/iommu/apple-dart.c static const struct apple_dart_hw apple_dart_hw_t6000 = { · line 938

Quoted 4 lines
drivers/iommu/apple-dart.c +1 -0
@@ -932,6 +935,50 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = {
  	.fmt = APPLE_DART2,
  };
 
+#ifdef CONFIG_PM_SLEEP

Between DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr(), I think we should be able to allow full coverage without any explicit #ifdefs or other annotations now.

Cheers,
Robin.

Quoted 56 lines
drivers/iommu/apple-dart.c +46 -0
+static int apple_dart_suspend(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		dart->save_tcr[sid] = readl_relaxed(dart->regs + DART_TCR(sid));
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			dart->save_ttbr[sid][idx] =
+				readl_relaxed(dart->regs + DART_TTBR(sid, idx));
+	}
+
+	return 0;
+}
+
+static int apple_dart_resume(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+	int ret;
+
+	ret = apple_dart_hw_reset(dart);
+	if (ret) {
+		dev_err(dev, "Failed to reset DART on resume\n");
+		return ret;
+	}
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			writel_relaxed(dart->save_ttbr[sid][idx],
+				       dart->regs + DART_TTBR(sid, idx));
+		writel_relaxed(dart->save_tcr[sid], dart->regs + DART_TCR(sid));
+	}
+
+	return 0;
+}
+
+static const struct dev_pm_ops apple_dart_pm_ops = {
+	.suspend	= apple_dart_suspend,
+	.resume		= apple_dart_resume,
+};
+#endif
+
  static const struct of_device_id apple_dart_of_match[] = {
  	{ .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 },
  	{ .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 },
@@ -944,6 +991,9 @@ static struct platform_driver apple_dart_driver = {
  		.name			= "apple-dart",
  		.of_match_table		= apple_dart_of_match,
  		.suppress_bind_attrs    = true,
+#ifdef CONFIG_PM_SLEEP
+		.pm			= &apple_dart_pm_ops,
+#endif
  	},
  	.probe	= apple_dart_probe,
  	.remove	= apple_dart_remove,
Reviewed-by on v1 1/1
#2 lore raw
In reply to Hector Martin

Hi,

Quoted 27 lines · Hector Martin

We need to save/restore the TCR/TTBR registers, since they are lost on power gate.

---
 drivers/iommu/apple-dart.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/iommu/apple-dart.c +4 -0
index 4f4a323be0d0..ed524765ad87 100644
@@ -121,6 +121,9 @@ struct apple_dart {
 
 	struct iommu_group *sid2group[DART_MAX_STREAMS];
 	struct iommu_device iommu;
+
+	u32 save_tcr[DART_MAX_STREAMS];
+	u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR];
 };
 
 /*
@@ -932,6 +935,50 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = {
 	.fmt = APPLE_DART2,
 };
 
+#ifdef CONFIG_PM_SLEEP

drivers/iommu/apple-dart.c static const struct apple_dart_hw apple_dart_hw_t6000 = { · line 948

Quoted 10 lines
drivers/iommu/apple-dart.c +10 -0
+static int apple_dart_suspend(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		dart->save_tcr[sid] = readl_relaxed(dart->regs + DART_TCR(sid));
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			dart->save_ttbr[sid][idx] =
+				readl_relaxed(dart->regs + DART_TTBR(sid, idx));

The rest of the driver uses the non-relaxed variants and I'd use the same here (and further below) to be consistent.

Quoted 22 lines
drivers/iommu/apple-dart.c +22 -0
+	}
+
+	return 0;
+}
+
+static int apple_dart_resume(struct device *dev)
+{
+	struct apple_dart *dart = dev_get_drvdata(dev);
+	unsigned int sid, idx;
+	int ret;
+
+	ret = apple_dart_hw_reset(dart);
+	if (ret) {
+		dev_err(dev, "Failed to reset DART on resume\n");
+		return ret;
+	}
+
+	for (sid = 0; sid < DART_MAX_STREAMS; sid++) {
+		for (idx = 0; idx < DART_MAX_TTBR; idx++)
+			writel_relaxed(dart->save_ttbr[sid][idx],
+				       dart->regs + DART_TTBR(sid, idx));
+		writel_relaxed(dart->save_tcr[sid], dart->regs + DART_TCR(sid));

drivers/iommu/apple-dart.c static const struct apple_dart_hw apple_dart_hw_t6000 = { · line 980

Quoted 10 lines
drivers/iommu/apple-dart.c +10 -0
+	}
+
+	return 0;
+}
+
+static const struct dev_pm_ops apple_dart_pm_ops = {
+	.suspend	= apple_dart_suspend,
+	.resume		= apple_dart_resume,
+};
+#endif

You can use DEFINE_SIMPLE_DEV_PM_OPS here and then ...

drivers/iommu/apple-dart.c static struct platform_driver apple_dart_driver = { · line 995

Quoted 10 lines
drivers/iommu/apple-dart.c +3 -0
+
 static const struct of_device_id apple_dart_of_match[] = {
 	{ .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 },
 	{ .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 },
@@ -944,6 +991,9 @@ static struct platform_driver apple_dart_driver = {
 		.name			= "apple-dart",
 		.of_match_table		= apple_dart_of_match,
 		.suppress_bind_attrs    = true,
+#ifdef CONFIG_PM_SLEEP
+		.pm			= &apple_dart_pm_ops,

... pm_sleep_ptr() here to get rid of these #ifdefs like e.g. in our nvme driver.

With those things addressed:

Sven