Skip to contentSkip to conversation

media: rockchip: rkcif: Fix a system hang and validate the pixel format

PATCH In review Mehdi Djait 2 participants 8 messages Nov 12 – Nov 17

Subsystems (4)
lore
Share & tools
lore

Mehdi Djait

author maintainer
#0 lore raw

cover media: rockchip: rkcif: Fix a system hang and validate the pixel format

Hello Michael :)

Sorry for the additional work with this flood of patches...

This series fixes two issues in the rkcif driver that stayed long enough on my TODO list.

I am using an RK3588 ROCK 5B with an IMX415 sensor.

Patch 1 drops the reset from the runtime suspend. The reset also resets the VICAP IOMMU and hangs the system when the IOMMU driver accesses the IOMMU registers at the same time. This was reproduced by repeatedly starting and stopping the stream with runtime PM enabled.

Steps to reproduce the issue:
* configure the media pipeline
* You can use this script here:
--------------------------------------------------------------------------------

$ cat stream-loop.sh
#!/bin/bash
# Start and stop yavta in a loop: stream for a random 0.1-1 s
# Usage: ./stream-loop.sh [device]

DEV=${1:-/dev/video0}

trap 'echo; echo "stopped after $i iterations"; exit 0' INT

i=0
while true; do

	i=$((i + 1))
	stream=$(printf "0.%03d" $((100 + RANDOM % 900)))

	echo "=== iteration $i: stream ${stream}s"
	timeout -s INT "$stream" yavta -c -f SGBRG10 -s 3864x2192 "$DEV"

done
--------------------------------------------------------------------------------

Patch 2 rejects pipelines where the pixel format does not match the media bus code on the source pad.

Steps to reproduce the issue:
* configure the media pipeline to use SGBRG10
* try to capture with yavta using the format SGBRG12

Fyi:
Another patch is also needed to not get the timeout everytime the streaming is stopped but some of the work on that patch is already done in Daniel's 4th patch:
https://lore.kernel.org/linux-media/[email protected]/

Mehdi Djait (2):
media: rockchip: rkcif: Don't reset the hardware in runtime suspend
media: rockchip: rkcif: Validate the pixel format against the media
bus code

 .../media/platform/rockchip/rkcif/rkcif-common.h  |  1 -
 drivers/media/platform/rockchip/rkcif/rkcif-dev.c | 15 ---------------
 .../media/platform/rockchip/rkcif/rkcif-stream.c  | 12 ++++++++++++
 3 files changed, 12 insertions(+), 16 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH 1/2 media: rockchip: rkcif: Don't reset the hardware in runtime suspend

The reset in the runtime suspend also resets the VICAP IOMMU.

When the IOMMU driver accesses the IOMMU registers inside the delay window between the assert and deassert, e.g. in rk_iommu_zap_lines() to shootdown IOTLB entries, the system hangs.

Drop the reset and the now unused reset control.

Fixes f53fb31a3889 ("media: rockchip: add driver for the rockchip camera interface")
---
 .../media/platform/rockchip/rkcif/rkcif-common.h  |  1 -
 drivers/media/platform/rockchip/rkcif/rkcif-dev.c | 15 ---------------
 2 files changed, 16 deletions(-)
drivers/media/platform/rockchip/rkcif/rkcif-common.h +0 -1
index 4d9211ba9bda..38208e8b7b2d 100644
@@ -237,7 +237,6 @@ struct rkcif_device {
 	struct clk_bulk_data clks[RKCIF_CLK_MAX];
 	unsigned int clks_num;
 	struct regmap *grf;
-	struct reset_control *reset;
 	void __iomem *base_addr;
 
 	struct rkcif_interface interfaces[RKCIF_IF_MAX];
drivers/media/platform/rockchip/rkcif/rkcif-dev.c +0 -15
index be3a174b9aab..dceabee2ecd6 100644
@@ -10,7 +10,6 @@
  */
 
 #include <linux/clk.h>
-#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
@@ -18,7 +17,6 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
-#include <linux/reset.h>
 
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-mc.h>
@@ -201,10 +199,6 @@ static int rkcif_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "failed to get clocks\n");
 
