xref: /linux/drivers/media/pci/intel/ipu6/ipu6-isys-queue.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013--2024 Intel Corporation
4  */
5 #include <linux/atomic.h>
6 #include <linux/cleanup.h>
7 #include <linux/bug.h>
8 #include <linux/device.h>
9 #include <linux/list.h>
10 #include <linux/lockdep.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/types.h>
14 
15 #include <media/media-entity.h>
16 #include <media/v4l2-subdev.h>
17 #include <media/videobuf2-dma-sg.h>
18 #include <media/videobuf2-v4l2.h>
19 
20 #include "ipu6-bus.h"
21 #include "ipu6-dma.h"
22 #include "ipu6-fw-isys.h"
23 #include "ipu6-isys.h"
24 #include "ipu6-isys-video.h"
25 
26 static int ipu6_isys_buf_init(struct vb2_buffer *vb)
27 {
28 	struct ipu6_isys *isys = vb2_get_drv_priv(vb->vb2_queue);
29 	struct sg_table *sg = vb2_dma_sg_plane_desc(vb, 0);
30 	struct vb2_v4l2_buffer *vvb = to_vb2_v4l2_buffer(vb);
31 	struct ipu6_isys_video_buffer *ivb =
32 		vb2_buffer_to_ipu6_isys_video_buffer(vvb);
33 	int ret;
34 
35 	ret = ipu6_dma_map_sgtable(isys->adev, sg, DMA_TO_DEVICE, 0);
36 	if (ret)
37 		return ret;
38 
39 	ivb->dma_addr = sg_dma_address(sg->sgl);
40 
41 	return 0;
42 }
43 
44 static void ipu6_isys_buf_cleanup(struct vb2_buffer *vb)
45 {
46 	struct ipu6_isys *isys = vb2_get_drv_priv(vb->vb2_queue);
47 	struct sg_table *sg = vb2_dma_sg_plane_desc(vb, 0);
48 	struct vb2_v4l2_buffer *vvb = to_vb2_v4l2_buffer(vb);
49 	struct ipu6_isys_video_buffer *ivb =
50 		vb2_buffer_to_ipu6_isys_video_buffer(vvb);
51 
52 	ivb->dma_addr = 0;
53 	ipu6_dma_unmap_sgtable(isys->adev, sg, DMA_TO_DEVICE, 0);
54 }
55 
56 static int ipu6_isys_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
57 				 unsigned int *num_planes, unsigned int sizes[],
58 				 struct device *alloc_devs[])
59 {
60 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(q);
61 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
62 	struct device *dev = &av->isys->adev->auxdev.dev;
63 	u32 size = ipu6_isys_get_data_size(av);
64 
65 	/* num_planes == 0: we're being called through VIDIOC_REQBUFS */
66 	if (!*num_planes) {
67 		sizes[0] = size;
68 	} else if (sizes[0] < size) {
69 		dev_dbg(dev, "%s: queue setup: size %u < %u\n",
70 			av->vdev.name, sizes[0], size);
71 		return -EINVAL;
72 	}
73 
74 	*num_planes = 1;
75 
76 	return 0;
77 }
78 
79 static int ipu6_isys_buf_prepare(struct vb2_buffer *vb)
80 {
81 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(vb->vb2_queue);
82 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
83 	struct device *dev = &av->isys->adev->auxdev.dev;
84 	u32 bytesperline = ipu6_isys_get_bytes_per_line(av);
85 	u32 height = ipu6_isys_get_frame_height(av);
86 	u32 size = ipu6_isys_get_data_size(av);
87 
88 	dev_dbg(dev, "buffer: %s: configured size %u, buffer size %lu\n",
89 		av->vdev.name, size, vb2_plane_size(vb, 0));
90 
91 	if (size > vb2_plane_size(vb, 0))
92 		return -EINVAL;
93 
94 	vb2_set_plane_payload(vb, 0, bytesperline * height);
95 
96 	return 0;
97 }
98 
99 /*
100  * Queue a buffer list back to incoming or active queues. The buffers
101  * are removed from the buffer list.
102  */
103 void ipu6_isys_buffer_list_queue(struct ipu6_isys_buffer_list *bl,
104 				 unsigned long op_flags,
105 				 enum vb2_buffer_state state)
106 {
107 	struct ipu6_isys_buffer *ib, *ib_safe;
108 	unsigned long flags;
109 	bool first = true;
110 
111 	if (!bl)
112 		return;
113 
114 	WARN_ON_ONCE(!bl->nbufs);
115 	WARN_ON_ONCE(op_flags & IPU6_ISYS_BUFFER_LIST_FL_ACTIVE &&
116 		     op_flags & IPU6_ISYS_BUFFER_LIST_FL_INCOMING);
117 
118 	list_for_each_entry_safe(ib, ib_safe, &bl->head, head) {
119 		struct ipu6_isys_video *av;
120 		struct vb2_buffer *vb = ipu6_isys_buffer_to_vb2_buffer(ib);
121 		struct ipu6_isys_queue *aq =
122 			vb2_queue_to_isys_queue(vb->vb2_queue);
123 		struct device *dev;
124 
125 		av = ipu6_isys_queue_to_video(aq);
126 		dev = &av->isys->adev->auxdev.dev;
127 		spin_lock_irqsave(&aq->lock, flags);
128 		list_del(&ib->head);
129 		if (op_flags & IPU6_ISYS_BUFFER_LIST_FL_ACTIVE)
130 			list_add(&ib->head, &aq->active);
131 		else if (op_flags & IPU6_ISYS_BUFFER_LIST_FL_INCOMING)
132 			list_add_tail(&ib->head, &aq->incoming);
133 		spin_unlock_irqrestore(&aq->lock, flags);
134 
135 		if (first) {
136 			dev_dbg(dev,
137 				"queue buf list %p flags %lx, s %d, %d bufs\n",
138 				bl, op_flags, state, bl->nbufs);
139 			first = false;
140 		}
141 
142 		bl->nbufs--;
143 	}
144 
145 	WARN_ON(bl->nbufs);
146 }
147 
148 /*
149  * flush_firmware_streamon_fail() - Flush in cases where requests may
150  * have been queued to firmware and the *firmware streamon fails for a
151  * reason or another.
152  */
153 static void flush_firmware_streamon_fail(struct ipu6_isys_stream *stream)
154 {
155 	struct device *dev = &stream->isys->adev->auxdev.dev;
156 	struct ipu6_isys_queue *aq;
157 	unsigned long flags;
158 
159 	lockdep_assert_held(&stream->mutex);
160 
161 	list_for_each_entry(aq, &stream->queues, node) {
162 		struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
163 		struct ipu6_isys_buffer *ib, *ib_safe;
164 
165 		spin_lock_irqsave(&aq->lock, flags);
166 		list_for_each_entry_safe(ib, ib_safe, &aq->active, head) {
167 			struct vb2_buffer *vb =
168 				ipu6_isys_buffer_to_vb2_buffer(ib);
169 
170 			list_del(&ib->head);
171 			if (av->streaming) {
172 				dev_dbg(dev,
173 					"%s: queue buffer %u back to incoming\n",
174 					av->vdev.name, vb->index);
175 				/* Queue already streaming, return to driver. */
176 				list_add(&ib->head, &aq->incoming);
177 				continue;
178 			}
179 			/* Queue not yet streaming, return to user. */
180 			dev_dbg(dev, "%s: return %u back to videobuf2\n",
181 				av->vdev.name, vb->index);
182 			vb2_buffer_done(ipu6_isys_buffer_to_vb2_buffer(ib),
183 					VB2_BUF_STATE_QUEUED);
184 		}
185 		spin_unlock_irqrestore(&aq->lock, flags);
186 	}
187 }
188 
189 /*
190  * Attempt obtaining a buffer list from the incoming queues, a list of buffers
191  * that contains one entry from each video buffer queue. If a buffer can't be
192  * obtained from every queue, the buffers are returned back to the queue.
193  */
194 static int buffer_list_get(struct ipu6_isys_stream *stream,
195 			   struct ipu6_isys_buffer_list *bl)
196 {
197 	struct device *dev = &stream->isys->adev->auxdev.dev;
198 	struct ipu6_isys_queue *aq;
199 	unsigned long flags;
200 	unsigned long buf_flag = IPU6_ISYS_BUFFER_LIST_FL_INCOMING;
201 
202 	lockdep_assert_held(&stream->mutex);
203 
204 	bl->nbufs = 0;
205 	INIT_LIST_HEAD(&bl->head);
206 
207 	list_for_each_entry(aq, &stream->queues, node) {
208 		struct ipu6_isys_buffer *ib;
209 
210 		spin_lock_irqsave(&aq->lock, flags);
211 		if (list_empty(&aq->incoming)) {
212 			spin_unlock_irqrestore(&aq->lock, flags);
213 			if (!list_empty(&bl->head))
214 				ipu6_isys_buffer_list_queue(bl, buf_flag, 0);
215 			return -ENODATA;
216 		}
217 
218 		ib = list_last_entry(&aq->incoming,
219 				     struct ipu6_isys_buffer, head);
220 
221 		dev_dbg(dev, "buffer: %s: buffer %u\n",
222 			ipu6_isys_queue_to_video(aq)->vdev.name,
223 			ipu6_isys_buffer_to_vb2_buffer(ib)->index);
224 		list_del(&ib->head);
225 		list_add(&ib->head, &bl->head);
226 		spin_unlock_irqrestore(&aq->lock, flags);
227 
228 		bl->nbufs++;
229 	}
230 
231 	dev_dbg(dev, "get buffer list %p, %u buffers\n", bl, bl->nbufs);
232 
233 	return 0;
234 }
235 
236 static void
237 ipu6_isys_buf_to_fw_frame_buf_pin(struct vb2_buffer *vb,
238 				  struct ipu6_fw_isys_frame_buff_set_abi *set)
239 {
240 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(vb->vb2_queue);
241 	struct vb2_v4l2_buffer *vvb = to_vb2_v4l2_buffer(vb);
242 	struct ipu6_isys_video_buffer *ivb =
243 		vb2_buffer_to_ipu6_isys_video_buffer(vvb);
244 
245 	set->output_pins[aq->fw_output].addr = ivb->dma_addr;
246 	set->output_pins[aq->fw_output].out_buf_id = vb->index + 1;
247 }
248 
249 /*
250  * Convert a buffer list to a isys fw ABI framebuffer set. The
251  * buffer list is not modified.
252  */
253 #define IPU6_ISYS_FRAME_NUM_THRESHOLD  (30)
254 void
255 ipu6_isys_buf_to_fw_frame_buf(struct ipu6_fw_isys_frame_buff_set_abi *set,
256 			      struct ipu6_isys_stream *stream,
257 			      struct ipu6_isys_buffer_list *bl)
258 {
259 	struct ipu6_isys_buffer *ib;
260 
261 	WARN_ON(!bl->nbufs);
262 
263 	set->send_irq_sof = 1;
264 	set->send_resp_sof = 1;
265 	set->send_irq_eof = 0;
266 	set->send_resp_eof = 0;
267 
268 	if (stream->streaming)
269 		set->send_irq_capture_ack = 0;
270 	else
271 		set->send_irq_capture_ack = 1;
272 	set->send_irq_capture_done = 0;
273 
274 	set->send_resp_capture_ack = 1;
275 	set->send_resp_capture_done = 1;
276 	if (atomic_read(&stream->sequence) >= IPU6_ISYS_FRAME_NUM_THRESHOLD) {
277 		set->send_resp_capture_ack = 0;
278 		set->send_resp_capture_done = 0;
279 	}
280 
281 	list_for_each_entry(ib, &bl->head, head) {
282 		struct vb2_buffer *vb = ipu6_isys_buffer_to_vb2_buffer(ib);
283 
284 		ipu6_isys_buf_to_fw_frame_buf_pin(vb, set);
285 	}
286 }
287 
288 /* Start streaming for real. The buffer list must be available. */
289 static int ipu6_isys_stream_start(struct ipu6_isys_video *av,
290 				  struct ipu6_isys_buffer_list *bl)
291 {
292 	struct ipu6_isys_stream *stream = av->stream;
293 	struct device *dev = &stream->isys->adev->auxdev.dev;
294 	struct ipu6_isys_buffer_list __bl;
295 	int ret;
296 
297 	guard(mutex)(&stream->isys->stream_mutex);
298 	ret = ipu6_isys_video_set_streaming(av, 1, bl);
299 	if (ret)
300 		goto out_requeue;
301 
302 	stream->streaming = 1;
303 
304 	bl = &__bl;
305 
306 	do {
307 		struct ipu6_fw_isys_frame_buff_set_abi *buf = NULL;
308 		struct isys_fw_msgs *msg;
309 		u16 send_type = IPU6_FW_ISYS_SEND_TYPE_STREAM_CAPTURE;
310 
311 		ret = buffer_list_get(stream, bl);
312 		if (ret < 0)
313 			break;
314 
315 		msg = ipu6_get_fw_msg_buf(stream);
316 		if (!msg)
317 			return -ENOMEM;
318 
319 		buf = &msg->fw_msg.frame;
320 		ipu6_isys_buf_to_fw_frame_buf(buf, stream, bl);
321 		ipu6_fw_isys_dump_frame_buff_set(dev, buf,
322 						 stream->nr_output_pins);
323 		ipu6_isys_buffer_list_queue(bl, IPU6_ISYS_BUFFER_LIST_FL_ACTIVE,
324 					    0);
325 		ret = ipu6_fw_isys_complex_cmd(stream->isys,
326 					       stream->stream_handle, buf,
327 					       msg->dma_addr, sizeof(*buf),
328 					       send_type);
329 	} while (!WARN_ON(ret));
330 
331 	return 0;
332 
333 out_requeue:
334 	if (bl && bl->nbufs)
335 		ipu6_isys_buffer_list_queue(bl,
336 					    IPU6_ISYS_BUFFER_LIST_FL_INCOMING,
337 					    VB2_BUF_STATE_QUEUED);
338 	flush_firmware_streamon_fail(stream);
339 
340 	return ret;
341 }
342 
343 static void buf_queue(struct vb2_buffer *vb)
344 {
345 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(vb->vb2_queue);
346 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
347 	struct vb2_v4l2_buffer *vvb = to_vb2_v4l2_buffer(vb);
348 	struct ipu6_isys_video_buffer *ivb =
349 		vb2_buffer_to_ipu6_isys_video_buffer(vvb);
350 	struct ipu6_isys_buffer *ib = &ivb->ib;
351 	struct device *dev = &av->isys->adev->auxdev.dev;
352 	struct ipu6_fw_isys_frame_buff_set_abi *buf = NULL;
353 	struct ipu6_isys_stream *stream = av->stream;
354 	struct ipu6_isys_buffer_list bl;
355 	struct isys_fw_msgs *msg;
356 	unsigned long flags;
357 	dma_addr_t dma;
358 	int ret;
359 
360 	dev_dbg(dev, "queue buffer %u for %s\n", vb->index, av->vdev.name);
361 
362 	dma = ivb->dma_addr;
363 	dev_dbg(dev, "iova: iova %pad\n", &dma);
364 
365 	spin_lock_irqsave(&aq->lock, flags);
366 	list_add(&ib->head, &aq->incoming);
367 	spin_unlock_irqrestore(&aq->lock, flags);
368 
369 	if (!vb2_start_streaming_called(vb->vb2_queue)) {
370 		dev_dbg(dev, "start_streaming hasn't been called yet on %s\n",
371 			av->vdev.name);
372 		return;
373 	}
374 
375 	mutex_lock(&stream->mutex);
376 
377 	if (stream->nr_streaming != stream->nr_queues) {
378 		dev_dbg(dev, "not streaming yet, adding to incoming\n");
379 		goto out;
380 	}
381 
382 	/*
383 	 * We just put one buffer to the incoming list of this queue
384 	 * (above). Let's see whether all queues in the pipeline would
385 	 * have a buffer.
386 	 */
387 	ret = buffer_list_get(stream, &bl);
388 	if (ret < 0) {
389 		dev_dbg(dev, "No buffers available\n");
390 		goto out;
391 	}
392 
393 	msg = ipu6_get_fw_msg_buf(stream);
394 	if (!msg) {
395 		ret = -ENOMEM;
396 		goto out;
397 	}
398 
399 	buf = &msg->fw_msg.frame;
400 	ipu6_isys_buf_to_fw_frame_buf(buf, stream, &bl);
401 	ipu6_fw_isys_dump_frame_buff_set(dev, buf, stream->nr_output_pins);
402 
403 	/*
404 	 * We must queue the buffers in the buffer list to the
405 	 * appropriate video buffer queues BEFORE passing them to the
406 	 * firmware since we could get a buffer event back before we
407 	 * have queued them ourselves to the active queue.
408 	 */
409 	ipu6_isys_buffer_list_queue(&bl, IPU6_ISYS_BUFFER_LIST_FL_ACTIVE, 0);
410 
411 	ret = ipu6_fw_isys_complex_cmd(stream->isys, stream->stream_handle,
412 				       buf, msg->dma_addr, sizeof(*buf),
413 				       IPU6_FW_ISYS_SEND_TYPE_STREAM_CAPTURE);
414 	if (ret < 0)
415 		dev_err(dev, "send stream capture failed\n");
416 
417 out:
418 	mutex_unlock(&stream->mutex);
419 }
420 
421 static int ipu6_isys_link_fmt_validate(struct ipu6_isys_queue *aq)
422 {
423 	struct v4l2_mbus_framefmt format, *__format;
424 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
425 	struct device *dev = &av->isys->adev->auxdev.dev;
426 	struct media_pad *remote_pad =
427 		media_pad_remote_pad_first(av->vdev.entity.pads);
428 	struct v4l2_subdev *sd;
429 	u32 r_stream, code;
430 
431 	if (!remote_pad)
432 		return -ENOTCONN;
433 
434 	sd = media_entity_to_v4l2_subdev(remote_pad->entity);
435 	r_stream = ipu6_isys_get_src_stream_by_src_pad(sd, remote_pad->index);
436 
437 	struct v4l2_subdev_state *state =
438 		v4l2_subdev_lock_and_get_active_state(sd);
439 
440 	__format = v4l2_subdev_state_get_format(state, remote_pad->index,
441 						r_stream);
442 	if (__format)
443 		format = *__format;
444 
445 	v4l2_subdev_unlock_state(state);
446 
447 	if (!__format) {
448 		dev_dbg(dev, "failed to get %s: pad %d, stream:%d format\n",
449 			sd->entity.name, remote_pad->index, r_stream);
450 		return -EPIPE;
451 	}
452 
453 	if (format.width != ipu6_isys_get_frame_width(av) ||
454 	    format.height != ipu6_isys_get_frame_height(av)) {
455 		dev_dbg(dev, "wrong width or height %ux%u (%ux%u expected)\n",
456 			ipu6_isys_get_frame_width(av),
457 			ipu6_isys_get_frame_height(av), format.width,
458 			format.height);
459 		return -EINVAL;
460 	}
461 
462 	code = ipu6_isys_get_isys_format(ipu6_isys_get_format(av), 0)->code;
463 	if (format.code != code) {
464 		dev_dbg(dev, "wrong mbus code 0x%8.8x (0x%8.8x expected)\n",
465 			code, format.code);
466 		return -EINVAL;
467 	}
468 
469 	return 0;
470 }
471 
472 static void return_buffers(struct ipu6_isys_queue *aq,
473 			   enum vb2_buffer_state state)
474 {
475 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
476 	struct ipu6_isys_buffer *ib;
477 	bool need_reset = false;
478 	unsigned long flags;
479 
480 	spin_lock_irqsave(&aq->lock, flags);
481 	while (!list_empty(&aq->incoming)) {
482 		struct vb2_buffer *vb;
483 
484 		ib = list_first_entry(&aq->incoming, struct ipu6_isys_buffer,
485 				      head);
486 		vb = ipu6_isys_buffer_to_vb2_buffer(ib);
487 		list_del(&ib->head);
488 		spin_unlock_irqrestore(&aq->lock, flags);
489 
490 		vb2_buffer_done(vb, state);
491 
492 		spin_lock_irqsave(&aq->lock, flags);
493 	}
494 
495 	/*
496 	 * Something went wrong (FW crash / HW hang / not all buffers
497 	 * returned from isys) if there are still buffers queued in active
498 	 * queue. We have to clean up places a bit.
499 	 */
500 	while (!list_empty(&aq->active)) {
501 		struct vb2_buffer *vb;
502 
503 		ib = list_first_entry(&aq->active, struct ipu6_isys_buffer,
504 				      head);
505 		vb = ipu6_isys_buffer_to_vb2_buffer(ib);
506 
507 		list_del(&ib->head);
508 		spin_unlock_irqrestore(&aq->lock, flags);
509 
510 		vb2_buffer_done(vb, state);
511 
512 		spin_lock_irqsave(&aq->lock, flags);
513 		need_reset = true;
514 	}
515 
516 	spin_unlock_irqrestore(&aq->lock, flags);
517 
518 	if (need_reset) {
519 		mutex_lock(&av->isys->mutex);
520 		av->isys->need_reset = true;
521 		mutex_unlock(&av->isys->mutex);
522 	}
523 }
524 
525 static void ipu6_isys_stream_cleanup(struct ipu6_isys_video *av)
526 {
527 	video_device_pipeline_stop(&av->vdev);
528 	ipu6_isys_put_stream(av->stream);
529 	av->stream = NULL;
530 }
531 
532 static int start_streaming(struct vb2_queue *q, unsigned int count)
533 {
534 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(q);
535 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
536 	struct device *dev = &av->isys->adev->auxdev.dev;
537 	const struct ipu6_isys_pixelformat *pfmt =
538 		ipu6_isys_get_isys_format(ipu6_isys_get_format(av), 0);
539 	struct ipu6_isys_buffer_list __bl, *bl = NULL;
540 	struct ipu6_isys_stream *stream;
541 	struct media_pad *source_pad, *remote_pad;
542 	int nr_queues, ret;
543 
544 	dev_dbg(dev, "stream: %s: width %u, height %u, css pixelformat %u\n",
545 		av->vdev.name, ipu6_isys_get_frame_width(av),
546 		ipu6_isys_get_frame_height(av), pfmt->css_pixelformat);
547 
548 	remote_pad = media_pad_remote_pad_unique(&av->pad);
549 	if (IS_ERR(remote_pad)) {
550 		dev_dbg(dev, "failed to get remote pad\n");
551 		ret = PTR_ERR(remote_pad);
552 		goto out_return_buffers;
553 	}
554 
555 	source_pad = media_pad_remote_pad_unique(&remote_pad->entity->pads[0]);
556 	if (IS_ERR(source_pad)) {
557 		dev_dbg(dev, "No external source entity\n");
558 		ret = PTR_ERR(source_pad);
559 		goto out_return_buffers;
560 	}
561 
562 	ret = ipu6_isys_setup_video(av, remote_pad, source_pad, &nr_queues);
563 	if (ret < 0) {
564 		dev_dbg(dev, "failed to setup video\n");
565 		goto out_return_buffers;
566 	}
567 
568 	ret = ipu6_isys_link_fmt_validate(aq);
569 	if (ret) {
570 		dev_dbg(dev,
571 			"%s: link format validation failed (%d)\n",
572 			av->vdev.name, ret);
573 		goto out_pipeline_stop;
574 	}
575 
576 	ret = ipu6_isys_fw_open(av->isys);
577 	if (ret)
578 		goto out_pipeline_stop;
579 
580 	stream = av->stream;
581 	mutex_lock(&stream->mutex);
582 	if (!stream->nr_streaming) {
583 		ret = ipu6_isys_video_prepare_stream(av, source_pad->entity,
584 						     nr_queues);
585 		if (ret)
586 			goto out_fw_close;
587 	}
588 
589 	stream->nr_streaming++;
590 	dev_dbg(dev, "queue %u of %u\n", stream->nr_streaming,
591 		stream->nr_queues);
592 
593 	list_add(&aq->node, &stream->queues);
594 	ipu6_isys_configure_stream_watermark(av, source_pad->entity);
595 	ipu6_isys_update_stream_watermark(av, true);
596 
597 	if (stream->nr_streaming != stream->nr_queues)
598 		goto out;
599 
600 	bl = &__bl;
601 	ret = buffer_list_get(stream, bl);
602 	if (ret < 0) {
603 		dev_warn(dev, "no buffer available, DRIVER BUG?\n");
604 		goto out;
605 	}
606 
607 	ret = ipu6_isys_stream_start(av, bl);
608 	if (ret)
609 		goto out_stream_start;
610 
611 out:
612 	mutex_unlock(&stream->mutex);
613 
614 	return 0;
615 
616 out_stream_start:
617 	ipu6_isys_update_stream_watermark(av, false);
618 	list_del(&aq->node);
619 	stream->nr_streaming--;
620 
621 out_fw_close:
622 	mutex_unlock(&stream->mutex);
623 	ipu6_isys_fw_close(av->isys);
624 
625 out_pipeline_stop:
626 	ipu6_isys_stream_cleanup(av);
627 
628 out_return_buffers:
629 	return_buffers(aq, VB2_BUF_STATE_QUEUED);
630 
631 	return ret;
632 }
633 
634 static void stop_streaming(struct vb2_queue *q)
635 {
636 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(q);
637 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
638 	struct ipu6_isys_stream *stream = av->stream;
639 
640 	mutex_lock(&stream->mutex);
641 
642 	ipu6_isys_update_stream_watermark(av, false);
643 
644 	mutex_lock(&av->isys->stream_mutex);
645 	if (stream->nr_streaming == stream->nr_queues && stream->streaming)
646 		ipu6_isys_video_set_streaming(av, 0, NULL);
647 	list_del(&aq->node);
648 	mutex_unlock(&av->isys->stream_mutex);
649 
650 	stream->nr_streaming--;
651 	stream->streaming = 0;
652 	mutex_unlock(&stream->mutex);
653 
654 	ipu6_isys_stream_cleanup(av);
655 
656 	return_buffers(aq, VB2_BUF_STATE_ERROR);
657 
658 	ipu6_isys_fw_close(av->isys);
659 }
660 
661 static unsigned int
662 get_sof_sequence_by_timestamp(struct ipu6_isys_stream *stream, u64 time)
663 {
664 	struct ipu6_isys *isys = stream->isys;
665 	struct device *dev = &isys->adev->auxdev.dev;
666 	unsigned int i;
667 
668 	/*
669 	 * The timestamp is invalid as no TSC in some FPGA platform,
670 	 * so get the sequence from pipeline directly in this case.
671 	 */
672 	if (time == 0)
673 		return atomic_read(&stream->sequence) - 1;
674 
675 	for (i = 0; i < IPU6_ISYS_MAX_PARALLEL_SOF; i++)
676 		if (time == stream->seq[i].timestamp) {
677 			dev_dbg(dev, "sof: using seq nr %u for ts %llu\n",
678 				stream->seq[i].sequence, time);
679 			return stream->seq[i].sequence;
680 		}
681 
682 	for (i = 0; i < IPU6_ISYS_MAX_PARALLEL_SOF; i++)
683 		dev_dbg(dev, "sof: sequence %u, timestamp value %llu\n",
684 			stream->seq[i].sequence, stream->seq[i].timestamp);
685 
686 	return 0;
687 }
688 
689 static u64 get_sof_ns_delta(struct ipu6_isys_video *av, u64 timestamp)
690 {
691 	struct ipu6_bus_device *adev = av->isys->adev;
692 	struct ipu6_device *isp = adev->isp;
693 	u64 delta, tsc_now;
694 
695 	ipu6_buttress_tsc_read(isp, &tsc_now);
696 	if (!tsc_now)
697 		return 0;
698 
699 	delta = tsc_now - timestamp;
700 
701 	return ipu6_buttress_tsc_ticks_to_ns(delta, isp);
702 }
703 
704 static void
705 ipu6_isys_buf_calc_sequence_time(struct ipu6_isys_buffer *ib, u64 time)
706 {
707 	struct vb2_buffer *vb = ipu6_isys_buffer_to_vb2_buffer(ib);
708 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
709 	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(vb->vb2_queue);
710 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
711 	struct device *dev = &av->isys->adev->auxdev.dev;
712 	struct ipu6_isys_stream *stream = av->stream;
713 	u64 ns;
714 	u32 sequence;
715 
716 	ns = ktime_get_ns() - get_sof_ns_delta(av, time);
717 	sequence = get_sof_sequence_by_timestamp(stream, time);
718 
719 	vbuf->vb2_buf.timestamp = ns;
720 	vbuf->sequence = sequence;
721 
722 	dev_dbg(dev, "buf: %s: buffer done, CPU-timestamp:%lld, sequence:%d\n",
723 		av->vdev.name, ktime_get_ns(), sequence);
724 	dev_dbg(dev, "index:%d, vbuf timestamp:%lld\n", vb->index,
725 		vbuf->vb2_buf.timestamp);
726 }
727 
728 static void ipu6_isys_queue_buf_done(struct ipu6_isys_buffer *ib)
729 {
730 	struct vb2_buffer *vb = ipu6_isys_buffer_to_vb2_buffer(ib);
731 
732 	if (atomic_read(&ib->str2mmio_flag)) {
733 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
734 		/*
735 		 * Operation on buffer is ended with error and will be reported
736 		 * to the userspace when it is de-queued
737 		 */
738 		atomic_set(&ib->str2mmio_flag, 0);
739 	} else {
740 		vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
741 	}
742 }
743 
744 static void
745 ipu6_stream_buf_ready(struct ipu6_isys_stream *stream, u8 pin_id, u32 pin_addr,
746 		      u64 time, bool error_check)
747 {
748 	struct ipu6_isys_queue *aq = stream->output_pins_queue[pin_id];
749 	struct ipu6_isys *isys = stream->isys;
750 	struct device *dev = &isys->adev->auxdev.dev;
751 	struct ipu6_isys_buffer *ib;
752 	struct vb2_buffer *vb;
753 	unsigned long flags;
754 	bool first = true;
755 	struct vb2_v4l2_buffer *buf;
756 
757 	spin_lock_irqsave(&aq->lock, flags);
758 	if (list_empty(&aq->active)) {
759 		spin_unlock_irqrestore(&aq->lock, flags);
760 		dev_err(dev, "active queue empty\n");
761 		return;
762 	}
763 
764 	list_for_each_entry_reverse(ib, &aq->active, head) {
765 		struct ipu6_isys_video_buffer *ivb;
766 		struct vb2_v4l2_buffer *vvb;
767 		dma_addr_t addr;
768 
769 		vb = ipu6_isys_buffer_to_vb2_buffer(ib);
770 		vvb = to_vb2_v4l2_buffer(vb);
771 		ivb = vb2_buffer_to_ipu6_isys_video_buffer(vvb);
772 		addr = ivb->dma_addr;
773 
774 		if (pin_addr != addr) {
775 			if (first)
776 				dev_err(dev, "Unexpected buffer address %pad\n",
777 					&addr);
778 			first = false;
779 			continue;
780 		}
781 
782 		if (error_check) {
783 			/*
784 			 * Check for error message:
785 			 * 'IPU6_FW_ISYS_ERROR_HW_REPORTED_STR2MMIO'
786 			 */
787 			atomic_set(&ib->str2mmio_flag, 1);
788 		}
789 		dev_dbg(dev, "buffer: found buffer %pad\n", &addr);
790 
791 		buf = to_vb2_v4l2_buffer(vb);
792 		buf->field = V4L2_FIELD_NONE;
793 
794 		list_del(&ib->head);
795 		spin_unlock_irqrestore(&aq->lock, flags);
796 
797 		ipu6_isys_buf_calc_sequence_time(ib, time);
798 
799 		ipu6_isys_queue_buf_done(ib);
800 
801 		return;
802 	}
803 
804 	dev_err(dev, "Failed to find a matching video buffer\n");
805 
806 	spin_unlock_irqrestore(&aq->lock, flags);
807 }
808 
809 void ipu6_isys_queue_buf_ready(struct ipu6_isys_stream *stream,
810 			       struct ipu6_fw_isys_resp_info_abi *info)
811 {
812 	u64 time = (u64)info->timestamp[1] << 32 | info->timestamp[0];
813 	bool err = info->error_info.error == IPU6_FW_ISYS_ERROR_HW_REPORTED_STR2MMIO;
814 
815 	ipu6_stream_buf_ready(stream, info->pin_id, info->pin.addr, time, err);
816 }
817 
818 static const struct vb2_ops ipu6_isys_queue_ops = {
819 	.queue_setup = ipu6_isys_queue_setup,
820 	.buf_init = ipu6_isys_buf_init,
821 	.buf_prepare = ipu6_isys_buf_prepare,
822 	.buf_cleanup = ipu6_isys_buf_cleanup,
823 	.start_streaming = start_streaming,
824 	.stop_streaming = stop_streaming,
825 	.buf_queue = buf_queue,
826 };
827 
828 int ipu6_isys_queue_init(struct ipu6_isys_queue *aq)
829 {
830 	struct ipu6_isys *isys = ipu6_isys_queue_to_video(aq)->isys;
831 	struct ipu6_isys_video *av = ipu6_isys_queue_to_video(aq);
832 	struct ipu6_bus_device *adev = isys->adev;
833 	int ret;
834 
835 	/* no support for userptr */
836 	if (!aq->vbq.io_modes)
837 		aq->vbq.io_modes = VB2_MMAP | VB2_DMABUF;
838 
839 	aq->vbq.drv_priv = isys;
840 	aq->vbq.ops = &ipu6_isys_queue_ops;
841 	aq->vbq.lock = &av->mutex;
842 	aq->vbq.mem_ops = &vb2_dma_sg_memops;
843 	aq->vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844 	aq->vbq.min_queued_buffers = 1;
845 	aq->vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
846 
847 	ret = vb2_queue_init(&aq->vbq);
848 	if (ret)
849 		return ret;
850 
851 	aq->vbq.dev = &adev->isp->pdev->dev;
852 	spin_lock_init(&aq->lock);
853 	INIT_LIST_HEAD(&aq->active);
854 	INIT_LIST_HEAD(&aq->incoming);
855 
856 	return 0;
857 }
858