bcm27xx: update 6.1 patches to latest version
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-6.1 / 950-1229-drivers-media-cfe-Add-more-robust-ISR-handlers.patch
1 From 6fb7a0b4c1dd6cf5b12ec2b2c197dcf8e58cd2b9 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Mon, 18 Dec 2023 09:52:45 +0000
4 Subject: [PATCH] drivers: media: cfe: Add more robust ISR handlers
5
6 Update the ISR logic to be more robust to sensors in problematic states
7 where interrupts may start arriving overlapped and/or missing.
8
9 1) Test for cur_frame in the FE handler, and if present, dequeue it in
10 an error state so that it does not get orphaned.
11
12 2) Move the sequence counter and timestamp variables to the node
13 structures. This allows the ISR to track channels running ahead when
14 interrupts arrive unordered.
15
16 3) Add a test to ensure we don't have a spurios (but harmlesS) call to
17 the FE handler in some circumstances.
18
19 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
20 ---
21 .../media/platform/raspberrypi/rp1_cfe/cfe.c | 92 ++++++++++---------
22 1 file changed, 49 insertions(+), 43 deletions(-)
23
24 --- a/drivers/media/platform/raspberrypi/rp1_cfe/cfe.c
25 +++ b/drivers/media/platform/raspberrypi/rp1_cfe/cfe.c
26 @@ -272,6 +272,8 @@ struct cfe_node {
27 /* Pointer to the parent handle */
28 struct cfe_device *cfe;
29 struct media_pad pad;
30 + unsigned int fs_count;
31 + u64 ts;
32 };
33
34 struct cfe_device {
35 @@ -311,9 +313,6 @@ struct cfe_device {
36 struct pisp_fe_device fe;
37
38 int fe_csi2_channel;
39 -
40 - unsigned int sequence;
41 - u64 ts;
42 };
43
44 static inline bool is_fe_enabled(struct cfe_device *cfe)
45 @@ -393,17 +392,6 @@ static bool test_all_nodes(struct cfe_de
46 return true;
47 }
48
49 -static void clear_all_nodes(struct cfe_device *cfe, unsigned long precond,
50 - unsigned long state)
51 -{
52 - unsigned int i;
53 -
54 - for (i = 0; i < NUM_NODES; i++) {
55 - if (check_state(cfe, precond, i))
56 - clear_state(cfe, state, i);
57 - }
58 -}
59 -
60 static int mipi_cfg_regs_show(struct seq_file *s, void *data)
61 {
62 struct cfe_device *cfe = s->private;
63 @@ -656,22 +644,22 @@ static void cfe_prepare_next_job(struct
64 }
65
66 static void cfe_process_buffer_complete(struct cfe_node *node,
67 - unsigned int sequence)
68 + enum vb2_buffer_state state)
69 {
70 struct cfe_device *cfe = node->cfe;
71
72 cfe_dbg_verbose("%s: [%s] buffer:%p\n", __func__,
73 node_desc[node->id].name, &node->cur_frm->vb.vb2_buf);
74
75 - node->cur_frm->vb.sequence = sequence;
76 - vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
77 + node->cur_frm->vb.sequence = node->fs_count - 1;
78 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf, state);
79 }
80
81 static void cfe_queue_event_sof(struct cfe_node *node)
82 {
83 struct v4l2_event event = {
84 .type = V4L2_EVENT_FRAME_SYNC,
85 - .u.frame_sync.frame_sequence = node->cfe->sequence,
86 + .u.frame_sync.frame_sequence = node->fs_count - 1,
87 };
88
89 v4l2_event_queue(&node->video_dev, &event);
90 @@ -680,28 +668,53 @@ static void cfe_queue_event_sof(struct c
91 static void cfe_sof_isr_handler(struct cfe_node *node)
92 {
93 struct cfe_device *cfe = node->cfe;
94 + bool matching_fs = true;
95 + unsigned int i;
96
97 cfe_dbg_verbose("%s: [%s] seq %u\n", __func__, node_desc[node->id].name,
98 - cfe->sequence);
99 -
100 - node->cur_frm = node->next_frm;
101 - node->next_frm = NULL;
102 + node->fs_count);
103
104 /*
105 - * If this is the first node to see a frame start, sample the
106 - * timestamp to use for all frames across all channels.
107 + * If the sensor is producing unexpected frame event ordering over a
108 + * sustained period of time, guard against the possibility of coming
109 + * here and orphaning the cur_frm if it's not been dequeued already.
110 + * Unfortunately, there is not enough hardware state to tell if this
111 + * may have occurred.
112 */
113 - if (!test_any_node(cfe, NODE_STREAMING | FS_INT))
114 - cfe->ts = ktime_get_ns();
115 + if (WARN(node->cur_frm, "%s: [%s] Orphanded frame at seq %u\n",
116 + __func__, node_desc[node->id].name, node->fs_count))
117 + cfe_process_buffer_complete(node, VB2_BUF_STATE_ERROR);
118
119 - set_state(cfe, FS_INT, node->id);
120 + node->cur_frm = node->next_frm;
121 + node->next_frm = NULL;
122 + node->fs_count++;
123
124 - /* If all nodes have seen a frame start, we can queue another job. */
125 - if (test_all_nodes(cfe, NODE_STREAMING, FS_INT))
126 + node->ts = ktime_get_ns();
127 + for (i = 0; i < NUM_NODES; i++) {
128 + if (!check_state(cfe, NODE_STREAMING, i) || i == node->id)
129 + continue;
130 + /*
131 + * This checks if any other node has seen a FS. If yes, use the
132 + * same timestamp, eventually across all node buffers.
133 + */
134 + if (cfe->node[i].fs_count >= node->fs_count)
135 + node->ts = cfe->node[i].ts;
136 + /*
137 + * This checks if all other node have seen a matching FS. If
138 + * yes, we can flag another job to be queued.
139 + */
140 + if (matching_fs && cfe->node[i].fs_count != node->fs_count)
141 + matching_fs = false;
142 + }
143 +
144 + if (matching_fs)
145 cfe->job_queued = false;
146
147 if (node->cur_frm)
148 - node->cur_frm->vb.vb2_buf.timestamp = cfe->ts;
149 + node->cur_frm->vb.vb2_buf.timestamp = node->ts;
150 +
151 + set_state(cfe, FS_INT, node->id);
152 + clear_state(cfe, FE_INT, node->id);
153
154 if (is_image_output_node(node))
155 cfe_queue_event_sof(node);
156 @@ -712,22 +725,14 @@ static void cfe_eof_isr_handler(struct c
157 struct cfe_device *cfe = node->cfe;
158
159 cfe_dbg_verbose("%s: [%s] seq %u\n", __func__, node_desc[node->id].name,
160 - cfe->sequence);
161 + node->fs_count - 1);
162
163 if (node->cur_frm)
164 - cfe_process_buffer_complete(node, cfe->sequence);
165 + cfe_process_buffer_complete(node, VB2_BUF_STATE_DONE);
166
167 node->cur_frm = NULL;
168 set_state(cfe, FE_INT, node->id);
169 -
170 - /*
171 - * If all nodes have seen a frame end, we can increment
172 - * the sequence counter now.
173 - */
174 - if (test_all_nodes(cfe, NODE_STREAMING, FE_INT)) {
175 - cfe->sequence++;
176 - clear_all_nodes(cfe, NODE_STREAMING, FE_INT | FS_INT);
177 - }
178 + clear_state(cfe, FS_INT, node->id);
179 }
180
181 static irqreturn_t cfe_isr(int irq, void *dev)
182 @@ -794,7 +799,8 @@ static irqreturn_t cfe_isr(int irq, void
183 * frame first before the FS handler for the current
184 * frame.
185 */
186 - if (check_state(cfe, FS_INT, node->id)) {
187 + if (check_state(cfe, FS_INT, node->id) &&
188 + !check_state(cfe, FE_INT, node->id)) {
189 cfe_dbg("%s: [%s] Handling missing previous FE interrupt\n",
190 __func__, node_desc[node->id].name);
191 cfe_eof_isr_handler(node);
192 @@ -1131,6 +1137,7 @@ static int cfe_start_streaming(struct vb
193
194 clear_state(cfe, FS_INT | FE_INT, node->id);
195 set_state(cfe, NODE_STREAMING, node->id);
196 + node->fs_count = 0;
197 cfe_start_channel(node);
198
199 if (!test_all_nodes(cfe, NODE_ENABLED, NODE_STREAMING)) {
200 @@ -1166,7 +1173,6 @@ static int cfe_start_streaming(struct vb
201 csi2_open_rx(&cfe->csi2);
202
203 cfe_dbg("Starting sensor streaming\n");
204 - cfe->sequence = 0;
205 ret = v4l2_subdev_call(cfe->sensor, video, s_stream, 1);
206 if (ret < 0) {
207 cfe_err("stream on failed in subdev\n");