-	rkcif->reset = devm_reset_control_array_get_exclusive(dev);
-	if (IS_ERR(rkcif->reset))
-		return PTR_ERR(rkcif->reset);
-
 	rkcif->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
 						     "rockchip,grf");
 	if (IS_ERR(rkcif->grf))
@@ -273,15 +267,6 @@ static int rkcif_runtime_suspend(struct device *dev)
 {
 	struct rkcif_device *rkcif = dev_get_drvdata(dev);
 
-	/*
-	 * Reset CIF (CRU, DMA, FIFOs) to allow a clean resume.
-	 * Since this resets the IOMMU too, we cannot issue this reset when
-	 * resuming.
-	 */
-	reset_control_assert(rkcif->reset);
-	udelay(5);
-	reset_control_deassert(rkcif->reset);
-
 	clk_bulk_disable_unprepare(rkcif->clks_num, rkcif->clks);
 
 	return 0;
Signature · 1 line
2.56.0
maintainer
#2 lore raw

Re: [PATCH v1 1/2] media: rockchip: rkcif: Don't reset the hardware in runtime suspend

Hi Mehdi,

Quoted 7 lines · Mehdi Djait

The reset in the runtime suspend also resets the VICAP IOMMU.

When the IOMMU driver accesses the IOMMU registers inside the delay window between the assert and deassert, e.g. in rk_iommu_zap_lines() to shootdown IOTLB entries, the system hangs.

Drop the reset and the now unused reset control.

I agree that the current situation is nothing more than a crude hack that happened to work in most cases. But I doubt simply removing the reset is the correct solution. Recently, I saw some downstream patch that makes the reset a shared one, which seems like the right thing to do here. What would you think about that?

Best regards,
Michael

Quoted 58 lines
Fixes f53fb31a3889 ("media: rockchip: add driver for the rockchip camera interface")
---
 .../media/platform/rockchip/rkcif/rkcif-common.h  |  1 -
 drivers/media/platform/rockchip/rkcif/rkcif-dev.c | 15 ---------------
 2 files changed, 16 deletions(-)
drivers/media/platform/rockchip/rkcif/rkcif-common.h +0 -1
index 4d9211ba9bda..38208e8b7b2d 100644
@@ -237,7 +237,6 @@ struct rkcif_device {
 	struct clk_bulk_data clks[RKCIF_CLK_MAX];
 	unsigned int clks_num;
 	struct regmap *grf;
-	struct reset_control *reset;
 	void __iomem *base_addr;
 
 	struct rkcif_interface interfaces[RKCIF_IF_MAX];
drivers/media/platform/rockchip/rkcif/rkcif-dev.c +0 -8
index be3a174b9aab..dceabee2ecd6 100644
@@ -10,7 +10,6 @@
  */
 
 #include <linux/clk.h>
-#include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
@@ -18,7 +17,6 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
-#include <linux/reset.h>
 
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-mc.h>
@@ -201,10 +199,6 @@ static int rkcif_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "failed to get clocks\n");
 
-	rkcif->reset = devm_reset_control_array_get_exclusive(dev);
-	if (IS_ERR(rkcif->reset))
-		return PTR_ERR(rkcif->reset);
-
 	rkcif->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
 						     "rockchip,grf");
 	if (IS_ERR(rkcif->grf))
