1 From c0863c6da514311d3db381bcc1be6e0435d9b8c9 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Fri, 5 Mar 2021 15:40:45 +0000
4 Subject: [PATCH] media: bcm2835-unicam: Fix bug in buffer swapping
7 If multiple sets of interrupts occur simultaneously, it may be unsafe
8 to swap buffers, as the hardware may already be re-using the current
9 buffers. In such cases, avoid swapping buffers, and wait for the next
10 opportunity at the Frame End interrupt to signal completion.
12 Additionally, check the packet compare status when watching for frame
13 end for buffers swaps, as this could also signify a frame end event.
15 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 .../media/platform/bcm2835/bcm2835-unicam.c | 21 ++++++++++++++++---
18 1 file changed, 18 insertions(+), 3 deletions(-)
20 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
21 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
22 @@ -800,6 +800,7 @@ static irqreturn_t unicam_isr(int irq, v
23 unsigned int sequence = unicam->sequence;
29 sta = reg_read(unicam, UNICAM_STA);
30 @@ -817,12 +818,18 @@ static irqreturn_t unicam_isr(int irq, v
34 + * Look for either the Frame End interrupt or the Packet Capture status
35 + * to signal a frame end.
37 + fe = (ista & UNICAM_FEI || sta & UNICAM_PI0);
40 * We must run the frame end handler first. If we have a valid next_frm
41 * and we get a simultaneout FE + FS interrupt, running the FS handler
42 * first would null out the next_frm ptr and we would have lost the
45 - if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
48 * Ensure we have swapped buffers already as we can't
49 * stop the peripheral. If no buffer is available, use a
50 @@ -833,7 +840,15 @@ static irqreturn_t unicam_isr(int irq, v
51 if (!unicam->node[i].streaming)
54 - if (unicam->node[i].cur_frm)
56 + * If cur_frm == next_frm, it means we have not had
57 + * a chance to swap buffers, likely due to having
58 + * multiple interrupts occurring simultaneously (like FE
59 + * + FS + LS). In this case, we cannot signal the buffer
60 + * as complete, as the HW will reuse that buffer.
62 + if (unicam->node[i].cur_frm &&
63 + unicam->node[i].cur_frm != unicam->node[i].next_frm)
64 unicam_process_buffer_complete(&unicam->node[i],
66 unicam->node[i].cur_frm = unicam->node[i].next_frm;
67 @@ -870,7 +885,7 @@ static irqreturn_t unicam_isr(int irq, v
68 * where the HW does not actually swap it if the new frame has
71 - if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
72 + if (ista & (UNICAM_FSI | UNICAM_LCI) && !fe) {
73 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
74 if (!unicam->node[i].streaming)