linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements
@ 2024-11-27 11:15 Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 1/4] drivers: media: bcm2835-unicam: Improve frame sequence count handling Naushir Patuck
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Naushir Patuck @ 2024-11-27 11:15 UTC (permalink / raw)
  To: Raspberry Pi Kernel Maintenance, Mauro Carvalho Chehab,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden
  Cc: linux-media, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	jacopo.mondi, Dave Stevenson, Naushir Patuck

Hi,

Version 2 of this series has no changes, except patch 5/5 from v1 has been
removed while I rework the logic donwstream first.

Thanks,
Naush

Naushir Patuck (4):
  drivers: media: bcm2835-unicam: Improve frame sequence count handling
  drivers: media: bcm2835-unicam: Allow setting of unpacked formats
  drivers: media: bcm2835-unicam: Disable trigger mode operation
  drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun

 .../media/platform/broadcom/bcm2835-unicam.c  | 42 ++++++++++++++-----
 1 file changed, 31 insertions(+), 11 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/4] drivers: media: bcm2835-unicam: Improve frame sequence count handling
  2024-11-27 11:15 [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements Naushir Patuck
@ 2024-11-27 11:15 ` Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 2/4] drivers: media: bcm2835-unicam: Allow setting of unpacked formats Naushir Patuck
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Naushir Patuck @ 2024-11-27 11:15 UTC (permalink / raw)
  To: Raspberry Pi Kernel Maintenance, Mauro Carvalho Chehab,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden
  Cc: linux-media, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	jacopo.mondi, Dave Stevenson, Naushir Patuck

Ensure that the frame sequence counter is incremented only if a previous
frame start interrupt has occurred, or a frame start + frame end has
occurred simultaneously.

This corresponds the sequence number with the actual number of frames
produced by the sensor, not the number of frame buffers dequeued back
to userland.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 .../media/platform/broadcom/bcm2835-unicam.c  | 22 +++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/broadcom/bcm2835-unicam.c b/drivers/media/platform/broadcom/bcm2835-unicam.c
index 3aed0e493c81..36fb186a0421 100644
--- a/drivers/media/platform/broadcom/bcm2835-unicam.c
+++ b/drivers/media/platform/broadcom/bcm2835-unicam.c
@@ -199,6 +199,7 @@ struct unicam_device {
 	/* subdevice async notifier */
 	struct v4l2_async_notifier notifier;
 	unsigned int sequence;
+	bool frame_started;
 
 	/* Sensor node */
 	struct {
@@ -742,6 +743,8 @@ static irqreturn_t unicam_isr(int irq, void *dev)
 	 * buffer forever.
 	 */
 	if (fe) {
+		bool inc_seq = unicam->frame_started;
+
 		/*
 		 * Ensure we have swapped buffers already as we can't
 		 * stop the peripheral. If no buffer is available, use a
@@ -761,11 +764,24 @@ static irqreturn_t unicam_isr(int irq, void *dev)
 			 * + FS + LS). In this case, we cannot signal the buffer
 			 * as complete, as the HW will reuse that buffer.
 			 */
-			if (node->cur_frm && node->cur_frm != node->next_frm)
+			if (node->cur_frm && node->cur_frm != node->next_frm) {
 				unicam_process_buffer_complete(node, sequence);
+				inc_seq = true;
+			}
 			node->cur_frm = node->next_frm;
 		}
-		unicam->sequence++;
+
+		/*
+		 * Increment the sequence number conditionally on either a FS
+		 * having already occurred, or in the FE + FS condition as
+		 * caught in the FE handler above. This ensures the sequence
+		 * number corresponds to the frames generated by the sensor, not
+		 * the frames dequeued to userland.
+		 */
+		if (inc_seq) {
+			unicam->sequence++;
+			unicam->frame_started = false;
+		}
 	}
 
 	if (ista & UNICAM_FSI) {
@@ -795,6 +811,7 @@ static irqreturn_t unicam_isr(int irq, void *dev)
 		}
 
 		unicam_queue_event_sof(unicam);
+		unicam->frame_started = true;
 	}
 
 	/*
@@ -1413,6 +1430,7 @@ static int unicam_sd_enable_streams(struct v4l2_subdev *sd,
 		if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE))
 			unicam_start_metadata(unicam);
 
+		unicam->frame_started = false;
 		unicam_start_rx(unicam, state);
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] drivers: media: bcm2835-unicam: Allow setting of unpacked formats
  2024-11-27 11:15 [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 1/4] drivers: media: bcm2835-unicam: Improve frame sequence count handling Naushir Patuck
@ 2024-11-27 11:15 ` Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 3/4] drivers: media: bcm2835-unicam: Disable trigger mode operation Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 4/4] drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun Naushir Patuck
  3 siblings, 0 replies; 5+ messages in thread