@@ -273,15 +267,6 @@ static int rkcif_runtime_suspend(struct device *dev)
 {
 	struct rkcif_device *rkcif = dev_get_drvdata(dev);
 
-	/*
-	 * Reset CIF (CRU, DMA, FIFOs) to allow a clean resume.

drivers/media/platform/rockchip/rkcif/rkcif-dev.c static int rkcif_runtime_suspend(struct device *dev) · line 272

Quoted 10 lines
drivers/media/platform/rockchip/rkcif/rkcif-dev.c +0 -7
-	 * Since this resets the IOMMU too, we cannot issue this reset when
-	 * resuming.
-	 */
-	reset_control_assert(rkcif->reset);
-	udelay(5);
-	reset_control_deassert(rkcif->reset);
-
 	clk_bulk_disable_unprepare(rkcif->clks_num, rkcif->clks);
 
 	return 0;

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

author maintainer
#3 lore raw

Re: [PATCH v1 1/2] media: rockchip: rkcif: Don't reset the hardware in runtime suspend

Hi Michael,

Quoted 9 lines · Michael Riesch

Hi Mehdi,

Quoted 6 lines · Mehdi Djait

The reset in the runtime suspend also resets the VICAP IOMMU.

When the IOMMU driver accesses the IOMMU registers inside the delay window between the assert and deassert, e.g. in rk_iommu_zap_lines() to shootdown IOTLB entries, the system hangs.

Quoted 7 lines
Quoted 1 line

Drop the reset and the now unused reset control.

I agree that the current situation is nothing more than a crude hack that happened to work in most cases. But I doubt simply removing the reset is the correct solution. Recently, I saw some downstream patch that makes the reset a shared one, which seems like the right thing to do here. What would you think about that?

Do you have a link for these patches ?

I also agree that this patch will not be the solution that will go in but the situation needs to change.

The VICAP_GLB_CTRL register does expose this:
reset the VICAP(except MMU/AXI MASTER/REG FILE)

The Rockchip-bsp kernel exports the disable and enable functions of the iommu driver and calls them in the rkcif driver, see:
https://github.com/rockchip-linux/kernel/blob/develop-6.1/drivers/media/platform/rockchip/cif/capture.c#L7269

--
Kind regards
Mehdi Djait

author maintainer
#4 lore raw

Re: [PATCH v1 1/2] media: rockchip: rkcif: Don't reset the hardware in runtime suspend

Quoted 25 lines · Mehdi Djait

Hi Michael,

Quoted 9 lines · Michael Riesch

Hi Mehdi,

Quoted 6 lines · Mehdi Djait

The reset in the runtime suspend also resets the VICAP IOMMU.

When the IOMMU driver accesses the IOMMU registers inside the delay window between the assert and deassert, e.g. in rk_iommu_zap_lines() to shootdown IOTLB entries, the system hangs.

Quoted 7 lines
Quoted 1 line

Drop the reset and the now unused reset control.

I agree that the current situation is nothing more than a crude hack that happened to work in most cases. But I doubt simply removing the reset is the correct solution. Recently, I saw some downstream patch that makes the reset a shared one, which seems like the right thing to do here. What would you think about that?

Do you have a link for these patches ?

I also agree that this patch will not be the solution that will go in but the situation needs to change.

Quoted 7 lines

The VICAP_GLB_CTRL register does expose this:
reset the VICAP(except MMU/AXI MASTER/REG FILE)

The Rockchip-bsp kernel exports the disable and enable functions of the iommu driver and calls them in the rkcif driver, see:
https://github.com/rockchip-linux/kernel/blob/develop-6.1/drivers/media/platform/rockchip/cif/capture.c#L7269

I mean, I of course don't think this is a solution to be considered, but this is what I found for now..

--
Kind regards
Mehdi Djait

maintainer
#5 lore raw

Re: [PATCH v1 1/2] media: rockchip: rkcif: Don't reset the hardware in runtime suspend

Hi Mehdi,

Quoted 15 lines · Mehdi Djait
Quoted 14 lines · Mehdi Djait

Hi Michael,

Quoted 11 lines · Michael Riesch

Hi Mehdi,

Quoted 7 lines · Mehdi Djait

The reset in the runtime suspend also resets the VICAP IOMMU.

When the IOMMU driver accesses the IOMMU registers inside the delay window between the assert and deassert, e.g. in rk_iommu_zap_lines() to shootdown IOTLB entries, the system hangs.

Drop the reset and the now unused reset control.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

I agree that the current situation is nothing more than a crude hack that happened to work in most cases. But I doubt simply removing the reset is the correct solution. Recently, I saw some downstream patch that makes the reset a shared one, which seems like the right thing to do here. What would you think about that?

Do you have a link for these patches ?

Unfortunately they are not yet public, but I presume I have to address that issue anyway soon. May I ask you for a bit of patience?

Quoted 6 lines
Quoted 6 lines

I also agree that this patch will not be the solution that will go in but the situation needs to change.

The VICAP_GLB_CTRL register does expose this:
reset the VICAP(except MMU/AXI MASTER/REG FILE)

Quoted 7 lines
Quoted 4 lines

The Rockchip-bsp kernel exports the disable and enable functions of the iommu driver and calls them in the rkcif driver, see:
https://github.com/rockchip-linux/kernel/blob/develop-6.1/drivers/media/platform/rockchip/cif/capture.c#L7269

I mean, I of course don't think this is a solution to be considered, but this is what I found for now..

Yeah I wouldn't really like to go in *that* direction unless it is absolutely necessary...

Best regards,
Michael

Quoted 4 lines

--
Kind regards
Mehdi Djait

author maintainer
#6 lore raw

PATCH 2/2 media: rockchip: rkcif: Validate the pixel format against the media bus code

In reply to Mehdi Djait

The link validation only checks the frame size, so a pixel format that does not match the media bus code on the source pad is accepted.

Reject such pipelines with -EPIPE.

---
 drivers/media/platform/rockchip/rkcif/rkcif-stream.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +12 -0
index 3130d420ad55..66726f654674 100644
@@ -460,6 +460,7 @@ static int rkcif_stream_link_validate(struct media_link *link)
 {
 	struct video_device *vdev =
 		media_entity_to_video_device(link->sink->entity);
+	const struct rkcif_output_fmt *fmt;
 	struct v4l2_mbus_framefmt *source_fmt;
 	struct v4l2_subdev *sd;
 	struct v4l2_subdev_state *state;
@@ -489,6 +490,17 @@ static int rkcif_stream_link_validate(struct media_link *link)
 		goto out;
 	}
 
+	fmt = rkcif_stream_find_output_fmt(stream, false,
+					   stream->pix.pixelformat);
+	if (!fmt || (fmt->mbus_code && fmt->mbus_code != source_fmt->code)) {
+		dev_dbg(stream->rkcif->dev,
+			"link '%s':%u -> '%s':%u not valid: Format mismatch\n",
+			link->source->entity->name, link->source->index,
+			link->sink->entity->name, link->sink->index);
+		ret = -EPIPE;
+		goto out;
+	}
+
 	ret = 0;
 
 out:
Signature · 1 line
2.56.0
maintainer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH v1 2/2] media: rockchip: rkcif: Validate the pixel format against the media bus code