From: Naushir Patuck @ 2024-11-27 11:15 UTC (permalink / raw)
  To: Raspberry Pi Kernel Maintenance, Mauro Carvalho Chehab,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden
  Cc: linux-media, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	jacopo.mondi, Dave Stevenson, Naushir Patuck

When matching formats via try_fmt/set_fmt ioctls, test for the unpacked
formats as well as packed formats. This allows userland clients setup
unpacking to 16-bits from the 10/12/14-packed CSI2 formats.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 drivers/media/platform/broadcom/bcm2835-unicam.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/broadcom/bcm2835-unicam.c b/drivers/media/platform/broadcom/bcm2835-unicam.c
index 36fb186a0421..d573d4d89881 100644
--- a/drivers/media/platform/broadcom/bcm2835-unicam.c
+++ b/drivers/media/platform/broadcom/bcm2835-unicam.c
@@ -547,7 +547,8 @@ unicam_find_format_by_fourcc(u32 fourcc, u32 pad)
 	}
 
 	for (i = 0; i < num_formats; ++i) {
-		if (formats[i].fourcc == fourcc)
+		if (formats[i].fourcc == fourcc ||
+		    formats[i].unpacked_fourcc == fourcc)
 			return &formats[i];
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] drivers: media: bcm2835-unicam: Disable trigger mode operation
  2024-11-27 11:15 [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 1/4] drivers: media: bcm2835-unicam: Improve frame sequence count handling Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 2/4] drivers: media: bcm2835-unicam: Allow setting of unpacked formats Naushir Patuck
@ 2024-11-27 11:15 ` Naushir Patuck
  2024-11-27 11:15 ` [PATCH v2 4/4] drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun Naushir Patuck
  3 siblings, 0 replies; 5+ messages in thread
From: Naushir Patuck @ 2024-11-27 11:15 UTC (permalink / raw)
  To: Raspberry Pi Kernel Maintenance, Mauro Carvalho Chehab,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden
  Cc: linux-media, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	jacopo.mondi, Dave Stevenson, Naushir Patuck

The imx219/imx708 sensors frequently generate a single corrupt frame
(image or embedded data) when the sensor first starts. This can either
be a missing line, or invalid samples within the line. This only occurrs
using the upstream Unicam kernel driver.

Disabling trigger mode elimiates this corruption. Since trigger mode is
a legacy feature copied from the firmware driver and not expected to be
needed, remove it. Tested on the Raspberry Pi cameras and shows no ill
effects.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 drivers/media/platform/broadcom/bcm2835-unicam.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/drivers/media/platform/broadcom/bcm2835-unicam.c b/drivers/media/platform/broadcom/bcm2835-unicam.c
index d573d4d89881..550eb1b064f1 100644
--- a/drivers/media/platform/broadcom/bcm2835-unicam.c
+++ b/drivers/media/platform/broadcom/bcm2835-unicam.c
@@ -834,11 +834,6 @@ static irqreturn_t unicam_isr(int irq, void *dev)
 		}
 	}
 
-	if (unicam_reg_read(unicam, UNICAM_ICTL) & UNICAM_FCM) {
-		/* Switch out of trigger mode if selected */
-		unicam_reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_TFC);
-		unicam_reg_write_field(unicam, UNICAM_ICTL, 0, UNICAM_FCM);
-	}
 	return IRQ_HANDLED;
 }
 
@@ -1002,8 +997,7 @@ static void unicam_start_rx(struct unicam_device *unicam,
 
 	unicam_reg_write_field(unicam, UNICAM_ANA, 0, UNICAM_DDL);
 
-	/* Always start in trigger frame capture mode (UNICAM_FCM set) */
-	val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM | UNICAM_IBOB;
+	val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_IBOB;
 	line_int_freq = max(fmt->height >> 2, 128);
 	unicam_set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
 	unicam_reg_write(unicam, UNICAM_ICTL, val);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun
  2024-11-27 11:15 [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements Naushir Patuck
                   ` (2 preceding siblings ...)
  2024-11-27 11:15 ` [PATCH v2 3/4] drivers: media: bcm2835-unicam: Disable trigger mode operation Naushir Patuck
@ 2024-11-27 11:15 ` Naushir Patuck
  3 siblings, 0 replies; 5+ messages in thread
From: Naushir Patuck @ 2024-11-27 11:15 UTC (permalink / raw)
  To: Raspberry Pi Kernel Maintenance, Mauro Carvalho Chehab,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden
  Cc: linux-media, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	jacopo.mondi, Dave Stevenson, Naushir Patuck

The Unicam hardware has been observed to cause a buffer overrun when
using the dummy buffer as a circular buffer. The conditions that cause
the overrun are not fully known, but it seems to occur when the memory
bus is heavily loaded.

To avoid the overrun, program the hardware with a buffer size of 0 when
using the dummy buffer. This will cause overrun into the allocated dummy
buffer, but avoid out of bounds writes.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 drivers/media/platform/broadcom/bcm2835-unicam.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/broadcom/bcm2835-unicam.c b/drivers/media/platform/broadcom/bcm2835-unicam.c
index 550eb1b064f1..f10064107d54 100644
--- a/drivers/media/platform/broadcom/bcm2835-unicam.c
+++ b/drivers/media/platform/broadcom/bcm2835-unicam.c
@@ -640,7 +640,14 @@ static inline void unicam_reg_write_field(struct unicam_device *unicam, u32 offs
 static void unicam_wr_dma_addr(struct unicam_node *node,
 			       struct unicam_buffer *buf)
 {
-	dma_addr_t endaddr = buf->dma_addr + buf->size;
+	/*
+	 * Due to a HW bug causing buffer overruns in circular buffer mode under
+	 * certain (not yet fully known) conditions, the dummy buffer allocation
+	 * is set to a a single page size, but the hardware gets programmed with
+	 * a buffer size of 0.
+	 */
+	dma_addr_t endaddr = buf->dma_addr +
+			     (buf != &node->dummy_buf ? buf->size : 0);
 
 	if (node->id == UNICAM_IMAGE_NODE) {
 		unicam_reg_write(node->dev, UNICAM_IBSA0, buf->dma_addr);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-11-27 11:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-27 11:15 [PATCH v2 0/4] media: bcm2835-unicam: Upstreaming various improvements Naushir Patuck
2024-11-27 11:15 ` [PATCH v2 1/4] drivers: media: bcm2835-unicam: Improve frame sequence count handling Naushir Patuck
2024-11-27 11:15 ` [PATCH v2 2/4] drivers: media: bcm2835-unicam: Allow setting of unpacked formats Naushir Patuck
2024-11-27 11:15 ` [PATCH v2 3/4] drivers: media: bcm2835-unicam: Disable trigger mode operation Naushir Patuck
2024-11-27 11:15 ` [PATCH v2 4/4] drivers: media: bcm2835-unicam: Fix for possible dummy buffer overrun Naushir Patuck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).