Hi Mehdi,

Thanks for the patch!

I think I had a check like this in the source code once, but kicked it out because it broke one of my valid pipelines. Let's take the first entry of mipi_out_fmts for instance: the capture block can write V4L2_PIX_FMT_YUYV image data to memory. However, I *think* it can not only accept MEDIA_BUS_FMT_YUYV8_1X16 as input format for that, but other mbus formats too. Thus, if I am not mistaken, we need to rework that at some point.

Cc Gerald who might have a pipeline that will be broken by this patch...
Quoted 6 lines · Mehdi Djait

The link validation only checks the frame size, so a pixel format that does not match the media bus code on the source pad is accepted.

Reject such pipelines with -EPIPE.

...but all the above said, the patch as such is valid, but I think the definition of possible formats needs a rework. I'll leave my

here just in case.

Best regards,
Michael

Quoted 24 lines
---
 drivers/media/platform/rockchip/rkcif/rkcif-stream.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +5 -0
index 3130d420ad55..66726f654674 100644
@@ -460,6 +460,7 @@ static int rkcif_stream_link_validate(struct media_link *link)
 {
 	struct video_device *vdev =
 		media_entity_to_video_device(link->sink->entity);
+	const struct rkcif_output_fmt *fmt;
 	struct v4l2_mbus_framefmt *source_fmt;
 	struct v4l2_subdev *sd;
 	struct v4l2_subdev_state *state;
@@ -489,6 +490,17 @@ static int rkcif_stream_link_validate(struct media_link *link)
 		goto out;
 	}
 
+	fmt = rkcif_stream_find_output_fmt(stream, false,
+					   stream->pix.pixelformat);
+	if (!fmt || (fmt->mbus_code && fmt->mbus_code != source_fmt->code)) {
+		dev_dbg(stream->rkcif->dev,

drivers/media/platform/rockchip/rkcif/rkcif-stream.c static int rkcif_stream_link_validate(struct media_link *link) · line 506

Quoted 10 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +7 -0
+			"link '%s':%u -> '%s':%u not valid: Format mismatch\n",
+			link->source->entity->name, link->source->index,
+			link->sink->entity->name, link->sink->index);
+		ret = -EPIPE;
+		goto out;
+	}
+
 	ret = 0;
 
 out:

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip